1 /******************************************************************************
2  *
3  * This file is provided under a dual BSD/GPLv2 license.  When using or
4  * redistributing this file, you may do so under either license.
5  *
6  * GPL LICENSE SUMMARY
7  *
8  * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
9  * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
10  * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
11  * Copyright(c) 2018 - 2020 Intel Corporation
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of version 2 of the GNU General Public License as
15  * published by the Free Software Foundation.
16  *
17  * This program is distributed in the hope that it will be useful, but
18  * WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * General Public License for more details.
21  *
22  * The full GNU General Public License is included in this distribution
23  * in the file called COPYING.
24  *
25  * Contact Information:
26  *  Intel Linux Wireless <linuxwifi@intel.com>
27  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
28  *
29  * BSD LICENSE
30  *
31  * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
32  * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
33  * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
34  * Copyright(c) 2018 - 2020 Intel Corporation
35  * All rights reserved.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
40  *
41  *  * Redistributions of source code must retain the above copyright
42  *    notice, this list of conditions and the following disclaimer.
43  *  * Redistributions in binary form must reproduce the above copyright
44  *    notice, this list of conditions and the following disclaimer in
45  *    the documentation and/or other materials provided with the
46  *    distribution.
47  *  * Neither the name Intel Corporation nor the names of its
48  *    contributors may be used to endorse or promote products derived
49  *    from this software without specific prior written permission.
50  *
51  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
52  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
53  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
54  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
55  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
56  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
57  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
58  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
59  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
60  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
61  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62  *****************************************************************************/
63 #include <asm/unaligned.h>
64 #include <linux/etherdevice.h>
65 #include <linux/skbuff.h>
66 #include "iwl-trans.h"
67 #include "mvm.h"
68 #include "fw-api.h"
69 
70 /*
71  * iwl_mvm_rx_rx_phy_cmd - REPLY_RX_PHY_CMD handler
72  *
73  * Copies the phy information in mvm->last_phy_info, it will be used when the
74  * actual data will come from the fw in the next packet.
75  */
76 void iwl_mvm_rx_rx_phy_cmd(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
77 {
78 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
79 
80 	memcpy(&mvm->last_phy_info, pkt->data, sizeof(mvm->last_phy_info));
81 	mvm->ampdu_ref++;
82 
83 #ifdef CONFIG_IWLWIFI_DEBUGFS
84 	if (mvm->last_phy_info.phy_flags & cpu_to_le16(RX_RES_PHY_FLAGS_AGG)) {
85 		spin_lock(&mvm->drv_stats_lock);
86 		mvm->drv_rx_stats.ampdu_count++;
87 		spin_unlock(&mvm->drv_stats_lock);
88 	}
89 #endif
90 }
91 
92 /*
93  * iwl_mvm_pass_packet_to_mac80211 - builds the packet for mac80211
94  *
95  * Adds the rxb to a new skb and give it to mac80211
96  */
97 static void iwl_mvm_pass_packet_to_mac80211(struct iwl_mvm *mvm,
98 					    struct ieee80211_sta *sta,
99 					    struct napi_struct *napi,
100 					    struct sk_buff *skb,
101 					    struct ieee80211_hdr *hdr, u16 len,
102 					    u8 crypt_len,
103 					    struct iwl_rx_cmd_buffer *rxb)
104 {
105 	unsigned int hdrlen = ieee80211_hdrlen(hdr->frame_control);
106 	unsigned int fraglen;
107 
108 	/*
109 	 * The 'hdrlen' (plus the 8 bytes for the SNAP and the crypt_len,
110 	 * but those are all multiples of 4 long) all goes away, but we
111 	 * want the *end* of it, which is going to be the start of the IP
112 	 * header, to be aligned when it gets pulled in.
113 	 * The beginning of the skb->data is aligned on at least a 4-byte
114 	 * boundary after allocation. Everything here is aligned at least
115 	 * on a 2-byte boundary so we can just take hdrlen & 3 and pad by
116 	 * the result.
117 	 */
118 	skb_reserve(skb, hdrlen & 3);
119 
120 	/* If frame is small enough to fit in skb->head, pull it completely.
121 	 * If not, only pull ieee80211_hdr (including crypto if present, and
122 	 * an additional 8 bytes for SNAP/ethertype, see below) so that
123 	 * splice() or TCP coalesce are more efficient.
124 	 *
125 	 * Since, in addition, ieee80211_data_to_8023() always pull in at
126 	 * least 8 bytes (possibly more for mesh) we can do the same here
127 	 * to save the cost of doing it later. That still doesn't pull in
128 	 * the actual IP header since the typical case has a SNAP header.
129 	 * If the latter changes (there are efforts in the standards group
130 	 * to do so) we should revisit this and ieee80211_data_to_8023().
131 	 */
132 	hdrlen = (len <= skb_tailroom(skb)) ? len : hdrlen + crypt_len + 8;
133 
134 	skb_put_data(skb, hdr, hdrlen);
135 	fraglen = len - hdrlen;
136 
137 	if (fraglen) {
138 		int offset = (void *)hdr + hdrlen -
139 			     rxb_addr(rxb) + rxb_offset(rxb);
140 
141 		skb_add_rx_frag(skb, 0, rxb_steal_page(rxb), offset,
142 				fraglen, rxb->truesize);
143 	}
144 
145 	ieee80211_rx_napi(mvm->hw, sta, skb, napi);
146 }
147 
148 /*
149  * iwl_mvm_get_signal_strength - use new rx PHY INFO API
150  * values are reported by the fw as positive values - need to negate
151  * to obtain their dBM.  Account for missing antennas by replacing 0
152  * values by -256dBm: practically 0 power and a non-feasible 8 bit value.
153  */
154 static void iwl_mvm_get_signal_strength(struct iwl_mvm *mvm,
155 					struct iwl_rx_phy_info *phy_info,
156 					struct ieee80211_rx_status *rx_status)
157 {
158 	int energy_a, energy_b, energy_c, max_energy;
159 	u32 val;
160 
161 	val =
162 	    le32_to_cpu(phy_info->non_cfg_phy[IWL_RX_INFO_ENERGY_ANT_ABC_IDX]);
163 	energy_a = (val & IWL_RX_INFO_ENERGY_ANT_A_MSK) >>
164 						IWL_RX_INFO_ENERGY_ANT_A_POS;
165 	energy_a = energy_a ? -energy_a : S8_MIN;
166 	energy_b = (val & IWL_RX_INFO_ENERGY_ANT_B_MSK) >>
167 						IWL_RX_INFO_ENERGY_ANT_B_POS;
168 	energy_b = energy_b ? -energy_b : S8_MIN;
169 	energy_c = (val & IWL_RX_INFO_ENERGY_ANT_C_MSK) >>
170 						IWL_RX_INFO_ENERGY_ANT_C_POS;
171 	energy_c = energy_c ? -energy_c : S8_MIN;
172 	max_energy = max(energy_a, energy_b);
173 	max_energy = max(max_energy, energy_c);
174 
175 	IWL_DEBUG_STATS(mvm, "energy In A %d B %d C %d , and max %d\n",
176 			energy_a, energy_b, energy_c, max_energy);
177 
178 	rx_status->signal = max_energy;
179 	rx_status->chains = (le16_to_cpu(phy_info->phy_flags) &
180 				RX_RES_PHY_FLAGS_ANTENNA)
181 					>> RX_RES_PHY_FLAGS_ANTENNA_POS;
182 	rx_status->chain_signal[0] = energy_a;
183 	rx_status->chain_signal[1] = energy_b;
184 	rx_status->chain_signal[2] = energy_c;
185 }
186 
187 /*
188  * iwl_mvm_set_mac80211_rx_flag - translate fw status to mac80211 format
189  * @mvm: the mvm object
190  * @hdr: 80211 header
191  * @stats: status in mac80211's format
192  * @rx_pkt_status: status coming from fw
193  *
194  * returns non 0 value if the packet should be dropped
195  */
196 static u32 iwl_mvm_set_mac80211_rx_flag(struct iwl_mvm *mvm,
197 					struct ieee80211_hdr *hdr,
198 					struct ieee80211_rx_status *stats,
199 					u32 rx_pkt_status,
200 					u8 *crypt_len)
201 {
202 	if (!ieee80211_has_protected(hdr->frame_control) ||
203 	    (rx_pkt_status & RX_MPDU_RES_STATUS_SEC_ENC_MSK) ==
204 			     RX_MPDU_RES_STATUS_SEC_NO_ENC)
205 		return 0;
206 
207 	/* packet was encrypted with unknown alg */
208 	if ((rx_pkt_status & RX_MPDU_RES_STATUS_SEC_ENC_MSK) ==
209 					RX_MPDU_RES_STATUS_SEC_ENC_ERR)
210 		return 0;
211 
212 	switch (rx_pkt_status & RX_MPDU_RES_STATUS_SEC_ENC_MSK) {
213 	case RX_MPDU_RES_STATUS_SEC_CCM_ENC:
214 		/* alg is CCM: check MIC only */
215 		if (!(rx_pkt_status & RX_MPDU_RES_STATUS_MIC_OK))
216 			return -1;
217 
218 		stats->flag |= RX_FLAG_DECRYPTED;
219 		*crypt_len = IEEE80211_CCMP_HDR_LEN;
220 		return 0;
221 
222 	case RX_MPDU_RES_STATUS_SEC_TKIP_ENC:
223 		/* Don't drop the frame and decrypt it in SW */
224 		if (!fw_has_api(&mvm->fw->ucode_capa,
225 				IWL_UCODE_TLV_API_DEPRECATE_TTAK) &&
226 		    !(rx_pkt_status & RX_MPDU_RES_STATUS_TTAK_OK))
227 			return 0;
228 		*crypt_len = IEEE80211_TKIP_IV_LEN;
229 		/* fall through */
230 
231 	case RX_MPDU_RES_STATUS_SEC_WEP_ENC:
232 		if (!(rx_pkt_status & RX_MPDU_RES_STATUS_ICV_OK))
233 			return -1;
234 
235 		stats->flag |= RX_FLAG_DECRYPTED;
236 		if ((rx_pkt_status & RX_MPDU_RES_STATUS_SEC_ENC_MSK) ==
237 				RX_MPDU_RES_STATUS_SEC_WEP_ENC)
238 			*crypt_len = IEEE80211_WEP_IV_LEN;
239 		return 0;
240 
241 	case RX_MPDU_RES_STATUS_SEC_EXT_ENC:
242 		if (!(rx_pkt_status & RX_MPDU_RES_STATUS_MIC_OK))
243 			return -1;
244 		stats->flag |= RX_FLAG_DECRYPTED;
245 		return 0;
246 
247 	default:
248 		/* Expected in monitor (not having the keys) */
249 		if (!mvm->monitor_on)
250 			IWL_ERR(mvm, "Unhandled alg: 0x%x\n", rx_pkt_status);
251 	}
252 
253 	return 0;
254 }
255 
256 static void iwl_mvm_rx_handle_tcm(struct iwl_mvm *mvm,
257 				  struct ieee80211_sta *sta,
258 				  struct ieee80211_hdr *hdr, u32 len,
259 				  struct iwl_rx_phy_info *phy_info,
260 				  u32 rate_n_flags)
261 {
262 	struct iwl_mvm_sta *mvmsta;
263 	struct iwl_mvm_tcm_mac *mdata;
264 	int mac;
265 	int ac = IEEE80211_AC_BE; /* treat non-QoS as BE */
266 	struct iwl_mvm_vif *mvmvif;
267 	/* expected throughput in 100Kbps, single stream, 20 MHz */
268 	static const u8 thresh_tpt[] = {
269 		9, 18, 30, 42, 60, 78, 90, 96, 120, 135,
270 	};
271 	u16 thr;
272 
273 	if (ieee80211_is_data_qos(hdr->frame_control))
274 		ac = tid_to_mac80211_ac[ieee80211_get_tid(hdr)];
275 
276 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
277 	mac = mvmsta->mac_id_n_color & FW_CTXT_ID_MSK;
278 
279 	if (time_after(jiffies, mvm->tcm.ts + MVM_TCM_PERIOD))
280 		schedule_delayed_work(&mvm->tcm.work, 0);
281 	mdata = &mvm->tcm.data[mac];
282 	mdata->rx.pkts[ac]++;
283 
284 	/* count the airtime only once for each ampdu */
285 	if (mdata->rx.last_ampdu_ref != mvm->ampdu_ref) {
286 		mdata->rx.last_ampdu_ref = mvm->ampdu_ref;
287 		mdata->rx.airtime += le16_to_cpu(phy_info->frame_time);
288 	}
289 
290 	if (!(rate_n_flags & (RATE_MCS_HT_MSK | RATE_MCS_VHT_MSK)))
291 		return;
292 
293 	mvmvif = iwl_mvm_vif_from_mac80211(mvmsta->vif);
294 
295 	if (mdata->opened_rx_ba_sessions ||
296 	    mdata->uapsd_nonagg_detect.detected ||
297 	    (!mvmvif->queue_params[IEEE80211_AC_VO].uapsd &&
298 	     !mvmvif->queue_params[IEEE80211_AC_VI].uapsd &&
299 	     !mvmvif->queue_params[IEEE80211_AC_BE].uapsd &&
300 	     !mvmvif->queue_params[IEEE80211_AC_BK].uapsd) ||
301 	    mvmsta->sta_id != mvmvif->ap_sta_id)
302 		return;
303 
304 	if (rate_n_flags & RATE_MCS_HT_MSK) {
305 		thr = thresh_tpt[rate_n_flags & RATE_HT_MCS_RATE_CODE_MSK];
306 		thr *= 1 + ((rate_n_flags & RATE_HT_MCS_NSS_MSK) >>
307 					RATE_HT_MCS_NSS_POS);
308 	} else {
309 		if (WARN_ON((rate_n_flags & RATE_VHT_MCS_RATE_CODE_MSK) >=
310 				ARRAY_SIZE(thresh_tpt)))
311 			return;
312 		thr = thresh_tpt[rate_n_flags & RATE_VHT_MCS_RATE_CODE_MSK];
313 		thr *= 1 + ((rate_n_flags & RATE_VHT_MCS_NSS_MSK) >>
314 					RATE_VHT_MCS_NSS_POS);
315 	}
316 
317 	thr <<= ((rate_n_flags & RATE_MCS_CHAN_WIDTH_MSK) >>
318 				RATE_MCS_CHAN_WIDTH_POS);
319 
320 	mdata->uapsd_nonagg_detect.rx_bytes += len;
321 	ewma_rate_add(&mdata->uapsd_nonagg_detect.rate, thr);
322 }
323 
324 static void iwl_mvm_rx_csum(struct ieee80211_sta *sta,
325 			    struct sk_buff *skb,
326 			    u32 status)
327 {
328 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
329 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(mvmsta->vif);
330 
331 	if (mvmvif->features & NETIF_F_RXCSUM &&
332 	    status & RX_MPDU_RES_STATUS_CSUM_DONE &&
333 	    status & RX_MPDU_RES_STATUS_CSUM_OK)
334 		skb->ip_summed = CHECKSUM_UNNECESSARY;
335 }
336 
337 /*
338  * iwl_mvm_rx_rx_mpdu - REPLY_RX_MPDU_CMD handler
339  *
340  * Handles the actual data of the Rx packet from the fw
341  */
342 void iwl_mvm_rx_rx_mpdu(struct iwl_mvm *mvm, struct napi_struct *napi,
343 			struct iwl_rx_cmd_buffer *rxb)
344 {
345 	struct ieee80211_hdr *hdr;
346 	struct ieee80211_rx_status *rx_status;
347 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
348 	struct iwl_rx_phy_info *phy_info;
349 	struct iwl_rx_mpdu_res_start *rx_res;
350 	struct ieee80211_sta *sta = NULL;
351 	struct sk_buff *skb;
352 	u32 len, pkt_len = iwl_rx_packet_payload_len(pkt);
353 	u32 rate_n_flags;
354 	u32 rx_pkt_status;
355 	u8 crypt_len = 0;
356 
357 	phy_info = &mvm->last_phy_info;
358 	rx_res = (struct iwl_rx_mpdu_res_start *)pkt->data;
359 	hdr = (struct ieee80211_hdr *)(pkt->data + sizeof(*rx_res));
360 	len = le16_to_cpu(rx_res->byte_count);
361 
362 	if (unlikely(len + sizeof(*rx_res) + sizeof(__le32) > pkt_len)) {
363 		IWL_DEBUG_DROP(mvm, "FW lied about packet len\n");
364 		return;
365 	}
366 
367 	rx_pkt_status = get_unaligned_le32((__le32 *)
368 		(pkt->data + sizeof(*rx_res) + len));
369 
370 	/* Dont use dev_alloc_skb(), we'll have enough headroom once
371 	 * ieee80211_hdr pulled.
372 	 */
373 	skb = alloc_skb(128, GFP_ATOMIC);
374 	if (!skb) {
375 		IWL_ERR(mvm, "alloc_skb failed\n");
376 		return;
377 	}
378 
379 	rx_status = IEEE80211_SKB_RXCB(skb);
380 
381 	/*
382 	 * drop the packet if it has failed being decrypted by HW
383 	 */
384 	if (iwl_mvm_set_mac80211_rx_flag(mvm, hdr, rx_status, rx_pkt_status,
385 					 &crypt_len)) {
386 		IWL_DEBUG_DROP(mvm, "Bad decryption results 0x%08x\n",
387 			       rx_pkt_status);
388 		kfree_skb(skb);
389 		return;
390 	}
391 
392 	/*
393 	 * Keep packets with CRC errors (and with overrun) for monitor mode
394 	 * (otherwise the firmware discards them) but mark them as bad.
395 	 */
396 	if (!(rx_pkt_status & RX_MPDU_RES_STATUS_CRC_OK) ||
397 	    !(rx_pkt_status & RX_MPDU_RES_STATUS_OVERRUN_OK)) {
398 		IWL_DEBUG_RX(mvm, "Bad CRC or FIFO: 0x%08X.\n", rx_pkt_status);
399 		rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
400 	}
401 
402 	/* This will be used in several places later */
403 	rate_n_flags = le32_to_cpu(phy_info->rate_n_flags);
404 
405 	/* rx_status carries information about the packet to mac80211 */
406 	rx_status->mactime = le64_to_cpu(phy_info->timestamp);
407 	rx_status->device_timestamp = le32_to_cpu(phy_info->system_timestamp);
408 	rx_status->band =
409 		(phy_info->phy_flags & cpu_to_le16(RX_RES_PHY_FLAGS_BAND_24)) ?
410 				NL80211_BAND_2GHZ : NL80211_BAND_5GHZ;
411 	rx_status->freq =
412 		ieee80211_channel_to_frequency(le16_to_cpu(phy_info->channel),
413 					       rx_status->band);
414 
415 	/* TSF as indicated by the firmware  is at INA time */
416 	rx_status->flag |= RX_FLAG_MACTIME_PLCP_START;
417 
418 	iwl_mvm_get_signal_strength(mvm, phy_info, rx_status);
419 
420 	IWL_DEBUG_STATS_LIMIT(mvm, "Rssi %d, TSF %llu\n", rx_status->signal,
421 			      (unsigned long long)rx_status->mactime);
422 
423 	rcu_read_lock();
424 	if (rx_pkt_status & RX_MPDU_RES_STATUS_SRC_STA_FOUND) {
425 		u32 id = rx_pkt_status & RX_MPDU_RES_STATUS_STA_ID_MSK;
426 
427 		id >>= RX_MDPU_RES_STATUS_STA_ID_SHIFT;
428 
429 		if (!WARN_ON_ONCE(id >= mvm->fw->ucode_capa.num_stations)) {
430 			sta = rcu_dereference(mvm->fw_id_to_mac_id[id]);
431 			if (IS_ERR(sta))
432 				sta = NULL;
433 		}
434 	} else if (!is_multicast_ether_addr(hdr->addr2)) {
435 		/* This is fine since we prevent two stations with the same
436 		 * address from being added.
437 		 */
438 		sta = ieee80211_find_sta_by_ifaddr(mvm->hw, hdr->addr2, NULL);
439 	}
440 
441 	if (sta) {
442 		struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
443 		struct ieee80211_vif *tx_blocked_vif =
444 			rcu_dereference(mvm->csa_tx_blocked_vif);
445 		struct iwl_fw_dbg_trigger_tlv *trig;
446 		struct ieee80211_vif *vif = mvmsta->vif;
447 
448 		/* We have tx blocked stations (with CS bit). If we heard
449 		 * frames from a blocked station on a new channel we can
450 		 * TX to it again.
451 		 */
452 		if (unlikely(tx_blocked_vif) && vif == tx_blocked_vif) {
453 			struct iwl_mvm_vif *mvmvif =
454 				iwl_mvm_vif_from_mac80211(tx_blocked_vif);
455 
456 			if (mvmvif->csa_target_freq == rx_status->freq)
457 				iwl_mvm_sta_modify_disable_tx_ap(mvm, sta,
458 								 false);
459 		}
460 
461 		rs_update_last_rssi(mvm, mvmsta, rx_status);
462 
463 		trig = iwl_fw_dbg_trigger_on(&mvm->fwrt,
464 					     ieee80211_vif_to_wdev(vif),
465 					     FW_DBG_TRIGGER_RSSI);
466 
467 		if (trig && ieee80211_is_beacon(hdr->frame_control)) {
468 			struct iwl_fw_dbg_trigger_low_rssi *rssi_trig;
469 			s32 rssi;
470 
471 			rssi_trig = (void *)trig->data;
472 			rssi = le32_to_cpu(rssi_trig->rssi);
473 
474 			if (rx_status->signal < rssi)
475 				iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
476 							NULL);
477 		}
478 
479 		if (!mvm->tcm.paused && len >= sizeof(*hdr) &&
480 		    !is_multicast_ether_addr(hdr->addr1) &&
481 		    ieee80211_is_data(hdr->frame_control))
482 			iwl_mvm_rx_handle_tcm(mvm, sta, hdr, len, phy_info,
483 					      rate_n_flags);
484 
485 		if (ieee80211_is_data(hdr->frame_control))
486 			iwl_mvm_rx_csum(sta, skb, rx_pkt_status);
487 	}
488 	rcu_read_unlock();
489 
490 	/* set the preamble flag if appropriate */
491 	if (phy_info->phy_flags & cpu_to_le16(RX_RES_PHY_FLAGS_SHORT_PREAMBLE))
492 		rx_status->enc_flags |= RX_ENC_FLAG_SHORTPRE;
493 
494 	if (phy_info->phy_flags & cpu_to_le16(RX_RES_PHY_FLAGS_AGG)) {
495 		/*
496 		 * We know which subframes of an A-MPDU belong
497 		 * together since we get a single PHY response
498 		 * from the firmware for all of them
499 		 */
500 		rx_status->flag |= RX_FLAG_AMPDU_DETAILS;
501 		rx_status->ampdu_reference = mvm->ampdu_ref;
502 	}
503 
504 	/* Set up the HT phy flags */
505 	switch (rate_n_flags & RATE_MCS_CHAN_WIDTH_MSK) {
506 	case RATE_MCS_CHAN_WIDTH_20:
507 		break;
508 	case RATE_MCS_CHAN_WIDTH_40:
509 		rx_status->bw = RATE_INFO_BW_40;
510 		break;
511 	case RATE_MCS_CHAN_WIDTH_80:
512 		rx_status->bw = RATE_INFO_BW_80;
513 		break;
514 	case RATE_MCS_CHAN_WIDTH_160:
515 		rx_status->bw = RATE_INFO_BW_160;
516 		break;
517 	}
518 	if (!(rate_n_flags & RATE_MCS_CCK_MSK) &&
519 	    rate_n_flags & RATE_MCS_SGI_MSK)
520 		rx_status->enc_flags |= RX_ENC_FLAG_SHORT_GI;
521 	if (rate_n_flags & RATE_HT_MCS_GF_MSK)
522 		rx_status->enc_flags |= RX_ENC_FLAG_HT_GF;
523 	if (rate_n_flags & RATE_MCS_LDPC_MSK)
524 		rx_status->enc_flags |= RX_ENC_FLAG_LDPC;
525 	if (rate_n_flags & RATE_MCS_HT_MSK) {
526 		u8 stbc = (rate_n_flags & RATE_MCS_STBC_MSK) >>
527 				RATE_MCS_STBC_POS;
528 		rx_status->encoding = RX_ENC_HT;
529 		rx_status->rate_idx = rate_n_flags & RATE_HT_MCS_INDEX_MSK;
530 		rx_status->enc_flags |= stbc << RX_ENC_FLAG_STBC_SHIFT;
531 	} else if (rate_n_flags & RATE_MCS_VHT_MSK) {
532 		u8 stbc = (rate_n_flags & RATE_MCS_STBC_MSK) >>
533 				RATE_MCS_STBC_POS;
534 		rx_status->nss =
535 			((rate_n_flags & RATE_VHT_MCS_NSS_MSK) >>
536 						RATE_VHT_MCS_NSS_POS) + 1;
537 		rx_status->rate_idx = rate_n_flags & RATE_VHT_MCS_RATE_CODE_MSK;
538 		rx_status->encoding = RX_ENC_VHT;
539 		rx_status->enc_flags |= stbc << RX_ENC_FLAG_STBC_SHIFT;
540 		if (rate_n_flags & RATE_MCS_BF_MSK)
541 			rx_status->enc_flags |= RX_ENC_FLAG_BF;
542 	} else {
543 		int rate = iwl_mvm_legacy_rate_to_mac80211_idx(rate_n_flags,
544 							       rx_status->band);
545 
546 		if (WARN(rate < 0 || rate > 0xFF,
547 			 "Invalid rate flags 0x%x, band %d,\n",
548 			 rate_n_flags, rx_status->band)) {
549 			kfree_skb(skb);
550 			return;
551 		}
552 		rx_status->rate_idx = rate;
553 	}
554 
555 #ifdef CONFIG_IWLWIFI_DEBUGFS
556 	iwl_mvm_update_frame_stats(mvm, rate_n_flags,
557 				   rx_status->flag & RX_FLAG_AMPDU_DETAILS);
558 #endif
559 
560 	if (unlikely((ieee80211_is_beacon(hdr->frame_control) ||
561 		      ieee80211_is_probe_resp(hdr->frame_control)) &&
562 		     mvm->sched_scan_pass_all == SCHED_SCAN_PASS_ALL_ENABLED))
563 		mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_FOUND;
564 
565 	if (unlikely(ieee80211_is_beacon(hdr->frame_control) ||
566 		     ieee80211_is_probe_resp(hdr->frame_control)))
567 		rx_status->boottime_ns = ktime_get_boottime_ns();
568 
569 	iwl_mvm_pass_packet_to_mac80211(mvm, sta, napi, skb, hdr, len,
570 					crypt_len, rxb);
571 }
572 
573 struct iwl_mvm_stat_data {
574 	struct iwl_mvm *mvm;
575 	__le32 flags;
576 	__le32 mac_id;
577 	u8 beacon_filter_average_energy;
578 	__le32 *beacon_counter;
579 	u8 *beacon_average_energy;
580 };
581 
582 static void iwl_mvm_stat_iterator(void *_data, u8 *mac,
583 				  struct ieee80211_vif *vif)
584 {
585 	struct iwl_mvm_stat_data *data = _data;
586 	struct iwl_mvm *mvm = data->mvm;
587 	int sig = -data->beacon_filter_average_energy;
588 	int last_event;
589 	int thold = vif->bss_conf.cqm_rssi_thold;
590 	int hyst = vif->bss_conf.cqm_rssi_hyst;
591 	u16 id = le32_to_cpu(data->mac_id);
592 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
593 	u16 vif_id = mvmvif->id;
594 
595 	/* This doesn't need the MAC ID check since it's not taking the
596 	 * data copied into the "data" struct, but rather the data from
597 	 * the notification directly.
598 	 */
599 	mvmvif->beacon_stats.num_beacons =
600 		le32_to_cpu(data->beacon_counter[vif_id]);
601 	mvmvif->beacon_stats.avg_signal =
602 		-data->beacon_average_energy[vif_id];
603 
604 	/* make sure that beacon statistics don't go backwards with TCM
605 	 * request to clear statistics
606 	 */
607 	if (le32_to_cpu(data->flags) & IWL_STATISTICS_REPLY_FLG_CLEAR)
608 		mvmvif->beacon_stats.accu_num_beacons +=
609 			mvmvif->beacon_stats.num_beacons;
610 
611 	if (mvmvif->id != id)
612 		return;
613 
614 	if (vif->type != NL80211_IFTYPE_STATION)
615 		return;
616 
617 	if (sig == 0) {
618 		IWL_DEBUG_RX(mvm, "RSSI is 0 - skip signal based decision\n");
619 		return;
620 	}
621 
622 	mvmvif->bf_data.ave_beacon_signal = sig;
623 
624 	/* BT Coex */
625 	if (mvmvif->bf_data.bt_coex_min_thold !=
626 	    mvmvif->bf_data.bt_coex_max_thold) {
627 		last_event = mvmvif->bf_data.last_bt_coex_event;
628 		if (sig > mvmvif->bf_data.bt_coex_max_thold &&
629 		    (last_event <= mvmvif->bf_data.bt_coex_min_thold ||
630 		     last_event == 0)) {
631 			mvmvif->bf_data.last_bt_coex_event = sig;
632 			IWL_DEBUG_RX(mvm, "cqm_iterator bt coex high %d\n",
633 				     sig);
634 			iwl_mvm_bt_rssi_event(mvm, vif, RSSI_EVENT_HIGH);
635 		} else if (sig < mvmvif->bf_data.bt_coex_min_thold &&
636 			   (last_event >= mvmvif->bf_data.bt_coex_max_thold ||
637 			    last_event == 0)) {
638 			mvmvif->bf_data.last_bt_coex_event = sig;
639 			IWL_DEBUG_RX(mvm, "cqm_iterator bt coex low %d\n",
640 				     sig);
641 			iwl_mvm_bt_rssi_event(mvm, vif, RSSI_EVENT_LOW);
642 		}
643 	}
644 
645 	if (!(vif->driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI))
646 		return;
647 
648 	/* CQM Notification */
649 	last_event = mvmvif->bf_data.last_cqm_event;
650 	if (thold && sig < thold && (last_event == 0 ||
651 				     sig < last_event - hyst)) {
652 		mvmvif->bf_data.last_cqm_event = sig;
653 		IWL_DEBUG_RX(mvm, "cqm_iterator cqm low %d\n",
654 			     sig);
655 		ieee80211_cqm_rssi_notify(
656 			vif,
657 			NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
658 			sig,
659 			GFP_KERNEL);
660 	} else if (sig > thold &&
661 		   (last_event == 0 || sig > last_event + hyst)) {
662 		mvmvif->bf_data.last_cqm_event = sig;
663 		IWL_DEBUG_RX(mvm, "cqm_iterator cqm high %d\n",
664 			     sig);
665 		ieee80211_cqm_rssi_notify(
666 			vif,
667 			NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
668 			sig,
669 			GFP_KERNEL);
670 	}
671 }
672 
673 static inline void
674 iwl_mvm_rx_stats_check_trigger(struct iwl_mvm *mvm, struct iwl_rx_packet *pkt)
675 {
676 	struct iwl_fw_dbg_trigger_tlv *trig;
677 	struct iwl_fw_dbg_trigger_stats *trig_stats;
678 	u32 trig_offset, trig_thold;
679 
680 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, NULL, FW_DBG_TRIGGER_STATS);
681 	if (!trig)
682 		return;
683 
684 	trig_stats = (void *)trig->data;
685 
686 	trig_offset = le32_to_cpu(trig_stats->stop_offset);
687 	trig_thold = le32_to_cpu(trig_stats->stop_threshold);
688 
689 	if (WARN_ON_ONCE(trig_offset >= iwl_rx_packet_payload_len(pkt)))
690 		return;
691 
692 	if (le32_to_cpup((__le32 *) (pkt->data + trig_offset)) < trig_thold)
693 		return;
694 
695 	iwl_fw_dbg_collect_trig(&mvm->fwrt, trig, NULL);
696 }
697 
698 static void iwl_mvm_stats_energy_iter(void *_data,
699 				      struct ieee80211_sta *sta)
700 {
701 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
702 	u8 *energy = _data;
703 	u32 sta_id = mvmsta->sta_id;
704 
705 	if (WARN_ONCE(sta_id >= IWL_MVM_STATION_COUNT_MAX, "sta_id %d >= %d",
706 		      sta_id, IWL_MVM_STATION_COUNT_MAX))
707 		return;
708 
709 	if (energy[sta_id])
710 		mvmsta->avg_energy = energy[sta_id];
711 
712 }
713 
714 static void
715 iwl_mvm_update_tcm_from_stats(struct iwl_mvm *mvm, __le32 *air_time_le,
716 			      __le32 *rx_bytes_le)
717 {
718 	int i;
719 
720 	spin_lock(&mvm->tcm.lock);
721 	for (i = 0; i < NUM_MAC_INDEX_DRIVER; i++) {
722 		struct iwl_mvm_tcm_mac *mdata = &mvm->tcm.data[i];
723 		u32 rx_bytes = le32_to_cpu(rx_bytes_le[i]);
724 		u32 airtime = le32_to_cpu(air_time_le[i]);
725 
726 		mdata->rx.airtime += airtime;
727 		mdata->uapsd_nonagg_detect.rx_bytes += rx_bytes;
728 		if (airtime) {
729 			/* re-init every time to store rate from FW */
730 			ewma_rate_init(&mdata->uapsd_nonagg_detect.rate);
731 			ewma_rate_add(&mdata->uapsd_nonagg_detect.rate,
732 				      rx_bytes * 8 / airtime);
733 		}
734 	}
735 	spin_unlock(&mvm->tcm.lock);
736 }
737 
738 static void
739 iwl_mvm_handle_rx_statistics_tlv(struct iwl_mvm *mvm,
740 				 struct iwl_rx_packet *pkt)
741 {
742 	struct iwl_mvm_stat_data data = {
743 		.mvm = mvm,
744 	};
745 	u8 beacon_average_energy[MAC_INDEX_AUX];
746 	u8 average_energy[IWL_MVM_STATION_COUNT_MAX];
747 	struct iwl_statistics_operational_ntfy *stats;
748 	int expected_size;
749 	__le32 flags;
750 	int i;
751 
752 	expected_size = sizeof(*stats);
753 	if (WARN_ONCE(iwl_rx_packet_payload_len(pkt) < expected_size,
754 		      "received invalid statistics size (%d)!, expected_size: %d\n",
755 		      iwl_rx_packet_payload_len(pkt), expected_size))
756 		return;
757 
758 	stats = (void *)&pkt->data;
759 
760 	if (WARN_ONCE(stats->hdr.type != FW_STATISTICS_OPERATIONAL ||
761 		      stats->hdr.version != 1,
762 		      "received unsupported hdr type %d, version %d\n",
763 		      stats->hdr.type, stats->hdr.version))
764 		return;
765 
766 	flags = stats->flags;
767 	mvm->radio_stats.rx_time = le64_to_cpu(stats->rx_time);
768 	mvm->radio_stats.tx_time = le64_to_cpu(stats->tx_time);
769 	mvm->radio_stats.on_time_rf = le64_to_cpu(stats->on_time_rf);
770 	mvm->radio_stats.on_time_scan = le64_to_cpu(stats->on_time_scan);
771 
772 	iwl_mvm_rx_stats_check_trigger(mvm, pkt);
773 
774 	data.mac_id = stats->mac_id;
775 	data.beacon_filter_average_energy =
776 		le32_to_cpu(stats->beacon_filter_average_energy);
777 	data.flags = flags;
778 	data.beacon_counter = stats->beacon_counter;
779 	for (i = 0; i < ARRAY_SIZE(beacon_average_energy); i++)
780 		beacon_average_energy[i] =
781 			le32_to_cpu(stats->beacon_average_energy[i]);
782 
783 	data.beacon_average_energy = beacon_average_energy;
784 
785 	ieee80211_iterate_active_interfaces(mvm->hw,
786 					    IEEE80211_IFACE_ITER_NORMAL,
787 					    iwl_mvm_stat_iterator,
788 					    &data);
789 
790 	for (i = 0; i < ARRAY_SIZE(average_energy); i++)
791 		average_energy[i] = le32_to_cpu(stats->average_energy[i]);
792 	ieee80211_iterate_stations_atomic(mvm->hw, iwl_mvm_stats_energy_iter,
793 					  average_energy);
794 	/*
795 	 * Don't update in case the statistics are not cleared, since
796 	 * we will end up counting twice the same airtime, once in TCM
797 	 * request and once in statistics notification.
798 	 */
799 	if (le32_to_cpu(flags) & IWL_STATISTICS_REPLY_FLG_CLEAR)
800 		iwl_mvm_update_tcm_from_stats(mvm, stats->air_time,
801 					      stats->rx_bytes);
802 }
803 
804 void iwl_mvm_handle_rx_statistics(struct iwl_mvm *mvm,
805 				  struct iwl_rx_packet *pkt)
806 {
807 	struct iwl_mvm_stat_data data = {
808 		.mvm = mvm,
809 	};
810 	__le32 *bytes, *air_time, flags;
811 	int expected_size;
812 	u8 *energy;
813 
814 	/* From ver 14 and up we use TLV statistics format */
815 	if (iwl_fw_lookup_notif_ver(mvm->fw, LONG_GROUP,
816 				    STATISTICS_CMD, 0) >= 14)
817 		return iwl_mvm_handle_rx_statistics_tlv(mvm, pkt);
818 
819 	if (!iwl_mvm_has_new_rx_stats_api(mvm)) {
820 		if (iwl_mvm_has_new_rx_api(mvm))
821 			expected_size = sizeof(struct iwl_notif_statistics_v11);
822 		else
823 			expected_size = sizeof(struct iwl_notif_statistics_v10);
824 	} else {
825 		expected_size = sizeof(struct iwl_notif_statistics);
826 	}
827 
828 	if (WARN_ONCE(iwl_rx_packet_payload_len(pkt) != expected_size,
829 		      "received invalid statistics size (%d)!\n",
830 		      iwl_rx_packet_payload_len(pkt)))
831 		return;
832 
833 	if (!iwl_mvm_has_new_rx_stats_api(mvm)) {
834 		struct iwl_notif_statistics_v11 *stats = (void *)&pkt->data;
835 
836 		data.mac_id = stats->rx.general.mac_id;
837 		data.beacon_filter_average_energy =
838 			stats->general.common.beacon_filter_average_energy;
839 
840 		mvm->rx_stats_v3 = stats->rx;
841 
842 		mvm->radio_stats.rx_time =
843 			le64_to_cpu(stats->general.common.rx_time);
844 		mvm->radio_stats.tx_time =
845 			le64_to_cpu(stats->general.common.tx_time);
846 		mvm->radio_stats.on_time_rf =
847 			le64_to_cpu(stats->general.common.on_time_rf);
848 		mvm->radio_stats.on_time_scan =
849 			le64_to_cpu(stats->general.common.on_time_scan);
850 
851 		data.beacon_counter = stats->general.beacon_counter;
852 		data.beacon_average_energy =
853 			stats->general.beacon_average_energy;
854 		flags = stats->flag;
855 	} else {
856 		struct iwl_notif_statistics *stats = (void *)&pkt->data;
857 
858 		data.mac_id = stats->rx.general.mac_id;
859 		data.beacon_filter_average_energy =
860 			stats->general.common.beacon_filter_average_energy;
861 
862 		mvm->rx_stats = stats->rx;
863 
864 		mvm->radio_stats.rx_time =
865 			le64_to_cpu(stats->general.common.rx_time);
866 		mvm->radio_stats.tx_time =
867 			le64_to_cpu(stats->general.common.tx_time);
868 		mvm->radio_stats.on_time_rf =
869 			le64_to_cpu(stats->general.common.on_time_rf);
870 		mvm->radio_stats.on_time_scan =
871 			le64_to_cpu(stats->general.common.on_time_scan);
872 
873 		data.beacon_counter = stats->general.beacon_counter;
874 		data.beacon_average_energy =
875 			stats->general.beacon_average_energy;
876 		flags = stats->flag;
877 	}
878 	data.flags = flags;
879 
880 	iwl_mvm_rx_stats_check_trigger(mvm, pkt);
881 
882 	ieee80211_iterate_active_interfaces(mvm->hw,
883 					    IEEE80211_IFACE_ITER_NORMAL,
884 					    iwl_mvm_stat_iterator,
885 					    &data);
886 
887 	if (!iwl_mvm_has_new_rx_api(mvm))
888 		return;
889 
890 	if (!iwl_mvm_has_new_rx_stats_api(mvm)) {
891 		struct iwl_notif_statistics_v11 *v11 = (void *)&pkt->data;
892 
893 		energy = (void *)&v11->load_stats.avg_energy;
894 		bytes = (void *)&v11->load_stats.byte_count;
895 		air_time = (void *)&v11->load_stats.air_time;
896 	} else {
897 		struct iwl_notif_statistics *stats = (void *)&pkt->data;
898 
899 		energy = (void *)&stats->load_stats.avg_energy;
900 		bytes = (void *)&stats->load_stats.byte_count;
901 		air_time = (void *)&stats->load_stats.air_time;
902 	}
903 	ieee80211_iterate_stations_atomic(mvm->hw, iwl_mvm_stats_energy_iter,
904 					  energy);
905 
906 	/*
907 	 * Don't update in case the statistics are not cleared, since
908 	 * we will end up counting twice the same airtime, once in TCM
909 	 * request and once in statistics notification.
910 	 */
911 	if (le32_to_cpu(flags) & IWL_STATISTICS_REPLY_FLG_CLEAR)
912 		iwl_mvm_update_tcm_from_stats(mvm, air_time, bytes);
913 
914 }
915 
916 void iwl_mvm_rx_statistics(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
917 {
918 	iwl_mvm_handle_rx_statistics(mvm, rxb_addr(rxb));
919 }
920 
921 void iwl_mvm_window_status_notif(struct iwl_mvm *mvm,
922 				 struct iwl_rx_cmd_buffer *rxb)
923 {
924 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
925 	struct iwl_ba_window_status_notif *notif = (void *)pkt->data;
926 	int i;
927 	u32 pkt_len = iwl_rx_packet_payload_len(pkt);
928 
929 	if (WARN_ONCE(pkt_len != sizeof(*notif),
930 		      "Received window status notification of wrong size (%u)\n",
931 		      pkt_len))
932 		return;
933 
934 	rcu_read_lock();
935 	for (i = 0; i < BA_WINDOW_STREAMS_MAX; i++) {
936 		struct ieee80211_sta *sta;
937 		u8 sta_id, tid;
938 		u64 bitmap;
939 		u32 ssn;
940 		u16 ratid;
941 		u16 received_mpdu;
942 
943 		ratid = le16_to_cpu(notif->ra_tid[i]);
944 		/* check that this TID is valid */
945 		if (!(ratid & BA_WINDOW_STATUS_VALID_MSK))
946 			continue;
947 
948 		received_mpdu = le16_to_cpu(notif->mpdu_rx_count[i]);
949 		if (received_mpdu == 0)
950 			continue;
951 
952 		tid = ratid & BA_WINDOW_STATUS_TID_MSK;
953 		/* get the station */
954 		sta_id = (ratid & BA_WINDOW_STATUS_STA_ID_MSK)
955 			 >> BA_WINDOW_STATUS_STA_ID_POS;
956 		sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
957 		if (IS_ERR_OR_NULL(sta))
958 			continue;
959 		bitmap = le64_to_cpu(notif->bitmap[i]);
960 		ssn = le32_to_cpu(notif->start_seq_num[i]);
961 
962 		/* update mac80211 with the bitmap for the reordering buffer */
963 		ieee80211_mark_rx_ba_filtered_frames(sta, tid, ssn, bitmap,
964 						     received_mpdu);
965 	}
966 	rcu_read_unlock();
967 }
968