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/ctype.h>
30 #include "sas_internal.h"
32 #include <scsi/scsi_host.h>
33 #include <scsi/scsi_device.h>
34 #include <scsi/scsi_tcq.h>
35 #include <scsi/scsi.h>
36 #include <scsi/scsi_eh.h>
37 #include <scsi/scsi_transport.h>
38 #include <scsi/scsi_transport_sas.h>
39 #include <scsi/sas_ata.h>
40 #include "../scsi_sas_internal.h"
41 #include "../scsi_transport_api.h"
42 #include "../scsi_priv.h"
44 #include <linux/err.h>
45 #include <linux/blkdev.h>
46 #include <linux/freezer.h>
47 #include <linux/scatterlist.h>
48 #include <linux/libata.h>
50 /* ---------- SCSI Host glue ---------- */
52 static void sas_scsi_task_done(struct sas_task
*task
)
54 struct task_status_struct
*ts
= &task
->task_status
;
55 struct scsi_cmnd
*sc
= task
->uldd_task
;
58 if (unlikely(task
->task_state_flags
& SAS_TASK_STATE_ABORTED
)) {
59 /* Aborted tasks will be completed by the error handler */
60 SAS_DPRINTK("task done but aborted\n");
65 SAS_DPRINTK("task_done called with non existing SCSI cmnd!\n");
66 list_del_init(&task
->list
);
71 if (ts
->resp
== SAS_TASK_UNDELIVERED
) {
74 } else { /* ts->resp == SAS_TASK_COMPLETE */
75 /* task delivered, what happened afterwards? */
77 case SAS_DEV_NO_RESPONSE
:
84 case SAS_DATA_UNDERRUN
:
85 scsi_set_resid(sc
, ts
->residual
);
86 if (scsi_bufflen(sc
) - scsi_get_resid(sc
) < sc
->underflow
)
89 case SAS_DATA_OVERRUN
:
93 hs
= DID_SOFT_ERROR
; /* retry */
95 case SAS_DEVICE_UNKNOWN
:
101 case SAS_OPEN_REJECT
:
102 if (ts
->open_rej_reason
== SAS_OREJ_RSVD_RETRY
)
103 hs
= DID_SOFT_ERROR
; /* retry */
107 case SAS_PROTO_RESPONSE
:
108 SAS_DPRINTK("LLDD:%s sent SAS_PROTO_RESP for an SSP "
109 "task; please report this\n",
110 task
->dev
->port
->ha
->sas_ha_name
);
112 case SAS_ABORTED_TASK
:
116 memcpy(sc
->sense_buffer
, ts
->buf
,
117 min(SCSI_SENSE_BUFFERSIZE
, ts
->buf_valid_size
));
118 stat
= SAM_CHECK_COND
;
125 ASSIGN_SAS_TASK(sc
, NULL
);
126 sc
->result
= (hs
<< 16) | stat
;
127 list_del_init(&task
->list
);
132 static enum task_attribute
sas_scsi_get_task_attr(struct scsi_cmnd
*cmd
)
134 enum task_attribute ta
= TASK_ATTR_SIMPLE
;
135 if (cmd
->request
&& blk_rq_tagged(cmd
->request
)) {
136 if (cmd
->device
->ordered_tags
&&
137 (cmd
->request
->cmd_flags
& REQ_HARDBARRIER
))
138 ta
= TASK_ATTR_ORDERED
;
143 static struct sas_task
*sas_create_task(struct scsi_cmnd
*cmd
,
144 struct domain_device
*dev
,
147 struct sas_task
*task
= sas_alloc_task(gfp_flags
);
153 task
->uldd_task
= cmd
;
154 ASSIGN_SAS_TASK(cmd
, task
);
157 task
->task_proto
= task
->dev
->tproto
; /* BUG_ON(!SSP) */
159 task
->ssp_task
.retry_count
= 1;
160 int_to_scsilun(cmd
->device
->lun
, &lun
);
161 memcpy(task
->ssp_task
.LUN
, &lun
.scsi_lun
, 8);
162 task
->ssp_task
.task_attr
= sas_scsi_get_task_attr(cmd
);
163 memcpy(task
->ssp_task
.cdb
, cmd
->cmnd
, 16);
165 task
->scatter
= scsi_sglist(cmd
);
166 task
->num_scatter
= scsi_sg_count(cmd
);
167 task
->total_xfer_len
= scsi_bufflen(cmd
);
168 task
->data_dir
= cmd
->sc_data_direction
;
170 task
->task_done
= sas_scsi_task_done
;
175 int sas_queue_up(struct sas_task
*task
)
177 struct sas_ha_struct
*sas_ha
= task
->dev
->port
->ha
;
178 struct scsi_core
*core
= &sas_ha
->core
;
182 spin_lock_irqsave(&core
->task_queue_lock
, flags
);
183 if (sas_ha
->lldd_queue_size
< core
->task_queue_size
+ 1) {
184 spin_unlock_irqrestore(&core
->task_queue_lock
, flags
);
185 return -SAS_QUEUE_FULL
;
187 list_add_tail(&task
->list
, &core
->task_queue
);
188 core
->task_queue_size
+= 1;
189 spin_unlock_irqrestore(&core
->task_queue_lock
, flags
);
190 wake_up_process(core
->queue_thread
);
196 * sas_queuecommand -- Enqueue a command for processing
197 * @parameters: See SCSI Core documentation
199 * Note: XXX: Remove the host unlock/lock pair when SCSI Core can
200 * call us without holding an IRQ spinlock...
202 int sas_queuecommand(struct scsi_cmnd
*cmd
,
203 void (*scsi_done
)(struct scsi_cmnd
*))
204 __releases(host
->host_lock
)
205 __acquires(dev
->sata_dev
.ap
->lock
)
206 __releases(dev
->sata_dev
.ap
->lock
)
207 __acquires(host
->host_lock
)
210 struct domain_device
*dev
= cmd_to_domain_dev(cmd
);
211 struct Scsi_Host
*host
= cmd
->device
->host
;
212 struct sas_internal
*i
= to_sas_internal(host
->transportt
);
214 spin_unlock_irq(host
->host_lock
);
217 struct sas_ha_struct
*sas_ha
= dev
->port
->ha
;
218 struct sas_task
*task
;
220 if (dev_is_sata(dev
)) {
223 spin_lock_irqsave(dev
->sata_dev
.ap
->lock
, flags
);
224 res
= ata_sas_queuecmd(cmd
, scsi_done
,
226 spin_unlock_irqrestore(dev
->sata_dev
.ap
->lock
, flags
);
231 task
= sas_create_task(cmd
, dev
, GFP_ATOMIC
);
235 cmd
->scsi_done
= scsi_done
;
236 /* Queue up, Direct Mode or Task Collector Mode. */
237 if (sas_ha
->lldd_max_execute_num
< 2)
238 res
= i
->dft
->lldd_execute_task(task
, 1, GFP_ATOMIC
);
240 res
= sas_queue_up(task
);
244 SAS_DPRINTK("lldd_execute_task returned: %d\n", res
);
245 ASSIGN_SAS_TASK(cmd
, NULL
);
247 if (res
== -SAS_QUEUE_FULL
) {
248 cmd
->result
= DID_SOFT_ERROR
<< 16; /* retry */
256 spin_lock_irq(host
->host_lock
);
260 static void sas_eh_finish_cmd(struct scsi_cmnd
*cmd
)
262 struct sas_task
*task
= TO_SAS_TASK(cmd
);
263 struct sas_ha_struct
*sas_ha
= SHOST_TO_SAS_HA(cmd
->device
->host
);
265 /* remove the aborted task flag to allow the task to be
266 * completed now. At this point, we only get called following
267 * an actual abort of the task, so we should be guaranteed not
268 * to be racing with any completions from the LLD (hence we
269 * don't need the task state lock to clear the flag) */
270 task
->task_state_flags
&= ~SAS_TASK_STATE_ABORTED
;
271 /* Now call task_done. However, task will be free'd after
273 task
->task_done(task
);
274 /* now finish the command and move it on to the error
275 * handler done list, this also takes it off the
276 * error handler pending list */
277 scsi_eh_finish_cmd(cmd
, &sas_ha
->eh_done_q
);
280 static void sas_scsi_clear_queue_lu(struct list_head
*error_q
, struct scsi_cmnd
*my_cmd
)
282 struct scsi_cmnd
*cmd
, *n
;
284 list_for_each_entry_safe(cmd
, n
, error_q
, eh_entry
) {
285 if (cmd
->device
->sdev_target
== my_cmd
->device
->sdev_target
&&
286 cmd
->device
->lun
== my_cmd
->device
->lun
)
287 sas_eh_finish_cmd(cmd
);
291 static void sas_scsi_clear_queue_I_T(struct list_head
*error_q
,
292 struct domain_device
*dev
)
294 struct scsi_cmnd
*cmd
, *n
;
296 list_for_each_entry_safe(cmd
, n
, error_q
, eh_entry
) {
297 struct domain_device
*x
= cmd_to_domain_dev(cmd
);
300 sas_eh_finish_cmd(cmd
);
304 static void sas_scsi_clear_queue_port(struct list_head
*error_q
,
305 struct asd_sas_port
*port
)
307 struct scsi_cmnd
*cmd
, *n
;
309 list_for_each_entry_safe(cmd
, n
, error_q
, eh_entry
) {
310 struct domain_device
*dev
= cmd_to_domain_dev(cmd
);
311 struct asd_sas_port
*x
= dev
->port
;
314 sas_eh_finish_cmd(cmd
);
318 enum task_disposition
{
326 static enum task_disposition
sas_scsi_find_task(struct sas_task
*task
)
328 struct sas_ha_struct
*ha
= task
->dev
->port
->ha
;
331 struct sas_internal
*si
=
332 to_sas_internal(task
->dev
->port
->ha
->core
.shost
->transportt
);
334 if (ha
->lldd_max_execute_num
> 1) {
335 struct scsi_core
*core
= &ha
->core
;
336 struct sas_task
*t
, *n
;
338 spin_lock_irqsave(&core
->task_queue_lock
, flags
);
339 list_for_each_entry_safe(t
, n
, &core
->task_queue
, list
) {
341 list_del_init(&t
->list
);
342 spin_unlock_irqrestore(&core
->task_queue_lock
,
344 SAS_DPRINTK("%s: task 0x%p aborted from "
347 return TASK_IS_ABORTED
;
350 spin_unlock_irqrestore(&core
->task_queue_lock
, flags
);
353 for (i
= 0; i
< 5; i
++) {
354 SAS_DPRINTK("%s: aborting task 0x%p\n", __func__
, task
);
355 res
= si
->dft
->lldd_abort_task(task
);
357 spin_lock_irqsave(&task
->task_state_lock
, flags
);
358 if (task
->task_state_flags
& SAS_TASK_STATE_DONE
) {
359 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
360 SAS_DPRINTK("%s: task 0x%p is done\n", __func__
,
364 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
366 if (res
== TMF_RESP_FUNC_COMPLETE
) {
367 SAS_DPRINTK("%s: task 0x%p is aborted\n",
369 return TASK_IS_ABORTED
;
370 } else if (si
->dft
->lldd_query_task
) {
371 SAS_DPRINTK("%s: querying task 0x%p\n",
373 res
= si
->dft
->lldd_query_task(task
);
375 case TMF_RESP_FUNC_SUCC
:
376 SAS_DPRINTK("%s: task 0x%p at LU\n",
378 return TASK_IS_AT_LU
;
379 case TMF_RESP_FUNC_COMPLETE
:
380 SAS_DPRINTK("%s: task 0x%p not at LU\n",
382 return TASK_IS_NOT_AT_LU
;
383 case TMF_RESP_FUNC_FAILED
:
384 SAS_DPRINTK("%s: task 0x%p failed to abort\n",
386 return TASK_ABORT_FAILED
;
394 static int sas_recover_lu(struct domain_device
*dev
, struct scsi_cmnd
*cmd
)
396 int res
= TMF_RESP_FUNC_FAILED
;
398 struct sas_internal
*i
=
399 to_sas_internal(dev
->port
->ha
->core
.shost
->transportt
);
401 int_to_scsilun(cmd
->device
->lun
, &lun
);
403 SAS_DPRINTK("eh: device %llx LUN %x has the task\n",
404 SAS_ADDR(dev
->sas_addr
),
407 if (i
->dft
->lldd_abort_task_set
)
408 res
= i
->dft
->lldd_abort_task_set(dev
, lun
.scsi_lun
);
410 if (res
== TMF_RESP_FUNC_FAILED
) {
411 if (i
->dft
->lldd_clear_task_set
)
412 res
= i
->dft
->lldd_clear_task_set(dev
, lun
.scsi_lun
);
415 if (res
== TMF_RESP_FUNC_FAILED
) {
416 if (i
->dft
->lldd_lu_reset
)
417 res
= i
->dft
->lldd_lu_reset(dev
, lun
.scsi_lun
);
423 static int sas_recover_I_T(struct domain_device
*dev
)
425 int res
= TMF_RESP_FUNC_FAILED
;
426 struct sas_internal
*i
=
427 to_sas_internal(dev
->port
->ha
->core
.shost
->transportt
);
429 SAS_DPRINTK("I_T nexus reset for dev %016llx\n",
430 SAS_ADDR(dev
->sas_addr
));
432 if (i
->dft
->lldd_I_T_nexus_reset
)
433 res
= i
->dft
->lldd_I_T_nexus_reset(dev
);
438 /* Find the sas_phy that's attached to this device */
439 struct sas_phy
*sas_find_local_phy(struct domain_device
*dev
)
441 struct domain_device
*pdev
= dev
->parent
;
442 struct ex_phy
*exphy
= NULL
;
445 /* Directly attached device */
447 return dev
->port
->phy
;
449 /* Otherwise look in the expander */
450 for (i
= 0; i
< pdev
->ex_dev
.num_phys
; i
++)
451 if (!memcmp(dev
->sas_addr
,
452 pdev
->ex_dev
.ex_phy
[i
].attached_sas_addr
,
454 exphy
= &pdev
->ex_dev
.ex_phy
[i
];
461 EXPORT_SYMBOL_GPL(sas_find_local_phy
);
463 /* Attempt to send a LUN reset message to a device */
464 int sas_eh_device_reset_handler(struct scsi_cmnd
*cmd
)
466 struct domain_device
*dev
= cmd_to_domain_dev(cmd
);
467 struct sas_internal
*i
=
468 to_sas_internal(dev
->port
->ha
->core
.shost
->transportt
);
472 int_to_scsilun(cmd
->device
->lun
, &lun
);
474 if (!i
->dft
->lldd_lu_reset
)
477 res
= i
->dft
->lldd_lu_reset(dev
, lun
.scsi_lun
);
478 if (res
== TMF_RESP_FUNC_SUCC
|| res
== TMF_RESP_FUNC_COMPLETE
)
484 /* Attempt to send a phy (bus) reset */
485 int sas_eh_bus_reset_handler(struct scsi_cmnd
*cmd
)
487 struct domain_device
*dev
= cmd_to_domain_dev(cmd
);
488 struct sas_phy
*phy
= sas_find_local_phy(dev
);
491 res
= sas_phy_reset(phy
, 1);
493 SAS_DPRINTK("Bus reset of %s failed 0x%x\n",
494 kobject_name(&phy
->dev
.kobj
),
496 if (res
== TMF_RESP_FUNC_SUCC
|| res
== TMF_RESP_FUNC_COMPLETE
)
502 /* Try to reset a device */
503 static int try_to_reset_cmd_device(struct scsi_cmnd
*cmd
)
506 struct Scsi_Host
*shost
= cmd
->device
->host
;
508 if (!shost
->hostt
->eh_device_reset_handler
)
511 res
= shost
->hostt
->eh_device_reset_handler(cmd
);
516 if (shost
->hostt
->eh_bus_reset_handler
)
517 return shost
->hostt
->eh_bus_reset_handler(cmd
);
522 static int sas_eh_handle_sas_errors(struct Scsi_Host
*shost
,
523 struct list_head
*work_q
,
524 struct list_head
*done_q
)
526 struct scsi_cmnd
*cmd
, *n
;
527 enum task_disposition res
= TASK_IS_DONE
;
528 int tmf_resp
, need_reset
;
529 struct sas_internal
*i
= to_sas_internal(shost
->transportt
);
531 struct sas_ha_struct
*ha
= SHOST_TO_SAS_HA(shost
);
534 list_for_each_entry_safe(cmd
, n
, work_q
, eh_entry
) {
535 struct sas_task
*task
= TO_SAS_TASK(cmd
);
540 list_del_init(&cmd
->eh_entry
);
542 spin_lock_irqsave(&task
->task_state_lock
, flags
);
543 need_reset
= task
->task_state_flags
& SAS_TASK_NEED_DEV_RESET
;
544 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
547 SAS_DPRINTK("%s: task 0x%p requests reset\n",
552 SAS_DPRINTK("trying to find task 0x%p\n", task
);
553 res
= sas_scsi_find_task(task
);
559 SAS_DPRINTK("%s: task 0x%p is done\n", __func__
,
561 sas_eh_finish_cmd(cmd
);
563 case TASK_IS_ABORTED
:
564 SAS_DPRINTK("%s: task 0x%p is aborted\n",
566 sas_eh_finish_cmd(cmd
);
569 SAS_DPRINTK("task 0x%p is at LU: lu recover\n", task
);
571 tmf_resp
= sas_recover_lu(task
->dev
, cmd
);
572 if (tmf_resp
== TMF_RESP_FUNC_COMPLETE
) {
573 SAS_DPRINTK("dev %016llx LU %x is "
577 sas_eh_finish_cmd(cmd
);
578 sas_scsi_clear_queue_lu(work_q
, cmd
);
582 case TASK_IS_NOT_AT_LU
:
583 case TASK_ABORT_FAILED
:
584 SAS_DPRINTK("task 0x%p is not at LU: I_T recover\n",
586 tmf_resp
= sas_recover_I_T(task
->dev
);
587 if (tmf_resp
== TMF_RESP_FUNC_COMPLETE
) {
588 struct domain_device
*dev
= task
->dev
;
589 SAS_DPRINTK("I_T %016llx recovered\n",
590 SAS_ADDR(task
->dev
->sas_addr
));
591 sas_eh_finish_cmd(cmd
);
592 sas_scsi_clear_queue_I_T(work_q
, dev
);
595 /* Hammer time :-) */
596 try_to_reset_cmd_device(cmd
);
597 if (i
->dft
->lldd_clear_nexus_port
) {
598 struct asd_sas_port
*port
= task
->dev
->port
;
599 SAS_DPRINTK("clearing nexus for port:%d\n",
601 res
= i
->dft
->lldd_clear_nexus_port(port
);
602 if (res
== TMF_RESP_FUNC_COMPLETE
) {
603 SAS_DPRINTK("clear nexus port:%d "
604 "succeeded\n", port
->id
);
605 sas_eh_finish_cmd(cmd
);
606 sas_scsi_clear_queue_port(work_q
,
611 if (i
->dft
->lldd_clear_nexus_ha
) {
612 SAS_DPRINTK("clear nexus ha\n");
613 res
= i
->dft
->lldd_clear_nexus_ha(ha
);
614 if (res
== TMF_RESP_FUNC_COMPLETE
) {
615 SAS_DPRINTK("clear nexus ha "
617 sas_eh_finish_cmd(cmd
);
621 /* If we are here -- this means that no amount
622 * of effort could recover from errors. Quite
623 * possibly the HA just disappeared.
625 SAS_DPRINTK("error from device %llx, LUN %x "
626 "couldn't be recovered in any way\n",
627 SAS_ADDR(task
->dev
->sas_addr
),
630 sas_eh_finish_cmd(cmd
);
634 return list_empty(work_q
);
636 SAS_DPRINTK("--- Exit %s -- clear_q\n", __func__
);
637 list_for_each_entry_safe(cmd
, n
, work_q
, eh_entry
)
638 sas_eh_finish_cmd(cmd
);
640 return list_empty(work_q
);
643 void sas_scsi_recover_host(struct Scsi_Host
*shost
)
645 struct sas_ha_struct
*ha
= SHOST_TO_SAS_HA(shost
);
647 LIST_HEAD(eh_work_q
);
649 spin_lock_irqsave(shost
->host_lock
, flags
);
650 list_splice_init(&shost
->eh_cmd_q
, &eh_work_q
);
651 spin_unlock_irqrestore(shost
->host_lock
, flags
);
653 SAS_DPRINTK("Enter %s\n", __func__
);
655 * Deal with commands that still have SAS tasks (i.e. they didn't
656 * complete via the normal sas_task completion mechanism)
658 if (sas_eh_handle_sas_errors(shost
, &eh_work_q
, &ha
->eh_done_q
))
662 * Now deal with SCSI commands that completed ok but have a an error
663 * code (and hopefully sense data) attached. This is roughly what
664 * scsi_unjam_host does, but we skip scsi_eh_abort_cmds because any
665 * command we see here has no sas_task and is thus unknown to the HA.
667 if (!scsi_eh_get_sense(&eh_work_q
, &ha
->eh_done_q
))
668 scsi_eh_ready_devs(shost
, &eh_work_q
, &ha
->eh_done_q
);
671 scsi_eh_flush_done_q(&ha
->eh_done_q
);
672 SAS_DPRINTK("--- Exit %s\n", __func__
);
676 enum blk_eh_timer_return
sas_scsi_timed_out(struct scsi_cmnd
*cmd
)
678 struct sas_task
*task
= TO_SAS_TASK(cmd
);
682 cmd
->request
->timeout
/= 2;
683 SAS_DPRINTK("command 0x%p, task 0x%p, gone: %s\n",
684 cmd
, task
, (cmd
->request
->timeout
?
685 "BLK_EH_RESET_TIMER" : "BLK_EH_NOT_HANDLED"));
686 if (!cmd
->request
->timeout
)
687 return BLK_EH_NOT_HANDLED
;
688 return BLK_EH_RESET_TIMER
;
691 spin_lock_irqsave(&task
->task_state_lock
, flags
);
692 BUG_ON(task
->task_state_flags
& SAS_TASK_STATE_ABORTED
);
693 if (task
->task_state_flags
& SAS_TASK_STATE_DONE
) {
694 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
695 SAS_DPRINTK("command 0x%p, task 0x%p, timed out: "
696 "BLK_EH_HANDLED\n", cmd
, task
);
697 return BLK_EH_HANDLED
;
699 if (!(task
->task_state_flags
& SAS_TASK_AT_INITIATOR
)) {
700 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
701 SAS_DPRINTK("command 0x%p, task 0x%p, not at initiator: "
702 "BLK_EH_RESET_TIMER\n",
704 return BLK_EH_RESET_TIMER
;
706 task
->task_state_flags
|= SAS_TASK_STATE_ABORTED
;
707 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
709 SAS_DPRINTK("command 0x%p, task 0x%p, timed out: BLK_EH_NOT_HANDLED\n",
712 return BLK_EH_NOT_HANDLED
;
715 int sas_ioctl(struct scsi_device
*sdev
, int cmd
, void __user
*arg
)
717 struct domain_device
*dev
= sdev_to_domain_dev(sdev
);
719 if (dev_is_sata(dev
))
720 return ata_scsi_ioctl(sdev
, cmd
, arg
);
725 struct domain_device
*sas_find_dev_by_rphy(struct sas_rphy
*rphy
)
727 struct Scsi_Host
*shost
= dev_to_shost(rphy
->dev
.parent
);
728 struct sas_ha_struct
*ha
= SHOST_TO_SAS_HA(shost
);
729 struct domain_device
*found_dev
= NULL
;
733 spin_lock_irqsave(&ha
->phy_port_lock
, flags
);
734 for (i
= 0; i
< ha
->num_phys
; i
++) {
735 struct asd_sas_port
*port
= ha
->sas_port
[i
];
736 struct domain_device
*dev
;
738 spin_lock(&port
->dev_list_lock
);
739 list_for_each_entry(dev
, &port
->dev_list
, dev_list_node
) {
740 if (rphy
== dev
->rphy
) {
742 spin_unlock(&port
->dev_list_lock
);
746 spin_unlock(&port
->dev_list_lock
);
749 spin_unlock_irqrestore(&ha
->phy_port_lock
, flags
);
754 static inline struct domain_device
*sas_find_target(struct scsi_target
*starget
)
756 struct sas_rphy
*rphy
= dev_to_rphy(starget
->dev
.parent
);
758 return sas_find_dev_by_rphy(rphy
);
761 int sas_target_alloc(struct scsi_target
*starget
)
763 struct domain_device
*found_dev
= sas_find_target(starget
);
769 if (dev_is_sata(found_dev
)) {
770 res
= sas_ata_init_host_and_port(found_dev
, starget
);
775 starget
->hostdata
= found_dev
;
779 #define SAS_DEF_QD 32
780 #define SAS_MAX_QD 64
782 int sas_slave_configure(struct scsi_device
*scsi_dev
)
784 struct domain_device
*dev
= sdev_to_domain_dev(scsi_dev
);
785 struct sas_ha_struct
*sas_ha
;
787 BUG_ON(dev
->rphy
->identify
.device_type
!= SAS_END_DEVICE
);
789 if (dev_is_sata(dev
)) {
790 ata_sas_slave_configure(scsi_dev
, dev
->sata_dev
.ap
);
794 sas_ha
= dev
->port
->ha
;
796 sas_read_port_mode_page(scsi_dev
);
798 if (scsi_dev
->tagged_supported
) {
799 scsi_set_tag_type(scsi_dev
, MSG_SIMPLE_TAG
);
800 scsi_activate_tcq(scsi_dev
, SAS_DEF_QD
);
802 SAS_DPRINTK("device %llx, LUN %x doesn't support "
803 "TCQ\n", SAS_ADDR(dev
->sas_addr
),
805 scsi_dev
->tagged_supported
= 0;
806 scsi_set_tag_type(scsi_dev
, 0);
807 scsi_deactivate_tcq(scsi_dev
, 1);
810 scsi_dev
->allow_restart
= 1;
815 void sas_slave_destroy(struct scsi_device
*scsi_dev
)
817 struct domain_device
*dev
= sdev_to_domain_dev(scsi_dev
);
819 if (dev_is_sata(dev
))
820 ata_port_disable(dev
->sata_dev
.ap
);
823 int sas_change_queue_depth(struct scsi_device
*scsi_dev
, int new_depth
)
825 int res
= min(new_depth
, SAS_MAX_QD
);
827 if (scsi_dev
->tagged_supported
)
828 scsi_adjust_queue_depth(scsi_dev
, scsi_get_tag_type(scsi_dev
),
831 struct domain_device
*dev
= sdev_to_domain_dev(scsi_dev
);
832 sas_printk("device %llx LUN %x queue depth changed to 1\n",
833 SAS_ADDR(dev
->sas_addr
),
835 scsi_adjust_queue_depth(scsi_dev
, 0, 1);
842 int sas_change_queue_type(struct scsi_device
*scsi_dev
, int qt
)
844 if (!scsi_dev
->tagged_supported
)
847 scsi_deactivate_tcq(scsi_dev
, 1);
849 scsi_set_tag_type(scsi_dev
, qt
);
850 scsi_activate_tcq(scsi_dev
, scsi_dev
->queue_depth
);
855 int sas_bios_param(struct scsi_device
*scsi_dev
,
856 struct block_device
*bdev
,
857 sector_t capacity
, int *hsc
)
861 sector_div(capacity
, 255*63);
867 /* ---------- Task Collector Thread implementation ---------- */
869 static void sas_queue(struct sas_ha_struct
*sas_ha
)
871 struct scsi_core
*core
= &sas_ha
->core
;
876 struct sas_internal
*i
= to_sas_internal(core
->shost
->transportt
);
878 spin_lock_irqsave(&core
->task_queue_lock
, flags
);
879 while (!kthread_should_stop() &&
880 !list_empty(&core
->task_queue
)) {
882 can_queue
= sas_ha
->lldd_queue_size
- core
->task_queue_size
;
883 if (can_queue
>= 0) {
884 can_queue
= core
->task_queue_size
;
885 list_splice_init(&core
->task_queue
, &q
);
887 struct list_head
*a
, *n
;
889 can_queue
= sas_ha
->lldd_queue_size
;
890 list_for_each_safe(a
, n
, &core
->task_queue
) {
891 list_move_tail(a
, &q
);
892 if (--can_queue
== 0)
895 can_queue
= sas_ha
->lldd_queue_size
;
897 core
->task_queue_size
-= can_queue
;
898 spin_unlock_irqrestore(&core
->task_queue_lock
, flags
);
900 struct sas_task
*task
= list_entry(q
.next
,
904 res
= i
->dft
->lldd_execute_task(task
, can_queue
,
907 __list_add(&q
, task
->list
.prev
, &task
->list
);
909 spin_lock_irqsave(&core
->task_queue_lock
, flags
);
911 list_splice_init(&q
, &core
->task_queue
); /*at head*/
912 core
->task_queue_size
+= can_queue
;
915 spin_unlock_irqrestore(&core
->task_queue_lock
, flags
);
919 * sas_queue_thread -- The Task Collector thread
920 * @_sas_ha: pointer to struct sas_ha
922 static int sas_queue_thread(void *_sas_ha
)
924 struct sas_ha_struct
*sas_ha
= _sas_ha
;
927 set_current_state(TASK_INTERRUPTIBLE
);
930 if (kthread_should_stop())
937 int sas_init_queue(struct sas_ha_struct
*sas_ha
)
939 struct scsi_core
*core
= &sas_ha
->core
;
941 spin_lock_init(&core
->task_queue_lock
);
942 core
->task_queue_size
= 0;
943 INIT_LIST_HEAD(&core
->task_queue
);
945 core
->queue_thread
= kthread_run(sas_queue_thread
, sas_ha
,
946 "sas_queue_%d", core
->shost
->host_no
);
947 if (IS_ERR(core
->queue_thread
))
948 return PTR_ERR(core
->queue_thread
);
952 void sas_shutdown_queue(struct sas_ha_struct
*sas_ha
)
955 struct scsi_core
*core
= &sas_ha
->core
;
956 struct sas_task
*task
, *n
;
958 kthread_stop(core
->queue_thread
);
960 if (!list_empty(&core
->task_queue
))
961 SAS_DPRINTK("HA: %llx: scsi core task queue is NOT empty!?\n",
962 SAS_ADDR(sas_ha
->sas_addr
));
964 spin_lock_irqsave(&core
->task_queue_lock
, flags
);
965 list_for_each_entry_safe(task
, n
, &core
->task_queue
, list
) {
966 struct scsi_cmnd
*cmd
= task
->uldd_task
;
968 list_del_init(&task
->list
);
970 ASSIGN_SAS_TASK(cmd
, NULL
);
972 cmd
->result
= DID_ABORT
<< 16;
975 spin_unlock_irqrestore(&core
->task_queue_lock
, flags
);
979 * Call the LLDD task abort routine directly. This function is intended for
980 * use by upper layers that need to tell the LLDD to abort a task.
982 int __sas_task_abort(struct sas_task
*task
)
984 struct sas_internal
*si
=
985 to_sas_internal(task
->dev
->port
->ha
->core
.shost
->transportt
);
989 spin_lock_irqsave(&task
->task_state_lock
, flags
);
990 if (task
->task_state_flags
& SAS_TASK_STATE_ABORTED
||
991 task
->task_state_flags
& SAS_TASK_STATE_DONE
) {
992 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
993 SAS_DPRINTK("%s: Task %p already finished.\n", __func__
,
997 task
->task_state_flags
|= SAS_TASK_STATE_ABORTED
;
998 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
1000 if (!si
->dft
->lldd_abort_task
)
1003 res
= si
->dft
->lldd_abort_task(task
);
1005 spin_lock_irqsave(&task
->task_state_lock
, flags
);
1006 if ((task
->task_state_flags
& SAS_TASK_STATE_DONE
) ||
1007 (res
== TMF_RESP_FUNC_COMPLETE
))
1009 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
1010 task
->task_done(task
);
1014 if (!(task
->task_state_flags
& SAS_TASK_STATE_DONE
))
1015 task
->task_state_flags
&= ~SAS_TASK_STATE_ABORTED
;
1016 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
1022 * Tell an upper layer that it needs to initiate an abort for a given task.
1023 * This should only ever be called by an LLDD.
1025 void sas_task_abort(struct sas_task
*task
)
1027 struct scsi_cmnd
*sc
= task
->uldd_task
;
1029 /* Escape for libsas internal commands */
1031 if (!del_timer(&task
->timer
))
1033 task
->timer
.function(task
->timer
.data
);
1037 if (dev_is_sata(task
->dev
)) {
1038 sas_ata_task_abort(task
);
1042 blk_abort_request(sc
->request
);
1043 scsi_schedule_eh(sc
->device
->host
);
1046 int sas_slave_alloc(struct scsi_device
*scsi_dev
)
1048 struct domain_device
*dev
= sdev_to_domain_dev(scsi_dev
);
1050 if (dev_is_sata(dev
))
1051 return ata_sas_port_init(dev
->sata_dev
.ap
);
1056 void sas_target_destroy(struct scsi_target
*starget
)
1058 struct domain_device
*found_dev
= sas_find_target(starget
);
1063 if (dev_is_sata(found_dev
))
1064 ata_sas_port_destroy(found_dev
->sata_dev
.ap
);
1069 static void sas_parse_addr(u8
*sas_addr
, const char *p
)
1072 for (i
= 0; i
< SAS_ADDR_SIZE
; i
++) {
1076 h
= isdigit(*p
) ? *p
-'0' : toupper(*p
)-'A'+10;
1078 l
= isdigit(*p
) ? *p
-'0' : toupper(*p
)-'A'+10;
1080 sas_addr
[i
] = (h
<<4) | l
;
1084 #define SAS_STRING_ADDR_SIZE 16
1086 int sas_request_addr(struct Scsi_Host
*shost
, u8
*addr
)
1089 const struct firmware
*fw
;
1091 res
= request_firmware(&fw
, "sas_addr", &shost
->shost_gendev
);
1095 if (fw
->size
< SAS_STRING_ADDR_SIZE
) {
1100 sas_parse_addr(addr
, fw
->data
);
1103 release_firmware(fw
);
1106 EXPORT_SYMBOL_GPL(sas_request_addr
);
1108 EXPORT_SYMBOL_GPL(sas_queuecommand
);
1109 EXPORT_SYMBOL_GPL(sas_target_alloc
);
1110 EXPORT_SYMBOL_GPL(sas_slave_configure
);
1111 EXPORT_SYMBOL_GPL(sas_slave_destroy
);
1112 EXPORT_SYMBOL_GPL(sas_change_queue_depth
);
1113 EXPORT_SYMBOL_GPL(sas_change_queue_type
);
1114 EXPORT_SYMBOL_GPL(sas_bios_param
);
1115 EXPORT_SYMBOL_GPL(__sas_task_abort
);
1116 EXPORT_SYMBOL_GPL(sas_task_abort
);
1117 EXPORT_SYMBOL_GPL(sas_phy_reset
);
1118 EXPORT_SYMBOL_GPL(sas_phy_enable
);
1119 EXPORT_SYMBOL_GPL(sas_eh_device_reset_handler
);
1120 EXPORT_SYMBOL_GPL(sas_eh_bus_reset_handler
);
1121 EXPORT_SYMBOL_GPL(sas_slave_alloc
);
1122 EXPORT_SYMBOL_GPL(sas_target_destroy
);
1123 EXPORT_SYMBOL_GPL(sas_ioctl
);