1 // SPDX-License-Identifier: GPL-2.0-only
3 * Aic94xx SAS/SATA Tasks
5 * Copyright (C) 2005 Adaptec, Inc. All rights reserved.
6 * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com>
9 #include <linux/spinlock.h>
11 #include "aic94xx_sas.h"
12 #include "aic94xx_hwi.h"
14 static void asd_unbuild_ata_ascb(struct asd_ascb
*a
);
15 static void asd_unbuild_smp_ascb(struct asd_ascb
*a
);
16 static void asd_unbuild_ssp_ascb(struct asd_ascb
*a
);
18 static void asd_can_dequeue(struct asd_ha_struct
*asd_ha
, int num
)
22 spin_lock_irqsave(&asd_ha
->seq
.pend_q_lock
, flags
);
23 asd_ha
->seq
.can_queue
+= num
;
24 spin_unlock_irqrestore(&asd_ha
->seq
.pend_q_lock
, flags
);
27 /* DMA_... to our direction translation.
29 static const u8 data_dir_flags
[] = {
30 [DMA_BIDIRECTIONAL
] = DATA_DIR_BYRECIPIENT
, /* UNSPECIFIED */
31 [DMA_TO_DEVICE
] = DATA_DIR_OUT
, /* OUTBOUND */
32 [DMA_FROM_DEVICE
] = DATA_DIR_IN
, /* INBOUND */
33 [DMA_NONE
] = DATA_DIR_NONE
, /* NO TRANSFER */
36 static int asd_map_scatterlist(struct sas_task
*task
,
40 struct asd_ascb
*ascb
= task
->lldd_task
;
41 struct asd_ha_struct
*asd_ha
= ascb
->ha
;
42 struct scatterlist
*sc
;
45 if (task
->data_dir
== DMA_NONE
)
48 if (task
->num_scatter
== 0) {
49 void *p
= task
->scatter
;
50 dma_addr_t dma
= dma_map_single(&asd_ha
->pcidev
->dev
, p
,
53 sg_arr
[0].bus_addr
= cpu_to_le64((u64
)dma
);
54 sg_arr
[0].size
= cpu_to_le32(task
->total_xfer_len
);
55 sg_arr
[0].flags
|= ASD_SG_EL_LIST_EOL
;
59 /* STP tasks come from libata which has already mapped
61 if (sas_protocol_ata(task
->task_proto
))
62 num_sg
= task
->num_scatter
;
64 num_sg
= dma_map_sg(&asd_ha
->pcidev
->dev
, task
->scatter
,
65 task
->num_scatter
, task
->data_dir
);
72 ascb
->sg_arr
= asd_alloc_coherent(asd_ha
,
73 num_sg
*sizeof(struct sg_el
),
79 for_each_sg(task
->scatter
, sc
, num_sg
, i
) {
81 &((struct sg_el
*)ascb
->sg_arr
->vaddr
)[i
];
82 sg
->bus_addr
= cpu_to_le64((u64
)sg_dma_address(sc
));
83 sg
->size
= cpu_to_le32((u32
)sg_dma_len(sc
));
85 sg
->flags
|= ASD_SG_EL_LIST_EOL
;
88 for_each_sg(task
->scatter
, sc
, 2, i
) {
90 cpu_to_le64((u64
)sg_dma_address(sc
));
91 sg_arr
[i
].size
= cpu_to_le32((u32
)sg_dma_len(sc
));
93 sg_arr
[1].next_sg_offs
= 2 * sizeof(*sg_arr
);
94 sg_arr
[1].flags
|= ASD_SG_EL_LIST_EOS
;
96 memset(&sg_arr
[2], 0, sizeof(*sg_arr
));
97 sg_arr
[2].bus_addr
=cpu_to_le64((u64
)ascb
->sg_arr
->dma_handle
);
100 for_each_sg(task
->scatter
, sc
, num_sg
, i
) {
102 cpu_to_le64((u64
)sg_dma_address(sc
));
103 sg_arr
[i
].size
= cpu_to_le32((u32
)sg_dma_len(sc
));
105 sg_arr
[i
-1].flags
|= ASD_SG_EL_LIST_EOL
;
110 if (sas_protocol_ata(task
->task_proto
))
111 dma_unmap_sg(&asd_ha
->pcidev
->dev
, task
->scatter
,
112 task
->num_scatter
, task
->data_dir
);
116 static void asd_unmap_scatterlist(struct asd_ascb
*ascb
)
118 struct asd_ha_struct
*asd_ha
= ascb
->ha
;
119 struct sas_task
*task
= ascb
->uldd_task
;
121 if (task
->data_dir
== DMA_NONE
)
124 if (task
->num_scatter
== 0) {
125 dma_addr_t dma
= (dma_addr_t
)
126 le64_to_cpu(ascb
->scb
->ssp_task
.sg_element
[0].bus_addr
);
127 dma_unmap_single(&ascb
->ha
->pcidev
->dev
, dma
,
128 task
->total_xfer_len
, task
->data_dir
);
132 asd_free_coherent(asd_ha
, ascb
->sg_arr
);
133 if (task
->task_proto
!= SAS_PROTOCOL_STP
)
134 dma_unmap_sg(&asd_ha
->pcidev
->dev
, task
->scatter
,
135 task
->num_scatter
, task
->data_dir
);
138 /* ---------- Task complete tasklet ---------- */
140 static void asd_get_response_tasklet(struct asd_ascb
*ascb
,
141 struct done_list_struct
*dl
)
143 struct asd_ha_struct
*asd_ha
= ascb
->ha
;
144 struct sas_task
*task
= ascb
->uldd_task
;
145 struct task_status_struct
*ts
= &task
->task_status
;
147 struct tc_resp_sb_struct
{
151 } __attribute__ ((packed
)) *resp_sb
= (void *) dl
->status_block
;
153 /* int size = ((resp_sb->flags & 7) << 8) | resp_sb->len_lsb; */
154 int edb_id
= ((resp_sb
->flags
& 0x70) >> 4)-1;
155 struct asd_ascb
*escb
;
156 struct asd_dma_tok
*edb
;
159 spin_lock_irqsave(&asd_ha
->seq
.tc_index_lock
, flags
);
160 escb
= asd_tc_index_find(&asd_ha
->seq
,
161 (int)le16_to_cpu(resp_sb
->index_escb
));
162 spin_unlock_irqrestore(&asd_ha
->seq
.tc_index_lock
, flags
);
165 ASD_DPRINTK("Uh-oh! No escb for this dl?!\n");
169 ts
->buf_valid_size
= 0;
170 edb
= asd_ha
->seq
.edb_arr
[edb_id
+ escb
->edb_index
];
172 if (task
->task_proto
== SAS_PROTOCOL_SSP
) {
173 struct ssp_response_iu
*iu
=
174 r
+ 16 + sizeof(struct ssp_frame_hdr
);
176 ts
->residual
= le32_to_cpu(*(__le32
*)r
);
178 sas_ssp_task_response(&asd_ha
->pcidev
->dev
, task
, iu
);
180 struct ata_task_resp
*resp
= (void *) &ts
->buf
[0];
182 ts
->residual
= le32_to_cpu(*(__le32
*)r
);
184 if (SAS_STATUS_BUF_SIZE
>= sizeof(*resp
)) {
185 resp
->frame_len
= le16_to_cpu(*(__le16
*)(r
+6));
186 memcpy(&resp
->ending_fis
[0], r
+16, ATA_RESP_FIS_SIZE
);
187 ts
->buf_valid_size
= sizeof(*resp
);
191 asd_invalidate_edb(escb
, edb_id
);
194 static void asd_task_tasklet_complete(struct asd_ascb
*ascb
,
195 struct done_list_struct
*dl
)
197 struct sas_task
*task
= ascb
->uldd_task
;
198 struct task_status_struct
*ts
= &task
->task_status
;
200 u8 opcode
= dl
->opcode
;
202 asd_can_dequeue(ascb
->ha
, 1);
207 ts
->resp
= SAS_TASK_COMPLETE
;
208 ts
->stat
= SAM_STAT_GOOD
;
211 ts
->resp
= SAS_TASK_COMPLETE
;
212 ts
->stat
= SAS_DATA_UNDERRUN
;
213 ts
->residual
= le32_to_cpu(*(__le32
*)dl
->status_block
);
216 ts
->resp
= SAS_TASK_COMPLETE
;
217 ts
->stat
= SAS_DATA_OVERRUN
;
222 ts
->resp
= SAS_TASK_COMPLETE
;
223 ts
->stat
= SAS_PROTO_RESPONSE
;
224 asd_get_response_tasklet(ascb
, dl
);
227 ts
->resp
= SAS_TASK_UNDELIVERED
;
228 ts
->stat
= SAS_OPEN_REJECT
;
229 if (dl
->status_block
[1] & 2)
230 ts
->open_rej_reason
= 1 + dl
->status_block
[2];
231 else if (dl
->status_block
[1] & 1)
232 ts
->open_rej_reason
= (dl
->status_block
[2] >> 4)+10;
234 ts
->open_rej_reason
= SAS_OREJ_UNKNOWN
;
237 ts
->resp
= SAS_TASK_UNDELIVERED
;
238 ts
->stat
= SAS_OPEN_TO
;
242 ts
->resp
= SAS_TASK_UNDELIVERED
;
243 ts
->stat
= SAS_PHY_DOWN
;
246 ts
->resp
= SAS_TASK_COMPLETE
;
247 ts
->stat
= SAS_PHY_DOWN
;
253 case TF_SMP_XMIT_RCV_ERR
:
254 case TC_ATA_R_ERR_RECV
:
255 ts
->resp
= SAS_TASK_COMPLETE
;
256 ts
->stat
= SAS_INTERRUPTED
;
262 ts
->resp
= SAS_TASK_UNDELIVERED
;
263 ts
->stat
= SAS_DEV_NO_RESPONSE
;
266 ts
->resp
= SAS_TASK_COMPLETE
;
267 ts
->stat
= SAS_NAK_R_ERR
;
269 case TA_I_T_NEXUS_LOSS
:
270 opcode
= dl
->status_block
[0];
273 case TF_INV_CONN_HANDLE
:
274 ts
->resp
= SAS_TASK_UNDELIVERED
;
275 ts
->stat
= SAS_DEVICE_UNKNOWN
;
277 case TF_REQUESTED_N_PENDING
:
278 ts
->resp
= SAS_TASK_UNDELIVERED
;
279 ts
->stat
= SAS_PENDING
;
281 case TC_TASK_CLEARED
:
283 ts
->resp
= SAS_TASK_COMPLETE
;
284 ts
->stat
= SAS_ABORTED_TASK
;
290 case TF_TMF_TAG_FREE
:
291 case TF_TMF_TASK_DONE
:
292 case TF_TMF_NO_CONN_HANDLE
:
295 case TF_DATA_OFFS_ERR
:
296 ts
->resp
= SAS_TASK_UNDELIVERED
;
297 ts
->stat
= SAS_DEV_NO_RESPONSE
;
300 case TC_LINK_ADM_RESP
:
303 case TC_PARTIAL_SG_LIST
:
305 ASD_DPRINTK("%s: dl opcode: 0x%x?\n", __func__
, opcode
);
309 switch (task
->task_proto
) {
310 case SAS_PROTOCOL_SATA
:
311 case SAS_PROTOCOL_STP
:
312 asd_unbuild_ata_ascb(ascb
);
314 case SAS_PROTOCOL_SMP
:
315 asd_unbuild_smp_ascb(ascb
);
317 case SAS_PROTOCOL_SSP
:
318 asd_unbuild_ssp_ascb(ascb
);
323 spin_lock_irqsave(&task
->task_state_lock
, flags
);
324 task
->task_state_flags
&= ~SAS_TASK_STATE_PENDING
;
325 task
->task_state_flags
&= ~SAS_TASK_AT_INITIATOR
;
326 task
->task_state_flags
|= SAS_TASK_STATE_DONE
;
327 if (unlikely((task
->task_state_flags
& SAS_TASK_STATE_ABORTED
))) {
328 struct completion
*completion
= ascb
->completion
;
329 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
330 ASD_DPRINTK("task 0x%p done with opcode 0x%x resp 0x%x "
331 "stat 0x%x but aborted by upper layer!\n",
332 task
, opcode
, ts
->resp
, ts
->stat
);
334 complete(completion
);
336 spin_unlock_irqrestore(&task
->task_state_lock
, flags
);
337 task
->lldd_task
= NULL
;
340 task
->task_done(task
);
344 /* ---------- ATA ---------- */
346 static int asd_build_ata_ascb(struct asd_ascb
*ascb
, struct sas_task
*task
,
349 struct domain_device
*dev
= task
->dev
;
356 if (unlikely(task
->ata_task
.device_control_reg_update
))
357 scb
->header
.opcode
= CONTROL_ATA_DEV
;
358 else if (dev
->sata_dev
.class == ATA_DEV_ATAPI
)
359 scb
->header
.opcode
= INITIATE_ATAPI_TASK
;
361 scb
->header
.opcode
= INITIATE_ATA_TASK
;
363 scb
->ata_task
.proto_conn_rate
= (1 << 5); /* STP */
364 if (dev
->port
->oob_mode
== SAS_OOB_MODE
)
365 scb
->ata_task
.proto_conn_rate
|= dev
->linkrate
;
367 scb
->ata_task
.total_xfer_len
= cpu_to_le32(task
->total_xfer_len
);
368 scb
->ata_task
.fis
= task
->ata_task
.fis
;
369 if (likely(!task
->ata_task
.device_control_reg_update
))
370 scb
->ata_task
.fis
.flags
|= 0x80; /* C=1: update ATA cmd reg */
371 scb
->ata_task
.fis
.flags
&= 0xF0; /* PM_PORT field shall be 0 */
372 if (dev
->sata_dev
.class == ATA_DEV_ATAPI
)
373 memcpy(scb
->ata_task
.atapi_packet
, task
->ata_task
.atapi_packet
,
375 scb
->ata_task
.sister_scb
= cpu_to_le16(0xFFFF);
376 scb
->ata_task
.conn_handle
= cpu_to_le16(
377 (u16
)(unsigned long)dev
->lldd_dev
);
379 if (likely(!task
->ata_task
.device_control_reg_update
)) {
381 if (task
->ata_task
.dma_xfer
)
382 flags
|= DATA_XFER_MODE_DMA
;
383 if (task
->ata_task
.use_ncq
&&
384 dev
->sata_dev
.class != ATA_DEV_ATAPI
)
385 flags
|= ATA_Q_TYPE_NCQ
;
386 flags
|= data_dir_flags
[task
->data_dir
];
387 scb
->ata_task
.ata_flags
= flags
;
389 scb
->ata_task
.retry_count
= task
->ata_task
.retry_count
;
392 if (task
->ata_task
.set_affil_pol
)
393 flags
|= SET_AFFIL_POLICY
;
394 if (task
->ata_task
.stp_affil_pol
)
395 flags
|= STP_AFFIL_POLICY
;
396 scb
->ata_task
.flags
= flags
;
398 ascb
->tasklet_complete
= asd_task_tasklet_complete
;
400 if (likely(!task
->ata_task
.device_control_reg_update
))
401 res
= asd_map_scatterlist(task
, scb
->ata_task
.sg_element
,
407 static void asd_unbuild_ata_ascb(struct asd_ascb
*a
)
409 asd_unmap_scatterlist(a
);
412 /* ---------- SMP ---------- */
414 static int asd_build_smp_ascb(struct asd_ascb
*ascb
, struct sas_task
*task
,
417 struct asd_ha_struct
*asd_ha
= ascb
->ha
;
418 struct domain_device
*dev
= task
->dev
;
421 dma_map_sg(&asd_ha
->pcidev
->dev
, &task
->smp_task
.smp_req
, 1,
423 dma_map_sg(&asd_ha
->pcidev
->dev
, &task
->smp_task
.smp_resp
, 1,
428 scb
->header
.opcode
= INITIATE_SMP_TASK
;
430 scb
->smp_task
.proto_conn_rate
= dev
->linkrate
;
432 scb
->smp_task
.smp_req
.bus_addr
=
433 cpu_to_le64((u64
)sg_dma_address(&task
->smp_task
.smp_req
));
434 scb
->smp_task
.smp_req
.size
=
435 cpu_to_le32((u32
)sg_dma_len(&task
->smp_task
.smp_req
)-4);
437 scb
->smp_task
.smp_resp
.bus_addr
=
438 cpu_to_le64((u64
)sg_dma_address(&task
->smp_task
.smp_resp
));
439 scb
->smp_task
.smp_resp
.size
=
440 cpu_to_le32((u32
)sg_dma_len(&task
->smp_task
.smp_resp
)-4);
442 scb
->smp_task
.sister_scb
= cpu_to_le16(0xFFFF);
443 scb
->smp_task
.conn_handle
= cpu_to_le16((u16
)
444 (unsigned long)dev
->lldd_dev
);
446 ascb
->tasklet_complete
= asd_task_tasklet_complete
;
451 static void asd_unbuild_smp_ascb(struct asd_ascb
*a
)
453 struct sas_task
*task
= a
->uldd_task
;
456 dma_unmap_sg(&a
->ha
->pcidev
->dev
, &task
->smp_task
.smp_req
, 1,
458 dma_unmap_sg(&a
->ha
->pcidev
->dev
, &task
->smp_task
.smp_resp
, 1,
462 /* ---------- SSP ---------- */
464 static int asd_build_ssp_ascb(struct asd_ascb
*ascb
, struct sas_task
*task
,
467 struct domain_device
*dev
= task
->dev
;
473 scb
->header
.opcode
= INITIATE_SSP_TASK
;
475 scb
->ssp_task
.proto_conn_rate
= (1 << 4); /* SSP */
476 scb
->ssp_task
.proto_conn_rate
|= dev
->linkrate
;
477 scb
->ssp_task
.total_xfer_len
= cpu_to_le32(task
->total_xfer_len
);
478 scb
->ssp_task
.ssp_frame
.frame_type
= SSP_DATA
;
479 memcpy(scb
->ssp_task
.ssp_frame
.hashed_dest_addr
, dev
->hashed_sas_addr
,
480 HASHED_SAS_ADDR_SIZE
);
481 memcpy(scb
->ssp_task
.ssp_frame
.hashed_src_addr
,
482 dev
->port
->ha
->hashed_sas_addr
, HASHED_SAS_ADDR_SIZE
);
483 scb
->ssp_task
.ssp_frame
.tptt
= cpu_to_be16(0xFFFF);
485 memcpy(scb
->ssp_task
.ssp_cmd
.lun
, task
->ssp_task
.LUN
, 8);
486 if (task
->ssp_task
.enable_first_burst
)
487 scb
->ssp_task
.ssp_cmd
.efb_prio_attr
|= EFB_MASK
;
488 scb
->ssp_task
.ssp_cmd
.efb_prio_attr
|= (task
->ssp_task
.task_prio
<< 3);
489 scb
->ssp_task
.ssp_cmd
.efb_prio_attr
|= (task
->ssp_task
.task_attr
& 7);
490 memcpy(scb
->ssp_task
.ssp_cmd
.cdb
, task
->ssp_task
.cmd
->cmnd
,
491 task
->ssp_task
.cmd
->cmd_len
);
493 scb
->ssp_task
.sister_scb
= cpu_to_le16(0xFFFF);
494 scb
->ssp_task
.conn_handle
= cpu_to_le16(
495 (u16
)(unsigned long)dev
->lldd_dev
);
496 scb
->ssp_task
.data_dir
= data_dir_flags
[task
->data_dir
];
497 scb
->ssp_task
.retry_count
= scb
->ssp_task
.retry_count
;
499 ascb
->tasklet_complete
= asd_task_tasklet_complete
;
501 res
= asd_map_scatterlist(task
, scb
->ssp_task
.sg_element
, gfp_flags
);
506 static void asd_unbuild_ssp_ascb(struct asd_ascb
*a
)
508 asd_unmap_scatterlist(a
);
511 /* ---------- Execute Task ---------- */
513 static int asd_can_queue(struct asd_ha_struct
*asd_ha
, int num
)
518 spin_lock_irqsave(&asd_ha
->seq
.pend_q_lock
, flags
);
519 if ((asd_ha
->seq
.can_queue
- num
) < 0)
520 res
= -SAS_QUEUE_FULL
;
522 asd_ha
->seq
.can_queue
-= num
;
523 spin_unlock_irqrestore(&asd_ha
->seq
.pend_q_lock
, flags
);
528 int asd_execute_task(struct sas_task
*task
, gfp_t gfp_flags
)
532 struct sas_task
*t
= task
;
533 struct asd_ascb
*ascb
= NULL
, *a
;
534 struct asd_ha_struct
*asd_ha
= task
->dev
->port
->ha
->lldd_ha
;
537 res
= asd_can_queue(asd_ha
, 1);
542 ascb
= asd_ascb_alloc_list(asd_ha
, &res
, gfp_flags
);
548 __list_add(&alist
, ascb
->list
.prev
, &ascb
->list
);
549 list_for_each_entry(a
, &alist
, list
) {
554 list_for_each_entry(a
, &alist
, list
) {
557 if (t
->task_proto
& SAS_PROTOCOL_STP
)
558 t
->task_proto
= SAS_PROTOCOL_STP
;
559 switch (t
->task_proto
) {
560 case SAS_PROTOCOL_SATA
:
561 case SAS_PROTOCOL_STP
:
562 res
= asd_build_ata_ascb(a
, t
, gfp_flags
);
564 case SAS_PROTOCOL_SMP
:
565 res
= asd_build_smp_ascb(a
, t
, gfp_flags
);
567 case SAS_PROTOCOL_SSP
:
568 res
= asd_build_ssp_ascb(a
, t
, gfp_flags
);
571 asd_printk("unknown sas_task proto: 0x%x\n",
579 spin_lock_irqsave(&t
->task_state_lock
, flags
);
580 t
->task_state_flags
|= SAS_TASK_AT_INITIATOR
;
581 spin_unlock_irqrestore(&t
->task_state_lock
, flags
);
583 list_del_init(&alist
);
585 res
= asd_post_ascb_list(asd_ha
, ascb
, 1);
588 __list_add(&alist
, ascb
->list
.prev
, &ascb
->list
);
595 struct asd_ascb
*b
= a
;
596 list_for_each_entry(a
, &alist
, list
) {
600 spin_lock_irqsave(&t
->task_state_lock
, flags
);
601 t
->task_state_flags
&= ~SAS_TASK_AT_INITIATOR
;
602 spin_unlock_irqrestore(&t
->task_state_lock
, flags
);
603 switch (t
->task_proto
) {
604 case SAS_PROTOCOL_SATA
:
605 case SAS_PROTOCOL_STP
:
606 asd_unbuild_ata_ascb(a
);
608 case SAS_PROTOCOL_SMP
:
609 asd_unbuild_smp_ascb(a
);
611 case SAS_PROTOCOL_SSP
:
612 asd_unbuild_ssp_ascb(a
);
619 list_del_init(&alist
);
622 asd_ascb_free_list(ascb
);
623 asd_can_dequeue(asd_ha
, 1);