1 // SPDX-License-Identifier: GPL-2.0-only
3 * Serial Attached SCSI (SAS) class SCSI Host glue.
5 * Copyright (C) 2005 Adaptec, Inc. All rights reserved.
9 #include <linux/kthread.h>
10 #include <linux/firmware.h>
11 #include <linux/export.h>
12 #include <linux/ctype.h>
13 #include <linux/kernel.h>
15 #include "sas_internal.h"
17 #include <scsi/scsi_host.h>
18 #include <scsi/scsi_device.h>
19 #include <scsi/scsi_tcq.h>
20 #include <scsi/scsi.h>
21 #include <scsi/scsi_eh.h>
22 #include <scsi/scsi_transport.h>
23 #include <scsi/scsi_transport_sas.h>
24 #include <scsi/sas_ata.h>
25 #include "../scsi_sas_internal.h"
26 #include "../scsi_transport_api.h"
27 #include "../scsi_priv.h"
29 #include <linux/err.h>
30 #include <linux/blkdev.h>
31 #include <linux/freezer.h>
32 #include <linux/gfp.h>
33 #include <linux/scatterlist.h>
34 #include <linux/libata.h>
36 /* record final status and free the task */
37 static void sas_end_task(struct scsi_cmnd *sc, struct sas_task *task)
39 struct task_status_struct *ts = &task->task_status;
42 if (ts->resp == SAS_TASK_UNDELIVERED) {
45 } else { /* ts->resp == SAS_TASK_COMPLETE */
46 /* task delivered, what happened afterwards? */
48 case SAS_DEV_NO_RESPONSE:
55 case SAS_DATA_UNDERRUN:
56 scsi_set_resid(sc, ts->residual);
57 if (scsi_bufflen(sc) - scsi_get_resid(sc) < sc->underflow)
60 case SAS_DATA_OVERRUN:
64 hs = DID_SOFT_ERROR; /* retry */
66 case SAS_DEVICE_UNKNOWN:
73 if (ts->open_rej_reason == SAS_OREJ_RSVD_RETRY)
74 hs = DID_SOFT_ERROR; /* retry */
78 case SAS_PROTO_RESPONSE:
79 pr_notice("LLDD:%s sent SAS_PROTO_RESP for an SSP task; please report this\n",
80 task->dev->port->ha->sas_ha_name);
82 case SAS_ABORTED_TASK:
85 case SAM_STAT_CHECK_CONDITION:
86 memcpy(sc->sense_buffer, ts->buf,
87 min(SCSI_SENSE_BUFFERSIZE, ts->buf_valid_size));
88 stat = SAM_STAT_CHECK_CONDITION;
96 sc->result = (hs << 16) | stat;
97 ASSIGN_SAS_TASK(sc, NULL);
101 static void sas_scsi_task_done(struct sas_task *task)
103 struct scsi_cmnd *sc = task->uldd_task;
104 struct domain_device *dev = task->dev;
105 struct sas_ha_struct *ha = dev->port->ha;
108 spin_lock_irqsave(&dev->done_lock, flags);
109 if (test_bit(SAS_HA_FROZEN, &ha->state))
112 ASSIGN_SAS_TASK(sc, NULL);
113 spin_unlock_irqrestore(&dev->done_lock, flags);
115 if (unlikely(!task)) {
116 /* task will be completed by the error handler */
117 pr_debug("task done but aborted\n");
122 pr_debug("task_done called with non existing SCSI cmnd!\n");
127 sas_end_task(sc, task);
131 static struct sas_task *sas_create_task(struct scsi_cmnd *cmd,
132 struct domain_device *dev,
135 struct sas_task *task = sas_alloc_task(gfp_flags);
141 task->uldd_task = cmd;
142 ASSIGN_SAS_TASK(cmd, task);
145 task->task_proto = task->dev->tproto; /* BUG_ON(!SSP) */
147 task->ssp_task.retry_count = 1;
148 int_to_scsilun(cmd->device->lun, &lun);
149 memcpy(task->ssp_task.LUN, &lun.scsi_lun, 8);
150 task->ssp_task.task_attr = TASK_ATTR_SIMPLE;
151 task->ssp_task.cmd = cmd;
153 task->scatter = scsi_sglist(cmd);
154 task->num_scatter = scsi_sg_count(cmd);
155 task->total_xfer_len = scsi_bufflen(cmd);
156 task->data_dir = cmd->sc_data_direction;
158 task->task_done = sas_scsi_task_done;
163 int sas_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
165 struct sas_internal *i = to_sas_internal(host->transportt);
166 struct domain_device *dev = cmd_to_domain_dev(cmd);
167 struct sas_task *task;
170 /* If the device fell off, no sense in issuing commands */
171 if (test_bit(SAS_DEV_GONE, &dev->state)) {
172 cmd->result = DID_BAD_TARGET << 16;
176 if (dev_is_sata(dev)) {
177 spin_lock_irq(dev->sata_dev.ap->lock);
178 res = ata_sas_queuecmd(cmd, dev->sata_dev.ap);
179 spin_unlock_irq(dev->sata_dev.ap->lock);
183 task = sas_create_task(cmd, dev, GFP_ATOMIC);
185 return SCSI_MLQUEUE_HOST_BUSY;
187 res = i->dft->lldd_execute_task(task, GFP_ATOMIC);
193 pr_debug("lldd_execute_task returned: %d\n", res);
194 ASSIGN_SAS_TASK(cmd, NULL);
196 if (res == -SAS_QUEUE_FULL)
197 cmd->result = DID_SOFT_ERROR << 16; /* retry */
199 cmd->result = DID_ERROR << 16;
205 static void sas_eh_finish_cmd(struct scsi_cmnd *cmd)
207 struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(cmd->device->host);
208 struct domain_device *dev = cmd_to_domain_dev(cmd);
209 struct sas_task *task = TO_SAS_TASK(cmd);
211 /* At this point, we only get called following an actual abort
212 * of the task, so we should be guaranteed not to be racing with
213 * any completions from the LLD. Task is freed after this.
215 sas_end_task(cmd, task);
217 if (dev_is_sata(dev)) {
218 /* defer commands to libata so that libata EH can
219 * handle ata qcs correctly
221 list_move_tail(&cmd->eh_entry, &sas_ha->eh_ata_q);
225 /* now finish the command and move it on to the error
226 * handler done list, this also takes it off the
227 * error handler pending list.
229 scsi_eh_finish_cmd(cmd, &sas_ha->eh_done_q);
232 static void sas_scsi_clear_queue_lu(struct list_head *error_q, struct scsi_cmnd *my_cmd)
234 struct scsi_cmnd *cmd, *n;
236 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
237 if (cmd->device->sdev_target == my_cmd->device->sdev_target &&
238 cmd->device->lun == my_cmd->device->lun)
239 sas_eh_finish_cmd(cmd);
243 static void sas_scsi_clear_queue_I_T(struct list_head *error_q,
244 struct domain_device *dev)
246 struct scsi_cmnd *cmd, *n;
248 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
249 struct domain_device *x = cmd_to_domain_dev(cmd);
252 sas_eh_finish_cmd(cmd);
256 static void sas_scsi_clear_queue_port(struct list_head *error_q,
257 struct asd_sas_port *port)
259 struct scsi_cmnd *cmd, *n;
261 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
262 struct domain_device *dev = cmd_to_domain_dev(cmd);
263 struct asd_sas_port *x = dev->port;
266 sas_eh_finish_cmd(cmd);
270 enum task_disposition {
278 static enum task_disposition sas_scsi_find_task(struct sas_task *task)
282 struct sas_internal *si =
283 to_sas_internal(task->dev->port->ha->core.shost->transportt);
285 for (i = 0; i < 5; i++) {
286 pr_notice("%s: aborting task 0x%p\n", __func__, task);
287 res = si->dft->lldd_abort_task(task);
289 spin_lock_irqsave(&task->task_state_lock, flags);
290 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
291 spin_unlock_irqrestore(&task->task_state_lock, flags);
292 pr_debug("%s: task 0x%p is done\n", __func__, task);
295 spin_unlock_irqrestore(&task->task_state_lock, flags);
297 if (res == TMF_RESP_FUNC_COMPLETE) {
298 pr_notice("%s: task 0x%p is aborted\n",
300 return TASK_IS_ABORTED;
301 } else if (si->dft->lldd_query_task) {
302 pr_notice("%s: querying task 0x%p\n", __func__, task);
303 res = si->dft->lldd_query_task(task);
305 case TMF_RESP_FUNC_SUCC:
306 pr_notice("%s: task 0x%p at LU\n", __func__,
308 return TASK_IS_AT_LU;
309 case TMF_RESP_FUNC_COMPLETE:
310 pr_notice("%s: task 0x%p not at LU\n",
312 return TASK_IS_NOT_AT_LU;
313 case TMF_RESP_FUNC_FAILED:
314 pr_notice("%s: task 0x%p failed to abort\n",
316 return TASK_ABORT_FAILED;
324 static int sas_recover_lu(struct domain_device *dev, struct scsi_cmnd *cmd)
326 int res = TMF_RESP_FUNC_FAILED;
328 struct sas_internal *i =
329 to_sas_internal(dev->port->ha->core.shost->transportt);
331 int_to_scsilun(cmd->device->lun, &lun);
333 pr_notice("eh: device %llx LUN %llx has the task\n",
334 SAS_ADDR(dev->sas_addr),
337 if (i->dft->lldd_abort_task_set)
338 res = i->dft->lldd_abort_task_set(dev, lun.scsi_lun);
340 if (res == TMF_RESP_FUNC_FAILED) {
341 if (i->dft->lldd_clear_task_set)
342 res = i->dft->lldd_clear_task_set(dev, lun.scsi_lun);
345 if (res == TMF_RESP_FUNC_FAILED) {
346 if (i->dft->lldd_lu_reset)
347 res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
353 static int sas_recover_I_T(struct domain_device *dev)
355 int res = TMF_RESP_FUNC_FAILED;
356 struct sas_internal *i =
357 to_sas_internal(dev->port->ha->core.shost->transportt);
359 pr_notice("I_T nexus reset for dev %016llx\n",
360 SAS_ADDR(dev->sas_addr));
362 if (i->dft->lldd_I_T_nexus_reset)
363 res = i->dft->lldd_I_T_nexus_reset(dev);
368 /* take a reference on the last known good phy for this device */
369 struct sas_phy *sas_get_local_phy(struct domain_device *dev)
371 struct sas_ha_struct *ha = dev->port->ha;
375 /* a published domain device always has a valid phy, it may be
376 * stale, but it is never NULL
380 spin_lock_irqsave(&ha->phy_port_lock, flags);
382 get_device(&phy->dev);
383 spin_unlock_irqrestore(&ha->phy_port_lock, flags);
387 EXPORT_SYMBOL_GPL(sas_get_local_phy);
389 static void sas_wait_eh(struct domain_device *dev)
391 struct sas_ha_struct *ha = dev->port->ha;
394 if (dev_is_sata(dev)) {
395 ata_port_wait_eh(dev->sata_dev.ap);
399 spin_lock_irq(&ha->lock);
401 while (test_bit(SAS_DEV_EH_PENDING, &dev->state)) {
402 prepare_to_wait(&ha->eh_wait_q, &wait, TASK_UNINTERRUPTIBLE);
403 spin_unlock_irq(&ha->lock);
405 spin_lock_irq(&ha->lock);
407 finish_wait(&ha->eh_wait_q, &wait);
409 spin_unlock_irq(&ha->lock);
411 /* make sure SCSI EH is complete */
412 if (scsi_host_in_recovery(ha->core.shost)) {
417 EXPORT_SYMBOL(sas_wait_eh);
419 static int sas_queue_reset(struct domain_device *dev, int reset_type,
422 struct sas_ha_struct *ha = dev->port->ha;
423 int scheduled = 0, tries = 100;
425 /* ata: promote lun reset to bus reset */
426 if (dev_is_sata(dev)) {
427 sas_ata_schedule_reset(dev);
429 sas_ata_wait_eh(dev);
433 while (!scheduled && tries--) {
434 spin_lock_irq(&ha->lock);
435 if (!test_bit(SAS_DEV_EH_PENDING, &dev->state) &&
436 !test_bit(reset_type, &dev->state)) {
439 list_add_tail(&dev->ssp_dev.eh_list_node, &ha->eh_dev_q);
440 set_bit(SAS_DEV_EH_PENDING, &dev->state);
441 set_bit(reset_type, &dev->state);
442 int_to_scsilun(lun, &dev->ssp_dev.reset_lun);
443 scsi_schedule_eh(ha->core.shost);
445 spin_unlock_irq(&ha->lock);
454 pr_warn("%s reset of %s failed\n",
455 reset_type == SAS_DEV_LU_RESET ? "LUN" : "Bus",
456 dev_name(&dev->rphy->dev));
461 int sas_eh_abort_handler(struct scsi_cmnd *cmd)
463 int res = TMF_RESP_FUNC_FAILED;
464 struct sas_task *task = TO_SAS_TASK(cmd);
465 struct Scsi_Host *host = cmd->device->host;
466 struct domain_device *dev = cmd_to_domain_dev(cmd);
467 struct sas_internal *i = to_sas_internal(host->transportt);
470 if (!i->dft->lldd_abort_task)
473 spin_lock_irqsave(host->host_lock, flags);
474 /* We cannot do async aborts for SATA devices */
475 if (dev_is_sata(dev) && !host->host_eh_scheduled) {
476 spin_unlock_irqrestore(host->host_lock, flags);
479 spin_unlock_irqrestore(host->host_lock, flags);
482 res = i->dft->lldd_abort_task(task);
484 pr_notice("no task to abort\n");
485 if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
490 EXPORT_SYMBOL_GPL(sas_eh_abort_handler);
492 /* Attempt to send a LUN reset message to a device */
493 int sas_eh_device_reset_handler(struct scsi_cmnd *cmd)
497 struct Scsi_Host *host = cmd->device->host;
498 struct domain_device *dev = cmd_to_domain_dev(cmd);
499 struct sas_internal *i = to_sas_internal(host->transportt);
501 if (current != host->ehandler)
502 return sas_queue_reset(dev, SAS_DEV_LU_RESET, cmd->device->lun, 0);
504 int_to_scsilun(cmd->device->lun, &lun);
506 if (!i->dft->lldd_lu_reset)
509 res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
510 if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
516 int sas_eh_target_reset_handler(struct scsi_cmnd *cmd)
519 struct Scsi_Host *host = cmd->device->host;
520 struct domain_device *dev = cmd_to_domain_dev(cmd);
521 struct sas_internal *i = to_sas_internal(host->transportt);
523 if (current != host->ehandler)
524 return sas_queue_reset(dev, SAS_DEV_RESET, 0, 0);
526 if (!i->dft->lldd_I_T_nexus_reset)
529 res = i->dft->lldd_I_T_nexus_reset(dev);
530 if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE ||
537 /* Try to reset a device */
538 static int try_to_reset_cmd_device(struct scsi_cmnd *cmd)
541 struct Scsi_Host *shost = cmd->device->host;
543 if (!shost->hostt->eh_device_reset_handler)
544 goto try_target_reset;
546 res = shost->hostt->eh_device_reset_handler(cmd);
551 if (shost->hostt->eh_target_reset_handler)
552 return shost->hostt->eh_target_reset_handler(cmd);
557 static void sas_eh_handle_sas_errors(struct Scsi_Host *shost, struct list_head *work_q)
559 struct scsi_cmnd *cmd, *n;
560 enum task_disposition res = TASK_IS_DONE;
561 int tmf_resp, need_reset;
562 struct sas_internal *i = to_sas_internal(shost->transportt);
564 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
567 /* clean out any commands that won the completion vs eh race */
568 list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
569 struct domain_device *dev = cmd_to_domain_dev(cmd);
570 struct sas_task *task;
572 spin_lock_irqsave(&dev->done_lock, flags);
573 /* by this point the lldd has either observed
574 * SAS_HA_FROZEN and is leaving the task alone, or has
575 * won the race with eh and decided to complete it
577 task = TO_SAS_TASK(cmd);
578 spin_unlock_irqrestore(&dev->done_lock, flags);
581 list_move_tail(&cmd->eh_entry, &done);
585 list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
586 struct sas_task *task = TO_SAS_TASK(cmd);
588 list_del_init(&cmd->eh_entry);
590 spin_lock_irqsave(&task->task_state_lock, flags);
591 need_reset = task->task_state_flags & SAS_TASK_NEED_DEV_RESET;
592 spin_unlock_irqrestore(&task->task_state_lock, flags);
595 pr_notice("%s: task 0x%p requests reset\n",
600 pr_debug("trying to find task 0x%p\n", task);
601 res = sas_scsi_find_task(task);
605 pr_notice("%s: task 0x%p is done\n", __func__,
607 sas_eh_finish_cmd(cmd);
609 case TASK_IS_ABORTED:
610 pr_notice("%s: task 0x%p is aborted\n",
612 sas_eh_finish_cmd(cmd);
615 pr_info("task 0x%p is at LU: lu recover\n", task);
617 tmf_resp = sas_recover_lu(task->dev, cmd);
618 if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
619 pr_notice("dev %016llx LU %llx is recovered\n",
622 sas_eh_finish_cmd(cmd);
623 sas_scsi_clear_queue_lu(work_q, cmd);
627 case TASK_IS_NOT_AT_LU:
628 case TASK_ABORT_FAILED:
629 pr_notice("task 0x%p is not at LU: I_T recover\n",
631 tmf_resp = sas_recover_I_T(task->dev);
632 if (tmf_resp == TMF_RESP_FUNC_COMPLETE ||
633 tmf_resp == -ENODEV) {
634 struct domain_device *dev = task->dev;
635 pr_notice("I_T %016llx recovered\n",
636 SAS_ADDR(task->dev->sas_addr));
637 sas_eh_finish_cmd(cmd);
638 sas_scsi_clear_queue_I_T(work_q, dev);
641 /* Hammer time :-) */
642 try_to_reset_cmd_device(cmd);
643 if (i->dft->lldd_clear_nexus_port) {
644 struct asd_sas_port *port = task->dev->port;
645 pr_debug("clearing nexus for port:%d\n",
647 res = i->dft->lldd_clear_nexus_port(port);
648 if (res == TMF_RESP_FUNC_COMPLETE) {
649 pr_notice("clear nexus port:%d succeeded\n",
651 sas_eh_finish_cmd(cmd);
652 sas_scsi_clear_queue_port(work_q,
657 if (i->dft->lldd_clear_nexus_ha) {
658 pr_debug("clear nexus ha\n");
659 res = i->dft->lldd_clear_nexus_ha(ha);
660 if (res == TMF_RESP_FUNC_COMPLETE) {
661 pr_notice("clear nexus ha succeeded\n");
662 sas_eh_finish_cmd(cmd);
666 /* If we are here -- this means that no amount
667 * of effort could recover from errors. Quite
668 * possibly the HA just disappeared.
670 pr_err("error from device %llx, LUN %llx couldn't be recovered in any way\n",
671 SAS_ADDR(task->dev->sas_addr),
674 sas_eh_finish_cmd(cmd);
679 list_splice_tail(&done, work_q);
680 list_splice_tail_init(&ha->eh_ata_q, work_q);
684 pr_debug("--- Exit %s -- clear_q\n", __func__);
685 list_for_each_entry_safe(cmd, n, work_q, eh_entry)
686 sas_eh_finish_cmd(cmd);
690 static void sas_eh_handle_resets(struct Scsi_Host *shost)
692 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
693 struct sas_internal *i = to_sas_internal(shost->transportt);
695 /* handle directed resets to sas devices */
696 spin_lock_irq(&ha->lock);
697 while (!list_empty(&ha->eh_dev_q)) {
698 struct domain_device *dev;
699 struct ssp_device *ssp;
701 ssp = list_entry(ha->eh_dev_q.next, typeof(*ssp), eh_list_node);
702 list_del_init(&ssp->eh_list_node);
703 dev = container_of(ssp, typeof(*dev), ssp_dev);
704 kref_get(&dev->kref);
705 WARN_ONCE(dev_is_sata(dev), "ssp reset to ata device?\n");
707 spin_unlock_irq(&ha->lock);
709 if (test_and_clear_bit(SAS_DEV_LU_RESET, &dev->state))
710 i->dft->lldd_lu_reset(dev, ssp->reset_lun.scsi_lun);
712 if (test_and_clear_bit(SAS_DEV_RESET, &dev->state))
713 i->dft->lldd_I_T_nexus_reset(dev);
716 spin_lock_irq(&ha->lock);
717 clear_bit(SAS_DEV_EH_PENDING, &dev->state);
720 spin_unlock_irq(&ha->lock);
724 void sas_scsi_recover_host(struct Scsi_Host *shost)
726 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
727 LIST_HEAD(eh_work_q);
734 spin_lock_irq(shost->host_lock);
735 list_splice_init(&shost->eh_cmd_q, &eh_work_q);
736 spin_unlock_irq(shost->host_lock);
738 pr_notice("Enter %s busy: %d failed: %d\n",
739 __func__, scsi_host_busy(shost), shost->host_failed);
741 * Deal with commands that still have SAS tasks (i.e. they didn't
742 * complete via the normal sas_task completion mechanism),
743 * SAS_HA_FROZEN gives eh dominion over all sas_task completion.
745 set_bit(SAS_HA_FROZEN, &ha->state);
746 sas_eh_handle_sas_errors(shost, &eh_work_q);
747 clear_bit(SAS_HA_FROZEN, &ha->state);
748 if (list_empty(&eh_work_q))
752 * Now deal with SCSI commands that completed ok but have a an error
753 * code (and hopefully sense data) attached. This is roughly what
754 * scsi_unjam_host does, but we skip scsi_eh_abort_cmds because any
755 * command we see here has no sas_task and is thus unknown to the HA.
757 sas_ata_eh(shost, &eh_work_q, &ha->eh_done_q);
758 if (!scsi_eh_get_sense(&eh_work_q, &ha->eh_done_q))
759 scsi_eh_ready_devs(shost, &eh_work_q, &ha->eh_done_q);
762 sas_eh_handle_resets(shost);
764 /* now link into libata eh --- if we have any ata devices */
765 sas_ata_strategy_handler(shost);
767 scsi_eh_flush_done_q(&ha->eh_done_q);
769 /* check if any new eh work was scheduled during the last run */
770 spin_lock_irq(&ha->lock);
771 if (ha->eh_active == 0) {
772 shost->host_eh_scheduled = 0;
775 spin_unlock_irq(&ha->lock);
780 pr_notice("--- Exit %s: busy: %d failed: %d tries: %d\n",
781 __func__, scsi_host_busy(shost),
782 shost->host_failed, tries);
785 int sas_ioctl(struct scsi_device *sdev, unsigned int cmd, void __user *arg)
787 struct domain_device *dev = sdev_to_domain_dev(sdev);
789 if (dev_is_sata(dev))
790 return ata_sas_scsi_ioctl(dev->sata_dev.ap, sdev, cmd, arg);
795 struct domain_device *sas_find_dev_by_rphy(struct sas_rphy *rphy)
797 struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent);
798 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
799 struct domain_device *found_dev = NULL;
803 spin_lock_irqsave(&ha->phy_port_lock, flags);
804 for (i = 0; i < ha->num_phys; i++) {
805 struct asd_sas_port *port = ha->sas_port[i];
806 struct domain_device *dev;
808 spin_lock(&port->dev_list_lock);
809 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
810 if (rphy == dev->rphy) {
812 spin_unlock(&port->dev_list_lock);
816 spin_unlock(&port->dev_list_lock);
819 spin_unlock_irqrestore(&ha->phy_port_lock, flags);
824 int sas_target_alloc(struct scsi_target *starget)
826 struct sas_rphy *rphy = dev_to_rphy(starget->dev.parent);
827 struct domain_device *found_dev = sas_find_dev_by_rphy(rphy);
832 kref_get(&found_dev->kref);
833 starget->hostdata = found_dev;
837 #define SAS_DEF_QD 256
839 int sas_slave_configure(struct scsi_device *scsi_dev)
841 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
843 BUG_ON(dev->rphy->identify.device_type != SAS_END_DEVICE);
845 if (dev_is_sata(dev)) {
846 ata_sas_slave_configure(scsi_dev, dev->sata_dev.ap);
850 sas_read_port_mode_page(scsi_dev);
852 if (scsi_dev->tagged_supported) {
853 scsi_change_queue_depth(scsi_dev, SAS_DEF_QD);
855 pr_notice("device %llx, LUN %llx doesn't support TCQ\n",
856 SAS_ADDR(dev->sas_addr), scsi_dev->lun);
857 scsi_change_queue_depth(scsi_dev, 1);
860 scsi_dev->allow_restart = 1;
865 int sas_change_queue_depth(struct scsi_device *sdev, int depth)
867 struct domain_device *dev = sdev_to_domain_dev(sdev);
869 if (dev_is_sata(dev))
870 return __ata_change_queue_depth(dev->sata_dev.ap, sdev, depth);
872 if (!sdev->tagged_supported)
874 return scsi_change_queue_depth(sdev, depth);
877 int sas_bios_param(struct scsi_device *scsi_dev,
878 struct block_device *bdev,
879 sector_t capacity, int *hsc)
883 sector_div(capacity, 255*63);
890 * Tell an upper layer that it needs to initiate an abort for a given task.
891 * This should only ever be called by an LLDD.
893 void sas_task_abort(struct sas_task *task)
895 struct scsi_cmnd *sc = task->uldd_task;
897 /* Escape for libsas internal commands */
899 struct sas_task_slow *slow = task->slow_task;
903 if (!del_timer(&slow->timer))
905 slow->timer.function(&slow->timer);
909 if (dev_is_sata(task->dev))
910 sas_ata_task_abort(task);
912 blk_abort_request(sc->request);
915 void sas_target_destroy(struct scsi_target *starget)
917 struct domain_device *found_dev = starget->hostdata;
922 starget->hostdata = NULL;
923 sas_put_device(found_dev);
926 #define SAS_STRING_ADDR_SIZE 16
928 int sas_request_addr(struct Scsi_Host *shost, u8 *addr)
931 const struct firmware *fw;
933 res = request_firmware(&fw, "sas_addr", &shost->shost_gendev);
937 if (fw->size < SAS_STRING_ADDR_SIZE) {
942 res = hex2bin(addr, fw->data, strnlen(fw->data, SAS_ADDR_SIZE * 2) / 2);
947 release_firmware(fw);
950 EXPORT_SYMBOL_GPL(sas_request_addr);
952 EXPORT_SYMBOL_GPL(sas_queuecommand);
953 EXPORT_SYMBOL_GPL(sas_target_alloc);
954 EXPORT_SYMBOL_GPL(sas_slave_configure);
955 EXPORT_SYMBOL_GPL(sas_change_queue_depth);
956 EXPORT_SYMBOL_GPL(sas_bios_param);
957 EXPORT_SYMBOL_GPL(sas_task_abort);
958 EXPORT_SYMBOL_GPL(sas_phy_reset);
959 EXPORT_SYMBOL_GPL(sas_eh_device_reset_handler);
960 EXPORT_SYMBOL_GPL(sas_eh_target_reset_handler);
961 EXPORT_SYMBOL_GPL(sas_target_destroy);
962 EXPORT_SYMBOL_GPL(sas_ioctl);