db378549251841572592e3608ad2e73e46c74688
[SCSI2SD.git] / software / SCSI2SD / src / scsi.c
1 //      Copyright (C) 2014 Michael McMaster <michael@codesrc.com>\r
2 //\r
3 //      This file is part of SCSI2SD.\r
4 //\r
5 //      SCSI2SD is free software: you can redistribute it and/or modify\r
6 //      it under the terms of the GNU General Public License as published by\r
7 //      the Free Software Foundation, either version 3 of the License, or\r
8 //      (at your option) any later version.\r
9 //\r
10 //      SCSI2SD is distributed in the hope that it will be useful,\r
11 //      but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12 //      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13 //      GNU General Public License for more details.\r
14 //\r
15 //      You should have received a copy of the GNU General Public License\r
16 //      along with SCSI2SD.  If not, see <http://www.gnu.org/licenses/>.\r
17 \r
18 #include "device.h"\r
19 #include "scsi.h"\r
20 #include "scsiPhy.h"\r
21 #include "config.h"\r
22 #include "bits.h"\r
23 #include "diagnostic.h"\r
24 #include "disk.h"\r
25 #include "inquiry.h"\r
26 #include "led.h"\r
27 #include "mode.h"\r
28 #include "disk.h"\r
29 #include "time.h"\r
30 #include "cdrom.h"\r
31 #include "debug.h"\r
32 #include "tape.h"\r
33 #include "mo.h"\r
34 #include "vendor.h"\r
35 \r
36 #include <string.h>\r
37 \r
38 // Global SCSI device state.\r
39 ScsiDevice scsiDev;\r
40 \r
41 static void enter_SelectionPhase(void);\r
42 static void process_SelectionPhase(void);\r
43 static void enter_MessageIn(uint8 message);\r
44 static void enter_Status(uint8 status);\r
45 static void enter_DataIn(int len);\r
46 static void process_DataIn(void);\r
47 static void process_DataOut(void);\r
48 static void process_Command(void);\r
49 \r
50 static void doReserveRelease(void);\r
51 \r
52 void enter_BusFree()\r
53 {\r
54         // This delay probably isn't needed for most SCSI hosts, but it won't\r
55         // hurt either. It's possible some of the samplers needed this delay.\r
56         if (scsiDev.compatMode < COMPAT_SCSI2)\r
57         {\r
58                 CyDelayUs(2);\r
59         }\r
60 \r
61         if (scsiDev.status != GOOD && isDebugEnabled())\r
62         {\r
63                 // We want to capture debug information for failure cases.\r
64                 CyDelay(64);\r
65         }\r
66 \r
67         SCSI_ClearPin(SCSI_Out_BSY);\r
68         // We now have a Bus Clear Delay of 800ns to release remaining signals.\r
69         SCSI_CTL_PHASE_Write(0);\r
70 \r
71         // Wait for the initiator to cease driving signals\r
72         // Bus settle delay + bus clear delay = 1200ns\r
73         CyDelayUs(2);\r
74 \r
75         ledOff();\r
76         scsiDev.phase = BUS_FREE;\r
77         scsiDev.selFlag = 0;\r
78 }\r
79 \r
80 static void enter_MessageIn(uint8 message)\r
81 {\r
82         scsiDev.msgIn = message;\r
83         scsiDev.phase = MESSAGE_IN;\r
84 }\r
85 \r
86 int process_MessageIn(int releaseBusFree)\r
87 {\r
88         scsiEnterPhase(MESSAGE_IN);\r
89         scsiWriteByte(scsiDev.msgIn);\r
90 \r
91         if (unlikely(scsiDev.atnFlag))\r
92         {\r
93                 // If there was a parity error, we go\r
94                 // back to MESSAGE_OUT first, get out parity error message, then come\r
95                 // back here.\r
96                 return 0;\r
97         }\r
98         else if ((scsiDev.msgIn == MSG_LINKED_COMMAND_COMPLETE) ||\r
99                 (scsiDev.msgIn == MSG_LINKED_COMMAND_COMPLETE_WITH_FLAG))\r
100         {\r
101                 // Go back to the command phase and start again.\r
102                 scsiDev.phase = COMMAND;\r
103                 scsiDev.parityError = 0;\r
104                 scsiDev.dataPtr = 0;\r
105                 scsiDev.savedDataPtr = 0;\r
106                 scsiDev.dataLen = 0;\r
107                 scsiDev.status = GOOD;\r
108                 transfer.blocks = 0;\r
109                 transfer.currentBlock = 0;\r
110                 return 0;\r
111         }\r
112         else if (releaseBusFree) /*if (scsiDev.msgIn == MSG_COMMAND_COMPLETE)*/\r
113         {\r
114                 enter_BusFree();\r
115                 return 1;\r
116         }\r
117         else\r
118         {\r
119                 return 1;\r
120         }\r
121 }\r
122 \r
123 static void messageReject()\r
124 {\r
125         scsiEnterPhase(MESSAGE_IN);\r
126         scsiWriteByte(MSG_REJECT);\r
127 }\r
128 \r
129 static void enter_Status(uint8 status)\r
130 {\r
131         scsiDev.status = status;\r
132         scsiDev.phase = STATUS;\r
133 \r
134         scsiDev.lastStatus = scsiDev.status;\r
135         scsiDev.lastSense = scsiDev.target->sense.code;\r
136         scsiDev.lastSenseASC = scsiDev.target->sense.asc;\r
137 }\r
138 \r
139 void process_Status()\r
140 {\r
141         scsiEnterPhase(STATUS);\r
142 \r
143         uint8 message;\r
144 \r
145         uint8 control = scsiDev.cdb[scsiDev.cdbLen - 1];\r
146 \r
147         if (scsiDev.target->cfg->quirks == CONFIG_QUIRKS_OMTI)\r
148         {\r
149                 // OMTI non-standard LINK control\r
150                 if (control & 0x01)\r
151                 {\r
152                         scsiDev.phase = COMMAND;\r
153                         return;\r
154                 }\r
155         }\r
156 \r
157         if ((scsiDev.status == GOOD) && (control & 0x01) &&\r
158                 scsiDev.target->cfg->quirks != CONFIG_QUIRKS_XEBEC)\r
159         {\r
160                 // Linked command.\r
161                 scsiDev.status = INTERMEDIATE;\r
162                 if (control & 0x02)\r
163                 {\r
164                         message = MSG_LINKED_COMMAND_COMPLETE_WITH_FLAG;\r
165                 }\r
166                 else\r
167                 {\r
168                         message = MSG_LINKED_COMMAND_COMPLETE;\r
169                 }\r
170         }\r
171         else\r
172         {\r
173                 message = MSG_COMMAND_COMPLETE;\r
174         }\r
175 \r
176 \r
177         if (scsiDev.target->cfg->quirks == CONFIG_QUIRKS_XEBEC)\r
178         {\r
179                 // More non-standardness. Expects 2 status bytes (really status + msg)\r
180                 // 00 d 000 err 0\r
181                 // d == disk number\r
182                 // ERR = 1 if error.\r
183                 if (scsiDev.status == GOOD)\r
184                 {\r
185                         scsiWriteByte(scsiDev.cdb[1] & 0x20);\r
186                 }\r
187                 else\r
188                 {\r
189                         scsiWriteByte((scsiDev.cdb[1] & 0x20) | 0x2);\r
190                 }\r
191                 CyDelayUs(10); // Seems to need a delay before changing phase bits.\r
192         }\r
193         else if (scsiDev.target->cfg->quirks == CONFIG_QUIRKS_OMTI)\r
194         {\r
195                 scsiDev.status |= (scsiDev.target->targetId & 0x03) << 5;\r
196                 scsiWriteByte(scsiDev.status);\r
197         }\r
198         else\r
199         {\r
200                 scsiWriteByte(scsiDev.status);\r
201         }\r
202 \r
203         scsiDev.lastStatus = scsiDev.status;\r
204         scsiDev.lastSense = scsiDev.target->sense.code;\r
205         scsiDev.lastSenseASC = scsiDev.target->sense.asc;\r
206 \r
207 \r
208         // Command Complete occurs AFTER a valid status has been\r
209         // sent. then we go bus-free.\r
210         enter_MessageIn(message);\r
211 }\r
212 \r
213 static void enter_DataIn(int len)\r
214 {\r
215         scsiDev.dataLen = len;\r
216         scsiDev.phase = DATA_IN;\r
217 }\r
218 \r
219 static void process_DataIn()\r
220 {\r
221         uint32 len;\r
222 \r
223         if (scsiDev.dataLen > sizeof(scsiDev.data))\r
224         {\r
225                 scsiDev.dataLen = sizeof(scsiDev.data);\r
226         }\r
227 \r
228         len = scsiDev.dataLen - scsiDev.dataPtr;\r
229         if (len > 0)\r
230         {\r
231                 scsiEnterPhase(DATA_IN);\r
232                 scsiWrite(scsiDev.data + scsiDev.dataPtr, len);\r
233                 scsiDev.dataPtr += len;\r
234         }\r
235 \r
236         if ((scsiDev.dataPtr >= scsiDev.dataLen) &&\r
237                 (transfer.currentBlock == transfer.blocks))\r
238         {\r
239                 enter_Status(GOOD);\r
240         }\r
241 }\r
242 \r
243 static void process_DataOut()\r
244 {\r
245         uint32 len;\r
246 \r
247         if (scsiDev.dataLen > sizeof(scsiDev.data))\r
248         {\r
249                 scsiDev.dataLen = sizeof(scsiDev.data);\r
250         }\r
251 \r
252         scsiDev.parityError = 0;\r
253         len = scsiDev.dataLen - scsiDev.dataPtr;\r
254         if (len > 0)\r
255         {\r
256                 scsiEnterPhase(DATA_OUT);\r
257 \r
258                 scsiRead(scsiDev.data + scsiDev.dataPtr, len);\r
259                 scsiDev.dataPtr += len;\r
260 \r
261                 if (scsiDev.parityError &&\r
262                         (scsiDev.boardCfg.flags & CONFIG_ENABLE_PARITY) &&\r
263                         (scsiDev.compatMode >= COMPAT_SCSI2))\r
264                 {\r
265                         scsiDev.target->sense.code = ABORTED_COMMAND;\r
266                         scsiDev.target->sense.asc = SCSI_PARITY_ERROR;\r
267                         enter_Status(CHECK_CONDITION);\r
268                 }\r
269         }\r
270 \r
271         if ((scsiDev.dataPtr >= scsiDev.dataLen) &&\r
272                 (transfer.currentBlock == transfer.blocks))\r
273         {\r
274                 if (scsiDev.postDataOutHook != NULL)\r
275                 {\r
276                         scsiDev.postDataOutHook();\r
277                 }\r
278                 else\r
279                 {\r
280                         enter_Status(GOOD);\r
281                 }\r
282         }\r
283 }\r
284 \r
285 static const uint8 CmdGroupBytes[8] = {6, 10, 10, 6, 6, 12, 6, 6};\r
286 static void process_Command()\r
287 {\r
288         int group;\r
289         uint8 command;\r
290         uint8 control;\r
291 \r
292         scsiEnterPhase(COMMAND);\r
293         scsiDev.parityError = 0;\r
294 \r
295         memset(scsiDev.cdb, 0, sizeof(scsiDev.cdb));\r
296         scsiDev.cdb[0] = scsiReadByte();\r
297 \r
298         group = scsiDev.cdb[0] >> 5;\r
299         scsiDev.cdbLen = CmdGroupBytes[group];\r
300         scsiRead(scsiDev.cdb + 1, scsiDev.cdbLen - 1);\r
301 \r
302         command = scsiDev.cdb[0];\r
303 \r
304         // Prefer LUN's set by IDENTIFY messages for newer hosts.\r
305         if (scsiDev.lun < 0)\r
306         {\r
307                 if (command == 0xE0 || command == 0xE4) // XEBEC s1410\r
308                 {\r
309                         scsiDev.lun = 0;\r
310                 }\r
311                 else\r
312                 {\r
313                         scsiDev.lun = scsiDev.cdb[1] >> 5;\r
314                 }\r
315         }\r
316 \r
317         // For Philips P2000C with Xebec S1410 SASI/MFM adapter\r
318         // http://bitsavers.trailing-edge.com/pdf/xebec/104524C_S1410Man_Aug83.pdf\r
319         if ((scsiDev.lun > 0) && (scsiDev.boardCfg.flags & CONFIG_MAP_LUNS_TO_IDS))\r
320         {\r
321                 int tgtIndex;\r
322                 for (tgtIndex = 0; tgtIndex < MAX_SCSI_TARGETS; ++tgtIndex)\r
323                 {\r
324                         if (scsiDev.targets[tgtIndex].targetId == scsiDev.lun)\r
325                         {\r
326                                 scsiDev.target = &scsiDev.targets[tgtIndex];\r
327                                 scsiDev.lun = 0;\r
328                                 break;\r
329                         }\r
330                 }\r
331         }\r
332 \r
333 \r
334         control = scsiDev.cdb[scsiDev.cdbLen - 1];\r
335 \r
336         scsiDev.cmdCount++;\r
337         const TargetConfig* cfg = scsiDev.target->cfg;\r
338 \r
339         if (unlikely(scsiDev.resetFlag))\r
340         {\r
341                 // Don't log bogus commands\r
342                 scsiDev.cmdCount--;\r
343                 memset(scsiDev.cdb, 0xff, sizeof(scsiDev.cdb));\r
344                 return;\r
345         }\r
346         else if (scsiDev.parityError &&\r
347                 (scsiDev.boardCfg.flags & CONFIG_ENABLE_PARITY) &&\r
348                 (scsiDev.compatMode >= COMPAT_SCSI2))\r
349         {\r
350                 scsiDev.target->sense.code = ABORTED_COMMAND;\r
351                 scsiDev.target->sense.asc = SCSI_PARITY_ERROR;\r
352                 enter_Status(CHECK_CONDITION);\r
353         }\r
354         else if ((control & 0x02) && ((control & 0x01) == 0) &&\r
355                 // used for head step options on xebec.\r
356                 likely(scsiDev.target->cfg->quirks != CONFIG_QUIRKS_XEBEC))\r
357         {\r
358                 // FLAG set without LINK flag.\r
359                 scsiDev.target->sense.code = ILLEGAL_REQUEST;\r
360                 scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;\r
361                 enter_Status(CHECK_CONDITION);\r
362         }\r
363         else if (command == 0x12)\r
364         {\r
365                 scsiInquiry();\r
366         }\r
367         else if (command == 0x03)\r
368         {\r
369                 // REQUEST SENSE\r
370                 uint32 allocLength = scsiDev.cdb[4];\r
371 \r
372                 if (scsiDev.target->cfg->quirks == CONFIG_QUIRKS_XEBEC)\r
373                 {\r
374                         // Completely non-standard\r
375                         allocLength = 4;\r
376                         if (scsiDev.target->sense.code == NO_SENSE)\r
377                                 scsiDev.data[0] = 0;\r
378                         else if (scsiDev.target->sense.code == ILLEGAL_REQUEST)\r
379                                 scsiDev.data[0] = 0x20; // Illegal command\r
380                         else if (scsiDev.target->sense.code == NOT_READY)\r
381                                 scsiDev.data[0] = 0x04; // Drive not ready\r
382                         else\r
383                                 scsiDev.data[0] = 0x11;  // Uncorrectable data error\r
384 \r
385                         scsiDev.data[1] = (scsiDev.cdb[1] & 0x20) | ((transfer.lba >> 16) & 0x1F);\r
386                         scsiDev.data[2] = transfer.lba >> 8;\r
387                         scsiDev.data[3] = transfer.lba;\r
388 \r
389                 }\r
390                 else\r
391                 {\r
392                 // As specified by the SASI and SCSI1 standard.\r
393                 // Newer initiators won't be specifying 0 anyway.\r
394                 if (allocLength == 0) allocLength = 4;\r
395 \r
396                 memset(scsiDev.data, 0, 256); // Max possible alloc length\r
397                 scsiDev.data[0] = 0xF0;\r
398                 scsiDev.data[2] = scsiDev.target->sense.code & 0x0F;\r
399 \r
400                 scsiDev.data[3] = transfer.lba >> 24;\r
401                 scsiDev.data[4] = transfer.lba >> 16;\r
402                 scsiDev.data[5] = transfer.lba >> 8;\r
403                 scsiDev.data[6] = transfer.lba;\r
404 \r
405                 // Additional bytes if there are errors to report\r
406                 scsiDev.data[7] = 10; // additional length\r
407                 scsiDev.data[12] = scsiDev.target->sense.asc >> 8;\r
408                 scsiDev.data[13] = scsiDev.target->sense.asc;\r
409                 }\r
410 \r
411                 // Silently truncate results. SCSI-2 spec 8.2.14.\r
412                 enter_DataIn(allocLength);\r
413 \r
414                 // This is a good time to clear out old sense information.\r
415                 scsiDev.target->sense.code = NO_SENSE;\r
416                 scsiDev.target->sense.asc = NO_ADDITIONAL_SENSE_INFORMATION;\r
417         }\r
418         // Some old SCSI drivers do NOT properly support\r
419         // unitAttention. eg. the Mac Plus would trigger a SCSI reset\r
420         // on receiving the unit attention response on boot, thus\r
421         // triggering another unit attention condition.\r
422         else if (scsiDev.target->unitAttention &&\r
423                 (scsiDev.boardCfg.flags & CONFIG_ENABLE_UNIT_ATTENTION))\r
424         {\r
425                 scsiDev.target->sense.code = UNIT_ATTENTION;\r
426                 scsiDev.target->sense.asc = scsiDev.target->unitAttention;\r
427 \r
428                 // If initiator doesn't do REQUEST SENSE for the next command, then\r
429                 // data is lost.\r
430                 scsiDev.target->unitAttention = 0;\r
431 \r
432                 enter_Status(CHECK_CONDITION);\r
433         }\r
434         else if (scsiDev.lun)\r
435         {\r
436                 scsiDev.target->sense.code = ILLEGAL_REQUEST;\r
437                 scsiDev.target->sense.asc = LOGICAL_UNIT_NOT_SUPPORTED;\r
438                 enter_Status(CHECK_CONDITION);\r
439         }\r
440         else if (command == 0x17 || command == 0x16)\r
441         {\r
442                 doReserveRelease();\r
443         }\r
444         else if ((scsiDev.target->reservedId >= 0) &&\r
445                 (scsiDev.target->reservedId != scsiDev.initiatorId))\r
446         {\r
447                 enter_Status(CONFLICT);\r
448         }\r
449         // Handle odd device types first that may override basic read and\r
450         // write commands. Will fall-through to generic disk handling.\r
451         else if (((cfg->deviceType == CONFIG_OPTICAL) && scsiCDRomCommand()) ||\r
452                 ((cfg->deviceType == CONFIG_SEQUENTIAL) && scsiTapeCommand()) ||\r
453                 ((cfg->deviceType == CONFIG_MO) && scsiMOCommand()))\r
454         {\r
455                 // Already handled.\r
456         }\r
457         else if (scsiDiskCommand())\r
458         {\r
459                 // Already handled.\r
460                 // check for the performance-critical read/write\r
461                 // commands ASAP.\r
462         }\r
463         else if (command == 0x1C)\r
464         {\r
465                 scsiReceiveDiagnostic();\r
466         }\r
467         else if (command == 0x1D)\r
468         {\r
469                 scsiSendDiagnostic();\r
470         }\r
471         else if (command == 0x3B)\r
472         {\r
473                 scsiWriteBuffer();\r
474         }\r
475         else if (command == 0x3C)\r
476         {\r
477                 scsiReadBuffer();\r
478         }\r
479         else if (!scsiModeCommand() && !scsiVendorCommand())\r
480         {\r
481                 scsiDev.target->sense.code = ILLEGAL_REQUEST;\r
482                 scsiDev.target->sense.asc = INVALID_COMMAND_OPERATION_CODE;\r
483                 enter_Status(CHECK_CONDITION);\r
484         }\r
485 \r
486         // Successful\r
487         if (scsiDev.phase == COMMAND) // No status set, and not in DATA_IN\r
488         {\r
489                 enter_Status(GOOD);\r
490         }\r
491 \r
492 }\r
493 \r
494 static void doReserveRelease()\r
495 {\r
496         int extentReservation = scsiDev.cdb[1] & 1;\r
497         int thirdPty = scsiDev.cdb[1] & 0x10;\r
498         int thirdPtyId = (scsiDev.cdb[1] >> 1) & 0x7;\r
499         uint8 command = scsiDev.cdb[0];\r
500 \r
501         int canRelease =\r
502                 (!thirdPty && (scsiDev.initiatorId == scsiDev.target->reservedId)) ||\r
503                         (thirdPty &&\r
504                                 (scsiDev.target->reserverId == scsiDev.initiatorId) &&\r
505                                 (scsiDev.target->reservedId == thirdPtyId)\r
506                         );\r
507 \r
508         if (extentReservation)\r
509         {\r
510                 // Not supported.\r
511                 scsiDev.target->sense.code = ILLEGAL_REQUEST;\r
512                 scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;\r
513                 enter_Status(CHECK_CONDITION);\r
514         }\r
515         else if (command == 0x17) // release\r
516         {\r
517                 if ((scsiDev.target->reservedId < 0) || canRelease)\r
518                 {\r
519                         scsiDev.target->reservedId = -1;\r
520                         scsiDev.target->reserverId = -1;\r
521                 }\r
522                 else\r
523                 {\r
524                         enter_Status(CONFLICT);\r
525                 }\r
526         }\r
527         else // assume reserve.\r
528         {\r
529                 if ((scsiDev.target->reservedId < 0) || canRelease)\r
530                 {\r
531                         scsiDev.target->reserverId = scsiDev.initiatorId;\r
532                         if (thirdPty)\r
533                         {\r
534                                 scsiDev.target->reservedId = thirdPtyId;\r
535                         }\r
536                         else\r
537                         {\r
538                                 scsiDev.target->reservedId = scsiDev.initiatorId;\r
539                         }\r
540                 }\r
541                 else\r
542                 {\r
543                         // Already reserved by someone else!\r
544                         enter_Status(CONFLICT);\r
545                 }\r
546         }\r
547 }\r
548 \r
549 static void scsiReset()\r
550 {\r
551         scsiDev.rstCount++;\r
552         ledOff();\r
553 \r
554         scsiPhyReset();\r
555         SCSI_Out_Ctl_Write(0);\r
556 \r
557         scsiDev.parityError = 0;\r
558         scsiDev.phase = BUS_FREE;\r
559         scsiDev.atnFlag = 0;\r
560         scsiDev.resetFlag = 0;\r
561         scsiDev.selFlag = 0;\r
562         scsiDev.lun = -1;\r
563         scsiDev.compatMode = COMPAT_UNKNOWN;\r
564 \r
565         if (scsiDev.target)\r
566         {\r
567                 if (scsiDev.target->unitAttention != POWER_ON_RESET)\r
568                 {\r
569                         scsiDev.target->unitAttention = SCSI_BUS_RESET;\r
570                 }\r
571                 scsiDev.target->reservedId = -1;\r
572                 scsiDev.target->reserverId = -1;\r
573                 scsiDev.target->sense.code = NO_SENSE;\r
574                 scsiDev.target->sense.asc = NO_ADDITIONAL_SENSE_INFORMATION;\r
575         }\r
576         scsiDev.target = NULL;\r
577         scsiDiskReset();\r
578 \r
579         scsiDev.postDataOutHook = NULL;\r
580 \r
581         // Sleep to allow the bus to settle down a bit.\r
582         // We must be ready again within the "Reset to selection time" of\r
583         // 250ms.\r
584         // There is no guarantee that the RST line will be negated by then.\r
585         // NOTE: We could be connected and powered by USB for configuration,\r
586         // in which case TERMPWR cannot be supplied, and reset will ALWAYS\r
587         // be true. Therefore, the sleep here must be slow to avoid slowing\r
588         // USB comms\r
589         CyDelay(1); // 1ms.\r
590 }\r
591 \r
592 static void enter_SelectionPhase()\r
593 {\r
594         // Ignore stale versions of this flag, but ensure we know the\r
595         // current value if the flag is still set.\r
596         scsiDev.atnFlag = 0;\r
597         scsiDev.parityError = 0;\r
598         scsiDev.dataPtr = 0;\r
599         scsiDev.savedDataPtr = 0;\r
600         scsiDev.dataLen = 0;\r
601         scsiDev.status = GOOD;\r
602         scsiDev.phase = SELECTION;\r
603         scsiDev.lun = -1;\r
604         scsiDev.discPriv = 0;\r
605 \r
606         scsiDev.initiatorId = -1;\r
607         scsiDev.target = NULL;\r
608 \r
609         transfer.blocks = 0;\r
610         transfer.currentBlock = 0;\r
611 \r
612         scsiDev.postDataOutHook = NULL;\r
613 }\r
614 \r
615 static void process_SelectionPhase()\r
616 {\r
617         // Selection delays.\r
618         // Many SCSI1 samplers that use a 5380 chip need a delay of at least 1ms.\r
619         // The Mac Plus boot-time (ie. rom code) selection abort time\r
620         // is < 1ms and must have no delay (standard suggests 250ms abort time)\r
621         // Most newer SCSI2 hosts don't care either way.\r
622         if (scsiDev.target->cfg->quirks == CONFIG_QUIRKS_XEBEC)\r
623         {\r
624                 CyDelay(1); // Simply won't work if set to 0.\r
625         }\r
626         else if (scsiDev.boardCfg.selectionDelay == 255) // auto\r
627         {\r
628                 if (scsiDev.compatMode < COMPAT_SCSI2)\r
629                 {\r
630                         CyDelay(1);\r
631                 }\r
632         }\r
633         else if (scsiDev.boardCfg.selectionDelay != 0)\r
634         {\r
635                 CyDelay(scsiDev.boardCfg.selectionDelay);\r
636         }\r
637 \r
638         int selLatchCfg = scsiDev.boardCfg.flags & CONFIG_ENABLE_SEL_LATCH;\r
639         int sel = (selLatchCfg && scsiDev.selFlag) || SCSI_ReadFilt(SCSI_Filt_SEL);\r
640 \r
641         int bsy = SCSI_ReadFilt(SCSI_Filt_BSY);\r
642 #ifdef SCSI_In_IO\r
643         int io = SCSI_ReadPin(SCSI_In_IO);\r
644 #else\r
645         int io = 0;\r
646 #endif\r
647 \r
648         // Only read these pins AFTER SEL and BSY - we don't want to catch them\r
649         // during a transition period.\r
650         uint8 mask = (selLatchCfg && scsiDev.selFlag) ? scsiDev.selDBX : scsiReadDBxPins();\r
651         int maskBitCount = countBits(mask);\r
652         int goodParity = (Lookup_OddParity[mask] == SCSI_ReadPin(SCSI_In_DBP));\r
653         int atnFlag = SCSI_ReadFilt(SCSI_Filt_ATN);\r
654 \r
655         int tgtIndex;\r
656         TargetState* target = NULL;\r
657         for (tgtIndex = 0; tgtIndex < MAX_SCSI_TARGETS; ++tgtIndex)\r
658         {\r
659                 if (mask & (1 << scsiDev.targets[tgtIndex].targetId))\r
660                 {\r
661                         target = &scsiDev.targets[tgtIndex];\r
662                         break;\r
663                 }\r
664         }\r
665         sel &= (selLatchCfg && scsiDev.selFlag) || SCSI_ReadFilt(SCSI_Filt_SEL);\r
666         bsy |= SCSI_ReadFilt(SCSI_Filt_BSY);\r
667 #ifdef SCSI_In_IO\r
668         io |= SCSI_ReadPin(SCSI_In_IO);\r
669 #endif\r
670         if (!bsy && !io && sel &&\r
671                 target &&\r
672                 (goodParity || !(scsiDev.boardCfg.flags & CONFIG_ENABLE_PARITY) || !atnFlag) &&\r
673                 likely(maskBitCount <= 2))\r
674         {\r
675                 // We've been selected!\r
676                 // Assert BSY - Selection success!\r
677                 // must happen within 200us (Selection abort time) of seeing our\r
678                 // ID + SEL.\r
679                 // (Note: the initiator will be waiting the "Selection time-out delay"\r
680                 // for our BSY response, which is actually a very generous 250ms)\r
681                 SCSI_SetPin(SCSI_Out_BSY);\r
682                 ledOn();\r
683 \r
684                 scsiDev.target = target;\r
685 \r
686                 // Do we enter MESSAGE OUT immediately ? SCSI 1 and 2 standards says\r
687                 // move to MESSAGE OUT if ATN is true before we assert BSY.\r
688                 // The initiator should assert ATN with SEL.\r
689                 scsiDev.atnFlag = atnFlag;\r
690 \r
691 \r
692                 // Unit attention breaks many older SCSI hosts. Disable it completely\r
693                 // for SCSI-1 (and older) hosts, regardless of our configured setting.\r
694                 // Enable the compatability mode also as many SASI and SCSI1\r
695                 // controllers don't generate parity bits.\r
696                 if (!scsiDev.atnFlag)\r
697                 {\r
698                         target->unitAttention = 0;\r
699                         scsiDev.compatMode = COMPAT_SCSI1;\r
700                 }\r
701                 else if (!(scsiDev.boardCfg.flags & CONFIG_ENABLE_SCSI2))\r
702                 {\r
703                         scsiDev.compatMode = COMPAT_SCSI2_DISABLED;\r
704                 }\r
705                 else\r
706                 {\r
707                         scsiDev.compatMode = COMPAT_SCSI2;\r
708                 }\r
709 \r
710                 scsiDev.selCount++;\r
711 \r
712 \r
713                 // Save our initiator now that we're no longer in a time-critical\r
714                 // section.\r
715                 // SCSI1/SASI initiators may not set their own ID.\r
716                 {\r
717                         int i;\r
718                         uint8_t initiatorMask = mask ^ (1 << target->targetId);\r
719                         scsiDev.initiatorId = -1;\r
720                         for (i = 0; i < 8; ++i)\r
721                         {\r
722                                 if (initiatorMask & (1 << i))\r
723                                 {\r
724                                         scsiDev.initiatorId = i;\r
725                                         break;\r
726                                 }\r
727                         }\r
728                 }\r
729 \r
730                 // Wait until the end of the selection phase.\r
731                 uint32_t selTimerBegin = getTime_ms();\r
732                 while (likely(!scsiDev.resetFlag))\r
733                 {\r
734                         if (!SCSI_ReadFilt(SCSI_Filt_SEL))\r
735                         {\r
736                                 break;\r
737                         }\r
738                         else if (elapsedTime_ms(selTimerBegin) >= 250)\r
739                         {\r
740                                 SCSI_ClearPin(SCSI_Out_BSY);\r
741                                 scsiDev.resetFlag = 1;\r
742                                 break;\r
743                         }\r
744                 }\r
745 \r
746                 scsiDev.phase = COMMAND;\r
747         }\r
748         else if (!sel)\r
749         {\r
750                 scsiDev.phase = BUS_BUSY;\r
751         }\r
752 \r
753         scsiDev.selFlag = 0;\r
754 }\r
755 \r
756 static void process_MessageOut()\r
757 {\r
758         scsiEnterPhase(MESSAGE_OUT);\r
759 \r
760         scsiDev.atnFlag = 0;\r
761         scsiDev.parityError = 0;\r
762         scsiDev.msgOut = scsiReadByte();\r
763         scsiDev.msgCount++;\r
764 \r
765         if (scsiDev.parityError &&\r
766                 (scsiDev.boardCfg.flags & CONFIG_ENABLE_PARITY) &&\r
767                 (scsiDev.compatMode >= COMPAT_SCSI2))\r
768         {\r
769                 // Skip the remaining message bytes, and then start the MESSAGE_OUT\r
770                 // phase again from the start. The initiator will re-send the\r
771                 // same set of messages.\r
772                 while (SCSI_ReadFilt(SCSI_Filt_ATN) && !scsiDev.resetFlag)\r
773                 {\r
774                         scsiReadByte();\r
775                 }\r
776 \r
777                 // Go-back and try the message again.\r
778                 scsiDev.atnFlag = 1;\r
779                 scsiDev.parityError = 0;\r
780         }\r
781         else if (scsiDev.msgOut == 0x00)\r
782         {\r
783                 // COMMAND COMPLETE. but why would the target be receiving this ? nfi.\r
784                 enter_BusFree();\r
785         }\r
786         else if (scsiDev.msgOut == 0x06)\r
787         {\r
788                 // ABORT\r
789                 scsiDiskReset();\r
790                 enter_BusFree();\r
791         }\r
792         else if (scsiDev.msgOut == 0x0C)\r
793         {\r
794                 // BUS DEVICE RESET\r
795 \r
796                 scsiDiskReset();\r
797 \r
798                 scsiDev.target->unitAttention = SCSI_BUS_RESET;\r
799 \r
800                 // ANY initiator can reset the reservation state via this message.\r
801                 scsiDev.target->reservedId = -1;\r
802                 scsiDev.target->reserverId = -1;\r
803                 enter_BusFree();\r
804         }\r
805         else if (scsiDev.msgOut == 0x05)\r
806         {\r
807                 // Initiate Detected Error\r
808                 // Ignore for now\r
809         }\r
810         else if (scsiDev.msgOut == 0x0F)\r
811         {\r
812                 // INITIATE RECOVERY\r
813                 // Ignore for now\r
814         }\r
815         else if (scsiDev.msgOut == 0x10)\r
816         {\r
817                 // RELEASE RECOVERY\r
818                 // Ignore for now\r
819                 enter_BusFree();\r
820         }\r
821         else if (scsiDev.msgOut == MSG_REJECT)\r
822         {\r
823                 // Message Reject\r
824                 // Oh well.\r
825                 scsiDev.resetFlag = 1;\r
826         }\r
827         else if (scsiDev.msgOut == 0x08)\r
828         {\r
829                 // NOP\r
830         }\r
831         else if (scsiDev.msgOut == 0x09)\r
832         {\r
833                 // Message Parity Error\r
834                 // Go back and re-send the last message.\r
835                 scsiDev.phase = MESSAGE_IN;\r
836         }\r
837         else if (scsiDev.msgOut & 0x80) // 0x80 -> 0xFF\r
838         {\r
839                 // IDENTIFY\r
840                 if ((scsiDev.msgOut & 0x18) || // Reserved bits set.\r
841                         (scsiDev.msgOut & 0x20))  // We don't have any target routines!\r
842                 {\r
843                         messageReject();\r
844                 }\r
845 \r
846                 scsiDev.lun = scsiDev.msgOut & 0x7;\r
847                 scsiDev.discPriv = \r
848                         ((scsiDev.msgOut & 0x40) && (scsiDev.initiatorId >= 0))\r
849                                 ? 1 : 0;\r
850         }\r
851         else if (scsiDev.msgOut >= 0x20 && scsiDev.msgOut <= 0x2F)\r
852         {\r
853                 // Two byte message. We don't support these. read and discard.\r
854                 scsiReadByte();\r
855 \r
856                 if (scsiDev.msgOut == 0x23) {\r
857                         // Ignore Wide Residue. We're only 8 bit anyway.\r
858                 } else {\r
859                         messageReject();\r
860                 }\r
861         }\r
862         else if (scsiDev.msgOut == 0x01)\r
863         {\r
864                 int i;\r
865 \r
866                 // Extended message.\r
867                 int msgLen = scsiReadByte();\r
868                 if (msgLen == 0) msgLen = 256;\r
869                 uint8_t extmsg[256];\r
870                 for (i = 0; i < msgLen && !scsiDev.resetFlag; ++i)\r
871                 {\r
872                         // Discard bytes.\r
873                         extmsg[i] = scsiReadByte();\r
874                 }\r
875 \r
876                 if (extmsg[0] == 3 && msgLen == 2) // Wide Data Request\r
877                 {\r
878                         // Negotiate down to 8bit\r
879                         scsiEnterPhase(MESSAGE_IN);\r
880                         static const uint8_t WDTR[] = {0x01, 0x02, 0x03, 0x00};\r
881                         scsiWrite(WDTR, sizeof(WDTR));\r
882                 }\r
883                 else if (extmsg[0] == 1 && msgLen == 3) // Synchronous data request\r
884                 {\r
885                         // Negotiate back to async\r
886                         scsiEnterPhase(MESSAGE_IN);\r
887                         static const uint8_t SDTR[] = {0x01, 0x03, 0x01, 0x00, 0x00};\r
888                         scsiWrite(SDTR, sizeof(SDTR));\r
889                 }\r
890                 else\r
891                 {\r
892                         // Not supported\r
893                         messageReject();\r
894                 }\r
895         }\r
896         else\r
897         {\r
898                 messageReject();\r
899         }\r
900 \r
901         // Re-check the ATN flag in case it stays asserted.\r
902         scsiDev.atnFlag |= SCSI_ReadFilt(SCSI_Filt_ATN);\r
903 }\r
904 \r
905 void scsiPoll(void)\r
906 {\r
907         if (unlikely(scsiDev.resetFlag))\r
908         {\r
909                 scsiReset();\r
910                 if ((scsiDev.resetFlag = SCSI_ReadFilt(SCSI_Filt_RST)))\r
911                 {\r
912                         // Still in reset phase. Do not try and process any commands.\r
913                         return;\r
914                 }\r
915         }\r
916 \r
917         switch (scsiDev.phase)\r
918         {\r
919         case BUS_FREE:\r
920                 if (SCSI_ReadFilt(SCSI_Filt_BSY))\r
921                 {\r
922                         scsiDev.phase = BUS_BUSY;\r
923                 }\r
924                 // The Arbitration phase is optional for SCSI1/SASI hosts if there is only\r
925                 // one initiator in the chain. Support this by moving\r
926                 // straight to selection if SEL is asserted.\r
927                 // ie. the initiator won't assert BSY and it's own ID before moving to selection.\r
928                 else if (SCSI_ReadFilt(SCSI_Filt_SEL) || scsiDev.selFlag)\r
929                 {\r
930                         enter_SelectionPhase();\r
931                 }\r
932         break;\r
933 \r
934         case BUS_BUSY:\r
935                 // Someone is using the bus. Perhaps they are trying to\r
936                 // select us.\r
937                 if (SCSI_ReadFilt(SCSI_Filt_SEL) || scsiDev.selFlag)\r
938                 {\r
939                         enter_SelectionPhase();\r
940                 }\r
941                 else if (!SCSI_ReadFilt(SCSI_Filt_BSY))\r
942                 {\r
943                         scsiDev.phase = BUS_FREE;\r
944                 }\r
945         break;\r
946 \r
947         case ARBITRATION:\r
948                 // TODO Support reselection.\r
949                 break;\r
950 \r
951         case SELECTION:\r
952                 process_SelectionPhase();\r
953         break;\r
954 \r
955         case RESELECTION:\r
956                 // Not currently supported!\r
957         break;\r
958 \r
959         case COMMAND:\r
960                 // Do not check ATN here. SCSI 1 & 2 initiators must set ATN\r
961                 // and SEL together upon entering the selection phase if they\r
962                 // want to send a message (IDENTIFY) immediately.\r
963                 if (scsiDev.atnFlag)\r
964                 {\r
965                         process_MessageOut();\r
966                 }\r
967                 else\r
968                 {\r
969                         process_Command();\r
970                 }\r
971         break;\r
972 \r
973         case DATA_IN:\r
974                 scsiDev.atnFlag |= SCSI_ReadFilt(SCSI_Filt_ATN);\r
975                 if (scsiDev.atnFlag)\r
976                 {\r
977                         process_MessageOut();\r
978                 }\r
979                 else\r
980                 {\r
981                         process_DataIn();\r
982                 }\r
983         break;\r
984 \r
985         case DATA_OUT:\r
986                 scsiDev.atnFlag |= SCSI_ReadFilt(SCSI_Filt_ATN);\r
987                 if (scsiDev.atnFlag)\r
988                 {\r
989                         process_MessageOut();\r
990                 }\r
991                 else\r
992                 {\r
993                         process_DataOut();\r
994                 }\r
995         break;\r
996 \r
997         case STATUS:\r
998                 scsiDev.atnFlag |= SCSI_ReadFilt(SCSI_Filt_ATN);\r
999                 if (scsiDev.atnFlag)\r
1000                 {\r
1001                         process_MessageOut();\r
1002                 }\r
1003                 else\r
1004                 {\r
1005                         process_Status();\r
1006                 }\r
1007         break;\r
1008 \r
1009         case MESSAGE_IN:\r
1010                 scsiDev.atnFlag |= SCSI_ReadFilt(SCSI_Filt_ATN);\r
1011                 if (scsiDev.atnFlag)\r
1012                 {\r
1013                         process_MessageOut();\r
1014                 }\r
1015                 else\r
1016                 {\r
1017                         process_MessageIn(1);\r
1018                 }\r
1019 \r
1020         break;\r
1021 \r
1022         case MESSAGE_OUT:\r
1023                 process_MessageOut();\r
1024         break;\r
1025         }\r
1026 }\r
1027 \r
1028 void scsiInit()\r
1029 {\r
1030         scsiDev.atnFlag = 0;\r
1031         scsiDev.resetFlag = 1;\r
1032         scsiDev.selFlag = 0;\r
1033         scsiDev.phase = BUS_FREE;\r
1034         scsiDev.target = NULL;\r
1035         scsiDev.compatMode = COMPAT_UNKNOWN;\r
1036 \r
1037         int i;\r
1038         for (i = 0; i < MAX_SCSI_TARGETS; ++i)\r
1039         {\r
1040                 const TargetConfig* cfg = getConfigByIndex(i);\r
1041                 if (cfg && (cfg->scsiId & CONFIG_TARGET_ENABLED))\r
1042                 {\r
1043                         scsiDev.targets[i].targetId = cfg->scsiId & CONFIG_TARGET_ID_BITS;\r
1044                         scsiDev.targets[i].cfg = cfg;\r
1045 \r
1046                         scsiDev.targets[i].liveCfg.bytesPerSector = cfg->bytesPerSector;\r
1047                 }\r
1048                 else\r
1049                 {\r
1050                         scsiDev.targets[i].targetId = 0xff;\r
1051                         scsiDev.targets[i].cfg = NULL;\r
1052                 }\r
1053                 scsiDev.targets[i].reservedId = -1;\r
1054                 scsiDev.targets[i].reserverId = -1;\r
1055                 scsiDev.targets[i].unitAttention = POWER_ON_RESET;\r
1056                 scsiDev.targets[i].sense.code = NO_SENSE;\r
1057                 scsiDev.targets[i].sense.asc = NO_ADDITIONAL_SENSE_INFORMATION;\r
1058         }\r
1059 }\r
1060 \r
1061 void scsiDisconnect()\r
1062 {\r
1063         scsiEnterPhase(MESSAGE_IN);\r
1064         scsiWriteByte(0x02); // save data pointer\r
1065         scsiWriteByte(0x04); // disconnect msg.\r
1066 \r
1067         // For now, the caller is responsible for tracking the disconnected\r
1068         // state, and calling scsiReconnect.\r
1069         // Ideally the client would exit their loop and we'd implement this\r
1070         // as part of scsiPoll\r
1071         int phase = scsiDev.phase;\r
1072         enter_BusFree();\r
1073         scsiDev.phase = phase;\r
1074 }\r
1075 \r
1076 int scsiReconnect()\r
1077 {\r
1078         int reconnected = 0;\r
1079 \r
1080         int sel = SCSI_ReadFilt(SCSI_Filt_SEL);\r
1081         int bsy = SCSI_ReadFilt(SCSI_Filt_BSY);\r
1082         if (!sel && !bsy)\r
1083         {\r
1084                 CyDelayUs(1);\r
1085                 sel = SCSI_ReadFilt(SCSI_Filt_SEL);\r
1086                 bsy = SCSI_ReadFilt(SCSI_Filt_BSY);\r
1087         }\r
1088 \r
1089         if (!sel && !bsy)\r
1090         {\r
1091                 // Arbitrate.\r
1092                 ledOn();\r
1093                 uint8_t scsiIdMask = 1 << scsiDev.target->targetId;\r
1094                 SCSI_Out_Bits_Write(scsiIdMask);\r
1095                 SCSI_Out_Ctl_Write(1); // Write bits manually.\r
1096                 SCSI_SetPin(SCSI_Out_BSY);\r
1097 \r
1098                 CyDelayUs(3); // arbitrate delay. 2.4us.\r
1099 \r
1100                 uint8_t dbx = scsiReadDBxPins();\r
1101                 sel = SCSI_ReadFilt(SCSI_Filt_SEL);\r
1102                 if (sel || ((dbx ^ scsiIdMask) > scsiIdMask))\r
1103                 {\r
1104                         // Lost arbitration.\r
1105                         SCSI_Out_Ctl_Write(0);\r
1106                         SCSI_ClearPin(SCSI_Out_BSY);\r
1107                         ledOff();\r
1108                 }\r
1109                 else\r
1110                 {\r
1111                         // Won arbitration\r
1112                         SCSI_SetPin(SCSI_Out_SEL);\r
1113                         CyDelayUs(1); // Bus clear + Bus settle.\r
1114 \r
1115                         // Reselection phase\r
1116                         SCSI_CTL_PHASE_Write(__scsiphase_io);\r
1117                         SCSI_Out_Bits_Write(scsiIdMask | (1 << scsiDev.initiatorId));\r
1118                         scsiDeskewDelay(); // 2 deskew delays\r
1119                         scsiDeskewDelay(); // 2 deskew delays\r
1120                         SCSI_ClearPin(SCSI_Out_BSY);\r
1121                         CyDelayUs(1);  // Bus Settle Delay\r
1122 \r
1123                         uint32_t waitStart_ms = getTime_ms();\r
1124                         bsy = SCSI_ReadFilt(SCSI_Filt_BSY);\r
1125                         // Wait for initiator.\r
1126                         while (\r
1127                                 !bsy &&\r
1128                                 !scsiDev.resetFlag &&\r
1129                                 (elapsedTime_ms(waitStart_ms) < 250))\r
1130                         {\r
1131                                 bsy = SCSI_ReadFilt(SCSI_Filt_BSY);\r
1132                         }\r
1133 \r
1134                         if (bsy)\r
1135                         {\r
1136                                 SCSI_SetPin(SCSI_Out_BSY);\r
1137                                 scsiDeskewDelay(); // 2 deskew delays\r
1138                                 scsiDeskewDelay(); // 2 deskew delays\r
1139                                 SCSI_ClearPin(SCSI_Out_SEL);\r
1140 \r
1141                                 // Prepare for the initial IDENTIFY message.\r
1142                                 SCSI_Out_Ctl_Write(0);\r
1143                                 scsiEnterPhase(MESSAGE_IN);\r
1144 \r
1145                                 // Send identify command\r
1146                                 scsiWriteByte(0x80);\r
1147 \r
1148                                 scsiEnterPhase(scsiDev.phase);\r
1149                                 reconnected = 1;\r
1150                         }\r
1151                         else\r
1152                         {\r
1153                                 // reselect timeout.\r
1154                                 SCSI_Out_Ctl_Write(0);\r
1155                                 SCSI_ClearPin(SCSI_Out_SEL);\r
1156                                 SCSI_CTL_PHASE_Write(0);\r
1157                                 ledOff();\r
1158                         }\r
1159                 }\r
1160         }\r
1161         return reconnected;\r
1162 }\r
1163 \r