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 "stm32f2xx.h"
\r
23 #include "stm32f4xx.h"
\r
28 // For SD write direct routines
\r
30 #include "bsp_driver_sd.h"
\r
34 #include "scsiPhy.h"
\r
44 BlockDevice blockDev;
\r
47 static int doSdInit()
\r
50 if (blockDev.state & DISK_PRESENT)
\r
52 blockDev.state = blockDev.state | DISK_INITIALISED;
\r
57 // Callback once all data has been read in the data out phase.
\r
58 static void doFormatUnitComplete(void)
\r
60 // TODO start writing the initialisation pattern to the SD
\r
62 scsiDev.phase = STATUS;
\r
65 static void doFormatUnitSkipData(int bytes)
\r
67 // We may not have enough memory to store the initialisation pattern and
\r
68 // defect list data. Since we're not making use of it yet anyway, just
\r
69 // discard the bytes.
\r
70 scsiEnterPhase(DATA_OUT);
\r
72 for (i = 0; i < bytes; ++i)
\r
78 // Callback from the data out phase.
\r
79 static void doFormatUnitPatternHeader(void)
\r
82 ((((uint16_t)scsiDev.data[2])) << 8) +
\r
86 ((((uint16_t)scsiDev.data[4 + 2])) << 8) +
\r
87 scsiDev.data[4 + 3];
\r
89 doFormatUnitSkipData(defectLength + patternLength);
\r
90 doFormatUnitComplete();
\r
93 // Callback from the data out phase.
\r
94 static void doFormatUnitHeader(void)
\r
96 int IP = (scsiDev.data[1] & 0x08) ? 1 : 0;
\r
97 int DSP = (scsiDev.data[1] & 0x04) ? 1 : 0;
\r
99 if (! DSP) // disable save parameters
\r
101 // Save the "MODE SELECT savable parameters"
\r
103 scsiDev.target->targetId,
\r
104 scsiDev.target->liveCfg.bytesPerSector);
\r
109 // We need to read the initialisation pattern header first.
\r
110 scsiDev.dataLen += 4;
\r
111 scsiDev.phase = DATA_OUT;
\r
112 scsiDev.postDataOutHook = doFormatUnitPatternHeader;
\r
116 // Read the defect list data
\r
118 ((((uint16_t)scsiDev.data[2])) << 8) +
\r
120 doFormatUnitSkipData(defectLength);
\r
121 doFormatUnitComplete();
\r
125 static void doReadCapacity()
\r
127 uint32_t lba = (((uint32_t) scsiDev.cdb[2]) << 24) +
\r
128 (((uint32_t) scsiDev.cdb[3]) << 16) +
\r
129 (((uint32_t) scsiDev.cdb[4]) << 8) +
\r
131 int pmi = scsiDev.cdb[8] & 1;
\r
133 uint32_t capacity = getScsiCapacity(
\r
134 scsiDev.target->cfg->sdSectorStart,
\r
135 scsiDev.target->liveCfg.bytesPerSector,
\r
136 scsiDev.target->cfg->scsiSectors);
\r
141 // We don't do anything with the "partial medium indicator", and
\r
142 // assume that delays are constant across each block. But the spec
\r
143 // says we must return this error if pmi is specified incorrectly.
\r
144 scsiDev.status = CHECK_CONDITION;
\r
145 scsiDev.target->sense.code = ILLEGAL_REQUEST;
\r
146 scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;
\r
147 scsiDev.phase = STATUS;
\r
149 else if (capacity > 0)
\r
151 uint32_t highestBlock = capacity - 1;
\r
153 scsiDev.data[0] = highestBlock >> 24;
\r
154 scsiDev.data[1] = highestBlock >> 16;
\r
155 scsiDev.data[2] = highestBlock >> 8;
\r
156 scsiDev.data[3] = highestBlock;
\r
158 uint32_t bytesPerSector = scsiDev.target->liveCfg.bytesPerSector;
\r
159 scsiDev.data[4] = bytesPerSector >> 24;
\r
160 scsiDev.data[5] = bytesPerSector >> 16;
\r
161 scsiDev.data[6] = bytesPerSector >> 8;
\r
162 scsiDev.data[7] = bytesPerSector;
\r
163 scsiDev.dataLen = 8;
\r
164 scsiDev.phase = DATA_IN;
\r
168 scsiDev.status = CHECK_CONDITION;
\r
169 scsiDev.target->sense.code = NOT_READY;
\r
170 scsiDev.target->sense.asc = MEDIUM_NOT_PRESENT;
\r
171 scsiDev.phase = STATUS;
\r
175 static void doWrite(uint32_t lba, uint32_t blocks)
\r
177 if (unlikely(scsiDev.target->cfg->deviceType == S2S_CFG_FLOPPY_14MB)) {
\r
178 // Floppies are supposed to be slow. Some systems can't handle a floppy
\r
179 // without an access time
\r
183 uint32_t bytesPerSector = scsiDev.target->liveCfg.bytesPerSector;
\r
185 if (unlikely(blockDev.state & DISK_WP) ||
\r
186 unlikely(scsiDev.target->cfg->deviceType == S2S_CFG_OPTICAL))
\r
189 scsiDev.status = CHECK_CONDITION;
\r
190 scsiDev.target->sense.code = ILLEGAL_REQUEST;
\r
191 scsiDev.target->sense.asc = WRITE_PROTECTED;
\r
192 scsiDev.phase = STATUS;
\r
194 else if (unlikely(((uint64_t) lba) + blocks >
\r
196 scsiDev.target->cfg->sdSectorStart,
\r
198 scsiDev.target->cfg->scsiSectors
\r
202 scsiDev.status = CHECK_CONDITION;
\r
203 scsiDev.target->sense.code = ILLEGAL_REQUEST;
\r
204 scsiDev.target->sense.asc = LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
\r
205 scsiDev.phase = STATUS;
\r
209 transfer.lba = lba;
\r
210 transfer.blocks = blocks;
\r
211 transfer.currentBlock = 0;
\r
212 scsiDev.phase = DATA_OUT;
\r
213 scsiDev.dataLen = bytesPerSector;
\r
214 scsiDev.dataPtr = bytesPerSector;
\r
216 // No need for single-block writes atm. Overhead of the
\r
217 // multi-block write is minimal.
\r
218 transfer.multiBlock = 1;
\r
221 // TODO uint32_t sdLBA =
\r
222 // TODO SCSISector2SD(
\r
223 // TODO scsiDev.target->cfg->sdSectorStart,
\r
224 // TODO bytesPerSector,
\r
226 // TODO uint32_t sdBlocks = blocks * SDSectorsPerSCSISector(bytesPerSector);
\r
227 // TODO sdWriteMultiSectorPrep(sdLBA, sdBlocks);
\r
232 static void doRead(uint32_t lba, uint32_t blocks)
\r
234 if (unlikely(scsiDev.target->cfg->deviceType == S2S_CFG_FLOPPY_14MB)) {
\r
235 // Floppies are supposed to be slow. Some systems can't handle a floppy
\r
236 // without an access time
\r
240 uint32_t capacity = getScsiCapacity(
\r
241 scsiDev.target->cfg->sdSectorStart,
\r
242 scsiDev.target->liveCfg.bytesPerSector,
\r
243 scsiDev.target->cfg->scsiSectors);
\r
244 if (unlikely(((uint64_t) lba) + blocks > capacity))
\r
246 scsiDev.status = CHECK_CONDITION;
\r
247 scsiDev.target->sense.code = ILLEGAL_REQUEST;
\r
248 scsiDev.target->sense.asc = LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
\r
249 scsiDev.phase = STATUS;
\r
253 transfer.lba = lba;
\r
254 transfer.blocks = blocks;
\r
255 transfer.currentBlock = 0;
\r
256 scsiDev.phase = DATA_IN;
\r
257 scsiDev.dataLen = 0; // No data yet
\r
259 uint32_t bytesPerSector = scsiDev.target->liveCfg.bytesPerSector;
\r
260 uint32_t sdSectorPerSCSISector = SDSectorsPerSCSISector(bytesPerSector);
\r
261 uint32_t sdSectors =
\r
262 blocks * sdSectorPerSCSISector;
\r
265 (sdSectors == 1) &&
\r
266 !(scsiDev.boardCfg.flags & S2S_CFG_ENABLE_CACHE)
\r
268 unlikely(((uint64_t) lba) + blocks == capacity)
\r
271 // We get errors on reading the last sector using a multi-sector
\r
273 transfer.multiBlock = 0;
\r
277 transfer.multiBlock = 1;
\r
279 // uint32_t sdLBA =
\r
281 // scsiDev.target->cfg->sdSectorStart,
\r
285 // TODO sdReadMultiSectorPrep(sdLBA, sdSectors);
\r
290 static void doSeek(uint32_t lba)
\r
294 scsiDev.target->cfg->sdSectorStart,
\r
295 scsiDev.target->liveCfg.bytesPerSector,
\r
296 scsiDev.target->cfg->scsiSectors)
\r
299 scsiDev.status = CHECK_CONDITION;
\r
300 scsiDev.target->sense.code = ILLEGAL_REQUEST;
\r
301 scsiDev.target->sense.asc = LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
\r
302 scsiDev.phase = STATUS;
\r
310 static int doTestUnitReady()
\r
313 if (likely(blockDev.state == (DISK_STARTED | DISK_PRESENT | DISK_INITIALISED)))
\r
317 else if (unlikely(!(blockDev.state & DISK_STARTED)))
\r
320 scsiDev.status = CHECK_CONDITION;
\r
321 scsiDev.target->sense.code = NOT_READY;
\r
322 scsiDev.target->sense.asc = LOGICAL_UNIT_NOT_READY_INITIALIZING_COMMAND_REQUIRED;
\r
323 scsiDev.phase = STATUS;
\r
325 else if (unlikely(!(blockDev.state & DISK_PRESENT)))
\r
328 scsiDev.status = CHECK_CONDITION;
\r
329 scsiDev.target->sense.code = NOT_READY;
\r
330 scsiDev.target->sense.asc = MEDIUM_NOT_PRESENT;
\r
331 scsiDev.phase = STATUS;
\r
333 else if (unlikely(!(blockDev.state & DISK_INITIALISED)))
\r
336 scsiDev.status = CHECK_CONDITION;
\r
337 scsiDev.target->sense.code = NOT_READY;
\r
338 scsiDev.target->sense.asc = LOGICAL_UNIT_NOT_READY_CAUSE_NOT_REPORTABLE;
\r
339 scsiDev.phase = STATUS;
\r
344 // Handle direct-access scsi device commands
\r
345 int scsiDiskCommand()
\r
347 int commandHandled = 1;
\r
349 uint8_t command = scsiDev.cdb[0];
\r
350 if (unlikely(command == 0x1B))
\r
353 // Enable or disable media access operations.
\r
354 // Ignore load/eject requests. We can't do that.
\r
355 //int immed = scsiDev.cdb[1] & 1;
\r
356 int start = scsiDev.cdb[4] & 1;
\r
360 blockDev.state = blockDev.state | DISK_STARTED;
\r
361 if (!(blockDev.state & DISK_INITIALISED))
\r
368 blockDev.state &= ~DISK_STARTED;
\r
371 else if (unlikely(command == 0x00))
\r
376 else if (unlikely(!doTestUnitReady()))
\r
378 // Status and sense codes already set by doTestUnitReady
\r
380 else if (likely(command == 0x08))
\r
384 (((uint32_t) scsiDev.cdb[1] & 0x1F) << 16) +
\r
385 (((uint32_t) scsiDev.cdb[2]) << 8) +
\r
387 uint32_t blocks = scsiDev.cdb[4];
\r
388 if (unlikely(blocks == 0)) blocks = 256;
\r
389 doRead(lba, blocks);
\r
391 else if (likely(command == 0x28))
\r
394 // Ignore all cache control bits - we don't support a memory cache.
\r
397 (((uint32_t) scsiDev.cdb[2]) << 24) +
\r
398 (((uint32_t) scsiDev.cdb[3]) << 16) +
\r
399 (((uint32_t) scsiDev.cdb[4]) << 8) +
\r
402 (((uint32_t) scsiDev.cdb[7]) << 8) +
\r
405 doRead(lba, blocks);
\r
407 else if (likely(command == 0x0A))
\r
411 (((uint32_t) scsiDev.cdb[1] & 0x1F) << 16) +
\r
412 (((uint32_t) scsiDev.cdb[2]) << 8) +
\r
414 uint32_t blocks = scsiDev.cdb[4];
\r
415 if (unlikely(blocks == 0)) blocks = 256;
\r
416 doWrite(lba, blocks);
\r
418 else if (likely(command == 0x2A) || // WRITE(10)
\r
419 unlikely(command == 0x2E)) // WRITE AND VERIFY
\r
421 // Ignore all cache control bits - we don't support a memory cache.
\r
422 // Don't bother verifying either. The SD card likely stores ECC
\r
423 // along with each flash row.
\r
426 (((uint32_t) scsiDev.cdb[2]) << 24) +
\r
427 (((uint32_t) scsiDev.cdb[3]) << 16) +
\r
428 (((uint32_t) scsiDev.cdb[4]) << 8) +
\r
431 (((uint32_t) scsiDev.cdb[7]) << 8) +
\r
434 doWrite(lba, blocks);
\r
436 else if (unlikely(command == 0x04))
\r
439 // We don't really do any formatting, but we need to read the correct
\r
440 // number of bytes in the DATA_OUT phase to make the SCSI host happy.
\r
442 int fmtData = (scsiDev.cdb[1] & 0x10) ? 1 : 0;
\r
445 // We need to read the parameter list, but we don't know how
\r
446 // big it is yet. Start with the header.
\r
447 scsiDev.dataLen = 4;
\r
448 scsiDev.phase = DATA_OUT;
\r
449 scsiDev.postDataOutHook = doFormatUnitHeader;
\r
453 // No data to read, we're already finished!
\r
456 else if (unlikely(command == 0x25))
\r
461 else if (unlikely(command == 0x0B))
\r
465 (((uint32_t) scsiDev.cdb[1] & 0x1F) << 16) +
\r
466 (((uint32_t) scsiDev.cdb[2]) << 8) +
\r
472 else if (unlikely(command == 0x2B))
\r
476 (((uint32_t) scsiDev.cdb[2]) << 24) +
\r
477 (((uint32_t) scsiDev.cdb[3]) << 16) +
\r
478 (((uint32_t) scsiDev.cdb[4]) << 8) +
\r
483 else if (unlikely(command == 0x36))
\r
485 // LOCK UNLOCK CACHE
\r
486 // We don't have a cache to lock data into. do nothing.
\r
488 else if (unlikely(command == 0x34))
\r
491 // We don't have a cache to pre-fetch into. do nothing.
\r
493 else if (unlikely(command == 0x1E))
\r
495 // PREVENT ALLOW MEDIUM REMOVAL
\r
496 // Not much we can do to prevent the user removing the SD card.
\r
499 else if (unlikely(command == 0x01))
\r
502 // Set the lun to a vendor-specific state. Ignore.
\r
504 else if (unlikely(command == 0x35))
\r
506 // SYNCHRONIZE CACHE
\r
507 // We don't have a cache. do nothing.
\r
509 else if (unlikely(command == 0x2F))
\r
512 // TODO: When they supply data to verify, we should read the data and
\r
513 // verify it. If they don't supply any data, just say success.
\r
514 if ((scsiDev.cdb[1] & 0x02) == 0)
\r
516 // They are asking us to do a medium verification with no data
\r
517 // comparison. Assume success, do nothing.
\r
521 // TODO. This means they are supplying data to verify against.
\r
522 // Technically we should probably grab the data and compare it.
\r
523 scsiDev.status = CHECK_CONDITION;
\r
524 scsiDev.target->sense.code = ILLEGAL_REQUEST;
\r
525 scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;
\r
526 scsiDev.phase = STATUS;
\r
529 else if (unlikely(command == 0x37))
\r
531 // READ DEFECT DATA
\r
532 uint32_t allocLength = (((uint16_t)scsiDev.cdb[7]) << 8) |
\r
535 scsiDev.data[0] = 0;
\r
536 scsiDev.data[1] = scsiDev.cdb[1];
\r
537 scsiDev.data[2] = 0;
\r
538 scsiDev.data[3] = 0;
\r
539 scsiDev.dataLen = 4;
\r
541 if (scsiDev.dataLen > allocLength)
\r
543 scsiDev.dataLen = allocLength;
\r
546 scsiDev.phase = DATA_IN;
\r
550 commandHandled = 0;
\r
553 return commandHandled;
\r
556 void scsiDiskPoll()
\r
558 uint32_t bytesPerSector = scsiDev.target->liveCfg.bytesPerSector;
\r
560 if (scsiDev.phase == DATA_IN &&
\r
561 transfer.currentBlock != transfer.blocks)
\r
563 // Take responsibility for waiting for the phase delays
\r
564 uint32_t phaseChangeDelayUs = scsiEnterPhaseImmediate(DATA_IN);
\r
566 int totalSDSectors =
\r
567 transfer.blocks * SDSectorsPerSCSISector(bytesPerSector);
\r
570 scsiDev.target->cfg->sdSectorStart,
\r
574 const int sdPerScsi = SDSectorsPerSCSISector(bytesPerSector);
\r
575 const int buffers = sizeof(scsiDev.data) / SD_SECTOR_SIZE;
\r
578 int scsiActive __attribute__((unused)) = 0; // unused if DMA disabled
\r
581 // It's highly unlikely that someone is going to use huge transfers
\r
582 // per scsi command, but if they do it'll be slower than usual.
\r
583 uint32_t totalScsiBytes = transfer.blocks * bytesPerSector;
\r
584 int useSlowDataCount = totalScsiBytes >= SCSI_XFER_MAX;
\r
585 if (!useSlowDataCount)
\r
587 scsiSetDataCount(totalScsiBytes);
\r
590 while ((i < totalSDSectors) &&
\r
591 likely(scsiDev.phase == DATA_IN) &&
\r
592 likely(!scsiDev.resetFlag))
\r
594 int completedDmaSectors;
\r
595 if (sdActive && (completedDmaSectors = sdReadDMAPoll(sdActive)))
\r
597 prep += completedDmaSectors;
\r
598 sdActive -= completedDmaSectors;
\r
599 } else if (sdActive > 1)
\r
601 if ((scsiDev.data[SD_SECTOR_SIZE * (prep % buffers) + 510] != 0xAA) ||
\r
602 (scsiDev.data[SD_SECTOR_SIZE * (prep % buffers) + 511] != 0x33))
\r
610 (prep - i < buffers) &&
\r
611 (prep < totalSDSectors) &&
\r
612 ((totalSDSectors - prep) >= sdPerScsi) &&
\r
613 (likely(!useSlowDataCount) || scsiPhyComplete()) &&
\r
614 (HAL_SD_GetState(&hsd) != HAL_SD_STATE_BUSY)) // rx complete but IRQ not fired yet.
\r
616 // Start an SD transfer if we have space.
\r
617 uint32_t startBuffer = prep % buffers;
\r
618 uint32_t sectors = totalSDSectors - prep;
\r
619 uint32_t freeBuffers = buffers - (prep - i);
\r
621 uint32_t contiguousBuffers = buffers - startBuffer;
\r
622 freeBuffers = freeBuffers < contiguousBuffers
\r
623 ? freeBuffers : contiguousBuffers;
\r
624 sectors = sectors < freeBuffers ? sectors : freeBuffers;
\r
626 if (sectors > 128) sectors = 128; // 65536 DMA limit !!
\r
628 // Round-down when we have odd sector sizes.
\r
629 if (sdPerScsi != 1)
\r
631 sectors = (sectors / sdPerScsi) * sdPerScsi;
\r
634 for (int dodgy = 0; dodgy < sectors; dodgy++)
\r
636 scsiDev.data[SD_SECTOR_SIZE * (startBuffer + dodgy) + 510] = 0xAA;
\r
637 scsiDev.data[SD_SECTOR_SIZE * (startBuffer + dodgy) + 511] = 0x33;
\r
640 sdReadDMA(sdLBA + prep, sectors, &scsiDev.data[SD_SECTOR_SIZE * startBuffer]);
\r
642 sdActive = sectors;
\r
644 if (useSlowDataCount)
\r
646 scsiSetDataCount((sectors / sdPerScsi) * bytesPerSector);
\r
649 // Wait now that the SD card is busy
\r
650 // Chances are we've probably already waited sufficient time,
\r
651 // but it's hard to measure microseconds cheaply. So just wait
\r
652 // extra just-in-case. Hopefully it's in parallel with dma.
\r
653 if (phaseChangeDelayUs > 0)
\r
655 s2s_delay_us(phaseChangeDelayUs);
\r
656 phaseChangeDelayUs = 0;
\r
660 if (((prep - i) > 0) &&
\r
663 int dmaBytes = SD_SECTOR_SIZE;
\r
664 if ((i % sdPerScsi) == (sdPerScsi - 1))
\r
666 dmaBytes = bytesPerSector % SD_SECTOR_SIZE;
\r
667 if (dmaBytes == 0) dmaBytes = SD_SECTOR_SIZE;
\r
670 uint8_t* scsiDmaData = &(scsiDev.data[SD_SECTOR_SIZE * (i % buffers)]);
\r
671 scsiWritePIO(scsiDmaData, dmaBytes);
\r
677 if (phaseChangeDelayUs > 0 && !scsiDev.resetFlag) // zero bytes ?
\r
679 s2s_delay_us(phaseChangeDelayUs);
\r
680 phaseChangeDelayUs = 0;
\r
683 if (scsiDev.resetFlag)
\r
685 HAL_SD_Abort(&hsd);
\r
689 // Wait for the SD transfer to complete before we disable IRQs.
\r
690 // (Otherwise some cards will cause an error if we don't sent the
\r
691 // stop transfer command via the DMA complete handler in time)
\r
692 while (HAL_SD_GetState(&hsd) == HAL_SD_STATE_BUSY)
\r
694 // Wait while keeping BSY.
\r
698 HAL_SD_CardStateTypeDef cardState = HAL_SD_GetCardState(&hsd);
\r
699 while (cardState == HAL_SD_CARD_PROGRAMMING || cardState == HAL_SD_CARD_SENDING)
\r
701 cardState = HAL_SD_GetCardState(&hsd);
\r
704 // We've finished transferring the data to the FPGA, now wait until it's
\r
705 // written to he SCSI bus.
\r
706 while (!scsiPhyComplete() &&
\r
707 likely(scsiDev.phase == DATA_IN) &&
\r
708 likely(!scsiDev.resetFlag))
\r
711 if (!scsiPhyComplete() && likely(!scsiDev.resetFlag))
\r
718 if (scsiDev.phase == DATA_IN)
\r
720 scsiDev.phase = STATUS;
\r
724 else if (scsiDev.phase == DATA_OUT &&
\r
725 transfer.currentBlock != transfer.blocks)
\r
727 scsiEnterPhase(DATA_OUT);
\r
729 const int sdPerScsi = SDSectorsPerSCSISector(bytesPerSector);
\r
730 int totalSDSectors = transfer.blocks * sdPerScsi;
\r
733 scsiDev.target->cfg->sdSectorStart,
\r
738 int disconnected = 0;
\r
740 int parityError = 0;
\r
741 int enableParity = scsiDev.boardCfg.flags & S2S_CFG_ENABLE_PARITY;
\r
743 uint32_t maxSectors = sizeof(scsiDev.data) / SD_SECTOR_SIZE;
\r
745 static_assert(SCSI_XFER_MAX >= sizeof(scsiDev.data), "Assumes SCSI_XFER_MAX >= sizeof(scsiDev.data)");
\r
747 // Start reading and filling fifos as soon as possible.
\r
748 // It's highly unlikely that someone is going to use huge transfers
\r
749 // per scsi command, but if they do it'll be slower than usual.
\r
750 // Note: Happens in Macintosh FWB HDD Toolkit benchmarks which default
\r
752 uint32_t totalTransferBytes = transfer.blocks * bytesPerSector;
\r
753 int useSlowDataCount = totalTransferBytes >= SCSI_XFER_MAX;
\r
754 if (!useSlowDataCount)
\r
756 DWT->CYCCNT = 0; // Start counting cycles
\r
757 scsiSetDataCount(totalTransferBytes);
\r
760 int lastWriteSize = 0;
\r
762 while ((i < totalSDSectors) &&
\r
763 likely(scsiDev.phase == DATA_OUT) &&
\r
764 likely(!scsiDev.resetFlag))
\r
765 // KEEP GOING to ensure FIFOs are in a good state.
\r
766 // likely(!parityError || !enableParity))
\r
768 if (bytesPerSector == SD_SECTOR_SIZE)
\r
770 uint32_t maxXferSectors = SCSI_XFER_MAX / SD_SECTOR_SIZE;
\r
771 uint32_t rem = totalSDSectors - i;
\r
772 uint32_t sectors = rem < maxXferSectors ? rem : maxXferSectors;
\r
774 uint32_t totalBytes = sectors * SD_SECTOR_SIZE;
\r
776 if (useSlowDataCount)
\r
778 scsiSetDataCount(totalBytes);
\r
781 lastWriteSize = sectors;
\r
782 HAL_SD_WriteBlocks_DMA(&hsd, i + sdLBA, sectors);
\r
786 uint32_t dmaFinishTime = 0;
\r
787 while (j < sectors && !scsiDev.resetFlag)
\r
790 HAL_SD_GetState(&hsd) != HAL_SD_STATE_BUSY &&
\r
796 if (!sdActive && ((prep - j) > 0))
\r
798 // Start an SD transfer if we have space.
\r
799 HAL_SD_WriteBlocks_Data(&hsd, &scsiDev.data[SD_SECTOR_SIZE * (j % maxSectors)]);
\r
804 if (((prep - j) < maxSectors) &&
\r
805 (prep < sectors) &&
\r
809 &scsiDev.data[(prep % maxSectors) * SD_SECTOR_SIZE],
\r
813 if (prep == sectors)
\r
815 dmaFinishTime = s2s_getTime_ms();
\r
819 if (i + prep >= totalSDSectors &&
\r
821 (!parityError || !enableParity) &&
\r
822 s2s_elapsedTime_ms(dmaFinishTime) >= 180)
\r
824 // We're transferring over the SCSI bus faster than the SD card
\r
825 // can write. All data is buffered, and we're just waiting for
\r
826 // the SD card to complete. The host won't let us disconnect.
\r
827 // Some drivers set a 250ms timeout on transfers to complete.
\r
828 // SD card writes are supposed to complete
\r
829 // within 200ms, but sometimes they don't.
\r
830 // Just pretend we're finished.
\r
832 clearBSY = process_MessageIn(0); // Will go to BUS_FREE state but keep BSY asserted.
\r
837 if (scsiDev.resetFlag)
\r
839 HAL_SD_Abort(&hsd);
\r
843 while (HAL_SD_GetState(&hsd) == HAL_SD_STATE_BUSY) {} // Waits for DMA to complete
\r
844 if (lastWriteSize > 1)
\r
846 SDMMC_CmdStopTransfer(hsd.Instance);
\r
850 while (sdIsBusy() &&
\r
851 s2s_elapsedTime_ms(dmaFinishTime) < 180)
\r
853 // Wait while the SD card is writing buffer to flash
\r
854 // The card may remain in the RECEIVING state (even though it's programming) if
\r
855 // it has buffer space to receive more data available.
\r
858 if (!disconnected &&
\r
859 i + sectors >= totalSDSectors &&
\r
860 (!parityError || !enableParity))
\r
862 // We're transferring over the SCSI bus faster than the SD card
\r
863 // can write. All data is buffered, and we're just waiting for
\r
864 // the SD card to complete. The host won't let us disconnect.
\r
865 // Some drivers set a 250ms timeout on transfers to complete.
\r
866 // SD card writes are supposed to complete
\r
867 // within 200ms, but sometimes they don't.
\r
868 // Just pretend we're finished.
\r
870 clearBSY = process_MessageIn(0); // Will go to BUS_FREE state but keep BSY asserted.
\r
873 // Wait while the SD card is writing buffer to flash
\r
874 // The card may remain in the RECEIVING state (even though it's programming) if
\r
875 // it has buffer space to receive more data available.
\r
876 while (sdIsBusy()) {}
\r
877 HAL_SD_CardStateTypeDef cardState = HAL_SD_GetCardState(&hsd);
\r
878 while (cardState == HAL_SD_CARD_PROGRAMMING || cardState == HAL_SD_CARD_RECEIVING)
\r
880 // Wait while the SD card is writing buffer to flash
\r
881 // The card may remain in the RECEIVING state (even though it's programming) if
\r
882 // it has buffer space to receive more data available.
\r
883 cardState = HAL_SD_GetCardState(&hsd);
\r
890 // Well, until we have some proper non-blocking SD code, we must
\r
891 // do this in a half-duplex fashion. We need to write as much as
\r
892 // possible in each SD card transaction.
\r
893 // use sg_dd from sg_utils3 tools to test.
\r
895 uint32_t rem = totalSDSectors - i;
\r
896 uint32_t sectors = rem < maxSectors ? rem : maxSectors;
\r
898 if (useSlowDataCount)
\r
900 scsiSetDataCount(sectors * bytesPerSector);
\r
903 for (int scsiSector = i; scsiSector < i + sectors; ++scsiSector)
\r
905 int dmaBytes = SD_SECTOR_SIZE;
\r
906 if ((scsiSector % sdPerScsi) == (sdPerScsi - 1))
\r
908 dmaBytes = bytesPerSector % SD_SECTOR_SIZE;
\r
909 if (dmaBytes == 0) dmaBytes = SD_SECTOR_SIZE;
\r
912 scsiReadPIO(&scsiDev.data[SD_SECTOR_SIZE * (scsiSector - i)], dmaBytes, &parityError);
\r
914 if (!parityError || !enableParity)
\r
916 BSP_SD_WriteBlocks_DMA(&scsiDev.data[0], i + sdLBA, sectors);
\r
922 // Should already be complete here as we've ready the FIFOs
\r
923 // by now. Check anyway.
\r
925 while (!scsiPhyComplete() && likely(!scsiDev.resetFlag))
\r
936 if (scsiDev.phase == DATA_OUT)
\r
939 (scsiDev.boardCfg.flags & S2S_CFG_ENABLE_PARITY))
\r
941 scsiDev.target->sense.code = ABORTED_COMMAND;
\r
942 scsiDev.target->sense.asc = SCSI_PARITY_ERROR;
\r
943 scsiDev.status = CHECK_CONDITION;;
\r
945 scsiDev.phase = STATUS;
\r
951 void scsiDiskReset()
\r
953 scsiDev.dataPtr = 0;
\r
954 scsiDev.savedDataPtr = 0;
\r
955 scsiDev.dataLen = 0;
\r
956 // transfer.lba = 0; // Needed in Request Sense to determine failure
\r
957 transfer.blocks = 0;
\r
958 transfer.currentBlock = 0;
\r
960 // Cancel long running commands!
\r
963 ((scsiDev.boardCfg.flags & S2S_CFG_ENABLE_CACHE) == 0) ||
\r
964 (transfer.multiBlock == 0)
\r
968 sdCompleteTransfer();
\r
971 transfer.multiBlock = 0;
\r
974 void scsiDiskInit()
\r
978 // Don't require the host to send us a START STOP UNIT command
\r
979 blockDev.state = DISK_STARTED;
\r