Merge tag 'scsi-misc' of git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi
[linux-2.6-microblaze.git] / drivers / scsi / sd.c
index 1ba83a8..65875a5 100644 (file)
@@ -48,6 +48,7 @@
 #include <linux/blkpg.h>
 #include <linux/blk-pm.h>
 #include <linux/delay.h>
+#include <linux/major.h>
 #include <linux/mutex.h>
 #include <linux/string_helpers.h>
 #include <linux/async.h>
@@ -1756,6 +1757,44 @@ static void sd_rescan(struct device *dev)
        sd_revalidate_disk(sdkp->disk);
 }
 
+static int sd_get_unique_id(struct gendisk *disk, u8 id[16],
+               enum blk_unique_id type)
+{
+       struct scsi_device *sdev = scsi_disk(disk)->device;
+       const struct scsi_vpd *vpd;
+       const unsigned char *d;
+       int ret = -ENXIO, len;
+
+       rcu_read_lock();
+       vpd = rcu_dereference(sdev->vpd_pg83);
+       if (!vpd)
+               goto out_unlock;
+
+       ret = -EINVAL;
+       for (d = vpd->data + 4; d < vpd->data + vpd->len; d += d[3] + 4) {
+               /* we only care about designators with LU association */
+               if (((d[1] >> 4) & 0x3) != 0x00)
+                       continue;
+               if ((d[1] & 0xf) != type)
+                       continue;
+
+               /*
+                * Only exit early if a 16-byte descriptor was found.  Otherwise
+                * keep looking as one with more entropy might still show up.
+                */
+               len = d[3];
+               if (len != 8 && len != 12 && len != 16)
+                       continue;
+               ret = len;
+               memcpy(id, d + 4, len);
+               if (len == 16)
+                       break;
+       }
+out_unlock:
+       rcu_read_unlock();
+       return ret;
+}
+
 static char sd_pr_type(enum pr_type type)
 {
        switch (type) {
@@ -1860,6 +1899,7 @@ static const struct block_device_operations sd_fops = {
        .check_events           = sd_check_events,
        .unlock_native_capacity = sd_unlock_native_capacity,
        .report_zones           = sd_zbc_report_zones,
+       .get_unique_id          = sd_get_unique_id,
        .pr_ops                 = &sd_pr_ops,
 };
 
@@ -3095,6 +3135,86 @@ static void sd_read_security(struct scsi_disk *sdkp, unsigned char *buffer)
                sdkp->security = 1;
 }
 
+static inline sector_t sd64_to_sectors(struct scsi_disk *sdkp, u8 *buf)
+{
+       return logical_to_sectors(sdkp->device, get_unaligned_be64(buf));
+}
+
+/**
+ * sd_read_cpr - Query concurrent positioning ranges
+ * @sdkp:      disk to query
+ */
+static void sd_read_cpr(struct scsi_disk *sdkp)
+{
+       struct blk_independent_access_ranges *iars = NULL;
+       unsigned char *buffer = NULL;
+       unsigned int nr_cpr = 0;
+       int i, vpd_len, buf_len = SD_BUF_SIZE;
+       u8 *desc;
+
+       /*
+        * We need to have the capacity set first for the block layer to be
+        * able to check the ranges.
+        */
+       if (sdkp->first_scan)
+               return;
+
+       if (!sdkp->capacity)
+               goto out;
+
+       /*
+        * Concurrent Positioning Ranges VPD: there can be at most 256 ranges,
+        * leading to a maximum page size of 64 + 256*32 bytes.
+        */
+       buf_len = 64 + 256*32;
+       buffer = kmalloc(buf_len, GFP_KERNEL);
+       if (!buffer || scsi_get_vpd_page(sdkp->device, 0xb9, buffer, buf_len))
+               goto out;
+
+       /* We must have at least a 64B header and one 32B range descriptor */
+       vpd_len = get_unaligned_be16(&buffer[2]) + 3;
+       if (vpd_len > buf_len || vpd_len < 64 + 32 || (vpd_len & 31)) {
+               sd_printk(KERN_ERR, sdkp,
+                         "Invalid Concurrent Positioning Ranges VPD page\n");
+               goto out;
+       }
+
+       nr_cpr = (vpd_len - 64) / 32;
+       if (nr_cpr == 1) {
+               nr_cpr = 0;
+               goto out;
+       }
+
+       iars = disk_alloc_independent_access_ranges(sdkp->disk, nr_cpr);
+       if (!iars) {
+               nr_cpr = 0;
+               goto out;
+       }
+
+       desc = &buffer[64];
+       for (i = 0; i < nr_cpr; i++, desc += 32) {
+               if (desc[0] != i) {
+                       sd_printk(KERN_ERR, sdkp,
+                               "Invalid Concurrent Positioning Range number\n");
+                       nr_cpr = 0;
+                       break;
+               }
+
+               iars->ia_range[i].sector = sd64_to_sectors(sdkp, desc + 8);
+               iars->ia_range[i].nr_sectors = sd64_to_sectors(sdkp, desc + 16);
+       }
+
+out:
+       disk_set_independent_access_ranges(sdkp->disk, iars);
+       if (nr_cpr && sdkp->nr_actuators != nr_cpr) {
+               sd_printk(KERN_NOTICE, sdkp,
+                         "%u concurrent positioning ranges\n", nr_cpr);
+               sdkp->nr_actuators = nr_cpr;
+       }
+
+       kfree(buffer);
+}
+
 /*
  * Determine the device's preferred I/O size for reads and writes
  * unless the reported value is unreasonably small, large, not a
@@ -3210,6 +3330,7 @@ static int sd_revalidate_disk(struct gendisk *disk)
                sd_read_app_tag_own(sdkp, buffer);
                sd_read_write_same(sdkp, buffer);
                sd_read_security(sdkp, buffer);
+               sd_read_cpr(sdkp);
        }
 
        /*
@@ -3707,7 +3828,12 @@ static int sd_resume_system(struct device *dev)
 static int sd_resume_runtime(struct device *dev)
 {
        struct scsi_disk *sdkp = dev_get_drvdata(dev);
-       struct scsi_device *sdp = sdkp->device;
+       struct scsi_device *sdp;
+
+       if (!sdkp)      /* E.g.: runtime resume at the start of sd_probe() */
+               return 0;
+
+       sdp = sdkp->device;
 
        if (sdp->ignore_media_change) {
                /* clear the device's sense data */