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
10e705c121SKalle Valo  *
11e705c121SKalle Valo  * This program is free software; you can redistribute it and/or modify
12e705c121SKalle Valo  * it under the terms of version 2 of the GNU General Public License as
13e705c121SKalle Valo  * published by the Free Software Foundation.
14e705c121SKalle Valo  *
15e705c121SKalle Valo  * This program is distributed in the hope that it will be useful, but
16e705c121SKalle Valo  * WITHOUT ANY WARRANTY; without even the implied warranty of
17e705c121SKalle Valo  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18e705c121SKalle Valo  * General Public License for more details.
19e705c121SKalle Valo  *
20e705c121SKalle Valo  * You should have received a copy of the GNU General Public License
21e705c121SKalle Valo  * along with this program; if not, write to the Free Software
22e705c121SKalle Valo  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
23e705c121SKalle Valo  * USA
24e705c121SKalle Valo  *
25e705c121SKalle Valo  * The full GNU General Public License is included in this distribution
26e705c121SKalle Valo  * in the file called COPYING.
27e705c121SKalle Valo  *
28e705c121SKalle Valo  * Contact Information:
29e705c121SKalle Valo  *  Intel Linux Wireless <ilw@linux.intel.com>
30e705c121SKalle Valo  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
31e705c121SKalle Valo  *
32e705c121SKalle Valo  * BSD LICENSE
33e705c121SKalle Valo  *
34e705c121SKalle Valo  * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
35e705c121SKalle Valo  * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
36e705c121SKalle Valo  * All rights reserved.
37e705c121SKalle Valo  *
38e705c121SKalle Valo  * Redistribution and use in source and binary forms, with or without
39e705c121SKalle Valo  * modification, are permitted provided that the following conditions
40e705c121SKalle Valo  * are met:
41e705c121SKalle Valo  *
42e705c121SKalle Valo  *  * Redistributions of source code must retain the above copyright
43e705c121SKalle Valo  *    notice, this list of conditions and the following disclaimer.
44e705c121SKalle Valo  *  * Redistributions in binary form must reproduce the above copyright
45e705c121SKalle Valo  *    notice, this list of conditions and the following disclaimer in
46e705c121SKalle Valo  *    the documentation and/or other materials provided with the
47e705c121SKalle Valo  *    distribution.
48e705c121SKalle Valo  *  * Neither the name Intel Corporation nor the names of its
49e705c121SKalle Valo  *    contributors may be used to endorse or promote products derived
50e705c121SKalle Valo  *    from this software without specific prior written permission.
51e705c121SKalle Valo  *
52e705c121SKalle Valo  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
53e705c121SKalle Valo  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
54e705c121SKalle Valo  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
55e705c121SKalle Valo  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
56e705c121SKalle Valo  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
57e705c121SKalle Valo  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
58e705c121SKalle Valo  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
59e705c121SKalle Valo  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
60e705c121SKalle Valo  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
61e705c121SKalle Valo  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
62e705c121SKalle Valo  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
63e705c121SKalle Valo  *****************************************************************************/
64e705c121SKalle Valo #include <linux/skbuff.h>
65e705c121SKalle Valo #include "iwl-trans.h"
66e705c121SKalle Valo #include "mvm.h"
67e705c121SKalle Valo #include "fw-api.h"
682f89a5d7SGolan Ben-Ami #include "fw-dbg.h"
69e705c121SKalle Valo 
70e705c121SKalle Valo /*
71e705c121SKalle Valo  * iwl_mvm_rx_rx_phy_cmd - REPLY_RX_PHY_CMD handler
72e705c121SKalle Valo  *
73e705c121SKalle Valo  * Copies the phy information in mvm->last_phy_info, it will be used when the
74e705c121SKalle Valo  * actual data will come from the fw in the next packet.
75e705c121SKalle Valo  */
76e705c121SKalle Valo void iwl_mvm_rx_rx_phy_cmd(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
77e705c121SKalle Valo {
78e705c121SKalle Valo 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
79e705c121SKalle Valo 
80e705c121SKalle Valo 	memcpy(&mvm->last_phy_info, pkt->data, sizeof(mvm->last_phy_info));
81e705c121SKalle Valo 	mvm->ampdu_ref++;
82e705c121SKalle Valo 
83e705c121SKalle Valo #ifdef CONFIG_IWLWIFI_DEBUGFS
84e705c121SKalle Valo 	if (mvm->last_phy_info.phy_flags & cpu_to_le16(RX_RES_PHY_FLAGS_AGG)) {
85e705c121SKalle Valo 		spin_lock(&mvm->drv_stats_lock);
86e705c121SKalle Valo 		mvm->drv_rx_stats.ampdu_count++;
87e705c121SKalle Valo 		spin_unlock(&mvm->drv_stats_lock);
88e705c121SKalle Valo 	}
89e705c121SKalle Valo #endif
90e705c121SKalle Valo }
91e705c121SKalle Valo 
92e705c121SKalle Valo /*
93e705c121SKalle Valo  * iwl_mvm_pass_packet_to_mac80211 - builds the packet for mac80211
94e705c121SKalle Valo  *
95e705c121SKalle Valo  * Adds the rxb to a new skb and give it to mac80211
96e705c121SKalle Valo  */
97e705c121SKalle Valo static void iwl_mvm_pass_packet_to_mac80211(struct iwl_mvm *mvm,
98e705c121SKalle Valo 					    struct napi_struct *napi,
99e705c121SKalle Valo 					    struct sk_buff *skb,
100e705c121SKalle Valo 					    struct ieee80211_hdr *hdr, u16 len,
101e705c121SKalle Valo 					    u32 ampdu_status, u8 crypt_len,
102e705c121SKalle Valo 					    struct iwl_rx_cmd_buffer *rxb)
103e705c121SKalle Valo {
104e705c121SKalle Valo 	unsigned int hdrlen, fraglen;
105e705c121SKalle Valo 
106e705c121SKalle Valo 	/* If frame is small enough to fit in skb->head, pull it completely.
107e705c121SKalle Valo 	 * If not, only pull ieee80211_hdr (including crypto if present, and
108e705c121SKalle Valo 	 * an additional 8 bytes for SNAP/ethertype, see below) so that
109e705c121SKalle Valo 	 * splice() or TCP coalesce are more efficient.
110e705c121SKalle Valo 	 *
111e705c121SKalle Valo 	 * Since, in addition, ieee80211_data_to_8023() always pull in at
112e705c121SKalle Valo 	 * least 8 bytes (possibly more for mesh) we can do the same here
113e705c121SKalle Valo 	 * to save the cost of doing it later. That still doesn't pull in
114e705c121SKalle Valo 	 * the actual IP header since the typical case has a SNAP header.
115e705c121SKalle Valo 	 * If the latter changes (there are efforts in the standards group
116e705c121SKalle Valo 	 * to do so) we should revisit this and ieee80211_data_to_8023().
117e705c121SKalle Valo 	 */
118e705c121SKalle Valo 	hdrlen = (len <= skb_tailroom(skb)) ? len :
119e705c121SKalle Valo 					      sizeof(*hdr) + crypt_len + 8;
120e705c121SKalle Valo 
121e705c121SKalle Valo 	memcpy(skb_put(skb, hdrlen), hdr, hdrlen);
122e705c121SKalle Valo 	fraglen = len - hdrlen;
123e705c121SKalle Valo 
124e705c121SKalle Valo 	if (fraglen) {
125e705c121SKalle Valo 		int offset = (void *)hdr + hdrlen -
126e705c121SKalle Valo 			     rxb_addr(rxb) + rxb_offset(rxb);
127e705c121SKalle Valo 
128e705c121SKalle Valo 		skb_add_rx_frag(skb, 0, rxb_steal_page(rxb), offset,
129e705c121SKalle Valo 				fraglen, rxb->truesize);
130e705c121SKalle Valo 	}
131e705c121SKalle Valo 
132e705c121SKalle Valo 	ieee80211_rx_napi(mvm->hw, skb, napi);
133e705c121SKalle Valo }
134e705c121SKalle Valo 
135e705c121SKalle Valo /*
136e705c121SKalle Valo  * iwl_mvm_get_signal_strength - use new rx PHY INFO API
137e705c121SKalle Valo  * values are reported by the fw as positive values - need to negate
138e705c121SKalle Valo  * to obtain their dBM.  Account for missing antennas by replacing 0
139e705c121SKalle Valo  * values by -256dBm: practically 0 power and a non-feasible 8 bit value.
140e705c121SKalle Valo  */
141e705c121SKalle Valo static void iwl_mvm_get_signal_strength(struct iwl_mvm *mvm,
142e705c121SKalle Valo 					struct iwl_rx_phy_info *phy_info,
143e705c121SKalle Valo 					struct ieee80211_rx_status *rx_status)
144e705c121SKalle Valo {
145e705c121SKalle Valo 	int energy_a, energy_b, energy_c, max_energy;
146e705c121SKalle Valo 	u32 val;
147e705c121SKalle Valo 
148e705c121SKalle Valo 	val =
149e705c121SKalle Valo 	    le32_to_cpu(phy_info->non_cfg_phy[IWL_RX_INFO_ENERGY_ANT_ABC_IDX]);
150e705c121SKalle Valo 	energy_a = (val & IWL_RX_INFO_ENERGY_ANT_A_MSK) >>
151e705c121SKalle Valo 						IWL_RX_INFO_ENERGY_ANT_A_POS;
152e705c121SKalle Valo 	energy_a = energy_a ? -energy_a : S8_MIN;
153e705c121SKalle Valo 	energy_b = (val & IWL_RX_INFO_ENERGY_ANT_B_MSK) >>
154e705c121SKalle Valo 						IWL_RX_INFO_ENERGY_ANT_B_POS;
155e705c121SKalle Valo 	energy_b = energy_b ? -energy_b : S8_MIN;
156e705c121SKalle Valo 	energy_c = (val & IWL_RX_INFO_ENERGY_ANT_C_MSK) >>
157e705c121SKalle Valo 						IWL_RX_INFO_ENERGY_ANT_C_POS;
158e705c121SKalle Valo 	energy_c = energy_c ? -energy_c : S8_MIN;
159e705c121SKalle Valo 	max_energy = max(energy_a, energy_b);
160e705c121SKalle Valo 	max_energy = max(max_energy, energy_c);
161e705c121SKalle Valo 
162e705c121SKalle Valo 	IWL_DEBUG_STATS(mvm, "energy In A %d B %d C %d , and max %d\n",
163e705c121SKalle Valo 			energy_a, energy_b, energy_c, max_energy);
164e705c121SKalle Valo 
165e705c121SKalle Valo 	rx_status->signal = max_energy;
166e705c121SKalle Valo 	rx_status->chains = (le16_to_cpu(phy_info->phy_flags) &
167e705c121SKalle Valo 				RX_RES_PHY_FLAGS_ANTENNA)
168e705c121SKalle Valo 					>> RX_RES_PHY_FLAGS_ANTENNA_POS;
169e705c121SKalle Valo 	rx_status->chain_signal[0] = energy_a;
170e705c121SKalle Valo 	rx_status->chain_signal[1] = energy_b;
171e705c121SKalle Valo 	rx_status->chain_signal[2] = energy_c;
172e705c121SKalle Valo }
173e705c121SKalle Valo 
174e705c121SKalle Valo /*
175e705c121SKalle Valo  * iwl_mvm_set_mac80211_rx_flag - translate fw status to mac80211 format
176e705c121SKalle Valo  * @mvm: the mvm object
177e705c121SKalle Valo  * @hdr: 80211 header
178e705c121SKalle Valo  * @stats: status in mac80211's format
179e705c121SKalle Valo  * @rx_pkt_status: status coming from fw
180e705c121SKalle Valo  *
181e705c121SKalle Valo  * returns non 0 value if the packet should be dropped
182e705c121SKalle Valo  */
183e705c121SKalle Valo static u32 iwl_mvm_set_mac80211_rx_flag(struct iwl_mvm *mvm,
184e705c121SKalle Valo 					struct ieee80211_hdr *hdr,
185e705c121SKalle Valo 					struct ieee80211_rx_status *stats,
186e705c121SKalle Valo 					u32 rx_pkt_status,
187e705c121SKalle Valo 					u8 *crypt_len)
188e705c121SKalle Valo {
189e705c121SKalle Valo 	if (!ieee80211_has_protected(hdr->frame_control) ||
190e705c121SKalle Valo 	    (rx_pkt_status & RX_MPDU_RES_STATUS_SEC_ENC_MSK) ==
191e705c121SKalle Valo 			     RX_MPDU_RES_STATUS_SEC_NO_ENC)
192e705c121SKalle Valo 		return 0;
193e705c121SKalle Valo 
194e705c121SKalle Valo 	/* packet was encrypted with unknown alg */
195e705c121SKalle Valo 	if ((rx_pkt_status & RX_MPDU_RES_STATUS_SEC_ENC_MSK) ==
196e705c121SKalle Valo 					RX_MPDU_RES_STATUS_SEC_ENC_ERR)
197e705c121SKalle Valo 		return 0;
198e705c121SKalle Valo 
199e705c121SKalle Valo 	switch (rx_pkt_status & RX_MPDU_RES_STATUS_SEC_ENC_MSK) {
200e705c121SKalle Valo 	case RX_MPDU_RES_STATUS_SEC_CCM_ENC:
201e705c121SKalle Valo 		/* alg is CCM: check MIC only */
202e705c121SKalle Valo 		if (!(rx_pkt_status & RX_MPDU_RES_STATUS_MIC_OK))
203e705c121SKalle Valo 			return -1;
204e705c121SKalle Valo 
205e705c121SKalle Valo 		stats->flag |= RX_FLAG_DECRYPTED;
206e705c121SKalle Valo 		*crypt_len = IEEE80211_CCMP_HDR_LEN;
207e705c121SKalle Valo 		return 0;
208e705c121SKalle Valo 
209e705c121SKalle Valo 	case RX_MPDU_RES_STATUS_SEC_TKIP_ENC:
210e705c121SKalle Valo 		/* Don't drop the frame and decrypt it in SW */
211e705c121SKalle Valo 		if (!(rx_pkt_status & RX_MPDU_RES_STATUS_TTAK_OK))
212e705c121SKalle Valo 			return 0;
213e705c121SKalle Valo 		*crypt_len = IEEE80211_TKIP_IV_LEN;
214e705c121SKalle Valo 		/* fall through if TTAK OK */
215e705c121SKalle Valo 
216e705c121SKalle Valo 	case RX_MPDU_RES_STATUS_SEC_WEP_ENC:
217e705c121SKalle Valo 		if (!(rx_pkt_status & RX_MPDU_RES_STATUS_ICV_OK))
218e705c121SKalle Valo 			return -1;
219e705c121SKalle Valo 
220e705c121SKalle Valo 		stats->flag |= RX_FLAG_DECRYPTED;
221e705c121SKalle Valo 		if ((rx_pkt_status & RX_MPDU_RES_STATUS_SEC_ENC_MSK) ==
222e705c121SKalle Valo 				RX_MPDU_RES_STATUS_SEC_WEP_ENC)
223e705c121SKalle Valo 			*crypt_len = IEEE80211_WEP_IV_LEN;
224e705c121SKalle Valo 		return 0;
225e705c121SKalle Valo 
226e705c121SKalle Valo 	case RX_MPDU_RES_STATUS_SEC_EXT_ENC:
227e705c121SKalle Valo 		if (!(rx_pkt_status & RX_MPDU_RES_STATUS_MIC_OK))
228e705c121SKalle Valo 			return -1;
229e705c121SKalle Valo 		stats->flag |= RX_FLAG_DECRYPTED;
230e705c121SKalle Valo 		return 0;
231e705c121SKalle Valo 
232e705c121SKalle Valo 	default:
233e705c121SKalle Valo 		IWL_ERR(mvm, "Unhandled alg: 0x%x\n", rx_pkt_status);
234e705c121SKalle Valo 	}
235e705c121SKalle Valo 
236e705c121SKalle Valo 	return 0;
237e705c121SKalle Valo }
238e705c121SKalle Valo 
239e705c121SKalle Valo static void iwl_mvm_rx_csum(struct ieee80211_sta *sta,
240e705c121SKalle Valo 			    struct sk_buff *skb,
241e705c121SKalle Valo 			    u32 status)
242e705c121SKalle Valo {
243e705c121SKalle Valo 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
244e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(mvmsta->vif);
245e705c121SKalle Valo 
246e705c121SKalle Valo 	if (mvmvif->features & NETIF_F_RXCSUM &&
247e705c121SKalle Valo 	    status & RX_MPDU_RES_STATUS_CSUM_DONE &&
248e705c121SKalle Valo 	    status & RX_MPDU_RES_STATUS_CSUM_OK)
249e705c121SKalle Valo 		skb->ip_summed = CHECKSUM_UNNECESSARY;
250e705c121SKalle Valo }
251e705c121SKalle Valo 
252e705c121SKalle Valo /*
253e705c121SKalle Valo  * iwl_mvm_rx_rx_mpdu - REPLY_RX_MPDU_CMD handler
254e705c121SKalle Valo  *
255e705c121SKalle Valo  * Handles the actual data of the Rx packet from the fw
256e705c121SKalle Valo  */
257e705c121SKalle Valo void iwl_mvm_rx_rx_mpdu(struct iwl_mvm *mvm, struct napi_struct *napi,
258e705c121SKalle Valo 			struct iwl_rx_cmd_buffer *rxb)
259e705c121SKalle Valo {
260e705c121SKalle Valo 	struct ieee80211_hdr *hdr;
261e705c121SKalle Valo 	struct ieee80211_rx_status *rx_status;
262e705c121SKalle Valo 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
263e705c121SKalle Valo 	struct iwl_rx_phy_info *phy_info;
264e705c121SKalle Valo 	struct iwl_rx_mpdu_res_start *rx_res;
265e705c121SKalle Valo 	struct ieee80211_sta *sta;
266e705c121SKalle Valo 	struct sk_buff *skb;
267e705c121SKalle Valo 	u32 len;
268e705c121SKalle Valo 	u32 ampdu_status;
269e705c121SKalle Valo 	u32 rate_n_flags;
270e705c121SKalle Valo 	u32 rx_pkt_status;
271e705c121SKalle Valo 	u8 crypt_len = 0;
272e705c121SKalle Valo 
273e705c121SKalle Valo 	phy_info = &mvm->last_phy_info;
274e705c121SKalle Valo 	rx_res = (struct iwl_rx_mpdu_res_start *)pkt->data;
275e705c121SKalle Valo 	hdr = (struct ieee80211_hdr *)(pkt->data + sizeof(*rx_res));
276e705c121SKalle Valo 	len = le16_to_cpu(rx_res->byte_count);
277e705c121SKalle Valo 	rx_pkt_status = le32_to_cpup((__le32 *)
278e705c121SKalle Valo 		(pkt->data + sizeof(*rx_res) + len));
279e705c121SKalle Valo 
280e705c121SKalle Valo 	/* Dont use dev_alloc_skb(), we'll have enough headroom once
281e705c121SKalle Valo 	 * ieee80211_hdr pulled.
282e705c121SKalle Valo 	 */
283e705c121SKalle Valo 	skb = alloc_skb(128, GFP_ATOMIC);
284e705c121SKalle Valo 	if (!skb) {
285e705c121SKalle Valo 		IWL_ERR(mvm, "alloc_skb failed\n");
286e705c121SKalle Valo 		return;
287e705c121SKalle Valo 	}
288e705c121SKalle Valo 
289e705c121SKalle Valo 	rx_status = IEEE80211_SKB_RXCB(skb);
290e705c121SKalle Valo 
291e705c121SKalle Valo 	/*
292e705c121SKalle Valo 	 * drop the packet if it has failed being decrypted by HW
293e705c121SKalle Valo 	 */
294e705c121SKalle Valo 	if (iwl_mvm_set_mac80211_rx_flag(mvm, hdr, rx_status, rx_pkt_status,
295e705c121SKalle Valo 					 &crypt_len)) {
296e705c121SKalle Valo 		IWL_DEBUG_DROP(mvm, "Bad decryption results 0x%08x\n",
297e705c121SKalle Valo 			       rx_pkt_status);
298e705c121SKalle Valo 		kfree_skb(skb);
299e705c121SKalle Valo 		return;
300e705c121SKalle Valo 	}
301e705c121SKalle Valo 
302e705c121SKalle Valo 	/*
303e705c121SKalle Valo 	 * Keep packets with CRC errors (and with overrun) for monitor mode
304e705c121SKalle Valo 	 * (otherwise the firmware discards them) but mark them as bad.
305e705c121SKalle Valo 	 */
306e705c121SKalle Valo 	if (!(rx_pkt_status & RX_MPDU_RES_STATUS_CRC_OK) ||
307e705c121SKalle Valo 	    !(rx_pkt_status & RX_MPDU_RES_STATUS_OVERRUN_OK)) {
308e705c121SKalle Valo 		IWL_DEBUG_RX(mvm, "Bad CRC or FIFO: 0x%08X.\n", rx_pkt_status);
309e705c121SKalle Valo 		rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
310e705c121SKalle Valo 	}
311e705c121SKalle Valo 
312e705c121SKalle Valo 	/* This will be used in several places later */
313e705c121SKalle Valo 	rate_n_flags = le32_to_cpu(phy_info->rate_n_flags);
314e705c121SKalle Valo 
315e705c121SKalle Valo 	/* rx_status carries information about the packet to mac80211 */
316e705c121SKalle Valo 	rx_status->mactime = le64_to_cpu(phy_info->timestamp);
317e705c121SKalle Valo 	rx_status->device_timestamp = le32_to_cpu(phy_info->system_timestamp);
318e705c121SKalle Valo 	rx_status->band =
319e705c121SKalle Valo 		(phy_info->phy_flags & cpu_to_le16(RX_RES_PHY_FLAGS_BAND_24)) ?
320e705c121SKalle Valo 				IEEE80211_BAND_2GHZ : IEEE80211_BAND_5GHZ;
321e705c121SKalle Valo 	rx_status->freq =
322e705c121SKalle Valo 		ieee80211_channel_to_frequency(le16_to_cpu(phy_info->channel),
323e705c121SKalle Valo 					       rx_status->band);
324e705c121SKalle Valo 	/*
325e705c121SKalle Valo 	 * TSF as indicated by the fw is at INA time, but mac80211 expects the
326e705c121SKalle Valo 	 * TSF at the beginning of the MPDU.
327e705c121SKalle Valo 	 */
328e705c121SKalle Valo 	/*rx_status->flag |= RX_FLAG_MACTIME_MPDU;*/
329e705c121SKalle Valo 
330e705c121SKalle Valo 	iwl_mvm_get_signal_strength(mvm, phy_info, rx_status);
331e705c121SKalle Valo 
332e705c121SKalle Valo 	IWL_DEBUG_STATS_LIMIT(mvm, "Rssi %d, TSF %llu\n", rx_status->signal,
333e705c121SKalle Valo 			      (unsigned long long)rx_status->mactime);
334e705c121SKalle Valo 
335e705c121SKalle Valo 	rcu_read_lock();
336e705c121SKalle Valo 	/*
337e705c121SKalle Valo 	 * We have tx blocked stations (with CS bit). If we heard frames from
338e705c121SKalle Valo 	 * a blocked station on a new channel we can TX to it again.
339e705c121SKalle Valo 	 */
340e705c121SKalle Valo 	if (unlikely(mvm->csa_tx_block_bcn_timeout)) {
341e705c121SKalle Valo 		sta = ieee80211_find_sta(
342e705c121SKalle Valo 			rcu_dereference(mvm->csa_tx_blocked_vif), hdr->addr2);
343e705c121SKalle Valo 		if (sta)
344e705c121SKalle Valo 			iwl_mvm_sta_modify_disable_tx_ap(mvm, sta, false);
345e705c121SKalle Valo 	}
346e705c121SKalle Valo 
347e705c121SKalle Valo 	/* This is fine since we don't support multiple AP interfaces */
348e705c121SKalle Valo 	sta = ieee80211_find_sta_by_ifaddr(mvm->hw, hdr->addr2, NULL);
349e705c121SKalle Valo 	if (sta) {
350e705c121SKalle Valo 		struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
351e705c121SKalle Valo 
352e705c121SKalle Valo 		rs_update_last_rssi(mvm, &mvmsta->lq_sta, rx_status);
353e705c121SKalle Valo 
354e705c121SKalle Valo 		if (iwl_fw_dbg_trigger_enabled(mvm->fw, FW_DBG_TRIGGER_RSSI) &&
355e705c121SKalle Valo 		    ieee80211_is_beacon(hdr->frame_control)) {
356e705c121SKalle Valo 			struct iwl_fw_dbg_trigger_tlv *trig;
357e705c121SKalle Valo 			struct iwl_fw_dbg_trigger_low_rssi *rssi_trig;
358e705c121SKalle Valo 			bool trig_check;
359e705c121SKalle Valo 			s32 rssi;
360e705c121SKalle Valo 
361e705c121SKalle Valo 			trig = iwl_fw_dbg_get_trigger(mvm->fw,
362e705c121SKalle Valo 						      FW_DBG_TRIGGER_RSSI);
363e705c121SKalle Valo 			rssi_trig = (void *)trig->data;
364e705c121SKalle Valo 			rssi = le32_to_cpu(rssi_trig->rssi);
365e705c121SKalle Valo 
366e705c121SKalle Valo 			trig_check =
367e705c121SKalle Valo 				iwl_fw_dbg_trigger_check_stop(mvm, mvmsta->vif,
368e705c121SKalle Valo 							      trig);
369e705c121SKalle Valo 			if (trig_check && rx_status->signal < rssi)
370e705c121SKalle Valo 				iwl_mvm_fw_dbg_collect_trig(mvm, trig, NULL);
371e705c121SKalle Valo 		}
372e705c121SKalle Valo 	}
373e705c121SKalle Valo 
374e705c121SKalle Valo 	if (sta && ieee80211_is_data(hdr->frame_control))
375e705c121SKalle Valo 		iwl_mvm_rx_csum(sta, skb, rx_pkt_status);
376e705c121SKalle Valo 
377e705c121SKalle Valo 	rcu_read_unlock();
378e705c121SKalle Valo 
379e705c121SKalle Valo 	/* set the preamble flag if appropriate */
380e705c121SKalle Valo 	if (phy_info->phy_flags & cpu_to_le16(RX_RES_PHY_FLAGS_SHORT_PREAMBLE))
381e705c121SKalle Valo 		rx_status->flag |= RX_FLAG_SHORTPRE;
382e705c121SKalle Valo 
383e705c121SKalle Valo 	if (phy_info->phy_flags & cpu_to_le16(RX_RES_PHY_FLAGS_AGG)) {
384e705c121SKalle Valo 		/*
385e705c121SKalle Valo 		 * We know which subframes of an A-MPDU belong
386e705c121SKalle Valo 		 * together since we get a single PHY response
387e705c121SKalle Valo 		 * from the firmware for all of them
388e705c121SKalle Valo 		 */
389e705c121SKalle Valo 		rx_status->flag |= RX_FLAG_AMPDU_DETAILS;
390e705c121SKalle Valo 		rx_status->ampdu_reference = mvm->ampdu_ref;
391e705c121SKalle Valo 	}
392e705c121SKalle Valo 
393e705c121SKalle Valo 	/* Set up the HT phy flags */
394e705c121SKalle Valo 	switch (rate_n_flags & RATE_MCS_CHAN_WIDTH_MSK) {
395e705c121SKalle Valo 	case RATE_MCS_CHAN_WIDTH_20:
396e705c121SKalle Valo 		break;
397e705c121SKalle Valo 	case RATE_MCS_CHAN_WIDTH_40:
398e705c121SKalle Valo 		rx_status->flag |= RX_FLAG_40MHZ;
399e705c121SKalle Valo 		break;
400e705c121SKalle Valo 	case RATE_MCS_CHAN_WIDTH_80:
401e705c121SKalle Valo 		rx_status->vht_flag |= RX_VHT_FLAG_80MHZ;
402e705c121SKalle Valo 		break;
403e705c121SKalle Valo 	case RATE_MCS_CHAN_WIDTH_160:
404e705c121SKalle Valo 		rx_status->vht_flag |= RX_VHT_FLAG_160MHZ;
405e705c121SKalle Valo 		break;
406e705c121SKalle Valo 	}
407e705c121SKalle Valo 	if (rate_n_flags & RATE_MCS_SGI_MSK)
408e705c121SKalle Valo 		rx_status->flag |= RX_FLAG_SHORT_GI;
409e705c121SKalle Valo 	if (rate_n_flags & RATE_HT_MCS_GF_MSK)
410e705c121SKalle Valo 		rx_status->flag |= RX_FLAG_HT_GF;
411e705c121SKalle Valo 	if (rate_n_flags & RATE_MCS_LDPC_MSK)
412e705c121SKalle Valo 		rx_status->flag |= RX_FLAG_LDPC;
413e705c121SKalle Valo 	if (rate_n_flags & RATE_MCS_HT_MSK) {
414e705c121SKalle Valo 		u8 stbc = (rate_n_flags & RATE_MCS_HT_STBC_MSK) >>
415e705c121SKalle Valo 				RATE_MCS_STBC_POS;
416e705c121SKalle Valo 		rx_status->flag |= RX_FLAG_HT;
417e705c121SKalle Valo 		rx_status->rate_idx = rate_n_flags & RATE_HT_MCS_INDEX_MSK;
418e705c121SKalle Valo 		rx_status->flag |= stbc << RX_FLAG_STBC_SHIFT;
419e705c121SKalle Valo 	} else if (rate_n_flags & RATE_MCS_VHT_MSK) {
420e705c121SKalle Valo 		u8 stbc = (rate_n_flags & RATE_MCS_VHT_STBC_MSK) >>
421e705c121SKalle Valo 				RATE_MCS_STBC_POS;
422e705c121SKalle Valo 		rx_status->vht_nss =
423e705c121SKalle Valo 			((rate_n_flags & RATE_VHT_MCS_NSS_MSK) >>
424e705c121SKalle Valo 						RATE_VHT_MCS_NSS_POS) + 1;
425e705c121SKalle Valo 		rx_status->rate_idx = rate_n_flags & RATE_VHT_MCS_RATE_CODE_MSK;
426e705c121SKalle Valo 		rx_status->flag |= RX_FLAG_VHT;
427e705c121SKalle Valo 		rx_status->flag |= stbc << RX_FLAG_STBC_SHIFT;
428e705c121SKalle Valo 		if (rate_n_flags & RATE_MCS_BF_MSK)
429e705c121SKalle Valo 			rx_status->vht_flag |= RX_VHT_FLAG_BF;
430e705c121SKalle Valo 	} else {
431e705c121SKalle Valo 		rx_status->rate_idx =
432e705c121SKalle Valo 			iwl_mvm_legacy_rate_to_mac80211_idx(rate_n_flags,
433e705c121SKalle Valo 							    rx_status->band);
434e705c121SKalle Valo 	}
435e705c121SKalle Valo 
436e705c121SKalle Valo #ifdef CONFIG_IWLWIFI_DEBUGFS
437e705c121SKalle Valo 	iwl_mvm_update_frame_stats(mvm, rate_n_flags,
438e705c121SKalle Valo 				   rx_status->flag & RX_FLAG_AMPDU_DETAILS);
439e705c121SKalle Valo #endif
440e705c121SKalle Valo 	iwl_mvm_pass_packet_to_mac80211(mvm, napi, skb, hdr, len, ampdu_status,
441e705c121SKalle Valo 					crypt_len, rxb);
442e705c121SKalle Valo }
443e705c121SKalle Valo 
444e705c121SKalle Valo static void iwl_mvm_update_rx_statistics(struct iwl_mvm *mvm,
445e705c121SKalle Valo 					 struct mvm_statistics_rx *rx_stats)
446e705c121SKalle Valo {
447e705c121SKalle Valo 	lockdep_assert_held(&mvm->mutex);
448e705c121SKalle Valo 
449e705c121SKalle Valo 	mvm->rx_stats = *rx_stats;
450e705c121SKalle Valo }
451e705c121SKalle Valo 
452e705c121SKalle Valo struct iwl_mvm_stat_data {
453e705c121SKalle Valo 	struct iwl_mvm *mvm;
454e705c121SKalle Valo 	__le32 mac_id;
455e705c121SKalle Valo 	u8 beacon_filter_average_energy;
456e705c121SKalle Valo 	struct mvm_statistics_general_v8 *general;
457e705c121SKalle Valo };
458e705c121SKalle Valo 
459e705c121SKalle Valo static void iwl_mvm_stat_iterator(void *_data, u8 *mac,
460e705c121SKalle Valo 				  struct ieee80211_vif *vif)
461e705c121SKalle Valo {
462e705c121SKalle Valo 	struct iwl_mvm_stat_data *data = _data;
463e705c121SKalle Valo 	struct iwl_mvm *mvm = data->mvm;
464e705c121SKalle Valo 	int sig = -data->beacon_filter_average_energy;
465e705c121SKalle Valo 	int last_event;
466e705c121SKalle Valo 	int thold = vif->bss_conf.cqm_rssi_thold;
467e705c121SKalle Valo 	int hyst = vif->bss_conf.cqm_rssi_hyst;
468e705c121SKalle Valo 	u16 id = le32_to_cpu(data->mac_id);
469e705c121SKalle Valo 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
470e705c121SKalle Valo 
471e705c121SKalle Valo 	/* This doesn't need the MAC ID check since it's not taking the
472e705c121SKalle Valo 	 * data copied into the "data" struct, but rather the data from
473e705c121SKalle Valo 	 * the notification directly.
474e705c121SKalle Valo 	 */
475e705c121SKalle Valo 	if (data->general) {
476e705c121SKalle Valo 		mvmvif->beacon_stats.num_beacons =
477e705c121SKalle Valo 			le32_to_cpu(data->general->beacon_counter[mvmvif->id]);
478e705c121SKalle Valo 		mvmvif->beacon_stats.avg_signal =
479e705c121SKalle Valo 			-data->general->beacon_average_energy[mvmvif->id];
480e705c121SKalle Valo 	}
481e705c121SKalle Valo 
482e705c121SKalle Valo 	if (mvmvif->id != id)
483e705c121SKalle Valo 		return;
484e705c121SKalle Valo 
485e705c121SKalle Valo 	if (vif->type != NL80211_IFTYPE_STATION)
486e705c121SKalle Valo 		return;
487e705c121SKalle Valo 
488e705c121SKalle Valo 	if (sig == 0) {
489e705c121SKalle Valo 		IWL_DEBUG_RX(mvm, "RSSI is 0 - skip signal based decision\n");
490e705c121SKalle Valo 		return;
491e705c121SKalle Valo 	}
492e705c121SKalle Valo 
493e705c121SKalle Valo 	mvmvif->bf_data.ave_beacon_signal = sig;
494e705c121SKalle Valo 
495e705c121SKalle Valo 	/* BT Coex */
496e705c121SKalle Valo 	if (mvmvif->bf_data.bt_coex_min_thold !=
497e705c121SKalle Valo 	    mvmvif->bf_data.bt_coex_max_thold) {
498e705c121SKalle Valo 		last_event = mvmvif->bf_data.last_bt_coex_event;
499e705c121SKalle Valo 		if (sig > mvmvif->bf_data.bt_coex_max_thold &&
500e705c121SKalle Valo 		    (last_event <= mvmvif->bf_data.bt_coex_min_thold ||
501e705c121SKalle Valo 		     last_event == 0)) {
502e705c121SKalle Valo 			mvmvif->bf_data.last_bt_coex_event = sig;
503e705c121SKalle Valo 			IWL_DEBUG_RX(mvm, "cqm_iterator bt coex high %d\n",
504e705c121SKalle Valo 				     sig);
505e705c121SKalle Valo 			iwl_mvm_bt_rssi_event(mvm, vif, RSSI_EVENT_HIGH);
506e705c121SKalle Valo 		} else if (sig < mvmvif->bf_data.bt_coex_min_thold &&
507e705c121SKalle Valo 			   (last_event >= mvmvif->bf_data.bt_coex_max_thold ||
508e705c121SKalle Valo 			    last_event == 0)) {
509e705c121SKalle Valo 			mvmvif->bf_data.last_bt_coex_event = sig;
510e705c121SKalle Valo 			IWL_DEBUG_RX(mvm, "cqm_iterator bt coex low %d\n",
511e705c121SKalle Valo 				     sig);
512e705c121SKalle Valo 			iwl_mvm_bt_rssi_event(mvm, vif, RSSI_EVENT_LOW);
513e705c121SKalle Valo 		}
514e705c121SKalle Valo 	}
515e705c121SKalle Valo 
516e705c121SKalle Valo 	if (!(vif->driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI))
517e705c121SKalle Valo 		return;
518e705c121SKalle Valo 
519e705c121SKalle Valo 	/* CQM Notification */
520e705c121SKalle Valo 	last_event = mvmvif->bf_data.last_cqm_event;
521e705c121SKalle Valo 	if (thold && sig < thold && (last_event == 0 ||
522e705c121SKalle Valo 				     sig < last_event - hyst)) {
523e705c121SKalle Valo 		mvmvif->bf_data.last_cqm_event = sig;
524e705c121SKalle Valo 		IWL_DEBUG_RX(mvm, "cqm_iterator cqm low %d\n",
525e705c121SKalle Valo 			     sig);
526e705c121SKalle Valo 		ieee80211_cqm_rssi_notify(
527e705c121SKalle Valo 			vif,
528e705c121SKalle Valo 			NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
529e705c121SKalle Valo 			GFP_KERNEL);
530e705c121SKalle Valo 	} else if (sig > thold &&
531e705c121SKalle Valo 		   (last_event == 0 || sig > last_event + hyst)) {
532e705c121SKalle Valo 		mvmvif->bf_data.last_cqm_event = sig;
533e705c121SKalle Valo 		IWL_DEBUG_RX(mvm, "cqm_iterator cqm high %d\n",
534e705c121SKalle Valo 			     sig);
535e705c121SKalle Valo 		ieee80211_cqm_rssi_notify(
536e705c121SKalle Valo 			vif,
537e705c121SKalle Valo 			NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
538e705c121SKalle Valo 			GFP_KERNEL);
539e705c121SKalle Valo 	}
540e705c121SKalle Valo }
541e705c121SKalle Valo 
542e705c121SKalle Valo static inline void
543e705c121SKalle Valo iwl_mvm_rx_stats_check_trigger(struct iwl_mvm *mvm, struct iwl_rx_packet *pkt)
544e705c121SKalle Valo {
545e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_tlv *trig;
546e705c121SKalle Valo 	struct iwl_fw_dbg_trigger_stats *trig_stats;
547e705c121SKalle Valo 	u32 trig_offset, trig_thold;
548e705c121SKalle Valo 
549e705c121SKalle Valo 	if (!iwl_fw_dbg_trigger_enabled(mvm->fw, FW_DBG_TRIGGER_STATS))
550e705c121SKalle Valo 		return;
551e705c121SKalle Valo 
552e705c121SKalle Valo 	trig = iwl_fw_dbg_get_trigger(mvm->fw, FW_DBG_TRIGGER_STATS);
553e705c121SKalle Valo 	trig_stats = (void *)trig->data;
554e705c121SKalle Valo 
555e705c121SKalle Valo 	if (!iwl_fw_dbg_trigger_check_stop(mvm, NULL, trig))
556e705c121SKalle Valo 		return;
557e705c121SKalle Valo 
558e705c121SKalle Valo 	trig_offset = le32_to_cpu(trig_stats->stop_offset);
559e705c121SKalle Valo 	trig_thold = le32_to_cpu(trig_stats->stop_threshold);
560e705c121SKalle Valo 
561e705c121SKalle Valo 	if (WARN_ON_ONCE(trig_offset >= iwl_rx_packet_payload_len(pkt)))
562e705c121SKalle Valo 		return;
563e705c121SKalle Valo 
564e705c121SKalle Valo 	if (le32_to_cpup((__le32 *) (pkt->data + trig_offset)) < trig_thold)
565e705c121SKalle Valo 		return;
566e705c121SKalle Valo 
567e705c121SKalle Valo 	iwl_mvm_fw_dbg_collect_trig(mvm, trig, NULL);
568e705c121SKalle Valo }
569e705c121SKalle Valo 
570e705c121SKalle Valo void iwl_mvm_handle_rx_statistics(struct iwl_mvm *mvm,
571e705c121SKalle Valo 				  struct iwl_rx_packet *pkt)
572e705c121SKalle Valo {
573e705c121SKalle Valo 	struct iwl_notif_statistics_v10 *stats = (void *)&pkt->data;
574e705c121SKalle Valo 	struct iwl_mvm_stat_data data = {
575e705c121SKalle Valo 		.mvm = mvm,
576e705c121SKalle Valo 	};
577e705c121SKalle Valo 	u32 temperature;
578e705c121SKalle Valo 
579e705c121SKalle Valo 	if (iwl_rx_packet_payload_len(pkt) != sizeof(*stats))
580e705c121SKalle Valo 		goto invalid;
581e705c121SKalle Valo 
582e705c121SKalle Valo 	temperature = le32_to_cpu(stats->general.radio_temperature);
583e705c121SKalle Valo 	data.mac_id = stats->rx.general.mac_id;
584e705c121SKalle Valo 	data.beacon_filter_average_energy =
585e705c121SKalle Valo 		stats->general.beacon_filter_average_energy;
586e705c121SKalle Valo 
587e705c121SKalle Valo 	iwl_mvm_update_rx_statistics(mvm, &stats->rx);
588e705c121SKalle Valo 
589e705c121SKalle Valo 	mvm->radio_stats.rx_time = le64_to_cpu(stats->general.rx_time);
590e705c121SKalle Valo 	mvm->radio_stats.tx_time = le64_to_cpu(stats->general.tx_time);
591e705c121SKalle Valo 	mvm->radio_stats.on_time_rf =
592e705c121SKalle Valo 		le64_to_cpu(stats->general.on_time_rf);
593e705c121SKalle Valo 	mvm->radio_stats.on_time_scan =
594e705c121SKalle Valo 		le64_to_cpu(stats->general.on_time_scan);
595e705c121SKalle Valo 
596e705c121SKalle Valo 	data.general = &stats->general;
597e705c121SKalle Valo 
598e705c121SKalle Valo 	iwl_mvm_rx_stats_check_trigger(mvm, pkt);
599e705c121SKalle Valo 
600e705c121SKalle Valo 	ieee80211_iterate_active_interfaces(mvm->hw,
601e705c121SKalle Valo 					    IEEE80211_IFACE_ITER_NORMAL,
602e705c121SKalle Valo 					    iwl_mvm_stat_iterator,
603e705c121SKalle Valo 					    &data);
604e705c121SKalle Valo 	return;
605e705c121SKalle Valo  invalid:
606e705c121SKalle Valo 	IWL_ERR(mvm, "received invalid statistics size (%d)!\n",
607e705c121SKalle Valo 		iwl_rx_packet_payload_len(pkt));
608e705c121SKalle Valo }
609e705c121SKalle Valo 
610e705c121SKalle Valo void iwl_mvm_rx_statistics(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
611e705c121SKalle Valo {
612e705c121SKalle Valo 	iwl_mvm_handle_rx_statistics(mvm, rxb_addr(rxb));
613e705c121SKalle Valo }
614