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