Improve XEBEC controller support
[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 == 0x0f &&\r
476                 scsiDev.target->cfg->quirks == CONFIG_QUIRKS_XEBEC)\r
477         {\r
478                 scsiWriteSectorBuffer();\r
479         }\r
480         else if (command == 0x3C)\r
481         {\r
482                 scsiReadBuffer();\r
483         }\r
484         else if (!scsiModeCommand() && !scsiVendorCommand())\r
485         {\r
486                 scsiDev.target->sense.code = ILLEGAL_REQUEST;\r
487                 scsiDev.target->sense.asc = INVALID_COMMAND_OPERATION_CODE;\r
488                 enter_Status(CHECK_CONDITION);\r
489         }\r
490 \r
491         // Successful\r
492         if (scsiDev.phase == COMMAND) // No status set, and not in DATA_IN\r
493         {\r
494                 enter_Status(GOOD);\r
495         }\r
496 \r
497 }\r
498 \r
499 static void doReserveRelease()\r
500 {\r
501         int extentReservation = scsiDev.cdb[1] & 1;\r
502         int thirdPty = scsiDev.cdb[1] & 0x10;\r
503         int thirdPtyId = (scsiDev.cdb[1] >> 1) & 0x7;\r
504         uint8 command = scsiDev.cdb[0];\r
505 \r
506         int canRelease =\r
507                 (!thirdPty && (scsiDev.initiatorId == scsiDev.target->reservedId)) ||\r
508                         (thirdPty &&\r
509                                 (scsiDev.target->reserverId == scsiDev.initiatorId) &&\r
510                                 (scsiDev.target->reservedId == thirdPtyId)\r
511                         );\r
512 \r
513         if (extentReservation)\r
514         {\r
515                 // Not supported.\r
516                 scsiDev.target->sense.code = ILLEGAL_REQUEST;\r
517                 scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;\r
518                 enter_Status(CHECK_CONDITION);\r
519         }\r
520         else if (command == 0x17) // release\r
521         {\r
522                 if ((scsiDev.target->reservedId < 0) || canRelease)\r
523                 {\r
524                         scsiDev.target->reservedId = -1;\r
525                         scsiDev.target->reserverId = -1;\r
526                 }\r
527                 else\r
528                 {\r
529                         enter_Status(CONFLICT);\r
530                 }\r
531         }\r
532         else // assume reserve.\r
533         {\r
534                 if ((scsiDev.target->reservedId < 0) || canRelease)\r
535                 {\r
536                         scsiDev.target->reserverId = scsiDev.initiatorId;\r
537                         if (thirdPty)\r
538                         {\r
539                                 scsiDev.target->reservedId = thirdPtyId;\r
540                         }\r
541                         else\r
542                         {\r
543                                 scsiDev.target->reservedId = scsiDev.initiatorId;\r
544                         }\r
545                 }\r
546                 else\r
547                 {\r
548                         // Already reserved by someone else!\r
549                         enter_Status(CONFLICT);\r
550                 }\r
551         }\r
552 }\r
553 \r
554 static void scsiReset()\r
555 {\r
556         scsiDev.rstCount++;\r
557         ledOff();\r
558 \r
559         scsiPhyReset();\r
560         SCSI_Out_Ctl_Write(0);\r
561 \r
562         scsiDev.parityError = 0;\r
563         scsiDev.phase = BUS_FREE;\r
564         scsiDev.atnFlag = 0;\r
565         scsiDev.resetFlag = 0;\r
566         scsiDev.selFlag = 0;\r
567         scsiDev.lun = -1;\r
568         scsiDev.compatMode = COMPAT_UNKNOWN;\r
569 \r
570         if (scsiDev.target)\r
571         {\r
572                 if (scsiDev.target->unitAttention != POWER_ON_RESET)\r
573                 {\r
574                         scsiDev.target->unitAttention = SCSI_BUS_RESET;\r
575                 }\r
576                 scsiDev.target->reservedId = -1;\r
577                 scsiDev.target->reserverId = -1;\r
578                 scsiDev.target->sense.code = NO_SENSE;\r
579                 scsiDev.target->sense.asc = NO_ADDITIONAL_SENSE_INFORMATION;\r
580         }\r
581         scsiDev.target = NULL;\r
582         scsiDiskReset();\r
583 \r
584         scsiDev.postDataOutHook = NULL;\r
585 \r
586         // Sleep to allow the bus to settle down a bit.\r
587         // We must be ready again within the "Reset to selection time" of\r
588         // 250ms.\r
589         // There is no guarantee that the RST line will be negated by then.\r
590         // NOTE: We could be connected and powered by USB for configuration,\r
591         // in which case TERMPWR cannot be supplied, and reset will ALWAYS\r
592         // be true. Therefore, the sleep here must be slow to avoid slowing\r
593         // USB comms\r
594         // Also, need to exit quickly for XEBEC controllers which may\r
595         // assert RST immediately before pulsing a SEL.\r
596         uint32_t rstTimerBegin = getTime_ms();\r
597         while (SCSI_ReadFilt(SCSI_Filt_RST))\r
598         {\r
599                 if (elapsedTime_ms(rstTimerBegin) >= 1)\r
600                 {\r
601                         break;\r
602                 }\r
603         }\r
604 }\r
605 \r
606 static void enter_SelectionPhase()\r
607 {\r
608         // Ignore stale versions of this flag, but ensure we know the\r
609         // current value if the flag is still set.\r
610         scsiDev.atnFlag = 0;\r
611         scsiDev.parityError = 0;\r
612         scsiDev.dataPtr = 0;\r
613         scsiDev.savedDataPtr = 0;\r
614         scsiDev.dataLen = 0;\r
615         scsiDev.status = GOOD;\r
616         scsiDev.phase = SELECTION;\r
617         scsiDev.lun = -1;\r
618         scsiDev.discPriv = 0;\r
619 \r
620         scsiDev.initiatorId = -1;\r
621         scsiDev.target = NULL;\r
622 \r
623         transfer.blocks = 0;\r
624         transfer.currentBlock = 0;\r
625 \r
626         scsiDev.postDataOutHook = NULL;\r
627 }\r
628 \r
629 static void process_SelectionPhase()\r
630 {\r
631         // Selection delays.\r
632         // Many SCSI1 samplers that use a 5380 chip need a delay of at least 1ms.\r
633         // The Mac Plus boot-time (ie. rom code) selection abort time\r
634         // is < 1ms and must have no delay (standard suggests 250ms abort time)\r
635         // Most newer SCSI2 hosts don't care either way.\r
636         if (scsiDev.target->cfg->quirks == CONFIG_QUIRKS_XEBEC)\r
637         {\r
638                 CyDelay(1); // Simply won't work if set to 0.\r
639         }\r
640         else if (scsiDev.boardCfg.selectionDelay == 255) // auto\r
641         {\r
642                 if (scsiDev.compatMode < COMPAT_SCSI2)\r
643                 {\r
644                         CyDelay(1);\r
645                 }\r
646         }\r
647         else if (scsiDev.boardCfg.selectionDelay != 0)\r
648         {\r
649                 CyDelay(scsiDev.boardCfg.selectionDelay);\r
650         }\r
651 \r
652         int selLatchCfg = scsiDev.boardCfg.flags & CONFIG_ENABLE_SEL_LATCH;\r
653         int sel = (selLatchCfg && scsiDev.selFlag) || SCSI_ReadFilt(SCSI_Filt_SEL);\r
654 \r
655         int bsy = SCSI_ReadFilt(SCSI_Filt_BSY);\r
656 #ifdef SCSI_In_IO\r
657         int io = SCSI_ReadPin(SCSI_In_IO);\r
658 #else\r
659         int io = 0;\r
660 #endif\r
661 \r
662         // Only read these pins AFTER SEL and BSY - we don't want to catch them\r
663         // during a transition period.\r
664         uint8 mask = (selLatchCfg && scsiDev.selFlag) ? scsiDev.selDBX : scsiReadDBxPins();\r
665         int maskBitCount = countBits(mask);\r
666         int goodParity = (Lookup_OddParity[mask] == SCSI_ReadPin(SCSI_In_DBP));\r
667         int atnFlag = SCSI_ReadFilt(SCSI_Filt_ATN);\r
668 \r
669         int tgtIndex;\r
670         TargetState* target = NULL;\r
671         for (tgtIndex = 0; tgtIndex < MAX_SCSI_TARGETS; ++tgtIndex)\r
672         {\r
673                 if (mask & (1 << scsiDev.targets[tgtIndex].targetId))\r
674                 {\r
675                         target = &scsiDev.targets[tgtIndex];\r
676                         break;\r
677                 }\r
678         }\r
679         sel &= (selLatchCfg && scsiDev.selFlag) || SCSI_ReadFilt(SCSI_Filt_SEL);\r
680         bsy |= SCSI_ReadFilt(SCSI_Filt_BSY);\r
681 #ifdef SCSI_In_IO\r
682         io |= SCSI_ReadPin(SCSI_In_IO);\r
683 #endif\r
684         if (!bsy && !io && sel &&\r
685                 target &&\r
686                 (goodParity || !(scsiDev.boardCfg.flags & CONFIG_ENABLE_PARITY) || !atnFlag) &&\r
687                 likely(maskBitCount <= 2))\r
688         {\r
689                 // We've been selected!\r
690                 // Assert BSY - Selection success!\r
691                 // must happen within 200us (Selection abort time) of seeing our\r
692                 // ID + SEL.\r
693                 // (Note: the initiator will be waiting the "Selection time-out delay"\r
694                 // for our BSY response, which is actually a very generous 250ms)\r
695                 SCSI_SetPin(SCSI_Out_BSY);\r
696                 ledOn();\r
697 \r
698                 scsiDev.target = target;\r
699 \r
700                 // Do we enter MESSAGE OUT immediately ? SCSI 1 and 2 standards says\r
701                 // move to MESSAGE OUT if ATN is true before we assert BSY.\r
702                 // The initiator should assert ATN with SEL.\r
703                 scsiDev.atnFlag = atnFlag;\r
704 \r
705 \r
706                 // Unit attention breaks many older SCSI hosts. Disable it completely\r
707                 // for SCSI-1 (and older) hosts, regardless of our configured setting.\r
708                 // Enable the compatability mode also as many SASI and SCSI1\r
709                 // controllers don't generate parity bits.\r
710                 if (!scsiDev.atnFlag)\r
711                 {\r
712                         target->unitAttention = 0;\r
713                         scsiDev.compatMode = COMPAT_SCSI1;\r
714                 }\r
715                 else if (!(scsiDev.boardCfg.flags & CONFIG_ENABLE_SCSI2))\r
716                 {\r
717                         scsiDev.compatMode = COMPAT_SCSI2_DISABLED;\r
718                 }\r
719                 else\r
720                 {\r
721                         scsiDev.compatMode = COMPAT_SCSI2;\r
722                 }\r
723 \r
724                 scsiDev.selCount++;\r
725 \r
726 \r
727                 // Save our initiator now that we're no longer in a time-critical\r
728                 // section.\r
729                 // SCSI1/SASI initiators may not set their own ID.\r
730                 {\r
731                         int i;\r
732                         uint8_t initiatorMask = mask ^ (1 << target->targetId);\r
733                         scsiDev.initiatorId = -1;\r
734                         for (i = 0; i < 8; ++i)\r
735                         {\r
736                                 if (initiatorMask & (1 << i))\r
737                                 {\r
738                                         scsiDev.initiatorId = i;\r
739                                         break;\r
740                                 }\r
741                         }\r
742                 }\r
743 \r
744                 // Wait until the end of the selection phase.\r
745                 uint32_t selTimerBegin = getTime_ms();\r
746                 while (likely(!scsiDev.resetFlag))\r
747                 {\r
748                         if (!SCSI_ReadFilt(SCSI_Filt_SEL))\r
749                         {\r
750                                 break;\r
751                         }\r
752                         else if (elapsedTime_ms(selTimerBegin) >= 10 &&\r
753                                 scsiDev.target->cfg->quirks == CONFIG_QUIRKS_XEBEC)\r
754                         {\r
755                                 // XEBEC hosts may not bother releasing SEL at all until\r
756                                 // just before the command ends.\r
757                                 break;\r
758                         }\r
759                         else if (elapsedTime_ms(selTimerBegin) >= 250)\r
760                         {\r
761                                 SCSI_ClearPin(SCSI_Out_BSY);\r
762                                 scsiDev.resetFlag = 1;\r
763                                 break;\r
764                         }\r
765                 }\r
766 \r
767                 scsiDev.phase = COMMAND;\r
768         }\r
769         else if (!sel)\r
770         {\r
771                 scsiDev.phase = BUS_BUSY;\r
772         }\r
773 \r
774         scsiDev.selFlag = 0;\r
775 }\r
776 \r
777 static void process_MessageOut()\r
778 {\r
779         scsiEnterPhase(MESSAGE_OUT);\r
780 \r
781         scsiDev.atnFlag = 0;\r
782         scsiDev.parityError = 0;\r
783         scsiDev.msgOut = scsiReadByte();\r
784         scsiDev.msgCount++;\r
785 \r
786         if (scsiDev.parityError &&\r
787                 (scsiDev.boardCfg.flags & CONFIG_ENABLE_PARITY) &&\r
788                 (scsiDev.compatMode >= COMPAT_SCSI2))\r
789         {\r
790                 // Skip the remaining message bytes, and then start the MESSAGE_OUT\r
791                 // phase again from the start. The initiator will re-send the\r
792                 // same set of messages.\r
793                 while (SCSI_ReadFilt(SCSI_Filt_ATN) && !scsiDev.resetFlag)\r
794                 {\r
795                         scsiReadByte();\r
796                 }\r
797 \r
798                 // Go-back and try the message again.\r
799                 scsiDev.atnFlag = 1;\r
800                 scsiDev.parityError = 0;\r
801         }\r
802         else if (scsiDev.msgOut == 0x00)\r
803         {\r
804                 // COMMAND COMPLETE. but why would the target be receiving this ? nfi.\r
805                 enter_BusFree();\r
806         }\r
807         else if (scsiDev.msgOut == 0x06)\r
808         {\r
809                 // ABORT\r
810                 scsiDiskReset();\r
811                 enter_BusFree();\r
812         }\r
813         else if (scsiDev.msgOut == 0x0C)\r
814         {\r
815                 // BUS DEVICE RESET\r
816 \r
817                 scsiDiskReset();\r
818 \r
819                 scsiDev.target->unitAttention = SCSI_BUS_RESET;\r
820 \r
821                 // ANY initiator can reset the reservation state via this message.\r
822                 scsiDev.target->reservedId = -1;\r
823                 scsiDev.target->reserverId = -1;\r
824                 enter_BusFree();\r
825         }\r
826         else if (scsiDev.msgOut == 0x05)\r
827         {\r
828                 // Initiate Detected Error\r
829                 // Ignore for now\r
830         }\r
831         else if (scsiDev.msgOut == 0x0F)\r
832         {\r
833                 // INITIATE RECOVERY\r
834                 // Ignore for now\r
835         }\r
836         else if (scsiDev.msgOut == 0x10)\r
837         {\r
838                 // RELEASE RECOVERY\r
839                 // Ignore for now\r
840                 enter_BusFree();\r
841         }\r
842         else if (scsiDev.msgOut == MSG_REJECT)\r
843         {\r
844                 // Message Reject\r
845                 // Oh well.\r
846                 scsiDev.resetFlag = 1;\r
847         }\r
848         else if (scsiDev.msgOut == 0x08)\r
849         {\r
850                 // NOP\r
851         }\r
852         else if (scsiDev.msgOut == 0x09)\r
853         {\r
854                 // Message Parity Error\r
855                 // Go back and re-send the last message.\r
856                 scsiDev.phase = MESSAGE_IN;\r
857         }\r
858         else if (scsiDev.msgOut & 0x80) // 0x80 -> 0xFF\r
859         {\r
860                 // IDENTIFY\r
861                 if ((scsiDev.msgOut & 0x18) || // Reserved bits set.\r
862                         (scsiDev.msgOut & 0x20))  // We don't have any target routines!\r
863                 {\r
864                         messageReject();\r
865                 }\r
866 \r
867                 scsiDev.lun = scsiDev.msgOut & 0x7;\r
868                 scsiDev.discPriv = \r
869                         ((scsiDev.msgOut & 0x40) && (scsiDev.initiatorId >= 0))\r
870                                 ? 1 : 0;\r
871         }\r
872         else if (scsiDev.msgOut >= 0x20 && scsiDev.msgOut <= 0x2F)\r
873         {\r
874                 // Two byte message. We don't support these. read and discard.\r
875                 scsiReadByte();\r
876 \r
877                 if (scsiDev.msgOut == 0x23) {\r
878                         // Ignore Wide Residue. We're only 8 bit anyway.\r
879                 } else {\r
880                         messageReject();\r
881                 }\r
882         }\r
883         else if (scsiDev.msgOut == 0x01)\r
884         {\r
885                 int i;\r
886 \r
887                 // Extended message.\r
888                 int msgLen = scsiReadByte();\r
889                 if (msgLen == 0) msgLen = 256;\r
890                 uint8_t extmsg[256];\r
891                 for (i = 0; i < msgLen && !scsiDev.resetFlag; ++i)\r
892                 {\r
893                         // Discard bytes.\r
894                         extmsg[i] = scsiReadByte();\r
895                 }\r
896 \r
897                 if (extmsg[0] == 3 && msgLen == 2) // Wide Data Request\r
898                 {\r
899                         // Negotiate down to 8bit\r
900                         scsiEnterPhase(MESSAGE_IN);\r
901                         static const uint8_t WDTR[] = {0x01, 0x02, 0x03, 0x00};\r
902                         scsiWrite(WDTR, sizeof(WDTR));\r
903                 }\r
904                 else if (extmsg[0] == 1 && msgLen == 3) // Synchronous data request\r
905                 {\r
906                         // Negotiate back to async\r
907                         scsiEnterPhase(MESSAGE_IN);\r
908                         static const uint8_t SDTR[] = {0x01, 0x03, 0x01, 0x00, 0x00};\r
909                         scsiWrite(SDTR, sizeof(SDTR));\r
910                 }\r
911                 else\r
912                 {\r
913                         // Not supported\r
914                         messageReject();\r
915                 }\r
916         }\r
917         else\r
918         {\r
919                 messageReject();\r
920         }\r
921 \r
922         // Re-check the ATN flag in case it stays asserted.\r
923         scsiDev.atnFlag |= SCSI_ReadFilt(SCSI_Filt_ATN);\r
924 }\r
925 \r
926 void scsiPoll(void)\r
927 {\r
928         if (unlikely(scsiDev.resetFlag))\r
929         {\r
930                 scsiReset();\r
931                 if ((scsiDev.resetFlag = SCSI_ReadFilt(SCSI_Filt_RST)))\r
932                 {\r
933                         // Still in reset phase. Do not try and process any commands.\r
934                         return;\r
935                 }\r
936         }\r
937 \r
938         switch (scsiDev.phase)\r
939         {\r
940         case BUS_FREE:\r
941                 if (SCSI_ReadFilt(SCSI_Filt_BSY))\r
942                 {\r
943                         scsiDev.phase = BUS_BUSY;\r
944                 }\r
945                 // The Arbitration phase is optional for SCSI1/SASI hosts if there is only\r
946                 // one initiator in the chain. Support this by moving\r
947                 // straight to selection if SEL is asserted.\r
948                 // ie. the initiator won't assert BSY and it's own ID before moving to selection.\r
949                 else if (SCSI_ReadFilt(SCSI_Filt_SEL) || scsiDev.selFlag)\r
950                 {\r
951                         enter_SelectionPhase();\r
952                 }\r
953         break;\r
954 \r
955         case BUS_BUSY:\r
956                 // Someone is using the bus. Perhaps they are trying to\r
957                 // select us.\r
958                 if (SCSI_ReadFilt(SCSI_Filt_SEL) || scsiDev.selFlag)\r
959                 {\r
960                         enter_SelectionPhase();\r
961                 }\r
962                 else if (!SCSI_ReadFilt(SCSI_Filt_BSY))\r
963                 {\r
964                         scsiDev.phase = BUS_FREE;\r
965                 }\r
966         break;\r
967 \r
968         case ARBITRATION:\r
969                 // TODO Support reselection.\r
970                 break;\r
971 \r
972         case SELECTION:\r
973                 process_SelectionPhase();\r
974         break;\r
975 \r
976         case RESELECTION:\r
977                 // Not currently supported!\r
978         break;\r
979 \r
980         case COMMAND:\r
981                 // Do not check ATN here. SCSI 1 & 2 initiators must set ATN\r
982                 // and SEL together upon entering the selection phase if they\r
983                 // want to send a message (IDENTIFY) immediately.\r
984                 if (scsiDev.atnFlag)\r
985                 {\r
986                         process_MessageOut();\r
987                 }\r
988                 else\r
989                 {\r
990                         process_Command();\r
991                 }\r
992         break;\r
993 \r
994         case DATA_IN:\r
995                 scsiDev.atnFlag |= SCSI_ReadFilt(SCSI_Filt_ATN);\r
996                 if (scsiDev.atnFlag)\r
997                 {\r
998                         process_MessageOut();\r
999                 }\r
1000                 else\r
1001                 {\r
1002                         process_DataIn();\r
1003                 }\r
1004         break;\r
1005 \r
1006         case DATA_OUT:\r
1007                 scsiDev.atnFlag |= SCSI_ReadFilt(SCSI_Filt_ATN);\r
1008                 if (scsiDev.atnFlag)\r
1009                 {\r
1010                         process_MessageOut();\r
1011                 }\r
1012                 else\r
1013                 {\r
1014                         process_DataOut();\r
1015                 }\r
1016         break;\r
1017 \r
1018         case STATUS:\r
1019                 scsiDev.atnFlag |= SCSI_ReadFilt(SCSI_Filt_ATN);\r
1020                 if (scsiDev.atnFlag)\r
1021                 {\r
1022                         process_MessageOut();\r
1023                 }\r
1024                 else\r
1025                 {\r
1026                         process_Status();\r
1027                 }\r
1028         break;\r
1029 \r
1030         case MESSAGE_IN:\r
1031                 scsiDev.atnFlag |= SCSI_ReadFilt(SCSI_Filt_ATN);\r
1032                 if (scsiDev.atnFlag)\r
1033                 {\r
1034                         process_MessageOut();\r
1035                 }\r
1036                 else\r
1037                 {\r
1038                         process_MessageIn(1);\r
1039                 }\r
1040 \r
1041         break;\r
1042 \r
1043         case MESSAGE_OUT:\r
1044                 process_MessageOut();\r
1045         break;\r
1046         }\r
1047 }\r
1048 \r
1049 void scsiInit()\r
1050 {\r
1051         scsiDev.atnFlag = 0;\r
1052         scsiDev.resetFlag = 1;\r
1053         scsiDev.selFlag = 0;\r
1054         scsiDev.phase = BUS_FREE;\r
1055         scsiDev.target = NULL;\r
1056         scsiDev.compatMode = COMPAT_UNKNOWN;\r
1057 \r
1058         int i;\r
1059         for (i = 0; i < MAX_SCSI_TARGETS; ++i)\r
1060         {\r
1061                 const TargetConfig* cfg = getConfigByIndex(i);\r
1062                 if (cfg && (cfg->scsiId & CONFIG_TARGET_ENABLED))\r
1063                 {\r
1064                         scsiDev.targets[i].targetId = cfg->scsiId & CONFIG_TARGET_ID_BITS;\r
1065                         scsiDev.targets[i].cfg = cfg;\r
1066 \r
1067                         scsiDev.targets[i].liveCfg.bytesPerSector = cfg->bytesPerSector;\r
1068                 }\r
1069                 else\r
1070                 {\r
1071                         scsiDev.targets[i].targetId = 0xff;\r
1072                         scsiDev.targets[i].cfg = NULL;\r
1073                 }\r
1074                 scsiDev.targets[i].reservedId = -1;\r
1075                 scsiDev.targets[i].reserverId = -1;\r
1076                 scsiDev.targets[i].unitAttention = POWER_ON_RESET;\r
1077                 scsiDev.targets[i].sense.code = NO_SENSE;\r
1078                 scsiDev.targets[i].sense.asc = NO_ADDITIONAL_SENSE_INFORMATION;\r
1079         }\r
1080 }\r
1081 \r
1082 void scsiDisconnect()\r
1083 {\r
1084         scsiEnterPhase(MESSAGE_IN);\r
1085         scsiWriteByte(0x02); // save data pointer\r
1086         scsiWriteByte(0x04); // disconnect msg.\r
1087 \r
1088         // For now, the caller is responsible for tracking the disconnected\r
1089         // state, and calling scsiReconnect.\r
1090         // Ideally the client would exit their loop and we'd implement this\r
1091         // as part of scsiPoll\r
1092         int phase = scsiDev.phase;\r
1093         enter_BusFree();\r
1094         scsiDev.phase = phase;\r
1095 }\r
1096 \r
1097 int scsiReconnect()\r
1098 {\r
1099         int reconnected = 0;\r
1100 \r
1101         int sel = SCSI_ReadFilt(SCSI_Filt_SEL);\r
1102         int bsy = SCSI_ReadFilt(SCSI_Filt_BSY);\r
1103         if (!sel && !bsy)\r
1104         {\r
1105                 CyDelayUs(1);\r
1106                 sel = SCSI_ReadFilt(SCSI_Filt_SEL);\r
1107                 bsy = SCSI_ReadFilt(SCSI_Filt_BSY);\r
1108         }\r
1109 \r
1110         if (!sel && !bsy)\r
1111         {\r
1112                 // Arbitrate.\r
1113                 ledOn();\r
1114                 uint8_t scsiIdMask = 1 << scsiDev.target->targetId;\r
1115                 SCSI_Out_Bits_Write(scsiIdMask);\r
1116                 SCSI_Out_Ctl_Write(1); // Write bits manually.\r
1117                 SCSI_SetPin(SCSI_Out_BSY);\r
1118 \r
1119                 CyDelayUs(3); // arbitrate delay. 2.4us.\r
1120 \r
1121                 uint8_t dbx = scsiReadDBxPins();\r
1122                 sel = SCSI_ReadFilt(SCSI_Filt_SEL);\r
1123                 if (sel || ((dbx ^ scsiIdMask) > scsiIdMask))\r
1124                 {\r
1125                         // Lost arbitration.\r
1126                         SCSI_Out_Ctl_Write(0);\r
1127                         SCSI_ClearPin(SCSI_Out_BSY);\r
1128                         ledOff();\r
1129                 }\r
1130                 else\r
1131                 {\r
1132                         // Won arbitration\r
1133                         SCSI_SetPin(SCSI_Out_SEL);\r
1134                         CyDelayUs(1); // Bus clear + Bus settle.\r
1135 \r
1136                         // Reselection phase\r
1137                         SCSI_CTL_PHASE_Write(__scsiphase_io);\r
1138                         SCSI_Out_Bits_Write(scsiIdMask | (1 << scsiDev.initiatorId));\r
1139                         scsiDeskewDelay(); // 2 deskew delays\r
1140                         scsiDeskewDelay(); // 2 deskew delays\r
1141                         SCSI_ClearPin(SCSI_Out_BSY);\r
1142                         CyDelayUs(1);  // Bus Settle Delay\r
1143 \r
1144                         uint32_t waitStart_ms = getTime_ms();\r
1145                         bsy = SCSI_ReadFilt(SCSI_Filt_BSY);\r
1146                         // Wait for initiator.\r
1147                         while (\r
1148                                 !bsy &&\r
1149                                 !scsiDev.resetFlag &&\r
1150                                 (elapsedTime_ms(waitStart_ms) < 250))\r
1151                         {\r
1152                                 bsy = SCSI_ReadFilt(SCSI_Filt_BSY);\r
1153                         }\r
1154 \r
1155                         if (bsy)\r
1156                         {\r
1157                                 SCSI_SetPin(SCSI_Out_BSY);\r
1158                                 scsiDeskewDelay(); // 2 deskew delays\r
1159                                 scsiDeskewDelay(); // 2 deskew delays\r
1160                                 SCSI_ClearPin(SCSI_Out_SEL);\r
1161 \r
1162                                 // Prepare for the initial IDENTIFY message.\r
1163                                 SCSI_Out_Ctl_Write(0);\r
1164                                 scsiEnterPhase(MESSAGE_IN);\r
1165 \r
1166                                 // Send identify command\r
1167                                 scsiWriteByte(0x80);\r
1168 \r
1169                                 scsiEnterPhase(scsiDev.phase);\r
1170                                 reconnected = 1;\r
1171                         }\r
1172                         else\r
1173                         {\r
1174                                 // reselect timeout.\r
1175                                 SCSI_Out_Ctl_Write(0);\r
1176                                 SCSI_ClearPin(SCSI_Out_SEL);\r
1177                                 SCSI_CTL_PHASE_Write(0);\r
1178                                 ledOff();\r
1179                         }\r
1180                 }\r
1181         }\r
1182         return reconnected;\r
1183 }\r
1184 \r