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