[SCSI] Add EVPD page 0x83 and 0x80 to sysfs
authorHannes Reinecke <hare@suse.de>
Sat, 15 Mar 2014 08:51:49 +0000 (09:51 +0100)
committerJames Bottomley <JBottomley@Parallels.com>
Thu, 27 Mar 2014 15:25:33 +0000 (08:25 -0700)
EVPD page 0x83 is used to uniquely identify the device.
So instead of having each and every program issue a separate
SG_IO call to retrieve this information it does make far more
sense to display it in sysfs.

Some older devices (most notably tapes) will only report reliable
information in page 0x80 (Unit Serial Number). So export this
in the sysfs attribute 'vpd_pg80'.

[jejb: checkpatch fix]
[hare: attach after transport configure]
[fengguang.wu@intel.com: spotted problems with the original now fixed]
Signed-off-by: Hannes Reinecke <hare@suse.de>
Signed-off-by: James Bottomley <JBottomley@Parallels.com>
drivers/scsi/scsi.c
drivers/scsi/scsi_scan.c
drivers/scsi/scsi_sysfs.c
include/scsi/scsi_device.h

index b2526ad..1d98ac9 100644 (file)
@@ -1029,6 +1029,93 @@ int scsi_get_vpd_page(struct scsi_device *sdev, u8 page, unsigned char *buf,
 }
 EXPORT_SYMBOL_GPL(scsi_get_vpd_page);
 
+/**
+ * scsi_attach_vpd - Attach Vital Product Data to a SCSI device structure
+ * @sdev: The device to ask
+ *
+ * Attach the 'Device Identification' VPD page (0x83) and the
+ * 'Unit Serial Number' VPD page (0x80) to a SCSI device
+ * structure. This information can be used to identify the device
+ * uniquely.
+ */
+void scsi_attach_vpd(struct scsi_device *sdev)
+{
+       int result, i;
+       int vpd_len = SCSI_VPD_PG_LEN;
+       int pg80_supported = 0;
+       int pg83_supported = 0;
+       unsigned char *vpd_buf;
+
+       if (sdev->skip_vpd_pages)
+               return;
+retry_pg0:
+       vpd_buf = kmalloc(vpd_len, GFP_KERNEL);
+       if (!vpd_buf)
+               return;
+
+       /* Ask for all the pages supported by this device */
+       result = scsi_vpd_inquiry(sdev, vpd_buf, 0, vpd_len);
+       if (result < 0) {
+               kfree(vpd_buf);
+               return;
+       }
+       if (result > vpd_len) {
+               vpd_len = result;
+               kfree(vpd_buf);
+               goto retry_pg0;
+       }
+
+       for (i = 4; i < result; i++) {
+               if (vpd_buf[i] == 0x80)
+                       pg80_supported = 1;
+               if (vpd_buf[i] == 0x83)
+                       pg83_supported = 1;
+       }
+       kfree(vpd_buf);
+       vpd_len = SCSI_VPD_PG_LEN;
+
+       if (pg80_supported) {
+retry_pg80:
+               vpd_buf = kmalloc(vpd_len, GFP_KERNEL);
+               if (!vpd_buf)
+                       return;
+
+               result = scsi_vpd_inquiry(sdev, vpd_buf, 0x80, vpd_len);
+               if (result < 0) {
+                       kfree(vpd_buf);
+                       return;
+               }
+               if (result > vpd_len) {
+                       vpd_len = result;
+                       kfree(vpd_buf);
+                       goto retry_pg80;
+               }
+               sdev->vpd_pg80_len = result;
+               sdev->vpd_pg80 = vpd_buf;
+               vpd_len = SCSI_VPD_PG_LEN;
+       }
+
+       if (pg83_supported) {
+retry_pg83:
+               vpd_buf = kmalloc(vpd_len, GFP_KERNEL);
+               if (!vpd_buf)
+                       return;
+
+               result = scsi_vpd_inquiry(sdev, vpd_buf, 0x83, vpd_len);
+               if (result < 0) {
+                       kfree(vpd_buf);
+                       return;
+               }
+               if (result > vpd_len) {
+                       vpd_len = result;
+                       kfree(vpd_buf);
+                       goto retry_pg83;
+               }
+               sdev->vpd_pg83_len = result;
+               sdev->vpd_pg83 = vpd_buf;
+       }
+}
+
 /**
  * scsi_report_opcode - Find out if a given command opcode is supported
  * @sdev:      scsi device to query
index 4109530..27f96d5 100644 (file)
@@ -970,6 +970,9 @@ static int scsi_add_lun(struct scsi_device *sdev, unsigned char *inq_result,
                }
        }
 
+       if (sdev->scsi_level >= SCSI_3)
+               scsi_attach_vpd(sdev);
+
        sdev->max_queue_depth = sdev->queue_depth;
 
        /*
index 8509822..1392474 100644 (file)
@@ -412,6 +412,8 @@ static void scsi_device_dev_release_usercontext(struct work_struct *work)
        /* NULL queue means the device can't be used */
        sdev->request_queue = NULL;
 
