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, pdev_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 		pdev_id = ath12k_hw_mac_id_to_pdev_id(ab->hw_params, mac_id);
2554 		ar = ab->pdevs[pdev_id].ar;
2555 		if (!rcu_dereference(ab->pdevs_active[pdev_id])) {
2556 			dev_kfree_skb_any(msdu);
2557 			continue;
2558 		}
2559 
2560 		if (test_bit(ATH12K_CAC_RUNNING, &ar->dev_flags)) {
2561 			dev_kfree_skb_any(msdu);
2562 			continue;
2563 		}
2564 
2565 		ret = ath12k_dp_rx_process_msdu(ar, msdu, msdu_list, &rx_status);
2566 		if (ret) {
2567 			ath12k_dbg(ab, ATH12K_DBG_DATA,
2568 				   "Unable to process msdu %d", ret);
2569 			dev_kfree_skb_any(msdu);
2570 			continue;
2571 		}
2572 
2573 		ath12k_dp_rx_deliver_msdu(ar, napi, msdu, &rx_status);
2574 	}
2575 
2576 	rcu_read_unlock();
2577 }
2578 
2579 int ath12k_dp_rx_process(struct ath12k_base *ab, int ring_id,
2580 			 struct napi_struct *napi, int budget)
2581 {
2582 	struct ath12k_rx_desc_info *desc_info;
2583 	struct ath12k_dp *dp = &ab->dp;
2584 	struct dp_rxdma_ring *rx_ring = &dp->rx_refill_buf_ring;
2585 	struct hal_reo_dest_ring *desc;
2586 	int num_buffs_reaped = 0;
2587 	struct sk_buff_head msdu_list;
2588 	struct ath12k_skb_rxcb *rxcb;
2589 	int total_msdu_reaped = 0;
2590 	struct hal_srng *srng;
2591 	struct sk_buff *msdu;
2592 	bool done = false;
2593 	int mac_id;
2594 	u64 desc_va;
2595 
2596 	__skb_queue_head_init(&msdu_list);
2597 
2598 	srng = &ab->hal.srng_list[dp->reo_dst_ring[ring_id].ring_id];
2599 
2600 	spin_lock_bh(&srng->lock);
2601 
2602 try_again:
2603 	ath12k_hal_srng_access_begin(ab, srng);
2604 
2605 	while ((desc = ath12k_hal_srng_dst_get_next_entry(ab, srng))) {
2606 		enum hal_reo_dest_ring_push_reason push_reason;
2607 		u32 cookie;
2608 
2609 		cookie = le32_get_bits(desc->buf_addr_info.info1,
2610 				       BUFFER_ADDR_INFO1_SW_COOKIE);
2611 
2612 		mac_id = le32_get_bits(desc->info0,
2613 				       HAL_REO_DEST_RING_INFO0_SRC_LINK_ID);
2614 
2615 		desc_va = ((u64)le32_to_cpu(desc->buf_va_hi) << 32 |
2616 			   le32_to_cpu(desc->buf_va_lo));
2617 		desc_info = (struct ath12k_rx_desc_info *)((unsigned long)desc_va);
2618 
2619 		/* retry manual desc retrieval */
2620 		if (!desc_info) {
2621 			desc_info = ath12k_dp_get_rx_desc(ab, cookie);
2622 			if (!desc_info) {
2623 				ath12k_warn(ab, "Invalid cookie in manual desc retrieval");
2624 				continue;
2625 			}
2626 		}
2627 
2628 		if (desc_info->magic != ATH12K_DP_RX_DESC_MAGIC)
2629 			ath12k_warn(ab, "Check HW CC implementation");
2630 
2631 		msdu = desc_info->skb;
2632 		desc_info->skb = NULL;
2633 
2634 		spin_lock_bh(&dp->rx_desc_lock);
2635 		list_move_tail(&desc_info->list, &dp->rx_desc_free_list);
2636 		spin_unlock_bh(&dp->rx_desc_lock);
2637 
2638 		rxcb = ATH12K_SKB_RXCB(msdu);
2639 		dma_unmap_single(ab->dev, rxcb->paddr,
2640 				 msdu->len + skb_tailroom(msdu),
2641 				 DMA_FROM_DEVICE);
2642 
2643 		num_buffs_reaped++;
2644 
2645 		push_reason = le32_get_bits(desc->info0,
2646 					    HAL_REO_DEST_RING_INFO0_PUSH_REASON);
2647 		if (push_reason !=
2648 		    HAL_REO_DEST_RING_PUSH_REASON_ROUTING_INSTRUCTION) {
2649 			dev_kfree_skb_any(msdu);
2650 			ab->soc_stats.hal_reo_error[dp->reo_dst_ring[ring_id].ring_id]++;
2651 			continue;
2652 		}
2653 
2654 		rxcb->is_first_msdu = !!(le32_to_cpu(desc->rx_msdu_info.info0) &
2655 					 RX_MSDU_DESC_INFO0_FIRST_MSDU_IN_MPDU);
2656 		rxcb->is_last_msdu = !!(le32_to_cpu(desc->rx_msdu_info.info0) &
2657 					RX_MSDU_DESC_INFO0_LAST_MSDU_IN_MPDU);
2658 		rxcb->is_continuation = !!(le32_to_cpu(desc->rx_msdu_info.info0) &
2659 					   RX_MSDU_DESC_INFO0_MSDU_CONTINUATION);
2660 		rxcb->mac_id = mac_id;
2661 		rxcb->peer_id = le32_get_bits(desc->rx_mpdu_info.peer_meta_data,
2662 					      RX_MPDU_DESC_META_DATA_PEER_ID);
2663 		rxcb->tid = le32_get_bits(desc->rx_mpdu_info.info0,
2664 					  RX_MPDU_DESC_INFO0_TID);
2665 
2666 		__skb_queue_tail(&msdu_list, msdu);
2667 
2668 		if (!rxcb->is_continuation) {
2669 			total_msdu_reaped++;
2670 			done = true;
2671 		} else {
2672 			done = false;
2673 		}
2674 
2675 		if (total_msdu_reaped >= budget)
2676 			break;
2677 	}
2678 
2679 	/* Hw might have updated the head pointer after we cached it.
2680 	 * In this case, even though there are entries in the ring we'll
2681 	 * get rx_desc NULL. Give the read another try with updated cached
2682 	 * head pointer so that we can reap complete MPDU in the current
2683 	 * rx processing.
2684 	 */
2685 	if (!done && ath12k_hal_srng_dst_num_free(ab, srng, true)) {
2686 		ath12k_hal_srng_access_end(ab, srng);
2687 		goto try_again;
2688 	}
2689 
2690 	ath12k_hal_srng_access_end(ab, srng);
2691 
2692 	spin_unlock_bh(&srng->lock);
2693 
2694 	if (!total_msdu_reaped)
2695 		goto exit;
2696 
2697 	/* TODO: Move to implicit BM? */
2698 	ath12k_dp_rx_bufs_replenish(ab, 0, rx_ring, num_buffs_reaped,
2699 				    ab->hw_params->hal_params->rx_buf_rbm, true);
2700 
2701 	ath12k_dp_rx_process_received_packets(ab, napi, &msdu_list,
2702 					      ring_id);
2703 
2704 exit:
2705 	return total_msdu_reaped;
2706 }
2707 
2708 static void ath12k_dp_rx_frag_timer(struct timer_list *timer)
2709 {
2710 	struct ath12k_dp_rx_tid *rx_tid = from_timer(rx_tid, timer, frag_timer);
2711 
2712 	spin_lock_bh(&rx_tid->ab->base_lock);
2713 	if (rx_tid->last_frag_no &&
2714 	    rx_tid->rx_frag_bitmap == GENMASK(rx_tid->last_frag_no, 0)) {
2715 		spin_unlock_bh(&rx_tid->ab->base_lock);
2716 		return;
2717 	}
2718 	ath12k_dp_rx_frags_cleanup(rx_tid, true);
2719 	spin_unlock_bh(&rx_tid->ab->base_lock);
2720 }
2721 
2722 int ath12k_dp_rx_peer_frag_setup(struct ath12k *ar, const u8 *peer_mac, int vdev_id)
2723 {
2724 	struct ath12k_base *ab = ar->ab;
2725 	struct crypto_shash *tfm;
2726 	struct ath12k_peer *peer;
2727 	struct ath12k_dp_rx_tid *rx_tid;
2728 	int i;
2729 
2730 	tfm = crypto_alloc_shash("michael_mic", 0, 0);
2731 	if (IS_ERR(tfm))
2732 		return PTR_ERR(tfm);
2733 
2734 	spin_lock_bh(&ab->base_lock);
2735 
2736 	peer = ath12k_peer_find(ab, vdev_id, peer_mac);
2737 	if (!peer) {
2738 		spin_unlock_bh(&ab->base_lock);
2739 		ath12k_warn(ab, "failed to find the peer to set up fragment info\n");
2740 		return -ENOENT;
2741 	}
2742 
2743 	for (i = 0; i <= IEEE80211_NUM_TIDS; i++) {
2744 		rx_tid = &peer->rx_tid[i];
2745 		rx_tid->ab = ab;
2746 		timer_setup(&rx_tid->frag_timer, ath12k_dp_rx_frag_timer, 0);
2747 		skb_queue_head_init(&rx_tid->rx_frags);
2748 	}
2749 
2750 	peer->tfm_mmic = tfm;
2751 	spin_unlock_bh(&ab->base_lock);
2752 
2753 	return 0;
2754 }
2755 
2756 static int ath12k_dp_rx_h_michael_mic(struct crypto_shash *tfm, u8 *key,
2757 				      struct ieee80211_hdr *hdr, u8 *data,
2758 				      size_t data_len, u8 *mic)
2759 {
2760 	SHASH_DESC_ON_STACK(desc, tfm);
2761 	u8 mic_hdr[16] = {0};
2762 	u8 tid = 0;
2763 	int ret;
2764 
2765 	if (!tfm)
2766 		return -EINVAL;
2767 
2768 	desc->tfm = tfm;
2769 
2770 	ret = crypto_shash_setkey(tfm, key, 8);
2771 	if (ret)
2772 		goto out;
2773 
2774 	ret = crypto_shash_init(desc);
2775 	if (ret)
2776 		goto out;
2777 
2778 	/* TKIP MIC header */
2779 	memcpy(mic_hdr, ieee80211_get_DA(hdr), ETH_ALEN);
2780 	memcpy(mic_hdr + ETH_ALEN, ieee80211_get_SA(hdr), ETH_ALEN);
2781 	if (ieee80211_is_data_qos(hdr->frame_control))
2782 		tid = ieee80211_get_tid(hdr);
2783 	mic_hdr[12] = tid;
2784 
2785 	ret = crypto_shash_update(desc, mic_hdr, 16);
2786 	if (ret)
2787 		goto out;
2788 	ret = crypto_shash_update(desc, data, data_len);
2789 	if (ret)
2790 		goto out;
2791 	ret = crypto_shash_final(desc, mic);
2792 out:
2793 	shash_desc_zero(desc);
2794 	return ret;
2795 }
2796 
2797 static int ath12k_dp_rx_h_verify_tkip_mic(struct ath12k *ar, struct ath12k_peer *peer,
2798 					  struct sk_buff *msdu)
2799 {
2800 	struct ath12k_base *ab = ar->ab;
2801 	struct hal_rx_desc *rx_desc = (struct hal_rx_desc *)msdu->data;
2802 	struct ieee80211_rx_status *rxs = IEEE80211_SKB_RXCB(msdu);
2803 	struct ieee80211_key_conf *key_conf;
2804 	struct ieee80211_hdr *hdr;
2805 	u8 mic[IEEE80211_CCMP_MIC_LEN];
2806 	int head_len, tail_len, ret;
2807 	size_t data_len;
2808 	u32 hdr_len, hal_rx_desc_sz = ar->ab->hw_params->hal_desc_sz;
2809 	u8 *key, *data;
2810 	u8 key_idx;
2811 
2812 	if (ath12k_dp_rx_h_enctype(ab, rx_desc) != HAL_ENCRYPT_TYPE_TKIP_MIC)
2813 		return 0;
2814 
2815 	hdr = (struct ieee80211_hdr *)(msdu->data + hal_rx_desc_sz);
2816 	hdr_len = ieee80211_hdrlen(hdr->frame_control);
2817 	head_len = hdr_len + hal_rx_desc_sz + IEEE80211_TKIP_IV_LEN;
2818 	tail_len = IEEE80211_CCMP_MIC_LEN + IEEE80211_TKIP_ICV_LEN + FCS_LEN;
2819 
2820 	if (!is_multicast_ether_addr(hdr->addr1))
2821 		key_idx = peer->ucast_keyidx;
2822 	else
2823 		key_idx = peer->mcast_keyidx;
2824 
2825 	key_conf = peer->keys[key_idx];
2826 
2827 	data = msdu->data + head_len;
2828 	data_len = msdu->len - head_len - tail_len;
2829 	key = &key_conf->key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY];
2830 
2831 	ret = ath12k_dp_rx_h_michael_mic(peer->tfm_mmic, key, hdr, data, data_len, mic);
2832 	if (ret || memcmp(mic, data + data_len, IEEE80211_CCMP_MIC_LEN))
2833 		goto mic_fail;
2834 
2835 	return 0;
2836 
2837 mic_fail:
2838 	(ATH12K_SKB_RXCB(msdu))->is_first_msdu = true;
2839 	(ATH12K_SKB_RXCB(msdu))->is_last_msdu = true;
2840 
2841 	rxs->flag |= RX_FLAG_MMIC_ERROR | RX_FLAG_MMIC_STRIPPED |
2842 		    RX_FLAG_IV_STRIPPED | RX_FLAG_DECRYPTED;
2843 	skb_pull(msdu, hal_rx_desc_sz);
2844 
2845 	ath12k_dp_rx_h_ppdu(ar, rx_desc, rxs);
2846 	ath12k_dp_rx_h_undecap(ar, msdu, rx_desc,
2847 			       HAL_ENCRYPT_TYPE_TKIP_MIC, rxs, true);
2848 	ieee80211_rx(ar->hw, msdu);
2849 	return -EINVAL;
2850 }
2851 
2852 static void ath12k_dp_rx_h_undecap_frag(struct ath12k *ar, struct sk_buff *msdu,
2853 					enum hal_encrypt_type enctype, u32 flags)
2854 {
2855 	struct ieee80211_hdr *hdr;
2856 	size_t hdr_len;
2857 	size_t crypto_len;
2858 	u32 hal_rx_desc_sz = ar->ab->hw_params->hal_desc_sz;
2859 
2860 	if (!flags)
2861 		return;
2862 
2863 	hdr = (struct ieee80211_hdr *)(msdu->data + hal_rx_desc_sz);
2864 
2865 	if (flags & RX_FLAG_MIC_STRIPPED)
2866 		skb_trim(msdu, msdu->len -
2867 			 ath12k_dp_rx_crypto_mic_len(ar, enctype));
2868 
2869 	if (flags & RX_FLAG_ICV_STRIPPED)
2870 		skb_trim(msdu, msdu->len -
2871 			 ath12k_dp_rx_crypto_icv_len(ar, enctype));
2872 
2873 	if (flags & RX_FLAG_IV_STRIPPED) {
2874 		hdr_len = ieee80211_hdrlen(hdr->frame_control);
2875 		crypto_len = ath12k_dp_rx_crypto_param_len(ar, enctype);
2876 
2877 		memmove(msdu->data + hal_rx_desc_sz + crypto_len,
2878 			msdu->data + hal_rx_desc_sz, hdr_len);
2879 		skb_pull(msdu, crypto_len);
2880 	}
2881 }
2882 
2883 static int ath12k_dp_rx_h_defrag(struct ath12k *ar,
2884 				 struct ath12k_peer *peer,
2885 				 struct ath12k_dp_rx_tid *rx_tid,
2886 				 struct sk_buff **defrag_skb)
2887 {
2888 	struct ath12k_base *ab = ar->ab;
2889 	struct hal_rx_desc *rx_desc;
2890 	struct sk_buff *skb, *first_frag, *last_frag;
2891 	struct ieee80211_hdr *hdr;
2892 	enum hal_encrypt_type enctype;
2893 	bool is_decrypted = false;
2894 	int msdu_len = 0;
2895 	int extra_space;
2896 	u32 flags, hal_rx_desc_sz = ar->ab->hw_params->hal_desc_sz;
2897 
2898 	first_frag = skb_peek(&rx_tid->rx_frags);
2899 	last_frag = skb_peek_tail(&rx_tid->rx_frags);
2900 
2901 	skb_queue_walk(&rx_tid->rx_frags, skb) {
2902 		flags = 0;
2903 		rx_desc = (struct hal_rx_desc *)skb->data;
2904 		hdr = (struct ieee80211_hdr *)(skb->data + hal_rx_desc_sz);
2905 
2906 		enctype = ath12k_dp_rx_h_enctype(ab, rx_desc);
2907 		if (enctype != HAL_ENCRYPT_TYPE_OPEN)
2908 			is_decrypted = ath12k_dp_rx_h_is_decrypted(ab,
2909 								   rx_desc);
2910 
2911 		if (is_decrypted) {
2912 			if (skb != first_frag)
2913 				flags |= RX_FLAG_IV_STRIPPED;
2914 			if (skb != last_frag)
2915 				flags |= RX_FLAG_ICV_STRIPPED |
2916 					 RX_FLAG_MIC_STRIPPED;
2917 		}
2918 
2919 		/* RX fragments are always raw packets */
2920 		if (skb != last_frag)
2921 			skb_trim(skb, skb->len - FCS_LEN);
2922 		ath12k_dp_rx_h_undecap_frag(ar, skb, enctype, flags);
2923 
2924 		if (skb != first_frag)
2925 			skb_pull(skb, hal_rx_desc_sz +
2926 				      ieee80211_hdrlen(hdr->frame_control));
2927 		msdu_len += skb->len;
2928 	}
2929 
2930 	extra_space = msdu_len - (DP_RX_BUFFER_SIZE + skb_tailroom(first_frag));
2931 	if (extra_space > 0 &&
2932 	    (pskb_expand_head(first_frag, 0, extra_space, GFP_ATOMIC) < 0))
2933 		return -ENOMEM;
2934 
2935 	__skb_unlink(first_frag, &rx_tid->rx_frags);
2936 	while ((skb = __skb_dequeue(&rx_tid->rx_frags))) {
2937 		skb_put_data(first_frag, skb->data, skb->len);
2938 		dev_kfree_skb_any(skb);
2939 	}
2940 
2941 	hdr = (struct ieee80211_hdr *)(first_frag->data + hal_rx_desc_sz);
2942 	hdr->frame_control &= ~__cpu_to_le16(IEEE80211_FCTL_MOREFRAGS);
2943 	ATH12K_SKB_RXCB(first_frag)->is_frag = 1;
2944 
2945 	if (ath12k_dp_rx_h_verify_tkip_mic(ar, peer, first_frag))
2946 		first_frag = NULL;
2947 
2948 	*defrag_skb = first_frag;
2949 	return 0;
2950 }
2951 
2952 static int ath12k_dp_rx_h_defrag_reo_reinject(struct ath12k *ar,
2953 					      struct ath12k_dp_rx_tid *rx_tid,
2954 					      struct sk_buff *defrag_skb)
2955 {
2956 	struct ath12k_base *ab = ar->ab;
2957 	struct ath12k_dp *dp = &ab->dp;
2958 	struct hal_rx_desc *rx_desc = (struct hal_rx_desc *)defrag_skb->data;
2959 	struct hal_reo_entrance_ring *reo_ent_ring;
2960 	struct hal_reo_dest_ring *reo_dest_ring;
2961 	struct dp_link_desc_bank *link_desc_banks;
2962 	struct hal_rx_msdu_link *msdu_link;
2963 	struct hal_rx_msdu_details *msdu0;
2964 	struct hal_srng *srng;
2965 	dma_addr_t link_paddr, buf_paddr;
2966 	u32 desc_bank, msdu_info, msdu_ext_info, mpdu_info;
2967 	u32 cookie, hal_rx_desc_sz, dest_ring_info0;
2968 	int ret;
2969 	struct ath12k_rx_desc_info *desc_info;
2970 	u8 dst_ind;
2971 
2972 	hal_rx_desc_sz = ab->hw_params->hal_desc_sz;
2973 	link_desc_banks = dp->link_desc_banks;
2974 	reo_dest_ring = rx_tid->dst_ring_desc;
2975 
2976 	ath12k_hal_rx_reo_ent_paddr_get(ab, &reo_dest_ring->buf_addr_info,
2977 					&link_paddr, &cookie);
2978 	desc_bank = u32_get_bits(cookie, DP_LINK_DESC_BANK_MASK);
2979 
2980 	msdu_link = (struct hal_rx_msdu_link *)(link_desc_banks[desc_bank].vaddr +
2981 			(link_paddr - link_desc_banks[desc_bank].paddr));
2982 	msdu0 = &msdu_link->msdu_link[0];
2983 	msdu_ext_info = le32_to_cpu(msdu0->rx_msdu_ext_info.info0);
2984 	dst_ind = u32_get_bits(msdu_ext_info, RX_MSDU_EXT_DESC_INFO0_REO_DEST_IND);
2985 
2986 	memset(msdu0, 0, sizeof(*msdu0));
2987 
2988 	msdu_info = u32_encode_bits(1, RX_MSDU_DESC_INFO0_FIRST_MSDU_IN_MPDU) |
2989 		    u32_encode_bits(1, RX_MSDU_DESC_INFO0_LAST_MSDU_IN_MPDU) |
2990 		    u32_encode_bits(0, RX_MSDU_DESC_INFO0_MSDU_CONTINUATION) |
2991 		    u32_encode_bits(defrag_skb->len - hal_rx_desc_sz,
2992 				    RX_MSDU_DESC_INFO0_MSDU_LENGTH) |
2993 		    u32_encode_bits(1, RX_MSDU_DESC_INFO0_VALID_SA) |
2994 		    u32_encode_bits(1, RX_MSDU_DESC_INFO0_VALID_DA);
2995 	msdu0->rx_msdu_info.info0 = cpu_to_le32(msdu_info);
2996 	msdu0->rx_msdu_ext_info.info0 = cpu_to_le32(msdu_ext_info);
2997 
2998 	/* change msdu len in hal rx desc */
2999 	ath12k_dp_rxdesc_set_msdu_len(ab, rx_desc, defrag_skb->len - hal_rx_desc_sz);
3000 
3001 	buf_paddr = dma_map_single(ab->dev, defrag_skb->data,
3002 				   defrag_skb->len + skb_tailroom(defrag_skb),
3003 				   DMA_FROM_DEVICE);
3004 	if (dma_mapping_error(ab->dev, buf_paddr))
3005 		return -ENOMEM;
3006 
3007 	spin_lock_bh(&dp->rx_desc_lock);
3008 	desc_info = list_first_entry_or_null(&dp->rx_desc_free_list,
3009 					     struct ath12k_rx_desc_info,
3010 					     list);
3011 	if (!desc_info) {
3012 		spin_unlock_bh(&dp->rx_desc_lock);
3013 		ath12k_warn(ab, "failed to find rx desc for reinject\n");
3014 		ret = -ENOMEM;
3015 		goto err_unmap_dma;
3016 	}
3017 
3018 	desc_info->skb = defrag_skb;
3019 
3020 	list_del(&desc_info->list);
3021 	list_add_tail(&desc_info->list, &dp->rx_desc_used_list);
3022 	spin_unlock_bh(&dp->rx_desc_lock);
3023 
3024 	ATH12K_SKB_RXCB(defrag_skb)->paddr = buf_paddr;
3025 
3026 	ath12k_hal_rx_buf_addr_info_set(&msdu0->buf_addr_info, buf_paddr,
3027 					desc_info->cookie,
3028 					HAL_RX_BUF_RBM_SW3_BM);
3029 
3030 	/* Fill mpdu details into reo entrance ring */
3031 	srng = &ab->hal.srng_list[dp->reo_reinject_ring.ring_id];
3032 
3033 	spin_lock_bh(&srng->lock);
3034 	ath12k_hal_srng_access_begin(ab, srng);
3035 
3036 	reo_ent_ring = ath12k_hal_srng_src_get_next_entry(ab, srng);
3037 	if (!reo_ent_ring) {
3038 		ath12k_hal_srng_access_end(ab, srng);
3039 		spin_unlock_bh(&srng->lock);
3040 		ret = -ENOSPC;
3041 		goto err_free_desc;
3042 	}
3043 	memset(reo_ent_ring, 0, sizeof(*reo_ent_ring));
3044 
3045 	ath12k_hal_rx_buf_addr_info_set(&reo_ent_ring->buf_addr_info, link_paddr,
3046 					cookie,
3047 					HAL_RX_BUF_RBM_WBM_CHIP0_IDLE_DESC_LIST);
3048 
3049 	mpdu_info = u32_encode_bits(1, RX_MPDU_DESC_INFO0_MSDU_COUNT) |
3050 		    u32_encode_bits(0, RX_MPDU_DESC_INFO0_FRAG_FLAG) |
3051 		    u32_encode_bits(1, RX_MPDU_DESC_INFO0_RAW_MPDU) |
3052 		    u32_encode_bits(1, RX_MPDU_DESC_INFO0_VALID_PN) |
3053 		    u32_encode_bits(rx_tid->tid, RX_MPDU_DESC_INFO0_TID);
3054 
3055 	reo_ent_ring->rx_mpdu_info.info0 = cpu_to_le32(mpdu_info);
3056 	reo_ent_ring->rx_mpdu_info.peer_meta_data =
3057 		reo_dest_ring->rx_mpdu_info.peer_meta_data;
3058 
3059 	/* Firmware expects physical address to be filled in queue_addr_lo in
3060 	 * the MLO scenario and in case of non MLO peer meta data needs to be
3061 	 * filled.
3062 	 * TODO: Need to handle for MLO scenario.
3063 	 */
3064 	reo_ent_ring->queue_addr_lo = reo_dest_ring->rx_mpdu_info.peer_meta_data;
3065 	reo_ent_ring->info0 = le32_encode_bits(dst_ind,
3066 					       HAL_REO_ENTR_RING_INFO0_DEST_IND);
3067 
3068 	reo_ent_ring->info1 = le32_encode_bits(rx_tid->cur_sn,
3069 					       HAL_REO_ENTR_RING_INFO1_MPDU_SEQ_NUM);
3070 	dest_ring_info0 = le32_get_bits(reo_dest_ring->info0,
3071 					HAL_REO_DEST_RING_INFO0_SRC_LINK_ID);
3072 	reo_ent_ring->info2 =
3073 		cpu_to_le32(u32_get_bits(dest_ring_info0,
3074 					 HAL_REO_ENTR_RING_INFO2_SRC_LINK_ID));
3075 
3076 	ath12k_hal_srng_access_end(ab, srng);
3077 	spin_unlock_bh(&srng->lock);
3078 
3079 	return 0;
3080 
3081 err_free_desc:
3082 	spin_lock_bh(&dp->rx_desc_lock);
3083 	list_del(&desc_info->list);
3084 	list_add_tail(&desc_info->list, &dp->rx_desc_free_list);
3085 	desc_info->skb = NULL;
3086 	spin_unlock_bh(&dp->rx_desc_lock);
3087 err_unmap_dma:
3088 	dma_unmap_single(ab->dev, buf_paddr, defrag_skb->len + skb_tailroom(defrag_skb),
3089 			 DMA_FROM_DEVICE);
3090 	return ret;
3091 }
3092 
3093 static int ath12k_dp_rx_h_cmp_frags(struct ath12k_base *ab,
3094 				    struct sk_buff *a, struct sk_buff *b)
3095 {
3096 	int frag1, frag2;
3097 
3098 	frag1 = ath12k_dp_rx_h_frag_no(ab, a);
3099 	frag2 = ath12k_dp_rx_h_frag_no(ab, b);
3100 
3101 	return frag1 - frag2;
3102 }
3103 
3104 static void ath12k_dp_rx_h_sort_frags(struct ath12k_base *ab,
3105 				      struct sk_buff_head *frag_list,
3106 				      struct sk_buff *cur_frag)
3107 {
3108 	struct sk_buff *skb;
3109 	int cmp;
3110 
3111 	skb_queue_walk(frag_list, skb) {
3112 		cmp = ath12k_dp_rx_h_cmp_frags(ab, skb, cur_frag);
3113 		if (cmp < 0)
3114 			continue;
3115 		__skb_queue_before(frag_list, skb, cur_frag);
3116 		return;
3117 	}
3118 	__skb_queue_tail(frag_list, cur_frag);
3119 }
3120 
3121 static u64 ath12k_dp_rx_h_get_pn(struct ath12k *ar, struct sk_buff *skb)
3122 {
3123 	struct ieee80211_hdr *hdr;
3124 	u64 pn = 0;
3125 	u8 *ehdr;
3126 	u32 hal_rx_desc_sz = ar->ab->hw_params->hal_desc_sz;
3127 
3128 	hdr = (struct ieee80211_hdr *)(skb->data + hal_rx_desc_sz);
3129 	ehdr = skb->data + hal_rx_desc_sz + ieee80211_hdrlen(hdr->frame_control);
3130 
3131 	pn = ehdr[0];
3132 	pn |= (u64)ehdr[1] << 8;
3133 	pn |= (u64)ehdr[4] << 16;
3134 	pn |= (u64)ehdr[5] << 24;
3135 	pn |= (u64)ehdr[6] << 32;
3136 	pn |= (u64)ehdr[7] << 40;
3137 
3138 	return pn;
3139 }
3140 
3141 static bool
3142 ath12k_dp_rx_h_defrag_validate_incr_pn(struct ath12k *ar, struct ath12k_dp_rx_tid *rx_tid)
3143 {
3144 	struct ath12k_base *ab = ar->ab;
3145 	enum hal_encrypt_type encrypt_type;
3146 	struct sk_buff *first_frag, *skb;
3147 	struct hal_rx_desc *desc;
3148 	u64 last_pn;
3149 	u64 cur_pn;
3150 
3151 	first_frag = skb_peek(&rx_tid->rx_frags);
3152 	desc = (struct hal_rx_desc *)first_frag->data;
3153 
3154 	encrypt_type = ath12k_dp_rx_h_enctype(ab, desc);
3155 	if (encrypt_type != HAL_ENCRYPT_TYPE_CCMP_128 &&
3156 	    encrypt_type != HAL_ENCRYPT_TYPE_CCMP_256 &&
3157 	    encrypt_type != HAL_ENCRYPT_TYPE_GCMP_128 &&
3158 	    encrypt_type != HAL_ENCRYPT_TYPE_AES_GCMP_256)
3159 		return true;
3160 
3161 	last_pn = ath12k_dp_rx_h_get_pn(ar, first_frag);
3162 	skb_queue_walk(&rx_tid->rx_frags, skb) {
3163 		if (skb == first_frag)
3164 			continue;
3165 
3166 		cur_pn = ath12k_dp_rx_h_get_pn(ar, skb);
3167 		if (cur_pn != last_pn + 1)
3168 			return false;
3169 		last_pn = cur_pn;
3170 	}
3171 	return true;
3172 }
3173 
3174 static int ath12k_dp_rx_frag_h_mpdu(struct ath12k *ar,
3175 				    struct sk_buff *msdu,
3176 				    struct hal_reo_dest_ring *ring_desc)
3177 {
3178 	struct ath12k_base *ab = ar->ab;
3179 	struct hal_rx_desc *rx_desc;
3180 	struct ath12k_peer *peer;
3181 	struct ath12k_dp_rx_tid *rx_tid;
3182 	struct sk_buff *defrag_skb = NULL;
3183 	u32 peer_id;
3184 	u16 seqno, frag_no;
3185 	u8 tid;
3186 	int ret = 0;
3187 	bool more_frags;
3188 
3189 	rx_desc = (struct hal_rx_desc *)msdu->data;
3190 	peer_id = ath12k_dp_rx_h_peer_id(ab, rx_desc);
3191 	tid = ath12k_dp_rx_h_tid(ab, rx_desc);
3192 	seqno = ath12k_dp_rx_h_seq_no(ab, rx_desc);
3193 	frag_no = ath12k_dp_rx_h_frag_no(ab, msdu);
3194 	more_frags = ath12k_dp_rx_h_more_frags(ab, msdu);
3195 
3196 	if (!ath12k_dp_rx_h_seq_ctrl_valid(ab, rx_desc) ||
3197 	    !ath12k_dp_rx_h_fc_valid(ab, rx_desc) ||
3198 	    tid > IEEE80211_NUM_TIDS)
3199 		return -EINVAL;
3200 
3201 	/* received unfragmented packet in reo
3202 	 * exception ring, this shouldn't happen
3203 	 * as these packets typically come from
3204 	 * reo2sw srngs.
3205 	 */
3206 	if (WARN_ON_ONCE(!frag_no && !more_frags))
3207 		return -EINVAL;
3208 
3209 	spin_lock_bh(&ab->base_lock);
3210 	peer = ath12k_peer_find_by_id(ab, peer_id);
3211 	if (!peer) {
3212 		ath12k_warn(ab, "failed to find the peer to de-fragment received fragment peer_id %d\n",
3213 			    peer_id);
3214 		ret = -ENOENT;
3215 		goto out_unlock;
3216 	}
3217 	rx_tid = &peer->rx_tid[tid];
3218 
3219 	if ((!skb_queue_empty(&rx_tid->rx_frags) && seqno != rx_tid->cur_sn) ||
3220 	    skb_queue_empty(&rx_tid->rx_frags)) {
3221 		/* Flush stored fragments and start a new sequence */
3222 		ath12k_dp_rx_frags_cleanup(rx_tid, true);
3223 		rx_tid->cur_sn = seqno;
3224 	}
3225 
3226 	if (rx_tid->rx_frag_bitmap & BIT(frag_no)) {
3227 		/* Fragment already present */
3228 		ret = -EINVAL;
3229 		goto out_unlock;
3230 	}
3231 
3232 	if (frag_no > __fls(rx_tid->rx_frag_bitmap))
3233 		__skb_queue_tail(&rx_tid->rx_frags, msdu);
3234 	else
3235 		ath12k_dp_rx_h_sort_frags(ab, &rx_tid->rx_frags, msdu);
3236 
3237 	rx_tid->rx_frag_bitmap |= BIT(frag_no);
3238 	if (!more_frags)
3239 		rx_tid->last_frag_no = frag_no;
3240 
3241 	if (frag_no == 0) {
3242 		rx_tid->dst_ring_desc = kmemdup(ring_desc,
3243 						sizeof(*rx_tid->dst_ring_desc),
3244 						GFP_ATOMIC);
3245 		if (!rx_tid->dst_ring_desc) {
3246 			ret = -ENOMEM;
3247 			goto out_unlock;
3248 		}
3249 	} else {
3250 		ath12k_dp_rx_link_desc_return(ab, ring_desc,
3251 					      HAL_WBM_REL_BM_ACT_PUT_IN_IDLE);
3252 	}
3253 
3254 	if (!rx_tid->last_frag_no ||
3255 	    rx_tid->rx_frag_bitmap != GENMASK(rx_tid->last_frag_no, 0)) {
3256 		mod_timer(&rx_tid->frag_timer, jiffies +
3257 					       ATH12K_DP_RX_FRAGMENT_TIMEOUT_MS);
3258 		goto out_unlock;
3259 	}
3260 
3261 	spin_unlock_bh(&ab->base_lock);
3262 	del_timer_sync(&rx_tid->frag_timer);
3263 	spin_lock_bh(&ab->base_lock);
3264 
3265 	peer = ath12k_peer_find_by_id(ab, peer_id);
3266 	if (!peer)
3267 		goto err_frags_cleanup;
3268 
3269 	if (!ath12k_dp_rx_h_defrag_validate_incr_pn(ar, rx_tid))
3270 		goto err_frags_cleanup;
3271 
3272 	if (ath12k_dp_rx_h_defrag(ar, peer, rx_tid, &defrag_skb))
3273 		goto err_frags_cleanup;
3274 
3275 	if (!defrag_skb)
3276 		goto err_frags_cleanup;
3277 
3278 	if (ath12k_dp_rx_h_defrag_reo_reinject(ar, rx_tid, defrag_skb))
3279 		goto err_frags_cleanup;
3280 
3281 	ath12k_dp_rx_frags_cleanup(rx_tid, false);
3282 	goto out_unlock;
3283 
3284 err_frags_cleanup:
3285 	dev_kfree_skb_any(defrag_skb);
3286 	ath12k_dp_rx_frags_cleanup(rx_tid, true);
3287 out_unlock:
3288 	spin_unlock_bh(&ab->base_lock);
3289 	return ret;
3290 }
3291 
3292 static int
3293 ath12k_dp_process_rx_err_buf(struct ath12k *ar, struct hal_reo_dest_ring *desc,
3294 			     bool drop, u32 cookie)
3295 {
3296 	struct ath12k_base *ab = ar->ab;
3297 	struct sk_buff *msdu;
3298 	struct ath12k_skb_rxcb *rxcb;
3299 	struct hal_rx_desc *rx_desc;
3300 	u16 msdu_len;
3301 	u32 hal_rx_desc_sz = ab->hw_params->hal_desc_sz;
3302 	struct ath12k_rx_desc_info *desc_info;
3303 	u64 desc_va;
3304 
3305 	desc_va = ((u64)le32_to_cpu(desc->buf_va_hi) << 32 |
3306 		   le32_to_cpu(desc->buf_va_lo));
3307 	desc_info = (struct ath12k_rx_desc_info *)((unsigned long)desc_va);
3308 
3309 	/* retry manual desc retrieval */
3310 	if (!desc_info) {
3311 		desc_info = ath12k_dp_get_rx_desc(ab, cookie);
3312 		if (!desc_info) {
3313 			ath12k_warn(ab, "Invalid cookie in manual desc retrieval");
3314 			return -EINVAL;
3315 		}
3316 	}
3317 
3318 	if (desc_info->magic != ATH12K_DP_RX_DESC_MAGIC)
3319 		ath12k_warn(ab, " RX Exception, Check HW CC implementation");
3320 
3321 	msdu = desc_info->skb;
3322 	desc_info->skb = NULL;
3323 	spin_lock_bh(&ab->dp.rx_desc_lock);
3324 	list_move_tail(&desc_info->list, &ab->dp.rx_desc_free_list);
3325 	spin_unlock_bh(&ab->dp.rx_desc_lock);
3326 
3327 	rxcb = ATH12K_SKB_RXCB(msdu);
3328 	dma_unmap_single(ar->ab->dev, rxcb->paddr,
3329 			 msdu->len + skb_tailroom(msdu),
3330 			 DMA_FROM_DEVICE);
3331 
3332 	if (drop) {
3333 		dev_kfree_skb_any(msdu);
3334 		return 0;
3335 	}
3336 
3337 	rcu_read_lock();
3338 	if (!rcu_dereference(ar->ab->pdevs_active[ar->pdev_idx])) {
3339 		dev_kfree_skb_any(msdu);
3340 		goto exit;
3341 	}
3342 
3343 	if (test_bit(ATH12K_CAC_RUNNING, &ar->dev_flags)) {
3344 		dev_kfree_skb_any(msdu);
3345 		goto exit;
3346 	}
3347 
3348 	rx_desc = (struct hal_rx_desc *)msdu->data;
3349 	msdu_len = ath12k_dp_rx_h_msdu_len(ar->ab, rx_desc);
3350 	if ((msdu_len + hal_rx_desc_sz) > DP_RX_BUFFER_SIZE) {
3351 		ath12k_warn(ar->ab, "invalid msdu leng %u", msdu_len);
3352 		ath12k_dbg_dump(ar->ab, ATH12K_DBG_DATA, NULL, "", rx_desc,
3353 				sizeof(*rx_desc));
3354 		dev_kfree_skb_any(msdu);
3355 		goto exit;
3356 	}
3357 
3358 	skb_put(msdu, hal_rx_desc_sz + msdu_len);
3359 
3360 	if (ath12k_dp_rx_frag_h_mpdu(ar, msdu, desc)) {
3361 		dev_kfree_skb_any(msdu);
3362 		ath12k_dp_rx_link_desc_return(ar->ab, desc,
3363 					      HAL_WBM_REL_BM_ACT_PUT_IN_IDLE);
3364 	}
3365 exit:
3366 	rcu_read_unlock();
3367 	return 0;
3368 }
3369 
3370 int ath12k_dp_rx_process_err(struct ath12k_base *ab, struct napi_struct *napi,
3371 			     int budget)
3372 {
3373 	u32 msdu_cookies[HAL_NUM_RX_MSDUS_PER_LINK_DESC];
3374 	struct dp_link_desc_bank *link_desc_banks;
3375 	enum hal_rx_buf_return_buf_manager rbm;
3376 	struct hal_rx_msdu_link *link_desc_va;
3377 	int tot_n_bufs_reaped, quota, ret, i;
3378 	struct hal_reo_dest_ring *reo_desc;
3379 	struct dp_rxdma_ring *rx_ring;
3380 	struct dp_srng *reo_except;
3381 	u32 desc_bank, num_msdus;
3382 	struct hal_srng *srng;
3383 	struct ath12k_dp *dp;
3384 	int mac_id;
3385 	struct ath12k *ar;
3386 	dma_addr_t paddr;
3387 	bool is_frag;
3388 	bool drop = false;
3389 	int pdev_id;
3390 
3391 	tot_n_bufs_reaped = 0;
3392 	quota = budget;
3393 
3394 	dp = &ab->dp;
3395 	reo_except = &dp->reo_except_ring;
3396 	link_desc_banks = dp->link_desc_banks;
3397 
3398 	srng = &ab->hal.srng_list[reo_except->ring_id];
3399 
3400 	spin_lock_bh(&srng->lock);
3401 
3402 	ath12k_hal_srng_access_begin(ab, srng);
3403 
3404 	while (budget &&
3405 	       (reo_desc = ath12k_hal_srng_dst_get_next_entry(ab, srng))) {
3406 		ab->soc_stats.err_ring_pkts++;
3407 		ret = ath12k_hal_desc_reo_parse_err(ab, reo_desc, &paddr,
3408 						    &desc_bank);
3409 		if (ret) {
3410 			ath12k_warn(ab, "failed to parse error reo desc %d\n",
3411 				    ret);
3412 			continue;
3413 		}
3414 		link_desc_va = link_desc_banks[desc_bank].vaddr +
3415 			       (paddr - link_desc_banks[desc_bank].paddr);
3416 		ath12k_hal_rx_msdu_link_info_get(link_desc_va, &num_msdus, msdu_cookies,
3417 						 &rbm);
3418 		if (rbm != HAL_RX_BUF_RBM_WBM_CHIP0_IDLE_DESC_LIST &&
3419 		    rbm != HAL_RX_BUF_RBM_SW3_BM &&
3420 		    rbm != ab->hw_params->hal_params->rx_buf_rbm) {
3421 			ab->soc_stats.invalid_rbm++;
3422 			ath12k_warn(ab, "invalid return buffer manager %d\n", rbm);
3423 			ath12k_dp_rx_link_desc_return(ab, reo_desc,
3424 						      HAL_WBM_REL_BM_ACT_REL_MSDU);
3425 			continue;
3426 		}
3427 
3428 		is_frag = !!(le32_to_cpu(reo_desc->rx_mpdu_info.info0) &
3429 			     RX_MPDU_DESC_INFO0_FRAG_FLAG);
3430 
3431 		/* Process only rx fragments with one msdu per link desc below, and drop
3432 		 * msdu's indicated due to error reasons.
3433 		 */
3434 		if (!is_frag || num_msdus > 1) {
3435 			drop = true;
3436 			/* Return the link desc back to wbm idle list */
3437 			ath12k_dp_rx_link_desc_return(ab, reo_desc,
3438 						      HAL_WBM_REL_BM_ACT_PUT_IN_IDLE);
3439 		}
3440 
3441 		for (i = 0; i < num_msdus; i++) {
3442 			mac_id = le32_get_bits(reo_desc->info0,
3443 					       HAL_REO_DEST_RING_INFO0_SRC_LINK_ID);
3444 
3445 			pdev_id = ath12k_hw_mac_id_to_pdev_id(ab->hw_params, mac_id);
3446 			ar = ab->pdevs[pdev_id].ar;
3447 
3448 			if (!ath12k_dp_process_rx_err_buf(ar, reo_desc, drop,
3449 							  msdu_cookies[i]))
3450 				tot_n_bufs_reaped++;
3451 		}
3452 
3453 		if (tot_n_bufs_reaped >= quota) {
3454 			tot_n_bufs_reaped = quota;
3455 			goto exit;
3456 		}
3457 
3458 		budget = quota - tot_n_bufs_reaped;
3459 	}
3460 
3461 exit:
3462 	ath12k_hal_srng_access_end(ab, srng);
3463 
3464 	spin_unlock_bh(&srng->lock);
3465 
3466 	rx_ring = &dp->rx_refill_buf_ring;
3467 
3468 	ath12k_dp_rx_bufs_replenish(ab, 0, rx_ring, tot_n_bufs_reaped,
3469 				    ab->hw_params->hal_params->rx_buf_rbm, true);
3470 
3471 	return tot_n_bufs_reaped;
3472 }
3473 
3474 static void ath12k_dp_rx_null_q_desc_sg_drop(struct ath12k *ar,
3475 					     int msdu_len,
3476 					     struct sk_buff_head *msdu_list)
3477 {
3478 	struct sk_buff *skb, *tmp;
3479 	struct ath12k_skb_rxcb *rxcb;
3480 	int n_buffs;
3481 
3482 	n_buffs = DIV_ROUND_UP(msdu_len,
3483 			       (DP_RX_BUFFER_SIZE - ar->ab->hw_params->hal_desc_sz));
3484 
3485 	skb_queue_walk_safe(msdu_list, skb, tmp) {
3486 		rxcb = ATH12K_SKB_RXCB(skb);
3487 		if (rxcb->err_rel_src == HAL_WBM_REL_SRC_MODULE_REO &&
3488 		    rxcb->err_code == HAL_REO_DEST_RING_ERROR_CODE_DESC_ADDR_ZERO) {
3489 			if (!n_buffs)
3490 				break;
3491 			__skb_unlink(skb, msdu_list);
3492 			dev_kfree_skb_any(skb);
3493 			n_buffs--;
3494 		}
3495 	}
3496 }
3497 
3498 static int ath12k_dp_rx_h_null_q_desc(struct ath12k *ar, struct sk_buff *msdu,
3499 				      struct ieee80211_rx_status *status,
3500 				      struct sk_buff_head *msdu_list)
3501 {
3502 	struct ath12k_base *ab = ar->ab;
3503 	u16 msdu_len, peer_id;
3504 	struct hal_rx_desc *desc = (struct hal_rx_desc *)msdu->data;
3505 	u8 l3pad_bytes;
3506 	struct ath12k_skb_rxcb *rxcb = ATH12K_SKB_RXCB(msdu);
3507 	u32 hal_rx_desc_sz = ar->ab->hw_params->hal_desc_sz;
3508 
3509 	msdu_len = ath12k_dp_rx_h_msdu_len(ab, desc);
3510 	peer_id = ath12k_dp_rx_h_peer_id(ab, desc);
3511 
3512 	spin_lock(&ab->base_lock);
3513 	if (!ath12k_peer_find_by_id(ab, peer_id)) {
3514 		spin_unlock(&ab->base_lock);
3515 		ath12k_dbg(ab, ATH12K_DBG_DATA, "invalid peer id received in wbm err pkt%d\n",
3516 			   peer_id);
3517 		return -EINVAL;
3518 	}
3519 	spin_unlock(&ab->base_lock);
3520 
3521 	if (!rxcb->is_frag && ((msdu_len + hal_rx_desc_sz) > DP_RX_BUFFER_SIZE)) {
3522 		/* First buffer will be freed by the caller, so deduct it's length */
3523 		msdu_len = msdu_len - (DP_RX_BUFFER_SIZE - hal_rx_desc_sz);
3524 		ath12k_dp_rx_null_q_desc_sg_drop(ar, msdu_len, msdu_list);
3525 		return -EINVAL;
3526 	}
3527 
3528 	/* Even after cleaning up the sg buffers in the msdu list with above check
3529 	 * any msdu received with continuation flag needs to be dropped as invalid.
3530 	 * This protects against some random err frame with continuation flag.
3531 	 */
3532 	if (rxcb->is_continuation)
3533 		return -EINVAL;
3534 
3535 	if (!ath12k_dp_rx_h_msdu_done(ab, desc)) {
3536 		ath12k_warn(ar->ab,
3537 			    "msdu_done bit not set in null_q_des processing\n");
3538 		__skb_queue_purge(msdu_list);
3539 		return -EIO;
3540 	}
3541 
3542 	/* Handle NULL queue descriptor violations arising out a missing
3543 	 * REO queue for a given peer or a given TID. This typically
3544 	 * may happen if a packet is received on a QOS enabled TID before the
3545 	 * ADDBA negotiation for that TID, when the TID queue is setup. Or
3546 	 * it may also happen for MC/BC frames if they are not routed to the
3547 	 * non-QOS TID queue, in the absence of any other default TID queue.
3548 	 * This error can show up both in a REO destination or WBM release ring.
3549 	 */
3550 
3551 	if (rxcb->is_frag) {
3552 		skb_pull(msdu, hal_rx_desc_sz);
3553 	} else {
3554 		l3pad_bytes = ath12k_dp_rx_h_l3pad(ab, desc);
3555 
3556 		if ((hal_rx_desc_sz + l3pad_bytes + msdu_len) > DP_RX_BUFFER_SIZE)
3557 			return -EINVAL;
3558 
3559 		skb_put(msdu, hal_rx_desc_sz + l3pad_bytes + msdu_len);
3560 		skb_pull(msdu, hal_rx_desc_sz + l3pad_bytes);
3561 	}
3562 	ath12k_dp_rx_h_ppdu(ar, desc, status);
3563 
3564 	ath12k_dp_rx_h_mpdu(ar, msdu, desc, status);
3565 
3566 	rxcb->tid = ath12k_dp_rx_h_tid(ab, desc);
3567 
3568 	/* Please note that caller will having the access to msdu and completing
3569 	 * rx with mac80211. Need not worry about cleaning up amsdu_list.
3570 	 */
3571 
3572 	return 0;
3573 }
3574 
3575 static bool ath12k_dp_rx_h_reo_err(struct ath12k *ar, struct sk_buff *msdu,
3576 				   struct ieee80211_rx_status *status,
3577 				   struct sk_buff_head *msdu_list)
3578 {
3579 	struct ath12k_skb_rxcb *rxcb = ATH12K_SKB_RXCB(msdu);
3580 	bool drop = false;
3581 
3582 	ar->ab->soc_stats.reo_error[rxcb->err_code]++;
3583 
3584 	switch (rxcb->err_code) {
3585 	case HAL_REO_DEST_RING_ERROR_CODE_DESC_ADDR_ZERO:
3586 		if (ath12k_dp_rx_h_null_q_desc(ar, msdu, status, msdu_list))
3587 			drop = true;
3588 		break;
3589 	case HAL_REO_DEST_RING_ERROR_CODE_PN_CHECK_FAILED:
3590 		/* TODO: Do not drop PN failed packets in the driver;
3591 		 * instead, it is good to drop such packets in mac80211
3592 		 * after incrementing the replay counters.
3593 		 */
3594 		fallthrough;
3595 	default:
3596 		/* TODO: Review other errors and process them to mac80211
3597 		 * as appropriate.
3598 		 */
3599 		drop = true;
3600 		break;
3601 	}
3602 
3603 	return drop;
3604 }
3605 
3606 static void ath12k_dp_rx_h_tkip_mic_err(struct ath12k *ar, struct sk_buff *msdu,
3607 					struct ieee80211_rx_status *status)
3608 {
3609 	struct ath12k_base *ab = ar->ab;
3610 	u16 msdu_len;
3611 	struct hal_rx_desc *desc = (struct hal_rx_desc *)msdu->data;
3612 	u8 l3pad_bytes;
3613 	struct ath12k_skb_rxcb *rxcb = ATH12K_SKB_RXCB(msdu);
3614 	u32 hal_rx_desc_sz = ar->ab->hw_params->hal_desc_sz;
3615 
3616 	rxcb->is_first_msdu = ath12k_dp_rx_h_first_msdu(ab, desc);
3617 	rxcb->is_last_msdu = ath12k_dp_rx_h_last_msdu(ab, desc);
3618 
3619 	l3pad_bytes = ath12k_dp_rx_h_l3pad(ab, desc);
3620 	msdu_len = ath12k_dp_rx_h_msdu_len(ab, desc);
3621 	skb_put(msdu, hal_rx_desc_sz + l3pad_bytes + msdu_len);
3622 	skb_pull(msdu, hal_rx_desc_sz + l3pad_bytes);
3623 
3624 	ath12k_dp_rx_h_ppdu(ar, desc, status);
3625 
3626 	status->flag |= (RX_FLAG_MMIC_STRIPPED | RX_FLAG_MMIC_ERROR |
3627 			 RX_FLAG_DECRYPTED);
3628 
3629 	ath12k_dp_rx_h_undecap(ar, msdu, desc,
3630 			       HAL_ENCRYPT_TYPE_TKIP_MIC, status, false);
3631 }
3632 
3633 static bool ath12k_dp_rx_h_rxdma_err(struct ath12k *ar,  struct sk_buff *msdu,
3634 				     struct ieee80211_rx_status *status)
3635 {
3636 	struct ath12k_base *ab = ar->ab;
3637 	struct ath12k_skb_rxcb *rxcb = ATH12K_SKB_RXCB(msdu);
3638 	struct hal_rx_desc *rx_desc = (struct hal_rx_desc *)msdu->data;
3639 	bool drop = false;
3640 	u32 err_bitmap;
3641 
3642 	ar->ab->soc_stats.rxdma_error[rxcb->err_code]++;
3643 
3644 	switch (rxcb->err_code) {
3645 	case HAL_REO_ENTR_RING_RXDMA_ECODE_DECRYPT_ERR:
3646 	case HAL_REO_ENTR_RING_RXDMA_ECODE_TKIP_MIC_ERR:
3647 		err_bitmap = ath12k_dp_rx_h_mpdu_err(ab, rx_desc);
3648 		if (err_bitmap & HAL_RX_MPDU_ERR_TKIP_MIC) {
3649 			ath12k_dp_rx_h_tkip_mic_err(ar, msdu, status);
3650 			break;
3651 		}
3652 		fallthrough;
3653 	default:
3654 		/* TODO: Review other rxdma error code to check if anything is
3655 		 * worth reporting to mac80211
3656 		 */
3657 		drop = true;
3658 		break;
3659 	}
3660 
3661 	return drop;
3662 }
3663 
3664 static void ath12k_dp_rx_wbm_err(struct ath12k *ar,
3665 				 struct napi_struct *napi,
3666 				 struct sk_buff *msdu,
3667 				 struct sk_buff_head *msdu_list)
3668 {
3669 	struct ath12k_skb_rxcb *rxcb = ATH12K_SKB_RXCB(msdu);
3670 	struct ieee80211_rx_status rxs = {0};
3671 	bool drop = true;
3672 
3673 	switch (rxcb->err_rel_src) {
3674 	case HAL_WBM_REL_SRC_MODULE_REO:
3675 		drop = ath12k_dp_rx_h_reo_err(ar, msdu, &rxs, msdu_list);
3676 		break;
3677 	case HAL_WBM_REL_SRC_MODULE_RXDMA:
3678 		drop = ath12k_dp_rx_h_rxdma_err(ar, msdu, &rxs);
3679 		break;
3680 	default:
3681 		/* msdu will get freed */
3682 		break;
3683 	}
3684 
3685 	if (drop) {
3686 		dev_kfree_skb_any(msdu);
3687 		return;
3688 	}
3689 
3690 	ath12k_dp_rx_deliver_msdu(ar, napi, msdu, &rxs);
3691 }
3692 
3693 int ath12k_dp_rx_process_wbm_err(struct ath12k_base *ab,
3694 				 struct napi_struct *napi, int budget)
3695 {
3696 	struct ath12k *ar;
3697 	struct ath12k_dp *dp = &ab->dp;
3698 	struct dp_rxdma_ring *rx_ring;
3699 	struct hal_rx_wbm_rel_info err_info;
3700 	struct hal_srng *srng;
3701 	struct sk_buff *msdu;
3702 	struct sk_buff_head msdu_list[MAX_RADIOS];
3703 	struct ath12k_skb_rxcb *rxcb;
3704 	void *rx_desc;
3705 	int mac_id;
3706 	int num_buffs_reaped = 0;
3707 	struct ath12k_rx_desc_info *desc_info;
3708 	int ret, i;
3709 
3710 	for (i = 0; i < ab->num_radios; i++)
3711 		__skb_queue_head_init(&msdu_list[i]);
3712 
3713 	srng = &ab->hal.srng_list[dp->rx_rel_ring.ring_id];
3714 	rx_ring = &dp->rx_refill_buf_ring;
3715 
3716 	spin_lock_bh(&srng->lock);
3717 
3718 	ath12k_hal_srng_access_begin(ab, srng);
3719 
3720 	while (budget) {
3721 		rx_desc = ath12k_hal_srng_dst_get_next_entry(ab, srng);
3722 		if (!rx_desc)
3723 			break;
3724 
3725 		ret = ath12k_hal_wbm_desc_parse_err(ab, rx_desc, &err_info);
3726 		if (ret) {
3727 			ath12k_warn(ab,
3728 				    "failed to parse rx error in wbm_rel ring desc %d\n",
3729 				    ret);
3730 			continue;
3731 		}
3732 
3733 		desc_info = (struct ath12k_rx_desc_info *)err_info.rx_desc;
3734 
3735 		/* retry manual desc retrieval if hw cc is not done */
3736 		if (!desc_info) {
3737 			desc_info = ath12k_dp_get_rx_desc(ab, err_info.cookie);
3738 			if (!desc_info) {
3739 				ath12k_warn(ab, "Invalid cookie in manual desc retrieval");
3740 				continue;
3741 			}
3742 		}
3743 
3744 		/* FIXME: Extract mac id correctly. Since descs are not tied
3745 		 * to mac, we can extract from vdev id in ring desc.
3746 		 */
3747 		mac_id = 0;
3748 
3749 		if (desc_info->magic != ATH12K_DP_RX_DESC_MAGIC)
3750 			ath12k_warn(ab, "WBM RX err, Check HW CC implementation");
3751 
3752 		msdu = desc_info->skb;
3753 		desc_info->skb = NULL;
3754 
3755 		spin_lock_bh(&dp->rx_desc_lock);
3756 		list_move_tail(&desc_info->list, &dp->rx_desc_free_list);
3757 		spin_unlock_bh(&dp->rx_desc_lock);
3758 
3759 		rxcb = ATH12K_SKB_RXCB(msdu);
3760 		dma_unmap_single(ab->dev, rxcb->paddr,
3761 				 msdu->len + skb_tailroom(msdu),
3762 				 DMA_FROM_DEVICE);
3763 
3764 		num_buffs_reaped++;
3765 
3766 		if (!err_info.continuation)
3767 			budget--;
3768 
3769 		if (err_info.push_reason !=
3770 		    HAL_REO_DEST_RING_PUSH_REASON_ERR_DETECTED) {
3771 			dev_kfree_skb_any(msdu);
3772 			continue;
3773 		}
3774 
3775 		rxcb->err_rel_src = err_info.err_rel_src;
3776 		rxcb->err_code = err_info.err_code;
3777 		rxcb->rx_desc = (struct hal_rx_desc *)msdu->data;
3778 		__skb_queue_tail(&msdu_list[mac_id], msdu);
3779 
3780 		rxcb->is_first_msdu = err_info.first_msdu;
3781 		rxcb->is_last_msdu = err_info.last_msdu;
3782 		rxcb->is_continuation = err_info.continuation;
3783 	}
3784 
3785 	ath12k_hal_srng_access_end(ab, srng);
3786 
3787 	spin_unlock_bh(&srng->lock);
3788 
3789 	if (!num_buffs_reaped)
3790 		goto done;
3791 
3792 	ath12k_dp_rx_bufs_replenish(ab, 0, rx_ring, num_buffs_reaped,
3793 				    ab->hw_params->hal_params->rx_buf_rbm, true);
3794 
3795 	rcu_read_lock();
3796 	for (i = 0; i <  ab->num_radios; i++) {
3797 		if (!rcu_dereference(ab->pdevs_active[i])) {
3798 			__skb_queue_purge(&msdu_list[i]);
3799 			continue;
3800 		}
3801 
3802 		ar = ab->pdevs[i].ar;
3803 
3804 		if (test_bit(ATH12K_CAC_RUNNING, &ar->dev_flags)) {
3805 			__skb_queue_purge(&msdu_list[i]);
3806 			continue;
3807 		}
3808 
3809 		while ((msdu = __skb_dequeue(&msdu_list[i])) != NULL)
3810 			ath12k_dp_rx_wbm_err(ar, napi, msdu, &msdu_list[i]);
3811 	}
3812 	rcu_read_unlock();
3813 done:
3814 	return num_buffs_reaped;
3815 }
3816 
3817 void ath12k_dp_rx_process_reo_status(struct ath12k_base *ab)
3818 {
3819 	struct ath12k_dp *dp = &ab->dp;
3820 	struct hal_tlv_64_hdr *hdr;
3821 	struct hal_srng *srng;
3822 	struct ath12k_dp_rx_reo_cmd *cmd, *tmp;
3823 	bool found = false;
3824 	u16 tag;
3825 	struct hal_reo_status reo_status;
3826 
3827 	srng = &ab->hal.srng_list[dp->reo_status_ring.ring_id];
3828 
3829 	memset(&reo_status, 0, sizeof(reo_status));
3830 
3831 	spin_lock_bh(&srng->lock);
3832 
3833 	ath12k_hal_srng_access_begin(ab, srng);
3834 
3835 	while ((hdr = ath12k_hal_srng_dst_get_next_entry(ab, srng))) {
3836 		tag = u64_get_bits(hdr->tl, HAL_SRNG_TLV_HDR_TAG);
3837 
3838 		switch (tag) {
3839 		case HAL_REO_GET_QUEUE_STATS_STATUS:
3840 			ath12k_hal_reo_status_queue_stats(ab, hdr,
3841 							  &reo_status);
3842 			break;
3843 		case HAL_REO_FLUSH_QUEUE_STATUS:
3844 			ath12k_hal_reo_flush_queue_status(ab, hdr,
3845 							  &reo_status);
3846 			break;
3847 		case HAL_REO_FLUSH_CACHE_STATUS:
3848 			ath12k_hal_reo_flush_cache_status(ab, hdr,
3849 							  &reo_status);
3850 			break;
3851 		case HAL_REO_UNBLOCK_CACHE_STATUS:
3852 			ath12k_hal_reo_unblk_cache_status(ab, hdr,
3853 							  &reo_status);
3854 			break;
3855 		case HAL_REO_FLUSH_TIMEOUT_LIST_STATUS:
3856 			ath12k_hal_reo_flush_timeout_list_status(ab, hdr,
3857 								 &reo_status);
3858 			break;
3859 		case HAL_REO_DESCRIPTOR_THRESHOLD_REACHED_STATUS:
3860 			ath12k_hal_reo_desc_thresh_reached_status(ab, hdr,
3861 								  &reo_status);
3862 			break;
3863 		case HAL_REO_UPDATE_RX_REO_QUEUE_STATUS:
3864 			ath12k_hal_reo_update_rx_reo_queue_status(ab, hdr,
3865 								  &reo_status);
3866 			break;
3867 		default:
3868 			ath12k_warn(ab, "Unknown reo status type %d\n", tag);
3869 			continue;
3870 		}
3871 
3872 		spin_lock_bh(&dp->reo_cmd_lock);
3873 		list_for_each_entry_safe(cmd, tmp, &dp->reo_cmd_list, list) {
3874 			if (reo_status.uniform_hdr.cmd_num == cmd->cmd_num) {
3875 				found = true;
3876 				list_del(&cmd->list);
3877 				break;
3878 			}
3879 		}
3880 		spin_unlock_bh(&dp->reo_cmd_lock);
3881 
3882 		if (found) {
3883 			cmd->handler(dp, (void *)&cmd->data,
3884 				     reo_status.uniform_hdr.cmd_status);
3885 			kfree(cmd);
3886 		}
3887 
3888 		found = false;
3889 	}
3890 
3891 	ath12k_hal_srng_access_end(ab, srng);
3892 
3893 	spin_unlock_bh(&srng->lock);
3894 }
3895 
3896 void ath12k_dp_rx_free(struct ath12k_base *ab)
3897 {
3898 	struct ath12k_dp *dp = &ab->dp;
3899 	int i;
3900 
3901 	ath12k_dp_srng_cleanup(ab, &dp->rx_refill_buf_ring.refill_buf_ring);
3902 
3903 	for (i = 0; i < ab->hw_params->num_rxmda_per_pdev; i++) {
3904 		if (ab->hw_params->rx_mac_buf_ring)
3905 			ath12k_dp_srng_cleanup(ab, &dp->rx_mac_buf_ring[i]);
3906 	}
3907 
3908 	for (i = 0; i < ab->hw_params->num_rxdma_dst_ring; i++)
3909 		ath12k_dp_srng_cleanup(ab, &dp->rxdma_err_dst_ring[i]);
3910 
3911 	ath12k_dp_srng_cleanup(ab, &dp->rxdma_mon_buf_ring.refill_buf_ring);
3912 	ath12k_dp_srng_cleanup(ab, &dp->tx_mon_buf_ring.refill_buf_ring);
3913 
3914 	ath12k_dp_rxdma_buf_free(ab);
3915 }
3916 
3917 void ath12k_dp_rx_pdev_free(struct ath12k_base *ab, int mac_id)
3918 {
3919 	struct ath12k *ar = ab->pdevs[mac_id].ar;
3920 
3921 	ath12k_dp_rx_pdev_srng_free(ar);
3922 }
3923 
3924 int ath12k_dp_rxdma_ring_sel_config_qcn9274(struct ath12k_base *ab)
3925 {
3926 	struct ath12k_dp *dp = &ab->dp;
3927 	struct htt_rx_ring_tlv_filter tlv_filter = {0};
3928 	u32 ring_id;
3929 	int ret;
3930 	u32 hal_rx_desc_sz = ab->hw_params->hal_desc_sz;
3931 
3932 	ring_id = dp->rx_refill_buf_ring.refill_buf_ring.ring_id;
3933 
3934 	tlv_filter.rx_filter = HTT_RX_TLV_FLAGS_RXDMA_RING;
3935 	tlv_filter.pkt_filter_flags2 = HTT_RX_FP_CTRL_PKT_FILTER_TLV_FLAGS2_BAR;
3936 	tlv_filter.pkt_filter_flags3 = HTT_RX_FP_DATA_PKT_FILTER_TLV_FLASG3_MCAST |
3937 					HTT_RX_FP_DATA_PKT_FILTER_TLV_FLASG3_UCAST |
3938 					HTT_RX_FP_DATA_PKT_FILTER_TLV_FLASG3_NULL_DATA;
3939 	tlv_filter.offset_valid = true;
3940 	tlv_filter.rx_packet_offset = hal_rx_desc_sz;
3941 
3942 	tlv_filter.rx_mpdu_start_offset =
3943 			ab->hw_params->hal_ops->rx_desc_get_mpdu_start_offset();
3944 	tlv_filter.rx_msdu_end_offset =
3945 		ab->hw_params->hal_ops->rx_desc_get_msdu_end_offset();
3946 
3947 	/* TODO: Selectively subscribe to required qwords within msdu_end
3948 	 * and mpdu_start and setup the mask in below msg
3949 	 * and modify the rx_desc struct
3950 	 */
3951 	ret = ath12k_dp_tx_htt_rx_filter_setup(ab, ring_id, 0,
3952 					       HAL_RXDMA_BUF,
3953 					       DP_RXDMA_REFILL_RING_SIZE,
3954 					       &tlv_filter);
3955 
3956 	return ret;
3957 }
3958 
3959 int ath12k_dp_rxdma_ring_sel_config_wcn7850(struct ath12k_base *ab)
3960 {
3961 	struct ath12k_dp *dp = &ab->dp;
3962 	struct htt_rx_ring_tlv_filter tlv_filter = {0};
3963 	u32 ring_id;
3964 	int ret;
3965 	u32 hal_rx_desc_sz = ab->hw_params->hal_desc_sz;
3966 	int i;
3967 
3968 	ring_id = dp->rx_refill_buf_ring.refill_buf_ring.ring_id;
3969 
3970 	tlv_filter.rx_filter = HTT_RX_TLV_FLAGS_RXDMA_RING;
3971 	tlv_filter.pkt_filter_flags2 = HTT_RX_FP_CTRL_PKT_FILTER_TLV_FLAGS2_BAR;
3972 	tlv_filter.pkt_filter_flags3 = HTT_RX_FP_DATA_PKT_FILTER_TLV_FLASG3_MCAST |
3973 					HTT_RX_FP_DATA_PKT_FILTER_TLV_FLASG3_UCAST |
3974 					HTT_RX_FP_DATA_PKT_FILTER_TLV_FLASG3_NULL_DATA;
3975 	tlv_filter.offset_valid = true;
3976 	tlv_filter.rx_packet_offset = hal_rx_desc_sz;
3977 
3978 	tlv_filter.rx_header_offset = offsetof(struct hal_rx_desc_wcn7850, pkt_hdr_tlv);
3979 
3980 	tlv_filter.rx_mpdu_start_offset =
3981 			ab->hw_params->hal_ops->rx_desc_get_mpdu_start_offset();
3982 	tlv_filter.rx_msdu_end_offset =
3983 		ab->hw_params->hal_ops->rx_desc_get_msdu_end_offset();
3984 
3985 	/* TODO: Selectively subscribe to required qwords within msdu_end
3986 	 * and mpdu_start and setup the mask in below msg
3987 	 * and modify the rx_desc struct
3988 	 */
3989 
3990 	for (i = 0; i < ab->hw_params->num_rxmda_per_pdev; i++) {
3991 		ring_id = dp->rx_mac_buf_ring[i].ring_id;
3992 		ret = ath12k_dp_tx_htt_rx_filter_setup(ab, ring_id, i,
3993 						       HAL_RXDMA_BUF,
3994 						       DP_RXDMA_REFILL_RING_SIZE,
3995 						       &tlv_filter);
3996 	}
3997 
3998 	return ret;
3999 }
4000 
4001 int ath12k_dp_rx_htt_setup(struct ath12k_base *ab)
4002 {
4003 	struct ath12k_dp *dp = &ab->dp;
4004 	u32 ring_id;
4005 	int i, ret;
4006 
4007 	/* TODO: Need to verify the HTT setup for QCN9224 */
4008 	ring_id = dp->rx_refill_buf_ring.refill_buf_ring.ring_id;
4009 	ret = ath12k_dp_tx_htt_srng_setup(ab, ring_id, 0, HAL_RXDMA_BUF);
4010 	if (ret) {
4011 		ath12k_warn(ab, "failed to configure rx_refill_buf_ring %d\n",
4012 			    ret);
4013 		return ret;
4014 	}
4015 
4016 	if (ab->hw_params->rx_mac_buf_ring) {
4017 		for (i = 0; i < ab->hw_params->num_rxmda_per_pdev; i++) {
4018 			ring_id = dp->rx_mac_buf_ring[i].ring_id;
4019 			ret = ath12k_dp_tx_htt_srng_setup(ab, ring_id,
4020 							  i, HAL_RXDMA_BUF);
4021 			if (ret) {
4022 				ath12k_warn(ab, "failed to configure rx_mac_buf_ring%d %d\n",
4023 					    i, ret);
4024 				return ret;
4025 			}
4026 		}
4027 	}
4028 
4029 	for (i = 0; i < ab->hw_params->num_rxdma_dst_ring; i++) {
4030 		ring_id = dp->rxdma_err_dst_ring[i].ring_id;
4031 		ret = ath12k_dp_tx_htt_srng_setup(ab, ring_id,
4032 						  i, HAL_RXDMA_DST);
4033 		if (ret) {
4034 			ath12k_warn(ab, "failed to configure rxdma_err_dest_ring%d %d\n",
4035 				    i, ret);
4036 			return ret;
4037 		}
4038 	}
4039 
4040 	if (ab->hw_params->rxdma1_enable) {
4041 		ring_id = dp->rxdma_mon_buf_ring.refill_buf_ring.ring_id;
4042 		ret = ath12k_dp_tx_htt_srng_setup(ab, ring_id,
4043 						  0, HAL_RXDMA_MONITOR_BUF);
4044 		if (ret) {
4045 			ath12k_warn(ab, "failed to configure rxdma_mon_buf_ring %d\n",
4046 				    ret);
4047 			return ret;
4048 		}
4049 
4050 		ring_id = dp->tx_mon_buf_ring.refill_buf_ring.ring_id;
4051 		ret = ath12k_dp_tx_htt_srng_setup(ab, ring_id,
4052 						  0, HAL_TX_MONITOR_BUF);
4053 		if (ret) {
4054 			ath12k_warn(ab, "failed to configure rxdma_mon_buf_ring %d\n",
4055 				    ret);
4056 			return ret;
4057 		}
4058 	}
4059 
4060 	ret = ab->hw_params->hw_ops->rxdma_ring_sel_config(ab);
4061 	if (ret) {
4062 		ath12k_warn(ab, "failed to setup rxdma ring selection config\n");
4063 		return ret;
4064 	}
4065 
4066 	return 0;
4067 }
4068 
4069 int ath12k_dp_rx_alloc(struct ath12k_base *ab)
4070 {
4071 	struct ath12k_dp *dp = &ab->dp;
4072 	int i, ret;
4073 
4074 	idr_init(&dp->rx_refill_buf_ring.bufs_idr);
4075 	spin_lock_init(&dp->rx_refill_buf_ring.idr_lock);
4076 
4077 	idr_init(&dp->rxdma_mon_buf_ring.bufs_idr);
4078 	spin_lock_init(&dp->rxdma_mon_buf_ring.idr_lock);
4079 
4080 	idr_init(&dp->tx_mon_buf_ring.bufs_idr);
4081 	spin_lock_init(&dp->tx_mon_buf_ring.idr_lock);
4082 
4083 	ret = ath12k_dp_srng_setup(ab,
4084 				   &dp->rx_refill_buf_ring.refill_buf_ring,
4085 				   HAL_RXDMA_BUF, 0, 0,
4086 				   DP_RXDMA_BUF_RING_SIZE);
4087 	if (ret) {
4088 		ath12k_warn(ab, "failed to setup rx_refill_buf_ring\n");
4089 		return ret;
4090 	}
4091 
4092 	if (ab->hw_params->rx_mac_buf_ring) {
4093 		for (i = 0; i < ab->hw_params->num_rxmda_per_pdev; i++) {
4094 			ret = ath12k_dp_srng_setup(ab,
4095 						   &dp->rx_mac_buf_ring[i],
4096 						   HAL_RXDMA_BUF, 1,
4097 						   i, 1024);
4098 			if (ret) {
4099 				ath12k_warn(ab, "failed to setup rx_mac_buf_ring %d\n",
4100 					    i);
4101 				return ret;
4102 			}
4103 		}
4104 	}
4105 
4106 	for (i = 0; i < ab->hw_params->num_rxdma_dst_ring; i++) {
4107 		ret = ath12k_dp_srng_setup(ab, &dp->rxdma_err_dst_ring[i],
4108 					   HAL_RXDMA_DST, 0, i,
4109 					   DP_RXDMA_ERR_DST_RING_SIZE);
4110 		if (ret) {
4111 			ath12k_warn(ab, "failed to setup rxdma_err_dst_ring %d\n", i);
4112 			return ret;
4113 		}
4114 	}
4115 
4116 	if (ab->hw_params->rxdma1_enable) {
4117 		ret = ath12k_dp_srng_setup(ab,
4118 					   &dp->rxdma_mon_buf_ring.refill_buf_ring,
4119 					   HAL_RXDMA_MONITOR_BUF, 0, 0,
4120 					   DP_RXDMA_MONITOR_BUF_RING_SIZE);
4121 		if (ret) {
4122 			ath12k_warn(ab, "failed to setup HAL_RXDMA_MONITOR_BUF\n");
4123 			return ret;
4124 		}
4125 
4126 		ret = ath12k_dp_srng_setup(ab,
4127 					   &dp->tx_mon_buf_ring.refill_buf_ring,
4128 					   HAL_TX_MONITOR_BUF, 0, 0,
4129 					   DP_TX_MONITOR_BUF_RING_SIZE);
4130 		if (ret) {
4131 			ath12k_warn(ab, "failed to setup DP_TX_MONITOR_BUF_RING_SIZE\n");
4132 			return ret;
4133 		}
4134 	}
4135 
4136 	ret = ath12k_dp_rxdma_buf_setup(ab);
4137 	if (ret) {
4138 		ath12k_warn(ab, "failed to setup rxdma ring\n");
4139 		return ret;
4140 	}
4141 
4142 	return 0;
4143 }
4144 
4145 int ath12k_dp_rx_pdev_alloc(struct ath12k_base *ab, int mac_id)
4146 {
4147 	struct ath12k *ar = ab->pdevs[mac_id].ar;
4148 	struct ath12k_pdev_dp *dp = &ar->dp;
4149 	u32 ring_id;
4150 	int i;
4151 	int ret;
4152 
4153 	if (!ab->hw_params->rxdma1_enable)
4154 		goto out;
4155 
4156 	ret = ath12k_dp_rx_pdev_srng_alloc(ar);
4157 	if (ret) {
4158 		ath12k_warn(ab, "failed to setup rx srngs\n");
4159 		return ret;
4160 	}
4161 
4162 	for (i = 0; i < ab->hw_params->num_rxmda_per_pdev; i++) {
4163 		ring_id = dp->rxdma_mon_dst_ring[i].ring_id;
4164 		ret = ath12k_dp_tx_htt_srng_setup(ab, ring_id,
4165 						  mac_id + i,
4166 						  HAL_RXDMA_MONITOR_DST);
4167 		if (ret) {
4168 			ath12k_warn(ab,
4169 				    "failed to configure rxdma_mon_dst_ring %d %d\n",
4170 				    i, ret);
4171 			return ret;
4172 		}
4173 
4174 		ring_id = dp->tx_mon_dst_ring[i].ring_id;
4175 		ret = ath12k_dp_tx_htt_srng_setup(ab, ring_id,
4176 						  mac_id + i,
4177 						  HAL_TX_MONITOR_DST);
4178 		if (ret) {
4179 			ath12k_warn(ab,
4180 				    "failed to configure tx_mon_dst_ring %d %d\n",
4181 				    i, ret);
4182 			return ret;
4183 		}
4184 	}
4185 out:
4186 	return 0;
4187 }
4188 
4189 static int ath12k_dp_rx_pdev_mon_status_attach(struct ath12k *ar)
4190 {
4191 	struct ath12k_pdev_dp *dp = &ar->dp;
4192 	struct ath12k_mon_data *pmon = (struct ath12k_mon_data *)&dp->mon_data;
4193 
4194 	skb_queue_head_init(&pmon->rx_status_q);
4195 
4196 	pmon->mon_ppdu_status = DP_PPDU_STATUS_START;
4197 
4198 	memset(&pmon->rx_mon_stats, 0,
4199 	       sizeof(pmon->rx_mon_stats));
4200 	return 0;
4201 }
4202 
4203 int ath12k_dp_rx_pdev_mon_attach(struct ath12k *ar)
4204 {
4205 	struct ath12k_pdev_dp *dp = &ar->dp;
4206 	struct ath12k_mon_data *pmon = &dp->mon_data;
4207 	int ret = 0;
4208 
4209 	ret = ath12k_dp_rx_pdev_mon_status_attach(ar);
4210 	if (ret) {
4211 		ath12k_warn(ar->ab, "pdev_mon_status_attach() failed");
4212 		return ret;
4213 	}
4214 
4215 	/* if rxdma1_enable is false, no need to setup
4216 	 * rxdma_mon_desc_ring.
4217 	 */
4218 	if (!ar->ab->hw_params->rxdma1_enable)
4219 		return 0;
4220 
4221 	pmon->mon_last_linkdesc_paddr = 0;
4222 	pmon->mon_last_buf_cookie = DP_RX_DESC_COOKIE_MAX + 1;
4223 	spin_lock_init(&pmon->mon_lock);
4224 
4225 	return 0;
4226 }
4227 
4228 int ath12k_dp_rx_pktlog_start(struct ath12k_base *ab)
4229 {
4230 	/* start reap timer */
4231 	mod_timer(&ab->mon_reap_timer,
4232 		  jiffies + msecs_to_jiffies(ATH12K_MON_TIMER_INTERVAL));
4233 
4234 	return 0;
4235 }
4236 
4237 int ath12k_dp_rx_pktlog_stop(struct ath12k_base *ab, bool stop_timer)
4238 {
4239 	int ret;
4240 
4241 	if (stop_timer)
4242 		del_timer_sync(&ab->mon_reap_timer);
4243 
4244 	/* reap all the monitor related rings */
4245 	ret = ath12k_dp_purge_mon_ring(ab);
4246 	if (ret) {
4247 		ath12k_warn(ab, "failed to purge dp mon ring: %d\n", ret);
4248 		return ret;
4249 	}
4250 
4251 	return 0;
4252 }
4253