scsi.c 33 KB

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