xref: /linux/drivers/net/wireless/intel/iwlwifi/mvm/rx.c (revision fab183d632628381b466a41479489541ac0e29a0)
1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /*
3  * Copyright (C) 2012-2014, 2018-2026 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 (!mvm->monitor_on)
193 			IWL_WARN(mvm, "Unhandled alg: 0x%x\n", rx_pkt_status);
194 	}
195 
196 	return 0;
197 }
198 
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)199 static void iwl_mvm_rx_handle_tcm(struct iwl_mvm *mvm,
200 				  struct ieee80211_sta *sta,
201 				  struct ieee80211_hdr *hdr, u32 len,
202 				  struct iwl_rx_phy_info *phy_info,
203 				  u32 rate_n_flags)
204 {
205 	struct iwl_mvm_sta *mvmsta;
206 	struct iwl_mvm_tcm_mac *mdata;
207 	int mac;
208 	int ac = IEEE80211_AC_BE; /* treat non-QoS as BE */
209 	struct iwl_mvm_vif *mvmvif;
210 	/* expected throughput in 100Kbps, single stream, 20 MHz */
211 	static const u8 thresh_tpt[] = {
212 		9, 18, 30, 42, 60, 78, 90, 96, 120, 135,
213 	};
214 	u16 thr;
215 
216 	if (ieee80211_is_data_qos(hdr->frame_control)) {
217 		u8 tid = ieee80211_get_tid(hdr);
218 
219 		if (tid < IWL_MAX_TID_COUNT)
220 			ac = tid_to_mac80211_ac[tid];
221 	}
222 
223 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
224 	mac = mvmsta->mac_id_n_color & FW_CTXT_ID_MSK;
225 
226 	if (time_after(jiffies, mvm->tcm.ts + MVM_TCM_PERIOD))
227 		schedule_delayed_work(&mvm->tcm.work, 0);
228 	mdata = &mvm->tcm.data[mac];
229 	mdata->rx.pkts[ac]++;
230 
231 	/* count the airtime only once for each ampdu */
232 	if (mdata->rx.last_ampdu_ref != mvm->ampdu_ref) {
233 		mdata->rx.last_ampdu_ref = mvm->ampdu_ref;
234 		mdata->rx.airtime += le16_to_cpu(phy_info->frame_time);
235 	}
236 
237 	if (!(rate_n_flags & (RATE_MCS_HT_MSK_V1 | RATE_MCS_VHT_MSK_V1)))
238 		return;
239 
240 	mvmvif = iwl_mvm_vif_from_mac80211(mvmsta->vif);
241 
242 	if (mdata->opened_rx_ba_sessions ||
243 	    mdata->uapsd_nonagg_detect.detected ||
244 	    (!mvmvif->deflink.queue_params[IEEE80211_AC_VO].uapsd &&
245 	     !mvmvif->deflink.queue_params[IEEE80211_AC_VI].uapsd &&
246 	     !mvmvif->deflink.queue_params[IEEE80211_AC_BE].uapsd &&
247 	     !mvmvif->deflink.queue_params[IEEE80211_AC_BK].uapsd) ||
248 	    mvmsta->deflink.sta_id != mvmvif->deflink.ap_sta_id)
249 		return;
250 
251 	if (rate_n_flags & RATE_MCS_HT_MSK_V1) {
252 		thr = thresh_tpt[rate_n_flags & RATE_HT_MCS_RATE_CODE_MSK_V1];
253 		thr *= 1 + ((rate_n_flags & RATE_HT_MCS_NSS_MSK_V1) >>
254 					RATE_HT_MCS_NSS_POS_V1);
255 	} else {
256 		if (WARN_ON((rate_n_flags & RATE_VHT_MCS_RATE_CODE_MSK) >=
257 				ARRAY_SIZE(thresh_tpt)))
258 			return;
259 		thr = thresh_tpt[rate_n_flags & RATE_VHT_MCS_RATE_CODE_MSK];
260 		thr *= 1 + FIELD_GET(RATE_MCS_NSS_MSK, rate_n_flags);
261 	}
262 
263 	thr <<= ((rate_n_flags & RATE_MCS_CHAN_WIDTH_MSK_V1) >>
264 				RATE_MCS_CHAN_WIDTH_POS);
265 
266 	mdata->uapsd_nonagg_detect.rx_bytes += len;
267 	ewma_rate_add(&mdata->uapsd_nonagg_detect.rate, thr);
268 }
269 
iwl_mvm_rx_csum(struct ieee80211_sta * sta,struct sk_buff * skb,u32 status)270 static void iwl_mvm_rx_csum(struct ieee80211_sta *sta,
271 			    struct sk_buff *skb,
272 			    u32 status)
273 {
274 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
275 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(mvmsta->vif);
276 
277 	if (mvmvif->features & NETIF_F_RXCSUM &&
278 	    status & RX_MPDU_RES_STATUS_CSUM_DONE &&
279 	    status & RX_MPDU_RES_STATUS_CSUM_OK)
280 		skb->ip_summed = CHECKSUM_UNNECESSARY;
281 }
282 
283 /*
284  * iwl_mvm_rx_rx_mpdu - REPLY_RX_MPDU_CMD handler
285  *
286  * Handles the actual data of the Rx packet from the fw
287  */
iwl_mvm_rx_rx_mpdu(struct iwl_mvm * mvm,struct napi_struct * napi,struct iwl_rx_cmd_buffer * rxb)288 void iwl_mvm_rx_rx_mpdu(struct iwl_mvm *mvm, struct napi_struct *napi,
289 			struct iwl_rx_cmd_buffer *rxb)
290 {
291 	struct ieee80211_hdr *hdr;
292 	struct ieee80211_rx_status *rx_status;
293 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
294 	struct iwl_rx_phy_info *phy_info;
295 	struct iwl_rx_mpdu_res_start *rx_res;
296 	struct ieee80211_sta *sta = NULL;
297 	struct sk_buff *skb;
298 	u32 len, pkt_len = iwl_rx_packet_payload_len(pkt);
299 	u32 rate_n_flags;
300 	u32 rx_pkt_status;
301 	u8 crypt_len = 0;
302 
303 	if (unlikely(pkt_len < sizeof(*rx_res))) {
304 		IWL_DEBUG_DROP(mvm, "Bad REPLY_RX_MPDU_CMD size\n");
305 		return;
306 	}
307 
308 	phy_info = &mvm->last_phy_info;
309 	rx_res = (struct iwl_rx_mpdu_res_start *)pkt->data;
310 	hdr = (struct ieee80211_hdr *)(pkt->data + sizeof(*rx_res));
311 	len = le16_to_cpu(rx_res->byte_count);
312 
313 	if (unlikely(len + sizeof(*rx_res) + sizeof(__le32) > pkt_len)) {
314 		IWL_DEBUG_DROP(mvm, "FW lied about packet len\n");
315 		return;
316 	}
317 
318 	rx_pkt_status = get_unaligned_le32((__le32 *)
319 		(pkt->data + sizeof(*rx_res) + len));
320 
321 	/* Dont use dev_alloc_skb(), we'll have enough headroom once
322 	 * ieee80211_hdr pulled.
323 	 */
324 	skb = alloc_skb(128, GFP_ATOMIC);
325 	if (!skb) {
326 		IWL_ERR(mvm, "alloc_skb failed\n");
327 		return;
328 	}
329 
330 	rx_status = IEEE80211_SKB_RXCB(skb);
331 
332 	/*
333 	 * Keep packets with CRC errors (and with overrun) for monitor mode
334 	 * (otherwise the firmware discards them) but mark them as bad.
335 	 */
336 	if (!(rx_pkt_status & RX_MPDU_RES_STATUS_CRC_OK) ||
337 	    !(rx_pkt_status & RX_MPDU_RES_STATUS_OVERRUN_OK)) {
338 		IWL_DEBUG_RX(mvm, "Bad CRC or FIFO: 0x%08X.\n", rx_pkt_status);
339 		rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
340 	}
341 
342 	/* This will be used in several places later */
343 	rate_n_flags = le32_to_cpu(phy_info->rate_n_flags);
344 
345 	/* rx_status carries information about the packet to mac80211 */
346 	rx_status->mactime = le64_to_cpu(phy_info->timestamp);
347 	rx_status->device_timestamp = le32_to_cpu(phy_info->system_timestamp);
348 	rx_status->band =
349 		(phy_info->phy_flags & cpu_to_le16(RX_RES_PHY_FLAGS_BAND_24)) ?
350 				NL80211_BAND_2GHZ : NL80211_BAND_5GHZ;
351 	rx_status->freq =
352 		ieee80211_channel_to_frequency(le16_to_cpu(phy_info->channel),
353 					       rx_status->band);
354 
355 	/* TSF as indicated by the firmware  is at INA time */
356 	rx_status->flag |= RX_FLAG_MACTIME_PLCP_START;
357 
358 	iwl_mvm_get_signal_strength(mvm, phy_info, rx_status);
359 
360 	IWL_DEBUG_STATS_LIMIT(mvm, "Rssi %d, TSF %llu\n", rx_status->signal,
361 			      (unsigned long long)rx_status->mactime);
362 
363 	rcu_read_lock();
364 	if (rx_pkt_status & RX_MPDU_RES_STATUS_SRC_STA_FOUND) {
365 		u32 id = rx_pkt_status & RX_MPDU_RES_STATUS_STA_ID_MSK;
366 
367 		id >>= RX_MDPU_RES_STATUS_STA_ID_SHIFT;
368 
369 		if (!WARN_ON_ONCE(id >= mvm->fw->ucode_capa.num_stations)) {
370 			sta = rcu_dereference(mvm->fw_id_to_mac_id[id]);
371 			if (IS_ERR(sta))
372 				sta = NULL;
373 		}
374 	} else if (!is_multicast_ether_addr(hdr->addr2)) {
375 		/* This is fine since we prevent two stations with the same
376 		 * address from being added.
377 		 */
378 		sta = ieee80211_find_sta_by_ifaddr(mvm->hw, hdr->addr2, NULL);
379 	}
380 
381 	if (sta) {
382 		struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
383 		struct ieee80211_vif *vif = mvmsta->vif;
384 		struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
385 
386 		/*
387 		 * Don't even try to decrypt a MCAST frame that was received
388 		 * before the managed vif is authorized, we'd fail anyway.
389 		 */
390 		if (is_multicast_ether_addr(hdr->addr1) &&
391 		    vif->type == NL80211_IFTYPE_STATION &&
392 		    !mvmvif->authorized &&
393 		    ieee80211_has_protected(hdr->frame_control)) {
394 			IWL_DEBUG_DROP(mvm, "MCAST before the vif is authorized\n");
395 			kfree_skb(skb);
396 			rcu_read_unlock();
397 			return;
398 		}
399 	}
400 
401 	/*
402 	 * drop the packet if it has failed being decrypted by HW
403 	 */
404 	if (iwl_mvm_set_mac80211_rx_flag(mvm, hdr, rx_status, rx_pkt_status,
405 					 &crypt_len)) {
406 		IWL_DEBUG_DROP(mvm, "Bad decryption results 0x%08x\n",
407 			       rx_pkt_status);
408 		kfree_skb(skb);
409 		rcu_read_unlock();
410 		return;
411 	}
412 
413 	if (sta) {
414 		struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
415 		struct ieee80211_vif *tx_blocked_vif =
416 			rcu_dereference(mvm->csa_tx_blocked_vif);
417 		struct iwl_fw_dbg_trigger_tlv *trig;
418 		struct ieee80211_vif *vif = mvmsta->vif;
419 
420 		/* We have tx blocked stations (with CS bit). If we heard
421 		 * frames from a blocked station on a new channel we can
422 		 * TX to it again.
423 		 */
424 		if (unlikely(tx_blocked_vif) && vif == tx_blocked_vif) {
425 			struct iwl_mvm_vif *mvmvif =
426 				iwl_mvm_vif_from_mac80211(tx_blocked_vif);
427 
428 			if (mvmvif->csa_target_freq == rx_status->freq)
429 				iwl_mvm_sta_modify_disable_tx_ap(mvm, sta,
430 								 false);
431 		}
432 
433 		rs_update_last_rssi(mvm, mvmsta, rx_status);
434 
435 		trig = iwl_fw_dbg_trigger_on(&mvm->fwrt,
436 					     ieee80211_vif_to_wdev(vif),
437 					     FW_DBG_TRIGGER_RSSI);
438 
439 		if (trig && ieee80211_is_beacon(hdr->frame_control)) {
440 			struct iwl_fw_dbg_trigger_low_rssi *rssi_trig;
441 			s32 rssi;
442 
443 			rssi_trig = (void *)trig->data;
444 			rssi = le32_to_cpu(rssi_trig->rssi);
445 
446 			if (rx_status->signal < rssi)
447 				iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
448 							NULL);
449 		}
450 
451 		if (!mvm->tcm.paused && len >= sizeof(*hdr) &&
452 		    !is_multicast_ether_addr(hdr->addr1) &&
453 		    ieee80211_is_data(hdr->frame_control))
454 			iwl_mvm_rx_handle_tcm(mvm, sta, hdr, len, phy_info,
455 					      rate_n_flags);
456 
457 		if (ieee80211_is_data(hdr->frame_control))
458 			iwl_mvm_rx_csum(sta, skb, rx_pkt_status);
459 	}
460 	rcu_read_unlock();
461 
462 	/* set the preamble flag if appropriate */
463 	if (phy_info->phy_flags & cpu_to_le16(RX_RES_PHY_FLAGS_SHORT_PREAMBLE))
464 		rx_status->enc_flags |= RX_ENC_FLAG_SHORTPRE;
465 
466 	if (phy_info->phy_flags & cpu_to_le16(RX_RES_PHY_FLAGS_AGG)) {
467 		/*
468 		 * We know which subframes of an A-MPDU belong
469 		 * together since we get a single PHY response
470 		 * from the firmware for all of them
471 		 */
472 		rx_status->flag |= RX_FLAG_AMPDU_DETAILS;
473 		rx_status->ampdu_reference = mvm->ampdu_ref;
474 	}
475 
476 	/* Set up the HT phy flags */
477 	switch (rate_n_flags & RATE_MCS_CHAN_WIDTH_MSK_V1) {
478 	case RATE_MCS_CHAN_WIDTH_20:
479 		break;
480 	case RATE_MCS_CHAN_WIDTH_40:
481 		rx_status->bw = RATE_INFO_BW_40;
482 		break;
483 	case RATE_MCS_CHAN_WIDTH_80:
484 		rx_status->bw = RATE_INFO_BW_80;
485 		break;
486 	case RATE_MCS_CHAN_WIDTH_160:
487 		rx_status->bw = RATE_INFO_BW_160;
488 		break;
489 	}
490 	if (!(rate_n_flags & RATE_MCS_CCK_MSK_V1) &&
491 	    rate_n_flags & RATE_MCS_SGI_MSK_V1)
492 		rx_status->enc_flags |= RX_ENC_FLAG_SHORT_GI;
493 	if (rate_n_flags & RATE_MCS_LDPC_MSK_V1)
494 		rx_status->enc_flags |= RX_ENC_FLAG_LDPC;
495 	if (rate_n_flags & RATE_MCS_HT_MSK_V1) {
496 		u8 stbc = (rate_n_flags & RATE_MCS_STBC_MSK) >>
497 				RATE_MCS_STBC_POS;
498 		rx_status->encoding = RX_ENC_HT;
499 		rx_status->rate_idx = rate_n_flags & RATE_HT_MCS_INDEX_MSK_V1;
500 		rx_status->enc_flags |= stbc << RX_ENC_FLAG_STBC_SHIFT;
501 	} else if (rate_n_flags & RATE_MCS_VHT_MSK_V1) {
502 		u8 stbc = (rate_n_flags & RATE_MCS_STBC_MSK) >>
503 				RATE_MCS_STBC_POS;
504 		rx_status->nss =
505 			FIELD_GET(RATE_MCS_NSS_MSK, rate_n_flags) + 1;
506 		rx_status->rate_idx = rate_n_flags & RATE_VHT_MCS_RATE_CODE_MSK;
507 		rx_status->encoding = RX_ENC_VHT;
508 		rx_status->enc_flags |= stbc << RX_ENC_FLAG_STBC_SHIFT;
509 		if (rate_n_flags & RATE_MCS_BF_MSK)
510 			rx_status->enc_flags |= RX_ENC_FLAG_BF;
511 	} else {
512 		int rate = iwl_mvm_legacy_rate_to_mac80211_idx(rate_n_flags,
513 							       rx_status->band);
514 
515 		if (WARN(rate < 0 || rate > 0xFF,
516 			 "Invalid rate flags 0x%x, band %d,\n",
517 			 rate_n_flags, rx_status->band)) {
518 			kfree_skb(skb);
519 			return;
520 		}
521 		rx_status->rate_idx = rate;
522 		/* override BW - it could be DUP and indicate the wrong BW */
523 		rx_status->bw = RATE_INFO_BW_20;
524 	}
525 
526 #ifdef CONFIG_IWLWIFI_DEBUGFS
527 	iwl_mvm_update_frame_stats(mvm, rate_n_flags,
528 				   rx_status->flag & RX_FLAG_AMPDU_DETAILS);
529 #endif
530 
531 	if (unlikely((ieee80211_is_beacon(hdr->frame_control) ||
532 		      ieee80211_is_probe_resp(hdr->frame_control)) &&
533 		     mvm->sched_scan_pass_all == SCHED_SCAN_PASS_ALL_ENABLED))
534 		mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_FOUND;
535 
536 	if (unlikely(ieee80211_is_beacon(hdr->frame_control) ||
537 		     ieee80211_is_probe_resp(hdr->frame_control)))
538 		rx_status->boottime_ns = ktime_get_boottime_ns();
539 
540 	iwl_mvm_pass_packet_to_mac80211(mvm, sta, napi, skb, hdr, len,
541 					crypt_len, rxb);
542 }
543 
544 struct iwl_mvm_stat_data {
545 	struct iwl_mvm *mvm;
546 	__le32 flags;
547 	__le32 mac_id;
548 	u8 beacon_filter_average_energy;
549 	__le32 *beacon_counter;
550 	u8 *beacon_average_energy;
551 };
552 
553 struct iwl_mvm_stat_data_all_macs {
554 	struct iwl_mvm *mvm;
555 	__le32 flags;
556 	struct iwl_stats_ntfy_per_mac *per_mac;
557 };
558 
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)559 static void iwl_mvm_update_link_sig(struct ieee80211_vif *vif, int sig,
560 				    struct iwl_mvm_vif_link_info *link_info,
561 				    struct ieee80211_bss_conf *bss_conf)
562 {
563 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
564 	struct iwl_mvm *mvm = mvmvif->mvm;
565 	int thold = bss_conf->cqm_rssi_thold;
566 	int hyst = bss_conf->cqm_rssi_hyst;
567 	int last_event;
568 
569 	if (sig == 0) {
570 		IWL_DEBUG_RX(mvm, "RSSI is 0 - skip signal based decision\n");
571 		return;
572 	}
573 
574 	link_info->bf_data.ave_beacon_signal = sig;
575 
576 	/* BT Coex */
577 	if (link_info->bf_data.bt_coex_min_thold !=
578 	    link_info->bf_data.bt_coex_max_thold) {
579 		last_event = link_info->bf_data.last_bt_coex_event;
580 		if (sig > link_info->bf_data.bt_coex_max_thold &&
581 		    (last_event <= link_info->bf_data.bt_coex_min_thold ||
582 		     last_event == 0)) {
583 			link_info->bf_data.last_bt_coex_event = sig;
584 			IWL_DEBUG_RX(mvm, "cqm_iterator bt coex high %d\n",
585 				     sig);
586 			iwl_mvm_bt_rssi_event(mvm, vif, RSSI_EVENT_HIGH);
587 		} else if (sig < link_info->bf_data.bt_coex_min_thold &&
588 			   (last_event >= link_info->bf_data.bt_coex_max_thold ||
589 			    last_event == 0)) {
590 			link_info->bf_data.last_bt_coex_event = sig;
591 			IWL_DEBUG_RX(mvm, "cqm_iterator bt coex low %d\n",
592 				     sig);
593 			iwl_mvm_bt_rssi_event(mvm, vif, RSSI_EVENT_LOW);
594 		}
595 	}
596 
597 	if (!(vif->driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI))
598 		return;
599 
600 	/* CQM Notification */
601 	last_event = link_info->bf_data.last_cqm_event;
602 	if (thold && sig < thold && (last_event == 0 ||
603 				     sig < last_event - hyst)) {
604 		link_info->bf_data.last_cqm_event = sig;
605 		IWL_DEBUG_RX(mvm, "cqm_iterator cqm low %d\n",
606 			     sig);
607 		ieee80211_cqm_rssi_notify(
608 			vif,
609 			NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
610 			sig,
611 			GFP_KERNEL);
612 	} else if (sig > thold &&
613 		   (last_event == 0 || sig > last_event + hyst)) {
614 		link_info->bf_data.last_cqm_event = sig;
615 		IWL_DEBUG_RX(mvm, "cqm_iterator cqm high %d\n",
616 			     sig);
617 		ieee80211_cqm_rssi_notify(
618 			vif,
619 			NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
620 			sig,
621 			GFP_KERNEL);
622 	}
623 }
624 
iwl_mvm_stat_iterator(void * _data,u8 * mac,struct ieee80211_vif * vif)625 static void iwl_mvm_stat_iterator(void *_data, u8 *mac,
626 				  struct ieee80211_vif *vif)
627 {
628 	struct iwl_mvm_stat_data *data = _data;
629 	int sig = -data->beacon_filter_average_energy;
630 	u16 id = le32_to_cpu(data->mac_id);
631 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
632 	u16 vif_id = mvmvif->id;
633 
634 	/* This doesn't need the MAC ID check since it's not taking the
635 	 * data copied into the "data" struct, but rather the data from
636 	 * the notification directly.
637 	 */
638 	mvmvif->deflink.beacon_stats.num_beacons =
639 		le32_to_cpu(data->beacon_counter[vif_id]);
640 	mvmvif->deflink.beacon_stats.avg_signal =
641 		-data->beacon_average_energy[vif_id];
642 
643 	if (mvmvif->id != id)
644 		return;
645 
646 	if (vif->type != NL80211_IFTYPE_STATION)
647 		return;
648 
649 	/* make sure that beacon statistics don't go backwards with TCM
650 	 * request to clear statistics
651 	 */
652 	if (le32_to_cpu(data->flags) & IWL_STATISTICS_REPLY_FLG_CLEAR)
653 		mvmvif->deflink.beacon_stats.accu_num_beacons +=
654 			mvmvif->deflink.beacon_stats.num_beacons;
655 
656 	/* This is used in pre-MLO API so use deflink */
657 	iwl_mvm_update_link_sig(vif, sig, &mvmvif->deflink, &vif->bss_conf);
658 }
659 
iwl_mvm_stat_iterator_all_macs(void * _data,u8 * mac,struct ieee80211_vif * vif)660 static void iwl_mvm_stat_iterator_all_macs(void *_data, u8 *mac,
661 					   struct ieee80211_vif *vif)
662 {
663 	struct iwl_mvm_stat_data_all_macs *data = _data;
664 	struct iwl_stats_ntfy_per_mac *mac_stats;
665 	int sig;
666 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
667 	u16 vif_id = mvmvif->id;
668 
669 	if (WARN_ONCE(vif_id >= MAC_INDEX_AUX, "invalid vif id: %d", vif_id))
670 		return;
671 
672 	if (vif->type != NL80211_IFTYPE_STATION)
673 		return;
674 
675 	mac_stats = &data->per_mac[vif_id];
676 
677 	mvmvif->deflink.beacon_stats.num_beacons =
678 		le32_to_cpu(mac_stats->beacon_counter);
679 	mvmvif->deflink.beacon_stats.avg_signal =
680 		-le32_to_cpu(mac_stats->beacon_average_energy);
681 
682 	/* make sure that beacon statistics don't go backwards with TCM
683 	 * request to clear statistics
684 	 */
685 	if (le32_to_cpu(data->flags) & IWL_STATISTICS_REPLY_FLG_CLEAR)
686 		mvmvif->deflink.beacon_stats.accu_num_beacons +=
687 			mvmvif->deflink.beacon_stats.num_beacons;
688 
689 	sig = -le32_to_cpu(mac_stats->beacon_filter_average_energy);
690 
691 	/* This is used in pre-MLO API so use deflink */
692 	iwl_mvm_update_link_sig(vif, sig, &mvmvif->deflink, &vif->bss_conf);
693 }
694 
695 static inline void
iwl_mvm_rx_stats_check_trigger(struct iwl_mvm * mvm,struct iwl_rx_packet * pkt)696 iwl_mvm_rx_stats_check_trigger(struct iwl_mvm *mvm, struct iwl_rx_packet *pkt)
697 {
698 	struct iwl_fw_dbg_trigger_tlv *trig;
699 	struct iwl_fw_dbg_trigger_stats *trig_stats;
700 	u32 trig_offset, trig_thold;
701 
702 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, NULL, FW_DBG_TRIGGER_STATS);
703 	if (!trig)
704 		return;
705 
706 	trig_stats = (void *)trig->data;
707 
708 	trig_offset = le32_to_cpu(trig_stats->stop_offset);
709 	trig_thold = le32_to_cpu(trig_stats->stop_threshold);
710 
711 	if (WARN_ON_ONCE(trig_offset >= iwl_rx_packet_payload_len(pkt)))
712 		return;
713 
714 	if (le32_to_cpup((__le32 *) (pkt->data + trig_offset)) < trig_thold)
715 		return;
716 
717 	iwl_fw_dbg_collect_trig(&mvm->fwrt, trig, NULL);
718 }
719 
iwl_mvm_stats_energy_iter(void * _data,struct ieee80211_sta * sta)720 static void iwl_mvm_stats_energy_iter(void *_data,
721 				      struct ieee80211_sta *sta)
722 {
723 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
724 	u8 *energy = _data;
725 	u32 sta_id = mvmsta->deflink.sta_id;
726 
727 	if (WARN_ONCE(sta_id >= IWL_STATION_COUNT_MAX, "sta_id %d >= %d",
728 		      sta_id, IWL_STATION_COUNT_MAX))
729 		return;
730 
731 	if (energy[sta_id])
732 		mvmsta->deflink.avg_energy = energy[sta_id];
733 
734 }
735 
736 static void
iwl_mvm_update_tcm_from_stats(struct iwl_mvm * mvm,__le32 * air_time_le,__le32 * rx_bytes_le)737 iwl_mvm_update_tcm_from_stats(struct iwl_mvm *mvm, __le32 *air_time_le,
738 			      __le32 *rx_bytes_le)
739 {
740 	int i;
741 
742 	spin_lock(&mvm->tcm.lock);
743 	for (i = 0; i < NUM_MAC_INDEX_DRIVER; i++) {
744 		struct iwl_mvm_tcm_mac *mdata = &mvm->tcm.data[i];
745 		u32 rx_bytes = le32_to_cpu(rx_bytes_le[i]);
746 		u32 airtime = le32_to_cpu(air_time_le[i]);
747 
748 		mdata->rx.airtime += airtime;
749 		mdata->uapsd_nonagg_detect.rx_bytes += rx_bytes;
750 		if (airtime) {
751 			/* re-init every time to store rate from FW */
752 			ewma_rate_init(&mdata->uapsd_nonagg_detect.rate);
753 			ewma_rate_add(&mdata->uapsd_nonagg_detect.rate,
754 				      rx_bytes * 8 / airtime);
755 		}
756 	}
757 	spin_unlock(&mvm->tcm.lock);
758 }
759 
760 static void
iwl_mvm_handle_per_phy_stats(struct iwl_mvm * mvm,struct iwl_stats_ntfy_per_phy_v1 * per_phy)761 iwl_mvm_handle_per_phy_stats(struct iwl_mvm *mvm,
762 			     struct iwl_stats_ntfy_per_phy_v1 *per_phy)
763 {
764 	int i;
765 
766 	for (i = 0; i < NUM_PHY_CTX; i++) {
767 		if (!mvm->phy_ctxts[i].ref)
768 			continue;
769 		mvm->phy_ctxts[i].channel_load_by_us =
770 			le32_to_cpu(per_phy[i].channel_load_by_us);
771 		mvm->phy_ctxts[i].channel_load_not_by_us =
772 			le32_to_cpu(per_phy[i].channel_load_not_by_us);
773 	}
774 }
775 
776 static void
iwl_mvm_stats_ver_15(struct iwl_mvm * mvm,struct iwl_statistics_operational_ntfy * stats)777 iwl_mvm_stats_ver_15(struct iwl_mvm *mvm,
778 		     struct iwl_statistics_operational_ntfy *stats)
779 {
780 	struct iwl_mvm_stat_data_all_macs data = {
781 		.mvm = mvm,
782 		.flags = stats->flags,
783 		.per_mac = stats->per_mac,
784 	};
785 
786 	ieee80211_iterate_active_interfaces(mvm->hw,
787 					    IEEE80211_IFACE_ITER_NORMAL,
788 					    iwl_mvm_stat_iterator_all_macs,
789 					    &data);
790 	iwl_mvm_handle_per_phy_stats(mvm, stats->per_phy);
791 }
792 
793 static void
iwl_mvm_stats_ver_14(struct iwl_mvm * mvm,struct iwl_statistics_operational_ntfy_ver_14 * stats)794 iwl_mvm_stats_ver_14(struct iwl_mvm *mvm,
795 		     struct iwl_statistics_operational_ntfy_ver_14 *stats)
796 {
797 	struct iwl_mvm_stat_data data = {
798 		.mvm = mvm,
799 	};
800 
801 	u8 beacon_average_energy[MAC_INDEX_AUX];
802 	__le32 flags;
803 	int i;
804 
805 	flags = stats->flags;
806 
807 	data.mac_id = stats->mac_id;
808 	data.beacon_filter_average_energy =
809 		le32_to_cpu(stats->beacon_filter_average_energy);
810 	data.flags = flags;
811 	data.beacon_counter = stats->beacon_counter;
812 
813 	for (i = 0; i < ARRAY_SIZE(beacon_average_energy); i++)
814 		beacon_average_energy[i] =
815 			le32_to_cpu(stats->beacon_average_energy[i]);
816 
817 	data.beacon_average_energy = beacon_average_energy;
818 
819 	ieee80211_iterate_active_interfaces(mvm->hw,
820 					    IEEE80211_IFACE_ITER_NORMAL,
821 					    iwl_mvm_stat_iterator,
822 					    &data);
823 }
824 
iwl_mvm_verify_stats_len(struct iwl_mvm * mvm,struct iwl_rx_packet * pkt,u32 expected_size)825 static bool iwl_mvm_verify_stats_len(struct iwl_mvm *mvm,
826 				     struct iwl_rx_packet *pkt,
827 				     u32 expected_size)
828 {
829 	struct iwl_statistics_ntfy_hdr *hdr;
830 
831 	if (WARN_ONCE(iwl_rx_packet_payload_len(pkt) < expected_size,
832 		      "received invalid statistics size (%d)!, expected_size: %d\n",
833 		      iwl_rx_packet_payload_len(pkt), expected_size))
834 		return false;
835 
836 	hdr = (void *)&pkt->data;
837 
838 	if (WARN_ONCE((hdr->type & IWL_STATISTICS_TYPE_MSK) != FW_STATISTICS_OPERATIONAL ||
839 		      hdr->version !=
840 		      iwl_fw_lookup_notif_ver(mvm->fw, LEGACY_GROUP, STATISTICS_NOTIFICATION, 0),
841 		      "received unsupported hdr type %d, version %d\n",
842 		      hdr->type, hdr->version))
843 		return false;
844 
845 	if (WARN_ONCE(le16_to_cpu(hdr->size) != expected_size,
846 		      "received invalid statistics size in header (%d)!, expected_size: %d\n",
847 		      le16_to_cpu(hdr->size), expected_size))
848 		return false;
849 
850 	return true;
851 }
852 
853 static void
iwl_mvm_stat_iterator_all_links(struct iwl_mvm * mvm,struct iwl_stats_ntfy_per_link * per_link)854 iwl_mvm_stat_iterator_all_links(struct iwl_mvm *mvm,
855 				struct iwl_stats_ntfy_per_link *per_link)
856 {
857 	u32 air_time[MAC_INDEX_AUX] = {};
858 	u32 rx_bytes[MAC_INDEX_AUX] = {};
859 	int fw_link_id;
860 
861 	/* driver uses link ID == MAC ID */
862 	for (fw_link_id = 0; fw_link_id < ARRAY_SIZE(mvm->vif_id_to_mac);
863 	     fw_link_id++) {
864 		struct iwl_stats_ntfy_per_link *link_stats;
865 		struct iwl_mvm_vif_link_info *link_info;
866 		struct iwl_mvm_vif *mvmvif;
867 		struct ieee80211_vif *vif;
868 		int link_id;
869 		int sig;
870 
871 		vif = iwl_mvm_rcu_dereference_vif_id(mvm, fw_link_id, false);
872 		if (!vif)
873 			continue;
874 
875 		if (vif->type != NL80211_IFTYPE_STATION)
876 			continue;
877 
878 		link_id = vif->bss_conf.link_id;
879 		if (link_id >= ARRAY_SIZE(mvmvif->link))
880 			continue;
881 
882 		mvmvif = iwl_mvm_vif_from_mac80211(vif);
883 		link_info = mvmvif->link[link_id];
884 		if (!link_info)
885 			continue;
886 
887 		link_stats = &per_link[fw_link_id];
888 
889 		link_info->beacon_stats.num_beacons =
890 			le32_to_cpu(link_stats->beacon_counter);
891 
892 		/* we basically just use the u8 to store 8 bits and then treat
893 		 * it as a s8 whenever we take it out to a different type.
894 		 */
895 		link_info->beacon_stats.avg_signal =
896 			-le32_to_cpu(link_stats->beacon_average_energy);
897 
898 		/* make sure that beacon statistics don't go backwards with TCM
899 		 * request to clear statistics
900 		 */
901 		if (mvm->statistics_clear)
902 			mvmvif->link[link_id]->beacon_stats.accu_num_beacons +=
903 				mvmvif->link[link_id]->beacon_stats.num_beacons;
904 
905 		sig = -le32_to_cpu(link_stats->beacon_filter_average_energy);
906 		iwl_mvm_update_link_sig(vif, sig, link_info, &vif->bss_conf);
907 
908 		if (WARN_ONCE(mvmvif->id >= MAC_INDEX_AUX,
909 			      "invalid mvmvif id: %d", mvmvif->id))
910 			continue;
911 
912 		air_time[mvmvif->id] +=
913 			le32_to_cpu(per_link[fw_link_id].air_time);
914 		rx_bytes[mvmvif->id] +=
915 			le32_to_cpu(per_link[fw_link_id].rx_bytes);
916 	}
917 
918 	/* Don't update in case the statistics are not cleared, since
919 	 * we will end up counting twice the same airtime, once in TCM
920 	 * request and once in statistics notification.
921 	 */
922 	if (mvm->statistics_clear) {
923 		__le32 air_time_le[MAC_INDEX_AUX];
924 		__le32 rx_bytes_le[MAC_INDEX_AUX];
925 		int vif_id;
926 
927 		for (vif_id = 0; vif_id < ARRAY_SIZE(air_time_le); vif_id++) {
928 			air_time_le[vif_id] = cpu_to_le32(air_time[vif_id]);
929 			rx_bytes_le[vif_id] = cpu_to_le32(rx_bytes[vif_id]);
930 		}
931 
932 		iwl_mvm_update_tcm_from_stats(mvm, air_time_le, rx_bytes_le);
933 	}
934 }
935 
iwl_mvm_handle_rx_system_oper_stats(struct iwl_mvm * mvm,struct iwl_rx_cmd_buffer * rxb)936 void iwl_mvm_handle_rx_system_oper_stats(struct iwl_mvm *mvm,
937 					 struct iwl_rx_cmd_buffer *rxb)
938 {
939 	u8 average_energy[IWL_STATION_COUNT_MAX];
940 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
941 	struct iwl_system_statistics_notif_oper_v3 *stats;
942 	int i;
943 	u32 notif_ver = iwl_fw_lookup_notif_ver(mvm->fw, STATISTICS_GROUP,
944 						STATISTICS_OPER_NOTIF, 0);
945 
946 	if (notif_ver != 3) {
947 		IWL_FW_CHECK_FAILED(mvm,
948 				    "Oper stats notif ver %d is not supported\n",
949 				    notif_ver);
950 		return;
951 	}
952 
953 	stats = (void *)&pkt->data;
954 	iwl_mvm_stat_iterator_all_links(mvm, stats->per_link);
955 
956 	for (i = 0; i < ARRAY_SIZE(average_energy); i++)
957 		average_energy[i] =
958 			le32_to_cpu(stats->per_sta[i].average_energy);
959 
960 	ieee80211_iterate_stations_atomic(mvm->hw, iwl_mvm_stats_energy_iter,
961 					  average_energy);
962 	iwl_mvm_handle_per_phy_stats(mvm, stats->per_phy);
963 }
964 
iwl_mvm_handle_rx_system_oper_part1_stats(struct iwl_mvm * mvm,struct iwl_rx_cmd_buffer * rxb)965 void iwl_mvm_handle_rx_system_oper_part1_stats(struct iwl_mvm *mvm,
966 					       struct iwl_rx_cmd_buffer *rxb)
967 {
968 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
969 	struct iwl_system_statistics_part1_notif_oper *part1_stats;
970 	int i;
971 	u32 notif_ver = iwl_fw_lookup_notif_ver(mvm->fw, STATISTICS_GROUP,
972 						STATISTICS_OPER_PART1_NOTIF, 0);
973 
974 	if (notif_ver != 4) {
975 		IWL_FW_CHECK_FAILED(mvm,
976 				    "Part1 stats notif ver %d is not supported\n",
977 				    notif_ver);
978 		return;
979 	}
980 
981 	part1_stats = (void *)&pkt->data;
982 	mvm->radio_stats.rx_time = 0;
983 	mvm->radio_stats.tx_time = 0;
984 	for (i = 0; i < ARRAY_SIZE(part1_stats->per_link); i++) {
985 		mvm->radio_stats.rx_time +=
986 			le64_to_cpu(part1_stats->per_link[i].rx_time);
987 		mvm->radio_stats.tx_time +=
988 			le64_to_cpu(part1_stats->per_link[i].tx_time);
989 	}
990 }
991 
992 static void
iwl_mvm_handle_rx_statistics_tlv(struct iwl_mvm * mvm,struct iwl_rx_packet * pkt)993 iwl_mvm_handle_rx_statistics_tlv(struct iwl_mvm *mvm,
994 				 struct iwl_rx_packet *pkt)
995 {
996 	u8 average_energy[IWL_STATION_COUNT_MAX];
997 	__le32 air_time[MAC_INDEX_AUX];
998 	__le32 rx_bytes[MAC_INDEX_AUX];
999 	__le32 flags = 0;
1000 	int i;
1001 	u32 notif_ver = iwl_fw_lookup_notif_ver(mvm->fw, LEGACY_GROUP,
1002 					      STATISTICS_NOTIFICATION, 0);
1003 
1004 	if (WARN_ONCE(notif_ver > 15,
1005 		      "invalid statistics version id: %d\n", notif_ver))
1006 		return;
1007 
1008 	if (notif_ver == 14) {
1009 		struct iwl_statistics_operational_ntfy_ver_14 *stats =
1010 			(void *)pkt->data;
1011 
1012 		if (!iwl_mvm_verify_stats_len(mvm, pkt, sizeof(*stats)))
1013 			return;
1014 
1015 		iwl_mvm_stats_ver_14(mvm, stats);
1016 
1017 		flags = stats->flags;
1018 		mvm->radio_stats.rx_time = le64_to_cpu(stats->rx_time);
1019 		mvm->radio_stats.tx_time = le64_to_cpu(stats->tx_time);
1020 		mvm->radio_stats.on_time_rf = le64_to_cpu(stats->on_time_rf);
1021 		mvm->radio_stats.on_time_scan =
1022 			le64_to_cpu(stats->on_time_scan);
1023 
1024 		for (i = 0; i < ARRAY_SIZE(average_energy); i++)
1025 			average_energy[i] = le32_to_cpu(stats->average_energy[i]);
1026 
1027 		for (i = 0; i < ARRAY_SIZE(air_time); i++) {
1028 			air_time[i] = stats->air_time[i];
1029 			rx_bytes[i] = stats->rx_bytes[i];
1030 		}
1031 	}
1032 
1033 	if (notif_ver == 15) {
1034 		struct iwl_statistics_operational_ntfy *stats =
1035 			(void *)pkt->data;
1036 
1037 		if (!iwl_mvm_verify_stats_len(mvm, pkt, sizeof(*stats)))
1038 			return;
1039 
1040 		iwl_mvm_stats_ver_15(mvm, stats);
1041 
1042 		flags = stats->flags;
1043 		mvm->radio_stats.rx_time = le64_to_cpu(stats->rx_time);
1044 		mvm->radio_stats.tx_time = le64_to_cpu(stats->tx_time);
1045 		mvm->radio_stats.on_time_rf = le64_to_cpu(stats->on_time_rf);
1046 		mvm->radio_stats.on_time_scan =
1047 			le64_to_cpu(stats->on_time_scan);
1048 
1049 		for (i = 0; i < ARRAY_SIZE(average_energy); i++)
1050 			average_energy[i] =
1051 				le32_to_cpu(stats->per_sta[i].average_energy);
1052 
1053 		for (i = 0; i < ARRAY_SIZE(air_time); i++) {
1054 			air_time[i] = stats->per_mac[i].air_time;
1055 			rx_bytes[i] = stats->per_mac[i].rx_bytes;
1056 		}
1057 	}
1058 
1059 	iwl_mvm_rx_stats_check_trigger(mvm, pkt);
1060 
1061 	ieee80211_iterate_stations_atomic(mvm->hw, iwl_mvm_stats_energy_iter,
1062 					  average_energy);
1063 	/*
1064 	 * Don't update in case the statistics are not cleared, since
1065 	 * we will end up counting twice the same airtime, once in TCM
1066 	 * request and once in statistics notification.
1067 	 */
1068 	if (le32_to_cpu(flags) & IWL_STATISTICS_REPLY_FLG_CLEAR)
1069 		iwl_mvm_update_tcm_from_stats(mvm, air_time, rx_bytes);
1070 }
1071 
iwl_mvm_handle_rx_statistics(struct iwl_mvm * mvm,struct iwl_rx_packet * pkt)1072 void iwl_mvm_handle_rx_statistics(struct iwl_mvm *mvm,
1073 				  struct iwl_rx_packet *pkt)
1074 {
1075 	struct iwl_mvm_stat_data data = {
1076 		.mvm = mvm,
1077 	};
1078 	__le32 *bytes, *air_time, flags;
1079 	int expected_size;
1080 	u8 *energy;
1081 	u8 cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw,
1082 					   WIDE_ID(SYSTEM_GROUP,
1083 						   SYSTEM_STATISTICS_CMD),
1084 					   IWL_FW_CMD_VER_UNKNOWN);
1085 
1086 	if (cmd_ver != IWL_FW_CMD_VER_UNKNOWN)
1087 		return;
1088 
1089 	/* From ver 14 and up we use TLV statistics format */
1090 	if (iwl_fw_lookup_notif_ver(mvm->fw, LEGACY_GROUP,
1091 				    STATISTICS_NOTIFICATION, 0) >= 14)
1092 		return iwl_mvm_handle_rx_statistics_tlv(mvm, pkt);
1093 
1094 	if (!iwl_mvm_has_new_rx_stats_api(mvm)) {
1095 		if (iwl_mvm_has_new_rx_api(mvm))
1096 			expected_size = sizeof(struct iwl_notif_statistics_v11);
1097 		else
1098 			expected_size = sizeof(struct iwl_notif_statistics_v10);
1099 	} else {
1100 		expected_size = sizeof(struct iwl_notif_statistics);
1101 	}
1102 
1103 	if (WARN_ONCE(iwl_rx_packet_payload_len(pkt) != expected_size,
1104 		      "received invalid statistics size (%d)!\n",
1105 		      iwl_rx_packet_payload_len(pkt)))
1106 		return;
1107 
1108 	if (!iwl_mvm_has_new_rx_stats_api(mvm)) {
1109 		struct iwl_notif_statistics_v11 *stats = (void *)&pkt->data;
1110 
1111 		data.mac_id = stats->rx.general.mac_id;
1112 		data.beacon_filter_average_energy =
1113 			stats->general.common.beacon_filter_average_energy;
1114 
1115 		mvm->rx_stats_v3 = stats->rx;
1116 
1117 		mvm->radio_stats.rx_time =
1118 			le64_to_cpu(stats->general.common.rx_time);
1119 		mvm->radio_stats.tx_time =
1120 			le64_to_cpu(stats->general.common.tx_time);
1121 		mvm->radio_stats.on_time_rf =
1122 			le64_to_cpu(stats->general.common.on_time_rf);
1123 		mvm->radio_stats.on_time_scan =
1124 			le64_to_cpu(stats->general.common.on_time_scan);
1125 
1126 		data.beacon_counter = stats->general.beacon_counter;
1127 		data.beacon_average_energy =
1128 			stats->general.beacon_average_energy;
1129 		flags = stats->flag;
1130 	} else {
1131 		struct iwl_notif_statistics *stats = (void *)&pkt->data;
1132 
1133 		data.mac_id = stats->rx.general.mac_id;
1134 		data.beacon_filter_average_energy =
1135 			stats->general.common.beacon_filter_average_energy;
1136 
1137 		mvm->rx_stats = stats->rx;
1138 
1139 		mvm->radio_stats.rx_time =
1140 			le64_to_cpu(stats->general.common.rx_time);
1141 		mvm->radio_stats.tx_time =
1142 			le64_to_cpu(stats->general.common.tx_time);
1143 		mvm->radio_stats.on_time_rf =
1144 			le64_to_cpu(stats->general.common.on_time_rf);
1145 		mvm->radio_stats.on_time_scan =
1146 			le64_to_cpu(stats->general.common.on_time_scan);
1147 
1148 		data.beacon_counter = stats->general.beacon_counter;
1149 		data.beacon_average_energy =
1150 			stats->general.beacon_average_energy;
1151 		flags = stats->flag;
1152 	}
1153 	data.flags = flags;
1154 
1155 	iwl_mvm_rx_stats_check_trigger(mvm, pkt);
1156 
1157 	ieee80211_iterate_active_interfaces(mvm->hw,
1158 					    IEEE80211_IFACE_ITER_NORMAL,
1159 					    iwl_mvm_stat_iterator,
1160 					    &data);
1161 
1162 	if (!iwl_mvm_has_new_rx_api(mvm))
1163 		return;
1164 
1165 	if (!iwl_mvm_has_new_rx_stats_api(mvm)) {
1166 		struct iwl_notif_statistics_v11 *v11 = (void *)&pkt->data;
1167 
1168 		energy = (void *)&v11->load_stats.avg_energy;
1169 		bytes = (void *)&v11->load_stats.byte_count;
1170 		air_time = (void *)&v11->load_stats.air_time;
1171 	} else {
1172 		struct iwl_notif_statistics *stats = (void *)&pkt->data;
1173 
1174 		energy = (void *)&stats->load_stats.avg_energy;
1175 		bytes = (void *)&stats->load_stats.byte_count;
1176 		air_time = (void *)&stats->load_stats.air_time;
1177 	}
1178 	ieee80211_iterate_stations_atomic(mvm->hw, iwl_mvm_stats_energy_iter,
1179 					  energy);
1180 
1181 	/*
1182 	 * Don't update in case the statistics are not cleared, since
1183 	 * we will end up counting twice the same airtime, once in TCM
1184 	 * request and once in statistics notification.
1185 	 */
1186 	if (le32_to_cpu(flags) & IWL_STATISTICS_REPLY_FLG_CLEAR)
1187 		iwl_mvm_update_tcm_from_stats(mvm, air_time, bytes);
1188 
1189 }
1190 
iwl_mvm_rx_statistics(struct iwl_mvm * mvm,struct iwl_rx_cmd_buffer * rxb)1191 void iwl_mvm_rx_statistics(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
1192 {
1193 	iwl_mvm_handle_rx_statistics(mvm, rxb_addr(rxb));
1194 }
1195 
iwl_mvm_window_status_notif(struct iwl_mvm * mvm,struct iwl_rx_cmd_buffer * rxb)1196 void iwl_mvm_window_status_notif(struct iwl_mvm *mvm,
1197 				 struct iwl_rx_cmd_buffer *rxb)
1198 {
1199 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1200 	struct iwl_ba_window_status_notif *notif = (void *)pkt->data;
1201 	int i;
1202 
1203 	BUILD_BUG_ON(ARRAY_SIZE(notif->ra_tid) != BA_WINDOW_STREAMS_MAX);
1204 	BUILD_BUG_ON(ARRAY_SIZE(notif->mpdu_rx_count) != BA_WINDOW_STREAMS_MAX);
1205 	BUILD_BUG_ON(ARRAY_SIZE(notif->bitmap) != BA_WINDOW_STREAMS_MAX);
1206 	BUILD_BUG_ON(ARRAY_SIZE(notif->start_seq_num) != BA_WINDOW_STREAMS_MAX);
1207 
1208 	rcu_read_lock();
1209 	for (i = 0; i < BA_WINDOW_STREAMS_MAX; i++) {
1210 		struct ieee80211_sta *sta;
1211 		u8 sta_id, tid;
1212 		u64 bitmap;
1213 		u32 ssn;
1214 		u16 ratid;
1215 		u16 received_mpdu;
1216 
1217 		ratid = le16_to_cpu(notif->ra_tid[i]);
1218 		/* check that this TID is valid */
1219 		if (!(ratid & BA_WINDOW_STATUS_VALID_MSK))
1220 			continue;
1221 
1222 		received_mpdu = le16_to_cpu(notif->mpdu_rx_count[i]);
1223 		if (received_mpdu == 0)
1224 			continue;
1225 
1226 		tid = ratid & BA_WINDOW_STATUS_TID_MSK;
1227 		/* get the station */
1228 		sta_id = (ratid & BA_WINDOW_STATUS_STA_ID_MSK)
1229 			 >> BA_WINDOW_STATUS_STA_ID_POS;
1230 		if (IWL_FW_CHECK(mvm,
1231 				 sta_id >= mvm->fw->ucode_capa.num_stations,
1232 				 "Invalid sta id (%d) in BA window status notification\n",
1233 				 sta_id))
1234 			continue;
1235 		sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
1236 		if (IS_ERR_OR_NULL(sta))
1237 			continue;
1238 		bitmap = le64_to_cpu(notif->bitmap[i]);
1239 		ssn = le32_to_cpu(notif->start_seq_num[i]);
1240 
1241 		/* update mac80211 with the bitmap for the reordering buffer */
1242 		ieee80211_mark_rx_ba_filtered_frames(sta, tid, ssn, bitmap,
1243 						     received_mpdu);
1244 	}
1245 	rcu_read_unlock();
1246 }
1247