]> git.sesse.net Git - pistorm/blob - platforms/amiga/piscsi/device_driver_amiga/piscsi-amiga.c
757078a5a3861354a8bf22e15e7cb0597f36d9ed
[pistorm] / platforms / amiga / piscsi / device_driver_amiga / piscsi-amiga.c
1 /*
2  * Based on:
3  * Amiga ZZ9000 USB Storage Driver (ZZ9000USBStorage.device)
4  * Copyright (C) 2016-2020, Lukas F. Hartmann <lukas@mntre.com>
5  * Based on code Copyright (C) 2016, Jason S. McMullan <jason.mcmullan@gmail.com>
6  * All rights reserved.
7  *
8  * Licensed under the MIT License:
9  *
10  * Permission is hereby granted, free of charge, to any person obtaining
11  * a copy of this software and associated documentation files (the "Software"),
12  * to deal in the Software without restriction, including without limitation
13  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14  * and/or sell copies of the Software, and to permit persons to whom the
15  * Software is furnished to do so, subject to the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be included
18  * in all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26  * DEALINGS IN THE SOFTWARE.
27  */
28
29 #include <exec/resident.h>
30 #include <exec/errors.h>
31 #include <exec/memory.h>
32 #include <exec/lists.h>
33 #include <exec/alerts.h>
34 #include <exec/tasks.h>
35 #include <exec/io.h>
36 #include <exec/execbase.h>
37
38 #include <libraries/expansion.h>
39
40 #include <devices/trackdisk.h>
41 #include <devices/timer.h>
42 #include <devices/scsidisk.h>
43
44 #include <dos/filehandler.h>
45
46 #include <proto/exec.h>
47 #include <proto/disk.h>
48 #include <proto/expansion.h>
49
50 #include <clib/debug_protos.h>
51 #include <stdint.h>
52 #include <stdlib.h>
53 #include "../piscsi-enums.h"
54
55 #define WRITESHORT(cmd, val) *(unsigned short *)((unsigned long)(PISCSI_OFFSET+cmd)) = val;
56 #define WRITELONG(cmd, val) *(unsigned long *)((unsigned long)(PISCSI_OFFSET+cmd)) = val;
57 #define WRITEBYTE(cmd, val) *(unsigned char *)((unsigned long)(PISCSI_OFFSET+cmd)) = val;
58
59 #define READSHORT(cmd, var) var = *(volatile unsigned short *)(PISCSI_OFFSET + cmd);
60 #define READLONG(cmd, var) var = *(volatile unsigned long *)(PISCSI_OFFSET + cmd);
61
62 #pragma pack(4)
63 struct piscsi_base {
64     struct Device* pi_dev;
65     struct piscsi_unit {
66         struct Unit unit;
67         uint32_t regs_ptr;
68
69         uint8_t enabled;
70         uint8_t present;
71         uint8_t valid;
72         uint8_t read_only;
73         uint8_t motor;
74         uint8_t unit_num;
75         uint16_t h, s;
76         uint32_t c;
77
78         uint32_t change_num;
79     } units[NUM_UNITS];
80 };
81
82 struct ExecBase* SysBase = NULL;
83
84 const char DevName[] = "pi-scsi.device";
85 const char DevIdString[] = "Pi-SCSI 0.1";
86
87 const UWORD DevVersion = 1;
88 const UWORD DevRevision = 0;
89
90 #include "stabs.h"
91
92 struct piscsi_base *dev_base = NULL;
93
94 struct WBStartup *_WBenchMsg = NULL;
95
96 //#define exit(...)
97 //#define debug(...)
98 #define KPrintF(...)
99
100 //#define bug(x,args...) KPrintF(x ,##args);
101 //#define debug(x,args...) bug("%s:%ld " x "\n", __func__, (unsigned long)__LINE__ ,##args)
102
103 uint8_t piscsi_perform_io(struct piscsi_unit *u, struct IORequest *io);
104 uint8_t piscsi_rw(struct piscsi_unit *u, struct IORequest *io, uint32_t offset, uint8_t write);
105 uint8_t piscsi_scsi(struct piscsi_unit *u, struct IORequest *io);
106
107 extern void* DOSBase[2];
108
109 uint32_t __UserDevInit(struct Device* dev) {
110     //uint8_t* registers = NULL;
111     SysBase = *(struct ExecBase **)4L;
112
113     KPrintF("Initializing devices.\n");
114
115     dev_base = AllocMem(sizeof(struct piscsi_base), MEMF_PUBLIC | MEMF_CLEAR);
116     dev_base->pi_dev = dev;
117
118     for (int i = 0; i < NUM_UNITS; i++) {
119         uint16_t r = 0;
120         WRITESHORT(PISCSI_CMD_DRVNUM, i);
121         dev_base->units[i].regs_ptr = PISCSI_OFFSET;
122         READSHORT(PISCSI_CMD_DRVTYPE, r);
123         dev_base->units[i].enabled = r;
124         dev_base->units[i].present = r;
125         dev_base->units[i].valid = r;
126         dev_base->units[i].unit_num = i;
127         if (dev_base->units[i].present) {
128             READLONG(PISCSI_CMD_CYLS, dev_base->units[i].c);
129             READSHORT(PISCSI_CMD_HEADS, dev_base->units[i].h);
130             READSHORT(PISCSI_CMD_SECS, dev_base->units[i].s);
131             KPrintF("C/H/S: %ld / %ld / %ld\n", dev_base->units[i].c, dev_base->units[i].h, dev_base->units[i].s);
132         }
133         dev_base->units[i].change_num++;
134         // Send any reset signal to the "SCSI" device here.
135     }
136
137     return 1;
138 }
139
140 uint32_t __UserDevCleanup(void) {
141     KPrintF("Cleaning up.\n");
142     FreeMem(dev_base, sizeof(struct piscsi_base));
143     return 0;
144 }
145
146 uint32_t __UserDevOpen(struct IOExtTD *iotd, uint32_t num, uint32_t flags) {
147     struct Node* node = (struct Node*)iotd;
148     int io_err = IOERR_OPENFAIL;
149
150     int unit_num = 0;
151     WRITELONG(PISCSI_CMD_DRVNUM, num);
152     READLONG(PISCSI_CMD_DRVNUM, unit_num);
153
154     KPrintF("Opening device %ld Flags: %ld (%lx)\n", unit_num, flags, flags);
155
156     if (iotd && unit_num < NUM_UNITS) {
157         if (dev_base->units[unit_num].enabled && dev_base->units[unit_num].present) {
158             io_err = 0;
159             iotd->iotd_Req.io_Unit = (struct Unit*)&dev_base->units[unit_num].unit;
160             iotd->iotd_Req.io_Unit->unit_flags = UNITF_ACTIVE;
161             iotd->iotd_Req.io_Unit->unit_OpenCnt = 1;
162         }
163     }
164
165 skip:;
166     iotd->iotd_Req.io_Error = io_err;
167
168     return io_err;
169 }
170
171 uint32_t __UserDevClose(struct IOExtTD *iotd) {
172   return 0;
173 }
174
175 void exit(int status) { }
176
177 int main() {
178     return 0;
179 }
180
181 ADDTABL_1(__BeginIO,a1);
182 void __BeginIO(struct IORequest *io) {
183     if (dev_base == NULL || io == NULL)
184         return;
185     
186     struct piscsi_unit *u;
187     struct Node* node = (struct Node*)io;
188     u = (struct piscsi_unit *)io->io_Unit;
189
190     if (node == NULL || u == NULL)
191         return;
192
193     KPrintF("io_Command = %ld, io_Flags = 0x%lx quick = %lx\n", io->io_Command, io->io_Flags, (io->io_Flags & IOF_QUICK));
194     io->io_Error = piscsi_perform_io(u, io);
195
196     if (!(io->io_Flags & IOF_QUICK)) {
197         ReplyMsg(&io->io_Message);
198     }
199 }
200
201 ADDTABL_1(__AbortIO,a1);
202 void __AbortIO(struct IORequest* io) {
203     KPrintF("AbortIO!\n");
204     if (!io) return;
205     io->io_Error = IOERR_ABORTED;
206 }
207
208 uint8_t piscsi_rw(struct piscsi_unit *u, struct IORequest *io, uint32_t offset, uint8_t write) {
209     struct IOStdReq *iostd = (struct IOStdReq *)io;
210     struct IOExtTD *iotd = (struct IOExtTD *)io;
211
212     uint8_t* data;
213     uint32_t len, num_blocks;
214     uint32_t block, max_addr;
215     uint8_t sderr;
216
217     data = iotd->iotd_Req.io_Data;
218     len = iotd->iotd_Req.io_Length;
219     //uint32_t offset2 = iostd->io_Offset;
220
221     max_addr = 0xffffffff;
222
223     // well... if we had 64 bits this would make sense
224     if ((offset > max_addr) || (offset+len > max_addr))
225         return IOERR_BADADDRESS;
226     if (data == 0)
227         return IOERR_BADADDRESS;
228     if (len < PISCSI_BLOCK_SIZE) {
229         iostd->io_Actual = 0;
230         return IOERR_BADLENGTH;
231     }
232
233     //block = offset;// >> SD_SECTOR_SHIFT;
234     //num_blocks = len;// >> SD_SECTOR_SHIFT;
235     sderr = 0;
236
237     if (write) {
238         //uint32_t retries = 10;
239         //KPrintF("Write %lx -> %lx %lx\n", (uint32_t)data, offset, len);
240         WRITELONG(PISCSI_CMD_ADDR1, (offset >> 9));
241         WRITELONG(PISCSI_CMD_ADDR2, len);
242         WRITELONG(PISCSI_CMD_ADDR3, (uint32_t)data);
243         WRITESHORT(PISCSI_CMD_WRITE, 1);
244     } else {
245         //KPrintF("read %lx %lx -> %lx\n", offset, len, (uint32_t)data);
246         WRITELONG(PISCSI_CMD_ADDR1, (offset >> 9));
247         WRITELONG(PISCSI_CMD_ADDR2, len);
248         WRITELONG(PISCSI_CMD_ADDR3, (uint32_t)data);
249         WRITESHORT(PISCSI_CMD_READ, 1);
250     }
251
252     if (sderr) {
253         iostd->io_Actual = 0;
254
255         if (sderr & SCSIERR_TIMEOUT)
256             return TDERR_DiskChanged;
257         if (sderr & SCSIERR_PARAM)
258             return TDERR_SeekError;
259         if (sderr & SCSIERR_ADDRESS)
260             return TDERR_SeekError;
261         if (sderr & (SCSIERR_ERASESEQ | SCSIERR_ERASERES))
262             return TDERR_BadSecPreamble;
263         if (sderr & SCSIERR_CRC)
264             return TDERR_BadSecSum;
265         if (sderr & SCSIERR_ILLEGAL)
266             return TDERR_TooFewSecs;
267         if (sderr & SCSIERR_IDLE)
268             return TDERR_PostReset;
269
270         return TDERR_SeekError;
271     } else {
272         iostd->io_Actual = len;
273     }
274
275     return 0;
276 }
277
278 #define PISCSI_ID_STRING "PISTORM Fake SCSI Disk  0.1 1111111111111111"
279
280 uint8_t piscsi_scsi(struct piscsi_unit *u, struct IORequest *io)
281 {
282     struct IOStdReq *iostd = (struct IOStdReq *)io;
283     struct SCSICmd *scsi = iostd->io_Data;
284     //uint8_t* registers = sdu->sdu_Registers;
285     uint8_t *data = (uint8_t *)scsi->scsi_Data;
286     uint32_t i, block, blocks, maxblocks;
287     uint8_t err = 0;
288
289     KPrintF("SCSI len=%ld, cmd = %02lx %02lx %02lx ... (%ld)\n",
290         iostd->io_Length, scsi->scsi_Command[0],
291         scsi->scsi_Command[1], scsi->scsi_Command[2],
292         scsi->scsi_CmdLength);
293
294     //maxblocks = u->s * u->c * u->h;
295
296     if (scsi->scsi_CmdLength < 6) {
297         //KPrintF("SCSICMD BADLENGTH2");
298         return IOERR_BADLENGTH;
299     }
300
301     if (scsi->scsi_Command == NULL) {
302         //KPrintF("SCSICMD IOERR_BADADDRESS1");
303         return IOERR_BADADDRESS;
304     }
305
306     scsi->scsi_Actual = 0;
307     //iostd->io_Actual = sizeof(*scsi);
308
309     switch (scsi->scsi_Command[0]) {
310         case 0x00:      // TEST_UNIT_READY
311             KPrintF("SCSI command: Test Unit Ready.\n");
312             err = 0;
313             break;
314         
315         case 0x12:      // INQUIRY
316             KPrintF("SCSI command: Inquiry.\n");
317             for (i = 0; i < scsi->scsi_Length; i++) {
318                 uint8_t val = 0;
319
320                 switch (i) {
321                     case 0: // SCSI device type: direct-access device
322                         val = (0 << 5) | 0;
323                         break;
324                     case 1: // RMB = 1
325                         val = (1 << 7);
326                         break;
327                     case 2: // VERSION = 0
328                         val = 0;
329                         break;
330                     case 3: // NORMACA=0, HISUP = 0, RESPONSE_DATA_FORMAT = 2
331                         val = (0 << 5) | (0 << 4) | 2;
332                         break;
333                     case 4: // ADDITIONAL_LENGTH = 44 - 4
334                         val = 44 - 4;
335                         break;
336                     default:
337                         if (i >= 8 && i < 44)
338                             val = PISCSI_ID_STRING[i - 8];
339                         else
340                             val = 0;
341                         break;
342                 }
343                 data[i] = val;
344             }
345             scsi->scsi_Actual = i;
346             err = 0;
347             break;
348         
349         case 0x08: // READ (6)
350         case 0x0a: // WRITE (6)
351             block = scsi->scsi_Command[1] & 0x1f;
352             block = (block << 8) | scsi->scsi_Command[2];
353             block = (block << 8) | scsi->scsi_Command[3];
354             blocks = scsi->scsi_Command[4];
355
356             READLONG(PISCSI_CMD_BLOCKS, maxblocks);
357             if (block + blocks > maxblocks) {
358                 err = IOERR_BADADDRESS;
359                 break;
360             }
361             /*if (scsi->scsi_Length < (blocks << SD_SECTOR_SHIFT)) {
362                 err = IOERR_BADLENGTH;
363                 break;
364             }*/
365             if (data == NULL) {
366                 err = IOERR_BADADDRESS;
367                 break;
368             }
369
370             if (scsi->scsi_Command[0] == 0x08) {
371                 //KPrintF("scsi_read %lx %lx\n",block,blocks);
372                 KPrintF("SCSI read %lx %lx -> %lx\n", block, blocks, (uint32_t)data);
373                 WRITELONG(PISCSI_CMD_ADDR2, block);
374                 WRITELONG(PISCSI_CMD_ADDR2, (blocks << 9));
375                 WRITELONG(PISCSI_CMD_ADDR3, (uint32_t)data);
376                 WRITESHORT(PISCSI_CMD_READ, 1);
377             }
378             else {
379                 //KPrintF("scsi_write %lx %lx\n",block,blocks);
380                 KPrintF("SCSI write %lx -> %lx %lx\n", (uint32_t)data, block, blocks);
381                 WRITELONG(PISCSI_CMD_ADDR2, block);
382                 WRITELONG(PISCSI_CMD_ADDR2, (blocks << 9));
383                 WRITELONG(PISCSI_CMD_ADDR3, (uint32_t)data);
384                 WRITESHORT(PISCSI_CMD_WRITE, 1);
385             }
386
387             scsi->scsi_Actual = scsi->scsi_Length;
388             err = 0;
389             break;
390         
391         case 0x25: // READ CAPACITY (10)
392             KPrintF("SCSI command: Read Capacity.\n");
393             if (scsi->scsi_CmdLength < 10) {
394                 err = HFERR_BadStatus;
395                 break;
396             }
397
398             block = *((uint32_t*)&scsi->scsi_Command[2]);
399
400             /*if ((scsi->scsi_Command[8] & 1) || block != 0) {
401                 // PMI Not supported
402                 KPrintF("PMI not supported.\n");
403                 err = HFERR_BadStatus;
404                 break;
405             }*/
406
407             if (scsi->scsi_Length < 8) {
408                 err = IOERR_BADLENGTH;
409                 break;
410             }
411
412             READLONG(PISCSI_CMD_BLOCKS, blocks);
413             ((uint32_t*)data)[0] = blocks - 1;
414             ((uint32_t*)data)[1] = PISCSI_BLOCK_SIZE;
415
416             scsi->scsi_Actual = 8;
417             err = 0;
418
419             break;
420         case 0x1a: // MODE SENSE (6)    
421             KPrintF("SCSI command: Mode Sense.\n");
422             data[0] = 3 + 8 + 0x16;
423             data[1] = 0; // MEDIUM TYPE
424             data[2] = 0;
425             data[3] = 8;
426
427             READLONG(PISCSI_CMD_BLOCKS, maxblocks);
428             (blocks = (maxblocks - 1) & 0xFFFFFF);
429
430             *((uint32_t *)&data[4]) = blocks;
431             *((uint32_t *)&data[8]) = PISCSI_BLOCK_SIZE;
432
433             switch (((UWORD)scsi->scsi_Command[2] << 8) | scsi->scsi_Command[3]) {
434                 case 0x0300: { // Format Device Mode
435                     KPrintF("Grabbing SCSI format device mode data.\n");
436                     uint8_t *datext = data + 12;
437                     datext[0] = 0x03;
438                     datext[1] = 0x16;
439                     datext[2] = 0x00;
440                     datext[3] = 0x01;
441                     *((uint32_t *)&datext[4]) = 0;
442                     *((uint32_t *)&datext[8]) = 0;
443                     *((uint16_t *)&datext[10]) = u->s;
444                     *((uint16_t *)&datext[12]) = PISCSI_BLOCK_SIZE;
445                     datext[14] = 0x00;
446                     datext[15] = 0x01;
447                     *((uint32_t *)&datext[16]) = 0;
448                     datext[20] = 0x80;
449
450                     scsi->scsi_Actual = data[0] + 1;
451                     err = 0;
452                     break;
453                 }
454                 case 0x0400: // Rigid Drive Geometry
455                     KPrintF("Grabbing SCSI rigid drive geometry.\n");
456                     uint8_t *datext = data + 12;
457                     datext[0] = 0x04;
458                     *((uint32_t *)&datext[1]) = u->c;
459                     datext[1] = 0x16;
460                     datext[5] = u->h;
461                     datext[6] = 0x00;
462                     *((uint32_t *)&datext[6]) = 0;
463                     *((uint32_t *)&datext[10]) = 0;
464                     *((uint32_t *)&datext[13]) = u->c;
465                     datext[17] = 0;
466                     *((uint32_t *)&datext[18]) = 0;
467                     *((uint16_t *)&datext[20]) = 5400;
468
469                     scsi->scsi_Actual = data[0] + 1;
470                     err = 0;
471                     break;
472                 
473                 default:
474                     KPrintF("[WARN] Unhandled mode sense thing: %lx\n", ((UWORD)scsi->scsi_Command[2] << 8) | scsi->scsi_Command[3]);
475                     err = HFERR_BadStatus;
476                     break;
477             }
478             break;
479         
480         case 0x37: // READ DEFECT DATA (10)
481             
482             break;
483
484         default:
485             KPrintF("Unknown/unhandled SCSI command %lx.\n", scsi->scsi_Command[0]);
486             err = HFERR_BadStatus;
487             break;
488     }
489
490     if (err != 0) {
491         KPrintF("Some SCSI error occured: %ld\n", err);
492         scsi->scsi_Actual = 0;
493     }
494
495     return err;
496 }
497
498 #define DUMMYCMD iostd->io_Actual = 0; break;
499 uint8_t piscsi_perform_io(struct piscsi_unit *u, struct IORequest *io) {
500     struct IOStdReq *iostd = (struct IOStdReq *)io;
501     struct IOExtTD *iotd = (struct IOExtTD *)io;
502
503     uint8_t *data;
504     uint32_t len;
505     uint32_t offset;
506     //struct DriveGeometry *geom;
507     uint8_t err = 0;
508
509     if (!u->enabled) {
510         return IOERR_OPENFAIL;
511     }
512
513     data = iotd->iotd_Req.io_Data;
514     len = iotd->iotd_Req.io_Length;
515
516     if (io->io_Error == IOERR_ABORTED) {
517         return io->io_Error;
518     }
519
520     //KPrintF("cmd: %s\n",cmd_name(io->io_Command));
521     //KPrintF("IO %lx Start, io_Flags = %ld, io_Command = %ld\n", io, io->io_Flags, io->io_Command);
522
523     switch (io->io_Command) {
524         case CMD_CLEAR:
525             /* Invalidate read buffer */
526             DUMMYCMD;
527         case CMD_UPDATE:
528             /* Flush write buffer */
529             DUMMYCMD;
530         case TD_PROTSTATUS:
531             DUMMYCMD;
532         case TD_CHANGENUM:
533             iostd->io_Actual = u->change_num;
534             break;
535         case TD_REMOVE:
536             DUMMYCMD;
537         case TD_CHANGESTATE:
538             DUMMYCMD;
539         case TD_GETDRIVETYPE:
540             iostd->io_Actual = DG_DIRECT_ACCESS;
541             break;
542         case TD_MOTOR:
543             iostd->io_Actual = u->motor;
544             u->motor = iostd->io_Length ? 1 : 0;
545             break;
546
547         case TD_FORMAT:
548             offset = iotd->iotd_Req.io_Offset;
549             //err = 0;
550             err = piscsi_rw(u, io, offset, 1);
551             break;
552         case CMD_WRITE:
553             offset = iotd->iotd_Req.io_Offset;
554             //err = 0;
555             err = piscsi_rw(u, io, offset, 1);
556             break;
557         case CMD_READ:
558             offset = iotd->iotd_Req.io_Offset;
559             //err = 0;
560             err = piscsi_rw(u, io, offset, 0);
561             break;
562         case HD_SCSICMD:
563             //err = 0;
564             err = piscsi_scsi(u, io);
565             break;
566         default: {
567             int cmd = io->io_Command;
568             KPrintF("Unknown IO command: %ld\n", cmd);
569             err = IOERR_NOCMD;
570             break;
571         }
572     }
573
574     return err;
575 }
576 #undef DUMMYCMD
577
578 ADDTABL_END();