2 * PMC-Sierra SPC 8001 SAS/SATA based host adapters driver
4 * Copyright (c) 2008-2009 USI Co., Ltd.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions, and the following disclaimer,
12 * without modification.
13 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
14 * substantially similar to the "NO WARRANTY" disclaimer below
15 * ("Disclaimer") and any redistribution must be conditioned upon
16 * including a substantially similar Disclaimer requirement for further
17 * binary redistribution.
18 * 3. Neither the names of the above-listed copyright holders nor the names
19 * of any contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * Alternatively, this software may be distributed under the terms of the
23 * GNU General Public License ("GPL") version 2 as published by the Free
24 * Software Foundation.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
30 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
35 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
36 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGES.
41 #include <linux/slab.h>
42 #include "pm8001_sas.h"
45 * pm8001_find_tag - from sas task to find out tag that belongs to this task
46 * @task: the task sent to the LLDD
47 * @tag: the found tag associated with the task
49 static int pm8001_find_tag(struct sas_task
*task
, u32
*tag
)
51 if (task
->lldd_task
) {
52 struct pm8001_ccb_info
*ccb
;
53 ccb
= task
->lldd_task
;
61 * pm8001_tag_clear - clear the tags bitmap
62 * @pm8001_ha: our hba struct
63 * @tag: the found tag associated with the task
65 static void pm8001_tag_clear(struct pm8001_hba_info
*pm8001_ha
, u32 tag
)
67 void *bitmap
= pm8001_ha
->tags
;
68 clear_bit(tag
, bitmap
);
71 static void pm8001_tag_free(struct pm8001_hba_info
*pm8001_ha
, u32 tag
)
73 pm8001_tag_clear(pm8001_ha
, tag
);
76 static void pm8001_tag_set(struct pm8001_hba_info
*pm8001_ha
, u32 tag
)
78 void *bitmap
= pm8001_ha
->tags
;
83 * pm8001_tag_alloc - allocate a empty tag for task used.
84 * @pm8001_ha: our hba struct
85 * @tag_out: the found empty tag .
87 inline int pm8001_tag_alloc(struct pm8001_hba_info
*pm8001_ha
, u32
*tag_out
)
89 unsigned int index
, tag
;
90 void *bitmap
= pm8001_ha
->tags
;
92 index
= find_first_zero_bit(bitmap
, pm8001_ha
->tags_num
);
94 if (tag
>= pm8001_ha
->tags_num
)
95 return -SAS_QUEUE_FULL
;
96 pm8001_tag_set(pm8001_ha
, tag
);
101 void pm8001_tag_init(struct pm8001_hba_info
*pm8001_ha
)
104 for (i
= 0; i
< pm8001_ha
->tags_num
; ++i
)
105 pm8001_tag_clear(pm8001_ha
, i
);
109 * pm8001_mem_alloc - allocate memory for pm8001.
111 * @virt_addr: the allocated virtual address
112 * @pphys_addr_hi: the physical address high byte address.
113 * @pphys_addr_lo: the physical address low byte address.
114 * @mem_size: memory size.
116 int pm8001_mem_alloc(struct pci_dev
*pdev
, void **virt_addr
,
117 dma_addr_t
*pphys_addr
, u32
*pphys_addr_hi
,
118 u32
*pphys_addr_lo
, u32 mem_size
, u32 align
)
120 caddr_t mem_virt_alloc
;
121 dma_addr_t mem_dma_handle
;
123 u64 align_offset
= 0;
125 align_offset
= (dma_addr_t
)align
- 1;
127 pci_alloc_consistent(pdev
, mem_size
+ align
, &mem_dma_handle
);
128 if (!mem_virt_alloc
) {
129 pm8001_printk("memory allocation error\n");
132 memset((void *)mem_virt_alloc
, 0, mem_size
+align
);
133 *pphys_addr
= mem_dma_handle
;
134 phys_align
= (*pphys_addr
+ align_offset
) & ~align_offset
;
135 *virt_addr
= (void *)mem_virt_alloc
+ phys_align
- *pphys_addr
;
136 *pphys_addr_hi
= upper_32_bits(phys_align
);
137 *pphys_addr_lo
= lower_32_bits(phys_align
);
141 * pm8001_find_ha_by_dev - from domain device which come from sas layer to
142 * find out our hba struct.
143 * @dev: the domain device which from sas layer.
146 struct pm8001_hba_info
*pm8001_find_ha_by_dev(struct domain_device
*dev
)
148 struct sas_ha_struct
*sha
= dev
->port
->ha
;
149 struct pm8001_hba_info
*pm8001_ha
= sha
->lldd_ha
;
154 * pm8001_phy_control - this function should be registered to
155 * sas_domain_function_template to provide libsas used, note: this is just
156 * control the HBA phy rather than other expander phy if you want control
157 * other phy, you should use SMP command.
158 * @sas_phy: which phy in HBA phys.
159 * @func: the operation.
160 * @funcdata: always NULL.
162 int pm8001_phy_control(struct asd_sas_phy
*sas_phy
, enum phy_func func
,
165 int rc
= 0, phy_id
= sas_phy
->id
;
166 struct pm8001_hba_info
*pm8001_ha
= NULL
;
167 struct sas_phy_linkrates
*rates
;
168 DECLARE_COMPLETION_ONSTACK(completion
);
169 pm8001_ha
= sas_phy
->ha
->lldd_ha
;
170 pm8001_ha
->phy
[phy_id
].enable_completion
= &completion
;
172 case PHY_FUNC_SET_LINK_RATE
:
174 if (rates
->minimum_linkrate
) {
175 pm8001_ha
->phy
[phy_id
].minimum_linkrate
=
176 rates
->minimum_linkrate
;
178 if (rates
->maximum_linkrate
) {
179 pm8001_ha
->phy
[phy_id
].maximum_linkrate
=
180 rates
->maximum_linkrate
;
182 if (pm8001_ha
->phy
[phy_id
].phy_state
== 0) {
183 PM8001_CHIP_DISP
->phy_start_req(pm8001_ha
, phy_id
);
184 wait_for_completion(&completion
);
186 PM8001_CHIP_DISP
->phy_ctl_req(pm8001_ha
, phy_id
,
189 case PHY_FUNC_HARD_RESET
:
190 if (pm8001_ha
->phy
[phy_id
].phy_state
== 0) {
191 PM8001_CHIP_DISP
->phy_start_req(pm8001_ha
, phy_id
);
192 wait_for_completion(&completion
);
194 PM8001_CHIP_DISP
->phy_ctl_req(pm8001_ha
, phy_id
,
197 case PHY_FUNC_LINK_RESET
:
198 if (pm8001_ha
->phy
[phy_id
].phy_state
== 0) {
199 PM8001_CHIP_DISP
->phy_start_req(pm8001_ha
, phy_id
);
200 wait_for_completion(&completion
);
202 PM8001_CHIP_DISP
->phy_ctl_req(pm8001_ha
, phy_id
,
205 case PHY_FUNC_RELEASE_SPINUP_HOLD
:
206 PM8001_CHIP_DISP
->phy_ctl_req(pm8001_ha
, phy_id
,
209 case PHY_FUNC_DISABLE
:
210 PM8001_CHIP_DISP
->phy_stop_req(pm8001_ha
, phy_id
);
220 * pm8001_scan_start - we should enable all HBA phys by sending the phy_start
222 * @shost: the scsi host data.
224 void pm8001_scan_start(struct Scsi_Host
*shost
)
227 struct pm8001_hba_info
*pm8001_ha
;
228 struct sas_ha_struct
*sha
= SHOST_TO_SAS_HA(shost
);
229 pm8001_ha
= sha
->lldd_ha
;
230 PM8001_CHIP_DISP
->sas_re_init_req(pm8001_ha
);
231 for (i
= 0; i
< pm8001_ha
->chip
->n_phy
; ++i
)
232 PM8001_CHIP_DISP
->phy_start_req(pm8001_ha
, i
);
235 int pm8001_scan_finished(struct Scsi_Host
*shost
, unsigned long time
)
237 /* give the phy enabling interrupt event time to come in (1s
238 * is empirically about all it takes) */
241 /* Wait for discovery to finish */
242 scsi_flush_work(shost
);
247 * pm8001_task_prep_smp - the dispatcher function, prepare data for smp task
248 * @pm8001_ha: our hba card information
249 * @ccb: the ccb which attached to smp task
251 static int pm8001_task_prep_smp(struct pm8001_hba_info
*pm8001_ha
,
252 struct pm8001_ccb_info
*ccb
)
254 return PM8001_CHIP_DISP
->smp_req(pm8001_ha
, ccb
);
257 u32
pm8001_get_ncq_tag(struct sas_task
*task
, u32
*tag
)
259 struct ata_queued_cmd
*qc
= task
->uldd_task
;
261 if (qc
->tf
.command
== ATA_CMD_FPDMA_WRITE
||
262 qc
->tf
.command
== ATA_CMD_FPDMA_READ
) {
271 * pm8001_task_prep_ata - the dispatcher function, prepare data for sata task
272 * @pm8001_ha: our hba card information
273 * @ccb: the ccb which attached to sata task
275 static int pm8001_task_prep_ata(struct pm8001_hba_info
*pm8001_ha
,
276 struct pm8001_ccb_info
*ccb
)
278 return PM8001_CHIP_DISP
->sata_req(pm8001_ha
, ccb
);
282 * pm8001_task_prep_ssp_tm - the dispatcher function, prepare task management data
283 * @pm8001_ha: our hba card information
284 * @ccb: the ccb which attached to TM
285 * @tmf: the task management IU
287 static int pm8001_task_prep_ssp_tm(struct pm8001_hba_info
*pm8001_ha
,
288 struct pm8001_ccb_info
*ccb
, struct pm8001_tmf_task
*tmf
)
290 return PM8001_CHIP_DISP
->ssp_tm_req(pm8001_ha
, ccb
, tmf
);
294 * pm8001_task_prep_ssp - the dispatcher function,prepare ssp data for ssp task
295 * @pm8001_ha: our hba card information
296 * @ccb: the ccb which attached to ssp task
298 static int pm8001_task_prep_ssp(struct pm8001_hba_info
*pm8001_ha
,
299 struct pm8001_ccb_info
*ccb
)
301 return PM8001_CHIP_DISP
->ssp_io_req(pm8001_ha
, ccb
);
304 /* Find the local port id that's attached to this device */
305 static int sas_find_local_port_id(struct domain_device
*dev
)
307 struct domain_device
*pdev
= dev
->parent
;
309 /* Directly attached device */
311 return dev
->port
->id
;
313 struct domain_device
*pdev_p
= pdev
->parent
;
315 return pdev
->port
->id
;
322 * pm8001_task_exec - queue the task(ssp, smp && ata) to the hardware.
323 * @task: the task to be execute.
324 * @num: if can_queue great than 1, the task can be queued up. for SMP task,
325 * we always execute one one time.
326 * @gfp_flags: gfp_flags.
327 * @is_tmf: if it is task management task.
328 * @tmf: the task management IU
330 #define DEV_IS_GONE(pm8001_dev) \
331 ((!pm8001_dev || (pm8001_dev->dev_type == NO_DEVICE)))
332 static int pm8001_task_exec(struct sas_task
*task
, const int num
,
333 gfp_t gfp_flags
, int is_tmf
, struct pm8001_tmf_task
*tmf
)
335 struct domain_device
*dev
= task
->dev
;
336 struct pm8001_hba_info
*pm8001_ha
;
337 struct pm8001_device
*pm8001_dev
;
338 struct pm8001_port
*port
= NULL
;
339 struct sas_task
*t
= task
;
340 struct pm8001_ccb_info
*ccb
;
341 u32 tag
= 0xdeadbeef, rc
, n_elem
= 0;
343 unsigned long flags
= 0, flags_libsas
= 0;
346 struct task_status_struct
*tsm
= &t
->task_status
;
347 tsm
->resp
= SAS_TASK_UNDELIVERED
;
348 tsm
->stat
= SAS_PHY_DOWN
;
349 if (dev
->dev_type
!= SATA_DEV
)
353 pm8001_ha
= pm8001_find_ha_by_dev(task
->dev
);
354 PM8001_IO_DBG(pm8001_ha
, pm8001_printk("pm8001_task_exec device \n "));
355 spin_lock_irqsave(&pm8001_ha
->lock
, flags
);
358 pm8001_dev
= dev
->lldd_dev
;
359 port
= &pm8001_ha
->port
[sas_find_local_port_id(dev
)];
360 if (DEV_IS_GONE(pm8001_dev
) || !port
->port_attached
) {
361 if (sas_protocol_ata(t
->task_proto
)) {
362 struct task_status_struct
*ts
= &t
->task_status
;
363 ts
->resp
= SAS_TASK_UNDELIVERED
;
364 ts
->stat
= SAS_PHY_DOWN
;
366 spin_unlock_irqrestore(&pm8001_ha
->lock
, flags
);
367 spin_unlock_irqrestore(dev
->sata_dev
.ap
->lock
,
370 spin_lock_irqsave(dev
->sata_dev
.ap
->lock
,
372 spin_lock_irqsave(&pm8001_ha
->lock
, flags
);
374 t
= list_entry(t
->list
.next
,
375 struct sas_task
, list
);
378 struct task_status_struct
*ts
= &t
->task_status
;
379 ts
->resp
= SAS_TASK_UNDELIVERED
;
380 ts
->stat
= SAS_PHY_DOWN
;
383 t
= list_entry(t
->list
.next
,
384 struct sas_task
, list
);
388 rc
= pm8001_tag_alloc(pm8001_ha
, &tag
);
391 ccb
= &pm8001_ha
->ccb_info
[tag
];
393 if (!sas_protocol_ata(t
->task_proto
)) {
394 if (t
->num_scatter
) {
395 n_elem
= dma_map_sg(pm8001_ha
->dev
,
405 n_elem
= t
->num_scatter
;
409 ccb
->n_elem
= n_elem
;
412 switch (t
->task_proto
) {
413 case SAS_PROTOCOL_SMP
:
414 rc
= pm8001_task_prep_smp(pm8001_ha
, ccb
);
416 case SAS_PROTOCOL_SSP
:
418 rc
= pm8001_task_prep_ssp_tm(pm8001_ha
,
421 rc
= pm8001_task_prep_ssp(pm8001_ha
, ccb
);
423 case SAS_PROTOCOL_SATA
:
424 case SAS_PROTOCOL_STP
:
425 case SAS_PROTOCOL_SATA
| SAS_PROTOCOL_STP
:
426 rc
= pm8001_task_prep_ata(pm8001_ha
, ccb
);
429 dev_printk(KERN_ERR
, pm8001_ha
->dev
,
430 "unknown sas_task proto: 0x%x\n",
437 PM8001_IO_DBG(pm8001_ha
,
438 pm8001_printk("rc is %x\n", rc
));
441 /* TODO: select normal or high priority */
442 spin_lock(&t
->task_state_lock
);
443 t
->task_state_flags
|= SAS_TASK_AT_INITIATOR
;
444 spin_unlock(&t
->task_state_lock
);
445 pm8001_dev
->running_req
++;
447 t
= list_entry(t
->list
.next
, struct sas_task
, list
);
453 pm8001_tag_free(pm8001_ha
, tag
);
455 dev_printk(KERN_ERR
, pm8001_ha
->dev
, "pm8001 exec failed[%d]!\n", rc
);
456 if (!sas_protocol_ata(t
->task_proto
))
458 dma_unmap_sg(pm8001_ha
->dev
, t
->scatter
, n_elem
,
461 spin_unlock_irqrestore(&pm8001_ha
->lock
, flags
);
466 * pm8001_queue_command - register for upper layer used, all IO commands sent
467 * to HBA are from this interface.
468 * @task: the task to be execute.
469 * @num: if can_queue great than 1, the task can be queued up. for SMP task,
470 * we always execute one one time
471 * @gfp_flags: gfp_flags
473 int pm8001_queue_command(struct sas_task
*task
, const int num
,
476 return pm8001_task_exec(task
, num
, gfp_flags
, 0, NULL
);
479 void pm8001_ccb_free(struct pm8001_hba_info
*pm8001_ha
, u32 ccb_idx
)
481 pm8001_tag_clear(pm8001_ha
, ccb_idx
);
485 * pm8001_ccb_task_free - free the sg for ssp and smp command, free the ccb.
486 * @pm8001_ha: our hba card information
487 * @ccb: the ccb which attached to ssp task
488 * @task: the task to be free.
489 * @ccb_idx: ccb index.
491 void pm8001_ccb_task_free(struct pm8001_hba_info
*pm8001_ha
,
492 struct sas_task
*task
, struct pm8001_ccb_info
*ccb
, u32 ccb_idx
)
496 if (!sas_protocol_ata(task
->task_proto
))
498 dma_unmap_sg(pm8001_ha
->dev
, task
->scatter
,
499 task
->num_scatter
, task
->data_dir
);
501 switch (task
->task_proto
) {
502 case SAS_PROTOCOL_SMP
:
503 dma_unmap_sg(pm8001_ha
->dev
, &task
->smp_task
.smp_resp
, 1,
505 dma_unmap_sg(pm8001_ha
->dev
, &task
->smp_task
.smp_req
, 1,
509 case SAS_PROTOCOL_SATA
:
510 case SAS_PROTOCOL_STP
:
511 case SAS_PROTOCOL_SSP
:
516 task
->lldd_task
= NULL
;
518 ccb
->ccb_tag
= 0xFFFFFFFF;
519 pm8001_ccb_free(pm8001_ha
, ccb_idx
);
523 * pm8001_alloc_dev - find a empty pm8001_device
524 * @pm8001_ha: our hba card information
526 struct pm8001_device
*pm8001_alloc_dev(struct pm8001_hba_info
*pm8001_ha
)
529 for (dev
= 0; dev
< PM8001_MAX_DEVICES
; dev
++) {
530 if (pm8001_ha
->devices
[dev
].dev_type
== NO_DEVICE
) {
531 pm8001_ha
->devices
[dev
].id
= dev
;
532 return &pm8001_ha
->devices
[dev
];
535 if (dev
== PM8001_MAX_DEVICES
) {
536 PM8001_FAIL_DBG(pm8001_ha
,
537 pm8001_printk("max support %d devices, ignore ..\n",
538 PM8001_MAX_DEVICES
));
543 static void pm8001_free_dev(struct pm8001_device
*pm8001_dev
)
545 u32 id
= pm8001_dev
->id
;
546 memset(pm8001_dev
, 0, sizeof(*pm8001_dev
));
548 pm8001_dev
->dev_type
= NO_DEVICE
;
549 pm8001_dev
->device_id
= PM8001_MAX_DEVICES
;
550 pm8001_dev
->sas_device
= NULL
;
554 * pm8001_dev_found_notify - libsas notify a device is found.
555 * @dev: the device structure which sas layer used.
557 * when libsas find a sas domain device, it should tell the LLDD that
558 * device is found, and then LLDD register this device to HBA firmware
559 * by the command "OPC_INB_REG_DEV", after that the HBA will assign a
560 * device ID(according to device's sas address) and returned it to LLDD. From
561 * now on, we communicate with HBA FW with the device ID which HBA assigned
562 * rather than sas address. it is the necessary step for our HBA but it is
563 * the optional for other HBA driver.
565 static int pm8001_dev_found_notify(struct domain_device
*dev
)
567 unsigned long flags
= 0;
569 struct pm8001_hba_info
*pm8001_ha
= NULL
;
570 struct domain_device
*parent_dev
= dev
->parent
;
571 struct pm8001_device
*pm8001_device
;
572 DECLARE_COMPLETION_ONSTACK(completion
);
574 pm8001_ha
= pm8001_find_ha_by_dev(dev
);
575 spin_lock_irqsave(&pm8001_ha
->lock
, flags
);
577 pm8001_device
= pm8001_alloc_dev(pm8001_ha
);
578 if (!pm8001_device
) {
582 pm8001_device
->sas_device
= dev
;
583 dev
->lldd_dev
= pm8001_device
;
584 pm8001_device
->dev_type
= dev
->dev_type
;
585 pm8001_device
->dcompletion
= &completion
;
586 if (parent_dev
&& DEV_IS_EXPANDER(parent_dev
->dev_type
)) {
589 for (phy_id
= 0; phy_id
< parent_dev
->ex_dev
.num_phys
;
591 phy
= &parent_dev
->ex_dev
.ex_phy
[phy_id
];
592 if (SAS_ADDR(phy
->attached_sas_addr
)
593 == SAS_ADDR(dev
->sas_addr
)) {
594 pm8001_device
->attached_phy
= phy_id
;
598 if (phy_id
== parent_dev
->ex_dev
.num_phys
) {
599 PM8001_FAIL_DBG(pm8001_ha
,
600 pm8001_printk("Error: no attached dev:%016llx"
601 " at ex:%016llx.\n", SAS_ADDR(dev
->sas_addr
),
602 SAS_ADDR(parent_dev
->sas_addr
)));
606 if (dev
->dev_type
== SATA_DEV
) {
607 pm8001_device
->attached_phy
=
608 dev
->rphy
->identify
.phy_identifier
;
609 flag
= 1; /* directly sata*/
611 } /*register this device to HBA*/
612 PM8001_DISC_DBG(pm8001_ha
, pm8001_printk("Found device\n"));
613 PM8001_CHIP_DISP
->reg_dev_req(pm8001_ha
, pm8001_device
, flag
);
614 spin_unlock_irqrestore(&pm8001_ha
->lock
, flags
);
615 wait_for_completion(&completion
);
616 if (dev
->dev_type
== SAS_END_DEV
)
618 pm8001_ha
->flags
|= PM8001F_RUN_TIME
;
621 spin_unlock_irqrestore(&pm8001_ha
->lock
, flags
);
625 int pm8001_dev_found(struct domain_device
*dev
)
627 return pm8001_dev_found_notify(dev
);
630 static void pm8001_task_done(struct sas_task
*task
)
632 if (!del_timer(&task
->timer
))
634 complete(&task
->completion
);
637 static void pm8001_tmf_timedout(unsigned long data
)
639 struct sas_task
*task
= (struct sas_task
*)data
;
641 task
->task_state_flags
|= SAS_TASK_STATE_ABORTED
;
642 complete(&task
->completion
);
645 #define PM8001_TASK_TIMEOUT 20
647 * pm8001_exec_internal_tmf_task - execute some task management commands.
648 * @dev: the wanted device.
649 * @tmf: which task management wanted to be take.
650 * @para_len: para_len.
651 * @parameter: ssp task parameter.
653 * when errors or exception happened, we may want to do something, for example
654 * abort the issued task which result in this execption, it is done by calling
655 * this function, note it is also with the task execute interface.
657 static int pm8001_exec_internal_tmf_task(struct domain_device
*dev
,
658 void *parameter
, u32 para_len
, struct pm8001_tmf_task
*tmf
)
661 struct sas_task
*task
= NULL
;
662 struct pm8001_hba_info
*pm8001_ha
= pm8001_find_ha_by_dev(dev
);
664 for (retry
= 0; retry
< 3; retry
++) {
665 task
= sas_alloc_task(GFP_KERNEL
);
670 task
->task_proto
= dev
->tproto
;
671 memcpy(&task
->ssp_task
, parameter
, para_len
);
672 task
->task_done
= pm8001_task_done
;
673 task
->timer
.data
= (unsigned long)task
;
674 task
->timer
.function
= pm8001_tmf_timedout
;
675 task
->timer
.expires
= jiffies
+ PM8001_TASK_TIMEOUT
*HZ
;
676 add_timer(&task
->timer
);
678 res
= pm8001_task_exec(task
, 1, GFP_KERNEL
, 1, tmf
);
681 del_timer(&task
->timer
);
682 PM8001_FAIL_DBG(pm8001_ha
,
683 pm8001_printk("Executing internal task "
687 wait_for_completion(&task
->completion
);
688 res
= -TMF_RESP_FUNC_FAILED
;
689 /* Even TMF timed out, return direct. */
690 if ((task
->task_state_flags
& SAS_TASK_STATE_ABORTED
)) {
691 if (!(task
->task_state_flags
& SAS_TASK_STATE_DONE
)) {
692 PM8001_FAIL_DBG(pm8001_ha
,
693 pm8001_printk("TMF task[%x]timeout.\n",
699 if (task
->task_status
.resp
== SAS_TASK_COMPLETE
&&
700 task
->task_status
.stat
== SAM_STAT_GOOD
) {
701 res
= TMF_RESP_FUNC_COMPLETE
;
705 if (task
->task_status
.resp
== SAS_TASK_COMPLETE
&&
706 task
->task_status
.stat
== SAS_DATA_UNDERRUN
) {
707 /* no error, but return the number of bytes of
709 res
= task
->task_status
.residual
;
713 if (task
->task_status
.resp
== SAS_TASK_COMPLETE
&&
714 task
->task_status
.stat
== SAS_DATA_OVERRUN
) {
715 PM8001_FAIL_DBG(pm8001_ha
,
716 pm8001_printk("Blocked task error.\n"));
720 PM8001_EH_DBG(pm8001_ha
,
721 pm8001_printk(" Task to dev %016llx response:"
722 "0x%x status 0x%x\n",
723 SAS_ADDR(dev
->sas_addr
),
724 task
->task_status
.resp
,
725 task
->task_status
.stat
));
731 BUG_ON(retry
== 3 && task
!= NULL
);
737 pm8001_exec_internal_task_abort(struct pm8001_hba_info
*pm8001_ha
,
738 struct pm8001_device
*pm8001_dev
, struct domain_device
*dev
, u32 flag
,
743 struct pm8001_ccb_info
*ccb
;
744 struct sas_task
*task
= NULL
;
746 for (retry
= 0; retry
< 3; retry
++) {
747 task
= sas_alloc_task(GFP_KERNEL
);
752 task
->task_proto
= dev
->tproto
;
753 task
->task_done
= pm8001_task_done
;
754 task
->timer
.data
= (unsigned long)task
;
755 task
->timer
.function
= pm8001_tmf_timedout
;
756 task
->timer
.expires
= jiffies
+ PM8001_TASK_TIMEOUT
* HZ
;
757 add_timer(&task
->timer
);
759 res
= pm8001_tag_alloc(pm8001_ha
, &ccb_tag
);
762 ccb
= &pm8001_ha
->ccb_info
[ccb_tag
];
763 ccb
->device
= pm8001_dev
;
764 ccb
->ccb_tag
= ccb_tag
;
767 res
= PM8001_CHIP_DISP
->task_abort(pm8001_ha
,
768 pm8001_dev
, flag
, task_tag
, ccb_tag
);
771 del_timer(&task
->timer
);
772 PM8001_FAIL_DBG(pm8001_ha
,
773 pm8001_printk("Executing internal task "
777 wait_for_completion(&task
->completion
);
778 res
= TMF_RESP_FUNC_FAILED
;
779 /* Even TMF timed out, return direct. */
780 if ((task
->task_state_flags
& SAS_TASK_STATE_ABORTED
)) {
781 if (!(task
->task_state_flags
& SAS_TASK_STATE_DONE
)) {
782 PM8001_FAIL_DBG(pm8001_ha
,
783 pm8001_printk("TMF task timeout.\n"));
788 if (task
->task_status
.resp
== SAS_TASK_COMPLETE
&&
789 task
->task_status
.stat
== SAM_STAT_GOOD
) {
790 res
= TMF_RESP_FUNC_COMPLETE
;
794 PM8001_EH_DBG(pm8001_ha
,
795 pm8001_printk(" Task to dev %016llx response: "
796 "0x%x status 0x%x\n",
797 SAS_ADDR(dev
->sas_addr
),
798 task
->task_status
.resp
,
799 task
->task_status
.stat
));
805 BUG_ON(retry
== 3 && task
!= NULL
);
811 * pm8001_dev_gone_notify - see the comments for "pm8001_dev_found_notify"
812 * @dev: the device structure which sas layer used.
814 static void pm8001_dev_gone_notify(struct domain_device
*dev
)
816 unsigned long flags
= 0;
818 struct pm8001_hba_info
*pm8001_ha
;
819 struct pm8001_device
*pm8001_dev
= dev
->lldd_dev
;
821 pm8001_ha
= pm8001_find_ha_by_dev(dev
);
822 spin_lock_irqsave(&pm8001_ha
->lock
, flags
);
823 pm8001_tag_alloc(pm8001_ha
, &tag
);
825 u32 device_id
= pm8001_dev
->device_id
;
827 PM8001_DISC_DBG(pm8001_ha
,
828 pm8001_printk("found dev[%d:%x] is gone.\n",
829 pm8001_dev
->device_id
, pm8001_dev
->dev_type
));
830 if (pm8001_dev
->running_req
) {
831 spin_unlock_irqrestore(&pm8001_ha
->lock
, flags
);
832 pm8001_exec_internal_task_abort(pm8001_ha
, pm8001_dev
,
834 spin_lock_irqsave(&pm8001_ha
->lock
, flags
);
836 PM8001_CHIP_DISP
->dereg_dev_req(pm8001_ha
, device_id
);
837 pm8001_free_dev(pm8001_dev
);
839 PM8001_DISC_DBG(pm8001_ha
,
840 pm8001_printk("Found dev has gone.\n"));
842 dev
->lldd_dev
= NULL
;
843 spin_unlock_irqrestore(&pm8001_ha
->lock
, flags
);
846 void pm8001_dev_gone(struct domain_device
*dev
)
848 pm8001_dev_gone_notify(dev
);
851 static int pm8001_issue_ssp_tmf(struct domain_device
*dev
,
852 u8
*lun
, struct pm8001_tmf_task
*tmf
)
854 struct sas_ssp_task ssp_task
;
855 if (!(dev
->tproto
& SAS_PROTOCOL_SSP
))
856 return TMF_RESP_FUNC_ESUPP
;
858 strncpy((u8
*)&ssp_task
.LUN
, lun
, 8);
859 return pm8001_exec_internal_tmf_task(dev
, &ssp_task
, sizeof(ssp_task
),
864 * Standard mandates link reset for ATA (type 0) and hard reset for
865 * SSP (type 1) , only for RECOVERY
867 int pm8001_I_T_nexus_reset(struct domain_device
*dev
)
869 int rc
= TMF_RESP_FUNC_FAILED
;
870 struct pm8001_device
*pm8001_dev
;
871 struct pm8001_hba_info
*pm8001_ha
;
873 if (!dev
|| !dev
->lldd_dev
)
876 pm8001_dev
= dev
->lldd_dev
;
877 pm8001_ha
= pm8001_find_ha_by_dev(dev
);
878 phy
= sas_find_local_phy(dev
);
880 if (dev_is_sata(dev
)) {
881 DECLARE_COMPLETION_ONSTACK(completion_setstate
);
882 if (scsi_is_sas_phy_local(phy
))
884 rc
= sas_phy_reset(phy
, 1);
886 rc
= pm8001_exec_internal_task_abort(pm8001_ha
, pm8001_dev
,
888 pm8001_dev
->setds_completion
= &completion_setstate
;
889 rc
= PM8001_CHIP_DISP
->set_dev_state_req(pm8001_ha
,
891 wait_for_completion(&completion_setstate
);
893 rc
= sas_phy_reset(phy
, 1);
896 PM8001_EH_DBG(pm8001_ha
, pm8001_printk(" for device[%x]:rc=%d\n",
897 pm8001_dev
->device_id
, rc
));
901 /* mandatory SAM-3, the task reset the specified LUN*/
902 int pm8001_lu_reset(struct domain_device
*dev
, u8
*lun
)
904 int rc
= TMF_RESP_FUNC_FAILED
;
905 struct pm8001_tmf_task tmf_task
;
906 struct pm8001_device
*pm8001_dev
= dev
->lldd_dev
;
907 struct pm8001_hba_info
*pm8001_ha
= pm8001_find_ha_by_dev(dev
);
908 if (dev_is_sata(dev
)) {
909 struct sas_phy
*phy
= sas_find_local_phy(dev
);
910 rc
= pm8001_exec_internal_task_abort(pm8001_ha
, pm8001_dev
,
912 rc
= sas_phy_reset(phy
, 1);
913 rc
= PM8001_CHIP_DISP
->set_dev_state_req(pm8001_ha
,
917 tmf_task
.tmf
= TMF_LU_RESET
;
918 rc
= pm8001_issue_ssp_tmf(dev
, lun
, &tmf_task
);
920 /* If failed, fall-through I_T_Nexus reset */
921 PM8001_EH_DBG(pm8001_ha
, pm8001_printk("for device[%x]:rc=%d\n",
922 pm8001_dev
->device_id
, rc
));
927 int pm8001_query_task(struct sas_task
*task
)
929 u32 tag
= 0xdeadbeef;
932 struct pm8001_tmf_task tmf_task
;
933 int rc
= TMF_RESP_FUNC_FAILED
;
934 if (unlikely(!task
|| !task
->lldd_task
|| !task
->dev
))
937 if (task
->task_proto
& SAS_PROTOCOL_SSP
) {
938 struct scsi_cmnd
*cmnd
= task
->uldd_task
;
939 struct domain_device
*dev
= task
->dev
;
940 struct pm8001_hba_info
*pm8001_ha
=
941 pm8001_find_ha_by_dev(dev
);
943 int_to_scsilun(cmnd
->device
->lun
, &lun
);
944 rc
= pm8001_find_tag(task
, &tag
);
946 rc
= TMF_RESP_FUNC_FAILED
;
949 PM8001_EH_DBG(pm8001_ha
, pm8001_printk("Query:["));
950 for (i
= 0; i
< 16; i
++)
951 printk(KERN_INFO
"%02x ", cmnd
->cmnd
[i
]);
952 printk(KERN_INFO
"]\n");
953 tmf_task
.tmf
= TMF_QUERY_TASK
;
954 tmf_task
.tag_of_task_to_be_managed
= tag
;
956 rc
= pm8001_issue_ssp_tmf(dev
, lun
.scsi_lun
, &tmf_task
);
958 /* The task is still in Lun, release it then */
959 case TMF_RESP_FUNC_SUCC
:
960 PM8001_EH_DBG(pm8001_ha
,
961 pm8001_printk("The task is still in Lun\n"));
963 /* The task is not in Lun or failed, reset the phy */
964 case TMF_RESP_FUNC_FAILED
:
965 case TMF_RESP_FUNC_COMPLETE
:
966 PM8001_EH_DBG(pm8001_ha
,
967 pm8001_printk("The task is not in Lun or failed,"
968 " reset the phy\n"));
972 pm8001_printk(":rc= %d\n", rc
);
976 /* mandatory SAM-3, still need free task/ccb info, abord the specified task */
977 int pm8001_abort_task(struct sas_task
*task
)
980 u32 tag
= 0xdeadbeef;
982 struct domain_device
*dev
;
983 struct pm8001_hba_info
*pm8001_ha
= NULL
;
984 struct pm8001_ccb_info
*ccb
;
986 struct pm8001_device
*pm8001_dev
;
987 struct pm8001_tmf_task tmf_task
;
988 int rc
= TMF_RESP_FUNC_FAILED
;
989 if (unlikely(!task
|| !task
->lldd_task
|| !task
->dev
))
991 spin_lock_irqsave(&task
->task_state_lock
, flags
);
992 if (task
->task_state_flags
& SAS_TASK_STATE_DONE
) {
993 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
994 rc
= TMF_RESP_FUNC_COMPLETE
;
997 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
998 if (task
->task_proto
& SAS_PROTOCOL_SSP
) {
999 struct scsi_cmnd
*cmnd
= task
->uldd_task
;
1001 ccb
= task
->lldd_task
;
1002 pm8001_dev
= dev
->lldd_dev
;
1003 pm8001_ha
= pm8001_find_ha_by_dev(dev
);
1004 int_to_scsilun(cmnd
->device
->lun
, &lun
);
1005 rc
= pm8001_find_tag(task
, &tag
);
1007 printk(KERN_INFO
"No such tag in %s\n", __func__
);
1008 rc
= TMF_RESP_FUNC_FAILED
;
1011 device_id
= pm8001_dev
->device_id
;
1012 PM8001_EH_DBG(pm8001_ha
,
1013 pm8001_printk("abort io to deviceid= %d\n", device_id
));
1014 tmf_task
.tmf
= TMF_ABORT_TASK
;
1015 tmf_task
.tag_of_task_to_be_managed
= tag
;
1016 rc
= pm8001_issue_ssp_tmf(dev
, lun
.scsi_lun
, &tmf_task
);
1017 pm8001_exec_internal_task_abort(pm8001_ha
, pm8001_dev
,
1018 pm8001_dev
->sas_device
, 0, tag
);
1019 } else if (task
->task_proto
& SAS_PROTOCOL_SATA
||
1020 task
->task_proto
& SAS_PROTOCOL_STP
) {
1022 pm8001_dev
= dev
->lldd_dev
;
1023 pm8001_ha
= pm8001_find_ha_by_dev(dev
);
1024 rc
= pm8001_find_tag(task
, &tag
);
1026 printk(KERN_INFO
"No such tag in %s\n", __func__
);
1027 rc
= TMF_RESP_FUNC_FAILED
;
1030 rc
= pm8001_exec_internal_task_abort(pm8001_ha
, pm8001_dev
,
1031 pm8001_dev
->sas_device
, 0, tag
);
1032 } else if (task
->task_proto
& SAS_PROTOCOL_SMP
) {
1035 pm8001_dev
= dev
->lldd_dev
;
1036 pm8001_ha
= pm8001_find_ha_by_dev(dev
);
1037 rc
= pm8001_find_tag(task
, &tag
);
1039 printk(KERN_INFO
"No such tag in %s\n", __func__
);
1040 rc
= TMF_RESP_FUNC_FAILED
;
1043 rc
= pm8001_exec_internal_task_abort(pm8001_ha
, pm8001_dev
,
1044 pm8001_dev
->sas_device
, 0, tag
);
1048 if (rc
!= TMF_RESP_FUNC_COMPLETE
)
1049 pm8001_printk("rc= %d\n", rc
);
1053 int pm8001_abort_task_set(struct domain_device
*dev
, u8
*lun
)
1055 int rc
= TMF_RESP_FUNC_FAILED
;
1056 struct pm8001_tmf_task tmf_task
;
1058 tmf_task
.tmf
= TMF_ABORT_TASK_SET
;
1059 rc
= pm8001_issue_ssp_tmf(dev
, lun
, &tmf_task
);
1063 int pm8001_clear_aca(struct domain_device
*dev
, u8
*lun
)
1065 int rc
= TMF_RESP_FUNC_FAILED
;
1066 struct pm8001_tmf_task tmf_task
;
1068 tmf_task
.tmf
= TMF_CLEAR_ACA
;
1069 rc
= pm8001_issue_ssp_tmf(dev
, lun
, &tmf_task
);
1074 int pm8001_clear_task_set(struct domain_device
*dev
, u8
*lun
)
1076 int rc
= TMF_RESP_FUNC_FAILED
;
1077 struct pm8001_tmf_task tmf_task
;
1078 struct pm8001_device
*pm8001_dev
= dev
->lldd_dev
;
1079 struct pm8001_hba_info
*pm8001_ha
= pm8001_find_ha_by_dev(dev
);
1081 PM8001_EH_DBG(pm8001_ha
,
1082 pm8001_printk("I_T_L_Q clear task set[%x]\n",
1083 pm8001_dev
->device_id
));
1084 tmf_task
.tmf
= TMF_CLEAR_TASK_SET
;
1085 rc
= pm8001_issue_ssp_tmf(dev
, lun
, &tmf_task
);