cxgb{3,4}*: improve Kconfig dependencies
[linux-2.6.git] / drivers / scsi / sd.c
index e63b85a..e567302 100644 (file)
@@ -46,7 +46,6 @@
 #include <linux/blkdev.h>
 #include <linux/blkpg.h>
 #include <linux/delay.h>
-#include <linux/smp_lock.h>
 #include <linux/mutex.h>
 #include <linux/string_helpers.h>
 #include <linux/async.h>
@@ -119,8 +118,8 @@ static DEFINE_IDA(sd_index_ida);
  * object after last put) */
 static DEFINE_MUTEX(sd_ref_mutex);
 
-struct kmem_cache *sd_cdb_cache;
-mempool_t *sd_cdb_pool;
+static struct kmem_cache *sd_cdb_cache;
+static mempool_t *sd_cdb_pool;
 
 static const char *sd_cache_types[] = {
        "write through", "none", "write back",
@@ -147,7 +146,7 @@ sd_store_cache_type(struct device *dev, struct device_attribute *attr,
                return -EINVAL;
 
        for (i = 0; i < ARRAY_SIZE(sd_cache_types); i++) {
-               const int len = strlen(sd_cache_types[i]);
+               len = strlen(sd_cache_types[i]);
                if (strncmp(sd_cache_types[i], buf, len) == 0 &&
                    buf[len] == '\n') {
                        ct = i;
@@ -259,6 +258,28 @@ sd_show_protection_type(struct device *dev, struct device_attribute *attr,
 }
 
 static ssize_t
+sd_show_protection_mode(struct device *dev, struct device_attribute *attr,
+                       char *buf)
+{
+       struct scsi_disk *sdkp = to_scsi_disk(dev);
+       struct scsi_device *sdp = sdkp->device;
+       unsigned int dif, dix;
+
+       dif = scsi_host_dif_capable(sdp->host, sdkp->protection_type);
+       dix = scsi_host_dix_capable(sdp->host, sdkp->protection_type);
+
+       if (!dix && scsi_host_dix_capable(sdp->host, SD_DIF_TYPE0_PROTECTION)) {
+               dif = 0;
+               dix = 1;
+       }
+
+       if (!dif && !dix)
+               return snprintf(buf, 20, "none\n");
+
+       return snprintf(buf, 20, "%s%u\n", dix ? "dix" : "dif", dif);
+}
+
+static ssize_t
 sd_show_app_tag_own(struct device *dev, struct device_attribute *attr,
                    char *buf)
 {
@@ -285,6 +306,7 @@ static struct device_attribute sd_disk_attrs[] = {
        __ATTR(manage_start_stop, S_IRUGO|S_IWUSR, sd_show_manage_start_stop,
               sd_store_manage_start_stop),
        __ATTR(protection_type, S_IRUGO, sd_show_protection_type, NULL),
+       __ATTR(protection_mode, S_IRUGO, sd_show_protection_mode, NULL),
        __ATTR(app_tag_own, S_IRUGO, sd_show_app_tag_own, NULL),
        __ATTR(thin_provisioning, S_IRUGO, sd_show_thin_provisioning, NULL),
        __ATTR_NULL,
@@ -477,9 +499,7 @@ static int scsi_setup_discard_cmnd(struct scsi_device *sdp, struct request *rq)
 
 static int scsi_setup_flush_cmnd(struct scsi_device *sdp, struct request *rq)
 {
-       /* for now, we use REQ_TYPE_BLOCK_PC. */
-       rq->cmd_type = REQ_TYPE_BLOCK_PC;
-       rq->timeout = SD_TIMEOUT;
+       rq->timeout = SD_FLUSH_TIMEOUT;
        rq->retries = SD_MAX_RETRIES;
        rq->cmd[0] = SYNCHRONIZE_CACHE;
        rq->cmd_len = 10;
@@ -563,7 +583,7 @@ static int sd_prep_fn(struct request_queue *q, struct request *rq)
                 * quietly refuse to do anything to a changed disc until 
                 * the changed bit has been reset
                 */
-               /* printk("SCSI disk has been changed. Prohibiting further I/O.\n"); */
+               /* printk("SCSI disk has been changed or is not present. Prohibiting further I/O.\n"); */
                goto out;
        }
 
@@ -798,6 +818,10 @@ static int sd_open(struct block_device *bdev, fmode_t mode)
 
        sdev = sdkp->device;
 
+       retval = scsi_autopm_get_device(sdev);
+       if (retval)
+               goto error_autopm;
+
        /*
         * If the device is in error recovery, wait until it is done.
         * If the device is offline, then disallow any access to it.
@@ -842,6 +866,8 @@ static int sd_open(struct block_device *bdev, fmode_t mode)
        return 0;
 
 error_out:
+       scsi_autopm_put_device(sdev);
+error_autopm:
        scsi_disk_put(sdkp);
        return retval;  
 }
@@ -866,7 +892,7 @@ static int sd_release(struct gendisk *disk, fmode_t mode)
 
        SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_release\n"));
 
-       if (atomic_dec_return(&sdkp->openers) && sdev->removable) {
+       if (atomic_dec_return(&sdkp->openers) == 0 && sdev->removable) {
                if (scsi_block_when_processing_errors(sdev))
                        scsi_set_medium_removal(sdev, SCSI_REMOVAL_ALLOW);
        }
@@ -875,6 +901,8 @@ static int sd_release(struct gendisk *disk, fmode_t mode)
         * XXX and what if there are packets in flight and this close()
         * XXX is followed by a "rmmod sd_mod"?
         */
+
+       scsi_autopm_put_device(sdev);
        scsi_disk_put(sdkp);
        return 0;
 }
@@ -962,30 +990,51 @@ out:
 
 static void set_media_not_present(struct scsi_disk *sdkp)
 {
-       sdkp->media_present = 0;
-       sdkp->capacity = 0;
-       sdkp->device->changed = 1;
+       if (sdkp->media_present)
+               sdkp->device->changed = 1;
+
+       if (sdkp->device->removable) {
+               sdkp->media_present = 0;
+               sdkp->capacity = 0;
+       }
+}
+
+static int media_not_present(struct scsi_disk *sdkp,
+                            struct scsi_sense_hdr *sshdr)
+{
+       if (!scsi_sense_valid(sshdr))
+               return 0;
+
+       /* not invoked for commands that could return deferred errors */
+       switch (sshdr->sense_key) {
+       case UNIT_ATTENTION:
+       case NOT_READY:
+               /* medium not present */
+               if (sshdr->asc == 0x3A) {
+                       set_media_not_present(sdkp);
+                       return 1;
+               }
+       }
+       return 0;
 }
 
 /**
- *     sd_media_changed - check if our medium changed
- *     @disk: kernel device descriptor 
+ *     sd_check_events - check media events
+ *     @disk: kernel device descriptor
+ *     @clearing: disk events currently being cleared
  *
- *     Returns 0 if not applicable or no change; 1 if change
+ *     Returns mask of DISK_EVENT_*.
  *
  *     Note: this function is invoked from the block subsystem.
  **/
-static int sd_media_changed(struct gendisk *disk)
+static unsigned int sd_check_events(struct gendisk *disk, unsigned int clearing)
 {
        struct scsi_disk *sdkp = scsi_disk(disk);
        struct scsi_device *sdp = sdkp->device;
        struct scsi_sense_hdr *sshdr = NULL;
        int retval;
 
-       SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_media_changed\n"));
-
-       if (!sdp->removable)
-               return 0;
+       SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_check_events\n"));
 
        /*
         * If the device is offline, don't send any commands - just pretend as
@@ -995,7 +1044,6 @@ static int sd_media_changed(struct gendisk *disk)
         */
        if (!scsi_device_online(sdp)) {
                set_media_not_present(sdkp);
-               retval = 1;
                goto out;
        }
 
@@ -1016,34 +1064,32 @@ static int sd_media_changed(struct gendisk *disk)
                                              sshdr);
        }
 
-       /*
-        * Unable to test, unit probably not ready.   This usually
-        * means there is no disc in the drive.  Mark as changed,
-        * and we will figure it out later once the drive is
-        * available again.
-        */
-       if (retval || (scsi_sense_valid(sshdr) &&
-                      /* 0x3a is medium not present */
-                      sshdr->asc == 0x3a)) {
+       /* failed to execute TUR, assume media not present */
+       if (host_byte(retval)) {
                set_media_not_present(sdkp);
-               retval = 1;
                goto out;
        }
 
+       if (media_not_present(sdkp, sshdr))
+               goto out;
+
        /*
         * For removable scsi disk we have to recognise the presence
-        * of a disk in the drive. This is kept in the struct scsi_disk
-        * struct and tested at open !  Daniel Roche (dan@lectra.fr)
+        * of a disk in the drive.
         */
+       if (!sdkp->media_present)
+               sdp->changed = 1;
        sdkp->media_present = 1;
-
-       retval = sdp->changed;
-       sdp->changed = 0;
 out:
-       if (retval != sdkp->previous_state)
-               sdev_evt_send_simple(sdp, SDEV_EVT_MEDIA_CHANGE, GFP_KERNEL);
-       sdkp->previous_state = retval;
+       /*
+        * sdp->changed is set under the following conditions:
+        *
+        *      Medium present state has changed in either direction.
+        *      Device has indicated UNIT_ATTENTION.
+        */
        kfree(sshdr);
+       retval = sdp->changed ? DISK_EVENT_MEDIA_CHANGE : 0;
+       sdp->changed = 0;
        return retval;
 }
 
@@ -1066,7 +1112,7 @@ static int sd_sync_cache(struct scsi_disk *sdkp)
                 * flush everything.
                 */
                res = scsi_execute_req(sdp, cmd, DMA_NONE, NULL, 0, &sshdr,
-                                      SD_TIMEOUT, SD_MAX_RETRIES, NULL);
+                                      SD_FLUSH_TIMEOUT, SD_MAX_RETRIES, NULL);
                if (res == 0)
                        break;
        }
@@ -1136,7 +1182,7 @@ static const struct block_device_operations sd_fops = {
 #ifdef CONFIG_COMPAT
        .compat_ioctl           = sd_compat_ioctl,
 #endif
-       .media_changed          = sd_media_changed,
+       .check_events           = sd_check_events,
        .revalidate_disk        = sd_revalidate_disk,
        .unlock_native_capacity = sd_unlock_native_capacity,
 };
@@ -1147,6 +1193,12 @@ static unsigned int sd_completed_bytes(struct scsi_cmnd *scmd)
        u64 end_lba = blk_rq_pos(scmd->request) + (scsi_bufflen(scmd) / 512);
        u64 bad_lba;
        int info_valid;
+       /*
+        * resid is optional but mostly filled in.  When it's unused,
+        * its value is zero, so we assume the whole buffer transferred
+        */
+       unsigned int transferred = scsi_bufflen(scmd) - scsi_get_resid(scmd);
+       unsigned int good_bytes;
 
        if (scmd->request->cmd_type != REQ_TYPE_FS)
                return 0;
@@ -1180,7 +1232,8 @@ static unsigned int sd_completed_bytes(struct scsi_cmnd *scmd)
        /* This computation should always be done in terms of
         * the resolution of the device's medium.
         */
-       return (bad_lba - start_lba) * scmd->device->sector_size;
+       good_bytes = (bad_lba - start_lba) * scmd->device->sector_size;
+       return min(good_bytes, transferred);
 }
 
 /**
@@ -1272,23 +1325,6 @@ static int sd_done(struct scsi_cmnd *SCpnt)
        return good_bytes;
 }
 
-static int media_not_present(struct scsi_disk *sdkp,
-                            struct scsi_sense_hdr *sshdr)
-{
-
-       if (!scsi_sense_valid(sshdr))
-               return 0;
-       /* not invoked for commands that could return deferred errors */
-       if (sshdr->sense_key != NOT_READY &&
-           sshdr->sense_key != UNIT_ATTENTION)
-               return 0;
-       if (sshdr->asc != 0x3A) /* medium not present */
-               return 0;
-
-       set_media_not_present(sdkp);
-       return 1;
-}
-
 /*
  * spinup disk - called only in sd_revalidate_disk()
  */
