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>
27 #include <linux/firmware.h>
28 #include <linux/export.h>
29 #include <linux/ctype.h>
31 #include "sas_internal.h"
33 #include <scsi/scsi_host.h>
34 #include <scsi/scsi_device.h>
35 #include <scsi/scsi_tcq.h>
36 #include <scsi/scsi.h>
37 #include <scsi/scsi_eh.h>
38 #include <scsi/scsi_transport.h>
39 #include <scsi/scsi_transport_sas.h>
40 #include <scsi/sas_ata.h>
41 #include "../scsi_sas_internal.h"
42 #include "../scsi_transport_api.h"
43 #include "../scsi_priv.h"
45 #include <linux/err.h>
46 #include <linux/blkdev.h>
47 #include <linux/freezer.h>
48 #include <linux/gfp.h>
49 #include <linux/scatterlist.h>
50 #include <linux/libata.h>
52 /* record final status and free the task */
53 static void sas_end_task(struct scsi_cmnd *sc, struct sas_task *task)
55 struct task_status_struct *ts = &task->task_status;
58 if (ts->resp == SAS_TASK_UNDELIVERED) {
61 } else { /* ts->resp == SAS_TASK_COMPLETE */
62 /* task delivered, what happened afterwards? */
64 case SAS_DEV_NO_RESPONSE:
71 case SAS_DATA_UNDERRUN:
72 scsi_set_resid(sc, ts->residual);
73 if (scsi_bufflen(sc) - scsi_get_resid(sc) < sc->underflow)
76 case SAS_DATA_OVERRUN:
80 hs = DID_SOFT_ERROR; /* retry */
82 case SAS_DEVICE_UNKNOWN:
89 if (ts->open_rej_reason == SAS_OREJ_RSVD_RETRY)
90 hs = DID_SOFT_ERROR; /* retry */
94 case SAS_PROTO_RESPONSE:
95 SAS_DPRINTK("LLDD:%s sent SAS_PROTO_RESP for an SSP "
96 "task; please report this\n",
97 task->dev->port->ha->sas_ha_name);
99 case SAS_ABORTED_TASK:
102 case SAM_STAT_CHECK_CONDITION:
103 memcpy(sc->sense_buffer, ts->buf,
104 min(SCSI_SENSE_BUFFERSIZE, ts->buf_valid_size));
105 stat = SAM_STAT_CHECK_CONDITION;
113 sc->result = (hs << 16) | stat;
114 ASSIGN_SAS_TASK(sc, NULL);
115 list_del_init(&task->list);
119 static void sas_scsi_task_done(struct sas_task *task)
121 struct scsi_cmnd *sc = task->uldd_task;
122 struct domain_device *dev = task->dev;
123 struct sas_ha_struct *ha = dev->port->ha;
126 spin_lock_irqsave(&dev->done_lock, flags);
127 if (test_bit(SAS_HA_FROZEN, &ha->state))
130 ASSIGN_SAS_TASK(sc, NULL);
131 spin_unlock_irqrestore(&dev->done_lock, flags);
133 if (unlikely(!task)) {
134 /* task will be completed by the error handler */
135 SAS_DPRINTK("task done but aborted\n");
140 SAS_DPRINTK("task_done called with non existing SCSI cmnd!\n");
141 list_del_init(&task->list);
146 sas_end_task(sc, task);
150 static struct sas_task *sas_create_task(struct scsi_cmnd *cmd,
151 struct domain_device *dev,
154 struct sas_task *task = sas_alloc_task(gfp_flags);
160 task->uldd_task = cmd;
161 ASSIGN_SAS_TASK(cmd, task);
164 task->task_proto = task->dev->tproto; /* BUG_ON(!SSP) */
166 task->ssp_task.retry_count = 1;
167 int_to_scsilun(cmd->device->lun, &lun);
168 memcpy(task->ssp_task.LUN, &lun.scsi_lun, 8);
169 task->ssp_task.task_attr = TASK_ATTR_SIMPLE;
170 memcpy(task->ssp_task.cdb, cmd->cmnd, 16);
172 task->scatter = scsi_sglist(cmd);
173 task->num_scatter = scsi_sg_count(cmd);
174 task->total_xfer_len = scsi_bufflen(cmd);
175 task->data_dir = cmd->sc_data_direction;
177 task->task_done = sas_scsi_task_done;
182 int sas_queue_up(struct sas_task *task)
184 struct sas_ha_struct *sas_ha = task->dev->port->ha;
185 struct scsi_core *core = &sas_ha->core;
189 spin_lock_irqsave(&core->task_queue_lock, flags);
190 if (sas_ha->lldd_queue_size < core->task_queue_size + 1) {
191 spin_unlock_irqrestore(&core->task_queue_lock, flags);
192 return -SAS_QUEUE_FULL;
194 list_add_tail(&task->list, &core->task_queue);
195 core->task_queue_size += 1;
196 spin_unlock_irqrestore(&core->task_queue_lock, flags);
197 wake_up_process(core->queue_thread);
202 int sas_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
204 struct sas_internal *i = to_sas_internal(host->transportt);
205 struct domain_device *dev = cmd_to_domain_dev(cmd);
206 struct sas_ha_struct *sas_ha = dev->port->ha;
207 struct sas_task *task;
210 /* If the device fell off, no sense in issuing commands */
211 if (test_bit(SAS_DEV_GONE, &dev->state)) {
212 cmd->result = DID_BAD_TARGET << 16;
216 if (dev_is_sata(dev)) {
217 spin_lock_irq(dev->sata_dev.ap->lock);
218 res = ata_sas_queuecmd(cmd, dev->sata_dev.ap);
219 spin_unlock_irq(dev->sata_dev.ap->lock);
223 task = sas_create_task(cmd, dev, GFP_ATOMIC);
225 return SCSI_MLQUEUE_HOST_BUSY;
227 /* Queue up, Direct Mode or Task Collector Mode. */
228 if (sas_ha->lldd_max_execute_num < 2)
229 res = i->dft->lldd_execute_task(task, 1, GFP_ATOMIC);
231 res = sas_queue_up(task);
238 SAS_DPRINTK("lldd_execute_task returned: %d\n", res);
239 ASSIGN_SAS_TASK(cmd, NULL);
241 if (res == -SAS_QUEUE_FULL)
242 cmd->result = DID_SOFT_ERROR << 16; /* retry */
244 cmd->result = DID_ERROR << 16;
250 static void sas_eh_finish_cmd(struct scsi_cmnd *cmd)
252 struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(cmd->device->host);
253 struct sas_task *task = TO_SAS_TASK(cmd);
255 /* At this point, we only get called following an actual abort
256 * of the task, so we should be guaranteed not to be racing with
257 * any completions from the LLD. Task is freed after this.
259 sas_end_task(cmd, task);
261 /* now finish the command and move it on to the error
262 * handler done list, this also takes it off the
263 * error handler pending list.
265 scsi_eh_finish_cmd(cmd, &sas_ha->eh_done_q);
268 static void sas_eh_defer_cmd(struct scsi_cmnd *cmd)
270 struct domain_device *dev = cmd_to_domain_dev(cmd);
271 struct sas_ha_struct *ha = dev->port->ha;
272 struct sas_task *task = TO_SAS_TASK(cmd);
274 if (!dev_is_sata(dev)) {
275 sas_eh_finish_cmd(cmd);
279 /* report the timeout to libata */
280 sas_end_task(cmd, task);
281 list_move_tail(&cmd->eh_entry, &ha->eh_ata_q);
284 static void sas_scsi_clear_queue_lu(struct list_head *error_q, struct scsi_cmnd *my_cmd)
286 struct scsi_cmnd *cmd, *n;
288 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
289 if (cmd->device->sdev_target == my_cmd->device->sdev_target &&
290 cmd->device->lun == my_cmd->device->lun)
291 sas_eh_defer_cmd(cmd);
295 static void sas_scsi_clear_queue_I_T(struct list_head *error_q,
296 struct domain_device *dev)
298 struct scsi_cmnd *cmd, *n;
300 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
301 struct domain_device *x = cmd_to_domain_dev(cmd);
304 sas_eh_finish_cmd(cmd);
308 static void sas_scsi_clear_queue_port(struct list_head *error_q,
309 struct asd_sas_port *port)
311 struct scsi_cmnd *cmd, *n;
313 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
314 struct domain_device *dev = cmd_to_domain_dev(cmd);
315 struct asd_sas_port *x = dev->port;
318 sas_eh_finish_cmd(cmd);
322 enum task_disposition {
331 static enum task_disposition sas_scsi_find_task(struct sas_task *task)
333 struct sas_ha_struct *ha = task->dev->port->ha;
336 struct sas_internal *si =
337 to_sas_internal(task->dev->port->ha->core.shost->transportt);
339 if (ha->lldd_max_execute_num > 1) {
340 struct scsi_core *core = &ha->core;
341 struct sas_task *t, *n;
343 mutex_lock(&core->task_queue_flush);
344 spin_lock_irqsave(&core->task_queue_lock, flags);
345 list_for_each_entry_safe(t, n, &core->task_queue, list)
347 list_del_init(&t->list);
350 spin_unlock_irqrestore(&core->task_queue_lock, flags);
351 mutex_unlock(&core->task_queue_flush);
354 return TASK_IS_NOT_AT_HA;
357 for (i = 0; i < 5; i++) {
358 SAS_DPRINTK("%s: aborting task 0x%p\n", __func__, task);
359 res = si->dft->lldd_abort_task(task);
361 spin_lock_irqsave(&task->task_state_lock, flags);
362 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
363 spin_unlock_irqrestore(&task->task_state_lock, flags);
364 SAS_DPRINTK("%s: task 0x%p is done\n", __func__,
368 spin_unlock_irqrestore(&task->task_state_lock, flags);
370 if (res == TMF_RESP_FUNC_COMPLETE) {
371 SAS_DPRINTK("%s: task 0x%p is aborted\n",
373 return TASK_IS_ABORTED;
374 } else if (si->dft->lldd_query_task) {
375 SAS_DPRINTK("%s: querying task 0x%p\n",
377 res = si->dft->lldd_query_task(task);
379 case TMF_RESP_FUNC_SUCC:
380 SAS_DPRINTK("%s: task 0x%p at LU\n",
382 return TASK_IS_AT_LU;
383 case TMF_RESP_FUNC_COMPLETE:
384 SAS_DPRINTK("%s: task 0x%p not at LU\n",
386 return TASK_IS_NOT_AT_LU;
387 case TMF_RESP_FUNC_FAILED:
388 SAS_DPRINTK("%s: task 0x%p failed to abort\n",
390 return TASK_ABORT_FAILED;
398 static int sas_recover_lu(struct domain_device *dev, struct scsi_cmnd *cmd)
400 int res = TMF_RESP_FUNC_FAILED;
402 struct sas_internal *i =
403 to_sas_internal(dev->port->ha->core.shost->transportt);
405 int_to_scsilun(cmd->device->lun, &lun);
407 SAS_DPRINTK("eh: device %llx LUN %x has the task\n",
408 SAS_ADDR(dev->sas_addr),
411 if (i->dft->lldd_abort_task_set)
412 res = i->dft->lldd_abort_task_set(dev, lun.scsi_lun);
414 if (res == TMF_RESP_FUNC_FAILED) {
415 if (i->dft->lldd_clear_task_set)
416 res = i->dft->lldd_clear_task_set(dev, lun.scsi_lun);
419 if (res == TMF_RESP_FUNC_FAILED) {
420 if (i->dft->lldd_lu_reset)
421 res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
427 static int sas_recover_I_T(struct domain_device *dev)
429 int res = TMF_RESP_FUNC_FAILED;
430 struct sas_internal *i =
431 to_sas_internal(dev->port->ha->core.shost->transportt);
433 SAS_DPRINTK("I_T nexus reset for dev %016llx\n",
434 SAS_ADDR(dev->sas_addr));
436 if (i->dft->lldd_I_T_nexus_reset)
437 res = i->dft->lldd_I_T_nexus_reset(dev);
442 /* take a reference on the last known good phy for this device */
443 struct sas_phy *sas_get_local_phy(struct domain_device *dev)
445 struct sas_ha_struct *ha = dev->port->ha;
449 /* a published domain device always has a valid phy, it may be
450 * stale, but it is never NULL
454 spin_lock_irqsave(&ha->phy_port_lock, flags);
456 get_device(&phy->dev);
457 spin_unlock_irqrestore(&ha->phy_port_lock, flags);
461 EXPORT_SYMBOL_GPL(sas_get_local_phy);
463 static void sas_wait_eh(struct domain_device *dev)
465 struct sas_ha_struct *ha = dev->port->ha;
468 if (dev_is_sata(dev)) {
469 ata_port_wait_eh(dev->sata_dev.ap);
473 spin_lock_irq(&ha->lock);
475 while (test_bit(SAS_DEV_EH_PENDING, &dev->state)) {
476 prepare_to_wait(&ha->eh_wait_q, &wait, TASK_UNINTERRUPTIBLE);
477 spin_unlock_irq(&ha->lock);
479 spin_lock_irq(&ha->lock);
481 finish_wait(&ha->eh_wait_q, &wait);
483 spin_unlock_irq(&ha->lock);
485 /* make sure SCSI EH is complete */
486 if (scsi_host_in_recovery(ha->core.shost)) {
491 EXPORT_SYMBOL(sas_wait_eh);
493 static int sas_queue_reset(struct domain_device *dev, int reset_type, int lun, int wait)
495 struct sas_ha_struct *ha = dev->port->ha;
496 int scheduled = 0, tries = 100;
498 /* ata: promote lun reset to bus reset */
499 if (dev_is_sata(dev)) {
500 sas_ata_schedule_reset(dev);
502 sas_ata_wait_eh(dev);
506 while (!scheduled && tries--) {
507 spin_lock_irq(&ha->lock);
508 if (!test_bit(SAS_DEV_EH_PENDING, &dev->state) &&
509 !test_bit(reset_type, &dev->state)) {
512 list_add_tail(&dev->ssp_dev.eh_list_node, &ha->eh_dev_q);
513 set_bit(SAS_DEV_EH_PENDING, &dev->state);
514 set_bit(reset_type, &dev->state);
515 int_to_scsilun(lun, &dev->ssp_dev.reset_lun);
516 scsi_schedule_eh(ha->core.shost);
518 spin_unlock_irq(&ha->lock);
527 SAS_DPRINTK("%s reset of %s failed\n",
528 reset_type == SAS_DEV_LU_RESET ? "LUN" : "Bus",
529 dev_name(&dev->rphy->dev));
534 int sas_eh_abort_handler(struct scsi_cmnd *cmd)
537 struct sas_task *task = TO_SAS_TASK(cmd);
538 struct Scsi_Host *host = cmd->device->host;
539 struct sas_internal *i = to_sas_internal(host->transportt);
541 if (current != host->ehandler)
544 if (!i->dft->lldd_abort_task)
547 res = i->dft->lldd_abort_task(task);
548 if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
553 EXPORT_SYMBOL_GPL(sas_eh_abort_handler);
555 /* Attempt to send a LUN reset message to a device */
556 int sas_eh_device_reset_handler(struct scsi_cmnd *cmd)
560 struct Scsi_Host *host = cmd->device->host;
561 struct domain_device *dev = cmd_to_domain_dev(cmd);
562 struct sas_internal *i = to_sas_internal(host->transportt);
564 if (current != host->ehandler)
565 return sas_queue_reset(dev, SAS_DEV_LU_RESET, cmd->device->lun, 0);
567 int_to_scsilun(cmd->device->lun, &lun);
569 if (!i->dft->lldd_lu_reset)
572 res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
573 if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
579 /* Attempt to send a phy (bus) reset */
580 int sas_eh_bus_reset_handler(struct scsi_cmnd *cmd)
582 struct domain_device *dev = cmd_to_domain_dev(cmd);
583 struct sas_phy *phy = sas_get_local_phy(dev);
584 struct Scsi_Host *host = cmd->device->host;
587 if (current != host->ehandler)
588 return sas_queue_reset(dev, SAS_DEV_RESET, 0, 0);
590 res = sas_phy_reset(phy, 1);
592 SAS_DPRINTK("Bus reset of %s failed 0x%x\n",
593 kobject_name(&phy->dev.kobj),
595 sas_put_local_phy(phy);
597 if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
603 /* Try to reset a device */
604 static int try_to_reset_cmd_device(struct scsi_cmnd *cmd)
607 struct Scsi_Host *shost = cmd->device->host;
609 if (!shost->hostt->eh_device_reset_handler)
612 res = shost->hostt->eh_device_reset_handler(cmd);
617 if (shost->hostt->eh_bus_reset_handler)
618 return shost->hostt->eh_bus_reset_handler(cmd);
623 static void sas_eh_handle_sas_errors(struct Scsi_Host *shost, struct list_head *work_q)
625 struct scsi_cmnd *cmd, *n;
626 enum task_disposition res = TASK_IS_DONE;
627 int tmf_resp, need_reset;
628 struct sas_internal *i = to_sas_internal(shost->transportt);
630 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
633 /* clean out any commands that won the completion vs eh race */
634 list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
635 struct domain_device *dev = cmd_to_domain_dev(cmd);
636 struct sas_task *task;
638 spin_lock_irqsave(&dev->done_lock, flags);
639 /* by this point the lldd has either observed
640 * SAS_HA_FROZEN and is leaving the task alone, or has
641 * won the race with eh and decided to complete it
643 task = TO_SAS_TASK(cmd);
644 spin_unlock_irqrestore(&dev->done_lock, flags);
647 list_move_tail(&cmd->eh_entry, &done);
651 list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
652 struct sas_task *task = TO_SAS_TASK(cmd);
654 list_del_init(&cmd->eh_entry);
656 spin_lock_irqsave(&task->task_state_lock, flags);
657 need_reset = task->task_state_flags & SAS_TASK_NEED_DEV_RESET;
658 spin_unlock_irqrestore(&task->task_state_lock, flags);
661 SAS_DPRINTK("%s: task 0x%p requests reset\n",
666 SAS_DPRINTK("trying to find task 0x%p\n", task);
667 res = sas_scsi_find_task(task);
672 case TASK_IS_NOT_AT_HA:
673 SAS_DPRINTK("%s: task 0x%p is not at ha: %s\n",
675 cmd->retries ? "retry" : "aborted");
678 sas_eh_finish_cmd(cmd);
681 SAS_DPRINTK("%s: task 0x%p is done\n", __func__,
683 sas_eh_defer_cmd(cmd);
685 case TASK_IS_ABORTED:
686 SAS_DPRINTK("%s: task 0x%p is aborted\n",
688 sas_eh_defer_cmd(cmd);
691 SAS_DPRINTK("task 0x%p is at LU: lu recover\n", task);
693 tmf_resp = sas_recover_lu(task->dev, cmd);
694 if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
695 SAS_DPRINTK("dev %016llx LU %x is "
699 sas_eh_defer_cmd(cmd);
700 sas_scsi_clear_queue_lu(work_q, cmd);
704 case TASK_IS_NOT_AT_LU:
705 case TASK_ABORT_FAILED:
706 SAS_DPRINTK("task 0x%p is not at LU: I_T recover\n",
708 tmf_resp = sas_recover_I_T(task->dev);
709 if (tmf_resp == TMF_RESP_FUNC_COMPLETE ||
710 tmf_resp == -ENODEV) {
711 struct domain_device *dev = task->dev;
712 SAS_DPRINTK("I_T %016llx recovered\n",
713 SAS_ADDR(task->dev->sas_addr));
714 sas_eh_finish_cmd(cmd);
715 sas_scsi_clear_queue_I_T(work_q, dev);
718 /* Hammer time :-) */
719 try_to_reset_cmd_device(cmd);
720 if (i->dft->lldd_clear_nexus_port) {
721 struct asd_sas_port *port = task->dev->port;
722 SAS_DPRINTK("clearing nexus for port:%d\n",
724 res = i->dft->lldd_clear_nexus_port(port);
725 if (res == TMF_RESP_FUNC_COMPLETE) {
726 SAS_DPRINTK("clear nexus port:%d "
727 "succeeded\n", port->id);
728 sas_eh_finish_cmd(cmd);
729 sas_scsi_clear_queue_port(work_q,
734 if (i->dft->lldd_clear_nexus_ha) {
735 SAS_DPRINTK("clear nexus ha\n");
736 res = i->dft->lldd_clear_nexus_ha(ha);
737 if (res == TMF_RESP_FUNC_COMPLETE) {
738 SAS_DPRINTK("clear nexus ha "
740 sas_eh_finish_cmd(cmd);
744 /* If we are here -- this means that no amount
745 * of effort could recover from errors. Quite
746 * possibly the HA just disappeared.
748 SAS_DPRINTK("error from device %llx, LUN %x "
749 "couldn't be recovered in any way\n",
750 SAS_ADDR(task->dev->sas_addr),
753 sas_eh_finish_cmd(cmd);
758 list_splice_tail(&done, work_q);
759 list_splice_tail_init(&ha->eh_ata_q, work_q);
763 SAS_DPRINTK("--- Exit %s -- clear_q\n", __func__);
764 list_for_each_entry_safe(cmd, n, work_q, eh_entry)
765 sas_eh_finish_cmd(cmd);
769 static void sas_eh_handle_resets(struct Scsi_Host *shost)
771 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
772 struct sas_internal *i = to_sas_internal(shost->transportt);
774 /* handle directed resets to sas devices */
775 spin_lock_irq(&ha->lock);
776 while (!list_empty(&ha->eh_dev_q)) {
777 struct domain_device *dev;
778 struct ssp_device *ssp;
780 ssp = list_entry(ha->eh_dev_q.next, typeof(*ssp), eh_list_node);
781 list_del_init(&ssp->eh_list_node);
782 dev = container_of(ssp, typeof(*dev), ssp_dev);
783 kref_get(&dev->kref);
784 WARN_ONCE(dev_is_sata(dev), "ssp reset to ata device?\n");
786 spin_unlock_irq(&ha->lock);
788 if (test_and_clear_bit(SAS_DEV_LU_RESET, &dev->state))
789 i->dft->lldd_lu_reset(dev, ssp->reset_lun.scsi_lun);
791 if (test_and_clear_bit(SAS_DEV_RESET, &dev->state))
792 i->dft->lldd_I_T_nexus_reset(dev);
795 spin_lock_irq(&ha->lock);
796 clear_bit(SAS_DEV_EH_PENDING, &dev->state);
799 spin_unlock_irq(&ha->lock);
803 void sas_scsi_recover_host(struct Scsi_Host *shost)
805 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
806 LIST_HEAD(eh_work_q);
813 spin_lock_irq(shost->host_lock);
814 list_splice_init(&shost->eh_cmd_q, &eh_work_q);
815 spin_unlock_irq(shost->host_lock);
817 SAS_DPRINTK("Enter %s busy: %d failed: %d\n",
818 __func__, shost->host_busy, shost->host_failed);
820 * Deal with commands that still have SAS tasks (i.e. they didn't
821 * complete via the normal sas_task completion mechanism),
822 * SAS_HA_FROZEN gives eh dominion over all sas_task completion.
824 set_bit(SAS_HA_FROZEN, &ha->state);
825 sas_eh_handle_sas_errors(shost, &eh_work_q);
826 clear_bit(SAS_HA_FROZEN, &ha->state);
827 if (list_empty(&eh_work_q))
831 * Now deal with SCSI commands that completed ok but have a an error
832 * code (and hopefully sense data) attached. This is roughly what
833 * scsi_unjam_host does, but we skip scsi_eh_abort_cmds because any
834 * command we see here has no sas_task and is thus unknown to the HA.
836 sas_ata_eh(shost, &eh_work_q, &ha->eh_done_q);
837 if (!scsi_eh_get_sense(&eh_work_q, &ha->eh_done_q))
838 scsi_eh_ready_devs(shost, &eh_work_q, &ha->eh_done_q);
841 if (ha->lldd_max_execute_num > 1)
842 wake_up_process(ha->core.queue_thread);
844 sas_eh_handle_resets(shost);
846 /* now link into libata eh --- if we have any ata devices */
847 sas_ata_strategy_handler(shost);
849 scsi_eh_flush_done_q(&ha->eh_done_q);
851 /* check if any new eh work was scheduled during the last run */
852 spin_lock_irq(&ha->lock);
853 if (ha->eh_active == 0) {
854 shost->host_eh_scheduled = 0;
857 spin_unlock_irq(&ha->lock);
862 SAS_DPRINTK("--- Exit %s: busy: %d failed: %d tries: %d\n",
863 __func__, shost->host_busy, shost->host_failed, tries);
866 enum blk_eh_timer_return sas_scsi_timed_out(struct scsi_cmnd *cmd)
868 scmd_printk(KERN_DEBUG, cmd, "command %p timed out\n", cmd);
870 return BLK_EH_NOT_HANDLED;
873 int sas_ioctl(struct scsi_device *sdev, int cmd, void __user *arg)
875 struct domain_device *dev = sdev_to_domain_dev(sdev);
877 if (dev_is_sata(dev))
878 return ata_sas_scsi_ioctl(dev->sata_dev.ap, sdev, cmd, arg);
883 struct domain_device *sas_find_dev_by_rphy(struct sas_rphy *rphy)
885 struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent);
886 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
887 struct domain_device *found_dev = NULL;
891 spin_lock_irqsave(&ha->phy_port_lock, flags);
892 for (i = 0; i < ha->num_phys; i++) {
893 struct asd_sas_port *port = ha->sas_port[i];
894 struct domain_device *dev;
896 spin_lock(&port->dev_list_lock);
897 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
898 if (rphy == dev->rphy) {
900 spin_unlock(&port->dev_list_lock);
904 spin_unlock(&port->dev_list_lock);
907 spin_unlock_irqrestore(&ha->phy_port_lock, flags);
912 int sas_target_alloc(struct scsi_target *starget)
914 struct sas_rphy *rphy = dev_to_rphy(starget->dev.parent);
915 struct domain_device *found_dev = sas_find_dev_by_rphy(rphy);
920 kref_get(&found_dev->kref);
921 starget->hostdata = found_dev;
925 #define SAS_DEF_QD 256
927 int sas_slave_configure(struct scsi_device *scsi_dev)
929 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
930 struct sas_ha_struct *sas_ha;
932 BUG_ON(dev->rphy->identify.device_type != SAS_END_DEVICE);
934 if (dev_is_sata(dev)) {
935 ata_sas_slave_configure(scsi_dev, dev->sata_dev.ap);
939 sas_ha = dev->port->ha;
941 sas_read_port_mode_page(scsi_dev);
943 if (scsi_dev->tagged_supported) {
944 scsi_set_tag_type(scsi_dev, MSG_SIMPLE_TAG);
945 scsi_activate_tcq(scsi_dev, SAS_DEF_QD);
947 SAS_DPRINTK("device %llx, LUN %x doesn't support "
948 "TCQ\n", SAS_ADDR(dev->sas_addr),
950 scsi_dev->tagged_supported = 0;
951 scsi_set_tag_type(scsi_dev, 0);
952 scsi_deactivate_tcq(scsi_dev, 1);
955 scsi_dev->allow_restart = 1;
960 int sas_change_queue_depth(struct scsi_device *sdev, int depth, int reason)
962 struct domain_device *dev = sdev_to_domain_dev(sdev);
964 if (dev_is_sata(dev))
965 return __ata_change_queue_depth(dev->sata_dev.ap, sdev, depth,
969 case SCSI_QDEPTH_DEFAULT:
970 case SCSI_QDEPTH_RAMP_UP:
971 if (!sdev->tagged_supported)
973 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
975 case SCSI_QDEPTH_QFULL:
976 scsi_track_queue_full(sdev, depth);
985 int sas_change_queue_type(struct scsi_device *scsi_dev, int qt)
987 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
989 if (dev_is_sata(dev))
992 if (!scsi_dev->tagged_supported)
995 scsi_deactivate_tcq(scsi_dev, 1);
997 scsi_set_tag_type(scsi_dev, qt);
998 scsi_activate_tcq(scsi_dev, scsi_dev->queue_depth);
1003 int sas_bios_param(struct scsi_device *scsi_dev,
1004 struct block_device *bdev,
1005 sector_t capacity, int *hsc)
1009 sector_div(capacity, 255*63);
1015 /* ---------- Task Collector Thread implementation ---------- */
1017 static void sas_queue(struct sas_ha_struct *sas_ha)
1019 struct scsi_core *core = &sas_ha->core;
1020 unsigned long flags;
1024 struct sas_internal *i = to_sas_internal(core->shost->transportt);
1026 mutex_lock(&core->task_queue_flush);
1027 spin_lock_irqsave(&core->task_queue_lock, flags);
1028 while (!kthread_should_stop() &&
1029 !list_empty(&core->task_queue) &&
1030 !test_bit(SAS_HA_FROZEN, &sas_ha->state)) {
1032 can_queue = sas_ha->lldd_queue_size - core->task_queue_size;
1033 if (can_queue >= 0) {
1034 can_queue = core->task_queue_size;
1035 list_splice_init(&core->task_queue, &q);
1037 struct list_head *a, *n;
1039 can_queue = sas_ha->lldd_queue_size;
1040 list_for_each_safe(a, n, &core->task_queue) {
1041 list_move_tail(a, &q);
1042 if (--can_queue == 0)
1045 can_queue = sas_ha->lldd_queue_size;
1047 core->task_queue_size -= can_queue;
1048 spin_unlock_irqrestore(&core->task_queue_lock, flags);
1050 struct sas_task *task = list_entry(q.next,
1054 res = i->dft->lldd_execute_task(task, can_queue,
1057 __list_add(&q, task->list.prev, &task->list);
1059 spin_lock_irqsave(&core->task_queue_lock, flags);
1061 list_splice_init(&q, &core->task_queue); /*at head*/
1062 core->task_queue_size += can_queue;
1065 spin_unlock_irqrestore(&core->task_queue_lock, flags);
1066 mutex_unlock(&core->task_queue_flush);
1070 * sas_queue_thread -- The Task Collector thread
1071 * @_sas_ha: pointer to struct sas_ha
1073 static int sas_queue_thread(void *_sas_ha)
1075 struct sas_ha_struct *sas_ha = _sas_ha;
1078 set_current_state(TASK_INTERRUPTIBLE);
1081 if (kthread_should_stop())
1088 int sas_init_queue(struct sas_ha_struct *sas_ha)
1090 struct scsi_core *core = &sas_ha->core;
1092 spin_lock_init(&core->task_queue_lock);
1093 mutex_init(&core->task_queue_flush);
1094 core->task_queue_size = 0;
1095 INIT_LIST_HEAD(&core->task_queue);
1097 core->queue_thread = kthread_run(sas_queue_thread, sas_ha,
1098 "sas_queue_%d", core->shost->host_no);
1099 if (IS_ERR(core->queue_thread))
1100 return PTR_ERR(core->queue_thread);
1104 void sas_shutdown_queue(struct sas_ha_struct *sas_ha)
1106 unsigned long flags;
1107 struct scsi_core *core = &sas_ha->core;
1108 struct sas_task *task, *n;
1110 kthread_stop(core->queue_thread);
1112 if (!list_empty(&core->task_queue))
1113 SAS_DPRINTK("HA: %llx: scsi core task queue is NOT empty!?\n",
1114 SAS_ADDR(sas_ha->sas_addr));
1116 spin_lock_irqsave(&core->task_queue_lock, flags);
1117 list_for_each_entry_safe(task, n, &core->task_queue, list) {
1118 struct scsi_cmnd *cmd = task->uldd_task;
1120 list_del_init(&task->list);
1122 ASSIGN_SAS_TASK(cmd, NULL);
1123 sas_free_task(task);
1124 cmd->result = DID_ABORT << 16;
1125 cmd->scsi_done(cmd);
1127 spin_unlock_irqrestore(&core->task_queue_lock, flags);
1131 * Tell an upper layer that it needs to initiate an abort for a given task.
1132 * This should only ever be called by an LLDD.
1134 void sas_task_abort(struct sas_task *task)
1136 struct scsi_cmnd *sc = task->uldd_task;
1138 /* Escape for libsas internal commands */
1140 if (!del_timer(&task->timer))
1142 task->timer.function(task->timer.data);
1146 if (dev_is_sata(task->dev)) {
1147 sas_ata_task_abort(task);
1149 struct request_queue *q = sc->device->request_queue;
1150 unsigned long flags;
1152 spin_lock_irqsave(q->queue_lock, flags);
1153 blk_abort_request(sc->request);
1154 spin_unlock_irqrestore(q->queue_lock, flags);
1158 void sas_target_destroy(struct scsi_target *starget)
1160 struct domain_device *found_dev = starget->hostdata;
1165 starget->hostdata = NULL;
1166 sas_put_device(found_dev);
1169 static void sas_parse_addr(u8 *sas_addr, const char *p)
1172 for (i = 0; i < SAS_ADDR_SIZE; i++) {
1176 h = isdigit(*p) ? *p-'0' : toupper(*p)-'A'+10;
1178 l = isdigit(*p) ? *p-'0' : toupper(*p)-'A'+10;
1180 sas_addr[i] = (h<<4) | l;
1184 #define SAS_STRING_ADDR_SIZE 16
1186 int sas_request_addr(struct Scsi_Host *shost, u8 *addr)
1189 const struct firmware *fw;
1191 res = request_firmware(&fw, "sas_addr", &shost->shost_gendev);
1195 if (fw->size < SAS_STRING_ADDR_SIZE) {
1200 sas_parse_addr(addr, fw->data);
1203 release_firmware(fw);
1206 EXPORT_SYMBOL_GPL(sas_request_addr);
1208 EXPORT_SYMBOL_GPL(sas_queuecommand);
1209 EXPORT_SYMBOL_GPL(sas_target_alloc);
1210 EXPORT_SYMBOL_GPL(sas_slave_configure);
1211 EXPORT_SYMBOL_GPL(sas_change_queue_depth);
1212 EXPORT_SYMBOL_GPL(sas_change_queue_type);
1213 EXPORT_SYMBOL_GPL(sas_bios_param);
1214 EXPORT_SYMBOL_GPL(sas_task_abort);
1215 EXPORT_SYMBOL_GPL(sas_phy_reset);
1216 EXPORT_SYMBOL_GPL(sas_eh_device_reset_handler);
1217 EXPORT_SYMBOL_GPL(sas_eh_bus_reset_handler);
1218 EXPORT_SYMBOL_GPL(sas_target_destroy);
1219 EXPORT_SYMBOL_GPL(sas_ioctl);