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