1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /*
3  * Copyright (C) 2012-2014, 2018-2023 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 #include "time-sync.h"
13 
iwl_mvm_check_pn(struct iwl_mvm * mvm,struct sk_buff * skb,int queue,struct ieee80211_sta * sta)14 static inline int iwl_mvm_check_pn(struct iwl_mvm *mvm, struct sk_buff *skb,
15 				   int queue, struct ieee80211_sta *sta)
16 {
17 	struct iwl_mvm_sta *mvmsta;
18 	struct ieee80211_hdr *hdr = (void *)skb_mac_header(skb);
19 	struct ieee80211_rx_status *stats = IEEE80211_SKB_RXCB(skb);
20 	struct iwl_mvm_key_pn *ptk_pn;
21 	int res;
22 	u8 tid, keyidx;
23 	u8 pn[IEEE80211_CCMP_PN_LEN];
24 	u8 *extiv;
25 
26 	/* do PN checking */
27 
28 	/* multicast and non-data only arrives on default queue */
29 	if (!ieee80211_is_data(hdr->frame_control) ||
30 	    is_multicast_ether_addr(hdr->addr1))
31 		return 0;
32 
33 	/* do not check PN for open AP */
34 	if (!(stats->flag & RX_FLAG_DECRYPTED))
35 		return 0;
36 
37 	/*
38 	 * avoid checking for default queue - we don't want to replicate
39 	 * all the logic that's necessary for checking the PN on fragmented
40 	 * frames, leave that to mac80211
41 	 */
42 	if (queue == 0)
43 		return 0;
44 
45 	/* if we are here - this for sure is either CCMP or GCMP */
46 	if (IS_ERR_OR_NULL(sta)) {
47 		IWL_DEBUG_DROP(mvm,
48 			       "expected hw-decrypted unicast frame for station\n");
49 		return -1;
50 	}
51 
52 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
53 
54 	extiv = (u8 *)hdr + ieee80211_hdrlen(hdr->frame_control);
55 	keyidx = extiv[3] >> 6;
56 
57 	ptk_pn = rcu_dereference(mvmsta->ptk_pn[keyidx]);
58 	if (!ptk_pn)
59 		return -1;
60 
61 	if (ieee80211_is_data_qos(hdr->frame_control))
62 		tid = ieee80211_get_tid(hdr);
63 	else
64 		tid = 0;
65 
66 	/* we don't use HCCA/802.11 QoS TSPECs, so drop such frames */
67 	if (tid >= IWL_MAX_TID_COUNT)
68 		return -1;
69 
70 	/* load pn */
71 	pn[0] = extiv[7];
72 	pn[1] = extiv[6];
73 	pn[2] = extiv[5];
74 	pn[3] = extiv[4];
75 	pn[4] = extiv[1];
76 	pn[5] = extiv[0];
77 
78 	res = memcmp(pn, ptk_pn->q[queue].pn[tid], IEEE80211_CCMP_PN_LEN);
79 	if (res < 0)
80 		return -1;
81 	if (!res && !(stats->flag & RX_FLAG_ALLOW_SAME_PN))
82 		return -1;
83 
84 	memcpy(ptk_pn->q[queue].pn[tid], pn, IEEE80211_CCMP_PN_LEN);
85 	stats->flag |= RX_FLAG_PN_VALIDATED;
86 
87 	return 0;
88 }
89 
90 /* 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)91 static int iwl_mvm_create_skb(struct iwl_mvm *mvm, struct sk_buff *skb,
92 			      struct ieee80211_hdr *hdr, u16 len, u8 crypt_len,
93 			      struct iwl_rx_cmd_buffer *rxb)
94 {
95 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
96 	struct iwl_rx_mpdu_desc *desc = (void *)pkt->data;
97 	unsigned int headlen, fraglen, pad_len = 0;
98 	unsigned int hdrlen = ieee80211_hdrlen(hdr->frame_control);
99 	u8 mic_crc_len = u8_get_bits(desc->mac_flags1,
100 				     IWL_RX_MPDU_MFLG1_MIC_CRC_LEN_MASK) << 1;
101 
102 	if (desc->mac_flags2 & IWL_RX_MPDU_MFLG2_PAD) {
103 		len -= 2;
104 		pad_len = 2;
105 	}
106 
107 	/*
108 	 * For non monitor interface strip the bytes the RADA might not have
109 	 * removed (it might be disabled, e.g. for mgmt frames). As a monitor
110 	 * interface cannot exist with other interfaces, this removal is safe
111 	 * and sufficient, in monitor mode there's no decryption being done.
112 	 */
113 	if (len > mic_crc_len && !ieee80211_hw_check(mvm->hw, RX_INCLUDES_FCS))
114 		len -= mic_crc_len;
115 
116 	/* If frame is small enough to fit in skb->head, pull it completely.
117 	 * If not, only pull ieee80211_hdr (including crypto if present, and
118 	 * an additional 8 bytes for SNAP/ethertype, see below) so that
119 	 * splice() or TCP coalesce are more efficient.
120 	 *
121 	 * Since, in addition, ieee80211_data_to_8023() always pull in at
122 	 * least 8 bytes (possibly more for mesh) we can do the same here
123 	 * to save the cost of doing it later. That still doesn't pull in
124 	 * the actual IP header since the typical case has a SNAP header.
125 	 * If the latter changes (there are efforts in the standards group
126 	 * to do so) we should revisit this and ieee80211_data_to_8023().
127 	 */
128 	headlen = (len <= skb_tailroom(skb)) ? len :
129 					       hdrlen + crypt_len + 8;
130 
131 	/* The firmware may align the packet to DWORD.
132 	 * The padding is inserted after the IV.
133 	 * After copying the header + IV skip the padding if
134 	 * present before copying packet data.
135 	 */
136 	hdrlen += crypt_len;
137 
138 	if (unlikely(headlen < hdrlen))
139 		return -EINVAL;
140 
141 	/* Since data doesn't move data while putting data on skb and that is
142 	 * the only way we use, data + len is the next place that hdr would be put
143 	 */
144 	skb_set_mac_header(skb, skb->len);
145 	skb_put_data(skb, hdr, hdrlen);
146 	skb_put_data(skb, (u8 *)hdr + hdrlen + pad_len, headlen - hdrlen);
147 
148 	/*
149 	 * If we did CHECKSUM_COMPLETE, the hardware only does it right for
150 	 * certain cases and starts the checksum after the SNAP. Check if
151 	 * this is the case - it's easier to just bail out to CHECKSUM_NONE
152 	 * in the cases the hardware didn't handle, since it's rare to see
153 	 * such packets, even though the hardware did calculate the checksum
154 	 * in this case, just starting after the MAC header instead.
155 	 *
156 	 * Starting from Bz hardware, it calculates starting directly after
157 	 * the MAC header, so that matches mac80211's expectation.
158 	 */
159 	if (skb->ip_summed == CHECKSUM_COMPLETE) {
160 		struct {
161 			u8 hdr[6];
162 			__be16 type;
163 		} __packed *shdr = (void *)((u8 *)hdr + hdrlen + pad_len);
164 
165 		if (unlikely(headlen - hdrlen < sizeof(*shdr) ||
166 			     !ether_addr_equal(shdr->hdr, rfc1042_header) ||
167 			     (shdr->type != htons(ETH_P_IP) &&
168 			      shdr->type != htons(ETH_P_ARP) &&
169 			      shdr->type != htons(ETH_P_IPV6) &&
170 			      shdr->type != htons(ETH_P_8021Q) &&
171 			      shdr->type != htons(ETH_P_PAE) &&
172 			      shdr->type != htons(ETH_P_TDLS))))
173 			skb->ip_summed = CHECKSUM_NONE;
174 		else if (mvm->trans->trans_cfg->device_family < IWL_DEVICE_FAMILY_BZ)
175 			/* mac80211 assumes full CSUM including SNAP header */
176 			skb_postpush_rcsum(skb, shdr, sizeof(*shdr));
177 	}
178 
179 	fraglen = len - headlen;
180 
181 	if (fraglen) {
182 		int offset = (u8 *)hdr + headlen + pad_len -
183 			     (u8 *)rxb_addr(rxb) + rxb_offset(rxb);
184 
185 		skb_add_rx_frag(skb, 0, rxb_steal_page(rxb), offset,
186 				fraglen, rxb->truesize);
187 	}
188 
189 	return 0;
190 }
191 
192 /* put a TLV on the skb and return data pointer
193  *
194  * Also pad to 4 the len and zero out all data part
195  */
196 static void *
iwl_mvm_radiotap_put_tlv(struct sk_buff * skb,u16 type,u16 len)197 iwl_mvm_radiotap_put_tlv(struct sk_buff *skb, u16 type, u16 len)
198 {
199 	struct ieee80211_radiotap_tlv *tlv;
200 
201 	tlv = skb_put(skb, sizeof(*tlv));
202 	tlv->type = cpu_to_le16(type);
203 	tlv->len = cpu_to_le16(len);
204 	return skb_put_zero(skb, ALIGN(len, 4));
205 }
206 
iwl_mvm_add_rtap_sniffer_config(struct iwl_mvm * mvm,struct sk_buff * skb)207 static void iwl_mvm_add_rtap_sniffer_config(struct iwl_mvm *mvm,
208 					    struct sk_buff *skb)
209 {
210 	struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
211 	struct ieee80211_radiotap_vendor_content *radiotap;
212 	const u16 vendor_data_len = sizeof(mvm->cur_aid);
213 
214 	if (!mvm->cur_aid)
215 		return;
216 
217 	radiotap = iwl_mvm_radiotap_put_tlv(skb,
218 					    IEEE80211_RADIOTAP_VENDOR_NAMESPACE,
219 					    sizeof(*radiotap) + vendor_data_len);
220 
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->oui_subtype = 1;
227 	radiotap->vendor_type = 0;
228 
229 	/* fill the data now */
230 	memcpy(radiotap->data, &mvm->cur_aid, sizeof(mvm->cur_aid));
231 
232 	rx_status->flag |= RX_FLAG_RADIOTAP_TLV_AT_END;
233 }
234 
235 /* 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)236 static void iwl_mvm_pass_packet_to_mac80211(struct iwl_mvm *mvm,
237 					    struct napi_struct *napi,
238 					    struct sk_buff *skb, int queue,
239 					    struct ieee80211_sta *sta)
240 {
241 	if (unlikely(iwl_mvm_check_pn(mvm, skb, queue, sta))) {
242 		kfree_skb(skb);
243 		return;
244 	}
245 
246 	ieee80211_rx_napi(mvm->hw, sta, skb, napi);
247 }
248 
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)249 static void iwl_mvm_get_signal_strength(struct iwl_mvm *mvm,
250 					struct ieee80211_rx_status *rx_status,
251 					u32 rate_n_flags, int energy_a,
252 					int energy_b)
253 {
254 	int max_energy;
255 	u32 rate_flags = rate_n_flags;
256 
257 	energy_a = energy_a ? -energy_a : S8_MIN;
258 	energy_b = energy_b ? -energy_b : S8_MIN;
259 	max_energy = max(energy_a, energy_b);
260 
261 	IWL_DEBUG_STATS(mvm, "energy In A %d B %d, and max %d\n",
262 			energy_a, energy_b, max_energy);
263 
264 	rx_status->signal = max_energy;
265 	rx_status->chains =
266 		(rate_flags & RATE_MCS_ANT_AB_MSK) >> RATE_MCS_ANT_POS;
267 	rx_status->chain_signal[0] = energy_a;
268 	rx_status->chain_signal[1] = energy_b;
269 }
270 
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)271 static int iwl_mvm_rx_mgmt_prot(struct ieee80211_sta *sta,
272 				struct ieee80211_hdr *hdr,
273 				struct iwl_rx_mpdu_desc *desc,
274 				u32 status,
275 				struct ieee80211_rx_status *stats)
276 {
277 	struct wireless_dev *wdev;
278 	struct iwl_mvm_sta *mvmsta;
279 	struct iwl_mvm_vif *mvmvif;
280 	u8 keyid;
281 	struct ieee80211_key_conf *key;
282 	u32 len = le16_to_cpu(desc->mpdu_len);
283 	const u8 *frame = (void *)hdr;
284 
285 	if ((status & IWL_RX_MPDU_STATUS_SEC_MASK) == IWL_RX_MPDU_STATUS_SEC_NONE)
286 		return 0;
287 
288 	/*
289 	 * For non-beacon, we don't really care. But beacons may
290 	 * be filtered out, and we thus need the firmware's replay
291 	 * detection, otherwise beacons the firmware previously
292 	 * filtered could be replayed, or something like that, and
293 	 * it can filter a lot - though usually only if nothing has
294 	 * changed.
295 	 */
296 	if (!ieee80211_is_beacon(hdr->frame_control))
297 		return 0;
298 
299 	if (!sta)
300 		return -1;
301 
302 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
303 	mvmvif = iwl_mvm_vif_from_mac80211(mvmsta->vif);
304 
305 	/* key mismatch - will also report !MIC_OK but we shouldn't count it */
306 	if (!(status & IWL_RX_MPDU_STATUS_KEY_VALID))
307 		goto report;
308 
309 	/* good cases */
310 	if (likely(status & IWL_RX_MPDU_STATUS_MIC_OK &&
311 		   !(status & IWL_RX_MPDU_STATUS_REPLAY_ERROR))) {
312 		stats->flag |= RX_FLAG_DECRYPTED;
313 		return 0;
314 	}
315 
316 	/*
317 	 * both keys will have the same cipher and MIC length, use
318 	 * whichever one is available
319 	 */
320 	key = rcu_dereference(mvmvif->bcn_prot.keys[0]);
321 	if (!key) {
322 		key = rcu_dereference(mvmvif->bcn_prot.keys[1]);
323 		if (!key)
324 			goto report;
325 	}
326 
327 	if (len < key->icv_len + IEEE80211_GMAC_PN_LEN + 2)
328 		goto report;
329 
330 	/* get the real key ID */
331 	keyid = frame[len - key->icv_len - IEEE80211_GMAC_PN_LEN - 2];
332 	/* and if that's the other key, look it up */
333 	if (keyid != key->keyidx) {
334 		/*
335 		 * shouldn't happen since firmware checked, but be safe
336 		 * in case the MIC length is wrong too, for example
337 		 */
338 		if (keyid != 6 && keyid != 7)
339 			return -1;
340 		key = rcu_dereference(mvmvif->bcn_prot.keys[keyid - 6]);
341 		if (!key)
342 			goto report;
343 	}
344 
345 	/* Report status to mac80211 */
346 	if (!(status & IWL_RX_MPDU_STATUS_MIC_OK))
347 		ieee80211_key_mic_failure(key);
348 	else if (status & IWL_RX_MPDU_STATUS_REPLAY_ERROR)
349 		ieee80211_key_replay(key);
350 report:
351 	wdev = ieee80211_vif_to_wdev(mvmsta->vif);
352 	if (wdev->netdev)
353 		cfg80211_rx_unprot_mlme_mgmt(wdev->netdev, (void *)hdr, len);
354 
355 	return -1;
356 }
357 
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)358 static int iwl_mvm_rx_crypto(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
359 			     struct ieee80211_hdr *hdr,
360 			     struct ieee80211_rx_status *stats, u16 phy_info,
361 			     struct iwl_rx_mpdu_desc *desc,
362 			     u32 pkt_flags, int queue, u8 *crypt_len)
363 {
364 	u32 status = le32_to_cpu(desc->status);
365 
366 	/*
367 	 * Drop UNKNOWN frames in aggregation, unless in monitor mode
368 	 * (where we don't have the keys).
369 	 * We limit this to aggregation because in TKIP this is a valid
370 	 * scenario, since we may not have the (correct) TTAK (phase 1
371 	 * key) in the firmware.
372 	 */
373 	if (phy_info & IWL_RX_MPDU_PHY_AMPDU &&
374 	    (status & IWL_RX_MPDU_STATUS_SEC_MASK) ==
375 	    IWL_RX_MPDU_STATUS_SEC_UNKNOWN && !mvm->monitor_on)
376 		return -1;
377 
378 	if (unlikely(ieee80211_is_mgmt(hdr->frame_control) &&
379 		     !ieee80211_has_protected(hdr->frame_control)))
380 		return iwl_mvm_rx_mgmt_prot(sta, hdr, desc, status, stats);
381 
382 	if (!ieee80211_has_protected(hdr->frame_control) ||
383 	    (status & IWL_RX_MPDU_STATUS_SEC_MASK) ==
384 	    IWL_RX_MPDU_STATUS_SEC_NONE)
385 		return 0;
386 
387 	/* TODO: handle packets encrypted with unknown alg */
388 
389 	switch (status & IWL_RX_MPDU_STATUS_SEC_MASK) {
390 	case IWL_RX_MPDU_STATUS_SEC_CCM:
391 	case IWL_RX_MPDU_STATUS_SEC_GCM:
392 		BUILD_BUG_ON(IEEE80211_CCMP_PN_LEN != IEEE80211_GCMP_PN_LEN);
393 		/* alg is CCM: check MIC only */
394 		if (!(status & IWL_RX_MPDU_STATUS_MIC_OK))
395 			return -1;
396 
397 		stats->flag |= RX_FLAG_DECRYPTED | RX_FLAG_MIC_STRIPPED;
398 		*crypt_len = IEEE80211_CCMP_HDR_LEN;
399 		return 0;
400 	case IWL_RX_MPDU_STATUS_SEC_TKIP:
401 		/* Don't drop the frame and decrypt it in SW */
402 		if (!fw_has_api(&mvm->fw->ucode_capa,
403 				IWL_UCODE_TLV_API_DEPRECATE_TTAK) &&
404 		    !(status & IWL_RX_MPDU_RES_STATUS_TTAK_OK))
405 			return 0;
406 
407 		if (mvm->trans->trans_cfg->gen2 &&
408 		    !(status & RX_MPDU_RES_STATUS_MIC_OK))
409 			stats->flag |= RX_FLAG_MMIC_ERROR;
410 
411 		*crypt_len = IEEE80211_TKIP_IV_LEN;
412 		fallthrough;
413 	case IWL_RX_MPDU_STATUS_SEC_WEP:
414 		if (!(status & IWL_RX_MPDU_STATUS_ICV_OK))
415 			return -1;
416 
417 		stats->flag |= RX_FLAG_DECRYPTED;
418 		if ((status & IWL_RX_MPDU_STATUS_SEC_MASK) ==
419 				IWL_RX_MPDU_STATUS_SEC_WEP)
420 			*crypt_len = IEEE80211_WEP_IV_LEN;
421 
422 		if (pkt_flags & FH_RSCSR_RADA_EN) {
423 			stats->flag |= RX_FLAG_ICV_STRIPPED;
424 			if (mvm->trans->trans_cfg->gen2)
425 				stats->flag |= RX_FLAG_MMIC_STRIPPED;
426 		}
427 
428 		return 0;
429 	case IWL_RX_MPDU_STATUS_SEC_EXT_ENC:
430 		if (!(status & IWL_RX_MPDU_STATUS_MIC_OK))
431 			return -1;
432 		stats->flag |= RX_FLAG_DECRYPTED;
433 		return 0;
434 	case RX_MPDU_RES_STATUS_SEC_CMAC_GMAC_ENC:
435 		break;
436 	default:
437 		/*
438 		 * Sometimes we can get frames that were not decrypted
439 		 * because the firmware didn't have the keys yet. This can
440 		 * happen after connection where we can get multicast frames
441 		 * before the GTK is installed.
442 		 * Silently drop those frames.
443 		 * Also drop un-decrypted frames in monitor mode.
444 		 */
445 		if (!is_multicast_ether_addr(hdr->addr1) &&
446 		    !mvm->monitor_on && net_ratelimit())
447 			IWL_WARN(mvm, "Unhandled alg: 0x%x\n", status);
448 	}
449 
450 	return 0;
451 }
452 
iwl_mvm_rx_csum(struct iwl_mvm * mvm,struct ieee80211_sta * sta,struct sk_buff * skb,struct iwl_rx_packet * pkt)453 static void iwl_mvm_rx_csum(struct iwl_mvm *mvm,
454 			    struct ieee80211_sta *sta,
455 			    struct sk_buff *skb,
456 			    struct iwl_rx_packet *pkt)
457 {
458 	struct iwl_rx_mpdu_desc *desc = (void *)pkt->data;
459 
460 	if (mvm->trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_AX210) {
461 		if (pkt->len_n_flags & cpu_to_le32(FH_RSCSR_RPA_EN)) {
462 			u16 hwsum = be16_to_cpu(desc->v3.raw_xsum);
463 
464 			skb->ip_summed = CHECKSUM_COMPLETE;
465 			skb->csum = csum_unfold(~(__force __sum16)hwsum);
466 		}
467 	} else {
468 		struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
469 		struct iwl_mvm_vif *mvmvif;
470 		u16 flags = le16_to_cpu(desc->l3l4_flags);
471 		u8 l3_prot = (u8)((flags & IWL_RX_L3L4_L3_PROTO_MASK) >>
472 				  IWL_RX_L3_PROTO_POS);
473 
474 		mvmvif = iwl_mvm_vif_from_mac80211(mvmsta->vif);
475 
476 		if (mvmvif->features & NETIF_F_RXCSUM &&
477 		    flags & IWL_RX_L3L4_TCP_UDP_CSUM_OK &&
478 		    (flags & IWL_RX_L3L4_IP_HDR_CSUM_OK ||
479 		     l3_prot == IWL_RX_L3_TYPE_IPV6 ||
480 		     l3_prot == IWL_RX_L3_TYPE_IPV6_FRAG))
481 			skb->ip_summed = CHECKSUM_UNNECESSARY;
482 	}
483 }
484 
485 /*
486  * returns true if a packet is a duplicate or invalid tid and should be dropped.
487  * Updates AMSDU PN tracking info
488  */
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)489 static bool iwl_mvm_is_dup(struct ieee80211_sta *sta, int queue,
490 			   struct ieee80211_rx_status *rx_status,
491 			   struct ieee80211_hdr *hdr,
492 			   struct iwl_rx_mpdu_desc *desc)
493 {
494 	struct iwl_mvm_sta *mvm_sta;
495 	struct iwl_mvm_rxq_dup_data *dup_data;
496 	u8 tid, sub_frame_idx;
497 
498 	if (WARN_ON(IS_ERR_OR_NULL(sta)))
499 		return false;
500 
501 	mvm_sta = iwl_mvm_sta_from_mac80211(sta);
502 
503 	if (WARN_ON_ONCE(!mvm_sta->dup_data))
504 		return false;
505 
506 	dup_data = &mvm_sta->dup_data[queue];
507 
508 	/*
509 	 * Drop duplicate 802.11 retransmissions
510 	 * (IEEE 802.11-2012: 9.3.2.10 "Duplicate detection and recovery")
511 	 */
512 	if (ieee80211_is_ctl(hdr->frame_control) ||
513 	    ieee80211_is_qos_nullfunc(hdr->frame_control) ||
514 	    is_multicast_ether_addr(hdr->addr1)) {
515 		rx_status->flag |= RX_FLAG_DUP_VALIDATED;
516 		return false;
517 	}
518 
519 	if (ieee80211_is_data_qos(hdr->frame_control)) {
520 		/* frame has qos control */
521 		tid = ieee80211_get_tid(hdr);
522 		if (tid >= IWL_MAX_TID_COUNT)
523 			return true;
524 	} else {
525 		tid = IWL_MAX_TID_COUNT;
526 	}
527 
528 	/* If this wasn't a part of an A-MSDU the sub-frame index will be 0 */
529 	sub_frame_idx = desc->amsdu_info &
530 		IWL_RX_MPDU_AMSDU_SUBFRAME_IDX_MASK;
531 
532 	if (unlikely(ieee80211_has_retry(hdr->frame_control) &&
533 		     dup_data->last_seq[tid] == hdr->seq_ctrl &&
534 		     dup_data->last_sub_frame[tid] >= sub_frame_idx))
535 		return true;
536 
537 	/* Allow same PN as the first subframe for following sub frames */
538 	if (dup_data->last_seq[tid] == hdr->seq_ctrl &&
539 	    sub_frame_idx > dup_data->last_sub_frame[tid] &&
540 	    desc->mac_flags2 & IWL_RX_MPDU_MFLG2_AMSDU)
541 		rx_status->flag |= RX_FLAG_ALLOW_SAME_PN;
542 
543 	dup_data->last_seq[tid] = hdr->seq_ctrl;
544 	dup_data->last_sub_frame[tid] = sub_frame_idx;
545 
546 	rx_status->flag |= RX_FLAG_DUP_VALIDATED;
547 
548 	return false;
549 }
550 
551 /*
552  * Returns true if sn2 - buffer_size < sn1 < sn2.
553  * To be used only in order to compare reorder buffer head with NSSN.
554  * We fully trust NSSN unless it is behind us due to reorder timeout.
555  * Reorder timeout can only bring us up to buffer_size SNs ahead of NSSN.
556  */
iwl_mvm_is_sn_less(u16 sn1,u16 sn2,u16 buffer_size)557 static bool iwl_mvm_is_sn_less(u16 sn1, u16 sn2, u16 buffer_size)
558 {
559 	return ieee80211_sn_less(sn1, sn2) &&
560 	       !ieee80211_sn_less(sn1, sn2 - buffer_size);
561 }
562 
iwl_mvm_sync_nssn(struct iwl_mvm * mvm,u8 baid,u16 nssn)563 static void iwl_mvm_sync_nssn(struct iwl_mvm *mvm, u8 baid, u16 nssn)
564 {
565 	if (IWL_MVM_USE_NSSN_SYNC) {
566 		struct iwl_mvm_nssn_sync_data notif = {
567 			.baid = baid,
568 			.nssn = nssn,
569 		};
570 
571 		iwl_mvm_sync_rx_queues_internal(mvm, IWL_MVM_RXQ_NSSN_SYNC, false,
572 						&notif, sizeof(notif));
573 	}
574 }
575 
576 #define RX_REORDER_BUF_TIMEOUT_MQ (HZ / 10)
577 
578 enum iwl_mvm_release_flags {
579 	IWL_MVM_RELEASE_SEND_RSS_SYNC = BIT(0),
580 	IWL_MVM_RELEASE_FROM_RSS_SYNC = BIT(1),
581 };
582 
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)583 static void iwl_mvm_release_frames(struct iwl_mvm *mvm,
584 				   struct ieee80211_sta *sta,
585 				   struct napi_struct *napi,
586 				   struct iwl_mvm_baid_data *baid_data,
587 				   struct iwl_mvm_reorder_buffer *reorder_buf,
588 				   u16 nssn, u32 flags)
589 {
590 	struct iwl_mvm_reorder_buf_entry *entries =
591 		&baid_data->entries[reorder_buf->queue *
592 				    baid_data->entries_per_queue];
593 	u16 ssn = reorder_buf->head_sn;
594 
595 	lockdep_assert_held(&reorder_buf->lock);
596 
597 	/*
598 	 * We keep the NSSN not too far behind, if we are sync'ing it and it
599 	 * is more than 2048 ahead of us, it must be behind us. Discard it.
600 	 * This can happen if the queue that hit the 0 / 2048 seqno was lagging
601 	 * behind and this queue already processed packets. The next if
602 	 * would have caught cases where this queue would have processed less
603 	 * than 64 packets, but it may have processed more than 64 packets.
604 	 */
605 	if ((flags & IWL_MVM_RELEASE_FROM_RSS_SYNC) &&
606 	    ieee80211_sn_less(nssn, ssn))
607 		goto set_timer;
608 
609 	/* ignore nssn smaller than head sn - this can happen due to timeout */
610 	if (iwl_mvm_is_sn_less(nssn, ssn, reorder_buf->buf_size))
611 		goto set_timer;
612 
613 	while (iwl_mvm_is_sn_less(ssn, nssn, reorder_buf->buf_size)) {
614 		int index = ssn % reorder_buf->buf_size;
615 		struct sk_buff_head *skb_list = &entries[index].e.frames;
616 		struct sk_buff *skb;
617 
618 		ssn = ieee80211_sn_inc(ssn);
619 		if ((flags & IWL_MVM_RELEASE_SEND_RSS_SYNC) &&
620 		    (ssn == 2048 || ssn == 0))
621 			iwl_mvm_sync_nssn(mvm, baid_data->baid, ssn);
622 
623 		/*
624 		 * Empty the list. Will have more than one frame for A-MSDU.
625 		 * Empty list is valid as well since nssn indicates frames were
626 		 * received.
627 		 */
628 		while ((skb = __skb_dequeue(skb_list))) {
629 			iwl_mvm_pass_packet_to_mac80211(mvm, napi, skb,
630 							reorder_buf->queue,
631 							sta);
632 			reorder_buf->num_stored--;
633 		}
634 	}
635 	reorder_buf->head_sn = nssn;
636 
637 set_timer:
638 	if (reorder_buf->num_stored && !reorder_buf->removed) {
639 		u16 index = reorder_buf->head_sn % reorder_buf->buf_size;
640 
641 		while (skb_queue_empty(&entries[index].e.frames))
642 			index = (index + 1) % reorder_buf->buf_size;
643 		/* modify timer to match next frame's expiration time */
644 		mod_timer(&reorder_buf->reorder_timer,
645 			  entries[index].e.reorder_time + 1 +
646 			  RX_REORDER_BUF_TIMEOUT_MQ);
647 	} else {
648 		del_timer(&reorder_buf->reorder_timer);
649 	}
650 }
651 
iwl_mvm_reorder_timer_expired(struct timer_list * t)652 void iwl_mvm_reorder_timer_expired(struct timer_list *t)
653 {
654 	struct iwl_mvm_reorder_buffer *buf = from_timer(buf, t, reorder_timer);
655 	struct iwl_mvm_baid_data *baid_data =
656 		iwl_mvm_baid_data_from_reorder_buf(buf);
657 	struct iwl_mvm_reorder_buf_entry *entries =
658 		&baid_data->entries[buf->queue * baid_data->entries_per_queue];
659 	int i;
660 	u16 sn = 0, index = 0;
661 	bool expired = false;
662 	bool cont = false;
663 
664 	spin_lock(&buf->lock);
665 
666 	if (!buf->num_stored || buf->removed) {
667 		spin_unlock(&buf->lock);
668 		return;
669 	}
670 
671 	for (i = 0; i < buf->buf_size ; i++) {
672 		index = (buf->head_sn + i) % buf->buf_size;
673 
674 		if (skb_queue_empty(&entries[index].e.frames)) {
675 			/*
676 			 * If there is a hole and the next frame didn't expire
677 			 * we want to break and not advance SN
678 			 */
679 			cont = false;
680 			continue;
681 		}
682 		if (!cont &&
683 		    !time_after(jiffies, entries[index].e.reorder_time +
684 					 RX_REORDER_BUF_TIMEOUT_MQ))
685 			break;
686 
687 		expired = true;
688 		/* continue until next hole after this expired frames */
689 		cont = true;
690 		sn = ieee80211_sn_add(buf->head_sn, i + 1);
691 	}
692 
693 	if (expired) {
694 		struct ieee80211_sta *sta;
695 		struct iwl_mvm_sta *mvmsta;
696 		u8 sta_id = ffs(baid_data->sta_mask) - 1;
697 
698 		rcu_read_lock();
699 		sta = rcu_dereference(buf->mvm->fw_id_to_mac_id[sta_id]);
700 		if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) {
701 			rcu_read_unlock();
702 			goto out;
703 		}
704 
705 		mvmsta = iwl_mvm_sta_from_mac80211(sta);
706 
707 		/* SN is set to the last expired frame + 1 */
708 		IWL_DEBUG_HT(buf->mvm,
709 			     "Releasing expired frames for sta %u, sn %d\n",
710 			     sta_id, sn);
711 		iwl_mvm_event_frame_timeout_callback(buf->mvm, mvmsta->vif,
712 						     sta, baid_data->tid);
713 		iwl_mvm_release_frames(buf->mvm, sta, NULL, baid_data,
714 				       buf, sn, IWL_MVM_RELEASE_SEND_RSS_SYNC);
715 		rcu_read_unlock();
716 	} else {
717 		/*
718 		 * If no frame expired and there are stored frames, index is now
719 		 * pointing to the first unexpired frame - modify timer
720 		 * accordingly to this frame.
721 		 */
722 		mod_timer(&buf->reorder_timer,
723 			  entries[index].e.reorder_time +
724 			  1 + RX_REORDER_BUF_TIMEOUT_MQ);
725 	}
726 
727 out:
728 	spin_unlock(&buf->lock);
729 }
730 
iwl_mvm_del_ba(struct iwl_mvm * mvm,int queue,struct iwl_mvm_delba_data * data)731 static void iwl_mvm_del_ba(struct iwl_mvm *mvm, int queue,
732 			   struct iwl_mvm_delba_data *data)
733 {
734 	struct iwl_mvm_baid_data *ba_data;
735 	struct ieee80211_sta *sta;
736 	struct iwl_mvm_reorder_buffer *reorder_buf;
737 	u8 baid = data->baid;
738 	u32 sta_id;
739 
740 	if (WARN_ONCE(baid >= IWL_MAX_BAID, "invalid BAID: %x\n", baid))
741 		return;
742 
743 	rcu_read_lock();
744 
745 	ba_data = rcu_dereference(mvm->baid_map[baid]);
746 	if (WARN_ON_ONCE(!ba_data))
747 		goto out;
748 
749 	/* pick any STA ID to find the pointer */
750 	sta_id = ffs(ba_data->sta_mask) - 1;
751 	sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
752 	if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta)))
753 		goto out;
754 
755 	reorder_buf = &ba_data->reorder_buf[queue];
756 
757 	/* release all frames that are in the reorder buffer to the stack */
758 	spin_lock_bh(&reorder_buf->lock);
759 	iwl_mvm_release_frames(mvm, sta, NULL, ba_data, reorder_buf,
760 			       ieee80211_sn_add(reorder_buf->head_sn,
761 						reorder_buf->buf_size),
762 			       0);
763 	spin_unlock_bh(&reorder_buf->lock);
764 	del_timer_sync(&reorder_buf->reorder_timer);
765 
766 out:
767 	rcu_read_unlock();
768 }
769 
iwl_mvm_release_frames_from_notif(struct iwl_mvm * mvm,struct napi_struct * napi,u8 baid,u16 nssn,int queue,u32 flags)770 static void iwl_mvm_release_frames_from_notif(struct iwl_mvm *mvm,
771 					      struct napi_struct *napi,
772 					      u8 baid, u16 nssn, int queue,
773 					      u32 flags)
774 {
775 	struct ieee80211_sta *sta;
776 	struct iwl_mvm_reorder_buffer *reorder_buf;
777 	struct iwl_mvm_baid_data *ba_data;
778 	u32 sta_id;
779 
780 	IWL_DEBUG_HT(mvm, "Frame release notification for BAID %u, NSSN %d\n",
781 		     baid, nssn);
782 
783 	if (WARN_ON_ONCE(baid == IWL_RX_REORDER_DATA_INVALID_BAID ||
784 			 baid >= ARRAY_SIZE(mvm->baid_map)))
785 		return;
786 
787 	rcu_read_lock();
788 
789 	ba_data = rcu_dereference(mvm->baid_map[baid]);
790 	if (!ba_data) {
791 		WARN(!(flags & IWL_MVM_RELEASE_FROM_RSS_SYNC),
792 		     "BAID %d not found in map\n", baid);
793 		goto out;
794 	}
795 
796 	/* pick any STA ID to find the pointer */
797 	sta_id = ffs(ba_data->sta_mask) - 1;
798 	sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
799 	if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta)))
800 		goto out;
801 
802 	reorder_buf = &ba_data->reorder_buf[queue];
803 
804 	spin_lock_bh(&reorder_buf->lock);
805 	iwl_mvm_release_frames(mvm, sta, napi, ba_data,
806 			       reorder_buf, nssn, flags);
807 	spin_unlock_bh(&reorder_buf->lock);
808 
809 out:
810 	rcu_read_unlock();
811 }
812 
iwl_mvm_nssn_sync(struct iwl_mvm * mvm,struct napi_struct * napi,int queue,const struct iwl_mvm_nssn_sync_data * data)813 static void iwl_mvm_nssn_sync(struct iwl_mvm *mvm,
814 			      struct napi_struct *napi, int queue,
815 			      const struct iwl_mvm_nssn_sync_data *data)
816 {
817 	iwl_mvm_release_frames_from_notif(mvm, napi, data->baid,
818 					  data->nssn, queue,
819 					  IWL_MVM_RELEASE_FROM_RSS_SYNC);
820 }
821 
iwl_mvm_rx_queue_notif(struct iwl_mvm * mvm,struct napi_struct * napi,struct iwl_rx_cmd_buffer * rxb,int queue)822 void iwl_mvm_rx_queue_notif(struct iwl_mvm *mvm, struct napi_struct *napi,
823 			    struct iwl_rx_cmd_buffer *rxb, int queue)
824 {
825 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
826 	struct iwl_rxq_sync_notification *notif;
827 	struct iwl_mvm_internal_rxq_notif *internal_notif;
828 	u32 len = iwl_rx_packet_payload_len(pkt);
829 
830 	notif = (void *)pkt->data;
831 	internal_notif = (void *)notif->payload;
832 
833 	if (WARN_ONCE(len < sizeof(*notif) + sizeof(*internal_notif),
834 		      "invalid notification size %d (%d)",
835 		      len, (int)(sizeof(*notif) + sizeof(*internal_notif))))
836 		return;
837 	len -= sizeof(*notif) + sizeof(*internal_notif);
838 
839 	if (internal_notif->sync &&
840 	    mvm->queue_sync_cookie != internal_notif->cookie) {
841 		WARN_ONCE(1, "Received expired RX queue sync message\n");
842 		return;
843 	}
844 
845 	switch (internal_notif->type) {
846 	case IWL_MVM_RXQ_EMPTY:
847 		WARN_ONCE(len, "invalid empty notification size %d", len);
848 		break;
849 	case IWL_MVM_RXQ_NOTIF_DEL_BA:
850 		if (WARN_ONCE(len != sizeof(struct iwl_mvm_delba_data),
851 			      "invalid delba notification size %d (%d)",
852 			      len, (int)sizeof(struct iwl_mvm_delba_data)))
853 			break;
854 		iwl_mvm_del_ba(mvm, queue, (void *)internal_notif->data);
855 		break;
856 	case IWL_MVM_RXQ_NSSN_SYNC:
857 		if (WARN_ONCE(len != sizeof(struct iwl_mvm_nssn_sync_data),
858 			      "invalid nssn sync notification size %d (%d)",
859 			      len, (int)sizeof(struct iwl_mvm_nssn_sync_data)))
860 			break;
861 		iwl_mvm_nssn_sync(mvm, napi, queue,
862 				  (void *)internal_notif->data);
863 		break;
864 	default:
865 		WARN_ONCE(1, "Invalid identifier %d", internal_notif->type);
866 	}
867 
868 	if (internal_notif->sync) {
869 		WARN_ONCE(!test_and_clear_bit(queue, &mvm->queue_sync_state),
870 			  "queue sync: queue %d responded a second time!\n",
871 			  queue);
872 		if (READ_ONCE(mvm->queue_sync_state) == 0)
873 			wake_up(&mvm->rx_sync_waitq);
874 	}
875 }
876 
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)877 static void iwl_mvm_oldsn_workaround(struct iwl_mvm *mvm,
878 				     struct ieee80211_sta *sta, int tid,
879 				     struct iwl_mvm_reorder_buffer *buffer,
880 				     u32 reorder, u32 gp2, int queue)
881 {
882 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
883 
884 	if (gp2 != buffer->consec_oldsn_ampdu_gp2) {
885 		/* we have a new (A-)MPDU ... */
886 
887 		/*
888 		 * reset counter to 0 if we didn't have any oldsn in
889 		 * the last A-MPDU (as detected by GP2 being identical)
890 		 */
891 		if (!buffer->consec_oldsn_prev_drop)
892 			buffer->consec_oldsn_drops = 0;
893 
894 		/* either way, update our tracking state */
895 		buffer->consec_oldsn_ampdu_gp2 = gp2;
896 	} else if (buffer->consec_oldsn_prev_drop) {
897 		/*
898 		 * tracking state didn't change, and we had an old SN
899 		 * indication before - do nothing in this case, we
900 		 * already noted this one down and are waiting for the
901 		 * next A-MPDU (by GP2)
902 		 */
903 		return;
904 	}
905 
906 	/* return unless this MPDU has old SN */
907 	if (!(reorder & IWL_RX_MPDU_REORDER_BA_OLD_SN))
908 		return;
909 
910 	/* update state */
911 	buffer->consec_oldsn_prev_drop = 1;
912 	buffer->consec_oldsn_drops++;
913 
914 	/* if limit is reached, send del BA and reset state */
915 	if (buffer->consec_oldsn_drops == IWL_MVM_AMPDU_CONSEC_DROPS_DELBA) {
916 		IWL_WARN(mvm,
917 			 "reached %d old SN frames from %pM on queue %d, stopping BA session on TID %d\n",
918 			 IWL_MVM_AMPDU_CONSEC_DROPS_DELBA,
919 			 sta->addr, queue, tid);
920 		ieee80211_stop_rx_ba_session(mvmsta->vif, BIT(tid), sta->addr);
921 		buffer->consec_oldsn_prev_drop = 0;
922 		buffer->consec_oldsn_drops = 0;
923 	}
924 }
925 
926 /*
927  * Returns true if the MPDU was buffered\dropped, false if it should be passed
928  * to upper layer.
929  */
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)930 static bool iwl_mvm_reorder(struct iwl_mvm *mvm,
931 			    struct napi_struct *napi,
932 			    int queue,
933 			    struct ieee80211_sta *sta,
934 			    struct sk_buff *skb,
935 			    struct iwl_rx_mpdu_desc *desc)
936 {
937 	struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
938 	struct ieee80211_hdr *hdr = (void *)skb_mac_header(skb);
939 	struct iwl_mvm_baid_data *baid_data;
940 	struct iwl_mvm_reorder_buffer *buffer;
941 	struct sk_buff *tail;
942 	u32 reorder = le32_to_cpu(desc->reorder_data);
943 	bool amsdu = desc->mac_flags2 & IWL_RX_MPDU_MFLG2_AMSDU;
944 	bool last_subframe =
945 		desc->amsdu_info & IWL_RX_MPDU_AMSDU_LAST_SUBFRAME;
946 	u8 tid = ieee80211_get_tid(hdr);
947 	u8 sub_frame_idx = desc->amsdu_info &
948 			   IWL_RX_MPDU_AMSDU_SUBFRAME_IDX_MASK;
949 	struct iwl_mvm_reorder_buf_entry *entries;
950 	u32 sta_mask;
951 	int index;
952 	u16 nssn, sn;
953 	u8 baid;
954 
955 	baid = (reorder & IWL_RX_MPDU_REORDER_BAID_MASK) >>
956 		IWL_RX_MPDU_REORDER_BAID_SHIFT;
957 
958 	if (mvm->trans->trans_cfg->device_family == IWL_DEVICE_FAMILY_9000)
959 		return false;
960 
961 	/*
962 	 * This also covers the case of receiving a Block Ack Request
963 	 * outside a BA session; we'll pass it to mac80211 and that
964 	 * then sends a delBA action frame.
965 	 * This also covers pure monitor mode, in which case we won't
966 	 * have any BA sessions.
967 	 */
968 	if (baid == IWL_RX_REORDER_DATA_INVALID_BAID)
969 		return false;
970 
971 	/* no sta yet */
972 	if (WARN_ONCE(IS_ERR_OR_NULL(sta),
973 		      "Got valid BAID without a valid station assigned\n"))
974 		return false;
975 
976 	/* not a data packet or a bar */
977 	if (!ieee80211_is_back_req(hdr->frame_control) &&
978 	    (!ieee80211_is_data_qos(hdr->frame_control) ||
979 	     is_multicast_ether_addr(hdr->addr1)))
980 		return false;
981 
982 	if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
983 		return false;
984 
985 	baid_data = rcu_dereference(mvm->baid_map[baid]);
986 	if (!baid_data) {
987 		IWL_DEBUG_RX(mvm,
988 			     "Got valid BAID but no baid allocated, bypass the re-ordering buffer. Baid %d reorder 0x%x\n",
989 			      baid, reorder);
990 		return false;
991 	}
992 
993 	rcu_read_lock();
994 	sta_mask = iwl_mvm_sta_fw_id_mask(mvm, sta, -1);
995 	rcu_read_unlock();
996 
997 	if (IWL_FW_CHECK(mvm,
998 			 tid != baid_data->tid ||
999 			 !(sta_mask & baid_data->sta_mask),
1000 			 "baid 0x%x is mapped to sta_mask:0x%x tid:%d, but was received for sta_mask:0x%x tid:%d\n",
1001 			 baid, baid_data->sta_mask, baid_data->tid,
1002 			 sta_mask, tid))
1003 		return false;
1004 
1005 	nssn = reorder & IWL_RX_MPDU_REORDER_NSSN_MASK;
1006 	sn = (reorder & IWL_RX_MPDU_REORDER_SN_MASK) >>
1007 		IWL_RX_MPDU_REORDER_SN_SHIFT;
1008 
1009 	buffer = &baid_data->reorder_buf[queue];
1010 	entries = &baid_data->entries[queue * baid_data->entries_per_queue];
1011 
1012 	spin_lock_bh(&buffer->lock);
1013 
1014 	if (!buffer->valid) {
1015 		if (reorder & IWL_RX_MPDU_REORDER_BA_OLD_SN) {
1016 			spin_unlock_bh(&buffer->lock);
1017 			return false;
1018 		}
1019 		buffer->valid = true;
1020 	}
1021 
1022 	if (ieee80211_is_back_req(hdr->frame_control)) {
1023 		iwl_mvm_release_frames(mvm, sta, napi, baid_data,
1024 				       buffer, nssn, 0);
1025 		goto drop;
1026 	}
1027 
1028 	/*
1029 	 * If there was a significant jump in the nssn - adjust.
1030 	 * If the SN is smaller than the NSSN it might need to first go into
1031 	 * the reorder buffer, in which case we just release up to it and the
1032 	 * rest of the function will take care of storing it and releasing up to
1033 	 * the nssn.
1034 	 * This should not happen. This queue has been lagging and it should
1035 	 * have been updated by a IWL_MVM_RXQ_NSSN_SYNC notification. Be nice
1036 	 * and update the other queues.
1037 	 */
1038 	if (!iwl_mvm_is_sn_less(nssn, buffer->head_sn + buffer->buf_size,
1039 				buffer->buf_size) ||
1040 	    !ieee80211_sn_less(sn, buffer->head_sn + buffer->buf_size)) {
1041 		u16 min_sn = ieee80211_sn_less(sn, nssn) ? sn : nssn;
1042 
1043 		iwl_mvm_release_frames(mvm, sta, napi, baid_data, buffer,
1044 				       min_sn, IWL_MVM_RELEASE_SEND_RSS_SYNC);
1045 	}
1046 
1047 	iwl_mvm_oldsn_workaround(mvm, sta, tid, buffer, reorder,
1048 				 rx_status->device_timestamp, queue);
1049 
1050 	/* drop any oudated packets */
1051 	if (ieee80211_sn_less(sn, buffer->head_sn))
1052 		goto drop;
1053 
1054 	/* release immediately if allowed by nssn and no stored frames */
1055 	if (!buffer->num_stored && ieee80211_sn_less(sn, nssn)) {
1056 		if (iwl_mvm_is_sn_less(buffer->head_sn, nssn,
1057 				       buffer->buf_size) &&
1058 		   (!amsdu || last_subframe)) {
1059 			/*
1060 			 * If we crossed the 2048 or 0 SN, notify all the
1061 			 * queues. This is done in order to avoid having a
1062 			 * head_sn that lags behind for too long. When that
1063 			 * happens, we can get to a situation where the head_sn
1064 			 * is within the interval [nssn - buf_size : nssn]
1065 			 * which will make us think that the nssn is a packet
1066 			 * that we already freed because of the reordering
1067 			 * buffer and we will ignore it. So maintain the
1068 			 * head_sn somewhat updated across all the queues:
1069 			 * when it crosses 0 and 2048.
1070 			 */
1071 			if (sn == 2048 || sn == 0)
1072 				iwl_mvm_sync_nssn(mvm, baid, sn);
1073 			buffer->head_sn = nssn;
1074 		}
1075 		/* No need to update AMSDU last SN - we are moving the head */
1076 		spin_unlock_bh(&buffer->lock);
1077 		return false;
1078 	}
1079 
1080 	/*
1081 	 * release immediately if there are no stored frames, and the sn is
1082 	 * equal to the head.
1083 	 * This can happen due to reorder timer, where NSSN is behind head_sn.
1084 	 * When we released everything, and we got the next frame in the
1085 	 * sequence, according to the NSSN we can't release immediately,
1086 	 * while technically there is no hole and we can move forward.
1087 	 */
1088 	if (!buffer->num_stored && sn == buffer->head_sn) {
1089 		if (!amsdu || last_subframe) {
1090 			if (sn == 2048 || sn == 0)
1091 				iwl_mvm_sync_nssn(mvm, baid, sn);
1092 			buffer->head_sn = ieee80211_sn_inc(buffer->head_sn);
1093 		}
1094 		/* No need to update AMSDU last SN - we are moving the head */
1095 		spin_unlock_bh(&buffer->lock);
1096 		return false;
1097 	}
1098 
1099 	index = sn % buffer->buf_size;
1100 
1101 	/*
1102 	 * Check if we already stored this frame
1103 	 * As AMSDU is either received or not as whole, logic is simple:
1104 	 * If we have frames in that position in the buffer and the last frame
1105 	 * originated from AMSDU had a different SN then it is a retransmission.
1106 	 * If it is the same SN then if the subframe index is incrementing it
1107 	 * is the same AMSDU - otherwise it is a retransmission.
1108 	 */
1109 	tail = skb_peek_tail(&entries[index].e.frames);
1110 	if (tail && !amsdu)
1111 		goto drop;
1112 	else if (tail && (sn != buffer->last_amsdu ||
1113 			  buffer->last_sub_index >= sub_frame_idx))
1114 		goto drop;
1115 
1116 	/* put in reorder buffer */
1117 	__skb_queue_tail(&entries[index].e.frames, skb);
1118 	buffer->num_stored++;
1119 	entries[index].e.reorder_time = jiffies;
1120 
1121 	if (amsdu) {
1122 		buffer->last_amsdu = sn;
1123 		buffer->last_sub_index = sub_frame_idx;
1124 	}
1125 
1126 	/*
1127 	 * We cannot trust NSSN for AMSDU sub-frames that are not the last.
1128 	 * The reason is that NSSN advances on the first sub-frame, and may
1129 	 * cause the reorder buffer to advance before all the sub-frames arrive.
1130 	 * Example: reorder buffer contains SN 0 & 2, and we receive AMSDU with
1131 	 * SN 1. NSSN for first sub frame will be 3 with the result of driver
1132 	 * releasing SN 0,1, 2. When sub-frame 1 arrives - reorder buffer is
1133 	 * already ahead and it will be dropped.
1134 	 * If the last sub-frame is not on this queue - we will get frame
1135 	 * release notification with up to date NSSN.
1136 	 */
1137 	if (!amsdu || last_subframe)
1138 		iwl_mvm_release_frames(mvm, sta, napi, baid_data,
1139 				       buffer, nssn,
1140 				       IWL_MVM_RELEASE_SEND_RSS_SYNC);
1141 
1142 	spin_unlock_bh(&buffer->lock);
1143 	return true;
1144 
1145 drop:
1146 	kfree_skb(skb);
1147 	spin_unlock_bh(&buffer->lock);
1148 	return true;
1149 }
1150 
iwl_mvm_agg_rx_received(struct iwl_mvm * mvm,u32 reorder_data,u8 baid)1151 static void iwl_mvm_agg_rx_received(struct iwl_mvm *mvm,
1152 				    u32 reorder_data, u8 baid)
1153 {
1154 	unsigned long now = jiffies;
1155 	unsigned long timeout;
1156 	struct iwl_mvm_baid_data *data;
1157 
1158 	rcu_read_lock();
1159 
1160 	data = rcu_dereference(mvm->baid_map[baid]);
1161 	if (!data) {
1162 		IWL_DEBUG_RX(mvm,
1163 			     "Got valid BAID but no baid allocated, bypass the re-ordering buffer. Baid %d reorder 0x%x\n",
1164 			      baid, reorder_data);
1165 		goto out;
1166 	}
1167 
1168 	if (!data->timeout)
1169 		goto out;
1170 
1171 	timeout = data->timeout;
1172 	/*
1173 	 * Do not update last rx all the time to avoid cache bouncing
1174 	 * between the rx queues.
1175 	 * Update it every timeout. Worst case is the session will
1176 	 * expire after ~ 2 * timeout, which doesn't matter that much.
1177 	 */
1178 	if (time_before(data->last_rx + TU_TO_JIFFIES(timeout), now))
1179 		/* Update is atomic */
1180 		data->last_rx = now;
1181 
1182 out:
1183 	rcu_read_unlock();
1184 }
1185 
iwl_mvm_flip_address(u8 * addr)1186 static void iwl_mvm_flip_address(u8 *addr)
1187 {
1188 	int i;
1189 	u8 mac_addr[ETH_ALEN];
1190 
1191 	for (i = 0; i < ETH_ALEN; i++)
1192 		mac_addr[i] = addr[ETH_ALEN - i - 1];
1193 	ether_addr_copy(addr, mac_addr);
1194 }
1195 
1196 struct iwl_mvm_rx_phy_data {
1197 	enum iwl_rx_phy_info_type info_type;
1198 	__le32 d0, d1, d2, d3, eht_d4, d5;
1199 	__le16 d4;
1200 	bool with_data;
1201 	bool first_subframe;
1202 	__le32 rx_vec[4];
1203 
1204 	u32 rate_n_flags;
1205 	u32 gp2_on_air_rise;
1206 	u16 phy_info;
1207 	u8 energy_a, energy_b;
1208 	u8 channel;
1209 };
1210 
iwl_mvm_decode_he_mu_ext(struct iwl_mvm * mvm,struct iwl_mvm_rx_phy_data * phy_data,struct ieee80211_radiotap_he_mu * he_mu)1211 static void iwl_mvm_decode_he_mu_ext(struct iwl_mvm *mvm,
1212 				     struct iwl_mvm_rx_phy_data *phy_data,
1213 				     struct ieee80211_radiotap_he_mu *he_mu)
1214 {
1215 	u32 phy_data2 = le32_to_cpu(phy_data->d2);
1216 	u32 phy_data3 = le32_to_cpu(phy_data->d3);
1217 	u16 phy_data4 = le16_to_cpu(phy_data->d4);
1218 	u32 rate_n_flags = phy_data->rate_n_flags;
1219 
1220 	if (FIELD_GET(IWL_RX_PHY_DATA4_HE_MU_EXT_CH1_CRC_OK, phy_data4)) {
1221 		he_mu->flags1 |=
1222 			cpu_to_le16(IEEE80211_RADIOTAP_HE_MU_FLAGS1_CH1_RU_KNOWN |
1223 				    IEEE80211_RADIOTAP_HE_MU_FLAGS1_CH1_CTR_26T_RU_KNOWN);
1224 
1225 		he_mu->flags1 |=
1226 			le16_encode_bits(FIELD_GET(IWL_RX_PHY_DATA4_HE_MU_EXT_CH1_CTR_RU,
1227 						   phy_data4),
1228 					 IEEE80211_RADIOTAP_HE_MU_FLAGS1_CH1_CTR_26T_RU);
1229 
1230 		he_mu->ru_ch1[0] = FIELD_GET(IWL_RX_PHY_DATA2_HE_MU_EXT_CH1_RU0,
1231 					     phy_data2);
1232 		he_mu->ru_ch1[1] = FIELD_GET(IWL_RX_PHY_DATA3_HE_MU_EXT_CH1_RU1,
1233 					     phy_data3);
1234 		he_mu->ru_ch1[2] = FIELD_GET(IWL_RX_PHY_DATA2_HE_MU_EXT_CH1_RU2,
1235 					     phy_data2);
1236 		he_mu->ru_ch1[3] = FIELD_GET(IWL_RX_PHY_DATA3_HE_MU_EXT_CH1_RU3,
1237 					     phy_data3);
1238 	}
1239 
1240 	if (FIELD_GET(IWL_RX_PHY_DATA4_HE_MU_EXT_CH2_CRC_OK, phy_data4) &&
1241 	    (rate_n_flags & RATE_MCS_CHAN_WIDTH_MSK_V1) != RATE_MCS_CHAN_WIDTH_20) {
1242 		he_mu->flags1 |=
1243 			cpu_to_le16(IEEE80211_RADIOTAP_HE_MU_FLAGS1_CH2_RU_KNOWN |
1244 				    IEEE80211_RADIOTAP_HE_MU_FLAGS1_CH2_CTR_26T_RU_KNOWN);
1245 
1246 		he_mu->flags2 |=
1247 			le16_encode_bits(FIELD_GET(IWL_RX_PHY_DATA4_HE_MU_EXT_CH2_CTR_RU,
1248 						   phy_data4),
1249 					 IEEE80211_RADIOTAP_HE_MU_FLAGS2_CH2_CTR_26T_RU);
1250 
1251 		he_mu->ru_ch2[0] = FIELD_GET(IWL_RX_PHY_DATA2_HE_MU_EXT_CH2_RU0,
1252 					     phy_data2);
1253 		he_mu->ru_ch2[1] = FIELD_GET(IWL_RX_PHY_DATA3_HE_MU_EXT_CH2_RU1,
1254 					     phy_data3);
1255 		he_mu->ru_ch2[2] = FIELD_GET(IWL_RX_PHY_DATA2_HE_MU_EXT_CH2_RU2,
1256 					     phy_data2);
1257 		he_mu->ru_ch2[3] = FIELD_GET(IWL_RX_PHY_DATA3_HE_MU_EXT_CH2_RU3,
1258 					     phy_data3);
1259 	}
1260 }
1261 
1262 static void
iwl_mvm_decode_he_phy_ru_alloc(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)1263 iwl_mvm_decode_he_phy_ru_alloc(struct iwl_mvm_rx_phy_data *phy_data,
1264 			       struct ieee80211_radiotap_he *he,
1265 			       struct ieee80211_radiotap_he_mu *he_mu,
1266 			       struct ieee80211_rx_status *rx_status)
1267 {
1268 	/*
1269 	 * Unfortunately, we have to leave the mac80211 data
1270 	 * incorrect for the case that we receive an HE-MU
1271 	 * transmission and *don't* have the HE phy data (due
1272 	 * to the bits being used for TSF). This shouldn't
1273 	 * happen though as management frames where we need
1274 	 * the TSF/timers are not be transmitted in HE-MU.
1275 	 */
1276 	u8 ru = le32_get_bits(phy_data->d1, IWL_RX_PHY_DATA1_HE_RU_ALLOC_MASK);
1277 	u32 rate_n_flags = phy_data->rate_n_flags;
1278 	u32 he_type = rate_n_flags & RATE_MCS_HE_TYPE_MSK_V1;
1279 	u8 offs = 0;
1280 
1281 	rx_status->bw = RATE_INFO_BW_HE_RU;
1282 
1283 	he->data1 |= cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA1_BW_RU_ALLOC_KNOWN);
1284 
1285 	switch (ru) {
1286 	case 0 ... 36:
1287 		rx_status->he_ru = NL80211_RATE_INFO_HE_RU_ALLOC_26;
1288 		offs = ru;
1289 		break;
1290 	case 37 ... 52:
1291 		rx_status->he_ru = NL80211_RATE_INFO_HE_RU_ALLOC_52;
1292 		offs = ru - 37;
1293 		break;
1294 	case 53 ... 60:
1295 		rx_status->he_ru = NL80211_RATE_INFO_HE_RU_ALLOC_106;
1296 		offs = ru - 53;
1297 		break;
1298 	case 61 ... 64:
1299 		rx_status->he_ru = NL80211_RATE_INFO_HE_RU_ALLOC_242;
1300 		offs = ru - 61;
1301 		break;
1302 	case 65 ... 66:
1303 		rx_status->he_ru = NL80211_RATE_INFO_HE_RU_ALLOC_484;
1304 		offs = ru - 65;
1305 		break;
1306 	case 67:
1307 		rx_status->he_ru = NL80211_RATE_INFO_HE_RU_ALLOC_996;
1308 		break;
1309 	case 68:
1310 		rx_status->he_ru = NL80211_RATE_INFO_HE_RU_ALLOC_2x996;
1311 		break;
1312 	}
1313 	he->data2 |= le16_encode_bits(offs,
1314 				      IEEE80211_RADIOTAP_HE_DATA2_RU_OFFSET);
1315 	he->data2 |= cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA2_PRISEC_80_KNOWN |
1316 				 IEEE80211_RADIOTAP_HE_DATA2_RU_OFFSET_KNOWN);
1317 	if (phy_data->d1 & cpu_to_le32(IWL_RX_PHY_DATA1_HE_RU_ALLOC_SEC80))
1318 		he->data2 |=
1319 			cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA2_PRISEC_80_SEC);
1320 
1321 #define CHECK_BW(bw) \
1322 	BUILD_BUG_ON(IEEE80211_RADIOTAP_HE_MU_FLAGS2_BW_FROM_SIG_A_BW_ ## bw ## MHZ != \
1323 		     RATE_MCS_CHAN_WIDTH_##bw >> RATE_MCS_CHAN_WIDTH_POS); \
1324 	BUILD_BUG_ON(IEEE80211_RADIOTAP_HE_DATA6_TB_PPDU_BW_ ## bw ## MHZ != \
1325 		     RATE_MCS_CHAN_WIDTH_##bw >> RATE_MCS_CHAN_WIDTH_POS)
1326 	CHECK_BW(20);
1327 	CHECK_BW(40);
1328 	CHECK_BW(80);
1329 	CHECK_BW(160);
1330 
1331 	if (he_mu)
1332 		he_mu->flags2 |=
1333 			le16_encode_bits(FIELD_GET(RATE_MCS_CHAN_WIDTH_MSK_V1,
1334 						   rate_n_flags),
1335 					 IEEE80211_RADIOTAP_HE_MU_FLAGS2_BW_FROM_SIG_A_BW);
1336 	else if (he_type == RATE_MCS_HE_TYPE_TRIG_V1)
1337 		he->data6 |=
1338 			cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA6_TB_PPDU_BW_KNOWN) |
1339 			le16_encode_bits(FIELD_GET(RATE_MCS_CHAN_WIDTH_MSK_V1,
1340 						   rate_n_flags),
1341 					 IEEE80211_RADIOTAP_HE_DATA6_TB_PPDU_BW);
1342 }
1343 
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,int queue)1344 static void iwl_mvm_decode_he_phy_data(struct iwl_mvm *mvm,
1345 				       struct iwl_mvm_rx_phy_data *phy_data,
1346 				       struct ieee80211_radiotap_he *he,
1347 				       struct ieee80211_radiotap_he_mu *he_mu,
1348 				       struct ieee80211_rx_status *rx_status,
1349 				       int queue)
1350 {
1351 	switch (phy_data->info_type) {
1352 	case IWL_RX_PHY_INFO_TYPE_NONE:
1353 	case IWL_RX_PHY_INFO_TYPE_CCK:
1354 	case IWL_RX_PHY_INFO_TYPE_OFDM_LGCY:
1355 	case IWL_RX_PHY_INFO_TYPE_HT:
1356 	case IWL_RX_PHY_INFO_TYPE_VHT_SU:
1357 	case IWL_RX_PHY_INFO_TYPE_VHT_MU:
1358 	case IWL_RX_PHY_INFO_TYPE_EHT_MU:
1359 	case IWL_RX_PHY_INFO_TYPE_EHT_TB:
1360 	case IWL_RX_PHY_INFO_TYPE_EHT_MU_EXT:
1361 	case IWL_RX_PHY_INFO_TYPE_EHT_TB_EXT:
1362 		return;
1363 	case IWL_RX_PHY_INFO_TYPE_HE_TB_EXT:
1364 		he->data1 |= cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA1_SPTL_REUSE_KNOWN |
1365 					 IEEE80211_RADIOTAP_HE_DATA1_SPTL_REUSE2_KNOWN |
1366 					 IEEE80211_RADIOTAP_HE_DATA1_SPTL_REUSE3_KNOWN |
1367 					 IEEE80211_RADIOTAP_HE_DATA1_SPTL_REUSE4_KNOWN);
1368 		he->data4 |= le16_encode_bits(le32_get_bits(phy_data->d2,
1369 							    IWL_RX_PHY_DATA2_HE_TB_EXT_SPTL_REUSE1),
1370 					      IEEE80211_RADIOTAP_HE_DATA4_TB_SPTL_REUSE1);
1371 		he->data4 |= le16_encode_bits(le32_get_bits(phy_data->d2,
1372 							    IWL_RX_PHY_DATA2_HE_TB_EXT_SPTL_REUSE2),
1373 					      IEEE80211_RADIOTAP_HE_DATA4_TB_SPTL_REUSE2);
1374 		he->data4 |= le16_encode_bits(le32_get_bits(phy_data->d2,
1375 							    IWL_RX_PHY_DATA2_HE_TB_EXT_SPTL_REUSE3),
1376 					      IEEE80211_RADIOTAP_HE_DATA4_TB_SPTL_REUSE3);
1377 		he->data4 |= le16_encode_bits(le32_get_bits(phy_data->d2,
1378 							    IWL_RX_PHY_DATA2_HE_TB_EXT_SPTL_REUSE4),
1379 					      IEEE80211_RADIOTAP_HE_DATA4_TB_SPTL_REUSE4);
1380 		fallthrough;
1381 	case IWL_RX_PHY_INFO_TYPE_HE_SU:
1382 	case IWL_RX_PHY_INFO_TYPE_HE_MU:
1383 	case IWL_RX_PHY_INFO_TYPE_HE_MU_EXT:
1384 	case IWL_RX_PHY_INFO_TYPE_HE_TB:
1385 		/* HE common */
1386 		he->data1 |= cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA1_LDPC_XSYMSEG_KNOWN |
1387 					 IEEE80211_RADIOTAP_HE_DATA1_DOPPLER_KNOWN |
1388 					 IEEE80211_RADIOTAP_HE_DATA1_BSS_COLOR_KNOWN);
1389 		he->data2 |= cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA2_PRE_FEC_PAD_KNOWN |
1390 					 IEEE80211_RADIOTAP_HE_DATA2_PE_DISAMBIG_KNOWN |
1391 					 IEEE80211_RADIOTAP_HE_DATA2_TXOP_KNOWN |
1392 					 IEEE80211_RADIOTAP_HE_DATA2_NUM_LTF_SYMS_KNOWN);
1393 		he->data3 |= le16_encode_bits(le32_get_bits(phy_data->d0,
1394 							    IWL_RX_PHY_DATA0_HE_BSS_COLOR_MASK),
1395 					      IEEE80211_RADIOTAP_HE_DATA3_BSS_COLOR);
1396 		if (phy_data->info_type != IWL_RX_PHY_INFO_TYPE_HE_TB &&
1397 		    phy_data->info_type != IWL_RX_PHY_INFO_TYPE_HE_TB_EXT) {
1398 			he->data1 |= cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA1_UL_DL_KNOWN);
1399 			he->data3 |= le16_encode_bits(le32_get_bits(phy_data->d0,
1400 							    IWL_RX_PHY_DATA0_HE_UPLINK),
1401 						      IEEE80211_RADIOTAP_HE_DATA3_UL_DL);
1402 		}
1403 		he->data3 |= le16_encode_bits(le32_get_bits(phy_data->d0,
1404 							    IWL_RX_PHY_DATA0_HE_LDPC_EXT_SYM),
1405 					      IEEE80211_RADIOTAP_HE_DATA3_LDPC_XSYMSEG);
1406 		he->data5 |= le16_encode_bits(le32_get_bits(phy_data->d0,
1407 							    IWL_RX_PHY_DATA0_HE_PRE_FEC_PAD_MASK),
1408 					      IEEE80211_RADIOTAP_HE_DATA5_PRE_FEC_PAD);
1409 		he->data5 |= le16_encode_bits(le32_get_bits(phy_data->d0,
1410 							    IWL_RX_PHY_DATA0_HE_PE_DISAMBIG),
1411 					      IEEE80211_RADIOTAP_HE_DATA5_PE_DISAMBIG);
1412 		he->data5 |= le16_encode_bits(le32_get_bits(phy_data->d1,
1413 							    IWL_RX_PHY_DATA1_HE_LTF_NUM_MASK),
1414 					      IEEE80211_RADIOTAP_HE_DATA5_NUM_LTF_SYMS);
1415 		he->data6 |= le16_encode_bits(le32_get_bits(phy_data->d0,
1416 							    IWL_RX_PHY_DATA0_HE_TXOP_DUR_MASK),
1417 					      IEEE80211_RADIOTAP_HE_DATA6_TXOP);
1418 		he->data6 |= le16_encode_bits(le32_get_bits(phy_data->d0,
1419 							    IWL_RX_PHY_DATA0_HE_DOPPLER),
1420 					      IEEE80211_RADIOTAP_HE_DATA6_DOPPLER);
1421 		break;
1422 	}
1423 
1424 	switch (phy_data->info_type) {
1425 	case IWL_RX_PHY_INFO_TYPE_HE_MU_EXT:
1426 	case IWL_RX_PHY_INFO_TYPE_HE_MU:
1427 	case IWL_RX_PHY_INFO_TYPE_HE_SU:
1428 		he->data1 |= cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA1_SPTL_REUSE_KNOWN);
1429 		he->data4 |= le16_encode_bits(le32_get_bits(phy_data->d0,
1430 							    IWL_RX_PHY_DATA0_HE_SPATIAL_REUSE_MASK),
1431 					      IEEE80211_RADIOTAP_HE_DATA4_SU_MU_SPTL_REUSE);
1432 		break;
1433 	default:
1434 		/* nothing here */
1435 		break;
1436 	}
1437 
1438 	switch (phy_data->info_type) {
1439 	case IWL_RX_PHY_INFO_TYPE_HE_MU_EXT:
1440 		he_mu->flags1 |=
1441 			le16_encode_bits(le16_get_bits(phy_data->d4,
1442 						       IWL_RX_PHY_DATA4_HE_MU_EXT_SIGB_DCM),
1443 					 IEEE80211_RADIOTAP_HE_MU_FLAGS1_SIG_B_DCM);
1444 		he_mu->flags1 |=
1445 			le16_encode_bits(le16_get_bits(phy_data->d4,
1446 						       IWL_RX_PHY_DATA4_HE_MU_EXT_SIGB_MCS_MASK),
1447 					 IEEE80211_RADIOTAP_HE_MU_FLAGS1_SIG_B_MCS);
1448 		he_mu->flags2 |=
1449 			le16_encode_bits(le16_get_bits(phy_data->d4,
1450 						       IWL_RX_PHY_DATA4_HE_MU_EXT_PREAMBLE_PUNC_TYPE_MASK),
1451 					 IEEE80211_RADIOTAP_HE_MU_FLAGS2_PUNC_FROM_SIG_A_BW);
1452 		iwl_mvm_decode_he_mu_ext(mvm, phy_data, he_mu);
1453 		fallthrough;
1454 	case IWL_RX_PHY_INFO_TYPE_HE_MU:
1455 		he_mu->flags2 |=
1456 			le16_encode_bits(le32_get_bits(phy_data->d1,
1457 						       IWL_RX_PHY_DATA1_HE_MU_SIBG_SYM_OR_USER_NUM_MASK),
1458 					 IEEE80211_RADIOTAP_HE_MU_FLAGS2_SIG_B_SYMS_USERS);
1459 		he_mu->flags2 |=
1460 			le16_encode_bits(le32_get_bits(phy_data->d1,
1461 						       IWL_RX_PHY_DATA1_HE_MU_SIGB_COMPRESSION),
1462 					 IEEE80211_RADIOTAP_HE_MU_FLAGS2_SIG_B_COMP);
1463 		fallthrough;
1464 	case IWL_RX_PHY_INFO_TYPE_HE_TB:
1465 	case IWL_RX_PHY_INFO_TYPE_HE_TB_EXT:
1466 		iwl_mvm_decode_he_phy_ru_alloc(phy_data, he, he_mu, rx_status);
1467 		break;
1468 	case IWL_RX_PHY_INFO_TYPE_HE_SU:
1469 		he->data1 |= cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA1_BEAM_CHANGE_KNOWN);
1470 		he->data3 |= le16_encode_bits(le32_get_bits(phy_data->d0,
1471 							    IWL_RX_PHY_DATA0_HE_BEAM_CHNG),
1472 					      IEEE80211_RADIOTAP_HE_DATA3_BEAM_CHANGE);
1473 		break;
1474 	default:
1475 		/* nothing */
1476 		break;
1477 	}
1478 }
1479 
1480 #define LE32_DEC_ENC(value, dec_bits, enc_bits) \
1481 	le32_encode_bits(le32_get_bits(value, dec_bits), enc_bits)
1482 
1483 #define IWL_MVM_ENC_USIG_VALUE_MASK(usig, in_value, dec_bits, enc_bits) do { \
1484 	typeof(enc_bits) _enc_bits = enc_bits; \
1485 	typeof(usig) _usig = usig; \
1486 	(_usig)->mask |= cpu_to_le32(_enc_bits); \
1487 	(_usig)->value |= LE32_DEC_ENC(in_value, dec_bits, _enc_bits); \
1488 } while (0)
1489 
1490 #define __IWL_MVM_ENC_EHT_RU(rt_data, rt_ru, fw_data, fw_ru) \
1491 	eht->data[(rt_data)] |= \
1492 		(cpu_to_le32 \
1493 		 (IEEE80211_RADIOTAP_EHT_DATA ## rt_data ## _RU_ALLOC_CC_ ## rt_ru ## _KNOWN) | \
1494 		 LE32_DEC_ENC(data ## fw_data, \
1495 			      IWL_RX_PHY_DATA ## fw_data ## _EHT_MU_EXT_RU_ALLOC_ ## fw_ru, \
1496 			      IEEE80211_RADIOTAP_EHT_DATA ## rt_data ## _RU_ALLOC_CC_ ## rt_ru))
1497 
1498 #define _IWL_MVM_ENC_EHT_RU(rt_data, rt_ru, fw_data, fw_ru)	\
1499 	__IWL_MVM_ENC_EHT_RU(rt_data, rt_ru, fw_data, fw_ru)
1500 
1501 #define IEEE80211_RADIOTAP_RU_DATA_1_1_1	1
1502 #define IEEE80211_RADIOTAP_RU_DATA_2_1_1	2
1503 #define IEEE80211_RADIOTAP_RU_DATA_1_1_2	2
1504 #define IEEE80211_RADIOTAP_RU_DATA_2_1_2	2
1505 #define IEEE80211_RADIOTAP_RU_DATA_1_2_1	3
1506 #define IEEE80211_RADIOTAP_RU_DATA_2_2_1	3
1507 #define IEEE80211_RADIOTAP_RU_DATA_1_2_2	3
1508 #define IEEE80211_RADIOTAP_RU_DATA_2_2_2	4
1509 
1510 #define IWL_RX_RU_DATA_A1			2
1511 #define IWL_RX_RU_DATA_A2			2
1512 #define IWL_RX_RU_DATA_B1			2
1513 #define IWL_RX_RU_DATA_B2			3
1514 #define IWL_RX_RU_DATA_C1			3
1515 #define IWL_RX_RU_DATA_C2			3
1516 #define IWL_RX_RU_DATA_D1			4
1517 #define IWL_RX_RU_DATA_D2			4
1518 
1519 #define IWL_MVM_ENC_EHT_RU(rt_ru, fw_ru)				\
1520 	_IWL_MVM_ENC_EHT_RU(IEEE80211_RADIOTAP_RU_DATA_ ## rt_ru,	\
1521 			    rt_ru,					\
1522 			    IWL_RX_RU_DATA_ ## fw_ru,			\
1523 			    fw_ru)
1524 
iwl_mvm_decode_eht_ext_mu(struct iwl_mvm * mvm,struct iwl_mvm_rx_phy_data * phy_data,struct ieee80211_rx_status * rx_status,struct ieee80211_radiotap_eht * eht,struct ieee80211_radiotap_eht_usig * usig)1525 static void iwl_mvm_decode_eht_ext_mu(struct iwl_mvm *mvm,
1526 				      struct iwl_mvm_rx_phy_data *phy_data,
1527 				      struct ieee80211_rx_status *rx_status,
1528 				      struct ieee80211_radiotap_eht *eht,
1529 				      struct ieee80211_radiotap_eht_usig *usig)
1530 {
1531 	if (phy_data->with_data) {
1532 		__le32 data1 = phy_data->d1;
1533 		__le32 data2 = phy_data->d2;
1534 		__le32 data3 = phy_data->d3;
1535 		__le32 data4 = phy_data->eht_d4;
1536 		__le32 data5 = phy_data->d5;
1537 		u32 phy_bw = phy_data->rate_n_flags & RATE_MCS_CHAN_WIDTH_MSK;
1538 
1539 		IWL_MVM_ENC_USIG_VALUE_MASK(usig, data5,
1540 					    IWL_RX_PHY_DATA5_EHT_TYPE_AND_COMP,
1541 					    IEEE80211_RADIOTAP_EHT_USIG2_MU_B0_B1_PPDU_TYPE);
1542 		IWL_MVM_ENC_USIG_VALUE_MASK(usig, data5,
1543 					    IWL_RX_PHY_DATA5_EHT_MU_PUNC_CH_CODE,
1544 					    IEEE80211_RADIOTAP_EHT_USIG2_MU_B3_B7_PUNCTURED_INFO);
1545 		IWL_MVM_ENC_USIG_VALUE_MASK(usig, data4,
1546 					    IWL_RX_PHY_DATA4_EHT_MU_EXT_SIGB_MCS,
1547 					    IEEE80211_RADIOTAP_EHT_USIG2_MU_B9_B10_SIG_MCS);
1548 		IWL_MVM_ENC_USIG_VALUE_MASK
1549 			(usig, data1, IWL_RX_PHY_DATA1_EHT_MU_NUM_SIG_SYM_USIGA2,
1550 			 IEEE80211_RADIOTAP_EHT_USIG2_MU_B11_B15_EHT_SIG_SYMBOLS);
1551 
1552 		eht->user_info[0] |=
1553 			cpu_to_le32(IEEE80211_RADIOTAP_EHT_USER_INFO_STA_ID_KNOWN) |
1554 			LE32_DEC_ENC(data5, IWL_RX_PHY_DATA5_EHT_MU_STA_ID_USR,
1555 				     IEEE80211_RADIOTAP_EHT_USER_INFO_STA_ID);
1556 
1557 		eht->known |= cpu_to_le32(IEEE80211_RADIOTAP_EHT_KNOWN_NR_NON_OFDMA_USERS_M);
1558 		eht->data[7] |= LE32_DEC_ENC
1559 			(data5, IWL_RX_PHY_DATA5_EHT_MU_NUM_USR_NON_OFDMA,
1560 			 IEEE80211_RADIOTAP_EHT_DATA7_NUM_OF_NON_OFDMA_USERS);
1561 
1562 		/*
1563 		 * Hardware labels the content channels/RU allocation values
1564 		 * as follows:
1565 		 *           Content Channel 1		Content Channel 2
1566 		 *   20 MHz: A1
1567 		 *   40 MHz: A1				B1
1568 		 *   80 MHz: A1 C1			B1 D1
1569 		 *  160 MHz: A1 C1 A2 C2		B1 D1 B2 D2
1570 		 *  320 MHz: A1 C1 A2 C2 A3 C3 A4 C4	B1 D1 B2 D2 B3 D3 B4 D4
1571 		 *
1572 		 * However firmware can only give us A1-D2, so the higher
1573 		 * frequencies are missing.
1574 		 */
1575 
1576 		switch (phy_bw) {
1577 		case RATE_MCS_CHAN_WIDTH_320:
1578 			/* additional values are missing in RX metadata */
1579 		case RATE_MCS_CHAN_WIDTH_160:
1580 			/* content channel 1 */
1581 			IWL_MVM_ENC_EHT_RU(1_2_1, A2);
1582 			IWL_MVM_ENC_EHT_RU(1_2_2, C2);
1583 			/* content channel 2 */
1584 			IWL_MVM_ENC_EHT_RU(2_2_1, B2);
1585 			IWL_MVM_ENC_EHT_RU(2_2_2, D2);
1586 			fallthrough;
1587 		case RATE_MCS_CHAN_WIDTH_80:
1588 			/* content channel 1 */
1589 			IWL_MVM_ENC_EHT_RU(1_1_2, C1);
1590 			/* content channel 2 */
1591 			IWL_MVM_ENC_EHT_RU(2_1_2, D1);
1592 			fallthrough;
1593 		case RATE_MCS_CHAN_WIDTH_40:
1594 			/* content channel 2 */
1595 			IWL_MVM_ENC_EHT_RU(2_1_1, B1);
1596 			fallthrough;
1597 		case RATE_MCS_CHAN_WIDTH_20:
1598 			IWL_MVM_ENC_EHT_RU(1_1_1, A1);
1599 			break;
1600 		}
1601 	} else {
1602 		__le32 usig_a1 = phy_data->rx_vec[0];
1603 		__le32 usig_a2 = phy_data->rx_vec[1];
1604 
1605 		IWL_MVM_ENC_USIG_VALUE_MASK(usig, usig_a1,
1606 					    IWL_RX_USIG_A1_DISREGARD,
1607 					    IEEE80211_RADIOTAP_EHT_USIG1_MU_B20_B24_DISREGARD);
1608 		IWL_MVM_ENC_USIG_VALUE_MASK(usig, usig_a1,
1609 					    IWL_RX_USIG_A1_VALIDATE,
1610 					    IEEE80211_RADIOTAP_EHT_USIG1_MU_B25_VALIDATE);
1611 		IWL_MVM_ENC_USIG_VALUE_MASK(usig, usig_a2,
1612 					    IWL_RX_USIG_A2_EHT_PPDU_TYPE,
1613 					    IEEE80211_RADIOTAP_EHT_USIG2_MU_B0_B1_PPDU_TYPE);
1614 		IWL_MVM_ENC_USIG_VALUE_MASK(usig, usig_a2,
1615 					    IWL_RX_USIG_A2_EHT_USIG2_VALIDATE_B2,
1616 					    IEEE80211_RADIOTAP_EHT_USIG2_MU_B2_VALIDATE);
1617 		IWL_MVM_ENC_USIG_VALUE_MASK(usig, usig_a2,
1618 					    IWL_RX_USIG_A2_EHT_PUNC_CHANNEL,
1619 					    IEEE80211_RADIOTAP_EHT_USIG2_MU_B3_B7_PUNCTURED_INFO);
1620 		IWL_MVM_ENC_USIG_VALUE_MASK(usig, usig_a2,
1621 					    IWL_RX_USIG_A2_EHT_USIG2_VALIDATE_B8,
1622 					    IEEE80211_RADIOTAP_EHT_USIG2_MU_B8_VALIDATE);
1623 		IWL_MVM_ENC_USIG_VALUE_MASK(usig, usig_a2,
1624 					    IWL_RX_USIG_A2_EHT_SIG_MCS,
1625 					    IEEE80211_RADIOTAP_EHT_USIG2_MU_B9_B10_SIG_MCS);
1626 		IWL_MVM_ENC_USIG_VALUE_MASK
1627 			(usig, usig_a2, IWL_RX_USIG_A2_EHT_SIG_SYM_NUM,
1628 			 IEEE80211_RADIOTAP_EHT_USIG2_MU_B11_B15_EHT_SIG_SYMBOLS);
1629 		IWL_MVM_ENC_USIG_VALUE_MASK(usig, usig_a2,
1630 					    IWL_RX_USIG_A2_EHT_CRC_OK,
1631 					    IEEE80211_RADIOTAP_EHT_USIG2_MU_B16_B19_CRC);
1632 	}
1633 }
1634 
iwl_mvm_decode_eht_ext_tb(struct iwl_mvm * mvm,struct iwl_mvm_rx_phy_data * phy_data,struct ieee80211_rx_status * rx_status,struct ieee80211_radiotap_eht * eht,struct ieee80211_radiotap_eht_usig * usig)1635 static void iwl_mvm_decode_eht_ext_tb(struct iwl_mvm *mvm,
1636 				      struct iwl_mvm_rx_phy_data *phy_data,
1637 				      struct ieee80211_rx_status *rx_status,
1638 				      struct ieee80211_radiotap_eht *eht,
1639 				      struct ieee80211_radiotap_eht_usig *usig)
1640 {
1641 	if (phy_data->with_data) {
1642 		__le32 data5 = phy_data->d5;
1643 
1644 		IWL_MVM_ENC_USIG_VALUE_MASK(usig, data5,
1645 					    IWL_RX_PHY_DATA5_EHT_TYPE_AND_COMP,
1646 					    IEEE80211_RADIOTAP_EHT_USIG2_TB_B0_B1_PPDU_TYPE);
1647 		IWL_MVM_ENC_USIG_VALUE_MASK(usig, data5,
1648 					    IWL_RX_PHY_DATA5_EHT_TB_SPATIAL_REUSE1,
1649 					    IEEE80211_RADIOTAP_EHT_USIG2_TB_B3_B6_SPATIAL_REUSE_1);
1650 
1651 		IWL_MVM_ENC_USIG_VALUE_MASK(usig, data5,
1652 					    IWL_RX_PHY_DATA5_EHT_TB_SPATIAL_REUSE2,
1653 					    IEEE80211_RADIOTAP_EHT_USIG2_TB_B7_B10_SPATIAL_REUSE_2);
1654 	} else {
1655 		__le32 usig_a1 = phy_data->rx_vec[0];
1656 		__le32 usig_a2 = phy_data->rx_vec[1];
1657 
1658 		IWL_MVM_ENC_USIG_VALUE_MASK(usig, usig_a1,
1659 					    IWL_RX_USIG_A1_DISREGARD,
1660 					    IEEE80211_RADIOTAP_EHT_USIG1_TB_B20_B25_DISREGARD);
1661 		IWL_MVM_ENC_USIG_VALUE_MASK(usig, usig_a2,
1662 					    IWL_RX_USIG_A2_EHT_PPDU_TYPE,
1663 					    IEEE80211_RADIOTAP_EHT_USIG2_TB_B0_B1_PPDU_TYPE);
1664 		IWL_MVM_ENC_USIG_VALUE_MASK(usig, usig_a2,
1665 					    IWL_RX_USIG_A2_EHT_USIG2_VALIDATE_B2,
1666 					    IEEE80211_RADIOTAP_EHT_USIG2_TB_B2_VALIDATE);
1667 		IWL_MVM_ENC_USIG_VALUE_MASK(usig, usig_a2,
1668 					    IWL_RX_USIG_A2_EHT_TRIG_SPATIAL_REUSE_1,
1669 					    IEEE80211_RADIOTAP_EHT_USIG2_TB_B3_B6_SPATIAL_REUSE_1);
1670 		IWL_MVM_ENC_USIG_VALUE_MASK(usig, usig_a2,
1671 					    IWL_RX_USIG_A2_EHT_TRIG_SPATIAL_REUSE_2,
1672 					    IEEE80211_RADIOTAP_EHT_USIG2_TB_B7_B10_SPATIAL_REUSE_2);
1673 		IWL_MVM_ENC_USIG_VALUE_MASK(usig, usig_a2,
1674 					    IWL_RX_USIG_A2_EHT_TRIG_USIG2_DISREGARD,
1675 					    IEEE80211_RADIOTAP_EHT_USIG2_TB_B11_B15_DISREGARD);
1676 		IWL_MVM_ENC_USIG_VALUE_MASK(usig, usig_a2,
1677 					    IWL_RX_USIG_A2_EHT_CRC_OK,
1678 					    IEEE80211_RADIOTAP_EHT_USIG2_TB_B16_B19_CRC);
1679 	}
1680 }
1681 
iwl_mvm_decode_eht_ru(struct iwl_mvm * mvm,struct ieee80211_rx_status * rx_status,struct ieee80211_radiotap_eht * eht)1682 static void iwl_mvm_decode_eht_ru(struct iwl_mvm *mvm,
1683 				  struct ieee80211_rx_status *rx_status,
1684 				  struct ieee80211_radiotap_eht *eht)
1685 {
1686 	u32 ru = le32_get_bits(eht->data[8],
1687 			       IEEE80211_RADIOTAP_EHT_DATA8_RU_ALLOC_TB_FMT_B7_B1);
1688 	enum nl80211_eht_ru_alloc nl_ru;
1689 
1690 	/* Using D1.5 Table 9-53a - Encoding of PS160 and RU Allocation subfields
1691 	 * in an EHT variant User Info field
1692 	 */
1693 
1694 	switch (ru) {
1695 	case 0 ... 36:
1696 		nl_ru = NL80211_RATE_INFO_EHT_RU_ALLOC_26;
1697 		break;
1698 	case 37 ... 52:
1699 		nl_ru = NL80211_RATE_INFO_EHT_RU_ALLOC_52;
1700 		break;
1701 	case 53 ... 60:
1702 		nl_ru = NL80211_RATE_INFO_EHT_RU_ALLOC_106;
1703 		break;
1704 	case 61 ... 64:
1705 		nl_ru = NL80211_RATE_INFO_EHT_RU_ALLOC_242;
1706 		break;
1707 	case 65 ... 66:
1708 		nl_ru = NL80211_RATE_INFO_EHT_RU_ALLOC_484;
1709 		break;
1710 	case 67:
1711 		nl_ru = NL80211_RATE_INFO_EHT_RU_ALLOC_996;
1712 		break;
1713 	case 68:
1714 		nl_ru = NL80211_RATE_INFO_EHT_RU_ALLOC_2x996;
1715 		break;
1716 	case 69:
1717 		nl_ru = NL80211_RATE_INFO_EHT_RU_ALLOC_4x996;
1718 		break;
1719 	case 70 ... 81:
1720 		nl_ru = NL80211_RATE_INFO_EHT_RU_ALLOC_52P26;
1721 		break;
1722 	case 82 ... 89:
1723 		nl_ru = NL80211_RATE_INFO_EHT_RU_ALLOC_106P26;
1724 		break;
1725 	case 90 ... 93:
1726 		nl_ru = NL80211_RATE_INFO_EHT_RU_ALLOC_484P242;
1727 		break;
1728 	case 94 ... 95:
1729 		nl_ru = NL80211_RATE_INFO_EHT_RU_ALLOC_996P484;
1730 		break;
1731 	case 96 ... 99:
1732 		nl_ru = NL80211_RATE_INFO_EHT_RU_ALLOC_996P484P242;
1733 		break;
1734 	case 100 ... 103:
1735 		nl_ru = NL80211_RATE_INFO_EHT_RU_ALLOC_2x996P484;
1736 		break;
1737 	case 104:
1738 		nl_ru = NL80211_RATE_INFO_EHT_RU_ALLOC_3x996;
1739 		break;
1740 	case 105 ... 106:
1741 		nl_ru = NL80211_RATE_INFO_EHT_RU_ALLOC_3x996P484;
1742 		break;
1743 	default:
1744 		return;
1745 	}
1746 
1747 	rx_status->bw = RATE_INFO_BW_EHT_RU;
1748 	rx_status->eht.ru = nl_ru;
1749 }
1750 
iwl_mvm_decode_eht_phy_data(struct iwl_mvm * mvm,struct iwl_mvm_rx_phy_data * phy_data,struct ieee80211_rx_status * rx_status,struct ieee80211_radiotap_eht * eht,struct ieee80211_radiotap_eht_usig * usig)1751 static void iwl_mvm_decode_eht_phy_data(struct iwl_mvm *mvm,
1752 					struct iwl_mvm_rx_phy_data *phy_data,
1753 					struct ieee80211_rx_status *rx_status,
1754 					struct ieee80211_radiotap_eht *eht,
1755 					struct ieee80211_radiotap_eht_usig *usig)
1756 
1757 {
1758 	__le32 data0 = phy_data->d0;
1759 	__le32 data1 = phy_data->d1;
1760 	__le32 usig_a1 = phy_data->rx_vec[0];
1761 	u8 info_type = phy_data->info_type;
1762 
1763 	/* Not in EHT range */
1764 	if (info_type < IWL_RX_PHY_INFO_TYPE_EHT_MU ||
1765 	    info_type > IWL_RX_PHY_INFO_TYPE_EHT_TB_EXT)
1766 		return;
1767 
1768 	usig->common |= cpu_to_le32
1769 		(IEEE80211_RADIOTAP_EHT_USIG_COMMON_UL_DL_KNOWN |
1770 		 IEEE80211_RADIOTAP_EHT_USIG_COMMON_BSS_COLOR_KNOWN);
1771 	if (phy_data->with_data) {
1772 		usig->common |= LE32_DEC_ENC(data0,
1773 					     IWL_RX_PHY_DATA0_EHT_UPLINK,
1774 					     IEEE80211_RADIOTAP_EHT_USIG_COMMON_UL_DL);
1775 		usig->common |= LE32_DEC_ENC(data0,
1776 					     IWL_RX_PHY_DATA0_EHT_BSS_COLOR_MASK,
1777 					     IEEE80211_RADIOTAP_EHT_USIG_COMMON_BSS_COLOR);
1778 	} else {
1779 		usig->common |= LE32_DEC_ENC(usig_a1,
1780 					     IWL_RX_USIG_A1_UL_FLAG,
1781 					     IEEE80211_RADIOTAP_EHT_USIG_COMMON_UL_DL);
1782 		usig->common |= LE32_DEC_ENC(usig_a1,
1783 					     IWL_RX_USIG_A1_BSS_COLOR,
1784 					     IEEE80211_RADIOTAP_EHT_USIG_COMMON_BSS_COLOR);
1785 	}
1786 
1787 	if (fw_has_capa(&mvm->fw->ucode_capa,
1788 			IWL_UCODE_TLV_CAPA_SNIFF_VALIDATE_SUPPORT)) {
1789 		usig->common |=
1790 			cpu_to_le32(IEEE80211_RADIOTAP_EHT_USIG_COMMON_VALIDATE_BITS_CHECKED);
1791 		usig->common |=
1792 			LE32_DEC_ENC(data0, IWL_RX_PHY_DATA0_EHT_VALIDATE,
1793 				     IEEE80211_RADIOTAP_EHT_USIG_COMMON_VALIDATE_BITS_OK);
1794 	}
1795 
1796 	eht->known |= cpu_to_le32(IEEE80211_RADIOTAP_EHT_KNOWN_SPATIAL_REUSE);
1797 	eht->data[0] |= LE32_DEC_ENC(data0,
1798 				     IWL_RX_PHY_DATA0_ETH_SPATIAL_REUSE_MASK,
1799 				     IEEE80211_RADIOTAP_EHT_DATA0_SPATIAL_REUSE);
1800 
1801 	/* All RU allocating size/index is in TB format */
1802 	eht->known |= cpu_to_le32(IEEE80211_RADIOTAP_EHT_KNOWN_RU_ALLOC_TB_FMT);
1803 	eht->data[8] |= LE32_DEC_ENC(data0, IWL_RX_PHY_DATA0_EHT_PS160,
1804 				     IEEE80211_RADIOTAP_EHT_DATA8_RU_ALLOC_TB_FMT_PS_160);
1805 	eht->data[8] |= LE32_DEC_ENC(data1, IWL_RX_PHY_DATA1_EHT_RU_ALLOC_B0,
1806 				     IEEE80211_RADIOTAP_EHT_DATA8_RU_ALLOC_TB_FMT_B0);
1807 	eht->data[8] |= LE32_DEC_ENC(data1, IWL_RX_PHY_DATA1_EHT_RU_ALLOC_B1_B7,
1808 				     IEEE80211_RADIOTAP_EHT_DATA8_RU_ALLOC_TB_FMT_B7_B1);
1809 
1810 	iwl_mvm_decode_eht_ru(mvm, rx_status, eht);
1811 
1812 	/* We only get here in case of IWL_RX_MPDU_PHY_TSF_OVERLOAD is set
1813 	 * which is on only in case of monitor mode so no need to check monitor
1814 	 * mode
1815 	 */
1816 	eht->known |= cpu_to_le32(IEEE80211_RADIOTAP_EHT_KNOWN_PRIMARY_80);
1817 	eht->data[1] |=
1818 		le32_encode_bits(mvm->monitor_p80,
1819 				 IEEE80211_RADIOTAP_EHT_DATA1_PRIMARY_80);
1820 
1821 	usig->common |= cpu_to_le32(IEEE80211_RADIOTAP_EHT_USIG_COMMON_TXOP_KNOWN);
1822 	if (phy_data->with_data)
1823 		usig->common |= LE32_DEC_ENC(data0, IWL_RX_PHY_DATA0_EHT_TXOP_DUR_MASK,
1824 					     IEEE80211_RADIOTAP_EHT_USIG_COMMON_TXOP);
1825 	else
1826 		usig->common |= LE32_DEC_ENC(usig_a1, IWL_RX_USIG_A1_TXOP_DURATION,
1827 					     IEEE80211_RADIOTAP_EHT_USIG_COMMON_TXOP);
1828 
1829 	eht->known |= cpu_to_le32(IEEE80211_RADIOTAP_EHT_KNOWN_LDPC_EXTRA_SYM_OM);
1830 	eht->data[0] |= LE32_DEC_ENC(data0, IWL_RX_PHY_DATA0_EHT_LDPC_EXT_SYM,
1831 				     IEEE80211_RADIOTAP_EHT_DATA0_LDPC_EXTRA_SYM_OM);
1832 
1833 	eht->known |= cpu_to_le32(IEEE80211_RADIOTAP_EHT_KNOWN_PRE_PADD_FACOR_OM);
1834 	eht->data[0] |= LE32_DEC_ENC(data0, IWL_RX_PHY_DATA0_EHT_PRE_FEC_PAD_MASK,
1835 				    IEEE80211_RADIOTAP_EHT_DATA0_PRE_PADD_FACOR_OM);
1836 
1837 	eht->known |= cpu_to_le32(IEEE80211_RADIOTAP_EHT_KNOWN_PE_DISAMBIGUITY_OM);
1838 	eht->data[0] |= LE32_DEC_ENC(data0, IWL_RX_PHY_DATA0_EHT_PE_DISAMBIG,
1839 				     IEEE80211_RADIOTAP_EHT_DATA0_PE_DISAMBIGUITY_OM);
1840 
1841 	/* TODO: what about IWL_RX_PHY_DATA0_EHT_BW320_SLOT */
1842 
1843 	if (!le32_get_bits(data0, IWL_RX_PHY_DATA0_EHT_SIGA_CRC_OK))
1844 		usig->common |= cpu_to_le32(IEEE80211_RADIOTAP_EHT_USIG_COMMON_BAD_USIG_CRC);
1845 
1846 	usig->common |= cpu_to_le32(IEEE80211_RADIOTAP_EHT_USIG_COMMON_PHY_VER_KNOWN);
1847 	usig->common |= LE32_DEC_ENC(data0, IWL_RX_PHY_DATA0_EHT_PHY_VER,
1848 				     IEEE80211_RADIOTAP_EHT_USIG_COMMON_PHY_VER);
1849 
1850 	/*
1851 	 * TODO: what about TB - IWL_RX_PHY_DATA1_EHT_TB_PILOT_TYPE,
1852 	 *			 IWL_RX_PHY_DATA1_EHT_TB_LOW_SS
1853 	 */
1854 
1855 	eht->known |= cpu_to_le32(IEEE80211_RADIOTAP_EHT_KNOWN_EHT_LTF);
1856 	eht->data[0] |= LE32_DEC_ENC(data1, IWL_RX_PHY_DATA1_EHT_SIG_LTF_NUM,
1857 				     IEEE80211_RADIOTAP_EHT_DATA0_EHT_LTF);
1858 
1859 	if (info_type == IWL_RX_PHY_INFO_TYPE_EHT_TB_EXT ||
1860 	    info_type == IWL_RX_PHY_INFO_TYPE_EHT_TB)
1861 		iwl_mvm_decode_eht_ext_tb(mvm, phy_data, rx_status, eht, usig);
1862 
1863 	if (info_type == IWL_RX_PHY_INFO_TYPE_EHT_MU_EXT ||
1864 	    info_type == IWL_RX_PHY_INFO_TYPE_EHT_MU)
1865 		iwl_mvm_decode_eht_ext_mu(mvm, phy_data, rx_status, eht, usig);
1866 }
1867 
iwl_mvm_rx_eht(struct iwl_mvm * mvm,struct sk_buff * skb,struct iwl_mvm_rx_phy_data * phy_data,int queue)1868 static void iwl_mvm_rx_eht(struct iwl_mvm *mvm, struct sk_buff *skb,
1869 			   struct iwl_mvm_rx_phy_data *phy_data,
1870 			   int queue)
1871 {
1872 	struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
1873 
1874 	struct ieee80211_radiotap_eht *eht;
1875 	struct ieee80211_radiotap_eht_usig *usig;
1876 	size_t eht_len = sizeof(*eht);
1877 
1878 	u32 rate_n_flags = phy_data->rate_n_flags;
1879 	u32 he_type = rate_n_flags & RATE_MCS_HE_TYPE_MSK;
1880 	/* EHT and HE have the same valus for LTF */
1881 	u8 ltf = IEEE80211_RADIOTAP_HE_DATA5_LTF_SIZE_UNKNOWN;
1882 	u16 phy_info = phy_data->phy_info;
1883 	u32 bw;
1884 
1885 	/* u32 for 1 user_info */
1886 	if (phy_data->with_data)
1887 		eht_len += sizeof(u32);
1888 
1889 	eht = iwl_mvm_radiotap_put_tlv(skb, IEEE80211_RADIOTAP_EHT, eht_len);
1890 
1891 	usig = iwl_mvm_radiotap_put_tlv(skb, IEEE80211_RADIOTAP_EHT_USIG,
1892 					sizeof(*usig));
1893 	rx_status->flag |= RX_FLAG_RADIOTAP_TLV_AT_END;
1894 	usig->common |=
1895 		cpu_to_le32(IEEE80211_RADIOTAP_EHT_USIG_COMMON_BW_KNOWN);
1896 
1897 	/* specific handling for 320MHz */
1898 	bw = FIELD_GET(RATE_MCS_CHAN_WIDTH_MSK, rate_n_flags);
1899 	if (bw == RATE_MCS_CHAN_WIDTH_320_VAL)
1900 		bw += FIELD_GET(IWL_RX_PHY_DATA0_EHT_BW320_SLOT,
1901 				le32_to_cpu(phy_data->d0));
1902 
1903 	usig->common |= cpu_to_le32
1904 		(FIELD_PREP(IEEE80211_RADIOTAP_EHT_USIG_COMMON_BW, bw));
1905 
1906 	/* report the AMPDU-EOF bit on single frames */
1907 	if (!queue && !(phy_info & IWL_RX_MPDU_PHY_AMPDU)) {
1908 		rx_status->flag |= RX_FLAG_AMPDU_DETAILS;
1909 		rx_status->flag |= RX_FLAG_AMPDU_EOF_BIT_KNOWN;
1910 		if (phy_data->d0 & cpu_to_le32(IWL_RX_PHY_DATA0_EHT_DELIM_EOF))
1911 			rx_status->flag |= RX_FLAG_AMPDU_EOF_BIT;
1912 	}
1913 
1914 	/* update aggregation data for monitor sake on default queue */
1915 	if (!queue && (phy_info & IWL_RX_MPDU_PHY_TSF_OVERLOAD) &&
1916 	    (phy_info & IWL_RX_MPDU_PHY_AMPDU) && phy_data->first_subframe) {
1917 		rx_status->flag |= RX_FLAG_AMPDU_EOF_BIT_KNOWN;
1918 		if (phy_data->d0 & cpu_to_le32(IWL_RX_PHY_DATA0_EHT_DELIM_EOF))
1919 			rx_status->flag |= RX_FLAG_AMPDU_EOF_BIT;
1920 	}
1921 
1922 	if (phy_info & IWL_RX_MPDU_PHY_TSF_OVERLOAD)
1923 		iwl_mvm_decode_eht_phy_data(mvm, phy_data, rx_status, eht, usig);
1924 
1925 #define CHECK_TYPE(F)							\
1926 	BUILD_BUG_ON(IEEE80211_RADIOTAP_HE_DATA1_FORMAT_ ## F !=	\
1927 		     (RATE_MCS_HE_TYPE_ ## F >> RATE_MCS_HE_TYPE_POS))
1928 
1929 	CHECK_TYPE(SU);
1930 	CHECK_TYPE(EXT_SU);
1931 	CHECK_TYPE(MU);
1932 	CHECK_TYPE(TRIG);
1933 
1934 	switch (FIELD_GET(RATE_MCS_HE_GI_LTF_MSK, rate_n_flags)) {
1935 	case 0:
1936 		if (he_type == RATE_MCS_HE_TYPE_TRIG) {
1937 			rx_status->eht.gi = NL80211_RATE_INFO_EHT_GI_1_6;
1938 			ltf = IEEE80211_RADIOTAP_HE_DATA5_LTF_SIZE_1X;
1939 		} else {
1940 			rx_status->eht.gi = NL80211_RATE_INFO_EHT_GI_0_8;
1941 			ltf = IEEE80211_RADIOTAP_HE_DATA5_LTF_SIZE_2X;
1942 		}
1943 		break;
1944 	case 1:
1945 		rx_status->eht.gi = NL80211_RATE_INFO_EHT_GI_1_6;
1946 		ltf = IEEE80211_RADIOTAP_HE_DATA5_LTF_SIZE_2X;
1947 		break;
1948 	case 2:
1949 		ltf = IEEE80211_RADIOTAP_HE_DATA5_LTF_SIZE_4X;
1950 		if (he_type == RATE_MCS_HE_TYPE_TRIG)
1951 			rx_status->eht.gi = NL80211_RATE_INFO_EHT_GI_3_2;
1952 		else
1953 			rx_status->eht.gi = NL80211_RATE_INFO_EHT_GI_0_8;
1954 		break;
1955 	case 3:
1956 		if (he_type != RATE_MCS_HE_TYPE_TRIG) {
1957 			ltf = IEEE80211_RADIOTAP_HE_DATA5_LTF_SIZE_4X;
1958 			rx_status->eht.gi = NL80211_RATE_INFO_EHT_GI_3_2;
1959 		}
1960 		break;
1961 	default:
1962 		/* nothing here */
1963 		break;
1964 	}
1965 
1966 	if (ltf != IEEE80211_RADIOTAP_HE_DATA5_LTF_SIZE_UNKNOWN) {
1967 		eht->known |= cpu_to_le32(IEEE80211_RADIOTAP_EHT_KNOWN_GI);
1968 		eht->data[0] |= cpu_to_le32
1969 			(FIELD_PREP(IEEE80211_RADIOTAP_EHT_DATA0_LTF,
1970 				    ltf) |
1971 			 FIELD_PREP(IEEE80211_RADIOTAP_EHT_DATA0_GI,
1972 				    rx_status->eht.gi));
1973 	}
1974 
1975 
1976 	if (!phy_data->with_data) {
1977 		eht->known |= cpu_to_le32(IEEE80211_RADIOTAP_EHT_KNOWN_NSS_S |
1978 					  IEEE80211_RADIOTAP_EHT_KNOWN_BEAMFORMED_S);
1979 		eht->data[7] |=
1980 			le32_encode_bits(le32_get_bits(phy_data->rx_vec[2],
1981 						       RX_NO_DATA_RX_VEC2_EHT_NSTS_MSK),
1982 					 IEEE80211_RADIOTAP_EHT_DATA7_NSS_S);
1983 		if (rate_n_flags & RATE_MCS_BF_MSK)
1984 			eht->data[7] |=
1985 				cpu_to_le32(IEEE80211_RADIOTAP_EHT_DATA7_BEAMFORMED_S);
1986 	} else {
1987 		eht->user_info[0] |=
1988 			cpu_to_le32(IEEE80211_RADIOTAP_EHT_USER_INFO_MCS_KNOWN |
1989 				    IEEE80211_RADIOTAP_EHT_USER_INFO_CODING_KNOWN |
1990 				    IEEE80211_RADIOTAP_EHT_USER_INFO_NSS_KNOWN_O |
1991 				    IEEE80211_RADIOTAP_EHT_USER_INFO_BEAMFORMING_KNOWN_O |
1992 				    IEEE80211_RADIOTAP_EHT_USER_INFO_DATA_FOR_USER);
1993 
1994 		if (rate_n_flags & RATE_MCS_BF_MSK)
1995 			eht->user_info[0] |=
1996 				cpu_to_le32(IEEE80211_RADIOTAP_EHT_USER_INFO_BEAMFORMING_O);
1997 
1998 		if (rate_n_flags & RATE_MCS_LDPC_MSK)
1999 			eht->user_info[0] |=
2000 				cpu_to_le32(IEEE80211_RADIOTAP_EHT_USER_INFO_CODING);
2001 
2002 		eht->user_info[0] |= cpu_to_le32
2003 			(FIELD_PREP(IEEE80211_RADIOTAP_EHT_USER_INFO_MCS,
2004 				    FIELD_GET(RATE_VHT_MCS_RATE_CODE_MSK,
2005 					      rate_n_flags)) |
2006 			 FIELD_PREP(IEEE80211_RADIOTAP_EHT_USER_INFO_NSS_O,
2007 				    FIELD_GET(RATE_MCS_NSS_MSK, rate_n_flags)));
2008 	}
2009 }
2010 
iwl_mvm_rx_he(struct iwl_mvm * mvm,struct sk_buff * skb,struct iwl_mvm_rx_phy_data * phy_data,int queue)2011 static void iwl_mvm_rx_he(struct iwl_mvm *mvm, struct sk_buff *skb,
2012 			  struct iwl_mvm_rx_phy_data *phy_data,
2013 			  int queue)
2014 {
2015 	struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
2016 	struct ieee80211_radiotap_he *he = NULL;
2017 	struct ieee80211_radiotap_he_mu *he_mu = NULL;
2018 	u32 rate_n_flags = phy_data->rate_n_flags;
2019 	u32 he_type = rate_n_flags & RATE_MCS_HE_TYPE_MSK;
2020 	u8 ltf;
2021 	static const struct ieee80211_radiotap_he known = {
2022 		.data1 = cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA1_DATA_MCS_KNOWN |
2023 				     IEEE80211_RADIOTAP_HE_DATA1_DATA_DCM_KNOWN |
2024 				     IEEE80211_RADIOTAP_HE_DATA1_STBC_KNOWN |
2025 				     IEEE80211_RADIOTAP_HE_DATA1_CODING_KNOWN),
2026 		.data2 = cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA2_GI_KNOWN |
2027 				     IEEE80211_RADIOTAP_HE_DATA2_TXBF_KNOWN),
2028 	};
2029 	static const struct ieee80211_radiotap_he_mu mu_known = {
2030 		.flags1 = cpu_to_le16(IEEE80211_RADIOTAP_HE_MU_FLAGS1_SIG_B_MCS_KNOWN |
2031 				      IEEE80211_RADIOTAP_HE_MU_FLAGS1_SIG_B_DCM_KNOWN |
2032 				      IEEE80211_RADIOTAP_HE_MU_FLAGS1_SIG_B_SYMS_USERS_KNOWN |
2033 				      IEEE80211_RADIOTAP_HE_MU_FLAGS1_SIG_B_COMP_KNOWN),
2034 		.flags2 = cpu_to_le16(IEEE80211_RADIOTAP_HE_MU_FLAGS2_PUNC_FROM_SIG_A_BW_KNOWN |
2035 				      IEEE80211_RADIOTAP_HE_MU_FLAGS2_BW_FROM_SIG_A_BW_KNOWN),
2036 	};
2037 	u16 phy_info = phy_data->phy_info;
2038 
2039 	he = skb_put_data(skb, &known, sizeof(known));
2040 	rx_status->flag |= RX_FLAG_RADIOTAP_HE;
2041 
2042 	if (phy_data->info_type == IWL_RX_PHY_INFO_TYPE_HE_MU ||
2043 	    phy_data->info_type == IWL_RX_PHY_INFO_TYPE_HE_MU_EXT) {
2044 		he_mu = skb_put_data(skb, &mu_known, sizeof(mu_known));
2045 		rx_status->flag |= RX_FLAG_RADIOTAP_HE_MU;
2046 	}
2047 
2048 	/* report the AMPDU-EOF bit on single frames */
2049 	if (!queue && !(phy_info & IWL_RX_MPDU_PHY_AMPDU)) {
2050 		rx_status->flag |= RX_FLAG_AMPDU_DETAILS;
2051 		rx_status->flag |= RX_FLAG_AMPDU_EOF_BIT_KNOWN;
2052 		if (phy_data->d0 & cpu_to_le32(IWL_RX_PHY_DATA0_HE_DELIM_EOF))
2053 			rx_status->flag |= RX_FLAG_AMPDU_EOF_BIT;
2054 	}
2055 
2056 	if (phy_info & IWL_RX_MPDU_PHY_TSF_OVERLOAD)
2057 		iwl_mvm_decode_he_phy_data(mvm, phy_data, he, he_mu, rx_status,
2058 					   queue);
2059 
2060 	/* update aggregation data for monitor sake on default queue */
2061 	if (!queue && (phy_info & IWL_RX_MPDU_PHY_TSF_OVERLOAD) &&
2062 	    (phy_info & IWL_RX_MPDU_PHY_AMPDU) && phy_data->first_subframe) {
2063 		rx_status->flag |= RX_FLAG_AMPDU_EOF_BIT_KNOWN;
2064 		if (phy_data->d0 & cpu_to_le32(IWL_RX_PHY_DATA0_EHT_DELIM_EOF))
2065 			rx_status->flag |= RX_FLAG_AMPDU_EOF_BIT;
2066 	}
2067 
2068 	if (he_type == RATE_MCS_HE_TYPE_EXT_SU &&
2069 	    rate_n_flags & RATE_MCS_HE_106T_MSK) {
2070 		rx_status->bw = RATE_INFO_BW_HE_RU;
2071 		rx_status->he_ru = NL80211_RATE_INFO_HE_RU_ALLOC_106;
2072 	}
2073 
2074 	/* actually data is filled in mac80211 */
2075 	if (he_type == RATE_MCS_HE_TYPE_SU ||
2076 	    he_type == RATE_MCS_HE_TYPE_EXT_SU)
2077 		he->data1 |=
2078 			cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA1_BW_RU_ALLOC_KNOWN);
2079 
2080 #define CHECK_TYPE(F)							\
2081 	BUILD_BUG_ON(IEEE80211_RADIOTAP_HE_DATA1_FORMAT_ ## F !=	\
2082 		     (RATE_MCS_HE_TYPE_ ## F >> RATE_MCS_HE_TYPE_POS))
2083 
2084 	CHECK_TYPE(SU);
2085 	CHECK_TYPE(EXT_SU);
2086 	CHECK_TYPE(MU);
2087 	CHECK_TYPE(TRIG);
2088 
2089 	he->data1 |= cpu_to_le16(he_type >> RATE_MCS_HE_TYPE_POS);
2090 
2091 	if (rate_n_flags & RATE_MCS_BF_MSK)
2092 		he->data5 |= cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA5_TXBF);
2093 
2094 	switch ((rate_n_flags & RATE_MCS_HE_GI_LTF_MSK) >>
2095 		RATE_MCS_HE_GI_LTF_POS) {
2096 	case 0:
2097 		if (he_type == RATE_MCS_HE_TYPE_TRIG)
2098 			rx_status->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
2099 		else
2100 			rx_status->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
2101 		if (he_type == RATE_MCS_HE_TYPE_MU)
2102 			ltf = IEEE80211_RADIOTAP_HE_DATA5_LTF_SIZE_4X;
2103 		else
2104 			ltf = IEEE80211_RADIOTAP_HE_DATA5_LTF_SIZE_1X;
2105 		break;
2106 	case 1:
2107 		if (he_type == RATE_MCS_HE_TYPE_TRIG)
2108 			rx_status->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
2109 		else
2110 			rx_status->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
2111 		ltf = IEEE80211_RADIOTAP_HE_DATA5_LTF_SIZE_2X;
2112 		break;
2113 	case 2:
2114 		if (he_type == RATE_MCS_HE_TYPE_TRIG) {
2115 			rx_status->he_gi = NL80211_RATE_INFO_HE_GI_3_2;
2116 			ltf = IEEE80211_RADIOTAP_HE_DATA5_LTF_SIZE_4X;
2117 		} else {
2118 			rx_status->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
2119 			ltf = IEEE80211_RADIOTAP_HE_DATA5_LTF_SIZE_2X;
2120 		}
2121 		break;
2122 	case 3:
2123 		rx_status->he_gi = NL80211_RATE_INFO_HE_GI_3_2;
2124 		ltf = IEEE80211_RADIOTAP_HE_DATA5_LTF_SIZE_4X;
2125 		break;
2126 	case 4:
2127 		rx_status->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
2128 		ltf = IEEE80211_RADIOTAP_HE_DATA5_LTF_SIZE_4X;
2129 		break;
2130 	default:
2131 		ltf = IEEE80211_RADIOTAP_HE_DATA5_LTF_SIZE_UNKNOWN;
2132 	}
2133 
2134 	he->data5 |= le16_encode_bits(ltf,
2135 				      IEEE80211_RADIOTAP_HE_DATA5_LTF_SIZE);
2136 }
2137 
iwl_mvm_decode_lsig(struct sk_buff * skb,struct iwl_mvm_rx_phy_data * phy_data)2138 static void iwl_mvm_decode_lsig(struct sk_buff *skb,
2139 				struct iwl_mvm_rx_phy_data *phy_data)
2140 {
2141 	struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
2142 	struct ieee80211_radiotap_lsig *lsig;
2143 
2144 	switch (phy_data->info_type) {
2145 	case IWL_RX_PHY_INFO_TYPE_HT:
2146 	case IWL_RX_PHY_INFO_TYPE_VHT_SU:
2147 	case IWL_RX_PHY_INFO_TYPE_VHT_MU:
2148 	case IWL_RX_PHY_INFO_TYPE_HE_TB_EXT:
2149 	case IWL_RX_PHY_INFO_TYPE_HE_SU:
2150 	case IWL_RX_PHY_INFO_TYPE_HE_MU:
2151 	case IWL_RX_PHY_INFO_TYPE_HE_MU_EXT:
2152 	case IWL_RX_PHY_INFO_TYPE_HE_TB:
2153 	case IWL_RX_PHY_INFO_TYPE_EHT_MU:
2154 	case IWL_RX_PHY_INFO_TYPE_EHT_TB:
2155 	case IWL_RX_PHY_INFO_TYPE_EHT_MU_EXT:
2156 	case IWL_RX_PHY_INFO_TYPE_EHT_TB_EXT:
2157 		lsig = skb_put(skb, sizeof(*lsig));
2158 		lsig->data1 = cpu_to_le16(IEEE80211_RADIOTAP_LSIG_DATA1_LENGTH_KNOWN);
2159 		lsig->data2 = le16_encode_bits(le32_get_bits(phy_data->d1,
2160 							     IWL_RX_PHY_DATA1_LSIG_LEN_MASK),
2161 					       IEEE80211_RADIOTAP_LSIG_DATA2_LENGTH);
2162 		rx_status->flag |= RX_FLAG_RADIOTAP_LSIG;
2163 		break;
2164 	default:
2165 		break;
2166 	}
2167 }
2168 
iwl_mvm_nl80211_band_from_rx_msdu(u8 phy_band)2169 static inline u8 iwl_mvm_nl80211_band_from_rx_msdu(u8 phy_band)
2170 {
2171 	switch (phy_band) {
2172 	case PHY_BAND_24:
2173 		return NL80211_BAND_2GHZ;
2174 	case PHY_BAND_5:
2175 		return NL80211_BAND_5GHZ;
2176 	case PHY_BAND_6:
2177 		return NL80211_BAND_6GHZ;
2178 	default:
2179 		WARN_ONCE(1, "Unsupported phy band (%u)\n", phy_band);
2180 		return NL80211_BAND_5GHZ;
2181 	}
2182 }
2183 
2184 struct iwl_rx_sta_csa {
2185 	bool all_sta_unblocked;
2186 	struct ieee80211_vif *vif;
2187 };
2188 
iwl_mvm_rx_get_sta_block_tx(void * data,struct ieee80211_sta * sta)2189 static void iwl_mvm_rx_get_sta_block_tx(void *data, struct ieee80211_sta *sta)
2190 {
2191 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
2192 	struct iwl_rx_sta_csa *rx_sta_csa = data;
2193 
2194 	if (mvmsta->vif != rx_sta_csa->vif)
2195 		return;
2196 
2197 	if (mvmsta->disable_tx)
2198 		rx_sta_csa->all_sta_unblocked = false;
2199 }
2200 
2201 /*
2202  * Note: requires also rx_status->band to be prefilled, as well
2203  * as phy_data (apart from phy_data->info_type)
2204  */
iwl_mvm_rx_fill_status(struct iwl_mvm * mvm,struct sk_buff * skb,struct iwl_mvm_rx_phy_data * phy_data,int queue)2205 static void iwl_mvm_rx_fill_status(struct iwl_mvm *mvm,
2206 				   struct sk_buff *skb,
2207 				   struct iwl_mvm_rx_phy_data *phy_data,
2208 				   int queue)
2209 {
2210 	struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
2211 	u32 rate_n_flags = phy_data->rate_n_flags;
2212 	u8 stbc = u32_get_bits(rate_n_flags, RATE_MCS_STBC_MSK);
2213 	u32 format = rate_n_flags & RATE_MCS_MOD_TYPE_MSK;
2214 	bool is_sgi;
2215 
2216 	phy_data->info_type = IWL_RX_PHY_INFO_TYPE_NONE;
2217 
2218 	if (phy_data->phy_info & IWL_RX_MPDU_PHY_TSF_OVERLOAD)
2219 		phy_data->info_type =
2220 			le32_get_bits(phy_data->d1,
2221 				      IWL_RX_PHY_DATA1_INFO_TYPE_MASK);
2222 
2223 	/* This may be overridden by iwl_mvm_rx_he() to HE_RU */
2224 	switch (rate_n_flags & RATE_MCS_CHAN_WIDTH_MSK) {
2225 	case RATE_MCS_CHAN_WIDTH_20:
2226 		break;
2227 	case RATE_MCS_CHAN_WIDTH_40:
2228 		rx_status->bw = RATE_INFO_BW_40;
2229 		break;
2230 	case RATE_MCS_CHAN_WIDTH_80:
2231 		rx_status->bw = RATE_INFO_BW_80;
2232 		break;
2233 	case RATE_MCS_CHAN_WIDTH_160:
2234 		rx_status->bw = RATE_INFO_BW_160;
2235 		break;
2236 	case RATE_MCS_CHAN_WIDTH_320:
2237 		rx_status->bw = RATE_INFO_BW_320;
2238 		break;
2239 	}
2240 
2241 	/* must be before L-SIG data */
2242 	if (format == RATE_MCS_HE_MSK)
2243 		iwl_mvm_rx_he(mvm, skb, phy_data, queue);
2244 
2245 	iwl_mvm_decode_lsig(skb, phy_data);
2246 
2247 	rx_status->device_timestamp = phy_data->gp2_on_air_rise;
2248 	rx_status->freq = ieee80211_channel_to_frequency(phy_data->channel,
2249 							 rx_status->band);
2250 	iwl_mvm_get_signal_strength(mvm, rx_status, rate_n_flags,
2251 				    phy_data->energy_a, phy_data->energy_b);
2252 
2253 	/* using TLV format and must be after all fixed len fields */
2254 	if (format == RATE_MCS_EHT_MSK)
2255 		iwl_mvm_rx_eht(mvm, skb, phy_data, queue);
2256 
2257 	if (unlikely(mvm->monitor_on))
2258 		iwl_mvm_add_rtap_sniffer_config(mvm, skb);
2259 
2260 	is_sgi = format == RATE_MCS_HE_MSK ?
2261 		iwl_he_is_sgi(rate_n_flags) :
2262 		rate_n_flags & RATE_MCS_SGI_MSK;
2263 
2264 	if (!(format == RATE_MCS_CCK_MSK) && is_sgi)
2265 		rx_status->enc_flags |= RX_ENC_FLAG_SHORT_GI;
2266 
2267 	if (rate_n_flags & RATE_MCS_LDPC_MSK)
2268 		rx_status->enc_flags |= RX_ENC_FLAG_LDPC;
2269 
2270 	switch (format) {
2271 	case RATE_MCS_VHT_MSK:
2272 		rx_status->encoding = RX_ENC_VHT;
2273 		break;
2274 	case RATE_MCS_HE_MSK:
2275 		rx_status->encoding = RX_ENC_HE;
2276 		rx_status->he_dcm =
2277 			!!(rate_n_flags & RATE_HE_DUAL_CARRIER_MODE_MSK);
2278 		break;
2279 	case RATE_MCS_EHT_MSK:
2280 		rx_status->encoding = RX_ENC_EHT;
2281 		break;
2282 	}
2283 
2284 	switch (format) {
2285 	case RATE_MCS_HT_MSK:
2286 		rx_status->encoding = RX_ENC_HT;
2287 		rx_status->rate_idx = RATE_HT_MCS_INDEX(rate_n_flags);
2288 		rx_status->enc_flags |= stbc << RX_ENC_FLAG_STBC_SHIFT;
2289 		break;
2290 	case RATE_MCS_VHT_MSK:
2291 	case RATE_MCS_HE_MSK:
2292 	case RATE_MCS_EHT_MSK:
2293 		rx_status->nss =
2294 			u32_get_bits(rate_n_flags, RATE_MCS_NSS_MSK) + 1;
2295 		rx_status->rate_idx = rate_n_flags & RATE_MCS_CODE_MSK;
2296 		rx_status->enc_flags |= stbc << RX_ENC_FLAG_STBC_SHIFT;
2297 		break;
2298 	default: {
2299 		int rate = iwl_mvm_legacy_hw_idx_to_mac80211_idx(rate_n_flags,
2300 								 rx_status->band);
2301 
2302 		rx_status->rate_idx = rate;
2303 
2304 		if ((rate < 0 || rate > 0xFF)) {
2305 			rx_status->rate_idx = 0;
2306 			if (net_ratelimit())
2307 				IWL_ERR(mvm, "Invalid rate flags 0x%x, band %d,\n",
2308 					rate_n_flags, rx_status->band);
2309 		}
2310 
2311 		break;
2312 		}
2313 	}
2314 }
2315 
iwl_mvm_rx_mpdu_mq(struct iwl_mvm * mvm,struct napi_struct * napi,struct iwl_rx_cmd_buffer * rxb,int queue)2316 void iwl_mvm_rx_mpdu_mq(struct iwl_mvm *mvm, struct napi_struct *napi,
2317 			struct iwl_rx_cmd_buffer *rxb, int queue)
2318 {
2319 	struct ieee80211_rx_status *rx_status;
2320 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
2321 	struct iwl_rx_mpdu_desc *desc = (void *)pkt->data;
2322 	struct ieee80211_hdr *hdr;
2323 	u32 len;
2324 	u32 pkt_len = iwl_rx_packet_payload_len(pkt);
2325 	struct ieee80211_sta *sta = NULL;
2326 	struct ieee80211_link_sta *link_sta = NULL;
2327 	struct sk_buff *skb;
2328 	u8 crypt_len = 0;
2329 	size_t desc_size;
2330 	struct iwl_mvm_rx_phy_data phy_data = {};
2331 	u32 format;
2332 
2333 	if (unlikely(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)))
2334 		return;
2335 
2336 	if (mvm->trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_AX210)
2337 		desc_size = sizeof(*desc);
2338 	else
2339 		desc_size = IWL_RX_DESC_SIZE_V1;
2340 
2341 	if (unlikely(pkt_len < desc_size)) {
2342 		IWL_DEBUG_DROP(mvm, "Bad REPLY_RX_MPDU_CMD size\n");
2343 		return;
2344 	}
2345 
2346 	if (mvm->trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_AX210) {
2347 		phy_data.rate_n_flags = le32_to_cpu(desc->v3.rate_n_flags);
2348 		phy_data.channel = desc->v3.channel;
2349 		phy_data.gp2_on_air_rise = le32_to_cpu(desc->v3.gp2_on_air_rise);
2350 		phy_data.energy_a = desc->v3.energy_a;
2351 		phy_data.energy_b = desc->v3.energy_b;
2352 
2353 		phy_data.d0 = desc->v3.phy_data0;
2354 		phy_data.d1 = desc->v3.phy_data1;
2355 		phy_data.d2 = desc->v3.phy_data2;
2356 		phy_data.d3 = desc->v3.phy_data3;
2357 		phy_data.eht_d4 = desc->phy_eht_data4;
2358 		phy_data.d5 = desc->v3.phy_data5;
2359 	} else {
2360 		phy_data.rate_n_flags = le32_to_cpu(desc->v1.rate_n_flags);
2361 		phy_data.channel = desc->v1.channel;
2362 		phy_data.gp2_on_air_rise = le32_to_cpu(desc->v1.gp2_on_air_rise);
2363 		phy_data.energy_a = desc->v1.energy_a;
2364 		phy_data.energy_b = desc->v1.energy_b;
2365 
2366 		phy_data.d0 = desc->v1.phy_data0;
2367 		phy_data.d1 = desc->v1.phy_data1;
2368 		phy_data.d2 = desc->v1.phy_data2;
2369 		phy_data.d3 = desc->v1.phy_data3;
2370 	}
2371 
2372 	if (iwl_fw_lookup_notif_ver(mvm->fw, LEGACY_GROUP,
2373 				    REPLY_RX_MPDU_CMD, 0) < 4) {
2374 		phy_data.rate_n_flags = iwl_new_rate_from_v1(phy_data.rate_n_flags);
2375 		IWL_DEBUG_DROP(mvm, "Got old format rate, converting. New rate: 0x%x\n",
2376 			       phy_data.rate_n_flags);
2377 	}
2378 
2379 	format = phy_data.rate_n_flags & RATE_MCS_MOD_TYPE_MSK;
2380 
2381 	len = le16_to_cpu(desc->mpdu_len);
2382 
2383 	if (unlikely(len + desc_size > pkt_len)) {
2384 		IWL_DEBUG_DROP(mvm, "FW lied about packet len\n");
2385 		return;
2386 	}
2387 
2388 	phy_data.with_data = true;
2389 	phy_data.phy_info = le16_to_cpu(desc->phy_info);
2390 	phy_data.d4 = desc->phy_data4;
2391 
2392 	hdr = (void *)(pkt->data + desc_size);
2393 	/* Dont use dev_alloc_skb(), we'll have enough headroom once
2394 	 * ieee80211_hdr pulled.
2395 	 */
2396 	skb = alloc_skb(128, GFP_ATOMIC);
2397 	if (!skb) {
2398 		IWL_ERR(mvm, "alloc_skb failed\n");
2399 		return;
2400 	}
2401 
2402 	if (desc->mac_flags2 & IWL_RX_MPDU_MFLG2_PAD) {
2403 		/*
2404 		 * If the device inserted padding it means that (it thought)
2405 		 * the 802.11 header wasn't a multiple of 4 bytes long. In
2406 		 * this case, reserve two bytes at the start of the SKB to
2407 		 * align the payload properly in case we end up copying it.
2408 		 */
2409 		skb_reserve(skb, 2);
2410 	}
2411 
2412 	rx_status = IEEE80211_SKB_RXCB(skb);
2413 
2414 	/*
2415 	 * Keep packets with CRC errors (and with overrun) for monitor mode
2416 	 * (otherwise the firmware discards them) but mark them as bad.
2417 	 */
2418 	if (!(desc->status & cpu_to_le32(IWL_RX_MPDU_STATUS_CRC_OK)) ||
2419 	    !(desc->status & cpu_to_le32(IWL_RX_MPDU_STATUS_OVERRUN_OK))) {
2420 		IWL_DEBUG_RX(mvm, "Bad CRC or FIFO: 0x%08X.\n",
2421 			     le32_to_cpu(desc->status));
2422 		rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
2423 	}
2424 
2425 	/* set the preamble flag if appropriate */
2426 	if (format == RATE_MCS_CCK_MSK &&
2427 	    phy_data.phy_info & IWL_RX_MPDU_PHY_SHORT_PREAMBLE)
2428 		rx_status->enc_flags |= RX_ENC_FLAG_SHORTPRE;
2429 
2430 	if (likely(!(phy_data.phy_info & IWL_RX_MPDU_PHY_TSF_OVERLOAD))) {
2431 		u64 tsf_on_air_rise;
2432 
2433 		if (mvm->trans->trans_cfg->device_family >=
2434 		    IWL_DEVICE_FAMILY_AX210)
2435 			tsf_on_air_rise = le64_to_cpu(desc->v3.tsf_on_air_rise);
2436 		else
2437 			tsf_on_air_rise = le64_to_cpu(desc->v1.tsf_on_air_rise);
2438 
2439 		rx_status->mactime = tsf_on_air_rise;
2440 		/* TSF as indicated by the firmware is at INA time */
2441 		rx_status->flag |= RX_FLAG_MACTIME_PLCP_START;
2442 	}
2443 
2444 	if (iwl_mvm_is_band_in_rx_supported(mvm)) {
2445 		u8 band = BAND_IN_RX_STATUS(desc->mac_phy_idx);
2446 
2447 		rx_status->band = iwl_mvm_nl80211_band_from_rx_msdu(band);
2448 	} else {
2449 		rx_status->band = phy_data.channel > 14 ? NL80211_BAND_5GHZ :
2450 			NL80211_BAND_2GHZ;
2451 	}
2452 
2453 	/* update aggregation data for monitor sake on default queue */
2454 	if (!queue && (phy_data.phy_info & IWL_RX_MPDU_PHY_AMPDU)) {
2455 		bool toggle_bit;
2456 
2457 		toggle_bit = phy_data.phy_info & IWL_RX_MPDU_PHY_AMPDU_TOGGLE;
2458 		rx_status->flag |= RX_FLAG_AMPDU_DETAILS;
2459 		/*
2460 		 * Toggle is switched whenever new aggregation starts. Make
2461 		 * sure ampdu_reference is never 0 so we can later use it to
2462 		 * see if the frame was really part of an A-MPDU or not.
2463 		 */
2464 		if (toggle_bit != mvm->ampdu_toggle) {
2465 			mvm->ampdu_ref++;
2466 			if (mvm->ampdu_ref == 0)
2467 				mvm->ampdu_ref++;
2468 			mvm->ampdu_toggle = toggle_bit;
2469 			phy_data.first_subframe = true;
2470 		}
2471 		rx_status->ampdu_reference = mvm->ampdu_ref;
2472 	}
2473 
2474 	rcu_read_lock();
2475 
2476 	if (desc->status & cpu_to_le32(IWL_RX_MPDU_STATUS_SRC_STA_FOUND)) {
2477 		u8 id = le32_get_bits(desc->status, IWL_RX_MPDU_STATUS_STA_ID);
2478 
2479 		if (!WARN_ON_ONCE(id >= mvm->fw->ucode_capa.num_stations)) {
2480 			sta = rcu_dereference(mvm->fw_id_to_mac_id[id]);
2481 			if (IS_ERR(sta))
2482 				sta = NULL;
2483 			link_sta = rcu_dereference(mvm->fw_id_to_link_sta[id]);
2484 
2485 			if (sta && sta->valid_links && link_sta) {
2486 				rx_status->link_valid = 1;
2487 				rx_status->link_id = link_sta->link_id;
2488 			}
2489 		}
2490 	} else if (!is_multicast_ether_addr(hdr->addr2)) {
2491 		/*
2492 		 * This is fine since we prevent two stations with the same
2493 		 * address from being added.
2494 		 */
2495 		sta = ieee80211_find_sta_by_ifaddr(mvm->hw, hdr->addr2, NULL);
2496 	}
2497 
2498 	if (iwl_mvm_rx_crypto(mvm, sta, hdr, rx_status, phy_data.phy_info, desc,
2499 			      le32_to_cpu(pkt->len_n_flags), queue,
2500 			      &crypt_len)) {
2501 		kfree_skb(skb);
2502 		goto out;
2503 	}
2504 
2505 	iwl_mvm_rx_fill_status(mvm, skb, &phy_data, queue);
2506 
2507 	if (sta) {
2508 		struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
2509 		struct ieee80211_vif *tx_blocked_vif =
2510 			rcu_dereference(mvm->csa_tx_blocked_vif);
2511 		u8 baid = (u8)((le32_to_cpu(desc->reorder_data) &
2512 			       IWL_RX_MPDU_REORDER_BAID_MASK) >>
2513 			       IWL_RX_MPDU_REORDER_BAID_SHIFT);
2514 		struct iwl_fw_dbg_trigger_tlv *trig;
2515 		struct ieee80211_vif *vif = mvmsta->vif;
2516 
2517 		if (!mvm->tcm.paused && len >= sizeof(*hdr) &&
2518 		    !is_multicast_ether_addr(hdr->addr1) &&
2519 		    ieee80211_is_data(hdr->frame_control) &&
2520 		    time_after(jiffies, mvm->tcm.ts + MVM_TCM_PERIOD))
2521 			schedule_delayed_work(&mvm->tcm.work, 0);
2522 
2523 		/*
2524 		 * We have tx blocked stations (with CS bit). If we heard
2525 		 * frames from a blocked station on a new channel we can
2526 		 * TX to it again.
2527 		 */
2528 		if (unlikely(tx_blocked_vif) && tx_blocked_vif == vif) {
2529 			struct iwl_mvm_vif *mvmvif =
2530 				iwl_mvm_vif_from_mac80211(tx_blocked_vif);
2531 			struct iwl_rx_sta_csa rx_sta_csa = {
2532 				.all_sta_unblocked = true,
2533 				.vif = tx_blocked_vif,
2534 			};
2535 
2536 			if (mvmvif->csa_target_freq == rx_status->freq)
2537 				iwl_mvm_sta_modify_disable_tx_ap(mvm, sta,
2538 								 false);
2539 			ieee80211_iterate_stations_atomic(mvm->hw,
2540 							  iwl_mvm_rx_get_sta_block_tx,
2541 							  &rx_sta_csa);
2542 
2543 			if (rx_sta_csa.all_sta_unblocked) {
2544 				RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);
2545 				/* Unblock BCAST / MCAST station */
2546 				iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, false);
2547 				cancel_delayed_work(&mvm->cs_tx_unblock_dwork);
2548 			}
2549 		}
2550 
2551 		rs_update_last_rssi(mvm, mvmsta, rx_status);
2552 
2553 		trig = iwl_fw_dbg_trigger_on(&mvm->fwrt,
2554 					     ieee80211_vif_to_wdev(vif),
2555 					     FW_DBG_TRIGGER_RSSI);
2556 
2557 		if (trig && ieee80211_is_beacon(hdr->frame_control)) {
2558 			struct iwl_fw_dbg_trigger_low_rssi *rssi_trig;
2559 			s32 rssi;
2560 
2561 			rssi_trig = (void *)trig->data;
2562 			rssi = le32_to_cpu(rssi_trig->rssi);
2563 
2564 			if (rx_status->signal < rssi)
2565 				iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
2566 							NULL);
2567 		}
2568 
2569 		if (ieee80211_is_data(hdr->frame_control))
2570 			iwl_mvm_rx_csum(mvm, sta, skb, pkt);
2571 
2572 		if (iwl_mvm_is_dup(sta, queue, rx_status, hdr, desc)) {
2573 			kfree_skb(skb);
2574 			goto out;
2575 		}
2576 
2577 		/*
2578 		 * Our hardware de-aggregates AMSDUs but copies the mac header
2579 		 * as it to the de-aggregated MPDUs. We need to turn off the
2580 		 * AMSDU bit in the QoS control ourselves.
2581 		 * In addition, HW reverses addr3 and addr4 - reverse it back.
2582 		 */
2583 		if ((desc->mac_flags2 & IWL_RX_MPDU_MFLG2_AMSDU) &&
2584 		    !WARN_ON(!ieee80211_is_data_qos(hdr->frame_control))) {
2585 			u8 *qc = ieee80211_get_qos_ctl(hdr);
2586 
2587 			*qc &= ~IEEE80211_QOS_CTL_A_MSDU_PRESENT;
2588 
2589 			if (mvm->trans->trans_cfg->device_family ==
2590 			    IWL_DEVICE_FAMILY_9000) {
2591 				iwl_mvm_flip_address(hdr->addr3);
2592 
2593 				if (ieee80211_has_a4(hdr->frame_control))
2594 					iwl_mvm_flip_address(hdr->addr4);
2595 			}
2596 		}
2597 		if (baid != IWL_RX_REORDER_DATA_INVALID_BAID) {
2598 			u32 reorder_data = le32_to_cpu(desc->reorder_data);
2599 
2600 			iwl_mvm_agg_rx_received(mvm, reorder_data, baid);
2601 		}
2602 	}
2603 
2604 	/* management stuff on default queue */
2605 	if (!queue) {
2606 		if (unlikely((ieee80211_is_beacon(hdr->frame_control) ||
2607 			      ieee80211_is_probe_resp(hdr->frame_control)) &&
2608 			     mvm->sched_scan_pass_all ==
2609 			     SCHED_SCAN_PASS_ALL_ENABLED))
2610 			mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_FOUND;
2611 
2612 		if (unlikely(ieee80211_is_beacon(hdr->frame_control) ||
2613 			     ieee80211_is_probe_resp(hdr->frame_control)))
2614 			rx_status->boottime_ns = ktime_get_boottime_ns();
2615 	}
2616 
2617 	if (iwl_mvm_create_skb(mvm, skb, hdr, len, crypt_len, rxb)) {
2618 		kfree_skb(skb);
2619 		goto out;
2620 	}
2621 
2622 	if (!iwl_mvm_reorder(mvm, napi, queue, sta, skb, desc) &&
2623 	    likely(!iwl_mvm_time_sync_frame(mvm, skb, hdr->addr2)) &&
2624 	    likely(!iwl_mvm_mei_filter_scan(mvm, skb))) {
2625 		if (mvm->trans->trans_cfg->device_family == IWL_DEVICE_FAMILY_9000 &&
2626 		    (desc->mac_flags2 & IWL_RX_MPDU_MFLG2_AMSDU) &&
2627 		    !(desc->amsdu_info & IWL_RX_MPDU_AMSDU_LAST_SUBFRAME))
2628 			rx_status->flag |= RX_FLAG_AMSDU_MORE;
2629 
2630 		iwl_mvm_pass_packet_to_mac80211(mvm, napi, skb, queue, sta);
2631 	}
2632 out:
2633 	rcu_read_unlock();
2634 }
2635 
iwl_mvm_rx_monitor_no_data(struct iwl_mvm * mvm,struct napi_struct * napi,struct iwl_rx_cmd_buffer * rxb,int queue)2636 void iwl_mvm_rx_monitor_no_data(struct iwl_mvm *mvm, struct napi_struct *napi,
2637 				struct iwl_rx_cmd_buffer *rxb, int queue)
2638 {
2639 	struct ieee80211_rx_status *rx_status;
2640 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
2641 	struct iwl_rx_no_data_ver_3 *desc = (void *)pkt->data;
2642 	u32 rssi;
2643 	u32 info_type;
2644 	struct ieee80211_sta *sta = NULL;
2645 	struct sk_buff *skb;
2646 	struct iwl_mvm_rx_phy_data phy_data;
2647 	u32 format;
2648 
2649 	if (unlikely(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)))
2650 		return;
2651 
2652 	if (unlikely(iwl_rx_packet_payload_len(pkt) < sizeof(struct iwl_rx_no_data)))
2653 		return;
2654 
2655 	rssi = le32_to_cpu(desc->rssi);
2656 	info_type = le32_to_cpu(desc->info) & RX_NO_DATA_INFO_TYPE_MSK;
2657 	phy_data.d0 = desc->phy_info[0];
2658 	phy_data.d1 = desc->phy_info[1];
2659 	phy_data.phy_info = IWL_RX_MPDU_PHY_TSF_OVERLOAD;
2660 	phy_data.gp2_on_air_rise = le32_to_cpu(desc->on_air_rise_time);
2661 	phy_data.rate_n_flags = le32_to_cpu(desc->rate);
2662 	phy_data.energy_a = u32_get_bits(rssi, RX_NO_DATA_CHAIN_A_MSK);
2663 	phy_data.energy_b = u32_get_bits(rssi, RX_NO_DATA_CHAIN_B_MSK);
2664 	phy_data.channel = u32_get_bits(rssi, RX_NO_DATA_CHANNEL_MSK);
2665 	phy_data.with_data = false;
2666 	phy_data.rx_vec[0] = desc->rx_vec[0];
2667 	phy_data.rx_vec[1] = desc->rx_vec[1];
2668 
2669 	if (iwl_fw_lookup_notif_ver(mvm->fw, DATA_PATH_GROUP,
2670 				    RX_NO_DATA_NOTIF, 0) < 2) {
2671 		IWL_DEBUG_DROP(mvm, "Got an old rate format. Old rate: 0x%x\n",
2672 			       phy_data.rate_n_flags);
2673 		phy_data.rate_n_flags = iwl_new_rate_from_v1(phy_data.rate_n_flags);
2674 		IWL_DEBUG_DROP(mvm, " Rate after conversion to the new format: 0x%x\n",
2675 			       phy_data.rate_n_flags);
2676 	}
2677 
2678 	format = phy_data.rate_n_flags & RATE_MCS_MOD_TYPE_MSK;
2679 
2680 	if (iwl_fw_lookup_notif_ver(mvm->fw, DATA_PATH_GROUP,
2681 				    RX_NO_DATA_NOTIF, 0) >= 3) {
2682 		if (unlikely(iwl_rx_packet_payload_len(pkt) <
2683 		    sizeof(struct iwl_rx_no_data_ver_3)))
2684 		/* invalid len for ver 3 */
2685 			return;
2686 		phy_data.rx_vec[2] = desc->rx_vec[2];
2687 		phy_data.rx_vec[3] = desc->rx_vec[3];
2688 	} else {
2689 		if (format == RATE_MCS_EHT_MSK)
2690 			/* no support for EHT before version 3 API */
2691 			return;
2692 	}
2693 
2694 	/* Dont use dev_alloc_skb(), we'll have enough headroom once
2695 	 * ieee80211_hdr pulled.
2696 	 */
2697 	skb = alloc_skb(128, GFP_ATOMIC);
2698 	if (!skb) {
2699 		IWL_ERR(mvm, "alloc_skb failed\n");
2700 		return;
2701 	}
2702 
2703 	rx_status = IEEE80211_SKB_RXCB(skb);
2704 
2705 	/* 0-length PSDU */
2706 	rx_status->flag |= RX_FLAG_NO_PSDU;
2707 
2708 	switch (info_type) {
2709 	case RX_NO_DATA_INFO_TYPE_NDP:
2710 		rx_status->zero_length_psdu_type =
2711 			IEEE80211_RADIOTAP_ZERO_LEN_PSDU_SOUNDING;
2712 		break;
2713 	case RX_NO_DATA_INFO_TYPE_MU_UNMATCHED:
2714 	case RX_NO_DATA_INFO_TYPE_TB_UNMATCHED:
2715 		rx_status->zero_length_psdu_type =
2716 			IEEE80211_RADIOTAP_ZERO_LEN_PSDU_NOT_CAPTURED;
2717 		break;
2718 	default:
2719 		rx_status->zero_length_psdu_type =
2720 			IEEE80211_RADIOTAP_ZERO_LEN_PSDU_VENDOR;
2721 		break;
2722 	}
2723 
2724 	rx_status->band = phy_data.channel > 14 ? NL80211_BAND_5GHZ :
2725 		NL80211_BAND_2GHZ;
2726 
2727 	iwl_mvm_rx_fill_status(mvm, skb, &phy_data, queue);
2728 
2729 	/* no more radio tap info should be put after this point.
2730 	 *
2731 	 * We mark it as mac header, for upper layers to know where
2732 	 * all radio tap header ends.
2733 	 *
2734 	 * Since data doesn't move data while putting data on skb and that is
2735 	 * the only way we use, data + len is the next place that hdr would be put
2736 	 */
2737 	skb_set_mac_header(skb, skb->len);
2738 
2739 	/*
2740 	 * Override the nss from the rx_vec since the rate_n_flags has
2741 	 * only 2 bits for the nss which gives a max of 4 ss but there
2742 	 * may be up to 8 spatial streams.
2743 	 */
2744 	switch (format) {
2745 	case RATE_MCS_VHT_MSK:
2746 		rx_status->nss =
2747 			le32_get_bits(desc->rx_vec[0],
2748 				      RX_NO_DATA_RX_VEC0_VHT_NSTS_MSK) + 1;
2749 		break;
2750 	case RATE_MCS_HE_MSK:
2751 		rx_status->nss =
2752 			le32_get_bits(desc->rx_vec[0],
2753 				      RX_NO_DATA_RX_VEC0_HE_NSTS_MSK) + 1;
2754 		break;
2755 	case RATE_MCS_EHT_MSK:
2756 		rx_status->nss =
2757 			le32_get_bits(desc->rx_vec[2],
2758 				      RX_NO_DATA_RX_VEC2_EHT_NSTS_MSK) + 1;
2759 	}
2760 
2761 	rcu_read_lock();
2762 	ieee80211_rx_napi(mvm->hw, sta, skb, napi);
2763 	rcu_read_unlock();
2764 }
2765 
iwl_mvm_rx_frame_release(struct iwl_mvm * mvm,struct napi_struct * napi,struct iwl_rx_cmd_buffer * rxb,int queue)2766 void iwl_mvm_rx_frame_release(struct iwl_mvm *mvm, struct napi_struct *napi,
2767 			      struct iwl_rx_cmd_buffer *rxb, int queue)
2768 {
2769 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
2770 	struct iwl_frame_release *release = (void *)pkt->data;
2771 
2772 	if (unlikely(iwl_rx_packet_payload_len(pkt) < sizeof(*release)))
2773 		return;
2774 
2775 	iwl_mvm_release_frames_from_notif(mvm, napi, release->baid,
2776 					  le16_to_cpu(release->nssn),
2777 					  queue, 0);
2778 }
2779 
iwl_mvm_rx_bar_frame_release(struct iwl_mvm * mvm,struct napi_struct * napi,struct iwl_rx_cmd_buffer * rxb,int queue)2780 void iwl_mvm_rx_bar_frame_release(struct iwl_mvm *mvm, struct napi_struct *napi,
2781 				  struct iwl_rx_cmd_buffer *rxb, int queue)
2782 {
2783 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
2784 	struct iwl_bar_frame_release *release = (void *)pkt->data;
2785 	unsigned int baid = le32_get_bits(release->ba_info,
2786 					  IWL_BAR_FRAME_RELEASE_BAID_MASK);
2787 	unsigned int nssn = le32_get_bits(release->ba_info,
2788 					  IWL_BAR_FRAME_RELEASE_NSSN_MASK);
2789 	unsigned int sta_id = le32_get_bits(release->sta_tid,
2790 					    IWL_BAR_FRAME_RELEASE_STA_MASK);
2791 	unsigned int tid = le32_get_bits(release->sta_tid,
2792 					 IWL_BAR_FRAME_RELEASE_TID_MASK);
2793 	struct iwl_mvm_baid_data *baid_data;
2794 
2795 	if (unlikely(iwl_rx_packet_payload_len(pkt) < sizeof(*release)))
2796 		return;
2797 
2798 	if (WARN_ON_ONCE(baid == IWL_RX_REORDER_DATA_INVALID_BAID ||
2799 			 baid >= ARRAY_SIZE(mvm->baid_map)))
2800 		return;
2801 
2802 	rcu_read_lock();
2803 	baid_data = rcu_dereference(mvm->baid_map[baid]);
2804 	if (!baid_data) {
2805 		IWL_DEBUG_RX(mvm,
2806 			     "Got valid BAID %d but not allocated, invalid BAR release!\n",
2807 			      baid);
2808 		goto out;
2809 	}
2810 
2811 	if (WARN(tid != baid_data->tid || sta_id > IWL_MVM_STATION_COUNT_MAX ||
2812 		 !(baid_data->sta_mask & BIT(sta_id)),
2813 		 "baid 0x%x is mapped to sta_mask:0x%x tid:%d, but BAR release received for sta:%d tid:%d\n",
2814 		 baid, baid_data->sta_mask, baid_data->tid, sta_id,
2815 		 tid))
2816 		goto out;
2817 
2818 	iwl_mvm_release_frames_from_notif(mvm, napi, baid, nssn, queue, 0);
2819 out:
2820 	rcu_read_unlock();
2821 }
2822