2 * Copyright (c) 2005-2011 Atheros Communications Inc.
3 * Copyright (c) 2011-2013 Qualcomm Atheros, Inc.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24 * Support for Copy Engine hardware, which is mainly used for
25 * communication between Host and Target over a PCIe interconnect.
29 * A single CopyEngine (CE) comprises two "rings":
33 * Each ring consists of a number of descriptors which specify
34 * an address, length, and meta-data.
36 * Typically, one side of the PCIe interconnect (Host or Target)
37 * controls one ring and the other side controls the other ring.
38 * The source side chooses when to initiate a transfer and it
39 * chooses what to send (buffer address, length). The destination
40 * side keeps a supply of "anonymous receive buffers" available and
41 * it handles incoming data as it arrives (when the destination
42 * recieves an interrupt).
44 * The sender may send a simple buffer (address/length) or it may
45 * send a small list of buffers. When a small list is sent, hardware
46 * "gathers" these and they end up in a single destination buffer
47 * with a single interrupt.
49 * There are several "contexts" managed by this layer -- more, it
50 * may seem -- than should be needed. These are provided mainly for
51 * maximum flexibility and especially to facilitate a simpler HIF
52 * implementation. There are per-CopyEngine recv, send, and watermark
53 * contexts. These are supplied by the caller when a recv, send,
54 * or watermark handler is established and they are echoed back to
55 * the caller when the respective callbacks are invoked. There is
56 * also a per-transfer context supplied by the caller when a buffer
57 * (or sendlist) is sent and when a buffer is enqueued for recv.
58 * These per-transfer contexts are echoed back to the caller when
59 * the buffer is sent/received.
62 static inline void ath10k_ce_dest_ring_write_index_set(struct ath10k
*ar
,
66 ath10k_pci_write32(ar
, ce_ctrl_addr
+ DST_WR_INDEX_ADDRESS
, n
);
69 static inline u32
ath10k_ce_dest_ring_write_index_get(struct ath10k
*ar
,
72 return ath10k_pci_read32(ar
, ce_ctrl_addr
+ DST_WR_INDEX_ADDRESS
);
75 static inline void ath10k_ce_src_ring_write_index_set(struct ath10k
*ar
,
79 struct ath10k_pci
*ar_pci
= ath10k_pci_priv(ar
);
80 void __iomem
*indicator_addr
;
82 if (!test_bit(ATH10K_PCI_FEATURE_HW_1_0_WORKAROUND
, ar_pci
->features
)) {
83 ath10k_pci_write32(ar
, ce_ctrl_addr
+ SR_WR_INDEX_ADDRESS
, n
);
87 /* workaround for QCA988x_1.0 HW CE */
88 indicator_addr
= ar_pci
->mem
+ ce_ctrl_addr
+ DST_WATERMARK_ADDRESS
;
90 if (ce_ctrl_addr
== ath10k_ce_base_address(CDC_WAR_DATA_CE
)) {
91 iowrite32((CDC_WAR_MAGIC_STR
| n
), indicator_addr
);
93 unsigned long irq_flags
;
94 local_irq_save(irq_flags
);
95 iowrite32(1, indicator_addr
);
98 * PCIE write waits for ACK in IPQ8K, there is no
99 * need to read back value.
101 (void)ioread32(indicator_addr
);
102 (void)ioread32(indicator_addr
); /* conservative */
104 ath10k_pci_write32(ar
, ce_ctrl_addr
+ SR_WR_INDEX_ADDRESS
, n
);
106 iowrite32(0, indicator_addr
);
107 local_irq_restore(irq_flags
);
111 static inline u32
ath10k_ce_src_ring_write_index_get(struct ath10k
*ar
,
114 return ath10k_pci_read32(ar
, ce_ctrl_addr
+ SR_WR_INDEX_ADDRESS
);
117 static inline u32
ath10k_ce_src_ring_read_index_get(struct ath10k
*ar
,
120 return ath10k_pci_read32(ar
, ce_ctrl_addr
+ CURRENT_SRRI_ADDRESS
);
123 static inline void ath10k_ce_src_ring_base_addr_set(struct ath10k
*ar
,
127 ath10k_pci_write32(ar
, ce_ctrl_addr
+ SR_BA_ADDRESS
, addr
);
130 static inline void ath10k_ce_src_ring_size_set(struct ath10k
*ar
,
134 ath10k_pci_write32(ar
, ce_ctrl_addr
+ SR_SIZE_ADDRESS
, n
);
137 static inline void ath10k_ce_src_ring_dmax_set(struct ath10k
*ar
,
141 u32 ctrl1_addr
= ath10k_pci_read32((ar
),
142 (ce_ctrl_addr
) + CE_CTRL1_ADDRESS
);
144 ath10k_pci_write32(ar
, ce_ctrl_addr
+ CE_CTRL1_ADDRESS
,
145 (ctrl1_addr
& ~CE_CTRL1_DMAX_LENGTH_MASK
) |
146 CE_CTRL1_DMAX_LENGTH_SET(n
));
149 static inline void ath10k_ce_src_ring_byte_swap_set(struct ath10k
*ar
,
153 u32 ctrl1_addr
= ath10k_pci_read32(ar
, ce_ctrl_addr
+ CE_CTRL1_ADDRESS
);
155 ath10k_pci_write32(ar
, ce_ctrl_addr
+ CE_CTRL1_ADDRESS
,
156 (ctrl1_addr
& ~CE_CTRL1_SRC_RING_BYTE_SWAP_EN_MASK
) |
157 CE_CTRL1_SRC_RING_BYTE_SWAP_EN_SET(n
));
160 static inline void ath10k_ce_dest_ring_byte_swap_set(struct ath10k
*ar
,
164 u32 ctrl1_addr
= ath10k_pci_read32(ar
, ce_ctrl_addr
+ CE_CTRL1_ADDRESS
);
166 ath10k_pci_write32(ar
, ce_ctrl_addr
+ CE_CTRL1_ADDRESS
,
167 (ctrl1_addr
& ~CE_CTRL1_DST_RING_BYTE_SWAP_EN_MASK
) |
168 CE_CTRL1_DST_RING_BYTE_SWAP_EN_SET(n
));
171 static inline u32
ath10k_ce_dest_ring_read_index_get(struct ath10k
*ar
,
174 return ath10k_pci_read32(ar
, ce_ctrl_addr
+ CURRENT_DRRI_ADDRESS
);
177 static inline void ath10k_ce_dest_ring_base_addr_set(struct ath10k
*ar
,
181 ath10k_pci_write32(ar
, ce_ctrl_addr
+ DR_BA_ADDRESS
, addr
);
184 static inline void ath10k_ce_dest_ring_size_set(struct ath10k
*ar
,
188 ath10k_pci_write32(ar
, ce_ctrl_addr
+ DR_SIZE_ADDRESS
, n
);
191 static inline void ath10k_ce_src_ring_highmark_set(struct ath10k
*ar
,
195 u32 addr
= ath10k_pci_read32(ar
, ce_ctrl_addr
+ SRC_WATERMARK_ADDRESS
);
197 ath10k_pci_write32(ar
, ce_ctrl_addr
+ SRC_WATERMARK_ADDRESS
,
198 (addr
& ~SRC_WATERMARK_HIGH_MASK
) |
199 SRC_WATERMARK_HIGH_SET(n
));
202 static inline void ath10k_ce_src_ring_lowmark_set(struct ath10k
*ar
,
206 u32 addr
= ath10k_pci_read32(ar
, ce_ctrl_addr
+ SRC_WATERMARK_ADDRESS
);
208 ath10k_pci_write32(ar
, ce_ctrl_addr
+ SRC_WATERMARK_ADDRESS
,
209 (addr
& ~SRC_WATERMARK_LOW_MASK
) |
210 SRC_WATERMARK_LOW_SET(n
));
213 static inline void ath10k_ce_dest_ring_highmark_set(struct ath10k
*ar
,
217 u32 addr
= ath10k_pci_read32(ar
, ce_ctrl_addr
+ DST_WATERMARK_ADDRESS
);
219 ath10k_pci_write32(ar
, ce_ctrl_addr
+ DST_WATERMARK_ADDRESS
,
220 (addr
& ~DST_WATERMARK_HIGH_MASK
) |
221 DST_WATERMARK_HIGH_SET(n
));
224 static inline void ath10k_ce_dest_ring_lowmark_set(struct ath10k
*ar
,
228 u32 addr
= ath10k_pci_read32(ar
, ce_ctrl_addr
+ DST_WATERMARK_ADDRESS
);
230 ath10k_pci_write32(ar
, ce_ctrl_addr
+ DST_WATERMARK_ADDRESS
,
231 (addr
& ~DST_WATERMARK_LOW_MASK
) |
232 DST_WATERMARK_LOW_SET(n
));
235 static inline void ath10k_ce_copy_complete_inter_enable(struct ath10k
*ar
,
238 u32 host_ie_addr
= ath10k_pci_read32(ar
,
239 ce_ctrl_addr
+ HOST_IE_ADDRESS
);
241 ath10k_pci_write32(ar
, ce_ctrl_addr
+ HOST_IE_ADDRESS
,
242 host_ie_addr
| HOST_IE_COPY_COMPLETE_MASK
);
245 static inline void ath10k_ce_copy_complete_intr_disable(struct ath10k
*ar
,
248 u32 host_ie_addr
= ath10k_pci_read32(ar
,
249 ce_ctrl_addr
+ HOST_IE_ADDRESS
);
251 ath10k_pci_write32(ar
, ce_ctrl_addr
+ HOST_IE_ADDRESS
,
252 host_ie_addr
& ~HOST_IE_COPY_COMPLETE_MASK
);
255 static inline void ath10k_ce_watermark_intr_disable(struct ath10k
*ar
,
258 u32 host_ie_addr
= ath10k_pci_read32(ar
,
259 ce_ctrl_addr
+ HOST_IE_ADDRESS
);
261 ath10k_pci_write32(ar
, ce_ctrl_addr
+ HOST_IE_ADDRESS
,
262 host_ie_addr
& ~CE_WATERMARK_MASK
);
265 static inline void ath10k_ce_error_intr_enable(struct ath10k
*ar
,
268 u32 misc_ie_addr
= ath10k_pci_read32(ar
,
269 ce_ctrl_addr
+ MISC_IE_ADDRESS
);
271 ath10k_pci_write32(ar
, ce_ctrl_addr
+ MISC_IE_ADDRESS
,
272 misc_ie_addr
| CE_ERROR_MASK
);
275 static inline void ath10k_ce_engine_int_status_clear(struct ath10k
*ar
,
279 ath10k_pci_write32(ar
, ce_ctrl_addr
+ HOST_IS_ADDRESS
, mask
);
284 * Guts of ath10k_ce_send, used by both ath10k_ce_send and
285 * ath10k_ce_sendlist_send.
286 * The caller takes responsibility for any needed locking.
288 static int ath10k_ce_send_nolock(struct ce_state
*ce_state
,
289 void *per_transfer_context
,
292 unsigned int transfer_id
,
295 struct ath10k
*ar
= ce_state
->ar
;
296 struct ce_ring_state
*src_ring
= ce_state
->src_ring
;
297 struct ce_desc
*desc
, *sdesc
;
298 unsigned int nentries_mask
= src_ring
->nentries_mask
;
299 unsigned int sw_index
= src_ring
->sw_index
;
300 unsigned int write_index
= src_ring
->write_index
;
301 u32 ctrl_addr
= ce_state
->ctrl_addr
;
305 if (nbytes
> ce_state
->src_sz_max
)
306 ath10k_warn("%s: send more we can (nbytes: %d, max: %d)\n",
307 __func__
, nbytes
, ce_state
->src_sz_max
);
311 if (unlikely(CE_RING_DELTA(nentries_mask
,
312 write_index
, sw_index
- 1) <= 0)) {
317 desc
= CE_SRC_RING_TO_DESC(src_ring
->base_addr_owner_space
,
319 sdesc
= CE_SRC_RING_TO_DESC(src_ring
->shadow_base
, write_index
);
321 desc_flags
|= SM(transfer_id
, CE_DESC_FLAGS_META_DATA
);
323 if (flags
& CE_SEND_FLAG_GATHER
)
324 desc_flags
|= CE_DESC_FLAGS_GATHER
;
325 if (flags
& CE_SEND_FLAG_BYTE_SWAP
)
326 desc_flags
|= CE_DESC_FLAGS_BYTE_SWAP
;
328 sdesc
->addr
= __cpu_to_le32(buffer
);
329 sdesc
->nbytes
= __cpu_to_le16(nbytes
);
330 sdesc
->flags
= __cpu_to_le16(desc_flags
);
334 src_ring
->per_transfer_context
[write_index
] = per_transfer_context
;
336 /* Update Source Ring Write Index */
337 write_index
= CE_RING_IDX_INCR(nentries_mask
, write_index
);
340 if (!(flags
& CE_SEND_FLAG_GATHER
))
341 ath10k_ce_src_ring_write_index_set(ar
, ctrl_addr
, write_index
);
343 src_ring
->write_index
= write_index
;
345 ath10k_pci_sleep(ar
);
349 int ath10k_ce_send(struct ce_state
*ce_state
,
350 void *per_transfer_context
,
353 unsigned int transfer_id
,
356 struct ath10k
*ar
= ce_state
->ar
;
357 struct ath10k_pci
*ar_pci
= ath10k_pci_priv(ar
);
360 spin_lock_bh(&ar_pci
->ce_lock
);
361 ret
= ath10k_ce_send_nolock(ce_state
, per_transfer_context
,
362 buffer
, nbytes
, transfer_id
, flags
);
363 spin_unlock_bh(&ar_pci
->ce_lock
);
368 void ath10k_ce_sendlist_buf_add(struct ce_sendlist
*sendlist
, u32 buffer
,
369 unsigned int nbytes
, u32 flags
)
371 unsigned int num_items
= sendlist
->num_items
;
372 struct ce_sendlist_item
*item
;
374 item
= &sendlist
->item
[num_items
];
376 item
->u
.nbytes
= nbytes
;
378 sendlist
->num_items
++;
381 int ath10k_ce_sendlist_send(struct ce_state
*ce_state
,
382 void *per_transfer_context
,
383 struct ce_sendlist
*sendlist
,
384 unsigned int transfer_id
)
386 struct ce_ring_state
*src_ring
= ce_state
->src_ring
;
387 struct ce_sendlist_item
*item
;
388 struct ath10k
*ar
= ce_state
->ar
;
389 struct ath10k_pci
*ar_pci
= ath10k_pci_priv(ar
);
390 unsigned int nentries_mask
= src_ring
->nentries_mask
;
391 unsigned int num_items
= sendlist
->num_items
;
392 unsigned int sw_index
;
393 unsigned int write_index
;
394 int i
, delta
, ret
= -ENOMEM
;
396 spin_lock_bh(&ar_pci
->ce_lock
);
398 sw_index
= src_ring
->sw_index
;
399 write_index
= src_ring
->write_index
;
401 delta
= CE_RING_DELTA(nentries_mask
, write_index
, sw_index
- 1);
403 if (delta
>= num_items
) {
405 * Handle all but the last item uniformly.
407 for (i
= 0; i
< num_items
- 1; i
++) {
408 item
= &sendlist
->item
[i
];
409 ret
= ath10k_ce_send_nolock(ce_state
,
410 CE_SENDLIST_ITEM_CTXT
,
412 item
->u
.nbytes
, transfer_id
,
414 CE_SEND_FLAG_GATHER
);
416 ath10k_warn("CE send failed for item: %d\n", i
);
419 * Provide valid context pointer for final item.
421 item
= &sendlist
->item
[i
];
422 ret
= ath10k_ce_send_nolock(ce_state
, per_transfer_context
,
423 (u32
) item
->data
, item
->u
.nbytes
,
424 transfer_id
, item
->flags
);
426 ath10k_warn("CE send failed for last item: %d\n", i
);
429 spin_unlock_bh(&ar_pci
->ce_lock
);
434 int ath10k_ce_recv_buf_enqueue(struct ce_state
*ce_state
,
435 void *per_recv_context
,
438 struct ce_ring_state
*dest_ring
= ce_state
->dest_ring
;
439 u32 ctrl_addr
= ce_state
->ctrl_addr
;
440 struct ath10k
*ar
= ce_state
->ar
;
441 struct ath10k_pci
*ar_pci
= ath10k_pci_priv(ar
);
442 unsigned int nentries_mask
= dest_ring
->nentries_mask
;
443 unsigned int write_index
;
444 unsigned int sw_index
;
447 spin_lock_bh(&ar_pci
->ce_lock
);
448 write_index
= dest_ring
->write_index
;
449 sw_index
= dest_ring
->sw_index
;
453 if (CE_RING_DELTA(nentries_mask
, write_index
, sw_index
- 1) > 0) {
454 struct ce_desc
*base
= dest_ring
->base_addr_owner_space
;
455 struct ce_desc
*desc
= CE_DEST_RING_TO_DESC(base
, write_index
);
457 /* Update destination descriptor */
458 desc
->addr
= __cpu_to_le32(buffer
);
461 dest_ring
->per_transfer_context
[write_index
] =
464 /* Update Destination Ring Write Index */
465 write_index
= CE_RING_IDX_INCR(nentries_mask
, write_index
);
466 ath10k_ce_dest_ring_write_index_set(ar
, ctrl_addr
, write_index
);
467 dest_ring
->write_index
= write_index
;
472 ath10k_pci_sleep(ar
);
473 spin_unlock_bh(&ar_pci
->ce_lock
);
479 * Guts of ath10k_ce_completed_recv_next.
480 * The caller takes responsibility for any necessary locking.
482 static int ath10k_ce_completed_recv_next_nolock(struct ce_state
*ce_state
,
483 void **per_transfer_contextp
,
485 unsigned int *nbytesp
,
486 unsigned int *transfer_idp
,
487 unsigned int *flagsp
)
489 struct ce_ring_state
*dest_ring
= ce_state
->dest_ring
;
490 unsigned int nentries_mask
= dest_ring
->nentries_mask
;
491 unsigned int sw_index
= dest_ring
->sw_index
;
493 struct ce_desc
*base
= dest_ring
->base_addr_owner_space
;
494 struct ce_desc
*desc
= CE_DEST_RING_TO_DESC(base
, sw_index
);
495 struct ce_desc sdesc
;
498 /* Copy in one go for performance reasons */
501 nbytes
= __le16_to_cpu(sdesc
.nbytes
);
504 * This closes a relatively unusual race where the Host
505 * sees the updated DRRI before the update to the
506 * corresponding descriptor has completed. We treat this
507 * as a descriptor that is not yet done.
514 /* Return data from completed destination descriptor */
515 *bufferp
= __le32_to_cpu(sdesc
.addr
);
517 *transfer_idp
= MS(__le16_to_cpu(sdesc
.flags
), CE_DESC_FLAGS_META_DATA
);
519 if (__le16_to_cpu(sdesc
.flags
) & CE_DESC_FLAGS_BYTE_SWAP
)
520 *flagsp
= CE_RECV_FLAG_SWAPPED
;
524 if (per_transfer_contextp
)
525 *per_transfer_contextp
=
526 dest_ring
->per_transfer_context
[sw_index
];
529 dest_ring
->per_transfer_context
[sw_index
] = NULL
;
531 /* Update sw_index */
532 sw_index
= CE_RING_IDX_INCR(nentries_mask
, sw_index
);
533 dest_ring
->sw_index
= sw_index
;
538 int ath10k_ce_completed_recv_next(struct ce_state
*ce_state
,
539 void **per_transfer_contextp
,
541 unsigned int *nbytesp
,
542 unsigned int *transfer_idp
,
543 unsigned int *flagsp
)
545 struct ath10k
*ar
= ce_state
->ar
;
546 struct ath10k_pci
*ar_pci
= ath10k_pci_priv(ar
);
549 spin_lock_bh(&ar_pci
->ce_lock
);
550 ret
= ath10k_ce_completed_recv_next_nolock(ce_state
,
551 per_transfer_contextp
,
553 transfer_idp
, flagsp
);
554 spin_unlock_bh(&ar_pci
->ce_lock
);
559 int ath10k_ce_revoke_recv_next(struct ce_state
*ce_state
,
560 void **per_transfer_contextp
,
563 struct ce_ring_state
*dest_ring
;
564 unsigned int nentries_mask
;
565 unsigned int sw_index
;
566 unsigned int write_index
;
569 struct ath10k_pci
*ar_pci
;
571 dest_ring
= ce_state
->dest_ring
;
577 ar_pci
= ath10k_pci_priv(ar
);
579 spin_lock_bh(&ar_pci
->ce_lock
);
581 nentries_mask
= dest_ring
->nentries_mask
;
582 sw_index
= dest_ring
->sw_index
;
583 write_index
= dest_ring
->write_index
;
584 if (write_index
!= sw_index
) {
585 struct ce_desc
*base
= dest_ring
->base_addr_owner_space
;
586 struct ce_desc
*desc
= CE_DEST_RING_TO_DESC(base
, sw_index
);
588 /* Return data from completed destination descriptor */
589 *bufferp
= __le32_to_cpu(desc
->addr
);
591 if (per_transfer_contextp
)
592 *per_transfer_contextp
=
593 dest_ring
->per_transfer_context
[sw_index
];
596 dest_ring
->per_transfer_context
[sw_index
] = NULL
;
598 /* Update sw_index */
599 sw_index
= CE_RING_IDX_INCR(nentries_mask
, sw_index
);
600 dest_ring
->sw_index
= sw_index
;
606 spin_unlock_bh(&ar_pci
->ce_lock
);
612 * Guts of ath10k_ce_completed_send_next.
613 * The caller takes responsibility for any necessary locking.
615 static int ath10k_ce_completed_send_next_nolock(struct ce_state
*ce_state
,
616 void **per_transfer_contextp
,
618 unsigned int *nbytesp
,
619 unsigned int *transfer_idp
)
621 struct ce_ring_state
*src_ring
= ce_state
->src_ring
;
622 u32 ctrl_addr
= ce_state
->ctrl_addr
;
623 struct ath10k
*ar
= ce_state
->ar
;
624 unsigned int nentries_mask
= src_ring
->nentries_mask
;
625 unsigned int sw_index
= src_ring
->sw_index
;
626 unsigned int read_index
;
629 if (src_ring
->hw_index
== sw_index
) {
631 * The SW completion index has caught up with the cached
632 * version of the HW completion index.
633 * Update the cached HW completion index to see whether
634 * the SW has really caught up to the HW, or if the cached
635 * value of the HW index has become stale.
639 ath10k_ce_src_ring_read_index_get(ar
, ctrl_addr
);
640 src_ring
->hw_index
&= nentries_mask
;
641 ath10k_pci_sleep(ar
);
643 read_index
= src_ring
->hw_index
;
645 if ((read_index
!= sw_index
) && (read_index
!= 0xffffffff)) {
646 struct ce_desc
*sbase
= src_ring
->shadow_base
;
647 struct ce_desc
*sdesc
= CE_SRC_RING_TO_DESC(sbase
, sw_index
);
649 /* Return data from completed source descriptor */
650 *bufferp
= __le32_to_cpu(sdesc
->addr
);
651 *nbytesp
= __le16_to_cpu(sdesc
->nbytes
);
652 *transfer_idp
= MS(__le16_to_cpu(sdesc
->flags
),
653 CE_DESC_FLAGS_META_DATA
);
655 if (per_transfer_contextp
)
656 *per_transfer_contextp
=
657 src_ring
->per_transfer_context
[sw_index
];
660 src_ring
->per_transfer_context
[sw_index
] = NULL
;
662 /* Update sw_index */
663 sw_index
= CE_RING_IDX_INCR(nentries_mask
, sw_index
);
664 src_ring
->sw_index
= sw_index
;
671 /* NB: Modeled after ath10k_ce_completed_send_next */
672 int ath10k_ce_cancel_send_next(struct ce_state
*ce_state
,
673 void **per_transfer_contextp
,
675 unsigned int *nbytesp
,
676 unsigned int *transfer_idp
)
678 struct ce_ring_state
*src_ring
;
679 unsigned int nentries_mask
;
680 unsigned int sw_index
;
681 unsigned int write_index
;
684 struct ath10k_pci
*ar_pci
;
686 src_ring
= ce_state
->src_ring
;
692 ar_pci
= ath10k_pci_priv(ar
);
694 spin_lock_bh(&ar_pci
->ce_lock
);
696 nentries_mask
= src_ring
->nentries_mask
;
697 sw_index
= src_ring
->sw_index
;
698 write_index
= src_ring
->write_index
;
700 if (write_index
!= sw_index
) {
701 struct ce_desc
*base
= src_ring
->base_addr_owner_space
;
702 struct ce_desc
*desc
= CE_SRC_RING_TO_DESC(base
, sw_index
);
704 /* Return data from completed source descriptor */
705 *bufferp
= __le32_to_cpu(desc
->addr
);
706 *nbytesp
= __le16_to_cpu(desc
->nbytes
);
707 *transfer_idp
= MS(__le16_to_cpu(desc
->flags
),
708 CE_DESC_FLAGS_META_DATA
);
710 if (per_transfer_contextp
)
711 *per_transfer_contextp
=
712 src_ring
->per_transfer_context
[sw_index
];
715 src_ring
->per_transfer_context
[sw_index
] = NULL
;
717 /* Update sw_index */
718 sw_index
= CE_RING_IDX_INCR(nentries_mask
, sw_index
);
719 src_ring
->sw_index
= sw_index
;
725 spin_unlock_bh(&ar_pci
->ce_lock
);
730 int ath10k_ce_completed_send_next(struct ce_state
*ce_state
,
731 void **per_transfer_contextp
,
733 unsigned int *nbytesp
,
734 unsigned int *transfer_idp
)
736 struct ath10k
*ar
= ce_state
->ar
;
737 struct ath10k_pci
*ar_pci
= ath10k_pci_priv(ar
);
740 spin_lock_bh(&ar_pci
->ce_lock
);
741 ret
= ath10k_ce_completed_send_next_nolock(ce_state
,
742 per_transfer_contextp
,
745 spin_unlock_bh(&ar_pci
->ce_lock
);
751 * Guts of interrupt handler for per-engine interrupts on a particular CE.
753 * Invokes registered callbacks for recv_complete,
754 * send_complete, and watermarks.
756 void ath10k_ce_per_engine_service(struct ath10k
*ar
, unsigned int ce_id
)
758 struct ath10k_pci
*ar_pci
= ath10k_pci_priv(ar
);
759 struct ce_state
*ce_state
= ar_pci
->ce_id_to_state
[ce_id
];
760 u32 ctrl_addr
= ce_state
->ctrl_addr
;
761 void *transfer_context
;
768 spin_lock_bh(&ar_pci
->ce_lock
);
770 /* Clear the copy-complete interrupts that will be handled here. */
771 ath10k_ce_engine_int_status_clear(ar
, ctrl_addr
,
772 HOST_IS_COPY_COMPLETE_MASK
);
774 if (ce_state
->recv_cb
) {
776 * Pop completed recv buffers and call the registered
777 * recv callback for each
779 while (ath10k_ce_completed_recv_next_nolock(ce_state
,
783 spin_unlock_bh(&ar_pci
->ce_lock
);
784 ce_state
->recv_cb(ce_state
, transfer_context
, buf
,
786 spin_lock_bh(&ar_pci
->ce_lock
);
790 if (ce_state
->send_cb
) {
792 * Pop completed send buffers and call the registered
793 * send callback for each
795 while (ath10k_ce_completed_send_next_nolock(ce_state
,
800 spin_unlock_bh(&ar_pci
->ce_lock
);
801 ce_state
->send_cb(ce_state
, transfer_context
,
803 spin_lock_bh(&ar_pci
->ce_lock
);
808 * Misc CE interrupts are not being handled, but still need
811 ath10k_ce_engine_int_status_clear(ar
, ctrl_addr
, CE_WATERMARK_MASK
);
813 spin_unlock_bh(&ar_pci
->ce_lock
);
814 ath10k_pci_sleep(ar
);
818 * Handler for per-engine interrupts on ALL active CEs.
819 * This is used in cases where the system is sharing a
820 * single interrput for all CEs
823 void ath10k_ce_per_engine_service_any(struct ath10k
*ar
)
825 struct ath10k_pci
*ar_pci
= ath10k_pci_priv(ar
);
830 intr_summary
= CE_INTERRUPT_SUMMARY(ar
);
832 for (ce_id
= 0; intr_summary
&& (ce_id
< ar_pci
->ce_count
); ce_id
++) {
833 if (intr_summary
& (1 << ce_id
))
834 intr_summary
&= ~(1 << ce_id
);
836 /* no intr pending on this CE */
839 ath10k_ce_per_engine_service(ar
, ce_id
);
842 ath10k_pci_sleep(ar
);
846 * Adjust interrupts for the copy complete handler.
847 * If it's needed for either send or recv, then unmask
848 * this interrupt; otherwise, mask it.
850 * Called with ce_lock held.
852 static void ath10k_ce_per_engine_handler_adjust(struct ce_state
*ce_state
,
853 int disable_copy_compl_intr
)
855 u32 ctrl_addr
= ce_state
->ctrl_addr
;
856 struct ath10k
*ar
= ce_state
->ar
;
860 if ((!disable_copy_compl_intr
) &&
861 (ce_state
->send_cb
|| ce_state
->recv_cb
))
862 ath10k_ce_copy_complete_inter_enable(ar
, ctrl_addr
);
864 ath10k_ce_copy_complete_intr_disable(ar
, ctrl_addr
);
866 ath10k_ce_watermark_intr_disable(ar
, ctrl_addr
);
868 ath10k_pci_sleep(ar
);
871 void ath10k_ce_disable_interrupts(struct ath10k
*ar
)
873 struct ath10k_pci
*ar_pci
= ath10k_pci_priv(ar
);
877 for (ce_id
= 0; ce_id
< ar_pci
->ce_count
; ce_id
++) {
878 struct ce_state
*ce_state
= ar_pci
->ce_id_to_state
[ce_id
];
879 u32 ctrl_addr
= ce_state
->ctrl_addr
;
881 ath10k_ce_copy_complete_intr_disable(ar
, ctrl_addr
);
883 ath10k_pci_sleep(ar
);
886 void ath10k_ce_send_cb_register(struct ce_state
*ce_state
,
887 void (*send_cb
) (struct ce_state
*ce_state
,
888 void *transfer_context
,
891 unsigned int transfer_id
),
892 int disable_interrupts
)
894 struct ath10k
*ar
= ce_state
->ar
;
895 struct ath10k_pci
*ar_pci
= ath10k_pci_priv(ar
);
897 spin_lock_bh(&ar_pci
->ce_lock
);
898 ce_state
->send_cb
= send_cb
;
899 ath10k_ce_per_engine_handler_adjust(ce_state
, disable_interrupts
);
900 spin_unlock_bh(&ar_pci
->ce_lock
);
903 void ath10k_ce_recv_cb_register(struct ce_state
*ce_state
,
904 void (*recv_cb
) (struct ce_state
*ce_state
,
905 void *transfer_context
,
908 unsigned int transfer_id
,
911 struct ath10k
*ar
= ce_state
->ar
;
912 struct ath10k_pci
*ar_pci
= ath10k_pci_priv(ar
);
914 spin_lock_bh(&ar_pci
->ce_lock
);
915 ce_state
->recv_cb
= recv_cb
;
916 ath10k_ce_per_engine_handler_adjust(ce_state
, 0);
917 spin_unlock_bh(&ar_pci
->ce_lock
);
920 static int ath10k_ce_init_src_ring(struct ath10k
*ar
,
922 struct ce_state
*ce_state
,
923 const struct ce_attr
*attr
)
925 struct ath10k_pci
*ar_pci
= ath10k_pci_priv(ar
);
926 struct ce_ring_state
*src_ring
;
927 unsigned int nentries
= attr
->src_nentries
;
928 unsigned int ce_nbytes
;
929 u32 ctrl_addr
= ath10k_ce_base_address(ce_id
);
930 dma_addr_t base_addr
;
933 nentries
= roundup_pow_of_two(nentries
);
935 if (ce_state
->src_ring
) {
936 WARN_ON(ce_state
->src_ring
->nentries
!= nentries
);
940 ce_nbytes
= sizeof(struct ce_ring_state
) + (nentries
* sizeof(void *));
941 ptr
= kzalloc(ce_nbytes
, GFP_KERNEL
);
945 ce_state
->src_ring
= (struct ce_ring_state
*)ptr
;
946 src_ring
= ce_state
->src_ring
;
948 ptr
+= sizeof(struct ce_ring_state
);
949 src_ring
->nentries
= nentries
;
950 src_ring
->nentries_mask
= nentries
- 1;
953 src_ring
->sw_index
= ath10k_ce_src_ring_read_index_get(ar
, ctrl_addr
);
954 src_ring
->sw_index
&= src_ring
->nentries_mask
;
955 src_ring
->hw_index
= src_ring
->sw_index
;
957 src_ring
->write_index
=
958 ath10k_ce_src_ring_write_index_get(ar
, ctrl_addr
);
959 src_ring
->write_index
&= src_ring
->nentries_mask
;
960 ath10k_pci_sleep(ar
);
962 src_ring
->per_transfer_context
= (void **)ptr
;
965 * Legacy platforms that do not support cache
966 * coherent DMA are unsupported
968 src_ring
->base_addr_owner_space_unaligned
=
969 pci_alloc_consistent(ar_pci
->pdev
,
970 (nentries
* sizeof(struct ce_desc
) +
973 src_ring
->base_addr_ce_space_unaligned
= base_addr
;
975 src_ring
->base_addr_owner_space
= PTR_ALIGN(
976 src_ring
->base_addr_owner_space_unaligned
,
978 src_ring
->base_addr_ce_space
= ALIGN(
979 src_ring
->base_addr_ce_space_unaligned
,
983 * Also allocate a shadow src ring in regular
984 * mem to use for faster access.
986 src_ring
->shadow_base_unaligned
=
987 kmalloc((nentries
* sizeof(struct ce_desc
) +
988 CE_DESC_RING_ALIGN
), GFP_KERNEL
);
990 src_ring
->shadow_base
= PTR_ALIGN(
991 src_ring
->shadow_base_unaligned
,
995 ath10k_ce_src_ring_base_addr_set(ar
, ctrl_addr
,
996 src_ring
->base_addr_ce_space
);
997 ath10k_ce_src_ring_size_set(ar
, ctrl_addr
, nentries
);
998 ath10k_ce_src_ring_dmax_set(ar
, ctrl_addr
, attr
->src_sz_max
);
999 ath10k_ce_src_ring_byte_swap_set(ar
, ctrl_addr
, 0);
1000 ath10k_ce_src_ring_lowmark_set(ar
, ctrl_addr
, 0);
1001 ath10k_ce_src_ring_highmark_set(ar
, ctrl_addr
, nentries
);
1002 ath10k_pci_sleep(ar
);
1007 static int ath10k_ce_init_dest_ring(struct ath10k
*ar
,
1009 struct ce_state
*ce_state
,
1010 const struct ce_attr
*attr
)
1012 struct ath10k_pci
*ar_pci
= ath10k_pci_priv(ar
);
1013 struct ce_ring_state
*dest_ring
;
1014 unsigned int nentries
= attr
->dest_nentries
;
1015 unsigned int ce_nbytes
;
1016 u32 ctrl_addr
= ath10k_ce_base_address(ce_id
);
1017 dma_addr_t base_addr
;
1020 nentries
= roundup_pow_of_two(nentries
);
1022 if (ce_state
->dest_ring
) {
1023 WARN_ON(ce_state
->dest_ring
->nentries
!= nentries
);
1027 ce_nbytes
= sizeof(struct ce_ring_state
) + (nentries
* sizeof(void *));
1028 ptr
= kzalloc(ce_nbytes
, GFP_KERNEL
);
1032 ce_state
->dest_ring
= (struct ce_ring_state
*)ptr
;
1033 dest_ring
= ce_state
->dest_ring
;
1035 ptr
+= sizeof(struct ce_ring_state
);
1036 dest_ring
->nentries
= nentries
;
1037 dest_ring
->nentries_mask
= nentries
- 1;
1039 ath10k_pci_wake(ar
);
1040 dest_ring
->sw_index
= ath10k_ce_dest_ring_read_index_get(ar
, ctrl_addr
);
1041 dest_ring
->sw_index
&= dest_ring
->nentries_mask
;
1042 dest_ring
->write_index
=
1043 ath10k_ce_dest_ring_write_index_get(ar
, ctrl_addr
);
1044 dest_ring
->write_index
&= dest_ring
->nentries_mask
;
1045 ath10k_pci_sleep(ar
);
1047 dest_ring
->per_transfer_context
= (void **)ptr
;
1050 * Legacy platforms that do not support cache
1051 * coherent DMA are unsupported
1053 dest_ring
->base_addr_owner_space_unaligned
=
1054 pci_alloc_consistent(ar_pci
->pdev
,
1055 (nentries
* sizeof(struct ce_desc
) +
1056 CE_DESC_RING_ALIGN
),
1058 dest_ring
->base_addr_ce_space_unaligned
= base_addr
;
1061 * Correctly initialize memory to 0 to prevent garbage
1062 * data crashing system when download firmware
1064 memset(dest_ring
->base_addr_owner_space_unaligned
, 0,
1065 nentries
* sizeof(struct ce_desc
) + CE_DESC_RING_ALIGN
);
1067 dest_ring
->base_addr_owner_space
= PTR_ALIGN(
1068 dest_ring
->base_addr_owner_space_unaligned
,
1069 CE_DESC_RING_ALIGN
);
1070 dest_ring
->base_addr_ce_space
= ALIGN(
1071 dest_ring
->base_addr_ce_space_unaligned
,
1072 CE_DESC_RING_ALIGN
);
1074 ath10k_pci_wake(ar
);
1075 ath10k_ce_dest_ring_base_addr_set(ar
, ctrl_addr
,
1076 dest_ring
->base_addr_ce_space
);
1077 ath10k_ce_dest_ring_size_set(ar
, ctrl_addr
, nentries
);
1078 ath10k_ce_dest_ring_byte_swap_set(ar
, ctrl_addr
, 0);
1079 ath10k_ce_dest_ring_lowmark_set(ar
, ctrl_addr
, 0);
1080 ath10k_ce_dest_ring_highmark_set(ar
, ctrl_addr
, nentries
);
1081 ath10k_pci_sleep(ar
);
1086 static struct ce_state
*ath10k_ce_init_state(struct ath10k
*ar
,
1088 const struct ce_attr
*attr
)
1090 struct ath10k_pci
*ar_pci
= ath10k_pci_priv(ar
);
1091 struct ce_state
*ce_state
= NULL
;
1092 u32 ctrl_addr
= ath10k_ce_base_address(ce_id
);
1094 spin_lock_bh(&ar_pci
->ce_lock
);
1096 if (!ar_pci
->ce_id_to_state
[ce_id
]) {
1097 ce_state
= kzalloc(sizeof(*ce_state
), GFP_ATOMIC
);
1098 if (ce_state
== NULL
) {
1099 spin_unlock_bh(&ar_pci
->ce_lock
);
1103 ar_pci
->ce_id_to_state
[ce_id
] = ce_state
;
1105 ce_state
->id
= ce_id
;
1106 ce_state
->ctrl_addr
= ctrl_addr
;
1107 ce_state
->state
= CE_RUNNING
;
1108 /* Save attribute flags */
1109 ce_state
->attr_flags
= attr
->flags
;
1110 ce_state
->src_sz_max
= attr
->src_sz_max
;
1113 spin_unlock_bh(&ar_pci
->ce_lock
);
1119 * Initialize a Copy Engine based on caller-supplied attributes.
1120 * This may be called once to initialize both source and destination
1121 * rings or it may be called twice for separate source and destination
1122 * initialization. It may be that only one side or the other is
1123 * initialized by software/firmware.
1125 struct ce_state
*ath10k_ce_init(struct ath10k
*ar
,
1127 const struct ce_attr
*attr
)
1129 struct ce_state
*ce_state
;
1130 u32 ctrl_addr
= ath10k_ce_base_address(ce_id
);
1132 ce_state
= ath10k_ce_init_state(ar
, ce_id
, attr
);
1134 ath10k_err("Failed to initialize CE state for ID: %d\n", ce_id
);
1138 if (attr
->src_nentries
) {
1139 if (ath10k_ce_init_src_ring(ar
, ce_id
, ce_state
, attr
)) {
1140 ath10k_err("Failed to initialize CE src ring for ID: %d\n",
1142 ath10k_ce_deinit(ce_state
);
1147 if (attr
->dest_nentries
) {
1148 if (ath10k_ce_init_dest_ring(ar
, ce_id
, ce_state
, attr
)) {
1149 ath10k_err("Failed to initialize CE dest ring for ID: %d\n",
1151 ath10k_ce_deinit(ce_state
);
1156 /* Enable CE error interrupts */
1157 ath10k_pci_wake(ar
);
1158 ath10k_ce_error_intr_enable(ar
, ctrl_addr
);
1159 ath10k_pci_sleep(ar
);
1164 void ath10k_ce_deinit(struct ce_state
*ce_state
)
1166 unsigned int ce_id
= ce_state
->id
;
1167 struct ath10k
*ar
= ce_state
->ar
;
1168 struct ath10k_pci
*ar_pci
= ath10k_pci_priv(ar
);
1170 ce_state
->state
= CE_UNUSED
;
1171 ar_pci
->ce_id_to_state
[ce_id
] = NULL
;
1173 if (ce_state
->src_ring
) {
1174 kfree(ce_state
->src_ring
->shadow_base_unaligned
);
1175 pci_free_consistent(ar_pci
->pdev
,
1176 (ce_state
->src_ring
->nentries
*
1177 sizeof(struct ce_desc
) +
1178 CE_DESC_RING_ALIGN
),
1179 ce_state
->src_ring
->base_addr_owner_space
,
1180 ce_state
->src_ring
->base_addr_ce_space
);
1181 kfree(ce_state
->src_ring
);
1184 if (ce_state
->dest_ring
) {
1185 pci_free_consistent(ar_pci
->pdev
,
1186 (ce_state
->dest_ring
->nentries
*
1187 sizeof(struct ce_desc
) +
1188 CE_DESC_RING_ALIGN
),
1189 ce_state
->dest_ring
->base_addr_owner_space
,
1190 ce_state
->dest_ring
->base_addr_ce_space
);
1191 kfree(ce_state
->dest_ring
);