scsi.c 34 KB

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