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