2 * Copyright (C) IBM Corp. 2003
4 * Author: Patrick Mansfield<patmans@us.ibm.com>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include <linux/bsg.h>
24 #include <linux/types.h>
25 #include <scsi/scsi.h>
30 #include <sys/ioctl.h>
32 #include <sys/types.h>
38 #include "libudev-private.h"
39 #include "random-util.h"
42 #include "string-util.h"
45 * A priority based list of id, naa, and binary/ascii for the identifier
46 * descriptor in VPD page 0x83.
48 * Brute force search for a match starting with the first value in the
49 * following id_search_list. This is not a performance issue, since there
50 * is normally one or some small number of descriptors.
52 static const struct scsi_id_search_values id_search_list
[] = {
53 { SCSI_ID_TGTGROUP
, SCSI_ID_NAA_DONT_CARE
, SCSI_ID_BINARY
},
54 { SCSI_ID_NAA
, SCSI_ID_NAA_IEEE_REG_EXTENDED
, SCSI_ID_BINARY
},
55 { SCSI_ID_NAA
, SCSI_ID_NAA_IEEE_REG_EXTENDED
, SCSI_ID_ASCII
},
56 { SCSI_ID_NAA
, SCSI_ID_NAA_IEEE_REG
, SCSI_ID_BINARY
},
57 { SCSI_ID_NAA
, SCSI_ID_NAA_IEEE_REG
, SCSI_ID_ASCII
},
59 * Devices already exist using NAA values that are now marked
60 * reserved. These should not conflict with other values, or it is
61 * a bug in the device. As long as we find the IEEE extended one
62 * first, we really don't care what other ones are used. Using
63 * don't care here means that a device that returns multiple
64 * non-IEEE descriptors in a random order will get different
67 { SCSI_ID_NAA
, SCSI_ID_NAA_DONT_CARE
, SCSI_ID_BINARY
},
68 { SCSI_ID_NAA
, SCSI_ID_NAA_DONT_CARE
, SCSI_ID_ASCII
},
69 { SCSI_ID_EUI_64
, SCSI_ID_NAA_DONT_CARE
, SCSI_ID_BINARY
},
70 { SCSI_ID_EUI_64
, SCSI_ID_NAA_DONT_CARE
, SCSI_ID_ASCII
},
71 { SCSI_ID_T10_VENDOR
, SCSI_ID_NAA_DONT_CARE
, SCSI_ID_BINARY
},
72 { SCSI_ID_T10_VENDOR
, SCSI_ID_NAA_DONT_CARE
, SCSI_ID_ASCII
},
73 { SCSI_ID_VENDOR_SPECIFIC
, SCSI_ID_NAA_DONT_CARE
, SCSI_ID_BINARY
},
74 { SCSI_ID_VENDOR_SPECIFIC
, SCSI_ID_NAA_DONT_CARE
, SCSI_ID_ASCII
},
77 static const char hex_str
[]="0123456789abcdef";
80 * Values returned in the result/status, only the ones used by the code
84 #define DID_NO_CONNECT 0x01 /* Unable to connect before timeout */
85 #define DID_BUS_BUSY 0x02 /* Bus remain busy until timeout */
86 #define DID_TIME_OUT 0x03 /* Timed out for some other reason */
87 #define DRIVER_TIMEOUT 0x06
88 #define DRIVER_SENSE 0x08 /* Sense_buffer has been set */
90 /* The following "category" function returns one of the following */
91 #define SG_ERR_CAT_CLEAN 0 /* No errors or other information */
92 #define SG_ERR_CAT_MEDIA_CHANGED 1 /* interpreted from sense buffer */
93 #define SG_ERR_CAT_RESET 2 /* interpreted from sense buffer */
94 #define SG_ERR_CAT_TIMEOUT 3
95 #define SG_ERR_CAT_RECOVERED 4 /* Successful command after recovered err */
96 #define SG_ERR_CAT_NOTSUPPORTED 5 /* Illegal / unsupported command */
97 #define SG_ERR_CAT_SENSE 98 /* Something else in the sense buffer */
98 #define SG_ERR_CAT_OTHER 99 /* Some other error/warning */
100 static int do_scsi_page80_inquiry(struct udev
*udev
,
101 struct scsi_id_device
*dev_scsi
, int fd
,
102 char *serial
, char *serial_short
, int max_len
);
104 static int sg_err_category_new(struct udev
*udev
,
105 int scsi_status
, int msg_status
, int
106 host_status
, int driver_status
, const
107 unsigned char *sense_buffer
, int sb_len
)
112 * XXX change to return only two values - failed or OK.
115 if (!scsi_status
&& !host_status
&& !driver_status
)
116 return SG_ERR_CAT_CLEAN
;
118 if (IN_SET(scsi_status
, SCSI_CHECK_CONDITION
, SCSI_COMMAND_TERMINATED
) ||
119 (driver_status
& 0xf) == DRIVER_SENSE
) {
120 if (sense_buffer
&& (sb_len
> 2)) {
124 if (sense_buffer
[0] & 0x2) {
125 sense_key
= sense_buffer
[1] & 0xf;
126 asc
= sense_buffer
[2];
128 sense_key
= sense_buffer
[2] & 0xf;
129 asc
= (sb_len
> 12) ? sense_buffer
[12] : 0;
132 if (sense_key
== RECOVERED_ERROR
)
133 return SG_ERR_CAT_RECOVERED
;
134 else if (sense_key
== UNIT_ATTENTION
) {
136 return SG_ERR_CAT_MEDIA_CHANGED
;
138 return SG_ERR_CAT_RESET
;
139 } else if (sense_key
== ILLEGAL_REQUEST
)
140 return SG_ERR_CAT_NOTSUPPORTED
;
142 return SG_ERR_CAT_SENSE
;
145 if (IN_SET(host_status
, DID_NO_CONNECT
, DID_BUS_BUSY
, DID_TIME_OUT
))
146 return SG_ERR_CAT_TIMEOUT
;
149 if (driver_status
== DRIVER_TIMEOUT
)
150 return SG_ERR_CAT_TIMEOUT
;
152 return SG_ERR_CAT_OTHER
;
155 static int sg_err_category3(struct udev
*udev
, struct sg_io_hdr
*hp
)
157 return sg_err_category_new(udev
,
158 hp
->status
, hp
->msg_status
,
159 hp
->host_status
, hp
->driver_status
,
160 hp
->sbp
, hp
->sb_len_wr
);
163 static int sg_err_category4(struct udev
*udev
, struct sg_io_v4
*hp
)
165 return sg_err_category_new(udev
, hp
->device_status
, 0,
166 hp
->transport_status
, hp
->driver_status
,
167 (unsigned char *)(uintptr_t)hp
->response
,
171 static int scsi_dump_sense(struct udev
*udev
,
172 struct scsi_id_device
*dev_scsi
,
173 unsigned char *sense_buffer
, int sb_len
)
181 char out_buffer
[256];
186 * Figure out and print the sense key, asc and ascq.
188 * If you want to suppress these for a particular drive model, add
189 * a black list entry in the scsi_id config file.
191 * XXX We probably need to: lookup the sense/asc/ascq in a retry
192 * table, and if found return 1 (after dumping the sense, asc, and
193 * ascq). So, if/when we get something like a power on/reset,
194 * we'll retry the command.
198 log_debug("%s: sense buffer empty", dev_scsi
->kernel
);
202 sense_class
= (sense_buffer
[0] >> 4) & 0x07;
203 code
= sense_buffer
[0] & 0xf;
205 if (sense_class
== 7) {
207 * extended sense data.
209 s
= sense_buffer
[7] + 8;
211 log_debug("%s: sense buffer too small %d bytes, %d bytes too short",
212 dev_scsi
->kernel
, sb_len
, s
- sb_len
);
215 if (IN_SET(code
, 0x0, 0x1)) {
216 sense_key
= sense_buffer
[2] & 0xf;
221 log_debug("%s: sense result too" " small %d bytes",
222 dev_scsi
->kernel
, s
);
225 asc
= sense_buffer
[12];
226 ascq
= sense_buffer
[13];
227 } else if (IN_SET(code
, 0x2, 0x3)) {
228 sense_key
= sense_buffer
[1] & 0xf;
229 asc
= sense_buffer
[2];
230 ascq
= sense_buffer
[3];
232 log_debug("%s: invalid sense code 0x%x",
233 dev_scsi
->kernel
, code
);
236 log_debug("%s: sense key 0x%x ASC 0x%x ASCQ 0x%x",
237 dev_scsi
->kernel
, sense_key
, asc
, ascq
);
240 log_debug("%s: sense buffer too small %d bytes, %d bytes too short",
241 dev_scsi
->kernel
, sb_len
, 4 - sb_len
);
245 if (sense_buffer
[0] < 15)
246 log_debug("%s: old sense key: 0x%x", dev_scsi
->kernel
, sense_buffer
[0] & 0x0f);
248 log_debug("%s: sense = %2x %2x",
249 dev_scsi
->kernel
, sense_buffer
[0], sense_buffer
[2]);
250 log_debug("%s: non-extended sense class %d code 0x%0x",
251 dev_scsi
->kernel
, sense_class
, code
);
256 for (i
= 0, j
= 0; (i
< s
) && (j
< 254); i
++) {
257 out_buffer
[j
++] = hex_str
[(sense_buffer
[i
] & 0xf0) >> 4];
258 out_buffer
[j
++] = hex_str
[sense_buffer
[i
] & 0x0f];
259 out_buffer
[j
++] = ' ';
261 out_buffer
[j
] = '\0';
262 log_debug("%s: sense dump:", dev_scsi
->kernel
);
263 log_debug("%s: %s", dev_scsi
->kernel
, out_buffer
);
269 static int scsi_dump(struct udev
*udev
,
270 struct scsi_id_device
*dev_scsi
, struct sg_io_hdr
*io
)
272 if (!io
->status
&& !io
->host_status
&& !io
->msg_status
&&
273 !io
->driver_status
) {
275 * Impossible, should not be called.
277 log_debug("%s: called with no error", __FUNCTION__
);
281 log_debug("%s: sg_io failed status 0x%x 0x%x 0x%x 0x%x",
282 dev_scsi
->kernel
, io
->driver_status
, io
->host_status
, io
->msg_status
, io
->status
);
283 if (io
->status
== SCSI_CHECK_CONDITION
)
284 return scsi_dump_sense(udev
, dev_scsi
, io
->sbp
, io
->sb_len_wr
);
289 static int scsi_dump_v4(struct udev
*udev
,
290 struct scsi_id_device
*dev_scsi
, struct sg_io_v4
*io
)
292 if (!io
->device_status
&& !io
->transport_status
&&
293 !io
->driver_status
) {
295 * Impossible, should not be called.
297 log_debug("%s: called with no error", __FUNCTION__
);
301 log_debug("%s: sg_io failed status 0x%x 0x%x 0x%x",
302 dev_scsi
->kernel
, io
->driver_status
, io
->transport_status
, io
->device_status
);
303 if (io
->device_status
== SCSI_CHECK_CONDITION
)
304 return scsi_dump_sense(udev
, dev_scsi
, (unsigned char *)(uintptr_t)io
->response
,
310 static int scsi_inquiry(struct udev
*udev
,
311 struct scsi_id_device
*dev_scsi
, int fd
,
312 unsigned char evpd
, unsigned char page
,
313 unsigned char *buf
, unsigned int buflen
)
315 unsigned char inq_cmd
[INQUIRY_CMDLEN
] =
316 { INQUIRY_CMD
, evpd
, page
, 0, buflen
, 0 };
317 unsigned char sense
[SENSE_BUFF_LEN
];
319 struct sg_io_v4 io_v4
;
320 struct sg_io_hdr io_hdr
;
321 int retry
= 3; /* rather random */
324 if (buflen
> SCSI_INQ_BUFF_LEN
) {
325 log_debug("buflen %d too long", buflen
);
330 if (dev_scsi
->use_sg
== 4) {
331 memzero(&io_v4
, sizeof(struct sg_io_v4
));
333 io_v4
.protocol
= BSG_PROTOCOL_SCSI
;
334 io_v4
.subprotocol
= BSG_SUB_PROTOCOL_SCSI_CMD
;
335 io_v4
.request_len
= sizeof(inq_cmd
);
336 io_v4
.request
= (uintptr_t)inq_cmd
;
337 io_v4
.max_response_len
= sizeof(sense
);
338 io_v4
.response
= (uintptr_t)sense
;
339 io_v4
.din_xfer_len
= buflen
;
340 io_v4
.din_xferp
= (uintptr_t)buf
;
341 io_buf
= (void *)&io_v4
;
343 memzero(&io_hdr
, sizeof(struct sg_io_hdr
));
344 io_hdr
.interface_id
= 'S';
345 io_hdr
.cmd_len
= sizeof(inq_cmd
);
346 io_hdr
.mx_sb_len
= sizeof(sense
);
347 io_hdr
.dxfer_direction
= SG_DXFER_FROM_DEV
;
348 io_hdr
.dxfer_len
= buflen
;
350 io_hdr
.cmdp
= inq_cmd
;
352 io_hdr
.timeout
= DEF_TIMEOUT
;
353 io_buf
= (void *)&io_hdr
;
356 retval
= ioctl(fd
, SG_IO
, io_buf
);
358 if (IN_SET(errno
, EINVAL
, ENOSYS
) && dev_scsi
->use_sg
== 4) {
359 dev_scsi
->use_sg
= 3;
362 log_debug_errno(errno
, "%s: ioctl failed: %m", dev_scsi
->kernel
);
366 if (dev_scsi
->use_sg
== 4)
367 retval
= sg_err_category4(udev
, io_buf
);
369 retval
= sg_err_category3(udev
, io_buf
);
372 case SG_ERR_CAT_NOTSUPPORTED
:
375 case SG_ERR_CAT_CLEAN
:
376 case SG_ERR_CAT_RECOVERED
:
381 if (dev_scsi
->use_sg
== 4)
382 retval
= scsi_dump_v4(udev
, dev_scsi
, io_buf
);
384 retval
= scsi_dump(udev
, dev_scsi
, io_buf
);
389 } else if (retval
> 0) {
397 log_debug("%s: Unable to get INQUIRY vpd %d page 0x%x.",
398 dev_scsi
->kernel
, evpd
, page
);
403 /* Get list of supported EVPD pages */
404 static int do_scsi_page0_inquiry(struct udev
*udev
,
405 struct scsi_id_device
*dev_scsi
, int fd
,
406 unsigned char *buffer
, unsigned int len
)
410 memzero(buffer
, len
);
411 retval
= scsi_inquiry(udev
, dev_scsi
, fd
, 1, 0x0, buffer
, len
);
415 if (buffer
[1] != 0) {
416 log_debug("%s: page 0 not available.", dev_scsi
->kernel
);
419 if (buffer
[3] > len
) {
420 log_debug("%s: page 0 buffer too long %d", dev_scsi
->kernel
, buffer
[3]);
425 * Following check is based on code once included in the 2.5.x
428 * Some ill behaved devices return the standard inquiry here
429 * rather than the evpd data, snoop the data to verify.
431 if (buffer
[3] > MODEL_LENGTH
) {
433 * If the vendor id appears in the page assume the page is
436 if (strneq((char *)&buffer
[VENDOR_LENGTH
], dev_scsi
->vendor
, VENDOR_LENGTH
)) {
437 log_debug("%s: invalid page0 data", dev_scsi
->kernel
);
445 * The caller checks that serial is long enough to include the vendor +
448 static int prepend_vendor_model(struct udev
*udev
,
449 struct scsi_id_device
*dev_scsi
, char *serial
)
453 strncpy(serial
, dev_scsi
->vendor
, VENDOR_LENGTH
);
454 strncat(serial
, dev_scsi
->model
, MODEL_LENGTH
);
455 ind
= strlen(serial
);
458 * This is not a complete check, since we are using strncat/cpy
459 * above, ind will never be too large.
461 if (ind
!= (VENDOR_LENGTH
+ MODEL_LENGTH
)) {
462 log_debug("%s: expected length %d, got length %d",
463 dev_scsi
->kernel
, (VENDOR_LENGTH
+ MODEL_LENGTH
), ind
);
470 * check_fill_0x83_id - check the page 0x83 id, if OK allocate and fill
473 static int check_fill_0x83_id(struct udev
*udev
,
474 struct scsi_id_device
*dev_scsi
,
475 unsigned char *page_83
,
476 const struct scsi_id_search_values
477 *id_search
, char *serial
, char *serial_short
,
478 int max_len
, char *wwn
,
479 char *wwn_vendor_extension
, char *tgpt_group
)
484 * ASSOCIATION must be with the device (value 0)
485 * or with the target port for SCSI_ID_TGTPORT
487 if ((page_83
[1] & 0x30) == 0x10) {
488 if (id_search
->id_type
!= SCSI_ID_TGTGROUP
)
490 } else if ((page_83
[1] & 0x30) != 0)
493 if ((page_83
[1] & 0x0f) != id_search
->id_type
)
497 * Possibly check NAA sub-type.
499 if ((id_search
->naa_type
!= SCSI_ID_NAA_DONT_CARE
) &&
500 (id_search
->naa_type
!= (page_83
[4] & 0xf0) >> 4))
504 * Check for matching code set - ASCII or BINARY.
506 if ((page_83
[0] & 0x0f) != id_search
->code_set
)
510 * page_83[3]: identifier length
513 if ((page_83
[0] & 0x0f) != SCSI_ID_ASCII
)
515 * If not ASCII, use two bytes for each binary value.
520 * Add one byte for the NUL termination, and one for the id_type.
523 if (id_search
->id_type
== SCSI_ID_VENDOR_SPECIFIC
)
524 len
+= VENDOR_LENGTH
+ MODEL_LENGTH
;
527 log_debug("%s: length %d too short - need %d",
528 dev_scsi
->kernel
, max_len
, len
);
532 if (id_search
->id_type
== SCSI_ID_TGTGROUP
&& tgpt_group
!= NULL
) {
535 group
= ((unsigned int)page_83
[6] << 8) | page_83
[7];
536 sprintf(tgpt_group
,"%x", group
);
540 serial
[0] = hex_str
[id_search
->id_type
];
543 * For SCSI_ID_VENDOR_SPECIFIC prepend the vendor and model before
544 * the id since it is not unique across all vendors and models,
545 * this differs from SCSI_ID_T10_VENDOR, where the vendor is
546 * included in the identifier.
548 if (id_search
->id_type
== SCSI_ID_VENDOR_SPECIFIC
)
549 if (prepend_vendor_model(udev
, dev_scsi
, &serial
[1]) < 0)
552 i
= 4; /* offset to the start of the identifier */
553 s
= j
= strlen(serial
);
554 if ((page_83
[0] & 0x0f) == SCSI_ID_ASCII
) {
558 while (i
< (4 + page_83
[3]))
559 serial
[j
++] = page_83
[i
++];
562 * Binary descriptor, convert to ASCII, using two bytes of
563 * ASCII for each byte in the page_83.
565 while (i
< (4 + page_83
[3])) {
566 serial
[j
++] = hex_str
[(page_83
[i
] & 0xf0) >> 4];
567 serial
[j
++] = hex_str
[page_83
[i
] & 0x0f];
572 strcpy(serial_short
, &serial
[s
]);
574 if (id_search
->id_type
== SCSI_ID_NAA
&& wwn
!= NULL
) {
575 strncpy(wwn
, &serial
[s
], 16);
576 if (wwn_vendor_extension
!= NULL
)
577 strncpy(wwn_vendor_extension
, &serial
[s
+ 16], 16);
583 /* Extract the raw binary from VPD 0x83 pre-SPC devices */
584 static int check_fill_0x83_prespc3(struct udev
*udev
,
585 struct scsi_id_device
*dev_scsi
,
586 unsigned char *page_83
,
587 const struct scsi_id_search_values
588 *id_search
, char *serial
, char *serial_short
, int max_len
)
592 serial
[0] = hex_str
[id_search
->id_type
];
593 /* serial has been memset to zero before */
594 j
= strlen(serial
); /* j = 1; */
596 for (i
= 0; (i
< page_83
[3]) && (j
< max_len
-3); ++i
) {
597 serial
[j
++] = hex_str
[(page_83
[4+i
] & 0xf0) >> 4];
598 serial
[j
++] = hex_str
[ page_83
[4+i
] & 0x0f];
600 serial
[max_len
-1] = 0;
601 strncpy(serial_short
, serial
, max_len
-1);
606 /* Get device identification VPD page */
607 static int do_scsi_page83_inquiry(struct udev
*udev
,
608 struct scsi_id_device
*dev_scsi
, int fd
,
609 char *serial
, char *serial_short
, int len
,
610 char *unit_serial_number
, char *wwn
,
611 char *wwn_vendor_extension
, char *tgpt_group
)
614 unsigned int id_ind
, j
;
615 unsigned char page_83
[SCSI_INQ_BUFF_LEN
];
617 /* also pick up the page 80 serial number */
618 do_scsi_page80_inquiry(udev
, dev_scsi
, fd
, NULL
, unit_serial_number
, MAX_SERIAL_LEN
);
620 memzero(page_83
, SCSI_INQ_BUFF_LEN
);
621 retval
= scsi_inquiry(udev
, dev_scsi
, fd
, 1, PAGE_83
, page_83
,
626 if (page_83
[1] != PAGE_83
) {
627 log_debug("%s: Invalid page 0x83", dev_scsi
->kernel
);
632 * XXX Some devices (IBM 3542) return all spaces for an identifier if
633 * the LUN is not actually configured. This leads to identifiers of
638 * Model 4, 5, and (some) model 6 EMC Symmetrix devices return
639 * a page 83 reply according to SCSI-2 format instead of SPC-2/3.
641 * The SCSI-2 page 83 format returns an IEEE WWN in binary
642 * encoded hexi-decimal in the 16 bytes following the initial
643 * 4-byte page 83 reply header.
645 * Both the SPC-2 and SPC-3 formats return an IEEE WWN as part
646 * of an Identification descriptor. The 3rd byte of the first
647 * Identification descriptor is a reserved (BSZ) byte field.
649 * Reference the 7th byte of the page 83 reply to determine
650 * whether the reply is compliant with SCSI-2 or SPC-2/3
651 * specifications. A zero value in the 7th byte indicates
652 * an SPC-2/3 conformant reply, (i.e., the reserved field of the
653 * first Identification descriptor). This byte will be non-zero
654 * for a SCSI-2 conformant page 83 reply from these EMC
655 * Symmetrix models since the 7th byte of the reply corresponds
656 * to the 4th and 5th nibbles of the 6-byte OUI for EMC, that is,
661 return check_fill_0x83_prespc3(udev
,
662 dev_scsi
, page_83
, id_search_list
,
663 serial
, serial_short
, len
);
666 * Search for a match in the prioritized id_search_list - since WWN ids
667 * come first we can pick up the WWN in check_fill_0x83_id().
670 id_ind
< sizeof(id_search_list
)/sizeof(id_search_list
[0]);
673 * Examine each descriptor returned. There is normally only
674 * one or a small number of descriptors.
676 for (j
= 4; j
<= (unsigned int)page_83
[3] + 3; j
+= page_83
[j
+ 3] + 4) {
677 retval
= check_fill_0x83_id(udev
,
678 dev_scsi
, &page_83
[j
],
679 &id_search_list
[id_ind
],
680 serial
, serial_short
, len
,
681 wwn
, wwn_vendor_extension
,
693 * Get device identification VPD page for older SCSI-2 device which is not
694 * compliant with either SPC-2 or SPC-3 format.
696 * Return the hard coded error code value 2 if the page 83 reply is not
697 * conformant to the SCSI-2 format.
699 static int do_scsi_page83_prespc3_inquiry(struct udev
*udev
,
700 struct scsi_id_device
*dev_scsi
, int fd
,
701 char *serial
, char *serial_short
, int len
)
705 unsigned char page_83
[SCSI_INQ_BUFF_LEN
];
707 memzero(page_83
, SCSI_INQ_BUFF_LEN
);
708 retval
= scsi_inquiry(udev
, dev_scsi
, fd
, 1, PAGE_83
, page_83
, SCSI_INQ_BUFF_LEN
);
712 if (page_83
[1] != PAGE_83
) {
713 log_debug("%s: Invalid page 0x83", dev_scsi
->kernel
);
717 * Model 4, 5, and (some) model 6 EMC Symmetrix devices return
718 * a page 83 reply according to SCSI-2 format instead of SPC-2/3.
720 * The SCSI-2 page 83 format returns an IEEE WWN in binary
721 * encoded hexi-decimal in the 16 bytes following the initial
722 * 4-byte page 83 reply header.
724 * Both the SPC-2 and SPC-3 formats return an IEEE WWN as part
725 * of an Identification descriptor. The 3rd byte of the first
726 * Identification descriptor is a reserved (BSZ) byte field.
728 * Reference the 7th byte of the page 83 reply to determine
729 * whether the reply is compliant with SCSI-2 or SPC-2/3
730 * specifications. A zero value in the 7th byte indicates
731 * an SPC-2/3 conformant reply, (i.e., the reserved field of the
732 * first Identification descriptor). This byte will be non-zero
733 * for a SCSI-2 conformant page 83 reply from these EMC
734 * Symmetrix models since the 7th byte of the reply corresponds
735 * to the 4th and 5th nibbles of the 6-byte OUI for EMC, that is,
741 serial
[0] = hex_str
[id_search_list
[0].id_type
];
743 * The first four bytes contain data, not a descriptor.
748 * Binary descriptor, convert to ASCII,
749 * using two bytes of ASCII for each byte
752 while (i
< (page_83
[3]+4)) {
753 serial
[j
++] = hex_str
[(page_83
[i
] & 0xf0) >> 4];
754 serial
[j
++] = hex_str
[page_83
[i
] & 0x0f];
760 /* Get unit serial number VPD page */
761 static int do_scsi_page80_inquiry(struct udev
*udev
,
762 struct scsi_id_device
*dev_scsi
, int fd
,
763 char *serial
, char *serial_short
, int max_len
)
769 unsigned char buf
[SCSI_INQ_BUFF_LEN
];
771 memzero(buf
, SCSI_INQ_BUFF_LEN
);
772 retval
= scsi_inquiry(udev
, dev_scsi
, fd
, 1, PAGE_80
, buf
, SCSI_INQ_BUFF_LEN
);
776 if (buf
[1] != PAGE_80
) {
777 log_debug("%s: Invalid page 0x80", dev_scsi
->kernel
);
781 len
= 1 + VENDOR_LENGTH
+ MODEL_LENGTH
+ buf
[3];
783 log_debug("%s: length %d too short - need %d",
784 dev_scsi
->kernel
, max_len
, len
);
788 * Prepend 'S' to avoid unlikely collision with page 0x83 vendor
789 * specific type where we prepend '0' + vendor + model.
792 if (serial
!= NULL
) {
794 ser_ind
= prepend_vendor_model(udev
, dev_scsi
, &serial
[1]);
797 ser_ind
++; /* for the leading 'S' */
798 for (i
= 4; i
< len
+ 4; i
++, ser_ind
++)
799 serial
[ser_ind
] = buf
[i
];
801 if (serial_short
!= NULL
) {
802 memcpy(serial_short
, &buf
[4], len
);
803 serial_short
[len
] = '\0';
808 int scsi_std_inquiry(struct udev
*udev
,
809 struct scsi_id_device
*dev_scsi
, const char *devname
)
812 unsigned char buf
[SCSI_INQ_BUFF_LEN
];
816 fd
= open(devname
, O_RDONLY
| O_NONBLOCK
| O_CLOEXEC
);
818 log_debug_errno(errno
, "scsi_id: cannot open %s: %m", devname
);
822 if (fstat(fd
, &statbuf
) < 0) {
823 log_debug_errno(errno
, "scsi_id: cannot stat %s: %m", devname
);
827 sprintf(dev_scsi
->kernel
,"%d:%d", major(statbuf
.st_rdev
),
828 minor(statbuf
.st_rdev
));
830 memzero(buf
, SCSI_INQ_BUFF_LEN
);
831 err
= scsi_inquiry(udev
, dev_scsi
, fd
, 0, 0, buf
, SCSI_INQ_BUFF_LEN
);
836 memcpy(dev_scsi
->vendor
, buf
+ 8, 8);
837 dev_scsi
->vendor
[8] = '\0';
838 memcpy(dev_scsi
->model
, buf
+ 16, 16);
839 dev_scsi
->model
[16] = '\0';
840 memcpy(dev_scsi
->revision
, buf
+ 32, 4);
841 dev_scsi
->revision
[4] = '\0';
842 sprintf(dev_scsi
->type
,"%x", buf
[0] & 0x1f);
849 int scsi_get_serial(struct udev
*udev
,
850 struct scsi_id_device
*dev_scsi
, const char *devname
,
851 int page_code
, int len
)
853 unsigned char page0
[SCSI_INQ_BUFF_LEN
];
859 memzero(dev_scsi
->serial
, len
);
861 for (cnt
= 20; cnt
> 0; cnt
--) {
862 struct timespec duration
;
864 fd
= open(devname
, O_RDONLY
| O_NONBLOCK
| O_CLOEXEC
);
865 if (fd
>= 0 || errno
!= EBUSY
)
868 duration
.tv_nsec
= (200 * 1000 * 1000) + (rand() % 100 * 1000 * 1000);
869 nanosleep(&duration
, NULL
);
874 if (page_code
== PAGE_80
) {
875 if (do_scsi_page80_inquiry(udev
, dev_scsi
, fd
, dev_scsi
->serial
, dev_scsi
->serial_short
, len
)) {
882 } else if (page_code
== PAGE_83
) {
883 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
)) {
890 } else if (page_code
== PAGE_83_PRE_SPC3
) {
891 retval
= do_scsi_page83_prespc3_inquiry(udev
, dev_scsi
, fd
, dev_scsi
->serial
, dev_scsi
->serial_short
, len
);
894 * Fallback to servicing a SPC-2/3 compliant page 83
895 * inquiry if the page 83 reply format does not
896 * conform to pre-SPC3 expectations.
899 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
)) {
915 } else if (page_code
!= 0x00) {
916 log_debug("%s: unsupported page code 0x%d", dev_scsi
->kernel
, page_code
);
922 * Get page 0, the page of the pages. By default, try from best to
923 * worst of supported pages: 0x83 then 0x80.
925 if (do_scsi_page0_inquiry(udev
, dev_scsi
, fd
, page0
, SCSI_INQ_BUFF_LEN
)) {
927 * Don't try anything else. Black list if a specific page
928 * should be used for this vendor+model, or maybe have an
929 * optional fall-back to page 0x80 or page 0x83.
935 for (ind
= 4; ind
<= page0
[3] + 3; ind
++)
936 if (page0
[ind
] == PAGE_83
)
937 if (!do_scsi_page83_inquiry(udev
, dev_scsi
, fd
,
938 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
)) {
946 for (ind
= 4; ind
<= page0
[3] + 3; ind
++)
947 if (page0
[ind
] == PAGE_80
)
948 if (!do_scsi_page80_inquiry(udev
, dev_scsi
, fd
,
949 dev_scsi
->serial
, dev_scsi
->serial_short
, len
)) {