1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
3 * Copyright (C) 2012-2014, 2018-2020 Intel Corporation
4 * Copyright (C) 2013-2015 Intel Mobile Communications GmbH
5 * Copyright (C) 2016-2017 Intel Deutschland GmbH
7 #include <asm/unaligned.h>
8 #include <linux/etherdevice.h>
9 #include <linux/skbuff.h>
10 #include "iwl-trans.h"
15 * iwl_mvm_rx_rx_phy_cmd - REPLY_RX_PHY_CMD handler
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.
20 void iwl_mvm_rx_rx_phy_cmd(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
22 struct iwl_rx_packet *pkt = rxb_addr(rxb);
24 memcpy(&mvm->last_phy_info, pkt->data, sizeof(mvm->last_phy_info));
27 #ifdef CONFIG_IWLWIFI_DEBUGFS
28 if (mvm->last_phy_info.phy_flags & cpu_to_le16(RX_RES_PHY_FLAGS_AGG)) {
29 spin_lock(&mvm->drv_stats_lock);
30 mvm->drv_rx_stats.ampdu_count++;
31 spin_unlock(&mvm->drv_stats_lock);
37 * iwl_mvm_pass_packet_to_mac80211 - builds the packet for mac80211
39 * Adds the rxb to a new skb and give it to mac80211
41 static void iwl_mvm_pass_packet_to_mac80211(struct iwl_mvm *mvm,
42 struct ieee80211_sta *sta,
43 struct napi_struct *napi,
45 struct ieee80211_hdr *hdr, u16 len,
47 struct iwl_rx_cmd_buffer *rxb)
49 unsigned int hdrlen = ieee80211_hdrlen(hdr->frame_control);
53 * The 'hdrlen' (plus the 8 bytes for the SNAP and the crypt_len,
54 * but those are all multiples of 4 long) all goes away, but we
55 * want the *end* of it, which is going to be the start of the IP
56 * header, to be aligned when it gets pulled in.
57 * The beginning of the skb->data is aligned on at least a 4-byte
58 * boundary after allocation. Everything here is aligned at least
59 * on a 2-byte boundary so we can just take hdrlen & 3 and pad by
62 skb_reserve(skb, hdrlen & 3);
64 /* If frame is small enough to fit in skb->head, pull it completely.
65 * If not, only pull ieee80211_hdr (including crypto if present, and
66 * an additional 8 bytes for SNAP/ethertype, see below) so that
67 * splice() or TCP coalesce are more efficient.
69 * Since, in addition, ieee80211_data_to_8023() always pull in at
70 * least 8 bytes (possibly more for mesh) we can do the same here
71 * to save the cost of doing it later. That still doesn't pull in
72 * the actual IP header since the typical case has a SNAP header.
73 * If the latter changes (there are efforts in the standards group
74 * to do so) we should revisit this and ieee80211_data_to_8023().
76 hdrlen = (len <= skb_tailroom(skb)) ? len : hdrlen + crypt_len + 8;
78 skb_put_data(skb, hdr, hdrlen);
79 fraglen = len - hdrlen;
82 int offset = (void *)hdr + hdrlen -
83 rxb_addr(rxb) + rxb_offset(rxb);
85 skb_add_rx_frag(skb, 0, rxb_steal_page(rxb), offset,
86 fraglen, rxb->truesize);
89 ieee80211_rx_napi(mvm->hw, sta, skb, napi);
93 * iwl_mvm_get_signal_strength - use new rx PHY INFO API
94 * values are reported by the fw as positive values - need to negate
95 * to obtain their dBM. Account for missing antennas by replacing 0
96 * values by -256dBm: practically 0 power and a non-feasible 8 bit value.
98 static void iwl_mvm_get_signal_strength(struct iwl_mvm *mvm,
99 struct iwl_rx_phy_info *phy_info,
100 struct ieee80211_rx_status *rx_status)
102 int energy_a, energy_b, energy_c, max_energy;
106 le32_to_cpu(phy_info->non_cfg_phy[IWL_RX_INFO_ENERGY_ANT_ABC_IDX]);
107 energy_a = (val & IWL_RX_INFO_ENERGY_ANT_A_MSK) >>
108 IWL_RX_INFO_ENERGY_ANT_A_POS;
109 energy_a = energy_a ? -energy_a : S8_MIN;
110 energy_b = (val & IWL_RX_INFO_ENERGY_ANT_B_MSK) >>
111 IWL_RX_INFO_ENERGY_ANT_B_POS;
112 energy_b = energy_b ? -energy_b : S8_MIN;
113 energy_c = (val & IWL_RX_INFO_ENERGY_ANT_C_MSK) >>
114 IWL_RX_INFO_ENERGY_ANT_C_POS;
115 energy_c = energy_c ? -energy_c : S8_MIN;
116 max_energy = max(energy_a, energy_b);
117 max_energy = max(max_energy, energy_c);
119 IWL_DEBUG_STATS(mvm, "energy In A %d B %d C %d , and max %d\n",
120 energy_a, energy_b, energy_c, max_energy);
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 rx_status->chain_signal[2] = energy_c;
132 * iwl_mvm_set_mac80211_rx_flag - translate fw status to mac80211 format
133 * @mvm: the mvm object
135 * @stats: status in mac80211's format
136 * @rx_pkt_status: status coming from fw
138 * returns non 0 value if the packet should be dropped
140 static u32 iwl_mvm_set_mac80211_rx_flag(struct iwl_mvm *mvm,
141 struct ieee80211_hdr *hdr,
142 struct ieee80211_rx_status *stats,
146 if (!ieee80211_has_protected(hdr->frame_control) ||
147 (rx_pkt_status & RX_MPDU_RES_STATUS_SEC_ENC_MSK) ==
148 RX_MPDU_RES_STATUS_SEC_NO_ENC)
151 /* packet was encrypted with unknown alg */
152 if ((rx_pkt_status & RX_MPDU_RES_STATUS_SEC_ENC_MSK) ==
153 RX_MPDU_RES_STATUS_SEC_ENC_ERR)
156 switch (rx_pkt_status & RX_MPDU_RES_STATUS_SEC_ENC_MSK) {
157 case RX_MPDU_RES_STATUS_SEC_CCM_ENC:
158 /* alg is CCM: check MIC only */
159 if (!(rx_pkt_status & RX_MPDU_RES_STATUS_MIC_OK))
162 stats->flag |= RX_FLAG_DECRYPTED;
163 *crypt_len = IEEE80211_CCMP_HDR_LEN;
166 case RX_MPDU_RES_STATUS_SEC_TKIP_ENC:
167 /* Don't drop the frame and decrypt it in SW */
168 if (!fw_has_api(&mvm->fw->ucode_capa,
169 IWL_UCODE_TLV_API_DEPRECATE_TTAK) &&
170 !(rx_pkt_status & RX_MPDU_RES_STATUS_TTAK_OK))
172 *crypt_len = IEEE80211_TKIP_IV_LEN;
175 case RX_MPDU_RES_STATUS_SEC_WEP_ENC:
176 if (!(rx_pkt_status & RX_MPDU_RES_STATUS_ICV_OK))
179 stats->flag |= RX_FLAG_DECRYPTED;
180 if ((rx_pkt_status & RX_MPDU_RES_STATUS_SEC_ENC_MSK) ==
181 RX_MPDU_RES_STATUS_SEC_WEP_ENC)
182 *crypt_len = IEEE80211_WEP_IV_LEN;
185 case RX_MPDU_RES_STATUS_SEC_EXT_ENC:
186 if (!(rx_pkt_status & RX_MPDU_RES_STATUS_MIC_OK))
188 stats->flag |= RX_FLAG_DECRYPTED;
192 /* Expected in monitor (not having the keys) */
193 if (!mvm->monitor_on)
194 IWL_ERR(mvm, "Unhandled alg: 0x%x\n", rx_pkt_status);
200 static void iwl_mvm_rx_handle_tcm(struct iwl_mvm *mvm,
201 struct ieee80211_sta *sta,
202 struct ieee80211_hdr *hdr, u32 len,
203 struct iwl_rx_phy_info *phy_info,
206 struct iwl_mvm_sta *mvmsta;
207 struct iwl_mvm_tcm_mac *mdata;
209 int ac = IEEE80211_AC_BE; /* treat non-QoS as BE */
210 struct iwl_mvm_vif *mvmvif;
211 /* expected throughput in 100Kbps, single stream, 20 MHz */
212 static const u8 thresh_tpt[] = {
213 9, 18, 30, 42, 60, 78, 90, 96, 120, 135,
217 if (ieee80211_is_data_qos(hdr->frame_control))
218 ac = tid_to_mac80211_ac[ieee80211_get_tid(hdr)];
220 mvmsta = iwl_mvm_sta_from_mac80211(sta);
221 mac = mvmsta->mac_id_n_color & FW_CTXT_ID_MSK;
223 if (time_after(jiffies, mvm->tcm.ts + MVM_TCM_PERIOD))
224 schedule_delayed_work(&mvm->tcm.work, 0);
225 mdata = &mvm->tcm.data[mac];
226 mdata->rx.pkts[ac]++;
228 /* count the airtime only once for each ampdu */
229 if (mdata->rx.last_ampdu_ref != mvm->ampdu_ref) {
230 mdata->rx.last_ampdu_ref = mvm->ampdu_ref;
231 mdata->rx.airtime += le16_to_cpu(phy_info->frame_time);
234 if (!(rate_n_flags & (RATE_MCS_HT_MSK | RATE_MCS_VHT_MSK)))
237 mvmvif = iwl_mvm_vif_from_mac80211(mvmsta->vif);
239 if (mdata->opened_rx_ba_sessions ||
240 mdata->uapsd_nonagg_detect.detected ||
241 (!mvmvif->queue_params[IEEE80211_AC_VO].uapsd &&
242 !mvmvif->queue_params[IEEE80211_AC_VI].uapsd &&
243 !mvmvif->queue_params[IEEE80211_AC_BE].uapsd &&
244 !mvmvif->queue_params[IEEE80211_AC_BK].uapsd) ||
245 mvmsta->sta_id != mvmvif->ap_sta_id)
248 if (rate_n_flags & RATE_MCS_HT_MSK) {
249 thr = thresh_tpt[rate_n_flags & RATE_HT_MCS_RATE_CODE_MSK];
250 thr *= 1 + ((rate_n_flags & RATE_HT_MCS_NSS_MSK) >>
251 RATE_HT_MCS_NSS_POS);
253 if (WARN_ON((rate_n_flags & RATE_VHT_MCS_RATE_CODE_MSK) >=
254 ARRAY_SIZE(thresh_tpt)))
256 thr = thresh_tpt[rate_n_flags & RATE_VHT_MCS_RATE_CODE_MSK];
257 thr *= 1 + ((rate_n_flags & RATE_VHT_MCS_NSS_MSK) >>
258 RATE_VHT_MCS_NSS_POS);
261 thr <<= ((rate_n_flags & RATE_MCS_CHAN_WIDTH_MSK) >>
262 RATE_MCS_CHAN_WIDTH_POS);
264 mdata->uapsd_nonagg_detect.rx_bytes += len;
265 ewma_rate_add(&mdata->uapsd_nonagg_detect.rate, thr);
268 static void iwl_mvm_rx_csum(struct ieee80211_sta *sta,
272 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
273 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(mvmsta->vif);
275 if (mvmvif->features & NETIF_F_RXCSUM &&
276 status & RX_MPDU_RES_STATUS_CSUM_DONE &&
277 status & RX_MPDU_RES_STATUS_CSUM_OK)
278 skb->ip_summed = CHECKSUM_UNNECESSARY;
282 * iwl_mvm_rx_rx_mpdu - REPLY_RX_MPDU_CMD handler
284 * Handles the actual data of the Rx packet from the fw
286 void iwl_mvm_rx_rx_mpdu(struct iwl_mvm *mvm, struct napi_struct *napi,
287 struct iwl_rx_cmd_buffer *rxb)
289 struct ieee80211_hdr *hdr;
290 struct ieee80211_rx_status *rx_status;
291 struct iwl_rx_packet *pkt = rxb_addr(rxb);
292 struct iwl_rx_phy_info *phy_info;
293 struct iwl_rx_mpdu_res_start *rx_res;
294 struct ieee80211_sta *sta = NULL;
296 u32 len, pkt_len = iwl_rx_packet_payload_len(pkt);
301 if (unlikely(pkt_len < sizeof(*rx_res))) {
302 IWL_DEBUG_DROP(mvm, "Bad REPLY_RX_MPDU_CMD size\n");
306 phy_info = &mvm->last_phy_info;
307 rx_res = (struct iwl_rx_mpdu_res_start *)pkt->data;
308 hdr = (struct ieee80211_hdr *)(pkt->data + sizeof(*rx_res));
309 len = le16_to_cpu(rx_res->byte_count);
311 if (unlikely(len + sizeof(*rx_res) + sizeof(__le32) > pkt_len)) {
312 IWL_DEBUG_DROP(mvm, "FW lied about packet len\n");
316 rx_pkt_status = get_unaligned_le32((__le32 *)
317 (pkt->data + sizeof(*rx_res) + len));
319 /* Dont use dev_alloc_skb(), we'll have enough headroom once
320 * ieee80211_hdr pulled.
322 skb = alloc_skb(128, GFP_ATOMIC);
324 IWL_ERR(mvm, "alloc_skb failed\n");
328 rx_status = IEEE80211_SKB_RXCB(skb);
331 * drop the packet if it has failed being decrypted by HW
333 if (iwl_mvm_set_mac80211_rx_flag(mvm, hdr, rx_status, rx_pkt_status,
335 IWL_DEBUG_DROP(mvm, "Bad decryption results 0x%08x\n",
342 * Keep packets with CRC errors (and with overrun) for monitor mode
343 * (otherwise the firmware discards them) but mark them as bad.
345 if (!(rx_pkt_status & RX_MPDU_RES_STATUS_CRC_OK) ||
346 !(rx_pkt_status & RX_MPDU_RES_STATUS_OVERRUN_OK)) {
347 IWL_DEBUG_RX(mvm, "Bad CRC or FIFO: 0x%08X.\n", rx_pkt_status);
348 rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
351 /* This will be used in several places later */
352 rate_n_flags = le32_to_cpu(phy_info->rate_n_flags);
354 /* rx_status carries information about the packet to mac80211 */
355 rx_status->mactime = le64_to_cpu(phy_info->timestamp);
356 rx_status->device_timestamp = le32_to_cpu(phy_info->system_timestamp);
358 (phy_info->phy_flags & cpu_to_le16(RX_RES_PHY_FLAGS_BAND_24)) ?
359 NL80211_BAND_2GHZ : NL80211_BAND_5GHZ;
361 ieee80211_channel_to_frequency(le16_to_cpu(phy_info->channel),
364 /* TSF as indicated by the firmware is at INA time */
365 rx_status->flag |= RX_FLAG_MACTIME_PLCP_START;
367 iwl_mvm_get_signal_strength(mvm, phy_info, rx_status);
369 IWL_DEBUG_STATS_LIMIT(mvm, "Rssi %d, TSF %llu\n", rx_status->signal,
370 (unsigned long long)rx_status->mactime);
373 if (rx_pkt_status & RX_MPDU_RES_STATUS_SRC_STA_FOUND) {
374 u32 id = rx_pkt_status & RX_MPDU_RES_STATUS_STA_ID_MSK;
376 id >>= RX_MDPU_RES_STATUS_STA_ID_SHIFT;
378 if (!WARN_ON_ONCE(id >= mvm->fw->ucode_capa.num_stations)) {
379 sta = rcu_dereference(mvm->fw_id_to_mac_id[id]);
383 } else if (!is_multicast_ether_addr(hdr->addr2)) {
384 /* This is fine since we prevent two stations with the same
385 * address from being added.
387 sta = ieee80211_find_sta_by_ifaddr(mvm->hw, hdr->addr2, NULL);
391 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
392 struct ieee80211_vif *tx_blocked_vif =
393 rcu_dereference(mvm->csa_tx_blocked_vif);
394 struct iwl_fw_dbg_trigger_tlv *trig;
395 struct ieee80211_vif *vif = mvmsta->vif;
397 /* We have tx blocked stations (with CS bit). If we heard
398 * frames from a blocked station on a new channel we can
401 if (unlikely(tx_blocked_vif) && vif == tx_blocked_vif) {
402 struct iwl_mvm_vif *mvmvif =
403 iwl_mvm_vif_from_mac80211(tx_blocked_vif);
405 if (mvmvif->csa_target_freq == rx_status->freq)
406 iwl_mvm_sta_modify_disable_tx_ap(mvm, sta,
410 rs_update_last_rssi(mvm, mvmsta, rx_status);
412 trig = iwl_fw_dbg_trigger_on(&mvm->fwrt,
413 ieee80211_vif_to_wdev(vif),
414 FW_DBG_TRIGGER_RSSI);
416 if (trig && ieee80211_is_beacon(hdr->frame_control)) {
417 struct iwl_fw_dbg_trigger_low_rssi *rssi_trig;
420 rssi_trig = (void *)trig->data;
421 rssi = le32_to_cpu(rssi_trig->rssi);
423 if (rx_status->signal < rssi)
424 iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
428 if (!mvm->tcm.paused && len >= sizeof(*hdr) &&
429 !is_multicast_ether_addr(hdr->addr1) &&
430 ieee80211_is_data(hdr->frame_control))
431 iwl_mvm_rx_handle_tcm(mvm, sta, hdr, len, phy_info,
434 if (ieee80211_is_data(hdr->frame_control))
435 iwl_mvm_rx_csum(sta, skb, rx_pkt_status);
439 /* set the preamble flag if appropriate */
440 if (phy_info->phy_flags & cpu_to_le16(RX_RES_PHY_FLAGS_SHORT_PREAMBLE))
441 rx_status->enc_flags |= RX_ENC_FLAG_SHORTPRE;
443 if (phy_info->phy_flags & cpu_to_le16(RX_RES_PHY_FLAGS_AGG)) {
445 * We know which subframes of an A-MPDU belong
446 * together since we get a single PHY response
447 * from the firmware for all of them
449 rx_status->flag |= RX_FLAG_AMPDU_DETAILS;
450 rx_status->ampdu_reference = mvm->ampdu_ref;
453 /* Set up the HT phy flags */
454 switch (rate_n_flags & RATE_MCS_CHAN_WIDTH_MSK) {
455 case RATE_MCS_CHAN_WIDTH_20:
457 case RATE_MCS_CHAN_WIDTH_40:
458 rx_status->bw = RATE_INFO_BW_40;
460 case RATE_MCS_CHAN_WIDTH_80:
461 rx_status->bw = RATE_INFO_BW_80;
463 case RATE_MCS_CHAN_WIDTH_160:
464 rx_status->bw = RATE_INFO_BW_160;
467 if (!(rate_n_flags & RATE_MCS_CCK_MSK) &&
468 rate_n_flags & RATE_MCS_SGI_MSK)
469 rx_status->enc_flags |= RX_ENC_FLAG_SHORT_GI;
470 if (rate_n_flags & RATE_HT_MCS_GF_MSK)
471 rx_status->enc_flags |= RX_ENC_FLAG_HT_GF;
472 if (rate_n_flags & RATE_MCS_LDPC_MSK)
473 rx_status->enc_flags |= RX_ENC_FLAG_LDPC;
474 if (rate_n_flags & RATE_MCS_HT_MSK) {
475 u8 stbc = (rate_n_flags & RATE_MCS_STBC_MSK) >>
477 rx_status->encoding = RX_ENC_HT;
478 rx_status->rate_idx = rate_n_flags & RATE_HT_MCS_INDEX_MSK;
479 rx_status->enc_flags |= stbc << RX_ENC_FLAG_STBC_SHIFT;
480 } else if (rate_n_flags & RATE_MCS_VHT_MSK) {
481 u8 stbc = (rate_n_flags & RATE_MCS_STBC_MSK) >>
484 ((rate_n_flags & RATE_VHT_MCS_NSS_MSK) >>
485 RATE_VHT_MCS_NSS_POS) + 1;
486 rx_status->rate_idx = rate_n_flags & RATE_VHT_MCS_RATE_CODE_MSK;
487 rx_status->encoding = RX_ENC_VHT;
488 rx_status->enc_flags |= stbc << RX_ENC_FLAG_STBC_SHIFT;
489 if (rate_n_flags & RATE_MCS_BF_MSK)
490 rx_status->enc_flags |= RX_ENC_FLAG_BF;
492 int rate = iwl_mvm_legacy_rate_to_mac80211_idx(rate_n_flags,
495 if (WARN(rate < 0 || rate > 0xFF,
496 "Invalid rate flags 0x%x, band %d,\n",
497 rate_n_flags, rx_status->band)) {
501 rx_status->rate_idx = rate;
504 #ifdef CONFIG_IWLWIFI_DEBUGFS
505 iwl_mvm_update_frame_stats(mvm, rate_n_flags,
506 rx_status->flag & RX_FLAG_AMPDU_DETAILS);
509 if (unlikely((ieee80211_is_beacon(hdr->frame_control) ||
510 ieee80211_is_probe_resp(hdr->frame_control)) &&
511 mvm->sched_scan_pass_all == SCHED_SCAN_PASS_ALL_ENABLED))
512 mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_FOUND;
514 if (unlikely(ieee80211_is_beacon(hdr->frame_control) ||
515 ieee80211_is_probe_resp(hdr->frame_control)))
516 rx_status->boottime_ns = ktime_get_boottime_ns();
518 iwl_mvm_pass_packet_to_mac80211(mvm, sta, napi, skb, hdr, len,
522 struct iwl_mvm_stat_data {
526 u8 beacon_filter_average_energy;
527 __le32 *beacon_counter;
528 u8 *beacon_average_energy;
531 static void iwl_mvm_stat_iterator(void *_data, u8 *mac,
532 struct ieee80211_vif *vif)
534 struct iwl_mvm_stat_data *data = _data;
535 struct iwl_mvm *mvm = data->mvm;
536 int sig = -data->beacon_filter_average_energy;
538 int thold = vif->bss_conf.cqm_rssi_thold;
539 int hyst = vif->bss_conf.cqm_rssi_hyst;
540 u16 id = le32_to_cpu(data->mac_id);
541 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
542 u16 vif_id = mvmvif->id;
544 /* This doesn't need the MAC ID check since it's not taking the
545 * data copied into the "data" struct, but rather the data from
546 * the notification directly.
548 mvmvif->beacon_stats.num_beacons =
549 le32_to_cpu(data->beacon_counter[vif_id]);
550 mvmvif->beacon_stats.avg_signal =
551 -data->beacon_average_energy[vif_id];
553 /* make sure that beacon statistics don't go backwards with TCM
554 * request to clear statistics
556 if (le32_to_cpu(data->flags) & IWL_STATISTICS_REPLY_FLG_CLEAR)
557 mvmvif->beacon_stats.accu_num_beacons +=
558 mvmvif->beacon_stats.num_beacons;
560 if (mvmvif->id != id)
563 if (vif->type != NL80211_IFTYPE_STATION)
567 IWL_DEBUG_RX(mvm, "RSSI is 0 - skip signal based decision\n");
571 mvmvif->bf_data.ave_beacon_signal = sig;
574 if (mvmvif->bf_data.bt_coex_min_thold !=
575 mvmvif->bf_data.bt_coex_max_thold) {
576 last_event = mvmvif->bf_data.last_bt_coex_event;
577 if (sig > mvmvif->bf_data.bt_coex_max_thold &&
578 (last_event <= mvmvif->bf_data.bt_coex_min_thold ||
580 mvmvif->bf_data.last_bt_coex_event = sig;
581 IWL_DEBUG_RX(mvm, "cqm_iterator bt coex high %d\n",
583 iwl_mvm_bt_rssi_event(mvm, vif, RSSI_EVENT_HIGH);
584 } else if (sig < mvmvif->bf_data.bt_coex_min_thold &&
585 (last_event >= mvmvif->bf_data.bt_coex_max_thold ||
587 mvmvif->bf_data.last_bt_coex_event = sig;
588 IWL_DEBUG_RX(mvm, "cqm_iterator bt coex low %d\n",
590 iwl_mvm_bt_rssi_event(mvm, vif, RSSI_EVENT_LOW);
594 if (!(vif->driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI))
597 /* CQM Notification */
598 last_event = mvmvif->bf_data.last_cqm_event;
599 if (thold && sig < thold && (last_event == 0 ||
600 sig < last_event - hyst)) {
601 mvmvif->bf_data.last_cqm_event = sig;
602 IWL_DEBUG_RX(mvm, "cqm_iterator cqm low %d\n",
604 ieee80211_cqm_rssi_notify(
606 NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
609 } else if (sig > thold &&
610 (last_event == 0 || sig > last_event + hyst)) {
611 mvmvif->bf_data.last_cqm_event = sig;
612 IWL_DEBUG_RX(mvm, "cqm_iterator cqm high %d\n",
614 ieee80211_cqm_rssi_notify(
616 NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
623 iwl_mvm_rx_stats_check_trigger(struct iwl_mvm *mvm, struct iwl_rx_packet *pkt)
625 struct iwl_fw_dbg_trigger_tlv *trig;
626 struct iwl_fw_dbg_trigger_stats *trig_stats;
627 u32 trig_offset, trig_thold;
629 trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, NULL, FW_DBG_TRIGGER_STATS);
633 trig_stats = (void *)trig->data;
635 trig_offset = le32_to_cpu(trig_stats->stop_offset);
636 trig_thold = le32_to_cpu(trig_stats->stop_threshold);
638 if (WARN_ON_ONCE(trig_offset >= iwl_rx_packet_payload_len(pkt)))
641 if (le32_to_cpup((__le32 *) (pkt->data + trig_offset)) < trig_thold)
644 iwl_fw_dbg_collect_trig(&mvm->fwrt, trig, NULL);
647 static void iwl_mvm_stats_energy_iter(void *_data,
648 struct ieee80211_sta *sta)
650 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
652 u32 sta_id = mvmsta->sta_id;
654 if (WARN_ONCE(sta_id >= IWL_MVM_STATION_COUNT_MAX, "sta_id %d >= %d",
655 sta_id, IWL_MVM_STATION_COUNT_MAX))
659 mvmsta->avg_energy = energy[sta_id];
664 iwl_mvm_update_tcm_from_stats(struct iwl_mvm *mvm, __le32 *air_time_le,
669 spin_lock(&mvm->tcm.lock);
670 for (i = 0; i < NUM_MAC_INDEX_DRIVER; i++) {
671 struct iwl_mvm_tcm_mac *mdata = &mvm->tcm.data[i];
672 u32 rx_bytes = le32_to_cpu(rx_bytes_le[i]);
673 u32 airtime = le32_to_cpu(air_time_le[i]);
675 mdata->rx.airtime += airtime;
676 mdata->uapsd_nonagg_detect.rx_bytes += rx_bytes;
678 /* re-init every time to store rate from FW */
679 ewma_rate_init(&mdata->uapsd_nonagg_detect.rate);
680 ewma_rate_add(&mdata->uapsd_nonagg_detect.rate,
681 rx_bytes * 8 / airtime);
684 spin_unlock(&mvm->tcm.lock);
688 iwl_mvm_handle_rx_statistics_tlv(struct iwl_mvm *mvm,
689 struct iwl_rx_packet *pkt)
691 struct iwl_mvm_stat_data data = {
694 u8 beacon_average_energy[MAC_INDEX_AUX];
695 u8 average_energy[IWL_MVM_STATION_COUNT_MAX];
696 struct iwl_statistics_operational_ntfy *stats;
701 expected_size = sizeof(*stats);
702 if (WARN_ONCE(iwl_rx_packet_payload_len(pkt) < expected_size,
703 "received invalid statistics size (%d)!, expected_size: %d\n",
704 iwl_rx_packet_payload_len(pkt), expected_size))
707 stats = (void *)&pkt->data;
709 if (WARN_ONCE(stats->hdr.type != FW_STATISTICS_OPERATIONAL ||
710 stats->hdr.version !=
711 iwl_fw_lookup_notif_ver(mvm->fw, LONG_GROUP, STATISTICS_CMD, 0),
712 "received unsupported hdr type %d, version %d\n",
713 stats->hdr.type, stats->hdr.version))
716 flags = stats->flags;
717 mvm->radio_stats.rx_time = le64_to_cpu(stats->rx_time);
718 mvm->radio_stats.tx_time = le64_to_cpu(stats->tx_time);
719 mvm->radio_stats.on_time_rf = le64_to_cpu(stats->on_time_rf);
720 mvm->radio_stats.on_time_scan = le64_to_cpu(stats->on_time_scan);
722 iwl_mvm_rx_stats_check_trigger(mvm, pkt);
724 data.mac_id = stats->mac_id;
725 data.beacon_filter_average_energy =
726 le32_to_cpu(stats->beacon_filter_average_energy);
728 data.beacon_counter = stats->beacon_counter;
729 for (i = 0; i < ARRAY_SIZE(beacon_average_energy); i++)
730 beacon_average_energy[i] =
731 le32_to_cpu(stats->beacon_average_energy[i]);
733 data.beacon_average_energy = beacon_average_energy;
735 ieee80211_iterate_active_interfaces(mvm->hw,
736 IEEE80211_IFACE_ITER_NORMAL,
737 iwl_mvm_stat_iterator,
740 for (i = 0; i < ARRAY_SIZE(average_energy); i++)
741 average_energy[i] = le32_to_cpu(stats->average_energy[i]);
742 ieee80211_iterate_stations_atomic(mvm->hw, iwl_mvm_stats_energy_iter,
745 * Don't update in case the statistics are not cleared, since
746 * we will end up counting twice the same airtime, once in TCM
747 * request and once in statistics notification.
749 if (le32_to_cpu(flags) & IWL_STATISTICS_REPLY_FLG_CLEAR)
750 iwl_mvm_update_tcm_from_stats(mvm, stats->air_time,
754 void iwl_mvm_handle_rx_statistics(struct iwl_mvm *mvm,
755 struct iwl_rx_packet *pkt)
757 struct iwl_mvm_stat_data data = {
760 __le32 *bytes, *air_time, flags;
764 /* From ver 14 and up we use TLV statistics format */
765 if (iwl_fw_lookup_notif_ver(mvm->fw, LONG_GROUP,
766 STATISTICS_CMD, 0) >= 14)
767 return iwl_mvm_handle_rx_statistics_tlv(mvm, pkt);
769 if (!iwl_mvm_has_new_rx_stats_api(mvm)) {
770 if (iwl_mvm_has_new_rx_api(mvm))
771 expected_size = sizeof(struct iwl_notif_statistics_v11);
773 expected_size = sizeof(struct iwl_notif_statistics_v10);
775 expected_size = sizeof(struct iwl_notif_statistics);
778 if (WARN_ONCE(iwl_rx_packet_payload_len(pkt) != expected_size,
779 "received invalid statistics size (%d)!\n",
780 iwl_rx_packet_payload_len(pkt)))
783 if (!iwl_mvm_has_new_rx_stats_api(mvm)) {
784 struct iwl_notif_statistics_v11 *stats = (void *)&pkt->data;
786 data.mac_id = stats->rx.general.mac_id;
787 data.beacon_filter_average_energy =
788 stats->general.common.beacon_filter_average_energy;
790 mvm->rx_stats_v3 = stats->rx;
792 mvm->radio_stats.rx_time =
793 le64_to_cpu(stats->general.common.rx_time);
794 mvm->radio_stats.tx_time =
795 le64_to_cpu(stats->general.common.tx_time);
796 mvm->radio_stats.on_time_rf =
797 le64_to_cpu(stats->general.common.on_time_rf);
798 mvm->radio_stats.on_time_scan =
799 le64_to_cpu(stats->general.common.on_time_scan);
801 data.beacon_counter = stats->general.beacon_counter;
802 data.beacon_average_energy =
803 stats->general.beacon_average_energy;
806 struct iwl_notif_statistics *stats = (void *)&pkt->data;
808 data.mac_id = stats->rx.general.mac_id;
809 data.beacon_filter_average_energy =
810 stats->general.common.beacon_filter_average_energy;
812 mvm->rx_stats = stats->rx;
814 mvm->radio_stats.rx_time =
815 le64_to_cpu(stats->general.common.rx_time);
816 mvm->radio_stats.tx_time =
817 le64_to_cpu(stats->general.common.tx_time);
818 mvm->radio_stats.on_time_rf =
819 le64_to_cpu(stats->general.common.on_time_rf);
820 mvm->radio_stats.on_time_scan =
821 le64_to_cpu(stats->general.common.on_time_scan);
823 data.beacon_counter = stats->general.beacon_counter;
824 data.beacon_average_energy =
825 stats->general.beacon_average_energy;
830 iwl_mvm_rx_stats_check_trigger(mvm, pkt);
832 ieee80211_iterate_active_interfaces(mvm->hw,
833 IEEE80211_IFACE_ITER_NORMAL,
834 iwl_mvm_stat_iterator,
837 if (!iwl_mvm_has_new_rx_api(mvm))
840 if (!iwl_mvm_has_new_rx_stats_api(mvm)) {
841 struct iwl_notif_statistics_v11 *v11 = (void *)&pkt->data;
843 energy = (void *)&v11->load_stats.avg_energy;
844 bytes = (void *)&v11->load_stats.byte_count;
845 air_time = (void *)&v11->load_stats.air_time;
847 struct iwl_notif_statistics *stats = (void *)&pkt->data;
849 energy = (void *)&stats->load_stats.avg_energy;
850 bytes = (void *)&stats->load_stats.byte_count;
851 air_time = (void *)&stats->load_stats.air_time;
853 ieee80211_iterate_stations_atomic(mvm->hw, iwl_mvm_stats_energy_iter,
857 * Don't update in case the statistics are not cleared, since
858 * we will end up counting twice the same airtime, once in TCM
859 * request and once in statistics notification.
861 if (le32_to_cpu(flags) & IWL_STATISTICS_REPLY_FLG_CLEAR)
862 iwl_mvm_update_tcm_from_stats(mvm, air_time, bytes);
866 void iwl_mvm_rx_statistics(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
868 iwl_mvm_handle_rx_statistics(mvm, rxb_addr(rxb));
871 void iwl_mvm_window_status_notif(struct iwl_mvm *mvm,
872 struct iwl_rx_cmd_buffer *rxb)
874 struct iwl_rx_packet *pkt = rxb_addr(rxb);
875 struct iwl_ba_window_status_notif *notif = (void *)pkt->data;
877 u32 pkt_len = iwl_rx_packet_payload_len(pkt);
879 if (WARN_ONCE(pkt_len != sizeof(*notif),
880 "Received window status notification of wrong size (%u)\n",
885 for (i = 0; i < BA_WINDOW_STREAMS_MAX; i++) {
886 struct ieee80211_sta *sta;
893 ratid = le16_to_cpu(notif->ra_tid[i]);
894 /* check that this TID is valid */
895 if (!(ratid & BA_WINDOW_STATUS_VALID_MSK))
898 received_mpdu = le16_to_cpu(notif->mpdu_rx_count[i]);
899 if (received_mpdu == 0)
902 tid = ratid & BA_WINDOW_STATUS_TID_MSK;
903 /* get the station */
904 sta_id = (ratid & BA_WINDOW_STATUS_STA_ID_MSK)
905 >> BA_WINDOW_STATUS_STA_ID_POS;
906 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
907 if (IS_ERR_OR_NULL(sta))
909 bitmap = le64_to_cpu(notif->bitmap[i]);
910 ssn = le32_to_cpu(notif->start_seq_num[i]);
912 /* update mac80211 with the bitmap for the reordering buffer */
913 ieee80211_mark_rx_ba_filtered_frames(sta, tid, ssn, bitmap,