Fedora kernel-2.6.17-1.2142_FC4 patched with stable patch-2.6.17.4-vs2.0.2-rc26.diff
[linux-2.6.git] / drivers / scsi / scsi_ioctl.c
index e26adda..a89aff6 100644 (file)
 #include <scsi/scsi_host.h>
 #include <scsi/scsi_ioctl.h>
 #include <scsi/scsi_request.h>
+#include <scsi/sg.h>
+#include <scsi/scsi_dbg.h>
 
 #include "scsi_logging.h"
 
 #define NORMAL_RETRIES                 5
 #define IOCTL_NORMAL_TIMEOUT                   (10 * HZ)
-#define FORMAT_UNIT_TIMEOUT            (2 * 60 * 60 * HZ)
-#define START_STOP_TIMEOUT             (60 * HZ)
-#define MOVE_MEDIUM_TIMEOUT            (5 * 60 * HZ)
-#define READ_ELEMENT_STATUS_TIMEOUT    (5 * 60 * HZ)
-#define READ_DEFECT_DATA_TIMEOUT       (60 * HZ )  /* ZIP-250 on parallel port takes as long! */
 
 #define MAX_BUF PAGE_SIZE
 
-/*
- * If we are told to probe a host, we will return 0 if  the host is not
- * present, 1 if the host is present, and will return an identifying
- * string at *arg, if arg is non null, filling to the length stored at
- * (int *) arg
+/**
+ * ioctl_probe  --  return host identification
+ * @host:      host to identify
+ * @buffer:    userspace buffer for identification
+ *
+ * Return an identifying string at @buffer, if @buffer is non-NULL, filling
+ * to the length stored at * (int *) @buffer.
  */
-
 static int ioctl_probe(struct Scsi_Host *host, void __user *buffer)
 {
        unsigned int len, slen;
        const char *string;
-       int temp = host->hostt->present;
 
-       if (temp && buffer) {
+       if (buffer) {
                if (get_user(len, (unsigned int __user *) buffer))
                        return -EFAULT;
 
@@ -62,7 +59,7 @@ static int ioctl_probe(struct Scsi_Host *host, void __user *buffer)
                                return -EFAULT;
                }
        }
-       return temp;
+       return 1;
 }
 
 /*
@@ -91,58 +88,49 @@ static int ioctl_probe(struct Scsi_Host *host, void __user *buffer)
 static int ioctl_internal_command(struct scsi_device *sdev, char *cmd,
                                  int timeout, int retries)
 {
-       struct scsi_request *sreq;
        int result;
+       struct scsi_sense_hdr sshdr;
 
        SCSI_LOG_IOCTL(1, printk("Trying ioctl with scsi command %d\n", *cmd));
 
-       sreq = scsi_allocate_request(sdev, GFP_KERNEL);
-       if (!sreq) {
-               printk("SCSI internal ioctl failed, no memory\n");
-               return -ENOMEM;
-       }
-
-       sreq->sr_data_direction = DMA_NONE;
-        scsi_wait_req(sreq, cmd, NULL, 0, timeout, retries);
+       result = scsi_execute_req(sdev, cmd, DMA_NONE, NULL, 0,
+                                 &sshdr, timeout, retries);
 
-       SCSI_LOG_IOCTL(2, printk("Ioctl returned  0x%x\n", sreq->sr_result));
+       SCSI_LOG_IOCTL(2, printk("Ioctl returned  0x%x\n", result));
 
-       if (driver_byte(sreq->sr_result)) {
-               switch (sreq->sr_sense_buffer[2] & 0xf) {
+       if ((driver_byte(result) & DRIVER_SENSE) &&
+           (scsi_sense_valid(&sshdr))) {
+               switch (sshdr.sense_key) {
                case ILLEGAL_REQUEST:
                        if (cmd[0] == ALLOW_MEDIUM_REMOVAL)
                                sdev->lockable = 0;
                        else
-                               printk("SCSI device (ioctl) reports ILLEGAL REQUEST.\n");
+                               printk(KERN_INFO "ioctl_internal_command: "
+                                      "ILLEGAL REQUEST asc=0x%x ascq=0x%x\n",
+                                      sshdr.asc, sshdr.ascq);
                        break;
                case NOT_READY: /* This happens if there is no disc in drive */
                        if (sdev->removable && (cmd[0] != TEST_UNIT_READY)) {
-                               printk(KERN_INFO "Device not ready.  Make sure there is a disc in the drive.\n");
+                               printk(KERN_INFO "Device not ready. Make sure"
+                                      " there is a disc in the drive.\n");
                                break;
                        }
                case UNIT_ATTENTION:
                        if (sdev->removable) {
                                sdev->changed = 1;
-                               sreq->sr_result = 0;    /* This is no longer considered an error */
+                               result = 0;     /* This is no longer considered an error */
                                break;
                        }
                default:        /* Fall through for non-removable media */
