rx.c 24.1 KB
Newer Older
J
Johannes Berg 已提交
1 2 3 4 5 6 7
/******************************************************************************
 *
 * This file is provided under a dual BSD/GPLv2 license.  When using or
 * redistributing this file, you may do so under either license.
 *
 * GPL LICENSE SUMMARY
 *
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
J
Johannes Berg 已提交
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
 * USA
 *
 * The full GNU General Public License is included in this distribution
27
 * in the file called COPYING.
J
Johannes Berg 已提交
28 29
 *
 * Contact Information:
30
 *  Intel Linux Wireless <linuxwifi@intel.com>
J
Johannes Berg 已提交
31 32 33 34
 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
 *
 * BSD LICENSE
 *
35
 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
36
 * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
J
Johannes Berg 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *  * Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *  * Neither the name Intel Corporation nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *****************************************************************************/
65
#include <linux/etherdevice.h>
66
#include <linux/skbuff.h>
J
Johannes Berg 已提交
67 68 69
#include "iwl-trans.h"
#include "mvm.h"
#include "fw-api.h"
70
#include "fw-dbg.h"
J
Johannes Berg 已提交
71 72 73 74 75 76 77

/*
 * iwl_mvm_rx_rx_phy_cmd - REPLY_RX_PHY_CMD handler
 *
 * Copies the phy information in mvm->last_phy_info, it will be used when the
 * actual data will come from the fw in the next packet.
 */
78
void iwl_mvm_rx_rx_phy_cmd(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
J
Johannes Berg 已提交
79 80 81 82 83
{
	struct iwl_rx_packet *pkt = rxb_addr(rxb);

	memcpy(&mvm->last_phy_info, pkt->data, sizeof(mvm->last_phy_info));
	mvm->ampdu_ref++;
84 85 86 87 88 89 90 91

#ifdef CONFIG_IWLWIFI_DEBUGFS
	if (mvm->last_phy_info.phy_flags & cpu_to_le16(RX_RES_PHY_FLAGS_AGG)) {
		spin_lock(&mvm->drv_stats_lock);
		mvm->drv_rx_stats.ampdu_count++;
		spin_unlock(&mvm->drv_stats_lock);
	}
#endif
J
Johannes Berg 已提交
92 93 94 95 96 97 98 99
}

/*
 * iwl_mvm_pass_packet_to_mac80211 - builds the packet for mac80211
 *
 * Adds the rxb to a new skb and give it to mac80211
 */
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,
J
Johannes Berg 已提交
103
					    struct ieee80211_hdr *hdr, u16 len,
104
					    u8 crypt_len,
105
					    struct iwl_rx_cmd_buffer *rxb)
J
Johannes Berg 已提交
106
{
107 108 109 110 111 112 113 114 115 116 117 118 119 120
	unsigned int hdrlen = ieee80211_hdrlen(hdr->frame_control);
	unsigned int fraglen;

	/*
	 * The 'hdrlen' (plus the 8 bytes for the SNAP and the crypt_len,
	 * but those are all multiples of 4 long) all goes away, but we
	 * want the *end* of it, which is going to be the start of the IP
	 * header, to be aligned when it gets pulled in.
	 * The beginning of the skb->data is aligned on at least a 4-byte
	 * boundary after allocation. Everything here is aligned at least
	 * on a 2-byte boundary so we can just take hdrlen & 3 and pad by
	 * the result.
	 */
	skb_reserve(skb, hdrlen & 3);
J
Johannes Berg 已提交
121 122

	/* If frame is small enough to fit in skb->head, pull it completely.
123 124 125 126 127 128 129 130 131 132
	 * If not, only pull ieee80211_hdr (including crypto if present, and
	 * an additional 8 bytes for SNAP/ethertype, see below) so that
	 * splice() or TCP coalesce are more efficient.
	 *
	 * Since, in addition, ieee80211_data_to_8023() always pull in at
	 * least 8 bytes (possibly more for mesh) we can do the same here
	 * to save the cost of doing it later. That still doesn't pull in
	 * the actual IP header since the typical case has a SNAP header.
	 * If the latter changes (there are efforts in the standards group
	 * to do so) we should revisit this and ieee80211_data_to_8023().
J
Johannes Berg 已提交
133
	 */
134
	hdrlen = (len <= skb_tailroom(skb)) ? len : hdrlen + crypt_len + 8;
J
Johannes Berg 已提交
135 136 137 138 139 140 141 142 143 144 145 146

	memcpy(skb_put(skb, hdrlen), hdr, hdrlen);
	fraglen = len - hdrlen;

	if (fraglen) {
		int offset = (void *)hdr + hdrlen -
			     rxb_addr(rxb) + rxb_offset(rxb);

		skb_add_rx_frag(skb, 0, rxb_steal_page(rxb), offset,
				fraglen, rxb->truesize);
	}

147
	ieee80211_rx_napi(mvm->hw, sta, skb, napi);
J
Johannes Berg 已提交
148 149
}

