xref: /linux/drivers/net/wireless/intel/iwlwifi/mvm/rx.c (revision 28e02bc9f412c3344b0e1f0c657dc6305ac75b4c)
18e99ea8dSJohannes Berg // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
28e99ea8dSJohannes Berg /*
3740dfeccSMiri Korenblit  * Copyright (C) 2012-2014, 2018-2024 Intel Corporation
48e99ea8dSJohannes Berg  * Copyright (C) 2013-2015 Intel Mobile Communications GmbH
58e99ea8dSJohannes Berg  * Copyright (C) 2016-2017 Intel Deutschland GmbH
68e99ea8dSJohannes Berg  */
7c5aaa8beSWang Xuerui #include <asm/unaligned.h>
8a399f980SJohannes Berg #include <linux/etherdevice.h>
9e705c121SKalle Valo #include <linux/skbuff.h>
10e705c121SKalle Valo #include "iwl-trans.h"
11e705c121SKalle Valo #include "mvm.h"
12e705c121SKalle Valo #include "fw-api.h"
13e705c121SKalle Valo 
14e705c121SKalle Valo /*
15e705c121SKalle Valo  * iwl_mvm_rx_rx_phy_cmd - REPLY_RX_PHY_CMD handler
16e705c121SKalle Valo  *
17e705c121SKalle Valo  * Copies the phy information in mvm->last_phy_info, it will be used when the
18e705c121SKalle Valo  * actual data will come from the fw in the next packet.
19e705c121SKalle Valo  */
20e705c121SKalle Valo void iwl_mvm_rx_rx_phy_cmd(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
21e705c121SKalle Valo {
22e705c121SKalle Valo 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
231e1a58beSJohannes Berg 	unsigned int pkt_len = iwl_rx_packet_payload_len(pkt);
241e1a58beSJohannes Berg 
251e1a58beSJohannes Berg 	if (unlikely(pkt_len < sizeof(mvm->last_phy_info)))
261e1a58beSJohannes Berg 		return;
27e705c121SKalle Valo 
28e705c121SKalle Valo 	memcpy(&mvm->last_phy_info, pkt->data, sizeof(mvm->last_phy_info));
29e705c121SKalle Valo 	mvm->ampdu_ref++;
30e705c121SKalle Valo 
31e705c121SKalle Valo #ifdef CONFIG_IWLWIFI_DEBUGFS
32e705c121SKalle Valo 	if (mvm->last_phy_info.phy_flags & cpu_to_le16(RX_RES_PHY_FLAGS_AGG)) {
33e705c121SKalle Valo 		spin_lock(&mvm->drv_stats_lock);
34e705c121SKalle Valo 		mvm->drv_rx_stats.ampdu_count++;
35e705c121SKalle Valo 		spin_unlock(&mvm->drv_stats_lock);
36e705c121SKalle Valo 	}
37e705c121SKalle Valo #endif
38e705c121SKalle Valo }
39e705c121SKalle Valo 
40e705c121SKalle Valo /*
41e705c121SKalle Valo  * iwl_mvm_pass_packet_to_mac80211 - builds the packet for mac80211
42e705c121SKalle Valo  *
43e705c121SKalle Valo  * Adds the rxb to a new skb and give it to mac80211
44e705c121SKalle Valo  */
45e705c121SKalle Valo static void iwl_mvm_pass_packet_to_mac80211(struct iwl_mvm *mvm,
4643ec72b7SJohannes Berg 					    struct ieee80211_sta *sta,
47e705c121SKalle Valo 					    struct napi_struct *napi,
48e705c121SKalle Valo 					    struct sk_buff *skb,
49e705c121SKalle Valo 					    struct ieee80211_hdr *hdr, u16 len,
50bdbc58abSDan Carpenter 					    u8 crypt_len,
51e705c121SKalle Valo 					    struct iwl_rx_cmd_buffer *rxb)
52e705c121SKalle Valo {
535cddd05cSJohannes Berg 	unsigned int hdrlen = ieee80211_hdrlen(hdr->frame_control);
545cddd05cSJohannes Berg 	unsigned int fraglen;
555cddd05cSJohannes Berg 
565cddd05cSJohannes Berg 	/*
575cddd05cSJohannes Berg 	 * The 'hdrlen' (plus the 8 bytes for the SNAP and the crypt_len,
585cddd05cSJohannes Berg 	 * but those are all multiples of 4 long) all goes away, but we
595cddd05cSJohannes Berg 	 * want the *end* of it, which is going to be the start of the IP
605cddd05cSJohannes Berg 	 * header, to be aligned when it gets pulled in.
615cddd05cSJohannes Berg 	 * The beginning of the skb->data is aligned on at least a 4-byte
625cddd05cSJohannes Berg 	 * boundary after allocation. Everything here is aligned at least
635cddd05cSJohannes Berg 	 * on a 2-byte boundary so we can just take hdrlen & 3 and pad by
645cddd05cSJohannes Berg 	 * the result.
655cddd05cSJohannes Berg 	 */
665cddd05cSJohannes Berg 	skb_reserve(skb, hdrlen & 3);
67e705c121SKalle Valo 
68e705c121SKalle Valo 	/* If frame is small enough to fit in skb->head, pull it completely.
69e705c121SKalle Valo 	 * If not, only pull ieee80211_hdr (including crypto if present, and
70e705c121SKalle Valo 	 * an additional 8 bytes for SNAP/ethertype, see below) so that
71e705c121SKalle Valo 	 * splice() or TCP coalesce are more efficient.
72e705c121SKalle Valo 	 *
73e705c121SKalle Valo 	 * Since, in addition, ieee80211_data_to_8023() always pull in at
74e705c121SKalle Valo 	 * least 8 bytes (possibly more for mesh) we can do the same here
75e705c121SKalle Valo 	 * to save the cost of doing it later. That still doesn't pull in
76e705c121SKalle Valo 	 * the actual IP header since the typical case has a SNAP header.
77e705c121SKalle Valo 	 * If the latter changes (there are efforts in the standards group
78e705c121SKalle Valo 	 * to do so) we should revisit this and ieee80211_data_to_8023().
79e705c121SKalle Valo 	 */
805cddd05cSJohannes Berg 	hdrlen = (len <= skb_tailroom(skb)) ? len : hdrlen + crypt_len + 8;
81e705c121SKalle Valo 
8259ae1d12SJohannes Berg 	skb_put_data(skb, hdr, hdrlen);
83e705c121SKalle Valo 	fraglen = len - hdrlen;
84e705c121SKalle Valo 
85e705c121SKalle Valo 	if (fraglen) {
863827cb59SJohannes Berg 		int offset = (u8 *)hdr + hdrlen -
873827cb59SJohannes Berg 			     (u8 *)rxb_addr(rxb) + rxb_offset(rxb);
88e705c121SKalle Valo 
89e705c121SKalle Valo 		skb_add_rx_frag(skb, 0, rxb_steal_page(rxb), offset,
90e705c121SKalle Valo 				fraglen, rxb->truesize);
91e705c121SKalle Valo 	}
92e705c121SKalle Valo 
9343ec72b7SJohannes Berg 	ieee80211_rx_napi(mvm->hw, sta, skb, napi);
94e705c121SKalle Valo }
95e705c121SKalle Valo 
96e705c121SKalle Valo /*
97e705c121SKalle Valo  * iwl_mvm_get_signal_strength - use new rx PHY INFO API
98e705c121SKalle Valo  * values are reported by the fw as positive values - need to negate
99e705c121SKalle Valo  * to obtain their dBM.  Account for missing antennas by replacing 0
100e705c121SKalle Valo  * values by -256dBm: practically 0 power and a non-feasible 8 bit value.
101e705c121SKalle Valo  */
102e705c121SKalle Valo static void iwl_mvm_get_signal_strength(struct iwl_mvm *mvm,
103e705c121SKalle Valo 					struct iwl_rx_phy_info *phy_info,
104e705c121SKalle Valo 					struct ieee80211_rx_status *rx_status)
105e705c121SKalle Valo {
10657b7b345SMiri Korenblit 	int energy_a, energy_b, max_energy;
107e705c121SKalle Valo 	u32 val;
108e705c121SKalle Valo 
109e705c121SKalle Valo 	val =
110e705c121SKalle Valo 	    le32_to_cpu(phy_info->non_cfg_phy[IWL_RX_INFO_ENERGY_ANT_ABC_IDX]);
111e705c121SKalle Valo 	energy_a = (val & IWL_RX_INFO_ENERGY_ANT_A_MSK) >>
112e705c121SKalle Valo 						IWL_RX_INFO_ENERGY_ANT_A_POS;
113e705c121SKalle Valo 	energy_a = energy_a ? -energy_a : S8_MIN;
114e705c121SKalle Valo 	energy_b = (val & IWL_RX_INFO_ENERGY_ANT_B_MSK) >>
115e705c121SKalle Valo 						IWL_RX_INFO_ENERGY_ANT_B_POS;
116e705c121SKalle Valo 	energy_b = energy_b ? -energy_b : S8_MIN;
117e705c121SKalle Valo 	max_energy = max(energy_a, energy_b);
118e705c121SKalle Valo 
11957b7b345SMiri Korenblit 	IWL_DEBUG_STATS(mvm, "energy In A %d B %d  , and max %d\n",
12057b7b345SMiri Korenblit 			energy_a, energy_b, max_energy);
121e705c121SKalle Valo 
122e705c121SKalle Valo 	rx_status->signal = max_energy;
123e705c121SKalle Valo 	rx_status->chains = (le16_to_cpu(phy_info->phy_flags) &
124e705c121SKalle Valo 				RX_RES_PHY_FLAGS_ANTENNA)
125e705c121SKalle Valo 					>> RX_RES_PHY_FLAGS_ANTENNA_POS;
126e705c121SKalle Valo 	rx_status->chain_signal[0] = energy_a;
127e705c121SKalle Valo 	rx_status->chain_signal[1] = energy_b;
128e705c121SKalle Valo }
129e705c121SKalle Valo 
130e705c121SKalle Valo /*
131e705c121SKalle Valo  * iwl_mvm_set_mac80211_rx_flag - translate fw status to mac80211 format
132e705c121SKalle Valo  * @mvm: the mvm object
133e705c121SKalle Valo  * @hdr: 80211 header
134e705c121SKalle Valo  * @stats: status in mac80211's format
135e705c121SKalle Valo  * @rx_pkt_status: status coming from fw
136e705c121SKalle Valo  *
137e705c121SKalle Valo  * returns non 0 value if the packet should be dropped
138e705c121SKalle Valo  */
139e705c121SKalle Valo static u32 iwl_mvm_set_mac80211_rx_flag(struct iwl_mvm *mvm,
140e705c121SKalle Valo 					struct ieee80211_hdr *hdr,
141e705c121SKalle Valo 					struct ieee80211_rx_status *stats,
142e705c121SKalle Valo 					u32 rx_pkt_status,
143e705c121SKalle Valo 					u8 *crypt_len)
144e705c121SKalle Valo {
145e705c121SKalle Valo 	if (!ieee80211_has_protected(hdr->frame_control) ||
146e705c121SKalle Valo 	    (rx_pkt_status & RX_MPDU_RES_STATUS_SEC_ENC_MSK) ==
147e705c121SKalle Valo 			     RX_MPDU_RES_STATUS_SEC_NO_ENC)
148e705c121SKalle Valo 		return 0;
149e705c121SKalle Valo 
150e705c121SKalle Valo 	/* packet was encrypted with unknown alg */
151e705c121SKalle Valo 	if ((rx_pkt_status & RX_MPDU_RES_STATUS_SEC_ENC_MSK) ==
152e705c121SKalle Valo 					RX_MPDU_RES_STATUS_SEC_ENC_ERR)
153e705c121SKalle Valo 		return 0;
154e705c121SKalle Valo 
155e705c121SKalle Valo 	switch (rx_pkt_status & RX_MPDU_RES_STATUS_SEC_ENC_MSK) {
156e705c121SKalle Valo 	case RX_MPDU_RES_STATUS_SEC_CCM_ENC:
157e705c121SKalle Valo 		/* alg is CCM: check MIC only */
158e705c121SKalle Valo 		if (!(rx_pkt_status & RX_MPDU_RES_STATUS_MIC_OK))
159e705c121SKalle Valo 			return -1;
160e705c121SKalle Valo 
161e705c121SKalle Valo 		stats->flag |= RX_FLAG_DECRYPTED;
162e705c121SKalle Valo 		*crypt_len = IEEE80211_CCMP_HDR_LEN;
163e705c121SKalle Valo 		return 0;
164e705c121SKalle Valo 
165e705c121SKalle Valo 	case RX_MPDU_RES_STATUS_SEC_TKIP_ENC:
166e705c121SKalle Valo 		/* Don't drop the frame and decrypt it in SW */
16757df3839SSara Sharon 		if (!fw_has_api(&mvm->fw->ucode_capa,
16857df3839SSara Sharon 				IWL_UCODE_TLV_API_DEPRECATE_TTAK) &&
16957df3839SSara Sharon 		    !(rx_pkt_status & RX_MPDU_RES_STATUS_TTAK_OK))
170e705c121SKalle Valo 			return 0;
171e705c121SKalle Valo 		*crypt_len = IEEE80211_TKIP_IV_LEN;
1725a2abdcaSGustavo A. R. Silva 		fallthrough;
173e705c121SKalle Valo 
174e705c121SKalle Valo 	case RX_MPDU_RES_STATUS_SEC_WEP_ENC:
175e705c121SKalle Valo 		if (!(rx_pkt_status & RX_MPDU_RES_STATUS_ICV_OK))
176e705c121SKalle Valo 			return -1;
177e705c121SKalle Valo 
178e705c121SKalle Valo 		stats->flag |= RX_FLAG_DECRYPTED;
179e705c121SKalle Valo 		if ((rx_pkt_status & RX_MPDU_RES_STATUS_SEC_ENC_MSK) ==
180e705c121SKalle Valo 				RX_MPDU_RES_STATUS_SEC_WEP_ENC)
181e705c121SKalle Valo 			*crypt_len = IEEE80211_WEP_IV_LEN;
182e705c121SKalle Valo 		return 0;
183e705c121SKalle Valo 
184e705c121SKalle Valo 	case RX_MPDU_RES_STATUS_SEC_EXT_ENC:
185e705c121SKalle Valo 		if (!(rx_pkt_status & RX_MPDU_RES_STATUS_MIC_OK))
186e705c121SKalle Valo 			return -1;
187e705c121SKalle Valo 		stats->flag |= RX_FLAG_DECRYPTED;
188e705c121SKalle Valo 		return 0;
189e705c121SKalle Valo 
190e705c121SKalle Valo 	default:
191baf41bc3SShaul Triebitz 		/* Expected in monitor (not having the keys) */
192baf41bc3SShaul Triebitz 		if (!mvm->monitor_on)
193876882b5SGolan Ben Ami 			IWL_WARN(mvm, "Unhandled alg: 0x%x\n", rx_pkt_status);
194e705c121SKalle Valo 	}
195e705c121SKalle Valo 
196e705c121SKalle Valo 	return 0;
197e705c121SKalle Valo }
198e705c121SKalle Valo 
1997d9d0d56SLuca Coelho static void iwl_mvm_rx_handle_tcm(struct iwl_mvm *mvm,
2007d9d0d56SLuca Coelho 				  struct ieee80211_sta *sta,
2017d9d0d56SLuca Coelho 				  struct ieee80211_hdr *hdr, u32 len,
2027d9d0d56SLuca Coelho 				  struct iwl_rx_phy_info *phy_info,
2037d9d0d56SLuca Coelho 				  u32 rate_n_flags)
2047d9d0d56SLuca Coelho {
2057d9d0d56SLuca Coelho 	struct iwl_mvm_sta *mvmsta;
2067d9d0d56SLuca Coelho 	struct iwl_mvm_tcm_mac *mdata;
2077d9d0d56SLuca Coelho 	int mac;
2087d9d0d56SLuca Coelho 	int ac = IEEE80211_AC_BE; /* treat non-QoS as BE */
209b0ffe455SJohannes Berg 	struct iwl_mvm_vif *mvmvif;
210b0ffe455SJohannes Berg 	/* expected throughput in 100Kbps, single stream, 20 MHz */
211b0ffe455SJohannes Berg 	static const u8 thresh_tpt[] = {
212b0ffe455SJohannes Berg 		9, 18, 30, 42, 60, 78, 90, 96, 120, 135,
213b0ffe455SJohannes Berg 	};
214b0ffe455SJohannes Berg 	u16 thr;
2157d9d0d56SLuca Coelho 
216aedb2b38SAnjaneyulu 	if (ieee80211_is_data_qos(hdr->frame_control)) {
217aedb2b38SAnjaneyulu 		u8 tid = ieee80211_get_tid(hdr);
218aedb2b38SAnjaneyulu 
219aedb2b38SAnjaneyulu 		if (tid < IWL_MAX_TID_COUNT)
220aedb2b38SAnjaneyulu 			ac = tid_to_mac80211_ac[tid];
221aedb2b38SAnjaneyulu 	}
2227d9d0d56SLuca Coelho 
2237d9d0d56SLuca Coelho 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
2247d9d0d56SLuca Coelho 	mac = mvmsta->mac_id_n_color & FW_CTXT_ID_MSK;
2257d9d0d56SLuca Coelho 
2267d9d0d56SLuca Coelho 	if (time_after(jiffies, mvm->tcm.ts + MVM_TCM_PERIOD))
2277d9d0d56SLuca Coelho 		schedule_delayed_work(&mvm->tcm.work, 0);
2287d9d0d56SLuca Coelho 	mdata = &mvm->tcm.data[mac];
2297d9d0d56SLuca Coelho 	mdata->rx.pkts[ac]++;
2307d9d0d56SLuca Coelho 
2317d9d0d56SLuca Coelho 	/* count the airtime only once for each ampdu */
2327d9d0d56SLuca Coelho 	if (mdata->rx.last_ampdu_ref != mvm->ampdu_ref) {
2337d9d0d56SLuca Coelho 		mdata->rx.last_ampdu_ref = mvm->ampdu_ref;
2347d9d0d56SLuca Coelho 		mdata->rx.airtime += le16_to_cpu(phy_info->frame_time);
2357d9d0d56SLuca Coelho 	}
2367d9d0d56SLuca Coelho 
23748c6ebc1SMiri Korenblit 	if (!(rate_n_flags & (RATE_MCS_HT_MSK_V1 | RATE_MCS_VHT_MSK_V1)))
2387d9d0d56SLuca Coelho 		return;
2397d9d0d56SLuca Coelho 
240b0ffe455SJohannes Berg 	mvmvif = iwl_mvm_vif_from_mac80211(mvmsta->vif);
241b0ffe455SJohannes Berg 
242b0ffe455SJohannes Berg 	if (mdata->opened_rx_ba_sessions ||
243b0ffe455SJohannes Berg 	    mdata->uapsd_nonagg_detect.detected ||
244650cadb7SGregory Greenman 	    (!mvmvif->deflink.queue_params[IEEE80211_AC_VO].uapsd &&
245650cadb7SGregory Greenman 	     !mvmvif->deflink.queue_params[IEEE80211_AC_VI].uapsd &&
246650cadb7SGregory Greenman 	     !mvmvif->deflink.queue_params[IEEE80211_AC_BE].uapsd &&
247650cadb7SGregory Greenman 	     !mvmvif->deflink.queue_params[IEEE80211_AC_BK].uapsd) ||
248c8ee33e1SGregory Greenman 	    mvmsta->deflink.sta_id != mvmvif->deflink.ap_sta_id)
249b0ffe455SJohannes Berg 		return;
250b0ffe455SJohannes Berg 
25148c6ebc1SMiri Korenblit 	if (rate_n_flags & RATE_MCS_HT_MSK_V1) {
25248c6ebc1SMiri Korenblit 		thr = thresh_tpt[rate_n_flags & RATE_HT_MCS_RATE_CODE_MSK_V1];
25348c6ebc1SMiri Korenblit 		thr *= 1 + ((rate_n_flags & RATE_HT_MCS_NSS_MSK_V1) >>
25448c6ebc1SMiri Korenblit 					RATE_HT_MCS_NSS_POS_V1);
255b0ffe455SJohannes Berg 	} else {
256b0ffe455SJohannes Berg 		if (WARN_ON((rate_n_flags & RATE_VHT_MCS_RATE_CODE_MSK) >=
257b0ffe455SJohannes Berg 				ARRAY_SIZE(thresh_tpt)))
258b0ffe455SJohannes Berg 			return;
259b0ffe455SJohannes Berg 		thr = thresh_tpt[rate_n_flags & RATE_VHT_MCS_RATE_CODE_MSK];
260774302d2SMordechay Goodstein 		thr *= 1 + FIELD_GET(RATE_MCS_NSS_MSK, rate_n_flags);
261b0ffe455SJohannes Berg 	}
262b0ffe455SJohannes Berg 
26348c6ebc1SMiri Korenblit 	thr <<= ((rate_n_flags & RATE_MCS_CHAN_WIDTH_MSK_V1) >>
264b0ffe455SJohannes Berg 				RATE_MCS_CHAN_WIDTH_POS);
265b0ffe455SJohannes Berg 
266b0ffe455SJohannes Berg 	mdata->uapsd_nonagg_detect.rx_bytes += len;
267b0ffe455SJohannes Berg 	ewma_rate_add(&mdata->uapsd_nonagg_detect.rate, thr);
2687d9d0d56SLuca Coelho }
2697d9d0d56SLuca Coelho 
270e705c121SKalle Valo static void iwl_mvm_rx_csum(struct ieee80211_sta *sta,
271e705c121SKalle Valo 			    struct sk_buff *skb,
272e705c121SKalle Valo 			    u32 status)
273e705c121SKalle Valo {
274e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
275e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(mvmsta->vif);
276e705c121SKalle Valo 
277e705c121SKalle Valo 	if (mvmvif->features & NETIF_F_RXCSUM &&
278e705c121SKalle Valo 	    status & RX_MPDU_RES_STATUS_CSUM_DONE &&
279e705c121SKalle Valo 	    status & RX_MPDU_RES_STATUS_CSUM_OK)
280e705c121SKalle Valo 		skb->ip_summed = CHECKSUM_UNNECESSARY;
281e705c121SKalle Valo }
282e705c121SKalle Valo 
283e705c121SKalle Valo /*
284e705c121SKalle Valo  * iwl_mvm_rx_rx_mpdu - REPLY_RX_MPDU_CMD handler
285e705c121SKalle Valo  *
286e705c121SKalle Valo  * Handles the actual data of the Rx packet from the fw
287e705c121SKalle Valo  */
288e705c121SKalle Valo void iwl_mvm_rx_rx_mpdu(struct iwl_mvm *mvm, struct napi_struct *napi,
289e705c121SKalle Valo 			struct iwl_rx_cmd_buffer *rxb)
290e705c121SKalle Valo {
291e705c121SKalle Valo 	struct ieee80211_hdr *hdr;
292e705c121SKalle Valo 	struct ieee80211_rx_status *rx_status;
293e705c121SKalle Valo 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
294e705c121SKalle Valo 	struct iwl_rx_phy_info *phy_info;
295e705c121SKalle Valo 	struct iwl_rx_mpdu_res_start *rx_res;
296a399f980SJohannes Berg 	struct ieee80211_sta *sta = NULL;
297e705c121SKalle Valo 	struct sk_buff *skb;
298efc0ec5aSJohannes Berg 	u32 len, pkt_len = iwl_rx_packet_payload_len(pkt);
299e705c121SKalle Valo 	u32 rate_n_flags;
300e705c121SKalle Valo 	u32 rx_pkt_status;
301e705c121SKalle Valo 	u8 crypt_len = 0;
302e705c121SKalle Valo 
303b8aba27cSJohannes Berg 	if (unlikely(pkt_len < sizeof(*rx_res))) {
304b8aba27cSJohannes Berg 		IWL_DEBUG_DROP(mvm, "Bad REPLY_RX_MPDU_CMD size\n");
305b8aba27cSJohannes Berg 		return;
306b8aba27cSJohannes Berg 	}
307b8aba27cSJohannes Berg 
308e705c121SKalle Valo 	phy_info = &mvm->last_phy_info;
309e705c121SKalle Valo 	rx_res = (struct iwl_rx_mpdu_res_start *)pkt->data;
310e705c121SKalle Valo 	hdr = (struct ieee80211_hdr *)(pkt->data + sizeof(*rx_res));
311e705c121SKalle Valo 	len = le16_to_cpu(rx_res->byte_count);
312efc0ec5aSJohannes Berg 
313efc0ec5aSJohannes Berg 	if (unlikely(len + sizeof(*rx_res) + sizeof(__le32) > pkt_len)) {
314efc0ec5aSJohannes Berg 		IWL_DEBUG_DROP(mvm, "FW lied about packet len\n");
315efc0ec5aSJohannes Berg 		return;
316efc0ec5aSJohannes Berg 	}
317efc0ec5aSJohannes Berg 
318c5aaa8beSWang Xuerui 	rx_pkt_status = get_unaligned_le32((__le32 *)
319e705c121SKalle Valo 		(pkt->data + sizeof(*rx_res) + len));
320e705c121SKalle Valo 
321e705c121SKalle Valo 	/* Dont use dev_alloc_skb(), we'll have enough headroom once
322e705c121SKalle Valo 	 * ieee80211_hdr pulled.
323e705c121SKalle Valo 	 */
324e705c121SKalle Valo 	skb = alloc_skb(128, GFP_ATOMIC);
325e705c121SKalle Valo 	if (!skb) {
326e705c121SKalle Valo 		IWL_ERR(mvm, "alloc_skb failed\n");
327e705c121SKalle Valo 		return;
328e705c121SKalle Valo 	}
329e705c121SKalle Valo 
330e705c121SKalle Valo 	rx_status = IEEE80211_SKB_RXCB(skb);
331e705c121SKalle Valo 
332e705c121SKalle Valo 	/*
333e705c121SKalle Valo 	 * Keep packets with CRC errors (and with overrun) for monitor mode
334e705c121SKalle Valo 	 * (otherwise the firmware discards them) but mark them as bad.
335e705c121SKalle Valo 	 */
336e705c121SKalle Valo 	if (!(rx_pkt_status & RX_MPDU_RES_STATUS_CRC_OK) ||
337e705c121SKalle Valo 	    !(rx_pkt_status & RX_MPDU_RES_STATUS_OVERRUN_OK)) {
338e705c121SKalle Valo 		IWL_DEBUG_RX(mvm, "Bad CRC or FIFO: 0x%08X.\n", rx_pkt_status);
339e705c121SKalle Valo 		rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
340e705c121SKalle Valo 	}
341e705c121SKalle Valo 
342e705c121SKalle Valo 	/* This will be used in several places later */
343e705c121SKalle Valo 	rate_n_flags = le32_to_cpu(phy_info->rate_n_flags);
344e705c121SKalle Valo 
345e705c121SKalle Valo 	/* rx_status carries information about the packet to mac80211 */
346e705c121SKalle Valo 	rx_status->mactime = le64_to_cpu(phy_info->timestamp);
347e705c121SKalle Valo 	rx_status->device_timestamp = le32_to_cpu(phy_info->system_timestamp);
348e705c121SKalle Valo 	rx_status->band =
349e705c121SKalle Valo 		(phy_info->phy_flags & cpu_to_le16(RX_RES_PHY_FLAGS_BAND_24)) ?
35057fbcce3SJohannes Berg 				NL80211_BAND_2GHZ : NL80211_BAND_5GHZ;
351e705c121SKalle Valo 	rx_status->freq =
352e705c121SKalle Valo 		ieee80211_channel_to_frequency(le16_to_cpu(phy_info->channel),
353e705c121SKalle Valo 					       rx_status->band);
35477fe7395SSara Sharon 
35577fe7395SSara Sharon 	/* TSF as indicated by the firmware  is at INA time */
35677fe7395SSara Sharon 	rx_status->flag |= RX_FLAG_MACTIME_PLCP_START;
357e705c121SKalle Valo 
358e705c121SKalle Valo 	iwl_mvm_get_signal_strength(mvm, phy_info, rx_status);
359e705c121SKalle Valo 
360e705c121SKalle Valo 	IWL_DEBUG_STATS_LIMIT(mvm, "Rssi %d, TSF %llu\n", rx_status->signal,
361e705c121SKalle Valo 			      (unsigned long long)rx_status->mactime);
362e705c121SKalle Valo 
363e705c121SKalle Valo 	rcu_read_lock();
364a399f980SJohannes Berg 	if (rx_pkt_status & RX_MPDU_RES_STATUS_SRC_STA_FOUND) {
365a399f980SJohannes Berg 		u32 id = rx_pkt_status & RX_MPDU_RES_STATUS_STA_ID_MSK;
366a399f980SJohannes Berg 
367a399f980SJohannes Berg 		id >>= RX_MDPU_RES_STATUS_STA_ID_SHIFT;
368a399f980SJohannes Berg 
369be9ae34eSNathan Errera 		if (!WARN_ON_ONCE(id >= mvm->fw->ucode_capa.num_stations)) {
370a399f980SJohannes Berg 			sta = rcu_dereference(mvm->fw_id_to_mac_id[id]);
371a399f980SJohannes Berg 			if (IS_ERR(sta))
372a399f980SJohannes Berg 				sta = NULL;
373a399f980SJohannes Berg 		}
374a399f980SJohannes Berg 	} else if (!is_multicast_ether_addr(hdr->addr2)) {
375a399f980SJohannes Berg 		/* This is fine since we prevent two stations with the same
376a399f980SJohannes Berg 		 * address from being added.
377e705c121SKalle Valo 		 */
378a399f980SJohannes Berg 		sta = ieee80211_find_sta_by_ifaddr(mvm->hw, hdr->addr2, NULL);
379e705c121SKalle Valo 	}
380e705c121SKalle Valo 
381e705c121SKalle Valo 	if (sta) {
382e705c121SKalle Valo 		struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
383147eb05fSEmmanuel Grumbach 		struct ieee80211_vif *vif = mvmsta->vif;
384147eb05fSEmmanuel Grumbach 		struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
385147eb05fSEmmanuel Grumbach 
386147eb05fSEmmanuel Grumbach 		/*
387147eb05fSEmmanuel Grumbach 		 * Don't even try to decrypt a MCAST frame that was received
388147eb05fSEmmanuel Grumbach 		 * before the managed vif is authorized, we'd fail anyway.
389147eb05fSEmmanuel Grumbach 		 */
3908e5a2636SAyala Beker 		if (is_multicast_ether_addr(hdr->addr1) &&
3918e5a2636SAyala Beker 		    vif->type == NL80211_IFTYPE_STATION &&
392147eb05fSEmmanuel Grumbach 		    !mvmvif->authorized &&
3938e5a2636SAyala Beker 		    ieee80211_has_protected(hdr->frame_control)) {
394147eb05fSEmmanuel Grumbach 			IWL_DEBUG_DROP(mvm, "MCAST before the vif is authorized\n");
395147eb05fSEmmanuel Grumbach 			kfree_skb(skb);
396147eb05fSEmmanuel Grumbach 			rcu_read_unlock();
397147eb05fSEmmanuel Grumbach 			return;
398147eb05fSEmmanuel Grumbach 		}
399147eb05fSEmmanuel Grumbach 	}
400147eb05fSEmmanuel Grumbach 
401147eb05fSEmmanuel Grumbach 	/*
402147eb05fSEmmanuel Grumbach 	 * drop the packet if it has failed being decrypted by HW
403147eb05fSEmmanuel Grumbach 	 */
404147eb05fSEmmanuel Grumbach 	if (iwl_mvm_set_mac80211_rx_flag(mvm, hdr, rx_status, rx_pkt_status,
405147eb05fSEmmanuel Grumbach 					 &crypt_len)) {
406147eb05fSEmmanuel Grumbach 		IWL_DEBUG_DROP(mvm, "Bad decryption results 0x%08x\n",
407147eb05fSEmmanuel Grumbach 			       rx_pkt_status);
408147eb05fSEmmanuel Grumbach 		kfree_skb(skb);
409147eb05fSEmmanuel Grumbach 		rcu_read_unlock();
410147eb05fSEmmanuel Grumbach 		return;
411147eb05fSEmmanuel Grumbach 	}
412147eb05fSEmmanuel Grumbach 
413147eb05fSEmmanuel Grumbach 	if (sta) {
414147eb05fSEmmanuel Grumbach 		struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
415d3a108a4SAndrei Otcheretianski 		struct ieee80211_vif *tx_blocked_vif =
416d3a108a4SAndrei Otcheretianski 			rcu_dereference(mvm->csa_tx_blocked_vif);
4176c042d75SSara Sharon 		struct iwl_fw_dbg_trigger_tlv *trig;
4186c042d75SSara Sharon 		struct ieee80211_vif *vif = mvmsta->vif;
419e705c121SKalle Valo 
420a399f980SJohannes Berg 		/* We have tx blocked stations (with CS bit). If we heard
421a399f980SJohannes Berg 		 * frames from a blocked station on a new channel we can
422a399f980SJohannes Berg 		 * TX to it again.
423a399f980SJohannes Berg 		 */
4246c042d75SSara Sharon 		if (unlikely(tx_blocked_vif) && vif == tx_blocked_vif) {
425d3a108a4SAndrei Otcheretianski 			struct iwl_mvm_vif *mvmvif =
426d3a108a4SAndrei Otcheretianski 				iwl_mvm_vif_from_mac80211(tx_blocked_vif);
427d3a108a4SAndrei Otcheretianski 
428d3a108a4SAndrei Otcheretianski 			if (mvmvif->csa_target_freq == rx_status->freq)
429d3a108a4SAndrei Otcheretianski 				iwl_mvm_sta_modify_disable_tx_ap(mvm, sta,
430d3a108a4SAndrei Otcheretianski 								 false);
431d3a108a4SAndrei Otcheretianski 		}
432a399f980SJohannes Berg 
433ecaf71deSGregory Greenman 		rs_update_last_rssi(mvm, mvmsta, rx_status);
434e705c121SKalle Valo 
4356c042d75SSara Sharon 		trig = iwl_fw_dbg_trigger_on(&mvm->fwrt,
4366c042d75SSara Sharon 					     ieee80211_vif_to_wdev(vif),
4376c042d75SSara Sharon 					     FW_DBG_TRIGGER_RSSI);
4386c042d75SSara Sharon 
4396c042d75SSara Sharon 		if (trig && ieee80211_is_beacon(hdr->frame_control)) {
440e705c121SKalle Valo 			struct iwl_fw_dbg_trigger_low_rssi *rssi_trig;
441e705c121SKalle Valo 			s32 rssi;
442e705c121SKalle Valo 
443e705c121SKalle Valo 			rssi_trig = (void *)trig->data;
444e705c121SKalle Valo 			rssi = le32_to_cpu(rssi_trig->rssi);
445e705c121SKalle Valo 
4466c042d75SSara Sharon 			if (rx_status->signal < rssi)
4477174beb6SJohannes Berg 				iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
4487174beb6SJohannes Berg 							NULL);
449e705c121SKalle Valo 		}
450e705c121SKalle Valo 
4517d9d0d56SLuca Coelho 		if (!mvm->tcm.paused && len >= sizeof(*hdr) &&
4527d9d0d56SLuca Coelho 		    !is_multicast_ether_addr(hdr->addr1) &&
4537d9d0d56SLuca Coelho 		    ieee80211_is_data(hdr->frame_control))
4547d9d0d56SLuca Coelho 			iwl_mvm_rx_handle_tcm(mvm, sta, hdr, len, phy_info,
4557d9d0d56SLuca Coelho 					      rate_n_flags);
4567d9d0d56SLuca Coelho 
457a399f980SJohannes Berg 		if (ieee80211_is_data(hdr->frame_control))
458e705c121SKalle Valo 			iwl_mvm_rx_csum(sta, skb, rx_pkt_status);
459a399f980SJohannes Berg 	}
460e705c121SKalle Valo 	rcu_read_unlock();
461e705c121SKalle Valo 
462e705c121SKalle Valo 	/* set the preamble flag if appropriate */
463e705c121SKalle Valo 	if (phy_info->phy_flags & cpu_to_le16(RX_RES_PHY_FLAGS_SHORT_PREAMBLE))
4647fdd69c5SJohannes Berg 		rx_status->enc_flags |= RX_ENC_FLAG_SHORTPRE;
465e705c121SKalle Valo 
466e705c121SKalle Valo 	if (phy_info->phy_flags & cpu_to_le16(RX_RES_PHY_FLAGS_AGG)) {
467e705c121SKalle Valo 		/*
468e705c121SKalle Valo 		 * We know which subframes of an A-MPDU belong
469e705c121SKalle Valo 		 * together since we get a single PHY response
470e705c121SKalle Valo 		 * from the firmware for all of them
471e705c121SKalle Valo 		 */
472e705c121SKalle Valo 		rx_status->flag |= RX_FLAG_AMPDU_DETAILS;
473e705c121SKalle Valo 		rx_status->ampdu_reference = mvm->ampdu_ref;
474e705c121SKalle Valo 	}
475e705c121SKalle Valo 
476e705c121SKalle Valo 	/* Set up the HT phy flags */
47748c6ebc1SMiri Korenblit 	switch (rate_n_flags & RATE_MCS_CHAN_WIDTH_MSK_V1) {
478e705c121SKalle Valo 	case RATE_MCS_CHAN_WIDTH_20:
479e705c121SKalle Valo 		break;
480e705c121SKalle Valo 	case RATE_MCS_CHAN_WIDTH_40:
481da6a4352SJohannes Berg 		rx_status->bw = RATE_INFO_BW_40;
482e705c121SKalle Valo 		break;
483e705c121SKalle Valo 	case RATE_MCS_CHAN_WIDTH_80:
484da6a4352SJohannes Berg 		rx_status->bw = RATE_INFO_BW_80;
485e705c121SKalle Valo 		break;
486e705c121SKalle Valo 	case RATE_MCS_CHAN_WIDTH_160:
487da6a4352SJohannes Berg 		rx_status->bw = RATE_INFO_BW_160;
488e705c121SKalle Valo 		break;
489e705c121SKalle Valo 	}
49048c6ebc1SMiri Korenblit 	if (!(rate_n_flags & RATE_MCS_CCK_MSK_V1) &&
49148c6ebc1SMiri Korenblit 	    rate_n_flags & RATE_MCS_SGI_MSK_V1)
4927fdd69c5SJohannes Berg 		rx_status->enc_flags |= RX_ENC_FLAG_SHORT_GI;
493e705c121SKalle Valo 	if (rate_n_flags & RATE_HT_MCS_GF_MSK)
4947fdd69c5SJohannes Berg 		rx_status->enc_flags |= RX_ENC_FLAG_HT_GF;
49548c6ebc1SMiri Korenblit 	if (rate_n_flags & RATE_MCS_LDPC_MSK_V1)
4967fdd69c5SJohannes Berg 		rx_status->enc_flags |= RX_ENC_FLAG_LDPC;
49748c6ebc1SMiri Korenblit 	if (rate_n_flags & RATE_MCS_HT_MSK_V1) {
49877e40945SSara Sharon 		u8 stbc = (rate_n_flags & RATE_MCS_STBC_MSK) >>
499e705c121SKalle Valo 				RATE_MCS_STBC_POS;
500da6a4352SJohannes Berg 		rx_status->encoding = RX_ENC_HT;
50148c6ebc1SMiri Korenblit 		rx_status->rate_idx = rate_n_flags & RATE_HT_MCS_INDEX_MSK_V1;
5027fdd69c5SJohannes Berg 		rx_status->enc_flags |= stbc << RX_ENC_FLAG_STBC_SHIFT;
50348c6ebc1SMiri Korenblit 	} else if (rate_n_flags & RATE_MCS_VHT_MSK_V1) {
50477e40945SSara Sharon 		u8 stbc = (rate_n_flags & RATE_MCS_STBC_MSK) >>
505e705c121SKalle Valo 				RATE_MCS_STBC_POS;
5068613c948SJohannes Berg 		rx_status->nss =
507774302d2SMordechay Goodstein 			FIELD_GET(RATE_MCS_NSS_MSK, rate_n_flags) + 1;
508e705c121SKalle Valo 		rx_status->rate_idx = rate_n_flags & RATE_VHT_MCS_RATE_CODE_MSK;
509da6a4352SJohannes Berg 		rx_status->encoding = RX_ENC_VHT;
5107fdd69c5SJohannes Berg 		rx_status->enc_flags |= stbc << RX_ENC_FLAG_STBC_SHIFT;
511e705c121SKalle Valo 		if (rate_n_flags & RATE_MCS_BF_MSK)
5127fdd69c5SJohannes Berg 			rx_status->enc_flags |= RX_ENC_FLAG_BF;
513e705c121SKalle Valo 	} else {
514cb2de6bbSSara Sharon 		int rate = iwl_mvm_legacy_rate_to_mac80211_idx(rate_n_flags,
515e705c121SKalle Valo 							       rx_status->band);
516cb2de6bbSSara Sharon 
517cb2de6bbSSara Sharon 		if (WARN(rate < 0 || rate > 0xFF,
518cb2de6bbSSara Sharon 			 "Invalid rate flags 0x%x, band %d,\n",
519cb2de6bbSSara Sharon 			 rate_n_flags, rx_status->band)) {
520cb2de6bbSSara Sharon 			kfree_skb(skb);
521cb2de6bbSSara Sharon 			return;
522cb2de6bbSSara Sharon 		}
523cb2de6bbSSara Sharon 		rx_status->rate_idx = rate;
524e705c121SKalle Valo 	}
525e705c121SKalle Valo 
526e705c121SKalle Valo #ifdef CONFIG_IWLWIFI_DEBUGFS
527e705c121SKalle Valo 	iwl_mvm_update_frame_stats(mvm, rate_n_flags,
528e705c121SKalle Valo 				   rx_status->flag & RX_FLAG_AMPDU_DETAILS);
529e705c121SKalle Valo #endif
530a339e918SLuca Coelho 
531a339e918SLuca Coelho 	if (unlikely((ieee80211_is_beacon(hdr->frame_control) ||
532a339e918SLuca Coelho 		      ieee80211_is_probe_resp(hdr->frame_control)) &&
533a339e918SLuca Coelho 		     mvm->sched_scan_pass_all == SCHED_SCAN_PASS_ALL_ENABLED))
534a339e918SLuca Coelho 		mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_FOUND;
535a339e918SLuca Coelho 
53643ec72b7SJohannes Berg 	if (unlikely(ieee80211_is_beacon(hdr->frame_control) ||
53743ec72b7SJohannes Berg 		     ieee80211_is_probe_resp(hdr->frame_control)))
5389285ec4cSJason A. Donenfeld 		rx_status->boottime_ns = ktime_get_boottime_ns();
53943ec72b7SJohannes Berg 
54043ec72b7SJohannes Berg 	iwl_mvm_pass_packet_to_mac80211(mvm, sta, napi, skb, hdr, len,
541bdbc58abSDan Carpenter 					crypt_len, rxb);
542e705c121SKalle Valo }
543e705c121SKalle Valo 
544e705c121SKalle Valo struct iwl_mvm_stat_data {
545e705c121SKalle Valo 	struct iwl_mvm *mvm;
546290d5e49SMordechay Goodstein 	__le32 flags;
547e705c121SKalle Valo 	__le32 mac_id;
548e705c121SKalle Valo 	u8 beacon_filter_average_energy;
549853f4954SMordechay Goodstein 	__le32 *beacon_counter;
550853f4954SMordechay Goodstein 	u8 *beacon_average_energy;
551e705c121SKalle Valo };
552e705c121SKalle Valo 
5536324c173SMordechay Goodstein struct iwl_mvm_stat_data_all_macs {
5546324c173SMordechay Goodstein 	struct iwl_mvm *mvm;
5556324c173SMordechay Goodstein 	__le32 flags;
556b6e3d1baSAnjaneyulu 	struct iwl_stats_ntfy_per_mac *per_mac;
5576324c173SMordechay Goodstein };
5586324c173SMordechay Goodstein 
55930ce0390SMiri Korenblit static void iwl_mvm_update_link_sig(struct ieee80211_vif *vif, int sig,
560*28e02bc9SJohannes Berg 				    struct iwl_mvm_vif_link_info *link_info,
561*28e02bc9SJohannes Berg 				    struct ieee80211_bss_conf *bss_conf)
562e705c121SKalle Valo {
563c1e458b9SIlan Peer 	struct iwl_mvm *mvm = iwl_mvm_vif_from_mac80211(vif)->mvm;
564c1e458b9SIlan Peer 	int thold = bss_conf->cqm_rssi_thold;
565c1e458b9SIlan Peer 	int hyst = bss_conf->cqm_rssi_hyst;
5666324c173SMordechay Goodstein 	int last_event;
56730ce0390SMiri Korenblit 	s8 exit_esr_thresh;
568e705c121SKalle Valo 
569e705c121SKalle Valo 	if (sig == 0) {
570e705c121SKalle Valo 		IWL_DEBUG_RX(mvm, "RSSI is 0 - skip signal based decision\n");
571e705c121SKalle Valo 		return;
572e705c121SKalle Valo 	}
573e705c121SKalle Valo 
574c1e458b9SIlan Peer 	link_info->bf_data.ave_beacon_signal = sig;
575e705c121SKalle Valo 
576e705c121SKalle Valo 	/* BT Coex */
577c1e458b9SIlan Peer 	if (link_info->bf_data.bt_coex_min_thold !=
578c1e458b9SIlan Peer 	    link_info->bf_data.bt_coex_max_thold) {
579c1e458b9SIlan Peer 		last_event = link_info->bf_data.last_bt_coex_event;
580c1e458b9SIlan Peer 		if (sig > link_info->bf_data.bt_coex_max_thold &&
581c1e458b9SIlan Peer 		    (last_event <= link_info->bf_data.bt_coex_min_thold ||
582e705c121SKalle Valo 		     last_event == 0)) {
583c1e458b9SIlan Peer 			link_info->bf_data.last_bt_coex_event = sig;
584e705c121SKalle Valo 			IWL_DEBUG_RX(mvm, "cqm_iterator bt coex high %d\n",
585e705c121SKalle Valo 				     sig);
586e705c121SKalle Valo 			iwl_mvm_bt_rssi_event(mvm, vif, RSSI_EVENT_HIGH);
587c1e458b9SIlan Peer 		} else if (sig < link_info->bf_data.bt_coex_min_thold &&
588c1e458b9SIlan Peer 			   (last_event >= link_info->bf_data.bt_coex_max_thold ||
589e705c121SKalle Valo 			    last_event == 0)) {
590c1e458b9SIlan Peer 			link_info->bf_data.last_bt_coex_event = sig;
591e705c121SKalle Valo 			IWL_DEBUG_RX(mvm, "cqm_iterator bt coex low %d\n",
592e705c121SKalle Valo 				     sig);
593e705c121SKalle Valo 			iwl_mvm_bt_rssi_event(mvm, vif, RSSI_EVENT_LOW);
594e705c121SKalle Valo 		}
595e705c121SKalle Valo 	}
596e705c121SKalle Valo 
597e705c121SKalle Valo 	if (!(vif->driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI))
598e705c121SKalle Valo 		return;
599e705c121SKalle Valo 
600e705c121SKalle Valo 	/* CQM Notification */
601c1e458b9SIlan Peer 	last_event = link_info->bf_data.last_cqm_event;
602e705c121SKalle Valo 	if (thold && sig < thold && (last_event == 0 ||
603e705c121SKalle Valo 				     sig < last_event - hyst)) {
604c1e458b9SIlan Peer 		link_info->bf_data.last_cqm_event = sig;
605e705c121SKalle Valo 		IWL_DEBUG_RX(mvm, "cqm_iterator cqm low %d\n",
606e705c121SKalle Valo 			     sig);
607e705c121SKalle Valo 		ieee80211_cqm_rssi_notify(
608e705c121SKalle Valo 			vif,
609e705c121SKalle Valo 			NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
610769f07d8SAndrzej Zaborowski 			sig,
611e705c121SKalle Valo 			GFP_KERNEL);
612e705c121SKalle Valo 	} else if (sig > thold &&
613e705c121SKalle Valo 		   (last_event == 0 || sig > last_event + hyst)) {
614c1e458b9SIlan Peer 		link_info->bf_data.last_cqm_event = sig;
615e705c121SKalle Valo 		IWL_DEBUG_RX(mvm, "cqm_iterator cqm high %d\n",
616e705c121SKalle Valo 			     sig);
617e705c121SKalle Valo 		ieee80211_cqm_rssi_notify(
618e705c121SKalle Valo 			vif,
619e705c121SKalle Valo 			NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
620769f07d8SAndrzej Zaborowski 			sig,
621e705c121SKalle Valo 			GFP_KERNEL);
622e705c121SKalle Valo 	}
62330ce0390SMiri Korenblit 
62430ce0390SMiri Korenblit 	/* ESR recalculation */
62530ce0390SMiri Korenblit 	if (!vif->cfg.assoc || !ieee80211_vif_is_mld(vif))
62630ce0390SMiri Korenblit 		return;
62730ce0390SMiri Korenblit 
62830ce0390SMiri Korenblit 	exit_esr_thresh =
62930ce0390SMiri Korenblit 		iwl_mvm_get_esr_rssi_thresh(mvm,
63030ce0390SMiri Korenblit 					    &bss_conf->chanreq.oper,
63130ce0390SMiri Korenblit 					    true);
63230ce0390SMiri Korenblit 
63330ce0390SMiri Korenblit 	if (sig < exit_esr_thresh)
63430ce0390SMiri Korenblit 		iwl_mvm_exit_esr(mvm, vif, IWL_MVM_ESR_EXIT_LOW_RSSI,
63530ce0390SMiri Korenblit 				 iwl_mvm_get_other_link(vif,
63630ce0390SMiri Korenblit 							bss_conf->link_id));
637e705c121SKalle Valo }
638e705c121SKalle Valo 
6396324c173SMordechay Goodstein static void iwl_mvm_stat_iterator(void *_data, u8 *mac,
6406324c173SMordechay Goodstein 				  struct ieee80211_vif *vif)
6416324c173SMordechay Goodstein {
6426324c173SMordechay Goodstein 	struct iwl_mvm_stat_data *data = _data;
6436324c173SMordechay Goodstein 	int sig = -data->beacon_filter_average_energy;
6446324c173SMordechay Goodstein 	u16 id = le32_to_cpu(data->mac_id);
6456324c173SMordechay Goodstein 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
6466324c173SMordechay Goodstein 	u16 vif_id = mvmvif->id;
6476324c173SMordechay Goodstein 
6486324c173SMordechay Goodstein 	/* This doesn't need the MAC ID check since it's not taking the
6496324c173SMordechay Goodstein 	 * data copied into the "data" struct, but rather the data from
6506324c173SMordechay Goodstein 	 * the notification directly.
6516324c173SMordechay Goodstein 	 */
652650cadb7SGregory Greenman 	mvmvif->deflink.beacon_stats.num_beacons =
6536324c173SMordechay Goodstein 		le32_to_cpu(data->beacon_counter[vif_id]);
654650cadb7SGregory Greenman 	mvmvif->deflink.beacon_stats.avg_signal =
6556324c173SMordechay Goodstein 		-data->beacon_average_energy[vif_id];
6566324c173SMordechay Goodstein 
6576324c173SMordechay Goodstein 	if (mvmvif->id != id)
6586324c173SMordechay Goodstein 		return;
6596324c173SMordechay Goodstein 
6606324c173SMordechay Goodstein 	if (vif->type != NL80211_IFTYPE_STATION)
6616324c173SMordechay Goodstein 		return;
6626324c173SMordechay Goodstein 
6636324c173SMordechay Goodstein 	/* make sure that beacon statistics don't go backwards with TCM
6646324c173SMordechay Goodstein 	 * request to clear statistics
6656324c173SMordechay Goodstein 	 */
6666324c173SMordechay Goodstein 	if (le32_to_cpu(data->flags) & IWL_STATISTICS_REPLY_FLG_CLEAR)
667650cadb7SGregory Greenman 		mvmvif->deflink.beacon_stats.accu_num_beacons +=
668650cadb7SGregory Greenman 			mvmvif->deflink.beacon_stats.num_beacons;
6696324c173SMordechay Goodstein 
670c1e458b9SIlan Peer 	/* This is used in pre-MLO API so use deflink */
671*28e02bc9SJohannes Berg 	iwl_mvm_update_link_sig(vif, sig, &mvmvif->deflink, &vif->bss_conf);
6726324c173SMordechay Goodstein }
6736324c173SMordechay Goodstein 
6746324c173SMordechay Goodstein static void iwl_mvm_stat_iterator_all_macs(void *_data, u8 *mac,
6756324c173SMordechay Goodstein 					   struct ieee80211_vif *vif)
6766324c173SMordechay Goodstein {
6776324c173SMordechay Goodstein 	struct iwl_mvm_stat_data_all_macs *data = _data;
678b6e3d1baSAnjaneyulu 	struct iwl_stats_ntfy_per_mac *mac_stats;
6796324c173SMordechay Goodstein 	int sig;
6806324c173SMordechay Goodstein 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
6816324c173SMordechay Goodstein 	u16 vif_id = mvmvif->id;
6826324c173SMordechay Goodstein 
683f1cbb0a8SDan Carpenter 	if (WARN_ONCE(vif_id >= MAC_INDEX_AUX, "invalid vif id: %d", vif_id))
6846324c173SMordechay Goodstein 		return;
6856324c173SMordechay Goodstein 
6866324c173SMordechay Goodstein 	if (vif->type != NL80211_IFTYPE_STATION)
6876324c173SMordechay Goodstein 		return;
6886324c173SMordechay Goodstein 
689b6e3d1baSAnjaneyulu 	mac_stats = &data->per_mac[vif_id];
6906324c173SMordechay Goodstein 
691650cadb7SGregory Greenman 	mvmvif->deflink.beacon_stats.num_beacons =
6926324c173SMordechay Goodstein 		le32_to_cpu(mac_stats->beacon_counter);
693650cadb7SGregory Greenman 	mvmvif->deflink.beacon_stats.avg_signal =
6946324c173SMordechay Goodstein 		-le32_to_cpu(mac_stats->beacon_average_energy);
6956324c173SMordechay Goodstein 
6966324c173SMordechay Goodstein 	/* make sure that beacon statistics don't go backwards with TCM
6976324c173SMordechay Goodstein 	 * request to clear statistics
6986324c173SMordechay Goodstein 	 */
6996324c173SMordechay Goodstein 	if (le32_to_cpu(data->flags) & IWL_STATISTICS_REPLY_FLG_CLEAR)
700650cadb7SGregory Greenman 		mvmvif->deflink.beacon_stats.accu_num_beacons +=
701650cadb7SGregory Greenman 			mvmvif->deflink.beacon_stats.num_beacons;
7026324c173SMordechay Goodstein 
7036324c173SMordechay Goodstein 	sig = -le32_to_cpu(mac_stats->beacon_filter_average_energy);
704c1e458b9SIlan Peer 
705c1e458b9SIlan Peer 	/* This is used in pre-MLO API so use deflink */
706*28e02bc9SJohannes Berg 	iwl_mvm_update_link_sig(vif, sig, &mvmvif->deflink, &vif->bss_conf);
7076324c173SMordechay Goodstein }
7086324c173SMordechay Goodstein 
709e705c121SKalle Valo static inline void
710e705c121SKalle Valo iwl_mvm_rx_stats_check_trigger(struct iwl_mvm *mvm, struct iwl_rx_packet *pkt)
711e705c121SKalle Valo {
712e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_tlv *trig;
713e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_stats *trig_stats;
714e705c121SKalle Valo 	u32 trig_offset, trig_thold;
715e705c121SKalle Valo 
7166c042d75SSara Sharon 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, NULL, FW_DBG_TRIGGER_STATS);
7176c042d75SSara Sharon 	if (!trig)
718e705c121SKalle Valo 		return;
719e705c121SKalle Valo 
720e705c121SKalle Valo 	trig_stats = (void *)trig->data;
721e705c121SKalle Valo 
722e705c121SKalle Valo 	trig_offset = le32_to_cpu(trig_stats->stop_offset);
723e705c121SKalle Valo 	trig_thold = le32_to_cpu(trig_stats->stop_threshold);
724e705c121SKalle Valo 
725e705c121SKalle Valo 	if (WARN_ON_ONCE(trig_offset >= iwl_rx_packet_payload_len(pkt)))
726e705c121SKalle Valo 		return;
727e705c121SKalle Valo 
728e705c121SKalle Valo 	if (le32_to_cpup((__le32 *) (pkt->data + trig_offset)) < trig_thold)
729e705c121SKalle Valo 		return;
730e705c121SKalle Valo 
7317174beb6SJohannes Berg 	iwl_fw_dbg_collect_trig(&mvm->fwrt, trig, NULL);
732e705c121SKalle Valo }
733e705c121SKalle Valo 
734c6bae216SMordechay Goodstein static void iwl_mvm_stats_energy_iter(void *_data,
735c6bae216SMordechay Goodstein 				      struct ieee80211_sta *sta)
736853f4954SMordechay Goodstein {
737c6bae216SMordechay Goodstein 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
738c6bae216SMordechay Goodstein 	u8 *energy = _data;
739c8ee33e1SGregory Greenman 	u32 sta_id = mvmsta->deflink.sta_id;
740853f4954SMordechay Goodstein 
741c6bae216SMordechay Goodstein 	if (WARN_ONCE(sta_id >= IWL_MVM_STATION_COUNT_MAX, "sta_id %d >= %d",
742c6bae216SMordechay Goodstein 		      sta_id, IWL_MVM_STATION_COUNT_MAX))
743853f4954SMordechay Goodstein 		return;
744853f4954SMordechay Goodstein 
745c6bae216SMordechay Goodstein 	if (energy[sta_id])
746c8ee33e1SGregory Greenman 		mvmsta->deflink.avg_energy = energy[sta_id];
747853f4954SMordechay Goodstein 
748853f4954SMordechay Goodstein }
749853f4954SMordechay Goodstein 
750853f4954SMordechay Goodstein static void
751853f4954SMordechay Goodstein iwl_mvm_update_tcm_from_stats(struct iwl_mvm *mvm, __le32 *air_time_le,
752853f4954SMordechay Goodstein 			      __le32 *rx_bytes_le)
753853f4954SMordechay Goodstein {
754853f4954SMordechay Goodstein 	int i;
755853f4954SMordechay Goodstein 
756853f4954SMordechay Goodstein 	spin_lock(&mvm->tcm.lock);
757853f4954SMordechay Goodstein 	for (i = 0; i < NUM_MAC_INDEX_DRIVER; i++) {
758853f4954SMordechay Goodstein 		struct iwl_mvm_tcm_mac *mdata = &mvm->tcm.data[i];
759853f4954SMordechay Goodstein 		u32 rx_bytes = le32_to_cpu(rx_bytes_le[i]);
760853f4954SMordechay Goodstein 		u32 airtime = le32_to_cpu(air_time_le[i]);
761853f4954SMordechay Goodstein 
762853f4954SMordechay Goodstein 		mdata->rx.airtime += airtime;
763853f4954SMordechay Goodstein 		mdata->uapsd_nonagg_detect.rx_bytes += rx_bytes;
764853f4954SMordechay Goodstein 		if (airtime) {
765853f4954SMordechay Goodstein 			/* re-init every time to store rate from FW */
766853f4954SMordechay Goodstein 			ewma_rate_init(&mdata->uapsd_nonagg_detect.rate);
767853f4954SMordechay Goodstein 			ewma_rate_add(&mdata->uapsd_nonagg_detect.rate,
768853f4954SMordechay Goodstein 				      rx_bytes * 8 / airtime);
769853f4954SMordechay Goodstein 		}
770853f4954SMordechay Goodstein 	}
771853f4954SMordechay Goodstein 	spin_unlock(&mvm->tcm.lock);
772853f4954SMordechay Goodstein }
773853f4954SMordechay Goodstein 
774740dfeccSMiri Korenblit static void iwl_mvm_handle_per_phy_stats(struct iwl_mvm *mvm,
775740dfeccSMiri Korenblit 					 struct iwl_stats_ntfy_per_phy *per_phy)
776740dfeccSMiri Korenblit {
777740dfeccSMiri Korenblit 	int i;
778740dfeccSMiri Korenblit 
779740dfeccSMiri Korenblit 	for (i = 0; i < NUM_PHY_CTX; i++) {
780740dfeccSMiri Korenblit 		if (!mvm->phy_ctxts[i].ref)
781740dfeccSMiri Korenblit 			continue;
782740dfeccSMiri Korenblit 		mvm->phy_ctxts[i].channel_load_by_us =
783740dfeccSMiri Korenblit 			le32_to_cpu(per_phy[i].channel_load_by_us);
784740dfeccSMiri Korenblit 	}
785740dfeccSMiri Korenblit }
786740dfeccSMiri Korenblit 
787853f4954SMordechay Goodstein static void
7886324c173SMordechay Goodstein iwl_mvm_stats_ver_15(struct iwl_mvm *mvm,
7896324c173SMordechay Goodstein 		     struct iwl_statistics_operational_ntfy *stats)
7906324c173SMordechay Goodstein {
7916324c173SMordechay Goodstein 	struct iwl_mvm_stat_data_all_macs data = {
7926324c173SMordechay Goodstein 		.mvm = mvm,
7936324c173SMordechay Goodstein 		.flags = stats->flags,
794b6e3d1baSAnjaneyulu 		.per_mac = stats->per_mac,
7956324c173SMordechay Goodstein 	};
7966324c173SMordechay Goodstein 
7976324c173SMordechay Goodstein 	ieee80211_iterate_active_interfaces(mvm->hw,
7986324c173SMordechay Goodstein 					    IEEE80211_IFACE_ITER_NORMAL,
7996324c173SMordechay Goodstein 					    iwl_mvm_stat_iterator_all_macs,
8006324c173SMordechay Goodstein 					    &data);
801740dfeccSMiri Korenblit 	iwl_mvm_handle_per_phy_stats(mvm, stats->per_phy);
8026324c173SMordechay Goodstein }
8036324c173SMordechay Goodstein 
8046324c173SMordechay Goodstein static void
8056324c173SMordechay Goodstein iwl_mvm_stats_ver_14(struct iwl_mvm *mvm,
8066324c173SMordechay Goodstein 		     struct iwl_statistics_operational_ntfy_ver_14 *stats)
807853f4954SMordechay Goodstein {
808853f4954SMordechay Goodstein 	struct iwl_mvm_stat_data data = {
809853f4954SMordechay Goodstein 		.mvm = mvm,
810853f4954SMordechay Goodstein 	};
8116324c173SMordechay Goodstein 
812853f4954SMordechay Goodstein 	u8 beacon_average_energy[MAC_INDEX_AUX];
813853f4954SMordechay Goodstein 	__le32 flags;
814853f4954SMordechay Goodstein 	int i;
815853f4954SMordechay Goodstein 
816853f4954SMordechay Goodstein 	flags = stats->flags;
817853f4954SMordechay Goodstein 
818853f4954SMordechay Goodstein 	data.mac_id = stats->mac_id;
819853f4954SMordechay Goodstein 	data.beacon_filter_average_energy =
820853f4954SMordechay Goodstein 		le32_to_cpu(stats->beacon_filter_average_energy);
821853f4954SMordechay Goodstein 	data.flags = flags;
822853f4954SMordechay Goodstein 	data.beacon_counter = stats->beacon_counter;
8236324c173SMordechay Goodstein 
824853f4954SMordechay Goodstein 	for (i = 0; i < ARRAY_SIZE(beacon_average_energy); i++)
825853f4954SMordechay Goodstein 		beacon_average_energy[i] =
826853f4954SMordechay Goodstein 			le32_to_cpu(stats->beacon_average_energy[i]);
827853f4954SMordechay Goodstein 
828853f4954SMordechay Goodstein 	data.beacon_average_energy = beacon_average_energy;
829853f4954SMordechay Goodstein 
830853f4954SMordechay Goodstein 	ieee80211_iterate_active_interfaces(mvm->hw,
831853f4954SMordechay Goodstein 					    IEEE80211_IFACE_ITER_NORMAL,
832853f4954SMordechay Goodstein 					    iwl_mvm_stat_iterator,
833853f4954SMordechay Goodstein 					    &data);
8346324c173SMordechay Goodstein }
8356324c173SMordechay Goodstein 
8366324c173SMordechay Goodstein static bool iwl_mvm_verify_stats_len(struct iwl_mvm *mvm,
8376324c173SMordechay Goodstein 				     struct iwl_rx_packet *pkt,
8386324c173SMordechay Goodstein 				     u32 expected_size)
8396324c173SMordechay Goodstein {
8406324c173SMordechay Goodstein 	struct iwl_statistics_ntfy_hdr *hdr;
8416324c173SMordechay Goodstein 
8426324c173SMordechay Goodstein 	if (WARN_ONCE(iwl_rx_packet_payload_len(pkt) < expected_size,
8436324c173SMordechay Goodstein 		      "received invalid statistics size (%d)!, expected_size: %d\n",
8446324c173SMordechay Goodstein 		      iwl_rx_packet_payload_len(pkt), expected_size))
8456324c173SMordechay Goodstein 		return false;
8466324c173SMordechay Goodstein 
8476324c173SMordechay Goodstein 	hdr = (void *)&pkt->data;
8486324c173SMordechay Goodstein 
8496324c173SMordechay Goodstein 	if (WARN_ONCE((hdr->type & IWL_STATISTICS_TYPE_MSK) != FW_STATISTICS_OPERATIONAL ||
8506324c173SMordechay Goodstein 		      hdr->version !=
8516324c173SMordechay Goodstein 		      iwl_fw_lookup_notif_ver(mvm->fw, LEGACY_GROUP, STATISTICS_NOTIFICATION, 0),
8526324c173SMordechay Goodstein 		      "received unsupported hdr type %d, version %d\n",
8536324c173SMordechay Goodstein 		      hdr->type, hdr->version))
8546324c173SMordechay Goodstein 		return false;
8556324c173SMordechay Goodstein 
8566324c173SMordechay Goodstein 	if (WARN_ONCE(le16_to_cpu(hdr->size) != expected_size,
8576324c173SMordechay Goodstein 		      "received invalid statistics size in header (%d)!, expected_size: %d\n",
8586324c173SMordechay Goodstein 		      le16_to_cpu(hdr->size), expected_size))
8596324c173SMordechay Goodstein 		return false;
8606324c173SMordechay Goodstein 
8616324c173SMordechay Goodstein 	return true;
8626324c173SMordechay Goodstein }
8636324c173SMordechay Goodstein 
8646324c173SMordechay Goodstein static void
865b6e3d1baSAnjaneyulu iwl_mvm_stat_iterator_all_links(struct iwl_mvm *mvm,
866b6e3d1baSAnjaneyulu 				struct iwl_stats_ntfy_per_link *per_link)
867b6e3d1baSAnjaneyulu {
868b6e3d1baSAnjaneyulu 	u32 air_time[MAC_INDEX_AUX] = {};
869b6e3d1baSAnjaneyulu 	u32 rx_bytes[MAC_INDEX_AUX] = {};
870b6e3d1baSAnjaneyulu 	int fw_link_id;
871b6e3d1baSAnjaneyulu 
872b6e3d1baSAnjaneyulu 	for (fw_link_id = 0; fw_link_id < ARRAY_SIZE(mvm->link_id_to_link_conf);
873b6e3d1baSAnjaneyulu 	     fw_link_id++) {
874b6e3d1baSAnjaneyulu 		struct iwl_stats_ntfy_per_link *link_stats;
875b6e3d1baSAnjaneyulu 		struct ieee80211_bss_conf *bss_conf;
876b6e3d1baSAnjaneyulu 		struct iwl_mvm_vif *mvmvif;
87710159a45SMiri Korenblit 		struct iwl_mvm_vif_link_info *link_info;
878b6e3d1baSAnjaneyulu 		int link_id;
879b6e3d1baSAnjaneyulu 		int sig;
880b6e3d1baSAnjaneyulu 
881b6e3d1baSAnjaneyulu 		bss_conf = iwl_mvm_rcu_fw_link_id_to_link_conf(mvm, fw_link_id,
882b6e3d1baSAnjaneyulu 							       false);
883b6e3d1baSAnjaneyulu 		if (!bss_conf)
884b6e3d1baSAnjaneyulu 			continue;
885b6e3d1baSAnjaneyulu 
886b6e3d1baSAnjaneyulu 		if (bss_conf->vif->type != NL80211_IFTYPE_STATION)
887b6e3d1baSAnjaneyulu 			continue;
888b6e3d1baSAnjaneyulu 
889b6e3d1baSAnjaneyulu 		link_id = bss_conf->link_id;
890b6e3d1baSAnjaneyulu 		if (link_id >= ARRAY_SIZE(mvmvif->link))
891b6e3d1baSAnjaneyulu 			continue;
892b6e3d1baSAnjaneyulu 
893b6e3d1baSAnjaneyulu 		mvmvif = iwl_mvm_vif_from_mac80211(bss_conf->vif);
89410159a45SMiri Korenblit 		link_info = mvmvif->link[link_id];
89510159a45SMiri Korenblit 		if (!link_info)
896b6e3d1baSAnjaneyulu 			continue;
897b6e3d1baSAnjaneyulu 
898b6e3d1baSAnjaneyulu 		link_stats = &per_link[fw_link_id];
899b6e3d1baSAnjaneyulu 
90010159a45SMiri Korenblit 		link_info->beacon_stats.num_beacons =
901b6e3d1baSAnjaneyulu 			le32_to_cpu(link_stats->beacon_counter);
902b6e3d1baSAnjaneyulu 
903b6e3d1baSAnjaneyulu 		/* we basically just use the u8 to store 8 bits and then treat
904b6e3d1baSAnjaneyulu 		 * it as a s8 whenever we take it out to a different type.
905b6e3d1baSAnjaneyulu 		 */
90610159a45SMiri Korenblit 		link_info->beacon_stats.avg_signal =
907b6e3d1baSAnjaneyulu 			-le32_to_cpu(link_stats->beacon_average_energy);
908b6e3d1baSAnjaneyulu 
90910159a45SMiri Korenblit 		if (link_info->phy_ctxt &&
91010159a45SMiri Korenblit 		    link_info->phy_ctxt->channel->band == NL80211_BAND_2GHZ)
9119c692112SMiri Korenblit 			iwl_mvm_bt_coex_update_link_esr(mvm, bss_conf->vif,
91210159a45SMiri Korenblit 							link_id);
91310159a45SMiri Korenblit 
914b6e3d1baSAnjaneyulu 		/* make sure that beacon statistics don't go backwards with TCM
915b6e3d1baSAnjaneyulu 		 * request to clear statistics
916b6e3d1baSAnjaneyulu 		 */
917b6e3d1baSAnjaneyulu 		if (mvm->statistics_clear)
918b6e3d1baSAnjaneyulu 			mvmvif->link[link_id]->beacon_stats.accu_num_beacons +=
919b6e3d1baSAnjaneyulu 				mvmvif->link[link_id]->beacon_stats.num_beacons;
920b6e3d1baSAnjaneyulu 
921b6e3d1baSAnjaneyulu 		sig = -le32_to_cpu(link_stats->beacon_filter_average_energy);
922*28e02bc9SJohannes Berg 		iwl_mvm_update_link_sig(bss_conf->vif, sig, link_info,
923*28e02bc9SJohannes Berg 					bss_conf);
924b6e3d1baSAnjaneyulu 
925b6e3d1baSAnjaneyulu 		if (WARN_ONCE(mvmvif->id >= MAC_INDEX_AUX,
926b6e3d1baSAnjaneyulu 			      "invalid mvmvif id: %d", mvmvif->id))
927b6e3d1baSAnjaneyulu 			continue;
928b6e3d1baSAnjaneyulu 
929b6e3d1baSAnjaneyulu 		air_time[mvmvif->id] +=
930b6e3d1baSAnjaneyulu 			le32_to_cpu(per_link[fw_link_id].air_time);
931b6e3d1baSAnjaneyulu 		rx_bytes[mvmvif->id] +=
932b6e3d1baSAnjaneyulu 			le32_to_cpu(per_link[fw_link_id].rx_bytes);
933b6e3d1baSAnjaneyulu 	}
934b6e3d1baSAnjaneyulu 
935b6e3d1baSAnjaneyulu 	/* Don't update in case the statistics are not cleared, since
936b6e3d1baSAnjaneyulu 	 * we will end up counting twice the same airtime, once in TCM
937b6e3d1baSAnjaneyulu 	 * request and once in statistics notification.
938b6e3d1baSAnjaneyulu 	 */
939b6e3d1baSAnjaneyulu 	if (mvm->statistics_clear) {
940b6e3d1baSAnjaneyulu 		__le32 air_time_le[MAC_INDEX_AUX];
941b6e3d1baSAnjaneyulu 		__le32 rx_bytes_le[MAC_INDEX_AUX];
942b6e3d1baSAnjaneyulu 		int vif_id;
943b6e3d1baSAnjaneyulu 
944b6e3d1baSAnjaneyulu 		for (vif_id = 0; vif_id < ARRAY_SIZE(air_time_le); vif_id++) {
945b6e3d1baSAnjaneyulu 			air_time_le[vif_id] = cpu_to_le32(air_time[vif_id]);
946b6e3d1baSAnjaneyulu 			rx_bytes_le[vif_id] = cpu_to_le32(rx_bytes[vif_id]);
947b6e3d1baSAnjaneyulu 		}
948b6e3d1baSAnjaneyulu 
949b6e3d1baSAnjaneyulu 		iwl_mvm_update_tcm_from_stats(mvm, air_time_le, rx_bytes_le);
950b6e3d1baSAnjaneyulu 	}
951b6e3d1baSAnjaneyulu }
952b6e3d1baSAnjaneyulu 
953df966c93SMiri Korenblit #define SEC_LINK_MIN_PERC 10
954df966c93SMiri Korenblit #define SEC_LINK_MIN_TX 3000
955df966c93SMiri Korenblit #define SEC_LINK_MIN_RX 400
956df966c93SMiri Korenblit 
957ec0d43d2SMiri Korenblit static void iwl_mvm_update_esr_mode_tpt(struct iwl_mvm *mvm)
958ec0d43d2SMiri Korenblit {
959ec0d43d2SMiri Korenblit 	struct ieee80211_vif *bss_vif = iwl_mvm_get_bss_vif(mvm);
960ec0d43d2SMiri Korenblit 	struct iwl_mvm_vif *mvmvif;
961ec0d43d2SMiri Korenblit 	struct iwl_mvm_sta *mvmsta;
962ec0d43d2SMiri Korenblit 	unsigned long total_tx = 0, total_rx = 0;
963df966c93SMiri Korenblit 	unsigned long sec_link_tx = 0, sec_link_rx = 0;
964df966c93SMiri Korenblit 	u8 sec_link_tx_perc, sec_link_rx_perc;
965df966c93SMiri Korenblit 	u8 sec_link;
966ec0d43d2SMiri Korenblit 
967ec0d43d2SMiri Korenblit 	lockdep_assert_held(&mvm->mutex);
968ec0d43d2SMiri Korenblit 
969ec0d43d2SMiri Korenblit 	if (!bss_vif)
970ec0d43d2SMiri Korenblit 		return;
971ec0d43d2SMiri Korenblit 
972ec0d43d2SMiri Korenblit 	mvmvif = iwl_mvm_vif_from_mac80211(bss_vif);
973ec0d43d2SMiri Korenblit 
974ec0d43d2SMiri Korenblit 	if (!mvmvif->esr_active || !mvmvif->ap_sta)
975ec0d43d2SMiri Korenblit 		return;
976ec0d43d2SMiri Korenblit 
977ec0d43d2SMiri Korenblit 	mvmsta = iwl_mvm_sta_from_mac80211(mvmvif->ap_sta);
978ec0d43d2SMiri Korenblit 	/* We only count for the AP sta in a MLO connection */
979ec0d43d2SMiri Korenblit 	if (!mvmsta->mpdu_counters)
980ec0d43d2SMiri Korenblit 		return;
981ec0d43d2SMiri Korenblit 
982df966c93SMiri Korenblit 	/* Get the FW ID of the secondary link */
983df966c93SMiri Korenblit 	sec_link = iwl_mvm_get_other_link(bss_vif,
984df966c93SMiri Korenblit 					  iwl_mvm_get_primary_link(bss_vif));
985df966c93SMiri Korenblit 	if (WARN_ON(!mvmvif->link[sec_link]))
986df966c93SMiri Korenblit 		return;
987df966c93SMiri Korenblit 	sec_link = mvmvif->link[sec_link]->fw_link_id;
988df966c93SMiri Korenblit 
989ec0d43d2SMiri Korenblit 	/* Sum up RX and TX MPDUs from the different queues/links */
990ec0d43d2SMiri Korenblit 	for (int q = 0; q < mvm->trans->num_rx_queues; q++) {
991ec0d43d2SMiri Korenblit 		spin_lock_bh(&mvmsta->mpdu_counters[q].lock);
992ec0d43d2SMiri Korenblit 
993ec0d43d2SMiri Korenblit 		/* The link IDs that doesn't exist will contain 0 */
994ec0d43d2SMiri Korenblit 		for (int link = 0; link < IWL_MVM_FW_MAX_LINK_ID; link++) {
995ec0d43d2SMiri Korenblit 			total_tx += mvmsta->mpdu_counters[q].per_link[link].tx;
996ec0d43d2SMiri Korenblit 			total_rx += mvmsta->mpdu_counters[q].per_link[link].rx;
997ec0d43d2SMiri Korenblit 		}
998df966c93SMiri Korenblit 
999df966c93SMiri Korenblit 		sec_link_tx += mvmsta->mpdu_counters[q].per_link[sec_link].tx;
1000df966c93SMiri Korenblit 		sec_link_rx += mvmsta->mpdu_counters[q].per_link[sec_link].rx;
1001df966c93SMiri Korenblit 
1002ec0d43d2SMiri Korenblit 		/*
1003ec0d43d2SMiri Korenblit 		 * In EMLSR we have statistics every 5 seconds, so we can reset
1004ec0d43d2SMiri Korenblit 		 * the counters upon every statistics notification.
1005ec0d43d2SMiri Korenblit 		 */
1006ec0d43d2SMiri Korenblit 		memset(mvmsta->mpdu_counters[q].per_link, 0,
1007ec0d43d2SMiri Korenblit 		       sizeof(mvmsta->mpdu_counters[q].per_link));
1008ec0d43d2SMiri Korenblit 
1009ec0d43d2SMiri Korenblit 		spin_unlock_bh(&mvmsta->mpdu_counters[q].lock);
1010ec0d43d2SMiri Korenblit 	}
1011ec0d43d2SMiri Korenblit 
1012ec0d43d2SMiri Korenblit 	/* If we don't have enough MPDUs - exit EMLSR */
1013ec0d43d2SMiri Korenblit 	if (total_tx < IWL_MVM_ENTER_ESR_TPT_THRESH &&
1014df966c93SMiri Korenblit 	    total_rx < IWL_MVM_ENTER_ESR_TPT_THRESH) {
1015ec0d43d2SMiri Korenblit 		iwl_mvm_block_esr(mvm, bss_vif, IWL_MVM_ESR_BLOCKED_TPT,
1016ec0d43d2SMiri Korenblit 				  iwl_mvm_get_primary_link(bss_vif));
1017df966c93SMiri Korenblit 		return;
1018df966c93SMiri Korenblit 	}
1019df966c93SMiri Korenblit 
1020df966c93SMiri Korenblit 	/* Calculate the percentage of the secondary link TX/RX */
1021df966c93SMiri Korenblit 	sec_link_tx_perc = total_tx ? sec_link_tx * 100 / total_tx : 0;
1022df966c93SMiri Korenblit 	sec_link_rx_perc = total_rx ? sec_link_rx * 100 / total_rx : 0;
1023df966c93SMiri Korenblit 
1024df966c93SMiri Korenblit 	/*
1025df966c93SMiri Korenblit 	 * The TX/RX percentage is checked only if it exceeds the required
1026df966c93SMiri Korenblit 	 * minimum. In addition, RX is checked only if the TX check failed.
1027df966c93SMiri Korenblit 	 */
1028df966c93SMiri Korenblit 	if ((total_tx > SEC_LINK_MIN_TX &&
1029df966c93SMiri Korenblit 	     sec_link_tx_perc < SEC_LINK_MIN_PERC) ||
1030df966c93SMiri Korenblit 	    (total_rx > SEC_LINK_MIN_RX &&
1031df966c93SMiri Korenblit 	     sec_link_rx_perc < SEC_LINK_MIN_PERC))
1032df966c93SMiri Korenblit 		iwl_mvm_exit_esr(mvm, bss_vif, IWL_MVM_ESR_EXIT_LINK_USAGE,
1033df966c93SMiri Korenblit 				 iwl_mvm_get_primary_link(bss_vif));
1034ec0d43d2SMiri Korenblit }
1035ec0d43d2SMiri Korenblit 
1036b6e3d1baSAnjaneyulu void iwl_mvm_handle_rx_system_oper_stats(struct iwl_mvm *mvm,
1037b6e3d1baSAnjaneyulu 					 struct iwl_rx_cmd_buffer *rxb)
1038b6e3d1baSAnjaneyulu {
1039b6e3d1baSAnjaneyulu 	u8 average_energy[IWL_MVM_STATION_COUNT_MAX];
1040b6e3d1baSAnjaneyulu 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1041b6e3d1baSAnjaneyulu 	struct iwl_system_statistics_notif_oper *stats;
1042b6e3d1baSAnjaneyulu 	int i;
1043b6e3d1baSAnjaneyulu 	u32 notif_ver = iwl_fw_lookup_notif_ver(mvm->fw, STATISTICS_GROUP,
1044b6e3d1baSAnjaneyulu 						STATISTICS_OPER_NOTIF, 0);
1045b6e3d1baSAnjaneyulu 
1046b6e3d1baSAnjaneyulu 	if (notif_ver != 3) {
1047b6e3d1baSAnjaneyulu 		IWL_FW_CHECK_FAILED(mvm,
1048b6e3d1baSAnjaneyulu 				    "Oper stats notif ver %d is not supported\n",
1049b6e3d1baSAnjaneyulu 				    notif_ver);
1050b6e3d1baSAnjaneyulu 		return;
1051b6e3d1baSAnjaneyulu 	}
1052b6e3d1baSAnjaneyulu 
1053b6e3d1baSAnjaneyulu 	stats = (void *)&pkt->data;
1054b6e3d1baSAnjaneyulu 	iwl_mvm_stat_iterator_all_links(mvm, stats->per_link);
1055b6e3d1baSAnjaneyulu 
1056b6e3d1baSAnjaneyulu 	for (i = 0; i < ARRAY_SIZE(average_energy); i++)
1057b6e3d1baSAnjaneyulu 		average_energy[i] =
1058b6e3d1baSAnjaneyulu 			le32_to_cpu(stats->per_sta[i].average_energy);
1059b6e3d1baSAnjaneyulu 
1060b6e3d1baSAnjaneyulu 	ieee80211_iterate_stations_atomic(mvm->hw, iwl_mvm_stats_energy_iter,
1061b6e3d1baSAnjaneyulu 					  average_energy);
1062740dfeccSMiri Korenblit 	iwl_mvm_handle_per_phy_stats(mvm, stats->per_phy);
1063ec0d43d2SMiri Korenblit 
1064ec0d43d2SMiri Korenblit 	iwl_mvm_update_esr_mode_tpt(mvm);
1065b6e3d1baSAnjaneyulu }
1066b6e3d1baSAnjaneyulu 
1067b6e3d1baSAnjaneyulu void iwl_mvm_handle_rx_system_oper_part1_stats(struct iwl_mvm *mvm,
1068b6e3d1baSAnjaneyulu 					       struct iwl_rx_cmd_buffer *rxb)
1069b6e3d1baSAnjaneyulu {
1070b6e3d1baSAnjaneyulu 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1071b6e3d1baSAnjaneyulu 	struct iwl_system_statistics_part1_notif_oper *part1_stats;
1072b6e3d1baSAnjaneyulu 	int i;
1073b6e3d1baSAnjaneyulu 	u32 notif_ver = iwl_fw_lookup_notif_ver(mvm->fw, STATISTICS_GROUP,
1074b6e3d1baSAnjaneyulu 						STATISTICS_OPER_PART1_NOTIF, 0);
1075b6e3d1baSAnjaneyulu 
1076b6e3d1baSAnjaneyulu 	if (notif_ver != 4) {
1077b6e3d1baSAnjaneyulu 		IWL_FW_CHECK_FAILED(mvm,
1078b6e3d1baSAnjaneyulu 				    "Part1 stats notif ver %d is not supported\n",
1079b6e3d1baSAnjaneyulu 				    notif_ver);
1080b6e3d1baSAnjaneyulu 		return;
1081b6e3d1baSAnjaneyulu 	}
1082b6e3d1baSAnjaneyulu 
1083b6e3d1baSAnjaneyulu 	part1_stats = (void *)&pkt->data;
1084b6e3d1baSAnjaneyulu 	mvm->radio_stats.rx_time = 0;
1085b6e3d1baSAnjaneyulu 	mvm->radio_stats.tx_time = 0;
1086b6e3d1baSAnjaneyulu 	for (i = 0; i < ARRAY_SIZE(part1_stats->per_link); i++) {
1087b6e3d1baSAnjaneyulu 		mvm->radio_stats.rx_time +=
1088b6e3d1baSAnjaneyulu 			le64_to_cpu(part1_stats->per_link[i].rx_time);
1089b6e3d1baSAnjaneyulu 		mvm->radio_stats.tx_time +=
1090b6e3d1baSAnjaneyulu 			le64_to_cpu(part1_stats->per_link[i].tx_time);
1091b6e3d1baSAnjaneyulu 	}
1092b6e3d1baSAnjaneyulu }
1093b6e3d1baSAnjaneyulu 
1094b6e3d1baSAnjaneyulu static void
10956324c173SMordechay Goodstein iwl_mvm_handle_rx_statistics_tlv(struct iwl_mvm *mvm,
10966324c173SMordechay Goodstein 				 struct iwl_rx_packet *pkt)
10976324c173SMordechay Goodstein {
10986324c173SMordechay Goodstein 	u8 average_energy[IWL_MVM_STATION_COUNT_MAX];
10996324c173SMordechay Goodstein 	__le32 air_time[MAC_INDEX_AUX];
11006324c173SMordechay Goodstein 	__le32 rx_bytes[MAC_INDEX_AUX];
11016324c173SMordechay Goodstein 	__le32 flags = 0;
11026324c173SMordechay Goodstein 	int i;
11036324c173SMordechay Goodstein 	u32 notif_ver = iwl_fw_lookup_notif_ver(mvm->fw, LEGACY_GROUP,
11046324c173SMordechay Goodstein 					      STATISTICS_NOTIFICATION, 0);
11056324c173SMordechay Goodstein 
11066324c173SMordechay Goodstein 	if (WARN_ONCE(notif_ver > 15,
11076324c173SMordechay Goodstein 		      "invalid statistics version id: %d\n", notif_ver))
11086324c173SMordechay Goodstein 		return;
11096324c173SMordechay Goodstein 
11106324c173SMordechay Goodstein 	if (notif_ver == 14) {
11116324c173SMordechay Goodstein 		struct iwl_statistics_operational_ntfy_ver_14 *stats =
11126324c173SMordechay Goodstein 			(void *)pkt->data;
11136324c173SMordechay Goodstein 
11146324c173SMordechay Goodstein 		if (!iwl_mvm_verify_stats_len(mvm, pkt, sizeof(*stats)))
11156324c173SMordechay Goodstein 			return;
11166324c173SMordechay Goodstein 
11176324c173SMordechay Goodstein 		iwl_mvm_stats_ver_14(mvm, stats);
11186324c173SMordechay Goodstein 
11196324c173SMordechay Goodstein 		flags = stats->flags;
11206324c173SMordechay Goodstein 		mvm->radio_stats.rx_time = le64_to_cpu(stats->rx_time);
11216324c173SMordechay Goodstein 		mvm->radio_stats.tx_time = le64_to_cpu(stats->tx_time);
11226324c173SMordechay Goodstein 		mvm->radio_stats.on_time_rf = le64_to_cpu(stats->on_time_rf);
11236324c173SMordechay Goodstein 		mvm->radio_stats.on_time_scan =
11246324c173SMordechay Goodstein 			le64_to_cpu(stats->on_time_scan);
1125853f4954SMordechay Goodstein 
1126853f4954SMordechay Goodstein 		for (i = 0; i < ARRAY_SIZE(average_energy); i++)
1127853f4954SMordechay Goodstein 			average_energy[i] = le32_to_cpu(stats->average_energy[i]);
11286324c173SMordechay Goodstein 
11296324c173SMordechay Goodstein 		for (i = 0; i < ARRAY_SIZE(air_time); i++) {
11306324c173SMordechay Goodstein 			air_time[i] = stats->air_time[i];
11316324c173SMordechay Goodstein 			rx_bytes[i] = stats->rx_bytes[i];
11326324c173SMordechay Goodstein 		}
11336324c173SMordechay Goodstein 	}
11346324c173SMordechay Goodstein 
11356324c173SMordechay Goodstein 	if (notif_ver == 15) {
11366324c173SMordechay Goodstein 		struct iwl_statistics_operational_ntfy *stats =
11376324c173SMordechay Goodstein 			(void *)pkt->data;
11386324c173SMordechay Goodstein 
11396324c173SMordechay Goodstein 		if (!iwl_mvm_verify_stats_len(mvm, pkt, sizeof(*stats)))
11406324c173SMordechay Goodstein 			return;
11416324c173SMordechay Goodstein 
11426324c173SMordechay Goodstein 		iwl_mvm_stats_ver_15(mvm, stats);
11436324c173SMordechay Goodstein 
11446324c173SMordechay Goodstein 		flags = stats->flags;
11456324c173SMordechay Goodstein 		mvm->radio_stats.rx_time = le64_to_cpu(stats->rx_time);
11466324c173SMordechay Goodstein 		mvm->radio_stats.tx_time = le64_to_cpu(stats->tx_time);
11476324c173SMordechay Goodstein 		mvm->radio_stats.on_time_rf = le64_to_cpu(stats->on_time_rf);
11486324c173SMordechay Goodstein 		mvm->radio_stats.on_time_scan =
11496324c173SMordechay Goodstein 			le64_to_cpu(stats->on_time_scan);
11506324c173SMordechay Goodstein 
11516324c173SMordechay Goodstein 		for (i = 0; i < ARRAY_SIZE(average_energy); i++)
11526324c173SMordechay Goodstein 			average_energy[i] =
1153b6e3d1baSAnjaneyulu 				le32_to_cpu(stats->per_sta[i].average_energy);
11546324c173SMordechay Goodstein 
11556324c173SMordechay Goodstein 		for (i = 0; i < ARRAY_SIZE(air_time); i++) {
1156b6e3d1baSAnjaneyulu 			air_time[i] = stats->per_mac[i].air_time;
1157b6e3d1baSAnjaneyulu 			rx_bytes[i] = stats->per_mac[i].rx_bytes;
11586324c173SMordechay Goodstein 		}
11596324c173SMordechay Goodstein 	}
11606324c173SMordechay Goodstein 
11616324c173SMordechay Goodstein 	iwl_mvm_rx_stats_check_trigger(mvm, pkt);
11626324c173SMordechay Goodstein 
1163c6bae216SMordechay Goodstein 	ieee80211_iterate_stations_atomic(mvm->hw, iwl_mvm_stats_energy_iter,
1164c6bae216SMordechay Goodstein 					  average_energy);
1165853f4954SMordechay Goodstein 	/*
1166853f4954SMordechay Goodstein 	 * Don't update in case the statistics are not cleared, since
1167853f4954SMordechay Goodstein 	 * we will end up counting twice the same airtime, once in TCM
1168853f4954SMordechay Goodstein 	 * request and once in statistics notification.
1169853f4954SMordechay Goodstein 	 */
1170853f4954SMordechay Goodstein 	if (le32_to_cpu(flags) & IWL_STATISTICS_REPLY_FLG_CLEAR)
11716324c173SMordechay Goodstein 		iwl_mvm_update_tcm_from_stats(mvm, air_time, rx_bytes);
1172853f4954SMordechay Goodstein }
1173853f4954SMordechay Goodstein 
1174e705c121SKalle Valo void iwl_mvm_handle_rx_statistics(struct iwl_mvm *mvm,
1175e705c121SKalle Valo 				  struct iwl_rx_packet *pkt)
1176e705c121SKalle Valo {
1177e705c121SKalle Valo 	struct iwl_mvm_stat_data data = {
1178e705c121SKalle Valo 		.mvm = mvm,
1179e705c121SKalle Valo 	};
1180853f4954SMordechay Goodstein 	__le32 *bytes, *air_time, flags;
11810e7ac018SSara Sharon 	int expected_size;
1182cfbeb598SGoodstein, Mordechay 	u8 *energy;
1183b6e3d1baSAnjaneyulu 	u8 cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw,
1184b6e3d1baSAnjaneyulu 					   WIDE_ID(SYSTEM_GROUP,
1185b6e3d1baSAnjaneyulu 						   SYSTEM_STATISTICS_CMD),
1186b6e3d1baSAnjaneyulu 					   IWL_FW_CMD_VER_UNKNOWN);
1187b6e3d1baSAnjaneyulu 
1188b6e3d1baSAnjaneyulu 	if (cmd_ver != IWL_FW_CMD_VER_UNKNOWN)
1189b6e3d1baSAnjaneyulu 		return;
1190853f4954SMordechay Goodstein 
1191853f4954SMordechay Goodstein 	/* From ver 14 and up we use TLV statistics format */
11926324c173SMordechay Goodstein 	if (iwl_fw_lookup_notif_ver(mvm->fw, LEGACY_GROUP,
11936324c173SMordechay Goodstein 				    STATISTICS_NOTIFICATION, 0) >= 14)
1194853f4954SMordechay Goodstein 		return iwl_mvm_handle_rx_statistics_tlv(mvm, pkt);
11950e7ac018SSara Sharon 
1196678d9b6dSLiad Kaufman 	if (!iwl_mvm_has_new_rx_stats_api(mvm)) {
1197678d9b6dSLiad Kaufman 		if (iwl_mvm_has_new_rx_api(mvm))
11980e7ac018SSara Sharon 			expected_size = sizeof(struct iwl_notif_statistics_v11);
11990e7ac018SSara Sharon 		else
12000e7ac018SSara Sharon 			expected_size = sizeof(struct iwl_notif_statistics_v10);
1201678d9b6dSLiad Kaufman 	} else {
1202606b9ab6SEmmanuel Grumbach 		expected_size = sizeof(struct iwl_notif_statistics);
1203678d9b6dSLiad Kaufman 	}
1204e705c121SKalle Valo 
120542fa5e0eSJohannes Berg 	if (WARN_ONCE(iwl_rx_packet_payload_len(pkt) != expected_size,
120642fa5e0eSJohannes Berg 		      "received invalid statistics size (%d)!\n",
120742fa5e0eSJohannes Berg 		      iwl_rx_packet_payload_len(pkt)))
12086a90f85aSLuca Coelho 		return;
1209e705c121SKalle Valo 
1210678d9b6dSLiad Kaufman 	if (!iwl_mvm_has_new_rx_stats_api(mvm)) {
1211678d9b6dSLiad Kaufman 		struct iwl_notif_statistics_v11 *stats = (void *)&pkt->data;
1212678d9b6dSLiad Kaufman 
1213e705c121SKalle Valo 		data.mac_id = stats->rx.general.mac_id;
1214e705c121SKalle Valo 		data.beacon_filter_average_energy =
12150e7ac018SSara Sharon 			stats->general.common.beacon_filter_average_energy;
1216e705c121SKalle Valo 
1217678d9b6dSLiad Kaufman 		mvm->rx_stats_v3 = stats->rx;
1218e705c121SKalle Valo 
1219678d9b6dSLiad Kaufman 		mvm->radio_stats.rx_time =
1220678d9b6dSLiad Kaufman 			le64_to_cpu(stats->general.common.rx_time);
1221678d9b6dSLiad Kaufman 		mvm->radio_stats.tx_time =
1222678d9b6dSLiad Kaufman 			le64_to_cpu(stats->general.common.tx_time);
1223e705c121SKalle Valo 		mvm->radio_stats.on_time_rf =
12240e7ac018SSara Sharon 			le64_to_cpu(stats->general.common.on_time_rf);
1225e705c121SKalle Valo 		mvm->radio_stats.on_time_scan =
12260e7ac018SSara Sharon 			le64_to_cpu(stats->general.common.on_time_scan);
1227e705c121SKalle Valo 
1228853f4954SMordechay Goodstein 		data.beacon_counter = stats->general.beacon_counter;
1229853f4954SMordechay Goodstein 		data.beacon_average_energy =
1230853f4954SMordechay Goodstein 			stats->general.beacon_average_energy;
1231678d9b6dSLiad Kaufman 		flags = stats->flag;
1232678d9b6dSLiad Kaufman 	} else {
1233606b9ab6SEmmanuel Grumbach 		struct iwl_notif_statistics *stats = (void *)&pkt->data;
1234678d9b6dSLiad Kaufman 
1235678d9b6dSLiad Kaufman 		data.mac_id = stats->rx.general.mac_id;
1236678d9b6dSLiad Kaufman 		data.beacon_filter_average_energy =
1237678d9b6dSLiad Kaufman 			stats->general.common.beacon_filter_average_energy;
1238678d9b6dSLiad Kaufman 
1239678d9b6dSLiad Kaufman 		mvm->rx_stats = stats->rx;
1240678d9b6dSLiad Kaufman 
1241678d9b6dSLiad Kaufman 		mvm->radio_stats.rx_time =
1242678d9b6dSLiad Kaufman 			le64_to_cpu(stats->general.common.rx_time);
1243678d9b6dSLiad Kaufman 		mvm->radio_stats.tx_time =
1244678d9b6dSLiad Kaufman 			le64_to_cpu(stats->general.common.tx_time);
1245678d9b6dSLiad Kaufman 		mvm->radio_stats.on_time_rf =
1246678d9b6dSLiad Kaufman 			le64_to_cpu(stats->general.common.on_time_rf);
1247678d9b6dSLiad Kaufman 		mvm->radio_stats.on_time_scan =
1248678d9b6dSLiad Kaufman 			le64_to_cpu(stats->general.common.on_time_scan);
1249678d9b6dSLiad Kaufman 
1250853f4954SMordechay Goodstein 		data.beacon_counter = stats->general.beacon_counter;
1251853f4954SMordechay Goodstein 		data.beacon_average_energy =
1252853f4954SMordechay Goodstein 			stats->general.beacon_average_energy;
1253678d9b6dSLiad Kaufman 		flags = stats->flag;
1254678d9b6dSLiad Kaufman 	}
1255290d5e49SMordechay Goodstein 	data.flags = flags;
1256678d9b6dSLiad Kaufman 
1257cfbeb598SGoodstein, Mordechay 	iwl_mvm_rx_stats_check_trigger(mvm, pkt);
1258cfbeb598SGoodstein, Mordechay 
1259cfbeb598SGoodstein, Mordechay 	ieee80211_iterate_active_interfaces(mvm->hw,
1260cfbeb598SGoodstein, Mordechay 					    IEEE80211_IFACE_ITER_NORMAL,
1261cfbeb598SGoodstein, Mordechay 					    iwl_mvm_stat_iterator,
1262cfbeb598SGoodstein, Mordechay 					    &data);
1263cfbeb598SGoodstein, Mordechay 
1264cfbeb598SGoodstein, Mordechay 	if (!iwl_mvm_has_new_rx_api(mvm))
1265cfbeb598SGoodstein, Mordechay 		return;
1266988b5968SSara Sharon 
1267678d9b6dSLiad Kaufman 	if (!iwl_mvm_has_new_rx_stats_api(mvm)) {
1268678d9b6dSLiad Kaufman 		struct iwl_notif_statistics_v11 *v11 = (void *)&pkt->data;
12690e7ac018SSara Sharon 
12700e7ac018SSara Sharon 		energy = (void *)&v11->load_stats.avg_energy;
1271b0ffe455SJohannes Berg 		bytes = (void *)&v11->load_stats.byte_count;
12720e7ac018SSara Sharon 		air_time = (void *)&v11->load_stats.air_time;
12730e7ac018SSara Sharon 	} else {
1274606b9ab6SEmmanuel Grumbach 		struct iwl_notif_statistics *stats = (void *)&pkt->data;
1275678d9b6dSLiad Kaufman 
12760e7ac018SSara Sharon 		energy = (void *)&stats->load_stats.avg_energy;
1277b0ffe455SJohannes Berg 		bytes = (void *)&stats->load_stats.byte_count;
12780e7ac018SSara Sharon 		air_time = (void *)&stats->load_stats.air_time;
12790e7ac018SSara Sharon 	}
1280c6bae216SMordechay Goodstein 	ieee80211_iterate_stations_atomic(mvm->hw, iwl_mvm_stats_energy_iter,
1281c6bae216SMordechay Goodstein 					  energy);
12827d9d0d56SLuca Coelho 
12837d9d0d56SLuca Coelho 	/*
12847d9d0d56SLuca Coelho 	 * Don't update in case the statistics are not cleared, since
12857d9d0d56SLuca Coelho 	 * we will end up counting twice the same airtime, once in TCM
12867d9d0d56SLuca Coelho 	 * request and once in statistics notification.
12877d9d0d56SLuca Coelho 	 */
1288853f4954SMordechay Goodstein 	if (le32_to_cpu(flags) & IWL_STATISTICS_REPLY_FLG_CLEAR)
1289853f4954SMordechay Goodstein 		iwl_mvm_update_tcm_from_stats(mvm, air_time, bytes);
12907d9d0d56SLuca Coelho 
1291e705c121SKalle Valo }
1292e705c121SKalle Valo 
1293e705c121SKalle Valo void iwl_mvm_rx_statistics(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
1294e705c121SKalle Valo {
1295e705c121SKalle Valo 	iwl_mvm_handle_rx_statistics(mvm, rxb_addr(rxb));
1296e705c121SKalle Valo }
12973af512d6SSara Sharon 
12983af512d6SSara Sharon void iwl_mvm_window_status_notif(struct iwl_mvm *mvm,
12993af512d6SSara Sharon 				 struct iwl_rx_cmd_buffer *rxb)
13003af512d6SSara Sharon {
13013af512d6SSara Sharon 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
13023af512d6SSara Sharon 	struct iwl_ba_window_status_notif *notif = (void *)pkt->data;
13033af512d6SSara Sharon 	int i;
13043af512d6SSara Sharon 
1305afc857bcSJohannes Berg 	BUILD_BUG_ON(ARRAY_SIZE(notif->ra_tid) != BA_WINDOW_STREAMS_MAX);
1306afc857bcSJohannes Berg 	BUILD_BUG_ON(ARRAY_SIZE(notif->mpdu_rx_count) != BA_WINDOW_STREAMS_MAX);
1307afc857bcSJohannes Berg 	BUILD_BUG_ON(ARRAY_SIZE(notif->bitmap) != BA_WINDOW_STREAMS_MAX);
1308afc857bcSJohannes Berg 	BUILD_BUG_ON(ARRAY_SIZE(notif->start_seq_num) != BA_WINDOW_STREAMS_MAX);
13093af512d6SSara Sharon 
13103af512d6SSara Sharon 	rcu_read_lock();
13113af512d6SSara Sharon 	for (i = 0; i < BA_WINDOW_STREAMS_MAX; i++) {
13123af512d6SSara Sharon 		struct ieee80211_sta *sta;
13133af512d6SSara Sharon 		u8 sta_id, tid;
13143af512d6SSara Sharon 		u64 bitmap;
13153af512d6SSara Sharon 		u32 ssn;
13163af512d6SSara Sharon 		u16 ratid;
13173af512d6SSara Sharon 		u16 received_mpdu;
13183af512d6SSara Sharon 
13193af512d6SSara Sharon 		ratid = le16_to_cpu(notif->ra_tid[i]);
13203af512d6SSara Sharon 		/* check that this TID is valid */
13213af512d6SSara Sharon 		if (!(ratid & BA_WINDOW_STATUS_VALID_MSK))
13223af512d6SSara Sharon 			continue;
13233af512d6SSara Sharon 
13243af512d6SSara Sharon 		received_mpdu = le16_to_cpu(notif->mpdu_rx_count[i]);
13253af512d6SSara Sharon 		if (received_mpdu == 0)
13263af512d6SSara Sharon 			continue;
13273af512d6SSara Sharon 
13283af512d6SSara Sharon 		tid = ratid & BA_WINDOW_STATUS_TID_MSK;
13293af512d6SSara Sharon 		/* get the station */
13303af512d6SSara Sharon 		sta_id = (ratid & BA_WINDOW_STATUS_STA_ID_MSK)
13313af512d6SSara Sharon 			 >> BA_WINDOW_STATUS_STA_ID_POS;
13323af512d6SSara Sharon 		sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
13333af512d6SSara Sharon 		if (IS_ERR_OR_NULL(sta))
13343af512d6SSara Sharon 			continue;
13353af512d6SSara Sharon 		bitmap = le64_to_cpu(notif->bitmap[i]);
13363af512d6SSara Sharon 		ssn = le32_to_cpu(notif->start_seq_num[i]);
13373af512d6SSara Sharon 
13383af512d6SSara Sharon 		/* update mac80211 with the bitmap for the reordering buffer */
13393af512d6SSara Sharon 		ieee80211_mark_rx_ba_filtered_frames(sta, tid, ssn, bitmap,
13403af512d6SSara Sharon 						     received_mpdu);
13413af512d6SSara Sharon 	}
13423af512d6SSara Sharon 	rcu_read_unlock();
13433af512d6SSara Sharon }
1344