scsiHostPhy.cpp 9.4 KB

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