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);
118 static void sas_scsi_task_done(struct sas_task *task)
120 struct scsi_cmnd *sc = task->uldd_task;
121 struct domain_device *dev = task->dev;
122 struct sas_ha_struct *ha = dev->port->ha;
125 spin_lock_irqsave(&dev->done_lock, flags);
126 if (test_bit(SAS_HA_FROZEN, &ha->state))
129 ASSIGN_SAS_TASK(sc, NULL);
130 spin_unlock_irqrestore(&dev->done_lock, flags);
132 if (unlikely(!task)) {
133 /* task will be completed by the error handler */
134 SAS_DPRINTK("task done but aborted\n");
139 SAS_DPRINTK("task_done called with non existing SCSI cmnd!\n");
144 sas_end_task(sc, task);
148 static struct sas_task *sas_create_task(struct scsi_cmnd *cmd,
149 struct domain_device *dev,
152 struct sas_task *task = sas_alloc_task(gfp_flags);
158 task->uldd_task = cmd;
159 ASSIGN_SAS_TASK(cmd, task);
162 task->task_proto = task->dev->tproto; /* BUG_ON(!SSP) */
164 task->ssp_task.retry_count = 1;
165 int_to_scsilun(cmd->device->lun, &lun);
166 memcpy(task->ssp_task.LUN, &lun.scsi_lun, 8);
167 task->ssp_task.task_attr = TASK_ATTR_SIMPLE;
168 task->ssp_task.cmd = cmd;
170 task->scatter = scsi_sglist(cmd);
171 task->num_scatter = scsi_sg_count(cmd);
172 task->total_xfer_len = scsi_bufflen(cmd);
173 task->data_dir = cmd->sc_data_direction;
175 task->task_done = sas_scsi_task_done;
180 int sas_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
182 struct sas_internal *i = to_sas_internal(host->transportt);
183 struct domain_device *dev = cmd_to_domain_dev(cmd);
184 struct sas_task *task;
187 /* If the device fell off, no sense in issuing commands */
188 if (test_bit(SAS_DEV_GONE, &dev->state)) {
189 cmd->result = DID_BAD_TARGET << 16;
193 if (dev_is_sata(dev)) {
194 spin_lock_irq(dev->sata_dev.ap->lock);
195 res = ata_sas_queuecmd(cmd, dev->sata_dev.ap);
196 spin_unlock_irq(dev->sata_dev.ap->lock);
200 task = sas_create_task(cmd, dev, GFP_ATOMIC);
202 return SCSI_MLQUEUE_HOST_BUSY;
204 res = i->dft->lldd_execute_task(task, GFP_ATOMIC);
210 SAS_DPRINTK("lldd_execute_task returned: %d\n", res);
211 ASSIGN_SAS_TASK(cmd, NULL);
213 if (res == -SAS_QUEUE_FULL)
214 cmd->result = DID_SOFT_ERROR << 16; /* retry */
216 cmd->result = DID_ERROR << 16;
222 static void sas_eh_finish_cmd(struct scsi_cmnd *cmd)
224 struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(cmd->device->host);
225 struct domain_device *dev = cmd_to_domain_dev(cmd);
226 struct sas_task *task = TO_SAS_TASK(cmd);
228 /* At this point, we only get called following an actual abort
229 * of the task, so we should be guaranteed not to be racing with
230 * any completions from the LLD. Task is freed after this.
232 sas_end_task(cmd, task);
234 if (dev_is_sata(dev)) {
235 /* defer commands to libata so that libata EH can
236 * handle ata qcs correctly
238 list_move_tail(&cmd->eh_entry, &sas_ha->eh_ata_q);
242 /* now finish the command and move it on to the error
243 * handler done list, this also takes it off the
244 * error handler pending list.
246 scsi_eh_finish_cmd(cmd, &sas_ha->eh_done_q);
249 static void sas_scsi_clear_queue_lu(struct list_head *error_q, struct scsi_cmnd *my_cmd)
251 struct scsi_cmnd *cmd, *n;
253 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
254 if (cmd->device->sdev_target == my_cmd->device->sdev_target &&
255 cmd->device->lun == my_cmd->device->lun)
256 sas_eh_finish_cmd(cmd);
260 static void sas_scsi_clear_queue_I_T(struct list_head *error_q,
261 struct domain_device *dev)
263 struct scsi_cmnd *cmd, *n;
265 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
266 struct domain_device *x = cmd_to_domain_dev(cmd);
269 sas_eh_finish_cmd(cmd);
273 static void sas_scsi_clear_queue_port(struct list_head *error_q,
274 struct asd_sas_port *port)
276 struct scsi_cmnd *cmd, *n;
278 list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
279 struct domain_device *dev = cmd_to_domain_dev(cmd);
280 struct asd_sas_port *x = dev->port;
283 sas_eh_finish_cmd(cmd);
287 enum task_disposition {
295 static enum task_disposition sas_scsi_find_task(struct sas_task *task)
299 struct sas_internal *si =
300 to_sas_internal(task->dev->port->ha->core.shost->transportt);
302 for (i = 0; i < 5; i++) {
303 SAS_DPRINTK("%s: aborting task 0x%p\n", __func__, task);
304 res = si->dft->lldd_abort_task(task);
306 spin_lock_irqsave(&task->task_state_lock, flags);
307 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
308 spin_unlock_irqrestore(&task->task_state_lock, flags);
309 SAS_DPRINTK("%s: task 0x%p is done\n", __func__,
313 spin_unlock_irqrestore(&task->task_state_lock, flags);
315 if (res == TMF_RESP_FUNC_COMPLETE) {
316 SAS_DPRINTK("%s: task 0x%p is aborted\n",
318 return TASK_IS_ABORTED;
319 } else if (si->dft->lldd_query_task) {
320 SAS_DPRINTK("%s: querying task 0x%p\n",
322 res = si->dft->lldd_query_task(task);
324 case TMF_RESP_FUNC_SUCC:
325 SAS_DPRINTK("%s: task 0x%p at LU\n",
327 return TASK_IS_AT_LU;
328 case TMF_RESP_FUNC_COMPLETE:
329 SAS_DPRINTK("%s: task 0x%p not at LU\n",
331 return TASK_IS_NOT_AT_LU;
332 case TMF_RESP_FUNC_FAILED:
333 SAS_DPRINTK("%s: task 0x%p failed to abort\n",
335 return TASK_ABORT_FAILED;
343 static int sas_recover_lu(struct domain_device *dev, struct scsi_cmnd *cmd)
345 int res = TMF_RESP_FUNC_FAILED;
347 struct sas_internal *i =
348 to_sas_internal(dev->port->ha->core.shost->transportt);
350 int_to_scsilun(cmd->device->lun, &lun);
352 SAS_DPRINTK("eh: device %llx LUN %llx has the task\n",
353 SAS_ADDR(dev->sas_addr),
356 if (i->dft->lldd_abort_task_set)
357 res = i->dft->lldd_abort_task_set(dev, lun.scsi_lun);
359 if (res == TMF_RESP_FUNC_FAILED) {
360 if (i->dft->lldd_clear_task_set)
361 res = i->dft->lldd_clear_task_set(dev, lun.scsi_lun);
364 if (res == TMF_RESP_FUNC_FAILED) {
365 if (i->dft->lldd_lu_reset)
366 res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
372 static int sas_recover_I_T(struct domain_device *dev)
374 int res = TMF_RESP_FUNC_FAILED;
375 struct sas_internal *i =
376 to_sas_internal(dev->port->ha->core.shost->transportt);
378 SAS_DPRINTK("I_T nexus reset for dev %016llx\n",
379 SAS_ADDR(dev->sas_addr));
381 if (i->dft->lldd_I_T_nexus_reset)
382 res = i->dft->lldd_I_T_nexus_reset(dev);
387 /* take a reference on the last known good phy for this device */
388 struct sas_phy *sas_get_local_phy(struct domain_device *dev)
390 struct sas_ha_struct *ha = dev->port->ha;
394 /* a published domain device always has a valid phy, it may be
395 * stale, but it is never NULL
399 spin_lock_irqsave(&ha->phy_port_lock, flags);
401 get_device(&phy->dev);
402 spin_unlock_irqrestore(&ha->phy_port_lock, flags);
406 EXPORT_SYMBOL_GPL(sas_get_local_phy);
408 static void sas_wait_eh(struct domain_device *dev)
410 struct sas_ha_struct *ha = dev->port->ha;
413 if (dev_is_sata(dev)) {
414 ata_port_wait_eh(dev->sata_dev.ap);
418 spin_lock_irq(&ha->lock);
420 while (test_bit(SAS_DEV_EH_PENDING, &dev->state)) {
421 prepare_to_wait(&ha->eh_wait_q, &wait, TASK_UNINTERRUPTIBLE);
422 spin_unlock_irq(&ha->lock);
424 spin_lock_irq(&ha->lock);
426 finish_wait(&ha->eh_wait_q, &wait);
428 spin_unlock_irq(&ha->lock);
430 /* make sure SCSI EH is complete */
431 if (scsi_host_in_recovery(ha->core.shost)) {
436 EXPORT_SYMBOL(sas_wait_eh);
438 static int sas_queue_reset(struct domain_device *dev, int reset_type,
441 struct sas_ha_struct *ha = dev->port->ha;
442 int scheduled = 0, tries = 100;
444 /* ata: promote lun reset to bus reset */
445 if (dev_is_sata(dev)) {
446 sas_ata_schedule_reset(dev);
448 sas_ata_wait_eh(dev);
452 while (!scheduled && tries--) {
453 spin_lock_irq(&ha->lock);
454 if (!test_bit(SAS_DEV_EH_PENDING, &dev->state) &&
455 !test_bit(reset_type, &dev->state)) {
458 list_add_tail(&dev->ssp_dev.eh_list_node, &ha->eh_dev_q);
459 set_bit(SAS_DEV_EH_PENDING, &dev->state);
460 set_bit(reset_type, &dev->state);
461 int_to_scsilun(lun, &dev->ssp_dev.reset_lun);
462 scsi_schedule_eh(ha->core.shost);
464 spin_unlock_irq(&ha->lock);
473 SAS_DPRINTK("%s reset of %s failed\n",
474 reset_type == SAS_DEV_LU_RESET ? "LUN" : "Bus",
475 dev_name(&dev->rphy->dev));
480 int sas_eh_abort_handler(struct scsi_cmnd *cmd)
483 struct sas_task *task = TO_SAS_TASK(cmd);
484 struct Scsi_Host *host = cmd->device->host;
485 struct sas_internal *i = to_sas_internal(host->transportt);
487 if (current != host->ehandler)
490 if (!i->dft->lldd_abort_task)
493 res = i->dft->lldd_abort_task(task);
494 if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
499 EXPORT_SYMBOL_GPL(sas_eh_abort_handler);
501 /* Attempt to send a LUN reset message to a device */
502 int sas_eh_device_reset_handler(struct scsi_cmnd *cmd)
506 struct Scsi_Host *host = cmd->device->host;
507 struct domain_device *dev = cmd_to_domain_dev(cmd);
508 struct sas_internal *i = to_sas_internal(host->transportt);
510 if (current != host->ehandler)
511 return sas_queue_reset(dev, SAS_DEV_LU_RESET, cmd->device->lun, 0);
513 int_to_scsilun(cmd->device->lun, &lun);
515 if (!i->dft->lldd_lu_reset)
518 res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
519 if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
525 int sas_eh_bus_reset_handler(struct scsi_cmnd *cmd)
528 struct Scsi_Host *host = cmd->device->host;
529 struct domain_device *dev = cmd_to_domain_dev(cmd);
530 struct sas_internal *i = to_sas_internal(host->transportt);
532 if (current != host->ehandler)
533 return sas_queue_reset(dev, SAS_DEV_RESET, 0, 0);
535 if (!i->dft->lldd_I_T_nexus_reset)
538 res = i->dft->lldd_I_T_nexus_reset(dev);
539 if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE ||
546 /* Try to reset a device */
547 static int try_to_reset_cmd_device(struct scsi_cmnd *cmd)
550 struct Scsi_Host *shost = cmd->device->host;
552 if (!shost->hostt->eh_device_reset_handler)
555 res = shost->hostt->eh_device_reset_handler(cmd);
560 if (shost->hostt->eh_bus_reset_handler)
561 return shost->hostt->eh_bus_reset_handler(cmd);
566 static void sas_eh_handle_sas_errors(struct Scsi_Host *shost, struct list_head *work_q)
568 struct scsi_cmnd *cmd, *n;
569 enum task_disposition res = TASK_IS_DONE;
570 int tmf_resp, need_reset;
571 struct sas_internal *i = to_sas_internal(shost->transportt);
573 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
576 /* clean out any commands that won the completion vs eh race */
577 list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
578 struct domain_device *dev = cmd_to_domain_dev(cmd);
579 struct sas_task *task;
581 spin_lock_irqsave(&dev->done_lock, flags);
582 /* by this point the lldd has either observed
583 * SAS_HA_FROZEN and is leaving the task alone, or has
584 * won the race with eh and decided to complete it
586 task = TO_SAS_TASK(cmd);
587 spin_unlock_irqrestore(&dev->done_lock, flags);
590 list_move_tail(&cmd->eh_entry, &done);
594 list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
595 struct sas_task *task = TO_SAS_TASK(cmd);
597 list_del_init(&cmd->eh_entry);
599 spin_lock_irqsave(&task->task_state_lock, flags);
600 need_reset = task->task_state_flags & SAS_TASK_NEED_DEV_RESET;
601 spin_unlock_irqrestore(&task->task_state_lock, flags);
604 SAS_DPRINTK("%s: task 0x%p requests reset\n",
609 SAS_DPRINTK("trying to find task 0x%p\n", task);
610 res = sas_scsi_find_task(task);
616 SAS_DPRINTK("%s: task 0x%p is done\n", __func__,
618 sas_eh_finish_cmd(cmd);
620 case TASK_IS_ABORTED:
621 SAS_DPRINTK("%s: task 0x%p is aborted\n",
623 sas_eh_finish_cmd(cmd);
626 SAS_DPRINTK("task 0x%p is at LU: lu recover\n", task);
628 tmf_resp = sas_recover_lu(task->dev, cmd);
629 if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
630 SAS_DPRINTK("dev %016llx LU %llx is "
634 sas_eh_finish_cmd(cmd);
635 sas_scsi_clear_queue_lu(work_q, cmd);
639 case TASK_IS_NOT_AT_LU:
640 case TASK_ABORT_FAILED:
641 SAS_DPRINTK("task 0x%p is not at LU: I_T recover\n",
643 tmf_resp = sas_recover_I_T(task->dev);
644 if (tmf_resp == TMF_RESP_FUNC_COMPLETE ||
645 tmf_resp == -ENODEV) {
646 struct domain_device *dev = task->dev;
647 SAS_DPRINTK("I_T %016llx recovered\n",
648 SAS_ADDR(task->dev->sas_addr));
649 sas_eh_finish_cmd(cmd);
650 sas_scsi_clear_queue_I_T(work_q, dev);
653 /* Hammer time :-) */
654 try_to_reset_cmd_device(cmd);
655 if (i->dft->lldd_clear_nexus_port) {
656 struct asd_sas_port *port = task->dev->port;
657 SAS_DPRINTK("clearing nexus for port:%d\n",
659 res = i->dft->lldd_clear_nexus_port(port);
660 if (res == TMF_RESP_FUNC_COMPLETE) {
661 SAS_DPRINTK("clear nexus port:%d "
662 "succeeded\n", port->id);
663 sas_eh_finish_cmd(cmd);
664 sas_scsi_clear_queue_port(work_q,
669 if (i->dft->lldd_clear_nexus_ha) {
670 SAS_DPRINTK("clear nexus ha\n");
671 res = i->dft->lldd_clear_nexus_ha(ha);
672 if (res == TMF_RESP_FUNC_COMPLETE) {
673 SAS_DPRINTK("clear nexus ha "
675 sas_eh_finish_cmd(cmd);
679 /* If we are here -- this means that no amount
680 * of effort could recover from errors. Quite
681 * possibly the HA just disappeared.
683 SAS_DPRINTK("error from device %llx, LUN %llx "
684 "couldn't be recovered in any way\n",
685 SAS_ADDR(task->dev->sas_addr),
688 sas_eh_finish_cmd(cmd);
693 list_splice_tail(&done, work_q);
694 list_splice_tail_init(&ha->eh_ata_q, work_q);
698 SAS_DPRINTK("--- Exit %s -- clear_q\n", __func__);
699 list_for_each_entry_safe(cmd, n, work_q, eh_entry)
700 sas_eh_finish_cmd(cmd);
704 static void sas_eh_handle_resets(struct Scsi_Host *shost)
706 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
707 struct sas_internal *i = to_sas_internal(shost->transportt);
709 /* handle directed resets to sas devices */
710 spin_lock_irq(&ha->lock);
711 while (!list_empty(&ha->eh_dev_q)) {
712 struct domain_device *dev;
713 struct ssp_device *ssp;
715 ssp = list_entry(ha->eh_dev_q.next, typeof(*ssp), eh_list_node);
716 list_del_init(&ssp->eh_list_node);
717 dev = container_of(ssp, typeof(*dev), ssp_dev);
718 kref_get(&dev->kref);
719 WARN_ONCE(dev_is_sata(dev), "ssp reset to ata device?\n");
721 spin_unlock_irq(&ha->lock);
723 if (test_and_clear_bit(SAS_DEV_LU_RESET, &dev->state))
724 i->dft->lldd_lu_reset(dev, ssp->reset_lun.scsi_lun);
726 if (test_and_clear_bit(SAS_DEV_RESET, &dev->state))
727 i->dft->lldd_I_T_nexus_reset(dev);
730 spin_lock_irq(&ha->lock);
731 clear_bit(SAS_DEV_EH_PENDING, &dev->state);
734 spin_unlock_irq(&ha->lock);
738 void sas_scsi_recover_host(struct Scsi_Host *shost)
740 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
741 LIST_HEAD(eh_work_q);
748 spin_lock_irq(shost->host_lock);
749 list_splice_init(&shost->eh_cmd_q, &eh_work_q);
750 spin_unlock_irq(shost->host_lock);
752 SAS_DPRINTK("Enter %s busy: %d failed: %d\n",
753 __func__, atomic_read(&shost->host_busy), shost->host_failed);
755 * Deal with commands that still have SAS tasks (i.e. they didn't
756 * complete via the normal sas_task completion mechanism),
757 * SAS_HA_FROZEN gives eh dominion over all sas_task completion.
759 set_bit(SAS_HA_FROZEN, &ha->state);
760 sas_eh_handle_sas_errors(shost, &eh_work_q);
761 clear_bit(SAS_HA_FROZEN, &ha->state);
762 if (list_empty(&eh_work_q))
766 * Now deal with SCSI commands that completed ok but have a an error
767 * code (and hopefully sense data) attached. This is roughly what
768 * scsi_unjam_host does, but we skip scsi_eh_abort_cmds because any
769 * command we see here has no sas_task and is thus unknown to the HA.
771 sas_ata_eh(shost, &eh_work_q, &ha->eh_done_q);
772 if (!scsi_eh_get_sense(&eh_work_q, &ha->eh_done_q))
773 scsi_eh_ready_devs(shost, &eh_work_q, &ha->eh_done_q);
776 sas_eh_handle_resets(shost);
778 /* now link into libata eh --- if we have any ata devices */
779 sas_ata_strategy_handler(shost);
781 scsi_eh_flush_done_q(&ha->eh_done_q);
783 /* check if any new eh work was scheduled during the last run */
784 spin_lock_irq(&ha->lock);
785 if (ha->eh_active == 0) {
786 shost->host_eh_scheduled = 0;
789 spin_unlock_irq(&ha->lock);
794 SAS_DPRINTK("--- Exit %s: busy: %d failed: %d tries: %d\n",
795 __func__, atomic_read(&shost->host_busy),
796 shost->host_failed, tries);
799 enum blk_eh_timer_return sas_scsi_timed_out(struct scsi_cmnd *cmd)
801 scmd_dbg(cmd, "command %p timed out\n", cmd);
803 return BLK_EH_NOT_HANDLED;
806 int sas_ioctl(struct scsi_device *sdev, int cmd, void __user *arg)
808 struct domain_device *dev = sdev_to_domain_dev(sdev);
810 if (dev_is_sata(dev))
811 return ata_sas_scsi_ioctl(dev->sata_dev.ap, sdev, cmd, arg);
816 struct domain_device *sas_find_dev_by_rphy(struct sas_rphy *rphy)
818 struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent);
819 struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
820 struct domain_device *found_dev = NULL;
824 spin_lock_irqsave(&ha->phy_port_lock, flags);
825 for (i = 0; i < ha->num_phys; i++) {
826 struct asd_sas_port *port = ha->sas_port[i];
827 struct domain_device *dev;
829 spin_lock(&port->dev_list_lock);
830 list_for_each_entry(dev, &port->dev_list, dev_list_node) {
831 if (rphy == dev->rphy) {
833 spin_unlock(&port->dev_list_lock);
837 spin_unlock(&port->dev_list_lock);
840 spin_unlock_irqrestore(&ha->phy_port_lock, flags);
845 int sas_target_alloc(struct scsi_target *starget)
847 struct sas_rphy *rphy = dev_to_rphy(starget->dev.parent);
848 struct domain_device *found_dev = sas_find_dev_by_rphy(rphy);
853 kref_get(&found_dev->kref);
854 starget->hostdata = found_dev;
858 #define SAS_DEF_QD 256
860 int sas_slave_configure(struct scsi_device *scsi_dev)
862 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
863 struct sas_ha_struct *sas_ha;
865 BUG_ON(dev->rphy->identify.device_type != SAS_END_DEVICE);
867 if (dev_is_sata(dev)) {
868 ata_sas_slave_configure(scsi_dev, dev->sata_dev.ap);
872 sas_ha = dev->port->ha;
874 sas_read_port_mode_page(scsi_dev);
876 if (scsi_dev->tagged_supported) {
877 scsi_change_queue_depth(scsi_dev, SAS_DEF_QD);
879 SAS_DPRINTK("device %llx, LUN %llx doesn't support "
880 "TCQ\n", SAS_ADDR(dev->sas_addr),
882 scsi_change_queue_depth(scsi_dev, 1);
885 scsi_dev->allow_restart = 1;
890 int sas_change_queue_depth(struct scsi_device *sdev, int depth)
892 struct domain_device *dev = sdev_to_domain_dev(sdev);
894 if (dev_is_sata(dev))
895 return __ata_change_queue_depth(dev->sata_dev.ap, sdev, depth);
897 if (!sdev->tagged_supported)
899 return scsi_change_queue_depth(sdev, depth);
902 int sas_bios_param(struct scsi_device *scsi_dev,
903 struct block_device *bdev,
904 sector_t capacity, int *hsc)
908 sector_div(capacity, 255*63);
915 * Tell an upper layer that it needs to initiate an abort for a given task.
916 * This should only ever be called by an LLDD.
918 void sas_task_abort(struct sas_task *task)
920 struct scsi_cmnd *sc = task->uldd_task;
922 /* Escape for libsas internal commands */
924 struct sas_task_slow *slow = task->slow_task;
928 if (!del_timer(&slow->timer))
930 slow->timer.function(slow->timer.data);
934 if (dev_is_sata(task->dev)) {
935 sas_ata_task_abort(task);
937 struct request_queue *q = sc->device->request_queue;
940 spin_lock_irqsave(q->queue_lock, flags);
941 blk_abort_request(sc->request);
942 spin_unlock_irqrestore(q->queue_lock, flags);
946 void sas_target_destroy(struct scsi_target *starget)
948 struct domain_device *found_dev = starget->hostdata;
953 starget->hostdata = NULL;
954 sas_put_device(found_dev);
957 static void sas_parse_addr(u8 *sas_addr, const char *p)
960 for (i = 0; i < SAS_ADDR_SIZE; i++) {
964 h = isdigit(*p) ? *p-'0' : toupper(*p)-'A'+10;
966 l = isdigit(*p) ? *p-'0' : toupper(*p)-'A'+10;
968 sas_addr[i] = (h<<4) | l;
972 #define SAS_STRING_ADDR_SIZE 16
974 int sas_request_addr(struct Scsi_Host *shost, u8 *addr)
977 const struct firmware *fw;
979 res = request_firmware(&fw, "sas_addr", &shost->shost_gendev);
983 if (fw->size < SAS_STRING_ADDR_SIZE) {
988 sas_parse_addr(addr, fw->data);
991 release_firmware(fw);
994 EXPORT_SYMBOL_GPL(sas_request_addr);
996 EXPORT_SYMBOL_GPL(sas_queuecommand);
997 EXPORT_SYMBOL_GPL(sas_target_alloc);
998 EXPORT_SYMBOL_GPL(sas_slave_configure);
999 EXPORT_SYMBOL_GPL(sas_change_queue_depth);
1000 EXPORT_SYMBOL_GPL(sas_bios_param);
1001 EXPORT_SYMBOL_GPL(sas_task_abort);
1002 EXPORT_SYMBOL_GPL(sas_phy_reset);
1003 EXPORT_SYMBOL_GPL(sas_eh_device_reset_handler);
1004 EXPORT_SYMBOL_GPL(sas_eh_bus_reset_handler);
1005 EXPORT_SYMBOL_GPL(sas_target_destroy);
1006 EXPORT_SYMBOL_GPL(sas_ioctl);