scsi_common.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * SCSI functions used by both the initiator and the target code.
  4. */
  5. #include <linux/bug.h>
  6. #include <linux/kernel.h>
  7. #include <linux/string.h>
  8. #include <linux/errno.h>
  9. #include <linux/module.h>
  10. #include <asm/unaligned.h>
  11. #include <scsi/scsi_common.h>
  12. MODULE_LICENSE("GPL v2");
  13. /* Command group 3 is reserved and should never be used. */
  14. const unsigned char scsi_command_size_tbl[8] = {
  15. 6, 10, 10, 12, 16, 12, 10, 10
  16. };
  17. EXPORT_SYMBOL(scsi_command_size_tbl);
  18. /* NB: These are exposed through /proc/scsi/scsi and form part of the ABI.
  19. * You may not alter any existing entry (although adding new ones is
  20. * encouraged once assigned by ANSI/INCITS T10).
  21. */
  22. static const char *const scsi_device_types[] = {
  23. "Direct-Access ",
  24. "Sequential-Access",
  25. "Printer ",
  26. "Processor ",
  27. "WORM ",
  28. "CD-ROM ",
  29. "Scanner ",
  30. "Optical Device ",
  31. "Medium Changer ",
  32. "Communications ",
  33. "ASC IT8 ",
  34. "ASC IT8 ",
  35. "RAID ",
  36. "Enclosure ",
  37. "Direct-Access-RBC",
  38. "Optical card ",
  39. "Bridge controller",
  40. "Object storage ",
  41. "Automation/Drive ",
  42. "Security Manager ",
  43. "Direct-Access-ZBC",
  44. };
  45. /**
  46. * scsi_device_type - Return 17-char string indicating device type.
  47. * @type: type number to look up
  48. */
  49. const char *scsi_device_type(unsigned type)
  50. {
  51. if (type == 0x1e)
  52. return "Well-known LUN ";
  53. if (type == 0x1f)
  54. return "No Device ";
  55. if (type >= ARRAY_SIZE(scsi_device_types))
  56. return "Unknown ";
  57. return scsi_device_types[type];
  58. }
  59. EXPORT_SYMBOL(scsi_device_type);
  60. /**
  61. * scsilun_to_int - convert a scsi_lun to an int
  62. * @scsilun: struct scsi_lun to be converted.
  63. *
  64. * Description:
  65. * Convert @scsilun from a struct scsi_lun to a four-byte host byte-ordered
  66. * integer, and return the result. The caller must check for
  67. * truncation before using this function.
  68. *
  69. * Notes:
  70. * For a description of the LUN format, post SCSI-3 see the SCSI
  71. * Architecture Model, for SCSI-3 see the SCSI Controller Commands.
  72. *
  73. * Given a struct scsi_lun of: d2 04 0b 03 00 00 00 00, this function
  74. * returns the integer: 0x0b03d204
  75. *
  76. * This encoding will return a standard integer LUN for LUNs smaller
  77. * than 256, which typically use a single level LUN structure with
  78. * addressing method 0.
  79. */
  80. u64 scsilun_to_int(struct scsi_lun *scsilun)
  81. {
  82. int i;
  83. u64 lun;
  84. lun = 0;
  85. for (i = 0; i < sizeof(lun); i += 2)
  86. lun = lun | (((u64)scsilun->scsi_lun[i] << ((i + 1) * 8)) |
  87. ((u64)scsilun->scsi_lun[i + 1] << (i * 8)));
  88. return lun;
  89. }
  90. EXPORT_SYMBOL(scsilun_to_int);
  91. /**
  92. * int_to_scsilun - reverts an int into a scsi_lun
  93. * @lun: integer to be reverted
  94. * @scsilun: struct scsi_lun to be set.
  95. *
  96. * Description:
  97. * Reverts the functionality of the scsilun_to_int, which packed
  98. * an 8-byte lun value into an int. This routine unpacks the int
  99. * back into the lun value.
  100. *
  101. * Notes:
  102. * Given an integer : 0x0b03d204, this function returns a
  103. * struct scsi_lun of: d2 04 0b 03 00 00 00 00
  104. *
  105. */
  106. void int_to_scsilun(u64 lun, struct scsi_lun *scsilun)
  107. {
  108. int i;
  109. memset(scsilun->scsi_lun, 0, sizeof(scsilun->scsi_lun));
  110. for (i = 0; i < sizeof(lun); i += 2) {
  111. scsilun->scsi_lun[i] = (lun >> 8) & 0xFF;
  112. scsilun->scsi_lun[i+1] = lun & 0xFF;
  113. lun = lun >> 16;
  114. }
  115. }
  116. EXPORT_SYMBOL(int_to_scsilun);
  117. /**
  118. * scsi_normalize_sense - normalize main elements from either fixed or
  119. * descriptor sense data format into a common format.
  120. *
  121. * @sense_buffer: byte array containing sense data returned by device
  122. * @sb_len: number of valid bytes in sense_buffer
  123. * @sshdr: pointer to instance of structure that common
  124. * elements are written to.
  125. *
  126. * Notes:
  127. * The "main elements" from sense data are: response_code, sense_key,
  128. * asc, ascq and additional_length (only for descriptor format).
  129. *
  130. * Typically this function can be called after a device has
  131. * responded to a SCSI command with the CHECK_CONDITION status.
  132. *
  133. * Return value:
  134. * true if valid sense data information found, else false;
  135. */
  136. bool scsi_normalize_sense(const u8 *sense_buffer, int sb_len,
  137. struct scsi_sense_hdr *sshdr)
  138. {
  139. memset(sshdr, 0, sizeof(struct scsi_sense_hdr));
  140. if (!sense_buffer || !sb_len)
  141. return false;
  142. sshdr->response_code = (sense_buffer[0] & 0x7f);
  143. if (!scsi_sense_valid(sshdr))
  144. return false;
  145. if (sshdr->response_code >= 0x72) {
  146. /*
  147. * descriptor format
  148. */
  149. if (sb_len > 1)
  150. sshdr->sense_key = (sense_buffer[1] & 0xf);
  151. if (sb_len > 2)
  152. sshdr->asc = sense_buffer[2];
  153. if (sb_len > 3)
  154. sshdr->ascq = sense_buffer[3];
  155. if (sb_len > 7)
  156. sshdr->additional_length = sense_buffer[7];
  157. } else {
  158. /*
  159. * fixed format
  160. */
  161. if (sb_len > 2)
  162. sshdr->sense_key = (sense_buffer[2] & 0xf);
  163. if (sb_len > 7) {
  164. sb_len = (sb_len < (sense_buffer[7] + 8)) ?
  165. sb_len : (sense_buffer[7] + 8);
  166. if (sb_len > 12)
  167. sshdr->asc = sense_buffer[12];
  168. if (sb_len > 13)
  169. sshdr->ascq = sense_buffer[13];
  170. }
  171. }
  172. return true;
  173. }
  174. EXPORT_SYMBOL(scsi_normalize_sense);
  175. /**
  176. * scsi_sense_desc_find - search for a given descriptor type in descriptor sense data format.
  177. * @sense_buffer: byte array of descriptor format sense data
  178. * @sb_len: number of valid bytes in sense_buffer
  179. * @desc_type: value of descriptor type to find
  180. * (e.g. 0 -> information)
  181. *
  182. * Notes:
  183. * only valid when sense data is in descriptor format
  184. *
  185. * Return value:
  186. * pointer to start of (first) descriptor if found else NULL
  187. */
  188. const u8 * scsi_sense_desc_find(const u8 * sense_buffer, int sb_len,
  189. int desc_type)
  190. {
  191. int add_sen_len, add_len, desc_len, k;
  192. const u8 * descp;
  193. if ((sb_len < 8) || (0 == (add_sen_len = sense_buffer[7])))
  194. return NULL;
  195. if ((sense_buffer[0] < 0x72) || (sense_buffer[0] > 0x73))
  196. return NULL;
  197. add_sen_len = (add_sen_len < (sb_len - 8)) ?
  198. add_sen_len : (sb_len - 8);
  199. descp = &sense_buffer[8];
  200. for (desc_len = 0, k = 0; k < add_sen_len; k += desc_len) {
  201. descp += desc_len;
  202. add_len = (k < (add_sen_len - 1)) ? descp[1]: -1;
  203. desc_len = add_len + 2;
  204. if (descp[0] == desc_type)
  205. return descp;
  206. if (add_len < 0) // short descriptor ??
  207. break;
  208. }
  209. return NULL;
  210. }
  211. EXPORT_SYMBOL(scsi_sense_desc_find);
  212. /**
  213. * scsi_build_sense_buffer - build sense data in a buffer
  214. * @desc: Sense format (non-zero == descriptor format,
  215. * 0 == fixed format)
  216. * @buf: Where to build sense data
  217. * @key: Sense key
  218. * @asc: Additional sense code
  219. * @ascq: Additional sense code qualifier
  220. *
  221. **/
  222. void scsi_build_sense_buffer(int desc, u8 *buf, u8 key, u8 asc, u8 ascq)
  223. {
  224. if (desc) {
  225. buf[0] = 0x72; /* descriptor, current */
  226. buf[1] = key;
  227. buf[2] = asc;
  228. buf[3] = ascq;
  229. buf[7] = 0;
  230. } else {
  231. buf[0] = 0x70; /* fixed, current */
  232. buf[2] = key;
  233. buf[7] = 0xa;
  234. buf[12] = asc;
  235. buf[13] = ascq;
  236. }
  237. }
  238. EXPORT_SYMBOL(scsi_build_sense_buffer);
  239. /**
  240. * scsi_set_sense_information - set the information field in a
  241. * formatted sense data buffer
  242. * @buf: Where to build sense data
  243. * @buf_len: buffer length
  244. * @info: 64-bit information value to be set
  245. *
  246. * Return value:
  247. * 0 on success or -EINVAL for invalid sense buffer length
  248. **/
  249. int scsi_set_sense_information(u8 *buf, int buf_len, u64 info)
  250. {
  251. if ((buf[0] & 0x7f) == 0x72) {
  252. u8 *ucp, len;
  253. len = buf[7];
  254. ucp = (char *)scsi_sense_desc_find(buf, len + 8, 0);
  255. if (!ucp) {
  256. buf[7] = len + 0xc;
  257. ucp = buf + 8 + len;
  258. }
  259. if (buf_len < len + 0xc)
  260. /* Not enough room for info */
  261. return -EINVAL;
  262. ucp[0] = 0;
  263. ucp[1] = 0xa;
  264. ucp[2] = 0x80; /* Valid bit */
  265. ucp[3] = 0;
  266. put_unaligned_be64(info, &ucp[4]);
  267. } else if ((buf[0] & 0x7f) == 0x70) {
  268. /*
  269. * Only set the 'VALID' bit if we can represent the value
  270. * correctly; otherwise just fill out the lower bytes and
  271. * clear the 'VALID' flag.
  272. */
  273. if (info <= 0xffffffffUL)
  274. buf[0] |= 0x80;
  275. else
  276. buf[0] &= 0x7f;
  277. put_unaligned_be32((u32)info, &buf[3]);
  278. }
  279. return 0;
  280. }
  281. EXPORT_SYMBOL(scsi_set_sense_information);
  282. /**
  283. * scsi_set_sense_field_pointer - set the field pointer sense key
  284. * specific information in a formatted sense data buffer
  285. * @buf: Where to build sense data
  286. * @buf_len: buffer length
  287. * @fp: field pointer to be set
  288. * @bp: bit pointer to be set
  289. * @cd: command/data bit
  290. *
  291. * Return value:
  292. * 0 on success or -EINVAL for invalid sense buffer length
  293. */
  294. int scsi_set_sense_field_pointer(u8 *buf, int buf_len, u16 fp, u8 bp, bool cd)
  295. {
  296. u8 *ucp, len;
  297. if ((buf[0] & 0x7f) == 0x72) {
  298. len = buf[7];
  299. ucp = (char *)scsi_sense_desc_find(buf, len + 8, 2);
  300. if (!ucp) {
  301. buf[7] = len + 8;
  302. ucp = buf + 8 + len;
  303. }
  304. if (buf_len < len + 8)
  305. /* Not enough room for info */
  306. return -EINVAL;
  307. ucp[0] = 2;
  308. ucp[1] = 6;
  309. ucp[4] = 0x80; /* Valid bit */
  310. if (cd)
  311. ucp[4] |= 0x40;
  312. if (bp < 0x8)
  313. ucp[4] |= 0x8 | bp;
  314. put_unaligned_be16(fp, &ucp[5]);
  315. } else if ((buf[0] & 0x7f) == 0x70) {
  316. len = buf[7];
  317. if (len < 18)
  318. buf[7] = 18;
  319. buf[15] = 0x80;
  320. if (cd)
  321. buf[15] |= 0x40;
  322. if (bp < 0x8)
  323. buf[15] |= 0x8 | bp;
  324. put_unaligned_be16(fp, &buf[16]);
  325. }
  326. return 0;
  327. }
  328. EXPORT_SYMBOL(scsi_set_sense_field_pointer);