1 // Copyright (C) 2013 Michael McMaster <michael@codesrc.com>
\r
2 // Copyright (C) 2014 Doug Brown <doug@downtowndougbrown.com>
\r
4 // This file is part of SCSI2SD.
\r
6 // SCSI2SD is free software: you can redistribute it and/or modify
\r
7 // it under the terms of the GNU General Public License as published by
\r
8 // the Free Software Foundation, either version 3 of the License, or
\r
9 // (at your option) any later version.
\r
11 // SCSI2SD is distributed in the hope that it will be useful,
\r
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 // GNU General Public License for more details.
\r
16 // You should have received a copy of the GNU General Public License
\r
17 // along with SCSI2SD. If not, see <http://www.gnu.org/licenses/>.
\r
20 #include "scsiPhy.h"
\r
30 BlockDevice blockDev;
\r
33 static int doSdInit()
\r
36 if (blockDev.state & DISK_PRESENT)
\r
42 blockDev.state = blockDev.state | DISK_INITIALISED;
\r
48 // Callback once all data has been read in the data out phase.
\r
49 static void doFormatUnitComplete(void)
\r
51 // TODO start writing the initialisation pattern to the SD
\r
53 scsiDev.phase = STATUS;
\r
56 static void doFormatUnitSkipData(int bytes)
\r
58 // We may not have enough memory to store the initialisation pattern and
\r
59 // defect list data. Since we're not making use of it yet anyway, just
\r
60 // discard the bytes.
\r
61 scsiEnterPhase(DATA_OUT);
\r
63 for (i = 0; i < bytes; ++i)
\r
69 // Callback from the data out phase.
\r
70 static void doFormatUnitPatternHeader(void)
\r
73 ((((uint16_t)scsiDev.data[2])) << 8) +
\r
77 ((((uint16_t)scsiDev.data[4 + 2])) << 8) +
\r
78 scsiDev.data[4 + 3];
\r
80 doFormatUnitSkipData(defectLength + patternLength);
\r
81 doFormatUnitComplete();
\r
84 // Callback from the data out phase.
\r
85 static void doFormatUnitHeader(void)
\r
87 int IP = (scsiDev.data[1] & 0x08) ? 1 : 0;
\r
88 int DSP = (scsiDev.data[1] & 0x04) ? 1 : 0;
\r
90 if (! DSP) // disable save parameters
\r
92 // Save the "MODE SELECT savable parameters"
\r
94 scsiDev.target->targetId,
\r
95 scsiDev.target->liveCfg.bytesPerSector);
\r
100 // We need to read the initialisation pattern header first.
\r
101 scsiDev.dataLen += 4;
\r
102 scsiDev.phase = DATA_OUT;
\r
103 scsiDev.postDataOutHook = doFormatUnitPatternHeader;
\r
107 // Read the defect list data
\r
109 ((((uint16_t)scsiDev.data[2])) << 8) +
\r
111 doFormatUnitSkipData(defectLength);
\r
112 doFormatUnitComplete();
\r
116 static void doReadCapacity()
\r
118 uint32_t lba = (((uint32_t) scsiDev.cdb[2]) << 24) +
\r
119 (((uint32_t) scsiDev.cdb[3]) << 16) +
\r
120 (((uint32_t) scsiDev.cdb[4]) << 8) +
\r
122 int pmi = scsiDev.cdb[8] & 1;
\r
124 uint32_t capacity = getScsiCapacity(
\r
125 scsiDev.target->cfg->sdSectorStart,
\r
126 scsiDev.target->liveCfg.bytesPerSector,
\r
127 scsiDev.target->cfg->scsiSectors);
\r
132 // We don't do anything with the "partial medium indicator", and
\r
133 // assume that delays are constant across each block. But the spec
\r
134 // says we must return this error if pmi is specified incorrectly.
\r
135 scsiDev.status = CHECK_CONDITION;
\r
136 scsiDev.target->sense.code = ILLEGAL_REQUEST;
\r
137 scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;
\r
138 scsiDev.phase = STATUS;
\r
140 else if (capacity > 0)
\r
142 uint32_t highestBlock = capacity - 1;
\r
144 scsiDev.data[0] = highestBlock >> 24;
\r
145 scsiDev.data[1] = highestBlock >> 16;
\r
146 scsiDev.data[2] = highestBlock >> 8;
\r
147 scsiDev.data[3] = highestBlock;
\r
149 uint32_t bytesPerSector = scsiDev.target->liveCfg.bytesPerSector;
\r
150 scsiDev.data[4] = bytesPerSector >> 24;
\r
151 scsiDev.data[5] = bytesPerSector >> 16;
\r
152 scsiDev.data[6] = bytesPerSector >> 8;
\r
153 scsiDev.data[7] = bytesPerSector;
\r
154 scsiDev.dataLen = 8;
\r
155 scsiDev.phase = DATA_IN;
\r
159 scsiDev.status = CHECK_CONDITION;
\r
160 scsiDev.target->sense.code = NOT_READY;
\r
161 scsiDev.target->sense.asc = MEDIUM_NOT_PRESENT;
\r
162 scsiDev.phase = STATUS;
\r
166 static void doWrite(uint32_t lba, uint32_t blocks)
\r
168 if (unlikely(scsiDev.target->cfg->deviceType == S2S_CFG_FLOPPY_14MB)) {
\r
169 // Floppies are supposed to be slow. Some systems can't handle a floppy
\r
170 // without an access time
\r
174 uint32_t bytesPerSector = scsiDev.target->liveCfg.bytesPerSector;
\r
176 if (unlikely(blockDev.state & DISK_WP) ||
\r
177 unlikely(scsiDev.target->cfg->deviceType == S2S_CFG_OPTICAL))
\r
180 scsiDev.status = CHECK_CONDITION;
\r
181 scsiDev.target->sense.code = ILLEGAL_REQUEST;
\r
182 scsiDev.target->sense.asc = WRITE_PROTECTED;
\r
183 scsiDev.phase = STATUS;
\r
185 else if (unlikely(((uint64_t) lba) + blocks >
\r
187 scsiDev.target->cfg->sdSectorStart,
\r
189 scsiDev.target->cfg->scsiSectors
\r
193 scsiDev.status = CHECK_CONDITION;
\r
194 scsiDev.target->sense.code = ILLEGAL_REQUEST;
\r
195 scsiDev.target->sense.asc = LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
\r
196 scsiDev.phase = STATUS;
\r
200 transfer.lba = lba;
\r
201 transfer.blocks = blocks;
\r
202 transfer.currentBlock = 0;
\r
203 scsiDev.phase = DATA_OUT;
\r
204 scsiDev.dataLen = bytesPerSector;
\r
205 scsiDev.dataPtr = bytesPerSector;
\r
207 // No need for single-block writes atm. Overhead of the
\r
208 // multi-block write is minimal.
\r
209 transfer.multiBlock = 1;
\r
212 // TODO uint32_t sdLBA =
\r
213 // TODO SCSISector2SD(
\r
214 // TODO scsiDev.target->cfg->sdSectorStart,
\r
215 // TODO bytesPerSector,
\r
217 // TODO uint32_t sdBlocks = blocks * SDSectorsPerSCSISector(bytesPerSector);
\r
218 // TODO sdWriteMultiSectorPrep(sdLBA, sdBlocks);
\r
223 static void doRead(uint32_t lba, uint32_t blocks)
\r
225 if (unlikely(scsiDev.target->cfg->deviceType == S2S_CFG_FLOPPY_14MB)) {
\r
226 // Floppies are supposed to be slow. Some systems can't handle a floppy
\r
227 // without an access time
\r
231 uint32_t capacity = getScsiCapacity(
\r
232 scsiDev.target->cfg->sdSectorStart,
\r
233 scsiDev.target->liveCfg.bytesPerSector,
\r
234 scsiDev.target->cfg->scsiSectors);
\r
235 if (unlikely(((uint64_t) lba) + blocks > capacity))
\r
237 scsiDev.status = CHECK_CONDITION;
\r
238 scsiDev.target->sense.code = ILLEGAL_REQUEST;
\r
239 scsiDev.target->sense.asc = LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
\r
240 scsiDev.phase = STATUS;
\r
244 transfer.lba = lba;
\r
245 transfer.blocks = blocks;
\r
246 transfer.currentBlock = 0;
\r
247 scsiDev.phase = DATA_IN;
\r
248 scsiDev.dataLen = 0; // No data yet
\r
250 uint32_t bytesPerSector = scsiDev.target->liveCfg.bytesPerSector;
\r
251 uint32_t sdSectorPerSCSISector = SDSectorsPerSCSISector(bytesPerSector);
\r
252 uint32_t sdSectors =
\r
253 blocks * sdSectorPerSCSISector;
\r
256 (sdSectors == 1) &&
\r
257 !(scsiDev.boardCfg.flags & S2S_CFG_ENABLE_CACHE)
\r
259 unlikely(((uint64_t) lba) + blocks == capacity)
\r
262 // We get errors on reading the last sector using a multi-sector
\r
264 transfer.multiBlock = 0;
\r
268 transfer.multiBlock = 1;
\r
270 // uint32_t sdLBA =
\r
272 // scsiDev.target->cfg->sdSectorStart,
\r
276 // TODO sdReadMultiSectorPrep(sdLBA, sdSectors);
\r
281 static void doSeek(uint32_t lba)
\r
285 scsiDev.target->cfg->sdSectorStart,
\r
286 scsiDev.target->liveCfg.bytesPerSector,
\r
287 scsiDev.target->cfg->scsiSectors)
\r
290 scsiDev.status = CHECK_CONDITION;
\r
291 scsiDev.target->sense.code = ILLEGAL_REQUEST;
\r
292 scsiDev.target->sense.asc = LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
\r
293 scsiDev.phase = STATUS;
\r
297 static int doTestUnitReady()
\r
300 if (likely(blockDev.state == (DISK_STARTED | DISK_PRESENT | DISK_INITIALISED)))
\r
304 else if (unlikely(!(blockDev.state & DISK_STARTED)))
\r
307 scsiDev.status = CHECK_CONDITION;
\r
308 scsiDev.target->sense.code = NOT_READY;
\r
309 scsiDev.target->sense.asc = LOGICAL_UNIT_NOT_READY_INITIALIZING_COMMAND_REQUIRED;
\r
310 scsiDev.phase = STATUS;
\r
312 else if (unlikely(!(blockDev.state & DISK_PRESENT)))
\r
315 scsiDev.status = CHECK_CONDITION;
\r
316 scsiDev.target->sense.code = NOT_READY;
\r
317 scsiDev.target->sense.asc = MEDIUM_NOT_PRESENT;
\r
318 scsiDev.phase = STATUS;
\r
320 else if (unlikely(!(blockDev.state & DISK_INITIALISED)))
\r
323 scsiDev.status = CHECK_CONDITION;
\r
324 scsiDev.target->sense.code = NOT_READY;
\r
325 scsiDev.target->sense.asc = LOGICAL_UNIT_NOT_READY_CAUSE_NOT_REPORTABLE;
\r
326 scsiDev.phase = STATUS;
\r
331 // Handle direct-access scsi device commands
\r
332 int scsiDiskCommand()
\r
334 int commandHandled = 1;
\r
336 uint8_t command = scsiDev.cdb[0];
\r
337 if (unlikely(command == 0x1B))
\r
340 // Enable or disable media access operations.
\r
341 // Ignore load/eject requests. We can't do that.
\r
342 //int immed = scsiDev.cdb[1] & 1;
\r
343 int start = scsiDev.cdb[4] & 1;
\r
347 blockDev.state = blockDev.state | DISK_STARTED;
\r
348 if (!(blockDev.state & DISK_INITIALISED))
\r
355 blockDev.state &= ~DISK_STARTED;
\r
358 else if (unlikely(command == 0x00))
\r
363 else if (unlikely(!doTestUnitReady()))
\r
365 // Status and sense codes already set by doTestUnitReady
\r
367 else if (likely(command == 0x08))
\r
371 (((uint32_t) scsiDev.cdb[1] & 0x1F) << 16) +
\r
372 (((uint32_t) scsiDev.cdb[2]) << 8) +
\r
374 uint32_t blocks = scsiDev.cdb[4];
\r
375 if (unlikely(blocks == 0)) blocks = 256;
\r
376 doRead(lba, blocks);
\r
378 else if (likely(command == 0x28))
\r
381 // Ignore all cache control bits - we don't support a memory cache.
\r
384 (((uint32_t) scsiDev.cdb[2]) << 24) +
\r
385 (((uint32_t) scsiDev.cdb[3]) << 16) +
\r
386 (((uint32_t) scsiDev.cdb[4]) << 8) +
\r
389 (((uint32_t) scsiDev.cdb[7]) << 8) +
\r
392 doRead(lba, blocks);
\r
394 else if (likely(command == 0x0A))
\r
398 (((uint32_t) scsiDev.cdb[1] & 0x1F) << 16) +
\r
399 (((uint32_t) scsiDev.cdb[2]) << 8) +
\r
401 uint32_t blocks = scsiDev.cdb[4];
\r
402 if (unlikely(blocks == 0)) blocks = 256;
\r
403 doWrite(lba, blocks);
\r
405 else if (likely(command == 0x2A) || // WRITE(10)
\r
406 unlikely(command == 0x2E)) // WRITE AND VERIFY
\r
408 // Ignore all cache control bits - we don't support a memory cache.
\r
409 // Don't bother verifying either. The SD card likely stores ECC
\r
410 // along with each flash row.
\r
413 (((uint32_t) scsiDev.cdb[2]) << 24) +
\r
414 (((uint32_t) scsiDev.cdb[3]) << 16) +
\r
415 (((uint32_t) scsiDev.cdb[4]) << 8) +
\r
418 (((uint32_t) scsiDev.cdb[7]) << 8) +
\r
421 doWrite(lba, blocks);
\r
423 else if (unlikely(command == 0x04))
\r
426 // We don't really do any formatting, but we need to read the correct
\r
427 // number of bytes in the DATA_OUT phase to make the SCSI host happy.
\r
429 int fmtData = (scsiDev.cdb[1] & 0x10) ? 1 : 0;
\r
432 // We need to read the parameter list, but we don't know how
\r
433 // big it is yet. Start with the header.
\r
434 scsiDev.dataLen = 4;
\r
435 scsiDev.phase = DATA_OUT;
\r
436 scsiDev.postDataOutHook = doFormatUnitHeader;
\r
440 // No data to read, we're already finished!
\r
443 else if (unlikely(command == 0x25))
\r
448 else if (unlikely(command == 0x0B))
\r
452 (((uint32_t) scsiDev.cdb[1] & 0x1F) << 16) +
\r
453 (((uint32_t) scsiDev.cdb[2]) << 8) +
\r
459 else if (unlikely(command == 0x2B))
\r
463 (((uint32_t) scsiDev.cdb[2]) << 24) +
\r
464 (((uint32_t) scsiDev.cdb[3]) << 16) +
\r
465 (((uint32_t) scsiDev.cdb[4]) << 8) +
\r
470 else if (unlikely(command == 0x36))
\r
472 // LOCK UNLOCK CACHE
\r
473 // We don't have a cache to lock data into. do nothing.
\r
475 else if (unlikely(command == 0x34))
\r
478 // We don't have a cache to pre-fetch into. do nothing.
\r
480 else if (unlikely(command == 0x1E))
\r
482 // PREVENT ALLOW MEDIUM REMOVAL
\r
483 // Not much we can do to prevent the user removing the SD card.
\r
486 else if (unlikely(command == 0x01))
\r
489 // Set the lun to a vendor-specific state. Ignore.
\r
491 else if (unlikely(command == 0x35))
\r
493 // SYNCHRONIZE CACHE
\r
494 // We don't have a cache. do nothing.
\r
496 else if (unlikely(command == 0x2F))
\r
499 // TODO: When they supply data to verify, we should read the data and
\r
500 // verify it. If they don't supply any data, just say success.
\r
501 if ((scsiDev.cdb[1] & 0x02) == 0)
\r
503 // They are asking us to do a medium verification with no data
\r
504 // comparison. Assume success, do nothing.
\r
508 // TODO. This means they are supplying data to verify against.
\r
509 // Technically we should probably grab the data and compare it.
\r
510 scsiDev.status = CHECK_CONDITION;
\r
511 scsiDev.target->sense.code = ILLEGAL_REQUEST;
\r
512 scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;
\r
513 scsiDev.phase = STATUS;
\r
516 else if (unlikely(command == 0x37))
\r
518 // READ DEFECT DATA
\r
519 scsiDev.status = CHECK_CONDITION;
\r
520 scsiDev.target->sense.code = NO_SENSE;
\r
521 scsiDev.target->sense.asc = DEFECT_LIST_NOT_FOUND;
\r
522 scsiDev.phase = STATUS;
\r
527 commandHandled = 0;
\r
530 return commandHandled;
\r
533 void scsiDiskPoll()
\r
535 uint32_t bytesPerSector = scsiDev.target->liveCfg.bytesPerSector;
\r
537 if (scsiDev.phase == DATA_IN &&
\r
538 transfer.currentBlock != transfer.blocks)
\r
540 scsiEnterPhase(DATA_IN);
\r
542 int totalSDSectors =
\r
543 transfer.blocks * SDSectorsPerSCSISector(bytesPerSector);
\r
546 scsiDev.target->cfg->sdSectorStart,
\r
550 const int sdPerScsi = SDSectorsPerSCSISector(bytesPerSector);
\r
551 int buffers = sizeof(scsiDev.data) / SD_SECTOR_SIZE;
\r
554 int scsiActive = 0;
\r
555 // int sdActive = 0;
\r
556 while ((i < totalSDSectors) &&
\r
557 likely(scsiDev.phase == DATA_IN) &&
\r
558 likely(!scsiDev.resetFlag))
\r
560 // Wait for the next DMA interrupt. It's beneficial to halt the
\r
561 // processor to give the DMA controller more memory bandwidth to
\r
566 while (scsiBusy && sdBusy)
\r
568 uint8_t intr = CyEnterCriticalSection();
\r
569 scsiBusy = scsiDMABusy();
\r
570 sdBusy = sdDMABusy();
\r
571 if (scsiBusy && sdBusy)
\r
575 CyExitCriticalSection(intr);
\r
580 if (sdActive && !sdBusy && sdReadSectorDMAPoll())
\r
586 // Usually SD is slower than the SCSI interface.
\r
587 // Prioritise starting the read of the next sector over starting a
\r
588 // SCSI transfer for the last sector
\r
589 // ie. NO "else" HERE.
\r
591 (prep - i < buffers) &&
\r
592 (prep < totalSDSectors))
\r
594 // Start an SD transfer if we have space.
\r
595 if (transfer.multiBlock)
\r
597 sdReadMultiSectorDMA(&scsiDev.data[SD_SECTOR_SIZE * (prep % buffers)]);
\r
601 sdReadSingleSectorDMA(sdLBA + prep, &scsiDev.data[SD_SECTOR_SIZE * (prep % buffers)]);
\r
606 if ((prep - i < buffers) &&
\r
607 (prep < totalSDSectors))
\r
609 // TODO MM Blocking reads are bad mmkay
\r
610 sdTmpRead(&scsiDev.data[SD_SECTOR_SIZE * (prep % buffers)], sdLBA + prep, 1);
\r
614 if (scsiActive && scsiPhyComplete() && scsiWriteDMAPoll())
\r
620 if (!scsiActive && ((prep - i) > 0))
\r
622 int dmaBytes = SD_SECTOR_SIZE;
\r
623 if ((i % sdPerScsi) == (sdPerScsi - 1))
\r
625 dmaBytes = bytesPerSector % SD_SECTOR_SIZE;
\r
626 if (dmaBytes == 0) dmaBytes = SD_SECTOR_SIZE;
\r
628 scsiWriteDMA(&scsiDev.data[SD_SECTOR_SIZE * (i % buffers)], dmaBytes);
\r
634 // We've finished transferring the data to the FPGA, now wait until it's
\r
635 // written to he SCSI bus.
\r
636 while (!scsiPhyComplete() &&
\r
637 likely(scsiDev.phase == DATA_IN) &&
\r
638 likely(!scsiDev.resetFlag))
\r
646 if (scsiDev.phase == DATA_IN)
\r
648 scsiDev.phase = STATUS;
\r
652 else if (scsiDev.phase == DATA_OUT &&
\r
653 transfer.currentBlock != transfer.blocks)
\r
655 scsiEnterPhase(DATA_OUT);
\r
657 const int sdPerScsi = SDSectorsPerSCSISector(bytesPerSector);
\r
658 int totalSDSectors = transfer.blocks * sdPerScsi;
\r
659 // int buffers = sizeof(scsiDev.data) / SD_SECTOR_SIZE;
\r
662 // int scsiDisconnected = 0;
\r
663 int scsiComplete = 0;
\r
664 // uint32_t lastActivityTime = s2s_getTime_ms();
\r
665 // int scsiActive = 0;
\r
666 // int sdActive = 0;
\r
668 while ((i < totalSDSectors) &&
\r
669 (likely(scsiDev.phase == DATA_OUT) || // scsiDisconnect keeps our phase.
\r
671 likely(!scsiDev.resetFlag))
\r
673 // Well, until we have some proper non-blocking SD code, we must
\r
674 // do this in a half-duplex fashion. We need to write as much as
\r
675 // possible in each SD card transaction.
\r
676 uint32_t maxSectors = sizeof(scsiDev.data) / SD_SECTOR_SIZE;
\r
678 totalSDSectors < maxSectors ? totalSDSectors : maxSectors;
\r
679 scsiRead(&scsiDev.data[0], sectors * SD_SECTOR_SIZE);
\r
680 sdTmpWrite(&scsiDev.data[0], i + transfer.lba, sectors);
\r
683 // Wait for the next DMA interrupt. It's beneficial to halt the
\r
684 // processor to give the DMA controller more memory bandwidth to
\r
688 while (scsiBusy && sdBusy)
\r
690 uint8_t intr = CyEnterCriticalSection();
\r
691 scsiBusy = scsiDMABusy();
\r
692 sdBusy = sdDMABusy();
\r
693 if (scsiBusy && sdBusy)
\r
697 CyExitCriticalSection(intr);
\r
700 if (sdActive && !sdBusy && sdWriteSectorDMAPoll())
\r
705 if (!sdActive && ((prep - i) > 0))
\r
707 // Start an SD transfer if we have space.
\r
708 sdWriteMultiSectorDMA(&scsiDev.data[SD_SECTOR_SIZE * (i % buffers)]);
\r
712 uint32_t now = getTime_ms();
\r
714 if (scsiActive && !scsiBusy && scsiReadDMAPoll())
\r
718 lastActivityTime = now;
\r
721 ((prep - i) < buffers) &&
\r
722 (prep < totalSDSectors) &&
\r
723 likely(!scsiDisconnected))
\r
725 int dmaBytes = SD_SECTOR_SIZE;
\r
726 if ((prep % sdPerScsi) == (sdPerScsi - 1))
\r
728 dmaBytes = bytesPerSector % SD_SECTOR_SIZE;
\r
729 if (dmaBytes == 0) dmaBytes = SD_SECTOR_SIZE;
\r
731 scsiReadDMA(&scsiDev.data[SD_SECTOR_SIZE * (prep % buffers)], dmaBytes);
\r
735 (scsiDev.boardCfg.flags & CONFIG_ENABLE_DISCONNECT) &&
\r
736 (scsiActive == 0) &&
\r
737 likely(!scsiDisconnected) &&
\r
738 unlikely(scsiDev.discPriv) &&
\r
739 unlikely(diffTime_ms(lastActivityTime, now) >= 20) &&
\r
740 likely(scsiDev.phase == DATA_OUT))
\r
742 // We're transferring over the SCSI bus faster than the SD card
\r
743 // can write. There is no more buffer space once we've finished
\r
744 // this SCSI transfer.
\r
745 // The NCR 53C700 interface chips have a 250ms "byte-to-byte"
\r
746 // timeout buffer. SD card writes are supposed to complete
\r
747 // within 200ms, but sometimes they don't.
\r
748 // The NCR 53C700 series is used on HP 9000 workstations.
\r
750 scsiDisconnected = 1;
\r
751 lastActivityTime = getTime_ms();
\r
753 else if (unlikely(scsiDisconnected) &&
\r
755 (prep == i) || // Buffers empty.
\r
756 // Send some messages every 100ms so we don't timeout.
\r
757 // At a minimum, a reselection involves an IDENTIFY message.
\r
758 unlikely(diffTime_ms(lastActivityTime, now) >= 100)
\r
761 int reconnected = scsiReconnect();
\r
764 scsiDisconnected = 0;
\r
765 lastActivityTime = getTime_ms(); // Don't disconnect immediately.
\r
767 else if (diffTime_ms(lastActivityTime, getTime_ms()) >= 10000)
\r
769 // Give up after 10 seconds of trying to reconnect.
\r
770 scsiDev.resetFlag = 1;
\r
774 likely(!scsiComplete) &&
\r
776 (prep == totalSDSectors) && // All scsi data read and buffered
\r
777 likely(!scsiDev.discPriv) && // Prefer disconnect where possible.
\r
778 unlikely(diffTime_ms(lastActivityTime, now) >= 150) &&
\r
780 likely(scsiDev.phase == DATA_OUT) &&
\r
781 !(scsiDev.cdb[scsiDev.cdbLen - 1] & 0x01) // Not linked command
\r
784 // We're transferring over the SCSI bus faster than the SD card
\r
785 // can write. All data is buffered, and we're just waiting for
\r
786 // the SD card to complete. The host won't let us disconnect.
\r
787 // Some drivers set a 250ms timeout on transfers to complete.
\r
788 // SD card writes are supposed to complete
\r
789 // within 200ms, but sometimes they don'to.
\r
790 // Just pretend we're finished.
\r
794 process_MessageIn(); // Will go to BUS_FREE state
\r
796 // Try and prevent anyone else using the SCSI bus while we're not ready.
\r
797 SCSI_SetPin(SCSI_Out_BSY);
\r
805 SCSI_ClearPin(SCSI_Out_BSY);
\r
808 !scsiDev.resetFlag &&
\r
809 unlikely(scsiDisconnected) &&
\r
810 (s2s_elapsedTime_ms(lastActivityTime) <= 10000))
\r
812 scsiDisconnected = !scsiReconnect();
\r
814 if (scsiDisconnected)
\r
816 // Failed to reconnect
\r
817 scsiDev.resetFlag = 1;
\r
821 if (scsiDev.phase == DATA_OUT)
\r
823 if (scsiDev.parityError &&
\r
824 (scsiDev.boardCfg.flags & S2S_CFG_ENABLE_PARITY) &&
\r
825 (scsiDev.compatMode >= COMPAT_SCSI2))
\r
827 scsiDev.target->sense.code = ABORTED_COMMAND;
\r
828 scsiDev.target->sense.asc = SCSI_PARITY_ERROR;
\r
829 scsiDev.status = CHECK_CONDITION;;
\r
831 scsiDev.phase = STATUS;
\r
837 void scsiDiskReset()
\r
839 scsiDev.dataPtr = 0;
\r
840 scsiDev.savedDataPtr = 0;
\r
841 scsiDev.dataLen = 0;
\r
842 // transfer.lba = 0; // Needed in Request Sense to determine failure
\r
843 transfer.blocks = 0;
\r
844 transfer.currentBlock = 0;
\r
846 // Cancel long running commands!
\r
848 ((scsiDev.boardCfg.flags & S2S_CFG_ENABLE_CACHE) == 0) ||
\r
849 (transfer.multiBlock == 0)
\r
853 sdCompleteTransfer();
\r
857 transfer.multiBlock = 0;
\r
860 void scsiDiskInit()
\r
864 // Don't require the host to send us a START STOP UNIT command
\r
865 blockDev.state = DISK_STARTED;
\r
866 // WP pin not available for micro-sd
\r
867 // TODO read card WP register
\r
871 blockDev.state = blockDev.state | DISK_WP;
\r