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 <asm/unaligned.h>
12 #include <scsi/scsi_common.h>
14 MODULE_LICENSE("GPL v2");
16 /* Command group 3 is reserved and should never be used. */
17 const unsigned char scsi_command_size_tbl[8] = {
18 6, 10, 10, 12, 16, 12, 10, 10
20 EXPORT_SYMBOL(scsi_command_size_tbl);
22 /* NB: These are exposed through /proc/scsi/scsi and form part of the ABI.
23 * You may not alter any existing entry (although adding new ones is
24 * encouraged once assigned by ANSI/INCITS T10).
26 static const char *const scsi_device_types[] = {
51 * scsi_device_type - Return 17-char string indicating device type.
52 * @type: type number to look up
54 const char *scsi_device_type(unsigned type)
57 return "Well-known LUN ";
60 if (type >= ARRAY_SIZE(scsi_device_types))
62 return scsi_device_types[type];
64 EXPORT_SYMBOL(scsi_device_type);
67 * scsilun_to_int - convert a scsi_lun to an int
68 * @scsilun: struct scsi_lun to be converted.
71 * Convert @scsilun from a struct scsi_lun to a four-byte host byte-ordered
72 * integer, and return the result. The caller must check for
73 * truncation before using this function.
76 * For a description of the LUN format, post SCSI-3 see the SCSI
77 * Architecture Model, for SCSI-3 see the SCSI Controller Commands.
79 * Given a struct scsi_lun of: d2 04 0b 03 00 00 00 00, this function
80 * returns the integer: 0x0b03d204
82 * This encoding will return a standard integer LUN for LUNs smaller
83 * than 256, which typically use a single level LUN structure with
84 * addressing method 0.
86 u64 scsilun_to_int(struct scsi_lun *scsilun)
92 for (i = 0; i < sizeof(lun); i += 2)
93 lun = lun | (((u64)scsilun->scsi_lun[i] << ((i + 1) * 8)) |
94 ((u64)scsilun->scsi_lun[i + 1] << (i * 8)));
97 EXPORT_SYMBOL(scsilun_to_int);
100 * int_to_scsilun - reverts an int into a scsi_lun
101 * @lun: integer to be reverted
102 * @scsilun: struct scsi_lun to be set.
105 * Reverts the functionality of the scsilun_to_int, which packed
106 * an 8-byte lun value into an int. This routine unpacks the int
107 * back into the lun value.
110 * Given an integer : 0x0b03d204, this function returns a
111 * struct scsi_lun of: d2 04 0b 03 00 00 00 00
114 void int_to_scsilun(u64 lun, struct scsi_lun *scsilun)
118 memset(scsilun->scsi_lun, 0, sizeof(scsilun->scsi_lun));
120 for (i = 0; i < sizeof(lun); i += 2) {
121 scsilun->scsi_lun[i] = (lun >> 8) & 0xFF;
122 scsilun->scsi_lun[i+1] = lun & 0xFF;
126 EXPORT_SYMBOL(int_to_scsilun);
129 * scsi_normalize_sense - normalize main elements from either fixed or
130 * descriptor sense data format into a common format.
132 * @sense_buffer: byte array containing sense data returned by device
133 * @sb_len: number of valid bytes in sense_buffer
134 * @sshdr: pointer to instance of structure that common
135 * elements are written to.
138 * The "main elements" from sense data are: response_code, sense_key,
139 * asc, ascq and additional_length (only for descriptor format).
141 * Typically this function can be called after a device has
142 * responded to a SCSI command with the CHECK_CONDITION status.
145 * true if valid sense data information found, else false;
147 bool scsi_normalize_sense(const u8 *sense_buffer, int sb_len,
148 struct scsi_sense_hdr *sshdr)
150 memset(sshdr, 0, sizeof(struct scsi_sense_hdr));
152 if (!sense_buffer || !sb_len)
155 sshdr->response_code = (sense_buffer[0] & 0x7f);
157 if (!scsi_sense_valid(sshdr))
160 if (sshdr->response_code >= 0x72) {
165 sshdr->sense_key = (sense_buffer[1] & 0xf);
167 sshdr->asc = sense_buffer[2];
169 sshdr->ascq = sense_buffer[3];
171 sshdr->additional_length = sense_buffer[7];
177 sshdr->sense_key = (sense_buffer[2] & 0xf);
179 sb_len = (sb_len < (sense_buffer[7] + 8)) ?
180 sb_len : (sense_buffer[7] + 8);
182 sshdr->asc = sense_buffer[12];
184 sshdr->ascq = sense_buffer[13];
190 EXPORT_SYMBOL(scsi_normalize_sense);
193 * scsi_sense_desc_find - search for a given descriptor type in descriptor sense data format.
194 * @sense_buffer: byte array of descriptor format sense data
195 * @sb_len: number of valid bytes in sense_buffer
196 * @desc_type: value of descriptor type to find
197 * (e.g. 0 -> information)
200 * only valid when sense data is in descriptor format
203 * pointer to start of (first) descriptor if found else NULL
205 const u8 * scsi_sense_desc_find(const u8 * sense_buffer, int sb_len,
208 int add_sen_len, add_len, desc_len, k;
211 if ((sb_len < 8) || (0 == (add_sen_len = sense_buffer[7])))
213 if ((sense_buffer[0] < 0x72) || (sense_buffer[0] > 0x73))
215 add_sen_len = (add_sen_len < (sb_len - 8)) ?
216 add_sen_len : (sb_len - 8);
217 descp = &sense_buffer[8];
218 for (desc_len = 0, k = 0; k < add_sen_len; k += desc_len) {
220 add_len = (k < (add_sen_len - 1)) ? descp[1]: -1;
221 desc_len = add_len + 2;
222 if (descp[0] == desc_type)
224 if (add_len < 0) // short descriptor ??
229 EXPORT_SYMBOL(scsi_sense_desc_find);
232 * scsi_build_sense_buffer - build sense data in a buffer
233 * @desc: Sense format (non-zero == descriptor format,
235 * @buf: Where to build sense data
237 * @asc: Additional sense code
238 * @ascq: Additional sense code qualifier
241 void scsi_build_sense_buffer(int desc, u8 *buf, u8 key, u8 asc, u8 ascq)
244 buf[0] = 0x72; /* descriptor, current */
250 buf[0] = 0x70; /* fixed, current */
257 EXPORT_SYMBOL(scsi_build_sense_buffer);
260 * scsi_set_sense_information - set the information field in a
261 * formatted sense data buffer
262 * @buf: Where to build sense data
263 * @buf_len: buffer length
264 * @info: 64-bit information value to be set
267 * 0 on success or -EINVAL for invalid sense buffer length
269 int scsi_set_sense_information(u8 *buf, int buf_len, u64 info)
271 if ((buf[0] & 0x7f) == 0x72) {
275 ucp = (char *)scsi_sense_desc_find(buf, len + 8, 0);
281 if (buf_len < len + 0xc)
282 /* Not enough room for info */
287 ucp[2] = 0x80; /* Valid bit */
289 put_unaligned_be64(info, &ucp[4]);
290 } else if ((buf[0] & 0x7f) == 0x70) {
292 * Only set the 'VALID' bit if we can represent the value
293 * correctly; otherwise just fill out the lower bytes and
294 * clear the 'VALID' flag.
296 if (info <= 0xffffffffUL)
300 put_unaligned_be32((u32)info, &buf[3]);
305 EXPORT_SYMBOL(scsi_set_sense_information);
308 * scsi_set_sense_field_pointer - set the field pointer sense key
309 * specific information in a formatted sense data buffer
310 * @buf: Where to build sense data
311 * @buf_len: buffer length
312 * @fp: field pointer to be set
313 * @bp: bit pointer to be set
314 * @cd: command/data bit
317 * 0 on success or -EINVAL for invalid sense buffer length
319 int scsi_set_sense_field_pointer(u8 *buf, int buf_len, u16 fp, u8 bp, bool cd)
323 if ((buf[0] & 0x7f) == 0x72) {
325 ucp = (char *)scsi_sense_desc_find(buf, len + 8, 2);
331 if (buf_len < len + 8)
332 /* Not enough room for info */
337 ucp[4] = 0x80; /* Valid bit */
342 put_unaligned_be16(fp, &ucp[5]);
343 } else if ((buf[0] & 0x7f) == 0x70) {
353 put_unaligned_be16(fp, &buf[16]);
358 EXPORT_SYMBOL(scsi_set_sense_field_pointer);