scsi.c 33 KB

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