blob: 2ff092252b7624b0f62938b8c59d78dce1d5b616 [file] [log] [blame]
Bart Van Assche07e38422015-05-08 10:07:48 +02001/*
2 * SCSI functions used by both the initiator and the target code.
3 */
4
5#include <linux/bug.h>
6#include <linux/kernel.h>
7#include <linux/string.h>
8#include <scsi/scsi_common.h>
9
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
13 */
14static const char *const scsi_device_types[] = {
15 "Direct-Access ",
16 "Sequential-Access",
17 "Printer ",
18 "Processor ",
19 "WORM ",
20 "CD-ROM ",
21 "Scanner ",
22 "Optical Device ",
23 "Medium Changer ",
24 "Communications ",
25 "ASC IT8 ",
26 "ASC IT8 ",
27 "RAID ",
28 "Enclosure ",
29 "Direct-Access-RBC",
30 "Optical card ",
31 "Bridge controller",
32 "Object storage ",
33 "Automation/Drive ",
34 "Security Manager ",
35 "Direct-Access-ZBC",
36};
37
38/**
39 * scsi_device_type - Return 17 char string indicating device type.
40 * @type: type number to look up
41 */
42const char *scsi_device_type(unsigned type)
43{
44 if (type == 0x1e)
45 return "Well-known LUN ";
46 if (type == 0x1f)
47 return "No Device ";
48 if (type >= ARRAY_SIZE(scsi_device_types))
49 return "Unknown ";
50 return scsi_device_types[type];
51}
52EXPORT_SYMBOL(scsi_device_type);
53
54/**
55 * scsilun_to_int - convert a scsi_lun to an int
56 * @scsilun: struct scsi_lun to be converted.
57 *
58 * Description:
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.
62 *
63 * Notes:
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.
66 *
67 * Given a struct scsi_lun of: d2 04 0b 03 00 00 00 00, this function
68 * returns the integer: 0x0b03d204
69 *
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.
73 */
74u64 scsilun_to_int(struct scsi_lun *scsilun)
75{
76 int i;
77 u64 lun;
78
79 lun = 0;
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)));
83 return lun;
84}
85EXPORT_SYMBOL(scsilun_to_int);
86
87/**
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.
91 *
92 * Description:
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.
96 *
97 * Notes:
98 * Given an integer : 0x0b03d204, this function returns a
99 * struct scsi_lun of: d2 04 0b 03 00 00 00 00
100 *
101 */
102void int_to_scsilun(u64 lun, struct scsi_lun *scsilun)
103{
104 int i;
105
106 memset(scsilun->scsi_lun, 0, sizeof(scsilun->scsi_lun));
107
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;
111 lun = lun >> 16;
112 }
113}
114EXPORT_SYMBOL(int_to_scsilun);
115
116/**
117 * scsi_normalize_sense - normalize main elements from either fixed or
118 * descriptor sense data format into a common format.
119 *
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.
124 *
125 * Notes:
126 * The "main elements" from sense data are: response_code, sense_key,
127 * asc, ascq and additional_length (only for descriptor format).
128 *
129 * Typically this function can be called after a device has
130 * responded to a SCSI command with the CHECK_CONDITION status.
131 *
132 * Return value:
133 * true if valid sense data information found, else false;
134 */
135bool scsi_normalize_sense(const u8 *sense_buffer, int sb_len,
136 struct scsi_sense_hdr *sshdr)
137{
138 if (!sense_buffer || !sb_len)
139 return false;
140
141 memset(sshdr, 0, sizeof(struct scsi_sense_hdr));
142
143 sshdr->response_code = (sense_buffer[0] & 0x7f);
144
145 if (!scsi_sense_valid(sshdr))
146 return false;
147
148 if (sshdr->response_code >= 0x72) {
149 /*
150 * descriptor format
151 */
152 if (sb_len > 1)
153 sshdr->sense_key = (sense_buffer[1] & 0xf);
154 if (sb_len > 2)
155 sshdr->asc = sense_buffer[2];
156 if (sb_len > 3)
157 sshdr->ascq = sense_buffer[3];
158 if (sb_len > 7)
159 sshdr->additional_length = sense_buffer[7];
160 } else {
161 /*
162 * fixed format
163 */
164 if (sb_len > 2)
165 sshdr->sense_key = (sense_buffer[2] & 0xf);
166 if (sb_len > 7) {
167 sb_len = (sb_len < (sense_buffer[7] + 8)) ?
168 sb_len : (sense_buffer[7] + 8);
169 if (sb_len > 12)
170 sshdr->asc = sense_buffer[12];
171 if (sb_len > 13)
172 sshdr->ascq = sense_buffer[13];
173 }
174 }
175
176 return true;
177}
178EXPORT_SYMBOL(scsi_normalize_sense);