disk.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  1. // Copyright (C) 2013 Michael McMaster <michael@codesrc.com>
  2. // Copyright (C) 2014 Doug Brown <doug@downtowndougbrown.com>
  3. //
  4. // This file is part of SCSI2SD.
  5. //
  6. // SCSI2SD is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // SCSI2SD is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU General Public License
  17. // along with SCSI2SD. If not, see <http://www.gnu.org/licenses/>.
  18. #include "scsi.h"
  19. #include "scsiPhy.h"
  20. #include "config.h"
  21. #include "debug.h"
  22. #include "disk.h"
  23. #include "sd.h"
  24. #include "time.h"
  25. #include <string.h>
  26. // Global
  27. BlockDevice blockDev;
  28. Transfer transfer;
  29. static int doSdInit()
  30. {
  31. int result = 0;
  32. if (blockDev.state & DISK_PRESENT)
  33. {
  34. blockDev.state = blockDev.state | DISK_INITIALISED;
  35. }
  36. return result;
  37. }
  38. // Callback once all data has been read in the data out phase.
  39. static void doFormatUnitComplete(void)
  40. {
  41. // TODO start writing the initialisation pattern to the SD
  42. // card
  43. scsiDev.phase = STATUS;
  44. }
  45. static void doFormatUnitSkipData(int bytes)
  46. {
  47. // We may not have enough memory to store the initialisation pattern and
  48. // defect list data. Since we're not making use of it yet anyway, just
  49. // discard the bytes.
  50. scsiEnterPhase(DATA_OUT);
  51. int i;
  52. for (i = 0; i < bytes; ++i)
  53. {
  54. scsiReadByte();
  55. }
  56. }
  57. // Callback from the data out phase.
  58. static void doFormatUnitPatternHeader(void)
  59. {
  60. int defectLength =
  61. ((((uint16_t)scsiDev.data[2])) << 8) +
  62. scsiDev.data[3];
  63. int patternLength =
  64. ((((uint16_t)scsiDev.data[4 + 2])) << 8) +
  65. scsiDev.data[4 + 3];
  66. doFormatUnitSkipData(defectLength + patternLength);
  67. doFormatUnitComplete();
  68. }
  69. // Callback from the data out phase.
  70. static void doFormatUnitHeader(void)
  71. {
  72. int IP = (scsiDev.data[1] & 0x08) ? 1 : 0;
  73. int DSP = (scsiDev.data[1] & 0x04) ? 1 : 0;
  74. if (! DSP) // disable save parameters
  75. {
  76. // Save the "MODE SELECT savable parameters"
  77. s2s_configSave(
  78. scsiDev.target->targetId,
  79. scsiDev.target->liveCfg.bytesPerSector);
  80. }
  81. if (IP)
  82. {
  83. // We need to read the initialisation pattern header first.
  84. scsiDev.dataLen += 4;
  85. scsiDev.phase = DATA_OUT;
  86. scsiDev.postDataOutHook = doFormatUnitPatternHeader;
  87. }
  88. else
  89. {
  90. // Read the defect list data
  91. int defectLength =
  92. ((((uint16_t)scsiDev.data[2])) << 8) +
  93. scsiDev.data[3];
  94. doFormatUnitSkipData(defectLength);
  95. doFormatUnitComplete();
  96. }
  97. }
  98. static void doReadCapacity()
  99. {
  100. uint32_t lba = (((uint32_t) scsiDev.cdb[2]) << 24) +
  101. (((uint32_t) scsiDev.cdb[3]) << 16) +
  102. (((uint32_t) scsiDev.cdb[4]) << 8) +
  103. scsiDev.cdb[5];
  104. int pmi = scsiDev.cdb[8] & 1;
  105. uint32_t capacity = getScsiCapacity(
  106. scsiDev.target->cfg->sdSectorStart,
  107. scsiDev.target->liveCfg.bytesPerSector,
  108. scsiDev.target->cfg->scsiSectors);
  109. if (!pmi && lba)
  110. {
  111. // error.
  112. // We don't do anything with the "partial medium indicator", and
  113. // assume that delays are constant across each block. But the spec
  114. // says we must return this error if pmi is specified incorrectly.
  115. scsiDev.status = CHECK_CONDITION;
  116. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  117. scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;
  118. scsiDev.phase = STATUS;
  119. }
  120. else if (capacity > 0)
  121. {
  122. uint32_t highestBlock = capacity - 1;
  123. scsiDev.data[0] = highestBlock >> 24;
  124. scsiDev.data[1] = highestBlock >> 16;
  125. scsiDev.data[2] = highestBlock >> 8;
  126. scsiDev.data[3] = highestBlock;
  127. uint32_t bytesPerSector = scsiDev.target->liveCfg.bytesPerSector;
  128. scsiDev.data[4] = bytesPerSector >> 24;
  129. scsiDev.data[5] = bytesPerSector >> 16;
  130. scsiDev.data[6] = bytesPerSector >> 8;
  131. scsiDev.data[7] = bytesPerSector;
  132. scsiDev.dataLen = 8;
  133. scsiDev.phase = DATA_IN;
  134. }
  135. else
  136. {
  137. scsiDev.status = CHECK_CONDITION;
  138. scsiDev.target->sense.code = NOT_READY;
  139. scsiDev.target->sense.asc = MEDIUM_NOT_PRESENT;
  140. scsiDev.phase = STATUS;
  141. }
  142. }
  143. static void doWrite(uint32_t lba, uint32_t blocks)
  144. {
  145. if (unlikely(scsiDev.target->cfg->deviceType == S2S_CFG_FLOPPY_14MB)) {
  146. // Floppies are supposed to be slow. Some systems can't handle a floppy
  147. // without an access time
  148. s2s_delay_ms(10);
  149. }
  150. uint32_t bytesPerSector = scsiDev.target->liveCfg.bytesPerSector;
  151. if (unlikely(blockDev.state & DISK_WP) ||
  152. unlikely(scsiDev.target->cfg->deviceType == S2S_CFG_OPTICAL))
  153. {
  154. scsiDev.status = CHECK_CONDITION;
  155. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  156. scsiDev.target->sense.asc = WRITE_PROTECTED;
  157. scsiDev.phase = STATUS;
  158. }
  159. else if (unlikely(((uint64_t) lba) + blocks >
  160. getScsiCapacity(
  161. scsiDev.target->cfg->sdSectorStart,
  162. bytesPerSector,
  163. scsiDev.target->cfg->scsiSectors
  164. )
  165. ))
  166. {
  167. scsiDev.status = CHECK_CONDITION;
  168. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  169. scsiDev.target->sense.asc = LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  170. scsiDev.phase = STATUS;
  171. }
  172. else
  173. {
  174. transfer.lba = lba;
  175. transfer.blocks = blocks;
  176. transfer.currentBlock = 0;
  177. scsiDev.phase = DATA_OUT;
  178. scsiDev.dataLen = bytesPerSector;
  179. scsiDev.dataPtr = bytesPerSector;
  180. // No need for single-block writes atm. Overhead of the
  181. // multi-block write is minimal.
  182. transfer.multiBlock = 1;
  183. // TODO uint32_t sdLBA =
  184. // TODO SCSISector2SD(
  185. // TODO scsiDev.target->cfg->sdSectorStart,
  186. // TODO bytesPerSector,
  187. // TODO lba);
  188. // TODO uint32_t sdBlocks = blocks * SDSectorsPerSCSISector(bytesPerSector);
  189. // TODO sdWriteMultiSectorPrep(sdLBA, sdBlocks);
  190. }
  191. }
  192. static void doRead(uint32_t lba, uint32_t blocks)
  193. {
  194. if (unlikely(scsiDev.target->cfg->deviceType == S2S_CFG_FLOPPY_14MB)) {
  195. // Floppies are supposed to be slow. Some systems can't handle a floppy
  196. // without an access time
  197. s2s_delay_ms(10);
  198. }
  199. uint32_t capacity = getScsiCapacity(
  200. scsiDev.target->cfg->sdSectorStart,
  201. scsiDev.target->liveCfg.bytesPerSector,
  202. scsiDev.target->cfg->scsiSectors);
  203. if (unlikely(((uint64_t) lba) + blocks > capacity))
  204. {
  205. scsiDev.status = CHECK_CONDITION;
  206. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  207. scsiDev.target->sense.asc = LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  208. scsiDev.phase = STATUS;
  209. }
  210. else
  211. {
  212. transfer.lba = lba;
  213. transfer.blocks = blocks;
  214. transfer.currentBlock = 0;
  215. scsiDev.phase = DATA_IN;
  216. scsiDev.dataLen = 0; // No data yet
  217. uint32_t bytesPerSector = scsiDev.target->liveCfg.bytesPerSector;
  218. uint32_t sdSectorPerSCSISector = SDSectorsPerSCSISector(bytesPerSector);
  219. uint32_t sdSectors =
  220. blocks * sdSectorPerSCSISector;
  221. if ((
  222. (sdSectors == 1) &&
  223. !(scsiDev.boardCfg.flags & S2S_CFG_ENABLE_CACHE)
  224. ) ||
  225. unlikely(((uint64_t) lba) + blocks == capacity)
  226. )
  227. {
  228. // We get errors on reading the last sector using a multi-sector
  229. // read :-(
  230. transfer.multiBlock = 0;
  231. }
  232. else
  233. {
  234. transfer.multiBlock = 1;
  235. // uint32_t sdLBA =
  236. // SCSISector2SD(
  237. // scsiDev.target->cfg->sdSectorStart,
  238. // bytesPerSector,
  239. // lba);
  240. // TODO sdReadMultiSectorPrep(sdLBA, sdSectors);
  241. }
  242. }
  243. }
  244. static void doSeek(uint32_t lba)
  245. {
  246. if (lba >=
  247. getScsiCapacity(
  248. scsiDev.target->cfg->sdSectorStart,
  249. scsiDev.target->liveCfg.bytesPerSector,
  250. scsiDev.target->cfg->scsiSectors)
  251. )
  252. {
  253. scsiDev.status = CHECK_CONDITION;
  254. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  255. scsiDev.target->sense.asc = LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE;
  256. scsiDev.phase = STATUS;
  257. }
  258. }
  259. static int doTestUnitReady()
  260. {
  261. int ready = 1;
  262. if (likely(blockDev.state == (DISK_STARTED | DISK_PRESENT | DISK_INITIALISED)))
  263. {
  264. // nothing to do.
  265. }
  266. else if (unlikely(!(blockDev.state & DISK_STARTED)))
  267. {
  268. ready = 0;
  269. scsiDev.status = CHECK_CONDITION;
  270. scsiDev.target->sense.code = NOT_READY;
  271. scsiDev.target->sense.asc = LOGICAL_UNIT_NOT_READY_INITIALIZING_COMMAND_REQUIRED;
  272. scsiDev.phase = STATUS;
  273. }
  274. else if (unlikely(!(blockDev.state & DISK_PRESENT)))
  275. {
  276. ready = 0;
  277. scsiDev.status = CHECK_CONDITION;
  278. scsiDev.target->sense.code = NOT_READY;
  279. scsiDev.target->sense.asc = MEDIUM_NOT_PRESENT;
  280. scsiDev.phase = STATUS;
  281. }
  282. else if (unlikely(!(blockDev.state & DISK_INITIALISED)))
  283. {
  284. ready = 0;
  285. scsiDev.status = CHECK_CONDITION;
  286. scsiDev.target->sense.code = NOT_READY;
  287. scsiDev.target->sense.asc = LOGICAL_UNIT_NOT_READY_CAUSE_NOT_REPORTABLE;
  288. scsiDev.phase = STATUS;
  289. }
  290. return ready;
  291. }
  292. // Handle direct-access scsi device commands
  293. int scsiDiskCommand()
  294. {
  295. int commandHandled = 1;
  296. uint8_t command = scsiDev.cdb[0];
  297. if (unlikely(command == 0x1B))
  298. {
  299. // START STOP UNIT
  300. // Enable or disable media access operations.
  301. // Ignore load/eject requests. We can't do that.
  302. //int immed = scsiDev.cdb[1] & 1;
  303. int start = scsiDev.cdb[4] & 1;
  304. if (start)
  305. {
  306. blockDev.state = blockDev.state | DISK_STARTED;
  307. if (!(blockDev.state & DISK_INITIALISED))
  308. {
  309. doSdInit();
  310. }
  311. }
  312. else
  313. {
  314. blockDev.state &= ~DISK_STARTED;
  315. }
  316. }
  317. else if (unlikely(command == 0x00))
  318. {
  319. // TEST UNIT READY
  320. doTestUnitReady();
  321. }
  322. else if (unlikely(!doTestUnitReady()))
  323. {
  324. // Status and sense codes already set by doTestUnitReady
  325. }
  326. else if (likely(command == 0x08))
  327. {
  328. // READ(6)
  329. uint32_t lba =
  330. (((uint32_t) scsiDev.cdb[1] & 0x1F) << 16) +
  331. (((uint32_t) scsiDev.cdb[2]) << 8) +
  332. scsiDev.cdb[3];
  333. uint32_t blocks = scsiDev.cdb[4];
  334. if (unlikely(blocks == 0)) blocks = 256;
  335. doRead(lba, blocks);
  336. }
  337. else if (likely(command == 0x28))
  338. {
  339. // READ(10)
  340. // Ignore all cache control bits - we don't support a memory cache.
  341. uint32_t lba =
  342. (((uint32_t) scsiDev.cdb[2]) << 24) +
  343. (((uint32_t) scsiDev.cdb[3]) << 16) +
  344. (((uint32_t) scsiDev.cdb[4]) << 8) +
  345. scsiDev.cdb[5];
  346. uint32_t blocks =
  347. (((uint32_t) scsiDev.cdb[7]) << 8) +
  348. scsiDev.cdb[8];
  349. doRead(lba, blocks);
  350. }
  351. else if (likely(command == 0x0A))
  352. {
  353. // WRITE(6)
  354. uint32_t lba =
  355. (((uint32_t) scsiDev.cdb[1] & 0x1F) << 16) +
  356. (((uint32_t) scsiDev.cdb[2]) << 8) +
  357. scsiDev.cdb[3];
  358. uint32_t blocks = scsiDev.cdb[4];
  359. if (unlikely(blocks == 0)) blocks = 256;
  360. doWrite(lba, blocks);
  361. }
  362. else if (likely(command == 0x2A) || // WRITE(10)
  363. unlikely(command == 0x2E)) // WRITE AND VERIFY
  364. {
  365. // Ignore all cache control bits - we don't support a memory cache.
  366. // Don't bother verifying either. The SD card likely stores ECC
  367. // along with each flash row.
  368. uint32_t lba =
  369. (((uint32_t) scsiDev.cdb[2]) << 24) +
  370. (((uint32_t) scsiDev.cdb[3]) << 16) +
  371. (((uint32_t) scsiDev.cdb[4]) << 8) +
  372. scsiDev.cdb[5];
  373. uint32_t blocks =
  374. (((uint32_t) scsiDev.cdb[7]) << 8) +
  375. scsiDev.cdb[8];
  376. doWrite(lba, blocks);
  377. }
  378. else if (unlikely(command == 0x04))
  379. {
  380. // FORMAT UNIT
  381. // We don't really do any formatting, but we need to read the correct
  382. // number of bytes in the DATA_OUT phase to make the SCSI host happy.
  383. int fmtData = (scsiDev.cdb[1] & 0x10) ? 1 : 0;
  384. if (fmtData)
  385. {
  386. // We need to read the parameter list, but we don't know how
  387. // big it is yet. Start with the header.
  388. scsiDev.dataLen = 4;
  389. scsiDev.phase = DATA_OUT;
  390. scsiDev.postDataOutHook = doFormatUnitHeader;
  391. }
  392. else
  393. {
  394. // No data to read, we're already finished!
  395. }
  396. }
  397. else if (unlikely(command == 0x25))
  398. {
  399. // READ CAPACITY
  400. doReadCapacity();
  401. }
  402. else if (unlikely(command == 0x0B))
  403. {
  404. // SEEK(6)
  405. uint32_t lba =
  406. (((uint32_t) scsiDev.cdb[1] & 0x1F) << 16) +
  407. (((uint32_t) scsiDev.cdb[2]) << 8) +
  408. scsiDev.cdb[3];
  409. doSeek(lba);
  410. }
  411. else if (unlikely(command == 0x2B))
  412. {
  413. // SEEK(10)
  414. uint32_t lba =
  415. (((uint32_t) scsiDev.cdb[2]) << 24) +
  416. (((uint32_t) scsiDev.cdb[3]) << 16) +
  417. (((uint32_t) scsiDev.cdb[4]) << 8) +
  418. scsiDev.cdb[5];
  419. doSeek(lba);
  420. }
  421. else if (unlikely(command == 0x36))
  422. {
  423. // LOCK UNLOCK CACHE
  424. // We don't have a cache to lock data into. do nothing.
  425. }
  426. else if (unlikely(command == 0x34))
  427. {
  428. // PRE-FETCH.
  429. // We don't have a cache to pre-fetch into. do nothing.
  430. }
  431. else if (unlikely(command == 0x1E))
  432. {
  433. // PREVENT ALLOW MEDIUM REMOVAL
  434. // Not much we can do to prevent the user removing the SD card.
  435. // do nothing.
  436. }
  437. else if (unlikely(command == 0x01))
  438. {
  439. // REZERO UNIT
  440. // Set the lun to a vendor-specific state. Ignore.
  441. }
  442. else if (unlikely(command == 0x35))
  443. {
  444. // SYNCHRONIZE CACHE
  445. // We don't have a cache. do nothing.
  446. }
  447. else if (unlikely(command == 0x2F))
  448. {
  449. // VERIFY
  450. // TODO: When they supply data to verify, we should read the data and
  451. // verify it. If they don't supply any data, just say success.
  452. if ((scsiDev.cdb[1] & 0x02) == 0)
  453. {
  454. // They are asking us to do a medium verification with no data
  455. // comparison. Assume success, do nothing.
  456. }
  457. else
  458. {
  459. // TODO. This means they are supplying data to verify against.
  460. // Technically we should probably grab the data and compare it.
  461. scsiDev.status = CHECK_CONDITION;
  462. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  463. scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;
  464. scsiDev.phase = STATUS;
  465. }
  466. }
  467. else if (unlikely(command == 0x37))
  468. {
  469. // READ DEFECT DATA
  470. scsiDev.status = CHECK_CONDITION;
  471. scsiDev.target->sense.code = NO_SENSE;
  472. scsiDev.target->sense.asc = DEFECT_LIST_NOT_FOUND;
  473. scsiDev.phase = STATUS;
  474. }
  475. else
  476. {
  477. commandHandled = 0;
  478. }
  479. return commandHandled;
  480. }
  481. void scsiDiskPoll()
  482. {
  483. uint32_t bytesPerSector = scsiDev.target->liveCfg.bytesPerSector;
  484. if (scsiDev.phase == DATA_IN &&
  485. transfer.currentBlock != transfer.blocks)
  486. {
  487. scsiEnterPhase(DATA_IN);
  488. int totalSDSectors =
  489. transfer.blocks * SDSectorsPerSCSISector(bytesPerSector);
  490. uint32_t sdLBA =
  491. SCSISector2SD(
  492. scsiDev.target->cfg->sdSectorStart,
  493. bytesPerSector,
  494. transfer.lba);
  495. const int sdPerScsi = SDSectorsPerSCSISector(bytesPerSector);
  496. int buffers = sizeof(scsiDev.data) / SD_SECTOR_SIZE;
  497. int prep = 0;
  498. int i = 0;
  499. int scsiActive = 0;
  500. // int sdActive = 0;
  501. while ((i < totalSDSectors) &&
  502. likely(scsiDev.phase == DATA_IN) &&
  503. likely(!scsiDev.resetFlag))
  504. {
  505. // Wait for the next DMA interrupt. It's beneficial to halt the
  506. // processor to give the DMA controller more memory bandwidth to
  507. // work with.
  508. #if 0
  509. int scsiBusy = 1;
  510. int sdBusy = 1;
  511. while (scsiBusy && sdBusy)
  512. {
  513. uint8_t intr = CyEnterCriticalSection();
  514. scsiBusy = scsiDMABusy();
  515. sdBusy = sdDMABusy();
  516. if (scsiBusy && sdBusy)
  517. {
  518. __WFI();
  519. }
  520. CyExitCriticalSection(intr);
  521. }
  522. #endif
  523. #if 0
  524. if (sdActive && !sdBusy && sdReadSectorDMAPoll())
  525. {
  526. sdActive = 0;
  527. prep++;
  528. }
  529. // Usually SD is slower than the SCSI interface.
  530. // Prioritise starting the read of the next sector over starting a
  531. // SCSI transfer for the last sector
  532. // ie. NO "else" HERE.
  533. if (!sdActive &&
  534. (prep - i < buffers) &&
  535. (prep < totalSDSectors))
  536. {
  537. // Start an SD transfer if we have space.
  538. if (transfer.multiBlock)
  539. {
  540. sdReadMultiSectorDMA(&scsiDev.data[SD_SECTOR_SIZE * (prep % buffers)]);
  541. }
  542. else
  543. {
  544. sdReadSingleSectorDMA(sdLBA + prep, &scsiDev.data[SD_SECTOR_SIZE * (prep % buffers)]);
  545. }
  546. sdActive = 1;
  547. }
  548. #endif
  549. if ((prep - i < buffers) &&
  550. (prep < totalSDSectors))
  551. {
  552. // TODO MM Blocking reads are bad mmkay
  553. sdTmpRead(&scsiDev.data[SD_SECTOR_SIZE * (prep % buffers)], sdLBA + prep, 1);
  554. prep++;
  555. }
  556. if (scsiActive && scsiPhyComplete() && scsiWriteDMAPoll())
  557. {
  558. scsiActive = 0;
  559. i++;
  560. scsiPhyFifoFlip();
  561. }
  562. if (!scsiActive && ((prep - i) > 0))
  563. {
  564. int dmaBytes = SD_SECTOR_SIZE;
  565. if ((i % sdPerScsi) == (sdPerScsi - 1))
  566. {
  567. dmaBytes = bytesPerSector % SD_SECTOR_SIZE;
  568. if (dmaBytes == 0) dmaBytes = SD_SECTOR_SIZE;
  569. }
  570. scsiWriteDMA(&scsiDev.data[SD_SECTOR_SIZE * (i % buffers)], dmaBytes);
  571. scsiActive = 1;
  572. }
  573. }
  574. // We've finished transferring the data to the FPGA, now wait until it's
  575. // written to he SCSI bus.
  576. while (!scsiPhyComplete() &&
  577. likely(scsiDev.phase == DATA_IN) &&
  578. likely(!scsiDev.resetFlag))
  579. {
  580. #if 0
  581. _WFI();
  582. #endif
  583. }
  584. if (scsiDev.phase == DATA_IN)
  585. {
  586. scsiDev.phase = STATUS;
  587. }
  588. scsiDiskReset();
  589. }
  590. else if (scsiDev.phase == DATA_OUT &&
  591. transfer.currentBlock != transfer.blocks)
  592. {
  593. scsiEnterPhase(DATA_OUT);
  594. const int sdPerScsi = SDSectorsPerSCSISector(bytesPerSector);
  595. int totalSDSectors = transfer.blocks * sdPerScsi;
  596. uint32_t sdLBA =
  597. SCSISector2SD(
  598. scsiDev.target->cfg->sdSectorStart,
  599. bytesPerSector,
  600. transfer.lba);
  601. // int buffers = sizeof(scsiDev.data) / SD_SECTOR_SIZE;
  602. // int prep = 0;
  603. int i = 0;
  604. // int scsiDisconnected = 0;
  605. int scsiComplete = 0;
  606. // uint32_t lastActivityTime = s2s_getTime_ms();
  607. // int scsiActive = 0;
  608. // int sdActive = 0;
  609. while ((i < totalSDSectors) &&
  610. (likely(scsiDev.phase == DATA_OUT) || // scsiDisconnect keeps our phase.
  611. scsiComplete) &&
  612. likely(!scsiDev.resetFlag))
  613. {
  614. // Well, until we have some proper non-blocking SD code, we must
  615. // do this in a half-duplex fashion. We need to write as much as
  616. // possible in each SD card transaction.
  617. uint32_t maxSectors = sizeof(scsiDev.data) / SD_SECTOR_SIZE;
  618. uint32_t sectors =
  619. totalSDSectors < maxSectors ? totalSDSectors : maxSectors;
  620. scsiRead(&scsiDev.data[0], sectors * SD_SECTOR_SIZE);
  621. sdTmpWrite(&scsiDev.data[0], i + sdLBA, sectors);
  622. i += sectors;
  623. #if 0
  624. // Wait for the next DMA interrupt. It's beneficial to halt the
  625. // processor to give the DMA controller more memory bandwidth to
  626. // work with.
  627. int scsiBusy = 1;
  628. int sdBusy = 1;
  629. while (scsiBusy && sdBusy)
  630. {
  631. uint8_t intr = CyEnterCriticalSection();
  632. scsiBusy = scsiDMABusy();
  633. sdBusy = sdDMABusy();
  634. if (scsiBusy && sdBusy)
  635. {
  636. __WFI();
  637. }
  638. CyExitCriticalSection(intr);
  639. }
  640. if (sdActive && !sdBusy && sdWriteSectorDMAPoll())
  641. {
  642. sdActive = 0;
  643. i++;
  644. }
  645. if (!sdActive && ((prep - i) > 0))
  646. {
  647. // Start an SD transfer if we have space.
  648. sdWriteMultiSectorDMA(&scsiDev.data[SD_SECTOR_SIZE * (i % buffers)]);
  649. sdActive = 1;
  650. }
  651. uint32_t now = getTime_ms();
  652. if (scsiActive && !scsiBusy && scsiReadDMAPoll())
  653. {
  654. scsiActive = 0;
  655. ++prep;
  656. lastActivityTime = now;
  657. }
  658. if (!scsiActive &&
  659. ((prep - i) < buffers) &&
  660. (prep < totalSDSectors) &&
  661. likely(!scsiDisconnected))
  662. {
  663. int dmaBytes = SD_SECTOR_SIZE;
  664. if ((prep % sdPerScsi) == (sdPerScsi - 1))
  665. {
  666. dmaBytes = bytesPerSector % SD_SECTOR_SIZE;
  667. if (dmaBytes == 0) dmaBytes = SD_SECTOR_SIZE;
  668. }
  669. scsiReadDMA(&scsiDev.data[SD_SECTOR_SIZE * (prep % buffers)], dmaBytes);
  670. scsiActive = 1;
  671. }
  672. else if (
  673. (scsiDev.boardCfg.flags & CONFIG_ENABLE_DISCONNECT) &&
  674. (scsiActive == 0) &&
  675. likely(!scsiDisconnected) &&
  676. unlikely(scsiDev.discPriv) &&
  677. unlikely(diffTime_ms(lastActivityTime, now) >= 20) &&
  678. likely(scsiDev.phase == DATA_OUT))
  679. {
  680. // We're transferring over the SCSI bus faster than the SD card
  681. // can write. There is no more buffer space once we've finished
  682. // this SCSI transfer.
  683. // The NCR 53C700 interface chips have a 250ms "byte-to-byte"
  684. // timeout buffer. SD card writes are supposed to complete
  685. // within 200ms, but sometimes they don't.
  686. // The NCR 53C700 series is used on HP 9000 workstations.
  687. scsiDisconnect();
  688. scsiDisconnected = 1;
  689. lastActivityTime = getTime_ms();
  690. }
  691. else if (unlikely(scsiDisconnected) &&
  692. (
  693. (prep == i) || // Buffers empty.
  694. // Send some messages every 100ms so we don't timeout.
  695. // At a minimum, a reselection involves an IDENTIFY message.
  696. unlikely(diffTime_ms(lastActivityTime, now) >= 100)
  697. ))
  698. {
  699. int reconnected = scsiReconnect();
  700. if (reconnected)
  701. {
  702. scsiDisconnected = 0;
  703. lastActivityTime = getTime_ms(); // Don't disconnect immediately.
  704. }
  705. else if (diffTime_ms(lastActivityTime, getTime_ms()) >= 10000)
  706. {
  707. // Give up after 10 seconds of trying to reconnect.
  708. scsiDev.resetFlag = 1;
  709. }
  710. }
  711. else if (
  712. likely(!scsiComplete) &&
  713. (sdActive == 1) &&
  714. (prep == totalSDSectors) && // All scsi data read and buffered
  715. likely(!scsiDev.discPriv) && // Prefer disconnect where possible.
  716. unlikely(diffTime_ms(lastActivityTime, now) >= 150) &&
  717. likely(scsiDev.phase == DATA_OUT) &&
  718. !(scsiDev.cdb[scsiDev.cdbLen - 1] & 0x01) // Not linked command
  719. )
  720. {
  721. // We're transferring over the SCSI bus faster than the SD card
  722. // can write. All data is buffered, and we're just waiting for
  723. // the SD card to complete. The host won't let us disconnect.
  724. // Some drivers set a 250ms timeout on transfers to complete.
  725. // SD card writes are supposed to complete
  726. // within 200ms, but sometimes they don'to.
  727. // Just pretend we're finished.
  728. scsiComplete = 1;
  729. process_Status();
  730. process_MessageIn(); // Will go to BUS_FREE state
  731. // Try and prevent anyone else using the SCSI bus while we're not ready.
  732. SCSI_SetPin(SCSI_Out_BSY);
  733. }
  734. #endif
  735. }
  736. #if 0
  737. if (scsiComplete)
  738. {
  739. SCSI_ClearPin(SCSI_Out_BSY);
  740. }
  741. while (
  742. !scsiDev.resetFlag &&
  743. unlikely(scsiDisconnected) &&
  744. (s2s_elapsedTime_ms(lastActivityTime) <= 10000))
  745. {
  746. scsiDisconnected = !scsiReconnect();
  747. }
  748. if (scsiDisconnected)
  749. {
  750. // Failed to reconnect
  751. scsiDev.resetFlag = 1;
  752. }
  753. #endif
  754. if (scsiDev.phase == DATA_OUT)
  755. {
  756. if (scsiDev.parityError &&
  757. (scsiDev.boardCfg.flags & S2S_CFG_ENABLE_PARITY) &&
  758. (scsiDev.compatMode >= COMPAT_SCSI2))
  759. {
  760. scsiDev.target->sense.code = ABORTED_COMMAND;
  761. scsiDev.target->sense.asc = SCSI_PARITY_ERROR;
  762. scsiDev.status = CHECK_CONDITION;;
  763. }
  764. scsiDev.phase = STATUS;
  765. }
  766. scsiDiskReset();
  767. }
  768. }
  769. void scsiDiskReset()
  770. {
  771. scsiDev.dataPtr = 0;
  772. scsiDev.savedDataPtr = 0;
  773. scsiDev.dataLen = 0;
  774. // transfer.lba = 0; // Needed in Request Sense to determine failure
  775. transfer.blocks = 0;
  776. transfer.currentBlock = 0;
  777. // Cancel long running commands!
  778. if (
  779. ((scsiDev.boardCfg.flags & S2S_CFG_ENABLE_CACHE) == 0) ||
  780. (transfer.multiBlock == 0)
  781. )
  782. {
  783. #if 0
  784. sdCompleteTransfer();
  785. #endif
  786. }
  787. transfer.multiBlock = 0;
  788. }
  789. void scsiDiskInit()
  790. {
  791. scsiDiskReset();
  792. // Don't require the host to send us a START STOP UNIT command
  793. blockDev.state = DISK_STARTED;
  794. }