1 // SPDX-License-Identifier: GPL-2.0
3 * SCSI functions used by both the initiator and the target code.
7 #include <linux/kernel.h>
8 #include <linux/string.h>
9 #include <linux/errno.h>
10 #include <asm/unaligned.h>
11 #include <scsi/scsi_common.h>
13 /* NB: These are exposed through /proc/scsi/scsi and form part of the ABI.
14 * You may not alter any existing entry (although adding new ones is
15 * encouraged once assigned by ANSI/INCITS T10).
17 static const char *const scsi_device_types
[] = {
42 * scsi_device_type - Return 17-char string indicating device type.
43 * @type: type number to look up
45 const char *scsi_device_type(unsigned type
)
48 return "Well-known LUN ";
51 if (type
>= ARRAY_SIZE(scsi_device_types
))
53 return scsi_device_types
[type
];
55 EXPORT_SYMBOL(scsi_device_type
);
58 * scsilun_to_int - convert a scsi_lun to an int
59 * @scsilun: struct scsi_lun to be converted.
62 * Convert @scsilun from a struct scsi_lun to a four-byte host byte-ordered
63 * integer, and return the result. The caller must check for
64 * truncation before using this function.
67 * For a description of the LUN format, post SCSI-3 see the SCSI
68 * Architecture Model, for SCSI-3 see the SCSI Controller Commands.
70 * Given a struct scsi_lun of: d2 04 0b 03 00 00 00 00, this function
71 * returns the integer: 0x0b03d204
73 * This encoding will return a standard integer LUN for LUNs smaller
74 * than 256, which typically use a single level LUN structure with
75 * addressing method 0.
77 u64
scsilun_to_int(struct scsi_lun
*scsilun
)
83 for (i
= 0; i
< sizeof(lun
); i
+= 2)
84 lun
= lun
| (((u64
)scsilun
->scsi_lun
[i
] << ((i
+ 1) * 8)) |
85 ((u64
)scsilun
->scsi_lun
[i
+ 1] << (i
* 8)));
88 EXPORT_SYMBOL(scsilun_to_int
);
91 * int_to_scsilun - reverts an int into a scsi_lun
92 * @lun: integer to be reverted
93 * @scsilun: struct scsi_lun to be set.
96 * Reverts the functionality of the scsilun_to_int, which packed
97 * an 8-byte lun value into an int. This routine unpacks the int
98 * back into the lun value.
101 * Given an integer : 0x0b03d204, this function returns a
102 * struct scsi_lun of: d2 04 0b 03 00 00 00 00
105 void int_to_scsilun(u64 lun
, struct scsi_lun
*scsilun
)
109 memset(scsilun
->scsi_lun
, 0, sizeof(scsilun
->scsi_lun
));
111 for (i
= 0; i
< sizeof(lun
); i
+= 2) {
112 scsilun
->scsi_lun
[i
] = (lun
>> 8) & 0xFF;
113 scsilun
->scsi_lun
[i
+1] = lun
& 0xFF;
117 EXPORT_SYMBOL(int_to_scsilun
);
120 * scsi_normalize_sense - normalize main elements from either fixed or
121 * descriptor sense data format into a common format.
123 * @sense_buffer: byte array containing sense data returned by device
124 * @sb_len: number of valid bytes in sense_buffer
125 * @sshdr: pointer to instance of structure that common
126 * elements are written to.
129 * The "main elements" from sense data are: response_code, sense_key,
130 * asc, ascq and additional_length (only for descriptor format).
132 * Typically this function can be called after a device has
133 * responded to a SCSI command with the CHECK_CONDITION status.
136 * true if valid sense data information found, else false;
138 bool scsi_normalize_sense(const u8
*sense_buffer
, int sb_len
,
139 struct scsi_sense_hdr
*sshdr
)
141 memset(sshdr
, 0, sizeof(struct scsi_sense_hdr
));
143 if (!sense_buffer
|| !sb_len
)
146 sshdr
->response_code
= (sense_buffer
[0] & 0x7f);
148 if (!scsi_sense_valid(sshdr
))
151 if (sshdr
->response_code
>= 0x72) {
156 sshdr
->sense_key
= (sense_buffer
[1] & 0xf);
158 sshdr
->asc
= sense_buffer
[2];
160 sshdr
->ascq
= sense_buffer
[3];
162 sshdr
->additional_length
= sense_buffer
[7];
168 sshdr
->sense_key
= (sense_buffer
[2] & 0xf);
170 sb_len
= (sb_len
< (sense_buffer
[7] + 8)) ?
171 sb_len
: (sense_buffer
[7] + 8);
173 sshdr
->asc
= sense_buffer
[12];
175 sshdr
->ascq
= sense_buffer
[13];
181 EXPORT_SYMBOL(scsi_normalize_sense
);
184 * scsi_sense_desc_find - search for a given descriptor type in descriptor sense data format.
185 * @sense_buffer: byte array of descriptor format sense data
186 * @sb_len: number of valid bytes in sense_buffer
187 * @desc_type: value of descriptor type to find
188 * (e.g. 0 -> information)
191 * only valid when sense data is in descriptor format
194 * pointer to start of (first) descriptor if found else NULL
196 const u8
* scsi_sense_desc_find(const u8
* sense_buffer
, int sb_len
,
199 int add_sen_len
, add_len
, desc_len
, k
;
202 if ((sb_len
< 8) || (0 == (add_sen_len
= sense_buffer
[7])))
204 if ((sense_buffer
[0] < 0x72) || (sense_buffer
[0] > 0x73))
206 add_sen_len
= (add_sen_len
< (sb_len
- 8)) ?
207 add_sen_len
: (sb_len
- 8);
208 descp
= &sense_buffer
[8];
209 for (desc_len
= 0, k
= 0; k
< add_sen_len
; k
+= desc_len
) {
211 add_len
= (k
< (add_sen_len
- 1)) ? descp
[1]: -1;
212 desc_len
= add_len
+ 2;
213 if (descp
[0] == desc_type
)
215 if (add_len
< 0) // short descriptor ??
220 EXPORT_SYMBOL(scsi_sense_desc_find
);
223 * scsi_build_sense_buffer - build sense data in a buffer
224 * @desc: Sense format (non-zero == descriptor format,
226 * @buf: Where to build sense data
228 * @asc: Additional sense code
229 * @ascq: Additional sense code qualifier
232 void scsi_build_sense_buffer(int desc
, u8
*buf
, u8 key
, u8 asc
, u8 ascq
)
235 buf
[0] = 0x72; /* descriptor, current */
241 buf
[0] = 0x70; /* fixed, current */
248 EXPORT_SYMBOL(scsi_build_sense_buffer
);
251 * scsi_set_sense_information - set the information field in a
252 * formatted sense data buffer
253 * @buf: Where to build sense data
254 * @buf_len: buffer length
255 * @info: 64-bit information value to be set
258 * 0 on success or -EINVAL for invalid sense buffer length
260 int scsi_set_sense_information(u8
*buf
, int buf_len
, u64 info
)
262 if ((buf
[0] & 0x7f) == 0x72) {
266 ucp
= (char *)scsi_sense_desc_find(buf
, len
+ 8, 0);
272 if (buf_len
< len
+ 0xc)
273 /* Not enough room for info */
278 ucp
[2] = 0x80; /* Valid bit */
280 put_unaligned_be64(info
, &ucp
[4]);
281 } else if ((buf
[0] & 0x7f) == 0x70) {
283 * Only set the 'VALID' bit if we can represent the value
284 * correctly; otherwise just fill out the lower bytes and
285 * clear the 'VALID' flag.
287 if (info
<= 0xffffffffUL
)
291 put_unaligned_be32((u32
)info
, &buf
[3]);
296 EXPORT_SYMBOL(scsi_set_sense_information
);
299 * scsi_set_sense_field_pointer - set the field pointer sense key
300 * specific information in a formatted sense data buffer
301 * @buf: Where to build sense data
302 * @buf_len: buffer length
303 * @fp: field pointer to be set
304 * @bp: bit pointer to be set
305 * @cd: command/data bit
308 * 0 on success or -EINVAL for invalid sense buffer length
310 int scsi_set_sense_field_pointer(u8
*buf
, int buf_len
, u16 fp
, u8 bp
, bool cd
)
314 if ((buf
[0] & 0x7f) == 0x72) {
316 ucp
= (char *)scsi_sense_desc_find(buf
, len
+ 8, 2);
322 if (buf_len
< len
+ 8)
323 /* Not enough room for info */
328 ucp
[4] = 0x80; /* Valid bit */
333 put_unaligned_be16(fp
, &ucp
[5]);
334 } else if ((buf
[0] & 0x7f) == 0x70) {
344 put_unaligned_be16(fp
, &buf
[16]);
349 EXPORT_SYMBOL(scsi_set_sense_field_pointer
);