1 // Copyright (C) 2014 Michael McMaster <michael@codesrc.com>
\r
3 // This file is part of SCSI2SD.
\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
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
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
20 #include "scsiPhy.h"
\r
23 #include "diagnostic.h"
\r
25 #include "inquiry.h"
\r
32 // Global SCSI device state.
\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
47 static void doReserveRelease(void);
\r
49 static void enter_BusFree()
\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
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
59 // Wait for the initiator to cease driving signals
\r
60 // Bus settle delay + bus clear delay = 1200ns
\r
64 scsiDev.phase = BUS_FREE;
\r
67 static void enter_MessageIn(uint8 message)
\r
69 scsiDev.msgIn = message;
\r
70 scsiDev.phase = MESSAGE_IN;
\r
73 static void process_MessageIn()
\r
75 scsiEnterPhase(MESSAGE_IN);
\r
76 scsiWriteByte(scsiDev.msgIn);
\r
78 if (scsiDev.atnFlag)
\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
84 else if ((scsiDev.msgIn == MSG_LINKED_COMMAND_COMPLETE) ||
\r
85 (scsiDev.msgIn == MSG_LINKED_COMMAND_COMPLETE_WITH_FLAG))
\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
97 else /*if (scsiDev.msgIn == MSG_COMMAND_COMPLETE)*/
\r
103 static void messageReject()
\r
105 scsiEnterPhase(MESSAGE_IN);
\r
106 scsiWriteByte(MSG_REJECT);
\r
109 static void enter_Status(uint8 status)
\r
111 scsiDev.status = status;
\r
112 scsiDev.phase = STATUS;
\r
116 scsiDev.lastStatus = scsiDev.status;
\r
117 scsiDev.lastSense = scsiDev.sense.code;
\r
121 static void process_Status()
\r
123 scsiEnterPhase(STATUS);
\r
127 uint8 control = scsiDev.cdb[scsiDev.cdbLen - 1];
\r
128 if ((scsiDev.status == GOOD) && (control & 0x01))
\r
131 scsiDev.status = INTERMEDIATE;
\r
132 if (control & 0x02)
\r
134 message = MSG_LINKED_COMMAND_COMPLETE_WITH_FLAG;
\r
138 message = MSG_LINKED_COMMAND_COMPLETE;
\r
143 message = MSG_COMMAND_COMPLETE;
\r
145 scsiWriteByte(scsiDev.status);
\r
148 scsiDev.lastStatus = scsiDev.status;
\r
149 scsiDev.lastSense = scsiDev.sense.code;
\r
152 // Command Complete occurs AFTER a valid status has been
\r
153 // sent. then we go bus-free.
\r
154 enter_MessageIn(message);
\r
157 static void enter_DataIn(int len)
\r
159 scsiDev.dataLen = len;
\r
160 scsiDev.phase = DATA_IN;
\r
163 static void process_DataIn()
\r
167 if (scsiDev.dataLen > sizeof(scsiDev.data))
\r
169 scsiDev.dataLen = sizeof(scsiDev.data);
\r
172 len = scsiDev.dataLen - scsiDev.dataPtr;
\r
175 scsiEnterPhase(DATA_IN);
\r
176 scsiWrite(scsiDev.data + scsiDev.dataPtr, len);
\r
177 scsiDev.dataPtr += len;
\r
180 if ((scsiDev.dataPtr >= scsiDev.dataLen) &&
\r
181 (transfer.currentBlock == transfer.blocks))
\r
183 enter_Status(GOOD);
\r
187 static void process_DataOut()
\r
191 if (scsiDev.dataLen > sizeof(scsiDev.data))
\r
193 scsiDev.dataLen = sizeof(scsiDev.data);
\r
196 scsiDev.parityError = 0;
\r
197 len = scsiDev.dataLen - scsiDev.dataPtr;
\r
200 scsiEnterPhase(DATA_OUT);
\r
202 scsiRead(scsiDev.data + scsiDev.dataPtr, len);
\r
203 scsiDev.dataPtr += len;
\r
205 // TODO re-implement parity checking
\r
206 if (0 && scsiDev.parityError && config->enableParity)
\r
208 scsiDev.sense.code = ABORTED_COMMAND;
\r
209 scsiDev.sense.asc = SCSI_PARITY_ERROR;
\r
210 enter_Status(CHECK_CONDITION);
\r
214 if ((scsiDev.dataPtr >= scsiDev.dataLen) &&
\r
215 (transfer.currentBlock == transfer.blocks))
\r
217 if (scsiDev.postDataOutHook != NULL)
\r
219 scsiDev.postDataOutHook();
\r
223 enter_Status(GOOD);
\r
228 static const uint8 CmdGroupBytes[8] = {6, 10, 10, 6, 6, 12, 6, 6};
\r
229 static void process_Command()
\r
236 scsiEnterPhase(COMMAND);
\r
237 scsiDev.parityError = 0;
\r
239 memset(scsiDev.cdb, 0, sizeof(scsiDev.cdb));
\r
240 scsiDev.cdb[0] = scsiReadByte();
\r
242 group = scsiDev.cdb[0] >> 5;
\r
243 scsiDev.cdbLen = CmdGroupBytes[group];
\r
244 scsiRead(scsiDev.cdb + 1, scsiDev.cdbLen - 1);
\r
246 command = scsiDev.cdb[0];
\r
247 lun = scsiDev.cdb[1] >> 5;
\r
248 control = scsiDev.cdb[scsiDev.cdbLen - 1];
\r
251 scsiDev.cmdCount++;
\r
254 if (scsiDev.resetFlag)
\r
257 // Don't log bogus commands
\r
258 scsiDev.cmdCount--;
\r
259 memset(scsiDev.cdb, 0xff, sizeof(scsiDev.cdb));
\r
263 else if (scsiDev.parityError)
\r
265 scsiDev.sense.code = ABORTED_COMMAND;
\r
266 scsiDev.sense.asc = SCSI_PARITY_ERROR;
\r
267 enter_Status(CHECK_CONDITION);
\r
269 else if ((control & 0x02) && ((control & 0x01) == 0))
\r
271 // FLAG set without LINK flag.
\r
272 scsiDev.sense.code = ILLEGAL_REQUEST;
\r
273 scsiDev.sense.asc = INVALID_FIELD_IN_CDB;
\r
274 enter_Status(CHECK_CONDITION);
\r
276 else if (command == 0x12)
\r
280 else if (command == 0x03)
\r
283 uint32 allocLength = scsiDev.cdb[4];
\r
284 if (allocLength == 0) allocLength = 256;
\r
285 memset(scsiDev.data, 0, 18);
\r
286 scsiDev.data[0] = 0xF0;
\r
287 scsiDev.data[2] = scsiDev.sense.code & 0x0F;
\r
289 scsiDev.data[3] = transfer.lba >> 24;
\r
290 scsiDev.data[4] = transfer.lba >> 16;
\r
291 scsiDev.data[5] = transfer.lba >> 8;
\r
292 scsiDev.data[6] = transfer.lba;
\r
294 // Additional bytes if there are errors to report
\r
295 int responseLength;
\r
296 if (scsiDev.sense.code == NO_SENSE)
\r
298 responseLength = 8;
\r
302 responseLength = 18;
\r
303 scsiDev.data[7] = 10; // additional length
\r
304 scsiDev.data[12] = scsiDev.sense.asc >> 8;
\r
305 scsiDev.data[13] = scsiDev.sense.asc;
\r
308 // Silently truncate results. SCSI-2 spec 8.2.14.
\r
310 (allocLength < responseLength) ? allocLength : responseLength
\r
313 // This is a good time to clear out old sense information.
\r
314 scsiDev.sense.code = NO_SENSE;
\r
315 scsiDev.sense.asc = NO_ADDITIONAL_SENSE_INFORMATION;
\r
317 // Some old SCSI drivers do NOT properly support
\r
318 // unitAttention. eg. the Mac Plus would trigger a SCSI reset
\r
319 // on receiving the unit attention response on boot, thus
\r
320 // triggering another unit attention condition.
\r
321 else if (scsiDev.unitAttention && config->enableUnitAttention)
\r
323 scsiDev.sense.code = UNIT_ATTENTION;
\r
324 scsiDev.sense.asc = scsiDev.unitAttention;
\r
326 // If initiator doesn't do REQUEST SENSE for the next command, then
\r
328 scsiDev.unitAttention = 0;
\r
330 enter_Status(CHECK_CONDITION);
\r
334 scsiDev.sense.code = ILLEGAL_REQUEST;
\r
335 scsiDev.sense.asc = LOGICAL_UNIT_NOT_SUPPORTED;
\r
336 enter_Status(CHECK_CONDITION);
\r
338 else if (command == 0x17 || command == 0x16)
\r
340 doReserveRelease();
\r
342 else if ((scsiDev.reservedId >= 0) &&
\r
343 (scsiDev.reservedId != scsiDev.initiatorId))
\r
345 enter_Status(CONFLICT);
\r
347 else if (command == 0x1C)
\r
349 scsiReceiveDiagnostic();
\r
351 else if (command == 0x1D)
\r
353 scsiSendDiagnostic();
\r
356 !scsiModeCommand() &&
\r
357 !scsiDiskCommand())
\r
359 scsiDev.sense.code = ILLEGAL_REQUEST;
\r
360 scsiDev.sense.asc = INVALID_COMMAND_OPERATION_CODE;
\r
361 enter_Status(CHECK_CONDITION);
\r
365 if (scsiDev.phase == COMMAND) // No status set, and not in DATA_IN
\r
367 enter_Status(GOOD);
\r
372 static void doReserveRelease()
\r
374 int extentReservation = scsiDev.cdb[1] & 1;
\r
375 int thirdPty = scsiDev.cdb[1] & 0x10;
\r
376 int thirdPtyId = (scsiDev.cdb[1] >> 1) & 0x7;
\r
377 uint8 command = scsiDev.cdb[0];
\r
380 (!thirdPty && (scsiDev.initiatorId == scsiDev.reservedId)) ||
\r
382 (scsiDev.reserverId == scsiDev.initiatorId) &&
\r
383 (scsiDev.reservedId == thirdPtyId)
\r
386 if (extentReservation)
\r
389 scsiDev.sense.code = ILLEGAL_REQUEST;
\r
390 scsiDev.sense.asc = INVALID_FIELD_IN_CDB;
\r
391 enter_Status(CHECK_CONDITION);
\r
393 else if (command == 0x17) // release
\r
395 if ((scsiDev.reservedId < 0) || canRelease)
\r
397 scsiDev.reservedId = -1;
\r
398 scsiDev.reserverId = -1;
\r
402 enter_Status(CONFLICT);
\r
405 else // assume reserve.
\r
407 if ((scsiDev.reservedId < 0) || canRelease)
\r
409 scsiDev.reserverId = scsiDev.initiatorId;
\r
412 scsiDev.reservedId = thirdPtyId;
\r
416 scsiDev.reservedId = scsiDev.initiatorId;
\r
421 // Already reserved by someone else!
\r
422 enter_Status(CONFLICT);
\r
427 static void scsiReset()
\r
430 scsiDev.rstCount++;
\r
436 scsiDev.parityError = 0;
\r
437 scsiDev.phase = BUS_FREE;
\r
438 scsiDev.atnFlag = 0;
\r
439 scsiDev.resetFlag = 0;
\r
441 if (scsiDev.unitAttention != POWER_ON_RESET)
\r
443 scsiDev.unitAttention = SCSI_BUS_RESET;
\r
445 scsiDev.reservedId = -1;
\r
446 scsiDev.reserverId = -1;
\r
447 scsiDev.sense.code = NO_SENSE;
\r
448 scsiDev.sense.asc = NO_ADDITIONAL_SENSE_INFORMATION;
\r
451 scsiDev.postDataOutHook = NULL;
\r
453 // Sleep to allow the bus to settle down a bit.
\r
454 // We must be ready again within the "Reset to selection time" of
\r
456 // There is no guarantee that the RST line will be negated by then.
\r
457 // NOTE: We could be connected and powered by USB for configuration,
\r
458 // in which case TERMPWR cannot be supplied, and reset will ALWAYS
\r
460 CyDelay(10); // 10ms.
\r
463 static void enter_SelectionPhase()
\r
465 // Ignore stale versions of this flag, but ensure we know the
\r
466 // current value if the flag is still set.
\r
467 scsiDev.atnFlag = 0;
\r
468 scsiDev.parityError = 0;
\r
469 scsiDev.dataPtr = 0;
\r
470 scsiDev.savedDataPtr = 0;
\r
471 scsiDev.dataLen = 0;
\r
472 scsiDev.status = GOOD;
\r
473 scsiDev.phase = SELECTION;
\r
475 transfer.blocks = 0;
\r
476 transfer.currentBlock = 0;
\r
478 scsiDev.postDataOutHook = NULL;
\r
481 static void process_SelectionPhase()
\r
483 int sel = SCSI_ReadPin(SCSI_In_SEL);
\r
484 int bsy = SCSI_ReadPin(SCSI_In_BSY);
\r
486 // Only read these pins AFTER SEL and BSY - we don't want to catch them
\r
487 // during a transition period.
\r
488 uint8 mask = scsiReadDBxPins();
\r
489 int maskBitCount = countBits(mask);
\r
490 int goodParity = (Lookup_OddParity[mask] == SCSI_ReadPin(SCSI_In_DBP));
\r
493 (mask & scsiDev.scsiIdMask) &&
\r
494 (goodParity || !config->enableParity) && (maskBitCount <= 2))
\r
496 // Do we enter MESSAGE OUT immediately ? SCSI 1 and 2 standards says
\r
497 // move to MESSAGE OUT if ATN is true before we assert BSY.
\r
498 // The initiator should assert ATN with SEL.
\r
499 scsiDev.atnFlag = SCSI_ReadPin(SCSI_ATN_INT);
\r
501 // Unit attention breaks many older SCSI hosts. Disable it completely for
\r
502 // SCSI-1 (and older) hosts, regardless of our configured setting.
\r
503 if (!scsiDev.atnFlag)
\r
505 scsiDev.unitAttention = 0;
\r
508 // We've been selected!
\r
509 // Assert BSY - Selection success!
\r
510 // must happen within 200us (Selection abort time) of seeing our
\r
512 // (Note: the initiator will be waiting the "Selection time-out delay"
\r
513 // for our BSY response, which is actually a very generous 250ms)
\r
514 SCSI_SetPin(SCSI_Out_BSY);
\r
518 scsiDev.selCount++;
\r
521 // Wait until the end of the selection phase.
\r
522 while (!scsiDev.resetFlag)
\r
524 if (!SCSI_ReadPin(SCSI_In_SEL))
\r
530 // Save our initiator now that we're no longer in a time-critical
\r
532 // SCSI1/SASI initiators may not set their own ID.
\r
533 if (maskBitCount == 2)
\r
536 uint8 initiatorMask = mask ^ scsiDev.scsiIdMask;
\r
537 scsiDev.initiatorId = 0;
\r
538 for (i = 0; i < 8; ++i)
\r
540 if (initiatorMask & (1 << i))
\r
542 scsiDev.initiatorId = i;
\r
549 scsiDev.initiatorId = -1;
\r
552 scsiDev.phase = COMMAND;
\r
556 scsiDev.phase = BUS_BUSY;
\r
560 static void process_MessageOut()
\r
562 scsiEnterPhase(MESSAGE_OUT);
\r
564 scsiDev.atnFlag = 0;
\r
565 scsiDev.parityError = 0;
\r
566 scsiDev.msgOut = scsiReadByte();
\r
568 scsiDev.msgCount++;
\r
571 if (scsiDev.parityError)
\r
573 // Skip the remaining message bytes, and then start the MESSAGE_OUT
\r
574 // phase again from the start. The initiator will re-send the
\r
575 // same set of messages.
\r
576 while (SCSI_ReadPin(SCSI_ATN_INT) && !scsiDev.resetFlag)
\r
581 // Go-back and try the message again.
\r
582 scsiDev.atnFlag = 1;
\r
583 scsiDev.parityError = 0;
\r
585 else if (scsiDev.msgOut == 0x00)
\r
587 // COMMAND COMPLETE. but why would the target be receiving this ? nfi.
\r
590 else if (scsiDev.msgOut == 0x06)
\r
596 else if (scsiDev.msgOut == 0x0C)
\r
598 // BUS DEVICE RESET
\r
602 scsiDev.unitAttention = SCSI_BUS_RESET;
\r
604 // ANY initiator can reset the reservation state via this message.
\r
605 scsiDev.reservedId = -1;
\r
606 scsiDev.reserverId = -1;
\r
609 else if (scsiDev.msgOut == 0x05)
\r
611 // Initiate Detected Error
\r
614 else if (scsiDev.msgOut == 0x0F)
\r
616 // INITIATE RECOVERY
\r
619 else if (scsiDev.msgOut == 0x10)
\r
621 // RELEASE RECOVERY
\r
625 else if (scsiDev.msgOut == MSG_REJECT)
\r
629 scsiDev.resetFlag = 1;
\r
631 else if (scsiDev.msgOut == 0x08)
\r
635 else if (scsiDev.msgOut == 0x09)
\r
637 // Message Parity Error
\r
638 // Go back and re-send the last message.
\r
639 scsiDev.phase = MESSAGE_IN;
\r
641 else if (scsiDev.msgOut & 0x80) // 0x80 -> 0xFF
\r
644 // We don't disconnect, so ignore disconnect privilege.
\r
645 if ((scsiDev.msgOut & 0x18) || // Reserved bits set.
\r
646 (scsiDev.msgOut & 0x20) || // We don't have any target routines!
\r
647 (scsiDev.msgOut & 0x7) // We only support LUN 0!
\r
653 else if (scsiDev.msgOut >= 0x20 && scsiDev.msgOut <= 0x2F)
\r
655 // Two byte message. We don't support these. read and discard.
\r
658 else if (scsiDev.msgOut == 0x01)
\r
662 // Extended message.
\r
663 int msgLen = scsiReadByte();
\r
664 if (msgLen == 0) msgLen = 256;
\r
665 for (i = 0; i < msgLen && !scsiDev.resetFlag; ++i)
\r
671 // We don't support ANY extended messages.
\r
672 // Modify Data Pointer: We don't support reselection.
\r
673 // Wide Data Transfer Request: No. 8bit only.
\r
674 // Synchronous data transfer request. No, we can't do that.
\r
675 // We don't support any 2-byte messages either.
\r
676 // And we don't support any optional 1-byte messages.
\r
677 // In each case, the correct response is MESSAGE REJECT.
\r
685 // Re-check the ATN flag in case it stays asserted.
\r
686 scsiDev.atnFlag |= SCSI_ReadPin(SCSI_ATN_INT);
\r
689 void scsiPoll(void)
\r
691 if (scsiDev.resetFlag)
\r
694 if ((scsiDev.resetFlag = SCSI_ReadPin(SCSI_RST_INT)))
\r
696 // Still in reset phase. Do not try and process any commands.
\r
701 switch (scsiDev.phase)
\r
704 if (SCSI_ReadPin(SCSI_In_BSY))
\r
706 scsiDev.phase = BUS_BUSY;
\r
708 // The Arbitration phase is optional for SCSI1/SASI hosts if there is only
\r
709 // one initiator in the chain. Support this by moving
\r
710 // straight to selection if SEL is asserted.
\r
711 // ie. the initiator won't assert BSY and it's own ID before moving to selection.
\r
712 else if (SCSI_ReadPin(SCSI_In_SEL))
\r
714 enter_SelectionPhase();
\r
719 // Someone is using the bus. Perhaps they are trying to
\r
721 if (SCSI_ReadPin(SCSI_In_SEL))
\r
723 enter_SelectionPhase();
\r
725 else if (!SCSI_ReadPin(SCSI_In_BSY))
\r
727 scsiDev.phase = BUS_FREE;
\r
732 // TODO Support reselection.
\r
736 process_SelectionPhase();
\r
740 // Not currently supported!
\r
744 // Do not check ATN here. SCSI 1 & 2 initiators must set ATN
\r
745 // and SEL together upon entering the selection phase if they
\r
746 // want to send a message (IDENTIFY) immediately.
\r
747 if (scsiDev.atnFlag)
\r
749 process_MessageOut();
\r
758 scsiDev.atnFlag |= SCSI_ReadPin(SCSI_ATN_INT);
\r
759 if (scsiDev.atnFlag)
\r
761 process_MessageOut();
\r
770 scsiDev.atnFlag |= SCSI_ReadPin(SCSI_ATN_INT);
\r
771 if (scsiDev.atnFlag)
\r
773 process_MessageOut();
\r
782 scsiDev.atnFlag |= SCSI_ReadPin(SCSI_ATN_INT);
\r
783 if (scsiDev.atnFlag)
\r
785 process_MessageOut();
\r
794 scsiDev.atnFlag |= SCSI_ReadPin(SCSI_ATN_INT);
\r
795 if (scsiDev.atnFlag)
\r
797 process_MessageOut();
\r
801 process_MessageIn();
\r
807 process_MessageOut();
\r
814 scsiDev.scsiIdMask = 1 << (config->scsiId);
\r
816 scsiDev.atnFlag = 0;
\r
817 scsiDev.resetFlag = 1;
\r
818 scsiDev.phase = BUS_FREE;
\r
819 scsiDev.reservedId = -1;
\r
820 scsiDev.reserverId = -1;
\r
821 scsiDev.unitAttention = POWER_ON_RESET;
\r