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