scsi.c 28 KB

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