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>
30 #include <linux/kernel.h>
32 #include "sas_internal.h"
34 #include <scsi/scsi_host.h>
35 #include <scsi/scsi_device.h>
36 #include <scsi/scsi_tcq.h>
37 #include <scsi/scsi.h>
38 #include <scsi/scsi_eh.h>
39 #include <scsi/scsi_transport.h>
40 #include <scsi/scsi_transport_sas.h>
41 #include <scsi/sas_ata.h>
42 #include "../scsi_sas_internal.h"
43 #include "../scsi_transport_api.h"
44 #include "../scsi_priv.h"
46 #include <linux/err.h>
47 #include <linux/blkdev.h>
48 #include <linux/freezer.h>
49 #include <linux/gfp.h>
50 #include <linux/scatterlist.h>
51 #include <linux/libata.h>
53 /* record final status and free the task */
54 static void sas_end_task(struct scsi_cmnd
*sc
, struct sas_task
*task
)
56 struct task_status_struct
*ts
= &task
->task_status
;
59 if (ts
->resp
== SAS_TASK_UNDELIVERED
) {
62 } else { /* ts->resp == SAS_TASK_COMPLETE */
63 /* task delivered, what happened afterwards? */
65 case SAS_DEV_NO_RESPONSE
:
72 case SAS_DATA_UNDERRUN
:
73 scsi_set_resid(sc
, ts
->residual
);
74 if (scsi_bufflen(sc
) - scsi_get_resid(sc
) < sc
->underflow
)
77 case SAS_DATA_OVERRUN
:
81 hs
= DID_SOFT_ERROR
; /* retry */
83 case SAS_DEVICE_UNKNOWN
:
90 if (ts
->open_rej_reason
== SAS_OREJ_RSVD_RETRY
)
91 hs
= DID_SOFT_ERROR
; /* retry */
95 case SAS_PROTO_RESPONSE
:
96 SAS_DPRINTK("LLDD:%s sent SAS_PROTO_RESP for an SSP "
97 "task; please report this\n",
98 task
->dev
->port
->ha
->sas_ha_name
);
100 case SAS_ABORTED_TASK
:
103 case SAM_STAT_CHECK_CONDITION
:
104 memcpy(sc
->sense_buffer
, ts
->buf
,
105 min(SCSI_SENSE_BUFFERSIZE
, ts
->buf_valid_size
));
106 stat
= SAM_STAT_CHECK_CONDITION
;
114 sc
->result
= (hs
<< 16) | stat
;
115 ASSIGN_SAS_TASK(sc
, NULL
);
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");
145 sas_end_task(sc
, task
);
149 static struct sas_task
*sas_create_task(struct scsi_cmnd
*cmd
,
150 struct domain_device
*dev
,
153 struct sas_task
*task
= sas_alloc_task(gfp_flags
);
159 task
->uldd_task
= cmd
;
160 ASSIGN_SAS_TASK(cmd
, task
);
163 task
->task_proto
= task
->dev
->tproto
; /* BUG_ON(!SSP) */
165 task
->ssp_task
.retry_count
= 1;
166 int_to_scsilun(cmd
->device
->lun
, &lun
);
167 memcpy(task
->ssp_task
.LUN
, &lun
.scsi_lun
, 8);
168 task
->ssp_task
.task_attr
= TASK_ATTR_SIMPLE
;
169 task
->ssp_task
.cmd
= cmd
;
171 task
->scatter
= scsi_sglist(cmd
);
172 task
->num_scatter
= scsi_sg_count(cmd
);
173 task
->total_xfer_len
= scsi_bufflen(cmd
);
174 task
->data_dir
= cmd
->sc_data_direction
;
176 task
->task_done
= sas_scsi_task_done
;
181 int sas_queuecommand(struct Scsi_Host
*host
, struct scsi_cmnd
*cmd
)
183 struct sas_internal
*i
= to_sas_internal(host
->transportt
);
184 struct domain_device
*dev
= cmd_to_domain_dev(cmd
);
185 struct sas_task
*task
;
188 /* If the device fell off, no sense in issuing commands */
189 if (test_bit(SAS_DEV_GONE
, &dev
->state
)) {
190 cmd
->result
= DID_BAD_TARGET
<< 16;
194 if (dev_is_sata(dev
)) {
195 spin_lock_irq(dev
->sata_dev
.ap
->lock
);
196 res
= ata_sas_queuecmd(cmd
, dev
->sata_dev
.ap
);
197 spin_unlock_irq(dev
->sata_dev
.ap
->lock
);
201 task
= sas_create_task(cmd
, dev
, GFP_ATOMIC
);
203 return SCSI_MLQUEUE_HOST_BUSY
;
205 res
= i
->dft
->lldd_execute_task(task
, GFP_ATOMIC
);
211 SAS_DPRINTK("lldd_execute_task returned: %d\n", res
);
212 ASSIGN_SAS_TASK(cmd
, NULL
);
214 if (res
== -SAS_QUEUE_FULL
)
215 cmd
->result
= DID_SOFT_ERROR
<< 16; /* retry */
217 cmd
->result
= DID_ERROR
<< 16;
223 static void sas_eh_finish_cmd(struct scsi_cmnd
*cmd
)
225 struct sas_ha_struct
*sas_ha
= SHOST_TO_SAS_HA(cmd
->device
->host
);
226 struct domain_device
*dev
= cmd_to_domain_dev(cmd
);
227 struct sas_task
*task
= TO_SAS_TASK(cmd
);
229 /* At this point, we only get called following an actual abort
230 * of the task, so we should be guaranteed not to be racing with
231 * any completions from the LLD. Task is freed after this.
233 sas_end_task(cmd
, task
);
235 if (dev_is_sata(dev
)) {
236 /* defer commands to libata so that libata EH can
237 * handle ata qcs correctly
239 list_move_tail(&cmd
->eh_entry
, &sas_ha
->eh_ata_q
);
243 /* now finish the command and move it on to the error
244 * handler done list, this also takes it off the
245 * error handler pending list.
247 scsi_eh_finish_cmd(cmd
, &sas_ha
->eh_done_q
);
250 static void sas_scsi_clear_queue_lu(struct list_head
*error_q
, struct scsi_cmnd
*my_cmd
)
252 struct scsi_cmnd
*cmd
, *n
;
254 list_for_each_entry_safe(cmd
, n
, error_q
, eh_entry
) {
255 if (cmd
->device
->sdev_target
== my_cmd
->device
->sdev_target
&&
256 cmd
->device
->lun
== my_cmd
->device
->lun
)
257 sas_eh_finish_cmd(cmd
);
261 static void sas_scsi_clear_queue_I_T(struct list_head
*error_q
,
262 struct domain_device
*dev
)
264 struct scsi_cmnd
*cmd
, *n
;
266 list_for_each_entry_safe(cmd
, n
, error_q
, eh_entry
) {
267 struct domain_device
*x
= cmd_to_domain_dev(cmd
);
270 sas_eh_finish_cmd(cmd
);
274 static void sas_scsi_clear_queue_port(struct list_head
*error_q
,
275 struct asd_sas_port
*port
)
277 struct scsi_cmnd
*cmd
, *n
;
279 list_for_each_entry_safe(cmd
, n
, error_q
, eh_entry
) {
280 struct domain_device
*dev
= cmd_to_domain_dev(cmd
);
281 struct asd_sas_port
*x
= dev
->port
;
284 sas_eh_finish_cmd(cmd
);
288 enum task_disposition
{
296 static enum task_disposition
sas_scsi_find_task(struct sas_task
*task
)
300 struct sas_internal
*si
=
301 to_sas_internal(task
->dev
->port
->ha
->core
.shost
->transportt
);
303 for (i
= 0; i
< 5; i
++) {
304 SAS_DPRINTK("%s: aborting task 0x%p\n", __func__
, task
);
305 res
= si
->dft
->lldd_abort_task(task
);
307 spin_lock_irqsave(&task
->task_state_lock
, flags
);
308 if (task
->task_state_flags
& SAS_TASK_STATE_DONE
) {
309 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
310 SAS_DPRINTK("%s: task 0x%p is done\n", __func__
,
314 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
316 if (res
== TMF_RESP_FUNC_COMPLETE
) {
317 SAS_DPRINTK("%s: task 0x%p is aborted\n",
319 return TASK_IS_ABORTED
;
320 } else if (si
->dft
->lldd_query_task
) {
321 SAS_DPRINTK("%s: querying task 0x%p\n",
323 res
= si
->dft
->lldd_query_task(task
);
325 case TMF_RESP_FUNC_SUCC
:
326 SAS_DPRINTK("%s: task 0x%p at LU\n",
328 return TASK_IS_AT_LU
;
329 case TMF_RESP_FUNC_COMPLETE
:
330 SAS_DPRINTK("%s: task 0x%p not at LU\n",
332 return TASK_IS_NOT_AT_LU
;
333 case TMF_RESP_FUNC_FAILED
:
334 SAS_DPRINTK("%s: task 0x%p failed to abort\n",
336 return TASK_ABORT_FAILED
;
344 static int sas_recover_lu(struct domain_device
*dev
, struct scsi_cmnd
*cmd
)
346 int res
= TMF_RESP_FUNC_FAILED
;
348 struct sas_internal
*i
=
349 to_sas_internal(dev
->port
->ha
->core
.shost
->transportt
);
351 int_to_scsilun(cmd
->device
->lun
, &lun
);
353 SAS_DPRINTK("eh: device %llx LUN %llx has the task\n",
354 SAS_ADDR(dev
->sas_addr
),
357 if (i
->dft
->lldd_abort_task_set
)
358 res
= i
->dft
->lldd_abort_task_set(dev
, lun
.scsi_lun
);
360 if (res
== TMF_RESP_FUNC_FAILED
) {
361 if (i
->dft
->lldd_clear_task_set
)
362 res
= i
->dft
->lldd_clear_task_set(dev
, lun
.scsi_lun
);
365 if (res
== TMF_RESP_FUNC_FAILED
) {
366 if (i
->dft
->lldd_lu_reset
)
367 res
= i
->dft
->lldd_lu_reset(dev
, lun
.scsi_lun
);
373 static int sas_recover_I_T(struct domain_device
*dev
)
375 int res
= TMF_RESP_FUNC_FAILED
;
376 struct sas_internal
*i
=
377 to_sas_internal(dev
->port
->ha
->core
.shost
->transportt
);
379 SAS_DPRINTK("I_T nexus reset for dev %016llx\n",
380 SAS_ADDR(dev
->sas_addr
));
382 if (i
->dft
->lldd_I_T_nexus_reset
)
383 res
= i
->dft
->lldd_I_T_nexus_reset(dev
);
388 /* take a reference on the last known good phy for this device */
389 struct sas_phy
*sas_get_local_phy(struct domain_device
*dev
)
391 struct sas_ha_struct
*ha
= dev
->port
->ha
;
395 /* a published domain device always has a valid phy, it may be
396 * stale, but it is never NULL
400 spin_lock_irqsave(&ha
->phy_port_lock
, flags
);
402 get_device(&phy
->dev
);
403 spin_unlock_irqrestore(&ha
->phy_port_lock
, flags
);
407 EXPORT_SYMBOL_GPL(sas_get_local_phy
);
409 static void sas_wait_eh(struct domain_device
*dev
)
411 struct sas_ha_struct
*ha
= dev
->port
->ha
;
414 if (dev_is_sata(dev
)) {
415 ata_port_wait_eh(dev
->sata_dev
.ap
);
419 spin_lock_irq(&ha
->lock
);
421 while (test_bit(SAS_DEV_EH_PENDING
, &dev
->state
)) {
422 prepare_to_wait(&ha
->eh_wait_q
, &wait
, TASK_UNINTERRUPTIBLE
);
423 spin_unlock_irq(&ha
->lock
);
425 spin_lock_irq(&ha
->lock
);
427 finish_wait(&ha
->eh_wait_q
, &wait
);
429 spin_unlock_irq(&ha
->lock
);
431 /* make sure SCSI EH is complete */
432 if (scsi_host_in_recovery(ha
->core
.shost
)) {
437 EXPORT_SYMBOL(sas_wait_eh
);
439 static int sas_queue_reset(struct domain_device
*dev
, int reset_type
,
442 struct sas_ha_struct
*ha
= dev
->port
->ha
;
443 int scheduled
= 0, tries
= 100;
445 /* ata: promote lun reset to bus reset */
446 if (dev_is_sata(dev
)) {
447 sas_ata_schedule_reset(dev
);
449 sas_ata_wait_eh(dev
);
453 while (!scheduled
&& tries
--) {
454 spin_lock_irq(&ha
->lock
);
455 if (!test_bit(SAS_DEV_EH_PENDING
, &dev
->state
) &&
456 !test_bit(reset_type
, &dev
->state
)) {
459 list_add_tail(&dev
->ssp_dev
.eh_list_node
, &ha
->eh_dev_q
);
460 set_bit(SAS_DEV_EH_PENDING
, &dev
->state
);
461 set_bit(reset_type
, &dev
->state
);
462 int_to_scsilun(lun
, &dev
->ssp_dev
.reset_lun
);
463 scsi_schedule_eh(ha
->core
.shost
);
465 spin_unlock_irq(&ha
->lock
);
474 SAS_DPRINTK("%s reset of %s failed\n",
475 reset_type
== SAS_DEV_LU_RESET
? "LUN" : "Bus",
476 dev_name(&dev
->rphy
->dev
));
481 int sas_eh_abort_handler(struct scsi_cmnd
*cmd
)
483 int res
= TMF_RESP_FUNC_FAILED
;
484 struct sas_task
*task
= TO_SAS_TASK(cmd
);
485 struct Scsi_Host
*host
= cmd
->device
->host
;
486 struct domain_device
*dev
= cmd_to_domain_dev(cmd
);
487 struct sas_internal
*i
= to_sas_internal(host
->transportt
);
490 if (!i
->dft
->lldd_abort_task
)
493 spin_lock_irqsave(host
->host_lock
, flags
);
494 /* We cannot do async aborts for SATA devices */
495 if (dev_is_sata(dev
) && !host
->host_eh_scheduled
) {
496 spin_unlock_irqrestore(host
->host_lock
, flags
);
499 spin_unlock_irqrestore(host
->host_lock
, flags
);
502 res
= i
->dft
->lldd_abort_task(task
);
504 SAS_DPRINTK("no task to abort\n");
505 if (res
== TMF_RESP_FUNC_SUCC
|| res
== TMF_RESP_FUNC_COMPLETE
)
510 EXPORT_SYMBOL_GPL(sas_eh_abort_handler
);
512 /* Attempt to send a LUN reset message to a device */
513 int sas_eh_device_reset_handler(struct scsi_cmnd
*cmd
)
517 struct Scsi_Host
*host
= cmd
->device
->host
;
518 struct domain_device
*dev
= cmd_to_domain_dev(cmd
);
519 struct sas_internal
*i
= to_sas_internal(host
->transportt
);
521 if (current
!= host
->ehandler
)
522 return sas_queue_reset(dev
, SAS_DEV_LU_RESET
, cmd
->device
->lun
, 0);
524 int_to_scsilun(cmd
->device
->lun
, &lun
);
526 if (!i
->dft
->lldd_lu_reset
)
529 res
= i
->dft
->lldd_lu_reset(dev
, lun
.scsi_lun
);
530 if (res
== TMF_RESP_FUNC_SUCC
|| res
== TMF_RESP_FUNC_COMPLETE
)
536 int sas_eh_target_reset_handler(struct scsi_cmnd
*cmd
)
539 struct Scsi_Host
*host
= cmd
->device
->host
;
540 struct domain_device
*dev
= cmd_to_domain_dev(cmd
);
541 struct sas_internal
*i
= to_sas_internal(host
->transportt
);
543 if (current
!= host
->ehandler
)
544 return sas_queue_reset(dev
, SAS_DEV_RESET
, 0, 0);
546 if (!i
->dft
->lldd_I_T_nexus_reset
)
549 res
= i
->dft
->lldd_I_T_nexus_reset(dev
);
550 if (res
== TMF_RESP_FUNC_SUCC
|| res
== TMF_RESP_FUNC_COMPLETE
||
557 /* Try to reset a device */
558 static int try_to_reset_cmd_device(struct scsi_cmnd
*cmd
)
561 struct Scsi_Host
*shost
= cmd
->device
->host
;
563 if (!shost
->hostt
->eh_device_reset_handler
)
564 goto try_target_reset
;
566 res
= shost
->hostt
->eh_device_reset_handler(cmd
);
571 if (shost
->hostt
->eh_target_reset_handler
)
572 return shost
->hostt
->eh_target_reset_handler(cmd
);
577 static void sas_eh_handle_sas_errors(struct Scsi_Host
*shost
, struct list_head
*work_q
)
579 struct scsi_cmnd
*cmd
, *n
;
580 enum task_disposition res
= TASK_IS_DONE
;
581 int tmf_resp
, need_reset
;
582 struct sas_internal
*i
= to_sas_internal(shost
->transportt
);
584 struct sas_ha_struct
*ha
= SHOST_TO_SAS_HA(shost
);
587 /* clean out any commands that won the completion vs eh race */
588 list_for_each_entry_safe(cmd
, n
, work_q
, eh_entry
) {
589 struct domain_device
*dev
= cmd_to_domain_dev(cmd
);
590 struct sas_task
*task
;
592 spin_lock_irqsave(&dev
->done_lock
, flags
);
593 /* by this point the lldd has either observed
594 * SAS_HA_FROZEN and is leaving the task alone, or has
595 * won the race with eh and decided to complete it
597 task
= TO_SAS_TASK(cmd
);
598 spin_unlock_irqrestore(&dev
->done_lock
, flags
);
601 list_move_tail(&cmd
->eh_entry
, &done
);
605 list_for_each_entry_safe(cmd
, n
, work_q
, eh_entry
) {
606 struct sas_task
*task
= TO_SAS_TASK(cmd
);
608 list_del_init(&cmd
->eh_entry
);
610 spin_lock_irqsave(&task
->task_state_lock
, flags
);
611 need_reset
= task
->task_state_flags
& SAS_TASK_NEED_DEV_RESET
;
612 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
615 SAS_DPRINTK("%s: task 0x%p requests reset\n",
620 SAS_DPRINTK("trying to find task 0x%p\n", task
);
621 res
= sas_scsi_find_task(task
);
625 SAS_DPRINTK("%s: task 0x%p is done\n", __func__
,
627 sas_eh_finish_cmd(cmd
);
629 case TASK_IS_ABORTED
:
630 SAS_DPRINTK("%s: task 0x%p is aborted\n",
632 sas_eh_finish_cmd(cmd
);
635 SAS_DPRINTK("task 0x%p is at LU: lu recover\n", task
);
637 tmf_resp
= sas_recover_lu(task
->dev
, cmd
);
638 if (tmf_resp
== TMF_RESP_FUNC_COMPLETE
) {
639 SAS_DPRINTK("dev %016llx LU %llx is "
643 sas_eh_finish_cmd(cmd
);
644 sas_scsi_clear_queue_lu(work_q
, cmd
);
648 case TASK_IS_NOT_AT_LU
:
649 case TASK_ABORT_FAILED
:
650 SAS_DPRINTK("task 0x%p is not at LU: I_T recover\n",
652 tmf_resp
= sas_recover_I_T(task
->dev
);
653 if (tmf_resp
== TMF_RESP_FUNC_COMPLETE
||
654 tmf_resp
== -ENODEV
) {
655 struct domain_device
*dev
= task
->dev
;
656 SAS_DPRINTK("I_T %016llx recovered\n",
657 SAS_ADDR(task
->dev
->sas_addr
));
658 sas_eh_finish_cmd(cmd
);
659 sas_scsi_clear_queue_I_T(work_q
, dev
);
662 /* Hammer time :-) */
663 try_to_reset_cmd_device(cmd
);
664 if (i
->dft
->lldd_clear_nexus_port
) {
665 struct asd_sas_port
*port
= task
->dev
->port
;
666 SAS_DPRINTK("clearing nexus for port:%d\n",
668 res
= i
->dft
->lldd_clear_nexus_port(port
);
669 if (res
== TMF_RESP_FUNC_COMPLETE
) {
670 SAS_DPRINTK("clear nexus port:%d "
671 "succeeded\n", port
->id
);
672 sas_eh_finish_cmd(cmd
);
673 sas_scsi_clear_queue_port(work_q
,
678 if (i
->dft
->lldd_clear_nexus_ha
) {
679 SAS_DPRINTK("clear nexus ha\n");
680 res
= i
->dft
->lldd_clear_nexus_ha(ha
);
681 if (res
== TMF_RESP_FUNC_COMPLETE
) {
682 SAS_DPRINTK("clear nexus ha "
684 sas_eh_finish_cmd(cmd
);
688 /* If we are here -- this means that no amount
689 * of effort could recover from errors. Quite
690 * possibly the HA just disappeared.
692 SAS_DPRINTK("error from device %llx, LUN %llx "
693 "couldn't be recovered in any way\n",
694 SAS_ADDR(task
->dev
->sas_addr
),
697 sas_eh_finish_cmd(cmd
);
702 list_splice_tail(&done
, work_q
);
703 list_splice_tail_init(&ha
->eh_ata_q
, work_q
);
707 SAS_DPRINTK("--- Exit %s -- clear_q\n", __func__
);
708 list_for_each_entry_safe(cmd
, n
, work_q
, eh_entry
)
709 sas_eh_finish_cmd(cmd
);
713 static void sas_eh_handle_resets(struct Scsi_Host
*shost
)
715 struct sas_ha_struct
*ha
= SHOST_TO_SAS_HA(shost
);
716 struct sas_internal
*i
= to_sas_internal(shost
->transportt
);
718 /* handle directed resets to sas devices */
719 spin_lock_irq(&ha
->lock
);
720 while (!list_empty(&ha
->eh_dev_q
)) {
721 struct domain_device
*dev
;
722 struct ssp_device
*ssp
;
724 ssp
= list_entry(ha
->eh_dev_q
.next
, typeof(*ssp
), eh_list_node
);
725 list_del_init(&ssp
->eh_list_node
);
726 dev
= container_of(ssp
, typeof(*dev
), ssp_dev
);
727 kref_get(&dev
->kref
);
728 WARN_ONCE(dev_is_sata(dev
), "ssp reset to ata device?\n");
730 spin_unlock_irq(&ha
->lock
);
732 if (test_and_clear_bit(SAS_DEV_LU_RESET
, &dev
->state
))
733 i
->dft
->lldd_lu_reset(dev
, ssp
->reset_lun
.scsi_lun
);
735 if (test_and_clear_bit(SAS_DEV_RESET
, &dev
->state
))
736 i
->dft
->lldd_I_T_nexus_reset(dev
);
739 spin_lock_irq(&ha
->lock
);
740 clear_bit(SAS_DEV_EH_PENDING
, &dev
->state
);
743 spin_unlock_irq(&ha
->lock
);
747 void sas_scsi_recover_host(struct Scsi_Host
*shost
)
749 struct sas_ha_struct
*ha
= SHOST_TO_SAS_HA(shost
);
750 LIST_HEAD(eh_work_q
);
757 spin_lock_irq(shost
->host_lock
);
758 list_splice_init(&shost
->eh_cmd_q
, &eh_work_q
);
759 spin_unlock_irq(shost
->host_lock
);
761 SAS_DPRINTK("Enter %s busy: %d failed: %d\n",
762 __func__
, atomic_read(&shost
->host_busy
), shost
->host_failed
);
764 * Deal with commands that still have SAS tasks (i.e. they didn't
765 * complete via the normal sas_task completion mechanism),
766 * SAS_HA_FROZEN gives eh dominion over all sas_task completion.
768 set_bit(SAS_HA_FROZEN
, &ha
->state
);
769 sas_eh_handle_sas_errors(shost
, &eh_work_q
);
770 clear_bit(SAS_HA_FROZEN
, &ha
->state
);
771 if (list_empty(&eh_work_q
))
775 * Now deal with SCSI commands that completed ok but have a an error
776 * code (and hopefully sense data) attached. This is roughly what
777 * scsi_unjam_host does, but we skip scsi_eh_abort_cmds because any
778 * command we see here has no sas_task and is thus unknown to the HA.
780 sas_ata_eh(shost
, &eh_work_q
, &ha
->eh_done_q
);
781 if (!scsi_eh_get_sense(&eh_work_q
, &ha
->eh_done_q
))
782 scsi_eh_ready_devs(shost
, &eh_work_q
, &ha
->eh_done_q
);
785 sas_eh_handle_resets(shost
);
787 /* now link into libata eh --- if we have any ata devices */
788 sas_ata_strategy_handler(shost
);
790 scsi_eh_flush_done_q(&ha
->eh_done_q
);
792 /* check if any new eh work was scheduled during the last run */
793 spin_lock_irq(&ha
->lock
);
794 if (ha
->eh_active
== 0) {
795 shost
->host_eh_scheduled
= 0;
798 spin_unlock_irq(&ha
->lock
);
803 SAS_DPRINTK("--- Exit %s: busy: %d failed: %d tries: %d\n",
804 __func__
, atomic_read(&shost
->host_busy
),
805 shost
->host_failed
, tries
);
808 int sas_ioctl(struct scsi_device
*sdev
, int cmd
, void __user
*arg
)
810 struct domain_device
*dev
= sdev_to_domain_dev(sdev
);
812 if (dev_is_sata(dev
))
813 return ata_sas_scsi_ioctl(dev
->sata_dev
.ap
, sdev
, cmd
, arg
);
818 struct domain_device
*sas_find_dev_by_rphy(struct sas_rphy
*rphy
)
820 struct Scsi_Host
*shost
= dev_to_shost(rphy
->dev
.parent
);
821 struct sas_ha_struct
*ha
= SHOST_TO_SAS_HA(shost
);
822 struct domain_device
*found_dev
= NULL
;
826 spin_lock_irqsave(&ha
->phy_port_lock
, flags
);
827 for (i
= 0; i
< ha
->num_phys
; i
++) {
828 struct asd_sas_port
*port
= ha
->sas_port
[i
];
829 struct domain_device
*dev
;
831 spin_lock(&port
->dev_list_lock
);
832 list_for_each_entry(dev
, &port
->dev_list
, dev_list_node
) {
833 if (rphy
== dev
->rphy
) {
835 spin_unlock(&port
->dev_list_lock
);
839 spin_unlock(&port
->dev_list_lock
);
842 spin_unlock_irqrestore(&ha
->phy_port_lock
, flags
);
847 int sas_target_alloc(struct scsi_target
*starget
)
849 struct sas_rphy
*rphy
= dev_to_rphy(starget
->dev
.parent
);
850 struct domain_device
*found_dev
= sas_find_dev_by_rphy(rphy
);
855 kref_get(&found_dev
->kref
);
856 starget
->hostdata
= found_dev
;
860 #define SAS_DEF_QD 256
862 int sas_slave_configure(struct scsi_device
*scsi_dev
)
864 struct domain_device
*dev
= sdev_to_domain_dev(scsi_dev
);
866 BUG_ON(dev
->rphy
->identify
.device_type
!= SAS_END_DEVICE
);
868 if (dev_is_sata(dev
)) {
869 ata_sas_slave_configure(scsi_dev
, dev
->sata_dev
.ap
);
873 sas_read_port_mode_page(scsi_dev
);
875 if (scsi_dev
->tagged_supported
) {
876 scsi_change_queue_depth(scsi_dev
, SAS_DEF_QD
);
878 SAS_DPRINTK("device %llx, LUN %llx doesn't support "
879 "TCQ\n", SAS_ADDR(dev
->sas_addr
),
881 scsi_change_queue_depth(scsi_dev
, 1);
884 scsi_dev
->allow_restart
= 1;
889 int sas_change_queue_depth(struct scsi_device
*sdev
, int depth
)
891 struct domain_device
*dev
= sdev_to_domain_dev(sdev
);
893 if (dev_is_sata(dev
))
894 return __ata_change_queue_depth(dev
->sata_dev
.ap
, sdev
, depth
);
896 if (!sdev
->tagged_supported
)
898 return scsi_change_queue_depth(sdev
, depth
);
901 int sas_bios_param(struct scsi_device
*scsi_dev
,
902 struct block_device
*bdev
,
903 sector_t capacity
, int *hsc
)
907 sector_div(capacity
, 255*63);
914 * Tell an upper layer that it needs to initiate an abort for a given task.
915 * This should only ever be called by an LLDD.
917 void sas_task_abort(struct sas_task
*task
)
919 struct scsi_cmnd
*sc
= task
->uldd_task
;
921 /* Escape for libsas internal commands */
923 struct sas_task_slow
*slow
= task
->slow_task
;
927 if (!del_timer(&slow
->timer
))
929 slow
->timer
.function(&slow
->timer
);
933 if (dev_is_sata(task
->dev
)) {
934 sas_ata_task_abort(task
);
936 struct request_queue
*q
= sc
->device
->request_queue
;
939 spin_lock_irqsave(q
->queue_lock
, flags
);
940 blk_abort_request(sc
->request
);
941 spin_unlock_irqrestore(q
->queue_lock
, flags
);
945 void sas_target_destroy(struct scsi_target
*starget
)
947 struct domain_device
*found_dev
= starget
->hostdata
;
952 starget
->hostdata
= NULL
;
953 sas_put_device(found_dev
);
956 #define SAS_STRING_ADDR_SIZE 16
958 int sas_request_addr(struct Scsi_Host
*shost
, u8
*addr
)
961 const struct firmware
*fw
;
963 res
= request_firmware(&fw
, "sas_addr", &shost
->shost_gendev
);
967 if (fw
->size
< SAS_STRING_ADDR_SIZE
) {
972 res
= hex2bin(addr
, fw
->data
, strnlen(fw
->data
, SAS_ADDR_SIZE
* 2) / 2);
977 release_firmware(fw
);
980 EXPORT_SYMBOL_GPL(sas_request_addr
);
982 EXPORT_SYMBOL_GPL(sas_queuecommand
);
983 EXPORT_SYMBOL_GPL(sas_target_alloc
);
984 EXPORT_SYMBOL_GPL(sas_slave_configure
);
985 EXPORT_SYMBOL_GPL(sas_change_queue_depth
);
986 EXPORT_SYMBOL_GPL(sas_bios_param
);
987 EXPORT_SYMBOL_GPL(sas_task_abort
);
988 EXPORT_SYMBOL_GPL(sas_phy_reset
);
989 EXPORT_SYMBOL_GPL(sas_eh_device_reset_handler
);
990 EXPORT_SYMBOL_GPL(sas_eh_target_reset_handler
);
991 EXPORT_SYMBOL_GPL(sas_target_destroy
);
992 EXPORT_SYMBOL_GPL(sas_ioctl
);