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 32
766 #define SAS_MAX_QD 64
768 int sas_slave_configure(struct scsi_device
*scsi_dev
)
770 struct domain_device
*dev
= sdev_to_domain_dev(scsi_dev
);
771 struct sas_ha_struct
*sas_ha
;
773 BUG_ON(dev
->rphy
->identify
.device_type
!= SAS_END_DEVICE
);
775 if (dev_is_sata(dev
)) {
776 ata_sas_slave_configure(scsi_dev
, dev
->sata_dev
.ap
);
780 sas_ha
= dev
->port
->ha
;
782 sas_read_port_mode_page(scsi_dev
);
784 if (scsi_dev
->tagged_supported
) {
785 scsi_set_tag_type(scsi_dev
, MSG_SIMPLE_TAG
);
786 scsi_activate_tcq(scsi_dev
, SAS_DEF_QD
);
788 SAS_DPRINTK("device %llx, LUN %x doesn't support "
789 "TCQ\n", SAS_ADDR(dev
->sas_addr
),
791 scsi_dev
->tagged_supported
= 0;
792 scsi_set_tag_type(scsi_dev
, 0);
793 scsi_deactivate_tcq(scsi_dev
, 1);
796 scsi_dev
->allow_restart
= 1;
801 void sas_slave_destroy(struct scsi_device
*scsi_dev
)
803 struct domain_device
*dev
= sdev_to_domain_dev(scsi_dev
);
805 if (dev_is_sata(dev
))
806 dev
->sata_dev
.ap
->link
.device
[0].class = ATA_DEV_NONE
;
809 int sas_change_queue_depth(struct scsi_device
*scsi_dev
, int new_depth
,
812 int res
= min(new_depth
, SAS_MAX_QD
);
814 if (reason
!= SCSI_QDEPTH_DEFAULT
)
817 if (scsi_dev
->tagged_supported
)
818 scsi_adjust_queue_depth(scsi_dev
, scsi_get_tag_type(scsi_dev
),
821 struct domain_device
*dev
= sdev_to_domain_dev(scsi_dev
);
822 sas_printk("device %llx LUN %x queue depth changed to 1\n",
823 SAS_ADDR(dev
->sas_addr
),
825 scsi_adjust_queue_depth(scsi_dev
, 0, 1);
832 int sas_change_queue_type(struct scsi_device
*scsi_dev
, int qt
)
834 if (!scsi_dev
->tagged_supported
)
837 scsi_deactivate_tcq(scsi_dev
, 1);
839 scsi_set_tag_type(scsi_dev
, qt
);
840 scsi_activate_tcq(scsi_dev
, scsi_dev
->queue_depth
);
845 int sas_bios_param(struct scsi_device
*scsi_dev
,
846 struct block_device
*bdev
,
847 sector_t capacity
, int *hsc
)
851 sector_div(capacity
, 255*63);
857 /* ---------- Task Collector Thread implementation ---------- */
859 static void sas_queue(struct sas_ha_struct
*sas_ha
)
861 struct scsi_core
*core
= &sas_ha
->core
;
866 struct sas_internal
*i
= to_sas_internal(core
->shost
->transportt
);
868 spin_lock_irqsave(&core
->task_queue_lock
, flags
);
869 while (!kthread_should_stop() &&
870 !list_empty(&core
->task_queue
)) {
872 can_queue
= sas_ha
->lldd_queue_size
- core
->task_queue_size
;
873 if (can_queue
>= 0) {
874 can_queue
= core
->task_queue_size
;
875 list_splice_init(&core
->task_queue
, &q
);
877 struct list_head
*a
, *n
;
879 can_queue
= sas_ha
->lldd_queue_size
;
880 list_for_each_safe(a
, n
, &core
->task_queue
) {
881 list_move_tail(a
, &q
);
882 if (--can_queue
== 0)
885 can_queue
= sas_ha
->lldd_queue_size
;
887 core
->task_queue_size
-= can_queue
;
888 spin_unlock_irqrestore(&core
->task_queue_lock
, flags
);
890 struct sas_task
*task
= list_entry(q
.next
,
894 res
= i
->dft
->lldd_execute_task(task
, can_queue
,
897 __list_add(&q
, task
->list
.prev
, &task
->list
);
899 spin_lock_irqsave(&core
->task_queue_lock
, flags
);
901 list_splice_init(&q
, &core
->task_queue
); /*at head*/
902 core
->task_queue_size
+= can_queue
;
905 spin_unlock_irqrestore(&core
->task_queue_lock
, flags
);
909 * sas_queue_thread -- The Task Collector thread
910 * @_sas_ha: pointer to struct sas_ha
912 static int sas_queue_thread(void *_sas_ha
)
914 struct sas_ha_struct
*sas_ha
= _sas_ha
;
917 set_current_state(TASK_INTERRUPTIBLE
);
920 if (kthread_should_stop())
927 int sas_init_queue(struct sas_ha_struct
*sas_ha
)
929 struct scsi_core
*core
= &sas_ha
->core
;
931 spin_lock_init(&core
->task_queue_lock
);
932 core
->task_queue_size
= 0;
933 INIT_LIST_HEAD(&core
->task_queue
);
935 core
->queue_thread
= kthread_run(sas_queue_thread
, sas_ha
,
936 "sas_queue_%d", core
->shost
->host_no
);
937 if (IS_ERR(core
->queue_thread
))
938 return PTR_ERR(core
->queue_thread
);
942 void sas_shutdown_queue(struct sas_ha_struct
*sas_ha
)
945 struct scsi_core
*core
= &sas_ha
->core
;
946 struct sas_task
*task
, *n
;
948 kthread_stop(core
->queue_thread
);
950 if (!list_empty(&core
->task_queue
))
951 SAS_DPRINTK("HA: %llx: scsi core task queue is NOT empty!?\n",
952 SAS_ADDR(sas_ha
->sas_addr
));
954 spin_lock_irqsave(&core
->task_queue_lock
, flags
);
955 list_for_each_entry_safe(task
, n
, &core
->task_queue
, list
) {
956 struct scsi_cmnd
*cmd
= task
->uldd_task
;
958 list_del_init(&task
->list
);
960 ASSIGN_SAS_TASK(cmd
, NULL
);
962 cmd
->result
= DID_ABORT
<< 16;
965 spin_unlock_irqrestore(&core
->task_queue_lock
, flags
);
969 * Call the LLDD task abort routine directly. This function is intended for
970 * use by upper layers that need to tell the LLDD to abort a task.
972 int __sas_task_abort(struct sas_task
*task
)
974 struct sas_internal
*si
=
975 to_sas_internal(task
->dev
->port
->ha
->core
.shost
->transportt
);
979 spin_lock_irqsave(&task
->task_state_lock
, flags
);
980 if (task
->task_state_flags
& SAS_TASK_STATE_ABORTED
||
981 task
->task_state_flags
& SAS_TASK_STATE_DONE
) {
982 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
983 SAS_DPRINTK("%s: Task %p already finished.\n", __func__
,
987 task
->task_state_flags
|= SAS_TASK_STATE_ABORTED
;
988 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
990 if (!si
->dft
->lldd_abort_task
)
993 res
= si
->dft
->lldd_abort_task(task
);
995 spin_lock_irqsave(&task
->task_state_lock
, flags
);
996 if ((task
->task_state_flags
& SAS_TASK_STATE_DONE
) ||
997 (res
== TMF_RESP_FUNC_COMPLETE
))
999 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
1000 task
->task_done(task
);
1004 if (!(task
->task_state_flags
& SAS_TASK_STATE_DONE
))
1005 task
->task_state_flags
&= ~SAS_TASK_STATE_ABORTED
;
1006 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
1012 * Tell an upper layer that it needs to initiate an abort for a given task.
1013 * This should only ever be called by an LLDD.
1015 void sas_task_abort(struct sas_task
*task
)
1017 struct scsi_cmnd
*sc
= task
->uldd_task
;
1019 /* Escape for libsas internal commands */
1021 if (!del_timer(&task
->timer
))
1023 task
->timer
.function(task
->timer
.data
);
1027 if (dev_is_sata(task
->dev
)) {
1028 sas_ata_task_abort(task
);
1030 struct request_queue
*q
= sc
->device
->request_queue
;
1031 unsigned long flags
;
1033 spin_lock_irqsave(q
->queue_lock
, flags
);
1034 blk_abort_request(sc
->request
);
1035 spin_unlock_irqrestore(q
->queue_lock
, flags
);
1036 scsi_schedule_eh(sc
->device
->host
);
1040 int sas_slave_alloc(struct scsi_device
*scsi_dev
)
1042 struct domain_device
*dev
= sdev_to_domain_dev(scsi_dev
);
1044 if (dev_is_sata(dev
))
1045 return ata_sas_port_init(dev
->sata_dev
.ap
);
1050 void sas_target_destroy(struct scsi_target
*starget
)
1052 struct domain_device
*found_dev
= sas_find_target(starget
);
1057 if (dev_is_sata(found_dev
))
1058 ata_sas_port_destroy(found_dev
->sata_dev
.ap
);
1063 static void sas_parse_addr(u8
*sas_addr
, const char *p
)
1066 for (i
= 0; i
< SAS_ADDR_SIZE
; i
++) {
1070 h
= isdigit(*p
) ? *p
-'0' : toupper(*p
)-'A'+10;
1072 l
= isdigit(*p
) ? *p
-'0' : toupper(*p
)-'A'+10;
1074 sas_addr
[i
] = (h
<<4) | l
;
1078 #define SAS_STRING_ADDR_SIZE 16
1080 int sas_request_addr(struct Scsi_Host
*shost
, u8
*addr
)
1083 const struct firmware
*fw
;
1085 res
= request_firmware(&fw
, "sas_addr", &shost
->shost_gendev
);
1089 if (fw
->size
< SAS_STRING_ADDR_SIZE
) {
1094 sas_parse_addr(addr
, fw
->data
);
1097 release_firmware(fw
);
1100 EXPORT_SYMBOL_GPL(sas_request_addr
);
1102 EXPORT_SYMBOL_GPL(sas_queuecommand
);
1103 EXPORT_SYMBOL_GPL(sas_target_alloc
);
1104 EXPORT_SYMBOL_GPL(sas_slave_configure
);
1105 EXPORT_SYMBOL_GPL(sas_slave_destroy
);
1106 EXPORT_SYMBOL_GPL(sas_change_queue_depth
);
1107 EXPORT_SYMBOL_GPL(sas_change_queue_type
);
1108 EXPORT_SYMBOL_GPL(sas_bios_param
);
1109 EXPORT_SYMBOL_GPL(__sas_task_abort
);
1110 EXPORT_SYMBOL_GPL(sas_task_abort
);
1111 EXPORT_SYMBOL_GPL(sas_phy_reset
);
1112 EXPORT_SYMBOL_GPL(sas_phy_enable
);
1113 EXPORT_SYMBOL_GPL(sas_eh_device_reset_handler
);
1114 EXPORT_SYMBOL_GPL(sas_eh_bus_reset_handler
);
1115 EXPORT_SYMBOL_GPL(sas_slave_alloc
);
1116 EXPORT_SYMBOL_GPL(sas_target_destroy
);
1117 EXPORT_SYMBOL_GPL(sas_ioctl
);