ZuluSCSI_log_trace.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /**
  2. * ZuluSCSI™ - Copyright (c) 2022 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. // SCSI trace logging
  22. #include "ZuluSCSI_log_trace.h"
  23. #include "ZuluSCSI_log.h"
  24. #include <scsi2sd.h>
  25. extern "C" {
  26. #include <scsi.h>
  27. #include <scsiPhy.h>
  28. }
  29. static bool g_LogData = false;
  30. static bool g_LogInitiatorCommand = false;
  31. static int g_InByteCount = 0;
  32. static int g_OutByteCount = 0;
  33. static uint16_t g_DataChecksum = 0;
  34. static const char *getCommandName(uint8_t cmd)
  35. {
  36. switch (cmd)
  37. {
  38. case 0x00: return "TestUnitReady";
  39. case 0x01: return "RezeroUnit";
  40. case 0x03: return "RequestSense";
  41. case 0x04: return "FormatUnit";
  42. case 0x08: return "Read6";
  43. case 0x0A: return "Write6";
  44. case 0x0B: return "Seek6";
  45. case 0x0F: return "WriteSectorBuffer";
  46. case 0x12: return "Inquiry";
  47. case 0x15: return "ModeSelect6";
  48. case 0x16: return "Reserve";
  49. case 0x17: return "Release";
  50. case 0x1A: return "ModeSense";
  51. case 0x1B: return "StartStopUnit";
  52. case 0x1C: return "ReceiveDiagnostic";
  53. case 0x1D: return "SendDiagnostic";
  54. case 0x1E: return "PreventAllowMediumRemoval";
  55. case 0x25: return "ReadCapacity";
  56. case 0x28: return "Read10";
  57. case 0x2A: return "Write10";
  58. case 0x2B: return "Seek10";
  59. case 0x2C: return "Erase10";
  60. case 0x2E: return "WriteVerify";
  61. case 0x2F: return "Verify";
  62. case 0x34: return "PreFetch";
  63. case 0x35: return "SynchronizeCache";
  64. case 0x36: return "LockUnlockCache";
  65. case 0x37: return "ReadDefectData";
  66. case 0x3B: return "WriteBuffer";
  67. case 0x3C: return "ReadBuffer";
  68. case 0x43: return "CDROM Read TOC";
  69. case 0x44: return "CDROM Read Header";
  70. case 0x4A: return "GetEventStatusNotification";
  71. case 0x55: return "ModeSelect10";
  72. case 0x5A: return "ModeSense10";
  73. case 0xAC: return "Erase12";
  74. case 0xC0: return "OMTI-5204 DefineFlexibleDiskFormat";
  75. case 0xC2: return "OMTI-5204 AssignDiskParameters";
  76. default: return "Unknown";
  77. }
  78. }
  79. static void printNewPhase(int phase, bool initiator = false)
  80. {
  81. g_LogData = false;
  82. g_LogInitiatorCommand = false;
  83. if (!g_log_debug)
  84. {
  85. return;
  86. }
  87. switch(phase)
  88. {
  89. case BUS_FREE:
  90. dbgmsg("-- BUS_FREE");
  91. break;
  92. case BUS_BUSY:
  93. dbgmsg("-- BUS_BUSY");
  94. break;
  95. case ARBITRATION:
  96. dbgmsg("---- ARBITRATION");
  97. break;
  98. case SELECTION:
  99. if (initiator)
  100. dbgmsg("---- SELECTION");
  101. else
  102. dbgmsg("---- SELECTION: ", (int)(*SCSI_STS_SELECTED & 7));
  103. break;
  104. case RESELECTION:
  105. dbgmsg("---- RESELECTION");
  106. break;
  107. case STATUS:
  108. if (initiator)
  109. {
  110. dbgmsg("---- STATUS");
  111. g_LogData = true;
  112. }
  113. else if (scsiDev.status == GOOD)
  114. {
  115. dbgmsg("---- STATUS: 0 GOOD");
  116. }
  117. else if (scsiDev.status == CHECK_CONDITION && scsiDev.target)
  118. {
  119. dbgmsg("---- STATUS: 2 CHECK_CONDITION, sense ", (uint32_t)scsiDev.target->sense.asc);
  120. }
  121. else
  122. {
  123. dbgmsg("---- STATUS: ", (int)scsiDev.status);
  124. }
  125. break;
  126. case COMMAND:
  127. g_LogInitiatorCommand = initiator;
  128. g_LogData = true;
  129. break;
  130. case DATA_IN:
  131. if (!initiator && scsiDev.target->syncOffset > 0)
  132. dbgmsg("---- DATA_IN, syncOffset ", (int)scsiDev.target->syncOffset,
  133. " syncPeriod ", (int)scsiDev.target->syncPeriod);
  134. else
  135. dbgmsg("---- DATA_IN");
  136. break;
  137. case DATA_OUT:
  138. if (!initiator && scsiDev.target->syncOffset > 0)
  139. dbgmsg("---- DATA_OUT, syncOffset ", (int)scsiDev.target->syncOffset,
  140. " syncPeriod ", (int)scsiDev.target->syncPeriod);
  141. else
  142. dbgmsg("---- DATA_OUT");
  143. break;
  144. case MESSAGE_IN:
  145. dbgmsg("---- MESSAGE_IN");
  146. g_LogData = true;
  147. break;
  148. case MESSAGE_OUT:
  149. dbgmsg("---- MESSAGE_OUT");
  150. g_LogData = true;
  151. break;
  152. default:
  153. dbgmsg("---- PHASE: ", phase);
  154. break;
  155. }
  156. }
  157. void scsiLogPhaseChange(int new_phase)
  158. {
  159. static int old_scsi_id = 0;
  160. static int old_phase = BUS_FREE;
  161. static int old_sync_period = 0;
  162. if (new_phase != old_phase)
  163. {
  164. if (old_phase == DATA_IN || old_phase == DATA_OUT)
  165. {
  166. dbgmsg("---- Total IN: ", g_InByteCount, " OUT: ", g_OutByteCount, " CHECKSUM: ", (int)g_DataChecksum);
  167. }
  168. g_InByteCount = g_OutByteCount = 0;
  169. g_DataChecksum = 0;
  170. if (old_phase >= 0 &&
  171. scsiDev.target != NULL &&
  172. old_scsi_id == scsiDev.target->targetId &&
  173. old_sync_period != scsiDev.target->syncPeriod)
  174. {
  175. // Add a log message when negotiated synchronous speed changes.
  176. int syncper = scsiDev.target->syncPeriod;
  177. int syncoff = scsiDev.target->syncOffset;
  178. if (syncper > 0)
  179. {
  180. int mbyte_per_s = (1000 + syncper * 2) / (syncper * 4);
  181. logmsg("SCSI ID ", (int)scsiDev.target->targetId,
  182. " negotiated synchronous mode ", mbyte_per_s, " MB/s ",
  183. "(period 4x", syncper, " ns, offset ", syncoff, " bytes)");
  184. }
  185. else
  186. {
  187. logmsg("SCSI ID ", (int)scsiDev.target->targetId,
  188. " negotiated asynchronous mode ",
  189. "(period 4x", syncper, " ns, offset ", syncoff, " bytes)");
  190. }
  191. }
  192. printNewPhase(new_phase);
  193. old_phase = new_phase;
  194. old_sync_period = scsiDev.target->syncPeriod;
  195. old_scsi_id = scsiDev.target->targetId;
  196. }
  197. }
  198. void scsiLogInitiatorPhaseChange(int new_phase)
  199. {
  200. static int old_phase = BUS_FREE;
  201. if (new_phase != old_phase)
  202. {
  203. if (old_phase == DATA_IN || old_phase == DATA_OUT)
  204. {
  205. dbgmsg("---- Total IN: ", g_InByteCount, " OUT: ", g_OutByteCount, " CHECKSUM: ", (int)g_DataChecksum);
  206. }
  207. g_InByteCount = g_OutByteCount = 0;
  208. g_DataChecksum = 0;
  209. printNewPhase(new_phase, true);
  210. old_phase = new_phase;
  211. }
  212. }
  213. void scsiLogDataIn(const uint8_t *buf, uint32_t length)
  214. {
  215. if (g_LogData)
  216. {
  217. dbgmsg("------ IN: ", bytearray(buf, length));
  218. }
  219. if (g_log_debug)
  220. {
  221. // BSD checksum algorithm
  222. for (uint32_t i = 0; i < length; i++)
  223. {
  224. g_DataChecksum = (g_DataChecksum >> 1) + ((g_DataChecksum & 1) << 15);
  225. g_DataChecksum += buf[i];
  226. }
  227. }
  228. g_InByteCount += length;
  229. }
  230. void scsiLogDataOut(const uint8_t *buf, uint32_t length)
  231. {
  232. if (buf == scsiDev.cdb || g_LogInitiatorCommand)
  233. {
  234. dbgmsg("---- COMMAND: ", getCommandName(buf[0]));
  235. }
  236. if (g_LogData)
  237. {
  238. dbgmsg("------ OUT: ", bytearray(buf, length));
  239. }
  240. if (g_log_debug)
  241. {
  242. // BSD checksum algorithm
  243. for (uint32_t i = 0; i < length; i++)
  244. {
  245. g_DataChecksum = (g_DataChecksum >> 1) + ((g_DataChecksum & 1) << 15);
  246. g_DataChecksum += buf[i];
  247. }
  248. }
  249. g_OutByteCount += length;
  250. }