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 	pdev_id = le32_get_bits(msg->info, HTT_T2H_PPDU_STATS_INFO_PDEV_ID);
1559 	ppdu_id = le32_to_cpu(msg->ppdu_id);
1560 
1561 	rcu_read_lock();
1562 	ar = ath12k_mac_get_ar_by_pdev_id(ab, pdev_id);
1563 	if (!ar) {
1564 		ret = -EINVAL;
1565 		goto exit;
1566 	}
1567 
1568 	spin_lock_bh(&ar->data_lock);
1569 	ppdu_info = ath12k_dp_htt_get_ppdu_desc(ar, ppdu_id);
1570 	if (!ppdu_info) {
1571 		spin_unlock_bh(&ar->data_lock);
1572 		ret = -EINVAL;
1573 		goto exit;
1574 	}
1575 
1576 	ppdu_info->ppdu_id = ppdu_id;
1577 	ret = ath12k_dp_htt_tlv_iter(ab, msg->data, len,
1578 				     ath12k_htt_tlv_ppdu_stats_parse,
1579 				     (void *)ppdu_info);
1580 	if (ret) {
1581 		spin_unlock_bh(&ar->data_lock);
1582 		ath12k_warn(ab, "Failed to parse tlv %d\n", ret);
1583 		goto exit;
1584 	}
1585 
1586 	/* back up data rate tlv for all peers */
1587 	if (ppdu_info->frame_type == HTT_STATS_PPDU_FTYPE_DATA &&
1588 	    (ppdu_info->tlv_bitmap & (1 << HTT_PPDU_STATS_TAG_USR_COMMON)) &&
1589 	    ppdu_info->delay_ba) {
1590 		for (i = 0; i < ppdu_info->ppdu_stats.common.num_users; i++) {
1591 			peer_id = ppdu_info->ppdu_stats.user_stats[i].peer_id;
1592 			spin_lock_bh(&ab->base_lock);
1593 			peer = ath12k_peer_find_by_id(ab, peer_id);
1594 			if (!peer) {
1595 				spin_unlock_bh(&ab->base_lock);
1596 				continue;
1597 			}
1598 
1599 			usr_stats = &ppdu_info->ppdu_stats.user_stats[i];
1600 			if (usr_stats->delay_ba)
1601 				ath12k_copy_to_delay_stats(peer, usr_stats);
1602 			spin_unlock_bh(&ab->base_lock);
1603 		}
1604 	}
1605 
1606 	/* restore all peers' data rate tlv to mu-bar tlv */
1607 	if (ppdu_info->frame_type == HTT_STATS_PPDU_FTYPE_BAR &&
1608 	    (ppdu_info->tlv_bitmap & (1 << HTT_PPDU_STATS_TAG_USR_COMMON))) {
1609 		for (i = 0; i < ppdu_info->bar_num_users; i++) {
1610 			peer_id = ppdu_info->ppdu_stats.user_stats[i].peer_id;
1611 			spin_lock_bh(&ab->base_lock);
1612 			peer = ath12k_peer_find_by_id(ab, peer_id);
1613 			if (!peer) {
1614 				spin_unlock_bh(&ab->base_lock);
1615 				continue;
1616 			}
1617 
1618 			usr_stats = &ppdu_info->ppdu_stats.user_stats[i];
1619 			if (peer->delayba_flag)
1620 				ath12k_copy_to_bar(peer, usr_stats);
1621 			spin_unlock_bh(&ab->base_lock);
1622 		}
1623 	}
1624 
1625 	spin_unlock_bh(&ar->data_lock);
1626 
1627 exit:
1628 	rcu_read_unlock();
1629 
1630 	return ret;
1631 }
1632 
1633 static void ath12k_htt_mlo_offset_event_handler(struct ath12k_base *ab,
1634 						struct sk_buff *skb)
1635 {
1636 	struct ath12k_htt_mlo_offset_msg *msg;
1637 	struct ath12k_pdev *pdev;
1638 	struct ath12k *ar;
1639 	u8 pdev_id;
1640 
1641 	msg = (struct ath12k_htt_mlo_offset_msg *)skb->data;
1642 	pdev_id = u32_get_bits(__le32_to_cpu(msg->info),
1643 			       HTT_T2H_MLO_OFFSET_INFO_PDEV_ID);
1644 	ar = ath12k_mac_get_ar_by_pdev_id(ab, pdev_id);
1645 
1646 	if (!ar) {
1647 		ath12k_warn(ab, "invalid pdev id %d on htt mlo offset\n", pdev_id);
1648 		return;
1649 	}
1650 
1651 	spin_lock_bh(&ar->data_lock);
1652 	pdev = ar->pdev;
1653 
1654 	pdev->timestamp.info = __le32_to_cpu(msg->info);
1655 	pdev->timestamp.sync_timestamp_lo_us = __le32_to_cpu(msg->sync_timestamp_lo_us);
1656 	pdev->timestamp.sync_timestamp_hi_us = __le32_to_cpu(msg->sync_timestamp_hi_us);
1657 	pdev->timestamp.mlo_offset_lo = __le32_to_cpu(msg->mlo_offset_lo);
1658 	pdev->timestamp.mlo_offset_hi = __le32_to_cpu(msg->mlo_offset_hi);
1659 	pdev->timestamp.mlo_offset_clks = __le32_to_cpu(msg->mlo_offset_clks);
1660 	pdev->timestamp.mlo_comp_clks = __le32_to_cpu(msg->mlo_comp_clks);
1661 	pdev->timestamp.mlo_comp_timer = __le32_to_cpu(msg->mlo_comp_timer);
1662 
1663 	spin_unlock_bh(&ar->data_lock);
1664 }
1665 
1666 void ath12k_dp_htt_htc_t2h_msg_handler(struct ath12k_base *ab,
1667 				       struct sk_buff *skb)
1668 {
1669 	struct ath12k_dp *dp = &ab->dp;
1670 	struct htt_resp_msg *resp = (struct htt_resp_msg *)skb->data;
1671 	enum htt_t2h_msg_type type;
1672 	u16 peer_id;
1673 	u8 vdev_id;
1674 	u8 mac_addr[ETH_ALEN];
1675 	u16 peer_mac_h16;
1676 	u16 ast_hash = 0;
1677 	u16 hw_peer_id;
1678 
1679 	type = le32_get_bits(resp->version_msg.version, HTT_T2H_MSG_TYPE);
1680 
1681 	ath12k_dbg(ab, ATH12K_DBG_DP_HTT, "dp_htt rx msg type :0x%0x\n", type);
1682 
1683 	switch (type) {
1684 	case HTT_T2H_MSG_TYPE_VERSION_CONF:
1685 		dp->htt_tgt_ver_major = le32_get_bits(resp->version_msg.version,
1686 						      HTT_T2H_VERSION_CONF_MAJOR);
1687 		dp->htt_tgt_ver_minor = le32_get_bits(resp->version_msg.version,
1688 						      HTT_T2H_VERSION_CONF_MINOR);
1689 		complete(&dp->htt_tgt_version_received);
1690 		break;
1691 	/* TODO: remove unused peer map versions after testing */
1692 	case HTT_T2H_MSG_TYPE_PEER_MAP:
1693 		vdev_id = le32_get_bits(resp->peer_map_ev.info,
1694 					HTT_T2H_PEER_MAP_INFO_VDEV_ID);
1695 		peer_id = le32_get_bits(resp->peer_map_ev.info,
1696 					HTT_T2H_PEER_MAP_INFO_PEER_ID);
1697 		peer_mac_h16 = le32_get_bits(resp->peer_map_ev.info1,
1698 					     HTT_T2H_PEER_MAP_INFO1_MAC_ADDR_H16);
1699 		ath12k_dp_get_mac_addr(le32_to_cpu(resp->peer_map_ev.mac_addr_l32),
1700 				       peer_mac_h16, mac_addr);
1701 		ath12k_peer_map_event(ab, vdev_id, peer_id, mac_addr, 0, 0);
1702 		break;
1703 	case HTT_T2H_MSG_TYPE_PEER_MAP2:
1704 		vdev_id = le32_get_bits(resp->peer_map_ev.info,
1705 					HTT_T2H_PEER_MAP_INFO_VDEV_ID);
1706 		peer_id = le32_get_bits(resp->peer_map_ev.info,
1707 					HTT_T2H_PEER_MAP_INFO_PEER_ID);
1708 		peer_mac_h16 = le32_get_bits(resp->peer_map_ev.info1,
1709 					     HTT_T2H_PEER_MAP_INFO1_MAC_ADDR_H16);
1710 		ath12k_dp_get_mac_addr(le32_to_cpu(resp->peer_map_ev.mac_addr_l32),
1711 				       peer_mac_h16, mac_addr);
1712 		ast_hash = le32_get_bits(resp->peer_map_ev.info2,
1713 					 HTT_T2H_PEER_MAP_INFO2_AST_HASH_VAL);
1714 		hw_peer_id = le32_get_bits(resp->peer_map_ev.info1,
1715 					   HTT_T2H_PEER_MAP_INFO1_HW_PEER_ID);
1716 		ath12k_peer_map_event(ab, vdev_id, peer_id, mac_addr, ast_hash,
1717 				      hw_peer_id);
1718 		break;
1719 	case HTT_T2H_MSG_TYPE_PEER_MAP3:
1720 		vdev_id = le32_get_bits(resp->peer_map_ev.info,
1721 					HTT_T2H_PEER_MAP_INFO_VDEV_ID);
1722 		peer_id = le32_get_bits(resp->peer_map_ev.info,
1723 					HTT_T2H_PEER_MAP_INFO_PEER_ID);
1724 		peer_mac_h16 = le32_get_bits(resp->peer_map_ev.info1,
1725 					     HTT_T2H_PEER_MAP_INFO1_MAC_ADDR_H16);
1726 		ath12k_dp_get_mac_addr(le32_to_cpu(resp->peer_map_ev.mac_addr_l32),
1727 				       peer_mac_h16, mac_addr);
1728 		ath12k_peer_map_event(ab, vdev_id, peer_id, mac_addr, ast_hash,
1729 				      peer_id);
1730 		break;
1731 	case HTT_T2H_MSG_TYPE_PEER_UNMAP:
1732 	case HTT_T2H_MSG_TYPE_PEER_UNMAP2:
1733 		peer_id = le32_get_bits(resp->peer_unmap_ev.info,
1734 					HTT_T2H_PEER_UNMAP_INFO_PEER_ID);
1735 		ath12k_peer_unmap_event(ab, peer_id);
1736 		break;
1737 	case HTT_T2H_MSG_TYPE_PPDU_STATS_IND:
1738 		ath12k_htt_pull_ppdu_stats(ab, skb);
1739 		break;
1740 	case HTT_T2H_MSG_TYPE_EXT_STATS_CONF:
1741 		break;
1742 	case HTT_T2H_MSG_TYPE_MLO_TIMESTAMP_OFFSET_IND:
1743 		ath12k_htt_mlo_offset_event_handler(ab, skb);
1744 		break;
1745 	default:
1746 		ath12k_dbg(ab, ATH12K_DBG_DP_HTT, "dp_htt event %d not handled\n",
1747 			   type);
1748 		break;
1749 	}
1750 
1751 	dev_kfree_skb_any(skb);
1752 }
1753 
1754 static int ath12k_dp_rx_msdu_coalesce(struct ath12k *ar,
1755 				      struct sk_buff_head *msdu_list,
1756 				      struct sk_buff *first, struct sk_buff *last,
1757 				      u8 l3pad_bytes, int msdu_len)
1758 {
1759 	struct ath12k_base *ab = ar->ab;
1760 	struct sk_buff *skb;
1761 	struct ath12k_skb_rxcb *rxcb = ATH12K_SKB_RXCB(first);
1762 	int buf_first_hdr_len, buf_first_len;
1763 	struct hal_rx_desc *ldesc;
1764 	int space_extra, rem_len, buf_len;
1765 	u32 hal_rx_desc_sz = ar->ab->hw_params->hal_desc_sz;
1766 
1767 	/* As the msdu is spread across multiple rx buffers,
1768 	 * find the offset to the start of msdu for computing
1769 	 * the length of the msdu in the first buffer.
1770 	 */
1771 	buf_first_hdr_len = hal_rx_desc_sz + l3pad_bytes;
1772 	buf_first_len = DP_RX_BUFFER_SIZE - buf_first_hdr_len;
1773 
1774 	if (WARN_ON_ONCE(msdu_len <= buf_first_len)) {
1775 		skb_put(first, buf_first_hdr_len + msdu_len);
1776 		skb_pull(first, buf_first_hdr_len);
1777 		return 0;
1778 	}
1779 
1780 	ldesc = (struct hal_rx_desc *)last->data;
1781 	rxcb->is_first_msdu = ath12k_dp_rx_h_first_msdu(ab, ldesc);
1782 	rxcb->is_last_msdu = ath12k_dp_rx_h_last_msdu(ab, ldesc);
1783 
1784 	/* MSDU spans over multiple buffers because the length of the MSDU
1785 	 * exceeds DP_RX_BUFFER_SIZE - HAL_RX_DESC_SIZE. So assume the data
1786 	 * in the first buf is of length DP_RX_BUFFER_SIZE - HAL_RX_DESC_SIZE.
1787 	 */
1788 	skb_put(first, DP_RX_BUFFER_SIZE);
1789 	skb_pull(first, buf_first_hdr_len);
1790 
1791 	/* When an MSDU spread over multiple buffers MSDU_END
1792 	 * tlvs are valid only in the last buffer. Copy those tlvs.
1793 	 */
1794 	ath12k_dp_rx_desc_end_tlv_copy(ab, rxcb->rx_desc, ldesc);
1795 
1796 	space_extra = msdu_len - (buf_first_len + skb_tailroom(first));
1797 	if (space_extra > 0 &&
1798 	    (pskb_expand_head(first, 0, space_extra, GFP_ATOMIC) < 0)) {
1799 		/* Free up all buffers of the MSDU */
1800 		while ((skb = __skb_dequeue(msdu_list)) != NULL) {
1801 			rxcb = ATH12K_SKB_RXCB(skb);
1802 			if (!rxcb->is_continuation) {
1803 				dev_kfree_skb_any(skb);
1804 				break;
1805 			}
1806 			dev_kfree_skb_any(skb);
1807 		}
1808 		return -ENOMEM;
1809 	}
1810 
1811 	rem_len = msdu_len - buf_first_len;
1812 	while ((skb = __skb_dequeue(msdu_list)) != NULL && rem_len > 0) {
1813 		rxcb = ATH12K_SKB_RXCB(skb);
1814 		if (rxcb->is_continuation)
1815 			buf_len = DP_RX_BUFFER_SIZE - hal_rx_desc_sz;
1816 		else
1817 			buf_len = rem_len;
1818 
1819 		if (buf_len > (DP_RX_BUFFER_SIZE - hal_rx_desc_sz)) {
1820 			WARN_ON_ONCE(1);
1821 			dev_kfree_skb_any(skb);
1822 			return -EINVAL;
1823 		}
1824 
1825 		skb_put(skb, buf_len + hal_rx_desc_sz);
1826 		skb_pull(skb, hal_rx_desc_sz);
1827 		skb_copy_from_linear_data(skb, skb_put(first, buf_len),
1828 					  buf_len);
1829 		dev_kfree_skb_any(skb);
1830 
1831 		rem_len -= buf_len;
1832 		if (!rxcb->is_continuation)
1833 			break;
1834 	}
1835 
1836 	return 0;
1837 }
1838 
1839 static struct sk_buff *ath12k_dp_rx_get_msdu_last_buf(struct sk_buff_head *msdu_list,
1840 						      struct sk_buff *first)
1841 {
1842 	struct sk_buff *skb;
1843 	struct ath12k_skb_rxcb *rxcb = ATH12K_SKB_RXCB(first);
1844 
1845 	if (!rxcb->is_continuation)
1846 		return first;
1847 
1848 	skb_queue_walk(msdu_list, skb) {
1849 		rxcb = ATH12K_SKB_RXCB(skb);
1850 		if (!rxcb->is_continuation)
1851 			return skb;
1852 	}
1853 
1854 	return NULL;
1855 }
1856 
1857 static void ath12k_dp_rx_h_csum_offload(struct ath12k *ar, struct sk_buff *msdu)
1858 {
1859 	struct ath12k_skb_rxcb *rxcb = ATH12K_SKB_RXCB(msdu);
1860 	struct ath12k_base *ab = ar->ab;
1861 	bool ip_csum_fail, l4_csum_fail;
1862 
1863 	ip_csum_fail = ath12k_dp_rx_h_ip_cksum_fail(ab, rxcb->rx_desc);
1864 	l4_csum_fail = ath12k_dp_rx_h_l4_cksum_fail(ab, rxcb->rx_desc);
1865 
1866 	msdu->ip_summed = (ip_csum_fail || l4_csum_fail) ?
1867 			  CHECKSUM_NONE : CHECKSUM_UNNECESSARY;
1868 }
1869 
1870 static int ath12k_dp_rx_crypto_mic_len(struct ath12k *ar,
1871 				       enum hal_encrypt_type enctype)
1872 {
1873 	switch (enctype) {
1874 	case HAL_ENCRYPT_TYPE_OPEN:
1875 	case HAL_ENCRYPT_TYPE_TKIP_NO_MIC:
1876 	case HAL_ENCRYPT_TYPE_TKIP_MIC:
1877 		return 0;
1878 	case HAL_ENCRYPT_TYPE_CCMP_128:
1879 		return IEEE80211_CCMP_MIC_LEN;
1880 	case HAL_ENCRYPT_TYPE_CCMP_256:
1881 		return IEEE80211_CCMP_256_MIC_LEN;
1882 	case HAL_ENCRYPT_TYPE_GCMP_128:
1883 	case HAL_ENCRYPT_TYPE_AES_GCMP_256:
1884 		return IEEE80211_GCMP_MIC_LEN;
1885 	case HAL_ENCRYPT_TYPE_WEP_40:
1886 	case HAL_ENCRYPT_TYPE_WEP_104:
1887 	case HAL_ENCRYPT_TYPE_WEP_128:
1888 	case HAL_ENCRYPT_TYPE_WAPI_GCM_SM4:
1889 	case HAL_ENCRYPT_TYPE_WAPI:
1890 		break;
1891 	}
1892 
1893 	ath12k_warn(ar->ab, "unsupported encryption type %d for mic len\n", enctype);
1894 	return 0;
1895 }
1896 
1897 static int ath12k_dp_rx_crypto_param_len(struct ath12k *ar,
1898 					 enum hal_encrypt_type enctype)
1899 {
1900 	switch (enctype) {
1901 	case HAL_ENCRYPT_TYPE_OPEN:
1902 		return 0;
1903 	case HAL_ENCRYPT_TYPE_TKIP_NO_MIC:
1904 	case HAL_ENCRYPT_TYPE_TKIP_MIC:
1905 		return IEEE80211_TKIP_IV_LEN;
1906 	case HAL_ENCRYPT_TYPE_CCMP_128:
1907 		return IEEE80211_CCMP_HDR_LEN;
1908 	case HAL_ENCRYPT_TYPE_CCMP_256:
1909 		return IEEE80211_CCMP_256_HDR_LEN;
1910 	case HAL_ENCRYPT_TYPE_GCMP_128:
1911 	case HAL_ENCRYPT_TYPE_AES_GCMP_256:
1912 		return IEEE80211_GCMP_HDR_LEN;
1913 	case HAL_ENCRYPT_TYPE_WEP_40:
1914 	case HAL_ENCRYPT_TYPE_WEP_104:
1915 	case HAL_ENCRYPT_TYPE_WEP_128:
1916 	case HAL_ENCRYPT_TYPE_WAPI_GCM_SM4:
1917 	case HAL_ENCRYPT_TYPE_WAPI:
1918 		break;
1919 	}
1920 
1921 	ath12k_warn(ar->ab, "unsupported encryption type %d\n", enctype);
1922 	return 0;
1923 }
1924 
1925 static int ath12k_dp_rx_crypto_icv_len(struct ath12k *ar,
1926 				       enum hal_encrypt_type enctype)
1927 {
1928 	switch (enctype) {
1929 	case HAL_ENCRYPT_TYPE_OPEN:
1930 	case HAL_ENCRYPT_TYPE_CCMP_128:
1931 	case HAL_ENCRYPT_TYPE_CCMP_256:
1932 	case HAL_ENCRYPT_TYPE_GCMP_128:
1933 	case HAL_ENCRYPT_TYPE_AES_GCMP_256:
1934 		return 0;
1935 	case HAL_ENCRYPT_TYPE_TKIP_NO_MIC:
1936 	case HAL_ENCRYPT_TYPE_TKIP_MIC:
1937 		return IEEE80211_TKIP_ICV_LEN;
1938 	case HAL_ENCRYPT_TYPE_WEP_40:
1939 	case HAL_ENCRYPT_TYPE_WEP_104:
1940 	case HAL_ENCRYPT_TYPE_WEP_128:
1941 	case HAL_ENCRYPT_TYPE_WAPI_GCM_SM4:
1942 	case HAL_ENCRYPT_TYPE_WAPI:
1943 		break;
1944 	}
1945 
1946 	ath12k_warn(ar->ab, "unsupported encryption type %d\n", enctype);
1947 	return 0;
1948 }
1949 
1950 static void ath12k_dp_rx_h_undecap_nwifi(struct ath12k *ar,
1951 					 struct sk_buff *msdu,
1952 					 enum hal_encrypt_type enctype,
1953 					 struct ieee80211_rx_status *status)
1954 {
1955 	struct ath12k_base *ab = ar->ab;
1956 	struct ath12k_skb_rxcb *rxcb = ATH12K_SKB_RXCB(msdu);
1957 	u8 decap_hdr[DP_MAX_NWIFI_HDR_LEN];
1958 	struct ieee80211_hdr *hdr;
1959 	size_t hdr_len;
1960 	u8 *crypto_hdr;
1961 	u16 qos_ctl;
1962 
1963 	/* pull decapped header */
1964 	hdr = (struct ieee80211_hdr *)msdu->data;
1965 	hdr_len = ieee80211_hdrlen(hdr->frame_control);
1966 	skb_pull(msdu, hdr_len);
1967 
1968 	/*  Rebuild qos header */
1969 	hdr->frame_control |= __cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
1970 
1971 	/* Reset the order bit as the HT_Control header is stripped */
1972 	hdr->frame_control &= ~(__cpu_to_le16(IEEE80211_FCTL_ORDER));
1973 
1974 	qos_ctl = rxcb->tid;
1975 
1976 	if (ath12k_dp_rx_h_mesh_ctl_present(ab, rxcb->rx_desc))
1977 		qos_ctl |= IEEE80211_QOS_CTL_MESH_CONTROL_PRESENT;
1978 
1979 	/* TODO: Add other QoS ctl fields when required */
1980 
1981 	/* copy decap header before overwriting for reuse below */
1982 	memcpy(decap_hdr, hdr, hdr_len);
1983 
1984 	/* Rebuild crypto header for mac80211 use */
1985 	if (!(status->flag & RX_FLAG_IV_STRIPPED)) {
1986 		crypto_hdr = skb_push(msdu, ath12k_dp_rx_crypto_param_len(ar, enctype));
1987 		ath12k_dp_rx_desc_get_crypto_header(ar->ab,
1988 						    rxcb->rx_desc, crypto_hdr,
1989 						    enctype);
1990 	}
1991 
1992 	memcpy(skb_push(msdu,
1993 			IEEE80211_QOS_CTL_LEN), &qos_ctl,
1994 			IEEE80211_QOS_CTL_LEN);
1995 	memcpy(skb_push(msdu, hdr_len), decap_hdr, hdr_len);
1996 }
1997 
1998 static void ath12k_dp_rx_h_undecap_raw(struct ath12k *ar, struct sk_buff *msdu,
1999 				       enum hal_encrypt_type enctype,
2000 				       struct ieee80211_rx_status *status,
2001 				       bool decrypted)
2002 {
2003 	struct ath12k_skb_rxcb *rxcb = ATH12K_SKB_RXCB(msdu);
2004 	struct ieee80211_hdr *hdr;
2005 	size_t hdr_len;
2006 	size_t crypto_len;
2007 
2008 	if (!rxcb->is_first_msdu ||
2009 	    !(rxcb->is_first_msdu && rxcb->is_last_msdu)) {
2010 		WARN_ON_ONCE(1);
2011 		return;
2012 	}
2013 
2014 	skb_trim(msdu, msdu->len - FCS_LEN);
2015 
2016 	if (!decrypted)
2017 		return;
2018 
2019 	hdr = (void *)msdu->data;
2020 
2021 	/* Tail */
2022 	if (status->flag & RX_FLAG_IV_STRIPPED) {
2023 		skb_trim(msdu, msdu->len -
2024 			 ath12k_dp_rx_crypto_mic_len(ar, enctype));
2025 
2026 		skb_trim(msdu, msdu->len -
2027 			 ath12k_dp_rx_crypto_icv_len(ar, enctype));
2028 	} else {
2029 		/* MIC */
2030 		if (status->flag & RX_FLAG_MIC_STRIPPED)
2031 			skb_trim(msdu, msdu->len -
2032 				 ath12k_dp_rx_crypto_mic_len(ar, enctype));
2033 
2034 		/* ICV */
2035 		if (status->flag & RX_FLAG_ICV_STRIPPED)
2036 			skb_trim(msdu, msdu->len -
2037 				 ath12k_dp_rx_crypto_icv_len(ar, enctype));
2038 	}
2039 
2040 	/* MMIC */
2041 	if ((status->flag & RX_FLAG_MMIC_STRIPPED) &&
2042 	    !ieee80211_has_morefrags(hdr->frame_control) &&
2043 	    enctype == HAL_ENCRYPT_TYPE_TKIP_MIC)
2044 		skb_trim(msdu, msdu->len - IEEE80211_CCMP_MIC_LEN);
2045 
2046 	/* Head */
2047 	if (status->flag & RX_FLAG_IV_STRIPPED) {
2048 		hdr_len = ieee80211_hdrlen(hdr->frame_control);
2049 		crypto_len = ath12k_dp_rx_crypto_param_len(ar, enctype);
2050 
2051 		memmove(msdu->data + crypto_len, msdu->data, hdr_len);
2052 		skb_pull(msdu, crypto_len);
2053 	}
2054 }
2055 
2056 static void ath12k_get_dot11_hdr_from_rx_desc(struct ath12k *ar,
2057 					      struct sk_buff *msdu,
2058 					      struct ath12k_skb_rxcb *rxcb,
2059 					      struct ieee80211_rx_status *status,
2060 					      enum hal_encrypt_type enctype)
2061 {
2062 	struct hal_rx_desc *rx_desc = rxcb->rx_desc;
2063 	struct ath12k_base *ab = ar->ab;
2064 	size_t hdr_len, crypto_len;
2065 	struct ieee80211_hdr *hdr;
2066 	u16 qos_ctl;
2067 	__le16 fc;
2068 	u8 *crypto_hdr;
2069 
2070 	if (!(status->flag & RX_FLAG_IV_STRIPPED)) {
2071 		crypto_len = ath12k_dp_rx_crypto_param_len(ar, enctype);
2072 		crypto_hdr = skb_push(msdu, crypto_len);
2073 		ath12k_dp_rx_desc_get_crypto_header(ab, rx_desc, crypto_hdr, enctype);
2074 	}
2075 
2076 	fc = cpu_to_le16(ath12k_dp_rxdesc_get_mpdu_frame_ctrl(ab, rx_desc));
2077 	hdr_len = ieee80211_hdrlen(fc);
2078 	skb_push(msdu, hdr_len);
2079 	hdr = (struct ieee80211_hdr *)msdu->data;
2080 	hdr->frame_control = fc;
2081 
2082 	/* Get wifi header from rx_desc */
2083 	ath12k_dp_rx_desc_get_dot11_hdr(ab, rx_desc, hdr);
2084 
2085 	if (rxcb->is_mcbc)
2086 		status->flag &= ~RX_FLAG_PN_VALIDATED;
2087 
2088 	/* Add QOS header */
2089 	if (ieee80211_is_data_qos(hdr->frame_control)) {
2090 		qos_ctl = rxcb->tid;
2091 		if (ath12k_dp_rx_h_mesh_ctl_present(ab, rx_desc))
2092 			qos_ctl |= IEEE80211_QOS_CTL_MESH_CONTROL_PRESENT;
2093 
2094 		/* TODO: Add other QoS ctl fields when required */
2095 		memcpy(msdu->data + (hdr_len - IEEE80211_QOS_CTL_LEN),
2096 		       &qos_ctl, IEEE80211_QOS_CTL_LEN);
2097 	}
2098 }
2099 
2100 static void ath12k_dp_rx_h_undecap_eth(struct ath12k *ar,
2101 				       struct sk_buff *msdu,
2102 				       enum hal_encrypt_type enctype,
2103 				       struct ieee80211_rx_status *status)
2104 {
2105 	struct ieee80211_hdr *hdr;
2106 	struct ethhdr *eth;
2107 	u8 da[ETH_ALEN];
2108 	u8 sa[ETH_ALEN];
2109 	struct ath12k_skb_rxcb *rxcb = ATH12K_SKB_RXCB(msdu);
2110 	struct ath12k_dp_rx_rfc1042_hdr rfc = {0xaa, 0xaa, 0x03, {0x00, 0x00, 0x00}};
2111 
2112 	eth = (struct ethhdr *)msdu->data;
2113 	ether_addr_copy(da, eth->h_dest);
2114 	ether_addr_copy(sa, eth->h_source);
2115 	rfc.snap_type = eth->h_proto;
2116 	skb_pull(msdu, sizeof(*eth));
2117 	memcpy(skb_push(msdu, sizeof(rfc)), &rfc,
2118 	       sizeof(rfc));
2119 	ath12k_get_dot11_hdr_from_rx_desc(ar, msdu, rxcb, status, enctype);
2120 
2121 	/* original 802.11 header has a different DA and in
2122 	 * case of 4addr it may also have different SA
2123 	 */
2124 	hdr = (struct ieee80211_hdr *)msdu->data;
2125 	ether_addr_copy(ieee80211_get_DA(hdr), da);
2126 	ether_addr_copy(ieee80211_get_SA(hdr), sa);
2127 }
2128 
2129 static void ath12k_dp_rx_h_undecap(struct ath12k *ar, struct sk_buff *msdu,
2130 				   struct hal_rx_desc *rx_desc,
2131 				   enum hal_encrypt_type enctype,
2132 				   struct ieee80211_rx_status *status,
2133 				   bool decrypted)
2134 {
2135 	struct ath12k_base *ab = ar->ab;
2136 	u8 decap;
2137 	struct ethhdr *ehdr;
2138 
2139 	decap = ath12k_dp_rx_h_decap_type(ab, rx_desc);
2140 
2141 	switch (decap) {
2142 	case DP_RX_DECAP_TYPE_NATIVE_WIFI:
2143 		ath12k_dp_rx_h_undecap_nwifi(ar, msdu, enctype, status);
2144 		break;
2145 	case DP_RX_DECAP_TYPE_RAW:
2146 		ath12k_dp_rx_h_undecap_raw(ar, msdu, enctype, status,
2147 					   decrypted);
2148 		break;
2149 	case DP_RX_DECAP_TYPE_ETHERNET2_DIX:
2150 		ehdr = (struct ethhdr *)msdu->data;
2151 
2152 		/* mac80211 allows fast path only for authorized STA */
2153 		if (ehdr->h_proto == cpu_to_be16(ETH_P_PAE)) {
2154 			ATH12K_SKB_RXCB(msdu)->is_eapol = true;
2155 			ath12k_dp_rx_h_undecap_eth(ar, msdu, enctype, status);
2156 			break;
2157 		}
2158 
2159 		/* PN for mcast packets will be validated in mac80211;
2160 		 * remove eth header and add 802.11 header.
2161 		 */
2162 		if (ATH12K_SKB_RXCB(msdu)->is_mcbc && decrypted)
2163 			ath12k_dp_rx_h_undecap_eth(ar, msdu, enctype, status);
2164 		break;
2165 	case DP_RX_DECAP_TYPE_8023:
2166 		/* TODO: Handle undecap for these formats */
2167 		break;
2168 	}
2169 }
2170 
2171 struct ath12k_peer *
2172 ath12k_dp_rx_h_find_peer(struct ath12k_base *ab, struct sk_buff *msdu)
2173 {
2174 	struct ath12k_skb_rxcb *rxcb = ATH12K_SKB_RXCB(msdu);
2175 	struct hal_rx_desc *rx_desc = rxcb->rx_desc;
2176 	struct ath12k_peer *peer = NULL;
2177 
2178 	lockdep_assert_held(&ab->base_lock);
2179 
2180 	if (rxcb->peer_id)
2181 		peer = ath12k_peer_find_by_id(ab, rxcb->peer_id);
2182 
2183 	if (peer)
2184 		return peer;
2185 
2186 	if (!rx_desc || !(ath12k_dp_rxdesc_mac_addr2_valid(ab, rx_desc)))
2187 		return NULL;
2188 
2189 	peer = ath12k_peer_find_by_addr(ab,
2190 					ath12k_dp_rxdesc_get_mpdu_start_addr2(ab,
2191 									      rx_desc));
2192 	return peer;
2193 }
2194 
2195 static void ath12k_dp_rx_h_mpdu(struct ath12k *ar,
2196 				struct sk_buff *msdu,
2197 				struct hal_rx_desc *rx_desc,
2198 				struct ieee80211_rx_status *rx_status)
2199 {
2200 	bool  fill_crypto_hdr;
2201 	struct ath12k_base *ab = ar->ab;
2202 	struct ath12k_skb_rxcb *rxcb;
2203 	enum hal_encrypt_type enctype;
2204 	bool is_decrypted = false;
2205 	struct ieee80211_hdr *hdr;
2206 	struct ath12k_peer *peer;
2207 	u32 err_bitmap;
2208 
2209 	/* PN for multicast packets will be checked in mac80211 */
2210 	rxcb = ATH12K_SKB_RXCB(msdu);
2211 	fill_crypto_hdr = ath12k_dp_rx_h_is_da_mcbc(ar->ab, rx_desc);
2212 	rxcb->is_mcbc = fill_crypto_hdr;
2213 
2214 	if (rxcb->is_mcbc)
2215 		rxcb->peer_id = ath12k_dp_rx_h_peer_id(ar->ab, rx_desc);
2216 
2217 	spin_lock_bh(&ar->ab->base_lock);
2218 	peer = ath12k_dp_rx_h_find_peer(ar->ab, msdu);
2219 	if (peer) {
2220 		if (rxcb->is_mcbc)
2221 			enctype = peer->sec_type_grp;
2222 		else
2223 			enctype = peer->sec_type;
2224 	} else {
2225 		enctype = HAL_ENCRYPT_TYPE_OPEN;
2226 	}
2227 	spin_unlock_bh(&ar->ab->base_lock);
2228 
2229 	err_bitmap = ath12k_dp_rx_h_mpdu_err(ab, rx_desc);
2230 	if (enctype != HAL_ENCRYPT_TYPE_OPEN && !err_bitmap)
2231 		is_decrypted = ath12k_dp_rx_h_is_decrypted(ab, rx_desc);
2232 
2233 	/* Clear per-MPDU flags while leaving per-PPDU flags intact */
2234 	rx_status->flag &= ~(RX_FLAG_FAILED_FCS_CRC |
2235 			     RX_FLAG_MMIC_ERROR |
2236 			     RX_FLAG_DECRYPTED |
2237 			     RX_FLAG_IV_STRIPPED |
2238 			     RX_FLAG_MMIC_STRIPPED);
2239 
2240 	if (err_bitmap & HAL_RX_MPDU_ERR_FCS)
2241 		rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
2242 	if (err_bitmap & HAL_RX_MPDU_ERR_TKIP_MIC)
2243 		rx_status->flag |= RX_FLAG_MMIC_ERROR;
2244 
2245 	if (is_decrypted) {
2246 		rx_status->flag |= RX_FLAG_DECRYPTED | RX_FLAG_MMIC_STRIPPED;
2247 
2248 		if (fill_crypto_hdr)
2249 			rx_status->flag |= RX_FLAG_MIC_STRIPPED |
2250 					RX_FLAG_ICV_STRIPPED;
2251 		else
2252 			rx_status->flag |= RX_FLAG_IV_STRIPPED |
2253 					   RX_FLAG_PN_VALIDATED;
2254 	}
2255 
2256 	ath12k_dp_rx_h_csum_offload(ar, msdu);
2257 	ath12k_dp_rx_h_undecap(ar, msdu, rx_desc,
2258 			       enctype, rx_status, is_decrypted);
2259 
2260 	if (!is_decrypted || fill_crypto_hdr)
2261 		return;
2262 
2263 	if (ath12k_dp_rx_h_decap_type(ar->ab, rx_desc) !=
2264 	    DP_RX_DECAP_TYPE_ETHERNET2_DIX) {
2265 		hdr = (void *)msdu->data;
2266 		hdr->frame_control &= ~__cpu_to_le16(IEEE80211_FCTL_PROTECTED);
2267 	}
2268 }
2269 
2270 static void ath12k_dp_rx_h_rate(struct ath12k *ar, struct hal_rx_desc *rx_desc,
2271 				struct ieee80211_rx_status *rx_status)
2272 {
2273 	struct ath12k_base *ab = ar->ab;
2274 	struct ieee80211_supported_band *sband;
2275 	enum rx_msdu_start_pkt_type pkt_type;
2276 	u8 bw;
2277 	u8 rate_mcs, nss;
2278 	u8 sgi;
2279 	bool is_cck;
2280 
2281 	pkt_type = ath12k_dp_rx_h_pkt_type(ab, rx_desc);
2282 	bw = ath12k_dp_rx_h_rx_bw(ab, rx_desc);
2283 	rate_mcs = ath12k_dp_rx_h_rate_mcs(ab, rx_desc);
2284 	nss = ath12k_dp_rx_h_nss(ab, rx_desc);
2285 	sgi = ath12k_dp_rx_h_sgi(ab, rx_desc);
2286 
2287 	switch (pkt_type) {
2288 	case RX_MSDU_START_PKT_TYPE_11A:
2289 	case RX_MSDU_START_PKT_TYPE_11B:
2290 		is_cck = (pkt_type == RX_MSDU_START_PKT_TYPE_11B);
2291 		sband = &ar->mac.sbands[rx_status->band];
2292 		rx_status->rate_idx = ath12k_mac_hw_rate_to_idx(sband, rate_mcs,
2293 								is_cck);
2294 		break;
2295 	case RX_MSDU_START_PKT_TYPE_11N:
2296 		rx_status->encoding = RX_ENC_HT;
2297 		if (rate_mcs > ATH12K_HT_MCS_MAX) {
2298 			ath12k_warn(ar->ab,
2299 				    "Received with invalid mcs in HT mode %d\n",
2300 				     rate_mcs);
2301 			break;
2302 		}
2303 		rx_status->rate_idx = rate_mcs + (8 * (nss - 1));
2304 		if (sgi)
2305 			rx_status->enc_flags |= RX_ENC_FLAG_SHORT_GI;
2306 		rx_status->bw = ath12k_mac_bw_to_mac80211_bw(bw);
2307 		break;
2308 	case RX_MSDU_START_PKT_TYPE_11AC:
2309 		rx_status->encoding = RX_ENC_VHT;
2310 		rx_status->rate_idx = rate_mcs;
2311 		if (rate_mcs > ATH12K_VHT_MCS_MAX) {
2312 			ath12k_warn(ar->ab,
2313 				    "Received with invalid mcs in VHT mode %d\n",
2314 				     rate_mcs);
2315 			break;
2316 		}
2317 		rx_status->nss = nss;
2318 		if (sgi)
2319 			rx_status->enc_flags |= RX_ENC_FLAG_SHORT_GI;
2320 		rx_status->bw = ath12k_mac_bw_to_mac80211_bw(bw);
2321 		break;
2322 	case RX_MSDU_START_PKT_TYPE_11AX:
2323 		rx_status->rate_idx = rate_mcs;
2324 		if (rate_mcs > ATH12K_HE_MCS_MAX) {
2325 			ath12k_warn(ar->ab,
2326 				    "Received with invalid mcs in HE mode %d\n",
2327 				    rate_mcs);
2328 			break;
2329 		}
2330 		rx_status->encoding = RX_ENC_HE;
2331 		rx_status->nss = nss;
2332 		rx_status->he_gi = ath12k_he_gi_to_nl80211_he_gi(sgi);
2333 		rx_status->bw = ath12k_mac_bw_to_mac80211_bw(bw);
2334 		break;
2335 	}
2336 }
2337 
2338 void ath12k_dp_rx_h_ppdu(struct ath12k *ar, struct hal_rx_desc *rx_desc,
2339 			 struct ieee80211_rx_status *rx_status)
2340 {
2341 	struct ath12k_base *ab = ar->ab;
2342 	u8 channel_num;
2343 	u32 center_freq, meta_data;
2344 	struct ieee80211_channel *channel;
2345 
2346 	rx_status->freq = 0;
2347 	rx_status->rate_idx = 0;
2348 	rx_status->nss = 0;
2349 	rx_status->encoding = RX_ENC_LEGACY;
2350 	rx_status->bw = RATE_INFO_BW_20;
2351 	rx_status->enc_flags = 0;
2352 
2353 	rx_status->flag |= RX_FLAG_NO_SIGNAL_VAL;
2354 
2355 	meta_data = ath12k_dp_rx_h_freq(ab, rx_desc);
2356 	channel_num = meta_data;
2357 	center_freq = meta_data >> 16;
2358 
2359 	if (center_freq >= 5935 && center_freq <= 7105) {
2360 		rx_status->band = NL80211_BAND_6GHZ;
2361 	} else if (channel_num >= 1 && channel_num <= 14) {
2362 		rx_status->band = NL80211_BAND_2GHZ;
2363 	} else if (channel_num >= 36 && channel_num <= 173) {
2364 		rx_status->band = NL80211_BAND_5GHZ;
2365 	} else {
2366 		spin_lock_bh(&ar->data_lock);
2367 		channel = ar->rx_channel;
2368 		if (channel) {
2369 			rx_status->band = channel->band;
2370 			channel_num =
2371 				ieee80211_frequency_to_channel(channel->center_freq);
2372 		}
2373 		spin_unlock_bh(&ar->data_lock);
2374 		ath12k_dbg_dump(ar->ab, ATH12K_DBG_DATA, NULL, "rx_desc: ",
2375 				rx_desc, sizeof(*rx_desc));
2376 	}
2377 
2378 	rx_status->freq = ieee80211_channel_to_frequency(channel_num,
2379 							 rx_status->band);
2380 
2381 	ath12k_dp_rx_h_rate(ar, rx_desc, rx_status);
2382 }
2383 
2384 static void ath12k_dp_rx_deliver_msdu(struct ath12k *ar, struct napi_struct *napi,
2385 				      struct sk_buff *msdu,
2386 				      struct ieee80211_rx_status *status)
2387 {
2388 	struct ath12k_base *ab = ar->ab;
2389 	static const struct ieee80211_radiotap_he known = {
2390 		.data1 = cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA1_DATA_MCS_KNOWN |
2391 				     IEEE80211_RADIOTAP_HE_DATA1_BW_RU_ALLOC_KNOWN),
2392 		.data2 = cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA2_GI_KNOWN),
2393 	};
2394 	struct ieee80211_radiotap_he *he;
2395 	struct ieee80211_rx_status *rx_status;
2396 	struct ieee80211_sta *pubsta;
2397 	struct ath12k_peer *peer;
2398 	struct ath12k_skb_rxcb *rxcb = ATH12K_SKB_RXCB(msdu);
2399 	u8 decap = DP_RX_DECAP_TYPE_RAW;
2400 	bool is_mcbc = rxcb->is_mcbc;
2401 	bool is_eapol = rxcb->is_eapol;
2402 
2403 	if (status->encoding == RX_ENC_HE && !(status->flag & RX_FLAG_RADIOTAP_HE) &&
2404 	    !(status->flag & RX_FLAG_SKIP_MONITOR)) {
2405 		he = skb_push(msdu, sizeof(known));
2406 		memcpy(he, &known, sizeof(known));
2407 		status->flag |= RX_FLAG_RADIOTAP_HE;
2408 	}
2409 
2410 	if (!(status->flag & RX_FLAG_ONLY_MONITOR))
2411 		decap = ath12k_dp_rx_h_decap_type(ab, rxcb->rx_desc);
2412 
2413 	spin_lock_bh(&ab->base_lock);
2414 	peer = ath12k_dp_rx_h_find_peer(ab, msdu);
2415 
2416 	pubsta = peer ? peer->sta : NULL;
2417 
2418 	spin_unlock_bh(&ab->base_lock);
2419 
2420 	ath12k_dbg(ab, ATH12K_DBG_DATA,
2421 		   "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",
2422 		   msdu,
2423 		   msdu->len,
2424 		   peer ? peer->addr : NULL,
2425 		   rxcb->tid,
2426 		   is_mcbc ? "mcast" : "ucast",
2427 		   ath12k_dp_rx_h_seq_no(ab, rxcb->rx_desc),
2428 		   (status->encoding == RX_ENC_LEGACY) ? "legacy" : "",
2429 		   (status->encoding == RX_ENC_HT) ? "ht" : "",
2430 		   (status->encoding == RX_ENC_VHT) ? "vht" : "",
2431 		   (status->encoding == RX_ENC_HE) ? "he" : "",
2432 		   (status->bw == RATE_INFO_BW_40) ? "40" : "",
2433 		   (status->bw == RATE_INFO_BW_80) ? "80" : "",
2434 		   (status->bw == RATE_INFO_BW_160) ? "160" : "",
2435 		   status->enc_flags & RX_ENC_FLAG_SHORT_GI ? "sgi " : "",
2436 		   status->rate_idx,
2437 		   status->nss,
2438 		   status->freq,
2439 		   status->band, status->flag,
2440 		   !!(status->flag & RX_FLAG_FAILED_FCS_CRC),
2441 		   !!(status->flag & RX_FLAG_MMIC_ERROR),
2442 		   !!(status->flag & RX_FLAG_AMSDU_MORE));
2443 
2444 	ath12k_dbg_dump(ab, ATH12K_DBG_DP_RX, NULL, "dp rx msdu: ",
2445 			msdu->data, msdu->len);
2446 
2447 	rx_status = IEEE80211_SKB_RXCB(msdu);
2448 	*rx_status = *status;
2449 
2450 	/* TODO: trace rx packet */
2451 
2452 	/* PN for multicast packets are not validate in HW,
2453 	 * so skip 802.3 rx path
2454 	 * Also, fast_rx expects the STA to be authorized, hence
2455 	 * eapol packets are sent in slow path.
2456 	 */
2457 	if (decap == DP_RX_DECAP_TYPE_ETHERNET2_DIX && !is_eapol &&
2458 	    !(is_mcbc && rx_status->flag & RX_FLAG_DECRYPTED))
2459 		rx_status->flag |= RX_FLAG_8023;
2460 
2461 	ieee80211_rx_napi(ar->hw, pubsta, msdu, napi);
2462 }
2463 
2464 static int ath12k_dp_rx_process_msdu(struct ath12k *ar,
2465 				     struct sk_buff *msdu,
2466 				     struct sk_buff_head *msdu_list,
2467 				     struct ieee80211_rx_status *rx_status)
2468 {
2469 	struct ath12k_base *ab = ar->ab;
2470 	struct hal_rx_desc *rx_desc, *lrx_desc;
2471 	struct ath12k_skb_rxcb *rxcb;
2472 	struct sk_buff *last_buf;
2473 	u8 l3_pad_bytes;
2474 	u16 msdu_len;
2475 	int ret;
2476 	u32 hal_rx_desc_sz = ar->ab->hw_params->hal_desc_sz;
2477 
2478 	last_buf = ath12k_dp_rx_get_msdu_last_buf(msdu_list, msdu);
2479 	if (!last_buf) {
2480 		ath12k_warn(ab,
2481 			    "No valid Rx buffer to access MSDU_END tlv\n");
2482 		ret = -EIO;
2483 		goto free_out;
2484 	}
2485 
2486 	rx_desc = (struct hal_rx_desc *)msdu->data;
2487 	lrx_desc = (struct hal_rx_desc *)last_buf->data;
2488 	if (!ath12k_dp_rx_h_msdu_done(ab, lrx_desc)) {
2489 		ath12k_warn(ab, "msdu_done bit in msdu_end is not set\n");
2490 		ret = -EIO;
2491 		goto free_out;
2492 	}
2493 
2494 	rxcb = ATH12K_SKB_RXCB(msdu);
2495 	rxcb->rx_desc = rx_desc;
2496 	msdu_len = ath12k_dp_rx_h_msdu_len(ab, lrx_desc);
2497 	l3_pad_bytes = ath12k_dp_rx_h_l3pad(ab, lrx_desc);
2498 
2499 	if (rxcb->is_frag) {
2500 		skb_pull(msdu, hal_rx_desc_sz);
2501 	} else if (!rxcb->is_continuation) {
2502 		if ((msdu_len + hal_rx_desc_sz) > DP_RX_BUFFER_SIZE) {
2503 			ret = -EINVAL;
2504 			ath12k_warn(ab, "invalid msdu len %u\n", msdu_len);
2505 			ath12k_dbg_dump(ab, ATH12K_DBG_DATA, NULL, "", rx_desc,
2506 					sizeof(*rx_desc));
2507 			goto free_out;
2508 		}
2509 		skb_put(msdu, hal_rx_desc_sz + l3_pad_bytes + msdu_len);
2510 		skb_pull(msdu, hal_rx_desc_sz + l3_pad_bytes);
2511 	} else {
2512 		ret = ath12k_dp_rx_msdu_coalesce(ar, msdu_list,
2513 						 msdu, last_buf,
2514 						 l3_pad_bytes, msdu_len);
2515 		if (ret) {
2516 			ath12k_warn(ab,
2517 				    "failed to coalesce msdu rx buffer%d\n", ret);
2518 			goto free_out;
2519 		}
2520 	}
2521 
2522 	ath12k_dp_rx_h_ppdu(ar, rx_desc, rx_status);
2523 	ath12k_dp_rx_h_mpdu(ar, msdu, rx_desc, rx_status);
2524 
2525 	rx_status->flag |= RX_FLAG_SKIP_MONITOR | RX_FLAG_DUP_VALIDATED;
2526 
2527 	return 0;
2528 
2529 free_out:
2530 	return ret;
2531 }
2532 
2533 static void ath12k_dp_rx_process_received_packets(struct ath12k_base *ab,
2534 						  struct napi_struct *napi,
2535 						  struct sk_buff_head *msdu_list,
2536 						  int ring_id)
2537 {
2538 	struct ieee80211_rx_status rx_status = {0};
2539 	struct ath12k_skb_rxcb *rxcb;
2540 	struct sk_buff *msdu;
2541 	struct ath12k *ar;
2542 	u8 mac_id;
2543 	int ret;
2544 
2545 	if (skb_queue_empty(msdu_list))
2546 		return;
2547 
2548 	rcu_read_lock();
2549 
2550 	while ((msdu = __skb_dequeue(msdu_list))) {
2551 		rxcb = ATH12K_SKB_RXCB(msdu);
2552 		mac_id = rxcb->mac_id;
2553 		ar = ab->pdevs[mac_id].ar;
2554 		if (!rcu_dereference(ab->pdevs_active[mac_id])) {
2555 			dev_kfree_skb_any(msdu);
2556 			continue;
2557 		}
2558 
2559 		if (test_bit(ATH12K_CAC_RUNNING, &ar->dev_flags)) {
2560 			dev_kfree_skb_any(msdu);
2561 			continue;
2562 		}
2563 
2564 		ret = ath12k_dp_rx_process_msdu(ar, msdu, msdu_list, &rx_status);
2565 		if (ret) {
2566 			ath12k_dbg(ab, ATH12K_DBG_DATA,
2567 				   "Unable to process msdu %d", ret);
2568 			dev_kfree_skb_any(msdu);
2569 			continue;
2570 		}
2571 
2572 		ath12k_dp_rx_deliver_msdu(ar, napi, msdu, &rx_status);
2573 	}
2574 
2575 	rcu_read_unlock();
2576 }
2577 
2578 int ath12k_dp_rx_process(struct ath12k_base *ab, int ring_id,
2579 			 struct napi_struct *napi, int budget)
2580 {
2581 	struct ath12k_rx_desc_info *desc_info;
2582 	struct ath12k_dp *dp = &ab->dp;
2583 	struct dp_rxdma_ring *rx_ring = &dp->rx_refill_buf_ring;
2584 	struct hal_reo_dest_ring *desc;
2585 	int num_buffs_reaped = 0;
2586 	struct sk_buff_head msdu_list;
2587 	struct ath12k_skb_rxcb *rxcb;
2588 	int total_msdu_reaped = 0;
2589 	struct hal_srng *srng;
2590 	struct sk_buff *msdu;
2591 	bool done = false;
2592 	int mac_id;
2593 	u64 desc_va;
2594 
2595 	__skb_queue_head_init(&msdu_list);
2596 
2597 	srng = &ab->hal.srng_list[dp->reo_dst_ring[ring_id].ring_id];
2598 
2599 	spin_lock_bh(&srng->lock);
2600 
2601 try_again:
2602 	ath12k_hal_srng_access_begin(ab, srng);
2603 
2604 	while ((desc = ath12k_hal_srng_dst_get_next_entry(ab, srng))) {
2605 		enum hal_reo_dest_ring_push_reason push_reason;
2606 		u32 cookie;
2607 
2608 		cookie = le32_get_bits(desc->buf_addr_info.info1,
2609 				       BUFFER_ADDR_INFO1_SW_COOKIE);
2610 
2611 		mac_id = le32_get_bits(desc->info0,
2612 				       HAL_REO_DEST_RING_INFO0_SRC_LINK_ID);
2613 
2614 		desc_va = ((u64)le32_to_cpu(desc->buf_va_hi) << 32 |
2615 			   le32_to_cpu(desc->buf_va_lo));
2616 		desc_info = (struct ath12k_rx_desc_info *)((unsigned long)desc_va);
2617 
2618 		/* retry manual desc retrieval */
2619 		if (!desc_info) {
2620 			desc_info = ath12k_dp_get_rx_desc(ab, cookie);
2621 			if (!desc_info) {
2622 				ath12k_warn(ab, "Invalid cookie in manual desc retrieval");
2623 				continue;
2624 			}
2625 		}
2626 
2627 		if (desc_info->magic != ATH12K_DP_RX_DESC_MAGIC)
2628 			ath12k_warn(ab, "Check HW CC implementation");
2629 
2630 		msdu = desc_info->skb;
2631 		desc_info->skb = NULL;
2632 
2633 		spin_lock_bh(&dp->rx_desc_lock);
2634 		list_move_tail(&desc_info->list, &dp->rx_desc_free_list);
2635 		spin_unlock_bh(&dp->rx_desc_lock);
2636 
2637 		rxcb = ATH12K_SKB_RXCB(msdu);
2638 		dma_unmap_single(ab->dev, rxcb->paddr,
2639 				 msdu->len + skb_tailroom(msdu),
2640 				 DMA_FROM_DEVICE);
2641 
2642 		num_buffs_reaped++;
2643 
2644 		push_reason = le32_get_bits(desc->info0,
2645 					    HAL_REO_DEST_RING_INFO0_PUSH_REASON);
2646 		if (push_reason !=
2647 		    HAL_REO_DEST_RING_PUSH_REASON_ROUTING_INSTRUCTION) {
2648 			dev_kfree_skb_any(msdu);
2649 			ab->soc_stats.hal_reo_error[dp->reo_dst_ring[ring_id].ring_id]++;
2650 			continue;
2651 		}
2652 
2653 		rxcb->is_first_msdu = !!(le32_to_cpu(desc->rx_msdu_info.info0) &
2654 					 RX_MSDU_DESC_INFO0_FIRST_MSDU_IN_MPDU);
2655 		rxcb->is_last_msdu = !!(le32_to_cpu(desc->rx_msdu_info.info0) &
2656 					RX_MSDU_DESC_INFO0_LAST_MSDU_IN_MPDU);
2657 		rxcb->is_continuation = !!(le32_to_cpu(desc->rx_msdu_info.info0) &
2658 					   RX_MSDU_DESC_INFO0_MSDU_CONTINUATION);
2659 		rxcb->mac_id = mac_id;
2660 		rxcb->peer_id = le32_get_bits(desc->rx_mpdu_info.peer_meta_data,
2661 					      RX_MPDU_DESC_META_DATA_PEER_ID);
2662 		rxcb->tid = le32_get_bits(desc->rx_mpdu_info.info0,
2663 					  RX_MPDU_DESC_INFO0_TID);
2664 
2665 		__skb_queue_tail(&msdu_list, msdu);
2666 
2667 		if (!rxcb->is_continuation) {
2668 			total_msdu_reaped++;
2669 			done = true;
2670 		} else {
2671 			done = false;
2672 		}
2673 
2674 		if (total_msdu_reaped >= budget)
2675 			break;
2676 	}
2677 
2678 	/* Hw might have updated the head pointer after we cached it.
2679 	 * In this case, even though there are entries in the ring we'll
2680 	 * get rx_desc NULL. Give the read another try with updated cached
2681 	 * head pointer so that we can reap complete MPDU in the current
2682 	 * rx processing.
2683 	 */
2684 	if (!done && ath12k_hal_srng_dst_num_free(ab, srng, true)) {
2685 		ath12k_hal_srng_access_end(ab, srng);
2686 		goto try_again;
2687 	}
2688 
2689 	ath12k_hal_srng_access_end(ab, srng);
2690 
2691 	spin_unlock_bh(&srng->lock);
2692 
2693 	if (!total_msdu_reaped)
2694 		goto exit;
2695 
2696 	/* TODO: Move to implicit BM? */
2697 	ath12k_dp_rx_bufs_replenish(ab, 0, rx_ring, num_buffs_reaped,
2698 				    ab->hw_params->hal_params->rx_buf_rbm, true);
2699 
2700 	ath12k_dp_rx_process_received_packets(ab, napi, &msdu_list,
2701 					      ring_id);
2702 
2703 exit:
2704 	return total_msdu_reaped;
2705 }
2706 
2707 static void ath12k_dp_rx_frag_timer(struct timer_list *timer)
2708 {
2709 	struct ath12k_dp_rx_tid *rx_tid = from_timer(rx_tid, timer, frag_timer);
2710 
2711 	spin_lock_bh(&rx_tid->ab->base_lock);
2712 	if (rx_tid->last_frag_no &&
2713 	    rx_tid->rx_frag_bitmap == GENMASK(rx_tid->last_frag_no, 0)) {
2714 		spin_unlock_bh(&rx_tid->ab->base_lock);
2715 		return;
2716 	}
2717 	ath12k_dp_rx_frags_cleanup(rx_tid, true);
2718 	spin_unlock_bh(&rx_tid->ab->base_lock);
2719 }
2720 
2721 int ath12k_dp_rx_peer_frag_setup(struct ath12k *ar, const u8 *peer_mac, int vdev_id)
2722 {
2723 	struct ath12k_base *ab = ar->ab;
2724 	struct crypto_shash *tfm;
2725 	struct ath12k_peer *peer;
2726 	struct ath12k_dp_rx_tid *rx_tid;
2727 	int i;
2728 
2729 	tfm = crypto_alloc_shash("michael_mic", 0, 0);
2730 	if (IS_ERR(tfm))
2731 		return PTR_ERR(tfm);
2732 
2733 	spin_lock_bh(&ab->base_lock);
2734 
2735 	peer = ath12k_peer_find(ab, vdev_id, peer_mac);
2736 	if (!peer) {
2737 		spin_unlock_bh(&ab->base_lock);
2738 		ath12k_warn(ab, "failed to find the peer to set up fragment info\n");
2739 		return -ENOENT;
2740 	}
2741 
2742 	for (i = 0; i <= IEEE80211_NUM_TIDS; i++) {
2743 		rx_tid = &peer->rx_tid[i];
2744 		rx_tid->ab = ab;
2745 		timer_setup(&rx_tid->frag_timer, ath12k_dp_rx_frag_timer, 0);
2746 		skb_queue_head_init(&rx_tid->rx_frags);
2747 	}
2748 
2749 	peer->tfm_mmic = tfm;
2750 	spin_unlock_bh(&ab->base_lock);
2751 
2752 	return 0;
2753 }
2754 
2755 static int ath12k_dp_rx_h_michael_mic(struct crypto_shash *tfm, u8 *key,
2756 				      struct ieee80211_hdr *hdr, u8 *data,
2757 				      size_t data_len, u8 *mic)
2758 {
2759 	SHASH_DESC_ON_STACK(desc, tfm);
2760 	u8 mic_hdr[16] = {0};
2761 	u8 tid = 0;
2762 	int ret;
2763 
2764 	if (!tfm)
2765 		return -EINVAL;
2766 
2767 	desc->tfm = tfm;
2768 
2769 	ret = crypto_shash_setkey(tfm, key, 8);
2770 	if (ret)
2771 		goto out;
2772 
2773 	ret = crypto_shash_init(desc);
2774 	if (ret)
2775 		goto out;
2776 
2777 	/* TKIP MIC header */
2778 	memcpy(mic_hdr, ieee80211_get_DA(hdr), ETH_ALEN);
2779 	memcpy(mic_hdr + ETH_ALEN, ieee80211_get_SA(hdr), ETH_ALEN);
2780 	if (ieee80211_is_data_qos(hdr->frame_control))
2781 		tid = ieee80211_get_tid(hdr);
2782 	mic_hdr[12] = tid;
2783 
2784 	ret = crypto_shash_update(desc, mic_hdr, 16);
2785 	if (ret)
2786 		goto out;
2787 	ret = crypto_shash_update(desc, data, data_len);
2788 	if (ret)
2789 		goto out;
2790 	ret = crypto_shash_final(desc, mic);
2791 out:
2792 	shash_desc_zero(desc);
2793 	return ret;
2794 }
2795 
2796 static int ath12k_dp_rx_h_verify_tkip_mic(struct ath12k *ar, struct ath12k_peer *peer,
2797 					  struct sk_buff *msdu)
2798 {
2799 	struct ath12k_base *ab = ar->ab;
2800 	struct hal_rx_desc *rx_desc = (struct hal_rx_desc *)msdu->data;
2801 	struct ieee80211_rx_status *rxs = IEEE80211_SKB_RXCB(msdu);
2802 	struct ieee80211_key_conf *key_conf;
2803 	struct ieee80211_hdr *hdr;
2804 	u8 mic[IEEE80211_CCMP_MIC_LEN];
2805 	int head_len, tail_len, ret;
2806 	size_t data_len;
2807 	u32 hdr_len, hal_rx_desc_sz = ar->ab->hw_params->hal_desc_sz;
2808 	u8 *key, *data;
2809 	u8 key_idx;
2810 
2811 	if (ath12k_dp_rx_h_enctype(ab, rx_desc) != HAL_ENCRYPT_TYPE_TKIP_MIC)
2812 		return 0;
2813 
2814 	hdr = (struct ieee80211_hdr *)(msdu->data + hal_rx_desc_sz);
2815 	hdr_len = ieee80211_hdrlen(hdr->frame_control);
2816 	head_len = hdr_len + hal_rx_desc_sz + IEEE80211_TKIP_IV_LEN;
2817 	tail_len = IEEE80211_CCMP_MIC_LEN + IEEE80211_TKIP_ICV_LEN + FCS_LEN;
2818 
2819 	if (!is_multicast_ether_addr(hdr->addr1))
2820 		key_idx = peer->ucast_keyidx;
2821 	else
2822 		key_idx = peer->mcast_keyidx;
2823 
2824 	key_conf = peer->keys[key_idx];
2825 
2826 	data = msdu->data + head_len;
2827 	data_len = msdu->len - head_len - tail_len;
2828 	key = &key_conf->key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY];
2829 
2830 	ret = ath12k_dp_rx_h_michael_mic(peer->tfm_mmic, key, hdr, data, data_len, mic);
2831 	if (ret || memcmp(mic, data + data_len, IEEE80211_CCMP_MIC_LEN))
2832 		goto mic_fail;
2833 
2834 	return 0;
2835 
2836 mic_fail:
2837 	(ATH12K_SKB_RXCB(msdu))->is_first_msdu = true;
2838 	(ATH12K_SKB_RXCB(msdu))->is_last_msdu = true;
2839 
2840 	rxs->flag |= RX_FLAG_MMIC_ERROR | RX_FLAG_MMIC_STRIPPED |
2841 		    RX_FLAG_IV_STRIPPED | RX_FLAG_DECRYPTED;
2842 	skb_pull(msdu, hal_rx_desc_sz);
2843 
2844 	ath12k_dp_rx_h_ppdu(ar, rx_desc, rxs);
2845 	ath12k_dp_rx_h_undecap(ar, msdu, rx_desc,
2846 			       HAL_ENCRYPT_TYPE_TKIP_MIC, rxs, true);
2847 	ieee80211_rx(ar->hw, msdu);
2848 	return -EINVAL;
2849 }
2850 
2851 static void ath12k_dp_rx_h_undecap_frag(struct ath12k *ar, struct sk_buff *msdu,
2852 					enum hal_encrypt_type enctype, u32 flags)
2853 {
2854 	struct ieee80211_hdr *hdr;
2855 	size_t hdr_len;
2856 	size_t crypto_len;
2857 	u32 hal_rx_desc_sz = ar->ab->hw_params->hal_desc_sz;
2858 
2859 	if (!flags)
2860 		return;
2861 
2862 	hdr = (struct ieee80211_hdr *)(msdu->data + hal_rx_desc_sz);
2863 
2864 	if (flags & RX_FLAG_MIC_STRIPPED)
2865 		skb_trim(msdu, msdu->len -
2866 			 ath12k_dp_rx_crypto_mic_len(ar, enctype));
2867 
2868 	if (flags & RX_FLAG_ICV_STRIPPED)
2869 		skb_trim(msdu, msdu->len -
2870 			 ath12k_dp_rx_crypto_icv_len(ar, enctype));
2871 
2872 	if (flags & RX_FLAG_IV_STRIPPED) {
2873 		hdr_len = ieee80211_hdrlen(hdr->frame_control);
2874 		crypto_len = ath12k_dp_rx_crypto_param_len(ar, enctype);
2875 
2876 		memmove(msdu->data + hal_rx_desc_sz + crypto_len,
2877 			msdu->data + hal_rx_desc_sz, hdr_len);
2878 		skb_pull(msdu, crypto_len);
2879 	}
2880 }
2881 
2882 static int ath12k_dp_rx_h_defrag(struct ath12k *ar,
2883 				 struct ath12k_peer *peer,
2884 				 struct ath12k_dp_rx_tid *rx_tid,
2885 				 struct sk_buff **defrag_skb)
2886 {
2887 	struct ath12k_base *ab = ar->ab;
2888 	struct hal_rx_desc *rx_desc;
2889 	struct sk_buff *skb, *first_frag, *last_frag;
2890 	struct ieee80211_hdr *hdr;
2891 	enum hal_encrypt_type enctype;
2892 	bool is_decrypted = false;
2893 	int msdu_len = 0;
2894 	int extra_space;
2895 	u32 flags, hal_rx_desc_sz = ar->ab->hw_params->hal_desc_sz;
2896 
2897 	first_frag = skb_peek(&rx_tid->rx_frags);
2898 	last_frag = skb_peek_tail(&rx_tid->rx_frags);
2899 
2900 	skb_queue_walk(&rx_tid->rx_frags, skb) {
2901 		flags = 0;
2902 		rx_desc = (struct hal_rx_desc *)skb->data;
2903 		hdr = (struct ieee80211_hdr *)(skb->data + hal_rx_desc_sz);
2904 
2905 		enctype = ath12k_dp_rx_h_enctype(ab, rx_desc);
2906 		if (enctype != HAL_ENCRYPT_TYPE_OPEN)
2907 			is_decrypted = ath12k_dp_rx_h_is_decrypted(ab,
2908 								   rx_desc);
2909 
2910 		if (is_decrypted) {
2911 			if (skb != first_frag)
2912 				flags |= RX_FLAG_IV_STRIPPED;
2913 			if (skb != last_frag)
2914 				flags |= RX_FLAG_ICV_STRIPPED |
2915 					 RX_FLAG_MIC_STRIPPED;
2916 		}
2917 
2918 		/* RX fragments are always raw packets */
2919 		if (skb != last_frag)
2920 			skb_trim(skb, skb->len - FCS_LEN);
2921 		ath12k_dp_rx_h_undecap_frag(ar, skb, enctype, flags);
2922 
2923 		if (skb != first_frag)
2924 			skb_pull(skb, hal_rx_desc_sz +
2925 				      ieee80211_hdrlen(hdr->frame_control));
2926 		msdu_len += skb->len;
2927 	}
2928 
2929 	extra_space = msdu_len - (DP_RX_BUFFER_SIZE + skb_tailroom(first_frag));
2930 	if (extra_space > 0 &&
2931 	    (pskb_expand_head(first_frag, 0, extra_space, GFP_ATOMIC) < 0))
2932 		return -ENOMEM;
2933 
2934 	__skb_unlink(first_frag, &rx_tid->rx_frags);
2935 	while ((skb = __skb_dequeue(&rx_tid->rx_frags))) {
2936 		skb_put_data(first_frag, skb->data, skb->len);
2937 		dev_kfree_skb_any(skb);
2938 	}
2939 
2940 	hdr = (struct ieee80211_hdr *)(first_frag->data + hal_rx_desc_sz);
2941 	hdr->frame_control &= ~__cpu_to_le16(IEEE80211_FCTL_MOREFRAGS);
2942 	ATH12K_SKB_RXCB(first_frag)->is_frag = 1;
2943 
2944 	if (ath12k_dp_rx_h_verify_tkip_mic(ar, peer, first_frag))
2945 		first_frag = NULL;
2946 
2947 	*defrag_skb = first_frag;
2948 	return 0;
2949 }
2950 
2951 static int ath12k_dp_rx_h_defrag_reo_reinject(struct ath12k *ar,
2952 					      struct ath12k_dp_rx_tid *rx_tid,
2953 					      struct sk_buff *defrag_skb)
2954 {
2955 	struct ath12k_base *ab = ar->ab;
2956 	struct ath12k_dp *dp = &ab->dp;
2957 	struct hal_rx_desc *rx_desc = (struct hal_rx_desc *)defrag_skb->data;
2958 	struct hal_reo_entrance_ring *reo_ent_ring;
2959 	struct hal_reo_dest_ring *reo_dest_ring;
2960 	struct dp_link_desc_bank *link_desc_banks;
2961 	struct hal_rx_msdu_link *msdu_link;
2962 	struct hal_rx_msdu_details *msdu0;
2963 	struct hal_srng *srng;
2964 	dma_addr_t link_paddr, buf_paddr;
2965 	u32 desc_bank, msdu_info, msdu_ext_info, mpdu_info;
2966 	u32 cookie, hal_rx_desc_sz, dest_ring_info0;
2967 	int ret;
2968 	struct ath12k_rx_desc_info *desc_info;
2969 	u8 dst_ind;
2970 
2971 	hal_rx_desc_sz = ab->hw_params->hal_desc_sz;
2972 	link_desc_banks = dp->link_desc_banks;
2973 	reo_dest_ring = rx_tid->dst_ring_desc;
2974 
2975 	ath12k_hal_rx_reo_ent_paddr_get(ab, &reo_dest_ring->buf_addr_info,
2976 					&link_paddr, &cookie);
2977 	desc_bank = u32_get_bits(cookie, DP_LINK_DESC_BANK_MASK);
2978 
2979 	msdu_link = (struct hal_rx_msdu_link *)(link_desc_banks[desc_bank].vaddr +
2980 			(link_paddr - link_desc_banks[desc_bank].paddr));
2981 	msdu0 = &msdu_link->msdu_link[0];
2982 	msdu_ext_info = le32_to_cpu(msdu0->rx_msdu_ext_info.info0);
2983 	dst_ind = u32_get_bits(msdu_ext_info, RX_MSDU_EXT_DESC_INFO0_REO_DEST_IND);
2984 
2985 	memset(msdu0, 0, sizeof(*msdu0));
2986 
2987 	msdu_info = u32_encode_bits(1, RX_MSDU_DESC_INFO0_FIRST_MSDU_IN_MPDU) |
2988 		    u32_encode_bits(1, RX_MSDU_DESC_INFO0_LAST_MSDU_IN_MPDU) |
2989 		    u32_encode_bits(0, RX_MSDU_DESC_INFO0_MSDU_CONTINUATION) |
2990 		    u32_encode_bits(defrag_skb->len - hal_rx_desc_sz,
2991 				    RX_MSDU_DESC_INFO0_MSDU_LENGTH) |
2992 		    u32_encode_bits(1, RX_MSDU_DESC_INFO0_VALID_SA) |
2993 		    u32_encode_bits(1, RX_MSDU_DESC_INFO0_VALID_DA);
2994 	msdu0->rx_msdu_info.info0 = cpu_to_le32(msdu_info);
2995 	msdu0->rx_msdu_ext_info.info0 = cpu_to_le32(msdu_ext_info);
2996 
2997 	/* change msdu len in hal rx desc */
2998 	ath12k_dp_rxdesc_set_msdu_len(ab, rx_desc, defrag_skb->len - hal_rx_desc_sz);
2999 
3000 	buf_paddr = dma_map_single(ab->dev, defrag_skb->data,
3001 				   defrag_skb->len + skb_tailroom(defrag_skb),
3002 				   DMA_FROM_DEVICE);
3003 	if (dma_mapping_error(ab->dev, buf_paddr))
3004 		return -ENOMEM;
3005 
3006 	spin_lock_bh(&dp->rx_desc_lock);
3007 	desc_info = list_first_entry_or_null(&dp->rx_desc_free_list,
3008 					     struct ath12k_rx_desc_info,
3009 					     list);
3010 	if (!desc_info) {
3011 		spin_unlock_bh(&dp->rx_desc_lock);
3012 		ath12k_warn(ab, "failed to find rx desc for reinject\n");
3013 		ret = -ENOMEM;
3014 		goto err_unmap_dma;
3015 	}
3016 
3017 	desc_info->skb = defrag_skb;
3018 
3019 	list_del(&desc_info->list);
3020 	list_add_tail(&desc_info->list, &dp->rx_desc_used_list);
3021 	spin_unlock_bh(&dp->rx_desc_lock);
3022 
3023 	ATH12K_SKB_RXCB(defrag_skb)->paddr = buf_paddr;
3024 
3025 	ath12k_hal_rx_buf_addr_info_set(&msdu0->buf_addr_info, buf_paddr,
3026 					desc_info->cookie,
3027 					HAL_RX_BUF_RBM_SW3_BM);
3028 
3029 	/* Fill mpdu details into reo entrace ring */
3030 	srng = &ab->hal.srng_list[dp->reo_reinject_ring.ring_id];
3031 
3032 	spin_lock_bh(&srng->lock);
3033 	ath12k_hal_srng_access_begin(ab, srng);
3034 
3035 	reo_ent_ring = ath12k_hal_srng_src_get_next_entry(ab, srng);
3036 	if (!reo_ent_ring) {
3037 		ath12k_hal_srng_access_end(ab, srng);
3038 		spin_unlock_bh(&srng->lock);
3039 		ret = -ENOSPC;
3040 		goto err_free_desc;
3041 	}
3042 	memset(reo_ent_ring, 0, sizeof(*reo_ent_ring));
3043 
3044 	ath12k_hal_rx_buf_addr_info_set(&reo_ent_ring->buf_addr_info, link_paddr,
3045 					cookie,
3046 					HAL_RX_BUF_RBM_WBM_CHIP0_IDLE_DESC_LIST);
3047 
3048 	mpdu_info = u32_encode_bits(1, RX_MPDU_DESC_INFO0_MSDU_COUNT) |
3049 		    u32_encode_bits(0, RX_MPDU_DESC_INFO0_FRAG_FLAG) |
3050 		    u32_encode_bits(1, RX_MPDU_DESC_INFO0_RAW_MPDU) |
3051 		    u32_encode_bits(1, RX_MPDU_DESC_INFO0_VALID_PN) |
3052 		    u32_encode_bits(rx_tid->tid, RX_MPDU_DESC_INFO0_TID);
3053 
3054 	reo_ent_ring->rx_mpdu_info.info0 = cpu_to_le32(mpdu_info);
3055 	reo_ent_ring->rx_mpdu_info.peer_meta_data =
3056 		reo_dest_ring->rx_mpdu_info.peer_meta_data;
3057 
3058 	/* Firmware expects physical address to be filled in queue_addr_lo in
3059 	 * the MLO scenario and in case of non MLO peer meta data needs to be
3060 	 * filled.
3061 	 * TODO: Need to handle for MLO scenario.
3062 	 */
3063 	reo_ent_ring->queue_addr_lo = reo_dest_ring->rx_mpdu_info.peer_meta_data;
3064 	reo_ent_ring->info0 = le32_encode_bits(dst_ind,
3065 					       HAL_REO_ENTR_RING_INFO0_DEST_IND);
3066 
3067 	reo_ent_ring->info1 = le32_encode_bits(rx_tid->cur_sn,
3068 					       HAL_REO_ENTR_RING_INFO1_MPDU_SEQ_NUM);
3069 	dest_ring_info0 = le32_get_bits(reo_dest_ring->info0,
3070 					HAL_REO_DEST_RING_INFO0_SRC_LINK_ID);
3071 	reo_ent_ring->info2 =
3072 		cpu_to_le32(u32_get_bits(dest_ring_info0,
3073 					 HAL_REO_ENTR_RING_INFO2_SRC_LINK_ID));
3074 
3075 	ath12k_hal_srng_access_end(ab, srng);
3076 	spin_unlock_bh(&srng->lock);
3077 
3078 	return 0;
3079 
3080 err_free_desc:
3081 	spin_lock_bh(&dp->rx_desc_lock);
3082 	list_del(&desc_info->list);
3083 	list_add_tail(&desc_info->list, &dp->rx_desc_free_list);
3084 	desc_info->skb = NULL;
3085 	spin_unlock_bh(&dp->rx_desc_lock);
3086 err_unmap_dma:
3087 	dma_unmap_single(ab->dev, buf_paddr, defrag_skb->len + skb_tailroom(defrag_skb),
3088 			 DMA_FROM_DEVICE);
3089 	return ret;
3090 }
3091 
3092 static int ath12k_dp_rx_h_cmp_frags(struct ath12k_base *ab,
3093 				    struct sk_buff *a, struct sk_buff *b)
3094 {
3095 	int frag1, frag2;
3096 
3097 	frag1 = ath12k_dp_rx_h_frag_no(ab, a);
3098 	frag2 = ath12k_dp_rx_h_frag_no(ab, b);
3099 
3100 	return frag1 - frag2;
3101 }
3102 
3103 static void ath12k_dp_rx_h_sort_frags(struct ath12k_base *ab,
3104 				      struct sk_buff_head *frag_list,
3105 				      struct sk_buff *cur_frag)
3106 {
3107 	struct sk_buff *skb;
3108 	int cmp;
3109 
3110 	skb_queue_walk(frag_list, skb) {
3111 		cmp = ath12k_dp_rx_h_cmp_frags(ab, skb, cur_frag);
3112 		if (cmp < 0)
3113 			continue;
3114 		__skb_queue_before(frag_list, skb, cur_frag);
3115 		return;
3116 	}
3117 	__skb_queue_tail(frag_list, cur_frag);
3118 }
3119 
3120 static u64 ath12k_dp_rx_h_get_pn(struct ath12k *ar, struct sk_buff *skb)
3121 {
3122 	struct ieee80211_hdr *hdr;
3123 	u64 pn = 0;
3124 	u8 *ehdr;
3125 	u32 hal_rx_desc_sz = ar->ab->hw_params->hal_desc_sz;
3126 
3127 	hdr = (struct ieee80211_hdr *)(skb->data + hal_rx_desc_sz);
3128 	ehdr = skb->data + hal_rx_desc_sz + ieee80211_hdrlen(hdr->frame_control);
3129 
3130 	pn = ehdr[0];
3131 	pn |= (u64)ehdr[1] << 8;
3132 	pn |= (u64)ehdr[4] << 16;
3133 	pn |= (u64)ehdr[5] << 24;
3134 	pn |= (u64)ehdr[6] << 32;
3135 	pn |= (u64)ehdr[7] << 40;
3136 
3137 	return pn;
3138 }
3139 
3140 static bool
3141 ath12k_dp_rx_h_defrag_validate_incr_pn(struct ath12k *ar, struct ath12k_dp_rx_tid *rx_tid)
3142 {
3143 	struct ath12k_base *ab = ar->ab;
3144 	enum hal_encrypt_type encrypt_type;
3145 	struct sk_buff *first_frag, *skb;
3146 	struct hal_rx_desc *desc;
3147 	u64 last_pn;
3148 	u64 cur_pn;
3149 
3150 	first_frag = skb_peek(&rx_tid->rx_frags);
3151 	desc = (struct hal_rx_desc *)first_frag->data;
3152 
3153 	encrypt_type = ath12k_dp_rx_h_enctype(ab, desc);
3154 	if (encrypt_type != HAL_ENCRYPT_TYPE_CCMP_128 &&
3155 	    encrypt_type != HAL_ENCRYPT_TYPE_CCMP_256 &&
3156 	    encrypt_type != HAL_ENCRYPT_TYPE_GCMP_128 &&
3157 	    encrypt_type != HAL_ENCRYPT_TYPE_AES_GCMP_256)
3158 		return true;
3159 
3160 	last_pn = ath12k_dp_rx_h_get_pn(ar, first_frag);
3161 	skb_queue_walk(&rx_tid->rx_frags, skb) {
3162 		if (skb == first_frag)
3163 			continue;
3164 
3165 		cur_pn = ath12k_dp_rx_h_get_pn(ar, skb);
3166 		if (cur_pn != last_pn + 1)
3167 			return false;
3168 		last_pn = cur_pn;
3169 	}
3170 	return true;
3171 }
3172 
3173 static int ath12k_dp_rx_frag_h_mpdu(struct ath12k *ar,
3174 				    struct sk_buff *msdu,
3175 				    struct hal_reo_dest_ring *ring_desc)
3176 {
3177 	struct ath12k_base *ab = ar->ab;
3178 	struct hal_rx_desc *rx_desc;
3179 	struct ath12k_peer *peer;
3180 	struct ath12k_dp_rx_tid *rx_tid;
3181 	struct sk_buff *defrag_skb = NULL;
3182 	u32 peer_id;
3183 	u16 seqno, frag_no;
3184 	u8 tid;
3185 	int ret = 0;
3186 	bool more_frags;
3187 
3188 	rx_desc = (struct hal_rx_desc *)msdu->data;
3189 	peer_id = ath12k_dp_rx_h_peer_id(ab, rx_desc);
3190 	tid = ath12k_dp_rx_h_tid(ab, rx_desc);
3191 	seqno = ath12k_dp_rx_h_seq_no(ab, rx_desc);
3192 	frag_no = ath12k_dp_rx_h_frag_no(ab, msdu);
3193 	more_frags = ath12k_dp_rx_h_more_frags(ab, msdu);
3194 
3195 	if (!ath12k_dp_rx_h_seq_ctrl_valid(ab, rx_desc) ||
3196 	    !ath12k_dp_rx_h_fc_valid(ab, rx_desc) ||
3197 	    tid > IEEE80211_NUM_TIDS)
3198 		return -EINVAL;
3199 
3200 	/* received unfragmented packet in reo
3201 	 * exception ring, this shouldn't happen
3202 	 * as these packets typically come from
3203 	 * reo2sw srngs.
3204 	 */
3205 	if (WARN_ON_ONCE(!frag_no && !more_frags))
3206 		return -EINVAL;
3207 
3208 	spin_lock_bh(&ab->base_lock);
3209 	peer = ath12k_peer_find_by_id(ab, peer_id);
3210 	if (!peer) {
3211 		ath12k_warn(ab, "failed to find the peer to de-fragment received fragment peer_id %d\n",
3212 			    peer_id);
3213 		ret = -ENOENT;
3214 		goto out_unlock;
3215 	}
3216 	rx_tid = &peer->rx_tid[tid];
3217 
3218 	if ((!skb_queue_empty(&rx_tid->rx_frags) && seqno != rx_tid->cur_sn) ||
3219 	    skb_queue_empty(&rx_tid->rx_frags)) {
3220 		/* Flush stored fragments and start a new sequence */
3221 		ath12k_dp_rx_frags_cleanup(rx_tid, true);
3222 		rx_tid->cur_sn = seqno;
3223 	}
3224 
3225 	if (rx_tid->rx_frag_bitmap & BIT(frag_no)) {
3226 		/* Fragment already present */
3227 		ret = -EINVAL;
3228 		goto out_unlock;
3229 	}
3230 
3231 	if (frag_no > __fls(rx_tid->rx_frag_bitmap))
3232 		__skb_queue_tail(&rx_tid->rx_frags, msdu);
3233 	else
3234 		ath12k_dp_rx_h_sort_frags(ab, &rx_tid->rx_frags, msdu);
3235 
3236 	rx_tid->rx_frag_bitmap |= BIT(frag_no);
3237 	if (!more_frags)
3238 		rx_tid->last_frag_no = frag_no;
3239 
3240 	if (frag_no == 0) {
3241 		rx_tid->dst_ring_desc = kmemdup(ring_desc,
3242 						sizeof(*rx_tid->dst_ring_desc),
3243 						GFP_ATOMIC);
3244 		if (!rx_tid->dst_ring_desc) {
3245 			ret = -ENOMEM;
3246 			goto out_unlock;
3247 		}
3248 	} else {
3249 		ath12k_dp_rx_link_desc_return(ab, ring_desc,
3250 					      HAL_WBM_REL_BM_ACT_PUT_IN_IDLE);
3251 	}
3252 
3253 	if (!rx_tid->last_frag_no ||
3254 	    rx_tid->rx_frag_bitmap != GENMASK(rx_tid->last_frag_no, 0)) {
3255 		mod_timer(&rx_tid->frag_timer, jiffies +
3256 					       ATH12K_DP_RX_FRAGMENT_TIMEOUT_MS);
3257 		goto out_unlock;
3258 	}
3259 
3260 	spin_unlock_bh(&ab->base_lock);
3261 	del_timer_sync(&rx_tid->frag_timer);
3262 	spin_lock_bh(&ab->base_lock);
3263 
3264 	peer = ath12k_peer_find_by_id(ab, peer_id);
3265 	if (!peer)
3266 		goto err_frags_cleanup;
3267 
3268 	if (!ath12k_dp_rx_h_defrag_validate_incr_pn(ar, rx_tid))
3269 		goto err_frags_cleanup;
3270 
3271 	if (ath12k_dp_rx_h_defrag(ar, peer, rx_tid, &defrag_skb))
3272 		goto err_frags_cleanup;
3273 
3274 	if (!defrag_skb)
3275 		goto err_frags_cleanup;
3276 
3277 	if (ath12k_dp_rx_h_defrag_reo_reinject(ar, rx_tid, defrag_skb))
3278 		goto err_frags_cleanup;
3279 
3280 	ath12k_dp_rx_frags_cleanup(rx_tid, false);
3281 	goto out_unlock;
3282 
3283 err_frags_cleanup:
3284 	dev_kfree_skb_any(defrag_skb);
3285 	ath12k_dp_rx_frags_cleanup(rx_tid, true);
3286 out_unlock:
3287 	spin_unlock_bh(&ab->base_lock);
3288 	return ret;
3289 }
3290 
3291 static int
3292 ath12k_dp_process_rx_err_buf(struct ath12k *ar, struct hal_reo_dest_ring *desc,
3293 			     bool drop, u32 cookie)
3294 {
3295 	struct ath12k_base *ab = ar->ab;
3296 	struct sk_buff *msdu;
3297 	struct ath12k_skb_rxcb *rxcb;
3298 	struct hal_rx_desc *rx_desc;
3299 	u16 msdu_len;
3300 	u32 hal_rx_desc_sz = ab->hw_params->hal_desc_sz;
3301 	struct ath12k_rx_desc_info *desc_info;
3302 	u64 desc_va;
3303 
3304 	desc_va = ((u64)le32_to_cpu(desc->buf_va_hi) << 32 |
3305 		   le32_to_cpu(desc->buf_va_lo));
3306 	desc_info = (struct ath12k_rx_desc_info *)((unsigned long)desc_va);
3307 
3308 	/* retry manual desc retrieval */
3309 	if (!desc_info) {
3310 		desc_info = ath12k_dp_get_rx_desc(ab, cookie);
3311 		if (!desc_info) {
3312 			ath12k_warn(ab, "Invalid cookie in manual desc retrieval");
3313 			return -EINVAL;
3314 		}
3315 	}
3316 
3317 	if (desc_info->magic != ATH12K_DP_RX_DESC_MAGIC)
3318 		ath12k_warn(ab, " RX Exception, Check HW CC implementation");
3319 
3320 	msdu = desc_info->skb;
3321 	desc_info->skb = NULL;
3322 	spin_lock_bh(&ab->dp.rx_desc_lock);
3323 	list_move_tail(&desc_info->list, &ab->dp.rx_desc_free_list);
3324 	spin_unlock_bh(&ab->dp.rx_desc_lock);
3325 
3326 	rxcb = ATH12K_SKB_RXCB(msdu);
3327 	dma_unmap_single(ar->ab->dev, rxcb->paddr,
3328 			 msdu->len + skb_tailroom(msdu),
3329 			 DMA_FROM_DEVICE);
3330 
3331 	if (drop) {
3332 		dev_kfree_skb_any(msdu);
3333 		return 0;
3334 	}
3335 
3336 	rcu_read_lock();
3337 	if (!rcu_dereference(ar->ab->pdevs_active[ar->pdev_idx])) {
3338 		dev_kfree_skb_any(msdu);
3339 		goto exit;
3340 	}
3341 
3342 	if (test_bit(ATH12K_CAC_RUNNING, &ar->dev_flags)) {
3343 		dev_kfree_skb_any(msdu);
3344 		goto exit;
3345 	}
3346 
3347 	rx_desc = (struct hal_rx_desc *)msdu->data;
3348 	msdu_len = ath12k_dp_rx_h_msdu_len(ar->ab, rx_desc);
3349 	if ((msdu_len + hal_rx_desc_sz) > DP_RX_BUFFER_SIZE) {
3350 		ath12k_warn(ar->ab, "invalid msdu leng %u", msdu_len);
3351 		ath12k_dbg_dump(ar->ab, ATH12K_DBG_DATA, NULL, "", rx_desc,
3352 				sizeof(*rx_desc));
3353 		dev_kfree_skb_any(msdu);
3354 		goto exit;
3355 	}
3356 
3357 	skb_put(msdu, hal_rx_desc_sz + msdu_len);
3358 
3359 	if (ath12k_dp_rx_frag_h_mpdu(ar, msdu, desc)) {
3360 		dev_kfree_skb_any(msdu);
3361 		ath12k_dp_rx_link_desc_return(ar->ab, desc,
3362 					      HAL_WBM_REL_BM_ACT_PUT_IN_IDLE);
3363 	}
3364 exit:
3365 	rcu_read_unlock();
3366 	return 0;
3367 }
3368 
3369 int ath12k_dp_rx_process_err(struct ath12k_base *ab, struct napi_struct *napi,
3370 			     int budget)
3371 {
3372 	u32 msdu_cookies[HAL_NUM_RX_MSDUS_PER_LINK_DESC];
3373 	struct dp_link_desc_bank *link_desc_banks;
3374 	enum hal_rx_buf_return_buf_manager rbm;
3375 	struct hal_rx_msdu_link *link_desc_va;
3376 	int tot_n_bufs_reaped, quota, ret, i;
3377 	struct hal_reo_dest_ring *reo_desc;
3378 	struct dp_rxdma_ring *rx_ring;
3379 	struct dp_srng *reo_except;
3380 	u32 desc_bank, num_msdus;
3381 	struct hal_srng *srng;
3382 	struct ath12k_dp *dp;
3383 	int mac_id;
3384 	struct ath12k *ar;
3385 	dma_addr_t paddr;
3386 	bool is_frag;
3387 	bool drop = false;
3388 
3389 	tot_n_bufs_reaped = 0;
3390 	quota = budget;
3391 
3392 	dp = &ab->dp;
3393 	reo_except = &dp->reo_except_ring;
3394 	link_desc_banks = dp->link_desc_banks;
3395 
3396 	srng = &ab->hal.srng_list[reo_except->ring_id];
3397 
3398 	spin_lock_bh(&srng->lock);
3399 
3400 	ath12k_hal_srng_access_begin(ab, srng);
3401 
3402 	while (budget &&
3403 	       (reo_desc = ath12k_hal_srng_dst_get_next_entry(ab, srng))) {
3404 		ab->soc_stats.err_ring_pkts++;
3405 		ret = ath12k_hal_desc_reo_parse_err(ab, reo_desc, &paddr,
3406 						    &desc_bank);
3407 		if (ret) {
3408 			ath12k_warn(ab, "failed to parse error reo desc %d\n",
3409 				    ret);
3410 			continue;
3411 		}
3412 		link_desc_va = link_desc_banks[desc_bank].vaddr +
3413 			       (paddr - link_desc_banks[desc_bank].paddr);
3414 		ath12k_hal_rx_msdu_link_info_get(link_desc_va, &num_msdus, msdu_cookies,
3415 						 &rbm);
3416 		if (rbm != HAL_RX_BUF_RBM_WBM_CHIP0_IDLE_DESC_LIST &&
3417 		    rbm != HAL_RX_BUF_RBM_SW3_BM &&
3418 		    rbm != ab->hw_params->hal_params->rx_buf_rbm) {
3419 			ab->soc_stats.invalid_rbm++;
3420 			ath12k_warn(ab, "invalid return buffer manager %d\n", rbm);
3421 			ath12k_dp_rx_link_desc_return(ab, reo_desc,
3422 						      HAL_WBM_REL_BM_ACT_REL_MSDU);
3423 			continue;
3424 		}
3425 
3426 		is_frag = !!(le32_to_cpu(reo_desc->rx_mpdu_info.info0) &
3427 			     RX_MPDU_DESC_INFO0_FRAG_FLAG);
3428 
3429 		/* Process only rx fragments with one msdu per link desc below, and drop
3430 		 * msdu's indicated due to error reasons.
3431 		 */
3432 		if (!is_frag || num_msdus > 1) {
3433 			drop = true;
3434 			/* Return the link desc back to wbm idle list */
3435 			ath12k_dp_rx_link_desc_return(ab, reo_desc,
3436 						      HAL_WBM_REL_BM_ACT_PUT_IN_IDLE);
3437 		}
3438 
3439 		for (i = 0; i < num_msdus; i++) {
3440 			mac_id = le32_get_bits(reo_desc->info0,
3441 					       HAL_REO_DEST_RING_INFO0_SRC_LINK_ID);
3442 
3443 			ar = ab->pdevs[mac_id].ar;
3444 
3445 			if (!ath12k_dp_process_rx_err_buf(ar, reo_desc, drop,
3446 							  msdu_cookies[i]))
3447 				tot_n_bufs_reaped++;
3448 		}
3449 
3450 		if (tot_n_bufs_reaped >= quota) {
3451 			tot_n_bufs_reaped = quota;
3452 			goto exit;
3453 		}
3454 
3455 		budget = quota - tot_n_bufs_reaped;
3456 	}
3457 
3458 exit:
3459 	ath12k_hal_srng_access_end(ab, srng);
3460 
3461 	spin_unlock_bh(&srng->lock);
3462 
3463 	rx_ring = &dp->rx_refill_buf_ring;
3464 
3465 	ath12k_dp_rx_bufs_replenish(ab, 0, rx_ring, tot_n_bufs_reaped,
3466 				    ab->hw_params->hal_params->rx_buf_rbm, true);
3467 
3468 	return tot_n_bufs_reaped;
3469 }
3470 
3471 static void ath12k_dp_rx_null_q_desc_sg_drop(struct ath12k *ar,
3472 					     int msdu_len,
3473 					     struct sk_buff_head *msdu_list)
3474 {
3475 	struct sk_buff *skb, *tmp;
3476 	struct ath12k_skb_rxcb *rxcb;
3477 	int n_buffs;
3478 
3479 	n_buffs = DIV_ROUND_UP(msdu_len,
3480 			       (DP_RX_BUFFER_SIZE - ar->ab->hw_params->hal_desc_sz));
3481 
3482 	skb_queue_walk_safe(msdu_list, skb, tmp) {
3483 		rxcb = ATH12K_SKB_RXCB(skb);
3484 		if (rxcb->err_rel_src == HAL_WBM_REL_SRC_MODULE_REO &&
3485 		    rxcb->err_code == HAL_REO_DEST_RING_ERROR_CODE_DESC_ADDR_ZERO) {
3486 			if (!n_buffs)
3487 				break;
3488 			__skb_unlink(skb, msdu_list);
3489 			dev_kfree_skb_any(skb);
3490 			n_buffs--;
3491 		}
3492 	}
3493 }
3494 
3495 static int ath12k_dp_rx_h_null_q_desc(struct ath12k *ar, struct sk_buff *msdu,
3496 				      struct ieee80211_rx_status *status,
3497 				      struct sk_buff_head *msdu_list)
3498 {
3499 	struct ath12k_base *ab = ar->ab;
3500 	u16 msdu_len, peer_id;
3501 	struct hal_rx_desc *desc = (struct hal_rx_desc *)msdu->data;
3502 	u8 l3pad_bytes;
3503 	struct ath12k_skb_rxcb *rxcb = ATH12K_SKB_RXCB(msdu);
3504 	u32 hal_rx_desc_sz = ar->ab->hw_params->hal_desc_sz;
3505 
3506 	msdu_len = ath12k_dp_rx_h_msdu_len(ab, desc);
3507 	peer_id = ath12k_dp_rx_h_peer_id(ab, desc);
3508 
3509 	spin_lock(&ab->base_lock);
3510 	if (!ath12k_peer_find_by_id(ab, peer_id)) {
3511 		spin_unlock(&ab->base_lock);
3512 		ath12k_dbg(ab, ATH12K_DBG_DATA, "invalid peer id received in wbm err pkt%d\n",
3513 			   peer_id);
3514 		return -EINVAL;
3515 	}
3516 	spin_unlock(&ab->base_lock);
3517 
3518 	if (!rxcb->is_frag && ((msdu_len + hal_rx_desc_sz) > DP_RX_BUFFER_SIZE)) {
3519 		/* First buffer will be freed by the caller, so deduct it's length */
3520 		msdu_len = msdu_len - (DP_RX_BUFFER_SIZE - hal_rx_desc_sz);
3521 		ath12k_dp_rx_null_q_desc_sg_drop(ar, msdu_len, msdu_list);
3522 		return -EINVAL;
3523 	}
3524 
3525 	/* Even after cleaning up the sg buffers in the msdu list with above check
3526 	 * any msdu received with continuation flag needs to be dropped as invalid.
3527 	 * This protects against some random err frame with continuation flag.
3528 	 */
3529 	if (rxcb->is_continuation)
3530 		return -EINVAL;
3531 
3532 	if (!ath12k_dp_rx_h_msdu_done(ab, desc)) {
3533 		ath12k_warn(ar->ab,
3534 			    "msdu_done bit not set in null_q_des processing\n");
3535 		__skb_queue_purge(msdu_list);
3536 		return -EIO;
3537 	}
3538 
3539 	/* Handle NULL queue descriptor violations arising out a missing
3540 	 * REO queue for a given peer or a given TID. This typically
3541 	 * may happen if a packet is received on a QOS enabled TID before the
3542 	 * ADDBA negotiation for that TID, when the TID queue is setup. Or
3543 	 * it may also happen for MC/BC frames if they are not routed to the
3544 	 * non-QOS TID queue, in the absence of any other default TID queue.
3545 	 * This error can show up both in a REO destination or WBM release ring.
3546 	 */
3547 
3548 	if (rxcb->is_frag) {
3549 		skb_pull(msdu, hal_rx_desc_sz);
3550 	} else {
3551 		l3pad_bytes = ath12k_dp_rx_h_l3pad(ab, desc);
3552 
3553 		if ((hal_rx_desc_sz + l3pad_bytes + msdu_len) > DP_RX_BUFFER_SIZE)
3554 			return -EINVAL;
3555 
3556 		skb_put(msdu, hal_rx_desc_sz + l3pad_bytes + msdu_len);
3557 		skb_pull(msdu, hal_rx_desc_sz + l3pad_bytes);
3558 	}
3559 	ath12k_dp_rx_h_ppdu(ar, desc, status);
3560 
3561 	ath12k_dp_rx_h_mpdu(ar, msdu, desc, status);
3562 
3563 	rxcb->tid = ath12k_dp_rx_h_tid(ab, desc);
3564 
3565 	/* Please note that caller will having the access to msdu and completing
3566 	 * rx with mac80211. Need not worry about cleaning up amsdu_list.
3567 	 */
3568 
3569 	return 0;
3570 }
3571 
3572 static bool ath12k_dp_rx_h_reo_err(struct ath12k *ar, struct sk_buff *msdu,
3573 				   struct ieee80211_rx_status *status,
3574 				   struct sk_buff_head *msdu_list)
3575 {
3576 	struct ath12k_skb_rxcb *rxcb = ATH12K_SKB_RXCB(msdu);
3577 	bool drop = false;
3578 
3579 	ar->ab->soc_stats.reo_error[rxcb->err_code]++;
3580 
3581 	switch (rxcb->err_code) {
3582 	case HAL_REO_DEST_RING_ERROR_CODE_DESC_ADDR_ZERO:
3583 		if (ath12k_dp_rx_h_null_q_desc(ar, msdu, status, msdu_list))
3584 			drop = true;
3585 		break;
3586 	case HAL_REO_DEST_RING_ERROR_CODE_PN_CHECK_FAILED:
3587 		/* TODO: Do not drop PN failed packets in the driver;
3588 		 * instead, it is good to drop such packets in mac80211
3589 		 * after incrementing the replay counters.
3590 		 */
3591 		fallthrough;
3592 	default:
3593 		/* TODO: Review other errors and process them to mac80211
3594 		 * as appropriate.
3595 		 */
3596 		drop = true;
3597 		break;
3598 	}
3599 
3600 	return drop;
3601 }
3602 
3603 static void ath12k_dp_rx_h_tkip_mic_err(struct ath12k *ar, struct sk_buff *msdu,
3604 					struct ieee80211_rx_status *status)
3605 {
3606 	struct ath12k_base *ab = ar->ab;
3607 	u16 msdu_len;
3608 	struct hal_rx_desc *desc = (struct hal_rx_desc *)msdu->data;
3609 	u8 l3pad_bytes;
3610 	struct ath12k_skb_rxcb *rxcb = ATH12K_SKB_RXCB(msdu);
3611 	u32 hal_rx_desc_sz = ar->ab->hw_params->hal_desc_sz;
3612 
3613 	rxcb->is_first_msdu = ath12k_dp_rx_h_first_msdu(ab, desc);
3614 	rxcb->is_last_msdu = ath12k_dp_rx_h_last_msdu(ab, desc);
3615 
3616 	l3pad_bytes = ath12k_dp_rx_h_l3pad(ab, desc);
3617 	msdu_len = ath12k_dp_rx_h_msdu_len(ab, desc);
3618 	skb_put(msdu, hal_rx_desc_sz + l3pad_bytes + msdu_len);
3619 	skb_pull(msdu, hal_rx_desc_sz + l3pad_bytes);
3620 
3621 	ath12k_dp_rx_h_ppdu(ar, desc, status);
3622 
3623 	status->flag |= (RX_FLAG_MMIC_STRIPPED | RX_FLAG_MMIC_ERROR |
3624 			 RX_FLAG_DECRYPTED);
3625 
3626 	ath12k_dp_rx_h_undecap(ar, msdu, desc,
3627 			       HAL_ENCRYPT_TYPE_TKIP_MIC, status, false);
3628 }
3629 
3630 static bool ath12k_dp_rx_h_rxdma_err(struct ath12k *ar,  struct sk_buff *msdu,
3631 				     struct ieee80211_rx_status *status)
3632 {
3633 	struct ath12k_base *ab = ar->ab;
3634 	struct ath12k_skb_rxcb *rxcb = ATH12K_SKB_RXCB(msdu);
3635 	struct hal_rx_desc *rx_desc = (struct hal_rx_desc *)msdu->data;
3636 	bool drop = false;
3637 	u32 err_bitmap;
3638 
3639 	ar->ab->soc_stats.rxdma_error[rxcb->err_code]++;
3640 
3641 	switch (rxcb->err_code) {
3642 	case HAL_REO_ENTR_RING_RXDMA_ECODE_DECRYPT_ERR:
3643 	case HAL_REO_ENTR_RING_RXDMA_ECODE_TKIP_MIC_ERR:
3644 		err_bitmap = ath12k_dp_rx_h_mpdu_err(ab, rx_desc);
3645 		if (err_bitmap & HAL_RX_MPDU_ERR_TKIP_MIC) {
3646 			ath12k_dp_rx_h_tkip_mic_err(ar, msdu, status);
3647 			break;
3648 		}
3649 		fallthrough;
3650 	default:
3651 		/* TODO: Review other rxdma error code to check if anything is
3652 		 * worth reporting to mac80211
3653 		 */
3654 		drop = true;
3655 		break;
3656 	}
3657 
3658 	return drop;
3659 }
3660 
3661 static void ath12k_dp_rx_wbm_err(struct ath12k *ar,
3662 				 struct napi_struct *napi,
3663 				 struct sk_buff *msdu,
3664 				 struct sk_buff_head *msdu_list)
3665 {
3666 	struct ath12k_skb_rxcb *rxcb = ATH12K_SKB_RXCB(msdu);
3667 	struct ieee80211_rx_status rxs = {0};
3668 	bool drop = true;
3669 
3670 	switch (rxcb->err_rel_src) {
3671 	case HAL_WBM_REL_SRC_MODULE_REO:
3672 		drop = ath12k_dp_rx_h_reo_err(ar, msdu, &rxs, msdu_list);
3673 		break;
3674 	case HAL_WBM_REL_SRC_MODULE_RXDMA:
3675 		drop = ath12k_dp_rx_h_rxdma_err(ar, msdu, &rxs);
3676 		break;
3677 	default:
3678 		/* msdu will get freed */
3679 		break;
3680 	}
3681 
3682 	if (drop) {
3683 		dev_kfree_skb_any(msdu);
3684 		return;
3685 	}
3686 
3687 	ath12k_dp_rx_deliver_msdu(ar, napi, msdu, &rxs);
3688 }
3689 
3690 int ath12k_dp_rx_process_wbm_err(struct ath12k_base *ab,
3691 				 struct napi_struct *napi, int budget)
3692 {
3693 	struct ath12k *ar;
3694 	struct ath12k_dp *dp = &ab->dp;
3695 	struct dp_rxdma_ring *rx_ring;
3696 	struct hal_rx_wbm_rel_info err_info;
3697 	struct hal_srng *srng;
3698 	struct sk_buff *msdu;
3699 	struct sk_buff_head msdu_list[MAX_RADIOS];
3700 	struct ath12k_skb_rxcb *rxcb;
3701 	void *rx_desc;
3702 	int mac_id;
3703 	int num_buffs_reaped = 0;
3704 	struct ath12k_rx_desc_info *desc_info;
3705 	int ret, i;
3706 
3707 	for (i = 0; i < ab->num_radios; i++)
3708 		__skb_queue_head_init(&msdu_list[i]);
3709 
3710 	srng = &ab->hal.srng_list[dp->rx_rel_ring.ring_id];
3711 	rx_ring = &dp->rx_refill_buf_ring;
3712 
3713 	spin_lock_bh(&srng->lock);
3714 
3715 	ath12k_hal_srng_access_begin(ab, srng);
3716 
3717 	while (budget) {
3718 		rx_desc = ath12k_hal_srng_dst_get_next_entry(ab, srng);
3719 		if (!rx_desc)
3720 			break;
3721 
3722 		ret = ath12k_hal_wbm_desc_parse_err(ab, rx_desc, &err_info);
3723 		if (ret) {
3724 			ath12k_warn(ab,
3725 				    "failed to parse rx error in wbm_rel ring desc %d\n",
3726 				    ret);
3727 			continue;
3728 		}
3729 
3730 		desc_info = (struct ath12k_rx_desc_info *)err_info.rx_desc;
3731 
3732 		/* retry manual desc retrieval if hw cc is not done */
3733 		if (!desc_info) {
3734 			desc_info = ath12k_dp_get_rx_desc(ab, err_info.cookie);
3735 			if (!desc_info) {
3736 				ath12k_warn(ab, "Invalid cookie in manual desc retrieval");
3737 				continue;
3738 			}
3739 		}
3740 
3741 		/* FIXME: Extract mac id correctly. Since descs are not tied
3742 		 * to mac, we can extract from vdev id in ring desc.
3743 		 */
3744 		mac_id = 0;
3745 
3746 		if (desc_info->magic != ATH12K_DP_RX_DESC_MAGIC)
3747 			ath12k_warn(ab, "WBM RX err, Check HW CC implementation");
3748 
3749 		msdu = desc_info->skb;
3750 		desc_info->skb = NULL;
3751 
3752 		spin_lock_bh(&dp->rx_desc_lock);
3753 		list_move_tail(&desc_info->list, &dp->rx_desc_free_list);
3754 		spin_unlock_bh(&dp->rx_desc_lock);
3755 
3756 		rxcb = ATH12K_SKB_RXCB(msdu);
3757 		dma_unmap_single(ab->dev, rxcb->paddr,
3758 				 msdu->len + skb_tailroom(msdu),
3759 				 DMA_FROM_DEVICE);
3760 
3761 		num_buffs_reaped++;
3762 
3763 		if (!err_info.continuation)
3764 			budget--;
3765 
3766 		if (err_info.push_reason !=
3767 		    HAL_REO_DEST_RING_PUSH_REASON_ERR_DETECTED) {
3768 			dev_kfree_skb_any(msdu);
3769 			continue;
3770 		}
3771 
3772 		rxcb->err_rel_src = err_info.err_rel_src;
3773 		rxcb->err_code = err_info.err_code;
3774 		rxcb->rx_desc = (struct hal_rx_desc *)msdu->data;
3775 		__skb_queue_tail(&msdu_list[mac_id], msdu);
3776 
3777 		rxcb->is_first_msdu = err_info.first_msdu;
3778 		rxcb->is_last_msdu = err_info.last_msdu;
3779 		rxcb->is_continuation = err_info.continuation;
3780 	}
3781 
3782 	ath12k_hal_srng_access_end(ab, srng);
3783 
3784 	spin_unlock_bh(&srng->lock);
3785 
3786 	if (!num_buffs_reaped)
3787 		goto done;
3788 
3789 	ath12k_dp_rx_bufs_replenish(ab, 0, rx_ring, num_buffs_reaped,
3790 				    ab->hw_params->hal_params->rx_buf_rbm, true);
3791 
3792 	rcu_read_lock();
3793 	for (i = 0; i <  ab->num_radios; i++) {
3794 		if (!rcu_dereference(ab->pdevs_active[i])) {
3795 			__skb_queue_purge(&msdu_list[i]);
3796 			continue;
3797 		}
3798 
3799 		ar = ab->pdevs[i].ar;
3800 
3801 		if (test_bit(ATH12K_CAC_RUNNING, &ar->dev_flags)) {
3802 			__skb_queue_purge(&msdu_list[i]);
3803 			continue;
3804 		}
3805 
3806 		while ((msdu = __skb_dequeue(&msdu_list[i])) != NULL)
3807 			ath12k_dp_rx_wbm_err(ar, napi, msdu, &msdu_list[i]);
3808 	}
3809 	rcu_read_unlock();
3810 done:
3811 	return num_buffs_reaped;
3812 }
3813 
3814 void ath12k_dp_rx_process_reo_status(struct ath12k_base *ab)
3815 {
3816 	struct ath12k_dp *dp = &ab->dp;
3817 	struct hal_tlv_64_hdr *hdr;
3818 	struct hal_srng *srng;
3819 	struct ath12k_dp_rx_reo_cmd *cmd, *tmp;
3820 	bool found = false;
3821 	u16 tag;
3822 	struct hal_reo_status reo_status;
3823 
3824 	srng = &ab->hal.srng_list[dp->reo_status_ring.ring_id];
3825 
3826 	memset(&reo_status, 0, sizeof(reo_status));
3827 
3828 	spin_lock_bh(&srng->lock);
3829 
3830 	ath12k_hal_srng_access_begin(ab, srng);
3831 
3832 	while ((hdr = ath12k_hal_srng_dst_get_next_entry(ab, srng))) {
3833 		tag = u64_get_bits(hdr->tl, HAL_SRNG_TLV_HDR_TAG);
3834 
3835 		switch (tag) {
3836 		case HAL_REO_GET_QUEUE_STATS_STATUS:
3837 			ath12k_hal_reo_status_queue_stats(ab, hdr,
3838 							  &reo_status);
3839 			break;
3840 		case HAL_REO_FLUSH_QUEUE_STATUS:
3841 			ath12k_hal_reo_flush_queue_status(ab, hdr,
3842 							  &reo_status);
3843 			break;
3844 		case HAL_REO_FLUSH_CACHE_STATUS:
3845 			ath12k_hal_reo_flush_cache_status(ab, hdr,
3846 							  &reo_status);
3847 			break;
3848 		case HAL_REO_UNBLOCK_CACHE_STATUS:
3849 			ath12k_hal_reo_unblk_cache_status(ab, hdr,
3850 							  &reo_status);
3851 			break;
3852 		case HAL_REO_FLUSH_TIMEOUT_LIST_STATUS:
3853 			ath12k_hal_reo_flush_timeout_list_status(ab, hdr,
3854 								 &reo_status);
3855 			break;
3856 		case HAL_REO_DESCRIPTOR_THRESHOLD_REACHED_STATUS:
3857 			ath12k_hal_reo_desc_thresh_reached_status(ab, hdr,
3858 								  &reo_status);
3859 			break;
3860 		case HAL_REO_UPDATE_RX_REO_QUEUE_STATUS:
3861 			ath12k_hal_reo_update_rx_reo_queue_status(ab, hdr,
3862 								  &reo_status);
3863 			break;
3864 		default:
3865 			ath12k_warn(ab, "Unknown reo status type %d\n", tag);
3866 			continue;
3867 		}
3868 
3869 		spin_lock_bh(&dp->reo_cmd_lock);
3870 		list_for_each_entry_safe(cmd, tmp, &dp->reo_cmd_list, list) {
3871 			if (reo_status.uniform_hdr.cmd_num == cmd->cmd_num) {
3872 				found = true;
3873 				list_del(&cmd->list);
3874 				break;
3875 			}
3876 		}
3877 		spin_unlock_bh(&dp->reo_cmd_lock);
3878 
3879 		if (found) {
3880 			cmd->handler(dp, (void *)&cmd->data,
3881 				     reo_status.uniform_hdr.cmd_status);
3882 			kfree(cmd);
3883 		}
3884 
3885 		found = false;
3886 	}
3887 
3888 	ath12k_hal_srng_access_end(ab, srng);
3889 
3890 	spin_unlock_bh(&srng->lock);
3891 }
3892 
3893 void ath12k_dp_rx_free(struct ath12k_base *ab)
3894 {
3895 	struct ath12k_dp *dp = &ab->dp;
3896 	int i;
3897 
3898 	ath12k_dp_srng_cleanup(ab, &dp->rx_refill_buf_ring.refill_buf_ring);
3899 
3900 	for (i = 0; i < ab->hw_params->num_rxmda_per_pdev; i++) {
3901 		if (ab->hw_params->rx_mac_buf_ring)
3902 			ath12k_dp_srng_cleanup(ab, &dp->rx_mac_buf_ring[i]);
3903 	}
3904 
3905 	for (i = 0; i < ab->hw_params->num_rxdma_dst_ring; i++)
3906 		ath12k_dp_srng_cleanup(ab, &dp->rxdma_err_dst_ring[i]);
3907 
3908 	ath12k_dp_srng_cleanup(ab, &dp->rxdma_mon_buf_ring.refill_buf_ring);
3909 	ath12k_dp_srng_cleanup(ab, &dp->tx_mon_buf_ring.refill_buf_ring);
3910 
3911 	ath12k_dp_rxdma_buf_free(ab);
3912 }
3913 
3914 void ath12k_dp_rx_pdev_free(struct ath12k_base *ab, int mac_id)
3915 {
3916 	struct ath12k *ar = ab->pdevs[mac_id].ar;
3917 
3918 	ath12k_dp_rx_pdev_srng_free(ar);
3919 }
3920 
3921 int ath12k_dp_rxdma_ring_sel_config_qcn9274(struct ath12k_base *ab)
3922 {
3923 	struct ath12k_dp *dp = &ab->dp;
3924 	struct htt_rx_ring_tlv_filter tlv_filter = {0};
3925 	u32 ring_id;
3926 	int ret;
3927 	u32 hal_rx_desc_sz = ab->hw_params->hal_desc_sz;
3928 
3929 	ring_id = dp->rx_refill_buf_ring.refill_buf_ring.ring_id;
3930 
3931 	tlv_filter.rx_filter = HTT_RX_TLV_FLAGS_RXDMA_RING;
3932 	tlv_filter.pkt_filter_flags2 = HTT_RX_FP_CTRL_PKT_FILTER_TLV_FLAGS2_BAR;
3933 	tlv_filter.pkt_filter_flags3 = HTT_RX_FP_DATA_PKT_FILTER_TLV_FLASG3_MCAST |
3934 					HTT_RX_FP_DATA_PKT_FILTER_TLV_FLASG3_UCAST |
3935 					HTT_RX_FP_DATA_PKT_FILTER_TLV_FLASG3_NULL_DATA;
3936 	tlv_filter.offset_valid = true;
3937 	tlv_filter.rx_packet_offset = hal_rx_desc_sz;
3938 
3939 	tlv_filter.rx_mpdu_start_offset =
3940 			ab->hw_params->hal_ops->rx_desc_get_mpdu_start_offset();
3941 	tlv_filter.rx_msdu_end_offset =
3942 		ab->hw_params->hal_ops->rx_desc_get_msdu_end_offset();
3943 
3944 	/* TODO: Selectively subscribe to required qwords within msdu_end
3945 	 * and mpdu_start and setup the mask in below msg
3946 	 * and modify the rx_desc struct
3947 	 */
3948 	ret = ath12k_dp_tx_htt_rx_filter_setup(ab, ring_id, 0,
3949 					       HAL_RXDMA_BUF,
3950 					       DP_RXDMA_REFILL_RING_SIZE,
3951 					       &tlv_filter);
3952 
3953 	return ret;
3954 }
3955 
3956 int ath12k_dp_rxdma_ring_sel_config_wcn7850(struct ath12k_base *ab)
3957 {
3958 	struct ath12k_dp *dp = &ab->dp;
3959 	struct htt_rx_ring_tlv_filter tlv_filter = {0};
3960 	u32 ring_id;
3961 	int ret;
3962 	u32 hal_rx_desc_sz = ab->hw_params->hal_desc_sz;
3963 	int i;
3964 
3965 	ring_id = dp->rx_refill_buf_ring.refill_buf_ring.ring_id;
3966 
3967 	tlv_filter.rx_filter = HTT_RX_TLV_FLAGS_RXDMA_RING;
3968 	tlv_filter.pkt_filter_flags2 = HTT_RX_FP_CTRL_PKT_FILTER_TLV_FLAGS2_BAR;
3969 	tlv_filter.pkt_filter_flags3 = HTT_RX_FP_DATA_PKT_FILTER_TLV_FLASG3_MCAST |
3970 					HTT_RX_FP_DATA_PKT_FILTER_TLV_FLASG3_UCAST |
3971 					HTT_RX_FP_DATA_PKT_FILTER_TLV_FLASG3_NULL_DATA;
3972 	tlv_filter.offset_valid = true;
3973 	tlv_filter.rx_packet_offset = hal_rx_desc_sz;
3974 
3975 	tlv_filter.rx_header_offset = offsetof(struct hal_rx_desc_wcn7850, pkt_hdr_tlv);
3976 
3977 	tlv_filter.rx_mpdu_start_offset =
3978 			ab->hw_params->hal_ops->rx_desc_get_mpdu_start_offset();
3979 	tlv_filter.rx_msdu_end_offset =
3980 		ab->hw_params->hal_ops->rx_desc_get_msdu_end_offset();
3981 
3982 	/* TODO: Selectively subscribe to required qwords within msdu_end
3983 	 * and mpdu_start and setup the mask in below msg
3984 	 * and modify the rx_desc struct
3985 	 */
3986 
3987 	for (i = 0; i < ab->hw_params->num_rxmda_per_pdev; i++) {
3988 		ring_id = dp->rx_mac_buf_ring[i].ring_id;
3989 		ret = ath12k_dp_tx_htt_rx_filter_setup(ab, ring_id, i,
3990 						       HAL_RXDMA_BUF,
3991 						       DP_RXDMA_REFILL_RING_SIZE,
3992 						       &tlv_filter);
3993 	}
3994 
3995 	return ret;
3996 }
3997 
3998 int ath12k_dp_rx_htt_setup(struct ath12k_base *ab)
3999 {
4000 	struct ath12k_dp *dp = &ab->dp;
4001 	u32 ring_id;
4002 	int i, ret;
4003 
4004 	/* TODO: Need to verify the HTT setup for QCN9224 */
4005 	ring_id = dp->rx_refill_buf_ring.refill_buf_ring.ring_id;
4006 	ret = ath12k_dp_tx_htt_srng_setup(ab, ring_id, 0, HAL_RXDMA_BUF);
4007 	if (ret) {
4008 		ath12k_warn(ab, "failed to configure rx_refill_buf_ring %d\n",
4009 			    ret);
4010 		return ret;
4011 	}
4012 
4013 	if (ab->hw_params->rx_mac_buf_ring) {
4014 		for (i = 0; i < ab->hw_params->num_rxmda_per_pdev; i++) {
4015 			ring_id = dp->rx_mac_buf_ring[i].ring_id;
4016 			ret = ath12k_dp_tx_htt_srng_setup(ab, ring_id,
4017 							  i, HAL_RXDMA_BUF);
4018 			if (ret) {
4019 				ath12k_warn(ab, "failed to configure rx_mac_buf_ring%d %d\n",
4020 					    i, ret);
4021 				return ret;
4022 			}
4023 		}
4024 	}
4025 
4026 	for (i = 0; i < ab->hw_params->num_rxdma_dst_ring; i++) {
4027 		ring_id = dp->rxdma_err_dst_ring[i].ring_id;
4028 		ret = ath12k_dp_tx_htt_srng_setup(ab, ring_id,
4029 						  i, HAL_RXDMA_DST);
4030 		if (ret) {
4031 			ath12k_warn(ab, "failed to configure rxdma_err_dest_ring%d %d\n",
4032 				    i, ret);
4033 			return ret;
4034 		}
4035 	}
4036 
4037 	if (ab->hw_params->rxdma1_enable) {
4038 		ring_id = dp->rxdma_mon_buf_ring.refill_buf_ring.ring_id;
4039 		ret = ath12k_dp_tx_htt_srng_setup(ab, ring_id,
4040 						  0, HAL_RXDMA_MONITOR_BUF);
4041 		if (ret) {
4042 			ath12k_warn(ab, "failed to configure rxdma_mon_buf_ring %d\n",
4043 				    ret);
4044 			return ret;
4045 		}
4046 
4047 		ring_id = dp->tx_mon_buf_ring.refill_buf_ring.ring_id;
4048 		ret = ath12k_dp_tx_htt_srng_setup(ab, ring_id,
4049 						  0, HAL_TX_MONITOR_BUF);
4050 		if (ret) {
4051 			ath12k_warn(ab, "failed to configure rxdma_mon_buf_ring %d\n",
4052 				    ret);
4053 			return ret;
4054 		}
4055 	}
4056 
4057 	ret = ab->hw_params->hw_ops->rxdma_ring_sel_config(ab);
4058 	if (ret) {
4059 		ath12k_warn(ab, "failed to setup rxdma ring selection config\n");
4060 		return ret;
4061 	}
4062 
4063 	return 0;
4064 }
4065 
4066 int ath12k_dp_rx_alloc(struct ath12k_base *ab)
4067 {
4068 	struct ath12k_dp *dp = &ab->dp;
4069 	int i, ret;
4070 
4071 	idr_init(&dp->rx_refill_buf_ring.bufs_idr);
4072 	spin_lock_init(&dp->rx_refill_buf_ring.idr_lock);
4073 
4074 	idr_init(&dp->rxdma_mon_buf_ring.bufs_idr);
4075 	spin_lock_init(&dp->rxdma_mon_buf_ring.idr_lock);
4076 
4077 	idr_init(&dp->tx_mon_buf_ring.bufs_idr);
4078 	spin_lock_init(&dp->tx_mon_buf_ring.idr_lock);
4079 
4080 	ret = ath12k_dp_srng_setup(ab,
4081 				   &dp->rx_refill_buf_ring.refill_buf_ring,
4082 				   HAL_RXDMA_BUF, 0, 0,
4083 				   DP_RXDMA_BUF_RING_SIZE);
4084 	if (ret) {
4085 		ath12k_warn(ab, "failed to setup rx_refill_buf_ring\n");
4086 		return ret;
4087 	}
4088 
4089 	if (ab->hw_params->rx_mac_buf_ring) {
4090 		for (i = 0; i < ab->hw_params->num_rxmda_per_pdev; i++) {
4091 			ret = ath12k_dp_srng_setup(ab,
4092 						   &dp->rx_mac_buf_ring[i],
4093 						   HAL_RXDMA_BUF, 1,
4094 						   i, 1024);
4095 			if (ret) {
4096 				ath12k_warn(ab, "failed to setup rx_mac_buf_ring %d\n",
4097 					    i);
4098 				return ret;
4099 			}
4100 		}
4101 	}
4102 
4103 	for (i = 0; i < ab->hw_params->num_rxdma_dst_ring; i++) {
4104 		ret = ath12k_dp_srng_setup(ab, &dp->rxdma_err_dst_ring[i],
4105 					   HAL_RXDMA_DST, 0, i,
4106 					   DP_RXDMA_ERR_DST_RING_SIZE);
4107 		if (ret) {
4108 			ath12k_warn(ab, "failed to setup rxdma_err_dst_ring %d\n", i);
4109 			return ret;
4110 		}
4111 	}
4112 
4113 	if (ab->hw_params->rxdma1_enable) {
4114 		ret = ath12k_dp_srng_setup(ab,
4115 					   &dp->rxdma_mon_buf_ring.refill_buf_ring,
4116 					   HAL_RXDMA_MONITOR_BUF, 0, 0,
4117 					   DP_RXDMA_MONITOR_BUF_RING_SIZE);
4118 		if (ret) {
4119 			ath12k_warn(ab, "failed to setup HAL_RXDMA_MONITOR_BUF\n");
4120 			return ret;
4121 		}
4122 
4123 		ret = ath12k_dp_srng_setup(ab,
4124 					   &dp->tx_mon_buf_ring.refill_buf_ring,
4125 					   HAL_TX_MONITOR_BUF, 0, 0,
4126 					   DP_TX_MONITOR_BUF_RING_SIZE);
4127 		if (ret) {
4128 			ath12k_warn(ab, "failed to setup DP_TX_MONITOR_BUF_RING_SIZE\n");
4129 			return ret;
4130 		}
4131 	}
4132 
4133 	ret = ath12k_dp_rxdma_buf_setup(ab);
4134 	if (ret) {
4135 		ath12k_warn(ab, "failed to setup rxdma ring\n");
4136 		return ret;
4137 	}
4138 
4139 	return 0;
4140 }
4141 
4142 int ath12k_dp_rx_pdev_alloc(struct ath12k_base *ab, int mac_id)
4143 {
4144 	struct ath12k *ar = ab->pdevs[mac_id].ar;
4145 	struct ath12k_pdev_dp *dp = &ar->dp;
4146 	u32 ring_id;
4147 	int i;
4148 	int ret;
4149 
4150 	if (!ab->hw_params->rxdma1_enable)
4151 		goto out;
4152 
4153 	ret = ath12k_dp_rx_pdev_srng_alloc(ar);
4154 	if (ret) {
4155 		ath12k_warn(ab, "failed to setup rx srngs\n");
4156 		return ret;
4157 	}
4158 
4159 	for (i = 0; i < ab->hw_params->num_rxmda_per_pdev; i++) {
4160 		ring_id = dp->rxdma_mon_dst_ring[i].ring_id;
4161 		ret = ath12k_dp_tx_htt_srng_setup(ab, ring_id,
4162 						  mac_id + i,
4163 						  HAL_RXDMA_MONITOR_DST);
4164 		if (ret) {
4165 			ath12k_warn(ab,
4166 				    "failed to configure rxdma_mon_dst_ring %d %d\n",
4167 				    i, ret);
4168 			return ret;
4169 		}
4170 
4171 		ring_id = dp->tx_mon_dst_ring[i].ring_id;
4172 		ret = ath12k_dp_tx_htt_srng_setup(ab, ring_id,
4173 						  mac_id + i,
4174 						  HAL_TX_MONITOR_DST);
4175 		if (ret) {
4176 			ath12k_warn(ab,
4177 				    "failed to configure tx_mon_dst_ring %d %d\n",
4178 				    i, ret);
4179 			return ret;
4180 		}
4181 	}
4182 out:
4183 	return 0;
4184 }
4185 
4186 static int ath12k_dp_rx_pdev_mon_status_attach(struct ath12k *ar)
4187 {
4188 	struct ath12k_pdev_dp *dp = &ar->dp;
4189 	struct ath12k_mon_data *pmon = (struct ath12k_mon_data *)&dp->mon_data;
4190 
4191 	skb_queue_head_init(&pmon->rx_status_q);
4192 
4193 	pmon->mon_ppdu_status = DP_PPDU_STATUS_START;
4194 
4195 	memset(&pmon->rx_mon_stats, 0,
4196 	       sizeof(pmon->rx_mon_stats));
4197 	return 0;
4198 }
4199 
4200 int ath12k_dp_rx_pdev_mon_attach(struct ath12k *ar)
4201 {
4202 	struct ath12k_pdev_dp *dp = &ar->dp;
4203 	struct ath12k_mon_data *pmon = &dp->mon_data;
4204 	int ret = 0;
4205 
4206 	ret = ath12k_dp_rx_pdev_mon_status_attach(ar);
4207 	if (ret) {
4208 		ath12k_warn(ar->ab, "pdev_mon_status_attach() failed");
4209 		return ret;
4210 	}
4211 
4212 	/* if rxdma1_enable is false, no need to setup
4213 	 * rxdma_mon_desc_ring.
4214 	 */
4215 	if (!ar->ab->hw_params->rxdma1_enable)
4216 		return 0;
4217 
4218 	pmon->mon_last_linkdesc_paddr = 0;
4219 	pmon->mon_last_buf_cookie = DP_RX_DESC_COOKIE_MAX + 1;
4220 	spin_lock_init(&pmon->mon_lock);
4221 
4222 	return 0;
4223 }
4224 
4225 int ath12k_dp_rx_pktlog_start(struct ath12k_base *ab)
4226 {
4227 	/* start reap timer */
4228 	mod_timer(&ab->mon_reap_timer,
4229 		  jiffies + msecs_to_jiffies(ATH12K_MON_TIMER_INTERVAL));
4230 
4231 	return 0;
4232 }
4233 
4234 int ath12k_dp_rx_pktlog_stop(struct ath12k_base *ab, bool stop_timer)
4235 {
4236 	int ret;
4237 
4238 	if (stop_timer)
4239 		del_timer_sync(&ab->mon_reap_timer);
4240 
4241 	/* reap all the monitor related rings */
4242 	ret = ath12k_dp_purge_mon_ring(ab);
4243 	if (ret) {
4244 		ath12k_warn(ab, "failed to purge dp mon ring: %d\n", ret);
4245 		return ret;
4246 	}
4247 
4248 	return 0;
4249 }
4250