scsi.c 34 KB

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