xref: /openbmc/linux/drivers/net/wireless/ath/ath11k/ce.c (revision e838c14a9ee1dfe660527128d8f2e3191c8b1aad)
1d5c65159SKalle Valo // SPDX-License-Identifier: BSD-3-Clause-Clear
2d5c65159SKalle Valo /*
3d5c65159SKalle Valo  * Copyright (c) 2018-2019 The Linux Foundation. All rights reserved.
4d5c65159SKalle Valo  */
5d5c65159SKalle Valo 
6d5c65159SKalle Valo #include "dp_rx.h"
7d5c65159SKalle Valo #include "debug.h"
8c4eacabeSGovind Singh #include "hif.h"
9d5c65159SKalle Valo 
10e3396b8bSCarl Huang const struct ce_attr ath11k_host_ce_config_ipq8074[] = {
11d5c65159SKalle Valo 	/* CE0: host->target HTC control and raw streams */
12d5c65159SKalle Valo 	{
13d5c65159SKalle Valo 		.flags = CE_ATTR_FLAGS,
14d5c65159SKalle Valo 		.src_nentries = 16,
15d5c65159SKalle Valo 		.src_sz_max = 2048,
16d5c65159SKalle Valo 		.dest_nentries = 0,
17d5c65159SKalle Valo 	},
18d5c65159SKalle Valo 
19d5c65159SKalle Valo 	/* CE1: target->host HTT + HTC control */
20d5c65159SKalle Valo 	{
21d5c65159SKalle Valo 		.flags = CE_ATTR_FLAGS,
22d5c65159SKalle Valo 		.src_nentries = 0,
23d5c65159SKalle Valo 		.src_sz_max = 2048,
24d5c65159SKalle Valo 		.dest_nentries = 512,
25d5c65159SKalle Valo 		.recv_cb = ath11k_htc_rx_completion_handler,
26d5c65159SKalle Valo 	},
27d5c65159SKalle Valo 
28d5c65159SKalle Valo 	/* CE2: target->host WMI */
29d5c65159SKalle Valo 	{
30d5c65159SKalle Valo 		.flags = CE_ATTR_FLAGS,
31d5c65159SKalle Valo 		.src_nentries = 0,
32d5c65159SKalle Valo 		.src_sz_max = 2048,
33d5c65159SKalle Valo 		.dest_nentries = 512,
34d5c65159SKalle Valo 		.recv_cb = ath11k_htc_rx_completion_handler,
35d5c65159SKalle Valo 	},
36d5c65159SKalle Valo 
37d5c65159SKalle Valo 	/* CE3: host->target WMI (mac0) */
38d5c65159SKalle Valo 	{
39d5c65159SKalle Valo 		.flags = CE_ATTR_FLAGS,
40d5c65159SKalle Valo 		.src_nentries = 32,
41d5c65159SKalle Valo 		.src_sz_max = 2048,
42d5c65159SKalle Valo 		.dest_nentries = 0,
43d5c65159SKalle Valo 	},
44d5c65159SKalle Valo 
45d5c65159SKalle Valo 	/* CE4: host->target HTT */
46d5c65159SKalle Valo 	{
47d5c65159SKalle Valo 		.flags = CE_ATTR_FLAGS | CE_ATTR_DIS_INTR,
48d5c65159SKalle Valo 		.src_nentries = 2048,
49d5c65159SKalle Valo 		.src_sz_max = 256,
50d5c65159SKalle Valo 		.dest_nentries = 0,
51d5c65159SKalle Valo 	},
52d5c65159SKalle Valo 
53d5c65159SKalle Valo 	/* CE5: target->host pktlog */
54d5c65159SKalle Valo 	{
55d5c65159SKalle Valo 		.flags = CE_ATTR_FLAGS,
56d5c65159SKalle Valo 		.src_nentries = 0,
57d5c65159SKalle Valo 		.src_sz_max = 2048,
58d5c65159SKalle Valo 		.dest_nentries = 512,
59d5c65159SKalle Valo 		.recv_cb = ath11k_dp_htt_htc_t2h_msg_handler,
60d5c65159SKalle Valo 	},
61d5c65159SKalle Valo 
62d5c65159SKalle Valo 	/* CE6: target autonomous hif_memcpy */
63d5c65159SKalle Valo 	{
64d5c65159SKalle Valo 		.flags = CE_ATTR_FLAGS | CE_ATTR_DIS_INTR,
65d5c65159SKalle Valo 		.src_nentries = 0,
66d5c65159SKalle Valo 		.src_sz_max = 0,
67d5c65159SKalle Valo 		.dest_nentries = 0,
68d5c65159SKalle Valo 	},
69d5c65159SKalle Valo 
70d5c65159SKalle Valo 	/* CE7: host->target WMI (mac1) */
71d5c65159SKalle Valo 	{
72d5c65159SKalle Valo 		.flags = CE_ATTR_FLAGS,
73d5c65159SKalle Valo 		.src_nentries = 32,
74d5c65159SKalle Valo 		.src_sz_max = 2048,
75d5c65159SKalle Valo 		.dest_nentries = 0,
76d5c65159SKalle Valo 	},
77d5c65159SKalle Valo 
78d5c65159SKalle Valo 	/* CE8: target autonomous hif_memcpy */
79d5c65159SKalle Valo 	{
80d5c65159SKalle Valo 		.flags = CE_ATTR_FLAGS,
81d5c65159SKalle Valo 		.src_nentries = 0,
82d5c65159SKalle Valo 		.src_sz_max = 0,
83d5c65159SKalle Valo 		.dest_nentries = 0,
84d5c65159SKalle Valo 	},
85d5c65159SKalle Valo 
86d5c65159SKalle Valo 	/* CE9: host->target WMI (mac2) */
87d5c65159SKalle Valo 	{
88d5c65159SKalle Valo 		.flags = CE_ATTR_FLAGS,
89d5c65159SKalle Valo 		.src_nentries = 32,
90d5c65159SKalle Valo 		.src_sz_max = 2048,
91d5c65159SKalle Valo 		.dest_nentries = 0,
92d5c65159SKalle Valo 	},
93d5c65159SKalle Valo 
94d5c65159SKalle Valo 	/* CE10: target->host HTT */
95d5c65159SKalle Valo 	{
96d5c65159SKalle Valo 		.flags = CE_ATTR_FLAGS,
97d5c65159SKalle Valo 		.src_nentries = 0,
98d5c65159SKalle Valo 		.src_sz_max = 2048,
99d5c65159SKalle Valo 		.dest_nentries = 512,
100d5c65159SKalle Valo 		.recv_cb = ath11k_htc_rx_completion_handler,
101d5c65159SKalle Valo 	},
102d5c65159SKalle Valo 
103d5c65159SKalle Valo 	/* CE11: Not used */
104d5c65159SKalle Valo 	{
105d5c65159SKalle Valo 		.flags = CE_ATTR_FLAGS,
106d5c65159SKalle Valo 		.src_nentries = 0,
107d5c65159SKalle Valo 		.src_sz_max = 0,
108d5c65159SKalle Valo 		.dest_nentries = 0,
109d5c65159SKalle Valo 	},
110d5c65159SKalle Valo };
111d5c65159SKalle Valo 
112e3396b8bSCarl Huang const struct ce_attr ath11k_host_ce_config_qca6390[] = {
113e3396b8bSCarl Huang 	/* CE0: host->target HTC control and raw streams */
114e3396b8bSCarl Huang 	{
115e3396b8bSCarl Huang 		.flags = CE_ATTR_FLAGS,
116e3396b8bSCarl Huang 		.src_nentries = 16,
117e3396b8bSCarl Huang 		.src_sz_max = 2048,
118e3396b8bSCarl Huang 		.dest_nentries = 0,
119e3396b8bSCarl Huang 	},
120e3396b8bSCarl Huang 
121e3396b8bSCarl Huang 	/* CE1: target->host HTT + HTC control */
122e3396b8bSCarl Huang 	{
123e3396b8bSCarl Huang 		.flags = CE_ATTR_FLAGS,
124e3396b8bSCarl Huang 		.src_nentries = 0,
125e3396b8bSCarl Huang 		.src_sz_max = 2048,
126e3396b8bSCarl Huang 		.dest_nentries = 512,
127e3396b8bSCarl Huang 		.recv_cb = ath11k_htc_rx_completion_handler,
128e3396b8bSCarl Huang 	},
129e3396b8bSCarl Huang 
130e3396b8bSCarl Huang 	/* CE2: target->host WMI */
131e3396b8bSCarl Huang 	{
132e3396b8bSCarl Huang 		.flags = CE_ATTR_FLAGS,
133e3396b8bSCarl Huang 		.src_nentries = 0,
134e3396b8bSCarl Huang 		.src_sz_max = 2048,
135e3396b8bSCarl Huang 		.dest_nentries = 512,
136e3396b8bSCarl Huang 		.recv_cb = ath11k_htc_rx_completion_handler,
137e3396b8bSCarl Huang 	},
138e3396b8bSCarl Huang 
139e3396b8bSCarl Huang 	/* CE3: host->target WMI (mac0) */
140e3396b8bSCarl Huang 	{
141e3396b8bSCarl Huang 		.flags = CE_ATTR_FLAGS,
142e3396b8bSCarl Huang 		.src_nentries = 32,
143e3396b8bSCarl Huang 		.src_sz_max = 2048,
144e3396b8bSCarl Huang 		.dest_nentries = 0,
145e3396b8bSCarl Huang 	},
146e3396b8bSCarl Huang 
147e3396b8bSCarl Huang 	/* CE4: host->target HTT */
148e3396b8bSCarl Huang 	{
149e3396b8bSCarl Huang 		.flags = CE_ATTR_FLAGS | CE_ATTR_DIS_INTR,
150e3396b8bSCarl Huang 		.src_nentries = 2048,
151e3396b8bSCarl Huang 		.src_sz_max = 256,
152e3396b8bSCarl Huang 		.dest_nentries = 0,
153e3396b8bSCarl Huang 	},
154e3396b8bSCarl Huang 
155e3396b8bSCarl Huang 	/* CE5: target->host pktlog */
156e3396b8bSCarl Huang 	{
157e3396b8bSCarl Huang 		.flags = CE_ATTR_FLAGS,
158e3396b8bSCarl Huang 		.src_nentries = 0,
159e3396b8bSCarl Huang 		.src_sz_max = 2048,
160e3396b8bSCarl Huang 		.dest_nentries = 512,
161e3396b8bSCarl Huang 		.recv_cb = ath11k_dp_htt_htc_t2h_msg_handler,
162e3396b8bSCarl Huang 	},
163e3396b8bSCarl Huang 
164e3396b8bSCarl Huang 	/* CE6: target autonomous hif_memcpy */
165e3396b8bSCarl Huang 	{
166e3396b8bSCarl Huang 		.flags = CE_ATTR_FLAGS | CE_ATTR_DIS_INTR,
167e3396b8bSCarl Huang 		.src_nentries = 0,
168e3396b8bSCarl Huang 		.src_sz_max = 0,
169e3396b8bSCarl Huang 		.dest_nentries = 0,
170e3396b8bSCarl Huang 	},
171e3396b8bSCarl Huang 
172e3396b8bSCarl Huang 	/* CE7: host->target WMI (mac1) */
173e3396b8bSCarl Huang 	{
174e3396b8bSCarl Huang 		.flags = CE_ATTR_FLAGS,
175e3396b8bSCarl Huang 		.src_nentries = 32,
176e3396b8bSCarl Huang 		.src_sz_max = 2048,
177e3396b8bSCarl Huang 		.dest_nentries = 0,
178e3396b8bSCarl Huang 	},
179e3396b8bSCarl Huang 
180e3396b8bSCarl Huang 	/* CE8: target autonomous hif_memcpy */
181e3396b8bSCarl Huang 	{
182e3396b8bSCarl Huang 		.flags = CE_ATTR_FLAGS,
183e3396b8bSCarl Huang 		.src_nentries = 0,
184e3396b8bSCarl Huang 		.src_sz_max = 0,
185e3396b8bSCarl Huang 		.dest_nentries = 0,
186e3396b8bSCarl Huang 	},
187e3396b8bSCarl Huang 
188e3396b8bSCarl Huang };
189e3396b8bSCarl Huang 
190d5c65159SKalle Valo static int ath11k_ce_rx_buf_enqueue_pipe(struct ath11k_ce_pipe *pipe,
191d5c65159SKalle Valo 					 struct sk_buff *skb, dma_addr_t paddr)
192d5c65159SKalle Valo {
193d5c65159SKalle Valo 	struct ath11k_base *ab = pipe->ab;
194d5c65159SKalle Valo 	struct ath11k_ce_ring *ring = pipe->dest_ring;
195d5c65159SKalle Valo 	struct hal_srng *srng;
196d5c65159SKalle Valo 	unsigned int write_index;
197d5c65159SKalle Valo 	unsigned int nentries_mask = ring->nentries_mask;
198d5c65159SKalle Valo 	u32 *desc;
199d5c65159SKalle Valo 	int ret;
200d5c65159SKalle Valo 
201d5c65159SKalle Valo 	lockdep_assert_held(&ab->ce.ce_lock);
202d5c65159SKalle Valo 
203d5c65159SKalle Valo 	write_index = ring->write_index;
204d5c65159SKalle Valo 
205d5c65159SKalle Valo 	srng = &ab->hal.srng_list[ring->hal_ring_id];
206d5c65159SKalle Valo 
207d5c65159SKalle Valo 	spin_lock_bh(&srng->lock);
208d5c65159SKalle Valo 
209d5c65159SKalle Valo 	ath11k_hal_srng_access_begin(ab, srng);
210d5c65159SKalle Valo 
211d5c65159SKalle Valo 	if (unlikely(ath11k_hal_srng_src_num_free(ab, srng, false) < 1)) {
212d5c65159SKalle Valo 		ret = -ENOSPC;
213d5c65159SKalle Valo 		goto exit;
214d5c65159SKalle Valo 	}
215d5c65159SKalle Valo 
216d5c65159SKalle Valo 	desc = ath11k_hal_srng_src_get_next_entry(ab, srng);
217d5c65159SKalle Valo 	if (!desc) {
218d5c65159SKalle Valo 		ret = -ENOSPC;
219d5c65159SKalle Valo 		goto exit;
220d5c65159SKalle Valo 	}
221d5c65159SKalle Valo 
222d5c65159SKalle Valo 	ath11k_hal_ce_dst_set_desc(desc, paddr);
223d5c65159SKalle Valo 
224d5c65159SKalle Valo 	ring->skb[write_index] = skb;
225d5c65159SKalle Valo 	write_index = CE_RING_IDX_INCR(nentries_mask, write_index);
226d5c65159SKalle Valo 	ring->write_index = write_index;
227d5c65159SKalle Valo 
228d5c65159SKalle Valo 	pipe->rx_buf_needed--;
229d5c65159SKalle Valo 
230d5c65159SKalle Valo 	ret = 0;
231d5c65159SKalle Valo exit:
232d5c65159SKalle Valo 	ath11k_hal_srng_access_end(ab, srng);
233d5c65159SKalle Valo 
234d5c65159SKalle Valo 	spin_unlock_bh(&srng->lock);
235d5c65159SKalle Valo 
236d5c65159SKalle Valo 	return ret;
237d5c65159SKalle Valo }
238d5c65159SKalle Valo 
239d5c65159SKalle Valo static int ath11k_ce_rx_post_pipe(struct ath11k_ce_pipe *pipe)
240d5c65159SKalle Valo {
241d5c65159SKalle Valo 	struct ath11k_base *ab = pipe->ab;
242d5c65159SKalle Valo 	struct sk_buff *skb;
243d5c65159SKalle Valo 	dma_addr_t paddr;
244d5c65159SKalle Valo 	int ret = 0;
245d5c65159SKalle Valo 
246d5c65159SKalle Valo 	if (!(pipe->dest_ring || pipe->status_ring))
247d5c65159SKalle Valo 		return 0;
248d5c65159SKalle Valo 
249d5c65159SKalle Valo 	spin_lock_bh(&ab->ce.ce_lock);
250d5c65159SKalle Valo 	while (pipe->rx_buf_needed) {
251d5c65159SKalle Valo 		skb = dev_alloc_skb(pipe->buf_sz);
252d5c65159SKalle Valo 		if (!skb) {
253d5c65159SKalle Valo 			ret = -ENOMEM;
254d5c65159SKalle Valo 			goto exit;
255d5c65159SKalle Valo 		}
256d5c65159SKalle Valo 
257d5c65159SKalle Valo 		WARN_ON_ONCE(!IS_ALIGNED((unsigned long)skb->data, 4));
258d5c65159SKalle Valo 
259d5c65159SKalle Valo 		paddr = dma_map_single(ab->dev, skb->data,
260d5c65159SKalle Valo 				       skb->len + skb_tailroom(skb),
261d5c65159SKalle Valo 				       DMA_FROM_DEVICE);
262d5c65159SKalle Valo 		if (unlikely(dma_mapping_error(ab->dev, paddr))) {
263d5c65159SKalle Valo 			ath11k_warn(ab, "failed to dma map ce rx buf\n");
264d5c65159SKalle Valo 			dev_kfree_skb_any(skb);
265d5c65159SKalle Valo 			ret = -EIO;
266d5c65159SKalle Valo 			goto exit;
267d5c65159SKalle Valo 		}
268d5c65159SKalle Valo 
269d5c65159SKalle Valo 		ATH11K_SKB_RXCB(skb)->paddr = paddr;
270d5c65159SKalle Valo 
271d5c65159SKalle Valo 		ret = ath11k_ce_rx_buf_enqueue_pipe(pipe, skb, paddr);
272d5c65159SKalle Valo 
273d5c65159SKalle Valo 		if (ret) {
274d5c65159SKalle Valo 			ath11k_warn(ab, "failed to enqueue rx buf: %d\n", ret);
275d5c65159SKalle Valo 			dma_unmap_single(ab->dev, paddr,
276d5c65159SKalle Valo 					 skb->len + skb_tailroom(skb),
277d5c65159SKalle Valo 					 DMA_FROM_DEVICE);
278d5c65159SKalle Valo 			dev_kfree_skb_any(skb);
279d5c65159SKalle Valo 			goto exit;
280d5c65159SKalle Valo 		}
281d5c65159SKalle Valo 	}
282d5c65159SKalle Valo 
283d5c65159SKalle Valo exit:
284d5c65159SKalle Valo 	spin_unlock_bh(&ab->ce.ce_lock);
285d5c65159SKalle Valo 	return ret;
286d5c65159SKalle Valo }
287d5c65159SKalle Valo 
288d5c65159SKalle Valo static int ath11k_ce_completed_recv_next(struct ath11k_ce_pipe *pipe,
289d5c65159SKalle Valo 					 struct sk_buff **skb, int *nbytes)
290d5c65159SKalle Valo {
291d5c65159SKalle Valo 	struct ath11k_base *ab = pipe->ab;
292d5c65159SKalle Valo 	struct hal_srng *srng;
293d5c65159SKalle Valo 	unsigned int sw_index;
294d5c65159SKalle Valo 	unsigned int nentries_mask;
295d5c65159SKalle Valo 	u32 *desc;
296d5c65159SKalle Valo 	int ret = 0;
297d5c65159SKalle Valo 
298d5c65159SKalle Valo 	spin_lock_bh(&ab->ce.ce_lock);
299d5c65159SKalle Valo 
300d5c65159SKalle Valo 	sw_index = pipe->dest_ring->sw_index;
301d5c65159SKalle Valo 	nentries_mask = pipe->dest_ring->nentries_mask;
302d5c65159SKalle Valo 
303d5c65159SKalle Valo 	srng = &ab->hal.srng_list[pipe->status_ring->hal_ring_id];
304d5c65159SKalle Valo 
305d5c65159SKalle Valo 	spin_lock_bh(&srng->lock);
306d5c65159SKalle Valo 
307d5c65159SKalle Valo 	ath11k_hal_srng_access_begin(ab, srng);
308d5c65159SKalle Valo 
309d5c65159SKalle Valo 	desc = ath11k_hal_srng_dst_get_next_entry(ab, srng);
310d5c65159SKalle Valo 	if (!desc) {
311d5c65159SKalle Valo 		ret = -EIO;
312d5c65159SKalle Valo 		goto err;
313d5c65159SKalle Valo 	}
314d5c65159SKalle Valo 
315d5c65159SKalle Valo 	*nbytes = ath11k_hal_ce_dst_status_get_length(desc);
316d5c65159SKalle Valo 	if (*nbytes == 0) {
317d5c65159SKalle Valo 		ret = -EIO;
318d5c65159SKalle Valo 		goto err;
319d5c65159SKalle Valo 	}
320d5c65159SKalle Valo 
321d5c65159SKalle Valo 	*skb = pipe->dest_ring->skb[sw_index];
322d5c65159SKalle Valo 	pipe->dest_ring->skb[sw_index] = NULL;
323d5c65159SKalle Valo 
324d5c65159SKalle Valo 	sw_index = CE_RING_IDX_INCR(nentries_mask, sw_index);
325d5c65159SKalle Valo 	pipe->dest_ring->sw_index = sw_index;
326d5c65159SKalle Valo 
327d5c65159SKalle Valo 	pipe->rx_buf_needed++;
328d5c65159SKalle Valo err:
329d5c65159SKalle Valo 	ath11k_hal_srng_access_end(ab, srng);
330d5c65159SKalle Valo 
331d5c65159SKalle Valo 	spin_unlock_bh(&srng->lock);
332d5c65159SKalle Valo 
333d5c65159SKalle Valo 	spin_unlock_bh(&ab->ce.ce_lock);
334d5c65159SKalle Valo 
335d5c65159SKalle Valo 	return ret;
336d5c65159SKalle Valo }
337d5c65159SKalle Valo 
338d5c65159SKalle Valo static void ath11k_ce_recv_process_cb(struct ath11k_ce_pipe *pipe)
339d5c65159SKalle Valo {
340d5c65159SKalle Valo 	struct ath11k_base *ab = pipe->ab;
341d5c65159SKalle Valo 	struct sk_buff *skb;
342d5c65159SKalle Valo 	struct sk_buff_head list;
343d5c65159SKalle Valo 	unsigned int nbytes, max_nbytes;
344d5c65159SKalle Valo 	int ret;
345d5c65159SKalle Valo 
346d5c65159SKalle Valo 	__skb_queue_head_init(&list);
347d5c65159SKalle Valo 	while (ath11k_ce_completed_recv_next(pipe, &skb, &nbytes) == 0) {
348d5c65159SKalle Valo 		max_nbytes = skb->len + skb_tailroom(skb);
349d5c65159SKalle Valo 		dma_unmap_single(ab->dev, ATH11K_SKB_RXCB(skb)->paddr,
350d5c65159SKalle Valo 				 max_nbytes, DMA_FROM_DEVICE);
351d5c65159SKalle Valo 
352d5c65159SKalle Valo 		if (unlikely(max_nbytes < nbytes)) {
353d5c65159SKalle Valo 			ath11k_warn(ab, "rxed more than expected (nbytes %d, max %d)",
354d5c65159SKalle Valo 				    nbytes, max_nbytes);
355d5c65159SKalle Valo 			dev_kfree_skb_any(skb);
356d5c65159SKalle Valo 			continue;
357d5c65159SKalle Valo 		}
358d5c65159SKalle Valo 
359d5c65159SKalle Valo 		skb_put(skb, nbytes);
360d5c65159SKalle Valo 		__skb_queue_tail(&list, skb);
361d5c65159SKalle Valo 	}
362d5c65159SKalle Valo 
363d5c65159SKalle Valo 	while ((skb = __skb_dequeue(&list))) {
364d5c65159SKalle Valo 		ath11k_dbg(ab, ATH11K_DBG_AHB, "rx ce pipe %d len %d\n",
365d5c65159SKalle Valo 			   pipe->pipe_num, skb->len);
366d5c65159SKalle Valo 		pipe->recv_cb(ab, skb);
367d5c65159SKalle Valo 	}
368d5c65159SKalle Valo 
369d5c65159SKalle Valo 	ret = ath11k_ce_rx_post_pipe(pipe);
370d5c65159SKalle Valo 	if (ret && ret != -ENOSPC) {
371d5c65159SKalle Valo 		ath11k_warn(ab, "failed to post rx buf to pipe: %d err: %d\n",
372d5c65159SKalle Valo 			    pipe->pipe_num, ret);
373d5c65159SKalle Valo 		mod_timer(&ab->rx_replenish_retry,
374d5c65159SKalle Valo 			  jiffies + ATH11K_CE_RX_POST_RETRY_JIFFIES);
375d5c65159SKalle Valo 	}
376d5c65159SKalle Valo }
377d5c65159SKalle Valo 
378d5c65159SKalle Valo static struct sk_buff *ath11k_ce_completed_send_next(struct ath11k_ce_pipe *pipe)
379d5c65159SKalle Valo {
380d5c65159SKalle Valo 	struct ath11k_base *ab = pipe->ab;
381d5c65159SKalle Valo 	struct hal_srng *srng;
382d5c65159SKalle Valo 	unsigned int sw_index;
383d5c65159SKalle Valo 	unsigned int nentries_mask;
384d5c65159SKalle Valo 	struct sk_buff *skb;
385d5c65159SKalle Valo 	u32 *desc;
386d5c65159SKalle Valo 
387d5c65159SKalle Valo 	spin_lock_bh(&ab->ce.ce_lock);
388d5c65159SKalle Valo 
389d5c65159SKalle Valo 	sw_index = pipe->src_ring->sw_index;
390d5c65159SKalle Valo 	nentries_mask = pipe->src_ring->nentries_mask;
391d5c65159SKalle Valo 
392d5c65159SKalle Valo 	srng = &ab->hal.srng_list[pipe->src_ring->hal_ring_id];
393d5c65159SKalle Valo 
394d5c65159SKalle Valo 	spin_lock_bh(&srng->lock);
395d5c65159SKalle Valo 
396d5c65159SKalle Valo 	ath11k_hal_srng_access_begin(ab, srng);
397d5c65159SKalle Valo 
398d5c65159SKalle Valo 	desc = ath11k_hal_srng_src_reap_next(ab, srng);
399d5c65159SKalle Valo 	if (!desc) {
400d5c65159SKalle Valo 		skb = ERR_PTR(-EIO);
401d5c65159SKalle Valo 		goto err_unlock;
402d5c65159SKalle Valo 	}
403d5c65159SKalle Valo 
404d5c65159SKalle Valo 	skb = pipe->src_ring->skb[sw_index];
405d5c65159SKalle Valo 
406d5c65159SKalle Valo 	pipe->src_ring->skb[sw_index] = NULL;
407d5c65159SKalle Valo 
408d5c65159SKalle Valo 	sw_index = CE_RING_IDX_INCR(nentries_mask, sw_index);
409d5c65159SKalle Valo 	pipe->src_ring->sw_index = sw_index;
410d5c65159SKalle Valo 
411d5c65159SKalle Valo err_unlock:
412d5c65159SKalle Valo 	spin_unlock_bh(&srng->lock);
413d5c65159SKalle Valo 
414d5c65159SKalle Valo 	spin_unlock_bh(&ab->ce.ce_lock);
415d5c65159SKalle Valo 
416d5c65159SKalle Valo 	return skb;
417d5c65159SKalle Valo }
418d5c65159SKalle Valo 
419d5c65159SKalle Valo static void ath11k_ce_send_done_cb(struct ath11k_ce_pipe *pipe)
420d5c65159SKalle Valo {
421d5c65159SKalle Valo 	struct ath11k_base *ab = pipe->ab;
422d5c65159SKalle Valo 	struct sk_buff *skb;
423d5c65159SKalle Valo 
424d5c65159SKalle Valo 	while (!IS_ERR(skb = ath11k_ce_completed_send_next(pipe))) {
425d5c65159SKalle Valo 		if (!skb)
426d5c65159SKalle Valo 			continue;
427d5c65159SKalle Valo 
428d5c65159SKalle Valo 		dma_unmap_single(ab->dev, ATH11K_SKB_CB(skb)->paddr, skb->len,
429d5c65159SKalle Valo 				 DMA_TO_DEVICE);
430d5c65159SKalle Valo 		dev_kfree_skb_any(skb);
431d5c65159SKalle Valo 	}
432d5c65159SKalle Valo }
433d5c65159SKalle Valo 
434c4eacabeSGovind Singh static void ath11k_ce_srng_msi_ring_params_setup(struct ath11k_base *ab, u32 ce_id,
435c4eacabeSGovind Singh 						 struct hal_srng_params *ring_params)
436c4eacabeSGovind Singh {
437c4eacabeSGovind Singh 	u32 msi_data_start;
438c4eacabeSGovind Singh 	u32 msi_data_count;
439c4eacabeSGovind Singh 	u32 msi_irq_start;
440c4eacabeSGovind Singh 	u32 addr_lo;
441c4eacabeSGovind Singh 	u32 addr_hi;
442c4eacabeSGovind Singh 	int ret;
443c4eacabeSGovind Singh 
444c4eacabeSGovind Singh 	ret = ath11k_get_user_msi_vector(ab, "CE",
445c4eacabeSGovind Singh 					 &msi_data_count, &msi_data_start,
446c4eacabeSGovind Singh 					 &msi_irq_start);
447c4eacabeSGovind Singh 
448c4eacabeSGovind Singh 	if (ret)
449c4eacabeSGovind Singh 		return;
450c4eacabeSGovind Singh 
451c4eacabeSGovind Singh 	ath11k_get_msi_address(ab, &addr_lo, &addr_hi);
452c4eacabeSGovind Singh 
453c4eacabeSGovind Singh 	ring_params->msi_addr = addr_lo;
454c4eacabeSGovind Singh 	ring_params->msi_addr |= (dma_addr_t)(((uint64_t)addr_hi) << 32);
455c4eacabeSGovind Singh 	ring_params->msi_data = (ce_id % msi_data_count) + msi_data_start;
456c4eacabeSGovind Singh 	ring_params->flags |= HAL_SRNG_FLAGS_MSI_INTR;
457c4eacabeSGovind Singh }
458c4eacabeSGovind Singh 
459d5c65159SKalle Valo static int ath11k_ce_init_ring(struct ath11k_base *ab,
460d5c65159SKalle Valo 			       struct ath11k_ce_ring *ce_ring,
461d5c65159SKalle Valo 			       int ce_id, enum hal_ring_type type)
462d5c65159SKalle Valo {
463d5c65159SKalle Valo 	struct hal_srng_params params = { 0 };
464d5c65159SKalle Valo 	int ret;
465d5c65159SKalle Valo 
466d5c65159SKalle Valo 	params.ring_base_paddr = ce_ring->base_addr_ce_space;
467d5c65159SKalle Valo 	params.ring_base_vaddr = ce_ring->base_addr_owner_space;
468d5c65159SKalle Valo 	params.num_entries = ce_ring->nentries;
469d5c65159SKalle Valo 
4701a05ed37SCarl Huang 	if (!(CE_ATTR_DIS_INTR & ab->hw_params.host_ce_config[ce_id].flags))
4711a05ed37SCarl Huang 		ath11k_ce_srng_msi_ring_params_setup(ab, ce_id, &params);
4721a05ed37SCarl Huang 
473d5c65159SKalle Valo 	switch (type) {
474d5c65159SKalle Valo 	case HAL_CE_SRC:
4756e5e9f59SKalle Valo 		if (!(CE_ATTR_DIS_INTR & ab->hw_params.host_ce_config[ce_id].flags))
476d5c65159SKalle Valo 			params.intr_batch_cntr_thres_entries = 1;
477d5c65159SKalle Valo 		break;
478d5c65159SKalle Valo 	case HAL_CE_DST:
4796e5e9f59SKalle Valo 		params.max_buffer_len = ab->hw_params.host_ce_config[ce_id].src_sz_max;
4806e5e9f59SKalle Valo 		if (!(ab->hw_params.host_ce_config[ce_id].flags & CE_ATTR_DIS_INTR)) {
481d5c65159SKalle Valo 			params.intr_timer_thres_us = 1024;
482d5c65159SKalle Valo 			params.flags |= HAL_SRNG_FLAGS_LOW_THRESH_INTR_EN;
483d5c65159SKalle Valo 			params.low_threshold = ce_ring->nentries - 3;
484d5c65159SKalle Valo 		}
485d5c65159SKalle Valo 		break;
486d5c65159SKalle Valo 	case HAL_CE_DST_STATUS:
4876e5e9f59SKalle Valo 		if (!(ab->hw_params.host_ce_config[ce_id].flags & CE_ATTR_DIS_INTR)) {
488d5c65159SKalle Valo 			params.intr_batch_cntr_thres_entries = 1;
489d5c65159SKalle Valo 			params.intr_timer_thres_us = 0x1000;
490d5c65159SKalle Valo 		}
491d5c65159SKalle Valo 		break;
492d5c65159SKalle Valo 	default:
493d5c65159SKalle Valo 		ath11k_warn(ab, "Invalid CE ring type %d\n", type);
494d5c65159SKalle Valo 		return -EINVAL;
495d5c65159SKalle Valo 	}
496d5c65159SKalle Valo 
497d5c65159SKalle Valo 	/* TODO: Init other params needed by HAL to init the ring */
498d5c65159SKalle Valo 
499d5c65159SKalle Valo 	ret = ath11k_hal_srng_setup(ab, type, ce_id, 0, &params);
500d5c65159SKalle Valo 	if (ret < 0) {
501d5c65159SKalle Valo 		ath11k_warn(ab, "failed to setup srng: %d ring_id %d\n",
502d5c65159SKalle Valo 			    ret, ce_id);
503d5c65159SKalle Valo 		return ret;
504d5c65159SKalle Valo 	}
505c4eacabeSGovind Singh 
506d5c65159SKalle Valo 	ce_ring->hal_ring_id = ret;
507d5c65159SKalle Valo 
508d5c65159SKalle Valo 	return 0;
509d5c65159SKalle Valo }
510d5c65159SKalle Valo 
511d5c65159SKalle Valo static struct ath11k_ce_ring *
512d5c65159SKalle Valo ath11k_ce_alloc_ring(struct ath11k_base *ab, int nentries, int desc_sz)
513d5c65159SKalle Valo {
514d5c65159SKalle Valo 	struct ath11k_ce_ring *ce_ring;
515d5c65159SKalle Valo 	dma_addr_t base_addr;
516d5c65159SKalle Valo 
517d5c65159SKalle Valo 	ce_ring = kzalloc(struct_size(ce_ring, skb, nentries), GFP_KERNEL);
518d5c65159SKalle Valo 	if (ce_ring == NULL)
519d5c65159SKalle Valo 		return ERR_PTR(-ENOMEM);
520d5c65159SKalle Valo 
521d5c65159SKalle Valo 	ce_ring->nentries = nentries;
522d5c65159SKalle Valo 	ce_ring->nentries_mask = nentries - 1;
523d5c65159SKalle Valo 
524d5c65159SKalle Valo 	/* Legacy platforms that do not support cache
525d5c65159SKalle Valo 	 * coherent DMA are unsupported
526d5c65159SKalle Valo 	 */
527d5c65159SKalle Valo 	ce_ring->base_addr_owner_space_unaligned =
528d5c65159SKalle Valo 		dma_alloc_coherent(ab->dev,
529d5c65159SKalle Valo 				   nentries * desc_sz + CE_DESC_RING_ALIGN,
530d5c65159SKalle Valo 				   &base_addr, GFP_KERNEL);
531d5c65159SKalle Valo 	if (!ce_ring->base_addr_owner_space_unaligned) {
532d5c65159SKalle Valo 		kfree(ce_ring);
533d5c65159SKalle Valo 		return ERR_PTR(-ENOMEM);
534d5c65159SKalle Valo 	}
535d5c65159SKalle Valo 
536d5c65159SKalle Valo 	ce_ring->base_addr_ce_space_unaligned = base_addr;
537d5c65159SKalle Valo 
538d5c65159SKalle Valo 	ce_ring->base_addr_owner_space = PTR_ALIGN(
539d5c65159SKalle Valo 			ce_ring->base_addr_owner_space_unaligned,
540d5c65159SKalle Valo 			CE_DESC_RING_ALIGN);
541d5c65159SKalle Valo 	ce_ring->base_addr_ce_space = ALIGN(
542d5c65159SKalle Valo 			ce_ring->base_addr_ce_space_unaligned,
543d5c65159SKalle Valo 			CE_DESC_RING_ALIGN);
544d5c65159SKalle Valo 
545d5c65159SKalle Valo 	return ce_ring;
546d5c65159SKalle Valo }
547d5c65159SKalle Valo 
548d5c65159SKalle Valo static int ath11k_ce_alloc_pipe(struct ath11k_base *ab, int ce_id)
549d5c65159SKalle Valo {
550d5c65159SKalle Valo 	struct ath11k_ce_pipe *pipe = &ab->ce.ce_pipe[ce_id];
5516e5e9f59SKalle Valo 	const struct ce_attr *attr = &ab->hw_params.host_ce_config[ce_id];
552c76fa846SDan Carpenter 	struct ath11k_ce_ring *ring;
553d5c65159SKalle Valo 	int nentries;
554d5c65159SKalle Valo 	int desc_sz;
555d5c65159SKalle Valo 
556d5c65159SKalle Valo 	pipe->attr_flags = attr->flags;
557d5c65159SKalle Valo 
558d5c65159SKalle Valo 	if (attr->src_nentries) {
559d5c65159SKalle Valo 		pipe->send_cb = ath11k_ce_send_done_cb;
560d5c65159SKalle Valo 		nentries = roundup_pow_of_two(attr->src_nentries);
561d5c65159SKalle Valo 		desc_sz = ath11k_hal_ce_get_desc_size(HAL_CE_DESC_SRC);
562c76fa846SDan Carpenter 		ring = ath11k_ce_alloc_ring(ab, nentries, desc_sz);
563c76fa846SDan Carpenter 		if (IS_ERR(ring))
564c76fa846SDan Carpenter 			return PTR_ERR(ring);
565c76fa846SDan Carpenter 		pipe->src_ring = ring;
566d5c65159SKalle Valo 	}
567d5c65159SKalle Valo 
568d5c65159SKalle Valo 	if (attr->dest_nentries) {
569d5c65159SKalle Valo 		pipe->recv_cb = attr->recv_cb;
570d5c65159SKalle Valo 		nentries = roundup_pow_of_two(attr->dest_nentries);
571d5c65159SKalle Valo 		desc_sz = ath11k_hal_ce_get_desc_size(HAL_CE_DESC_DST);
572c76fa846SDan Carpenter 		ring = ath11k_ce_alloc_ring(ab, nentries, desc_sz);
573c76fa846SDan Carpenter 		if (IS_ERR(ring))
574c76fa846SDan Carpenter 			return PTR_ERR(ring);
575c76fa846SDan Carpenter 		pipe->dest_ring = ring;
576d5c65159SKalle Valo 
577d5c65159SKalle Valo 		desc_sz = ath11k_hal_ce_get_desc_size(HAL_CE_DESC_DST_STATUS);
578c76fa846SDan Carpenter 		ring = ath11k_ce_alloc_ring(ab, nentries, desc_sz);
579c76fa846SDan Carpenter 		if (IS_ERR(ring))
580c76fa846SDan Carpenter 			return PTR_ERR(ring);
581c76fa846SDan Carpenter 		pipe->status_ring = ring;
582d5c65159SKalle Valo 	}
583d5c65159SKalle Valo 
584d5c65159SKalle Valo 	return 0;
585d5c65159SKalle Valo }
586d5c65159SKalle Valo 
587d5c65159SKalle Valo void ath11k_ce_per_engine_service(struct ath11k_base *ab, u16 ce_id)
588d5c65159SKalle Valo {
589d5c65159SKalle Valo 	struct ath11k_ce_pipe *pipe = &ab->ce.ce_pipe[ce_id];
590d5c65159SKalle Valo 
591d5c65159SKalle Valo 	if (pipe->send_cb)
592d5c65159SKalle Valo 		pipe->send_cb(pipe);
593d5c65159SKalle Valo 
594d5c65159SKalle Valo 	if (pipe->recv_cb)
595d5c65159SKalle Valo 		ath11k_ce_recv_process_cb(pipe);
596d5c65159SKalle Valo }
597d5c65159SKalle Valo 
598d5c65159SKalle Valo void ath11k_ce_poll_send_completed(struct ath11k_base *ab, u8 pipe_id)
599d5c65159SKalle Valo {
600d5c65159SKalle Valo 	struct ath11k_ce_pipe *pipe = &ab->ce.ce_pipe[pipe_id];
601d5c65159SKalle Valo 
602d5c65159SKalle Valo 	if ((pipe->attr_flags & CE_ATTR_DIS_INTR) && pipe->send_cb)
603d5c65159SKalle Valo 		pipe->send_cb(pipe);
604d5c65159SKalle Valo }
6052c3960c2SGovind Singh EXPORT_SYMBOL(ath11k_ce_per_engine_service);
606d5c65159SKalle Valo 
607d5c65159SKalle Valo int ath11k_ce_send(struct ath11k_base *ab, struct sk_buff *skb, u8 pipe_id,
608d5c65159SKalle Valo 		   u16 transfer_id)
609d5c65159SKalle Valo {
610d5c65159SKalle Valo 	struct ath11k_ce_pipe *pipe = &ab->ce.ce_pipe[pipe_id];
611d5c65159SKalle Valo 	struct hal_srng *srng;
612d5c65159SKalle Valo 	u32 *desc;
613d5c65159SKalle Valo 	unsigned int write_index, sw_index;
614d5c65159SKalle Valo 	unsigned int nentries_mask;
615d5c65159SKalle Valo 	int ret = 0;
616d5c65159SKalle Valo 	u8 byte_swap_data = 0;
617d5c65159SKalle Valo 	int num_used;
618d5c65159SKalle Valo 
619d5c65159SKalle Valo 	/* Check if some entries could be regained by handling tx completion if
620d5c65159SKalle Valo 	 * the CE has interrupts disabled and the used entries is more than the
621d5c65159SKalle Valo 	 * defined usage threshold.
622d5c65159SKalle Valo 	 */
623d5c65159SKalle Valo 	if (pipe->attr_flags & CE_ATTR_DIS_INTR) {
624d5c65159SKalle Valo 		spin_lock_bh(&ab->ce.ce_lock);
625d5c65159SKalle Valo 		write_index = pipe->src_ring->write_index;
626d5c65159SKalle Valo 
627d5c65159SKalle Valo 		sw_index = pipe->src_ring->sw_index;
628d5c65159SKalle Valo 
629d5c65159SKalle Valo 		if (write_index >= sw_index)
630d5c65159SKalle Valo 			num_used = write_index - sw_index;
631d5c65159SKalle Valo 		else
632d5c65159SKalle Valo 			num_used = pipe->src_ring->nentries - sw_index +
633d5c65159SKalle Valo 				   write_index;
634d5c65159SKalle Valo 
635d5c65159SKalle Valo 		spin_unlock_bh(&ab->ce.ce_lock);
636d5c65159SKalle Valo 
637d5c65159SKalle Valo 		if (num_used > ATH11K_CE_USAGE_THRESHOLD)
638d5c65159SKalle Valo 			ath11k_ce_poll_send_completed(ab, pipe->pipe_num);
639d5c65159SKalle Valo 	}
640d5c65159SKalle Valo 
641d5c65159SKalle Valo 	if (test_bit(ATH11K_FLAG_CRASH_FLUSH, &ab->dev_flags))
642d5c65159SKalle Valo 		return -ESHUTDOWN;
643d5c65159SKalle Valo 
644d5c65159SKalle Valo 	spin_lock_bh(&ab->ce.ce_lock);
645d5c65159SKalle Valo 
646d5c65159SKalle Valo 	write_index = pipe->src_ring->write_index;
647d5c65159SKalle Valo 	nentries_mask = pipe->src_ring->nentries_mask;
648d5c65159SKalle Valo 
649d5c65159SKalle Valo 	srng = &ab->hal.srng_list[pipe->src_ring->hal_ring_id];
650d5c65159SKalle Valo 
651d5c65159SKalle Valo 	spin_lock_bh(&srng->lock);
652d5c65159SKalle Valo 
653d5c65159SKalle Valo 	ath11k_hal_srng_access_begin(ab, srng);
654d5c65159SKalle Valo 
655d5c65159SKalle Valo 	if (unlikely(ath11k_hal_srng_src_num_free(ab, srng, false) < 1)) {
656d5c65159SKalle Valo 		ath11k_hal_srng_access_end(ab, srng);
657d5c65159SKalle Valo 		ret = -ENOBUFS;
658d5c65159SKalle Valo 		goto err_unlock;
659d5c65159SKalle Valo 	}
660d5c65159SKalle Valo 
661d5c65159SKalle Valo 	desc = ath11k_hal_srng_src_get_next_reaped(ab, srng);
662d5c65159SKalle Valo 	if (!desc) {
663d5c65159SKalle Valo 		ath11k_hal_srng_access_end(ab, srng);
664d5c65159SKalle Valo 		ret = -ENOBUFS;
665d5c65159SKalle Valo 		goto err_unlock;
666d5c65159SKalle Valo 	}
667d5c65159SKalle Valo 
668d5c65159SKalle Valo 	if (pipe->attr_flags & CE_ATTR_BYTE_SWAP_DATA)
669d5c65159SKalle Valo 		byte_swap_data = 1;
670d5c65159SKalle Valo 
671d5c65159SKalle Valo 	ath11k_hal_ce_src_set_desc(desc, ATH11K_SKB_CB(skb)->paddr,
672d5c65159SKalle Valo 				   skb->len, transfer_id, byte_swap_data);
673d5c65159SKalle Valo 
674d5c65159SKalle Valo 	pipe->src_ring->skb[write_index] = skb;
675d5c65159SKalle Valo 	pipe->src_ring->write_index = CE_RING_IDX_INCR(nentries_mask,
676d5c65159SKalle Valo 						       write_index);
677d5c65159SKalle Valo 
678d5c65159SKalle Valo 	ath11k_hal_srng_access_end(ab, srng);
679d5c65159SKalle Valo 
680d5c65159SKalle Valo 	spin_unlock_bh(&srng->lock);
681d5c65159SKalle Valo 
682d5c65159SKalle Valo 	spin_unlock_bh(&ab->ce.ce_lock);
683d5c65159SKalle Valo 
684d5c65159SKalle Valo 	return 0;
685d5c65159SKalle Valo 
686d5c65159SKalle Valo err_unlock:
687d5c65159SKalle Valo 	spin_unlock_bh(&srng->lock);
688d5c65159SKalle Valo 
689d5c65159SKalle Valo 	spin_unlock_bh(&ab->ce.ce_lock);
690d5c65159SKalle Valo 
691d5c65159SKalle Valo 	return ret;
692d5c65159SKalle Valo }
693d5c65159SKalle Valo 
694d5c65159SKalle Valo static void ath11k_ce_rx_pipe_cleanup(struct ath11k_ce_pipe *pipe)
695d5c65159SKalle Valo {
696d5c65159SKalle Valo 	struct ath11k_base *ab = pipe->ab;
697d5c65159SKalle Valo 	struct ath11k_ce_ring *ring = pipe->dest_ring;
698d5c65159SKalle Valo 	struct sk_buff *skb;
699d5c65159SKalle Valo 	int i;
700d5c65159SKalle Valo 
701d5c65159SKalle Valo 	if (!(ring && pipe->buf_sz))
702d5c65159SKalle Valo 		return;
703d5c65159SKalle Valo 
704d5c65159SKalle Valo 	for (i = 0; i < ring->nentries; i++) {
705d5c65159SKalle Valo 		skb = ring->skb[i];
706d5c65159SKalle Valo 		if (!skb)
707d5c65159SKalle Valo 			continue;
708d5c65159SKalle Valo 
709d5c65159SKalle Valo 		ring->skb[i] = NULL;
710d5c65159SKalle Valo 		dma_unmap_single(ab->dev, ATH11K_SKB_RXCB(skb)->paddr,
711d5c65159SKalle Valo 				 skb->len + skb_tailroom(skb), DMA_FROM_DEVICE);
712d5c65159SKalle Valo 		dev_kfree_skb_any(skb);
713d5c65159SKalle Valo 	}
714d5c65159SKalle Valo }
715d5c65159SKalle Valo 
716*e838c14aSCarl Huang static void ath11k_ce_shadow_config(struct ath11k_base *ab)
717*e838c14aSCarl Huang {
718*e838c14aSCarl Huang 	int i;
719*e838c14aSCarl Huang 
720*e838c14aSCarl Huang 	for (i = 0; i < ab->hw_params.ce_count; i++) {
721*e838c14aSCarl Huang 		if (ab->hw_params.host_ce_config[i].src_nentries)
722*e838c14aSCarl Huang 			ath11k_hal_srng_update_shadow_config(ab,
723*e838c14aSCarl Huang 							     HAL_CE_SRC, i);
724*e838c14aSCarl Huang 
725*e838c14aSCarl Huang 		if (ab->hw_params.host_ce_config[i].dest_nentries) {
726*e838c14aSCarl Huang 			ath11k_hal_srng_update_shadow_config(ab,
727*e838c14aSCarl Huang 							     HAL_CE_DST, i);
728*e838c14aSCarl Huang 
729*e838c14aSCarl Huang 			ath11k_hal_srng_update_shadow_config(ab,
730*e838c14aSCarl Huang 							     HAL_CE_DST_STATUS, i);
731*e838c14aSCarl Huang 		}
732*e838c14aSCarl Huang 	}
733*e838c14aSCarl Huang }
734*e838c14aSCarl Huang 
735*e838c14aSCarl Huang void ath11k_ce_get_shadow_config(struct ath11k_base *ab,
736*e838c14aSCarl Huang 				 u32 **shadow_cfg, u32 *shadow_cfg_len)
737*e838c14aSCarl Huang {
738*e838c14aSCarl Huang 	if (!ab->hw_params.supports_shadow_regs)
739*e838c14aSCarl Huang 		return;
740*e838c14aSCarl Huang 
741*e838c14aSCarl Huang 	ath11k_hal_srng_get_shadow_config(ab, shadow_cfg, shadow_cfg_len);
742*e838c14aSCarl Huang 
743*e838c14aSCarl Huang 	/* shadow is already configured */
744*e838c14aSCarl Huang 	if (*shadow_cfg_len)
745*e838c14aSCarl Huang 		return;
746*e838c14aSCarl Huang 
747*e838c14aSCarl Huang 	/* shadow isn't configured yet, configure now.
748*e838c14aSCarl Huang 	 * non-CE srngs are configured firstly, then
749*e838c14aSCarl Huang 	 * all CE srngs.
750*e838c14aSCarl Huang 	 */
751*e838c14aSCarl Huang 	ath11k_hal_srng_shadow_config(ab);
752*e838c14aSCarl Huang 	ath11k_ce_shadow_config(ab);
753*e838c14aSCarl Huang 
754*e838c14aSCarl Huang 	/* get the shadow configuration */
755*e838c14aSCarl Huang 	ath11k_hal_srng_get_shadow_config(ab, shadow_cfg, shadow_cfg_len);
756*e838c14aSCarl Huang }
757*e838c14aSCarl Huang EXPORT_SYMBOL(ath11k_ce_get_shadow_config);
758*e838c14aSCarl Huang 
759d5c65159SKalle Valo void ath11k_ce_cleanup_pipes(struct ath11k_base *ab)
760d5c65159SKalle Valo {
761d5c65159SKalle Valo 	struct ath11k_ce_pipe *pipe;
762d5c65159SKalle Valo 	int pipe_num;
763d5c65159SKalle Valo 
764d9d4b5f3SKalle Valo 	for (pipe_num = 0; pipe_num < ab->hw_params.ce_count; pipe_num++) {
765d5c65159SKalle Valo 		pipe = &ab->ce.ce_pipe[pipe_num];
766d5c65159SKalle Valo 		ath11k_ce_rx_pipe_cleanup(pipe);
767d5c65159SKalle Valo 
768d5c65159SKalle Valo 		/* Cleanup any src CE's which have interrupts disabled */
769d5c65159SKalle Valo 		ath11k_ce_poll_send_completed(ab, pipe_num);
770d5c65159SKalle Valo 
771d5c65159SKalle Valo 		/* NOTE: Should we also clean up tx buffer in all pipes? */
772d5c65159SKalle Valo 	}
773d5c65159SKalle Valo }
7747f4beda2SGovind Singh EXPORT_SYMBOL(ath11k_ce_cleanup_pipes);
775d5c65159SKalle Valo 
776d5c65159SKalle Valo void ath11k_ce_rx_post_buf(struct ath11k_base *ab)
777d5c65159SKalle Valo {
778d5c65159SKalle Valo 	struct ath11k_ce_pipe *pipe;
779d5c65159SKalle Valo 	int i;
780d5c65159SKalle Valo 	int ret;
781d5c65159SKalle Valo 
782d9d4b5f3SKalle Valo 	for (i = 0; i < ab->hw_params.ce_count; i++) {
783d5c65159SKalle Valo 		pipe = &ab->ce.ce_pipe[i];
784d5c65159SKalle Valo 		ret = ath11k_ce_rx_post_pipe(pipe);
785d5c65159SKalle Valo 		if (ret) {
786d5c65159SKalle Valo 			if (ret == -ENOSPC)
787d5c65159SKalle Valo 				continue;
788d5c65159SKalle Valo 
789d5c65159SKalle Valo 			ath11k_warn(ab, "failed to post rx buf to pipe: %d err: %d\n",
790d5c65159SKalle Valo 				    i, ret);
791d5c65159SKalle Valo 			mod_timer(&ab->rx_replenish_retry,
792d5c65159SKalle Valo 				  jiffies + ATH11K_CE_RX_POST_RETRY_JIFFIES);
793d5c65159SKalle Valo 
794d5c65159SKalle Valo 			return;
795d5c65159SKalle Valo 		}
796d5c65159SKalle Valo 	}
797d5c65159SKalle Valo }
7982c3960c2SGovind Singh EXPORT_SYMBOL(ath11k_ce_rx_post_buf);
799d5c65159SKalle Valo 
800d5c65159SKalle Valo void ath11k_ce_rx_replenish_retry(struct timer_list *t)
801d5c65159SKalle Valo {
802d5c65159SKalle Valo 	struct ath11k_base *ab = from_timer(ab, t, rx_replenish_retry);
803d5c65159SKalle Valo 
804d5c65159SKalle Valo 	ath11k_ce_rx_post_buf(ab);
805d5c65159SKalle Valo }
806d5c65159SKalle Valo 
807d5c65159SKalle Valo int ath11k_ce_init_pipes(struct ath11k_base *ab)
808d5c65159SKalle Valo {
809d5c65159SKalle Valo 	struct ath11k_ce_pipe *pipe;
810d5c65159SKalle Valo 	int i;
811d5c65159SKalle Valo 	int ret;
812d5c65159SKalle Valo 
813*e838c14aSCarl Huang 	ath11k_ce_get_shadow_config(ab, &ab->qmi.ce_cfg.shadow_reg_v2,
814*e838c14aSCarl Huang 				    &ab->qmi.ce_cfg.shadow_reg_v2_len);
815*e838c14aSCarl Huang 
816d9d4b5f3SKalle Valo 	for (i = 0; i < ab->hw_params.ce_count; i++) {
817d5c65159SKalle Valo 		pipe = &ab->ce.ce_pipe[i];
818d5c65159SKalle Valo 
819d5c65159SKalle Valo 		if (pipe->src_ring) {
820d5c65159SKalle Valo 			ret = ath11k_ce_init_ring(ab, pipe->src_ring, i,
821d5c65159SKalle Valo 						  HAL_CE_SRC);
822d5c65159SKalle Valo 			if (ret) {
823d5c65159SKalle Valo 				ath11k_warn(ab, "failed to init src ring: %d\n",
824d5c65159SKalle Valo 					    ret);
825d5c65159SKalle Valo 				/* Should we clear any partial init */
826d5c65159SKalle Valo 				return ret;
827d5c65159SKalle Valo 			}
828d5c65159SKalle Valo 
829d5c65159SKalle Valo 			pipe->src_ring->write_index = 0;
830d5c65159SKalle Valo 			pipe->src_ring->sw_index = 0;
831d5c65159SKalle Valo 		}
832d5c65159SKalle Valo 
833d5c65159SKalle Valo 		if (pipe->dest_ring) {
834d5c65159SKalle Valo 			ret = ath11k_ce_init_ring(ab, pipe->dest_ring, i,
835d5c65159SKalle Valo 						  HAL_CE_DST);
836d5c65159SKalle Valo 			if (ret) {
837d5c65159SKalle Valo 				ath11k_warn(ab, "failed to init dest ring: %d\n",
838d5c65159SKalle Valo 					    ret);
839d5c65159SKalle Valo 				/* Should we clear any partial init */
840d5c65159SKalle Valo 				return ret;
841d5c65159SKalle Valo 			}
842d5c65159SKalle Valo 
843d5c65159SKalle Valo 			pipe->rx_buf_needed = pipe->dest_ring->nentries ?
844d5c65159SKalle Valo 					      pipe->dest_ring->nentries - 2 : 0;
845d5c65159SKalle Valo 
846d5c65159SKalle Valo 			pipe->dest_ring->write_index = 0;
847d5c65159SKalle Valo 			pipe->dest_ring->sw_index = 0;
848d5c65159SKalle Valo 		}
849d5c65159SKalle Valo 
850d5c65159SKalle Valo 		if (pipe->status_ring) {
851d5c65159SKalle Valo 			ret = ath11k_ce_init_ring(ab, pipe->status_ring, i,
852d5c65159SKalle Valo 						  HAL_CE_DST_STATUS);
853d5c65159SKalle Valo 			if (ret) {
854d5c65159SKalle Valo 				ath11k_warn(ab, "failed to init dest status ing: %d\n",
855d5c65159SKalle Valo 					    ret);
856d5c65159SKalle Valo 				/* Should we clear any partial init */
857d5c65159SKalle Valo 				return ret;
858d5c65159SKalle Valo 			}
859d5c65159SKalle Valo 
860d5c65159SKalle Valo 			pipe->status_ring->write_index = 0;
861d5c65159SKalle Valo 			pipe->status_ring->sw_index = 0;
862d5c65159SKalle Valo 		}
863d5c65159SKalle Valo 	}
864d5c65159SKalle Valo 
865d5c65159SKalle Valo 	return 0;
866d5c65159SKalle Valo }
867d5c65159SKalle Valo 
868d5c65159SKalle Valo void ath11k_ce_free_pipes(struct ath11k_base *ab)
869d5c65159SKalle Valo {
870d5c65159SKalle Valo 	struct ath11k_ce_pipe *pipe;
871d5c65159SKalle Valo 	int desc_sz;
872d5c65159SKalle Valo 	int i;
873d5c65159SKalle Valo 
874d9d4b5f3SKalle Valo 	for (i = 0; i < ab->hw_params.ce_count; i++) {
875d5c65159SKalle Valo 		pipe = &ab->ce.ce_pipe[i];
876d5c65159SKalle Valo 
877d5c65159SKalle Valo 		if (pipe->src_ring) {
878d5c65159SKalle Valo 			desc_sz = ath11k_hal_ce_get_desc_size(HAL_CE_DESC_SRC);
879d5c65159SKalle Valo 			dma_free_coherent(ab->dev,
880d5c65159SKalle Valo 					  pipe->src_ring->nentries * desc_sz +
881d5c65159SKalle Valo 					  CE_DESC_RING_ALIGN,
882d5c65159SKalle Valo 					  pipe->src_ring->base_addr_owner_space,
883d5c65159SKalle Valo 					  pipe->src_ring->base_addr_ce_space);
884d5c65159SKalle Valo 			kfree(pipe->src_ring);
885d5c65159SKalle Valo 			pipe->src_ring = NULL;
886d5c65159SKalle Valo 		}
887d5c65159SKalle Valo 
888d5c65159SKalle Valo 		if (pipe->dest_ring) {
889d5c65159SKalle Valo 			desc_sz = ath11k_hal_ce_get_desc_size(HAL_CE_DESC_DST);
890d5c65159SKalle Valo 			dma_free_coherent(ab->dev,
891d5c65159SKalle Valo 					  pipe->dest_ring->nentries * desc_sz +
892d5c65159SKalle Valo 					  CE_DESC_RING_ALIGN,
893d5c65159SKalle Valo 					  pipe->dest_ring->base_addr_owner_space,
894d5c65159SKalle Valo 					  pipe->dest_ring->base_addr_ce_space);
895d5c65159SKalle Valo 			kfree(pipe->dest_ring);
896d5c65159SKalle Valo 			pipe->dest_ring = NULL;
897d5c65159SKalle Valo 		}
898d5c65159SKalle Valo 
899d5c65159SKalle Valo 		if (pipe->status_ring) {
900d5c65159SKalle Valo 			desc_sz =
901d5c65159SKalle Valo 			  ath11k_hal_ce_get_desc_size(HAL_CE_DESC_DST_STATUS);
902d5c65159SKalle Valo 			dma_free_coherent(ab->dev,
903d5c65159SKalle Valo 					  pipe->status_ring->nentries * desc_sz +
904d5c65159SKalle Valo 					  CE_DESC_RING_ALIGN,
905d5c65159SKalle Valo 					  pipe->status_ring->base_addr_owner_space,
906d5c65159SKalle Valo 					  pipe->status_ring->base_addr_ce_space);
907d5c65159SKalle Valo 			kfree(pipe->status_ring);
908d5c65159SKalle Valo 			pipe->status_ring = NULL;
909d5c65159SKalle Valo 		}
910d5c65159SKalle Valo 	}
911d5c65159SKalle Valo }
9126e0355afSGovind Singh EXPORT_SYMBOL(ath11k_ce_free_pipes);
913d5c65159SKalle Valo 
914d5c65159SKalle Valo int ath11k_ce_alloc_pipes(struct ath11k_base *ab)
915d5c65159SKalle Valo {
916d5c65159SKalle Valo 	struct ath11k_ce_pipe *pipe;
917d5c65159SKalle Valo 	int i;
918d5c65159SKalle Valo 	int ret;
919d5c65159SKalle Valo 	const struct ce_attr *attr;
920d5c65159SKalle Valo 
921d5c65159SKalle Valo 	spin_lock_init(&ab->ce.ce_lock);
922d5c65159SKalle Valo 
923d9d4b5f3SKalle Valo 	for (i = 0; i < ab->hw_params.ce_count; i++) {
9246e5e9f59SKalle Valo 		attr = &ab->hw_params.host_ce_config[i];
925d5c65159SKalle Valo 		pipe = &ab->ce.ce_pipe[i];
926d5c65159SKalle Valo 		pipe->pipe_num = i;
927d5c65159SKalle Valo 		pipe->ab = ab;
928d5c65159SKalle Valo 		pipe->buf_sz = attr->src_sz_max;
929d5c65159SKalle Valo 
930d5c65159SKalle Valo 		ret = ath11k_ce_alloc_pipe(ab, i);
931d5c65159SKalle Valo 		if (ret) {
932d5c65159SKalle Valo 			/* Free any parial successful allocation */
933d5c65159SKalle Valo 			ath11k_ce_free_pipes(ab);
934d5c65159SKalle Valo 			return ret;
935d5c65159SKalle Valo 		}
936d5c65159SKalle Valo 	}
937d5c65159SKalle Valo 
938d5c65159SKalle Valo 	return 0;
939d5c65159SKalle Valo }
9407f4beda2SGovind Singh EXPORT_SYMBOL(ath11k_ce_alloc_pipes);
941d5c65159SKalle Valo 
942d5c65159SKalle Valo /* For Big Endian Host, Copy Engine byte_swap is enabled
943d5c65159SKalle Valo  * When Copy Engine does byte_swap, need to byte swap again for the
944d5c65159SKalle Valo  * Host to get/put buffer content in the correct byte order
945d5c65159SKalle Valo  */
946d5c65159SKalle Valo void ath11k_ce_byte_swap(void *mem, u32 len)
947d5c65159SKalle Valo {
948d5c65159SKalle Valo 	int i;
949d5c65159SKalle Valo 
950d5c65159SKalle Valo 	if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN)) {
951d5c65159SKalle Valo 		if (!mem)
952d5c65159SKalle Valo 			return;
953d5c65159SKalle Valo 
954d5c65159SKalle Valo 		for (i = 0; i < (len / 4); i++) {
955d5c65159SKalle Valo 			*(u32 *)mem = swab32(*(u32 *)mem);
956d5c65159SKalle Valo 			mem += 4;
957d5c65159SKalle Valo 		}
958d5c65159SKalle Valo 	}
959d5c65159SKalle Valo }
960d5c65159SKalle Valo 
961e3396b8bSCarl Huang int ath11k_ce_get_attr_flags(struct ath11k_base *ab, int ce_id)
962d5c65159SKalle Valo {
963d9d4b5f3SKalle Valo 	if (ce_id >= ab->hw_params.ce_count)
964d5c65159SKalle Valo 		return -EINVAL;
965d5c65159SKalle Valo 
9666e5e9f59SKalle Valo 	return ab->hw_params.host_ce_config[ce_id].flags;
967d5c65159SKalle Valo }
9686e0355afSGovind Singh EXPORT_SYMBOL(ath11k_ce_get_attr_flags);
969