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