]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/udev/scsi_id/scsi_serial.c
Merge pull request #12392 from poettering/firstboot-salt
[thirdparty/systemd.git] / src / udev / scsi_id / scsi_serial.c
CommitLineData
e7145211 1/* SPDX-License-Identifier: GPL-2.0+ */
e0f83fbe 2/*
810adae9 3 * Copyright © IBM Corp. 2003
c521693b 4 *
8b5651bb 5 * Author: Patrick Mansfield<patmans@us.ibm.com>
c521693b
GKH
6 */
7
c521693b 8#include <errno.h>
c521693b 9#include <fcntl.h>
78d9ecfd 10#include <inttypes.h>
07630cea
LP
11#include <linux/bsg.h>
12#include <linux/types.h>
78d9ecfd 13#include <scsi/scsi.h>
c521693b 14#include <scsi/sg.h>
07630cea
LP
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>
1aa1e248 23
0a970718 24#include "memory-util.h"
07630cea 25#include "random-util.h"
c521693b 26#include "scsi.h"
1aa1e248 27#include "scsi_id.h"
07630cea 28#include "string-util.h"
c521693b
GKH
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 */
38static const struct scsi_id_search_values id_search_list[] = {
461577ee
ZJS
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 },
912541b0
KS
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 */
461577ee
ZJS
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 },
c521693b
GKH
61};
62
63static const char hex_str[]="0123456789abcdef";
64
c521693b
GKH
65/*
66 * Values returned in the result/status, only the ones used by the code
67 * are used here.
68 */
69
461577ee
ZJS
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 */
c521693b
GKH
75
76/* The following "category" function returns one of the following */
912541b0
KS
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 */
461577ee
ZJS
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 */
c521693b 85
efc2774c 86static int do_scsi_page80_inquiry(struct scsi_id_device *dev_scsi, int fd,
912541b0 87 char *serial, char *serial_short, int max_len);
4e9fdfcc 88
efc2774c 89static int sg_err_category_new(int scsi_status, int msg_status, int
912541b0 90 host_status, int driver_status, const
efc2774c 91 unsigned char *sense_buffer, int sb_len) {
912541b0
KS
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
4c701096
YW
101 if (IN_SET(scsi_status, SCSI_CHECK_CONDITION, SCSI_COMMAND_TERMINATED) ||
102 (driver_status & 0xf) == DRIVER_SENSE) {
912541b0
KS
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;
1f6b4113 122 } else if (sense_key == ILLEGAL_REQUEST)
912541b0 123 return SG_ERR_CAT_NOTSUPPORTED;
912541b0
KS
124 }
125 return SG_ERR_CAT_SENSE;
126 }
127 if (host_status) {
4c701096 128 if (IN_SET(host_status, DID_NO_CONNECT, DID_BUS_BUSY, DID_TIME_OUT))
912541b0
KS
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;
c521693b
GKH
136}
137
efc2774c
YW
138static int sg_err_category3(struct sg_io_hdr *hp) {
139 return sg_err_category_new(hp->status, hp->msg_status,
912541b0
KS
140 hp->host_status, hp->driver_status,
141 hp->sbp, hp->sb_len_wr);
c521693b
GKH
142}
143
efc2774c
YW
144static int sg_err_category4(struct sg_io_v4 *hp) {
145 return sg_err_category_new(hp->device_status, 0,
912541b0
KS
146 hp->transport_status, hp->driver_status,
147 (unsigned char *)(uintptr_t)hp->response,
148 hp->response_len);
78d9ecfd
HR
149}
150
efc2774c
YW
151static int scsi_dump_sense(struct scsi_id_device *dev_scsi,
152 unsigned char *sense_buffer, int sb_len) {
912541b0
KS
153 int s;
154 int code;
155 int sense_class;
156 int sense_key;
157 int asc, ascq;
c521693b 158
912541b0
KS
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
8b803ad6 171 if (sb_len < 1)
ed0cb346
FS
172 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
173 "%s: sense buffer empty",
174 dev_scsi->kernel);
912541b0
KS
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;
8b803ad6 184 if (sb_len < s)
ed0cb346
FS
185 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
186 "%s: sense buffer too small %d bytes, %d bytes too short",
187 dev_scsi->kernel, sb_len,
188 s - sb_len);
8b803ad6 189
4c701096 190 if (IN_SET(code, 0x0, 0x1)) {
912541b0 191 sense_key = sense_buffer[2] & 0xf;
8b803ad6 192 if (s < 14)
912541b0
KS
193 /*
194 * Possible?
195 */
ed0cb346
FS
196 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
197 "%s: sense result too small %d bytes",
198 dev_scsi->kernel, s);
8b803ad6 199
912541b0
KS
200 asc = sense_buffer[12];
201 ascq = sense_buffer[13];
4c701096 202 } else if (IN_SET(code, 0x2, 0x3)) {
912541b0
KS
203 sense_key = sense_buffer[1] & 0xf;
204 asc = sense_buffer[2];
205 ascq = sense_buffer[3];
8b803ad6 206 } else
ed0cb346
FS
207 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
208 "%s: invalid sense code 0x%x",
209 dev_scsi->kernel, code);
8b803ad6 210
9f6445e3 211 log_debug("%s: sense key 0x%x ASC 0x%x ASCQ 0x%x",
461577ee 212 dev_scsi->kernel, sense_key, asc, ascq);
912541b0 213 } else {
8b803ad6 214 if (sb_len < 4)
ed0cb346
FS
215 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
216 "%s: sense buffer too small %d bytes, %d bytes too short",
217 dev_scsi->kernel, sb_len,
218 4 - sb_len);
912541b0
KS
219
220 if (sense_buffer[0] < 15)
9f6445e3 221 log_debug("%s: old sense key: 0x%x", dev_scsi->kernel, sense_buffer[0] & 0x0f);
912541b0 222 else
9f6445e3 223 log_debug("%s: sense = %2x %2x",
461577ee 224 dev_scsi->kernel, sense_buffer[0], sense_buffer[2]);
9f6445e3 225 log_debug("%s: non-extended sense class %d code 0x%0x",
461577ee 226 dev_scsi->kernel, sense_class, code);
912541b0
KS
227
228 }
c521693b 229
912541b0 230 return -1;
c521693b
GKH
231}
232
efc2774c 233static int scsi_dump(struct scsi_id_device *dev_scsi, struct sg_io_hdr *io) {
912541b0 234 if (!io->status && !io->host_status && !io->msg_status &&
8b803ad6 235 !io->driver_status)
912541b0
KS
236 /*
237 * Impossible, should not be called.
238 */
ed0cb346
FS
239 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
240 "%s: called with no error",
241 __FUNCTION__);
912541b0 242
9f6445e3 243 log_debug("%s: sg_io failed status 0x%x 0x%x 0x%x 0x%x",
461577ee 244 dev_scsi->kernel, io->driver_status, io->host_status, io->msg_status, io->status);
912541b0 245 if (io->status == SCSI_CHECK_CONDITION)
efc2774c 246 return scsi_dump_sense(dev_scsi, io->sbp, io->sb_len_wr);
912541b0
KS
247 else
248 return -1;
78d9ecfd
HR
249}
250
efc2774c 251static int scsi_dump_v4(struct scsi_id_device *dev_scsi, struct sg_io_v4 *io) {
912541b0 252 if (!io->device_status && !io->transport_status &&
8b803ad6 253 !io->driver_status)
912541b0
KS
254 /*
255 * Impossible, should not be called.
256 */
ed0cb346
FS
257 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
258 "%s: called with no error",
259 __FUNCTION__);
912541b0 260
9f6445e3 261 log_debug("%s: sg_io failed status 0x%x 0x%x 0x%x",
461577ee 262 dev_scsi->kernel, io->driver_status, io->transport_status, io->device_status);
912541b0 263 if (io->device_status == SCSI_CHECK_CONDITION)
efc2774c 264 return scsi_dump_sense(dev_scsi, (unsigned char *)(uintptr_t)io->response,
912541b0
KS
265 io->response_len);
266 else
267 return -1;
c521693b
GKH
268}
269
efc2774c 270static int scsi_inquiry(struct scsi_id_device *dev_scsi, int fd,
912541b0 271 unsigned char evpd, unsigned char page,
14cb109d 272 unsigned char *buf, unsigned buflen) {
912541b0
KS
273 unsigned char inq_cmd[INQUIRY_CMDLEN] =
274 { INQUIRY_CMD, evpd, page, 0, buflen, 0 };
275 unsigned char sense[SENSE_BUFF_LEN];
276 void *io_buf;
277 struct sg_io_v4 io_v4;
278 struct sg_io_hdr io_hdr;
279 int retry = 3; /* rather random */
280 int retval;
281
8b803ad6 282 if (buflen > SCSI_INQ_BUFF_LEN)
ed0cb346
FS
283 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
284 "buflen %d too long", buflen);
c521693b
GKH
285
286resend:
912541b0 287 if (dev_scsi->use_sg == 4) {
29804cc1 288 memzero(&io_v4, sizeof(struct sg_io_v4));
912541b0
KS
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 {
29804cc1 300 memzero(&io_hdr, sizeof(struct sg_io_hdr));
912541b0
KS
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) {
3742095b 315 if (IN_SET(errno, EINVAL, ENOSYS) && dev_scsi->use_sg == 4) {
912541b0
KS
316 dev_scsi->use_sg = 3;
317 goto resend;
318 }
56f64d95 319 log_debug_errno(errno, "%s: ioctl failed: %m", dev_scsi->kernel);
912541b0
KS
320 goto error;
321 }
322
323 if (dev_scsi->use_sg == 4)
efc2774c 324 retval = sg_err_category4(io_buf);
912541b0 325 else
efc2774c 326 retval = sg_err_category3(io_buf);
912541b0
KS
327
328 switch (retval) {
329 case SG_ERR_CAT_NOTSUPPORTED:
330 buf[1] = 0;
4831981d 331 _fallthrough_;
912541b0
KS
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)
efc2774c 339 retval = scsi_dump_v4(dev_scsi, io_buf);
912541b0 340 else
efc2774c 341 retval = scsi_dump(dev_scsi, io_buf);
912541b0
KS
342 }
343
344 if (!retval) {
345 retval = buflen;
346 } else if (retval > 0) {
baa30fbc 347 if (--retry > 0)
912541b0 348 goto resend;
912541b0
KS
349 retval = -1;
350 }
c521693b 351
01f950e2 352error:
912541b0 353 if (retval < 0)
9f6445e3 354 log_debug("%s: Unable to get INQUIRY vpd %d page 0x%x.",
461577ee 355 dev_scsi->kernel, evpd, page);
062db23d 356
912541b0 357 return retval;
c521693b
GKH
358}
359
01f950e2 360/* Get list of supported EVPD pages */
efc2774c 361static int do_scsi_page0_inquiry(struct scsi_id_device *dev_scsi, int fd,
14cb109d 362 unsigned char *buffer, unsigned len) {
912541b0
KS
363 int retval;
364
29804cc1 365 memzero(buffer, len);
efc2774c 366 retval = scsi_inquiry(dev_scsi, fd, 1, 0x0, buffer, len);
912541b0
KS
367 if (retval < 0)
368 return 1;
369
370 if (buffer[1] != 0) {
9f6445e3 371 log_debug("%s: page 0 not available.", dev_scsi->kernel);
912541b0
KS
372 return 1;
373 }
374 if (buffer[3] > len) {
461577ee 375 log_debug("%s: page 0 buffer too long %d", dev_scsi->kernel, buffer[3]);
912541b0
KS
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 */
881a62de 391 if (strneq((char*) buffer + VENDOR_LENGTH, dev_scsi->vendor, VENDOR_LENGTH)) {
9f6445e3 392 log_debug("%s: invalid page0 data", dev_scsi->kernel);
912541b0
KS
393 return 1;
394 }
395 }
396 return 0;
c521693b
GKH
397}
398
099c77fd
ZJS
399static int append_vendor_model(
400 const struct scsi_id_device *dev_scsi,
401 char buf[static VENDOR_LENGTH + MODEL_LENGTH]) {
912541b0 402
099c77fd
ZJS
403 if (strnlen(dev_scsi->vendor, VENDOR_LENGTH) != VENDOR_LENGTH)
404 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
405 "%s: bad vendor string \"%s\"",
406 dev_scsi->kernel, dev_scsi->vendor);
407 if (strnlen(dev_scsi->model, MODEL_LENGTH) != MODEL_LENGTH)
ed0cb346 408 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
099c77fd
ZJS
409 "%s: bad model string \"%s\"",
410 dev_scsi->kernel, dev_scsi->model);
411 memcpy(buf, dev_scsi->vendor, VENDOR_LENGTH);
412 memcpy(buf + VENDOR_LENGTH, dev_scsi->model, MODEL_LENGTH);
413 return VENDOR_LENGTH + MODEL_LENGTH;
c521693b
GKH
414}
415
bb061708 416/*
c521693b
GKH
417 * check_fill_0x83_id - check the page 0x83 id, if OK allocate and fill
418 * serial number.
bb061708 419 */
efc2774c 420static int check_fill_0x83_id(struct scsi_id_device *dev_scsi,
912541b0
KS
421 unsigned char *page_83,
422 const struct scsi_id_search_values
423 *id_search, char *serial, char *serial_short,
424 int max_len, char *wwn,
efc2774c 425 char *wwn_vendor_extension, char *tgpt_group) {
912541b0
KS
426 int i, j, s, len;
427
428 /*
429 * ASSOCIATION must be with the device (value 0)
430 * or with the target port for SCSI_ID_TGTPORT
431 */
432 if ((page_83[1] & 0x30) == 0x10) {
433 if (id_search->id_type != SCSI_ID_TGTGROUP)
434 return 1;
1f6b4113 435 } else if ((page_83[1] & 0x30) != 0)
912541b0 436 return 1;
912541b0
KS
437
438 if ((page_83[1] & 0x0f) != id_search->id_type)
439 return 1;
440
441 /*
442 * Possibly check NAA sub-type.
443 */
444 if ((id_search->naa_type != SCSI_ID_NAA_DONT_CARE) &&
445 (id_search->naa_type != (page_83[4] & 0xf0) >> 4))
446 return 1;
447
448 /*
449 * Check for matching code set - ASCII or BINARY.
450 */
451 if ((page_83[0] & 0x0f) != id_search->code_set)
452 return 1;
453
454 /*
455 * page_83[3]: identifier length
456 */
457 len = page_83[3];
458 if ((page_83[0] & 0x0f) != SCSI_ID_ASCII)
459 /*
460 * If not ASCII, use two bytes for each binary value.
461 */
462 len *= 2;
463
464 /*
465 * Add one byte for the NUL termination, and one for the id_type.
466 */
467 len += 2;
468 if (id_search->id_type == SCSI_ID_VENDOR_SPECIFIC)
469 len += VENDOR_LENGTH + MODEL_LENGTH;
470
471 if (max_len < len) {
9f6445e3 472 log_debug("%s: length %d too short - need %d",
461577ee 473 dev_scsi->kernel, max_len, len);
912541b0
KS
474 return 1;
475 }
476
477 if (id_search->id_type == SCSI_ID_TGTGROUP && tgpt_group != NULL) {
14cb109d 478 unsigned group;
912541b0 479
14cb109d 480 group = ((unsigned)page_83[6] << 8) | page_83[7];
912541b0
KS
481 sprintf(tgpt_group,"%x", group);
482 return 1;
483 }
484
485 serial[0] = hex_str[id_search->id_type];
486
487 /*
488 * For SCSI_ID_VENDOR_SPECIFIC prepend the vendor and model before
489 * the id since it is not unique across all vendors and models,
490 * this differs from SCSI_ID_T10_VENDOR, where the vendor is
491 * included in the identifier.
492 */
493 if (id_search->id_type == SCSI_ID_VENDOR_SPECIFIC)
099c77fd 494 if (append_vendor_model(dev_scsi, serial + 1) < 0)
912541b0 495 return 1;
912541b0
KS
496
497 i = 4; /* offset to the start of the identifier */
498 s = j = strlen(serial);
499 if ((page_83[0] & 0x0f) == SCSI_ID_ASCII) {
500 /*
501 * ASCII descriptor.
502 */
503 while (i < (4 + page_83[3]))
504 serial[j++] = page_83[i++];
505 } else {
506 /*
507 * Binary descriptor, convert to ASCII, using two bytes of
508 * ASCII for each byte in the page_83.
509 */
510 while (i < (4 + page_83[3])) {
511 serial[j++] = hex_str[(page_83[i] & 0xf0) >> 4];
512 serial[j++] = hex_str[page_83[i] & 0x0f];
513 i++;
514 }
515 }
516
881a62de 517 strcpy(serial_short, serial + s);
912541b0
KS
518
519 if (id_search->id_type == SCSI_ID_NAA && wwn != NULL) {
881a62de 520 strncpy(wwn, serial + s, 16);
4e361acc 521 if (wwn_vendor_extension)
881a62de 522 strncpy(wwn_vendor_extension, serial + s + 16, 16);
912541b0
KS
523 }
524
525 return 0;
c521693b
GKH
526}
527
cbba4a54 528/* Extract the raw binary from VPD 0x83 pre-SPC devices */
efc2774c 529static int check_fill_0x83_prespc3(struct scsi_id_device *dev_scsi,
912541b0
KS
530 unsigned char *page_83,
531 const struct scsi_id_search_values
efc2774c 532 *id_search, char *serial, char *serial_short, int max_len) {
912541b0
KS
533 int i, j;
534
c0373eb0 535 serial[0] = hex_str[SCSI_ID_NAA];
912541b0
KS
536 /* serial has been memset to zero before */
537 j = strlen(serial); /* j = 1; */
538
539 for (i = 0; (i < page_83[3]) && (j < max_len-3); ++i) {
540 serial[j++] = hex_str[(page_83[4+i] & 0xf0) >> 4];
541 serial[j++] = hex_str[ page_83[4+i] & 0x0f];
542 }
543 serial[max_len-1] = 0;
544 strncpy(serial_short, serial, max_len-1);
545 return 0;
cbba4a54
KG
546}
547
01f950e2 548/* Get device identification VPD page */
efc2774c 549static int do_scsi_page83_inquiry(struct scsi_id_device *dev_scsi, int fd,
912541b0
KS
550 char *serial, char *serial_short, int len,
551 char *unit_serial_number, char *wwn,
efc2774c 552 char *wwn_vendor_extension, char *tgpt_group) {
912541b0 553 int retval;
14cb109d 554 unsigned id_ind, j;
912541b0 555 unsigned char page_83[SCSI_INQ_BUFF_LEN];
c521693b 556
912541b0 557 /* also pick up the page 80 serial number */
efc2774c 558 do_scsi_page80_inquiry(dev_scsi, fd, NULL, unit_serial_number, MAX_SERIAL_LEN);
4e9fdfcc 559
29804cc1 560 memzero(page_83, SCSI_INQ_BUFF_LEN);
efc2774c 561 retval = scsi_inquiry(dev_scsi, fd, 1, PAGE_83, page_83,
912541b0
KS
562 SCSI_INQ_BUFF_LEN);
563 if (retval < 0)
564 return 1;
565
566 if (page_83[1] != PAGE_83) {
9f6445e3 567 log_debug("%s: Invalid page 0x83", dev_scsi->kernel);
912541b0
KS
568 return 1;
569 }
570
571 /*
572 * XXX Some devices (IBM 3542) return all spaces for an identifier if
573 * the LUN is not actually configured. This leads to identifiers of
574 * the form: "1 ".
575 */
576
577 /*
578 * Model 4, 5, and (some) model 6 EMC Symmetrix devices return
579 * a page 83 reply according to SCSI-2 format instead of SPC-2/3.
580 *
581 * The SCSI-2 page 83 format returns an IEEE WWN in binary
582 * encoded hexi-decimal in the 16 bytes following the initial
583 * 4-byte page 83 reply header.
584 *
585 * Both the SPC-2 and SPC-3 formats return an IEEE WWN as part
586 * of an Identification descriptor. The 3rd byte of the first
587 * Identification descriptor is a reserved (BSZ) byte field.
588 *
589 * Reference the 7th byte of the page 83 reply to determine
590 * whether the reply is compliant with SCSI-2 or SPC-2/3
591 * specifications. A zero value in the 7th byte indicates
592 * an SPC-2/3 conformant reply, (i.e., the reserved field of the
593 * first Identification descriptor). This byte will be non-zero
594 * for a SCSI-2 conformant page 83 reply from these EMC
595 * Symmetrix models since the 7th byte of the reply corresponds
596 * to the 4th and 5th nibbles of the 6-byte OUI for EMC, that is,
597 * 0x006048.
598 */
599
600 if (page_83[6] != 0)
efc2774c 601 return check_fill_0x83_prespc3(dev_scsi, page_83, id_search_list,
912541b0
KS
602 serial, serial_short, len);
603
604 /*
605 * Search for a match in the prioritized id_search_list - since WWN ids
4e9fdfcc 606 * come first we can pick up the WWN in check_fill_0x83_id().
912541b0
KS
607 */
608 for (id_ind = 0;
609 id_ind < sizeof(id_search_list)/sizeof(id_search_list[0]);
610 id_ind++) {
611 /*
612 * Examine each descriptor returned. There is normally only
613 * one or a small number of descriptors.
614 */
1f7b6872 615 for (j = 4; j <= ((unsigned)page_83[2] << 8) + (unsigned)page_83[3] + 3; j += page_83[j + 3] + 4) {
881a62de
ZJS
616 retval = check_fill_0x83_id(dev_scsi, page_83 + j,
617 id_search_list + id_ind,
912541b0
KS
618 serial, serial_short, len,
619 wwn, wwn_vendor_extension,
620 tgpt_group);
baa30fbc 621 if (!retval)
912541b0 622 return retval;
baa30fbc 623 else if (retval < 0)
912541b0 624 return retval;
912541b0
KS
625 }
626 }
627 return 1;
c521693b
GKH
628}
629
50be1401
EG
630/*
631 * Get device identification VPD page for older SCSI-2 device which is not
632 * compliant with either SPC-2 or SPC-3 format.
633 *
634 * Return the hard coded error code value 2 if the page 83 reply is not
635 * conformant to the SCSI-2 format.
636 */
efc2774c
YW
637static int do_scsi_page83_prespc3_inquiry(struct scsi_id_device *dev_scsi, int fd,
638 char *serial, char *serial_short, int len) {
912541b0
KS
639 int retval;
640 int i, j;
641 unsigned char page_83[SCSI_INQ_BUFF_LEN];
642
29804cc1 643 memzero(page_83, SCSI_INQ_BUFF_LEN);
efc2774c 644 retval = scsi_inquiry(dev_scsi, fd, 1, PAGE_83, page_83, SCSI_INQ_BUFF_LEN);
912541b0
KS
645 if (retval < 0)
646 return 1;
647
648 if (page_83[1] != PAGE_83) {
9f6445e3 649 log_debug("%s: Invalid page 0x83", dev_scsi->kernel);
912541b0
KS
650 return 1;
651 }
652 /*
653 * Model 4, 5, and (some) model 6 EMC Symmetrix devices return
654 * a page 83 reply according to SCSI-2 format instead of SPC-2/3.
655 *
656 * The SCSI-2 page 83 format returns an IEEE WWN in binary
657 * encoded hexi-decimal in the 16 bytes following the initial
658 * 4-byte page 83 reply header.
659 *
660 * Both the SPC-2 and SPC-3 formats return an IEEE WWN as part
661 * of an Identification descriptor. The 3rd byte of the first
662 * Identification descriptor is a reserved (BSZ) byte field.
663 *
664 * Reference the 7th byte of the page 83 reply to determine
665 * whether the reply is compliant with SCSI-2 or SPC-2/3
666 * specifications. A zero value in the 7th byte indicates
667 * an SPC-2/3 conformant reply, (i.e., the reserved field of the
668 * first Identification descriptor). This byte will be non-zero
669 * for a SCSI-2 conformant page 83 reply from these EMC
670 * Symmetrix models since the 7th byte of the reply corresponds
671 * to the 4th and 5th nibbles of the 6-byte OUI for EMC, that is,
672 * 0x006048.
673 */
674 if (page_83[6] == 0)
675 return 2;
676
c0373eb0 677 serial[0] = hex_str[SCSI_ID_NAA];
912541b0
KS
678 /*
679 * The first four bytes contain data, not a descriptor.
680 */
681 i = 4;
682 j = strlen(serial);
683 /*
684 * Binary descriptor, convert to ASCII,
685 * using two bytes of ASCII for each byte
686 * in the page_83.
687 */
688 while (i < (page_83[3]+4)) {
689 serial[j++] = hex_str[(page_83[i] & 0xf0) >> 4];
690 serial[j++] = hex_str[page_83[i] & 0x0f];
691 i++;
692 }
912541b0 693 return 0;
50be1401
EG
694}
695
01f950e2 696/* Get unit serial number VPD page */
efc2774c
YW
697static int do_scsi_page80_inquiry(struct scsi_id_device *dev_scsi, int fd,
698 char *serial, char *serial_short, int max_len) {
912541b0
KS
699 int retval;
700 int ser_ind;
701 int i;
702 int len;
703 unsigned char buf[SCSI_INQ_BUFF_LEN];
704
29804cc1 705 memzero(buf, SCSI_INQ_BUFF_LEN);
efc2774c 706 retval = scsi_inquiry(dev_scsi, fd, 1, PAGE_80, buf, SCSI_INQ_BUFF_LEN);
912541b0
KS
707 if (retval < 0)
708 return retval;
709
710 if (buf[1] != PAGE_80) {
9f6445e3 711 log_debug("%s: Invalid page 0x80", dev_scsi->kernel);
912541b0
KS
712 return 1;
713 }
714
715 len = 1 + VENDOR_LENGTH + MODEL_LENGTH + buf[3];
716 if (max_len < len) {
9f6445e3 717 log_debug("%s: length %d too short - need %d",
461577ee 718 dev_scsi->kernel, max_len, len);
912541b0
KS
719 return 1;
720 }
721 /*
722 * Prepend 'S' to avoid unlikely collision with page 0x83 vendor
723 * specific type where we prepend '0' + vendor + model.
724 */
4e9fdfcc 725 len = buf[3];
4e361acc 726 if (serial) {
4e9fdfcc 727 serial[0] = 'S';
099c77fd 728 ser_ind = append_vendor_model(dev_scsi, serial + 1);
4e9fdfcc
DZ
729 if (ser_ind < 0)
730 return 1;
b929bf04 731 ser_ind++; /* for the leading 'S' */
4e9fdfcc
DZ
732 for (i = 4; i < len + 4; i++, ser_ind++)
733 serial[ser_ind] = buf[i];
734 }
4e361acc 735 if (serial_short) {
881a62de 736 memcpy(serial_short, buf + 4, len);
4e9fdfcc
DZ
737 serial_short[len] = '\0';
738 }
912541b0 739 return 0;
c521693b
GKH
740}
741
efc2774c 742int scsi_std_inquiry(struct scsi_id_device *dev_scsi, const char *devname) {
912541b0
KS
743 int fd;
744 unsigned char buf[SCSI_INQ_BUFF_LEN];
745 struct stat statbuf;
746 int err = 0;
747
c8a202b7 748 fd = open(devname, O_RDONLY | O_NONBLOCK | O_CLOEXEC);
912541b0 749 if (fd < 0) {
56f64d95 750 log_debug_errno(errno, "scsi_id: cannot open %s: %m", devname);
912541b0
KS
751 return 1;
752 }
753
754 if (fstat(fd, &statbuf) < 0) {
56f64d95 755 log_debug_errno(errno, "scsi_id: cannot stat %s: %m", devname);
912541b0
KS
756 err = 2;
757 goto out;
758 }
759 sprintf(dev_scsi->kernel,"%d:%d", major(statbuf.st_rdev),
760 minor(statbuf.st_rdev));
761
29804cc1 762 memzero(buf, SCSI_INQ_BUFF_LEN);
efc2774c 763 err = scsi_inquiry(dev_scsi, fd, 0, 0, buf, SCSI_INQ_BUFF_LEN);
912541b0
KS
764 if (err < 0)
765 goto out;
766
767 err = 0;
768 memcpy(dev_scsi->vendor, buf + 8, 8);
769 dev_scsi->vendor[8] = '\0';
770 memcpy(dev_scsi->model, buf + 16, 16);
771 dev_scsi->model[16] = '\0';
772 memcpy(dev_scsi->revision, buf + 32, 4);
773 dev_scsi->revision[4] = '\0';
774 sprintf(dev_scsi->type,"%x", buf[0] & 0x1f);
87cf9f5a 775
24dabba5 776out:
912541b0
KS
777 close(fd);
778 return err;
87cf9f5a
HR
779}
780
efc2774c
YW
781int scsi_get_serial(struct scsi_id_device *dev_scsi, const char *devname,
782 int page_code, int len) {
912541b0
KS
783 unsigned char page0[SCSI_INQ_BUFF_LEN];
784 int fd = -1;
785 int cnt;
786 int ind;
787 int retval;
788
29804cc1 789 memzero(dev_scsi->serial, len);
ef309a68 790 initialize_srand();
912541b0
KS
791 for (cnt = 20; cnt > 0; cnt--) {
792 struct timespec duration;
793
c8a202b7 794 fd = open(devname, O_RDONLY | O_NONBLOCK | O_CLOEXEC);
912541b0
KS
795 if (fd >= 0 || errno != EBUSY)
796 break;
797 duration.tv_sec = 0;
798 duration.tv_nsec = (200 * 1000 * 1000) + (rand() % 100 * 1000 * 1000);
799 nanosleep(&duration, NULL);
800 }
801 if (fd < 0)
802 return 1;
803
804 if (page_code == PAGE_80) {
efc2774c 805 if (do_scsi_page80_inquiry(dev_scsi, fd, dev_scsi->serial, dev_scsi->serial_short, len)) {
912541b0
KS
806 retval = 1;
807 goto completed;
808 } else {
809 retval = 0;
810 goto completed;
811 }
812 } else if (page_code == PAGE_83) {
efc2774c 813 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)) {
912541b0
KS
814 retval = 1;
815 goto completed;
816 } else {
817 retval = 0;
818 goto completed;
819 }
820 } else if (page_code == PAGE_83_PRE_SPC3) {
efc2774c 821 retval = do_scsi_page83_prespc3_inquiry(dev_scsi, fd, dev_scsi->serial, dev_scsi->serial_short, len);
912541b0
KS
822 if (retval) {
823 /*
824 * Fallback to servicing a SPC-2/3 compliant page 83
825 * inquiry if the page 83 reply format does not
826 * conform to pre-SPC3 expectations.
827 */
828 if (retval == 2) {
efc2774c 829 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)) {
912541b0
KS
830 retval = 1;
831 goto completed;
832 } else {
833 retval = 0;
834 goto completed;
835 }
836 }
837 else {
838 retval = 1;
839 goto completed;
840 }
841 } else {
842 retval = 0;
843 goto completed;
844 }
845 } else if (page_code != 0x00) {
9f6445e3 846 log_debug("%s: unsupported page code 0x%d", dev_scsi->kernel, page_code);
ab261e12
LN
847 retval = 1;
848 goto completed;
912541b0
KS
849 }
850
851 /*
852 * Get page 0, the page of the pages. By default, try from best to
853 * worst of supported pages: 0x83 then 0x80.
854 */
efc2774c 855 if (do_scsi_page0_inquiry(dev_scsi, fd, page0, SCSI_INQ_BUFF_LEN)) {
912541b0
KS
856 /*
857 * Don't try anything else. Black list if a specific page
858 * should be used for this vendor+model, or maybe have an
859 * optional fall-back to page 0x80 or page 0x83.
860 */
861 retval = 1;
862 goto completed;
863 }
864
912541b0
KS
865 for (ind = 4; ind <= page0[3] + 3; ind++)
866 if (page0[ind] == PAGE_83)
efc2774c 867 if (!do_scsi_page83_inquiry(dev_scsi, fd,
912541b0
KS
868 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)) {
869 /*
870 * Success
871 */
872 retval = 0;
873 goto completed;
874 }
875
876 for (ind = 4; ind <= page0[3] + 3; ind++)
877 if (page0[ind] == PAGE_80)
efc2774c 878 if (!do_scsi_page80_inquiry(dev_scsi, fd,
912541b0
KS
879 dev_scsi->serial, dev_scsi->serial_short, len)) {
880 /*
881 * Success
882 */
883 retval = 0;
884 goto completed;
885 }
886 retval = 1;
24dabba5 887
c521693b 888completed:
912541b0
KS
889 close(fd);
890 return retval;
c521693b 891}