37518337241c38d211c1feb2407c87a84dc5959b
[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         SCSI_ClearPin(SCSI_Out_BSY);\r
52         // We now have a Bus Clear Delay of 800ns to release remaining signals.\r
53         SCSI_ClearPin(SCSI_Out_MSG);\r
54         SCSI_ClearPin(SCSI_Out_CD);\r
55         SCSI_CTL_IO_Write(0);\r
56 \r
57         // Wait for the initiator to cease driving signals\r
58         // Bus settle delay + bus clear delay = 1200ns\r
59         CyDelayUs(2);\r
60 \r
61         ledOff();\r
62         scsiDev.phase = BUS_FREE;\r
63 }\r
64 \r
65 static void enter_MessageIn(uint8 message)\r
66 {\r
67         scsiDev.msgIn = message;\r
68         scsiDev.phase = MESSAGE_IN;\r
69 }\r
70 \r
71 static void process_MessageIn()\r
72 {\r
73         scsiEnterPhase(MESSAGE_IN);\r
74         scsiWriteByte(scsiDev.msgIn);\r
75 \r
76         if (scsiDev.atnFlag)\r
77         {\r
78                 // If there was a parity error, we go\r
79                 // back to MESSAGE_OUT first, get out parity error message, then come\r
80                 // back here.\r
81         }\r
82         else if ((scsiDev.msgIn == MSG_LINKED_COMMAND_COMPLETE) ||\r
83                 (scsiDev.msgIn == MSG_LINKED_COMMAND_COMPLETE_WITH_FLAG))\r
84         {\r
85                 // Go back to the command phase and start again.\r
86                 scsiDev.phase = COMMAND;\r
87                 scsiDev.parityError = 0;\r
88                 scsiDev.dataPtr = 0;\r
89                 scsiDev.savedDataPtr = 0;\r
90                 scsiDev.dataLen = 0;\r
91                 scsiDev.status = GOOD;\r
92                 transfer.blocks = 0;\r
93                 transfer.currentBlock = 0;\r
94         }\r
95         else /*if (scsiDev.msgIn == MSG_COMMAND_COMPLETE)*/\r
96         {\r
97                 enter_BusFree();\r
98         }\r
99 }\r
100 \r
101 static void messageReject()\r
102 {\r
103         scsiEnterPhase(MESSAGE_IN);\r
104         scsiWriteByte(MSG_REJECT);\r
105 }\r
106 \r
107 static void enter_Status(uint8 status)\r
108 {\r
109         scsiDev.status = status;\r
110         scsiDev.phase = STATUS;\r
111 \r
112 \r
113         #ifdef MM_DEBUG\r
114         scsiDev.lastStatus = scsiDev.status;\r
115         scsiDev.lastSense = scsiDev.sense.code;\r
116         #endif\r
117 }\r
118 \r
119 static void process_Status()\r
120 {\r
121         scsiEnterPhase(STATUS);\r
122 \r
123         uint8 message;\r
124 \r
125         uint8 control = scsiDev.cdb[scsiDev.cdbLen - 1];\r
126         if ((scsiDev.status == GOOD) && (control & 0x01))\r
127         {\r
128                 // Linked command.\r
129                 scsiDev.status = INTERMEDIATE;\r
130                 if (control & 0x02)\r
131                 {\r
132                         message = MSG_LINKED_COMMAND_COMPLETE_WITH_FLAG;\r
133                 }\r
134                 else\r
135                 {\r
136                         message = MSG_LINKED_COMMAND_COMPLETE;\r
137                 }\r
138         }\r
139         else\r
140         {\r
141                 message = MSG_COMMAND_COMPLETE;\r
142         }\r
143         scsiWriteByte(scsiDev.status);\r
144 \r
145         #ifdef MM_DEBUG\r
146         scsiDev.lastStatus = scsiDev.status;\r
147         scsiDev.lastSense = scsiDev.sense.code;\r
148         #endif\r
149 \r
150         // Command Complete occurs AFTER a valid status has been\r
151         // sent. then we go bus-free.\r
152         enter_MessageIn(message);\r
153 }\r
154 \r
155 static void enter_DataIn(int len)\r
156 {\r
157         scsiDev.dataLen = len;\r
158         scsiDev.phase = DATA_IN;\r
159 }\r
160 \r
161 static void process_DataIn()\r
162 {\r
163         uint32 len;\r
164 \r
165         if (scsiDev.dataLen > sizeof(scsiDev.data))\r
166         {\r
167                 scsiDev.dataLen = sizeof(scsiDev.data);\r
168         }\r
169 \r
170         len = scsiDev.dataLen - scsiDev.dataPtr;\r
171         if (len > 0)\r
172         {\r
173                 scsiEnterPhase(DATA_IN);\r
174                 scsiWrite(scsiDev.data + scsiDev.dataPtr, len);\r
175                 scsiDev.dataPtr += len;\r
176         }\r
177 \r
178         if ((scsiDev.dataPtr >= scsiDev.dataLen) &&\r
179                 (transfer.currentBlock == transfer.blocks))\r
180         {\r
181                 enter_Status(GOOD);\r
182         }\r
183 }\r
184 \r
185 static void process_DataOut()\r
186 {\r
187         uint32 len;\r
188 \r
189         if (scsiDev.dataLen > sizeof(scsiDev.data))\r
190         {\r
191                 scsiDev.dataLen = sizeof(scsiDev.data);\r
192         }\r
193 \r
194         scsiDev.parityError = 0;\r
195         len = scsiDev.dataLen - scsiDev.dataPtr;\r
196         if (len > 0)\r
197         {\r
198                 scsiEnterPhase(DATA_OUT);\r
199 \r
200                 scsiRead(scsiDev.data + scsiDev.dataPtr, len);\r
201                 scsiDev.dataPtr += len;\r
202 \r
203                 // TODO re-implement parity checking\r
204                 if (0 && scsiDev.parityError && config->enableParity)\r
205                 {\r
206                         scsiDev.sense.code = ABORTED_COMMAND;\r
207                         scsiDev.sense.asc = SCSI_PARITY_ERROR;\r
208                         enter_Status(CHECK_CONDITION);\r
209                 }\r
210         }\r
211 \r
212         if ((scsiDev.dataPtr >= scsiDev.dataLen) &&\r
213                 (transfer.currentBlock == transfer.blocks))\r
214         {\r
215                 if (scsiDev.postDataOutHook != NULL)\r
216                 {\r
217                         scsiDev.postDataOutHook();\r
218                 }\r
219                 else\r
220                 {\r
221                         enter_Status(GOOD);\r
222                 }\r
223         }\r
224 }\r
225 \r
226 static const uint8 CmdGroupBytes[8] = {6, 10, 10, 6, 6, 12, 6, 6};\r
227 static void process_Command()\r
228 {\r
229         int group;\r
230         uint8 command;\r
231         uint8 lun;\r
232         uint8 control;\r
233 \r
234         scsiEnterPhase(COMMAND);\r
235         scsiDev.parityError = 0;\r
236 \r
237         memset(scsiDev.cdb, 0, sizeof(scsiDev.cdb));\r
238         scsiDev.cdb[0] = scsiReadByte();\r
239 \r
240         group = scsiDev.cdb[0] >> 5;\r
241         scsiDev.cdbLen = CmdGroupBytes[group];\r
242         scsiRead(scsiDev.cdb + 1, scsiDev.cdbLen - 1);\r
243 \r
244         command = scsiDev.cdb[0];\r
245         lun = scsiDev.cdb[1] >> 5;\r
246         control = scsiDev.cdb[scsiDev.cdbLen - 1];\r
247 \r
248         #ifdef MM_DEBUG\r
249         scsiDev.cmdCount++;\r
250         #endif\r
251 \r
252         if (scsiDev.resetFlag)\r
253         {\r
254 #ifdef MM_DEBUG\r
255                 // Don't log bogus commands\r
256                 scsiDev.cmdCount--;\r
257                 memset(scsiDev.cdb, 0xff, sizeof(scsiDev.cdb));\r
258 #endif\r
259                 return;\r
260         }\r
261         else if (scsiDev.parityError)\r
262         {\r
263                 scsiDev.sense.code = ABORTED_COMMAND;\r
264                 scsiDev.sense.asc = SCSI_PARITY_ERROR;\r
265                 enter_Status(CHECK_CONDITION);\r
266         }\r
267         else if ((control & 0x02) && ((control & 0x01) == 0))\r
268         {\r
269                 // FLAG set without LINK flag.\r
270                 scsiDev.sense.code = ILLEGAL_REQUEST;\r
271                 scsiDev.sense.asc = INVALID_FIELD_IN_CDB;\r
272                 enter_Status(CHECK_CONDITION);\r
273         }\r
274         else if (command == 0x12)\r
275         {\r
276                 scsiInquiry();\r
277         }\r
278         else if (command == 0x03)\r
279         {\r
280                 // REQUEST SENSE\r
281                 uint32 allocLength = scsiDev.cdb[4];\r
282                 if (allocLength == 0) allocLength = 256;\r
283                 memset(scsiDev.data, 0, 18);\r
284                 scsiDev.data[0] = 0xF0;\r
285                 scsiDev.data[2] = scsiDev.sense.code & 0x0F;\r
286 \r
287                 scsiDev.data[3] = transfer.lba >> 24;\r
288                 scsiDev.data[4] = transfer.lba >> 16;\r
289                 scsiDev.data[5] = transfer.lba >> 8;\r
290                 scsiDev.data[6] = transfer.lba;\r
291 \r
292                 // Additional bytes if there are errors to report\r
293                 int responseLength;\r
294                 if (scsiDev.sense.code == NO_SENSE)\r
295                 {\r
296                         responseLength = 8;\r
297                 }\r
298                 else\r
299                 {\r
300                         responseLength = 18;\r
301                         scsiDev.data[7] = 10; // additional length\r
302                         scsiDev.data[12] = scsiDev.sense.asc >> 8;\r
303                         scsiDev.data[13] = scsiDev.sense.asc;\r
304                 }\r
305 \r
306                 // Silently truncate results. SCSI-2 spec 8.2.14.\r
307                 enter_DataIn(\r
308                         (allocLength < responseLength) ? allocLength : responseLength\r
309                         );\r
310 \r
311                 // This is a good time to clear out old sense information.\r
312                 scsiDev.sense.code = NO_SENSE;\r
313                 scsiDev.sense.asc = NO_ADDITIONAL_SENSE_INFORMATION;\r
314         }\r
315         // Some old SCSI drivers do NOT properly support\r
316         // unitAttention. eg. the Mac Plus would trigger a SCSI reset\r
317         // on receiving the unit attention response on boot, thus\r
318         // triggering another unit attention condition.\r
319         else if (scsiDev.unitAttention && config->enableUnitAttention)\r
320         {\r
321                 scsiDev.sense.code = UNIT_ATTENTION;\r
322                 scsiDev.sense.asc = scsiDev.unitAttention;\r
323 \r
324                 // If initiator doesn't do REQUEST SENSE for the next command, then\r
325                 // data is lost.\r
326                 scsiDev.unitAttention = 0;\r
327 \r
328                 enter_Status(CHECK_CONDITION);\r
329         }\r
330         else if (lun)\r
331         {\r
332                 scsiDev.sense.code = ILLEGAL_REQUEST;\r
333                 scsiDev.sense.asc = LOGICAL_UNIT_NOT_SUPPORTED;\r
334                 enter_Status(CHECK_CONDITION);\r
335         }\r
336         else if (command == 0x17 || command == 0x16)\r
337         {\r
338                 doReserveRelease();\r
339         }\r
340         else if ((scsiDev.reservedId >= 0) &&\r
341                 (scsiDev.reservedId != scsiDev.initiatorId))\r
342         {\r
343                 enter_Status(CONFLICT);\r
344         }\r
345         else if (command == 0x1C)\r
346         {\r
347                 scsiReceiveDiagnostic();\r
348         }\r
349         else if (command == 0x1D)\r
350         {\r
351                 scsiSendDiagnostic();\r
352         }\r
353         else if (\r
354                 !scsiModeCommand() &&\r
355                 !scsiDiskCommand())\r
356         {\r
357                 scsiDev.sense.code = ILLEGAL_REQUEST;\r
358                 scsiDev.sense.asc = INVALID_COMMAND_OPERATION_CODE;\r
359                 enter_Status(CHECK_CONDITION);\r
360         }\r
361 \r
362         // Successful\r
363         if (scsiDev.phase == COMMAND) // No status set, and not in DATA_IN\r
364         {\r
365                 enter_Status(GOOD);\r
366         }\r
367 \r
368 }\r
369 \r
370 static void doReserveRelease()\r
371 {\r
372         int extentReservation = scsiDev.cdb[1] & 1;\r
373         int thirdPty = scsiDev.cdb[1] & 0x10;\r
374         int thirdPtyId = (scsiDev.cdb[1] >> 1) & 0x7;\r
375         uint8 command = scsiDev.cdb[0];\r
376 \r
377         int canRelease =\r
378                 (!thirdPty && (scsiDev.initiatorId == scsiDev.reservedId)) ||\r
379                         (thirdPty &&\r
380                                 (scsiDev.reserverId == scsiDev.initiatorId) &&\r
381                                 (scsiDev.reservedId == thirdPtyId)\r
382                         );\r
383 \r
384         if (extentReservation)\r
385         {\r
386                 // Not supported.\r
387                 scsiDev.sense.code = ILLEGAL_REQUEST;\r
388                 scsiDev.sense.asc = INVALID_FIELD_IN_CDB;\r
389                 enter_Status(CHECK_CONDITION);\r
390         }\r
391         else if (command == 0x17) // release\r
392         {\r
393                 if ((scsiDev.reservedId < 0) || canRelease)\r
394                 {\r
395                         scsiDev.reservedId = -1;\r
396                         scsiDev.reserverId = -1;\r
397                 }\r
398                 else\r
399                 {\r
400                         enter_Status(CONFLICT);\r
401                 }\r
402         }\r
403         else // assume reserve.\r
404         {\r
405                 if ((scsiDev.reservedId < 0) || canRelease)\r
406                 {\r
407                         scsiDev.reserverId = scsiDev.initiatorId;\r
408                         if (thirdPty)\r
409                         {\r
410                                 scsiDev.reservedId = thirdPtyId;\r
411                         }\r
412                         else\r
413                         {\r
414                                 scsiDev.reservedId = scsiDev.initiatorId;\r
415                         }\r
416                 }\r
417                 else\r
418                 {\r
419                         // Already reserved by someone else!\r
420                         enter_Status(CONFLICT);\r
421                 }\r
422         }\r
423 }\r
424 \r
425 static void scsiReset()\r
426 {\r
427 #ifdef MM_DEBUG\r
428         scsiDev.rstCount++;\r
429 #endif\r
430         ledOff();\r
431 \r
432         scsiPhyReset();\r
433 \r
434         scsiDev.parityError = 0;\r
435         scsiDev.phase = BUS_FREE;\r
436         scsiDev.atnFlag = 0;\r
437         scsiDev.resetFlag = 0;\r
438 \r
439         if (scsiDev.unitAttention != POWER_ON_RESET)\r
440         {\r
441                 scsiDev.unitAttention = SCSI_BUS_RESET;\r
442         }\r
443         scsiDev.reservedId = -1;\r
444         scsiDev.reserverId = -1;\r
445         scsiDev.sense.code = NO_SENSE;\r
446         scsiDev.sense.asc = NO_ADDITIONAL_SENSE_INFORMATION;\r
447         scsiDiskReset();\r
448 \r
449         scsiDev.postDataOutHook = NULL;\r
450 \r
451         // Sleep to allow the bus to settle down a bit.\r
452         // We must be ready again within the "Reset to selection time" of\r
453         // 250ms.\r
454         // There is no guarantee that the RST line will be negated by then.\r
455         // NOTE: We could be connected and powered by USB for configuration,\r
456         // in which case TERMPWR cannot be supplied, and reset will ALWAYS\r
457         // be true.\r
458         CyDelay(10); // 10ms.\r
459 }\r
460 \r
461 static void enter_SelectionPhase()\r
462 {\r
463         // Ignore stale versions of this flag, but ensure we know the\r
464         // current value if the flag is still set.\r
465         scsiDev.atnFlag = 0;\r
466         scsiDev.parityError = 0;\r
467         scsiDev.dataPtr = 0;\r
468         scsiDev.savedDataPtr = 0;\r
469         scsiDev.dataLen = 0;\r
470         scsiDev.status = GOOD;\r
471         scsiDev.phase = SELECTION;\r
472 \r
473         transfer.blocks = 0;\r
474         transfer.currentBlock = 0;\r
475 \r
476         scsiDev.postDataOutHook = NULL;\r
477 }\r
478 \r
479 static void process_SelectionPhase()\r
480 {\r
481         int sel = SCSI_ReadPin(SCSI_In_SEL);\r
482         int bsy = SCSI_ReadPin(SCSI_In_BSY);\r
483 \r
484         // Only read these pins AFTER SEL and BSY - we don't want to catch them\r
485         // during a transition period.\r
486         uint8 mask = scsiReadDBxPins();\r
487         int maskBitCount = countBits(mask);\r
488         int goodParity = (Lookup_OddParity[mask] == SCSI_ReadPin(SCSI_In_DBP));\r
489 \r
490         if (!bsy && sel &&\r
491                 (mask & scsiDev.scsiIdMask) &&\r
492                 (goodParity || !config->enableParity) && (maskBitCount <= 2))\r
493         {\r
494                 // Do we enter MESSAGE OUT immediately ? SCSI 1 and 2 standards says\r
495                 // move to MESSAGE OUT if ATN is true before we release BSY.\r
496                 // The initiate should assert ATN with SEL.\r
497                 scsiDev.atnFlag = SCSI_ReadPin(SCSI_ATN_INT);\r
498 \r
499                 // We've been selected!\r
500                 // Assert BSY - Selection success!\r
501                 // must happen within 200us (Selection abort time) of seeing our\r
502                 // ID + SEL.\r
503                 // (Note: the initiator will be waiting the "Selection time-out delay"\r
504                 // for our BSY response, which is actually a very generous 250ms)\r
505                 SCSI_SetPin(SCSI_Out_BSY);\r
506                 ledOn();\r
507 \r
508                 #ifdef MM_DEBUG\r
509                 scsiDev.selCount++;\r
510                 #endif\r
511 \r
512                 // Wait until the end of the selection phase.\r
513                 while (!scsiDev.resetFlag)\r
514                 {\r
515                         if (!SCSI_ReadPin(SCSI_In_SEL))\r
516                         {\r
517                                 break;\r
518                         }\r
519                 }\r
520 \r
521                 // Save our initiator now that we're no longer in a time-critical\r
522                 // section.\r
523                 // SCSI1/SASI initiators may not set their own ID.\r
524                 if (maskBitCount == 2)\r
525                 {\r
526                         int i;\r
527                         uint8 initiatorMask = mask ^ scsiDev.scsiIdMask;\r
528                         scsiDev.initiatorId = 0;\r
529                         for (i = 0; i < 8; ++i)\r
530                         {\r
531                                 if (initiatorMask & (1 << i))\r
532                                 {\r
533                                         scsiDev.initiatorId = i;\r
534                                         break;\r
535                                 }\r
536                         }\r
537                 }\r
538                 else\r
539                 {\r
540                         scsiDev.initiatorId = -1;\r
541                 }\r
542 \r
543                 scsiDev.phase = COMMAND;\r
544         }\r
545         else if (!sel)\r
546         {\r
547                 scsiDev.phase = BUS_BUSY;\r
548         }\r
549 }\r
550 \r
551 static void process_MessageOut()\r
552 {\r
553         scsiEnterPhase(MESSAGE_OUT);\r
554 \r
555         scsiDev.atnFlag = 0;\r
556         scsiDev.parityError = 0;\r
557         scsiDev.msgOut = scsiReadByte();\r
558 #ifdef MM_DEBUG\r
559         scsiDev.msgCount++;\r
560 #endif\r
561 \r
562         if (scsiDev.parityError)\r
563         {\r
564                 // Skip the remaining message bytes, and then start the MESSAGE_OUT\r
565                 // phase again from the start. The initiator will re-send the\r
566                 // same set of messages.\r
567                 while (SCSI_ReadPin(SCSI_ATN_INT) && !scsiDev.resetFlag)\r
568                 {\r
569                         scsiReadByte();\r
570                 }\r
571 \r
572                 // Go-back and try the message again.\r
573                 scsiDev.atnFlag = 1;\r
574                 scsiDev.parityError = 0;\r
575         }\r
576         else if (scsiDev.msgOut == 0x00)\r
577         {\r
578                 // COMMAND COMPLETE. but why would the target be receiving this ? nfi.\r
579                 enter_BusFree();\r
580         }\r
581         else if (scsiDev.msgOut == 0x06)\r
582         {\r
583                 // ABORT\r
584                 scsiDiskReset();\r
585                 enter_BusFree();\r
586         }\r
587         else if (scsiDev.msgOut == 0x0C)\r
588         {\r
589                 // BUS DEVICE RESET\r
590 \r
591                 scsiDiskReset();\r
592 \r
593                 scsiDev.unitAttention = SCSI_BUS_RESET;\r
594 \r
595                 // ANY initiator can reset the reservation state via this message.\r
596                 scsiDev.reservedId = -1;\r
597                 scsiDev.reserverId = -1;\r
598                 enter_BusFree();\r
599         }\r
600         else if (scsiDev.msgOut == 0x05)\r
601         {\r
602                 // Initiate Detected Error\r
603                 // Ignore for now\r
604         }\r
605         else if (scsiDev.msgOut == 0x0F)\r
606         {\r
607                 // INITIATE RECOVERY\r
608                 // Ignore for now\r
609         }\r
610         else if (scsiDev.msgOut == 0x10)\r
611         {\r
612                 // RELEASE RECOVERY\r
613                 // Ignore for now\r
614                 enter_BusFree();\r
615         }\r
616         else if (scsiDev.msgOut == MSG_REJECT)\r
617         {\r
618                 // Message Reject\r
619                 // Oh well.\r
620                 scsiDev.resetFlag = 1;\r
621         }\r
622         else if (scsiDev.msgOut == 0x08)\r
623         {\r
624                 // NOP\r
625         }\r
626         else if (scsiDev.msgOut == 0x09)\r
627         {\r
628                 // Message Parity Error\r
629                 // Go back and re-send the last message.\r
630                 scsiDev.phase = MESSAGE_IN;\r
631         }\r
632         else if (scsiDev.msgOut & 0x80) // 0x80 -> 0xFF\r
633         {\r
634                 // IDENTIFY\r
635                 // We don't disconnect, so ignore disconnect privilege.\r
636                 if ((scsiDev.msgOut & 0x18) || // Reserved bits set.\r
637                         (scsiDev.msgOut & 0x20)  || // We don't have any target routines!\r
638                         (scsiDev.msgOut & 0x7) // We only support LUN 0!\r
639                         )\r
640                 {\r
641                         messageReject();\r
642                 }\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