BlueSCSI_log_trace.cpp 7.1 KB

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