xref: /freebsd/sys/contrib/dev/iwlwifi/mvm/rxmq.c (revision 089104e0e01f080c9cd45dc5f34c4f636dea4ca7)
1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /*
3  * Copyright (C) 2012-2014, 2018-2021 Intel Corporation
4  * Copyright (C) 2013-2015 Intel Mobile Communications GmbH
5  * Copyright (C) 2015-2017 Intel Deutschland GmbH
6  */
7 #include <linux/etherdevice.h>
8 #include <linux/skbuff.h>
9 #if defined(__FreeBSD__)
10 #include <net/ieee80211_radiotap.h>
11 #endif
12 #include "iwl-trans.h"
13 #include "mvm.h"
14 #include "fw-api.h"
15 
16 static void *iwl_mvm_skb_get_hdr(struct sk_buff *skb)
17 {
18 	struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
19 	u8 *data = skb->data;
20 
21 	/* Alignment concerns */
22 	BUILD_BUG_ON(sizeof(struct ieee80211_radiotap_he) % 4);
23 	BUILD_BUG_ON(sizeof(struct ieee80211_radiotap_he_mu) % 4);
24 	BUILD_BUG_ON(sizeof(struct ieee80211_radiotap_lsig) % 4);
25 	BUILD_BUG_ON(sizeof(struct ieee80211_vendor_radiotap) % 4);
26 
27 	if (rx_status->flag & RX_FLAG_RADIOTAP_HE)
28 		data += sizeof(struct ieee80211_radiotap_he);
29 	if (rx_status->flag & RX_FLAG_RADIOTAP_HE_MU)
30 		data += sizeof(struct ieee80211_radiotap_he_mu);
31 	if (rx_status->flag & RX_FLAG_RADIOTAP_LSIG)
32 		data += sizeof(struct ieee80211_radiotap_lsig);
33 	if (rx_status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA) {
34 		struct ieee80211_vendor_radiotap *radiotap = (void *)data;
35 
36 		data += sizeof(*radiotap) + radiotap->len + radiotap->pad;
37 	}
38 
39 	return data;
40 }
41 
42 static inline int iwl_mvm_check_pn(struct iwl_mvm *mvm, struct sk_buff *skb,
43 				   int queue, struct ieee80211_sta *sta)
44 {
45 	struct iwl_mvm_sta *mvmsta;
46 	struct ieee80211_hdr *hdr = iwl_mvm_skb_get_hdr(skb);
47 	struct ieee80211_rx_status *stats = IEEE80211_SKB_RXCB(skb);
48 	struct iwl_mvm_key_pn *ptk_pn;
49 	int res;
50 	u8 tid, keyidx;
51 	u8 pn[IEEE80211_CCMP_PN_LEN];
52 	u8 *extiv;
53 
54 	/* do PN checking */
55 
56 	/* multicast and non-data only arrives on default queue */
57 	if (!ieee80211_is_data(hdr->frame_control) ||
58 	    is_multicast_ether_addr(hdr->addr1))
59 		return 0;
60 
61 	/* do not check PN for open AP */
62 	if (!(stats->flag & RX_FLAG_DECRYPTED))
63 		return 0;
64 
65 	/*
66 	 * avoid checking for default queue - we don't want to replicate
67 	 * all the logic that's necessary for checking the PN on fragmented
68 	 * frames, leave that to mac80211
69 	 */
70 	if (queue == 0)
71 		return 0;
72 
73 	/* if we are here - this for sure is either CCMP or GCMP */
74 	if (IS_ERR_OR_NULL(sta)) {
75 		IWL_DEBUG_DROP(mvm,
76 			       "expected hw-decrypted unicast frame for station\n");
77 		return -1;
78 	}
79 
80 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
81 
82 	extiv = (u8 *)hdr + ieee80211_hdrlen(hdr->frame_control);
83 	keyidx = extiv[3] >> 6;
84 
85 	ptk_pn = rcu_dereference(mvmsta->ptk_pn[keyidx]);
86 	if (!ptk_pn)
87 		return -1;
88 
89 	if (ieee80211_is_data_qos(hdr->frame_control))
90 		tid = ieee80211_get_tid(hdr);
91 	else
92 		tid = 0;
93 
94 	/* we don't use HCCA/802.11 QoS TSPECs, so drop such frames */
95 	if (tid >= IWL_MAX_TID_COUNT)
96 		return -1;
97 
98 	/* load pn */
99 	pn[0] = extiv[7];
100 	pn[1] = extiv[6];
101 	pn[2] = extiv[5];
102 	pn[3] = extiv[4];
103 	pn[4] = extiv[1];
104 	pn[5] = extiv[0];
105 
106 	res = memcmp(pn, ptk_pn->q[queue].pn[tid], IEEE80211_CCMP_PN_LEN);
107 	if (res < 0)
108 		return -1;
109 	if (!res && !(stats->flag & RX_FLAG_ALLOW_SAME_PN))
110 		return -1;
111 
112 	memcpy(ptk_pn->q[queue].pn[tid], pn, IEEE80211_CCMP_PN_LEN);
113 	stats->flag |= RX_FLAG_PN_VALIDATED;
114 
115 	return 0;
116 }
117 
118 /* iwl_mvm_create_skb Adds the rxb to a new skb */
119 static int iwl_mvm_create_skb(struct iwl_mvm *mvm, struct sk_buff *skb,
120 			      struct ieee80211_hdr *hdr, u16 len, u8 crypt_len,
121 			      struct iwl_rx_cmd_buffer *rxb)
122 {
123 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
124 	struct iwl_rx_mpdu_desc *desc = (void *)pkt->data;
125 	unsigned int headlen, fraglen, pad_len = 0;
126 	unsigned int hdrlen = ieee80211_hdrlen(hdr->frame_control);
127 	u8 mic_crc_len = u8_get_bits(desc->mac_flags1,
128 				     IWL_RX_MPDU_MFLG1_MIC_CRC_LEN_MASK) << 1;
129 
130 	if (desc->mac_flags2 & IWL_RX_MPDU_MFLG2_PAD) {
131 		len -= 2;
132 		pad_len = 2;
133 	}
134 
135 	/*
136 	 * For non monitor interface strip the bytes the RADA might not have
137 	 * removed. As monitor interface cannot exist with other interfaces
138 	 * this removal is safe.
139 	 */
140 	if (mic_crc_len && !ieee80211_hw_check(mvm->hw, RX_INCLUDES_FCS)) {
141 		u32 pkt_flags = le32_to_cpu(pkt->len_n_flags);
142 
143 		/*
144 		 * If RADA was not enabled then decryption was not performed so
145 		 * the MIC cannot be removed.
146 		 */
147 		if (!(pkt_flags & FH_RSCSR_RADA_EN)) {
148 			if (WARN_ON(crypt_len > mic_crc_len))
149 				return -EINVAL;
150 
151 			mic_crc_len -= crypt_len;
152 		}
153 
154 		if (WARN_ON(mic_crc_len > len))
155 			return -EINVAL;
156 
157 		len -= mic_crc_len;
158 	}
159 
160 	/* If frame is small enough to fit in skb->head, pull it completely.
161 	 * If not, only pull ieee80211_hdr (including crypto if present, and
162 	 * an additional 8 bytes for SNAP/ethertype, see below) so that
163 	 * splice() or TCP coalesce are more efficient.
164 	 *
165 	 * Since, in addition, ieee80211_data_to_8023() always pull in at
166 	 * least 8 bytes (possibly more for mesh) we can do the same here
167 	 * to save the cost of doing it later. That still doesn't pull in
168 	 * the actual IP header since the typical case has a SNAP header.
169 	 * If the latter changes (there are efforts in the standards group
170 	 * to do so) we should revisit this and ieee80211_data_to_8023().
171 	 */
172 	headlen = (len <= skb_tailroom(skb)) ? len :
173 					       hdrlen + crypt_len + 8;
174 
175 	/* The firmware may align the packet to DWORD.
176 	 * The padding is inserted after the IV.
177 	 * After copying the header + IV skip the padding if
178 	 * present before copying packet data.
179 	 */
180 	hdrlen += crypt_len;
181 
182 	if (unlikely(headlen < hdrlen))
183 		return -EINVAL;
184 
185 	skb_put_data(skb, hdr, hdrlen);
186 	skb_put_data(skb, (u8 *)hdr + hdrlen + pad_len, headlen - hdrlen);
187 
188 	/*
189 	 * If we did CHECKSUM_COMPLETE, the hardware only does it right for
190 	 * certain cases and starts the checksum after the SNAP. Check if
191 	 * this is the case - it's easier to just bail out to CHECKSUM_NONE
192 	 * in the cases the hardware didn't handle, since it's rare to see
193 	 * such packets, even though the hardware did calculate the checksum
194 	 * in this case, just starting after the MAC header instead.
195 	 *
196 	 * Starting from Bz hardware, it calculates starting directly after
197 	 * the MAC header, so that matches mac80211's expectation.
198 	 */
199 	if (skb->ip_summed == CHECKSUM_COMPLETE &&
200 	    mvm->trans->trans_cfg->device_family < IWL_DEVICE_FAMILY_BZ) {
201 		struct {
202 			u8 hdr[6];
203 			__be16 type;
204 		} __packed *shdr = (void *)((u8 *)hdr + hdrlen + pad_len);
205 
206 		if (unlikely(headlen - hdrlen < sizeof(*shdr) ||
207 			     !ether_addr_equal(shdr->hdr, rfc1042_header) ||
208 			     (shdr->type != htons(ETH_P_IP) &&
209 			      shdr->type != htons(ETH_P_ARP) &&
210 			      shdr->type != htons(ETH_P_IPV6) &&
211 			      shdr->type != htons(ETH_P_8021Q) &&
212 			      shdr->type != htons(ETH_P_PAE) &&
213 			      shdr->type != htons(ETH_P_TDLS))))
214 			skb->ip_summed = CHECKSUM_NONE;
215 		else
216 			/* mac80211 assumes full CSUM including SNAP header */
217 			skb_postpush_rcsum(skb, shdr, sizeof(*shdr));
218 	}
219 
220 	fraglen = len - headlen;
221 
222 	if (fraglen) {
223 		int offset = (u8 *)hdr + headlen + pad_len -
224 			     (u8 *)rxb_addr(rxb) + rxb_offset(rxb);
225 
226 		skb_add_rx_frag(skb, 0, rxb_steal_page(rxb), offset,
227 				fraglen, rxb->truesize);
228 	}
229 
230 	return 0;
231 }
232 
233 static void iwl_mvm_add_rtap_sniffer_config(struct iwl_mvm *mvm,
234 					    struct sk_buff *skb)
235 {
236 	struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
237 	struct ieee80211_vendor_radiotap *radiotap;
238 	const int size = sizeof(*radiotap) + sizeof(__le16);
239 
240 	if (!mvm->cur_aid)
241 		return;
242 
243 	/* ensure alignment */
244 	BUILD_BUG_ON((size + 2) % 4);
245 
246 	radiotap = skb_put(skb, size + 2);
247 	radiotap->align = 1;
248 	/* Intel OUI */
249 	radiotap->oui[0] = 0xf6;
250 	radiotap->oui[1] = 0x54;
251 	radiotap->oui[2] = 0x25;
252 	/* radiotap sniffer config sub-namespace */
253 	radiotap->subns = 1;
254 	radiotap->present = 0x1;
255 	radiotap->len = size - sizeof(*radiotap);
256 	radiotap->pad = 2;
257 
258 	/* fill the data now */
259 	memcpy(radiotap->data, &mvm->cur_aid, sizeof(mvm->cur_aid));
260 	/* and clear the padding */
261 	memset(radiotap->data + sizeof(__le16), 0, radiotap->pad);
262 
263 	rx_status->flag |= RX_FLAG_RADIOTAP_VENDOR_DATA;
264 }
265 
266 /* iwl_mvm_pass_packet_to_mac80211 - passes the packet for mac80211 */
267 static void iwl_mvm_pass_packet_to_mac80211(struct iwl_mvm *mvm,
268 					    struct napi_struct *napi,
269 					    struct sk_buff *skb, int queue,
270 					    struct ieee80211_sta *sta)
271 {
272 	if (iwl_mvm_check_pn(mvm, skb, queue, sta))
273 		kfree_skb(skb);
274 	else
275 		ieee80211_rx_napi(mvm->hw, sta, skb, napi);
276 }
277 
278 static void iwl_mvm_get_signal_strength(struct iwl_mvm *mvm,
279 					struct ieee80211_rx_status *rx_status,
280 					u32 rate_n_flags, int energy_a,
281 					int energy_b)
282 {
283 	int max_energy;
284 	u32 rate_flags = rate_n_flags;
285 
286 	energy_a = energy_a ? -energy_a : S8_MIN;
287 	energy_b = energy_b ? -energy_b : S8_MIN;
288 	max_energy = max(energy_a, energy_b);
289 
290 	IWL_DEBUG_STATS(mvm, "energy In A %d B %d, and max %d\n",
291 			energy_a, energy_b, max_energy);
292 
293 	rx_status->signal = max_energy;
294 	rx_status->chains =
295 		(rate_flags & RATE_MCS_ANT_AB_MSK) >> RATE_MCS_ANT_POS;
296 	rx_status->chain_signal[0] = energy_a;
297 	rx_status->chain_signal[1] = energy_b;
298 }
299 
300 static int iwl_mvm_rx_mgmt_prot(struct ieee80211_sta *sta,
301 				struct ieee80211_hdr *hdr,
302 				struct iwl_rx_mpdu_desc *desc,
303 				u32 status)
304 {
305 	struct iwl_mvm_sta *mvmsta;
306 	struct iwl_mvm_vif *mvmvif;
307 	u8 keyid;
308 	struct ieee80211_key_conf *key;
309 	u32 len = le16_to_cpu(desc->mpdu_len);
310 	const u8 *frame = (void *)hdr;
311 
312 	if ((status & IWL_RX_MPDU_STATUS_SEC_MASK) == IWL_RX_MPDU_STATUS_SEC_NONE)
313 		return 0;
314 
315 	/*
316 	 * For non-beacon, we don't really care. But beacons may
317 	 * be filtered out, and we thus need the firmware's replay
318 	 * detection, otherwise beacons the firmware previously
319 	 * filtered could be replayed, or something like that, and
320 	 * it can filter a lot - though usually only if nothing has
321 	 * changed.
322 	 */
323 	if (!ieee80211_is_beacon(hdr->frame_control))
324 		return 0;
325 
326 	/* key mismatch - will also report !MIC_OK but we shouldn't count it */
327 	if (!(status & IWL_RX_MPDU_STATUS_KEY_VALID))
328 		return -1;
329 
330 	/* good cases */
331 	if (likely(status & IWL_RX_MPDU_STATUS_MIC_OK &&
332 		   !(status & IWL_RX_MPDU_STATUS_REPLAY_ERROR)))
333 		return 0;
334 
335 	if (!sta)
336 		return -1;
337 
338 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
339 
340 	mvmvif = iwl_mvm_vif_from_mac80211(mvmsta->vif);
341 
342 	/*
343 	 * both keys will have the same cipher and MIC length, use
344 	 * whichever one is available
345 	 */
346 	key = rcu_dereference(mvmvif->bcn_prot.keys[0]);
347 	if (!key) {
348 		key = rcu_dereference(mvmvif->bcn_prot.keys[1]);
349 		if (!key)
350 			return -1;
351 	}
352 
353 	if (len < key->icv_len + IEEE80211_GMAC_PN_LEN + 2)
354 		return -1;
355 
356 	/* get the real key ID */
357 	keyid = frame[len - key->icv_len - IEEE80211_GMAC_PN_LEN - 2];
358 	/* and if that's the other key, look it up */
359 	if (keyid != key->keyidx) {
360 		/*
361 		 * shouldn't happen since firmware checked, but be safe
362 		 * in case the MIC length is wrong too, for example
363 		 */
364 		if (keyid != 6 && keyid != 7)
365 			return -1;
366 		key = rcu_dereference(mvmvif->bcn_prot.keys[keyid - 6]);
367 		if (!key)
368 			return -1;
369 	}
370 
371 	/* Report status to mac80211 */
372 	if (!(status & IWL_RX_MPDU_STATUS_MIC_OK))
373 		ieee80211_key_mic_failure(key);
374 	else if (status & IWL_RX_MPDU_STATUS_REPLAY_ERROR)
375 		ieee80211_key_replay(key);
376 
377 	return -1;
378 }
379 
380 static int iwl_mvm_rx_crypto(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
381 			     struct ieee80211_hdr *hdr,
382 			     struct ieee80211_rx_status *stats, u16 phy_info,
383 			     struct iwl_rx_mpdu_desc *desc,
384 			     u32 pkt_flags, int queue, u8 *crypt_len)
385 {
386 	u32 status = le32_to_cpu(desc->status);
387 
388 	/*
389 	 * Drop UNKNOWN frames in aggregation, unless in monitor mode
390 	 * (where we don't have the keys).
391 	 * We limit this to aggregation because in TKIP this is a valid
392 	 * scenario, since we may not have the (correct) TTAK (phase 1
393 	 * key) in the firmware.
394 	 */
395 	if (phy_info & IWL_RX_MPDU_PHY_AMPDU &&
396 	    (status & IWL_RX_MPDU_STATUS_SEC_MASK) ==
397 	    IWL_RX_MPDU_STATUS_SEC_UNKNOWN && !mvm->monitor_on)
398 		return -1;
399 
400 	if (unlikely(ieee80211_is_mgmt(hdr->frame_control) &&
401 		     !ieee80211_has_protected(hdr->frame_control)))
402 		return iwl_mvm_rx_mgmt_prot(sta, hdr, desc, status);
403 
404 	if (!ieee80211_has_protected(hdr->frame_control) ||
405 	    (status & IWL_RX_MPDU_STATUS_SEC_MASK) ==
406 	    IWL_RX_MPDU_STATUS_SEC_NONE)
407 		return 0;
408 
409 	/* TODO: handle packets encrypted with unknown alg */
410 #if defined(__FreeBSD__)
411 	/* XXX-BZ do similar to rx.c for now as these are plenty. */
412 	if ((status & IWL_RX_MPDU_STATUS_SEC_MASK) ==
413 	    IWL_RX_MPDU_STATUS_SEC_ENC_ERR)
414 		return (0);
415 #endif
416 
417 	switch (status & IWL_RX_MPDU_STATUS_SEC_MASK) {
418 	case IWL_RX_MPDU_STATUS_SEC_CCM:
419 	case IWL_RX_MPDU_STATUS_SEC_GCM:
420 		BUILD_BUG_ON(IEEE80211_CCMP_PN_LEN != IEEE80211_GCMP_PN_LEN);
421 		/* alg is CCM: check MIC only */
422 		if (!(status & IWL_RX_MPDU_STATUS_MIC_OK))
423 			return -1;
424 
425 		stats->flag |= RX_FLAG_DECRYPTED;
426 		if (pkt_flags & FH_RSCSR_RADA_EN)
427 			stats->flag |= RX_FLAG_MIC_STRIPPED;
428 		*crypt_len = IEEE80211_CCMP_HDR_LEN;
429 		return 0;
430 	case IWL_RX_MPDU_STATUS_SEC_TKIP:
431 		/* Don't drop the frame and decrypt it in SW */
432 		if (!fw_has_api(&mvm->fw->ucode_capa,
433 				IWL_UCODE_TLV_API_DEPRECATE_TTAK) &&
434 		    !(status & IWL_RX_MPDU_RES_STATUS_TTAK_OK))
435 			return 0;
436 
437 		if (mvm->trans->trans_cfg->gen2 &&
438 		    !(status & RX_MPDU_RES_STATUS_MIC_OK))
439 			stats->flag |= RX_FLAG_MMIC_ERROR;
440 
441 		*crypt_len = IEEE80211_TKIP_IV_LEN;
442 		fallthrough;
443 	case IWL_RX_MPDU_STATUS_SEC_WEP:
444 		if (!(status & IWL_RX_MPDU_STATUS_ICV_OK))
445 			return -1;
446 
447 		stats->flag |= RX_FLAG_DECRYPTED;
448 		if ((status & IWL_RX_MPDU_STATUS_SEC_MASK) ==
449 				IWL_RX_MPDU_STATUS_SEC_WEP)
450 			*crypt_len = IEEE80211_WEP_IV_LEN;
451 
452 		if (pkt_flags & FH_RSCSR_RADA_EN) {
453 			stats->flag |= RX_FLAG_ICV_STRIPPED;
454 			if (mvm->trans->trans_cfg->gen2)
455 				stats->flag |= RX_FLAG_MMIC_STRIPPED;
456 		}
457 
458 		return 0;
459 	case IWL_RX_MPDU_STATUS_SEC_EXT_ENC:
460 		if (!(status & IWL_RX_MPDU_STATUS_MIC_OK))
461 			return -1;
462 		stats->flag |= RX_FLAG_DECRYPTED;
463 		return 0;
464 	case RX_MPDU_RES_STATUS_SEC_CMAC_GMAC_ENC:
465 		break;
466 	default:
467 		/*
468 		 * Sometimes we can get frames that were not decrypted
469 		 * because the firmware didn't have the keys yet. This can
470 		 * happen after connection where we can get multicast frames
471 		 * before the GTK is installed.
472 		 * Silently drop those frames.
473 		 * Also drop un-decrypted frames in monitor mode.
474 		 */
475 		if (!is_multicast_ether_addr(hdr->addr1) &&
476 		    !mvm->monitor_on && net_ratelimit())
477 #if defined(__linux__)
478 			IWL_ERR(mvm, "Unhandled alg: 0x%x\n", status);
479 #elif defined(__FreeBSD__)
480 			IWL_ERR(mvm, "%s: Unhandled alg: 0x%x\n",
481 			    __func__, status);
482 #endif
483 	}
484 
485 	return 0;
486 }
487 
488 static void iwl_mvm_rx_csum(struct iwl_mvm *mvm,
489 			    struct ieee80211_sta *sta,
490 			    struct sk_buff *skb,
491 			    struct iwl_rx_packet *pkt)
492 {
493 	struct iwl_rx_mpdu_desc *desc = (void *)pkt->data;
494 
495 	if (mvm->trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_AX210) {
496 		if (pkt->len_n_flags & cpu_to_le32(FH_RSCSR_RPA_EN)) {
497 			u16 hwsum = be16_to_cpu(desc->v3.raw_xsum);
498 
499 			skb->ip_summed = CHECKSUM_COMPLETE;
500 			skb->csum = csum_unfold(~(__force __sum16)hwsum);
501 		}
502 	} else {
503 		struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
504 		struct iwl_mvm_vif *mvmvif;
505 		u16 flags = le16_to_cpu(desc->l3l4_flags);
506 		u8 l3_prot = (u8)((flags & IWL_RX_L3L4_L3_PROTO_MASK) >>
507 				  IWL_RX_L3_PROTO_POS);
508 
509 		mvmvif = iwl_mvm_vif_from_mac80211(mvmsta->vif);
510 
511 		if (mvmvif->features & NETIF_F_RXCSUM &&
512 		    flags & IWL_RX_L3L4_TCP_UDP_CSUM_OK &&
513 		    (flags & IWL_RX_L3L4_IP_HDR_CSUM_OK ||
514 		     l3_prot == IWL_RX_L3_TYPE_IPV6 ||
515 		     l3_prot == IWL_RX_L3_TYPE_IPV6_FRAG))
516 			skb->ip_summed = CHECKSUM_UNNECESSARY;
517 	}
518 }
519 
520 /*
521  * returns true if a packet is a duplicate and should be dropped.
522  * Updates AMSDU PN tracking info
523  */
524 static bool iwl_mvm_is_dup(struct ieee80211_sta *sta, int queue,
525 			   struct ieee80211_rx_status *rx_status,
526 			   struct ieee80211_hdr *hdr,
527 			   struct iwl_rx_mpdu_desc *desc)
528 {
529 	struct iwl_mvm_sta *mvm_sta;
530 	struct iwl_mvm_rxq_dup_data *dup_data;
531 	u8 tid, sub_frame_idx;
532 
533 	if (WARN_ON(IS_ERR_OR_NULL(sta)))
534 		return false;
535 
536 	mvm_sta = iwl_mvm_sta_from_mac80211(sta);
537 #if defined(__FreeBSD__)
538 	if (WARN_ON(mvm_sta->dup_data == NULL))
539 		return false;
540 #endif
541 	dup_data = &mvm_sta->dup_data[queue];
542 
543 	/*
544 	 * Drop duplicate 802.11 retransmissions
545 	 * (IEEE 802.11-2012: 9.3.2.10 "Duplicate detection and recovery")
546 	 */
547 	if (ieee80211_is_ctl(hdr->frame_control) ||
548 	    ieee80211_is_qos_nullfunc(hdr->frame_control) ||
549 	    is_multicast_ether_addr(hdr->addr1)) {
550 		rx_status->flag |= RX_FLAG_DUP_VALIDATED;
551 		return false;
552 	}
553 
554 	if (ieee80211_is_data_qos(hdr->frame_control))
555 		/* frame has qos control */
556 		tid = ieee80211_get_tid(hdr);
557 	else
558 		tid = IWL_MAX_TID_COUNT;
559 
560 	/* If this wasn't a part of an A-MSDU the sub-frame index will be 0 */
561 	sub_frame_idx = desc->amsdu_info &
562 		IWL_RX_MPDU_AMSDU_SUBFRAME_IDX_MASK;
563 
564 	if (unlikely(ieee80211_has_retry(hdr->frame_control) &&
565 		     dup_data->last_seq[tid] == hdr->seq_ctrl &&
566 		     dup_data->last_sub_frame[tid] >= sub_frame_idx))
567 		return true;
568 
569 	/* Allow same PN as the first subframe for following sub frames */
570 	if (dup_data->last_seq[tid] == hdr->seq_ctrl &&
571 	    sub_frame_idx > dup_data->last_sub_frame[tid] &&
572 	    desc->mac_flags2 & IWL_RX_MPDU_MFLG2_AMSDU)
573 		rx_status->flag |= RX_FLAG_ALLOW_SAME_PN;
574 
575 	dup_data->last_seq[tid] = hdr->seq_ctrl;
576 	dup_data->last_sub_frame[tid] = sub_frame_idx;
577 
578 	rx_status->flag |= RX_FLAG_DUP_VALIDATED;
579 
580 	return false;
581 }
582 
583 /*
584  * Returns true if sn2 - buffer_size < sn1 < sn2.
585  * To be used only in order to compare reorder buffer head with NSSN.
586  * We fully trust NSSN unless it is behind us due to reorder timeout.
587  * Reorder timeout can only bring us up to buffer_size SNs ahead of NSSN.
588  */
589 static bool iwl_mvm_is_sn_less(u16 sn1, u16 sn2, u16 buffer_size)
590 {
591 	return ieee80211_sn_less(sn1, sn2) &&
592 	       !ieee80211_sn_less(sn1, sn2 - buffer_size);
593 }
594 
595 static void iwl_mvm_sync_nssn(struct iwl_mvm *mvm, u8 baid, u16 nssn)
596 {
597 	if (IWL_MVM_USE_NSSN_SYNC) {
598 		struct iwl_mvm_nssn_sync_data notif = {
599 			.baid = baid,
600 			.nssn = nssn,
601 		};
602 
603 		iwl_mvm_sync_rx_queues_internal(mvm, IWL_MVM_RXQ_NSSN_SYNC, false,
604 						&notif, sizeof(notif));
605 	}
606 }
607 
608 #define RX_REORDER_BUF_TIMEOUT_MQ (HZ / 10)
609 
610 enum iwl_mvm_release_flags {
611 	IWL_MVM_RELEASE_SEND_RSS_SYNC = BIT(0),
612 	IWL_MVM_RELEASE_FROM_RSS_SYNC = BIT(1),
613 };
614 
615 static void iwl_mvm_release_frames(struct iwl_mvm *mvm,
616 				   struct ieee80211_sta *sta,
617 				   struct napi_struct *napi,
618 				   struct iwl_mvm_baid_data *baid_data,
619 				   struct iwl_mvm_reorder_buffer *reorder_buf,
620 				   u16 nssn, u32 flags)
621 {
622 	struct iwl_mvm_reorder_buf_entry *entries =
623 		&baid_data->entries[reorder_buf->queue *
624 				    baid_data->entries_per_queue];
625 	u16 ssn = reorder_buf->head_sn;
626 
627 	lockdep_assert_held(&reorder_buf->lock);
628 
629 	/*
630 	 * We keep the NSSN not too far behind, if we are sync'ing it and it
631 	 * is more than 2048 ahead of us, it must be behind us. Discard it.
632 	 * This can happen if the queue that hit the 0 / 2048 seqno was lagging
633 	 * behind and this queue already processed packets. The next if
634 	 * would have caught cases where this queue would have processed less
635 	 * than 64 packets, but it may have processed more than 64 packets.
636 	 */
637 	if ((flags & IWL_MVM_RELEASE_FROM_RSS_SYNC) &&
638 	    ieee80211_sn_less(nssn, ssn))
639 		goto set_timer;
640 
641 	/* ignore nssn smaller than head sn - this can happen due to timeout */
642 	if (iwl_mvm_is_sn_less(nssn, ssn, reorder_buf->buf_size))
643 		goto set_timer;
644 
645 	while (iwl_mvm_is_sn_less(ssn, nssn, reorder_buf->buf_size)) {
646 		int index = ssn % reorder_buf->buf_size;
647 		struct sk_buff_head *skb_list = &entries[index].e.frames;
648 		struct sk_buff *skb;
649 
650 		ssn = ieee80211_sn_inc(ssn);
651 		if ((flags & IWL_MVM_RELEASE_SEND_RSS_SYNC) &&
652 		    (ssn == 2048 || ssn == 0))
653 			iwl_mvm_sync_nssn(mvm, baid_data->baid, ssn);
654 
655 		/*
656 		 * Empty the list. Will have more than one frame for A-MSDU.
657 		 * Empty list is valid as well since nssn indicates frames were
658 		 * received.
659 		 */
660 		while ((skb = __skb_dequeue(skb_list))) {
661 			iwl_mvm_pass_packet_to_mac80211(mvm, napi, skb,
662 							reorder_buf->queue,
663 							sta);
664 			reorder_buf->num_stored--;
665 		}
666 	}
667 	reorder_buf->head_sn = nssn;
668 
669 set_timer:
670 	if (reorder_buf->num_stored && !reorder_buf->removed) {
671 		u16 index = reorder_buf->head_sn % reorder_buf->buf_size;
672 
673 		while (skb_queue_empty(&entries[index].e.frames))
674 			index = (index + 1) % reorder_buf->buf_size;
675 		/* modify timer to match next frame's expiration time */
676 		mod_timer(&reorder_buf->reorder_timer,
677 			  entries[index].e.reorder_time + 1 +
678 			  RX_REORDER_BUF_TIMEOUT_MQ);
679 	} else {
680 		del_timer(&reorder_buf->reorder_timer);
681 	}
682 }
683 
684 void iwl_mvm_reorder_timer_expired(struct timer_list *t)
685 {
686 	struct iwl_mvm_reorder_buffer *buf = from_timer(buf, t, reorder_timer);
687 	struct iwl_mvm_baid_data *baid_data =
688 		iwl_mvm_baid_data_from_reorder_buf(buf);
689 	struct iwl_mvm_reorder_buf_entry *entries =
690 		&baid_data->entries[buf->queue * baid_data->entries_per_queue];
691 	int i;
692 	u16 sn = 0, index = 0;
693 	bool expired = false;
694 	bool cont = false;
695 
696 	spin_lock(&buf->lock);
697 
698 	if (!buf->num_stored || buf->removed) {
699 		spin_unlock(&buf->lock);
700 		return;
701 	}
702 
703 	for (i = 0; i < buf->buf_size ; i++) {
704 		index = (buf->head_sn + i) % buf->buf_size;
705 
706 		if (skb_queue_empty(&entries[index].e.frames)) {
707 			/*
708 			 * If there is a hole and the next frame didn't expire
709 			 * we want to break and not advance SN
710 			 */
711 			cont = false;
712 			continue;
713 		}
714 		if (!cont &&
715 		    !time_after(jiffies, entries[index].e.reorder_time +
716 					 RX_REORDER_BUF_TIMEOUT_MQ))
717 			break;
718 
719 		expired = true;
720 		/* continue until next hole after this expired frames */
721 		cont = true;
722 		sn = ieee80211_sn_add(buf->head_sn, i + 1);
723 	}
724 
725 	if (expired) {
726 		struct ieee80211_sta *sta;
727 		struct iwl_mvm_sta *mvmsta;
728 		u8 sta_id = baid_data->sta_id;
729 
730 		rcu_read_lock();
731 		sta = rcu_dereference(buf->mvm->fw_id_to_mac_id[sta_id]);
732 		mvmsta = iwl_mvm_sta_from_mac80211(sta);
733 
734 		/* SN is set to the last expired frame + 1 */
735 		IWL_DEBUG_HT(buf->mvm,
736 			     "Releasing expired frames for sta %u, sn %d\n",
737 			     sta_id, sn);
738 		iwl_mvm_event_frame_timeout_callback(buf->mvm, mvmsta->vif,
739 						     sta, baid_data->tid);
740 		iwl_mvm_release_frames(buf->mvm, sta, NULL, baid_data,
741 				       buf, sn, IWL_MVM_RELEASE_SEND_RSS_SYNC);
742 		rcu_read_unlock();
743 	} else {
744 		/*
745 		 * If no frame expired and there are stored frames, index is now
746 		 * pointing to the first unexpired frame - modify timer
747 		 * accordingly to this frame.
748 		 */
749 		mod_timer(&buf->reorder_timer,
750 			  entries[index].e.reorder_time +
751 			  1 + RX_REORDER_BUF_TIMEOUT_MQ);
752 	}
753 	spin_unlock(&buf->lock);
754 }
755 
756 static void iwl_mvm_del_ba(struct iwl_mvm *mvm, int queue,
757 			   struct iwl_mvm_delba_data *data)
758 {
759 	struct iwl_mvm_baid_data *ba_data;
760 	struct ieee80211_sta *sta;
761 	struct iwl_mvm_reorder_buffer *reorder_buf;
762 	u8 baid = data->baid;
763 
764 	if (WARN_ONCE(baid >= IWL_MAX_BAID, "invalid BAID: %x\n", baid))
765 		return;
766 
767 	rcu_read_lock();
768 
769 	ba_data = rcu_dereference(mvm->baid_map[baid]);
770 	if (WARN_ON_ONCE(!ba_data))
771 		goto out;
772 
773 	sta = rcu_dereference(mvm->fw_id_to_mac_id[ba_data->sta_id]);
774 	if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta)))
775 		goto out;
776 
777 	reorder_buf = &ba_data->reorder_buf[queue];
778 
779 	/* release all frames that are in the reorder buffer to the stack */
780 	spin_lock_bh(&reorder_buf->lock);
781 	iwl_mvm_release_frames(mvm, sta, NULL, ba_data, reorder_buf,
782 			       ieee80211_sn_add(reorder_buf->head_sn,
783 						reorder_buf->buf_size),
784 			       0);
785 	spin_unlock_bh(&reorder_buf->lock);
786 	del_timer_sync(&reorder_buf->reorder_timer);
787 
788 out:
789 	rcu_read_unlock();
790 }
791 
792 static void iwl_mvm_release_frames_from_notif(struct iwl_mvm *mvm,
793 					      struct napi_struct *napi,
794 					      u8 baid, u16 nssn, int queue,
795 					      u32 flags)
796 {
797 	struct ieee80211_sta *sta;
798 	struct iwl_mvm_reorder_buffer *reorder_buf;
799 	struct iwl_mvm_baid_data *ba_data;
800 
801 	IWL_DEBUG_HT(mvm, "Frame release notification for BAID %u, NSSN %d\n",
802 		     baid, nssn);
803 
804 	if (WARN_ON_ONCE(baid == IWL_RX_REORDER_DATA_INVALID_BAID ||
805 			 baid >= ARRAY_SIZE(mvm->baid_map)))
806 		return;
807 
808 	rcu_read_lock();
809 
810 	ba_data = rcu_dereference(mvm->baid_map[baid]);
811 	if (!ba_data) {
812 		WARN(!(flags & IWL_MVM_RELEASE_FROM_RSS_SYNC),
813 		     "BAID %d not found in map\n", baid);
814 		goto out;
815 	}
816 
817 	sta = rcu_dereference(mvm->fw_id_to_mac_id[ba_data->sta_id]);
818 	if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta)))
819 		goto out;
820 
821 	reorder_buf = &ba_data->reorder_buf[queue];
822 
823 	spin_lock_bh(&reorder_buf->lock);
824 	iwl_mvm_release_frames(mvm, sta, napi, ba_data,
825 			       reorder_buf, nssn, flags);
826 	spin_unlock_bh(&reorder_buf->lock);
827 
828 out:
829 	rcu_read_unlock();
830 }
831 
832 static void iwl_mvm_nssn_sync(struct iwl_mvm *mvm,
833 			      struct napi_struct *napi, int queue,
834 			      const struct iwl_mvm_nssn_sync_data *data)
835 {
836 	iwl_mvm_release_frames_from_notif(mvm, napi, data->baid,
837 					  data->nssn, queue,
838 					  IWL_MVM_RELEASE_FROM_RSS_SYNC);
839 }
840 
841 void iwl_mvm_rx_queue_notif(struct iwl_mvm *mvm, struct napi_struct *napi,
842 			    struct iwl_rx_cmd_buffer *rxb, int queue)
843 {
844 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
845 	struct iwl_rxq_sync_notification *notif;
846 	struct iwl_mvm_internal_rxq_notif *internal_notif;
847 	u32 len = iwl_rx_packet_payload_len(pkt);
848 
849 	notif = (void *)pkt->data;
850 	internal_notif = (void *)notif->payload;
851 
852 	if (WARN_ONCE(len < sizeof(*notif) + sizeof(*internal_notif),
853 		      "invalid notification size %d (%d)",
854 		      len, (int)(sizeof(*notif) + sizeof(*internal_notif))))
855 		return;
856 	len -= sizeof(*notif) + sizeof(*internal_notif);
857 
858 	if (internal_notif->sync &&
859 	    mvm->queue_sync_cookie != internal_notif->cookie) {
860 		WARN_ONCE(1, "Received expired RX queue sync message\n");
861 		return;
862 	}
863 
864 	switch (internal_notif->type) {
865 	case IWL_MVM_RXQ_EMPTY:
866 		WARN_ONCE(len, "invalid empty notification size %d", len);
867 		break;
868 	case IWL_MVM_RXQ_NOTIF_DEL_BA:
869 		if (WARN_ONCE(len != sizeof(struct iwl_mvm_delba_data),
870 			      "invalid delba notification size %d (%d)",
871 			      len, (int)sizeof(struct iwl_mvm_delba_data)))
872 			break;
873 		iwl_mvm_del_ba(mvm, queue, (void *)internal_notif->data);
874 		break;
875 	case IWL_MVM_RXQ_NSSN_SYNC:
876 		if (WARN_ONCE(len != sizeof(struct iwl_mvm_nssn_sync_data),
877 			      "invalid nssn sync notification size %d (%d)",
878 			      len, (int)sizeof(struct iwl_mvm_nssn_sync_data)))
879 			break;
880 		iwl_mvm_nssn_sync(mvm, napi, queue,
881 				  (void *)internal_notif->data);
882 		break;
883 	default:
884 		WARN_ONCE(1, "Invalid identifier %d", internal_notif->type);
885 	}
886 
887 	if (internal_notif->sync) {
888 		WARN_ONCE(!test_and_clear_bit(queue, &mvm->queue_sync_state),
889 			  "queue sync: queue %d responded a second time!\n",
890 			  queue);
891 		if (READ_ONCE(mvm->queue_sync_state) == 0)
892 			wake_up(&mvm->rx_sync_waitq);
893 	}
894 }
895 
896 static void iwl_mvm_oldsn_workaround(struct iwl_mvm *mvm,
897 				     struct ieee80211_sta *sta, int tid,
898 				     struct iwl_mvm_reorder_buffer *buffer,
899 				     u32 reorder, u32 gp2, int queue)
900 {
901 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
902 
903 	if (gp2 != buffer->consec_oldsn_ampdu_gp2) {
904 		/* we have a new (A-)MPDU ... */
905 
906 		/*
907 		 * reset counter to 0 if we didn't have any oldsn in
908 		 * the last A-MPDU (as detected by GP2 being identical)
909 		 */
910 		if (!buffer->consec_oldsn_prev_drop)
911 			buffer->consec_oldsn_drops = 0;
912 
913 		/* either way, update our tracking state */
914 		buffer->consec_oldsn_ampdu_gp2 = gp2;
915 	} else if (buffer->consec_oldsn_prev_drop) {
916 		/*
917 		 * tracking state didn't change, and we had an old SN
918 		 * indication before - do nothing in this case, we
919 		 * already noted this one down and are waiting for the
920 		 * next A-MPDU (by GP2)
921 		 */
922 		return;
923 	}
924 
925 	/* return unless this MPDU has old SN */
926 	if (!(reorder & IWL_RX_MPDU_REORDER_BA_OLD_SN))
927 		return;
928 
929 	/* update state */
930 	buffer->consec_oldsn_prev_drop = 1;
931 	buffer->consec_oldsn_drops++;
932 
933 	/* if limit is reached, send del BA and reset state */
934 	if (buffer->consec_oldsn_drops == IWL_MVM_AMPDU_CONSEC_DROPS_DELBA) {
935 		IWL_WARN(mvm,
936 			 "reached %d old SN frames from %pM on queue %d, stopping BA session on TID %d\n",
937 			 IWL_MVM_AMPDU_CONSEC_DROPS_DELBA,
938 			 sta->addr, queue, tid);
939 		ieee80211_stop_rx_ba_session(mvmsta->vif, BIT(tid), sta->addr);
940 		buffer->consec_oldsn_prev_drop = 0;
941 		buffer->consec_oldsn_drops = 0;
942 	}
943 }
944 
945 /*
946  * Returns true if the MPDU was buffered\dropped, false if it should be passed
947  * to upper layer.
948  */
949 static bool iwl_mvm_reorder(struct iwl_mvm *mvm,
950 			    struct napi_struct *napi,
951 			    int queue,
952 			    struct ieee80211_sta *sta,
953 			    struct sk_buff *skb,
954 			    struct iwl_rx_mpdu_desc *desc)
955 {
956 	struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
957 	struct ieee80211_hdr *hdr = iwl_mvm_skb_get_hdr(skb);
958 	struct iwl_mvm_sta *mvm_sta;
959 	struct iwl_mvm_baid_data *baid_data;
960 	struct iwl_mvm_reorder_buffer *buffer;
961 	struct sk_buff *tail;
962 	u32 reorder = le32_to_cpu(desc->reorder_data);
963 	bool amsdu = desc->mac_flags2 & IWL_RX_MPDU_MFLG2_AMSDU;
964 	bool last_subframe =
965 		desc->amsdu_info & IWL_RX_MPDU_AMSDU_LAST_SUBFRAME;
966 #if defined(__linux__)
967 	u8 tid = ieee80211_get_tid(hdr);
968 #elif defined(__FreeBSD__)
969 	u8 tid;
970 #endif
971 	u8 sub_frame_idx = desc->amsdu_info &
972 			   IWL_RX_MPDU_AMSDU_SUBFRAME_IDX_MASK;
973 	struct iwl_mvm_reorder_buf_entry *entries;
974 	int index;
975 	u16 nssn, sn;
976 	u8 baid;
977 
978 	baid = (reorder & IWL_RX_MPDU_REORDER_BAID_MASK) >>
979 		IWL_RX_MPDU_REORDER_BAID_SHIFT;
980 
981 	/*
982 	 * This also covers the case of receiving a Block Ack Request
983 	 * outside a BA session; we'll pass it to mac80211 and that
984 	 * then sends a delBA action frame.
985 	 * This also covers pure monitor mode, in which case we won't
986 	 * have any BA sessions.
987 	 */
988 	if (baid == IWL_RX_REORDER_DATA_INVALID_BAID)
989 		return false;
990 
991 	/* no sta yet */
992 	if (WARN_ONCE(IS_ERR_OR_NULL(sta),
993 		      "Got valid BAID without a valid station assigned\n"))
994 		return false;
995 
996 	mvm_sta = iwl_mvm_sta_from_mac80211(sta);
997 
998 	/* not a data packet or a bar */
999 	if (!ieee80211_is_back_req(hdr->frame_control) &&
1000 	    (!ieee80211_is_data_qos(hdr->frame_control) ||
1001 	     is_multicast_ether_addr(hdr->addr1)))
1002 		return false;
1003 
1004 	if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
1005 		return false;
1006 
1007 	baid_data = rcu_dereference(mvm->baid_map[baid]);
1008 	if (!baid_data) {
1009 		IWL_DEBUG_RX(mvm,
1010 			     "Got valid BAID but no baid allocated, bypass the re-ordering buffer. Baid %d reorder 0x%x\n",
1011 			      baid, reorder);
1012 		return false;
1013 	}
1014 
1015 #if defined(__FreeBSD__)
1016 	tid = ieee80211_get_tid(hdr);
1017 #endif
1018 	if (WARN(tid != baid_data->tid || mvm_sta->sta_id != baid_data->sta_id,
1019 		 "baid 0x%x is mapped to sta:%d tid:%d, but was received for sta:%d tid:%d\n",
1020 		 baid, baid_data->sta_id, baid_data->tid, mvm_sta->sta_id,
1021 		 tid))
1022 		return false;
1023 
1024 	nssn = reorder & IWL_RX_MPDU_REORDER_NSSN_MASK;
1025 	sn = (reorder & IWL_RX_MPDU_REORDER_SN_MASK) >>
1026 		IWL_RX_MPDU_REORDER_SN_SHIFT;
1027 
1028 	buffer = &baid_data->reorder_buf[queue];
1029 	entries = &baid_data->entries[queue * baid_data->entries_per_queue];
1030 
1031 	spin_lock_bh(&buffer->lock);
1032 
1033 	if (!buffer->valid) {
1034 		if (reorder & IWL_RX_MPDU_REORDER_BA_OLD_SN) {
1035 			spin_unlock_bh(&buffer->lock);
1036 			return false;
1037 		}
1038 		buffer->valid = true;
1039 	}
1040 
1041 	if (ieee80211_is_back_req(hdr->frame_control)) {
1042 		iwl_mvm_release_frames(mvm, sta, napi, baid_data,
1043 				       buffer, nssn, 0);
1044 		goto drop;
1045 	}
1046 
1047 	/*
1048 	 * If there was a significant jump in the nssn - adjust.
1049 	 * If the SN is smaller than the NSSN it might need to first go into
1050 	 * the reorder buffer, in which case we just release up to it and the
1051 	 * rest of the function will take care of storing it and releasing up to
1052 	 * the nssn.
1053 	 * This should not happen. This queue has been lagging and it should
1054 	 * have been updated by a IWL_MVM_RXQ_NSSN_SYNC notification. Be nice
1055 	 * and update the other queues.
1056 	 */
1057 	if (!iwl_mvm_is_sn_less(nssn, buffer->head_sn + buffer->buf_size,
1058 				buffer->buf_size) ||
1059 	    !ieee80211_sn_less(sn, buffer->head_sn + buffer->buf_size)) {
1060 		u16 min_sn = ieee80211_sn_less(sn, nssn) ? sn : nssn;
1061 
1062 		iwl_mvm_release_frames(mvm, sta, napi, baid_data, buffer,
1063 				       min_sn, IWL_MVM_RELEASE_SEND_RSS_SYNC);
1064 	}
1065 
1066 	iwl_mvm_oldsn_workaround(mvm, sta, tid, buffer, reorder,
1067 				 rx_status->device_timestamp, queue);
1068 
1069 	/* drop any oudated packets */
1070 	if (ieee80211_sn_less(sn, buffer->head_sn))
1071 		goto drop;
1072 
1073 	/* release immediately if allowed by nssn and no stored frames */
1074 	if (!buffer->num_stored && ieee80211_sn_less(sn, nssn)) {
1075 		if (iwl_mvm_is_sn_less(buffer->head_sn, nssn,
1076 				       buffer->buf_size) &&
1077 		   (!amsdu || last_subframe)) {
1078 			/*
1079 			 * If we crossed the 2048 or 0 SN, notify all the
1080 			 * queues. This is done in order to avoid having a
1081 			 * head_sn that lags behind for too long. When that
1082 			 * happens, we can get to a situation where the head_sn
1083 			 * is within the interval [nssn - buf_size : nssn]
1084 			 * which will make us think that the nssn is a packet
1085 			 * that we already freed because of the reordering
1086 			 * buffer and we will ignore it. So maintain the
1087 			 * head_sn somewhat updated across all the queues:
1088 			 * when it crosses 0 and 2048.
1089 			 */
1090 			if (sn == 2048 || sn == 0)
1091 				iwl_mvm_sync_nssn(mvm, baid, sn);
1092 			buffer->head_sn = nssn;
1093 		}
1094 		/* No need to update AMSDU last SN - we are moving the head */
1095 		spin_unlock_bh(&buffer->lock);
1096 		return false;
1097 	}
1098 
1099 	/*
1100 	 * release immediately if there are no stored frames, and the sn is
1101 	 * equal to the head.
1102 	 * This can happen due to reorder timer, where NSSN is behind head_sn.
1103 	 * When we released everything, and we got the next frame in the
1104 	 * sequence, according to the NSSN we can't release immediately,
1105 	 * while technically there is no hole and we can move forward.
1106 	 */
1107 	if (!buffer->num_stored && sn == buffer->head_sn) {
1108 		if (!amsdu || last_subframe) {
1109 			if (sn == 2048 || sn == 0)
1110 				iwl_mvm_sync_nssn(mvm, baid, sn);
1111 			buffer->head_sn = ieee80211_sn_inc(buffer->head_sn);
1112 		}
1113 		/* No need to update AMSDU last SN - we are moving the head */
1114 		spin_unlock_bh(&buffer->lock);
1115 		return false;
1116 	}
1117 
1118 	index = sn % buffer->buf_size;
1119 
1120 	/*
1121 	 * Check if we already stored this frame
1122 	 * As AMSDU is either received or not as whole, logic is simple:
1123 	 * If we have frames in that position in the buffer and the last frame
1124 	 * originated from AMSDU had a different SN then it is a retransmission.
1125 	 * If it is the same SN then if the subframe index is incrementing it
1126 	 * is the same AMSDU - otherwise it is a retransmission.
1127 	 */
1128 	tail = skb_peek_tail(&entries[index].e.frames);
1129 	if (tail && !amsdu)
1130 		goto drop;
1131 	else if (tail && (sn != buffer->last_amsdu ||
1132 			  buffer->last_sub_index >= sub_frame_idx))
1133 		goto drop;
1134 
1135 	/* put in reorder buffer */
1136 	__skb_queue_tail(&entries[index].e.frames, skb);
1137 	buffer->num_stored++;
1138 	entries[index].e.reorder_time = jiffies;
1139 
1140 	if (amsdu) {
1141 		buffer->last_amsdu = sn;
1142 		buffer->last_sub_index = sub_frame_idx;
1143 	}
1144 
1145 	/*
1146 	 * We cannot trust NSSN for AMSDU sub-frames that are not the last.
1147 	 * The reason is that NSSN advances on the first sub-frame, and may
1148 	 * cause the reorder buffer to advance before all the sub-frames arrive.
1149 	 * Example: reorder buffer contains SN 0 & 2, and we receive AMSDU with
1150 	 * SN 1. NSSN for first sub frame will be 3 with the result of driver
1151 	 * releasing SN 0,1, 2. When sub-frame 1 arrives - reorder buffer is
1152 	 * already ahead and it will be dropped.
1153 	 * If the last sub-frame is not on this queue - we will get frame
1154 	 * release notification with up to date NSSN.
1155 	 */
1156 	if (!amsdu || last_subframe)
1157 		iwl_mvm_release_frames(mvm, sta, napi, baid_data,
1158 				       buffer, nssn,
1159 				       IWL_MVM_RELEASE_SEND_RSS_SYNC);
1160 
1161 	spin_unlock_bh(&buffer->lock);
1162 	return true;
1163 
1164 drop:
1165 	kfree_skb(skb);
1166 	spin_unlock_bh(&buffer->lock);
1167 	return true;
1168 }
1169 
1170 static void iwl_mvm_agg_rx_received(struct iwl_mvm *mvm,
1171 				    u32 reorder_data, u8 baid)
1172 {
1173 	unsigned long now = jiffies;
1174 	unsigned long timeout;
1175 	struct iwl_mvm_baid_data *data;
1176 
1177 	rcu_read_lock();
1178 
1179 	data = rcu_dereference(mvm->baid_map[baid]);
1180 	if (!data) {
1181 		IWL_DEBUG_RX(mvm,
1182 			     "Got valid BAID but no baid allocated, bypass the re-ordering buffer. Baid %d reorder 0x%x\n",
1183 			      baid, reorder_data);
1184 		goto out;
1185 	}
1186 
1187 	if (!data->timeout)
1188 		goto out;
1189 
1190 	timeout = data->timeout;
1191 	/*
1192 	 * Do not update last rx all the time to avoid cache bouncing
1193 	 * between the rx queues.
1194 	 * Update it every timeout. Worst case is the session will
1195 	 * expire after ~ 2 * timeout, which doesn't matter that much.
1196 	 */
1197 	if (time_before(data->last_rx + TU_TO_JIFFIES(timeout), now))
1198 		/* Update is atomic */
1199 		data->last_rx = now;
1200 
1201 out:
1202 	rcu_read_unlock();
1203 }
1204 
1205 static void iwl_mvm_flip_address(u8 *addr)
1206 {
1207 	int i;
1208 	u8 mac_addr[ETH_ALEN];
1209 
1210 	for (i = 0; i < ETH_ALEN; i++)
1211 		mac_addr[i] = addr[ETH_ALEN - i - 1];
1212 	ether_addr_copy(addr, mac_addr);
1213 }
1214 
1215 struct iwl_mvm_rx_phy_data {
1216 	enum iwl_rx_phy_info_type info_type;
1217 	__le32 d0, d1, d2, d3;
1218 	__le16 d4;
1219 };
1220 
1221 static void iwl_mvm_decode_he_mu_ext(struct iwl_mvm *mvm,
1222 				     struct iwl_mvm_rx_phy_data *phy_data,
1223 				     u32 rate_n_flags,
1224 				     struct ieee80211_radiotap_he_mu *he_mu)
1225 {
1226 	u32 phy_data2 = le32_to_cpu(phy_data->d2);
1227 	u32 phy_data3 = le32_to_cpu(phy_data->d3);
1228 	u16 phy_data4 = le16_to_cpu(phy_data->d4);
1229 
1230 	if (FIELD_GET(IWL_RX_PHY_DATA4_HE_MU_EXT_CH1_CRC_OK, phy_data4)) {
1231 		he_mu->flags1 |=
1232 			cpu_to_le16(IEEE80211_RADIOTAP_HE_MU_FLAGS1_CH1_RU_KNOWN |
1233 				    IEEE80211_RADIOTAP_HE_MU_FLAGS1_CH1_CTR_26T_RU_KNOWN);
1234 
1235 		he_mu->flags1 |=
1236 			le16_encode_bits(FIELD_GET(IWL_RX_PHY_DATA4_HE_MU_EXT_CH1_CTR_RU,
1237 						   phy_data4),
1238 					 IEEE80211_RADIOTAP_HE_MU_FLAGS1_CH1_CTR_26T_RU);
1239 
1240 		he_mu->ru_ch1[0] = FIELD_GET(IWL_RX_PHY_DATA2_HE_MU_EXT_CH1_RU0,
1241 					     phy_data2);
1242 		he_mu->ru_ch1[1] = FIELD_GET(IWL_RX_PHY_DATA3_HE_MU_EXT_CH1_RU1,
1243 					     phy_data3);
1244 		he_mu->ru_ch1[2] = FIELD_GET(IWL_RX_PHY_DATA2_HE_MU_EXT_CH1_RU2,
1245 					     phy_data2);
1246 		he_mu->ru_ch1[3] = FIELD_GET(IWL_RX_PHY_DATA3_HE_MU_EXT_CH1_RU3,
1247 					     phy_data3);
1248 	}
1249 
1250 	if (FIELD_GET(IWL_RX_PHY_DATA4_HE_MU_EXT_CH2_CRC_OK, phy_data4) &&
1251 	    (rate_n_flags & RATE_MCS_CHAN_WIDTH_MSK_V1) != RATE_MCS_CHAN_WIDTH_20) {
1252 		he_mu->flags1 |=
1253 			cpu_to_le16(IEEE80211_RADIOTAP_HE_MU_FLAGS1_CH2_RU_KNOWN |
1254 				    IEEE80211_RADIOTAP_HE_MU_FLAGS1_CH2_CTR_26T_RU_KNOWN);
1255 
1256 		he_mu->flags2 |=
1257 			le16_encode_bits(FIELD_GET(IWL_RX_PHY_DATA4_HE_MU_EXT_CH2_CTR_RU,
1258 						   phy_data4),
1259 					 IEEE80211_RADIOTAP_HE_MU_FLAGS2_CH2_CTR_26T_RU);
1260 
1261 		he_mu->ru_ch2[0] = FIELD_GET(IWL_RX_PHY_DATA2_HE_MU_EXT_CH2_RU0,
1262 					     phy_data2);
1263 		he_mu->ru_ch2[1] = FIELD_GET(IWL_RX_PHY_DATA3_HE_MU_EXT_CH2_RU1,
1264 					     phy_data3);
1265 		he_mu->ru_ch2[2] = FIELD_GET(IWL_RX_PHY_DATA2_HE_MU_EXT_CH2_RU2,
1266 					     phy_data2);
1267 		he_mu->ru_ch2[3] = FIELD_GET(IWL_RX_PHY_DATA3_HE_MU_EXT_CH2_RU3,
1268 					     phy_data3);
1269 	}
1270 }
1271 
1272 static void
1273 iwl_mvm_decode_he_phy_ru_alloc(struct iwl_mvm_rx_phy_data *phy_data,
1274 			       u32 rate_n_flags,
1275 			       struct ieee80211_radiotap_he *he,
1276 			       struct ieee80211_radiotap_he_mu *he_mu,
1277 			       struct ieee80211_rx_status *rx_status)
1278 {
1279 	/*
1280 	 * Unfortunately, we have to leave the mac80211 data
1281 	 * incorrect for the case that we receive an HE-MU
1282 	 * transmission and *don't* have the HE phy data (due
1283 	 * to the bits being used for TSF). This shouldn't
1284 	 * happen though as management frames where we need
1285 	 * the TSF/timers are not be transmitted in HE-MU.
1286 	 */
1287 	u8 ru = le32_get_bits(phy_data->d1, IWL_RX_PHY_DATA1_HE_RU_ALLOC_MASK);
1288 	u32 he_type = rate_n_flags & RATE_MCS_HE_TYPE_MSK_V1;
1289 	u8 offs = 0;
1290 
1291 	rx_status->bw = RATE_INFO_BW_HE_RU;
1292 
1293 	he->data1 |= cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA1_BW_RU_ALLOC_KNOWN);
1294 
1295 	switch (ru) {
1296 	case 0 ... 36:
1297 		rx_status->he_ru = NL80211_RATE_INFO_HE_RU_ALLOC_26;
1298 		offs = ru;
1299 		break;
1300 	case 37 ... 52:
1301 		rx_status->he_ru = NL80211_RATE_INFO_HE_RU_ALLOC_52;
1302 		offs = ru - 37;
1303 		break;
1304 	case 53 ... 60:
1305 		rx_status->he_ru = NL80211_RATE_INFO_HE_RU_ALLOC_106;
1306 		offs = ru - 53;
1307 		break;
1308 	case 61 ... 64:
1309 		rx_status->he_ru = NL80211_RATE_INFO_HE_RU_ALLOC_242;
1310 		offs = ru - 61;
1311 		break;
1312 	case 65 ... 66:
1313 		rx_status->he_ru = NL80211_RATE_INFO_HE_RU_ALLOC_484;
1314 		offs = ru - 65;
1315 		break;
1316 	case 67:
1317 		rx_status->he_ru = NL80211_RATE_INFO_HE_RU_ALLOC_996;
1318 		break;
1319 	case 68:
1320 		rx_status->he_ru = NL80211_RATE_INFO_HE_RU_ALLOC_2x996;
1321 		break;
1322 	}
1323 	he->data2 |= le16_encode_bits(offs,
1324 				      IEEE80211_RADIOTAP_HE_DATA2_RU_OFFSET);
1325 	he->data2 |= cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA2_PRISEC_80_KNOWN |
1326 				 IEEE80211_RADIOTAP_HE_DATA2_RU_OFFSET_KNOWN);
1327 	if (phy_data->d1 & cpu_to_le32(IWL_RX_PHY_DATA1_HE_RU_ALLOC_SEC80))
1328 		he->data2 |=
1329 			cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA2_PRISEC_80_SEC);
1330 
1331 #define CHECK_BW(bw) \
1332 	BUILD_BUG_ON(IEEE80211_RADIOTAP_HE_MU_FLAGS2_BW_FROM_SIG_A_BW_ ## bw ## MHZ != \
1333 		     RATE_MCS_CHAN_WIDTH_##bw >> RATE_MCS_CHAN_WIDTH_POS); \
1334 	BUILD_BUG_ON(IEEE80211_RADIOTAP_HE_DATA6_TB_PPDU_BW_ ## bw ## MHZ != \
1335 		     RATE_MCS_CHAN_WIDTH_##bw >> RATE_MCS_CHAN_WIDTH_POS)
1336 	CHECK_BW(20);
1337 	CHECK_BW(40);
1338 	CHECK_BW(80);
1339 	CHECK_BW(160);
1340 
1341 	if (he_mu)
1342 		he_mu->flags2 |=
1343 			le16_encode_bits(FIELD_GET(RATE_MCS_CHAN_WIDTH_MSK_V1,
1344 						   rate_n_flags),
1345 					 IEEE80211_RADIOTAP_HE_MU_FLAGS2_BW_FROM_SIG_A_BW);
1346 	else if (he_type == RATE_MCS_HE_TYPE_TRIG_V1)
1347 		he->data6 |=
1348 			cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA6_TB_PPDU_BW_KNOWN) |
1349 			le16_encode_bits(FIELD_GET(RATE_MCS_CHAN_WIDTH_MSK_V1,
1350 						   rate_n_flags),
1351 					 IEEE80211_RADIOTAP_HE_DATA6_TB_PPDU_BW);
1352 }
1353 
1354 static void iwl_mvm_decode_he_phy_data(struct iwl_mvm *mvm,
1355 				       struct iwl_mvm_rx_phy_data *phy_data,
1356 				       struct ieee80211_radiotap_he *he,
1357 				       struct ieee80211_radiotap_he_mu *he_mu,
1358 				       struct ieee80211_rx_status *rx_status,
1359 				       u32 rate_n_flags, int queue)
1360 {
1361 	switch (phy_data->info_type) {
1362 	case IWL_RX_PHY_INFO_TYPE_NONE:
1363 	case IWL_RX_PHY_INFO_TYPE_CCK:
1364 	case IWL_RX_PHY_INFO_TYPE_OFDM_LGCY:
1365 	case IWL_RX_PHY_INFO_TYPE_HT:
1366 	case IWL_RX_PHY_INFO_TYPE_VHT_SU:
1367 	case IWL_RX_PHY_INFO_TYPE_VHT_MU:
1368 		return;
1369 	case IWL_RX_PHY_INFO_TYPE_HE_TB_EXT:
1370 		he->data1 |= cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA1_SPTL_REUSE_KNOWN |
1371 					 IEEE80211_RADIOTAP_HE_DATA1_SPTL_REUSE2_KNOWN |
1372 					 IEEE80211_RADIOTAP_HE_DATA1_SPTL_REUSE3_KNOWN |
1373 					 IEEE80211_RADIOTAP_HE_DATA1_SPTL_REUSE4_KNOWN);
1374 		he->data4 |= le16_encode_bits(le32_get_bits(phy_data->d2,
1375 							    IWL_RX_PHY_DATA2_HE_TB_EXT_SPTL_REUSE1),
1376 					      IEEE80211_RADIOTAP_HE_DATA4_TB_SPTL_REUSE1);
1377 		he->data4 |= le16_encode_bits(le32_get_bits(phy_data->d2,
1378 							    IWL_RX_PHY_DATA2_HE_TB_EXT_SPTL_REUSE2),
1379 					      IEEE80211_RADIOTAP_HE_DATA4_TB_SPTL_REUSE2);
1380 		he->data4 |= le16_encode_bits(le32_get_bits(phy_data->d2,
1381 							    IWL_RX_PHY_DATA2_HE_TB_EXT_SPTL_REUSE3),
1382 					      IEEE80211_RADIOTAP_HE_DATA4_TB_SPTL_REUSE3);
1383 		he->data4 |= le16_encode_bits(le32_get_bits(phy_data->d2,
1384 							    IWL_RX_PHY_DATA2_HE_TB_EXT_SPTL_REUSE4),
1385 					      IEEE80211_RADIOTAP_HE_DATA4_TB_SPTL_REUSE4);
1386 		fallthrough;
1387 	case IWL_RX_PHY_INFO_TYPE_HE_SU:
1388 	case IWL_RX_PHY_INFO_TYPE_HE_MU:
1389 	case IWL_RX_PHY_INFO_TYPE_HE_MU_EXT:
1390 	case IWL_RX_PHY_INFO_TYPE_HE_TB:
1391 		/* HE common */
1392 		he->data1 |= cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA1_LDPC_XSYMSEG_KNOWN |
1393 					 IEEE80211_RADIOTAP_HE_DATA1_DOPPLER_KNOWN |
1394 					 IEEE80211_RADIOTAP_HE_DATA1_BSS_COLOR_KNOWN);
1395 		he->data2 |= cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA2_PRE_FEC_PAD_KNOWN |
1396 					 IEEE80211_RADIOTAP_HE_DATA2_PE_DISAMBIG_KNOWN |
1397 					 IEEE80211_RADIOTAP_HE_DATA2_TXOP_KNOWN |
1398 					 IEEE80211_RADIOTAP_HE_DATA2_NUM_LTF_SYMS_KNOWN);
1399 		he->data3 |= le16_encode_bits(le32_get_bits(phy_data->d0,
1400 							    IWL_RX_PHY_DATA0_HE_BSS_COLOR_MASK),
1401 					      IEEE80211_RADIOTAP_HE_DATA3_BSS_COLOR);
1402 		if (phy_data->info_type != IWL_RX_PHY_INFO_TYPE_HE_TB &&
1403 		    phy_data->info_type != IWL_RX_PHY_INFO_TYPE_HE_TB_EXT) {
1404 			he->data1 |= cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA1_UL_DL_KNOWN);
1405 			he->data3 |= le16_encode_bits(le32_get_bits(phy_data->d0,
1406 							    IWL_RX_PHY_DATA0_HE_UPLINK),
1407 						      IEEE80211_RADIOTAP_HE_DATA3_UL_DL);
1408 		}
1409 		he->data3 |= le16_encode_bits(le32_get_bits(phy_data->d0,
1410 							    IWL_RX_PHY_DATA0_HE_LDPC_EXT_SYM),
1411 					      IEEE80211_RADIOTAP_HE_DATA3_LDPC_XSYMSEG);
1412 		he->data5 |= le16_encode_bits(le32_get_bits(phy_data->d0,
1413 							    IWL_RX_PHY_DATA0_HE_PRE_FEC_PAD_MASK),
1414 					      IEEE80211_RADIOTAP_HE_DATA5_PRE_FEC_PAD);
1415 		he->data5 |= le16_encode_bits(le32_get_bits(phy_data->d0,
1416 							    IWL_RX_PHY_DATA0_HE_PE_DISAMBIG),
1417 					      IEEE80211_RADIOTAP_HE_DATA5_PE_DISAMBIG);
1418 		he->data5 |= le16_encode_bits(le32_get_bits(phy_data->d1,
1419 							    IWL_RX_PHY_DATA1_HE_LTF_NUM_MASK),
1420 					      IEEE80211_RADIOTAP_HE_DATA5_NUM_LTF_SYMS);
1421 		he->data6 |= le16_encode_bits(le32_get_bits(phy_data->d0,
1422 							    IWL_RX_PHY_DATA0_HE_TXOP_DUR_MASK),
1423 					      IEEE80211_RADIOTAP_HE_DATA6_TXOP);
1424 		he->data6 |= le16_encode_bits(le32_get_bits(phy_data->d0,
1425 							    IWL_RX_PHY_DATA0_HE_DOPPLER),
1426 					      IEEE80211_RADIOTAP_HE_DATA6_DOPPLER);
1427 		break;
1428 	}
1429 
1430 	switch (phy_data->info_type) {
1431 	case IWL_RX_PHY_INFO_TYPE_HE_MU_EXT:
1432 	case IWL_RX_PHY_INFO_TYPE_HE_MU:
1433 	case IWL_RX_PHY_INFO_TYPE_HE_SU:
1434 		he->data1 |= cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA1_SPTL_REUSE_KNOWN);
1435 		he->data4 |= le16_encode_bits(le32_get_bits(phy_data->d0,
1436 							    IWL_RX_PHY_DATA0_HE_SPATIAL_REUSE_MASK),
1437 					      IEEE80211_RADIOTAP_HE_DATA4_SU_MU_SPTL_REUSE);
1438 		break;
1439 	default:
1440 		/* nothing here */
1441 		break;
1442 	}
1443 
1444 	switch (phy_data->info_type) {
1445 	case IWL_RX_PHY_INFO_TYPE_HE_MU_EXT:
1446 		he_mu->flags1 |=
1447 			le16_encode_bits(le16_get_bits(phy_data->d4,
1448 						       IWL_RX_PHY_DATA4_HE_MU_EXT_SIGB_DCM),
1449 					 IEEE80211_RADIOTAP_HE_MU_FLAGS1_SIG_B_DCM);
1450 		he_mu->flags1 |=
1451 			le16_encode_bits(le16_get_bits(phy_data->d4,
1452 						       IWL_RX_PHY_DATA4_HE_MU_EXT_SIGB_MCS_MASK),
1453 					 IEEE80211_RADIOTAP_HE_MU_FLAGS1_SIG_B_MCS);
1454 		he_mu->flags2 |=
1455 			le16_encode_bits(le16_get_bits(phy_data->d4,
1456 						       IWL_RX_PHY_DATA4_HE_MU_EXT_PREAMBLE_PUNC_TYPE_MASK),
1457 					 IEEE80211_RADIOTAP_HE_MU_FLAGS2_PUNC_FROM_SIG_A_BW);
1458 		iwl_mvm_decode_he_mu_ext(mvm, phy_data, rate_n_flags, he_mu);
1459 		fallthrough;
1460 	case IWL_RX_PHY_INFO_TYPE_HE_MU:
1461 		he_mu->flags2 |=
1462 			le16_encode_bits(le32_get_bits(phy_data->d1,
1463 						       IWL_RX_PHY_DATA1_HE_MU_SIBG_SYM_OR_USER_NUM_MASK),
1464 					 IEEE80211_RADIOTAP_HE_MU_FLAGS2_SIG_B_SYMS_USERS);
1465 		he_mu->flags2 |=
1466 			le16_encode_bits(le32_get_bits(phy_data->d1,
1467 						       IWL_RX_PHY_DATA1_HE_MU_SIGB_COMPRESSION),
1468 					 IEEE80211_RADIOTAP_HE_MU_FLAGS2_SIG_B_COMP);
1469 		fallthrough;
1470 	case IWL_RX_PHY_INFO_TYPE_HE_TB:
1471 	case IWL_RX_PHY_INFO_TYPE_HE_TB_EXT:
1472 		iwl_mvm_decode_he_phy_ru_alloc(phy_data, rate_n_flags,
1473 					       he, he_mu, rx_status);
1474 		break;
1475 	case IWL_RX_PHY_INFO_TYPE_HE_SU:
1476 		he->data1 |= cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA1_BEAM_CHANGE_KNOWN);
1477 		he->data3 |= le16_encode_bits(le32_get_bits(phy_data->d0,
1478 							    IWL_RX_PHY_DATA0_HE_BEAM_CHNG),
1479 					      IEEE80211_RADIOTAP_HE_DATA3_BEAM_CHANGE);
1480 		break;
1481 	default:
1482 		/* nothing */
1483 		break;
1484 	}
1485 }
1486 
1487 static void iwl_mvm_rx_he(struct iwl_mvm *mvm, struct sk_buff *skb,
1488 			  struct iwl_mvm_rx_phy_data *phy_data,
1489 			  u32 rate_n_flags, u16 phy_info, int queue)
1490 {
1491 	struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
1492 	struct ieee80211_radiotap_he *he = NULL;
1493 	struct ieee80211_radiotap_he_mu *he_mu = NULL;
1494 	u32 he_type = rate_n_flags & RATE_MCS_HE_TYPE_MSK;
1495 	u8 stbc, ltf;
1496 	static const struct ieee80211_radiotap_he known = {
1497 		.data1 = cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA1_DATA_MCS_KNOWN |
1498 				     IEEE80211_RADIOTAP_HE_DATA1_DATA_DCM_KNOWN |
1499 				     IEEE80211_RADIOTAP_HE_DATA1_STBC_KNOWN |
1500 				     IEEE80211_RADIOTAP_HE_DATA1_CODING_KNOWN),
1501 		.data2 = cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA2_GI_KNOWN |
1502 				     IEEE80211_RADIOTAP_HE_DATA2_TXBF_KNOWN),
1503 	};
1504 	static const struct ieee80211_radiotap_he_mu mu_known = {
1505 		.flags1 = cpu_to_le16(IEEE80211_RADIOTAP_HE_MU_FLAGS1_SIG_B_MCS_KNOWN |
1506 				      IEEE80211_RADIOTAP_HE_MU_FLAGS1_SIG_B_DCM_KNOWN |
1507 				      IEEE80211_RADIOTAP_HE_MU_FLAGS1_SIG_B_SYMS_USERS_KNOWN |
1508 				      IEEE80211_RADIOTAP_HE_MU_FLAGS1_SIG_B_COMP_KNOWN),
1509 		.flags2 = cpu_to_le16(IEEE80211_RADIOTAP_HE_MU_FLAGS2_PUNC_FROM_SIG_A_BW_KNOWN |
1510 				      IEEE80211_RADIOTAP_HE_MU_FLAGS2_BW_FROM_SIG_A_BW_KNOWN),
1511 	};
1512 
1513 	he = skb_put_data(skb, &known, sizeof(known));
1514 	rx_status->flag |= RX_FLAG_RADIOTAP_HE;
1515 
1516 	if (phy_data->info_type == IWL_RX_PHY_INFO_TYPE_HE_MU ||
1517 	    phy_data->info_type == IWL_RX_PHY_INFO_TYPE_HE_MU_EXT) {
1518 		he_mu = skb_put_data(skb, &mu_known, sizeof(mu_known));
1519 		rx_status->flag |= RX_FLAG_RADIOTAP_HE_MU;
1520 	}
1521 
1522 	/* report the AMPDU-EOF bit on single frames */
1523 	if (!queue && !(phy_info & IWL_RX_MPDU_PHY_AMPDU)) {
1524 		rx_status->flag |= RX_FLAG_AMPDU_DETAILS;
1525 		rx_status->flag |= RX_FLAG_AMPDU_EOF_BIT_KNOWN;
1526 		if (phy_data->d0 & cpu_to_le32(IWL_RX_PHY_DATA0_HE_DELIM_EOF))
1527 			rx_status->flag |= RX_FLAG_AMPDU_EOF_BIT;
1528 	}
1529 
1530 	if (phy_info & IWL_RX_MPDU_PHY_TSF_OVERLOAD)
1531 		iwl_mvm_decode_he_phy_data(mvm, phy_data, he, he_mu, rx_status,
1532 					   rate_n_flags, queue);
1533 
1534 	/* update aggregation data for monitor sake on default queue */
1535 	if (!queue && (phy_info & IWL_RX_MPDU_PHY_TSF_OVERLOAD) &&
1536 	    (phy_info & IWL_RX_MPDU_PHY_AMPDU)) {
1537 		bool toggle_bit = phy_info & IWL_RX_MPDU_PHY_AMPDU_TOGGLE;
1538 
1539 		/* toggle is switched whenever new aggregation starts */
1540 		if (toggle_bit != mvm->ampdu_toggle) {
1541 			rx_status->flag |= RX_FLAG_AMPDU_EOF_BIT_KNOWN;
1542 			if (phy_data->d0 & cpu_to_le32(IWL_RX_PHY_DATA0_HE_DELIM_EOF))
1543 				rx_status->flag |= RX_FLAG_AMPDU_EOF_BIT;
1544 		}
1545 	}
1546 
1547 	if (he_type == RATE_MCS_HE_TYPE_EXT_SU &&
1548 	    rate_n_flags & RATE_MCS_HE_106T_MSK) {
1549 		rx_status->bw = RATE_INFO_BW_HE_RU;
1550 		rx_status->he_ru = NL80211_RATE_INFO_HE_RU_ALLOC_106;
1551 	}
1552 
1553 	/* actually data is filled in mac80211 */
1554 	if (he_type == RATE_MCS_HE_TYPE_SU ||
1555 	    he_type == RATE_MCS_HE_TYPE_EXT_SU)
1556 		he->data1 |=
1557 			cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA1_BW_RU_ALLOC_KNOWN);
1558 
1559 	stbc = (rate_n_flags & RATE_MCS_STBC_MSK) >> RATE_MCS_STBC_POS;
1560 	rx_status->nss =
1561 		((rate_n_flags & RATE_MCS_NSS_MSK) >>
1562 		 RATE_MCS_NSS_POS) + 1;
1563 	rx_status->rate_idx = rate_n_flags & RATE_MCS_CODE_MSK;
1564 	rx_status->encoding = RX_ENC_HE;
1565 	rx_status->enc_flags |= stbc << RX_ENC_FLAG_STBC_SHIFT;
1566 	if (rate_n_flags & RATE_MCS_BF_MSK)
1567 		rx_status->enc_flags |= RX_ENC_FLAG_BF;
1568 
1569 	rx_status->he_dcm =
1570 		!!(rate_n_flags & RATE_HE_DUAL_CARRIER_MODE_MSK);
1571 
1572 #define CHECK_TYPE(F)							\
1573 	BUILD_BUG_ON(IEEE80211_RADIOTAP_HE_DATA1_FORMAT_ ## F !=	\
1574 		     (RATE_MCS_HE_TYPE_ ## F >> RATE_MCS_HE_TYPE_POS))
1575 
1576 	CHECK_TYPE(SU);
1577 	CHECK_TYPE(EXT_SU);
1578 	CHECK_TYPE(MU);
1579 	CHECK_TYPE(TRIG);
1580 
1581 	he->data1 |= cpu_to_le16(he_type >> RATE_MCS_HE_TYPE_POS);
1582 
1583 	if (rate_n_flags & RATE_MCS_BF_MSK)
1584 		he->data5 |= cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA5_TXBF);
1585 
1586 	switch ((rate_n_flags & RATE_MCS_HE_GI_LTF_MSK) >>
1587 		RATE_MCS_HE_GI_LTF_POS) {
1588 	case 0:
1589 		if (he_type == RATE_MCS_HE_TYPE_TRIG)
1590 			rx_status->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
1591 		else
1592 			rx_status->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
1593 		if (he_type == RATE_MCS_HE_TYPE_MU)
1594 			ltf = IEEE80211_RADIOTAP_HE_DATA5_LTF_SIZE_4X;
1595 		else
1596 			ltf = IEEE80211_RADIOTAP_HE_DATA5_LTF_SIZE_1X;
1597 		break;
1598 	case 1:
1599 		if (he_type == RATE_MCS_HE_TYPE_TRIG)
1600 			rx_status->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
1601 		else
1602 			rx_status->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
1603 		ltf = IEEE80211_RADIOTAP_HE_DATA5_LTF_SIZE_2X;
1604 		break;
1605 	case 2:
1606 		if (he_type == RATE_MCS_HE_TYPE_TRIG) {
1607 			rx_status->he_gi = NL80211_RATE_INFO_HE_GI_3_2;
1608 			ltf = IEEE80211_RADIOTAP_HE_DATA5_LTF_SIZE_4X;
1609 		} else {
1610 			rx_status->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
1611 			ltf = IEEE80211_RADIOTAP_HE_DATA5_LTF_SIZE_2X;
1612 		}
1613 		break;
1614 	case 3:
1615 		rx_status->he_gi = NL80211_RATE_INFO_HE_GI_3_2;
1616 		ltf = IEEE80211_RADIOTAP_HE_DATA5_LTF_SIZE_4X;
1617 		break;
1618 	case 4:
1619 		rx_status->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
1620 		ltf = IEEE80211_RADIOTAP_HE_DATA5_LTF_SIZE_4X;
1621 		break;
1622 	default:
1623 		ltf = IEEE80211_RADIOTAP_HE_DATA5_LTF_SIZE_UNKNOWN;
1624 	}
1625 
1626 	he->data5 |= le16_encode_bits(ltf,
1627 				      IEEE80211_RADIOTAP_HE_DATA5_LTF_SIZE);
1628 }
1629 
1630 static void iwl_mvm_decode_lsig(struct sk_buff *skb,
1631 				struct iwl_mvm_rx_phy_data *phy_data)
1632 {
1633 	struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
1634 	struct ieee80211_radiotap_lsig *lsig;
1635 
1636 	switch (phy_data->info_type) {
1637 	case IWL_RX_PHY_INFO_TYPE_HT:
1638 	case IWL_RX_PHY_INFO_TYPE_VHT_SU:
1639 	case IWL_RX_PHY_INFO_TYPE_VHT_MU:
1640 	case IWL_RX_PHY_INFO_TYPE_HE_TB_EXT:
1641 	case IWL_RX_PHY_INFO_TYPE_HE_SU:
1642 	case IWL_RX_PHY_INFO_TYPE_HE_MU:
1643 	case IWL_RX_PHY_INFO_TYPE_HE_MU_EXT:
1644 	case IWL_RX_PHY_INFO_TYPE_HE_TB:
1645 		lsig = skb_put(skb, sizeof(*lsig));
1646 		lsig->data1 = cpu_to_le16(IEEE80211_RADIOTAP_LSIG_DATA1_LENGTH_KNOWN);
1647 		lsig->data2 = le16_encode_bits(le32_get_bits(phy_data->d1,
1648 							     IWL_RX_PHY_DATA1_LSIG_LEN_MASK),
1649 					       IEEE80211_RADIOTAP_LSIG_DATA2_LENGTH);
1650 		rx_status->flag |= RX_FLAG_RADIOTAP_LSIG;
1651 		break;
1652 	default:
1653 		break;
1654 	}
1655 }
1656 
1657 static inline u8 iwl_mvm_nl80211_band_from_rx_msdu(u8 phy_band)
1658 {
1659 	switch (phy_band) {
1660 	case PHY_BAND_24:
1661 		return NL80211_BAND_2GHZ;
1662 	case PHY_BAND_5:
1663 		return NL80211_BAND_5GHZ;
1664 	case PHY_BAND_6:
1665 		return NL80211_BAND_6GHZ;
1666 	default:
1667 		WARN_ONCE(1, "Unsupported phy band (%u)\n", phy_band);
1668 		return NL80211_BAND_5GHZ;
1669 	}
1670 }
1671 
1672 struct iwl_rx_sta_csa {
1673 	bool all_sta_unblocked;
1674 	struct ieee80211_vif *vif;
1675 };
1676 
1677 static void iwl_mvm_rx_get_sta_block_tx(void *data, struct ieee80211_sta *sta)
1678 {
1679 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1680 	struct iwl_rx_sta_csa *rx_sta_csa = data;
1681 
1682 	if (mvmsta->vif != rx_sta_csa->vif)
1683 		return;
1684 
1685 	if (mvmsta->disable_tx)
1686 		rx_sta_csa->all_sta_unblocked = false;
1687 }
1688 
1689 void iwl_mvm_rx_mpdu_mq(struct iwl_mvm *mvm, struct napi_struct *napi,
1690 			struct iwl_rx_cmd_buffer *rxb, int queue)
1691 {
1692 	struct ieee80211_rx_status *rx_status;
1693 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1694 	struct iwl_rx_mpdu_desc *desc = (void *)pkt->data;
1695 	struct ieee80211_hdr *hdr;
1696 	u32 len;
1697 	u32 pkt_len = iwl_rx_packet_payload_len(pkt);
1698 	u32 rate_n_flags, gp2_on_air_rise;
1699 	u16 phy_info;
1700 	struct ieee80211_sta *sta = NULL;
1701 	struct sk_buff *skb;
1702 	u8 crypt_len = 0, channel, energy_a, energy_b;
1703 	size_t desc_size;
1704 	struct iwl_mvm_rx_phy_data phy_data = {
1705 		.info_type = IWL_RX_PHY_INFO_TYPE_NONE,
1706 	};
1707 	u32 format;
1708 	bool is_sgi;
1709 
1710 	if (unlikely(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)))
1711 		return;
1712 
1713 	if (mvm->trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_AX210)
1714 		desc_size = sizeof(*desc);
1715 	else
1716 		desc_size = IWL_RX_DESC_SIZE_V1;
1717 
1718 	if (unlikely(pkt_len < desc_size)) {
1719 		IWL_DEBUG_DROP(mvm, "Bad REPLY_RX_MPDU_CMD size\n");
1720 		return;
1721 	}
1722 
1723 	if (mvm->trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_AX210) {
1724 		rate_n_flags = le32_to_cpu(desc->v3.rate_n_flags);
1725 		channel = desc->v3.channel;
1726 		gp2_on_air_rise = le32_to_cpu(desc->v3.gp2_on_air_rise);
1727 		energy_a = desc->v3.energy_a;
1728 		energy_b = desc->v3.energy_b;
1729 
1730 		phy_data.d0 = desc->v3.phy_data0;
1731 		phy_data.d1 = desc->v3.phy_data1;
1732 		phy_data.d2 = desc->v3.phy_data2;
1733 		phy_data.d3 = desc->v3.phy_data3;
1734 	} else {
1735 		rate_n_flags = le32_to_cpu(desc->v1.rate_n_flags);
1736 		channel = desc->v1.channel;
1737 		gp2_on_air_rise = le32_to_cpu(desc->v1.gp2_on_air_rise);
1738 		energy_a = desc->v1.energy_a;
1739 		energy_b = desc->v1.energy_b;
1740 
1741 		phy_data.d0 = desc->v1.phy_data0;
1742 		phy_data.d1 = desc->v1.phy_data1;
1743 		phy_data.d2 = desc->v1.phy_data2;
1744 		phy_data.d3 = desc->v1.phy_data3;
1745 	}
1746 	if (iwl_fw_lookup_notif_ver(mvm->fw, LEGACY_GROUP,
1747 				    REPLY_RX_MPDU_CMD, 0) < 4) {
1748 		rate_n_flags = iwl_new_rate_from_v1(rate_n_flags);
1749 		IWL_DEBUG_DROP(mvm, "Got old format rate, converting. New rate: 0x%x\n",
1750 			       rate_n_flags);
1751 	}
1752 	format = rate_n_flags & RATE_MCS_MOD_TYPE_MSK;
1753 
1754 	len = le16_to_cpu(desc->mpdu_len);
1755 
1756 	if (unlikely(len + desc_size > pkt_len)) {
1757 		IWL_DEBUG_DROP(mvm, "FW lied about packet len\n");
1758 		return;
1759 	}
1760 
1761 	phy_info = le16_to_cpu(desc->phy_info);
1762 	phy_data.d4 = desc->phy_data4;
1763 
1764 	if (phy_info & IWL_RX_MPDU_PHY_TSF_OVERLOAD)
1765 		phy_data.info_type =
1766 			le32_get_bits(phy_data.d1,
1767 				      IWL_RX_PHY_DATA1_INFO_TYPE_MASK);
1768 
1769 	hdr = (void *)(pkt->data + desc_size);
1770 	/* Dont use dev_alloc_skb(), we'll have enough headroom once
1771 	 * ieee80211_hdr pulled.
1772 	 */
1773 	skb = alloc_skb(128, GFP_ATOMIC);
1774 	if (!skb) {
1775 		IWL_ERR(mvm, "alloc_skb failed\n");
1776 		return;
1777 	}
1778 
1779 	if (desc->mac_flags2 & IWL_RX_MPDU_MFLG2_PAD) {
1780 		/*
1781 		 * If the device inserted padding it means that (it thought)
1782 		 * the 802.11 header wasn't a multiple of 4 bytes long. In
1783 		 * this case, reserve two bytes at the start of the SKB to
1784 		 * align the payload properly in case we end up copying it.
1785 		 */
1786 		skb_reserve(skb, 2);
1787 	}
1788 
1789 	rx_status = IEEE80211_SKB_RXCB(skb);
1790 
1791 	/* This may be overridden by iwl_mvm_rx_he() to HE_RU */
1792 	switch (rate_n_flags & RATE_MCS_CHAN_WIDTH_MSK) {
1793 	case RATE_MCS_CHAN_WIDTH_20:
1794 		break;
1795 	case RATE_MCS_CHAN_WIDTH_40:
1796 		rx_status->bw = RATE_INFO_BW_40;
1797 		break;
1798 	case RATE_MCS_CHAN_WIDTH_80:
1799 		rx_status->bw = RATE_INFO_BW_80;
1800 		break;
1801 	case RATE_MCS_CHAN_WIDTH_160:
1802 		rx_status->bw = RATE_INFO_BW_160;
1803 		break;
1804 	}
1805 
1806 	if (format == RATE_MCS_HE_MSK)
1807 		iwl_mvm_rx_he(mvm, skb, &phy_data, rate_n_flags,
1808 			      phy_info, queue);
1809 
1810 	iwl_mvm_decode_lsig(skb, &phy_data);
1811 
1812 	/*
1813 	 * Keep packets with CRC errors (and with overrun) for monitor mode
1814 	 * (otherwise the firmware discards them) but mark them as bad.
1815 	 */
1816 	if (!(desc->status & cpu_to_le32(IWL_RX_MPDU_STATUS_CRC_OK)) ||
1817 	    !(desc->status & cpu_to_le32(IWL_RX_MPDU_STATUS_OVERRUN_OK))) {
1818 		IWL_DEBUG_RX(mvm, "Bad CRC or FIFO: 0x%08X.\n",
1819 			     le32_to_cpu(desc->status));
1820 		rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
1821 	}
1822 	/* set the preamble flag if appropriate */
1823 	if (format == RATE_MCS_CCK_MSK &&
1824 	    phy_info & IWL_RX_MPDU_PHY_SHORT_PREAMBLE)
1825 		rx_status->enc_flags |= RX_ENC_FLAG_SHORTPRE;
1826 
1827 	if (likely(!(phy_info & IWL_RX_MPDU_PHY_TSF_OVERLOAD))) {
1828 		u64 tsf_on_air_rise;
1829 
1830 		if (mvm->trans->trans_cfg->device_family >=
1831 		    IWL_DEVICE_FAMILY_AX210)
1832 			tsf_on_air_rise = le64_to_cpu(desc->v3.tsf_on_air_rise);
1833 		else
1834 			tsf_on_air_rise = le64_to_cpu(desc->v1.tsf_on_air_rise);
1835 
1836 		rx_status->mactime = tsf_on_air_rise;
1837 		/* TSF as indicated by the firmware is at INA time */
1838 		rx_status->flag |= RX_FLAG_MACTIME_PLCP_START;
1839 	}
1840 
1841 	rx_status->device_timestamp = gp2_on_air_rise;
1842 	if (iwl_mvm_is_band_in_rx_supported(mvm)) {
1843 		u8 band = BAND_IN_RX_STATUS(desc->mac_phy_idx);
1844 
1845 		rx_status->band = iwl_mvm_nl80211_band_from_rx_msdu(band);
1846 	} else {
1847 		rx_status->band = channel > 14 ? NL80211_BAND_5GHZ :
1848 			NL80211_BAND_2GHZ;
1849 	}
1850 	rx_status->freq = ieee80211_channel_to_frequency(channel,
1851 							 rx_status->band);
1852 	iwl_mvm_get_signal_strength(mvm, rx_status, rate_n_flags, energy_a,
1853 				    energy_b);
1854 
1855 	/* update aggregation data for monitor sake on default queue */
1856 	if (!queue && (phy_info & IWL_RX_MPDU_PHY_AMPDU)) {
1857 		bool toggle_bit = phy_info & IWL_RX_MPDU_PHY_AMPDU_TOGGLE;
1858 
1859 		rx_status->flag |= RX_FLAG_AMPDU_DETAILS;
1860 		/*
1861 		 * Toggle is switched whenever new aggregation starts. Make
1862 		 * sure ampdu_reference is never 0 so we can later use it to
1863 		 * see if the frame was really part of an A-MPDU or not.
1864 		 */
1865 		if (toggle_bit != mvm->ampdu_toggle) {
1866 			mvm->ampdu_ref++;
1867 			if (mvm->ampdu_ref == 0)
1868 				mvm->ampdu_ref++;
1869 			mvm->ampdu_toggle = toggle_bit;
1870 		}
1871 		rx_status->ampdu_reference = mvm->ampdu_ref;
1872 	}
1873 
1874 	if (unlikely(mvm->monitor_on))
1875 		iwl_mvm_add_rtap_sniffer_config(mvm, skb);
1876 
1877 	rcu_read_lock();
1878 
1879 	if (desc->status & cpu_to_le32(IWL_RX_MPDU_STATUS_SRC_STA_FOUND)) {
1880 		u8 id = le32_get_bits(desc->status, IWL_RX_MPDU_STATUS_STA_ID);
1881 
1882 		if (!WARN_ON_ONCE(id >= mvm->fw->ucode_capa.num_stations)) {
1883 			sta = rcu_dereference(mvm->fw_id_to_mac_id[id]);
1884 			if (IS_ERR(sta))
1885 				sta = NULL;
1886 		}
1887 	} else if (!is_multicast_ether_addr(hdr->addr2)) {
1888 		/*
1889 		 * This is fine since we prevent two stations with the same
1890 		 * address from being added.
1891 		 */
1892 		sta = ieee80211_find_sta_by_ifaddr(mvm->hw, hdr->addr2, NULL);
1893 	}
1894 
1895 	if (iwl_mvm_rx_crypto(mvm, sta, hdr, rx_status, phy_info, desc,
1896 			      le32_to_cpu(pkt->len_n_flags), queue,
1897 			      &crypt_len)) {
1898 		kfree_skb(skb);
1899 		goto out;
1900 	}
1901 
1902 	if (sta) {
1903 		struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1904 		struct ieee80211_vif *tx_blocked_vif =
1905 			rcu_dereference(mvm->csa_tx_blocked_vif);
1906 		u8 baid = (u8)((le32_to_cpu(desc->reorder_data) &
1907 			       IWL_RX_MPDU_REORDER_BAID_MASK) >>
1908 			       IWL_RX_MPDU_REORDER_BAID_SHIFT);
1909 		struct iwl_fw_dbg_trigger_tlv *trig;
1910 		struct ieee80211_vif *vif = mvmsta->vif;
1911 
1912 		if (!mvm->tcm.paused && len >= sizeof(*hdr) &&
1913 		    !is_multicast_ether_addr(hdr->addr1) &&
1914 		    ieee80211_is_data(hdr->frame_control) &&
1915 		    time_after(jiffies, mvm->tcm.ts + MVM_TCM_PERIOD))
1916 			schedule_delayed_work(&mvm->tcm.work, 0);
1917 
1918 		/*
1919 		 * We have tx blocked stations (with CS bit). If we heard
1920 		 * frames from a blocked station on a new channel we can
1921 		 * TX to it again.
1922 		 */
1923 		if (unlikely(tx_blocked_vif) && tx_blocked_vif == vif) {
1924 			struct iwl_mvm_vif *mvmvif =
1925 				iwl_mvm_vif_from_mac80211(tx_blocked_vif);
1926 			struct iwl_rx_sta_csa rx_sta_csa = {
1927 				.all_sta_unblocked = true,
1928 				.vif = tx_blocked_vif,
1929 			};
1930 
1931 			if (mvmvif->csa_target_freq == rx_status->freq)
1932 				iwl_mvm_sta_modify_disable_tx_ap(mvm, sta,
1933 								 false);
1934 			ieee80211_iterate_stations_atomic(mvm->hw,
1935 							  iwl_mvm_rx_get_sta_block_tx,
1936 							  &rx_sta_csa);
1937 
1938 			if (rx_sta_csa.all_sta_unblocked) {
1939 				RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);
1940 				/* Unblock BCAST / MCAST station */
1941 				iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, false);
1942 				cancel_delayed_work_sync(&mvm->cs_tx_unblock_dwork);
1943 			}
1944 		}
1945 
1946 		rs_update_last_rssi(mvm, mvmsta, rx_status);
1947 
1948 		trig = iwl_fw_dbg_trigger_on(&mvm->fwrt,
1949 					     ieee80211_vif_to_wdev(vif),
1950 					     FW_DBG_TRIGGER_RSSI);
1951 
1952 		if (trig && ieee80211_is_beacon(hdr->frame_control)) {
1953 			struct iwl_fw_dbg_trigger_low_rssi *rssi_trig;
1954 			s32 rssi;
1955 
1956 			rssi_trig = (void *)trig->data;
1957 			rssi = le32_to_cpu(rssi_trig->rssi);
1958 
1959 			if (rx_status->signal < rssi)
1960 				iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
1961 #if defined(__linux__)
1962 							NULL);
1963 #elif defined(__FreeBSD__)
1964 							"");
1965 #endif
1966 		}
1967 
1968 		if (ieee80211_is_data(hdr->frame_control))
1969 			iwl_mvm_rx_csum(mvm, sta, skb, pkt);
1970 
1971 		if (iwl_mvm_is_dup(sta, queue, rx_status, hdr, desc)) {
1972 			kfree_skb(skb);
1973 			goto out;
1974 		}
1975 
1976 		/*
1977 		 * Our hardware de-aggregates AMSDUs but copies the mac header
1978 		 * as it to the de-aggregated MPDUs. We need to turn off the
1979 		 * AMSDU bit in the QoS control ourselves.
1980 		 * In addition, HW reverses addr3 and addr4 - reverse it back.
1981 		 */
1982 		if ((desc->mac_flags2 & IWL_RX_MPDU_MFLG2_AMSDU) &&
1983 		    !WARN_ON(!ieee80211_is_data_qos(hdr->frame_control))) {
1984 			u8 *qc = ieee80211_get_qos_ctl(hdr);
1985 
1986 			*qc &= ~IEEE80211_QOS_CTL_A_MSDU_PRESENT;
1987 
1988 			if (mvm->trans->trans_cfg->device_family ==
1989 			    IWL_DEVICE_FAMILY_9000) {
1990 				iwl_mvm_flip_address(hdr->addr3);
1991 
1992 				if (ieee80211_has_a4(hdr->frame_control))
1993 					iwl_mvm_flip_address(hdr->addr4);
1994 			}
1995 		}
1996 		if (baid != IWL_RX_REORDER_DATA_INVALID_BAID) {
1997 			u32 reorder_data = le32_to_cpu(desc->reorder_data);
1998 
1999 			iwl_mvm_agg_rx_received(mvm, reorder_data, baid);
2000 		}
2001 	}
2002 
2003 	is_sgi = format == RATE_MCS_HE_MSK ?
2004 		iwl_he_is_sgi(rate_n_flags) :
2005 		rate_n_flags & RATE_MCS_SGI_MSK;
2006 
2007 	if (!(format == RATE_MCS_CCK_MSK) && is_sgi)
2008 		rx_status->enc_flags |= RX_ENC_FLAG_SHORT_GI;
2009 	if (rate_n_flags & RATE_MCS_LDPC_MSK)
2010 		rx_status->enc_flags |= RX_ENC_FLAG_LDPC;
2011 	if (format == RATE_MCS_HT_MSK) {
2012 		u8 stbc = (rate_n_flags & RATE_MCS_STBC_MSK) >>
2013 			RATE_MCS_STBC_POS;
2014 		rx_status->encoding = RX_ENC_HT;
2015 		rx_status->rate_idx = RATE_HT_MCS_INDEX(rate_n_flags);
2016 		rx_status->enc_flags |= stbc << RX_ENC_FLAG_STBC_SHIFT;
2017 	} else if (format == RATE_MCS_VHT_MSK) {
2018 		u8 stbc = (rate_n_flags & RATE_MCS_STBC_MSK) >>
2019 			RATE_MCS_STBC_POS;
2020 		rx_status->nss = ((rate_n_flags & RATE_MCS_NSS_MSK) >>
2021 			RATE_MCS_NSS_POS) + 1;
2022 		rx_status->rate_idx = rate_n_flags & RATE_MCS_CODE_MSK;
2023 		rx_status->encoding = RX_ENC_VHT;
2024 		rx_status->enc_flags |= stbc << RX_ENC_FLAG_STBC_SHIFT;
2025 		if (rate_n_flags & RATE_MCS_BF_MSK)
2026 			rx_status->enc_flags |= RX_ENC_FLAG_BF;
2027 	} else if (!(format == RATE_MCS_HE_MSK)) {
2028 		int rate = iwl_mvm_legacy_hw_idx_to_mac80211_idx(rate_n_flags,
2029 								 rx_status->band);
2030 
2031 		if (WARN(rate < 0 || rate > 0xFF,
2032 			 "Invalid rate flags 0x%x, band %d,\n",
2033 			 rate_n_flags, rx_status->band)) {
2034 			kfree_skb(skb);
2035 			goto out;
2036 		}
2037 		rx_status->rate_idx = rate;
2038 	}
2039 
2040 	/* management stuff on default queue */
2041 	if (!queue) {
2042 		if (unlikely((ieee80211_is_beacon(hdr->frame_control) ||
2043 			      ieee80211_is_probe_resp(hdr->frame_control)) &&
2044 			     mvm->sched_scan_pass_all ==
2045 			     SCHED_SCAN_PASS_ALL_ENABLED))
2046 			mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_FOUND;
2047 
2048 		if (unlikely(ieee80211_is_beacon(hdr->frame_control) ||
2049 			     ieee80211_is_probe_resp(hdr->frame_control)))
2050 			rx_status->boottime_ns = ktime_get_boottime_ns();
2051 	}
2052 
2053 	if (iwl_mvm_create_skb(mvm, skb, hdr, len, crypt_len, rxb)) {
2054 		kfree_skb(skb);
2055 		goto out;
2056 	}
2057 
2058 	if (!iwl_mvm_reorder(mvm, napi, queue, sta, skb, desc))
2059 		iwl_mvm_pass_packet_to_mac80211(mvm, napi, skb, queue,
2060 						sta);
2061 out:
2062 	rcu_read_unlock();
2063 }
2064 
2065 void iwl_mvm_rx_monitor_no_data(struct iwl_mvm *mvm, struct napi_struct *napi,
2066 				struct iwl_rx_cmd_buffer *rxb, int queue)
2067 {
2068 	struct ieee80211_rx_status *rx_status;
2069 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
2070 	struct iwl_rx_no_data *desc = (void *)pkt->data;
2071 	u32 rate_n_flags = le32_to_cpu(desc->rate);
2072 	u32 gp2_on_air_rise = le32_to_cpu(desc->on_air_rise_time);
2073 	u32 rssi = le32_to_cpu(desc->rssi);
2074 	u32 info_type = le32_to_cpu(desc->info) & RX_NO_DATA_INFO_TYPE_MSK;
2075 	u16 phy_info = IWL_RX_MPDU_PHY_TSF_OVERLOAD;
2076 	struct ieee80211_sta *sta = NULL;
2077 	struct sk_buff *skb;
2078 	u8 channel, energy_a, energy_b;
2079 	u32 format;
2080 	struct iwl_mvm_rx_phy_data phy_data = {
2081 		.info_type = le32_get_bits(desc->phy_info[1],
2082 					   IWL_RX_PHY_DATA1_INFO_TYPE_MASK),
2083 		.d0 = desc->phy_info[0],
2084 		.d1 = desc->phy_info[1],
2085 	};
2086 	bool is_sgi;
2087 
2088 	if (iwl_fw_lookup_notif_ver(mvm->fw, DATA_PATH_GROUP,
2089 				    RX_NO_DATA_NOTIF, 0) < 2) {
2090 		IWL_DEBUG_DROP(mvm, "Got an old rate format. Old rate: 0x%x\n",
2091 			       rate_n_flags);
2092 		rate_n_flags = iwl_new_rate_from_v1(rate_n_flags);
2093 		IWL_DEBUG_DROP(mvm, " Rate after conversion to the new format: 0x%x\n",
2094 			       rate_n_flags);
2095 	}
2096 	format = rate_n_flags & RATE_MCS_MOD_TYPE_MSK;
2097 
2098 	if (unlikely(iwl_rx_packet_payload_len(pkt) < sizeof(*desc)))
2099 		return;
2100 
2101 	if (unlikely(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)))
2102 		return;
2103 
2104 	energy_a = (rssi & RX_NO_DATA_CHAIN_A_MSK) >> RX_NO_DATA_CHAIN_A_POS;
2105 	energy_b = (rssi & RX_NO_DATA_CHAIN_B_MSK) >> RX_NO_DATA_CHAIN_B_POS;
2106 	channel = (rssi & RX_NO_DATA_CHANNEL_MSK) >> RX_NO_DATA_CHANNEL_POS;
2107 
2108 	/* Dont use dev_alloc_skb(), we'll have enough headroom once
2109 	 * ieee80211_hdr pulled.
2110 	 */
2111 	skb = alloc_skb(128, GFP_ATOMIC);
2112 	if (!skb) {
2113 		IWL_ERR(mvm, "alloc_skb failed\n");
2114 		return;
2115 	}
2116 
2117 	rx_status = IEEE80211_SKB_RXCB(skb);
2118 
2119 	/* 0-length PSDU */
2120 	rx_status->flag |= RX_FLAG_NO_PSDU;
2121 
2122 	switch (info_type) {
2123 	case RX_NO_DATA_INFO_TYPE_NDP:
2124 		rx_status->zero_length_psdu_type =
2125 			IEEE80211_RADIOTAP_ZERO_LEN_PSDU_SOUNDING;
2126 		break;
2127 	case RX_NO_DATA_INFO_TYPE_MU_UNMATCHED:
2128 	case RX_NO_DATA_INFO_TYPE_HE_TB_UNMATCHED:
2129 		rx_status->zero_length_psdu_type =
2130 			IEEE80211_RADIOTAP_ZERO_LEN_PSDU_NOT_CAPTURED;
2131 		break;
2132 	default:
2133 		rx_status->zero_length_psdu_type =
2134 			IEEE80211_RADIOTAP_ZERO_LEN_PSDU_VENDOR;
2135 		break;
2136 	}
2137 
2138 	/* This may be overridden by iwl_mvm_rx_he() to HE_RU */
2139 	switch (rate_n_flags & RATE_MCS_CHAN_WIDTH_MSK) {
2140 	case RATE_MCS_CHAN_WIDTH_20:
2141 		break;
2142 	case RATE_MCS_CHAN_WIDTH_40:
2143 		rx_status->bw = RATE_INFO_BW_40;
2144 		break;
2145 	case RATE_MCS_CHAN_WIDTH_80:
2146 		rx_status->bw = RATE_INFO_BW_80;
2147 		break;
2148 	case RATE_MCS_CHAN_WIDTH_160:
2149 		rx_status->bw = RATE_INFO_BW_160;
2150 		break;
2151 	}
2152 
2153 	if (format == RATE_MCS_HE_MSK)
2154 		iwl_mvm_rx_he(mvm, skb, &phy_data, rate_n_flags,
2155 			      phy_info, queue);
2156 
2157 	iwl_mvm_decode_lsig(skb, &phy_data);
2158 
2159 	rx_status->device_timestamp = gp2_on_air_rise;
2160 	rx_status->band = channel > 14 ? NL80211_BAND_5GHZ :
2161 		NL80211_BAND_2GHZ;
2162 	rx_status->freq = ieee80211_channel_to_frequency(channel,
2163 							 rx_status->band);
2164 	iwl_mvm_get_signal_strength(mvm, rx_status, rate_n_flags, energy_a,
2165 				    energy_b);
2166 
2167 	rcu_read_lock();
2168 
2169 	is_sgi = format == RATE_MCS_HE_MSK ?
2170 		iwl_he_is_sgi(rate_n_flags) :
2171 		rate_n_flags & RATE_MCS_SGI_MSK;
2172 
2173 	if (!(format == RATE_MCS_CCK_MSK) && is_sgi)
2174 		rx_status->enc_flags |= RX_ENC_FLAG_SHORT_GI;
2175 	if (rate_n_flags & RATE_MCS_LDPC_MSK)
2176 		rx_status->enc_flags |= RX_ENC_FLAG_LDPC;
2177 	if (format == RATE_MCS_HT_MSK) {
2178 		u8 stbc = (rate_n_flags & RATE_MCS_STBC_MSK) >>
2179 				RATE_MCS_STBC_POS;
2180 		rx_status->encoding = RX_ENC_HT;
2181 		rx_status->rate_idx = RATE_HT_MCS_INDEX(rate_n_flags);
2182 		rx_status->enc_flags |= stbc << RX_ENC_FLAG_STBC_SHIFT;
2183 	} else if (format == RATE_MCS_VHT_MSK) {
2184 		u8 stbc = (rate_n_flags & RATE_MCS_STBC_MSK) >>
2185 				RATE_MCS_STBC_POS;
2186 		rx_status->rate_idx = rate_n_flags & RATE_MCS_CODE_MSK;
2187 		rx_status->encoding = RX_ENC_VHT;
2188 		rx_status->enc_flags |= stbc << RX_ENC_FLAG_STBC_SHIFT;
2189 		if (rate_n_flags & RATE_MCS_BF_MSK)
2190 			rx_status->enc_flags |= RX_ENC_FLAG_BF;
2191 		/*
2192 		 * take the nss from the rx_vec since the rate_n_flags has
2193 		 * only 2 bits for the nss which gives a max of 4 ss but
2194 		 * there may be up to 8 spatial streams
2195 		 */
2196 		rx_status->nss =
2197 			le32_get_bits(desc->rx_vec[0],
2198 				      RX_NO_DATA_RX_VEC0_VHT_NSTS_MSK) + 1;
2199 	} else if (format == RATE_MCS_HE_MSK) {
2200 		rx_status->nss =
2201 			le32_get_bits(desc->rx_vec[0],
2202 				      RX_NO_DATA_RX_VEC0_HE_NSTS_MSK) + 1;
2203 	} else {
2204 		int rate = iwl_mvm_legacy_hw_idx_to_mac80211_idx(rate_n_flags,
2205 							       rx_status->band);
2206 
2207 		if (WARN(rate < 0 || rate > 0xFF,
2208 			 "Invalid rate flags 0x%x, band %d,\n",
2209 			 rate_n_flags, rx_status->band)) {
2210 			kfree_skb(skb);
2211 			goto out;
2212 		}
2213 		rx_status->rate_idx = rate;
2214 	}
2215 
2216 	ieee80211_rx_napi(mvm->hw, sta, skb, napi);
2217 out:
2218 	rcu_read_unlock();
2219 }
2220 
2221 void iwl_mvm_rx_frame_release(struct iwl_mvm *mvm, struct napi_struct *napi,
2222 			      struct iwl_rx_cmd_buffer *rxb, int queue)
2223 {
2224 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
2225 	struct iwl_frame_release *release = (void *)pkt->data;
2226 
2227 	if (unlikely(iwl_rx_packet_payload_len(pkt) < sizeof(*release)))
2228 		return;
2229 
2230 	iwl_mvm_release_frames_from_notif(mvm, napi, release->baid,
2231 					  le16_to_cpu(release->nssn),
2232 					  queue, 0);
2233 }
2234 
2235 void iwl_mvm_rx_bar_frame_release(struct iwl_mvm *mvm, struct napi_struct *napi,
2236 				  struct iwl_rx_cmd_buffer *rxb, int queue)
2237 {
2238 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
2239 	struct iwl_bar_frame_release *release = (void *)pkt->data;
2240 	unsigned int baid = le32_get_bits(release->ba_info,
2241 					  IWL_BAR_FRAME_RELEASE_BAID_MASK);
2242 	unsigned int nssn = le32_get_bits(release->ba_info,
2243 					  IWL_BAR_FRAME_RELEASE_NSSN_MASK);
2244 	unsigned int sta_id = le32_get_bits(release->sta_tid,
2245 					    IWL_BAR_FRAME_RELEASE_STA_MASK);
2246 	unsigned int tid = le32_get_bits(release->sta_tid,
2247 					 IWL_BAR_FRAME_RELEASE_TID_MASK);
2248 	struct iwl_mvm_baid_data *baid_data;
2249 
2250 	if (unlikely(iwl_rx_packet_payload_len(pkt) < sizeof(*release)))
2251 		return;
2252 
2253 	if (WARN_ON_ONCE(baid == IWL_RX_REORDER_DATA_INVALID_BAID ||
2254 			 baid >= ARRAY_SIZE(mvm->baid_map)))
2255 		return;
2256 
2257 	rcu_read_lock();
2258 	baid_data = rcu_dereference(mvm->baid_map[baid]);
2259 	if (!baid_data) {
2260 		IWL_DEBUG_RX(mvm,
2261 			     "Got valid BAID %d but not allocated, invalid BAR release!\n",
2262 			      baid);
2263 		goto out;
2264 	}
2265 
2266 	if (WARN(tid != baid_data->tid || sta_id != baid_data->sta_id,
2267 		 "baid 0x%x is mapped to sta:%d tid:%d, but BAR release received for sta:%d tid:%d\n",
2268 		 baid, baid_data->sta_id, baid_data->tid, sta_id,
2269 		 tid))
2270 		goto out;
2271 
2272 	iwl_mvm_release_frames_from_notif(mvm, napi, baid, nssn, queue, 0);
2273 out:
2274 	rcu_read_unlock();
2275 }
2276