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
19 #include "stm32f2xx.h"
\r
22 #include "scsiPhy.h"
\r
31 BlockDevice blockDev;
\r
34 static int doSdInit()
\r
37 if (blockDev.state & DISK_PRESENT)
\r
39 blockDev.state = blockDev.state | DISK_INITIALISED;
\r
44 // Callback once all data has been read in the data out phase.
\r
45 static void doFormatUnitComplete(void)
\r
47 // TODO start writing the initialisation pattern to the SD
\r
49 scsiDev.phase = STATUS;
\r
52 static void doFormatUnitSkipData(int bytes)
\r
54 // We may not have enough memory to store the initialisation pattern and
\r
55 // defect list data. Since we're not making use of it yet anyway, just
\r
56 // discard the bytes.
\r
57 scsiEnterPhase(DATA_OUT);
\r
59 for (i = 0; i < bytes; ++i)
\r
65 // Callback from the data out phase.
\r
66 static void doFormatUnitPatternHeader(void)
\r
69 ((((uint16_t)scsiDev.data[2])) << 8) +
\r
73 ((((uint16_t)scsiDev.data[4 + 2])) << 8) +
\r
74 scsiDev.data[4 + 3];
\r
76 doFormatUnitSkipData(defectLength + patternLength);
\r
77 doFormatUnitComplete();
\r
80 // Callback from the data out phase.
\r
81 static void doFormatUnitHeader(void)
\r
83 int IP = (scsiDev.data[1] & 0x08) ? 1 : 0;
\r
84 int DSP = (scsiDev.data[1] & 0x04) ? 1 : 0;
\r
86 if (! DSP) // disable save parameters
\r
88 // Save the "MODE SELECT savable parameters"
\r
90 scsiDev.target->targetId,
\r
91 scsiDev.target->liveCfg.bytesPerSector);
\r
96 // We need to read the initialisation pattern header first.
\r
97 scsiDev.dataLen += 4;
\r
98 scsiDev.phase = DATA_OUT;
\r
99 scsiDev.postDataOutHook = doFormatUnitPatternHeader;
\r
103 // Read the defect list data
\r
105 ((((uint16_t)scsiDev.data[2])) << 8) +
\r
107 doFormatUnitSkipData(defectLength);
\r
108 doFormatUnitComplete();
\r
112 static void doReadCapacity()
\r
114 uint32_t lba = (((uint32_t) scsiDev.cdb[2]) << 24) +
\r
115 (((uint32_t) scsiDev.cdb[3]) << 16) +
\r
116 (((uint32_t) scsiDev.cdb[4]) << 8) +
\r
118 int pmi = scsiDev.cdb[8] & 1;
\r
120 uint32_t capacity = getScsiCapacity(
\r
121 scsiDev.target->cfg->sdSectorStart,
\r
122 scsiDev.target->liveCfg.bytesPerSector,
\r
123 scsiDev.target->cfg->scsiSectors);
\r
128 // We don't do anything with the "partial medium indicator", and
\r
129 // assume that delays are constant across each block. But the spec
\r
130 // says we must return this error if pmi is specified incorrectly.
\r
131 scsiDev.status = CHECK_CONDITION;
\r
132 scsiDev.target->sense.code = ILLEGAL_REQUEST;
\r
133 scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;
\r
134 scsiDev.phase = STATUS;
\r
136 else if (capacity > 0)
\r
138 uint32_t highestBlock = capacity - 1;
\r
140 scsiDev.data[0] = highestBlock >> 24;
\r
141 scsiDev.data[1] = highestBlock >> 16;
\r
142 scsiDev.data[2] = highestBlock >> 8;
\r
143 scsiDev.data[3] = highestBlock;
\r
145 uint32_t bytesPerSector = scsiDev.target->liveCfg.bytesPerSector;
\r
146 scsiDev.data[4] = bytesPerSector >> 24;
\r
147 scsiDev.data[5] = bytesPerSector >> 16;
\r
148 scsiDev.data[6] = bytesPerSector >> 8;
\r
149 scsiDev.data[7] = bytesPerSector;
\r
150 scsiDev.dataLen = 8;
\r
151 scsiDev.phase = DATA_IN;
\r
155 scsiDev.status = CHECK_CONDITION;
\r
156 scsiDev.target->sense.code = NOT_READY;
\r
157 scsiDev.target->sense.asc = MEDIUM_NOT_PRESENT;
\r
158 scsiDev.phase = STATUS;
\r
162 static void doWrite(uint32_t lba, uint32_t blocks)
\r
164 if (unlikely(scsiDev.target->cfg->deviceType == S2S_CFG_FLOPPY_14MB)) {
\r
165 // Floppies are supposed to be slow. Some systems can't handle a floppy
\r
166 // without an access time
\r
170 uint32_t bytesPerSector = scsiDev.target->liveCfg.bytesPerSector;
\r
172 if (unlikely(blockDev.state & DISK_WP) ||
\r
173 unlikely(scsiDev.target->cfg->deviceType == S2S_CFG_OPTICAL))
\r
176 scsiDev.status = CHECK_CONDITION;
\r
177 scsiDev.target->sense.code = ILLEGAL_REQUEST;
\r
178 scsiDev.target->sense.asc = WRITE_PROTECTED;
\r
179 scsiDev.phase = STATUS;
\r
181 else if (unlikely(((uint64_t) lba) + blocks >
\r
183 scsiDev.target->cfg->sdSectorStart,
\r
185 scsiDev.target->cfg->scsiSectors
\r
189 scsiDev.status = CHECK_CONDITION;
\r
190 scsiDev.target->sense.code = ILLEGAL_REQUEST;
\r
191 scsiDev.target->sense.asc = LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
\r
192 scsiDev.phase = STATUS;
\r
196 transfer.lba = lba;
\r
197 transfer.blocks = blocks;
\r
198 transfer.currentBlock = 0;
\r
199 scsiDev.phase = DATA_OUT;
\r
200 scsiDev.dataLen = bytesPerSector;
\r
201 scsiDev.dataPtr = bytesPerSector;
\r
203 // No need for single-block writes atm. Overhead of the
\r
204 // multi-block write is minimal.
\r
205 transfer.multiBlock = 1;
\r
208 // TODO uint32_t sdLBA =
\r
209 // TODO SCSISector2SD(
\r
210 // TODO scsiDev.target->cfg->sdSectorStart,
\r
211 // TODO bytesPerSector,
\r
213 // TODO uint32_t sdBlocks = blocks * SDSectorsPerSCSISector(bytesPerSector);
\r
214 // TODO sdWriteMultiSectorPrep(sdLBA, sdBlocks);
\r
219 static void doRead(uint32_t lba, uint32_t blocks)
\r
221 if (unlikely(scsiDev.target->cfg->deviceType == S2S_CFG_FLOPPY_14MB)) {
\r
222 // Floppies are supposed to be slow. Some systems can't handle a floppy
\r
223 // without an access time
\r
227 uint32_t capacity = getScsiCapacity(
\r
228 scsiDev.target->cfg->sdSectorStart,
\r
229 scsiDev.target->liveCfg.bytesPerSector,
\r
230 scsiDev.target->cfg->scsiSectors);
\r
231 if (unlikely(((uint64_t) lba) + blocks > capacity))
\r
233 scsiDev.status = CHECK_CONDITION;
\r
234 scsiDev.target->sense.code = ILLEGAL_REQUEST;
\r
235 scsiDev.target->sense.asc = LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
\r
236 scsiDev.phase = STATUS;
\r
240 transfer.lba = lba;
\r
241 transfer.blocks = blocks;
\r
242 transfer.currentBlock = 0;
\r
243 scsiDev.phase = DATA_IN;
\r
244 scsiDev.dataLen = 0; // No data yet
\r
246 uint32_t bytesPerSector = scsiDev.target->liveCfg.bytesPerSector;
\r
247 uint32_t sdSectorPerSCSISector = SDSectorsPerSCSISector(bytesPerSector);
\r
248 uint32_t sdSectors =
\r
249 blocks * sdSectorPerSCSISector;
\r
252 (sdSectors == 1) &&
\r
253 !(scsiDev.boardCfg.flags & S2S_CFG_ENABLE_CACHE)
\r
255 unlikely(((uint64_t) lba) + blocks == capacity)
\r
258 // We get errors on reading the last sector using a multi-sector
\r
260 transfer.multiBlock = 0;
\r
264 transfer.multiBlock = 1;
\r
266 // uint32_t sdLBA =
\r
268 // scsiDev.target->cfg->sdSectorStart,
\r
272 // TODO sdReadMultiSectorPrep(sdLBA, sdSectors);
\r
277 static void doSeek(uint32_t lba)
\r
281 scsiDev.target->cfg->sdSectorStart,
\r
282 scsiDev.target->liveCfg.bytesPerSector,
\r
283 scsiDev.target->cfg->scsiSectors)
\r
286 scsiDev.status = CHECK_CONDITION;
\r
287 scsiDev.target->sense.code = ILLEGAL_REQUEST;
\r
288 scsiDev.target->sense.asc = LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
\r
289 scsiDev.phase = STATUS;
\r
293 static int doTestUnitReady()
\r
296 if (likely(blockDev.state == (DISK_STARTED | DISK_PRESENT | DISK_INITIALISED)))
\r
300 else if (unlikely(!(blockDev.state & DISK_STARTED)))
\r
303 scsiDev.status = CHECK_CONDITION;
\r
304 scsiDev.target->sense.code = NOT_READY;
\r
305 scsiDev.target->sense.asc = LOGICAL_UNIT_NOT_READY_INITIALIZING_COMMAND_REQUIRED;
\r
306 scsiDev.phase = STATUS;
\r
308 else if (unlikely(!(blockDev.state & DISK_PRESENT)))
\r
311 scsiDev.status = CHECK_CONDITION;
\r
312 scsiDev.target->sense.code = NOT_READY;
\r
313 scsiDev.target->sense.asc = MEDIUM_NOT_PRESENT;
\r
314 scsiDev.phase = STATUS;
\r
316 else if (unlikely(!(blockDev.state & DISK_INITIALISED)))
\r
319 scsiDev.status = CHECK_CONDITION;
\r
320 scsiDev.target->sense.code = NOT_READY;
\r
321 scsiDev.target->sense.asc = LOGICAL_UNIT_NOT_READY_CAUSE_NOT_REPORTABLE;
\r
322 scsiDev.phase = STATUS;
\r
327 // Handle direct-access scsi device commands
\r
328 int scsiDiskCommand()
\r
330 int commandHandled = 1;
\r
332 uint8_t command = scsiDev.cdb[0];
\r
333 if (unlikely(command == 0x1B))
\r
336 // Enable or disable media access operations.
\r
337 // Ignore load/eject requests. We can't do that.
\r
338 //int immed = scsiDev.cdb[1] & 1;
\r
339 int start = scsiDev.cdb[4] & 1;
\r
343 blockDev.state = blockDev.state | DISK_STARTED;
\r
344 if (!(blockDev.state & DISK_INITIALISED))
\r
351 blockDev.state &= ~DISK_STARTED;
\r
354 else if (unlikely(command == 0x00))
\r
359 else if (unlikely(!doTestUnitReady()))
\r
361 // Status and sense codes already set by doTestUnitReady
\r
363 else if (likely(command == 0x08))
\r
367 (((uint32_t) scsiDev.cdb[1] & 0x1F) << 16) +
\r
368 (((uint32_t) scsiDev.cdb[2]) << 8) +
\r
370 uint32_t blocks = scsiDev.cdb[4];
\r
371 if (unlikely(blocks == 0)) blocks = 256;
\r
372 doRead(lba, blocks);
\r
374 else if (likely(command == 0x28))
\r
377 // Ignore all cache control bits - we don't support a memory cache.
\r
380 (((uint32_t) scsiDev.cdb[2]) << 24) +
\r
381 (((uint32_t) scsiDev.cdb[3]) << 16) +
\r
382 (((uint32_t) scsiDev.cdb[4]) << 8) +
\r
385 (((uint32_t) scsiDev.cdb[7]) << 8) +
\r
388 doRead(lba, blocks);
\r
390 else if (likely(command == 0x0A))
\r
394 (((uint32_t) scsiDev.cdb[1] & 0x1F) << 16) +
\r
395 (((uint32_t) scsiDev.cdb[2]) << 8) +
\r
397 uint32_t blocks = scsiDev.cdb[4];
\r
398 if (unlikely(blocks == 0)) blocks = 256;
\r
399 doWrite(lba, blocks);
\r
401 else if (likely(command == 0x2A) || // WRITE(10)
\r
402 unlikely(command == 0x2E)) // WRITE AND VERIFY
\r
404 // Ignore all cache control bits - we don't support a memory cache.
\r
405 // Don't bother verifying either. The SD card likely stores ECC
\r
406 // along with each flash row.
\r
409 (((uint32_t) scsiDev.cdb[2]) << 24) +
\r
410 (((uint32_t) scsiDev.cdb[3]) << 16) +
\r
411 (((uint32_t) scsiDev.cdb[4]) << 8) +
\r
414 (((uint32_t) scsiDev.cdb[7]) << 8) +
\r
417 doWrite(lba, blocks);
\r
419 else if (unlikely(command == 0x04))
\r
422 // We don't really do any formatting, but we need to read the correct
\r
423 // number of bytes in the DATA_OUT phase to make the SCSI host happy.
\r
425 int fmtData = (scsiDev.cdb[1] & 0x10) ? 1 : 0;
\r
428 // We need to read the parameter list, but we don't know how
\r
429 // big it is yet. Start with the header.
\r
430 scsiDev.dataLen = 4;
\r
431 scsiDev.phase = DATA_OUT;
\r
432 scsiDev.postDataOutHook = doFormatUnitHeader;
\r
436 // No data to read, we're already finished!
\r
439 else if (unlikely(command == 0x25))
\r
444 else if (unlikely(command == 0x0B))
\r
448 (((uint32_t) scsiDev.cdb[1] & 0x1F) << 16) +
\r
449 (((uint32_t) scsiDev.cdb[2]) << 8) +
\r
455 else if (unlikely(command == 0x2B))
\r
459 (((uint32_t) scsiDev.cdb[2]) << 24) +
\r
460 (((uint32_t) scsiDev.cdb[3]) << 16) +
\r
461 (((uint32_t) scsiDev.cdb[4]) << 8) +
\r
466 else if (unlikely(command == 0x36))
\r
468 // LOCK UNLOCK CACHE
\r
469 // We don't have a cache to lock data into. do nothing.
\r
471 else if (unlikely(command == 0x34))
\r
474 // We don't have a cache to pre-fetch into. do nothing.
\r
476 else if (unlikely(command == 0x1E))
\r
478 // PREVENT ALLOW MEDIUM REMOVAL
\r
479 // Not much we can do to prevent the user removing the SD card.
\r
482 else if (unlikely(command == 0x01))
\r
485 // Set the lun to a vendor-specific state. Ignore.
\r
487 else if (unlikely(command == 0x35))
\r
489 // SYNCHRONIZE CACHE
\r
490 // We don't have a cache. do nothing.
\r
492 else if (unlikely(command == 0x2F))
\r
495 // TODO: When they supply data to verify, we should read the data and
\r
496 // verify it. If they don't supply any data, just say success.
\r
497 if ((scsiDev.cdb[1] & 0x02) == 0)
\r
499 // They are asking us to do a medium verification with no data
\r
500 // comparison. Assume success, do nothing.
\r
504 // TODO. This means they are supplying data to verify against.
\r
505 // Technically we should probably grab the data and compare it.
\r
506 scsiDev.status = CHECK_CONDITION;
\r
507 scsiDev.target->sense.code = ILLEGAL_REQUEST;
\r
508 scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;
\r
509 scsiDev.phase = STATUS;
\r
512 else if (unlikely(command == 0x37))
\r
514 // READ DEFECT DATA
\r
515 scsiDev.status = CHECK_CONDITION;
\r
516 scsiDev.target->sense.code = NO_SENSE;
\r
517 scsiDev.target->sense.asc = DEFECT_LIST_NOT_FOUND;
\r
518 scsiDev.phase = STATUS;
\r
523 commandHandled = 0;
\r
526 return commandHandled;
\r
529 void scsiDiskPoll()
\r
531 uint32_t bytesPerSector = scsiDev.target->liveCfg.bytesPerSector;
\r
533 if (scsiDev.phase == DATA_IN &&
\r
534 transfer.currentBlock != transfer.blocks)
\r
536 scsiEnterPhase(DATA_IN);
\r
538 int totalSDSectors =
\r
539 transfer.blocks * SDSectorsPerSCSISector(bytesPerSector);
\r
542 scsiDev.target->cfg->sdSectorStart,
\r
546 const int sdPerScsi = SDSectorsPerSCSISector(bytesPerSector);
\r
547 int buffers = sizeof(scsiDev.data) / SD_SECTOR_SIZE;
\r
550 int scsiActive = 0;
\r
552 while ((i < totalSDSectors) &&
\r
553 likely(scsiDev.phase == DATA_IN) &&
\r
554 likely(!scsiDev.resetFlag))
\r
556 // Wait for the next DMA interrupt. It's beneficial to halt the
\r
557 // processor to give the DMA controller more memory bandwidth to
\r
559 if (sdActive && scsiActive)
\r
564 if (sdActive && sdReadDMAPoll())
\r
571 (prep - i < buffers) &&
\r
572 (prep < totalSDSectors))
\r
574 // Start an SD transfer if we have space.
\r
575 uint32_t startBuffer = prep % buffers;
\r
576 uint32_t sectors = totalSDSectors - prep;
\r
577 if (!scsiActive && prep == i)
\r
579 sectors = 1; // We need to get some data to send ASAP !
\r
583 uint32_t freeBuffers = buffers - (prep - i);
\r
584 uint32_t contiguousBuffers = buffers - startBuffer;
\r
585 freeBuffers = freeBuffers < contiguousBuffers
\r
586 ? freeBuffers : contiguousBuffers;
\r
587 sectors = sectors < freeBuffers ? sectors : freeBuffers;
\r
589 sdReadDMA(sdLBA + prep, sectors, &scsiDev.data[SD_SECTOR_SIZE * startBuffer]);
\r
591 sdActive = sectors;
\r
594 if (scsiActive && scsiPhyComplete() && scsiWriteDMAPoll())
\r
600 if (!scsiActive && ((prep - i) > 0))
\r
602 int dmaBytes = SD_SECTOR_SIZE;
\r
603 if ((i % sdPerScsi) == (sdPerScsi - 1))
\r
605 dmaBytes = bytesPerSector % SD_SECTOR_SIZE;
\r
606 if (dmaBytes == 0) dmaBytes = SD_SECTOR_SIZE;
\r
608 scsiWriteDMA(&scsiDev.data[SD_SECTOR_SIZE * (i % buffers)], dmaBytes);
\r
613 // We've finished transferring the data to the FPGA, now wait until it's
\r
614 // written to he SCSI bus.
\r
615 while (!scsiPhyComplete() &&
\r
616 likely(scsiDev.phase == DATA_IN) &&
\r
617 likely(!scsiDev.resetFlag))
\r
623 if (scsiDev.phase == DATA_IN)
\r
625 scsiDev.phase = STATUS;
\r
629 else if (scsiDev.phase == DATA_OUT &&
\r
630 transfer.currentBlock != transfer.blocks)
\r
632 scsiEnterPhase(DATA_OUT);
\r
634 const int sdPerScsi = SDSectorsPerSCSISector(bytesPerSector);
\r
635 int totalSDSectors = transfer.blocks * sdPerScsi;
\r
638 scsiDev.target->cfg->sdSectorStart,
\r
641 // int buffers = sizeof(scsiDev.data) / SD_SECTOR_SIZE;
\r
644 // int scsiDisconnected = 0;
\r
645 int scsiComplete = 0;
\r
646 // uint32_t lastActivityTime = s2s_getTime_ms();
\r
647 // int scsiActive = 0;
\r
648 // int sdActive = 0;
\r
650 while ((i < totalSDSectors) &&
\r
651 (likely(scsiDev.phase == DATA_OUT) || // scsiDisconnect keeps our phase.
\r
653 likely(!scsiDev.resetFlag))
\r
655 // Well, until we have some proper non-blocking SD code, we must
\r
656 // do this in a half-duplex fashion. We need to write as much as
\r
657 // possible in each SD card transaction.
\r
658 uint32_t maxSectors = sizeof(scsiDev.data) / SD_SECTOR_SIZE;
\r
659 uint32_t rem = totalSDSectors - i;
\r
661 rem < maxSectors ? rem : maxSectors;
\r
662 scsiRead(&scsiDev.data[0], sectors * SD_SECTOR_SIZE);
\r
663 sdTmpWrite(&scsiDev.data[0], i + sdLBA, sectors);
\r
666 // Wait for the next DMA interrupt. It's beneficial to halt the
\r
667 // processor to give the DMA controller more memory bandwidth to
\r
671 while (scsiBusy && sdBusy)
\r
673 uint8_t intr = CyEnterCriticalSection();
\r
674 scsiBusy = scsiDMABusy();
\r
675 sdBusy = sdDMABusy();
\r
676 if (scsiBusy && sdBusy)
\r
680 CyExitCriticalSection(intr);
\r
683 if (sdActive && !sdBusy && sdWriteSectorDMAPoll())
\r
688 if (!sdActive && ((prep - i) > 0))
\r
690 // Start an SD transfer if we have space.
\r
691 sdWriteMultiSectorDMA(&scsiDev.data[SD_SECTOR_SIZE * (i % buffers)]);
\r
695 uint32_t now = getTime_ms();
\r
697 if (scsiActive && !scsiBusy && scsiReadDMAPoll())
\r
701 lastActivityTime = now;
\r
704 ((prep - i) < buffers) &&
\r
705 (prep < totalSDSectors) &&
\r
706 likely(!scsiDisconnected))
\r
708 int dmaBytes = SD_SECTOR_SIZE;
\r
709 if ((prep % sdPerScsi) == (sdPerScsi - 1))
\r
711 dmaBytes = bytesPerSector % SD_SECTOR_SIZE;
\r
712 if (dmaBytes == 0) dmaBytes = SD_SECTOR_SIZE;
\r
714 scsiReadDMA(&scsiDev.data[SD_SECTOR_SIZE * (prep % buffers)], dmaBytes);
\r
718 (scsiDev.boardCfg.flags & CONFIG_ENABLE_DISCONNECT) &&
\r
719 (scsiActive == 0) &&
\r
720 likely(!scsiDisconnected) &&
\r
721 unlikely(scsiDev.discPriv) &&
\r
722 unlikely(diffTime_ms(lastActivityTime, now) >= 20) &&
\r
723 likely(scsiDev.phase == DATA_OUT))
\r
725 // We're transferring over the SCSI bus faster than the SD card
\r
726 // can write. There is no more buffer space once we've finished
\r
727 // this SCSI transfer.
\r
728 // The NCR 53C700 interface chips have a 250ms "byte-to-byte"
\r
729 // timeout buffer. SD card writes are supposed to complete
\r
730 // within 200ms, but sometimes they don't.
\r
731 // The NCR 53C700 series is used on HP 9000 workstations.
\r
733 scsiDisconnected = 1;
\r
734 lastActivityTime = getTime_ms();
\r
736 else if (unlikely(scsiDisconnected) &&
\r
738 (prep == i) || // Buffers empty.
\r
739 // Send some messages every 100ms so we don't timeout.
\r
740 // At a minimum, a reselection involves an IDENTIFY message.
\r
741 unlikely(diffTime_ms(lastActivityTime, now) >= 100)
\r
744 int reconnected = scsiReconnect();
\r
747 scsiDisconnected = 0;
\r
748 lastActivityTime = getTime_ms(); // Don't disconnect immediately.
\r
750 else if (diffTime_ms(lastActivityTime, getTime_ms()) >= 10000)
\r
752 // Give up after 10 seconds of trying to reconnect.
\r
753 scsiDev.resetFlag = 1;
\r
757 likely(!scsiComplete) &&
\r
759 (prep == totalSDSectors) && // All scsi data read and buffered
\r
760 likely(!scsiDev.discPriv) && // Prefer disconnect where possible.
\r
761 unlikely(diffTime_ms(lastActivityTime, now) >= 150) &&
\r
763 likely(scsiDev.phase == DATA_OUT) &&
\r
764 !(scsiDev.cdb[scsiDev.cdbLen - 1] & 0x01) // Not linked command
\r
767 // We're transferring over the SCSI bus faster than the SD card
\r
768 // can write. All data is buffered, and we're just waiting for
\r
769 // the SD card to complete. The host won't let us disconnect.
\r
770 // Some drivers set a 250ms timeout on transfers to complete.
\r
771 // SD card writes are supposed to complete
\r
772 // within 200ms, but sometimes they don'to.
\r
773 // Just pretend we're finished.
\r
777 process_MessageIn(); // Will go to BUS_FREE state
\r
779 // Try and prevent anyone else using the SCSI bus while we're not ready.
\r
780 SCSI_SetPin(SCSI_Out_BSY);
\r
788 SCSI_ClearPin(SCSI_Out_BSY);
\r
791 !scsiDev.resetFlag &&
\r
792 unlikely(scsiDisconnected) &&
\r
793 (s2s_elapsedTime_ms(lastActivityTime) <= 10000))
\r
795 scsiDisconnected = !scsiReconnect();
\r
797 if (scsiDisconnected)
\r
799 // Failed to reconnect
\r
800 scsiDev.resetFlag = 1;
\r
804 if (scsiDev.phase == DATA_OUT)
\r
806 if (scsiDev.parityError &&
\r
807 (scsiDev.boardCfg.flags & S2S_CFG_ENABLE_PARITY) &&
\r
808 (scsiDev.compatMode >= COMPAT_SCSI2))
\r
810 scsiDev.target->sense.code = ABORTED_COMMAND;
\r
811 scsiDev.target->sense.asc = SCSI_PARITY_ERROR;
\r
812 scsiDev.status = CHECK_CONDITION;;
\r
814 scsiDev.phase = STATUS;
\r
820 void scsiDiskReset()
\r
822 scsiDev.dataPtr = 0;
\r
823 scsiDev.savedDataPtr = 0;
\r
824 scsiDev.dataLen = 0;
\r
825 // transfer.lba = 0; // Needed in Request Sense to determine failure
\r
826 transfer.blocks = 0;
\r
827 transfer.currentBlock = 0;
\r
829 // Cancel long running commands!
\r
832 ((scsiDev.boardCfg.flags & S2S_CFG_ENABLE_CACHE) == 0) ||
\r
833 (transfer.multiBlock == 0)
\r
837 sdCompleteTransfer();
\r
840 transfer.multiBlock = 0;
\r
843 void scsiDiskInit()
\r
847 // Don't require the host to send us a START STOP UNIT command
\r
848 blockDev.state = DISK_STARTED;
\r