150 151
/*
 * iwl_mvm_get_signal_strength - use new rx PHY INFO API
152 153 154
 * values are reported by the fw as positive values - need to negate
 * to obtain their dBM.  Account for missing antennas by replacing 0
 * values by -256dBm: practically 0 power and a non-feasible 8 bit value.
155
 */
156 157 158
static void iwl_mvm_get_signal_strength(struct iwl_mvm *mvm,
					struct iwl_rx_phy_info *phy_info,
					struct ieee80211_rx_status *rx_status)
159 160 161 162 163 164
{
	int energy_a, energy_b, energy_c, max_energy;
	u32 val;

	val =
	    le32_to_cpu(phy_info->non_cfg_phy[IWL_RX_INFO_ENERGY_ANT_ABC_IDX]);
165 166
	energy_a = (val & IWL_RX_INFO_ENERGY_ANT_A_MSK) >>
						IWL_RX_INFO_ENERGY_ANT_A_POS;
167
	energy_a = energy_a ? -energy_a : S8_MIN;
168 169
	energy_b = (val & IWL_RX_INFO_ENERGY_ANT_B_MSK) >>
						IWL_RX_INFO_ENERGY_ANT_B_POS;
170
	energy_b = energy_b ? -energy_b : S8_MIN;
171 172
	energy_c = (val & IWL_RX_INFO_ENERGY_ANT_C_MSK) >>
						IWL_RX_INFO_ENERGY_ANT_C_POS;
173
	energy_c = energy_c ? -energy_c : S8_MIN;
174 175 176 177 178 179
	max_energy = max(energy_a, energy_b);
	max_energy = max(max_energy, energy_c);

	IWL_DEBUG_STATS(mvm, "energy In A %d B %d C %d , and max %d\n",
			energy_a, energy_b, energy_c, max_energy);

180 181 182 183 184 185 186
	rx_status->signal = max_energy;
	rx_status->chains = (le16_to_cpu(phy_info->phy_flags) &
				RX_RES_PHY_FLAGS_ANTENNA)
					>> RX_RES_PHY_FLAGS_ANTENNA_POS;
	rx_status->chain_signal[0] = energy_a;
	rx_status->chain_signal[1] = energy_b;
	rx_status->chain_signal[2] = energy_c;
187 188
}

J
Johannes Berg 已提交
189 190 191 192 193 194 195 196 197 198 199 200
/*
 * iwl_mvm_set_mac80211_rx_flag - translate fw status to mac80211 format
 * @mvm: the mvm object
 * @hdr: 80211 header
 * @stats: status in mac80211's format
 * @rx_pkt_status: status coming from fw
 *
 * returns non 0 value if the packet should be dropped
 */
static u32 iwl_mvm_set_mac80211_rx_flag(struct iwl_mvm *mvm,
					struct ieee80211_hdr *hdr,
					struct ieee80211_rx_status *stats,
201 202
					u32 rx_pkt_status,
					u8 *crypt_len)
