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