disk.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  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 "stm32f2xx.h"
  19. #include "scsi.h"
  20. #include "scsiPhy.h"
  21. #include "config.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. const int buffers = sizeof(scsiDev.data) / SD_SECTOR_SIZE;
  497. int prep = 0;
  498. int i = 0;
  499. int scsiActive __attribute__((unused)) = 0; // unused if DMA disabled
  500. int sdActive = 0;
  501. while ((i < totalSDSectors) &&
  502. likely(scsiDev.phase == DATA_IN) &&
  503. likely(!scsiDev.resetFlag))
  504. {
  505. int completedDmaSectors;
  506. if (sdActive && (completedDmaSectors = sdReadDMAPoll(sdActive)))
  507. {
  508. prep += completedDmaSectors;
  509. sdActive -= completedDmaSectors;
  510. } else if (sdActive > 1)
  511. {
  512. if ((scsiDev.data[SD_SECTOR_SIZE * (prep % buffers) + 510] != 0xAA) ||
  513. (scsiDev.data[SD_SECTOR_SIZE * (prep % buffers) + 511] != 0x33))
  514. {
  515. prep += 1;
  516. sdActive -= 1;
  517. }
  518. }
  519. if (!sdActive &&
  520. (prep - i < buffers) &&
  521. (prep < totalSDSectors))
  522. {
  523. // Start an SD transfer if we have space.
  524. uint32_t startBuffer = prep % buffers;
  525. uint32_t sectors = totalSDSectors - prep;
  526. uint32_t freeBuffers = buffers - (prep - i);
  527. uint32_t contiguousBuffers = buffers - startBuffer;
  528. freeBuffers = freeBuffers < contiguousBuffers
  529. ? freeBuffers : contiguousBuffers;
  530. sectors = sectors < freeBuffers ? sectors : freeBuffers;
  531. if (sectors > 128) sectors = 128; // 65536 DMA limit !!
  532. for (int dodgy = 0; dodgy < sectors; dodgy++)
  533. {
  534. scsiDev.data[SD_SECTOR_SIZE * (startBuffer + dodgy) + 510] = 0xAA;
  535. scsiDev.data[SD_SECTOR_SIZE * (startBuffer + dodgy) + 511] = 0x33;
  536. }
  537. sdReadDMA(sdLBA + prep, sectors, &scsiDev.data[SD_SECTOR_SIZE * startBuffer]);
  538. sdActive = sectors;
  539. }
  540. #ifdef SCSI_FSMC_DMA
  541. if (scsiActive && scsiPhyComplete() && scsiWriteDMAPoll())
  542. {
  543. scsiActive = 0;
  544. i++;
  545. scsiPhyFifoFlip();
  546. }
  547. if (!scsiActive && ((prep - i) > 0))
  548. {
  549. int dmaBytes = SD_SECTOR_SIZE;
  550. if ((i % sdPerScsi) == (sdPerScsi - 1))
  551. {
  552. dmaBytes = bytesPerSector % SD_SECTOR_SIZE;
  553. if (dmaBytes == 0) dmaBytes = SD_SECTOR_SIZE;
  554. }
  555. scsiWriteDMA(&scsiDev.data[SD_SECTOR_SIZE * (i % buffers)], dmaBytes);
  556. scsiActive = 1;
  557. }
  558. #else
  559. if ((prep - i) > 0)
  560. {
  561. int dmaBytes = SD_SECTOR_SIZE;
  562. if ((i % sdPerScsi) == (sdPerScsi - 1))
  563. {
  564. dmaBytes = bytesPerSector % SD_SECTOR_SIZE;
  565. if (dmaBytes == 0) dmaBytes = SD_SECTOR_SIZE;
  566. }
  567. uint16_t* scsiDmaData = (uint16_t*) &(scsiDev.data[SD_SECTOR_SIZE * (i % buffers)]);
  568. // Manually unrolled loop for performance.
  569. // -Os won't unroll this for us automatically,
  570. // especially since scsiPhyTx does volatile stuff.
  571. // Reduces bus utilisation by making the fsmc split
  572. // 32bits into 2 16 bit writes.
  573. int k = 0;
  574. for (; k + 4 < (dmaBytes + 1) / 2; k += 4)
  575. {
  576. scsiPhyTx32(scsiDmaData[k], scsiDmaData[k+1]);
  577. scsiPhyTx32(scsiDmaData[k+2], scsiDmaData[k+3]);
  578. }
  579. for (; k < (dmaBytes + 1) / 2; ++k)
  580. {
  581. scsiPhyTx(scsiDmaData[k]);
  582. }
  583. i++;
  584. while (!scsiPhyComplete() && !scsiDev.resetFlag)
  585. {
  586. __WFE(); // Wait for event
  587. }
  588. scsiPhyFifoFlip();
  589. scsiSetDataCount(dmaBytes);
  590. }
  591. #endif
  592. }
  593. // We've finished transferring the data to the FPGA, now wait until it's
  594. // written to he SCSI bus.
  595. while (!scsiPhyComplete() &&
  596. likely(scsiDev.phase == DATA_IN) &&
  597. likely(!scsiDev.resetFlag))
  598. {
  599. __WFE(); // Wait for event
  600. }
  601. if (scsiDev.phase == DATA_IN)
  602. {
  603. scsiDev.phase = STATUS;
  604. }
  605. scsiDiskReset();
  606. }
  607. else if (scsiDev.phase == DATA_OUT &&
  608. transfer.currentBlock != transfer.blocks)
  609. {
  610. scsiEnterPhase(DATA_OUT);
  611. const int sdPerScsi = SDSectorsPerSCSISector(bytesPerSector);
  612. int totalSDSectors = transfer.blocks * sdPerScsi;
  613. uint32_t sdLBA =
  614. SCSISector2SD(
  615. scsiDev.target->cfg->sdSectorStart,
  616. bytesPerSector,
  617. transfer.lba);
  618. // int buffers = sizeof(scsiDev.data) / SD_SECTOR_SIZE;
  619. // int prep = 0;
  620. int i = 0;
  621. // int scsiDisconnected = 0;
  622. int scsiComplete = 0;
  623. // uint32_t lastActivityTime = s2s_getTime_ms();
  624. // int scsiActive = 0;
  625. // int sdActive = 0;
  626. int parityError = 0;
  627. while ((i < totalSDSectors) &&
  628. (likely(scsiDev.phase == DATA_OUT) || // scsiDisconnect keeps our phase.
  629. scsiComplete) &&
  630. likely(!scsiDev.resetFlag) &&
  631. likely(!parityError))
  632. {
  633. // Well, until we have some proper non-blocking SD code, we must
  634. // do this in a half-duplex fashion. We need to write as much as
  635. // possible in each SD card transaction.
  636. uint32_t maxSectors = sizeof(scsiDev.data) / SD_SECTOR_SIZE;
  637. uint32_t rem = totalSDSectors - i;
  638. uint32_t sectors =
  639. rem < maxSectors ? rem : maxSectors;
  640. scsiRead(&scsiDev.data[0], sectors * SD_SECTOR_SIZE, &parityError);
  641. if (!parityError)
  642. {
  643. sdTmpWrite(&scsiDev.data[0], i + sdLBA, sectors);
  644. }
  645. i += sectors;
  646. #if 0
  647. // Wait for the next DMA interrupt. It's beneficial to halt the
  648. // processor to give the DMA controller more memory bandwidth to
  649. // work with.
  650. int scsiBusy = 1;
  651. int sdBusy = 1;
  652. while (scsiBusy && sdBusy)
  653. {
  654. uint8_t intr = CyEnterCriticalSection();
  655. scsiBusy = scsiDMABusy();
  656. sdBusy = sdDMABusy();
  657. if (scsiBusy && sdBusy)
  658. {
  659. __WFI();
  660. }
  661. CyExitCriticalSection(intr);
  662. }
  663. if (sdActive && !sdBusy && sdWriteSectorDMAPoll())
  664. {
  665. sdActive = 0;
  666. i++;
  667. }
  668. if (!sdActive && ((prep - i) > 0))
  669. {
  670. // Start an SD transfer if we have space.
  671. sdWriteMultiSectorDMA(&scsiDev.data[SD_SECTOR_SIZE * (i % buffers)]);
  672. sdActive = 1;
  673. }
  674. uint32_t now = getTime_ms();
  675. if (scsiActive && !scsiBusy && scsiReadDMAPoll())
  676. {
  677. scsiActive = 0;
  678. ++prep;
  679. lastActivityTime = now;
  680. }
  681. if (!scsiActive &&
  682. ((prep - i) < buffers) &&
  683. (prep < totalSDSectors) &&
  684. likely(!scsiDisconnected))
  685. {
  686. int dmaBytes = SD_SECTOR_SIZE;
  687. if ((prep % sdPerScsi) == (sdPerScsi - 1))
  688. {
  689. dmaBytes = bytesPerSector % SD_SECTOR_SIZE;
  690. if (dmaBytes == 0) dmaBytes = SD_SECTOR_SIZE;
  691. }
  692. scsiReadDMA(&scsiDev.data[SD_SECTOR_SIZE * (prep % buffers)], dmaBytes);
  693. scsiActive = 1;
  694. }
  695. else if (
  696. (scsiDev.boardCfg.flags & CONFIG_ENABLE_DISCONNECT) &&
  697. (scsiActive == 0) &&
  698. likely(!scsiDisconnected) &&
  699. unlikely(scsiDev.discPriv) &&
  700. unlikely(diffTime_ms(lastActivityTime, now) >= 20) &&
  701. likely(scsiDev.phase == DATA_OUT))
  702. {
  703. // We're transferring over the SCSI bus faster than the SD card
  704. // can write. There is no more buffer space once we've finished
  705. // this SCSI transfer.
  706. // The NCR 53C700 interface chips have a 250ms "byte-to-byte"
  707. // timeout buffer. SD card writes are supposed to complete
  708. // within 200ms, but sometimes they don't.
  709. // The NCR 53C700 series is used on HP 9000 workstations.
  710. scsiDisconnect();
  711. scsiDisconnected = 1;
  712. lastActivityTime = getTime_ms();
  713. }
  714. else if (unlikely(scsiDisconnected) &&
  715. (
  716. (prep == i) || // Buffers empty.
  717. // Send some messages every 100ms so we don't timeout.
  718. // At a minimum, a reselection involves an IDENTIFY message.
  719. unlikely(diffTime_ms(lastActivityTime, now) >= 100)
  720. ))
  721. {
  722. int reconnected = scsiReconnect();
  723. if (reconnected)
  724. {
  725. scsiDisconnected = 0;
  726. lastActivityTime = getTime_ms(); // Don't disconnect immediately.
  727. }
  728. else if (diffTime_ms(lastActivityTime, getTime_ms()) >= 10000)
  729. {
  730. // Give up after 10 seconds of trying to reconnect.
  731. scsiDev.resetFlag = 1;
  732. }
  733. }
  734. else if (
  735. likely(!scsiComplete) &&
  736. (sdActive == 1) &&
  737. (prep == totalSDSectors) && // All scsi data read and buffered
  738. likely(!scsiDev.discPriv) && // Prefer disconnect where possible.
  739. unlikely(diffTime_ms(lastActivityTime, now) >= 150) &&
  740. likely(scsiDev.phase == DATA_OUT) &&
  741. !(scsiDev.cdb[scsiDev.cdbLen - 1] & 0x01) // Not linked command
  742. )
  743. {
  744. // We're transferring over the SCSI bus faster than the SD card
  745. // can write. All data is buffered, and we're just waiting for
  746. // the SD card to complete. The host won't let us disconnect.
  747. // Some drivers set a 250ms timeout on transfers to complete.
  748. // SD card writes are supposed to complete
  749. // within 200ms, but sometimes they don'to.
  750. // Just pretend we're finished.
  751. scsiComplete = 1;
  752. process_Status();
  753. process_MessageIn(); // Will go to BUS_FREE state
  754. // Try and prevent anyone else using the SCSI bus while we're not ready.
  755. SCSI_SetPin(SCSI_Out_BSY);
  756. }
  757. #endif
  758. }
  759. #if 0
  760. if (scsiComplete)
  761. {
  762. SCSI_ClearPin(SCSI_Out_BSY);
  763. }
  764. while (
  765. !scsiDev.resetFlag &&
  766. unlikely(scsiDisconnected) &&
  767. (s2s_elapsedTime_ms(lastActivityTime) <= 10000))
  768. {
  769. scsiDisconnected = !scsiReconnect();
  770. }
  771. if (scsiDisconnected)
  772. {
  773. // Failed to reconnect
  774. scsiDev.resetFlag = 1;
  775. }
  776. #endif
  777. if (scsiDev.phase == DATA_OUT)
  778. {
  779. if (parityError &&
  780. (scsiDev.boardCfg.flags & S2S_CFG_ENABLE_PARITY))
  781. {
  782. scsiDev.target->sense.code = ABORTED_COMMAND;
  783. scsiDev.target->sense.asc = SCSI_PARITY_ERROR;
  784. scsiDev.status = CHECK_CONDITION;;
  785. }
  786. scsiDev.phase = STATUS;
  787. }
  788. scsiDiskReset();
  789. }
  790. }
  791. void scsiDiskReset()
  792. {
  793. scsiDev.dataPtr = 0;
  794. scsiDev.savedDataPtr = 0;
  795. scsiDev.dataLen = 0;
  796. // transfer.lba = 0; // Needed in Request Sense to determine failure
  797. transfer.blocks = 0;
  798. transfer.currentBlock = 0;
  799. // Cancel long running commands!
  800. #if 0
  801. if (
  802. ((scsiDev.boardCfg.flags & S2S_CFG_ENABLE_CACHE) == 0) ||
  803. (transfer.multiBlock == 0)
  804. )
  805. #endif
  806. {
  807. sdCompleteTransfer();
  808. }
  809. transfer.multiBlock = 0;
  810. }
  811. void scsiDiskInit()
  812. {
  813. scsiDiskReset();
  814. // Don't require the host to send us a START STOP UNIT command
  815. blockDev.state = DISK_STARTED;
  816. }