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