ZuluSCSI_log_trace.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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/Rewind";
  40. case 0x03: return "RequestSense";
  41. case 0x04: return "FormatUnit";
  42. case 0x05: return "ReadBlockLimits";
  43. case 0x08: return "Read6";
  44. case 0x0A: return "Write6";
  45. case 0x0B: return "Seek6";
  46. case 0x0C: return "Xebec InitializeDriveCharacteristics";
  47. case 0x0F: return "Xebec WriteSectorBuffer";
  48. case 0x10: return "WriteFilemarks";
  49. case 0x11: return "Space";
  50. case 0x12: return "Inquiry";
  51. case 0x13: return "Verify";
  52. case 0x15: return "ModeSelect6";
  53. case 0x16: return "Reserve";
  54. case 0x17: return "Release";
  55. case 0x19: return "Erase";
  56. case 0x1A: return "ModeSense";
  57. case 0x1B: return "StartStopUnit";
  58. case 0x1C: return "ReceiveDiagnostic";
  59. case 0x1D: return "SendDiagnostic";
  60. case 0x1E: return "PreventAllowMediumRemoval";
  61. case 0x25: return "ReadCapacity";
  62. case 0x28: return "Read10";
  63. case 0x2A: return "Write10";
  64. case 0x2B: return "Seek10";
  65. case 0x2C: return "Erase10";
  66. case 0x2E: return "WriteVerify";
  67. case 0x2F: return "Verify";
  68. case 0x34: return "PreFetch/ReadPosition";
  69. case 0x35: return "SynchronizeCache";
  70. case 0x36: return "LockUnlockCache";
  71. case 0x37: return "ReadDefectData";
  72. case 0x3B: return "WriteBuffer";
  73. case 0x3C: return "ReadBuffer";
  74. case 0x42: return "CDROM Read SubChannel";
  75. case 0x43: return "CDROM Read TOC";
  76. case 0x44: return "CDROM Read Header";
  77. case 0x46: return "CDROM GetConfiguration";
  78. case 0x4A: return "GetEventStatusNotification";
  79. case 0x4B: return "CDROM PauseResume";
  80. case 0x4E: return "CDROM StopPlayScan";
  81. case 0x51: return "CDROM ReadDiscInformation";
  82. case 0x45: return "CDROM PlayAudio10";
  83. case 0xA5: return "CDROM PlayAudio12";
  84. case 0x47: return "CDROM PlayAudioMSF";
  85. case 0x48: return "CDROM PauseResume";
  86. case 0x52: return "CDROM ReadTrackInformation";
  87. case 0xBB: return "CDROM SetCDSpeed";
  88. case 0xBD: return "CDROM MechanismStatus";
  89. case 0xBE: return "ReadCD";
  90. case 0xB9: return "ReadCDMSF";
  91. case 0x55: return "ModeSelect10";
  92. case 0x5A: return "ModeSense10";
  93. case 0xAC: return "Erase12";
  94. case 0xA8: return "Read12";
  95. case 0xC0: return "OMTI-5204 DefineFlexibleDiskFormat";
  96. case 0xC2: return "OMTI-5204 AssignDiskParameters";
  97. case 0xE0: return "Xebec RAM Diagnostic";
  98. case 0xE4: return "Xebec Drive Diagnostic";
  99. default: return "Unknown";
  100. }
  101. }
  102. static void printNewPhase(int phase, bool initiator = false)
  103. {
  104. g_LogData = false;
  105. g_LogInitiatorCommand = false;
  106. if (!g_log_debug)
  107. {
  108. return;
  109. }
  110. switch(phase)
  111. {
  112. case BUS_FREE:
  113. dbgmsg("-- BUS_FREE");
  114. break;
  115. case BUS_BUSY:
  116. dbgmsg("-- BUS_BUSY");
  117. break;
  118. case ARBITRATION:
  119. dbgmsg("---- ARBITRATION");
  120. break;
  121. case SELECTION:
  122. if (initiator)
  123. dbgmsg("---- SELECTION");
  124. else
  125. dbgmsg("---- SELECTION: ", (int)(*SCSI_STS_SELECTED & 7));
  126. break;
  127. case RESELECTION:
  128. dbgmsg("---- RESELECTION");
  129. break;
  130. case STATUS:
  131. if (initiator)
  132. {
  133. dbgmsg("---- STATUS");
  134. g_LogData = true;
  135. }
  136. else if (scsiDev.status == GOOD)
  137. {
  138. dbgmsg("---- STATUS: 0 GOOD");
  139. }
  140. else if (scsiDev.status == CHECK_CONDITION && scsiDev.target)
  141. {
  142. dbgmsg("---- STATUS: 2 CHECK_CONDITION, sense ", (uint32_t)scsiDev.target->sense.asc);
  143. }
  144. else
  145. {
  146. dbgmsg("---- STATUS: ", (int)scsiDev.status);
  147. }
  148. break;
  149. case COMMAND:
  150. g_LogInitiatorCommand = initiator;
  151. g_LogData = true;
  152. break;
  153. case DATA_IN:
  154. if (!initiator && scsiDev.target->syncOffset > 0)
  155. dbgmsg("---- DATA_IN, syncOffset ", (int)scsiDev.target->syncOffset,
  156. " syncPeriod ", (int)scsiDev.target->syncPeriod);
  157. else
  158. dbgmsg("---- DATA_IN");
  159. break;
  160. case DATA_OUT:
  161. if (!initiator && scsiDev.target->syncOffset > 0)
  162. dbgmsg("---- DATA_OUT, syncOffset ", (int)scsiDev.target->syncOffset,
  163. " syncPeriod ", (int)scsiDev.target->syncPeriod);
  164. // log Xebec vendor commands data
  165. else if (scsiDev.cdb[0] == 0x0C || scsiDev.cdb[0] == 0x0F)
  166. g_LogData = true;
  167. else
  168. dbgmsg("---- DATA_OUT");
  169. break;
  170. case MESSAGE_IN:
  171. dbgmsg("---- MESSAGE_IN");
  172. g_LogData = true;
  173. break;
  174. case MESSAGE_OUT:
  175. dbgmsg("---- MESSAGE_OUT");
  176. g_LogData = true;
  177. break;
  178. default:
  179. dbgmsg("---- PHASE: ", phase);
  180. break;
  181. }
  182. }
  183. void scsiLogPhaseChange(int new_phase)
  184. {
  185. static int old_scsi_id = 0;
  186. static int old_phase = BUS_FREE;
  187. static int old_sync_period = 0;
  188. if (new_phase != old_phase)
  189. {
  190. if (old_phase == DATA_IN || old_phase == DATA_OUT)
  191. {
  192. dbgmsg("---- Total IN: ", g_InByteCount, " OUT: ", g_OutByteCount, " CHECKSUM: ", (int)g_DataChecksum);
  193. }
  194. // log Xebec vendor command
  195. if (old_phase == DATA_OUT && scsiDev.cdb[0] == 0x0C && g_OutByteCount == 8)
  196. {
  197. int cylinders = ((uint16_t)scsiDev.data[0] << 8) + scsiDev.data[1];
  198. int heads = scsiDev.data[2];
  199. int reducedWrite = ((uint16_t)scsiDev.data[3] << 8) + scsiDev.data[4];
  200. int writePrecomp = ((uint16_t)scsiDev.data[5] << 8) + scsiDev.data[6];
  201. int eccBurst = scsiDev.data[7];
  202. dbgmsg("---- Xebec Initialize Drive Characteristics: cylinders=", cylinders, " heads=", heads,
  203. " reducedWrite=", reducedWrite, " writePrecomp=", writePrecomp, " eccBurst=", eccBurst);
  204. }
  205. g_InByteCount = g_OutByteCount = 0;
  206. g_DataChecksum = 0;
  207. if (old_phase >= 0 &&
  208. scsiDev.target != NULL &&
  209. old_scsi_id == scsiDev.target->targetId &&
  210. old_sync_period != scsiDev.target->syncPeriod)
  211. {
  212. // Add a log message when negotiated synchronous speed changes.
  213. int syncper = scsiDev.target->syncPeriod;
  214. int syncoff = scsiDev.target->syncOffset;
  215. if (syncper > 0)
  216. {
  217. int mbyte_per_s = (1000 + syncper * 2) / (syncper * 4);
  218. logmsg("SCSI ID ", (int)scsiDev.target->targetId,
  219. " negotiated synchronous mode ", mbyte_per_s, " MB/s ",
  220. "(period 4x", syncper, " ns, offset ", syncoff, " bytes)");
  221. }
  222. else
  223. {
  224. logmsg("SCSI ID ", (int)scsiDev.target->targetId,
  225. " negotiated asynchronous mode ",
  226. "(period 4x", syncper, " ns, offset ", syncoff, " bytes)");
  227. }
  228. }
  229. printNewPhase(new_phase);
  230. old_phase = new_phase;
  231. old_sync_period = scsiDev.target->syncPeriod;
  232. old_scsi_id = scsiDev.target->targetId;
  233. }
  234. }
  235. void scsiLogInitiatorPhaseChange(int new_phase)
  236. {
  237. static int old_phase = BUS_FREE;
  238. if (new_phase != old_phase)
  239. {
  240. if (old_phase == DATA_IN || old_phase == DATA_OUT)
  241. {
  242. dbgmsg("---- Total IN: ", g_InByteCount, " OUT: ", g_OutByteCount, " CHECKSUM: ", (int)g_DataChecksum);
  243. }
  244. g_InByteCount = g_OutByteCount = 0;
  245. g_DataChecksum = 0;
  246. printNewPhase(new_phase, true);
  247. old_phase = new_phase;
  248. }
  249. }
  250. void scsiLogDataIn(const uint8_t *buf, uint32_t length)
  251. {
  252. if (g_LogData)
  253. {
  254. dbgmsg("------ IN: ", bytearray(buf, length));
  255. }
  256. if (g_log_debug)
  257. {
  258. // BSD checksum algorithm
  259. for (uint32_t i = 0; i < length; i++)
  260. {
  261. g_DataChecksum = (g_DataChecksum >> 1) + ((g_DataChecksum & 1) << 15);
  262. g_DataChecksum += buf[i];
  263. }
  264. }
  265. g_InByteCount += length;
  266. }
  267. void scsiLogDataOut(const uint8_t *buf, uint32_t length)
  268. {
  269. if (buf == scsiDev.cdb || g_LogInitiatorCommand)
  270. {
  271. dbgmsg("---- COMMAND: ", getCommandName(buf[0]));
  272. }
  273. if (g_LogData)
  274. {
  275. dbgmsg("------ OUT: ", bytearray(buf, length));
  276. }
  277. if (g_log_debug)
  278. {
  279. // BSD checksum algorithm
  280. for (uint32_t i = 0; i < length; i++)
  281. {
  282. g_DataChecksum = (g_DataChecksum >> 1) + ((g_DataChecksum & 1) << 15);
  283. g_DataChecksum += buf[i];
  284. }
  285. }
  286. g_OutByteCount += length;
  287. }