@@ -1417,7 +1453,7 @@ sd_spinup_disk(struct scsi_disk *sdkp)
 /*
  * Determine whether disk supports Data Integrity Field.
  */
-void sd_read_protection_type(struct scsi_disk *sdkp, unsigned char *buffer)
+static void sd_read_protection_type(struct scsi_disk *sdkp, unsigned char *buffer)
 {
        struct scsi_device *sdp = sdkp->device;
        u8 type;
@@ -1463,7 +1499,7 @@ static void read_capacity_error(struct scsi_disk *sdkp, struct scsi_device *sdp,
         */
        if (sdp->removable &&
            sense_valid && sshdr->sense_key == NOT_READY)
-               sdp->changed = 1;
+               set_media_not_present(sdkp);
 
        /*
         * We used to set media_present to 0 here to indicate no media
@@ -1492,6 +1528,9 @@ static int read_capacity_16(struct scsi_disk *sdkp, struct scsi_device *sdp,
        unsigned long long lba;
        unsigned sector_size;
 
+       if (sdp->no_read_capacity_16)
+               return -EINVAL;
+
        do {
                memset(cmd, 0, 16);
                cmd[0] = SERVICE_ACTION_IN;
@@ -1548,7 +1587,7 @@ static int read_capacity_16(struct scsi_disk *sdkp, struct scsi_device *sdp,
        }
 
        /* Logical blocks per physical block exponent */
-       sdkp->hw_sector_size = (1 << (buffer[13] & 0xf)) * sector_size;
+       sdkp->physical_block_size = (1 << (buffer[13] & 0xf)) * sector_size;
 
        /* Lowest aligned logical block */
        alignment = ((buffer[14] & 0x3f) << 8 | buffer[15]) * sector_size;
@@ -1561,7 +1600,7 @@ static int read_capacity_16(struct scsi_disk *sdkp, struct scsi_device *sdp,
                struct request_queue *q = sdp->request_queue;
 
                sdkp->thin_provisioning = 1;
-               q->limits.discard_granularity = sdkp->hw_sector_size;
+               q->limits.discard_granularity = sdkp->physical_block_size;
                q->limits.max_discard_sectors = 0xffffffff;
 
                if (buffer[14] & 0x40) /* TPRZ */
@@ -1620,6 +1659,15 @@ static int read_capacity_10(struct scsi_disk *sdkp, struct scsi_device *sdp,
        sector_size = get_unaligned_be32(&buffer[4]);
        lba = get_unaligned_be32(&buffer[0]);
 
+       if (sdp->no_read_capacity_16 && (lba == 0xffffffff)) {
+               /* Some buggy (usb cardreader) devices return an lba of
+                  0xffffffff when the want to report a size of 0 (with
+                  which they really mean no media is present) */
+               sdkp->capacity = 0;
+               sdkp->physical_block_size = sector_size;
+               return sector_size;
+       }
+
        if ((sizeof(sdkp->capacity) == 4) && (lba == 0xffffffff)) {
                sd_printk(KERN_ERR, sdkp, "Too big for this kernel. Use a "
                        "kernel compiled with support for large block "
@@ -1629,7 +1677,7 @@ static int read_capacity_10(struct scsi_disk *sdkp, struct scsi_device *sdp,
        }
 
        sdkp->capacity = lba + 1;
-       sdkp->hw_sector_size = sector_size;
+       sdkp->physical_block_size = sector_size;
        return sector_size;
 }
 
@@ -1750,10 +1798,10 @@ got_data:
                                  (unsigned long long)sdkp->capacity,
                                  sector_size, cap_str_10, cap_str_2);
 
-                       if (sdkp->hw_sector_size != sector_size)
+                       if (sdkp->physical_block_size != sector_size)
                                sd_printk(KERN_NOTICE, sdkp,
                                          "%u-byte physical blocks\n",
-                                         sdkp->hw_sector_size);
+                                         sdkp->physical_block_size);
                }
        }
 
@@ -1767,7 +1815,8 @@ got_data:
        else if (sector_size == 256)
                sdkp->capacity >>= 1;
 
-       blk_queue_physical_block_size(sdp->request_queue, sdkp->hw_sector_size);
+       blk_queue_physical_block_size(sdp->request_queue,
+                                     sdkp->physical_block_size);
        sdkp->device->sector_size = sector_size;
 }
 
