]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - drivers/scsi/libsas/sas_scsi_host.c
[SCSI] libsas: export sas_find_local_phy function
[linux-2.6-omap-h63xx.git] / drivers / scsi / libsas / sas_scsi_host.c
index d70ddfda93fc968cd2d001f80f0a3c1487b32a8c..f3706f4b79ce16083197f277010d8573479098b7 100644 (file)
 #include <scsi/scsi_eh.h>
 #include <scsi/scsi_transport.h>
 #include <scsi/scsi_transport_sas.h>
+#include <scsi/sas_ata.h>
 #include "../scsi_sas_internal.h"
 #include "../scsi_transport_api.h"
 #include "../scsi_priv.h"
 
 #include <linux/err.h>
 #include <linux/blkdev.h>
+#include <linux/freezer.h>
 #include <linux/scatterlist.h>
+#include <linux/libata.h>
 
 /* ---------- SCSI Host glue ---------- */
 
-#define TO_SAS_TASK(_scsi_cmd)  ((void *)(_scsi_cmd)->host_scribble)
-#define ASSIGN_SAS_TASK(_sc, _t) do { (_sc)->host_scribble = (void *) _t; } while (0)
-
 static void sas_scsi_task_done(struct sas_task *task)
 {
        struct task_status_struct *ts = &task->task_status;
        struct scsi_cmnd *sc = task->uldd_task;
-       struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(sc->device->host);
-       unsigned ts_flags = task->task_state_flags;
        int hs = 0, stat = 0;
 
+       if (unlikely(task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
+               /* Aborted tasks will be completed by the error handler */
+               SAS_DPRINTK("task done but aborted\n");
+               return;
+       }
+
        if (unlikely(!sc)) {
                SAS_DPRINTK("task_done called with non existing SCSI cmnd!\n");
                list_del_init(&task->list);
@@ -108,7 +112,7 @@ static void sas_scsi_task_done(struct sas_task *task)
                        break;
                case SAM_CHECK_COND:
                        memcpy(sc->sense_buffer, ts->buf,
-                              max(SCSI_SENSE_BUFFERSIZE, ts->buf_valid_size));
+                              min(SCSI_SENSE_BUFFERSIZE, ts->buf_valid_size));
                        stat = SAM_CHECK_COND;
                        break;
                default:
@@ -120,11 +124,7 @@ static void sas_scsi_task_done(struct sas_task *task)
        sc->result = (hs << 16) | stat;
        list_del_init(&task->list);
        sas_free_task(task);
-       /* This is very ugly but this is how SCSI Core works. */
-       if (ts_flags & SAS_TASK_STATE_ABORTED)
-               scsi_eh_finish_cmd(sc, &sas_ha->eh_done_q);
-       else
-               sc->scsi_done(sc);
+       sc->scsi_done(sc);
 }
 
 static enum task_attribute sas_scsi_get_task_attr(struct scsi_cmnd *cmd)
@@ -148,7 +148,6 @@ static struct sas_task *sas_create_task(struct scsi_cmnd *cmd,
        if (!task)
                return NULL;
 
-       *(u32 *)cmd->sense_buffer = 0;
        task->uldd_task = cmd;
        ASSIGN_SAS_TASK(cmd, task);
 
@@ -171,7 +170,7 @@ static struct sas_task *sas_create_task(struct scsi_cmnd *cmd,
        return task;
 }
 
-static int sas_queue_up(struct sas_task *task)
+int sas_queue_up(struct sas_task *task)
 {
        struct sas_ha_struct *sas_ha = task->dev->port->ha;
        struct scsi_core *core = &sas_ha->core;
@@ -200,6 +199,10 @@ static int sas_queue_up(struct sas_task *task)
  */
 int sas_queuecommand(struct scsi_cmnd *cmd,
                     void (*scsi_done)(struct scsi_cmnd *))
+       __releases(host->host_lock)
+       __acquires(dev->sata_dev.ap->lock)
+       __releases(dev->sata_dev.ap->lock)
+       __acquires(host->host_lock)
 {
        int res = 0;
        struct domain_device *dev = cmd_to_domain_dev(cmd);
@@ -212,6 +215,16 @@ int sas_queuecommand(struct scsi_cmnd *cmd,
                struct sas_ha_struct *sas_ha = dev->port->ha;
                struct sas_task *task;
 
+               if (dev_is_sata(dev)) {
+                       unsigned long flags;
+
+                       spin_lock_irqsave(dev->sata_dev.ap->lock, flags);
+                       res = ata_sas_queuecmd(cmd, scsi_done,
+                                              dev->sata_dev.ap);
+                       spin_unlock_irqrestore(dev->sata_dev.ap->lock, flags);
+                       goto out;
+               }
+
                res = -ENOMEM;
                task = sas_create_task(cmd, dev, GFP_ATOMIC);
                if (!task)
@@ -242,13 +255,34 @@ out:
        return res;
 }
 
+static void sas_eh_finish_cmd(struct scsi_cmnd *cmd)
+{
+       struct sas_task *task = TO_SAS_TASK(cmd);
+       struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(cmd->device->host);
+
+       /* remove the aborted task flag to allow the task to be
+        * completed now. At this point, we only get called following
+        * an actual abort of the task, so we should be guaranteed not
+        * to be racing with any completions from the LLD (hence we
+        * don't need the task state lock to clear the flag) */
+       task->task_state_flags &= ~SAS_TASK_STATE_ABORTED;
+       /* Now call task_done.  However, task will be free'd after
+        * this */
+       task->task_done(task);
+       /* now finish the command and move it on to the error
+        * handler done list, this also takes it off the
+        * error handler pending list */
+       scsi_eh_finish_cmd(cmd, &sas_ha->eh_done_q);
+}
+
 static void sas_scsi_clear_queue_lu(struct list_head *error_q, struct scsi_cmnd *my_cmd)
 {
        struct scsi_cmnd *cmd, *n;
 
        list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
-               if (cmd == my_cmd)
-                       list_del_init(&cmd->eh_entry);
+               if (cmd->device->sdev_target == my_cmd->device->sdev_target &&
+                   cmd->device->lun == my_cmd->device->lun)
+                       sas_eh_finish_cmd(cmd);
        }
 }
 
@@ -261,7 +295,7 @@ static void sas_scsi_clear_queue_I_T(struct list_head *error_q,
                struct domain_device *x = cmd_to_domain_dev(cmd);
 
                if (x == dev)
-                       list_del_init(&cmd->eh_entry);
+                       sas_eh_finish_cmd(cmd);
        }
 }
 
@@ -275,7 +309,7 @@ static void sas_scsi_clear_queue_port(struct list_head *error_q,
                struct asd_sas_port *x = dev->port;
 
                if (x == port)
-                       list_del_init(&cmd->eh_entry);
+                       sas_eh_finish_cmd(cmd);
        }
 }
 
@@ -400,7 +434,7 @@ static int sas_recover_I_T(struct domain_device *dev)
 }
 
 /* Find the sas_phy that's attached to this device */
