xref: /linux/drivers/net/wireless/intel/iwlwifi/mvm/rx.c (revision 5a2abdcadc3bd7c8f9a54a89bf0ed1926db993a3)
18e99ea8dSJohannes Berg // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
28e99ea8dSJohannes Berg /*
38e99ea8dSJohannes Berg  * Copyright (C) 2012-2014, 2018-2020 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);
23e705c121SKalle Valo 
24e705c121SKalle Valo 	memcpy(&mvm->last_phy_info, pkt->data, sizeof(mvm->last_phy_info));
25e705c121SKalle Valo 	mvm->ampdu_ref++;
26e705c121SKalle Valo 
27e705c121SKalle Valo #ifdef CONFIG_IWLWIFI_DEBUGFS
28e705c121SKalle Valo 	if (mvm->last_phy_info.phy_flags & cpu_to_le16(RX_RES_PHY_FLAGS_AGG)) {
29e705c121SKalle Valo 		spin_lock(&mvm->drv_stats_lock);
30e705c121SKalle Valo 		mvm->drv_rx_stats.ampdu_count++;
31e705c121SKalle Valo 		spin_unlock(&mvm->drv_stats_lock);
32e705c121SKalle Valo 	}
33e705c121SKalle Valo #endif
34e705c121SKalle Valo }
35e705c121SKalle Valo 
36e705c121SKalle Valo /*
37e705c121SKalle Valo  * iwl_mvm_pass_packet_to_mac80211 - builds the packet for mac80211
38e705c121SKalle Valo  *
39e705c121SKalle Valo  * Adds the rxb to a new skb and give it to mac80211
40e705c121SKalle Valo  */
41e705c121SKalle Valo static void iwl_mvm_pass_packet_to_mac80211(struct iwl_mvm *mvm,
4243ec72b7SJohannes Berg 					    struct ieee80211_sta *sta,
43e705c121SKalle Valo 					    struct napi_struct *napi,
44e705c121SKalle Valo 					    struct sk_buff *skb,
45e705c121SKalle Valo 					    struct ieee80211_hdr *hdr, u16 len,
46bdbc58abSDan Carpenter 					    u8 crypt_len,
47e705c121SKalle Valo 					    struct iwl_rx_cmd_buffer *rxb)
48e705c121SKalle Valo {
495cddd05cSJohannes Berg 	unsigned int hdrlen = ieee80211_hdrlen(hdr->frame_control);
505cddd05cSJohannes Berg 	unsigned int fraglen;
515cddd05cSJohannes Berg 
525cddd05cSJohannes Berg 	/*
535cddd05cSJohannes Berg 	 * The 'hdrlen' (plus the 8 bytes for the SNAP and the crypt_len,
545cddd05cSJohannes Berg 	 * but those are all multiples of 4 long) all goes away, but we
555cddd05cSJohannes Berg 	 * want the *end* of it, which is going to be the start of the IP
565cddd05cSJohannes Berg 	 * header, to be aligned when it gets pulled in.
575cddd05cSJohannes Berg 	 * The beginning of the skb->data is aligned on at least a 4-byte
585cddd05cSJohannes Berg 	 * boundary after allocation. Everything here is aligned at least
595cddd05cSJohannes Berg 	 * on a 2-byte boundary so we can just take hdrlen & 3 and pad by
605cddd05cSJohannes Berg 	 * the result.
615cddd05cSJohannes Berg 	 */
625cddd05cSJohannes Berg 	skb_reserve(skb, hdrlen & 3);
63e705c121SKalle Valo 
64e705c121SKalle Valo 	/* If frame is small enough to fit in skb->head, pull it completely.
65e705c121SKalle Valo 	 * If not, only pull ieee80211_hdr (including crypto if present, and
66e705c121SKalle Valo 	 * an additional 8 bytes for SNAP/ethertype, see below) so that
67e705c121SKalle Valo 	 * splice() or TCP coalesce are more efficient.
68e705c121SKalle Valo 	 *
69e705c121SKalle Valo 	 * Since, in addition, ieee80211_data_to_8023() always pull in at
70e705c121SKalle Valo 	 * least 8 bytes (possibly more for mesh) we can do the same here
71e705c121SKalle Valo 	 * to save the cost of doing it later. That still doesn't pull in
72e705c121SKalle Valo 	 * the actual IP header since the typical case has a SNAP header.
73e705c121SKalle Valo 	 * If the latter changes (there are efforts in the standards group
74e705c121SKalle Valo 	 * to do so) we should revisit this and ieee80211_data_to_8023().
75e705c121SKalle Valo 	 */
765cddd05cSJohannes Berg 	hdrlen = (len <= skb_tailroom(skb)) ? len : hdrlen + crypt_len + 8;
77e705c121SKalle Valo 
7859ae1d12SJohannes Berg 	skb_put_data(skb, hdr, hdrlen);
79e705c121SKalle Valo 	fraglen = len - hdrlen;
80e705c121SKalle Valo 
81e705c121SKalle Valo 	if (fraglen) {
82e705c121SKalle Valo 		int offset = (void *)hdr + hdrlen -
83e705c121SKalle Valo 			     rxb_addr(rxb) + rxb_offset(rxb);
84e705c121SKalle Valo 
85e705c121SKalle Valo 		skb_add_rx_frag(skb, 0, rxb_steal_page(rxb), offset,
86e705c121SKalle Valo 				fraglen, rxb->truesize);
87e705c121SKalle Valo 	}
88e705c121SKalle Valo 
8943ec72b7SJohannes Berg 	ieee80211_rx_napi(mvm->hw, sta, skb, napi);
90e705c121SKalle Valo }
91e705c121SKalle Valo 
92e705c121SKalle Valo /*
93e705c121SKalle Valo  * iwl_mvm_get_signal_strength - use new rx PHY INFO API
94e705c121SKalle Valo  * values are reported by the fw as positive values - need to negate
95e705c121SKalle Valo  * to obtain their dBM.  Account for missing antennas by replacing 0
96e705c121SKalle Valo  * values by -256dBm: practically 0 power and a non-feasible 8 bit value.
97e705c121SKalle Valo  */
98e705c121SKalle Valo static void iwl_mvm_get_signal_strength(struct iwl_mvm *mvm,
99e705c121SKalle Valo 					struct iwl_rx_phy_info *phy_info,
100e705c121SKalle Valo 					struct ieee80211_rx_status *rx_status)
101e705c121SKalle Valo {
102e705c121SKalle Valo 	int energy_a, energy_b, energy_c, max_energy;
103e705c121SKalle Valo 	u32 val;
104e705c121SKalle Valo 
105e705c121SKalle Valo 	val =
106e705c121SKalle Valo 	    le32_to_cpu(phy_info->non_cfg_phy[IWL_RX_INFO_ENERGY_ANT_ABC_IDX]);
107e705c121SKalle Valo 	energy_a = (val & IWL_RX_INFO_ENERGY_ANT_A_MSK) >>
108e705c121SKalle Valo 						IWL_RX_INFO_ENERGY_ANT_A_POS;
109e705c121SKalle Valo 	energy_a = energy_a ? -energy_a : S8_MIN;
110e705c121SKalle Valo 	energy_b = (val & IWL_RX_INFO_ENERGY_ANT_B_MSK) >>
111e705c121SKalle Valo 						IWL_RX_INFO_ENERGY_ANT_B_POS;
112e705c121SKalle Valo 	energy_b = energy_b ? -energy_b : S8_MIN;
113e705c121SKalle Valo 	energy_c = (val & IWL_RX_INFO_ENERGY_ANT_C_MSK) >>
114e705c121SKalle Valo 						IWL_RX_INFO_ENERGY_ANT_C_POS;
115e705c121SKalle Valo 	energy_c = energy_c ? -energy_c : S8_MIN;
116e705c121SKalle Valo 	max_energy = max(energy_a, energy_b);
117e705c121SKalle Valo 	max_energy = max(max_energy, energy_c);
118e705c121SKalle Valo 
119e705c121SKalle Valo 	IWL_DEBUG_STATS(mvm, "energy In A %d B %d C %d , and max %d\n",
120e705c121SKalle Valo 			energy_a, energy_b, energy_c, 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 	rx_status->chain_signal[2] = energy_c;
129e705c121SKalle Valo }
130e705c121SKalle Valo 
131e705c121SKalle Valo /*
132e705c121SKalle Valo  * iwl_mvm_set_mac80211_rx_flag - translate fw status to mac80211 format
133e705c121SKalle Valo  * @mvm: the mvm object
134e705c121SKalle Valo  * @hdr: 80211 header
135e705c121SKalle Valo  * @stats: status in mac80211's format
136e705c121SKalle Valo  * @rx_pkt_status: status coming from fw
137e705c121SKalle Valo  *
138e705c121SKalle Valo  * returns non 0 value if the packet should be dropped
139e705c121SKalle Valo  */
140e705c121SKalle Valo static u32 iwl_mvm_set_mac80211_rx_flag(struct iwl_mvm *mvm,
141e705c121SKalle Valo 					struct ieee80211_hdr *hdr,
142e705c121SKalle Valo 					struct ieee80211_rx_status *stats,
143e705c121SKalle Valo 					u32 rx_pkt_status,
144e705c121SKalle Valo 					u8 *crypt_len)
145e705c121SKalle Valo {
146e705c121SKalle Valo 	if (!ieee80211_has_protected(hdr->frame_control) ||
147e705c121SKalle Valo 	    (rx_pkt_status & RX_MPDU_RES_STATUS_SEC_ENC_MSK) ==
148e705c121SKalle Valo 			     RX_MPDU_RES_STATUS_SEC_NO_ENC)
149e705c121SKalle Valo 		return 0;
150e705c121SKalle Valo 
151e705c121SKalle Valo 	/* packet was encrypted with unknown alg */
152e705c121SKalle Valo 	if ((rx_pkt_status & RX_MPDU_RES_STATUS_SEC_ENC_MSK) ==
153e705c121SKalle Valo 					RX_MPDU_RES_STATUS_SEC_ENC_ERR)
154e705c121SKalle Valo 		return 0;
155e705c121SKalle Valo 
156e705c121SKalle Valo 	switch (rx_pkt_status & RX_MPDU_RES_STATUS_SEC_ENC_MSK) {
157e705c121SKalle Valo 	case RX_MPDU_RES_STATUS_SEC_CCM_ENC:
158e705c121SKalle Valo 		/* alg is CCM: check MIC only */
159e705c121SKalle Valo 		if (!(rx_pkt_status & RX_MPDU_RES_STATUS_MIC_OK))
160e705c121SKalle Valo 			return -1;
161e705c121SKalle Valo 
162e705c121SKalle Valo 		stats->flag |= RX_FLAG_DECRYPTED;
163e705c121SKalle Valo 		*crypt_len = IEEE80211_CCMP_HDR_LEN;
164e705c121SKalle Valo 		return 0;
165e705c121SKalle Valo 
166e705c121SKalle Valo 	case RX_MPDU_RES_STATUS_SEC_TKIP_ENC:
167e705c121SKalle Valo 		/* Don't drop the frame and decrypt it in SW */
16857df3839SSara Sharon 		if (!fw_has_api(&mvm->fw->ucode_capa,
16957df3839SSara Sharon 				IWL_UCODE_TLV_API_DEPRECATE_TTAK) &&
17057df3839SSara Sharon 		    !(rx_pkt_status & RX_MPDU_RES_STATUS_TTAK_OK))
171e705c121SKalle Valo 			return 0;
172e705c121SKalle Valo 		*crypt_len = IEEE80211_TKIP_IV_LEN;
173*5a2abdcaSGustavo A. R. Silva 		fallthrough;
174e705c121SKalle Valo 
175e705c121SKalle Valo 	case RX_MPDU_RES_STATUS_SEC_WEP_ENC:
176e705c121SKalle Valo 		if (!(rx_pkt_status & RX_MPDU_RES_STATUS_ICV_OK))
177e705c121SKalle Valo 			return -1;
178e705c121SKalle Valo 
179e705c121SKalle Valo 		stats->flag |= RX_FLAG_DECRYPTED;
180e705c121SKalle Valo 		if ((rx_pkt_status & RX_MPDU_RES_STATUS_SEC_ENC_MSK) ==
181e705c121SKalle Valo 				RX_MPDU_RES_STATUS_SEC_WEP_ENC)
182e705c121SKalle Valo 			*crypt_len = IEEE80211_WEP_IV_LEN;
183e705c121SKalle Valo 		return 0;
184e705c121SKalle Valo 
185e705c121SKalle Valo 	case RX_MPDU_RES_STATUS_SEC_EXT_ENC:
186e705c121SKalle Valo 		if (!(rx_pkt_status & RX_MPDU_RES_STATUS_MIC_OK))
187e705c121SKalle Valo 			return -1;
188e705c121SKalle Valo 		stats->flag |= RX_FLAG_DECRYPTED;
189e705c121SKalle Valo 		return 0;
190e705c121SKalle Valo 
191e705c121SKalle Valo 	default:
192baf41bc3SShaul Triebitz 		/* Expected in monitor (not having the keys) */
193baf41bc3SShaul Triebitz 		if (!mvm->monitor_on)
194e705c121SKalle Valo 			IWL_ERR(mvm, "Unhandled alg: 0x%x\n", rx_pkt_status);
195e705c121SKalle Valo 	}
196e705c121SKalle Valo 
197e705c121SKalle Valo 	return 0;
198e705c121SKalle Valo }
199e705c121SKalle Valo 
2007d9d0d56SLuca Coelho static void iwl_mvm_rx_handle_tcm(struct iwl_mvm *mvm,
2017d9d0d56SLuca Coelho 				  struct ieee80211_sta *sta,
2027d9d0d56SLuca Coelho 				  struct ieee80211_hdr *hdr, u32 len,
2037d9d0d56SLuca Coelho 				  struct iwl_rx_phy_info *phy_info,
2047d9d0d56SLuca Coelho 				  u32 rate_n_flags)
2057d9d0d56SLuca Coelho {
2067d9d0d56SLuca Coelho 	struct iwl_mvm_sta *mvmsta;
2077d9d0d56SLuca Coelho 	struct iwl_mvm_tcm_mac *mdata;
2087d9d0d56SLuca Coelho 	int mac;
2097d9d0d56SLuca Coelho 	int ac = IEEE80211_AC_BE; /* treat non-QoS as BE */
210b0ffe455SJohannes Berg 	struct iwl_mvm_vif *mvmvif;
211b0ffe455SJohannes Berg 	/* expected throughput in 100Kbps, single stream, 20 MHz */
212b0ffe455SJohannes Berg 	static const u8 thresh_tpt[] = {
213b0ffe455SJohannes Berg 		9, 18, 30, 42, 60, 78, 90, 96, 120, 135,
214b0ffe455SJohannes Berg 	};
215b0ffe455SJohannes Berg 	u16 thr;
2167d9d0d56SLuca Coelho 
2177d9d0d56SLuca Coelho 	if (ieee80211_is_data_qos(hdr->frame_control))
2187d9d0d56SLuca Coelho 		ac = tid_to_mac80211_ac[ieee80211_get_tid(hdr)];
2197d9d0d56SLuca Coelho 
2207d9d0d56SLuca Coelho 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
2217d9d0d56SLuca Coelho 	mac = mvmsta->mac_id_n_color & FW_CTXT_ID_MSK;
2227d9d0d56SLuca Coelho 
2237d9d0d56SLuca Coelho 	if (time_after(jiffies, mvm->tcm.ts + MVM_TCM_PERIOD))
2247d9d0d56SLuca Coelho 		schedule_delayed_work(&mvm->tcm.work, 0);
2257d9d0d56SLuca Coelho 	mdata = &mvm->tcm.data[mac];
2267d9d0d56SLuca Coelho 	mdata->rx.pkts[ac]++;
2277d9d0d56SLuca Coelho 
2287d9d0d56SLuca Coelho 	/* count the airtime only once for each ampdu */
2297d9d0d56SLuca Coelho 	if (mdata->rx.last_ampdu_ref != mvm->ampdu_ref) {
2307d9d0d56SLuca Coelho 		mdata->rx.last_ampdu_ref = mvm->ampdu_ref;
2317d9d0d56SLuca Coelho 		mdata->rx.airtime += le16_to_cpu(phy_info->frame_time);
2327d9d0d56SLuca Coelho 	}
2337d9d0d56SLuca Coelho 
2347d9d0d56SLuca Coelho 	if (!(rate_n_flags & (RATE_MCS_HT_MSK | RATE_MCS_VHT_MSK)))
2357d9d0d56SLuca Coelho 		return;
2367d9d0d56SLuca Coelho 
237b0ffe455SJohannes Berg 	mvmvif = iwl_mvm_vif_from_mac80211(mvmsta->vif);
238b0ffe455SJohannes Berg 
239b0ffe455SJohannes Berg 	if (mdata->opened_rx_ba_sessions ||
240b0ffe455SJohannes Berg 	    mdata->uapsd_nonagg_detect.detected ||
241b0ffe455SJohannes Berg 	    (!mvmvif->queue_params[IEEE80211_AC_VO].uapsd &&
242b0ffe455SJohannes Berg 	     !mvmvif->queue_params[IEEE80211_AC_VI].uapsd &&
243b0ffe455SJohannes Berg 	     !mvmvif->queue_params[IEEE80211_AC_BE].uapsd &&
244b0ffe455SJohannes Berg 	     !mvmvif->queue_params[IEEE80211_AC_BK].uapsd) ||
245b0ffe455SJohannes Berg 	    mvmsta->sta_id != mvmvif->ap_sta_id)
246b0ffe455SJohannes Berg 		return;
247b0ffe455SJohannes Berg 
248b0ffe455SJohannes Berg 	if (rate_n_flags & RATE_MCS_HT_MSK) {
249b0ffe455SJohannes Berg 		thr = thresh_tpt[rate_n_flags & RATE_HT_MCS_RATE_CODE_MSK];
250b0ffe455SJohannes Berg 		thr *= 1 + ((rate_n_flags & RATE_HT_MCS_NSS_MSK) >>
251b0ffe455SJohannes Berg 					RATE_HT_MCS_NSS_POS);
252b0ffe455SJohannes Berg 	} else {
253b0ffe455SJohannes Berg 		if (WARN_ON((rate_n_flags & RATE_VHT_MCS_RATE_CODE_MSK) >=
254b0ffe455SJohannes Berg 				ARRAY_SIZE(thresh_tpt)))
255b0ffe455SJohannes Berg 			return;
256b0ffe455SJohannes Berg 		thr = thresh_tpt[rate_n_flags & RATE_VHT_MCS_RATE_CODE_MSK];
257b0ffe455SJohannes Berg 		thr *= 1 + ((rate_n_flags & RATE_VHT_MCS_NSS_MSK) >>
258b0ffe455SJohannes Berg 					RATE_VHT_MCS_NSS_POS);
259b0ffe455SJohannes Berg 	}
260b0ffe455SJohannes Berg 
261b0ffe455SJohannes Berg 	thr <<= ((rate_n_flags & RATE_MCS_CHAN_WIDTH_MSK) >>
262b0ffe455SJohannes Berg 				RATE_MCS_CHAN_WIDTH_POS);
263b0ffe455SJohannes Berg 
264b0ffe455SJohannes Berg 	mdata->uapsd_nonagg_detect.rx_bytes += len;
265b0ffe455SJohannes Berg 	ewma_rate_add(&mdata->uapsd_nonagg_detect.rate, thr);
2667d9d0d56SLuca Coelho }
2677d9d0d56SLuca Coelho 
268e705c121SKalle Valo static void iwl_mvm_rx_csum(struct ieee80211_sta *sta,
269e705c121SKalle Valo 			    struct sk_buff *skb,
270e705c121SKalle Valo 			    u32 status)
271e705c121SKalle Valo {
272e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
273e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(mvmsta->vif);
274e705c121SKalle Valo 
275e705c121SKalle Valo 	if (mvmvif->features & NETIF_F_RXCSUM &&
276e705c121SKalle Valo 	    status & RX_MPDU_RES_STATUS_CSUM_DONE &&
277e705c121SKalle Valo 	    status & RX_MPDU_RES_STATUS_CSUM_OK)
278e705c121SKalle Valo 		skb->ip_summed = CHECKSUM_UNNECESSARY;
279e705c121SKalle Valo }
280e705c121SKalle Valo 
281e705c121SKalle Valo /*
282e705c121SKalle Valo  * iwl_mvm_rx_rx_mpdu - REPLY_RX_MPDU_CMD handler
283e705c121SKalle Valo  *
284e705c121SKalle Valo  * Handles the actual data of the Rx packet from the fw
285e705c121SKalle Valo  */
286e705c121SKalle Valo void iwl_mvm_rx_rx_mpdu(struct iwl_mvm *mvm, struct napi_struct *napi,
287e705c121SKalle Valo 			struct iwl_rx_cmd_buffer *rxb)
288e705c121SKalle Valo {
289e705c121SKalle Valo 	struct ieee80211_hdr *hdr;
290e705c121SKalle Valo 	struct ieee80211_rx_status *rx_status;
291e705c121SKalle Valo 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
292e705c121SKalle Valo 	struct iwl_rx_phy_info *phy_info;
293e705c121SKalle Valo 	struct iwl_rx_mpdu_res_start *rx_res;
294a399f980SJohannes Berg 	struct ieee80211_sta *sta = NULL;
295e705c121SKalle Valo 	struct sk_buff *skb;
296efc0ec5aSJohannes Berg 	u32 len, pkt_len = iwl_rx_packet_payload_len(pkt);
297e705c121SKalle Valo 	u32 rate_n_flags;
298e705c121SKalle Valo 	u32 rx_pkt_status;
299e705c121SKalle Valo 	u8 crypt_len = 0;
300e705c121SKalle Valo 
301b8aba27cSJohannes Berg 	if (unlikely(pkt_len < sizeof(*rx_res))) {
302b8aba27cSJohannes Berg 		IWL_DEBUG_DROP(mvm, "Bad REPLY_RX_MPDU_CMD size\n");
303b8aba27cSJohannes Berg 		return;
304b8aba27cSJohannes Berg 	}
305b8aba27cSJohannes Berg 
306e705c121SKalle Valo 	phy_info = &mvm->last_phy_info;
307e705c121SKalle Valo 	rx_res = (struct iwl_rx_mpdu_res_start *)pkt->data;
308e705c121SKalle Valo 	hdr = (struct ieee80211_hdr *)(pkt->data + sizeof(*rx_res));
309e705c121SKalle Valo 	len = le16_to_cpu(rx_res->byte_count);
310efc0ec5aSJohannes Berg 
311efc0ec5aSJohannes Berg 	if (unlikely(len + sizeof(*rx_res) + sizeof(__le32) > pkt_len)) {
312efc0ec5aSJohannes Berg 		IWL_DEBUG_DROP(mvm, "FW lied about packet len\n");
313efc0ec5aSJohannes Berg 		return;
314efc0ec5aSJohannes Berg 	}
315efc0ec5aSJohannes Berg 
316c5aaa8beSWang Xuerui 	rx_pkt_status = get_unaligned_le32((__le32 *)
317e705c121SKalle Valo 		(pkt->data + sizeof(*rx_res) + len));
318e705c121SKalle Valo 
319e705c121SKalle Valo 	/* Dont use dev_alloc_skb(), we'll have enough headroom once
320e705c121SKalle Valo 	 * ieee80211_hdr pulled.
321e705c121SKalle Valo 	 */
322e705c121SKalle Valo 	skb = alloc_skb(128, GFP_ATOMIC);
323e705c121SKalle Valo 	if (!skb) {
324e705c121SKalle Valo 		IWL_ERR(mvm, "alloc_skb failed\n");
325e705c121SKalle Valo 		return;
326e705c121SKalle Valo 	}
327e705c121SKalle Valo 
328e705c121SKalle Valo 	rx_status = IEEE80211_SKB_RXCB(skb);
329e705c121SKalle Valo 
330e705c121SKalle Valo 	/*
331e705c121SKalle Valo 	 * drop the packet if it has failed being decrypted by HW
332e705c121SKalle Valo 	 */
333e705c121SKalle Valo 	if (iwl_mvm_set_mac80211_rx_flag(mvm, hdr, rx_status, rx_pkt_status,
334e705c121SKalle Valo 					 &crypt_len)) {
335e705c121SKalle Valo 		IWL_DEBUG_DROP(mvm, "Bad decryption results 0x%08x\n",
336e705c121SKalle Valo 			       rx_pkt_status);
337e705c121SKalle Valo 		kfree_skb(skb);
338e705c121SKalle Valo 		return;
339e705c121SKalle Valo 	}
340e705c121SKalle Valo 
341e705c121SKalle Valo 	/*
342e705c121SKalle Valo 	 * Keep packets with CRC errors (and with overrun) for monitor mode
343e705c121SKalle Valo 	 * (otherwise the firmware discards them) but mark them as bad.
344e705c121SKalle Valo 	 */
345e705c121SKalle Valo 	if (!(rx_pkt_status & RX_MPDU_RES_STATUS_CRC_OK) ||
346e705c121SKalle Valo 	    !(rx_pkt_status & RX_MPDU_RES_STATUS_OVERRUN_OK)) {
347e705c121SKalle Valo 		IWL_DEBUG_RX(mvm, "Bad CRC or FIFO: 0x%08X.\n", rx_pkt_status);
348e705c121SKalle Valo 		rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
349e705c121SKalle Valo 	}
350e705c121SKalle Valo 
351e705c121SKalle Valo 	/* This will be used in several places later */
352e705c121SKalle Valo 	rate_n_flags = le32_to_cpu(phy_info->rate_n_flags);
353e705c121SKalle Valo 
354e705c121SKalle Valo 	/* rx_status carries information about the packet to mac80211 */
355e705c121SKalle Valo 	rx_status->mactime = le64_to_cpu(phy_info->timestamp);
356e705c121SKalle Valo 	rx_status->device_timestamp = le32_to_cpu(phy_info->system_timestamp);
357e705c121SKalle Valo 	rx_status->band =
358e705c121SKalle Valo 		(phy_info->phy_flags & cpu_to_le16(RX_RES_PHY_FLAGS_BAND_24)) ?
35957fbcce3SJohannes Berg 				NL80211_BAND_2GHZ : NL80211_BAND_5GHZ;
360e705c121SKalle Valo 	rx_status->freq =
361e705c121SKalle Valo 		ieee80211_channel_to_frequency(le16_to_cpu(phy_info->channel),
362e705c121SKalle Valo 					       rx_status->band);
36377fe7395SSara Sharon 
36477fe7395SSara Sharon 	/* TSF as indicated by the firmware  is at INA time */
36577fe7395SSara Sharon 	rx_status->flag |= RX_FLAG_MACTIME_PLCP_START;
366e705c121SKalle Valo 
367e705c121SKalle Valo 	iwl_mvm_get_signal_strength(mvm, phy_info, rx_status);
368e705c121SKalle Valo 
369e705c121SKalle Valo 	IWL_DEBUG_STATS_LIMIT(mvm, "Rssi %d, TSF %llu\n", rx_status->signal,
370e705c121SKalle Valo 			      (unsigned long long)rx_status->mactime);
371e705c121SKalle Valo 
372e705c121SKalle Valo 	rcu_read_lock();
373a399f980SJohannes Berg 	if (rx_pkt_status & RX_MPDU_RES_STATUS_SRC_STA_FOUND) {
374a399f980SJohannes Berg 		u32 id = rx_pkt_status & RX_MPDU_RES_STATUS_STA_ID_MSK;
375a399f980SJohannes Berg 
376a399f980SJohannes Berg 		id >>= RX_MDPU_RES_STATUS_STA_ID_SHIFT;
377a399f980SJohannes Berg 
378be9ae34eSNathan Errera 		if (!WARN_ON_ONCE(id >= mvm->fw->ucode_capa.num_stations)) {
379a399f980SJohannes Berg 			sta = rcu_dereference(mvm->fw_id_to_mac_id[id]);
380a399f980SJohannes Berg 			if (IS_ERR(sta))
381a399f980SJohannes Berg 				sta = NULL;
382a399f980SJohannes Berg 		}
383a399f980SJohannes Berg 	} else if (!is_multicast_ether_addr(hdr->addr2)) {
384a399f980SJohannes Berg 		/* This is fine since we prevent two stations with the same
385a399f980SJohannes Berg 		 * address from being added.
386e705c121SKalle Valo 		 */
387a399f980SJohannes Berg 		sta = ieee80211_find_sta_by_ifaddr(mvm->hw, hdr->addr2, NULL);
388e705c121SKalle Valo 	}
389e705c121SKalle Valo 
390e705c121SKalle Valo 	if (sta) {
391e705c121SKalle Valo 		struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
392d3a108a4SAndrei Otcheretianski 		struct ieee80211_vif *tx_blocked_vif =
393d3a108a4SAndrei Otcheretianski 			rcu_dereference(mvm->csa_tx_blocked_vif);
3946c042d75SSara Sharon 		struct iwl_fw_dbg_trigger_tlv *trig;
3956c042d75SSara Sharon 		struct ieee80211_vif *vif = mvmsta->vif;
396e705c121SKalle Valo 
397a399f980SJohannes Berg 		/* We have tx blocked stations (with CS bit). If we heard
398a399f980SJohannes Berg 		 * frames from a blocked station on a new channel we can
399a399f980SJohannes Berg 		 * TX to it again.
400a399f980SJohannes Berg 		 */
4016c042d75SSara Sharon 		if (unlikely(tx_blocked_vif) && vif == tx_blocked_vif) {
402d3a108a4SAndrei Otcheretianski 			struct iwl_mvm_vif *mvmvif =
403d3a108a4SAndrei Otcheretianski 				iwl_mvm_vif_from_mac80211(tx_blocked_vif);
404d3a108a4SAndrei Otcheretianski 
405d3a108a4SAndrei Otcheretianski 			if (mvmvif->csa_target_freq == rx_status->freq)
406d3a108a4SAndrei Otcheretianski 				iwl_mvm_sta_modify_disable_tx_ap(mvm, sta,
407d3a108a4SAndrei Otcheretianski 								 false);
408d3a108a4SAndrei Otcheretianski 		}
409a399f980SJohannes Berg 
410ecaf71deSGregory Greenman 		rs_update_last_rssi(mvm, mvmsta, rx_status);
411e705c121SKalle Valo 
4126c042d75SSara Sharon 		trig = iwl_fw_dbg_trigger_on(&mvm->fwrt,
4136c042d75SSara Sharon 					     ieee80211_vif_to_wdev(vif),
4146c042d75SSara Sharon 					     FW_DBG_TRIGGER_RSSI);
4156c042d75SSara Sharon 
4166c042d75SSara Sharon 		if (trig && ieee80211_is_beacon(hdr->frame_control)) {
417e705c121SKalle Valo 			struct iwl_fw_dbg_trigger_low_rssi *rssi_trig;
418e705c121SKalle Valo 			s32 rssi;
419e705c121SKalle Valo 
420e705c121SKalle Valo 			rssi_trig = (void *)trig->data;
421e705c121SKalle Valo 			rssi = le32_to_cpu(rssi_trig->rssi);
422e705c121SKalle Valo 
4236c042d75SSara Sharon 			if (rx_status->signal < rssi)
4247174beb6SJohannes Berg 				iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
4257174beb6SJohannes Berg 							NULL);
426e705c121SKalle Valo 		}
427e705c121SKalle Valo 
4287d9d0d56SLuca Coelho 		if (!mvm->tcm.paused && len >= sizeof(*hdr) &&
4297d9d0d56SLuca Coelho 		    !is_multicast_ether_addr(hdr->addr1) &&
4307d9d0d56SLuca Coelho 		    ieee80211_is_data(hdr->frame_control))
4317d9d0d56SLuca Coelho 			iwl_mvm_rx_handle_tcm(mvm, sta, hdr, len, phy_info,
4327d9d0d56SLuca Coelho 					      rate_n_flags);
4337d9d0d56SLuca Coelho 
434a399f980SJohannes Berg 		if (ieee80211_is_data(hdr->frame_control))
435e705c121SKalle Valo 			iwl_mvm_rx_csum(sta, skb, rx_pkt_status);
436a399f980SJohannes Berg 	}
437e705c121SKalle Valo 	rcu_read_unlock();
438e705c121SKalle Valo 
439e705c121SKalle Valo 	/* set the preamble flag if appropriate */
440e705c121SKalle Valo 	if (phy_info->phy_flags & cpu_to_le16(RX_RES_PHY_FLAGS_SHORT_PREAMBLE))
4417fdd69c5SJohannes Berg 		rx_status->enc_flags |= RX_ENC_FLAG_SHORTPRE;
442e705c121SKalle Valo 
443e705c121SKalle Valo 	if (phy_info->phy_flags & cpu_to_le16(RX_RES_PHY_FLAGS_AGG)) {
444e705c121SKalle Valo 		/*
445e705c121SKalle Valo 		 * We know which subframes of an A-MPDU belong
446e705c121SKalle Valo 		 * together since we get a single PHY response
447e705c121SKalle Valo 		 * from the firmware for all of them
448e705c121SKalle Valo 		 */
449e705c121SKalle Valo 		rx_status->flag |= RX_FLAG_AMPDU_DETAILS;
450e705c121SKalle Valo 		rx_status->ampdu_reference = mvm->ampdu_ref;
451e705c121SKalle Valo 	}
452e705c121SKalle Valo 
453e705c121SKalle Valo 	/* Set up the HT phy flags */
454e705c121SKalle Valo 	switch (rate_n_flags & RATE_MCS_CHAN_WIDTH_MSK) {
455e705c121SKalle Valo 	case RATE_MCS_CHAN_WIDTH_20:
456e705c121SKalle Valo 		break;
457e705c121SKalle Valo 	case RATE_MCS_CHAN_WIDTH_40:
458da6a4352SJohannes Berg 		rx_status->bw = RATE_INFO_BW_40;
459e705c121SKalle Valo 		break;
460e705c121SKalle Valo 	case RATE_MCS_CHAN_WIDTH_80:
461da6a4352SJohannes Berg 		rx_status->bw = RATE_INFO_BW_80;
462e705c121SKalle Valo 		break;
463e705c121SKalle Valo 	case RATE_MCS_CHAN_WIDTH_160:
464da6a4352SJohannes Berg 		rx_status->bw = RATE_INFO_BW_160;
465e705c121SKalle Valo 		break;
466e705c121SKalle Valo 	}
4674c59ff5aSSara Sharon 	if (!(rate_n_flags & RATE_MCS_CCK_MSK) &&
4684c59ff5aSSara Sharon 	    rate_n_flags & RATE_MCS_SGI_MSK)
4697fdd69c5SJohannes Berg 		rx_status->enc_flags |= RX_ENC_FLAG_SHORT_GI;
470e705c121SKalle Valo 	if (rate_n_flags & RATE_HT_MCS_GF_MSK)
4717fdd69c5SJohannes Berg 		rx_status->enc_flags |= RX_ENC_FLAG_HT_GF;
472e705c121SKalle Valo 	if (rate_n_flags & RATE_MCS_LDPC_MSK)
4737fdd69c5SJohannes Berg 		rx_status->enc_flags |= RX_ENC_FLAG_LDPC;
474e705c121SKalle Valo 	if (rate_n_flags & RATE_MCS_HT_MSK) {
47577e40945SSara Sharon 		u8 stbc = (rate_n_flags & RATE_MCS_STBC_MSK) >>
476e705c121SKalle Valo 				RATE_MCS_STBC_POS;
477da6a4352SJohannes Berg 		rx_status->encoding = RX_ENC_HT;
478e705c121SKalle Valo 		rx_status->rate_idx = rate_n_flags & RATE_HT_MCS_INDEX_MSK;
4797fdd69c5SJohannes Berg 		rx_status->enc_flags |= stbc << RX_ENC_FLAG_STBC_SHIFT;
480e705c121SKalle Valo 	} else if (rate_n_flags & RATE_MCS_VHT_MSK) {
48177e40945SSara Sharon 		u8 stbc = (rate_n_flags & RATE_MCS_STBC_MSK) >>
482e705c121SKalle Valo 				RATE_MCS_STBC_POS;
4838613c948SJohannes Berg 		rx_status->nss =
484e705c121SKalle Valo 			((rate_n_flags & RATE_VHT_MCS_NSS_MSK) >>
485e705c121SKalle Valo 						RATE_VHT_MCS_NSS_POS) + 1;
486e705c121SKalle Valo 		rx_status->rate_idx = rate_n_flags & RATE_VHT_MCS_RATE_CODE_MSK;
487da6a4352SJohannes Berg 		rx_status->encoding = RX_ENC_VHT;
4887fdd69c5SJohannes Berg 		rx_status->enc_flags |= stbc << RX_ENC_FLAG_STBC_SHIFT;
489e705c121SKalle Valo 		if (rate_n_flags & RATE_MCS_BF_MSK)
4907fdd69c5SJohannes Berg 			rx_status->enc_flags |= RX_ENC_FLAG_BF;
491e705c121SKalle Valo 	} else {
492cb2de6bbSSara Sharon 		int rate = iwl_mvm_legacy_rate_to_mac80211_idx(rate_n_flags,
493e705c121SKalle Valo 							       rx_status->band);
494cb2de6bbSSara Sharon 
495cb2de6bbSSara Sharon 		if (WARN(rate < 0 || rate > 0xFF,
496cb2de6bbSSara Sharon 			 "Invalid rate flags 0x%x, band %d,\n",
497cb2de6bbSSara Sharon 			 rate_n_flags, rx_status->band)) {
498cb2de6bbSSara Sharon 			kfree_skb(skb);
499cb2de6bbSSara Sharon 			return;
500cb2de6bbSSara Sharon 		}
501cb2de6bbSSara Sharon 		rx_status->rate_idx = rate;
502e705c121SKalle Valo 	}
503e705c121SKalle Valo 
504e705c121SKalle Valo #ifdef CONFIG_IWLWIFI_DEBUGFS
505e705c121SKalle Valo 	iwl_mvm_update_frame_stats(mvm, rate_n_flags,
506e705c121SKalle Valo 				   rx_status->flag & RX_FLAG_AMPDU_DETAILS);
507e705c121SKalle Valo #endif
508a339e918SLuca Coelho 
509a339e918SLuca Coelho 	if (unlikely((ieee80211_is_beacon(hdr->frame_control) ||
510a339e918SLuca Coelho 		      ieee80211_is_probe_resp(hdr->frame_control)) &&
511a339e918SLuca Coelho 		     mvm->sched_scan_pass_all == SCHED_SCAN_PASS_ALL_ENABLED))
512a339e918SLuca Coelho 		mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_FOUND;
513a339e918SLuca Coelho 
51443ec72b7SJohannes Berg 	if (unlikely(ieee80211_is_beacon(hdr->frame_control) ||
51543ec72b7SJohannes Berg 		     ieee80211_is_probe_resp(hdr->frame_control)))
5169285ec4cSJason A. Donenfeld 		rx_status->boottime_ns = ktime_get_boottime_ns();
51743ec72b7SJohannes Berg 
51843ec72b7SJohannes Berg 	iwl_mvm_pass_packet_to_mac80211(mvm, sta, napi, skb, hdr, len,
519bdbc58abSDan Carpenter 					crypt_len, rxb);
520e705c121SKalle Valo }
521e705c121SKalle Valo 
522e705c121SKalle Valo struct iwl_mvm_stat_data {
523e705c121SKalle Valo 	struct iwl_mvm *mvm;
524290d5e49SMordechay Goodstein 	__le32 flags;
525e705c121SKalle Valo 	__le32 mac_id;
526e705c121SKalle Valo 	u8 beacon_filter_average_energy;
527853f4954SMordechay Goodstein 	__le32 *beacon_counter;
528853f4954SMordechay Goodstein 	u8 *beacon_average_energy;
529e705c121SKalle Valo };
530e705c121SKalle Valo 
531e705c121SKalle Valo static void iwl_mvm_stat_iterator(void *_data, u8 *mac,
532e705c121SKalle Valo 				  struct ieee80211_vif *vif)
533e705c121SKalle Valo {
534e705c121SKalle Valo 	struct iwl_mvm_stat_data *data = _data;
535e705c121SKalle Valo 	struct iwl_mvm *mvm = data->mvm;
536e705c121SKalle Valo 	int sig = -data->beacon_filter_average_energy;
537e705c121SKalle Valo 	int last_event;
538e705c121SKalle Valo 	int thold = vif->bss_conf.cqm_rssi_thold;
539e705c121SKalle Valo 	int hyst = vif->bss_conf.cqm_rssi_hyst;
540e705c121SKalle Valo 	u16 id = le32_to_cpu(data->mac_id);
541e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
54255ed14cdSEmmanuel Grumbach 	u16 vif_id = mvmvif->id;
543e705c121SKalle Valo 
544e705c121SKalle Valo 	/* This doesn't need the MAC ID check since it's not taking the
545e705c121SKalle Valo 	 * data copied into the "data" struct, but rather the data from
546e705c121SKalle Valo 	 * the notification directly.
547e705c121SKalle Valo 	 */
548e705c121SKalle Valo 	mvmvif->beacon_stats.num_beacons =
549853f4954SMordechay Goodstein 		le32_to_cpu(data->beacon_counter[vif_id]);
550e705c121SKalle Valo 	mvmvif->beacon_stats.avg_signal =
551853f4954SMordechay Goodstein 		-data->beacon_average_energy[vif_id];
552e705c121SKalle Valo 
553290d5e49SMordechay Goodstein 	/* make sure that beacon statistics don't go backwards with TCM
554290d5e49SMordechay Goodstein 	 * request to clear statistics
555290d5e49SMordechay Goodstein 	 */
556290d5e49SMordechay Goodstein 	if (le32_to_cpu(data->flags) & IWL_STATISTICS_REPLY_FLG_CLEAR)
557290d5e49SMordechay Goodstein 		mvmvif->beacon_stats.accu_num_beacons +=
558290d5e49SMordechay Goodstein 			mvmvif->beacon_stats.num_beacons;
559290d5e49SMordechay Goodstein 
560e705c121SKalle Valo 	if (mvmvif->id != id)
561e705c121SKalle Valo 		return;
562e705c121SKalle Valo 
563e705c121SKalle Valo 	if (vif->type != NL80211_IFTYPE_STATION)
564e705c121SKalle Valo 		return;
565e705c121SKalle Valo 
566e705c121SKalle Valo 	if (sig == 0) {
567e705c121SKalle Valo 		IWL_DEBUG_RX(mvm, "RSSI is 0 - skip signal based decision\n");
568e705c121SKalle Valo 		return;
569e705c121SKalle Valo 	}
570e705c121SKalle Valo 
571e705c121SKalle Valo 	mvmvif->bf_data.ave_beacon_signal = sig;
572e705c121SKalle Valo 
573e705c121SKalle Valo 	/* BT Coex */
574e705c121SKalle Valo 	if (mvmvif->bf_data.bt_coex_min_thold !=
575e705c121SKalle Valo 	    mvmvif->bf_data.bt_coex_max_thold) {
576e705c121SKalle Valo 		last_event = mvmvif->bf_data.last_bt_coex_event;
577e705c121SKalle Valo 		if (sig > mvmvif->bf_data.bt_coex_max_thold &&
578e705c121SKalle Valo 		    (last_event <= mvmvif->bf_data.bt_coex_min_thold ||
579e705c121SKalle Valo 		     last_event == 0)) {
580e705c121SKalle Valo 			mvmvif->bf_data.last_bt_coex_event = sig;
581e705c121SKalle Valo 			IWL_DEBUG_RX(mvm, "cqm_iterator bt coex high %d\n",
582e705c121SKalle Valo 				     sig);
583e705c121SKalle Valo 			iwl_mvm_bt_rssi_event(mvm, vif, RSSI_EVENT_HIGH);
584e705c121SKalle Valo 		} else if (sig < mvmvif->bf_data.bt_coex_min_thold &&
585e705c121SKalle Valo 			   (last_event >= mvmvif->bf_data.bt_coex_max_thold ||
586e705c121SKalle Valo 			    last_event == 0)) {
587e705c121SKalle Valo 			mvmvif->bf_data.last_bt_coex_event = sig;
588e705c121SKalle Valo 			IWL_DEBUG_RX(mvm, "cqm_iterator bt coex low %d\n",
589e705c121SKalle Valo 				     sig);
590e705c121SKalle Valo 			iwl_mvm_bt_rssi_event(mvm, vif, RSSI_EVENT_LOW);
591e705c121SKalle Valo 		}
592e705c121SKalle Valo 	}
593e705c121SKalle Valo 
594e705c121SKalle Valo 	if (!(vif->driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI))
595e705c121SKalle Valo 		return;
596e705c121SKalle Valo 
597e705c121SKalle Valo 	/* CQM Notification */
598e705c121SKalle Valo 	last_event = mvmvif->bf_data.last_cqm_event;
599e705c121SKalle Valo 	if (thold && sig < thold && (last_event == 0 ||
600e705c121SKalle Valo 				     sig < last_event - hyst)) {
601e705c121SKalle Valo 		mvmvif->bf_data.last_cqm_event = sig;
602e705c121SKalle Valo 		IWL_DEBUG_RX(mvm, "cqm_iterator cqm low %d\n",
603e705c121SKalle Valo 			     sig);
604e705c121SKalle Valo 		ieee80211_cqm_rssi_notify(
605e705c121SKalle Valo 			vif,
606e705c121SKalle Valo 			NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
607769f07d8SAndrzej Zaborowski 			sig,
608e705c121SKalle Valo 			GFP_KERNEL);
609e705c121SKalle Valo 	} else if (sig > thold &&
610e705c121SKalle Valo 		   (last_event == 0 || sig > last_event + hyst)) {
611e705c121SKalle Valo 		mvmvif->bf_data.last_cqm_event = sig;
612e705c121SKalle Valo 		IWL_DEBUG_RX(mvm, "cqm_iterator cqm high %d\n",
613e705c121SKalle Valo 			     sig);
614e705c121SKalle Valo 		ieee80211_cqm_rssi_notify(
615e705c121SKalle Valo 			vif,
616e705c121SKalle Valo 			NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
617769f07d8SAndrzej Zaborowski 			sig,
618e705c121SKalle Valo 			GFP_KERNEL);
619e705c121SKalle Valo 	}
620e705c121SKalle Valo }
621e705c121SKalle Valo 
622e705c121SKalle Valo static inline void
623e705c121SKalle Valo iwl_mvm_rx_stats_check_trigger(struct iwl_mvm *mvm, struct iwl_rx_packet *pkt)
624e705c121SKalle Valo {
625e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_tlv *trig;
626e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_stats *trig_stats;
627e705c121SKalle Valo 	u32 trig_offset, trig_thold;
628e705c121SKalle Valo 
6296c042d75SSara Sharon 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, NULL, FW_DBG_TRIGGER_STATS);
6306c042d75SSara Sharon 	if (!trig)
631e705c121SKalle Valo 		return;
632e705c121SKalle Valo 
633e705c121SKalle Valo 	trig_stats = (void *)trig->data;
634e705c121SKalle Valo 
635e705c121SKalle Valo 	trig_offset = le32_to_cpu(trig_stats->stop_offset);
636e705c121SKalle Valo 	trig_thold = le32_to_cpu(trig_stats->stop_threshold);
637e705c121SKalle Valo 
638e705c121SKalle Valo 	if (WARN_ON_ONCE(trig_offset >= iwl_rx_packet_payload_len(pkt)))
639e705c121SKalle Valo 		return;
640e705c121SKalle Valo 
641e705c121SKalle Valo 	if (le32_to_cpup((__le32 *) (pkt->data + trig_offset)) < trig_thold)
642e705c121SKalle Valo 		return;
643e705c121SKalle Valo 
6447174beb6SJohannes Berg 	iwl_fw_dbg_collect_trig(&mvm->fwrt, trig, NULL);
645e705c121SKalle Valo }
646e705c121SKalle Valo 
647c6bae216SMordechay Goodstein static void iwl_mvm_stats_energy_iter(void *_data,
648c6bae216SMordechay Goodstein 				      struct ieee80211_sta *sta)
649853f4954SMordechay Goodstein {
650c6bae216SMordechay Goodstein 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
651c6bae216SMordechay Goodstein 	u8 *energy = _data;
652c6bae216SMordechay Goodstein 	u32 sta_id = mvmsta->sta_id;
653853f4954SMordechay Goodstein 
654c6bae216SMordechay Goodstein 	if (WARN_ONCE(sta_id >= IWL_MVM_STATION_COUNT_MAX, "sta_id %d >= %d",
655c6bae216SMordechay Goodstein 		      sta_id, IWL_MVM_STATION_COUNT_MAX))
656853f4954SMordechay Goodstein 		return;
657853f4954SMordechay Goodstein 
658c6bae216SMordechay Goodstein 	if (energy[sta_id])
659c6bae216SMordechay Goodstein 		mvmsta->avg_energy = energy[sta_id];
660853f4954SMordechay Goodstein 
661853f4954SMordechay Goodstein }
662853f4954SMordechay Goodstein 
663853f4954SMordechay Goodstein static void
664853f4954SMordechay Goodstein iwl_mvm_update_tcm_from_stats(struct iwl_mvm *mvm, __le32 *air_time_le,
665853f4954SMordechay Goodstein 			      __le32 *rx_bytes_le)
666853f4954SMordechay Goodstein {
667853f4954SMordechay Goodstein 	int i;
668853f4954SMordechay Goodstein 
669853f4954SMordechay Goodstein 	spin_lock(&mvm->tcm.lock);
670853f4954SMordechay Goodstein 	for (i = 0; i < NUM_MAC_INDEX_DRIVER; i++) {
671853f4954SMordechay Goodstein 		struct iwl_mvm_tcm_mac *mdata = &mvm->tcm.data[i];
672853f4954SMordechay Goodstein 		u32 rx_bytes = le32_to_cpu(rx_bytes_le[i]);
673853f4954SMordechay Goodstein 		u32 airtime = le32_to_cpu(air_time_le[i]);
674853f4954SMordechay Goodstein 
675853f4954SMordechay Goodstein 		mdata->rx.airtime += airtime;
676853f4954SMordechay Goodstein 		mdata->uapsd_nonagg_detect.rx_bytes += rx_bytes;
677853f4954SMordechay Goodstein 		if (airtime) {
678853f4954SMordechay Goodstein 			/* re-init every time to store rate from FW */
679853f4954SMordechay Goodstein 			ewma_rate_init(&mdata->uapsd_nonagg_detect.rate);
680853f4954SMordechay Goodstein 			ewma_rate_add(&mdata->uapsd_nonagg_detect.rate,
681853f4954SMordechay Goodstein 				      rx_bytes * 8 / airtime);
682853f4954SMordechay Goodstein 		}
683853f4954SMordechay Goodstein 	}
684853f4954SMordechay Goodstein 	spin_unlock(&mvm->tcm.lock);
685853f4954SMordechay Goodstein }
686853f4954SMordechay Goodstein 
687853f4954SMordechay Goodstein static void
688853f4954SMordechay Goodstein iwl_mvm_handle_rx_statistics_tlv(struct iwl_mvm *mvm,
689853f4954SMordechay Goodstein 				 struct iwl_rx_packet *pkt)
690853f4954SMordechay Goodstein {
691853f4954SMordechay Goodstein 	struct iwl_mvm_stat_data data = {
692853f4954SMordechay Goodstein 		.mvm = mvm,
693853f4954SMordechay Goodstein 	};
694853f4954SMordechay Goodstein 	u8 beacon_average_energy[MAC_INDEX_AUX];
695853f4954SMordechay Goodstein 	u8 average_energy[IWL_MVM_STATION_COUNT_MAX];
696853f4954SMordechay Goodstein 	struct iwl_statistics_operational_ntfy *stats;
697853f4954SMordechay Goodstein 	int expected_size;
698853f4954SMordechay Goodstein 	__le32 flags;
699853f4954SMordechay Goodstein 	int i;
700853f4954SMordechay Goodstein 
701853f4954SMordechay Goodstein 	expected_size = sizeof(*stats);
702853f4954SMordechay Goodstein 	if (WARN_ONCE(iwl_rx_packet_payload_len(pkt) < expected_size,
703853f4954SMordechay Goodstein 		      "received invalid statistics size (%d)!, expected_size: %d\n",
704853f4954SMordechay Goodstein 		      iwl_rx_packet_payload_len(pkt), expected_size))
705853f4954SMordechay Goodstein 		return;
706853f4954SMordechay Goodstein 
707853f4954SMordechay Goodstein 	stats = (void *)&pkt->data;
708853f4954SMordechay Goodstein 
709853f4954SMordechay Goodstein 	if (WARN_ONCE(stats->hdr.type != FW_STATISTICS_OPERATIONAL ||
710c0f46dcaSMordechay Goodstein 		      stats->hdr.version !=
711c0f46dcaSMordechay Goodstein 		      iwl_fw_lookup_notif_ver(mvm->fw, LONG_GROUP, STATISTICS_CMD, 0),
712853f4954SMordechay Goodstein 		      "received unsupported hdr type %d, version %d\n",
713853f4954SMordechay Goodstein 		      stats->hdr.type, stats->hdr.version))
714853f4954SMordechay Goodstein 		return;
715853f4954SMordechay Goodstein 
716853f4954SMordechay Goodstein 	flags = stats->flags;
717853f4954SMordechay Goodstein 	mvm->radio_stats.rx_time = le64_to_cpu(stats->rx_time);
718853f4954SMordechay Goodstein 	mvm->radio_stats.tx_time = le64_to_cpu(stats->tx_time);
719853f4954SMordechay Goodstein 	mvm->radio_stats.on_time_rf = le64_to_cpu(stats->on_time_rf);
720853f4954SMordechay Goodstein 	mvm->radio_stats.on_time_scan = le64_to_cpu(stats->on_time_scan);
721853f4954SMordechay Goodstein 
722853f4954SMordechay Goodstein 	iwl_mvm_rx_stats_check_trigger(mvm, pkt);
723853f4954SMordechay Goodstein 
724853f4954SMordechay Goodstein 	data.mac_id = stats->mac_id;
725853f4954SMordechay Goodstein 	data.beacon_filter_average_energy =
726853f4954SMordechay Goodstein 		le32_to_cpu(stats->beacon_filter_average_energy);
727853f4954SMordechay Goodstein 	data.flags = flags;
728853f4954SMordechay Goodstein 	data.beacon_counter = stats->beacon_counter;
729853f4954SMordechay Goodstein 	for (i = 0; i < ARRAY_SIZE(beacon_average_energy); i++)
730853f4954SMordechay Goodstein 		beacon_average_energy[i] =
731853f4954SMordechay Goodstein 			le32_to_cpu(stats->beacon_average_energy[i]);
732853f4954SMordechay Goodstein 
733853f4954SMordechay Goodstein 	data.beacon_average_energy = beacon_average_energy;
734853f4954SMordechay Goodstein 
735853f4954SMordechay Goodstein 	ieee80211_iterate_active_interfaces(mvm->hw,
736853f4954SMordechay Goodstein 					    IEEE80211_IFACE_ITER_NORMAL,
737853f4954SMordechay Goodstein 					    iwl_mvm_stat_iterator,
738853f4954SMordechay Goodstein 					    &data);
739853f4954SMordechay Goodstein 
740853f4954SMordechay Goodstein 	for (i = 0; i < ARRAY_SIZE(average_energy); i++)
741853f4954SMordechay Goodstein 		average_energy[i] = le32_to_cpu(stats->average_energy[i]);
742c6bae216SMordechay Goodstein 	ieee80211_iterate_stations_atomic(mvm->hw, iwl_mvm_stats_energy_iter,
743c6bae216SMordechay Goodstein 					  average_energy);
744853f4954SMordechay Goodstein 	/*
745853f4954SMordechay Goodstein 	 * Don't update in case the statistics are not cleared, since
746853f4954SMordechay Goodstein 	 * we will end up counting twice the same airtime, once in TCM
747853f4954SMordechay Goodstein 	 * request and once in statistics notification.
748853f4954SMordechay Goodstein 	 */
749853f4954SMordechay Goodstein 	if (le32_to_cpu(flags) & IWL_STATISTICS_REPLY_FLG_CLEAR)
750853f4954SMordechay Goodstein 		iwl_mvm_update_tcm_from_stats(mvm, stats->air_time,
751853f4954SMordechay Goodstein 					      stats->rx_bytes);
752853f4954SMordechay Goodstein }
753853f4954SMordechay Goodstein 
754e705c121SKalle Valo void iwl_mvm_handle_rx_statistics(struct iwl_mvm *mvm,
755e705c121SKalle Valo 				  struct iwl_rx_packet *pkt)
756e705c121SKalle Valo {
757e705c121SKalle Valo 	struct iwl_mvm_stat_data data = {
758e705c121SKalle Valo 		.mvm = mvm,
759e705c121SKalle Valo 	};
760853f4954SMordechay Goodstein 	__le32 *bytes, *air_time, flags;
7610e7ac018SSara Sharon 	int expected_size;
762cfbeb598SGoodstein, Mordechay 	u8 *energy;
763853f4954SMordechay Goodstein 
764853f4954SMordechay Goodstein 	/* From ver 14 and up we use TLV statistics format */
765853f4954SMordechay Goodstein 	if (iwl_fw_lookup_notif_ver(mvm->fw, LONG_GROUP,
766853f4954SMordechay Goodstein 				    STATISTICS_CMD, 0) >= 14)
767853f4954SMordechay Goodstein 		return iwl_mvm_handle_rx_statistics_tlv(mvm, pkt);
7680e7ac018SSara Sharon 
769678d9b6dSLiad Kaufman 	if (!iwl_mvm_has_new_rx_stats_api(mvm)) {
770678d9b6dSLiad Kaufman 		if (iwl_mvm_has_new_rx_api(mvm))
7710e7ac018SSara Sharon 			expected_size = sizeof(struct iwl_notif_statistics_v11);
7720e7ac018SSara Sharon 		else
7730e7ac018SSara Sharon 			expected_size = sizeof(struct iwl_notif_statistics_v10);
774678d9b6dSLiad Kaufman 	} else {
775606b9ab6SEmmanuel Grumbach 		expected_size = sizeof(struct iwl_notif_statistics);
776678d9b6dSLiad Kaufman 	}
777e705c121SKalle Valo 
77842fa5e0eSJohannes Berg 	if (WARN_ONCE(iwl_rx_packet_payload_len(pkt) != expected_size,
77942fa5e0eSJohannes Berg 		      "received invalid statistics size (%d)!\n",
78042fa5e0eSJohannes Berg 		      iwl_rx_packet_payload_len(pkt)))
7816a90f85aSLuca Coelho 		return;
782e705c121SKalle Valo 
783678d9b6dSLiad Kaufman 	if (!iwl_mvm_has_new_rx_stats_api(mvm)) {
784678d9b6dSLiad Kaufman 		struct iwl_notif_statistics_v11 *stats = (void *)&pkt->data;
785678d9b6dSLiad Kaufman 
786e705c121SKalle Valo 		data.mac_id = stats->rx.general.mac_id;
787e705c121SKalle Valo 		data.beacon_filter_average_energy =
7880e7ac018SSara Sharon 			stats->general.common.beacon_filter_average_energy;
789e705c121SKalle Valo 
790678d9b6dSLiad Kaufman 		mvm->rx_stats_v3 = stats->rx;
791e705c121SKalle Valo 
792678d9b6dSLiad Kaufman 		mvm->radio_stats.rx_time =
793678d9b6dSLiad Kaufman 			le64_to_cpu(stats->general.common.rx_time);
794678d9b6dSLiad Kaufman 		mvm->radio_stats.tx_time =
795678d9b6dSLiad Kaufman 			le64_to_cpu(stats->general.common.tx_time);
796e705c121SKalle Valo 		mvm->radio_stats.on_time_rf =
7970e7ac018SSara Sharon 			le64_to_cpu(stats->general.common.on_time_rf);
798e705c121SKalle Valo 		mvm->radio_stats.on_time_scan =
7990e7ac018SSara Sharon 			le64_to_cpu(stats->general.common.on_time_scan);
800e705c121SKalle Valo 
801853f4954SMordechay Goodstein 		data.beacon_counter = stats->general.beacon_counter;
802853f4954SMordechay Goodstein 		data.beacon_average_energy =
803853f4954SMordechay Goodstein 			stats->general.beacon_average_energy;
804678d9b6dSLiad Kaufman 		flags = stats->flag;
805678d9b6dSLiad Kaufman 	} else {
806606b9ab6SEmmanuel Grumbach 		struct iwl_notif_statistics *stats = (void *)&pkt->data;
807678d9b6dSLiad Kaufman 
808678d9b6dSLiad Kaufman 		data.mac_id = stats->rx.general.mac_id;
809678d9b6dSLiad Kaufman 		data.beacon_filter_average_energy =
810678d9b6dSLiad Kaufman 			stats->general.common.beacon_filter_average_energy;
811678d9b6dSLiad Kaufman 
812678d9b6dSLiad Kaufman 		mvm->rx_stats = stats->rx;
813678d9b6dSLiad Kaufman 
814678d9b6dSLiad Kaufman 		mvm->radio_stats.rx_time =
815678d9b6dSLiad Kaufman 			le64_to_cpu(stats->general.common.rx_time);
816678d9b6dSLiad Kaufman 		mvm->radio_stats.tx_time =
817678d9b6dSLiad Kaufman 			le64_to_cpu(stats->general.common.tx_time);
818678d9b6dSLiad Kaufman 		mvm->radio_stats.on_time_rf =
819678d9b6dSLiad Kaufman 			le64_to_cpu(stats->general.common.on_time_rf);
820678d9b6dSLiad Kaufman 		mvm->radio_stats.on_time_scan =
821678d9b6dSLiad Kaufman 			le64_to_cpu(stats->general.common.on_time_scan);
822678d9b6dSLiad Kaufman 
823853f4954SMordechay Goodstein 		data.beacon_counter = stats->general.beacon_counter;
824853f4954SMordechay Goodstein 		data.beacon_average_energy =
825853f4954SMordechay Goodstein 			stats->general.beacon_average_energy;
826678d9b6dSLiad Kaufman 		flags = stats->flag;
827678d9b6dSLiad Kaufman 	}
828290d5e49SMordechay Goodstein 	data.flags = flags;
829678d9b6dSLiad Kaufman 
830cfbeb598SGoodstein, Mordechay 	iwl_mvm_rx_stats_check_trigger(mvm, pkt);
831cfbeb598SGoodstein, Mordechay 
832cfbeb598SGoodstein, Mordechay 	ieee80211_iterate_active_interfaces(mvm->hw,
833cfbeb598SGoodstein, Mordechay 					    IEEE80211_IFACE_ITER_NORMAL,
834cfbeb598SGoodstein, Mordechay 					    iwl_mvm_stat_iterator,
835cfbeb598SGoodstein, Mordechay 					    &data);
836cfbeb598SGoodstein, Mordechay 
837cfbeb598SGoodstein, Mordechay 	if (!iwl_mvm_has_new_rx_api(mvm))
838cfbeb598SGoodstein, Mordechay 		return;
839988b5968SSara Sharon 
840678d9b6dSLiad Kaufman 	if (!iwl_mvm_has_new_rx_stats_api(mvm)) {
841678d9b6dSLiad Kaufman 		struct iwl_notif_statistics_v11 *v11 = (void *)&pkt->data;
8420e7ac018SSara Sharon 
8430e7ac018SSara Sharon 		energy = (void *)&v11->load_stats.avg_energy;
844b0ffe455SJohannes Berg 		bytes = (void *)&v11->load_stats.byte_count;
8450e7ac018SSara Sharon 		air_time = (void *)&v11->load_stats.air_time;
8460e7ac018SSara Sharon 	} else {
847606b9ab6SEmmanuel Grumbach 		struct iwl_notif_statistics *stats = (void *)&pkt->data;
848678d9b6dSLiad Kaufman 
8490e7ac018SSara Sharon 		energy = (void *)&stats->load_stats.avg_energy;
850b0ffe455SJohannes Berg 		bytes = (void *)&stats->load_stats.byte_count;
8510e7ac018SSara Sharon 		air_time = (void *)&stats->load_stats.air_time;
8520e7ac018SSara Sharon 	}
853c6bae216SMordechay Goodstein 	ieee80211_iterate_stations_atomic(mvm->hw, iwl_mvm_stats_energy_iter,
854c6bae216SMordechay Goodstein 					  energy);
8557d9d0d56SLuca Coelho 
8567d9d0d56SLuca Coelho 	/*
8577d9d0d56SLuca Coelho 	 * Don't update in case the statistics are not cleared, since
8587d9d0d56SLuca Coelho 	 * we will end up counting twice the same airtime, once in TCM
8597d9d0d56SLuca Coelho 	 * request and once in statistics notification.
8607d9d0d56SLuca Coelho 	 */
861853f4954SMordechay Goodstein 	if (le32_to_cpu(flags) & IWL_STATISTICS_REPLY_FLG_CLEAR)
862853f4954SMordechay Goodstein 		iwl_mvm_update_tcm_from_stats(mvm, air_time, bytes);
8637d9d0d56SLuca Coelho 
864e705c121SKalle Valo }
865e705c121SKalle Valo 
866e705c121SKalle Valo void iwl_mvm_rx_statistics(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
867e705c121SKalle Valo {
868e705c121SKalle Valo 	iwl_mvm_handle_rx_statistics(mvm, rxb_addr(rxb));
869e705c121SKalle Valo }
8703af512d6SSara Sharon 
8713af512d6SSara Sharon void iwl_mvm_window_status_notif(struct iwl_mvm *mvm,
8723af512d6SSara Sharon 				 struct iwl_rx_cmd_buffer *rxb)
8733af512d6SSara Sharon {
8743af512d6SSara Sharon 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
8753af512d6SSara Sharon 	struct iwl_ba_window_status_notif *notif = (void *)pkt->data;
8763af512d6SSara Sharon 	int i;
8773af512d6SSara Sharon 	u32 pkt_len = iwl_rx_packet_payload_len(pkt);
8783af512d6SSara Sharon 
8793af512d6SSara Sharon 	if (WARN_ONCE(pkt_len != sizeof(*notif),
8803af512d6SSara Sharon 		      "Received window status notification of wrong size (%u)\n",
8813af512d6SSara Sharon 		      pkt_len))
8823af512d6SSara Sharon 		return;
8833af512d6SSara Sharon 
8843af512d6SSara Sharon 	rcu_read_lock();
8853af512d6SSara Sharon 	for (i = 0; i < BA_WINDOW_STREAMS_MAX; i++) {
8863af512d6SSara Sharon 		struct ieee80211_sta *sta;
8873af512d6SSara Sharon 		u8 sta_id, tid;
8883af512d6SSara Sharon 		u64 bitmap;
8893af512d6SSara Sharon 		u32 ssn;
8903af512d6SSara Sharon 		u16 ratid;
8913af512d6SSara Sharon 		u16 received_mpdu;
8923af512d6SSara Sharon 
8933af512d6SSara Sharon 		ratid = le16_to_cpu(notif->ra_tid[i]);
8943af512d6SSara Sharon 		/* check that this TID is valid */
8953af512d6SSara Sharon 		if (!(ratid & BA_WINDOW_STATUS_VALID_MSK))
8963af512d6SSara Sharon 			continue;
8973af512d6SSara Sharon 
8983af512d6SSara Sharon 		received_mpdu = le16_to_cpu(notif->mpdu_rx_count[i]);
8993af512d6SSara Sharon 		if (received_mpdu == 0)
9003af512d6SSara Sharon 			continue;
9013af512d6SSara Sharon 
9023af512d6SSara Sharon 		tid = ratid & BA_WINDOW_STATUS_TID_MSK;
9033af512d6SSara Sharon 		/* get the station */
9043af512d6SSara Sharon 		sta_id = (ratid & BA_WINDOW_STATUS_STA_ID_MSK)
9053af512d6SSara Sharon 			 >> BA_WINDOW_STATUS_STA_ID_POS;
9063af512d6SSara Sharon 		sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
9073af512d6SSara Sharon 		if (IS_ERR_OR_NULL(sta))
9083af512d6SSara Sharon 			continue;
9093af512d6SSara Sharon 		bitmap = le64_to_cpu(notif->bitmap[i]);
9103af512d6SSara Sharon 		ssn = le32_to_cpu(notif->start_seq_num[i]);
9113af512d6SSara Sharon 
9123af512d6SSara Sharon 		/* update mac80211 with the bitmap for the reordering buffer */
9133af512d6SSara Sharon 		ieee80211_mark_rx_ba_filtered_frames(sta, tid, ssn, bitmap,
9143af512d6SSara Sharon 						     received_mpdu);
9153af512d6SSara Sharon 	}
9163af512d6SSara Sharon 	rcu_read_unlock();
9173af512d6SSara Sharon }
918