scsi_common.c 8.7 KB

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