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