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 <linux/module.h>
11 #include <uapi/linux/pr.h>
12 #include <linux/unaligned.h>
13 #include <scsi/scsi_common.h>
15 MODULE_DESCRIPTION("SCSI functions used by both the initiator and the target code");
16 MODULE_LICENSE("GPL v2");
18 /* Command group 3 is reserved and should never be used. */
19 const unsigned char scsi_command_size_tbl
[8] = {
20 6, 10, 10, 12, 16, 12, 10, 10
22 EXPORT_SYMBOL(scsi_command_size_tbl
);
24 /* NB: These are exposed through /proc/scsi/scsi and form part of the ABI.
25 * You may not alter any existing entry (although adding new ones is
26 * encouraged once assigned by ANSI/INCITS T10).
28 static const char *const scsi_device_types
[] = {
53 * scsi_device_type - Return 17-char string indicating device type.
54 * @type: type number to look up
56 const char *scsi_device_type(unsigned type
)
59 return "Well-known LUN ";
62 if (type
>= ARRAY_SIZE(scsi_device_types
))
64 return scsi_device_types
[type
];
66 EXPORT_SYMBOL(scsi_device_type
);
68 enum pr_type
scsi_pr_type_to_block(enum scsi_pr_type type
)
71 case SCSI_PR_WRITE_EXCLUSIVE
:
72 return PR_WRITE_EXCLUSIVE
;
73 case SCSI_PR_EXCLUSIVE_ACCESS
:
74 return PR_EXCLUSIVE_ACCESS
;
75 case SCSI_PR_WRITE_EXCLUSIVE_REG_ONLY
:
76 return PR_WRITE_EXCLUSIVE_REG_ONLY
;
77 case SCSI_PR_EXCLUSIVE_ACCESS_REG_ONLY
:
78 return PR_EXCLUSIVE_ACCESS_REG_ONLY
;
79 case SCSI_PR_WRITE_EXCLUSIVE_ALL_REGS
:
80 return PR_WRITE_EXCLUSIVE_ALL_REGS
;
81 case SCSI_PR_EXCLUSIVE_ACCESS_ALL_REGS
:
82 return PR_EXCLUSIVE_ACCESS_ALL_REGS
;
87 EXPORT_SYMBOL_GPL(scsi_pr_type_to_block
);
89 enum scsi_pr_type
block_pr_type_to_scsi(enum pr_type type
)
92 case PR_WRITE_EXCLUSIVE
:
93 return SCSI_PR_WRITE_EXCLUSIVE
;
94 case PR_EXCLUSIVE_ACCESS
:
95 return SCSI_PR_EXCLUSIVE_ACCESS
;
96 case PR_WRITE_EXCLUSIVE_REG_ONLY
:
97 return SCSI_PR_WRITE_EXCLUSIVE_REG_ONLY
;
98 case PR_EXCLUSIVE_ACCESS_REG_ONLY
:
99 return SCSI_PR_EXCLUSIVE_ACCESS_REG_ONLY
;
100 case PR_WRITE_EXCLUSIVE_ALL_REGS
:
101 return SCSI_PR_WRITE_EXCLUSIVE_ALL_REGS
;
102 case PR_EXCLUSIVE_ACCESS_ALL_REGS
:
103 return SCSI_PR_EXCLUSIVE_ACCESS_ALL_REGS
;
108 EXPORT_SYMBOL_GPL(block_pr_type_to_scsi
);
111 * scsilun_to_int - convert a scsi_lun to an int
112 * @scsilun: struct scsi_lun to be converted.
115 * Convert @scsilun from a struct scsi_lun to a four-byte host byte-ordered
116 * integer, and return the result. The caller must check for
117 * truncation before using this function.
120 * For a description of the LUN format, post SCSI-3 see the SCSI
121 * Architecture Model, for SCSI-3 see the SCSI Controller Commands.
123 * Given a struct scsi_lun of: d2 04 0b 03 00 00 00 00, this function
124 * returns the integer: 0x0b03d204
126 * This encoding will return a standard integer LUN for LUNs smaller
127 * than 256, which typically use a single level LUN structure with
128 * addressing method 0.
130 u64
scsilun_to_int(struct scsi_lun
*scsilun
)
136 for (i
= 0; i
< sizeof(lun
); i
+= 2)
137 lun
= lun
| (((u64
)scsilun
->scsi_lun
[i
] << ((i
+ 1) * 8)) |
138 ((u64
)scsilun
->scsi_lun
[i
+ 1] << (i
* 8)));
141 EXPORT_SYMBOL(scsilun_to_int
);
144 * int_to_scsilun - reverts an int into a scsi_lun
145 * @lun: integer to be reverted
146 * @scsilun: struct scsi_lun to be set.
149 * Reverts the functionality of the scsilun_to_int, which packed
150 * an 8-byte lun value into an int. This routine unpacks the int
151 * back into the lun value.
154 * Given an integer : 0x0b03d204, this function returns a
155 * struct scsi_lun of: d2 04 0b 03 00 00 00 00
158 void int_to_scsilun(u64 lun
, struct scsi_lun
*scsilun
)
162 memset(scsilun
->scsi_lun
, 0, sizeof(scsilun
->scsi_lun
));
164 for (i
= 0; i
< sizeof(lun
); i
+= 2) {
165 scsilun
->scsi_lun
[i
] = (lun
>> 8) & 0xFF;
166 scsilun
->scsi_lun
[i
+1] = lun
& 0xFF;
170 EXPORT_SYMBOL(int_to_scsilun
);
173 * scsi_normalize_sense - normalize main elements from either fixed or
174 * descriptor sense data format into a common format.
176 * @sense_buffer: byte array containing sense data returned by device
177 * @sb_len: number of valid bytes in sense_buffer
178 * @sshdr: pointer to instance of structure that common
179 * elements are written to.
182 * The "main elements" from sense data are: response_code, sense_key,
183 * asc, ascq and additional_length (only for descriptor format).
185 * Typically this function can be called after a device has
186 * responded to a SCSI command with the CHECK_CONDITION status.
189 * true if valid sense data information found, else false;
191 bool scsi_normalize_sense(const u8
*sense_buffer
, int sb_len
,
192 struct scsi_sense_hdr
*sshdr
)
194 memset(sshdr
, 0, sizeof(struct scsi_sense_hdr
));
196 if (!sense_buffer
|| !sb_len
)
199 sshdr
->response_code
= (sense_buffer
[0] & 0x7f);
201 if (!scsi_sense_valid(sshdr
))
204 if (sshdr
->response_code
>= 0x72) {
209 sshdr
->sense_key
= (sense_buffer
[1] & 0xf);
211 sshdr
->asc
= sense_buffer
[2];
213 sshdr
->ascq
= sense_buffer
[3];
215 sshdr
->additional_length
= sense_buffer
[7];
221 sshdr
->sense_key
= (sense_buffer
[2] & 0xf);
223 sb_len
= min(sb_len
, sense_buffer
[7] + 8);
225 sshdr
->asc
= sense_buffer
[12];
227 sshdr
->ascq
= sense_buffer
[13];
233 EXPORT_SYMBOL(scsi_normalize_sense
);
236 * scsi_sense_desc_find - search for a given descriptor type in descriptor sense data format.
237 * @sense_buffer: byte array of descriptor format sense data
238 * @sb_len: number of valid bytes in sense_buffer
239 * @desc_type: value of descriptor type to find
240 * (e.g. 0 -> information)
243 * only valid when sense data is in descriptor format
246 * pointer to start of (first) descriptor if found else NULL
248 const u8
* scsi_sense_desc_find(const u8
* sense_buffer
, int sb_len
,
251 int add_sen_len
, add_len
, desc_len
, k
;
254 if ((sb_len
< 8) || (0 == (add_sen_len
= sense_buffer
[7])))
256 if ((sense_buffer
[0] < 0x72) || (sense_buffer
[0] > 0x73))
258 add_sen_len
= (add_sen_len
< (sb_len
- 8)) ?
259 add_sen_len
: (sb_len
- 8);
260 descp
= &sense_buffer
[8];
261 for (desc_len
= 0, k
= 0; k
< add_sen_len
; k
+= desc_len
) {
263 add_len
= (k
< (add_sen_len
- 1)) ? descp
[1]: -1;
264 desc_len
= add_len
+ 2;
265 if (descp
[0] == desc_type
)
267 if (add_len
< 0) // short descriptor ??
272 EXPORT_SYMBOL(scsi_sense_desc_find
);
275 * scsi_build_sense_buffer - build sense data in a buffer
276 * @desc: Sense format (non-zero == descriptor format,
278 * @buf: Where to build sense data
280 * @asc: Additional sense code
281 * @ascq: Additional sense code qualifier
284 void scsi_build_sense_buffer(int desc
, u8
*buf
, u8 key
, u8 asc
, u8 ascq
)
287 buf
[0] = 0x72; /* descriptor, current */
293 buf
[0] = 0x70; /* fixed, current */
300 EXPORT_SYMBOL(scsi_build_sense_buffer
);
303 * scsi_set_sense_information - set the information field in a
304 * formatted sense data buffer
305 * @buf: Where to build sense data
306 * @buf_len: buffer length
307 * @info: 64-bit information value to be set
310 * 0 on success or -EINVAL for invalid sense buffer length
312 int scsi_set_sense_information(u8
*buf
, int buf_len
, u64 info
)
314 if ((buf
[0] & 0x7f) == 0x72) {
318 ucp
= (char *)scsi_sense_desc_find(buf
, len
+ 8, 0);
324 if (buf_len
< len
+ 0xc)
325 /* Not enough room for info */
330 ucp
[2] = 0x80; /* Valid bit */
332 put_unaligned_be64(info
, &ucp
[4]);
333 } else if ((buf
[0] & 0x7f) == 0x70) {
335 * Only set the 'VALID' bit if we can represent the value
336 * correctly; otherwise just fill out the lower bytes and
337 * clear the 'VALID' flag.
339 if (info
<= 0xffffffffUL
)
343 put_unaligned_be32((u32
)info
, &buf
[3]);
348 EXPORT_SYMBOL(scsi_set_sense_information
);
351 * scsi_set_sense_field_pointer - set the field pointer sense key
352 * specific information in a formatted sense data buffer
353 * @buf: Where to build sense data
354 * @buf_len: buffer length
355 * @fp: field pointer to be set
356 * @bp: bit pointer to be set
357 * @cd: command/data bit
360 * 0 on success or -EINVAL for invalid sense buffer length
362 int scsi_set_sense_field_pointer(u8
*buf
, int buf_len
, u16 fp
, u8 bp
, bool cd
)
366 if ((buf
[0] & 0x7f) == 0x72) {
368 ucp
= (char *)scsi_sense_desc_find(buf
, len
+ 8, 2);
374 if (buf_len
< len
+ 8)
375 /* Not enough room for info */
380 ucp
[4] = 0x80; /* Valid bit */
385 put_unaligned_be16(fp
, &ucp
[5]);
386 } else if ((buf
[0] & 0x7f) == 0x70) {
396 put_unaligned_be16(fp
, &buf
[16]);
401 EXPORT_SYMBOL(scsi_set_sense_field_pointer
);