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