xref: /linux/drivers/net/wireless/intel/iwlwifi/mvm/rx.c (revision 4c59ff5a9a9c54cc26c807dc2fa6933f7e9fa4ef)
1e705c121SKalle Valo /******************************************************************************
2e705c121SKalle Valo  *
3e705c121SKalle Valo  * This file is provided under a dual BSD/GPLv2 license.  When using or
4e705c121SKalle Valo  * redistributing this file, you may do so under either license.
5e705c121SKalle Valo  *
6e705c121SKalle Valo  * GPL LICENSE SUMMARY
7e705c121SKalle Valo  *
8e705c121SKalle Valo  * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
9e705c121SKalle Valo  * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
10cfbeb598SGoodstein, Mordechay  * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
11e705c121SKalle Valo  *
12e705c121SKalle Valo  * This program is free software; you can redistribute it and/or modify
13e705c121SKalle Valo  * it under the terms of version 2 of the GNU General Public License as
14e705c121SKalle Valo  * published by the Free Software Foundation.
15e705c121SKalle Valo  *
16e705c121SKalle Valo  * This program is distributed in the hope that it will be useful, but
17e705c121SKalle Valo  * WITHOUT ANY WARRANTY; without even the implied warranty of
18e705c121SKalle Valo  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19e705c121SKalle Valo  * General Public License for more details.
20e705c121SKalle Valo  *
21e705c121SKalle Valo  * You should have received a copy of the GNU General Public License
22e705c121SKalle Valo  * along with this program; if not, write to the Free Software
23e705c121SKalle Valo  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
24e705c121SKalle Valo  * USA
25e705c121SKalle Valo  *
26e705c121SKalle Valo  * The full GNU General Public License is included in this distribution
27e705c121SKalle Valo  * in the file called COPYING.
28e705c121SKalle Valo  *
29e705c121SKalle Valo  * Contact Information:
30cb2f8277SEmmanuel Grumbach  *  Intel Linux Wireless <linuxwifi@intel.com>
31e705c121SKalle Valo  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
32e705c121SKalle Valo  *
33e705c121SKalle Valo  * BSD LICENSE
34e705c121SKalle Valo  *
35e705c121SKalle Valo  * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
36e705c121SKalle Valo  * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
37e705c121SKalle Valo  * All rights reserved.
38e705c121SKalle Valo  *
39e705c121SKalle Valo  * Redistribution and use in source and binary forms, with or without
40e705c121SKalle Valo  * modification, are permitted provided that the following conditions
41e705c121SKalle Valo  * are met:
42e705c121SKalle Valo  *
43e705c121SKalle Valo  *  * Redistributions of source code must retain the above copyright
44e705c121SKalle Valo  *    notice, this list of conditions and the following disclaimer.
45e705c121SKalle Valo  *  * Redistributions in binary form must reproduce the above copyright
46e705c121SKalle Valo  *    notice, this list of conditions and the following disclaimer in
47e705c121SKalle Valo  *    the documentation and/or other materials provided with the
48e705c121SKalle Valo  *    distribution.
49e705c121SKalle Valo  *  * Neither the name Intel Corporation nor the names of its
50e705c121SKalle Valo  *    contributors may be used to endorse or promote products derived
51e705c121SKalle Valo  *    from this software without specific prior written permission.
52e705c121SKalle Valo  *
53e705c121SKalle Valo  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
54e705c121SKalle Valo  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
55e705c121SKalle Valo  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
56e705c121SKalle Valo  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
57e705c121SKalle Valo  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
58e705c121SKalle Valo  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
59e705c121SKalle Valo  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
60e705c121SKalle Valo  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
61e705c121SKalle Valo  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
62e705c121SKalle Valo  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
63e705c121SKalle Valo  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
64e705c121SKalle Valo  *****************************************************************************/
65a399f980SJohannes Berg #include <linux/etherdevice.h>
66e705c121SKalle Valo #include <linux/skbuff.h>
67e705c121SKalle Valo #include "iwl-trans.h"
68e705c121SKalle Valo #include "mvm.h"
69e705c121SKalle Valo #include "fw-api.h"
70e705c121SKalle Valo 
71e705c121SKalle Valo /*
72e705c121SKalle Valo  * iwl_mvm_rx_rx_phy_cmd - REPLY_RX_PHY_CMD handler
73e705c121SKalle Valo  *
74e705c121SKalle Valo  * Copies the phy information in mvm->last_phy_info, it will be used when the
75e705c121SKalle Valo  * actual data will come from the fw in the next packet.
76e705c121SKalle Valo  */
77e705c121SKalle Valo void iwl_mvm_rx_rx_phy_cmd(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
78e705c121SKalle Valo {
79e705c121SKalle Valo 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
80e705c121SKalle Valo 
81e705c121SKalle Valo 	memcpy(&mvm->last_phy_info, pkt->data, sizeof(mvm->last_phy_info));
82e705c121SKalle Valo 	mvm->ampdu_ref++;
83e705c121SKalle Valo 
84e705c121SKalle Valo #ifdef CONFIG_IWLWIFI_DEBUGFS
85e705c121SKalle Valo 	if (mvm->last_phy_info.phy_flags & cpu_to_le16(RX_RES_PHY_FLAGS_AGG)) {
86e705c121SKalle Valo 		spin_lock(&mvm->drv_stats_lock);
87e705c121SKalle Valo 		mvm->drv_rx_stats.ampdu_count++;
88e705c121SKalle Valo 		spin_unlock(&mvm->drv_stats_lock);
89e705c121SKalle Valo 	}
90e705c121SKalle Valo #endif
91e705c121SKalle Valo }
92e705c121SKalle Valo 
93e705c121SKalle Valo /*
94e705c121SKalle Valo  * iwl_mvm_pass_packet_to_mac80211 - builds the packet for mac80211
95e705c121SKalle Valo  *
96e705c121SKalle Valo  * Adds the rxb to a new skb and give it to mac80211
97e705c121SKalle Valo  */
98e705c121SKalle Valo static void iwl_mvm_pass_packet_to_mac80211(struct iwl_mvm *mvm,
9943ec72b7SJohannes Berg 					    struct ieee80211_sta *sta,
100e705c121SKalle Valo 					    struct napi_struct *napi,
101e705c121SKalle Valo 					    struct sk_buff *skb,
102e705c121SKalle Valo 					    struct ieee80211_hdr *hdr, u16 len,
103bdbc58abSDan Carpenter 					    u8 crypt_len,
104e705c121SKalle Valo 					    struct iwl_rx_cmd_buffer *rxb)
105e705c121SKalle Valo {
1065cddd05cSJohannes Berg 	unsigned int hdrlen = ieee80211_hdrlen(hdr->frame_control);
1075cddd05cSJohannes Berg 	unsigned int fraglen;
1085cddd05cSJohannes Berg 
1095cddd05cSJohannes Berg 	/*
1105cddd05cSJohannes Berg 	 * The 'hdrlen' (plus the 8 bytes for the SNAP and the crypt_len,
1115cddd05cSJohannes Berg 	 * but those are all multiples of 4 long) all goes away, but we
1125cddd05cSJohannes Berg 	 * want the *end* of it, which is going to be the start of the IP
1135cddd05cSJohannes Berg 	 * header, to be aligned when it gets pulled in.
1145cddd05cSJohannes Berg 	 * The beginning of the skb->data is aligned on at least a 4-byte
1155cddd05cSJohannes Berg 	 * boundary after allocation. Everything here is aligned at least
1165cddd05cSJohannes Berg 	 * on a 2-byte boundary so we can just take hdrlen & 3 and pad by
1175cddd05cSJohannes Berg 	 * the result.
1185cddd05cSJohannes Berg 	 */
1195cddd05cSJohannes Berg 	skb_reserve(skb, hdrlen & 3);
120e705c121SKalle Valo 
121e705c121SKalle Valo 	/* If frame is small enough to fit in skb->head, pull it completely.
122e705c121SKalle Valo 	 * If not, only pull ieee80211_hdr (including crypto if present, and
123e705c121SKalle Valo 	 * an additional 8 bytes for SNAP/ethertype, see below) so that
124e705c121SKalle Valo 	 * splice() or TCP coalesce are more efficient.
125e705c121SKalle Valo 	 *
126e705c121SKalle Valo 	 * Since, in addition, ieee80211_data_to_8023() always pull in at
127e705c121SKalle Valo 	 * least 8 bytes (possibly more for mesh) we can do the same here
128e705c121SKalle Valo 	 * to save the cost of doing it later. That still doesn't pull in
129e705c121SKalle Valo 	 * the actual IP header since the typical case has a SNAP header.
130e705c121SKalle Valo 	 * If the latter changes (there are efforts in the standards group
131e705c121SKalle Valo 	 * to do so) we should revisit this and ieee80211_data_to_8023().
132e705c121SKalle Valo 	 */
1335cddd05cSJohannes Berg 	hdrlen = (len <= skb_tailroom(skb)) ? len : hdrlen + crypt_len + 8;
134e705c121SKalle Valo 
13559ae1d12SJohannes Berg 	skb_put_data(skb, hdr, hdrlen);
136e705c121SKalle Valo 	fraglen = len - hdrlen;
137e705c121SKalle Valo 
138e705c121SKalle Valo 	if (fraglen) {
139e705c121SKalle Valo 		int offset = (void *)hdr + hdrlen -
140e705c121SKalle Valo 			     rxb_addr(rxb) + rxb_offset(rxb);
141e705c121SKalle Valo 
142e705c121SKalle Valo 		skb_add_rx_frag(skb, 0, rxb_steal_page(rxb), offset,
143e705c121SKalle Valo 				fraglen, rxb->truesize);
144e705c121SKalle Valo 	}
145e705c121SKalle Valo 
14643ec72b7SJohannes Berg 	ieee80211_rx_napi(mvm->hw, sta, skb, napi);
147e705c121SKalle Valo }
148e705c121SKalle Valo 
149e705c121SKalle Valo /*
150e705c121SKalle Valo  * iwl_mvm_get_signal_strength - use new rx PHY INFO API
151e705c121SKalle Valo  * values are reported by the fw as positive values - need to negate
152e705c121SKalle Valo  * to obtain their dBM.  Account for missing antennas by replacing 0
153e705c121SKalle Valo  * values by -256dBm: practically 0 power and a non-feasible 8 bit value.
154e705c121SKalle Valo  */
155e705c121SKalle Valo static void iwl_mvm_get_signal_strength(struct iwl_mvm *mvm,
156e705c121SKalle Valo 					struct iwl_rx_phy_info *phy_info,
157e705c121SKalle Valo 					struct ieee80211_rx_status *rx_status)
158e705c121SKalle Valo {
159e705c121SKalle Valo 	int energy_a, energy_b, energy_c, max_energy;
160e705c121SKalle Valo 	u32 val;
161e705c121SKalle Valo 
162e705c121SKalle Valo 	val =
163e705c121SKalle Valo 	    le32_to_cpu(phy_info->non_cfg_phy[IWL_RX_INFO_ENERGY_ANT_ABC_IDX]);
164e705c121SKalle Valo 	energy_a = (val & IWL_RX_INFO_ENERGY_ANT_A_MSK) >>
165e705c121SKalle Valo 						IWL_RX_INFO_ENERGY_ANT_A_POS;
166e705c121SKalle Valo 	energy_a = energy_a ? -energy_a : S8_MIN;
167e705c121SKalle Valo 	energy_b = (val & IWL_RX_INFO_ENERGY_ANT_B_MSK) >>
168e705c121SKalle Valo 						IWL_RX_INFO_ENERGY_ANT_B_POS;
169e705c121SKalle Valo 	energy_b = energy_b ? -energy_b : S8_MIN;
170e705c121SKalle Valo 	energy_c = (val & IWL_RX_INFO_ENERGY_ANT_C_MSK) >>
171e705c121SKalle Valo 						IWL_RX_INFO_ENERGY_ANT_C_POS;
172e705c121SKalle Valo 	energy_c = energy_c ? -energy_c : S8_MIN;
173e705c121SKalle Valo 	max_energy = max(energy_a, energy_b);
174e705c121SKalle Valo 	max_energy = max(max_energy, energy_c);
175e705c121SKalle Valo 
176e705c121SKalle Valo 	IWL_DEBUG_STATS(mvm, "energy In A %d B %d C %d , and max %d\n",
177e705c121SKalle Valo 			energy_a, energy_b, energy_c, max_energy);
178e705c121SKalle Valo 
179e705c121SKalle Valo 	rx_status->signal = max_energy;
180e705c121SKalle Valo 	rx_status->chains = (le16_to_cpu(phy_info->phy_flags) &
181e705c121SKalle Valo 				RX_RES_PHY_FLAGS_ANTENNA)
182e705c121SKalle Valo 					>> RX_RES_PHY_FLAGS_ANTENNA_POS;
183e705c121SKalle Valo 	rx_status->chain_signal[0] = energy_a;
184e705c121SKalle Valo 	rx_status->chain_signal[1] = energy_b;
185e705c121SKalle Valo 	rx_status->chain_signal[2] = energy_c;
186e705c121SKalle Valo }
187e705c121SKalle Valo 
188e705c121SKalle Valo /*
189e705c121SKalle Valo  * iwl_mvm_set_mac80211_rx_flag - translate fw status to mac80211 format
190e705c121SKalle Valo  * @mvm: the mvm object
191e705c121SKalle Valo  * @hdr: 80211 header
192e705c121SKalle Valo  * @stats: status in mac80211's format
193e705c121SKalle Valo  * @rx_pkt_status: status coming from fw
194e705c121SKalle Valo  *
195e705c121SKalle Valo  * returns non 0 value if the packet should be dropped
196e705c121SKalle Valo  */
197e705c121SKalle Valo static u32 iwl_mvm_set_mac80211_rx_flag(struct iwl_mvm *mvm,
198e705c121SKalle Valo 					struct ieee80211_hdr *hdr,
199e705c121SKalle Valo 					struct ieee80211_rx_status *stats,
200e705c121SKalle Valo 					u32 rx_pkt_status,
201e705c121SKalle Valo 					u8 *crypt_len)
202e705c121SKalle Valo {
203e705c121SKalle Valo 	if (!ieee80211_has_protected(hdr->frame_control) ||
204e705c121SKalle Valo 	    (rx_pkt_status & RX_MPDU_RES_STATUS_SEC_ENC_MSK) ==
205e705c121SKalle Valo 			     RX_MPDU_RES_STATUS_SEC_NO_ENC)
206e705c121SKalle Valo 		return 0;
207e705c121SKalle Valo 
208e705c121SKalle Valo 	/* packet was encrypted with unknown alg */
209e705c121SKalle Valo 	if ((rx_pkt_status & RX_MPDU_RES_STATUS_SEC_ENC_MSK) ==
210e705c121SKalle Valo 					RX_MPDU_RES_STATUS_SEC_ENC_ERR)
211e705c121SKalle Valo 		return 0;
212e705c121SKalle Valo 
213e705c121SKalle Valo 	switch (rx_pkt_status & RX_MPDU_RES_STATUS_SEC_ENC_MSK) {
214e705c121SKalle Valo 	case RX_MPDU_RES_STATUS_SEC_CCM_ENC:
215e705c121SKalle Valo 		/* alg is CCM: check MIC only */
216e705c121SKalle Valo 		if (!(rx_pkt_status & RX_MPDU_RES_STATUS_MIC_OK))
217e705c121SKalle Valo 			return -1;
218e705c121SKalle Valo 
219e705c121SKalle Valo 		stats->flag |= RX_FLAG_DECRYPTED;
220e705c121SKalle Valo 		*crypt_len = IEEE80211_CCMP_HDR_LEN;
221e705c121SKalle Valo 		return 0;
222e705c121SKalle Valo 
223e705c121SKalle Valo 	case RX_MPDU_RES_STATUS_SEC_TKIP_ENC:
224e705c121SKalle Valo 		/* Don't drop the frame and decrypt it in SW */
225e705c121SKalle Valo 		if (!(rx_pkt_status & RX_MPDU_RES_STATUS_TTAK_OK))
226e705c121SKalle Valo 			return 0;
227e705c121SKalle Valo 		*crypt_len = IEEE80211_TKIP_IV_LEN;
228e705c121SKalle Valo 		/* fall through if TTAK OK */
229e705c121SKalle Valo 
230e705c121SKalle Valo 	case RX_MPDU_RES_STATUS_SEC_WEP_ENC:
231e705c121SKalle Valo 		if (!(rx_pkt_status & RX_MPDU_RES_STATUS_ICV_OK))
232e705c121SKalle Valo 			return -1;
233e705c121SKalle Valo 
234e705c121SKalle Valo 		stats->flag |= RX_FLAG_DECRYPTED;
235e705c121SKalle Valo 		if ((rx_pkt_status & RX_MPDU_RES_STATUS_SEC_ENC_MSK) ==
236e705c121SKalle Valo 				RX_MPDU_RES_STATUS_SEC_WEP_ENC)
237e705c121SKalle Valo 			*crypt_len = IEEE80211_WEP_IV_LEN;
238e705c121SKalle Valo 		return 0;
239e705c121SKalle Valo 
240e705c121SKalle Valo 	case RX_MPDU_RES_STATUS_SEC_EXT_ENC:
241e705c121SKalle Valo 		if (!(rx_pkt_status & RX_MPDU_RES_STATUS_MIC_OK))
242e705c121SKalle Valo 			return -1;
243e705c121SKalle Valo 		stats->flag |= RX_FLAG_DECRYPTED;
244e705c121SKalle Valo 		return 0;
245e705c121SKalle Valo 
246e705c121SKalle Valo 	default:
247baf41bc3SShaul Triebitz 		/* Expected in monitor (not having the keys) */
248baf41bc3SShaul Triebitz 		if (!mvm->monitor_on)
249e705c121SKalle Valo 			IWL_ERR(mvm, "Unhandled alg: 0x%x\n", rx_pkt_status);
250e705c121SKalle Valo 	}
251e705c121SKalle Valo 
252e705c121SKalle Valo 	return 0;
253e705c121SKalle Valo }
254e705c121SKalle Valo 
255e705c121SKalle Valo static void iwl_mvm_rx_csum(struct ieee80211_sta *sta,
256e705c121SKalle Valo 			    struct sk_buff *skb,
257e705c121SKalle Valo 			    u32 status)
258e705c121SKalle Valo {
259e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
260e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(mvmsta->vif);
261e705c121SKalle Valo 
262e705c121SKalle Valo 	if (mvmvif->features & NETIF_F_RXCSUM &&
263e705c121SKalle Valo 	    status & RX_MPDU_RES_STATUS_CSUM_DONE &&
264e705c121SKalle Valo 	    status & RX_MPDU_RES_STATUS_CSUM_OK)
265e705c121SKalle Valo 		skb->ip_summed = CHECKSUM_UNNECESSARY;
266e705c121SKalle Valo }
267e705c121SKalle Valo 
268e705c121SKalle Valo /*
269e705c121SKalle Valo  * iwl_mvm_rx_rx_mpdu - REPLY_RX_MPDU_CMD handler
270e705c121SKalle Valo  *
271e705c121SKalle Valo  * Handles the actual data of the Rx packet from the fw
272e705c121SKalle Valo  */
273e705c121SKalle Valo void iwl_mvm_rx_rx_mpdu(struct iwl_mvm *mvm, struct napi_struct *napi,
274e705c121SKalle Valo 			struct iwl_rx_cmd_buffer *rxb)
275e705c121SKalle Valo {
276e705c121SKalle Valo 	struct ieee80211_hdr *hdr;
277e705c121SKalle Valo 	struct ieee80211_rx_status *rx_status;
278e705c121SKalle Valo 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
279e705c121SKalle Valo 	struct iwl_rx_phy_info *phy_info;
280e705c121SKalle Valo 	struct iwl_rx_mpdu_res_start *rx_res;
281a399f980SJohannes Berg 	struct ieee80211_sta *sta = NULL;
282e705c121SKalle Valo 	struct sk_buff *skb;
283e705c121SKalle Valo 	u32 len;
284e705c121SKalle Valo 	u32 rate_n_flags;
285e705c121SKalle Valo 	u32 rx_pkt_status;
286e705c121SKalle Valo 	u8 crypt_len = 0;
28716e4dd8fSLuca Coelho 	bool take_ref;
288e705c121SKalle Valo 
289e705c121SKalle Valo 	phy_info = &mvm->last_phy_info;
290e705c121SKalle Valo 	rx_res = (struct iwl_rx_mpdu_res_start *)pkt->data;
291e705c121SKalle Valo 	hdr = (struct ieee80211_hdr *)(pkt->data + sizeof(*rx_res));
292e705c121SKalle Valo 	len = le16_to_cpu(rx_res->byte_count);
293e705c121SKalle Valo 	rx_pkt_status = le32_to_cpup((__le32 *)
294e705c121SKalle Valo 		(pkt->data + sizeof(*rx_res) + len));
295e705c121SKalle Valo 
296e705c121SKalle Valo 	/* Dont use dev_alloc_skb(), we'll have enough headroom once
297e705c121SKalle Valo 	 * ieee80211_hdr pulled.
298e705c121SKalle Valo 	 */
299e705c121SKalle Valo 	skb = alloc_skb(128, GFP_ATOMIC);
300e705c121SKalle Valo 	if (!skb) {
301e705c121SKalle Valo 		IWL_ERR(mvm, "alloc_skb failed\n");
302e705c121SKalle Valo 		return;
303e705c121SKalle Valo 	}
304e705c121SKalle Valo 
305e705c121SKalle Valo 	rx_status = IEEE80211_SKB_RXCB(skb);
306e705c121SKalle Valo 
307e705c121SKalle Valo 	/*
308e705c121SKalle Valo 	 * drop the packet if it has failed being decrypted by HW
309e705c121SKalle Valo 	 */
310e705c121SKalle Valo 	if (iwl_mvm_set_mac80211_rx_flag(mvm, hdr, rx_status, rx_pkt_status,
311e705c121SKalle Valo 					 &crypt_len)) {
312e705c121SKalle Valo 		IWL_DEBUG_DROP(mvm, "Bad decryption results 0x%08x\n",
313e705c121SKalle Valo 			       rx_pkt_status);
314e705c121SKalle Valo 		kfree_skb(skb);
315e705c121SKalle Valo 		return;
316e705c121SKalle Valo 	}
317e705c121SKalle Valo 
318e705c121SKalle Valo 	/*
319e705c121SKalle Valo 	 * Keep packets with CRC errors (and with overrun) for monitor mode
320e705c121SKalle Valo 	 * (otherwise the firmware discards them) but mark them as bad.
321e705c121SKalle Valo 	 */
322e705c121SKalle Valo 	if (!(rx_pkt_status & RX_MPDU_RES_STATUS_CRC_OK) ||
323e705c121SKalle Valo 	    !(rx_pkt_status & RX_MPDU_RES_STATUS_OVERRUN_OK)) {
324e705c121SKalle Valo 		IWL_DEBUG_RX(mvm, "Bad CRC or FIFO: 0x%08X.\n", rx_pkt_status);
325e705c121SKalle Valo 		rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
326e705c121SKalle Valo 	}
327e705c121SKalle Valo 
328e705c121SKalle Valo 	/* This will be used in several places later */
329e705c121SKalle Valo 	rate_n_flags = le32_to_cpu(phy_info->rate_n_flags);
330e705c121SKalle Valo 
331e705c121SKalle Valo 	/* rx_status carries information about the packet to mac80211 */
332e705c121SKalle Valo 	rx_status->mactime = le64_to_cpu(phy_info->timestamp);
333e705c121SKalle Valo 	rx_status->device_timestamp = le32_to_cpu(phy_info->system_timestamp);
334e705c121SKalle Valo 	rx_status->band =
335e705c121SKalle Valo 		(phy_info->phy_flags & cpu_to_le16(RX_RES_PHY_FLAGS_BAND_24)) ?
33657fbcce3SJohannes Berg 				NL80211_BAND_2GHZ : NL80211_BAND_5GHZ;
337e705c121SKalle Valo 	rx_status->freq =
338e705c121SKalle Valo 		ieee80211_channel_to_frequency(le16_to_cpu(phy_info->channel),
339e705c121SKalle Valo 					       rx_status->band);
34077fe7395SSara Sharon 
34177fe7395SSara Sharon 	/* TSF as indicated by the firmware  is at INA time */
34277fe7395SSara Sharon 	rx_status->flag |= RX_FLAG_MACTIME_PLCP_START;
343e705c121SKalle Valo 
344e705c121SKalle Valo 	iwl_mvm_get_signal_strength(mvm, phy_info, rx_status);
345e705c121SKalle Valo 
346e705c121SKalle Valo 	IWL_DEBUG_STATS_LIMIT(mvm, "Rssi %d, TSF %llu\n", rx_status->signal,
347e705c121SKalle Valo 			      (unsigned long long)rx_status->mactime);
348e705c121SKalle Valo 
349e705c121SKalle Valo 	rcu_read_lock();
350a399f980SJohannes Berg 	if (rx_pkt_status & RX_MPDU_RES_STATUS_SRC_STA_FOUND) {
351a399f980SJohannes Berg 		u32 id = rx_pkt_status & RX_MPDU_RES_STATUS_STA_ID_MSK;
352a399f980SJohannes Berg 
353a399f980SJohannes Berg 		id >>= RX_MDPU_RES_STATUS_STA_ID_SHIFT;
354a399f980SJohannes Berg 
3550ae98812SSara Sharon 		if (!WARN_ON_ONCE(id >= ARRAY_SIZE(mvm->fw_id_to_mac_id))) {
356a399f980SJohannes Berg 			sta = rcu_dereference(mvm->fw_id_to_mac_id[id]);
357a399f980SJohannes Berg 			if (IS_ERR(sta))
358a399f980SJohannes Berg 				sta = NULL;
359a399f980SJohannes Berg 		}
360a399f980SJohannes Berg 	} else if (!is_multicast_ether_addr(hdr->addr2)) {
361a399f980SJohannes Berg 		/* This is fine since we prevent two stations with the same
362a399f980SJohannes Berg 		 * address from being added.
363e705c121SKalle Valo 		 */
364a399f980SJohannes Berg 		sta = ieee80211_find_sta_by_ifaddr(mvm->hw, hdr->addr2, NULL);
365e705c121SKalle Valo 	}
366e705c121SKalle Valo 
367e705c121SKalle Valo 	if (sta) {
368e705c121SKalle Valo 		struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
369d3a108a4SAndrei Otcheretianski 		struct ieee80211_vif *tx_blocked_vif =
370d3a108a4SAndrei Otcheretianski 			rcu_dereference(mvm->csa_tx_blocked_vif);
371e705c121SKalle Valo 
372a399f980SJohannes Berg 		/* We have tx blocked stations (with CS bit). If we heard
373a399f980SJohannes Berg 		 * frames from a blocked station on a new channel we can
374a399f980SJohannes Berg 		 * TX to it again.
375a399f980SJohannes Berg 		 */
376d3a108a4SAndrei Otcheretianski 		if (unlikely(tx_blocked_vif) &&
377d3a108a4SAndrei Otcheretianski 		    mvmsta->vif == tx_blocked_vif) {
378d3a108a4SAndrei Otcheretianski 			struct iwl_mvm_vif *mvmvif =
379d3a108a4SAndrei Otcheretianski 				iwl_mvm_vif_from_mac80211(tx_blocked_vif);
380d3a108a4SAndrei Otcheretianski 
381d3a108a4SAndrei Otcheretianski 			if (mvmvif->csa_target_freq == rx_status->freq)
382d3a108a4SAndrei Otcheretianski 				iwl_mvm_sta_modify_disable_tx_ap(mvm, sta,
383d3a108a4SAndrei Otcheretianski 								 false);
384d3a108a4SAndrei Otcheretianski 		}
385a399f980SJohannes Berg 
386e705c121SKalle Valo 		rs_update_last_rssi(mvm, &mvmsta->lq_sta, rx_status);
387e705c121SKalle Valo 
388e705c121SKalle Valo 		if (iwl_fw_dbg_trigger_enabled(mvm->fw, FW_DBG_TRIGGER_RSSI) &&
389e705c121SKalle Valo 		    ieee80211_is_beacon(hdr->frame_control)) {
390e705c121SKalle Valo 			struct iwl_fw_dbg_trigger_tlv *trig;
391e705c121SKalle Valo 			struct iwl_fw_dbg_trigger_low_rssi *rssi_trig;
392e705c121SKalle Valo 			bool trig_check;
393e705c121SKalle Valo 			s32 rssi;
394e705c121SKalle Valo 
395e705c121SKalle Valo 			trig = iwl_fw_dbg_get_trigger(mvm->fw,
396e705c121SKalle Valo 						      FW_DBG_TRIGGER_RSSI);
397e705c121SKalle Valo 			rssi_trig = (void *)trig->data;
398e705c121SKalle Valo 			rssi = le32_to_cpu(rssi_trig->rssi);
399e705c121SKalle Valo 
400e705c121SKalle Valo 			trig_check =
4017174beb6SJohannes Berg 				iwl_fw_dbg_trigger_check_stop(&mvm->fwrt,
4027174beb6SJohannes Berg 							      ieee80211_vif_to_wdev(mvmsta->vif),
403e705c121SKalle Valo 							      trig);
404e705c121SKalle Valo 			if (trig_check && rx_status->signal < rssi)
4057174beb6SJohannes Berg 				iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
4067174beb6SJohannes Berg 							NULL);
407e705c121SKalle Valo 		}
408e705c121SKalle Valo 
409a399f980SJohannes Berg 		if (ieee80211_is_data(hdr->frame_control))
410e705c121SKalle Valo 			iwl_mvm_rx_csum(sta, skb, rx_pkt_status);
411a399f980SJohannes Berg 	}
412e705c121SKalle Valo 	rcu_read_unlock();
413e705c121SKalle Valo 
414e705c121SKalle Valo 	/* set the preamble flag if appropriate */
415e705c121SKalle Valo 	if (phy_info->phy_flags & cpu_to_le16(RX_RES_PHY_FLAGS_SHORT_PREAMBLE))
4167fdd69c5SJohannes Berg 		rx_status->enc_flags |= RX_ENC_FLAG_SHORTPRE;
417e705c121SKalle Valo 
418e705c121SKalle Valo 	if (phy_info->phy_flags & cpu_to_le16(RX_RES_PHY_FLAGS_AGG)) {
419e705c121SKalle Valo 		/*
420e705c121SKalle Valo 		 * We know which subframes of an A-MPDU belong
421e705c121SKalle Valo 		 * together since we get a single PHY response
422e705c121SKalle Valo 		 * from the firmware for all of them
423e705c121SKalle Valo 		 */
424e705c121SKalle Valo 		rx_status->flag |= RX_FLAG_AMPDU_DETAILS;
425e705c121SKalle Valo 		rx_status->ampdu_reference = mvm->ampdu_ref;
426e705c121SKalle Valo 	}
427e705c121SKalle Valo 
428e705c121SKalle Valo 	/* Set up the HT phy flags */
429e705c121SKalle Valo 	switch (rate_n_flags & RATE_MCS_CHAN_WIDTH_MSK) {
430e705c121SKalle Valo 	case RATE_MCS_CHAN_WIDTH_20:
431e705c121SKalle Valo 		break;
432e705c121SKalle Valo 	case RATE_MCS_CHAN_WIDTH_40:
433da6a4352SJohannes Berg 		rx_status->bw = RATE_INFO_BW_40;
434e705c121SKalle Valo 		break;
435e705c121SKalle Valo 	case RATE_MCS_CHAN_WIDTH_80:
436da6a4352SJohannes Berg 		rx_status->bw = RATE_INFO_BW_80;
437e705c121SKalle Valo 		break;
438e705c121SKalle Valo 	case RATE_MCS_CHAN_WIDTH_160:
439da6a4352SJohannes Berg 		rx_status->bw = RATE_INFO_BW_160;
440e705c121SKalle Valo 		break;
441e705c121SKalle Valo 	}
442*4c59ff5aSSara Sharon 	if (!(rate_n_flags & RATE_MCS_CCK_MSK) &&
443*4c59ff5aSSara Sharon 	    rate_n_flags & RATE_MCS_SGI_MSK)
4447fdd69c5SJohannes Berg 		rx_status->enc_flags |= RX_ENC_FLAG_SHORT_GI;
445e705c121SKalle Valo 	if (rate_n_flags & RATE_HT_MCS_GF_MSK)
4467fdd69c5SJohannes Berg 		rx_status->enc_flags |= RX_ENC_FLAG_HT_GF;
447e705c121SKalle Valo 	if (rate_n_flags & RATE_MCS_LDPC_MSK)
4487fdd69c5SJohannes Berg 		rx_status->enc_flags |= RX_ENC_FLAG_LDPC;
449e705c121SKalle Valo 	if (rate_n_flags & RATE_MCS_HT_MSK) {
45077e40945SSara Sharon 		u8 stbc = (rate_n_flags & RATE_MCS_STBC_MSK) >>
451e705c121SKalle Valo 				RATE_MCS_STBC_POS;
452da6a4352SJohannes Berg 		rx_status->encoding = RX_ENC_HT;
453e705c121SKalle Valo 		rx_status->rate_idx = rate_n_flags & RATE_HT_MCS_INDEX_MSK;
4547fdd69c5SJohannes Berg 		rx_status->enc_flags |= stbc << RX_ENC_FLAG_STBC_SHIFT;
455e705c121SKalle Valo 	} else if (rate_n_flags & RATE_MCS_VHT_MSK) {
45677e40945SSara Sharon 		u8 stbc = (rate_n_flags & RATE_MCS_STBC_MSK) >>
457e705c121SKalle Valo 				RATE_MCS_STBC_POS;
4588613c948SJohannes Berg 		rx_status->nss =
459e705c121SKalle Valo 			((rate_n_flags & RATE_VHT_MCS_NSS_MSK) >>
460e705c121SKalle Valo 						RATE_VHT_MCS_NSS_POS) + 1;
461e705c121SKalle Valo 		rx_status->rate_idx = rate_n_flags & RATE_VHT_MCS_RATE_CODE_MSK;
462da6a4352SJohannes Berg 		rx_status->encoding = RX_ENC_VHT;
4637fdd69c5SJohannes Berg 		rx_status->enc_flags |= stbc << RX_ENC_FLAG_STBC_SHIFT;
464e705c121SKalle Valo 		if (rate_n_flags & RATE_MCS_BF_MSK)
4657fdd69c5SJohannes Berg 			rx_status->enc_flags |= RX_ENC_FLAG_BF;
466e705c121SKalle Valo 	} else {
467cb2de6bbSSara Sharon 		int rate = iwl_mvm_legacy_rate_to_mac80211_idx(rate_n_flags,
468e705c121SKalle Valo 							       rx_status->band);
469cb2de6bbSSara Sharon 
470cb2de6bbSSara Sharon 		if (WARN(rate < 0 || rate > 0xFF,
471cb2de6bbSSara Sharon 			 "Invalid rate flags 0x%x, band %d,\n",
472cb2de6bbSSara Sharon 			 rate_n_flags, rx_status->band)) {
473cb2de6bbSSara Sharon 			kfree_skb(skb);
474cb2de6bbSSara Sharon 			return;
475cb2de6bbSSara Sharon 		}
476cb2de6bbSSara Sharon 		rx_status->rate_idx = rate;
477e705c121SKalle Valo 	}
478e705c121SKalle Valo 
479e705c121SKalle Valo #ifdef CONFIG_IWLWIFI_DEBUGFS
480e705c121SKalle Valo 	iwl_mvm_update_frame_stats(mvm, rate_n_flags,
481e705c121SKalle Valo 				   rx_status->flag & RX_FLAG_AMPDU_DETAILS);
482e705c121SKalle Valo #endif
483a339e918SLuca Coelho 
484a339e918SLuca Coelho 	if (unlikely((ieee80211_is_beacon(hdr->frame_control) ||
485a339e918SLuca Coelho 		      ieee80211_is_probe_resp(hdr->frame_control)) &&
486a339e918SLuca Coelho 		     mvm->sched_scan_pass_all == SCHED_SCAN_PASS_ALL_ENABLED))
487a339e918SLuca Coelho 		mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_FOUND;
488a339e918SLuca Coelho 
48943ec72b7SJohannes Berg 	if (unlikely(ieee80211_is_beacon(hdr->frame_control) ||
49043ec72b7SJohannes Berg 		     ieee80211_is_probe_resp(hdr->frame_control)))
49143ec72b7SJohannes Berg 		rx_status->boottime_ns = ktime_get_boot_ns();
49243ec72b7SJohannes Berg 
49316e4dd8fSLuca Coelho 	/* Take a reference briefly to kick off a d0i3 entry delay so
49416e4dd8fSLuca Coelho 	 * we can handle bursts of RX packets without toggling the
49516e4dd8fSLuca Coelho 	 * state too often.  But don't do this for beacons if we are
49616e4dd8fSLuca Coelho 	 * going to idle because the beacon filtering changes we make
49716e4dd8fSLuca Coelho 	 * cause the firmware to send us collateral beacons. */
49816e4dd8fSLuca Coelho 	take_ref = !(test_bit(STATUS_TRANS_GOING_IDLE, &mvm->trans->status) &&
49916e4dd8fSLuca Coelho 		     ieee80211_is_beacon(hdr->frame_control));
50016e4dd8fSLuca Coelho 
50116e4dd8fSLuca Coelho 	if (take_ref)
50216e4dd8fSLuca Coelho 		iwl_mvm_ref(mvm, IWL_MVM_REF_RX);
50316e4dd8fSLuca Coelho 
50443ec72b7SJohannes Berg 	iwl_mvm_pass_packet_to_mac80211(mvm, sta, napi, skb, hdr, len,
505bdbc58abSDan Carpenter 					crypt_len, rxb);
50616e4dd8fSLuca Coelho 
50716e4dd8fSLuca Coelho 	if (take_ref)
50816e4dd8fSLuca Coelho 		iwl_mvm_unref(mvm, IWL_MVM_REF_RX);
509e705c121SKalle Valo }
510e705c121SKalle Valo 
511e705c121SKalle Valo struct iwl_mvm_stat_data {
512e705c121SKalle Valo 	struct iwl_mvm *mvm;
513e705c121SKalle Valo 	__le32 mac_id;
514e705c121SKalle Valo 	u8 beacon_filter_average_energy;
5150e7ac018SSara Sharon 	void *general;
516e705c121SKalle Valo };
517e705c121SKalle Valo 
518e705c121SKalle Valo static void iwl_mvm_stat_iterator(void *_data, u8 *mac,
519e705c121SKalle Valo 				  struct ieee80211_vif *vif)
520e705c121SKalle Valo {
521e705c121SKalle Valo 	struct iwl_mvm_stat_data *data = _data;
522e705c121SKalle Valo 	struct iwl_mvm *mvm = data->mvm;
523e705c121SKalle Valo 	int sig = -data->beacon_filter_average_energy;
524e705c121SKalle Valo 	int last_event;
525e705c121SKalle Valo 	int thold = vif->bss_conf.cqm_rssi_thold;
526e705c121SKalle Valo 	int hyst = vif->bss_conf.cqm_rssi_hyst;
527e705c121SKalle Valo 	u16 id = le32_to_cpu(data->mac_id);
528e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
529e705c121SKalle Valo 
530e705c121SKalle Valo 	/* This doesn't need the MAC ID check since it's not taking the
531e705c121SKalle Valo 	 * data copied into the "data" struct, but rather the data from
532e705c121SKalle Valo 	 * the notification directly.
533e705c121SKalle Valo 	 */
534e705c121SKalle Valo 	if (data->general) {
5350e7ac018SSara Sharon 		u16 vif_id = mvmvif->id;
5360e7ac018SSara Sharon 
5370e7ac018SSara Sharon 		if (iwl_mvm_is_cdb_supported(mvm)) {
5380e7ac018SSara Sharon 			struct mvm_statistics_general_cdb *general =
5390e7ac018SSara Sharon 				data->general;
5400e7ac018SSara Sharon 
541e705c121SKalle Valo 			mvmvif->beacon_stats.num_beacons =
5420e7ac018SSara Sharon 				le32_to_cpu(general->beacon_counter[vif_id]);
543e705c121SKalle Valo 			mvmvif->beacon_stats.avg_signal =
5440e7ac018SSara Sharon 				-general->beacon_average_energy[vif_id];
5450e7ac018SSara Sharon 		} else {
5460e7ac018SSara Sharon 			struct mvm_statistics_general_v8 *general =
5470e7ac018SSara Sharon 				data->general;
5480e7ac018SSara Sharon 
5490e7ac018SSara Sharon 			mvmvif->beacon_stats.num_beacons =
5500e7ac018SSara Sharon 				le32_to_cpu(general->beacon_counter[vif_id]);
5510e7ac018SSara Sharon 			mvmvif->beacon_stats.avg_signal =
5520e7ac018SSara Sharon 				-general->beacon_average_energy[vif_id];
5530e7ac018SSara Sharon 		}
554e705c121SKalle Valo 	}
555e705c121SKalle Valo 
556e705c121SKalle Valo 	if (mvmvif->id != id)
557e705c121SKalle Valo 		return;
558e705c121SKalle Valo 
559e705c121SKalle Valo 	if (vif->type != NL80211_IFTYPE_STATION)
560e705c121SKalle Valo 		return;
561e705c121SKalle Valo 
562e705c121SKalle Valo 	if (sig == 0) {
563e705c121SKalle Valo 		IWL_DEBUG_RX(mvm, "RSSI is 0 - skip signal based decision\n");
564e705c121SKalle Valo 		return;
565e705c121SKalle Valo 	}
566e705c121SKalle Valo 
567e705c121SKalle Valo 	mvmvif->bf_data.ave_beacon_signal = sig;
568e705c121SKalle Valo 
569e705c121SKalle Valo 	/* BT Coex */
570e705c121SKalle Valo 	if (mvmvif->bf_data.bt_coex_min_thold !=
571e705c121SKalle Valo 	    mvmvif->bf_data.bt_coex_max_thold) {
572e705c121SKalle Valo 		last_event = mvmvif->bf_data.last_bt_coex_event;
573e705c121SKalle Valo 		if (sig > mvmvif->bf_data.bt_coex_max_thold &&
574e705c121SKalle Valo 		    (last_event <= mvmvif->bf_data.bt_coex_min_thold ||
575e705c121SKalle Valo 		     last_event == 0)) {
576e705c121SKalle Valo 			mvmvif->bf_data.last_bt_coex_event = sig;
577e705c121SKalle Valo 			IWL_DEBUG_RX(mvm, "cqm_iterator bt coex high %d\n",
578e705c121SKalle Valo 				     sig);
579e705c121SKalle Valo 			iwl_mvm_bt_rssi_event(mvm, vif, RSSI_EVENT_HIGH);
580e705c121SKalle Valo 		} else if (sig < mvmvif->bf_data.bt_coex_min_thold &&
581e705c121SKalle Valo 			   (last_event >= mvmvif->bf_data.bt_coex_max_thold ||
582e705c121SKalle Valo 			    last_event == 0)) {
583e705c121SKalle Valo 			mvmvif->bf_data.last_bt_coex_event = sig;
584e705c121SKalle Valo 			IWL_DEBUG_RX(mvm, "cqm_iterator bt coex low %d\n",
585e705c121SKalle Valo 				     sig);
586e705c121SKalle Valo 			iwl_mvm_bt_rssi_event(mvm, vif, RSSI_EVENT_LOW);
587e705c121SKalle Valo 		}
588e705c121SKalle Valo 	}
589e705c121SKalle Valo 
590e705c121SKalle Valo 	if (!(vif->driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI))
591e705c121SKalle Valo 		return;
592e705c121SKalle Valo 
593e705c121SKalle Valo 	/* CQM Notification */
594e705c121SKalle Valo 	last_event = mvmvif->bf_data.last_cqm_event;
595e705c121SKalle Valo 	if (thold && sig < thold && (last_event == 0 ||
596e705c121SKalle Valo 				     sig < last_event - hyst)) {
597e705c121SKalle Valo 		mvmvif->bf_data.last_cqm_event = sig;
598e705c121SKalle Valo 		IWL_DEBUG_RX(mvm, "cqm_iterator cqm low %d\n",
599e705c121SKalle Valo 			     sig);
600e705c121SKalle Valo 		ieee80211_cqm_rssi_notify(
601e705c121SKalle Valo 			vif,
602e705c121SKalle Valo 			NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
603769f07d8SAndrzej Zaborowski 			sig,
604e705c121SKalle Valo 			GFP_KERNEL);
605e705c121SKalle Valo 	} else if (sig > thold &&
606e705c121SKalle Valo 		   (last_event == 0 || sig > last_event + hyst)) {
607e705c121SKalle Valo 		mvmvif->bf_data.last_cqm_event = sig;
608e705c121SKalle Valo 		IWL_DEBUG_RX(mvm, "cqm_iterator cqm high %d\n",
609e705c121SKalle Valo 			     sig);
610e705c121SKalle Valo 		ieee80211_cqm_rssi_notify(
611e705c121SKalle Valo 			vif,
612e705c121SKalle Valo 			NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
613769f07d8SAndrzej Zaborowski 			sig,
614e705c121SKalle Valo 			GFP_KERNEL);
615e705c121SKalle Valo 	}
616e705c121SKalle Valo }
617e705c121SKalle Valo 
618e705c121SKalle Valo static inline void
619e705c121SKalle Valo iwl_mvm_rx_stats_check_trigger(struct iwl_mvm *mvm, struct iwl_rx_packet *pkt)
620e705c121SKalle Valo {
621e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_tlv *trig;
622e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_stats *trig_stats;
623e705c121SKalle Valo 	u32 trig_offset, trig_thold;
624e705c121SKalle Valo 
625e705c121SKalle Valo 	if (!iwl_fw_dbg_trigger_enabled(mvm->fw, FW_DBG_TRIGGER_STATS))
626e705c121SKalle Valo 		return;
627e705c121SKalle Valo 
628e705c121SKalle Valo 	trig = iwl_fw_dbg_get_trigger(mvm->fw, FW_DBG_TRIGGER_STATS);
629e705c121SKalle Valo 	trig_stats = (void *)trig->data;
630e705c121SKalle Valo 
6317174beb6SJohannes Berg 	if (!iwl_fw_dbg_trigger_check_stop(&mvm->fwrt, NULL, trig))
632e705c121SKalle Valo 		return;
633e705c121SKalle Valo 
634e705c121SKalle Valo 	trig_offset = le32_to_cpu(trig_stats->stop_offset);
635e705c121SKalle Valo 	trig_thold = le32_to_cpu(trig_stats->stop_threshold);
636e705c121SKalle Valo 
637e705c121SKalle Valo 	if (WARN_ON_ONCE(trig_offset >= iwl_rx_packet_payload_len(pkt)))
638e705c121SKalle Valo 		return;
639e705c121SKalle Valo 
640e705c121SKalle Valo 	if (le32_to_cpup((__le32 *) (pkt->data + trig_offset)) < trig_thold)
641e705c121SKalle Valo 		return;
642e705c121SKalle Valo 
6437174beb6SJohannes Berg 	iwl_fw_dbg_collect_trig(&mvm->fwrt, trig, NULL);
644e705c121SKalle Valo }
645e705c121SKalle Valo 
646e705c121SKalle Valo void iwl_mvm_handle_rx_statistics(struct iwl_mvm *mvm,
647e705c121SKalle Valo 				  struct iwl_rx_packet *pkt)
648e705c121SKalle Valo {
649e705c121SKalle Valo 	struct iwl_mvm_stat_data data = {
650e705c121SKalle Valo 		.mvm = mvm,
651e705c121SKalle Valo 	};
6520e7ac018SSara Sharon 	int expected_size;
653cfbeb598SGoodstein, Mordechay 	int i;
654cfbeb598SGoodstein, Mordechay 	u8 *energy;
655cfbeb598SGoodstein, Mordechay 	__le32 *bytes, *air_time;
656678d9b6dSLiad Kaufman 	__le32 flags;
6570e7ac018SSara Sharon 
658678d9b6dSLiad Kaufman 	if (!iwl_mvm_has_new_rx_stats_api(mvm)) {
659678d9b6dSLiad Kaufman 		if (iwl_mvm_has_new_rx_api(mvm))
6600e7ac018SSara Sharon 			expected_size = sizeof(struct iwl_notif_statistics_v11);
6610e7ac018SSara Sharon 		else
6620e7ac018SSara Sharon 			expected_size = sizeof(struct iwl_notif_statistics_v10);
663678d9b6dSLiad Kaufman 	} else {
664678d9b6dSLiad Kaufman 		expected_size = sizeof(struct iwl_notif_statistics_cdb);
665678d9b6dSLiad Kaufman 	}
666e705c121SKalle Valo 
66742fa5e0eSJohannes Berg 	if (WARN_ONCE(iwl_rx_packet_payload_len(pkt) != expected_size,
66842fa5e0eSJohannes Berg 		      "received invalid statistics size (%d)!\n",
66942fa5e0eSJohannes Berg 		      iwl_rx_packet_payload_len(pkt)))
6706a90f85aSLuca Coelho 		return;
671e705c121SKalle Valo 
672678d9b6dSLiad Kaufman 	if (!iwl_mvm_has_new_rx_stats_api(mvm)) {
673678d9b6dSLiad Kaufman 		struct iwl_notif_statistics_v11 *stats = (void *)&pkt->data;
674678d9b6dSLiad Kaufman 
675e705c121SKalle Valo 		data.mac_id = stats->rx.general.mac_id;
676e705c121SKalle Valo 		data.beacon_filter_average_energy =
6770e7ac018SSara Sharon 			stats->general.common.beacon_filter_average_energy;
678e705c121SKalle Valo 
679678d9b6dSLiad Kaufman 		mvm->rx_stats_v3 = stats->rx;
680e705c121SKalle Valo 
681678d9b6dSLiad Kaufman 		mvm->radio_stats.rx_time =
682678d9b6dSLiad Kaufman 			le64_to_cpu(stats->general.common.rx_time);
683678d9b6dSLiad Kaufman 		mvm->radio_stats.tx_time =
684678d9b6dSLiad Kaufman 			le64_to_cpu(stats->general.common.tx_time);
685e705c121SKalle Valo 		mvm->radio_stats.on_time_rf =
6860e7ac018SSara Sharon 			le64_to_cpu(stats->general.common.on_time_rf);
687e705c121SKalle Valo 		mvm->radio_stats.on_time_scan =
6880e7ac018SSara Sharon 			le64_to_cpu(stats->general.common.on_time_scan);
689e705c121SKalle Valo 
690e705c121SKalle Valo 		data.general = &stats->general;
691cfbeb598SGoodstein, Mordechay 
692678d9b6dSLiad Kaufman 		flags = stats->flag;
693678d9b6dSLiad Kaufman 	} else {
694678d9b6dSLiad Kaufman 		struct iwl_notif_statistics_cdb *stats = (void *)&pkt->data;
695678d9b6dSLiad Kaufman 
696678d9b6dSLiad Kaufman 		data.mac_id = stats->rx.general.mac_id;
697678d9b6dSLiad Kaufman 		data.beacon_filter_average_energy =
698678d9b6dSLiad Kaufman 			stats->general.common.beacon_filter_average_energy;
699678d9b6dSLiad Kaufman 
700678d9b6dSLiad Kaufman 		mvm->rx_stats = stats->rx;
701678d9b6dSLiad Kaufman 
702678d9b6dSLiad Kaufman 		mvm->radio_stats.rx_time =
703678d9b6dSLiad Kaufman 			le64_to_cpu(stats->general.common.rx_time);
704678d9b6dSLiad Kaufman 		mvm->radio_stats.tx_time =
705678d9b6dSLiad Kaufman 			le64_to_cpu(stats->general.common.tx_time);
706678d9b6dSLiad Kaufman 		mvm->radio_stats.on_time_rf =
707678d9b6dSLiad Kaufman 			le64_to_cpu(stats->general.common.on_time_rf);
708678d9b6dSLiad Kaufman 		mvm->radio_stats.on_time_scan =
709678d9b6dSLiad Kaufman 			le64_to_cpu(stats->general.common.on_time_scan);
710678d9b6dSLiad Kaufman 
711678d9b6dSLiad Kaufman 		data.general = &stats->general;
712678d9b6dSLiad Kaufman 
713678d9b6dSLiad Kaufman 		flags = stats->flag;
714678d9b6dSLiad Kaufman 	}
715678d9b6dSLiad Kaufman 
716cfbeb598SGoodstein, Mordechay 	iwl_mvm_rx_stats_check_trigger(mvm, pkt);
717cfbeb598SGoodstein, Mordechay 
718cfbeb598SGoodstein, Mordechay 	ieee80211_iterate_active_interfaces(mvm->hw,
719cfbeb598SGoodstein, Mordechay 					    IEEE80211_IFACE_ITER_NORMAL,
720cfbeb598SGoodstein, Mordechay 					    iwl_mvm_stat_iterator,
721cfbeb598SGoodstein, Mordechay 					    &data);
722cfbeb598SGoodstein, Mordechay 
723cfbeb598SGoodstein, Mordechay 	if (!iwl_mvm_has_new_rx_api(mvm))
724cfbeb598SGoodstein, Mordechay 		return;
725988b5968SSara Sharon 
726678d9b6dSLiad Kaufman 	if (!iwl_mvm_has_new_rx_stats_api(mvm)) {
727678d9b6dSLiad Kaufman 		struct iwl_notif_statistics_v11 *v11 = (void *)&pkt->data;
7280e7ac018SSara Sharon 
7290e7ac018SSara Sharon 		energy = (void *)&v11->load_stats.avg_energy;
7300e7ac018SSara Sharon 		bytes = (void *)&v11->load_stats.byte_count;
7310e7ac018SSara Sharon 		air_time = (void *)&v11->load_stats.air_time;
7320e7ac018SSara Sharon 	} else {
733678d9b6dSLiad Kaufman 		struct iwl_notif_statistics_cdb *stats = (void *)&pkt->data;
734678d9b6dSLiad Kaufman 
7350e7ac018SSara Sharon 		energy = (void *)&stats->load_stats.avg_energy;
7360e7ac018SSara Sharon 		bytes = (void *)&stats->load_stats.byte_count;
7370e7ac018SSara Sharon 		air_time = (void *)&stats->load_stats.air_time;
7380e7ac018SSara Sharon 	}
739988b5968SSara Sharon 
740988b5968SSara Sharon 	rcu_read_lock();
7410ae98812SSara Sharon 	for (i = 0; i < ARRAY_SIZE(mvm->fw_id_to_mac_id); i++) {
742988b5968SSara Sharon 		struct iwl_mvm_sta *sta;
743988b5968SSara Sharon 
7440e7ac018SSara Sharon 		if (!energy[i])
745988b5968SSara Sharon 			continue;
746988b5968SSara Sharon 
747988b5968SSara Sharon 		sta = iwl_mvm_sta_from_staid_rcu(mvm, i);
748988b5968SSara Sharon 		if (!sta)
749988b5968SSara Sharon 			continue;
7500e7ac018SSara Sharon 		sta->avg_energy = energy[i];
751988b5968SSara Sharon 	}
752988b5968SSara Sharon 	rcu_read_unlock();
753e705c121SKalle Valo }
754e705c121SKalle Valo 
755e705c121SKalle Valo void iwl_mvm_rx_statistics(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
756e705c121SKalle Valo {
757e705c121SKalle Valo 	iwl_mvm_handle_rx_statistics(mvm, rxb_addr(rxb));
758e705c121SKalle Valo }
7593af512d6SSara Sharon 
7603af512d6SSara Sharon void iwl_mvm_window_status_notif(struct iwl_mvm *mvm,
7613af512d6SSara Sharon 				 struct iwl_rx_cmd_buffer *rxb)
7623af512d6SSara Sharon {
7633af512d6SSara Sharon 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
7643af512d6SSara Sharon 	struct iwl_ba_window_status_notif *notif = (void *)pkt->data;
7653af512d6SSara Sharon 	int i;
7663af512d6SSara Sharon 	u32 pkt_len = iwl_rx_packet_payload_len(pkt);
7673af512d6SSara Sharon 
7683af512d6SSara Sharon 	if (WARN_ONCE(pkt_len != sizeof(*notif),
7693af512d6SSara Sharon 		      "Received window status notification of wrong size (%u)\n",
7703af512d6SSara Sharon 		      pkt_len))
7713af512d6SSara Sharon 		return;
7723af512d6SSara Sharon 
7733af512d6SSara Sharon 	rcu_read_lock();
7743af512d6SSara Sharon 	for (i = 0; i < BA_WINDOW_STREAMS_MAX; i++) {
7753af512d6SSara Sharon 		struct ieee80211_sta *sta;
7763af512d6SSara Sharon 		u8 sta_id, tid;
7773af512d6SSara Sharon 		u64 bitmap;
7783af512d6SSara Sharon 		u32 ssn;
7793af512d6SSara Sharon 		u16 ratid;
7803af512d6SSara Sharon 		u16 received_mpdu;
7813af512d6SSara Sharon 
7823af512d6SSara Sharon 		ratid = le16_to_cpu(notif->ra_tid[i]);
7833af512d6SSara Sharon 		/* check that this TID is valid */
7843af512d6SSara Sharon 		if (!(ratid & BA_WINDOW_STATUS_VALID_MSK))
7853af512d6SSara Sharon 			continue;
7863af512d6SSara Sharon 
7873af512d6SSara Sharon 		received_mpdu = le16_to_cpu(notif->mpdu_rx_count[i]);
7883af512d6SSara Sharon 		if (received_mpdu == 0)
7893af512d6SSara Sharon 			continue;
7903af512d6SSara Sharon 
7913af512d6SSara Sharon 		tid = ratid & BA_WINDOW_STATUS_TID_MSK;
7923af512d6SSara Sharon 		/* get the station */
7933af512d6SSara Sharon 		sta_id = (ratid & BA_WINDOW_STATUS_STA_ID_MSK)
7943af512d6SSara Sharon 			 >> BA_WINDOW_STATUS_STA_ID_POS;
7953af512d6SSara Sharon 		sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
7963af512d6SSara Sharon 		if (IS_ERR_OR_NULL(sta))
7973af512d6SSara Sharon 			continue;
7983af512d6SSara Sharon 		bitmap = le64_to_cpu(notif->bitmap[i]);
7993af512d6SSara Sharon 		ssn = le32_to_cpu(notif->start_seq_num[i]);
8003af512d6SSara Sharon 
8013af512d6SSara Sharon 		/* update mac80211 with the bitmap for the reordering buffer */
8023af512d6SSara Sharon 		ieee80211_mark_rx_ba_filtered_frames(sta, tid, ssn, bitmap,
8033af512d6SSara Sharon 						     received_mpdu);
8043af512d6SSara Sharon 	}
8053af512d6SSara Sharon 	rcu_read_unlock();
8063af512d6SSara Sharon }
807