Linux Audio

Check our new training course

Real-Time Linux with PREEMPT_RT training

Feb 18-20, 2025
Register
Loading...
v6.8
   1// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
   2/*
   3 * Copyright (C) 2012-2014, 2018-2023 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 */
  20void 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 */
  45static 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 = (u8 *)hdr + hdrlen -
  87			     (u8 *)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 */
 102static 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 */
 139static 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_WARN(mvm, "Unhandled alg: 0x%x\n", rx_pkt_status);
 194	}
 195
 196	return 0;
 197}
 198
 199static 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		u8 tid = ieee80211_get_tid(hdr);
 218
 219		if (tid < IWL_MAX_TID_COUNT)
 220			ac = tid_to_mac80211_ac[tid];
 221	}
 222
 223	mvmsta = iwl_mvm_sta_from_mac80211(sta);
 224	mac = mvmsta->mac_id_n_color & FW_CTXT_ID_MSK;
 225
 226	if (time_after(jiffies, mvm->tcm.ts + MVM_TCM_PERIOD))
 227		schedule_delayed_work(&mvm->tcm.work, 0);
 228	mdata = &mvm->tcm.data[mac];
 229	mdata->rx.pkts[ac]++;
 230
 231	/* count the airtime only once for each ampdu */
 232	if (mdata->rx.last_ampdu_ref != mvm->ampdu_ref) {
 233		mdata->rx.last_ampdu_ref = mvm->ampdu_ref;
 234		mdata->rx.airtime += le16_to_cpu(phy_info->frame_time);
 235	}
 236
 237	if (!(rate_n_flags & (RATE_MCS_HT_MSK_V1 | RATE_MCS_VHT_MSK_V1)))
 238		return;
 239
 240	mvmvif = iwl_mvm_vif_from_mac80211(mvmsta->vif);
 241
 242	if (mdata->opened_rx_ba_sessions ||
 243	    mdata->uapsd_nonagg_detect.detected ||
 244	    (!mvmvif->deflink.queue_params[IEEE80211_AC_VO].uapsd &&
 245	     !mvmvif->deflink.queue_params[IEEE80211_AC_VI].uapsd &&
 246	     !mvmvif->deflink.queue_params[IEEE80211_AC_BE].uapsd &&
 247	     !mvmvif->deflink.queue_params[IEEE80211_AC_BK].uapsd) ||
 248	    mvmsta->deflink.sta_id != mvmvif->deflink.ap_sta_id)
 249		return;
 250
 251	if (rate_n_flags & RATE_MCS_HT_MSK_V1) {
 252		thr = thresh_tpt[rate_n_flags & RATE_HT_MCS_RATE_CODE_MSK_V1];
 253		thr *= 1 + ((rate_n_flags & RATE_HT_MCS_NSS_MSK_V1) >>
 254					RATE_HT_MCS_NSS_POS_V1);
 255	} else {
 256		if (WARN_ON((rate_n_flags & RATE_VHT_MCS_RATE_CODE_MSK) >=
 257				ARRAY_SIZE(thresh_tpt)))
 258			return;
 259		thr = thresh_tpt[rate_n_flags & RATE_VHT_MCS_RATE_CODE_MSK];
 260		thr *= 1 + FIELD_GET(RATE_MCS_NSS_MSK, rate_n_flags);
 261	}
 262
 263	thr <<= ((rate_n_flags & RATE_MCS_CHAN_WIDTH_MSK_V1) >>
 264				RATE_MCS_CHAN_WIDTH_POS);
 265
 266	mdata->uapsd_nonagg_detect.rx_bytes += len;
 267	ewma_rate_add(&mdata->uapsd_nonagg_detect.rate, thr);
 268}
 269
 270static void iwl_mvm_rx_csum(struct ieee80211_sta *sta,
 271			    struct sk_buff *skb,
 272			    u32 status)
 273{
 274	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
 275	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(mvmsta->vif);
 276
 277	if (mvmvif->features & NETIF_F_RXCSUM &&
 278	    status & RX_MPDU_RES_STATUS_CSUM_DONE &&
 279	    status & RX_MPDU_RES_STATUS_CSUM_OK)
 280		skb->ip_summed = CHECKSUM_UNNECESSARY;
 281}
 282
 283/*
 284 * iwl_mvm_rx_rx_mpdu - REPLY_RX_MPDU_CMD handler
 285 *
 286 * Handles the actual data of the Rx packet from the fw
 287 */
 288void iwl_mvm_rx_rx_mpdu(struct iwl_mvm *mvm, struct napi_struct *napi,
 289			struct iwl_rx_cmd_buffer *rxb)
 290{
 291	struct ieee80211_hdr *hdr;
 292	struct ieee80211_rx_status *rx_status;
 293	struct iwl_rx_packet *pkt = rxb_addr(rxb);
 294	struct iwl_rx_phy_info *phy_info;
 295	struct iwl_rx_mpdu_res_start *rx_res;
 296	struct ieee80211_sta *sta = NULL;
 297	struct sk_buff *skb;
 298	u32 len, pkt_len = iwl_rx_packet_payload_len(pkt);
 299	u32 rate_n_flags;
 300	u32 rx_pkt_status;
 301	u8 crypt_len = 0;
 302
 303	if (unlikely(pkt_len < sizeof(*rx_res))) {
 304		IWL_DEBUG_DROP(mvm, "Bad REPLY_RX_MPDU_CMD size\n");
 305		return;
 306	}
 307
 308	phy_info = &mvm->last_phy_info;
 309	rx_res = (struct iwl_rx_mpdu_res_start *)pkt->data;
 310	hdr = (struct ieee80211_hdr *)(pkt->data + sizeof(*rx_res));
 311	len = le16_to_cpu(rx_res->byte_count);
 312
 313	if (unlikely(len + sizeof(*rx_res) + sizeof(__le32) > pkt_len)) {
 314		IWL_DEBUG_DROP(mvm, "FW lied about packet len\n");
 315		return;
 316	}
 317
 318	rx_pkt_status = get_unaligned_le32((__le32 *)
 319		(pkt->data + sizeof(*rx_res) + len));
 320
 321	/* Dont use dev_alloc_skb(), we'll have enough headroom once
 322	 * ieee80211_hdr pulled.
 323	 */
 324	skb = alloc_skb(128, GFP_ATOMIC);
 325	if (!skb) {
 326		IWL_ERR(mvm, "alloc_skb failed\n");
 327		return;
 328	}
 329
 330	rx_status = IEEE80211_SKB_RXCB(skb);
 331
 332	/*
 
 
 
 
 
 
 
 
 
 
 
 333	 * Keep packets with CRC errors (and with overrun) for monitor mode
 334	 * (otherwise the firmware discards them) but mark them as bad.
 335	 */
 336	if (!(rx_pkt_status & RX_MPDU_RES_STATUS_CRC_OK) ||
 337	    !(rx_pkt_status & RX_MPDU_RES_STATUS_OVERRUN_OK)) {
 338		IWL_DEBUG_RX(mvm, "Bad CRC or FIFO: 0x%08X.\n", rx_pkt_status);
 339		rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
 340	}
 341
 342	/* This will be used in several places later */
 343	rate_n_flags = le32_to_cpu(phy_info->rate_n_flags);
 344
 345	/* rx_status carries information about the packet to mac80211 */
 346	rx_status->mactime = le64_to_cpu(phy_info->timestamp);
 347	rx_status->device_timestamp = le32_to_cpu(phy_info->system_timestamp);
 348	rx_status->band =
 349		(phy_info->phy_flags & cpu_to_le16(RX_RES_PHY_FLAGS_BAND_24)) ?
 350				NL80211_BAND_2GHZ : NL80211_BAND_5GHZ;
 351	rx_status->freq =
 352		ieee80211_channel_to_frequency(le16_to_cpu(phy_info->channel),
 353					       rx_status->band);
 354
 355	/* TSF as indicated by the firmware  is at INA time */
 356	rx_status->flag |= RX_FLAG_MACTIME_PLCP_START;
 357
 358	iwl_mvm_get_signal_strength(mvm, phy_info, rx_status);
 359
 360	IWL_DEBUG_STATS_LIMIT(mvm, "Rssi %d, TSF %llu\n", rx_status->signal,
 361			      (unsigned long long)rx_status->mactime);
 362
 363	rcu_read_lock();
 364	if (rx_pkt_status & RX_MPDU_RES_STATUS_SRC_STA_FOUND) {
 365		u32 id = rx_pkt_status & RX_MPDU_RES_STATUS_STA_ID_MSK;
 366
 367		id >>= RX_MDPU_RES_STATUS_STA_ID_SHIFT;
 368
 369		if (!WARN_ON_ONCE(id >= mvm->fw->ucode_capa.num_stations)) {
 370			sta = rcu_dereference(mvm->fw_id_to_mac_id[id]);
 371			if (IS_ERR(sta))
 372				sta = NULL;
 373		}
 374	} else if (!is_multicast_ether_addr(hdr->addr2)) {
 375		/* This is fine since we prevent two stations with the same
 376		 * address from being added.
 377		 */
 378		sta = ieee80211_find_sta_by_ifaddr(mvm->hw, hdr->addr2, NULL);
 379	}
 380
 381	if (sta) {
 382		struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
 383		struct ieee80211_vif *vif = mvmsta->vif;
 384		struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
 385
 386		/*
 387		 * Don't even try to decrypt a MCAST frame that was received
 388		 * before the managed vif is authorized, we'd fail anyway.
 389		 */
 390		if (is_multicast_ether_addr(hdr->addr1) &&
 391		    vif->type == NL80211_IFTYPE_STATION &&
 392		    !mvmvif->authorized &&
 393		    ieee80211_has_protected(hdr->frame_control)) {
 394			IWL_DEBUG_DROP(mvm, "MCAST before the vif is authorized\n");
 395			kfree_skb(skb);
 396			rcu_read_unlock();
 397			return;
 398		}
 399	}
 400
 401	/*
 402	 * drop the packet if it has failed being decrypted by HW
 403	 */
 404	if (iwl_mvm_set_mac80211_rx_flag(mvm, hdr, rx_status, rx_pkt_status,
 405					 &crypt_len)) {
 406		IWL_DEBUG_DROP(mvm, "Bad decryption results 0x%08x\n",
 407			       rx_pkt_status);
 408		kfree_skb(skb);
 409		rcu_read_unlock();
 410		return;
 411	}
 412
 413	if (sta) {
 414		struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
 415		struct ieee80211_vif *tx_blocked_vif =
 416			rcu_dereference(mvm->csa_tx_blocked_vif);
 417		struct iwl_fw_dbg_trigger_tlv *trig;
 418		struct ieee80211_vif *vif = mvmsta->vif;
 419
 420		/* We have tx blocked stations (with CS bit). If we heard
 421		 * frames from a blocked station on a new channel we can
 422		 * TX to it again.
 423		 */
 424		if (unlikely(tx_blocked_vif) && vif == tx_blocked_vif) {
 
 425			struct iwl_mvm_vif *mvmvif =
 426				iwl_mvm_vif_from_mac80211(tx_blocked_vif);
 427
 428			if (mvmvif->csa_target_freq == rx_status->freq)
 429				iwl_mvm_sta_modify_disable_tx_ap(mvm, sta,
 430								 false);
 431		}
 432
 433		rs_update_last_rssi(mvm, mvmsta, rx_status);
 434
 435		trig = iwl_fw_dbg_trigger_on(&mvm->fwrt,
 436					     ieee80211_vif_to_wdev(vif),
 437					     FW_DBG_TRIGGER_RSSI);
 438
 439		if (trig && ieee80211_is_beacon(hdr->frame_control)) {
 440			struct iwl_fw_dbg_trigger_low_rssi *rssi_trig;
 
 441			s32 rssi;
 442
 
 
 443			rssi_trig = (void *)trig->data;
 444			rssi = le32_to_cpu(rssi_trig->rssi);
 445
 446			if (rx_status->signal < rssi)
 
 
 
 
 447				iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
 448							NULL);
 449		}
 450
 451		if (!mvm->tcm.paused && len >= sizeof(*hdr) &&
 452		    !is_multicast_ether_addr(hdr->addr1) &&
 453		    ieee80211_is_data(hdr->frame_control))
 454			iwl_mvm_rx_handle_tcm(mvm, sta, hdr, len, phy_info,
 455					      rate_n_flags);
 456
 457		if (ieee80211_is_data(hdr->frame_control))
 458			iwl_mvm_rx_csum(sta, skb, rx_pkt_status);
 459	}
 460	rcu_read_unlock();
 461
 462	/* set the preamble flag if appropriate */
 463	if (phy_info->phy_flags & cpu_to_le16(RX_RES_PHY_FLAGS_SHORT_PREAMBLE))
 464		rx_status->enc_flags |= RX_ENC_FLAG_SHORTPRE;
 465
 466	if (phy_info->phy_flags & cpu_to_le16(RX_RES_PHY_FLAGS_AGG)) {
 467		/*
 468		 * We know which subframes of an A-MPDU belong
 469		 * together since we get a single PHY response
 470		 * from the firmware for all of them
 471		 */
 472		rx_status->flag |= RX_FLAG_AMPDU_DETAILS;
 473		rx_status->ampdu_reference = mvm->ampdu_ref;
 474	}
 475
 476	/* Set up the HT phy flags */
 477	switch (rate_n_flags & RATE_MCS_CHAN_WIDTH_MSK_V1) {
 478	case RATE_MCS_CHAN_WIDTH_20:
 479		break;
 480	case RATE_MCS_CHAN_WIDTH_40:
 481		rx_status->bw = RATE_INFO_BW_40;
 482		break;
 483	case RATE_MCS_CHAN_WIDTH_80:
 484		rx_status->bw = RATE_INFO_BW_80;
 485		break;
 486	case RATE_MCS_CHAN_WIDTH_160:
 487		rx_status->bw = RATE_INFO_BW_160;
 488		break;
 489	}
 490	if (!(rate_n_flags & RATE_MCS_CCK_MSK_V1) &&
 491	    rate_n_flags & RATE_MCS_SGI_MSK_V1)
 492		rx_status->enc_flags |= RX_ENC_FLAG_SHORT_GI;
 493	if (rate_n_flags & RATE_HT_MCS_GF_MSK)
 494		rx_status->enc_flags |= RX_ENC_FLAG_HT_GF;
 495	if (rate_n_flags & RATE_MCS_LDPC_MSK_V1)
 496		rx_status->enc_flags |= RX_ENC_FLAG_LDPC;
 497	if (rate_n_flags & RATE_MCS_HT_MSK_V1) {
 498		u8 stbc = (rate_n_flags & RATE_MCS_STBC_MSK) >>
 499				RATE_MCS_STBC_POS;
 500		rx_status->encoding = RX_ENC_HT;
 501		rx_status->rate_idx = rate_n_flags & RATE_HT_MCS_INDEX_MSK_V1;
 502		rx_status->enc_flags |= stbc << RX_ENC_FLAG_STBC_SHIFT;
 503	} else if (rate_n_flags & RATE_MCS_VHT_MSK_V1) {
 504		u8 stbc = (rate_n_flags & RATE_MCS_STBC_MSK) >>
 505				RATE_MCS_STBC_POS;
 506		rx_status->nss =
 507			FIELD_GET(RATE_MCS_NSS_MSK, rate_n_flags) + 1;
 
 508		rx_status->rate_idx = rate_n_flags & RATE_VHT_MCS_RATE_CODE_MSK;
 509		rx_status->encoding = RX_ENC_VHT;
 510		rx_status->enc_flags |= stbc << RX_ENC_FLAG_STBC_SHIFT;
 511		if (rate_n_flags & RATE_MCS_BF_MSK)
 512			rx_status->enc_flags |= RX_ENC_FLAG_BF;
 513	} else {
 514		int rate = iwl_mvm_legacy_rate_to_mac80211_idx(rate_n_flags,
 515							       rx_status->band);
 516
 517		if (WARN(rate < 0 || rate > 0xFF,
 518			 "Invalid rate flags 0x%x, band %d,\n",
 519			 rate_n_flags, rx_status->band)) {
 520			kfree_skb(skb);
 521			return;
 522		}
 523		rx_status->rate_idx = rate;
 524	}
 525
 526#ifdef CONFIG_IWLWIFI_DEBUGFS
 527	iwl_mvm_update_frame_stats(mvm, rate_n_flags,
 528				   rx_status->flag & RX_FLAG_AMPDU_DETAILS);
 529#endif
 530
 531	if (unlikely((ieee80211_is_beacon(hdr->frame_control) ||
 532		      ieee80211_is_probe_resp(hdr->frame_control)) &&
 533		     mvm->sched_scan_pass_all == SCHED_SCAN_PASS_ALL_ENABLED))
 534		mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_FOUND;
 535
 536	if (unlikely(ieee80211_is_beacon(hdr->frame_control) ||
 537		     ieee80211_is_probe_resp(hdr->frame_control)))
 538		rx_status->boottime_ns = ktime_get_boottime_ns();
 
 
 
 
 
 
 
 
 
 
 
 539
 540	iwl_mvm_pass_packet_to_mac80211(mvm, sta, napi, skb, hdr, len,
 541					crypt_len, rxb);
 
 
 
 542}
 543
 544struct iwl_mvm_stat_data {
 545	struct iwl_mvm *mvm;
 546	__le32 flags;
 547	__le32 mac_id;
 548	u8 beacon_filter_average_energy;
 549	__le32 *beacon_counter;
 550	u8 *beacon_average_energy;
 551};
 552
 553struct iwl_mvm_stat_data_all_macs {
 554	struct iwl_mvm *mvm;
 555	__le32 flags;
 556	struct iwl_stats_ntfy_per_mac *per_mac;
 557};
 558
 559static void iwl_mvm_update_vif_sig(struct ieee80211_vif *vif, int sig)
 560{
 561	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
 562	struct iwl_mvm *mvm = mvmvif->mvm;
 
 
 563	int thold = vif->bss_conf.cqm_rssi_thold;
 564	int hyst = vif->bss_conf.cqm_rssi_hyst;
 565	int last_event;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 566
 567	if (sig == 0) {
 568		IWL_DEBUG_RX(mvm, "RSSI is 0 - skip signal based decision\n");
 569		return;
 570	}
 571
 572	mvmvif->bf_data.ave_beacon_signal = sig;
 573
 574	/* BT Coex */
 575	if (mvmvif->bf_data.bt_coex_min_thold !=
 576	    mvmvif->bf_data.bt_coex_max_thold) {
 577		last_event = mvmvif->bf_data.last_bt_coex_event;
 578		if (sig > mvmvif->bf_data.bt_coex_max_thold &&
 579		    (last_event <= mvmvif->bf_data.bt_coex_min_thold ||
 580		     last_event == 0)) {
 581			mvmvif->bf_data.last_bt_coex_event = sig;
 582			IWL_DEBUG_RX(mvm, "cqm_iterator bt coex high %d\n",
 583				     sig);
 584			iwl_mvm_bt_rssi_event(mvm, vif, RSSI_EVENT_HIGH);
 585		} else if (sig < mvmvif->bf_data.bt_coex_min_thold &&
 586			   (last_event >= mvmvif->bf_data.bt_coex_max_thold ||
 587			    last_event == 0)) {
 588			mvmvif->bf_data.last_bt_coex_event = sig;
 589			IWL_DEBUG_RX(mvm, "cqm_iterator bt coex low %d\n",
 590				     sig);
 591			iwl_mvm_bt_rssi_event(mvm, vif, RSSI_EVENT_LOW);
 592		}
 593	}
 594
 595	if (!(vif->driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI))
 596		return;
 597
 598	/* CQM Notification */
 599	last_event = mvmvif->bf_data.last_cqm_event;
 600	if (thold && sig < thold && (last_event == 0 ||
 601				     sig < last_event - hyst)) {
 602		mvmvif->bf_data.last_cqm_event = sig;
 603		IWL_DEBUG_RX(mvm, "cqm_iterator cqm low %d\n",
 604			     sig);
 605		ieee80211_cqm_rssi_notify(
 606			vif,
 607			NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
 608			sig,
 609			GFP_KERNEL);
 610	} else if (sig > thold &&
 611		   (last_event == 0 || sig > last_event + hyst)) {
 612		mvmvif->bf_data.last_cqm_event = sig;
 613		IWL_DEBUG_RX(mvm, "cqm_iterator cqm high %d\n",
 614			     sig);
 615		ieee80211_cqm_rssi_notify(
 616			vif,
 617			NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
 618			sig,
 619			GFP_KERNEL);
 620	}
 621}
 622
 623static void iwl_mvm_stat_iterator(void *_data, u8 *mac,
 624				  struct ieee80211_vif *vif)
 625{
 626	struct iwl_mvm_stat_data *data = _data;
 627	int sig = -data->beacon_filter_average_energy;
 628	u16 id = le32_to_cpu(data->mac_id);
 629	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
 630	u16 vif_id = mvmvif->id;
 631
 632	/* This doesn't need the MAC ID check since it's not taking the
 633	 * data copied into the "data" struct, but rather the data from
 634	 * the notification directly.
 635	 */
 636	mvmvif->deflink.beacon_stats.num_beacons =
 637		le32_to_cpu(data->beacon_counter[vif_id]);
 638	mvmvif->deflink.beacon_stats.avg_signal =
 639		-data->beacon_average_energy[vif_id];
 640
 641	if (mvmvif->id != id)
 642		return;
 643
 644	if (vif->type != NL80211_IFTYPE_STATION)
 645		return;
 646
 647	/* make sure that beacon statistics don't go backwards with TCM
 648	 * request to clear statistics
 649	 */
 650	if (le32_to_cpu(data->flags) & IWL_STATISTICS_REPLY_FLG_CLEAR)
 651		mvmvif->deflink.beacon_stats.accu_num_beacons +=
 652			mvmvif->deflink.beacon_stats.num_beacons;
 653
 654	iwl_mvm_update_vif_sig(vif, sig);
 655}
 656
 657static void iwl_mvm_stat_iterator_all_macs(void *_data, u8 *mac,
 658					   struct ieee80211_vif *vif)
 659{
 660	struct iwl_mvm_stat_data_all_macs *data = _data;
 661	struct iwl_stats_ntfy_per_mac *mac_stats;
 662	int sig;
 663	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
 664	u16 vif_id = mvmvif->id;
 665
 666	if (WARN_ONCE(vif_id >= MAC_INDEX_AUX, "invalid vif id: %d", vif_id))
 667		return;
 668
 669	if (vif->type != NL80211_IFTYPE_STATION)
 670		return;
 671
 672	mac_stats = &data->per_mac[vif_id];
 673
 674	mvmvif->deflink.beacon_stats.num_beacons =
 675		le32_to_cpu(mac_stats->beacon_counter);
 676	mvmvif->deflink.beacon_stats.avg_signal =
 677		-le32_to_cpu(mac_stats->beacon_average_energy);
 678
 679	/* make sure that beacon statistics don't go backwards with TCM
 680	 * request to clear statistics
 681	 */
 682	if (le32_to_cpu(data->flags) & IWL_STATISTICS_REPLY_FLG_CLEAR)
 683		mvmvif->deflink.beacon_stats.accu_num_beacons +=
 684			mvmvif->deflink.beacon_stats.num_beacons;
 685
 686	sig = -le32_to_cpu(mac_stats->beacon_filter_average_energy);
 687	iwl_mvm_update_vif_sig(vif, sig);
 688}
 689
 690static inline void
 691iwl_mvm_rx_stats_check_trigger(struct iwl_mvm *mvm, struct iwl_rx_packet *pkt)
 692{
 693	struct iwl_fw_dbg_trigger_tlv *trig;
 694	struct iwl_fw_dbg_trigger_stats *trig_stats;
 695	u32 trig_offset, trig_thold;
 696
 697	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, NULL, FW_DBG_TRIGGER_STATS);
 698	if (!trig)
 699		return;
 700
 
 701	trig_stats = (void *)trig->data;
 702
 
 
 
 703	trig_offset = le32_to_cpu(trig_stats->stop_offset);
 704	trig_thold = le32_to_cpu(trig_stats->stop_threshold);
 705
 706	if (WARN_ON_ONCE(trig_offset >= iwl_rx_packet_payload_len(pkt)))
 707		return;
 708
 709	if (le32_to_cpup((__le32 *) (pkt->data + trig_offset)) < trig_thold)
 710		return;
 711
 712	iwl_fw_dbg_collect_trig(&mvm->fwrt, trig, NULL);
 713}
 714
 715static void iwl_mvm_stats_energy_iter(void *_data,
 716				      struct ieee80211_sta *sta)
 717{
 718	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
 719	u8 *energy = _data;
 720	u32 sta_id = mvmsta->deflink.sta_id;
 721
 722	if (WARN_ONCE(sta_id >= IWL_MVM_STATION_COUNT_MAX, "sta_id %d >= %d",
 723		      sta_id, IWL_MVM_STATION_COUNT_MAX))
 724		return;
 725
 726	if (energy[sta_id])
 727		mvmsta->deflink.avg_energy = energy[sta_id];
 728
 729}
 730
 731static void
 732iwl_mvm_update_tcm_from_stats(struct iwl_mvm *mvm, __le32 *air_time_le,
 733			      __le32 *rx_bytes_le)
 734{
 735	int i;
 736
 737	spin_lock(&mvm->tcm.lock);
 738	for (i = 0; i < NUM_MAC_INDEX_DRIVER; i++) {
 739		struct iwl_mvm_tcm_mac *mdata = &mvm->tcm.data[i];
 740		u32 rx_bytes = le32_to_cpu(rx_bytes_le[i]);
 741		u32 airtime = le32_to_cpu(air_time_le[i]);
 742
 743		mdata->rx.airtime += airtime;
 744		mdata->uapsd_nonagg_detect.rx_bytes += rx_bytes;
 745		if (airtime) {
 746			/* re-init every time to store rate from FW */
 747			ewma_rate_init(&mdata->uapsd_nonagg_detect.rate);
 748			ewma_rate_add(&mdata->uapsd_nonagg_detect.rate,
 749				      rx_bytes * 8 / airtime);
 750		}
 751	}
 752	spin_unlock(&mvm->tcm.lock);
 753}
 754
 755static void
 756iwl_mvm_stats_ver_15(struct iwl_mvm *mvm,
 757		     struct iwl_statistics_operational_ntfy *stats)
 758{
 759	struct iwl_mvm_stat_data_all_macs data = {
 760		.mvm = mvm,
 761		.flags = stats->flags,
 762		.per_mac = stats->per_mac,
 763	};
 764
 765	ieee80211_iterate_active_interfaces(mvm->hw,
 766					    IEEE80211_IFACE_ITER_NORMAL,
 767					    iwl_mvm_stat_iterator_all_macs,
 768					    &data);
 769}
 770
 771static void
 772iwl_mvm_stats_ver_14(struct iwl_mvm *mvm,
 773		     struct iwl_statistics_operational_ntfy_ver_14 *stats)
 774{
 775	struct iwl_mvm_stat_data data = {
 776		.mvm = mvm,
 777	};
 778
 779	u8 beacon_average_energy[MAC_INDEX_AUX];
 780	__le32 flags;
 781	int i;
 782
 783	flags = stats->flags;
 784
 785	data.mac_id = stats->mac_id;
 786	data.beacon_filter_average_energy =
 787		le32_to_cpu(stats->beacon_filter_average_energy);
 788	data.flags = flags;
 789	data.beacon_counter = stats->beacon_counter;
 790
 791	for (i = 0; i < ARRAY_SIZE(beacon_average_energy); i++)
 792		beacon_average_energy[i] =
 793			le32_to_cpu(stats->beacon_average_energy[i]);
 794
 795	data.beacon_average_energy = beacon_average_energy;
 796
 797	ieee80211_iterate_active_interfaces(mvm->hw,
 798					    IEEE80211_IFACE_ITER_NORMAL,
 799					    iwl_mvm_stat_iterator,
 800					    &data);
 801}
 802
 803static bool iwl_mvm_verify_stats_len(struct iwl_mvm *mvm,
 804				     struct iwl_rx_packet *pkt,
 805				     u32 expected_size)
 806{
 807	struct iwl_statistics_ntfy_hdr *hdr;
 808
 809	if (WARN_ONCE(iwl_rx_packet_payload_len(pkt) < expected_size,
 810		      "received invalid statistics size (%d)!, expected_size: %d\n",
 811		      iwl_rx_packet_payload_len(pkt), expected_size))
 812		return false;
 813
 814	hdr = (void *)&pkt->data;
 815
 816	if (WARN_ONCE((hdr->type & IWL_STATISTICS_TYPE_MSK) != FW_STATISTICS_OPERATIONAL ||
 817		      hdr->version !=
 818		      iwl_fw_lookup_notif_ver(mvm->fw, LEGACY_GROUP, STATISTICS_NOTIFICATION, 0),
 819		      "received unsupported hdr type %d, version %d\n",
 820		      hdr->type, hdr->version))
 821		return false;
 822
 823	if (WARN_ONCE(le16_to_cpu(hdr->size) != expected_size,
 824		      "received invalid statistics size in header (%d)!, expected_size: %d\n",
 825		      le16_to_cpu(hdr->size), expected_size))
 826		return false;
 827
 828	return true;
 829}
 830
 831static void
 832iwl_mvm_stat_iterator_all_links(struct iwl_mvm *mvm,
 833				struct iwl_stats_ntfy_per_link *per_link)
 834{
 835	u32 air_time[MAC_INDEX_AUX] = {};
 836	u32 rx_bytes[MAC_INDEX_AUX] = {};
 837	int fw_link_id;
 838
 839	for (fw_link_id = 0; fw_link_id < ARRAY_SIZE(mvm->link_id_to_link_conf);
 840	     fw_link_id++) {
 841		struct iwl_stats_ntfy_per_link *link_stats;
 842		struct ieee80211_bss_conf *bss_conf;
 843		struct iwl_mvm_vif *mvmvif;
 844		int link_id;
 845		int sig;
 846
 847		bss_conf = iwl_mvm_rcu_fw_link_id_to_link_conf(mvm, fw_link_id,
 848							       false);
 849		if (!bss_conf)
 850			continue;
 851
 852		if (bss_conf->vif->type != NL80211_IFTYPE_STATION)
 853			continue;
 854
 855		link_id = bss_conf->link_id;
 856		if (link_id >= ARRAY_SIZE(mvmvif->link))
 857			continue;
 858
 859		mvmvif = iwl_mvm_vif_from_mac80211(bss_conf->vif);
 860		if (!mvmvif || !mvmvif->link[link_id])
 861			continue;
 862
 863		link_stats = &per_link[fw_link_id];
 864
 865		mvmvif->link[link_id]->beacon_stats.num_beacons =
 866			le32_to_cpu(link_stats->beacon_counter);
 867
 868		/* we basically just use the u8 to store 8 bits and then treat
 869		 * it as a s8 whenever we take it out to a different type.
 870		 */
 871		mvmvif->link[link_id]->beacon_stats.avg_signal =
 872			-le32_to_cpu(link_stats->beacon_average_energy);
 873
 874		/* make sure that beacon statistics don't go backwards with TCM
 875		 * request to clear statistics
 876		 */
 877		if (mvm->statistics_clear)
 878			mvmvif->link[link_id]->beacon_stats.accu_num_beacons +=
 879				mvmvif->link[link_id]->beacon_stats.num_beacons;
 880
 881		sig = -le32_to_cpu(link_stats->beacon_filter_average_energy);
 882		iwl_mvm_update_vif_sig(bss_conf->vif, sig);
 883
 884		if (WARN_ONCE(mvmvif->id >= MAC_INDEX_AUX,
 885			      "invalid mvmvif id: %d", mvmvif->id))
 886			continue;
 887
 888		air_time[mvmvif->id] +=
 889			le32_to_cpu(per_link[fw_link_id].air_time);
 890		rx_bytes[mvmvif->id] +=
 891			le32_to_cpu(per_link[fw_link_id].rx_bytes);
 892	}
 893
 894	/* Don't update in case the statistics are not cleared, since
 895	 * we will end up counting twice the same airtime, once in TCM
 896	 * request and once in statistics notification.
 897	 */
 898	if (mvm->statistics_clear) {
 899		__le32 air_time_le[MAC_INDEX_AUX];
 900		__le32 rx_bytes_le[MAC_INDEX_AUX];
 901		int vif_id;
 902
 903		for (vif_id = 0; vif_id < ARRAY_SIZE(air_time_le); vif_id++) {
 904			air_time_le[vif_id] = cpu_to_le32(air_time[vif_id]);
 905			rx_bytes_le[vif_id] = cpu_to_le32(rx_bytes[vif_id]);
 906		}
 907
 908		iwl_mvm_update_tcm_from_stats(mvm, air_time_le, rx_bytes_le);
 909	}
 910}
 911
 912void iwl_mvm_handle_rx_system_oper_stats(struct iwl_mvm *mvm,
 913					 struct iwl_rx_cmd_buffer *rxb)
 914{
 915	u8 average_energy[IWL_MVM_STATION_COUNT_MAX];
 916	struct iwl_rx_packet *pkt = rxb_addr(rxb);
 917	struct iwl_system_statistics_notif_oper *stats;
 918	int i;
 919	u32 notif_ver = iwl_fw_lookup_notif_ver(mvm->fw, STATISTICS_GROUP,
 920						STATISTICS_OPER_NOTIF, 0);
 921
 922	if (notif_ver != 3) {
 923		IWL_FW_CHECK_FAILED(mvm,
 924				    "Oper stats notif ver %d is not supported\n",
 925				    notif_ver);
 926		return;
 927	}
 928
 929	stats = (void *)&pkt->data;
 930	iwl_mvm_stat_iterator_all_links(mvm, stats->per_link);
 931
 932	for (i = 0; i < ARRAY_SIZE(average_energy); i++)
 933		average_energy[i] =
 934			le32_to_cpu(stats->per_sta[i].average_energy);
 935
 936	ieee80211_iterate_stations_atomic(mvm->hw, iwl_mvm_stats_energy_iter,
 937					  average_energy);
 938}
 939
 940void iwl_mvm_handle_rx_system_oper_part1_stats(struct iwl_mvm *mvm,
 941					       struct iwl_rx_cmd_buffer *rxb)
 942{
 943	struct iwl_rx_packet *pkt = rxb_addr(rxb);
 944	struct iwl_system_statistics_part1_notif_oper *part1_stats;
 945	int i;
 946	u32 notif_ver = iwl_fw_lookup_notif_ver(mvm->fw, STATISTICS_GROUP,
 947						STATISTICS_OPER_PART1_NOTIF, 0);
 948
 949	if (notif_ver != 4) {
 950		IWL_FW_CHECK_FAILED(mvm,
 951				    "Part1 stats notif ver %d is not supported\n",
 952				    notif_ver);
 953		return;
 954	}
 955
 956	part1_stats = (void *)&pkt->data;
 957	mvm->radio_stats.rx_time = 0;
 958	mvm->radio_stats.tx_time = 0;
 959	for (i = 0; i < ARRAY_SIZE(part1_stats->per_link); i++) {
 960		mvm->radio_stats.rx_time +=
 961			le64_to_cpu(part1_stats->per_link[i].rx_time);
 962		mvm->radio_stats.tx_time +=
 963			le64_to_cpu(part1_stats->per_link[i].tx_time);
 964	}
 965}
 966
 967static void
 968iwl_mvm_handle_rx_statistics_tlv(struct iwl_mvm *mvm,
 969				 struct iwl_rx_packet *pkt)
 970{
 971	u8 average_energy[IWL_MVM_STATION_COUNT_MAX];
 972	__le32 air_time[MAC_INDEX_AUX];
 973	__le32 rx_bytes[MAC_INDEX_AUX];
 974	__le32 flags = 0;
 975	int i;
 976	u32 notif_ver = iwl_fw_lookup_notif_ver(mvm->fw, LEGACY_GROUP,
 977					      STATISTICS_NOTIFICATION, 0);
 978
 979	if (WARN_ONCE(notif_ver > 15,
 980		      "invalid statistics version id: %d\n", notif_ver))
 981		return;
 982
 983	if (notif_ver == 14) {
 984		struct iwl_statistics_operational_ntfy_ver_14 *stats =
 985			(void *)pkt->data;
 986
 987		if (!iwl_mvm_verify_stats_len(mvm, pkt, sizeof(*stats)))
 988			return;
 989
 990		iwl_mvm_stats_ver_14(mvm, stats);
 991
 992		flags = stats->flags;
 993		mvm->radio_stats.rx_time = le64_to_cpu(stats->rx_time);
 994		mvm->radio_stats.tx_time = le64_to_cpu(stats->tx_time);
 995		mvm->radio_stats.on_time_rf = le64_to_cpu(stats->on_time_rf);
 996		mvm->radio_stats.on_time_scan =
 997			le64_to_cpu(stats->on_time_scan);
 998
 999		for (i = 0; i < ARRAY_SIZE(average_energy); i++)
1000			average_energy[i] = le32_to_cpu(stats->average_energy[i]);
1001
1002		for (i = 0; i < ARRAY_SIZE(air_time); i++) {
1003			air_time[i] = stats->air_time[i];
1004			rx_bytes[i] = stats->rx_bytes[i];
1005		}
1006	}
1007
1008	if (notif_ver == 15) {
1009		struct iwl_statistics_operational_ntfy *stats =
1010			(void *)pkt->data;
1011
1012		if (!iwl_mvm_verify_stats_len(mvm, pkt, sizeof(*stats)))
1013			return;
1014
1015		iwl_mvm_stats_ver_15(mvm, stats);
1016
1017		flags = stats->flags;
1018		mvm->radio_stats.rx_time = le64_to_cpu(stats->rx_time);
1019		mvm->radio_stats.tx_time = le64_to_cpu(stats->tx_time);
1020		mvm->radio_stats.on_time_rf = le64_to_cpu(stats->on_time_rf);
1021		mvm->radio_stats.on_time_scan =
1022			le64_to_cpu(stats->on_time_scan);
1023
1024		for (i = 0; i < ARRAY_SIZE(average_energy); i++)
1025			average_energy[i] =
1026				le32_to_cpu(stats->per_sta[i].average_energy);
1027
1028		for (i = 0; i < ARRAY_SIZE(air_time); i++) {
1029			air_time[i] = stats->per_mac[i].air_time;
1030			rx_bytes[i] = stats->per_mac[i].rx_bytes;
1031		}
1032	}
1033
1034	iwl_mvm_rx_stats_check_trigger(mvm, pkt);
1035
1036	ieee80211_iterate_stations_atomic(mvm->hw, iwl_mvm_stats_energy_iter,
1037					  average_energy);
1038	/*
1039	 * Don't update in case the statistics are not cleared, since
1040	 * we will end up counting twice the same airtime, once in TCM
1041	 * request and once in statistics notification.
1042	 */
1043	if (le32_to_cpu(flags) & IWL_STATISTICS_REPLY_FLG_CLEAR)
1044		iwl_mvm_update_tcm_from_stats(mvm, air_time, rx_bytes);
1045}
1046
1047void iwl_mvm_handle_rx_statistics(struct iwl_mvm *mvm,
1048				  struct iwl_rx_packet *pkt)
1049{
1050	struct iwl_mvm_stat_data data = {
1051		.mvm = mvm,
1052	};
1053	__le32 *bytes, *air_time, flags;
1054	int expected_size;
 
1055	u8 *energy;
1056	u8 cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw,
1057					   WIDE_ID(SYSTEM_GROUP,
1058						   SYSTEM_STATISTICS_CMD),
1059					   IWL_FW_CMD_VER_UNKNOWN);
1060
1061	if (cmd_ver != IWL_FW_CMD_VER_UNKNOWN)
1062		return;
1063
1064	/* From ver 14 and up we use TLV statistics format */
1065	if (iwl_fw_lookup_notif_ver(mvm->fw, LEGACY_GROUP,
1066				    STATISTICS_NOTIFICATION, 0) >= 14)
1067		return iwl_mvm_handle_rx_statistics_tlv(mvm, pkt);
1068
1069	if (!iwl_mvm_has_new_rx_stats_api(mvm)) {
1070		if (iwl_mvm_has_new_rx_api(mvm))
1071			expected_size = sizeof(struct iwl_notif_statistics_v11);
1072		else
1073			expected_size = sizeof(struct iwl_notif_statistics_v10);
1074	} else {
1075		expected_size = sizeof(struct iwl_notif_statistics);
1076	}
1077
1078	if (WARN_ONCE(iwl_rx_packet_payload_len(pkt) != expected_size,
1079		      "received invalid statistics size (%d)!\n",
1080		      iwl_rx_packet_payload_len(pkt)))
1081		return;
1082
1083	if (!iwl_mvm_has_new_rx_stats_api(mvm)) {
1084		struct iwl_notif_statistics_v11 *stats = (void *)&pkt->data;
1085
1086		data.mac_id = stats->rx.general.mac_id;
1087		data.beacon_filter_average_energy =
1088			stats->general.common.beacon_filter_average_energy;
1089
1090		mvm->rx_stats_v3 = stats->rx;
1091
1092		mvm->radio_stats.rx_time =
1093			le64_to_cpu(stats->general.common.rx_time);
1094		mvm->radio_stats.tx_time =
1095			le64_to_cpu(stats->general.common.tx_time);
1096		mvm->radio_stats.on_time_rf =
1097			le64_to_cpu(stats->general.common.on_time_rf);
1098		mvm->radio_stats.on_time_scan =
1099			le64_to_cpu(stats->general.common.on_time_scan);
1100
1101		data.beacon_counter = stats->general.beacon_counter;
1102		data.beacon_average_energy =
1103			stats->general.beacon_average_energy;
1104		flags = stats->flag;
1105	} else {
1106		struct iwl_notif_statistics *stats = (void *)&pkt->data;
1107
1108		data.mac_id = stats->rx.general.mac_id;
1109		data.beacon_filter_average_energy =
1110			stats->general.common.beacon_filter_average_energy;
1111
1112		mvm->rx_stats = stats->rx;
1113
1114		mvm->radio_stats.rx_time =
1115			le64_to_cpu(stats->general.common.rx_time);
1116		mvm->radio_stats.tx_time =
1117			le64_to_cpu(stats->general.common.tx_time);
1118		mvm->radio_stats.on_time_rf =
1119			le64_to_cpu(stats->general.common.on_time_rf);
1120		mvm->radio_stats.on_time_scan =
1121			le64_to_cpu(stats->general.common.on_time_scan);
1122
1123		data.beacon_counter = stats->general.beacon_counter;
1124		data.beacon_average_energy =
1125			stats->general.beacon_average_energy;
1126		flags = stats->flag;
1127	}
1128	data.flags = flags;
1129
1130	iwl_mvm_rx_stats_check_trigger(mvm, pkt);
1131
1132	ieee80211_iterate_active_interfaces(mvm->hw,
1133					    IEEE80211_IFACE_ITER_NORMAL,
1134					    iwl_mvm_stat_iterator,
1135					    &data);
1136
1137	if (!iwl_mvm_has_new_rx_api(mvm))
1138		return;
1139
1140	if (!iwl_mvm_has_new_rx_stats_api(mvm)) {
1141		struct iwl_notif_statistics_v11 *v11 = (void *)&pkt->data;
1142
1143		energy = (void *)&v11->load_stats.avg_energy;
1144		bytes = (void *)&v11->load_stats.byte_count;
1145		air_time = (void *)&v11->load_stats.air_time;
1146	} else {
1147		struct iwl_notif_statistics *stats = (void *)&pkt->data;
1148
1149		energy = (void *)&stats->load_stats.avg_energy;
1150		bytes = (void *)&stats->load_stats.byte_count;
1151		air_time = (void *)&stats->load_stats.air_time;
1152	}
1153	ieee80211_iterate_stations_atomic(mvm->hw, iwl_mvm_stats_energy_iter,
1154					  energy);
1155
1156	/*
1157	 * Don't update in case the statistics are not cleared, since
1158	 * we will end up counting twice the same airtime, once in TCM
1159	 * request and once in statistics notification.
1160	 */
1161	if (le32_to_cpu(flags) & IWL_STATISTICS_REPLY_FLG_CLEAR)
1162		iwl_mvm_update_tcm_from_stats(mvm, air_time, bytes);
1163
 
 
 
 
 
 
1164}
1165
1166void iwl_mvm_rx_statistics(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
1167{
1168	iwl_mvm_handle_rx_statistics(mvm, rxb_addr(rxb));
1169}
1170
1171void iwl_mvm_window_status_notif(struct iwl_mvm *mvm,
1172				 struct iwl_rx_cmd_buffer *rxb)
1173{
1174	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1175	struct iwl_ba_window_status_notif *notif = (void *)pkt->data;
1176	int i;
 
1177
1178	BUILD_BUG_ON(ARRAY_SIZE(notif->ra_tid) != BA_WINDOW_STREAMS_MAX);
1179	BUILD_BUG_ON(ARRAY_SIZE(notif->mpdu_rx_count) != BA_WINDOW_STREAMS_MAX);
1180	BUILD_BUG_ON(ARRAY_SIZE(notif->bitmap) != BA_WINDOW_STREAMS_MAX);
1181	BUILD_BUG_ON(ARRAY_SIZE(notif->start_seq_num) != BA_WINDOW_STREAMS_MAX);
1182
1183	rcu_read_lock();
1184	for (i = 0; i < BA_WINDOW_STREAMS_MAX; i++) {
1185		struct ieee80211_sta *sta;
1186		u8 sta_id, tid;
1187		u64 bitmap;
1188		u32 ssn;
1189		u16 ratid;
1190		u16 received_mpdu;
1191
1192		ratid = le16_to_cpu(notif->ra_tid[i]);
1193		/* check that this TID is valid */
1194		if (!(ratid & BA_WINDOW_STATUS_VALID_MSK))
1195			continue;
1196
1197		received_mpdu = le16_to_cpu(notif->mpdu_rx_count[i]);
1198		if (received_mpdu == 0)
1199			continue;
1200
1201		tid = ratid & BA_WINDOW_STATUS_TID_MSK;
1202		/* get the station */
1203		sta_id = (ratid & BA_WINDOW_STATUS_STA_ID_MSK)
1204			 >> BA_WINDOW_STATUS_STA_ID_POS;
1205		sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
1206		if (IS_ERR_OR_NULL(sta))
1207			continue;
1208		bitmap = le64_to_cpu(notif->bitmap[i]);
1209		ssn = le32_to_cpu(notif->start_seq_num[i]);
1210
1211		/* update mac80211 with the bitmap for the reordering buffer */
1212		ieee80211_mark_rx_ba_filtered_frames(sta, tid, ssn, bitmap,
1213						     received_mpdu);
1214	}
1215	rcu_read_unlock();
1216}
v4.17
  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 *
 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
 71/*
 72 * iwl_mvm_rx_rx_phy_cmd - REPLY_RX_PHY_CMD handler
 73 *
 74 * Copies the phy information in mvm->last_phy_info, it will be used when the
 75 * actual data will come from the fw in the next packet.
 76 */
 77void iwl_mvm_rx_rx_phy_cmd(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
 78{
 79	struct iwl_rx_packet *pkt = rxb_addr(rxb);
 
 
 
 
 80
 81	memcpy(&mvm->last_phy_info, pkt->data, sizeof(mvm->last_phy_info));
 82	mvm->ampdu_ref++;
 83
 84#ifdef CONFIG_IWLWIFI_DEBUGFS
 85	if (mvm->last_phy_info.phy_flags & cpu_to_le16(RX_RES_PHY_FLAGS_AGG)) {
 86		spin_lock(&mvm->drv_stats_lock);
 87		mvm->drv_rx_stats.ampdu_count++;
 88		spin_unlock(&mvm->drv_stats_lock);
 89	}
 90#endif
 91}
 92
 93/*
 94 * iwl_mvm_pass_packet_to_mac80211 - builds the packet for mac80211
 95 *
 96 * Adds the rxb to a new skb and give it to mac80211
 97 */
 98static void iwl_mvm_pass_packet_to_mac80211(struct iwl_mvm *mvm,
 99					    struct ieee80211_sta *sta,
100					    struct napi_struct *napi,
101					    struct sk_buff *skb,
102					    struct ieee80211_hdr *hdr, u16 len,
103					    u8 crypt_len,
104					    struct iwl_rx_cmd_buffer *rxb)
105{
106	unsigned int hdrlen = ieee80211_hdrlen(hdr->frame_control);
107	unsigned int fraglen;
108
109	/*
110	 * The 'hdrlen' (plus the 8 bytes for the SNAP and the crypt_len,
111	 * but those are all multiples of 4 long) all goes away, but we
112	 * want the *end* of it, which is going to be the start of the IP
113	 * header, to be aligned when it gets pulled in.
114	 * The beginning of the skb->data is aligned on at least a 4-byte
115	 * boundary after allocation. Everything here is aligned at least
116	 * on a 2-byte boundary so we can just take hdrlen & 3 and pad by
117	 * the result.
118	 */
119	skb_reserve(skb, hdrlen & 3);
120
121	/* If frame is small enough to fit in skb->head, pull it completely.
122	 * If not, only pull ieee80211_hdr (including crypto if present, and
123	 * an additional 8 bytes for SNAP/ethertype, see below) so that
124	 * splice() or TCP coalesce are more efficient.
125	 *
126	 * Since, in addition, ieee80211_data_to_8023() always pull in at
127	 * least 8 bytes (possibly more for mesh) we can do the same here
128	 * to save the cost of doing it later. That still doesn't pull in
129	 * the actual IP header since the typical case has a SNAP header.
130	 * If the latter changes (there are efforts in the standards group
131	 * to do so) we should revisit this and ieee80211_data_to_8023().
132	 */
133	hdrlen = (len <= skb_tailroom(skb)) ? len : hdrlen + crypt_len + 8;
134
135	skb_put_data(skb, hdr, hdrlen);
136	fraglen = len - hdrlen;
137
138	if (fraglen) {
139		int offset = (void *)hdr + hdrlen -
140			     rxb_addr(rxb) + rxb_offset(rxb);
141
142		skb_add_rx_frag(skb, 0, rxb_steal_page(rxb), offset,
143				fraglen, rxb->truesize);
144	}
145
146	ieee80211_rx_napi(mvm->hw, sta, skb, napi);
147}
148
149/*
150 * iwl_mvm_get_signal_strength - use new rx PHY INFO API
151 * values are reported by the fw as positive values - need to negate
152 * to obtain their dBM.  Account for missing antennas by replacing 0
153 * values by -256dBm: practically 0 power and a non-feasible 8 bit value.
154 */
155static void iwl_mvm_get_signal_strength(struct iwl_mvm *mvm,
156					struct iwl_rx_phy_info *phy_info,
157					struct ieee80211_rx_status *rx_status)
158{
159	int energy_a, energy_b, energy_c, max_energy;
160	u32 val;
161
162	val =
163	    le32_to_cpu(phy_info->non_cfg_phy[IWL_RX_INFO_ENERGY_ANT_ABC_IDX]);
164	energy_a = (val & IWL_RX_INFO_ENERGY_ANT_A_MSK) >>
165						IWL_RX_INFO_ENERGY_ANT_A_POS;
166	energy_a = energy_a ? -energy_a : S8_MIN;
167	energy_b = (val & IWL_RX_INFO_ENERGY_ANT_B_MSK) >>
168						IWL_RX_INFO_ENERGY_ANT_B_POS;
169	energy_b = energy_b ? -energy_b : S8_MIN;
170	energy_c = (val & IWL_RX_INFO_ENERGY_ANT_C_MSK) >>
171						IWL_RX_INFO_ENERGY_ANT_C_POS;
172	energy_c = energy_c ? -energy_c : S8_MIN;
173	max_energy = max(energy_a, energy_b);
174	max_energy = max(max_energy, energy_c);
175
176	IWL_DEBUG_STATS(mvm, "energy In A %d B %d C %d , and max %d\n",
177			energy_a, energy_b, energy_c, max_energy);
178
179	rx_status->signal = max_energy;
180	rx_status->chains = (le16_to_cpu(phy_info->phy_flags) &
181				RX_RES_PHY_FLAGS_ANTENNA)
182					>> RX_RES_PHY_FLAGS_ANTENNA_POS;
183	rx_status->chain_signal[0] = energy_a;
184	rx_status->chain_signal[1] = energy_b;
185	rx_status->chain_signal[2] = energy_c;
186}
187
188/*
189 * iwl_mvm_set_mac80211_rx_flag - translate fw status to mac80211 format
190 * @mvm: the mvm object
191 * @hdr: 80211 header
192 * @stats: status in mac80211's format
193 * @rx_pkt_status: status coming from fw
194 *
195 * returns non 0 value if the packet should be dropped
196 */
197static u32 iwl_mvm_set_mac80211_rx_flag(struct iwl_mvm *mvm,
198					struct ieee80211_hdr *hdr,
199					struct ieee80211_rx_status *stats,
200					u32 rx_pkt_status,
201					u8 *crypt_len)
202{
203	if (!ieee80211_has_protected(hdr->frame_control) ||
204	    (rx_pkt_status & RX_MPDU_RES_STATUS_SEC_ENC_MSK) ==
205			     RX_MPDU_RES_STATUS_SEC_NO_ENC)
206		return 0;
207
208	/* packet was encrypted with unknown alg */
209	if ((rx_pkt_status & RX_MPDU_RES_STATUS_SEC_ENC_MSK) ==
210					RX_MPDU_RES_STATUS_SEC_ENC_ERR)
211		return 0;
212
213	switch (rx_pkt_status & RX_MPDU_RES_STATUS_SEC_ENC_MSK) {
214	case RX_MPDU_RES_STATUS_SEC_CCM_ENC:
215		/* alg is CCM: check MIC only */
216		if (!(rx_pkt_status & RX_MPDU_RES_STATUS_MIC_OK))
217			return -1;
218
219		stats->flag |= RX_FLAG_DECRYPTED;
220		*crypt_len = IEEE80211_CCMP_HDR_LEN;
221		return 0;
222
223	case RX_MPDU_RES_STATUS_SEC_TKIP_ENC:
224		/* Don't drop the frame and decrypt it in SW */
225		if (!fw_has_api(&mvm->fw->ucode_capa,
226				IWL_UCODE_TLV_API_DEPRECATE_TTAK) &&
227		    !(rx_pkt_status & RX_MPDU_RES_STATUS_TTAK_OK))
228			return 0;
229		*crypt_len = IEEE80211_TKIP_IV_LEN;
230		/* fall through if TTAK OK */
231
232	case RX_MPDU_RES_STATUS_SEC_WEP_ENC:
233		if (!(rx_pkt_status & RX_MPDU_RES_STATUS_ICV_OK))
234			return -1;
235
236		stats->flag |= RX_FLAG_DECRYPTED;
237		if ((rx_pkt_status & RX_MPDU_RES_STATUS_SEC_ENC_MSK) ==
238				RX_MPDU_RES_STATUS_SEC_WEP_ENC)
239			*crypt_len = IEEE80211_WEP_IV_LEN;
240		return 0;
241
242	case RX_MPDU_RES_STATUS_SEC_EXT_ENC:
243		if (!(rx_pkt_status & RX_MPDU_RES_STATUS_MIC_OK))
244			return -1;
245		stats->flag |= RX_FLAG_DECRYPTED;
246		return 0;
247
248	default:
249		/* Expected in monitor (not having the keys) */
250		if (!mvm->monitor_on)
251			IWL_ERR(mvm, "Unhandled alg: 0x%x\n", rx_pkt_status);
252	}
253
254	return 0;
255}
256
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
257static void iwl_mvm_rx_csum(struct ieee80211_sta *sta,
258			    struct sk_buff *skb,
259			    u32 status)
260{
261	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
262	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(mvmsta->vif);
263
264	if (mvmvif->features & NETIF_F_RXCSUM &&
265	    status & RX_MPDU_RES_STATUS_CSUM_DONE &&
266	    status & RX_MPDU_RES_STATUS_CSUM_OK)
267		skb->ip_summed = CHECKSUM_UNNECESSARY;
268}
269
270/*
271 * iwl_mvm_rx_rx_mpdu - REPLY_RX_MPDU_CMD handler
272 *
273 * Handles the actual data of the Rx packet from the fw
274 */
275void iwl_mvm_rx_rx_mpdu(struct iwl_mvm *mvm, struct napi_struct *napi,
276			struct iwl_rx_cmd_buffer *rxb)
277{
278	struct ieee80211_hdr *hdr;
279	struct ieee80211_rx_status *rx_status;
280	struct iwl_rx_packet *pkt = rxb_addr(rxb);
281	struct iwl_rx_phy_info *phy_info;
282	struct iwl_rx_mpdu_res_start *rx_res;
283	struct ieee80211_sta *sta = NULL;
284	struct sk_buff *skb;
285	u32 len;
286	u32 rate_n_flags;
287	u32 rx_pkt_status;
288	u8 crypt_len = 0;
289	bool take_ref;
 
 
 
 
290
291	phy_info = &mvm->last_phy_info;
292	rx_res = (struct iwl_rx_mpdu_res_start *)pkt->data;
293	hdr = (struct ieee80211_hdr *)(pkt->data + sizeof(*rx_res));
294	len = le16_to_cpu(rx_res->byte_count);
295	rx_pkt_status = le32_to_cpup((__le32 *)
 
 
 
 
 
 
296		(pkt->data + sizeof(*rx_res) + len));
297
298	/* Dont use dev_alloc_skb(), we'll have enough headroom once
299	 * ieee80211_hdr pulled.
300	 */
301	skb = alloc_skb(128, GFP_ATOMIC);
302	if (!skb) {
303		IWL_ERR(mvm, "alloc_skb failed\n");
304		return;
305	}
306
307	rx_status = IEEE80211_SKB_RXCB(skb);
308
309	/*
310	 * drop the packet if it has failed being decrypted by HW
311	 */
312	if (iwl_mvm_set_mac80211_rx_flag(mvm, hdr, rx_status, rx_pkt_status,
313					 &crypt_len)) {
314		IWL_DEBUG_DROP(mvm, "Bad decryption results 0x%08x\n",
315			       rx_pkt_status);
316		kfree_skb(skb);
317		return;
318	}
319
320	/*
321	 * Keep packets with CRC errors (and with overrun) for monitor mode
322	 * (otherwise the firmware discards them) but mark them as bad.
323	 */
324	if (!(rx_pkt_status & RX_MPDU_RES_STATUS_CRC_OK) ||
325	    !(rx_pkt_status & RX_MPDU_RES_STATUS_OVERRUN_OK)) {
326		IWL_DEBUG_RX(mvm, "Bad CRC or FIFO: 0x%08X.\n", rx_pkt_status);
327		rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
328	}
329
330	/* This will be used in several places later */
331	rate_n_flags = le32_to_cpu(phy_info->rate_n_flags);
332
333	/* rx_status carries information about the packet to mac80211 */
334	rx_status->mactime = le64_to_cpu(phy_info->timestamp);
335	rx_status->device_timestamp = le32_to_cpu(phy_info->system_timestamp);
336	rx_status->band =
337		(phy_info->phy_flags & cpu_to_le16(RX_RES_PHY_FLAGS_BAND_24)) ?
338				NL80211_BAND_2GHZ : NL80211_BAND_5GHZ;
339	rx_status->freq =
340		ieee80211_channel_to_frequency(le16_to_cpu(phy_info->channel),
341					       rx_status->band);
342
343	/* TSF as indicated by the firmware  is at INA time */
344	rx_status->flag |= RX_FLAG_MACTIME_PLCP_START;
345
346	iwl_mvm_get_signal_strength(mvm, phy_info, rx_status);
347
348	IWL_DEBUG_STATS_LIMIT(mvm, "Rssi %d, TSF %llu\n", rx_status->signal,
349			      (unsigned long long)rx_status->mactime);
350
351	rcu_read_lock();
352	if (rx_pkt_status & RX_MPDU_RES_STATUS_SRC_STA_FOUND) {
353		u32 id = rx_pkt_status & RX_MPDU_RES_STATUS_STA_ID_MSK;
354
355		id >>= RX_MDPU_RES_STATUS_STA_ID_SHIFT;
356
357		if (!WARN_ON_ONCE(id >= ARRAY_SIZE(mvm->fw_id_to_mac_id))) {
358			sta = rcu_dereference(mvm->fw_id_to_mac_id[id]);
359			if (IS_ERR(sta))
360				sta = NULL;
361		}
362	} else if (!is_multicast_ether_addr(hdr->addr2)) {
363		/* This is fine since we prevent two stations with the same
364		 * address from being added.
365		 */
366		sta = ieee80211_find_sta_by_ifaddr(mvm->hw, hdr->addr2, NULL);
367	}
368
369	if (sta) {
370		struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
371		struct ieee80211_vif *tx_blocked_vif =
372			rcu_dereference(mvm->csa_tx_blocked_vif);
 
 
373
374		/* We have tx blocked stations (with CS bit). If we heard
375		 * frames from a blocked station on a new channel we can
376		 * TX to it again.
377		 */
378		if (unlikely(tx_blocked_vif) &&
379		    mvmsta->vif == tx_blocked_vif) {
380			struct iwl_mvm_vif *mvmvif =
381				iwl_mvm_vif_from_mac80211(tx_blocked_vif);
382
383			if (mvmvif->csa_target_freq == rx_status->freq)
384				iwl_mvm_sta_modify_disable_tx_ap(mvm, sta,
385								 false);
386		}
387
388		rs_update_last_rssi(mvm, mvmsta, rx_status);
389
390		if (iwl_fw_dbg_trigger_enabled(mvm->fw, FW_DBG_TRIGGER_RSSI) &&
391		    ieee80211_is_beacon(hdr->frame_control)) {
392			struct iwl_fw_dbg_trigger_tlv *trig;
 
 
393			struct iwl_fw_dbg_trigger_low_rssi *rssi_trig;
394			bool trig_check;
395			s32 rssi;
396
397			trig = iwl_fw_dbg_get_trigger(mvm->fw,
398						      FW_DBG_TRIGGER_RSSI);
399			rssi_trig = (void *)trig->data;
400			rssi = le32_to_cpu(rssi_trig->rssi);
401
402			trig_check =
403				iwl_fw_dbg_trigger_check_stop(&mvm->fwrt,
404							      ieee80211_vif_to_wdev(mvmsta->vif),
405							      trig);
406			if (trig_check && rx_status->signal < rssi)
407				iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
408							NULL);
409		}
410
 
 
 
 
 
 
411		if (ieee80211_is_data(hdr->frame_control))
412			iwl_mvm_rx_csum(sta, skb, rx_pkt_status);
413	}
414	rcu_read_unlock();
415
416	/* set the preamble flag if appropriate */
417	if (phy_info->phy_flags & cpu_to_le16(RX_RES_PHY_FLAGS_SHORT_PREAMBLE))
418		rx_status->enc_flags |= RX_ENC_FLAG_SHORTPRE;
419
420	if (phy_info->phy_flags & cpu_to_le16(RX_RES_PHY_FLAGS_AGG)) {
421		/*
422		 * We know which subframes of an A-MPDU belong
423		 * together since we get a single PHY response
424		 * from the firmware for all of them
425		 */
426		rx_status->flag |= RX_FLAG_AMPDU_DETAILS;
427		rx_status->ampdu_reference = mvm->ampdu_ref;
428	}
429
430	/* Set up the HT phy flags */
431	switch (rate_n_flags & RATE_MCS_CHAN_WIDTH_MSK) {
432	case RATE_MCS_CHAN_WIDTH_20:
433		break;
434	case RATE_MCS_CHAN_WIDTH_40:
435		rx_status->bw = RATE_INFO_BW_40;
436		break;
437	case RATE_MCS_CHAN_WIDTH_80:
438		rx_status->bw = RATE_INFO_BW_80;
439		break;
440	case RATE_MCS_CHAN_WIDTH_160:
441		rx_status->bw = RATE_INFO_BW_160;
442		break;
443	}
444	if (!(rate_n_flags & RATE_MCS_CCK_MSK) &&
445	    rate_n_flags & RATE_MCS_SGI_MSK)
446		rx_status->enc_flags |= RX_ENC_FLAG_SHORT_GI;
447	if (rate_n_flags & RATE_HT_MCS_GF_MSK)
448		rx_status->enc_flags |= RX_ENC_FLAG_HT_GF;
449	if (rate_n_flags & RATE_MCS_LDPC_MSK)
450		rx_status->enc_flags |= RX_ENC_FLAG_LDPC;
451	if (rate_n_flags & RATE_MCS_HT_MSK) {
452		u8 stbc = (rate_n_flags & RATE_MCS_STBC_MSK) >>
453				RATE_MCS_STBC_POS;
454		rx_status->encoding = RX_ENC_HT;
455		rx_status->rate_idx = rate_n_flags & RATE_HT_MCS_INDEX_MSK;
456		rx_status->enc_flags |= stbc << RX_ENC_FLAG_STBC_SHIFT;
457	} else if (rate_n_flags & RATE_MCS_VHT_MSK) {
458		u8 stbc = (rate_n_flags & RATE_MCS_STBC_MSK) >>
459				RATE_MCS_STBC_POS;
460		rx_status->nss =
461			((rate_n_flags & RATE_VHT_MCS_NSS_MSK) >>
462						RATE_VHT_MCS_NSS_POS) + 1;
463		rx_status->rate_idx = rate_n_flags & RATE_VHT_MCS_RATE_CODE_MSK;
464		rx_status->encoding = RX_ENC_VHT;
465		rx_status->enc_flags |= stbc << RX_ENC_FLAG_STBC_SHIFT;
466		if (rate_n_flags & RATE_MCS_BF_MSK)
467			rx_status->enc_flags |= RX_ENC_FLAG_BF;
468	} else {
469		int rate = iwl_mvm_legacy_rate_to_mac80211_idx(rate_n_flags,
470							       rx_status->band);
471
472		if (WARN(rate < 0 || rate > 0xFF,
473			 "Invalid rate flags 0x%x, band %d,\n",
474			 rate_n_flags, rx_status->band)) {
475			kfree_skb(skb);
476			return;
477		}
478		rx_status->rate_idx = rate;
479	}
480
481#ifdef CONFIG_IWLWIFI_DEBUGFS
482	iwl_mvm_update_frame_stats(mvm, rate_n_flags,
483				   rx_status->flag & RX_FLAG_AMPDU_DETAILS);
484#endif
485
486	if (unlikely((ieee80211_is_beacon(hdr->frame_control) ||
487		      ieee80211_is_probe_resp(hdr->frame_control)) &&
488		     mvm->sched_scan_pass_all == SCHED_SCAN_PASS_ALL_ENABLED))
489		mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_FOUND;
490
491	if (unlikely(ieee80211_is_beacon(hdr->frame_control) ||
492		     ieee80211_is_probe_resp(hdr->frame_control)))
493		rx_status->boottime_ns = ktime_get_boot_ns();
494
495	/* Take a reference briefly to kick off a d0i3 entry delay so
496	 * we can handle bursts of RX packets without toggling the
497	 * state too often.  But don't do this for beacons if we are
498	 * going to idle because the beacon filtering changes we make
499	 * cause the firmware to send us collateral beacons. */
500	take_ref = !(test_bit(STATUS_TRANS_GOING_IDLE, &mvm->trans->status) &&
501		     ieee80211_is_beacon(hdr->frame_control));
502
503	if (take_ref)
504		iwl_mvm_ref(mvm, IWL_MVM_REF_RX);
505
506	iwl_mvm_pass_packet_to_mac80211(mvm, sta, napi, skb, hdr, len,
507					crypt_len, rxb);
508
509	if (take_ref)
510		iwl_mvm_unref(mvm, IWL_MVM_REF_RX);
511}
512
513struct iwl_mvm_stat_data {
514	struct iwl_mvm *mvm;
 
515	__le32 mac_id;
516	u8 beacon_filter_average_energy;
517	void *general;
 
518};
519
520static void iwl_mvm_stat_iterator(void *_data, u8 *mac,
521				  struct ieee80211_vif *vif)
 
 
 
 
 
522{
523	struct iwl_mvm_stat_data *data = _data;
524	struct iwl_mvm *mvm = data->mvm;
525	int sig = -data->beacon_filter_average_energy;
526	int last_event;
527	int thold = vif->bss_conf.cqm_rssi_thold;
528	int hyst = vif->bss_conf.cqm_rssi_hyst;
529	u16 id = le32_to_cpu(data->mac_id);
530	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
531
532	/* This doesn't need the MAC ID check since it's not taking the
533	 * data copied into the "data" struct, but rather the data from
534	 * the notification directly.
535	 */
536	if (data->general) {
537		u16 vif_id = mvmvif->id;
538
539		if (iwl_mvm_is_cdb_supported(mvm)) {
540			struct mvm_statistics_general_cdb *general =
541				data->general;
542
543			mvmvif->beacon_stats.num_beacons =
544				le32_to_cpu(general->beacon_counter[vif_id]);
545			mvmvif->beacon_stats.avg_signal =
546				-general->beacon_average_energy[vif_id];
547		} else {
548			struct mvm_statistics_general_v8 *general =
549				data->general;
550
551			mvmvif->beacon_stats.num_beacons =
552				le32_to_cpu(general->beacon_counter[vif_id]);
553			mvmvif->beacon_stats.avg_signal =
554				-general->beacon_average_energy[vif_id];
555		}
556	}
557
558	if (mvmvif->id != id)
559		return;
560
561	if (vif->type != NL80211_IFTYPE_STATION)
562		return;
563
564	if (sig == 0) {
565		IWL_DEBUG_RX(mvm, "RSSI is 0 - skip signal based decision\n");
566		return;
567	}
568
569	mvmvif->bf_data.ave_beacon_signal = sig;
570
571	/* BT Coex */
572	if (mvmvif->bf_data.bt_coex_min_thold !=
573	    mvmvif->bf_data.bt_coex_max_thold) {
574		last_event = mvmvif->bf_data.last_bt_coex_event;
575		if (sig > mvmvif->bf_data.bt_coex_max_thold &&
576		    (last_event <= mvmvif->bf_data.bt_coex_min_thold ||
577		     last_event == 0)) {
578			mvmvif->bf_data.last_bt_coex_event = sig;
579			IWL_DEBUG_RX(mvm, "cqm_iterator bt coex high %d\n",
580				     sig);
581			iwl_mvm_bt_rssi_event(mvm, vif, RSSI_EVENT_HIGH);
582		} else if (sig < mvmvif->bf_data.bt_coex_min_thold &&
583			   (last_event >= mvmvif->bf_data.bt_coex_max_thold ||
584			    last_event == 0)) {
585			mvmvif->bf_data.last_bt_coex_event = sig;
586			IWL_DEBUG_RX(mvm, "cqm_iterator bt coex low %d\n",
587				     sig);
588			iwl_mvm_bt_rssi_event(mvm, vif, RSSI_EVENT_LOW);
589		}
590	}
591
592	if (!(vif->driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI))
593		return;
594
595	/* CQM Notification */
596	last_event = mvmvif->bf_data.last_cqm_event;
597	if (thold && sig < thold && (last_event == 0 ||
598				     sig < last_event - hyst)) {
599		mvmvif->bf_data.last_cqm_event = sig;
600		IWL_DEBUG_RX(mvm, "cqm_iterator cqm low %d\n",
601			     sig);
602		ieee80211_cqm_rssi_notify(
603			vif,
604			NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
605			sig,
606			GFP_KERNEL);
607	} else if (sig > thold &&
608		   (last_event == 0 || sig > last_event + hyst)) {
609		mvmvif->bf_data.last_cqm_event = sig;
610		IWL_DEBUG_RX(mvm, "cqm_iterator cqm high %d\n",
611			     sig);
612		ieee80211_cqm_rssi_notify(
613			vif,
614			NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
615			sig,
616			GFP_KERNEL);
617	}
618}
619
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
620static inline void
621iwl_mvm_rx_stats_check_trigger(struct iwl_mvm *mvm, struct iwl_rx_packet *pkt)
622{
623	struct iwl_fw_dbg_trigger_tlv *trig;
624	struct iwl_fw_dbg_trigger_stats *trig_stats;
625	u32 trig_offset, trig_thold;
626
627	if (!iwl_fw_dbg_trigger_enabled(mvm->fw, FW_DBG_TRIGGER_STATS))
 
628		return;
629
630	trig = iwl_fw_dbg_get_trigger(mvm->fw, FW_DBG_TRIGGER_STATS);
631	trig_stats = (void *)trig->data;
632
633	if (!iwl_fw_dbg_trigger_check_stop(&mvm->fwrt, NULL, trig))
634		return;
635
636	trig_offset = le32_to_cpu(trig_stats->stop_offset);
637	trig_thold = le32_to_cpu(trig_stats->stop_threshold);
638
639	if (WARN_ON_ONCE(trig_offset >= iwl_rx_packet_payload_len(pkt)))
640		return;
641
642	if (le32_to_cpup((__le32 *) (pkt->data + trig_offset)) < trig_thold)
643		return;
644
645	iwl_fw_dbg_collect_trig(&mvm->fwrt, trig, NULL);
646}
647
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
648void iwl_mvm_handle_rx_statistics(struct iwl_mvm *mvm,
649				  struct iwl_rx_packet *pkt)
650{
651	struct iwl_mvm_stat_data data = {
652		.mvm = mvm,
653	};
 
654	int expected_size;
655	int i;
656	u8 *energy;
657	__le32 *bytes, *air_time;
658	__le32 flags;
 
 
 
 
 
 
 
 
 
 
659
660	if (!iwl_mvm_has_new_rx_stats_api(mvm)) {
661		if (iwl_mvm_has_new_rx_api(mvm))
662			expected_size = sizeof(struct iwl_notif_statistics_v11);
663		else
664			expected_size = sizeof(struct iwl_notif_statistics_v10);
665	} else {
666		expected_size = sizeof(struct iwl_notif_statistics_cdb);
667	}
668
669	if (WARN_ONCE(iwl_rx_packet_payload_len(pkt) != expected_size,
670		      "received invalid statistics size (%d)!\n",
671		      iwl_rx_packet_payload_len(pkt)))
672		return;
673
674	if (!iwl_mvm_has_new_rx_stats_api(mvm)) {
675		struct iwl_notif_statistics_v11 *stats = (void *)&pkt->data;
676
677		data.mac_id = stats->rx.general.mac_id;
678		data.beacon_filter_average_energy =
679			stats->general.common.beacon_filter_average_energy;
680
681		mvm->rx_stats_v3 = stats->rx;
682
683		mvm->radio_stats.rx_time =
684			le64_to_cpu(stats->general.common.rx_time);
685		mvm->radio_stats.tx_time =
686			le64_to_cpu(stats->general.common.tx_time);
687		mvm->radio_stats.on_time_rf =
688			le64_to_cpu(stats->general.common.on_time_rf);
689		mvm->radio_stats.on_time_scan =
690			le64_to_cpu(stats->general.common.on_time_scan);
691
692		data.general = &stats->general;
693
 
694		flags = stats->flag;
695	} else {
696		struct iwl_notif_statistics_cdb *stats = (void *)&pkt->data;
697
698		data.mac_id = stats->rx.general.mac_id;
699		data.beacon_filter_average_energy =
700			stats->general.common.beacon_filter_average_energy;
701
702		mvm->rx_stats = stats->rx;
703
704		mvm->radio_stats.rx_time =
705			le64_to_cpu(stats->general.common.rx_time);
706		mvm->radio_stats.tx_time =
707			le64_to_cpu(stats->general.common.tx_time);
708		mvm->radio_stats.on_time_rf =
709			le64_to_cpu(stats->general.common.on_time_rf);
710		mvm->radio_stats.on_time_scan =
711			le64_to_cpu(stats->general.common.on_time_scan);
712
713		data.general = &stats->general;
714
 
715		flags = stats->flag;
716	}
 
717
718	iwl_mvm_rx_stats_check_trigger(mvm, pkt);
719
720	ieee80211_iterate_active_interfaces(mvm->hw,
721					    IEEE80211_IFACE_ITER_NORMAL,
722					    iwl_mvm_stat_iterator,
723					    &data);
724
725	if (!iwl_mvm_has_new_rx_api(mvm))
726		return;
727
728	if (!iwl_mvm_has_new_rx_stats_api(mvm)) {
729		struct iwl_notif_statistics_v11 *v11 = (void *)&pkt->data;
730
731		energy = (void *)&v11->load_stats.avg_energy;
732		bytes = (void *)&v11->load_stats.byte_count;
733		air_time = (void *)&v11->load_stats.air_time;
734	} else {
735		struct iwl_notif_statistics_cdb *stats = (void *)&pkt->data;
736
737		energy = (void *)&stats->load_stats.avg_energy;
738		bytes = (void *)&stats->load_stats.byte_count;
739		air_time = (void *)&stats->load_stats.air_time;
740	}
 
 
741
742	rcu_read_lock();
743	for (i = 0; i < ARRAY_SIZE(mvm->fw_id_to_mac_id); i++) {
744		struct iwl_mvm_sta *sta;
745
746		if (!energy[i])
747			continue;
 
748
749		sta = iwl_mvm_sta_from_staid_rcu(mvm, i);
750		if (!sta)
751			continue;
752		sta->avg_energy = energy[i];
753	}
754	rcu_read_unlock();
755}
756
757void iwl_mvm_rx_statistics(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
758{
759	iwl_mvm_handle_rx_statistics(mvm, rxb_addr(rxb));
760}
761
762void iwl_mvm_window_status_notif(struct iwl_mvm *mvm,
763				 struct iwl_rx_cmd_buffer *rxb)
764{
765	struct iwl_rx_packet *pkt = rxb_addr(rxb);
766	struct iwl_ba_window_status_notif *notif = (void *)pkt->data;
767	int i;
768	u32 pkt_len = iwl_rx_packet_payload_len(pkt);
769
770	if (WARN_ONCE(pkt_len != sizeof(*notif),
771		      "Received window status notification of wrong size (%u)\n",
772		      pkt_len))
773		return;
774
775	rcu_read_lock();
776	for (i = 0; i < BA_WINDOW_STREAMS_MAX; i++) {
777		struct ieee80211_sta *sta;
778		u8 sta_id, tid;
779		u64 bitmap;
780		u32 ssn;
781		u16 ratid;
782		u16 received_mpdu;
783
784		ratid = le16_to_cpu(notif->ra_tid[i]);
785		/* check that this TID is valid */
786		if (!(ratid & BA_WINDOW_STATUS_VALID_MSK))
787			continue;
788
789		received_mpdu = le16_to_cpu(notif->mpdu_rx_count[i]);
790		if (received_mpdu == 0)
791			continue;
792
793		tid = ratid & BA_WINDOW_STATUS_TID_MSK;
794		/* get the station */
795		sta_id = (ratid & BA_WINDOW_STATUS_STA_ID_MSK)
796			 >> BA_WINDOW_STATUS_STA_ID_POS;
797		sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
798		if (IS_ERR_OR_NULL(sta))
799			continue;
800		bitmap = le64_to_cpu(notif->bitmap[i]);
801		ssn = le32_to_cpu(notif->start_seq_num[i]);
802
803		/* update mac80211 with the bitmap for the reordering buffer */
804		ieee80211_mark_rx_ba_filtered_frames(sta, tid, ssn, bitmap,
805						     received_mpdu);
806	}
807	rcu_read_unlock();
808}