@@ -1861,10 +1910,14 @@ sd_read_cache_type(struct scsi_disk *sdkp, unsigned char *buffer)
        int old_rcd = sdkp->RCD;
        int old_dpofua = sdkp->DPOFUA;
 
-       if (sdp->skip_ms_page_8)
-               goto defaults;
-
-       if (sdp->type == TYPE_RBC) {
+       if (sdp->skip_ms_page_8) {
+               if (sdp->type == TYPE_RBC)
+                       goto defaults;
+               else {
+                       modepage = 0x3F;
+                       dbd = 0;
+               }
+       } else if (sdp->type == TYPE_RBC) {
                modepage = 6;
                dbd = 8;
        } else {
@@ -1892,13 +1945,11 @@ sd_read_cache_type(struct scsi_disk *sdkp, unsigned char *buffer)
         */
        if (len < 3)
                goto bad_sense;
-       if (len > 20)
-               len = 20;
-
-       /* Take headers and block descriptors into account */
-       len += data.header_length + data.block_descriptor_length;
-       if (len > SD_BUF_SIZE)
-               goto bad_sense;
+       else if (len > SD_BUF_SIZE) {
+               sd_printk(KERN_NOTICE, sdkp, "Truncating mode parameter "
+                         "data from %d to %d bytes\n", len, SD_BUF_SIZE);
+               len = SD_BUF_SIZE;
+       }
 
        /* Get the data */
        res = sd_do_mode_sense(sdp, dbd, modepage, buffer, len, &data, &sshdr);
@@ -1906,16 +1957,45 @@ sd_read_cache_type(struct scsi_disk *sdkp, unsigned char *buffer)
        if (scsi_status_is_good(res)) {
                int offset = data.header_length + data.block_descriptor_length;
 
-               if (offset >= SD_BUF_SIZE - 2) {
-                       sd_printk(KERN_ERR, sdkp, "Malformed MODE SENSE response\n");
-                       goto defaults;
+               while (offset < len) {
+                       u8 page_code = buffer[offset] & 0x3F;
+                       u8 spf       = buffer[offset] & 0x40;
+
+                       if (page_code == 8 || page_code == 6) {
+                               /* We're interested only in the first 3 bytes.
+                                */
+                               if (len - offset <= 2) {
+                                       sd_printk(KERN_ERR, sdkp, "Incomplete "
+                                                 "mode parameter data\n");
+                                       goto defaults;
+                               } else {
+                                       modepage = page_code;
+                                       goto Page_found;
+                               }
+                       } else {
+                               /* Go to the next page */
+                               if (spf && len - offset > 3)
+                                       offset += 4 + (buffer[offset+2] << 8) +
+                                               buffer[offset+3];
+                               else if (!spf && len - offset > 1)
+                                       offset += 2 + buffer[offset+1];
+                               else {
+                                       sd_printk(KERN_ERR, sdkp, "Incomplete "
+                                                 "mode parameter data\n");
+                                       goto defaults;
+                               }
+                       }
                }
 
-               if ((buffer[offset] & 0x3f) != modepage) {
+               if (modepage == 0x3F) {
+                       sd_printk(KERN_ERR, sdkp, "No Caching mode page "
+                                 "present\n");
+                       goto defaults;
+               } else if ((buffer[offset] & 0x3f) != modepage) {
                        sd_printk(KERN_ERR, sdkp, "Got wrong page\n");
                        goto defaults;
                }
-
+       Page_found:
                if (modepage == 8) {
                        sdkp->WCE = ((buffer[offset + 2] & 0x04) != 0);
                        sdkp->RCD = ((buffer[offset + 2] & 0x01) != 0);
@@ -1963,7 +2043,7 @@ defaults:
  * The ATO bit indicates whether the DIF application tag is available
  * for use by the operating system.
  */
-void sd_read_app_tag_own(struct scsi_disk *sdkp, unsigned char *buffer)
+static void sd_read_app_tag_own(struct scsi_disk *sdkp, unsigned char *buffer)
 {
        int res, offset;
        struct scsi_device *sdp = sdkp->device;
@@ -2033,14 +2113,24 @@ static void sd_read_block_limits(struct scsi_disk *sdkp)
                lba_count = get_unaligned_be32(&buffer[20]);
                desc_count = get_unaligned_be32(&buffer[24]);
 
-               if (lba_count) {
-                       q->limits.max_discard_sectors =
-                               lba_count * sector_sz >> 9;
-
-                       if (desc_count)
+               if (lba_count && desc_count) {
+                       if (sdkp->tpvpd && !sdkp->tpu)
+                               sdkp->unmap = 0;
+                       else
                                sdkp->unmap = 1;
                }
 
+               if (sdkp->tpvpd && !sdkp->tpu && !sdkp->tpws) {
+                       sd_printk(KERN_ERR, sdkp, "Thin provisioning is " \
+                                 "enabled but neither TPU, nor TPWS are " \
+                                 "set. Disabling discard!\n");
+                       goto out;
+               }
+
+               if (lba_count)
+                       q->limits.max_discard_sectors =
+                               lba_count * sector_sz >> 9;
+
                granularity = get_unaligned_be32(&buffer[28]);
 
                if (granularity)
@@ -2081,6 +2171,31 @@ static void sd_read_block_characteristics(struct scsi_disk *sdkp)
        kfree(buffer);
 }
 
+/**
+ * sd_read_thin_provisioning - Query thin provisioning VPD page
+ * @disk: disk to query
+ */
+static void sd_read_thin_provisioning(struct scsi_disk *sdkp)
+{
+       unsigned char *buffer;
+       const int vpd_len = 8;
+
+       if (sdkp->thin_provisioning == 0)
+               return;
+
+       buffer = kmalloc(vpd_len, GFP_KERNEL);
+
+       if (!buffer || scsi_get_vpd_page(sdkp->device, 0xb2, buffer, vpd_len))
+               goto out;
+
+       sdkp->tpvpd = 1;
+       sdkp->tpu   = (buffer[5] >> 7) & 1;     /* UNMAP */
+       sdkp->tpws  = (buffer[5] >> 6) & 1;     /* WRITE SAME(16) with UNMAP */
+
+ out:
+       kfree(buffer);
+}
+
 static int sd_try_extended_inquiry(struct scsi_device *sdp)
 {
        /*
@@ -2103,7 +2218,7 @@ static int sd_revalidate_disk(struct gendisk *disk)
        struct scsi_disk *sdkp = scsi_disk(disk);
        struct scsi_device *sdp = sdkp->device;
        unsigned char *buffer;
-       unsigned ordered;
+       unsigned flush = 0;
 
        SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp,
                                      "sd_revalidate_disk\n"));
@@ -2132,6 +2247,7 @@ static int sd_revalidate_disk(struct gendisk *disk)
                sd_read_capacity(sdkp, buffer);
 
                if (sd_try_extended_inquiry(sdp)) {
+                       sd_read_thin_provisioning(sdkp);
                        sd_read_block_limits(sdkp);
                        sd_read_block_characteristics(sdkp);
                }
@@ -2145,17 +2261,15 @@ static int sd_revalidate_disk(struct gendisk *disk)
 
        /*
         * We now have all cache related info, determine how we deal
-        * with ordered requests.  Note that as the current SCSI
-        * dispatch function can alter request order, we cannot use
-        * QUEUE_ORDERED_TAG_* even when ordered tag is supported.
+        * with flush requests.
         */
-       if (sdkp->WCE)
-               ordered = sdkp->DPOFUA
-                       ? QUEUE_ORDERED_DRAIN_FUA : QUEUE_ORDERED_DRAIN_FLUSH;
-       else
-               ordered = QUEUE_ORDERED_DRAIN;
+       if (sdkp->WCE) {
+               flush |= REQ_FLUSH;
+               if (sdkp->DPOFUA)
+                       flush |= REQ_FUA;
+       }
 
-       blk_queue_ordered(sdkp->disk->queue, ordered);
+       blk_queue_flush(sdkp->disk->queue, flush);
 
        set_capacity(disk, sdkp->capacity);
        kfree(buffer);
@@ -2246,11 +2360,10 @@ static void sd_probe_async(void *data, async_cookie_t cookie)
        index = sdkp->index;
        dev = &sdp->sdev_gendev;
 
-       if (index < SD_MAX_DISKS) {
-               gd->major = sd_major((index & 0xf0) >> 4);
-               gd->first_minor = ((index & 0xf) << 4) | (index & 0xfff00);
-               gd->minors = SD_MINORS;
-       }
+       gd->major = sd_major((index & 0xf0) >> 4);
+       gd->first_minor = ((index & 0xf) << 4) | (index & 0xfff00);
+       gd->minors = SD_MINORS;
+
        gd->fops = &sd_fops;
        gd->private_data = &sdkp->driver;
        gd->queue = sdkp->device->request_queue;
@@ -2272,10 +2385,11 @@ static void sd_probe_async(void *data, async_cookie_t cookie)
 
        gd->driverfs_dev = &sdp->sdev_gendev;
        gd->flags = GENHD_FL_EXT_DEVT;
-       if (sdp->removable)
+       if (sdp->removable) {
                gd->flags |= GENHD_FL_REMOVABLE;
+               gd->events |= DISK_EVENT_MEDIA_CHANGE;
+       }
 
-       dev_set_drvdata(dev, sdkp);
        add_disk(gd);
        sd_dif_config_host(sdkp);
 
@@ -2283,6 +2397,7 @@ static void sd_probe_async(void *data, async_cookie_t cookie)
 
        sd_printk(KERN_NOTICE, sdkp, "Attached SCSI %sdisk\n",
                  sdp->removable ? "removable " : "");
+       scsi_autopm_put_device(sdp);
        put_device(&sdkp->dev);
 }
 
@@ -2309,7 +2424,7 @@ static int sd_probe(struct device *dev)
        struct scsi_device *sdp = to_scsi_device(dev);
        struct scsi_disk *sdkp;
        struct gendisk *gd;
-       u32 index;
+       int index;
        int error;
 
        error = -ENODEV;
@@ -2340,6 +2455,12 @@ static int sd_probe(struct device *dev)
        if (error)
                goto out_put;
 
+       if (index >= SD_MAX_DISKS) {
+               error = -ENODEV;
+               sdev_printk(KERN_WARNING, sdp, "SCSI disk (sd) name space exhausted.\n");
+               goto out_free_index;
+       }
+
        error = sd_format_disk_name("sd", index, gd->disk_name, DISK_NAME_LEN);
        if (error)
                goto out_free_index;
@@ -2349,7 +2470,6 @@ static int sd_probe(struct device *dev)
        sdkp->disk = gd;
        sdkp->index = index;
        atomic_set(&sdkp->openers, 0);
-       sdkp->previous_state = 1;
 
        if (!sdp->request_queue->rq_timeout) {
                if (sdp->type != TYPE_MOD)
@@ -2360,14 +2480,15 @@ static int sd_probe(struct device *dev)
        }
 
        device_initialize(&sdkp->dev);
-       sdkp->dev.parent = &sdp->sdev_gendev;
+       sdkp->dev.parent = dev;
        sdkp->dev.class = &sd_disk_class;
-       dev_set_name(&sdkp->dev, dev_name(&sdp->sdev_gendev));
+       dev_set_name(&sdkp->dev, dev_name(dev));
 
        if (device_add(&sdkp->dev))
                goto out_free_index;
 
-       get_device(&sdp->sdev_gendev);
+       get_device(dev);
+       dev_set_drvdata(dev, sdkp);
 
        get_device(&sdkp->dev); /* prevent release before async_schedule */
        async_schedule(sd_probe_async, sdkp);
@@ -2401,8 +2522,10 @@ static int sd_remove(struct device *dev)
 {
        struct scsi_disk *sdkp;
 
-       async_synchronize_full();
        sdkp = dev_get_drvdata(dev);
+       scsi_autopm_get_device(sdkp->device);
+
+       async_synchronize_full();
        blk_queue_prep_rq(sdkp->device->request_queue, scsi_prep_fn);
        blk_queue_unprep_rq(sdkp->device->request_queue, NULL);
        device_del(&sdkp->dev);
@@ -2616,15 +2739,15 @@ module_exit(exit_sd);
 static void sd_print_sense_hdr(struct scsi_disk *sdkp,
                               struct scsi_sense_hdr *sshdr)
 {
-       sd_printk(KERN_INFO, sdkp, "");
+       sd_printk(KERN_INFO, sdkp, " ");
        scsi_show_sense_hdr(sshdr);
-       sd_printk(KERN_INFO, sdkp, "");
+       sd_printk(KERN_INFO, sdkp, " ");
        scsi_show_extd_sense(sshdr->asc, sshdr->ascq);
 }
 
 static void sd_print_result(struct scsi_disk *sdkp, int result)
 {
-       sd_printk(KERN_INFO, sdkp, "");
+       sd_printk(KERN_INFO, sdkp, " ");
        scsi_show_result(result);
 }