J
Johannes Berg 已提交
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
{
	if (!ieee80211_has_protected(hdr->frame_control) ||
	    (rx_pkt_status & RX_MPDU_RES_STATUS_SEC_ENC_MSK) ==
			     RX_MPDU_RES_STATUS_SEC_NO_ENC)
		return 0;

	/* packet was encrypted with unknown alg */
	if ((rx_pkt_status & RX_MPDU_RES_STATUS_SEC_ENC_MSK) ==
					RX_MPDU_RES_STATUS_SEC_ENC_ERR)
		return 0;

	switch (rx_pkt_status & RX_MPDU_RES_STATUS_SEC_ENC_MSK) {
	case RX_MPDU_RES_STATUS_SEC_CCM_ENC:
		/* alg is CCM: check MIC only */
		if (!(rx_pkt_status & RX_MPDU_RES_STATUS_MIC_OK))
			return -1;

		stats->flag |= RX_FLAG_DECRYPTED;
221
		*crypt_len = IEEE80211_CCMP_HDR_LEN;
J
Johannes Berg 已提交
222 223 224 225 226 227
		return 0;

	case RX_MPDU_RES_STATUS_SEC_TKIP_ENC:
		/* Don't drop the frame and decrypt it in SW */
		if (!(rx_pkt_status & RX_MPDU_RES_STATUS_TTAK_OK))
			return 0;
228
		*crypt_len = IEEE80211_TKIP_IV_LEN;
J
Johannes Berg 已提交
229 230 231 232 233 234 235
		/* fall through if TTAK OK */

	case RX_MPDU_RES_STATUS_SEC_WEP_ENC:
		if (!(rx_pkt_status & RX_MPDU_RES_STATUS_ICV_OK))
			return -1;

		stats->flag |= RX_FLAG_DECRYPTED;
236 237 238
		if ((rx_pkt_status & RX_MPDU_RES_STATUS_SEC_ENC_MSK) ==
				RX_MPDU_RES_STATUS_SEC_WEP_ENC)
			*crypt_len = IEEE80211_WEP_IV_LEN;
J
Johannes Berg 已提交
239 240
		return 0;

241 242 243 244 245 246
	case RX_MPDU_RES_STATUS_SEC_EXT_ENC:
		if (!(rx_pkt_status & RX_MPDU_RES_STATUS_MIC_OK))
			return -1;
		stats->flag |= RX_FLAG_DECRYPTED;
		return 0;

J
Johannes Berg 已提交
247 248 249 250 251 252 253
	default:
		IWL_ERR(mvm, "Unhandled alg: 0x%x\n", rx_pkt_status);
	}

	return 0;
}

254 255 256 257 258 259 260 261 262 263 264 265 266
static void iwl_mvm_rx_csum(struct ieee80211_sta *sta,
			    struct sk_buff *skb,
			    u32 status)
{
	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(mvmsta->vif);

	if (mvmvif->features & NETIF_F_RXCSUM &&
	    status & RX_MPDU_RES_STATUS_CSUM_DONE &&
	    status & RX_MPDU_RES_STATUS_CSUM_OK)
		skb->ip_summed = CHECKSUM_UNNECESSARY;
}

J
Johannes Berg 已提交
267 268 269 270 271
/*
 * iwl_mvm_rx_rx_mpdu - REPLY_RX_MPDU_CMD handler
 *
 * Handles the actual data of the Rx packet from the fw
 */
272 273
void iwl_mvm_rx_rx_mpdu(struct iwl_mvm *mvm, struct napi_struct *napi,
			struct iwl_rx_cmd_buffer *rxb)
