1 // SPDX-License-Identifier: BSD-3-Clause-Clear
2 /*
3  * Copyright (c) 2018-2021 The Linux Foundation. All rights reserved.
4  * Copyright (c) 2021-2022 Qualcomm Innovation Center, Inc. All rights reserved.
5  */
6 
7 #include <linux/ieee80211.h>
8 #include <linux/kernel.h>
9 #include <linux/skbuff.h>
10 #include <crypto/hash.h>
11 #include "core.h"
12 #include "debug.h"
13 #include "hal_desc.h"
14 #include "hw.h"
15 #include "dp_rx.h"
16 #include "hal_rx.h"
17 #include "dp_tx.h"
18 #include "peer.h"
19 #include "dp_mon.h"
20 
21 #define ATH12K_DP_RX_FRAGMENT_TIMEOUT_MS (2 * HZ)
22 
23 static enum hal_encrypt_type ath12k_dp_rx_h_enctype(struct ath12k_base *ab,
24 						    struct hal_rx_desc *desc)
25 {
26 	if (!ab->hw_params->hal_ops->rx_desc_encrypt_valid(desc))
27 		return HAL_ENCRYPT_TYPE_OPEN;
28 
29 	return ab->hw_params->hal_ops->rx_desc_get_encrypt_type(desc);
30 }
31 
32 u8 ath12k_dp_rx_h_decap_type(struct ath12k_base *ab,
33 			     struct hal_rx_desc *desc)
34 {
35 	return ab->hw_params->hal_ops->rx_desc_get_decap_type(desc);
36 }
37 
38 static u8 ath12k_dp_rx_h_mesh_ctl_present(struct ath12k_base *ab,
39 					  struct hal_rx_desc *desc)
40 {
41 	return ab->hw_params->hal_ops->rx_desc_get_mesh_ctl(desc);
42 }
43 
44 static bool ath12k_dp_rx_h_seq_ctrl_valid(struct ath12k_base *ab,
45 					  struct hal_rx_desc *desc)
46 {
47 	return ab->hw_params->hal_ops->rx_desc_get_mpdu_seq_ctl_vld(desc);
48 }
49 
50 static bool ath12k_dp_rx_h_fc_valid(struct ath12k_base *ab,
51 				    struct hal_rx_desc *desc)
52 {
53 	return ab->hw_params->hal_ops->rx_desc_get_mpdu_fc_valid(desc);
54 }
55 
56 static bool ath12k_dp_rx_h_more_frags(struct ath12k_base *ab,
57 				      struct sk_buff *skb)
58 {
59 	struct ieee80211_hdr *hdr;
60 
61 	hdr = (struct ieee80211_hdr *)(skb->data + ab->hw_params->hal_desc_sz);
62 	return ieee80211_has_morefrags(hdr->frame_control);
63 }
64 
65 static u16 ath12k_dp_rx_h_frag_no(struct ath12k_base *ab,
66 				  struct sk_buff *skb)
67 {
68 	struct ieee80211_hdr *hdr;
69 
70 	hdr = (struct ieee80211_hdr *)(skb->data + ab->hw_params->hal_desc_sz);
71 	return le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG;
72 }
73 
74 static u16 ath12k_dp_rx_h_seq_no(struct ath12k_base *ab,
75 				 struct hal_rx_desc *desc)
76 {
77 	return ab->hw_params->hal_ops->rx_desc_get_mpdu_start_seq_no(desc);
78 }
79 
80 static bool ath12k_dp_rx_h_msdu_done(struct ath12k_base *ab,
81 				     struct hal_rx_desc *desc)
82 {
83 	return ab->hw_params->hal_ops->dp_rx_h_msdu_done(desc);
84 }
85 
86 static bool ath12k_dp_rx_h_l4_cksum_fail(struct ath12k_base *ab,
87 					 struct hal_rx_desc *desc)
88 {
89 	return ab->hw_params->hal_ops->dp_rx_h_l4_cksum_fail(desc);
90 }
91 
92 static bool ath12k_dp_rx_h_ip_cksum_fail(struct ath12k_base *ab,
93 					 struct hal_rx_desc *desc)
94 {
95 	return ab->hw_params->hal_ops->dp_rx_h_ip_cksum_fail(desc);
96 }
97 
98 static bool ath12k_dp_rx_h_is_decrypted(struct ath12k_base *ab,
99 					struct hal_rx_desc *desc)
100 {
101 	return ab->hw_params->hal_ops->dp_rx_h_is_decrypted(desc);
102 }
103 
104 u32 ath12k_dp_rx_h_mpdu_err(struct ath12k_base *ab,
105 			    struct hal_rx_desc *desc)
106 {
107 	return ab->hw_params->hal_ops->dp_rx_h_mpdu_err(desc);
108 }
109 
110 static u16 ath12k_dp_rx_h_msdu_len(struct ath12k_base *ab,
111 				   struct hal_rx_desc *desc)
112 {
113 	return ab->hw_params->hal_ops->rx_desc_get_msdu_len(desc);
114 }
115 
116 static u8 ath12k_dp_rx_h_sgi(struct ath12k_base *ab,
117 			     struct hal_rx_desc *desc)
118 {
119 	return ab->hw_params->hal_ops->rx_desc_get_msdu_sgi(desc);
120 }
121 
122 static u8 ath12k_dp_rx_h_rate_mcs(struct ath12k_base *ab,
123 				  struct hal_rx_desc *desc)
124 {
125 	return ab->hw_params->hal_ops->rx_desc_get_msdu_rate_mcs(desc);
126 }
127 
128 static u8 ath12k_dp_rx_h_rx_bw(struct ath12k_base *ab,
129 			       struct hal_rx_desc *desc)
130 {
131 	return ab->hw_params->hal_ops->rx_desc_get_msdu_rx_bw(desc);
132 }
133 
134 static u32 ath12k_dp_rx_h_freq(struct ath12k_base *ab,
135 			       struct hal_rx_desc *desc)
136 {
137 	return ab->hw_params->hal_ops->rx_desc_get_msdu_freq(desc);
138 }
139 
140 static u8 ath12k_dp_rx_h_pkt_type(struct ath12k_base *ab,
141 				  struct hal_rx_desc *desc)
142 {
143 	return ab->hw_params->hal_ops->rx_desc_get_msdu_pkt_type(desc);
144 }
145 
146 static u8 ath12k_dp_rx_h_nss(struct ath12k_base *ab,
147 			     struct hal_rx_desc *desc)
148 {
149 	return hweight8(ab->hw_params->hal_ops->rx_desc_get_msdu_nss(desc));
150 }
151 
152 static u8 ath12k_dp_rx_h_tid(struct ath12k_base *ab,
153 			     struct hal_rx_desc *desc)
154 {
155 	return ab->hw_params->hal_ops->rx_desc_get_mpdu_tid(desc);
156 }
157 
158 static u16 ath12k_dp_rx_h_peer_id(struct ath12k_base *ab,
159 				  struct hal_rx_desc *desc)
160 {
161 	return ab->hw_params->hal_ops->rx_desc_get_mpdu_peer_id(desc);
162 }
163 
164 u8 ath12k_dp_rx_h_l3pad(struct ath12k_base *ab,
165 			struct hal_rx_desc *desc)
166 {
167 	return ab->hw_params->hal_ops->rx_desc_get_l3_pad_bytes(desc);
168 }
169 
170 static bool ath12k_dp_rx_h_first_msdu(struct ath12k_base *ab,
171 				      struct hal_rx_desc *desc)
172 {
173 	return ab->hw_params->hal_ops->rx_desc_get_first_msdu(desc);
174 }
175 
176 static bool ath12k_dp_rx_h_last_msdu(struct ath12k_base *ab,
177 				     struct hal_rx_desc *desc)
178 {
179 	return ab->hw_params->hal_ops->rx_desc_get_last_msdu(desc);
180 }
181 
182 static void ath12k_dp_rx_desc_end_tlv_copy(struct ath12k_base *ab,
183 					   struct hal_rx_desc *fdesc,
184 					   struct hal_rx_desc *ldesc)
185 {
186 	ab->hw_params->hal_ops->rx_desc_copy_end_tlv(fdesc, ldesc);
187 }
188 
189 static void ath12k_dp_rxdesc_set_msdu_len(struct ath12k_base *ab,
190 					  struct hal_rx_desc *desc,
191 					  u16 len)
192 {
193 	ab->hw_params->hal_ops->rx_desc_set_msdu_len(desc, len);
194 }
195 
196 static bool ath12k_dp_rx_h_is_da_mcbc(struct ath12k_base *ab,
197 				      struct hal_rx_desc *desc)
198 {
199 	return (ath12k_dp_rx_h_first_msdu(ab, desc) &&
200 		ab->hw_params->hal_ops->rx_desc_is_da_mcbc(desc));
201 }
202 
203 static bool ath12k_dp_rxdesc_mac_addr2_valid(struct ath12k_base *ab,
204 					     struct hal_rx_desc *desc)
205 {
206 	return ab->hw_params->hal_ops->rx_desc_mac_addr2_valid(desc);
207 }
208 
209 static u8 *ath12k_dp_rxdesc_get_mpdu_start_addr2(struct ath12k_base *ab,
210 						 struct hal_rx_desc *desc)
211 {
212 	return ab->hw_params->hal_ops->rx_desc_mpdu_start_addr2(desc);
213 }
214 
215 static void ath12k_dp_rx_desc_get_dot11_hdr(struct ath12k_base *ab,
216 					    struct hal_rx_desc *desc,
217 					    struct ieee80211_hdr *hdr)
218 {
219 	ab->hw_params->hal_ops->rx_desc_get_dot11_hdr(desc, hdr);
220 }
221 
222 static void ath12k_dp_rx_desc_get_crypto_header(struct ath12k_base *ab,
223 						struct hal_rx_desc *desc,
224 						u8 *crypto_hdr,
225 						enum hal_encrypt_type enctype)
226 {
227 	ab->hw_params->hal_ops->rx_desc_get_crypto_header(desc, crypto_hdr, enctype);
228 }
229 
230 static u16 ath12k_dp_rxdesc_get_mpdu_frame_ctrl(struct ath12k_base *ab,
231 						struct hal_rx_desc *desc)
232 {
233 	return ab->hw_params->hal_ops->rx_desc_get_mpdu_frame_ctl(desc);
234 }
235 
236 static int ath12k_dp_purge_mon_ring(struct ath12k_base *ab)
237 {
238 	int i, reaped = 0;
239 	unsigned long timeout = jiffies + msecs_to_jiffies(DP_MON_PURGE_TIMEOUT_MS);
240 
241 	do {
242 		for (i = 0; i < ab->hw_params->num_rxmda_per_pdev; i++)
243 			reaped += ath12k_dp_mon_process_ring(ab, i, NULL,
244 							     DP_MON_SERVICE_BUDGET,
245 							     ATH12K_DP_RX_MONITOR_MODE);
246 
247 		/* nothing more to reap */
248 		if (reaped < DP_MON_SERVICE_BUDGET)
249 			return 0;
250 
251 	} while (time_before(jiffies, timeout));
252 
253 	ath12k_warn(ab, "dp mon ring purge timeout");
254 
255 	return -ETIMEDOUT;
256 }
257 
258 /* Returns number of Rx buffers replenished */
259 int ath12k_dp_rx_bufs_replenish(struct ath12k_base *ab, int mac_id,
260 				struct dp_rxdma_ring *rx_ring,
261 				int req_entries,
262 				enum hal_rx_buf_return_buf_manager mgr,
263 				bool hw_cc)
264 {
265 	struct ath12k_buffer_addr *desc;
266 	struct hal_srng *srng;
267 	struct sk_buff *skb;
268 	int num_free;
269 	int num_remain;
270 	int buf_id;
271 	u32 cookie;
272 	dma_addr_t paddr;
273 	struct ath12k_dp *dp = &ab->dp;
274 	struct ath12k_rx_desc_info *rx_desc;
275 
276 	req_entries = min(req_entries, rx_ring->bufs_max);
277 
278 	srng = &ab->hal.srng_list[rx_ring->refill_buf_ring.ring_id];
279 
280 	spin_lock_bh(&srng->lock);
281 
282 	ath12k_hal_srng_access_begin(ab, srng);
283 
284 	num_free = ath12k_hal_srng_src_num_free(ab, srng, true);
285 	if (!req_entries && (num_free > (rx_ring->bufs_max * 3) / 4))
286 		req_entries = num_free;
287 
288 	req_entries = min(num_free, req_entries);
289 	num_remain = req_entries;
290 
291 	while (num_remain > 0) {
292 		skb = dev_alloc_skb(DP_RX_BUFFER_SIZE +
293 				    DP_RX_BUFFER_ALIGN_SIZE);
294 		if (!skb)
295 			break;
296 
297 		if (!IS_ALIGNED((unsigned long)skb->data,
298 				DP_RX_BUFFER_ALIGN_SIZE)) {
299 			skb_pull(skb,
300 				 PTR_ALIGN(skb->data, DP_RX_BUFFER_ALIGN_SIZE) -
301 				 skb->data);
302 		}
303 
304 		paddr = dma_map_single(ab->dev, skb->data,
305 				       skb->len + skb_tailroom(skb),
306 				       DMA_FROM_DEVICE);
307 		if (dma_mapping_error(ab->dev, paddr))
308 			goto fail_free_skb;
309 
310 		if (hw_cc) {
311 			spin_lock_bh(&dp->rx_desc_lock);
312 
313 			/* Get desc from free list and store in used list
314 			 * for cleanup purposes
315 			 *
316 			 * TODO: pass the removed descs rather than
317 			 * add/read to optimize
318 			 */
319 			rx_desc = list_first_entry_or_null(&dp->rx_desc_free_list,
320 							   struct ath12k_rx_desc_info,
321 							   list);
322 			if (!rx_desc) {
323 				spin_unlock_bh(&dp->rx_desc_lock);
324 				goto fail_dma_unmap;
325 			}
326 
327 			rx_desc->skb = skb;
328 			cookie = rx_desc->cookie;
329 			list_del(&rx_desc->list);
330 			list_add_tail(&rx_desc->list, &dp->rx_desc_used_list);
331 
332 			spin_unlock_bh(&dp->rx_desc_lock);
333 		} else {
334 			spin_lock_bh(&rx_ring->idr_lock);
335 			buf_id = idr_alloc(&rx_ring->bufs_idr, skb, 0,
336 					   rx_ring->bufs_max * 3, GFP_ATOMIC);
337 			spin_unlock_bh(&rx_ring->idr_lock);
338 			if (buf_id < 0)
339 				goto fail_dma_unmap;
340 			cookie = u32_encode_bits(mac_id,
341 						 DP_RXDMA_BUF_COOKIE_PDEV_ID) |
342 				 u32_encode_bits(buf_id,
343 						 DP_RXDMA_BUF_COOKIE_BUF_ID);
344 		}
345 
346 		desc = ath12k_hal_srng_src_get_next_entry(ab, srng);
347 		if (!desc)
348 			goto fail_buf_unassign;
349 
350 		ATH12K_SKB_RXCB(skb)->paddr = paddr;
351 
352 		num_remain--;
353 
354 		ath12k_hal_rx_buf_addr_info_set(desc, paddr, cookie, mgr);
355 	}
356 
357 	ath12k_hal_srng_access_end(ab, srng);
358 
359 	spin_unlock_bh(&srng->lock);
360 
361 	return req_entries - num_remain;
362 
363 fail_buf_unassign:
364 	if (hw_cc) {
365 		spin_lock_bh(&dp->rx_desc_lock);
366 		list_del(&rx_desc->list);
367 		list_add_tail(&rx_desc->list, &dp->rx_desc_free_list);
368 		rx_desc->skb = NULL;
369 		spin_unlock_bh(&dp->rx_desc_lock);
370 	} else {
371 		spin_lock_bh(&rx_ring->idr_lock);
372 		idr_remove(&rx_ring->bufs_idr, buf_id);
373 		spin_unlock_bh(&rx_ring->idr_lock);
374 	}
375 fail_dma_unmap:
376 	dma_unmap_single(ab->dev, paddr, skb->len + skb_tailroom(skb),
377 			 DMA_FROM_DEVICE);
378 fail_free_skb:
379 	dev_kfree_skb_any(skb);
380 
381 	ath12k_hal_srng_access_end(ab, srng);
382 
383 	spin_unlock_bh(&srng->lock);
384 
385 	return req_entries - num_remain;
386 }
387 
388 static int ath12k_dp_rxdma_buf_ring_free(struct ath12k_base *ab,
389 					 struct dp_rxdma_ring *rx_ring)
390 {
391 	struct sk_buff *skb;
392 	int buf_id;
393 
394 	spin_lock_bh(&rx_ring->idr_lock);
395 	idr_for_each_entry(&rx_ring->bufs_idr, skb, buf_id) {
396 		idr_remove(&rx_ring->bufs_idr, buf_id);
397 		/* TODO: Understand where internal driver does this dma_unmap
398 		 * of rxdma_buffer.
399 		 */
400 		dma_unmap_single(ab->dev, ATH12K_SKB_RXCB(skb)->paddr,
401 				 skb->len + skb_tailroom(skb), DMA_FROM_DEVICE);
402 		dev_kfree_skb_any(skb);
403 	}
404 
405 	idr_destroy(&rx_ring->bufs_idr);
406 	spin_unlock_bh(&rx_ring->idr_lock);
407 
408 	return 0;
409 }
410 
411 static int ath12k_dp_rxdma_buf_free(struct ath12k_base *ab)
412 {
413 	struct ath12k_dp *dp = &ab->dp;
414 	struct dp_rxdma_ring *rx_ring = &dp->rx_refill_buf_ring;
415 
416 	ath12k_dp_rxdma_buf_ring_free(ab, rx_ring);
417 
418 	rx_ring = &dp->rxdma_mon_buf_ring;
419 	ath12k_dp_rxdma_buf_ring_free(ab, rx_ring);
420 
421 	rx_ring = &dp->tx_mon_buf_ring;
422 	ath12k_dp_rxdma_buf_ring_free(ab, rx_ring);
423 
424 	return 0;
425 }
426 
427 static int ath12k_dp_rxdma_ring_buf_setup(struct ath12k_base *ab,
428 					  struct dp_rxdma_ring *rx_ring,
429 					  u32 ringtype)
430 {
431 	int num_entries;
432 
433 	num_entries = rx_ring->refill_buf_ring.size /
434 		ath12k_hal_srng_get_entrysize(ab, ringtype);
435 
436 	rx_ring->bufs_max = num_entries;
437 	if ((ringtype == HAL_RXDMA_MONITOR_BUF) || (ringtype == HAL_TX_MONITOR_BUF))
438 		ath12k_dp_mon_buf_replenish(ab, rx_ring, num_entries);
439 	else
440 		ath12k_dp_rx_bufs_replenish(ab, 0, rx_ring, num_entries,
441 					    ab->hw_params->hal_params->rx_buf_rbm,
442 					    ringtype == HAL_RXDMA_BUF);
443 	return 0;
444 }
445 
446 static int ath12k_dp_rxdma_buf_setup(struct ath12k_base *ab)
447 {
448 	struct ath12k_dp *dp = &ab->dp;
449 	struct dp_rxdma_ring *rx_ring = &dp->rx_refill_buf_ring;
450 	int ret;
451 
452 	ret = ath12k_dp_rxdma_ring_buf_setup(ab, rx_ring,
453 					     HAL_RXDMA_BUF);
454 	if (ret) {
455 		ath12k_warn(ab,
456 			    "failed to setup HAL_RXDMA_BUF\n");
457 		return ret;
458 	}
459 
460 	if (ab->hw_params->rxdma1_enable) {
461 		rx_ring = &dp->rxdma_mon_buf_ring;
462 		ret = ath12k_dp_rxdma_ring_buf_setup(ab, rx_ring,
463 						     HAL_RXDMA_MONITOR_BUF);
464 		if (ret) {
465 			ath12k_warn(ab,
466 				    "failed to setup HAL_RXDMA_MONITOR_BUF\n");
467 			return ret;
468 		}
469 
470 		rx_ring = &dp->tx_mon_buf_ring;
471 		ret = ath12k_dp_rxdma_ring_buf_setup(ab, rx_ring,
472 						     HAL_TX_MONITOR_BUF);
473 		if (ret) {
474 			ath12k_warn(ab,
475 				    "failed to setup HAL_TX_MONITOR_BUF\n");
476 			return ret;
477 		}
478 	}
479 
480 	return 0;
481 }
482 
483 static void ath12k_dp_rx_pdev_srng_free(struct ath12k *ar)
484 {
485 	struct ath12k_pdev_dp *dp = &ar->dp;
486 	struct ath12k_base *ab = ar->ab;
487 	int i;
488 
489 	for (i = 0; i < ab->hw_params->num_rxmda_per_pdev; i++) {
490 		ath12k_dp_srng_cleanup(ab, &dp->rxdma_mon_dst_ring[i]);
491 		ath12k_dp_srng_cleanup(ab, &dp->tx_mon_dst_ring[i]);
492 	}
493 }
494 
495 void ath12k_dp_rx_pdev_reo_cleanup(struct ath12k_base *ab)
496 {
497 	struct ath12k_dp *dp = &ab->dp;
498 	int i;
499 
500 	for (i = 0; i < DP_REO_DST_RING_MAX; i++)
501 		ath12k_dp_srng_cleanup(ab, &dp->reo_dst_ring[i]);
502 }
503 
504 int ath12k_dp_rx_pdev_reo_setup(struct ath12k_base *ab)
505 {
506 	struct ath12k_dp *dp = &ab->dp;
507 	int ret;
508 	int i;
509 
510 	for (i = 0; i < DP_REO_DST_RING_MAX; i++) {
511 		ret = ath12k_dp_srng_setup(ab, &dp->reo_dst_ring[i],
512 					   HAL_REO_DST, i, 0,
513 					   DP_REO_DST_RING_SIZE);
514 		if (ret) {
515 			ath12k_warn(ab, "failed to setup reo_dst_ring\n");
516 			goto err_reo_cleanup;
517 		}
518 	}
519 
520 	return 0;
521 
522 err_reo_cleanup:
523 	ath12k_dp_rx_pdev_reo_cleanup(ab);
524 
525 	return ret;
526 }
527 
528 static int ath12k_dp_rx_pdev_srng_alloc(struct ath12k *ar)
529 {
530 	struct ath12k_pdev_dp *dp = &ar->dp;
531 	struct ath12k_base *ab = ar->ab;
532 	int i;
533 	int ret;
534 	u32 mac_id = dp->mac_id;
535 
536 	for (i = 0; i < ab->hw_params->num_rxmda_per_pdev; i++) {
537 		ret = ath12k_dp_srng_setup(ar->ab,
538 					   &dp->rxdma_mon_dst_ring[i],
539 					   HAL_RXDMA_MONITOR_DST,
540 					   0, mac_id + i,
541 					   DP_RXDMA_MONITOR_DST_RING_SIZE);
542 		if (ret) {
543 			ath12k_warn(ar->ab,
544 				    "failed to setup HAL_RXDMA_MONITOR_DST\n");
545 			return ret;
546 		}
547 
548 		ret = ath12k_dp_srng_setup(ar->ab,
549 					   &dp->tx_mon_dst_ring[i],
550 					   HAL_TX_MONITOR_DST,
551 					   0, mac_id + i,
552 					   DP_TX_MONITOR_DEST_RING_SIZE);
553 		if (ret) {
554 			ath12k_warn(ar->ab,
555 				    "failed to setup HAL_TX_MONITOR_DST\n");
556 			return ret;
557 		}
558 	}
559 
560 	return 0;
561 }
562 
563 void ath12k_dp_rx_reo_cmd_list_cleanup(struct ath12k_base *ab)
564 {
565 	struct ath12k_dp *dp = &ab->dp;
566 	struct ath12k_dp_rx_reo_cmd *cmd, *tmp;
567 	struct ath12k_dp_rx_reo_cache_flush_elem *cmd_cache, *tmp_cache;
568 
569 	spin_lock_bh(&dp->reo_cmd_lock);
570 	list_for_each_entry_safe(cmd, tmp, &dp->reo_cmd_list, list) {
571 		list_del(&cmd->list);
572 		dma_unmap_single(ab->dev, cmd->data.paddr,
573 				 cmd->data.size, DMA_BIDIRECTIONAL);
574 		kfree(cmd->data.vaddr);
575 		kfree(cmd);
576 	}
577 
578 	list_for_each_entry_safe(cmd_cache, tmp_cache,
579 				 &dp->reo_cmd_cache_flush_list, list) {
580 		list_del(&cmd_cache->list);
581 		dp->reo_cmd_cache_flush_count--;
582 		dma_unmap_single(ab->dev, cmd_cache->data.paddr,
583 				 cmd_cache->data.size, DMA_BIDIRECTIONAL);
584 		kfree(cmd_cache->data.vaddr);
585 		kfree(cmd_cache);
586 	}
587 	spin_unlock_bh(&dp->reo_cmd_lock);
588 }
589 
590 static void ath12k_dp_reo_cmd_free(struct ath12k_dp *dp, void *ctx,
591 				   enum hal_reo_cmd_status status)
592 {
593 	struct ath12k_dp_rx_tid *rx_tid = ctx;
594 
595 	if (status != HAL_REO_CMD_SUCCESS)
596 		ath12k_warn(dp->ab, "failed to flush rx tid hw desc, tid %d status %d\n",
597 			    rx_tid->tid, status);
598 
599 	dma_unmap_single(dp->ab->dev, rx_tid->paddr, rx_tid->size,
600 			 DMA_BIDIRECTIONAL);
601 	kfree(rx_tid->vaddr);
602 	rx_tid->vaddr = NULL;
603 }
604 
605 static int ath12k_dp_reo_cmd_send(struct ath12k_base *ab, struct ath12k_dp_rx_tid *rx_tid,
606 				  enum hal_reo_cmd_type type,
607 				  struct ath12k_hal_reo_cmd *cmd,
608 				  void (*cb)(struct ath12k_dp *dp, void *ctx,
609 					     enum hal_reo_cmd_status status))
610 {
611 	struct ath12k_dp *dp = &ab->dp;
612 	struct ath12k_dp_rx_reo_cmd *dp_cmd;
613 	struct hal_srng *cmd_ring;
614 	int cmd_num;
615 
616 	cmd_ring = &ab->hal.srng_list[dp->reo_cmd_ring.ring_id];
617 	cmd_num = ath12k_hal_reo_cmd_send(ab, cmd_ring, type, cmd);
618 
619 	/* cmd_num should start from 1, during failure return the error code */
620 	if (cmd_num < 0)
621 		return cmd_num;
622 
623 	/* reo cmd ring descriptors has cmd_num starting from 1 */
624 	if (cmd_num == 0)
625 		return -EINVAL;
626 
627 	if (!cb)
628 		return 0;
629 
630 	/* Can this be optimized so that we keep the pending command list only
631 	 * for tid delete command to free up the resource on the command status
632 	 * indication?
633 	 */
634 	dp_cmd = kzalloc(sizeof(*dp_cmd), GFP_ATOMIC);
635 
636 	if (!dp_cmd)
637 		return -ENOMEM;
638 
639 	memcpy(&dp_cmd->data, rx_tid, sizeof(*rx_tid));
640 	dp_cmd->cmd_num = cmd_num;
641 	dp_cmd->handler = cb;
642 
643 	spin_lock_bh(&dp->reo_cmd_lock);
644 	list_add_tail(&dp_cmd->list, &dp->reo_cmd_list);
645 	spin_unlock_bh(&dp->reo_cmd_lock);
646 
647 	return 0;
648 }
649 
650 static void ath12k_dp_reo_cache_flush(struct ath12k_base *ab,
651 				      struct ath12k_dp_rx_tid *rx_tid)
652 {
653 	struct ath12k_hal_reo_cmd cmd = {0};
654 	unsigned long tot_desc_sz, desc_sz;
655 	int ret;
656 
657 	tot_desc_sz = rx_tid->size;
658 	desc_sz = ath12k_hal_reo_qdesc_size(0, HAL_DESC_REO_NON_QOS_TID);
659 
660 	while (tot_desc_sz > desc_sz) {
661 		tot_desc_sz -= desc_sz;
662 		cmd.addr_lo = lower_32_bits(rx_tid->paddr + tot_desc_sz);
663 		cmd.addr_hi = upper_32_bits(rx_tid->paddr);
664 		ret = ath12k_dp_reo_cmd_send(ab, rx_tid,
665 					     HAL_REO_CMD_FLUSH_CACHE, &cmd,
666 					     NULL);
667 		if (ret)
668 			ath12k_warn(ab,
669 				    "failed to send HAL_REO_CMD_FLUSH_CACHE, tid %d (%d)\n",
670 				    rx_tid->tid, ret);
671 	}
672 
673 	memset(&cmd, 0, sizeof(cmd));
674 	cmd.addr_lo = lower_32_bits(rx_tid->paddr);
675 	cmd.addr_hi = upper_32_bits(rx_tid->paddr);
676 	cmd.flag = HAL_REO_CMD_FLG_NEED_STATUS;
677 	ret = ath12k_dp_reo_cmd_send(ab, rx_tid,
678 				     HAL_REO_CMD_FLUSH_CACHE,
679 				     &cmd, ath12k_dp_reo_cmd_free);
680 	if (ret) {
681 		ath12k_err(ab, "failed to send HAL_REO_CMD_FLUSH_CACHE cmd, tid %d (%d)\n",
682 			   rx_tid->tid, ret);
683 		dma_unmap_single(ab->dev, rx_tid->paddr, rx_tid->size,
684 				 DMA_BIDIRECTIONAL);
685 		kfree(rx_tid->vaddr);
686 		rx_tid->vaddr = NULL;
687 	}
688 }
689 
690 static void ath12k_dp_rx_tid_del_func(struct ath12k_dp *dp, void *ctx,
691 				      enum hal_reo_cmd_status status)
692 {
693 	struct ath12k_base *ab = dp->ab;
694 	struct ath12k_dp_rx_tid *rx_tid = ctx;
695 	struct ath12k_dp_rx_reo_cache_flush_elem *elem, *tmp;
696 
697 	if (status == HAL_REO_CMD_DRAIN) {
698 		goto free_desc;
699 	} else if (status != HAL_REO_CMD_SUCCESS) {
700 		/* Shouldn't happen! Cleanup in case of other failure? */
701 		ath12k_warn(ab, "failed to delete rx tid %d hw descriptor %d\n",
702 			    rx_tid->tid, status);
703 		return;
704 	}
705 
706 	elem = kzalloc(sizeof(*elem), GFP_ATOMIC);
707 	if (!elem)
708 		goto free_desc;
709 
710 	elem->ts = jiffies;
711 	memcpy(&elem->data, rx_tid, sizeof(*rx_tid));
712 
713 	spin_lock_bh(&dp->reo_cmd_lock);
714 	list_add_tail(&elem->list, &dp->reo_cmd_cache_flush_list);
715 	dp->reo_cmd_cache_flush_count++;
716 
717 	/* Flush and invalidate aged REO desc from HW cache */
718 	list_for_each_entry_safe(elem, tmp, &dp->reo_cmd_cache_flush_list,
719 				 list) {
720 		if (dp->reo_cmd_cache_flush_count > ATH12K_DP_RX_REO_DESC_FREE_THRES ||
721 		    time_after(jiffies, elem->ts +
722 			       msecs_to_jiffies(ATH12K_DP_RX_REO_DESC_FREE_TIMEOUT_MS))) {
723 			list_del(&elem->list);
724 			dp->reo_cmd_cache_flush_count--;
725 
726 			/* Unlock the reo_cmd_lock before using ath12k_dp_reo_cmd_send()
727 			 * within ath12k_dp_reo_cache_flush. The reo_cmd_cache_flush_list
728 			 * is used in only two contexts, one is in this function called
729 			 * from napi and the other in ath12k_dp_free during core destroy.
730 			 * Before dp_free, the irqs would be disabled and would wait to
731 			 * synchronize. Hence there wouldn’t be any race against add or
732 			 * delete to this list. Hence unlock-lock is safe here.
733 			 */
734 			spin_unlock_bh(&dp->reo_cmd_lock);
735 
736 			ath12k_dp_reo_cache_flush(ab, &elem->data);
737 			kfree(elem);
738 			spin_lock_bh(&dp->reo_cmd_lock);
739 		}
740 	}
741 	spin_unlock_bh(&dp->reo_cmd_lock);
742 
743 	return;
744 free_desc:
745 	dma_unmap_single(ab->dev, rx_tid->paddr, rx_tid->size,
746 			 DMA_BIDIRECTIONAL);
747 	kfree(rx_tid->vaddr);
748 	rx_tid->vaddr = NULL;
749 }
750 
751 static void ath12k_peer_rx_tid_qref_setup(struct ath12k_base *ab, u16 peer_id, u16 tid,
752 					  dma_addr_t paddr)
753 {
754 	struct ath12k_reo_queue_ref *qref;
755 	struct ath12k_dp *dp = &ab->dp;
756 
757 	if (!ab->hw_params->reoq_lut_support)
758 		return;
759 
760 	/* TODO: based on ML peer or not, select the LUT. below assumes non
761 	 * ML peer
762 	 */
763 	qref = (struct ath12k_reo_queue_ref *)dp->reoq_lut.vaddr +
764 			(peer_id * (IEEE80211_NUM_TIDS + 1) + tid);
765 
766 	qref->info0 = u32_encode_bits(lower_32_bits(paddr),
767 				      BUFFER_ADDR_INFO0_ADDR);
768 	qref->info1 = u32_encode_bits(upper_32_bits(paddr),
769 				      BUFFER_ADDR_INFO1_ADDR) |
770 		      u32_encode_bits(tid, DP_REO_QREF_NUM);
771 }
772 
773 static void ath12k_peer_rx_tid_qref_reset(struct ath12k_base *ab, u16 peer_id, u16 tid)
774 {
775 	struct ath12k_reo_queue_ref *qref;
776 	struct ath12k_dp *dp = &ab->dp;
777 
778 	if (!ab->hw_params->reoq_lut_support)
779 		return;
780 
781 	/* TODO: based on ML peer or not, select the LUT. below assumes non
782 	 * ML peer
783 	 */
784 	qref = (struct ath12k_reo_queue_ref *)dp->reoq_lut.vaddr +
785 			(peer_id * (IEEE80211_NUM_TIDS + 1) + tid);
786 
787 	qref->info0 = u32_encode_bits(0, BUFFER_ADDR_INFO0_ADDR);
788 	qref->info1 = u32_encode_bits(0, BUFFER_ADDR_INFO1_ADDR) |
789 		      u32_encode_bits(tid, DP_REO_QREF_NUM);
790 }
791 
792 void ath12k_dp_rx_peer_tid_delete(struct ath12k *ar,
793 				  struct ath12k_peer *peer, u8 tid)
794 {
795 	struct ath12k_hal_reo_cmd cmd = {0};
796 	struct ath12k_dp_rx_tid *rx_tid = &peer->rx_tid[tid];
797 	int ret;
798 
799 	if (!rx_tid->active)
800 		return;
801 
802 	cmd.flag = HAL_REO_CMD_FLG_NEED_STATUS;
803 	cmd.addr_lo = lower_32_bits(rx_tid->paddr);
804 	cmd.addr_hi = upper_32_bits(rx_tid->paddr);
805 	cmd.upd0 = HAL_REO_CMD_UPD0_VLD;
806 	ret = ath12k_dp_reo_cmd_send(ar->ab, rx_tid,
807 				     HAL_REO_CMD_UPDATE_RX_QUEUE, &cmd,
808 				     ath12k_dp_rx_tid_del_func);
809 	if (ret) {
810 		ath12k_err(ar->ab, "failed to send HAL_REO_CMD_UPDATE_RX_QUEUE cmd, tid %d (%d)\n",
811 			   tid, ret);
812 		dma_unmap_single(ar->ab->dev, rx_tid->paddr, rx_tid->size,
813 				 DMA_BIDIRECTIONAL);
814 		kfree(rx_tid->vaddr);
815 		rx_tid->vaddr = NULL;
816 	}
817 
818 	ath12k_peer_rx_tid_qref_reset(ar->ab, peer->peer_id, tid);
819 
820 	rx_tid->active = false;
821 }
822 
823 /* TODO: it's strange (and ugly) that struct hal_reo_dest_ring is converted
824  * to struct hal_wbm_release_ring, I couldn't figure out the logic behind
825  * that.
826  */
827 static int ath12k_dp_rx_link_desc_return(struct ath12k_base *ab,
828 					 struct hal_reo_dest_ring *ring,
829 					 enum hal_wbm_rel_bm_act action)
830 {
831 	struct hal_wbm_release_ring *link_desc = (struct hal_wbm_release_ring *)ring;
832 	struct hal_wbm_release_ring *desc;
833 	struct ath12k_dp *dp = &ab->dp;
834 	struct hal_srng *srng;
835 	int ret = 0;
836 
837 	srng = &ab->hal.srng_list[dp->wbm_desc_rel_ring.ring_id];
838 
839 	spin_lock_bh(&srng->lock);
840 
841 	ath12k_hal_srng_access_begin(ab, srng);
842 
843 	desc = ath12k_hal_srng_src_get_next_entry(ab, srng);
844 	if (!desc) {
845 		ret = -ENOBUFS;
846 		goto exit;
847 	}
848 
849 	ath12k_hal_rx_msdu_link_desc_set(ab, desc, link_desc, action);
850 
851 exit:
852 	ath12k_hal_srng_access_end(ab, srng);
853 
854 	spin_unlock_bh(&srng->lock);
855 
856 	return ret;
857 }
858 
859 static void ath12k_dp_rx_frags_cleanup(struct ath12k_dp_rx_tid *rx_tid,
860 				       bool rel_link_desc)
861 {
862 	struct ath12k_base *ab = rx_tid->ab;
863 
864 	lockdep_assert_held(&ab->base_lock);
865 
866 	if (rx_tid->dst_ring_desc) {
867 		if (rel_link_desc)
868 			ath12k_dp_rx_link_desc_return(ab, rx_tid->dst_ring_desc,
869 						      HAL_WBM_REL_BM_ACT_PUT_IN_IDLE);
870 		kfree(rx_tid->dst_ring_desc);
871 		rx_tid->dst_ring_desc = NULL;
872 	}
873 
874 	rx_tid->cur_sn = 0;
875 	rx_tid->last_frag_no = 0;
876 	rx_tid->rx_frag_bitmap = 0;
877 	__skb_queue_purge(&rx_tid->rx_frags);
878 }
879 
880 void ath12k_dp_rx_peer_tid_cleanup(struct ath12k *ar, struct ath12k_peer *peer)
881 {
882 	struct ath12k_dp_rx_tid *rx_tid;
883 	int i;
884 
885 	lockdep_assert_held(&ar->ab->base_lock);
886 
887 	for (i = 0; i <= IEEE80211_NUM_TIDS; i++) {
888 		rx_tid = &peer->rx_tid[i];
889 
890 		ath12k_dp_rx_peer_tid_delete(ar, peer, i);
891 		ath12k_dp_rx_frags_cleanup(rx_tid, true);
892 
893 		spin_unlock_bh(&ar->ab->base_lock);
894 		del_timer_sync(&rx_tid->frag_timer);
895 		spin_lock_bh(&ar->ab->base_lock);
896 	}
897 }
898 
899 static int ath12k_peer_rx_tid_reo_update(struct ath12k *ar,
900 					 struct ath12k_peer *peer,
901 					 struct ath12k_dp_rx_tid *rx_tid,
902 					 u32 ba_win_sz, u16 ssn,
903 					 bool update_ssn)
904 {
905 	struct ath12k_hal_reo_cmd cmd = {0};
906 	int ret;
907 
908 	cmd.addr_lo = lower_32_bits(rx_tid->paddr);
909 	cmd.addr_hi = upper_32_bits(rx_tid->paddr);
910 	cmd.flag = HAL_REO_CMD_FLG_NEED_STATUS;
911 	cmd.upd0 = HAL_REO_CMD_UPD0_BA_WINDOW_SIZE;
912 	cmd.ba_window_size = ba_win_sz;
913 
914 	if (update_ssn) {
915 		cmd.upd0 |= HAL_REO_CMD_UPD0_SSN;
916 		cmd.upd2 = u32_encode_bits(ssn, HAL_REO_CMD_UPD2_SSN);
917 	}
918 
919 	ret = ath12k_dp_reo_cmd_send(ar->ab, rx_tid,
920 				     HAL_REO_CMD_UPDATE_RX_QUEUE, &cmd,
921 				     NULL);
922 	if (ret) {
923 		ath12k_warn(ar->ab, "failed to update rx tid queue, tid %d (%d)\n",
924 			    rx_tid->tid, ret);
925 		return ret;
926 	}
927 
928 	rx_tid->ba_win_sz = ba_win_sz;
929 
930 	return 0;
931 }
932 
933 int ath12k_dp_rx_peer_tid_setup(struct ath12k *ar, const u8 *peer_mac, int vdev_id,
934 				u8 tid, u32 ba_win_sz, u16 ssn,
935 				enum hal_pn_type pn_type)
936 {
937 	struct ath12k_base *ab = ar->ab;
938 	struct ath12k_dp *dp = &ab->dp;
939 	struct hal_rx_reo_queue *addr_aligned;
940 	struct ath12k_peer *peer;
941 	struct ath12k_dp_rx_tid *rx_tid;
942 	u32 hw_desc_sz;
943 	void *vaddr;
944 	dma_addr_t paddr;
945 	int ret;
946 
947 	spin_lock_bh(&ab->base_lock);
948 
949 	peer = ath12k_peer_find(ab, vdev_id, peer_mac);
950 	if (!peer) {
951 		spin_unlock_bh(&ab->base_lock);
952 		ath12k_warn(ab, "failed to find the peer to set up rx tid\n");
953 		return -ENOENT;
954 	}
955 
956 	if (ab->hw_params->reoq_lut_support && !dp->reoq_lut.vaddr) {
957 		spin_unlock_bh(&ab->base_lock);
958 		ath12k_warn(ab, "reo qref table is not setup\n");
959 		return -EINVAL;
960 	}
961 
962 	if (peer->peer_id > DP_MAX_PEER_ID || tid > IEEE80211_NUM_TIDS) {
963 		ath12k_warn(ab, "peer id of peer %d or tid %d doesn't allow reoq setup\n",
964 			    peer->peer_id, tid);
965 		spin_unlock_bh(&ab->base_lock);
966 		return -EINVAL;
967 	}
968 
969 	rx_tid = &peer->rx_tid[tid];
970 	/* Update the tid queue if it is already setup */
971 	if (rx_tid->active) {
972 		paddr = rx_tid->paddr;
973 		ret = ath12k_peer_rx_tid_reo_update(ar, peer, rx_tid,
974 						    ba_win_sz, ssn, true);
975 		spin_unlock_bh(&ab->base_lock);
976 		if (ret) {
977 			ath12k_warn(ab, "failed to update reo for rx tid %d\n", tid);
978 			return ret;
979 		}
980 
981 		if (!ab->hw_params->reoq_lut_support) {
982 			ret = ath12k_wmi_peer_rx_reorder_queue_setup(ar, vdev_id,
983 								     peer_mac,
984 								     paddr, tid, 1,
985 								     ba_win_sz);
986 			if (ret) {
987 				ath12k_warn(ab, "failed to setup peer rx reorder queuefor tid %d: %d\n",
988 					    tid, ret);
989 				return ret;
990 			}
991 		}
992 
993 		return 0;
994 	}
995 
996 	rx_tid->tid = tid;
997 
998 	rx_tid->ba_win_sz = ba_win_sz;
999 
1000 	/* TODO: Optimize the memory allocation for qos tid based on
1001 	 * the actual BA window size in REO tid update path.
1002 	 */
1003 	if (tid == HAL_DESC_REO_NON_QOS_TID)
1004 		hw_desc_sz = ath12k_hal_reo_qdesc_size(ba_win_sz, tid);
1005 	else
1006 		hw_desc_sz = ath12k_hal_reo_qdesc_size(DP_BA_WIN_SZ_MAX, tid);
1007 
1008 	vaddr = kzalloc(hw_desc_sz + HAL_LINK_DESC_ALIGN - 1, GFP_ATOMIC);
1009 	if (!vaddr) {
1010 		spin_unlock_bh(&ab->base_lock);
1011 		return -ENOMEM;
1012 	}
1013 
1014 	addr_aligned = PTR_ALIGN(vaddr, HAL_LINK_DESC_ALIGN);
1015 
1016 	ath12k_hal_reo_qdesc_setup(addr_aligned, tid, ba_win_sz,
1017 				   ssn, pn_type);
1018 
1019 	paddr = dma_map_single(ab->dev, addr_aligned, hw_desc_sz,
1020 			       DMA_BIDIRECTIONAL);
1021 
1022 	ret = dma_mapping_error(ab->dev, paddr);
1023 	if (ret) {
1024 		spin_unlock_bh(&ab->base_lock);
1025 		goto err_mem_free;
1026 	}
1027 
1028 	rx_tid->vaddr = vaddr;
1029 	rx_tid->paddr = paddr;
1030 	rx_tid->size = hw_desc_sz;
1031 	rx_tid->active = true;
1032 
1033 	if (ab->hw_params->reoq_lut_support) {
1034 		/* Update the REO queue LUT at the corresponding peer id
1035 		 * and tid with qaddr.
1036 		 */
1037 		ath12k_peer_rx_tid_qref_setup(ab, peer->peer_id, tid, paddr);
1038 		spin_unlock_bh(&ab->base_lock);
1039 	} else {
1040 		spin_unlock_bh(&ab->base_lock);
1041 		ret = ath12k_wmi_peer_rx_reorder_queue_setup(ar, vdev_id, peer_mac,
1042 							     paddr, tid, 1, ba_win_sz);
1043 	}
1044 
1045 	return ret;
1046 
1047 err_mem_free:
1048 	kfree(vaddr);
1049 
1050 	return ret;
1051 }
1052 
1053 int ath12k_dp_rx_ampdu_start(struct ath12k *ar,
1054 			     struct ieee80211_ampdu_params *params)
1055 {
1056 	struct ath12k_base *ab = ar->ab;
1057 	struct ath12k_sta *arsta = (void *)params->sta->drv_priv;
1058 	int vdev_id = arsta->arvif->vdev_id;
1059 	int ret;
1060 
1061 	ret = ath12k_dp_rx_peer_tid_setup(ar, params->sta->addr, vdev_id,
1062 					  params->tid, params->buf_size,
1063 					  params->ssn, arsta->pn_type);
1064 	if (ret)
1065 		ath12k_warn(ab, "failed to setup rx tid %d\n", ret);
1066 
1067 	return ret;
1068 }
1069 
1070 int ath12k_dp_rx_ampdu_stop(struct ath12k *ar,
1071 			    struct ieee80211_ampdu_params *params)
1072 {
1073 	struct ath12k_base *ab = ar->ab;
1074 	struct ath12k_peer *peer;
1075 	struct ath12k_sta *arsta = (void *)params->sta->drv_priv;
1076 	int vdev_id = arsta->arvif->vdev_id;
1077 	bool active;
1078 	int ret;
1079 
1080 	spin_lock_bh(&ab->base_lock);
1081 
1082 	peer = ath12k_peer_find(ab, vdev_id, params->sta->addr);
1083 	if (!peer) {
1084 		spin_unlock_bh(&ab->base_lock);
1085 		ath12k_warn(ab, "failed to find the peer to stop rx aggregation\n");
1086 		return -ENOENT;
1087 	}
1088 
1089 	active = peer->rx_tid[params->tid].active;
1090 
1091 	if (!active) {
1092 		spin_unlock_bh(&ab->base_lock);
1093 		return 0;
1094 	}
1095 
1096 	ret = ath12k_peer_rx_tid_reo_update(ar, peer, peer->rx_tid, 1, 0, false);
1097 	spin_unlock_bh(&ab->base_lock);
1098 	if (ret) {
1099 		ath12k_warn(ab, "failed to update reo for rx tid %d: %d\n",
1100 			    params->tid, ret);
1101 		return ret;
1102 	}
1103 
1104 	return ret;
1105 }
1106 
1107 int ath12k_dp_rx_peer_pn_replay_config(struct ath12k_vif *arvif,
1108 				       const u8 *peer_addr,
1109 				       enum set_key_cmd key_cmd,
1110 				       struct ieee80211_key_conf *key)
1111 {
1112 	struct ath12k *ar = arvif->ar;
1113 	struct ath12k_base *ab = ar->ab;
1114 	struct ath12k_hal_reo_cmd cmd = {0};
1115 	struct ath12k_peer *peer;
1116 	struct ath12k_dp_rx_tid *rx_tid;
1117 	u8 tid;
1118 	int ret = 0;
1119 
1120 	/* NOTE: Enable PN/TSC replay check offload only for unicast frames.
1121 	 * We use mac80211 PN/TSC replay check functionality for bcast/mcast
1122 	 * for now.
1123 	 */
1124 	if (!(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
1125 		return 0;
1126 
1127 	cmd.flag = HAL_REO_CMD_FLG_NEED_STATUS;
1128 	cmd.upd0 = HAL_REO_CMD_UPD0_PN |
1129 		    HAL_REO_CMD_UPD0_PN_SIZE |
1130 		    HAL_REO_CMD_UPD0_PN_VALID |
1131 		    HAL_REO_CMD_UPD0_PN_CHECK |
1132 		    HAL_REO_CMD_UPD0_SVLD;
1133 
1134 	switch (key->cipher) {
1135 	case WLAN_CIPHER_SUITE_TKIP:
1136 	case WLAN_CIPHER_SUITE_CCMP:
1137 	case WLAN_CIPHER_SUITE_CCMP_256:
1138 	case WLAN_CIPHER_SUITE_GCMP:
1139 	case WLAN_CIPHER_SUITE_GCMP_256:
1140 		if (key_cmd == SET_KEY) {
1141 			cmd.upd1 |= HAL_REO_CMD_UPD1_PN_CHECK;
1142 			cmd.pn_size = 48;
1143 		}
1144 		break;
1145 	default:
1146 		break;
1147 	}
1148 
1149 	spin_lock_bh(&ab->base_lock);
1150 
1151 	peer = ath12k_peer_find(ab, arvif->vdev_id, peer_addr);
1152 	if (!peer) {
1153 		spin_unlock_bh(&ab->base_lock);
1154 		ath12k_warn(ab, "failed to find the peer %pM to configure pn replay detection\n",
1155 			    peer_addr);
1156 		return -ENOENT;
1157 	}
1158 
1159 	for (tid = 0; tid <= IEEE80211_NUM_TIDS; tid++) {
1160 		rx_tid = &peer->rx_tid[tid];
1161 		if (!rx_tid->active)
1162 			continue;
1163 		cmd.addr_lo = lower_32_bits(rx_tid->paddr);
1164 		cmd.addr_hi = upper_32_bits(rx_tid->paddr);
1165 		ret = ath12k_dp_reo_cmd_send(ab, rx_tid,
1166 					     HAL_REO_CMD_UPDATE_RX_QUEUE,
1167 					     &cmd, NULL);
1168 		if (ret) {
1169 			ath12k_warn(ab, "failed to configure rx tid %d queue of peer %pM for pn replay detection %d\n",
1170 				    tid, peer_addr, ret);
1171 			break;
1172 		}
1173 	}
1174 
1175 	spin_unlock_bh(&ab->base_lock);
1176 
1177 	return ret;
1178 }
1179 
1180 static int ath12k_get_ppdu_user_index(struct htt_ppdu_stats *ppdu_stats,
1181 				      u16 peer_id)
1182 {
1183 	int i;
1184 
1185 	for (i = 0; i < HTT_PPDU_STATS_MAX_USERS - 1; i++) {
1186 		if (ppdu_stats->user_stats[i].is_valid_peer_id) {
1187 			if (peer_id == ppdu_stats->user_stats[i].peer_id)
1188 				return i;
1189 		} else {
1190 			return i;
1191 		}
1192 	}
1193 
1194 	return -EINVAL;
1195 }
1196 
1197 static int ath12k_htt_tlv_ppdu_stats_parse(struct ath12k_base *ab,
1198 					   u16 tag, u16 len, const void *ptr,
1199 					   void *data)
1200 {
1201 	const struct htt_ppdu_stats_usr_cmpltn_ack_ba_status *ba_status;
1202 	const struct htt_ppdu_stats_usr_cmpltn_cmn *cmplt_cmn;
1203 	const struct htt_ppdu_stats_user_rate *user_rate;
1204 	struct htt_ppdu_stats_info *ppdu_info;
1205 	struct htt_ppdu_user_stats *user_stats;
1206 	int cur_user;
1207 	u16 peer_id;
1208 
1209 	ppdu_info = data;
1210 
1211 	switch (tag) {
1212 	case HTT_PPDU_STATS_TAG_COMMON:
1213 		if (len < sizeof(struct htt_ppdu_stats_common)) {
1214 			ath12k_warn(ab, "Invalid len %d for the tag 0x%x\n",
1215 				    len, tag);
1216 			return -EINVAL;
1217 		}
1218 		memcpy(&ppdu_info->ppdu_stats.common, ptr,
1219 		       sizeof(struct htt_ppdu_stats_common));
1220 		break;
1221 	case HTT_PPDU_STATS_TAG_USR_RATE:
1222 		if (len < sizeof(struct htt_ppdu_stats_user_rate)) {
1223 			ath12k_warn(ab, "Invalid len %d for the tag 0x%x\n",
1224 				    len, tag);
1225 			return -EINVAL;
1226 		}
1227 		user_rate = ptr;
1228 		peer_id = le16_to_cpu(user_rate->sw_peer_id);
1229 		cur_user = ath12k_get_ppdu_user_index(&ppdu_info->ppdu_stats,
1230 						      peer_id);
1231 		if (cur_user < 0)
1232 			return -EINVAL;
1233 		user_stats = &ppdu_info->ppdu_stats.user_stats[cur_user];
1234 		user_stats->peer_id = peer_id;
1235 		user_stats->is_valid_peer_id = true;
1236 		memcpy(&user_stats->rate, ptr,
1237 		       sizeof(struct htt_ppdu_stats_user_rate));
1238 		user_stats->tlv_flags |= BIT(tag);
1239 		break;
1240 	case HTT_PPDU_STATS_TAG_USR_COMPLTN_COMMON:
1241 		if (len < sizeof(struct htt_ppdu_stats_usr_cmpltn_cmn)) {
1242 			ath12k_warn(ab, "Invalid len %d for the tag 0x%x\n",
1243 				    len, tag);
1244 			return -EINVAL;
1245 		}
1246 
1247 		cmplt_cmn = ptr;
1248 		peer_id = le16_to_cpu(cmplt_cmn->sw_peer_id);
1249 		cur_user = ath12k_get_ppdu_user_index(&ppdu_info->ppdu_stats,
1250 						      peer_id);
1251 		if (cur_user < 0)
1252 			return -EINVAL;
1253 		user_stats = &ppdu_info->ppdu_stats.user_stats[cur_user];
1254 		user_stats->peer_id = peer_id;
1255 		user_stats->is_valid_peer_id = true;
1256 		memcpy(&user_stats->cmpltn_cmn, ptr,
1257 		       sizeof(struct htt_ppdu_stats_usr_cmpltn_cmn));
1258 		user_stats->tlv_flags |= BIT(tag);
1259 		break;
1260 	case HTT_PPDU_STATS_TAG_USR_COMPLTN_ACK_BA_STATUS:
1261 		if (len <
1262 		    sizeof(struct htt_ppdu_stats_usr_cmpltn_ack_ba_status)) {
1263 			ath12k_warn(ab, "Invalid len %d for the tag 0x%x\n",
1264 				    len, tag);
1265 			return -EINVAL;
1266 		}
1267 
1268 		ba_status = ptr;
1269 		peer_id = le16_to_cpu(ba_status->sw_peer_id);
1270 		cur_user = ath12k_get_ppdu_user_index(&ppdu_info->ppdu_stats,
1271 						      peer_id);
1272 		if (cur_user < 0)
1273 			return -EINVAL;
1274 		user_stats = &ppdu_info->ppdu_stats.user_stats[cur_user];
1275 		user_stats->peer_id = peer_id;
1276 		user_stats->is_valid_peer_id = true;
1277 		memcpy(&user_stats->ack_ba, ptr,
1278 		       sizeof(struct htt_ppdu_stats_usr_cmpltn_ack_ba_status));
1279 		user_stats->tlv_flags |= BIT(tag);
1280 		break;
1281 	}
1282 	return 0;
1283 }
1284 
1285 static int ath12k_dp_htt_tlv_iter(struct ath12k_base *ab, const void *ptr, size_t len,
1286 				  int (*iter)(struct ath12k_base *ar, u16 tag, u16 len,
1287 					      const void *ptr, void *data),
1288 				  void *data)
1289 {
1290 	const struct htt_tlv *tlv;
1291 	const void *begin = ptr;
1292 	u16 tlv_tag, tlv_len;
1293 	int ret = -EINVAL;
1294 
1295 	while (len > 0) {
1296 		if (len < sizeof(*tlv)) {
1297 			ath12k_err(ab, "htt tlv parse failure at byte %zd (%zu bytes left, %zu expected)\n",
1298 				   ptr - begin, len, sizeof(*tlv));
1299 			return -EINVAL;
1300 		}
1301 		tlv = (struct htt_tlv *)ptr;
1302 		tlv_tag = le32_get_bits(tlv->header, HTT_TLV_TAG);
1303 		tlv_len = le32_get_bits(tlv->header, HTT_TLV_LEN);
1304 		ptr += sizeof(*tlv);
1305 		len -= sizeof(*tlv);
1306 
1307 		if (tlv_len > len) {
1308 			ath12k_err(ab, "htt tlv parse failure of tag %u at byte %zd (%zu bytes left, %u expected)\n",
1309 				   tlv_tag, ptr - begin, len, tlv_len);
1310 			return -EINVAL;
1311 		}
1312 		ret = iter(ab, tlv_tag, tlv_len, ptr, data);
1313 		if (ret == -ENOMEM)
1314 			return ret;
1315 
1316 		ptr += tlv_len;
1317 		len -= tlv_len;
1318 	}
1319 	return 0;
1320 }
1321 
1322 static void
1323 ath12k_update_per_peer_tx_stats(struct ath12k *ar,
1324 				struct htt_ppdu_stats *ppdu_stats, u8 user)
1325 {
1326 	struct ath12k_base *ab = ar->ab;
1327 	struct ath12k_peer *peer;
1328 	struct ieee80211_sta *sta;
1329 	struct ath12k_sta *arsta;
1330 	struct htt_ppdu_stats_user_rate *user_rate;
1331 	struct ath12k_per_peer_tx_stats *peer_stats = &ar->peer_tx_stats;
1332 	struct htt_ppdu_user_stats *usr_stats = &ppdu_stats->user_stats[user];
1333 	struct htt_ppdu_stats_common *common = &ppdu_stats->common;
1334 	int ret;
1335 	u8 flags, mcs, nss, bw, sgi, dcm, rate_idx = 0;
1336 	u32 v, succ_bytes = 0;
1337 	u16 tones, rate = 0, succ_pkts = 0;
1338 	u32 tx_duration = 0;
1339 	u8 tid = HTT_PPDU_STATS_NON_QOS_TID;
1340 	bool is_ampdu = false;
1341 
1342 	if (!usr_stats)
1343 		return;
1344 
1345 	if (!(usr_stats->tlv_flags & BIT(HTT_PPDU_STATS_TAG_USR_RATE)))
1346 		return;
1347 
1348 	if (usr_stats->tlv_flags & BIT(HTT_PPDU_STATS_TAG_USR_COMPLTN_COMMON))
1349 		is_ampdu =
1350 			HTT_USR_CMPLTN_IS_AMPDU(usr_stats->cmpltn_cmn.flags);
1351 
1352 	if (usr_stats->tlv_flags &
1353 	    BIT(HTT_PPDU_STATS_TAG_USR_COMPLTN_ACK_BA_STATUS)) {
1354 		succ_bytes = le32_to_cpu(usr_stats->ack_ba.success_bytes);
1355 		succ_pkts = le32_get_bits(usr_stats->ack_ba.info,
1356 					  HTT_PPDU_STATS_ACK_BA_INFO_NUM_MSDU_M);
1357 		tid = le32_get_bits(usr_stats->ack_ba.info,
1358 				    HTT_PPDU_STATS_ACK_BA_INFO_TID_NUM);
1359 	}
1360 
1361 	if (common->fes_duration_us)
1362 		tx_duration = le32_to_cpu(common->fes_duration_us);
1363 
1364 	user_rate = &usr_stats->rate;
1365 	flags = HTT_USR_RATE_PREAMBLE(user_rate->rate_flags);
1366 	bw = HTT_USR_RATE_BW(user_rate->rate_flags) - 2;
1367 	nss = HTT_USR_RATE_NSS(user_rate->rate_flags) + 1;
1368 	mcs = HTT_USR_RATE_MCS(user_rate->rate_flags);
1369 	sgi = HTT_USR_RATE_GI(user_rate->rate_flags);
1370 	dcm = HTT_USR_RATE_DCM(user_rate->rate_flags);
1371 
1372 	/* Note: If host configured fixed rates and in some other special
1373 	 * cases, the broadcast/management frames are sent in different rates.
1374 	 * Firmware rate's control to be skipped for this?
1375 	 */
1376 
1377 	if (flags == WMI_RATE_PREAMBLE_HE && mcs > ATH12K_HE_MCS_MAX) {
1378 		ath12k_warn(ab, "Invalid HE mcs %d peer stats",  mcs);
1379 		return;
1380 	}
1381 
1382 	if (flags == WMI_RATE_PREAMBLE_VHT && mcs > ATH12K_VHT_MCS_MAX) {
1383 		ath12k_warn(ab, "Invalid VHT mcs %d peer stats",  mcs);
1384 		return;
1385 	}
1386 
1387 	if (flags == WMI_RATE_PREAMBLE_HT && (mcs > ATH12K_HT_MCS_MAX || nss < 1)) {
1388 		ath12k_warn(ab, "Invalid HT mcs %d nss %d peer stats",
1389 			    mcs, nss);
1390 		return;
1391 	}
1392 
1393 	if (flags == WMI_RATE_PREAMBLE_CCK || flags == WMI_RATE_PREAMBLE_OFDM) {
1394 		ret = ath12k_mac_hw_ratecode_to_legacy_rate(mcs,
1395 							    flags,
1396 							    &rate_idx,
1397 							    &rate);
1398 		if (ret < 0)
1399 			return;
1400 	}
1401 
1402 	rcu_read_lock();
1403 	spin_lock_bh(&ab->base_lock);
1404 	peer = ath12k_peer_find_by_id(ab, usr_stats->peer_id);
1405 
1406 	if (!peer || !peer->sta) {
1407 		spin_unlock_bh(&ab->base_lock);
1408 		rcu_read_unlock();
1409 		return;
1410 	}
1411 
1412 	sta = peer->sta;
1413 	arsta = (struct ath12k_sta *)sta->drv_priv;
1414 
1415 	memset(&arsta->txrate, 0, sizeof(arsta->txrate));
1416 
1417 	switch (flags) {
1418 	case WMI_RATE_PREAMBLE_OFDM:
1419 		arsta->txrate.legacy = rate;
1420 		break;
1421 	case WMI_RATE_PREAMBLE_CCK:
1422 		arsta->txrate.legacy = rate;
1423 		break;
1424 	case WMI_RATE_PREAMBLE_HT:
1425 		arsta->txrate.mcs = mcs + 8 * (nss - 1);
1426 		arsta->txrate.flags = RATE_INFO_FLAGS_MCS;
1427 		if (sgi)
1428 			arsta->txrate.flags |= RATE_INFO_FLAGS_SHORT_GI;
1429 		break;
1430 	case WMI_RATE_PREAMBLE_VHT:
1431 		arsta->txrate.mcs = mcs;
1432 		arsta->txrate.flags = RATE_INFO_FLAGS_VHT_MCS;
1433 		if (sgi)
1434 			arsta->txrate.flags |= RATE_INFO_FLAGS_SHORT_GI;
1435 		break;
1436 	case WMI_RATE_PREAMBLE_HE:
1437 		arsta->txrate.mcs = mcs;
1438 		arsta->txrate.flags = RATE_INFO_FLAGS_HE_MCS;
1439 		arsta->txrate.he_dcm = dcm;
1440 		arsta->txrate.he_gi = ath12k_he_gi_to_nl80211_he_gi(sgi);
1441 		tones = le16_to_cpu(user_rate->ru_end) -
1442 			le16_to_cpu(user_rate->ru_start) + 1;
1443 		v = ath12k_he_ru_tones_to_nl80211_he_ru_alloc(tones);
1444 		arsta->txrate.he_ru_alloc = v;
1445 		break;
1446 	}
1447 
1448 	arsta->txrate.nss = nss;
1449 	arsta->txrate.bw = ath12k_mac_bw_to_mac80211_bw(bw);
1450 	arsta->tx_duration += tx_duration;
1451 	memcpy(&arsta->last_txrate, &arsta->txrate, sizeof(struct rate_info));
1452 
1453 	/* PPDU stats reported for mgmt packet doesn't have valid tx bytes.
1454 	 * So skip peer stats update for mgmt packets.
1455 	 */
1456 	if (tid < HTT_PPDU_STATS_NON_QOS_TID) {
1457 		memset(peer_stats, 0, sizeof(*peer_stats));
1458 		peer_stats->succ_pkts = succ_pkts;
1459 		peer_stats->succ_bytes = succ_bytes;
1460 		peer_stats->is_ampdu = is_ampdu;
1461 		peer_stats->duration = tx_duration;
1462 		peer_stats->ba_fails =
1463 			HTT_USR_CMPLTN_LONG_RETRY(usr_stats->cmpltn_cmn.flags) +
1464 			HTT_USR_CMPLTN_SHORT_RETRY(usr_stats->cmpltn_cmn.flags);
1465 	}
1466 
1467 	spin_unlock_bh(&ab->base_lock);
1468 	rcu_read_unlock();
1469 }
1470 
1471 static void ath12k_htt_update_ppdu_stats(struct ath12k *ar,
1472 					 struct htt_ppdu_stats *ppdu_stats)
1473 {
1474 	u8 user;
1475 
1476 	for (user = 0; user < HTT_PPDU_STATS_MAX_USERS - 1; user++)
1477 		ath12k_update_per_peer_tx_stats(ar, ppdu_stats, user);
1478 }
1479 
1480 static
1481 struct htt_ppdu_stats_info *ath12k_dp_htt_get_ppdu_desc(struct ath12k *ar,
1482 							u32 ppdu_id)
1483 {
1484 	struct htt_ppdu_stats_info *ppdu_info;
1485 
1486 	lockdep_assert_held(&ar->data_lock);
1487 	if (!list_empty(&ar->ppdu_stats_info)) {
1488 		list_for_each_entry(ppdu_info, &ar->ppdu_stats_info, list) {
1489 			if (ppdu_info->ppdu_id == ppdu_id)
1490 				return ppdu_info;
1491 		}
1492 
1493 		if (ar->ppdu_stat_list_depth > HTT_PPDU_DESC_MAX_DEPTH) {
1494 			ppdu_info = list_first_entry(&ar->ppdu_stats_info,
1495 						     typeof(*ppdu_info), list);
1496 			list_del(&ppdu_info->list);
1497 			ar->ppdu_stat_list_depth--;
1498 			ath12k_htt_update_ppdu_stats(ar, &ppdu_info->ppdu_stats);
1499 			kfree(ppdu_info);
1500 		}
1501 	}
1502 
1503 	ppdu_info = kzalloc(sizeof(*ppdu_info), GFP_ATOMIC);
1504 	if (!ppdu_info)
1505 		return NULL;
1506 
1507 	list_add_tail(&ppdu_info->list, &ar->ppdu_stats_info);
1508 	ar->ppdu_stat_list_depth++;
1509 
1510 	return ppdu_info;
1511 }
1512 
1513 static void ath12k_copy_to_delay_stats(struct ath12k_peer *peer,
1514 				       struct htt_ppdu_user_stats *usr_stats)
1515 {
1516 	peer->ppdu_stats_delayba.sw_peer_id = le16_to_cpu(usr_stats->rate.sw_peer_id);
1517 	peer->ppdu_stats_delayba.info0 = le32_to_cpu(usr_stats->rate.info0);
1518 	peer->ppdu_stats_delayba.ru_end = le16_to_cpu(usr_stats->rate.ru_end);
1519 	peer->ppdu_stats_delayba.ru_start = le16_to_cpu(usr_stats->rate.ru_start);
1520 	peer->ppdu_stats_delayba.info1 = le32_to_cpu(usr_stats->rate.info1);
1521 	peer->ppdu_stats_delayba.rate_flags = le32_to_cpu(usr_stats->rate.rate_flags);
1522 	peer->ppdu_stats_delayba.resp_rate_flags =
1523 		le32_to_cpu(usr_stats->rate.resp_rate_flags);
1524 
1525 	peer->delayba_flag = true;
1526 }
1527 
1528 static void ath12k_copy_to_bar(struct ath12k_peer *peer,
1529 			       struct htt_ppdu_user_stats *usr_stats)
1530 {
1531 	usr_stats->rate.sw_peer_id = cpu_to_le16(peer->ppdu_stats_delayba.sw_peer_id);
1532 	usr_stats->rate.info0 = cpu_to_le32(peer->ppdu_stats_delayba.info0);
1533 	usr_stats->rate.ru_end = cpu_to_le16(peer->ppdu_stats_delayba.ru_end);
1534 	usr_stats->rate.ru_start = cpu_to_le16(peer->ppdu_stats_delayba.ru_start);
1535 	usr_stats->rate.info1 = cpu_to_le32(peer->ppdu_stats_delayba.info1);
1536 	usr_stats->rate.rate_flags = cpu_to_le32(peer->ppdu_stats_delayba.rate_flags);
1537 	usr_stats->rate.resp_rate_flags =
1538 		cpu_to_le32(peer->ppdu_stats_delayba.resp_rate_flags);
1539 
1540 	peer->delayba_flag = false;
1541 }
1542 
1543 static int ath12k_htt_pull_ppdu_stats(struct ath12k_base *ab,
1544 				      struct sk_buff *skb)
1545 {
1546 	struct ath12k_htt_ppdu_stats_msg *msg;
1547 	struct htt_ppdu_stats_info *ppdu_info;
1548 	struct ath12k_peer *peer = NULL;
1549 	struct htt_ppdu_user_stats *usr_stats = NULL;
1550 	u32 peer_id = 0;
1551 	struct ath12k *ar;
1552 	int ret, i;
1553 	u8 pdev_id;
1554 	u32 ppdu_id, len;
1555 
1556 	msg = (struct ath12k_htt_ppdu_stats_msg *)skb->data;
1557 	len = le32_get_bits(msg->info, HTT_T2H_PPDU_STATS_INFO_PAYLOAD_SIZE);
1558 	if (len > (skb->len - struct_size(msg, data, 0))) {
1559 		ath12k_warn(ab,
1560 			    "HTT PPDU STATS event has unexpected payload size %u, should be smaller than %u\n",
1561 			    len, skb->len);
1562 		return -EINVAL;
1563 	}
1564 
1565 	pdev_id = le32_get_bits(msg->info, HTT_T2H_PPDU_STATS_INFO_PDEV_ID);
1566 	ppdu_id = le32_to_cpu(msg->ppdu_id);
1567 
1568 	rcu_read_lock();
1569 	ar = ath12k_mac_get_ar_by_pdev_id(ab, pdev_id);
1570 	if (!ar) {
1571 		ret = -EINVAL;
1572 		goto exit;
1573 	}
1574 
1575 	spin_lock_bh(&ar->data_lock);
1576 	ppdu_info = ath12k_dp_htt_get_ppdu_desc(ar, ppdu_id);
1577 	if (!ppdu_info) {
1578 		spin_unlock_bh(&ar->data_lock);
1579 		ret = -EINVAL;
1580 		goto exit;
1581 	}
1582 
1583 	ppdu_info->ppdu_id = ppdu_id;
1584 	ret = ath12k_dp_htt_tlv_iter(ab, msg->data, len,
1585 				     ath12k_htt_tlv_ppdu_stats_parse,
1586 				     (void *)ppdu_info);
1587 	if (ret) {
1588 		spin_unlock_bh(&ar->data_lock);
1589 		ath12k_warn(ab, "Failed to parse tlv %d\n", ret);
1590 		goto exit;
1591 	}
1592 
1593 	if (ppdu_info->ppdu_stats.common.num_users >= HTT_PPDU_STATS_MAX_USERS) {
1594 		spin_unlock_bh(&ar->data_lock);
1595 		ath12k_warn(ab,
1596 			    "HTT PPDU STATS event has unexpected num_users %u, should be smaller than %u\n",
1597 			    ppdu_info->ppdu_stats.common.num_users,
1598 			    HTT_PPDU_STATS_MAX_USERS);
1599 		ret = -EINVAL;
1600 		goto exit;
1601 	}
1602 
1603 	/* back up data rate tlv for all peers */
1604 	if (ppdu_info->frame_type == HTT_STATS_PPDU_FTYPE_DATA &&
1605 	    (ppdu_info->tlv_bitmap & (1 << HTT_PPDU_STATS_TAG_USR_COMMON)) &&
1606 	    ppdu_info->delay_ba) {
1607 		for (i = 0; i < ppdu_info->ppdu_stats.common.num_users; i++) {
1608 			peer_id = ppdu_info->ppdu_stats.user_stats[i].peer_id;
1609 			spin_lock_bh(&ab->base_lock);
1610 			peer = ath12k_peer_find_by_id(ab, peer_id);
1611 			if (!peer) {
1612 				spin_unlock_bh(&ab->base_lock);
1613 				continue;
1614 			}
1615 
1616 			usr_stats = &ppdu_info->ppdu_stats.user_stats[i];
1617 			if (usr_stats->delay_ba)
1618 				ath12k_copy_to_delay_stats(peer, usr_stats);
1619 			spin_unlock_bh(&ab->base_lock);
1620 		}
1621 	}
1622 
1623 	/* restore all peers' data rate tlv to mu-bar tlv */
1624 	if (ppdu_info->frame_type == HTT_STATS_PPDU_FTYPE_BAR &&
1625 	    (ppdu_info->tlv_bitmap & (1 << HTT_PPDU_STATS_TAG_USR_COMMON))) {
1626 		for (i = 0; i < ppdu_info->bar_num_users; i++) {
1627 			peer_id = ppdu_info->ppdu_stats.user_stats[i].peer_id;
1628 			spin_lock_bh(&ab->base_lock);
1629 			peer = ath12k_peer_find_by_id(ab, peer_id);
1630 			if (!peer) {
1631 				spin_unlock_bh(&ab->base_lock);
1632 				continue;
1633 			}
1634 
1635 			usr_stats = &ppdu_info->ppdu_stats.user_stats[i];
1636 			if (peer->delayba_flag)
1637 				ath12k_copy_to_bar(peer, usr_stats);
1638 			spin_unlock_bh(&ab->base_lock);
1639 		}
1640 	}
1641 
1642 	spin_unlock_bh(&ar->data_lock);
1643 
1644 exit:
1645 	rcu_read_unlock();
1646 
1647 	return ret;
1648 }
1649 
1650 static void ath12k_htt_mlo_offset_event_handler(struct ath12k_base *ab,
1651 						struct sk_buff *skb)
1652 {
1653 	struct ath12k_htt_mlo_offset_msg *msg;
1654 	struct ath12k_pdev *pdev;
1655 	struct ath12k *ar;
1656 	u8 pdev_id;
1657 
1658 	msg = (struct ath12k_htt_mlo_offset_msg *)skb->data;
1659 	pdev_id = u32_get_bits(__le32_to_cpu(msg->info),
1660 			       HTT_T2H_MLO_OFFSET_INFO_PDEV_ID);
1661 
1662 	rcu_read_lock();
1663 	ar = ath12k_mac_get_ar_by_pdev_id(ab, pdev_id);
1664 	if (!ar) {
1665 		ath12k_warn(ab, "invalid pdev id %d on htt mlo offset\n", pdev_id);
1666 		goto exit;
1667 	}
1668 
1669 	spin_lock_bh(&ar->data_lock);
1670 	pdev = ar->pdev;
1671 
1672 	pdev->timestamp.info = __le32_to_cpu(msg->info);
1673 	pdev->timestamp.sync_timestamp_lo_us = __le32_to_cpu(msg->sync_timestamp_lo_us);
1674 	pdev->timestamp.sync_timestamp_hi_us = __le32_to_cpu(msg->sync_timestamp_hi_us);
1675 	pdev->timestamp.mlo_offset_lo = __le32_to_cpu(msg->mlo_offset_lo);
1676 	pdev->timestamp.mlo_offset_hi = __le32_to_cpu(msg->mlo_offset_hi);
1677 	pdev->timestamp.mlo_offset_clks = __le32_to_cpu(msg->mlo_offset_clks);
1678 	pdev->timestamp.mlo_comp_clks = __le32_to_cpu(msg->mlo_comp_clks);
1679 	pdev->timestamp.mlo_comp_timer = __le32_to_cpu(msg->mlo_comp_timer);
1680 
1681 	spin_unlock_bh(&ar->data_lock);
1682 exit:
1683 	rcu_read_unlock();
1684 }
1685 
1686 void ath12k_dp_htt_htc_t2h_msg_handler(struct ath12k_base *ab,
1687 				       struct sk_buff *skb)
1688 {
1689 	struct ath12k_dp *dp = &ab->dp;
1690 	struct htt_resp_msg *resp = (struct htt_resp_msg *)skb->data;
1691 	enum htt_t2h_msg_type type;
1692 	u16 peer_id;
1693 	u8 vdev_id;
1694 	u8 mac_addr[ETH_ALEN];
1695 	u16 peer_mac_h16;
1696 	u16 ast_hash = 0;
1697 	u16 hw_peer_id;
1698 
1699 	type = le32_get_bits(resp->version_msg.version, HTT_T2H_MSG_TYPE);
1700 
1701 	ath12k_dbg(ab, ATH12K_DBG_DP_HTT, "dp_htt rx msg type :0x%0x\n", type);
1702 
1703 	switch (type) {
1704 	case HTT_T2H_MSG_TYPE_VERSION_CONF:
1705 		dp->htt_tgt_ver_major = le32_get_bits(resp->version_msg.version,
1706 						      HTT_T2H_VERSION_CONF_MAJOR);
1707 		dp->htt_tgt_ver_minor = le32_get_bits(resp->version_msg.version,
1708 						      HTT_T2H_VERSION_CONF_MINOR);
1709 		complete(&dp->htt_tgt_version_received);
1710 		break;
1711 	/* TODO: remove unused peer map versions after testing */
1712 	case HTT_T2H_MSG_TYPE_PEER_MAP:
1713 		vdev_id = le32_get_bits(resp->peer_map_ev.info,
1714 					HTT_T2H_PEER_MAP_INFO_VDEV_ID);
1715 		peer_id = le32_get_bits(resp->peer_map_ev.info,
1716 					HTT_T2H_PEER_MAP_INFO_PEER_ID);
1717 		peer_mac_h16 = le32_get_bits(resp->peer_map_ev.info1,
1718 					     HTT_T2H_PEER_MAP_INFO1_MAC_ADDR_H16);
1719 		ath12k_dp_get_mac_addr(le32_to_cpu(resp->peer_map_ev.mac_addr_l32),
1720 				       peer_mac_h16, mac_addr);
1721 		ath12k_peer_map_event(ab, vdev_id, peer_id, mac_addr, 0, 0);
1722 		break;
1723 	case HTT_T2H_MSG_TYPE_PEER_MAP2:
1724 		vdev_id = le32_get_bits(resp->peer_map_ev.info,
1725 					HTT_T2H_PEER_MAP_INFO_VDEV_ID);
1726 		peer_id = le32_get_bits(resp->peer_map_ev.info,
1727 					HTT_T2H_PEER_MAP_INFO_PEER_ID);
1728 		peer_mac_h16 = le32_get_bits(resp->peer_map_ev.info1,
1729 					     HTT_T2H_PEER_MAP_INFO1_MAC_ADDR_H16);
1730 		ath12k_dp_get_mac_addr(le32_to_cpu(resp->peer_map_ev.mac_addr_l32),
1731 				       peer_mac_h16, mac_addr);
1732 		ast_hash = le32_get_bits(resp->peer_map_ev.info2,
1733 					 HTT_T2H_PEER_MAP_INFO2_AST_HASH_VAL);
1734 		hw_peer_id = le32_get_bits(resp->peer_map_ev.info1,
1735 					   HTT_T2H_PEER_MAP_INFO1_HW_PEER_ID);
1736 		ath12k_peer_map_event(ab, vdev_id, peer_id, mac_addr, ast_hash,
1737 				      hw_peer_id);
1738 		break;
1739 	case HTT_T2H_MSG_TYPE_PEER_MAP3:
1740 		vdev_id = le32_get_bits(resp->peer_map_ev.info,
1741 					HTT_T2H_PEER_MAP_INFO_VDEV_ID);
1742 		peer_id = le32_get_bits(resp->peer_map_ev.info,
1743 					HTT_T2H_PEER_MAP_INFO_PEER_ID);
1744 		peer_mac_h16 = le32_get_bits(resp->peer_map_ev.info1,
1745 					     HTT_T2H_PEER_MAP_INFO1_MAC_ADDR_H16);
1746 		ath12k_dp_get_mac_addr(le32_to_cpu(resp->peer_map_ev.mac_addr_l32),
1747 				       peer_mac_h16, mac_addr);
1748 		ath12k_peer_map_event(ab, vdev_id, peer_id, mac_addr, ast_hash,
1749 				      peer_id);
1750 		break;
1751 	case HTT_T2H_MSG_TYPE_PEER_UNMAP:
1752 	case HTT_T2H_MSG_TYPE_PEER_UNMAP2:
1753 		peer_id = le32_get_bits(resp->peer_unmap_ev.info,
1754 					HTT_T2H_PEER_UNMAP_INFO_PEER_ID);
1755 		ath12k_peer_unmap_event(ab, peer_id);
1756 		break;
1757 	case HTT_T2H_MSG_TYPE_PPDU_STATS_IND:
1758 		ath12k_htt_pull_ppdu_stats(ab, skb);
1759 		break;
1760 	case HTT_T2H_MSG_TYPE_EXT_STATS_CONF:
1761 		break;
1762 	case HTT_T2H_MSG_TYPE_MLO_TIMESTAMP_OFFSET_IND:
1763 		ath12k_htt_mlo_offset_event_handler(ab, skb);
1764 		break;
1765 	default:
1766 		ath12k_dbg(ab, ATH12K_DBG_DP_HTT, "dp_htt event %d not handled\n",
1767 			   type);
1768 		break;
1769 	}
1770 
1771 	dev_kfree_skb_any(skb);
1772 }
1773 
1774 static int ath12k_dp_rx_msdu_coalesce(struct ath12k *ar,
1775 				      struct sk_buff_head *msdu_list,
1776 				      struct sk_buff *first, struct sk_buff *last,
1777 				      u8 l3pad_bytes, int msdu_len)
1778 {
1779 	struct ath12k_base *ab = ar->ab;
1780 	struct sk_buff *skb;
1781 	struct ath12k_skb_rxcb *rxcb = ATH12K_SKB_RXCB(first);
1782 	int buf_first_hdr_len, buf_first_len;
1783 	struct hal_rx_desc *ldesc;
1784 	int space_extra, rem_len, buf_len;
1785 	u32 hal_rx_desc_sz = ar->ab->hw_params->hal_desc_sz;
1786 
1787 	/* As the msdu is spread across multiple rx buffers,
1788 	 * find the offset to the start of msdu for computing
1789 	 * the length of the msdu in the first buffer.
1790 	 */
1791 	buf_first_hdr_len = hal_rx_desc_sz + l3pad_bytes;
1792 	buf_first_len = DP_RX_BUFFER_SIZE - buf_first_hdr_len;
1793 
1794 	if (WARN_ON_ONCE(msdu_len <= buf_first_len)) {
1795 		skb_put(first, buf_first_hdr_len + msdu_len);
1796 		skb_pull(first, buf_first_hdr_len);
1797 		return 0;
1798 	}
1799 
1800 	ldesc = (struct hal_rx_desc *)last->data;
1801 	rxcb->is_first_msdu = ath12k_dp_rx_h_first_msdu(ab, ldesc);
1802 	rxcb->is_last_msdu = ath12k_dp_rx_h_last_msdu(ab, ldesc);
1803 
1804 	/* MSDU spans over multiple buffers because the length of the MSDU
1805 	 * exceeds DP_RX_BUFFER_SIZE - HAL_RX_DESC_SIZE. So assume the data
1806 	 * in the first buf is of length DP_RX_BUFFER_SIZE - HAL_RX_DESC_SIZE.
1807 	 */
1808 	skb_put(first, DP_RX_BUFFER_SIZE);
1809 	skb_pull(first, buf_first_hdr_len);
1810 
1811 	/* When an MSDU spread over multiple buffers MSDU_END
1812 	 * tlvs are valid only in the last buffer. Copy those tlvs.
1813 	 */
1814 	ath12k_dp_rx_desc_end_tlv_copy(ab, rxcb->rx_desc, ldesc);
1815 
1816 	space_extra = msdu_len - (buf_first_len + skb_tailroom(first));
1817 	if (space_extra > 0 &&
1818 	    (pskb_expand_head(first, 0, space_extra, GFP_ATOMIC) < 0)) {
1819 		/* Free up all buffers of the MSDU */
1820 		while ((skb = __skb_dequeue(msdu_list)) != NULL) {
1821 			rxcb = ATH12K_SKB_RXCB(skb);
1822 			if (!rxcb->is_continuation) {
1823 				dev_kfree_skb_any(skb);
1824 				break;
1825 			}
1826 			dev_kfree_skb_any(skb);
1827 		}
1828 		return -ENOMEM;
1829 	}
1830 
1831 	rem_len = msdu_len - buf_first_len;
1832 	while ((skb = __skb_dequeue(msdu_list)) != NULL && rem_len > 0) {
1833 		rxcb = ATH12K_SKB_RXCB(skb);
1834 		if (rxcb->is_continuation)
1835 			buf_len = DP_RX_BUFFER_SIZE - hal_rx_desc_sz;
1836 		else
1837 			buf_len = rem_len;
1838 
1839 		if (buf_len > (DP_RX_BUFFER_SIZE - hal_rx_desc_sz)) {
1840 			WARN_ON_ONCE(1);
1841 			dev_kfree_skb_any(skb);
1842 			return -EINVAL;
1843 		}
1844 
1845 		skb_put(skb, buf_len + hal_rx_desc_sz);
1846 		skb_pull(skb, hal_rx_desc_sz);
1847 		skb_copy_from_linear_data(skb, skb_put(first, buf_len),
1848 					  buf_len);
1849 		dev_kfree_skb_any(skb);
1850 
1851 		rem_len -= buf_len;
1852 		if (!rxcb->is_continuation)
1853 			break;
1854 	}
1855 
1856 	return 0;
1857 }
1858 
1859 static struct sk_buff *ath12k_dp_rx_get_msdu_last_buf(struct sk_buff_head *msdu_list,
1860 						      struct sk_buff *first)
1861 {
1862 	struct sk_buff *skb;
1863 	struct ath12k_skb_rxcb *rxcb = ATH12K_SKB_RXCB(first);
1864 
1865 	if (!rxcb->is_continuation)
1866 		return first;
1867 
1868 	skb_queue_walk(msdu_list, skb) {
1869 		rxcb = ATH12K_SKB_RXCB(skb);
1870 		if (!rxcb->is_continuation)
1871 			return skb;
1872 	}
1873 
1874 	return NULL;
1875 }
1876 
1877 static void ath12k_dp_rx_h_csum_offload(struct ath12k *ar, struct sk_buff *msdu)
1878 {
1879 	struct ath12k_skb_rxcb *rxcb = ATH12K_SKB_RXCB(msdu);
1880 	struct ath12k_base *ab = ar->ab;
1881 	bool ip_csum_fail, l4_csum_fail;
1882 
1883 	ip_csum_fail = ath12k_dp_rx_h_ip_cksum_fail(ab, rxcb->rx_desc);
1884 	l4_csum_fail = ath12k_dp_rx_h_l4_cksum_fail(ab, rxcb->rx_desc);
1885 
1886 	msdu->ip_summed = (ip_csum_fail || l4_csum_fail) ?
1887 			  CHECKSUM_NONE : CHECKSUM_UNNECESSARY;
1888 }
1889 
1890 static int ath12k_dp_rx_crypto_mic_len(struct ath12k *ar,
1891 				       enum hal_encrypt_type enctype)
1892 {
1893 	switch (enctype) {
1894 	case HAL_ENCRYPT_TYPE_OPEN:
1895 	case HAL_ENCRYPT_TYPE_TKIP_NO_MIC:
1896 	case HAL_ENCRYPT_TYPE_TKIP_MIC:
1897 		return 0;
1898 	case HAL_ENCRYPT_TYPE_CCMP_128:
1899 		return IEEE80211_CCMP_MIC_LEN;
1900 	case HAL_ENCRYPT_TYPE_CCMP_256:
1901 		return IEEE80211_CCMP_256_MIC_LEN;
1902 	case HAL_ENCRYPT_TYPE_GCMP_128:
1903 	case HAL_ENCRYPT_TYPE_AES_GCMP_256:
1904 		return IEEE80211_GCMP_MIC_LEN;
1905 	case HAL_ENCRYPT_TYPE_WEP_40:
1906 	case HAL_ENCRYPT_TYPE_WEP_104:
1907 	case HAL_ENCRYPT_TYPE_WEP_128:
1908 	case HAL_ENCRYPT_TYPE_WAPI_GCM_SM4:
1909 	case HAL_ENCRYPT_TYPE_WAPI:
1910 		break;
1911 	}
1912 
1913 	ath12k_warn(ar->ab, "unsupported encryption type %d for mic len\n", enctype);
1914 	return 0;
1915 }
1916 
1917 static int ath12k_dp_rx_crypto_param_len(struct ath12k *ar,
1918 					 enum hal_encrypt_type enctype)
1919 {
1920 	switch (enctype) {
1921 	case HAL_ENCRYPT_TYPE_OPEN:
1922 		return 0;
1923 	case HAL_ENCRYPT_TYPE_TKIP_NO_MIC:
1924 	case HAL_ENCRYPT_TYPE_TKIP_MIC:
1925 		return IEEE80211_TKIP_IV_LEN;
1926 	case HAL_ENCRYPT_TYPE_CCMP_128:
1927 		return IEEE80211_CCMP_HDR_LEN;
1928 	case HAL_ENCRYPT_TYPE_CCMP_256:
1929 		return IEEE80211_CCMP_256_HDR_LEN;
1930 	case HAL_ENCRYPT_TYPE_GCMP_128:
1931 	case HAL_ENCRYPT_TYPE_AES_GCMP_256:
1932 		return IEEE80211_GCMP_HDR_LEN;
1933 	case HAL_ENCRYPT_TYPE_WEP_40:
1934 	case HAL_ENCRYPT_TYPE_WEP_104:
1935 	case HAL_ENCRYPT_TYPE_WEP_128:
1936 	case HAL_ENCRYPT_TYPE_WAPI_GCM_SM4:
1937 	case HAL_ENCRYPT_TYPE_WAPI:
1938 		break;
1939 	}
1940 
1941 	ath12k_warn(ar->ab, "unsupported encryption type %d\n", enctype);
1942 	return 0;
1943 }
1944 
1945 static int ath12k_dp_rx_crypto_icv_len(struct ath12k *ar,
1946 				       enum hal_encrypt_type enctype)
1947 {
1948 	switch (enctype) {
1949 	case HAL_ENCRYPT_TYPE_OPEN:
1950 	case HAL_ENCRYPT_TYPE_CCMP_128:
1951 	case HAL_ENCRYPT_TYPE_CCMP_256:
1952 	case HAL_ENCRYPT_TYPE_GCMP_128:
1953 	case HAL_ENCRYPT_TYPE_AES_GCMP_256:
1954 		return 0;
1955 	case HAL_ENCRYPT_TYPE_TKIP_NO_MIC:
1956 	case HAL_ENCRYPT_TYPE_TKIP_MIC:
1957 		return IEEE80211_TKIP_ICV_LEN;
1958 	case HAL_ENCRYPT_TYPE_WEP_40:
1959 	case HAL_ENCRYPT_TYPE_WEP_104:
1960 	case HAL_ENCRYPT_TYPE_WEP_128:
1961 	case HAL_ENCRYPT_TYPE_WAPI_GCM_SM4:
1962 	case HAL_ENCRYPT_TYPE_WAPI:
1963 		break;
1964 	}
1965 
1966 	ath12k_warn(ar->ab, "unsupported encryption type %d\n", enctype);
1967 	return 0;
1968 }
1969 
1970 static void ath12k_dp_rx_h_undecap_nwifi(struct ath12k *ar,
1971 					 struct sk_buff *msdu,
1972 					 enum hal_encrypt_type enctype,
1973 					 struct ieee80211_rx_status *status)
1974 {
1975 	struct ath12k_base *ab = ar->ab;
1976 	struct ath12k_skb_rxcb *rxcb = ATH12K_SKB_RXCB(msdu);
1977 	u8 decap_hdr[DP_MAX_NWIFI_HDR_LEN];
1978 	struct ieee80211_hdr *hdr;
1979 	size_t hdr_len;
1980 	u8 *crypto_hdr;
1981 	u16 qos_ctl;
1982 
1983 	/* pull decapped header */
1984 	hdr = (struct ieee80211_hdr *)msdu->data;
1985 	hdr_len = ieee80211_hdrlen(hdr->frame_control);
1986 	skb_pull(msdu, hdr_len);
1987 
1988 	/*  Rebuild qos header */
1989 	hdr->frame_control |= __cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
1990 
1991 	/* Reset the order bit as the HT_Control header is stripped */
1992 	hdr->frame_control &= ~(__cpu_to_le16(IEEE80211_FCTL_ORDER));
1993 
1994 	qos_ctl = rxcb->tid;
1995 
1996 	if (ath12k_dp_rx_h_mesh_ctl_present(ab, rxcb->rx_desc))
1997 		qos_ctl |= IEEE80211_QOS_CTL_MESH_CONTROL_PRESENT;
1998 
1999 	/* TODO: Add other QoS ctl fields when required */
2000 
2001 	/* copy decap header before overwriting for reuse below */
2002 	memcpy(decap_hdr, hdr, hdr_len);
2003 
2004 	/* Rebuild crypto header for mac80211 use */
2005 	if (!(status->flag & RX_FLAG_IV_STRIPPED)) {
2006 		crypto_hdr = skb_push(msdu, ath12k_dp_rx_crypto_param_len(ar, enctype));
2007 		ath12k_dp_rx_desc_get_crypto_header(ar->ab,
2008 						    rxcb->rx_desc, crypto_hdr,
2009 						    enctype);
2010 	}
2011 
2012 	memcpy(skb_push(msdu,
2013 			IEEE80211_QOS_CTL_LEN), &qos_ctl,
2014 			IEEE80211_QOS_CTL_LEN);
2015 	memcpy(skb_push(msdu, hdr_len), decap_hdr, hdr_len);
2016 }
2017 
2018 static void ath12k_dp_rx_h_undecap_raw(struct ath12k *ar, struct sk_buff *msdu,
2019 				       enum hal_encrypt_type enctype,
2020 				       struct ieee80211_rx_status *status,
2021 				       bool decrypted)
2022 {
2023 	struct ath12k_skb_rxcb *rxcb = ATH12K_SKB_RXCB(msdu);
2024 	struct ieee80211_hdr *hdr;
2025 	size_t hdr_len;
2026 	size_t crypto_len;
2027 
2028 	if (!rxcb->is_first_msdu ||
2029 	    !(rxcb->is_first_msdu && rxcb->is_last_msdu)) {
2030 		WARN_ON_ONCE(1);
2031 		return;
2032 	}
2033 
2034 	skb_trim(msdu, msdu->len - FCS_LEN);
2035 
2036 	if (!decrypted)
2037 		return;
2038 
2039 	hdr = (void *)msdu->data;
2040 
2041 	/* Tail */
2042 	if (status->flag & RX_FLAG_IV_STRIPPED) {
2043 		skb_trim(msdu, msdu->len -
2044 			 ath12k_dp_rx_crypto_mic_len(ar, enctype));
2045 
2046 		skb_trim(msdu, msdu->len -
2047 			 ath12k_dp_rx_crypto_icv_len(ar, enctype));
2048 	} else {
2049 		/* MIC */
2050 		if (status->flag & RX_FLAG_MIC_STRIPPED)
2051 			skb_trim(msdu, msdu->len -
2052 				 ath12k_dp_rx_crypto_mic_len(ar, enctype));
2053 
2054 		/* ICV */
2055 		if (status->flag & RX_FLAG_ICV_STRIPPED)
2056 			skb_trim(msdu, msdu->len -
2057 				 ath12k_dp_rx_crypto_icv_len(ar, enctype));
2058 	}
2059 
2060 	/* MMIC */
2061 	if ((status->flag & RX_FLAG_MMIC_STRIPPED) &&
2062 	    !ieee80211_has_morefrags(hdr->frame_control) &&
2063 	    enctype == HAL_ENCRYPT_TYPE_TKIP_MIC)
2064 		skb_trim(msdu, msdu->len - IEEE80211_CCMP_MIC_LEN);
2065 
2066 	/* Head */
2067 	if (status->flag & RX_FLAG_IV_STRIPPED) {
2068 		hdr_len = ieee80211_hdrlen(hdr->frame_control);
2069 		crypto_len = ath12k_dp_rx_crypto_param_len(ar, enctype);
2070 
2071 		memmove(msdu->data + crypto_len, msdu->data, hdr_len);
2072 		skb_pull(msdu, crypto_len);
2073 	}
2074 }
2075 
2076 static void ath12k_get_dot11_hdr_from_rx_desc(struct ath12k *ar,
2077 					      struct sk_buff *msdu,
2078 					      struct ath12k_skb_rxcb *rxcb,
2079 					      struct ieee80211_rx_status *status,
2080 					      enum hal_encrypt_type enctype)
2081 {
2082 	struct hal_rx_desc *rx_desc = rxcb->rx_desc;
2083 	struct ath12k_base *ab = ar->ab;
2084 	size_t hdr_len, crypto_len;
2085 	struct ieee80211_hdr *hdr;
2086 	u16 qos_ctl;
2087 	__le16 fc;
2088 	u8 *crypto_hdr;
2089 
2090 	if (!(status->flag & RX_FLAG_IV_STRIPPED)) {
2091 		crypto_len = ath12k_dp_rx_crypto_param_len(ar, enctype);
2092 		crypto_hdr = skb_push(msdu, crypto_len);
2093 		ath12k_dp_rx_desc_get_crypto_header(ab, rx_desc, crypto_hdr, enctype);
2094 	}
2095 
2096 	fc = cpu_to_le16(ath12k_dp_rxdesc_get_mpdu_frame_ctrl(ab, rx_desc));
2097 	hdr_len = ieee80211_hdrlen(fc);
2098 	skb_push(msdu, hdr_len);
2099 	hdr = (struct ieee80211_hdr *)msdu->data;
2100 	hdr->frame_control = fc;
2101 
2102 	/* Get wifi header from rx_desc */
2103 	ath12k_dp_rx_desc_get_dot11_hdr(ab, rx_desc, hdr);
2104 
2105 	if (rxcb->is_mcbc)
2106 		status->flag &= ~RX_FLAG_PN_VALIDATED;
2107 
2108 	/* Add QOS header */
2109 	if (ieee80211_is_data_qos(hdr->frame_control)) {
2110 		qos_ctl = rxcb->tid;
2111 		if (ath12k_dp_rx_h_mesh_ctl_present(ab, rx_desc))
2112 			qos_ctl |= IEEE80211_QOS_CTL_MESH_CONTROL_PRESENT;
2113 
2114 		/* TODO: Add other QoS ctl fields when required */
2115 		memcpy(msdu->data + (hdr_len - IEEE80211_QOS_CTL_LEN),
2116 		       &qos_ctl, IEEE80211_QOS_CTL_LEN);
2117 	}
2118 }
2119 
2120 static void ath12k_dp_rx_h_undecap_eth(struct ath12k *ar,
2121 				       struct sk_buff *msdu,
2122 				       enum hal_encrypt_type enctype,
2123 				       struct ieee80211_rx_status *status)
2124 {
2125 	struct ieee80211_hdr *hdr;
2126 	struct ethhdr *eth;
2127 	u8 da[ETH_ALEN];
2128 	u8 sa[ETH_ALEN];
2129 	struct ath12k_skb_rxcb *rxcb = ATH12K_SKB_RXCB(msdu);
2130 	struct ath12k_dp_rx_rfc1042_hdr rfc = {0xaa, 0xaa, 0x03, {0x00, 0x00, 0x00}};
2131 
2132 	eth = (struct ethhdr *)msdu->data;
2133 	ether_addr_copy(da, eth->h_dest);
2134 	ether_addr_copy(sa, eth->h_source);
2135 	rfc.snap_type = eth->h_proto;
2136 	skb_pull(msdu, sizeof(*eth));
2137 	memcpy(skb_push(msdu, sizeof(rfc)), &rfc,
2138 	       sizeof(rfc));
2139 	ath12k_get_dot11_hdr_from_rx_desc(ar, msdu, rxcb, status, enctype);
2140 
2141 	/* original 802.11 header has a different DA and in
2142 	 * case of 4addr it may also have different SA
2143 	 */
2144 	hdr = (struct ieee80211_hdr *)msdu->data;
2145 	ether_addr_copy(ieee80211_get_DA(hdr), da);
2146 	ether_addr_copy(ieee80211_get_SA(hdr), sa);
2147 }
2148 
2149 static void ath12k_dp_rx_h_undecap(struct ath12k *ar, struct sk_buff *msdu,
2150 				   struct hal_rx_desc *rx_desc,
2151 				   enum hal_encrypt_type enctype,
2152 				   struct ieee80211_rx_status *status,
2153 				   bool decrypted)
2154 {
2155 	struct ath12k_base *ab = ar->ab;
2156 	u8 decap;
2157 	struct ethhdr *ehdr;
2158 
2159 	decap = ath12k_dp_rx_h_decap_type(ab, rx_desc);
2160 
2161 	switch (decap) {
2162 	case DP_RX_DECAP_TYPE_NATIVE_WIFI:
2163 		ath12k_dp_rx_h_undecap_nwifi(ar, msdu, enctype, status);
2164 		break;
2165 	case DP_RX_DECAP_TYPE_RAW:
2166 		ath12k_dp_rx_h_undecap_raw(ar, msdu, enctype, status,
2167 					   decrypted);
2168 		break;
2169 	case DP_RX_DECAP_TYPE_ETHERNET2_DIX:
2170 		ehdr = (struct ethhdr *)msdu->data;
2171 
2172 		/* mac80211 allows fast path only for authorized STA */
2173 		if (ehdr->h_proto == cpu_to_be16(ETH_P_PAE)) {
2174 			ATH12K_SKB_RXCB(msdu)->is_eapol = true;
2175 			ath12k_dp_rx_h_undecap_eth(ar, msdu, enctype, status);
2176 			break;
2177 		}
2178 
2179 		/* PN for mcast packets will be validated in mac80211;
2180 		 * remove eth header and add 802.11 header.
2181 		 */
2182 		if (ATH12K_SKB_RXCB(msdu)->is_mcbc && decrypted)
2183 			ath12k_dp_rx_h_undecap_eth(ar, msdu, enctype, status);
2184 		break;
2185 	case DP_RX_DECAP_TYPE_8023:
2186 		/* TODO: Handle undecap for these formats */
2187 		break;
2188 	}
2189 }
2190 
2191 struct ath12k_peer *
2192 ath12k_dp_rx_h_find_peer(struct ath12k_base *ab, struct sk_buff *msdu)
2193 {
2194 	struct ath12k_skb_rxcb *rxcb = ATH12K_SKB_RXCB(msdu);
2195 	struct hal_rx_desc *rx_desc = rxcb->rx_desc;
2196 	struct ath12k_peer *peer = NULL;
2197 
2198 	lockdep_assert_held(&ab->base_lock);
2199 
2200 	if (rxcb->peer_id)
2201 		peer = ath12k_peer_find_by_id(ab, rxcb->peer_id);
2202 
2203 	if (peer)
2204 		return peer;
2205 
2206 	if (!rx_desc || !(ath12k_dp_rxdesc_mac_addr2_valid(ab, rx_desc)))
2207 		return NULL;
2208 
2209 	peer = ath12k_peer_find_by_addr(ab,
2210 					ath12k_dp_rxdesc_get_mpdu_start_addr2(ab,
2211 									      rx_desc));
2212 	return peer;
2213 }
2214 
2215 static void ath12k_dp_rx_h_mpdu(struct ath12k *ar,
2216 				struct sk_buff *msdu,
2217 				struct hal_rx_desc *rx_desc,
2218 				struct ieee80211_rx_status *rx_status)
2219 {
2220 	bool  fill_crypto_hdr;
2221 	struct ath12k_base *ab = ar->ab;
2222 	struct ath12k_skb_rxcb *rxcb;
2223 	enum hal_encrypt_type enctype;
2224 	bool is_decrypted = false;
2225 	struct ieee80211_hdr *hdr;
2226 	struct ath12k_peer *peer;
2227 	u32 err_bitmap;
2228 
2229 	/* PN for multicast packets will be checked in mac80211 */
2230 	rxcb = ATH12K_SKB_RXCB(msdu);
2231 	fill_crypto_hdr = ath12k_dp_rx_h_is_da_mcbc(ar->ab, rx_desc);
2232 	rxcb->is_mcbc = fill_crypto_hdr;
2233 
2234 	if (rxcb->is_mcbc)
2235 		rxcb->peer_id = ath12k_dp_rx_h_peer_id(ar->ab, rx_desc);
2236 
2237 	spin_lock_bh(&ar->ab->base_lock);
2238 	peer = ath12k_dp_rx_h_find_peer(ar->ab, msdu);
2239 	if (peer) {
2240 		if (rxcb->is_mcbc)
2241 			enctype = peer->sec_type_grp;
2242 		else
2243 			enctype = peer->sec_type;
2244 	} else {
2245 		enctype = HAL_ENCRYPT_TYPE_OPEN;
2246 	}
2247 	spin_unlock_bh(&ar->ab->base_lock);
2248 
2249 	err_bitmap = ath12k_dp_rx_h_mpdu_err(ab, rx_desc);
2250 	if (enctype != HAL_ENCRYPT_TYPE_OPEN && !err_bitmap)
2251 		is_decrypted = ath12k_dp_rx_h_is_decrypted(ab, rx_desc);
2252 
2253 	/* Clear per-MPDU flags while leaving per-PPDU flags intact */
2254 	rx_status->flag &= ~(RX_FLAG_FAILED_FCS_CRC |
2255 			     RX_FLAG_MMIC_ERROR |
2256 			     RX_FLAG_DECRYPTED |
2257 			     RX_FLAG_IV_STRIPPED |
2258 			     RX_FLAG_MMIC_STRIPPED);
2259 
2260 	if (err_bitmap & HAL_RX_MPDU_ERR_FCS)
2261 		rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
2262 	if (err_bitmap & HAL_RX_MPDU_ERR_TKIP_MIC)
2263 		rx_status->flag |= RX_FLAG_MMIC_ERROR;
2264 
2265 	if (is_decrypted) {
2266 		rx_status->flag |= RX_FLAG_DECRYPTED | RX_FLAG_MMIC_STRIPPED;
2267 
2268 		if (fill_crypto_hdr)
2269 			rx_status->flag |= RX_FLAG_MIC_STRIPPED |
2270 					RX_FLAG_ICV_STRIPPED;
2271 		else
2272 			rx_status->flag |= RX_FLAG_IV_STRIPPED |
2273 					   RX_FLAG_PN_VALIDATED;
2274 	}
2275 
2276 	ath12k_dp_rx_h_csum_offload(ar, msdu);
2277 	ath12k_dp_rx_h_undecap(ar, msdu, rx_desc,
2278 			       enctype, rx_status, is_decrypted);
2279 
2280 	if (!is_decrypted || fill_crypto_hdr)
2281 		return;
2282 
2283 	if (ath12k_dp_rx_h_decap_type(ar->ab, rx_desc) !=
2284 	    DP_RX_DECAP_TYPE_ETHERNET2_DIX) {
2285 		hdr = (void *)msdu->data;
2286 		hdr->frame_control &= ~__cpu_to_le16(IEEE80211_FCTL_PROTECTED);
2287 	}
2288 }
2289 
2290 static void ath12k_dp_rx_h_rate(struct ath12k *ar, struct hal_rx_desc *rx_desc,
2291 				struct ieee80211_rx_status *rx_status)
2292 {
2293 	struct ath12k_base *ab = ar->ab;
2294 	struct ieee80211_supported_band *sband;
2295 	enum rx_msdu_start_pkt_type pkt_type;
2296 	u8 bw;
2297 	u8 rate_mcs, nss;
2298 	u8 sgi;
2299 	bool is_cck;
2300 
2301 	pkt_type = ath12k_dp_rx_h_pkt_type(ab, rx_desc);
2302 	bw = ath12k_dp_rx_h_rx_bw(ab, rx_desc);
2303 	rate_mcs = ath12k_dp_rx_h_rate_mcs(ab, rx_desc);
2304 	nss = ath12k_dp_rx_h_nss(ab, rx_desc);
2305 	sgi = ath12k_dp_rx_h_sgi(ab, rx_desc);
2306 
2307 	switch (pkt_type) {
2308 	case RX_MSDU_START_PKT_TYPE_11A:
2309 	case RX_MSDU_START_PKT_TYPE_11B:
2310 		is_cck = (pkt_type == RX_MSDU_START_PKT_TYPE_11B);
2311 		sband = &ar->mac.sbands[rx_status->band];
2312 		rx_status->rate_idx = ath12k_mac_hw_rate_to_idx(sband, rate_mcs,
2313 								is_cck);
2314 		break;
2315 	case RX_MSDU_START_PKT_TYPE_11N:
2316 		rx_status->encoding = RX_ENC_HT;
2317 		if (rate_mcs > ATH12K_HT_MCS_MAX) {
2318 			ath12k_warn(ar->ab,
2319 				    "Received with invalid mcs in HT mode %d\n",
2320 				     rate_mcs);
2321 			break;
2322 		}
2323 		rx_status->rate_idx = rate_mcs + (8 * (nss - 1));
2324 		if (sgi)
2325 			rx_status->enc_flags |= RX_ENC_FLAG_SHORT_GI;
2326 		rx_status->bw = ath12k_mac_bw_to_mac80211_bw(bw);
2327 		break;
2328 	case RX_MSDU_START_PKT_TYPE_11AC:
2329 		rx_status->encoding = RX_ENC_VHT;
2330 		rx_status->rate_idx = rate_mcs;
2331 		if (rate_mcs > ATH12K_VHT_MCS_MAX) {
2332 			ath12k_warn(ar->ab,
2333 				    "Received with invalid mcs in VHT mode %d\n",
2334 				     rate_mcs);
2335 			break;
2336 		}
2337 		rx_status->nss = nss;
2338 		if (sgi)
2339 			rx_status->enc_flags |= RX_ENC_FLAG_SHORT_GI;
2340 		rx_status->bw = ath12k_mac_bw_to_mac80211_bw(bw);
2341 		break;
2342 	case RX_MSDU_START_PKT_TYPE_11AX:
2343 		rx_status->rate_idx = rate_mcs;
2344 		if (rate_mcs > ATH12K_HE_MCS_MAX) {
2345 			ath12k_warn(ar->ab,
2346 				    "Received with invalid mcs in HE mode %d\n",
2347 				    rate_mcs);
2348 			break;
2349 		}
2350 		rx_status->encoding = RX_ENC_HE;
2351 		rx_status->nss = nss;
2352 		rx_status->he_gi = ath12k_he_gi_to_nl80211_he_gi(sgi);
2353 		rx_status->bw = ath12k_mac_bw_to_mac80211_bw(bw);
2354 		break;
2355 	}
2356 }
2357 
2358 void ath12k_dp_rx_h_ppdu(struct ath12k *ar, struct hal_rx_desc *rx_desc,
2359 			 struct ieee80211_rx_status *rx_status)
2360 {
2361 	struct ath12k_base *ab = ar->ab;
2362 	u8 channel_num;
2363 	u32 center_freq, meta_data;
2364 	struct ieee80211_channel *channel;
2365 
2366 	rx_status->freq = 0;
2367 	rx_status->rate_idx = 0;
2368 	rx_status->nss = 0;
2369 	rx_status->encoding = RX_ENC_LEGACY;
2370 	rx_status->bw = RATE_INFO_BW_20;
2371 	rx_status->enc_flags = 0;
2372 
2373 	rx_status->flag |= RX_FLAG_NO_SIGNAL_VAL;
2374 
2375 	meta_data = ath12k_dp_rx_h_freq(ab, rx_desc);
2376 	channel_num = meta_data;
2377 	center_freq = meta_data >> 16;
2378 
2379 	if (center_freq >= ATH12K_MIN_6G_FREQ &&
2380 	    center_freq <= ATH12K_MAX_6G_FREQ) {
2381 		rx_status->band = NL80211_BAND_6GHZ;
2382 		rx_status->freq = center_freq;
2383 	} else if (channel_num >= 1 && channel_num <= 14) {
2384 		rx_status->band = NL80211_BAND_2GHZ;
2385 	} else if (channel_num >= 36 && channel_num <= 173) {
2386 		rx_status->band = NL80211_BAND_5GHZ;
2387 	} else {
2388 		spin_lock_bh(&ar->data_lock);
2389 		channel = ar->rx_channel;
2390 		if (channel) {
2391 			rx_status->band = channel->band;
2392 			channel_num =
2393 				ieee80211_frequency_to_channel(channel->center_freq);
2394 		}
2395 		spin_unlock_bh(&ar->data_lock);
2396 		ath12k_dbg_dump(ar->ab, ATH12K_DBG_DATA, NULL, "rx_desc: ",
2397 				rx_desc, sizeof(*rx_desc));
2398 	}
2399 
2400 	if (rx_status->band != NL80211_BAND_6GHZ)
2401 		rx_status->freq = ieee80211_channel_to_frequency(channel_num,
2402 								 rx_status->band);
2403 
2404 	ath12k_dp_rx_h_rate(ar, rx_desc, rx_status);
2405 }
2406 
2407 static void ath12k_dp_rx_deliver_msdu(struct ath12k *ar, struct napi_struct *napi,
2408 				      struct sk_buff *msdu,
2409 				      struct ieee80211_rx_status *status)
2410 {
2411 	struct ath12k_base *ab = ar->ab;
2412 	static const struct ieee80211_radiotap_he known = {
2413 		.data1 = cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA1_DATA_MCS_KNOWN |
2414 				     IEEE80211_RADIOTAP_HE_DATA1_BW_RU_ALLOC_KNOWN),
2415 		.data2 = cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA2_GI_KNOWN),
2416 	};
2417 	struct ieee80211_radiotap_he *he;
2418 	struct ieee80211_rx_status *rx_status;
2419 	struct ieee80211_sta *pubsta;
2420 	struct ath12k_peer *peer;
2421 	struct ath12k_skb_rxcb *rxcb = ATH12K_SKB_RXCB(msdu);
2422 	u8 decap = DP_RX_DECAP_TYPE_RAW;
2423 	bool is_mcbc = rxcb->is_mcbc;
2424 	bool is_eapol = rxcb->is_eapol;
2425 
2426 	if (status->encoding == RX_ENC_HE && !(status->flag & RX_FLAG_RADIOTAP_HE) &&
2427 	    !(status->flag & RX_FLAG_SKIP_MONITOR)) {
2428 		he = skb_push(msdu, sizeof(known));
2429 		memcpy(he, &known, sizeof(known));
2430 		status->flag |= RX_FLAG_RADIOTAP_HE;
2431 	}
2432 
2433 	if (!(status->flag & RX_FLAG_ONLY_MONITOR))
2434 		decap = ath12k_dp_rx_h_decap_type(ab, rxcb->rx_desc);
2435 
2436 	spin_lock_bh(&ab->base_lock);
2437 	peer = ath12k_dp_rx_h_find_peer(ab, msdu);
2438 
2439 	pubsta = peer ? peer->sta : NULL;
2440 
2441 	spin_unlock_bh(&ab->base_lock);
2442 
2443 	ath12k_dbg(ab, ATH12K_DBG_DATA,
2444 		   "rx skb %pK len %u peer %pM %d %s sn %u %s%s%s%s%s%s%s%s rate_idx %u vht_nss %u freq %u band %u flag 0x%x fcs-err %i mic-err %i amsdu-more %i\n",
2445 		   msdu,
2446 		   msdu->len,
2447 		   peer ? peer->addr : NULL,
2448 		   rxcb->tid,
2449 		   is_mcbc ? "mcast" : "ucast",
2450 		   ath12k_dp_rx_h_seq_no(ab, rxcb->rx_desc),
2451 		   (status->encoding == RX_ENC_LEGACY) ? "legacy" : "",
2452 		   (status->encoding == RX_ENC_HT) ? "ht" : "",
2453 		   (status->encoding == RX_ENC_VHT) ? "vht" : "",
2454 		   (status->encoding == RX_ENC_HE) ? "he" : "",
2455 		   (status->bw == RATE_INFO_BW_40) ? "40" : "",
2456 		   (status->bw == RATE_INFO_BW_80) ? "80" : "",
2457 		   (status->bw == RATE_INFO_BW_160) ? "160" : "",
2458 		   status->enc_flags & RX_ENC_FLAG_SHORT_GI ? "sgi " : "",
2459 		   status->rate_idx,
2460 		   status->nss,
2461 		   status->freq,
2462 		   status->band, status->flag,
2463 		   !!(status->flag & RX_FLAG_FAILED_FCS_CRC),
2464 		   !!(status->flag & RX_FLAG_MMIC_ERROR),
2465 		   !!(status->flag & RX_FLAG_AMSDU_MORE));
2466 
2467 	ath12k_dbg_dump(ab, ATH12K_DBG_DP_RX, NULL, "dp rx msdu: ",
2468 			msdu->data, msdu->len);
2469 
2470 	rx_status = IEEE80211_SKB_RXCB(msdu);
2471 	*rx_status = *status;
2472 
2473 	/* TODO: trace rx packet */
2474 
2475 	/* PN for multicast packets are not validate in HW,
2476 	 * so skip 802.3 rx path
2477 	 * Also, fast_rx expects the STA to be authorized, hence
2478 	 * eapol packets are sent in slow path.
2479 	 */
2480 	if (decap == DP_RX_DECAP_TYPE_ETHERNET2_DIX && !is_eapol &&
2481 	    !(is_mcbc && rx_status->flag & RX_FLAG_DECRYPTED))
2482 		rx_status->flag |= RX_FLAG_8023;
2483 
2484 	ieee80211_rx_napi(ar->hw, pubsta, msdu, napi);
2485 }
2486 
2487 static int ath12k_dp_rx_process_msdu(struct ath12k *ar,
2488 				     struct sk_buff *msdu,
2489 				     struct sk_buff_head *msdu_list,
2490 				     struct ieee80211_rx_status *rx_status)
2491 {
2492 	struct ath12k_base *ab = ar->ab;
2493 	struct hal_rx_desc *rx_desc, *lrx_desc;
2494 	struct ath12k_skb_rxcb *rxcb;
2495 	struct sk_buff *last_buf;
2496 	u8 l3_pad_bytes;
2497 	u16 msdu_len;
2498 	int ret;
2499 	u32 hal_rx_desc_sz = ar->ab->hw_params->hal_desc_sz;
2500 
2501 	last_buf = ath12k_dp_rx_get_msdu_last_buf(msdu_list, msdu);
2502 	if (!last_buf) {
2503 		ath12k_warn(ab,
2504 			    "No valid Rx buffer to access MSDU_END tlv\n");
2505 		ret = -EIO;
2506 		goto free_out;
2507 	}
2508 
2509 	rx_desc = (struct hal_rx_desc *)msdu->data;
2510 	lrx_desc = (struct hal_rx_desc *)last_buf->data;
2511 	if (!ath12k_dp_rx_h_msdu_done(ab, lrx_desc)) {
2512 		ath12k_warn(ab, "msdu_done bit in msdu_end is not set\n");
2513 		ret = -EIO;
2514 		goto free_out;
2515 	}
2516 
2517 	rxcb = ATH12K_SKB_RXCB(msdu);
2518 	rxcb->rx_desc = rx_desc;
2519 	msdu_len = ath12k_dp_rx_h_msdu_len(ab, lrx_desc);
2520 	l3_pad_bytes = ath12k_dp_rx_h_l3pad(ab, lrx_desc);
2521 
2522 	if (rxcb->is_frag) {
2523 		skb_pull(msdu, hal_rx_desc_sz);
2524 	} else if (!rxcb->is_continuation) {
2525 		if ((msdu_len + hal_rx_desc_sz) > DP_RX_BUFFER_SIZE) {
2526 			ret = -EINVAL;
2527 			ath12k_warn(ab, "invalid msdu len %u\n", msdu_len);
2528 			ath12k_dbg_dump(ab, ATH12K_DBG_DATA, NULL, "", rx_desc,
2529 					sizeof(*rx_desc));
2530 			goto free_out;
2531 		}
2532 		skb_put(msdu, hal_rx_desc_sz + l3_pad_bytes + msdu_len);
2533 		skb_pull(msdu, hal_rx_desc_sz + l3_pad_bytes);
2534 	} else {
2535 		ret = ath12k_dp_rx_msdu_coalesce(ar, msdu_list,
2536 						 msdu, last_buf,
2537 						 l3_pad_bytes, msdu_len);
2538 		if (ret) {
2539 			ath12k_warn(ab,
2540 				    "failed to coalesce msdu rx buffer%d\n", ret);
2541 			goto free_out;
2542 		}
2543 	}
2544 
2545 	ath12k_dp_rx_h_ppdu(ar, rx_desc, rx_status);
2546 	ath12k_dp_rx_h_mpdu(ar, msdu, rx_desc, rx_status);
2547 
2548 	rx_status->flag |= RX_FLAG_SKIP_MONITOR | RX_FLAG_DUP_VALIDATED;
2549 
2550 	return 0;
2551 
2552 free_out:
2553 	return ret;
2554 }
2555 
2556 static void ath12k_dp_rx_process_received_packets(struct ath12k_base *ab,
2557 						  struct napi_struct *napi,
2558 						  struct sk_buff_head *msdu_list,
2559 						  int ring_id)
2560 {
2561 	struct ieee80211_rx_status rx_status = {0};
2562 	struct ath12k_skb_rxcb *rxcb;
2563 	struct sk_buff *msdu;
2564 	struct ath12k *ar;
2565 	u8 mac_id, pdev_id;
2566 	int ret;
2567 
2568 	if (skb_queue_empty(msdu_list))
2569 		return;
2570 
2571 	rcu_read_lock();
2572 
2573 	while ((msdu = __skb_dequeue(msdu_list))) {
2574 		rxcb = ATH12K_SKB_RXCB(msdu);
2575 		mac_id = rxcb->mac_id;
2576 		pdev_id = ath12k_hw_mac_id_to_pdev_id(ab->hw_params, mac_id);
2577 		ar = ab->pdevs[pdev_id].ar;
2578 		if (!rcu_dereference(ab->pdevs_active[pdev_id])) {
2579 			dev_kfree_skb_any(msdu);
2580 			continue;
2581 		}
2582 
2583 		if (test_bit(ATH12K_CAC_RUNNING, &ar->dev_flags)) {
2584 			dev_kfree_skb_any(msdu);
2585 			continue;
2586 		}
2587 
2588 		ret = ath12k_dp_rx_process_msdu(ar, msdu, msdu_list, &rx_status);
2589 		if (ret) {
2590 			ath12k_dbg(ab, ATH12K_DBG_DATA,
2591 				   "Unable to process msdu %d", ret);
2592 			dev_kfree_skb_any(msdu);
2593 			continue;
2594 		}
2595 
2596 		ath12k_dp_rx_deliver_msdu(ar, napi, msdu, &rx_status);
2597 	}
2598 
2599 	rcu_read_unlock();
2600 }
2601 
2602 int ath12k_dp_rx_process(struct ath12k_base *ab, int ring_id,
2603 			 struct napi_struct *napi, int budget)
2604 {
2605 	struct ath12k_rx_desc_info *desc_info;
2606 	struct ath12k_dp *dp = &ab->dp;
2607 	struct dp_rxdma_ring *rx_ring = &dp->rx_refill_buf_ring;
2608 	struct hal_reo_dest_ring *desc;
2609 	int num_buffs_reaped = 0;
2610 	struct sk_buff_head msdu_list;
2611 	struct ath12k_skb_rxcb *rxcb;
2612 	int total_msdu_reaped = 0;
2613 	struct hal_srng *srng;
2614 	struct sk_buff *msdu;
2615 	bool done = false;
2616 	int mac_id;
2617 	u64 desc_va;
2618 
2619 	__skb_queue_head_init(&msdu_list);
2620 
2621 	srng = &ab->hal.srng_list[dp->reo_dst_ring[ring_id].ring_id];
2622 
2623 	spin_lock_bh(&srng->lock);
2624 
2625 try_again:
2626 	ath12k_hal_srng_access_begin(ab, srng);
2627 
2628 	while ((desc = ath12k_hal_srng_dst_get_next_entry(ab, srng))) {
2629 		enum hal_reo_dest_ring_push_reason push_reason;
2630 		u32 cookie;
2631 
2632 		cookie = le32_get_bits(desc->buf_addr_info.info1,
2633 				       BUFFER_ADDR_INFO1_SW_COOKIE);
2634 
2635 		mac_id = le32_get_bits(desc->info0,
2636 				       HAL_REO_DEST_RING_INFO0_SRC_LINK_ID);
2637 
2638 		desc_va = ((u64)le32_to_cpu(desc->buf_va_hi) << 32 |
2639 			   le32_to_cpu(desc->buf_va_lo));
2640 		desc_info = (struct ath12k_rx_desc_info *)((unsigned long)desc_va);
2641 
2642 		/* retry manual desc retrieval */
2643 		if (!desc_info) {
2644 			desc_info = ath12k_dp_get_rx_desc(ab, cookie);
2645 			if (!desc_info) {
2646 				ath12k_warn(ab, "Invalid cookie in manual desc retrieval");
2647 				continue;
2648 			}
2649 		}
2650 
2651 		if (desc_info->magic != ATH12K_DP_RX_DESC_MAGIC)
2652 			ath12k_warn(ab, "Check HW CC implementation");
2653 
2654 		msdu = desc_info->skb;
2655 		desc_info->skb = NULL;
2656 
2657 		spin_lock_bh(&dp->rx_desc_lock);
2658 		list_move_tail(&desc_info->list, &dp->rx_desc_free_list);
2659 		spin_unlock_bh(&dp->rx_desc_lock);
2660 
2661 		rxcb = ATH12K_SKB_RXCB(msdu);
2662 		dma_unmap_single(ab->dev, rxcb->paddr,
2663 				 msdu->len + skb_tailroom(msdu),
2664 				 DMA_FROM_DEVICE);
2665 
2666 		num_buffs_reaped++;
2667 
2668 		push_reason = le32_get_bits(desc->info0,
2669 					    HAL_REO_DEST_RING_INFO0_PUSH_REASON);
2670 		if (push_reason !=
2671 		    HAL_REO_DEST_RING_PUSH_REASON_ROUTING_INSTRUCTION) {
2672 			dev_kfree_skb_any(msdu);
2673 			ab->soc_stats.hal_reo_error[dp->reo_dst_ring[ring_id].ring_id]++;
2674 			continue;
2675 		}
2676 
2677 		rxcb->is_first_msdu = !!(le32_to_cpu(desc->rx_msdu_info.info0) &
2678 					 RX_MSDU_DESC_INFO0_FIRST_MSDU_IN_MPDU);
2679 		rxcb->is_last_msdu = !!(le32_to_cpu(desc->rx_msdu_info.info0) &
2680 					RX_MSDU_DESC_INFO0_LAST_MSDU_IN_MPDU);
2681 		rxcb->is_continuation = !!(le32_to_cpu(desc->rx_msdu_info.info0) &
2682 					   RX_MSDU_DESC_INFO0_MSDU_CONTINUATION);
2683 		rxcb->mac_id = mac_id;
2684 		rxcb->peer_id = le32_get_bits(desc->rx_mpdu_info.peer_meta_data,
2685 					      RX_MPDU_DESC_META_DATA_PEER_ID);
2686 		rxcb->tid = le32_get_bits(desc->rx_mpdu_info.info0,
2687 					  RX_MPDU_DESC_INFO0_TID);
2688 
2689 		__skb_queue_tail(&msdu_list, msdu);
2690 
2691 		if (!rxcb->is_continuation) {
2692 			total_msdu_reaped++;
2693 			done = true;
2694 		} else {
2695 			done = false;
2696 		}
2697 
2698 		if (total_msdu_reaped >= budget)
2699 			break;
2700 	}
2701 
2702 	/* Hw might have updated the head pointer after we cached it.
2703 	 * In this case, even though there are entries in the ring we'll
2704 	 * get rx_desc NULL. Give the read another try with updated cached
2705 	 * head pointer so that we can reap complete MPDU in the current
2706 	 * rx processing.
2707 	 */
2708 	if (!done && ath12k_hal_srng_dst_num_free(ab, srng, true)) {
2709 		ath12k_hal_srng_access_end(ab, srng);
2710 		goto try_again;
2711 	}
2712 
2713 	ath12k_hal_srng_access_end(ab, srng);
2714 
2715 	spin_unlock_bh(&srng->lock);
2716 
2717 	if (!total_msdu_reaped)
2718 		goto exit;
2719 
2720 	/* TODO: Move to implicit BM? */
2721 	ath12k_dp_rx_bufs_replenish(ab, 0, rx_ring, num_buffs_reaped,
2722 				    ab->hw_params->hal_params->rx_buf_rbm, true);
2723 
2724 	ath12k_dp_rx_process_received_packets(ab, napi, &msdu_list,
2725 					      ring_id);
2726 
2727 exit:
2728 	return total_msdu_reaped;
2729 }
2730 
2731 static void ath12k_dp_rx_frag_timer(struct timer_list *timer)
2732 {
2733 	struct ath12k_dp_rx_tid *rx_tid = from_timer(rx_tid, timer, frag_timer);
2734 
2735 	spin_lock_bh(&rx_tid->ab->base_lock);
2736 	if (rx_tid->last_frag_no &&
2737 	    rx_tid->rx_frag_bitmap == GENMASK(rx_tid->last_frag_no, 0)) {
2738 		spin_unlock_bh(&rx_tid->ab->base_lock);
2739 		return;
2740 	}
2741 	ath12k_dp_rx_frags_cleanup(rx_tid, true);
2742 	spin_unlock_bh(&rx_tid->ab->base_lock);
2743 }
2744 
2745 int ath12k_dp_rx_peer_frag_setup(struct ath12k *ar, const u8 *peer_mac, int vdev_id)
2746 {
2747 	struct ath12k_base *ab = ar->ab;
2748 	struct crypto_shash *tfm;
2749 	struct ath12k_peer *peer;
2750 	struct ath12k_dp_rx_tid *rx_tid;
2751 	int i;
2752 
2753 	tfm = crypto_alloc_shash("michael_mic", 0, 0);
2754 	if (IS_ERR(tfm))
2755 		return PTR_ERR(tfm);
2756 
2757 	spin_lock_bh(&ab->base_lock);
2758 
2759 	peer = ath12k_peer_find(ab, vdev_id, peer_mac);
2760 	if (!peer) {
2761 		spin_unlock_bh(&ab->base_lock);
2762 		crypto_free_shash(tfm);
2763 		ath12k_warn(ab, "failed to find the peer to set up fragment info\n");
2764 		return -ENOENT;
2765 	}
2766 
2767 	for (i = 0; i <= IEEE80211_NUM_TIDS; i++) {
2768 		rx_tid = &peer->rx_tid[i];
2769 		rx_tid->ab = ab;
2770 		timer_setup(&rx_tid->frag_timer, ath12k_dp_rx_frag_timer, 0);
2771 		skb_queue_head_init(&rx_tid->rx_frags);
2772 	}
2773 
2774 	peer->tfm_mmic = tfm;
2775 	peer->dp_setup_done = true;
2776 	spin_unlock_bh(&ab->base_lock);
2777 
2778 	return 0;
2779 }
2780 
2781 static int ath12k_dp_rx_h_michael_mic(struct crypto_shash *tfm, u8 *key,
2782 				      struct ieee80211_hdr *hdr, u8 *data,
2783 				      size_t data_len, u8 *mic)
2784 {
2785 	SHASH_DESC_ON_STACK(desc, tfm);
2786 	u8 mic_hdr[16] = {0};
2787 	u8 tid = 0;
2788 	int ret;
2789 
2790 	if (!tfm)
2791 		return -EINVAL;
2792 
2793 	desc->tfm = tfm;
2794 
2795 	ret = crypto_shash_setkey(tfm, key, 8);
2796 	if (ret)
2797 		goto out;
2798 
2799 	ret = crypto_shash_init(desc);
2800 	if (ret)
2801 		goto out;
2802 
2803 	/* TKIP MIC header */
2804 	memcpy(mic_hdr, ieee80211_get_DA(hdr), ETH_ALEN);
2805 	memcpy(mic_hdr + ETH_ALEN, ieee80211_get_SA(hdr), ETH_ALEN);
2806 	if (ieee80211_is_data_qos(hdr->frame_control))
2807 		tid = ieee80211_get_tid(hdr);
2808 	mic_hdr[12] = tid;
2809 
2810 	ret = crypto_shash_update(desc, mic_hdr, 16);
2811 	if (ret)
2812 		goto out;
2813 	ret = crypto_shash_update(desc, data, data_len);
2814 	if (ret)
2815 		goto out;
2816 	ret = crypto_shash_final(desc, mic);
2817 out:
2818 	shash_desc_zero(desc);
2819 	return ret;
2820 }
2821 
2822 static int ath12k_dp_rx_h_verify_tkip_mic(struct ath12k *ar, struct ath12k_peer *peer,
2823 					  struct sk_buff *msdu)
2824 {
2825 	struct ath12k_base *ab = ar->ab;
2826 	struct hal_rx_desc *rx_desc = (struct hal_rx_desc *)msdu->data;
2827 	struct ieee80211_rx_status *rxs = IEEE80211_SKB_RXCB(msdu);
2828 	struct ieee80211_key_conf *key_conf;
2829 	struct ieee80211_hdr *hdr;
2830 	u8 mic[IEEE80211_CCMP_MIC_LEN];
2831 	int head_len, tail_len, ret;
2832 	size_t data_len;
2833 	u32 hdr_len, hal_rx_desc_sz = ar->ab->hw_params->hal_desc_sz;
2834 	u8 *key, *data;
2835 	u8 key_idx;
2836 
2837 	if (ath12k_dp_rx_h_enctype(ab, rx_desc) != HAL_ENCRYPT_TYPE_TKIP_MIC)
2838 		return 0;
2839 
2840 	hdr = (struct ieee80211_hdr *)(msdu->data + hal_rx_desc_sz);
2841 	hdr_len = ieee80211_hdrlen(hdr->frame_control);
2842 	head_len = hdr_len + hal_rx_desc_sz + IEEE80211_TKIP_IV_LEN;
2843 	tail_len = IEEE80211_CCMP_MIC_LEN + IEEE80211_TKIP_ICV_LEN + FCS_LEN;
2844 
2845 	if (!is_multicast_ether_addr(hdr->addr1))
2846 		key_idx = peer->ucast_keyidx;
2847 	else
2848 		key_idx = peer->mcast_keyidx;
2849 
2850 	key_conf = peer->keys[key_idx];
2851 
2852 	data = msdu->data + head_len;
2853 	data_len = msdu->len - head_len - tail_len;
2854 	key = &key_conf->key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY];
2855 
2856 	ret = ath12k_dp_rx_h_michael_mic(peer->tfm_mmic, key, hdr, data, data_len, mic);
2857 	if (ret || memcmp(mic, data + data_len, IEEE80211_CCMP_MIC_LEN))
2858 		goto mic_fail;
2859 
2860 	return 0;
2861 
2862 mic_fail:
2863 	(ATH12K_SKB_RXCB(msdu))->is_first_msdu = true;
2864 	(ATH12K_SKB_RXCB(msdu))->is_last_msdu = true;
2865 
2866 	rxs->flag |= RX_FLAG_MMIC_ERROR | RX_FLAG_MMIC_STRIPPED |
2867 		    RX_FLAG_IV_STRIPPED | RX_FLAG_DECRYPTED;
2868 	skb_pull(msdu, hal_rx_desc_sz);
2869 
2870 	ath12k_dp_rx_h_ppdu(ar, rx_desc, rxs);
2871 	ath12k_dp_rx_h_undecap(ar, msdu, rx_desc,
2872 			       HAL_ENCRYPT_TYPE_TKIP_MIC, rxs, true);
2873 	ieee80211_rx(ar->hw, msdu);
2874 	return -EINVAL;
2875 }
2876 
2877 static void ath12k_dp_rx_h_undecap_frag(struct ath12k *ar, struct sk_buff *msdu,
2878 					enum hal_encrypt_type enctype, u32 flags)
2879 {
2880 	struct ieee80211_hdr *hdr;
2881 	size_t hdr_len;
2882 	size_t crypto_len;
2883 	u32 hal_rx_desc_sz = ar->ab->hw_params->hal_desc_sz;
2884 
2885 	if (!flags)
2886 		return;
2887 
2888 	hdr = (struct ieee80211_hdr *)(msdu->data + hal_rx_desc_sz);
2889 
2890 	if (flags & RX_FLAG_MIC_STRIPPED)
2891 		skb_trim(msdu, msdu->len -
2892 			 ath12k_dp_rx_crypto_mic_len(ar, enctype));
2893 
2894 	if (flags & RX_FLAG_ICV_STRIPPED)
2895 		skb_trim(msdu, msdu->len -
2896 			 ath12k_dp_rx_crypto_icv_len(ar, enctype));
2897 
2898 	if (flags & RX_FLAG_IV_STRIPPED) {
2899 		hdr_len = ieee80211_hdrlen(hdr->frame_control);
2900 		crypto_len = ath12k_dp_rx_crypto_param_len(ar, enctype);
2901 
2902 		memmove(msdu->data + hal_rx_desc_sz + crypto_len,
2903 			msdu->data + hal_rx_desc_sz, hdr_len);
2904 		skb_pull(msdu, crypto_len);
2905 	}
2906 }
2907 
2908 static int ath12k_dp_rx_h_defrag(struct ath12k *ar,
2909 				 struct ath12k_peer *peer,
2910 				 struct ath12k_dp_rx_tid *rx_tid,
2911 				 struct sk_buff **defrag_skb)
2912 {
2913 	struct ath12k_base *ab = ar->ab;
2914 	struct hal_rx_desc *rx_desc;
2915 	struct sk_buff *skb, *first_frag, *last_frag;
2916 	struct ieee80211_hdr *hdr;
2917 	enum hal_encrypt_type enctype;
2918 	bool is_decrypted = false;
2919 	int msdu_len = 0;
2920 	int extra_space;
2921 	u32 flags, hal_rx_desc_sz = ar->ab->hw_params->hal_desc_sz;
2922 
2923 	first_frag = skb_peek(&rx_tid->rx_frags);
2924 	last_frag = skb_peek_tail(&rx_tid->rx_frags);
2925 
2926 	skb_queue_walk(&rx_tid->rx_frags, skb) {
2927 		flags = 0;
2928 		rx_desc = (struct hal_rx_desc *)skb->data;
2929 		hdr = (struct ieee80211_hdr *)(skb->data + hal_rx_desc_sz);
2930 
2931 		enctype = ath12k_dp_rx_h_enctype(ab, rx_desc);
2932 		if (enctype != HAL_ENCRYPT_TYPE_OPEN)
2933 			is_decrypted = ath12k_dp_rx_h_is_decrypted(ab,
2934 								   rx_desc);
2935 
2936 		if (is_decrypted) {
2937 			if (skb != first_frag)
2938 				flags |= RX_FLAG_IV_STRIPPED;
2939 			if (skb != last_frag)
2940 				flags |= RX_FLAG_ICV_STRIPPED |
2941 					 RX_FLAG_MIC_STRIPPED;
2942 		}
2943 
2944 		/* RX fragments are always raw packets */
2945 		if (skb != last_frag)
2946 			skb_trim(skb, skb->len - FCS_LEN);
2947 		ath12k_dp_rx_h_undecap_frag(ar, skb, enctype, flags);
2948 
2949 		if (skb != first_frag)
2950 			skb_pull(skb, hal_rx_desc_sz +
2951 				      ieee80211_hdrlen(hdr->frame_control));
2952 		msdu_len += skb->len;
2953 	}
2954 
2955 	extra_space = msdu_len - (DP_RX_BUFFER_SIZE + skb_tailroom(first_frag));
2956 	if (extra_space > 0 &&
2957 	    (pskb_expand_head(first_frag, 0, extra_space, GFP_ATOMIC) < 0))
2958 		return -ENOMEM;
2959 
2960 	__skb_unlink(first_frag, &rx_tid->rx_frags);
2961 	while ((skb = __skb_dequeue(&rx_tid->rx_frags))) {
2962 		skb_put_data(first_frag, skb->data, skb->len);
2963 		dev_kfree_skb_any(skb);
2964 	}
2965 
2966 	hdr = (struct ieee80211_hdr *)(first_frag->data + hal_rx_desc_sz);
2967 	hdr->frame_control &= ~__cpu_to_le16(IEEE80211_FCTL_MOREFRAGS);
2968 	ATH12K_SKB_RXCB(first_frag)->is_frag = 1;
2969 
2970 	if (ath12k_dp_rx_h_verify_tkip_mic(ar, peer, first_frag))
2971 		first_frag = NULL;
2972 
2973 	*defrag_skb = first_frag;
2974 	return 0;
2975 }
2976 
2977 static int ath12k_dp_rx_h_defrag_reo_reinject(struct ath12k *ar,
2978 					      struct ath12k_dp_rx_tid *rx_tid,
2979 					      struct sk_buff *defrag_skb)
2980 {
2981 	struct ath12k_base *ab = ar->ab;
2982 	struct ath12k_dp *dp = &ab->dp;
2983 	struct hal_rx_desc *rx_desc = (struct hal_rx_desc *)defrag_skb->data;
2984 	struct hal_reo_entrance_ring *reo_ent_ring;
2985 	struct hal_reo_dest_ring *reo_dest_ring;
2986 	struct dp_link_desc_bank *link_desc_banks;
2987 	struct hal_rx_msdu_link *msdu_link;
2988 	struct hal_rx_msdu_details *msdu0;
2989 	struct hal_srng *srng;
2990 	dma_addr_t link_paddr, buf_paddr;
2991 	u32 desc_bank, msdu_info, msdu_ext_info, mpdu_info;
2992 	u32 cookie, hal_rx_desc_sz, dest_ring_info0, queue_addr_hi;
2993 	int ret;
2994 	struct ath12k_rx_desc_info *desc_info;
2995 	u8 dst_ind;
2996 
2997 	hal_rx_desc_sz = ab->hw_params->hal_desc_sz;
2998 	link_desc_banks = dp->link_desc_banks;
2999 	reo_dest_ring = rx_tid->dst_ring_desc;
3000 
3001 	ath12k_hal_rx_reo_ent_paddr_get(ab, &reo_dest_ring->buf_addr_info,
3002 					&link_paddr, &cookie);
3003 	desc_bank = u32_get_bits(cookie, DP_LINK_DESC_BANK_MASK);
3004 
3005 	msdu_link = (struct hal_rx_msdu_link *)(link_desc_banks[desc_bank].vaddr +
3006 			(link_paddr - link_desc_banks[desc_bank].paddr));
3007 	msdu0 = &msdu_link->msdu_link[0];
3008 	msdu_ext_info = le32_to_cpu(msdu0->rx_msdu_ext_info.info0);
3009 	dst_ind = u32_get_bits(msdu_ext_info, RX_MSDU_EXT_DESC_INFO0_REO_DEST_IND);
3010 
3011 	memset(msdu0, 0, sizeof(*msdu0));
3012 
3013 	msdu_info = u32_encode_bits(1, RX_MSDU_DESC_INFO0_FIRST_MSDU_IN_MPDU) |
3014 		    u32_encode_bits(1, RX_MSDU_DESC_INFO0_LAST_MSDU_IN_MPDU) |
3015 		    u32_encode_bits(0, RX_MSDU_DESC_INFO0_MSDU_CONTINUATION) |
3016 		    u32_encode_bits(defrag_skb->len - hal_rx_desc_sz,
3017 				    RX_MSDU_DESC_INFO0_MSDU_LENGTH) |
3018 		    u32_encode_bits(1, RX_MSDU_DESC_INFO0_VALID_SA) |
3019 		    u32_encode_bits(1, RX_MSDU_DESC_INFO0_VALID_DA);
3020 	msdu0->rx_msdu_info.info0 = cpu_to_le32(msdu_info);
3021 	msdu0->rx_msdu_ext_info.info0 = cpu_to_le32(msdu_ext_info);
3022 
3023 	/* change msdu len in hal rx desc */
3024 	ath12k_dp_rxdesc_set_msdu_len(ab, rx_desc, defrag_skb->len - hal_rx_desc_sz);
3025 
3026 	buf_paddr = dma_map_single(ab->dev, defrag_skb->data,
3027 				   defrag_skb->len + skb_tailroom(defrag_skb),
3028 				   DMA_TO_DEVICE);
3029 	if (dma_mapping_error(ab->dev, buf_paddr))
3030 		return -ENOMEM;
3031 
3032 	spin_lock_bh(&dp->rx_desc_lock);
3033 	desc_info = list_first_entry_or_null(&dp->rx_desc_free_list,
3034 					     struct ath12k_rx_desc_info,
3035 					     list);
3036 	if (!desc_info) {
3037 		spin_unlock_bh(&dp->rx_desc_lock);
3038 		ath12k_warn(ab, "failed to find rx desc for reinject\n");
3039 		ret = -ENOMEM;
3040 		goto err_unmap_dma;
3041 	}
3042 
3043 	desc_info->skb = defrag_skb;
3044 
3045 	list_del(&desc_info->list);
3046 	list_add_tail(&desc_info->list, &dp->rx_desc_used_list);
3047 	spin_unlock_bh(&dp->rx_desc_lock);
3048 
3049 	ATH12K_SKB_RXCB(defrag_skb)->paddr = buf_paddr;
3050 
3051 	ath12k_hal_rx_buf_addr_info_set(&msdu0->buf_addr_info, buf_paddr,
3052 					desc_info->cookie,
3053 					HAL_RX_BUF_RBM_SW3_BM);
3054 
3055 	/* Fill mpdu details into reo entrance ring */
3056 	srng = &ab->hal.srng_list[dp->reo_reinject_ring.ring_id];
3057 
3058 	spin_lock_bh(&srng->lock);
3059 	ath12k_hal_srng_access_begin(ab, srng);
3060 
3061 	reo_ent_ring = ath12k_hal_srng_src_get_next_entry(ab, srng);
3062 	if (!reo_ent_ring) {
3063 		ath12k_hal_srng_access_end(ab, srng);
3064 		spin_unlock_bh(&srng->lock);
3065 		ret = -ENOSPC;
3066 		goto err_free_desc;
3067 	}
3068 	memset(reo_ent_ring, 0, sizeof(*reo_ent_ring));
3069 
3070 	ath12k_hal_rx_buf_addr_info_set(&reo_ent_ring->buf_addr_info, link_paddr,
3071 					cookie,
3072 					HAL_RX_BUF_RBM_WBM_CHIP0_IDLE_DESC_LIST);
3073 
3074 	mpdu_info = u32_encode_bits(1, RX_MPDU_DESC_INFO0_MSDU_COUNT) |
3075 		    u32_encode_bits(0, RX_MPDU_DESC_INFO0_FRAG_FLAG) |
3076 		    u32_encode_bits(1, RX_MPDU_DESC_INFO0_RAW_MPDU) |
3077 		    u32_encode_bits(1, RX_MPDU_DESC_INFO0_VALID_PN) |
3078 		    u32_encode_bits(rx_tid->tid, RX_MPDU_DESC_INFO0_TID);
3079 
3080 	reo_ent_ring->rx_mpdu_info.info0 = cpu_to_le32(mpdu_info);
3081 	reo_ent_ring->rx_mpdu_info.peer_meta_data =
3082 		reo_dest_ring->rx_mpdu_info.peer_meta_data;
3083 
3084 	reo_ent_ring->queue_addr_lo = cpu_to_le32(lower_32_bits(rx_tid->paddr));
3085 	queue_addr_hi = upper_32_bits(rx_tid->paddr);
3086 	reo_ent_ring->info0 = le32_encode_bits(queue_addr_hi,
3087 					       HAL_REO_ENTR_RING_INFO0_QUEUE_ADDR_HI) |
3088 			      le32_encode_bits(dst_ind,
3089 					       HAL_REO_ENTR_RING_INFO0_DEST_IND);
3090 
3091 	reo_ent_ring->info1 = le32_encode_bits(rx_tid->cur_sn,
3092 					       HAL_REO_ENTR_RING_INFO1_MPDU_SEQ_NUM);
3093 	dest_ring_info0 = le32_get_bits(reo_dest_ring->info0,
3094 					HAL_REO_DEST_RING_INFO0_SRC_LINK_ID);
3095 	reo_ent_ring->info2 =
3096 		cpu_to_le32(u32_get_bits(dest_ring_info0,
3097 					 HAL_REO_ENTR_RING_INFO2_SRC_LINK_ID));
3098 
3099 	ath12k_hal_srng_access_end(ab, srng);
3100 	spin_unlock_bh(&srng->lock);
3101 
3102 	return 0;
3103 
3104 err_free_desc:
3105 	spin_lock_bh(&dp->rx_desc_lock);
3106 	list_del(&desc_info->list);
3107 	list_add_tail(&desc_info->list, &dp->rx_desc_free_list);
3108 	desc_info->skb = NULL;
3109 	spin_unlock_bh(&dp->rx_desc_lock);
3110 err_unmap_dma:
3111 	dma_unmap_single(ab->dev, buf_paddr, defrag_skb->len + skb_tailroom(defrag_skb),
3112 			 DMA_TO_DEVICE);
3113 	return ret;
3114 }
3115 
3116 static int ath12k_dp_rx_h_cmp_frags(struct ath12k_base *ab,
3117 				    struct sk_buff *a, struct sk_buff *b)
3118 {
3119 	int frag1, frag2;
3120 
3121 	frag1 = ath12k_dp_rx_h_frag_no(ab, a);
3122 	frag2 = ath12k_dp_rx_h_frag_no(ab, b);
3123 
3124 	return frag1 - frag2;
3125 }
3126 
3127 static void ath12k_dp_rx_h_sort_frags(struct ath12k_base *ab,
3128 				      struct sk_buff_head *frag_list,
3129 				      struct sk_buff *cur_frag)
3130 {
3131 	struct sk_buff *skb;
3132 	int cmp;
3133 
3134 	skb_queue_walk(frag_list, skb) {
3135 		cmp = ath12k_dp_rx_h_cmp_frags(ab, skb, cur_frag);
3136 		if (cmp < 0)
3137 			continue;
3138 		__skb_queue_before(frag_list, skb, cur_frag);
3139 		return;
3140 	}
3141 	__skb_queue_tail(frag_list, cur_frag);
3142 }
3143 
3144 static u64 ath12k_dp_rx_h_get_pn(struct ath12k *ar, struct sk_buff *skb)
3145 {
3146 	struct ieee80211_hdr *hdr;
3147 	u64 pn = 0;
3148 	u8 *ehdr;
3149 	u32 hal_rx_desc_sz = ar->ab->hw_params->hal_desc_sz;
3150 
3151 	hdr = (struct ieee80211_hdr *)(skb->data + hal_rx_desc_sz);
3152 	ehdr = skb->data + hal_rx_desc_sz + ieee80211_hdrlen(hdr->frame_control);
3153 
3154 	pn = ehdr[0];
3155 	pn |= (u64)ehdr[1] << 8;
3156 	pn |= (u64)ehdr[4] << 16;
3157 	pn |= (u64)ehdr[5] << 24;
3158 	pn |= (u64)ehdr[6] << 32;
3159 	pn |= (u64)ehdr[7] << 40;
3160 
3161 	return pn;
3162 }
3163 
3164 static bool
3165 ath12k_dp_rx_h_defrag_validate_incr_pn(struct ath12k *ar, struct ath12k_dp_rx_tid *rx_tid)
3166 {
3167 	struct ath12k_base *ab = ar->ab;
3168 	enum hal_encrypt_type encrypt_type;
3169 	struct sk_buff *first_frag, *skb;
3170 	struct hal_rx_desc *desc;
3171 	u64 last_pn;
3172 	u64 cur_pn;
3173 
3174 	first_frag = skb_peek(&rx_tid->rx_frags);
3175 	desc = (struct hal_rx_desc *)first_frag->data;
3176 
3177 	encrypt_type = ath12k_dp_rx_h_enctype(ab, desc);
3178 	if (encrypt_type != HAL_ENCRYPT_TYPE_CCMP_128 &&
3179 	    encrypt_type != HAL_ENCRYPT_TYPE_CCMP_256 &&
3180 	    encrypt_type != HAL_ENCRYPT_TYPE_GCMP_128 &&
3181 	    encrypt_type != HAL_ENCRYPT_TYPE_AES_GCMP_256)
3182 		return true;
3183 
3184 	last_pn = ath12k_dp_rx_h_get_pn(ar, first_frag);
3185 	skb_queue_walk(&rx_tid->rx_frags, skb) {
3186 		if (skb == first_frag)
3187 			continue;
3188 
3189 		cur_pn = ath12k_dp_rx_h_get_pn(ar, skb);
3190 		if (cur_pn != last_pn + 1)
3191 			return false;
3192 		last_pn = cur_pn;
3193 	}
3194 	return true;
3195 }
3196 
3197 static int ath12k_dp_rx_frag_h_mpdu(struct ath12k *ar,
3198 				    struct sk_buff *msdu,
3199 				    struct hal_reo_dest_ring *ring_desc)
3200 {
3201 	struct ath12k_base *ab = ar->ab;
3202 	struct hal_rx_desc *rx_desc;
3203 	struct ath12k_peer *peer;
3204 	struct ath12k_dp_rx_tid *rx_tid;
3205 	struct sk_buff *defrag_skb = NULL;
3206 	u32 peer_id;
3207 	u16 seqno, frag_no;
3208 	u8 tid;
3209 	int ret = 0;
3210 	bool more_frags;
3211 
3212 	rx_desc = (struct hal_rx_desc *)msdu->data;
3213 	peer_id = ath12k_dp_rx_h_peer_id(ab, rx_desc);
3214 	tid = ath12k_dp_rx_h_tid(ab, rx_desc);
3215 	seqno = ath12k_dp_rx_h_seq_no(ab, rx_desc);
3216 	frag_no = ath12k_dp_rx_h_frag_no(ab, msdu);
3217 	more_frags = ath12k_dp_rx_h_more_frags(ab, msdu);
3218 
3219 	if (!ath12k_dp_rx_h_seq_ctrl_valid(ab, rx_desc) ||
3220 	    !ath12k_dp_rx_h_fc_valid(ab, rx_desc) ||
3221 	    tid > IEEE80211_NUM_TIDS)
3222 		return -EINVAL;
3223 
3224 	/* received unfragmented packet in reo
3225 	 * exception ring, this shouldn't happen
3226 	 * as these packets typically come from
3227 	 * reo2sw srngs.
3228 	 */
3229 	if (WARN_ON_ONCE(!frag_no && !more_frags))
3230 		return -EINVAL;
3231 
3232 	spin_lock_bh(&ab->base_lock);
3233 	peer = ath12k_peer_find_by_id(ab, peer_id);
3234 	if (!peer) {
3235 		ath12k_warn(ab, "failed to find the peer to de-fragment received fragment peer_id %d\n",
3236 			    peer_id);
3237 		ret = -ENOENT;
3238 		goto out_unlock;
3239 	}
3240 
3241 	if (!peer->dp_setup_done) {
3242 		ath12k_warn(ab, "The peer %pM [%d] has uninitialized datapath\n",
3243 			    peer->addr, peer_id);
3244 		ret = -ENOENT;
3245 		goto out_unlock;
3246 	}
3247 
3248 	rx_tid = &peer->rx_tid[tid];
3249 
3250 	if ((!skb_queue_empty(&rx_tid->rx_frags) && seqno != rx_tid->cur_sn) ||
3251 	    skb_queue_empty(&rx_tid->rx_frags)) {
3252 		/* Flush stored fragments and start a new sequence */
3253 		ath12k_dp_rx_frags_cleanup(rx_tid, true);
3254 		rx_tid->cur_sn = seqno;
3255 	}
3256 
3257 	if (rx_tid->rx_frag_bitmap & BIT(frag_no)) {
3258 		/* Fragment already present */
3259 		ret = -EINVAL;
3260 		goto out_unlock;
3261 	}
3262 
3263 	if ((!rx_tid->rx_frag_bitmap || frag_no > __fls(rx_tid->rx_frag_bitmap)))
3264 		__skb_queue_tail(&rx_tid->rx_frags, msdu);
3265 	else
3266 		ath12k_dp_rx_h_sort_frags(ab, &rx_tid->rx_frags, msdu);
3267 
3268 	rx_tid->rx_frag_bitmap |= BIT(frag_no);
3269 	if (!more_frags)
3270 		rx_tid->last_frag_no = frag_no;
3271 
3272 	if (frag_no == 0) {
3273 		rx_tid->dst_ring_desc = kmemdup(ring_desc,
3274 						sizeof(*rx_tid->dst_ring_desc),
3275 						GFP_ATOMIC);
3276 		if (!rx_tid->dst_ring_desc) {
3277 			ret = -ENOMEM;
3278 			goto out_unlock;
3279 		}
3280 	} else {
3281 		ath12k_dp_rx_link_desc_return(ab, ring_desc,
3282 					      HAL_WBM_REL_BM_ACT_PUT_IN_IDLE);
3283 	}
3284 
3285 	if (!rx_tid->last_frag_no ||
3286 	    rx_tid->rx_frag_bitmap != GENMASK(rx_tid->last_frag_no, 0)) {
3287 		mod_timer(&rx_tid->frag_timer, jiffies +
3288 					       ATH12K_DP_RX_FRAGMENT_TIMEOUT_MS);
3289 		goto out_unlock;
3290 	}
3291 
3292 	spin_unlock_bh(&ab->base_lock);
3293 	del_timer_sync(&rx_tid->frag_timer);
3294 	spin_lock_bh(&ab->base_lock);
3295 
3296 	peer = ath12k_peer_find_by_id(ab, peer_id);
3297 	if (!peer)
3298 		goto err_frags_cleanup;
3299 
3300 	if (!ath12k_dp_rx_h_defrag_validate_incr_pn(ar, rx_tid))
3301 		goto err_frags_cleanup;
3302 
3303 	if (ath12k_dp_rx_h_defrag(ar, peer, rx_tid, &defrag_skb))
3304 		goto err_frags_cleanup;
3305 
3306 	if (!defrag_skb)
3307 		goto err_frags_cleanup;
3308 
3309 	if (ath12k_dp_rx_h_defrag_reo_reinject(ar, rx_tid, defrag_skb))
3310 		goto err_frags_cleanup;
3311 
3312 	ath12k_dp_rx_frags_cleanup(rx_tid, false);
3313 	goto out_unlock;
3314 
3315 err_frags_cleanup:
3316 	dev_kfree_skb_any(defrag_skb);
3317 	ath12k_dp_rx_frags_cleanup(rx_tid, true);
3318 out_unlock:
3319 	spin_unlock_bh(&ab->base_lock);
3320 	return ret;
3321 }
3322 
3323 static int
3324 ath12k_dp_process_rx_err_buf(struct ath12k *ar, struct hal_reo_dest_ring *desc,
3325 			     bool drop, u32 cookie)
3326 {
3327 	struct ath12k_base *ab = ar->ab;
3328 	struct sk_buff *msdu;
3329 	struct ath12k_skb_rxcb *rxcb;
3330 	struct hal_rx_desc *rx_desc;
3331 	u16 msdu_len;
3332 	u32 hal_rx_desc_sz = ab->hw_params->hal_desc_sz;
3333 	struct ath12k_rx_desc_info *desc_info;
3334 	u64 desc_va;
3335 
3336 	desc_va = ((u64)le32_to_cpu(desc->buf_va_hi) << 32 |
3337 		   le32_to_cpu(desc->buf_va_lo));
3338 	desc_info = (struct ath12k_rx_desc_info *)((unsigned long)desc_va);
3339 
3340 	/* retry manual desc retrieval */
3341 	if (!desc_info) {
3342 		desc_info = ath12k_dp_get_rx_desc(ab, cookie);
3343 		if (!desc_info) {
3344 			ath12k_warn(ab, "Invalid cookie in manual desc retrieval");
3345 			return -EINVAL;
3346 		}
3347 	}
3348 
3349 	if (desc_info->magic != ATH12K_DP_RX_DESC_MAGIC)
3350 		ath12k_warn(ab, " RX Exception, Check HW CC implementation");
3351 
3352 	msdu = desc_info->skb;
3353 	desc_info->skb = NULL;
3354 	spin_lock_bh(&ab->dp.rx_desc_lock);
3355 	list_move_tail(&desc_info->list, &ab->dp.rx_desc_free_list);
3356 	spin_unlock_bh(&ab->dp.rx_desc_lock);
3357 
3358 	rxcb = ATH12K_SKB_RXCB(msdu);
3359 	dma_unmap_single(ar->ab->dev, rxcb->paddr,
3360 			 msdu->len + skb_tailroom(msdu),
3361 			 DMA_FROM_DEVICE);
3362 
3363 	if (drop) {
3364 		dev_kfree_skb_any(msdu);
3365 		return 0;
3366 	}
3367 
3368 	rcu_read_lock();
3369 	if (!rcu_dereference(ar->ab->pdevs_active[ar->pdev_idx])) {
3370 		dev_kfree_skb_any(msdu);
3371 		goto exit;
3372 	}
3373 
3374 	if (test_bit(ATH12K_CAC_RUNNING, &ar->dev_flags)) {
3375 		dev_kfree_skb_any(msdu);
3376 		goto exit;
3377 	}
3378 
3379 	rx_desc = (struct hal_rx_desc *)msdu->data;
3380 	msdu_len = ath12k_dp_rx_h_msdu_len(ar->ab, rx_desc);
3381 	if ((msdu_len + hal_rx_desc_sz) > DP_RX_BUFFER_SIZE) {
3382 		ath12k_warn(ar->ab, "invalid msdu leng %u", msdu_len);
3383 		ath12k_dbg_dump(ar->ab, ATH12K_DBG_DATA, NULL, "", rx_desc,
3384 				sizeof(*rx_desc));
3385 		dev_kfree_skb_any(msdu);
3386 		goto exit;
3387 	}
3388 
3389 	skb_put(msdu, hal_rx_desc_sz + msdu_len);
3390 
3391 	if (ath12k_dp_rx_frag_h_mpdu(ar, msdu, desc)) {
3392 		dev_kfree_skb_any(msdu);
3393 		ath12k_dp_rx_link_desc_return(ar->ab, desc,
3394 					      HAL_WBM_REL_BM_ACT_PUT_IN_IDLE);
3395 	}
3396 exit:
3397 	rcu_read_unlock();
3398 	return 0;
3399 }
3400 
3401 int ath12k_dp_rx_process_err(struct ath12k_base *ab, struct napi_struct *napi,
3402 			     int budget)
3403 {
3404 	u32 msdu_cookies[HAL_NUM_RX_MSDUS_PER_LINK_DESC];
3405 	struct dp_link_desc_bank *link_desc_banks;
3406 	enum hal_rx_buf_return_buf_manager rbm;
3407 	struct hal_rx_msdu_link *link_desc_va;
3408 	int tot_n_bufs_reaped, quota, ret, i;
3409 	struct hal_reo_dest_ring *reo_desc;
3410 	struct dp_rxdma_ring *rx_ring;
3411 	struct dp_srng *reo_except;
3412 	u32 desc_bank, num_msdus;
3413 	struct hal_srng *srng;
3414 	struct ath12k_dp *dp;
3415 	int mac_id;
3416 	struct ath12k *ar;
3417 	dma_addr_t paddr;
3418 	bool is_frag;
3419 	bool drop = false;
3420 	int pdev_id;
3421 
3422 	tot_n_bufs_reaped = 0;
3423 	quota = budget;
3424 
3425 	dp = &ab->dp;
3426 	reo_except = &dp->reo_except_ring;
3427 	link_desc_banks = dp->link_desc_banks;
3428 
3429 	srng = &ab->hal.srng_list[reo_except->ring_id];
3430 
3431 	spin_lock_bh(&srng->lock);
3432 
3433 	ath12k_hal_srng_access_begin(ab, srng);
3434 
3435 	while (budget &&
3436 	       (reo_desc = ath12k_hal_srng_dst_get_next_entry(ab, srng))) {
3437 		ab->soc_stats.err_ring_pkts++;
3438 		ret = ath12k_hal_desc_reo_parse_err(ab, reo_desc, &paddr,
3439 						    &desc_bank);
3440 		if (ret) {
3441 			ath12k_warn(ab, "failed to parse error reo desc %d\n",
3442 				    ret);
3443 			continue;
3444 		}
3445 		link_desc_va = link_desc_banks[desc_bank].vaddr +
3446 			       (paddr - link_desc_banks[desc_bank].paddr);
3447 		ath12k_hal_rx_msdu_link_info_get(link_desc_va, &num_msdus, msdu_cookies,
3448 						 &rbm);
3449 		if (rbm != HAL_RX_BUF_RBM_WBM_CHIP0_IDLE_DESC_LIST &&
3450 		    rbm != HAL_RX_BUF_RBM_SW3_BM &&
3451 		    rbm != ab->hw_params->hal_params->rx_buf_rbm) {
3452 			ab->soc_stats.invalid_rbm++;
3453 			ath12k_warn(ab, "invalid return buffer manager %d\n", rbm);
3454 			ath12k_dp_rx_link_desc_return(ab, reo_desc,
3455 						      HAL_WBM_REL_BM_ACT_REL_MSDU);
3456 			continue;
3457 		}
3458 
3459 		is_frag = !!(le32_to_cpu(reo_desc->rx_mpdu_info.info0) &
3460 			     RX_MPDU_DESC_INFO0_FRAG_FLAG);
3461 
3462 		/* Process only rx fragments with one msdu per link desc below, and drop
3463 		 * msdu's indicated due to error reasons.
3464 		 */
3465 		if (!is_frag || num_msdus > 1) {
3466 			drop = true;
3467 			/* Return the link desc back to wbm idle list */
3468 			ath12k_dp_rx_link_desc_return(ab, reo_desc,
3469 						      HAL_WBM_REL_BM_ACT_PUT_IN_IDLE);
3470 		}
3471 
3472 		for (i = 0; i < num_msdus; i++) {
3473 			mac_id = le32_get_bits(reo_desc->info0,
3474 					       HAL_REO_DEST_RING_INFO0_SRC_LINK_ID);
3475 
3476 			pdev_id = ath12k_hw_mac_id_to_pdev_id(ab->hw_params, mac_id);
3477 			ar = ab->pdevs[pdev_id].ar;
3478 
3479 			if (!ath12k_dp_process_rx_err_buf(ar, reo_desc, drop,
3480 							  msdu_cookies[i]))
3481 				tot_n_bufs_reaped++;
3482 		}
3483 
3484 		if (tot_n_bufs_reaped >= quota) {
3485 			tot_n_bufs_reaped = quota;
3486 			goto exit;
3487 		}
3488 
3489 		budget = quota - tot_n_bufs_reaped;
3490 	}
3491 
3492 exit:
3493 	ath12k_hal_srng_access_end(ab, srng);
3494 
3495 	spin_unlock_bh(&srng->lock);
3496 
3497 	rx_ring = &dp->rx_refill_buf_ring;
3498 
3499 	ath12k_dp_rx_bufs_replenish(ab, 0, rx_ring, tot_n_bufs_reaped,
3500 				    ab->hw_params->hal_params->rx_buf_rbm, true);
3501 
3502 	return tot_n_bufs_reaped;
3503 }
3504 
3505 static void ath12k_dp_rx_null_q_desc_sg_drop(struct ath12k *ar,
3506 					     int msdu_len,
3507 					     struct sk_buff_head *msdu_list)
3508 {
3509 	struct sk_buff *skb, *tmp;
3510 	struct ath12k_skb_rxcb *rxcb;
3511 	int n_buffs;
3512 
3513 	n_buffs = DIV_ROUND_UP(msdu_len,
3514 			       (DP_RX_BUFFER_SIZE - ar->ab->hw_params->hal_desc_sz));
3515 
3516 	skb_queue_walk_safe(msdu_list, skb, tmp) {
3517 		rxcb = ATH12K_SKB_RXCB(skb);
3518 		if (rxcb->err_rel_src == HAL_WBM_REL_SRC_MODULE_REO &&
3519 		    rxcb->err_code == HAL_REO_DEST_RING_ERROR_CODE_DESC_ADDR_ZERO) {
3520 			if (!n_buffs)
3521 				break;
3522 			__skb_unlink(skb, msdu_list);
3523 			dev_kfree_skb_any(skb);
3524 			n_buffs--;
3525 		}
3526 	}
3527 }
3528 
3529 static int ath12k_dp_rx_h_null_q_desc(struct ath12k *ar, struct sk_buff *msdu,
3530 				      struct ieee80211_rx_status *status,
3531 				      struct sk_buff_head *msdu_list)
3532 {
3533 	struct ath12k_base *ab = ar->ab;
3534 	u16 msdu_len, peer_id;
3535 	struct hal_rx_desc *desc = (struct hal_rx_desc *)msdu->data;
3536 	u8 l3pad_bytes;
3537 	struct ath12k_skb_rxcb *rxcb = ATH12K_SKB_RXCB(msdu);
3538 	u32 hal_rx_desc_sz = ar->ab->hw_params->hal_desc_sz;
3539 
3540 	msdu_len = ath12k_dp_rx_h_msdu_len(ab, desc);
3541 	peer_id = ath12k_dp_rx_h_peer_id(ab, desc);
3542 
3543 	spin_lock(&ab->base_lock);
3544 	if (!ath12k_peer_find_by_id(ab, peer_id)) {
3545 		spin_unlock(&ab->base_lock);
3546 		ath12k_dbg(ab, ATH12K_DBG_DATA, "invalid peer id received in wbm err pkt%d\n",
3547 			   peer_id);
3548 		return -EINVAL;
3549 	}
3550 	spin_unlock(&ab->base_lock);
3551 
3552 	if (!rxcb->is_frag && ((msdu_len + hal_rx_desc_sz) > DP_RX_BUFFER_SIZE)) {
3553 		/* First buffer will be freed by the caller, so deduct it's length */
3554 		msdu_len = msdu_len - (DP_RX_BUFFER_SIZE - hal_rx_desc_sz);
3555 		ath12k_dp_rx_null_q_desc_sg_drop(ar, msdu_len, msdu_list);
3556 		return -EINVAL;
3557 	}
3558 
3559 	/* Even after cleaning up the sg buffers in the msdu list with above check
3560 	 * any msdu received with continuation flag needs to be dropped as invalid.
3561 	 * This protects against some random err frame with continuation flag.
3562 	 */
3563 	if (rxcb->is_continuation)
3564 		return -EINVAL;
3565 
3566 	if (!ath12k_dp_rx_h_msdu_done(ab, desc)) {
3567 		ath12k_warn(ar->ab,
3568 			    "msdu_done bit not set in null_q_des processing\n");
3569 		__skb_queue_purge(msdu_list);
3570 		return -EIO;
3571 	}
3572 
3573 	/* Handle NULL queue descriptor violations arising out a missing
3574 	 * REO queue for a given peer or a given TID. This typically
3575 	 * may happen if a packet is received on a QOS enabled TID before the
3576 	 * ADDBA negotiation for that TID, when the TID queue is setup. Or
3577 	 * it may also happen for MC/BC frames if they are not routed to the
3578 	 * non-QOS TID queue, in the absence of any other default TID queue.
3579 	 * This error can show up both in a REO destination or WBM release ring.
3580 	 */
3581 
3582 	if (rxcb->is_frag) {
3583 		skb_pull(msdu, hal_rx_desc_sz);
3584 	} else {
3585 		l3pad_bytes = ath12k_dp_rx_h_l3pad(ab, desc);
3586 
3587 		if ((hal_rx_desc_sz + l3pad_bytes + msdu_len) > DP_RX_BUFFER_SIZE)
3588 			return -EINVAL;
3589 
3590 		skb_put(msdu, hal_rx_desc_sz + l3pad_bytes + msdu_len);
3591 		skb_pull(msdu, hal_rx_desc_sz + l3pad_bytes);
3592 	}
3593 	ath12k_dp_rx_h_ppdu(ar, desc, status);
3594 
3595 	ath12k_dp_rx_h_mpdu(ar, msdu, desc, status);
3596 
3597 	rxcb->tid = ath12k_dp_rx_h_tid(ab, desc);
3598 
3599 	/* Please note that caller will having the access to msdu and completing
3600 	 * rx with mac80211. Need not worry about cleaning up amsdu_list.
3601 	 */
3602 
3603 	return 0;
3604 }
3605 
3606 static bool ath12k_dp_rx_h_reo_err(struct ath12k *ar, struct sk_buff *msdu,
3607 				   struct ieee80211_rx_status *status,
3608 				   struct sk_buff_head *msdu_list)
3609 {
3610 	struct ath12k_skb_rxcb *rxcb = ATH12K_SKB_RXCB(msdu);
3611 	bool drop = false;
3612 
3613 	ar->ab->soc_stats.reo_error[rxcb->err_code]++;
3614 
3615 	switch (rxcb->err_code) {
3616 	case HAL_REO_DEST_RING_ERROR_CODE_DESC_ADDR_ZERO:
3617 		if (ath12k_dp_rx_h_null_q_desc(ar, msdu, status, msdu_list))
3618 			drop = true;
3619 		break;
3620 	case HAL_REO_DEST_RING_ERROR_CODE_PN_CHECK_FAILED:
3621 		/* TODO: Do not drop PN failed packets in the driver;
3622 		 * instead, it is good to drop such packets in mac80211
3623 		 * after incrementing the replay counters.
3624 		 */
3625 		fallthrough;
3626 	default:
3627 		/* TODO: Review other errors and process them to mac80211
3628 		 * as appropriate.
3629 		 */
3630 		drop = true;
3631 		break;
3632 	}
3633 
3634 	return drop;
3635 }
3636 
3637 static void ath12k_dp_rx_h_tkip_mic_err(struct ath12k *ar, struct sk_buff *msdu,
3638 					struct ieee80211_rx_status *status)
3639 {
3640 	struct ath12k_base *ab = ar->ab;
3641 	u16 msdu_len;
3642 	struct hal_rx_desc *desc = (struct hal_rx_desc *)msdu->data;
3643 	u8 l3pad_bytes;
3644 	struct ath12k_skb_rxcb *rxcb = ATH12K_SKB_RXCB(msdu);
3645 	u32 hal_rx_desc_sz = ar->ab->hw_params->hal_desc_sz;
3646 
3647 	rxcb->is_first_msdu = ath12k_dp_rx_h_first_msdu(ab, desc);
3648 	rxcb->is_last_msdu = ath12k_dp_rx_h_last_msdu(ab, desc);
3649 
3650 	l3pad_bytes = ath12k_dp_rx_h_l3pad(ab, desc);
3651 	msdu_len = ath12k_dp_rx_h_msdu_len(ab, desc);
3652 	skb_put(msdu, hal_rx_desc_sz + l3pad_bytes + msdu_len);
3653 	skb_pull(msdu, hal_rx_desc_sz + l3pad_bytes);
3654 
3655 	ath12k_dp_rx_h_ppdu(ar, desc, status);
3656 
3657 	status->flag |= (RX_FLAG_MMIC_STRIPPED | RX_FLAG_MMIC_ERROR |
3658 			 RX_FLAG_DECRYPTED);
3659 
3660 	ath12k_dp_rx_h_undecap(ar, msdu, desc,
3661 			       HAL_ENCRYPT_TYPE_TKIP_MIC, status, false);
3662 }
3663 
3664 static bool ath12k_dp_rx_h_rxdma_err(struct ath12k *ar,  struct sk_buff *msdu,
3665 				     struct ieee80211_rx_status *status)
3666 {
3667 	struct ath12k_base *ab = ar->ab;
3668 	struct ath12k_skb_rxcb *rxcb = ATH12K_SKB_RXCB(msdu);
3669 	struct hal_rx_desc *rx_desc = (struct hal_rx_desc *)msdu->data;
3670 	bool drop = false;
3671 	u32 err_bitmap;
3672 
3673 	ar->ab->soc_stats.rxdma_error[rxcb->err_code]++;
3674 
3675 	switch (rxcb->err_code) {
3676 	case HAL_REO_ENTR_RING_RXDMA_ECODE_DECRYPT_ERR:
3677 	case HAL_REO_ENTR_RING_RXDMA_ECODE_TKIP_MIC_ERR:
3678 		err_bitmap = ath12k_dp_rx_h_mpdu_err(ab, rx_desc);
3679 		if (err_bitmap & HAL_RX_MPDU_ERR_TKIP_MIC) {
3680 			ath12k_dp_rx_h_tkip_mic_err(ar, msdu, status);
3681 			break;
3682 		}
3683 		fallthrough;
3684 	default:
3685 		/* TODO: Review other rxdma error code to check if anything is
3686 		 * worth reporting to mac80211
3687 		 */
3688 		drop = true;
3689 		break;
3690 	}
3691 
3692 	return drop;
3693 }
3694 
3695 static void ath12k_dp_rx_wbm_err(struct ath12k *ar,
3696 				 struct napi_struct *napi,
3697 				 struct sk_buff *msdu,
3698 				 struct sk_buff_head *msdu_list)
3699 {
3700 	struct ath12k_skb_rxcb *rxcb = ATH12K_SKB_RXCB(msdu);
3701 	struct ieee80211_rx_status rxs = {0};
3702 	bool drop = true;
3703 
3704 	switch (rxcb->err_rel_src) {
3705 	case HAL_WBM_REL_SRC_MODULE_REO:
3706 		drop = ath12k_dp_rx_h_reo_err(ar, msdu, &rxs, msdu_list);
3707 		break;
3708 	case HAL_WBM_REL_SRC_MODULE_RXDMA:
3709 		drop = ath12k_dp_rx_h_rxdma_err(ar, msdu, &rxs);
3710 		break;
3711 	default:
3712 		/* msdu will get freed */
3713 		break;
3714 	}
3715 
3716 	if (drop) {
3717 		dev_kfree_skb_any(msdu);
3718 		return;
3719 	}
3720 
3721 	ath12k_dp_rx_deliver_msdu(ar, napi, msdu, &rxs);
3722 }
3723 
3724 int ath12k_dp_rx_process_wbm_err(struct ath12k_base *ab,
3725 				 struct napi_struct *napi, int budget)
3726 {
3727 	struct ath12k *ar;
3728 	struct ath12k_dp *dp = &ab->dp;
3729 	struct dp_rxdma_ring *rx_ring;
3730 	struct hal_rx_wbm_rel_info err_info;
3731 	struct hal_srng *srng;
3732 	struct sk_buff *msdu;
3733 	struct sk_buff_head msdu_list[MAX_RADIOS];
3734 	struct ath12k_skb_rxcb *rxcb;
3735 	void *rx_desc;
3736 	int mac_id;
3737 	int num_buffs_reaped = 0;
3738 	struct ath12k_rx_desc_info *desc_info;
3739 	int ret, i;
3740 
3741 	for (i = 0; i < ab->num_radios; i++)
3742 		__skb_queue_head_init(&msdu_list[i]);
3743 
3744 	srng = &ab->hal.srng_list[dp->rx_rel_ring.ring_id];
3745 	rx_ring = &dp->rx_refill_buf_ring;
3746 
3747 	spin_lock_bh(&srng->lock);
3748 
3749 	ath12k_hal_srng_access_begin(ab, srng);
3750 
3751 	while (budget) {
3752 		rx_desc = ath12k_hal_srng_dst_get_next_entry(ab, srng);
3753 		if (!rx_desc)
3754 			break;
3755 
3756 		ret = ath12k_hal_wbm_desc_parse_err(ab, rx_desc, &err_info);
3757 		if (ret) {
3758 			ath12k_warn(ab,
3759 				    "failed to parse rx error in wbm_rel ring desc %d\n",
3760 				    ret);
3761 			continue;
3762 		}
3763 
3764 		desc_info = (struct ath12k_rx_desc_info *)err_info.rx_desc;
3765 
3766 		/* retry manual desc retrieval if hw cc is not done */
3767 		if (!desc_info) {
3768 			desc_info = ath12k_dp_get_rx_desc(ab, err_info.cookie);
3769 			if (!desc_info) {
3770 				ath12k_warn(ab, "Invalid cookie in manual desc retrieval");
3771 				continue;
3772 			}
3773 		}
3774 
3775 		/* FIXME: Extract mac id correctly. Since descs are not tied
3776 		 * to mac, we can extract from vdev id in ring desc.
3777 		 */
3778 		mac_id = 0;
3779 
3780 		if (desc_info->magic != ATH12K_DP_RX_DESC_MAGIC)
3781 			ath12k_warn(ab, "WBM RX err, Check HW CC implementation");
3782 
3783 		msdu = desc_info->skb;
3784 		desc_info->skb = NULL;
3785 
3786 		spin_lock_bh(&dp->rx_desc_lock);
3787 		list_move_tail(&desc_info->list, &dp->rx_desc_free_list);
3788 		spin_unlock_bh(&dp->rx_desc_lock);
3789 
3790 		rxcb = ATH12K_SKB_RXCB(msdu);
3791 		dma_unmap_single(ab->dev, rxcb->paddr,
3792 				 msdu->len + skb_tailroom(msdu),
3793 				 DMA_FROM_DEVICE);
3794 
3795 		num_buffs_reaped++;
3796 
3797 		if (!err_info.continuation)
3798 			budget--;
3799 
3800 		if (err_info.push_reason !=
3801 		    HAL_REO_DEST_RING_PUSH_REASON_ERR_DETECTED) {
3802 			dev_kfree_skb_any(msdu);
3803 			continue;
3804 		}
3805 
3806 		rxcb->err_rel_src = err_info.err_rel_src;
3807 		rxcb->err_code = err_info.err_code;
3808 		rxcb->rx_desc = (struct hal_rx_desc *)msdu->data;
3809 		__skb_queue_tail(&msdu_list[mac_id], msdu);
3810 
3811 		rxcb->is_first_msdu = err_info.first_msdu;
3812 		rxcb->is_last_msdu = err_info.last_msdu;
3813 		rxcb->is_continuation = err_info.continuation;
3814 	}
3815 
3816 	ath12k_hal_srng_access_end(ab, srng);
3817 
3818 	spin_unlock_bh(&srng->lock);
3819 
3820 	if (!num_buffs_reaped)
3821 		goto done;
3822 
3823 	ath12k_dp_rx_bufs_replenish(ab, 0, rx_ring, num_buffs_reaped,
3824 				    ab->hw_params->hal_params->rx_buf_rbm, true);
3825 
3826 	rcu_read_lock();
3827 	for (i = 0; i <  ab->num_radios; i++) {
3828 		if (!rcu_dereference(ab->pdevs_active[i])) {
3829 			__skb_queue_purge(&msdu_list[i]);
3830 			continue;
3831 		}
3832 
3833 		ar = ab->pdevs[i].ar;
3834 
3835 		if (test_bit(ATH12K_CAC_RUNNING, &ar->dev_flags)) {
3836 			__skb_queue_purge(&msdu_list[i]);
3837 			continue;
3838 		}
3839 
3840 		while ((msdu = __skb_dequeue(&msdu_list[i])) != NULL)
3841 			ath12k_dp_rx_wbm_err(ar, napi, msdu, &msdu_list[i]);
3842 	}
3843 	rcu_read_unlock();
3844 done:
3845 	return num_buffs_reaped;
3846 }
3847 
3848 void ath12k_dp_rx_process_reo_status(struct ath12k_base *ab)
3849 {
3850 	struct ath12k_dp *dp = &ab->dp;
3851 	struct hal_tlv_64_hdr *hdr;
3852 	struct hal_srng *srng;
3853 	struct ath12k_dp_rx_reo_cmd *cmd, *tmp;
3854 	bool found = false;
3855 	u16 tag;
3856 	struct hal_reo_status reo_status;
3857 
3858 	srng = &ab->hal.srng_list[dp->reo_status_ring.ring_id];
3859 
3860 	memset(&reo_status, 0, sizeof(reo_status));
3861 
3862 	spin_lock_bh(&srng->lock);
3863 
3864 	ath12k_hal_srng_access_begin(ab, srng);
3865 
3866 	while ((hdr = ath12k_hal_srng_dst_get_next_entry(ab, srng))) {
3867 		tag = u64_get_bits(hdr->tl, HAL_SRNG_TLV_HDR_TAG);
3868 
3869 		switch (tag) {
3870 		case HAL_REO_GET_QUEUE_STATS_STATUS:
3871 			ath12k_hal_reo_status_queue_stats(ab, hdr,
3872 							  &reo_status);
3873 			break;
3874 		case HAL_REO_FLUSH_QUEUE_STATUS:
3875 			ath12k_hal_reo_flush_queue_status(ab, hdr,
3876 							  &reo_status);
3877 			break;
3878 		case HAL_REO_FLUSH_CACHE_STATUS:
3879 			ath12k_hal_reo_flush_cache_status(ab, hdr,
3880 							  &reo_status);
3881 			break;
3882 		case HAL_REO_UNBLOCK_CACHE_STATUS:
3883 			ath12k_hal_reo_unblk_cache_status(ab, hdr,
3884 							  &reo_status);
3885 			break;
3886 		case HAL_REO_FLUSH_TIMEOUT_LIST_STATUS:
3887 			ath12k_hal_reo_flush_timeout_list_status(ab, hdr,
3888 								 &reo_status);
3889 			break;
3890 		case HAL_REO_DESCRIPTOR_THRESHOLD_REACHED_STATUS:
3891 			ath12k_hal_reo_desc_thresh_reached_status(ab, hdr,
3892 								  &reo_status);
3893 			break;
3894 		case HAL_REO_UPDATE_RX_REO_QUEUE_STATUS:
3895 			ath12k_hal_reo_update_rx_reo_queue_status(ab, hdr,
3896 								  &reo_status);
3897 			break;
3898 		default:
3899 			ath12k_warn(ab, "Unknown reo status type %d\n", tag);
3900 			continue;
3901 		}
3902 
3903 		spin_lock_bh(&dp->reo_cmd_lock);
3904 		list_for_each_entry_safe(cmd, tmp, &dp->reo_cmd_list, list) {
3905 			if (reo_status.uniform_hdr.cmd_num == cmd->cmd_num) {
3906 				found = true;
3907 				list_del(&cmd->list);
3908 				break;
3909 			}
3910 		}
3911 		spin_unlock_bh(&dp->reo_cmd_lock);
3912 
3913 		if (found) {
3914 			cmd->handler(dp, (void *)&cmd->data,
3915 				     reo_status.uniform_hdr.cmd_status);
3916 			kfree(cmd);
3917 		}
3918 
3919 		found = false;
3920 	}
3921 
3922 	ath12k_hal_srng_access_end(ab, srng);
3923 
3924 	spin_unlock_bh(&srng->lock);
3925 }
3926 
3927 void ath12k_dp_rx_free(struct ath12k_base *ab)
3928 {
3929 	struct ath12k_dp *dp = &ab->dp;
3930 	int i;
3931 
3932 	ath12k_dp_srng_cleanup(ab, &dp->rx_refill_buf_ring.refill_buf_ring);
3933 
3934 	for (i = 0; i < ab->hw_params->num_rxmda_per_pdev; i++) {
3935 		if (ab->hw_params->rx_mac_buf_ring)
3936 			ath12k_dp_srng_cleanup(ab, &dp->rx_mac_buf_ring[i]);
3937 	}
3938 
3939 	for (i = 0; i < ab->hw_params->num_rxdma_dst_ring; i++)
3940 		ath12k_dp_srng_cleanup(ab, &dp->rxdma_err_dst_ring[i]);
3941 
3942 	ath12k_dp_srng_cleanup(ab, &dp->rxdma_mon_buf_ring.refill_buf_ring);
3943 	ath12k_dp_srng_cleanup(ab, &dp->tx_mon_buf_ring.refill_buf_ring);
3944 
3945 	ath12k_dp_rxdma_buf_free(ab);
3946 }
3947 
3948 void ath12k_dp_rx_pdev_free(struct ath12k_base *ab, int mac_id)
3949 {
3950 	struct ath12k *ar = ab->pdevs[mac_id].ar;
3951 
3952 	ath12k_dp_rx_pdev_srng_free(ar);
3953 }
3954 
3955 int ath12k_dp_rxdma_ring_sel_config_qcn9274(struct ath12k_base *ab)
3956 {
3957 	struct ath12k_dp *dp = &ab->dp;
3958 	struct htt_rx_ring_tlv_filter tlv_filter = {0};
3959 	u32 ring_id;
3960 	int ret;
3961 	u32 hal_rx_desc_sz = ab->hw_params->hal_desc_sz;
3962 
3963 	ring_id = dp->rx_refill_buf_ring.refill_buf_ring.ring_id;
3964 
3965 	tlv_filter.rx_filter = HTT_RX_TLV_FLAGS_RXDMA_RING;
3966 	tlv_filter.pkt_filter_flags2 = HTT_RX_FP_CTRL_PKT_FILTER_TLV_FLAGS2_BAR;
3967 	tlv_filter.pkt_filter_flags3 = HTT_RX_FP_DATA_PKT_FILTER_TLV_FLASG3_MCAST |
3968 					HTT_RX_FP_DATA_PKT_FILTER_TLV_FLASG3_UCAST |
3969 					HTT_RX_FP_DATA_PKT_FILTER_TLV_FLASG3_NULL_DATA;
3970 	tlv_filter.offset_valid = true;
3971 	tlv_filter.rx_packet_offset = hal_rx_desc_sz;
3972 
3973 	tlv_filter.rx_mpdu_start_offset =
3974 			ab->hw_params->hal_ops->rx_desc_get_mpdu_start_offset();
3975 	tlv_filter.rx_msdu_end_offset =
3976 		ab->hw_params->hal_ops->rx_desc_get_msdu_end_offset();
3977 
3978 	/* TODO: Selectively subscribe to required qwords within msdu_end
3979 	 * and mpdu_start and setup the mask in below msg
3980 	 * and modify the rx_desc struct
3981 	 */
3982 	ret = ath12k_dp_tx_htt_rx_filter_setup(ab, ring_id, 0,
3983 					       HAL_RXDMA_BUF,
3984 					       DP_RXDMA_REFILL_RING_SIZE,
3985 					       &tlv_filter);
3986 
3987 	return ret;
3988 }
3989 
3990 int ath12k_dp_rxdma_ring_sel_config_wcn7850(struct ath12k_base *ab)
3991 {
3992 	struct ath12k_dp *dp = &ab->dp;
3993 	struct htt_rx_ring_tlv_filter tlv_filter = {0};
3994 	u32 ring_id;
3995 	int ret;
3996 	u32 hal_rx_desc_sz = ab->hw_params->hal_desc_sz;
3997 	int i;
3998 
3999 	ring_id = dp->rx_refill_buf_ring.refill_buf_ring.ring_id;
4000 
4001 	tlv_filter.rx_filter = HTT_RX_TLV_FLAGS_RXDMA_RING;
4002 	tlv_filter.pkt_filter_flags2 = HTT_RX_FP_CTRL_PKT_FILTER_TLV_FLAGS2_BAR;
4003 	tlv_filter.pkt_filter_flags3 = HTT_RX_FP_DATA_PKT_FILTER_TLV_FLASG3_MCAST |
4004 					HTT_RX_FP_DATA_PKT_FILTER_TLV_FLASG3_UCAST |
4005 					HTT_RX_FP_DATA_PKT_FILTER_TLV_FLASG3_NULL_DATA;
4006 	tlv_filter.offset_valid = true;
4007 	tlv_filter.rx_packet_offset = hal_rx_desc_sz;
4008 
4009 	tlv_filter.rx_header_offset = offsetof(struct hal_rx_desc_wcn7850, pkt_hdr_tlv);
4010 
4011 	tlv_filter.rx_mpdu_start_offset =
4012 			ab->hw_params->hal_ops->rx_desc_get_mpdu_start_offset();
4013 	tlv_filter.rx_msdu_end_offset =
4014 		ab->hw_params->hal_ops->rx_desc_get_msdu_end_offset();
4015 
4016 	/* TODO: Selectively subscribe to required qwords within msdu_end
4017 	 * and mpdu_start and setup the mask in below msg
4018 	 * and modify the rx_desc struct
4019 	 */
4020 
4021 	for (i = 0; i < ab->hw_params->num_rxmda_per_pdev; i++) {
4022 		ring_id = dp->rx_mac_buf_ring[i].ring_id;
4023 		ret = ath12k_dp_tx_htt_rx_filter_setup(ab, ring_id, i,
4024 						       HAL_RXDMA_BUF,
4025 						       DP_RXDMA_REFILL_RING_SIZE,
4026 						       &tlv_filter);
4027 	}
4028 
4029 	return ret;
4030 }
4031 
4032 int ath12k_dp_rx_htt_setup(struct ath12k_base *ab)
4033 {
4034 	struct ath12k_dp *dp = &ab->dp;
4035 	u32 ring_id;
4036 	int i, ret;
4037 
4038 	/* TODO: Need to verify the HTT setup for QCN9224 */
4039 	ring_id = dp->rx_refill_buf_ring.refill_buf_ring.ring_id;
4040 	ret = ath12k_dp_tx_htt_srng_setup(ab, ring_id, 0, HAL_RXDMA_BUF);
4041 	if (ret) {
4042 		ath12k_warn(ab, "failed to configure rx_refill_buf_ring %d\n",
4043 			    ret);
4044 		return ret;
4045 	}
4046 
4047 	if (ab->hw_params->rx_mac_buf_ring) {
4048 		for (i = 0; i < ab->hw_params->num_rxmda_per_pdev; i++) {
4049 			ring_id = dp->rx_mac_buf_ring[i].ring_id;
4050 			ret = ath12k_dp_tx_htt_srng_setup(ab, ring_id,
4051 							  i, HAL_RXDMA_BUF);
4052 			if (ret) {
4053 				ath12k_warn(ab, "failed to configure rx_mac_buf_ring%d %d\n",
4054 					    i, ret);
4055 				return ret;
4056 			}
4057 		}
4058 	}
4059 
4060 	for (i = 0; i < ab->hw_params->num_rxdma_dst_ring; i++) {
4061 		ring_id = dp->rxdma_err_dst_ring[i].ring_id;
4062 		ret = ath12k_dp_tx_htt_srng_setup(ab, ring_id,
4063 						  i, HAL_RXDMA_DST);
4064 		if (ret) {
4065 			ath12k_warn(ab, "failed to configure rxdma_err_dest_ring%d %d\n",
4066 				    i, ret);
4067 			return ret;
4068 		}
4069 	}
4070 
4071 	if (ab->hw_params->rxdma1_enable) {
4072 		ring_id = dp->rxdma_mon_buf_ring.refill_buf_ring.ring_id;
4073 		ret = ath12k_dp_tx_htt_srng_setup(ab, ring_id,
4074 						  0, HAL_RXDMA_MONITOR_BUF);
4075 		if (ret) {
4076 			ath12k_warn(ab, "failed to configure rxdma_mon_buf_ring %d\n",
4077 				    ret);
4078 			return ret;
4079 		}
4080 
4081 		ring_id = dp->tx_mon_buf_ring.refill_buf_ring.ring_id;
4082 		ret = ath12k_dp_tx_htt_srng_setup(ab, ring_id,
4083 						  0, HAL_TX_MONITOR_BUF);
4084 		if (ret) {
4085 			ath12k_warn(ab, "failed to configure rxdma_mon_buf_ring %d\n",
4086 				    ret);
4087 			return ret;
4088 		}
4089 	}
4090 
4091 	ret = ab->hw_params->hw_ops->rxdma_ring_sel_config(ab);
4092 	if (ret) {
4093 		ath12k_warn(ab, "failed to setup rxdma ring selection config\n");
4094 		return ret;
4095 	}
4096 
4097 	return 0;
4098 }
4099 
4100 int ath12k_dp_rx_alloc(struct ath12k_base *ab)
4101 {
4102 	struct ath12k_dp *dp = &ab->dp;
4103 	int i, ret;
4104 
4105 	idr_init(&dp->rx_refill_buf_ring.bufs_idr);
4106 	spin_lock_init(&dp->rx_refill_buf_ring.idr_lock);
4107 
4108 	idr_init(&dp->rxdma_mon_buf_ring.bufs_idr);
4109 	spin_lock_init(&dp->rxdma_mon_buf_ring.idr_lock);
4110 
4111 	idr_init(&dp->tx_mon_buf_ring.bufs_idr);
4112 	spin_lock_init(&dp->tx_mon_buf_ring.idr_lock);
4113 
4114 	ret = ath12k_dp_srng_setup(ab,
4115 				   &dp->rx_refill_buf_ring.refill_buf_ring,
4116 				   HAL_RXDMA_BUF, 0, 0,
4117 				   DP_RXDMA_BUF_RING_SIZE);
4118 	if (ret) {
4119 		ath12k_warn(ab, "failed to setup rx_refill_buf_ring\n");
4120 		return ret;
4121 	}
4122 
4123 	if (ab->hw_params->rx_mac_buf_ring) {
4124 		for (i = 0; i < ab->hw_params->num_rxmda_per_pdev; i++) {
4125 			ret = ath12k_dp_srng_setup(ab,
4126 						   &dp->rx_mac_buf_ring[i],
4127 						   HAL_RXDMA_BUF, 1,
4128 						   i, 1024);
4129 			if (ret) {
4130 				ath12k_warn(ab, "failed to setup rx_mac_buf_ring %d\n",
4131 					    i);
4132 				return ret;
4133 			}
4134 		}
4135 	}
4136 
4137 	for (i = 0; i < ab->hw_params->num_rxdma_dst_ring; i++) {
4138 		ret = ath12k_dp_srng_setup(ab, &dp->rxdma_err_dst_ring[i],
4139 					   HAL_RXDMA_DST, 0, i,
4140 					   DP_RXDMA_ERR_DST_RING_SIZE);
4141 		if (ret) {
4142 			ath12k_warn(ab, "failed to setup rxdma_err_dst_ring %d\n", i);
4143 			return ret;
4144 		}
4145 	}
4146 
4147 	if (ab->hw_params->rxdma1_enable) {
4148 		ret = ath12k_dp_srng_setup(ab,
4149 					   &dp->rxdma_mon_buf_ring.refill_buf_ring,
4150 					   HAL_RXDMA_MONITOR_BUF, 0, 0,
4151 					   DP_RXDMA_MONITOR_BUF_RING_SIZE);
4152 		if (ret) {
4153 			ath12k_warn(ab, "failed to setup HAL_RXDMA_MONITOR_BUF\n");
4154 			return ret;
4155 		}
4156 
4157 		ret = ath12k_dp_srng_setup(ab,
4158 					   &dp->tx_mon_buf_ring.refill_buf_ring,
4159 					   HAL_TX_MONITOR_BUF, 0, 0,
4160 					   DP_TX_MONITOR_BUF_RING_SIZE);
4161 		if (ret) {
4162 			ath12k_warn(ab, "failed to setup DP_TX_MONITOR_BUF_RING_SIZE\n");
4163 			return ret;
4164 		}
4165 	}
4166 
4167 	ret = ath12k_dp_rxdma_buf_setup(ab);
4168 	if (ret) {
4169 		ath12k_warn(ab, "failed to setup rxdma ring\n");
4170 		return ret;
4171 	}
4172 
4173 	return 0;
4174 }
4175 
4176 int ath12k_dp_rx_pdev_alloc(struct ath12k_base *ab, int mac_id)
4177 {
4178 	struct ath12k *ar = ab->pdevs[mac_id].ar;
4179 	struct ath12k_pdev_dp *dp = &ar->dp;
4180 	u32 ring_id;
4181 	int i;
4182 	int ret;
4183 
4184 	if (!ab->hw_params->rxdma1_enable)
4185 		goto out;
4186 
4187 	ret = ath12k_dp_rx_pdev_srng_alloc(ar);
4188 	if (ret) {
4189 		ath12k_warn(ab, "failed to setup rx srngs\n");
4190 		return ret;
4191 	}
4192 
4193 	for (i = 0; i < ab->hw_params->num_rxmda_per_pdev; i++) {
4194 		ring_id = dp->rxdma_mon_dst_ring[i].ring_id;
4195 		ret = ath12k_dp_tx_htt_srng_setup(ab, ring_id,
4196 						  mac_id + i,
4197 						  HAL_RXDMA_MONITOR_DST);
4198 		if (ret) {
4199 			ath12k_warn(ab,
4200 				    "failed to configure rxdma_mon_dst_ring %d %d\n",
4201 				    i, ret);
4202 			return ret;
4203 		}
4204 
4205 		ring_id = dp->tx_mon_dst_ring[i].ring_id;
4206 		ret = ath12k_dp_tx_htt_srng_setup(ab, ring_id,
4207 						  mac_id + i,
4208 						  HAL_TX_MONITOR_DST);
4209 		if (ret) {
4210 			ath12k_warn(ab,
4211 				    "failed to configure tx_mon_dst_ring %d %d\n",
4212 				    i, ret);
4213 			return ret;
4214 		}
4215 	}
4216 out:
4217 	return 0;
4218 }
4219 
4220 static int ath12k_dp_rx_pdev_mon_status_attach(struct ath12k *ar)
4221 {
4222 	struct ath12k_pdev_dp *dp = &ar->dp;
4223 	struct ath12k_mon_data *pmon = (struct ath12k_mon_data *)&dp->mon_data;
4224 
4225 	skb_queue_head_init(&pmon->rx_status_q);
4226 
4227 	pmon->mon_ppdu_status = DP_PPDU_STATUS_START;
4228 
4229 	memset(&pmon->rx_mon_stats, 0,
4230 	       sizeof(pmon->rx_mon_stats));
4231 	return 0;
4232 }
4233 
4234 int ath12k_dp_rx_pdev_mon_attach(struct ath12k *ar)
4235 {
4236 	struct ath12k_pdev_dp *dp = &ar->dp;
4237 	struct ath12k_mon_data *pmon = &dp->mon_data;
4238 	int ret = 0;
4239 
4240 	ret = ath12k_dp_rx_pdev_mon_status_attach(ar);
4241 	if (ret) {
4242 		ath12k_warn(ar->ab, "pdev_mon_status_attach() failed");
4243 		return ret;
4244 	}
4245 
4246 	/* if rxdma1_enable is false, no need to setup
4247 	 * rxdma_mon_desc_ring.
4248 	 */
4249 	if (!ar->ab->hw_params->rxdma1_enable)
4250 		return 0;
4251 
4252 	pmon->mon_last_linkdesc_paddr = 0;
4253 	pmon->mon_last_buf_cookie = DP_RX_DESC_COOKIE_MAX + 1;
4254 	spin_lock_init(&pmon->mon_lock);
4255 
4256 	return 0;
4257 }
4258 
4259 int ath12k_dp_rx_pktlog_start(struct ath12k_base *ab)
4260 {
4261 	/* start reap timer */
4262 	mod_timer(&ab->mon_reap_timer,
4263 		  jiffies + msecs_to_jiffies(ATH12K_MON_TIMER_INTERVAL));
4264 
4265 	return 0;
4266 }
4267 
4268 int ath12k_dp_rx_pktlog_stop(struct ath12k_base *ab, bool stop_timer)
4269 {
4270 	int ret;
4271 
4272 	if (stop_timer)
4273 		del_timer_sync(&ab->mon_reap_timer);
4274 
4275 	/* reap all the monitor related rings */
4276 	ret = ath12k_dp_purge_mon_ring(ab);
4277 	if (ret) {
4278 		ath12k_warn(ab, "failed to purge dp mon ring: %d\n", ret);
4279 		return ret;
4280 	}
4281 
4282 	return 0;
4283 }
4284