1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * Copyright © IBM Corp. 2003
5 * Author: Patrick Mansfield<patmans@us.ibm.com>
11 #include <linux/bsg.h>
12 #include <linux/types.h>
13 #include <scsi/scsi.h>
17 #include <sys/ioctl.h>
19 #include <sys/types.h>
23 #include "devnum-util.h"
24 #include "memory-util.h"
25 #include "random-util.h"
28 #include "string-util.h"
31 * A priority based list of id, naa, and binary/ascii for the identifier
32 * descriptor in VPD page 0x83.
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.
38 static const struct scsi_id_search_values id_search_list
[] = {
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
},
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
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
},
63 static const char hex_str
[]="0123456789abcdef";
66 * Values returned in the result/status, only the ones used by the code
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 DID_TRANSPORT_DISRUPTED 0x0e /* Transport disrupted and should retry */
74 #define DRIVER_TIMEOUT 0x06
75 #define DRIVER_SENSE 0x08 /* Sense_buffer has been set */
77 /* The following "category" function returns one of the following */
78 #define SG_ERR_CAT_CLEAN 0 /* No errors or other information */
79 #define SG_ERR_CAT_MEDIA_CHANGED 1 /* interpreted from sense buffer */
80 #define SG_ERR_CAT_RESET 2 /* interpreted from sense buffer */
81 #define SG_ERR_CAT_TIMEOUT 3
82 #define SG_ERR_CAT_RECOVERED 4 /* Successful command after recovered err */
83 #define SG_ERR_CAT_NOTSUPPORTED 5 /* Illegal / unsupported command */
84 #define SG_ERR_CAT_RETRY 6 /* Command should be retried */
85 #define SG_ERR_CAT_SENSE 98 /* Something else in the sense buffer */
86 #define SG_ERR_CAT_OTHER 99 /* Some other error/warning */
88 static int do_scsi_page80_inquiry(struct scsi_id_device
*dev_scsi
, int fd
,
89 char *serial
, char *serial_short
, int max_len
);
91 static int sg_err_category_new(int scsi_status
, int msg_status
, int
92 host_status
, int driver_status
, const
93 unsigned char *sense_buffer
, int sb_len
) {
97 * XXX change to return only two values - failed or OK.
100 if (!scsi_status
&& !host_status
&& !driver_status
)
101 return SG_ERR_CAT_CLEAN
;
103 if (IN_SET(scsi_status
, SCSI_CHECK_CONDITION
, SCSI_COMMAND_TERMINATED
) ||
104 (driver_status
& 0xf) == DRIVER_SENSE
) {
105 if (sense_buffer
&& (sb_len
> 2)) {
109 if (sense_buffer
[0] & 0x2) {
110 sense_key
= sense_buffer
[1] & 0xf;
111 asc
= sense_buffer
[2];
113 sense_key
= sense_buffer
[2] & 0xf;
114 asc
= (sb_len
> 12) ? sense_buffer
[12] : 0;
117 if (sense_key
== RECOVERED_ERROR
)
118 return SG_ERR_CAT_RECOVERED
;
119 else if (sense_key
== UNIT_ATTENTION
) {
121 return SG_ERR_CAT_MEDIA_CHANGED
;
123 return SG_ERR_CAT_RESET
;
124 } else if (sense_key
== ILLEGAL_REQUEST
)
125 return SG_ERR_CAT_NOTSUPPORTED
;
127 return SG_ERR_CAT_SENSE
;
130 if (IN_SET(host_status
, DID_NO_CONNECT
, DID_BUS_BUSY
, DID_TIME_OUT
))
131 return SG_ERR_CAT_TIMEOUT
;
132 if (host_status
== DID_TRANSPORT_DISRUPTED
)
133 return SG_ERR_CAT_RETRY
;
136 if (driver_status
== DRIVER_TIMEOUT
)
137 return SG_ERR_CAT_TIMEOUT
;
139 return SG_ERR_CAT_OTHER
;
142 static int sg_err_category3(struct sg_io_hdr
*hp
) {
143 return sg_err_category_new(hp
->status
, hp
->msg_status
,
144 hp
->host_status
, hp
->driver_status
,
145 hp
->sbp
, hp
->sb_len_wr
);
148 static int sg_err_category4(struct sg_io_v4
*hp
) {
149 return sg_err_category_new(hp
->device_status
, 0,
150 hp
->transport_status
, hp
->driver_status
,
151 (unsigned char *)(uintptr_t)hp
->response
,
155 static int scsi_dump_sense(struct scsi_id_device
*dev_scsi
,
156 unsigned char *sense_buffer
, int sb_len
) {
158 unsigned code
, sense_class
, sense_key
, asc
, ascq
;
161 * Figure out and print the sense key, asc and ascq.
163 * If you want to suppress these for a particular drive model, add
164 * a deny list entry in the scsi_id config file.
166 * XXX We probably need to: lookup the sense/asc/ascq in a retry
167 * table, and if found return 1 (after dumping the sense, asc, and
168 * ascq). So, if/when we get something like a power on/reset,
169 * we'll retry the command.
173 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
),
174 "%s: sense buffer empty",
177 sense_class
= (sense_buffer
[0] >> 4) & 0x07;
178 code
= sense_buffer
[0] & 0xf;
180 if (sense_class
== 7) {
182 * extended sense data.
184 s
= sense_buffer
[7] + 8;
186 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
),
187 "%s: sense buffer too small %d bytes, %d bytes too short",
188 dev_scsi
->kernel
, sb_len
,
191 if (IN_SET(code
, 0x0, 0x1)) {
192 sense_key
= sense_buffer
[2] & 0xf;
197 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
),
198 "%s: sense result too small %d bytes",
199 dev_scsi
->kernel
, s
);
201 asc
= sense_buffer
[12];
202 ascq
= sense_buffer
[13];
203 } else if (IN_SET(code
, 0x2, 0x3)) {
204 sense_key
= sense_buffer
[1] & 0xf;
205 asc
= sense_buffer
[2];
206 ascq
= sense_buffer
[3];
208 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
),
209 "%s: invalid sense code 0x%x",
210 dev_scsi
->kernel
, code
);
212 log_debug("%s: sense key 0x%x ASC 0x%x ASCQ 0x%x",
213 dev_scsi
->kernel
, sense_key
, asc
, ascq
);
216 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
),
217 "%s: sense buffer too small %d bytes, %d bytes too short",
218 dev_scsi
->kernel
, sb_len
,
221 if (sense_buffer
[0] < 15)
222 log_debug("%s: old sense key: 0x%x", dev_scsi
->kernel
, sense_buffer
[0] & 0x0fu
);
224 log_debug("%s: sense = %2x %2x",
225 dev_scsi
->kernel
, sense_buffer
[0], sense_buffer
[2]);
226 log_debug("%s: non-extended sense class %u code 0x%0x",
227 dev_scsi
->kernel
, sense_class
, code
);
234 static int scsi_dump(struct scsi_id_device
*dev_scsi
, struct sg_io_hdr
*io
) {
235 if (!io
->status
&& !io
->host_status
&& !io
->msg_status
&&
238 * Impossible, should not be called.
240 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
),
241 "%s: called with no error",
244 log_debug("%s: sg_io failed status 0x%x 0x%x 0x%x 0x%x",
245 dev_scsi
->kernel
, io
->driver_status
, io
->host_status
, io
->msg_status
, io
->status
);
246 if (io
->status
== SCSI_CHECK_CONDITION
)
247 return scsi_dump_sense(dev_scsi
, io
->sbp
, io
->sb_len_wr
);
252 static int scsi_dump_v4(struct scsi_id_device
*dev_scsi
, struct sg_io_v4
*io
) {
253 if (!io
->device_status
&& !io
->transport_status
&&
256 * Impossible, should not be called.
258 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
),
259 "%s: called with no error",
262 log_debug("%s: sg_io failed status 0x%x 0x%x 0x%x",
263 dev_scsi
->kernel
, io
->driver_status
, io
->transport_status
, io
->device_status
);
264 if (io
->device_status
== SCSI_CHECK_CONDITION
)
265 return scsi_dump_sense(dev_scsi
, (unsigned char *)(uintptr_t)io
->response
,
271 static int scsi_inquiry(struct scsi_id_device
*dev_scsi
, int fd
,
272 unsigned char evpd
, unsigned char page
,
273 unsigned char *buf
, unsigned buflen
) {
274 unsigned char inq_cmd
[INQUIRY_CMDLEN
] =
275 { INQUIRY_CMD
, evpd
, page
, 0, buflen
, 0 };
276 unsigned char sense
[SENSE_BUFF_LEN
];
278 struct sg_io_v4 io_v4
;
279 struct sg_io_hdr io_hdr
;
280 int retry
= 3; /* rather random */
283 if (buflen
> SCSI_INQ_BUFF_LEN
)
284 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
),
285 "buflen %u too long", buflen
);
288 if (dev_scsi
->use_sg
== 4) {
289 memzero(&io_v4
, sizeof(struct sg_io_v4
));
291 io_v4
.protocol
= BSG_PROTOCOL_SCSI
;
292 io_v4
.subprotocol
= BSG_SUB_PROTOCOL_SCSI_CMD
;
293 io_v4
.request_len
= sizeof(inq_cmd
);
294 io_v4
.request
= (uintptr_t)inq_cmd
;
295 io_v4
.max_response_len
= sizeof(sense
);
296 io_v4
.response
= (uintptr_t)sense
;
297 io_v4
.din_xfer_len
= buflen
;
298 io_v4
.din_xferp
= (uintptr_t)buf
;
299 io_buf
= (void *)&io_v4
;
301 memzero(&io_hdr
, sizeof(struct sg_io_hdr
));
302 io_hdr
.interface_id
= 'S';
303 io_hdr
.cmd_len
= sizeof(inq_cmd
);
304 io_hdr
.mx_sb_len
= sizeof(sense
);
305 io_hdr
.dxfer_direction
= SG_DXFER_FROM_DEV
;
306 io_hdr
.dxfer_len
= buflen
;
308 io_hdr
.cmdp
= inq_cmd
;
310 io_hdr
.timeout
= DEF_TIMEOUT
;
311 io_buf
= (void *)&io_hdr
;
314 retval
= ioctl(fd
, SG_IO
, io_buf
);
316 if (IN_SET(errno
, EINVAL
, ENOSYS
) && dev_scsi
->use_sg
== 4) {
317 dev_scsi
->use_sg
= 3;
320 log_debug_errno(errno
, "%s: ioctl failed: %m", dev_scsi
->kernel
);
324 if (dev_scsi
->use_sg
== 4)
325 retval
= sg_err_category4(io_buf
);
327 retval
= sg_err_category3(io_buf
);
330 case SG_ERR_CAT_NOTSUPPORTED
:
333 case SG_ERR_CAT_CLEAN
:
334 case SG_ERR_CAT_RECOVERED
:
337 case SG_ERR_CAT_RETRY
:
341 if (dev_scsi
->use_sg
== 4)
342 retval
= scsi_dump_v4(dev_scsi
, io_buf
);
344 retval
= scsi_dump(dev_scsi
, io_buf
);
349 } else if (retval
> 0) {
357 log_debug("%s: Unable to get INQUIRY vpd %d page 0x%x.",
358 dev_scsi
->kernel
, evpd
, page
);
363 /* Get list of supported EVPD pages */
364 static int do_scsi_page0_inquiry(struct scsi_id_device
*dev_scsi
, int fd
,
365 unsigned char *buffer
, unsigned len
) {
368 memzero(buffer
, len
);
369 retval
= scsi_inquiry(dev_scsi
, fd
, 1, 0x0, buffer
, len
);
373 if (buffer
[1] != 0) {
374 log_debug("%s: page 0 not available.", dev_scsi
->kernel
);
377 if (buffer
[3] > len
) {
378 log_debug("%s: page 0 buffer too long %d", dev_scsi
->kernel
, buffer
[3]);
383 * Following check is based on code once included in the 2.5.x
386 * Some ill behaved devices return the standard inquiry here
387 * rather than the evpd data, snoop the data to verify.
389 if (buffer
[3] > MODEL_LENGTH
) {
391 * If the vendor id appears in the page assume the page is
394 if (strneq((char*) buffer
+ VENDOR_LENGTH
, dev_scsi
->vendor
, VENDOR_LENGTH
)) {
395 log_debug("%s: invalid page0 data", dev_scsi
->kernel
);
402 static int append_vendor_model(
403 const struct scsi_id_device
*dev_scsi
,
404 char buf
[static VENDOR_LENGTH
+ MODEL_LENGTH
]) {
409 if (strnlen(dev_scsi
->vendor
, VENDOR_LENGTH
) != VENDOR_LENGTH
)
410 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
),
411 "%s: bad vendor string \"%s\"",
412 dev_scsi
->kernel
, dev_scsi
->vendor
);
413 if (strnlen(dev_scsi
->model
, MODEL_LENGTH
) != MODEL_LENGTH
)
414 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
),
415 "%s: bad model string \"%s\"",
416 dev_scsi
->kernel
, dev_scsi
->model
);
417 memcpy(buf
, dev_scsi
->vendor
, VENDOR_LENGTH
);
418 memcpy(buf
+ VENDOR_LENGTH
, dev_scsi
->model
, MODEL_LENGTH
);
419 return VENDOR_LENGTH
+ MODEL_LENGTH
;
423 * check_fill_0x83_id - check the page 0x83 id, if OK allocate and fill
426 static int check_fill_0x83_id(struct scsi_id_device
*dev_scsi
,
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
,
431 char *wwn_vendor_extension
, char *tgpt_group
) {
435 * ASSOCIATION must be with the device (value 0)
436 * or with the target port for SCSI_ID_TGTPORT
438 if ((page_83
[1] & 0x30) == 0x10) {
439 if (id_search
->id_type
!= SCSI_ID_TGTGROUP
)
441 } else if ((page_83
[1] & 0x30) != 0)
444 if ((page_83
[1] & 0x0f) != id_search
->id_type
)
448 * Possibly check NAA sub-type.
450 if ((id_search
->naa_type
!= SCSI_ID_NAA_DONT_CARE
) &&
451 (id_search
->naa_type
!= (page_83
[4] & 0xf0) >> 4))
455 * Check for matching code set - ASCII or BINARY.
457 if ((page_83
[0] & 0x0f) != id_search
->code_set
)
461 * page_83[3]: identifier length
464 if ((page_83
[0] & 0x0f) != SCSI_ID_ASCII
)
466 * If not ASCII, use two bytes for each binary value.
471 * Add one byte for the NUL termination, and one for the id_type.
474 if (id_search
->id_type
== SCSI_ID_VENDOR_SPECIFIC
)
475 len
+= VENDOR_LENGTH
+ MODEL_LENGTH
;
478 log_debug("%s: length %d too short - need %d",
479 dev_scsi
->kernel
, max_len
, len
);
483 if (id_search
->id_type
== SCSI_ID_TGTGROUP
&& tgpt_group
!= NULL
) {
486 group
= ((unsigned)page_83
[6] << 8) | page_83
[7];
487 sprintf(tgpt_group
,"%x", group
);
491 serial
[0] = hex_str
[id_search
->id_type
];
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.
499 if (id_search
->id_type
== SCSI_ID_VENDOR_SPECIFIC
)
500 if (append_vendor_model(dev_scsi
, serial
+ 1) < 0)
503 i
= 4; /* offset to the start of the identifier */
504 s
= j
= strlen(serial
);
505 if ((page_83
[0] & 0x0f) == SCSI_ID_ASCII
) {
509 while (i
< (4 + page_83
[3]))
510 serial
[j
++] = page_83
[i
++];
513 * Binary descriptor, convert to ASCII, using two bytes of
514 * ASCII for each byte in the page_83.
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];
523 strcpy(serial_short
, serial
+ s
);
525 if (id_search
->id_type
== SCSI_ID_NAA
&& wwn
!= NULL
) {
526 strncpy(wwn
, serial
+ s
, 16);
527 if (wwn_vendor_extension
)
528 strncpy(wwn_vendor_extension
, serial
+ s
+ 16, 16);
534 /* Extract the raw binary from VPD 0x83 pre-SPC devices */
535 static int check_fill_0x83_prespc3(struct scsi_id_device
*dev_scsi
,
536 unsigned char *page_83
,
537 const struct scsi_id_search_values
538 *id_search
, char *serial
, char *serial_short
, int max_len
) {
541 serial
[0] = hex_str
[SCSI_ID_NAA
];
542 /* serial has been memset to zero before */
543 j
= strlen(serial
); /* j = 1; */
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];
549 serial
[max_len
-1] = 0;
550 strncpy(serial_short
, serial
, max_len
-1);
554 /* Get device identification VPD page */
555 static int do_scsi_page83_inquiry(struct scsi_id_device
*dev_scsi
, int fd
,
556 char *serial
, char *serial_short
, int len
,
557 char *unit_serial_number
, char *wwn
,
558 char *wwn_vendor_extension
, char *tgpt_group
) {
561 unsigned char page_83
[SCSI_INQ_BUFF_LEN
];
563 /* also pick up the page 80 serial number */
564 do_scsi_page80_inquiry(dev_scsi
, fd
, NULL
, unit_serial_number
, MAX_SERIAL_LEN
);
566 memzero(page_83
, SCSI_INQ_BUFF_LEN
);
567 retval
= scsi_inquiry(dev_scsi
, fd
, 1, PAGE_83
, page_83
,
572 if (page_83
[1] != PAGE_83
) {
573 log_debug("%s: Invalid page 0x83", dev_scsi
->kernel
);
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
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.
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.
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.
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,
607 return check_fill_0x83_prespc3(dev_scsi
, page_83
, id_search_list
,
608 serial
, serial_short
, len
);
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().
615 id_ind
< sizeof(id_search_list
)/sizeof(id_search_list
[0]);
618 * Examine each descriptor returned. There is normally only
619 * one or a small number of descriptors.
621 for (j
= 4; j
<= ((unsigned)page_83
[2] << 8) + (unsigned)page_83
[3] + 3; j
+= page_83
[j
+ 3] + 4) {
622 retval
= check_fill_0x83_id(dev_scsi
, page_83
+ j
,
623 id_search_list
+ id_ind
,
624 serial
, serial_short
, len
,
625 wwn
, wwn_vendor_extension
,
637 * Get device identification VPD page for older SCSI-2 device which is not
638 * compliant with either SPC-2 or SPC-3 format.
640 * Return the hard coded error code value 2 if the page 83 reply is not
641 * conformant to the SCSI-2 format.
643 static int do_scsi_page83_prespc3_inquiry(struct scsi_id_device
*dev_scsi
, int fd
,
644 char *serial
, char *serial_short
, int len
) {
647 unsigned char page_83
[SCSI_INQ_BUFF_LEN
];
649 memzero(page_83
, SCSI_INQ_BUFF_LEN
);
650 retval
= scsi_inquiry(dev_scsi
, fd
, 1, PAGE_83
, page_83
, SCSI_INQ_BUFF_LEN
);
654 if (page_83
[1] != PAGE_83
) {
655 log_debug("%s: Invalid page 0x83", dev_scsi
->kernel
);
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.
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.
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.
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,
683 serial
[0] = hex_str
[SCSI_ID_NAA
];
685 * The first four bytes contain data, not a descriptor.
690 * Binary descriptor, convert to ASCII,
691 * using two bytes of ASCII for each byte
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];
702 /* Get unit serial number VPD page */
703 static int do_scsi_page80_inquiry(struct scsi_id_device
*dev_scsi
, int fd
,
704 char *serial
, char *serial_short
, int max_len
) {
709 unsigned char buf
[SCSI_INQ_BUFF_LEN
];
711 memzero(buf
, SCSI_INQ_BUFF_LEN
);
712 retval
= scsi_inquiry(dev_scsi
, fd
, 1, PAGE_80
, buf
, SCSI_INQ_BUFF_LEN
);
716 if (buf
[1] != PAGE_80
) {
717 log_debug("%s: Invalid page 0x80", dev_scsi
->kernel
);
721 len
= 1 + VENDOR_LENGTH
+ MODEL_LENGTH
+ buf
[3];
723 log_debug("%s: length %d too short - need %d",
724 dev_scsi
->kernel
, max_len
, len
);
728 * Prepend 'S' to avoid unlikely collision with page 0x83 vendor
729 * specific type where we prepend '0' + vendor + model.
734 ser_ind
= append_vendor_model(dev_scsi
, serial
+ 1);
737 ser_ind
++; /* for the leading 'S' */
738 for (i
= 4; i
< len
+ 4; i
++, ser_ind
++)
739 serial
[ser_ind
] = buf
[i
];
742 memcpy(serial_short
, buf
+ 4, len
);
743 serial_short
[len
] = '\0';
748 int scsi_std_inquiry(struct scsi_id_device
*dev_scsi
, const char *devname
) {
750 unsigned char buf
[SCSI_INQ_BUFF_LEN
];
754 fd
= open(devname
, O_RDONLY
| O_NONBLOCK
| O_CLOEXEC
| O_NOCTTY
);
756 log_debug_errno(errno
, "scsi_id: cannot open %s: %m", devname
);
760 if (fstat(fd
, &statbuf
) < 0) {
761 log_debug_errno(errno
, "scsi_id: cannot stat %s: %m", devname
);
765 format_devnum(statbuf
.st_rdev
, dev_scsi
->kernel
);
767 memzero(buf
, SCSI_INQ_BUFF_LEN
);
768 err
= scsi_inquiry(dev_scsi
, fd
, 0, 0, buf
, SCSI_INQ_BUFF_LEN
);
773 memcpy(dev_scsi
->vendor
, buf
+ 8, 8);
774 dev_scsi
->vendor
[8] = '\0';
775 memcpy(dev_scsi
->model
, buf
+ 16, 16);
776 dev_scsi
->model
[16] = '\0';
777 memcpy(dev_scsi
->revision
, buf
+ 32, 4);
778 dev_scsi
->revision
[4] = '\0';
779 dev_scsi
->type
= buf
[0] & 0x1f;
786 int scsi_get_serial(struct scsi_id_device
*dev_scsi
, const char *devname
,
787 int page_code
, int len
) {
788 unsigned char page0
[SCSI_INQ_BUFF_LEN
];
794 memzero(dev_scsi
->serial
, len
);
795 for (cnt
= 20; cnt
> 0; cnt
--) {
796 struct timespec duration
;
798 fd
= open(devname
, O_RDONLY
| O_NONBLOCK
| O_CLOEXEC
| O_NOCTTY
);
799 if (fd
>= 0 || errno
!= EBUSY
)
802 duration
.tv_nsec
= (200 * 1000 * 1000) + (random_u32() % 100 * 1000 * 1000);
803 nanosleep(&duration
, NULL
);
808 if (page_code
== PAGE_80
) {
809 if (do_scsi_page80_inquiry(dev_scsi
, fd
, dev_scsi
->serial
, dev_scsi
->serial_short
, len
)) {
816 } else if (page_code
== PAGE_83
) {
817 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
)) {
824 } else if (page_code
== PAGE_83_PRE_SPC3
) {
825 retval
= do_scsi_page83_prespc3_inquiry(dev_scsi
, fd
, dev_scsi
->serial
, dev_scsi
->serial_short
, len
);
828 * Fallback to servicing a SPC-2/3 compliant page 83
829 * inquiry if the page 83 reply format does not
830 * conform to pre-SPC3 expectations.
833 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
)) {
849 } else if (page_code
!= 0x00) {
850 log_debug("%s: unsupported page code 0x%d", dev_scsi
->kernel
, page_code
);
856 * Get page 0, the page of the pages. By default, try from best to
857 * worst of supported pages: 0x83 then 0x80.
859 if (do_scsi_page0_inquiry(dev_scsi
, fd
, page0
, SCSI_INQ_BUFF_LEN
)) {
861 * Don't try anything else. Black list if a specific page
862 * should be used for this vendor+model, or maybe have an
863 * optional fall-back to page 0x80 or page 0x83.
869 for (ind
= 4; ind
<= page0
[3] + 3; ind
++)
870 if (page0
[ind
] == PAGE_83
)
871 if (!do_scsi_page83_inquiry(dev_scsi
, fd
,
872 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
)) {
880 for (ind
= 4; ind
<= page0
[3] + 3; ind
++)
881 if (page0
[ind
] == PAGE_80
)
882 if (!do_scsi_page80_inquiry(dev_scsi
, fd
,
883 dev_scsi
->serial
, dev_scsi
->serial_short
, len
)) {