]>
Commit | Line | Data |
---|---|---|
52ad194e | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
663996b3 | 2 | /* |
b012e921 | 3 | * Copyright © IBM Corp. 2003 |
663996b3 MS |
4 | * |
5 | * Author: Patrick Mansfield<patmans@us.ibm.com> | |
663996b3 MS |
6 | */ |
7 | ||
663996b3 | 8 | #include <errno.h> |
663996b3 | 9 | #include <fcntl.h> |
663996b3 | 10 | #include <inttypes.h> |
db2df898 MP |
11 | #include <linux/bsg.h> |
12 | #include <linux/types.h> | |
663996b3 MS |
13 | #include <scsi/scsi.h> |
14 | #include <scsi/sg.h> | |
db2df898 MP |
15 | #include <stdio.h> |
16 | #include <stdlib.h> | |
17 | #include <string.h> | |
18 | #include <sys/ioctl.h> | |
19 | #include <sys/stat.h> | |
20 | #include <sys/types.h> | |
21 | #include <time.h> | |
22 | #include <unistd.h> | |
663996b3 | 23 | |
bb4f798a | 24 | #include "memory-util.h" |
db2df898 | 25 | #include "random-util.h" |
663996b3 MS |
26 | #include "scsi.h" |
27 | #include "scsi_id.h" | |
db2df898 | 28 | #include "string-util.h" |
663996b3 MS |
29 | |
30 | /* | |
31 | * A priority based list of id, naa, and binary/ascii for the identifier | |
32 | * descriptor in VPD page 0x83. | |
33 | * | |
34 | * Brute force search for a match starting with the first value in the | |
35 | * following id_search_list. This is not a performance issue, since there | |
36 | * is normally one or some small number of descriptors. | |
37 | */ | |
38 | static const struct scsi_id_search_values id_search_list[] = { | |
4c89c718 MP |
39 | { SCSI_ID_TGTGROUP, SCSI_ID_NAA_DONT_CARE, SCSI_ID_BINARY }, |
40 | { SCSI_ID_NAA, SCSI_ID_NAA_IEEE_REG_EXTENDED, SCSI_ID_BINARY }, | |
41 | { SCSI_ID_NAA, SCSI_ID_NAA_IEEE_REG_EXTENDED, SCSI_ID_ASCII }, | |
42 | { SCSI_ID_NAA, SCSI_ID_NAA_IEEE_REG, SCSI_ID_BINARY }, | |
43 | { SCSI_ID_NAA, SCSI_ID_NAA_IEEE_REG, SCSI_ID_ASCII }, | |
663996b3 MS |
44 | /* |
45 | * Devices already exist using NAA values that are now marked | |
46 | * reserved. These should not conflict with other values, or it is | |
47 | * a bug in the device. As long as we find the IEEE extended one | |
48 | * first, we really don't care what other ones are used. Using | |
49 | * don't care here means that a device that returns multiple | |
50 | * non-IEEE descriptors in a random order will get different | |
51 | * names. | |
52 | */ | |
4c89c718 MP |
53 | { SCSI_ID_NAA, SCSI_ID_NAA_DONT_CARE, SCSI_ID_BINARY }, |
54 | { SCSI_ID_NAA, SCSI_ID_NAA_DONT_CARE, SCSI_ID_ASCII }, | |
55 | { SCSI_ID_EUI_64, SCSI_ID_NAA_DONT_CARE, SCSI_ID_BINARY }, | |
56 | { SCSI_ID_EUI_64, SCSI_ID_NAA_DONT_CARE, SCSI_ID_ASCII }, | |
57 | { SCSI_ID_T10_VENDOR, SCSI_ID_NAA_DONT_CARE, SCSI_ID_BINARY }, | |
58 | { SCSI_ID_T10_VENDOR, SCSI_ID_NAA_DONT_CARE, SCSI_ID_ASCII }, | |
59 | { SCSI_ID_VENDOR_SPECIFIC, SCSI_ID_NAA_DONT_CARE, SCSI_ID_BINARY }, | |
60 | { SCSI_ID_VENDOR_SPECIFIC, SCSI_ID_NAA_DONT_CARE, SCSI_ID_ASCII }, | |
663996b3 MS |
61 | }; |
62 | ||
63 | static const char hex_str[]="0123456789abcdef"; | |
64 | ||
65 | /* | |
66 | * Values returned in the result/status, only the ones used by the code | |
67 | * are used here. | |
68 | */ | |
69 | ||
4c89c718 MP |
70 | #define DID_NO_CONNECT 0x01 /* Unable to connect before timeout */ |
71 | #define DID_BUS_BUSY 0x02 /* Bus remain busy until timeout */ | |
72 | #define DID_TIME_OUT 0x03 /* Timed out for some other reason */ | |
73 | #define DRIVER_TIMEOUT 0x06 | |
74 | #define DRIVER_SENSE 0x08 /* Sense_buffer has been set */ | |
663996b3 MS |
75 | |
76 | /* The following "category" function returns one of the following */ | |
77 | #define SG_ERR_CAT_CLEAN 0 /* No errors or other information */ | |
78 | #define SG_ERR_CAT_MEDIA_CHANGED 1 /* interpreted from sense buffer */ | |
79 | #define SG_ERR_CAT_RESET 2 /* interpreted from sense buffer */ | |
4c89c718 MP |
80 | #define SG_ERR_CAT_TIMEOUT 3 |
81 | #define SG_ERR_CAT_RECOVERED 4 /* Successful command after recovered err */ | |
82 | #define SG_ERR_CAT_NOTSUPPORTED 5 /* Illegal / unsupported command */ | |
83 | #define SG_ERR_CAT_SENSE 98 /* Something else in the sense buffer */ | |
84 | #define SG_ERR_CAT_OTHER 99 /* Some other error/warning */ | |
663996b3 | 85 | |
6e866b33 | 86 | static int do_scsi_page80_inquiry(struct scsi_id_device *dev_scsi, int fd, |
663996b3 MS |
87 | char *serial, char *serial_short, int max_len); |
88 | ||
6e866b33 | 89 | static int sg_err_category_new(int scsi_status, int msg_status, int |
663996b3 | 90 | host_status, int driver_status, const |
6e866b33 | 91 | unsigned char *sense_buffer, int sb_len) { |
663996b3 MS |
92 | scsi_status &= 0x7e; |
93 | ||
94 | /* | |
95 | * XXX change to return only two values - failed or OK. | |
96 | */ | |
97 | ||
98 | if (!scsi_status && !host_status && !driver_status) | |
99 | return SG_ERR_CAT_CLEAN; | |
100 | ||
f5e65279 MB |
101 | if (IN_SET(scsi_status, SCSI_CHECK_CONDITION, SCSI_COMMAND_TERMINATED) || |
102 | (driver_status & 0xf) == DRIVER_SENSE) { | |
663996b3 MS |
103 | if (sense_buffer && (sb_len > 2)) { |
104 | int sense_key; | |
105 | unsigned char asc; | |
106 | ||
107 | if (sense_buffer[0] & 0x2) { | |
108 | sense_key = sense_buffer[1] & 0xf; | |
109 | asc = sense_buffer[2]; | |
110 | } else { | |
111 | sense_key = sense_buffer[2] & 0xf; | |
112 | asc = (sb_len > 12) ? sense_buffer[12] : 0; | |
113 | } | |
114 | ||
115 | if (sense_key == RECOVERED_ERROR) | |
116 | return SG_ERR_CAT_RECOVERED; | |
117 | else if (sense_key == UNIT_ATTENTION) { | |
118 | if (0x28 == asc) | |
119 | return SG_ERR_CAT_MEDIA_CHANGED; | |
120 | if (0x29 == asc) | |
121 | return SG_ERR_CAT_RESET; | |
6300502b | 122 | } else if (sense_key == ILLEGAL_REQUEST) |
663996b3 | 123 | return SG_ERR_CAT_NOTSUPPORTED; |
663996b3 MS |
124 | } |
125 | return SG_ERR_CAT_SENSE; | |
126 | } | |
127 | if (host_status) { | |
f5e65279 | 128 | if (IN_SET(host_status, DID_NO_CONNECT, DID_BUS_BUSY, DID_TIME_OUT)) |
663996b3 MS |
129 | return SG_ERR_CAT_TIMEOUT; |
130 | } | |
131 | if (driver_status) { | |
132 | if (driver_status == DRIVER_TIMEOUT) | |
133 | return SG_ERR_CAT_TIMEOUT; | |
134 | } | |
135 | return SG_ERR_CAT_OTHER; | |
136 | } | |
137 | ||
6e866b33 MB |
138 | static int sg_err_category3(struct sg_io_hdr *hp) { |
139 | return sg_err_category_new(hp->status, hp->msg_status, | |
663996b3 MS |
140 | hp->host_status, hp->driver_status, |
141 | hp->sbp, hp->sb_len_wr); | |
142 | } | |
143 | ||
6e866b33 MB |
144 | static int sg_err_category4(struct sg_io_v4 *hp) { |
145 | return sg_err_category_new(hp->device_status, 0, | |
663996b3 MS |
146 | hp->transport_status, hp->driver_status, |
147 | (unsigned char *)(uintptr_t)hp->response, | |
148 | hp->response_len); | |
149 | } | |
150 | ||
6e866b33 MB |
151 | static int scsi_dump_sense(struct scsi_id_device *dev_scsi, |
152 | unsigned char *sense_buffer, int sb_len) { | |
663996b3 MS |
153 | int s; |
154 | int code; | |
155 | int sense_class; | |
156 | int sense_key; | |
157 | int asc, ascq; | |
663996b3 MS |
158 | |
159 | /* | |
160 | * Figure out and print the sense key, asc and ascq. | |
161 | * | |
162 | * If you want to suppress these for a particular drive model, add | |
163 | * a black list entry in the scsi_id config file. | |
164 | * | |
165 | * XXX We probably need to: lookup the sense/asc/ascq in a retry | |
166 | * table, and if found return 1 (after dumping the sense, asc, and | |
167 | * ascq). So, if/when we get something like a power on/reset, | |
168 | * we'll retry the command. | |
169 | */ | |
170 | ||
171 | if (sb_len < 1) { | |
60f067b4 | 172 | log_debug("%s: sense buffer empty", dev_scsi->kernel); |
663996b3 MS |
173 | return -1; |
174 | } | |
175 | ||
176 | sense_class = (sense_buffer[0] >> 4) & 0x07; | |
177 | code = sense_buffer[0] & 0xf; | |
178 | ||
179 | if (sense_class == 7) { | |
180 | /* | |
181 | * extended sense data. | |
182 | */ | |
183 | s = sense_buffer[7] + 8; | |
184 | if (sb_len < s) { | |
60f067b4 | 185 | log_debug("%s: sense buffer too small %d bytes, %d bytes too short", |
4c89c718 | 186 | dev_scsi->kernel, sb_len, s - sb_len); |
663996b3 MS |
187 | return -1; |
188 | } | |
f5e65279 | 189 | if (IN_SET(code, 0x0, 0x1)) { |
663996b3 MS |
190 | sense_key = sense_buffer[2] & 0xf; |
191 | if (s < 14) { | |
192 | /* | |
193 | * Possible? | |
194 | */ | |
60f067b4 | 195 | log_debug("%s: sense result too" " small %d bytes", |
4c89c718 | 196 | dev_scsi->kernel, s); |
663996b3 MS |
197 | return -1; |
198 | } | |
199 | asc = sense_buffer[12]; | |
200 | ascq = sense_buffer[13]; | |
f5e65279 | 201 | } else if (IN_SET(code, 0x2, 0x3)) { |
663996b3 MS |
202 | sense_key = sense_buffer[1] & 0xf; |
203 | asc = sense_buffer[2]; | |
204 | ascq = sense_buffer[3]; | |
205 | } else { | |
60f067b4 | 206 | log_debug("%s: invalid sense code 0x%x", |
4c89c718 | 207 | dev_scsi->kernel, code); |
663996b3 MS |
208 | return -1; |
209 | } | |
60f067b4 | 210 | log_debug("%s: sense key 0x%x ASC 0x%x ASCQ 0x%x", |
4c89c718 | 211 | dev_scsi->kernel, sense_key, asc, ascq); |
663996b3 MS |
212 | } else { |
213 | if (sb_len < 4) { | |
60f067b4 | 214 | log_debug("%s: sense buffer too small %d bytes, %d bytes too short", |
4c89c718 | 215 | dev_scsi->kernel, sb_len, 4 - sb_len); |
663996b3 MS |
216 | return -1; |
217 | } | |
218 | ||
219 | if (sense_buffer[0] < 15) | |
60f067b4 | 220 | log_debug("%s: old sense key: 0x%x", dev_scsi->kernel, sense_buffer[0] & 0x0f); |
663996b3 | 221 | else |
60f067b4 | 222 | log_debug("%s: sense = %2x %2x", |
4c89c718 | 223 | dev_scsi->kernel, sense_buffer[0], sense_buffer[2]); |
60f067b4 | 224 | log_debug("%s: non-extended sense class %d code 0x%0x", |
4c89c718 | 225 | dev_scsi->kernel, sense_class, code); |
663996b3 MS |
226 | |
227 | } | |
228 | ||
663996b3 MS |
229 | return -1; |
230 | } | |
231 | ||
6e866b33 | 232 | static int scsi_dump(struct scsi_id_device *dev_scsi, struct sg_io_hdr *io) { |
663996b3 MS |
233 | if (!io->status && !io->host_status && !io->msg_status && |
234 | !io->driver_status) { | |
235 | /* | |
236 | * Impossible, should not be called. | |
237 | */ | |
60f067b4 | 238 | log_debug("%s: called with no error", __FUNCTION__); |
663996b3 MS |
239 | return -1; |
240 | } | |
241 | ||
60f067b4 | 242 | log_debug("%s: sg_io failed status 0x%x 0x%x 0x%x 0x%x", |
4c89c718 | 243 | dev_scsi->kernel, io->driver_status, io->host_status, io->msg_status, io->status); |
663996b3 | 244 | if (io->status == SCSI_CHECK_CONDITION) |
6e866b33 | 245 | return scsi_dump_sense(dev_scsi, io->sbp, io->sb_len_wr); |
663996b3 MS |
246 | else |
247 | return -1; | |
248 | } | |
249 | ||
6e866b33 | 250 | static int scsi_dump_v4(struct scsi_id_device *dev_scsi, struct sg_io_v4 *io) { |
663996b3 MS |
251 | if (!io->device_status && !io->transport_status && |
252 | !io->driver_status) { | |
253 | /* | |
254 | * Impossible, should not be called. | |
255 | */ | |
60f067b4 | 256 | log_debug("%s: called with no error", __FUNCTION__); |
663996b3 MS |
257 | return -1; |
258 | } | |
259 | ||
60f067b4 | 260 | log_debug("%s: sg_io failed status 0x%x 0x%x 0x%x", |
4c89c718 | 261 | dev_scsi->kernel, io->driver_status, io->transport_status, io->device_status); |
663996b3 | 262 | if (io->device_status == SCSI_CHECK_CONDITION) |
6e866b33 | 263 | return scsi_dump_sense(dev_scsi, (unsigned char *)(uintptr_t)io->response, |
663996b3 MS |
264 | io->response_len); |
265 | else | |
266 | return -1; | |
267 | } | |
268 | ||
6e866b33 | 269 | static int scsi_inquiry(struct scsi_id_device *dev_scsi, int fd, |
663996b3 | 270 | unsigned char evpd, unsigned char page, |
6e866b33 | 271 | unsigned char *buf, unsigned buflen) { |
663996b3 MS |
272 | unsigned char inq_cmd[INQUIRY_CMDLEN] = |
273 | { INQUIRY_CMD, evpd, page, 0, buflen, 0 }; | |
274 | unsigned char sense[SENSE_BUFF_LEN]; | |
275 | void *io_buf; | |
276 | struct sg_io_v4 io_v4; | |
277 | struct sg_io_hdr io_hdr; | |
278 | int retry = 3; /* rather random */ | |
279 | int retval; | |
280 | ||
281 | if (buflen > SCSI_INQ_BUFF_LEN) { | |
60f067b4 | 282 | log_debug("buflen %d too long", buflen); |
663996b3 MS |
283 | return -1; |
284 | } | |
285 | ||
286 | resend: | |
287 | if (dev_scsi->use_sg == 4) { | |
60f067b4 | 288 | memzero(&io_v4, sizeof(struct sg_io_v4)); |
663996b3 MS |
289 | io_v4.guard = 'Q'; |
290 | io_v4.protocol = BSG_PROTOCOL_SCSI; | |
291 | io_v4.subprotocol = BSG_SUB_PROTOCOL_SCSI_CMD; | |
292 | io_v4.request_len = sizeof(inq_cmd); | |
293 | io_v4.request = (uintptr_t)inq_cmd; | |
294 | io_v4.max_response_len = sizeof(sense); | |
295 | io_v4.response = (uintptr_t)sense; | |
296 | io_v4.din_xfer_len = buflen; | |
297 | io_v4.din_xferp = (uintptr_t)buf; | |
298 | io_buf = (void *)&io_v4; | |
299 | } else { | |
60f067b4 | 300 | memzero(&io_hdr, sizeof(struct sg_io_hdr)); |
663996b3 MS |
301 | io_hdr.interface_id = 'S'; |
302 | io_hdr.cmd_len = sizeof(inq_cmd); | |
303 | io_hdr.mx_sb_len = sizeof(sense); | |
304 | io_hdr.dxfer_direction = SG_DXFER_FROM_DEV; | |
305 | io_hdr.dxfer_len = buflen; | |
306 | io_hdr.dxferp = buf; | |
307 | io_hdr.cmdp = inq_cmd; | |
308 | io_hdr.sbp = sense; | |
309 | io_hdr.timeout = DEF_TIMEOUT; | |
310 | io_buf = (void *)&io_hdr; | |
311 | } | |
312 | ||
313 | retval = ioctl(fd, SG_IO, io_buf); | |
314 | if (retval < 0) { | |
f5e65279 | 315 | if (IN_SET(errno, EINVAL, ENOSYS) && dev_scsi->use_sg == 4) { |
663996b3 MS |
316 | dev_scsi->use_sg = 3; |
317 | goto resend; | |
318 | } | |
f47781d8 | 319 | log_debug_errno(errno, "%s: ioctl failed: %m", dev_scsi->kernel); |
663996b3 MS |
320 | goto error; |
321 | } | |
322 | ||
323 | if (dev_scsi->use_sg == 4) | |
6e866b33 | 324 | retval = sg_err_category4(io_buf); |
663996b3 | 325 | else |
6e866b33 | 326 | retval = sg_err_category3(io_buf); |
663996b3 MS |
327 | |
328 | switch (retval) { | |
329 | case SG_ERR_CAT_NOTSUPPORTED: | |
330 | buf[1] = 0; | |
52ad194e | 331 | _fallthrough_; |
663996b3 MS |
332 | case SG_ERR_CAT_CLEAN: |
333 | case SG_ERR_CAT_RECOVERED: | |
334 | retval = 0; | |
335 | break; | |
336 | ||
337 | default: | |
338 | if (dev_scsi->use_sg == 4) | |
6e866b33 | 339 | retval = scsi_dump_v4(dev_scsi, io_buf); |
663996b3 | 340 | else |
6e866b33 | 341 | retval = scsi_dump(dev_scsi, io_buf); |
663996b3 MS |
342 | } |
343 | ||
344 | if (!retval) { | |
345 | retval = buflen; | |
346 | } else if (retval > 0) { | |
347 | if (--retry > 0) | |
348 | goto resend; | |
349 | retval = -1; | |
350 | } | |
351 | ||
352 | error: | |
353 | if (retval < 0) | |
60f067b4 | 354 | log_debug("%s: Unable to get INQUIRY vpd %d page 0x%x.", |
4c89c718 | 355 | dev_scsi->kernel, evpd, page); |
663996b3 MS |
356 | |
357 | return retval; | |
358 | } | |
359 | ||
360 | /* Get list of supported EVPD pages */ | |
6e866b33 MB |
361 | static int do_scsi_page0_inquiry(struct scsi_id_device *dev_scsi, int fd, |
362 | unsigned char *buffer, unsigned len) { | |
663996b3 MS |
363 | int retval; |
364 | ||
60f067b4 | 365 | memzero(buffer, len); |
6e866b33 | 366 | retval = scsi_inquiry(dev_scsi, fd, 1, 0x0, buffer, len); |
663996b3 MS |
367 | if (retval < 0) |
368 | return 1; | |
369 | ||
370 | if (buffer[1] != 0) { | |
60f067b4 | 371 | log_debug("%s: page 0 not available.", dev_scsi->kernel); |
663996b3 MS |
372 | return 1; |
373 | } | |
374 | if (buffer[3] > len) { | |
4c89c718 | 375 | log_debug("%s: page 0 buffer too long %d", dev_scsi->kernel, buffer[3]); |
663996b3 MS |
376 | return 1; |
377 | } | |
378 | ||
379 | /* | |
380 | * Following check is based on code once included in the 2.5.x | |
381 | * kernel. | |
382 | * | |
383 | * Some ill behaved devices return the standard inquiry here | |
384 | * rather than the evpd data, snoop the data to verify. | |
385 | */ | |
386 | if (buffer[3] > MODEL_LENGTH) { | |
387 | /* | |
388 | * If the vendor id appears in the page assume the page is | |
389 | * invalid. | |
390 | */ | |
391 | if (strneq((char *)&buffer[VENDOR_LENGTH], dev_scsi->vendor, VENDOR_LENGTH)) { | |
60f067b4 | 392 | log_debug("%s: invalid page0 data", dev_scsi->kernel); |
663996b3 MS |
393 | return 1; |
394 | } | |
395 | } | |
396 | return 0; | |
397 | } | |
398 | ||
399 | /* | |
400 | * The caller checks that serial is long enough to include the vendor + | |
401 | * model. | |
402 | */ | |
6e866b33 | 403 | static int prepend_vendor_model(struct scsi_id_device *dev_scsi, char *serial) { |
663996b3 MS |
404 | int ind; |
405 | ||
406 | strncpy(serial, dev_scsi->vendor, VENDOR_LENGTH); | |
407 | strncat(serial, dev_scsi->model, MODEL_LENGTH); | |
408 | ind = strlen(serial); | |
409 | ||
410 | /* | |
411 | * This is not a complete check, since we are using strncat/cpy | |
412 | * above, ind will never be too large. | |
413 | */ | |
414 | if (ind != (VENDOR_LENGTH + MODEL_LENGTH)) { | |
60f067b4 | 415 | log_debug("%s: expected length %d, got length %d", |
4c89c718 | 416 | dev_scsi->kernel, (VENDOR_LENGTH + MODEL_LENGTH), ind); |
663996b3 MS |
417 | return -1; |
418 | } | |
419 | return ind; | |
420 | } | |
421 | ||
422 | /* | |
423 | * check_fill_0x83_id - check the page 0x83 id, if OK allocate and fill | |
424 | * serial number. | |
425 | */ | |
6e866b33 | 426 | static int check_fill_0x83_id(struct scsi_id_device *dev_scsi, |
663996b3 MS |
427 | unsigned char *page_83, |
428 | const struct scsi_id_search_values | |
429 | *id_search, char *serial, char *serial_short, | |
430 | int max_len, char *wwn, | |
6e866b33 | 431 | char *wwn_vendor_extension, char *tgpt_group) { |
663996b3 MS |
432 | int i, j, s, len; |
433 | ||
434 | /* | |
435 | * ASSOCIATION must be with the device (value 0) | |
436 | * or with the target port for SCSI_ID_TGTPORT | |
437 | */ | |
438 | if ((page_83[1] & 0x30) == 0x10) { | |
439 | if (id_search->id_type != SCSI_ID_TGTGROUP) | |
440 | return 1; | |
6300502b | 441 | } else if ((page_83[1] & 0x30) != 0) |
663996b3 | 442 | return 1; |
663996b3 MS |
443 | |
444 | if ((page_83[1] & 0x0f) != id_search->id_type) | |
445 | return 1; | |
446 | ||
447 | /* | |
448 | * Possibly check NAA sub-type. | |
449 | */ | |
450 | if ((id_search->naa_type != SCSI_ID_NAA_DONT_CARE) && | |
451 | (id_search->naa_type != (page_83[4] & 0xf0) >> 4)) | |
452 | return 1; | |
453 | ||
454 | /* | |
455 | * Check for matching code set - ASCII or BINARY. | |
456 | */ | |
457 | if ((page_83[0] & 0x0f) != id_search->code_set) | |
458 | return 1; | |
459 | ||
460 | /* | |
461 | * page_83[3]: identifier length | |
462 | */ | |
463 | len = page_83[3]; | |
464 | if ((page_83[0] & 0x0f) != SCSI_ID_ASCII) | |
465 | /* | |
466 | * If not ASCII, use two bytes for each binary value. | |
467 | */ | |
468 | len *= 2; | |
469 | ||
470 | /* | |
471 | * Add one byte for the NUL termination, and one for the id_type. | |
472 | */ | |
473 | len += 2; | |
474 | if (id_search->id_type == SCSI_ID_VENDOR_SPECIFIC) | |
475 | len += VENDOR_LENGTH + MODEL_LENGTH; | |
476 | ||
477 | if (max_len < len) { | |
60f067b4 | 478 | log_debug("%s: length %d too short - need %d", |
4c89c718 | 479 | dev_scsi->kernel, max_len, len); |
663996b3 MS |
480 | return 1; |
481 | } | |
482 | ||
483 | if (id_search->id_type == SCSI_ID_TGTGROUP && tgpt_group != NULL) { | |
6e866b33 | 484 | unsigned group; |
663996b3 | 485 | |
6e866b33 | 486 | group = ((unsigned)page_83[6] << 8) | page_83[7]; |
663996b3 MS |
487 | sprintf(tgpt_group,"%x", group); |
488 | return 1; | |
489 | } | |
490 | ||
491 | serial[0] = hex_str[id_search->id_type]; | |
492 | ||
493 | /* | |
494 | * For SCSI_ID_VENDOR_SPECIFIC prepend the vendor and model before | |
495 | * the id since it is not unique across all vendors and models, | |
496 | * this differs from SCSI_ID_T10_VENDOR, where the vendor is | |
497 | * included in the identifier. | |
498 | */ | |
499 | if (id_search->id_type == SCSI_ID_VENDOR_SPECIFIC) | |
6e866b33 | 500 | if (prepend_vendor_model(dev_scsi, &serial[1]) < 0) |
663996b3 MS |
501 | return 1; |
502 | ||
503 | i = 4; /* offset to the start of the identifier */ | |
504 | s = j = strlen(serial); | |
505 | if ((page_83[0] & 0x0f) == SCSI_ID_ASCII) { | |
506 | /* | |
507 | * ASCII descriptor. | |
508 | */ | |
509 | while (i < (4 + page_83[3])) | |
510 | serial[j++] = page_83[i++]; | |
511 | } else { | |
512 | /* | |
513 | * Binary descriptor, convert to ASCII, using two bytes of | |
514 | * ASCII for each byte in the page_83. | |
515 | */ | |
516 | while (i < (4 + page_83[3])) { | |
517 | serial[j++] = hex_str[(page_83[i] & 0xf0) >> 4]; | |
518 | serial[j++] = hex_str[page_83[i] & 0x0f]; | |
519 | i++; | |
520 | } | |
521 | } | |
522 | ||
523 | strcpy(serial_short, &serial[s]); | |
524 | ||
525 | if (id_search->id_type == SCSI_ID_NAA && wwn != NULL) { | |
526 | strncpy(wwn, &serial[s], 16); | |
6300502b | 527 | if (wwn_vendor_extension != NULL) |
663996b3 | 528 | strncpy(wwn_vendor_extension, &serial[s + 16], 16); |
663996b3 MS |
529 | } |
530 | ||
531 | return 0; | |
532 | } | |
533 | ||
534 | /* Extract the raw binary from VPD 0x83 pre-SPC devices */ | |
6e866b33 | 535 | static int check_fill_0x83_prespc3(struct scsi_id_device *dev_scsi, |
663996b3 MS |
536 | unsigned char *page_83, |
537 | const struct scsi_id_search_values | |
6e866b33 | 538 | *id_search, char *serial, char *serial_short, int max_len) { |
663996b3 MS |
539 | int i, j; |
540 | ||
b012e921 | 541 | serial[0] = hex_str[SCSI_ID_NAA]; |
663996b3 MS |
542 | /* serial has been memset to zero before */ |
543 | j = strlen(serial); /* j = 1; */ | |
544 | ||
545 | for (i = 0; (i < page_83[3]) && (j < max_len-3); ++i) { | |
546 | serial[j++] = hex_str[(page_83[4+i] & 0xf0) >> 4]; | |
547 | serial[j++] = hex_str[ page_83[4+i] & 0x0f]; | |
548 | } | |
549 | serial[max_len-1] = 0; | |
550 | strncpy(serial_short, serial, max_len-1); | |
551 | return 0; | |
552 | } | |
553 | ||
663996b3 | 554 | /* Get device identification VPD page */ |
6e866b33 | 555 | static int do_scsi_page83_inquiry(struct scsi_id_device *dev_scsi, int fd, |
663996b3 MS |
556 | char *serial, char *serial_short, int len, |
557 | char *unit_serial_number, char *wwn, | |
6e866b33 | 558 | char *wwn_vendor_extension, char *tgpt_group) { |
663996b3 | 559 | int retval; |
6e866b33 | 560 | unsigned id_ind, j; |
663996b3 MS |
561 | unsigned char page_83[SCSI_INQ_BUFF_LEN]; |
562 | ||
563 | /* also pick up the page 80 serial number */ | |
6e866b33 | 564 | do_scsi_page80_inquiry(dev_scsi, fd, NULL, unit_serial_number, MAX_SERIAL_LEN); |
663996b3 | 565 | |
60f067b4 | 566 | memzero(page_83, SCSI_INQ_BUFF_LEN); |
6e866b33 | 567 | retval = scsi_inquiry(dev_scsi, fd, 1, PAGE_83, page_83, |
663996b3 MS |
568 | SCSI_INQ_BUFF_LEN); |
569 | if (retval < 0) | |
570 | return 1; | |
571 | ||
572 | if (page_83[1] != PAGE_83) { | |
60f067b4 | 573 | log_debug("%s: Invalid page 0x83", dev_scsi->kernel); |
663996b3 MS |
574 | return 1; |
575 | } | |
576 | ||
577 | /* | |
578 | * XXX Some devices (IBM 3542) return all spaces for an identifier if | |
579 | * the LUN is not actually configured. This leads to identifiers of | |
580 | * the form: "1 ". | |
581 | */ | |
582 | ||
583 | /* | |
584 | * Model 4, 5, and (some) model 6 EMC Symmetrix devices return | |
585 | * a page 83 reply according to SCSI-2 format instead of SPC-2/3. | |
586 | * | |
587 | * The SCSI-2 page 83 format returns an IEEE WWN in binary | |
588 | * encoded hexi-decimal in the 16 bytes following the initial | |
589 | * 4-byte page 83 reply header. | |
590 | * | |
591 | * Both the SPC-2 and SPC-3 formats return an IEEE WWN as part | |
592 | * of an Identification descriptor. The 3rd byte of the first | |
593 | * Identification descriptor is a reserved (BSZ) byte field. | |
594 | * | |
595 | * Reference the 7th byte of the page 83 reply to determine | |
596 | * whether the reply is compliant with SCSI-2 or SPC-2/3 | |
597 | * specifications. A zero value in the 7th byte indicates | |
598 | * an SPC-2/3 conformant reply, (i.e., the reserved field of the | |
599 | * first Identification descriptor). This byte will be non-zero | |
600 | * for a SCSI-2 conformant page 83 reply from these EMC | |
601 | * Symmetrix models since the 7th byte of the reply corresponds | |
602 | * to the 4th and 5th nibbles of the 6-byte OUI for EMC, that is, | |
603 | * 0x006048. | |
604 | */ | |
605 | ||
606 | if (page_83[6] != 0) | |
6e866b33 | 607 | return check_fill_0x83_prespc3(dev_scsi, page_83, id_search_list, |
663996b3 MS |
608 | serial, serial_short, len); |
609 | ||
610 | /* | |
611 | * Search for a match in the prioritized id_search_list - since WWN ids | |
612 | * come first we can pick up the WWN in check_fill_0x83_id(). | |
613 | */ | |
614 | for (id_ind = 0; | |
615 | id_ind < sizeof(id_search_list)/sizeof(id_search_list[0]); | |
616 | id_ind++) { | |
617 | /* | |
618 | * Examine each descriptor returned. There is normally only | |
619 | * one or a small number of descriptors. | |
620 | */ | |
6e866b33 MB |
621 | for (j = 4; j <= (unsigned)page_83[3] + 3; j += page_83[j + 3] + 4) { |
622 | retval = check_fill_0x83_id(dev_scsi, &page_83[j], | |
663996b3 MS |
623 | &id_search_list[id_ind], |
624 | serial, serial_short, len, | |
625 | wwn, wwn_vendor_extension, | |
626 | tgpt_group); | |
627 | if (!retval) | |
628 | return retval; | |
629 | else if (retval < 0) | |
630 | return retval; | |
631 | } | |
632 | } | |
633 | return 1; | |
634 | } | |
635 | ||
636 | /* | |
637 | * Get device identification VPD page for older SCSI-2 device which is not | |
638 | * compliant with either SPC-2 or SPC-3 format. | |
639 | * | |
640 | * Return the hard coded error code value 2 if the page 83 reply is not | |
641 | * conformant to the SCSI-2 format. | |
642 | */ | |
6e866b33 MB |
643 | static int do_scsi_page83_prespc3_inquiry(struct scsi_id_device *dev_scsi, int fd, |
644 | char *serial, char *serial_short, int len) { | |
663996b3 MS |
645 | int retval; |
646 | int i, j; | |
647 | unsigned char page_83[SCSI_INQ_BUFF_LEN]; | |
648 | ||
60f067b4 | 649 | memzero(page_83, SCSI_INQ_BUFF_LEN); |
6e866b33 | 650 | retval = scsi_inquiry(dev_scsi, fd, 1, PAGE_83, page_83, SCSI_INQ_BUFF_LEN); |
663996b3 MS |
651 | if (retval < 0) |
652 | return 1; | |
653 | ||
654 | if (page_83[1] != PAGE_83) { | |
60f067b4 | 655 | log_debug("%s: Invalid page 0x83", dev_scsi->kernel); |
663996b3 MS |
656 | return 1; |
657 | } | |
658 | /* | |
659 | * Model 4, 5, and (some) model 6 EMC Symmetrix devices return | |
660 | * a page 83 reply according to SCSI-2 format instead of SPC-2/3. | |
661 | * | |
662 | * The SCSI-2 page 83 format returns an IEEE WWN in binary | |
663 | * encoded hexi-decimal in the 16 bytes following the initial | |
664 | * 4-byte page 83 reply header. | |
665 | * | |
666 | * Both the SPC-2 and SPC-3 formats return an IEEE WWN as part | |
667 | * of an Identification descriptor. The 3rd byte of the first | |
668 | * Identification descriptor is a reserved (BSZ) byte field. | |
669 | * | |
670 | * Reference the 7th byte of the page 83 reply to determine | |
671 | * whether the reply is compliant with SCSI-2 or SPC-2/3 | |
672 | * specifications. A zero value in the 7th byte indicates | |
673 | * an SPC-2/3 conformant reply, (i.e., the reserved field of the | |
674 | * first Identification descriptor). This byte will be non-zero | |
675 | * for a SCSI-2 conformant page 83 reply from these EMC | |
676 | * Symmetrix models since the 7th byte of the reply corresponds | |
677 | * to the 4th and 5th nibbles of the 6-byte OUI for EMC, that is, | |
678 | * 0x006048. | |
679 | */ | |
680 | if (page_83[6] == 0) | |
681 | return 2; | |
682 | ||
b012e921 | 683 | serial[0] = hex_str[SCSI_ID_NAA]; |
663996b3 MS |
684 | /* |
685 | * The first four bytes contain data, not a descriptor. | |
686 | */ | |
687 | i = 4; | |
688 | j = strlen(serial); | |
689 | /* | |
690 | * Binary descriptor, convert to ASCII, | |
691 | * using two bytes of ASCII for each byte | |
692 | * in the page_83. | |
693 | */ | |
694 | while (i < (page_83[3]+4)) { | |
695 | serial[j++] = hex_str[(page_83[i] & 0xf0) >> 4]; | |
696 | serial[j++] = hex_str[page_83[i] & 0x0f]; | |
697 | i++; | |
698 | } | |
699 | return 0; | |
700 | } | |
701 | ||
702 | /* Get unit serial number VPD page */ | |
6e866b33 MB |
703 | static int do_scsi_page80_inquiry(struct scsi_id_device *dev_scsi, int fd, |
704 | char *serial, char *serial_short, int max_len) { | |
663996b3 MS |
705 | int retval; |
706 | int ser_ind; | |
707 | int i; | |
708 | int len; | |
709 | unsigned char buf[SCSI_INQ_BUFF_LEN]; | |
710 | ||
60f067b4 | 711 | memzero(buf, SCSI_INQ_BUFF_LEN); |
6e866b33 | 712 | retval = scsi_inquiry(dev_scsi, fd, 1, PAGE_80, buf, SCSI_INQ_BUFF_LEN); |
663996b3 MS |
713 | if (retval < 0) |
714 | return retval; | |
715 | ||
716 | if (buf[1] != PAGE_80) { | |
60f067b4 | 717 | log_debug("%s: Invalid page 0x80", dev_scsi->kernel); |
663996b3 MS |
718 | return 1; |
719 | } | |
720 | ||
721 | len = 1 + VENDOR_LENGTH + MODEL_LENGTH + buf[3]; | |
722 | if (max_len < len) { | |
60f067b4 | 723 | log_debug("%s: length %d too short - need %d", |
4c89c718 | 724 | dev_scsi->kernel, max_len, len); |
663996b3 MS |
725 | return 1; |
726 | } | |
727 | /* | |
728 | * Prepend 'S' to avoid unlikely collision with page 0x83 vendor | |
729 | * specific type where we prepend '0' + vendor + model. | |
730 | */ | |
731 | len = buf[3]; | |
732 | if (serial != NULL) { | |
733 | serial[0] = 'S'; | |
6e866b33 | 734 | ser_ind = prepend_vendor_model(dev_scsi, &serial[1]); |
663996b3 MS |
735 | if (ser_ind < 0) |
736 | return 1; | |
737 | ser_ind++; /* for the leading 'S' */ | |
738 | for (i = 4; i < len + 4; i++, ser_ind++) | |
739 | serial[ser_ind] = buf[i]; | |
740 | } | |
741 | if (serial_short != NULL) { | |
742 | memcpy(serial_short, &buf[4], len); | |
743 | serial_short[len] = '\0'; | |
744 | } | |
745 | return 0; | |
746 | } | |
747 | ||
6e866b33 | 748 | int scsi_std_inquiry(struct scsi_id_device *dev_scsi, const char *devname) { |
663996b3 MS |
749 | int fd; |
750 | unsigned char buf[SCSI_INQ_BUFF_LEN]; | |
751 | struct stat statbuf; | |
752 | int err = 0; | |
753 | ||
60f067b4 | 754 | fd = open(devname, O_RDONLY | O_NONBLOCK | O_CLOEXEC); |
663996b3 | 755 | if (fd < 0) { |
f47781d8 | 756 | log_debug_errno(errno, "scsi_id: cannot open %s: %m", devname); |
663996b3 MS |
757 | return 1; |
758 | } | |
759 | ||
760 | if (fstat(fd, &statbuf) < 0) { | |
f47781d8 | 761 | log_debug_errno(errno, "scsi_id: cannot stat %s: %m", devname); |
663996b3 MS |
762 | err = 2; |
763 | goto out; | |
764 | } | |
765 | sprintf(dev_scsi->kernel,"%d:%d", major(statbuf.st_rdev), | |
766 | minor(statbuf.st_rdev)); | |
767 | ||
60f067b4 | 768 | memzero(buf, SCSI_INQ_BUFF_LEN); |
6e866b33 | 769 | err = scsi_inquiry(dev_scsi, fd, 0, 0, buf, SCSI_INQ_BUFF_LEN); |
663996b3 MS |
770 | if (err < 0) |
771 | goto out; | |
772 | ||
773 | err = 0; | |
774 | memcpy(dev_scsi->vendor, buf + 8, 8); | |
775 | dev_scsi->vendor[8] = '\0'; | |
776 | memcpy(dev_scsi->model, buf + 16, 16); | |
777 | dev_scsi->model[16] = '\0'; | |
778 | memcpy(dev_scsi->revision, buf + 32, 4); | |
779 | dev_scsi->revision[4] = '\0'; | |
780 | sprintf(dev_scsi->type,"%x", buf[0] & 0x1f); | |
781 | ||
782 | out: | |
783 | close(fd); | |
784 | return err; | |
785 | } | |
786 | ||
6e866b33 MB |
787 | int scsi_get_serial(struct scsi_id_device *dev_scsi, const char *devname, |
788 | int page_code, int len) { | |
663996b3 MS |
789 | unsigned char page0[SCSI_INQ_BUFF_LEN]; |
790 | int fd = -1; | |
791 | int cnt; | |
792 | int ind; | |
793 | int retval; | |
794 | ||
60f067b4 | 795 | memzero(dev_scsi->serial, len); |
f47781d8 | 796 | initialize_srand(); |
663996b3 MS |
797 | for (cnt = 20; cnt > 0; cnt--) { |
798 | struct timespec duration; | |
799 | ||
60f067b4 | 800 | fd = open(devname, O_RDONLY | O_NONBLOCK | O_CLOEXEC); |
663996b3 MS |
801 | if (fd >= 0 || errno != EBUSY) |
802 | break; | |
803 | duration.tv_sec = 0; | |
804 | duration.tv_nsec = (200 * 1000 * 1000) + (rand() % 100 * 1000 * 1000); | |
805 | nanosleep(&duration, NULL); | |
806 | } | |
807 | if (fd < 0) | |
808 | return 1; | |
809 | ||
810 | if (page_code == PAGE_80) { | |
6e866b33 | 811 | if (do_scsi_page80_inquiry(dev_scsi, fd, dev_scsi->serial, dev_scsi->serial_short, len)) { |
663996b3 MS |
812 | retval = 1; |
813 | goto completed; | |
814 | } else { | |
815 | retval = 0; | |
816 | goto completed; | |
817 | } | |
818 | } else if (page_code == PAGE_83) { | |
6e866b33 | 819 | if (do_scsi_page83_inquiry(dev_scsi, fd, dev_scsi->serial, dev_scsi->serial_short, len, dev_scsi->unit_serial_number, dev_scsi->wwn, dev_scsi->wwn_vendor_extension, dev_scsi->tgpt_group)) { |
663996b3 MS |
820 | retval = 1; |
821 | goto completed; | |
822 | } else { | |
823 | retval = 0; | |
824 | goto completed; | |
825 | } | |
826 | } else if (page_code == PAGE_83_PRE_SPC3) { | |
6e866b33 | 827 | retval = do_scsi_page83_prespc3_inquiry(dev_scsi, fd, dev_scsi->serial, dev_scsi->serial_short, len); |
663996b3 MS |
828 | if (retval) { |
829 | /* | |
830 | * Fallback to servicing a SPC-2/3 compliant page 83 | |
831 | * inquiry if the page 83 reply format does not | |
832 | * conform to pre-SPC3 expectations. | |
833 | */ | |
834 | if (retval == 2) { | |
6e866b33 | 835 | if (do_scsi_page83_inquiry(dev_scsi, fd, dev_scsi->serial, dev_scsi->serial_short, len, dev_scsi->unit_serial_number, dev_scsi->wwn, dev_scsi->wwn_vendor_extension, dev_scsi->tgpt_group)) { |
663996b3 MS |
836 | retval = 1; |
837 | goto completed; | |
838 | } else { | |
839 | retval = 0; | |
840 | goto completed; | |
841 | } | |
842 | } | |
843 | else { | |
844 | retval = 1; | |
845 | goto completed; | |
846 | } | |
847 | } else { | |
848 | retval = 0; | |
849 | goto completed; | |
850 | } | |
851 | } else if (page_code != 0x00) { | |
60f067b4 | 852 | log_debug("%s: unsupported page code 0x%d", dev_scsi->kernel, page_code); |
663996b3 MS |
853 | retval = 1; |
854 | goto completed; | |
855 | } | |
856 | ||
857 | /* | |
858 | * Get page 0, the page of the pages. By default, try from best to | |
859 | * worst of supported pages: 0x83 then 0x80. | |
860 | */ | |
6e866b33 | 861 | if (do_scsi_page0_inquiry(dev_scsi, fd, page0, SCSI_INQ_BUFF_LEN)) { |
663996b3 MS |
862 | /* |
863 | * Don't try anything else. Black list if a specific page | |
864 | * should be used for this vendor+model, or maybe have an | |
865 | * optional fall-back to page 0x80 or page 0x83. | |
866 | */ | |
867 | retval = 1; | |
868 | goto completed; | |
869 | } | |
870 | ||
871 | for (ind = 4; ind <= page0[3] + 3; ind++) | |
872 | if (page0[ind] == PAGE_83) | |
6e866b33 | 873 | if (!do_scsi_page83_inquiry(dev_scsi, fd, |
663996b3 MS |
874 | dev_scsi->serial, dev_scsi->serial_short, len, dev_scsi->unit_serial_number, dev_scsi->wwn, dev_scsi->wwn_vendor_extension, dev_scsi->tgpt_group)) { |
875 | /* | |
876 | * Success | |
877 | */ | |
878 | retval = 0; | |
879 | goto completed; | |
880 | } | |
881 | ||
882 | for (ind = 4; ind <= page0[3] + 3; ind++) | |
883 | if (page0[ind] == PAGE_80) | |
6e866b33 | 884 | if (!do_scsi_page80_inquiry(dev_scsi, fd, |
663996b3 MS |
885 | dev_scsi->serial, dev_scsi->serial_short, len)) { |
886 | /* | |
887 | * Success | |
888 | */ | |
889 | retval = 0; | |
890 | goto completed; | |
891 | } | |
892 | retval = 1; | |
893 | ||
894 | completed: | |
895 | close(fd); | |
896 | return retval; | |
897 | } |