scsi.c 37 KB

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