constants.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * ASCII values for a number of symbolic constants, printing functions,
  4. * etc.
  5. * Additions for SCSI 2 and Linux 2.2.x by D. Gilbert (990422)
  6. * Additions for SCSI 3+ (SPC-3 T10/1416-D Rev 07 3 May 2002)
  7. * by D. Gilbert and aeb (20020609)
  8. * Updated to SPC-4 T10/1713-D Rev 36g, D. Gilbert 20130701
  9. */
  10. #include <linux/blkdev.h>
  11. #include <linux/module.h>
  12. #include <linux/kernel.h>
  13. #include <scsi/scsi.h>
  14. #include <scsi/scsi_cmnd.h>
  15. #include <scsi/scsi_device.h>
  16. #include <scsi/scsi_host.h>
  17. #include <scsi/scsi_eh.h>
  18. #include <scsi/scsi_dbg.h>
  19. /* Commands with service actions that change the command name */
  20. #define THIRD_PARTY_COPY_OUT 0x83
  21. #define THIRD_PARTY_COPY_IN 0x84
  22. struct sa_name_list {
  23. int opcode;
  24. const struct value_name_pair *arr;
  25. int arr_sz;
  26. };
  27. struct value_name_pair {
  28. int value;
  29. const char * name;
  30. };
  31. static const char * cdb_byte0_names[] = {
  32. /* 00-03 */ "Test Unit Ready", "Rezero Unit/Rewind", NULL, "Request Sense",
  33. /* 04-07 */ "Format Unit/Medium", "Read Block Limits", NULL,
  34. "Reassign Blocks",
  35. /* 08-0d */ "Read(6)", NULL, "Write(6)", "Seek(6)", NULL, NULL,
  36. /* 0e-12 */ NULL, "Read Reverse", "Write Filemarks", "Space", "Inquiry",
  37. /* 13-16 */ "Verify(6)", "Recover Buffered Data", "Mode Select(6)",
  38. "Reserve(6)",
  39. /* 17-1a */ "Release(6)", "Copy", "Erase", "Mode Sense(6)",
  40. /* 1b-1d */ "Start/Stop Unit", "Receive Diagnostic", "Send Diagnostic",
  41. /* 1e-1f */ "Prevent/Allow Medium Removal", NULL,
  42. /* 20-22 */ NULL, NULL, NULL,
  43. /* 23-28 */ "Read Format Capacities", "Set Window",
  44. "Read Capacity(10)", NULL, NULL, "Read(10)",
  45. /* 29-2d */ "Read Generation", "Write(10)", "Seek(10)", "Erase(10)",
  46. "Read updated block",
  47. /* 2e-31 */ "Write Verify(10)", "Verify(10)", "Search High", "Search Equal",
  48. /* 32-34 */ "Search Low", "Set Limits", "Prefetch/Read Position",
  49. /* 35-37 */ "Synchronize Cache(10)", "Lock/Unlock Cache(10)",
  50. "Read Defect Data(10)",
  51. /* 38-3c */ "Medium Scan", "Compare", "Copy Verify", "Write Buffer",
  52. "Read Buffer",
  53. /* 3d-3f */ "Update Block", "Read Long(10)", "Write Long(10)",
  54. /* 40-41 */ "Change Definition", "Write Same(10)",
  55. /* 42-48 */ "Unmap/Read sub-channel", "Read TOC/PMA/ATIP",
  56. "Read density support", "Play audio(10)", "Get configuration",
  57. "Play audio msf", "Sanitize/Play audio track/index",
  58. /* 49-4f */ "Play track relative(10)", "Get event status notification",
  59. "Pause/resume", "Log Select", "Log Sense", "Stop play/scan",
  60. NULL,
  61. /* 50-55 */ "Xdwrite", "Xpwrite, Read disk info", "Xdread, Read track info",
  62. "Reserve track", "Send OPC info", "Mode Select(10)",
  63. /* 56-5b */ "Reserve(10)", "Release(10)", "Repair track", "Read master cue",
  64. "Mode Sense(10)", "Close track/session",
  65. /* 5c-5f */ "Read buffer capacity", "Send cue sheet", "Persistent reserve in",
  66. "Persistent reserve out",
  67. /* 60-67 */ NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  68. /* 68-6f */ NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  69. /* 70-77 */ NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  70. /* 78-7f */ NULL, NULL, NULL, NULL, NULL, NULL, "Extended CDB",
  71. "Variable length",
  72. /* 80-84 */ "Xdwrite(16)", "Rebuild(16)", "Regenerate(16)",
  73. "Third party copy out", "Third party copy in",
  74. /* 85-89 */ "ATA command pass through(16)", "Access control in",
  75. "Access control out", "Read(16)", "Compare and Write",
  76. /* 8a-8f */ "Write(16)", "ORWrite", "Read attributes", "Write attributes",
  77. "Write and verify(16)", "Verify(16)",
  78. /* 90-94 */ "Pre-fetch(16)", "Synchronize cache(16)",
  79. "Lock/unlock cache(16)", "Write same(16)", NULL,
  80. /* 95-99 */ NULL, NULL, NULL, NULL, NULL,
  81. /* 9a-9f */ NULL, NULL, NULL, "Service action bidirectional",
  82. "Service action in(16)", "Service action out(16)",
  83. /* a0-a5 */ "Report luns", "ATA command pass through(12)/Blank",
  84. "Security protocol in", "Maintenance in", "Maintenance out",
  85. "Move medium/play audio(12)",
  86. /* a6-a9 */ "Exchange medium", "Move medium attached", "Read(12)",
  87. "Play track relative(12)",
  88. /* aa-ae */ "Write(12)", NULL, "Erase(12), Get Performance",
  89. "Read DVD structure", "Write and verify(12)",
  90. /* af-b1 */ "Verify(12)", "Search data high(12)", "Search data equal(12)",
  91. /* b2-b4 */ "Search data low(12)", "Set limits(12)",
  92. "Read element status attached",
  93. /* b5-b6 */ "Security protocol out", "Send volume tag, set streaming",
  94. /* b7-b9 */ "Read defect data(12)", "Read element status", "Read CD msf",
  95. /* ba-bc */ "Redundancy group (in), Scan",
  96. "Redundancy group (out), Set cd-rom speed", "Spare (in), Play cd",
  97. /* bd-bf */ "Spare (out), Mechanism status", "Volume set (in), Read cd",
  98. "Volume set (out), Send DVD structure",
  99. };
  100. static const struct value_name_pair maint_in_arr[] = {
  101. {0x5, "Report identifying information"},
  102. {0xa, "Report target port groups"},
  103. {0xb, "Report aliases"},
  104. {0xc, "Report supported operation codes"},
  105. {0xd, "Report supported task management functions"},
  106. {0xe, "Report priority"},
  107. {0xf, "Report timestamp"},
  108. {0x10, "Management protocol in"},
  109. };
  110. #define MAINT_IN_SZ ARRAY_SIZE(maint_in_arr)
  111. static const struct value_name_pair maint_out_arr[] = {
  112. {0x6, "Set identifying information"},
  113. {0xa, "Set target port groups"},
  114. {0xb, "Change aliases"},
  115. {0xc, "Remove I_T nexus"},
  116. {0xe, "Set priority"},
  117. {0xf, "Set timestamp"},
  118. {0x10, "Management protocol out"},
  119. };
  120. #define MAINT_OUT_SZ ARRAY_SIZE(maint_out_arr)
  121. static const struct value_name_pair serv_in12_arr[] = {
  122. {0x1, "Read media serial number"},
  123. };
  124. #define SERV_IN12_SZ ARRAY_SIZE(serv_in12_arr)
  125. static const struct value_name_pair serv_out12_arr[] = {
  126. {-1, "dummy entry"},
  127. };
  128. #define SERV_OUT12_SZ ARRAY_SIZE(serv_out12_arr)
  129. static const struct value_name_pair serv_bidi_arr[] = {
  130. {-1, "dummy entry"},
  131. };
  132. #define SERV_BIDI_SZ ARRAY_SIZE(serv_bidi_arr)
  133. static const struct value_name_pair serv_in16_arr[] = {
  134. {0x10, "Read capacity(16)"},
  135. {0x11, "Read long(16)"},
  136. {0x12, "Get LBA status"},
  137. {0x13, "Report referrals"},
  138. };
  139. #define SERV_IN16_SZ ARRAY_SIZE(serv_in16_arr)
  140. static const struct value_name_pair serv_out16_arr[] = {
  141. {0x11, "Write long(16)"},
  142. {0x1f, "Notify data transfer device(16)"},
  143. };
  144. #define SERV_OUT16_SZ ARRAY_SIZE(serv_out16_arr)
  145. static const struct value_name_pair pr_in_arr[] = {
  146. {0x0, "Persistent reserve in, read keys"},
  147. {0x1, "Persistent reserve in, read reservation"},
  148. {0x2, "Persistent reserve in, report capabilities"},
  149. {0x3, "Persistent reserve in, read full status"},
  150. };
  151. #define PR_IN_SZ ARRAY_SIZE(pr_in_arr)
  152. static const struct value_name_pair pr_out_arr[] = {
  153. {0x0, "Persistent reserve out, register"},
  154. {0x1, "Persistent reserve out, reserve"},
  155. {0x2, "Persistent reserve out, release"},
  156. {0x3, "Persistent reserve out, clear"},
  157. {0x4, "Persistent reserve out, preempt"},
  158. {0x5, "Persistent reserve out, preempt and abort"},
  159. {0x6, "Persistent reserve out, register and ignore existing key"},
  160. {0x7, "Persistent reserve out, register and move"},
  161. };
  162. #define PR_OUT_SZ ARRAY_SIZE(pr_out_arr)
  163. /* SPC-4 rev 34 renamed the Extended Copy opcode to Third Party Copy Out.
  164. LID1 (List Identifier length: 1 byte) is the Extended Copy found in SPC-2
  165. and SPC-3 */
  166. static const struct value_name_pair tpc_out_arr[] = {
  167. {0x0, "Extended copy(LID1)"},
  168. {0x1, "Extended copy(LID4)"},
  169. {0x10, "Populate token"},
  170. {0x11, "Write using token"},
  171. {0x1c, "Copy operation abort"},
  172. };
  173. #define TPC_OUT_SZ ARRAY_SIZE(tpc_out_arr)
  174. static const struct value_name_pair tpc_in_arr[] = {
  175. {0x0, "Receive copy status(LID1)"},
  176. {0x1, "Receive copy data(LID1)"},
  177. {0x3, "Receive copy operating parameters"},
  178. {0x4, "Receive copy failure details(LID1)"},
  179. {0x5, "Receive copy status(LID4)"},
  180. {0x6, "Receive copy data(LID4)"},
  181. {0x7, "Receive ROD token information"},
  182. {0x8, "Report all ROD tokens"},
  183. };
  184. #define TPC_IN_SZ ARRAY_SIZE(tpc_in_arr)
  185. static const struct value_name_pair variable_length_arr[] = {
  186. {0x1, "Rebuild(32)"},
  187. {0x2, "Regenerate(32)"},
  188. {0x3, "Xdread(32)"},
  189. {0x4, "Xdwrite(32)"},
  190. {0x5, "Xdwrite extended(32)"},
  191. {0x6, "Xpwrite(32)"},
  192. {0x7, "Xdwriteread(32)"},
  193. {0x8, "Xdwrite extended(64)"},
  194. {0x9, "Read(32)"},
  195. {0xa, "Verify(32)"},
  196. {0xb, "Write(32)"},
  197. {0xc, "Write an verify(32)"},
  198. {0xd, "Write same(32)"},
  199. {0x8801, "Format OSD"},
  200. {0x8802, "Create (osd)"},
  201. {0x8803, "List (osd)"},
  202. {0x8805, "Read (osd)"},
  203. {0x8806, "Write (osd)"},
  204. {0x8807, "Append (osd)"},
  205. {0x8808, "Flush (osd)"},
  206. {0x880a, "Remove (osd)"},
  207. {0x880b, "Create partition (osd)"},
  208. {0x880c, "Remove partition (osd)"},
  209. {0x880e, "Get attributes (osd)"},
  210. {0x880f, "Set attributes (osd)"},
  211. {0x8812, "Create and write (osd)"},
  212. {0x8815, "Create collection (osd)"},
  213. {0x8816, "Remove collection (osd)"},
  214. {0x8817, "List collection (osd)"},
  215. {0x8818, "Set key (osd)"},
  216. {0x8819, "Set master key (osd)"},
  217. {0x881a, "Flush collection (osd)"},
  218. {0x881b, "Flush partition (osd)"},
  219. {0x881c, "Flush OSD"},
  220. {0x8f7e, "Perform SCSI command (osd)"},
  221. {0x8f7f, "Perform task management function (osd)"},
  222. };
  223. #define VARIABLE_LENGTH_SZ ARRAY_SIZE(variable_length_arr)
  224. static struct sa_name_list sa_names_arr[] = {
  225. {VARIABLE_LENGTH_CMD, variable_length_arr, VARIABLE_LENGTH_SZ},
  226. {MAINTENANCE_IN, maint_in_arr, MAINT_IN_SZ},
  227. {MAINTENANCE_OUT, maint_out_arr, MAINT_OUT_SZ},
  228. {PERSISTENT_RESERVE_IN, pr_in_arr, PR_IN_SZ},
  229. {PERSISTENT_RESERVE_OUT, pr_out_arr, PR_OUT_SZ},
  230. {SERVICE_ACTION_IN_12, serv_in12_arr, SERV_IN12_SZ},
  231. {SERVICE_ACTION_OUT_12, serv_out12_arr, SERV_OUT12_SZ},
  232. {SERVICE_ACTION_BIDIRECTIONAL, serv_bidi_arr, SERV_BIDI_SZ},
  233. {SERVICE_ACTION_IN_16, serv_in16_arr, SERV_IN16_SZ},
  234. {SERVICE_ACTION_OUT_16, serv_out16_arr, SERV_OUT16_SZ},
  235. {THIRD_PARTY_COPY_IN, tpc_in_arr, TPC_IN_SZ},
  236. {THIRD_PARTY_COPY_OUT, tpc_out_arr, TPC_OUT_SZ},
  237. {0, NULL, 0},
  238. };
  239. bool scsi_opcode_sa_name(int opcode, int service_action,
  240. const char **cdb_name, const char **sa_name)
  241. {
  242. struct sa_name_list *sa_name_ptr;
  243. const struct value_name_pair *arr = NULL;
  244. int arr_sz, k;
  245. *cdb_name = NULL;
  246. if (opcode >= VENDOR_SPECIFIC_CDB)
  247. return false;
  248. if (opcode < ARRAY_SIZE(cdb_byte0_names))
  249. *cdb_name = cdb_byte0_names[opcode];
  250. for (sa_name_ptr = sa_names_arr; sa_name_ptr->arr; ++sa_name_ptr) {
  251. if (sa_name_ptr->opcode == opcode) {
  252. arr = sa_name_ptr->arr;
  253. arr_sz = sa_name_ptr->arr_sz;
  254. break;
  255. }
  256. }
  257. if (!arr)
  258. return false;
  259. for (k = 0; k < arr_sz; ++k, ++arr) {
  260. if (service_action == arr->value)
  261. break;
  262. }
  263. if (k < arr_sz)
  264. *sa_name = arr->name;
  265. return true;
  266. }
  267. struct error_info {
  268. unsigned short code12; /* 0x0302 looks better than 0x03,0x02 */
  269. unsigned short size;
  270. };
  271. /*
  272. * There are 700+ entries in this table. To save space, we don't store
  273. * (code, pointer) pairs, which would make sizeof(struct
  274. * error_info)==16 on 64 bits. Rather, the second element just stores
  275. * the size (including \0) of the corresponding string, and we use the
  276. * sum of these to get the appropriate offset into additional_text
  277. * defined below. This approach saves 12 bytes per entry.
  278. */
  279. static const struct error_info additional[] =
  280. {
  281. #define SENSE_CODE(c, s) {c, sizeof(s)},
  282. #include "sense_codes.h"
  283. #undef SENSE_CODE
  284. };
  285. static const char *additional_text =
  286. #define SENSE_CODE(c, s) s "\0"
  287. #include "sense_codes.h"
  288. #undef SENSE_CODE
  289. ;
  290. struct error_info2 {
  291. unsigned char code1, code2_min, code2_max;
  292. const char * str;
  293. const char * fmt;
  294. };
  295. static const struct error_info2 additional2[] =
  296. {
  297. {0x40, 0x00, 0x7f, "Ram failure", ""},
  298. {0x40, 0x80, 0xff, "Diagnostic failure on component", ""},
  299. {0x41, 0x00, 0xff, "Data path failure", ""},
  300. {0x42, 0x00, 0xff, "Power-on or self-test failure", ""},
  301. {0x4D, 0x00, 0xff, "Tagged overlapped commands", "task tag "},
  302. {0x70, 0x00, 0xff, "Decompression exception", "short algorithm id of "},
  303. {0, 0, 0, NULL, NULL}
  304. };
  305. /* description of the sense key values */
  306. static const char * const snstext[] = {
  307. "No Sense", /* 0: There is no sense information */
  308. "Recovered Error", /* 1: The last command completed successfully
  309. but used error correction */
  310. "Not Ready", /* 2: The addressed target is not ready */
  311. "Medium Error", /* 3: Data error detected on the medium */
  312. "Hardware Error", /* 4: Controller or device failure */
  313. "Illegal Request", /* 5: Error in request */
  314. "Unit Attention", /* 6: Removable medium was changed, or
  315. the target has been reset, or ... */
  316. "Data Protect", /* 7: Access to the data is blocked */
  317. "Blank Check", /* 8: Reached unexpected written or unwritten
  318. region of the medium */
  319. "Vendor Specific(9)",
  320. "Copy Aborted", /* A: COPY or COMPARE was aborted */
  321. "Aborted Command", /* B: The target aborted the command */
  322. "Equal", /* C: A SEARCH DATA command found data equal,
  323. reserved in SPC-4 rev 36 */
  324. "Volume Overflow", /* D: Medium full with still data to be written */
  325. "Miscompare", /* E: Source data and data on the medium
  326. do not agree */
  327. "Completed", /* F: command completed sense data reported,
  328. may occur for successful command */
  329. };
  330. /* Get sense key string or NULL if not available */
  331. const char *
  332. scsi_sense_key_string(unsigned char key)
  333. {
  334. if (key < ARRAY_SIZE(snstext))
  335. return snstext[key];
  336. return NULL;
  337. }
  338. EXPORT_SYMBOL(scsi_sense_key_string);
  339. /*
  340. * Get additional sense code string or NULL if not available.
  341. * This string may contain a "%x" and should be printed with ascq as arg.
  342. */
  343. const char *
  344. scsi_extd_sense_format(unsigned char asc, unsigned char ascq, const char **fmt)
  345. {
  346. int i;
  347. unsigned short code = ((asc << 8) | ascq);
  348. unsigned offset = 0;
  349. *fmt = NULL;
  350. for (i = 0; i < ARRAY_SIZE(additional); i++) {
  351. if (additional[i].code12 == code)
  352. return additional_text + offset;
  353. offset += additional[i].size;
  354. }
  355. for (i = 0; additional2[i].fmt; i++) {
  356. if (additional2[i].code1 == asc &&
  357. ascq >= additional2[i].code2_min &&
  358. ascq <= additional2[i].code2_max) {
  359. *fmt = additional2[i].fmt;
  360. return additional2[i].str;
  361. }
  362. }
  363. return NULL;
  364. }
  365. EXPORT_SYMBOL(scsi_extd_sense_format);
  366. static const char * const hostbyte_table[]={
  367. "DID_OK", "DID_NO_CONNECT", "DID_BUS_BUSY", "DID_TIME_OUT", "DID_BAD_TARGET",
  368. "DID_ABORT", "DID_PARITY", "DID_ERROR", "DID_RESET", "DID_BAD_INTR",
  369. "DID_PASSTHROUGH", "DID_SOFT_ERROR", "DID_IMM_RETRY", "DID_REQUEUE",
  370. "DID_TRANSPORT_DISRUPTED", "DID_TRANSPORT_FAILFAST", "DID_TARGET_FAILURE",
  371. "DID_NEXUS_FAILURE" };
  372. static const char * const driverbyte_table[]={
  373. "DRIVER_OK", "DRIVER_BUSY", "DRIVER_SOFT", "DRIVER_MEDIA", "DRIVER_ERROR",
  374. "DRIVER_INVALID", "DRIVER_TIMEOUT", "DRIVER_HARD", "DRIVER_SENSE"};
  375. const char *scsi_hostbyte_string(int result)
  376. {
  377. const char *hb_string = NULL;
  378. int hb = host_byte(result);
  379. if (hb < ARRAY_SIZE(hostbyte_table))
  380. hb_string = hostbyte_table[hb];
  381. return hb_string;
  382. }
  383. EXPORT_SYMBOL(scsi_hostbyte_string);
  384. const char *scsi_driverbyte_string(int result)
  385. {
  386. const char *db_string = NULL;
  387. int db = driver_byte(result);
  388. if (db < ARRAY_SIZE(driverbyte_table))
  389. db_string = driverbyte_table[db];
  390. return db_string;
  391. }
  392. EXPORT_SYMBOL(scsi_driverbyte_string);
  393. #define scsi_mlreturn_name(result) { result, #result }
  394. static const struct value_name_pair scsi_mlreturn_arr[] = {
  395. scsi_mlreturn_name(NEEDS_RETRY),
  396. scsi_mlreturn_name(SUCCESS),
  397. scsi_mlreturn_name(FAILED),
  398. scsi_mlreturn_name(QUEUED),
  399. scsi_mlreturn_name(SOFT_ERROR),
  400. scsi_mlreturn_name(ADD_TO_MLQUEUE),
  401. scsi_mlreturn_name(TIMEOUT_ERROR),
  402. scsi_mlreturn_name(SCSI_RETURN_NOT_HANDLED),
  403. scsi_mlreturn_name(FAST_IO_FAIL)
  404. };
  405. const char *scsi_mlreturn_string(int result)
  406. {
  407. const struct value_name_pair *arr = scsi_mlreturn_arr;
  408. int k;
  409. for (k = 0; k < ARRAY_SIZE(scsi_mlreturn_arr); ++k, ++arr) {
  410. if (result == arr->value)
  411. return arr->name;
  412. }
  413. return NULL;
  414. }
  415. EXPORT_SYMBOL(scsi_mlreturn_string);