J
Johannes Berg 已提交
274 275
{
	struct ieee80211_hdr *hdr;
276
	struct ieee80211_rx_status *rx_status;
J
Johannes Berg 已提交
277 278 279
	struct iwl_rx_packet *pkt = rxb_addr(rxb);
	struct iwl_rx_phy_info *phy_info;
	struct iwl_rx_mpdu_res_start *rx_res;
280
	struct ieee80211_sta *sta = NULL;
281
	struct sk_buff *skb;
J
Johannes Berg 已提交
282 283 284
	u32 len;
	u32 rate_n_flags;
	u32 rx_pkt_status;
285
	u8 crypt_len = 0;
286
	bool take_ref;
J
Johannes Berg 已提交
287 288 289 290 291 292 293 294

	phy_info = &mvm->last_phy_info;
	rx_res = (struct iwl_rx_mpdu_res_start *)pkt->data;
	hdr = (struct ieee80211_hdr *)(pkt->data + sizeof(*rx_res));
	len = le16_to_cpu(rx_res->byte_count);
	rx_pkt_status = le32_to_cpup((__le32 *)
		(pkt->data + sizeof(*rx_res) + len));

295 296 297 298 299 300
	/* Dont use dev_alloc_skb(), we'll have enough headroom once
	 * ieee80211_hdr pulled.
	 */
	skb = alloc_skb(128, GFP_ATOMIC);
	if (!skb) {
		IWL_ERR(mvm, "alloc_skb failed\n");
301
		return;
302 303 304
	}

	rx_status = IEEE80211_SKB_RXCB(skb);
J
Johannes Berg 已提交
305 306 307 308

	/*
	 * drop the packet if it has failed being decrypted by HW
	 */
309 310
	if (iwl_mvm_set_mac80211_rx_flag(mvm, hdr, rx_status, rx_pkt_status,
					 &crypt_len)) {
J
Johannes Berg 已提交
311 312
		IWL_DEBUG_DROP(mvm, "Bad decryption results 0x%08x\n",
			       rx_pkt_status);
313
		kfree_skb(skb);
314
		return;
J
Johannes Berg 已提交
315 316
	}

317 318 319 320
	/*
	 * Keep packets with CRC errors (and with overrun) for monitor mode
	 * (otherwise the firmware discards them) but mark them as bad.
	 */
J
Johannes Berg 已提交
321 322 323
	if (!(rx_pkt_status & RX_MPDU_RES_STATUS_CRC_OK) ||
	    !(rx_pkt_status & RX_MPDU_RES_STATUS_OVERRUN_OK)) {
		IWL_DEBUG_RX(mvm, "Bad CRC or FIFO: 0x%08X.\n", rx_pkt_status);
324
		rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
J
Johannes Berg 已提交
325 326 327 328 329 330
	}

	/* This will be used in several places later */
	rate_n_flags = le32_to_cpu(phy_info->rate_n_flags);

	/* rx_status carries information about the packet to mac80211 */
331 332 333
	rx_status->mactime = le64_to_cpu(phy_info->timestamp);
	rx_status->device_timestamp = le32_to_cpu(phy_info->system_timestamp);
	rx_status->band =
J
Johannes Berg 已提交
334
		(phy_info->phy_flags & cpu_to_le16(RX_RES_PHY_FLAGS_BAND_24)) ?
335
				NL80211_BAND_2GHZ : NL80211_BAND_5GHZ;
336
	rx_status->freq =
J
Johannes Berg 已提交
337
		ieee80211_channel_to_frequency(le16_to_cpu(phy_info->channel),
338
					       rx_status->band);
339 340 341

	/* TSF as indicated by the firmware  is at INA time */
	rx_status->flag |= RX_FLAG_MACTIME_PLCP_START;
J
Johannes Berg 已提交
342

343
	iwl_mvm_get_signal_strength(mvm, phy_info, rx_status);
J
Johannes Berg 已提交
344

345 346
	IWL_DEBUG_STATS_LIMIT(mvm, "Rssi %d, TSF %llu\n", rx_status->signal,
			      (unsigned long long)rx_status->mactime);
J
Johannes Berg 已提交
347

348
	rcu_read_lock();
349 350 351 352 353
	if (rx_pkt_status & RX_MPDU_RES_STATUS_SRC_STA_FOUND) {
		u32 id = rx_pkt_status & RX_MPDU_RES_STATUS_STA_ID_MSK;

		id >>= RX_MDPU_RES_STATUS_STA_ID_SHIFT;

354
		if (!WARN_ON_ONCE(id >= ARRAY_SIZE(mvm->fw_id_to_mac_id))) {
355 356 357 358 359 360 361 362 363
			sta = rcu_dereference(mvm->fw_id_to_mac_id[id]);
			if (IS_ERR(sta))
				sta = NULL;
		}
	} else if (!is_multicast_ether_addr(hdr->addr2)) {
		/* This is fine since we prevent two stations with the same
		 * address from being added.
		 */
		sta = ieee80211_find_sta_by_ifaddr(mvm->hw, hdr->addr2, NULL);
364 365 366
	}

	if (sta) {
367
		struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
368 369
		struct ieee80211_vif *tx_blocked_vif =
			rcu_dereference(mvm->csa_tx_blocked_vif);
370

371 372 373 374
		/* We have tx blocked stations (with CS bit). If we heard
		 * frames from a blocked station on a new channel we can
		 * TX to it again.
		 */
375 376 377 378 379 380 381 382 383
		if (unlikely(tx_blocked_vif) &&
		    mvmsta->vif == tx_blocked_vif) {
			struct iwl_mvm_vif *mvmvif =
				iwl_mvm_vif_from_mac80211(tx_blocked_vif);

			if (mvmvif->csa_target_freq == rx_status->freq)
				iwl_mvm_sta_modify_disable_tx_ap(mvm, sta,
								 false);
		}
384

385
		rs_update_last_rssi(mvm, &mvmsta->lq_sta, rx_status);
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402

		if (iwl_fw_dbg_trigger_enabled(mvm->fw, FW_DBG_TRIGGER_RSSI) &&
		    ieee80211_is_beacon(hdr->frame_control)) {
			struct iwl_fw_dbg_trigger_tlv *trig;
			struct iwl_fw_dbg_trigger_low_rssi *rssi_trig;
			bool trig_check;
			s32 rssi;

			trig = iwl_fw_dbg_get_trigger(mvm->fw,
						      FW_DBG_TRIGGER_RSSI);
			rssi_trig = (void *)trig->data;
			rssi = le32_to_cpu(rssi_trig->rssi);

			trig_check =
				iwl_fw_dbg_trigger_check_stop(mvm, mvmsta->vif,
							      trig);
			if (trig_check && rx_status->signal < rssi)
403
				iwl_mvm_fw_dbg_collect_trig(mvm, trig, NULL);
404
		}
405

406 407 408
		if (ieee80211_is_data(hdr->frame_control))
			iwl_mvm_rx_csum(sta, skb, rx_pkt_status);
	}
409 410
	rcu_read_unlock();

J
Johannes Berg 已提交
411 412
	/* set the preamble flag if appropriate */
	if (phy_info->phy_flags & cpu_to_le16(RX_RES_PHY_FLAGS_SHORT_PREAMBLE))
413
		rx_status->flag |= RX_FLAG_SHORTPRE;
J
Johannes Berg 已提交
414 415 416 417 418 419 420

	if (phy_info->phy_flags & cpu_to_le16(RX_RES_PHY_FLAGS_AGG)) {
		/*
		 * We know which subframes of an A-MPDU belong
		 * together since we get a single PHY response
		 * from the firmware for all of them
		 */
421 422
		rx_status->flag |= RX_FLAG_AMPDU_DETAILS;
		rx_status->ampdu_reference = mvm->ampdu_ref;
J
Johannes Berg 已提交
423 424 425 426 427 428 429
	}

	/* Set up the HT phy flags */
	switch (rate_n_flags & RATE_MCS_CHAN_WIDTH_MSK) {
	case RATE_MCS_CHAN_WIDTH_20:
		break;
	case RATE_MCS_CHAN_WIDTH_40:
430
		rx_status->flag |= RX_FLAG_40MHZ;
J
Johannes Berg 已提交
431 432
		break;
	case RATE_MCS_CHAN_WIDTH_80:
433
		rx_status->vht_flag |= RX_VHT_FLAG_80MHZ;
J
Johannes Berg 已提交
434 435
		break;
	case RATE_MCS_CHAN_WIDTH_160:
436
		rx_status->vht_flag |= RX_VHT_FLAG_160MHZ;
J
Johannes Berg 已提交
437 438 439
		break;
	}
	if (rate_n_flags & RATE_MCS_SGI_MSK)
440
		rx_status->flag |= RX_FLAG_SHORT_GI;
J
Johannes Berg 已提交
441
	if (rate_n_flags & RATE_HT_MCS_GF_MSK)
442
		rx_status->flag |= RX_FLAG_HT_GF;
443
	if (rate_n_flags & RATE_MCS_LDPC_MSK)
444
		rx_status->flag |= RX_FLAG_LDPC;
J
Johannes Berg 已提交
445
	if (rate_n_flags & RATE_MCS_HT_MSK) {
446
		u8 stbc = (rate_n_flags & RATE_MCS_STBC_MSK) >>
447
				RATE_MCS_STBC_POS;
448 449 450
		rx_status->flag |= RX_FLAG_HT;
		rx_status->rate_idx = rate_n_flags & RATE_HT_MCS_INDEX_MSK;
		rx_status->flag |= stbc << RX_FLAG_STBC_SHIFT;
J
Johannes Berg 已提交
451
	} else if (rate_n_flags & RATE_MCS_VHT_MSK) {
452
		u8 stbc = (rate_n_flags & RATE_MCS_STBC_MSK) >>
453
				RATE_MCS_STBC_POS;
454
		rx_status->vht_nss =
J
Johannes Berg 已提交
455 456
			((rate_n_flags & RATE_VHT_MCS_NSS_MSK) >>
						RATE_VHT_MCS_NSS_POS) + 1;
457 458 459
		rx_status->rate_idx = rate_n_flags & RATE_VHT_MCS_RATE_CODE_MSK;
		rx_status->flag |= RX_FLAG_VHT;
		rx_status->flag |= stbc << RX_FLAG_STBC_SHIFT;
460
		if (rate_n_flags & RATE_MCS_BF_MSK)
461
			rx_status->vht_flag |= RX_VHT_FLAG_BF;
J
Johannes Berg 已提交
462
	} else {
463 464 465 466 467 468 469 470 471 472
		int rate = iwl_mvm_legacy_rate_to_mac80211_idx(rate_n_flags,
							       rx_status->band);

		if (WARN(rate < 0 || rate > 0xFF,
			 "Invalid rate flags 0x%x, band %d,\n",
			 rate_n_flags, rx_status->band)) {
			kfree_skb(skb);
			return;
		}
		rx_status->rate_idx = rate;
J
Johannes Berg 已提交
473 474
	}

475
#ifdef CONFIG_IWLWIFI_DEBUGFS
476
	iwl_mvm_update_frame_stats(mvm, rate_n_flags,
477
				   rx_status->flag & RX_FLAG_AMPDU_DETAILS);
478
#endif
479 480 481 482 483 484

	if (unlikely((ieee80211_is_beacon(hdr->frame_control) ||
		      ieee80211_is_probe_resp(hdr->frame_control)) &&
		     mvm->sched_scan_pass_all == SCHED_SCAN_PASS_ALL_ENABLED))
		mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_FOUND;

485 486 487 488
	if (unlikely(ieee80211_is_beacon(hdr->frame_control) ||
		     ieee80211_is_probe_resp(hdr->frame_control)))
		rx_status->boottime_ns = ktime_get_boot_ns();

489 490 491 492 493 494 495 496 497 498 499
	/* Take a reference briefly to kick off a d0i3 entry delay so
	 * we can handle bursts of RX packets without toggling the
	 * state too often.  But don't do this for beacons if we are
	 * going to idle because the beacon filtering changes we make
	 * cause the firmware to send us collateral beacons. */
	take_ref = !(test_bit(STATUS_TRANS_GOING_IDLE, &mvm->trans->status) &&
		     ieee80211_is_beacon(hdr->frame_control));

	if (take_ref)
		iwl_mvm_ref(mvm, IWL_MVM_REF_RX);

500
	iwl_mvm_pass_packet_to_mac80211(mvm, sta, napi, skb, hdr, len,
501
					crypt_len, rxb);
502 503 504

	if (take_ref)
		iwl_mvm_unref(mvm, IWL_MVM_REF_RX);
J
Johannes Berg 已提交
505
}
506

