xref: /freebsd/sys/contrib/dev/iwlwifi/mvm/rxmq.c (revision 8ddb146abcdf061be9f2c0db7e391697dafad85c)
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 	u8 tid = ieee80211_get_tid(hdr);
967 	u8 sub_frame_idx = desc->amsdu_info &
968 			   IWL_RX_MPDU_AMSDU_SUBFRAME_IDX_MASK;
969 	struct iwl_mvm_reorder_buf_entry *entries;
970 	int index;
971 	u16 nssn, sn;
972 	u8 baid;
973 
974 	baid = (reorder & IWL_RX_MPDU_REORDER_BAID_MASK) >>
975 		IWL_RX_MPDU_REORDER_BAID_SHIFT;
976 
977 	/*
978 	 * This also covers the case of receiving a Block Ack Request
979 	 * outside a BA session; we'll pass it to mac80211 and that
980 	 * then sends a delBA action frame.
981 	 * This also covers pure monitor mode, in which case we won't
982 	 * have any BA sessions.
983 	 */
984 	if (baid == IWL_RX_REORDER_DATA_INVALID_BAID)
985 		return false;
986 
987 	/* no sta yet */
988 	if (WARN_ONCE(IS_ERR_OR_NULL(sta),
989 		      "Got valid BAID without a valid station assigned\n"))
990 		return false;
991 
992 	mvm_sta = iwl_mvm_sta_from_mac80211(sta);
993 
994 	/* not a data packet or a bar */
995 	if (!ieee80211_is_back_req(hdr->frame_control) &&
996 	    (!ieee80211_is_data_qos(hdr->frame_control) ||
997 	     is_multicast_ether_addr(hdr->addr1)))
998 		return false;
999 
1000 	if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
1001 		return false;
1002 
1003 	baid_data = rcu_dereference(mvm->baid_map[baid]);
1004 	if (!baid_data) {
1005 		IWL_DEBUG_RX(mvm,
1006 			     "Got valid BAID but no baid allocated, bypass the re-ordering buffer. Baid %d reorder 0x%x\n",
1007 			      baid, reorder);
1008 		return false;
1009 	}
1010 
1011 	if (WARN(tid != baid_data->tid || mvm_sta->sta_id != baid_data->sta_id,
1012 		 "baid 0x%x is mapped to sta:%d tid:%d, but was received for sta:%d tid:%d\n",
1013 		 baid, baid_data->sta_id, baid_data->tid, mvm_sta->sta_id,
1014 		 tid))
1015 		return false;
1016 
1017 	nssn = reorder & IWL_RX_MPDU_REORDER_NSSN_MASK;
1018 	sn = (reorder & IWL_RX_MPDU_REORDER_SN_MASK) >>
1019 		IWL_RX_MPDU_REORDER_SN_SHIFT;
1020 
1021 	buffer = &baid_data->reorder_buf[queue];
1022 	entries = &baid_data->entries[queue * baid_data->entries_per_queue];
1023 
1024 	spin_lock_bh(&buffer->lock);
1025 
1026 	if (!buffer->valid) {
1027 		if (reorder & IWL_RX_MPDU_REORDER_BA_OLD_SN) {
1028 			spin_unlock_bh(&buffer->lock);
1029 			return false;
1030 		}
1031 		buffer->valid = true;
1032 	}
1033 
1034 	if (ieee80211_is_back_req(hdr->frame_control)) {
1035 		iwl_mvm_release_frames(mvm, sta, napi, baid_data,
1036 				       buffer, nssn, 0);
1037 		goto drop;
1038 	}
1039 
1040 	/*
1041 	 * If there was a significant jump in the nssn - adjust.
1042 	 * If the SN is smaller than the NSSN it might need to first go into
1043 	 * the reorder buffer, in which case we just release up to it and the
1044 	 * rest of the function will take care of storing it and releasing up to
1045 	 * the nssn.
1046 	 * This should not happen. This queue has been lagging and it should
1047 	 * have been updated by a IWL_MVM_RXQ_NSSN_SYNC notification. Be nice
1048 	 * and update the other queues.
1049 	 */
1050 	if (!iwl_mvm_is_sn_less(nssn, buffer->head_sn + buffer->buf_size,
1051 				buffer->buf_size) ||
1052 	    !ieee80211_sn_less(sn, buffer->head_sn + buffer->buf_size)) {
1053 		u16 min_sn = ieee80211_sn_less(sn, nssn) ? sn : nssn;
1054 
1055 		iwl_mvm_release_frames(mvm, sta, napi, baid_data, buffer,
1056 				       min_sn, IWL_MVM_RELEASE_SEND_RSS_SYNC);
1057 	}
1058 
1059 	iwl_mvm_oldsn_workaround(mvm, sta, tid, buffer, reorder,
1060 				 rx_status->device_timestamp, queue);
1061 
1062 	/* drop any oudated packets */
1063 	if (ieee80211_sn_less(sn, buffer->head_sn))
1064 		goto drop;
1065 
1066 	/* release immediately if allowed by nssn and no stored frames */
1067 	if (!buffer->num_stored && ieee80211_sn_less(sn, nssn)) {
1068 		if (iwl_mvm_is_sn_less(buffer->head_sn, nssn,
1069 				       buffer->buf_size) &&
1070 		   (!amsdu || last_subframe)) {
1071 			/*
1072 			 * If we crossed the 2048 or 0 SN, notify all the
1073 			 * queues. This is done in order to avoid having a
1074 			 * head_sn that lags behind for too long. When that
1075 			 * happens, we can get to a situation where the head_sn
1076 			 * is within the interval [nssn - buf_size : nssn]
1077 			 * which will make us think that the nssn is a packet
1078 			 * that we already freed because of the reordering
1079 			 * buffer and we will ignore it. So maintain the
1080 			 * head_sn somewhat updated across all the queues:
1081 			 * when it crosses 0 and 2048.
1082 			 */
1083 			if (sn == 2048 || sn == 0)
1084 				iwl_mvm_sync_nssn(mvm, baid, sn);
1085 			buffer->head_sn = nssn;
1086 		}
1087 		/* No need to update AMSDU last SN - we are moving the head */
1088 		spin_unlock_bh(&buffer->lock);
1089 		return false;
1090 	}
1091 
1092 	/*
1093 	 * release immediately if there are no stored frames, and the sn is
1094 	 * equal to the head.
1095 	 * This can happen due to reorder timer, where NSSN is behind head_sn.
1096 	 * When we released everything, and we got the next frame in the
1097 	 * sequence, according to the NSSN we can't release immediately,
1098 	 * while technically there is no hole and we can move forward.
1099 	 */
1100 	if (!buffer->num_stored && sn == buffer->head_sn) {
1101 		if (!amsdu || last_subframe) {
1102 			if (sn == 2048 || sn == 0)
1103 				iwl_mvm_sync_nssn(mvm, baid, sn);
1104 			buffer->head_sn = ieee80211_sn_inc(buffer->head_sn);
1105 		}
1106 		/* No need to update AMSDU last SN - we are moving the head */
1107 		spin_unlock_bh(&buffer->lock);
1108 		return false;
1109 	}
1110 
1111 	index = sn % buffer->buf_size;
1112 
1113 	/*
1114 	 * Check if we already stored this frame
1115 	 * As AMSDU is either received or not as whole, logic is simple:
1116 	 * If we have frames in that position in the buffer and the last frame
1117 	 * originated from AMSDU had a different SN then it is a retransmission.
1118 	 * If it is the same SN then if the subframe index is incrementing it
1119 	 * is the same AMSDU - otherwise it is a retransmission.
1120 	 */
1121 	tail = skb_peek_tail(&entries[index].e.frames);
1122 	if (tail && !amsdu)
1123 		goto drop;
1124 	else if (tail && (sn != buffer->last_amsdu ||
1125 			  buffer->last_sub_index >= sub_frame_idx))
1126 		goto drop;
1127 
1128 	/* put in reorder buffer */
1129 	__skb_queue_tail(&entries[index].e.frames, skb);
1130 	buffer->num_stored++;
1131 	entries[index].e.reorder_time = jiffies;
1132 
1133 	if (amsdu) {
1134 		buffer->last_amsdu = sn;
1135 		buffer->last_sub_index = sub_frame_idx;
1136 	}
1137 
1138 	/*
1139 	 * We cannot trust NSSN for AMSDU sub-frames that are not the last.
1140 	 * The reason is that NSSN advances on the first sub-frame, and may
1141 	 * cause the reorder buffer to advance before all the sub-frames arrive.
1142 	 * Example: reorder buffer contains SN 0 & 2, and we receive AMSDU with
1143 	 * SN 1. NSSN for first sub frame will be 3 with the result of driver
1144 	 * releasing SN 0,1, 2. When sub-frame 1 arrives - reorder buffer is
1145 	 * already ahead and it will be dropped.
1146 	 * If the last sub-frame is not on this queue - we will get frame
1147 	 * release notification with up to date NSSN.
1148 	 */
1149 	if (!amsdu || last_subframe)
1150 		iwl_mvm_release_frames(mvm, sta, napi, baid_data,
1151 				       buffer, nssn,
1152 				       IWL_MVM_RELEASE_SEND_RSS_SYNC);
1153 
1154 	spin_unlock_bh(&buffer->lock);
1155 	return true;
1156 
1157 drop:
1158 	kfree_skb(skb);
1159 	spin_unlock_bh(&buffer->lock);
1160 	return true;
1161 }
1162 
1163 static void iwl_mvm_agg_rx_received(struct iwl_mvm *mvm,
1164 				    u32 reorder_data, u8 baid)
1165 {
1166 	unsigned long now = jiffies;
1167 	unsigned long timeout;
1168 	struct iwl_mvm_baid_data *data;
1169 
1170 	rcu_read_lock();
1171 
1172 	data = rcu_dereference(mvm->baid_map[baid]);
1173 	if (!data) {
1174 		IWL_DEBUG_RX(mvm,
1175 			     "Got valid BAID but no baid allocated, bypass the re-ordering buffer. Baid %d reorder 0x%x\n",
1176 			      baid, reorder_data);
1177 		goto out;
1178 	}
1179 
1180 	if (!data->timeout)
1181 		goto out;
1182 
1183 	timeout = data->timeout;
1184 	/*
1185 	 * Do not update last rx all the time to avoid cache bouncing
1186 	 * between the rx queues.
1187 	 * Update it every timeout. Worst case is the session will
1188 	 * expire after ~ 2 * timeout, which doesn't matter that much.
1189 	 */
1190 	if (time_before(data->last_rx + TU_TO_JIFFIES(timeout), now))
1191 		/* Update is atomic */
1192 		data->last_rx = now;
1193 
1194 out:
1195 	rcu_read_unlock();
1196 }
1197 
1198 static void iwl_mvm_flip_address(u8 *addr)
1199 {
1200 	int i;
1201 	u8 mac_addr[ETH_ALEN];
1202 
1203 	for (i = 0; i < ETH_ALEN; i++)
1204 		mac_addr[i] = addr[ETH_ALEN - i - 1];
1205 	ether_addr_copy(addr, mac_addr);
1206 }
1207 
1208 struct iwl_mvm_rx_phy_data {
1209 	enum iwl_rx_phy_info_type info_type;
1210 	__le32 d0, d1, d2, d3;
1211 	__le16 d4;
1212 };
1213 
1214 static void iwl_mvm_decode_he_mu_ext(struct iwl_mvm *mvm,
1215 				     struct iwl_mvm_rx_phy_data *phy_data,
1216 				     u32 rate_n_flags,
1217 				     struct ieee80211_radiotap_he_mu *he_mu)
1218 {
1219 	u32 phy_data2 = le32_to_cpu(phy_data->d2);
1220 	u32 phy_data3 = le32_to_cpu(phy_data->d3);
1221 	u16 phy_data4 = le16_to_cpu(phy_data->d4);
1222 
1223 	if (FIELD_GET(IWL_RX_PHY_DATA4_HE_MU_EXT_CH1_CRC_OK, phy_data4)) {
1224 		he_mu->flags1 |=
1225 			cpu_to_le16(IEEE80211_RADIOTAP_HE_MU_FLAGS1_CH1_RU_KNOWN |
1226 				    IEEE80211_RADIOTAP_HE_MU_FLAGS1_CH1_CTR_26T_RU_KNOWN);
1227 
1228 		he_mu->flags1 |=
1229 			le16_encode_bits(FIELD_GET(IWL_RX_PHY_DATA4_HE_MU_EXT_CH1_CTR_RU,
1230 						   phy_data4),
1231 					 IEEE80211_RADIOTAP_HE_MU_FLAGS1_CH1_CTR_26T_RU);
1232 
1233 		he_mu->ru_ch1[0] = FIELD_GET(IWL_RX_PHY_DATA2_HE_MU_EXT_CH1_RU0,
1234 					     phy_data2);
1235 		he_mu->ru_ch1[1] = FIELD_GET(IWL_RX_PHY_DATA3_HE_MU_EXT_CH1_RU1,
1236 					     phy_data3);
1237 		he_mu->ru_ch1[2] = FIELD_GET(IWL_RX_PHY_DATA2_HE_MU_EXT_CH1_RU2,
1238 					     phy_data2);
1239 		he_mu->ru_ch1[3] = FIELD_GET(IWL_RX_PHY_DATA3_HE_MU_EXT_CH1_RU3,
1240 					     phy_data3);
1241 	}
1242 
1243 	if (FIELD_GET(IWL_RX_PHY_DATA4_HE_MU_EXT_CH2_CRC_OK, phy_data4) &&
1244 	    (rate_n_flags & RATE_MCS_CHAN_WIDTH_MSK_V1) != RATE_MCS_CHAN_WIDTH_20) {
1245 		he_mu->flags1 |=
1246 			cpu_to_le16(IEEE80211_RADIOTAP_HE_MU_FLAGS1_CH2_RU_KNOWN |
1247 				    IEEE80211_RADIOTAP_HE_MU_FLAGS1_CH2_CTR_26T_RU_KNOWN);
1248 
1249 		he_mu->flags2 |=
1250 			le16_encode_bits(FIELD_GET(IWL_RX_PHY_DATA4_HE_MU_EXT_CH2_CTR_RU,
1251 						   phy_data4),
1252 					 IEEE80211_RADIOTAP_HE_MU_FLAGS2_CH2_CTR_26T_RU);
1253 
1254 		he_mu->ru_ch2[0] = FIELD_GET(IWL_RX_PHY_DATA2_HE_MU_EXT_CH2_RU0,
1255 					     phy_data2);
1256 		he_mu->ru_ch2[1] = FIELD_GET(IWL_RX_PHY_DATA3_HE_MU_EXT_CH2_RU1,
1257 					     phy_data3);
1258 		he_mu->ru_ch2[2] = FIELD_GET(IWL_RX_PHY_DATA2_HE_MU_EXT_CH2_RU2,
1259 					     phy_data2);
1260 		he_mu->ru_ch2[3] = FIELD_GET(IWL_RX_PHY_DATA3_HE_MU_EXT_CH2_RU3,
1261 					     phy_data3);
1262 	}
1263 }
1264 
1265 static void
1266 iwl_mvm_decode_he_phy_ru_alloc(struct iwl_mvm_rx_phy_data *phy_data,
1267 			       u32 rate_n_flags,
1268 			       struct ieee80211_radiotap_he *he,
1269 			       struct ieee80211_radiotap_he_mu *he_mu,
1270 			       struct ieee80211_rx_status *rx_status)
1271 {
1272 	/*
1273 	 * Unfortunately, we have to leave the mac80211 data
1274 	 * incorrect for the case that we receive an HE-MU
1275 	 * transmission and *don't* have the HE phy data (due
1276 	 * to the bits being used for TSF). This shouldn't
1277 	 * happen though as management frames where we need
1278 	 * the TSF/timers are not be transmitted in HE-MU.
1279 	 */
1280 	u8 ru = le32_get_bits(phy_data->d1, IWL_RX_PHY_DATA1_HE_RU_ALLOC_MASK);
1281 	u32 he_type = rate_n_flags & RATE_MCS_HE_TYPE_MSK_V1;
1282 	u8 offs = 0;
1283 
1284 	rx_status->bw = RATE_INFO_BW_HE_RU;
1285 
1286 	he->data1 |= cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA1_BW_RU_ALLOC_KNOWN);
1287 
1288 	switch (ru) {
1289 	case 0 ... 36:
1290 		rx_status->he_ru = NL80211_RATE_INFO_HE_RU_ALLOC_26;
1291 		offs = ru;
1292 		break;
1293 	case 37 ... 52:
1294 		rx_status->he_ru = NL80211_RATE_INFO_HE_RU_ALLOC_52;
1295 		offs = ru - 37;
1296 		break;
1297 	case 53 ... 60:
1298 		rx_status->he_ru = NL80211_RATE_INFO_HE_RU_ALLOC_106;
1299 		offs = ru - 53;
1300 		break;
1301 	case 61 ... 64:
1302 		rx_status->he_ru = NL80211_RATE_INFO_HE_RU_ALLOC_242;
1303 		offs = ru - 61;
1304 		break;
1305 	case 65 ... 66:
1306 		rx_status->he_ru = NL80211_RATE_INFO_HE_RU_ALLOC_484;
1307 		offs = ru - 65;
1308 		break;
1309 	case 67:
1310 		rx_status->he_ru = NL80211_RATE_INFO_HE_RU_ALLOC_996;
1311 		break;
1312 	case 68:
1313 		rx_status->he_ru = NL80211_RATE_INFO_HE_RU_ALLOC_2x996;
1314 		break;
1315 	}
1316 	he->data2 |= le16_encode_bits(offs,
1317 				      IEEE80211_RADIOTAP_HE_DATA2_RU_OFFSET);
1318 	he->data2 |= cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA2_PRISEC_80_KNOWN |
1319 				 IEEE80211_RADIOTAP_HE_DATA2_RU_OFFSET_KNOWN);
1320 	if (phy_data->d1 & cpu_to_le32(IWL_RX_PHY_DATA1_HE_RU_ALLOC_SEC80))
1321 		he->data2 |=
1322 			cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA2_PRISEC_80_SEC);
1323 
1324 #define CHECK_BW(bw) \
1325 	BUILD_BUG_ON(IEEE80211_RADIOTAP_HE_MU_FLAGS2_BW_FROM_SIG_A_BW_ ## bw ## MHZ != \
1326 		     RATE_MCS_CHAN_WIDTH_##bw >> RATE_MCS_CHAN_WIDTH_POS); \
1327 	BUILD_BUG_ON(IEEE80211_RADIOTAP_HE_DATA6_TB_PPDU_BW_ ## bw ## MHZ != \
1328 		     RATE_MCS_CHAN_WIDTH_##bw >> RATE_MCS_CHAN_WIDTH_POS)
1329 	CHECK_BW(20);
1330 	CHECK_BW(40);
1331 	CHECK_BW(80);
1332 	CHECK_BW(160);
1333 
1334 	if (he_mu)
1335 		he_mu->flags2 |=
1336 			le16_encode_bits(FIELD_GET(RATE_MCS_CHAN_WIDTH_MSK_V1,
1337 						   rate_n_flags),
1338 					 IEEE80211_RADIOTAP_HE_MU_FLAGS2_BW_FROM_SIG_A_BW);
1339 	else if (he_type == RATE_MCS_HE_TYPE_TRIG_V1)
1340 		he->data6 |=
1341 			cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA6_TB_PPDU_BW_KNOWN) |
1342 			le16_encode_bits(FIELD_GET(RATE_MCS_CHAN_WIDTH_MSK_V1,
1343 						   rate_n_flags),
1344 					 IEEE80211_RADIOTAP_HE_DATA6_TB_PPDU_BW);
1345 }
1346 
1347 static void iwl_mvm_decode_he_phy_data(struct iwl_mvm *mvm,
1348 				       struct iwl_mvm_rx_phy_data *phy_data,
1349 				       struct ieee80211_radiotap_he *he,
1350 				       struct ieee80211_radiotap_he_mu *he_mu,
1351 				       struct ieee80211_rx_status *rx_status,
1352 				       u32 rate_n_flags, int queue)
1353 {
1354 	switch (phy_data->info_type) {
1355 	case IWL_RX_PHY_INFO_TYPE_NONE:
1356 	case IWL_RX_PHY_INFO_TYPE_CCK:
1357 	case IWL_RX_PHY_INFO_TYPE_OFDM_LGCY:
1358 	case IWL_RX_PHY_INFO_TYPE_HT:
1359 	case IWL_RX_PHY_INFO_TYPE_VHT_SU:
1360 	case IWL_RX_PHY_INFO_TYPE_VHT_MU:
1361 		return;
1362 	case IWL_RX_PHY_INFO_TYPE_HE_TB_EXT:
1363 		he->data1 |= cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA1_SPTL_REUSE_KNOWN |
1364 					 IEEE80211_RADIOTAP_HE_DATA1_SPTL_REUSE2_KNOWN |
1365 					 IEEE80211_RADIOTAP_HE_DATA1_SPTL_REUSE3_KNOWN |
1366 					 IEEE80211_RADIOTAP_HE_DATA1_SPTL_REUSE4_KNOWN);
1367 		he->data4 |= le16_encode_bits(le32_get_bits(phy_data->d2,
1368 							    IWL_RX_PHY_DATA2_HE_TB_EXT_SPTL_REUSE1),
1369 					      IEEE80211_RADIOTAP_HE_DATA4_TB_SPTL_REUSE1);
1370 		he->data4 |= le16_encode_bits(le32_get_bits(phy_data->d2,
1371 							    IWL_RX_PHY_DATA2_HE_TB_EXT_SPTL_REUSE2),
1372 					      IEEE80211_RADIOTAP_HE_DATA4_TB_SPTL_REUSE2);
1373 		he->data4 |= le16_encode_bits(le32_get_bits(phy_data->d2,
1374 							    IWL_RX_PHY_DATA2_HE_TB_EXT_SPTL_REUSE3),
1375 					      IEEE80211_RADIOTAP_HE_DATA4_TB_SPTL_REUSE3);
1376 		he->data4 |= le16_encode_bits(le32_get_bits(phy_data->d2,
1377 							    IWL_RX_PHY_DATA2_HE_TB_EXT_SPTL_REUSE4),
1378 					      IEEE80211_RADIOTAP_HE_DATA4_TB_SPTL_REUSE4);
1379 		fallthrough;
1380 	case IWL_RX_PHY_INFO_TYPE_HE_SU:
1381 	case IWL_RX_PHY_INFO_TYPE_HE_MU:
1382 	case IWL_RX_PHY_INFO_TYPE_HE_MU_EXT:
1383 	case IWL_RX_PHY_INFO_TYPE_HE_TB:
1384 		/* HE common */
1385 		he->data1 |= cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA1_LDPC_XSYMSEG_KNOWN |
1386 					 IEEE80211_RADIOTAP_HE_DATA1_DOPPLER_KNOWN |
1387 					 IEEE80211_RADIOTAP_HE_DATA1_BSS_COLOR_KNOWN);
1388 		he->data2 |= cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA2_PRE_FEC_PAD_KNOWN |
1389 					 IEEE80211_RADIOTAP_HE_DATA2_PE_DISAMBIG_KNOWN |
1390 					 IEEE80211_RADIOTAP_HE_DATA2_TXOP_KNOWN |
1391 					 IEEE80211_RADIOTAP_HE_DATA2_NUM_LTF_SYMS_KNOWN);
1392 		he->data3 |= le16_encode_bits(le32_get_bits(phy_data->d0,
1393 							    IWL_RX_PHY_DATA0_HE_BSS_COLOR_MASK),
1394 					      IEEE80211_RADIOTAP_HE_DATA3_BSS_COLOR);
1395 		if (phy_data->info_type != IWL_RX_PHY_INFO_TYPE_HE_TB &&
1396 		    phy_data->info_type != IWL_RX_PHY_INFO_TYPE_HE_TB_EXT) {
1397 			he->data1 |= cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA1_UL_DL_KNOWN);
1398 			he->data3 |= le16_encode_bits(le32_get_bits(phy_data->d0,
1399 							    IWL_RX_PHY_DATA0_HE_UPLINK),
1400 						      IEEE80211_RADIOTAP_HE_DATA3_UL_DL);
1401 		}
1402 		he->data3 |= le16_encode_bits(le32_get_bits(phy_data->d0,
1403 							    IWL_RX_PHY_DATA0_HE_LDPC_EXT_SYM),
1404 					      IEEE80211_RADIOTAP_HE_DATA3_LDPC_XSYMSEG);
1405 		he->data5 |= le16_encode_bits(le32_get_bits(phy_data->d0,
1406 							    IWL_RX_PHY_DATA0_HE_PRE_FEC_PAD_MASK),
1407 					      IEEE80211_RADIOTAP_HE_DATA5_PRE_FEC_PAD);
1408 		he->data5 |= le16_encode_bits(le32_get_bits(phy_data->d0,
1409 							    IWL_RX_PHY_DATA0_HE_PE_DISAMBIG),
1410 					      IEEE80211_RADIOTAP_HE_DATA5_PE_DISAMBIG);
1411 		he->data5 |= le16_encode_bits(le32_get_bits(phy_data->d1,
1412 							    IWL_RX_PHY_DATA1_HE_LTF_NUM_MASK),
1413 					      IEEE80211_RADIOTAP_HE_DATA5_NUM_LTF_SYMS);
1414 		he->data6 |= le16_encode_bits(le32_get_bits(phy_data->d0,
1415 							    IWL_RX_PHY_DATA0_HE_TXOP_DUR_MASK),
1416 					      IEEE80211_RADIOTAP_HE_DATA6_TXOP);
1417 		he->data6 |= le16_encode_bits(le32_get_bits(phy_data->d0,
1418 							    IWL_RX_PHY_DATA0_HE_DOPPLER),
1419 					      IEEE80211_RADIOTAP_HE_DATA6_DOPPLER);
1420 		break;
1421 	}
1422 
1423 	switch (phy_data->info_type) {
1424 	case IWL_RX_PHY_INFO_TYPE_HE_MU_EXT:
1425 	case IWL_RX_PHY_INFO_TYPE_HE_MU:
1426 	case IWL_RX_PHY_INFO_TYPE_HE_SU:
1427 		he->data1 |= cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA1_SPTL_REUSE_KNOWN);
1428 		he->data4 |= le16_encode_bits(le32_get_bits(phy_data->d0,
1429 							    IWL_RX_PHY_DATA0_HE_SPATIAL_REUSE_MASK),
1430 					      IEEE80211_RADIOTAP_HE_DATA4_SU_MU_SPTL_REUSE);
1431 		break;
1432 	default:
1433 		/* nothing here */
1434 		break;
1435 	}
1436 
1437 	switch (phy_data->info_type) {
1438 	case IWL_RX_PHY_INFO_TYPE_HE_MU_EXT:
1439 		he_mu->flags1 |=
1440 			le16_encode_bits(le16_get_bits(phy_data->d4,
1441 						       IWL_RX_PHY_DATA4_HE_MU_EXT_SIGB_DCM),
1442 					 IEEE80211_RADIOTAP_HE_MU_FLAGS1_SIG_B_DCM);
1443 		he_mu->flags1 |=
1444 			le16_encode_bits(le16_get_bits(phy_data->d4,
1445 						       IWL_RX_PHY_DATA4_HE_MU_EXT_SIGB_MCS_MASK),
1446 					 IEEE80211_RADIOTAP_HE_MU_FLAGS1_SIG_B_MCS);
1447 		he_mu->flags2 |=
1448 			le16_encode_bits(le16_get_bits(phy_data->d4,
1449 						       IWL_RX_PHY_DATA4_HE_MU_EXT_PREAMBLE_PUNC_TYPE_MASK),
1450 					 IEEE80211_RADIOTAP_HE_MU_FLAGS2_PUNC_FROM_SIG_A_BW);
1451 		iwl_mvm_decode_he_mu_ext(mvm, phy_data, rate_n_flags, he_mu);
1452 		fallthrough;
1453 	case IWL_RX_PHY_INFO_TYPE_HE_MU:
1454 		he_mu->flags2 |=
1455 			le16_encode_bits(le32_get_bits(phy_data->d1,
1456 						       IWL_RX_PHY_DATA1_HE_MU_SIBG_SYM_OR_USER_NUM_MASK),
1457 					 IEEE80211_RADIOTAP_HE_MU_FLAGS2_SIG_B_SYMS_USERS);
1458 		he_mu->flags2 |=
1459 			le16_encode_bits(le32_get_bits(phy_data->d1,
1460 						       IWL_RX_PHY_DATA1_HE_MU_SIGB_COMPRESSION),
1461 					 IEEE80211_RADIOTAP_HE_MU_FLAGS2_SIG_B_COMP);
1462 		fallthrough;
1463 	case IWL_RX_PHY_INFO_TYPE_HE_TB:
1464 	case IWL_RX_PHY_INFO_TYPE_HE_TB_EXT:
1465 		iwl_mvm_decode_he_phy_ru_alloc(phy_data, rate_n_flags,
1466 					       he, he_mu, rx_status);
1467 		break;
1468 	case IWL_RX_PHY_INFO_TYPE_HE_SU:
1469 		he->data1 |= cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA1_BEAM_CHANGE_KNOWN);
1470 		he->data3 |= le16_encode_bits(le32_get_bits(phy_data->d0,
1471 							    IWL_RX_PHY_DATA0_HE_BEAM_CHNG),
1472 					      IEEE80211_RADIOTAP_HE_DATA3_BEAM_CHANGE);
1473 		break;
1474 	default:
1475 		/* nothing */
1476 		break;
1477 	}
1478 }
1479 
1480 static void iwl_mvm_rx_he(struct iwl_mvm *mvm, struct sk_buff *skb,
1481 			  struct iwl_mvm_rx_phy_data *phy_data,
1482 			  u32 rate_n_flags, u16 phy_info, int queue)
1483 {
1484 	struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
1485 	struct ieee80211_radiotap_he *he = NULL;
1486 	struct ieee80211_radiotap_he_mu *he_mu = NULL;
1487 	u32 he_type = rate_n_flags & RATE_MCS_HE_TYPE_MSK;
1488 	u8 stbc, ltf;
1489 	static const struct ieee80211_radiotap_he known = {
1490 		.data1 = cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA1_DATA_MCS_KNOWN |
1491 				     IEEE80211_RADIOTAP_HE_DATA1_DATA_DCM_KNOWN |
1492 				     IEEE80211_RADIOTAP_HE_DATA1_STBC_KNOWN |
1493 				     IEEE80211_RADIOTAP_HE_DATA1_CODING_KNOWN),
1494 		.data2 = cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA2_GI_KNOWN |
1495 				     IEEE80211_RADIOTAP_HE_DATA2_TXBF_KNOWN),
1496 	};
1497 	static const struct ieee80211_radiotap_he_mu mu_known = {
1498 		.flags1 = cpu_to_le16(IEEE80211_RADIOTAP_HE_MU_FLAGS1_SIG_B_MCS_KNOWN |
1499 				      IEEE80211_RADIOTAP_HE_MU_FLAGS1_SIG_B_DCM_KNOWN |
1500 				      IEEE80211_RADIOTAP_HE_MU_FLAGS1_SIG_B_SYMS_USERS_KNOWN |
1501 				      IEEE80211_RADIOTAP_HE_MU_FLAGS1_SIG_B_COMP_KNOWN),
1502 		.flags2 = cpu_to_le16(IEEE80211_RADIOTAP_HE_MU_FLAGS2_PUNC_FROM_SIG_A_BW_KNOWN |
1503 				      IEEE80211_RADIOTAP_HE_MU_FLAGS2_BW_FROM_SIG_A_BW_KNOWN),
1504 	};
1505 
1506 	he = skb_put_data(skb, &known, sizeof(known));
1507 	rx_status->flag |= RX_FLAG_RADIOTAP_HE;
1508 
1509 	if (phy_data->info_type == IWL_RX_PHY_INFO_TYPE_HE_MU ||
1510 	    phy_data->info_type == IWL_RX_PHY_INFO_TYPE_HE_MU_EXT) {
1511 		he_mu = skb_put_data(skb, &mu_known, sizeof(mu_known));
1512 		rx_status->flag |= RX_FLAG_RADIOTAP_HE_MU;
1513 	}
1514 
1515 	/* report the AMPDU-EOF bit on single frames */
1516 	if (!queue && !(phy_info & IWL_RX_MPDU_PHY_AMPDU)) {
1517 		rx_status->flag |= RX_FLAG_AMPDU_DETAILS;
1518 		rx_status->flag |= RX_FLAG_AMPDU_EOF_BIT_KNOWN;
1519 		if (phy_data->d0 & cpu_to_le32(IWL_RX_PHY_DATA0_HE_DELIM_EOF))
1520 			rx_status->flag |= RX_FLAG_AMPDU_EOF_BIT;
1521 	}
1522 
1523 	if (phy_info & IWL_RX_MPDU_PHY_TSF_OVERLOAD)
1524 		iwl_mvm_decode_he_phy_data(mvm, phy_data, he, he_mu, rx_status,
1525 					   rate_n_flags, queue);
1526 
1527 	/* update aggregation data for monitor sake on default queue */
1528 	if (!queue && (phy_info & IWL_RX_MPDU_PHY_TSF_OVERLOAD) &&
1529 	    (phy_info & IWL_RX_MPDU_PHY_AMPDU)) {
1530 		bool toggle_bit = phy_info & IWL_RX_MPDU_PHY_AMPDU_TOGGLE;
1531 
1532 		/* toggle is switched whenever new aggregation starts */
1533 		if (toggle_bit != mvm->ampdu_toggle) {
1534 			rx_status->flag |= RX_FLAG_AMPDU_EOF_BIT_KNOWN;
1535 			if (phy_data->d0 & cpu_to_le32(IWL_RX_PHY_DATA0_HE_DELIM_EOF))
1536 				rx_status->flag |= RX_FLAG_AMPDU_EOF_BIT;
1537 		}
1538 	}
1539 
1540 	if (he_type == RATE_MCS_HE_TYPE_EXT_SU &&
1541 	    rate_n_flags & RATE_MCS_HE_106T_MSK) {
1542 		rx_status->bw = RATE_INFO_BW_HE_RU;
1543 		rx_status->he_ru = NL80211_RATE_INFO_HE_RU_ALLOC_106;
1544 	}
1545 
1546 	/* actually data is filled in mac80211 */
1547 	if (he_type == RATE_MCS_HE_TYPE_SU ||
1548 	    he_type == RATE_MCS_HE_TYPE_EXT_SU)
1549 		he->data1 |=
1550 			cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA1_BW_RU_ALLOC_KNOWN);
1551 
1552 	stbc = (rate_n_flags & RATE_MCS_STBC_MSK) >> RATE_MCS_STBC_POS;
1553 	rx_status->nss =
1554 		((rate_n_flags & RATE_MCS_NSS_MSK) >>
1555 		 RATE_MCS_NSS_POS) + 1;
1556 	rx_status->rate_idx = rate_n_flags & RATE_MCS_CODE_MSK;
1557 	rx_status->encoding = RX_ENC_HE;
1558 	rx_status->enc_flags |= stbc << RX_ENC_FLAG_STBC_SHIFT;
1559 	if (rate_n_flags & RATE_MCS_BF_MSK)
1560 		rx_status->enc_flags |= RX_ENC_FLAG_BF;
1561 
1562 	rx_status->he_dcm =
1563 		!!(rate_n_flags & RATE_HE_DUAL_CARRIER_MODE_MSK);
1564 
1565 #define CHECK_TYPE(F)							\
1566 	BUILD_BUG_ON(IEEE80211_RADIOTAP_HE_DATA1_FORMAT_ ## F !=	\
1567 		     (RATE_MCS_HE_TYPE_ ## F >> RATE_MCS_HE_TYPE_POS))
1568 
1569 	CHECK_TYPE(SU);
1570 	CHECK_TYPE(EXT_SU);
1571 	CHECK_TYPE(MU);
1572 	CHECK_TYPE(TRIG);
1573 
1574 	he->data1 |= cpu_to_le16(he_type >> RATE_MCS_HE_TYPE_POS);
1575 
1576 	if (rate_n_flags & RATE_MCS_BF_MSK)
1577 		he->data5 |= cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA5_TXBF);
1578 
1579 	switch ((rate_n_flags & RATE_MCS_HE_GI_LTF_MSK) >>
1580 		RATE_MCS_HE_GI_LTF_POS) {
1581 	case 0:
1582 		if (he_type == RATE_MCS_HE_TYPE_TRIG)
1583 			rx_status->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
1584 		else
1585 			rx_status->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
1586 		if (he_type == RATE_MCS_HE_TYPE_MU)
1587 			ltf = IEEE80211_RADIOTAP_HE_DATA5_LTF_SIZE_4X;
1588 		else
1589 			ltf = IEEE80211_RADIOTAP_HE_DATA5_LTF_SIZE_1X;
1590 		break;
1591 	case 1:
1592 		if (he_type == RATE_MCS_HE_TYPE_TRIG)
1593 			rx_status->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
1594 		else
1595 			rx_status->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
1596 		ltf = IEEE80211_RADIOTAP_HE_DATA5_LTF_SIZE_2X;
1597 		break;
1598 	case 2:
1599 		if (he_type == RATE_MCS_HE_TYPE_TRIG) {
1600 			rx_status->he_gi = NL80211_RATE_INFO_HE_GI_3_2;
1601 			ltf = IEEE80211_RADIOTAP_HE_DATA5_LTF_SIZE_4X;
1602 		} else {
1603 			rx_status->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
1604 			ltf = IEEE80211_RADIOTAP_HE_DATA5_LTF_SIZE_2X;
1605 		}
1606 		break;
1607 	case 3:
1608 		rx_status->he_gi = NL80211_RATE_INFO_HE_GI_3_2;
1609 		ltf = IEEE80211_RADIOTAP_HE_DATA5_LTF_SIZE_4X;
1610 		break;
1611 	case 4:
1612 		rx_status->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
1613 		ltf = IEEE80211_RADIOTAP_HE_DATA5_LTF_SIZE_4X;
1614 		break;
1615 	default:
1616 		ltf = IEEE80211_RADIOTAP_HE_DATA5_LTF_SIZE_UNKNOWN;
1617 	}
1618 
1619 	he->data5 |= le16_encode_bits(ltf,
1620 				      IEEE80211_RADIOTAP_HE_DATA5_LTF_SIZE);
1621 }
1622 
1623 static void iwl_mvm_decode_lsig(struct sk_buff *skb,
1624 				struct iwl_mvm_rx_phy_data *phy_data)
1625 {
1626 	struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
1627 	struct ieee80211_radiotap_lsig *lsig;
1628 
1629 	switch (phy_data->info_type) {
1630 	case IWL_RX_PHY_INFO_TYPE_HT:
1631 	case IWL_RX_PHY_INFO_TYPE_VHT_SU:
1632 	case IWL_RX_PHY_INFO_TYPE_VHT_MU:
1633 	case IWL_RX_PHY_INFO_TYPE_HE_TB_EXT:
1634 	case IWL_RX_PHY_INFO_TYPE_HE_SU:
1635 	case IWL_RX_PHY_INFO_TYPE_HE_MU:
1636 	case IWL_RX_PHY_INFO_TYPE_HE_MU_EXT:
1637 	case IWL_RX_PHY_INFO_TYPE_HE_TB:
1638 		lsig = skb_put(skb, sizeof(*lsig));
1639 		lsig->data1 = cpu_to_le16(IEEE80211_RADIOTAP_LSIG_DATA1_LENGTH_KNOWN);
1640 		lsig->data2 = le16_encode_bits(le32_get_bits(phy_data->d1,
1641 							     IWL_RX_PHY_DATA1_LSIG_LEN_MASK),
1642 					       IEEE80211_RADIOTAP_LSIG_DATA2_LENGTH);
1643 		rx_status->flag |= RX_FLAG_RADIOTAP_LSIG;
1644 		break;
1645 	default:
1646 		break;
1647 	}
1648 }
1649 
1650 static inline u8 iwl_mvm_nl80211_band_from_rx_msdu(u8 phy_band)
1651 {
1652 	switch (phy_band) {
1653 	case PHY_BAND_24:
1654 		return NL80211_BAND_2GHZ;
1655 	case PHY_BAND_5:
1656 		return NL80211_BAND_5GHZ;
1657 	case PHY_BAND_6:
1658 		return NL80211_BAND_6GHZ;
1659 	default:
1660 		WARN_ONCE(1, "Unsupported phy band (%u)\n", phy_band);
1661 		return NL80211_BAND_5GHZ;
1662 	}
1663 }
1664 
1665 struct iwl_rx_sta_csa {
1666 	bool all_sta_unblocked;
1667 	struct ieee80211_vif *vif;
1668 };
1669 
1670 static void iwl_mvm_rx_get_sta_block_tx(void *data, struct ieee80211_sta *sta)
1671 {
1672 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1673 	struct iwl_rx_sta_csa *rx_sta_csa = data;
1674 
1675 	if (mvmsta->vif != rx_sta_csa->vif)
1676 		return;
1677 
1678 	if (mvmsta->disable_tx)
1679 		rx_sta_csa->all_sta_unblocked = false;
1680 }
1681 
1682 void iwl_mvm_rx_mpdu_mq(struct iwl_mvm *mvm, struct napi_struct *napi,
1683 			struct iwl_rx_cmd_buffer *rxb, int queue)
1684 {
1685 	struct ieee80211_rx_status *rx_status;
1686 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1687 	struct iwl_rx_mpdu_desc *desc = (void *)pkt->data;
1688 	struct ieee80211_hdr *hdr;
1689 	u32 len;
1690 	u32 pkt_len = iwl_rx_packet_payload_len(pkt);
1691 	u32 rate_n_flags, gp2_on_air_rise;
1692 	u16 phy_info;
1693 	struct ieee80211_sta *sta = NULL;
1694 	struct sk_buff *skb;
1695 	u8 crypt_len = 0, channel, energy_a, energy_b;
1696 	size_t desc_size;
1697 	struct iwl_mvm_rx_phy_data phy_data = {
1698 		.info_type = IWL_RX_PHY_INFO_TYPE_NONE,
1699 	};
1700 	u32 format;
1701 	bool is_sgi;
1702 
1703 	if (unlikely(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)))
1704 		return;
1705 
1706 	if (mvm->trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_AX210)
1707 		desc_size = sizeof(*desc);
1708 	else
1709 		desc_size = IWL_RX_DESC_SIZE_V1;
1710 
1711 	if (unlikely(pkt_len < desc_size)) {
1712 		IWL_DEBUG_DROP(mvm, "Bad REPLY_RX_MPDU_CMD size\n");
1713 		return;
1714 	}
1715 
1716 	if (mvm->trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_AX210) {
1717 		rate_n_flags = le32_to_cpu(desc->v3.rate_n_flags);
1718 		channel = desc->v3.channel;
1719 		gp2_on_air_rise = le32_to_cpu(desc->v3.gp2_on_air_rise);
1720 		energy_a = desc->v3.energy_a;
1721 		energy_b = desc->v3.energy_b;
1722 
1723 		phy_data.d0 = desc->v3.phy_data0;
1724 		phy_data.d1 = desc->v3.phy_data1;
1725 		phy_data.d2 = desc->v3.phy_data2;
1726 		phy_data.d3 = desc->v3.phy_data3;
1727 	} else {
1728 		rate_n_flags = le32_to_cpu(desc->v1.rate_n_flags);
1729 		channel = desc->v1.channel;
1730 		gp2_on_air_rise = le32_to_cpu(desc->v1.gp2_on_air_rise);
1731 		energy_a = desc->v1.energy_a;
1732 		energy_b = desc->v1.energy_b;
1733 
1734 		phy_data.d0 = desc->v1.phy_data0;
1735 		phy_data.d1 = desc->v1.phy_data1;
1736 		phy_data.d2 = desc->v1.phy_data2;
1737 		phy_data.d3 = desc->v1.phy_data3;
1738 	}
1739 	if (iwl_fw_lookup_notif_ver(mvm->fw, LEGACY_GROUP,
1740 				    REPLY_RX_MPDU_CMD, 0) < 4) {
1741 		rate_n_flags = iwl_new_rate_from_v1(rate_n_flags);
1742 		IWL_DEBUG_DROP(mvm, "Got old format rate, converting. New rate: 0x%x\n",
1743 			       rate_n_flags);
1744 	}
1745 	format = rate_n_flags & RATE_MCS_MOD_TYPE_MSK;
1746 
1747 	len = le16_to_cpu(desc->mpdu_len);
1748 
1749 	if (unlikely(len + desc_size > pkt_len)) {
1750 		IWL_DEBUG_DROP(mvm, "FW lied about packet len\n");
1751 		return;
1752 	}
1753 
1754 	phy_info = le16_to_cpu(desc->phy_info);
1755 	phy_data.d4 = desc->phy_data4;
1756 
1757 	if (phy_info & IWL_RX_MPDU_PHY_TSF_OVERLOAD)
1758 		phy_data.info_type =
1759 			le32_get_bits(phy_data.d1,
1760 				      IWL_RX_PHY_DATA1_INFO_TYPE_MASK);
1761 
1762 	hdr = (void *)(pkt->data + desc_size);
1763 	/* Dont use dev_alloc_skb(), we'll have enough headroom once
1764 	 * ieee80211_hdr pulled.
1765 	 */
1766 	skb = alloc_skb(128, GFP_ATOMIC);
1767 	if (!skb) {
1768 		IWL_ERR(mvm, "alloc_skb failed\n");
1769 		return;
1770 	}
1771 
1772 	if (desc->mac_flags2 & IWL_RX_MPDU_MFLG2_PAD) {
1773 		/*
1774 		 * If the device inserted padding it means that (it thought)
1775 		 * the 802.11 header wasn't a multiple of 4 bytes long. In
1776 		 * this case, reserve two bytes at the start of the SKB to
1777 		 * align the payload properly in case we end up copying it.
1778 		 */
1779 		skb_reserve(skb, 2);
1780 	}
1781 
1782 	rx_status = IEEE80211_SKB_RXCB(skb);
1783 
1784 	/* This may be overridden by iwl_mvm_rx_he() to HE_RU */
1785 	switch (rate_n_flags & RATE_MCS_CHAN_WIDTH_MSK) {
1786 	case RATE_MCS_CHAN_WIDTH_20:
1787 		break;
1788 	case RATE_MCS_CHAN_WIDTH_40:
1789 		rx_status->bw = RATE_INFO_BW_40;
1790 		break;
1791 	case RATE_MCS_CHAN_WIDTH_80:
1792 		rx_status->bw = RATE_INFO_BW_80;
1793 		break;
1794 	case RATE_MCS_CHAN_WIDTH_160:
1795 		rx_status->bw = RATE_INFO_BW_160;
1796 		break;
1797 	}
1798 
1799 	if (format == RATE_MCS_HE_MSK)
1800 		iwl_mvm_rx_he(mvm, skb, &phy_data, rate_n_flags,
1801 			      phy_info, queue);
1802 
1803 	iwl_mvm_decode_lsig(skb, &phy_data);
1804 
1805 	/*
1806 	 * Keep packets with CRC errors (and with overrun) for monitor mode
1807 	 * (otherwise the firmware discards them) but mark them as bad.
1808 	 */
1809 	if (!(desc->status & cpu_to_le32(IWL_RX_MPDU_STATUS_CRC_OK)) ||
1810 	    !(desc->status & cpu_to_le32(IWL_RX_MPDU_STATUS_OVERRUN_OK))) {
1811 		IWL_DEBUG_RX(mvm, "Bad CRC or FIFO: 0x%08X.\n",
1812 			     le32_to_cpu(desc->status));
1813 		rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
1814 	}
1815 	/* set the preamble flag if appropriate */
1816 	if (format == RATE_MCS_CCK_MSK &&
1817 	    phy_info & IWL_RX_MPDU_PHY_SHORT_PREAMBLE)
1818 		rx_status->enc_flags |= RX_ENC_FLAG_SHORTPRE;
1819 
1820 	if (likely(!(phy_info & IWL_RX_MPDU_PHY_TSF_OVERLOAD))) {
1821 		u64 tsf_on_air_rise;
1822 
1823 		if (mvm->trans->trans_cfg->device_family >=
1824 		    IWL_DEVICE_FAMILY_AX210)
1825 			tsf_on_air_rise = le64_to_cpu(desc->v3.tsf_on_air_rise);
1826 		else
1827 			tsf_on_air_rise = le64_to_cpu(desc->v1.tsf_on_air_rise);
1828 
1829 		rx_status->mactime = tsf_on_air_rise;
1830 		/* TSF as indicated by the firmware is at INA time */
1831 		rx_status->flag |= RX_FLAG_MACTIME_PLCP_START;
1832 	}
1833 
1834 	rx_status->device_timestamp = gp2_on_air_rise;
1835 	if (iwl_mvm_is_band_in_rx_supported(mvm)) {
1836 		u8 band = BAND_IN_RX_STATUS(desc->mac_phy_idx);
1837 
1838 		rx_status->band = iwl_mvm_nl80211_band_from_rx_msdu(band);
1839 	} else {
1840 		rx_status->band = channel > 14 ? NL80211_BAND_5GHZ :
1841 			NL80211_BAND_2GHZ;
1842 	}
1843 	rx_status->freq = ieee80211_channel_to_frequency(channel,
1844 							 rx_status->band);
1845 	iwl_mvm_get_signal_strength(mvm, rx_status, rate_n_flags, energy_a,
1846 				    energy_b);
1847 
1848 	/* update aggregation data for monitor sake on default queue */
1849 	if (!queue && (phy_info & IWL_RX_MPDU_PHY_AMPDU)) {
1850 		bool toggle_bit = phy_info & IWL_RX_MPDU_PHY_AMPDU_TOGGLE;
1851 
1852 		rx_status->flag |= RX_FLAG_AMPDU_DETAILS;
1853 		/*
1854 		 * Toggle is switched whenever new aggregation starts. Make
1855 		 * sure ampdu_reference is never 0 so we can later use it to
1856 		 * see if the frame was really part of an A-MPDU or not.
1857 		 */
1858 		if (toggle_bit != mvm->ampdu_toggle) {
1859 			mvm->ampdu_ref++;
1860 			if (mvm->ampdu_ref == 0)
1861 				mvm->ampdu_ref++;
1862 			mvm->ampdu_toggle = toggle_bit;
1863 		}
1864 		rx_status->ampdu_reference = mvm->ampdu_ref;
1865 	}
1866 
1867 	if (unlikely(mvm->monitor_on))
1868 		iwl_mvm_add_rtap_sniffer_config(mvm, skb);
1869 
1870 	rcu_read_lock();
1871 
1872 	if (desc->status & cpu_to_le32(IWL_RX_MPDU_STATUS_SRC_STA_FOUND)) {
1873 		u8 id = le32_get_bits(desc->status, IWL_RX_MPDU_STATUS_STA_ID);
1874 
1875 		if (!WARN_ON_ONCE(id >= mvm->fw->ucode_capa.num_stations)) {
1876 			sta = rcu_dereference(mvm->fw_id_to_mac_id[id]);
1877 			if (IS_ERR(sta))
1878 				sta = NULL;
1879 		}
1880 	} else if (!is_multicast_ether_addr(hdr->addr2)) {
1881 		/*
1882 		 * This is fine since we prevent two stations with the same
1883 		 * address from being added.
1884 		 */
1885 		sta = ieee80211_find_sta_by_ifaddr(mvm->hw, hdr->addr2, NULL);
1886 	}
1887 
1888 	if (iwl_mvm_rx_crypto(mvm, sta, hdr, rx_status, phy_info, desc,
1889 			      le32_to_cpu(pkt->len_n_flags), queue,
1890 			      &crypt_len)) {
1891 		kfree_skb(skb);
1892 		goto out;
1893 	}
1894 
1895 	if (sta) {
1896 		struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1897 		struct ieee80211_vif *tx_blocked_vif =
1898 			rcu_dereference(mvm->csa_tx_blocked_vif);
1899 		u8 baid = (u8)((le32_to_cpu(desc->reorder_data) &
1900 			       IWL_RX_MPDU_REORDER_BAID_MASK) >>
1901 			       IWL_RX_MPDU_REORDER_BAID_SHIFT);
1902 		struct iwl_fw_dbg_trigger_tlv *trig;
1903 		struct ieee80211_vif *vif = mvmsta->vif;
1904 
1905 		if (!mvm->tcm.paused && len >= sizeof(*hdr) &&
1906 		    !is_multicast_ether_addr(hdr->addr1) &&
1907 		    ieee80211_is_data(hdr->frame_control) &&
1908 		    time_after(jiffies, mvm->tcm.ts + MVM_TCM_PERIOD))
1909 			schedule_delayed_work(&mvm->tcm.work, 0);
1910 
1911 		/*
1912 		 * We have tx blocked stations (with CS bit). If we heard
1913 		 * frames from a blocked station on a new channel we can
1914 		 * TX to it again.
1915 		 */
1916 		if (unlikely(tx_blocked_vif) && tx_blocked_vif == vif) {
1917 			struct iwl_mvm_vif *mvmvif =
1918 				iwl_mvm_vif_from_mac80211(tx_blocked_vif);
1919 			struct iwl_rx_sta_csa rx_sta_csa = {
1920 				.all_sta_unblocked = true,
1921 				.vif = tx_blocked_vif,
1922 			};
1923 
1924 			if (mvmvif->csa_target_freq == rx_status->freq)
1925 				iwl_mvm_sta_modify_disable_tx_ap(mvm, sta,
1926 								 false);
1927 			ieee80211_iterate_stations_atomic(mvm->hw,
1928 							  iwl_mvm_rx_get_sta_block_tx,
1929 							  &rx_sta_csa);
1930 
1931 			if (rx_sta_csa.all_sta_unblocked) {
1932 				RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);
1933 				/* Unblock BCAST / MCAST station */
1934 				iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, false);
1935 				cancel_delayed_work_sync(&mvm->cs_tx_unblock_dwork);
1936 			}
1937 		}
1938 
1939 		rs_update_last_rssi(mvm, mvmsta, rx_status);
1940 
1941 		trig = iwl_fw_dbg_trigger_on(&mvm->fwrt,
1942 					     ieee80211_vif_to_wdev(vif),
1943 					     FW_DBG_TRIGGER_RSSI);
1944 
1945 		if (trig && ieee80211_is_beacon(hdr->frame_control)) {
1946 			struct iwl_fw_dbg_trigger_low_rssi *rssi_trig;
1947 			s32 rssi;
1948 
1949 			rssi_trig = (void *)trig->data;
1950 			rssi = le32_to_cpu(rssi_trig->rssi);
1951 
1952 			if (rx_status->signal < rssi)
1953 				iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
1954 							NULL);
1955 		}
1956 
1957 		if (ieee80211_is_data(hdr->frame_control))
1958 			iwl_mvm_rx_csum(mvm, sta, skb, pkt);
1959 
1960 		if (iwl_mvm_is_dup(sta, queue, rx_status, hdr, desc)) {
1961 			kfree_skb(skb);
1962 			goto out;
1963 		}
1964 
1965 		/*
1966 		 * Our hardware de-aggregates AMSDUs but copies the mac header
1967 		 * as it to the de-aggregated MPDUs. We need to turn off the
1968 		 * AMSDU bit in the QoS control ourselves.
1969 		 * In addition, HW reverses addr3 and addr4 - reverse it back.
1970 		 */
1971 		if ((desc->mac_flags2 & IWL_RX_MPDU_MFLG2_AMSDU) &&
1972 		    !WARN_ON(!ieee80211_is_data_qos(hdr->frame_control))) {
1973 			u8 *qc = ieee80211_get_qos_ctl(hdr);
1974 
1975 			*qc &= ~IEEE80211_QOS_CTL_A_MSDU_PRESENT;
1976 
1977 			if (mvm->trans->trans_cfg->device_family ==
1978 			    IWL_DEVICE_FAMILY_9000) {
1979 				iwl_mvm_flip_address(hdr->addr3);
1980 
1981 				if (ieee80211_has_a4(hdr->frame_control))
1982 					iwl_mvm_flip_address(hdr->addr4);
1983 			}
1984 		}
1985 		if (baid != IWL_RX_REORDER_DATA_INVALID_BAID) {
1986 			u32 reorder_data = le32_to_cpu(desc->reorder_data);
1987 
1988 			iwl_mvm_agg_rx_received(mvm, reorder_data, baid);
1989 		}
1990 	}
1991 
1992 	is_sgi = format == RATE_MCS_HE_MSK ?
1993 		iwl_he_is_sgi(rate_n_flags) :
1994 		rate_n_flags & RATE_MCS_SGI_MSK;
1995 
1996 	if (!(format == RATE_MCS_CCK_MSK) && is_sgi)
1997 		rx_status->enc_flags |= RX_ENC_FLAG_SHORT_GI;
1998 	if (rate_n_flags & RATE_MCS_LDPC_MSK)
1999 		rx_status->enc_flags |= RX_ENC_FLAG_LDPC;
2000 	if (format == RATE_MCS_HT_MSK) {
2001 		u8 stbc = (rate_n_flags & RATE_MCS_STBC_MSK) >>
2002 			RATE_MCS_STBC_POS;
2003 		rx_status->encoding = RX_ENC_HT;
2004 		rx_status->rate_idx = RATE_HT_MCS_INDEX(rate_n_flags);
2005 		rx_status->enc_flags |= stbc << RX_ENC_FLAG_STBC_SHIFT;
2006 	} else if (format == RATE_MCS_VHT_MSK) {
2007 		u8 stbc = (rate_n_flags & RATE_MCS_STBC_MSK) >>
2008 			RATE_MCS_STBC_POS;
2009 		rx_status->nss = ((rate_n_flags & RATE_MCS_NSS_MSK) >>
2010 			RATE_MCS_NSS_POS) + 1;
2011 		rx_status->rate_idx = rate_n_flags & RATE_MCS_CODE_MSK;
2012 		rx_status->encoding = RX_ENC_VHT;
2013 		rx_status->enc_flags |= stbc << RX_ENC_FLAG_STBC_SHIFT;
2014 		if (rate_n_flags & RATE_MCS_BF_MSK)
2015 			rx_status->enc_flags |= RX_ENC_FLAG_BF;
2016 	} else if (!(format == RATE_MCS_HE_MSK)) {
2017 		int rate = iwl_mvm_legacy_hw_idx_to_mac80211_idx(rate_n_flags,
2018 								 rx_status->band);
2019 
2020 		if (WARN(rate < 0 || rate > 0xFF,
2021 			 "Invalid rate flags 0x%x, band %d,\n",
2022 			 rate_n_flags, rx_status->band)) {
2023 			kfree_skb(skb);
2024 			goto out;
2025 		}
2026 		rx_status->rate_idx = rate;
2027 	}
2028 
2029 	/* management stuff on default queue */
2030 	if (!queue) {
2031 		if (unlikely((ieee80211_is_beacon(hdr->frame_control) ||
2032 			      ieee80211_is_probe_resp(hdr->frame_control)) &&
2033 			     mvm->sched_scan_pass_all ==
2034 			     SCHED_SCAN_PASS_ALL_ENABLED))
2035 			mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_FOUND;
2036 
2037 		if (unlikely(ieee80211_is_beacon(hdr->frame_control) ||
2038 			     ieee80211_is_probe_resp(hdr->frame_control)))
2039 			rx_status->boottime_ns = ktime_get_boottime_ns();
2040 	}
2041 
2042 	if (iwl_mvm_create_skb(mvm, skb, hdr, len, crypt_len, rxb)) {
2043 		kfree_skb(skb);
2044 		goto out;
2045 	}
2046 
2047 	if (!iwl_mvm_reorder(mvm, napi, queue, sta, skb, desc))
2048 		iwl_mvm_pass_packet_to_mac80211(mvm, napi, skb, queue,
2049 						sta);
2050 out:
2051 	rcu_read_unlock();
2052 }
2053 
2054 void iwl_mvm_rx_monitor_no_data(struct iwl_mvm *mvm, struct napi_struct *napi,
2055 				struct iwl_rx_cmd_buffer *rxb, int queue)
2056 {
2057 	struct ieee80211_rx_status *rx_status;
2058 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
2059 	struct iwl_rx_no_data *desc = (void *)pkt->data;
2060 	u32 rate_n_flags = le32_to_cpu(desc->rate);
2061 	u32 gp2_on_air_rise = le32_to_cpu(desc->on_air_rise_time);
2062 	u32 rssi = le32_to_cpu(desc->rssi);
2063 	u32 info_type = le32_to_cpu(desc->info) & RX_NO_DATA_INFO_TYPE_MSK;
2064 	u16 phy_info = IWL_RX_MPDU_PHY_TSF_OVERLOAD;
2065 	struct ieee80211_sta *sta = NULL;
2066 	struct sk_buff *skb;
2067 	u8 channel, energy_a, energy_b;
2068 	u32 format;
2069 	struct iwl_mvm_rx_phy_data phy_data = {
2070 		.info_type = le32_get_bits(desc->phy_info[1],
2071 					   IWL_RX_PHY_DATA1_INFO_TYPE_MASK),
2072 		.d0 = desc->phy_info[0],
2073 		.d1 = desc->phy_info[1],
2074 	};
2075 	bool is_sgi;
2076 
2077 	if (iwl_fw_lookup_notif_ver(mvm->fw, DATA_PATH_GROUP,
2078 				    RX_NO_DATA_NOTIF, 0) < 2) {
2079 		IWL_DEBUG_DROP(mvm, "Got an old rate format. Old rate: 0x%x\n",
2080 			       rate_n_flags);
2081 		rate_n_flags = iwl_new_rate_from_v1(rate_n_flags);
2082 		IWL_DEBUG_DROP(mvm, " Rate after conversion to the new format: 0x%x\n",
2083 			       rate_n_flags);
2084 	}
2085 	format = rate_n_flags & RATE_MCS_MOD_TYPE_MSK;
2086 
2087 	if (unlikely(iwl_rx_packet_payload_len(pkt) < sizeof(*desc)))
2088 		return;
2089 
2090 	if (unlikely(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)))
2091 		return;
2092 
2093 	energy_a = (rssi & RX_NO_DATA_CHAIN_A_MSK) >> RX_NO_DATA_CHAIN_A_POS;
2094 	energy_b = (rssi & RX_NO_DATA_CHAIN_B_MSK) >> RX_NO_DATA_CHAIN_B_POS;
2095 	channel = (rssi & RX_NO_DATA_CHANNEL_MSK) >> RX_NO_DATA_CHANNEL_POS;
2096 
2097 	/* Dont use dev_alloc_skb(), we'll have enough headroom once
2098 	 * ieee80211_hdr pulled.
2099 	 */
2100 	skb = alloc_skb(128, GFP_ATOMIC);
2101 	if (!skb) {
2102 		IWL_ERR(mvm, "alloc_skb failed\n");
2103 		return;
2104 	}
2105 
2106 	rx_status = IEEE80211_SKB_RXCB(skb);
2107 
2108 	/* 0-length PSDU */
2109 	rx_status->flag |= RX_FLAG_NO_PSDU;
2110 
2111 	switch (info_type) {
2112 	case RX_NO_DATA_INFO_TYPE_NDP:
2113 		rx_status->zero_length_psdu_type =
2114 			IEEE80211_RADIOTAP_ZERO_LEN_PSDU_SOUNDING;
2115 		break;
2116 	case RX_NO_DATA_INFO_TYPE_MU_UNMATCHED:
2117 	case RX_NO_DATA_INFO_TYPE_HE_TB_UNMATCHED:
2118 		rx_status->zero_length_psdu_type =
2119 			IEEE80211_RADIOTAP_ZERO_LEN_PSDU_NOT_CAPTURED;
2120 		break;
2121 	default:
2122 		rx_status->zero_length_psdu_type =
2123 			IEEE80211_RADIOTAP_ZERO_LEN_PSDU_VENDOR;
2124 		break;
2125 	}
2126 
2127 	/* This may be overridden by iwl_mvm_rx_he() to HE_RU */
2128 	switch (rate_n_flags & RATE_MCS_CHAN_WIDTH_MSK) {
2129 	case RATE_MCS_CHAN_WIDTH_20:
2130 		break;
2131 	case RATE_MCS_CHAN_WIDTH_40:
2132 		rx_status->bw = RATE_INFO_BW_40;
2133 		break;
2134 	case RATE_MCS_CHAN_WIDTH_80:
2135 		rx_status->bw = RATE_INFO_BW_80;
2136 		break;
2137 	case RATE_MCS_CHAN_WIDTH_160:
2138 		rx_status->bw = RATE_INFO_BW_160;
2139 		break;
2140 	}
2141 
2142 	if (format == RATE_MCS_HE_MSK)
2143 		iwl_mvm_rx_he(mvm, skb, &phy_data, rate_n_flags,
2144 			      phy_info, queue);
2145 
2146 	iwl_mvm_decode_lsig(skb, &phy_data);
2147 
2148 	rx_status->device_timestamp = gp2_on_air_rise;
2149 	rx_status->band = channel > 14 ? NL80211_BAND_5GHZ :
2150 		NL80211_BAND_2GHZ;
2151 	rx_status->freq = ieee80211_channel_to_frequency(channel,
2152 							 rx_status->band);
2153 	iwl_mvm_get_signal_strength(mvm, rx_status, rate_n_flags, energy_a,
2154 				    energy_b);
2155 
2156 	rcu_read_lock();
2157 
2158 	is_sgi = format == RATE_MCS_HE_MSK ?
2159 		iwl_he_is_sgi(rate_n_flags) :
2160 		rate_n_flags & RATE_MCS_SGI_MSK;
2161 
2162 	if (!(format == RATE_MCS_CCK_MSK) && is_sgi)
2163 		rx_status->enc_flags |= RX_ENC_FLAG_SHORT_GI;
2164 	if (rate_n_flags & RATE_MCS_LDPC_MSK)
2165 		rx_status->enc_flags |= RX_ENC_FLAG_LDPC;
2166 	if (format == RATE_MCS_HT_MSK) {
2167 		u8 stbc = (rate_n_flags & RATE_MCS_STBC_MSK) >>
2168 				RATE_MCS_STBC_POS;
2169 		rx_status->encoding = RX_ENC_HT;
2170 		rx_status->rate_idx = RATE_HT_MCS_INDEX(rate_n_flags);
2171 		rx_status->enc_flags |= stbc << RX_ENC_FLAG_STBC_SHIFT;
2172 	} else if (format == RATE_MCS_VHT_MSK) {
2173 		u8 stbc = (rate_n_flags & RATE_MCS_STBC_MSK) >>
2174 				RATE_MCS_STBC_POS;
2175 		rx_status->rate_idx = rate_n_flags & RATE_MCS_CODE_MSK;
2176 		rx_status->encoding = RX_ENC_VHT;
2177 		rx_status->enc_flags |= stbc << RX_ENC_FLAG_STBC_SHIFT;
2178 		if (rate_n_flags & RATE_MCS_BF_MSK)
2179 			rx_status->enc_flags |= RX_ENC_FLAG_BF;
2180 		/*
2181 		 * take the nss from the rx_vec since the rate_n_flags has
2182 		 * only 2 bits for the nss which gives a max of 4 ss but
2183 		 * there may be up to 8 spatial streams
2184 		 */
2185 		rx_status->nss =
2186 			le32_get_bits(desc->rx_vec[0],
2187 				      RX_NO_DATA_RX_VEC0_VHT_NSTS_MSK) + 1;
2188 	} else if (format == RATE_MCS_HE_MSK) {
2189 		rx_status->nss =
2190 			le32_get_bits(desc->rx_vec[0],
2191 				      RX_NO_DATA_RX_VEC0_HE_NSTS_MSK) + 1;
2192 	} else {
2193 		int rate = iwl_mvm_legacy_hw_idx_to_mac80211_idx(rate_n_flags,
2194 							       rx_status->band);
2195 
2196 		if (WARN(rate < 0 || rate > 0xFF,
2197 			 "Invalid rate flags 0x%x, band %d,\n",
2198 			 rate_n_flags, rx_status->band)) {
2199 			kfree_skb(skb);
2200 			goto out;
2201 		}
2202 		rx_status->rate_idx = rate;
2203 	}
2204 
2205 	ieee80211_rx_napi(mvm->hw, sta, skb, napi);
2206 out:
2207 	rcu_read_unlock();
2208 }
2209 
2210 void iwl_mvm_rx_frame_release(struct iwl_mvm *mvm, struct napi_struct *napi,
2211 			      struct iwl_rx_cmd_buffer *rxb, int queue)
2212 {
2213 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
2214 	struct iwl_frame_release *release = (void *)pkt->data;
2215 
2216 	if (unlikely(iwl_rx_packet_payload_len(pkt) < sizeof(*release)))
2217 		return;
2218 
2219 	iwl_mvm_release_frames_from_notif(mvm, napi, release->baid,
2220 					  le16_to_cpu(release->nssn),
2221 					  queue, 0);
2222 }
2223 
2224 void iwl_mvm_rx_bar_frame_release(struct iwl_mvm *mvm, struct napi_struct *napi,
2225 				  struct iwl_rx_cmd_buffer *rxb, int queue)
2226 {
2227 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
2228 	struct iwl_bar_frame_release *release = (void *)pkt->data;
2229 	unsigned int baid = le32_get_bits(release->ba_info,
2230 					  IWL_BAR_FRAME_RELEASE_BAID_MASK);
2231 	unsigned int nssn = le32_get_bits(release->ba_info,
2232 					  IWL_BAR_FRAME_RELEASE_NSSN_MASK);
2233 	unsigned int sta_id = le32_get_bits(release->sta_tid,
2234 					    IWL_BAR_FRAME_RELEASE_STA_MASK);
2235 	unsigned int tid = le32_get_bits(release->sta_tid,
2236 					 IWL_BAR_FRAME_RELEASE_TID_MASK);
2237 	struct iwl_mvm_baid_data *baid_data;
2238 
2239 	if (unlikely(iwl_rx_packet_payload_len(pkt) < sizeof(*release)))
2240 		return;
2241 
2242 	if (WARN_ON_ONCE(baid == IWL_RX_REORDER_DATA_INVALID_BAID ||
2243 			 baid >= ARRAY_SIZE(mvm->baid_map)))
2244 		return;
2245 
2246 	rcu_read_lock();
2247 	baid_data = rcu_dereference(mvm->baid_map[baid]);
2248 	if (!baid_data) {
2249 		IWL_DEBUG_RX(mvm,
2250 			     "Got valid BAID %d but not allocated, invalid BAR release!\n",
2251 			      baid);
2252 		goto out;
2253 	}
2254 
2255 	if (WARN(tid != baid_data->tid || sta_id != baid_data->sta_id,
2256 		 "baid 0x%x is mapped to sta:%d tid:%d, but BAR release received for sta:%d tid:%d\n",
2257 		 baid, baid_data->sta_id, baid_data->tid, sta_id,
2258 		 tid))
2259 		goto out;
2260 
2261 	iwl_mvm_release_frames_from_notif(mvm, napi, baid, nssn, queue, 0);
2262 out:
2263 	rcu_read_unlock();
2264 }
2265