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 <linux/kthread.h>
28 #include "sas_internal.h"
30 #include <scsi/scsi_host.h>
31 #include <scsi/scsi_device.h>
32 #include <scsi/scsi_tcq.h>
33 #include <scsi/scsi.h>
34 #include <scsi/scsi_eh.h>
35 #include <scsi/scsi_transport.h>
36 #include <scsi/scsi_transport_sas.h>
37 #include "../scsi_sas_internal.h"
38 #include "../scsi_transport_api.h"
39 #include "../scsi_priv.h"
41 #include <linux/err.h>
42 #include <linux/blkdev.h>
43 #include <linux/scatterlist.h>
45 /* ---------- SCSI Host glue ---------- */
47 #define TO_SAS_TASK(_scsi_cmd) ((void *)(_scsi_cmd)->host_scribble)
48 #define ASSIGN_SAS_TASK(_sc, _t) do { (_sc)->host_scribble = (void *) _t; } while (0)
50 static void sas_scsi_task_done(struct sas_task *task)
52 struct task_status_struct *ts = &task->task_status;
53 struct scsi_cmnd *sc = task->uldd_task;
54 struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(sc->device->host);
55 unsigned ts_flags = task->task_state_flags;
59 SAS_DPRINTK("task_done called with non existing SCSI cmnd!\n");
60 list_del_init(&task->list);
65 if (ts->resp == SAS_TASK_UNDELIVERED) {
68 } else { /* ts->resp == SAS_TASK_COMPLETE */
69 /* task delivered, what happened afterwards? */
71 case SAS_DEV_NO_RESPONSE:
78 case SAS_DATA_UNDERRUN:
79 sc->resid = ts->residual;
80 if (sc->request_bufflen - sc->resid < sc->underflow)
83 case SAS_DATA_OVERRUN:
87 hs = DID_SOFT_ERROR; /* retry */
89 case SAS_DEVICE_UNKNOWN:
96 if (ts->open_rej_reason == SAS_OREJ_RSVD_RETRY)
97 hs = DID_SOFT_ERROR; /* retry */
101 case SAS_PROTO_RESPONSE:
102 SAS_DPRINTK("LLDD:%s sent SAS_PROTO_RESP for an SSP "
103 "task; please report this\n",
104 task->dev->port->ha->sas_ha_name);
106 case SAS_ABORTED_TASK:
110 memcpy(sc->sense_buffer, ts->buf,
111 max(SCSI_SENSE_BUFFERSIZE, ts->buf_valid_size));
112 stat = SAM_CHECK_COND;
119 ASSIGN_SAS_TASK(sc, NULL);
120 sc->result = (hs << 16) | stat;
121 list_del_init(&task->list);
123 /* This is very ugly but this is how SCSI Core works. */
124 if (ts_flags & SAS_TASK_STATE_ABORTED)
125 scsi_eh_finish_cmd(sc, &sas_ha->eh_done_q);
130 static enum task_attribute sas_scsi_get_task_attr(struct scsi_cmnd *cmd)
132 enum task_attribute ta = TASK_ATTR_SIMPLE;
133 if (cmd->request && blk_rq_tagged(cmd->request)) {
134 if (cmd->device->ordered_tags &&
135 (cmd->request->cmd_flags & REQ_HARDBARRIER))
136 ta = TASK_ATTR_ORDERED;
141 static struct sas_task *sas_create_task(struct scsi_cmnd *cmd,
142 struct domain_device *dev,
145 struct sas_task *task = sas_alloc_task(gfp_flags);
151 *(u32 *)cmd->sense_buffer = 0;
152 task->uldd_task = cmd;
153 ASSIGN_SAS_TASK(cmd, task);
156 task->task_proto = task->dev->tproto; /* BUG_ON(!SSP) */
158 task->ssp_task.retry_count = 1;
159 int_to_scsilun(cmd->device->lun, &lun);
160 memcpy(task->ssp_task.LUN, &lun.scsi_lun, 8);
161 task->ssp_task.task_attr = sas_scsi_get_task_attr(cmd);
162 memcpy(task->ssp_task.cdb, cmd->cmnd, 16);
164 task->scatter = cmd->request_buffer;
165 task->num_scatter = cmd->use_sg;
166 task->total_xfer_len = cmd->request_bufflen;
167 task->data_dir = cmd->sc_data_direction;
169 task->task_done = sas_scsi_task_done;
174 static int sas_queue_up(struct sas_task *task)
176 struct sas_ha_struct *sas_ha = task->dev->port->ha;
177 struct scsi_core *core = &sas_ha->core;
181 spin_lock_irqsave(&core->task_queue_lock, flags);
182 if (sas_ha->lldd_queue_size < core->task_queue_size + 1) {
183 spin_unlock_irqrestore(&core->task_queue_lock, flags);
184 return -SAS_QUEUE_FULL;
186 list_add_tail(&task->list, &core->task_queue);
187 core->task_queue_size += 1;
188 spin_unlock_irqrestore(&core->task_queue_lock, flags);
189 wake_up_process(core->queue_thread);
195 * sas_queuecommand -- Enqueue a command for processing
196 * @parameters: See SCSI Core documentation
198 * Note: XXX: Remove the host unlock/lock pair when SCSI Core can
199 * call us without holding an IRQ spinlock...
201 int sas_queuecommand(struct scsi_cmnd *cmd,
202 void (*scsi_done)(struct scsi_cmnd *))
205 struct domain_device *dev = cmd_to_domain_dev(cmd);
206 struct Scsi_Host *host = cmd->device->host;
207 struct sas_internal *i = to_sas_internal(host->transportt);
209 spin_unlock_irq(host->host_lock);
212 struct sas_ha_struct *sas_ha = dev->port->ha;
213 struct sas_task *task;
216 task = sas_create_task(cmd, dev, GFP_ATOMIC);
220 cmd->scsi_done = scsi_done;
221 /* Queue up, Direct Mode or Task Collector Mode. */
222 if (sas_ha->lldd_max_execute_num < 2)
223 res = i->dft->lldd_execute_task(task, 1, GFP_ATOMIC);
225 res = sas_queue_up(task);
229 SAS_DPRINTK("lldd_execute_task returned: %d\n", res);
230 ASSIGN_SAS_TASK(cmd, NULL);
232 if (res == -SAS_QUEUE_FULL) {
233 cmd->result = DID_SOFT_ERROR << 16; /* retry */
241 spin_lock_irq(host->host_lock);
245 static void sas_scsi_clear_queue_lu(struct list_head *error_q, struct scsi_cmnd *my_cmd)
247 struct scsi_cmnd *cmd, *n;
249 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
251 list_del_init(&cmd->eh_entry);
255 static void sas_scsi_clear_queue_I_T(struct list_head *error_q,
256 struct domain_device *dev)
258 struct scsi_cmnd *cmd, *n;
260 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
261 struct domain_device *x = cmd_to_domain_dev(cmd);
264 list_del_init(&cmd->eh_entry);
268 static void sas_scsi_clear_queue_port(struct list_head *error_q,
269 struct asd_sas_port *port)
271 struct scsi_cmnd *cmd, *n;
273 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
274 struct domain_device *dev = cmd_to_domain_dev(cmd);
275 struct asd_sas_port *x = dev->port;
278 list_del_init(&cmd->eh_entry);
282 enum task_disposition {
290 static enum task_disposition sas_scsi_find_task(struct sas_task *task)
292 struct sas_ha_struct *ha = task->dev->port->ha;
295 struct sas_internal *si =
296 to_sas_internal(task->dev->port->ha->core.shost->transportt);
298 if (ha->lldd_max_execute_num > 1) {
299 struct scsi_core *core = &ha->core;
300 struct sas_task *t, *n;
302 spin_lock_irqsave(&core->task_queue_lock, flags);
303 list_for_each_entry_safe(t, n, &core->task_queue, list) {
305 list_del_init(&t->list);
306 spin_unlock_irqrestore(&core->task_queue_lock,
308 SAS_DPRINTK("%s: task 0x%p aborted from "
311 return TASK_IS_ABORTED;
314 spin_unlock_irqrestore(&core->task_queue_lock, flags);
317 for (i = 0; i < 5; i++) {
318 SAS_DPRINTK("%s: aborting task 0x%p\n", __FUNCTION__, task);
319 res = si->dft->lldd_abort_task(task);
321 spin_lock_irqsave(&task->task_state_lock, flags);
322 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
323 spin_unlock_irqrestore(&task->task_state_lock, flags);
324 SAS_DPRINTK("%s: task 0x%p is done\n", __FUNCTION__,
328 spin_unlock_irqrestore(&task->task_state_lock, flags);
330 if (res == TMF_RESP_FUNC_COMPLETE) {
331 SAS_DPRINTK("%s: task 0x%p is aborted\n",
333 return TASK_IS_ABORTED;
334 } else if (si->dft->lldd_query_task) {
335 SAS_DPRINTK("%s: querying task 0x%p\n",
337 res = si->dft->lldd_query_task(task);
339 case TMF_RESP_FUNC_SUCC:
340 SAS_DPRINTK("%s: task 0x%p at LU\n",
342 return TASK_IS_AT_LU;
343 case TMF_RESP_FUNC_COMPLETE:
344 SAS_DPRINTK("%s: task 0x%p not at LU\n",
346 return TASK_IS_NOT_AT_LU;
347 case TMF_RESP_FUNC_FAILED:
348 SAS_DPRINTK("%s: task 0x%p failed to abort\n",
350 return TASK_ABORT_FAILED;
358 static int sas_recover_lu(struct domain_device *dev, struct scsi_cmnd *cmd)
360 int res = TMF_RESP_FUNC_FAILED;
362 struct sas_internal *i =
363 to_sas_internal(dev->port->ha->core.shost->transportt);
365 int_to_scsilun(cmd->device->lun, &lun);
367 SAS_DPRINTK("eh: device %llx LUN %x has the task\n",
368 SAS_ADDR(dev->sas_addr),
371 if (i->dft->lldd_abort_task_set)
372 res = i->dft->lldd_abort_task_set(dev, lun.scsi_lun);
374 if (res == TMF_RESP_FUNC_FAILED) {
375 if (i->dft->lldd_clear_task_set)
376 res = i->dft->lldd_clear_task_set(dev, lun.scsi_lun);
379 if (res == TMF_RESP_FUNC_FAILED) {
380 if (i->dft->lldd_lu_reset)
381 res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
387 static int sas_recover_I_T(struct domain_device *dev)
389 int res = TMF_RESP_FUNC_FAILED;
390 struct sas_internal *i =
391 to_sas_internal(dev->port->ha->core.shost->transportt);
393 SAS_DPRINTK("I_T nexus reset for dev %016llx\n",
394 SAS_ADDR(dev->sas_addr));
396 if (i->dft->lldd_I_T_nexus_reset)
397 res = i->dft->lldd_I_T_nexus_reset(dev);
402 /* Find the sas_phy that's attached to this device */
403 struct sas_phy *find_local_sas_phy(struct domain_device *dev)
405 struct domain_device *pdev = dev->parent;
406 struct ex_phy *exphy = NULL;
409 /* Directly attached device */
411 return dev->port->phy;
413 /* Otherwise look in the expander */
414 for (i = 0; i < pdev->ex_dev.num_phys; i++)
415 if (!memcmp(dev->sas_addr,
416 pdev->ex_dev.ex_phy[i].attached_sas_addr,
418 exphy = &pdev->ex_dev.ex_phy[i];
426 /* Attempt to send a LUN reset message to a device */
427 int sas_eh_device_reset_handler(struct scsi_cmnd *cmd)
429 struct domain_device *dev = cmd_to_domain_dev(cmd);
430 struct sas_internal *i =
431 to_sas_internal(dev->port->ha->core.shost->transportt);
435 int_to_scsilun(cmd->device->lun, &lun);
437 if (!i->dft->lldd_lu_reset)
440 res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
441 if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
447 /* Attempt to send a phy (bus) reset */
448 int sas_eh_bus_reset_handler(struct scsi_cmnd *cmd)
450 struct domain_device *dev = cmd_to_domain_dev(cmd);
451 struct sas_phy *phy = find_local_sas_phy(dev);
454 res = sas_phy_reset(phy, 1);
456 SAS_DPRINTK("Bus reset of %s failed 0x%x\n",
457 phy->dev.kobj.k_name,
459 if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
465 /* Try to reset a device */
466 static int try_to_reset_cmd_device(struct Scsi_Host *shost,
467 struct scsi_cmnd *cmd)
471 if (!shost->hostt->eh_device_reset_handler)
474 res = shost->hostt->eh_device_reset_handler(cmd);
479 if (shost->hostt->eh_bus_reset_handler)
480 return shost->hostt->eh_bus_reset_handler(cmd);
485 static int sas_eh_handle_sas_errors(struct Scsi_Host *shost,
486 struct list_head *work_q,
487 struct list_head *done_q)
489 struct scsi_cmnd *cmd, *n;
490 enum task_disposition res = TASK_IS_DONE;
491 int tmf_resp, need_reset;
492 struct sas_internal *i = to_sas_internal(shost->transportt);
494 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
497 list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
498 struct sas_task *task = TO_SAS_TASK(cmd);
503 list_del_init(&cmd->eh_entry);
505 spin_lock_irqsave(&task->task_state_lock, flags);
506 need_reset = task->task_state_flags & SAS_TASK_NEED_DEV_RESET;
507 spin_unlock_irqrestore(&task->task_state_lock, flags);
509 SAS_DPRINTK("trying to find task 0x%p\n", task);
510 res = sas_scsi_find_task(task);
516 SAS_DPRINTK("%s: task 0x%p is done\n", __FUNCTION__,
518 task->task_done(task);
520 try_to_reset_cmd_device(shost, cmd);
522 case TASK_IS_ABORTED:
523 SAS_DPRINTK("%s: task 0x%p is aborted\n",
525 task->task_done(task);
527 try_to_reset_cmd_device(shost, cmd);
530 SAS_DPRINTK("task 0x%p is at LU: lu recover\n", task);
531 tmf_resp = sas_recover_lu(task->dev, cmd);
532 if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
533 SAS_DPRINTK("dev %016llx LU %x is "
537 task->task_done(task);
539 try_to_reset_cmd_device(shost, cmd);
540 sas_scsi_clear_queue_lu(work_q, cmd);
544 case TASK_IS_NOT_AT_LU:
545 case TASK_ABORT_FAILED:
546 SAS_DPRINTK("task 0x%p is not at LU: I_T recover\n",
548 tmf_resp = sas_recover_I_T(task->dev);
549 if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
550 SAS_DPRINTK("I_T %016llx recovered\n",
551 SAS_ADDR(task->dev->sas_addr));
552 task->task_done(task);
554 try_to_reset_cmd_device(shost, cmd);
555 sas_scsi_clear_queue_I_T(work_q, task->dev);
558 /* Hammer time :-) */
559 if (i->dft->lldd_clear_nexus_port) {
560 struct asd_sas_port *port = task->dev->port;
561 SAS_DPRINTK("clearing nexus for port:%d\n",
563 res = i->dft->lldd_clear_nexus_port(port);
564 if (res == TMF_RESP_FUNC_COMPLETE) {
565 SAS_DPRINTK("clear nexus port:%d "
566 "succeeded\n", port->id);
567 task->task_done(task);
569 try_to_reset_cmd_device(shost, cmd);
570 sas_scsi_clear_queue_port(work_q,
575 if (i->dft->lldd_clear_nexus_ha) {
576 SAS_DPRINTK("clear nexus ha\n");
577 res = i->dft->lldd_clear_nexus_ha(ha);
578 if (res == TMF_RESP_FUNC_COMPLETE) {
579 SAS_DPRINTK("clear nexus ha "
581 task->task_done(task);
583 try_to_reset_cmd_device(shost, cmd);
587 /* If we are here -- this means that no amount
588 * of effort could recover from errors. Quite
589 * possibly the HA just disappeared.
591 SAS_DPRINTK("error from device %llx, LUN %x "
592 "couldn't be recovered in any way\n",
593 SAS_ADDR(task->dev->sas_addr),
596 task->task_done(task);
598 try_to_reset_cmd_device(shost, cmd);
603 return list_empty(work_q);
605 SAS_DPRINTK("--- Exit %s -- clear_q\n", __FUNCTION__);
606 list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
607 struct sas_task *task = TO_SAS_TASK(cmd);
608 list_del_init(&cmd->eh_entry);
609 task->task_done(task);
611 return list_empty(work_q);
614 void sas_scsi_recover_host(struct Scsi_Host *shost)
616 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
618 LIST_HEAD(eh_work_q);
620 spin_lock_irqsave(shost->host_lock, flags);
621 list_splice_init(&shost->eh_cmd_q, &eh_work_q);
622 spin_unlock_irqrestore(shost->host_lock, flags);
624 SAS_DPRINTK("Enter %s\n", __FUNCTION__);
626 * Deal with commands that still have SAS tasks (i.e. they didn't
627 * complete via the normal sas_task completion mechanism)
629 if (sas_eh_handle_sas_errors(shost, &eh_work_q, &ha->eh_done_q))
633 * Now deal with SCSI commands that completed ok but have a an error
634 * code (and hopefully sense data) attached. This is roughly what
635 * scsi_unjam_host does, but we skip scsi_eh_abort_cmds because any
636 * command we see here has no sas_task and is thus unknown to the HA.
638 if (!scsi_eh_get_sense(&eh_work_q, &ha->eh_done_q))
639 scsi_eh_ready_devs(shost, &eh_work_q, &ha->eh_done_q);
642 scsi_eh_flush_done_q(&ha->eh_done_q);
643 SAS_DPRINTK("--- Exit %s\n", __FUNCTION__);
647 enum scsi_eh_timer_return sas_scsi_timed_out(struct scsi_cmnd *cmd)
649 struct sas_task *task = TO_SAS_TASK(cmd);
653 cmd->timeout_per_command /= 2;
654 SAS_DPRINTK("command 0x%p, task 0x%p, gone: %s\n",
655 cmd, task, (cmd->timeout_per_command ?
656 "EH_RESET_TIMER" : "EH_NOT_HANDLED"));
657 if (!cmd->timeout_per_command)
658 return EH_NOT_HANDLED;
659 return EH_RESET_TIMER;
662 spin_lock_irqsave(&task->task_state_lock, flags);
663 BUG_ON(task->task_state_flags & SAS_TASK_STATE_ABORTED);
664 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
665 spin_unlock_irqrestore(&task->task_state_lock, flags);
666 SAS_DPRINTK("command 0x%p, task 0x%p, timed out: EH_HANDLED\n",
670 if (!(task->task_state_flags & SAS_TASK_AT_INITIATOR)) {
671 spin_unlock_irqrestore(&task->task_state_lock, flags);
672 SAS_DPRINTK("command 0x%p, task 0x%p, not at initiator: "
675 return EH_RESET_TIMER;
677 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
678 spin_unlock_irqrestore(&task->task_state_lock, flags);
680 SAS_DPRINTK("command 0x%p, task 0x%p, timed out: EH_NOT_HANDLED\n",
683 return EH_NOT_HANDLED;
686 struct domain_device *sas_find_dev_by_rphy(struct sas_rphy *rphy)
688 struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent);
689 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
690 struct domain_device *found_dev = NULL;
694 spin_lock_irqsave(&ha->phy_port_lock, flags);
695 for (i = 0; i < ha->num_phys; i++) {
696 struct asd_sas_port *port = ha->sas_port[i];
697 struct domain_device *dev;
699 spin_lock(&port->dev_list_lock);
700 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
701 if (rphy == dev->rphy) {
703 spin_unlock(&port->dev_list_lock);
707 spin_unlock(&port->dev_list_lock);
710 spin_unlock_irqrestore(&ha->phy_port_lock, flags);
715 static inline struct domain_device *sas_find_target(struct scsi_target *starget)
717 struct sas_rphy *rphy = dev_to_rphy(starget->dev.parent);
719 return sas_find_dev_by_rphy(rphy);
722 int sas_target_alloc(struct scsi_target *starget)
724 struct domain_device *found_dev = sas_find_target(starget);
729 starget->hostdata = found_dev;
733 #define SAS_DEF_QD 32
734 #define SAS_MAX_QD 64
736 int sas_slave_configure(struct scsi_device *scsi_dev)
738 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
739 struct sas_ha_struct *sas_ha;
741 BUG_ON(dev->rphy->identify.device_type != SAS_END_DEVICE);
743 sas_ha = dev->port->ha;
745 sas_read_port_mode_page(scsi_dev);
747 if (scsi_dev->tagged_supported) {
748 scsi_set_tag_type(scsi_dev, MSG_SIMPLE_TAG);
749 scsi_activate_tcq(scsi_dev, SAS_DEF_QD);
751 SAS_DPRINTK("device %llx, LUN %x doesn't support "
752 "TCQ\n", SAS_ADDR(dev->sas_addr),
754 scsi_dev->tagged_supported = 0;
755 scsi_set_tag_type(scsi_dev, 0);
756 scsi_deactivate_tcq(scsi_dev, 1);
759 scsi_dev->allow_restart = 1;
764 void sas_slave_destroy(struct scsi_device *scsi_dev)
768 int sas_change_queue_depth(struct scsi_device *scsi_dev, int new_depth)
770 int res = min(new_depth, SAS_MAX_QD);
772 if (scsi_dev->tagged_supported)
773 scsi_adjust_queue_depth(scsi_dev, scsi_get_tag_type(scsi_dev),
776 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
777 sas_printk("device %llx LUN %x queue depth changed to 1\n",
778 SAS_ADDR(dev->sas_addr),
780 scsi_adjust_queue_depth(scsi_dev, 0, 1);
787 int sas_change_queue_type(struct scsi_device *scsi_dev, int qt)
789 if (!scsi_dev->tagged_supported)
792 scsi_deactivate_tcq(scsi_dev, 1);
794 scsi_set_tag_type(scsi_dev, qt);
795 scsi_activate_tcq(scsi_dev, scsi_dev->queue_depth);
800 int sas_bios_param(struct scsi_device *scsi_dev,
801 struct block_device *bdev,
802 sector_t capacity, int *hsc)
806 sector_div(capacity, 255*63);
812 /* ---------- Task Collector Thread implementation ---------- */
814 static void sas_queue(struct sas_ha_struct *sas_ha)
816 struct scsi_core *core = &sas_ha->core;
821 struct sas_internal *i = to_sas_internal(core->shost->transportt);
823 spin_lock_irqsave(&core->task_queue_lock, flags);
824 while (!kthread_should_stop() &&
825 !list_empty(&core->task_queue)) {
827 can_queue = sas_ha->lldd_queue_size - core->task_queue_size;
828 if (can_queue >= 0) {
829 can_queue = core->task_queue_size;
830 list_splice_init(&core->task_queue, &q);
832 struct list_head *a, *n;
834 can_queue = sas_ha->lldd_queue_size;
835 list_for_each_safe(a, n, &core->task_queue) {
836 list_move_tail(a, &q);
837 if (--can_queue == 0)
840 can_queue = sas_ha->lldd_queue_size;
842 core->task_queue_size -= can_queue;
843 spin_unlock_irqrestore(&core->task_queue_lock, flags);
845 struct sas_task *task = list_entry(q.next,
849 res = i->dft->lldd_execute_task(task, can_queue,
852 __list_add(&q, task->list.prev, &task->list);
854 spin_lock_irqsave(&core->task_queue_lock, flags);
856 list_splice_init(&q, &core->task_queue); /*at head*/
857 core->task_queue_size += can_queue;
860 spin_unlock_irqrestore(&core->task_queue_lock, flags);
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;
871 current->flags |= PF_NOFREEZE;
874 set_current_state(TASK_INTERRUPTIBLE);
877 if (kthread_should_stop())
884 int sas_init_queue(struct sas_ha_struct *sas_ha)
886 struct scsi_core *core = &sas_ha->core;
888 spin_lock_init(&core->task_queue_lock);
889 core->task_queue_size = 0;
890 INIT_LIST_HEAD(&core->task_queue);
892 core->queue_thread = kthread_run(sas_queue_thread, sas_ha,
893 "sas_queue_%d", core->shost->host_no);
894 if (IS_ERR(core->queue_thread))
895 return PTR_ERR(core->queue_thread);
899 void sas_shutdown_queue(struct sas_ha_struct *sas_ha)
902 struct scsi_core *core = &sas_ha->core;
903 struct sas_task *task, *n;
905 kthread_stop(core->queue_thread);
907 if (!list_empty(&core->task_queue))
908 SAS_DPRINTK("HA: %llx: scsi core task queue is NOT empty!?\n",
909 SAS_ADDR(sas_ha->sas_addr));
911 spin_lock_irqsave(&core->task_queue_lock, flags);
912 list_for_each_entry_safe(task, n, &core->task_queue, list) {
913 struct scsi_cmnd *cmd = task->uldd_task;
915 list_del_init(&task->list);
917 ASSIGN_SAS_TASK(cmd, NULL);
919 cmd->result = DID_ABORT << 16;
922 spin_unlock_irqrestore(&core->task_queue_lock, flags);
926 * Call the LLDD task abort routine directly. This function is intended for
927 * use by upper layers that need to tell the LLDD to abort a task.
929 int __sas_task_abort(struct sas_task *task)
931 struct sas_internal *si =
932 to_sas_internal(task->dev->port->ha->core.shost->transportt);
936 spin_lock_irqsave(&task->task_state_lock, flags);
937 if (task->task_state_flags & SAS_TASK_STATE_ABORTED ||
938 task->task_state_flags & SAS_TASK_STATE_DONE) {
939 spin_unlock_irqrestore(&task->task_state_lock, flags);
940 SAS_DPRINTK("%s: Task %p already finished.\n", __FUNCTION__,
944 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
945 spin_unlock_irqrestore(&task->task_state_lock, flags);
947 if (!si->dft->lldd_abort_task)
950 res = si->dft->lldd_abort_task(task);
952 spin_lock_irqsave(&task->task_state_lock, flags);
953 if ((task->task_state_flags & SAS_TASK_STATE_DONE) ||
954 (res == TMF_RESP_FUNC_COMPLETE))
956 spin_unlock_irqrestore(&task->task_state_lock, flags);
957 task->task_done(task);
961 if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
962 task->task_state_flags &= ~SAS_TASK_STATE_ABORTED;
963 spin_unlock_irqrestore(&task->task_state_lock, flags);
969 * Tell an upper layer that it needs to initiate an abort for a given task.
970 * This should only ever be called by an LLDD.
972 void sas_task_abort(struct sas_task *task)
974 struct scsi_cmnd *sc = task->uldd_task;
976 /* Escape for libsas internal commands */
978 if (!del_timer(&task->timer))
980 task->timer.function(task->timer.data);
984 scsi_req_abort_cmd(sc);
985 scsi_schedule_eh(sc->device->host);
988 EXPORT_SYMBOL_GPL(sas_queuecommand);
989 EXPORT_SYMBOL_GPL(sas_target_alloc);
990 EXPORT_SYMBOL_GPL(sas_slave_configure);
991 EXPORT_SYMBOL_GPL(sas_slave_destroy);
992 EXPORT_SYMBOL_GPL(sas_change_queue_depth);
993 EXPORT_SYMBOL_GPL(sas_change_queue_type);
994 EXPORT_SYMBOL_GPL(sas_bios_param);
995 EXPORT_SYMBOL_GPL(__sas_task_abort);
996 EXPORT_SYMBOL_GPL(sas_task_abort);
997 EXPORT_SYMBOL_GPL(sas_phy_reset);
998 EXPORT_SYMBOL_GPL(sas_phy_enable);
999 EXPORT_SYMBOL_GPL(sas_eh_device_reset_handler);
1000 EXPORT_SYMBOL_GPL(sas_eh_bus_reset_handler);