libata and starting/stopping ATAPI floppy devices
[pandora-kernel.git] / drivers / ata / libata-scsi.c
index 9fbb39c..264ae60 100644 (file)
@@ -45,7 +45,7 @@
 #include <scsi/scsi_transport.h>
 #include <linux/libata.h>
 #include <linux/hdreg.h>
-#include <asm/uaccess.h>
+#include <linux/uaccess.h>
 
 #include "libata.h"
 
@@ -53,9 +53,9 @@
 
 typedef unsigned int (*ata_xlat_func_t)(struct ata_queued_cmd *qc);
 
-static struct ata_device * __ata_scsi_find_dev(struct ata_port *ap,
+static struct ata_device *__ata_scsi_find_dev(struct ata_port *ap,
                                        const struct scsi_device *scsidev);
-static struct ata_device * ata_scsi_find_dev(struct ata_port *ap,
+static struct ata_device *ata_scsi_find_dev(struct ata_port *ap,
                                            const struct scsi_device *scsidev);
 static int ata_scsi_user_scan(struct Scsi_Host *shost, unsigned int channel,
                              unsigned int id, unsigned int lun);
@@ -110,6 +110,74 @@ static struct scsi_transport_template ata_scsi_transport_template = {
 };
 
 
+static const struct {
+       enum link_pm    value;
+       const char      *name;
+} link_pm_policy[] = {
+       { NOT_AVAILABLE, "max_performance" },
+       { MIN_POWER, "min_power" },
+       { MAX_PERFORMANCE, "max_performance" },
+       { MEDIUM_POWER, "medium_power" },
+};
+
+static const char *ata_scsi_lpm_get(enum link_pm policy)
+{
+       int i;
+
+       for (i = 0; i < ARRAY_SIZE(link_pm_policy); i++)
+               if (link_pm_policy[i].value == policy)
+                       return link_pm_policy[i].name;
+
+       return NULL;
+}
+
+static ssize_t ata_scsi_lpm_put(struct class_device *class_dev,
+       const char *buf, size_t count)
+{
+       struct Scsi_Host *shost = class_to_shost(class_dev);
+       struct ata_port *ap = ata_shost_to_port(shost);
+       enum link_pm policy = 0;
+       int i;
+
+       /*
+        * we are skipping array location 0 on purpose - this
+        * is because a value of NOT_AVAILABLE is displayed
+        * to the user as max_performance, but when the user
+        * writes "max_performance", they actually want the
+        * value to match MAX_PERFORMANCE.
+        */
+       for (i = 1; i < ARRAY_SIZE(link_pm_policy); i++) {
+               const int len = strlen(link_pm_policy[i].name);
+               if (strncmp(link_pm_policy[i].name, buf, len) == 0 &&
+                  buf[len] == '\n') {
+                       policy = link_pm_policy[i].value;
+                       break;
+               }
+       }
+       if (!policy)
+               return -EINVAL;
+
+       ata_lpm_schedule(ap, policy);
+       return count;
+}
+
+static ssize_t
+ata_scsi_lpm_show(struct class_device *class_dev, char *buf)
+{
+       struct Scsi_Host *shost = class_to_shost(class_dev);
+       struct ata_port *ap = ata_shost_to_port(shost);
+       const char *policy =
+               ata_scsi_lpm_get(ap->pm_policy);
+
+       if (!policy)
+               return -EINVAL;
+
+       return snprintf(buf, 23, "%s\n", policy);
+}
+CLASS_DEVICE_ATTR(link_power_management_policy, S_IRUGO | S_IWUSR,
+               ata_scsi_lpm_show, ata_scsi_lpm_put);
+EXPORT_SYMBOL_GPL(class_device_attr_link_power_management_policy);
+
 static void ata_scsi_invalid_field(struct scsi_cmnd *cmd,
                                   void (*done)(struct scsi_cmnd *))
 {
@@ -228,7 +296,7 @@ int ata_cmd_ioctl(struct scsi_device *scsidev, void __user *arg)
 
                scsi_cmd[1]  = (4 << 1); /* PIO Data-in */
                scsi_cmd[2]  = 0x0e;     /* no off.line or cc, read from dev,
-                                           block count in sector count field */
+                                           block count in sector count field */
                data_dir = DMA_FROM_DEVICE;
        } else {
                scsi_cmd[1]  = (3 << 1); /* Non-data */
@@ -252,7 +320,7 @@ int ata_cmd_ioctl(struct scsi_device *scsidev, void __user *arg)
        /* Good values for timeout and retries?  Values below
           from scsi_ioctl_send_command() for default case... */
        cmd_result = scsi_execute(scsidev, scsi_cmd, data_dir, argbuf, argsize,
-                                 sensebuf, (10*HZ), 5, 0);
+                                 sensebuf, (10*HZ), 5, 0);
 
        if (driver_byte(cmd_result) == DRIVER_SENSE) {/* sense data available */
                u8 *desc = sensebuf + 8;
@@ -263,18 +331,18 @@ int ata_cmd_ioctl(struct scsi_device *scsidev, void __user *arg)
                if (cmd_result & SAM_STAT_CHECK_CONDITION) {
                        struct scsi_sense_hdr sshdr;
                        scsi_normalize_sense(sensebuf, SCSI_SENSE_BUFFERSIZE,
-                                             &sshdr);
-                       if (sshdr.sense_key==0 &&
-                           sshdr.asc==0 && sshdr.ascq==0)
+                                            &sshdr);
+                       if (sshdr.sense_key == 0 &&
+                           sshdr.asc == 0 && sshdr.ascq == 0)
                                cmd_result &= ~SAM_STAT_CHECK_CONDITION;
                }
 
                /* Send userspace a few ATA registers (same as drivers/ide) */
-               if (sensebuf[0] == 0x72 &&     /* format is "descriptor" */
-                   desc[0] == 0x09 ) {        /* code is "ATA Descriptor" */
-                       args[0] = desc[13];    /* status */
-                       args[1] = desc[3];     /* error */
-                       args[2] = desc[5];     /* sector count (0:7) */
+               if (sensebuf[0] == 0x72 &&      /* format is "descriptor" */
+                   desc[0] == 0x09) {          /* code is "ATA Descriptor" */
+                       args[0] = desc[13];     /* status */
+                       args[1] = desc[3];      /* error */
+                       args[2] = desc[5];      /* sector count (0:7) */
                        if (copy_to_user(arg, args, sizeof(args)))
                                rc = -EFAULT;
                }
@@ -350,8 +418,8 @@ int ata_task_ioctl(struct scsi_device *scsidev, void __user *arg)
                        struct scsi_sense_hdr sshdr;
                        scsi_normalize_sense(sensebuf, SCSI_SENSE_BUFFERSIZE,
                                                &sshdr);
-                       if (sshdr.sense_key==0 &&
-                               sshdr.asc==0 && sshdr.ascq==0)
+                       if (sshdr.sense_key == 0 &&
+                               sshdr.asc == 0 && sshdr.ascq == 0)
                                cmd_result &= ~SAM_STAT_CHECK_CONDITION;
                }
 
@@ -773,6 +841,9 @@ static void ata_scsi_dev_config(struct scsi_device *sdev,
                blk_queue_max_hw_segments(q, q->max_hw_segments - 1);
        }
 
+       if (dev->flags & ATA_DFLAG_AN)
+               set_bit(SDEV_EVT_MEDIA_CHANGE, sdev->supported_events);
+
        if (dev->flags & ATA_DFLAG_NCQ) {
                int depth;
 
@@ -801,7 +872,8 @@ int ata_scsi_slave_config(struct scsi_device *sdev)
 
        ata_scsi_sdev_config(sdev);
 
-       sdev->manage_start_stop = 1;
+       if (dev->class == ATA_DEV_ATA)
+               sdev->manage_start_stop = 1;
 
        if (dev)
                ata_scsi_dev_config(sdev, dev);
@@ -975,7 +1047,7 @@ static unsigned int ata_scsi_start_stop_xlat(struct ata_queued_cmd *qc)
                if ((qc->dev->flags & ATA_DFLAG_SPUNDOWN) &&
                    (system_state == SYSTEM_HALT ||
                     system_state == SYSTEM_POWER_OFF)) {
-                       static unsigned long warned = 0;
+                       static unsigned long warned;
 
                        if (!test_and_set_bit(0, &warned)) {
                                ata_dev_printk(qc->dev, KERN_WARNING,
@@ -1040,6 +1112,9 @@ static unsigned int ata_scsi_flush_xlat(struct ata_queued_cmd *qc)
        else
                tf->command = ATA_CMD_FLUSH;
 
+       /* flush is critical for IO integrity, consider it an IO command */
+       qc->flags |= ATA_QCFLAG_IO;
+
        return 0;
 }
 
@@ -1361,32 +1436,9 @@ nothing_to_do:
 static void ata_scsi_qc_complete(struct ata_queued_cmd *qc)
 {
        struct ata_port *ap = qc->ap;
-       struct ata_eh_info *ehi = &qc->dev->link->eh_info;
        struct scsi_cmnd *cmd = qc->scsicmd;
        u8 *cdb = cmd->cmnd;
-       int need_sense = (qc->err_mask != 0);
-
-       /* We snoop the SET_FEATURES - Write Cache ON/OFF command, and
-        * schedule EH_REVALIDATE operation to update the IDENTIFY DEVICE
-        * cache
-        */
-       if (ap->ops->error_handler && !need_sense) {
-               switch (qc->tf.command) {
-               case ATA_CMD_SET_FEATURES:
-                       if ((qc->tf.feature == SETFEATURES_WC_ON) ||
-                           (qc->tf.feature == SETFEATURES_WC_OFF)) {
-                               ehi->action |= ATA_EH_REVALIDATE;
-                               ata_port_schedule_eh(ap);
-                       }
-                       break;
-
-               case ATA_CMD_INIT_DEV_PARAMS: /* CHS translation changed */
-               case ATA_CMD_SET_MULTI: /* multi_count changed */
-                       ehi->action |= ATA_EH_REVALIDATE;
-                       ata_port_schedule_eh(ap);
-                       break;
-               }
-       }
+       int need_sense = (qc->err_mask != 0);
 
        /* For ATA pass thru (SAT) commands, generate a sense block if
         * user mandated it or if there's an error.  Note that if we
@@ -1396,7 +1448,7 @@ static void ata_scsi_qc_complete(struct ata_queued_cmd *qc)
         * was no error, SK, ASC and ASCQ will all be zero.
         */
        if (((cdb[0] == ATA_16) || (cdb[0] == ATA_12)) &&
-           ((cdb[2] & 0x20) || need_sense)) {
+           ((cdb[2] & 0x20) || need_sense)) {
                ata_gen_passthru_sense(qc);
        } else {
                if (!need_sense) {
@@ -1500,7 +1552,7 @@ static int ata_scsi_translate(struct ata_device *dev, struct scsi_cmnd *cmd,
        return 0;
 
 early_finish:
-        ata_qc_free(qc);
+       ata_qc_free(qc);
        qc->scsidone(cmd);
        DPRINTK("EXIT - early finish (good or error)\n");
        return 0;
@@ -1544,7 +1596,7 @@ static unsigned int ata_scsi_rbuf_get(struct scsi_cmnd *cmd, u8 **buf_out)
        struct scatterlist *sg = scsi_sglist(cmd);
 
        if (sg) {
-               buf = kmap_atomic(sg->page, KM_IRQ0) + sg->offset;
+               buf = kmap_atomic(sg_page(sg), KM_IRQ0) + sg->offset;
                buflen = sg->length;
        } else {
                buf = NULL;
@@ -1590,8 +1642,8 @@ static inline void ata_scsi_rbuf_put(struct scsi_cmnd *cmd, u8 *buf)
  */
 
 void ata_scsi_rbuf_fill(struct ata_scsi_args *args,
-                       unsigned int (*actor) (struct ata_scsi_args *args,
-                                          u8 *rbuf, unsigned int buflen))
+                       unsigned int (*actor) (struct ata_scsi_args *args,
+                                              u8 *rbuf, unsigned int buflen))
 {
        u8 *rbuf;
        unsigned int buflen, rc;
@@ -2140,7 +2192,7 @@ saving_not_supp:
  *     None.
  */
 unsigned int ata_scsiop_read_cap(struct ata_scsi_args *args, u8 *rbuf,
-                               unsigned int buflen)
+                                unsigned int buflen)
 {
        u64 last_lba = args->dev->n_sectors - 1; /* LBA of the last block */
 
@@ -2434,11 +2486,40 @@ static unsigned int atapi_xlat(struct ata_queued_cmd *qc)
        if (!using_pio && ata_check_atapi_dma(qc))
                using_pio = 1;
 
-       /* Some controller variants snoop this value for Packet transfers
-          to do state machine and FIFO management. Thus we want to set it
-          properly, and for DMA where it is effectively meaningless */
+       /* Some controller variants snoop this value for Packet
+        * transfers to do state machine and FIFO management.  Thus we
+        * want to set it properly, and for DMA where it is
+        * effectively meaningless.
+        */
        nbytes = min(qc->nbytes, (unsigned int)63 * 1024);
 
+       /* Most ATAPI devices which honor transfer chunk size don't
+        * behave according to the spec when odd chunk size which
+        * matches the transfer length is specified.  If the number of
+        * bytes to transfer is 2n+1.  According to the spec, what
+        * should happen is to indicate that 2n+1 is going to be
+        * transferred and transfer 2n+2 bytes where the last byte is
+        * padding.
+        *
+        * In practice, this doesn't happen.  ATAPI devices first
+        * indicate and transfer 2n bytes and then indicate and
+        * transfer 2 bytes where the last byte is padding.
+        *
+        * This inconsistency confuses several controllers which
+        * perform PIO using DMA such as Intel AHCIs and sil3124/32.
+        * These controllers use actual number of transferred bytes to
+        * update DMA poitner and transfer of 4n+2 bytes make those
+        * controller push DMA pointer by 4n+4 bytes because SATA data
+        * FISes are aligned to 4 bytes.  This causes data corruption
+        * and buffer overrun.
+        *
+        * Always setting nbytes to even number solves this problem
+        * because then ATAPI devices don't have to split data at 2n
+        * boundaries.
+        */
+       if (nbytes & 0x1)
+               nbytes++;
+
        qc->tf.lbam = (nbytes & 0xFF);
        qc->tf.lbah = (nbytes >> 8);
 
@@ -2464,7 +2545,7 @@ static unsigned int atapi_xlat(struct ata_queued_cmd *qc)
        return 0;
 }
 
-static struct ata_device * ata_find_dev(struct ata_port *ap, int devno)
+static struct ata_device *ata_find_dev(struct ata_port *ap, int devno)
 {
        if (ap->nr_pmp_links == 0) {
                if (likely(devno < ata_link_max_devices(&ap->link)))
@@ -2477,8 +2558,8 @@ static struct ata_device * ata_find_dev(struct ata_port *ap, int devno)
        return NULL;
 }
 
-static struct ata_device * __ata_scsi_find_dev(struct ata_port *ap,
-                                       const struct scsi_device *scsidev)
+static struct ata_device *__ata_scsi_find_dev(struct ata_port *ap,
+                                             const struct scsi_device *scsidev)
 {
        int devno;
 
@@ -2564,27 +2645,27 @@ static u8
 ata_scsi_map_proto(u8 byte1)
 {
        switch((byte1 & 0x1e) >> 1) {
-               case 3:         /* Non-data */
-                       return ATA_PROT_NODATA;
-
-               case 6:         /* DMA */
-               case 10:        /* UDMA Data-in */
-               case 11:        /* UDMA Data-Out */
-                       return ATA_PROT_DMA;
-
-               case 4:         /* PIO Data-in */
-               case 5:         /* PIO Data-out */
-                       return ATA_PROT_PIO;
-
-               case 0:         /* Hard Reset */
-               case 1:         /* SRST */
-               case 8:         /* Device Diagnostic */
-               case 9:         /* Device Reset */
-               case 7:         /* DMA Queued */
-               case 12:        /* FPDMA */
-               case 15:        /* Return Response Info */
-               default:        /* Reserved */
-                       break;
+       case 3:         /* Non-data */
+               return ATA_PROT_NODATA;
+
+       case 6:         /* DMA */
+       case 10:        /* UDMA Data-in */
+       case 11:        /* UDMA Data-Out */
+               return ATA_PROT_DMA;
+
+       case 4:         /* PIO Data-in */
+       case 5:         /* PIO Data-out */
+               return ATA_PROT_PIO;
+
+       case 0:         /* Hard Reset */
+       case 1:         /* SRST */
+       case 8:         /* Device Diagnostic */
+       case 9:         /* Device Reset */
+       case 7:         /* DMA Queued */
+       case 12:        /* FPDMA */
+       case 15:        /* Return Response Info */
+       default:        /* Reserved */
+               break;
        }
 
        return ATA_PROT_UNKNOWN;
@@ -2719,8 +2800,8 @@ static unsigned int ata_scsi_pass_thru(struct ata_queued_cmd *qc)
         */
        qc->nbytes = scsi_bufflen(scmd);
 
-       /* request result TF */
-       qc->flags |= ATA_QCFLAG_RESULT_TF;
+       /* request result TF and be quiet about device error */
+       qc->flags |= ATA_QCFLAG_RESULT_TF | ATA_QCFLAG_QUIET;
 
        return 0;
 
@@ -2818,7 +2899,8 @@ static inline int __ata_scsi_queuecmd(struct scsi_cmnd *scmd,
                xlat_func = NULL;
                if (likely((scsi_op != ATA_16) || !atapi_passthru16)) {
                        /* relay SCSI command to ATAPI device */
-                       if (unlikely(scmd->cmd_len > dev->cdb_len))
+                       int len = COMMAND_SIZE(scsi_op);
+                       if (unlikely(len > scmd->cmd_len || len > dev->cdb_len))
                                goto bad_cdb_len;
 
                        xlat_func = atapi_xlat;
@@ -2919,94 +3001,94 @@ void ata_scsi_simulate(struct ata_device *dev, struct scsi_cmnd *cmd,
        args.done = done;
 
        switch(scsicmd[0]) {
-               /* TODO: worth improving? */
-               case FORMAT_UNIT:
+       /* TODO: worth improving? */
+       case FORMAT_UNIT:
+               ata_scsi_invalid_field(cmd, done);
+               break;
+
+       case INQUIRY:
+               if (scsicmd[1] & 2)                /* is CmdDt set?  */
                        ata_scsi_invalid_field(cmd, done);
+               else if ((scsicmd[1] & 1) == 0)    /* is EVPD clear? */
+                       ata_scsi_rbuf_fill(&args, ata_scsiop_inq_std);
+               else switch (scsicmd[2]) {
+               case 0x00:
+                       ata_scsi_rbuf_fill(&args, ata_scsiop_inq_00);
                        break;
-
-               case INQUIRY:
-                       if (scsicmd[1] & 2)                /* is CmdDt set?  */
-                               ata_scsi_invalid_field(cmd, done);
-                       else if ((scsicmd[1] & 1) == 0)    /* is EVPD clear? */
-                               ata_scsi_rbuf_fill(&args, ata_scsiop_inq_std);
-                       else switch (scsicmd[2]) {
-                       case 0x00:
-                               ata_scsi_rbuf_fill(&args, ata_scsiop_inq_00);
-                               break;
-                       case 0x80:
-                               ata_scsi_rbuf_fill(&args, ata_scsiop_inq_80);
-                               break;
-                       case 0x83:
-                               ata_scsi_rbuf_fill(&args, ata_scsiop_inq_83);
-                               break;
-                       case 0x89:
-                               ata_scsi_rbuf_fill(&args, ata_scsiop_inq_89);
-                               break;
-                       default:
-                               ata_scsi_invalid_field(cmd, done);
-                               break;
-                       }
+               case 0x80:
+                       ata_scsi_rbuf_fill(&args, ata_scsiop_inq_80);
                        break;
-
-               case MODE_SENSE:
-               case MODE_SENSE_10:
-                       ata_scsi_rbuf_fill(&args, ata_scsiop_mode_sense);
+               case 0x83:
+                       ata_scsi_rbuf_fill(&args, ata_scsiop_inq_83);
                        break;
-
-               case MODE_SELECT:       /* unconditionally return */
-               case MODE_SELECT_10:    /* bad-field-in-cdb */
+               case 0x89:
+                       ata_scsi_rbuf_fill(&args, ata_scsiop_inq_89);
+                       break;
+               default:
                        ata_scsi_invalid_field(cmd, done);
                        break;
+               }
+               break;
+
+       case MODE_SENSE:
+       case MODE_SENSE_10:
+               ata_scsi_rbuf_fill(&args, ata_scsiop_mode_sense);
+               break;
+
+       case MODE_SELECT:       /* unconditionally return */
+       case MODE_SELECT_10:    /* bad-field-in-cdb */
+               ata_scsi_invalid_field(cmd, done);
+               break;
 
-               case READ_CAPACITY:
+       case READ_CAPACITY:
+               ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
+               break;
+
+       case SERVICE_ACTION_IN:
+               if ((scsicmd[1] & 0x1f) == SAI_READ_CAPACITY_16)
                        ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
-                       break;
+               else
+                       ata_scsi_invalid_field(cmd, done);
+               break;
 
-               case SERVICE_ACTION_IN:
-                       if ((scsicmd[1] & 0x1f) == SAI_READ_CAPACITY_16)
-                               ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
-                       else
-                               ata_scsi_invalid_field(cmd, done);
-                       break;
+       case REPORT_LUNS:
+               ata_scsi_rbuf_fill(&args, ata_scsiop_report_luns);
+               break;
 
-               case REPORT_LUNS:
-                       ata_scsi_rbuf_fill(&args, ata_scsiop_report_luns);
-                       break;
+       case REQUEST_SENSE:
+               ata_scsi_set_sense(cmd, 0, 0, 0);
+               cmd->result = (DRIVER_SENSE << 24);
+               done(cmd);
+               break;
 
-               case REQUEST_SENSE:
-                       ata_scsi_set_sense(cmd, 0, 0, 0);
-                       cmd->result = (DRIVER_SENSE << 24);
-                       done(cmd);
-                       break;
+       /* if we reach this, then writeback caching is disabled,
+        * turning this into a no-op.
+        */
+       case SYNCHRONIZE_CACHE:
+               /* fall through */
+
+       /* no-op's, complete with success */
+       case REZERO_UNIT:
+       case SEEK_6:
+       case SEEK_10:
+       case TEST_UNIT_READY:
+               ata_scsi_rbuf_fill(&args, ata_scsiop_noop);
+               break;
 
-               /* if we reach this, then writeback caching is disabled,
-                * turning this into a no-op.
-                */
-               case SYNCHRONIZE_CACHE:
-                       /* fall through */
-
-               /* no-op's, complete with success */
-               case REZERO_UNIT:
-               case SEEK_6:
-               case SEEK_10:
-               case TEST_UNIT_READY:
+       case SEND_DIAGNOSTIC:
+               tmp8 = scsicmd[1] & ~(1 << 3);
+               if ((tmp8 == 0x4) && (!scsicmd[3]) && (!scsicmd[4]))
                        ata_scsi_rbuf_fill(&args, ata_scsiop_noop);
-                       break;
-
-               case SEND_DIAGNOSTIC:
-                       tmp8 = scsicmd[1] & ~(1 << 3);
-                       if ((tmp8 == 0x4) && (!scsicmd[3]) && (!scsicmd[4]))
-                               ata_scsi_rbuf_fill(&args, ata_scsiop_noop);
-                       else
-                               ata_scsi_invalid_field(cmd, done);
-                       break;
+               else
+                       ata_scsi_invalid_field(cmd, done);
+               break;
 
-               /* all other commands */
-               default:
-                       ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, 0x20, 0x0);
-                       /* "Invalid command operation code" */
-                       done(cmd);
-                       break;
+       /* all other commands */
+       default:
+               ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, 0x20, 0x0);
+               /* "Invalid command operation code" */
+               done(cmd);
+               break;
        }
 }
 
@@ -3248,10 +3330,9 @@ static void ata_scsi_handle_link_detach(struct ata_link *link)
  */
 void ata_scsi_media_change_notify(struct ata_device *dev)
 {
-#ifdef OTHER_AN_PATCHES_HAVE_BEEN_APPLIED
        if (dev->sdev)
-               scsi_device_event_notify(dev->sdev, SDEV_MEDIA_CHANGE);
-#endif
+               sdev_evt_send_simple(dev->sdev, SDEV_EVT_MEDIA_CHANGE,
+                                    GFP_ATOMIC);
 }
 
 /**