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 /* ---------- SCSI Host glue ---------- */
54 static void sas_scsi_task_done(struct sas_task
*task
)
56 struct task_status_struct
*ts
= &task
->task_status
;
57 struct scsi_cmnd
*sc
= task
->uldd_task
;
60 if (unlikely(task
->task_state_flags
& SAS_TASK_STATE_ABORTED
)) {
61 /* Aborted tasks will be completed by the error handler */
62 SAS_DPRINTK("task done but aborted\n");
67 SAS_DPRINTK("task_done called with non existing SCSI cmnd!\n");
68 list_del_init(&task
->list
);
73 if (ts
->resp
== SAS_TASK_UNDELIVERED
) {
76 } else { /* ts->resp == SAS_TASK_COMPLETE */
77 /* task delivered, what happened afterwards? */
79 case SAS_DEV_NO_RESPONSE
:
86 case SAS_DATA_UNDERRUN
:
87 scsi_set_resid(sc
, ts
->residual
);
88 if (scsi_bufflen(sc
) - scsi_get_resid(sc
) < sc
->underflow
)
91 case SAS_DATA_OVERRUN
:
95 hs
= DID_SOFT_ERROR
; /* retry */
97 case SAS_DEVICE_UNKNOWN
:
103 case SAS_OPEN_REJECT
:
104 if (ts
->open_rej_reason
== SAS_OREJ_RSVD_RETRY
)
105 hs
= DID_SOFT_ERROR
; /* retry */
109 case SAS_PROTO_RESPONSE
:
110 SAS_DPRINTK("LLDD:%s sent SAS_PROTO_RESP for an SSP "
111 "task; please report this\n",
112 task
->dev
->port
->ha
->sas_ha_name
);
114 case SAS_ABORTED_TASK
:
117 case SAM_STAT_CHECK_CONDITION
:
118 memcpy(sc
->sense_buffer
, ts
->buf
,
119 min(SCSI_SENSE_BUFFERSIZE
, ts
->buf_valid_size
));
120 stat
= SAM_STAT_CHECK_CONDITION
;
127 ASSIGN_SAS_TASK(sc
, NULL
);
128 sc
->result
= (hs
<< 16) | stat
;
129 list_del_init(&task
->list
);
134 static struct sas_task
*sas_create_task(struct scsi_cmnd
*cmd
,
135 struct domain_device
*dev
,
138 struct sas_task
*task
= sas_alloc_task(gfp_flags
);
144 task
->uldd_task
= cmd
;
145 ASSIGN_SAS_TASK(cmd
, task
);
148 task
->task_proto
= task
->dev
->tproto
; /* BUG_ON(!SSP) */
150 task
->ssp_task
.retry_count
= 1;
151 int_to_scsilun(cmd
->device
->lun
, &lun
);
152 memcpy(task
->ssp_task
.LUN
, &lun
.scsi_lun
, 8);
153 task
->ssp_task
.task_attr
= TASK_ATTR_SIMPLE
;
154 memcpy(task
->ssp_task
.cdb
, cmd
->cmnd
, 16);
156 task
->scatter
= scsi_sglist(cmd
);
157 task
->num_scatter
= scsi_sg_count(cmd
);
158 task
->total_xfer_len
= scsi_bufflen(cmd
);
159 task
->data_dir
= cmd
->sc_data_direction
;
161 task
->task_done
= sas_scsi_task_done
;
166 int sas_queue_up(struct sas_task
*task
)
168 struct sas_ha_struct
*sas_ha
= task
->dev
->port
->ha
;
169 struct scsi_core
*core
= &sas_ha
->core
;
173 spin_lock_irqsave(&core
->task_queue_lock
, flags
);
174 if (sas_ha
->lldd_queue_size
< core
->task_queue_size
+ 1) {
175 spin_unlock_irqrestore(&core
->task_queue_lock
, flags
);
176 return -SAS_QUEUE_FULL
;
178 list_add_tail(&task
->list
, &core
->task_queue
);
179 core
->task_queue_size
+= 1;
180 spin_unlock_irqrestore(&core
->task_queue_lock
, flags
);
181 wake_up_process(core
->queue_thread
);
186 int sas_queuecommand(struct Scsi_Host
*host
, struct scsi_cmnd
*cmd
)
188 struct sas_internal
*i
= to_sas_internal(host
->transportt
);
189 struct domain_device
*dev
= cmd_to_domain_dev(cmd
);
190 struct sas_ha_struct
*sas_ha
= dev
->port
->ha
;
191 struct sas_task
*task
;
194 /* If the device fell off, no sense in issuing commands */
196 cmd
->result
= DID_BAD_TARGET
<< 16;
200 if (dev_is_sata(dev
)) {
203 spin_lock_irqsave(dev
->sata_dev
.ap
->lock
, flags
);
204 res
= ata_sas_queuecmd(cmd
, dev
->sata_dev
.ap
);
205 spin_unlock_irqrestore(dev
->sata_dev
.ap
->lock
, flags
);
209 task
= sas_create_task(cmd
, dev
, GFP_ATOMIC
);
211 return SCSI_MLQUEUE_HOST_BUSY
;
213 /* Queue up, Direct Mode or Task Collector Mode. */
214 if (sas_ha
->lldd_max_execute_num
< 2)
215 res
= i
->dft
->lldd_execute_task(task
, 1, GFP_ATOMIC
);
217 res
= sas_queue_up(task
);
224 SAS_DPRINTK("lldd_execute_task returned: %d\n", res
);
225 ASSIGN_SAS_TASK(cmd
, NULL
);
227 if (res
== -SAS_QUEUE_FULL
)
228 cmd
->result
= DID_SOFT_ERROR
<< 16; /* retry */
230 cmd
->result
= DID_ERROR
<< 16;
236 static void sas_eh_finish_cmd(struct scsi_cmnd
*cmd
)
238 struct sas_task
*task
= TO_SAS_TASK(cmd
);
239 struct sas_ha_struct
*sas_ha
= SHOST_TO_SAS_HA(cmd
->device
->host
);
241 /* remove the aborted task flag to allow the task to be
242 * completed now. At this point, we only get called following
243 * an actual abort of the task, so we should be guaranteed not
244 * to be racing with any completions from the LLD (hence we
245 * don't need the task state lock to clear the flag) */
246 task
->task_state_flags
&= ~SAS_TASK_STATE_ABORTED
;
247 /* Now call task_done. However, task will be free'd after
249 task
->task_done(task
);
250 /* now finish the command and move it on to the error
251 * handler done list, this also takes it off the
252 * error handler pending list */
253 scsi_eh_finish_cmd(cmd
, &sas_ha
->eh_done_q
);
256 static void sas_scsi_clear_queue_lu(struct list_head
*error_q
, struct scsi_cmnd
*my_cmd
)
258 struct scsi_cmnd
*cmd
, *n
;
260 list_for_each_entry_safe(cmd
, n
, error_q
, eh_entry
) {
261 if (cmd
->device
->sdev_target
== my_cmd
->device
->sdev_target
&&
262 cmd
->device
->lun
== my_cmd
->device
->lun
)
263 sas_eh_finish_cmd(cmd
);
267 static void sas_scsi_clear_queue_I_T(struct list_head
*error_q
,
268 struct domain_device
*dev
)
270 struct scsi_cmnd
*cmd
, *n
;
272 list_for_each_entry_safe(cmd
, n
, error_q
, eh_entry
) {
273 struct domain_device
*x
= cmd_to_domain_dev(cmd
);
276 sas_eh_finish_cmd(cmd
);
280 static void sas_scsi_clear_queue_port(struct list_head
*error_q
,
281 struct asd_sas_port
*port
)
283 struct scsi_cmnd
*cmd
, *n
;
285 list_for_each_entry_safe(cmd
, n
, error_q
, eh_entry
) {
286 struct domain_device
*dev
= cmd_to_domain_dev(cmd
);
287 struct asd_sas_port
*x
= dev
->port
;
290 sas_eh_finish_cmd(cmd
);
294 enum task_disposition
{
302 static enum task_disposition
sas_scsi_find_task(struct sas_task
*task
)
304 struct sas_ha_struct
*ha
= task
->dev
->port
->ha
;
307 struct sas_internal
*si
=
308 to_sas_internal(task
->dev
->port
->ha
->core
.shost
->transportt
);
310 if (ha
->lldd_max_execute_num
> 1) {
311 struct scsi_core
*core
= &ha
->core
;
312 struct sas_task
*t
, *n
;
314 spin_lock_irqsave(&core
->task_queue_lock
, flags
);
315 list_for_each_entry_safe(t
, n
, &core
->task_queue
, list
) {
317 list_del_init(&t
->list
);
318 spin_unlock_irqrestore(&core
->task_queue_lock
,
320 SAS_DPRINTK("%s: task 0x%p aborted from "
323 return TASK_IS_ABORTED
;
326 spin_unlock_irqrestore(&core
->task_queue_lock
, flags
);
329 for (i
= 0; i
< 5; i
++) {
330 SAS_DPRINTK("%s: aborting task 0x%p\n", __func__
, task
);
331 res
= si
->dft
->lldd_abort_task(task
);
333 spin_lock_irqsave(&task
->task_state_lock
, flags
);
334 if (task
->task_state_flags
& SAS_TASK_STATE_DONE
) {
335 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
336 SAS_DPRINTK("%s: task 0x%p is done\n", __func__
,
340 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
342 if (res
== TMF_RESP_FUNC_COMPLETE
) {
343 SAS_DPRINTK("%s: task 0x%p is aborted\n",
345 return TASK_IS_ABORTED
;
346 } else if (si
->dft
->lldd_query_task
) {
347 SAS_DPRINTK("%s: querying task 0x%p\n",
349 res
= si
->dft
->lldd_query_task(task
);
351 case TMF_RESP_FUNC_SUCC
:
352 SAS_DPRINTK("%s: task 0x%p at LU\n",
354 return TASK_IS_AT_LU
;
355 case TMF_RESP_FUNC_COMPLETE
:
356 SAS_DPRINTK("%s: task 0x%p not at LU\n",
358 return TASK_IS_NOT_AT_LU
;
359 case TMF_RESP_FUNC_FAILED
:
360 SAS_DPRINTK("%s: task 0x%p failed to abort\n",
362 return TASK_ABORT_FAILED
;
370 static int sas_recover_lu(struct domain_device
*dev
, struct scsi_cmnd
*cmd
)
372 int res
= TMF_RESP_FUNC_FAILED
;
374 struct sas_internal
*i
=
375 to_sas_internal(dev
->port
->ha
->core
.shost
->transportt
);
377 int_to_scsilun(cmd
->device
->lun
, &lun
);
379 SAS_DPRINTK("eh: device %llx LUN %x has the task\n",
380 SAS_ADDR(dev
->sas_addr
),
383 if (i
->dft
->lldd_abort_task_set
)
384 res
= i
->dft
->lldd_abort_task_set(dev
, lun
.scsi_lun
);
386 if (res
== TMF_RESP_FUNC_FAILED
) {
387 if (i
->dft
->lldd_clear_task_set
)
388 res
= i
->dft
->lldd_clear_task_set(dev
, lun
.scsi_lun
);
391 if (res
== TMF_RESP_FUNC_FAILED
) {
392 if (i
->dft
->lldd_lu_reset
)
393 res
= i
->dft
->lldd_lu_reset(dev
, lun
.scsi_lun
);
399 static int sas_recover_I_T(struct domain_device
*dev
)
401 int res
= TMF_RESP_FUNC_FAILED
;
402 struct sas_internal
*i
=
403 to_sas_internal(dev
->port
->ha
->core
.shost
->transportt
);
405 SAS_DPRINTK("I_T nexus reset for dev %016llx\n",
406 SAS_ADDR(dev
->sas_addr
));
408 if (i
->dft
->lldd_I_T_nexus_reset
)
409 res
= i
->dft
->lldd_I_T_nexus_reset(dev
);
414 /* Find the sas_phy that's attached to this device */
415 struct sas_phy
*sas_find_local_phy(struct domain_device
*dev
)
417 struct domain_device
*pdev
= dev
->parent
;
418 struct ex_phy
*exphy
= NULL
;
421 /* Directly attached device */
423 return dev
->port
->phy
;
425 /* Otherwise look in the expander */
426 for (i
= 0; i
< pdev
->ex_dev
.num_phys
; i
++)
427 if (!memcmp(dev
->sas_addr
,
428 pdev
->ex_dev
.ex_phy
[i
].attached_sas_addr
,
430 exphy
= &pdev
->ex_dev
.ex_phy
[i
];
437 EXPORT_SYMBOL_GPL(sas_find_local_phy
);
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
= sas_find_local_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_cmnd
*cmd
)
482 struct Scsi_Host
*shost
= cmd
->device
->host
;
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
);
523 SAS_DPRINTK("%s: task 0x%p requests reset\n",
528 SAS_DPRINTK("trying to find task 0x%p\n", task
);
529 res
= sas_scsi_find_task(task
);
535 SAS_DPRINTK("%s: task 0x%p is done\n", __func__
,
537 sas_eh_finish_cmd(cmd
);
539 case TASK_IS_ABORTED
:
540 SAS_DPRINTK("%s: task 0x%p is aborted\n",
542 sas_eh_finish_cmd(cmd
);
545 SAS_DPRINTK("task 0x%p is at LU: lu recover\n", task
);
547 tmf_resp
= sas_recover_lu(task
->dev
, cmd
);
548 if (tmf_resp
== TMF_RESP_FUNC_COMPLETE
) {
549 SAS_DPRINTK("dev %016llx LU %x is "
553 sas_eh_finish_cmd(cmd
);
554 sas_scsi_clear_queue_lu(work_q
, cmd
);
558 case TASK_IS_NOT_AT_LU
:
559 case TASK_ABORT_FAILED
:
560 SAS_DPRINTK("task 0x%p is not at LU: I_T recover\n",
562 tmf_resp
= sas_recover_I_T(task
->dev
);
563 if (tmf_resp
== TMF_RESP_FUNC_COMPLETE
) {
564 struct domain_device
*dev
= task
->dev
;
565 SAS_DPRINTK("I_T %016llx recovered\n",
566 SAS_ADDR(task
->dev
->sas_addr
));
567 sas_eh_finish_cmd(cmd
);
568 sas_scsi_clear_queue_I_T(work_q
, dev
);
571 /* Hammer time :-) */
572 try_to_reset_cmd_device(cmd
);
573 if (i
->dft
->lldd_clear_nexus_port
) {
574 struct asd_sas_port
*port
= task
->dev
->port
;
575 SAS_DPRINTK("clearing nexus for port:%d\n",
577 res
= i
->dft
->lldd_clear_nexus_port(port
);
578 if (res
== TMF_RESP_FUNC_COMPLETE
) {
579 SAS_DPRINTK("clear nexus port:%d "
580 "succeeded\n", port
->id
);
581 sas_eh_finish_cmd(cmd
);
582 sas_scsi_clear_queue_port(work_q
,
587 if (i
->dft
->lldd_clear_nexus_ha
) {
588 SAS_DPRINTK("clear nexus ha\n");
589 res
= i
->dft
->lldd_clear_nexus_ha(ha
);
590 if (res
== TMF_RESP_FUNC_COMPLETE
) {
591 SAS_DPRINTK("clear nexus ha "
593 sas_eh_finish_cmd(cmd
);
597 /* If we are here -- this means that no amount
598 * of effort could recover from errors. Quite
599 * possibly the HA just disappeared.
601 SAS_DPRINTK("error from device %llx, LUN %x "
602 "couldn't be recovered in any way\n",
603 SAS_ADDR(task
->dev
->sas_addr
),
606 sas_eh_finish_cmd(cmd
);
610 return list_empty(work_q
);
612 SAS_DPRINTK("--- Exit %s -- clear_q\n", __func__
);
613 list_for_each_entry_safe(cmd
, n
, work_q
, eh_entry
)
614 sas_eh_finish_cmd(cmd
);
616 return list_empty(work_q
);
619 void sas_scsi_recover_host(struct Scsi_Host
*shost
)
621 struct sas_ha_struct
*ha
= SHOST_TO_SAS_HA(shost
);
623 LIST_HEAD(eh_work_q
);
625 spin_lock_irqsave(shost
->host_lock
, flags
);
626 list_splice_init(&shost
->eh_cmd_q
, &eh_work_q
);
627 shost
->host_eh_scheduled
= 0;
628 spin_unlock_irqrestore(shost
->host_lock
, flags
);
630 SAS_DPRINTK("Enter %s\n", __func__
);
632 * Deal with commands that still have SAS tasks (i.e. they didn't
633 * complete via the normal sas_task completion mechanism)
635 if (sas_eh_handle_sas_errors(shost
, &eh_work_q
, &ha
->eh_done_q
))
639 * Now deal with SCSI commands that completed ok but have a an error
640 * code (and hopefully sense data) attached. This is roughly what
641 * scsi_unjam_host does, but we skip scsi_eh_abort_cmds because any
642 * command we see here has no sas_task and is thus unknown to the HA.
644 if (!sas_ata_eh(shost
, &eh_work_q
, &ha
->eh_done_q
))
645 if (!scsi_eh_get_sense(&eh_work_q
, &ha
->eh_done_q
))
646 scsi_eh_ready_devs(shost
, &eh_work_q
, &ha
->eh_done_q
);
649 /* now link into libata eh --- if we have any ata devices */
650 sas_ata_strategy_handler(shost
);
652 scsi_eh_flush_done_q(&ha
->eh_done_q
);
654 SAS_DPRINTK("--- Exit %s\n", __func__
);
658 enum blk_eh_timer_return
sas_scsi_timed_out(struct scsi_cmnd
*cmd
)
660 struct sas_task
*task
= TO_SAS_TASK(cmd
);
662 enum blk_eh_timer_return rtn
;
664 if (sas_ata_timed_out(cmd
, task
, &rtn
))
668 cmd
->request
->timeout
/= 2;
669 SAS_DPRINTK("command 0x%p, task 0x%p, gone: %s\n",
670 cmd
, task
, (cmd
->request
->timeout
?
671 "BLK_EH_RESET_TIMER" : "BLK_EH_NOT_HANDLED"));
672 if (!cmd
->request
->timeout
)
673 return BLK_EH_NOT_HANDLED
;
674 return BLK_EH_RESET_TIMER
;
677 spin_lock_irqsave(&task
->task_state_lock
, flags
);
678 BUG_ON(task
->task_state_flags
& SAS_TASK_STATE_ABORTED
);
679 if (task
->task_state_flags
& SAS_TASK_STATE_DONE
) {
680 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
681 SAS_DPRINTK("command 0x%p, task 0x%p, timed out: "
682 "BLK_EH_HANDLED\n", cmd
, task
);
683 return BLK_EH_HANDLED
;
685 if (!(task
->task_state_flags
& SAS_TASK_AT_INITIATOR
)) {
686 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
687 SAS_DPRINTK("command 0x%p, task 0x%p, not at initiator: "
688 "BLK_EH_RESET_TIMER\n",
690 return BLK_EH_RESET_TIMER
;
692 task
->task_state_flags
|= SAS_TASK_STATE_ABORTED
;
693 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
695 SAS_DPRINTK("command 0x%p, task 0x%p, timed out: BLK_EH_NOT_HANDLED\n",
698 return BLK_EH_NOT_HANDLED
;
701 int sas_ioctl(struct scsi_device
*sdev
, int cmd
, void __user
*arg
)
703 struct domain_device
*dev
= sdev_to_domain_dev(sdev
);
705 if (dev_is_sata(dev
))
706 return ata_sas_scsi_ioctl(dev
->sata_dev
.ap
, sdev
, cmd
, arg
);
711 struct domain_device
*sas_find_dev_by_rphy(struct sas_rphy
*rphy
)
713 struct Scsi_Host
*shost
= dev_to_shost(rphy
->dev
.parent
);
714 struct sas_ha_struct
*ha
= SHOST_TO_SAS_HA(shost
);
715 struct domain_device
*found_dev
= NULL
;
719 spin_lock_irqsave(&ha
->phy_port_lock
, flags
);
720 for (i
= 0; i
< ha
->num_phys
; i
++) {
721 struct asd_sas_port
*port
= ha
->sas_port
[i
];
722 struct domain_device
*dev
;
724 spin_lock(&port
->dev_list_lock
);
725 list_for_each_entry(dev
, &port
->dev_list
, dev_list_node
) {
726 if (rphy
== dev
->rphy
) {
728 spin_unlock(&port
->dev_list_lock
);
732 spin_unlock(&port
->dev_list_lock
);
735 spin_unlock_irqrestore(&ha
->phy_port_lock
, flags
);
740 static inline struct domain_device
*sas_find_target(struct scsi_target
*starget
)
742 struct sas_rphy
*rphy
= dev_to_rphy(starget
->dev
.parent
);
744 return sas_find_dev_by_rphy(rphy
);
747 int sas_target_alloc(struct scsi_target
*starget
)
749 struct domain_device
*found_dev
= sas_find_target(starget
);
755 if (dev_is_sata(found_dev
)) {
756 res
= sas_ata_init_host_and_port(found_dev
, starget
);
761 starget
->hostdata
= found_dev
;
765 #define SAS_DEF_QD 256
767 int sas_slave_configure(struct scsi_device
*scsi_dev
)
769 struct domain_device
*dev
= sdev_to_domain_dev(scsi_dev
);
770 struct sas_ha_struct
*sas_ha
;
772 BUG_ON(dev
->rphy
->identify
.device_type
!= SAS_END_DEVICE
);
774 if (dev_is_sata(dev
)) {
775 ata_sas_slave_configure(scsi_dev
, dev
->sata_dev
.ap
);
779 sas_ha
= dev
->port
->ha
;
781 sas_read_port_mode_page(scsi_dev
);
783 if (scsi_dev
->tagged_supported
) {
784 scsi_set_tag_type(scsi_dev
, MSG_SIMPLE_TAG
);
785 scsi_activate_tcq(scsi_dev
, SAS_DEF_QD
);
787 SAS_DPRINTK("device %llx, LUN %x doesn't support "
788 "TCQ\n", SAS_ADDR(dev
->sas_addr
),
790 scsi_dev
->tagged_supported
= 0;
791 scsi_set_tag_type(scsi_dev
, 0);
792 scsi_deactivate_tcq(scsi_dev
, 1);
795 scsi_dev
->allow_restart
= 1;
800 void sas_slave_destroy(struct scsi_device
*scsi_dev
)
802 struct domain_device
*dev
= sdev_to_domain_dev(scsi_dev
);
804 if (dev_is_sata(dev
))
805 sas_to_ata_dev(dev
)->class = ATA_DEV_NONE
;
808 int sas_change_queue_depth(struct scsi_device
*sdev
, int depth
, int reason
)
810 struct domain_device
*dev
= sdev_to_domain_dev(sdev
);
812 if (dev_is_sata(dev
))
813 return __ata_change_queue_depth(dev
->sata_dev
.ap
, sdev
, depth
,
817 case SCSI_QDEPTH_DEFAULT
:
818 case SCSI_QDEPTH_RAMP_UP
:
819 if (!sdev
->tagged_supported
)
821 scsi_adjust_queue_depth(sdev
, scsi_get_tag_type(sdev
), depth
);
823 case SCSI_QDEPTH_QFULL
:
824 scsi_track_queue_full(sdev
, depth
);
833 int sas_change_queue_type(struct scsi_device
*scsi_dev
, int qt
)
835 struct domain_device
*dev
= sdev_to_domain_dev(scsi_dev
);
837 if (dev_is_sata(dev
))
840 if (!scsi_dev
->tagged_supported
)
843 scsi_deactivate_tcq(scsi_dev
, 1);
845 scsi_set_tag_type(scsi_dev
, qt
);
846 scsi_activate_tcq(scsi_dev
, scsi_dev
->queue_depth
);
851 int sas_bios_param(struct scsi_device
*scsi_dev
,
852 struct block_device
*bdev
,
853 sector_t capacity
, int *hsc
)
857 sector_div(capacity
, 255*63);
863 /* ---------- Task Collector Thread implementation ---------- */
865 static void sas_queue(struct sas_ha_struct
*sas_ha
)
867 struct scsi_core
*core
= &sas_ha
->core
;
872 struct sas_internal
*i
= to_sas_internal(core
->shost
->transportt
);
874 spin_lock_irqsave(&core
->task_queue_lock
, flags
);
875 while (!kthread_should_stop() &&
876 !list_empty(&core
->task_queue
)) {
878 can_queue
= sas_ha
->lldd_queue_size
- core
->task_queue_size
;
879 if (can_queue
>= 0) {
880 can_queue
= core
->task_queue_size
;
881 list_splice_init(&core
->task_queue
, &q
);
883 struct list_head
*a
, *n
;
885 can_queue
= sas_ha
->lldd_queue_size
;
886 list_for_each_safe(a
, n
, &core
->task_queue
) {
887 list_move_tail(a
, &q
);
888 if (--can_queue
== 0)
891 can_queue
= sas_ha
->lldd_queue_size
;
893 core
->task_queue_size
-= can_queue
;
894 spin_unlock_irqrestore(&core
->task_queue_lock
, flags
);
896 struct sas_task
*task
= list_entry(q
.next
,
900 res
= i
->dft
->lldd_execute_task(task
, can_queue
,
903 __list_add(&q
, task
->list
.prev
, &task
->list
);
905 spin_lock_irqsave(&core
->task_queue_lock
, flags
);
907 list_splice_init(&q
, &core
->task_queue
); /*at head*/
908 core
->task_queue_size
+= can_queue
;
911 spin_unlock_irqrestore(&core
->task_queue_lock
, flags
);
915 * sas_queue_thread -- The Task Collector thread
916 * @_sas_ha: pointer to struct sas_ha
918 static int sas_queue_thread(void *_sas_ha
)
920 struct sas_ha_struct
*sas_ha
= _sas_ha
;
923 set_current_state(TASK_INTERRUPTIBLE
);
926 if (kthread_should_stop())
933 int sas_init_queue(struct sas_ha_struct
*sas_ha
)
935 struct scsi_core
*core
= &sas_ha
->core
;
937 spin_lock_init(&core
->task_queue_lock
);
938 core
->task_queue_size
= 0;
939 INIT_LIST_HEAD(&core
->task_queue
);
941 core
->queue_thread
= kthread_run(sas_queue_thread
, sas_ha
,
942 "sas_queue_%d", core
->shost
->host_no
);
943 if (IS_ERR(core
->queue_thread
))
944 return PTR_ERR(core
->queue_thread
);
948 void sas_shutdown_queue(struct sas_ha_struct
*sas_ha
)
951 struct scsi_core
*core
= &sas_ha
->core
;
952 struct sas_task
*task
, *n
;
954 kthread_stop(core
->queue_thread
);
956 if (!list_empty(&core
->task_queue
))
957 SAS_DPRINTK("HA: %llx: scsi core task queue is NOT empty!?\n",
958 SAS_ADDR(sas_ha
->sas_addr
));
960 spin_lock_irqsave(&core
->task_queue_lock
, flags
);
961 list_for_each_entry_safe(task
, n
, &core
->task_queue
, list
) {
962 struct scsi_cmnd
*cmd
= task
->uldd_task
;
964 list_del_init(&task
->list
);
966 ASSIGN_SAS_TASK(cmd
, NULL
);
968 cmd
->result
= DID_ABORT
<< 16;
971 spin_unlock_irqrestore(&core
->task_queue_lock
, flags
);
975 * Call the LLDD task abort routine directly. This function is intended for
976 * use by upper layers that need to tell the LLDD to abort a task.
978 int __sas_task_abort(struct sas_task
*task
)
980 struct sas_internal
*si
=
981 to_sas_internal(task
->dev
->port
->ha
->core
.shost
->transportt
);
985 spin_lock_irqsave(&task
->task_state_lock
, flags
);
986 if (task
->task_state_flags
& SAS_TASK_STATE_ABORTED
||
987 task
->task_state_flags
& SAS_TASK_STATE_DONE
) {
988 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
989 SAS_DPRINTK("%s: Task %p already finished.\n", __func__
,
993 task
->task_state_flags
|= SAS_TASK_STATE_ABORTED
;
994 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
996 if (!si
->dft
->lldd_abort_task
)
999 res
= si
->dft
->lldd_abort_task(task
);
1001 spin_lock_irqsave(&task
->task_state_lock
, flags
);
1002 if ((task
->task_state_flags
& SAS_TASK_STATE_DONE
) ||
1003 (res
== TMF_RESP_FUNC_COMPLETE
))
1005 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
1006 task
->task_done(task
);
1010 if (!(task
->task_state_flags
& SAS_TASK_STATE_DONE
))
1011 task
->task_state_flags
&= ~SAS_TASK_STATE_ABORTED
;
1012 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
1018 * Tell an upper layer that it needs to initiate an abort for a given task.
1019 * This should only ever be called by an LLDD.
1021 void sas_task_abort(struct sas_task
*task
)
1023 struct scsi_cmnd
*sc
= task
->uldd_task
;
1025 /* Escape for libsas internal commands */
1027 if (!del_timer(&task
->timer
))
1029 task
->timer
.function(task
->timer
.data
);
1033 if (dev_is_sata(task
->dev
)) {
1034 sas_ata_task_abort(task
);
1036 struct request_queue
*q
= sc
->device
->request_queue
;
1037 unsigned long flags
;
1039 spin_lock_irqsave(q
->queue_lock
, flags
);
1040 blk_abort_request(sc
->request
);
1041 spin_unlock_irqrestore(q
->queue_lock
, flags
);
1042 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
);