507
static void iwl_mvm_update_rx_statistics(struct iwl_mvm *mvm,
508
					 struct mvm_statistics_rx *rx_stats)
509 510
{
	lockdep_assert_held(&mvm->mutex);
511 512

	mvm->rx_stats = *rx_stats;
513 514
}

515 516
struct iwl_mvm_stat_data {
	struct iwl_mvm *mvm;
517
	__le32 mac_id;
518
	u8 beacon_filter_average_energy;
519
	void *general;
520 521 522 523 524 525 526
};

static void iwl_mvm_stat_iterator(void *_data, u8 *mac,
				  struct ieee80211_vif *vif)
{
	struct iwl_mvm_stat_data *data = _data;
	struct iwl_mvm *mvm = data->mvm;
527
	int sig = -data->beacon_filter_average_energy;
528 529 530
	int last_event;
	int thold = vif->bss_conf.cqm_rssi_thold;
	int hyst = vif->bss_conf.cqm_rssi_hyst;
531
	u16 id = le32_to_cpu(data->mac_id);
532 533
	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);

534 535 536 537 538
	/* This doesn't need the MAC ID check since it's not taking the
	 * data copied into the "data" struct, but rather the data from
	 * the notification directly.
	 */
	if (data->general) {
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
		u16 vif_id = mvmvif->id;

		if (iwl_mvm_is_cdb_supported(mvm)) {
			struct mvm_statistics_general_cdb *general =
				data->general;

			mvmvif->beacon_stats.num_beacons =
				le32_to_cpu(general->beacon_counter[vif_id]);
			mvmvif->beacon_stats.avg_signal =
				-general->beacon_average_energy[vif_id];
		} else {
			struct mvm_statistics_general_v8 *general =
				data->general;

			mvmvif->beacon_stats.num_beacons =
				le32_to_cpu(general->beacon_counter[vif_id]);
			mvmvif->beacon_stats.avg_signal =
				-general->beacon_average_energy[vif_id];
		}

559 560
	}

