1 |
|
/* |
2 |
|
* scsi_amiga.cpp - SCSI Manager, Amiga specific stuff |
3 |
|
* |
4 |
< |
* Basilisk II (C) 1997-2001 Christian Bauer |
4 |
> |
* Basilisk II (C) 1997-2008 Christian Bauer |
5 |
|
* |
6 |
|
* This program is free software; you can redistribute it and/or modify |
7 |
|
* it under the terms of the GNU General Public License as published by |
22 |
|
#include <exec/memory.h> |
23 |
|
#include <devices/trackdisk.h> |
24 |
|
#include <devices/scsidisk.h> |
25 |
+ |
#define __USE_SYSBASE |
26 |
|
#include <proto/exec.h> |
27 |
+ |
#include <inline/exec.h> |
28 |
|
|
29 |
|
#include "sysdeps.h" |
30 |
|
#include "main.h" |
46 |
|
|
47 |
|
static ULONG buffer_size; // Size of data buffer |
48 |
|
static UBYTE *buffer = NULL; // Pointer to data buffer |
49 |
+ |
static ULONG buffer_memf; // Buffer memory flags |
50 |
|
|
51 |
|
static UBYTE cmd_buffer[12]; // Buffer for SCSI command |
52 |
|
|
53 |
|
const int SENSE_LENGTH = 256; |
54 |
|
static UBYTE *sense_buffer = NULL; // Buffer for autosense data |
55 |
|
|
56 |
+ |
static bool direct_transfers_supported = false; // Direct data transfers (bypassing the buffer) are supported |
57 |
+ |
|
58 |
|
|
59 |
|
/* |
60 |
|
* Initialization |
64 |
|
{ |
65 |
|
int id, lun; |
66 |
|
|
67 |
+ |
int memtype = PrefsFindInt32("scsimemtype"); |
68 |
+ |
switch (memtype) { |
69 |
+ |
case 1: |
70 |
+ |
buffer_memf = MEMF_24BITDMA | MEMF_PUBLIC; |
71 |
+ |
break; |
72 |
+ |
case 2: |
73 |
+ |
buffer_memf = MEMF_ANY | MEMF_PUBLIC; |
74 |
+ |
direct_transfers_supported = true; |
75 |
+ |
break; |
76 |
+ |
default: |
77 |
+ |
buffer_memf = MEMF_CHIP | MEMF_PUBLIC; |
78 |
+ |
break; |
79 |
+ |
} |
80 |
+ |
|
81 |
|
// Create port and buffers |
82 |
|
the_port = CreateMsgPort(); |
83 |
< |
buffer = (UBYTE *)AllocMem(buffer_size = 0x10000, MEMF_CHIP | MEMF_PUBLIC); |
83 |
> |
buffer = (UBYTE *)AllocMem(buffer_size = 0x10000, buffer_memf); |
84 |
|
sense_buffer = (UBYTE *)AllocMem(SENSE_LENGTH, MEMF_CHIP | MEMF_PUBLIC); |
85 |
|
if (the_port == NULL || buffer == NULL || sense_buffer == NULL) { |
86 |
< |
ErrorAlert(GetString(STR_NO_MEM_ERR)); |
86 |
> |
ErrorAlert(STR_NO_MEM_ERR); |
87 |
|
QuitEmulator(); |
88 |
|
} |
89 |
|
|
102 |
|
struct IOStdReq *io = (struct IOStdReq *)CreateIORequest(the_port, sizeof(struct IOStdReq)); |
103 |
|
if (io == NULL) |
104 |
|
continue; |
105 |
< |
if (OpenDevice((UBYTE *)dev_name, dev_unit + lun * 10, (struct IORequest *)io, 0)) { |
105 |
> |
if (OpenDevice((UBYTE *) dev_name, dev_unit + lun * 10, (struct IORequest *)io, 0)) { |
106 |
|
DeleteIORequest(io); |
107 |
|
continue; |
108 |
|
} |
161 |
|
if (size <= buffer_size) |
162 |
|
return true; |
163 |
|
|
164 |
< |
UBYTE *new_buffer = (UBYTE *)AllocMem(size, MEMF_CHIP | MEMF_PUBLIC); |
164 |
> |
UBYTE *new_buffer = (UBYTE *)AllocMem(size, buffer_memf); |
165 |
|
if (new_buffer == NULL) |
166 |
|
return false; |
167 |
|
FreeMem(buffer, buffer_size); |
215 |
|
|
216 |
|
bool scsi_send_cmd(size_t data_length, bool reading, int sg_size, uint8 **sg_ptr, uint32 *sg_len, uint16 *stat, uint32 timeout) |
217 |
|
{ |
218 |
< |
// Check if buffer is large enough, allocate new buffer if needed |
219 |
< |
if (!try_buffer(data_length)) { |
220 |
< |
char str[256]; |
221 |
< |
sprintf(str, GetString(STR_SCSI_BUFFER_ERR), data_length); |
203 |
< |
ErrorAlert(str); |
204 |
< |
return false; |
205 |
< |
} |
218 |
> |
// Bypass the buffer if there's only one S/G table entry |
219 |
> |
bool do_direct_transfer = (sg_size == 1 && ((uint32)sg_ptr[0] & 1) == 0 && direct_transfers_supported); |
220 |
> |
|
221 |
> |
if (!do_direct_transfer) { |
222 |
|
|
223 |
< |
// Process S/G table when writing |
224 |
< |
if (!reading) { |
225 |
< |
D(bug(" writing to buffer\n")); |
226 |
< |
uint8 *buffer_ptr = buffer; |
227 |
< |
for (int i=0; i<sg_size; i++) { |
228 |
< |
uint32 len = sg_len[i]; |
229 |
< |
D(bug(" %d bytes from %08lx\n", len, sg_ptr[i])); |
230 |
< |
memcpy(buffer_ptr, sg_ptr[i], len); |
231 |
< |
buffer_ptr += len; |
223 |
> |
// Check if buffer is large enough, allocate new buffer if needed |
224 |
> |
if (!try_buffer(data_length)) { |
225 |
> |
char str[256]; |
226 |
> |
sprintf(str, GetString(STR_SCSI_BUFFER_ERR), data_length); |
227 |
> |
ErrorAlert(str); |
228 |
> |
return false; |
229 |
> |
} |
230 |
> |
|
231 |
> |
// Process S/G table when writing |
232 |
> |
if (!reading) { |
233 |
> |
D(bug(" writing to buffer\n")); |
234 |
> |
uint8 *buffer_ptr = buffer; |
235 |
> |
for (int i=0; i<sg_size; i++) { |
236 |
> |
uint32 len = sg_len[i]; |
237 |
> |
D(bug(" %d bytes from %08lx\n", len, sg_ptr[i])); |
238 |
> |
memcpy(buffer_ptr, sg_ptr[i], len); |
239 |
> |
buffer_ptr += len; |
240 |
> |
} |
241 |
|
} |
242 |
|
} |
243 |
|
|
249 |
|
D(bug(" autosense\n")); |
250 |
|
memcpy(buffer, sense_buffer, scsi.scsi_SenseActual); |
251 |
|
scsi.scsi_Status = 0; |
252 |
+ |
do_direct_transfer = false; |
253 |
|
|
254 |
|
} else { |
255 |
|
|
256 |
|
// No, send regular command |
257 |
|
D(bug(" sending command, length %ld\n", data_length)); |
258 |
< |
scsi.scsi_Data = (UWORD *)buffer; |
259 |
< |
scsi.scsi_Length = data_length; |
258 |
> |
if (do_direct_transfer) { |
259 |
> |
scsi.scsi_Data = (UWORD *)sg_ptr[0]; |
260 |
> |
scsi.scsi_Length = sg_len[0]; |
261 |
> |
} else { |
262 |
> |
scsi.scsi_Data = (UWORD *)buffer; |
263 |
> |
scsi.scsi_Length = data_length; |
264 |
> |
} |
265 |
|
scsi.scsi_Actual = 0; |
266 |
|
scsi.scsi_Flags = (reading ? SCSIF_READ : SCSIF_WRITE) | SCSIF_AUTOSENSE; |
267 |
|
scsi.scsi_SenseActual = 0; |
271 |
|
*stat = scsi.scsi_Status; |
272 |
|
} |
273 |
|
|
274 |
< |
// Process S/G table when reading |
275 |
< |
if (res == 0) { |
276 |
< |
D(bug(" reading from buffer\n")); |
277 |
< |
uint8 *buffer_ptr = buffer; |
278 |
< |
for (int i=0; i<sg_size; i++) { |
279 |
< |
uint32 len = sg_len[i]; |
280 |
< |
D(bug(" %d bytes to %08lx\n", len, sg_ptr[i])); |
281 |
< |
memcpy(sg_ptr[i], buffer_ptr, len); |
282 |
< |
buffer_ptr += len; |
274 |
> |
if (!do_direct_transfer) { |
275 |
> |
|
276 |
> |
// Process S/G table when reading |
277 |
> |
if (reading && res == 0) { |
278 |
> |
D(bug(" reading from buffer\n")); |
279 |
> |
uint8 *buffer_ptr = buffer; |
280 |
> |
for (int i=0; i<sg_size; i++) { |
281 |
> |
uint32 len = sg_len[i]; |
282 |
> |
D(bug(" %d bytes to %08lx\n", len, sg_ptr[i])); |
283 |
> |
memcpy(sg_ptr[i], buffer_ptr, len); |
284 |
> |
buffer_ptr += len; |
285 |
> |
} |
286 |
|
} |
287 |
|
} |
288 |
|
return res == 0; |