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