561 562 563 564 565 566
	if (mvmvif->id != id)
		return;

	if (vif->type != NL80211_IFTYPE_STATION)
		return;

567 568 569 570 571
	if (sig == 0) {
		IWL_DEBUG_RX(mvm, "RSSI is 0 - skip signal based decision\n");
		return;
	}

572 573
	mvmvif->bf_data.ave_beacon_signal = sig;

574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
	/* BT Coex */
	if (mvmvif->bf_data.bt_coex_min_thold !=
	    mvmvif->bf_data.bt_coex_max_thold) {
		last_event = mvmvif->bf_data.last_bt_coex_event;
		if (sig > mvmvif->bf_data.bt_coex_max_thold &&
		    (last_event <= mvmvif->bf_data.bt_coex_min_thold ||
		     last_event == 0)) {
			mvmvif->bf_data.last_bt_coex_event = sig;
			IWL_DEBUG_RX(mvm, "cqm_iterator bt coex high %d\n",
				     sig);
			iwl_mvm_bt_rssi_event(mvm, vif, RSSI_EVENT_HIGH);
		} else if (sig < mvmvif->bf_data.bt_coex_min_thold &&
			   (last_event >= mvmvif->bf_data.bt_coex_max_thold ||
			    last_event == 0)) {
			mvmvif->bf_data.last_bt_coex_event = sig;
			IWL_DEBUG_RX(mvm, "cqm_iterator bt coex low %d\n",
				     sig);
			iwl_mvm_bt_rssi_event(mvm, vif, RSSI_EVENT_LOW);
		}
	}

