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