ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/AmigaOS/scsi_amiga.cpp
Revision: 1.9
Committed: 2008-01-01T09:40:31Z (16 years, 10 months ago) by gbeauche
Branch: MAIN
CVS Tags: HEAD
Changes since 1.8: +1 -1 lines
Log Message:
Happy New Year!

File Contents

# User Rev Content
1 cebix 1.1 /*
2     * scsi_amiga.cpp - SCSI Manager, Amiga specific stuff
3     *
4 gbeauche 1.9 * Basilisk II (C) 1997-2008 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 cebix 1.8 static bool direct_transfers_supported = false; // Direct data transfers (bypassing the buffer) are supported
57    
58 cebix 1.1
59     /*
60     * Initialization
61     */
62    
63     void SCSIInit(void)
64     {
65     int id, lun;
66    
67 cebix 1.4 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 cebix 1.8 direct_transfers_supported = true;
75 cebix 1.4 break;
76     default:
77     buffer_memf = MEMF_CHIP | MEMF_PUBLIC;
78     break;
79     }
80    
81 cebix 1.1 // Create port and buffers
82     the_port = CreateMsgPort();
83 cebix 1.4 buffer = (UBYTE *)AllocMem(buffer_size = 0x10000, buffer_memf);
84 cebix 1.1 sense_buffer = (UBYTE *)AllocMem(SENSE_LENGTH, MEMF_CHIP | MEMF_PUBLIC);
85     if (the_port == NULL || buffer == NULL || sense_buffer == NULL) {
86 cebix 1.5 ErrorAlert(STR_NO_MEM_ERR);
87 cebix 1.1 QuitEmulator();
88     }
89    
90     // Create and open IORequests for all 8 units (and all 8 LUNs)
91     for (id=0; id<8; id++) {
92     for (lun=0; lun<8; lun++)
93     ios[id*8+lun] = NULL;
94     char prefs_name[16];
95     sprintf(prefs_name, "scsi%d", id);
96     const char *str = PrefsFindString(prefs_name);
97     if (str) {
98     char dev_name[256];
99     ULONG dev_unit = 0;
100     if (sscanf(str, "%[^/]/%ld", dev_name, &dev_unit) == 2) {
101     for (lun=0; lun<8; lun++) {
102     struct IOStdReq *io = (struct IOStdReq *)CreateIORequest(the_port, sizeof(struct IOStdReq));
103     if (io == NULL)
104     continue;
105 jlachmann 1.7 if (OpenDevice((UBYTE *) dev_name, dev_unit + lun * 10, (struct IORequest *)io, 0)) {
106 cebix 1.1 DeleteIORequest(io);
107     continue;
108     }
109     io->io_Data = &scsi;
110     io->io_Length = sizeof(scsi);
111     io->io_Command = HD_SCSICMD;
112     ios[id*8+lun] = io;
113     }
114     }
115     }
116     }
117    
118     // Reset SCSI bus
119     SCSIReset();
120    
121     // Init SCSICmd
122     memset(&scsi, 0, sizeof(scsi));
123     scsi.scsi_Command = cmd_buffer;
124     scsi.scsi_SenseData = sense_buffer;
125     scsi.scsi_SenseLength = SENSE_LENGTH;
126     }
127    
128    
129     /*
130     * Deinitialization
131     */
132    
133     void SCSIExit(void)
134     {
135     // Close all devices
136     for (int i=0; i<8; i++)
137     for (int j=0; j<8; j++) {
138     struct IOStdReq *io = ios[i*8+j];
139     if (io) {
140     CloseDevice((struct IORequest *)io);
141     DeleteIORequest(io);
142     }
143     }
144    
145     // Delete port and buffers
146     if (the_port)
147     DeleteMsgPort(the_port);
148     if (buffer)
149     FreeMem(buffer, buffer_size);
150     if (sense_buffer)
151     FreeMem(sense_buffer, SENSE_LENGTH);
152     }
153    
154    
155     /*
156     * Check if requested data size fits into buffer, allocate new buffer if needed
157     */
158    
159     static bool try_buffer(int size)
160     {
161     if (size <= buffer_size)
162     return true;
163    
164 cebix 1.4 UBYTE *new_buffer = (UBYTE *)AllocMem(size, buffer_memf);
165 cebix 1.1 if (new_buffer == NULL)
166     return false;
167     FreeMem(buffer, buffer_size);
168     buffer = new_buffer;
169     buffer_size = size;
170     return true;
171     }
172    
173    
174     /*
175     * Set SCSI command to be sent by scsi_send_cmd()
176     */
177    
178     void scsi_set_cmd(int cmd_length, uint8 *cmd)
179     {
180     scsi.scsi_CmdLength = cmd_length;
181     memcpy(cmd_buffer, cmd, cmd_length);
182     }
183    
184    
185     /*
186     * Check for presence of SCSI target
187     */
188    
189     bool scsi_is_target_present(int id)
190     {
191     return ios[id * 8] != NULL;
192     }
193    
194    
195     /*
196     * Set SCSI target (returns false on error)
197     */
198    
199     bool scsi_set_target(int id, int lun)
200     {
201     struct IOStdReq *new_io = ios[id * 8 + lun];
202     if (new_io == NULL)
203     return false;
204     if (new_io != io)
205     scsi.scsi_SenseActual = 0; // Clear sense data when selecting new target
206     io = new_io;
207     return true;
208     }
209    
210    
211     /*
212     * Send SCSI command to active target (scsi_set_command() must have been called),
213     * read/write data according to S/G table (returns false on error); timeout is in 1/60 sec
214     */
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 cebix 1.8 // 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     // 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 cebix 1.1
231 cebix 1.8 // 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 cebix 1.1 }
242     }
243    
244     // Request Sense and autosense data valid?
245     BYTE res = 0;
246     if (cmd_buffer[0] == 0x03 && scsi.scsi_SenseActual) {
247    
248     // Yes, fake command
249     D(bug(" autosense\n"));
250     memcpy(buffer, sense_buffer, scsi.scsi_SenseActual);
251     scsi.scsi_Status = 0;
252 cebix 1.8 do_direct_transfer = false;
253 cebix 1.1
254     } else {
255    
256     // No, send regular command
257     D(bug(" sending command, length %ld\n", data_length));
258 cebix 1.8 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 cebix 1.1 scsi.scsi_Actual = 0;
266     scsi.scsi_Flags = (reading ? SCSIF_READ : SCSIF_WRITE) | SCSIF_AUTOSENSE;
267     scsi.scsi_SenseActual = 0;
268     scsi.scsi_Status = 0;
269     res = DoIO((struct IORequest *)io);
270     D(bug(" command sent, res %d, status %d\n", res, scsi.scsi_Status));
271     *stat = scsi.scsi_Status;
272     }
273    
274 cebix 1.8 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 cebix 1.1 }
287     }
288     return res == 0;
289     }