595 596 597 598 599 600 601 602 603 604 605 606 607
	if (!(vif->driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI))
		return;

	/* CQM Notification */
	last_event = mvmvif->bf_data.last_cqm_event;
	if (thold && sig < thold && (last_event == 0 ||
				     sig < last_event - hyst)) {
		mvmvif->bf_data.last_cqm_event = sig;
		IWL_DEBUG_RX(mvm, "cqm_iterator cqm low %d\n",
			     sig);
		ieee80211_cqm_rssi_notify(
			vif,
			NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
608
			sig,
609 610 611 612 613 614 615 616 617
			GFP_KERNEL);
	} else if (sig > thold &&
		   (last_event == 0 || sig > last_event + hyst)) {
		mvmvif->bf_data.last_cqm_event = sig;
		IWL_DEBUG_RX(mvm, "cqm_iterator cqm high %d\n",
			     sig);
		ieee80211_cqm_rssi_notify(
			vif,
			NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
618
			sig,
619 620 621 622
			GFP_KERNEL);
	}
}

623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
static inline void
iwl_mvm_rx_stats_check_trigger(struct iwl_mvm *mvm, struct iwl_rx_packet *pkt)
{
	struct iwl_fw_dbg_trigger_tlv *trig;
	struct iwl_fw_dbg_trigger_stats *trig_stats;
	u32 trig_offset, trig_thold;

	if (!iwl_fw_dbg_trigger_enabled(mvm->fw, FW_DBG_TRIGGER_STATS))
		return;

	trig = iwl_fw_dbg_get_trigger(mvm->fw, FW_DBG_TRIGGER_STATS);
	trig_stats = (void *)trig->data;

	if (!iwl_fw_dbg_trigger_check_stop(mvm, NULL, trig))
		return;

	trig_offset = le32_to_cpu(trig_stats->stop_offset);
	trig_thold = le32_to_cpu(trig_stats->stop_threshold);

	if (WARN_ON_ONCE(trig_offset >= iwl_rx_packet_payload_len(pkt)))
		return;

	if (le32_to_cpup((__le32 *) (pkt->data + trig_offset)) < trig_thold)
		return;

648
	iwl_mvm_fw_dbg_collect_trig(mvm, trig, NULL);
649 650
}

651 652
void iwl_mvm_handle_rx_statistics(struct iwl_mvm *mvm,
				  struct iwl_rx_packet *pkt)
