2 * SCSI functions used by both the initiator and the target code.
6 #include <linux/kernel.h>
7 #include <linux/string.h>
8 #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
14 static const char *const scsi_device_types
[] = {
39 * scsi_device_type - Return 17 char string indicating device type.
40 * @type: type number to look up
42 const char *scsi_device_type(unsigned type
)
45 return "Well-known LUN ";
48 if (type
>= ARRAY_SIZE(scsi_device_types
))
50 return scsi_device_types
[type
];
52 EXPORT_SYMBOL(scsi_device_type
);
55 * scsilun_to_int - convert a scsi_lun to an int
56 * @scsilun: struct scsi_lun to be converted.
59 * Convert @scsilun from a struct scsi_lun to a four byte host byte-ordered
60 * integer, and return the result. The caller must check for
61 * truncation before using this function.
64 * For a description of the LUN format, post SCSI-3 see the SCSI
65 * Architecture Model, for SCSI-3 see the SCSI Controller Commands.
67 * Given a struct scsi_lun of: d2 04 0b 03 00 00 00 00, this function
68 * returns the integer: 0x0b03d204
70 * This encoding will return a standard integer LUN for LUNs smaller
71 * than 256, which typically use a single level LUN structure with
72 * addressing method 0.
74 u64
scsilun_to_int(struct scsi_lun
*scsilun
)
80 for (i
= 0; i
< sizeof(lun
); i
+= 2)
81 lun
= lun
| (((u64
)scsilun
->scsi_lun
[i
] << ((i
+ 1) * 8)) |
82 ((u64
)scsilun
->scsi_lun
[i
+ 1] << (i
* 8)));
85 EXPORT_SYMBOL(scsilun_to_int
);
88 * int_to_scsilun - reverts an int into a scsi_lun
89 * @lun: integer to be reverted
90 * @scsilun: struct scsi_lun to be set.
93 * Reverts the functionality of the scsilun_to_int, which packed
94 * an 8-byte lun value into an int. This routine unpacks the int
95 * back into the lun value.
98 * Given an integer : 0x0b03d204, this function returns a
99 * struct scsi_lun of: d2 04 0b 03 00 00 00 00
102 void int_to_scsilun(u64 lun
, struct scsi_lun
*scsilun
)
106 memset(scsilun
->scsi_lun
, 0, sizeof(scsilun
->scsi_lun
));
108 for (i
= 0; i
< sizeof(lun
); i
+= 2) {
109 scsilun
->scsi_lun
[i
] = (lun
>> 8) & 0xFF;
110 scsilun
->scsi_lun
[i
+1] = lun
& 0xFF;
114 EXPORT_SYMBOL(int_to_scsilun
);
117 * scsi_normalize_sense - normalize main elements from either fixed or
118 * descriptor sense data format into a common format.
120 * @sense_buffer: byte array containing sense data returned by device
121 * @sb_len: number of valid bytes in sense_buffer
122 * @sshdr: pointer to instance of structure that common
123 * elements are written to.
126 * The "main elements" from sense data are: response_code, sense_key,
127 * asc, ascq and additional_length (only for descriptor format).
129 * Typically this function can be called after a device has
130 * responded to a SCSI command with the CHECK_CONDITION status.
133 * true if valid sense data information found, else false;
135 bool scsi_normalize_sense(const u8
*sense_buffer
, int sb_len
,
136 struct scsi_sense_hdr
*sshdr
)
138 if (!sense_buffer
|| !sb_len
)
141 memset(sshdr
, 0, sizeof(struct scsi_sense_hdr
));
143 sshdr
->response_code
= (sense_buffer
[0] & 0x7f);
145 if (!scsi_sense_valid(sshdr
))
148 if (sshdr
->response_code
>= 0x72) {
153 sshdr
->sense_key
= (sense_buffer
[1] & 0xf);
155 sshdr
->asc
= sense_buffer
[2];
157 sshdr
->ascq
= sense_buffer
[3];
159 sshdr
->additional_length
= sense_buffer
[7];
165 sshdr
->sense_key
= (sense_buffer
[2] & 0xf);
167 sb_len
= (sb_len
< (sense_buffer
[7] + 8)) ?
168 sb_len
: (sense_buffer
[7] + 8);
170 sshdr
->asc
= sense_buffer
[12];
172 sshdr
->ascq
= sense_buffer
[13];
178 EXPORT_SYMBOL(scsi_normalize_sense
);