scsi_common.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 <scsi/scsi_common.h>
  8. /* NB: These are exposed through /proc/scsi/scsi and form part of the ABI.
  9. * You may not alter any existing entry (although adding new ones is
  10. * encouraged once assigned by ANSI/INCITS T10
  11. */
  12. static const char *const scsi_device_types[] = {
  13. "Direct-Access ",
  14. "Sequential-Access",
  15. "Printer ",
  16. "Processor ",
  17. "WORM ",
  18. "CD-ROM ",
  19. "Scanner ",
  20. "Optical Device ",
  21. "Medium Changer ",
  22. "Communications ",
  23. "ASC IT8 ",
  24. "ASC IT8 ",
  25. "RAID ",
  26. "Enclosure ",
  27. "Direct-Access-RBC",
  28. "Optical card ",
  29. "Bridge controller",
  30. "Object storage ",
  31. "Automation/Drive ",
  32. "Security Manager ",
  33. "Direct-Access-ZBC",
  34. };
  35. /**
  36. * scsi_device_type - Return 17 char string indicating device type.
  37. * @type: type number to look up
  38. */
  39. const char *scsi_device_type(unsigned type)
  40. {
  41. if (type == 0x1e)
  42. return "Well-known LUN ";
  43. if (type == 0x1f)
  44. return "No Device ";
  45. if (type >= ARRAY_SIZE(scsi_device_types))
  46. return "Unknown ";
  47. return scsi_device_types[type];
  48. }
  49. EXPORT_SYMBOL(scsi_device_type);
  50. /**
  51. * scsilun_to_int - convert a scsi_lun to an int
  52. * @scsilun: struct scsi_lun to be converted.
  53. *
  54. * Description:
  55. * Convert @scsilun from a struct scsi_lun to a four byte host byte-ordered
  56. * integer, and return the result. The caller must check for
  57. * truncation before using this function.
  58. *
  59. * Notes:
  60. * For a description of the LUN format, post SCSI-3 see the SCSI
  61. * Architecture Model, for SCSI-3 see the SCSI Controller Commands.
  62. *
  63. * Given a struct scsi_lun of: d2 04 0b 03 00 00 00 00, this function
  64. * returns the integer: 0x0b03d204
  65. *
  66. * This encoding will return a standard integer LUN for LUNs smaller
  67. * than 256, which typically use a single level LUN structure with
  68. * addressing method 0.
  69. */
  70. u64 scsilun_to_int(struct scsi_lun *scsilun)
  71. {
  72. int i;
  73. u64 lun;
  74. lun = 0;
  75. for (i = 0; i < sizeof(lun); i += 2)
  76. lun = lun | (((u64)scsilun->scsi_lun[i] << ((i + 1) * 8)) |
  77. ((u64)scsilun->scsi_lun[i + 1] << (i * 8)));
  78. return lun;
  79. }
  80. EXPORT_SYMBOL(scsilun_to_int);
  81. /**
  82. * int_to_scsilun - reverts an int into a scsi_lun
  83. * @lun: integer to be reverted
  84. * @scsilun: struct scsi_lun to be set.
  85. *
  86. * Description:
  87. * Reverts the functionality of the scsilun_to_int, which packed
  88. * an 8-byte lun value into an int. This routine unpacks the int
  89. * back into the lun value.
  90. *
  91. * Notes:
  92. * Given an integer : 0x0b03d204, this function returns a
  93. * struct scsi_lun of: d2 04 0b 03 00 00 00 00
  94. *
  95. */
  96. void int_to_scsilun(u64 lun, struct scsi_lun *scsilun)
  97. {
  98. int i;
  99. memset(scsilun->scsi_lun, 0, sizeof(scsilun->scsi_lun));
  100. for (i = 0; i < sizeof(lun); i += 2) {
  101. scsilun->scsi_lun[i] = (lun >> 8) & 0xFF;
  102. scsilun->scsi_lun[i+1] = lun & 0xFF;
  103. lun = lun >> 16;
  104. }
  105. }
  106. EXPORT_SYMBOL(int_to_scsilun);
  107. /**
  108. * scsi_normalize_sense - normalize main elements from either fixed or
  109. * descriptor sense data format into a common format.
  110. *
  111. * @sense_buffer: byte array containing sense data returned by device
  112. * @sb_len: number of valid bytes in sense_buffer
  113. * @sshdr: pointer to instance of structure that common
  114. * elements are written to.
  115. *
  116. * Notes:
  117. * The "main elements" from sense data are: response_code, sense_key,
  118. * asc, ascq and additional_length (only for descriptor format).
  119. *
  120. * Typically this function can be called after a device has
  121. * responded to a SCSI command with the CHECK_CONDITION status.
  122. *
  123. * Return value:
  124. * true if valid sense data information found, else false;
  125. */
  126. bool scsi_normalize_sense(const u8 *sense_buffer, int sb_len,
  127. struct scsi_sense_hdr *sshdr)
  128. {
  129. if (!sense_buffer || !sb_len)
  130. return false;
  131. memset(sshdr, 0, sizeof(struct scsi_sense_hdr));
  132. sshdr->response_code = (sense_buffer[0] & 0x7f);
  133. if (!scsi_sense_valid(sshdr))
  134. return false;
  135. if (sshdr->response_code >= 0x72) {
  136. /*
  137. * descriptor format
  138. */
  139. if (sb_len > 1)
  140. sshdr->sense_key = (sense_buffer[1] & 0xf);
  141. if (sb_len > 2)
  142. sshdr->asc = sense_buffer[2];
  143. if (sb_len > 3)
  144. sshdr->ascq = sense_buffer[3];
  145. if (sb_len > 7)
  146. sshdr->additional_length = sense_buffer[7];
  147. } else {
  148. /*
  149. * fixed format
  150. */
  151. if (sb_len > 2)
  152. sshdr->sense_key = (sense_buffer[2] & 0xf);
  153. if (sb_len > 7) {
  154. sb_len = (sb_len < (sense_buffer[7] + 8)) ?
  155. sb_len : (sense_buffer[7] + 8);
  156. if (sb_len > 12)
  157. sshdr->asc = sense_buffer[12];
  158. if (sb_len > 13)
  159. sshdr->ascq = sense_buffer[13];
  160. }
  161. }
  162. return true;
  163. }
  164. EXPORT_SYMBOL(scsi_normalize_sense);