scsi.c 30 KB

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