1 /******************************************************************************
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.
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
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.
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.
21 * The full GNU General Public License is included in this distribution
22 * in the file called COPYING.
24 * Contact Information:
25 * Intel Linux Wireless <linuxwifi@intel.com>
26 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
30 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
31 * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
32 * All rights reserved.
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
38 * * Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * * Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in
42 * the documentation and/or other materials provided with the
44 * * Neither the name Intel Corporation nor the names of its
45 * contributors may be used to endorse or promote products derived
46 * from this software without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
49 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
50 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
51 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
52 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
53 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
54 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
55 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
56 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
57 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
58 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
59 *****************************************************************************/
60 #include <linux/etherdevice.h>
61 #include <linux/skbuff.h>
62 #include "iwl-trans.h"
67 * iwl_mvm_rx_rx_phy_cmd - REPLY_RX_PHY_CMD handler
69 * Copies the phy information in mvm->last_phy_info, it will be used when the
70 * actual data will come from the fw in the next packet.
72 void iwl_mvm_rx_rx_phy_cmd(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
74 struct iwl_rx_packet *pkt = rxb_addr(rxb);
76 memcpy(&mvm->last_phy_info, pkt->data, sizeof(mvm->last_phy_info));
79 #ifdef CONFIG_IWLWIFI_DEBUGFS
80 if (mvm->last_phy_info.phy_flags & cpu_to_le16(RX_RES_PHY_FLAGS_AGG)) {
81 spin_lock(&mvm->drv_stats_lock);
82 mvm->drv_rx_stats.ampdu_count++;
83 spin_unlock(&mvm->drv_stats_lock);
89 * iwl_mvm_pass_packet_to_mac80211 - builds the packet for mac80211
91 * Adds the rxb to a new skb and give it to mac80211
93 static void iwl_mvm_pass_packet_to_mac80211(struct iwl_mvm *mvm,
94 struct ieee80211_sta *sta,
95 struct napi_struct *napi,
97 struct ieee80211_hdr *hdr, u16 len,
99 struct iwl_rx_cmd_buffer *rxb)
101 unsigned int hdrlen = ieee80211_hdrlen(hdr->frame_control);
102 unsigned int fraglen;
105 * The 'hdrlen' (plus the 8 bytes for the SNAP and the crypt_len,
106 * but those are all multiples of 4 long) all goes away, but we
107 * want the *end* of it, which is going to be the start of the IP
108 * header, to be aligned when it gets pulled in.
109 * The beginning of the skb->data is aligned on at least a 4-byte
110 * boundary after allocation. Everything here is aligned at least
111 * on a 2-byte boundary so we can just take hdrlen & 3 and pad by
114 skb_reserve(skb, hdrlen & 3);
116 /* If frame is small enough to fit in skb->head, pull it completely.
117 * If not, only pull ieee80211_hdr (including crypto if present, and
118 * an additional 8 bytes for SNAP/ethertype, see below) so that
119 * splice() or TCP coalesce are more efficient.
121 * Since, in addition, ieee80211_data_to_8023() always pull in at
122 * least 8 bytes (possibly more for mesh) we can do the same here
123 * to save the cost of doing it later. That still doesn't pull in
124 * the actual IP header since the typical case has a SNAP header.
125 * If the latter changes (there are efforts in the standards group
126 * to do so) we should revisit this and ieee80211_data_to_8023().
128 hdrlen = (len <= skb_tailroom(skb)) ? len : hdrlen + crypt_len + 8;
130 skb_put_data(skb, hdr, hdrlen);
131 fraglen = len - hdrlen;
134 int offset = (void *)hdr + hdrlen -
135 rxb_addr(rxb) + rxb_offset(rxb);
137 skb_add_rx_frag(skb, 0, rxb_steal_page(rxb), offset,
138 fraglen, rxb->truesize);
141 ieee80211_rx_napi(mvm->hw, sta, skb, napi);
145 * iwl_mvm_get_signal_strength - use new rx PHY INFO API
146 * values are reported by the fw as positive values - need to negate
147 * to obtain their dBM. Account for missing antennas by replacing 0
148 * values by -256dBm: practically 0 power and a non-feasible 8 bit value.
150 static void iwl_mvm_get_signal_strength(struct iwl_mvm *mvm,
151 struct iwl_rx_phy_info *phy_info,
152 struct ieee80211_rx_status *rx_status)
154 int energy_a, energy_b, energy_c, max_energy;
158 le32_to_cpu(phy_info->non_cfg_phy[IWL_RX_INFO_ENERGY_ANT_ABC_IDX]);
159 energy_a = (val & IWL_RX_INFO_ENERGY_ANT_A_MSK) >>
160 IWL_RX_INFO_ENERGY_ANT_A_POS;
161 energy_a = energy_a ? -energy_a : S8_MIN;
162 energy_b = (val & IWL_RX_INFO_ENERGY_ANT_B_MSK) >>
163 IWL_RX_INFO_ENERGY_ANT_B_POS;
164 energy_b = energy_b ? -energy_b : S8_MIN;
165 energy_c = (val & IWL_RX_INFO_ENERGY_ANT_C_MSK) >>
166 IWL_RX_INFO_ENERGY_ANT_C_POS;
167 energy_c = energy_c ? -energy_c : S8_MIN;
168 max_energy = max(energy_a, energy_b);
169 max_energy = max(max_energy, energy_c);
171 IWL_DEBUG_STATS(mvm, "energy In A %d B %d C %d , and max %d\n",
172 energy_a, energy_b, energy_c, max_energy);
174 rx_status->signal = max_energy;
175 rx_status->chains = (le16_to_cpu(phy_info->phy_flags) &
176 RX_RES_PHY_FLAGS_ANTENNA)
177 >> RX_RES_PHY_FLAGS_ANTENNA_POS;
178 rx_status->chain_signal[0] = energy_a;
179 rx_status->chain_signal[1] = energy_b;
180 rx_status->chain_signal[2] = energy_c;
184 * iwl_mvm_set_mac80211_rx_flag - translate fw status to mac80211 format
185 * @mvm: the mvm object
187 * @stats: status in mac80211's format
188 * @rx_pkt_status: status coming from fw
190 * returns non 0 value if the packet should be dropped
192 static u32 iwl_mvm_set_mac80211_rx_flag(struct iwl_mvm *mvm,
193 struct ieee80211_hdr *hdr,
194 struct ieee80211_rx_status *stats,
198 if (!ieee80211_has_protected(hdr->frame_control) ||
199 (rx_pkt_status & RX_MPDU_RES_STATUS_SEC_ENC_MSK) ==
200 RX_MPDU_RES_STATUS_SEC_NO_ENC)
203 /* packet was encrypted with unknown alg */
204 if ((rx_pkt_status & RX_MPDU_RES_STATUS_SEC_ENC_MSK) ==
205 RX_MPDU_RES_STATUS_SEC_ENC_ERR)
208 switch (rx_pkt_status & RX_MPDU_RES_STATUS_SEC_ENC_MSK) {
209 case RX_MPDU_RES_STATUS_SEC_CCM_ENC:
210 /* alg is CCM: check MIC only */
211 if (!(rx_pkt_status & RX_MPDU_RES_STATUS_MIC_OK))
214 stats->flag |= RX_FLAG_DECRYPTED;
215 *crypt_len = IEEE80211_CCMP_HDR_LEN;
218 case RX_MPDU_RES_STATUS_SEC_TKIP_ENC:
219 /* Don't drop the frame and decrypt it in SW */
220 if (!fw_has_api(&mvm->fw->ucode_capa,
221 IWL_UCODE_TLV_API_DEPRECATE_TTAK) &&
222 !(rx_pkt_status & RX_MPDU_RES_STATUS_TTAK_OK))
224 *crypt_len = IEEE80211_TKIP_IV_LEN;
225 /* fall through if TTAK OK */
227 case RX_MPDU_RES_STATUS_SEC_WEP_ENC:
228 if (!(rx_pkt_status & RX_MPDU_RES_STATUS_ICV_OK))
231 stats->flag |= RX_FLAG_DECRYPTED;
232 if ((rx_pkt_status & RX_MPDU_RES_STATUS_SEC_ENC_MSK) ==
233 RX_MPDU_RES_STATUS_SEC_WEP_ENC)
234 *crypt_len = IEEE80211_WEP_IV_LEN;
237 case RX_MPDU_RES_STATUS_SEC_EXT_ENC:
238 if (!(rx_pkt_status & RX_MPDU_RES_STATUS_MIC_OK))
240 stats->flag |= RX_FLAG_DECRYPTED;
244 /* Expected in monitor (not having the keys) */
245 if (!mvm->monitor_on)
246 IWL_ERR(mvm, "Unhandled alg: 0x%x\n", rx_pkt_status);
252 static void iwl_mvm_rx_handle_tcm(struct iwl_mvm *mvm,
253 struct ieee80211_sta *sta,
254 struct ieee80211_hdr *hdr, u32 len,
255 struct iwl_rx_phy_info *phy_info,
258 struct iwl_mvm_sta *mvmsta;
259 struct iwl_mvm_tcm_mac *mdata;
261 int ac = IEEE80211_AC_BE; /* treat non-QoS as BE */
262 struct iwl_mvm_vif *mvmvif;
263 /* expected throughput in 100Kbps, single stream, 20 MHz */
264 static const u8 thresh_tpt[] = {
265 9, 18, 30, 42, 60, 78, 90, 96, 120, 135,
269 if (ieee80211_is_data_qos(hdr->frame_control))
270 ac = tid_to_mac80211_ac[ieee80211_get_tid(hdr)];
272 mvmsta = iwl_mvm_sta_from_mac80211(sta);
273 mac = mvmsta->mac_id_n_color & FW_CTXT_ID_MSK;
275 if (time_after(jiffies, mvm->tcm.ts + MVM_TCM_PERIOD))
276 schedule_delayed_work(&mvm->tcm.work, 0);
277 mdata = &mvm->tcm.data[mac];
278 mdata->rx.pkts[ac]++;
280 /* count the airtime only once for each ampdu */
281 if (mdata->rx.last_ampdu_ref != mvm->ampdu_ref) {
282 mdata->rx.last_ampdu_ref = mvm->ampdu_ref;
283 mdata->rx.airtime += le16_to_cpu(phy_info->frame_time);
286 if (!(rate_n_flags & (RATE_MCS_HT_MSK | RATE_MCS_VHT_MSK)))
289 mvmvif = iwl_mvm_vif_from_mac80211(mvmsta->vif);
291 if (mdata->opened_rx_ba_sessions ||
292 mdata->uapsd_nonagg_detect.detected ||
293 (!mvmvif->queue_params[IEEE80211_AC_VO].uapsd &&
294 !mvmvif->queue_params[IEEE80211_AC_VI].uapsd &&
295 !mvmvif->queue_params[IEEE80211_AC_BE].uapsd &&
296 !mvmvif->queue_params[IEEE80211_AC_BK].uapsd) ||
297 mvmsta->sta_id != mvmvif->ap_sta_id)
300 if (rate_n_flags & RATE_MCS_HT_MSK) {
301 thr = thresh_tpt[rate_n_flags & RATE_HT_MCS_RATE_CODE_MSK];
302 thr *= 1 + ((rate_n_flags & RATE_HT_MCS_NSS_MSK) >>
303 RATE_HT_MCS_NSS_POS);
305 if (WARN_ON((rate_n_flags & RATE_VHT_MCS_RATE_CODE_MSK) >=
306 ARRAY_SIZE(thresh_tpt)))
308 thr = thresh_tpt[rate_n_flags & RATE_VHT_MCS_RATE_CODE_MSK];
309 thr *= 1 + ((rate_n_flags & RATE_VHT_MCS_NSS_MSK) >>
310 RATE_VHT_MCS_NSS_POS);
313 thr <<= ((rate_n_flags & RATE_MCS_CHAN_WIDTH_MSK) >>
314 RATE_MCS_CHAN_WIDTH_POS);
316 mdata->uapsd_nonagg_detect.rx_bytes += len;
317 ewma_rate_add(&mdata->uapsd_nonagg_detect.rate, thr);
320 static void iwl_mvm_rx_csum(struct ieee80211_sta *sta,
324 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
325 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(mvmsta->vif);
327 if (mvmvif->features & NETIF_F_RXCSUM &&
328 status & RX_MPDU_RES_STATUS_CSUM_DONE &&
329 status & RX_MPDU_RES_STATUS_CSUM_OK)
330 skb->ip_summed = CHECKSUM_UNNECESSARY;
334 * iwl_mvm_rx_rx_mpdu - REPLY_RX_MPDU_CMD handler
336 * Handles the actual data of the Rx packet from the fw
338 void iwl_mvm_rx_rx_mpdu(struct iwl_mvm *mvm, struct napi_struct *napi,
339 struct iwl_rx_cmd_buffer *rxb)
341 struct ieee80211_hdr *hdr;
342 struct ieee80211_rx_status *rx_status;
343 struct iwl_rx_packet *pkt = rxb_addr(rxb);
344 struct iwl_rx_phy_info *phy_info;
345 struct iwl_rx_mpdu_res_start *rx_res;
346 struct ieee80211_sta *sta = NULL;
354 phy_info = &mvm->last_phy_info;
355 rx_res = (struct iwl_rx_mpdu_res_start *)pkt->data;
356 hdr = (struct ieee80211_hdr *)(pkt->data + sizeof(*rx_res));
357 len = le16_to_cpu(rx_res->byte_count);
358 rx_pkt_status = le32_to_cpup((__le32 *)
359 (pkt->data + sizeof(*rx_res) + len));
361 /* Dont use dev_alloc_skb(), we'll have enough headroom once
362 * ieee80211_hdr pulled.
364 skb = alloc_skb(128, GFP_ATOMIC);
366 IWL_ERR(mvm, "alloc_skb failed\n");
370 rx_status = IEEE80211_SKB_RXCB(skb);
373 * drop the packet if it has failed being decrypted by HW
375 if (iwl_mvm_set_mac80211_rx_flag(mvm, hdr, rx_status, rx_pkt_status,
377 IWL_DEBUG_DROP(mvm, "Bad decryption results 0x%08x\n",
384 * Keep packets with CRC errors (and with overrun) for monitor mode
385 * (otherwise the firmware discards them) but mark them as bad.
387 if (!(rx_pkt_status & RX_MPDU_RES_STATUS_CRC_OK) ||
388 !(rx_pkt_status & RX_MPDU_RES_STATUS_OVERRUN_OK)) {
389 IWL_DEBUG_RX(mvm, "Bad CRC or FIFO: 0x%08X.\n", rx_pkt_status);
390 rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
393 /* This will be used in several places later */
394 rate_n_flags = le32_to_cpu(phy_info->rate_n_flags);
396 /* rx_status carries information about the packet to mac80211 */
397 rx_status->mactime = le64_to_cpu(phy_info->timestamp);
398 rx_status->device_timestamp = le32_to_cpu(phy_info->system_timestamp);
400 (phy_info->phy_flags & cpu_to_le16(RX_RES_PHY_FLAGS_BAND_24)) ?
401 NL80211_BAND_2GHZ : NL80211_BAND_5GHZ;
403 ieee80211_channel_to_frequency(le16_to_cpu(phy_info->channel),
406 /* TSF as indicated by the firmware is at INA time */
407 rx_status->flag |= RX_FLAG_MACTIME_PLCP_START;
409 iwl_mvm_get_signal_strength(mvm, phy_info, rx_status);
411 IWL_DEBUG_STATS_LIMIT(mvm, "Rssi %d, TSF %llu\n", rx_status->signal,
412 (unsigned long long)rx_status->mactime);
415 if (rx_pkt_status & RX_MPDU_RES_STATUS_SRC_STA_FOUND) {
416 u32 id = rx_pkt_status & RX_MPDU_RES_STATUS_STA_ID_MSK;
418 id >>= RX_MDPU_RES_STATUS_STA_ID_SHIFT;
420 if (!WARN_ON_ONCE(id >= ARRAY_SIZE(mvm->fw_id_to_mac_id))) {
421 sta = rcu_dereference(mvm->fw_id_to_mac_id[id]);
425 } else if (!is_multicast_ether_addr(hdr->addr2)) {
426 /* This is fine since we prevent two stations with the same
427 * address from being added.
429 sta = ieee80211_find_sta_by_ifaddr(mvm->hw, hdr->addr2, NULL);
433 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
434 struct ieee80211_vif *tx_blocked_vif =
435 rcu_dereference(mvm->csa_tx_blocked_vif);
437 /* We have tx blocked stations (with CS bit). If we heard
438 * frames from a blocked station on a new channel we can
441 if (unlikely(tx_blocked_vif) &&
442 mvmsta->vif == tx_blocked_vif) {
443 struct iwl_mvm_vif *mvmvif =
444 iwl_mvm_vif_from_mac80211(tx_blocked_vif);
446 if (mvmvif->csa_target_freq == rx_status->freq)
447 iwl_mvm_sta_modify_disable_tx_ap(mvm, sta,
451 rs_update_last_rssi(mvm, mvmsta, rx_status);
453 if (iwl_fw_dbg_trigger_enabled(mvm->fw, FW_DBG_TRIGGER_RSSI) &&
454 ieee80211_is_beacon(hdr->frame_control)) {
455 struct iwl_fw_dbg_trigger_tlv *trig;
456 struct iwl_fw_dbg_trigger_low_rssi *rssi_trig;
460 trig = iwl_fw_dbg_get_trigger(mvm->fw,
461 FW_DBG_TRIGGER_RSSI);
462 rssi_trig = (void *)trig->data;
463 rssi = le32_to_cpu(rssi_trig->rssi);
466 iwl_fw_dbg_trigger_check_stop(&mvm->fwrt,
467 ieee80211_vif_to_wdev(mvmsta->vif),
469 if (trig_check && rx_status->signal < rssi)
470 iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
474 if (!mvm->tcm.paused && len >= sizeof(*hdr) &&
475 !is_multicast_ether_addr(hdr->addr1) &&
476 ieee80211_is_data(hdr->frame_control))
477 iwl_mvm_rx_handle_tcm(mvm, sta, hdr, len, phy_info,
480 if (ieee80211_is_data(hdr->frame_control))
481 iwl_mvm_rx_csum(sta, skb, rx_pkt_status);
485 /* set the preamble flag if appropriate */
486 if (phy_info->phy_flags & cpu_to_le16(RX_RES_PHY_FLAGS_SHORT_PREAMBLE))
487 rx_status->enc_flags |= RX_ENC_FLAG_SHORTPRE;
489 if (phy_info->phy_flags & cpu_to_le16(RX_RES_PHY_FLAGS_AGG)) {
491 * We know which subframes of an A-MPDU belong
492 * together since we get a single PHY response
493 * from the firmware for all of them
495 rx_status->flag |= RX_FLAG_AMPDU_DETAILS;
496 rx_status->ampdu_reference = mvm->ampdu_ref;
499 /* Set up the HT phy flags */
500 switch (rate_n_flags & RATE_MCS_CHAN_WIDTH_MSK) {
501 case RATE_MCS_CHAN_WIDTH_20:
503 case RATE_MCS_CHAN_WIDTH_40:
504 rx_status->bw = RATE_INFO_BW_40;
506 case RATE_MCS_CHAN_WIDTH_80:
507 rx_status->bw = RATE_INFO_BW_80;
509 case RATE_MCS_CHAN_WIDTH_160:
510 rx_status->bw = RATE_INFO_BW_160;
513 if (!(rate_n_flags & RATE_MCS_CCK_MSK) &&
514 rate_n_flags & RATE_MCS_SGI_MSK)
515 rx_status->enc_flags |= RX_ENC_FLAG_SHORT_GI;
516 if (rate_n_flags & RATE_HT_MCS_GF_MSK)
517 rx_status->enc_flags |= RX_ENC_FLAG_HT_GF;
518 if (rate_n_flags & RATE_MCS_LDPC_MSK)
519 rx_status->enc_flags |= RX_ENC_FLAG_LDPC;
520 if (rate_n_flags & RATE_MCS_HT_MSK) {
521 u8 stbc = (rate_n_flags & RATE_MCS_STBC_MSK) >>
523 rx_status->encoding = RX_ENC_HT;
524 rx_status->rate_idx = rate_n_flags & RATE_HT_MCS_INDEX_MSK;
525 rx_status->enc_flags |= stbc << RX_ENC_FLAG_STBC_SHIFT;
526 } else if (rate_n_flags & RATE_MCS_VHT_MSK) {
527 u8 stbc = (rate_n_flags & RATE_MCS_STBC_MSK) >>
530 ((rate_n_flags & RATE_VHT_MCS_NSS_MSK) >>
531 RATE_VHT_MCS_NSS_POS) + 1;
532 rx_status->rate_idx = rate_n_flags & RATE_VHT_MCS_RATE_CODE_MSK;
533 rx_status->encoding = RX_ENC_VHT;
534 rx_status->enc_flags |= stbc << RX_ENC_FLAG_STBC_SHIFT;
535 if (rate_n_flags & RATE_MCS_BF_MSK)
536 rx_status->enc_flags |= RX_ENC_FLAG_BF;
538 int rate = iwl_mvm_legacy_rate_to_mac80211_idx(rate_n_flags,
541 if (WARN(rate < 0 || rate > 0xFF,
542 "Invalid rate flags 0x%x, band %d,\n",
543 rate_n_flags, rx_status->band)) {
547 rx_status->rate_idx = rate;
550 #ifdef CONFIG_IWLWIFI_DEBUGFS
551 iwl_mvm_update_frame_stats(mvm, rate_n_flags,
552 rx_status->flag & RX_FLAG_AMPDU_DETAILS);
555 if (unlikely((ieee80211_is_beacon(hdr->frame_control) ||
556 ieee80211_is_probe_resp(hdr->frame_control)) &&
557 mvm->sched_scan_pass_all == SCHED_SCAN_PASS_ALL_ENABLED))
558 mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_FOUND;
560 if (unlikely(ieee80211_is_beacon(hdr->frame_control) ||
561 ieee80211_is_probe_resp(hdr->frame_control)))
562 rx_status->boottime_ns = ktime_get_boot_ns();
564 /* Take a reference briefly to kick off a d0i3 entry delay so
565 * we can handle bursts of RX packets without toggling the
566 * state too often. But don't do this for beacons if we are
567 * going to idle because the beacon filtering changes we make
568 * cause the firmware to send us collateral beacons. */
569 take_ref = !(test_bit(STATUS_TRANS_GOING_IDLE, &mvm->trans->status) &&
570 ieee80211_is_beacon(hdr->frame_control));
573 iwl_mvm_ref(mvm, IWL_MVM_REF_RX);
575 iwl_mvm_pass_packet_to_mac80211(mvm, sta, napi, skb, hdr, len,
579 iwl_mvm_unref(mvm, IWL_MVM_REF_RX);
582 struct iwl_mvm_stat_data {
585 u8 beacon_filter_average_energy;
589 static void iwl_mvm_stat_iterator(void *_data, u8 *mac,
590 struct ieee80211_vif *vif)
592 struct iwl_mvm_stat_data *data = _data;
593 struct iwl_mvm *mvm = data->mvm;
594 int sig = -data->beacon_filter_average_energy;
596 int thold = vif->bss_conf.cqm_rssi_thold;
597 int hyst = vif->bss_conf.cqm_rssi_hyst;
598 u16 id = le32_to_cpu(data->mac_id);
599 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
601 /* This doesn't need the MAC ID check since it's not taking the
602 * data copied into the "data" struct, but rather the data from
603 * the notification directly.
606 u16 vif_id = mvmvif->id;
608 if (iwl_mvm_is_cdb_supported(mvm)) {
609 struct mvm_statistics_general_cdb *general =
612 mvmvif->beacon_stats.num_beacons =
613 le32_to_cpu(general->beacon_counter[vif_id]);
614 mvmvif->beacon_stats.avg_signal =
615 -general->beacon_average_energy[vif_id];
617 struct mvm_statistics_general_v8 *general =
620 mvmvif->beacon_stats.num_beacons =
621 le32_to_cpu(general->beacon_counter[vif_id]);
622 mvmvif->beacon_stats.avg_signal =
623 -general->beacon_average_energy[vif_id];
627 if (mvmvif->id != id)
630 if (vif->type != NL80211_IFTYPE_STATION)
634 IWL_DEBUG_RX(mvm, "RSSI is 0 - skip signal based decision\n");
638 mvmvif->bf_data.ave_beacon_signal = sig;
641 if (mvmvif->bf_data.bt_coex_min_thold !=
642 mvmvif->bf_data.bt_coex_max_thold) {
643 last_event = mvmvif->bf_data.last_bt_coex_event;
644 if (sig > mvmvif->bf_data.bt_coex_max_thold &&
645 (last_event <= mvmvif->bf_data.bt_coex_min_thold ||
647 mvmvif->bf_data.last_bt_coex_event = sig;
648 IWL_DEBUG_RX(mvm, "cqm_iterator bt coex high %d\n",
650 iwl_mvm_bt_rssi_event(mvm, vif, RSSI_EVENT_HIGH);
651 } else if (sig < mvmvif->bf_data.bt_coex_min_thold &&
652 (last_event >= mvmvif->bf_data.bt_coex_max_thold ||
654 mvmvif->bf_data.last_bt_coex_event = sig;
655 IWL_DEBUG_RX(mvm, "cqm_iterator bt coex low %d\n",
657 iwl_mvm_bt_rssi_event(mvm, vif, RSSI_EVENT_LOW);
661 if (!(vif->driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI))
664 /* CQM Notification */
665 last_event = mvmvif->bf_data.last_cqm_event;
666 if (thold && sig < thold && (last_event == 0 ||
667 sig < last_event - hyst)) {
668 mvmvif->bf_data.last_cqm_event = sig;
669 IWL_DEBUG_RX(mvm, "cqm_iterator cqm low %d\n",
671 ieee80211_cqm_rssi_notify(
673 NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
676 } else if (sig > thold &&
677 (last_event == 0 || sig > last_event + hyst)) {
678 mvmvif->bf_data.last_cqm_event = sig;
679 IWL_DEBUG_RX(mvm, "cqm_iterator cqm high %d\n",
681 ieee80211_cqm_rssi_notify(
683 NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
690 iwl_mvm_rx_stats_check_trigger(struct iwl_mvm *mvm, struct iwl_rx_packet *pkt)
692 struct iwl_fw_dbg_trigger_tlv *trig;
693 struct iwl_fw_dbg_trigger_stats *trig_stats;
694 u32 trig_offset, trig_thold;
696 if (!iwl_fw_dbg_trigger_enabled(mvm->fw, FW_DBG_TRIGGER_STATS))
699 trig = iwl_fw_dbg_get_trigger(mvm->fw, FW_DBG_TRIGGER_STATS);
700 trig_stats = (void *)trig->data;
702 if (!iwl_fw_dbg_trigger_check_stop(&mvm->fwrt, NULL, trig))
705 trig_offset = le32_to_cpu(trig_stats->stop_offset);
706 trig_thold = le32_to_cpu(trig_stats->stop_threshold);
708 if (WARN_ON_ONCE(trig_offset >= iwl_rx_packet_payload_len(pkt)))
711 if (le32_to_cpup((__le32 *) (pkt->data + trig_offset)) < trig_thold)
714 iwl_fw_dbg_collect_trig(&mvm->fwrt, trig, NULL);
717 void iwl_mvm_handle_rx_statistics(struct iwl_mvm *mvm,
718 struct iwl_rx_packet *pkt)
720 struct iwl_mvm_stat_data data = {
730 if (!iwl_mvm_has_new_rx_stats_api(mvm)) {
731 if (iwl_mvm_has_new_rx_api(mvm))
732 expected_size = sizeof(struct iwl_notif_statistics_v11);
734 expected_size = sizeof(struct iwl_notif_statistics_v10);
736 expected_size = sizeof(struct iwl_notif_statistics_cdb);
739 if (WARN_ONCE(iwl_rx_packet_payload_len(pkt) != expected_size,
740 "received invalid statistics size (%d)!\n",
741 iwl_rx_packet_payload_len(pkt)))
744 if (!iwl_mvm_has_new_rx_stats_api(mvm)) {
745 struct iwl_notif_statistics_v11 *stats = (void *)&pkt->data;
747 data.mac_id = stats->rx.general.mac_id;
748 data.beacon_filter_average_energy =
749 stats->general.common.beacon_filter_average_energy;
751 mvm->rx_stats_v3 = stats->rx;
753 mvm->radio_stats.rx_time =
754 le64_to_cpu(stats->general.common.rx_time);
755 mvm->radio_stats.tx_time =
756 le64_to_cpu(stats->general.common.tx_time);
757 mvm->radio_stats.on_time_rf =
758 le64_to_cpu(stats->general.common.on_time_rf);
759 mvm->radio_stats.on_time_scan =
760 le64_to_cpu(stats->general.common.on_time_scan);
762 data.general = &stats->general;
766 struct iwl_notif_statistics_cdb *stats = (void *)&pkt->data;
768 data.mac_id = stats->rx.general.mac_id;
769 data.beacon_filter_average_energy =
770 stats->general.common.beacon_filter_average_energy;
772 mvm->rx_stats = stats->rx;
774 mvm->radio_stats.rx_time =
775 le64_to_cpu(stats->general.common.rx_time);
776 mvm->radio_stats.tx_time =
777 le64_to_cpu(stats->general.common.tx_time);
778 mvm->radio_stats.on_time_rf =
779 le64_to_cpu(stats->general.common.on_time_rf);
780 mvm->radio_stats.on_time_scan =
781 le64_to_cpu(stats->general.common.on_time_scan);
783 data.general = &stats->general;
788 iwl_mvm_rx_stats_check_trigger(mvm, pkt);
790 ieee80211_iterate_active_interfaces(mvm->hw,
791 IEEE80211_IFACE_ITER_NORMAL,
792 iwl_mvm_stat_iterator,
795 if (!iwl_mvm_has_new_rx_api(mvm))
798 if (!iwl_mvm_has_new_rx_stats_api(mvm)) {
799 struct iwl_notif_statistics_v11 *v11 = (void *)&pkt->data;
801 energy = (void *)&v11->load_stats.avg_energy;
802 bytes = (void *)&v11->load_stats.byte_count;
803 air_time = (void *)&v11->load_stats.air_time;
805 struct iwl_notif_statistics_cdb *stats = (void *)&pkt->data;
807 energy = (void *)&stats->load_stats.avg_energy;
808 bytes = (void *)&stats->load_stats.byte_count;
809 air_time = (void *)&stats->load_stats.air_time;
813 for (i = 0; i < ARRAY_SIZE(mvm->fw_id_to_mac_id); i++) {
814 struct iwl_mvm_sta *sta;
819 sta = iwl_mvm_sta_from_staid_rcu(mvm, i);
822 sta->avg_energy = energy[i];
827 * Don't update in case the statistics are not cleared, since
828 * we will end up counting twice the same airtime, once in TCM
829 * request and once in statistics notification.
831 if (!(le32_to_cpu(flags) & IWL_STATISTICS_REPLY_FLG_CLEAR))
834 spin_lock(&mvm->tcm.lock);
835 for (i = 0; i < NUM_MAC_INDEX_DRIVER; i++) {
836 struct iwl_mvm_tcm_mac *mdata = &mvm->tcm.data[i];
837 u32 airtime = le32_to_cpu(air_time[i]);
838 u32 rx_bytes = le32_to_cpu(bytes[i]);
840 mdata->uapsd_nonagg_detect.rx_bytes += rx_bytes;
842 /* re-init every time to store rate from FW */
843 ewma_rate_init(&mdata->uapsd_nonagg_detect.rate);
844 ewma_rate_add(&mdata->uapsd_nonagg_detect.rate,
845 rx_bytes * 8 / airtime);
848 mdata->rx.airtime += airtime;
850 spin_unlock(&mvm->tcm.lock);
853 void iwl_mvm_rx_statistics(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
855 iwl_mvm_handle_rx_statistics(mvm, rxb_addr(rxb));
858 void iwl_mvm_window_status_notif(struct iwl_mvm *mvm,
859 struct iwl_rx_cmd_buffer *rxb)
861 struct iwl_rx_packet *pkt = rxb_addr(rxb);
862 struct iwl_ba_window_status_notif *notif = (void *)pkt->data;
864 u32 pkt_len = iwl_rx_packet_payload_len(pkt);
866 if (WARN_ONCE(pkt_len != sizeof(*notif),
867 "Received window status notification of wrong size (%u)\n",
872 for (i = 0; i < BA_WINDOW_STREAMS_MAX; i++) {
873 struct ieee80211_sta *sta;
880 ratid = le16_to_cpu(notif->ra_tid[i]);
881 /* check that this TID is valid */
882 if (!(ratid & BA_WINDOW_STATUS_VALID_MSK))
885 received_mpdu = le16_to_cpu(notif->mpdu_rx_count[i]);
886 if (received_mpdu == 0)
889 tid = ratid & BA_WINDOW_STATUS_TID_MSK;
890 /* get the station */
891 sta_id = (ratid & BA_WINDOW_STATUS_STA_ID_MSK)
892 >> BA_WINDOW_STATUS_STA_ID_POS;
893 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
894 if (IS_ERR_OR_NULL(sta))
896 bitmap = le64_to_cpu(notif->bitmap[i]);
897 ssn = le32_to_cpu(notif->start_seq_num[i]);
899 /* update mac80211 with the bitmap for the reordering buffer */
900 ieee80211_mark_rx_ba_filtered_frames(sta, tid, ssn, bitmap,