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