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