-struct sas_phy *find_local_sas_phy(struct domain_device *dev)
+struct sas_phy *sas_find_local_phy(struct domain_device *dev)
 {
        struct domain_device *pdev = dev->parent;
        struct ex_phy *exphy = NULL;
@@ -422,6 +456,7 @@ struct sas_phy *find_local_sas_phy(struct domain_device *dev)
        BUG_ON(!exphy);
        return exphy->phy;
 }
+EXPORT_SYMBOL_GPL(sas_find_local_phy);
 
 /* Attempt to send a LUN reset message to a device */
 int sas_eh_device_reset_handler(struct scsi_cmnd *cmd)
@@ -448,13 +483,13 @@ int sas_eh_device_reset_handler(struct scsi_cmnd *cmd)
 int sas_eh_bus_reset_handler(struct scsi_cmnd *cmd)
 {
        struct domain_device *dev = cmd_to_domain_dev(cmd);
-       struct sas_phy *phy = find_local_sas_phy(dev);
+       struct sas_phy *phy = sas_find_local_phy(dev);
        int res;
 
        res = sas_phy_reset(phy, 1);
        if (res)
                SAS_DPRINTK("Bus reset of %s failed 0x%x\n",
-                           phy->dev.kobj.k_name,
+                           kobject_name(&phy->dev.kobj),
                            res);
        if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
                return SUCCESS;
@@ -515,14 +550,14 @@ Again:
                case TASK_IS_DONE:
                        SAS_DPRINTK("%s: task 0x%p is done\n", __FUNCTION__,
                                    task);
-                       task->task_done(task);
+                       sas_eh_finish_cmd(cmd);
                        if (need_reset)
                                try_to_reset_cmd_device(shost, cmd);
                        continue;
                case TASK_IS_ABORTED:
                        SAS_DPRINTK("%s: task 0x%p is aborted\n",
                                    __FUNCTION__, task);
-                       task->task_done(task);
+                       sas_eh_finish_cmd(cmd);
                        if (need_reset)
                                try_to_reset_cmd_device(shost, cmd);
                        continue;
@@ -534,7 +569,7 @@ Again:
                                            "recovered\n",
                                            SAS_ADDR(task->dev),
                                            cmd->device->lun);
