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 "sas_internal.h"
28 #include <scsi/scsi_host.h>
29 #include <scsi/scsi_device.h>
30 #include <scsi/scsi_tcq.h>
31 #include <scsi/scsi.h>
32 #include <scsi/scsi_eh.h>
33 #include <scsi/scsi_transport.h>
34 #include <scsi/scsi_transport_sas.h>
35 #include "../scsi_sas_internal.h"
36 #include "../scsi_transport_api.h"
37 #include "../scsi_priv.h"
39 #include <linux/err.h>
40 #include <linux/blkdev.h>
41 #include <linux/scatterlist.h>
43 /* ---------- SCSI Host glue ---------- */
45 #define TO_SAS_TASK(_scsi_cmd) ((void *)(_scsi_cmd)->host_scribble)
46 #define ASSIGN_SAS_TASK(_sc, _t) do { (_sc)->host_scribble = (void *) _t; } while (0)
48 static void sas_scsi_task_done(struct sas_task
*task
)
50 struct task_status_struct
*ts
= &task
->task_status
;
51 struct scsi_cmnd
*sc
= task
->uldd_task
;
52 struct sas_ha_struct
*sas_ha
= SHOST_TO_SAS_HA(sc
->device
->host
);
53 unsigned ts_flags
= task
->task_state_flags
;
57 SAS_DPRINTK("task_done called with non existing SCSI cmnd!\n");
58 list_del_init(&task
->list
);
63 if (ts
->resp
== SAS_TASK_UNDELIVERED
) {
66 } else { /* ts->resp == SAS_TASK_COMPLETE */
67 /* task delivered, what happened afterwards? */
69 case SAS_DEV_NO_RESPONSE
:
76 case SAS_DATA_UNDERRUN
:
77 sc
->resid
= ts
->residual
;
78 if (sc
->request_bufflen
- sc
->resid
< sc
->underflow
)
81 case SAS_DATA_OVERRUN
:
85 hs
= DID_SOFT_ERROR
; /* retry */
87 case SAS_DEVICE_UNKNOWN
:
94 if (ts
->open_rej_reason
== SAS_OREJ_RSVD_RETRY
)
95 hs
= DID_SOFT_ERROR
; /* retry */
99 case SAS_PROTO_RESPONSE
:
100 SAS_DPRINTK("LLDD:%s sent SAS_PROTO_RESP for an SSP "
101 "task; please report this\n",
102 task
->dev
->port
->ha
->sas_ha_name
);
104 case SAS_ABORTED_TASK
:
108 memcpy(sc
->sense_buffer
, ts
->buf
,
109 max(SCSI_SENSE_BUFFERSIZE
, ts
->buf_valid_size
));
110 stat
= SAM_CHECK_COND
;
117 ASSIGN_SAS_TASK(sc
, NULL
);
118 sc
->result
= (hs
<< 16) | stat
;
119 list_del_init(&task
->list
);
121 /* This is very ugly but this is how SCSI Core works. */
122 if (ts_flags
& SAS_TASK_STATE_ABORTED
)
123 scsi_eh_finish_cmd(sc
, &sas_ha
->eh_done_q
);
128 static enum task_attribute
sas_scsi_get_task_attr(struct scsi_cmnd
*cmd
)
130 enum task_attribute ta
= TASK_ATTR_SIMPLE
;
131 if (cmd
->request
&& blk_rq_tagged(cmd
->request
)) {
132 if (cmd
->device
->ordered_tags
&&
133 (cmd
->request
->cmd_flags
& REQ_HARDBARRIER
))
139 static struct sas_task
*sas_create_task(struct scsi_cmnd
*cmd
,
140 struct domain_device
*dev
,
143 struct sas_task
*task
= sas_alloc_task(gfp_flags
);
149 *(u32
*)cmd
->sense_buffer
= 0;
150 task
->uldd_task
= cmd
;
151 ASSIGN_SAS_TASK(cmd
, task
);
154 task
->task_proto
= task
->dev
->tproto
; /* BUG_ON(!SSP) */
156 task
->ssp_task
.retry_count
= 1;
157 int_to_scsilun(cmd
->device
->lun
, &lun
);
158 memcpy(task
->ssp_task
.LUN
, &lun
.scsi_lun
, 8);
159 task
->ssp_task
.task_attr
= sas_scsi_get_task_attr(cmd
);
160 memcpy(task
->ssp_task
.cdb
, cmd
->cmnd
, 16);
162 task
->scatter
= cmd
->request_buffer
;
163 task
->num_scatter
= cmd
->use_sg
;
164 task
->total_xfer_len
= cmd
->request_bufflen
;
165 task
->data_dir
= cmd
->sc_data_direction
;
167 task
->task_done
= sas_scsi_task_done
;
172 static int sas_queue_up(struct sas_task
*task
)
174 struct sas_ha_struct
*sas_ha
= task
->dev
->port
->ha
;
175 struct scsi_core
*core
= &sas_ha
->core
;
179 spin_lock_irqsave(&core
->task_queue_lock
, flags
);
180 if (sas_ha
->lldd_queue_size
< core
->task_queue_size
+ 1) {
181 spin_unlock_irqrestore(&core
->task_queue_lock
, flags
);
182 return -SAS_QUEUE_FULL
;
184 list_add_tail(&task
->list
, &core
->task_queue
);
185 core
->task_queue_size
+= 1;
186 spin_unlock_irqrestore(&core
->task_queue_lock
, flags
);
187 up(&core
->queue_thread_sema
);
193 * sas_queuecommand -- Enqueue a command for processing
194 * @parameters: See SCSI Core documentation
196 * Note: XXX: Remove the host unlock/lock pair when SCSI Core can
197 * call us without holding an IRQ spinlock...
199 int sas_queuecommand(struct scsi_cmnd
*cmd
,
200 void (*scsi_done
)(struct scsi_cmnd
*))
203 struct domain_device
*dev
= cmd_to_domain_dev(cmd
);
204 struct Scsi_Host
*host
= cmd
->device
->host
;
205 struct sas_internal
*i
= to_sas_internal(host
->transportt
);
207 spin_unlock_irq(host
->host_lock
);
210 struct sas_ha_struct
*sas_ha
= dev
->port
->ha
;
211 struct sas_task
*task
;
214 task
= sas_create_task(cmd
, dev
, GFP_ATOMIC
);
218 cmd
->scsi_done
= scsi_done
;
219 /* Queue up, Direct Mode or Task Collector Mode. */
220 if (sas_ha
->lldd_max_execute_num
< 2)
221 res
= i
->dft
->lldd_execute_task(task
, 1, GFP_ATOMIC
);
223 res
= sas_queue_up(task
);
227 SAS_DPRINTK("lldd_execute_task returned: %d\n", res
);
228 ASSIGN_SAS_TASK(cmd
, NULL
);
230 if (res
== -SAS_QUEUE_FULL
) {
231 cmd
->result
= DID_SOFT_ERROR
<< 16; /* retry */
239 spin_lock_irq(host
->host_lock
);
243 static void sas_scsi_clear_queue_lu(struct list_head
*error_q
, struct scsi_cmnd
*my_cmd
)
245 struct scsi_cmnd
*cmd
, *n
;
247 list_for_each_entry_safe(cmd
, n
, error_q
, eh_entry
) {
249 list_del_init(&cmd
->eh_entry
);
253 static void sas_scsi_clear_queue_I_T(struct list_head
*error_q
,
254 struct domain_device
*dev
)
256 struct scsi_cmnd
*cmd
, *n
;
258 list_for_each_entry_safe(cmd
, n
, error_q
, eh_entry
) {
259 struct domain_device
*x
= cmd_to_domain_dev(cmd
);
262 list_del_init(&cmd
->eh_entry
);
266 static void sas_scsi_clear_queue_port(struct list_head
*error_q
,
267 struct asd_sas_port
*port
)
269 struct scsi_cmnd
*cmd
, *n
;
271 list_for_each_entry_safe(cmd
, n
, error_q
, eh_entry
) {
272 struct domain_device
*dev
= cmd_to_domain_dev(cmd
);
273 struct asd_sas_port
*x
= dev
->port
;
276 list_del_init(&cmd
->eh_entry
);
280 enum task_disposition
{
288 static enum task_disposition
sas_scsi_find_task(struct sas_task
*task
)
290 struct sas_ha_struct
*ha
= task
->dev
->port
->ha
;
293 struct sas_internal
*si
=
294 to_sas_internal(task
->dev
->port
->ha
->core
.shost
->transportt
);
296 if (ha
->lldd_max_execute_num
> 1) {
297 struct scsi_core
*core
= &ha
->core
;
298 struct sas_task
*t
, *n
;
300 spin_lock_irqsave(&core
->task_queue_lock
, flags
);
301 list_for_each_entry_safe(t
, n
, &core
->task_queue
, list
) {
303 list_del_init(&t
->list
);
304 spin_unlock_irqrestore(&core
->task_queue_lock
,
306 SAS_DPRINTK("%s: task 0x%p aborted from "
309 return TASK_IS_ABORTED
;
312 spin_unlock_irqrestore(&core
->task_queue_lock
, flags
);
315 for (i
= 0; i
< 5; i
++) {
316 SAS_DPRINTK("%s: aborting task 0x%p\n", __FUNCTION__
, task
);
317 res
= si
->dft
->lldd_abort_task(task
);
319 spin_lock_irqsave(&task
->task_state_lock
, flags
);
320 if (task
->task_state_flags
& SAS_TASK_STATE_DONE
) {
321 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
322 SAS_DPRINTK("%s: task 0x%p is done\n", __FUNCTION__
,
326 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
328 if (res
== TMF_RESP_FUNC_COMPLETE
) {
329 SAS_DPRINTK("%s: task 0x%p is aborted\n",
331 return TASK_IS_ABORTED
;
332 } else if (si
->dft
->lldd_query_task
) {
333 SAS_DPRINTK("%s: querying task 0x%p\n",
335 res
= si
->dft
->lldd_query_task(task
);
337 case TMF_RESP_FUNC_SUCC
:
338 SAS_DPRINTK("%s: task 0x%p at LU\n",
340 return TASK_IS_AT_LU
;
341 case TMF_RESP_FUNC_COMPLETE
:
342 SAS_DPRINTK("%s: task 0x%p not at LU\n",
344 return TASK_IS_NOT_AT_LU
;
345 case TMF_RESP_FUNC_FAILED
:
346 SAS_DPRINTK("%s: task 0x%p failed to abort\n",
348 return TASK_ABORT_FAILED
;
356 static int sas_recover_lu(struct domain_device
*dev
, struct scsi_cmnd
*cmd
)
358 int res
= TMF_RESP_FUNC_FAILED
;
360 struct sas_internal
*i
=
361 to_sas_internal(dev
->port
->ha
->core
.shost
->transportt
);
363 int_to_scsilun(cmd
->device
->lun
, &lun
);
365 SAS_DPRINTK("eh: device %llx LUN %x has the task\n",
366 SAS_ADDR(dev
->sas_addr
),
369 if (i
->dft
->lldd_abort_task_set
)
370 res
= i
->dft
->lldd_abort_task_set(dev
, lun
.scsi_lun
);
372 if (res
== TMF_RESP_FUNC_FAILED
) {
373 if (i
->dft
->lldd_clear_task_set
)
374 res
= i
->dft
->lldd_clear_task_set(dev
, lun
.scsi_lun
);
377 if (res
== TMF_RESP_FUNC_FAILED
) {
378 if (i
->dft
->lldd_lu_reset
)
379 res
= i
->dft
->lldd_lu_reset(dev
, lun
.scsi_lun
);
385 static int sas_recover_I_T(struct domain_device
*dev
)
387 int res
= TMF_RESP_FUNC_FAILED
;
388 struct sas_internal
*i
=
389 to_sas_internal(dev
->port
->ha
->core
.shost
->transportt
);
391 SAS_DPRINTK("I_T nexus reset for dev %016llx\n",
392 SAS_ADDR(dev
->sas_addr
));
394 if (i
->dft
->lldd_I_T_nexus_reset
)
395 res
= i
->dft
->lldd_I_T_nexus_reset(dev
);
400 /* Find the sas_phy that's attached to this device */
401 struct sas_phy
*find_local_sas_phy(struct domain_device
*dev
)
403 struct domain_device
*pdev
= dev
->parent
;
404 struct ex_phy
*exphy
= NULL
;
407 /* Directly attached device */
409 return dev
->port
->phy
;
411 /* Otherwise look in the expander */
412 for (i
= 0; i
< pdev
->ex_dev
.num_phys
; i
++)
413 if (!memcmp(dev
->sas_addr
,
414 pdev
->ex_dev
.ex_phy
[i
].attached_sas_addr
,
416 exphy
= &pdev
->ex_dev
.ex_phy
[i
];
424 /* Attempt to send a target reset message to a device */
425 int sas_eh_device_reset_handler(struct scsi_cmnd
*cmd
)
427 struct domain_device
*dev
= cmd_to_domain_dev(cmd
);
428 struct sas_phy
*phy
= find_local_sas_phy(dev
);
431 res
= sas_phy_reset(phy
, 1);
433 SAS_DPRINTK("Device reset of %s failed 0x%x\n",
434 phy
->dev
.kobj
.k_name
,
436 if (res
== TMF_RESP_FUNC_SUCC
|| res
== TMF_RESP_FUNC_COMPLETE
)
442 /* Try to reset a device */
443 static int try_to_reset_cmd_device(struct Scsi_Host
*shost
,
444 struct scsi_cmnd
*cmd
)
446 if (!shost
->hostt
->eh_device_reset_handler
)
449 return shost
->hostt
->eh_device_reset_handler(cmd
);
452 static int sas_eh_handle_sas_errors(struct Scsi_Host
*shost
,
453 struct list_head
*work_q
,
454 struct list_head
*done_q
)
456 struct scsi_cmnd
*cmd
, *n
;
457 enum task_disposition res
= TASK_IS_DONE
;
458 int tmf_resp
, need_reset
;
459 struct sas_internal
*i
= to_sas_internal(shost
->transportt
);
461 struct sas_ha_struct
*ha
= SHOST_TO_SAS_HA(shost
);
464 list_for_each_entry_safe(cmd
, n
, work_q
, eh_entry
) {
465 struct sas_task
*task
= TO_SAS_TASK(cmd
);
470 list_del_init(&cmd
->eh_entry
);
472 spin_lock_irqsave(&task
->task_state_lock
, flags
);
473 need_reset
= task
->task_state_flags
& SAS_TASK_NEED_DEV_RESET
;
474 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
476 SAS_DPRINTK("trying to find task 0x%p\n", task
);
477 res
= sas_scsi_find_task(task
);
483 SAS_DPRINTK("%s: task 0x%p is done\n", __FUNCTION__
,
485 task
->task_done(task
);
487 try_to_reset_cmd_device(shost
, cmd
);
489 case TASK_IS_ABORTED
:
490 SAS_DPRINTK("%s: task 0x%p is aborted\n",
492 task
->task_done(task
);
494 try_to_reset_cmd_device(shost
, cmd
);
497 SAS_DPRINTK("task 0x%p is at LU: lu recover\n", task
);
498 tmf_resp
= sas_recover_lu(task
->dev
, cmd
);
499 if (tmf_resp
== TMF_RESP_FUNC_COMPLETE
) {
500 SAS_DPRINTK("dev %016llx LU %x is "
504 task
->task_done(task
);
506 try_to_reset_cmd_device(shost
, cmd
);
507 sas_scsi_clear_queue_lu(work_q
, cmd
);
511 case TASK_IS_NOT_AT_LU
:
512 case TASK_ABORT_FAILED
:
513 SAS_DPRINTK("task 0x%p is not at LU: I_T recover\n",
515 tmf_resp
= sas_recover_I_T(task
->dev
);
516 if (tmf_resp
== TMF_RESP_FUNC_COMPLETE
) {
517 SAS_DPRINTK("I_T %016llx recovered\n",
518 SAS_ADDR(task
->dev
->sas_addr
));
519 task
->task_done(task
);
521 try_to_reset_cmd_device(shost
, cmd
);
522 sas_scsi_clear_queue_I_T(work_q
, task
->dev
);
525 /* Hammer time :-) */
526 if (i
->dft
->lldd_clear_nexus_port
) {
527 struct asd_sas_port
*port
= task
->dev
->port
;
528 SAS_DPRINTK("clearing nexus for port:%d\n",
530 res
= i
->dft
->lldd_clear_nexus_port(port
);
531 if (res
== TMF_RESP_FUNC_COMPLETE
) {
532 SAS_DPRINTK("clear nexus port:%d "
533 "succeeded\n", port
->id
);
534 task
->task_done(task
);
536 try_to_reset_cmd_device(shost
, cmd
);
537 sas_scsi_clear_queue_port(work_q
,
542 if (i
->dft
->lldd_clear_nexus_ha
) {
543 SAS_DPRINTK("clear nexus ha\n");
544 res
= i
->dft
->lldd_clear_nexus_ha(ha
);
545 if (res
== TMF_RESP_FUNC_COMPLETE
) {
546 SAS_DPRINTK("clear nexus ha "
548 task
->task_done(task
);
550 try_to_reset_cmd_device(shost
, cmd
);
554 /* If we are here -- this means that no amount
555 * of effort could recover from errors. Quite
556 * possibly the HA just disappeared.
558 SAS_DPRINTK("error from device %llx, LUN %x "
559 "couldn't be recovered in any way\n",
560 SAS_ADDR(task
->dev
->sas_addr
),
563 task
->task_done(task
);
565 try_to_reset_cmd_device(shost
, cmd
);
570 return list_empty(work_q
);
572 SAS_DPRINTK("--- Exit %s -- clear_q\n", __FUNCTION__
);
573 list_for_each_entry_safe(cmd
, n
, work_q
, eh_entry
) {
574 struct sas_task
*task
= TO_SAS_TASK(cmd
);
575 list_del_init(&cmd
->eh_entry
);
576 task
->task_done(task
);
578 return list_empty(work_q
);
581 void sas_scsi_recover_host(struct Scsi_Host
*shost
)
583 struct sas_ha_struct
*ha
= SHOST_TO_SAS_HA(shost
);
585 LIST_HEAD(eh_work_q
);
587 spin_lock_irqsave(shost
->host_lock
, flags
);
588 list_splice_init(&shost
->eh_cmd_q
, &eh_work_q
);
589 spin_unlock_irqrestore(shost
->host_lock
, flags
);
591 SAS_DPRINTK("Enter %s\n", __FUNCTION__
);
593 * Deal with commands that still have SAS tasks (i.e. they didn't
594 * complete via the normal sas_task completion mechanism)
596 if (sas_eh_handle_sas_errors(shost
, &eh_work_q
, &ha
->eh_done_q
))
600 * Now deal with SCSI commands that completed ok but have a an error
601 * code (and hopefully sense data) attached. This is roughly what
602 * scsi_unjam_host does, but we skip scsi_eh_abort_cmds because any
603 * command we see here has no sas_task and is thus unknown to the HA.
605 if (!scsi_eh_get_sense(&eh_work_q
, &ha
->eh_done_q
))
606 scsi_eh_ready_devs(shost
, &eh_work_q
, &ha
->eh_done_q
);
609 scsi_eh_flush_done_q(&ha
->eh_done_q
);
610 SAS_DPRINTK("--- Exit %s\n", __FUNCTION__
);
614 enum scsi_eh_timer_return
sas_scsi_timed_out(struct scsi_cmnd
*cmd
)
616 struct sas_task
*task
= TO_SAS_TASK(cmd
);
620 cmd
->timeout_per_command
/= 2;
621 SAS_DPRINTK("command 0x%p, task 0x%p, gone: %s\n",
622 cmd
, task
, (cmd
->timeout_per_command
?
623 "EH_RESET_TIMER" : "EH_NOT_HANDLED"));
624 if (!cmd
->timeout_per_command
)
625 return EH_NOT_HANDLED
;
626 return EH_RESET_TIMER
;
629 spin_lock_irqsave(&task
->task_state_lock
, flags
);
630 BUG_ON(task
->task_state_flags
& SAS_TASK_STATE_ABORTED
);
631 if (task
->task_state_flags
& SAS_TASK_STATE_DONE
) {
632 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
633 SAS_DPRINTK("command 0x%p, task 0x%p, timed out: EH_HANDLED\n",
637 if (!(task
->task_state_flags
& SAS_TASK_AT_INITIATOR
)) {
638 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
639 SAS_DPRINTK("command 0x%p, task 0x%p, not at initiator: "
642 return EH_RESET_TIMER
;
644 task
->task_state_flags
|= SAS_TASK_STATE_ABORTED
;
645 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
647 SAS_DPRINTK("command 0x%p, task 0x%p, timed out: EH_NOT_HANDLED\n",
650 return EH_NOT_HANDLED
;
653 struct domain_device
*sas_find_dev_by_rphy(struct sas_rphy
*rphy
)
655 struct Scsi_Host
*shost
= dev_to_shost(rphy
->dev
.parent
);
656 struct sas_ha_struct
*ha
= SHOST_TO_SAS_HA(shost
);
657 struct domain_device
*found_dev
= NULL
;
661 spin_lock_irqsave(&ha
->phy_port_lock
, flags
);
662 for (i
= 0; i
< ha
->num_phys
; i
++) {
663 struct asd_sas_port
*port
= ha
->sas_port
[i
];
664 struct domain_device
*dev
;
666 spin_lock(&port
->dev_list_lock
);
667 list_for_each_entry(dev
, &port
->dev_list
, dev_list_node
) {
668 if (rphy
== dev
->rphy
) {
670 spin_unlock(&port
->dev_list_lock
);
674 spin_unlock(&port
->dev_list_lock
);
677 spin_unlock_irqrestore(&ha
->phy_port_lock
, flags
);
682 static inline struct domain_device
*sas_find_target(struct scsi_target
*starget
)
684 struct sas_rphy
*rphy
= dev_to_rphy(starget
->dev
.parent
);
686 return sas_find_dev_by_rphy(rphy
);
689 int sas_target_alloc(struct scsi_target
*starget
)
691 struct domain_device
*found_dev
= sas_find_target(starget
);
696 starget
->hostdata
= found_dev
;
700 #define SAS_DEF_QD 32
701 #define SAS_MAX_QD 64
703 int sas_slave_configure(struct scsi_device
*scsi_dev
)
705 struct domain_device
*dev
= sdev_to_domain_dev(scsi_dev
);
706 struct sas_ha_struct
*sas_ha
;
708 BUG_ON(dev
->rphy
->identify
.device_type
!= SAS_END_DEVICE
);
710 sas_ha
= dev
->port
->ha
;
712 sas_read_port_mode_page(scsi_dev
);
714 if (scsi_dev
->tagged_supported
) {
715 scsi_set_tag_type(scsi_dev
, MSG_SIMPLE_TAG
);
716 scsi_activate_tcq(scsi_dev
, SAS_DEF_QD
);
718 SAS_DPRINTK("device %llx, LUN %x doesn't support "
719 "TCQ\n", SAS_ADDR(dev
->sas_addr
),
721 scsi_dev
->tagged_supported
= 0;
722 scsi_set_tag_type(scsi_dev
, 0);
723 scsi_deactivate_tcq(scsi_dev
, 1);
726 scsi_dev
->allow_restart
= 1;
731 void sas_slave_destroy(struct scsi_device
*scsi_dev
)
735 int sas_change_queue_depth(struct scsi_device
*scsi_dev
, int new_depth
)
737 int res
= min(new_depth
, SAS_MAX_QD
);
739 if (scsi_dev
->tagged_supported
)
740 scsi_adjust_queue_depth(scsi_dev
, scsi_get_tag_type(scsi_dev
),
743 struct domain_device
*dev
= sdev_to_domain_dev(scsi_dev
);
744 sas_printk("device %llx LUN %x queue depth changed to 1\n",
745 SAS_ADDR(dev
->sas_addr
),
747 scsi_adjust_queue_depth(scsi_dev
, 0, 1);
754 int sas_change_queue_type(struct scsi_device
*scsi_dev
, int qt
)
756 if (!scsi_dev
->tagged_supported
)
759 scsi_deactivate_tcq(scsi_dev
, 1);
761 scsi_set_tag_type(scsi_dev
, qt
);
762 scsi_activate_tcq(scsi_dev
, scsi_dev
->queue_depth
);
767 int sas_bios_param(struct scsi_device
*scsi_dev
,
768 struct block_device
*bdev
,
769 sector_t capacity
, int *hsc
)
773 sector_div(capacity
, 255*63);
779 /* ---------- Task Collector Thread implementation ---------- */
781 static void sas_queue(struct sas_ha_struct
*sas_ha
)
783 struct scsi_core
*core
= &sas_ha
->core
;
788 struct sas_internal
*i
= to_sas_internal(core
->shost
->transportt
);
790 spin_lock_irqsave(&core
->task_queue_lock
, flags
);
791 while (!core
->queue_thread_kill
&&
792 !list_empty(&core
->task_queue
)) {
794 can_queue
= sas_ha
->lldd_queue_size
- core
->task_queue_size
;
795 if (can_queue
>= 0) {
796 can_queue
= core
->task_queue_size
;
797 list_splice_init(&core
->task_queue
, &q
);
799 struct list_head
*a
, *n
;
801 can_queue
= sas_ha
->lldd_queue_size
;
802 list_for_each_safe(a
, n
, &core
->task_queue
) {
803 list_move_tail(a
, &q
);
804 if (--can_queue
== 0)
807 can_queue
= sas_ha
->lldd_queue_size
;
809 core
->task_queue_size
-= can_queue
;
810 spin_unlock_irqrestore(&core
->task_queue_lock
, flags
);
812 struct sas_task
*task
= list_entry(q
.next
,
816 res
= i
->dft
->lldd_execute_task(task
, can_queue
,
819 __list_add(&q
, task
->list
.prev
, &task
->list
);
821 spin_lock_irqsave(&core
->task_queue_lock
, flags
);
823 list_splice_init(&q
, &core
->task_queue
); /*at head*/
824 core
->task_queue_size
+= can_queue
;
827 spin_unlock_irqrestore(&core
->task_queue_lock
, flags
);
830 static DECLARE_COMPLETION(queue_th_comp
);
833 * sas_queue_thread -- The Task Collector thread
834 * @_sas_ha: pointer to struct sas_ha
836 static int sas_queue_thread(void *_sas_ha
)
838 struct sas_ha_struct
*sas_ha
= _sas_ha
;
839 struct scsi_core
*core
= &sas_ha
->core
;
841 daemonize("sas_queue_%d", core
->shost
->host_no
);
842 current
->flags
|= PF_NOFREEZE
;
844 complete(&queue_th_comp
);
847 down_interruptible(&core
->queue_thread_sema
);
849 if (core
->queue_thread_kill
)
853 complete(&queue_th_comp
);
858 int sas_init_queue(struct sas_ha_struct
*sas_ha
)
861 struct scsi_core
*core
= &sas_ha
->core
;
863 spin_lock_init(&core
->task_queue_lock
);
864 core
->task_queue_size
= 0;
865 INIT_LIST_HEAD(&core
->task_queue
);
866 init_MUTEX_LOCKED(&core
->queue_thread_sema
);
868 res
= kernel_thread(sas_queue_thread
, sas_ha
, 0);
870 wait_for_completion(&queue_th_comp
);
872 return res
< 0 ? res
: 0;
875 void sas_shutdown_queue(struct sas_ha_struct
*sas_ha
)
878 struct scsi_core
*core
= &sas_ha
->core
;
879 struct sas_task
*task
, *n
;
881 init_completion(&queue_th_comp
);
882 core
->queue_thread_kill
= 1;
883 up(&core
->queue_thread_sema
);
884 wait_for_completion(&queue_th_comp
);
886 if (!list_empty(&core
->task_queue
))
887 SAS_DPRINTK("HA: %llx: scsi core task queue is NOT empty!?\n",
888 SAS_ADDR(sas_ha
->sas_addr
));
890 spin_lock_irqsave(&core
->task_queue_lock
, flags
);
891 list_for_each_entry_safe(task
, n
, &core
->task_queue
, list
) {
892 struct scsi_cmnd
*cmd
= task
->uldd_task
;
894 list_del_init(&task
->list
);
896 ASSIGN_SAS_TASK(cmd
, NULL
);
898 cmd
->result
= DID_ABORT
<< 16;
901 spin_unlock_irqrestore(&core
->task_queue_lock
, flags
);
905 * Call the LLDD task abort routine directly. This function is intended for
906 * use by upper layers that need to tell the LLDD to abort a task.
908 int __sas_task_abort(struct sas_task
*task
)
910 struct sas_internal
*si
=
911 to_sas_internal(task
->dev
->port
->ha
->core
.shost
->transportt
);
915 spin_lock_irqsave(&task
->task_state_lock
, flags
);
916 if (task
->task_state_flags
& SAS_TASK_STATE_ABORTED
||
917 task
->task_state_flags
& SAS_TASK_STATE_DONE
) {
918 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
919 SAS_DPRINTK("%s: Task %p already finished.\n", __FUNCTION__
,
923 task
->task_state_flags
|= SAS_TASK_STATE_ABORTED
;
924 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
926 if (!si
->dft
->lldd_abort_task
)
929 res
= si
->dft
->lldd_abort_task(task
);
931 spin_lock_irqsave(&task
->task_state_lock
, flags
);
932 if ((task
->task_state_flags
& SAS_TASK_STATE_DONE
) ||
933 (res
== TMF_RESP_FUNC_COMPLETE
))
935 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
936 task
->task_done(task
);
940 if (!(task
->task_state_flags
& SAS_TASK_STATE_DONE
))
941 task
->task_state_flags
&= ~SAS_TASK_STATE_ABORTED
;
942 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
948 * Tell an upper layer that it needs to initiate an abort for a given task.
949 * This should only ever be called by an LLDD.
951 void sas_task_abort(struct sas_task
*task
)
953 struct scsi_cmnd
*sc
= task
->uldd_task
;
955 /* Escape for libsas internal commands */
957 if (!del_timer(&task
->timer
))
959 task
->timer
.function(task
->timer
.data
);
963 scsi_req_abort_cmd(sc
);
964 scsi_schedule_eh(sc
->device
->host
);
967 EXPORT_SYMBOL_GPL(sas_queuecommand
);
968 EXPORT_SYMBOL_GPL(sas_target_alloc
);
969 EXPORT_SYMBOL_GPL(sas_slave_configure
);
970 EXPORT_SYMBOL_GPL(sas_slave_destroy
);
971 EXPORT_SYMBOL_GPL(sas_change_queue_depth
);
972 EXPORT_SYMBOL_GPL(sas_change_queue_type
);
973 EXPORT_SYMBOL_GPL(sas_bios_param
);
974 EXPORT_SYMBOL_GPL(__sas_task_abort
);
975 EXPORT_SYMBOL_GPL(sas_task_abort
);
976 EXPORT_SYMBOL_GPL(sas_phy_reset
);
977 EXPORT_SYMBOL_GPL(sas_phy_enable
);
978 EXPORT_SYMBOL_GPL(sas_eh_device_reset_handler
);