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