emac: Fix clock control for 405EX and 405EXr chips
[safe/jmp/linux-2.6] / drivers / scsi / sd.c
index 55e6ed4..55310db 100644 (file)
@@ -48,6 +48,7 @@
 #include <linux/delay.h>
 #include <linux/mutex.h>
 #include <linux/string_helpers.h>
+#include <linux/async.h>
 #include <asm/uaccess.h>
 
 #include <scsi/scsi.h>
@@ -106,6 +107,7 @@ static void scsi_disk_release(struct device *cdev);
 static void sd_print_sense_hdr(struct scsi_disk *, struct scsi_sense_hdr *);
 static void sd_print_result(struct scsi_disk *, int);
 
+static DEFINE_SPINLOCK(sd_index_lock);
 static DEFINE_IDA(sd_index_ida);
 
 /* This semaphore is used to mediate the 0->1 reference get in the
@@ -609,17 +611,15 @@ static int sd_prep_fn(struct request_queue *q, struct request *rq)
  *     In the latter case @inode and @filp carry an abridged amount
  *     of information as noted above.
  **/
-static int sd_open(struct inode *inode, struct file *filp)
+static int sd_open(struct block_device *bdev, fmode_t mode)
 {
-       struct gendisk *disk = inode->i_bdev->bd_disk;
-       struct scsi_disk *sdkp;
+       struct scsi_disk *sdkp = scsi_disk_get(bdev->bd_disk);
        struct scsi_device *sdev;
        int retval;
 
-       if (!(sdkp = scsi_disk_get(disk)))
+       if (!sdkp)
                return -ENXIO;
 
-
        SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_open\n"));
 
        sdev = sdkp->device;
@@ -633,14 +633,13 @@ static int sd_open(struct inode *inode, struct file *filp)
                goto error_out;
 
        if (sdev->removable || sdkp->write_prot)
-               check_disk_change(inode->i_bdev);
+               check_disk_change(bdev);
 
        /*
         * If the drive is empty, just let the open fail.
         */
        retval = -ENOMEDIUM;
-       if (sdev->removable && !sdkp->media_present &&
-           !(filp->f_flags & O_NDELAY))
+       if (sdev->removable && !sdkp->media_present && !(mode & FMODE_NDELAY))
                goto error_out;
 
        /*
@@ -648,7 +647,7 @@ static int sd_open(struct inode *inode, struct file *filp)
         * if the user expects to be able to write to the thing.
         */
        retval = -EROFS;
-       if (sdkp->write_prot && (filp->f_mode & FMODE_WRITE))
+       if (sdkp->write_prot && (mode & FMODE_WRITE))
                goto error_out;
 
        /*
@@ -684,9 +683,8 @@ error_out:
  *     Note: may block (uninterruptible) if error recovery is underway
  *     on this disk.
  **/
-static int sd_release(struct inode *inode, struct file *filp)
+static int sd_release(struct gendisk *disk, fmode_t mode)
 {
-       struct gendisk *disk = inode->i_bdev->bd_disk;
        struct scsi_disk *sdkp = scsi_disk(disk);
        struct scsi_device *sdev = sdkp->device;
 
@@ -743,10 +741,9 @@ static int sd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  *     Note: most ioctls are forward onto the block subsystem or further
  *     down in the scsi subsystem.
  **/
-static int sd_ioctl(struct inode * inode, struct file * filp, 
+static int sd_ioctl(struct block_device *bdev, fmode_t mode,
                    unsigned int cmd, unsigned long arg)
 {
-       struct block_device *bdev = inode->i_bdev;
        struct gendisk *disk = bdev->bd_disk;
        struct scsi_device *sdp = scsi_disk(disk)->device;
        void __user *p = (void __user *)arg;
@@ -761,7 +758,8 @@ static int sd_ioctl(struct inode * inode, struct file * filp,
         * may try and take the device offline, in which case all further
         * access to the device is prohibited.
         */
-       error = scsi_nonblockable_ioctl(sdp, cmd, p, filp);
+       error = scsi_nonblockable_ioctl(sdp, cmd, p,
+                                       (mode & FMODE_NDELAY) != 0);
        if (!scsi_block_when_processing_errors(sdp) || !error)
                return error;
 
@@ -775,7 +773,7 @@ static int sd_ioctl(struct inode * inode, struct file * filp,
                case SCSI_IOCTL_GET_BUS_NUMBER:
                        return scsi_ioctl(sdp, cmd, p);
                default:
-                       error = scsi_cmd_ioctl(filp, disk->queue, disk, cmd, p);
+                       error = scsi_cmd_ioctl(disk->queue, disk, mode, cmd, p);
                        if (error != -ENOTTY)
                                return error;
        }
@@ -888,7 +886,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);
+                                      SD_TIMEOUT, SD_MAX_RETRIES, NULL);
                if (res == 0)
                        break;
        }
@@ -928,11 +926,10 @@ static void sd_rescan(struct device *dev)
  * This gets directly called from VFS. When the ioctl 
  * is not recognized we go back to the other translation paths. 
  */
-static long sd_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
+static int sd_compat_ioctl(struct block_device *bdev, fmode_t mode,
+                          unsigned int cmd, unsigned long arg)
 {
-       struct block_device *bdev = file->f_path.dentry->d_inode->i_bdev;
-       struct gendisk *disk = bdev->bd_disk;
-       struct scsi_device *sdev = scsi_disk(disk)->device;
+       struct scsi_device *sdev = scsi_disk(bdev->bd_disk)->device;
 
        /*
         * If we are in the middle of error recovery, don't let anyone
@@ -962,7 +959,7 @@ static struct block_device_operations sd_fops = {
        .owner                  = THIS_MODULE,
        .open                   = sd_open,
        .release                = sd_release,
-       .ioctl                  = sd_ioctl,
+       .locked_ioctl           = sd_ioctl,
        .getgeo                 = sd_getgeo,
 #ifdef CONFIG_COMPAT
        .compat_ioctl           = sd_compat_ioctl,
@@ -1084,15 +1081,6 @@ static int sd_done(struct scsi_cmnd *SCpnt)
                        scsi_print_sense("sd", SCpnt);
                        good_bytes = sd_completed_bytes(SCpnt);
                }
-               if (!scsi_device_protection(SCpnt->device) &&
-                   SCpnt->device->use_10_for_rw &&
-                   (SCpnt->cmnd[0] == READ_10 ||
-                    SCpnt->cmnd[0] == WRITE_10))
-                       SCpnt->device->use_10_for_rw = 0;
-               if (SCpnt->device->use_10_for_ms &&
-                   (SCpnt->cmnd[0] == MODE_SENSE_10 ||
-                    SCpnt->cmnd[0] == MODE_SELECT_10))
-                       SCpnt->device->use_10_for_ms = 0;
                break;
        default:
                break;
@@ -1148,7 +1136,7 @@ sd_spinup_disk(struct scsi_disk *sdkp)
                        the_result = scsi_execute_req(sdkp->device, cmd,
                                                      DMA_NONE, NULL, 0,
                                                      &sshdr, SD_TIMEOUT,
-                                                     SD_MAX_RETRIES);
+                                                     SD_MAX_RETRIES, NULL);
 
                        /*
                         * If the drive has indicated to us that it
@@ -1206,7 +1194,8 @@ sd_spinup_disk(struct scsi_disk *sdkp)
                                        cmd[4] |= 1 << 4;
                                scsi_execute_req(sdkp->device, cmd, DMA_NONE,
                                                 NULL, 0, &sshdr,
-                                                SD_TIMEOUT, SD_MAX_RETRIES);
+                                                SD_TIMEOUT, SD_MAX_RETRIES,
+                                                NULL);
                                spintime_expire = jiffies + 100 * HZ;
                                spintime = 1;
                        }
@@ -1320,7 +1309,7 @@ repeat:
                
                the_result = scsi_execute_req(sdp, cmd, DMA_FROM_DEVICE,
                                              buffer, longrc ? 13 : 8, &sshdr,
-                                             SD_TIMEOUT, SD_MAX_RETRIES);
+                                             SD_TIMEOUT, SD_MAX_RETRIES, NULL);
 
                if (media_not_present(sdkp, &sshdr))
                        return;
@@ -1445,7 +1434,7 @@ got_data:
 
        {
                char cap_str_2[10], cap_str_10[10];
-               u64 sz = sdkp->capacity << ffz(~sector_size);
+               u64 sz = (u64)sdkp->capacity << ilog2(sector_size);
 
                string_get_size(sz, STRING_UNITS_2, cap_str_2,
                                sizeof(cap_str_2));
@@ -1815,6 +1804,71 @@ static int sd_format_disk_name(char *prefix, int index, char *buf, int buflen)
        return 0;
 }
 
+/*
+ * The asynchronous part of sd_probe
+ */
+static void sd_probe_async(void *data, async_cookie_t cookie)
+{
+       struct scsi_disk *sdkp = data;
+       struct scsi_device *sdp;
+       struct gendisk *gd;
+       u32 index;
+       struct device *dev;
+
+       sdp = sdkp->device;
+       gd = sdkp->disk;
+       index = sdkp->index;
+       dev = &sdp->sdev_gendev;
+
+       if (!sdp->request_queue->rq_timeout) {
+               if (sdp->type != TYPE_MOD)
+                       blk_queue_rq_timeout(sdp->request_queue, SD_TIMEOUT);
+               else
+                       blk_queue_rq_timeout(sdp->request_queue,
+                                            SD_MOD_TIMEOUT);
+       }
+
+       device_initialize(&sdkp->dev);
+       sdkp->dev.parent = &sdp->sdev_gendev;
+       sdkp->dev.class = &sd_disk_class;
+       dev_set_name(&sdkp->dev, dev_name(&sdp->sdev_gendev));
+
+       if (device_add(&sdkp->dev))
+               goto out_free_index;
+
+       get_device(&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->fops = &sd_fops;
+       gd->private_data = &sdkp->driver;
+       gd->queue = sdkp->device->request_queue;
+
+       sd_revalidate_disk(gd);
+
+       blk_queue_prep_rq(sdp->request_queue, sd_prep_fn);
+
+       gd->driverfs_dev = &sdp->sdev_gendev;
+       gd->flags = GENHD_FL_EXT_DEVT | GENHD_FL_DRIVERFS;
+       if (sdp->removable)
+               gd->flags |= GENHD_FL_REMOVABLE;
+
+       dev_set_drvdata(dev, sdkp);
+       add_disk(gd);
+       sd_dif_config_host(sdkp);
+
+       sd_printk(KERN_NOTICE, sdkp, "Attached SCSI %sdisk\n",
+                 sdp->removable ? "removable " : "");
+
+       return;
+
+ out_free_index:
+       ida_remove(&sd_index_ida, index);
+}
+
 /**
  *     sd_probe - called during driver initialization and whenever a
  *     new scsi device is attached to the system. It is called once
@@ -1861,7 +1915,9 @@ static int sd_probe(struct device *dev)
                if (!ida_pre_get(&sd_index_ida, GFP_KERNEL))
                        goto out_put;
 
+               spin_lock(&sd_index_lock);
                error = ida_get_new(&sd_index_ida, &index);
+               spin_unlock(&sd_index_lock);
        } while (error == -EAGAIN);
 
        if (error)
@@ -1878,53 +1934,14 @@ static int sd_probe(struct device *dev)
        sdkp->openers = 0;
        sdkp->previous_state = 1;
 
-       if (!sdp->request_queue->rq_timeout) {
-               if (sdp->type != TYPE_MOD)
-                       blk_queue_rq_timeout(sdp->request_queue, SD_TIMEOUT);
-               else
-                       blk_queue_rq_timeout(sdp->request_queue,
-                                            SD_MOD_TIMEOUT);
-       }
-
-       device_initialize(&sdkp->dev);
-       sdkp->dev.parent = &sdp->sdev_gendev;
-       sdkp->dev.class = &sd_disk_class;
-       strncpy(sdkp->dev.bus_id, sdp->sdev_gendev.bus_id, BUS_ID_SIZE);
-
-       if (device_add(&sdkp->dev))
-               goto out_free_index;
-
-       get_device(&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->fops = &sd_fops;
-       gd->private_data = &sdkp->driver;
-       gd->queue = sdkp->device->request_queue;
-
-       sd_revalidate_disk(gd);
-
-       blk_queue_prep_rq(sdp->request_queue, sd_prep_fn);
-
-       gd->driverfs_dev = &sdp->sdev_gendev;
-       gd->flags = GENHD_FL_EXT_DEVT | GENHD_FL_DRIVERFS;
-       if (sdp->removable)
-               gd->flags |= GENHD_FL_REMOVABLE;
-
-       dev_set_drvdata(dev, sdkp);
-       add_disk(gd);
-       sd_dif_config_host(sdkp);
-
-       sd_printk(KERN_NOTICE, sdkp, "Attached SCSI %sdisk\n",
-                 sdp->removable ? "removable " : "");
+       async_schedule(sd_probe_async, sdkp);
 
        return 0;
 
  out_free_index:
+       spin_lock(&sd_index_lock);
        ida_remove(&sd_index_ida, index);
+       spin_unlock(&sd_index_lock);
  out_put:
        put_disk(gd);
  out_free:
@@ -1974,7 +1991,9 @@ static void scsi_disk_release(struct device *dev)
        struct scsi_disk *sdkp = to_scsi_disk(dev);
        struct gendisk *disk = sdkp->disk;
        
+       spin_lock(&sd_index_lock);
        ida_remove(&sd_index_ida, sdkp->index);
+       spin_unlock(&sd_index_lock);
 
        disk->private_data = NULL;
        put_disk(disk);
@@ -2000,7 +2019,7 @@ static int sd_start_stop_device(struct scsi_disk *sdkp, int start)
                return -ENODEV;
 
        res = scsi_execute_req(sdp, cmd, DMA_NONE, NULL, 0, &sshdr,
-                              SD_TIMEOUT, SD_MAX_RETRIES);
+                              SD_TIMEOUT, SD_MAX_RETRIES, NULL);
        if (res) {
                sd_printk(KERN_WARNING, sdkp, "START_STOP FAILED\n");
                sd_print_result(sdkp, res);