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_ata.h>
38 #include "../scsi_sas_internal.h"
39 #include "../scsi_transport_api.h"
40 #include "../scsi_priv.h"
42 #include <linux/err.h>
43 #include <linux/blkdev.h>
44 #include <linux/freezer.h>
45 #include <linux/scatterlist.h>
46 #include <linux/libata.h>
48 /* ---------- SCSI Host glue ---------- */
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 scsi_set_resid(sc, ts->residual);
80 if (scsi_bufflen(sc) - scsi_get_resid(sc) < 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 = scsi_sglist(cmd);
165 task->num_scatter = scsi_sg_count(cmd);
166 task->total_xfer_len = scsi_bufflen(cmd);
167 task->data_dir = cmd->sc_data_direction;
169 task->task_done = sas_scsi_task_done;
174 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;
215 if (dev_is_sata(dev)) {
218 spin_lock_irqsave(dev->sata_dev.ap->lock, flags);
219 res = ata_sas_queuecmd(cmd, scsi_done,
221 spin_unlock_irqrestore(dev->sata_dev.ap->lock, flags);
226 task = sas_create_task(cmd, dev, GFP_ATOMIC);
230 cmd->scsi_done = scsi_done;
231 /* Queue up, Direct Mode or Task Collector Mode. */
232 if (sas_ha->lldd_max_execute_num < 2)
233 res = i->dft->lldd_execute_task(task, 1, GFP_ATOMIC);
235 res = sas_queue_up(task);
239 SAS_DPRINTK("lldd_execute_task returned: %d\n", res);
240 ASSIGN_SAS_TASK(cmd, NULL);
242 if (res == -SAS_QUEUE_FULL) {
243 cmd->result = DID_SOFT_ERROR << 16; /* retry */
251 spin_lock_irq(host->host_lock);
255 static void sas_scsi_clear_queue_lu(struct list_head *error_q, struct scsi_cmnd *my_cmd)
257 struct scsi_cmnd *cmd, *n;
259 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
261 list_del_init(&cmd->eh_entry);
265 static void sas_scsi_clear_queue_I_T(struct list_head *error_q,
266 struct domain_device *dev)
268 struct scsi_cmnd *cmd, *n;
270 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
271 struct domain_device *x = cmd_to_domain_dev(cmd);
274 list_del_init(&cmd->eh_entry);
278 static void sas_scsi_clear_queue_port(struct list_head *error_q,
279 struct asd_sas_port *port)
281 struct scsi_cmnd *cmd, *n;
283 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
284 struct domain_device *dev = cmd_to_domain_dev(cmd);
285 struct asd_sas_port *x = dev->port;
288 list_del_init(&cmd->eh_entry);
292 enum task_disposition {
300 static enum task_disposition sas_scsi_find_task(struct sas_task *task)
302 struct sas_ha_struct *ha = task->dev->port->ha;
305 struct sas_internal *si =
306 to_sas_internal(task->dev->port->ha->core.shost->transportt);
308 if (ha->lldd_max_execute_num > 1) {
309 struct scsi_core *core = &ha->core;
310 struct sas_task *t, *n;
312 spin_lock_irqsave(&core->task_queue_lock, flags);
313 list_for_each_entry_safe(t, n, &core->task_queue, list) {
315 list_del_init(&t->list);
316 spin_unlock_irqrestore(&core->task_queue_lock,
318 SAS_DPRINTK("%s: task 0x%p aborted from "
321 return TASK_IS_ABORTED;
324 spin_unlock_irqrestore(&core->task_queue_lock, flags);
327 for (i = 0; i < 5; i++) {
328 SAS_DPRINTK("%s: aborting task 0x%p\n", __FUNCTION__, task);
329 res = si->dft->lldd_abort_task(task);
331 spin_lock_irqsave(&task->task_state_lock, flags);
332 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
333 spin_unlock_irqrestore(&task->task_state_lock, flags);
334 SAS_DPRINTK("%s: task 0x%p is done\n", __FUNCTION__,
338 spin_unlock_irqrestore(&task->task_state_lock, flags);
340 if (res == TMF_RESP_FUNC_COMPLETE) {
341 SAS_DPRINTK("%s: task 0x%p is aborted\n",
343 return TASK_IS_ABORTED;
344 } else if (si->dft->lldd_query_task) {
345 SAS_DPRINTK("%s: querying task 0x%p\n",
347 res = si->dft->lldd_query_task(task);
349 case TMF_RESP_FUNC_SUCC:
350 SAS_DPRINTK("%s: task 0x%p at LU\n",
352 return TASK_IS_AT_LU;
353 case TMF_RESP_FUNC_COMPLETE:
354 SAS_DPRINTK("%s: task 0x%p not at LU\n",
356 return TASK_IS_NOT_AT_LU;
357 case TMF_RESP_FUNC_FAILED:
358 SAS_DPRINTK("%s: task 0x%p failed to abort\n",
360 return TASK_ABORT_FAILED;
368 static int sas_recover_lu(struct domain_device *dev, struct scsi_cmnd *cmd)
370 int res = TMF_RESP_FUNC_FAILED;
372 struct sas_internal *i =
373 to_sas_internal(dev->port->ha->core.shost->transportt);
375 int_to_scsilun(cmd->device->lun, &lun);
377 SAS_DPRINTK("eh: device %llx LUN %x has the task\n",
378 SAS_ADDR(dev->sas_addr),
381 if (i->dft->lldd_abort_task_set)
382 res = i->dft->lldd_abort_task_set(dev, lun.scsi_lun);
384 if (res == TMF_RESP_FUNC_FAILED) {
385 if (i->dft->lldd_clear_task_set)
386 res = i->dft->lldd_clear_task_set(dev, lun.scsi_lun);
389 if (res == TMF_RESP_FUNC_FAILED) {
390 if (i->dft->lldd_lu_reset)
391 res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
397 static int sas_recover_I_T(struct domain_device *dev)
399 int res = TMF_RESP_FUNC_FAILED;
400 struct sas_internal *i =
401 to_sas_internal(dev->port->ha->core.shost->transportt);
403 SAS_DPRINTK("I_T nexus reset for dev %016llx\n",
404 SAS_ADDR(dev->sas_addr));
406 if (i->dft->lldd_I_T_nexus_reset)
407 res = i->dft->lldd_I_T_nexus_reset(dev);
412 /* Find the sas_phy that's attached to this device */
413 struct sas_phy *find_local_sas_phy(struct domain_device *dev)
415 struct domain_device *pdev = dev->parent;
416 struct ex_phy *exphy = NULL;
419 /* Directly attached device */
421 return dev->port->phy;
423 /* Otherwise look in the expander */
424 for (i = 0; i < pdev->ex_dev.num_phys; i++)
425 if (!memcmp(dev->sas_addr,
426 pdev->ex_dev.ex_phy[i].attached_sas_addr,
428 exphy = &pdev->ex_dev.ex_phy[i];
436 /* Attempt to send a LUN reset message to a device */
437 int sas_eh_device_reset_handler(struct scsi_cmnd *cmd)
439 struct domain_device *dev = cmd_to_domain_dev(cmd);
440 struct sas_internal *i =
441 to_sas_internal(dev->port->ha->core.shost->transportt);
445 int_to_scsilun(cmd->device->lun, &lun);
447 if (!i->dft->lldd_lu_reset)
450 res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
451 if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
457 /* Attempt to send a phy (bus) reset */
458 int sas_eh_bus_reset_handler(struct scsi_cmnd *cmd)
460 struct domain_device *dev = cmd_to_domain_dev(cmd);
461 struct sas_phy *phy = find_local_sas_phy(dev);
464 res = sas_phy_reset(phy, 1);
466 SAS_DPRINTK("Bus reset of %s failed 0x%x\n",
467 phy->dev.kobj.k_name,
469 if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
475 /* Try to reset a device */
476 static int try_to_reset_cmd_device(struct Scsi_Host *shost,
477 struct scsi_cmnd *cmd)
481 if (!shost->hostt->eh_device_reset_handler)
484 res = shost->hostt->eh_device_reset_handler(cmd);
489 if (shost->hostt->eh_bus_reset_handler)
490 return shost->hostt->eh_bus_reset_handler(cmd);
495 static int sas_eh_handle_sas_errors(struct Scsi_Host *shost,
496 struct list_head *work_q,
497 struct list_head *done_q)
499 struct scsi_cmnd *cmd, *n;
500 enum task_disposition res = TASK_IS_DONE;
501 int tmf_resp, need_reset;
502 struct sas_internal *i = to_sas_internal(shost->transportt);
504 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
507 list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
508 struct sas_task *task = TO_SAS_TASK(cmd);
513 list_del_init(&cmd->eh_entry);
515 spin_lock_irqsave(&task->task_state_lock, flags);
516 need_reset = task->task_state_flags & SAS_TASK_NEED_DEV_RESET;
517 spin_unlock_irqrestore(&task->task_state_lock, flags);
519 SAS_DPRINTK("trying to find task 0x%p\n", task);
520 res = sas_scsi_find_task(task);
526 SAS_DPRINTK("%s: task 0x%p is done\n", __FUNCTION__,
528 task->task_done(task);
530 try_to_reset_cmd_device(shost, cmd);
532 case TASK_IS_ABORTED:
533 SAS_DPRINTK("%s: task 0x%p is aborted\n",
535 task->task_done(task);
537 try_to_reset_cmd_device(shost, cmd);
540 SAS_DPRINTK("task 0x%p is at LU: lu recover\n", task);
541 tmf_resp = sas_recover_lu(task->dev, cmd);
542 if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
543 SAS_DPRINTK("dev %016llx LU %x is "
547 task->task_done(task);
549 try_to_reset_cmd_device(shost, cmd);
550 sas_scsi_clear_queue_lu(work_q, cmd);
554 case TASK_IS_NOT_AT_LU:
555 case TASK_ABORT_FAILED:
556 SAS_DPRINTK("task 0x%p is not at LU: I_T recover\n",
558 tmf_resp = sas_recover_I_T(task->dev);
559 if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
560 SAS_DPRINTK("I_T %016llx recovered\n",
561 SAS_ADDR(task->dev->sas_addr));
562 task->task_done(task);
564 try_to_reset_cmd_device(shost, cmd);
565 sas_scsi_clear_queue_I_T(work_q, task->dev);
568 /* Hammer time :-) */
569 if (i->dft->lldd_clear_nexus_port) {
570 struct asd_sas_port *port = task->dev->port;
571 SAS_DPRINTK("clearing nexus for port:%d\n",
573 res = i->dft->lldd_clear_nexus_port(port);
574 if (res == TMF_RESP_FUNC_COMPLETE) {
575 SAS_DPRINTK("clear nexus port:%d "
576 "succeeded\n", port->id);
577 task->task_done(task);
579 try_to_reset_cmd_device(shost, cmd);
580 sas_scsi_clear_queue_port(work_q,
585 if (i->dft->lldd_clear_nexus_ha) {
586 SAS_DPRINTK("clear nexus ha\n");
587 res = i->dft->lldd_clear_nexus_ha(ha);
588 if (res == TMF_RESP_FUNC_COMPLETE) {
589 SAS_DPRINTK("clear nexus ha "
591 task->task_done(task);
593 try_to_reset_cmd_device(shost, cmd);
597 /* If we are here -- this means that no amount
598 * of effort could recover from errors. Quite
599 * possibly the HA just disappeared.
601 SAS_DPRINTK("error from device %llx, LUN %x "
602 "couldn't be recovered in any way\n",
603 SAS_ADDR(task->dev->sas_addr),
606 task->task_done(task);
608 try_to_reset_cmd_device(shost, cmd);
613 return list_empty(work_q);
615 SAS_DPRINTK("--- Exit %s -- clear_q\n", __FUNCTION__);
616 list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
617 struct sas_task *task = TO_SAS_TASK(cmd);
618 list_del_init(&cmd->eh_entry);
619 task->task_done(task);
621 return list_empty(work_q);
624 void sas_scsi_recover_host(struct Scsi_Host *shost)
626 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
628 LIST_HEAD(eh_work_q);
630 spin_lock_irqsave(shost->host_lock, flags);
631 list_splice_init(&shost->eh_cmd_q, &eh_work_q);
632 spin_unlock_irqrestore(shost->host_lock, flags);
634 SAS_DPRINTK("Enter %s\n", __FUNCTION__);
636 * Deal with commands that still have SAS tasks (i.e. they didn't
637 * complete via the normal sas_task completion mechanism)
639 if (sas_eh_handle_sas_errors(shost, &eh_work_q, &ha->eh_done_q))
643 * Now deal with SCSI commands that completed ok but have a an error
644 * code (and hopefully sense data) attached. This is roughly what
645 * scsi_unjam_host does, but we skip scsi_eh_abort_cmds because any
646 * command we see here has no sas_task and is thus unknown to the HA.
648 if (!scsi_eh_get_sense(&eh_work_q, &ha->eh_done_q))
649 scsi_eh_ready_devs(shost, &eh_work_q, &ha->eh_done_q);
652 scsi_eh_flush_done_q(&ha->eh_done_q);
653 SAS_DPRINTK("--- Exit %s\n", __FUNCTION__);
657 enum scsi_eh_timer_return sas_scsi_timed_out(struct scsi_cmnd *cmd)
659 struct sas_task *task = TO_SAS_TASK(cmd);
663 cmd->timeout_per_command /= 2;
664 SAS_DPRINTK("command 0x%p, task 0x%p, gone: %s\n",
665 cmd, task, (cmd->timeout_per_command ?
666 "EH_RESET_TIMER" : "EH_NOT_HANDLED"));
667 if (!cmd->timeout_per_command)
668 return EH_NOT_HANDLED;
669 return EH_RESET_TIMER;
672 spin_lock_irqsave(&task->task_state_lock, flags);
673 BUG_ON(task->task_state_flags & SAS_TASK_STATE_ABORTED);
674 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
675 spin_unlock_irqrestore(&task->task_state_lock, flags);
676 SAS_DPRINTK("command 0x%p, task 0x%p, timed out: EH_HANDLED\n",
680 if (!(task->task_state_flags & SAS_TASK_AT_INITIATOR)) {
681 spin_unlock_irqrestore(&task->task_state_lock, flags);
682 SAS_DPRINTK("command 0x%p, task 0x%p, not at initiator: "
685 return EH_RESET_TIMER;
687 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
688 spin_unlock_irqrestore(&task->task_state_lock, flags);
690 SAS_DPRINTK("command 0x%p, task 0x%p, timed out: EH_NOT_HANDLED\n",
693 return EH_NOT_HANDLED;
696 int sas_ioctl(struct scsi_device *sdev, int cmd, void __user *arg)
698 struct domain_device *dev = sdev_to_domain_dev(sdev);
700 if (dev_is_sata(dev))
701 return ata_scsi_ioctl(sdev, cmd, arg);
706 struct domain_device *sas_find_dev_by_rphy(struct sas_rphy *rphy)
708 struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent);
709 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
710 struct domain_device *found_dev = NULL;
714 spin_lock_irqsave(&ha->phy_port_lock, flags);
715 for (i = 0; i < ha->num_phys; i++) {
716 struct asd_sas_port *port = ha->sas_port[i];
717 struct domain_device *dev;
719 spin_lock(&port->dev_list_lock);
720 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
721 if (rphy == dev->rphy) {
723 spin_unlock(&port->dev_list_lock);
727 spin_unlock(&port->dev_list_lock);
730 spin_unlock_irqrestore(&ha->phy_port_lock, flags);
735 static inline struct domain_device *sas_find_target(struct scsi_target *starget)
737 struct sas_rphy *rphy = dev_to_rphy(starget->dev.parent);
739 return sas_find_dev_by_rphy(rphy);
742 int sas_target_alloc(struct scsi_target *starget)
744 struct domain_device *found_dev = sas_find_target(starget);
750 if (dev_is_sata(found_dev)) {
751 res = sas_ata_init_host_and_port(found_dev, starget);
756 starget->hostdata = found_dev;
760 #define SAS_DEF_QD 32
761 #define SAS_MAX_QD 64
763 int sas_slave_configure(struct scsi_device *scsi_dev)
765 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
766 struct sas_ha_struct *sas_ha;
768 BUG_ON(dev->rphy->identify.device_type != SAS_END_DEVICE);
770 if (dev_is_sata(dev)) {
771 ata_sas_slave_configure(scsi_dev, dev->sata_dev.ap);
775 sas_ha = dev->port->ha;
777 sas_read_port_mode_page(scsi_dev);
779 if (scsi_dev->tagged_supported) {
780 scsi_set_tag_type(scsi_dev, MSG_SIMPLE_TAG);
781 scsi_activate_tcq(scsi_dev, SAS_DEF_QD);
783 SAS_DPRINTK("device %llx, LUN %x doesn't support "
784 "TCQ\n", SAS_ADDR(dev->sas_addr),
786 scsi_dev->tagged_supported = 0;
787 scsi_set_tag_type(scsi_dev, 0);
788 scsi_deactivate_tcq(scsi_dev, 1);
791 scsi_dev->allow_restart = 1;
796 void sas_slave_destroy(struct scsi_device *scsi_dev)
798 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
800 if (dev_is_sata(dev))
801 ata_port_disable(dev->sata_dev.ap);
804 int sas_change_queue_depth(struct scsi_device *scsi_dev, int new_depth)
806 int res = min(new_depth, SAS_MAX_QD);
808 if (scsi_dev->tagged_supported)
809 scsi_adjust_queue_depth(scsi_dev, scsi_get_tag_type(scsi_dev),
812 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
813 sas_printk("device %llx LUN %x queue depth changed to 1\n",
814 SAS_ADDR(dev->sas_addr),
816 scsi_adjust_queue_depth(scsi_dev, 0, 1);
823 int sas_change_queue_type(struct scsi_device *scsi_dev, int qt)
825 if (!scsi_dev->tagged_supported)
828 scsi_deactivate_tcq(scsi_dev, 1);
830 scsi_set_tag_type(scsi_dev, qt);
831 scsi_activate_tcq(scsi_dev, scsi_dev->queue_depth);
836 int sas_bios_param(struct scsi_device *scsi_dev,
837 struct block_device *bdev,
838 sector_t capacity, int *hsc)
842 sector_div(capacity, 255*63);
848 /* ---------- Task Collector Thread implementation ---------- */
850 static void sas_queue(struct sas_ha_struct *sas_ha)
852 struct scsi_core *core = &sas_ha->core;
857 struct sas_internal *i = to_sas_internal(core->shost->transportt);
859 spin_lock_irqsave(&core->task_queue_lock, flags);
860 while (!kthread_should_stop() &&
861 !list_empty(&core->task_queue)) {
863 can_queue = sas_ha->lldd_queue_size - core->task_queue_size;
864 if (can_queue >= 0) {
865 can_queue = core->task_queue_size;
866 list_splice_init(&core->task_queue, &q);
868 struct list_head *a, *n;
870 can_queue = sas_ha->lldd_queue_size;
871 list_for_each_safe(a, n, &core->task_queue) {
872 list_move_tail(a, &q);
873 if (--can_queue == 0)
876 can_queue = sas_ha->lldd_queue_size;
878 core->task_queue_size -= can_queue;
879 spin_unlock_irqrestore(&core->task_queue_lock, flags);
881 struct sas_task *task = list_entry(q.next,
885 res = i->dft->lldd_execute_task(task, can_queue,
888 __list_add(&q, task->list.prev, &task->list);
890 spin_lock_irqsave(&core->task_queue_lock, flags);
892 list_splice_init(&q, &core->task_queue); /*at head*/
893 core->task_queue_size += can_queue;
896 spin_unlock_irqrestore(&core->task_queue_lock, flags);
900 * sas_queue_thread -- The Task Collector thread
901 * @_sas_ha: pointer to struct sas_ha
903 static int sas_queue_thread(void *_sas_ha)
905 struct sas_ha_struct *sas_ha = _sas_ha;
908 set_current_state(TASK_INTERRUPTIBLE);
911 if (kthread_should_stop())
918 int sas_init_queue(struct sas_ha_struct *sas_ha)
920 struct scsi_core *core = &sas_ha->core;
922 spin_lock_init(&core->task_queue_lock);
923 core->task_queue_size = 0;
924 INIT_LIST_HEAD(&core->task_queue);
926 core->queue_thread = kthread_run(sas_queue_thread, sas_ha,
927 "sas_queue_%d", core->shost->host_no);
928 if (IS_ERR(core->queue_thread))
929 return PTR_ERR(core->queue_thread);
933 void sas_shutdown_queue(struct sas_ha_struct *sas_ha)
936 struct scsi_core *core = &sas_ha->core;
937 struct sas_task *task, *n;
939 kthread_stop(core->queue_thread);
941 if (!list_empty(&core->task_queue))
942 SAS_DPRINTK("HA: %llx: scsi core task queue is NOT empty!?\n",
943 SAS_ADDR(sas_ha->sas_addr));
945 spin_lock_irqsave(&core->task_queue_lock, flags);
946 list_for_each_entry_safe(task, n, &core->task_queue, list) {
947 struct scsi_cmnd *cmd = task->uldd_task;
949 list_del_init(&task->list);
951 ASSIGN_SAS_TASK(cmd, NULL);
953 cmd->result = DID_ABORT << 16;
956 spin_unlock_irqrestore(&core->task_queue_lock, flags);
960 * Call the LLDD task abort routine directly. This function is intended for
961 * use by upper layers that need to tell the LLDD to abort a task.
963 int __sas_task_abort(struct sas_task *task)
965 struct sas_internal *si =
966 to_sas_internal(task->dev->port->ha->core.shost->transportt);
970 spin_lock_irqsave(&task->task_state_lock, flags);
971 if (task->task_state_flags & SAS_TASK_STATE_ABORTED ||
972 task->task_state_flags & SAS_TASK_STATE_DONE) {
973 spin_unlock_irqrestore(&task->task_state_lock, flags);
974 SAS_DPRINTK("%s: Task %p already finished.\n", __FUNCTION__,
978 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
979 spin_unlock_irqrestore(&task->task_state_lock, flags);
981 if (!si->dft->lldd_abort_task)
984 res = si->dft->lldd_abort_task(task);
986 spin_lock_irqsave(&task->task_state_lock, flags);
987 if ((task->task_state_flags & SAS_TASK_STATE_DONE) ||
988 (res == TMF_RESP_FUNC_COMPLETE))
990 spin_unlock_irqrestore(&task->task_state_lock, flags);
991 task->task_done(task);
995 if (!(task->task_state_flags & SAS_TASK_STATE_DONE))
996 task->task_state_flags &= ~SAS_TASK_STATE_ABORTED;
997 spin_unlock_irqrestore(&task->task_state_lock, flags);
1003 * Tell an upper layer that it needs to initiate an abort for a given task.
1004 * This should only ever be called by an LLDD.
1006 void sas_task_abort(struct sas_task *task)
1008 struct scsi_cmnd *sc = task->uldd_task;
1010 /* Escape for libsas internal commands */
1012 if (!del_timer(&task->timer))
1014 task->timer.function(task->timer.data);
1018 if (dev_is_sata(task->dev)) {
1019 sas_ata_task_abort(task);
1023 scsi_req_abort_cmd(sc);
1024 scsi_schedule_eh(sc->device->host);
1027 int sas_slave_alloc(struct scsi_device *scsi_dev)
1029 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
1031 if (dev_is_sata(dev))
1032 return ata_sas_port_init(dev->sata_dev.ap);
1037 void sas_target_destroy(struct scsi_target *starget)
1039 struct domain_device *found_dev = sas_find_target(starget);
1044 if (dev_is_sata(found_dev))
1045 ata_sas_port_destroy(found_dev->sata_dev.ap);
1050 EXPORT_SYMBOL_GPL(sas_queuecommand);
1051 EXPORT_SYMBOL_GPL(sas_target_alloc);
1052 EXPORT_SYMBOL_GPL(sas_slave_configure);
1053 EXPORT_SYMBOL_GPL(sas_slave_destroy);
1054 EXPORT_SYMBOL_GPL(sas_change_queue_depth);
1055 EXPORT_SYMBOL_GPL(sas_change_queue_type);
1056 EXPORT_SYMBOL_GPL(sas_bios_param);
1057 EXPORT_SYMBOL_GPL(__sas_task_abort);
1058 EXPORT_SYMBOL_GPL(sas_task_abort);
1059 EXPORT_SYMBOL_GPL(sas_phy_reset);
1060 EXPORT_SYMBOL_GPL(sas_phy_enable);
1061 EXPORT_SYMBOL_GPL(sas_eh_device_reset_handler);
1062 EXPORT_SYMBOL_GPL(sas_eh_bus_reset_handler);
1063 EXPORT_SYMBOL_GPL(sas_slave_alloc);
1064 EXPORT_SYMBOL_GPL(sas_target_destroy);
1065 EXPORT_SYMBOL_GPL(sas_ioctl);