-                       printk("SCSI error: host %d id %d lun %d return code = %x\n",
-                              sdev->host->host_no,
-                              sdev->id,
-                              sdev->lun,
-                              sreq->sr_result);
-                       printk("\tSense class %x, sense error %x, extended sense %x\n",
-                              sense_class(sreq->sr_sense_buffer[0]),
-                              sense_error(sreq->sr_sense_buffer[0]),
-                              sreq->sr_sense_buffer[2] & 0xf);
-
+                       sdev_printk(KERN_INFO, sdev,
+                                   "ioctl_internal_command return code = %x\n",
+                                   result);
+                       scsi_print_sense_hdr("   ", &sshdr);
+                       break;
                }
        }
 
-       result = sreq->sr_result;
        SCSI_LOG_IOCTL(2, printk("IOCTL Releasing command\n"));
-       scsi_release_request(sreq);
        return result;
 }
 
@@ -167,187 +155,7 @@ int scsi_set_medium_removal(struct scsi_device *sdev, char state)
                sdev->locked = (state == SCSI_REMOVAL_PREVENT);
        return ret;
 }
-
-/*
- * This interface is deprecated - users should use the scsi generic (sg)
- * interface instead, as this is a more flexible approach to performing
- * generic SCSI commands on a device.
- *
- * The structure that we are passed should look like:
- *
- * struct sdata {
- *  unsigned int inlen;      [i] Length of data to be written to device 
- *  unsigned int outlen;     [i] Length of data to be read from device 
- *  unsigned char cmd[x];    [i] SCSI command (6 <= x <= 12).
- *                           [o] Data read from device starts here.
- *                           [o] On error, sense buffer starts here.
- *  unsigned char wdata[y];  [i] Data written to device starts here.
- * };
- * Notes:
- *   -  The SCSI command length is determined by examining the 1st byte
- *      of the given command. There is no way to override this.
- *   -  Data transfers are limited to PAGE_SIZE (4K on i386, 8K on alpha).
- *   -  The length (x + y) must be at least OMAX_SB_LEN bytes long to
- *      accommodate the sense buffer when an error occurs.
- *      The sense buffer is truncated to OMAX_SB_LEN (16) bytes so that
- *      old code will not be surprised.
- *   -  If a Unix error occurs (e.g. ENOMEM) then the user will receive
- *      a negative return and the Unix error code in 'errno'. 
- *      If the SCSI command succeeds then 0 is returned.
- *      Positive numbers returned are the compacted SCSI error codes (4 
- *      bytes in one int) where the lowest byte is the SCSI status.
- *      See the drivers/scsi/scsi.h file for more information on this.
- *
- */
-#define OMAX_SB_LEN 16         /* Old sense buffer length */
-
-int scsi_ioctl_send_command(struct scsi_device *sdev,
-                           struct scsi_ioctl_command __user *sic)
-{
-       char *buf;
-       unsigned char cmd[MAX_COMMAND_SIZE];
-       char __user *cmd_in;
-       struct scsi_request *sreq;
-       unsigned char opcode;
-       unsigned int inlen, outlen, cmdlen;
-       unsigned int needed, buf_needed;
-       int timeout, retries, result;
-       int data_direction, gfp_mask = GFP_KERNEL;
-
-       if (!sic)
-               return -EINVAL;
-
-       if (sdev->host->unchecked_isa_dma)
-               gfp_mask |= GFP_DMA;
-
-       /*
-        * Verify that we can read at least this much.
-        */
-       if (verify_area(VERIFY_READ, sic, sizeof(Scsi_Ioctl_Command)))
-               return -EFAULT;
-
-       if(__get_user(inlen, &sic->inlen))
-               return -EFAULT;
-               
-       if(__get_user(outlen, &sic->outlen))
-               return -EFAULT;
-
-       /*
-        * We do not transfer more than MAX_BUF with this interface.
-        * If the user needs to transfer more data than this, they
-        * should use scsi_generics (sg) instead.
-        */
-       if (inlen > MAX_BUF)
-               return -EINVAL;
-       if (outlen > MAX_BUF)
-               return -EINVAL;
-
-       cmd_in = sic->data;
-       if(get_user(opcode, cmd_in))
-               return -EFAULT;
-
-       needed = buf_needed = (inlen > outlen ? inlen : outlen);
-       if (buf_needed) {
-               buf_needed = (buf_needed + 511) & ~511;
-               if (buf_needed > MAX_BUF)
-                       buf_needed = MAX_BUF;
-               buf = kmalloc(buf_needed, gfp_mask);
-               if (!buf)
-                       return -ENOMEM;
-               memset(buf, 0, buf_needed);
-               if (inlen == 0) {
-                       data_direction = DMA_FROM_DEVICE;
-               } else if (outlen == 0 ) {
-                       data_direction = DMA_TO_DEVICE;
-               } else {
-                       /*
-                        * Can this ever happen?
-                        */
-                       data_direction = DMA_BIDIRECTIONAL;
-               }
-
-       } else {
-               buf = NULL;
-               data_direction = DMA_NONE;
-       }
-
-       /*
-        * Obtain the command from the user's address space.
-        */
-       cmdlen = COMMAND_SIZE(opcode);
-       
-       result = -EFAULT;
-
-       if (verify_area(VERIFY_READ, cmd_in, cmdlen + inlen))
-               goto error;
-
-       if(__copy_from_user(cmd, cmd_in, cmdlen))
-               goto error;
-
-       /*
-        * Obtain the data to be sent to the device (if any).
-        */
-
-       if(copy_from_user(buf, cmd_in + cmdlen, inlen))
-               goto error;
-
-       switch (opcode) {
-       case SEND_DIAGNOSTIC:
-       case FORMAT_UNIT:
-               timeout = FORMAT_UNIT_TIMEOUT;
-               retries = 1;
-               break;
-       case START_STOP:
-               timeout = START_STOP_TIMEOUT;
-               retries = NORMAL_RETRIES;
-               break;
-       case MOVE_MEDIUM:
-               timeout = MOVE_MEDIUM_TIMEOUT;
-               retries = NORMAL_RETRIES;
-               break;
-       case READ_ELEMENT_STATUS:
-               timeout = READ_ELEMENT_STATUS_TIMEOUT;
-               retries = NORMAL_RETRIES;
-               break;
-       case READ_DEFECT_DATA:
-               timeout = READ_DEFECT_DATA_TIMEOUT;
-               retries = 1;
-               break;
-       default:
-               timeout = IOCTL_NORMAL_TIMEOUT;
-               retries = NORMAL_RETRIES;
-               break;
-       }
-
-       sreq = scsi_allocate_request(sdev, GFP_KERNEL);
-        if (!sreq) {
-                result = -EINTR;
-                goto error;
-        }
-
-       sreq->sr_data_direction = data_direction;
-        scsi_wait_req(sreq, cmd, buf, needed, timeout, retries);
-
-       /* 
-        * If there was an error condition, pass the info back to the user. 
-        */
-       result = sreq->sr_result;
-       if (result) {
-               int sb_len = sizeof(sreq->sr_sense_buffer);
-
-               sb_len = (sb_len > OMAX_SB_LEN) ? OMAX_SB_LEN : sb_len;
-               if (copy_to_user(cmd_in, sreq->sr_sense_buffer, sb_len))
-                       result = -EFAULT;
-       } else {
-               if (copy_to_user(cmd_in, buf, outlen))
-                       result = -EFAULT;
-       }       
-
-       scsi_release_request(sreq);
-error:
-       kfree(buf);
-       return result;
-}
+EXPORT_SYMBOL(scsi_set_medium_removal);
 
 /*
  * The scsi_ioctl_get_pci() function places into arg the value
@@ -400,7 +208,8 @@ int scsi_ioctl(struct scsi_device *sdev, int cmd, void __user *arg)
        case SCSI_IOCTL_SYNC:
        case SCSI_IOCTL_START_UNIT:
        case SCSI_IOCTL_STOP_UNIT:
-               printk(KERN_WARNING "program %s is using a deprecated SCSI ioctl, please convert it to SG_IO\n", current->comm);
+               printk(KERN_WARNING "program %s is using a deprecated SCSI "
+                      "ioctl, please convert it to SG_IO\n", current->comm);
                break;
        default:
                break;
@@ -408,7 +217,7 @@ int scsi_ioctl(struct scsi_device *sdev, int cmd, void __user *arg)
 
        switch (cmd) {
        case SCSI_IOCTL_GET_IDLUN:
-               if (verify_area(VERIFY_WRITE, arg, sizeof(struct scsi_idlun)))
+               if (!access_ok(VERIFY_WRITE, arg, sizeof(struct scsi_idlun)))
                        return -EFAULT;
 
                __put_user((sdev->id & 0xff)
@@ -426,7 +235,7 @@ int scsi_ioctl(struct scsi_device *sdev, int cmd, void __user *arg)
        case SCSI_IOCTL_SEND_COMMAND:
                if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
                        return -EACCES;
-               return scsi_ioctl_send_command(sdev, arg);
+               return sg_scsi_ioctl(NULL, sdev->request_queue, NULL, arg);
        case SCSI_IOCTL_DOORLOCK:
                return scsi_set_medium_removal(sdev, SCSI_REMOVAL_PREVENT);
        case SCSI_IOCTL_DOORUNLOCK:
@@ -456,3 +265,51 @@ int scsi_ioctl(struct scsi_device *sdev, int cmd, void __user *arg)
        }
        return -EINVAL;
 }
+EXPORT_SYMBOL(scsi_ioctl);
+
+/*
+ * the scsi_nonblock_ioctl() function is designed for ioctls which may
+ * be executed even if the device is in recovery.
+ */
+int scsi_nonblockable_ioctl(struct scsi_device *sdev, int cmd,
+                           void __user *arg, struct file *filp)
+{
+       int val, result;
+
+       /* The first set of iocts may be executed even if we're doing
+        * error processing, as long as the device was opened
+        * non-blocking */
+       if (filp && filp->f_flags & O_NONBLOCK) {
+               if (scsi_host_in_recovery(sdev->host))
+                       return -ENODEV;
+       } else if (!scsi_block_when_processing_errors(sdev))
+               return -ENODEV;
+
+       switch (cmd) {
+       case SG_SCSI_RESET:
+               result = get_user(val, (int __user *)arg);
+               if (result)
+                       return result;
+               if (val == SG_SCSI_RESET_NOTHING)
+                       return 0;
+               switch (val) {
+               case SG_SCSI_RESET_DEVICE:
+                       val = SCSI_TRY_RESET_DEVICE;
+                       break;
+               case SG_SCSI_RESET_BUS:
+                       val = SCSI_TRY_RESET_BUS;
+                       break;
+               case SG_SCSI_RESET_HOST:
+                       val = SCSI_TRY_RESET_HOST;
+                       break;
+               default:
+                       return -EINVAL;
+               }
+               if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
+                       return -EACCES;
+               return (scsi_reset_provider(sdev, val) ==
+                       SUCCESS) ? 0 : -EIO;
+       }
+       return -ENODEV;
+}
+EXPORT_SYMBOL(scsi_nonblockable_ioctl);