2 * PMC-Sierra PM8001/8081/8088/8089 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 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
);
170 pm8001_ha
= sas_phy
->ha
->lldd_ha
;
171 pm8001_ha
->phy
[phy_id
].enable_completion
= &completion
;
173 case PHY_FUNC_SET_LINK_RATE
:
175 if (rates
->minimum_linkrate
) {
176 pm8001_ha
->phy
[phy_id
].minimum_linkrate
=
177 rates
->minimum_linkrate
;
179 if (rates
->maximum_linkrate
) {
180 pm8001_ha
->phy
[phy_id
].maximum_linkrate
=
181 rates
->maximum_linkrate
;
183 if (pm8001_ha
->phy
[phy_id
].phy_state
== 0) {
184 PM8001_CHIP_DISP
->phy_start_req(pm8001_ha
, phy_id
);
185 wait_for_completion(&completion
);
187 PM8001_CHIP_DISP
->phy_ctl_req(pm8001_ha
, phy_id
,
190 case PHY_FUNC_HARD_RESET
:
191 if (pm8001_ha
->phy
[phy_id
].phy_state
== 0) {
192 PM8001_CHIP_DISP
->phy_start_req(pm8001_ha
, phy_id
);
193 wait_for_completion(&completion
);
195 PM8001_CHIP_DISP
->phy_ctl_req(pm8001_ha
, phy_id
,
198 case PHY_FUNC_LINK_RESET
:
199 if (pm8001_ha
->phy
[phy_id
].phy_state
== 0) {
200 PM8001_CHIP_DISP
->phy_start_req(pm8001_ha
, phy_id
);
201 wait_for_completion(&completion
);
203 PM8001_CHIP_DISP
->phy_ctl_req(pm8001_ha
, phy_id
,
206 case PHY_FUNC_RELEASE_SPINUP_HOLD
:
207 PM8001_CHIP_DISP
->phy_ctl_req(pm8001_ha
, phy_id
,
210 case PHY_FUNC_DISABLE
:
211 PM8001_CHIP_DISP
->phy_stop_req(pm8001_ha
, phy_id
);
213 case PHY_FUNC_GET_EVENTS
:
214 spin_lock_irqsave(&pm8001_ha
->lock
, flags
);
215 if (pm8001_ha
->chip_id
== chip_8001
) {
216 if (-1 == pm8001_bar4_shift(pm8001_ha
,
217 (phy_id
< 4) ? 0x30000 : 0x40000)) {
218 spin_unlock_irqrestore(&pm8001_ha
->lock
, flags
);
223 struct sas_phy
*phy
= sas_phy
->phy
;
224 uint32_t *qp
= (uint32_t *)(((char *)
225 pm8001_ha
->io_mem
[2].memvirtaddr
)
226 + 0x1034 + (0x4000 * (phy_id
& 3)));
228 phy
->invalid_dword_count
= qp
[0];
229 phy
->running_disparity_error_count
= qp
[1];
230 phy
->loss_of_dword_sync_count
= qp
[3];
231 phy
->phy_reset_problem_count
= qp
[4];
233 if (pm8001_ha
->chip_id
== chip_8001
)
234 pm8001_bar4_shift(pm8001_ha
, 0);
235 spin_unlock_irqrestore(&pm8001_ha
->lock
, flags
);
245 * pm8001_scan_start - we should enable all HBA phys by sending the phy_start
247 * @shost: the scsi host data.
249 void pm8001_scan_start(struct Scsi_Host
*shost
)
252 struct pm8001_hba_info
*pm8001_ha
;
253 struct sas_ha_struct
*sha
= SHOST_TO_SAS_HA(shost
);
254 pm8001_ha
= sha
->lldd_ha
;
255 /* SAS_RE_INITIALIZATION not available in SPCv/ve */
256 if (pm8001_ha
->chip_id
== chip_8001
)
257 PM8001_CHIP_DISP
->sas_re_init_req(pm8001_ha
);
258 for (i
= 0; i
< pm8001_ha
->chip
->n_phy
; ++i
)
259 PM8001_CHIP_DISP
->phy_start_req(pm8001_ha
, i
);
262 int pm8001_scan_finished(struct Scsi_Host
*shost
, unsigned long time
)
264 struct sas_ha_struct
*ha
= SHOST_TO_SAS_HA(shost
);
266 /* give the phy enabling interrupt event time to come in (1s
267 * is empirically about all it takes) */
270 /* Wait for discovery to finish */
276 * pm8001_task_prep_smp - the dispatcher function, prepare data for smp task
277 * @pm8001_ha: our hba card information
278 * @ccb: the ccb which attached to smp task
280 static int pm8001_task_prep_smp(struct pm8001_hba_info
*pm8001_ha
,
281 struct pm8001_ccb_info
*ccb
)
283 return PM8001_CHIP_DISP
->smp_req(pm8001_ha
, ccb
);
286 u32
pm8001_get_ncq_tag(struct sas_task
*task
, u32
*tag
)
288 struct ata_queued_cmd
*qc
= task
->uldd_task
;
290 if (qc
->tf
.command
== ATA_CMD_FPDMA_WRITE
||
291 qc
->tf
.command
== ATA_CMD_FPDMA_READ
) {
300 * pm8001_task_prep_ata - the dispatcher function, prepare data for sata task
301 * @pm8001_ha: our hba card information
302 * @ccb: the ccb which attached to sata task
304 static int pm8001_task_prep_ata(struct pm8001_hba_info
*pm8001_ha
,
305 struct pm8001_ccb_info
*ccb
)
307 return PM8001_CHIP_DISP
->sata_req(pm8001_ha
, ccb
);
311 * pm8001_task_prep_ssp_tm - the dispatcher function, prepare task management data
312 * @pm8001_ha: our hba card information
313 * @ccb: the ccb which attached to TM
314 * @tmf: the task management IU
316 static int pm8001_task_prep_ssp_tm(struct pm8001_hba_info
*pm8001_ha
,
317 struct pm8001_ccb_info
*ccb
, struct pm8001_tmf_task
*tmf
)
319 return PM8001_CHIP_DISP
->ssp_tm_req(pm8001_ha
, ccb
, tmf
);
323 * pm8001_task_prep_ssp - the dispatcher function,prepare ssp data for ssp task
324 * @pm8001_ha: our hba card information
325 * @ccb: the ccb which attached to ssp task
327 static int pm8001_task_prep_ssp(struct pm8001_hba_info
*pm8001_ha
,
328 struct pm8001_ccb_info
*ccb
)
330 return PM8001_CHIP_DISP
->ssp_io_req(pm8001_ha
, ccb
);
333 /* Find the local port id that's attached to this device */
334 static int sas_find_local_port_id(struct domain_device
*dev
)
336 struct domain_device
*pdev
= dev
->parent
;
338 /* Directly attached device */
340 return dev
->port
->id
;
342 struct domain_device
*pdev_p
= pdev
->parent
;
344 return pdev
->port
->id
;
351 * pm8001_task_exec - queue the task(ssp, smp && ata) to the hardware.
352 * @task: the task to be execute.
353 * @num: if can_queue great than 1, the task can be queued up. for SMP task,
354 * we always execute one one time.
355 * @gfp_flags: gfp_flags.
356 * @is_tmf: if it is task management task.
357 * @tmf: the task management IU
359 #define DEV_IS_GONE(pm8001_dev) \
360 ((!pm8001_dev || (pm8001_dev->dev_type == SAS_PHY_UNUSED)))
361 static int pm8001_task_exec(struct sas_task
*task
, const int num
,
362 gfp_t gfp_flags
, int is_tmf
, struct pm8001_tmf_task
*tmf
)
364 struct domain_device
*dev
= task
->dev
;
365 struct pm8001_hba_info
*pm8001_ha
;
366 struct pm8001_device
*pm8001_dev
;
367 struct pm8001_port
*port
= NULL
;
368 struct sas_task
*t
= task
;
369 struct pm8001_ccb_info
*ccb
;
370 u32 tag
= 0xdeadbeef, rc
, n_elem
= 0;
372 unsigned long flags
= 0;
375 struct task_status_struct
*tsm
= &t
->task_status
;
376 tsm
->resp
= SAS_TASK_UNDELIVERED
;
377 tsm
->stat
= SAS_PHY_DOWN
;
378 if (dev
->dev_type
!= SAS_SATA_DEV
)
382 pm8001_ha
= pm8001_find_ha_by_dev(task
->dev
);
383 PM8001_IO_DBG(pm8001_ha
, pm8001_printk("pm8001_task_exec device \n "));
384 spin_lock_irqsave(&pm8001_ha
->lock
, flags
);
387 pm8001_dev
= dev
->lldd_dev
;
388 port
= &pm8001_ha
->port
[sas_find_local_port_id(dev
)];
389 if (DEV_IS_GONE(pm8001_dev
) || !port
->port_attached
) {
390 if (sas_protocol_ata(t
->task_proto
)) {
391 struct task_status_struct
*ts
= &t
->task_status
;
392 ts
->resp
= SAS_TASK_UNDELIVERED
;
393 ts
->stat
= SAS_PHY_DOWN
;
395 spin_unlock_irqrestore(&pm8001_ha
->lock
, flags
);
397 spin_lock_irqsave(&pm8001_ha
->lock
, flags
);
399 t
= list_entry(t
->list
.next
,
400 struct sas_task
, list
);
403 struct task_status_struct
*ts
= &t
->task_status
;
404 ts
->resp
= SAS_TASK_UNDELIVERED
;
405 ts
->stat
= SAS_PHY_DOWN
;
408 t
= list_entry(t
->list
.next
,
409 struct sas_task
, list
);
413 rc
= pm8001_tag_alloc(pm8001_ha
, &tag
);
416 ccb
= &pm8001_ha
->ccb_info
[tag
];
418 if (!sas_protocol_ata(t
->task_proto
)) {
419 if (t
->num_scatter
) {
420 n_elem
= dma_map_sg(pm8001_ha
->dev
,
430 n_elem
= t
->num_scatter
;
434 ccb
->n_elem
= n_elem
;
437 switch (t
->task_proto
) {
438 case SAS_PROTOCOL_SMP
:
439 rc
= pm8001_task_prep_smp(pm8001_ha
, ccb
);
441 case SAS_PROTOCOL_SSP
:
443 rc
= pm8001_task_prep_ssp_tm(pm8001_ha
,
446 rc
= pm8001_task_prep_ssp(pm8001_ha
, ccb
);
448 case SAS_PROTOCOL_SATA
:
449 case SAS_PROTOCOL_STP
:
450 case SAS_PROTOCOL_SATA
| SAS_PROTOCOL_STP
:
451 rc
= pm8001_task_prep_ata(pm8001_ha
, ccb
);
454 dev_printk(KERN_ERR
, pm8001_ha
->dev
,
455 "unknown sas_task proto: 0x%x\n",
462 PM8001_IO_DBG(pm8001_ha
,
463 pm8001_printk("rc is %x\n", rc
));
466 /* TODO: select normal or high priority */
467 spin_lock(&t
->task_state_lock
);
468 t
->task_state_flags
|= SAS_TASK_AT_INITIATOR
;
469 spin_unlock(&t
->task_state_lock
);
470 pm8001_dev
->running_req
++;
472 t
= list_entry(t
->list
.next
, struct sas_task
, list
);
478 pm8001_tag_free(pm8001_ha
, tag
);
480 dev_printk(KERN_ERR
, pm8001_ha
->dev
, "pm8001 exec failed[%d]!\n", rc
);
481 if (!sas_protocol_ata(t
->task_proto
))
483 dma_unmap_sg(pm8001_ha
->dev
, t
->scatter
, n_elem
,
486 spin_unlock_irqrestore(&pm8001_ha
->lock
, flags
);
491 * pm8001_queue_command - register for upper layer used, all IO commands sent
492 * to HBA are from this interface.
493 * @task: the task to be execute.
494 * @num: if can_queue great than 1, the task can be queued up. for SMP task,
495 * we always execute one one time
496 * @gfp_flags: gfp_flags
498 int pm8001_queue_command(struct sas_task
*task
, const int num
,
501 return pm8001_task_exec(task
, num
, gfp_flags
, 0, NULL
);
504 void pm8001_ccb_free(struct pm8001_hba_info
*pm8001_ha
, u32 ccb_idx
)
506 pm8001_tag_clear(pm8001_ha
, ccb_idx
);
510 * pm8001_ccb_task_free - free the sg for ssp and smp command, free the ccb.
511 * @pm8001_ha: our hba card information
512 * @ccb: the ccb which attached to ssp task
513 * @task: the task to be free.
514 * @ccb_idx: ccb index.
516 void pm8001_ccb_task_free(struct pm8001_hba_info
*pm8001_ha
,
517 struct sas_task
*task
, struct pm8001_ccb_info
*ccb
, u32 ccb_idx
)
521 if (!sas_protocol_ata(task
->task_proto
))
523 dma_unmap_sg(pm8001_ha
->dev
, task
->scatter
,
524 task
->num_scatter
, task
->data_dir
);
526 switch (task
->task_proto
) {
527 case SAS_PROTOCOL_SMP
:
528 dma_unmap_sg(pm8001_ha
->dev
, &task
->smp_task
.smp_resp
, 1,
530 dma_unmap_sg(pm8001_ha
->dev
, &task
->smp_task
.smp_req
, 1,
534 case SAS_PROTOCOL_SATA
:
535 case SAS_PROTOCOL_STP
:
536 case SAS_PROTOCOL_SSP
:
541 task
->lldd_task
= NULL
;
543 ccb
->ccb_tag
= 0xFFFFFFFF;
545 pm8001_ccb_free(pm8001_ha
, ccb_idx
);
549 * pm8001_alloc_dev - find a empty pm8001_device
550 * @pm8001_ha: our hba card information
552 struct pm8001_device
*pm8001_alloc_dev(struct pm8001_hba_info
*pm8001_ha
)
555 for (dev
= 0; dev
< PM8001_MAX_DEVICES
; dev
++) {
556 if (pm8001_ha
->devices
[dev
].dev_type
== SAS_PHY_UNUSED
) {
557 pm8001_ha
->devices
[dev
].id
= dev
;
558 return &pm8001_ha
->devices
[dev
];
561 if (dev
== PM8001_MAX_DEVICES
) {
562 PM8001_FAIL_DBG(pm8001_ha
,
563 pm8001_printk("max support %d devices, ignore ..\n",
564 PM8001_MAX_DEVICES
));
569 * pm8001_find_dev - find a matching pm8001_device
570 * @pm8001_ha: our hba card information
572 struct pm8001_device
*pm8001_find_dev(struct pm8001_hba_info
*pm8001_ha
,
576 for (dev
= 0; dev
< PM8001_MAX_DEVICES
; dev
++) {
577 if (pm8001_ha
->devices
[dev
].device_id
== device_id
)
578 return &pm8001_ha
->devices
[dev
];
580 if (dev
== PM8001_MAX_DEVICES
) {
581 PM8001_FAIL_DBG(pm8001_ha
, pm8001_printk("NO MATCHING "
582 "DEVICE FOUND !!!\n"));
587 static void pm8001_free_dev(struct pm8001_device
*pm8001_dev
)
589 u32 id
= pm8001_dev
->id
;
590 memset(pm8001_dev
, 0, sizeof(*pm8001_dev
));
592 pm8001_dev
->dev_type
= SAS_PHY_UNUSED
;
593 pm8001_dev
->device_id
= PM8001_MAX_DEVICES
;
594 pm8001_dev
->sas_device
= NULL
;
598 * pm8001_dev_found_notify - libsas notify a device is found.
599 * @dev: the device structure which sas layer used.
601 * when libsas find a sas domain device, it should tell the LLDD that
602 * device is found, and then LLDD register this device to HBA firmware
603 * by the command "OPC_INB_REG_DEV", after that the HBA will assign a
604 * device ID(according to device's sas address) and returned it to LLDD. From
605 * now on, we communicate with HBA FW with the device ID which HBA assigned
606 * rather than sas address. it is the necessary step for our HBA but it is
607 * the optional for other HBA driver.
609 static int pm8001_dev_found_notify(struct domain_device
*dev
)
611 unsigned long flags
= 0;
613 struct pm8001_hba_info
*pm8001_ha
= NULL
;
614 struct domain_device
*parent_dev
= dev
->parent
;
615 struct pm8001_device
*pm8001_device
;
616 DECLARE_COMPLETION_ONSTACK(completion
);
618 pm8001_ha
= pm8001_find_ha_by_dev(dev
);
619 spin_lock_irqsave(&pm8001_ha
->lock
, flags
);
621 pm8001_device
= pm8001_alloc_dev(pm8001_ha
);
622 if (!pm8001_device
) {
626 pm8001_device
->sas_device
= dev
;
627 dev
->lldd_dev
= pm8001_device
;
628 pm8001_device
->dev_type
= dev
->dev_type
;
629 pm8001_device
->dcompletion
= &completion
;
630 if (parent_dev
&& DEV_IS_EXPANDER(parent_dev
->dev_type
)) {
633 for (phy_id
= 0; phy_id
< parent_dev
->ex_dev
.num_phys
;
635 phy
= &parent_dev
->ex_dev
.ex_phy
[phy_id
];
636 if (SAS_ADDR(phy
->attached_sas_addr
)
637 == SAS_ADDR(dev
->sas_addr
)) {
638 pm8001_device
->attached_phy
= phy_id
;
642 if (phy_id
== parent_dev
->ex_dev
.num_phys
) {
643 PM8001_FAIL_DBG(pm8001_ha
,
644 pm8001_printk("Error: no attached dev:%016llx"
645 " at ex:%016llx.\n", SAS_ADDR(dev
->sas_addr
),
646 SAS_ADDR(parent_dev
->sas_addr
)));
650 if (dev
->dev_type
== SAS_SATA_DEV
) {
651 pm8001_device
->attached_phy
=
652 dev
->rphy
->identify
.phy_identifier
;
653 flag
= 1; /* directly sata*/
655 } /*register this device to HBA*/
656 PM8001_DISC_DBG(pm8001_ha
, pm8001_printk("Found device\n"));
657 PM8001_CHIP_DISP
->reg_dev_req(pm8001_ha
, pm8001_device
, flag
);
658 spin_unlock_irqrestore(&pm8001_ha
->lock
, flags
);
659 wait_for_completion(&completion
);
660 if (dev
->dev_type
== SAS_END_DEVICE
)
662 pm8001_ha
->flags
= PM8001F_RUN_TIME
;
665 spin_unlock_irqrestore(&pm8001_ha
->lock
, flags
);
669 int pm8001_dev_found(struct domain_device
*dev
)
671 return pm8001_dev_found_notify(dev
);
674 void pm8001_task_done(struct sas_task
*task
)
676 if (!del_timer(&task
->slow_task
->timer
))
678 complete(&task
->slow_task
->completion
);
681 static void pm8001_tmf_timedout(unsigned long data
)
683 struct sas_task
*task
= (struct sas_task
*)data
;
685 task
->task_state_flags
|= SAS_TASK_STATE_ABORTED
;
686 complete(&task
->slow_task
->completion
);
689 #define PM8001_TASK_TIMEOUT 20
691 * pm8001_exec_internal_tmf_task - execute some task management commands.
692 * @dev: the wanted device.
693 * @tmf: which task management wanted to be take.
694 * @para_len: para_len.
695 * @parameter: ssp task parameter.
697 * when errors or exception happened, we may want to do something, for example
698 * abort the issued task which result in this execption, it is done by calling
699 * this function, note it is also with the task execute interface.
701 static int pm8001_exec_internal_tmf_task(struct domain_device
*dev
,
702 void *parameter
, u32 para_len
, struct pm8001_tmf_task
*tmf
)
705 struct sas_task
*task
= NULL
;
706 struct pm8001_hba_info
*pm8001_ha
= pm8001_find_ha_by_dev(dev
);
708 for (retry
= 0; retry
< 3; retry
++) {
709 task
= sas_alloc_slow_task(GFP_KERNEL
);
714 task
->task_proto
= dev
->tproto
;
715 memcpy(&task
->ssp_task
, parameter
, para_len
);
716 task
->task_done
= pm8001_task_done
;
717 task
->slow_task
->timer
.data
= (unsigned long)task
;
718 task
->slow_task
->timer
.function
= pm8001_tmf_timedout
;
719 task
->slow_task
->timer
.expires
= jiffies
+ PM8001_TASK_TIMEOUT
*HZ
;
720 add_timer(&task
->slow_task
->timer
);
722 res
= pm8001_task_exec(task
, 1, GFP_KERNEL
, 1, tmf
);
725 del_timer(&task
->slow_task
->timer
);
726 PM8001_FAIL_DBG(pm8001_ha
,
727 pm8001_printk("Executing internal task "
731 wait_for_completion(&task
->slow_task
->completion
);
732 res
= -TMF_RESP_FUNC_FAILED
;
733 /* Even TMF timed out, return direct. */
734 if ((task
->task_state_flags
& SAS_TASK_STATE_ABORTED
)) {
735 if (!(task
->task_state_flags
& SAS_TASK_STATE_DONE
)) {
736 PM8001_FAIL_DBG(pm8001_ha
,
737 pm8001_printk("TMF task[%x]timeout.\n",
743 if (task
->task_status
.resp
== SAS_TASK_COMPLETE
&&
744 task
->task_status
.stat
== SAM_STAT_GOOD
) {
745 res
= TMF_RESP_FUNC_COMPLETE
;
749 if (task
->task_status
.resp
== SAS_TASK_COMPLETE
&&
750 task
->task_status
.stat
== SAS_DATA_UNDERRUN
) {
751 /* no error, but return the number of bytes of
753 res
= task
->task_status
.residual
;
757 if (task
->task_status
.resp
== SAS_TASK_COMPLETE
&&
758 task
->task_status
.stat
== SAS_DATA_OVERRUN
) {
759 PM8001_FAIL_DBG(pm8001_ha
,
760 pm8001_printk("Blocked task error.\n"));
764 PM8001_EH_DBG(pm8001_ha
,
765 pm8001_printk(" Task to dev %016llx response:"
766 "0x%x status 0x%x\n",
767 SAS_ADDR(dev
->sas_addr
),
768 task
->task_status
.resp
,
769 task
->task_status
.stat
));
775 BUG_ON(retry
== 3 && task
!= NULL
);
781 pm8001_exec_internal_task_abort(struct pm8001_hba_info
*pm8001_ha
,
782 struct pm8001_device
*pm8001_dev
, struct domain_device
*dev
, u32 flag
,
787 struct pm8001_ccb_info
*ccb
;
788 struct sas_task
*task
= NULL
;
790 for (retry
= 0; retry
< 3; retry
++) {
791 task
= sas_alloc_slow_task(GFP_KERNEL
);
796 task
->task_proto
= dev
->tproto
;
797 task
->task_done
= pm8001_task_done
;
798 task
->slow_task
->timer
.data
= (unsigned long)task
;
799 task
->slow_task
->timer
.function
= pm8001_tmf_timedout
;
800 task
->slow_task
->timer
.expires
= jiffies
+ PM8001_TASK_TIMEOUT
* HZ
;
801 add_timer(&task
->slow_task
->timer
);
803 res
= pm8001_tag_alloc(pm8001_ha
, &ccb_tag
);
806 ccb
= &pm8001_ha
->ccb_info
[ccb_tag
];
807 ccb
->device
= pm8001_dev
;
808 ccb
->ccb_tag
= ccb_tag
;
811 res
= PM8001_CHIP_DISP
->task_abort(pm8001_ha
,
812 pm8001_dev
, flag
, task_tag
, ccb_tag
);
815 del_timer(&task
->slow_task
->timer
);
816 PM8001_FAIL_DBG(pm8001_ha
,
817 pm8001_printk("Executing internal task "
821 wait_for_completion(&task
->slow_task
->completion
);
822 res
= TMF_RESP_FUNC_FAILED
;
823 /* Even TMF timed out, return direct. */
824 if ((task
->task_state_flags
& SAS_TASK_STATE_ABORTED
)) {
825 if (!(task
->task_state_flags
& SAS_TASK_STATE_DONE
)) {
826 PM8001_FAIL_DBG(pm8001_ha
,
827 pm8001_printk("TMF task timeout.\n"));
832 if (task
->task_status
.resp
== SAS_TASK_COMPLETE
&&
833 task
->task_status
.stat
== SAM_STAT_GOOD
) {
834 res
= TMF_RESP_FUNC_COMPLETE
;
838 PM8001_EH_DBG(pm8001_ha
,
839 pm8001_printk(" Task to dev %016llx response: "
840 "0x%x status 0x%x\n",
841 SAS_ADDR(dev
->sas_addr
),
842 task
->task_status
.resp
,
843 task
->task_status
.stat
));
849 BUG_ON(retry
== 3 && task
!= NULL
);
855 * pm8001_dev_gone_notify - see the comments for "pm8001_dev_found_notify"
856 * @dev: the device structure which sas layer used.
858 static void pm8001_dev_gone_notify(struct domain_device
*dev
)
860 unsigned long flags
= 0;
862 struct pm8001_hba_info
*pm8001_ha
;
863 struct pm8001_device
*pm8001_dev
= dev
->lldd_dev
;
865 pm8001_ha
= pm8001_find_ha_by_dev(dev
);
866 spin_lock_irqsave(&pm8001_ha
->lock
, flags
);
867 pm8001_tag_alloc(pm8001_ha
, &tag
);
869 u32 device_id
= pm8001_dev
->device_id
;
871 PM8001_DISC_DBG(pm8001_ha
,
872 pm8001_printk("found dev[%d:%x] is gone.\n",
873 pm8001_dev
->device_id
, pm8001_dev
->dev_type
));
874 if (pm8001_dev
->running_req
) {
875 spin_unlock_irqrestore(&pm8001_ha
->lock
, flags
);
876 pm8001_exec_internal_task_abort(pm8001_ha
, pm8001_dev
,
878 spin_lock_irqsave(&pm8001_ha
->lock
, flags
);
880 PM8001_CHIP_DISP
->dereg_dev_req(pm8001_ha
, device_id
);
881 pm8001_free_dev(pm8001_dev
);
883 PM8001_DISC_DBG(pm8001_ha
,
884 pm8001_printk("Found dev has gone.\n"));
886 dev
->lldd_dev
= NULL
;
887 spin_unlock_irqrestore(&pm8001_ha
->lock
, flags
);
890 void pm8001_dev_gone(struct domain_device
*dev
)
892 pm8001_dev_gone_notify(dev
);
895 static int pm8001_issue_ssp_tmf(struct domain_device
*dev
,
896 u8
*lun
, struct pm8001_tmf_task
*tmf
)
898 struct sas_ssp_task ssp_task
;
899 if (!(dev
->tproto
& SAS_PROTOCOL_SSP
))
900 return TMF_RESP_FUNC_ESUPP
;
902 strncpy((u8
*)&ssp_task
.LUN
, lun
, 8);
903 return pm8001_exec_internal_tmf_task(dev
, &ssp_task
, sizeof(ssp_task
),
907 /* retry commands by ha, by task and/or by device */
908 void pm8001_open_reject_retry(
909 struct pm8001_hba_info
*pm8001_ha
,
910 struct sas_task
*task_to_close
,
911 struct pm8001_device
*device_to_close
)
916 if (pm8001_ha
== NULL
)
919 spin_lock_irqsave(&pm8001_ha
->lock
, flags
);
921 for (i
= 0; i
< PM8001_MAX_CCB
; i
++) {
922 struct sas_task
*task
;
923 struct task_status_struct
*ts
;
924 struct pm8001_device
*pm8001_dev
;
925 unsigned long flags1
;
927 struct pm8001_ccb_info
*ccb
= &pm8001_ha
->ccb_info
[i
];
929 pm8001_dev
= ccb
->device
;
930 if (!pm8001_dev
|| (pm8001_dev
->dev_type
== SAS_PHY_UNUSED
))
932 if (!device_to_close
) {
933 uintptr_t d
= (uintptr_t)pm8001_dev
934 - (uintptr_t)&pm8001_ha
->devices
;
935 if (((d
% sizeof(*pm8001_dev
)) != 0)
936 || ((d
/ sizeof(*pm8001_dev
)) >= PM8001_MAX_DEVICES
))
938 } else if (pm8001_dev
!= device_to_close
)
941 if (!tag
|| (tag
== 0xFFFFFFFF))
944 if (!task
|| !task
->task_done
)
946 if (task_to_close
&& (task
!= task_to_close
))
948 ts
= &task
->task_status
;
949 ts
->resp
= SAS_TASK_COMPLETE
;
950 /* Force the midlayer to retry */
951 ts
->stat
= SAS_OPEN_REJECT
;
952 ts
->open_rej_reason
= SAS_OREJ_RSVD_RETRY
;
954 pm8001_dev
->running_req
--;
955 spin_lock_irqsave(&task
->task_state_lock
, flags1
);
956 task
->task_state_flags
&= ~SAS_TASK_STATE_PENDING
;
957 task
->task_state_flags
&= ~SAS_TASK_AT_INITIATOR
;
958 task
->task_state_flags
|= SAS_TASK_STATE_DONE
;
959 if (unlikely((task
->task_state_flags
960 & SAS_TASK_STATE_ABORTED
))) {
961 spin_unlock_irqrestore(&task
->task_state_lock
,
963 pm8001_ccb_task_free(pm8001_ha
, task
, ccb
, tag
);
965 spin_unlock_irqrestore(&task
->task_state_lock
,
967 pm8001_ccb_task_free(pm8001_ha
, task
, ccb
, tag
);
968 mb();/* in order to force CPU ordering */
969 spin_unlock_irqrestore(&pm8001_ha
->lock
, flags
);
970 task
->task_done(task
);
971 spin_lock_irqsave(&pm8001_ha
->lock
, flags
);
975 spin_unlock_irqrestore(&pm8001_ha
->lock
, flags
);
979 * Standard mandates link reset for ATA (type 0) and hard reset for
980 * SSP (type 1) , only for RECOVERY
982 int pm8001_I_T_nexus_reset(struct domain_device
*dev
)
984 int rc
= TMF_RESP_FUNC_FAILED
;
985 struct pm8001_device
*pm8001_dev
;
986 struct pm8001_hba_info
*pm8001_ha
;
989 if (!dev
|| !dev
->lldd_dev
)
992 pm8001_dev
= dev
->lldd_dev
;
993 pm8001_ha
= pm8001_find_ha_by_dev(dev
);
994 phy
= sas_get_local_phy(dev
);
996 if (dev_is_sata(dev
)) {
997 DECLARE_COMPLETION_ONSTACK(completion_setstate
);
998 if (scsi_is_sas_phy_local(phy
)) {
1002 rc
= sas_phy_reset(phy
, 1);
1004 rc
= pm8001_exec_internal_task_abort(pm8001_ha
, pm8001_dev
,
1006 pm8001_dev
->setds_completion
= &completion_setstate
;
1007 rc
= PM8001_CHIP_DISP
->set_dev_state_req(pm8001_ha
,
1009 wait_for_completion(&completion_setstate
);
1011 rc
= sas_phy_reset(phy
, 1);
1014 PM8001_EH_DBG(pm8001_ha
, pm8001_printk(" for device[%x]:rc=%d\n",
1015 pm8001_dev
->device_id
, rc
));
1017 sas_put_local_phy(phy
);
1022 * This function handle the IT_NEXUS_XXX event or completion
1023 * status code for SSP/SATA/SMP I/O request.
1025 int pm8001_I_T_nexus_event_handler(struct domain_device
*dev
)
1027 int rc
= TMF_RESP_FUNC_FAILED
;
1028 struct pm8001_device
*pm8001_dev
;
1029 struct pm8001_hba_info
*pm8001_ha
;
1030 struct sas_phy
*phy
;
1033 if (!dev
|| !dev
->lldd_dev
)
1036 pm8001_dev
= dev
->lldd_dev
;
1037 device_id
= pm8001_dev
->device_id
;
1038 pm8001_ha
= pm8001_find_ha_by_dev(dev
);
1040 PM8001_EH_DBG(pm8001_ha
,
1041 pm8001_printk("I_T_Nexus handler invoked !!"));
1043 phy
= sas_get_local_phy(dev
);
1045 if (dev_is_sata(dev
)) {
1046 DECLARE_COMPLETION_ONSTACK(completion_setstate
);
1047 if (scsi_is_sas_phy_local(phy
)) {
1051 /* send internal ssp/sata/smp abort command to FW */
1052 rc
= pm8001_exec_internal_task_abort(pm8001_ha
, pm8001_dev
,
1056 /* deregister the target device */
1057 pm8001_dev_gone_notify(dev
);
1060 /*send phy reset to hard reset target */
1061 rc
= sas_phy_reset(phy
, 1);
1063 pm8001_dev
->setds_completion
= &completion_setstate
;
1065 wait_for_completion(&completion_setstate
);
1067 /* send internal ssp/sata/smp abort command to FW */
1068 rc
= pm8001_exec_internal_task_abort(pm8001_ha
, pm8001_dev
,
1072 /* deregister the target device */
1073 pm8001_dev_gone_notify(dev
);
1076 /*send phy reset to hard reset target */
1077 rc
= sas_phy_reset(phy
, 1);
1080 PM8001_EH_DBG(pm8001_ha
, pm8001_printk(" for device[%x]:rc=%d\n",
1081 pm8001_dev
->device_id
, rc
));
1083 sas_put_local_phy(phy
);
1087 /* mandatory SAM-3, the task reset the specified LUN*/
1088 int pm8001_lu_reset(struct domain_device
*dev
, u8
*lun
)
1090 int rc
= TMF_RESP_FUNC_FAILED
;
1091 struct pm8001_tmf_task tmf_task
;
1092 struct pm8001_device
*pm8001_dev
= dev
->lldd_dev
;
1093 struct pm8001_hba_info
*pm8001_ha
= pm8001_find_ha_by_dev(dev
);
1094 if (dev_is_sata(dev
)) {
1095 struct sas_phy
*phy
= sas_get_local_phy(dev
);
1096 rc
= pm8001_exec_internal_task_abort(pm8001_ha
, pm8001_dev
,
1098 rc
= sas_phy_reset(phy
, 1);
1099 sas_put_local_phy(phy
);
1100 rc
= PM8001_CHIP_DISP
->set_dev_state_req(pm8001_ha
,
1104 tmf_task
.tmf
= TMF_LU_RESET
;
1105 rc
= pm8001_issue_ssp_tmf(dev
, lun
, &tmf_task
);
1107 /* If failed, fall-through I_T_Nexus reset */
1108 PM8001_EH_DBG(pm8001_ha
, pm8001_printk("for device[%x]:rc=%d\n",
1109 pm8001_dev
->device_id
, rc
));
1113 /* optional SAM-3 */
1114 int pm8001_query_task(struct sas_task
*task
)
1116 u32 tag
= 0xdeadbeef;
1118 struct scsi_lun lun
;
1119 struct pm8001_tmf_task tmf_task
;
1120 int rc
= TMF_RESP_FUNC_FAILED
;
1121 if (unlikely(!task
|| !task
->lldd_task
|| !task
->dev
))
1124 if (task
->task_proto
& SAS_PROTOCOL_SSP
) {
1125 struct scsi_cmnd
*cmnd
= task
->uldd_task
;
1126 struct domain_device
*dev
= task
->dev
;
1127 struct pm8001_hba_info
*pm8001_ha
=
1128 pm8001_find_ha_by_dev(dev
);
1130 int_to_scsilun(cmnd
->device
->lun
, &lun
);
1131 rc
= pm8001_find_tag(task
, &tag
);
1133 rc
= TMF_RESP_FUNC_FAILED
;
1136 PM8001_EH_DBG(pm8001_ha
, pm8001_printk("Query:["));
1137 for (i
= 0; i
< 16; i
++)
1138 printk(KERN_INFO
"%02x ", cmnd
->cmnd
[i
]);
1139 printk(KERN_INFO
"]\n");
1140 tmf_task
.tmf
= TMF_QUERY_TASK
;
1141 tmf_task
.tag_of_task_to_be_managed
= tag
;
1143 rc
= pm8001_issue_ssp_tmf(dev
, lun
.scsi_lun
, &tmf_task
);
1145 /* The task is still in Lun, release it then */
1146 case TMF_RESP_FUNC_SUCC
:
1147 PM8001_EH_DBG(pm8001_ha
,
1148 pm8001_printk("The task is still in Lun\n"));
1150 /* The task is not in Lun or failed, reset the phy */
1151 case TMF_RESP_FUNC_FAILED
:
1152 case TMF_RESP_FUNC_COMPLETE
:
1153 PM8001_EH_DBG(pm8001_ha
,
1154 pm8001_printk("The task is not in Lun or failed,"
1155 " reset the phy\n"));
1159 pm8001_printk(":rc= %d\n", rc
);
1163 /* mandatory SAM-3, still need free task/ccb info, abord the specified task */
1164 int pm8001_abort_task(struct sas_task
*task
)
1166 unsigned long flags
;
1167 u32 tag
= 0xdeadbeef;
1169 struct domain_device
*dev
;
1170 struct pm8001_hba_info
*pm8001_ha
= NULL
;
1171 struct pm8001_ccb_info
*ccb
;
1172 struct scsi_lun lun
;
1173 struct pm8001_device
*pm8001_dev
;
1174 struct pm8001_tmf_task tmf_task
;
1175 int rc
= TMF_RESP_FUNC_FAILED
;
1176 if (unlikely(!task
|| !task
->lldd_task
|| !task
->dev
))
1178 spin_lock_irqsave(&task
->task_state_lock
, flags
);
1179 if (task
->task_state_flags
& SAS_TASK_STATE_DONE
) {
1180 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
1181 rc
= TMF_RESP_FUNC_COMPLETE
;
1184 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
1185 if (task
->task_proto
& SAS_PROTOCOL_SSP
) {
1186 struct scsi_cmnd
*cmnd
= task
->uldd_task
;
1188 ccb
= task
->lldd_task
;
1189 pm8001_dev
= dev
->lldd_dev
;
1190 pm8001_ha
= pm8001_find_ha_by_dev(dev
);
1191 int_to_scsilun(cmnd
->device
->lun
, &lun
);
1192 rc
= pm8001_find_tag(task
, &tag
);
1194 printk(KERN_INFO
"No such tag in %s\n", __func__
);
1195 rc
= TMF_RESP_FUNC_FAILED
;
1198 device_id
= pm8001_dev
->device_id
;
1199 PM8001_EH_DBG(pm8001_ha
,
1200 pm8001_printk("abort io to deviceid= %d\n", device_id
));
1201 tmf_task
.tmf
= TMF_ABORT_TASK
;
1202 tmf_task
.tag_of_task_to_be_managed
= tag
;
1203 rc
= pm8001_issue_ssp_tmf(dev
, lun
.scsi_lun
, &tmf_task
);
1204 pm8001_exec_internal_task_abort(pm8001_ha
, pm8001_dev
,
1205 pm8001_dev
->sas_device
, 0, tag
);
1206 } else if (task
->task_proto
& SAS_PROTOCOL_SATA
||
1207 task
->task_proto
& SAS_PROTOCOL_STP
) {
1209 pm8001_dev
= dev
->lldd_dev
;
1210 pm8001_ha
= pm8001_find_ha_by_dev(dev
);
1211 rc
= pm8001_find_tag(task
, &tag
);
1213 printk(KERN_INFO
"No such tag in %s\n", __func__
);
1214 rc
= TMF_RESP_FUNC_FAILED
;
1217 rc
= pm8001_exec_internal_task_abort(pm8001_ha
, pm8001_dev
,
1218 pm8001_dev
->sas_device
, 0, tag
);
1219 } else if (task
->task_proto
& SAS_PROTOCOL_SMP
) {
1222 pm8001_dev
= dev
->lldd_dev
;
1223 pm8001_ha
= pm8001_find_ha_by_dev(dev
);
1224 rc
= pm8001_find_tag(task
, &tag
);
1226 printk(KERN_INFO
"No such tag in %s\n", __func__
);
1227 rc
= TMF_RESP_FUNC_FAILED
;
1230 rc
= pm8001_exec_internal_task_abort(pm8001_ha
, pm8001_dev
,
1231 pm8001_dev
->sas_device
, 0, tag
);
1235 if (rc
!= TMF_RESP_FUNC_COMPLETE
)
1236 pm8001_printk("rc= %d\n", rc
);
1240 int pm8001_abort_task_set(struct domain_device
*dev
, u8
*lun
)
1242 int rc
= TMF_RESP_FUNC_FAILED
;
1243 struct pm8001_tmf_task tmf_task
;
1245 tmf_task
.tmf
= TMF_ABORT_TASK_SET
;
1246 rc
= pm8001_issue_ssp_tmf(dev
, lun
, &tmf_task
);
1250 int pm8001_clear_aca(struct domain_device
*dev
, u8
*lun
)
1252 int rc
= TMF_RESP_FUNC_FAILED
;
1253 struct pm8001_tmf_task tmf_task
;
1255 tmf_task
.tmf
= TMF_CLEAR_ACA
;
1256 rc
= pm8001_issue_ssp_tmf(dev
, lun
, &tmf_task
);
1261 int pm8001_clear_task_set(struct domain_device
*dev
, u8
*lun
)
1263 int rc
= TMF_RESP_FUNC_FAILED
;
1264 struct pm8001_tmf_task tmf_task
;
1265 struct pm8001_device
*pm8001_dev
= dev
->lldd_dev
;
1266 struct pm8001_hba_info
*pm8001_ha
= pm8001_find_ha_by_dev(dev
);
1268 PM8001_EH_DBG(pm8001_ha
,
1269 pm8001_printk("I_T_L_Q clear task set[%x]\n",
1270 pm8001_dev
->device_id
));
1271 tmf_task
.tmf
= TMF_CLEAR_TASK_SET
;
1272 rc
= pm8001_issue_ssp_tmf(dev
, lun
, &tmf_task
);