chiark / gitweb /
[PATCH] add scsi_id "extra" program from Patrick Mansfield <patmans@us.ibm.com>
[elogind.git] / extras / scsi_id / scsi_serial.c
1 /*
2  * scsi_serial.c
3  *
4  * Code related to requesting and getting an id from a scsi device
5  *
6  * Copyright (C) IBM Corp. 2003
7  *
8  *  This library is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU Lesser General Public License as
10  *  published by the Free Software Foundation; either version 2.1 of the
11  *  License, or (at your option) any later version.
12  *
13  *  This library is distributed in the hope that it will be useful, but
14  *  WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  *  Lesser General Public License for more details.
17  *
18  *  You should have received a copy of the GNU Lesser General Public
19  *  License along with this library; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21  *  USA
22  */
23
24 #include <sys/types.h>
25 #include <sys/ioctl.h>
26 #include <stdio.h>
27 #include <errno.h>
28 #include <string.h>
29 #include <fcntl.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <syslog.h>
33 #include <scsi/sg.h>
34 #include <sys/libsysfs.h>
35 #include "scsi_id.h"
36 #include "scsi.h"
37
38 /*
39  * A priority based list of id, naa, and binary/ascii for the identifier
40  * descriptor in VPD page 0x83.
41  *
42  * Brute force search for a match starting with the first value in the
43  * following id_search_list. This is not a performance issue, since there
44  * is normally one or some small number of descriptors.
45  */
46 static const struct scsi_id_search_values id_search_list[] = {
47         { SCSI_ID_NAA,  SCSI_ID_NAA_IEEE_REG_EXTENDED,  SCSI_ID_BINARY },
48         { SCSI_ID_NAA,  SCSI_ID_NAA_IEEE_REG_EXTENDED,  SCSI_ID_ASCII },
49         { SCSI_ID_NAA,  SCSI_ID_NAA_IEEE_REG,   SCSI_ID_BINARY },
50         { SCSI_ID_NAA,  SCSI_ID_NAA_IEEE_REG,   SCSI_ID_ASCII },
51         /*
52          * Devices already exist using NAA values that are now marked
53          * reserved. These should not conflict with other values, or it is
54          * a bug in the device. As long as we find the IEEE extended one
55          * first, we really don't care what other ones are used. Using
56          * don't care here means that a device that returns multiple
57          * non-IEEE descriptors in a random order will get different
58          * names.
59          */
60         { SCSI_ID_NAA,  SCSI_ID_NAA_DONT_CARE,  SCSI_ID_BINARY },
61         { SCSI_ID_NAA,  SCSI_ID_NAA_DONT_CARE,  SCSI_ID_ASCII },
62         { SCSI_ID_EUI_64,       SCSI_ID_NAA_DONT_CARE,  SCSI_ID_BINARY },
63         { SCSI_ID_EUI_64,       SCSI_ID_NAA_DONT_CARE,  SCSI_ID_ASCII },
64         { SCSI_ID_T10_VENDOR,   SCSI_ID_NAA_DONT_CARE,  SCSI_ID_BINARY },
65         { SCSI_ID_T10_VENDOR,   SCSI_ID_NAA_DONT_CARE,  SCSI_ID_ASCII },
66         { SCSI_ID_VENDOR_SPECIFIC,      SCSI_ID_NAA_DONT_CARE,  SCSI_ID_BINARY },
67         { SCSI_ID_VENDOR_SPECIFIC,      SCSI_ID_NAA_DONT_CARE,  SCSI_ID_ASCII },
68 };
69
70 static const char hex_str[]="0123456789abcdef";
71
72 /*
73  * XXX maybe move all these to an sg_io.c file.
74  *
75  * From here ...
76  */
77
78 /*
79  * Values returned in the result/status, only the ones used by the code
80  * are used here.
81  */
82
83 #define DID_NO_CONNECT 0x01     /* Unable to connect before timeout */
84
85 #define DID_BUS_BUSY 0x02       /* Bus remain busy until timeout */
86 #define DID_TIME_OUT 0x03       /* Timed out for some other reason */
87
88 #define DRIVER_TIMEOUT 0x06
89 #define DRIVER_SENSE 0x08       /* Sense_buffer has been set */
90
91 /* The following "category" function returns one of the following */
92 #define SG_ERR_CAT_CLEAN        0      /* No errors or other information */
93 #define SG_ERR_CAT_MEDIA_CHANGED        1 /* interpreted from sense buffer */
94 #define SG_ERR_CAT_RESET        2      /* interpreted from sense buffer */
95 #define SG_ERR_CAT_TIMEOUT      3
96 #define SG_ERR_CAT_RECOVERED    4  /* Successful command after recovered err */
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 sg_err_category_new(int scsi_status, int msg_status, int
101                                host_status, int driver_status, const
102                                unsigned char *sense_buffer, int sb_len)
103 {
104         scsi_status &= 0x7e;
105
106         /*
107          * XXX change to return only two values - failed or OK.
108          */
109
110         /*
111          * checks msg_status
112          */
113         if (!scsi_status && !msg_status && !host_status && !driver_status)
114                 return SG_ERR_CAT_CLEAN;
115
116         if ((scsi_status == SCSI_CHECK_CONDITION) ||
117             (scsi_status == SCSI_COMMAND_TERMINATED) ||
118             ((driver_status & 0xf) == DRIVER_SENSE)) {
119                 if (sense_buffer && (sb_len > 2)) {
120                         int sense_key;
121                         unsigned char asc;
122
123                         if (sense_buffer[0] & 0x2) {
124                                 sense_key = sense_buffer[1] & 0xf;
125                                 asc = sense_buffer[2];
126                         } else {
127                                 sense_key = sense_buffer[2] & 0xf;
128                                 asc = (sb_len > 12) ? sense_buffer[12] : 0;
129                         }
130
131                         if (sense_key == RECOVERED_ERROR)
132                                 return SG_ERR_CAT_RECOVERED;
133                         else if (sense_key == UNIT_ATTENTION) {
134                                 if (0x28 == asc)
135                                         return SG_ERR_CAT_MEDIA_CHANGED;
136                                 if (0x29 == asc)
137                                         return SG_ERR_CAT_RESET;
138                         }
139                 }
140                 return SG_ERR_CAT_SENSE;
141         }
142         if (!host_status) {
143                 if ((host_status == DID_NO_CONNECT) ||
144                     (host_status == DID_BUS_BUSY) ||
145                     (host_status == 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 sg_io_hdr *hp)
156 {
157         return sg_err_category_new(hp->status, hp->msg_status,
158                                    hp->host_status, hp->driver_status,
159                                    hp->sbp, hp->sb_len_wr);
160 }
161
162 static int scsi_dump_sense(struct sysfs_class_device *scsi_dev,
163                            struct sg_io_hdr *io)
164 {
165         unsigned char *sense_buffer;
166         int s;
167         int sb_len;
168         int code;
169         int sense_class;
170         int sense_key;
171         int descriptor_format;
172         int asc, ascq;
173 #ifdef DUMP_SENSE
174         char out_buffer[256];
175         int i, j;
176 #endif
177
178         /*
179          * Figure out and print the sense key, asc and ascq.
180          *
181          * If you want to suppress these for a particular drive model, add
182          * a black list entry in the scsi_id config file.
183          *
184          * XXX We probably need to: lookup the sense/asc/ascq in a retry
185          * table, and if found return 1 (after dumping the sense, asc, and
186          * ascq). So, if/when we get something like a power on/reset,
187          * we'll retry the command.
188          */
189
190         dprintf("got check condition\n");
191
192         sb_len = io->sb_len_wr;
193         if (sb_len < 1) {
194                 log_message(LOG_WARNING, "%s: sense buffer empty\n",
195                             scsi_dev->name);
196                 return -1;
197         }
198
199         sense_buffer = io->sbp;
200         sense_class = (sense_buffer[0] >> 4) & 0x07;
201         code = sense_buffer[0] & 0xf;
202
203         if (sense_class == 7) {
204                 /*
205                  * extended sense data.
206                  */
207                 s = sense_buffer[7] + 8;
208                 if (sb_len < s) {
209                         log_message(LOG_WARNING,
210                                     "%s: sense buffer too small %d bytes,"
211                                     " %d bytes too short\n", scsi_dev->name,
212                                     sb_len, s - sb_len);
213                         return -1;
214                 }
215                 if ((code == 0x0) || (code == 0x1)) {
216                         descriptor_format = 0;
217                         sense_key = sense_buffer[2] & 0xf;
218                         if (s < 14) {
219                                 /*
220                                  * Possible?
221                                  */
222                                 log_message(LOG_WARNING, "%s: sense result too"
223                                             " small %d bytes\n",
224                                             scsi_dev->name, s);
225                                 return -1;
226                         }
227                         asc = sense_buffer[12];
228                         ascq = sense_buffer[13];
229                 } else if ((code == 0x2) || (code == 0x3)) {
230                         descriptor_format = 1;
231                         sense_key = sense_buffer[1] & 0xf;
232                         asc = sense_buffer[2];
233                         ascq = sense_buffer[3];
234                 } else {
235                         log_message(LOG_WARNING,
236                                     "%s: invalid sense code 0x%x\n",
237                                     scsi_dev->name, code);
238                         return -1;
239                 }
240                 log_message(LOG_WARNING,
241                             "%s: sense key 0x%x ASC 0x%x ASCQ 0x%x\n",
242                             scsi_dev->name, sense_key, asc, ascq);
243         } else {
244                 if (sb_len < 4) {
245                         log_message(LOG_WARNING,
246                                     "%s: sense buffer too small %d bytes, %d bytes too short\n",
247                                     scsi_dev->name, sb_len, 4 - sb_len);
248                         return -1;
249                 }
250
251                 if (sense_buffer[0] < 15)
252                         log_message(LOG_WARNING, "%s: old sense key: 0x%x\n",
253                                     scsi_dev->name, sense_buffer[0] & 0x0f);
254                 else
255                         log_message(LOG_WARNING, "%s: sense = %2x %2x\n",
256                                     scsi_dev->name,  sense_buffer[0],
257                                     sense_buffer[2]);
258                 log_message(LOG_WARNING,
259                             "%s: non-extended sense class %d code 0x%0x ",
260                             scsi_dev->name, sense_class, code);
261
262         }
263
264 #ifdef DUMP_SENSE
265         for (i = 0, j = 0; (i < s) && (j < 254); i++) {
266                 dprintf("i %d, j %d\n", i, j);
267                 out_buffer[j++] = hex_str[(sense_buffer[i] & 0xf0) >> 4];
268                 out_buffer[j++] = hex_str[sense_buffer[i] & 0x0f];
269                 out_buffer[j++] = ' ';
270         }
271         out_buffer[j] = '\0';
272         log_message(LOG_WARNING, "%s: sense dump:\n", scsi_dev->name);
273         log_message(LOG_WARNING, "%s: %s\n", scsi_dev->name, out_buffer);
274
275 #endif
276         return -1;
277 }
278
279 static int scsi_dump(struct sysfs_class_device *scsi_dev, struct sg_io_hdr *io)
280 {
281         if (!io->status && !io->host_status && !io->msg_status &&
282             !io->driver_status) {
283                 /*
284                  * Impossible, should not be called.
285                  */
286                 log_message(LOG_WARNING, "%s: called with no error\n",
287                             __FUNCTION__);
288                 return -1;
289         }
290
291         log_message(LOG_WARNING, "%s: sg_io failed status 0x%x 0x%x 0x%x 0x%x\n",
292                     scsi_dev->name, io->driver_status, io->host_status,
293                     io->msg_status, io->status);
294         if (io->status == SCSI_CHECK_CONDITION)
295                 return scsi_dump_sense(scsi_dev, io);
296         else
297                 return -1;
298 }
299
300 static int scsi_inquiry(struct sysfs_class_device *scsi_dev, int fd,
301                         unsigned char evpd, unsigned char page, unsigned
302                         char *buf, unsigned int buflen)
303 {
304         unsigned char inq_cmd[INQUIRY_CMDLEN] =
305                 { INQUIRY_CMD, evpd, page, 0, buflen, 0 };
306         unsigned char sense[SENSE_BUFF_LEN];
307         struct sg_io_hdr io_hdr;
308         int retval;
309         unsigned char *inq;
310         unsigned char *buffer;
311         int retry = 3; /* rather random */
312
313         if (buflen > 255) {
314                 log_message(LOG_WARNING, "buflen %d too long\n", buflen);
315                 return -1;
316         }
317         inq = malloc(OFFSET + sizeof (inq_cmd) + 512);
318         memset(inq, 0, OFFSET + sizeof (inq_cmd) + 512);
319         buffer = inq + OFFSET;
320
321 resend:
322         memset(&io_hdr, 0, sizeof(struct sg_io_hdr));
323         io_hdr.interface_id = 'S';
324         io_hdr.cmd_len = sizeof(inq_cmd);
325         io_hdr.mx_sb_len = sizeof(sense);
326         io_hdr.dxfer_direction = SG_DXFER_FROM_DEV;
327         io_hdr.dxfer_len = buflen;
328         io_hdr.dxferp = buffer;
329         io_hdr.cmdp = inq_cmd;
330         io_hdr.sbp = sense;
331         io_hdr.timeout = DEF_TIMEOUT;
332
333         if (ioctl(fd, SG_IO, &io_hdr) < 0) {
334                 log_message(LOG_WARNING, "%s ioctl failed: %s\n",
335                             scsi_dev->name, strerror(errno));
336                 return -1;
337         }
338
339         retval = sg_err_category3(&io_hdr);
340
341         switch (retval) {
342                 case SG_ERR_CAT_CLEAN:
343                 case SG_ERR_CAT_RECOVERED:
344                         retval = 0;
345                         break;
346
347                 default:
348                         retval = scsi_dump(scsi_dev, &io_hdr);
349         }
350
351         if (!retval) {
352                 retval = buflen;
353                 memcpy(buf, buffer, retval);
354         } else if (retval > 0) {
355                 if (--retry > 0) {
356                         dprintf("%s: Retrying ...\n", scsi_dev->name);
357                         goto resend;
358                 }
359                 retval = -1;
360         }
361
362         free(inq);
363         return retval;
364 }
365
366 /*
367  * XXX maybe move all these to an sg_io.c file.
368  *
369  * Ending here.
370  */
371
372 int do_scsi_page0_inquiry(struct sysfs_class_device *scsi_dev, int fd,
373                           char *buffer, int len)
374 {
375         int retval;
376         char *vendor;
377
378         memset(buffer, 0, len);
379         retval = scsi_inquiry(scsi_dev, fd, 1, 0x0, buffer, len);
380         if (retval < 0)
381                 return 1;
382
383         if (buffer[1] != 0) {
384                 log_message(LOG_WARNING, "%s: page 0 not available.\n",
385                             scsi_dev->name);
386                 return 1;
387         }
388         if (buffer[3] > len) {
389                 log_message(LOG_WARNING, "%s: page 0 buffer too long %d",
390                            scsi_dev->name,  buffer[3]);
391                 return 1;
392         }
393
394         /*
395          * Following check is based on code once included in the 2.5.x
396          * kernel.
397          *
398          * Some ill behaved devices return the standard inquiry here
399          * rather than the evpd data, snoop the data to verify.
400          */
401         if (buffer[3] > MODEL_LENGTH) {
402                 /*
403                  * If the vendor id appears in the page assume the page is
404                  * invalid.
405                  */
406                 vendor = sysfs_get_attr(scsi_dev, "vendor");
407                 if (!vendor) {
408                         log_message(LOG_WARNING, "%s: no vendor attribute\n",
409                                     scsi_dev->name);
410                         return 1;
411                 }
412                 if (!strncmp(&buffer[VENDOR_LENGTH], vendor, VENDOR_LENGTH)) {
413                         log_message(LOG_WARNING, "%s invalid page0 data\n",
414                                     scsi_dev->name);
415                         return 1;
416                 }
417         }
418         return 0;
419 }
420
421 /*
422  * The caller checks that serial is long enough to include the vendor +
423  * model.
424  */
425 static int prepend_vendor_model(struct sysfs_class_device *scsi_dev,
426                                 char *serial)
427 {
428         char *attr;
429         int ind;
430
431         attr = sysfs_get_attr(scsi_dev, "vendor");
432         if (!attr) {
433                 log_message(LOG_WARNING, "%s: no vendor attribute\n",
434                             scsi_dev->name);
435                 return 1;
436         }
437         strncpy(serial, attr, VENDOR_LENGTH);
438         ind = strlen(serial) - 1;
439         /*
440          * Remove sysfs added newlines.
441          */
442         if (serial[ind] == '\n')
443                 serial[ind] = '\0';
444
445         attr = sysfs_get_attr(scsi_dev, "model");
446         if (!attr) {
447                 log_message(LOG_WARNING, "%s: no model attribute\n",
448                             scsi_dev->name);
449                 return 1;
450         }
451         strncat(serial, attr, MODEL_LENGTH);
452         ind = strlen(serial) - 1;
453         if (serial[ind] == '\n')
454                 serial[ind] = '\0';
455         else
456                 ind++;
457
458         /*
459          * This is not a complete check, since we are using strncat/cpy
460          * above, ind will never be too large.
461          */
462         if (ind != (VENDOR_LENGTH + MODEL_LENGTH)) {
463                 log_message(LOG_WARNING, "%s: expected length %d, got length %d\n",
464                             scsi_dev->name, (VENDOR_LENGTH + MODEL_LENGTH),
465                             ind);
466                 return 1;
467         }
468         return ind;
469 }
470
471 /**
472  * check_fill_0x83_id - check the page 0x83 id, if OK allocate and fill
473  * serial number.
474  **/
475 static int check_fill_0x83_id(struct sysfs_class_device *scsi_dev,
476                               char *page_83,
477                               const struct scsi_id_search_values *id_search,
478                               char *serial, int max_len)
479 {
480         int i, j, len;
481
482         /*
483          * ASSOCIATION must be with the device (value 0)
484          */
485         if ((page_83[1] & 0x30) != 0)
486                 return 1;
487
488         if ((page_83[1] & 0x0f) != id_search->id_type)
489                 return 1;
490
491         /*
492          * Possibly check NAA sub-type.
493          */
494         if ((id_search->naa_type != SCSI_ID_NAA_DONT_CARE) &&
495             (id_search->naa_type != (page_83[4] & 0xf0) >> 4))
496                 return 1;
497
498         /*
499          * Check for matching code set - ASCII or BINARY.
500          */
501         if ((page_83[0] & 0x0f) != id_search->code_set)
502                 return 1;
503
504         /*
505          * page_83[3]: identifier length
506          */
507         len = page_83[3];
508         if ((page_83[0] & 0x0f) != SCSI_ID_ASCII)
509                 /*
510                  * If not ASCII, use two bytes for each binary value.
511                  */
512                 len *= 2;
513
514         /*
515          * Add one byte for the NUL termination, and one for the id_type.
516          */
517         len += 2;
518         if (id_search->id_type == SCSI_ID_VENDOR_SPECIFIC)
519                 len += VENDOR_LENGTH + MODEL_LENGTH;
520
521         if (max_len < len) {
522                 log_message(LOG_WARNING, "%s: length %d too short - need %d\n",
523                             scsi_dev->name, max_len, len);
524                 return 1;
525         }
526
527         serial[0] = hex_str[id_search->id_type];
528
529         /*
530          * Prepend the vendor and model before the id since if it is not
531          * unique across all vendors and models.
532          */
533         if (id_search->id_type == SCSI_ID_VENDOR_SPECIFIC)
534                 if (prepend_vendor_model(scsi_dev, &serial[1]) < 0) {
535                         dprintf("prepend failed\n");
536                         return 1;
537                 }
538
539         i = 4; /* offset to the start of the identifier */
540         j = strlen(serial);
541         if ((page_83[0] & 0x0f) == SCSI_ID_ASCII) {
542                 /*
543                  * ASCII descriptor.
544                  */
545                 while (i < (4 + page_83[3]))
546                         serial[j++] = page_83[i++];
547         } else {
548                 /*
549                  * Binary descriptor, convert to ASCII, using two bytes of
550                  * ASCII for each byte in the page_83.
551                  */
552                 while (i < (4 + page_83[3])) {
553                         serial[j++] = hex_str[(page_83[i] & 0xf0) >> 4];
554                         serial[j++] = hex_str[page_83[i] & 0x0f];
555                         i++;
556                 }
557         }
558         return 0;
559 }
560
561 static int do_scsi_page83_inquiry(struct sysfs_class_device *scsi_dev, int fd,
562                                   char *serial, int len)
563 {
564         int retval;
565         int id_ind, j;
566         unsigned char page_83[256];
567
568         memset(page_83, 0, 256);
569         retval = scsi_inquiry(scsi_dev, fd, 1, 0x83, page_83, 255);
570         if (retval < 0)
571                 return 1;
572
573         if (page_83[1] != 0x83) {
574                 log_message(LOG_WARNING, "%s: Invalid page 0x83\n",
575                             scsi_dev->name);
576                 return 1;
577         }
578
579         /*
580          * Search for a match in the prioritized id_search_list.
581          */
582         for (id_ind = 0;
583              id_ind < sizeof(id_search_list)/sizeof(id_search_list[0]);
584              id_ind++) {
585                 /*
586                  * Examine each descriptor returned. There is normally only
587                  * one or a small number of descriptors.
588                  */
589                 for (j = 4; j <= page_83[3] + 3;
590                         j += page_83[j + 3] + 4) {
591                         retval = check_fill_0x83_id(scsi_dev, &page_83[j],
592                                                     &id_search_list[id_ind],
593                                                     serial, len);
594                         dprintf("%s id desc %d/%d/%d\n", scsi_dev->name,
595                                 id_search_list[id_ind].id_type,
596                                 id_search_list[id_ind].naa_type,
597                                 id_search_list[id_ind].code_set);
598                         if (!retval) {
599                                 dprintf("       used\n");
600                                 return retval;
601                         } else if (retval < 0) {
602                                 dprintf("       failed\n");
603                                 return retval;
604                         } else {
605                                 dprintf("       not used\n");
606                         }
607                 }
608         }
609         return 1;
610 }
611
612 int do_scsi_page80_inquiry(struct sysfs_class_device *scsi_dev, int fd,
613                            char *serial, int max_len)
614 {
615         int retval;
616         int ser_ind;
617         int i;
618         int len;
619         unsigned char buf[256];
620
621         memset(buf, 0, 256);
622         retval = scsi_inquiry(scsi_dev, fd, 1, 0x80, buf, 255);
623         if (retval < 0)
624                 return retval;
625
626         if (buf[1] != 0x80) {
627                 log_message(LOG_WARNING, "%s: Invalid page 0x80\n",
628                             scsi_dev->name);
629                 return 1;
630         }
631
632         len = 1 + VENDOR_LENGTH + MODEL_LENGTH + buf[3];
633         if (max_len < len) {
634                 log_message(LOG_WARNING, "%s: length %d too short - need %d\n",
635                             scsi_dev->name, max_len, len);
636                 return 1;
637         }
638         /*
639          * Prepend 'S' to avoid unlikely collision with page 0x83 vendor
640          * specific type where we prepend '0' + vendor + model.
641          */
642         serial[0] = 'S';
643         ser_ind = prepend_vendor_model(scsi_dev, &serial[1]);
644         if (ser_ind < 0)
645                 return 1;
646         len = buf[3];
647         for (i = 4; i < len + 4; i++, ser_ind++)
648                 serial[ser_ind] = buf[i];
649         return 0;
650 }
651
652 int scsi_get_serial (struct sysfs_class_device *scsi_dev, const char *devname,
653                      int page_code, char *serial, int len)
654 {
655         unsigned char page0[256];
656         int fd;
657         int ind;
658         int retval;
659
660         if (len > 255) {
661         }
662         memset(serial, 0, len);
663         dprintf("opening %s\n", devname);
664         fd = open(devname, O_RDONLY);
665         if (fd < 0) {
666                 log_message(LOG_WARNING, "%s cannot open %s: %s\n",
667                             scsi_dev->name, devname, strerror(errno));
668                 return 1;
669         }
670
671         if (page_code == 0x80) {
672                 if (do_scsi_page80_inquiry(scsi_dev, fd, serial, len)) {
673                         retval = 1;
674                         goto completed;
675                 } else  {
676                         retval = 0;
677                         goto completed;
678                 }
679         } else if (page_code == 0x83) {
680                 if (do_scsi_page83_inquiry(scsi_dev, fd, serial, len)) {
681                         retval = 1;
682                         goto completed;
683                 } else  {
684                         retval = 0;
685                         goto completed;
686                 }
687         } else if (page_code != 0x00) {
688                 log_message(LOG_WARNING, "%s unsupported page code 0x%d\n",
689                             scsi_dev->name, page_code);
690                 return 1;
691         }
692
693         /*
694          * Get page 0, the page of the pages. By default, try from best to
695          * worst of supported pages: 0x83 then 0x80.
696          */
697         if (do_scsi_page0_inquiry(scsi_dev, fd, page0, 255)) {
698                 /*
699                  * Don't try anything else. Black list if a specific page
700                  * should be used for this vendor+model, or maybe have an
701                  * optional fall-back to page 0x80 or page 0x83.
702                  */
703                 retval = 1;
704                 goto completed;
705         }
706
707         dprintf("%s: Checking page0\n", scsi_dev->name);
708
709         for (ind = 4; ind <= page0[3] + 3; ind++)
710                 if (page0[ind] == 0x83)
711                         if (!do_scsi_page83_inquiry(scsi_dev, fd, serial,
712                                                     len)) {
713                                 /*
714                                  * Success
715                                  */
716                                 retval = 0;
717                                 goto completed;
718                         }
719
720         for (ind = 4; ind <= page0[3] + 3; ind++)
721                 if (page0[ind] == 0x80)
722                         if (!do_scsi_page80_inquiry(scsi_dev, fd, serial,
723                                                     len)) {
724                                 /*
725                                  * Success
726                                  */
727                                 retval = 0;
728                                 goto completed;
729                         }
730         retval = 1;
731 completed:
732         if (close(fd) < 0)
733                 log_message(LOG_WARNING, "close failed: %s", strerror(errno));
734         return retval;
735 }