1 |
cebix |
1.1 |
/* |
2 |
|
|
* scsi_amiga.cpp - SCSI Manager, Amiga specific stuff |
3 |
|
|
* |
4 |
jlachmann |
1.7 |
* Basilisk II (C) 1997-2001 Christian Bauer |
5 |
cebix |
1.1 |
* |
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 |
8 |
|
|
* the Free Software Foundation; either version 2 of the License, or |
9 |
|
|
* (at your option) any later version. |
10 |
|
|
* |
11 |
|
|
* This program is distributed in the hope that it will be useful, |
12 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
|
|
* GNU General Public License for more details. |
15 |
|
|
* |
16 |
|
|
* You should have received a copy of the GNU General Public License |
17 |
|
|
* along with this program; if not, write to the Free Software |
18 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
19 |
|
|
*/ |
20 |
|
|
|
21 |
|
|
#include <exec/types.h> |
22 |
|
|
#include <exec/memory.h> |
23 |
|
|
#include <devices/trackdisk.h> |
24 |
|
|
#include <devices/scsidisk.h> |
25 |
jlachmann |
1.7 |
#define __USE_SYSBASE |
26 |
cebix |
1.1 |
#include <proto/exec.h> |
27 |
jlachmann |
1.7 |
#include <inline/exec.h> |
28 |
cebix |
1.1 |
|
29 |
|
|
#include "sysdeps.h" |
30 |
|
|
#include "main.h" |
31 |
|
|
#include "prefs.h" |
32 |
|
|
#include "user_strings.h" |
33 |
|
|
#include "scsi.h" |
34 |
|
|
|
35 |
|
|
#define DEBUG 0 |
36 |
|
|
#include "debug.h" |
37 |
|
|
|
38 |
|
|
|
39 |
|
|
// Global variables |
40 |
|
|
static struct SCSICmd scsi; |
41 |
|
|
|
42 |
|
|
static IOStdReq *ios[8*8]; // IORequests for 8 units and 8 LUNs each |
43 |
|
|
static IOStdReq *io; // Active IORequest (selected target) |
44 |
|
|
|
45 |
|
|
static struct MsgPort *the_port = NULL; // Message port for device communication |
46 |
|
|
|
47 |
|
|
static ULONG buffer_size; // Size of data buffer |
48 |
|
|
static UBYTE *buffer = NULL; // Pointer to data buffer |
49 |
cebix |
1.4 |
static ULONG buffer_memf; // Buffer memory flags |
50 |
cebix |
1.1 |
|
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 |
|
|
|
57 |
|
|
/* |
58 |
|
|
* Initialization |
59 |
|
|
*/ |
60 |
|
|
|
61 |
|
|
void SCSIInit(void) |
62 |
|
|
{ |
63 |
|
|
int id, lun; |
64 |
|
|
|
65 |
cebix |
1.4 |
int memtype = PrefsFindInt32("scsimemtype"); |
66 |
|
|
switch (memtype) { |
67 |
|
|
case 1: |
68 |
|
|
buffer_memf = MEMF_24BITDMA | MEMF_PUBLIC; |
69 |
|
|
break; |
70 |
|
|
case 2: |
71 |
|
|
buffer_memf = MEMF_ANY | MEMF_PUBLIC; |
72 |
|
|
break; |
73 |
|
|
default: |
74 |
|
|
buffer_memf = MEMF_CHIP | MEMF_PUBLIC; |
75 |
|
|
break; |
76 |
|
|
} |
77 |
|
|
|
78 |
cebix |
1.1 |
// Create port and buffers |
79 |
|
|
the_port = CreateMsgPort(); |
80 |
cebix |
1.4 |
buffer = (UBYTE *)AllocMem(buffer_size = 0x10000, buffer_memf); |
81 |
cebix |
1.1 |
sense_buffer = (UBYTE *)AllocMem(SENSE_LENGTH, MEMF_CHIP | MEMF_PUBLIC); |
82 |
|
|
if (the_port == NULL || buffer == NULL || sense_buffer == NULL) { |
83 |
cebix |
1.5 |
ErrorAlert(STR_NO_MEM_ERR); |
84 |
cebix |
1.1 |
QuitEmulator(); |
85 |
|
|
} |
86 |
|
|
|
87 |
|
|
// Create and open IORequests for all 8 units (and all 8 LUNs) |
88 |
|
|
for (id=0; id<8; id++) { |
89 |
|
|
for (lun=0; lun<8; lun++) |
90 |
|
|
ios[id*8+lun] = NULL; |
91 |
|
|
char prefs_name[16]; |
92 |
|
|
sprintf(prefs_name, "scsi%d", id); |
93 |
|
|
const char *str = PrefsFindString(prefs_name); |
94 |
|
|
if (str) { |
95 |
|
|
char dev_name[256]; |
96 |
|
|
ULONG dev_unit = 0; |
97 |
|
|
if (sscanf(str, "%[^/]/%ld", dev_name, &dev_unit) == 2) { |
98 |
|
|
for (lun=0; lun<8; lun++) { |
99 |
|
|
struct IOStdReq *io = (struct IOStdReq *)CreateIORequest(the_port, sizeof(struct IOStdReq)); |
100 |
|
|
if (io == NULL) |
101 |
|
|
continue; |
102 |
jlachmann |
1.7 |
if (OpenDevice((UBYTE *) dev_name, dev_unit + lun * 10, (struct IORequest *)io, 0)) { |
103 |
cebix |
1.1 |
DeleteIORequest(io); |
104 |
|
|
continue; |
105 |
|
|
} |
106 |
|
|
io->io_Data = &scsi; |
107 |
|
|
io->io_Length = sizeof(scsi); |
108 |
|
|
io->io_Command = HD_SCSICMD; |
109 |
|
|
ios[id*8+lun] = io; |
110 |
|
|
} |
111 |
|
|
} |
112 |
|
|
} |
113 |
|
|
} |
114 |
|
|
|
115 |
|
|
// Reset SCSI bus |
116 |
|
|
SCSIReset(); |
117 |
|
|
|
118 |
|
|
// Init SCSICmd |
119 |
|
|
memset(&scsi, 0, sizeof(scsi)); |
120 |
|
|
scsi.scsi_Command = cmd_buffer; |
121 |
|
|
scsi.scsi_SenseData = sense_buffer; |
122 |
|
|
scsi.scsi_SenseLength = SENSE_LENGTH; |
123 |
|
|
} |
124 |
|
|
|
125 |
|
|
|
126 |
|
|
/* |
127 |
|
|
* Deinitialization |
128 |
|
|
*/ |
129 |
|
|
|
130 |
|
|
void SCSIExit(void) |
131 |
|
|
{ |
132 |
|
|
// Close all devices |
133 |
|
|
for (int i=0; i<8; i++) |
134 |
|
|
for (int j=0; j<8; j++) { |
135 |
|
|
struct IOStdReq *io = ios[i*8+j]; |
136 |
|
|
if (io) { |
137 |
|
|
CloseDevice((struct IORequest *)io); |
138 |
|
|
DeleteIORequest(io); |
139 |
|
|
} |
140 |
|
|
} |
141 |
|
|
|
142 |
|
|
// Delete port and buffers |
143 |
|
|
if (the_port) |
144 |
|
|
DeleteMsgPort(the_port); |
145 |
|
|
if (buffer) |
146 |
|
|
FreeMem(buffer, buffer_size); |
147 |
|
|
if (sense_buffer) |
148 |
|
|
FreeMem(sense_buffer, SENSE_LENGTH); |
149 |
|
|
} |
150 |
|
|
|
151 |
|
|
|
152 |
|
|
/* |
153 |
|
|
* Check if requested data size fits into buffer, allocate new buffer if needed |
154 |
|
|
*/ |
155 |
|
|
|
156 |
|
|
static bool try_buffer(int size) |
157 |
|
|
{ |
158 |
|
|
if (size <= buffer_size) |
159 |
|
|
return true; |
160 |
|
|
|
161 |
cebix |
1.4 |
UBYTE *new_buffer = (UBYTE *)AllocMem(size, buffer_memf); |
162 |
cebix |
1.1 |
if (new_buffer == NULL) |
163 |
|
|
return false; |
164 |
|
|
FreeMem(buffer, buffer_size); |
165 |
|
|
buffer = new_buffer; |
166 |
|
|
buffer_size = size; |
167 |
|
|
return true; |
168 |
|
|
} |
169 |
|
|
|
170 |
|
|
|
171 |
|
|
/* |
172 |
|
|
* Set SCSI command to be sent by scsi_send_cmd() |
173 |
|
|
*/ |
174 |
|
|
|
175 |
|
|
void scsi_set_cmd(int cmd_length, uint8 *cmd) |
176 |
|
|
{ |
177 |
|
|
scsi.scsi_CmdLength = cmd_length; |
178 |
|
|
memcpy(cmd_buffer, cmd, cmd_length); |
179 |
|
|
} |
180 |
|
|
|
181 |
|
|
|
182 |
|
|
/* |
183 |
|
|
* Check for presence of SCSI target |
184 |
|
|
*/ |
185 |
|
|
|
186 |
|
|
bool scsi_is_target_present(int id) |
187 |
|
|
{ |
188 |
|
|
return ios[id * 8] != NULL; |
189 |
|
|
} |
190 |
|
|
|
191 |
|
|
|
192 |
|
|
/* |
193 |
|
|
* Set SCSI target (returns false on error) |
194 |
|
|
*/ |
195 |
|
|
|
196 |
|
|
bool scsi_set_target(int id, int lun) |
197 |
|
|
{ |
198 |
|
|
struct IOStdReq *new_io = ios[id * 8 + lun]; |
199 |
|
|
if (new_io == NULL) |
200 |
|
|
return false; |
201 |
|
|
if (new_io != io) |
202 |
|
|
scsi.scsi_SenseActual = 0; // Clear sense data when selecting new target |
203 |
|
|
io = new_io; |
204 |
|
|
return true; |
205 |
|
|
} |
206 |
|
|
|
207 |
|
|
|
208 |
|
|
/* |
209 |
|
|
* Send SCSI command to active target (scsi_set_command() must have been called), |
210 |
|
|
* read/write data according to S/G table (returns false on error); timeout is in 1/60 sec |
211 |
|
|
*/ |
212 |
|
|
|
213 |
|
|
bool scsi_send_cmd(size_t data_length, bool reading, int sg_size, uint8 **sg_ptr, uint32 *sg_len, uint16 *stat, uint32 timeout) |
214 |
|
|
{ |
215 |
|
|
// Check if buffer is large enough, allocate new buffer if needed |
216 |
|
|
if (!try_buffer(data_length)) { |
217 |
|
|
char str[256]; |
218 |
|
|
sprintf(str, GetString(STR_SCSI_BUFFER_ERR), data_length); |
219 |
|
|
ErrorAlert(str); |
220 |
|
|
return false; |
221 |
|
|
} |
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; |
232 |
|
|
} |
233 |
|
|
} |
234 |
|
|
|
235 |
|
|
// Request Sense and autosense data valid? |
236 |
|
|
BYTE res = 0; |
237 |
|
|
if (cmd_buffer[0] == 0x03 && scsi.scsi_SenseActual) { |
238 |
|
|
|
239 |
|
|
// Yes, fake command |
240 |
|
|
D(bug(" autosense\n")); |
241 |
|
|
memcpy(buffer, sense_buffer, scsi.scsi_SenseActual); |
242 |
|
|
scsi.scsi_Status = 0; |
243 |
|
|
|
244 |
|
|
} else { |
245 |
|
|
|
246 |
|
|
// No, send regular command |
247 |
|
|
D(bug(" sending command, length %ld\n", data_length)); |
248 |
|
|
scsi.scsi_Data = (UWORD *)buffer; |
249 |
|
|
scsi.scsi_Length = data_length; |
250 |
|
|
scsi.scsi_Actual = 0; |
251 |
|
|
scsi.scsi_Flags = (reading ? SCSIF_READ : SCSIF_WRITE) | SCSIF_AUTOSENSE; |
252 |
|
|
scsi.scsi_SenseActual = 0; |
253 |
|
|
scsi.scsi_Status = 0; |
254 |
|
|
res = DoIO((struct IORequest *)io); |
255 |
|
|
D(bug(" command sent, res %d, status %d\n", res, scsi.scsi_Status)); |
256 |
|
|
*stat = scsi.scsi_Status; |
257 |
|
|
} |
258 |
|
|
|
259 |
|
|
// Process S/G table when reading |
260 |
cebix |
1.4 |
if (reading && res == 0) { |
261 |
cebix |
1.1 |
D(bug(" reading from buffer\n")); |
262 |
|
|
uint8 *buffer_ptr = buffer; |
263 |
|
|
for (int i=0; i<sg_size; i++) { |
264 |
|
|
uint32 len = sg_len[i]; |
265 |
|
|
D(bug(" %d bytes to %08lx\n", len, sg_ptr[i])); |
266 |
|
|
memcpy(sg_ptr[i], buffer_ptr, len); |
267 |
|
|
buffer_ptr += len; |
268 |
|
|
} |
269 |
|
|
} |
270 |
|
|
return res == 0; |
271 |
|
|
} |