xref: /linux/drivers/net/wireless/intel/iwlwifi/mvm/rxmq.c (revision 34f7c6e7d4396090692a09789db231e12cb4762b)
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 		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 
1196 static void iwl_mvm_decode_he_mu_ext(struct iwl_mvm *mvm,
1197 				     struct iwl_mvm_rx_phy_data *phy_data,
1198 				     u32 rate_n_flags,
1199 				     struct ieee80211_radiotap_he_mu *he_mu)
1200 {
1201 	u32 phy_data2 = le32_to_cpu(phy_data->d2);
1202 	u32 phy_data3 = le32_to_cpu(phy_data->d3);
1203 	u16 phy_data4 = le16_to_cpu(phy_data->d4);
1204 
1205 	if (FIELD_GET(IWL_RX_PHY_DATA4_HE_MU_EXT_CH1_CRC_OK, phy_data4)) {
1206 		he_mu->flags1 |=
1207 			cpu_to_le16(IEEE80211_RADIOTAP_HE_MU_FLAGS1_CH1_RU_KNOWN |
1208 				    IEEE80211_RADIOTAP_HE_MU_FLAGS1_CH1_CTR_26T_RU_KNOWN);
1209 
1210 		he_mu->flags1 |=
1211 			le16_encode_bits(FIELD_GET(IWL_RX_PHY_DATA4_HE_MU_EXT_CH1_CTR_RU,
1212 						   phy_data4),
1213 					 IEEE80211_RADIOTAP_HE_MU_FLAGS1_CH1_CTR_26T_RU);
1214 
1215 		he_mu->ru_ch1[0] = FIELD_GET(IWL_RX_PHY_DATA2_HE_MU_EXT_CH1_RU0,
1216 					     phy_data2);
1217 		he_mu->ru_ch1[1] = FIELD_GET(IWL_RX_PHY_DATA3_HE_MU_EXT_CH1_RU1,
1218 					     phy_data3);
1219 		he_mu->ru_ch1[2] = FIELD_GET(IWL_RX_PHY_DATA2_HE_MU_EXT_CH1_RU2,
1220 					     phy_data2);
1221 		he_mu->ru_ch1[3] = FIELD_GET(IWL_RX_PHY_DATA3_HE_MU_EXT_CH1_RU3,
1222 					     phy_data3);
1223 	}
1224 
1225 	if (FIELD_GET(IWL_RX_PHY_DATA4_HE_MU_EXT_CH2_CRC_OK, phy_data4) &&
1226 	    (rate_n_flags & RATE_MCS_CHAN_WIDTH_MSK_V1) != RATE_MCS_CHAN_WIDTH_20) {
1227 		he_mu->flags1 |=
1228 			cpu_to_le16(IEEE80211_RADIOTAP_HE_MU_FLAGS1_CH2_RU_KNOWN |
1229 				    IEEE80211_RADIOTAP_HE_MU_FLAGS1_CH2_CTR_26T_RU_KNOWN);
1230 
1231 		he_mu->flags2 |=
1232 			le16_encode_bits(FIELD_GET(IWL_RX_PHY_DATA4_HE_MU_EXT_CH2_CTR_RU,
1233 						   phy_data4),
1234 					 IEEE80211_RADIOTAP_HE_MU_FLAGS2_CH2_CTR_26T_RU);
1235 
1236 		he_mu->ru_ch2[0] = FIELD_GET(IWL_RX_PHY_DATA2_HE_MU_EXT_CH2_RU0,
1237 					     phy_data2);
1238 		he_mu->ru_ch2[1] = FIELD_GET(IWL_RX_PHY_DATA3_HE_MU_EXT_CH2_RU1,
1239 					     phy_data3);
1240 		he_mu->ru_ch2[2] = FIELD_GET(IWL_RX_PHY_DATA2_HE_MU_EXT_CH2_RU2,
1241 					     phy_data2);
1242 		he_mu->ru_ch2[3] = FIELD_GET(IWL_RX_PHY_DATA3_HE_MU_EXT_CH2_RU3,
1243 					     phy_data3);
1244 	}
1245 }
1246 
1247 static void
1248 iwl_mvm_decode_he_phy_ru_alloc(struct iwl_mvm_rx_phy_data *phy_data,
1249 			       u32 rate_n_flags,
1250 			       struct ieee80211_radiotap_he *he,
1251 			       struct ieee80211_radiotap_he_mu *he_mu,
1252 			       struct ieee80211_rx_status *rx_status)
1253 {
1254 	/*
1255 	 * Unfortunately, we have to leave the mac80211 data
1256 	 * incorrect for the case that we receive an HE-MU
1257 	 * transmission and *don't* have the HE phy data (due
1258 	 * to the bits being used for TSF). This shouldn't
1259 	 * happen though as management frames where we need
1260 	 * the TSF/timers are not be transmitted in HE-MU.
1261 	 */
1262 	u8 ru = le32_get_bits(phy_data->d1, IWL_RX_PHY_DATA1_HE_RU_ALLOC_MASK);
1263 	u32 he_type = rate_n_flags & RATE_MCS_HE_TYPE_MSK_V1;
1264 	u8 offs = 0;
1265 
1266 	rx_status->bw = RATE_INFO_BW_HE_RU;
1267 
1268 	he->data1 |= cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA1_BW_RU_ALLOC_KNOWN);
1269 
1270 	switch (ru) {
1271 	case 0 ... 36:
1272 		rx_status->he_ru = NL80211_RATE_INFO_HE_RU_ALLOC_26;
1273 		offs = ru;
1274 		break;
1275 	case 37 ... 52:
1276 		rx_status->he_ru = NL80211_RATE_INFO_HE_RU_ALLOC_52;
1277 		offs = ru - 37;
1278 		break;
1279 	case 53 ... 60:
1280 		rx_status->he_ru = NL80211_RATE_INFO_HE_RU_ALLOC_106;
1281 		offs = ru - 53;
1282 		break;
1283 	case 61 ... 64:
1284 		rx_status->he_ru = NL80211_RATE_INFO_HE_RU_ALLOC_242;
1285 		offs = ru - 61;
1286 		break;
1287 	case 65 ... 66:
1288 		rx_status->he_ru = NL80211_RATE_INFO_HE_RU_ALLOC_484;
1289 		offs = ru - 65;
1290 		break;
1291 	case 67:
1292 		rx_status->he_ru = NL80211_RATE_INFO_HE_RU_ALLOC_996;
1293 		break;
1294 	case 68:
1295 		rx_status->he_ru = NL80211_RATE_INFO_HE_RU_ALLOC_2x996;
1296 		break;
1297 	}
1298 	he->data2 |= le16_encode_bits(offs,
1299 				      IEEE80211_RADIOTAP_HE_DATA2_RU_OFFSET);
1300 	he->data2 |= cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA2_PRISEC_80_KNOWN |
1301 				 IEEE80211_RADIOTAP_HE_DATA2_RU_OFFSET_KNOWN);
1302 	if (phy_data->d1 & cpu_to_le32(IWL_RX_PHY_DATA1_HE_RU_ALLOC_SEC80))
1303 		he->data2 |=
1304 			cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA2_PRISEC_80_SEC);
1305 
1306 #define CHECK_BW(bw) \
1307 	BUILD_BUG_ON(IEEE80211_RADIOTAP_HE_MU_FLAGS2_BW_FROM_SIG_A_BW_ ## bw ## MHZ != \
1308 		     RATE_MCS_CHAN_WIDTH_##bw >> RATE_MCS_CHAN_WIDTH_POS); \
1309 	BUILD_BUG_ON(IEEE80211_RADIOTAP_HE_DATA6_TB_PPDU_BW_ ## bw ## MHZ != \
1310 		     RATE_MCS_CHAN_WIDTH_##bw >> RATE_MCS_CHAN_WIDTH_POS)
1311 	CHECK_BW(20);
1312 	CHECK_BW(40);
1313 	CHECK_BW(80);
1314 	CHECK_BW(160);
1315 
1316 	if (he_mu)
1317 		he_mu->flags2 |=
1318 			le16_encode_bits(FIELD_GET(RATE_MCS_CHAN_WIDTH_MSK_V1,
1319 						   rate_n_flags),
1320 					 IEEE80211_RADIOTAP_HE_MU_FLAGS2_BW_FROM_SIG_A_BW);
1321 	else if (he_type == RATE_MCS_HE_TYPE_TRIG_V1)
1322 		he->data6 |=
1323 			cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA6_TB_PPDU_BW_KNOWN) |
1324 			le16_encode_bits(FIELD_GET(RATE_MCS_CHAN_WIDTH_MSK_V1,
1325 						   rate_n_flags),
1326 					 IEEE80211_RADIOTAP_HE_DATA6_TB_PPDU_BW);
1327 }
1328 
1329 static void iwl_mvm_decode_he_phy_data(struct iwl_mvm *mvm,
1330 				       struct iwl_mvm_rx_phy_data *phy_data,
1331 				       struct ieee80211_radiotap_he *he,
1332 				       struct ieee80211_radiotap_he_mu *he_mu,
1333 				       struct ieee80211_rx_status *rx_status,
1334 				       u32 rate_n_flags, int queue)
1335 {
1336 	switch (phy_data->info_type) {
1337 	case IWL_RX_PHY_INFO_TYPE_NONE:
1338 	case IWL_RX_PHY_INFO_TYPE_CCK:
1339 	case IWL_RX_PHY_INFO_TYPE_OFDM_LGCY:
1340 	case IWL_RX_PHY_INFO_TYPE_HT:
1341 	case IWL_RX_PHY_INFO_TYPE_VHT_SU:
1342 	case IWL_RX_PHY_INFO_TYPE_VHT_MU:
1343 		return;
1344 	case IWL_RX_PHY_INFO_TYPE_HE_TB_EXT:
1345 		he->data1 |= cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA1_SPTL_REUSE_KNOWN |
1346 					 IEEE80211_RADIOTAP_HE_DATA1_SPTL_REUSE2_KNOWN |
1347 					 IEEE80211_RADIOTAP_HE_DATA1_SPTL_REUSE3_KNOWN |
1348 					 IEEE80211_RADIOTAP_HE_DATA1_SPTL_REUSE4_KNOWN);
1349 		he->data4 |= le16_encode_bits(le32_get_bits(phy_data->d2,
1350 							    IWL_RX_PHY_DATA2_HE_TB_EXT_SPTL_REUSE1),
1351 					      IEEE80211_RADIOTAP_HE_DATA4_TB_SPTL_REUSE1);
1352 		he->data4 |= le16_encode_bits(le32_get_bits(phy_data->d2,
1353 							    IWL_RX_PHY_DATA2_HE_TB_EXT_SPTL_REUSE2),
1354 					      IEEE80211_RADIOTAP_HE_DATA4_TB_SPTL_REUSE2);
1355 		he->data4 |= le16_encode_bits(le32_get_bits(phy_data->d2,
1356 							    IWL_RX_PHY_DATA2_HE_TB_EXT_SPTL_REUSE3),
1357 					      IEEE80211_RADIOTAP_HE_DATA4_TB_SPTL_REUSE3);
1358 		he->data4 |= le16_encode_bits(le32_get_bits(phy_data->d2,
1359 							    IWL_RX_PHY_DATA2_HE_TB_EXT_SPTL_REUSE4),
1360 					      IEEE80211_RADIOTAP_HE_DATA4_TB_SPTL_REUSE4);
1361 		fallthrough;
1362 	case IWL_RX_PHY_INFO_TYPE_HE_SU:
1363 	case IWL_RX_PHY_INFO_TYPE_HE_MU:
1364 	case IWL_RX_PHY_INFO_TYPE_HE_MU_EXT:
1365 	case IWL_RX_PHY_INFO_TYPE_HE_TB:
1366 		/* HE common */
1367 		he->data1 |= cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA1_LDPC_XSYMSEG_KNOWN |
1368 					 IEEE80211_RADIOTAP_HE_DATA1_DOPPLER_KNOWN |
1369 					 IEEE80211_RADIOTAP_HE_DATA1_BSS_COLOR_KNOWN);
1370 		he->data2 |= cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA2_PRE_FEC_PAD_KNOWN |
1371 					 IEEE80211_RADIOTAP_HE_DATA2_PE_DISAMBIG_KNOWN |
1372 					 IEEE80211_RADIOTAP_HE_DATA2_TXOP_KNOWN |
1373 					 IEEE80211_RADIOTAP_HE_DATA2_NUM_LTF_SYMS_KNOWN);
1374 		he->data3 |= le16_encode_bits(le32_get_bits(phy_data->d0,
1375 							    IWL_RX_PHY_DATA0_HE_BSS_COLOR_MASK),
1376 					      IEEE80211_RADIOTAP_HE_DATA3_BSS_COLOR);
1377 		if (phy_data->info_type != IWL_RX_PHY_INFO_TYPE_HE_TB &&
1378 		    phy_data->info_type != IWL_RX_PHY_INFO_TYPE_HE_TB_EXT) {
1379 			he->data1 |= cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA1_UL_DL_KNOWN);
1380 			he->data3 |= le16_encode_bits(le32_get_bits(phy_data->d0,
1381 							    IWL_RX_PHY_DATA0_HE_UPLINK),
1382 						      IEEE80211_RADIOTAP_HE_DATA3_UL_DL);
1383 		}
1384 		he->data3 |= le16_encode_bits(le32_get_bits(phy_data->d0,
1385 							    IWL_RX_PHY_DATA0_HE_LDPC_EXT_SYM),
1386 					      IEEE80211_RADIOTAP_HE_DATA3_LDPC_XSYMSEG);
1387 		he->data5 |= le16_encode_bits(le32_get_bits(phy_data->d0,
1388 							    IWL_RX_PHY_DATA0_HE_PRE_FEC_PAD_MASK),
1389 					      IEEE80211_RADIOTAP_HE_DATA5_PRE_FEC_PAD);
1390 		he->data5 |= le16_encode_bits(le32_get_bits(phy_data->d0,
1391 							    IWL_RX_PHY_DATA0_HE_PE_DISAMBIG),
1392 					      IEEE80211_RADIOTAP_HE_DATA5_PE_DISAMBIG);
1393 		he->data5 |= le16_encode_bits(le32_get_bits(phy_data->d1,
1394 							    IWL_RX_PHY_DATA1_HE_LTF_NUM_MASK),
1395 					      IEEE80211_RADIOTAP_HE_DATA5_NUM_LTF_SYMS);
1396 		he->data6 |= le16_encode_bits(le32_get_bits(phy_data->d0,
1397 							    IWL_RX_PHY_DATA0_HE_TXOP_DUR_MASK),
1398 					      IEEE80211_RADIOTAP_HE_DATA6_TXOP);
1399 		he->data6 |= le16_encode_bits(le32_get_bits(phy_data->d0,
1400 							    IWL_RX_PHY_DATA0_HE_DOPPLER),
1401 					      IEEE80211_RADIOTAP_HE_DATA6_DOPPLER);
1402 		break;
1403 	}
1404 
1405 	switch (phy_data->info_type) {
1406 	case IWL_RX_PHY_INFO_TYPE_HE_MU_EXT:
1407 	case IWL_RX_PHY_INFO_TYPE_HE_MU:
1408 	case IWL_RX_PHY_INFO_TYPE_HE_SU:
1409 		he->data1 |= cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA1_SPTL_REUSE_KNOWN);
1410 		he->data4 |= le16_encode_bits(le32_get_bits(phy_data->d0,
1411 							    IWL_RX_PHY_DATA0_HE_SPATIAL_REUSE_MASK),
1412 					      IEEE80211_RADIOTAP_HE_DATA4_SU_MU_SPTL_REUSE);
1413 		break;
1414 	default:
1415 		/* nothing here */
1416 		break;
1417 	}
1418 
1419 	switch (phy_data->info_type) {
1420 	case IWL_RX_PHY_INFO_TYPE_HE_MU_EXT:
1421 		he_mu->flags1 |=
1422 			le16_encode_bits(le16_get_bits(phy_data->d4,
1423 						       IWL_RX_PHY_DATA4_HE_MU_EXT_SIGB_DCM),
1424 					 IEEE80211_RADIOTAP_HE_MU_FLAGS1_SIG_B_DCM);
1425 		he_mu->flags1 |=
1426 			le16_encode_bits(le16_get_bits(phy_data->d4,
1427 						       IWL_RX_PHY_DATA4_HE_MU_EXT_SIGB_MCS_MASK),
1428 					 IEEE80211_RADIOTAP_HE_MU_FLAGS1_SIG_B_MCS);
1429 		he_mu->flags2 |=
1430 			le16_encode_bits(le16_get_bits(phy_data->d4,
1431 						       IWL_RX_PHY_DATA4_HE_MU_EXT_PREAMBLE_PUNC_TYPE_MASK),
1432 					 IEEE80211_RADIOTAP_HE_MU_FLAGS2_PUNC_FROM_SIG_A_BW);
1433 		iwl_mvm_decode_he_mu_ext(mvm, phy_data, rate_n_flags, he_mu);
1434 		fallthrough;
1435 	case IWL_RX_PHY_INFO_TYPE_HE_MU:
1436 		he_mu->flags2 |=
1437 			le16_encode_bits(le32_get_bits(phy_data->d1,
1438 						       IWL_RX_PHY_DATA1_HE_MU_SIBG_SYM_OR_USER_NUM_MASK),
1439 					 IEEE80211_RADIOTAP_HE_MU_FLAGS2_SIG_B_SYMS_USERS);
1440 		he_mu->flags2 |=
1441 			le16_encode_bits(le32_get_bits(phy_data->d1,
1442 						       IWL_RX_PHY_DATA1_HE_MU_SIGB_COMPRESSION),
1443 					 IEEE80211_RADIOTAP_HE_MU_FLAGS2_SIG_B_COMP);
1444 		fallthrough;
1445 	case IWL_RX_PHY_INFO_TYPE_HE_TB:
1446 	case IWL_RX_PHY_INFO_TYPE_HE_TB_EXT:
1447 		iwl_mvm_decode_he_phy_ru_alloc(phy_data, rate_n_flags,
1448 					       he, he_mu, rx_status);
1449 		break;
1450 	case IWL_RX_PHY_INFO_TYPE_HE_SU:
1451 		he->data1 |= cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA1_BEAM_CHANGE_KNOWN);
1452 		he->data3 |= le16_encode_bits(le32_get_bits(phy_data->d0,
1453 							    IWL_RX_PHY_DATA0_HE_BEAM_CHNG),
1454 					      IEEE80211_RADIOTAP_HE_DATA3_BEAM_CHANGE);
1455 		break;
1456 	default:
1457 		/* nothing */
1458 		break;
1459 	}
1460 }
1461 
1462 static void iwl_mvm_rx_he(struct iwl_mvm *mvm, struct sk_buff *skb,
1463 			  struct iwl_mvm_rx_phy_data *phy_data,
1464 			  u32 rate_n_flags, u16 phy_info, int queue)
1465 {
1466 	struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
1467 	struct ieee80211_radiotap_he *he = NULL;
1468 	struct ieee80211_radiotap_he_mu *he_mu = NULL;
1469 	u32 he_type = rate_n_flags & RATE_MCS_HE_TYPE_MSK;
1470 	u8 stbc, ltf;
1471 	static const struct ieee80211_radiotap_he known = {
1472 		.data1 = cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA1_DATA_MCS_KNOWN |
1473 				     IEEE80211_RADIOTAP_HE_DATA1_DATA_DCM_KNOWN |
1474 				     IEEE80211_RADIOTAP_HE_DATA1_STBC_KNOWN |
1475 				     IEEE80211_RADIOTAP_HE_DATA1_CODING_KNOWN),
1476 		.data2 = cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA2_GI_KNOWN |
1477 				     IEEE80211_RADIOTAP_HE_DATA2_TXBF_KNOWN),
1478 	};
1479 	static const struct ieee80211_radiotap_he_mu mu_known = {
1480 		.flags1 = cpu_to_le16(IEEE80211_RADIOTAP_HE_MU_FLAGS1_SIG_B_MCS_KNOWN |
1481 				      IEEE80211_RADIOTAP_HE_MU_FLAGS1_SIG_B_DCM_KNOWN |
1482 				      IEEE80211_RADIOTAP_HE_MU_FLAGS1_SIG_B_SYMS_USERS_KNOWN |
1483 				      IEEE80211_RADIOTAP_HE_MU_FLAGS1_SIG_B_COMP_KNOWN),
1484 		.flags2 = cpu_to_le16(IEEE80211_RADIOTAP_HE_MU_FLAGS2_PUNC_FROM_SIG_A_BW_KNOWN |
1485 				      IEEE80211_RADIOTAP_HE_MU_FLAGS2_BW_FROM_SIG_A_BW_KNOWN),
1486 	};
1487 
1488 	he = skb_put_data(skb, &known, sizeof(known));
1489 	rx_status->flag |= RX_FLAG_RADIOTAP_HE;
1490 
1491 	if (phy_data->info_type == IWL_RX_PHY_INFO_TYPE_HE_MU ||
1492 	    phy_data->info_type == IWL_RX_PHY_INFO_TYPE_HE_MU_EXT) {
1493 		he_mu = skb_put_data(skb, &mu_known, sizeof(mu_known));
1494 		rx_status->flag |= RX_FLAG_RADIOTAP_HE_MU;
1495 	}
1496 
1497 	/* report the AMPDU-EOF bit on single frames */
1498 	if (!queue && !(phy_info & IWL_RX_MPDU_PHY_AMPDU)) {
1499 		rx_status->flag |= RX_FLAG_AMPDU_DETAILS;
1500 		rx_status->flag |= RX_FLAG_AMPDU_EOF_BIT_KNOWN;
1501 		if (phy_data->d0 & cpu_to_le32(IWL_RX_PHY_DATA0_HE_DELIM_EOF))
1502 			rx_status->flag |= RX_FLAG_AMPDU_EOF_BIT;
1503 	}
1504 
1505 	if (phy_info & IWL_RX_MPDU_PHY_TSF_OVERLOAD)
1506 		iwl_mvm_decode_he_phy_data(mvm, phy_data, he, he_mu, rx_status,
1507 					   rate_n_flags, queue);
1508 
1509 	/* update aggregation data for monitor sake on default queue */
1510 	if (!queue && (phy_info & IWL_RX_MPDU_PHY_TSF_OVERLOAD) &&
1511 	    (phy_info & IWL_RX_MPDU_PHY_AMPDU)) {
1512 		bool toggle_bit = phy_info & IWL_RX_MPDU_PHY_AMPDU_TOGGLE;
1513 
1514 		/* toggle is switched whenever new aggregation starts */
1515 		if (toggle_bit != mvm->ampdu_toggle) {
1516 			rx_status->flag |= RX_FLAG_AMPDU_EOF_BIT_KNOWN;
1517 			if (phy_data->d0 & cpu_to_le32(IWL_RX_PHY_DATA0_HE_DELIM_EOF))
1518 				rx_status->flag |= RX_FLAG_AMPDU_EOF_BIT;
1519 		}
1520 	}
1521 
1522 	if (he_type == RATE_MCS_HE_TYPE_EXT_SU &&
1523 	    rate_n_flags & RATE_MCS_HE_106T_MSK) {
1524 		rx_status->bw = RATE_INFO_BW_HE_RU;
1525 		rx_status->he_ru = NL80211_RATE_INFO_HE_RU_ALLOC_106;
1526 	}
1527 
1528 	/* actually data is filled in mac80211 */
1529 	if (he_type == RATE_MCS_HE_TYPE_SU ||
1530 	    he_type == RATE_MCS_HE_TYPE_EXT_SU)
1531 		he->data1 |=
1532 			cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA1_BW_RU_ALLOC_KNOWN);
1533 
1534 	stbc = (rate_n_flags & RATE_MCS_STBC_MSK) >> RATE_MCS_STBC_POS;
1535 	rx_status->nss =
1536 		((rate_n_flags & RATE_MCS_NSS_MSK) >>
1537 		 RATE_MCS_NSS_POS) + 1;
1538 	rx_status->rate_idx = rate_n_flags & RATE_MCS_CODE_MSK;
1539 	rx_status->encoding = RX_ENC_HE;
1540 	rx_status->enc_flags |= stbc << RX_ENC_FLAG_STBC_SHIFT;
1541 	if (rate_n_flags & RATE_MCS_BF_MSK)
1542 		rx_status->enc_flags |= RX_ENC_FLAG_BF;
1543 
1544 	rx_status->he_dcm =
1545 		!!(rate_n_flags & RATE_HE_DUAL_CARRIER_MODE_MSK);
1546 
1547 #define CHECK_TYPE(F)							\
1548 	BUILD_BUG_ON(IEEE80211_RADIOTAP_HE_DATA1_FORMAT_ ## F !=	\
1549 		     (RATE_MCS_HE_TYPE_ ## F >> RATE_MCS_HE_TYPE_POS))
1550 
1551 	CHECK_TYPE(SU);
1552 	CHECK_TYPE(EXT_SU);
1553 	CHECK_TYPE(MU);
1554 	CHECK_TYPE(TRIG);
1555 
1556 	he->data1 |= cpu_to_le16(he_type >> RATE_MCS_HE_TYPE_POS);
1557 
1558 	if (rate_n_flags & RATE_MCS_BF_MSK)
1559 		he->data5 |= cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA5_TXBF);
1560 
1561 	switch ((rate_n_flags & RATE_MCS_HE_GI_LTF_MSK) >>
1562 		RATE_MCS_HE_GI_LTF_POS) {
1563 	case 0:
1564 		if (he_type == RATE_MCS_HE_TYPE_TRIG)
1565 			rx_status->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
1566 		else
1567 			rx_status->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
1568 		if (he_type == RATE_MCS_HE_TYPE_MU)
1569 			ltf = IEEE80211_RADIOTAP_HE_DATA5_LTF_SIZE_4X;
1570 		else
1571 			ltf = IEEE80211_RADIOTAP_HE_DATA5_LTF_SIZE_1X;
1572 		break;
1573 	case 1:
1574 		if (he_type == RATE_MCS_HE_TYPE_TRIG)
1575 			rx_status->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
1576 		else
1577 			rx_status->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
1578 		ltf = IEEE80211_RADIOTAP_HE_DATA5_LTF_SIZE_2X;
1579 		break;
1580 	case 2:
1581 		if (he_type == RATE_MCS_HE_TYPE_TRIG) {
1582 			rx_status->he_gi = NL80211_RATE_INFO_HE_GI_3_2;
1583 			ltf = IEEE80211_RADIOTAP_HE_DATA5_LTF_SIZE_4X;
1584 		} else {
1585 			rx_status->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
1586 			ltf = IEEE80211_RADIOTAP_HE_DATA5_LTF_SIZE_2X;
1587 		}
1588 		break;
1589 	case 3:
1590 		rx_status->he_gi = NL80211_RATE_INFO_HE_GI_3_2;
1591 		ltf = IEEE80211_RADIOTAP_HE_DATA5_LTF_SIZE_4X;
1592 		break;
1593 	case 4:
1594 		rx_status->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
1595 		ltf = IEEE80211_RADIOTAP_HE_DATA5_LTF_SIZE_4X;
1596 		break;
1597 	default:
1598 		ltf = IEEE80211_RADIOTAP_HE_DATA5_LTF_SIZE_UNKNOWN;
1599 	}
1600 
1601 	he->data5 |= le16_encode_bits(ltf,
1602 				      IEEE80211_RADIOTAP_HE_DATA5_LTF_SIZE);
1603 }
1604 
1605 static void iwl_mvm_decode_lsig(struct sk_buff *skb,
1606 				struct iwl_mvm_rx_phy_data *phy_data)
1607 {
1608 	struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
1609 	struct ieee80211_radiotap_lsig *lsig;
1610 
1611 	switch (phy_data->info_type) {
1612 	case IWL_RX_PHY_INFO_TYPE_HT:
1613 	case IWL_RX_PHY_INFO_TYPE_VHT_SU:
1614 	case IWL_RX_PHY_INFO_TYPE_VHT_MU:
1615 	case IWL_RX_PHY_INFO_TYPE_HE_TB_EXT:
1616 	case IWL_RX_PHY_INFO_TYPE_HE_SU:
1617 	case IWL_RX_PHY_INFO_TYPE_HE_MU:
1618 	case IWL_RX_PHY_INFO_TYPE_HE_MU_EXT:
1619 	case IWL_RX_PHY_INFO_TYPE_HE_TB:
1620 		lsig = skb_put(skb, sizeof(*lsig));
1621 		lsig->data1 = cpu_to_le16(IEEE80211_RADIOTAP_LSIG_DATA1_LENGTH_KNOWN);
1622 		lsig->data2 = le16_encode_bits(le32_get_bits(phy_data->d1,
1623 							     IWL_RX_PHY_DATA1_LSIG_LEN_MASK),
1624 					       IEEE80211_RADIOTAP_LSIG_DATA2_LENGTH);
1625 		rx_status->flag |= RX_FLAG_RADIOTAP_LSIG;
1626 		break;
1627 	default:
1628 		break;
1629 	}
1630 }
1631 
1632 static inline u8 iwl_mvm_nl80211_band_from_rx_msdu(u8 phy_band)
1633 {
1634 	switch (phy_band) {
1635 	case PHY_BAND_24:
1636 		return NL80211_BAND_2GHZ;
1637 	case PHY_BAND_5:
1638 		return NL80211_BAND_5GHZ;
1639 	case PHY_BAND_6:
1640 		return NL80211_BAND_6GHZ;
1641 	default:
1642 		WARN_ONCE(1, "Unsupported phy band (%u)\n", phy_band);
1643 		return NL80211_BAND_5GHZ;
1644 	}
1645 }
1646 
1647 struct iwl_rx_sta_csa {
1648 	bool all_sta_unblocked;
1649 	struct ieee80211_vif *vif;
1650 };
1651 
1652 static void iwl_mvm_rx_get_sta_block_tx(void *data, struct ieee80211_sta *sta)
1653 {
1654 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1655 	struct iwl_rx_sta_csa *rx_sta_csa = data;
1656 
1657 	if (mvmsta->vif != rx_sta_csa->vif)
1658 		return;
1659 
1660 	if (mvmsta->disable_tx)
1661 		rx_sta_csa->all_sta_unblocked = false;
1662 }
1663 
1664 void iwl_mvm_rx_mpdu_mq(struct iwl_mvm *mvm, struct napi_struct *napi,
1665 			struct iwl_rx_cmd_buffer *rxb, int queue)
1666 {
1667 	struct ieee80211_rx_status *rx_status;
1668 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1669 	struct iwl_rx_mpdu_desc *desc = (void *)pkt->data;
1670 	struct ieee80211_hdr *hdr;
1671 	u32 len;
1672 	u32 pkt_len = iwl_rx_packet_payload_len(pkt);
1673 	u32 rate_n_flags, gp2_on_air_rise;
1674 	u16 phy_info;
1675 	struct ieee80211_sta *sta = NULL;
1676 	struct sk_buff *skb;
1677 	u8 crypt_len = 0, channel, energy_a, energy_b;
1678 	size_t desc_size;
1679 	struct iwl_mvm_rx_phy_data phy_data = {
1680 		.info_type = IWL_RX_PHY_INFO_TYPE_NONE,
1681 	};
1682 	u32 format;
1683 	bool is_sgi;
1684 
1685 	if (unlikely(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)))
1686 		return;
1687 
1688 	if (mvm->trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_AX210)
1689 		desc_size = sizeof(*desc);
1690 	else
1691 		desc_size = IWL_RX_DESC_SIZE_V1;
1692 
1693 	if (unlikely(pkt_len < desc_size)) {
1694 		IWL_DEBUG_DROP(mvm, "Bad REPLY_RX_MPDU_CMD size\n");
1695 		return;
1696 	}
1697 
1698 	if (mvm->trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_AX210) {
1699 		rate_n_flags = le32_to_cpu(desc->v3.rate_n_flags);
1700 		channel = desc->v3.channel;
1701 		gp2_on_air_rise = le32_to_cpu(desc->v3.gp2_on_air_rise);
1702 		energy_a = desc->v3.energy_a;
1703 		energy_b = desc->v3.energy_b;
1704 
1705 		phy_data.d0 = desc->v3.phy_data0;
1706 		phy_data.d1 = desc->v3.phy_data1;
1707 		phy_data.d2 = desc->v3.phy_data2;
1708 		phy_data.d3 = desc->v3.phy_data3;
1709 	} else {
1710 		rate_n_flags = le32_to_cpu(desc->v1.rate_n_flags);
1711 		channel = desc->v1.channel;
1712 		gp2_on_air_rise = le32_to_cpu(desc->v1.gp2_on_air_rise);
1713 		energy_a = desc->v1.energy_a;
1714 		energy_b = desc->v1.energy_b;
1715 
1716 		phy_data.d0 = desc->v1.phy_data0;
1717 		phy_data.d1 = desc->v1.phy_data1;
1718 		phy_data.d2 = desc->v1.phy_data2;
1719 		phy_data.d3 = desc->v1.phy_data3;
1720 	}
1721 	if (iwl_fw_lookup_notif_ver(mvm->fw, LEGACY_GROUP,
1722 				    REPLY_RX_MPDU_CMD, 0) < 4) {
1723 		rate_n_flags = iwl_new_rate_from_v1(rate_n_flags);
1724 		IWL_DEBUG_DROP(mvm, "Got old format rate, converting. New rate: 0x%x\n",
1725 			       rate_n_flags);
1726 	}
1727 	format = rate_n_flags & RATE_MCS_MOD_TYPE_MSK;
1728 
1729 	len = le16_to_cpu(desc->mpdu_len);
1730 
1731 	if (unlikely(len + desc_size > pkt_len)) {
1732 		IWL_DEBUG_DROP(mvm, "FW lied about packet len\n");
1733 		return;
1734 	}
1735 
1736 	phy_info = le16_to_cpu(desc->phy_info);
1737 	phy_data.d4 = desc->phy_data4;
1738 
1739 	if (phy_info & IWL_RX_MPDU_PHY_TSF_OVERLOAD)
1740 		phy_data.info_type =
1741 			le32_get_bits(phy_data.d1,
1742 				      IWL_RX_PHY_DATA1_INFO_TYPE_MASK);
1743 
1744 	hdr = (void *)(pkt->data + desc_size);
1745 	/* Dont use dev_alloc_skb(), we'll have enough headroom once
1746 	 * ieee80211_hdr pulled.
1747 	 */
1748 	skb = alloc_skb(128, GFP_ATOMIC);
1749 	if (!skb) {
1750 		IWL_ERR(mvm, "alloc_skb failed\n");
1751 		return;
1752 	}
1753 
1754 	if (desc->mac_flags2 & IWL_RX_MPDU_MFLG2_PAD) {
1755 		/*
1756 		 * If the device inserted padding it means that (it thought)
1757 		 * the 802.11 header wasn't a multiple of 4 bytes long. In
1758 		 * this case, reserve two bytes at the start of the SKB to
1759 		 * align the payload properly in case we end up copying it.
1760 		 */
1761 		skb_reserve(skb, 2);
1762 	}
1763 
1764 	rx_status = IEEE80211_SKB_RXCB(skb);
1765 
1766 	/* This may be overridden by iwl_mvm_rx_he() to HE_RU */
1767 	switch (rate_n_flags & RATE_MCS_CHAN_WIDTH_MSK) {
1768 	case RATE_MCS_CHAN_WIDTH_20:
1769 		break;
1770 	case RATE_MCS_CHAN_WIDTH_40:
1771 		rx_status->bw = RATE_INFO_BW_40;
1772 		break;
1773 	case RATE_MCS_CHAN_WIDTH_80:
1774 		rx_status->bw = RATE_INFO_BW_80;
1775 		break;
1776 	case RATE_MCS_CHAN_WIDTH_160:
1777 		rx_status->bw = RATE_INFO_BW_160;
1778 		break;
1779 	}
1780 
1781 	if (format == RATE_MCS_HE_MSK)
1782 		iwl_mvm_rx_he(mvm, skb, &phy_data, rate_n_flags,
1783 			      phy_info, queue);
1784 
1785 	iwl_mvm_decode_lsig(skb, &phy_data);
1786 
1787 	/*
1788 	 * Keep packets with CRC errors (and with overrun) for monitor mode
1789 	 * (otherwise the firmware discards them) but mark them as bad.
1790 	 */
1791 	if (!(desc->status & cpu_to_le32(IWL_RX_MPDU_STATUS_CRC_OK)) ||
1792 	    !(desc->status & cpu_to_le32(IWL_RX_MPDU_STATUS_OVERRUN_OK))) {
1793 		IWL_DEBUG_RX(mvm, "Bad CRC or FIFO: 0x%08X.\n",
1794 			     le32_to_cpu(desc->status));
1795 		rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
1796 	}
1797 	/* set the preamble flag if appropriate */
1798 	if (format == RATE_MCS_CCK_MSK &&
1799 	    phy_info & IWL_RX_MPDU_PHY_SHORT_PREAMBLE)
1800 		rx_status->enc_flags |= RX_ENC_FLAG_SHORTPRE;
1801 
1802 	if (likely(!(phy_info & IWL_RX_MPDU_PHY_TSF_OVERLOAD))) {
1803 		u64 tsf_on_air_rise;
1804 
1805 		if (mvm->trans->trans_cfg->device_family >=
1806 		    IWL_DEVICE_FAMILY_AX210)
1807 			tsf_on_air_rise = le64_to_cpu(desc->v3.tsf_on_air_rise);
1808 		else
1809 			tsf_on_air_rise = le64_to_cpu(desc->v1.tsf_on_air_rise);
1810 
1811 		rx_status->mactime = tsf_on_air_rise;
1812 		/* TSF as indicated by the firmware is at INA time */
1813 		rx_status->flag |= RX_FLAG_MACTIME_PLCP_START;
1814 	}
1815 
1816 	rx_status->device_timestamp = gp2_on_air_rise;
1817 	if (iwl_mvm_is_band_in_rx_supported(mvm)) {
1818 		u8 band = BAND_IN_RX_STATUS(desc->mac_phy_idx);
1819 
1820 		rx_status->band = iwl_mvm_nl80211_band_from_rx_msdu(band);
1821 	} else {
1822 		rx_status->band = channel > 14 ? NL80211_BAND_5GHZ :
1823 			NL80211_BAND_2GHZ;
1824 	}
1825 	rx_status->freq = ieee80211_channel_to_frequency(channel,
1826 							 rx_status->band);
1827 	iwl_mvm_get_signal_strength(mvm, rx_status, rate_n_flags, energy_a,
1828 				    energy_b);
1829 
1830 	/* update aggregation data for monitor sake on default queue */
1831 	if (!queue && (phy_info & IWL_RX_MPDU_PHY_AMPDU)) {
1832 		bool toggle_bit = phy_info & IWL_RX_MPDU_PHY_AMPDU_TOGGLE;
1833 
1834 		rx_status->flag |= RX_FLAG_AMPDU_DETAILS;
1835 		/*
1836 		 * Toggle is switched whenever new aggregation starts. Make
1837 		 * sure ampdu_reference is never 0 so we can later use it to
1838 		 * see if the frame was really part of an A-MPDU or not.
1839 		 */
1840 		if (toggle_bit != mvm->ampdu_toggle) {
1841 			mvm->ampdu_ref++;
1842 			if (mvm->ampdu_ref == 0)
1843 				mvm->ampdu_ref++;
1844 			mvm->ampdu_toggle = toggle_bit;
1845 		}
1846 		rx_status->ampdu_reference = mvm->ampdu_ref;
1847 	}
1848 
1849 	if (unlikely(mvm->monitor_on))
1850 		iwl_mvm_add_rtap_sniffer_config(mvm, skb);
1851 
1852 	rcu_read_lock();
1853 
1854 	if (desc->status & cpu_to_le32(IWL_RX_MPDU_STATUS_SRC_STA_FOUND)) {
1855 		u8 id = le32_get_bits(desc->status, IWL_RX_MPDU_STATUS_STA_ID);
1856 
1857 		if (!WARN_ON_ONCE(id >= mvm->fw->ucode_capa.num_stations)) {
1858 			sta = rcu_dereference(mvm->fw_id_to_mac_id[id]);
1859 			if (IS_ERR(sta))
1860 				sta = NULL;
1861 		}
1862 	} else if (!is_multicast_ether_addr(hdr->addr2)) {
1863 		/*
1864 		 * This is fine since we prevent two stations with the same
1865 		 * address from being added.
1866 		 */
1867 		sta = ieee80211_find_sta_by_ifaddr(mvm->hw, hdr->addr2, NULL);
1868 	}
1869 
1870 	if (iwl_mvm_rx_crypto(mvm, sta, hdr, rx_status, phy_info, desc,
1871 			      le32_to_cpu(pkt->len_n_flags), queue,
1872 			      &crypt_len)) {
1873 		kfree_skb(skb);
1874 		goto out;
1875 	}
1876 
1877 	if (sta) {
1878 		struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1879 		struct ieee80211_vif *tx_blocked_vif =
1880 			rcu_dereference(mvm->csa_tx_blocked_vif);
1881 		u8 baid = (u8)((le32_to_cpu(desc->reorder_data) &
1882 			       IWL_RX_MPDU_REORDER_BAID_MASK) >>
1883 			       IWL_RX_MPDU_REORDER_BAID_SHIFT);
1884 		struct iwl_fw_dbg_trigger_tlv *trig;
1885 		struct ieee80211_vif *vif = mvmsta->vif;
1886 
1887 		if (!mvm->tcm.paused && len >= sizeof(*hdr) &&
1888 		    !is_multicast_ether_addr(hdr->addr1) &&
1889 		    ieee80211_is_data(hdr->frame_control) &&
1890 		    time_after(jiffies, mvm->tcm.ts + MVM_TCM_PERIOD))
1891 			schedule_delayed_work(&mvm->tcm.work, 0);
1892 
1893 		/*
1894 		 * We have tx blocked stations (with CS bit). If we heard
1895 		 * frames from a blocked station on a new channel we can
1896 		 * TX to it again.
1897 		 */
1898 		if (unlikely(tx_blocked_vif) && tx_blocked_vif == vif) {
1899 			struct iwl_mvm_vif *mvmvif =
1900 				iwl_mvm_vif_from_mac80211(tx_blocked_vif);
1901 			struct iwl_rx_sta_csa rx_sta_csa = {
1902 				.all_sta_unblocked = true,
1903 				.vif = tx_blocked_vif,
1904 			};
1905 
1906 			if (mvmvif->csa_target_freq == rx_status->freq)
1907 				iwl_mvm_sta_modify_disable_tx_ap(mvm, sta,
1908 								 false);
1909 			ieee80211_iterate_stations_atomic(mvm->hw,
1910 							  iwl_mvm_rx_get_sta_block_tx,
1911 							  &rx_sta_csa);
1912 
1913 			if (rx_sta_csa.all_sta_unblocked) {
1914 				RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);
1915 				/* Unblock BCAST / MCAST station */
1916 				iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, false);
1917 				cancel_delayed_work_sync(&mvm->cs_tx_unblock_dwork);
1918 			}
1919 		}
1920 
1921 		rs_update_last_rssi(mvm, mvmsta, rx_status);
1922 
1923 		trig = iwl_fw_dbg_trigger_on(&mvm->fwrt,
1924 					     ieee80211_vif_to_wdev(vif),
1925 					     FW_DBG_TRIGGER_RSSI);
1926 
1927 		if (trig && ieee80211_is_beacon(hdr->frame_control)) {
1928 			struct iwl_fw_dbg_trigger_low_rssi *rssi_trig;
1929 			s32 rssi;
1930 
1931 			rssi_trig = (void *)trig->data;
1932 			rssi = le32_to_cpu(rssi_trig->rssi);
1933 
1934 			if (rx_status->signal < rssi)
1935 				iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
1936 							NULL);
1937 		}
1938 
1939 		if (ieee80211_is_data(hdr->frame_control))
1940 			iwl_mvm_rx_csum(mvm, sta, skb, pkt);
1941 
1942 		if (iwl_mvm_is_dup(sta, queue, rx_status, hdr, desc)) {
1943 			kfree_skb(skb);
1944 			goto out;
1945 		}
1946 
1947 		/*
1948 		 * Our hardware de-aggregates AMSDUs but copies the mac header
1949 		 * as it to the de-aggregated MPDUs. We need to turn off the
1950 		 * AMSDU bit in the QoS control ourselves.
1951 		 * In addition, HW reverses addr3 and addr4 - reverse it back.
1952 		 */
1953 		if ((desc->mac_flags2 & IWL_RX_MPDU_MFLG2_AMSDU) &&
1954 		    !WARN_ON(!ieee80211_is_data_qos(hdr->frame_control))) {
1955 			u8 *qc = ieee80211_get_qos_ctl(hdr);
1956 
1957 			*qc &= ~IEEE80211_QOS_CTL_A_MSDU_PRESENT;
1958 
1959 			if (mvm->trans->trans_cfg->device_family ==
1960 			    IWL_DEVICE_FAMILY_9000) {
1961 				iwl_mvm_flip_address(hdr->addr3);
1962 
1963 				if (ieee80211_has_a4(hdr->frame_control))
1964 					iwl_mvm_flip_address(hdr->addr4);
1965 			}
1966 		}
1967 		if (baid != IWL_RX_REORDER_DATA_INVALID_BAID) {
1968 			u32 reorder_data = le32_to_cpu(desc->reorder_data);
1969 
1970 			iwl_mvm_agg_rx_received(mvm, reorder_data, baid);
1971 		}
1972 	}
1973 
1974 	is_sgi = format == RATE_MCS_HE_MSK ?
1975 		iwl_he_is_sgi(rate_n_flags) :
1976 		rate_n_flags & RATE_MCS_SGI_MSK;
1977 
1978 	if (!(format == RATE_MCS_CCK_MSK) && is_sgi)
1979 		rx_status->enc_flags |= RX_ENC_FLAG_SHORT_GI;
1980 	if (rate_n_flags & RATE_MCS_LDPC_MSK)
1981 		rx_status->enc_flags |= RX_ENC_FLAG_LDPC;
1982 	if (format == RATE_MCS_HT_MSK) {
1983 		u8 stbc = (rate_n_flags & RATE_MCS_STBC_MSK) >>
1984 			RATE_MCS_STBC_POS;
1985 		rx_status->encoding = RX_ENC_HT;
1986 		rx_status->rate_idx = RATE_HT_MCS_INDEX(rate_n_flags);
1987 		rx_status->enc_flags |= stbc << RX_ENC_FLAG_STBC_SHIFT;
1988 	} else if (format == RATE_MCS_VHT_MSK) {
1989 		u8 stbc = (rate_n_flags & RATE_MCS_STBC_MSK) >>
1990 			RATE_MCS_STBC_POS;
1991 		rx_status->nss = ((rate_n_flags & RATE_MCS_NSS_MSK) >>
1992 			RATE_MCS_NSS_POS) + 1;
1993 		rx_status->rate_idx = rate_n_flags & RATE_MCS_CODE_MSK;
1994 		rx_status->encoding = RX_ENC_VHT;
1995 		rx_status->enc_flags |= stbc << RX_ENC_FLAG_STBC_SHIFT;
1996 		if (rate_n_flags & RATE_MCS_BF_MSK)
1997 			rx_status->enc_flags |= RX_ENC_FLAG_BF;
1998 	} else if (!(format == RATE_MCS_HE_MSK)) {
1999 		int rate = iwl_mvm_legacy_hw_idx_to_mac80211_idx(rate_n_flags,
2000 								 rx_status->band);
2001 
2002 		if (WARN(rate < 0 || rate > 0xFF,
2003 			 "Invalid rate flags 0x%x, band %d,\n",
2004 			 rate_n_flags, rx_status->band)) {
2005 			kfree_skb(skb);
2006 			goto out;
2007 		}
2008 		rx_status->rate_idx = rate;
2009 	}
2010 
2011 	/* management stuff on default queue */
2012 	if (!queue) {
2013 		if (unlikely((ieee80211_is_beacon(hdr->frame_control) ||
2014 			      ieee80211_is_probe_resp(hdr->frame_control)) &&
2015 			     mvm->sched_scan_pass_all ==
2016 			     SCHED_SCAN_PASS_ALL_ENABLED))
2017 			mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_FOUND;
2018 
2019 		if (unlikely(ieee80211_is_beacon(hdr->frame_control) ||
2020 			     ieee80211_is_probe_resp(hdr->frame_control)))
2021 			rx_status->boottime_ns = ktime_get_boottime_ns();
2022 	}
2023 
2024 	if (iwl_mvm_create_skb(mvm, skb, hdr, len, crypt_len, rxb)) {
2025 		kfree_skb(skb);
2026 		goto out;
2027 	}
2028 
2029 	if (!iwl_mvm_reorder(mvm, napi, queue, sta, skb, desc))
2030 		iwl_mvm_pass_packet_to_mac80211(mvm, napi, skb, queue,
2031 						sta);
2032 out:
2033 	rcu_read_unlock();
2034 }
2035 
2036 void iwl_mvm_rx_monitor_no_data(struct iwl_mvm *mvm, struct napi_struct *napi,
2037 				struct iwl_rx_cmd_buffer *rxb, int queue)
2038 {
2039 	struct ieee80211_rx_status *rx_status;
2040 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
2041 	struct iwl_rx_no_data *desc = (void *)pkt->data;
2042 	u32 rate_n_flags = le32_to_cpu(desc->rate);
2043 	u32 gp2_on_air_rise = le32_to_cpu(desc->on_air_rise_time);
2044 	u32 rssi = le32_to_cpu(desc->rssi);
2045 	u32 info_type = le32_to_cpu(desc->info) & RX_NO_DATA_INFO_TYPE_MSK;
2046 	u16 phy_info = IWL_RX_MPDU_PHY_TSF_OVERLOAD;
2047 	struct ieee80211_sta *sta = NULL;
2048 	struct sk_buff *skb;
2049 	u8 channel, energy_a, energy_b;
2050 	u32 format;
2051 	struct iwl_mvm_rx_phy_data phy_data = {
2052 		.info_type = le32_get_bits(desc->phy_info[1],
2053 					   IWL_RX_PHY_DATA1_INFO_TYPE_MASK),
2054 		.d0 = desc->phy_info[0],
2055 		.d1 = desc->phy_info[1],
2056 	};
2057 	bool is_sgi;
2058 
2059 	if (iwl_fw_lookup_notif_ver(mvm->fw, DATA_PATH_GROUP,
2060 				    RX_NO_DATA_NOTIF, 0) < 2) {
2061 		IWL_DEBUG_DROP(mvm, "Got an old rate format. Old rate: 0x%x\n",
2062 			       rate_n_flags);
2063 		rate_n_flags = iwl_new_rate_from_v1(rate_n_flags);
2064 		IWL_DEBUG_DROP(mvm, " Rate after conversion to the new format: 0x%x\n",
2065 			       rate_n_flags);
2066 	}
2067 	format = rate_n_flags & RATE_MCS_MOD_TYPE_MSK;
2068 
2069 	if (unlikely(iwl_rx_packet_payload_len(pkt) < sizeof(*desc)))
2070 		return;
2071 
2072 	if (unlikely(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)))
2073 		return;
2074 
2075 	energy_a = (rssi & RX_NO_DATA_CHAIN_A_MSK) >> RX_NO_DATA_CHAIN_A_POS;
2076 	energy_b = (rssi & RX_NO_DATA_CHAIN_B_MSK) >> RX_NO_DATA_CHAIN_B_POS;
2077 	channel = (rssi & RX_NO_DATA_CHANNEL_MSK) >> RX_NO_DATA_CHANNEL_POS;
2078 
2079 	/* Dont use dev_alloc_skb(), we'll have enough headroom once
2080 	 * ieee80211_hdr pulled.
2081 	 */
2082 	skb = alloc_skb(128, GFP_ATOMIC);
2083 	if (!skb) {
2084 		IWL_ERR(mvm, "alloc_skb failed\n");
2085 		return;
2086 	}
2087 
2088 	rx_status = IEEE80211_SKB_RXCB(skb);
2089 
2090 	/* 0-length PSDU */
2091 	rx_status->flag |= RX_FLAG_NO_PSDU;
2092 
2093 	switch (info_type) {
2094 	case RX_NO_DATA_INFO_TYPE_NDP:
2095 		rx_status->zero_length_psdu_type =
2096 			IEEE80211_RADIOTAP_ZERO_LEN_PSDU_SOUNDING;
2097 		break;
2098 	case RX_NO_DATA_INFO_TYPE_MU_UNMATCHED:
2099 	case RX_NO_DATA_INFO_TYPE_HE_TB_UNMATCHED:
2100 		rx_status->zero_length_psdu_type =
2101 			IEEE80211_RADIOTAP_ZERO_LEN_PSDU_NOT_CAPTURED;
2102 		break;
2103 	default:
2104 		rx_status->zero_length_psdu_type =
2105 			IEEE80211_RADIOTAP_ZERO_LEN_PSDU_VENDOR;
2106 		break;
2107 	}
2108 
2109 	/* This may be overridden by iwl_mvm_rx_he() to HE_RU */
2110 	switch (rate_n_flags & RATE_MCS_CHAN_WIDTH_MSK) {
2111 	case RATE_MCS_CHAN_WIDTH_20:
2112 		break;
2113 	case RATE_MCS_CHAN_WIDTH_40:
2114 		rx_status->bw = RATE_INFO_BW_40;
2115 		break;
2116 	case RATE_MCS_CHAN_WIDTH_80:
2117 		rx_status->bw = RATE_INFO_BW_80;
2118 		break;
2119 	case RATE_MCS_CHAN_WIDTH_160:
2120 		rx_status->bw = RATE_INFO_BW_160;
2121 		break;
2122 	}
2123 
2124 	if (format == RATE_MCS_HE_MSK)
2125 		iwl_mvm_rx_he(mvm, skb, &phy_data, rate_n_flags,
2126 			      phy_info, queue);
2127 
2128 	iwl_mvm_decode_lsig(skb, &phy_data);
2129 
2130 	rx_status->device_timestamp = gp2_on_air_rise;
2131 	rx_status->band = channel > 14 ? NL80211_BAND_5GHZ :
2132 		NL80211_BAND_2GHZ;
2133 	rx_status->freq = ieee80211_channel_to_frequency(channel,
2134 							 rx_status->band);
2135 	iwl_mvm_get_signal_strength(mvm, rx_status, rate_n_flags, energy_a,
2136 				    energy_b);
2137 
2138 	rcu_read_lock();
2139 
2140 	is_sgi = format == RATE_MCS_HE_MSK ?
2141 		iwl_he_is_sgi(rate_n_flags) :
2142 		rate_n_flags & RATE_MCS_SGI_MSK;
2143 
2144 	if (!(format == RATE_MCS_CCK_MSK) && is_sgi)
2145 		rx_status->enc_flags |= RX_ENC_FLAG_SHORT_GI;
2146 	if (rate_n_flags & RATE_MCS_LDPC_MSK)
2147 		rx_status->enc_flags |= RX_ENC_FLAG_LDPC;
2148 	if (format == RATE_MCS_HT_MSK) {
2149 		u8 stbc = (rate_n_flags & RATE_MCS_STBC_MSK) >>
2150 				RATE_MCS_STBC_POS;
2151 		rx_status->encoding = RX_ENC_HT;
2152 		rx_status->rate_idx = RATE_HT_MCS_INDEX(rate_n_flags);
2153 		rx_status->enc_flags |= stbc << RX_ENC_FLAG_STBC_SHIFT;
2154 	} else if (format == RATE_MCS_VHT_MSK) {
2155 		u8 stbc = (rate_n_flags & RATE_MCS_STBC_MSK) >>
2156 				RATE_MCS_STBC_POS;
2157 		rx_status->rate_idx = rate_n_flags & RATE_MCS_CODE_MSK;
2158 		rx_status->encoding = RX_ENC_VHT;
2159 		rx_status->enc_flags |= stbc << RX_ENC_FLAG_STBC_SHIFT;
2160 		if (rate_n_flags & RATE_MCS_BF_MSK)
2161 			rx_status->enc_flags |= RX_ENC_FLAG_BF;
2162 		/*
2163 		 * take the nss from the rx_vec since the rate_n_flags has
2164 		 * only 2 bits for the nss which gives a max of 4 ss but
2165 		 * there may be up to 8 spatial streams
2166 		 */
2167 		rx_status->nss =
2168 			le32_get_bits(desc->rx_vec[0],
2169 				      RX_NO_DATA_RX_VEC0_VHT_NSTS_MSK) + 1;
2170 	} else if (format == RATE_MCS_HE_MSK) {
2171 		rx_status->nss =
2172 			le32_get_bits(desc->rx_vec[0],
2173 				      RX_NO_DATA_RX_VEC0_HE_NSTS_MSK) + 1;
2174 	} else {
2175 		int rate = iwl_mvm_legacy_hw_idx_to_mac80211_idx(rate_n_flags,
2176 							       rx_status->band);
2177 
2178 		if (WARN(rate < 0 || rate > 0xFF,
2179 			 "Invalid rate flags 0x%x, band %d,\n",
2180 			 rate_n_flags, rx_status->band)) {
2181 			kfree_skb(skb);
2182 			goto out;
2183 		}
2184 		rx_status->rate_idx = rate;
2185 	}
2186 
2187 	ieee80211_rx_napi(mvm->hw, sta, skb, napi);
2188 out:
2189 	rcu_read_unlock();
2190 }
2191 
2192 void iwl_mvm_rx_frame_release(struct iwl_mvm *mvm, struct napi_struct *napi,
2193 			      struct iwl_rx_cmd_buffer *rxb, int queue)
2194 {
2195 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
2196 	struct iwl_frame_release *release = (void *)pkt->data;
2197 
2198 	if (unlikely(iwl_rx_packet_payload_len(pkt) < sizeof(*release)))
2199 		return;
2200 
2201 	iwl_mvm_release_frames_from_notif(mvm, napi, release->baid,
2202 					  le16_to_cpu(release->nssn),
2203 					  queue, 0);
2204 }
2205 
2206 void iwl_mvm_rx_bar_frame_release(struct iwl_mvm *mvm, struct napi_struct *napi,
2207 				  struct iwl_rx_cmd_buffer *rxb, int queue)
2208 {
2209 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
2210 	struct iwl_bar_frame_release *release = (void *)pkt->data;
2211 	unsigned int baid = le32_get_bits(release->ba_info,
2212 					  IWL_BAR_FRAME_RELEASE_BAID_MASK);
2213 	unsigned int nssn = le32_get_bits(release->ba_info,
2214 					  IWL_BAR_FRAME_RELEASE_NSSN_MASK);
2215 	unsigned int sta_id = le32_get_bits(release->sta_tid,
2216 					    IWL_BAR_FRAME_RELEASE_STA_MASK);
2217 	unsigned int tid = le32_get_bits(release->sta_tid,
2218 					 IWL_BAR_FRAME_RELEASE_TID_MASK);
2219 	struct iwl_mvm_baid_data *baid_data;
2220 
2221 	if (unlikely(iwl_rx_packet_payload_len(pkt) < sizeof(*release)))
2222 		return;
2223 
2224 	if (WARN_ON_ONCE(baid == IWL_RX_REORDER_DATA_INVALID_BAID ||
2225 			 baid >= ARRAY_SIZE(mvm->baid_map)))
2226 		return;
2227 
2228 	rcu_read_lock();
2229 	baid_data = rcu_dereference(mvm->baid_map[baid]);
2230 	if (!baid_data) {
2231 		IWL_DEBUG_RX(mvm,
2232 			     "Got valid BAID %d but not allocated, invalid BAR release!\n",
2233 			      baid);
2234 		goto out;
2235 	}
2236 
2237 	if (WARN(tid != baid_data->tid || sta_id != baid_data->sta_id,
2238 		 "baid 0x%x is mapped to sta:%d tid:%d, but BAR release received for sta:%d tid:%d\n",
2239 		 baid, baid_data->sta_id, baid_data->tid, sta_id,
2240 		 tid))
2241 		goto out;
2242 
2243 	iwl_mvm_release_frames_from_notif(mvm, napi, baid, nssn, queue, 0);
2244 out:
2245 	rcu_read_unlock();
2246 }
2247