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