]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/scsi_id/scsi_serial.c
tree-wide: use IN_SET macro (#6977)
[thirdparty/systemd.git] / src / udev / scsi_id / scsi_serial.c
1 /*
2 * Copyright (C) IBM Corp. 2003
3 *
4 * Author: Patrick Mansfield<patmans@us.ibm.com>
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <inttypes.h>
23 #include <linux/bsg.h>
24 #include <linux/types.h>
25 #include <scsi/scsi.h>
26 #include <scsi/sg.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sys/ioctl.h>
31 #include <sys/stat.h>
32 #include <sys/types.h>
33 #include <time.h>
34 #include <unistd.h>
35
36 #include "libudev.h"
37
38 #include "libudev-private.h"
39 #include "random-util.h"
40 #include "scsi.h"
41 #include "scsi_id.h"
42 #include "string-util.h"
43
44 /*
45 * A priority based list of id, naa, and binary/ascii for the identifier
46 * descriptor in VPD page 0x83.
47 *
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.
51 */
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 },
58 /*
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
65 * names.
66 */
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 },
75 };
76
77 static const char hex_str[]="0123456789abcdef";
78
79 /*
80 * Values returned in the result/status, only the ones used by the code
81 * are used here.
82 */
83
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 */
89
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 */
99
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);
103
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)
108 {
109 scsi_status &= 0x7e;
110
111 /*
112 * XXX change to return only two values - failed or OK.
113 */
114
115 if (!scsi_status && !host_status && !driver_status)
116 return SG_ERR_CAT_CLEAN;
117
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)) {
121 int sense_key;
122 unsigned char asc;
123
124 if (sense_buffer[0] & 0x2) {
125 sense_key = sense_buffer[1] & 0xf;
126 asc = sense_buffer[2];
127 } else {
128 sense_key = sense_buffer[2] & 0xf;
129 asc = (sb_len > 12) ? sense_buffer[12] : 0;
130 }
131
132 if (sense_key == RECOVERED_ERROR)
133 return SG_ERR_CAT_RECOVERED;
134 else if (sense_key == UNIT_ATTENTION) {
135 if (0x28 == asc)
136 return SG_ERR_CAT_MEDIA_CHANGED;
137 if (0x29 == asc)
138 return SG_ERR_CAT_RESET;
139 } else if (sense_key == ILLEGAL_REQUEST)
140 return SG_ERR_CAT_NOTSUPPORTED;
141 }
142 return SG_ERR_CAT_SENSE;
143 }
144 if (host_status) {
145 if (IN_SET(host_status, DID_NO_CONNECT, DID_BUS_BUSY, DID_TIME_OUT))
146 return SG_ERR_CAT_TIMEOUT;
147 }
148 if (driver_status) {
149 if (driver_status == DRIVER_TIMEOUT)
150 return SG_ERR_CAT_TIMEOUT;
151 }
152 return SG_ERR_CAT_OTHER;
153 }
154
155 static int sg_err_category3(struct udev *udev, struct sg_io_hdr *hp)
156 {
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);
161 }
162
163 static int sg_err_category4(struct udev *udev, struct sg_io_v4 *hp)
164 {
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,
168 hp->response_len);
169 }
170
171 static int scsi_dump_sense(struct udev *udev,
172 struct scsi_id_device *dev_scsi,
173 unsigned char *sense_buffer, int sb_len)
174 {
175 int s;
176 int code;
177 int sense_class;
178 int sense_key;
179 int asc, ascq;
180 #ifdef DUMP_SENSE
181 char out_buffer[256];
182 int i, j;
183 #endif
184
185 /*
186 * Figure out and print the sense key, asc and ascq.
187 *
188 * If you want to suppress these for a particular drive model, add
189 * a black list entry in the scsi_id config file.
190 *
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.
195 */
196
197 if (sb_len < 1) {
198 log_debug("%s: sense buffer empty", dev_scsi->kernel);
199 return -1;
200 }
201
202 sense_class = (sense_buffer[0] >> 4) & 0x07;
203 code = sense_buffer[0] & 0xf;
204
205 if (sense_class == 7) {
206 /*
207 * extended sense data.
208 */
209 s = sense_buffer[7] + 8;
210 if (sb_len < s) {
211 log_debug("%s: sense buffer too small %d bytes, %d bytes too short",
212 dev_scsi->kernel, sb_len, s - sb_len);
213 return -1;
214 }
215 if (IN_SET(code, 0x0, 0x1)) {
216 sense_key = sense_buffer[2] & 0xf;
217 if (s < 14) {
218 /*
219 * Possible?
220 */
221 log_debug("%s: sense result too" " small %d bytes",
222 dev_scsi->kernel, s);
223 return -1;
224 }
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];
231 } else {
232 log_debug("%s: invalid sense code 0x%x",
233 dev_scsi->kernel, code);
234 return -1;
235 }
236 log_debug("%s: sense key 0x%x ASC 0x%x ASCQ 0x%x",
237 dev_scsi->kernel, sense_key, asc, ascq);
238 } else {
239 if (sb_len < 4) {
240 log_debug("%s: sense buffer too small %d bytes, %d bytes too short",
241 dev_scsi->kernel, sb_len, 4 - sb_len);
242 return -1;
243 }
244
245 if (sense_buffer[0] < 15)
246 log_debug("%s: old sense key: 0x%x", dev_scsi->kernel, sense_buffer[0] & 0x0f);
247 else
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);
252
253 }
254
255 #ifdef DUMP_SENSE
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++] = ' ';
260 }
261 out_buffer[j] = '\0';
262 log_debug("%s: sense dump:", dev_scsi->kernel);
263 log_debug("%s: %s", dev_scsi->kernel, out_buffer);
264
265 #endif
266 return -1;
267 }
268
269 static int scsi_dump(struct udev *udev,
270 struct scsi_id_device *dev_scsi, struct sg_io_hdr *io)
271 {
272 if (!io->status && !io->host_status && !io->msg_status &&
273 !io->driver_status) {
274 /*
275 * Impossible, should not be called.
276 */
277 log_debug("%s: called with no error", __FUNCTION__);
278 return -1;
279 }
280
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);
285 else
286 return -1;
287 }
288
289 static int scsi_dump_v4(struct udev *udev,
290 struct scsi_id_device *dev_scsi, struct sg_io_v4 *io)
291 {
292 if (!io->device_status && !io->transport_status &&
293 !io->driver_status) {
294 /*
295 * Impossible, should not be called.
296 */
297 log_debug("%s: called with no error", __FUNCTION__);
298 return -1;
299 }
300
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,
305 io->response_len);
306 else
307 return -1;
308 }
309
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)
314 {
315 unsigned char inq_cmd[INQUIRY_CMDLEN] =
316 { INQUIRY_CMD, evpd, page, 0, buflen, 0 };
317 unsigned char sense[SENSE_BUFF_LEN];
318 void *io_buf;
319 struct sg_io_v4 io_v4;
320 struct sg_io_hdr io_hdr;
321 int retry = 3; /* rather random */
322 int retval;
323
324 if (buflen > SCSI_INQ_BUFF_LEN) {
325 log_debug("buflen %d too long", buflen);
326 return -1;
327 }
328
329 resend:
330 if (dev_scsi->use_sg == 4) {
331 memzero(&io_v4, sizeof(struct sg_io_v4));
332 io_v4.guard = 'Q';
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;
342 } else {
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;
349 io_hdr.dxferp = buf;
350 io_hdr.cmdp = inq_cmd;
351 io_hdr.sbp = sense;
352 io_hdr.timeout = DEF_TIMEOUT;
353 io_buf = (void *)&io_hdr;
354 }
355
356 retval = ioctl(fd, SG_IO, io_buf);
357 if (retval < 0) {
358 if (IN_SET(errno, EINVAL, ENOSYS) && dev_scsi->use_sg == 4) {
359 dev_scsi->use_sg = 3;
360 goto resend;
361 }
362 log_debug_errno(errno, "%s: ioctl failed: %m", dev_scsi->kernel);
363 goto error;
364 }
365
366 if (dev_scsi->use_sg == 4)
367 retval = sg_err_category4(udev, io_buf);
368 else
369 retval = sg_err_category3(udev, io_buf);
370
371 switch (retval) {
372 case SG_ERR_CAT_NOTSUPPORTED:
373 buf[1] = 0;
374 /* Fallthrough */
375 case SG_ERR_CAT_CLEAN:
376 case SG_ERR_CAT_RECOVERED:
377 retval = 0;
378 break;
379
380 default:
381 if (dev_scsi->use_sg == 4)
382 retval = scsi_dump_v4(udev, dev_scsi, io_buf);
383 else
384 retval = scsi_dump(udev, dev_scsi, io_buf);
385 }
386
387 if (!retval) {
388 retval = buflen;
389 } else if (retval > 0) {
390 if (--retry > 0)
391 goto resend;
392 retval = -1;
393 }
394
395 error:
396 if (retval < 0)
397 log_debug("%s: Unable to get INQUIRY vpd %d page 0x%x.",
398 dev_scsi->kernel, evpd, page);
399
400 return retval;
401 }
402
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)
407 {
408 int retval;
409
410 memzero(buffer, len);
411 retval = scsi_inquiry(udev, dev_scsi, fd, 1, 0x0, buffer, len);
412 if (retval < 0)
413 return 1;
414
415 if (buffer[1] != 0) {
416 log_debug("%s: page 0 not available.", dev_scsi->kernel);
417 return 1;
418 }
419 if (buffer[3] > len) {
420 log_debug("%s: page 0 buffer too long %d", dev_scsi->kernel, buffer[3]);
421 return 1;
422 }
423
424 /*
425 * Following check is based on code once included in the 2.5.x
426 * kernel.
427 *
428 * Some ill behaved devices return the standard inquiry here
429 * rather than the evpd data, snoop the data to verify.
430 */
431 if (buffer[3] > MODEL_LENGTH) {
432 /*
433 * If the vendor id appears in the page assume the page is
434 * invalid.
435 */
436 if (strneq((char *)&buffer[VENDOR_LENGTH], dev_scsi->vendor, VENDOR_LENGTH)) {
437 log_debug("%s: invalid page0 data", dev_scsi->kernel);
438 return 1;
439 }
440 }
441 return 0;
442 }
443
444 /*
445 * The caller checks that serial is long enough to include the vendor +
446 * model.
447 */
448 static int prepend_vendor_model(struct udev *udev,
449 struct scsi_id_device *dev_scsi, char *serial)
450 {
451 int ind;
452
453 strncpy(serial, dev_scsi->vendor, VENDOR_LENGTH);
454 strncat(serial, dev_scsi->model, MODEL_LENGTH);
455 ind = strlen(serial);
456
457 /*
458 * This is not a complete check, since we are using strncat/cpy
459 * above, ind will never be too large.
460 */
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);
464 return -1;
465 }
466 return ind;
467 }
468
469 /*
470 * check_fill_0x83_id - check the page 0x83 id, if OK allocate and fill
471 * serial number.
472 */
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)
480 {
481 int i, j, s, len;
482
483 /*
484 * ASSOCIATION must be with the device (value 0)
485 * or with the target port for SCSI_ID_TGTPORT
486 */
487 if ((page_83[1] & 0x30) == 0x10) {
488 if (id_search->id_type != SCSI_ID_TGTGROUP)
489 return 1;
490 } else if ((page_83[1] & 0x30) != 0)
491 return 1;
492
493 if ((page_83[1] & 0x0f) != id_search->id_type)
494 return 1;
495
496 /*
497 * Possibly check NAA sub-type.
498 */
499 if ((id_search->naa_type != SCSI_ID_NAA_DONT_CARE) &&
500 (id_search->naa_type != (page_83[4] & 0xf0) >> 4))
501 return 1;
502
503 /*
504 * Check for matching code set - ASCII or BINARY.
505 */
506 if ((page_83[0] & 0x0f) != id_search->code_set)
507 return 1;
508
509 /*
510 * page_83[3]: identifier length
511 */
512 len = page_83[3];
513 if ((page_83[0] & 0x0f) != SCSI_ID_ASCII)
514 /*
515 * If not ASCII, use two bytes for each binary value.
516 */
517 len *= 2;
518
519 /*
520 * Add one byte for the NUL termination, and one for the id_type.
521 */
522 len += 2;
523 if (id_search->id_type == SCSI_ID_VENDOR_SPECIFIC)
524 len += VENDOR_LENGTH + MODEL_LENGTH;
525
526 if (max_len < len) {
527 log_debug("%s: length %d too short - need %d",
528 dev_scsi->kernel, max_len, len);
529 return 1;
530 }
531
532 if (id_search->id_type == SCSI_ID_TGTGROUP && tgpt_group != NULL) {
533 unsigned int group;
534
535 group = ((unsigned int)page_83[6] << 8) | page_83[7];
536 sprintf(tgpt_group,"%x", group);
537 return 1;
538 }
539
540 serial[0] = hex_str[id_search->id_type];
541
542 /*
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.
547 */
548 if (id_search->id_type == SCSI_ID_VENDOR_SPECIFIC)
549 if (prepend_vendor_model(udev, dev_scsi, &serial[1]) < 0)
550 return 1;
551
552 i = 4; /* offset to the start of the identifier */
553 s = j = strlen(serial);
554 if ((page_83[0] & 0x0f) == SCSI_ID_ASCII) {
555 /*
556 * ASCII descriptor.
557 */
558 while (i < (4 + page_83[3]))
559 serial[j++] = page_83[i++];
560 } else {
561 /*
562 * Binary descriptor, convert to ASCII, using two bytes of
563 * ASCII for each byte in the page_83.
564 */
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];
568 i++;
569 }
570 }
571
572 strcpy(serial_short, &serial[s]);
573
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);
578 }
579
580 return 0;
581 }
582
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)
589 {
590 int i, j;
591
592 serial[0] = hex_str[id_search->id_type];
593 /* serial has been memset to zero before */
594 j = strlen(serial); /* j = 1; */
595
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];
599 }
600 serial[max_len-1] = 0;
601 strncpy(serial_short, serial, max_len-1);
602 return 0;
603 }
604
605
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)
612 {
613 int retval;
614 unsigned int id_ind, j;
615 unsigned char page_83[SCSI_INQ_BUFF_LEN];
616
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);
619
620 memzero(page_83, SCSI_INQ_BUFF_LEN);
621 retval = scsi_inquiry(udev, dev_scsi, fd, 1, PAGE_83, page_83,
622 SCSI_INQ_BUFF_LEN);
623 if (retval < 0)
624 return 1;
625
626 if (page_83[1] != PAGE_83) {
627 log_debug("%s: Invalid page 0x83", dev_scsi->kernel);
628 return 1;
629 }
630
631 /*
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
634 * the form: "1 ".
635 */
636
637 /*
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.
640 *
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.
644 *
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.
648 *
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,
657 * 0x006048.
658 */
659
660 if (page_83[6] != 0)
661 return check_fill_0x83_prespc3(udev,
662 dev_scsi, page_83, id_search_list,
663 serial, serial_short, len);
664
665 /*
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().
668 */
669 for (id_ind = 0;
670 id_ind < sizeof(id_search_list)/sizeof(id_search_list[0]);
671 id_ind++) {
672 /*
673 * Examine each descriptor returned. There is normally only
674 * one or a small number of descriptors.
675 */
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,
682 tgpt_group);
683 if (!retval)
684 return retval;
685 else if (retval < 0)
686 return retval;
687 }
688 }
689 return 1;
690 }
691
692 /*
693 * Get device identification VPD page for older SCSI-2 device which is not
694 * compliant with either SPC-2 or SPC-3 format.
695 *
696 * Return the hard coded error code value 2 if the page 83 reply is not
697 * conformant to the SCSI-2 format.
698 */
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)
702 {
703 int retval;
704 int i, j;
705 unsigned char page_83[SCSI_INQ_BUFF_LEN];
706
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);
709 if (retval < 0)
710 return 1;
711
712 if (page_83[1] != PAGE_83) {
713 log_debug("%s: Invalid page 0x83", dev_scsi->kernel);
714 return 1;
715 }
716 /*
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.
719 *
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.
723 *
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.
727 *
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,
736 * 0x006048.
737 */
738 if (page_83[6] == 0)
739 return 2;
740
741 serial[0] = hex_str[id_search_list[0].id_type];
742 /*
743 * The first four bytes contain data, not a descriptor.
744 */
745 i = 4;
746 j = strlen(serial);
747 /*
748 * Binary descriptor, convert to ASCII,
749 * using two bytes of ASCII for each byte
750 * in the page_83.
751 */
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];
755 i++;
756 }
757 return 0;
758 }
759
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)
764 {
765 int retval;
766 int ser_ind;
767 int i;
768 int len;
769 unsigned char buf[SCSI_INQ_BUFF_LEN];
770
771 memzero(buf, SCSI_INQ_BUFF_LEN);
772 retval = scsi_inquiry(udev, dev_scsi, fd, 1, PAGE_80, buf, SCSI_INQ_BUFF_LEN);
773 if (retval < 0)
774 return retval;
775
776 if (buf[1] != PAGE_80) {
777 log_debug("%s: Invalid page 0x80", dev_scsi->kernel);
778 return 1;
779 }
780
781 len = 1 + VENDOR_LENGTH + MODEL_LENGTH + buf[3];
782 if (max_len < len) {
783 log_debug("%s: length %d too short - need %d",
784 dev_scsi->kernel, max_len, len);
785 return 1;
786 }
787 /*
788 * Prepend 'S' to avoid unlikely collision with page 0x83 vendor
789 * specific type where we prepend '0' + vendor + model.
790 */
791 len = buf[3];
792 if (serial != NULL) {
793 serial[0] = 'S';
794 ser_ind = prepend_vendor_model(udev, dev_scsi, &serial[1]);
795 if (ser_ind < 0)
796 return 1;
797 ser_ind++; /* for the leading 'S' */
798 for (i = 4; i < len + 4; i++, ser_ind++)
799 serial[ser_ind] = buf[i];
800 }
801 if (serial_short != NULL) {
802 memcpy(serial_short, &buf[4], len);
803 serial_short[len] = '\0';
804 }
805 return 0;
806 }
807
808 int scsi_std_inquiry(struct udev *udev,
809 struct scsi_id_device *dev_scsi, const char *devname)
810 {
811 int fd;
812 unsigned char buf[SCSI_INQ_BUFF_LEN];
813 struct stat statbuf;
814 int err = 0;
815
816 fd = open(devname, O_RDONLY | O_NONBLOCK | O_CLOEXEC);
817 if (fd < 0) {
818 log_debug_errno(errno, "scsi_id: cannot open %s: %m", devname);
819 return 1;
820 }
821
822 if (fstat(fd, &statbuf) < 0) {
823 log_debug_errno(errno, "scsi_id: cannot stat %s: %m", devname);
824 err = 2;
825 goto out;
826 }
827 sprintf(dev_scsi->kernel,"%d:%d", major(statbuf.st_rdev),
828 minor(statbuf.st_rdev));
829
830 memzero(buf, SCSI_INQ_BUFF_LEN);
831 err = scsi_inquiry(udev, dev_scsi, fd, 0, 0, buf, SCSI_INQ_BUFF_LEN);
832 if (err < 0)
833 goto out;
834
835 err = 0;
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);
843
844 out:
845 close(fd);
846 return err;
847 }
848
849 int scsi_get_serial(struct udev *udev,
850 struct scsi_id_device *dev_scsi, const char *devname,
851 int page_code, int len)
852 {
853 unsigned char page0[SCSI_INQ_BUFF_LEN];
854 int fd = -1;
855 int cnt;
856 int ind;
857 int retval;
858
859 memzero(dev_scsi->serial, len);
860 initialize_srand();
861 for (cnt = 20; cnt > 0; cnt--) {
862 struct timespec duration;
863
864 fd = open(devname, O_RDONLY | O_NONBLOCK | O_CLOEXEC);
865 if (fd >= 0 || errno != EBUSY)
866 break;
867 duration.tv_sec = 0;
868 duration.tv_nsec = (200 * 1000 * 1000) + (rand() % 100 * 1000 * 1000);
869 nanosleep(&duration, NULL);
870 }
871 if (fd < 0)
872 return 1;
873
874 if (page_code == PAGE_80) {
875 if (do_scsi_page80_inquiry(udev, dev_scsi, fd, dev_scsi->serial, dev_scsi->serial_short, len)) {
876 retval = 1;
877 goto completed;
878 } else {
879 retval = 0;
880 goto completed;
881 }
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)) {
884 retval = 1;
885 goto completed;
886 } else {
887 retval = 0;
888 goto completed;
889 }
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);
892 if (retval) {
893 /*
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.
897 */
898 if (retval == 2) {
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)) {
900 retval = 1;
901 goto completed;
902 } else {
903 retval = 0;
904 goto completed;
905 }
906 }
907 else {
908 retval = 1;
909 goto completed;
910 }
911 } else {
912 retval = 0;
913 goto completed;
914 }
915 } else if (page_code != 0x00) {
916 log_debug("%s: unsupported page code 0x%d", dev_scsi->kernel, page_code);
917 retval = 1;
918 goto completed;
919 }
920
921 /*
922 * Get page 0, the page of the pages. By default, try from best to
923 * worst of supported pages: 0x83 then 0x80.
924 */
925 if (do_scsi_page0_inquiry(udev, dev_scsi, fd, page0, SCSI_INQ_BUFF_LEN)) {
926 /*
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.
930 */
931 retval = 1;
932 goto completed;
933 }
934
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)) {
939 /*
940 * Success
941 */
942 retval = 0;
943 goto completed;
944 }
945
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)) {
950 /*
951 * Success
952 */
953 retval = 0;
954 goto completed;
955 }
956 retval = 1;
957
958 completed:
959 close(fd);
960 return retval;
961 }