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