scsi.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482
  1. // Copyright (C) 2014 Michael McMaster <michael@codesrc.com>
  2. // Copyright (c) 2023 joshua stein <jcs@jcs.org>
  3. // Copyright (c) 2023 Andrea Ottaviani <andrea.ottaviani.69@gmail.com>
  4. // Copyright (c) 2024-2025 Rabbit Hole Computing™
  5. //
  6. // This file is part of SCSI2SD.
  7. //
  8. // SCSI2SD is free software: you can redistribute it and/or modify
  9. // it under the terms of the GNU General Public License as published by
  10. // the Free Software Foundation, either version 3 of the License, or
  11. // (at your option) any later version.
  12. //
  13. // SCSI2SD is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU General Public License
  19. // along with SCSI2SD. If not, see <http://www.gnu.org/licenses/>.
  20. #include "scsi.h"
  21. #include "scsiPhy.h"
  22. #include "config.h"
  23. #include "diagnostic.h"
  24. #include "disk.h"
  25. #include "inquiry.h"
  26. #include "led.h"
  27. #include "mode.h"
  28. #include "scsi2sd_time.h"
  29. #include "timings.h"
  30. #include "bsp.h"
  31. #include "cdrom.h"
  32. #include "network.h"
  33. #include "tape.h"
  34. #include "mo.h"
  35. #include "vendor.h"
  36. #include <string.h>
  37. #include "toolbox.h"
  38. // Global SCSI device state.
  39. ScsiDevice scsiDev S2S_DMA_ALIGN;
  40. static void enter_SelectionPhase(void);
  41. static void process_SelectionPhase(void);
  42. static void enter_MessageIn(uint8_t message);
  43. static void enter_Status(uint8_t status);
  44. static void enter_DataIn(int len);
  45. static void process_DataIn(void);
  46. static void process_DataOut(void);
  47. static void process_Command(void);
  48. static void doReserveRelease(void);
  49. void enter_BusFree()
  50. {
  51. // This delay probably isn't needed for most SCSI hosts, but it won't
  52. // hurt either. It's possible some of the samplers needed this delay.
  53. if (scsiDev.compatMode < COMPAT_SCSI2)
  54. {
  55. s2s_delay_us(2);
  56. }
  57. #if 0
  58. if (scsiDev.status != GOOD)// && isDebugEnabled())
  59. {
  60. // We want to capture debug information for failure cases.
  61. s2s_delay_ms(80);
  62. }
  63. #endif
  64. scsiEnterBusFree();
  65. // Wait for the initiator to cease driving signals
  66. // Bus settle delay + bus clear delay = 1200ns
  67. // Just waiting the clear delay is sufficient.
  68. s2s_delay_ns(800);
  69. s2s_ledOff();
  70. scsiDev.phase = BUS_FREE;
  71. scsiDev.selFlag = 0;
  72. }
  73. static void enter_MessageIn(uint8_t message)
  74. {
  75. scsiDev.msgIn = message;
  76. scsiDev.phase = MESSAGE_IN;
  77. }
  78. int process_MessageIn(int releaseBusFree)
  79. {
  80. scsiEnterPhase(MESSAGE_IN);
  81. scsiWriteByte(scsiDev.msgIn);
  82. if (unlikely(scsiDev.atnFlag))
  83. {
  84. // If there was a parity error, we go
  85. // back to MESSAGE_OUT first, get out parity error message, then come
  86. // back here.
  87. return 0;
  88. }
  89. else if ((scsiDev.msgIn == MSG_LINKED_COMMAND_COMPLETE) ||
  90. (scsiDev.msgIn == MSG_LINKED_COMMAND_COMPLETE_WITH_FLAG))
  91. {
  92. // Go back to the command phase and start again.
  93. scsiDev.phase = COMMAND;
  94. scsiDev.dataPtr = 0;
  95. scsiDev.savedDataPtr = 0;
  96. scsiDev.dataLen = 0;
  97. scsiDev.status = GOOD;
  98. transfer.blocks = 0;
  99. transfer.currentBlock = 0;
  100. return 0;
  101. }
  102. else if (releaseBusFree) /*if (scsiDev.msgIn == MSG_COMMAND_COMPLETE)*/
  103. {
  104. enter_BusFree();
  105. return 1;
  106. }
  107. else
  108. {
  109. return 1;
  110. }
  111. }
  112. static void messageReject()
  113. {
  114. scsiEnterPhase(MESSAGE_IN);
  115. scsiWriteByte(MSG_REJECT);
  116. }
  117. static void enter_Status(uint8_t status)
  118. {
  119. scsiDev.status = status;
  120. scsiDev.phase = STATUS;
  121. scsiDev.lastStatus = scsiDev.status;
  122. scsiDev.lastSense = scsiDev.target->sense.code;
  123. scsiDev.lastSenseASC = scsiDev.target->sense.asc;
  124. }
  125. void process_Status()
  126. {
  127. scsiEnterPhase(STATUS);
  128. if (scsiDev.target->cfg->quirks == S2S_CFG_QUIRKS_EWSD)
  129. {
  130. s2s_delay_ms(1);
  131. }
  132. uint8_t message;
  133. uint8_t control = scsiDev.cdb[scsiDev.cdbLen - 1];
  134. if (scsiDev.target->cfg->quirks == S2S_CFG_QUIRKS_OMTI)
  135. {
  136. // All commands have a control byte, except 0xC0
  137. if (scsiDev.cdb[0] == 0xC0)
  138. {
  139. control = 0;
  140. }
  141. // OMTI non-standard LINK control
  142. if (control & 0x01)
  143. {
  144. scsiDev.phase = COMMAND;
  145. return;
  146. }
  147. }
  148. if ((scsiDev.status == GOOD) && (control & 0x01) &&
  149. scsiDev.target->cfg->quirks != S2S_CFG_QUIRKS_XEBEC)
  150. {
  151. // Linked command.
  152. scsiDev.status = INTERMEDIATE;
  153. if (control & 0x02)
  154. {
  155. message = MSG_LINKED_COMMAND_COMPLETE_WITH_FLAG;
  156. }
  157. else
  158. {
  159. message = MSG_LINKED_COMMAND_COMPLETE;
  160. }
  161. }
  162. else
  163. {
  164. message = MSG_COMMAND_COMPLETE;
  165. }
  166. if (scsiDev.target->cfg->quirks == S2S_CFG_QUIRKS_XEBEC)
  167. {
  168. // More non-standardness. Expects 2 status bytes (really status + msg)
  169. // 00 d 000 err 0
  170. // d == disk number
  171. // ERR = 1 if error.
  172. if (scsiDev.status == GOOD)
  173. {
  174. scsiWriteByte(scsiDev.cdb[1] & 0x20);
  175. }
  176. else
  177. {
  178. scsiWriteByte((scsiDev.cdb[1] & 0x20) | 0x2);
  179. }
  180. s2s_delay_us(10); // Seems to need a delay before changing phase bits.
  181. }
  182. else if (scsiDev.target->cfg->quirks == S2S_CFG_QUIRKS_OMTI)
  183. {
  184. scsiDev.status |= (scsiDev.target->targetId & 0x03) << 5;
  185. scsiWriteByte(scsiDev.status);
  186. }
  187. else
  188. {
  189. scsiWriteByte(scsiDev.status);
  190. }
  191. scsiDev.lastStatus = scsiDev.status;
  192. scsiDev.lastSense = scsiDev.target->sense.code;
  193. scsiDev.lastSenseASC = scsiDev.target->sense.asc;
  194. // Command Complete occurs AFTER a valid status has been
  195. // sent. then we go bus-free.
  196. enter_MessageIn(message);
  197. }
  198. static void enter_DataIn(int len)
  199. {
  200. scsiDev.dataLen = len;
  201. scsiDev.phase = DATA_IN;
  202. }
  203. static void process_DataIn()
  204. {
  205. uint32_t len;
  206. if (scsiDev.dataLen > sizeof(scsiDev.data))
  207. {
  208. scsiDev.dataLen = sizeof(scsiDev.data);
  209. }
  210. len = scsiDev.dataLen - scsiDev.dataPtr;
  211. if (len > 0)
  212. {
  213. scsiEnterPhase(DATA_IN);
  214. scsiWrite(scsiDev.data + scsiDev.dataPtr, len);
  215. scsiDev.dataPtr += len;
  216. }
  217. if ((scsiDev.dataPtr >= scsiDev.dataLen) &&
  218. (transfer.currentBlock == transfer.blocks))
  219. {
  220. enter_Status(GOOD);
  221. }
  222. }
  223. static void process_DataOut()
  224. {
  225. uint32_t len;
  226. if (scsiDev.dataLen > sizeof(scsiDev.data))
  227. {
  228. scsiDev.dataLen = sizeof(scsiDev.data);
  229. }
  230. len = scsiDev.dataLen - scsiDev.dataPtr;
  231. if (len > 0)
  232. {
  233. scsiEnterPhase(DATA_OUT);
  234. int parityError = 0;
  235. scsiRead(scsiDev.data + scsiDev.dataPtr, len, &parityError);
  236. scsiDev.dataPtr += len;
  237. if (parityError &&
  238. (scsiDev.boardCfg.flags & S2S_CFG_ENABLE_PARITY))
  239. {
  240. scsiDev.target->sense.code = ABORTED_COMMAND;
  241. scsiDev.target->sense.asc = SCSI_PARITY_ERROR;
  242. enter_Status(CHECK_CONDITION);
  243. }
  244. }
  245. if ((scsiDev.dataPtr >= scsiDev.dataLen) &&
  246. (transfer.currentBlock == transfer.blocks))
  247. {
  248. if (scsiDev.postDataOutHook != NULL)
  249. {
  250. scsiDev.postDataOutHook();
  251. }
  252. else
  253. {
  254. enter_Status(GOOD);
  255. }
  256. }
  257. }
  258. static const uint8_t CmdGroupBytes[8] = {6, 10, 10, 6, 16, 12, 6, 6};
  259. static void process_Command()
  260. {
  261. int group;
  262. uint8_t command;
  263. uint8_t control;
  264. scsiEnterPhase(COMMAND);
  265. memset(scsiDev.cdb + 6, 0, sizeof(scsiDev.cdb) - 6);
  266. int parityError = 0;
  267. scsiRead(scsiDev.cdb, 6, &parityError);
  268. command = scsiDev.cdb[0];
  269. group = scsiDev.cdb[0] >> 5;
  270. scsiDev.cdbLen = CmdGroupBytes[group];
  271. scsiVendorCommandSetLen(scsiDev.cdb[0], &scsiDev.cdbLen);
  272. if (parityError &&
  273. (scsiDev.boardCfg.flags & S2S_CFG_ENABLE_PARITY))
  274. {
  275. // Don't try and read more bytes, as we cannot be sure what group
  276. // the command should be.
  277. }
  278. else if (scsiDev.cdbLen - 6 > 0)
  279. {
  280. scsiRead(scsiDev.cdb + 6, scsiDev.cdbLen - 6, &parityError);
  281. }
  282. // Prefer LUN's set by IDENTIFY messages for newer hosts.
  283. if (scsiDev.lun < 0)
  284. {
  285. if (command == 0xE0 || command == 0xE4) // XEBEC s1410
  286. {
  287. scsiDev.lun = 0;
  288. }
  289. else
  290. {
  291. scsiDev.lun = scsiDev.cdb[1] >> 5;
  292. }
  293. }
  294. // For Philips P2000C with Xebec S1410 SASI/MFM adapter
  295. // http://bitsavers.trailing-edge.com/pdf/xebec/104524C_S1410Man_Aug83.pdf
  296. if ((scsiDev.lun > 0) && (scsiDev.boardCfg.flags & S2S_CFG_MAP_LUNS_TO_IDS))
  297. {
  298. int tgtIndex;
  299. for (tgtIndex = 0; tgtIndex < S2S_MAX_TARGETS; ++tgtIndex)
  300. {
  301. if (scsiDev.targets[tgtIndex].targetId == scsiDev.lun)
  302. {
  303. scsiDev.target = &scsiDev.targets[tgtIndex];
  304. scsiDev.lun = 0;
  305. break;
  306. }
  307. }
  308. }
  309. control = scsiDev.cdb[scsiDev.cdbLen - 1];
  310. scsiDev.cmdCount++;
  311. const S2S_TargetCfg* cfg = scsiDev.target->cfg;
  312. if (unlikely(scsiDev.resetFlag))
  313. {
  314. // Don't log bogus commands
  315. scsiDev.cmdCount--;
  316. memset(scsiDev.cdb, 0xff, sizeof(scsiDev.cdb));
  317. return;
  318. }
  319. // X68000 and strange "0x00 0xXX .. .. .. .." command
  320. else if ((command == 0x00) && likely(scsiDev.target->cfg->quirks == S2S_CFG_QUIRKS_X68000))
  321. {
  322. if (scsiDev.cdb[1] == 0x28)
  323. {
  324. scsiDev.target->sense.code = NO_SENSE;
  325. scsiDev.target->sense.asc = NO_ADDITIONAL_SENSE_INFORMATION;
  326. enter_Status(CHECK_CONDITION);
  327. return;
  328. } else if (scsiDev.cdb[1] == 0x03)
  329. {
  330. scsiDev.target->sense.code = NO_SENSE;
  331. scsiDev.target->sense.asc = NO_ADDITIONAL_SENSE_INFORMATION;
  332. enter_Status(GOOD);
  333. return;
  334. }
  335. }
  336. else if (parityError &&
  337. (scsiDev.boardCfg.flags & S2S_CFG_ENABLE_PARITY))
  338. {
  339. scsiDev.target->sense.code = ABORTED_COMMAND;
  340. scsiDev.target->sense.asc = SCSI_PARITY_ERROR;
  341. enter_Status(CHECK_CONDITION);
  342. }
  343. else if ((control & 0x02) && ((control & 0x01) == 0) &&
  344. // used for head step options on xebec.
  345. likely(scsiDev.target->cfg->quirks != S2S_CFG_QUIRKS_XEBEC))
  346. {
  347. // FLAG set without LINK flag.
  348. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  349. scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;
  350. enter_Status(CHECK_CONDITION);
  351. }
  352. else if (command == 0x12)
  353. {
  354. s2s_scsiInquiry();
  355. }
  356. else if (command == 0x03)
  357. {
  358. // REQUEST SENSE
  359. uint32_t allocLength = scsiDev.cdb[4];
  360. if (scsiDev.target->cfg->quirks == S2S_CFG_QUIRKS_XEBEC)
  361. {
  362. // Completely non-standard
  363. allocLength = 4;
  364. switch (scsiDev.target->sense.code)
  365. {
  366. case NO_SENSE:
  367. scsiDev.data[0] = 0;
  368. break;
  369. case MEDIUM_ERROR:
  370. switch (scsiDev.target->sense.asc)
  371. {
  372. case NO_SEEK_COMPLETE:
  373. scsiDev.data[0] = 0x15; // Seek Error
  374. break;
  375. case WRITE_ERROR_AUTO_REALLOCATION_FAILED:
  376. scsiDev.data[0] = 0x03; // Write fault
  377. break;
  378. default:
  379. case UNRECOVERED_READ_ERROR:
  380. scsiDev.data[0] = 0x11; // Uncorrectable read error
  381. break;
  382. }
  383. break;
  384. case ILLEGAL_REQUEST:
  385. switch (scsiDev.target->sense.asc)
  386. {
  387. case LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE:
  388. scsiDev.data[0] = 0x14; // Target sector not found
  389. break;
  390. case WRITE_PROTECTED:
  391. scsiDev.data[0] = 0x03; // Write fault
  392. break;
  393. default:
  394. scsiDev.data[0] = 0x20; // Invalid command
  395. break;
  396. }
  397. break;
  398. case NOT_READY:
  399. switch (scsiDev.target->sense.asc)
  400. {
  401. default:
  402. case MEDIUM_NOT_PRESENT:
  403. scsiDev.data[0] = 0x04; // Drive not ready
  404. break;
  405. case LOGICAL_UNIT_NOT_READY_INITIALIZING_COMMAND_REQUIRED:
  406. scsiDev.data[0] = 0x1A; // Format Error
  407. break;
  408. }
  409. break;
  410. default:
  411. scsiDev.data[0] = 0x11; // Uncorrectable data error
  412. break;
  413. }
  414. scsiDev.data[1] = (scsiDev.cdb[1] & 0x20) | ((transfer.lba >> 16) & 0x1F);
  415. scsiDev.data[2] = transfer.lba >> 8;
  416. scsiDev.data[3] = transfer.lba;
  417. }
  418. else if (cfg->quirks == S2S_CFG_QUIRKS_OMTI)
  419. {
  420. // The response is completely non-standard.
  421. if (likely(allocLength > 12))
  422. allocLength = 12;
  423. else if (unlikely(allocLength < 4))
  424. allocLength = 4;
  425. if (cfg->deviceType != S2S_CFG_SEQUENTIAL)
  426. allocLength = 4;
  427. memset(scsiDev.data, 0, allocLength);
  428. if (scsiDev.target->sense.code == NO_SENSE)
  429. {
  430. // Nothing to report.
  431. }
  432. else if (scsiDev.target->sense.code == UNIT_ATTENTION &&
  433. cfg->deviceType == S2S_CFG_SEQUENTIAL)
  434. {
  435. scsiDev.data[0] = 0x10; // Tape exception
  436. }
  437. else if (scsiDev.target->sense.code == ILLEGAL_REQUEST)
  438. {
  439. if (scsiDev.target->sense.asc == LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE)
  440. {
  441. if (cfg->deviceType == S2S_CFG_SEQUENTIAL)
  442. scsiDev.data[0] = 0x10; // Tape exception
  443. else
  444. scsiDev.data[0] = 0x21; // Illegal Parameters
  445. }
  446. else if (scsiDev.target->sense.asc == INVALID_COMMAND_OPERATION_CODE)
  447. {
  448. scsiDev.data[0] = 0x20; // Invalid Command
  449. }
  450. }
  451. else if (scsiDev.target->sense.code == NOT_READY)
  452. {
  453. scsiDev.data[0] = 0x04; // Drive not ready
  454. }
  455. else if (scsiDev.target->sense.code == BLANK_CHECK)
  456. {
  457. scsiDev.data[0] = 0x10; // Tape exception
  458. }
  459. else
  460. {
  461. scsiDev.data[0] = 0x11; // Uncorrectable data error
  462. }
  463. scsiDev.data[1] = (scsiDev.cdb[1] & 0x60) | ((transfer.lba >> 16) & 0x1F);
  464. scsiDev.data[2] = transfer.lba >> 8;
  465. scsiDev.data[3] = transfer.lba;
  466. if (cfg->deviceType == S2S_CFG_SEQUENTIAL)
  467. {
  468. // For the tape drive there are 8 extra sense bytes.
  469. if (scsiDev.target->sense.code == BLANK_CHECK)
  470. scsiDev.data[11] = 0x88; // End of data recorded on the tape
  471. else if (scsiDev.target->sense.code == UNIT_ATTENTION)
  472. scsiDev.data[5] = 0x81; // Power On Reset occurred
  473. else if (scsiDev.target->sense.code == ILLEGAL_REQUEST &&
  474. scsiDev.target->sense.asc == LOGICAL_BLOCK_ADDRESS_OUT_OF_RANGE)
  475. scsiDev.data[4] = 0x81; // File Mark detected
  476. }
  477. }
  478. else
  479. {
  480. // As specified by the SASI and SCSI1 standard.
  481. // Newer initiators won't be specifying 0 anyway.
  482. if (allocLength == 0) allocLength = 4;
  483. memset(scsiDev.data, 0, 256); // Max possible alloc length
  484. scsiDev.data[0] = 0xF0;
  485. scsiDev.data[2] = scsiDev.target->sense.code & 0x0F;
  486. scsiDev.data[3] = transfer.lba >> 24;
  487. scsiDev.data[4] = transfer.lba >> 16;
  488. scsiDev.data[5] = transfer.lba >> 8;
  489. scsiDev.data[6] = transfer.lba;
  490. // Additional bytes if there are errors to report
  491. scsiDev.data[7] = 10; // additional length
  492. scsiDev.data[12] = scsiDev.target->sense.asc >> 8;
  493. scsiDev.data[13] = scsiDev.target->sense.asc;
  494. if ((scsiDev.target->cfg->quirks == S2S_CFG_QUIRKS_EWSD))
  495. {
  496. /* EWSD seems not to want something behind additional length. (8 + 0x0e = 22) */
  497. allocLength=22;
  498. scsiDev.data[7] = 0x0e;
  499. }
  500. }
  501. // Silently truncate results. SCSI-2 spec 8.2.14.
  502. enter_DataIn(allocLength);
  503. // This is a good time to clear out old sense information.
  504. scsiDev.target->sense.code = NO_SENSE;
  505. scsiDev.target->sense.asc = NO_ADDITIONAL_SENSE_INFORMATION;
  506. }
  507. // Some old SCSI drivers do NOT properly support
  508. // unitAttention. eg. the Mac Plus would trigger a SCSI reset
  509. // on receiving the unit attention response on boot, thus
  510. // triggering another unit attention condition.
  511. else if (scsiDev.target->unitAttention &&
  512. scsiDev.target->unitAttentionStop == 0 &&
  513. ((scsiDev.boardCfg.flags & S2S_CFG_ENABLE_UNIT_ATTENTION) ||
  514. (scsiDev.target->cfg->quirks == S2S_CFG_QUIRKS_EWSD)))
  515. {
  516. /* EWSD requires unitAttention to be sent only once. */
  517. if (scsiDev.target->cfg->quirks == S2S_CFG_QUIRKS_EWSD)
  518. {
  519. scsiDev.target->unitAttentionStop = 1;
  520. }
  521. scsiDev.target->sense.code = UNIT_ATTENTION;
  522. scsiDev.target->sense.asc = scsiDev.target->unitAttention;
  523. // If initiator doesn't do REQUEST SENSE for the next command, then
  524. // data is lost.
  525. scsiDev.target->unitAttention = 0;
  526. enter_Status(CHECK_CONDITION);
  527. }
  528. else if (scsiDev.lun && (command < 0xD0))
  529. {
  530. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  531. scsiDev.target->sense.asc = LOGICAL_UNIT_NOT_SUPPORTED;
  532. enter_Status(CHECK_CONDITION);
  533. }
  534. else if (command == 0x17 || command == 0x16)
  535. {
  536. doReserveRelease();
  537. }
  538. else if ((scsiDev.target->reservedId >= 0) &&
  539. (scsiDev.target->reservedId != scsiDev.initiatorId))
  540. {
  541. enter_Status(CONFLICT);
  542. }
  543. // Handle Toolbox commands, overriding other vendor commands if enabled
  544. else if (scsiToolboxEnabled() && scsiToolboxCommand())
  545. {
  546. // already handled
  547. }
  548. // Handle odd device types first that may override basic read and
  549. // write commands. Will fall-through to generic disk handling.
  550. // Some device specific vendor commands are located here instead
  551. // of in scsiVendorCommand()
  552. else if (((cfg->deviceType == S2S_CFG_OPTICAL) && scsiCDRomCommand()) ||
  553. ((cfg->deviceType == S2S_CFG_SEQUENTIAL) && scsiTapeCommand()) ||
  554. #ifdef ZULUSCSI_NETWORK
  555. ((cfg->deviceType == S2S_CFG_NETWORK && scsiNetworkCommand())) ||
  556. #endif // ZULUSCSI_NETWORK
  557. ((cfg->deviceType == S2S_CFG_MO) && scsiMOCommand()))
  558. {
  559. // Already handled.
  560. }
  561. else if (scsiDiskCommand())
  562. {
  563. // Already handled.
  564. // check for the performance-critical read/write
  565. // commands ASAP.
  566. }
  567. else if (command == 0x1C)
  568. {
  569. scsiReceiveDiagnostic();
  570. }
  571. else if (command == 0x1D)
  572. {
  573. scsiSendDiagnostic();
  574. }
  575. else if (command == 0x3B)
  576. {
  577. scsiWriteBuffer();
  578. }
  579. else if (command == 0x3C)
  580. {
  581. scsiReadBuffer();
  582. }
  583. else if (scsiModeCommand())
  584. {
  585. // handled
  586. }
  587. else if (scsiVendorCommand())
  588. {
  589. // handled
  590. }
  591. else if (unlikely(command == 0x00))
  592. {
  593. // TEST UNIT READY
  594. doTestUnitReady();
  595. }
  596. else if (unlikely(!doTestUnitReady()))
  597. {
  598. // This should be last as it can override other commands
  599. // Status and sense codes already set by doTestUnitReady
  600. }
  601. else
  602. {
  603. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  604. scsiDev.target->sense.asc = INVALID_COMMAND_OPERATION_CODE;
  605. enter_Status(CHECK_CONDITION);
  606. }
  607. // Successful
  608. if (scsiDev.phase == COMMAND) // No status set, and not in DATA_IN
  609. {
  610. enter_Status(GOOD);
  611. }
  612. }
  613. static void doReserveRelease()
  614. {
  615. int extentReservation = scsiDev.cdb[1] & 1;
  616. int thirdPty = scsiDev.cdb[1] & 0x10;
  617. int thirdPtyId = (scsiDev.cdb[1] >> 1) & 0x7;
  618. uint8_t command = scsiDev.cdb[0];
  619. int canRelease =
  620. (!thirdPty && (scsiDev.initiatorId == scsiDev.target->reservedId)) ||
  621. (thirdPty &&
  622. (scsiDev.target->reserverId == scsiDev.initiatorId) &&
  623. (scsiDev.target->reservedId == thirdPtyId)
  624. );
  625. if (extentReservation)
  626. {
  627. // Not supported.
  628. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  629. scsiDev.target->sense.asc = INVALID_FIELD_IN_CDB;
  630. enter_Status(CHECK_CONDITION);
  631. }
  632. else if (command == 0x17) // release
  633. {
  634. if ((scsiDev.target->reservedId < 0) || canRelease)
  635. {
  636. scsiDev.target->reservedId = -1;
  637. scsiDev.target->reserverId = -1;
  638. }
  639. else
  640. {
  641. enter_Status(CONFLICT);
  642. }
  643. }
  644. else // assume reserve.
  645. {
  646. if ((scsiDev.target->reservedId < 0) || canRelease)
  647. {
  648. scsiDev.target->reserverId = scsiDev.initiatorId;
  649. if (thirdPty)
  650. {
  651. scsiDev.target->reservedId = thirdPtyId;
  652. }
  653. else
  654. {
  655. scsiDev.target->reservedId = scsiDev.initiatorId;
  656. }
  657. }
  658. else
  659. {
  660. // Already reserved by someone else!
  661. enter_Status(CONFLICT);
  662. }
  663. }
  664. }
  665. static uint32_t resetUntil = 0;
  666. static void scsiReset()
  667. {
  668. scsiDev.rstCount++;
  669. s2s_ledOff();
  670. scsiPhyReset();
  671. scsiDev.phase = BUS_FREE;
  672. scsiDev.atnFlag = 0;
  673. scsiDev.resetFlag = 0;
  674. scsiDev.selFlag = 0;
  675. scsiDev.lun = -1;
  676. scsiDev.compatMode = COMPAT_UNKNOWN;
  677. if (scsiDev.target)
  678. {
  679. if (scsiDev.target->unitAttention != POWER_ON_RESET)
  680. {
  681. scsiDev.target->unitAttention = SCSI_BUS_RESET;
  682. }
  683. scsiDev.target->reservedId = -1;
  684. scsiDev.target->reserverId = -1;
  685. scsiDev.target->sense.code = NO_SENSE;
  686. scsiDev.target->sense.asc = NO_ADDITIONAL_SENSE_INFORMATION;
  687. }
  688. scsiDev.target = NULL;
  689. for (int i = 0; i < S2S_MAX_TARGETS; ++i)
  690. {
  691. if (g_force_sync > 0)
  692. {
  693. scsiDev.targets[i].syncPeriod = g_force_sync;
  694. scsiDev.targets[i].syncOffset = g_force_offset;
  695. }
  696. else
  697. {
  698. scsiDev.targets[i].syncOffset = 0;
  699. scsiDev.targets[i].syncPeriod = 0;
  700. }
  701. }
  702. scsiDev.minSyncPeriod = 0;
  703. scsiDiskReset();
  704. scsiDev.postDataOutHook = NULL;
  705. scsiDev.sdUnderrunCount = 0;
  706. // Sleep to allow the bus to settle down a bit.
  707. // We must be ready again within the "Reset to selection time" of
  708. // 250ms.
  709. // There is no guarantee that the RST line will be negated by then.
  710. // NOTE: We could be connected and powered by USB for configuration,
  711. // in which case TERMPWR cannot be supplied, and reset will ALWAYS
  712. // be true. Therefore, the sleep here must be slow to avoid slowing
  713. // USB comms
  714. resetUntil = s2s_getTime_ms() + 2; // At least 1ms.
  715. }
  716. static void enter_SelectionPhase()
  717. {
  718. // Ignore stale versions of this flag, but ensure we know the
  719. // current value if the flag is still set.
  720. scsiDev.atnFlag = 0;
  721. scsiDev.dataPtr = 0;
  722. scsiDev.savedDataPtr = 0;
  723. scsiDev.dataLen = 0;
  724. scsiDev.status = GOOD;
  725. scsiDev.phase = SELECTION;
  726. scsiDev.lun = -1;
  727. scsiDev.discPriv = 0;
  728. scsiDev.initiatorId = -1;
  729. scsiDev.target = NULL;
  730. transfer.blocks = 0;
  731. transfer.currentBlock = 0;
  732. scsiDev.postDataOutHook = NULL;
  733. scsiDev.needSyncNegotiationAck = 0;
  734. }
  735. static void process_SelectionPhase()
  736. {
  737. // Selection delays.
  738. // Many SCSI1 samplers that use a 5380 chip need a delay of at least 1ms.
  739. // The Mac Plus boot-time (ie. rom code) selection abort time
  740. // is < 1ms and must have no delay (standard suggests 250ms abort time)
  741. // Most newer SCSI2 hosts don't care either way.
  742. if (scsiDev.target->cfg->quirks == S2S_CFG_QUIRKS_XEBEC)
  743. {
  744. s2s_delay_ms(1); // Simply won't work if set to 0.
  745. }
  746. else if (scsiDev.boardCfg.selectionDelay == 255) // auto
  747. {
  748. if (scsiDev.compatMode < COMPAT_SCSI2)
  749. {
  750. s2s_delay_ms(1);
  751. }
  752. }
  753. else if (scsiDev.boardCfg.selectionDelay != 0)
  754. {
  755. s2s_delay_ms(scsiDev.boardCfg.selectionDelay);
  756. }
  757. uint8_t selStatus = *SCSI_STS_SELECTED;
  758. if ((selStatus == 0) && (scsiDev.boardCfg.flags & S2S_CFG_ENABLE_SEL_LATCH))
  759. {
  760. selStatus = scsiDev.selFlag;
  761. }
  762. int tgtIndex;
  763. TargetState* target = NULL;
  764. for (tgtIndex = 0; tgtIndex < S2S_MAX_TARGETS; ++tgtIndex)
  765. {
  766. if (scsiDev.targets[tgtIndex].targetId == (selStatus & 7))
  767. {
  768. target = &scsiDev.targets[tgtIndex];
  769. break;
  770. }
  771. }
  772. if ((target != NULL) && (selStatus & 0x40))
  773. {
  774. // We've been selected!
  775. // Assert BSY - Selection success!
  776. // must happen within 200us (Selection abort time) of seeing our
  777. // ID + SEL.
  778. // (Note: the initiator will be waiting the "Selection time-out delay"
  779. // for our BSY response, which is actually a very generous 250ms)
  780. *SCSI_CTRL_BSY = 1;
  781. s2s_ledOn();
  782. scsiDev.target = target;
  783. // Do we enter MESSAGE OUT immediately ? SCSI 1 and 2 standards says
  784. // move to MESSAGE OUT if ATN is true before we assert BSY.
  785. // The initiator should assert ATN with SEL.
  786. scsiDev.atnFlag = selStatus & 0x80;
  787. // Unit attention breaks many older SCSI hosts. Disable it completely
  788. // for SCSI-1 (and older) hosts, regardless of our configured setting.
  789. // Enable the compatability mode also as many SASI and SCSI1
  790. // controllers don't generate parity bits.
  791. if (!scsiDev.atnFlag)
  792. {
  793. target->unitAttention = 0;
  794. scsiDev.compatMode = COMPAT_SCSI1;
  795. }
  796. else if (!(scsiDev.boardCfg.flags & S2S_CFG_ENABLE_SCSI2))
  797. {
  798. scsiDev.compatMode = COMPAT_SCSI2_DISABLED;
  799. }
  800. else
  801. {
  802. scsiDev.compatMode = COMPAT_SCSI2;
  803. }
  804. scsiDev.selCount++;
  805. // Save our initiator now that we're no longer in a time-critical
  806. // section.
  807. // SCSI1/SASI initiators may not set their own ID.
  808. scsiDev.initiatorId = (selStatus >> 3) & 0x7;
  809. // Wait until the end of the selection phase.
  810. uint32_t selTimerBegin = s2s_getTime_ms();
  811. while (likely(!scsiDev.resetFlag))
  812. {
  813. if (!scsiStatusSEL())
  814. {
  815. break;
  816. }
  817. else if (s2s_elapsedTime_ms(selTimerBegin) >= 10 &&
  818. scsiDev.target->cfg->quirks == S2S_CFG_QUIRKS_XEBEC)
  819. {
  820. // XEBEC hosts may not bother releasing SEL at all until
  821. // just before the command ends.
  822. break;
  823. }
  824. else if (s2s_elapsedTime_ms(selTimerBegin) >= 250)
  825. {
  826. *SCSI_CTRL_BSY = 0;
  827. scsiDev.resetFlag = 1;
  828. break;
  829. }
  830. }
  831. scsiDev.phase = COMMAND;
  832. }
  833. else if (!selStatus)
  834. {
  835. scsiDev.phase = BUS_BUSY;
  836. }
  837. scsiDev.selFlag = 0;
  838. }
  839. static void process_MessageOut()
  840. {
  841. int wasNeedSyncNegotiationAck = scsiDev.needSyncNegotiationAck;
  842. scsiDev.needSyncNegotiationAck = 0; // Successful on -most- messages.
  843. scsiEnterPhase(MESSAGE_OUT);
  844. scsiDev.atnFlag = 0;
  845. scsiDev.msgOut = scsiReadByte();
  846. scsiDev.msgCount++;
  847. if (scsiParityError() &&
  848. (scsiDev.boardCfg.flags & S2S_CFG_ENABLE_PARITY))
  849. {
  850. // Skip the remaining message bytes, and then start the MESSAGE_OUT
  851. // phase again from the start. The initiator will re-send the
  852. // same set of messages.
  853. while (scsiStatusATN() && !scsiDev.resetFlag)
  854. {
  855. scsiReadByte();
  856. }
  857. // Go-back and try the message again.
  858. scsiDev.atnFlag = 1;
  859. }
  860. else if (scsiDev.msgOut == 0x00)
  861. {
  862. // COMMAND COMPLETE. but why would the target be receiving this ? nfi.
  863. enter_BusFree();
  864. }
  865. else if (scsiDev.msgOut == 0x06)
  866. {
  867. // ABORT
  868. scsiDiskReset();
  869. enter_BusFree();
  870. }
  871. else if (scsiDev.msgOut == 0x0C)
  872. {
  873. // BUS DEVICE RESET
  874. scsiDiskReset();
  875. scsiDev.target->unitAttention = SCSI_BUS_RESET;
  876. // ANY initiator can reset the reservation state via this message.
  877. scsiDev.target->reservedId = -1;
  878. scsiDev.target->reserverId = -1;
  879. // Cancel any sync negotiation
  880. scsiDev.target->syncOffset = 0;
  881. scsiDev.target->syncPeriod = 0;
  882. enter_BusFree();
  883. }
  884. else if (scsiDev.msgOut == 0x05)
  885. {
  886. // Initiator Detected Error
  887. // Ignore for now
  888. }
  889. else if (scsiDev.msgOut == 0x0F)
  890. {
  891. // INITIATE RECOVERY
  892. // Ignore for now
  893. }
  894. else if (scsiDev.msgOut == 0x10)
  895. {
  896. // RELEASE RECOVERY
  897. // Ignore for now
  898. enter_BusFree();
  899. }
  900. else if (scsiDev.msgOut == MSG_REJECT)
  901. {
  902. // Message Rejected
  903. // Oh well.
  904. if (wasNeedSyncNegotiationAck)
  905. {
  906. scsiDev.target->syncOffset = 0;
  907. scsiDev.target->syncPeriod = 0;
  908. }
  909. }
  910. else if (scsiDev.msgOut == 0x08)
  911. {
  912. // NOP
  913. }
  914. else if (scsiDev.msgOut == 0x09)
  915. {
  916. // Message Parity Error
  917. // Go back and re-send the last message.
  918. scsiDev.phase = MESSAGE_IN;
  919. if (wasNeedSyncNegotiationAck)
  920. {
  921. scsiDev.target->syncOffset = 0;
  922. scsiDev.target->syncPeriod = 0;
  923. }
  924. }
  925. else if (scsiDev.msgOut & 0x80) // 0x80 -> 0xFF
  926. {
  927. // IDENTIFY
  928. if ((scsiDev.msgOut & 0x18) || // Reserved bits set.
  929. (scsiDev.msgOut & 0x20)) // We don't have any target routines!
  930. {
  931. messageReject();
  932. }
  933. scsiDev.lun = scsiDev.msgOut & 0x7;
  934. scsiDev.discPriv =
  935. ((scsiDev.msgOut & 0x40) && (scsiDev.initiatorId >= 0))
  936. ? 1 : 0;
  937. }
  938. else if (scsiDev.msgOut >= 0x20 && scsiDev.msgOut <= 0x2F)
  939. {
  940. // Two byte message. We don't support these. read and discard.
  941. scsiReadByte();
  942. if (scsiDev.msgOut == 0x23) {
  943. // Ignore Wide Residue. We're only 8 bit anyway.
  944. } else {
  945. messageReject();
  946. }
  947. }
  948. else if (scsiDev.msgOut == 0x01)
  949. {
  950. int i;
  951. // Extended message. These include speed negotiation
  952. int msgLen = scsiReadByte();
  953. if (msgLen == 0) msgLen = 256;
  954. uint8_t extmsg[256];
  955. for (i = 0; i < msgLen && !scsiDev.resetFlag; ++i)
  956. {
  957. // Discard bytes.
  958. extmsg[i] = scsiReadByte();
  959. }
  960. if (extmsg[0] == 3 && msgLen == 2) // Wide Data Request
  961. {
  962. // Negotiate down to 8bit
  963. scsiEnterPhase(MESSAGE_IN);
  964. static const uint8_t WDTR[] = {0x01, 0x02, 0x03, 0x00};
  965. scsiWrite(WDTR, sizeof(WDTR));
  966. // SDTR becomes invalidated.
  967. scsiDev.target->syncOffset = 0;
  968. scsiDev.target->syncPeriod = 0;
  969. }
  970. else if (extmsg[0] == 1 && msgLen == 3) // Synchronous data request
  971. {
  972. int oldPeriod = scsiDev.target->syncPeriod;
  973. int oldOffset = scsiDev.target->syncOffset;
  974. int transferPeriod = extmsg[1]; //This is actually 1/4 of the duration of transfer speed in ns
  975. int offset = extmsg[2]; //Req/Ack offset
  976. if ((
  977. (transferPeriod > 0) &&
  978. (transferPeriod < scsiDev.minSyncPeriod)) ||
  979. (scsiDev.minSyncPeriod == 0))
  980. {
  981. scsiDev.minSyncPeriod = transferPeriod;
  982. }
  983. if ((transferPeriod > 80) || // 320ns, 3.125MB/s
  984. // Amiga A590 (WD33C93 chip) only does 3.5MB/s sync
  985. // After 80 we start to run out of bits in the fpga timing
  986. // register.
  987. (transferPeriod == 0) ||
  988. (offset == 0) ||
  989. ((scsiDev.boardCfg.scsiSpeed != S2S_CFG_SPEED_NoLimit) &&
  990. (scsiDev.boardCfg.scsiSpeed <= S2S_CFG_SPEED_ASYNC_50)))
  991. {
  992. scsiDev.target->syncOffset = 0;
  993. scsiDev.target->syncPeriod = 0;
  994. } else {
  995. scsiDev.target->syncOffset = offset <= 15 ? offset : 15;
  996. // FAST20 / 50ns / 20MHz is disabled for now due to
  997. // data corruption while reading data. We can count the
  998. // ACK's correctly, but can't save the data to a register
  999. // before it changes. (ie. transferPeriod == 12)
  1000. //****TODO Try to get higher speeds
  1001. //The Adaptec AHA2940-UW seems to support 10, 13.4, 16 and 20 MHz
  1002. //The speeds above correspond to syncPeriod values of 25, 18, 15 and 12 (maybe, the last 3 are truncated)
  1003. //We will set the syncPeriod and syncOffset to the fastest we
  1004. //can support if the initiator requests a faster speed
  1005. if (scsiDev.boardCfg.scsiSpeed == S2S_CFG_SPEED_SYNC_20 || scsiDev.boardCfg.scsiSpeed == S2S_CFG_SPEED_NoLimit)
  1006. {
  1007. if (transferPeriod <= g_max_sync_20_period)
  1008. scsiDev.target->syncPeriod = g_max_sync_20_period;
  1009. else
  1010. scsiDev.target->syncPeriod = transferPeriod;
  1011. }
  1012. else if (scsiDev.boardCfg.scsiSpeed >= S2S_CFG_SPEED_SYNC_10)
  1013. {
  1014. if (transferPeriod <= g_max_sync_10_period)
  1015. scsiDev.target->syncPeriod = g_max_sync_10_period;
  1016. else
  1017. scsiDev.target->syncPeriod = transferPeriod;
  1018. }
  1019. else if (scsiDev.boardCfg.scsiSpeed == S2S_CFG_SPEED_SYNC_5)
  1020. {
  1021. if (transferPeriod <= g_max_sync_5_period)
  1022. scsiDev.target->syncPeriod = g_max_sync_5_period;
  1023. else
  1024. scsiDev.target->syncPeriod = transferPeriod;
  1025. }
  1026. }
  1027. //Reply back with negotiation speed (if different from previous one)
  1028. //Reply should be the same as request (if we support it) or slower if we don't
  1029. //Slower in this context means a higher syncPeriod or lower syncOffset
  1030. if (transferPeriod != oldPeriod ||
  1031. scsiDev.target->syncPeriod != oldPeriod ||
  1032. offset != oldOffset ||
  1033. scsiDev.target->syncOffset != oldOffset ||
  1034. !wasNeedSyncNegotiationAck) // Don't get into infinite loops negotiating.
  1035. {
  1036. scsiEnterPhase(MESSAGE_IN);
  1037. uint8_t SDTR[] = {0x01, 0x03, 0x01, scsiDev.target->syncPeriod, scsiDev.target->syncOffset};
  1038. scsiWrite(SDTR, sizeof(SDTR));
  1039. scsiDev.needSyncNegotiationAck = 1; // Check if this message is rejected.
  1040. scsiDev.sdUnderrunCount = 0; // reset counter, may work now.
  1041. // Set to the theoretical speed, then adjust if we measure lower
  1042. // actual speeds.
  1043. scsiDev.hostSpeedKBs = s2s_getScsiRateKBs();
  1044. scsiDev.hostSpeedMeasured = 0;
  1045. }
  1046. }
  1047. else
  1048. {
  1049. // Not supported
  1050. messageReject();
  1051. }
  1052. }
  1053. else
  1054. {
  1055. messageReject();
  1056. }
  1057. // Re-check the ATN flag in case it stays asserted.
  1058. scsiDev.atnFlag |= scsiStatusATN();
  1059. if (!scsiDev.atnFlag)
  1060. {
  1061. // Message wasn't rejected!
  1062. scsiDev.needSyncNegotiationAck = 0;
  1063. }
  1064. }
  1065. void scsiPoll(void)
  1066. {
  1067. if (resetUntil != 0 && resetUntil > s2s_getTime_ms())
  1068. {
  1069. return;
  1070. }
  1071. resetUntil = 0;
  1072. if (unlikely(scsiDev.resetFlag))
  1073. {
  1074. scsiReset();
  1075. // Still in reset phase for a few ms.
  1076. // Do not try and process any commands.
  1077. return;
  1078. }
  1079. switch (scsiDev.phase)
  1080. {
  1081. case BUS_FREE:
  1082. if (scsiStatusBSY())
  1083. {
  1084. scsiDev.phase = BUS_BUSY;
  1085. }
  1086. // The Arbitration phase is optional for SCSI1/SASI hosts if there is only
  1087. // one initiator in the chain. Support this by moving
  1088. // straight to selection if SEL is asserted.
  1089. // ie. the initiator won't assert BSY and it's own ID before moving to selection.
  1090. else if (scsiDev.selFlag || *SCSI_STS_SELECTED)
  1091. {
  1092. enter_SelectionPhase();
  1093. }
  1094. break;
  1095. case BUS_BUSY:
  1096. // Someone is using the bus. Perhaps they are trying to
  1097. // select us.
  1098. if (scsiDev.selFlag || *SCSI_STS_SELECTED)
  1099. {
  1100. enter_SelectionPhase();
  1101. }
  1102. else if (!scsiStatusBSY())
  1103. {
  1104. scsiDev.phase = BUS_FREE;
  1105. }
  1106. break;
  1107. case ARBITRATION:
  1108. // TODO Support reselection.
  1109. break;
  1110. case SELECTION:
  1111. process_SelectionPhase();
  1112. break;
  1113. case RESELECTION:
  1114. // Not currently supported!
  1115. break;
  1116. case COMMAND:
  1117. // Do not check ATN here. SCSI 1 & 2 initiators must set ATN
  1118. // and SEL together upon entering the selection phase if they
  1119. // want to send a message (IDENTIFY) immediately.
  1120. if (scsiDev.atnFlag)
  1121. {
  1122. process_MessageOut();
  1123. }
  1124. else
  1125. {
  1126. process_Command();
  1127. }
  1128. break;
  1129. case DATA_IN:
  1130. scsiDev.atnFlag |= scsiStatusATN();
  1131. if (scsiDev.atnFlag)
  1132. {
  1133. process_MessageOut();
  1134. }
  1135. else
  1136. {
  1137. process_DataIn();
  1138. }
  1139. break;
  1140. case DATA_OUT:
  1141. scsiDev.atnFlag |= scsiStatusATN();
  1142. if (scsiDev.atnFlag)
  1143. {
  1144. process_MessageOut();
  1145. }
  1146. else
  1147. {
  1148. process_DataOut();
  1149. }
  1150. break;
  1151. case STATUS:
  1152. scsiDev.atnFlag |= scsiStatusATN();
  1153. if (scsiDev.atnFlag)
  1154. {
  1155. process_MessageOut();
  1156. }
  1157. else
  1158. {
  1159. process_Status();
  1160. }
  1161. break;
  1162. case MESSAGE_IN:
  1163. scsiDev.atnFlag |= scsiStatusATN();
  1164. if (scsiDev.atnFlag)
  1165. {
  1166. process_MessageOut();
  1167. }
  1168. else
  1169. {
  1170. process_MessageIn(1);
  1171. }
  1172. break;
  1173. case MESSAGE_OUT:
  1174. process_MessageOut();
  1175. break;
  1176. }
  1177. }
  1178. void scsiInit()
  1179. {
  1180. static int firstInit = 1;
  1181. scsiDev.atnFlag = 0;
  1182. scsiDev.resetFlag = 1;
  1183. scsiDev.selFlag = 0;
  1184. scsiDev.phase = BUS_FREE;
  1185. scsiDev.target = NULL;
  1186. scsiDev.compatMode = COMPAT_UNKNOWN;
  1187. scsiDev.hostSpeedKBs = 0;
  1188. scsiDev.hostSpeedMeasured = 0;
  1189. int i;
  1190. for (i = 0; i < S2S_MAX_TARGETS; ++i)
  1191. {
  1192. const S2S_TargetCfg* cfg = s2s_getConfigByIndex(i);
  1193. if (cfg && (cfg->scsiId & S2S_CFG_TARGET_ENABLED))
  1194. {
  1195. scsiDev.targets[i].targetId = cfg->scsiId & S2S_CFG_TARGET_ID_BITS;
  1196. scsiDev.targets[i].cfg = cfg;
  1197. scsiDev.targets[i].liveCfg.bytesPerSector = cfg->bytesPerSector;
  1198. }
  1199. else
  1200. {
  1201. scsiDev.targets[i].targetId = 0xff;
  1202. scsiDev.targets[i].cfg = NULL;
  1203. }
  1204. scsiDev.targets[i].reservedId = -1;
  1205. scsiDev.targets[i].reserverId = -1;
  1206. if (firstInit)
  1207. {
  1208. if ((cfg->deviceType == S2S_CFG_MO) && (scsiDev.target->cfg->quirks == S2S_CFG_QUIRKS_EWSD))
  1209. {
  1210. scsiDev.targets[i].unitAttention = POWER_ON_RESET_OR_BUS_DEVICE_RESET_OCCURRED;
  1211. } else
  1212. {
  1213. scsiDev.targets[i].unitAttention = POWER_ON_RESET;
  1214. }
  1215. }
  1216. else
  1217. {
  1218. scsiDev.targets[i].unitAttention = PARAMETERS_CHANGED;
  1219. }
  1220. scsiDev.targets[i].sense.code = NO_SENSE;
  1221. scsiDev.targets[i].sense.asc = NO_ADDITIONAL_SENSE_INFORMATION;
  1222. if (g_force_sync > 0)
  1223. {
  1224. scsiDev.targets[i].syncPeriod = g_force_sync;
  1225. scsiDev.targets[i].syncOffset = g_force_offset;
  1226. }
  1227. else
  1228. {
  1229. scsiDev.targets[i].syncOffset = 0;
  1230. scsiDev.targets[i].syncPeriod = 0;
  1231. }
  1232. // Always "start" the device. Many systems (eg. Apple System 7)
  1233. // won't respond properly to
  1234. // LOGICAL_UNIT_NOT_READY_INITIALIZING_COMMAND_REQUIRED sense
  1235. // code
  1236. scsiDev.targets[i].started = 1;
  1237. }
  1238. firstInit = 0;
  1239. }
  1240. /* TODO REENABLE
  1241. void scsiDisconnect()
  1242. {
  1243. scsiEnterPhase(MESSAGE_IN);
  1244. scsiWriteByte(0x02); // save data pointer
  1245. scsiWriteByte(0x04); // disconnect msg.
  1246. // For now, the caller is responsible for tracking the disconnected
  1247. // state, and calling scsiReconnect.
  1248. // Ideally the client would exit their loop and we'd implement this
  1249. // as part of scsiPoll
  1250. int phase = scsiDev.phase;
  1251. enter_BusFree();
  1252. scsiDev.phase = phase;
  1253. }
  1254. */
  1255. /* TODO REENABLE
  1256. int scsiReconnect()
  1257. {
  1258. int reconnected = 0;
  1259. int sel = SCSI_ReadFilt(SCSI_Filt_SEL);
  1260. int bsy = SCSI_ReadFilt(SCSI_Filt_BSY);
  1261. if (!sel && !bsy)
  1262. {
  1263. s2s_delay_us(1);
  1264. sel = SCSI_ReadFilt(SCSI_Filt_SEL);
  1265. bsy = SCSI_ReadFilt(SCSI_Filt_BSY);
  1266. }
  1267. if (!sel && !bsy)
  1268. {
  1269. // Arbitrate.
  1270. s2s_ledOn();
  1271. uint8_t scsiIdMask = 1 << scsiDev.target->targetId;
  1272. SCSI_Out_Bits_Write(scsiIdMask);
  1273. SCSI_Out_Ctl_Write(1); // Write bits manually.
  1274. SCSI_SetPin(SCSI_Out_BSY);
  1275. s2s_delay_us(3); // arbitrate delay. 2.4us.
  1276. uint8_t dbx = scsiReadDBxPins();
  1277. sel = SCSI_ReadFilt(SCSI_Filt_SEL);
  1278. if (sel || ((dbx ^ scsiIdMask) > scsiIdMask))
  1279. {
  1280. // Lost arbitration.
  1281. SCSI_Out_Ctl_Write(0);
  1282. SCSI_ClearPin(SCSI_Out_BSY);
  1283. s2s_ledOff();
  1284. }
  1285. else
  1286. {
  1287. // Won arbitration
  1288. SCSI_SetPin(SCSI_Out_SEL);
  1289. s2s_delay_us(1); // Bus clear + Bus settle.
  1290. // Reselection phase
  1291. SCSI_CTL_PHASE_Write(__scsiphase_io);
  1292. SCSI_Out_Bits_Write(scsiIdMask | (1 << scsiDev.initiatorId));
  1293. scsiDeskewDelay(); // 2 deskew delays
  1294. scsiDeskewDelay(); // 2 deskew delays
  1295. SCSI_ClearPin(SCSI_Out_BSY);
  1296. s2s_delay_us(1); // Bus Settle Delay
  1297. uint32_t waitStart_ms = getTime_ms();
  1298. bsy = SCSI_ReadFilt(SCSI_Filt_BSY);
  1299. // Wait for initiator.
  1300. while (
  1301. !bsy &&
  1302. !scsiDev.resetFlag &&
  1303. (elapsedTime_ms(waitStart_ms) < 250))
  1304. {
  1305. bsy = SCSI_ReadFilt(SCSI_Filt_BSY);
  1306. }
  1307. if (bsy)
  1308. {
  1309. SCSI_SetPin(SCSI_Out_BSY);
  1310. scsiDeskewDelay(); // 2 deskew delays
  1311. scsiDeskewDelay(); // 2 deskew delays
  1312. SCSI_ClearPin(SCSI_Out_SEL);
  1313. // Prepare for the initial IDENTIFY message.
  1314. SCSI_Out_Ctl_Write(0);
  1315. scsiEnterPhase(MESSAGE_IN);
  1316. // Send identify command
  1317. scsiWriteByte(0x80);
  1318. scsiEnterPhase(scsiDev.phase);
  1319. reconnected = 1;
  1320. }
  1321. else
  1322. {
  1323. // reselect timeout.
  1324. SCSI_Out_Ctl_Write(0);
  1325. SCSI_ClearPin(SCSI_Out_SEL);
  1326. SCSI_CTL_PHASE_Write(0);
  1327. s2s_ledOff();
  1328. }
  1329. }
  1330. }
  1331. return reconnected;
  1332. }
  1333. */