2 * SCSI functions used by both the initiator and the target code.
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>
12 /* NB: These are exposed through /proc/scsi/scsi and form part of the ABI.
13 * You may not alter any existing entry (although adding new ones is
14 * encouraged once assigned by ANSI/INCITS T10
16 static const char *const scsi_device_types
[] = {
41 * scsi_device_type - Return 17 char string indicating device type.
42 * @type: type number to look up
44 const char *scsi_device_type(unsigned type
)
47 return "Well-known LUN ";
50 if (type
>= ARRAY_SIZE(scsi_device_types
))
52 return scsi_device_types
[type
];
54 EXPORT_SYMBOL(scsi_device_type
);
57 * scsilun_to_int - convert a scsi_lun to an int
58 * @scsilun: struct scsi_lun to be converted.
61 * Convert @scsilun from a struct scsi_lun to a four byte host byte-ordered
62 * integer, and return the result. The caller must check for
63 * truncation before using this function.
66 * For a description of the LUN format, post SCSI-3 see the SCSI
67 * Architecture Model, for SCSI-3 see the SCSI Controller Commands.
69 * Given a struct scsi_lun of: d2 04 0b 03 00 00 00 00, this function
70 * returns the integer: 0x0b03d204
72 * This encoding will return a standard integer LUN for LUNs smaller
73 * than 256, which typically use a single level LUN structure with
74 * addressing method 0.
76 u64
scsilun_to_int(struct scsi_lun
*scsilun
)
82 for (i
= 0; i
< sizeof(lun
); i
+= 2)
83 lun
= lun
| (((u64
)scsilun
->scsi_lun
[i
] << ((i
+ 1) * 8)) |
84 ((u64
)scsilun
->scsi_lun
[i
+ 1] << (i
* 8)));
87 EXPORT_SYMBOL(scsilun_to_int
);
90 * int_to_scsilun - reverts an int into a scsi_lun
91 * @lun: integer to be reverted
92 * @scsilun: struct scsi_lun to be set.
95 * Reverts the functionality of the scsilun_to_int, which packed
96 * an 8-byte lun value into an int. This routine unpacks the int
97 * back into the lun value.
100 * Given an integer : 0x0b03d204, this function returns a
101 * struct scsi_lun of: d2 04 0b 03 00 00 00 00
104 void int_to_scsilun(u64 lun
, struct scsi_lun
*scsilun
)
108 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;
116 EXPORT_SYMBOL(int_to_scsilun
);
119 * scsi_normalize_sense - normalize main elements from either fixed or
120 * descriptor sense data format into a common format.
122 * @sense_buffer: byte array containing sense data returned by device
123 * @sb_len: number of valid bytes in sense_buffer
124 * @sshdr: pointer to instance of structure that common
125 * elements are written to.
128 * The "main elements" from sense data are: response_code, sense_key,
129 * asc, ascq and additional_length (only for descriptor format).
131 * Typically this function can be called after a device has
132 * responded to a SCSI command with the CHECK_CONDITION status.
135 * true if valid sense data information found, else false;
137 bool scsi_normalize_sense(const u8
*sense_buffer
, int sb_len
,
138 struct scsi_sense_hdr
*sshdr
)
140 if (!sense_buffer
|| !sb_len
)
143 memset(sshdr
, 0, sizeof(struct scsi_sense_hdr
));
145 sshdr
->response_code
= (sense_buffer
[0] & 0x7f);
147 if (!scsi_sense_valid(sshdr
))
150 if (sshdr
->response_code
>= 0x72) {
155 sshdr
->sense_key
= (sense_buffer
[1] & 0xf);
157 sshdr
->asc
= sense_buffer
[2];
159 sshdr
->ascq
= sense_buffer
[3];
161 sshdr
->additional_length
= sense_buffer
[7];
167 sshdr
->sense_key
= (sense_buffer
[2] & 0xf);
169 sb_len
= (sb_len
< (sense_buffer
[7] + 8)) ?
170 sb_len
: (sense_buffer
[7] + 8);
172 sshdr
->asc
= sense_buffer
[12];
174 sshdr
->ascq
= sense_buffer
[13];
180 EXPORT_SYMBOL(scsi_normalize_sense
);
183 * scsi_sense_desc_find - search for a given descriptor type in descriptor sense data format.
184 * @sense_buffer: byte array of descriptor format sense data
185 * @sb_len: number of valid bytes in sense_buffer
186 * @desc_type: value of descriptor type to find
187 * (e.g. 0 -> information)
190 * only valid when sense data is in descriptor format
193 * pointer to start of (first) descriptor if found else NULL
195 const u8
* scsi_sense_desc_find(const u8
* sense_buffer
, int sb_len
,
198 int add_sen_len
, add_len
, desc_len
, k
;
201 if ((sb_len
< 8) || (0 == (add_sen_len
= sense_buffer
[7])))
203 if ((sense_buffer
[0] < 0x72) || (sense_buffer
[0] > 0x73))
205 add_sen_len
= (add_sen_len
< (sb_len
- 8)) ?
206 add_sen_len
: (sb_len
- 8);
207 descp
= &sense_buffer
[8];
208 for (desc_len
= 0, k
= 0; k
< add_sen_len
; k
+= desc_len
) {
210 add_len
= (k
< (add_sen_len
- 1)) ? descp
[1]: -1;
211 desc_len
= add_len
+ 2;
212 if (descp
[0] == desc_type
)
214 if (add_len
< 0) // short descriptor ??
219 EXPORT_SYMBOL(scsi_sense_desc_find
);
222 * scsi_build_sense_buffer - build sense data in a buffer
223 * @desc: Sense format (non zero == descriptor format,
225 * @buf: Where to build sense data
227 * @asc: Additional sense code
228 * @ascq: Additional sense code qualifier
231 void scsi_build_sense_buffer(int desc
, u8
*buf
, u8 key
, u8 asc
, u8 ascq
)
234 buf
[0] = 0x72; /* descriptor, current */
240 buf
[0] = 0x70; /* fixed, current */
247 EXPORT_SYMBOL(scsi_build_sense_buffer
);
250 * scsi_set_sense_information - set the information field in a
251 * formatted sense data buffer
252 * @buf: Where to build sense data
253 * @buf_len: buffer length
254 * @info: 64-bit information value to be set
257 * 0 on success or EINVAL for invalid sense buffer length
259 int scsi_set_sense_information(u8
*buf
, int buf_len
, u64 info
)
261 if ((buf
[0] & 0x7f) == 0x72) {
265 ucp
= (char *)scsi_sense_desc_find(buf
, len
+ 8, 0);
271 if (buf_len
< len
+ 0xc)
272 /* Not enough room for info */
277 ucp
[2] = 0x80; /* Valid bit */
279 put_unaligned_be64(info
, &ucp
[4]);
280 } else if ((buf
[0] & 0x7f) == 0x70) {
282 * Only set the 'VALID' bit if we can represent the value
283 * correctly; otherwise just fill out the lower bytes and
284 * clear the 'VALID' flag.
286 if (info
<= 0xffffffffUL
)
290 put_unaligned_be32((u32
)info
, &buf
[3]);
295 EXPORT_SYMBOL(scsi_set_sense_information
);
298 * scsi_set_sense_field_pointer - set the field pointer sense key
299 * specific information in a formatted sense data buffer
300 * @buf: Where to build sense data
301 * @buf_len: buffer length
302 * @fp: field pointer to be set
303 * @bp: bit pointer to be set
304 * @cd: command/data bit
307 * 0 on success or EINVAL for invalid sense buffer length
309 int scsi_set_sense_field_pointer(u8
*buf
, int buf_len
, u16 fp
, u8 bp
, bool cd
)
313 if ((buf
[0] & 0x7f) == 0x72) {
315 ucp
= (char *)scsi_sense_desc_find(buf
, len
+ 8, 2);
321 if (buf_len
< len
+ 8)
322 /* Not enough room for info */
327 ucp
[4] = 0x80; /* Valid bit */
332 put_unaligned_be16(fp
, &ucp
[5]);
333 } else if ((buf
[0] & 0x7f) == 0x70) {
343 put_unaligned_be16(fp
, &buf
[16]);
348 EXPORT_SYMBOL(scsi_set_sense_field_pointer
);