2 * Serial Attached SCSI (SAS) class SCSI Host glue.
4 * Copyright (C) 2005 Adaptec, Inc. All rights reserved.
5 * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com>
7 * This file is licensed under GPLv2.
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of the
12 * License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
26 #include "sas_internal.h"
28 #include <scsi/scsi_host.h>
29 #include <scsi/scsi_device.h>
30 #include <scsi/scsi_tcq.h>
31 #include <scsi/scsi.h>
32 #include <scsi/scsi_eh.h>
33 #include <scsi/scsi_transport.h>
34 #include <scsi/scsi_transport_sas.h>
35 #include "../scsi_sas_internal.h"
36 #include "../scsi_transport_api.h"
37 #include "../scsi_priv.h"
39 #include <linux/err.h>
40 #include <linux/blkdev.h>
41 #include <linux/scatterlist.h>
43 /* ---------- SCSI Host glue ---------- */
45 #define TO_SAS_TASK(_scsi_cmd) ((void *)(_scsi_cmd)->host_scribble)
46 #define ASSIGN_SAS_TASK(_sc, _t) do { (_sc)->host_scribble = (void *) _t; } while (0)
48 static void sas_scsi_task_done(struct sas_task *task)
50 struct task_status_struct *ts = &task->task_status;
51 struct scsi_cmnd *sc = task->uldd_task;
52 struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(sc->device->host);
53 unsigned ts_flags = task->task_state_flags;
57 SAS_DPRINTK("task_done called with non existing SCSI cmnd!\n");
58 list_del_init(&task->list);
63 if (ts->resp == SAS_TASK_UNDELIVERED) {
66 } else { /* ts->resp == SAS_TASK_COMPLETE */
67 /* task delivered, what happened afterwards? */
69 case SAS_DEV_NO_RESPONSE:
76 case SAS_DATA_UNDERRUN:
77 sc->resid = ts->residual;
78 if (sc->request_bufflen - sc->resid < sc->underflow)
81 case SAS_DATA_OVERRUN:
85 hs = DID_SOFT_ERROR; /* retry */
87 case SAS_DEVICE_UNKNOWN:
94 if (ts->open_rej_reason == SAS_OREJ_RSVD_RETRY)
95 hs = DID_SOFT_ERROR; /* retry */
99 case SAS_PROTO_RESPONSE:
100 SAS_DPRINTK("LLDD:%s sent SAS_PROTO_RESP for an SSP "
101 "task; please report this\n",
102 task->dev->port->ha->sas_ha_name);
104 case SAS_ABORTED_TASK:
108 memcpy(sc->sense_buffer, ts->buf,
109 max(SCSI_SENSE_BUFFERSIZE, ts->buf_valid_size));
110 stat = SAM_CHECK_COND;
117 ASSIGN_SAS_TASK(sc, NULL);
118 sc->result = (hs << 16) | stat;
119 list_del_init(&task->list);
121 /* This is very ugly but this is how SCSI Core works. */
122 if (ts_flags & SAS_TASK_STATE_ABORTED)
123 scsi_eh_finish_cmd(sc, &sas_ha->eh_done_q);
128 static enum task_attribute sas_scsi_get_task_attr(struct scsi_cmnd *cmd)
130 enum task_attribute ta = TASK_ATTR_SIMPLE;
131 if (cmd->request && blk_rq_tagged(cmd->request)) {
132 if (cmd->device->ordered_tags &&
133 (cmd->request->cmd_flags & REQ_HARDBARRIER))
134 ta = TASK_ATTR_ORDERED;
139 static struct sas_task *sas_create_task(struct scsi_cmnd *cmd,
140 struct domain_device *dev,
143 struct sas_task *task = sas_alloc_task(gfp_flags);
149 *(u32 *)cmd->sense_buffer = 0;
150 task->uldd_task = cmd;
151 ASSIGN_SAS_TASK(cmd, task);
154 task->task_proto = task->dev->tproto; /* BUG_ON(!SSP) */
156 task->ssp_task.retry_count = 1;
157 int_to_scsilun(cmd->device->lun, &lun);
158 memcpy(task->ssp_task.LUN, &lun.scsi_lun, 8);
159 task->ssp_task.task_attr = sas_scsi_get_task_attr(cmd);
160 memcpy(task->ssp_task.cdb, cmd->cmnd, 16);
162 task->scatter = cmd->request_buffer;
163 task->num_scatter = cmd->use_sg;
164 task->total_xfer_len = cmd->request_bufflen;
165 task->data_dir = cmd->sc_data_direction;
167 task->task_done = sas_scsi_task_done;
172 static int sas_queue_up(struct sas_task *task)
174 struct sas_ha_struct *sas_ha = task->dev->port->ha;
175 struct scsi_core *core = &sas_ha->core;
179 spin_lock_irqsave(&core->task_queue_lock, flags);
180 if (sas_ha->lldd_queue_size < core->task_queue_size + 1) {
181 spin_unlock_irqrestore(&core->task_queue_lock, flags);
182 return -SAS_QUEUE_FULL;
184 list_add_tail(&task->list, &core->task_queue);
185 core->task_queue_size += 1;
186 spin_unlock_irqrestore(&core->task_queue_lock, flags);
187 up(&core->queue_thread_sema);
193 * sas_queuecommand -- Enqueue a command for processing
194 * @parameters: See SCSI Core documentation
196 * Note: XXX: Remove the host unlock/lock pair when SCSI Core can
197 * call us without holding an IRQ spinlock...
199 int sas_queuecommand(struct scsi_cmnd *cmd,
200 void (*scsi_done)(struct scsi_cmnd *))
203 struct domain_device *dev = cmd_to_domain_dev(cmd);
204 struct Scsi_Host *host = cmd->device->host;
205 struct sas_internal *i = to_sas_internal(host->transportt);
207 spin_unlock_irq(host->host_lock);
210 struct sas_ha_struct *sas_ha = dev->port->ha;
211 struct sas_task *task;
214 task = sas_create_task(cmd, dev, GFP_ATOMIC);
218 cmd->scsi_done = scsi_done;
219 /* Queue up, Direct Mode or Task Collector Mode. */
220 if (sas_ha->lldd_max_execute_num < 2)
221 res = i->dft->lldd_execute_task(task, 1, GFP_ATOMIC);
223 res = sas_queue_up(task);
227 SAS_DPRINTK("lldd_execute_task returned: %d\n", res);
228 ASSIGN_SAS_TASK(cmd, NULL);
230 if (res == -SAS_QUEUE_FULL) {
231 cmd->result = DID_SOFT_ERROR << 16; /* retry */
239 spin_lock_irq(host->host_lock);
243 static void sas_scsi_clear_queue_lu(struct list_head *error_q, struct scsi_cmnd *my_cmd)
245 struct scsi_cmnd *cmd, *n;
247 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
249 list_del_init(&cmd->eh_entry);
253 static void sas_scsi_clear_queue_I_T(struct list_head *error_q,
254 struct domain_device *dev)
256 struct scsi_cmnd *cmd, *n;
258 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
259 struct domain_device *x = cmd_to_domain_dev(cmd);
262 list_del_init(&cmd->eh_entry);
266 static void sas_scsi_clear_queue_port(struct list_head *error_q,
267 struct asd_sas_port *port)
269 struct scsi_cmnd *cmd, *n;
271 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
272 struct domain_device *dev = cmd_to_domain_dev(cmd);
273 struct asd_sas_port *x = dev->port;
276 list_del_init(&cmd->eh_entry);
280 enum task_disposition {
288 static enum task_disposition sas_scsi_find_task(struct sas_task *task)
290 struct sas_ha_struct *ha = task->dev->port->ha;
293 struct sas_internal *si =
294 to_sas_internal(task->dev->port->ha->core.shost->transportt);
296 if (ha->lldd_max_execute_num > 1) {
297 struct scsi_core *core = &ha->core;
298 struct sas_task *t, *n;
300 spin_lock_irqsave(&core->task_queue_lock, flags);
301 list_for_each_entry_safe(t, n, &core->task_queue, list) {
303 list_del_init(&t->list);
304 spin_unlock_irqrestore(&core->task_queue_lock,
306 SAS_DPRINTK("%s: task 0x%p aborted from "
309 return TASK_IS_ABORTED;
312 spin_unlock_irqrestore(&core->task_queue_lock, flags);
315 for (i = 0; i < 5; i++) {
316 SAS_DPRINTK("%s: aborting task 0x%p\n", __FUNCTION__, task);
317 res = si->dft->lldd_abort_task(task);
319 spin_lock_irqsave(&task->task_state_lock, flags);
320 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
321 spin_unlock_irqrestore(&task->task_state_lock, flags);
322 SAS_DPRINTK("%s: task 0x%p is done\n", __FUNCTION__,
326 spin_unlock_irqrestore(&task->task_state_lock, flags);
328 if (res == TMF_RESP_FUNC_COMPLETE) {
329 SAS_DPRINTK("%s: task 0x%p is aborted\n",
331 return TASK_IS_ABORTED;
332 } else if (si->dft->lldd_query_task) {
333 SAS_DPRINTK("%s: querying task 0x%p\n",
335 res = si->dft->lldd_query_task(task);
337 case TMF_RESP_FUNC_SUCC:
338 SAS_DPRINTK("%s: task 0x%p at LU\n",
340 return TASK_IS_AT_LU;
341 case TMF_RESP_FUNC_COMPLETE:
342 SAS_DPRINTK("%s: task 0x%p not at LU\n",
344 return TASK_IS_NOT_AT_LU;
345 case TMF_RESP_FUNC_FAILED:
346 SAS_DPRINTK("%s: task 0x%p failed to abort\n",
348 return TASK_ABORT_FAILED;
356 static int sas_recover_lu(struct domain_device *dev, struct scsi_cmnd *cmd)
358 int res = TMF_RESP_FUNC_FAILED;
360 struct sas_internal *i =
361 to_sas_internal(dev->port->ha->core.shost->transportt);
363 int_to_scsilun(cmd->device->lun, &lun);
365 SAS_DPRINTK("eh: device %llx LUN %x has the task\n",
366 SAS_ADDR(dev->sas_addr),
369 if (i->dft->lldd_abort_task_set)
370 res = i->dft->lldd_abort_task_set(dev, lun.scsi_lun);
372 if (res == TMF_RESP_FUNC_FAILED) {
373 if (i->dft->lldd_clear_task_set)
374 res = i->dft->lldd_clear_task_set(dev, lun.scsi_lun);
377 if (res == TMF_RESP_FUNC_FAILED) {
378 if (i->dft->lldd_lu_reset)
379 res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
385 static int sas_recover_I_T(struct domain_device *dev)
387 int res = TMF_RESP_FUNC_FAILED;
388 struct sas_internal *i =
389 to_sas_internal(dev->port->ha->core.shost->transportt);
391 SAS_DPRINTK("I_T nexus reset for dev %016llx\n",
392 SAS_ADDR(dev->sas_addr));
394 if (i->dft->lldd_I_T_nexus_reset)
395 res = i->dft->lldd_I_T_nexus_reset(dev);
400 /* Find the sas_phy that's attached to this device */
401 struct sas_phy *find_local_sas_phy(struct domain_device *dev)
403 struct domain_device *pdev = dev->parent;
404 struct ex_phy *exphy = NULL;
407 /* Directly attached device */
409 return dev->port->phy;
411 /* Otherwise look in the expander */
412 for (i = 0; i < pdev->ex_dev.num_phys; i++)
413 if (!memcmp(dev->sas_addr,
414 pdev->ex_dev.ex_phy[i].attached_sas_addr,
416 exphy = &pdev->ex_dev.ex_phy[i];
424 /* Attempt to send a LUN reset message to a device */
425 int sas_eh_device_reset_handler(struct scsi_cmnd *cmd)
427 struct domain_device *dev = cmd_to_domain_dev(cmd);
428 struct sas_internal *i =
429 to_sas_internal(dev->port->ha->core.shost->transportt);
433 int_to_scsilun(cmd->device->lun, &lun);
435 if (!i->dft->lldd_lu_reset)
438 res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
439 if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
445 /* Attempt to send a phy (bus) reset */
446 int sas_eh_bus_reset_handler(struct scsi_cmnd *cmd)
448 struct domain_device *dev = cmd_to_domain_dev(cmd);
449 struct sas_phy *phy = find_local_sas_phy(dev);
452 res = sas_phy_reset(phy, 1);
454 SAS_DPRINTK("Bus reset of %s failed 0x%x\n",
455 phy->dev.kobj.k_name,
457 if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
463 /* Try to reset a device */
464 static int try_to_reset_cmd_device(struct Scsi_Host *shost,
465 struct scsi_cmnd *cmd)
469 if (!shost->hostt->eh_device_reset_handler)
472 res = shost->hostt->eh_device_reset_handler(cmd);
477 if (shost->hostt->eh_bus_reset_handler)
478 return shost->hostt->eh_bus_reset_handler(cmd);
483 static int sas_eh_handle_sas_errors(struct Scsi_Host *shost,
484 struct list_head *work_q,
485 struct list_head *done_q)
487 struct scsi_cmnd *cmd, *n;
488 enum task_disposition res = TASK_IS_DONE;
489 int tmf_resp, need_reset;
490 struct sas_internal *i = to_sas_internal(shost->transportt);
492 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
495 list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
496 struct sas_task *task = TO_SAS_TASK(cmd);
501 list_del_init(&cmd->eh_entry);
503 spin_lock_irqsave(&task->task_state_lock, flags);
504 need_reset = task->task_state_flags & SAS_TASK_NEED_DEV_RESET;
505 spin_unlock_irqrestore(&task->task_state_lock, flags);
507 SAS_DPRINTK("trying to find task 0x%p\n", task);
508 res = sas_scsi_find_task(task);
514 SAS_DPRINTK("%s: task 0x%p is done\n", __FUNCTION__,
516 task->task_done(task);
518 try_to_reset_cmd_device(shost, cmd);
520 case TASK_IS_ABORTED:
521 SAS_DPRINTK("%s: task 0x%p is aborted\n",
523 task->task_done(task);
525 try_to_reset_cmd_device(shost, cmd);
528 SAS_DPRINTK("task 0x%p is at LU: lu recover\n", task);
529 tmf_resp = sas_recover_lu(task->dev, cmd);
530 if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
531 SAS_DPRINTK("dev %016llx LU %x is "
535 task->task_done(task);
537 try_to_reset_cmd_device(shost, cmd);
538 sas_scsi_clear_queue_lu(work_q, cmd);
542 case TASK_IS_NOT_AT_LU:
543 case TASK_ABORT_FAILED:
544 SAS_DPRINTK("task 0x%p is not at LU: I_T recover\n",
546 tmf_resp = sas_recover_I_T(task->dev);
547 if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
548 SAS_DPRINTK("I_T %016llx recovered\n",
549 SAS_ADDR(task->dev->sas_addr));
550 task->task_done(task);
552 try_to_reset_cmd_device(shost, cmd);
553 sas_scsi_clear_queue_I_T(work_q, task->dev);
556 /* Hammer time :-) */
557 if (i->dft->lldd_clear_nexus_port) {
558 struct asd_sas_port *port = task->dev->port;
559 SAS_DPRINTK("clearing nexus for port:%d\n",
561 res = i->dft->lldd_clear_nexus_port(port);
562 if (res == TMF_RESP_FUNC_COMPLETE) {
563 SAS_DPRINTK("clear nexus port:%d "
564 "succeeded\n", port->id);
565 task->task_done(task);
567 try_to_reset_cmd_device(shost, cmd);
568 sas_scsi_clear_queue_port(work_q,
573 if (i->dft->lldd_clear_nexus_ha) {
574 SAS_DPRINTK("clear nexus ha\n");
575 res = i->dft->lldd_clear_nexus_ha(ha);
576 if (res == TMF_RESP_FUNC_COMPLETE) {
577 SAS_DPRINTK("clear nexus ha "
579 task->task_done(task);
581 try_to_reset_cmd_device(shost, cmd);
585 /* If we are here -- this means that no amount
586 * of effort could recover from errors. Quite
587 * possibly the HA just disappeared.
589 SAS_DPRINTK("error from device %llx, LUN %x "
590 "couldn't be recovered in any way\n",
591 SAS_ADDR(task->dev->sas_addr),
594 task->task_done(task);
596 try_to_reset_cmd_device(shost, cmd);
601 return list_empty(work_q);
603 SAS_DPRINTK("--- Exit %s -- clear_q\n", __FUNCTION__);
604 list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
605 struct sas_task *task = TO_SAS_TASK(cmd);
606 list_del_init(&cmd->eh_entry);
607 task->task_done(task);
609 return list_empty(work_q);
612 void sas_scsi_recover_host(struct Scsi_Host *shost)
614 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
616 LIST_HEAD(eh_work_q);
618 spin_lock_irqsave(shost->host_lock, flags);
619 list_splice_init(&shost->eh_cmd_q, &eh_work_q);
620 spin_unlock_irqrestore(shost->host_lock, flags);
622 SAS_DPRINTK("Enter %s\n", __FUNCTION__);
624 * Deal with commands that still have SAS tasks (i.e. they didn't
625 * complete via the normal sas_task completion mechanism)
627 if (sas_eh_handle_sas_errors(shost, &eh_work_q, &ha->eh_done_q))
631 * Now deal with SCSI commands that completed ok but have a an error
632 * code (and hopefully sense data) attached. This is roughly what
633 * scsi_unjam_host does, but we skip scsi_eh_abort_cmds because any
634 * command we see here has no sas_task and is thus unknown to the HA.
636 if (!scsi_eh_get_sense(&eh_work_q, &ha->eh_done_q))
637 scsi_eh_ready_devs(shost, &eh_work_q, &ha->eh_done_q);
640 scsi_eh_flush_done_q(&ha->eh_done_q);
641 SAS_DPRINTK("--- Exit %s\n", __FUNCTION__);
645 enum scsi_eh_timer_return sas_scsi_timed_out(struct scsi_cmnd *cmd)
647 struct sas_task *task = TO_SAS_TASK(cmd);
651 cmd->timeout_per_command /= 2;
652 SAS_DPRINTK("command 0x%p, task 0x%p, gone: %s\n",
653 cmd, task, (cmd->timeout_per_command ?
654 "EH_RESET_TIMER" : "EH_NOT_HANDLED"));
655 if (!cmd->timeout_per_command)
656 return EH_NOT_HANDLED;
657 return EH_RESET_TIMER;
660 spin_lock_irqsave(&task->task_state_lock, flags);
661 BUG_ON(task->task_state_flags & SAS_TASK_STATE_ABORTED);
662 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
663 spin_unlock_irqrestore(&task->task_state_lock, flags);
664 SAS_DPRINTK("command 0x%p, task 0x%p, timed out: EH_HANDLED\n",
668 if (!(task->task_state_flags & SAS_TASK_AT_INITIATOR)) {
669 spin_unlock_irqrestore(&task->task_state_lock, flags);
670 SAS_DPRINTK("command 0x%p, task 0x%p, not at initiator: "
673 return EH_RESET_TIMER;
675 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
676 spin_unlock_irqrestore(&task->task_state_lock, flags);
678 SAS_DPRINTK("command 0x%p, task 0x%p, timed out: EH_NOT_HANDLED\n",
681 return EH_NOT_HANDLED;
684 struct domain_device *sas_find_dev_by_rphy(struct sas_rphy *rphy)
686 struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent);
687 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
688 struct domain_device *found_dev = NULL;
692 spin_lock_irqsave(&ha->phy_port_lock, flags);
693 for (i = 0; i < ha->num_phys; i++) {
694 struct asd_sas_port *port = ha->sas_port[i];
695 struct domain_device *dev;
697 spin_lock(&port->dev_list_lock);
698 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
699 if (rphy == dev->rphy) {
701 spin_unlock(&port->dev_list_lock);
705 spin_unlock(&port->dev_list_lock);
708 spin_unlock_irqrestore(&ha->phy_port_lock, flags);
713 static inline struct domain_device *sas_find_target(struct scsi_target *starget)
715 struct sas_rphy *rphy = dev_to_rphy(starget->dev.parent);
717 return sas_find_dev_by_rphy(rphy);
720 int sas_target_alloc(struct scsi_target *starget)
722 struct domain_device *found_dev = sas_find_target(starget);
727 starget->hostdata = found_dev;
731 #define SAS_DEF_QD 32
732 #define SAS_MAX_QD 64
734 int sas_slave_configure(struct scsi_device *scsi_dev)
736 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
737 struct sas_ha_struct *sas_ha;
739 BUG_ON(dev->rphy->identify.device_type != SAS_END_DEVICE);
741 sas_ha = dev->port->ha;
743 sas_read_port_mode_page(scsi_dev);
745 if (scsi_dev->tagged_supported) {
746 scsi_set_tag_type(scsi_dev, MSG_SIMPLE_TAG);
747 scsi_activate_tcq(scsi_dev, SAS_DEF_QD);
749 SAS_DPRINTK("device %llx, LUN %x doesn't support "
750 "TCQ\n", SAS_ADDR(dev->sas_addr),
752 scsi_dev->tagged_supported = 0;
753 scsi_set_tag_type(scsi_dev, 0);
754 scsi_deactivate_tcq(scsi_dev, 1);
757 scsi_dev->allow_restart = 1;
762 void sas_slave_destroy(struct scsi_device *scsi_dev)
766 int sas_change_queue_depth(struct scsi_device *scsi_dev, int new_depth)
768 int res = min(new_depth, SAS_MAX_QD);
770 if (scsi_dev->tagged_supported)
771 scsi_adjust_queue_depth(scsi_dev, scsi_get_tag_type(scsi_dev),
774 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
775 sas_printk("device %llx LUN %x queue depth changed to 1\n",
776 SAS_ADDR(dev->sas_addr),
778 scsi_adjust_queue_depth(scsi_dev, 0, 1);
785 int sas_change_queue_type(struct scsi_device *scsi_dev, int qt)
787 if (!scsi_dev->tagged_supported)
790 scsi_deactivate_tcq(scsi_dev, 1);
792 scsi_set_tag_type(scsi_dev, qt);
793 scsi_activate_tcq(scsi_dev, scsi_dev->queue_depth);
798 int sas_bios_param(struct scsi_device *scsi_dev,
799 struct block_device *bdev,
800 sector_t capacity, int *hsc)
804 sector_div(capacity, 255*63);
810 /* ---------- Task Collector Thread implementation ---------- */
812 static void sas_queue(struct sas_ha_struct *sas_ha)
814 struct scsi_core *core = &sas_ha->core;
819 struct sas_internal *i = to_sas_internal(core->shost->transportt);
821 spin_lock_irqsave(&core->task_queue_lock, flags);
822 while (!core->queue_thread_kill &&
823 !list_empty(&core->task_queue)) {
825 can_queue = sas_ha->lldd_queue_size - core->task_queue_size;
826 if (can_queue >= 0) {
827 can_queue = core->task_queue_size;
828 list_splice_init(&core->task_queue, &q);
830 struct list_head *a, *n;
832 can_queue = sas_ha->lldd_queue_size;
833 list_for_each_safe(a, n, &core->task_queue) {
834 list_move_tail(a, &q);
835 if (--can_queue == 0)
838 can_queue = sas_ha->lldd_queue_size;
840 core->task_queue_size -= can_queue;
841 spin_unlock_irqrestore(&core->task_queue_lock, flags);
843 struct sas_task *task = list_entry(q.next,
847 res = i->dft->lldd_execute_task(task, can_queue,
850 __list_add(&q, task->list.prev, &task->list);
852 spin_lock_irqsave(&core->task_queue_lock, flags);
854 list_splice_init(&q, &core->task_queue); /*at head*/
855 core->task_queue_size += can_queue;
858 spin_unlock_irqrestore(&core->task_queue_lock, flags);
861 static DECLARE_COMPLETION(queue_th_comp);
864 * sas_queue_thread -- The Task Collector thread
865 * @_sas_ha: pointer to struct sas_ha
867 static int sas_queue_thread(void *_sas_ha)
869 struct sas_ha_struct *sas_ha = _sas_ha;
870 struct scsi_core *core = &sas_ha->core;
872 daemonize("sas_queue_%d", core->shost->host_no);
873 current->flags |= PF_NOFREEZE;
875 complete(&queue_th_comp);
878 down_interruptible(&core->queue_thread_sema);
880 if (core->queue_thread_kill)
884 complete(&queue_th_comp);
889 int sas_init_queue(struct sas_ha_struct *sas_ha)
892 struct scsi_core *core = &sas_ha->core;
894 spin_lock_init(&core->task_queue_lock);
895 core->task_queue_size = 0;
896 INIT_LIST_HEAD(&core->task_queue);
897 init_MUTEX_LOCKED(&core->queue_thread_sema);
899 res = kernel_thread(sas_queue_thread, sas_ha, 0);
901 wait_for_completion(&queue_th_comp);
903 return res < 0 ? res : 0;
906 void sas_shutdown_queue(struct sas_ha_struct *sas_ha)
909 struct scsi_core *core = &sas_ha->core;
910 struct sas_task *task, *n;
912 init_completion(&queue_th_comp);
913 core->queue_thread_kill = 1;
914 up(&core->queue_thread_sema);
915 wait_for_completion(&queue_th_comp);
917 if (!list_empty(&core->task_queue))
918 SAS_DPRINTK("HA: %llx: scsi core task queue is NOT empty!?\n",
919 SAS_ADDR(sas_ha->sas_addr));
921 spin_lock_irqsave(&core->task_queue_lock, flags);
922 list_for_each_entry_safe(task, n, &core->task_queue, list) {
923 struct scsi_cmnd *cmd = task->uldd_task;
925 list_del_init(&task->list);
927 ASSIGN_SAS_TASK(cmd, NULL);
929 cmd->result = DID_ABORT << 16;
932 spin_unlock_irqrestore(&core->task_queue_lock, flags);
936 * Call the LLDD task abort routine directly. This function is intended for
937 * use by upper layers that need to tell the LLDD to abort a task.
939 int __sas_task_abort(struct sas_task *task)
941 struct sas_internal *si =
942 to_sas_internal(task->dev->port->ha->core.shost->transportt);
946 spin_lock_irqsave(&task->task_state_lock, flags);
947 if (task->task_state_flags & SAS_TASK_STATE_ABORTED ||
948 task->task_state_flags & SAS_TASK_STATE_DONE) {
949 spin_unlock_irqrestore(&task->task_state_lock, flags);
950 SAS_DPRINTK("%s: Task %p already finished.\n", __FUNCTION__,
954 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
955 spin_unlock_irqrestore(&task->task_state_lock, flags);
957 if (!si->dft->lldd_abort_task)
960 res = si->dft->lldd_abort_task(task);
962 spin_lock_irqsave(&task->task_state_lock, flags);
963 if ((task->task_state_flags & SAS_TASK_STATE_DONE) ||
964 (res == TMF_RESP_FUNC_COMPLETE))
966 spin_unlock_irqrestore(&task->task_state_lock, flags);
967 task->task_done(task);
971 if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
972 task->task_state_flags &= ~SAS_TASK_STATE_ABORTED;
973 spin_unlock_irqrestore(&task->task_state_lock, flags);
979 * Tell an upper layer that it needs to initiate an abort for a given task.
980 * This should only ever be called by an LLDD.
982 void sas_task_abort(struct sas_task *task)
984 struct scsi_cmnd *sc = task->uldd_task;
986 /* Escape for libsas internal commands */
988 if (!del_timer(&task->timer))
990 task->timer.function(task->timer.data);
994 scsi_req_abort_cmd(sc);
995 scsi_schedule_eh(sc->device->host);
998 EXPORT_SYMBOL_GPL(sas_queuecommand);
999 EXPORT_SYMBOL_GPL(sas_target_alloc);
1000 EXPORT_SYMBOL_GPL(sas_slave_configure);
1001 EXPORT_SYMBOL_GPL(sas_slave_destroy);
1002 EXPORT_SYMBOL_GPL(sas_change_queue_depth);
1003 EXPORT_SYMBOL_GPL(sas_change_queue_type);
1004 EXPORT_SYMBOL_GPL(sas_bios_param);
1005 EXPORT_SYMBOL_GPL(__sas_task_abort);
1006 EXPORT_SYMBOL_GPL(sas_task_abort);
1007 EXPORT_SYMBOL_GPL(sas_phy_reset);
1008 EXPORT_SYMBOL_GPL(sas_phy_enable);
1009 EXPORT_SYMBOL_GPL(sas_eh_device_reset_handler);
1010 EXPORT_SYMBOL_GPL(sas_eh_bus_reset_handler);