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