scsi_common.c 8.7 KB

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