scsiHostPhy.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /**
  2. * ZuluSCSI™ - Copyright (c) 2022-2025 Rabbit Hole Computing™
  3. *
  4. * ZuluSCSI™ firmware is licensed under the GPL version 3 or any later version. 
  5. *
  6. * https://www.gnu.org/licenses/gpl-3.0.html
  7. * ----
  8. * This program 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. * This program 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 this program.  If not, see <https://www.gnu.org/licenses/>.
  20. **/
  21. #include "scsiHostPhy.h"
  22. #include "BlueSCSI_platform.h"
  23. #include "BlueSCSI_log.h"
  24. #include "BlueSCSI_log_trace.h"
  25. #include "scsi_accel_host.h"
  26. #include <assert.h>
  27. #include <scsi2sd.h>
  28. extern "C" {
  29. #include <scsi.h>
  30. }
  31. volatile int g_scsiHostPhyReset;
  32. bool perform_parity_checking = true;
  33. #ifndef PLATFORM_HAS_INITIATOR_MODE
  34. // Dummy functions for platforms without hardware support for
  35. // SCSI initiator mode.
  36. void scsiHostPhyReset(void) {}
  37. bool scsiHostPhySelect(int target_id) { return false; }
  38. int scsiHostPhyGetPhase() { return 0; }
  39. bool scsiHostRequestWaiting() { return false; }
  40. uint32_t scsiHostWrite(const uint8_t *data, uint32_t count) { return 0; }
  41. uint32_t scsiHostRead(uint8_t *data, uint32_t count) { return 0; }
  42. void scsiHostPhyRelease();
  43. #else
  44. // Release bus and pulse RST signal, initialize PHY to host mode.
  45. void scsiHostPhyReset(void)
  46. {
  47. SCSI_RELEASE_OUTPUTS();
  48. SCSI_ENABLE_INITIATOR();
  49. scsi_accel_host_init();
  50. SCSI_OUT(RST, 1);
  51. delay(2);
  52. SCSI_OUT(RST, 0);
  53. delay(250);
  54. g_scsiHostPhyReset = false;
  55. }
  56. // Select a device and an initiator, ids 0-7.
  57. // Returns true if the target answers to selection request.
  58. bool scsiHostPhySelect(int target_id, uint8_t initiator_id)
  59. {
  60. SCSI_ENABLE_INITIATOR();
  61. SCSI_RELEASE_OUTPUTS();
  62. // We can't write individual data bus bits, so use a bit modified
  63. // arbitration scheme. We always yield to any other initiator on
  64. // the bus.
  65. scsiLogInitiatorPhaseChange(BUS_BUSY);
  66. SCSI_OUT(REQ, 0);
  67. SCSI_OUT(BSY, 1);
  68. for (int wait = 0; wait < 10; wait++)
  69. {
  70. delayMicroseconds(1);
  71. if (SCSI_IN_DATA() != 0)
  72. {
  73. dbgmsg("scsiHostPhySelect: bus is busy");
  74. scsiLogInitiatorPhaseChange(BUS_FREE);
  75. SCSI_RELEASE_OUTPUTS();
  76. return false;
  77. }
  78. }
  79. // Selection phase
  80. scsiLogInitiatorPhaseChange(SELECTION);
  81. dbgmsg("------ SELECTING ", target_id, " with initiator ID ", (int)initiator_id);
  82. SCSI_OUT(SEL, 1);
  83. delayMicroseconds(5);
  84. SCSI_OUT_DATA((1 << target_id) | (1 << initiator_id));
  85. delayMicroseconds(5);
  86. SCSI_OUT(BSY, 0);
  87. // Wait for target to respond
  88. for (int wait = 0; wait < 2500; wait++)
  89. {
  90. delayMicroseconds(100);
  91. if (SCSI_IN(BSY))
  92. {
  93. break;
  94. }
  95. }
  96. if (!SCSI_IN(BSY))
  97. {
  98. // No response
  99. SCSI_RELEASE_OUTPUTS();
  100. return false;
  101. }
  102. // We need to assert OUT_BSY to enable IO buffer U105 to read status signals.
  103. SCSI_RELEASE_DATA_REQ();
  104. // SCSI_OUT(BSY, 1);
  105. SCSI_OUT(SEL, 0);
  106. SCSI_ENABLE_INITIATOR();
  107. return true;
  108. }
  109. // Read the current communication phase as signaled by the target
  110. int scsiHostPhyGetPhase()
  111. {
  112. static absolute_time_t last_online_time;
  113. if (g_scsiHostPhyReset)
  114. {
  115. // Reset request from watchdog timer
  116. scsiHostPhyRelease();
  117. return BUS_FREE;
  118. }
  119. int phase = 0;
  120. bool req_in = SCSI_IN(REQ);
  121. if (SCSI_IN(CD)) phase |= __scsiphase_cd;
  122. if (SCSI_IN(IO)) phase |= __scsiphase_io;
  123. if (SCSI_IN(MSG)) phase |= __scsiphase_msg;
  124. if (phase == 0 && absolute_time_diff_us(last_online_time, get_absolute_time()) > 100)
  125. {
  126. // BlueSCSI doesn't need to assert OUT_BSY to check whether the bus is in use
  127. // SCSI_OUT(BSY, 0);
  128. delayMicroseconds(1);
  129. if (!SCSI_IN(BSY))
  130. {
  131. scsiLogInitiatorPhaseChange(BUS_FREE);
  132. return BUS_FREE;
  133. }
  134. // Still online, re-enable OUT_BSY to enable IO buffers
  135. // SCSI_OUT(BSY, 1);
  136. last_online_time = get_absolute_time();
  137. }
  138. else if (phase != 0)
  139. {
  140. last_online_time = get_absolute_time();
  141. }
  142. if (!req_in)
  143. {
  144. // Don't act on phase changes until target asserts request signal.
  145. // This filters out any spurious changes on control signals.
  146. return BUS_BUSY;
  147. }
  148. else
  149. {
  150. scsiLogInitiatorPhaseChange(phase);
  151. return phase;
  152. }
  153. }
  154. bool scsiHostRequestWaiting()
  155. {
  156. return SCSI_IN(REQ);
  157. }
  158. // Blocking data transfer
  159. #define SCSIHOST_WAIT_ACTIVE(pin) \
  160. if (!SCSI_IN(pin)) { \
  161. if (!SCSI_IN(pin)) { \
  162. while(!SCSI_IN(pin) && !g_scsiHostPhyReset); \
  163. } \
  164. }
  165. #define SCSIHOST_WAIT_INACTIVE(pin) \
  166. if (SCSI_IN(pin)) { \
  167. if (SCSI_IN(pin)) { \
  168. while(SCSI_IN(pin) && !g_scsiHostPhyReset); \
  169. } \
  170. }
  171. // Write one byte to SCSI target using the handshake mechanism
  172. static inline void scsiHostWriteOneByte(uint8_t value)
  173. {
  174. SCSIHOST_WAIT_ACTIVE(REQ);
  175. SCSI_OUT_DATA(value);
  176. delay_100ns(); // DB setup time before ACK
  177. SCSI_OUT(ACK, 1);
  178. SCSIHOST_WAIT_INACTIVE(REQ);
  179. SCSI_RELEASE_DATA_REQ();
  180. SCSI_OUT(ACK, 0);
  181. }
  182. // Read one byte from SCSI target using the handshake mechanism.
  183. static inline uint8_t scsiHostReadOneByte(int* parityError)
  184. {
  185. SCSIHOST_WAIT_ACTIVE(REQ);
  186. uint16_t r = SCSI_IN_DATA();
  187. SCSI_OUT(ACK, 1);
  188. SCSIHOST_WAIT_INACTIVE(REQ);
  189. SCSI_OUT(ACK, 0);
  190. if (parityError && r != (g_scsi_parity_lookup[r & 0xFF] ^ (SCSI_IO_DATA_MASK >> SCSI_IO_SHIFT)))
  191. {
  192. logmsg("Parity error in scsiReadOneByte(): ", (uint32_t)r);
  193. *parityError = 1;
  194. }
  195. return (uint8_t)r;
  196. }
  197. uint32_t scsiHostWrite(const uint8_t *data, uint32_t count)
  198. {
  199. scsiLogDataOut(data, count);
  200. int cd_start = SCSI_IN(CD);
  201. int msg_start = SCSI_IN(MSG);
  202. for (uint32_t i = 0; i < count; i++)
  203. {
  204. while (!SCSI_IN(REQ))
  205. {
  206. if (g_scsiHostPhyReset || SCSI_IN(IO) || SCSI_IN(CD) != cd_start || SCSI_IN(MSG) != msg_start)
  207. {
  208. // Target switched out of DATA_OUT mode
  209. logmsg("scsiHostWrite: sent ", (int)i, " bytes, expected ", (int)count);
  210. return i;
  211. }
  212. }
  213. scsiHostWriteOneByte(data[i]);
  214. }
  215. return count;
  216. }
  217. uint32_t scsiHostRead(uint8_t *data, uint32_t count)
  218. {
  219. int parityError = 0;
  220. uint32_t fullcount = count;
  221. int cd_start = SCSI_IN(CD);
  222. int msg_start = SCSI_IN(MSG);
  223. if ((count & 1) == 0 && ((uint32_t)data & 1) == 0)
  224. {
  225. // Even number of bytes, use accelerated routine
  226. count = scsi_accel_host_read(data, count, &parityError, &g_scsiHostPhyReset);
  227. }
  228. else
  229. {
  230. for (uint32_t i = 0; i < count; i++)
  231. {
  232. uint32_t start = millis();
  233. while (!SCSI_IN(REQ) && (millis() - start) < 10000)
  234. {
  235. // Wait for REQ asserted
  236. }
  237. int io = SCSI_IN(IO);
  238. int cd = SCSI_IN(CD);
  239. int msg = SCSI_IN(MSG);
  240. if (g_scsiHostPhyReset)
  241. {
  242. dbgmsg("sciHostRead: aborting due to reset request");
  243. count = i;
  244. break;
  245. }
  246. else if (!io || cd != cd_start || msg != msg_start)
  247. {
  248. dbgmsg("scsiHostRead: aborting because target switched transfer phase (IO: ", io, ", CD: ", cd, ", MSG: ", msg, ")");
  249. count = i;
  250. break;
  251. }
  252. data[i] = scsiHostReadOneByte(&parityError);
  253. }
  254. }
  255. scsiLogDataIn(data, count);
  256. if (g_scsiHostPhyReset || parityError)
  257. {
  258. return 0;
  259. }
  260. else
  261. {
  262. if (count < fullcount)
  263. {
  264. logmsg("scsiHostRead: received ", (int)count, " bytes, expected ", (int)fullcount);
  265. }
  266. return count;
  267. }
  268. }
  269. // Release bus signals and expect the target to do the same.
  270. // Cycles ACK in case target still holds BSY and REQ.
  271. void scsiHostWaitBusFree()
  272. {
  273. SCSI_RELEASE_OUTPUTS();
  274. sleep_us(2);
  275. // Wait for the target to release BSY signal.
  276. // If the target is expecting more data, transfer dummy bytes.
  277. // This happens for some reason with READ6 command on IBM H3171-S2.
  278. uint32_t start = millis();
  279. int extra_bytes = 0;
  280. while (SCSI_IN(BSY))
  281. {
  282. platform_poll();
  283. if (SCSI_IN(REQ))
  284. {
  285. // Target is expecting something more
  286. // Transfer dummy bytes
  287. SCSI_OUT(BSY, 1);
  288. sleep_us(1);
  289. while (SCSI_IN(REQ))
  290. {
  291. scsiHostReadOneByte(nullptr);
  292. extra_bytes++;
  293. sleep_us(1);
  294. }
  295. SCSI_OUT(BSY, 0);
  296. sleep_us(1);
  297. }
  298. if ((uint32_t)(millis() - start) > 10000)
  299. {
  300. logmsg("Target is holding BSY for unexpectedly long, running reset.");
  301. scsiHostPhyReset();
  302. break;
  303. }
  304. }
  305. if (extra_bytes > 0)
  306. {
  307. dbgmsg("---- Target requested ", extra_bytes, " extra bytes after command complete");
  308. }
  309. scsiHostPhyRelease();
  310. }
  311. // Release all bus signals
  312. void scsiHostPhyRelease()
  313. {
  314. scsiLogInitiatorPhaseChange(BUS_FREE);
  315. SCSI_RELEASE_OUTPUTS();
  316. }
  317. void setInitiatorModeParityCheck(const bool checkParity) {
  318. perform_parity_checking = checkParity;
  319. }
  320. #endif