scsi.c 35 KB

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