Use the LUN provided in IDENTIFY messages in preference to the CDB.
[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 \r
30 #include <string.h>\r
31 \r
32 // Global SCSI device state.\r
33 ScsiDevice scsiDev;\r
34 \r
35 static void enter_SelectionPhase(void);\r
36 static void process_SelectionPhase(void);\r
37 static void enter_BusFree(void);\r
38 static void enter_MessageIn(uint8 message);\r
39 static void process_MessageIn(void);\r
40 static void enter_Status(uint8 status);\r
41 static void process_Status(void);\r
42 static void enter_DataIn(int len);\r
43 static void process_DataIn(void);\r
44 static void process_DataOut(void);\r
45 static void process_Command(void);\r
46 \r
47 static void doReserveRelease(void);\r
48 \r
49 static void enter_BusFree()\r
50 {\r
51         // This delay probably isn't needed for most SCSI hosts, but it won't\r
52         // hurt either. It's possible some of the samplers needed this delay.\r
53         CyDelayUs(2);\r
54 \r
55         SCSI_ClearPin(SCSI_Out_BSY);\r
56         // We now have a Bus Clear Delay of 800ns to release remaining signals.\r
57         SCSI_CTL_PHASE_Write(0);\r
58 \r
59         // Wait for the initiator to cease driving signals\r
60         // Bus settle delay + bus clear delay = 1200ns\r
61         CyDelayUs(2);\r
62 \r
63         ledOff();\r
64         scsiDev.phase = BUS_FREE;\r
65 }\r
66 \r
67 static void enter_MessageIn(uint8 message)\r
68 {\r
69         scsiDev.msgIn = message;\r
70         scsiDev.phase = MESSAGE_IN;\r
71 }\r
72 \r
73 static void process_MessageIn()\r
74 {\r
75         scsiEnterPhase(MESSAGE_IN);\r
76         scsiWriteByte(scsiDev.msgIn);\r
77 \r
78         if (scsiDev.atnFlag)\r
79         {\r
80                 // If there was a parity error, we go\r
81                 // back to MESSAGE_OUT first, get out parity error message, then come\r
82                 // back here.\r
83         }\r
84         else if ((scsiDev.msgIn == MSG_LINKED_COMMAND_COMPLETE) ||\r
85                 (scsiDev.msgIn == MSG_LINKED_COMMAND_COMPLETE_WITH_FLAG))\r
86         {\r
87                 // Go back to the command phase and start again.\r
88                 scsiDev.phase = COMMAND;\r
89                 scsiDev.parityError = 0;\r
90                 scsiDev.dataPtr = 0;\r
91                 scsiDev.savedDataPtr = 0;\r
92                 scsiDev.dataLen = 0;\r
93                 scsiDev.status = GOOD;\r
94                 transfer.blocks = 0;\r
95                 transfer.currentBlock = 0;\r
96         }\r
97         else /*if (scsiDev.msgIn == MSG_COMMAND_COMPLETE)*/\r
98         {\r
99                 enter_BusFree();\r
100         }\r
101 }\r
102 \r
103 static void messageReject()\r
104 {\r
105         scsiEnterPhase(MESSAGE_IN);\r
106         scsiWriteByte(MSG_REJECT);\r
107 }\r
108 \r
109 static void enter_Status(uint8 status)\r
110 {\r
111         scsiDev.status = status;\r
112         scsiDev.phase = STATUS;\r
113 \r
114         scsiDev.lastStatus = scsiDev.status;\r
115         scsiDev.lastSense = scsiDev.sense.code;\r
116 }\r
117 \r
118 static void process_Status()\r
119 {\r
120         scsiEnterPhase(STATUS);\r
121 \r
122         uint8 message;\r
123 \r
124         uint8 control = scsiDev.cdb[scsiDev.cdbLen - 1];\r
125         if ((scsiDev.status == GOOD) && (control & 0x01))\r
126         {\r
127                 // Linked command.\r
128                 scsiDev.status = INTERMEDIATE;\r
129                 if (control & 0x02)\r
130                 {\r
131                         message = MSG_LINKED_COMMAND_COMPLETE_WITH_FLAG;\r
132                 }\r
133                 else\r
134                 {\r
135                         message = MSG_LINKED_COMMAND_COMPLETE;\r
136                 }\r
137         }\r
138         else\r
139         {\r
140                 message = MSG_COMMAND_COMPLETE;\r
141         }\r
142         scsiWriteByte(scsiDev.status);\r
143 \r
144         scsiDev.lastStatus = scsiDev.status;\r
145         scsiDev.lastSense = scsiDev.sense.code;\r
146 \r
147         // Command Complete occurs AFTER a valid status has been\r
148         // sent. then we go bus-free.\r
149         enter_MessageIn(message);\r
150 }\r
151 \r
152 static void enter_DataIn(int len)\r
153 {\r
154         scsiDev.dataLen = len;\r
155         scsiDev.phase = DATA_IN;\r
156 }\r
157 \r
158 static void process_DataIn()\r
159 {\r
160         uint32 len;\r
161 \r
162         if (scsiDev.dataLen > sizeof(scsiDev.data))\r
163         {\r
164                 scsiDev.dataLen = sizeof(scsiDev.data);\r
165         }\r
166 \r
167         len = scsiDev.dataLen - scsiDev.dataPtr;\r
168         if (len > 0)\r
169         {\r
170                 scsiEnterPhase(DATA_IN);\r
171                 scsiWrite(scsiDev.data + scsiDev.dataPtr, len);\r
172                 scsiDev.dataPtr += len;\r
173         }\r
174 \r
175         if ((scsiDev.dataPtr >= scsiDev.dataLen) &&\r
176                 (transfer.currentBlock == transfer.blocks))\r
177         {\r
178                 enter_Status(GOOD);\r
179         }\r
180 }\r
181 \r
182 static void process_DataOut()\r
183 {\r
184         uint32 len;\r
185 \r
186         if (scsiDev.dataLen > sizeof(scsiDev.data))\r
187         {\r
188                 scsiDev.dataLen = sizeof(scsiDev.data);\r
189         }\r
190 \r
191         scsiDev.parityError = 0;\r
192         len = scsiDev.dataLen - scsiDev.dataPtr;\r
193         if (len > 0)\r
194         {\r
195                 scsiEnterPhase(DATA_OUT);\r
196 \r
197                 scsiRead(scsiDev.data + scsiDev.dataPtr, len);\r
198                 scsiDev.dataPtr += len;\r
199 \r
200                 // TODO re-implement parity checking\r
201                 if (0 && scsiDev.parityError && config->enableParity)\r
202                 {\r
203                         scsiDev.sense.code = ABORTED_COMMAND;\r
204                         scsiDev.sense.asc = SCSI_PARITY_ERROR;\r
205                         enter_Status(CHECK_CONDITION);\r
206                 }\r
207         }\r
208 \r
209         if ((scsiDev.dataPtr >= scsiDev.dataLen) &&\r
210                 (transfer.currentBlock == transfer.blocks))\r
211         {\r
212                 if (scsiDev.postDataOutHook != NULL)\r
213                 {\r
214                         scsiDev.postDataOutHook();\r
215                 }\r
216                 else\r
217                 {\r
218                         enter_Status(GOOD);\r
219                 }\r
220         }\r
221 }\r
222 \r
223 static const uint8 CmdGroupBytes[8] = {6, 10, 10, 6, 6, 12, 6, 6};\r
224 static void process_Command()\r
225 {\r
226         int group;\r
227         uint8 command;\r
228         uint8 control;\r
229 \r
230         scsiEnterPhase(COMMAND);\r
231         scsiDev.parityError = 0;\r
232 \r
233         memset(scsiDev.cdb, 0, sizeof(scsiDev.cdb));\r
234         scsiDev.cdb[0] = scsiReadByte();\r
235 \r
236         group = scsiDev.cdb[0] >> 5;\r
237         scsiDev.cdbLen = CmdGroupBytes[group];\r
238         scsiRead(scsiDev.cdb + 1, scsiDev.cdbLen - 1);\r
239 \r
240         command = scsiDev.cdb[0];\r
241 \r
242         // Prefer LUN's set by IDENTIFY messages for newer hosts.\r
243         if (scsiDev.lun < 0)\r
244         {\r
245                 scsiDev.lun = scsiDev.cdb[1] >> 5;\r
246         }\r
247 \r
248         control = scsiDev.cdb[scsiDev.cdbLen - 1];\r
249 \r
250         scsiDev.cmdCount++;\r
251 \r
252         if (scsiDev.resetFlag)\r
253         {\r
254                 // Don't log bogus commands\r
255                 scsiDev.cmdCount--;\r
256                 memset(scsiDev.cdb, 0xff, sizeof(scsiDev.cdb));\r
257                 return;\r
258         }\r
259         else if (scsiDev.parityError)\r
260         {\r
261                 scsiDev.sense.code = ABORTED_COMMAND;\r
262                 scsiDev.sense.asc = SCSI_PARITY_ERROR;\r
263                 enter_Status(CHECK_CONDITION);\r
264         }\r
265         else if ((control & 0x02) && ((control & 0x01) == 0))\r
266         {\r
267                 // FLAG set without LINK flag.\r
268                 scsiDev.sense.code = ILLEGAL_REQUEST;\r
269                 scsiDev.sense.asc = INVALID_FIELD_IN_CDB;\r
270                 enter_Status(CHECK_CONDITION);\r
271         }\r
272         else if (command == 0x12)\r
273         {\r
274                 scsiInquiry();\r
275         }\r
276         else if (command == 0x03)\r
277         {\r
278                 // REQUEST SENSE\r
279                 uint32 allocLength = scsiDev.cdb[4];\r
280 \r
281                 // As specified by the SASI and SCSI1 standard.\r
282                 // Newer initiators won't be specifying 0 anyway.\r
283                 if (allocLength == 0) allocLength = 4;\r
284 \r
285                 memset(scsiDev.data, 0, 256); // Max possible alloc length\r
286                 scsiDev.data[0] = 0xF0;\r
287                 scsiDev.data[2] = scsiDev.sense.code & 0x0F;\r
288 \r
289                 scsiDev.data[3] = transfer.lba >> 24;\r
290                 scsiDev.data[4] = transfer.lba >> 16;\r
291                 scsiDev.data[5] = transfer.lba >> 8;\r
292                 scsiDev.data[6] = transfer.lba;\r
293 \r
294                 // Additional bytes if there are errors to report\r
295                 scsiDev.data[7] = 10; // additional length\r
296                 scsiDev.data[12] = scsiDev.sense.asc >> 8;\r
297                 scsiDev.data[13] = scsiDev.sense.asc;\r
298 \r
299                 // Silently truncate results. SCSI-2 spec 8.2.14.\r
300                 enter_DataIn(allocLength);\r
301 \r
302                 // This is a good time to clear out old sense information.\r
303                 scsiDev.sense.code = NO_SENSE;\r
304                 scsiDev.sense.asc = NO_ADDITIONAL_SENSE_INFORMATION;\r
305         }\r
306         // Some old SCSI drivers do NOT properly support\r
307         // unitAttention. eg. the Mac Plus would trigger a SCSI reset\r
308         // on receiving the unit attention response on boot, thus\r
309         // triggering another unit attention condition.\r
310         else if (scsiDev.unitAttention && config->enableUnitAttention)\r
311         {\r
312                 scsiDev.sense.code = UNIT_ATTENTION;\r
313                 scsiDev.sense.asc = scsiDev.unitAttention;\r
314 \r
315                 // If initiator doesn't do REQUEST SENSE for the next command, then\r
316                 // data is lost.\r
317                 scsiDev.unitAttention = 0;\r
318 \r
319                 enter_Status(CHECK_CONDITION);\r
320         }\r
321         else if (scsiDev.lun)\r
322         {\r
323                 scsiDev.sense.code = ILLEGAL_REQUEST;\r
324                 scsiDev.sense.asc = LOGICAL_UNIT_NOT_SUPPORTED;\r
325                 enter_Status(CHECK_CONDITION);\r
326         }\r
327         else if (command == 0x17 || command == 0x16)\r
328         {\r
329                 doReserveRelease();\r
330         }\r
331         else if ((scsiDev.reservedId >= 0) &&\r
332                 (scsiDev.reservedId != scsiDev.initiatorId))\r
333         {\r
334                 enter_Status(CONFLICT);\r
335         }\r
336         else if (command == 0x1C)\r
337         {\r
338                 scsiReceiveDiagnostic();\r
339         }\r
340         else if (command == 0x1D)\r
341         {\r
342                 scsiSendDiagnostic();\r
343         }\r
344         else if (command == 0x3C)\r
345         {\r
346                 scsiReadBuffer();\r
347         }\r
348         else if (\r
349                 !scsiModeCommand() &&\r
350                 !scsiDiskCommand())\r
351         {\r
352                 scsiDev.sense.code = ILLEGAL_REQUEST;\r
353                 scsiDev.sense.asc = INVALID_COMMAND_OPERATION_CODE;\r
354                 enter_Status(CHECK_CONDITION);\r
355         }\r
356 \r
357         // Successful\r
358         if (scsiDev.phase == COMMAND) // No status set, and not in DATA_IN\r
359         {\r
360                 enter_Status(GOOD);\r
361         }\r
362 \r
363 }\r
364 \r
365 static void doReserveRelease()\r
366 {\r
367         int extentReservation = scsiDev.cdb[1] & 1;\r
368         int thirdPty = scsiDev.cdb[1] & 0x10;\r
369         int thirdPtyId = (scsiDev.cdb[1] >> 1) & 0x7;\r
370         uint8 command = scsiDev.cdb[0];\r
371 \r
372         int canRelease =\r
373                 (!thirdPty && (scsiDev.initiatorId == scsiDev.reservedId)) ||\r
374                         (thirdPty &&\r
375                                 (scsiDev.reserverId == scsiDev.initiatorId) &&\r
376                                 (scsiDev.reservedId == thirdPtyId)\r
377                         );\r
378 \r
379         if (extentReservation)\r
380         {\r
381                 // Not supported.\r
382                 scsiDev.sense.code = ILLEGAL_REQUEST;\r
383                 scsiDev.sense.asc = INVALID_FIELD_IN_CDB;\r
384                 enter_Status(CHECK_CONDITION);\r
385         }\r
386         else if (command == 0x17) // release\r
387         {\r
388                 if ((scsiDev.reservedId < 0) || canRelease)\r
389                 {\r
390                         scsiDev.reservedId = -1;\r
391                         scsiDev.reserverId = -1;\r
392                 }\r
393                 else\r
394                 {\r
395                         enter_Status(CONFLICT);\r
396                 }\r
397         }\r
398         else // assume reserve.\r
399         {\r
400                 if ((scsiDev.reservedId < 0) || canRelease)\r
401                 {\r
402                         scsiDev.reserverId = scsiDev.initiatorId;\r
403                         if (thirdPty)\r
404                         {\r
405                                 scsiDev.reservedId = thirdPtyId;\r
406                         }\r
407                         else\r
408                         {\r
409                                 scsiDev.reservedId = scsiDev.initiatorId;\r
410                         }\r
411                 }\r
412                 else\r
413                 {\r
414                         // Already reserved by someone else!\r
415                         enter_Status(CONFLICT);\r
416                 }\r
417         }\r
418 }\r
419 \r
420 static void scsiReset()\r
421 {\r
422         scsiDev.rstCount++;\r
423         ledOff();\r
424 \r
425         scsiPhyReset();\r
426         SCSI_Out_Ctl_Write(0);\r
427 \r
428         scsiDev.parityError = 0;\r
429         scsiDev.phase = BUS_FREE;\r
430         scsiDev.atnFlag = 0;\r
431         scsiDev.resetFlag = 0;\r
432         scsiDev.lun = -1;\r
433 \r
434         if (scsiDev.unitAttention != POWER_ON_RESET)\r
435         {\r
436                 scsiDev.unitAttention = SCSI_BUS_RESET;\r
437         }\r
438         scsiDev.reservedId = -1;\r
439         scsiDev.reserverId = -1;\r
440         scsiDev.sense.code = NO_SENSE;\r
441         scsiDev.sense.asc = NO_ADDITIONAL_SENSE_INFORMATION;\r
442         scsiDiskReset();\r
443 \r
444         scsiDev.postDataOutHook = NULL;\r
445 \r
446         // Sleep to allow the bus to settle down a bit.\r
447         // We must be ready again within the "Reset to selection time" of\r
448         // 250ms.\r
449         // There is no guarantee that the RST line will be negated by then.\r
450         // NOTE: We could be connected and powered by USB for configuration,\r
451         // in which case TERMPWR cannot be supplied, and reset will ALWAYS\r
452         // be true.\r
453         CyDelay(10); // 10ms.\r
454 }\r
455 \r
456 static void enter_SelectionPhase()\r
457 {\r
458         // Ignore stale versions of this flag, but ensure we know the\r
459         // current value if the flag is still set.\r
460         scsiDev.atnFlag = 0;\r
461         scsiDev.parityError = 0;\r
462         scsiDev.dataPtr = 0;\r
463         scsiDev.savedDataPtr = 0;\r
464         scsiDev.dataLen = 0;\r
465         scsiDev.status = GOOD;\r
466         scsiDev.phase = SELECTION;\r
467         scsiDev.lun = -1;\r
468 \r
469         transfer.blocks = 0;\r
470         transfer.currentBlock = 0;\r
471 \r
472         scsiDev.postDataOutHook = NULL;\r
473 }\r
474 \r
475 static void process_SelectionPhase()\r
476 {\r
477         int sel = SCSI_ReadPin(SCSI_In_SEL);\r
478         int bsy = SCSI_ReadPin(SCSI_In_BSY);\r
479 \r
480         // Only read these pins AFTER SEL and BSY - we don't want to catch them\r
481         // during a transition period.\r
482         uint8 mask = scsiReadDBxPins();\r
483         int maskBitCount = countBits(mask);\r
484         int goodParity = (Lookup_OddParity[mask] == SCSI_ReadPin(SCSI_In_DBP));\r
485 \r
486         if (!bsy && sel &&\r
487                 (mask & scsiDev.scsiIdMask) &&\r
488                 (goodParity || !config->enableParity) && (maskBitCount <= 2))\r
489         {\r
490                 // Do we enter MESSAGE OUT immediately ? SCSI 1 and 2 standards says\r
491                 // move to MESSAGE OUT if ATN is true before we assert BSY.\r
492                 // The initiator should assert ATN with SEL.\r
493                 scsiDev.atnFlag = SCSI_ReadPin(SCSI_ATN_INT);\r
494                 \r
495                 // Unit attention breaks many older SCSI hosts. Disable it completely for\r
496                 // SCSI-1 (and older) hosts, regardless of our configured setting.\r
497                 if (!scsiDev.atnFlag)\r
498                 {\r
499                         scsiDev.unitAttention = 0;\r
500                 }\r
501 \r
502                 // We've been selected!\r
503                 // Assert BSY - Selection success!\r
504                 // must happen within 200us (Selection abort time) of seeing our\r
505                 // ID + SEL.\r
506                 // (Note: the initiator will be waiting the "Selection time-out delay"\r
507                 // for our BSY response, which is actually a very generous 250ms)\r
508                 SCSI_SetPin(SCSI_Out_BSY);\r
509                 ledOn();\r
510 \r
511                 scsiDev.selCount++;\r
512 \r
513                 // Wait until the end of the selection phase.\r
514                 while (!scsiDev.resetFlag)\r
515                 {\r
516                         if (!SCSI_ReadPin(SCSI_In_SEL))\r
517                         {\r
518                                 break;\r
519                         }\r
520                 }\r
521 \r
522                 // Save our initiator now that we're no longer in a time-critical\r
523                 // section.\r
524                 // SCSI1/SASI initiators may not set their own ID.\r
525                 if (maskBitCount == 2)\r
526                 {\r
527                         int i;\r
528                         uint8 initiatorMask = mask ^ scsiDev.scsiIdMask;\r
529                         scsiDev.initiatorId = 0;\r
530                         for (i = 0; i < 8; ++i)\r
531                         {\r
532                                 if (initiatorMask & (1 << i))\r
533                                 {\r
534                                         scsiDev.initiatorId = i;\r
535                                         break;\r
536                                 }\r
537                         }\r
538                 }\r
539                 else\r
540                 {\r
541                         scsiDev.initiatorId = -1;\r
542                 }\r
543 \r
544                 scsiDev.phase = COMMAND;\r
545         }\r
546         else if (!sel)\r
547         {\r
548                 scsiDev.phase = BUS_BUSY;\r
549         }\r
550 }\r
551 \r
552 static void process_MessageOut()\r
553 {\r
554         scsiEnterPhase(MESSAGE_OUT);\r
555 \r
556         scsiDev.atnFlag = 0;\r
557         scsiDev.parityError = 0;\r
558         scsiDev.msgOut = scsiReadByte();\r
559         scsiDev.msgCount++;\r
560 \r
561         if (scsiDev.parityError)\r
562         {\r
563                 // Skip the remaining message bytes, and then start the MESSAGE_OUT\r
564                 // phase again from the start. The initiator will re-send the\r
565                 // same set of messages.\r
566                 while (SCSI_ReadPin(SCSI_ATN_INT) && !scsiDev.resetFlag)\r
567                 {\r
568                         scsiReadByte();\r
569                 }\r
570 \r
571                 // Go-back and try the message again.\r
572                 scsiDev.atnFlag = 1;\r
573                 scsiDev.parityError = 0;\r
574         }\r
575         else if (scsiDev.msgOut == 0x00)\r
576         {\r
577                 // COMMAND COMPLETE. but why would the target be receiving this ? nfi.\r
578                 enter_BusFree();\r
579         }\r
580         else if (scsiDev.msgOut == 0x06)\r
581         {\r
582                 // ABORT\r
583                 scsiDiskReset();\r
584                 enter_BusFree();\r
585         }\r
586         else if (scsiDev.msgOut == 0x0C)\r
587         {\r
588                 // BUS DEVICE RESET\r
589 \r
590                 scsiDiskReset();\r
591 \r
592                 scsiDev.unitAttention = SCSI_BUS_RESET;\r
593 \r
594                 // ANY initiator can reset the reservation state via this message.\r
595                 scsiDev.reservedId = -1;\r
596                 scsiDev.reserverId = -1;\r
597                 enter_BusFree();\r
598         }\r
599         else if (scsiDev.msgOut == 0x05)\r
600         {\r
601                 // Initiate Detected Error\r
602                 // Ignore for now\r
603         }\r
604         else if (scsiDev.msgOut == 0x0F)\r
605         {\r
606                 // INITIATE RECOVERY\r
607                 // Ignore for now\r
608         }\r
609         else if (scsiDev.msgOut == 0x10)\r
610         {\r
611                 // RELEASE RECOVERY\r
612                 // Ignore for now\r
613                 enter_BusFree();\r
614         }\r
615         else if (scsiDev.msgOut == MSG_REJECT)\r
616         {\r
617                 // Message Reject\r
618                 // Oh well.\r
619                 scsiDev.resetFlag = 1;\r
620         }\r
621         else if (scsiDev.msgOut == 0x08)\r
622         {\r
623                 // NOP\r
624         }\r
625         else if (scsiDev.msgOut == 0x09)\r
626         {\r
627                 // Message Parity Error\r
628                 // Go back and re-send the last message.\r
629                 scsiDev.phase = MESSAGE_IN;\r
630         }\r
631         else if (scsiDev.msgOut & 0x80) // 0x80 -> 0xFF\r
632         {\r
633                 // IDENTIFY\r
634                 // We don't disconnect, so ignore disconnect privilege.\r
635                 if ((scsiDev.msgOut & 0x18) || // Reserved bits set.\r
636                         (scsiDev.msgOut & 0x20))  // We don't have any target routines!\r
637                 {\r
638                         messageReject();\r
639                 }\r
640 \r
641                 scsiDev.lun = scsiDev.msgOut & 0x7;\r
642                 //scsiDev.allowDisconnect = scsiDev.msgOut & 0x40;\r
643         }\r
644         else if (scsiDev.msgOut >= 0x20 && scsiDev.msgOut <= 0x2F)\r
645         {\r
646                 // Two byte message. We don't support these. read and discard.\r
647                 scsiReadByte();\r
648         }\r
649         else if (scsiDev.msgOut == 0x01)\r
650         {\r
651                 int i;\r
652 \r
653                 // Extended message.\r
654                 int msgLen = scsiReadByte();\r
655                 if (msgLen == 0) msgLen = 256;\r
656                 for (i = 0; i < msgLen && !scsiDev.resetFlag; ++i)\r
657                 {\r
658                         // Discard bytes.\r
659                         scsiReadByte();\r
660                 }\r
661 \r
662                 // We don't support ANY extended messages.\r
663                 // Modify Data Pointer:  We don't support reselection.\r
664                 // Wide Data Transfer Request: No. 8bit only.\r
665                 // Synchronous data transfer request. No, we can't do that.\r
666                 // We don't support any 2-byte messages either.\r
667                 // And we don't support any optional 1-byte messages.\r
668                 // In each case, the correct response is MESSAGE REJECT.\r
669                 messageReject();\r
670         }\r
671         else\r
672         {\r
673                 messageReject();\r
674         }\r
675 \r
676         // Re-check the ATN flag in case it stays asserted.\r
677         scsiDev.atnFlag |= SCSI_ReadPin(SCSI_ATN_INT);\r
678 }\r
679 \r
680 void scsiPoll(void)\r
681 {\r
682         if (scsiDev.resetFlag)\r
683         {\r
684                 scsiReset();\r
685                 if ((scsiDev.resetFlag = SCSI_ReadPin(SCSI_RST_INT)))\r
686                 {\r
687                         // Still in reset phase. Do not try and process any commands.\r
688                         return;\r
689                 }\r
690         }\r
691 \r
692         switch (scsiDev.phase)\r
693         {\r
694         case BUS_FREE:\r
695                 if (SCSI_ReadPin(SCSI_In_BSY))\r
696                 {\r
697                         scsiDev.phase = BUS_BUSY;\r
698                 }\r
699                 // The Arbitration phase is optional for SCSI1/SASI hosts if there is only\r
700                 // one initiator in the chain. Support this by moving\r
701                 // straight to selection if SEL is asserted.\r
702                 // ie. the initiator won't assert BSY and it's own ID before moving to selection.\r
703                 else if (SCSI_ReadPin(SCSI_In_SEL))\r
704                 {\r
705                         enter_SelectionPhase();\r
706                 }\r
707         break;\r
708 \r
709         case BUS_BUSY:\r
710                 // Someone is using the bus. Perhaps they are trying to\r
711                 // select us.\r
712                 if (SCSI_ReadPin(SCSI_In_SEL))\r
713                 {\r
714                         enter_SelectionPhase();\r
715                 }\r
716                 else if (!SCSI_ReadPin(SCSI_In_BSY))\r
717                 {\r
718                         scsiDev.phase = BUS_FREE;\r
719                 }\r
720         break;\r
721 \r
722         case ARBITRATION:\r
723                 // TODO Support reselection.\r
724                 break;\r
725 \r
726         case SELECTION:\r
727                 process_SelectionPhase();\r
728         break;\r
729 \r
730         case RESELECTION:\r
731                 // Not currently supported!\r
732         break;\r
733 \r
734         case COMMAND:\r
735                 // Do not check ATN here. SCSI 1 & 2 initiators must set ATN\r
736                 // and SEL together upon entering the selection phase if they\r
737                 // want to send a message (IDENTIFY) immediately.\r
738                 if (scsiDev.atnFlag)\r
739                 {\r
740                         process_MessageOut();\r
741                 }\r
742                 else\r
743                 {\r
744                         process_Command();\r
745                 }\r
746         break;\r
747 \r
748         case DATA_IN:\r
749                 scsiDev.atnFlag |= SCSI_ReadPin(SCSI_ATN_INT);\r
750                 if (scsiDev.atnFlag)\r
751                 {\r
752                         process_MessageOut();\r
753                 }\r
754                 else\r
755                 {\r
756                         process_DataIn();\r
757                 }\r
758         break;\r
759 \r
760         case DATA_OUT:\r
761                 scsiDev.atnFlag |= SCSI_ReadPin(SCSI_ATN_INT);\r
762                 if (scsiDev.atnFlag)\r
763                 {\r
764                         process_MessageOut();\r
765                 }\r
766                 else\r
767                 {\r
768                         process_DataOut();\r
769                 }\r
770         break;\r
771 \r
772         case STATUS:\r
773                 scsiDev.atnFlag |= SCSI_ReadPin(SCSI_ATN_INT);\r
774                 if (scsiDev.atnFlag)\r
775                 {\r
776                         process_MessageOut();\r
777                 }\r
778                 else\r
779                 {\r
780                         process_Status();\r
781                 }\r
782         break;\r
783 \r
784         case MESSAGE_IN:\r
785                 scsiDev.atnFlag |= SCSI_ReadPin(SCSI_ATN_INT);\r
786                 if (scsiDev.atnFlag)\r
787                 {\r
788                         process_MessageOut();\r
789                 }\r
790                 else\r
791                 {\r
792                         process_MessageIn();\r
793                 }\r
794 \r
795         break;\r
796 \r
797         case MESSAGE_OUT:\r
798                 process_MessageOut();\r
799         break;\r
800         }\r
801 }\r
802 \r
803 void scsiInit()\r
804 {\r
805         scsiDev.scsiIdMask = 1 << (config->scsiId);\r
806 \r
807         scsiDev.atnFlag = 0;\r
808         scsiDev.resetFlag = 1;\r
809         scsiDev.phase = BUS_FREE;\r
810         scsiDev.reservedId = -1;\r
811         scsiDev.reserverId = -1;\r
812         scsiDev.unitAttention = POWER_ON_RESET;\r
813 }\r
814 \r