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>
28 #include "sas_internal.h"
30 #include <scsi/scsi_host.h>
31 #include <scsi/scsi_device.h>
32 #include <scsi/scsi_tcq.h>
33 #include <scsi/scsi.h>
34 #include <scsi/scsi_eh.h>
35 #include <scsi/scsi_transport.h>
36 #include <scsi/scsi_transport_sas.h>
37 #include <scsi/sas_ata.h>
38 #include "../scsi_sas_internal.h"
39 #include "../scsi_transport_api.h"
40 #include "../scsi_priv.h"
42 #include <linux/err.h>
43 #include <linux/blkdev.h>
44 #include <linux/freezer.h>
45 #include <linux/scatterlist.h>
46 #include <linux/libata.h>
48 /* ---------- SCSI Host glue ---------- */
50 static void sas_scsi_task_done(struct sas_task
*task
)
52 struct task_status_struct
*ts
= &task
->task_status
;
53 struct scsi_cmnd
*sc
= task
->uldd_task
;
54 struct sas_ha_struct
*sas_ha
= SHOST_TO_SAS_HA(sc
->device
->host
);
55 unsigned ts_flags
= task
->task_state_flags
;
59 SAS_DPRINTK("task_done called with non existing SCSI cmnd!\n");
60 list_del_init(&task
->list
);
65 if (ts
->resp
== SAS_TASK_UNDELIVERED
) {
68 } else { /* ts->resp == SAS_TASK_COMPLETE */
69 /* task delivered, what happened afterwards? */
71 case SAS_DEV_NO_RESPONSE
:
78 case SAS_DATA_UNDERRUN
:
79 scsi_set_resid(sc
, ts
->residual
);
80 if (scsi_bufflen(sc
) - scsi_get_resid(sc
) < sc
->underflow
)
83 case SAS_DATA_OVERRUN
:
87 hs
= DID_SOFT_ERROR
; /* retry */
89 case SAS_DEVICE_UNKNOWN
:
96 if (ts
->open_rej_reason
== SAS_OREJ_RSVD_RETRY
)
97 hs
= DID_SOFT_ERROR
; /* retry */
101 case SAS_PROTO_RESPONSE
:
102 SAS_DPRINTK("LLDD:%s sent SAS_PROTO_RESP for an SSP "
103 "task; please report this\n",
104 task
->dev
->port
->ha
->sas_ha_name
);
106 case SAS_ABORTED_TASK
:
110 memcpy(sc
->sense_buffer
, ts
->buf
,
111 min(SCSI_SENSE_BUFFERSIZE
, ts
->buf_valid_size
));
112 stat
= SAM_CHECK_COND
;
119 ASSIGN_SAS_TASK(sc
, NULL
);
120 sc
->result
= (hs
<< 16) | stat
;
121 list_del_init(&task
->list
);
123 /* This is very ugly but this is how SCSI Core works. */
124 if (ts_flags
& SAS_TASK_STATE_ABORTED
)
125 scsi_eh_finish_cmd(sc
, &sas_ha
->eh_done_q
);
130 static enum task_attribute
sas_scsi_get_task_attr(struct scsi_cmnd
*cmd
)
132 enum task_attribute ta
= TASK_ATTR_SIMPLE
;
133 if (cmd
->request
&& blk_rq_tagged(cmd
->request
)) {
134 if (cmd
->device
->ordered_tags
&&
135 (cmd
->request
->cmd_flags
& REQ_HARDBARRIER
))
136 ta
= TASK_ATTR_ORDERED
;
141 static struct sas_task
*sas_create_task(struct scsi_cmnd
*cmd
,
142 struct domain_device
*dev
,
145 struct sas_task
*task
= sas_alloc_task(gfp_flags
);
151 task
->uldd_task
= cmd
;
152 ASSIGN_SAS_TASK(cmd
, task
);
155 task
->task_proto
= task
->dev
->tproto
; /* BUG_ON(!SSP) */
157 task
->ssp_task
.retry_count
= 1;
158 int_to_scsilun(cmd
->device
->lun
, &lun
);
159 memcpy(task
->ssp_task
.LUN
, &lun
.scsi_lun
, 8);
160 task
->ssp_task
.task_attr
= sas_scsi_get_task_attr(cmd
);
161 memcpy(task
->ssp_task
.cdb
, cmd
->cmnd
, 16);
163 task
->scatter
= scsi_sglist(cmd
);
164 task
->num_scatter
= scsi_sg_count(cmd
);
165 task
->total_xfer_len
= scsi_bufflen(cmd
);
166 task
->data_dir
= cmd
->sc_data_direction
;
168 task
->task_done
= sas_scsi_task_done
;
173 int sas_queue_up(struct sas_task
*task
)
175 struct sas_ha_struct
*sas_ha
= task
->dev
->port
->ha
;
176 struct scsi_core
*core
= &sas_ha
->core
;
180 spin_lock_irqsave(&core
->task_queue_lock
, flags
);
181 if (sas_ha
->lldd_queue_size
< core
->task_queue_size
+ 1) {
182 spin_unlock_irqrestore(&core
->task_queue_lock
, flags
);
183 return -SAS_QUEUE_FULL
;
185 list_add_tail(&task
->list
, &core
->task_queue
);
186 core
->task_queue_size
+= 1;
187 spin_unlock_irqrestore(&core
->task_queue_lock
, flags
);
188 wake_up_process(core
->queue_thread
);
194 * sas_queuecommand -- Enqueue a command for processing
195 * @parameters: See SCSI Core documentation
197 * Note: XXX: Remove the host unlock/lock pair when SCSI Core can
198 * call us without holding an IRQ spinlock...
200 int sas_queuecommand(struct scsi_cmnd
*cmd
,
201 void (*scsi_done
)(struct scsi_cmnd
*))
202 __releases(host
->host_lock
)
203 __acquires(dev
->sata_dev
.ap
->lock
)
204 __releases(dev
->sata_dev
.ap
->lock
)
205 __acquires(host
->host_lock
)
208 struct domain_device
*dev
= cmd_to_domain_dev(cmd
);
209 struct Scsi_Host
*host
= cmd
->device
->host
;
210 struct sas_internal
*i
= to_sas_internal(host
->transportt
);
212 spin_unlock_irq(host
->host_lock
);
215 struct sas_ha_struct
*sas_ha
= dev
->port
->ha
;
216 struct sas_task
*task
;
218 if (dev_is_sata(dev
)) {
221 spin_lock_irqsave(dev
->sata_dev
.ap
->lock
, flags
);
222 res
= ata_sas_queuecmd(cmd
, scsi_done
,
224 spin_unlock_irqrestore(dev
->sata_dev
.ap
->lock
, flags
);
229 task
= sas_create_task(cmd
, dev
, GFP_ATOMIC
);
233 cmd
->scsi_done
= scsi_done
;
234 /* Queue up, Direct Mode or Task Collector Mode. */
235 if (sas_ha
->lldd_max_execute_num
< 2)
236 res
= i
->dft
->lldd_execute_task(task
, 1, GFP_ATOMIC
);
238 res
= sas_queue_up(task
);
242 SAS_DPRINTK("lldd_execute_task returned: %d\n", res
);
243 ASSIGN_SAS_TASK(cmd
, NULL
);
245 if (res
== -SAS_QUEUE_FULL
) {
246 cmd
->result
= DID_SOFT_ERROR
<< 16; /* retry */
254 spin_lock_irq(host
->host_lock
);
258 static void sas_scsi_clear_queue_lu(struct list_head
*error_q
, struct scsi_cmnd
*my_cmd
)
260 struct scsi_cmnd
*cmd
, *n
;
262 list_for_each_entry_safe(cmd
, n
, error_q
, eh_entry
) {
264 list_del_init(&cmd
->eh_entry
);
268 static void sas_scsi_clear_queue_I_T(struct list_head
*error_q
,
269 struct domain_device
*dev
)
271 struct scsi_cmnd
*cmd
, *n
;
273 list_for_each_entry_safe(cmd
, n
, error_q
, eh_entry
) {
274 struct domain_device
*x
= cmd_to_domain_dev(cmd
);
277 list_del_init(&cmd
->eh_entry
);
281 static void sas_scsi_clear_queue_port(struct list_head
*error_q
,
282 struct asd_sas_port
*port
)
284 struct scsi_cmnd
*cmd
, *n
;
286 list_for_each_entry_safe(cmd
, n
, error_q
, eh_entry
) {
287 struct domain_device
*dev
= cmd_to_domain_dev(cmd
);
288 struct asd_sas_port
*x
= dev
->port
;
291 list_del_init(&cmd
->eh_entry
);
295 enum task_disposition
{
303 static enum task_disposition
sas_scsi_find_task(struct sas_task
*task
)
305 struct sas_ha_struct
*ha
= task
->dev
->port
->ha
;
308 struct sas_internal
*si
=
309 to_sas_internal(task
->dev
->port
->ha
->core
.shost
->transportt
);
311 if (ha
->lldd_max_execute_num
> 1) {
312 struct scsi_core
*core
= &ha
->core
;
313 struct sas_task
*t
, *n
;
315 spin_lock_irqsave(&core
->task_queue_lock
, flags
);
316 list_for_each_entry_safe(t
, n
, &core
->task_queue
, list
) {
318 list_del_init(&t
->list
);
319 spin_unlock_irqrestore(&core
->task_queue_lock
,
321 SAS_DPRINTK("%s: task 0x%p aborted from "
324 return TASK_IS_ABORTED
;
327 spin_unlock_irqrestore(&core
->task_queue_lock
, flags
);
330 for (i
= 0; i
< 5; i
++) {
331 SAS_DPRINTK("%s: aborting task 0x%p\n", __FUNCTION__
, task
);
332 res
= si
->dft
->lldd_abort_task(task
);
334 spin_lock_irqsave(&task
->task_state_lock
, flags
);
335 if (task
->task_state_flags
& SAS_TASK_STATE_DONE
) {
336 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
337 SAS_DPRINTK("%s: task 0x%p is done\n", __FUNCTION__
,
341 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
343 if (res
== TMF_RESP_FUNC_COMPLETE
) {
344 SAS_DPRINTK("%s: task 0x%p is aborted\n",
346 return TASK_IS_ABORTED
;
347 } else if (si
->dft
->lldd_query_task
) {
348 SAS_DPRINTK("%s: querying task 0x%p\n",
350 res
= si
->dft
->lldd_query_task(task
);
352 case TMF_RESP_FUNC_SUCC
:
353 SAS_DPRINTK("%s: task 0x%p at LU\n",
355 return TASK_IS_AT_LU
;
356 case TMF_RESP_FUNC_COMPLETE
:
357 SAS_DPRINTK("%s: task 0x%p not at LU\n",
359 return TASK_IS_NOT_AT_LU
;
360 case TMF_RESP_FUNC_FAILED
:
361 SAS_DPRINTK("%s: task 0x%p failed to abort\n",
363 return TASK_ABORT_FAILED
;
371 static int sas_recover_lu(struct domain_device
*dev
, struct scsi_cmnd
*cmd
)
373 int res
= TMF_RESP_FUNC_FAILED
;
375 struct sas_internal
*i
=
376 to_sas_internal(dev
->port
->ha
->core
.shost
->transportt
);
378 int_to_scsilun(cmd
->device
->lun
, &lun
);
380 SAS_DPRINTK("eh: device %llx LUN %x has the task\n",
381 SAS_ADDR(dev
->sas_addr
),
384 if (i
->dft
->lldd_abort_task_set
)
385 res
= i
->dft
->lldd_abort_task_set(dev
, lun
.scsi_lun
);
387 if (res
== TMF_RESP_FUNC_FAILED
) {
388 if (i
->dft
->lldd_clear_task_set
)
389 res
= i
->dft
->lldd_clear_task_set(dev
, lun
.scsi_lun
);
392 if (res
== TMF_RESP_FUNC_FAILED
) {
393 if (i
->dft
->lldd_lu_reset
)
394 res
= i
->dft
->lldd_lu_reset(dev
, lun
.scsi_lun
);
400 static int sas_recover_I_T(struct domain_device
*dev
)
402 int res
= TMF_RESP_FUNC_FAILED
;
403 struct sas_internal
*i
=
404 to_sas_internal(dev
->port
->ha
->core
.shost
->transportt
);
406 SAS_DPRINTK("I_T nexus reset for dev %016llx\n",
407 SAS_ADDR(dev
->sas_addr
));
409 if (i
->dft
->lldd_I_T_nexus_reset
)
410 res
= i
->dft
->lldd_I_T_nexus_reset(dev
);
415 /* Find the sas_phy that's attached to this device */
416 static struct sas_phy
*find_local_sas_phy(struct domain_device
*dev
)
418 struct domain_device
*pdev
= dev
->parent
;
419 struct ex_phy
*exphy
= NULL
;
422 /* Directly attached device */
424 return dev
->port
->phy
;
426 /* Otherwise look in the expander */
427 for (i
= 0; i
< pdev
->ex_dev
.num_phys
; i
++)
428 if (!memcmp(dev
->sas_addr
,
429 pdev
->ex_dev
.ex_phy
[i
].attached_sas_addr
,
431 exphy
= &pdev
->ex_dev
.ex_phy
[i
];
439 /* Attempt to send a LUN reset message to a device */
440 int sas_eh_device_reset_handler(struct scsi_cmnd
*cmd
)
442 struct domain_device
*dev
= cmd_to_domain_dev(cmd
);
443 struct sas_internal
*i
=
444 to_sas_internal(dev
->port
->ha
->core
.shost
->transportt
);
448 int_to_scsilun(cmd
->device
->lun
, &lun
);
450 if (!i
->dft
->lldd_lu_reset
)
453 res
= i
->dft
->lldd_lu_reset(dev
, lun
.scsi_lun
);
454 if (res
== TMF_RESP_FUNC_SUCC
|| res
== TMF_RESP_FUNC_COMPLETE
)
460 /* Attempt to send a phy (bus) reset */
461 int sas_eh_bus_reset_handler(struct scsi_cmnd
*cmd
)
463 struct domain_device
*dev
= cmd_to_domain_dev(cmd
);
464 struct sas_phy
*phy
= find_local_sas_phy(dev
);
467 res
= sas_phy_reset(phy
, 1);
469 SAS_DPRINTK("Bus reset of %s failed 0x%x\n",
470 kobject_name(&phy
->dev
.kobj
),
472 if (res
== TMF_RESP_FUNC_SUCC
|| res
== TMF_RESP_FUNC_COMPLETE
)
478 /* Try to reset a device */
479 static int try_to_reset_cmd_device(struct Scsi_Host
*shost
,
480 struct scsi_cmnd
*cmd
)
484 if (!shost
->hostt
->eh_device_reset_handler
)
487 res
= shost
->hostt
->eh_device_reset_handler(cmd
);
492 if (shost
->hostt
->eh_bus_reset_handler
)
493 return shost
->hostt
->eh_bus_reset_handler(cmd
);
498 static int sas_eh_handle_sas_errors(struct Scsi_Host
*shost
,
499 struct list_head
*work_q
,
500 struct list_head
*done_q
)
502 struct scsi_cmnd
*cmd
, *n
;
503 enum task_disposition res
= TASK_IS_DONE
;
504 int tmf_resp
, need_reset
;
505 struct sas_internal
*i
= to_sas_internal(shost
->transportt
);
507 struct sas_ha_struct
*ha
= SHOST_TO_SAS_HA(shost
);
510 list_for_each_entry_safe(cmd
, n
, work_q
, eh_entry
) {
511 struct sas_task
*task
= TO_SAS_TASK(cmd
);
516 list_del_init(&cmd
->eh_entry
);
518 spin_lock_irqsave(&task
->task_state_lock
, flags
);
519 need_reset
= task
->task_state_flags
& SAS_TASK_NEED_DEV_RESET
;
520 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
522 SAS_DPRINTK("trying to find task 0x%p\n", task
);
523 res
= sas_scsi_find_task(task
);
529 SAS_DPRINTK("%s: task 0x%p is done\n", __FUNCTION__
,
531 task
->task_done(task
);
533 try_to_reset_cmd_device(shost
, cmd
);
535 case TASK_IS_ABORTED
:
536 SAS_DPRINTK("%s: task 0x%p is aborted\n",
538 task
->task_done(task
);
540 try_to_reset_cmd_device(shost
, cmd
);
543 SAS_DPRINTK("task 0x%p is at LU: lu recover\n", task
);
544 tmf_resp
= sas_recover_lu(task
->dev
, cmd
);
545 if (tmf_resp
== TMF_RESP_FUNC_COMPLETE
) {
546 SAS_DPRINTK("dev %016llx LU %x is "
550 task
->task_done(task
);
552 try_to_reset_cmd_device(shost
, cmd
);
553 sas_scsi_clear_queue_lu(work_q
, cmd
);
557 case TASK_IS_NOT_AT_LU
:
558 case TASK_ABORT_FAILED
:
559 SAS_DPRINTK("task 0x%p is not at LU: I_T recover\n",
561 tmf_resp
= sas_recover_I_T(task
->dev
);
562 if (tmf_resp
== TMF_RESP_FUNC_COMPLETE
) {
563 SAS_DPRINTK("I_T %016llx recovered\n",
564 SAS_ADDR(task
->dev
->sas_addr
));
565 task
->task_done(task
);
567 try_to_reset_cmd_device(shost
, cmd
);
568 sas_scsi_clear_queue_I_T(work_q
, task
->dev
);
571 /* Hammer time :-) */
572 if (i
->dft
->lldd_clear_nexus_port
) {
573 struct asd_sas_port
*port
= task
->dev
->port
;
574 SAS_DPRINTK("clearing nexus for port:%d\n",
576 res
= i
->dft
->lldd_clear_nexus_port(port
);
577 if (res
== TMF_RESP_FUNC_COMPLETE
) {
578 SAS_DPRINTK("clear nexus port:%d "
579 "succeeded\n", port
->id
);
580 task
->task_done(task
);
582 try_to_reset_cmd_device(shost
, cmd
);
583 sas_scsi_clear_queue_port(work_q
,
588 if (i
->dft
->lldd_clear_nexus_ha
) {
589 SAS_DPRINTK("clear nexus ha\n");
590 res
= i
->dft
->lldd_clear_nexus_ha(ha
);
591 if (res
== TMF_RESP_FUNC_COMPLETE
) {
592 SAS_DPRINTK("clear nexus ha "
594 task
->task_done(task
);
596 try_to_reset_cmd_device(shost
, cmd
);
600 /* If we are here -- this means that no amount
601 * of effort could recover from errors. Quite
602 * possibly the HA just disappeared.
604 SAS_DPRINTK("error from device %llx, LUN %x "
605 "couldn't be recovered in any way\n",
606 SAS_ADDR(task
->dev
->sas_addr
),
609 task
->task_done(task
);
611 try_to_reset_cmd_device(shost
, cmd
);
616 return list_empty(work_q
);
618 SAS_DPRINTK("--- Exit %s -- clear_q\n", __FUNCTION__
);
619 list_for_each_entry_safe(cmd
, n
, work_q
, eh_entry
) {
620 struct sas_task
*task
= TO_SAS_TASK(cmd
);
621 list_del_init(&cmd
->eh_entry
);
622 task
->task_done(task
);
624 return list_empty(work_q
);
627 void sas_scsi_recover_host(struct Scsi_Host
*shost
)
629 struct sas_ha_struct
*ha
= SHOST_TO_SAS_HA(shost
);
631 LIST_HEAD(eh_work_q
);
633 spin_lock_irqsave(shost
->host_lock
, flags
);
634 list_splice_init(&shost
->eh_cmd_q
, &eh_work_q
);
635 spin_unlock_irqrestore(shost
->host_lock
, flags
);
637 SAS_DPRINTK("Enter %s\n", __FUNCTION__
);
639 * Deal with commands that still have SAS tasks (i.e. they didn't
640 * complete via the normal sas_task completion mechanism)
642 if (sas_eh_handle_sas_errors(shost
, &eh_work_q
, &ha
->eh_done_q
))
646 * Now deal with SCSI commands that completed ok but have a an error
647 * code (and hopefully sense data) attached. This is roughly what
648 * scsi_unjam_host does, but we skip scsi_eh_abort_cmds because any
649 * command we see here has no sas_task and is thus unknown to the HA.
651 if (!scsi_eh_get_sense(&eh_work_q
, &ha
->eh_done_q
))
652 scsi_eh_ready_devs(shost
, &eh_work_q
, &ha
->eh_done_q
);
655 scsi_eh_flush_done_q(&ha
->eh_done_q
);
656 SAS_DPRINTK("--- Exit %s\n", __FUNCTION__
);
660 enum scsi_eh_timer_return
sas_scsi_timed_out(struct scsi_cmnd
*cmd
)
662 struct sas_task
*task
= TO_SAS_TASK(cmd
);
666 cmd
->timeout_per_command
/= 2;
667 SAS_DPRINTK("command 0x%p, task 0x%p, gone: %s\n",
668 cmd
, task
, (cmd
->timeout_per_command
?
669 "EH_RESET_TIMER" : "EH_NOT_HANDLED"));
670 if (!cmd
->timeout_per_command
)
671 return EH_NOT_HANDLED
;
672 return EH_RESET_TIMER
;
675 spin_lock_irqsave(&task
->task_state_lock
, flags
);
676 BUG_ON(task
->task_state_flags
& SAS_TASK_STATE_ABORTED
);
677 if (task
->task_state_flags
& SAS_TASK_STATE_DONE
) {
678 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
679 SAS_DPRINTK("command 0x%p, task 0x%p, timed out: EH_HANDLED\n",
683 if (!(task
->task_state_flags
& SAS_TASK_AT_INITIATOR
)) {
684 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
685 SAS_DPRINTK("command 0x%p, task 0x%p, not at initiator: "
688 return EH_RESET_TIMER
;
690 task
->task_state_flags
|= SAS_TASK_STATE_ABORTED
;
691 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
693 SAS_DPRINTK("command 0x%p, task 0x%p, timed out: EH_NOT_HANDLED\n",
696 return EH_NOT_HANDLED
;
699 int sas_ioctl(struct scsi_device
*sdev
, int cmd
, void __user
*arg
)
701 struct domain_device
*dev
= sdev_to_domain_dev(sdev
);
703 if (dev_is_sata(dev
))
704 return ata_scsi_ioctl(sdev
, cmd
, arg
);
709 struct domain_device
*sas_find_dev_by_rphy(struct sas_rphy
*rphy
)
711 struct Scsi_Host
*shost
= dev_to_shost(rphy
->dev
.parent
);
712 struct sas_ha_struct
*ha
= SHOST_TO_SAS_HA(shost
);
713 struct domain_device
*found_dev
= NULL
;
717 spin_lock_irqsave(&ha
->phy_port_lock
, flags
);
718 for (i
= 0; i
< ha
->num_phys
; i
++) {
719 struct asd_sas_port
*port
= ha
->sas_port
[i
];
720 struct domain_device
*dev
;
722 spin_lock(&port
->dev_list_lock
);
723 list_for_each_entry(dev
, &port
->dev_list
, dev_list_node
) {
724 if (rphy
== dev
->rphy
) {
726 spin_unlock(&port
->dev_list_lock
);
730 spin_unlock(&port
->dev_list_lock
);
733 spin_unlock_irqrestore(&ha
->phy_port_lock
, flags
);
738 static inline struct domain_device
*sas_find_target(struct scsi_target
*starget
)
740 struct sas_rphy
*rphy
= dev_to_rphy(starget
->dev
.parent
);
742 return sas_find_dev_by_rphy(rphy
);
745 int sas_target_alloc(struct scsi_target
*starget
)
747 struct domain_device
*found_dev
= sas_find_target(starget
);
753 if (dev_is_sata(found_dev
)) {
754 res
= sas_ata_init_host_and_port(found_dev
, starget
);
759 starget
->hostdata
= found_dev
;
763 #define SAS_DEF_QD 32
764 #define SAS_MAX_QD 64
766 int sas_slave_configure(struct scsi_device
*scsi_dev
)
768 struct domain_device
*dev
= sdev_to_domain_dev(scsi_dev
);
769 struct sas_ha_struct
*sas_ha
;
771 BUG_ON(dev
->rphy
->identify
.device_type
!= SAS_END_DEVICE
);
773 if (dev_is_sata(dev
)) {
774 ata_sas_slave_configure(scsi_dev
, dev
->sata_dev
.ap
);
778 sas_ha
= dev
->port
->ha
;
780 sas_read_port_mode_page(scsi_dev
);
782 if (scsi_dev
->tagged_supported
) {
783 scsi_set_tag_type(scsi_dev
, MSG_SIMPLE_TAG
);
784 scsi_activate_tcq(scsi_dev
, SAS_DEF_QD
);
786 SAS_DPRINTK("device %llx, LUN %x doesn't support "
787 "TCQ\n", SAS_ADDR(dev
->sas_addr
),
789 scsi_dev
->tagged_supported
= 0;
790 scsi_set_tag_type(scsi_dev
, 0);
791 scsi_deactivate_tcq(scsi_dev
, 1);
794 scsi_dev
->allow_restart
= 1;
799 void sas_slave_destroy(struct scsi_device
*scsi_dev
)
801 struct domain_device
*dev
= sdev_to_domain_dev(scsi_dev
);
803 if (dev_is_sata(dev
))
804 ata_port_disable(dev
->sata_dev
.ap
);
807 int sas_change_queue_depth(struct scsi_device
*scsi_dev
, int new_depth
)
809 int res
= min(new_depth
, SAS_MAX_QD
);
811 if (scsi_dev
->tagged_supported
)
812 scsi_adjust_queue_depth(scsi_dev
, scsi_get_tag_type(scsi_dev
),
815 struct domain_device
*dev
= sdev_to_domain_dev(scsi_dev
);
816 sas_printk("device %llx LUN %x queue depth changed to 1\n",
817 SAS_ADDR(dev
->sas_addr
),
819 scsi_adjust_queue_depth(scsi_dev
, 0, 1);
826 int sas_change_queue_type(struct scsi_device
*scsi_dev
, int qt
)
828 if (!scsi_dev
->tagged_supported
)
831 scsi_deactivate_tcq(scsi_dev
, 1);
833 scsi_set_tag_type(scsi_dev
, qt
);
834 scsi_activate_tcq(scsi_dev
, scsi_dev
->queue_depth
);
839 int sas_bios_param(struct scsi_device
*scsi_dev
,
840 struct block_device
*bdev
,
841 sector_t capacity
, int *hsc
)
845 sector_div(capacity
, 255*63);
851 /* ---------- Task Collector Thread implementation ---------- */
853 static void sas_queue(struct sas_ha_struct
*sas_ha
)
855 struct scsi_core
*core
= &sas_ha
->core
;
860 struct sas_internal
*i
= to_sas_internal(core
->shost
->transportt
);
862 spin_lock_irqsave(&core
->task_queue_lock
, flags
);
863 while (!kthread_should_stop() &&
864 !list_empty(&core
->task_queue
)) {
866 can_queue
= sas_ha
->lldd_queue_size
- core
->task_queue_size
;
867 if (can_queue
>= 0) {
868 can_queue
= core
->task_queue_size
;
869 list_splice_init(&core
->task_queue
, &q
);
871 struct list_head
*a
, *n
;
873 can_queue
= sas_ha
->lldd_queue_size
;
874 list_for_each_safe(a
, n
, &core
->task_queue
) {
875 list_move_tail(a
, &q
);
876 if (--can_queue
== 0)
879 can_queue
= sas_ha
->lldd_queue_size
;
881 core
->task_queue_size
-= can_queue
;
882 spin_unlock_irqrestore(&core
->task_queue_lock
, flags
);
884 struct sas_task
*task
= list_entry(q
.next
,
888 res
= i
->dft
->lldd_execute_task(task
, can_queue
,
891 __list_add(&q
, task
->list
.prev
, &task
->list
);
893 spin_lock_irqsave(&core
->task_queue_lock
, flags
);
895 list_splice_init(&q
, &core
->task_queue
); /*at head*/
896 core
->task_queue_size
+= can_queue
;
899 spin_unlock_irqrestore(&core
->task_queue_lock
, flags
);
903 * sas_queue_thread -- The Task Collector thread
904 * @_sas_ha: pointer to struct sas_ha
906 static int sas_queue_thread(void *_sas_ha
)
908 struct sas_ha_struct
*sas_ha
= _sas_ha
;
911 set_current_state(TASK_INTERRUPTIBLE
);
914 if (kthread_should_stop())
921 int sas_init_queue(struct sas_ha_struct
*sas_ha
)
923 struct scsi_core
*core
= &sas_ha
->core
;
925 spin_lock_init(&core
->task_queue_lock
);
926 core
->task_queue_size
= 0;
927 INIT_LIST_HEAD(&core
->task_queue
);
929 core
->queue_thread
= kthread_run(sas_queue_thread
, sas_ha
,
930 "sas_queue_%d", core
->shost
->host_no
);
931 if (IS_ERR(core
->queue_thread
))
932 return PTR_ERR(core
->queue_thread
);
936 void sas_shutdown_queue(struct sas_ha_struct
*sas_ha
)
939 struct scsi_core
*core
= &sas_ha
->core
;
940 struct sas_task
*task
, *n
;
942 kthread_stop(core
->queue_thread
);
944 if (!list_empty(&core
->task_queue
))
945 SAS_DPRINTK("HA: %llx: scsi core task queue is NOT empty!?\n",
946 SAS_ADDR(sas_ha
->sas_addr
));
948 spin_lock_irqsave(&core
->task_queue_lock
, flags
);
949 list_for_each_entry_safe(task
, n
, &core
->task_queue
, list
) {
950 struct scsi_cmnd
*cmd
= task
->uldd_task
;
952 list_del_init(&task
->list
);
954 ASSIGN_SAS_TASK(cmd
, NULL
);
956 cmd
->result
= DID_ABORT
<< 16;
959 spin_unlock_irqrestore(&core
->task_queue_lock
, flags
);
963 * Call the LLDD task abort routine directly. This function is intended for
964 * use by upper layers that need to tell the LLDD to abort a task.
966 int __sas_task_abort(struct sas_task
*task
)
968 struct sas_internal
*si
=
969 to_sas_internal(task
->dev
->port
->ha
->core
.shost
->transportt
);
973 spin_lock_irqsave(&task
->task_state_lock
, flags
);
974 if (task
->task_state_flags
& SAS_TASK_STATE_ABORTED
||
975 task
->task_state_flags
& SAS_TASK_STATE_DONE
) {
976 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
977 SAS_DPRINTK("%s: Task %p already finished.\n", __FUNCTION__
,
981 task
->task_state_flags
|= SAS_TASK_STATE_ABORTED
;
982 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
984 if (!si
->dft
->lldd_abort_task
)
987 res
= si
->dft
->lldd_abort_task(task
);
989 spin_lock_irqsave(&task
->task_state_lock
, flags
);
990 if ((task
->task_state_flags
& SAS_TASK_STATE_DONE
) ||
991 (res
== TMF_RESP_FUNC_COMPLETE
))
993 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
994 task
->task_done(task
);
998 if (!(task
->task_state_flags
& SAS_TASK_STATE_DONE
))
999 task
->task_state_flags
&= ~SAS_TASK_STATE_ABORTED
;
1000 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
1006 * Tell an upper layer that it needs to initiate an abort for a given task.
1007 * This should only ever be called by an LLDD.
1009 void sas_task_abort(struct sas_task
*task
)
1011 struct scsi_cmnd
*sc
= task
->uldd_task
;
1013 /* Escape for libsas internal commands */
1015 if (!del_timer(&task
->timer
))
1017 task
->timer
.function(task
->timer
.data
);
1021 if (dev_is_sata(task
->dev
)) {
1022 sas_ata_task_abort(task
);
1026 scsi_req_abort_cmd(sc
);
1027 scsi_schedule_eh(sc
->device
->host
);
1030 int sas_slave_alloc(struct scsi_device
*scsi_dev
)
1032 struct domain_device
*dev
= sdev_to_domain_dev(scsi_dev
);
1034 if (dev_is_sata(dev
))
1035 return ata_sas_port_init(dev
->sata_dev
.ap
);
1040 void sas_target_destroy(struct scsi_target
*starget
)
1042 struct domain_device
*found_dev
= sas_find_target(starget
);
1047 if (dev_is_sata(found_dev
))
1048 ata_sas_port_destroy(found_dev
->sata_dev
.ap
);
1053 EXPORT_SYMBOL_GPL(sas_queuecommand
);
1054 EXPORT_SYMBOL_GPL(sas_target_alloc
);
1055 EXPORT_SYMBOL_GPL(sas_slave_configure
);
1056 EXPORT_SYMBOL_GPL(sas_slave_destroy
);
1057 EXPORT_SYMBOL_GPL(sas_change_queue_depth
);
1058 EXPORT_SYMBOL_GPL(sas_change_queue_type
);
1059 EXPORT_SYMBOL_GPL(sas_bios_param
);
1060 EXPORT_SYMBOL_GPL(__sas_task_abort
);
1061 EXPORT_SYMBOL_GPL(sas_task_abort
);
1062 EXPORT_SYMBOL_GPL(sas_phy_reset
);
1063 EXPORT_SYMBOL_GPL(sas_phy_enable
);
1064 EXPORT_SYMBOL_GPL(sas_eh_device_reset_handler
);
1065 EXPORT_SYMBOL_GPL(sas_eh_bus_reset_handler
);
1066 EXPORT_SYMBOL_GPL(sas_slave_alloc
);
1067 EXPORT_SYMBOL_GPL(sas_target_destroy
);
1068 EXPORT_SYMBOL_GPL(sas_ioctl
);