scsi_serial.c revision b4bbcaa9c44260e88402cb8f9a5fb8ac7f35e123
/*
* Copyright (C) IBM Corp. 2003
*
* Author: Patrick Mansfield<patmans@us.ibm.com>
*
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "libudev.h"
#include "libudev-private.h"
#include "random-util.h"
#include "scsi.h"
#include "scsi_id.h"
#include "string-util.h"
/*
* descriptor in VPD page 0x83.
*
* Brute force search for a match starting with the first value in the
* following id_search_list. This is not a performance issue, since there
* is normally one or some small number of descriptors.
*/
static const struct scsi_id_search_values id_search_list[] = {
/*
* Devices already exist using NAA values that are now marked
* reserved. These should not conflict with other values, or it is
* a bug in the device. As long as we find the IEEE extended one
* first, we really don't care what other ones are used. Using
* don't care here means that a device that returns multiple
* non-IEEE descriptors in a random order will get different
* names.
*/
};
static const char hex_str[]="0123456789abcdef";
/*
* are used here.
*/
#define DRIVER_TIMEOUT 0x06
/* The following "category" function returns one of the following */
#define SG_ERR_CAT_CLEAN 0 /* No errors or other information */
#define SG_ERR_CAT_TIMEOUT 3
int scsi_status, int msg_status, int
host_status, int driver_status, const
unsigned char *sense_buffer, int sb_len)
{
scsi_status &= 0x7e;
/*
* XXX change to return only two values - failed or OK.
*/
return SG_ERR_CAT_CLEAN;
if ((scsi_status == SCSI_CHECK_CONDITION) ||
(scsi_status == SCSI_COMMAND_TERMINATED) ||
int sense_key;
unsigned char asc;
if (sense_buffer[0] & 0x2) {
} else {
}
if (sense_key == RECOVERED_ERROR)
return SG_ERR_CAT_RECOVERED;
else if (sense_key == UNIT_ATTENTION) {
if (0x28 == asc)
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;
}
if (host_status) {
if ((host_status == DID_NO_CONNECT) ||
(host_status == DID_BUS_BUSY) ||
(host_status == DID_TIME_OUT))
return SG_ERR_CAT_TIMEOUT;
}
if (driver_status) {
if (driver_status == DRIVER_TIMEOUT)
return SG_ERR_CAT_TIMEOUT;
}
return SG_ERR_CAT_OTHER;
}
{
return sg_err_category_new(udev,
}
{
hp->response_len);
}
struct scsi_id_device *dev_scsi,
unsigned char *sense_buffer, int sb_len)
{
int s;
int code;
int sense_class;
int sense_key;
#ifdef DUMP_SENSE
char out_buffer[256];
int i, j;
#endif
/*
* Figure out and print the sense key, asc and ascq.
*
* If you want to suppress these for a particular drive model, add
* a black list entry in the scsi_id config file.
*
* table, and if found return 1 (after dumping the sense, asc, and
* we'll retry the command.
*/
if (sb_len < 1) {
return -1;
}
if (sense_class == 7) {
/*
* extended sense data.
*/
if (sb_len < s) {
log_debug("%s: sense buffer too small %d bytes, %d bytes too short",
return -1;
}
if (s < 14) {
/*
* Possible?
*/
log_debug("%s: sense result too" " small %d bytes",
return -1;
}
} else {
log_debug("%s: invalid sense code 0x%x",
return -1;
}
log_debug("%s: sense key 0x%x ASC 0x%x ASCQ 0x%x",
} else {
if (sb_len < 4) {
log_debug("%s: sense buffer too small %d bytes, %d bytes too short",
return -1;
}
if (sense_buffer[0] < 15)
else
log_debug("%s: sense = %2x %2x",
log_debug("%s: non-extended sense class %d code 0x%0x",
}
#ifdef DUMP_SENSE
for (i = 0, j = 0; (i < s) && (j < 254); i++) {
out_buffer[j++] = ' ';
}
out_buffer[j] = '\0';
#endif
return -1;
}
{
!io->driver_status) {
/*
* Impossible, should not be called.
*/
return -1;
}
log_debug("%s: sg_io failed status 0x%x 0x%x 0x%x 0x%x",
else
return -1;
}
{
!io->driver_status) {
/*
* Impossible, should not be called.
*/
return -1;
}
log_debug("%s: sg_io failed status 0x%x 0x%x 0x%x",
io->device_status);
io->response_len);
else
return -1;
}
{
unsigned char inq_cmd[INQUIRY_CMDLEN] =
unsigned char sense[SENSE_BUFF_LEN];
void *io_buf;
int retval;
if (buflen > SCSI_INQ_BUFF_LEN) {
return -1;
}
} else {
}
if (retval < 0) {
goto resend;
}
goto error;
}
else
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:
else
}
if (!retval) {
} else if (retval > 0) {
if (--retry > 0)
goto resend;
retval = -1;
}
if (retval < 0)
log_debug("%s: Unable to get INQUIRY vpd %d page 0x%x.",
return retval;
}
/* Get list of supported EVPD pages */
{
int retval;
if (retval < 0)
return 1;
if (buffer[1] != 0) {
return 1;
}
return 1;
}
/*
* Following check is based on code once included in the 2.5.x
* kernel.
*
* Some ill behaved devices return the standard inquiry here
* rather than the evpd data, snoop the data to verify.
*/
/*
* If the vendor id appears in the page assume the page is
* invalid.
*/
return 1;
}
}
return 0;
}
/*
* The caller checks that serial is long enough to include the vendor +
* model.
*/
{
int ind;
/*
* above, ind will never be too large.
*/
log_debug("%s: expected length %d, got length %d",
return -1;
}
return ind;
}
/*
* check_fill_0x83_id - check the page 0x83 id, if OK allocate and fill
* serial number.
*/
struct scsi_id_device *dev_scsi,
unsigned char *page_83,
const struct scsi_id_search_values
char *wwn_vendor_extension, char *tgpt_group)
{
int i, j, s, len;
/*
* ASSOCIATION must be with the device (value 0)
* or with the target port for SCSI_ID_TGTPORT
*/
return 1;
return 1;
return 1;
/*
* Possibly check NAA sub-type.
*/
return 1;
/*
* Check for matching code set - ASCII or BINARY.
*/
return 1;
/*
* page_83[3]: identifier length
*/
/*
* If not ASCII, use two bytes for each binary value.
*/
len *= 2;
/*
* Add one byte for the NUL termination, and one for the id_type.
*/
len += 2;
log_debug("%s: length %d too short - need %d",
return 1;
}
unsigned int group;
return 1;
}
/*
* For SCSI_ID_VENDOR_SPECIFIC prepend the vendor and model before
* the id since it is not unique across all vendors and models,
* this differs from SCSI_ID_T10_VENDOR, where the vendor is
* included in the identifier.
*/
return 1;
i = 4; /* offset to the start of the identifier */
/*
* ASCII descriptor.
*/
} else {
/*
* Binary descriptor, convert to ASCII, using two bytes of
* ASCII for each byte in the page_83.
*/
i++;
}
}
if (wwn_vendor_extension != NULL)
}
return 0;
}
/* Extract the raw binary from VPD 0x83 pre-SPC devices */
struct scsi_id_device *dev_scsi,
unsigned char *page_83,
const struct scsi_id_search_values
{
int i, j;
/* serial has been memset to zero before */
}
return 0;
}
/* Get device identification VPD page */
char *unit_serial_number, char *wwn,
char *wwn_vendor_extension, char *tgpt_group)
{
int retval;
unsigned int id_ind, j;
unsigned char page_83[SCSI_INQ_BUFF_LEN];
/* also pick up the page 80 serial number */
if (retval < 0)
return 1;
return 1;
}
/*
* XXX Some devices (IBM 3542) return all spaces for an identifier if
* the LUN is not actually configured. This leads to identifiers of
* 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(udev,
/*
* Search for a match in the prioritized id_search_list - since WWN ids
* come first we can pick up the WWN in check_fill_0x83_id().
*/
for (id_ind = 0;
id_ind++) {
/*
* Examine each descriptor returned. There is normally only
* one or a small number of descriptors.
*/
if (!retval)
return retval;
else if (retval < 0)
return retval;
}
}
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.
*/
{
int retval;
int i, j;
unsigned char page_83[SCSI_INQ_BUFF_LEN];
if (retval < 0)
return 1;
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;
/*
* The first four bytes contain data, not a descriptor.
*/
i = 4;
/*
* Binary descriptor, convert to ASCII,
* using two bytes of ASCII for each byte
* in the page_83.
*/
i++;
}
return 0;
}
/* Get unit serial number VPD page */
{
int retval;
int ser_ind;
int i;
int len;
unsigned char buf[SCSI_INQ_BUFF_LEN];
if (retval < 0)
return retval;
return 1;
}
log_debug("%s: length %d too short - need %d",
return 1;
}
/*
* Prepend 'S' to avoid unlikely collision with page 0x83 vendor
* specific type where we prepend '0' + vendor + model.
*/
serial[0] = 'S';
if (ser_ind < 0)
return 1;
ser_ind++; /* for the leading 'S' */
}
if (serial_short != NULL) {
}
return 0;
}
{
int fd;
unsigned char buf[SCSI_INQ_BUFF_LEN];
int err = 0;
if (fd < 0) {
return 1;
}
err = 2;
goto out;
}
if (err < 0)
goto out;
err = 0;
out:
return err;
}
{
unsigned char page0[SCSI_INQ_BUFF_LEN];
int fd = -1;
int cnt;
int ind;
int retval;
break;
}
if (fd < 0)
return 1;
retval = 1;
goto completed;
} else {
retval = 0;
goto completed;
}
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)) {
retval = 1;
goto completed;
} else {
retval = 0;
goto completed;
}
} else if (page_code == PAGE_83_PRE_SPC3) {
retval = do_scsi_page83_prespc3_inquiry(udev, dev_scsi, fd, dev_scsi->serial, dev_scsi->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(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)) {
retval = 1;
goto completed;
} else {
retval = 0;
goto completed;
}
}
else {
retval = 1;
goto completed;
}
} else {
retval = 0;
goto completed;
}
} else if (page_code != 0x00) {
retval = 1;
goto completed;
}
/*
* Get page 0, the page of the pages. By default, try from best to
* worst of supported pages: 0x83 then 0x80.
*/
/*
* Don't try anything else. Black list if a specific page
* should be used for this vendor+model, or maybe have an
* optional fall-back to page 0x80 or page 0x83.
*/
retval = 1;
goto completed;
}
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)) {
/*
* Success
*/
retval = 0;
goto completed;
}
/*
* Success
*/
retval = 0;
goto completed;
}
retval = 1;
return retval;
}