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_ClearPin(SCSI_Out_MSG);
\r
58 SCSI_ClearPin(SCSI_Out_CD);
\r
59 SCSI_CTL_IO_Write(0);
\r
61 // Wait for the initiator to cease driving signals
\r
62 // Bus settle delay + bus clear delay = 1200ns
\r
66 scsiDev.phase = BUS_FREE;
\r
69 static void enter_MessageIn(uint8 message)
\r
71 scsiDev.msgIn = message;
\r
72 scsiDev.phase = MESSAGE_IN;
\r
75 static void process_MessageIn()
\r
77 scsiEnterPhase(MESSAGE_IN);
\r
78 scsiWriteByte(scsiDev.msgIn);
\r
80 if (scsiDev.atnFlag)
\r
82 // If there was a parity error, we go
\r
83 // back to MESSAGE_OUT first, get out parity error message, then come
\r
86 else if ((scsiDev.msgIn == MSG_LINKED_COMMAND_COMPLETE) ||
\r
87 (scsiDev.msgIn == MSG_LINKED_COMMAND_COMPLETE_WITH_FLAG))
\r
89 // Go back to the command phase and start again.
\r
90 scsiDev.phase = COMMAND;
\r
91 scsiDev.parityError = 0;
\r
92 scsiDev.dataPtr = 0;
\r
93 scsiDev.savedDataPtr = 0;
\r
94 scsiDev.dataLen = 0;
\r
95 scsiDev.status = GOOD;
\r
96 transfer.blocks = 0;
\r
97 transfer.currentBlock = 0;
\r
99 else /*if (scsiDev.msgIn == MSG_COMMAND_COMPLETE)*/
\r
105 static void messageReject()
\r
107 scsiEnterPhase(MESSAGE_IN);
\r
108 scsiWriteByte(MSG_REJECT);
\r
111 static void enter_Status(uint8 status)
\r
113 scsiDev.status = status;
\r
114 scsiDev.phase = STATUS;
\r
118 scsiDev.lastStatus = scsiDev.status;
\r
119 scsiDev.lastSense = scsiDev.sense.code;
\r
123 static void process_Status()
\r
125 scsiEnterPhase(STATUS);
\r
129 uint8 control = scsiDev.cdb[scsiDev.cdbLen - 1];
\r
130 if ((scsiDev.status == GOOD) && (control & 0x01))
\r
133 scsiDev.status = INTERMEDIATE;
\r
134 if (control & 0x02)
\r
136 message = MSG_LINKED_COMMAND_COMPLETE_WITH_FLAG;
\r
140 message = MSG_LINKED_COMMAND_COMPLETE;
\r
145 message = MSG_COMMAND_COMPLETE;
\r
147 scsiWriteByte(scsiDev.status);
\r
150 scsiDev.lastStatus = scsiDev.status;
\r
151 scsiDev.lastSense = scsiDev.sense.code;
\r
154 // Command Complete occurs AFTER a valid status has been
\r
155 // sent. then we go bus-free.
\r
156 enter_MessageIn(message);
\r
159 static void enter_DataIn(int len)
\r
161 scsiDev.dataLen = len;
\r
162 scsiDev.phase = DATA_IN;
\r
165 static void process_DataIn()
\r
169 if (scsiDev.dataLen > sizeof(scsiDev.data))
\r
171 scsiDev.dataLen = sizeof(scsiDev.data);
\r
174 len = scsiDev.dataLen - scsiDev.dataPtr;
\r
177 scsiEnterPhase(DATA_IN);
\r
178 scsiWrite(scsiDev.data + scsiDev.dataPtr, len);
\r
179 scsiDev.dataPtr += len;
\r
182 if ((scsiDev.dataPtr >= scsiDev.dataLen) &&
\r
183 (transfer.currentBlock == transfer.blocks))
\r
185 enter_Status(GOOD);
\r
189 static void process_DataOut()
\r
193 if (scsiDev.dataLen > sizeof(scsiDev.data))
\r
195 scsiDev.dataLen = sizeof(scsiDev.data);
\r
198 scsiDev.parityError = 0;
\r
199 len = scsiDev.dataLen - scsiDev.dataPtr;
\r
202 scsiEnterPhase(DATA_OUT);
\r
204 scsiRead(scsiDev.data + scsiDev.dataPtr, len);
\r
205 scsiDev.dataPtr += len;
\r
207 // TODO re-implement parity checking
\r
208 if (0 && scsiDev.parityError && config->enableParity)
\r
210 scsiDev.sense.code = ABORTED_COMMAND;
\r
211 scsiDev.sense.asc = SCSI_PARITY_ERROR;
\r
212 enter_Status(CHECK_CONDITION);
\r
216 if ((scsiDev.dataPtr >= scsiDev.dataLen) &&
\r
217 (transfer.currentBlock == transfer.blocks))
\r
219 if (scsiDev.postDataOutHook != NULL)
\r
221 scsiDev.postDataOutHook();
\r
225 enter_Status(GOOD);
\r
230 static const uint8 CmdGroupBytes[8] = {6, 10, 10, 6, 6, 12, 6, 6};
\r
231 static void process_Command()
\r
238 scsiEnterPhase(COMMAND);
\r
239 scsiDev.parityError = 0;
\r
241 memset(scsiDev.cdb, 0, sizeof(scsiDev.cdb));
\r
242 scsiDev.cdb[0] = scsiReadByte();
\r
244 group = scsiDev.cdb[0] >> 5;
\r
245 scsiDev.cdbLen = CmdGroupBytes[group];
\r
246 scsiRead(scsiDev.cdb + 1, scsiDev.cdbLen - 1);
\r
248 command = scsiDev.cdb[0];
\r
249 lun = scsiDev.cdb[1] >> 5;
\r
250 control = scsiDev.cdb[scsiDev.cdbLen - 1];
\r
253 scsiDev.cmdCount++;
\r
256 if (scsiDev.resetFlag)
\r
259 // Don't log bogus commands
\r
260 scsiDev.cmdCount--;
\r
261 memset(scsiDev.cdb, 0xff, sizeof(scsiDev.cdb));
\r
265 else if (scsiDev.parityError)
\r
267 scsiDev.sense.code = ABORTED_COMMAND;
\r
268 scsiDev.sense.asc = SCSI_PARITY_ERROR;
\r
269 enter_Status(CHECK_CONDITION);
\r
271 else if ((control & 0x02) && ((control & 0x01) == 0))
\r
273 // FLAG set without LINK flag.
\r
274 scsiDev.sense.code = ILLEGAL_REQUEST;
\r
275 scsiDev.sense.asc = INVALID_FIELD_IN_CDB;
\r
276 enter_Status(CHECK_CONDITION);
\r
278 else if (command == 0x12)
\r
282 else if (command == 0x03)
\r
285 uint32 allocLength = scsiDev.cdb[4];
\r
286 if (allocLength == 0) allocLength = 256;
\r
287 memset(scsiDev.data, 0, 18);
\r
288 scsiDev.data[0] = 0xF0;
\r
289 scsiDev.data[2] = scsiDev.sense.code & 0x0F;
\r
291 scsiDev.data[3] = transfer.lba >> 24;
\r
292 scsiDev.data[4] = transfer.lba >> 16;
\r
293 scsiDev.data[5] = transfer.lba >> 8;
\r
294 scsiDev.data[6] = transfer.lba;
\r
296 // Additional bytes if there are errors to report
\r
297 int responseLength;
\r
298 if (scsiDev.sense.code == NO_SENSE)
\r
300 responseLength = 8;
\r
304 responseLength = 18;
\r
305 scsiDev.data[7] = 10; // additional length
\r
306 scsiDev.data[12] = scsiDev.sense.asc >> 8;
\r
307 scsiDev.data[13] = scsiDev.sense.asc;
\r
310 // Silently truncate results. SCSI-2 spec 8.2.14.
\r
312 (allocLength < responseLength) ? allocLength : responseLength
\r
315 // This is a good time to clear out old sense information.
\r
316 scsiDev.sense.code = NO_SENSE;
\r
317 scsiDev.sense.asc = NO_ADDITIONAL_SENSE_INFORMATION;
\r
319 // Some old SCSI drivers do NOT properly support
\r
320 // unitAttention. eg. the Mac Plus would trigger a SCSI reset
\r
321 // on receiving the unit attention response on boot, thus
\r
322 // triggering another unit attention condition.
\r
323 else if (scsiDev.unitAttention && config->enableUnitAttention)
\r
325 scsiDev.sense.code = UNIT_ATTENTION;
\r
326 scsiDev.sense.asc = scsiDev.unitAttention;
\r
328 // If initiator doesn't do REQUEST SENSE for the next command, then
\r
330 scsiDev.unitAttention = 0;
\r
332 enter_Status(CHECK_CONDITION);
\r
336 scsiDev.sense.code = ILLEGAL_REQUEST;
\r
337 scsiDev.sense.asc = LOGICAL_UNIT_NOT_SUPPORTED;
\r
338 enter_Status(CHECK_CONDITION);
\r
340 else if (command == 0x17 || command == 0x16)
\r
342 doReserveRelease();
\r
344 else if ((scsiDev.reservedId >= 0) &&
\r
345 (scsiDev.reservedId != scsiDev.initiatorId))
\r
347 enter_Status(CONFLICT);
\r
349 else if (command == 0x1C)
\r
351 scsiReceiveDiagnostic();
\r
353 else if (command == 0x1D)
\r
355 scsiSendDiagnostic();
\r
358 !scsiModeCommand() &&
\r
359 !scsiDiskCommand())
\r
361 scsiDev.sense.code = ILLEGAL_REQUEST;
\r
362 scsiDev.sense.asc = INVALID_COMMAND_OPERATION_CODE;
\r
363 enter_Status(CHECK_CONDITION);
\r
367 if (scsiDev.phase == COMMAND) // No status set, and not in DATA_IN
\r
369 enter_Status(GOOD);
\r
374 static void doReserveRelease()
\r
376 int extentReservation = scsiDev.cdb[1] & 1;
\r
377 int thirdPty = scsiDev.cdb[1] & 0x10;
\r
378 int thirdPtyId = (scsiDev.cdb[1] >> 1) & 0x7;
\r
379 uint8 command = scsiDev.cdb[0];
\r
382 (!thirdPty && (scsiDev.initiatorId == scsiDev.reservedId)) ||
\r
384 (scsiDev.reserverId == scsiDev.initiatorId) &&
\r
385 (scsiDev.reservedId == thirdPtyId)
\r
388 if (extentReservation)
\r
391 scsiDev.sense.code = ILLEGAL_REQUEST;
\r
392 scsiDev.sense.asc = INVALID_FIELD_IN_CDB;
\r
393 enter_Status(CHECK_CONDITION);
\r
395 else if (command == 0x17) // release
\r
397 if ((scsiDev.reservedId < 0) || canRelease)
\r
399 scsiDev.reservedId = -1;
\r
400 scsiDev.reserverId = -1;
\r
404 enter_Status(CONFLICT);
\r
407 else // assume reserve.
\r
409 if ((scsiDev.reservedId < 0) || canRelease)
\r
411 scsiDev.reserverId = scsiDev.initiatorId;
\r
414 scsiDev.reservedId = thirdPtyId;
\r
418 scsiDev.reservedId = scsiDev.initiatorId;
\r
423 // Already reserved by someone else!
\r
424 enter_Status(CONFLICT);
\r
429 static void scsiReset()
\r
432 scsiDev.rstCount++;
\r
438 scsiDev.parityError = 0;
\r
439 scsiDev.phase = BUS_FREE;
\r
440 scsiDev.atnFlag = 0;
\r
441 scsiDev.resetFlag = 0;
\r
443 if (scsiDev.unitAttention != POWER_ON_RESET)
\r
445 scsiDev.unitAttention = SCSI_BUS_RESET;
\r
447 scsiDev.reservedId = -1;
\r
448 scsiDev.reserverId = -1;
\r
449 scsiDev.sense.code = NO_SENSE;
\r
450 scsiDev.sense.asc = NO_ADDITIONAL_SENSE_INFORMATION;
\r
453 scsiDev.postDataOutHook = NULL;
\r
455 // Sleep to allow the bus to settle down a bit.
\r
456 // We must be ready again within the "Reset to selection time" of
\r
458 // There is no guarantee that the RST line will be negated by then.
\r
459 // NOTE: We could be connected and powered by USB for configuration,
\r
460 // in which case TERMPWR cannot be supplied, and reset will ALWAYS
\r
462 CyDelay(10); // 10ms.
\r
465 static void enter_SelectionPhase()
\r
467 // Ignore stale versions of this flag, but ensure we know the
\r
468 // current value if the flag is still set.
\r
469 scsiDev.atnFlag = 0;
\r
470 scsiDev.parityError = 0;
\r
471 scsiDev.dataPtr = 0;
\r
472 scsiDev.savedDataPtr = 0;
\r
473 scsiDev.dataLen = 0;
\r
474 scsiDev.status = GOOD;
\r
475 scsiDev.phase = SELECTION;
\r
477 transfer.blocks = 0;
\r
478 transfer.currentBlock = 0;
\r
480 scsiDev.postDataOutHook = NULL;
\r
483 static void process_SelectionPhase()
\r
485 int sel = SCSI_ReadPin(SCSI_In_SEL);
\r
486 int bsy = SCSI_ReadPin(SCSI_In_BSY);
\r
488 // Only read these pins AFTER SEL and BSY - we don't want to catch them
\r
489 // during a transition period.
\r
490 uint8 mask = scsiReadDBxPins();
\r
491 int maskBitCount = countBits(mask);
\r
492 int goodParity = (Lookup_OddParity[mask] == SCSI_ReadPin(SCSI_In_DBP));
\r
495 (mask & scsiDev.scsiIdMask) &&
\r
496 (goodParity || !config->enableParity) && (maskBitCount <= 2))
\r
498 // Do we enter MESSAGE OUT immediately ? SCSI 1 and 2 standards says
\r
499 // move to MESSAGE OUT if ATN is true before we assert BSY.
\r
500 // The initiator should assert ATN with SEL.
\r
501 scsiDev.atnFlag = SCSI_ReadPin(SCSI_ATN_INT);
\r
503 // Unit attention breaks many older SCSI hosts. Disable it completely for
\r
504 // SCSI-1 (and older) hosts, regardless of our configured setting.
\r
505 if (!scsiDev.atnFlag)
\r
507 scsiDev.unitAttention = 0;
\r
510 // We've been selected!
\r
511 // Assert BSY - Selection success!
\r
512 // must happen within 200us (Selection abort time) of seeing our
\r
514 // (Note: the initiator will be waiting the "Selection time-out delay"
\r
515 // for our BSY response, which is actually a very generous 250ms)
\r
516 SCSI_SetPin(SCSI_Out_BSY);
\r
520 scsiDev.selCount++;
\r
523 // Wait until the end of the selection phase.
\r
524 while (!scsiDev.resetFlag)
\r
526 if (!SCSI_ReadPin(SCSI_In_SEL))
\r
532 // Save our initiator now that we're no longer in a time-critical
\r
534 // SCSI1/SASI initiators may not set their own ID.
\r
535 if (maskBitCount == 2)
\r
538 uint8 initiatorMask = mask ^ scsiDev.scsiIdMask;
\r
539 scsiDev.initiatorId = 0;
\r
540 for (i = 0; i < 8; ++i)
\r
542 if (initiatorMask & (1 << i))
\r
544 scsiDev.initiatorId = i;
\r
551 scsiDev.initiatorId = -1;
\r
554 scsiDev.phase = COMMAND;
\r
558 scsiDev.phase = BUS_BUSY;
\r
562 static void process_MessageOut()
\r
564 scsiEnterPhase(MESSAGE_OUT);
\r
566 scsiDev.atnFlag = 0;
\r
567 scsiDev.parityError = 0;
\r
568 scsiDev.msgOut = scsiReadByte();
\r
570 scsiDev.msgCount++;
\r
573 if (scsiDev.parityError)
\r
575 // Skip the remaining message bytes, and then start the MESSAGE_OUT
\r
576 // phase again from the start. The initiator will re-send the
\r
577 // same set of messages.
\r
578 while (SCSI_ReadPin(SCSI_ATN_INT) && !scsiDev.resetFlag)
\r
583 // Go-back and try the message again.
\r
584 scsiDev.atnFlag = 1;
\r
585 scsiDev.parityError = 0;
\r
587 else if (scsiDev.msgOut == 0x00)
\r
589 // COMMAND COMPLETE. but why would the target be receiving this ? nfi.
\r
592 else if (scsiDev.msgOut == 0x06)
\r
598 else if (scsiDev.msgOut == 0x0C)
\r
600 // BUS DEVICE RESET
\r
604 scsiDev.unitAttention = SCSI_BUS_RESET;
\r
606 // ANY initiator can reset the reservation state via this message.
\r
607 scsiDev.reservedId = -1;
\r
608 scsiDev.reserverId = -1;
\r
611 else if (scsiDev.msgOut == 0x05)
\r
613 // Initiate Detected Error
\r
616 else if (scsiDev.msgOut == 0x0F)
\r
618 // INITIATE RECOVERY
\r
621 else if (scsiDev.msgOut == 0x10)
\r
623 // RELEASE RECOVERY
\r
627 else if (scsiDev.msgOut == MSG_REJECT)
\r
631 scsiDev.resetFlag = 1;
\r
633 else if (scsiDev.msgOut == 0x08)
\r
637 else if (scsiDev.msgOut == 0x09)
\r
639 // Message Parity Error
\r
640 // Go back and re-send the last message.
\r
641 scsiDev.phase = MESSAGE_IN;
\r
643 else if (scsiDev.msgOut & 0x80) // 0x80 -> 0xFF
\r
646 // We don't disconnect, so ignore disconnect privilege.
\r
647 if ((scsiDev.msgOut & 0x18) || // Reserved bits set.
\r
648 (scsiDev.msgOut & 0x20) || // We don't have any target routines!
\r
649 (scsiDev.msgOut & 0x7) // We only support LUN 0!
\r
655 else if (scsiDev.msgOut >= 0x20 && scsiDev.msgOut <= 0x2F)
\r
657 // Two byte message. We don't support these. read and discard.
\r
660 else if (scsiDev.msgOut == 0x01)
\r
664 // Extended message.
\r
665 int msgLen = scsiReadByte();
\r
666 if (msgLen == 0) msgLen = 256;
\r
667 for (i = 0; i < msgLen && !scsiDev.resetFlag; ++i)
\r
673 // We don't support ANY extended messages.
\r
674 // Modify Data Pointer: We don't support reselection.
\r
675 // Wide Data Transfer Request: No. 8bit only.
\r
676 // Synchronous data transfer request. No, we can't do that.
\r
677 // We don't support any 2-byte messages either.
\r
678 // And we don't support any optional 1-byte messages.
\r
679 // In each case, the correct response is MESSAGE REJECT.
\r
687 // Re-check the ATN flag in case it stays asserted.
\r
688 scsiDev.atnFlag |= SCSI_ReadPin(SCSI_ATN_INT);
\r
691 void scsiPoll(void)
\r
693 if (scsiDev.resetFlag)
\r
696 if ((scsiDev.resetFlag = SCSI_ReadPin(SCSI_RST_INT)))
\r
698 // Still in reset phase. Do not try and process any commands.
\r
703 switch (scsiDev.phase)
\r
706 if (SCSI_ReadPin(SCSI_In_BSY))
\r
708 scsiDev.phase = BUS_BUSY;
\r
710 // The Arbitration phase is optional for SCSI1/SASI hosts if there is only
\r
711 // one initiator in the chain. Support this by moving
\r
712 // straight to selection if SEL is asserted.
\r
713 // ie. the initiator won't assert BSY and it's own ID before moving to selection.
\r
714 else if (SCSI_ReadPin(SCSI_In_SEL))
\r
716 enter_SelectionPhase();
\r
721 // Someone is using the bus. Perhaps they are trying to
\r
723 if (SCSI_ReadPin(SCSI_In_SEL))
\r
725 enter_SelectionPhase();
\r
727 else if (!SCSI_ReadPin(SCSI_In_BSY))
\r
729 scsiDev.phase = BUS_FREE;
\r
734 // TODO Support reselection.
\r
738 process_SelectionPhase();
\r
742 // Not currently supported!
\r
746 // Do not check ATN here. SCSI 1 & 2 initiators must set ATN
\r
747 // and SEL together upon entering the selection phase if they
\r
748 // want to send a message (IDENTIFY) immediately.
\r
749 if (scsiDev.atnFlag)
\r
751 process_MessageOut();
\r
760 scsiDev.atnFlag |= SCSI_ReadPin(SCSI_ATN_INT);
\r
761 if (scsiDev.atnFlag)
\r
763 process_MessageOut();
\r
772 scsiDev.atnFlag |= SCSI_ReadPin(SCSI_ATN_INT);
\r
773 if (scsiDev.atnFlag)
\r
775 process_MessageOut();
\r
784 scsiDev.atnFlag |= SCSI_ReadPin(SCSI_ATN_INT);
\r
785 if (scsiDev.atnFlag)
\r
787 process_MessageOut();
\r
796 scsiDev.atnFlag |= SCSI_ReadPin(SCSI_ATN_INT);
\r
797 if (scsiDev.atnFlag)
\r
799 process_MessageOut();
\r
803 process_MessageIn();
\r
809 process_MessageOut();
\r
816 scsiDev.scsiIdMask = 1 << (config->scsiId);
\r
818 scsiDev.atnFlag = 0;
\r
819 scsiDev.resetFlag = 1;
\r
820 scsiDev.phase = BUS_FREE;
\r
821 scsiDev.reservedId = -1;
\r
822 scsiDev.reserverId = -1;
\r
823 scsiDev.unitAttention = POWER_ON_RESET;
\r