653
{
654
	struct iwl_notif_statistics_cdb *stats = (void *)&pkt->data;
655 656 657
	struct iwl_mvm_stat_data data = {
		.mvm = mvm,
	};
658
	int expected_size;
659 660 661
	int i;
	u8 *energy;
	__le32 *bytes, *air_time;
662 663 664 665 666 667 668

	if (iwl_mvm_is_cdb_supported(mvm))
		expected_size = sizeof(*stats);
	else if (iwl_mvm_has_new_rx_api(mvm))
		expected_size = sizeof(struct iwl_notif_statistics_v11);
	else
		expected_size = sizeof(struct iwl_notif_statistics_v10);
669

670 671 672 673 674
	if (iwl_rx_packet_payload_len(pkt) != expected_size) {
		IWL_ERR(mvm, "received invalid statistics size (%d)!\n",
			iwl_rx_packet_payload_len(pkt));
		return;
	}
675

676 677
	data.mac_id = stats->rx.general.mac_id;
	data.beacon_filter_average_energy =
678
		stats->general.common.beacon_filter_average_energy;
679

680
	iwl_mvm_update_rx_statistics(mvm, &stats->rx);
681

682 683
	mvm->radio_stats.rx_time = le64_to_cpu(stats->general.common.rx_time);
	mvm->radio_stats.tx_time = le64_to_cpu(stats->general.common.tx_time);
684
	mvm->radio_stats.on_time_rf =
685
		le64_to_cpu(stats->general.common.on_time_rf);
686
	mvm->radio_stats.on_time_scan =
687
		le64_to_cpu(stats->general.common.on_time_scan);
688

689
	data.general = &stats->general;
690

691 692
	iwl_mvm_rx_stats_check_trigger(mvm, pkt);

693 694 695 696
	ieee80211_iterate_active_interfaces(mvm->hw,
					    IEEE80211_IFACE_ITER_NORMAL,
					    iwl_mvm_stat_iterator,
					    &data);
697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726

	if (!iwl_mvm_has_new_rx_api(mvm))
		return;

	if (!iwl_mvm_is_cdb_supported(mvm)) {
		struct iwl_notif_statistics_v11 *v11 =
			(void *)&pkt->data;

		energy = (void *)&v11->load_stats.avg_energy;
		bytes = (void *)&v11->load_stats.byte_count;
		air_time = (void *)&v11->load_stats.air_time;
	} else {
		energy = (void *)&stats->load_stats.avg_energy;
		bytes = (void *)&stats->load_stats.byte_count;
		air_time = (void *)&stats->load_stats.air_time;
	}

	rcu_read_lock();
	for (i = 0; i < ARRAY_SIZE(mvm->fw_id_to_mac_id); i++) {
		struct iwl_mvm_sta *sta;

		if (!energy[i])
			continue;

		sta = iwl_mvm_sta_from_staid_rcu(mvm, i);
		if (!sta)
			continue;
		sta->avg_energy = energy[i];
	}
	rcu_read_unlock();
727 728
}

729
void iwl_mvm_rx_statistics(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
730 731
{
	iwl_mvm_handle_rx_statistics(mvm, rxb_addr(rxb));
732
}
733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780

void iwl_mvm_window_status_notif(struct iwl_mvm *mvm,
				 struct iwl_rx_cmd_buffer *rxb)
{
	struct iwl_rx_packet *pkt = rxb_addr(rxb);
	struct iwl_ba_window_status_notif *notif = (void *)pkt->data;
	int i;
	u32 pkt_len = iwl_rx_packet_payload_len(pkt);

	if (WARN_ONCE(pkt_len != sizeof(*notif),
		      "Received window status notification of wrong size (%u)\n",
		      pkt_len))
		return;

	rcu_read_lock();
	for (i = 0; i < BA_WINDOW_STREAMS_MAX; i++) {
		struct ieee80211_sta *sta;
		u8 sta_id, tid;
		u64 bitmap;
		u32 ssn;
		u16 ratid;
		u16 received_mpdu;

		ratid = le16_to_cpu(notif->ra_tid[i]);
		/* check that this TID is valid */
		if (!(ratid & BA_WINDOW_STATUS_VALID_MSK))
			continue;

		received_mpdu = le16_to_cpu(notif->mpdu_rx_count[i]);
		if (received_mpdu == 0)
			continue;

		tid = ratid & BA_WINDOW_STATUS_TID_MSK;
		/* get the station */
		sta_id = (ratid & BA_WINDOW_STATUS_STA_ID_MSK)
			 >> BA_WINDOW_STATUS_STA_ID_POS;
		sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
		if (IS_ERR_OR_NULL(sta))
			continue;
		bitmap = le64_to_cpu(notif->bitmap[i]);
		ssn = le32_to_cpu(notif->start_seq_num[i]);

		/* update mac80211 with the bitmap for the reordering buffer */
		ieee80211_mark_rx_ba_filtered_frames(sta, tid, ssn, bitmap,
						     received_mpdu);
	}
	rcu_read_unlock();
}