-                               task->task_done(task);
+                               sas_eh_finish_cmd(cmd);
                                if (need_reset)
                                        try_to_reset_cmd_device(shost, cmd);
                                sas_scsi_clear_queue_lu(work_q, cmd);
@@ -549,7 +584,7 @@ Again:
                        if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
                                SAS_DPRINTK("I_T %016llx recovered\n",
                                            SAS_ADDR(task->dev->sas_addr));
-                               task->task_done(task);
+                               sas_eh_finish_cmd(cmd);
                                if (need_reset)
                                        try_to_reset_cmd_device(shost, cmd);
                                sas_scsi_clear_queue_I_T(work_q, task->dev);
@@ -564,7 +599,7 @@ Again:
                                if (res == TMF_RESP_FUNC_COMPLETE) {
                                        SAS_DPRINTK("clear nexus port:%d "
                                                    "succeeded\n", port->id);
-                                       task->task_done(task);
+                                       sas_eh_finish_cmd(cmd);
                                        if (need_reset)
                                                try_to_reset_cmd_device(shost, cmd);
                                        sas_scsi_clear_queue_port(work_q,
@@ -578,10 +613,10 @@ Again:
                                if (res == TMF_RESP_FUNC_COMPLETE) {
                                        SAS_DPRINTK("clear nexus ha "
                                                    "succeeded\n");
-                                       task->task_done(task);
+                                       sas_eh_finish_cmd(cmd);
                                        if (need_reset)
                                                try_to_reset_cmd_device(shost, cmd);
-                                       goto out;
+                                       goto clear_q;
                                }
                        }
                        /* If we are here -- this means that no amount
@@ -593,21 +628,18 @@ Again:
                                    SAS_ADDR(task->dev->sas_addr),
                                    cmd->device->lun);
 
-                       task->task_done(task);
+                       sas_eh_finish_cmd(cmd);
                        if (need_reset)
                                try_to_reset_cmd_device(shost, cmd);
                        goto clear_q;
                }
        }
-out:
        return list_empty(work_q);
 clear_q:
        SAS_DPRINTK("--- Exit %s -- clear_q\n", __FUNCTION__);
-       list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
-               struct sas_task *task = TO_SAS_TASK(cmd);
-               list_del_init(&cmd->eh_entry);
-               task->task_done(task);
-       }
+       list_for_each_entry_safe(cmd, n, work_q, eh_entry)
+               sas_eh_finish_cmd(cmd);
+
        return list_empty(work_q);
 }
 
@@ -683,6 +715,16 @@ enum scsi_eh_timer_return sas_scsi_timed_out(struct scsi_cmnd *cmd)
        return EH_NOT_HANDLED;
 }
 
+int sas_ioctl(struct scsi_device *sdev, int cmd, void __user *arg)
+{
+       struct domain_device *dev = sdev_to_domain_dev(sdev);
+
+       if (dev_is_sata(dev))
+               return ata_scsi_ioctl(sdev, cmd, arg);
+
+       return -EINVAL;
+}
+
 struct domain_device *sas_find_dev_by_rphy(struct sas_rphy *rphy)
 {
        struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent);
@@ -722,10 +764,17 @@ static inline struct domain_device *sas_find_target(struct scsi_target *starget)
 int sas_target_alloc(struct scsi_target *starget)
 {
        struct domain_device *found_dev = sas_find_target(starget);
+       int res;
 
        if (!found_dev)
                return -ENODEV;
 
+       if (dev_is_sata(found_dev)) {
+               res = sas_ata_init_host_and_port(found_dev, starget);
+               if (res)
+                       return res;
+       }
+
        starget->hostdata = found_dev;
        return 0;
 }
@@ -740,6 +789,11 @@ int sas_slave_configure(struct scsi_device *scsi_dev)
 
        BUG_ON(dev->rphy->identify.device_type != SAS_END_DEVICE);
 
+       if (dev_is_sata(dev)) {
+               ata_sas_slave_configure(scsi_dev, dev->sata_dev.ap);
+               return 0;
+       }
+
        sas_ha = dev->port->ha;
 
        sas_read_port_mode_page(scsi_dev);
@@ -763,6 +817,10 @@ int sas_slave_configure(struct scsi_device *scsi_dev)
 
 void sas_slave_destroy(struct scsi_device *scsi_dev)
 {
+       struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
+
+       if (dev_is_sata(dev))
+               ata_port_disable(dev->sata_dev.ap);
 }
 
 int sas_change_queue_depth(struct scsi_device *scsi_dev, int new_depth)
@@ -868,8 +926,6 @@ static int sas_queue_thread(void *_sas_ha)
 {
        struct sas_ha_struct *sas_ha = _sas_ha;
 
-       current->flags |= PF_NOFREEZE;
-
        while (1) {
                set_current_state(TASK_INTERRUPTIBLE);
                schedule();
@@ -981,10 +1037,38 @@ void sas_task_abort(struct sas_task *task)
                return;
        }
 
+       if (dev_is_sata(task->dev)) {
+               sas_ata_task_abort(task);
+               return;
+       }
+
        scsi_req_abort_cmd(sc);
        scsi_schedule_eh(sc->device->host);
 }
 
+int sas_slave_alloc(struct scsi_device *scsi_dev)
+{
+       struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
+
+       if (dev_is_sata(dev))
+               return ata_sas_port_init(dev->sata_dev.ap);
+
+       return 0;
+}
+
+void sas_target_destroy(struct scsi_target *starget)
+{
+       struct domain_device *found_dev = sas_find_target(starget);
+
+       if (!found_dev)
+               return;
+
+       if (dev_is_sata(found_dev))
+               ata_sas_port_destroy(found_dev->sata_dev.ap);
+
+       return;
+}
+
 EXPORT_SYMBOL_GPL(sas_queuecommand);
 EXPORT_SYMBOL_GPL(sas_target_alloc);
 EXPORT_SYMBOL_GPL(sas_slave_configure);
@@ -998,3 +1082,6 @@ EXPORT_SYMBOL_GPL(sas_phy_reset);
 EXPORT_SYMBOL_GPL(sas_phy_enable);
 EXPORT_SYMBOL_GPL(sas_eh_device_reset_handler);
 EXPORT_SYMBOL_GPL(sas_eh_bus_reset_handler);
+EXPORT_SYMBOL_GPL(sas_slave_alloc);
+EXPORT_SYMBOL_GPL(sas_target_destroy);
+EXPORT_SYMBOL_GPL(sas_ioctl);