scsi.c 34 KB

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