xref: /freebsd/sys/contrib/dev/iwlwifi/mvm/rx.c (revision 6b627f88584ce13118e0a24951b503c0b1f2d5a7)
1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /*
3  * Copyright (C) 2012-2014, 2018-2025 Intel Corporation
4  * Copyright (C) 2013-2015 Intel Mobile Communications GmbH
5  * Copyright (C) 2016-2017 Intel Deutschland GmbH
6  */
7 #include <linux/unaligned.h>
8 #include <linux/etherdevice.h>
9 #include <linux/skbuff.h>
10 #include "iwl-trans.h"
11 #include "mvm.h"
12 #include "fw-api.h"
13 
14 /*
15  * iwl_mvm_rx_rx_phy_cmd - REPLY_RX_PHY_CMD handler
16  *
17  * Copies the phy information in mvm->last_phy_info, it will be used when the
18  * actual data will come from the fw in the next packet.
19  */
iwl_mvm_rx_rx_phy_cmd(struct iwl_mvm * mvm,struct iwl_rx_cmd_buffer * rxb)20 void iwl_mvm_rx_rx_phy_cmd(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
21 {
22 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
23 	unsigned int pkt_len = iwl_rx_packet_payload_len(pkt);
24 
25 	if (unlikely(pkt_len < sizeof(mvm->last_phy_info)))
26 		return;
27 
28 	memcpy(&mvm->last_phy_info, pkt->data, sizeof(mvm->last_phy_info));
29 	mvm->ampdu_ref++;
30 
31 #ifdef CONFIG_IWLWIFI_DEBUGFS
32 	if (mvm->last_phy_info.phy_flags & cpu_to_le16(RX_RES_PHY_FLAGS_AGG)) {
33 		spin_lock(&mvm->drv_stats_lock);
34 		mvm->drv_rx_stats.ampdu_count++;
35 		spin_unlock(&mvm->drv_stats_lock);
36 	}
37 #endif
38 }
39 
40 /*
41  * iwl_mvm_pass_packet_to_mac80211 - builds the packet for mac80211
42  *
43  * Adds the rxb to a new skb and give it to mac80211
44  */
iwl_mvm_pass_packet_to_mac80211(struct iwl_mvm * mvm,struct ieee80211_sta * sta,struct napi_struct * napi,struct sk_buff * skb,struct ieee80211_hdr * hdr,u16 len,u8 crypt_len,struct iwl_rx_cmd_buffer * rxb)45 static void iwl_mvm_pass_packet_to_mac80211(struct iwl_mvm *mvm,
46 					    struct ieee80211_sta *sta,
47 					    struct napi_struct *napi,
48 					    struct sk_buff *skb,
49 					    struct ieee80211_hdr *hdr, u16 len,
50 					    u8 crypt_len,
51 					    struct iwl_rx_cmd_buffer *rxb)
52 {
53 	unsigned int hdrlen = ieee80211_hdrlen(hdr->frame_control);
54 	unsigned int fraglen;
55 
56 	/*
57 	 * The 'hdrlen' (plus the 8 bytes for the SNAP and the crypt_len,
58 	 * but those are all multiples of 4 long) all goes away, but we
59 	 * want the *end* of it, which is going to be the start of the IP
60 	 * header, to be aligned when it gets pulled in.
61 	 * The beginning of the skb->data is aligned on at least a 4-byte
62 	 * boundary after allocation. Everything here is aligned at least
63 	 * on a 2-byte boundary so we can just take hdrlen & 3 and pad by
64 	 * the result.
65 	 */
66 	skb_reserve(skb, hdrlen & 3);
67 
68 	/* If frame is small enough to fit in skb->head, pull it completely.
69 	 * If not, only pull ieee80211_hdr (including crypto if present, and
70 	 * an additional 8 bytes for SNAP/ethertype, see below) so that
71 	 * splice() or TCP coalesce are more efficient.
72 	 *
73 	 * Since, in addition, ieee80211_data_to_8023() always pull in at
74 	 * least 8 bytes (possibly more for mesh) we can do the same here
75 	 * to save the cost of doing it later. That still doesn't pull in
76 	 * the actual IP header since the typical case has a SNAP header.
77 	 * If the latter changes (there are efforts in the standards group
78 	 * to do so) we should revisit this and ieee80211_data_to_8023().
79 	 */
80 	hdrlen = (len <= skb_tailroom(skb)) ? len : hdrlen + crypt_len + 8;
81 
82 	skb_put_data(skb, hdr, hdrlen);
83 	fraglen = len - hdrlen;
84 
85 	if (fraglen) {
86 		int offset = (u8 *)hdr + hdrlen -
87 			     (u8 *)rxb_addr(rxb) + rxb_offset(rxb);
88 
89 		skb_add_rx_frag(skb, 0, rxb_steal_page(rxb), offset,
90 				fraglen, rxb->truesize);
91 	}
92 
93 	ieee80211_rx_napi(mvm->hw, sta, skb, napi);
94 }
95 
96 /*
97  * iwl_mvm_get_signal_strength - use new rx PHY INFO API
98  * values are reported by the fw as positive values - need to negate
99  * to obtain their dBM.  Account for missing antennas by replacing 0
100  * values by -256dBm: practically 0 power and a non-feasible 8 bit value.
101  */
iwl_mvm_get_signal_strength(struct iwl_mvm * mvm,struct iwl_rx_phy_info * phy_info,struct ieee80211_rx_status * rx_status)102 static void iwl_mvm_get_signal_strength(struct iwl_mvm *mvm,
103 					struct iwl_rx_phy_info *phy_info,
104 					struct ieee80211_rx_status *rx_status)
105 {
106 	int energy_a, energy_b, max_energy;
107 	u32 val;
108 
109 	val =
110 	    le32_to_cpu(phy_info->non_cfg_phy[IWL_RX_INFO_ENERGY_ANT_ABC_IDX]);
111 	energy_a = (val & IWL_RX_INFO_ENERGY_ANT_A_MSK) >>
112 						IWL_RX_INFO_ENERGY_ANT_A_POS;
113 	energy_a = energy_a ? -energy_a : S8_MIN;
114 	energy_b = (val & IWL_RX_INFO_ENERGY_ANT_B_MSK) >>
115 						IWL_RX_INFO_ENERGY_ANT_B_POS;
116 	energy_b = energy_b ? -energy_b : S8_MIN;
117 	max_energy = max(energy_a, energy_b);
118 
119 	IWL_DEBUG_STATS(mvm, "energy In A %d B %d  , and max %d\n",
120 			energy_a, energy_b, max_energy);
121 
122 	rx_status->signal = max_energy;
123 	rx_status->chains = (le16_to_cpu(phy_info->phy_flags) &
124 				RX_RES_PHY_FLAGS_ANTENNA)
125 					>> RX_RES_PHY_FLAGS_ANTENNA_POS;
126 	rx_status->chain_signal[0] = energy_a;
127 	rx_status->chain_signal[1] = energy_b;
128 }
129 
130 /*
131  * iwl_mvm_set_mac80211_rx_flag - translate fw status to mac80211 format
132  * @mvm: the mvm object
133  * @hdr: 80211 header
134  * @stats: status in mac80211's format
135  * @rx_pkt_status: status coming from fw
136  *
137  * returns non 0 value if the packet should be dropped
138  */
iwl_mvm_set_mac80211_rx_flag(struct iwl_mvm * mvm,struct ieee80211_hdr * hdr,struct ieee80211_rx_status * stats,u32 rx_pkt_status,u8 * crypt_len)139 static u32 iwl_mvm_set_mac80211_rx_flag(struct iwl_mvm *mvm,
140 					struct ieee80211_hdr *hdr,
141 					struct ieee80211_rx_status *stats,
142 					u32 rx_pkt_status,
143 					u8 *crypt_len)
144 {
145 	if (!ieee80211_has_protected(hdr->frame_control) ||
146 	    (rx_pkt_status & RX_MPDU_RES_STATUS_SEC_ENC_MSK) ==
147 			     RX_MPDU_RES_STATUS_SEC_NO_ENC)
148 		return 0;
149 
150 	/* packet was encrypted with unknown alg */
151 	if ((rx_pkt_status & RX_MPDU_RES_STATUS_SEC_ENC_MSK) ==
152 					RX_MPDU_RES_STATUS_SEC_ENC_ERR)
153 		return 0;
154 
155 	switch (rx_pkt_status & RX_MPDU_RES_STATUS_SEC_ENC_MSK) {
156 	case RX_MPDU_RES_STATUS_SEC_CCM_ENC:
157 		/* alg is CCM: check MIC only */
158 		if (!(rx_pkt_status & RX_MPDU_RES_STATUS_MIC_OK))
159 			return -1;
160 
161 		stats->flag |= RX_FLAG_DECRYPTED;
162 		*crypt_len = IEEE80211_CCMP_HDR_LEN;
163 		return 0;
164 
165 	case RX_MPDU_RES_STATUS_SEC_TKIP_ENC:
166 		/* Don't drop the frame and decrypt it in SW */
167 		if (!fw_has_api(&mvm->fw->ucode_capa,
168 				IWL_UCODE_TLV_API_DEPRECATE_TTAK) &&
169 		    !(rx_pkt_status & RX_MPDU_RES_STATUS_TTAK_OK))
170 			return 0;
171 		*crypt_len = IEEE80211_TKIP_IV_LEN;
172 		fallthrough;
173 
174 	case RX_MPDU_RES_STATUS_SEC_WEP_ENC:
175 		if (!(rx_pkt_status & RX_MPDU_RES_STATUS_ICV_OK))
176 			return -1;
177 
178 		stats->flag |= RX_FLAG_DECRYPTED;
179 		if ((rx_pkt_status & RX_MPDU_RES_STATUS_SEC_ENC_MSK) ==
180 				RX_MPDU_RES_STATUS_SEC_WEP_ENC)
181 			*crypt_len = IEEE80211_WEP_IV_LEN;
182 		return 0;
183 
184 	case RX_MPDU_RES_STATUS_SEC_EXT_ENC:
185 		if (!(rx_pkt_status & RX_MPDU_RES_STATUS_MIC_OK))
186 			return -1;
187 		stats->flag |= RX_FLAG_DECRYPTED;
188 		return 0;
189 
190 	default:
191 		/* Expected in monitor (not having the keys) */
192 #if defined(__linux__)
193 		if (!mvm->monitor_on)
194 			IWL_WARN(mvm, "Unhandled alg: 0x%x\n", rx_pkt_status);
195 #elif defined(__FreeBSD__)
196 		if (!mvm->monitor_on && net_ratelimit())
197 			IWL_WARN(mvm, "%s: Unhandled alg: 0x%x\n", __func__, rx_pkt_status);
198 #endif
199 	}
200 
201 	return 0;
202 }
203 
iwl_mvm_rx_handle_tcm(struct iwl_mvm * mvm,struct ieee80211_sta * sta,struct ieee80211_hdr * hdr,u32 len,struct iwl_rx_phy_info * phy_info,u32 rate_n_flags)204 static void iwl_mvm_rx_handle_tcm(struct iwl_mvm *mvm,
205 				  struct ieee80211_sta *sta,
206 				  struct ieee80211_hdr *hdr, u32 len,
207 				  struct iwl_rx_phy_info *phy_info,
208 				  u32 rate_n_flags)
209 {
210 	struct iwl_mvm_sta *mvmsta;
211 	struct iwl_mvm_tcm_mac *mdata;
212 	int mac;
213 	int ac = IEEE80211_AC_BE; /* treat non-QoS as BE */
214 	struct iwl_mvm_vif *mvmvif;
215 	/* expected throughput in 100Kbps, single stream, 20 MHz */
216 	static const u8 thresh_tpt[] = {
217 		9, 18, 30, 42, 60, 78, 90, 96, 120, 135,
218 	};
219 	u16 thr;
220 
221 	if (ieee80211_is_data_qos(hdr->frame_control)) {
222 		u8 tid = ieee80211_get_tid(hdr);
223 
224 		if (tid < IWL_MAX_TID_COUNT)
225 			ac = tid_to_mac80211_ac[tid];
226 	}
227 
228 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
229 	mac = mvmsta->mac_id_n_color & FW_CTXT_ID_MSK;
230 
231 	if (time_after(jiffies, mvm->tcm.ts + MVM_TCM_PERIOD))
232 		schedule_delayed_work(&mvm->tcm.work, 0);
233 	mdata = &mvm->tcm.data[mac];
234 	mdata->rx.pkts[ac]++;
235 
236 	/* count the airtime only once for each ampdu */
237 	if (mdata->rx.last_ampdu_ref != mvm->ampdu_ref) {
238 		mdata->rx.last_ampdu_ref = mvm->ampdu_ref;
239 		mdata->rx.airtime += le16_to_cpu(phy_info->frame_time);
240 	}
241 
242 	if (!(rate_n_flags & (RATE_MCS_HT_MSK_V1 | RATE_MCS_VHT_MSK_V1)))
243 		return;
244 
245 	mvmvif = iwl_mvm_vif_from_mac80211(mvmsta->vif);
246 
247 	if (mdata->opened_rx_ba_sessions ||
248 	    mdata->uapsd_nonagg_detect.detected ||
249 	    (!mvmvif->deflink.queue_params[IEEE80211_AC_VO].uapsd &&
250 	     !mvmvif->deflink.queue_params[IEEE80211_AC_VI].uapsd &&
251 	     !mvmvif->deflink.queue_params[IEEE80211_AC_BE].uapsd &&
252 	     !mvmvif->deflink.queue_params[IEEE80211_AC_BK].uapsd) ||
253 	    mvmsta->deflink.sta_id != mvmvif->deflink.ap_sta_id)
254 		return;
255 
256 	if (rate_n_flags & RATE_MCS_HT_MSK_V1) {
257 		thr = thresh_tpt[rate_n_flags & RATE_HT_MCS_RATE_CODE_MSK_V1];
258 		thr *= 1 + ((rate_n_flags & RATE_HT_MCS_NSS_MSK_V1) >>
259 					RATE_HT_MCS_NSS_POS_V1);
260 	} else {
261 		if (WARN_ON((rate_n_flags & RATE_VHT_MCS_RATE_CODE_MSK) >=
262 				ARRAY_SIZE(thresh_tpt)))
263 			return;
264 		thr = thresh_tpt[rate_n_flags & RATE_VHT_MCS_RATE_CODE_MSK];
265 		thr *= 1 + FIELD_GET(RATE_MCS_NSS_MSK, rate_n_flags);
266 	}
267 
268 	thr <<= ((rate_n_flags & RATE_MCS_CHAN_WIDTH_MSK_V1) >>
269 				RATE_MCS_CHAN_WIDTH_POS);
270 
271 	mdata->uapsd_nonagg_detect.rx_bytes += len;
272 	ewma_rate_add(&mdata->uapsd_nonagg_detect.rate, thr);
273 }
274 
iwl_mvm_rx_csum(struct ieee80211_sta * sta,struct sk_buff * skb,u32 status)275 static void iwl_mvm_rx_csum(struct ieee80211_sta *sta,
276 			    struct sk_buff *skb,
277 			    u32 status)
278 {
279 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
280 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(mvmsta->vif);
281 
282 	if (mvmvif->features & NETIF_F_RXCSUM &&
283 	    status & RX_MPDU_RES_STATUS_CSUM_DONE &&
284 	    status & RX_MPDU_RES_STATUS_CSUM_OK)
285 		skb->ip_summed = CHECKSUM_UNNECESSARY;
286 }
287 
288 /*
289  * iwl_mvm_rx_rx_mpdu - REPLY_RX_MPDU_CMD handler
290  *
291  * Handles the actual data of the Rx packet from the fw
292  */
iwl_mvm_rx_rx_mpdu(struct iwl_mvm * mvm,struct napi_struct * napi,struct iwl_rx_cmd_buffer * rxb)293 void iwl_mvm_rx_rx_mpdu(struct iwl_mvm *mvm, struct napi_struct *napi,
294 			struct iwl_rx_cmd_buffer *rxb)
295 {
296 	struct ieee80211_hdr *hdr;
297 	struct ieee80211_rx_status *rx_status;
298 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
299 	struct iwl_rx_phy_info *phy_info;
300 	struct iwl_rx_mpdu_res_start *rx_res;
301 	struct ieee80211_sta *sta = NULL;
302 	struct sk_buff *skb;
303 	u32 len, pkt_len = iwl_rx_packet_payload_len(pkt);
304 	u32 rate_n_flags;
305 	u32 rx_pkt_status;
306 	u8 crypt_len = 0;
307 
308 	if (unlikely(pkt_len < sizeof(*rx_res))) {
309 		IWL_DEBUG_DROP(mvm, "Bad REPLY_RX_MPDU_CMD size\n");
310 		return;
311 	}
312 
313 	phy_info = &mvm->last_phy_info;
314 	rx_res = (struct iwl_rx_mpdu_res_start *)pkt->data;
315 	hdr = (struct ieee80211_hdr *)(pkt->data + sizeof(*rx_res));
316 	len = le16_to_cpu(rx_res->byte_count);
317 
318 	if (unlikely(len + sizeof(*rx_res) + sizeof(__le32) > pkt_len)) {
319 		IWL_DEBUG_DROP(mvm, "FW lied about packet len\n");
320 		return;
321 	}
322 
323 	rx_pkt_status = get_unaligned_le32((__le32 *)
324 		(pkt->data + sizeof(*rx_res) + len));
325 
326 	/* Dont use dev_alloc_skb(), we'll have enough headroom once
327 	 * ieee80211_hdr pulled.
328 	 */
329 	skb = alloc_skb(128, GFP_ATOMIC);
330 	if (!skb) {
331 		IWL_ERR(mvm, "alloc_skb failed\n");
332 		return;
333 	}
334 
335 	rx_status = IEEE80211_SKB_RXCB(skb);
336 
337 	/*
338 	 * Keep packets with CRC errors (and with overrun) for monitor mode
339 	 * (otherwise the firmware discards them) but mark them as bad.
340 	 */
341 	if (!(rx_pkt_status & RX_MPDU_RES_STATUS_CRC_OK) ||
342 	    !(rx_pkt_status & RX_MPDU_RES_STATUS_OVERRUN_OK)) {
343 		IWL_DEBUG_RX(mvm, "Bad CRC or FIFO: 0x%08X.\n", rx_pkt_status);
344 		rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
345 	}
346 
347 	/* This will be used in several places later */
348 	rate_n_flags = le32_to_cpu(phy_info->rate_n_flags);
349 
350 	/* rx_status carries information about the packet to mac80211 */
351 	rx_status->mactime = le64_to_cpu(phy_info->timestamp);
352 	rx_status->device_timestamp = le32_to_cpu(phy_info->system_timestamp);
353 	rx_status->band =
354 		(phy_info->phy_flags & cpu_to_le16(RX_RES_PHY_FLAGS_BAND_24)) ?
355 				NL80211_BAND_2GHZ : NL80211_BAND_5GHZ;
356 	rx_status->freq =
357 		ieee80211_channel_to_frequency(le16_to_cpu(phy_info->channel),
358 					       rx_status->band);
359 
360 	/* TSF as indicated by the firmware  is at INA time */
361 	rx_status->flag |= RX_FLAG_MACTIME_PLCP_START;
362 
363 	iwl_mvm_get_signal_strength(mvm, phy_info, rx_status);
364 
365 	IWL_DEBUG_STATS_LIMIT(mvm, "Rssi %d, TSF %llu\n", rx_status->signal,
366 			      (unsigned long long)rx_status->mactime);
367 
368 	rcu_read_lock();
369 	if (rx_pkt_status & RX_MPDU_RES_STATUS_SRC_STA_FOUND) {
370 		u32 id = rx_pkt_status & RX_MPDU_RES_STATUS_STA_ID_MSK;
371 
372 		id >>= RX_MDPU_RES_STATUS_STA_ID_SHIFT;
373 
374 		if (!WARN_ON_ONCE(id >= mvm->fw->ucode_capa.num_stations)) {
375 			sta = rcu_dereference(mvm->fw_id_to_mac_id[id]);
376 			if (IS_ERR(sta))
377 				sta = NULL;
378 		}
379 	} else if (!is_multicast_ether_addr(hdr->addr2)) {
380 		/* This is fine since we prevent two stations with the same
381 		 * address from being added.
382 		 */
383 		sta = ieee80211_find_sta_by_ifaddr(mvm->hw, hdr->addr2, NULL);
384 	}
385 
386 	if (sta) {
387 		struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
388 		struct ieee80211_vif *vif = mvmsta->vif;
389 		struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
390 
391 		/*
392 		 * Don't even try to decrypt a MCAST frame that was received
393 		 * before the managed vif is authorized, we'd fail anyway.
394 		 */
395 		if (is_multicast_ether_addr(hdr->addr1) &&
396 		    vif->type == NL80211_IFTYPE_STATION &&
397 		    !mvmvif->authorized &&
398 		    ieee80211_has_protected(hdr->frame_control)) {
399 			IWL_DEBUG_DROP(mvm, "MCAST before the vif is authorized\n");
400 			kfree_skb(skb);
401 			rcu_read_unlock();
402 			return;
403 		}
404 	}
405 
406 	/*
407 	 * drop the packet if it has failed being decrypted by HW
408 	 */
409 	if (iwl_mvm_set_mac80211_rx_flag(mvm, hdr, rx_status, rx_pkt_status,
410 					 &crypt_len)) {
411 		IWL_DEBUG_DROP(mvm, "Bad decryption results 0x%08x\n",
412 			       rx_pkt_status);
413 		kfree_skb(skb);
414 		rcu_read_unlock();
415 		return;
416 	}
417 
418 	if (sta) {
419 		struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
420 		struct ieee80211_vif *tx_blocked_vif =
421 			rcu_dereference(mvm->csa_tx_blocked_vif);
422 		struct iwl_fw_dbg_trigger_tlv *trig;
423 		struct ieee80211_vif *vif = mvmsta->vif;
424 
425 		/* We have tx blocked stations (with CS bit). If we heard
426 		 * frames from a blocked station on a new channel we can
427 		 * TX to it again.
428 		 */
429 		if (unlikely(tx_blocked_vif) && vif == tx_blocked_vif) {
430 			struct iwl_mvm_vif *mvmvif =
431 				iwl_mvm_vif_from_mac80211(tx_blocked_vif);
432 
433 			if (mvmvif->csa_target_freq == rx_status->freq)
434 				iwl_mvm_sta_modify_disable_tx_ap(mvm, sta,
435 								 false);
436 		}
437 
438 		rs_update_last_rssi(mvm, mvmsta, rx_status);
439 
440 		trig = iwl_fw_dbg_trigger_on(&mvm->fwrt,
441 					     ieee80211_vif_to_wdev(vif),
442 					     FW_DBG_TRIGGER_RSSI);
443 
444 		if (trig && ieee80211_is_beacon(hdr->frame_control)) {
445 			struct iwl_fw_dbg_trigger_low_rssi *rssi_trig;
446 			s32 rssi;
447 
448 			rssi_trig = (void *)trig->data;
449 			rssi = le32_to_cpu(rssi_trig->rssi);
450 
451 			if (rx_status->signal < rssi)
452 				iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
453 #if defined(__linux__)
454 							NULL);
455 #elif defined(__FreeBSD__)
456 							"");
457 #endif
458 		}
459 
460 		if (!mvm->tcm.paused && len >= sizeof(*hdr) &&
461 		    !is_multicast_ether_addr(hdr->addr1) &&
462 		    ieee80211_is_data(hdr->frame_control))
463 			iwl_mvm_rx_handle_tcm(mvm, sta, hdr, len, phy_info,
464 					      rate_n_flags);
465 
466 		if (ieee80211_is_data(hdr->frame_control))
467 			iwl_mvm_rx_csum(sta, skb, rx_pkt_status);
468 	}
469 	rcu_read_unlock();
470 
471 	/* set the preamble flag if appropriate */
472 	if (phy_info->phy_flags & cpu_to_le16(RX_RES_PHY_FLAGS_SHORT_PREAMBLE))
473 		rx_status->enc_flags |= RX_ENC_FLAG_SHORTPRE;
474 
475 	if (phy_info->phy_flags & cpu_to_le16(RX_RES_PHY_FLAGS_AGG)) {
476 		/*
477 		 * We know which subframes of an A-MPDU belong
478 		 * together since we get a single PHY response
479 		 * from the firmware for all of them
480 		 */
481 		rx_status->flag |= RX_FLAG_AMPDU_DETAILS;
482 		rx_status->ampdu_reference = mvm->ampdu_ref;
483 	}
484 
485 	/* Set up the HT phy flags */
486 	switch (rate_n_flags & RATE_MCS_CHAN_WIDTH_MSK_V1) {
487 	case RATE_MCS_CHAN_WIDTH_20:
488 		break;
489 	case RATE_MCS_CHAN_WIDTH_40:
490 		rx_status->bw = RATE_INFO_BW_40;
491 		break;
492 	case RATE_MCS_CHAN_WIDTH_80:
493 		rx_status->bw = RATE_INFO_BW_80;
494 		break;
495 	case RATE_MCS_CHAN_WIDTH_160:
496 		rx_status->bw = RATE_INFO_BW_160;
497 		break;
498 	}
499 	if (!(rate_n_flags & RATE_MCS_CCK_MSK_V1) &&
500 	    rate_n_flags & RATE_MCS_SGI_MSK_V1)
501 		rx_status->enc_flags |= RX_ENC_FLAG_SHORT_GI;
502 	if (rate_n_flags & RATE_MCS_LDPC_MSK_V1)
503 		rx_status->enc_flags |= RX_ENC_FLAG_LDPC;
504 	if (rate_n_flags & RATE_MCS_HT_MSK_V1) {
505 		u8 stbc = (rate_n_flags & RATE_MCS_STBC_MSK) >>
506 				RATE_MCS_STBC_POS;
507 		rx_status->encoding = RX_ENC_HT;
508 		rx_status->rate_idx = rate_n_flags & RATE_HT_MCS_INDEX_MSK_V1;
509 		rx_status->enc_flags |= stbc << RX_ENC_FLAG_STBC_SHIFT;
510 	} else if (rate_n_flags & RATE_MCS_VHT_MSK_V1) {
511 		u8 stbc = (rate_n_flags & RATE_MCS_STBC_MSK) >>
512 				RATE_MCS_STBC_POS;
513 		rx_status->nss =
514 			FIELD_GET(RATE_MCS_NSS_MSK, rate_n_flags) + 1;
515 		rx_status->rate_idx = rate_n_flags & RATE_VHT_MCS_RATE_CODE_MSK;
516 		rx_status->encoding = RX_ENC_VHT;
517 		rx_status->enc_flags |= stbc << RX_ENC_FLAG_STBC_SHIFT;
518 		if (rate_n_flags & RATE_MCS_BF_MSK)
519 			rx_status->enc_flags |= RX_ENC_FLAG_BF;
520 	} else {
521 		int rate = iwl_mvm_legacy_rate_to_mac80211_idx(rate_n_flags,
522 							       rx_status->band);
523 
524 		if (WARN(rate < 0 || rate > 0xFF,
525 			 "Invalid rate flags 0x%x, band %d,\n",
526 			 rate_n_flags, rx_status->band)) {
527 			kfree_skb(skb);
528 			return;
529 		}
530 		rx_status->rate_idx = rate;
531 	}
532 
533 #ifdef CONFIG_IWLWIFI_DEBUGFS
534 	iwl_mvm_update_frame_stats(mvm, rate_n_flags,
535 				   rx_status->flag & RX_FLAG_AMPDU_DETAILS);
536 #endif
537 
538 	if (unlikely((ieee80211_is_beacon(hdr->frame_control) ||
539 		      ieee80211_is_probe_resp(hdr->frame_control)) &&
540 		     mvm->sched_scan_pass_all == SCHED_SCAN_PASS_ALL_ENABLED))
541 		mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_FOUND;
542 
543 	if (unlikely(ieee80211_is_beacon(hdr->frame_control) ||
544 		     ieee80211_is_probe_resp(hdr->frame_control)))
545 		rx_status->boottime_ns = ktime_get_boottime_ns();
546 
547 	iwl_mvm_pass_packet_to_mac80211(mvm, sta, napi, skb, hdr, len,
548 					crypt_len, rxb);
549 }
550 
551 struct iwl_mvm_stat_data {
552 	struct iwl_mvm *mvm;
553 	__le32 flags;
554 	__le32 mac_id;
555 	u8 beacon_filter_average_energy;
556 	__le32 *beacon_counter;
557 	u8 *beacon_average_energy;
558 };
559 
560 struct iwl_mvm_stat_data_all_macs {
561 	struct iwl_mvm *mvm;
562 	__le32 flags;
563 	struct iwl_stats_ntfy_per_mac *per_mac;
564 };
565 
iwl_mvm_update_link_sig(struct ieee80211_vif * vif,int sig,struct iwl_mvm_vif_link_info * link_info,struct ieee80211_bss_conf * bss_conf)566 static void iwl_mvm_update_link_sig(struct ieee80211_vif *vif, int sig,
567 				    struct iwl_mvm_vif_link_info *link_info,
568 				    struct ieee80211_bss_conf *bss_conf)
569 {
570 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
571 	struct iwl_mvm *mvm = mvmvif->mvm;
572 	int thold = bss_conf->cqm_rssi_thold;
573 	int hyst = bss_conf->cqm_rssi_hyst;
574 	int last_event;
575 	s8 exit_esr_thresh;
576 
577 	if (sig == 0) {
578 		IWL_DEBUG_RX(mvm, "RSSI is 0 - skip signal based decision\n");
579 		return;
580 	}
581 
582 	link_info->bf_data.ave_beacon_signal = sig;
583 
584 	/* BT Coex */
585 	if (link_info->bf_data.bt_coex_min_thold !=
586 	    link_info->bf_data.bt_coex_max_thold) {
587 		last_event = link_info->bf_data.last_bt_coex_event;
588 		if (sig > link_info->bf_data.bt_coex_max_thold &&
589 		    (last_event <= link_info->bf_data.bt_coex_min_thold ||
590 		     last_event == 0)) {
591 			link_info->bf_data.last_bt_coex_event = sig;
592 			IWL_DEBUG_RX(mvm, "cqm_iterator bt coex high %d\n",
593 				     sig);
594 			iwl_mvm_bt_rssi_event(mvm, vif, RSSI_EVENT_HIGH);
595 		} else if (sig < link_info->bf_data.bt_coex_min_thold &&
596 			   (last_event >= link_info->bf_data.bt_coex_max_thold ||
597 			    last_event == 0)) {
598 			link_info->bf_data.last_bt_coex_event = sig;
599 			IWL_DEBUG_RX(mvm, "cqm_iterator bt coex low %d\n",
600 				     sig);
601 			iwl_mvm_bt_rssi_event(mvm, vif, RSSI_EVENT_LOW);
602 		}
603 	}
604 
605 	if (!(vif->driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI))
606 		return;
607 
608 	/* CQM Notification */
609 	last_event = link_info->bf_data.last_cqm_event;
610 	if (thold && sig < thold && (last_event == 0 ||
611 				     sig < last_event - hyst)) {
612 		link_info->bf_data.last_cqm_event = sig;
613 		IWL_DEBUG_RX(mvm, "cqm_iterator cqm low %d\n",
614 			     sig);
615 		ieee80211_cqm_rssi_notify(
616 			vif,
617 			NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
618 			sig,
619 			GFP_KERNEL);
620 	} else if (sig > thold &&
621 		   (last_event == 0 || sig > last_event + hyst)) {
622 		link_info->bf_data.last_cqm_event = sig;
623 		IWL_DEBUG_RX(mvm, "cqm_iterator cqm high %d\n",
624 			     sig);
625 		ieee80211_cqm_rssi_notify(
626 			vif,
627 			NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
628 			sig,
629 			GFP_KERNEL);
630 	}
631 
632 	/* ESR recalculation */
633 	if (!vif->cfg.assoc || !ieee80211_vif_is_mld(vif))
634 		return;
635 
636 	/* We're not in EMLSR and our signal is bad, try to switch link maybe */
637 	if (sig < IWL_MVM_LOW_RSSI_MLO_SCAN_THRESH && !mvmvif->esr_active) {
638 		iwl_mvm_int_mlo_scan(mvm, vif);
639 		return;
640 	}
641 
642 	/* We are in EMLSR, check if we need to exit */
643 	exit_esr_thresh =
644 		iwl_mvm_get_esr_rssi_thresh(mvm,
645 					    &bss_conf->chanreq.oper,
646 					    true);
647 
648 	if (sig < exit_esr_thresh)
649 		iwl_mvm_exit_esr(mvm, vif, IWL_MVM_ESR_EXIT_LOW_RSSI,
650 				 iwl_mvm_get_other_link(vif,
651 							bss_conf->link_id));
652 }
653 
iwl_mvm_stat_iterator(void * _data,u8 * mac,struct ieee80211_vif * vif)654 static void iwl_mvm_stat_iterator(void *_data, u8 *mac,
655 				  struct ieee80211_vif *vif)
656 {
657 	struct iwl_mvm_stat_data *data = _data;
658 	int sig = -data->beacon_filter_average_energy;
659 	u16 id = le32_to_cpu(data->mac_id);
660 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
661 	u16 vif_id = mvmvif->id;
662 
663 	/* This doesn't need the MAC ID check since it's not taking the
664 	 * data copied into the "data" struct, but rather the data from
665 	 * the notification directly.
666 	 */
667 	mvmvif->deflink.beacon_stats.num_beacons =
668 		le32_to_cpu(data->beacon_counter[vif_id]);
669 	mvmvif->deflink.beacon_stats.avg_signal =
670 		-data->beacon_average_energy[vif_id];
671 
672 	if (mvmvif->id != id)
673 		return;
674 
675 	if (vif->type != NL80211_IFTYPE_STATION)
676 		return;
677 
678 	/* make sure that beacon statistics don't go backwards with TCM
679 	 * request to clear statistics
680 	 */
681 	if (le32_to_cpu(data->flags) & IWL_STATISTICS_REPLY_FLG_CLEAR)
682 		mvmvif->deflink.beacon_stats.accu_num_beacons +=
683 			mvmvif->deflink.beacon_stats.num_beacons;
684 
685 	/* This is used in pre-MLO API so use deflink */
686 	iwl_mvm_update_link_sig(vif, sig, &mvmvif->deflink, &vif->bss_conf);
687 }
688 
iwl_mvm_stat_iterator_all_macs(void * _data,u8 * mac,struct ieee80211_vif * vif)689 static void iwl_mvm_stat_iterator_all_macs(void *_data, u8 *mac,
690 					   struct ieee80211_vif *vif)
691 {
692 	struct iwl_mvm_stat_data_all_macs *data = _data;
693 	struct iwl_stats_ntfy_per_mac *mac_stats;
694 	int sig;
695 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
696 	u16 vif_id = mvmvif->id;
697 
698 	if (WARN_ONCE(vif_id >= MAC_INDEX_AUX, "invalid vif id: %d", vif_id))
699 		return;
700 
701 	if (vif->type != NL80211_IFTYPE_STATION)
702 		return;
703 
704 	mac_stats = &data->per_mac[vif_id];
705 
706 	mvmvif->deflink.beacon_stats.num_beacons =
707 		le32_to_cpu(mac_stats->beacon_counter);
708 	mvmvif->deflink.beacon_stats.avg_signal =
709 		-le32_to_cpu(mac_stats->beacon_average_energy);
710 
711 	/* make sure that beacon statistics don't go backwards with TCM
712 	 * request to clear statistics
713 	 */
714 	if (le32_to_cpu(data->flags) & IWL_STATISTICS_REPLY_FLG_CLEAR)
715 		mvmvif->deflink.beacon_stats.accu_num_beacons +=
716 			mvmvif->deflink.beacon_stats.num_beacons;
717 
718 	sig = -le32_to_cpu(mac_stats->beacon_filter_average_energy);
719 
720 	/* This is used in pre-MLO API so use deflink */
721 	iwl_mvm_update_link_sig(vif, sig, &mvmvif->deflink, &vif->bss_conf);
722 }
723 
724 static inline void
iwl_mvm_rx_stats_check_trigger(struct iwl_mvm * mvm,struct iwl_rx_packet * pkt)725 iwl_mvm_rx_stats_check_trigger(struct iwl_mvm *mvm, struct iwl_rx_packet *pkt)
726 {
727 	struct iwl_fw_dbg_trigger_tlv *trig;
728 	struct iwl_fw_dbg_trigger_stats *trig_stats;
729 	u32 trig_offset, trig_thold;
730 
731 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, NULL, FW_DBG_TRIGGER_STATS);
732 	if (!trig)
733 		return;
734 
735 	trig_stats = (void *)trig->data;
736 
737 	trig_offset = le32_to_cpu(trig_stats->stop_offset);
738 	trig_thold = le32_to_cpu(trig_stats->stop_threshold);
739 
740 	if (WARN_ON_ONCE(trig_offset >= iwl_rx_packet_payload_len(pkt)))
741 		return;
742 
743 	if (le32_to_cpup((__le32 *) (pkt->data + trig_offset)) < trig_thold)
744 		return;
745 
746 #if defined(__linux__)
747 	iwl_fw_dbg_collect_trig(&mvm->fwrt, trig, NULL);
748 #elif defined(__FreeBSD__)
749 	iwl_fw_dbg_collect_trig(&mvm->fwrt, trig, "");
750 #endif
751 }
752 
iwl_mvm_stats_energy_iter(void * _data,struct ieee80211_sta * sta)753 static void iwl_mvm_stats_energy_iter(void *_data,
754 				      struct ieee80211_sta *sta)
755 {
756 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
757 	u8 *energy = _data;
758 	u32 sta_id = mvmsta->deflink.sta_id;
759 
760 	if (WARN_ONCE(sta_id >= IWL_STATION_COUNT_MAX, "sta_id %d >= %d",
761 		      sta_id, IWL_STATION_COUNT_MAX))
762 		return;
763 
764 	if (energy[sta_id])
765 		mvmsta->deflink.avg_energy = energy[sta_id];
766 
767 }
768 
769 static void
iwl_mvm_update_tcm_from_stats(struct iwl_mvm * mvm,__le32 * air_time_le,__le32 * rx_bytes_le)770 iwl_mvm_update_tcm_from_stats(struct iwl_mvm *mvm, __le32 *air_time_le,
771 			      __le32 *rx_bytes_le)
772 {
773 	int i;
774 
775 	spin_lock(&mvm->tcm.lock);
776 	for (i = 0; i < NUM_MAC_INDEX_DRIVER; i++) {
777 		struct iwl_mvm_tcm_mac *mdata = &mvm->tcm.data[i];
778 		u32 rx_bytes = le32_to_cpu(rx_bytes_le[i]);
779 		u32 airtime = le32_to_cpu(air_time_le[i]);
780 
781 		mdata->rx.airtime += airtime;
782 		mdata->uapsd_nonagg_detect.rx_bytes += rx_bytes;
783 		if (airtime) {
784 			/* re-init every time to store rate from FW */
785 			ewma_rate_init(&mdata->uapsd_nonagg_detect.rate);
786 			ewma_rate_add(&mdata->uapsd_nonagg_detect.rate,
787 				      rx_bytes * 8 / airtime);
788 		}
789 	}
790 	spin_unlock(&mvm->tcm.lock);
791 }
792 
iwl_mvm_handle_per_phy_stats(struct iwl_mvm * mvm,struct iwl_stats_ntfy_per_phy * per_phy)793 static void iwl_mvm_handle_per_phy_stats(struct iwl_mvm *mvm,
794 					 struct iwl_stats_ntfy_per_phy *per_phy)
795 {
796 	int i;
797 
798 	for (i = 0; i < NUM_PHY_CTX; i++) {
799 		if (!mvm->phy_ctxts[i].ref)
800 			continue;
801 		mvm->phy_ctxts[i].channel_load_by_us =
802 			le32_to_cpu(per_phy[i].channel_load_by_us);
803 		mvm->phy_ctxts[i].channel_load_not_by_us =
804 			le32_to_cpu(per_phy[i].channel_load_not_by_us);
805 	}
806 }
807 
808 static void
iwl_mvm_stats_ver_15(struct iwl_mvm * mvm,struct iwl_statistics_operational_ntfy * stats)809 iwl_mvm_stats_ver_15(struct iwl_mvm *mvm,
810 		     struct iwl_statistics_operational_ntfy *stats)
811 {
812 	struct iwl_mvm_stat_data_all_macs data = {
813 		.mvm = mvm,
814 		.flags = stats->flags,
815 		.per_mac = stats->per_mac,
816 	};
817 
818 	ieee80211_iterate_active_interfaces(mvm->hw,
819 					    IEEE80211_IFACE_ITER_NORMAL,
820 					    iwl_mvm_stat_iterator_all_macs,
821 					    &data);
822 	iwl_mvm_handle_per_phy_stats(mvm, stats->per_phy);
823 }
824 
825 static void
iwl_mvm_stats_ver_14(struct iwl_mvm * mvm,struct iwl_statistics_operational_ntfy_ver_14 * stats)826 iwl_mvm_stats_ver_14(struct iwl_mvm *mvm,
827 		     struct iwl_statistics_operational_ntfy_ver_14 *stats)
828 {
829 	struct iwl_mvm_stat_data data = {
830 		.mvm = mvm,
831 	};
832 
833 	u8 beacon_average_energy[MAC_INDEX_AUX];
834 	__le32 flags;
835 	int i;
836 
837 	flags = stats->flags;
838 
839 	data.mac_id = stats->mac_id;
840 	data.beacon_filter_average_energy =
841 		le32_to_cpu(stats->beacon_filter_average_energy);
842 	data.flags = flags;
843 	data.beacon_counter = stats->beacon_counter;
844 
845 	for (i = 0; i < ARRAY_SIZE(beacon_average_energy); i++)
846 		beacon_average_energy[i] =
847 			le32_to_cpu(stats->beacon_average_energy[i]);
848 
849 	data.beacon_average_energy = beacon_average_energy;
850 
851 	ieee80211_iterate_active_interfaces(mvm->hw,
852 					    IEEE80211_IFACE_ITER_NORMAL,
853 					    iwl_mvm_stat_iterator,
854 					    &data);
855 }
856 
iwl_mvm_verify_stats_len(struct iwl_mvm * mvm,struct iwl_rx_packet * pkt,u32 expected_size)857 static bool iwl_mvm_verify_stats_len(struct iwl_mvm *mvm,
858 				     struct iwl_rx_packet *pkt,
859 				     u32 expected_size)
860 {
861 	struct iwl_statistics_ntfy_hdr *hdr;
862 
863 	if (WARN_ONCE(iwl_rx_packet_payload_len(pkt) < expected_size,
864 		      "received invalid statistics size (%d)!, expected_size: %d\n",
865 		      iwl_rx_packet_payload_len(pkt), expected_size))
866 		return false;
867 
868 	hdr = (void *)&pkt->data;
869 
870 	if (WARN_ONCE((hdr->type & IWL_STATISTICS_TYPE_MSK) != FW_STATISTICS_OPERATIONAL ||
871 		      hdr->version !=
872 		      iwl_fw_lookup_notif_ver(mvm->fw, LEGACY_GROUP, STATISTICS_NOTIFICATION, 0),
873 		      "received unsupported hdr type %d, version %d\n",
874 		      hdr->type, hdr->version))
875 		return false;
876 
877 	if (WARN_ONCE(le16_to_cpu(hdr->size) != expected_size,
878 		      "received invalid statistics size in header (%d)!, expected_size: %d\n",
879 		      le16_to_cpu(hdr->size), expected_size))
880 		return false;
881 
882 	return true;
883 }
884 
885 static void
iwl_mvm_stat_iterator_all_links(struct iwl_mvm * mvm,struct iwl_stats_ntfy_per_link * per_link)886 iwl_mvm_stat_iterator_all_links(struct iwl_mvm *mvm,
887 				struct iwl_stats_ntfy_per_link *per_link)
888 {
889 	u32 air_time[MAC_INDEX_AUX] = {};
890 	u32 rx_bytes[MAC_INDEX_AUX] = {};
891 	int fw_link_id;
892 
893 	/* driver uses link ID == MAC ID */
894 	for (fw_link_id = 0; fw_link_id < ARRAY_SIZE(mvm->vif_id_to_mac);
895 	     fw_link_id++) {
896 		struct iwl_stats_ntfy_per_link *link_stats;
897 		struct iwl_mvm_vif_link_info *link_info;
898 		struct iwl_mvm_vif *mvmvif;
899 		struct ieee80211_vif *vif;
900 		int link_id;
901 		int sig;
902 
903 		vif = iwl_mvm_rcu_dereference_vif_id(mvm, fw_link_id, false);
904 		if (!vif)
905 			continue;
906 
907 		if (vif->type != NL80211_IFTYPE_STATION)
908 			continue;
909 
910 		link_id = vif->bss_conf.link_id;
911 		if (link_id >= ARRAY_SIZE(mvmvif->link))
912 			continue;
913 
914 		mvmvif = iwl_mvm_vif_from_mac80211(vif);
915 		link_info = mvmvif->link[link_id];
916 		if (!link_info)
917 			continue;
918 
919 		link_stats = &per_link[fw_link_id];
920 
921 		link_info->beacon_stats.num_beacons =
922 			le32_to_cpu(link_stats->beacon_counter);
923 
924 		/* we basically just use the u8 to store 8 bits and then treat
925 		 * it as a s8 whenever we take it out to a different type.
926 		 */
927 		link_info->beacon_stats.avg_signal =
928 			-le32_to_cpu(link_stats->beacon_average_energy);
929 
930 		if (link_info->phy_ctxt &&
931 		    link_info->phy_ctxt->channel->band == NL80211_BAND_2GHZ)
932 			iwl_mvm_bt_coex_update_link_esr(mvm, vif, link_id);
933 
934 		/* make sure that beacon statistics don't go backwards with TCM
935 		 * request to clear statistics
936 		 */
937 		if (mvm->statistics_clear)
938 			mvmvif->link[link_id]->beacon_stats.accu_num_beacons +=
939 				mvmvif->link[link_id]->beacon_stats.num_beacons;
940 
941 		sig = -le32_to_cpu(link_stats->beacon_filter_average_energy);
942 		iwl_mvm_update_link_sig(vif, sig, link_info, &vif->bss_conf);
943 
944 		if (WARN_ONCE(mvmvif->id >= MAC_INDEX_AUX,
945 			      "invalid mvmvif id: %d", mvmvif->id))
946 			continue;
947 
948 		air_time[mvmvif->id] +=
949 			le32_to_cpu(per_link[fw_link_id].air_time);
950 		rx_bytes[mvmvif->id] +=
951 			le32_to_cpu(per_link[fw_link_id].rx_bytes);
952 	}
953 
954 	/* Don't update in case the statistics are not cleared, since
955 	 * we will end up counting twice the same airtime, once in TCM
956 	 * request and once in statistics notification.
957 	 */
958 	if (mvm->statistics_clear) {
959 		__le32 air_time_le[MAC_INDEX_AUX];
960 		__le32 rx_bytes_le[MAC_INDEX_AUX];
961 		int vif_id;
962 
963 		for (vif_id = 0; vif_id < ARRAY_SIZE(air_time_le); vif_id++) {
964 			air_time_le[vif_id] = cpu_to_le32(air_time[vif_id]);
965 			rx_bytes_le[vif_id] = cpu_to_le32(rx_bytes[vif_id]);
966 		}
967 
968 		iwl_mvm_update_tcm_from_stats(mvm, air_time_le, rx_bytes_le);
969 	}
970 }
971 
972 #define SEC_LINK_MIN_PERC 10
973 #define SEC_LINK_MIN_TX 3000
974 #define SEC_LINK_MIN_RX 400
975 
976 /* Accept a ~20% short window to avoid issues due to jitter */
977 #define IWL_MVM_TPT_MIN_COUNT_WINDOW (IWL_MVM_TPT_COUNT_WINDOW_SEC * HZ * 4 / 5)
978 
iwl_mvm_update_esr_mode_tpt(struct iwl_mvm * mvm)979 static void iwl_mvm_update_esr_mode_tpt(struct iwl_mvm *mvm)
980 {
981 	struct ieee80211_vif *bss_vif = iwl_mvm_get_bss_vif(mvm);
982 	struct iwl_mvm_vif *mvmvif;
983 	struct iwl_mvm_sta *mvmsta;
984 	unsigned long total_tx = 0, total_rx = 0;
985 	unsigned long sec_link_tx = 0, sec_link_rx = 0;
986 	u8 sec_link_tx_perc, sec_link_rx_perc;
987 	u8 sec_link;
988 	bool skip = false;
989 
990 	lockdep_assert_held(&mvm->mutex);
991 
992 	if (IS_ERR_OR_NULL(bss_vif))
993 		return;
994 
995 	mvmvif = iwl_mvm_vif_from_mac80211(bss_vif);
996 
997 	if (!mvmvif->esr_active || !mvmvif->ap_sta)
998 		return;
999 
1000 	mvmsta = iwl_mvm_sta_from_mac80211(mvmvif->ap_sta);
1001 	/* We only count for the AP sta in a MLO connection */
1002 	if (!mvmsta->mpdu_counters)
1003 		return;
1004 
1005 	/* Get the FW ID of the secondary link */
1006 	sec_link = iwl_mvm_get_other_link(bss_vif,
1007 					  iwl_mvm_get_primary_link(bss_vif));
1008 	if (WARN_ON(!mvmvif->link[sec_link]))
1009 		return;
1010 	sec_link = mvmvif->link[sec_link]->fw_link_id;
1011 
1012 	/* Sum up RX and TX MPDUs from the different queues/links */
1013 	for (int q = 0; q < mvm->trans->info.num_rxqs; q++) {
1014 		spin_lock_bh(&mvmsta->mpdu_counters[q].lock);
1015 
1016 		/* The link IDs that doesn't exist will contain 0 */
1017 		for (int link = 0; link < IWL_FW_MAX_LINK_ID; link++) {
1018 			total_tx += mvmsta->mpdu_counters[q].per_link[link].tx;
1019 			total_rx += mvmsta->mpdu_counters[q].per_link[link].rx;
1020 		}
1021 
1022 		sec_link_tx += mvmsta->mpdu_counters[q].per_link[sec_link].tx;
1023 		sec_link_rx += mvmsta->mpdu_counters[q].per_link[sec_link].rx;
1024 
1025 		/*
1026 		 * In EMLSR we have statistics every 5 seconds, so we can reset
1027 		 * the counters upon every statistics notification.
1028 		 * The FW sends the notification regularly, but it will be
1029 		 * misaligned at the start. Skipping the measurement if it is
1030 		 * short will synchronize us.
1031 		 */
1032 		if (jiffies - mvmsta->mpdu_counters[q].window_start <
1033 		    IWL_MVM_TPT_MIN_COUNT_WINDOW)
1034 			skip = true;
1035 		mvmsta->mpdu_counters[q].window_start = jiffies;
1036 		memset(mvmsta->mpdu_counters[q].per_link, 0,
1037 		       sizeof(mvmsta->mpdu_counters[q].per_link));
1038 
1039 		spin_unlock_bh(&mvmsta->mpdu_counters[q].lock);
1040 	}
1041 
1042 	if (skip) {
1043 		IWL_DEBUG_INFO(mvm, "MPDU statistics window was short\n");
1044 		return;
1045 	}
1046 
1047 	IWL_DEBUG_INFO(mvm, "total Tx MPDUs: %ld. total Rx MPDUs: %ld\n",
1048 		       total_tx, total_rx);
1049 
1050 	/* If we don't have enough MPDUs - exit EMLSR */
1051 	if (total_tx < IWL_MVM_ENTER_ESR_TPT_THRESH &&
1052 	    total_rx < IWL_MVM_ENTER_ESR_TPT_THRESH) {
1053 		iwl_mvm_block_esr(mvm, bss_vif, IWL_MVM_ESR_BLOCKED_TPT,
1054 				  iwl_mvm_get_primary_link(bss_vif));
1055 		return;
1056 	}
1057 
1058 	IWL_DEBUG_INFO(mvm, "Secondary Link %d: Tx MPDUs: %ld. Rx MPDUs: %ld\n",
1059 		       sec_link, sec_link_tx, sec_link_rx);
1060 
1061 	/* Calculate the percentage of the secondary link TX/RX */
1062 	sec_link_tx_perc = total_tx ? sec_link_tx * 100 / total_tx : 0;
1063 	sec_link_rx_perc = total_rx ? sec_link_rx * 100 / total_rx : 0;
1064 
1065 	/*
1066 	 * The TX/RX percentage is checked only if it exceeds the required
1067 	 * minimum. In addition, RX is checked only if the TX check failed.
1068 	 */
1069 	if ((total_tx > SEC_LINK_MIN_TX &&
1070 	     sec_link_tx_perc < SEC_LINK_MIN_PERC) ||
1071 	    (total_rx > SEC_LINK_MIN_RX &&
1072 	     sec_link_rx_perc < SEC_LINK_MIN_PERC))
1073 		iwl_mvm_exit_esr(mvm, bss_vif, IWL_MVM_ESR_EXIT_LINK_USAGE,
1074 				 iwl_mvm_get_primary_link(bss_vif));
1075 }
1076 
iwl_mvm_handle_rx_system_oper_stats(struct iwl_mvm * mvm,struct iwl_rx_cmd_buffer * rxb)1077 void iwl_mvm_handle_rx_system_oper_stats(struct iwl_mvm *mvm,
1078 					 struct iwl_rx_cmd_buffer *rxb)
1079 {
1080 	u8 average_energy[IWL_STATION_COUNT_MAX];
1081 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1082 	struct iwl_system_statistics_notif_oper *stats;
1083 	int i;
1084 	u32 notif_ver = iwl_fw_lookup_notif_ver(mvm->fw, STATISTICS_GROUP,
1085 						STATISTICS_OPER_NOTIF, 0);
1086 
1087 	if (notif_ver != 3) {
1088 		IWL_FW_CHECK_FAILED(mvm,
1089 				    "Oper stats notif ver %d is not supported\n",
1090 				    notif_ver);
1091 		return;
1092 	}
1093 
1094 	stats = (void *)&pkt->data;
1095 	iwl_mvm_stat_iterator_all_links(mvm, stats->per_link);
1096 
1097 	for (i = 0; i < ARRAY_SIZE(average_energy); i++)
1098 		average_energy[i] =
1099 			le32_to_cpu(stats->per_sta[i].average_energy);
1100 
1101 	ieee80211_iterate_stations_atomic(mvm->hw, iwl_mvm_stats_energy_iter,
1102 					  average_energy);
1103 	iwl_mvm_handle_per_phy_stats(mvm, stats->per_phy);
1104 
1105 	iwl_mvm_update_esr_mode_tpt(mvm);
1106 }
1107 
iwl_mvm_handle_rx_system_oper_part1_stats(struct iwl_mvm * mvm,struct iwl_rx_cmd_buffer * rxb)1108 void iwl_mvm_handle_rx_system_oper_part1_stats(struct iwl_mvm *mvm,
1109 					       struct iwl_rx_cmd_buffer *rxb)
1110 {
1111 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1112 	struct iwl_system_statistics_part1_notif_oper *part1_stats;
1113 	int i;
1114 	u32 notif_ver = iwl_fw_lookup_notif_ver(mvm->fw, STATISTICS_GROUP,
1115 						STATISTICS_OPER_PART1_NOTIF, 0);
1116 
1117 	if (notif_ver != 4) {
1118 		IWL_FW_CHECK_FAILED(mvm,
1119 				    "Part1 stats notif ver %d is not supported\n",
1120 				    notif_ver);
1121 		return;
1122 	}
1123 
1124 	part1_stats = (void *)&pkt->data;
1125 	mvm->radio_stats.rx_time = 0;
1126 	mvm->radio_stats.tx_time = 0;
1127 	for (i = 0; i < ARRAY_SIZE(part1_stats->per_link); i++) {
1128 		mvm->radio_stats.rx_time +=
1129 			le64_to_cpu(part1_stats->per_link[i].rx_time);
1130 		mvm->radio_stats.tx_time +=
1131 			le64_to_cpu(part1_stats->per_link[i].tx_time);
1132 	}
1133 }
1134 
1135 static void
iwl_mvm_handle_rx_statistics_tlv(struct iwl_mvm * mvm,struct iwl_rx_packet * pkt)1136 iwl_mvm_handle_rx_statistics_tlv(struct iwl_mvm *mvm,
1137 				 struct iwl_rx_packet *pkt)
1138 {
1139 	u8 average_energy[IWL_STATION_COUNT_MAX];
1140 	__le32 air_time[MAC_INDEX_AUX];
1141 	__le32 rx_bytes[MAC_INDEX_AUX];
1142 	__le32 flags = 0;
1143 	int i;
1144 	u32 notif_ver = iwl_fw_lookup_notif_ver(mvm->fw, LEGACY_GROUP,
1145 					      STATISTICS_NOTIFICATION, 0);
1146 
1147 	if (WARN_ONCE(notif_ver > 15,
1148 		      "invalid statistics version id: %d\n", notif_ver))
1149 		return;
1150 
1151 	if (notif_ver == 14) {
1152 		struct iwl_statistics_operational_ntfy_ver_14 *stats =
1153 			(void *)pkt->data;
1154 
1155 		if (!iwl_mvm_verify_stats_len(mvm, pkt, sizeof(*stats)))
1156 			return;
1157 
1158 		iwl_mvm_stats_ver_14(mvm, stats);
1159 
1160 		flags = stats->flags;
1161 		mvm->radio_stats.rx_time = le64_to_cpu(stats->rx_time);
1162 		mvm->radio_stats.tx_time = le64_to_cpu(stats->tx_time);
1163 		mvm->radio_stats.on_time_rf = le64_to_cpu(stats->on_time_rf);
1164 		mvm->radio_stats.on_time_scan =
1165 			le64_to_cpu(stats->on_time_scan);
1166 
1167 		for (i = 0; i < ARRAY_SIZE(average_energy); i++)
1168 			average_energy[i] = le32_to_cpu(stats->average_energy[i]);
1169 
1170 		for (i = 0; i < ARRAY_SIZE(air_time); i++) {
1171 			air_time[i] = stats->air_time[i];
1172 			rx_bytes[i] = stats->rx_bytes[i];
1173 		}
1174 	}
1175 
1176 	if (notif_ver == 15) {
1177 		struct iwl_statistics_operational_ntfy *stats =
1178 			(void *)pkt->data;
1179 
1180 		if (!iwl_mvm_verify_stats_len(mvm, pkt, sizeof(*stats)))
1181 			return;
1182 
1183 		iwl_mvm_stats_ver_15(mvm, stats);
1184 
1185 		flags = stats->flags;
1186 		mvm->radio_stats.rx_time = le64_to_cpu(stats->rx_time);
1187 		mvm->radio_stats.tx_time = le64_to_cpu(stats->tx_time);
1188 		mvm->radio_stats.on_time_rf = le64_to_cpu(stats->on_time_rf);
1189 		mvm->radio_stats.on_time_scan =
1190 			le64_to_cpu(stats->on_time_scan);
1191 
1192 		for (i = 0; i < ARRAY_SIZE(average_energy); i++)
1193 			average_energy[i] =
1194 				le32_to_cpu(stats->per_sta[i].average_energy);
1195 
1196 		for (i = 0; i < ARRAY_SIZE(air_time); i++) {
1197 			air_time[i] = stats->per_mac[i].air_time;
1198 			rx_bytes[i] = stats->per_mac[i].rx_bytes;
1199 		}
1200 	}
1201 
1202 	iwl_mvm_rx_stats_check_trigger(mvm, pkt);
1203 
1204 	ieee80211_iterate_stations_atomic(mvm->hw, iwl_mvm_stats_energy_iter,
1205 					  average_energy);
1206 	/*
1207 	 * Don't update in case the statistics are not cleared, since
1208 	 * we will end up counting twice the same airtime, once in TCM
1209 	 * request and once in statistics notification.
1210 	 */
1211 	if (le32_to_cpu(flags) & IWL_STATISTICS_REPLY_FLG_CLEAR)
1212 		iwl_mvm_update_tcm_from_stats(mvm, air_time, rx_bytes);
1213 }
1214 
iwl_mvm_handle_rx_statistics(struct iwl_mvm * mvm,struct iwl_rx_packet * pkt)1215 void iwl_mvm_handle_rx_statistics(struct iwl_mvm *mvm,
1216 				  struct iwl_rx_packet *pkt)
1217 {
1218 	struct iwl_mvm_stat_data data = {
1219 		.mvm = mvm,
1220 	};
1221 	__le32 *bytes, *air_time, flags;
1222 	int expected_size;
1223 	u8 *energy;
1224 	u8 cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw,
1225 					   WIDE_ID(SYSTEM_GROUP,
1226 						   SYSTEM_STATISTICS_CMD),
1227 					   IWL_FW_CMD_VER_UNKNOWN);
1228 
1229 	if (cmd_ver != IWL_FW_CMD_VER_UNKNOWN)
1230 		return;
1231 
1232 	/* From ver 14 and up we use TLV statistics format */
1233 	if (iwl_fw_lookup_notif_ver(mvm->fw, LEGACY_GROUP,
1234 				    STATISTICS_NOTIFICATION, 0) >= 14)
1235 		return iwl_mvm_handle_rx_statistics_tlv(mvm, pkt);
1236 
1237 	if (!iwl_mvm_has_new_rx_stats_api(mvm)) {
1238 		if (iwl_mvm_has_new_rx_api(mvm))
1239 			expected_size = sizeof(struct iwl_notif_statistics_v11);
1240 		else
1241 			expected_size = sizeof(struct iwl_notif_statistics_v10);
1242 	} else {
1243 		expected_size = sizeof(struct iwl_notif_statistics);
1244 	}
1245 
1246 	if (WARN_ONCE(iwl_rx_packet_payload_len(pkt) != expected_size,
1247 		      "received invalid statistics size (%d)!\n",
1248 		      iwl_rx_packet_payload_len(pkt)))
1249 		return;
1250 
1251 	if (!iwl_mvm_has_new_rx_stats_api(mvm)) {
1252 		struct iwl_notif_statistics_v11 *stats = (void *)&pkt->data;
1253 
1254 		data.mac_id = stats->rx.general.mac_id;
1255 		data.beacon_filter_average_energy =
1256 			stats->general.common.beacon_filter_average_energy;
1257 
1258 		mvm->rx_stats_v3 = stats->rx;
1259 
1260 		mvm->radio_stats.rx_time =
1261 			le64_to_cpu(stats->general.common.rx_time);
1262 		mvm->radio_stats.tx_time =
1263 			le64_to_cpu(stats->general.common.tx_time);
1264 		mvm->radio_stats.on_time_rf =
1265 			le64_to_cpu(stats->general.common.on_time_rf);
1266 		mvm->radio_stats.on_time_scan =
1267 			le64_to_cpu(stats->general.common.on_time_scan);
1268 
1269 		data.beacon_counter = stats->general.beacon_counter;
1270 		data.beacon_average_energy =
1271 			stats->general.beacon_average_energy;
1272 		flags = stats->flag;
1273 	} else {
1274 		struct iwl_notif_statistics *stats = (void *)&pkt->data;
1275 
1276 		data.mac_id = stats->rx.general.mac_id;
1277 		data.beacon_filter_average_energy =
1278 			stats->general.common.beacon_filter_average_energy;
1279 
1280 		mvm->rx_stats = stats->rx;
1281 
1282 		mvm->radio_stats.rx_time =
1283 			le64_to_cpu(stats->general.common.rx_time);
1284 		mvm->radio_stats.tx_time =
1285 			le64_to_cpu(stats->general.common.tx_time);
1286 		mvm->radio_stats.on_time_rf =
1287 			le64_to_cpu(stats->general.common.on_time_rf);
1288 		mvm->radio_stats.on_time_scan =
1289 			le64_to_cpu(stats->general.common.on_time_scan);
1290 
1291 		data.beacon_counter = stats->general.beacon_counter;
1292 		data.beacon_average_energy =
1293 			stats->general.beacon_average_energy;
1294 		flags = stats->flag;
1295 	}
1296 	data.flags = flags;
1297 
1298 	iwl_mvm_rx_stats_check_trigger(mvm, pkt);
1299 
1300 	ieee80211_iterate_active_interfaces(mvm->hw,
1301 					    IEEE80211_IFACE_ITER_NORMAL,
1302 					    iwl_mvm_stat_iterator,
1303 					    &data);
1304 
1305 	if (!iwl_mvm_has_new_rx_api(mvm))
1306 		return;
1307 
1308 	if (!iwl_mvm_has_new_rx_stats_api(mvm)) {
1309 		struct iwl_notif_statistics_v11 *v11 = (void *)&pkt->data;
1310 
1311 		energy = (void *)&v11->load_stats.avg_energy;
1312 		bytes = (void *)&v11->load_stats.byte_count;
1313 		air_time = (void *)&v11->load_stats.air_time;
1314 	} else {
1315 		struct iwl_notif_statistics *stats = (void *)&pkt->data;
1316 
1317 		energy = (void *)&stats->load_stats.avg_energy;
1318 		bytes = (void *)&stats->load_stats.byte_count;
1319 		air_time = (void *)&stats->load_stats.air_time;
1320 	}
1321 	ieee80211_iterate_stations_atomic(mvm->hw, iwl_mvm_stats_energy_iter,
1322 					  energy);
1323 
1324 	/*
1325 	 * Don't update in case the statistics are not cleared, since
1326 	 * we will end up counting twice the same airtime, once in TCM
1327 	 * request and once in statistics notification.
1328 	 */
1329 	if (le32_to_cpu(flags) & IWL_STATISTICS_REPLY_FLG_CLEAR)
1330 		iwl_mvm_update_tcm_from_stats(mvm, air_time, bytes);
1331 
1332 }
1333 
iwl_mvm_rx_statistics(struct iwl_mvm * mvm,struct iwl_rx_cmd_buffer * rxb)1334 void iwl_mvm_rx_statistics(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
1335 {
1336 	iwl_mvm_handle_rx_statistics(mvm, rxb_addr(rxb));
1337 }
1338 
iwl_mvm_window_status_notif(struct iwl_mvm * mvm,struct iwl_rx_cmd_buffer * rxb)1339 void iwl_mvm_window_status_notif(struct iwl_mvm *mvm,
1340 				 struct iwl_rx_cmd_buffer *rxb)
1341 {
1342 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1343 	struct iwl_ba_window_status_notif *notif = (void *)pkt->data;
1344 	int i;
1345 
1346 	BUILD_BUG_ON(ARRAY_SIZE(notif->ra_tid) != BA_WINDOW_STREAMS_MAX);
1347 	BUILD_BUG_ON(ARRAY_SIZE(notif->mpdu_rx_count) != BA_WINDOW_STREAMS_MAX);
1348 	BUILD_BUG_ON(ARRAY_SIZE(notif->bitmap) != BA_WINDOW_STREAMS_MAX);
1349 	BUILD_BUG_ON(ARRAY_SIZE(notif->start_seq_num) != BA_WINDOW_STREAMS_MAX);
1350 
1351 	rcu_read_lock();
1352 	for (i = 0; i < BA_WINDOW_STREAMS_MAX; i++) {
1353 		struct ieee80211_sta *sta;
1354 		u8 sta_id, tid;
1355 		u64 bitmap;
1356 		u32 ssn;
1357 		u16 ratid;
1358 		u16 received_mpdu;
1359 
1360 		ratid = le16_to_cpu(notif->ra_tid[i]);
1361 		/* check that this TID is valid */
1362 		if (!(ratid & BA_WINDOW_STATUS_VALID_MSK))
1363 			continue;
1364 
1365 		received_mpdu = le16_to_cpu(notif->mpdu_rx_count[i]);
1366 		if (received_mpdu == 0)
1367 			continue;
1368 
1369 		tid = ratid & BA_WINDOW_STATUS_TID_MSK;
1370 		/* get the station */
1371 		sta_id = (ratid & BA_WINDOW_STATUS_STA_ID_MSK)
1372 			 >> BA_WINDOW_STATUS_STA_ID_POS;
1373 		sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
1374 		if (IS_ERR_OR_NULL(sta))
1375 			continue;
1376 		bitmap = le64_to_cpu(notif->bitmap[i]);
1377 		ssn = le32_to_cpu(notif->start_seq_num[i]);
1378 
1379 		/* update mac80211 with the bitmap for the reordering buffer */
1380 		ieee80211_mark_rx_ba_filtered_frames(sta, tid, ssn, bitmap,
1381 						     received_mpdu);
1382 	}
1383 	rcu_read_unlock();
1384 }
1385