scsi.c 33 KB

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