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