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