X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=extras%2Fscsi_id%2Fscsi_serial.c;h=28e82db3ca8e0dca73ad3f9179f8c45766450876;hp=8b9fbe06814c0f8cc531b9720018322336aee38c;hb=c70560feef0eb61a150cd2f956f0beead4313ffe;hpb=1134a81b98e189a8ec99a1ea4aba8e52b358873b diff --git a/extras/scsi_id/scsi_serial.c b/extras/scsi_id/scsi_serial.c index 8b9fbe068..28e82db3c 100644 --- a/extras/scsi_id/scsi_serial.c +++ b/extras/scsi_id/scsi_serial.c @@ -5,20 +5,12 @@ * * Copyright (C) IBM Corp. 2003 * - * This library is free software; you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation; either version 2.1 of the - * License, or (at your option) any later version. + * Author: + * Patrick Mansfield * - * This library is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 - * USA + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation version 2 of the License. */ #include @@ -30,11 +22,12 @@ #include #include #include -#include /* need __user when built via klibc */ #include -#include -#include "scsi_id.h" + +#include "../../udev.h" #include "scsi.h" +#include "scsi_id.h" +#include "scsi_id_version.h" /* * A priority based list of id, naa, and binary/ascii for the identifier @@ -75,22 +68,21 @@ static const char hex_str[]="0123456789abcdef"; * are used here. */ -#define DID_NO_CONNECT 0x01 /* Unable to connect before timeout */ - -#define DID_BUS_BUSY 0x02 /* Bus remain busy until timeout */ -#define DID_TIME_OUT 0x03 /* Timed out for some other reason */ - -#define DRIVER_TIMEOUT 0x06 -#define DRIVER_SENSE 0x08 /* Sense_buffer has been set */ +#define DID_NO_CONNECT 0x01 /* Unable to connect before timeout */ +#define DID_BUS_BUSY 0x02 /* Bus remain busy until timeout */ +#define DID_TIME_OUT 0x03 /* Timed out for some other reason */ +#define DRIVER_TIMEOUT 0x06 +#define DRIVER_SENSE 0x08 /* Sense_buffer has been set */ /* The following "category" function returns one of the following */ -#define SG_ERR_CAT_CLEAN 0 /* No errors or other information */ -#define SG_ERR_CAT_MEDIA_CHANGED 1 /* interpreted from sense buffer */ -#define SG_ERR_CAT_RESET 2 /* interpreted from sense buffer */ -#define SG_ERR_CAT_TIMEOUT 3 -#define SG_ERR_CAT_RECOVERED 4 /* Successful command after recovered err */ -#define SG_ERR_CAT_SENSE 98 /* Something else in the sense buffer */ -#define SG_ERR_CAT_OTHER 99 /* Some other error/warning */ +#define SG_ERR_CAT_CLEAN 0 /* No errors or other information */ +#define SG_ERR_CAT_MEDIA_CHANGED 1 /* interpreted from sense buffer */ +#define SG_ERR_CAT_RESET 2 /* interpreted from sense buffer */ +#define SG_ERR_CAT_TIMEOUT 3 +#define SG_ERR_CAT_RECOVERED 4 /* Successful command after recovered err */ +#define SG_ERR_CAT_NOTSUPPORTED 5 /* Illegal / unsupported command */ +#define SG_ERR_CAT_SENSE 98 /* Something else in the sense buffer */ +#define SG_ERR_CAT_OTHER 99 /* Some other error/warning */ static int sg_err_category_new(int scsi_status, int msg_status, int host_status, int driver_status, const @@ -130,6 +122,8 @@ static int sg_err_category_new(int scsi_status, int msg_status, int return SG_ERR_CAT_MEDIA_CHANGED; if (0x29 == asc) return SG_ERR_CAT_RESET; + } else if (sense_key == ILLEGAL_REQUEST) { + return SG_ERR_CAT_NOTSUPPORTED; } } return SG_ERR_CAT_SENSE; @@ -154,7 +148,7 @@ static int sg_err_category3(struct sg_io_hdr *hp) hp->sbp, hp->sb_len_wr); } -static int scsi_dump_sense(struct sysfs_device *scsi_dev, struct sg_io_hdr *io) +static int scsi_dump_sense(struct sysfs_device *dev_scsi, struct sg_io_hdr *io) { unsigned char *sense_buffer; int s; @@ -181,12 +175,11 @@ static int scsi_dump_sense(struct sysfs_device *scsi_dev, struct sg_io_hdr *io) * we'll retry the command. */ - dprintf("got check condition\n"); + dbg("got check condition\n"); sb_len = io->sb_len_wr; if (sb_len < 1) { - log_message(LOG_WARNING, "%s: sense buffer empty\n", - scsi_dev->name); + info("%s: sense buffer empty\n", dev_scsi->kernel); return -1; } @@ -200,10 +193,8 @@ static int scsi_dump_sense(struct sysfs_device *scsi_dev, struct sg_io_hdr *io) */ s = sense_buffer[7] + 8; if (sb_len < s) { - log_message(LOG_WARNING, - "%s: sense buffer too small %d bytes," - " %d bytes too short\n", scsi_dev->name, - sb_len, s - sb_len); + info("%s: sense buffer too small %d bytes, %d bytes too short\n", + dev_scsi->kernel, sb_len, s - sb_len); return -1; } if ((code == 0x0) || (code == 0x1)) { @@ -213,9 +204,8 @@ static int scsi_dump_sense(struct sysfs_device *scsi_dev, struct sg_io_hdr *io) /* * Possible? */ - log_message(LOG_WARNING, "%s: sense result too" - " small %d bytes\n", - scsi_dev->name, s); + info("%s: sense result too" " small %d bytes\n", + dev_scsi->kernel, s); return -1; } asc = sense_buffer[12]; @@ -226,74 +216,66 @@ static int scsi_dump_sense(struct sysfs_device *scsi_dev, struct sg_io_hdr *io) asc = sense_buffer[2]; ascq = sense_buffer[3]; } else { - log_message(LOG_WARNING, - "%s: invalid sense code 0x%x\n", - scsi_dev->name, code); + info("%s: invalid sense code 0x%x\n", + dev_scsi->kernel, code); return -1; } - log_message(LOG_WARNING, - "%s: sense key 0x%x ASC 0x%x ASCQ 0x%x\n", - scsi_dev->name, sense_key, asc, ascq); + info("%s: sense key 0x%x ASC 0x%x ASCQ 0x%x\n", + dev_scsi->kernel, sense_key, asc, ascq); } else { if (sb_len < 4) { - log_message(LOG_WARNING, - "%s: sense buffer too small %d bytes, %d bytes too short\n", - scsi_dev->name, sb_len, 4 - sb_len); + info("%s: sense buffer too small %d bytes, %d bytes too short\n", + dev_scsi->kernel, sb_len, 4 - sb_len); return -1; } if (sense_buffer[0] < 15) - log_message(LOG_WARNING, "%s: old sense key: 0x%x\n", - scsi_dev->name, sense_buffer[0] & 0x0f); + info("%s: old sense key: 0x%x\n", dev_scsi->kernel, sense_buffer[0] & 0x0f); else - log_message(LOG_WARNING, "%s: sense = %2x %2x\n", - scsi_dev->name, sense_buffer[0], - sense_buffer[2]); - log_message(LOG_WARNING, - "%s: non-extended sense class %d code 0x%0x\n", - scsi_dev->name, sense_class, code); + info("%s: sense = %2x %2x\n", + dev_scsi->kernel, sense_buffer[0], sense_buffer[2]); + info("%s: non-extended sense class %d code 0x%0x\n", + dev_scsi->kernel, sense_class, code); } #ifdef DUMP_SENSE for (i = 0, j = 0; (i < s) && (j < 254); i++) { - dprintf("i %d, j %d\n", i, j); + dbg("i %d, j %d\n", i, j); out_buffer[j++] = hex_str[(sense_buffer[i] & 0xf0) >> 4]; out_buffer[j++] = hex_str[sense_buffer[i] & 0x0f]; out_buffer[j++] = ' '; } out_buffer[j] = '\0'; - log_message(LOG_WARNING, "%s: sense dump:\n", scsi_dev->name); - log_message(LOG_WARNING, "%s: %s\n", scsi_dev->name, out_buffer); + info("%s: sense dump:\n", dev_scsi->kernel); + info("%s: %s\n", dev_scsi->kernel, out_buffer); #endif return -1; } -static int scsi_dump(struct sysfs_device *scsi_dev, struct sg_io_hdr *io) +static int scsi_dump(struct sysfs_device *dev_scsi, struct sg_io_hdr *io) { if (!io->status && !io->host_status && !io->msg_status && !io->driver_status) { /* * Impossible, should not be called. */ - log_message(LOG_WARNING, "%s: called with no error\n", - __FUNCTION__); + info("%s: called with no error\n", __FUNCTION__); return -1; } - log_message(LOG_WARNING, "%s: sg_io failed status 0x%x 0x%x 0x%x 0x%x\n", - scsi_dev->name, io->driver_status, io->host_status, - io->msg_status, io->status); + info("%s: sg_io failed status 0x%x 0x%x 0x%x 0x%x\n", + dev_scsi->kernel, io->driver_status, io->host_status, io->msg_status, io->status); if (io->status == SCSI_CHECK_CONDITION) - return scsi_dump_sense(scsi_dev, io); + return scsi_dump_sense(dev_scsi, io); else return -1; } -static int scsi_inquiry(struct sysfs_device *scsi_dev, int fd, unsigned - char evpd, unsigned char page, unsigned char *buf, - unsigned int buflen) +static int scsi_inquiry(struct sysfs_device *dev_scsi, int fd, + unsigned char evpd, unsigned char page, + unsigned char *buf, unsigned int buflen) { unsigned char inq_cmd[INQUIRY_CMDLEN] = { INQUIRY_CMD, evpd, page, 0, buflen, 0 }; @@ -303,12 +285,12 @@ static int scsi_inquiry(struct sysfs_device *scsi_dev, int fd, unsigned int retry = 3; /* rather random */ if (buflen > SCSI_INQ_BUFF_LEN) { - log_message(LOG_WARNING, "buflen %d too long\n", buflen); + info("buflen %d too long\n", buflen); return -1; } resend: - dprintf("%s evpd %d, page 0x%x\n", scsi_dev->name, evpd, page); + dbg("%s evpd %d, page 0x%x\n", dev_scsi->kernel, evpd, page); memset(&io_hdr, 0, sizeof(struct sg_io_hdr)); io_hdr.interface_id = 'S'; @@ -322,8 +304,7 @@ resend: io_hdr.timeout = DEF_TIMEOUT; if (ioctl(fd, SG_IO, &io_hdr) < 0) { - log_message(LOG_WARNING, "%s: ioctl failed: %s\n", - scsi_dev->name, strerror(errno)); + info("%s: ioctl failed: %s\n", dev_scsi->kernel, strerror(errno)); retval = -1; goto error; } @@ -331,20 +312,23 @@ resend: retval = sg_err_category3(&io_hdr); switch (retval) { + case SG_ERR_CAT_NOTSUPPORTED: + buf[1] = 0; + /* Fallthrough */ case SG_ERR_CAT_CLEAN: case SG_ERR_CAT_RECOVERED: retval = 0; break; default: - retval = scsi_dump(scsi_dev, &io_hdr); + retval = scsi_dump(dev_scsi, &io_hdr); } if (!retval) { retval = buflen; } else if (retval > 0) { if (--retry > 0) { - dprintf("%s: Retrying ...\n", scsi_dev->name); + dbg("%s: Retrying ...\n", dev_scsi->kernel); goto resend; } retval = -1; @@ -352,33 +336,30 @@ resend: error: if (retval < 0) - log_message(LOG_WARNING, - "%s: Unable to get INQUIRY vpd %d page 0x%x.\n", - scsi_dev->name, evpd, page); + info("%s: Unable to get INQUIRY vpd %d page 0x%x.\n", + dev_scsi->kernel, evpd, page); return retval; } /* Get list of supported EVPD pages */ -static int do_scsi_page0_inquiry(struct sysfs_device *scsi_dev, int fd, - char *buffer, int len) +static int do_scsi_page0_inquiry(struct sysfs_device *dev_scsi, int fd, + unsigned char *buffer, unsigned int len) { int retval; - char vendor[MAX_ATTR_LEN]; + const char *vendor; memset(buffer, 0, len); - retval = scsi_inquiry(scsi_dev, fd, 1, 0x0, buffer, len); + retval = scsi_inquiry(dev_scsi, fd, 1, 0x0, buffer, len); if (retval < 0) return 1; if (buffer[1] != 0) { - log_message(LOG_WARNING, "%s: page 0 not available.\n", - scsi_dev->name); + info("%s: page 0 not available.\n", dev_scsi->kernel); return 1; } if (buffer[3] > len) { - log_message(LOG_WARNING, "%s: page 0 buffer too long %d\n", - scsi_dev->name, buffer[3]); + info("%s: page 0 buffer too long %d\n", dev_scsi->kernel, buffer[3]); return 1; } @@ -394,16 +375,13 @@ static int do_scsi_page0_inquiry(struct sysfs_device *scsi_dev, int fd, * If the vendor id appears in the page assume the page is * invalid. */ - if (sysfs_get_attr(scsi_dev->path, "vendor", vendor, - MAX_ATTR_LEN)) { - log_message(LOG_WARNING, - "%s: cannot get model attribute\n", - scsi_dev->name); + vendor = sysfs_attr_get_value(dev_scsi->devpath, "vendor"); + if (!vendor) { + info("%s: cannot get model attribute\n", dev_scsi->kernel); return 1; } - if (!strncmp(&buffer[VENDOR_LENGTH], vendor, VENDOR_LENGTH)) { - log_message(LOG_WARNING, "%s: invalid page0 data\n", - scsi_dev->name); + if (!strncmp((char *)&buffer[VENDOR_LENGTH], vendor, VENDOR_LENGTH)) { + info("%s: invalid page0 data\n", dev_scsi->kernel); return 1; } } @@ -414,44 +392,35 @@ static int do_scsi_page0_inquiry(struct sysfs_device *scsi_dev, int fd, * The caller checks that serial is long enough to include the vendor + * model. */ -static int prepend_vendor_model(struct sysfs_device *scsi_dev, char *serial) +static int prepend_vendor_model(struct sysfs_device *dev_scsi, char *serial) { - char attr[MAX_ATTR_LEN]; + const char *attr; int ind; - if (sysfs_get_attr(scsi_dev->path, "vendor", attr, MAX_ATTR_LEN)) { - log_message(LOG_WARNING, "%s: cannot get vendor attribute\n", - scsi_dev->name); + attr = sysfs_attr_get_value(dev_scsi->devpath, "vendor"); + if (!attr) { + info("%s: cannot get vendor attribute\n", dev_scsi->kernel); return 1; } strncpy(serial, attr, VENDOR_LENGTH); ind = strlen(serial) - 1; - /* - * Remove sysfs added newlines. - */ - if (serial[ind] == '\n') - serial[ind] = '\0'; - if (sysfs_get_attr(scsi_dev->path, "model", attr, MAX_ATTR_LEN)) { - log_message(LOG_WARNING, "%s: cannot get model attribute\n", - scsi_dev->name); + attr = sysfs_attr_get_value(dev_scsi->devpath, "model"); + if (!attr) { + info("%s: cannot get model attribute\n", dev_scsi->kernel); return 1; } strncat(serial, attr, MODEL_LENGTH); ind = strlen(serial) - 1; - if (serial[ind] == '\n') - serial[ind] = '\0'; - else - ind++; + ind++; /* * This is not a complete check, since we are using strncat/cpy * above, ind will never be too large. */ if (ind != (VENDOR_LENGTH + MODEL_LENGTH)) { - log_message(LOG_WARNING, "%s: expected length %d, got length %d\n", - scsi_dev->name, (VENDOR_LENGTH + MODEL_LENGTH), - ind); + info("%s: expected length %d, got length %d\n", + dev_scsi->kernel, (VENDOR_LENGTH + MODEL_LENGTH), ind); return 1; } return ind; @@ -461,11 +430,12 @@ static int prepend_vendor_model(struct sysfs_device *scsi_dev, char *serial) * check_fill_0x83_id - check the page 0x83 id, if OK allocate and fill * serial number. **/ -static int check_fill_0x83_id(struct sysfs_device *scsi_dev, char - *page_83, const struct scsi_id_search_values - *id_search, char *serial, int max_len) +static int check_fill_0x83_id(struct sysfs_device *dev_scsi, + unsigned char *page_83, + const struct scsi_id_search_values + *id_search, char *serial, char *serial_short, int max_len) { - int i, j, len; + int i, j, s, len; /* * ASSOCIATION must be with the device (value 0) @@ -499,7 +469,7 @@ static int check_fill_0x83_id(struct sysfs_device *scsi_dev, char */ len *= 2; - /* + /* * Add one byte for the NUL termination, and one for the id_type. */ len += 2; @@ -507,8 +477,8 @@ static int check_fill_0x83_id(struct sysfs_device *scsi_dev, char len += VENDOR_LENGTH + MODEL_LENGTH; if (max_len < len) { - log_message(LOG_WARNING, "%s: length %d too short - need %d\n", - scsi_dev->name, max_len, len); + info("%s: length %d too short - need %d\n", + dev_scsi->kernel, max_len, len); return 1; } @@ -521,13 +491,13 @@ static int check_fill_0x83_id(struct sysfs_device *scsi_dev, char * included in the identifier. */ if (id_search->id_type == SCSI_ID_VENDOR_SPECIFIC) - if (prepend_vendor_model(scsi_dev, &serial[1]) < 0) { - dprintf("prepend failed\n"); + if (prepend_vendor_model(dev_scsi, &serial[1]) < 0) { + dbg("prepend failed\n"); return 1; } i = 4; /* offset to the start of the identifier */ - j = strlen(serial); + s = j = strlen(serial); if ((page_83[0] & 0x0f) == SCSI_ID_ASCII) { /* * ASCII descriptor. @@ -545,26 +515,49 @@ static int check_fill_0x83_id(struct sysfs_device *scsi_dev, char i++; } } + + strcpy(serial_short, &serial[s]); return 0; } +/* Extract the raw binary from VPD 0x83 pre-SPC devices */ +static int check_fill_0x83_prespc3(struct sysfs_device *dev_scsi, + unsigned char *page_83, + const struct scsi_id_search_values + *id_search, char *serial, char *serial_short, int max_len) +{ + int i, j; + + dbg("using pre-spc3-83 for %s\n", dev_scsi->kernel); + serial[0] = hex_str[id_search->id_type]; + /* serial has been memset to zero before */ + j = strlen(serial); /* j = 1; */ + + for (i = 0; i < page_83[3]; ++i) { + serial[j++] = hex_str[(page_83[4+i] & 0xf0) >> 4]; + serial[j++] = hex_str[ page_83[4+i] & 0x0f]; + } + strcpy(serial_short, serial); + return 0; +} + + /* Get device identification VPD page */ -static int do_scsi_page83_inquiry(struct sysfs_device *scsi_dev, int fd, - char *serial, int len) +static int do_scsi_page83_inquiry(struct sysfs_device *dev_scsi, int fd, + char *serial, char *serial_short, int len) { int retval; unsigned int id_ind, j; unsigned char page_83[SCSI_INQ_BUFF_LEN]; memset(page_83, 0, SCSI_INQ_BUFF_LEN); - retval = scsi_inquiry(scsi_dev, fd, 1, 0x83, page_83, + retval = scsi_inquiry(dev_scsi, fd, 1, PAGE_83, page_83, SCSI_INQ_BUFF_LEN); if (retval < 0) return 1; - if (page_83[1] != 0x83) { - log_message(LOG_WARNING, "%s: Invalid page 0x83\n", - scsi_dev->name); + if (page_83[1] != PAGE_83) { + info("%s: Invalid page 0x83\n", dev_scsi->kernel); return 1; } @@ -574,6 +567,33 @@ static int do_scsi_page83_inquiry(struct sysfs_device *scsi_dev, int fd, * the form: "1 ". */ + /* + * Model 4, 5, and (some) model 6 EMC Symmetrix devices return + * a page 83 reply according to SCSI-2 format instead of SPC-2/3. + * + * The SCSI-2 page 83 format returns an IEEE WWN in binary + * encoded hexi-decimal in the 16 bytes following the initial + * 4-byte page 83 reply header. + * + * Both the SPC-2 and SPC-3 formats return an IEEE WWN as part + * of an Identification descriptor. The 3rd byte of the first + * Identification descriptor is a reserved (BSZ) byte field. + * + * Reference the 7th byte of the page 83 reply to determine + * whether the reply is compliant with SCSI-2 or SPC-2/3 + * specifications. A zero value in the 7th byte indicates + * an SPC-2/3 conformant reply, (i.e., the reserved field of the + * first Identification descriptor). This byte will be non-zero + * for a SCSI-2 conformant page 83 reply from these EMC + * Symmetrix models since the 7th byte of the reply corresponds + * to the 4th and 5th nibbles of the 6-byte OUI for EMC, that is, + * 0x006048. + */ + + if (page_83[6] != 0) + return check_fill_0x83_prespc3(dev_scsi, page_83, id_search_list, + serial, serial_short, len); + /* * Search for a match in the prioritized id_search_list. */ @@ -585,30 +605,98 @@ static int do_scsi_page83_inquiry(struct sysfs_device *scsi_dev, int fd, * one or a small number of descriptors. */ for (j = 4; j <= (unsigned int)page_83[3] + 3; j += page_83[j + 3] + 4) { - retval = check_fill_0x83_id(scsi_dev, &page_83[j], + retval = check_fill_0x83_id(dev_scsi, &page_83[j], &id_search_list[id_ind], - serial, len); - dprintf("%s id desc %d/%d/%d\n", scsi_dev->name, + serial, serial_short, len); + dbg("%s id desc %d/%d/%d\n", dev_scsi->kernel, id_search_list[id_ind].id_type, id_search_list[id_ind].naa_type, id_search_list[id_ind].code_set); if (!retval) { - dprintf(" used\n"); + dbg(" used\n"); return retval; } else if (retval < 0) { - dprintf(" failed\n"); + dbg(" failed\n"); return retval; } else { - dprintf(" not used\n"); + dbg(" not used\n"); } } } return 1; } +/* + * Get device identification VPD page for older SCSI-2 device which is not + * compliant with either SPC-2 or SPC-3 format. + * + * Return the hard coded error code value 2 if the page 83 reply is not + * conformant to the SCSI-2 format. + */ +static int do_scsi_page83_prespc3_inquiry(struct sysfs_device *dev_scsi, int fd, + char *serial, char *serial_short, int len) +{ + int retval; + int i, j; + unsigned char page_83[SCSI_INQ_BUFF_LEN]; + + memset(page_83, 0, SCSI_INQ_BUFF_LEN); + retval = scsi_inquiry(dev_scsi, fd, 1, PAGE_83, page_83, SCSI_INQ_BUFF_LEN); + if (retval < 0) + return 1; + + if (page_83[1] != PAGE_83) { + info("%s: Invalid page 0x83\n", dev_scsi->kernel); + return 1; + } + /* + * Model 4, 5, and (some) model 6 EMC Symmetrix devices return + * a page 83 reply according to SCSI-2 format instead of SPC-2/3. + * + * The SCSI-2 page 83 format returns an IEEE WWN in binary + * encoded hexi-decimal in the 16 bytes following the initial + * 4-byte page 83 reply header. + * + * Both the SPC-2 and SPC-3 formats return an IEEE WWN as part + * of an Identification descriptor. The 3rd byte of the first + * Identification descriptor is a reserved (BSZ) byte field. + * + * Reference the 7th byte of the page 83 reply to determine + * whether the reply is compliant with SCSI-2 or SPC-2/3 + * specifications. A zero value in the 7th byte indicates + * an SPC-2/3 conformant reply, (i.e., the reserved field of the + * first Identification descriptor). This byte will be non-zero + * for a SCSI-2 conformant page 83 reply from these EMC + * Symmetrix models since the 7th byte of the reply corresponds + * to the 4th and 5th nibbles of the 6-byte OUI for EMC, that is, + * 0x006048. + */ + if (page_83[6] == 0) + return 2; + + serial[0] = hex_str[id_search_list[0].id_type]; + /* + * The first four bytes contain data, not a descriptor. + */ + i = 4; + j = strlen(serial); + /* + * Binary descriptor, convert to ASCII, + * using two bytes of ASCII for each byte + * in the page_83. + */ + while (i < (page_83[3]+4)) { + serial[j++] = hex_str[(page_83[i] & 0xf0) >> 4]; + serial[j++] = hex_str[page_83[i] & 0x0f]; + i++; + } + dbg("using pre-spc3-83 for %s\n", dev_scsi->kernel); + return 0; +} + /* Get unit serial number VPD page */ -static int do_scsi_page80_inquiry(struct sysfs_device *scsi_dev, int fd, - char *serial, int max_len) +static int do_scsi_page80_inquiry(struct sysfs_device *dev_scsi, int fd, + char *serial, char *serial_short, int max_len) { int retval; int ser_ind; @@ -617,20 +705,19 @@ static int do_scsi_page80_inquiry(struct sysfs_device *scsi_dev, int fd, unsigned char buf[SCSI_INQ_BUFF_LEN]; memset(buf, 0, SCSI_INQ_BUFF_LEN); - retval = scsi_inquiry(scsi_dev, fd, 1, 0x80, buf, SCSI_INQ_BUFF_LEN); + retval = scsi_inquiry(dev_scsi, fd, 1, PAGE_80, buf, SCSI_INQ_BUFF_LEN); if (retval < 0) return retval; - if (buf[1] != 0x80) { - log_message(LOG_WARNING, "%s: Invalid page 0x80\n", - scsi_dev->name); + if (buf[1] != PAGE_80) { + info("%s: Invalid page 0x80\n", dev_scsi->kernel); return 1; } len = 1 + VENDOR_LENGTH + MODEL_LENGTH + buf[3]; if (max_len < len) { - log_message(LOG_WARNING, "%s: length %d too short - need %d\n", - scsi_dev->name, max_len, len); + info("%s: length %d too short - need %d\n", + dev_scsi->kernel, max_len, len); return 1; } /* @@ -638,17 +725,50 @@ static int do_scsi_page80_inquiry(struct sysfs_device *scsi_dev, int fd, * specific type where we prepend '0' + vendor + model. */ serial[0] = 'S'; - ser_ind = prepend_vendor_model(scsi_dev, &serial[1]); + ser_ind = prepend_vendor_model(dev_scsi, &serial[1]); if (ser_ind < 0) return 1; len = buf[3]; for (i = 4; i < len + 4; i++, ser_ind++) serial[ser_ind] = buf[i]; + memcpy(serial_short, &buf[4], len); + serial_short[len] = '\0'; + return 0; +} + +int scsi_std_inquiry(struct sysfs_device *dev_scsi, const char *devname, + char *vendor, char *model, char *rev, char *type) +{ + int retval; + int fd; + unsigned char buf[SCSI_INQ_BUFF_LEN]; + + dbg("opening %s\n", devname); + fd = open(devname, O_RDONLY | O_NONBLOCK); + if (fd < 0) { + info("%s: cannot open %s: %s\n", + dev_scsi->kernel, devname, strerror(errno)); + return 1; + } + + memset(buf, 0, SCSI_INQ_BUFF_LEN); + retval = scsi_inquiry(dev_scsi, fd, 0, 0, buf, SCSI_INQ_BUFF_LEN); + if (retval < 0) + return retval; + + memcpy(vendor, buf + 8, 8); + memcpy(model, buf + 16, 16); + memcpy(rev, buf + 32, 4); + sprintf(type,"%x", buf[0] & 0x1f); + + if (close(fd) < 0) + info("%s: close failed: %s\n", dev_scsi->kernel, strerror(errno)); + return 0; } -int scsi_get_serial (struct sysfs_device *scsi_dev, const char *devname, - int page_code, char *serial, int len) +int scsi_get_serial (struct sysfs_device *dev_scsi, const char *devname, + int page_code, char *serial, char *serial_short, int len) { unsigned char page0[SCSI_INQ_BUFF_LEN]; int fd; @@ -656,33 +776,57 @@ int scsi_get_serial (struct sysfs_device *scsi_dev, const char *devname, int retval; memset(serial, 0, len); - dprintf("opening %s\n", devname); + dbg("opening %s\n", devname); fd = open(devname, O_RDONLY | O_NONBLOCK); if (fd < 0) { - log_message(LOG_WARNING, "%s: cannot open %s: %s\n", - scsi_dev->name, devname, strerror(errno)); + info("%s: cannot open %s: %s\n", + dev_scsi->kernel, devname, strerror(errno)); return 1; } - if (page_code == 0x80) { - if (do_scsi_page80_inquiry(scsi_dev, fd, serial, len)) { + if (page_code == PAGE_80) { + if (do_scsi_page80_inquiry(dev_scsi, fd, serial, serial_short, len)) { retval = 1; goto completed; } else { retval = 0; goto completed; } - } else if (page_code == 0x83) { - if (do_scsi_page83_inquiry(scsi_dev, fd, serial, len)) { + } else if (page_code == PAGE_83) { + if (do_scsi_page83_inquiry(dev_scsi, fd, serial, serial_short, len)) { retval = 1; goto completed; } else { retval = 0; goto completed; } + } else if (page_code == PAGE_83_PRE_SPC3) { + retval = do_scsi_page83_prespc3_inquiry(dev_scsi, fd, serial, serial_short, len); + if (retval) { + /* + * Fallback to servicing a SPC-2/3 compliant page 83 + * inquiry if the page 83 reply format does not + * conform to pre-SPC3 expectations. + */ + if (retval == 2) { + if (do_scsi_page83_inquiry(dev_scsi, fd, serial, serial_short, len)) { + retval = 1; + goto completed; + } else { + retval = 0; + goto completed; + } + } + else { + retval = 1; + goto completed; + } + } else { + retval = 0; + goto completed; + } } else if (page_code != 0x00) { - log_message(LOG_WARNING, "%s: unsupported page code 0x%d\n", - scsi_dev->name, page_code); + info("%s: unsupported page code 0x%d\n", dev_scsi->kernel, page_code); return 1; } @@ -690,7 +834,7 @@ int scsi_get_serial (struct sysfs_device *scsi_dev, const char *devname, * Get page 0, the page of the pages. By default, try from best to * worst of supported pages: 0x83 then 0x80. */ - if (do_scsi_page0_inquiry(scsi_dev, fd, page0, SCSI_INQ_BUFF_LEN)) { + if (do_scsi_page0_inquiry(dev_scsi, fd, page0, SCSI_INQ_BUFF_LEN)) { /* * Don't try anything else. Black list if a specific page * should be used for this vendor+model, or maybe have an @@ -700,12 +844,12 @@ int scsi_get_serial (struct sysfs_device *scsi_dev, const char *devname, goto completed; } - dprintf("%s: Checking page0\n", scsi_dev->name); + dbg("%s: Checking page0\n", dev_scsi->kernel); for (ind = 4; ind <= page0[3] + 3; ind++) - if (page0[ind] == 0x83) - if (!do_scsi_page83_inquiry(scsi_dev, fd, serial, - len)) { + if (page0[ind] == PAGE_83) + if (!do_scsi_page83_inquiry(dev_scsi, fd, + serial, serial_short, len)) { /* * Success */ @@ -714,9 +858,9 @@ int scsi_get_serial (struct sysfs_device *scsi_dev, const char *devname, } for (ind = 4; ind <= page0[3] + 3; ind++) - if (page0[ind] == 0x80) - if (!do_scsi_page80_inquiry(scsi_dev, fd, serial, - len)) { + if (page0[ind] == PAGE_80) + if (!do_scsi_page80_inquiry(dev_scsi, fd, + serial, serial_short, len)) { /* * Success */ @@ -726,7 +870,6 @@ int scsi_get_serial (struct sysfs_device *scsi_dev, const char *devname, retval = 1; completed: if (close(fd) < 0) - log_message(LOG_WARNING, "%s: close failed: %s\n", - scsi_dev->name, strerror(errno)); + info("%s: close failed: %s\n", dev_scsi->kernel, strerror(errno)); return retval; }