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