+       kfree(sdev->vpd_pg83);
+       kfree(sdev->vpd_pg80);
        kfree(sdev->inquiry);
        kfree(sdev);
 
@@ -751,8 +753,32 @@ store_queue_type_field(struct device *dev, struct device_attribute *attr,
 static DEVICE_ATTR(queue_type, S_IRUGO | S_IWUSR, show_queue_type_field,
                   store_queue_type_field);
 
+#define sdev_vpd_pg_attr(_page)                                                \
+static ssize_t                                                 \
+show_vpd_##_page(struct file *filp, struct kobject *kobj,      \
+                struct bin_attribute *bin_attr,                        \
+                char *buf, loff_t off, size_t count)                   \
+{                                                                      \
+       struct device *dev = container_of(kobj, struct device, kobj);   \
+       struct scsi_device *sdev = to_scsi_device(dev);                 \
+       if (!sdev->vpd_##_page)                                         \
+               return -EINVAL;                                         \
+       return memory_read_from_buffer(buf, count, &off,                \
+                                      sdev->vpd_##_page,               \
+                                      sdev->vpd_##_page##_len);        \
+}                                                                      \
+static struct bin_attribute dev_attr_vpd_##_page = {           \
+       .attr = {.name = __stringify(vpd_##_page), .mode = S_IRUGO },   \
+       .size = 0,                                                      \
+       .read = show_vpd_##_page,                                       \
+};
+
+sdev_vpd_pg_attr(pg83);
+sdev_vpd_pg_attr(pg80);
+
 static ssize_t
-show_iostat_counterbits(struct device *dev, struct device_attribute *attr,                             char *buf)
+show_iostat_counterbits(struct device *dev, struct device_attribute *attr,
+                       char *buf)
 {
        return snprintf(buf, 20, "%d\n", (int)sizeof(atomic_t) * 8);
 }
@@ -936,8 +962,14 @@ static struct attribute *scsi_sdev_attrs[] = {
        NULL
 };
 
+static struct bin_attribute *scsi_sdev_bin_attrs[] = {
+       &dev_attr_vpd_pg83,
+       &dev_attr_vpd_pg80,
+       NULL
+};
 static struct attribute_group scsi_sdev_attr_group = {
        .attrs =        scsi_sdev_attrs,
+       .bin_attrs =    scsi_sdev_bin_attrs,
        .is_visible =   scsi_sdev_attr_is_visible,
 };
 
index ccabdc1..4e845b8 100644 (file)
@@ -113,6 +113,12 @@ struct scsi_device {
        const char * vendor;            /* [back_compat] point into 'inquiry' ... */
        const char * model;             /* ... after scan; point to static string */
        const char * rev;               /* ... "nullnullnullnull" before scan */
+
+#define SCSI_VPD_PG_LEN                255
+       int vpd_pg83_len;
+       unsigned char *vpd_pg83;
+       int vpd_pg80_len;
+       unsigned char *vpd_pg80;
        unsigned char current_tag;      /* current tag */
        struct scsi_target      *sdev_target;   /* used only for single_lun */
 
@@ -320,6 +326,7 @@ extern int scsi_add_device(struct Scsi_Host *host, uint channel,
 extern int scsi_register_device_handler(struct scsi_device_handler *scsi_dh);
 extern void scsi_remove_device(struct scsi_device *);
 extern int scsi_unregister_device_handler(struct scsi_device_handler *scsi_dh);
+void scsi_attach_vpd(struct scsi_device *sdev);
 
 extern int scsi_device_get(struct scsi_device *);
 extern void scsi_device_put(struct scsi_device *);