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 "pm8001_sas.h"
44 * pm8001_find_tag - from sas task to find out tag that belongs to this task
45 * @task: the task sent to the LLDD
46 * @tag: the found tag associated with the task
48 static int pm8001_find_tag(struct sas_task
*task
, u32
*tag
)
50 if (task
->lldd_task
) {
51 struct pm8001_ccb_info
*ccb
;
52 ccb
= task
->lldd_task
;
60 * pm8001_tag_clear - clear the tags bitmap
61 * @pm8001_ha: our hba struct
62 * @tag: the found tag associated with the task
64 static void pm8001_tag_clear(struct pm8001_hba_info
*pm8001_ha
, u32 tag
)
66 void *bitmap
= pm8001_ha
->tags
;
67 clear_bit(tag
, bitmap
);
70 static void pm8001_tag_free(struct pm8001_hba_info
*pm8001_ha
, u32 tag
)
72 pm8001_tag_clear(pm8001_ha
, tag
);
75 static void pm8001_tag_set(struct pm8001_hba_info
*pm8001_ha
, u32 tag
)
77 void *bitmap
= pm8001_ha
->tags
;
82 * pm8001_tag_alloc - allocate a empty tag for task used.
83 * @pm8001_ha: our hba struct
84 * @tag_out: the found empty tag .
86 inline int pm8001_tag_alloc(struct pm8001_hba_info
*pm8001_ha
, u32
*tag_out
)
88 unsigned int index
, tag
;
89 void *bitmap
= pm8001_ha
->tags
;
91 index
= find_first_zero_bit(bitmap
, pm8001_ha
->tags_num
);
93 if (tag
>= pm8001_ha
->tags_num
)
94 return -SAS_QUEUE_FULL
;
95 pm8001_tag_set(pm8001_ha
, tag
);
100 void pm8001_tag_init(struct pm8001_hba_info
*pm8001_ha
)
103 for (i
= 0; i
< pm8001_ha
->tags_num
; ++i
)
104 pm8001_tag_clear(pm8001_ha
, i
);
108 * pm8001_mem_alloc - allocate memory for pm8001.
110 * @virt_addr: the allocated virtual address
111 * @pphys_addr_hi: the physical address high byte address.
112 * @pphys_addr_lo: the physical address low byte address.
113 * @mem_size: memory size.
115 int pm8001_mem_alloc(struct pci_dev
*pdev
, void **virt_addr
,
116 dma_addr_t
*pphys_addr
, u32
*pphys_addr_hi
,
117 u32
*pphys_addr_lo
, u32 mem_size
, u32 align
)
119 caddr_t mem_virt_alloc
;
120 dma_addr_t mem_dma_handle
;
122 u64 align_offset
= 0;
124 align_offset
= (dma_addr_t
)align
- 1;
126 pci_alloc_consistent(pdev
, mem_size
+ align
, &mem_dma_handle
);
127 if (!mem_virt_alloc
) {
128 pm8001_printk("memory allocation error\n");
131 memset((void *)mem_virt_alloc
, 0, mem_size
+align
);
132 *pphys_addr
= mem_dma_handle
;
133 phys_align
= (*pphys_addr
+ align_offset
) & ~align_offset
;
134 *virt_addr
= (void *)mem_virt_alloc
+ phys_align
- *pphys_addr
;
135 *pphys_addr_hi
= upper_32_bits(phys_align
);
136 *pphys_addr_lo
= lower_32_bits(phys_align
);
140 * pm8001_find_ha_by_dev - from domain device which come from sas layer to
141 * find out our hba struct.
142 * @dev: the domain device which from sas layer.
145 struct pm8001_hba_info
*pm8001_find_ha_by_dev(struct domain_device
*dev
)
147 struct sas_ha_struct
*sha
= dev
->port
->ha
;
148 struct pm8001_hba_info
*pm8001_ha
= sha
->lldd_ha
;
153 * pm8001_phy_control - this function should be registered to
154 * sas_domain_function_template to provide libsas used, note: this is just
155 * control the HBA phy rather than other expander phy if you want control
156 * other phy, you should use SMP command.
157 * @sas_phy: which phy in HBA phys.
158 * @func: the operation.
159 * @funcdata: always NULL.
161 int pm8001_phy_control(struct asd_sas_phy
*sas_phy
, enum phy_func func
,
164 int rc
= 0, phy_id
= sas_phy
->id
;
165 struct pm8001_hba_info
*pm8001_ha
= NULL
;
166 struct sas_phy_linkrates
*rates
;
167 DECLARE_COMPLETION_ONSTACK(completion
);
168 pm8001_ha
= sas_phy
->ha
->lldd_ha
;
169 pm8001_ha
->phy
[phy_id
].enable_completion
= &completion
;
171 case PHY_FUNC_SET_LINK_RATE
:
173 if (rates
->minimum_linkrate
) {
174 pm8001_ha
->phy
[phy_id
].minimum_linkrate
=
175 rates
->minimum_linkrate
;
177 if (rates
->maximum_linkrate
) {
178 pm8001_ha
->phy
[phy_id
].maximum_linkrate
=
179 rates
->maximum_linkrate
;
181 if (pm8001_ha
->phy
[phy_id
].phy_state
== 0) {
182 PM8001_CHIP_DISP
->phy_start_req(pm8001_ha
, phy_id
);
183 wait_for_completion(&completion
);
185 PM8001_CHIP_DISP
->phy_ctl_req(pm8001_ha
, phy_id
,
188 case PHY_FUNC_HARD_RESET
:
189 if (pm8001_ha
->phy
[phy_id
].phy_state
== 0) {
190 PM8001_CHIP_DISP
->phy_start_req(pm8001_ha
, phy_id
);
191 wait_for_completion(&completion
);
193 PM8001_CHIP_DISP
->phy_ctl_req(pm8001_ha
, phy_id
,
196 case PHY_FUNC_LINK_RESET
:
197 if (pm8001_ha
->phy
[phy_id
].phy_state
== 0) {
198 PM8001_CHIP_DISP
->phy_start_req(pm8001_ha
, phy_id
);
199 wait_for_completion(&completion
);
201 PM8001_CHIP_DISP
->phy_ctl_req(pm8001_ha
, phy_id
,
204 case PHY_FUNC_RELEASE_SPINUP_HOLD
:
205 PM8001_CHIP_DISP
->phy_ctl_req(pm8001_ha
, phy_id
,
208 case PHY_FUNC_DISABLE
:
209 PM8001_CHIP_DISP
->phy_stop_req(pm8001_ha
, phy_id
);
218 int pm8001_slave_alloc(struct scsi_device
*scsi_dev
)
220 struct domain_device
*dev
= sdev_to_domain_dev(scsi_dev
);
221 if (dev_is_sata(dev
)) {
222 /* We don't need to rescan targets
223 * if REPORT_LUNS request is failed
225 if (scsi_dev
->lun
> 0)
227 scsi_dev
->tagged_supported
= 1;
229 return sas_slave_alloc(scsi_dev
);
233 * pm8001_scan_start - we should enable all HBA phys by sending the phy_start
235 * @shost: the scsi host data.
237 void pm8001_scan_start(struct Scsi_Host
*shost
)
240 struct pm8001_hba_info
*pm8001_ha
;
241 struct sas_ha_struct
*sha
= SHOST_TO_SAS_HA(shost
);
242 pm8001_ha
= sha
->lldd_ha
;
243 PM8001_CHIP_DISP
->sas_re_init_req(pm8001_ha
);
244 for (i
= 0; i
< pm8001_ha
->chip
->n_phy
; ++i
)
245 PM8001_CHIP_DISP
->phy_start_req(pm8001_ha
, i
);
248 int pm8001_scan_finished(struct Scsi_Host
*shost
, unsigned long time
)
250 /* give the phy enabling interrupt event time to come in (1s
251 * is empirically about all it takes) */
254 /* Wait for discovery to finish */
255 scsi_flush_work(shost
);
260 * pm8001_task_prep_smp - the dispatcher function, prepare data for smp task
261 * @pm8001_ha: our hba card information
262 * @ccb: the ccb which attached to smp task
264 static int pm8001_task_prep_smp(struct pm8001_hba_info
*pm8001_ha
,
265 struct pm8001_ccb_info
*ccb
)
267 return PM8001_CHIP_DISP
->smp_req(pm8001_ha
, ccb
);
270 u32
pm8001_get_ncq_tag(struct sas_task
*task
, u32
*tag
)
272 struct ata_queued_cmd
*qc
= task
->uldd_task
;
274 if (qc
->tf
.command
== ATA_CMD_FPDMA_WRITE
||
275 qc
->tf
.command
== ATA_CMD_FPDMA_READ
) {
284 * pm8001_task_prep_ata - the dispatcher function, prepare data for sata task
285 * @pm8001_ha: our hba card information
286 * @ccb: the ccb which attached to sata task
288 static int pm8001_task_prep_ata(struct pm8001_hba_info
*pm8001_ha
,
289 struct pm8001_ccb_info
*ccb
)
291 return PM8001_CHIP_DISP
->sata_req(pm8001_ha
, ccb
);
295 * pm8001_task_prep_ssp_tm - the dispatcher function, prepare task management data
296 * @pm8001_ha: our hba card information
297 * @ccb: the ccb which attached to TM
298 * @tmf: the task management IU
300 static int pm8001_task_prep_ssp_tm(struct pm8001_hba_info
*pm8001_ha
,
301 struct pm8001_ccb_info
*ccb
, struct pm8001_tmf_task
*tmf
)
303 return PM8001_CHIP_DISP
->ssp_tm_req(pm8001_ha
, ccb
, tmf
);
307 * pm8001_task_prep_ssp - the dispatcher function,prepare ssp data for ssp task
308 * @pm8001_ha: our hba card information
309 * @ccb: the ccb which attached to ssp task
311 static int pm8001_task_prep_ssp(struct pm8001_hba_info
*pm8001_ha
,
312 struct pm8001_ccb_info
*ccb
)
314 return PM8001_CHIP_DISP
->ssp_io_req(pm8001_ha
, ccb
);
316 int pm8001_slave_configure(struct scsi_device
*sdev
)
318 struct domain_device
*dev
= sdev_to_domain_dev(sdev
);
319 int ret
= sas_slave_configure(sdev
);
322 if (dev_is_sata(dev
)) {
323 #ifdef PM8001_DISABLE_NCQ
324 struct ata_port
*ap
= dev
->sata_dev
.ap
;
325 struct ata_device
*adev
= ap
->link
.device
;
326 adev
->flags
|= ATA_DFLAG_NCQ_OFF
;
327 scsi_adjust_queue_depth(sdev
, MSG_SIMPLE_TAG
, 1);
332 /* Find the local port id that's attached to this device */
333 static int sas_find_local_port_id(struct domain_device
*dev
)
335 struct domain_device
*pdev
= dev
->parent
;
337 /* Directly attached device */
339 return dev
->port
->id
;
341 struct domain_device
*pdev_p
= pdev
->parent
;
343 return pdev
->port
->id
;
350 * pm8001_task_exec - queue the task(ssp, smp && ata) to the hardware.
351 * @task: the task to be execute.
352 * @num: if can_queue great than 1, the task can be queued up. for SMP task,
353 * we always execute one one time.
354 * @gfp_flags: gfp_flags.
355 * @is_tmf: if it is task management task.
356 * @tmf: the task management IU
358 #define DEV_IS_GONE(pm8001_dev) \
359 ((!pm8001_dev || (pm8001_dev->dev_type == NO_DEVICE)))
360 static int pm8001_task_exec(struct sas_task
*task
, const int num
,
361 gfp_t gfp_flags
, int is_tmf
, struct pm8001_tmf_task
*tmf
)
363 struct domain_device
*dev
= task
->dev
;
364 struct pm8001_hba_info
*pm8001_ha
;
365 struct pm8001_device
*pm8001_dev
;
366 struct pm8001_port
*port
= NULL
;
367 struct sas_task
*t
= task
;
368 struct pm8001_ccb_info
*ccb
;
369 u32 tag
= 0xdeadbeef, rc
, n_elem
= 0;
371 unsigned long flags
= 0, flags_libsas
= 0;
374 struct task_status_struct
*tsm
= &t
->task_status
;
375 tsm
->resp
= SAS_TASK_UNDELIVERED
;
376 tsm
->stat
= SAS_PHY_DOWN
;
377 if (dev
->dev_type
!= SATA_DEV
)
381 pm8001_ha
= pm8001_find_ha_by_dev(task
->dev
);
382 PM8001_IO_DBG(pm8001_ha
, pm8001_printk("pm8001_task_exec device \n "));
383 spin_lock_irqsave(&pm8001_ha
->lock
, flags
);
386 pm8001_dev
= dev
->lldd_dev
;
387 if (DEV_IS_GONE(pm8001_dev
)) {
389 PM8001_IO_DBG(pm8001_ha
,
390 pm8001_printk("device %d not ready.\n",
391 pm8001_dev
->device_id
));
393 PM8001_IO_DBG(pm8001_ha
,
394 pm8001_printk("device %016llx not "
395 "ready.\n", SAS_ADDR(dev
->sas_addr
)));
400 port
= &pm8001_ha
->port
[sas_find_local_port_id(dev
)];
401 if (!port
->port_attached
) {
402 if (sas_protocol_ata(t
->task_proto
)) {
403 struct task_status_struct
*ts
= &t
->task_status
;
404 ts
->resp
= SAS_TASK_UNDELIVERED
;
405 ts
->stat
= SAS_PHY_DOWN
;
407 spin_unlock_irqrestore(&pm8001_ha
->lock
, flags
);
408 spin_unlock_irqrestore(dev
->sata_dev
.ap
->lock
,
411 spin_lock_irqsave(dev
->sata_dev
.ap
->lock
,
413 spin_lock_irqsave(&pm8001_ha
->lock
, flags
);
415 t
= list_entry(t
->list
.next
,
416 struct sas_task
, list
);
419 struct task_status_struct
*ts
= &t
->task_status
;
420 ts
->resp
= SAS_TASK_UNDELIVERED
;
421 ts
->stat
= SAS_PHY_DOWN
;
424 t
= list_entry(t
->list
.next
,
425 struct sas_task
, list
);
429 rc
= pm8001_tag_alloc(pm8001_ha
, &tag
);
432 ccb
= &pm8001_ha
->ccb_info
[tag
];
434 if (!sas_protocol_ata(t
->task_proto
)) {
435 if (t
->num_scatter
) {
436 n_elem
= dma_map_sg(pm8001_ha
->dev
,
446 n_elem
= t
->num_scatter
;
450 ccb
->n_elem
= n_elem
;
453 switch (t
->task_proto
) {
454 case SAS_PROTOCOL_SMP
:
455 rc
= pm8001_task_prep_smp(pm8001_ha
, ccb
);
457 case SAS_PROTOCOL_SSP
:
459 rc
= pm8001_task_prep_ssp_tm(pm8001_ha
,
462 rc
= pm8001_task_prep_ssp(pm8001_ha
, ccb
);
464 case SAS_PROTOCOL_SATA
:
465 case SAS_PROTOCOL_STP
:
466 case SAS_PROTOCOL_SATA
| SAS_PROTOCOL_STP
:
467 rc
= pm8001_task_prep_ata(pm8001_ha
, ccb
);
470 dev_printk(KERN_ERR
, pm8001_ha
->dev
,
471 "unknown sas_task proto: 0x%x\n",
478 PM8001_IO_DBG(pm8001_ha
,
479 pm8001_printk("rc is %x\n", rc
));
482 /* TODO: select normal or high priority */
483 spin_lock(&t
->task_state_lock
);
484 t
->task_state_flags
|= SAS_TASK_AT_INITIATOR
;
485 spin_unlock(&t
->task_state_lock
);
486 pm8001_dev
->running_req
++;
488 t
= list_entry(t
->list
.next
, struct sas_task
, list
);
494 pm8001_tag_free(pm8001_ha
, tag
);
496 dev_printk(KERN_ERR
, pm8001_ha
->dev
, "pm8001 exec failed[%d]!\n", rc
);
497 if (!sas_protocol_ata(t
->task_proto
))
499 dma_unmap_sg(pm8001_ha
->dev
, t
->scatter
, n_elem
,
502 spin_unlock_irqrestore(&pm8001_ha
->lock
, flags
);
507 * pm8001_queue_command - register for upper layer used, all IO commands sent
508 * to HBA are from this interface.
509 * @task: the task to be execute.
510 * @num: if can_queue great than 1, the task can be queued up. for SMP task,
511 * we always execute one one time
512 * @gfp_flags: gfp_flags
514 int pm8001_queue_command(struct sas_task
*task
, const int num
,
517 return pm8001_task_exec(task
, num
, gfp_flags
, 0, NULL
);
520 void pm8001_ccb_free(struct pm8001_hba_info
*pm8001_ha
, u32 ccb_idx
)
522 pm8001_tag_clear(pm8001_ha
, ccb_idx
);
526 * pm8001_ccb_task_free - free the sg for ssp and smp command, free the ccb.
527 * @pm8001_ha: our hba card information
528 * @ccb: the ccb which attached to ssp task
529 * @task: the task to be free.
530 * @ccb_idx: ccb index.
532 void pm8001_ccb_task_free(struct pm8001_hba_info
*pm8001_ha
,
533 struct sas_task
*task
, struct pm8001_ccb_info
*ccb
, u32 ccb_idx
)
537 if (!sas_protocol_ata(task
->task_proto
))
539 dma_unmap_sg(pm8001_ha
->dev
, task
->scatter
,
540 task
->num_scatter
, task
->data_dir
);
542 switch (task
->task_proto
) {
543 case SAS_PROTOCOL_SMP
:
544 dma_unmap_sg(pm8001_ha
->dev
, &task
->smp_task
.smp_resp
, 1,
546 dma_unmap_sg(pm8001_ha
->dev
, &task
->smp_task
.smp_req
, 1,
550 case SAS_PROTOCOL_SATA
:
551 case SAS_PROTOCOL_STP
:
552 case SAS_PROTOCOL_SSP
:
557 task
->lldd_task
= NULL
;
559 ccb
->ccb_tag
= 0xFFFFFFFF;
560 pm8001_ccb_free(pm8001_ha
, ccb_idx
);
564 * pm8001_alloc_dev - find a empty pm8001_device
565 * @pm8001_ha: our hba card information
567 struct pm8001_device
*pm8001_alloc_dev(struct pm8001_hba_info
*pm8001_ha
)
570 for (dev
= 0; dev
< PM8001_MAX_DEVICES
; dev
++) {
571 if (pm8001_ha
->devices
[dev
].dev_type
== NO_DEVICE
) {
572 pm8001_ha
->devices
[dev
].id
= dev
;
573 return &pm8001_ha
->devices
[dev
];
576 if (dev
== PM8001_MAX_DEVICES
) {
577 PM8001_FAIL_DBG(pm8001_ha
,
578 pm8001_printk("max support %d devices, ignore ..\n",
579 PM8001_MAX_DEVICES
));
584 static void pm8001_free_dev(struct pm8001_device
*pm8001_dev
)
586 u32 id
= pm8001_dev
->id
;
587 memset(pm8001_dev
, 0, sizeof(*pm8001_dev
));
589 pm8001_dev
->dev_type
= NO_DEVICE
;
590 pm8001_dev
->device_id
= PM8001_MAX_DEVICES
;
591 pm8001_dev
->sas_device
= NULL
;
595 * pm8001_dev_found_notify - libsas notify a device is found.
596 * @dev: the device structure which sas layer used.
598 * when libsas find a sas domain device, it should tell the LLDD that
599 * device is found, and then LLDD register this device to HBA firmware
600 * by the command "OPC_INB_REG_DEV", after that the HBA will assign a
601 * device ID(according to device's sas address) and returned it to LLDD. From
602 * now on, we communicate with HBA FW with the device ID which HBA assigned
603 * rather than sas address. it is the necessary step for our HBA but it is
604 * the optional for other HBA driver.
606 static int pm8001_dev_found_notify(struct domain_device
*dev
)
608 unsigned long flags
= 0;
610 struct pm8001_hba_info
*pm8001_ha
= NULL
;
611 struct domain_device
*parent_dev
= dev
->parent
;
612 struct pm8001_device
*pm8001_device
;
613 DECLARE_COMPLETION_ONSTACK(completion
);
615 pm8001_ha
= pm8001_find_ha_by_dev(dev
);
616 spin_lock_irqsave(&pm8001_ha
->lock
, flags
);
618 pm8001_device
= pm8001_alloc_dev(pm8001_ha
);
619 if (!pm8001_device
) {
623 pm8001_device
->sas_device
= dev
;
624 dev
->lldd_dev
= pm8001_device
;
625 pm8001_device
->dev_type
= dev
->dev_type
;
626 pm8001_device
->dcompletion
= &completion
;
627 if (parent_dev
&& DEV_IS_EXPANDER(parent_dev
->dev_type
)) {
630 for (phy_id
= 0; phy_id
< parent_dev
->ex_dev
.num_phys
;
632 phy
= &parent_dev
->ex_dev
.ex_phy
[phy_id
];
633 if (SAS_ADDR(phy
->attached_sas_addr
)
634 == SAS_ADDR(dev
->sas_addr
)) {
635 pm8001_device
->attached_phy
= phy_id
;
639 if (phy_id
== parent_dev
->ex_dev
.num_phys
) {
640 PM8001_FAIL_DBG(pm8001_ha
,
641 pm8001_printk("Error: no attached dev:%016llx"
642 " at ex:%016llx.\n", SAS_ADDR(dev
->sas_addr
),
643 SAS_ADDR(parent_dev
->sas_addr
)));
647 if (dev
->dev_type
== SATA_DEV
) {
648 pm8001_device
->attached_phy
=
649 dev
->rphy
->identify
.phy_identifier
;
650 flag
= 1; /* directly sata*/
652 } /*register this device to HBA*/
653 PM8001_DISC_DBG(pm8001_ha
, pm8001_printk("Found device \n"));
654 PM8001_CHIP_DISP
->reg_dev_req(pm8001_ha
, pm8001_device
, flag
);
655 spin_unlock_irqrestore(&pm8001_ha
->lock
, flags
);
656 wait_for_completion(&completion
);
657 if (dev
->dev_type
== SAS_END_DEV
)
659 pm8001_ha
->flags
|= PM8001F_RUN_TIME
;
662 spin_unlock_irqrestore(&pm8001_ha
->lock
, flags
);
666 int pm8001_dev_found(struct domain_device
*dev
)
668 return pm8001_dev_found_notify(dev
);
672 * pm8001_alloc_task - allocate a task structure for TMF
674 static struct sas_task
*pm8001_alloc_task(void)
676 struct sas_task
*task
= kzalloc(sizeof(*task
), GFP_KERNEL
);
678 INIT_LIST_HEAD(&task
->list
);
679 spin_lock_init(&task
->task_state_lock
);
680 task
->task_state_flags
= SAS_TASK_STATE_PENDING
;
681 init_timer(&task
->timer
);
682 init_completion(&task
->completion
);
687 static void pm8001_free_task(struct sas_task
*task
)
690 BUG_ON(!list_empty(&task
->list
));
695 static void pm8001_task_done(struct sas_task
*task
)
697 if (!del_timer(&task
->timer
))
699 complete(&task
->completion
);
702 static void pm8001_tmf_timedout(unsigned long data
)
704 struct sas_task
*task
= (struct sas_task
*)data
;
706 task
->task_state_flags
|= SAS_TASK_STATE_ABORTED
;
707 complete(&task
->completion
);
710 #define PM8001_TASK_TIMEOUT 20
712 * pm8001_exec_internal_tmf_task - execute some task management commands.
713 * @dev: the wanted device.
714 * @tmf: which task management wanted to be take.
715 * @para_len: para_len.
716 * @parameter: ssp task parameter.
718 * when errors or exception happened, we may want to do something, for example
719 * abort the issued task which result in this execption, it is done by calling
720 * this function, note it is also with the task execute interface.
722 static int pm8001_exec_internal_tmf_task(struct domain_device
*dev
,
723 void *parameter
, u32 para_len
, struct pm8001_tmf_task
*tmf
)
726 struct sas_task
*task
= NULL
;
727 struct pm8001_hba_info
*pm8001_ha
= pm8001_find_ha_by_dev(dev
);
729 for (retry
= 0; retry
< 3; retry
++) {
730 task
= pm8001_alloc_task();
735 task
->task_proto
= dev
->tproto
;
736 memcpy(&task
->ssp_task
, parameter
, para_len
);
737 task
->task_done
= pm8001_task_done
;
738 task
->timer
.data
= (unsigned long)task
;
739 task
->timer
.function
= pm8001_tmf_timedout
;
740 task
->timer
.expires
= jiffies
+ PM8001_TASK_TIMEOUT
*HZ
;
741 add_timer(&task
->timer
);
743 res
= pm8001_task_exec(task
, 1, GFP_KERNEL
, 1, tmf
);
746 del_timer(&task
->timer
);
747 PM8001_FAIL_DBG(pm8001_ha
,
748 pm8001_printk("Executing internal task "
752 wait_for_completion(&task
->completion
);
753 res
= -TMF_RESP_FUNC_FAILED
;
754 /* Even TMF timed out, return direct. */
755 if ((task
->task_state_flags
& SAS_TASK_STATE_ABORTED
)) {
756 if (!(task
->task_state_flags
& SAS_TASK_STATE_DONE
)) {
757 PM8001_FAIL_DBG(pm8001_ha
,
758 pm8001_printk("TMF task[%x]timeout.\n",
764 if (task
->task_status
.resp
== SAS_TASK_COMPLETE
&&
765 task
->task_status
.stat
== SAM_GOOD
) {
766 res
= TMF_RESP_FUNC_COMPLETE
;
770 if (task
->task_status
.resp
== SAS_TASK_COMPLETE
&&
771 task
->task_status
.stat
== SAS_DATA_UNDERRUN
) {
772 /* no error, but return the number of bytes of
774 res
= task
->task_status
.residual
;
778 if (task
->task_status
.resp
== SAS_TASK_COMPLETE
&&
779 task
->task_status
.stat
== SAS_DATA_OVERRUN
) {
780 PM8001_FAIL_DBG(pm8001_ha
,
781 pm8001_printk("Blocked task error.\n"));
785 PM8001_EH_DBG(pm8001_ha
,
786 pm8001_printk(" Task to dev %016llx response:"
787 "0x%x status 0x%x\n",
788 SAS_ADDR(dev
->sas_addr
),
789 task
->task_status
.resp
,
790 task
->task_status
.stat
));
791 pm8001_free_task(task
);
796 BUG_ON(retry
== 3 && task
!= NULL
);
798 pm8001_free_task(task
);
803 pm8001_exec_internal_task_abort(struct pm8001_hba_info
*pm8001_ha
,
804 struct pm8001_device
*pm8001_dev
, struct domain_device
*dev
, u32 flag
,
809 struct pm8001_ccb_info
*ccb
;
810 struct sas_task
*task
= NULL
;
812 for (retry
= 0; retry
< 3; retry
++) {
813 task
= pm8001_alloc_task();
818 task
->task_proto
= dev
->tproto
;
819 task
->task_done
= pm8001_task_done
;
820 task
->timer
.data
= (unsigned long)task
;
821 task
->timer
.function
= pm8001_tmf_timedout
;
822 task
->timer
.expires
= jiffies
+ PM8001_TASK_TIMEOUT
* HZ
;
823 add_timer(&task
->timer
);
825 res
= pm8001_tag_alloc(pm8001_ha
, &ccb_tag
);
828 ccb
= &pm8001_ha
->ccb_info
[ccb_tag
];
829 ccb
->device
= pm8001_dev
;
830 ccb
->ccb_tag
= ccb_tag
;
833 res
= PM8001_CHIP_DISP
->task_abort(pm8001_ha
,
834 pm8001_dev
, flag
, task_tag
, ccb_tag
);
837 del_timer(&task
->timer
);
838 PM8001_FAIL_DBG(pm8001_ha
,
839 pm8001_printk("Executing internal task "
843 wait_for_completion(&task
->completion
);
844 res
= TMF_RESP_FUNC_FAILED
;
845 /* Even TMF timed out, return direct. */
846 if ((task
->task_state_flags
& SAS_TASK_STATE_ABORTED
)) {
847 if (!(task
->task_state_flags
& SAS_TASK_STATE_DONE
)) {
848 PM8001_FAIL_DBG(pm8001_ha
,
849 pm8001_printk("TMF task timeout.\n"));
854 if (task
->task_status
.resp
== SAS_TASK_COMPLETE
&&
855 task
->task_status
.stat
== SAM_GOOD
) {
856 res
= TMF_RESP_FUNC_COMPLETE
;
860 PM8001_EH_DBG(pm8001_ha
,
861 pm8001_printk(" Task to dev %016llx response: "
862 "0x%x status 0x%x\n",
863 SAS_ADDR(dev
->sas_addr
),
864 task
->task_status
.resp
,
865 task
->task_status
.stat
));
866 pm8001_free_task(task
);
871 BUG_ON(retry
== 3 && task
!= NULL
);
873 pm8001_free_task(task
);
878 * pm8001_dev_gone_notify - see the comments for "pm8001_dev_found_notify"
879 * @dev: the device structure which sas layer used.
881 static void pm8001_dev_gone_notify(struct domain_device
*dev
)
883 unsigned long flags
= 0;
885 struct pm8001_hba_info
*pm8001_ha
;
886 struct pm8001_device
*pm8001_dev
= dev
->lldd_dev
;
887 u32 device_id
= pm8001_dev
->device_id
;
888 pm8001_ha
= pm8001_find_ha_by_dev(dev
);
889 spin_lock_irqsave(&pm8001_ha
->lock
, flags
);
890 pm8001_tag_alloc(pm8001_ha
, &tag
);
892 PM8001_DISC_DBG(pm8001_ha
,
893 pm8001_printk("found dev[%d:%x] is gone.\n",
894 pm8001_dev
->device_id
, pm8001_dev
->dev_type
));
895 if (pm8001_dev
->running_req
) {
896 spin_unlock_irqrestore(&pm8001_ha
->lock
, flags
);
897 pm8001_exec_internal_task_abort(pm8001_ha
, pm8001_dev
,
899 spin_lock_irqsave(&pm8001_ha
->lock
, flags
);
901 PM8001_CHIP_DISP
->dereg_dev_req(pm8001_ha
, device_id
);
902 pm8001_free_dev(pm8001_dev
);
904 PM8001_DISC_DBG(pm8001_ha
,
905 pm8001_printk("Found dev has gone.\n"));
907 dev
->lldd_dev
= NULL
;
908 spin_unlock_irqrestore(&pm8001_ha
->lock
, flags
);
911 void pm8001_dev_gone(struct domain_device
*dev
)
913 pm8001_dev_gone_notify(dev
);
916 static int pm8001_issue_ssp_tmf(struct domain_device
*dev
,
917 u8
*lun
, struct pm8001_tmf_task
*tmf
)
919 struct sas_ssp_task ssp_task
;
920 if (!(dev
->tproto
& SAS_PROTOCOL_SSP
))
921 return TMF_RESP_FUNC_ESUPP
;
923 strncpy((u8
*)&ssp_task
.LUN
, lun
, 8);
924 return pm8001_exec_internal_tmf_task(dev
, &ssp_task
, sizeof(ssp_task
),
929 * Standard mandates link reset for ATA (type 0) and hard reset for
930 * SSP (type 1) , only for RECOVERY
932 int pm8001_I_T_nexus_reset(struct domain_device
*dev
)
934 int rc
= TMF_RESP_FUNC_FAILED
;
935 struct pm8001_device
*pm8001_dev
;
936 struct pm8001_hba_info
*pm8001_ha
;
938 if (!dev
|| !dev
->lldd_dev
)
941 pm8001_dev
= dev
->lldd_dev
;
942 pm8001_ha
= pm8001_find_ha_by_dev(dev
);
943 phy
= sas_find_local_phy(dev
);
945 if (dev_is_sata(dev
)) {
946 DECLARE_COMPLETION_ONSTACK(completion_setstate
);
947 if (scsi_is_sas_phy_local(phy
))
949 rc
= sas_phy_reset(phy
, 1);
951 rc
= pm8001_exec_internal_task_abort(pm8001_ha
, pm8001_dev
,
953 pm8001_dev
->setds_completion
= &completion_setstate
;
954 rc
= PM8001_CHIP_DISP
->set_dev_state_req(pm8001_ha
,
956 wait_for_completion(&completion_setstate
);
958 rc
= sas_phy_reset(phy
, 1);
961 PM8001_EH_DBG(pm8001_ha
, pm8001_printk(" for device[%x]:rc=%d\n",
962 pm8001_dev
->device_id
, rc
));
966 /* mandatory SAM-3, the task reset the specified LUN*/
967 int pm8001_lu_reset(struct domain_device
*dev
, u8
*lun
)
969 int rc
= TMF_RESP_FUNC_FAILED
;
970 struct pm8001_tmf_task tmf_task
;
971 struct pm8001_device
*pm8001_dev
= dev
->lldd_dev
;
972 struct pm8001_hba_info
*pm8001_ha
= pm8001_find_ha_by_dev(dev
);
973 if (dev_is_sata(dev
)) {
974 struct sas_phy
*phy
= sas_find_local_phy(dev
);
975 rc
= pm8001_exec_internal_task_abort(pm8001_ha
, pm8001_dev
,
977 rc
= sas_phy_reset(phy
, 1);
978 rc
= PM8001_CHIP_DISP
->set_dev_state_req(pm8001_ha
,
982 tmf_task
.tmf
= TMF_LU_RESET
;
983 rc
= pm8001_issue_ssp_tmf(dev
, lun
, &tmf_task
);
985 /* If failed, fall-through I_T_Nexus reset */
986 PM8001_EH_DBG(pm8001_ha
, pm8001_printk("for device[%x]:rc=%d\n",
987 pm8001_dev
->device_id
, rc
));
992 int pm8001_query_task(struct sas_task
*task
)
994 u32 tag
= 0xdeadbeef;
997 struct pm8001_tmf_task tmf_task
;
998 int rc
= TMF_RESP_FUNC_FAILED
;
999 if (unlikely(!task
|| !task
->lldd_task
|| !task
->dev
))
1002 if (task
->task_proto
& SAS_PROTOCOL_SSP
) {
1003 struct scsi_cmnd
*cmnd
= task
->uldd_task
;
1004 struct domain_device
*dev
= task
->dev
;
1005 struct pm8001_hba_info
*pm8001_ha
=
1006 pm8001_find_ha_by_dev(dev
);
1008 int_to_scsilun(cmnd
->device
->lun
, &lun
);
1009 rc
= pm8001_find_tag(task
, &tag
);
1011 rc
= TMF_RESP_FUNC_FAILED
;
1014 PM8001_EH_DBG(pm8001_ha
, pm8001_printk("Query:["));
1015 for (i
= 0; i
< 16; i
++)
1016 printk(KERN_INFO
"%02x ", cmnd
->cmnd
[i
]);
1017 printk(KERN_INFO
"]\n");
1018 tmf_task
.tmf
= TMF_QUERY_TASK
;
1019 tmf_task
.tag_of_task_to_be_managed
= tag
;
1021 rc
= pm8001_issue_ssp_tmf(dev
, lun
.scsi_lun
, &tmf_task
);
1023 /* The task is still in Lun, release it then */
1024 case TMF_RESP_FUNC_SUCC
:
1025 PM8001_EH_DBG(pm8001_ha
,
1026 pm8001_printk("The task is still in Lun \n"));
1027 /* The task is not in Lun or failed, reset the phy */
1028 case TMF_RESP_FUNC_FAILED
:
1029 case TMF_RESP_FUNC_COMPLETE
:
1030 PM8001_EH_DBG(pm8001_ha
,
1031 pm8001_printk("The task is not in Lun or failed,"
1032 " reset the phy \n"));
1036 pm8001_printk(":rc= %d\n", rc
);
1040 /* mandatory SAM-3, still need free task/ccb info, abord the specified task */
1041 int pm8001_abort_task(struct sas_task
*task
)
1043 unsigned long flags
;
1044 u32 tag
= 0xdeadbeef;
1046 struct domain_device
*dev
;
1047 struct pm8001_hba_info
*pm8001_ha
= NULL
;
1048 struct pm8001_ccb_info
*ccb
;
1049 struct scsi_lun lun
;
1050 struct pm8001_device
*pm8001_dev
;
1051 struct pm8001_tmf_task tmf_task
;
1052 int rc
= TMF_RESP_FUNC_FAILED
;
1053 if (unlikely(!task
|| !task
->lldd_task
|| !task
->dev
))
1055 spin_lock_irqsave(&task
->task_state_lock
, flags
);
1056 if (task
->task_state_flags
& SAS_TASK_STATE_DONE
) {
1057 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
1058 rc
= TMF_RESP_FUNC_COMPLETE
;
1061 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
1062 if (task
->task_proto
& SAS_PROTOCOL_SSP
) {
1063 struct scsi_cmnd
*cmnd
= task
->uldd_task
;
1065 ccb
= task
->lldd_task
;
1066 pm8001_dev
= dev
->lldd_dev
;
1067 pm8001_ha
= pm8001_find_ha_by_dev(dev
);
1068 int_to_scsilun(cmnd
->device
->lun
, &lun
);
1069 rc
= pm8001_find_tag(task
, &tag
);
1071 printk(KERN_INFO
"No such tag in %s\n", __func__
);
1072 rc
= TMF_RESP_FUNC_FAILED
;
1075 device_id
= pm8001_dev
->device_id
;
1076 PM8001_EH_DBG(pm8001_ha
,
1077 pm8001_printk("abort io to deviceid= %d\n", device_id
));
1078 tmf_task
.tmf
= TMF_ABORT_TASK
;
1079 tmf_task
.tag_of_task_to_be_managed
= tag
;
1080 rc
= pm8001_issue_ssp_tmf(dev
, lun
.scsi_lun
, &tmf_task
);
1081 pm8001_exec_internal_task_abort(pm8001_ha
, pm8001_dev
,
1082 pm8001_dev
->sas_device
, 0, tag
);
1083 } else if (task
->task_proto
& SAS_PROTOCOL_SATA
||
1084 task
->task_proto
& SAS_PROTOCOL_STP
) {
1086 pm8001_dev
= dev
->lldd_dev
;
1087 pm8001_ha
= pm8001_find_ha_by_dev(dev
);
1088 rc
= pm8001_find_tag(task
, &tag
);
1090 printk(KERN_INFO
"No such tag in %s\n", __func__
);
1091 rc
= TMF_RESP_FUNC_FAILED
;
1094 rc
= pm8001_exec_internal_task_abort(pm8001_ha
, pm8001_dev
,
1095 pm8001_dev
->sas_device
, 0, tag
);
1096 } else if (task
->task_proto
& SAS_PROTOCOL_SMP
) {
1099 pm8001_dev
= dev
->lldd_dev
;
1100 pm8001_ha
= pm8001_find_ha_by_dev(dev
);
1101 rc
= pm8001_find_tag(task
, &tag
);
1103 printk(KERN_INFO
"No such tag in %s\n", __func__
);
1104 rc
= TMF_RESP_FUNC_FAILED
;
1107 rc
= pm8001_exec_internal_task_abort(pm8001_ha
, pm8001_dev
,
1108 pm8001_dev
->sas_device
, 0, tag
);
1112 if (rc
!= TMF_RESP_FUNC_COMPLETE
)
1113 pm8001_printk("rc= %d\n", rc
);
1117 int pm8001_abort_task_set(struct domain_device
*dev
, u8
*lun
)
1119 int rc
= TMF_RESP_FUNC_FAILED
;
1120 struct pm8001_tmf_task tmf_task
;
1122 tmf_task
.tmf
= TMF_ABORT_TASK_SET
;
1123 rc
= pm8001_issue_ssp_tmf(dev
, lun
, &tmf_task
);
1127 int pm8001_clear_aca(struct domain_device
*dev
, u8
*lun
)
1129 int rc
= TMF_RESP_FUNC_FAILED
;
1130 struct pm8001_tmf_task tmf_task
;
1132 tmf_task
.tmf
= TMF_CLEAR_ACA
;
1133 rc
= pm8001_issue_ssp_tmf(dev
, lun
, &tmf_task
);
1138 int pm8001_clear_task_set(struct domain_device
*dev
, u8
*lun
)
1140 int rc
= TMF_RESP_FUNC_FAILED
;
1141 struct pm8001_tmf_task tmf_task
;
1142 struct pm8001_device
*pm8001_dev
= dev
->lldd_dev
;
1143 struct pm8001_hba_info
*pm8001_ha
= pm8001_find_ha_by_dev(dev
);
1145 PM8001_EH_DBG(pm8001_ha
,
1146 pm8001_printk("I_T_L_Q clear task set[%x]\n",
1147 pm8001_dev
->device_id
));
1148 tmf_task
.tmf
= TMF_CLEAR_TASK_SET
;
1149 rc
= pm8001_issue_ssp_tmf(dev
, lun
, &tmf_task
);