scsi.c 35 KB

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