1 // SPDX-License-Identifier: BSD-3-Clause-Clear
2 /*
3  * Copyright (c) 2018-2019 The Linux Foundation. All rights reserved.
4  */
5 
6 #include <linux/ieee80211.h>
7 #include <linux/kernel.h>
8 #include <linux/skbuff.h>
9 #include <crypto/hash.h>
10 #include "core.h"
11 #include "debug.h"
12 #include "hal_desc.h"
13 #include "hw.h"
14 #include "dp_rx.h"
15 #include "hal_rx.h"
16 #include "dp_tx.h"
17 #include "peer.h"
18 
19 #define ATH11K_DP_RX_FRAGMENT_TIMEOUT_MS (2 * HZ)
20 
21 static u8 *ath11k_dp_rx_h_80211_hdr(struct hal_rx_desc *desc)
22 {
23 	return desc->hdr_status;
24 }
25 
26 static enum hal_encrypt_type ath11k_dp_rx_h_mpdu_start_enctype(struct hal_rx_desc *desc)
27 {
28 	if (!(__le32_to_cpu(desc->mpdu_start.info1) &
29 	    RX_MPDU_START_INFO1_ENCRYPT_INFO_VALID))
30 		return HAL_ENCRYPT_TYPE_OPEN;
31 
32 	return FIELD_GET(RX_MPDU_START_INFO2_ENC_TYPE,
33 			 __le32_to_cpu(desc->mpdu_start.info2));
34 }
35 
36 static u8 ath11k_dp_rx_h_msdu_start_decap_type(struct hal_rx_desc *desc)
37 {
38 	return FIELD_GET(RX_MSDU_START_INFO2_DECAP_FORMAT,
39 			 __le32_to_cpu(desc->msdu_start.info2));
40 }
41 
42 static u8 ath11k_dp_rx_h_msdu_start_mesh_ctl_present(struct hal_rx_desc *desc)
43 {
44 	return FIELD_GET(RX_MSDU_START_INFO2_MESH_CTRL_PRESENT,
45 			 __le32_to_cpu(desc->msdu_start.info2));
46 }
47 
48 static bool ath11k_dp_rx_h_mpdu_start_seq_ctrl_valid(struct hal_rx_desc *desc)
49 {
50 	return !!FIELD_GET(RX_MPDU_START_INFO1_MPDU_SEQ_CTRL_VALID,
51 			   __le32_to_cpu(desc->mpdu_start.info1));
52 }
53 
54 static bool ath11k_dp_rx_h_mpdu_start_fc_valid(struct hal_rx_desc *desc)
55 {
56 	return !!FIELD_GET(RX_MPDU_START_INFO1_MPDU_FCTRL_VALID,
57 			   __le32_to_cpu(desc->mpdu_start.info1));
58 }
59 
60 static bool ath11k_dp_rx_h_mpdu_start_more_frags(struct sk_buff *skb)
61 {
62 	struct ieee80211_hdr *hdr;
63 
64 	hdr = (struct ieee80211_hdr *)(skb->data + HAL_RX_DESC_SIZE);
65 	return ieee80211_has_morefrags(hdr->frame_control);
66 }
67 
68 static u16 ath11k_dp_rx_h_mpdu_start_frag_no(struct sk_buff *skb)
69 {
70 	struct ieee80211_hdr *hdr;
71 
72 	hdr = (struct ieee80211_hdr *)(skb->data + HAL_RX_DESC_SIZE);
73 	return le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG;
74 }
75 
76 static u16 ath11k_dp_rx_h_mpdu_start_seq_no(struct hal_rx_desc *desc)
77 {
78 	return FIELD_GET(RX_MPDU_START_INFO1_MPDU_SEQ_NUM,
79 			 __le32_to_cpu(desc->mpdu_start.info1));
80 }
81 
82 static bool ath11k_dp_rx_h_attn_msdu_done(struct hal_rx_desc *desc)
83 {
84 	return !!FIELD_GET(RX_ATTENTION_INFO2_MSDU_DONE,
85 			   __le32_to_cpu(desc->attention.info2));
86 }
87 
88 static bool ath11k_dp_rx_h_attn_l4_cksum_fail(struct hal_rx_desc *desc)
89 {
90 	return !!FIELD_GET(RX_ATTENTION_INFO1_TCP_UDP_CKSUM_FAIL,
91 			   __le32_to_cpu(desc->attention.info1));
92 }
93 
94 static bool ath11k_dp_rx_h_attn_ip_cksum_fail(struct hal_rx_desc *desc)
95 {
96 	return !!FIELD_GET(RX_ATTENTION_INFO1_IP_CKSUM_FAIL,
97 			   __le32_to_cpu(desc->attention.info1));
98 }
99 
100 static bool ath11k_dp_rx_h_attn_is_decrypted(struct hal_rx_desc *desc)
101 {
102 	return (FIELD_GET(RX_ATTENTION_INFO2_DCRYPT_STATUS_CODE,
103 			  __le32_to_cpu(desc->attention.info2)) ==
104 		RX_DESC_DECRYPT_STATUS_CODE_OK);
105 }
106 
107 static u32 ath11k_dp_rx_h_attn_mpdu_err(struct hal_rx_desc *desc)
108 {
109 	u32 info = __le32_to_cpu(desc->attention.info1);
110 	u32 errmap = 0;
111 
112 	if (info & RX_ATTENTION_INFO1_FCS_ERR)
113 		errmap |= DP_RX_MPDU_ERR_FCS;
114 
115 	if (info & RX_ATTENTION_INFO1_DECRYPT_ERR)
116 		errmap |= DP_RX_MPDU_ERR_DECRYPT;
117 
118 	if (info & RX_ATTENTION_INFO1_TKIP_MIC_ERR)
119 		errmap |= DP_RX_MPDU_ERR_TKIP_MIC;
120 
121 	if (info & RX_ATTENTION_INFO1_A_MSDU_ERROR)
122 		errmap |= DP_RX_MPDU_ERR_AMSDU_ERR;
123 
124 	if (info & RX_ATTENTION_INFO1_OVERFLOW_ERR)
125 		errmap |= DP_RX_MPDU_ERR_OVERFLOW;
126 
127 	if (info & RX_ATTENTION_INFO1_MSDU_LEN_ERR)
128 		errmap |= DP_RX_MPDU_ERR_MSDU_LEN;
129 
130 	if (info & RX_ATTENTION_INFO1_MPDU_LEN_ERR)
131 		errmap |= DP_RX_MPDU_ERR_MPDU_LEN;
132 
133 	return errmap;
134 }
135 
136 static u16 ath11k_dp_rx_h_msdu_start_msdu_len(struct hal_rx_desc *desc)
137 {
138 	return FIELD_GET(RX_MSDU_START_INFO1_MSDU_LENGTH,
139 			 __le32_to_cpu(desc->msdu_start.info1));
140 }
141 
142 static u8 ath11k_dp_rx_h_msdu_start_sgi(struct hal_rx_desc *desc)
143 {
144 	return FIELD_GET(RX_MSDU_START_INFO3_SGI,
145 			 __le32_to_cpu(desc->msdu_start.info3));
146 }
147 
148 static u8 ath11k_dp_rx_h_msdu_start_rate_mcs(struct hal_rx_desc *desc)
149 {
150 	return FIELD_GET(RX_MSDU_START_INFO3_RATE_MCS,
151 			 __le32_to_cpu(desc->msdu_start.info3));
152 }
153 
154 static u8 ath11k_dp_rx_h_msdu_start_rx_bw(struct hal_rx_desc *desc)
155 {
156 	return FIELD_GET(RX_MSDU_START_INFO3_RECV_BW,
157 			 __le32_to_cpu(desc->msdu_start.info3));
158 }
159 
160 static u32 ath11k_dp_rx_h_msdu_start_freq(struct hal_rx_desc *desc)
161 {
162 	return __le32_to_cpu(desc->msdu_start.phy_meta_data);
163 }
164 
165 static u8 ath11k_dp_rx_h_msdu_start_pkt_type(struct hal_rx_desc *desc)
166 {
167 	return FIELD_GET(RX_MSDU_START_INFO3_PKT_TYPE,
168 			 __le32_to_cpu(desc->msdu_start.info3));
169 }
170 
171 static u8 ath11k_dp_rx_h_msdu_start_nss(struct hal_rx_desc *desc)
172 {
173 	u8 mimo_ss_bitmap = FIELD_GET(RX_MSDU_START_INFO3_MIMO_SS_BITMAP,
174 				      __le32_to_cpu(desc->msdu_start.info3));
175 
176 	return hweight8(mimo_ss_bitmap);
177 }
178 
179 static u8 ath11k_dp_rx_h_mpdu_start_tid(struct hal_rx_desc *desc)
180 {
181 	return FIELD_GET(RX_MPDU_START_INFO2_TID,
182 			 __le32_to_cpu(desc->mpdu_start.info2));
183 }
184 
185 static u16 ath11k_dp_rx_h_mpdu_start_peer_id(struct hal_rx_desc *desc)
186 {
187 	return __le16_to_cpu(desc->mpdu_start.sw_peer_id);
188 }
189 
190 static u8 ath11k_dp_rx_h_msdu_end_l3pad(struct hal_rx_desc *desc)
191 {
192 	return FIELD_GET(RX_MSDU_END_INFO2_L3_HDR_PADDING,
193 			 __le32_to_cpu(desc->msdu_end.info2));
194 }
195 
196 static bool ath11k_dp_rx_h_msdu_end_first_msdu(struct hal_rx_desc *desc)
197 {
198 	return !!FIELD_GET(RX_MSDU_END_INFO2_FIRST_MSDU,
199 			   __le32_to_cpu(desc->msdu_end.info2));
200 }
201 
202 static bool ath11k_dp_rx_h_msdu_end_last_msdu(struct hal_rx_desc *desc)
203 {
204 	return !!FIELD_GET(RX_MSDU_END_INFO2_LAST_MSDU,
205 			   __le32_to_cpu(desc->msdu_end.info2));
206 }
207 
208 static void ath11k_dp_rx_desc_end_tlv_copy(struct hal_rx_desc *fdesc,
209 					   struct hal_rx_desc *ldesc)
210 {
211 	memcpy((u8 *)&fdesc->msdu_end, (u8 *)&ldesc->msdu_end,
212 	       sizeof(struct rx_msdu_end));
213 	memcpy((u8 *)&fdesc->attention, (u8 *)&ldesc->attention,
214 	       sizeof(struct rx_attention));
215 	memcpy((u8 *)&fdesc->mpdu_end, (u8 *)&ldesc->mpdu_end,
216 	       sizeof(struct rx_mpdu_end));
217 }
218 
219 static u32 ath11k_dp_rxdesc_get_mpdulen_err(struct hal_rx_desc *rx_desc)
220 {
221 	struct rx_attention *rx_attn;
222 
223 	rx_attn = &rx_desc->attention;
224 
225 	return FIELD_GET(RX_ATTENTION_INFO1_MPDU_LEN_ERR,
226 			 __le32_to_cpu(rx_attn->info1));
227 }
228 
229 static u32 ath11k_dp_rxdesc_get_decap_format(struct hal_rx_desc *rx_desc)
230 {
231 	struct rx_msdu_start *rx_msdu_start;
232 
233 	rx_msdu_start = &rx_desc->msdu_start;
234 
235 	return FIELD_GET(RX_MSDU_START_INFO2_DECAP_FORMAT,
236 			 __le32_to_cpu(rx_msdu_start->info2));
237 }
238 
239 static u8 *ath11k_dp_rxdesc_get_80211hdr(struct hal_rx_desc *rx_desc)
240 {
241 	u8 *rx_pkt_hdr;
242 
243 	rx_pkt_hdr = &rx_desc->msdu_payload[0];
244 
245 	return rx_pkt_hdr;
246 }
247 
248 static bool ath11k_dp_rxdesc_mpdu_valid(struct hal_rx_desc *rx_desc)
249 {
250 	u32 tlv_tag;
251 
252 	tlv_tag = FIELD_GET(HAL_TLV_HDR_TAG,
253 			    __le32_to_cpu(rx_desc->mpdu_start_tag));
254 
255 	return tlv_tag == HAL_RX_MPDU_START;
256 }
257 
258 static u32 ath11k_dp_rxdesc_get_ppduid(struct hal_rx_desc *rx_desc)
259 {
260 	return __le16_to_cpu(rx_desc->mpdu_start.phy_ppdu_id);
261 }
262 
263 /* Returns number of Rx buffers replenished */
264 int ath11k_dp_rxbufs_replenish(struct ath11k_base *ab, int mac_id,
265 			       struct dp_rxdma_ring *rx_ring,
266 			       int req_entries,
267 			       enum hal_rx_buf_return_buf_manager mgr,
268 			       gfp_t gfp)
269 {
270 	struct hal_srng *srng;
271 	u32 *desc;
272 	struct sk_buff *skb;
273 	int num_free;
274 	int num_remain;
275 	int buf_id;
276 	u32 cookie;
277 	dma_addr_t paddr;
278 
279 	req_entries = min(req_entries, rx_ring->bufs_max);
280 
281 	srng = &ab->hal.srng_list[rx_ring->refill_buf_ring.ring_id];
282 
283 	spin_lock_bh(&srng->lock);
284 
285 	ath11k_hal_srng_access_begin(ab, srng);
286 
287 	num_free = ath11k_hal_srng_src_num_free(ab, srng, true);
288 	if (!req_entries && (num_free > (rx_ring->bufs_max * 3) / 4))
289 		req_entries = num_free;
290 
291 	req_entries = min(num_free, req_entries);
292 	num_remain = req_entries;
293 
294 	while (num_remain > 0) {
295 		skb = dev_alloc_skb(DP_RX_BUFFER_SIZE +
296 				    DP_RX_BUFFER_ALIGN_SIZE);
297 		if (!skb)
298 			break;
299 
300 		if (!IS_ALIGNED((unsigned long)skb->data,
301 				DP_RX_BUFFER_ALIGN_SIZE)) {
302 			skb_pull(skb,
303 				 PTR_ALIGN(skb->data, DP_RX_BUFFER_ALIGN_SIZE) -
304 				 skb->data);
305 		}
306 
307 		paddr = dma_map_single(ab->dev, skb->data,
308 				       skb->len + skb_tailroom(skb),
309 				       DMA_FROM_DEVICE);
310 		if (dma_mapping_error(ab->dev, paddr))
311 			goto fail_free_skb;
312 
313 		spin_lock_bh(&rx_ring->idr_lock);
314 		buf_id = idr_alloc(&rx_ring->bufs_idr, skb, 0,
315 				   rx_ring->bufs_max * 3, gfp);
316 		spin_unlock_bh(&rx_ring->idr_lock);
317 		if (buf_id < 0)
318 			goto fail_dma_unmap;
319 
320 		desc = ath11k_hal_srng_src_get_next_entry(ab, srng);
321 		if (!desc)
322 			goto fail_idr_remove;
323 
324 		ATH11K_SKB_RXCB(skb)->paddr = paddr;
325 
326 		cookie = FIELD_PREP(DP_RXDMA_BUF_COOKIE_PDEV_ID, mac_id) |
327 			 FIELD_PREP(DP_RXDMA_BUF_COOKIE_BUF_ID, buf_id);
328 
329 		num_remain--;
330 
331 		ath11k_hal_rx_buf_addr_info_set(desc, paddr, cookie, mgr);
332 	}
333 
334 	ath11k_hal_srng_access_end(ab, srng);
335 
336 	spin_unlock_bh(&srng->lock);
337 
338 	return req_entries - num_remain;
339 
340 fail_idr_remove:
341 	spin_lock_bh(&rx_ring->idr_lock);
342 	idr_remove(&rx_ring->bufs_idr, buf_id);
343 	spin_unlock_bh(&rx_ring->idr_lock);
344 fail_dma_unmap:
345 	dma_unmap_single(ab->dev, paddr, skb->len + skb_tailroom(skb),
346 			 DMA_FROM_DEVICE);
347 fail_free_skb:
348 	dev_kfree_skb_any(skb);
349 
350 	ath11k_hal_srng_access_end(ab, srng);
351 
352 	spin_unlock_bh(&srng->lock);
353 
354 	return req_entries - num_remain;
355 }
356 
357 static int ath11k_dp_rxdma_buf_ring_free(struct ath11k *ar,
358 					 struct dp_rxdma_ring *rx_ring)
359 {
360 	struct ath11k_pdev_dp *dp = &ar->dp;
361 	struct sk_buff *skb;
362 	int buf_id;
363 
364 	spin_lock_bh(&rx_ring->idr_lock);
365 	idr_for_each_entry(&rx_ring->bufs_idr, skb, buf_id) {
366 		idr_remove(&rx_ring->bufs_idr, buf_id);
367 		/* TODO: Understand where internal driver does this dma_unmap of
368 		 * of rxdma_buffer.
369 		 */
370 		dma_unmap_single(ar->ab->dev, ATH11K_SKB_RXCB(skb)->paddr,
371 				 skb->len + skb_tailroom(skb), DMA_FROM_DEVICE);
372 		dev_kfree_skb_any(skb);
373 	}
374 
375 	idr_destroy(&rx_ring->bufs_idr);
376 	spin_unlock_bh(&rx_ring->idr_lock);
377 
378 	rx_ring = &dp->rx_mon_status_refill_ring;
379 
380 	spin_lock_bh(&rx_ring->idr_lock);
381 	idr_for_each_entry(&rx_ring->bufs_idr, skb, buf_id) {
382 		idr_remove(&rx_ring->bufs_idr, buf_id);
383 		/* XXX: Understand where internal driver does this dma_unmap of
384 		 * of rxdma_buffer.
385 		 */
386 		dma_unmap_single(ar->ab->dev, ATH11K_SKB_RXCB(skb)->paddr,
387 				 skb->len + skb_tailroom(skb), DMA_BIDIRECTIONAL);
388 		dev_kfree_skb_any(skb);
389 	}
390 
391 	idr_destroy(&rx_ring->bufs_idr);
392 	spin_unlock_bh(&rx_ring->idr_lock);
393 	return 0;
394 }
395 
396 static int ath11k_dp_rxdma_pdev_buf_free(struct ath11k *ar)
397 {
398 	struct ath11k_pdev_dp *dp = &ar->dp;
399 	struct dp_rxdma_ring *rx_ring = &dp->rx_refill_buf_ring;
400 
401 	ath11k_dp_rxdma_buf_ring_free(ar, rx_ring);
402 
403 	rx_ring = &dp->rxdma_mon_buf_ring;
404 	ath11k_dp_rxdma_buf_ring_free(ar, rx_ring);
405 
406 	rx_ring = &dp->rx_mon_status_refill_ring;
407 	ath11k_dp_rxdma_buf_ring_free(ar, rx_ring);
408 	return 0;
409 }
410 
411 static int ath11k_dp_rxdma_ring_buf_setup(struct ath11k *ar,
412 					  struct dp_rxdma_ring *rx_ring,
413 					  u32 ringtype)
414 {
415 	struct ath11k_pdev_dp *dp = &ar->dp;
416 	int num_entries;
417 
418 	num_entries = rx_ring->refill_buf_ring.size /
419 		      ath11k_hal_srng_get_entrysize(ringtype);
420 
421 	rx_ring->bufs_max = num_entries;
422 	ath11k_dp_rxbufs_replenish(ar->ab, dp->mac_id, rx_ring, num_entries,
423 				   HAL_RX_BUF_RBM_SW3_BM, GFP_KERNEL);
424 	return 0;
425 }
426 
427 static int ath11k_dp_rxdma_pdev_buf_setup(struct ath11k *ar)
428 {
429 	struct ath11k_pdev_dp *dp = &ar->dp;
430 	struct dp_rxdma_ring *rx_ring = &dp->rx_refill_buf_ring;
431 
432 	ath11k_dp_rxdma_ring_buf_setup(ar, rx_ring, HAL_RXDMA_BUF);
433 
434 	rx_ring = &dp->rxdma_mon_buf_ring;
435 	ath11k_dp_rxdma_ring_buf_setup(ar, rx_ring, HAL_RXDMA_MONITOR_BUF);
436 
437 	rx_ring = &dp->rx_mon_status_refill_ring;
438 	ath11k_dp_rxdma_ring_buf_setup(ar, rx_ring, HAL_RXDMA_MONITOR_STATUS);
439 
440 	return 0;
441 }
442 
443 static void ath11k_dp_rx_pdev_srng_free(struct ath11k *ar)
444 {
445 	struct ath11k_pdev_dp *dp = &ar->dp;
446 
447 	ath11k_dp_srng_cleanup(ar->ab, &dp->rx_refill_buf_ring.refill_buf_ring);
448 	ath11k_dp_srng_cleanup(ar->ab, &dp->rxdma_err_dst_ring);
449 	ath11k_dp_srng_cleanup(ar->ab, &dp->rx_mon_status_refill_ring.refill_buf_ring);
450 	ath11k_dp_srng_cleanup(ar->ab, &dp->rxdma_mon_buf_ring.refill_buf_ring);
451 }
452 
453 void ath11k_dp_pdev_reo_cleanup(struct ath11k_base *ab)
454 {
455 	struct ath11k_dp *dp = &ab->dp;
456 	int i;
457 
458 	for (i = 0; i < DP_REO_DST_RING_MAX; i++)
459 		ath11k_dp_srng_cleanup(ab, &dp->reo_dst_ring[i]);
460 }
461 
462 int ath11k_dp_pdev_reo_setup(struct ath11k_base *ab)
463 {
464 	struct ath11k_dp *dp = &ab->dp;
465 	int ret;
466 	int i;
467 
468 	for (i = 0; i < DP_REO_DST_RING_MAX; i++) {
469 		ret = ath11k_dp_srng_setup(ab, &dp->reo_dst_ring[i],
470 					   HAL_REO_DST, i, 0,
471 					   DP_REO_DST_RING_SIZE);
472 		if (ret) {
473 			ath11k_warn(ab, "failed to setup reo_dst_ring\n");
474 			goto err_reo_cleanup;
475 		}
476 	}
477 
478 	return 0;
479 
480 err_reo_cleanup:
481 	ath11k_dp_pdev_reo_cleanup(ab);
482 
483 	return ret;
484 }
485 
486 static int ath11k_dp_rx_pdev_srng_alloc(struct ath11k *ar)
487 {
488 	struct ath11k_pdev_dp *dp = &ar->dp;
489 	struct dp_srng *srng = NULL;
490 	int ret;
491 
492 	ret = ath11k_dp_srng_setup(ar->ab,
493 				   &dp->rx_refill_buf_ring.refill_buf_ring,
494 				   HAL_RXDMA_BUF, 0,
495 				   dp->mac_id, DP_RXDMA_BUF_RING_SIZE);
496 	if (ret) {
497 		ath11k_warn(ar->ab, "failed to setup rx_refill_buf_ring\n");
498 		return ret;
499 	}
500 
501 	ret = ath11k_dp_srng_setup(ar->ab, &dp->rxdma_err_dst_ring,
502 				   HAL_RXDMA_DST, 0, dp->mac_id,
503 				   DP_RXDMA_ERR_DST_RING_SIZE);
504 	if (ret) {
505 		ath11k_warn(ar->ab, "failed to setup rxdma_err_dst_ring\n");
506 		return ret;
507 	}
508 
509 	srng = &dp->rx_mon_status_refill_ring.refill_buf_ring;
510 	ret = ath11k_dp_srng_setup(ar->ab,
511 				   srng,
512 				   HAL_RXDMA_MONITOR_STATUS, 0, dp->mac_id,
513 				   DP_RXDMA_MON_STATUS_RING_SIZE);
514 	if (ret) {
515 		ath11k_warn(ar->ab,
516 			    "failed to setup rx_mon_status_refill_ring\n");
517 		return ret;
518 	}
519 	ret = ath11k_dp_srng_setup(ar->ab,
520 				   &dp->rxdma_mon_buf_ring.refill_buf_ring,
521 				   HAL_RXDMA_MONITOR_BUF, 0, dp->mac_id,
522 				   DP_RXDMA_MONITOR_BUF_RING_SIZE);
523 	if (ret) {
524 		ath11k_warn(ar->ab,
525 			    "failed to setup HAL_RXDMA_MONITOR_BUF\n");
526 		return ret;
527 	}
528 
529 	ret = ath11k_dp_srng_setup(ar->ab, &dp->rxdma_mon_dst_ring,
530 				   HAL_RXDMA_MONITOR_DST, 0, dp->mac_id,
531 				   DP_RXDMA_MONITOR_DST_RING_SIZE);
532 	if (ret) {
533 		ath11k_warn(ar->ab,
534 			    "failed to setup HAL_RXDMA_MONITOR_DST\n");
535 		return ret;
536 	}
537 
538 	ret = ath11k_dp_srng_setup(ar->ab, &dp->rxdma_mon_desc_ring,
539 				   HAL_RXDMA_MONITOR_DESC, 0, dp->mac_id,
540 				   DP_RXDMA_MONITOR_DESC_RING_SIZE);
541 	if (ret) {
542 		ath11k_warn(ar->ab,
543 			    "failed to setup HAL_RXDMA_MONITOR_DESC\n");
544 		return ret;
545 	}
546 
547 	return 0;
548 }
549 
550 void ath11k_dp_reo_cmd_list_cleanup(struct ath11k_base *ab)
551 {
552 	struct ath11k_dp *dp = &ab->dp;
553 	struct dp_reo_cmd *cmd, *tmp;
554 	struct dp_reo_cache_flush_elem *cmd_cache, *tmp_cache;
555 
556 	spin_lock_bh(&dp->reo_cmd_lock);
557 	list_for_each_entry_safe(cmd, tmp, &dp->reo_cmd_list, list) {
558 		list_del(&cmd->list);
559 		dma_unmap_single(ab->dev, cmd->data.paddr,
560 				 cmd->data.size, DMA_BIDIRECTIONAL);
561 		kfree(cmd->data.vaddr);
562 		kfree(cmd);
563 	}
564 
565 	list_for_each_entry_safe(cmd_cache, tmp_cache,
566 				 &dp->reo_cmd_cache_flush_list, list) {
567 		list_del(&cmd_cache->list);
568 		dp->reo_cmd_cache_flush_count--;
569 		dma_unmap_single(ab->dev, cmd_cache->data.paddr,
570 				 cmd_cache->data.size, DMA_BIDIRECTIONAL);
571 		kfree(cmd_cache->data.vaddr);
572 		kfree(cmd_cache);
573 	}
574 	spin_unlock_bh(&dp->reo_cmd_lock);
575 }
576 
577 static void ath11k_dp_reo_cmd_free(struct ath11k_dp *dp, void *ctx,
578 				   enum hal_reo_cmd_status status)
579 {
580 	struct dp_rx_tid *rx_tid = ctx;
581 
582 	if (status != HAL_REO_CMD_SUCCESS)
583 		ath11k_warn(dp->ab, "failed to flush rx tid hw desc, tid %d status %d\n",
584 			    rx_tid->tid, status);
585 
586 	dma_unmap_single(dp->ab->dev, rx_tid->paddr, rx_tid->size,
587 			 DMA_BIDIRECTIONAL);
588 	kfree(rx_tid->vaddr);
589 }
590 
591 static void ath11k_dp_reo_cache_flush(struct ath11k_base *ab,
592 				      struct dp_rx_tid *rx_tid)
593 {
594 	struct ath11k_hal_reo_cmd cmd = {0};
595 	unsigned long tot_desc_sz, desc_sz;
596 	int ret;
597 
598 	tot_desc_sz = rx_tid->size;
599 	desc_sz = ath11k_hal_reo_qdesc_size(0, HAL_DESC_REO_NON_QOS_TID);
600 
601 	while (tot_desc_sz > desc_sz) {
602 		tot_desc_sz -= desc_sz;
603 		cmd.addr_lo = lower_32_bits(rx_tid->paddr + tot_desc_sz);
604 		cmd.addr_hi = upper_32_bits(rx_tid->paddr);
605 		ret = ath11k_dp_tx_send_reo_cmd(ab, rx_tid,
606 						HAL_REO_CMD_FLUSH_CACHE, &cmd,
607 						NULL);
608 		if (ret)
609 			ath11k_warn(ab,
610 				    "failed to send HAL_REO_CMD_FLUSH_CACHE, tid %d (%d)\n",
611 				    rx_tid->tid, ret);
612 	}
613 
614 	memset(&cmd, 0, sizeof(cmd));
615 	cmd.addr_lo = lower_32_bits(rx_tid->paddr);
616 	cmd.addr_hi = upper_32_bits(rx_tid->paddr);
617 	cmd.flag |= HAL_REO_CMD_FLG_NEED_STATUS;
618 	ret = ath11k_dp_tx_send_reo_cmd(ab, rx_tid,
619 					HAL_REO_CMD_FLUSH_CACHE,
620 					&cmd, ath11k_dp_reo_cmd_free);
621 	if (ret) {
622 		ath11k_err(ab, "failed to send HAL_REO_CMD_FLUSH_CACHE cmd, tid %d (%d)\n",
623 			   rx_tid->tid, ret);
624 		dma_unmap_single(ab->dev, rx_tid->paddr, rx_tid->size,
625 				 DMA_BIDIRECTIONAL);
626 		kfree(rx_tid->vaddr);
627 	}
628 }
629 
630 static void ath11k_dp_rx_tid_del_func(struct ath11k_dp *dp, void *ctx,
631 				      enum hal_reo_cmd_status status)
632 {
633 	struct ath11k_base *ab = dp->ab;
634 	struct dp_rx_tid *rx_tid = ctx;
635 	struct dp_reo_cache_flush_elem *elem, *tmp;
636 
637 	if (status == HAL_REO_CMD_DRAIN) {
638 		goto free_desc;
639 	} else if (status != HAL_REO_CMD_SUCCESS) {
640 		/* Shouldn't happen! Cleanup in case of other failure? */
641 		ath11k_warn(ab, "failed to delete rx tid %d hw descriptor %d\n",
642 			    rx_tid->tid, status);
643 		return;
644 	}
645 
646 	elem = kzalloc(sizeof(*elem), GFP_ATOMIC);
647 	if (!elem)
648 		goto free_desc;
649 
650 	elem->ts = jiffies;
651 	memcpy(&elem->data, rx_tid, sizeof(*rx_tid));
652 
653 	spin_lock_bh(&dp->reo_cmd_lock);
654 	list_add_tail(&elem->list, &dp->reo_cmd_cache_flush_list);
655 	dp->reo_cmd_cache_flush_count++;
656 	spin_unlock_bh(&dp->reo_cmd_lock);
657 
658 	/* Flush and invalidate aged REO desc from HW cache */
659 	spin_lock_bh(&dp->reo_cmd_lock);
660 	list_for_each_entry_safe(elem, tmp, &dp->reo_cmd_cache_flush_list,
661 				 list) {
662 		if (dp->reo_cmd_cache_flush_count > DP_REO_DESC_FREE_THRESHOLD ||
663 		    time_after(jiffies, elem->ts +
664 			       msecs_to_jiffies(DP_REO_DESC_FREE_TIMEOUT_MS))) {
665 			list_del(&elem->list);
666 			dp->reo_cmd_cache_flush_count--;
667 			spin_unlock_bh(&dp->reo_cmd_lock);
668 
669 			ath11k_dp_reo_cache_flush(ab, &elem->data);
670 			kfree(elem);
671 			spin_lock_bh(&dp->reo_cmd_lock);
672 		}
673 	}
674 	spin_unlock_bh(&dp->reo_cmd_lock);
675 
676 	return;
677 free_desc:
678 	dma_unmap_single(ab->dev, rx_tid->paddr, rx_tid->size,
679 			 DMA_BIDIRECTIONAL);
680 	kfree(rx_tid->vaddr);
681 }
682 
683 void ath11k_peer_rx_tid_delete(struct ath11k *ar,
684 			       struct ath11k_peer *peer, u8 tid)
685 {
686 	struct ath11k_hal_reo_cmd cmd = {0};
687 	struct dp_rx_tid *rx_tid = &peer->rx_tid[tid];
688 	int ret;
689 
690 	if (!rx_tid->active)
691 		return;
692 
693 	cmd.flag = HAL_REO_CMD_FLG_NEED_STATUS;
694 	cmd.addr_lo = lower_32_bits(rx_tid->paddr);
695 	cmd.addr_hi = upper_32_bits(rx_tid->paddr);
696 	cmd.upd0 |= HAL_REO_CMD_UPD0_VLD;
697 	ret = ath11k_dp_tx_send_reo_cmd(ar->ab, rx_tid,
698 					HAL_REO_CMD_UPDATE_RX_QUEUE, &cmd,
699 					ath11k_dp_rx_tid_del_func);
700 	if (ret) {
701 		ath11k_err(ar->ab, "failed to send HAL_REO_CMD_UPDATE_RX_QUEUE cmd, tid %d (%d)\n",
702 			   tid, ret);
703 		dma_unmap_single(ar->ab->dev, rx_tid->paddr, rx_tid->size,
704 				 DMA_BIDIRECTIONAL);
705 		kfree(rx_tid->vaddr);
706 	}
707 
708 	rx_tid->active = false;
709 }
710 
711 static int ath11k_dp_rx_link_desc_return(struct ath11k_base *ab,
712 					 u32 *link_desc,
713 					 enum hal_wbm_rel_bm_act action)
714 {
715 	struct ath11k_dp *dp = &ab->dp;
716 	struct hal_srng *srng;
717 	u32 *desc;
718 	int ret = 0;
719 
720 	srng = &ab->hal.srng_list[dp->wbm_desc_rel_ring.ring_id];
721 
722 	spin_lock_bh(&srng->lock);
723 
724 	ath11k_hal_srng_access_begin(ab, srng);
725 
726 	desc = ath11k_hal_srng_src_get_next_entry(ab, srng);
727 	if (!desc) {
728 		ret = -ENOBUFS;
729 		goto exit;
730 	}
731 
732 	ath11k_hal_rx_msdu_link_desc_set(ab, (void *)desc, (void *)link_desc,
733 					 action);
734 
735 exit:
736 	ath11k_hal_srng_access_end(ab, srng);
737 
738 	spin_unlock_bh(&srng->lock);
739 
740 	return ret;
741 }
742 
743 static void ath11k_dp_rx_frags_cleanup(struct dp_rx_tid *rx_tid, bool rel_link_desc)
744 {
745 	struct ath11k_base *ab = rx_tid->ab;
746 
747 	lockdep_assert_held(&ab->base_lock);
748 
749 	if (rx_tid->dst_ring_desc) {
750 		if (rel_link_desc)
751 			ath11k_dp_rx_link_desc_return(ab, (u32 *)rx_tid->dst_ring_desc,
752 						      HAL_WBM_REL_BM_ACT_PUT_IN_IDLE);
753 		kfree(rx_tid->dst_ring_desc);
754 		rx_tid->dst_ring_desc = NULL;
755 	}
756 
757 	rx_tid->cur_sn = 0;
758 	rx_tid->last_frag_no = 0;
759 	rx_tid->rx_frag_bitmap = 0;
760 	__skb_queue_purge(&rx_tid->rx_frags);
761 }
762 
763 void ath11k_peer_rx_tid_cleanup(struct ath11k *ar, struct ath11k_peer *peer)
764 {
765 	struct dp_rx_tid *rx_tid;
766 	int i;
767 
768 	lockdep_assert_held(&ar->ab->base_lock);
769 
770 	for (i = 0; i <= IEEE80211_NUM_TIDS; i++) {
771 		rx_tid = &peer->rx_tid[i];
772 
773 		ath11k_peer_rx_tid_delete(ar, peer, i);
774 		ath11k_dp_rx_frags_cleanup(rx_tid, true);
775 
776 		spin_unlock_bh(&ar->ab->base_lock);
777 		del_timer_sync(&rx_tid->frag_timer);
778 		spin_lock_bh(&ar->ab->base_lock);
779 	}
780 }
781 
782 static int ath11k_peer_rx_tid_reo_update(struct ath11k *ar,
783 					 struct ath11k_peer *peer,
784 					 struct dp_rx_tid *rx_tid,
785 					 u32 ba_win_sz, u16 ssn,
786 					 bool update_ssn)
787 {
788 	struct ath11k_hal_reo_cmd cmd = {0};
789 	int ret;
790 
791 	cmd.addr_lo = lower_32_bits(rx_tid->paddr);
792 	cmd.addr_hi = upper_32_bits(rx_tid->paddr);
793 	cmd.flag = HAL_REO_CMD_FLG_NEED_STATUS;
794 	cmd.upd0 = HAL_REO_CMD_UPD0_BA_WINDOW_SIZE;
795 	cmd.ba_window_size = ba_win_sz;
796 
797 	if (update_ssn) {
798 		cmd.upd0 |= HAL_REO_CMD_UPD0_SSN;
799 		cmd.upd2 = FIELD_PREP(HAL_REO_CMD_UPD2_SSN, ssn);
800 	}
801 
802 	ret = ath11k_dp_tx_send_reo_cmd(ar->ab, rx_tid,
803 					HAL_REO_CMD_UPDATE_RX_QUEUE, &cmd,
804 					NULL);
805 	if (ret) {
806 		ath11k_warn(ar->ab, "failed to update rx tid queue, tid %d (%d)\n",
807 			    rx_tid->tid, ret);
808 		return ret;
809 	}
810 
811 	rx_tid->ba_win_sz = ba_win_sz;
812 
813 	return 0;
814 }
815 
816 static void ath11k_dp_rx_tid_mem_free(struct ath11k_base *ab,
817 				      const u8 *peer_mac, int vdev_id, u8 tid)
818 {
819 	struct ath11k_peer *peer;
820 	struct dp_rx_tid *rx_tid;
821 
822 	spin_lock_bh(&ab->base_lock);
823 
824 	peer = ath11k_peer_find(ab, vdev_id, peer_mac);
825 	if (!peer) {
826 		ath11k_warn(ab, "failed to find the peer to free up rx tid mem\n");
827 		goto unlock_exit;
828 	}
829 
830 	rx_tid = &peer->rx_tid[tid];
831 	if (!rx_tid->active)
832 		goto unlock_exit;
833 
834 	dma_unmap_single(ab->dev, rx_tid->paddr, rx_tid->size,
835 			 DMA_BIDIRECTIONAL);
836 	kfree(rx_tid->vaddr);
837 
838 	rx_tid->active = false;
839 
840 unlock_exit:
841 	spin_unlock_bh(&ab->base_lock);
842 }
843 
844 int ath11k_peer_rx_tid_setup(struct ath11k *ar, const u8 *peer_mac, int vdev_id,
845 			     u8 tid, u32 ba_win_sz, u16 ssn,
846 			     enum hal_pn_type pn_type)
847 {
848 	struct ath11k_base *ab = ar->ab;
849 	struct ath11k_peer *peer;
850 	struct dp_rx_tid *rx_tid;
851 	u32 hw_desc_sz;
852 	u32 *addr_aligned;
853 	void *vaddr;
854 	dma_addr_t paddr;
855 	int ret;
856 
857 	spin_lock_bh(&ab->base_lock);
858 
859 	peer = ath11k_peer_find(ab, vdev_id, peer_mac);
860 	if (!peer) {
861 		ath11k_warn(ab, "failed to find the peer to set up rx tid\n");
862 		spin_unlock_bh(&ab->base_lock);
863 		return -ENOENT;
864 	}
865 
866 	rx_tid = &peer->rx_tid[tid];
867 	/* Update the tid queue if it is already setup */
868 	if (rx_tid->active) {
869 		paddr = rx_tid->paddr;
870 		ret = ath11k_peer_rx_tid_reo_update(ar, peer, rx_tid,
871 						    ba_win_sz, ssn, true);
872 		spin_unlock_bh(&ab->base_lock);
873 		if (ret) {
874 			ath11k_warn(ab, "failed to update reo for rx tid %d\n", tid);
875 			return ret;
876 		}
877 
878 		ret = ath11k_wmi_peer_rx_reorder_queue_setup(ar, vdev_id,
879 							     peer_mac, paddr,
880 							     tid, 1, ba_win_sz);
881 		if (ret)
882 			ath11k_warn(ab, "failed to send wmi command to update rx reorder queue, tid :%d (%d)\n",
883 				    tid, ret);
884 		return ret;
885 	}
886 
887 	rx_tid->tid = tid;
888 
889 	rx_tid->ba_win_sz = ba_win_sz;
890 
891 	/* TODO: Optimize the memory allocation for qos tid based on the
892 	 * the actual BA window size in REO tid update path.
893 	 */
894 	if (tid == HAL_DESC_REO_NON_QOS_TID)
895 		hw_desc_sz = ath11k_hal_reo_qdesc_size(ba_win_sz, tid);
896 	else
897 		hw_desc_sz = ath11k_hal_reo_qdesc_size(DP_BA_WIN_SZ_MAX, tid);
898 
899 	vaddr = kzalloc(hw_desc_sz + HAL_LINK_DESC_ALIGN - 1, GFP_ATOMIC);
900 	if (!vaddr) {
901 		spin_unlock_bh(&ab->base_lock);
902 		return -ENOMEM;
903 	}
904 
905 	addr_aligned = PTR_ALIGN(vaddr, HAL_LINK_DESC_ALIGN);
906 
907 	ath11k_hal_reo_qdesc_setup(addr_aligned, tid, ba_win_sz,
908 				   ssn, pn_type);
909 
910 	paddr = dma_map_single(ab->dev, addr_aligned, hw_desc_sz,
911 			       DMA_BIDIRECTIONAL);
912 
913 	ret = dma_mapping_error(ab->dev, paddr);
914 	if (ret) {
915 		spin_unlock_bh(&ab->base_lock);
916 		goto err_mem_free;
917 	}
918 
919 	rx_tid->vaddr = vaddr;
920 	rx_tid->paddr = paddr;
921 	rx_tid->size = hw_desc_sz;
922 	rx_tid->active = true;
923 
924 	spin_unlock_bh(&ab->base_lock);
925 
926 	ret = ath11k_wmi_peer_rx_reorder_queue_setup(ar, vdev_id, peer_mac,
927 						     paddr, tid, 1, ba_win_sz);
928 	if (ret) {
929 		ath11k_warn(ar->ab, "failed to setup rx reorder queue, tid :%d (%d)\n",
930 			    tid, ret);
931 		ath11k_dp_rx_tid_mem_free(ab, peer_mac, vdev_id, tid);
932 	}
933 
934 	return ret;
935 
936 err_mem_free:
937 	kfree(vaddr);
938 
939 	return ret;
940 }
941 
942 int ath11k_dp_rx_ampdu_start(struct ath11k *ar,
943 			     struct ieee80211_ampdu_params *params)
944 {
945 	struct ath11k_base *ab = ar->ab;
946 	struct ath11k_sta *arsta = (void *)params->sta->drv_priv;
947 	int vdev_id = arsta->arvif->vdev_id;
948 	int ret;
949 
950 	ret = ath11k_peer_rx_tid_setup(ar, params->sta->addr, vdev_id,
951 				       params->tid, params->buf_size,
952 				       params->ssn, arsta->pn_type);
953 	if (ret)
954 		ath11k_warn(ab, "failed to setup rx tid %d\n", ret);
955 
956 	return ret;
957 }
958 
959 int ath11k_dp_rx_ampdu_stop(struct ath11k *ar,
960 			    struct ieee80211_ampdu_params *params)
961 {
962 	struct ath11k_base *ab = ar->ab;
963 	struct ath11k_peer *peer;
964 	struct ath11k_sta *arsta = (void *)params->sta->drv_priv;
965 	int vdev_id = arsta->arvif->vdev_id;
966 	dma_addr_t paddr;
967 	bool active;
968 	int ret;
969 
970 	spin_lock_bh(&ab->base_lock);
971 
972 	peer = ath11k_peer_find(ab, vdev_id, params->sta->addr);
973 	if (!peer) {
974 		ath11k_warn(ab, "failed to find the peer to stop rx aggregation\n");
975 		spin_unlock_bh(&ab->base_lock);
976 		return -ENOENT;
977 	}
978 
979 	paddr = peer->rx_tid[params->tid].paddr;
980 	active = peer->rx_tid[params->tid].active;
981 
982 	if (!active) {
983 		spin_unlock_bh(&ab->base_lock);
984 		return 0;
985 	}
986 
987 	ret = ath11k_peer_rx_tid_reo_update(ar, peer, peer->rx_tid, 1, 0, false);
988 	spin_unlock_bh(&ab->base_lock);
989 	if (ret) {
990 		ath11k_warn(ab, "failed to update reo for rx tid %d: %d\n",
991 			    params->tid, ret);
992 		return ret;
993 	}
994 
995 	ret = ath11k_wmi_peer_rx_reorder_queue_setup(ar, vdev_id,
996 						     params->sta->addr, paddr,
997 						     params->tid, 1, 1);
998 	if (ret)
999 		ath11k_warn(ab, "failed to send wmi to delete rx tid %d\n",
1000 			    ret);
1001 
1002 	return ret;
1003 }
1004 
1005 int ath11k_dp_peer_rx_pn_replay_config(struct ath11k_vif *arvif,
1006 				       const u8 *peer_addr,
1007 				       enum set_key_cmd key_cmd,
1008 				       struct ieee80211_key_conf *key)
1009 {
1010 	struct ath11k *ar = arvif->ar;
1011 	struct ath11k_base *ab = ar->ab;
1012 	struct ath11k_hal_reo_cmd cmd = {0};
1013 	struct ath11k_peer *peer;
1014 	struct dp_rx_tid *rx_tid;
1015 	u8 tid;
1016 	int ret = 0;
1017 
1018 	/* NOTE: Enable PN/TSC replay check offload only for unicast frames.
1019 	 * We use mac80211 PN/TSC replay check functionality for bcast/mcast
1020 	 * for now.
1021 	 */
1022 	if (!(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
1023 		return 0;
1024 
1025 	cmd.flag |= HAL_REO_CMD_FLG_NEED_STATUS;
1026 	cmd.upd0 |= HAL_REO_CMD_UPD0_PN |
1027 		    HAL_REO_CMD_UPD0_PN_SIZE |
1028 		    HAL_REO_CMD_UPD0_PN_VALID |
1029 		    HAL_REO_CMD_UPD0_PN_CHECK |
1030 		    HAL_REO_CMD_UPD0_SVLD;
1031 
1032 	switch (key->cipher) {
1033 	case WLAN_CIPHER_SUITE_TKIP:
1034 	case WLAN_CIPHER_SUITE_CCMP:
1035 	case WLAN_CIPHER_SUITE_CCMP_256:
1036 	case WLAN_CIPHER_SUITE_GCMP:
1037 	case WLAN_CIPHER_SUITE_GCMP_256:
1038 		if (key_cmd == SET_KEY) {
1039 			cmd.upd1 |= HAL_REO_CMD_UPD1_PN_CHECK;
1040 			cmd.pn_size = 48;
1041 		}
1042 		break;
1043 	default:
1044 		break;
1045 	}
1046 
1047 	spin_lock_bh(&ab->base_lock);
1048 
1049 	peer = ath11k_peer_find(ab, arvif->vdev_id, peer_addr);
1050 	if (!peer) {
1051 		ath11k_warn(ab, "failed to find the peer to configure pn replay detection\n");
1052 		spin_unlock_bh(&ab->base_lock);
1053 		return -ENOENT;
1054 	}
1055 
1056 	for (tid = 0; tid <= IEEE80211_NUM_TIDS; tid++) {
1057 		rx_tid = &peer->rx_tid[tid];
1058 		if (!rx_tid->active)
1059 			continue;
1060 		cmd.addr_lo = lower_32_bits(rx_tid->paddr);
1061 		cmd.addr_hi = upper_32_bits(rx_tid->paddr);
1062 		ret = ath11k_dp_tx_send_reo_cmd(ab, rx_tid,
1063 						HAL_REO_CMD_UPDATE_RX_QUEUE,
1064 						&cmd, NULL);
1065 		if (ret) {
1066 			ath11k_warn(ab, "failed to configure rx tid %d queue for pn replay detection %d\n",
1067 				    tid, ret);
1068 			break;
1069 		}
1070 	}
1071 
1072 	spin_unlock_bh(&ar->ab->base_lock);
1073 
1074 	return ret;
1075 }
1076 
1077 static inline int ath11k_get_ppdu_user_index(struct htt_ppdu_stats *ppdu_stats,
1078 					     u16 peer_id)
1079 {
1080 	int i;
1081 
1082 	for (i = 0; i < HTT_PPDU_STATS_MAX_USERS - 1; i++) {
1083 		if (ppdu_stats->user_stats[i].is_valid_peer_id) {
1084 			if (peer_id == ppdu_stats->user_stats[i].peer_id)
1085 				return i;
1086 		} else {
1087 			return i;
1088 		}
1089 	}
1090 
1091 	return -EINVAL;
1092 }
1093 
1094 static int ath11k_htt_tlv_ppdu_stats_parse(struct ath11k_base *ab,
1095 					   u16 tag, u16 len, const void *ptr,
1096 					   void *data)
1097 {
1098 	struct htt_ppdu_stats_info *ppdu_info;
1099 	struct htt_ppdu_user_stats *user_stats;
1100 	int cur_user;
1101 	u16 peer_id;
1102 
1103 	ppdu_info = (struct htt_ppdu_stats_info *)data;
1104 
1105 	switch (tag) {
1106 	case HTT_PPDU_STATS_TAG_COMMON:
1107 		if (len < sizeof(struct htt_ppdu_stats_common)) {
1108 			ath11k_warn(ab, "Invalid len %d for the tag 0x%x\n",
1109 				    len, tag);
1110 			return -EINVAL;
1111 		}
1112 		memcpy((void *)&ppdu_info->ppdu_stats.common, ptr,
1113 		       sizeof(struct htt_ppdu_stats_common));
1114 		break;
1115 	case HTT_PPDU_STATS_TAG_USR_RATE:
1116 		if (len < sizeof(struct htt_ppdu_stats_user_rate)) {
1117 			ath11k_warn(ab, "Invalid len %d for the tag 0x%x\n",
1118 				    len, tag);
1119 			return -EINVAL;
1120 		}
1121 
1122 		peer_id = ((struct htt_ppdu_stats_user_rate *)ptr)->sw_peer_id;
1123 		cur_user = ath11k_get_ppdu_user_index(&ppdu_info->ppdu_stats,
1124 						      peer_id);
1125 		if (cur_user < 0)
1126 			return -EINVAL;
1127 		user_stats = &ppdu_info->ppdu_stats.user_stats[cur_user];
1128 		user_stats->peer_id = peer_id;
1129 		user_stats->is_valid_peer_id = true;
1130 		memcpy((void *)&user_stats->rate, ptr,
1131 		       sizeof(struct htt_ppdu_stats_user_rate));
1132 		user_stats->tlv_flags |= BIT(tag);
1133 		break;
1134 	case HTT_PPDU_STATS_TAG_USR_COMPLTN_COMMON:
1135 		if (len < sizeof(struct htt_ppdu_stats_usr_cmpltn_cmn)) {
1136 			ath11k_warn(ab, "Invalid len %d for the tag 0x%x\n",
1137 				    len, tag);
1138 			return -EINVAL;
1139 		}
1140 
1141 		peer_id = ((struct htt_ppdu_stats_usr_cmpltn_cmn *)ptr)->sw_peer_id;
1142 		cur_user = ath11k_get_ppdu_user_index(&ppdu_info->ppdu_stats,
1143 						      peer_id);
1144 		if (cur_user < 0)
1145 			return -EINVAL;
1146 		user_stats = &ppdu_info->ppdu_stats.user_stats[cur_user];
1147 		user_stats->peer_id = peer_id;
1148 		user_stats->is_valid_peer_id = true;
1149 		memcpy((void *)&user_stats->cmpltn_cmn, ptr,
1150 		       sizeof(struct htt_ppdu_stats_usr_cmpltn_cmn));
1151 		user_stats->tlv_flags |= BIT(tag);
1152 		break;
1153 	case HTT_PPDU_STATS_TAG_USR_COMPLTN_ACK_BA_STATUS:
1154 		if (len <
1155 		    sizeof(struct htt_ppdu_stats_usr_cmpltn_ack_ba_status)) {
1156 			ath11k_warn(ab, "Invalid len %d for the tag 0x%x\n",
1157 				    len, tag);
1158 			return -EINVAL;
1159 		}
1160 
1161 		peer_id =
1162 		((struct htt_ppdu_stats_usr_cmpltn_ack_ba_status *)ptr)->sw_peer_id;
1163 		cur_user = ath11k_get_ppdu_user_index(&ppdu_info->ppdu_stats,
1164 						      peer_id);
1165 		if (cur_user < 0)
1166 			return -EINVAL;
1167 		user_stats = &ppdu_info->ppdu_stats.user_stats[cur_user];
1168 		user_stats->peer_id = peer_id;
1169 		user_stats->is_valid_peer_id = true;
1170 		memcpy((void *)&user_stats->ack_ba, ptr,
1171 		       sizeof(struct htt_ppdu_stats_usr_cmpltn_ack_ba_status));
1172 		user_stats->tlv_flags |= BIT(tag);
1173 		break;
1174 	}
1175 	return 0;
1176 }
1177 
1178 int ath11k_dp_htt_tlv_iter(struct ath11k_base *ab, const void *ptr, size_t len,
1179 			   int (*iter)(struct ath11k_base *ar, u16 tag, u16 len,
1180 				       const void *ptr, void *data),
1181 			   void *data)
1182 {
1183 	const struct htt_tlv *tlv;
1184 	const void *begin = ptr;
1185 	u16 tlv_tag, tlv_len;
1186 	int ret = -EINVAL;
1187 
1188 	while (len > 0) {
1189 		if (len < sizeof(*tlv)) {
1190 			ath11k_err(ab, "htt tlv parse failure at byte %zd (%zu bytes left, %zu expected)\n",
1191 				   ptr - begin, len, sizeof(*tlv));
1192 			return -EINVAL;
1193 		}
1194 		tlv = (struct htt_tlv *)ptr;
1195 		tlv_tag = FIELD_GET(HTT_TLV_TAG, tlv->header);
1196 		tlv_len = FIELD_GET(HTT_TLV_LEN, tlv->header);
1197 		ptr += sizeof(*tlv);
1198 		len -= sizeof(*tlv);
1199 
1200 		if (tlv_len > len) {
1201 			ath11k_err(ab, "htt tlv parse failure of tag %hhu at byte %zd (%zu bytes left, %hhu expected)\n",
1202 				   tlv_tag, ptr - begin, len, tlv_len);
1203 			return -EINVAL;
1204 		}
1205 		ret = iter(ab, tlv_tag, tlv_len, ptr, data);
1206 		if (ret == -ENOMEM)
1207 			return ret;
1208 
1209 		ptr += tlv_len;
1210 		len -= tlv_len;
1211 	}
1212 	return 0;
1213 }
1214 
1215 static inline u32 ath11k_he_gi_to_nl80211_he_gi(u8 sgi)
1216 {
1217 	u32 ret = 0;
1218 
1219 	switch (sgi) {
1220 	case RX_MSDU_START_SGI_0_8_US:
1221 		ret = NL80211_RATE_INFO_HE_GI_0_8;
1222 		break;
1223 	case RX_MSDU_START_SGI_1_6_US:
1224 		ret = NL80211_RATE_INFO_HE_GI_1_6;
1225 		break;
1226 	case RX_MSDU_START_SGI_3_2_US:
1227 		ret = NL80211_RATE_INFO_HE_GI_3_2;
1228 		break;
1229 	}
1230 
1231 	return ret;
1232 }
1233 
1234 static void
1235 ath11k_update_per_peer_tx_stats(struct ath11k *ar,
1236 				struct htt_ppdu_stats *ppdu_stats, u8 user)
1237 {
1238 	struct ath11k_base *ab = ar->ab;
1239 	struct ath11k_peer *peer;
1240 	struct ieee80211_sta *sta;
1241 	struct ath11k_sta *arsta;
1242 	struct htt_ppdu_stats_user_rate *user_rate;
1243 	struct ath11k_per_peer_tx_stats *peer_stats = &ar->peer_tx_stats;
1244 	struct htt_ppdu_user_stats *usr_stats = &ppdu_stats->user_stats[user];
1245 	struct htt_ppdu_stats_common *common = &ppdu_stats->common;
1246 	int ret;
1247 	u8 flags, mcs, nss, bw, sgi, dcm, rate_idx = 0;
1248 	u32 succ_bytes = 0;
1249 	u16 rate = 0, succ_pkts = 0;
1250 	u32 tx_duration = 0;
1251 	u8 tid = HTT_PPDU_STATS_NON_QOS_TID;
1252 	bool is_ampdu = false;
1253 
1254 	if (!usr_stats)
1255 		return;
1256 
1257 	if (!(usr_stats->tlv_flags & BIT(HTT_PPDU_STATS_TAG_USR_RATE)))
1258 		return;
1259 
1260 	if (usr_stats->tlv_flags & BIT(HTT_PPDU_STATS_TAG_USR_COMPLTN_COMMON))
1261 		is_ampdu =
1262 			HTT_USR_CMPLTN_IS_AMPDU(usr_stats->cmpltn_cmn.flags);
1263 
1264 	if (usr_stats->tlv_flags &
1265 	    BIT(HTT_PPDU_STATS_TAG_USR_COMPLTN_ACK_BA_STATUS)) {
1266 		succ_bytes = usr_stats->ack_ba.success_bytes;
1267 		succ_pkts = FIELD_GET(HTT_PPDU_STATS_ACK_BA_INFO_NUM_MSDU_M,
1268 				      usr_stats->ack_ba.info);
1269 		tid = FIELD_GET(HTT_PPDU_STATS_ACK_BA_INFO_TID_NUM,
1270 				usr_stats->ack_ba.info);
1271 	}
1272 
1273 	if (common->fes_duration_us)
1274 		tx_duration = common->fes_duration_us;
1275 
1276 	user_rate = &usr_stats->rate;
1277 	flags = HTT_USR_RATE_PREAMBLE(user_rate->rate_flags);
1278 	bw = HTT_USR_RATE_BW(user_rate->rate_flags) - 2;
1279 	nss = HTT_USR_RATE_NSS(user_rate->rate_flags) + 1;
1280 	mcs = HTT_USR_RATE_MCS(user_rate->rate_flags);
1281 	sgi = HTT_USR_RATE_GI(user_rate->rate_flags);
1282 	dcm = HTT_USR_RATE_DCM(user_rate->rate_flags);
1283 
1284 	/* Note: If host configured fixed rates and in some other special
1285 	 * cases, the broadcast/management frames are sent in different rates.
1286 	 * Firmware rate's control to be skipped for this?
1287 	 */
1288 
1289 	if (flags == WMI_RATE_PREAMBLE_HE && mcs > 11) {
1290 		ath11k_warn(ab, "Invalid HE mcs %hhd peer stats",  mcs);
1291 		return;
1292 	}
1293 
1294 	if (flags == WMI_RATE_PREAMBLE_HE && mcs > ATH11K_HE_MCS_MAX) {
1295 		ath11k_warn(ab, "Invalid HE mcs %hhd peer stats",  mcs);
1296 		return;
1297 	}
1298 
1299 	if (flags == WMI_RATE_PREAMBLE_VHT && mcs > ATH11K_VHT_MCS_MAX) {
1300 		ath11k_warn(ab, "Invalid VHT mcs %hhd peer stats",  mcs);
1301 		return;
1302 	}
1303 
1304 	if (flags == WMI_RATE_PREAMBLE_HT && (mcs > ATH11K_HT_MCS_MAX || nss < 1)) {
1305 		ath11k_warn(ab, "Invalid HT mcs %hhd nss %hhd peer stats",
1306 			    mcs, nss);
1307 		return;
1308 	}
1309 
1310 	if (flags == WMI_RATE_PREAMBLE_CCK || flags == WMI_RATE_PREAMBLE_OFDM) {
1311 		ret = ath11k_mac_hw_ratecode_to_legacy_rate(mcs,
1312 							    flags,
1313 							    &rate_idx,
1314 							    &rate);
1315 		if (ret < 0)
1316 			return;
1317 	}
1318 
1319 	rcu_read_lock();
1320 	spin_lock_bh(&ab->base_lock);
1321 	peer = ath11k_peer_find_by_id(ab, usr_stats->peer_id);
1322 
1323 	if (!peer || !peer->sta) {
1324 		spin_unlock_bh(&ab->base_lock);
1325 		rcu_read_unlock();
1326 		return;
1327 	}
1328 
1329 	sta = peer->sta;
1330 	arsta = (struct ath11k_sta *)sta->drv_priv;
1331 
1332 	memset(&arsta->txrate, 0, sizeof(arsta->txrate));
1333 
1334 	switch (flags) {
1335 	case WMI_RATE_PREAMBLE_OFDM:
1336 		arsta->txrate.legacy = rate;
1337 		break;
1338 	case WMI_RATE_PREAMBLE_CCK:
1339 		arsta->txrate.legacy = rate;
1340 		break;
1341 	case WMI_RATE_PREAMBLE_HT:
1342 		arsta->txrate.mcs = mcs + 8 * (nss - 1);
1343 		arsta->txrate.flags = RATE_INFO_FLAGS_MCS;
1344 		if (sgi)
1345 			arsta->txrate.flags |= RATE_INFO_FLAGS_SHORT_GI;
1346 		break;
1347 	case WMI_RATE_PREAMBLE_VHT:
1348 		arsta->txrate.mcs = mcs;
1349 		arsta->txrate.flags = RATE_INFO_FLAGS_VHT_MCS;
1350 		if (sgi)
1351 			arsta->txrate.flags |= RATE_INFO_FLAGS_SHORT_GI;
1352 		break;
1353 	case WMI_RATE_PREAMBLE_HE:
1354 		arsta->txrate.mcs = mcs;
1355 		arsta->txrate.flags = RATE_INFO_FLAGS_HE_MCS;
1356 		arsta->txrate.he_dcm = dcm;
1357 		arsta->txrate.he_gi = ath11k_he_gi_to_nl80211_he_gi(sgi);
1358 		arsta->txrate.he_ru_alloc = ath11k_he_ru_tones_to_nl80211_he_ru_alloc(
1359 						(user_rate->ru_end -
1360 						 user_rate->ru_start) + 1);
1361 		break;
1362 	}
1363 
1364 	arsta->txrate.nss = nss;
1365 	arsta->txrate.bw = ath11k_mac_bw_to_mac80211_bw(bw);
1366 	arsta->tx_duration += tx_duration;
1367 	memcpy(&arsta->last_txrate, &arsta->txrate, sizeof(struct rate_info));
1368 
1369 	/* PPDU stats reported for mgmt packet doesn't have valid tx bytes.
1370 	 * So skip peer stats update for mgmt packets.
1371 	 */
1372 	if (tid < HTT_PPDU_STATS_NON_QOS_TID) {
1373 		memset(peer_stats, 0, sizeof(*peer_stats));
1374 		peer_stats->succ_pkts = succ_pkts;
1375 		peer_stats->succ_bytes = succ_bytes;
1376 		peer_stats->is_ampdu = is_ampdu;
1377 		peer_stats->duration = tx_duration;
1378 		peer_stats->ba_fails =
1379 			HTT_USR_CMPLTN_LONG_RETRY(usr_stats->cmpltn_cmn.flags) +
1380 			HTT_USR_CMPLTN_SHORT_RETRY(usr_stats->cmpltn_cmn.flags);
1381 
1382 		if (ath11k_debug_is_extd_tx_stats_enabled(ar))
1383 			ath11k_accumulate_per_peer_tx_stats(arsta,
1384 							    peer_stats, rate_idx);
1385 	}
1386 
1387 	spin_unlock_bh(&ab->base_lock);
1388 	rcu_read_unlock();
1389 }
1390 
1391 static void ath11k_htt_update_ppdu_stats(struct ath11k *ar,
1392 					 struct htt_ppdu_stats *ppdu_stats)
1393 {
1394 	u8 user;
1395 
1396 	for (user = 0; user < HTT_PPDU_STATS_MAX_USERS - 1; user++)
1397 		ath11k_update_per_peer_tx_stats(ar, ppdu_stats, user);
1398 }
1399 
1400 static
1401 struct htt_ppdu_stats_info *ath11k_dp_htt_get_ppdu_desc(struct ath11k *ar,
1402 							u32 ppdu_id)
1403 {
1404 	struct htt_ppdu_stats_info *ppdu_info;
1405 
1406 	spin_lock_bh(&ar->data_lock);
1407 	if (!list_empty(&ar->ppdu_stats_info)) {
1408 		list_for_each_entry(ppdu_info, &ar->ppdu_stats_info, list) {
1409 			if (ppdu_info->ppdu_id == ppdu_id) {
1410 				spin_unlock_bh(&ar->data_lock);
1411 				return ppdu_info;
1412 			}
1413 		}
1414 
1415 		if (ar->ppdu_stat_list_depth > HTT_PPDU_DESC_MAX_DEPTH) {
1416 			ppdu_info = list_first_entry(&ar->ppdu_stats_info,
1417 						     typeof(*ppdu_info), list);
1418 			list_del(&ppdu_info->list);
1419 			ar->ppdu_stat_list_depth--;
1420 			ath11k_htt_update_ppdu_stats(ar, &ppdu_info->ppdu_stats);
1421 			kfree(ppdu_info);
1422 		}
1423 	}
1424 	spin_unlock_bh(&ar->data_lock);
1425 
1426 	ppdu_info = kzalloc(sizeof(*ppdu_info), GFP_KERNEL);
1427 	if (!ppdu_info)
1428 		return NULL;
1429 
1430 	spin_lock_bh(&ar->data_lock);
1431 	list_add_tail(&ppdu_info->list, &ar->ppdu_stats_info);
1432 	ar->ppdu_stat_list_depth++;
1433 	spin_unlock_bh(&ar->data_lock);
1434 
1435 	return ppdu_info;
1436 }
1437 
1438 static int ath11k_htt_pull_ppdu_stats(struct ath11k_base *ab,
1439 				      struct sk_buff *skb)
1440 {
1441 	struct ath11k_htt_ppdu_stats_msg *msg;
1442 	struct htt_ppdu_stats_info *ppdu_info;
1443 	struct ath11k *ar;
1444 	int ret;
1445 	u8 pdev_id;
1446 	u32 ppdu_id, len;
1447 
1448 	msg = (struct ath11k_htt_ppdu_stats_msg *)skb->data;
1449 	len = FIELD_GET(HTT_T2H_PPDU_STATS_INFO_PAYLOAD_SIZE, msg->info);
1450 	pdev_id = FIELD_GET(HTT_T2H_PPDU_STATS_INFO_PDEV_ID, msg->info);
1451 	ppdu_id = msg->ppdu_id;
1452 
1453 	rcu_read_lock();
1454 	ar = ath11k_mac_get_ar_by_pdev_id(ab, pdev_id);
1455 	if (!ar) {
1456 		ret = -EINVAL;
1457 		goto exit;
1458 	}
1459 
1460 	if (ath11k_debug_is_pktlog_lite_mode_enabled(ar))
1461 		trace_ath11k_htt_ppdu_stats(ar, skb->data, len);
1462 
1463 	ppdu_info = ath11k_dp_htt_get_ppdu_desc(ar, ppdu_id);
1464 	if (!ppdu_info) {
1465 		ret = -EINVAL;
1466 		goto exit;
1467 	}
1468 
1469 	ppdu_info->ppdu_id = ppdu_id;
1470 	ret = ath11k_dp_htt_tlv_iter(ab, msg->data, len,
1471 				     ath11k_htt_tlv_ppdu_stats_parse,
1472 				     (void *)ppdu_info);
1473 	if (ret) {
1474 		ath11k_warn(ab, "Failed to parse tlv %d\n", ret);
1475 		goto exit;
1476 	}
1477 
1478 exit:
1479 	rcu_read_unlock();
1480 
1481 	return ret;
1482 }
1483 
1484 static void ath11k_htt_pktlog(struct ath11k_base *ab, struct sk_buff *skb)
1485 {
1486 	struct htt_pktlog_msg *data = (struct htt_pktlog_msg *)skb->data;
1487 	struct ath_pktlog_hdr *hdr = (struct ath_pktlog_hdr *)data;
1488 	struct ath11k *ar;
1489 	u8 pdev_id;
1490 
1491 	pdev_id = FIELD_GET(HTT_T2H_PPDU_STATS_INFO_PDEV_ID, data->hdr);
1492 	ar = ath11k_mac_get_ar_by_pdev_id(ab, pdev_id);
1493 	if (!ar) {
1494 		ath11k_warn(ab, "invalid pdev id %d on htt pktlog\n", pdev_id);
1495 		return;
1496 	}
1497 
1498 	trace_ath11k_htt_pktlog(ar, data->payload, hdr->size,
1499 				ar->ab->pktlog_defs_checksum);
1500 }
1501 
1502 static void ath11k_htt_backpressure_event_handler(struct ath11k_base *ab,
1503 						  struct sk_buff *skb)
1504 {
1505 	u32 *data = (u32 *)skb->data;
1506 	u8 pdev_id, ring_type, ring_id;
1507 	u16 hp, tp;
1508 	u32 backpressure_time;
1509 
1510 	pdev_id = FIELD_GET(HTT_BACKPRESSURE_EVENT_PDEV_ID_M, *data);
1511 	ring_type = FIELD_GET(HTT_BACKPRESSURE_EVENT_RING_TYPE_M, *data);
1512 	ring_id = FIELD_GET(HTT_BACKPRESSURE_EVENT_RING_ID_M, *data);
1513 	++data;
1514 
1515 	hp = FIELD_GET(HTT_BACKPRESSURE_EVENT_HP_M, *data);
1516 	tp = FIELD_GET(HTT_BACKPRESSURE_EVENT_TP_M, *data);
1517 	++data;
1518 
1519 	backpressure_time = *data;
1520 
1521 	ath11k_dbg(ab, ATH11K_DBG_DP_HTT, "htt backpressure event, pdev %d, ring type %d,ring id %d, hp %d tp %d, backpressure time %d\n",
1522 		   pdev_id, ring_type, ring_id, hp, tp, backpressure_time);
1523 }
1524 
1525 void ath11k_dp_htt_htc_t2h_msg_handler(struct ath11k_base *ab,
1526 				       struct sk_buff *skb)
1527 {
1528 	struct ath11k_dp *dp = &ab->dp;
1529 	struct htt_resp_msg *resp = (struct htt_resp_msg *)skb->data;
1530 	enum htt_t2h_msg_type type = FIELD_GET(HTT_T2H_MSG_TYPE, *(u32 *)resp);
1531 	u16 peer_id;
1532 	u8 vdev_id;
1533 	u8 mac_addr[ETH_ALEN];
1534 	u16 peer_mac_h16;
1535 	u16 ast_hash;
1536 
1537 	ath11k_dbg(ab, ATH11K_DBG_DP_HTT, "dp_htt rx msg type :0x%0x\n", type);
1538 
1539 	switch (type) {
1540 	case HTT_T2H_MSG_TYPE_VERSION_CONF:
1541 		dp->htt_tgt_ver_major = FIELD_GET(HTT_T2H_VERSION_CONF_MAJOR,
1542 						  resp->version_msg.version);
1543 		dp->htt_tgt_ver_minor = FIELD_GET(HTT_T2H_VERSION_CONF_MINOR,
1544 						  resp->version_msg.version);
1545 		complete(&dp->htt_tgt_version_received);
1546 		break;
1547 	case HTT_T2H_MSG_TYPE_PEER_MAP:
1548 		vdev_id = FIELD_GET(HTT_T2H_PEER_MAP_INFO_VDEV_ID,
1549 				    resp->peer_map_ev.info);
1550 		peer_id = FIELD_GET(HTT_T2H_PEER_MAP_INFO_PEER_ID,
1551 				    resp->peer_map_ev.info);
1552 		peer_mac_h16 = FIELD_GET(HTT_T2H_PEER_MAP_INFO1_MAC_ADDR_H16,
1553 					 resp->peer_map_ev.info1);
1554 		ath11k_dp_get_mac_addr(resp->peer_map_ev.mac_addr_l32,
1555 				       peer_mac_h16, mac_addr);
1556 		ast_hash = FIELD_GET(HTT_T2H_PEER_MAP_INFO2_AST_HASH_VAL,
1557 				     resp->peer_map_ev.info2);
1558 		ath11k_peer_map_event(ab, vdev_id, peer_id, mac_addr, ast_hash);
1559 		break;
1560 	case HTT_T2H_MSG_TYPE_PEER_UNMAP:
1561 		peer_id = FIELD_GET(HTT_T2H_PEER_UNMAP_INFO_PEER_ID,
1562 				    resp->peer_unmap_ev.info);
1563 		ath11k_peer_unmap_event(ab, peer_id);
1564 		break;
1565 	case HTT_T2H_MSG_TYPE_PPDU_STATS_IND:
1566 		ath11k_htt_pull_ppdu_stats(ab, skb);
1567 		break;
1568 	case HTT_T2H_MSG_TYPE_EXT_STATS_CONF:
1569 		ath11k_dbg_htt_ext_stats_handler(ab, skb);
1570 		break;
1571 	case HTT_T2H_MSG_TYPE_PKTLOG:
1572 		ath11k_htt_pktlog(ab, skb);
1573 		break;
1574 	case HTT_T2H_MSG_TYPE_BKPRESSURE_EVENT_IND:
1575 		ath11k_htt_backpressure_event_handler(ab, skb);
1576 		break;
1577 	default:
1578 		ath11k_warn(ab, "htt event %d not handled\n", type);
1579 		break;
1580 	}
1581 
1582 	dev_kfree_skb_any(skb);
1583 }
1584 
1585 static int ath11k_dp_rx_msdu_coalesce(struct ath11k *ar,
1586 				      struct sk_buff_head *msdu_list,
1587 				      struct sk_buff *first, struct sk_buff *last,
1588 				      u8 l3pad_bytes, int msdu_len)
1589 {
1590 	struct sk_buff *skb;
1591 	struct ath11k_skb_rxcb *rxcb = ATH11K_SKB_RXCB(first);
1592 	int buf_first_hdr_len, buf_first_len;
1593 	struct hal_rx_desc *ldesc;
1594 	int space_extra;
1595 	int rem_len;
1596 	int buf_len;
1597 
1598 	/* As the msdu is spread across multiple rx buffers,
1599 	 * find the offset to the start of msdu for computing
1600 	 * the length of the msdu in the first buffer.
1601 	 */
1602 	buf_first_hdr_len = HAL_RX_DESC_SIZE + l3pad_bytes;
1603 	buf_first_len = DP_RX_BUFFER_SIZE - buf_first_hdr_len;
1604 
1605 	if (WARN_ON_ONCE(msdu_len <= buf_first_len)) {
1606 		skb_put(first, buf_first_hdr_len + msdu_len);
1607 		skb_pull(first, buf_first_hdr_len);
1608 		return 0;
1609 	}
1610 
1611 	ldesc = (struct hal_rx_desc *)last->data;
1612 	rxcb->is_first_msdu = ath11k_dp_rx_h_msdu_end_first_msdu(ldesc);
1613 	rxcb->is_last_msdu = ath11k_dp_rx_h_msdu_end_last_msdu(ldesc);
1614 
1615 	/* MSDU spans over multiple buffers because the length of the MSDU
1616 	 * exceeds DP_RX_BUFFER_SIZE - HAL_RX_DESC_SIZE. So assume the data
1617 	 * in the first buf is of length DP_RX_BUFFER_SIZE - HAL_RX_DESC_SIZE.
1618 	 */
1619 	skb_put(first, DP_RX_BUFFER_SIZE);
1620 	skb_pull(first, buf_first_hdr_len);
1621 
1622 	/* When an MSDU spread over multiple buffers attention, MSDU_END and
1623 	 * MPDU_END tlvs are valid only in the last buffer. Copy those tlvs.
1624 	 */
1625 	ath11k_dp_rx_desc_end_tlv_copy(rxcb->rx_desc, ldesc);
1626 
1627 	space_extra = msdu_len - (buf_first_len + skb_tailroom(first));
1628 	if (space_extra > 0 &&
1629 	    (pskb_expand_head(first, 0, space_extra, GFP_ATOMIC) < 0)) {
1630 		/* Free up all buffers of the MSDU */
1631 		while ((skb = __skb_dequeue(msdu_list)) != NULL) {
1632 			rxcb = ATH11K_SKB_RXCB(skb);
1633 			if (!rxcb->is_continuation) {
1634 				dev_kfree_skb_any(skb);
1635 				break;
1636 			}
1637 			dev_kfree_skb_any(skb);
1638 		}
1639 		return -ENOMEM;
1640 	}
1641 
1642 	rem_len = msdu_len - buf_first_len;
1643 	while ((skb = __skb_dequeue(msdu_list)) != NULL && rem_len > 0) {
1644 		rxcb = ATH11K_SKB_RXCB(skb);
1645 		if (rxcb->is_continuation)
1646 			buf_len = DP_RX_BUFFER_SIZE - HAL_RX_DESC_SIZE;
1647 		else
1648 			buf_len = rem_len;
1649 
1650 		if (buf_len > (DP_RX_BUFFER_SIZE - HAL_RX_DESC_SIZE)) {
1651 			WARN_ON_ONCE(1);
1652 			dev_kfree_skb_any(skb);
1653 			return -EINVAL;
1654 		}
1655 
1656 		skb_put(skb, buf_len + HAL_RX_DESC_SIZE);
1657 		skb_pull(skb, HAL_RX_DESC_SIZE);
1658 		skb_copy_from_linear_data(skb, skb_put(first, buf_len),
1659 					  buf_len);
1660 		dev_kfree_skb_any(skb);
1661 
1662 		rem_len -= buf_len;
1663 		if (!rxcb->is_continuation)
1664 			break;
1665 	}
1666 
1667 	return 0;
1668 }
1669 
1670 static struct sk_buff *ath11k_dp_rx_get_msdu_last_buf(struct sk_buff_head *msdu_list,
1671 						      struct sk_buff *first)
1672 {
1673 	struct sk_buff *skb;
1674 	struct ath11k_skb_rxcb *rxcb = ATH11K_SKB_RXCB(first);
1675 
1676 	if (!rxcb->is_continuation)
1677 		return first;
1678 
1679 	skb_queue_walk(msdu_list, skb) {
1680 		rxcb = ATH11K_SKB_RXCB(skb);
1681 		if (!rxcb->is_continuation)
1682 			return skb;
1683 	}
1684 
1685 	return NULL;
1686 }
1687 
1688 static void ath11k_dp_rx_h_csum_offload(struct sk_buff *msdu)
1689 {
1690 	struct ath11k_skb_rxcb *rxcb = ATH11K_SKB_RXCB(msdu);
1691 	bool ip_csum_fail, l4_csum_fail;
1692 
1693 	ip_csum_fail = ath11k_dp_rx_h_attn_ip_cksum_fail(rxcb->rx_desc);
1694 	l4_csum_fail = ath11k_dp_rx_h_attn_l4_cksum_fail(rxcb->rx_desc);
1695 
1696 	msdu->ip_summed = (ip_csum_fail || l4_csum_fail) ?
1697 			  CHECKSUM_NONE : CHECKSUM_UNNECESSARY;
1698 }
1699 
1700 static int ath11k_dp_rx_crypto_mic_len(struct ath11k *ar,
1701 				       enum hal_encrypt_type enctype)
1702 {
1703 	switch (enctype) {
1704 	case HAL_ENCRYPT_TYPE_OPEN:
1705 	case HAL_ENCRYPT_TYPE_TKIP_NO_MIC:
1706 	case HAL_ENCRYPT_TYPE_TKIP_MIC:
1707 		return 0;
1708 	case HAL_ENCRYPT_TYPE_CCMP_128:
1709 		return IEEE80211_CCMP_MIC_LEN;
1710 	case HAL_ENCRYPT_TYPE_CCMP_256:
1711 		return IEEE80211_CCMP_256_MIC_LEN;
1712 	case HAL_ENCRYPT_TYPE_GCMP_128:
1713 	case HAL_ENCRYPT_TYPE_AES_GCMP_256:
1714 		return IEEE80211_GCMP_MIC_LEN;
1715 	case HAL_ENCRYPT_TYPE_WEP_40:
1716 	case HAL_ENCRYPT_TYPE_WEP_104:
1717 	case HAL_ENCRYPT_TYPE_WEP_128:
1718 	case HAL_ENCRYPT_TYPE_WAPI_GCM_SM4:
1719 	case HAL_ENCRYPT_TYPE_WAPI:
1720 		break;
1721 	}
1722 
1723 	ath11k_warn(ar->ab, "unsupported encryption type %d for mic len\n", enctype);
1724 	return 0;
1725 }
1726 
1727 static int ath11k_dp_rx_crypto_param_len(struct ath11k *ar,
1728 					 enum hal_encrypt_type enctype)
1729 {
1730 	switch (enctype) {
1731 	case HAL_ENCRYPT_TYPE_OPEN:
1732 		return 0;
1733 	case HAL_ENCRYPT_TYPE_TKIP_NO_MIC:
1734 	case HAL_ENCRYPT_TYPE_TKIP_MIC:
1735 		return IEEE80211_TKIP_IV_LEN;
1736 	case HAL_ENCRYPT_TYPE_CCMP_128:
1737 		return IEEE80211_CCMP_HDR_LEN;
1738 	case HAL_ENCRYPT_TYPE_CCMP_256:
1739 		return IEEE80211_CCMP_256_HDR_LEN;
1740 	case HAL_ENCRYPT_TYPE_GCMP_128:
1741 	case HAL_ENCRYPT_TYPE_AES_GCMP_256:
1742 		return IEEE80211_GCMP_HDR_LEN;
1743 	case HAL_ENCRYPT_TYPE_WEP_40:
1744 	case HAL_ENCRYPT_TYPE_WEP_104:
1745 	case HAL_ENCRYPT_TYPE_WEP_128:
1746 	case HAL_ENCRYPT_TYPE_WAPI_GCM_SM4:
1747 	case HAL_ENCRYPT_TYPE_WAPI:
1748 		break;
1749 	}
1750 
1751 	ath11k_warn(ar->ab, "unsupported encryption type %d\n", enctype);
1752 	return 0;
1753 }
1754 
1755 static int ath11k_dp_rx_crypto_icv_len(struct ath11k *ar,
1756 				       enum hal_encrypt_type enctype)
1757 {
1758 	switch (enctype) {
1759 	case HAL_ENCRYPT_TYPE_OPEN:
1760 	case HAL_ENCRYPT_TYPE_CCMP_128:
1761 	case HAL_ENCRYPT_TYPE_CCMP_256:
1762 	case HAL_ENCRYPT_TYPE_GCMP_128:
1763 	case HAL_ENCRYPT_TYPE_AES_GCMP_256:
1764 		return 0;
1765 	case HAL_ENCRYPT_TYPE_TKIP_NO_MIC:
1766 	case HAL_ENCRYPT_TYPE_TKIP_MIC:
1767 		return IEEE80211_TKIP_ICV_LEN;
1768 	case HAL_ENCRYPT_TYPE_WEP_40:
1769 	case HAL_ENCRYPT_TYPE_WEP_104:
1770 	case HAL_ENCRYPT_TYPE_WEP_128:
1771 	case HAL_ENCRYPT_TYPE_WAPI_GCM_SM4:
1772 	case HAL_ENCRYPT_TYPE_WAPI:
1773 		break;
1774 	}
1775 
1776 	ath11k_warn(ar->ab, "unsupported encryption type %d\n", enctype);
1777 	return 0;
1778 }
1779 
1780 static void ath11k_dp_rx_h_undecap_nwifi(struct ath11k *ar,
1781 					 struct sk_buff *msdu,
1782 					 u8 *first_hdr,
1783 					 enum hal_encrypt_type enctype,
1784 					 struct ieee80211_rx_status *status)
1785 {
1786 	struct ath11k_skb_rxcb *rxcb = ATH11K_SKB_RXCB(msdu);
1787 	u8 decap_hdr[DP_MAX_NWIFI_HDR_LEN];
1788 	struct ieee80211_hdr *hdr;
1789 	size_t hdr_len;
1790 	u8 da[ETH_ALEN];
1791 	u8 sa[ETH_ALEN];
1792 	u16 qos_ctl = 0;
1793 	u8 *qos;
1794 
1795 	/* copy SA & DA and pull decapped header */
1796 	hdr = (struct ieee80211_hdr *)msdu->data;
1797 	hdr_len = ieee80211_hdrlen(hdr->frame_control);
1798 	ether_addr_copy(da, ieee80211_get_DA(hdr));
1799 	ether_addr_copy(sa, ieee80211_get_SA(hdr));
1800 	skb_pull(msdu, ieee80211_hdrlen(hdr->frame_control));
1801 
1802 	if (rxcb->is_first_msdu) {
1803 		/* original 802.11 header is valid for the first msdu
1804 		 * hence we can reuse the same header
1805 		 */
1806 		hdr = (struct ieee80211_hdr *)first_hdr;
1807 		hdr_len = ieee80211_hdrlen(hdr->frame_control);
1808 
1809 		/* Each A-MSDU subframe will be reported as a separate MSDU,
1810 		 * so strip the A-MSDU bit from QoS Ctl.
1811 		 */
1812 		if (ieee80211_is_data_qos(hdr->frame_control)) {
1813 			qos = ieee80211_get_qos_ctl(hdr);
1814 			qos[0] &= ~IEEE80211_QOS_CTL_A_MSDU_PRESENT;
1815 		}
1816 	} else {
1817 		/*  Rebuild qos header if this is a middle/last msdu */
1818 		hdr->frame_control |= __cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
1819 
1820 		/* Reset the order bit as the HT_Control header is stripped */
1821 		hdr->frame_control &= ~(__cpu_to_le16(IEEE80211_FCTL_ORDER));
1822 
1823 		qos_ctl = rxcb->tid;
1824 
1825 		if (ath11k_dp_rx_h_msdu_start_mesh_ctl_present(rxcb->rx_desc))
1826 			qos_ctl |= IEEE80211_QOS_CTL_MESH_CONTROL_PRESENT;
1827 
1828 		/* TODO Add other QoS ctl fields when required */
1829 
1830 		/* copy decap header before overwriting for reuse below */
1831 		memcpy(decap_hdr, (uint8_t *)hdr, hdr_len);
1832 	}
1833 
1834 	if (!(status->flag & RX_FLAG_IV_STRIPPED)) {
1835 		memcpy(skb_push(msdu,
1836 				ath11k_dp_rx_crypto_param_len(ar, enctype)),
1837 		       (void *)hdr + hdr_len,
1838 		       ath11k_dp_rx_crypto_param_len(ar, enctype));
1839 	}
1840 
1841 	if (!rxcb->is_first_msdu) {
1842 		memcpy(skb_push(msdu,
1843 				IEEE80211_QOS_CTL_LEN), &qos_ctl,
1844 				IEEE80211_QOS_CTL_LEN);
1845 		memcpy(skb_push(msdu, hdr_len), decap_hdr, hdr_len);
1846 		return;
1847 	}
1848 
1849 	memcpy(skb_push(msdu, hdr_len), hdr, hdr_len);
1850 
1851 	/* original 802.11 header has a different DA and in
1852 	 * case of 4addr it may also have different SA
1853 	 */
1854 	hdr = (struct ieee80211_hdr *)msdu->data;
1855 	ether_addr_copy(ieee80211_get_DA(hdr), da);
1856 	ether_addr_copy(ieee80211_get_SA(hdr), sa);
1857 }
1858 
1859 static void ath11k_dp_rx_h_undecap_raw(struct ath11k *ar, struct sk_buff *msdu,
1860 				       enum hal_encrypt_type enctype,
1861 				       struct ieee80211_rx_status *status,
1862 				       bool decrypted)
1863 {
1864 	struct ath11k_skb_rxcb *rxcb = ATH11K_SKB_RXCB(msdu);
1865 	struct ieee80211_hdr *hdr;
1866 	size_t hdr_len;
1867 	size_t crypto_len;
1868 
1869 	if (!rxcb->is_first_msdu ||
1870 	    !(rxcb->is_first_msdu && rxcb->is_last_msdu)) {
1871 		WARN_ON_ONCE(1);
1872 		return;
1873 	}
1874 
1875 	skb_trim(msdu, msdu->len - FCS_LEN);
1876 
1877 	if (!decrypted)
1878 		return;
1879 
1880 	hdr = (void *)msdu->data;
1881 
1882 	/* Tail */
1883 	if (status->flag & RX_FLAG_IV_STRIPPED) {
1884 		skb_trim(msdu, msdu->len -
1885 			 ath11k_dp_rx_crypto_mic_len(ar, enctype));
1886 
1887 		skb_trim(msdu, msdu->len -
1888 			 ath11k_dp_rx_crypto_icv_len(ar, enctype));
1889 	} else {
1890 		/* MIC */
1891 		if (status->flag & RX_FLAG_MIC_STRIPPED)
1892 			skb_trim(msdu, msdu->len -
1893 				 ath11k_dp_rx_crypto_mic_len(ar, enctype));
1894 
1895 		/* ICV */
1896 		if (status->flag & RX_FLAG_ICV_STRIPPED)
1897 			skb_trim(msdu, msdu->len -
1898 				 ath11k_dp_rx_crypto_icv_len(ar, enctype));
1899 	}
1900 
1901 	/* MMIC */
1902 	if ((status->flag & RX_FLAG_MMIC_STRIPPED) &&
1903 	    !ieee80211_has_morefrags(hdr->frame_control) &&
1904 	    enctype == HAL_ENCRYPT_TYPE_TKIP_MIC)
1905 		skb_trim(msdu, msdu->len - IEEE80211_CCMP_MIC_LEN);
1906 
1907 	/* Head */
1908 	if (status->flag & RX_FLAG_IV_STRIPPED) {
1909 		hdr_len = ieee80211_hdrlen(hdr->frame_control);
1910 		crypto_len = ath11k_dp_rx_crypto_param_len(ar, enctype);
1911 
1912 		memmove((void *)msdu->data + crypto_len,
1913 			(void *)msdu->data, hdr_len);
1914 		skb_pull(msdu, crypto_len);
1915 	}
1916 }
1917 
1918 static void *ath11k_dp_rx_h_find_rfc1042(struct ath11k *ar,
1919 					 struct sk_buff *msdu,
1920 					 enum hal_encrypt_type enctype)
1921 {
1922 	struct ath11k_skb_rxcb *rxcb = ATH11K_SKB_RXCB(msdu);
1923 	struct ieee80211_hdr *hdr;
1924 	size_t hdr_len, crypto_len;
1925 	void *rfc1042;
1926 	bool is_amsdu;
1927 
1928 	is_amsdu = !(rxcb->is_first_msdu && rxcb->is_last_msdu);
1929 	hdr = (struct ieee80211_hdr *)ath11k_dp_rx_h_80211_hdr(rxcb->rx_desc);
1930 	rfc1042 = hdr;
1931 
1932 	if (rxcb->is_first_msdu) {
1933 		hdr_len = ieee80211_hdrlen(hdr->frame_control);
1934 		crypto_len = ath11k_dp_rx_crypto_param_len(ar, enctype);
1935 
1936 		rfc1042 += hdr_len + crypto_len;
1937 	}
1938 
1939 	if (is_amsdu)
1940 		rfc1042 += sizeof(struct ath11k_dp_amsdu_subframe_hdr);
1941 
1942 	return rfc1042;
1943 }
1944 
1945 static void ath11k_dp_rx_h_undecap_eth(struct ath11k *ar,
1946 				       struct sk_buff *msdu,
1947 				       u8 *first_hdr,
1948 				       enum hal_encrypt_type enctype,
1949 				       struct ieee80211_rx_status *status)
1950 {
1951 	struct ieee80211_hdr *hdr;
1952 	struct ethhdr *eth;
1953 	size_t hdr_len;
1954 	u8 da[ETH_ALEN];
1955 	u8 sa[ETH_ALEN];
1956 	void *rfc1042;
1957 
1958 	rfc1042 = ath11k_dp_rx_h_find_rfc1042(ar, msdu, enctype);
1959 	if (WARN_ON_ONCE(!rfc1042))
1960 		return;
1961 
1962 	/* pull decapped header and copy SA & DA */
1963 	eth = (struct ethhdr *)msdu->data;
1964 	ether_addr_copy(da, eth->h_dest);
1965 	ether_addr_copy(sa, eth->h_source);
1966 	skb_pull(msdu, sizeof(struct ethhdr));
1967 
1968 	/* push rfc1042/llc/snap */
1969 	memcpy(skb_push(msdu, sizeof(struct ath11k_dp_rfc1042_hdr)), rfc1042,
1970 	       sizeof(struct ath11k_dp_rfc1042_hdr));
1971 
1972 	/* push original 802.11 header */
1973 	hdr = (struct ieee80211_hdr *)first_hdr;
1974 	hdr_len = ieee80211_hdrlen(hdr->frame_control);
1975 
1976 	if (!(status->flag & RX_FLAG_IV_STRIPPED)) {
1977 		memcpy(skb_push(msdu,
1978 				ath11k_dp_rx_crypto_param_len(ar, enctype)),
1979 		       (void *)hdr + hdr_len,
1980 		       ath11k_dp_rx_crypto_param_len(ar, enctype));
1981 	}
1982 
1983 	memcpy(skb_push(msdu, hdr_len), hdr, hdr_len);
1984 
1985 	/* original 802.11 header has a different DA and in
1986 	 * case of 4addr it may also have different SA
1987 	 */
1988 	hdr = (struct ieee80211_hdr *)msdu->data;
1989 	ether_addr_copy(ieee80211_get_DA(hdr), da);
1990 	ether_addr_copy(ieee80211_get_SA(hdr), sa);
1991 }
1992 
1993 static void ath11k_dp_rx_h_undecap(struct ath11k *ar, struct sk_buff *msdu,
1994 				   struct hal_rx_desc *rx_desc,
1995 				   enum hal_encrypt_type enctype,
1996 				   struct ieee80211_rx_status *status,
1997 				   bool decrypted)
1998 {
1999 	u8 *first_hdr;
2000 	u8 decap;
2001 
2002 	first_hdr = ath11k_dp_rx_h_80211_hdr(rx_desc);
2003 	decap = ath11k_dp_rx_h_msdu_start_decap_type(rx_desc);
2004 
2005 	switch (decap) {
2006 	case DP_RX_DECAP_TYPE_NATIVE_WIFI:
2007 		ath11k_dp_rx_h_undecap_nwifi(ar, msdu, first_hdr,
2008 					     enctype, status);
2009 		break;
2010 	case DP_RX_DECAP_TYPE_RAW:
2011 		ath11k_dp_rx_h_undecap_raw(ar, msdu, enctype, status,
2012 					   decrypted);
2013 		break;
2014 	case DP_RX_DECAP_TYPE_ETHERNET2_DIX:
2015 		/* TODO undecap support for middle/last msdu's of amsdu */
2016 		ath11k_dp_rx_h_undecap_eth(ar, msdu, first_hdr,
2017 					   enctype, status);
2018 		break;
2019 	case DP_RX_DECAP_TYPE_8023:
2020 		/* TODO: Handle undecap for these formats */
2021 		break;
2022 	}
2023 }
2024 
2025 static void ath11k_dp_rx_h_mpdu(struct ath11k *ar,
2026 				struct sk_buff *msdu,
2027 				struct hal_rx_desc *rx_desc,
2028 				struct ieee80211_rx_status *rx_status)
2029 {
2030 	bool  fill_crypto_hdr, mcast;
2031 	enum hal_encrypt_type enctype;
2032 	bool is_decrypted = false;
2033 	struct ieee80211_hdr *hdr;
2034 	struct ath11k_peer *peer;
2035 	u32 err_bitmap;
2036 
2037 	hdr = (struct ieee80211_hdr *)msdu->data;
2038 
2039 	/* PN for multicast packets will be checked in mac80211 */
2040 
2041 	mcast = is_multicast_ether_addr(hdr->addr1);
2042 	fill_crypto_hdr = mcast;
2043 
2044 	is_decrypted = ath11k_dp_rx_h_attn_is_decrypted(rx_desc);
2045 
2046 	spin_lock_bh(&ar->ab->base_lock);
2047 	peer = ath11k_peer_find_by_addr(ar->ab, hdr->addr2);
2048 	if (peer) {
2049 		if (mcast)
2050 			enctype = peer->sec_type_grp;
2051 		else
2052 			enctype = peer->sec_type;
2053 	} else {
2054 		enctype = HAL_ENCRYPT_TYPE_OPEN;
2055 	}
2056 	spin_unlock_bh(&ar->ab->base_lock);
2057 
2058 	err_bitmap = ath11k_dp_rx_h_attn_mpdu_err(rx_desc);
2059 
2060 	/* Clear per-MPDU flags while leaving per-PPDU flags intact */
2061 	rx_status->flag &= ~(RX_FLAG_FAILED_FCS_CRC |
2062 			     RX_FLAG_MMIC_ERROR |
2063 			     RX_FLAG_DECRYPTED |
2064 			     RX_FLAG_IV_STRIPPED |
2065 			     RX_FLAG_MMIC_STRIPPED);
2066 
2067 	if (err_bitmap & DP_RX_MPDU_ERR_FCS)
2068 		rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
2069 	if (err_bitmap & DP_RX_MPDU_ERR_TKIP_MIC)
2070 		rx_status->flag |= RX_FLAG_MMIC_ERROR;
2071 
2072 	if (is_decrypted) {
2073 		rx_status->flag |= RX_FLAG_DECRYPTED | RX_FLAG_MMIC_STRIPPED;
2074 
2075 		if (fill_crypto_hdr)
2076 			rx_status->flag |= RX_FLAG_MIC_STRIPPED |
2077 					RX_FLAG_ICV_STRIPPED;
2078 		else
2079 			rx_status->flag |= RX_FLAG_IV_STRIPPED |
2080 					   RX_FLAG_PN_VALIDATED;
2081 	}
2082 
2083 	ath11k_dp_rx_h_csum_offload(msdu);
2084 	ath11k_dp_rx_h_undecap(ar, msdu, rx_desc,
2085 			       enctype, rx_status, is_decrypted);
2086 
2087 	if (!is_decrypted || fill_crypto_hdr)
2088 		return;
2089 
2090 	hdr = (void *)msdu->data;
2091 	hdr->frame_control &= ~__cpu_to_le16(IEEE80211_FCTL_PROTECTED);
2092 }
2093 
2094 static void ath11k_dp_rx_h_rate(struct ath11k *ar, struct hal_rx_desc *rx_desc,
2095 				struct ieee80211_rx_status *rx_status)
2096 {
2097 	struct ieee80211_supported_band *sband;
2098 	enum rx_msdu_start_pkt_type pkt_type;
2099 	u8 bw;
2100 	u8 rate_mcs, nss;
2101 	u8 sgi;
2102 	bool is_cck;
2103 
2104 	pkt_type = ath11k_dp_rx_h_msdu_start_pkt_type(rx_desc);
2105 	bw = ath11k_dp_rx_h_msdu_start_rx_bw(rx_desc);
2106 	rate_mcs = ath11k_dp_rx_h_msdu_start_rate_mcs(rx_desc);
2107 	nss = ath11k_dp_rx_h_msdu_start_nss(rx_desc);
2108 	sgi = ath11k_dp_rx_h_msdu_start_sgi(rx_desc);
2109 
2110 	switch (pkt_type) {
2111 	case RX_MSDU_START_PKT_TYPE_11A:
2112 	case RX_MSDU_START_PKT_TYPE_11B:
2113 		is_cck = (pkt_type == RX_MSDU_START_PKT_TYPE_11B);
2114 		sband = &ar->mac.sbands[rx_status->band];
2115 		rx_status->rate_idx = ath11k_mac_hw_rate_to_idx(sband, rate_mcs,
2116 								is_cck);
2117 		break;
2118 	case RX_MSDU_START_PKT_TYPE_11N:
2119 		rx_status->encoding = RX_ENC_HT;
2120 		if (rate_mcs > ATH11K_HT_MCS_MAX) {
2121 			ath11k_warn(ar->ab,
2122 				    "Received with invalid mcs in HT mode %d\n",
2123 				     rate_mcs);
2124 			break;
2125 		}
2126 		rx_status->rate_idx = rate_mcs + (8 * (nss - 1));
2127 		if (sgi)
2128 			rx_status->enc_flags |= RX_ENC_FLAG_SHORT_GI;
2129 		rx_status->bw = ath11k_mac_bw_to_mac80211_bw(bw);
2130 		break;
2131 	case RX_MSDU_START_PKT_TYPE_11AC:
2132 		rx_status->encoding = RX_ENC_VHT;
2133 		rx_status->rate_idx = rate_mcs;
2134 		if (rate_mcs > ATH11K_VHT_MCS_MAX) {
2135 			ath11k_warn(ar->ab,
2136 				    "Received with invalid mcs in VHT mode %d\n",
2137 				     rate_mcs);
2138 			break;
2139 		}
2140 		rx_status->nss = nss;
2141 		if (sgi)
2142 			rx_status->enc_flags |= RX_ENC_FLAG_SHORT_GI;
2143 		rx_status->bw = ath11k_mac_bw_to_mac80211_bw(bw);
2144 		break;
2145 	case RX_MSDU_START_PKT_TYPE_11AX:
2146 		rx_status->rate_idx = rate_mcs;
2147 		if (rate_mcs > ATH11K_HE_MCS_MAX) {
2148 			ath11k_warn(ar->ab,
2149 				    "Received with invalid mcs in HE mode %d\n",
2150 				    rate_mcs);
2151 			break;
2152 		}
2153 		rx_status->encoding = RX_ENC_HE;
2154 		rx_status->nss = nss;
2155 		rx_status->he_gi = ath11k_he_gi_to_nl80211_he_gi(sgi);
2156 		rx_status->bw = ath11k_mac_bw_to_mac80211_bw(bw);
2157 		break;
2158 	}
2159 }
2160 
2161 static void ath11k_dp_rx_h_ppdu(struct ath11k *ar, struct hal_rx_desc *rx_desc,
2162 				struct ieee80211_rx_status *rx_status)
2163 {
2164 	u8 channel_num;
2165 
2166 	rx_status->freq = 0;
2167 	rx_status->rate_idx = 0;
2168 	rx_status->nss = 0;
2169 	rx_status->encoding = RX_ENC_LEGACY;
2170 	rx_status->bw = RATE_INFO_BW_20;
2171 
2172 	rx_status->flag |= RX_FLAG_NO_SIGNAL_VAL;
2173 
2174 	channel_num = ath11k_dp_rx_h_msdu_start_freq(rx_desc);
2175 
2176 	if (channel_num >= 1 && channel_num <= 14) {
2177 		rx_status->band = NL80211_BAND_2GHZ;
2178 	} else if (channel_num >= 36 && channel_num <= 173) {
2179 		rx_status->band = NL80211_BAND_5GHZ;
2180 	} else {
2181 		spin_lock_bh(&ar->data_lock);
2182 		rx_status->band = ar->rx_channel->band;
2183 		channel_num =
2184 			ieee80211_frequency_to_channel(ar->rx_channel->center_freq);
2185 		spin_unlock_bh(&ar->data_lock);
2186 		ath11k_dbg_dump(ar->ab, ATH11K_DBG_DATA, NULL, "rx_desc: ",
2187 				rx_desc, sizeof(struct hal_rx_desc));
2188 	}
2189 
2190 	rx_status->freq = ieee80211_channel_to_frequency(channel_num,
2191 							 rx_status->band);
2192 
2193 	ath11k_dp_rx_h_rate(ar, rx_desc, rx_status);
2194 }
2195 
2196 static char *ath11k_print_get_tid(struct ieee80211_hdr *hdr, char *out,
2197 				  size_t size)
2198 {
2199 	u8 *qc;
2200 	int tid;
2201 
2202 	if (!ieee80211_is_data_qos(hdr->frame_control))
2203 		return "";
2204 
2205 	qc = ieee80211_get_qos_ctl(hdr);
2206 	tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
2207 	snprintf(out, size, "tid %d", tid);
2208 
2209 	return out;
2210 }
2211 
2212 static void ath11k_dp_rx_deliver_msdu(struct ath11k *ar, struct napi_struct *napi,
2213 				      struct sk_buff *msdu)
2214 {
2215 	static const struct ieee80211_radiotap_he known = {
2216 		.data1 = cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA1_DATA_MCS_KNOWN |
2217 				     IEEE80211_RADIOTAP_HE_DATA1_BW_RU_ALLOC_KNOWN),
2218 		.data2 = cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA2_GI_KNOWN),
2219 	};
2220 	struct ieee80211_rx_status *status;
2221 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)msdu->data;
2222 	struct ieee80211_radiotap_he *he = NULL;
2223 	char tid[32];
2224 
2225 	status = IEEE80211_SKB_RXCB(msdu);
2226 	if (status->encoding == RX_ENC_HE) {
2227 		he = skb_push(msdu, sizeof(known));
2228 		memcpy(he, &known, sizeof(known));
2229 		status->flag |= RX_FLAG_RADIOTAP_HE;
2230 	}
2231 
2232 	ath11k_dbg(ar->ab, ATH11K_DBG_DATA,
2233 		   "rx skb %pK len %u peer %pM %s %s sn %u %s%s%s%s%s%s%s %srate_idx %u vht_nss %u freq %u band %u flag 0x%x fcs-err %i mic-err %i amsdu-more %i\n",
2234 		   msdu,
2235 		   msdu->len,
2236 		   ieee80211_get_SA(hdr),
2237 		   ath11k_print_get_tid(hdr, tid, sizeof(tid)),
2238 		   is_multicast_ether_addr(ieee80211_get_DA(hdr)) ?
2239 							"mcast" : "ucast",
2240 		   (__le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_SEQ) >> 4,
2241 		   (status->encoding == RX_ENC_LEGACY) ? "legacy" : "",
2242 		   (status->encoding == RX_ENC_HT) ? "ht" : "",
2243 		   (status->encoding == RX_ENC_VHT) ? "vht" : "",
2244 		   (status->encoding == RX_ENC_HE) ? "he" : "",
2245 		   (status->bw == RATE_INFO_BW_40) ? "40" : "",
2246 		   (status->bw == RATE_INFO_BW_80) ? "80" : "",
2247 		   (status->bw == RATE_INFO_BW_160) ? "160" : "",
2248 		   status->enc_flags & RX_ENC_FLAG_SHORT_GI ? "sgi " : "",
2249 		   status->rate_idx,
2250 		   status->nss,
2251 		   status->freq,
2252 		   status->band, status->flag,
2253 		   !!(status->flag & RX_FLAG_FAILED_FCS_CRC),
2254 		   !!(status->flag & RX_FLAG_MMIC_ERROR),
2255 		   !!(status->flag & RX_FLAG_AMSDU_MORE));
2256 
2257 	/* TODO: trace rx packet */
2258 
2259 	ieee80211_rx_napi(ar->hw, NULL, msdu, napi);
2260 }
2261 
2262 static int ath11k_dp_rx_process_msdu(struct ath11k *ar,
2263 				     struct sk_buff *msdu,
2264 				     struct sk_buff_head *msdu_list)
2265 {
2266 	struct hal_rx_desc *rx_desc, *lrx_desc;
2267 	struct ieee80211_rx_status rx_status = {0};
2268 	struct ieee80211_rx_status *status;
2269 	struct ath11k_skb_rxcb *rxcb;
2270 	struct ieee80211_hdr *hdr;
2271 	struct sk_buff *last_buf;
2272 	u8 l3_pad_bytes;
2273 	u8 *hdr_status;
2274 	u16 msdu_len;
2275 	int ret;
2276 
2277 	last_buf = ath11k_dp_rx_get_msdu_last_buf(msdu_list, msdu);
2278 	if (!last_buf) {
2279 		ath11k_warn(ar->ab,
2280 			    "No valid Rx buffer to access Atten/MSDU_END/MPDU_END tlvs\n");
2281 		ret = -EIO;
2282 		goto free_out;
2283 	}
2284 
2285 	rx_desc = (struct hal_rx_desc *)msdu->data;
2286 	lrx_desc = (struct hal_rx_desc *)last_buf->data;
2287 	if (!ath11k_dp_rx_h_attn_msdu_done(lrx_desc)) {
2288 		ath11k_warn(ar->ab, "msdu_done bit in attention is not set\n");
2289 		ret = -EIO;
2290 		goto free_out;
2291 	}
2292 
2293 	rxcb = ATH11K_SKB_RXCB(msdu);
2294 	rxcb->rx_desc = rx_desc;
2295 	msdu_len = ath11k_dp_rx_h_msdu_start_msdu_len(rx_desc);
2296 	l3_pad_bytes = ath11k_dp_rx_h_msdu_end_l3pad(lrx_desc);
2297 
2298 	if (rxcb->is_frag) {
2299 		skb_pull(msdu, HAL_RX_DESC_SIZE);
2300 	} else if (!rxcb->is_continuation) {
2301 		if ((msdu_len + HAL_RX_DESC_SIZE) > DP_RX_BUFFER_SIZE) {
2302 			hdr_status = ath11k_dp_rx_h_80211_hdr(rx_desc);
2303 			ret = -EINVAL;
2304 			ath11k_warn(ar->ab, "invalid msdu len %u\n", msdu_len);
2305 			ath11k_dbg_dump(ar->ab, ATH11K_DBG_DATA, NULL, "", hdr_status,
2306 					sizeof(struct ieee80211_hdr));
2307 			ath11k_dbg_dump(ar->ab, ATH11K_DBG_DATA, NULL, "", rx_desc,
2308 					sizeof(struct hal_rx_desc));
2309 			goto free_out;
2310 		}
2311 		skb_put(msdu, HAL_RX_DESC_SIZE + l3_pad_bytes + msdu_len);
2312 		skb_pull(msdu, HAL_RX_DESC_SIZE + l3_pad_bytes);
2313 	} else {
2314 		ret = ath11k_dp_rx_msdu_coalesce(ar, msdu_list,
2315 						 msdu, last_buf,
2316 						 l3_pad_bytes, msdu_len);
2317 		if (ret) {
2318 			ath11k_warn(ar->ab,
2319 				    "failed to coalesce msdu rx buffer%d\n", ret);
2320 			goto free_out;
2321 		}
2322 	}
2323 
2324 	hdr = (struct ieee80211_hdr *)msdu->data;
2325 
2326 	/* Process only data frames */
2327 	if (!ieee80211_is_data(hdr->frame_control))
2328 		return -EINVAL;
2329 
2330 	ath11k_dp_rx_h_ppdu(ar, rx_desc, &rx_status);
2331 	ath11k_dp_rx_h_mpdu(ar, msdu, rx_desc, &rx_status);
2332 
2333 	rx_status.flag |= RX_FLAG_SKIP_MONITOR | RX_FLAG_DUP_VALIDATED;
2334 
2335 	status = IEEE80211_SKB_RXCB(msdu);
2336 	*status = rx_status;
2337 	return 0;
2338 
2339 free_out:
2340 	return ret;
2341 }
2342 
2343 static void ath11k_dp_rx_process_received_packets(struct ath11k_base *ab,
2344 						  struct napi_struct *napi,
2345 						  struct sk_buff_head *msdu_list,
2346 						  int *quota, int ring_id)
2347 {
2348 	struct ath11k_skb_rxcb *rxcb;
2349 	struct sk_buff *msdu;
2350 	struct ath11k *ar;
2351 	u8 mac_id;
2352 	int ret;
2353 
2354 	if (skb_queue_empty(msdu_list))
2355 		return;
2356 
2357 	rcu_read_lock();
2358 
2359 	while (*quota && (msdu = __skb_dequeue(msdu_list))) {
2360 		rxcb = ATH11K_SKB_RXCB(msdu);
2361 		mac_id = rxcb->mac_id;
2362 		ar = ab->pdevs[mac_id].ar;
2363 		if (!rcu_dereference(ab->pdevs_active[mac_id])) {
2364 			dev_kfree_skb_any(msdu);
2365 			continue;
2366 		}
2367 
2368 		if (test_bit(ATH11K_CAC_RUNNING, &ar->dev_flags)) {
2369 			dev_kfree_skb_any(msdu);
2370 			continue;
2371 		}
2372 
2373 		ret = ath11k_dp_rx_process_msdu(ar, msdu, msdu_list);
2374 		if (ret) {
2375 			ath11k_dbg(ab, ATH11K_DBG_DATA,
2376 				   "Unable to process msdu %d", ret);
2377 			dev_kfree_skb_any(msdu);
2378 			continue;
2379 		}
2380 
2381 		ath11k_dp_rx_deliver_msdu(ar, napi, msdu);
2382 		(*quota)--;
2383 	}
2384 
2385 	rcu_read_unlock();
2386 }
2387 
2388 int ath11k_dp_process_rx(struct ath11k_base *ab, int ring_id,
2389 			 struct napi_struct *napi, int budget)
2390 {
2391 	struct ath11k_dp *dp = &ab->dp;
2392 	struct dp_rxdma_ring *rx_ring;
2393 	int num_buffs_reaped[MAX_RADIOS] = {0};
2394 	struct sk_buff_head msdu_list;
2395 	struct ath11k_skb_rxcb *rxcb;
2396 	int total_msdu_reaped = 0;
2397 	struct hal_srng *srng;
2398 	struct sk_buff *msdu;
2399 	int quota = budget;
2400 	bool done = false;
2401 	int buf_id, mac_id;
2402 	struct ath11k *ar;
2403 	u32 *rx_desc;
2404 	int i;
2405 
2406 	__skb_queue_head_init(&msdu_list);
2407 
2408 	srng = &ab->hal.srng_list[dp->reo_dst_ring[ring_id].ring_id];
2409 
2410 	spin_lock_bh(&srng->lock);
2411 
2412 	ath11k_hal_srng_access_begin(ab, srng);
2413 
2414 try_again:
2415 	while ((rx_desc = ath11k_hal_srng_dst_get_next_entry(ab, srng))) {
2416 		struct hal_reo_dest_ring desc = *(struct hal_reo_dest_ring *)rx_desc;
2417 		enum hal_reo_dest_ring_push_reason push_reason;
2418 		u32 cookie;
2419 
2420 		cookie = FIELD_GET(BUFFER_ADDR_INFO1_SW_COOKIE,
2421 				   desc.buf_addr_info.info1);
2422 		buf_id = FIELD_GET(DP_RXDMA_BUF_COOKIE_BUF_ID,
2423 				   cookie);
2424 		mac_id = FIELD_GET(DP_RXDMA_BUF_COOKIE_PDEV_ID, cookie);
2425 
2426 		ar = ab->pdevs[mac_id].ar;
2427 		rx_ring = &ar->dp.rx_refill_buf_ring;
2428 		spin_lock_bh(&rx_ring->idr_lock);
2429 		msdu = idr_find(&rx_ring->bufs_idr, buf_id);
2430 		if (!msdu) {
2431 			ath11k_warn(ab, "frame rx with invalid buf_id %d\n",
2432 				    buf_id);
2433 			spin_unlock_bh(&rx_ring->idr_lock);
2434 			continue;
2435 		}
2436 
2437 		idr_remove(&rx_ring->bufs_idr, buf_id);
2438 		spin_unlock_bh(&rx_ring->idr_lock);
2439 
2440 		rxcb = ATH11K_SKB_RXCB(msdu);
2441 		dma_unmap_single(ab->dev, rxcb->paddr,
2442 				 msdu->len + skb_tailroom(msdu),
2443 				 DMA_FROM_DEVICE);
2444 
2445 		num_buffs_reaped[mac_id]++;
2446 		total_msdu_reaped++;
2447 
2448 		push_reason = FIELD_GET(HAL_REO_DEST_RING_INFO0_PUSH_REASON,
2449 					desc.info0);
2450 		if (push_reason !=
2451 		    HAL_REO_DEST_RING_PUSH_REASON_ROUTING_INSTRUCTION) {
2452 			dev_kfree_skb_any(msdu);
2453 			ab->soc_stats.hal_reo_error[dp->reo_dst_ring[ring_id].ring_id]++;
2454 			continue;
2455 		}
2456 
2457 		rxcb->is_first_msdu = !!(desc.rx_msdu_info.info0 &
2458 					 RX_MSDU_DESC_INFO0_FIRST_MSDU_IN_MPDU);
2459 		rxcb->is_last_msdu = !!(desc.rx_msdu_info.info0 &
2460 					RX_MSDU_DESC_INFO0_LAST_MSDU_IN_MPDU);
2461 		rxcb->is_continuation = !!(desc.rx_msdu_info.info0 &
2462 					   RX_MSDU_DESC_INFO0_MSDU_CONTINUATION);
2463 		rxcb->mac_id = mac_id;
2464 		rxcb->tid = FIELD_GET(HAL_REO_DEST_RING_INFO0_RX_QUEUE_NUM,
2465 				      desc.info0);
2466 
2467 		__skb_queue_tail(&msdu_list, msdu);
2468 
2469 		if (total_msdu_reaped >= quota && !rxcb->is_continuation) {
2470 			done = true;
2471 			break;
2472 		}
2473 	}
2474 
2475 	/* Hw might have updated the head pointer after we cached it.
2476 	 * In this case, even though there are entries in the ring we'll
2477 	 * get rx_desc NULL. Give the read another try with updated cached
2478 	 * head pointer so that we can reap complete MPDU in the current
2479 	 * rx processing.
2480 	 */
2481 	if (!done && ath11k_hal_srng_dst_num_free(ab, srng, true)) {
2482 		ath11k_hal_srng_access_end(ab, srng);
2483 		goto try_again;
2484 	}
2485 
2486 	ath11k_hal_srng_access_end(ab, srng);
2487 
2488 	spin_unlock_bh(&srng->lock);
2489 
2490 	if (!total_msdu_reaped)
2491 		goto exit;
2492 
2493 	for (i = 0; i < ab->num_radios; i++) {
2494 		if (!num_buffs_reaped[i])
2495 			continue;
2496 
2497 		ar = ab->pdevs[i].ar;
2498 		rx_ring = &ar->dp.rx_refill_buf_ring;
2499 
2500 		ath11k_dp_rxbufs_replenish(ab, i, rx_ring, num_buffs_reaped[i],
2501 					   HAL_RX_BUF_RBM_SW3_BM, GFP_ATOMIC);
2502 	}
2503 
2504 	ath11k_dp_rx_process_received_packets(ab, napi, &msdu_list,
2505 					      &quota, ring_id);
2506 
2507 exit:
2508 	return budget - quota;
2509 }
2510 
2511 static void ath11k_dp_rx_update_peer_stats(struct ath11k_sta *arsta,
2512 					   struct hal_rx_mon_ppdu_info *ppdu_info)
2513 {
2514 	struct ath11k_rx_peer_stats *rx_stats = arsta->rx_stats;
2515 	u32 num_msdu;
2516 
2517 	if (!rx_stats)
2518 		return;
2519 
2520 	num_msdu = ppdu_info->tcp_msdu_count + ppdu_info->tcp_ack_msdu_count +
2521 		   ppdu_info->udp_msdu_count + ppdu_info->other_msdu_count;
2522 
2523 	rx_stats->num_msdu += num_msdu;
2524 	rx_stats->tcp_msdu_count += ppdu_info->tcp_msdu_count +
2525 				    ppdu_info->tcp_ack_msdu_count;
2526 	rx_stats->udp_msdu_count += ppdu_info->udp_msdu_count;
2527 	rx_stats->other_msdu_count += ppdu_info->other_msdu_count;
2528 
2529 	if (ppdu_info->preamble_type == HAL_RX_PREAMBLE_11A ||
2530 	    ppdu_info->preamble_type == HAL_RX_PREAMBLE_11B) {
2531 		ppdu_info->nss = 1;
2532 		ppdu_info->mcs = HAL_RX_MAX_MCS;
2533 		ppdu_info->tid = IEEE80211_NUM_TIDS;
2534 	}
2535 
2536 	if (ppdu_info->nss > 0 && ppdu_info->nss <= HAL_RX_MAX_NSS)
2537 		rx_stats->nss_count[ppdu_info->nss - 1] += num_msdu;
2538 
2539 	if (ppdu_info->mcs <= HAL_RX_MAX_MCS)
2540 		rx_stats->mcs_count[ppdu_info->mcs] += num_msdu;
2541 
2542 	if (ppdu_info->gi < HAL_RX_GI_MAX)
2543 		rx_stats->gi_count[ppdu_info->gi] += num_msdu;
2544 
2545 	if (ppdu_info->bw < HAL_RX_BW_MAX)
2546 		rx_stats->bw_count[ppdu_info->bw] += num_msdu;
2547 
2548 	if (ppdu_info->ldpc < HAL_RX_SU_MU_CODING_MAX)
2549 		rx_stats->coding_count[ppdu_info->ldpc] += num_msdu;
2550 
2551 	if (ppdu_info->tid <= IEEE80211_NUM_TIDS)
2552 		rx_stats->tid_count[ppdu_info->tid] += num_msdu;
2553 
2554 	if (ppdu_info->preamble_type < HAL_RX_PREAMBLE_MAX)
2555 		rx_stats->pream_cnt[ppdu_info->preamble_type] += num_msdu;
2556 
2557 	if (ppdu_info->reception_type < HAL_RX_RECEPTION_TYPE_MAX)
2558 		rx_stats->reception_type[ppdu_info->reception_type] += num_msdu;
2559 
2560 	if (ppdu_info->is_stbc)
2561 		rx_stats->stbc_count += num_msdu;
2562 
2563 	if (ppdu_info->beamformed)
2564 		rx_stats->beamformed_count += num_msdu;
2565 
2566 	if (ppdu_info->num_mpdu_fcs_ok > 1)
2567 		rx_stats->ampdu_msdu_count += num_msdu;
2568 	else
2569 		rx_stats->non_ampdu_msdu_count += num_msdu;
2570 
2571 	rx_stats->num_mpdu_fcs_ok += ppdu_info->num_mpdu_fcs_ok;
2572 	rx_stats->num_mpdu_fcs_err += ppdu_info->num_mpdu_fcs_err;
2573 	rx_stats->dcm_count += ppdu_info->dcm;
2574 	rx_stats->ru_alloc_cnt[ppdu_info->ru_alloc] += num_msdu;
2575 
2576 	arsta->rssi_comb = ppdu_info->rssi_comb;
2577 	rx_stats->rx_duration += ppdu_info->rx_duration;
2578 	arsta->rx_duration = rx_stats->rx_duration;
2579 }
2580 
2581 static struct sk_buff *ath11k_dp_rx_alloc_mon_status_buf(struct ath11k_base *ab,
2582 							 struct dp_rxdma_ring *rx_ring,
2583 							 int *buf_id, gfp_t gfp)
2584 {
2585 	struct sk_buff *skb;
2586 	dma_addr_t paddr;
2587 
2588 	skb = dev_alloc_skb(DP_RX_BUFFER_SIZE +
2589 			    DP_RX_BUFFER_ALIGN_SIZE);
2590 
2591 	if (!skb)
2592 		goto fail_alloc_skb;
2593 
2594 	if (!IS_ALIGNED((unsigned long)skb->data,
2595 			DP_RX_BUFFER_ALIGN_SIZE)) {
2596 		skb_pull(skb, PTR_ALIGN(skb->data, DP_RX_BUFFER_ALIGN_SIZE) -
2597 			 skb->data);
2598 	}
2599 
2600 	paddr = dma_map_single(ab->dev, skb->data,
2601 			       skb->len + skb_tailroom(skb),
2602 			       DMA_BIDIRECTIONAL);
2603 	if (unlikely(dma_mapping_error(ab->dev, paddr)))
2604 		goto fail_free_skb;
2605 
2606 	spin_lock_bh(&rx_ring->idr_lock);
2607 	*buf_id = idr_alloc(&rx_ring->bufs_idr, skb, 0,
2608 			    rx_ring->bufs_max, gfp);
2609 	spin_unlock_bh(&rx_ring->idr_lock);
2610 	if (*buf_id < 0)
2611 		goto fail_dma_unmap;
2612 
2613 	ATH11K_SKB_RXCB(skb)->paddr = paddr;
2614 	return skb;
2615 
2616 fail_dma_unmap:
2617 	dma_unmap_single(ab->dev, paddr, skb->len + skb_tailroom(skb),
2618 			 DMA_BIDIRECTIONAL);
2619 fail_free_skb:
2620 	dev_kfree_skb_any(skb);
2621 fail_alloc_skb:
2622 	return NULL;
2623 }
2624 
2625 int ath11k_dp_rx_mon_status_bufs_replenish(struct ath11k_base *ab, int mac_id,
2626 					   struct dp_rxdma_ring *rx_ring,
2627 					   int req_entries,
2628 					   enum hal_rx_buf_return_buf_manager mgr,
2629 					   gfp_t gfp)
2630 {
2631 	struct hal_srng *srng;
2632 	u32 *desc;
2633 	struct sk_buff *skb;
2634 	int num_free;
2635 	int num_remain;
2636 	int buf_id;
2637 	u32 cookie;
2638 	dma_addr_t paddr;
2639 
2640 	req_entries = min(req_entries, rx_ring->bufs_max);
2641 
2642 	srng = &ab->hal.srng_list[rx_ring->refill_buf_ring.ring_id];
2643 
2644 	spin_lock_bh(&srng->lock);
2645 
2646 	ath11k_hal_srng_access_begin(ab, srng);
2647 
2648 	num_free = ath11k_hal_srng_src_num_free(ab, srng, true);
2649 
2650 	req_entries = min(num_free, req_entries);
2651 	num_remain = req_entries;
2652 
2653 	while (num_remain > 0) {
2654 		skb = ath11k_dp_rx_alloc_mon_status_buf(ab, rx_ring,
2655 							&buf_id, gfp);
2656 		if (!skb)
2657 			break;
2658 		paddr = ATH11K_SKB_RXCB(skb)->paddr;
2659 
2660 		desc = ath11k_hal_srng_src_get_next_entry(ab, srng);
2661 		if (!desc)
2662 			goto fail_desc_get;
2663 
2664 		cookie = FIELD_PREP(DP_RXDMA_BUF_COOKIE_PDEV_ID, mac_id) |
2665 			 FIELD_PREP(DP_RXDMA_BUF_COOKIE_BUF_ID, buf_id);
2666 
2667 		num_remain--;
2668 
2669 		ath11k_hal_rx_buf_addr_info_set(desc, paddr, cookie, mgr);
2670 	}
2671 
2672 	ath11k_hal_srng_access_end(ab, srng);
2673 
2674 	spin_unlock_bh(&srng->lock);
2675 
2676 	return req_entries - num_remain;
2677 
2678 fail_desc_get:
2679 	spin_lock_bh(&rx_ring->idr_lock);
2680 	idr_remove(&rx_ring->bufs_idr, buf_id);
2681 	spin_unlock_bh(&rx_ring->idr_lock);
2682 	dma_unmap_single(ab->dev, paddr, skb->len + skb_tailroom(skb),
2683 			 DMA_BIDIRECTIONAL);
2684 	dev_kfree_skb_any(skb);
2685 	ath11k_hal_srng_access_end(ab, srng);
2686 	spin_unlock_bh(&srng->lock);
2687 
2688 	return req_entries - num_remain;
2689 }
2690 
2691 static int ath11k_dp_rx_reap_mon_status_ring(struct ath11k_base *ab, int mac_id,
2692 					     int *budget, struct sk_buff_head *skb_list)
2693 {
2694 	struct ath11k *ar = ab->pdevs[mac_id].ar;
2695 	struct ath11k_pdev_dp *dp = &ar->dp;
2696 	struct dp_rxdma_ring *rx_ring = &dp->rx_mon_status_refill_ring;
2697 	struct hal_srng *srng;
2698 	void *rx_mon_status_desc;
2699 	struct sk_buff *skb;
2700 	struct ath11k_skb_rxcb *rxcb;
2701 	struct hal_tlv_hdr *tlv;
2702 	u32 cookie;
2703 	int buf_id;
2704 	dma_addr_t paddr;
2705 	u8 rbm;
2706 	int num_buffs_reaped = 0;
2707 
2708 	srng = &ab->hal.srng_list[rx_ring->refill_buf_ring.ring_id];
2709 
2710 	spin_lock_bh(&srng->lock);
2711 
2712 	ath11k_hal_srng_access_begin(ab, srng);
2713 	while (*budget) {
2714 		*budget -= 1;
2715 		rx_mon_status_desc =
2716 			ath11k_hal_srng_src_peek(ab, srng);
2717 		if (!rx_mon_status_desc)
2718 			break;
2719 
2720 		ath11k_hal_rx_buf_addr_info_get(rx_mon_status_desc, &paddr,
2721 						&cookie, &rbm);
2722 		if (paddr) {
2723 			buf_id = FIELD_GET(DP_RXDMA_BUF_COOKIE_BUF_ID, cookie);
2724 
2725 			spin_lock_bh(&rx_ring->idr_lock);
2726 			skb = idr_find(&rx_ring->bufs_idr, buf_id);
2727 			if (!skb) {
2728 				ath11k_warn(ab, "rx monitor status with invalid buf_id %d\n",
2729 					    buf_id);
2730 				spin_unlock_bh(&rx_ring->idr_lock);
2731 				goto move_next;
2732 			}
2733 
2734 			idr_remove(&rx_ring->bufs_idr, buf_id);
2735 			spin_unlock_bh(&rx_ring->idr_lock);
2736 
2737 			rxcb = ATH11K_SKB_RXCB(skb);
2738 
2739 			dma_sync_single_for_cpu(ab->dev, rxcb->paddr,
2740 						skb->len + skb_tailroom(skb),
2741 						DMA_FROM_DEVICE);
2742 
2743 			dma_unmap_single(ab->dev, rxcb->paddr,
2744 					 skb->len + skb_tailroom(skb),
2745 					 DMA_BIDIRECTIONAL);
2746 
2747 			tlv = (struct hal_tlv_hdr *)skb->data;
2748 			if (FIELD_GET(HAL_TLV_HDR_TAG, tlv->tl) !=
2749 					HAL_RX_STATUS_BUFFER_DONE) {
2750 				ath11k_warn(ab, "mon status DONE not set %lx\n",
2751 					    FIELD_GET(HAL_TLV_HDR_TAG,
2752 						      tlv->tl));
2753 				dev_kfree_skb_any(skb);
2754 				goto move_next;
2755 			}
2756 
2757 			__skb_queue_tail(skb_list, skb);
2758 		}
2759 move_next:
2760 		skb = ath11k_dp_rx_alloc_mon_status_buf(ab, rx_ring,
2761 							&buf_id, GFP_ATOMIC);
2762 
2763 		if (!skb) {
2764 			ath11k_hal_rx_buf_addr_info_set(rx_mon_status_desc, 0, 0,
2765 							HAL_RX_BUF_RBM_SW3_BM);
2766 			num_buffs_reaped++;
2767 			break;
2768 		}
2769 		rxcb = ATH11K_SKB_RXCB(skb);
2770 
2771 		cookie = FIELD_PREP(DP_RXDMA_BUF_COOKIE_PDEV_ID, mac_id) |
2772 			 FIELD_PREP(DP_RXDMA_BUF_COOKIE_BUF_ID, buf_id);
2773 
2774 		ath11k_hal_rx_buf_addr_info_set(rx_mon_status_desc, rxcb->paddr,
2775 						cookie, HAL_RX_BUF_RBM_SW3_BM);
2776 		ath11k_hal_srng_src_get_next_entry(ab, srng);
2777 		num_buffs_reaped++;
2778 	}
2779 	ath11k_hal_srng_access_end(ab, srng);
2780 	spin_unlock_bh(&srng->lock);
2781 
2782 	return num_buffs_reaped;
2783 }
2784 
2785 int ath11k_dp_rx_process_mon_status(struct ath11k_base *ab, int mac_id,
2786 				    struct napi_struct *napi, int budget)
2787 {
2788 	struct ath11k *ar = ab->pdevs[mac_id].ar;
2789 	enum hal_rx_mon_status hal_status;
2790 	struct sk_buff *skb;
2791 	struct sk_buff_head skb_list;
2792 	struct hal_rx_mon_ppdu_info ppdu_info;
2793 	struct ath11k_peer *peer;
2794 	struct ath11k_sta *arsta;
2795 	int num_buffs_reaped = 0;
2796 
2797 	__skb_queue_head_init(&skb_list);
2798 
2799 	num_buffs_reaped = ath11k_dp_rx_reap_mon_status_ring(ab, mac_id, &budget,
2800 							     &skb_list);
2801 	if (!num_buffs_reaped)
2802 		goto exit;
2803 
2804 	while ((skb = __skb_dequeue(&skb_list))) {
2805 		memset(&ppdu_info, 0, sizeof(ppdu_info));
2806 		ppdu_info.peer_id = HAL_INVALID_PEERID;
2807 
2808 		if (ath11k_debug_is_pktlog_rx_stats_enabled(ar))
2809 			trace_ath11k_htt_rxdesc(ar, skb->data, DP_RX_BUFFER_SIZE);
2810 
2811 		hal_status = ath11k_hal_rx_parse_mon_status(ab, &ppdu_info, skb);
2812 
2813 		if (ppdu_info.peer_id == HAL_INVALID_PEERID ||
2814 		    hal_status != HAL_RX_MON_STATUS_PPDU_DONE) {
2815 			dev_kfree_skb_any(skb);
2816 			continue;
2817 		}
2818 
2819 		rcu_read_lock();
2820 		spin_lock_bh(&ab->base_lock);
2821 		peer = ath11k_peer_find_by_id(ab, ppdu_info.peer_id);
2822 
2823 		if (!peer || !peer->sta) {
2824 			ath11k_dbg(ab, ATH11K_DBG_DATA,
2825 				   "failed to find the peer with peer_id %d\n",
2826 				   ppdu_info.peer_id);
2827 			spin_unlock_bh(&ab->base_lock);
2828 			rcu_read_unlock();
2829 			dev_kfree_skb_any(skb);
2830 			continue;
2831 		}
2832 
2833 		arsta = (struct ath11k_sta *)peer->sta->drv_priv;
2834 		ath11k_dp_rx_update_peer_stats(arsta, &ppdu_info);
2835 
2836 		if (ath11k_debug_is_pktlog_peer_valid(ar, peer->addr))
2837 			trace_ath11k_htt_rxdesc(ar, skb->data, DP_RX_BUFFER_SIZE);
2838 
2839 		spin_unlock_bh(&ab->base_lock);
2840 		rcu_read_unlock();
2841 
2842 		dev_kfree_skb_any(skb);
2843 	}
2844 exit:
2845 	return num_buffs_reaped;
2846 }
2847 
2848 static void ath11k_dp_rx_frag_timer(struct timer_list *timer)
2849 {
2850 	struct dp_rx_tid *rx_tid = from_timer(rx_tid, timer, frag_timer);
2851 
2852 	spin_lock_bh(&rx_tid->ab->base_lock);
2853 	if (rx_tid->last_frag_no &&
2854 	    rx_tid->rx_frag_bitmap == GENMASK(rx_tid->last_frag_no, 0)) {
2855 		spin_unlock_bh(&rx_tid->ab->base_lock);
2856 		return;
2857 	}
2858 	ath11k_dp_rx_frags_cleanup(rx_tid, true);
2859 	spin_unlock_bh(&rx_tid->ab->base_lock);
2860 }
2861 
2862 int ath11k_peer_rx_frag_setup(struct ath11k *ar, const u8 *peer_mac, int vdev_id)
2863 {
2864 	struct ath11k_base *ab = ar->ab;
2865 	struct crypto_shash *tfm;
2866 	struct ath11k_peer *peer;
2867 	struct dp_rx_tid *rx_tid;
2868 	int i;
2869 
2870 	tfm = crypto_alloc_shash("michael_mic", 0, 0);
2871 	if (IS_ERR(tfm))
2872 		return PTR_ERR(tfm);
2873 
2874 	spin_lock_bh(&ab->base_lock);
2875 
2876 	peer = ath11k_peer_find(ab, vdev_id, peer_mac);
2877 	if (!peer) {
2878 		ath11k_warn(ab, "failed to find the peer to set up fragment info\n");
2879 		spin_unlock_bh(&ab->base_lock);
2880 		return -ENOENT;
2881 	}
2882 
2883 	for (i = 0; i <= IEEE80211_NUM_TIDS; i++) {
2884 		rx_tid = &peer->rx_tid[i];
2885 		rx_tid->ab = ab;
2886 		timer_setup(&rx_tid->frag_timer, ath11k_dp_rx_frag_timer, 0);
2887 		skb_queue_head_init(&rx_tid->rx_frags);
2888 	}
2889 
2890 	peer->tfm_mmic = tfm;
2891 	spin_unlock_bh(&ab->base_lock);
2892 
2893 	return 0;
2894 }
2895 
2896 static int ath11k_dp_rx_h_michael_mic(struct crypto_shash *tfm, u8 *key,
2897 				      struct ieee80211_hdr *hdr, u8 *data,
2898 				      size_t data_len, u8 *mic)
2899 {
2900 	SHASH_DESC_ON_STACK(desc, tfm);
2901 	u8 mic_hdr[16] = {0};
2902 	u8 tid = 0;
2903 	int ret;
2904 
2905 	if (!tfm)
2906 		return -EINVAL;
2907 
2908 	desc->tfm = tfm;
2909 
2910 	ret = crypto_shash_setkey(tfm, key, 8);
2911 	if (ret)
2912 		goto out;
2913 
2914 	ret = crypto_shash_init(desc);
2915 	if (ret)
2916 		goto out;
2917 
2918 	/* TKIP MIC header */
2919 	memcpy(mic_hdr, ieee80211_get_DA(hdr), ETH_ALEN);
2920 	memcpy(mic_hdr + ETH_ALEN, ieee80211_get_SA(hdr), ETH_ALEN);
2921 	if (ieee80211_is_data_qos(hdr->frame_control))
2922 		tid = ieee80211_get_tid(hdr);
2923 	mic_hdr[12] = tid;
2924 
2925 	ret = crypto_shash_update(desc, mic_hdr, 16);
2926 	if (ret)
2927 		goto out;
2928 	ret = crypto_shash_update(desc, data, data_len);
2929 	if (ret)
2930 		goto out;
2931 	ret = crypto_shash_final(desc, mic);
2932 out:
2933 	shash_desc_zero(desc);
2934 	return ret;
2935 }
2936 
2937 static int ath11k_dp_rx_h_verify_tkip_mic(struct ath11k *ar, struct ath11k_peer *peer,
2938 					  struct sk_buff *msdu)
2939 {
2940 	struct hal_rx_desc *rx_desc = (struct hal_rx_desc *)msdu->data;
2941 	struct ieee80211_rx_status *rxs = IEEE80211_SKB_RXCB(msdu);
2942 	struct ieee80211_key_conf *key_conf;
2943 	struct ieee80211_hdr *hdr;
2944 	u8 mic[IEEE80211_CCMP_MIC_LEN];
2945 	int head_len, tail_len, ret;
2946 	size_t data_len;
2947 	u32 hdr_len;
2948 	u8 *key, *data;
2949 	u8 key_idx;
2950 
2951 	if (ath11k_dp_rx_h_mpdu_start_enctype(rx_desc) != HAL_ENCRYPT_TYPE_TKIP_MIC)
2952 		return 0;
2953 
2954 	hdr = (struct ieee80211_hdr *)(msdu->data + HAL_RX_DESC_SIZE);
2955 	hdr_len = ieee80211_hdrlen(hdr->frame_control);
2956 	head_len = hdr_len + HAL_RX_DESC_SIZE + IEEE80211_TKIP_IV_LEN;
2957 	tail_len = IEEE80211_CCMP_MIC_LEN + IEEE80211_TKIP_ICV_LEN + FCS_LEN;
2958 
2959 	if (!is_multicast_ether_addr(hdr->addr1))
2960 		key_idx = peer->ucast_keyidx;
2961 	else
2962 		key_idx = peer->mcast_keyidx;
2963 
2964 	key_conf = peer->keys[key_idx];
2965 
2966 	data = msdu->data + head_len;
2967 	data_len = msdu->len - head_len - tail_len;
2968 	key = &key_conf->key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY];
2969 
2970 	ret = ath11k_dp_rx_h_michael_mic(peer->tfm_mmic, key, hdr, data, data_len, mic);
2971 	if (ret || memcmp(mic, data + data_len, IEEE80211_CCMP_MIC_LEN))
2972 		goto mic_fail;
2973 
2974 	return 0;
2975 
2976 mic_fail:
2977 	(ATH11K_SKB_RXCB(msdu))->is_first_msdu = true;
2978 	(ATH11K_SKB_RXCB(msdu))->is_last_msdu = true;
2979 
2980 	rxs->flag |= RX_FLAG_MMIC_ERROR | RX_FLAG_MMIC_STRIPPED |
2981 		    RX_FLAG_IV_STRIPPED | RX_FLAG_DECRYPTED;
2982 	skb_pull(msdu, HAL_RX_DESC_SIZE);
2983 
2984 	ath11k_dp_rx_h_ppdu(ar, rx_desc, rxs);
2985 	ath11k_dp_rx_h_undecap(ar, msdu, rx_desc,
2986 			       HAL_ENCRYPT_TYPE_TKIP_MIC, rxs, true);
2987 	ieee80211_rx(ar->hw, msdu);
2988 	return -EINVAL;
2989 }
2990 
2991 static void ath11k_dp_rx_h_undecap_frag(struct ath11k *ar, struct sk_buff *msdu,
2992 					enum hal_encrypt_type enctype, u32 flags)
2993 {
2994 	struct ieee80211_hdr *hdr;
2995 	size_t hdr_len;
2996 	size_t crypto_len;
2997 
2998 	if (!flags)
2999 		return;
3000 
3001 	hdr = (struct ieee80211_hdr *)(msdu->data + HAL_RX_DESC_SIZE);
3002 
3003 	if (flags & RX_FLAG_MIC_STRIPPED)
3004 		skb_trim(msdu, msdu->len -
3005 			 ath11k_dp_rx_crypto_mic_len(ar, enctype));
3006 
3007 	if (flags & RX_FLAG_ICV_STRIPPED)
3008 		skb_trim(msdu, msdu->len -
3009 			 ath11k_dp_rx_crypto_icv_len(ar, enctype));
3010 
3011 	if (flags & RX_FLAG_IV_STRIPPED) {
3012 		hdr_len = ieee80211_hdrlen(hdr->frame_control);
3013 		crypto_len = ath11k_dp_rx_crypto_param_len(ar, enctype);
3014 
3015 		memmove((void *)msdu->data + HAL_RX_DESC_SIZE + crypto_len,
3016 			(void *)msdu->data + HAL_RX_DESC_SIZE, hdr_len);
3017 		skb_pull(msdu, crypto_len);
3018 	}
3019 }
3020 
3021 static int ath11k_dp_rx_h_defrag(struct ath11k *ar,
3022 				 struct ath11k_peer *peer,
3023 				 struct dp_rx_tid *rx_tid,
3024 				 struct sk_buff **defrag_skb)
3025 {
3026 	struct hal_rx_desc *rx_desc;
3027 	struct sk_buff *skb, *first_frag, *last_frag;
3028 	struct ieee80211_hdr *hdr;
3029 	enum hal_encrypt_type enctype;
3030 	bool is_decrypted = false;
3031 	int msdu_len = 0;
3032 	int extra_space;
3033 	u32 flags;
3034 
3035 	first_frag = skb_peek(&rx_tid->rx_frags);
3036 	last_frag = skb_peek_tail(&rx_tid->rx_frags);
3037 
3038 	skb_queue_walk(&rx_tid->rx_frags, skb) {
3039 		flags = 0;
3040 		rx_desc = (struct hal_rx_desc *)skb->data;
3041 		hdr = (struct ieee80211_hdr *)(skb->data + HAL_RX_DESC_SIZE);
3042 
3043 		enctype = ath11k_dp_rx_h_mpdu_start_enctype(rx_desc);
3044 		if (enctype != HAL_ENCRYPT_TYPE_OPEN)
3045 			is_decrypted = ath11k_dp_rx_h_attn_is_decrypted(rx_desc);
3046 
3047 		if (is_decrypted) {
3048 			if (skb != first_frag)
3049 				flags |=  RX_FLAG_IV_STRIPPED;
3050 			if (skb != last_frag)
3051 				flags |= RX_FLAG_ICV_STRIPPED |
3052 					 RX_FLAG_MIC_STRIPPED;
3053 		}
3054 
3055 		/* RX fragments are always raw packets */
3056 		if (skb != last_frag)
3057 			skb_trim(skb, skb->len - FCS_LEN);
3058 		ath11k_dp_rx_h_undecap_frag(ar, skb, enctype, flags);
3059 
3060 		if (skb != first_frag)
3061 			skb_pull(skb, HAL_RX_DESC_SIZE +
3062 				      ieee80211_hdrlen(hdr->frame_control));
3063 		msdu_len += skb->len;
3064 	}
3065 
3066 	extra_space = msdu_len - (DP_RX_BUFFER_SIZE + skb_tailroom(first_frag));
3067 	if (extra_space > 0 &&
3068 	    (pskb_expand_head(first_frag, 0, extra_space, GFP_ATOMIC) < 0))
3069 		return -ENOMEM;
3070 
3071 	__skb_unlink(first_frag, &rx_tid->rx_frags);
3072 	while ((skb = __skb_dequeue(&rx_tid->rx_frags))) {
3073 		skb_put_data(first_frag, skb->data, skb->len);
3074 		dev_kfree_skb_any(skb);
3075 	}
3076 
3077 	hdr = (struct ieee80211_hdr *)(first_frag->data + HAL_RX_DESC_SIZE);
3078 	hdr->frame_control &= ~__cpu_to_le16(IEEE80211_FCTL_MOREFRAGS);
3079 	ATH11K_SKB_RXCB(first_frag)->is_frag = 1;
3080 
3081 	if (ath11k_dp_rx_h_verify_tkip_mic(ar, peer, first_frag))
3082 		first_frag = NULL;
3083 
3084 	*defrag_skb = first_frag;
3085 	return 0;
3086 }
3087 
3088 static int ath11k_dp_rx_h_defrag_reo_reinject(struct ath11k *ar, struct dp_rx_tid *rx_tid,
3089 					      struct sk_buff *defrag_skb)
3090 {
3091 	struct ath11k_base *ab = ar->ab;
3092 	struct ath11k_pdev_dp *dp = &ar->dp;
3093 	struct dp_rxdma_ring *rx_refill_ring = &dp->rx_refill_buf_ring;
3094 	struct hal_rx_desc *rx_desc = (struct hal_rx_desc *)defrag_skb->data;
3095 	struct hal_reo_entrance_ring *reo_ent_ring;
3096 	struct hal_reo_dest_ring *reo_dest_ring;
3097 	struct dp_link_desc_bank *link_desc_banks;
3098 	struct hal_rx_msdu_link *msdu_link;
3099 	struct hal_rx_msdu_details *msdu0;
3100 	struct hal_srng *srng;
3101 	dma_addr_t paddr;
3102 	u32 desc_bank, msdu_info, mpdu_info;
3103 	u32 dst_idx, cookie;
3104 	u32 *msdu_len_offset;
3105 	int ret, buf_id;
3106 
3107 	link_desc_banks = ab->dp.link_desc_banks;
3108 	reo_dest_ring = rx_tid->dst_ring_desc;
3109 
3110 	ath11k_hal_rx_reo_ent_paddr_get(ab, reo_dest_ring, &paddr, &desc_bank);
3111 	msdu_link = (struct hal_rx_msdu_link *)(link_desc_banks[desc_bank].vaddr +
3112 			(paddr - link_desc_banks[desc_bank].paddr));
3113 	msdu0 = &msdu_link->msdu_link[0];
3114 	dst_idx = FIELD_GET(RX_MSDU_DESC_INFO0_REO_DEST_IND, msdu0->rx_msdu_info.info0);
3115 	memset(msdu0, 0, sizeof(*msdu0));
3116 
3117 	msdu_info = FIELD_PREP(RX_MSDU_DESC_INFO0_FIRST_MSDU_IN_MPDU, 1) |
3118 		    FIELD_PREP(RX_MSDU_DESC_INFO0_LAST_MSDU_IN_MPDU, 1) |
3119 		    FIELD_PREP(RX_MSDU_DESC_INFO0_MSDU_CONTINUATION, 0) |
3120 		    FIELD_PREP(RX_MSDU_DESC_INFO0_MSDU_LENGTH,
3121 			       defrag_skb->len - HAL_RX_DESC_SIZE) |
3122 		    FIELD_PREP(RX_MSDU_DESC_INFO0_REO_DEST_IND, dst_idx) |
3123 		    FIELD_PREP(RX_MSDU_DESC_INFO0_VALID_SA, 1) |
3124 		    FIELD_PREP(RX_MSDU_DESC_INFO0_VALID_DA, 1);
3125 	msdu0->rx_msdu_info.info0 = msdu_info;
3126 
3127 	/* change msdu len in hal rx desc */
3128 	msdu_len_offset = (u32 *)&rx_desc->msdu_start;
3129 	*msdu_len_offset &= ~(RX_MSDU_START_INFO1_MSDU_LENGTH);
3130 	*msdu_len_offset |= defrag_skb->len - HAL_RX_DESC_SIZE;
3131 
3132 	paddr = dma_map_single(ab->dev, defrag_skb->data,
3133 			       defrag_skb->len + skb_tailroom(defrag_skb),
3134 			       DMA_FROM_DEVICE);
3135 	if (dma_mapping_error(ab->dev, paddr))
3136 		return -ENOMEM;
3137 
3138 	spin_lock_bh(&rx_refill_ring->idr_lock);
3139 	buf_id = idr_alloc(&rx_refill_ring->bufs_idr, defrag_skb, 0,
3140 			   rx_refill_ring->bufs_max * 3, GFP_ATOMIC);
3141 	spin_unlock_bh(&rx_refill_ring->idr_lock);
3142 	if (buf_id < 0) {
3143 		ret = -ENOMEM;
3144 		goto err_unmap_dma;
3145 	}
3146 
3147 	ATH11K_SKB_RXCB(defrag_skb)->paddr = paddr;
3148 	cookie = FIELD_PREP(DP_RXDMA_BUF_COOKIE_PDEV_ID, dp->mac_id) |
3149 		 FIELD_PREP(DP_RXDMA_BUF_COOKIE_BUF_ID, buf_id);
3150 
3151 	ath11k_hal_rx_buf_addr_info_set(msdu0, paddr, cookie, HAL_RX_BUF_RBM_SW3_BM);
3152 
3153 	/* Fill mpdu details into reo entrace ring */
3154 	srng = &ab->hal.srng_list[ab->dp.reo_reinject_ring.ring_id];
3155 
3156 	spin_lock_bh(&srng->lock);
3157 	ath11k_hal_srng_access_begin(ab, srng);
3158 
3159 	reo_ent_ring = (struct hal_reo_entrance_ring *)
3160 			ath11k_hal_srng_src_get_next_entry(ab, srng);
3161 	if (!reo_ent_ring) {
3162 		ath11k_hal_srng_access_end(ab, srng);
3163 		spin_unlock_bh(&srng->lock);
3164 		ret = -ENOSPC;
3165 		goto err_free_idr;
3166 	}
3167 	memset(reo_ent_ring, 0, sizeof(*reo_ent_ring));
3168 
3169 	ath11k_hal_rx_reo_ent_paddr_get(ab, reo_dest_ring, &paddr, &desc_bank);
3170 	ath11k_hal_rx_buf_addr_info_set(reo_ent_ring, paddr, desc_bank,
3171 					HAL_RX_BUF_RBM_WBM_IDLE_DESC_LIST);
3172 
3173 	mpdu_info = FIELD_PREP(RX_MPDU_DESC_INFO0_MSDU_COUNT, 1) |
3174 		    FIELD_PREP(RX_MPDU_DESC_INFO0_SEQ_NUM, rx_tid->cur_sn) |
3175 		    FIELD_PREP(RX_MPDU_DESC_INFO0_FRAG_FLAG, 0) |
3176 		    FIELD_PREP(RX_MPDU_DESC_INFO0_VALID_SA, 1) |
3177 		    FIELD_PREP(RX_MPDU_DESC_INFO0_VALID_DA, 1) |
3178 		    FIELD_PREP(RX_MPDU_DESC_INFO0_RAW_MPDU, 1) |
3179 		    FIELD_PREP(RX_MPDU_DESC_INFO0_VALID_PN, 1);
3180 
3181 	reo_ent_ring->rx_mpdu_info.info0 = mpdu_info;
3182 	reo_ent_ring->rx_mpdu_info.meta_data = reo_dest_ring->rx_mpdu_info.meta_data;
3183 	reo_ent_ring->queue_addr_lo = reo_dest_ring->queue_addr_lo;
3184 	reo_ent_ring->info0 = FIELD_PREP(HAL_REO_ENTR_RING_INFO0_QUEUE_ADDR_HI,
3185 					 FIELD_GET(HAL_REO_DEST_RING_INFO0_QUEUE_ADDR_HI,
3186 						   reo_dest_ring->info0)) |
3187 			      FIELD_PREP(HAL_REO_ENTR_RING_INFO0_DEST_IND, dst_idx);
3188 	ath11k_hal_srng_access_end(ab, srng);
3189 	spin_unlock_bh(&srng->lock);
3190 
3191 	return 0;
3192 
3193 err_free_idr:
3194 	spin_lock_bh(&rx_refill_ring->idr_lock);
3195 	idr_remove(&rx_refill_ring->bufs_idr, buf_id);
3196 	spin_unlock_bh(&rx_refill_ring->idr_lock);
3197 err_unmap_dma:
3198 	dma_unmap_single(ab->dev, paddr, defrag_skb->len + skb_tailroom(defrag_skb),
3199 			 DMA_FROM_DEVICE);
3200 	return ret;
3201 }
3202 
3203 static int ath11k_dp_rx_h_cmp_frags(struct sk_buff *a, struct sk_buff *b)
3204 {
3205 	int frag1, frag2;
3206 
3207 	frag1 = ath11k_dp_rx_h_mpdu_start_frag_no(a);
3208 	frag2 = ath11k_dp_rx_h_mpdu_start_frag_no(b);
3209 
3210 	return frag1 - frag2;
3211 }
3212 
3213 static void ath11k_dp_rx_h_sort_frags(struct sk_buff_head *frag_list,
3214 				      struct sk_buff *cur_frag)
3215 {
3216 	struct sk_buff *skb;
3217 	int cmp;
3218 
3219 	skb_queue_walk(frag_list, skb) {
3220 		cmp = ath11k_dp_rx_h_cmp_frags(skb, cur_frag);
3221 		if (cmp < 0)
3222 			continue;
3223 		__skb_queue_before(frag_list, skb, cur_frag);
3224 		return;
3225 	}
3226 	__skb_queue_tail(frag_list, cur_frag);
3227 }
3228 
3229 static u64 ath11k_dp_rx_h_get_pn(struct sk_buff *skb)
3230 {
3231 	struct ieee80211_hdr *hdr;
3232 	u64 pn = 0;
3233 	u8 *ehdr;
3234 
3235 	hdr = (struct ieee80211_hdr *)(skb->data + HAL_RX_DESC_SIZE);
3236 	ehdr = skb->data + HAL_RX_DESC_SIZE + ieee80211_hdrlen(hdr->frame_control);
3237 
3238 	pn = ehdr[0];
3239 	pn |= (u64)ehdr[1] << 8;
3240 	pn |= (u64)ehdr[4] << 16;
3241 	pn |= (u64)ehdr[5] << 24;
3242 	pn |= (u64)ehdr[6] << 32;
3243 	pn |= (u64)ehdr[7] << 40;
3244 
3245 	return pn;
3246 }
3247 
3248 static bool
3249 ath11k_dp_rx_h_defrag_validate_incr_pn(struct ath11k *ar, struct dp_rx_tid *rx_tid)
3250 {
3251 	enum hal_encrypt_type encrypt_type;
3252 	struct sk_buff *first_frag, *skb;
3253 	struct hal_rx_desc *desc;
3254 	u64 last_pn;
3255 	u64 cur_pn;
3256 
3257 	first_frag = skb_peek(&rx_tid->rx_frags);
3258 	desc = (struct hal_rx_desc *)first_frag->data;
3259 
3260 	encrypt_type = ath11k_dp_rx_h_mpdu_start_enctype(desc);
3261 	if (encrypt_type != HAL_ENCRYPT_TYPE_CCMP_128 &&
3262 	    encrypt_type != HAL_ENCRYPT_TYPE_CCMP_256 &&
3263 	    encrypt_type != HAL_ENCRYPT_TYPE_GCMP_128 &&
3264 	    encrypt_type != HAL_ENCRYPT_TYPE_AES_GCMP_256)
3265 		return true;
3266 
3267 	last_pn = ath11k_dp_rx_h_get_pn(first_frag);
3268 	skb_queue_walk(&rx_tid->rx_frags, skb) {
3269 		if (skb == first_frag)
3270 			continue;
3271 
3272 		cur_pn = ath11k_dp_rx_h_get_pn(skb);
3273 		if (cur_pn != last_pn + 1)
3274 			return false;
3275 		last_pn = cur_pn;
3276 	}
3277 	return true;
3278 }
3279 
3280 static int ath11k_dp_rx_frag_h_mpdu(struct ath11k *ar,
3281 				    struct sk_buff *msdu,
3282 				    u32 *ring_desc)
3283 {
3284 	struct ath11k_base *ab = ar->ab;
3285 	struct hal_rx_desc *rx_desc;
3286 	struct ath11k_peer *peer;
3287 	struct dp_rx_tid *rx_tid;
3288 	struct sk_buff *defrag_skb = NULL;
3289 	u32 peer_id;
3290 	u16 seqno, frag_no;
3291 	u8 tid;
3292 	int ret = 0;
3293 	bool more_frags;
3294 
3295 	rx_desc = (struct hal_rx_desc *)msdu->data;
3296 	peer_id = ath11k_dp_rx_h_mpdu_start_peer_id(rx_desc);
3297 	tid = ath11k_dp_rx_h_mpdu_start_tid(rx_desc);
3298 	seqno = ath11k_dp_rx_h_mpdu_start_seq_no(rx_desc);
3299 	frag_no = ath11k_dp_rx_h_mpdu_start_frag_no(msdu);
3300 	more_frags = ath11k_dp_rx_h_mpdu_start_more_frags(msdu);
3301 
3302 	if (!ath11k_dp_rx_h_mpdu_start_seq_ctrl_valid(rx_desc) ||
3303 	    !ath11k_dp_rx_h_mpdu_start_fc_valid(rx_desc) ||
3304 	    tid > IEEE80211_NUM_TIDS)
3305 		return -EINVAL;
3306 
3307 	/* received unfragmented packet in reo
3308 	 * exception ring, this shouldn't happen
3309 	 * as these packets typically come from
3310 	 * reo2sw srngs.
3311 	 */
3312 	if (WARN_ON_ONCE(!frag_no && !more_frags))
3313 		return -EINVAL;
3314 
3315 	spin_lock_bh(&ab->base_lock);
3316 	peer = ath11k_peer_find_by_id(ab, peer_id);
3317 	if (!peer) {
3318 		ath11k_warn(ab, "failed to find the peer to de-fragment received fragment peer_id %d\n",
3319 			    peer_id);
3320 		ret = -ENOENT;
3321 		goto out_unlock;
3322 	}
3323 	rx_tid = &peer->rx_tid[tid];
3324 
3325 	if ((!skb_queue_empty(&rx_tid->rx_frags) && seqno != rx_tid->cur_sn) ||
3326 	    skb_queue_empty(&rx_tid->rx_frags)) {
3327 		/* Flush stored fragments and start a new sequence */
3328 		ath11k_dp_rx_frags_cleanup(rx_tid, true);
3329 		rx_tid->cur_sn = seqno;
3330 	}
3331 
3332 	if (rx_tid->rx_frag_bitmap & BIT(frag_no)) {
3333 		/* Fragment already present */
3334 		ret = -EINVAL;
3335 		goto out_unlock;
3336 	}
3337 
3338 	if (frag_no > __fls(rx_tid->rx_frag_bitmap))
3339 		__skb_queue_tail(&rx_tid->rx_frags, msdu);
3340 	else
3341 		ath11k_dp_rx_h_sort_frags(&rx_tid->rx_frags, msdu);
3342 
3343 	rx_tid->rx_frag_bitmap |= BIT(frag_no);
3344 	if (!more_frags)
3345 		rx_tid->last_frag_no = frag_no;
3346 
3347 	if (frag_no == 0) {
3348 		rx_tid->dst_ring_desc = kmemdup(ring_desc,
3349 						sizeof(*rx_tid->dst_ring_desc),
3350 						GFP_ATOMIC);
3351 		if (!rx_tid->dst_ring_desc) {
3352 			ret = -ENOMEM;
3353 			goto out_unlock;
3354 		}
3355 	} else {
3356 		ath11k_dp_rx_link_desc_return(ab, ring_desc,
3357 					      HAL_WBM_REL_BM_ACT_PUT_IN_IDLE);
3358 	}
3359 
3360 	if (!rx_tid->last_frag_no ||
3361 	    rx_tid->rx_frag_bitmap != GENMASK(rx_tid->last_frag_no, 0)) {
3362 		mod_timer(&rx_tid->frag_timer, jiffies +
3363 					       ATH11K_DP_RX_FRAGMENT_TIMEOUT_MS);
3364 		goto out_unlock;
3365 	}
3366 
3367 	spin_unlock_bh(&ab->base_lock);
3368 	del_timer_sync(&rx_tid->frag_timer);
3369 	spin_lock_bh(&ab->base_lock);
3370 
3371 	peer = ath11k_peer_find_by_id(ab, peer_id);
3372 	if (!peer)
3373 		goto err_frags_cleanup;
3374 
3375 	if (!ath11k_dp_rx_h_defrag_validate_incr_pn(ar, rx_tid))
3376 		goto err_frags_cleanup;
3377 
3378 	if (ath11k_dp_rx_h_defrag(ar, peer, rx_tid, &defrag_skb))
3379 		goto err_frags_cleanup;
3380 
3381 	if (!defrag_skb)
3382 		goto err_frags_cleanup;
3383 
3384 	if (ath11k_dp_rx_h_defrag_reo_reinject(ar, rx_tid, defrag_skb))
3385 		goto err_frags_cleanup;
3386 
3387 	ath11k_dp_rx_frags_cleanup(rx_tid, false);
3388 	goto out_unlock;
3389 
3390 err_frags_cleanup:
3391 	dev_kfree_skb_any(defrag_skb);
3392 	ath11k_dp_rx_frags_cleanup(rx_tid, true);
3393 out_unlock:
3394 	spin_unlock_bh(&ab->base_lock);
3395 	return ret;
3396 }
3397 
3398 static int
3399 ath11k_dp_process_rx_err_buf(struct ath11k *ar, u32 *ring_desc, int buf_id, bool drop)
3400 {
3401 	struct ath11k_pdev_dp *dp = &ar->dp;
3402 	struct dp_rxdma_ring *rx_ring = &dp->rx_refill_buf_ring;
3403 	struct sk_buff *msdu;
3404 	struct ath11k_skb_rxcb *rxcb;
3405 	struct hal_rx_desc *rx_desc;
3406 	u8 *hdr_status;
3407 	u16 msdu_len;
3408 
3409 	spin_lock_bh(&rx_ring->idr_lock);
3410 	msdu = idr_find(&rx_ring->bufs_idr, buf_id);
3411 	if (!msdu) {
3412 		ath11k_warn(ar->ab, "rx err buf with invalid buf_id %d\n",
3413 			    buf_id);
3414 		spin_unlock_bh(&rx_ring->idr_lock);
3415 		return -EINVAL;
3416 	}
3417 
3418 	idr_remove(&rx_ring->bufs_idr, buf_id);
3419 	spin_unlock_bh(&rx_ring->idr_lock);
3420 
3421 	rxcb = ATH11K_SKB_RXCB(msdu);
3422 	dma_unmap_single(ar->ab->dev, rxcb->paddr,
3423 			 msdu->len + skb_tailroom(msdu),
3424 			 DMA_FROM_DEVICE);
3425 
3426 	if (drop) {
3427 		dev_kfree_skb_any(msdu);
3428 		return 0;
3429 	}
3430 
3431 	rcu_read_lock();
3432 	if (!rcu_dereference(ar->ab->pdevs_active[ar->pdev_idx])) {
3433 		dev_kfree_skb_any(msdu);
3434 		goto exit;
3435 	}
3436 
3437 	if (test_bit(ATH11K_CAC_RUNNING, &ar->dev_flags)) {
3438 		dev_kfree_skb_any(msdu);
3439 		goto exit;
3440 	}
3441 
3442 	rx_desc = (struct hal_rx_desc *)msdu->data;
3443 	msdu_len = ath11k_dp_rx_h_msdu_start_msdu_len(rx_desc);
3444 	if ((msdu_len + HAL_RX_DESC_SIZE) > DP_RX_BUFFER_SIZE) {
3445 		hdr_status = ath11k_dp_rx_h_80211_hdr(rx_desc);
3446 		ath11k_warn(ar->ab, "invalid msdu leng %u", msdu_len);
3447 		ath11k_dbg_dump(ar->ab, ATH11K_DBG_DATA, NULL, "", hdr_status,
3448 				sizeof(struct ieee80211_hdr));
3449 		ath11k_dbg_dump(ar->ab, ATH11K_DBG_DATA, NULL, "", rx_desc,
3450 				sizeof(struct hal_rx_desc));
3451 		dev_kfree_skb_any(msdu);
3452 		goto exit;
3453 	}
3454 
3455 	skb_put(msdu, HAL_RX_DESC_SIZE + msdu_len);
3456 
3457 	if (ath11k_dp_rx_frag_h_mpdu(ar, msdu, ring_desc)) {
3458 		dev_kfree_skb_any(msdu);
3459 		ath11k_dp_rx_link_desc_return(ar->ab, ring_desc,
3460 					      HAL_WBM_REL_BM_ACT_PUT_IN_IDLE);
3461 	}
3462 exit:
3463 	rcu_read_unlock();
3464 	return 0;
3465 }
3466 
3467 int ath11k_dp_process_rx_err(struct ath11k_base *ab, struct napi_struct *napi,
3468 			     int budget)
3469 {
3470 	u32 msdu_cookies[HAL_NUM_RX_MSDUS_PER_LINK_DESC];
3471 	struct dp_link_desc_bank *link_desc_banks;
3472 	enum hal_rx_buf_return_buf_manager rbm;
3473 	int tot_n_bufs_reaped, quota, ret, i;
3474 	int n_bufs_reaped[MAX_RADIOS] = {0};
3475 	struct dp_rxdma_ring *rx_ring;
3476 	struct dp_srng *reo_except;
3477 	u32 desc_bank, num_msdus;
3478 	struct hal_srng *srng;
3479 	struct ath11k_dp *dp;
3480 	void *link_desc_va;
3481 	int buf_id, mac_id;
3482 	struct ath11k *ar;
3483 	dma_addr_t paddr;
3484 	u32 *desc;
3485 	bool is_frag;
3486 	u8 drop = 0;
3487 
3488 	tot_n_bufs_reaped = 0;
3489 	quota = budget;
3490 
3491 	dp = &ab->dp;
3492 	reo_except = &dp->reo_except_ring;
3493 	link_desc_banks = dp->link_desc_banks;
3494 
3495 	srng = &ab->hal.srng_list[reo_except->ring_id];
3496 
3497 	spin_lock_bh(&srng->lock);
3498 
3499 	ath11k_hal_srng_access_begin(ab, srng);
3500 
3501 	while (budget &&
3502 	       (desc = ath11k_hal_srng_dst_get_next_entry(ab, srng))) {
3503 		struct hal_reo_dest_ring *reo_desc = (struct hal_reo_dest_ring *)desc;
3504 
3505 		ab->soc_stats.err_ring_pkts++;
3506 		ret = ath11k_hal_desc_reo_parse_err(ab, desc, &paddr,
3507 						    &desc_bank);
3508 		if (ret) {
3509 			ath11k_warn(ab, "failed to parse error reo desc %d\n",
3510 				    ret);
3511 			continue;
3512 		}
3513 		link_desc_va = link_desc_banks[desc_bank].vaddr +
3514 			       (paddr - link_desc_banks[desc_bank].paddr);
3515 		ath11k_hal_rx_msdu_link_info_get(link_desc_va, &num_msdus, msdu_cookies,
3516 						 &rbm);
3517 		if (rbm != HAL_RX_BUF_RBM_WBM_IDLE_DESC_LIST &&
3518 		    rbm != HAL_RX_BUF_RBM_SW3_BM) {
3519 			ab->soc_stats.invalid_rbm++;
3520 			ath11k_warn(ab, "invalid return buffer manager %d\n", rbm);
3521 			ath11k_dp_rx_link_desc_return(ab, desc,
3522 						      HAL_WBM_REL_BM_ACT_REL_MSDU);
3523 			continue;
3524 		}
3525 
3526 		is_frag = !!(reo_desc->rx_mpdu_info.info0 & RX_MPDU_DESC_INFO0_FRAG_FLAG);
3527 
3528 		/* Process only rx fragments with one msdu per link desc below, and drop
3529 		 * msdu's indicated due to error reasons.
3530 		 */
3531 		if (!is_frag || num_msdus > 1) {
3532 			drop = 1;
3533 			/* Return the link desc back to wbm idle list */
3534 			ath11k_dp_rx_link_desc_return(ab, desc,
3535 						      HAL_WBM_REL_BM_ACT_PUT_IN_IDLE);
3536 		}
3537 
3538 		for (i = 0; i < num_msdus; i++) {
3539 			buf_id = FIELD_GET(DP_RXDMA_BUF_COOKIE_BUF_ID,
3540 					   msdu_cookies[i]);
3541 
3542 			mac_id = FIELD_GET(DP_RXDMA_BUF_COOKIE_PDEV_ID,
3543 					   msdu_cookies[i]);
3544 
3545 			ar = ab->pdevs[mac_id].ar;
3546 
3547 			if (!ath11k_dp_process_rx_err_buf(ar, desc, buf_id, drop)) {
3548 				n_bufs_reaped[mac_id]++;
3549 				tot_n_bufs_reaped++;
3550 			}
3551 		}
3552 
3553 		if (tot_n_bufs_reaped >= quota) {
3554 			tot_n_bufs_reaped = quota;
3555 			goto exit;
3556 		}
3557 
3558 		budget = quota - tot_n_bufs_reaped;
3559 	}
3560 
3561 exit:
3562 	ath11k_hal_srng_access_end(ab, srng);
3563 
3564 	spin_unlock_bh(&srng->lock);
3565 
3566 	for (i = 0; i <  ab->num_radios; i++) {
3567 		if (!n_bufs_reaped[i])
3568 			continue;
3569 
3570 		ar = ab->pdevs[i].ar;
3571 		rx_ring = &ar->dp.rx_refill_buf_ring;
3572 
3573 		ath11k_dp_rxbufs_replenish(ab, i, rx_ring, n_bufs_reaped[i],
3574 					   HAL_RX_BUF_RBM_SW3_BM, GFP_ATOMIC);
3575 	}
3576 
3577 	return tot_n_bufs_reaped;
3578 }
3579 
3580 static void ath11k_dp_rx_null_q_desc_sg_drop(struct ath11k *ar,
3581 					     int msdu_len,
3582 					     struct sk_buff_head *msdu_list)
3583 {
3584 	struct sk_buff *skb, *tmp;
3585 	struct ath11k_skb_rxcb *rxcb;
3586 	int n_buffs;
3587 
3588 	n_buffs = DIV_ROUND_UP(msdu_len,
3589 			       (DP_RX_BUFFER_SIZE - HAL_RX_DESC_SIZE));
3590 
3591 	skb_queue_walk_safe(msdu_list, skb, tmp) {
3592 		rxcb = ATH11K_SKB_RXCB(skb);
3593 		if (rxcb->err_rel_src == HAL_WBM_REL_SRC_MODULE_REO &&
3594 		    rxcb->err_code == HAL_REO_DEST_RING_ERROR_CODE_DESC_ADDR_ZERO) {
3595 			if (!n_buffs)
3596 				break;
3597 			__skb_unlink(skb, msdu_list);
3598 			dev_kfree_skb_any(skb);
3599 			n_buffs--;
3600 		}
3601 	}
3602 }
3603 
3604 static int ath11k_dp_rx_h_null_q_desc(struct ath11k *ar, struct sk_buff *msdu,
3605 				      struct ieee80211_rx_status *status,
3606 				      struct sk_buff_head *msdu_list)
3607 {
3608 	u16 msdu_len;
3609 	struct hal_rx_desc *desc = (struct hal_rx_desc *)msdu->data;
3610 	u8 l3pad_bytes;
3611 	struct ath11k_skb_rxcb *rxcb = ATH11K_SKB_RXCB(msdu);
3612 
3613 	msdu_len = ath11k_dp_rx_h_msdu_start_msdu_len(desc);
3614 
3615 	if (!rxcb->is_frag && ((msdu_len + HAL_RX_DESC_SIZE) > DP_RX_BUFFER_SIZE)) {
3616 		/* First buffer will be freed by the caller, so deduct it's length */
3617 		msdu_len = msdu_len - (DP_RX_BUFFER_SIZE - HAL_RX_DESC_SIZE);
3618 		ath11k_dp_rx_null_q_desc_sg_drop(ar, msdu_len, msdu_list);
3619 		return -EINVAL;
3620 	}
3621 
3622 	if (!ath11k_dp_rx_h_attn_msdu_done(desc)) {
3623 		ath11k_warn(ar->ab,
3624 			    "msdu_done bit not set in null_q_des processing\n");
3625 		__skb_queue_purge(msdu_list);
3626 		return -EIO;
3627 	}
3628 
3629 	/* Handle NULL queue descriptor violations arising out a missing
3630 	 * REO queue for a given peer or a given TID. This typically
3631 	 * may happen if a packet is received on a QOS enabled TID before the
3632 	 * ADDBA negotiation for that TID, when the TID queue is setup. Or
3633 	 * it may also happen for MC/BC frames if they are not routed to the
3634 	 * non-QOS TID queue, in the absence of any other default TID queue.
3635 	 * This error can show up both in a REO destination or WBM release ring.
3636 	 */
3637 
3638 	rxcb->is_first_msdu = ath11k_dp_rx_h_msdu_end_first_msdu(desc);
3639 	rxcb->is_last_msdu = ath11k_dp_rx_h_msdu_end_last_msdu(desc);
3640 
3641 	if (rxcb->is_frag) {
3642 		skb_pull(msdu, HAL_RX_DESC_SIZE);
3643 	} else {
3644 		l3pad_bytes = ath11k_dp_rx_h_msdu_end_l3pad(desc);
3645 
3646 		if ((HAL_RX_DESC_SIZE + l3pad_bytes + msdu_len) > DP_RX_BUFFER_SIZE)
3647 			return -EINVAL;
3648 
3649 		skb_put(msdu, HAL_RX_DESC_SIZE + l3pad_bytes + msdu_len);
3650 		skb_pull(msdu, HAL_RX_DESC_SIZE + l3pad_bytes);
3651 	}
3652 	ath11k_dp_rx_h_ppdu(ar, desc, status);
3653 
3654 	ath11k_dp_rx_h_mpdu(ar, msdu, desc, status);
3655 
3656 	rxcb->tid = ath11k_dp_rx_h_mpdu_start_tid(desc);
3657 
3658 	/* Please note that caller will having the access to msdu and completing
3659 	 * rx with mac80211. Need not worry about cleaning up amsdu_list.
3660 	 */
3661 
3662 	return 0;
3663 }
3664 
3665 static bool ath11k_dp_rx_h_reo_err(struct ath11k *ar, struct sk_buff *msdu,
3666 				   struct ieee80211_rx_status *status,
3667 				   struct sk_buff_head *msdu_list)
3668 {
3669 	struct ath11k_skb_rxcb *rxcb = ATH11K_SKB_RXCB(msdu);
3670 	bool drop = false;
3671 
3672 	ar->ab->soc_stats.reo_error[rxcb->err_code]++;
3673 
3674 	switch (rxcb->err_code) {
3675 	case HAL_REO_DEST_RING_ERROR_CODE_DESC_ADDR_ZERO:
3676 		if (ath11k_dp_rx_h_null_q_desc(ar, msdu, status, msdu_list))
3677 			drop = true;
3678 		break;
3679 	case HAL_REO_DEST_RING_ERROR_CODE_PN_CHECK_FAILED:
3680 		/* TODO: Do not drop PN failed packets in the driver;
3681 		 * instead, it is good to drop such packets in mac80211
3682 		 * after incrementing the replay counters.
3683 		 */
3684 
3685 		/* fall through */
3686 	default:
3687 		/* TODO: Review other errors and process them to mac80211
3688 		 * as appropriate.
3689 		 */
3690 		drop = true;
3691 		break;
3692 	}
3693 
3694 	return drop;
3695 }
3696 
3697 static void ath11k_dp_rx_h_tkip_mic_err(struct ath11k *ar, struct sk_buff *msdu,
3698 					struct ieee80211_rx_status *status)
3699 {
3700 	u16 msdu_len;
3701 	struct hal_rx_desc *desc = (struct hal_rx_desc *)msdu->data;
3702 	u8 l3pad_bytes;
3703 	struct ath11k_skb_rxcb *rxcb = ATH11K_SKB_RXCB(msdu);
3704 
3705 	rxcb->is_first_msdu = ath11k_dp_rx_h_msdu_end_first_msdu(desc);
3706 	rxcb->is_last_msdu = ath11k_dp_rx_h_msdu_end_last_msdu(desc);
3707 
3708 	l3pad_bytes = ath11k_dp_rx_h_msdu_end_l3pad(desc);
3709 	msdu_len = ath11k_dp_rx_h_msdu_start_msdu_len(desc);
3710 	skb_put(msdu, HAL_RX_DESC_SIZE + l3pad_bytes + msdu_len);
3711 	skb_pull(msdu, HAL_RX_DESC_SIZE + l3pad_bytes);
3712 
3713 	ath11k_dp_rx_h_ppdu(ar, desc, status);
3714 
3715 	status->flag |= (RX_FLAG_MMIC_STRIPPED | RX_FLAG_MMIC_ERROR |
3716 			 RX_FLAG_DECRYPTED);
3717 
3718 	ath11k_dp_rx_h_undecap(ar, msdu, desc,
3719 			       HAL_ENCRYPT_TYPE_TKIP_MIC, status, false);
3720 }
3721 
3722 static bool ath11k_dp_rx_h_rxdma_err(struct ath11k *ar,  struct sk_buff *msdu,
3723 				     struct ieee80211_rx_status *status)
3724 {
3725 	struct ath11k_skb_rxcb *rxcb = ATH11K_SKB_RXCB(msdu);
3726 	bool drop = false;
3727 
3728 	ar->ab->soc_stats.rxdma_error[rxcb->err_code]++;
3729 
3730 	switch (rxcb->err_code) {
3731 	case HAL_REO_ENTR_RING_RXDMA_ECODE_TKIP_MIC_ERR:
3732 		ath11k_dp_rx_h_tkip_mic_err(ar, msdu, status);
3733 		break;
3734 	default:
3735 		/* TODO: Review other rxdma error code to check if anything is
3736 		 * worth reporting to mac80211
3737 		 */
3738 		drop = true;
3739 		break;
3740 	}
3741 
3742 	return drop;
3743 }
3744 
3745 static void ath11k_dp_rx_wbm_err(struct ath11k *ar,
3746 				 struct napi_struct *napi,
3747 				 struct sk_buff *msdu,
3748 				 struct sk_buff_head *msdu_list)
3749 {
3750 	struct ath11k_skb_rxcb *rxcb = ATH11K_SKB_RXCB(msdu);
3751 	struct ieee80211_rx_status rxs = {0};
3752 	struct ieee80211_rx_status *status;
3753 	bool drop = true;
3754 
3755 	switch (rxcb->err_rel_src) {
3756 	case HAL_WBM_REL_SRC_MODULE_REO:
3757 		drop = ath11k_dp_rx_h_reo_err(ar, msdu, &rxs, msdu_list);
3758 		break;
3759 	case HAL_WBM_REL_SRC_MODULE_RXDMA:
3760 		drop = ath11k_dp_rx_h_rxdma_err(ar, msdu, &rxs);
3761 		break;
3762 	default:
3763 		/* msdu will get freed */
3764 		break;
3765 	}
3766 
3767 	if (drop) {
3768 		dev_kfree_skb_any(msdu);
3769 		return;
3770 	}
3771 
3772 	status = IEEE80211_SKB_RXCB(msdu);
3773 	*status = rxs;
3774 
3775 	ath11k_dp_rx_deliver_msdu(ar, napi, msdu);
3776 }
3777 
3778 int ath11k_dp_rx_process_wbm_err(struct ath11k_base *ab,
3779 				 struct napi_struct *napi, int budget)
3780 {
3781 	struct ath11k *ar;
3782 	struct ath11k_dp *dp = &ab->dp;
3783 	struct dp_rxdma_ring *rx_ring;
3784 	struct hal_rx_wbm_rel_info err_info;
3785 	struct hal_srng *srng;
3786 	struct sk_buff *msdu;
3787 	struct sk_buff_head msdu_list[MAX_RADIOS];
3788 	struct ath11k_skb_rxcb *rxcb;
3789 	u32 *rx_desc;
3790 	int buf_id, mac_id;
3791 	int num_buffs_reaped[MAX_RADIOS] = {0};
3792 	int total_num_buffs_reaped = 0;
3793 	int ret, i;
3794 
3795 	for (i = 0; i < MAX_RADIOS; i++)
3796 		__skb_queue_head_init(&msdu_list[i]);
3797 
3798 	srng = &ab->hal.srng_list[dp->rx_rel_ring.ring_id];
3799 
3800 	spin_lock_bh(&srng->lock);
3801 
3802 	ath11k_hal_srng_access_begin(ab, srng);
3803 
3804 	while (budget) {
3805 		rx_desc = ath11k_hal_srng_dst_get_next_entry(ab, srng);
3806 		if (!rx_desc)
3807 			break;
3808 
3809 		ret = ath11k_hal_wbm_desc_parse_err(ab, rx_desc, &err_info);
3810 		if (ret) {
3811 			ath11k_warn(ab,
3812 				    "failed to parse rx error in wbm_rel ring desc %d\n",
3813 				    ret);
3814 			continue;
3815 		}
3816 
3817 		buf_id = FIELD_GET(DP_RXDMA_BUF_COOKIE_BUF_ID, err_info.cookie);
3818 		mac_id = FIELD_GET(DP_RXDMA_BUF_COOKIE_PDEV_ID, err_info.cookie);
3819 
3820 		ar = ab->pdevs[mac_id].ar;
3821 		rx_ring = &ar->dp.rx_refill_buf_ring;
3822 
3823 		spin_lock_bh(&rx_ring->idr_lock);
3824 		msdu = idr_find(&rx_ring->bufs_idr, buf_id);
3825 		if (!msdu) {
3826 			ath11k_warn(ab, "frame rx with invalid buf_id %d pdev %d\n",
3827 				    buf_id, mac_id);
3828 			spin_unlock_bh(&rx_ring->idr_lock);
3829 			continue;
3830 		}
3831 
3832 		idr_remove(&rx_ring->bufs_idr, buf_id);
3833 		spin_unlock_bh(&rx_ring->idr_lock);
3834 
3835 		rxcb = ATH11K_SKB_RXCB(msdu);
3836 		dma_unmap_single(ab->dev, rxcb->paddr,
3837 				 msdu->len + skb_tailroom(msdu),
3838 				 DMA_FROM_DEVICE);
3839 
3840 		num_buffs_reaped[mac_id]++;
3841 		total_num_buffs_reaped++;
3842 		budget--;
3843 
3844 		if (err_info.push_reason !=
3845 		    HAL_REO_DEST_RING_PUSH_REASON_ERR_DETECTED) {
3846 			dev_kfree_skb_any(msdu);
3847 			continue;
3848 		}
3849 
3850 		rxcb->err_rel_src = err_info.err_rel_src;
3851 		rxcb->err_code = err_info.err_code;
3852 		rxcb->rx_desc = (struct hal_rx_desc *)msdu->data;
3853 		__skb_queue_tail(&msdu_list[mac_id], msdu);
3854 	}
3855 
3856 	ath11k_hal_srng_access_end(ab, srng);
3857 
3858 	spin_unlock_bh(&srng->lock);
3859 
3860 	if (!total_num_buffs_reaped)
3861 		goto done;
3862 
3863 	for (i = 0; i <  ab->num_radios; i++) {
3864 		if (!num_buffs_reaped[i])
3865 			continue;
3866 
3867 		ar = ab->pdevs[i].ar;
3868 		rx_ring = &ar->dp.rx_refill_buf_ring;
3869 
3870 		ath11k_dp_rxbufs_replenish(ab, i, rx_ring, num_buffs_reaped[i],
3871 					   HAL_RX_BUF_RBM_SW3_BM, GFP_ATOMIC);
3872 	}
3873 
3874 	rcu_read_lock();
3875 	for (i = 0; i <  ab->num_radios; i++) {
3876 		if (!rcu_dereference(ab->pdevs_active[i])) {
3877 			__skb_queue_purge(&msdu_list[i]);
3878 			continue;
3879 		}
3880 
3881 		ar = ab->pdevs[i].ar;
3882 
3883 		if (test_bit(ATH11K_CAC_RUNNING, &ar->dev_flags)) {
3884 			__skb_queue_purge(&msdu_list[i]);
3885 			continue;
3886 		}
3887 
3888 		while ((msdu = __skb_dequeue(&msdu_list[i])) != NULL)
3889 			ath11k_dp_rx_wbm_err(ar, napi, msdu, &msdu_list[i]);
3890 	}
3891 	rcu_read_unlock();
3892 done:
3893 	return total_num_buffs_reaped;
3894 }
3895 
3896 int ath11k_dp_process_rxdma_err(struct ath11k_base *ab, int mac_id, int budget)
3897 {
3898 	struct ath11k *ar = ab->pdevs[mac_id].ar;
3899 	struct dp_srng *err_ring = &ar->dp.rxdma_err_dst_ring;
3900 	struct dp_rxdma_ring *rx_ring = &ar->dp.rx_refill_buf_ring;
3901 	struct dp_link_desc_bank *link_desc_banks = ab->dp.link_desc_banks;
3902 	struct hal_srng *srng;
3903 	u32 msdu_cookies[HAL_NUM_RX_MSDUS_PER_LINK_DESC];
3904 	enum hal_rx_buf_return_buf_manager rbm;
3905 	enum hal_reo_entr_rxdma_ecode rxdma_err_code;
3906 	struct ath11k_skb_rxcb *rxcb;
3907 	struct sk_buff *skb;
3908 	struct hal_reo_entrance_ring *entr_ring;
3909 	void *desc;
3910 	int num_buf_freed = 0;
3911 	int quota = budget;
3912 	dma_addr_t paddr;
3913 	u32 desc_bank;
3914 	void *link_desc_va;
3915 	int num_msdus;
3916 	int i;
3917 	int buf_id;
3918 
3919 	srng = &ab->hal.srng_list[err_ring->ring_id];
3920 
3921 	spin_lock_bh(&srng->lock);
3922 
3923 	ath11k_hal_srng_access_begin(ab, srng);
3924 
3925 	while (quota-- &&
3926 	       (desc = ath11k_hal_srng_dst_get_next_entry(ab, srng))) {
3927 		ath11k_hal_rx_reo_ent_paddr_get(ab, desc, &paddr, &desc_bank);
3928 
3929 		entr_ring = (struct hal_reo_entrance_ring *)desc;
3930 		rxdma_err_code =
3931 			FIELD_GET(HAL_REO_ENTR_RING_INFO1_RXDMA_ERROR_CODE,
3932 				  entr_ring->info1);
3933 		ab->soc_stats.rxdma_error[rxdma_err_code]++;
3934 
3935 		link_desc_va = link_desc_banks[desc_bank].vaddr +
3936 			       (paddr - link_desc_banks[desc_bank].paddr);
3937 		ath11k_hal_rx_msdu_link_info_get(link_desc_va, &num_msdus,
3938 						 msdu_cookies, &rbm);
3939 
3940 		for (i = 0; i < num_msdus; i++) {
3941 			buf_id = FIELD_GET(DP_RXDMA_BUF_COOKIE_BUF_ID,
3942 					   msdu_cookies[i]);
3943 
3944 			spin_lock_bh(&rx_ring->idr_lock);
3945 			skb = idr_find(&rx_ring->bufs_idr, buf_id);
3946 			if (!skb) {
3947 				ath11k_warn(ab, "rxdma error with invalid buf_id %d\n",
3948 					    buf_id);
3949 				spin_unlock_bh(&rx_ring->idr_lock);
3950 				continue;
3951 			}
3952 
3953 			idr_remove(&rx_ring->bufs_idr, buf_id);
3954 			spin_unlock_bh(&rx_ring->idr_lock);
3955 
3956 			rxcb = ATH11K_SKB_RXCB(skb);
3957 			dma_unmap_single(ab->dev, rxcb->paddr,
3958 					 skb->len + skb_tailroom(skb),
3959 					 DMA_FROM_DEVICE);
3960 			dev_kfree_skb_any(skb);
3961 
3962 			num_buf_freed++;
3963 		}
3964 
3965 		ath11k_dp_rx_link_desc_return(ab, desc,
3966 					      HAL_WBM_REL_BM_ACT_PUT_IN_IDLE);
3967 	}
3968 
3969 	ath11k_hal_srng_access_end(ab, srng);
3970 
3971 	spin_unlock_bh(&srng->lock);
3972 
3973 	if (num_buf_freed)
3974 		ath11k_dp_rxbufs_replenish(ab, mac_id, rx_ring, num_buf_freed,
3975 					   HAL_RX_BUF_RBM_SW3_BM, GFP_ATOMIC);
3976 
3977 	return budget - quota;
3978 }
3979 
3980 void ath11k_dp_process_reo_status(struct ath11k_base *ab)
3981 {
3982 	struct ath11k_dp *dp = &ab->dp;
3983 	struct hal_srng *srng;
3984 	struct dp_reo_cmd *cmd, *tmp;
3985 	bool found = false;
3986 	u32 *reo_desc;
3987 	u16 tag;
3988 	struct hal_reo_status reo_status;
3989 
3990 	srng = &ab->hal.srng_list[dp->reo_status_ring.ring_id];
3991 
3992 	memset(&reo_status, 0, sizeof(reo_status));
3993 
3994 	spin_lock_bh(&srng->lock);
3995 
3996 	ath11k_hal_srng_access_begin(ab, srng);
3997 
3998 	while ((reo_desc = ath11k_hal_srng_dst_get_next_entry(ab, srng))) {
3999 		tag = FIELD_GET(HAL_SRNG_TLV_HDR_TAG, *reo_desc);
4000 
4001 		switch (tag) {
4002 		case HAL_REO_GET_QUEUE_STATS_STATUS:
4003 			ath11k_hal_reo_status_queue_stats(ab, reo_desc,
4004 							  &reo_status);
4005 			break;
4006 		case HAL_REO_FLUSH_QUEUE_STATUS:
4007 			ath11k_hal_reo_flush_queue_status(ab, reo_desc,
4008 							  &reo_status);
4009 			break;
4010 		case HAL_REO_FLUSH_CACHE_STATUS:
4011 			ath11k_hal_reo_flush_cache_status(ab, reo_desc,
4012 							  &reo_status);
4013 			break;
4014 		case HAL_REO_UNBLOCK_CACHE_STATUS:
4015 			ath11k_hal_reo_unblk_cache_status(ab, reo_desc,
4016 							  &reo_status);
4017 			break;
4018 		case HAL_REO_FLUSH_TIMEOUT_LIST_STATUS:
4019 			ath11k_hal_reo_flush_timeout_list_status(ab, reo_desc,
4020 								 &reo_status);
4021 			break;
4022 		case HAL_REO_DESCRIPTOR_THRESHOLD_REACHED_STATUS:
4023 			ath11k_hal_reo_desc_thresh_reached_status(ab, reo_desc,
4024 								  &reo_status);
4025 			break;
4026 		case HAL_REO_UPDATE_RX_REO_QUEUE_STATUS:
4027 			ath11k_hal_reo_update_rx_reo_queue_status(ab, reo_desc,
4028 								  &reo_status);
4029 			break;
4030 		default:
4031 			ath11k_warn(ab, "Unknown reo status type %d\n", tag);
4032 			continue;
4033 		}
4034 
4035 		spin_lock_bh(&dp->reo_cmd_lock);
4036 		list_for_each_entry_safe(cmd, tmp, &dp->reo_cmd_list, list) {
4037 			if (reo_status.uniform_hdr.cmd_num == cmd->cmd_num) {
4038 				found = true;
4039 				list_del(&cmd->list);
4040 				break;
4041 			}
4042 		}
4043 		spin_unlock_bh(&dp->reo_cmd_lock);
4044 
4045 		if (found) {
4046 			cmd->handler(dp, (void *)&cmd->data,
4047 				     reo_status.uniform_hdr.cmd_status);
4048 			kfree(cmd);
4049 		}
4050 
4051 		found = false;
4052 	}
4053 
4054 	ath11k_hal_srng_access_end(ab, srng);
4055 
4056 	spin_unlock_bh(&srng->lock);
4057 }
4058 
4059 void ath11k_dp_rx_pdev_free(struct ath11k_base *ab, int mac_id)
4060 {
4061 	struct ath11k *ar = ab->pdevs[mac_id].ar;
4062 
4063 	ath11k_dp_rx_pdev_srng_free(ar);
4064 	ath11k_dp_rxdma_pdev_buf_free(ar);
4065 }
4066 
4067 int ath11k_dp_rx_pdev_alloc(struct ath11k_base *ab, int mac_id)
4068 {
4069 	struct ath11k *ar = ab->pdevs[mac_id].ar;
4070 	struct ath11k_pdev_dp *dp = &ar->dp;
4071 	u32 ring_id;
4072 	int ret;
4073 
4074 	ret = ath11k_dp_rx_pdev_srng_alloc(ar);
4075 	if (ret) {
4076 		ath11k_warn(ab, "failed to setup rx srngs\n");
4077 		return ret;
4078 	}
4079 
4080 	ret = ath11k_dp_rxdma_pdev_buf_setup(ar);
4081 	if (ret) {
4082 		ath11k_warn(ab, "failed to setup rxdma ring\n");
4083 		return ret;
4084 	}
4085 
4086 	ring_id = dp->rx_refill_buf_ring.refill_buf_ring.ring_id;
4087 	ret = ath11k_dp_tx_htt_srng_setup(ab, ring_id, mac_id, HAL_RXDMA_BUF);
4088 	if (ret) {
4089 		ath11k_warn(ab, "failed to configure rx_refill_buf_ring %d\n",
4090 			    ret);
4091 		return ret;
4092 	}
4093 
4094 	ring_id = dp->rxdma_err_dst_ring.ring_id;
4095 	ret = ath11k_dp_tx_htt_srng_setup(ab, ring_id, mac_id, HAL_RXDMA_DST);
4096 	if (ret) {
4097 		ath11k_warn(ab, "failed to configure rxdma_err_dest_ring %d\n",
4098 			    ret);
4099 		return ret;
4100 	}
4101 
4102 	ring_id = dp->rxdma_mon_buf_ring.refill_buf_ring.ring_id;
4103 	ret = ath11k_dp_tx_htt_srng_setup(ab, ring_id,
4104 					  mac_id, HAL_RXDMA_MONITOR_BUF);
4105 	if (ret) {
4106 		ath11k_warn(ab, "failed to configure rxdma_mon_buf_ring %d\n",
4107 			    ret);
4108 		return ret;
4109 	}
4110 	ret = ath11k_dp_tx_htt_srng_setup(ab,
4111 					  dp->rxdma_mon_dst_ring.ring_id,
4112 					  mac_id, HAL_RXDMA_MONITOR_DST);
4113 	if (ret) {
4114 		ath11k_warn(ab, "failed to configure rxdma_mon_dst_ring %d\n",
4115 			    ret);
4116 		return ret;
4117 	}
4118 	ret = ath11k_dp_tx_htt_srng_setup(ab,
4119 					  dp->rxdma_mon_desc_ring.ring_id,
4120 					  mac_id, HAL_RXDMA_MONITOR_DESC);
4121 	if (ret) {
4122 		ath11k_warn(ab, "failed to configure rxdma_mon_dst_ring %d\n",
4123 			    ret);
4124 		return ret;
4125 	}
4126 	ring_id = dp->rx_mon_status_refill_ring.refill_buf_ring.ring_id;
4127 	ret = ath11k_dp_tx_htt_srng_setup(ab, ring_id, mac_id,
4128 					  HAL_RXDMA_MONITOR_STATUS);
4129 	if (ret) {
4130 		ath11k_warn(ab,
4131 			    "failed to configure mon_status_refill_ring %d\n",
4132 			    ret);
4133 		return ret;
4134 	}
4135 	return 0;
4136 }
4137 
4138 static void ath11k_dp_mon_set_frag_len(u32 *total_len, u32 *frag_len)
4139 {
4140 	if (*total_len >= (DP_RX_BUFFER_SIZE - sizeof(struct hal_rx_desc))) {
4141 		*frag_len = DP_RX_BUFFER_SIZE - sizeof(struct hal_rx_desc);
4142 		*total_len -= *frag_len;
4143 	} else {
4144 		*frag_len = *total_len;
4145 		*total_len = 0;
4146 	}
4147 }
4148 
4149 static
4150 int ath11k_dp_rx_monitor_link_desc_return(struct ath11k *ar,
4151 					  void *p_last_buf_addr_info,
4152 					  u8 mac_id)
4153 {
4154 	struct ath11k_pdev_dp *dp = &ar->dp;
4155 	struct dp_srng *dp_srng;
4156 	void *hal_srng;
4157 	void *src_srng_desc;
4158 	int ret = 0;
4159 
4160 	dp_srng = &dp->rxdma_mon_desc_ring;
4161 	hal_srng = &ar->ab->hal.srng_list[dp_srng->ring_id];
4162 
4163 	ath11k_hal_srng_access_begin(ar->ab, hal_srng);
4164 
4165 	src_srng_desc = ath11k_hal_srng_src_get_next_entry(ar->ab, hal_srng);
4166 
4167 	if (src_srng_desc) {
4168 		struct ath11k_buffer_addr *src_desc =
4169 				(struct ath11k_buffer_addr *)src_srng_desc;
4170 
4171 		*src_desc = *((struct ath11k_buffer_addr *)p_last_buf_addr_info);
4172 	} else {
4173 		ath11k_dbg(ar->ab, ATH11K_DBG_DATA,
4174 			   "Monitor Link Desc Ring %d Full", mac_id);
4175 		ret = -ENOMEM;
4176 	}
4177 
4178 	ath11k_hal_srng_access_end(ar->ab, hal_srng);
4179 	return ret;
4180 }
4181 
4182 static
4183 void ath11k_dp_rx_mon_next_link_desc_get(void *rx_msdu_link_desc,
4184 					 dma_addr_t *paddr, u32 *sw_cookie,
4185 					 void **pp_buf_addr_info)
4186 {
4187 	struct hal_rx_msdu_link *msdu_link =
4188 			(struct hal_rx_msdu_link *)rx_msdu_link_desc;
4189 	struct ath11k_buffer_addr *buf_addr_info;
4190 	u8 rbm = 0;
4191 
4192 	buf_addr_info = (struct ath11k_buffer_addr *)&msdu_link->buf_addr_info;
4193 
4194 	ath11k_hal_rx_buf_addr_info_get(buf_addr_info, paddr, sw_cookie, &rbm);
4195 
4196 	*pp_buf_addr_info = (void *)buf_addr_info;
4197 }
4198 
4199 static int ath11k_dp_pkt_set_pktlen(struct sk_buff *skb, u32 len)
4200 {
4201 	if (skb->len > len) {
4202 		skb_trim(skb, len);
4203 	} else {
4204 		if (skb_tailroom(skb) < len - skb->len) {
4205 			if ((pskb_expand_head(skb, 0,
4206 					      len - skb->len - skb_tailroom(skb),
4207 					      GFP_ATOMIC))) {
4208 				dev_kfree_skb_any(skb);
4209 				return -ENOMEM;
4210 			}
4211 		}
4212 		skb_put(skb, (len - skb->len));
4213 	}
4214 	return 0;
4215 }
4216 
4217 static void ath11k_hal_rx_msdu_list_get(struct ath11k *ar,
4218 					void *msdu_link_desc,
4219 					struct hal_rx_msdu_list *msdu_list,
4220 					u16 *num_msdus)
4221 {
4222 	struct hal_rx_msdu_details *msdu_details = NULL;
4223 	struct rx_msdu_desc *msdu_desc_info = NULL;
4224 	struct hal_rx_msdu_link *msdu_link = NULL;
4225 	int i;
4226 	u32 last = FIELD_PREP(RX_MSDU_DESC_INFO0_LAST_MSDU_IN_MPDU, 1);
4227 	u32 first = FIELD_PREP(RX_MSDU_DESC_INFO0_FIRST_MSDU_IN_MPDU, 1);
4228 	u8  tmp  = 0;
4229 
4230 	msdu_link = (struct hal_rx_msdu_link *)msdu_link_desc;
4231 	msdu_details = &msdu_link->msdu_link[0];
4232 
4233 	for (i = 0; i < HAL_RX_NUM_MSDU_DESC; i++) {
4234 		if (FIELD_GET(BUFFER_ADDR_INFO0_ADDR,
4235 			      msdu_details[i].buf_addr_info.info0) == 0) {
4236 			msdu_desc_info = &msdu_details[i - 1].rx_msdu_info;
4237 			msdu_desc_info->info0 |= last;
4238 			;
4239 			break;
4240 		}
4241 		msdu_desc_info = &msdu_details[i].rx_msdu_info;
4242 
4243 		if (!i)
4244 			msdu_desc_info->info0 |= first;
4245 		else if (i == (HAL_RX_NUM_MSDU_DESC - 1))
4246 			msdu_desc_info->info0 |= last;
4247 		msdu_list->msdu_info[i].msdu_flags = msdu_desc_info->info0;
4248 		msdu_list->msdu_info[i].msdu_len =
4249 			 HAL_RX_MSDU_PKT_LENGTH_GET(msdu_desc_info->info0);
4250 		msdu_list->sw_cookie[i] =
4251 			FIELD_GET(BUFFER_ADDR_INFO1_SW_COOKIE,
4252 				  msdu_details[i].buf_addr_info.info1);
4253 		tmp = FIELD_GET(BUFFER_ADDR_INFO1_RET_BUF_MGR,
4254 				msdu_details[i].buf_addr_info.info1);
4255 		msdu_list->rbm[i] = tmp;
4256 	}
4257 	*num_msdus = i;
4258 }
4259 
4260 static u32 ath11k_dp_rx_mon_comp_ppduid(u32 msdu_ppdu_id, u32 *ppdu_id,
4261 					u32 *rx_bufs_used)
4262 {
4263 	u32 ret = 0;
4264 
4265 	if ((*ppdu_id < msdu_ppdu_id) &&
4266 	    ((msdu_ppdu_id - *ppdu_id) < DP_NOT_PPDU_ID_WRAP_AROUND)) {
4267 		*ppdu_id = msdu_ppdu_id;
4268 		ret = msdu_ppdu_id;
4269 	} else if ((*ppdu_id > msdu_ppdu_id) &&
4270 		((*ppdu_id - msdu_ppdu_id) > DP_NOT_PPDU_ID_WRAP_AROUND)) {
4271 		/* mon_dst is behind than mon_status
4272 		 * skip dst_ring and free it
4273 		 */
4274 		*rx_bufs_used += 1;
4275 		*ppdu_id = msdu_ppdu_id;
4276 		ret = msdu_ppdu_id;
4277 	}
4278 	return ret;
4279 }
4280 
4281 static void ath11k_dp_mon_get_buf_len(struct hal_rx_msdu_desc_info *info,
4282 				      bool *is_frag, u32 *total_len,
4283 				      u32 *frag_len, u32 *msdu_cnt)
4284 {
4285 	if (info->msdu_flags & RX_MSDU_DESC_INFO0_MSDU_CONTINUATION) {
4286 		if (!*is_frag) {
4287 			*total_len = info->msdu_len;
4288 			*is_frag = true;
4289 		}
4290 		ath11k_dp_mon_set_frag_len(total_len,
4291 					   frag_len);
4292 	} else {
4293 		if (*is_frag) {
4294 			ath11k_dp_mon_set_frag_len(total_len,
4295 						   frag_len);
4296 		} else {
4297 			*frag_len = info->msdu_len;
4298 		}
4299 		*is_frag = false;
4300 		*msdu_cnt -= 1;
4301 	}
4302 }
4303 
4304 static u32
4305 ath11k_dp_rx_mon_mpdu_pop(struct ath11k *ar,
4306 			  void *ring_entry, struct sk_buff **head_msdu,
4307 			  struct sk_buff **tail_msdu, u32 *npackets,
4308 			  u32 *ppdu_id)
4309 {
4310 	struct ath11k_pdev_dp *dp = &ar->dp;
4311 	struct ath11k_mon_data *pmon = (struct ath11k_mon_data *)&dp->mon_data;
4312 	struct dp_rxdma_ring *rx_ring = &dp->rxdma_mon_buf_ring;
4313 	struct sk_buff *msdu = NULL, *last = NULL;
4314 	struct hal_rx_msdu_list msdu_list;
4315 	void *p_buf_addr_info, *p_last_buf_addr_info;
4316 	struct hal_rx_desc *rx_desc;
4317 	void *rx_msdu_link_desc;
4318 	dma_addr_t paddr;
4319 	u16 num_msdus = 0;
4320 	u32 rx_buf_size, rx_pkt_offset, sw_cookie;
4321 	u32 rx_bufs_used = 0, i = 0;
4322 	u32 msdu_ppdu_id = 0, msdu_cnt = 0;
4323 	u32 total_len = 0, frag_len = 0;
4324 	bool is_frag, is_first_msdu;
4325 	bool drop_mpdu = false;
4326 	struct ath11k_skb_rxcb *rxcb;
4327 	struct hal_reo_entrance_ring *ent_desc =
4328 			(struct hal_reo_entrance_ring *)ring_entry;
4329 	int buf_id;
4330 
4331 	ath11k_hal_rx_reo_ent_buf_paddr_get(ring_entry, &paddr,
4332 					    &sw_cookie, &p_last_buf_addr_info,
4333 					    &msdu_cnt);
4334 
4335 	if (FIELD_GET(HAL_REO_ENTR_RING_INFO1_RXDMA_PUSH_REASON,
4336 		      ent_desc->info1) ==
4337 		      HAL_REO_DEST_RING_PUSH_REASON_ERR_DETECTED) {
4338 		u8 rxdma_err =
4339 			FIELD_GET(HAL_REO_ENTR_RING_INFO1_RXDMA_ERROR_CODE,
4340 				  ent_desc->info1);
4341 		if (rxdma_err == HAL_REO_ENTR_RING_RXDMA_ECODE_FLUSH_REQUEST_ERR ||
4342 		    rxdma_err == HAL_REO_ENTR_RING_RXDMA_ECODE_MPDU_LEN_ERR ||
4343 		    rxdma_err == HAL_REO_ENTR_RING_RXDMA_ECODE_OVERFLOW_ERR) {
4344 			drop_mpdu = true;
4345 			pmon->rx_mon_stats.dest_mpdu_drop++;
4346 		}
4347 	}
4348 
4349 	is_frag = false;
4350 	is_first_msdu = true;
4351 
4352 	do {
4353 		if (pmon->mon_last_linkdesc_paddr == paddr) {
4354 			pmon->rx_mon_stats.dup_mon_linkdesc_cnt++;
4355 			return rx_bufs_used;
4356 		}
4357 
4358 		rx_msdu_link_desc =
4359 			(void *)pmon->link_desc_banks[sw_cookie].vaddr +
4360 			(paddr - pmon->link_desc_banks[sw_cookie].paddr);
4361 
4362 		ath11k_hal_rx_msdu_list_get(ar, rx_msdu_link_desc, &msdu_list,
4363 					    &num_msdus);
4364 
4365 		for (i = 0; i < num_msdus; i++) {
4366 			u32 l2_hdr_offset;
4367 
4368 			if (pmon->mon_last_buf_cookie == msdu_list.sw_cookie[i]) {
4369 				ath11k_dbg(ar->ab, ATH11K_DBG_DATA,
4370 					   "i %d last_cookie %d is same\n",
4371 					   i, pmon->mon_last_buf_cookie);
4372 				drop_mpdu = true;
4373 				pmon->rx_mon_stats.dup_mon_buf_cnt++;
4374 				continue;
4375 			}
4376 			buf_id = FIELD_GET(DP_RXDMA_BUF_COOKIE_BUF_ID,
4377 					   msdu_list.sw_cookie[i]);
4378 
4379 			spin_lock_bh(&rx_ring->idr_lock);
4380 			msdu = idr_find(&rx_ring->bufs_idr, buf_id);
4381 			spin_unlock_bh(&rx_ring->idr_lock);
4382 			if (!msdu) {
4383 				ath11k_dbg(ar->ab, ATH11K_DBG_DATA,
4384 					   "msdu_pop: invalid buf_id %d\n", buf_id);
4385 				break;
4386 			}
4387 			rxcb = ATH11K_SKB_RXCB(msdu);
4388 			if (!rxcb->unmapped) {
4389 				dma_unmap_single(ar->ab->dev, rxcb->paddr,
4390 						 msdu->len +
4391 						 skb_tailroom(msdu),
4392 						 DMA_FROM_DEVICE);
4393 				rxcb->unmapped = 1;
4394 			}
4395 			if (drop_mpdu) {
4396 				ath11k_dbg(ar->ab, ATH11K_DBG_DATA,
4397 					   "i %d drop msdu %p *ppdu_id %x\n",
4398 					   i, msdu, *ppdu_id);
4399 				dev_kfree_skb_any(msdu);
4400 				msdu = NULL;
4401 				goto next_msdu;
4402 			}
4403 
4404 			rx_desc = (struct hal_rx_desc *)msdu->data;
4405 
4406 			rx_pkt_offset = sizeof(struct hal_rx_desc);
4407 			l2_hdr_offset = ath11k_dp_rx_h_msdu_end_l3pad(rx_desc);
4408 
4409 			if (is_first_msdu) {
4410 				if (!ath11k_dp_rxdesc_mpdu_valid(rx_desc)) {
4411 					drop_mpdu = true;
4412 					dev_kfree_skb_any(msdu);
4413 					msdu = NULL;
4414 					pmon->mon_last_linkdesc_paddr = paddr;
4415 					goto next_msdu;
4416 				}
4417 
4418 				msdu_ppdu_id =
4419 					ath11k_dp_rxdesc_get_ppduid(rx_desc);
4420 
4421 				if (ath11k_dp_rx_mon_comp_ppduid(msdu_ppdu_id,
4422 								 ppdu_id,
4423 								 &rx_bufs_used)) {
4424 					if (rx_bufs_used) {
4425 						drop_mpdu = true;
4426 						dev_kfree_skb_any(msdu);
4427 						msdu = NULL;
4428 						goto next_msdu;
4429 					}
4430 					return rx_bufs_used;
4431 				}
4432 				pmon->mon_last_linkdesc_paddr = paddr;
4433 				is_first_msdu = false;
4434 			}
4435 			ath11k_dp_mon_get_buf_len(&msdu_list.msdu_info[i],
4436 						  &is_frag, &total_len,
4437 						  &frag_len, &msdu_cnt);
4438 			rx_buf_size = rx_pkt_offset + l2_hdr_offset + frag_len;
4439 
4440 			ath11k_dp_pkt_set_pktlen(msdu, rx_buf_size);
4441 
4442 			if (!(*head_msdu))
4443 				*head_msdu = msdu;
4444 			else if (last)
4445 				last->next = msdu;
4446 
4447 			last = msdu;
4448 next_msdu:
4449 			pmon->mon_last_buf_cookie = msdu_list.sw_cookie[i];
4450 			rx_bufs_used++;
4451 			spin_lock_bh(&rx_ring->idr_lock);
4452 			idr_remove(&rx_ring->bufs_idr, buf_id);
4453 			spin_unlock_bh(&rx_ring->idr_lock);
4454 		}
4455 
4456 		ath11k_dp_rx_mon_next_link_desc_get(rx_msdu_link_desc, &paddr,
4457 						    &sw_cookie,
4458 						    &p_buf_addr_info);
4459 
4460 		if (ath11k_dp_rx_monitor_link_desc_return(ar,
4461 							  p_last_buf_addr_info,
4462 							  dp->mac_id))
4463 			ath11k_dbg(ar->ab, ATH11K_DBG_DATA,
4464 				   "dp_rx_monitor_link_desc_return failed");
4465 
4466 		p_last_buf_addr_info = p_buf_addr_info;
4467 
4468 	} while (paddr && msdu_cnt);
4469 
4470 	if (last)
4471 		last->next = NULL;
4472 
4473 	*tail_msdu = msdu;
4474 
4475 	if (msdu_cnt == 0)
4476 		*npackets = 1;
4477 
4478 	return rx_bufs_used;
4479 }
4480 
4481 static void ath11k_dp_rx_msdus_set_payload(struct sk_buff *msdu)
4482 {
4483 	u32 rx_pkt_offset, l2_hdr_offset;
4484 
4485 	rx_pkt_offset = sizeof(struct hal_rx_desc);
4486 	l2_hdr_offset = ath11k_dp_rx_h_msdu_end_l3pad((struct hal_rx_desc *)msdu->data);
4487 	skb_pull(msdu, rx_pkt_offset + l2_hdr_offset);
4488 }
4489 
4490 static struct sk_buff *
4491 ath11k_dp_rx_mon_merg_msdus(struct ath11k *ar,
4492 			    u32 mac_id, struct sk_buff *head_msdu,
4493 			    struct sk_buff *last_msdu,
4494 			    struct ieee80211_rx_status *rxs)
4495 {
4496 	struct sk_buff *msdu, *mpdu_buf, *prev_buf;
4497 	u32 decap_format, wifi_hdr_len;
4498 	struct hal_rx_desc *rx_desc;
4499 	char *hdr_desc;
4500 	u8 *dest;
4501 	struct ieee80211_hdr_3addr *wh;
4502 
4503 	mpdu_buf = NULL;
4504 
4505 	if (!head_msdu)
4506 		goto err_merge_fail;
4507 
4508 	rx_desc = (struct hal_rx_desc *)head_msdu->data;
4509 
4510 	if (ath11k_dp_rxdesc_get_mpdulen_err(rx_desc))
4511 		return NULL;
4512 
4513 	decap_format = ath11k_dp_rxdesc_get_decap_format(rx_desc);
4514 
4515 	ath11k_dp_rx_h_ppdu(ar, rx_desc, rxs);
4516 
4517 	if (decap_format == DP_RX_DECAP_TYPE_RAW) {
4518 		ath11k_dp_rx_msdus_set_payload(head_msdu);
4519 
4520 		prev_buf = head_msdu;
4521 		msdu = head_msdu->next;
4522 
4523 		while (msdu) {
4524 			ath11k_dp_rx_msdus_set_payload(msdu);
4525 
4526 			prev_buf = msdu;
4527 			msdu = msdu->next;
4528 		}
4529 
4530 		prev_buf->next = NULL;
4531 
4532 		skb_trim(prev_buf, prev_buf->len - HAL_RX_FCS_LEN);
4533 	} else if (decap_format == DP_RX_DECAP_TYPE_NATIVE_WIFI) {
4534 		__le16 qos_field;
4535 		u8 qos_pkt = 0;
4536 
4537 		rx_desc = (struct hal_rx_desc *)head_msdu->data;
4538 		hdr_desc = ath11k_dp_rxdesc_get_80211hdr(rx_desc);
4539 
4540 		/* Base size */
4541 		wifi_hdr_len = sizeof(struct ieee80211_hdr_3addr);
4542 		wh = (struct ieee80211_hdr_3addr *)hdr_desc;
4543 
4544 		if (ieee80211_is_data_qos(wh->frame_control)) {
4545 			struct ieee80211_qos_hdr *qwh =
4546 					(struct ieee80211_qos_hdr *)hdr_desc;
4547 
4548 			qos_field = qwh->qos_ctrl;
4549 			qos_pkt = 1;
4550 		}
4551 		msdu = head_msdu;
4552 
4553 		while (msdu) {
4554 			rx_desc = (struct hal_rx_desc *)msdu->data;
4555 			hdr_desc = ath11k_dp_rxdesc_get_80211hdr(rx_desc);
4556 
4557 			if (qos_pkt) {
4558 				dest = skb_push(msdu, sizeof(__le16));
4559 				if (!dest)
4560 					goto err_merge_fail;
4561 				memcpy(dest, hdr_desc, wifi_hdr_len);
4562 				memcpy(dest + wifi_hdr_len,
4563 				       (u8 *)&qos_field, sizeof(__le16));
4564 			}
4565 			ath11k_dp_rx_msdus_set_payload(msdu);
4566 			prev_buf = msdu;
4567 			msdu = msdu->next;
4568 		}
4569 		dest = skb_put(prev_buf, HAL_RX_FCS_LEN);
4570 		if (!dest)
4571 			goto err_merge_fail;
4572 
4573 		ath11k_dbg(ar->ab, ATH11K_DBG_DATA,
4574 			   "mpdu_buf %pK mpdu_buf->len %u",
4575 			   prev_buf, prev_buf->len);
4576 	} else {
4577 		ath11k_dbg(ar->ab, ATH11K_DBG_DATA,
4578 			   "decap format %d is not supported!\n",
4579 			   decap_format);
4580 		goto err_merge_fail;
4581 	}
4582 
4583 	return head_msdu;
4584 
4585 err_merge_fail:
4586 	if (mpdu_buf && decap_format != DP_RX_DECAP_TYPE_RAW) {
4587 		ath11k_dbg(ar->ab, ATH11K_DBG_DATA,
4588 			   "err_merge_fail mpdu_buf %pK", mpdu_buf);
4589 		/* Free the head buffer */
4590 		dev_kfree_skb_any(mpdu_buf);
4591 	}
4592 	return NULL;
4593 }
4594 
4595 static int ath11k_dp_rx_mon_deliver(struct ath11k *ar, u32 mac_id,
4596 				    struct sk_buff *head_msdu,
4597 				    struct sk_buff *tail_msdu,
4598 				    struct napi_struct *napi)
4599 {
4600 	struct ath11k_pdev_dp *dp = &ar->dp;
4601 	struct sk_buff *mon_skb, *skb_next, *header;
4602 	struct ieee80211_rx_status *rxs = &dp->rx_status, *status;
4603 
4604 	mon_skb = ath11k_dp_rx_mon_merg_msdus(ar, mac_id, head_msdu,
4605 					      tail_msdu, rxs);
4606 
4607 	if (!mon_skb)
4608 		goto mon_deliver_fail;
4609 
4610 	header = mon_skb;
4611 
4612 	rxs->flag = 0;
4613 	do {
4614 		skb_next = mon_skb->next;
4615 		if (!skb_next)
4616 			rxs->flag &= ~RX_FLAG_AMSDU_MORE;
4617 		else
4618 			rxs->flag |= RX_FLAG_AMSDU_MORE;
4619 
4620 		if (mon_skb == header) {
4621 			header = NULL;
4622 			rxs->flag &= ~RX_FLAG_ALLOW_SAME_PN;
4623 		} else {
4624 			rxs->flag |= RX_FLAG_ALLOW_SAME_PN;
4625 		}
4626 		rxs->flag |= RX_FLAG_ONLY_MONITOR;
4627 
4628 		status = IEEE80211_SKB_RXCB(mon_skb);
4629 		*status = *rxs;
4630 
4631 		ath11k_dp_rx_deliver_msdu(ar, napi, mon_skb);
4632 		mon_skb = skb_next;
4633 	} while (mon_skb);
4634 	rxs->flag = 0;
4635 
4636 	return 0;
4637 
4638 mon_deliver_fail:
4639 	mon_skb = head_msdu;
4640 	while (mon_skb) {
4641 		skb_next = mon_skb->next;
4642 		dev_kfree_skb_any(mon_skb);
4643 		mon_skb = skb_next;
4644 	}
4645 	return -EINVAL;
4646 }
4647 
4648 static void ath11k_dp_rx_mon_dest_process(struct ath11k *ar, u32 quota,
4649 					  struct napi_struct *napi)
4650 {
4651 	struct ath11k_pdev_dp *dp = &ar->dp;
4652 	struct ath11k_mon_data *pmon = (struct ath11k_mon_data *)&dp->mon_data;
4653 	void *ring_entry;
4654 	void *mon_dst_srng;
4655 	u32 ppdu_id;
4656 	u32 rx_bufs_used;
4657 	struct ath11k_pdev_mon_stats *rx_mon_stats;
4658 	u32	 npackets = 0;
4659 
4660 	mon_dst_srng = &ar->ab->hal.srng_list[dp->rxdma_mon_dst_ring.ring_id];
4661 
4662 	if (!mon_dst_srng) {
4663 		ath11k_warn(ar->ab,
4664 			    "HAL Monitor Destination Ring Init Failed -- %pK",
4665 			    mon_dst_srng);
4666 		return;
4667 	}
4668 
4669 	spin_lock_bh(&pmon->mon_lock);
4670 
4671 	ath11k_hal_srng_access_begin(ar->ab, mon_dst_srng);
4672 
4673 	ppdu_id = pmon->mon_ppdu_info.ppdu_id;
4674 	rx_bufs_used = 0;
4675 	rx_mon_stats = &pmon->rx_mon_stats;
4676 
4677 	while ((ring_entry = ath11k_hal_srng_dst_peek(ar->ab, mon_dst_srng))) {
4678 		struct sk_buff *head_msdu, *tail_msdu;
4679 
4680 		head_msdu = NULL;
4681 		tail_msdu = NULL;
4682 
4683 		rx_bufs_used += ath11k_dp_rx_mon_mpdu_pop(ar, ring_entry,
4684 							  &head_msdu,
4685 							  &tail_msdu,
4686 							  &npackets, &ppdu_id);
4687 
4688 		if (ppdu_id != pmon->mon_ppdu_info.ppdu_id) {
4689 			pmon->mon_ppdu_status = DP_PPDU_STATUS_START;
4690 			ath11k_dbg(ar->ab, ATH11K_DBG_DATA,
4691 				   "dest_rx: new ppdu_id %x != status ppdu_id %x",
4692 				   ppdu_id, pmon->mon_ppdu_info.ppdu_id);
4693 			break;
4694 		}
4695 		if (head_msdu && tail_msdu) {
4696 			ath11k_dp_rx_mon_deliver(ar, dp->mac_id, head_msdu,
4697 						 tail_msdu, napi);
4698 			rx_mon_stats->dest_mpdu_done++;
4699 		}
4700 
4701 		ring_entry = ath11k_hal_srng_dst_get_next_entry(ar->ab,
4702 								mon_dst_srng);
4703 	}
4704 	ath11k_hal_srng_access_end(ar->ab, mon_dst_srng);
4705 
4706 	spin_unlock_bh(&pmon->mon_lock);
4707 
4708 	if (rx_bufs_used) {
4709 		rx_mon_stats->dest_ppdu_done++;
4710 		ath11k_dp_rxbufs_replenish(ar->ab, dp->mac_id,
4711 					   &dp->rxdma_mon_buf_ring,
4712 					   rx_bufs_used,
4713 					   HAL_RX_BUF_RBM_SW3_BM, GFP_ATOMIC);
4714 	}
4715 }
4716 
4717 static void ath11k_dp_rx_mon_status_process_tlv(struct ath11k *ar,
4718 						u32 quota,
4719 						struct napi_struct *napi)
4720 {
4721 	struct ath11k_pdev_dp *dp = &ar->dp;
4722 	struct ath11k_mon_data *pmon = (struct ath11k_mon_data *)&dp->mon_data;
4723 	struct hal_rx_mon_ppdu_info *ppdu_info;
4724 	struct sk_buff *status_skb;
4725 	u32 tlv_status = HAL_TLV_STATUS_BUF_DONE;
4726 	struct ath11k_pdev_mon_stats *rx_mon_stats;
4727 
4728 	ppdu_info = &pmon->mon_ppdu_info;
4729 	rx_mon_stats = &pmon->rx_mon_stats;
4730 
4731 	if (pmon->mon_ppdu_status != DP_PPDU_STATUS_START)
4732 		return;
4733 
4734 	while (!skb_queue_empty(&pmon->rx_status_q)) {
4735 		status_skb = skb_dequeue(&pmon->rx_status_q);
4736 
4737 		tlv_status = ath11k_hal_rx_parse_mon_status(ar->ab, ppdu_info,
4738 							    status_skb);
4739 		if (tlv_status == HAL_TLV_STATUS_PPDU_DONE) {
4740 			rx_mon_stats->status_ppdu_done++;
4741 			pmon->mon_ppdu_status = DP_PPDU_STATUS_DONE;
4742 			ath11k_dp_rx_mon_dest_process(ar, quota, napi);
4743 			pmon->mon_ppdu_status = DP_PPDU_STATUS_START;
4744 		}
4745 		dev_kfree_skb_any(status_skb);
4746 	}
4747 }
4748 
4749 static int ath11k_dp_mon_process_rx(struct ath11k_base *ab, int mac_id,
4750 				    struct napi_struct *napi, int budget)
4751 {
4752 	struct ath11k *ar = ab->pdevs[mac_id].ar;
4753 	struct ath11k_pdev_dp *dp = &ar->dp;
4754 	struct ath11k_mon_data *pmon = (struct ath11k_mon_data *)&dp->mon_data;
4755 	int num_buffs_reaped = 0;
4756 
4757 	num_buffs_reaped = ath11k_dp_rx_reap_mon_status_ring(ar->ab, dp->mac_id, &budget,
4758 							     &pmon->rx_status_q);
4759 	if (num_buffs_reaped)
4760 		ath11k_dp_rx_mon_status_process_tlv(ar, budget, napi);
4761 
4762 	return num_buffs_reaped;
4763 }
4764 
4765 int ath11k_dp_rx_process_mon_rings(struct ath11k_base *ab, int mac_id,
4766 				   struct napi_struct *napi, int budget)
4767 {
4768 	struct ath11k *ar = ab->pdevs[mac_id].ar;
4769 	int ret = 0;
4770 
4771 	if (test_bit(ATH11K_FLAG_MONITOR_ENABLED, &ar->monitor_flags))
4772 		ret = ath11k_dp_mon_process_rx(ab, mac_id, napi, budget);
4773 	else
4774 		ret = ath11k_dp_rx_process_mon_status(ab, mac_id, napi, budget);
4775 	return ret;
4776 }
4777 
4778 static int ath11k_dp_rx_pdev_mon_status_attach(struct ath11k *ar)
4779 {
4780 	struct ath11k_pdev_dp *dp = &ar->dp;
4781 	struct ath11k_mon_data *pmon = (struct ath11k_mon_data *)&dp->mon_data;
4782 
4783 	skb_queue_head_init(&pmon->rx_status_q);
4784 
4785 	pmon->mon_ppdu_status = DP_PPDU_STATUS_START;
4786 
4787 	memset(&pmon->rx_mon_stats, 0,
4788 	       sizeof(pmon->rx_mon_stats));
4789 	return 0;
4790 }
4791 
4792 int ath11k_dp_rx_pdev_mon_attach(struct ath11k *ar)
4793 {
4794 	struct ath11k_pdev_dp *dp = &ar->dp;
4795 	struct ath11k_mon_data *pmon = &dp->mon_data;
4796 	struct hal_srng *mon_desc_srng = NULL;
4797 	struct dp_srng *dp_srng;
4798 	int ret = 0;
4799 	u32 n_link_desc = 0;
4800 
4801 	ret = ath11k_dp_rx_pdev_mon_status_attach(ar);
4802 	if (ret) {
4803 		ath11k_warn(ar->ab, "pdev_mon_status_attach() failed");
4804 		return ret;
4805 	}
4806 
4807 	dp_srng = &dp->rxdma_mon_desc_ring;
4808 	n_link_desc = dp_srng->size /
4809 		ath11k_hal_srng_get_entrysize(HAL_RXDMA_MONITOR_DESC);
4810 	mon_desc_srng =
4811 		&ar->ab->hal.srng_list[dp->rxdma_mon_desc_ring.ring_id];
4812 
4813 	ret = ath11k_dp_link_desc_setup(ar->ab, pmon->link_desc_banks,
4814 					HAL_RXDMA_MONITOR_DESC, mon_desc_srng,
4815 					n_link_desc);
4816 	if (ret) {
4817 		ath11k_warn(ar->ab, "mon_link_desc_pool_setup() failed");
4818 		return ret;
4819 	}
4820 	pmon->mon_last_linkdesc_paddr = 0;
4821 	pmon->mon_last_buf_cookie = DP_RX_DESC_COOKIE_MAX + 1;
4822 	spin_lock_init(&pmon->mon_lock);
4823 	return 0;
4824 }
4825 
4826 static int ath11k_dp_mon_link_free(struct ath11k *ar)
4827 {
4828 	struct ath11k_pdev_dp *dp = &ar->dp;
4829 	struct ath11k_mon_data *pmon = &dp->mon_data;
4830 
4831 	ath11k_dp_link_desc_cleanup(ar->ab, pmon->link_desc_banks,
4832 				    HAL_RXDMA_MONITOR_DESC,
4833 				    &dp->rxdma_mon_desc_ring);
4834 	return 0;
4835 }
4836 
4837 int ath11k_dp_rx_pdev_mon_detach(struct ath11k *ar)
4838 {
4839 	ath11k_dp_mon_link_free(ar);
4840 	return 0;
4841 }
4842