#include <sys/types.h>
#include <sys/ioctl.h>
+#include <sys/stat.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <syslog.h>
+#include <inttypes.h>
+#include <scsi/scsi.h>
#include <scsi/sg.h>
+#include <linux/types.h>
+/* #include <linux/bsg.h> */
+#include "bsg.h"
-#include "../../udev.h"
+#include "../../udev/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
* XXX change to return only two values - failed or OK.
*/
- /*
- * checks msg_status
- */
- if (!scsi_status && !msg_status && !host_status && !driver_status)
+ if (!scsi_status && !host_status && !driver_status)
return SG_ERR_CAT_CLEAN;
if ((scsi_status == SCSI_CHECK_CONDITION) ||
hp->sbp, hp->sb_len_wr);
}
-static int scsi_dump_sense(struct sysfs_device *dev_scsi, struct sg_io_hdr *io)
+static int sg_err_category4(struct sg_io_v4 *hp)
+{
+ return sg_err_category_new(hp->device_status, 0,
+ hp->transport_status, hp->driver_status,
+ (unsigned char *)hp->response,
+ hp->response_len);
+}
+
+static int scsi_dump_sense(struct scsi_id_device *dev_scsi,
+ unsigned char *sense_buffer, int sb_len)
{
- unsigned char *sense_buffer;
int s;
- int sb_len;
int code;
int sense_class;
int sense_key;
dbg("got check condition\n");
- sb_len = io->sb_len_wr;
if (sb_len < 1) {
info("%s: sense buffer empty\n", dev_scsi->kernel);
return -1;
}
- sense_buffer = io->sbp;
sense_class = (sense_buffer[0] >> 4) & 0x07;
code = sense_buffer[0] & 0xf;
return -1;
}
-static int scsi_dump(struct sysfs_device *dev_scsi, struct sg_io_hdr *io)
+static int scsi_dump(struct scsi_id_device *dev_scsi, struct sg_io_hdr *io)
{
if (!io->status && !io->host_status && !io->msg_status &&
!io->driver_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(dev_scsi, io);
+ return scsi_dump_sense(dev_scsi, io->sbp, io->sb_len_wr);
+ else
+ return -1;
+}
+
+static int scsi_dump_v4(struct scsi_id_device *dev_scsi, struct sg_io_v4 *io)
+{
+ if (!io->device_status && !io->transport_status &&
+ !io->driver_status) {
+ /*
+ * Impossible, should not be called.
+ */
+ info("%s: called with no error\n", __FUNCTION__);
+ return -1;
+ }
+
+ info("%s: sg_io failed status 0x%x 0x%x 0x%x\n",
+ dev_scsi->kernel, io->driver_status, io->transport_status,
+ io->device_status);
+ if (io->device_status == SCSI_CHECK_CONDITION)
+ return scsi_dump_sense(dev_scsi, (unsigned char *)io->response,
+ io->response_len);
else
return -1;
}
-static int scsi_inquiry(struct sysfs_device *dev_scsi, int fd,
+static int scsi_inquiry(struct scsi_id_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 };
unsigned char sense[SENSE_BUFF_LEN];
- struct sg_io_hdr io_hdr;
+ void *io_buf;
int retval;
int retry = 3; /* rather random */
resend:
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';
- io_hdr.cmd_len = sizeof(inq_cmd);
- io_hdr.mx_sb_len = sizeof(sense);
- io_hdr.dxfer_direction = SG_DXFER_FROM_DEV;
- io_hdr.dxfer_len = buflen;
- io_hdr.dxferp = buf;
- io_hdr.cmdp = inq_cmd;
- io_hdr.sbp = sense;
- io_hdr.timeout = DEF_TIMEOUT;
-
- if (ioctl(fd, SG_IO, &io_hdr) < 0) {
+ if (dev_scsi->use_sg == 4) {
+ struct sg_io_v4 io_v4;
+
+ memset(&io_v4, 0, sizeof(struct sg_io_v4));
+ io_v4.guard = 'Q';
+ io_v4.protocol = BSG_PROTOCOL_SCSI;
+ io_v4.subprotocol = BSG_SUB_PROTOCOL_SCSI_CMD;
+ io_v4.request_len = sizeof(inq_cmd);
+ io_v4.request = (uintptr_t)inq_cmd;
+ io_v4.max_response_len = sizeof(sense);
+ io_v4.response = (uintptr_t)sense;
+ io_v4.din_xfer_len = buflen;
+ io_v4.din_xferp = (uintptr_t)buf;
+ io_buf = (void *)&io_v4;
+ } else {
+ struct sg_io_hdr io_hdr;
+
+ memset(&io_hdr, 0, sizeof(struct sg_io_hdr));
+ io_hdr.interface_id = 'S';
+ io_hdr.cmd_len = sizeof(inq_cmd);
+ io_hdr.mx_sb_len = sizeof(sense);
+ io_hdr.dxfer_direction = SG_DXFER_FROM_DEV;
+ io_hdr.dxfer_len = buflen;
+ io_hdr.dxferp = buf;
+ io_hdr.cmdp = inq_cmd;
+ io_hdr.sbp = sense;
+ io_hdr.timeout = DEF_TIMEOUT;
+ io_buf = (void *)&io_hdr;
+ }
+
+ retval = ioctl(fd, SG_IO, io_buf);
+ if (retval < 0) {
+ if ((errno == EINVAL || errno == ENOSYS) && dev_scsi->use_sg == 4) {
+ dev_scsi->use_sg = 3;
+ goto resend;
+ }
info("%s: ioctl failed: %s\n", dev_scsi->kernel, strerror(errno));
- retval = -1;
goto error;
}
- retval = sg_err_category3(&io_hdr);
+ if (dev_scsi->use_sg == 4)
+ retval = sg_err_category4(io_buf);
+ else
+ retval = sg_err_category3(io_buf);
switch (retval) {
case SG_ERR_CAT_NOTSUPPORTED:
break;
default:
- retval = scsi_dump(dev_scsi, &io_hdr);
+ if (dev_scsi->use_sg == 4)
+ retval = scsi_dump_v4(dev_scsi, io_buf);
+ else
+ retval = scsi_dump(dev_scsi, io_buf);
}
if (!retval) {
}
/* Get list of supported EVPD pages */
-static int do_scsi_page0_inquiry(struct sysfs_device *dev_scsi, int fd,
+static int do_scsi_page0_inquiry(struct scsi_id_device *dev_scsi, int fd,
unsigned char *buffer, unsigned int len)
{
int retval;
- const char *vendor;
memset(buffer, 0, len);
retval = scsi_inquiry(dev_scsi, fd, 1, 0x0, buffer, len);
* If the vendor id appears in the page assume the page is
* invalid.
*/
- 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((char *)&buffer[VENDOR_LENGTH], vendor, VENDOR_LENGTH)) {
+ if (!strncmp((char *)&buffer[VENDOR_LENGTH], dev_scsi->vendor, VENDOR_LENGTH)) {
info("%s: invalid page0 data\n", dev_scsi->kernel);
return 1;
}
* The caller checks that serial is long enough to include the vendor +
* model.
*/
-static int prepend_vendor_model(struct sysfs_device *dev_scsi, char *serial)
+static int prepend_vendor_model(struct scsi_id_device *dev_scsi, char *serial)
{
- const char *attr;
int ind;
- 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);
+ strncpy(serial, dev_scsi->vendor, VENDOR_LENGTH);
ind = strlen(serial) - 1;
- 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);
+ strncat(serial, dev_scsi->model, MODEL_LENGTH);
ind = strlen(serial) - 1;
ind++;
* 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 *dev_scsi,
+static int check_fill_0x83_id(struct scsi_id_device *dev_scsi,
unsigned char *page_83,
const struct scsi_id_search_values
*id_search, char *serial, char *serial_short, int max_len)
}
/* Extract the raw binary from VPD 0x83 pre-SPC devices */
-static int check_fill_0x83_prespc3(struct sysfs_device *dev_scsi,
+static int check_fill_0x83_prespc3(struct scsi_id_device *dev_scsi,
unsigned char *page_83,
const struct scsi_id_search_values
*id_search, char *serial, char *serial_short, int max_len)
/* Get device identification VPD page */
-static int do_scsi_page83_inquiry(struct sysfs_device *dev_scsi, int fd,
+static int do_scsi_page83_inquiry(struct scsi_id_device *dev_scsi, int fd,
char *serial, char *serial_short, int len)
{
int retval;
* 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,
+static int do_scsi_page83_prespc3_inquiry(struct scsi_id_device *dev_scsi, int fd,
char *serial, char *serial_short, int len)
{
int retval;
}
/* Get unit serial number VPD page */
-static int do_scsi_page80_inquiry(struct sysfs_device *dev_scsi, int fd,
+static int do_scsi_page80_inquiry(struct scsi_id_device *dev_scsi, int fd,
char *serial, char *serial_short, int max_len)
{
int retval;
return 0;
}
-int scsi_std_inquiry(struct sysfs_device *dev_scsi, const char *devname,
- char *vendor, char *model, char *rev, char *type)
+int scsi_std_inquiry(struct scsi_id_device *dev_scsi, const char *devname)
{
- int retval;
int fd;
unsigned char buf[SCSI_INQ_BUFF_LEN];
+ struct stat statbuf;
+ int err = 0;
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));
+ info("scsi_id: cannot open %s: %s\n",
+ 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));
+ if (fstat(fd, &statbuf) < 0) {
+ info("scsi_id: cannot stat %s: %s\n",
+ devname, strerror(errno));
+ err = 2;
+ goto out;
+ }
+ sprintf(dev_scsi->kernel,"%d:%d", major(statbuf.st_rdev),
+ minor(statbuf.st_rdev));
- return 0;
+ memset(buf, 0, SCSI_INQ_BUFF_LEN);
+ err = scsi_inquiry(dev_scsi, fd, 0, 0, buf, SCSI_INQ_BUFF_LEN);
+ if (err < 0)
+ goto out;
+
+ err = 0;
+ memcpy(dev_scsi->vendor, buf + 8, 8);
+ dev_scsi->vendor[8] = '\0';
+ memcpy(dev_scsi->model, buf + 16, 16);
+ dev_scsi->model[16] = '\0';
+ memcpy(dev_scsi->revision, buf + 32, 4);
+ dev_scsi->revision[4] = '\0';
+ sprintf(dev_scsi->type,"%x", buf[0] & 0x1f);
+
+out:
+ close(fd);
+ return err;
}
-int scsi_get_serial (struct sysfs_device *dev_scsi, const char *devname,
- int page_code, char *serial, char *serial_short, int len)
+int scsi_get_serial (struct scsi_id_device *dev_scsi, const char *devname,
+ int page_code, char *serial_short, int len)
{
unsigned char page0[SCSI_INQ_BUFF_LEN];
- int fd;
+ int fd = -1;
+ int cnt = 10;
int ind;
int retval;
- memset(serial, 0, len);
+ memset(dev_scsi->serial, 0, 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;
+ while (--cnt) {
+ fd = open(devname, O_RDONLY | O_NONBLOCK);
+ if (fd >= 0)
+ break;
+ info("%s: cannot open %s: %s\n", dev_scsi->kernel, devname, strerror(errno));
+ if (errno != EBUSY)
+ break;
+ usleep(500000 + (rand() % 100000) );
}
+ if (fd < 0)
+ return 1;
if (page_code == PAGE_80) {
- if (do_scsi_page80_inquiry(dev_scsi, fd, serial, serial_short, len)) {
+ if (do_scsi_page80_inquiry(dev_scsi, fd, dev_scsi->serial, serial_short, len)) {
retval = 1;
goto completed;
} else {
goto completed;
}
} else if (page_code == PAGE_83) {
- if (do_scsi_page83_inquiry(dev_scsi, fd, serial, serial_short, len)) {
+ if (do_scsi_page83_inquiry(dev_scsi, fd, dev_scsi->serial, serial_short, len)) {
retval = 1;
goto completed;
} else {
goto completed;
}
} else if (page_code == PAGE_83_PRE_SPC3) {
- retval = do_scsi_page83_prespc3_inquiry(dev_scsi, fd, serial, serial_short, len);
+ retval = do_scsi_page83_prespc3_inquiry(dev_scsi, fd, dev_scsi->serial, serial_short, len);
if (retval) {
/*
* Fallback to servicing a SPC-2/3 compliant page 83
* conform to pre-SPC3 expectations.
*/
if (retval == 2) {
- if (do_scsi_page83_inquiry(dev_scsi, fd, serial, serial_short, len)) {
+ if (do_scsi_page83_inquiry(dev_scsi, fd, dev_scsi->serial, serial_short, len)) {
retval = 1;
goto completed;
} else {
for (ind = 4; ind <= page0[3] + 3; ind++)
if (page0[ind] == PAGE_83)
if (!do_scsi_page83_inquiry(dev_scsi, fd,
- serial, serial_short, len)) {
+ dev_scsi->serial, serial_short, len)) {
/*
* Success
*/
for (ind = 4; ind <= page0[3] + 3; ind++)
if (page0[ind] == PAGE_80)
if (!do_scsi_page80_inquiry(dev_scsi, fd,
- serial, serial_short, len)) {
+ dev_scsi->serial, serial_short, len)) {
/*
* Success
*/
goto completed;
}
retval = 1;
+
completed:
- if (close(fd) < 0)
- info("%s: close failed: %s\n", dev_scsi->kernel, strerror(errno));
+ close(fd);
return retval;
}