scsi.c 34 KB

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