18e99ea8dSJohannes Berg // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
28e99ea8dSJohannes Berg /*
3147eb05fSEmmanuel Grumbach * Copyright (C) 2012-2014, 2018-2023 Intel Corporation
48e99ea8dSJohannes Berg * Copyright (C) 2013-2015 Intel Mobile Communications GmbH
58e99ea8dSJohannes Berg * Copyright (C) 2016-2017 Intel Deutschland GmbH
68e99ea8dSJohannes Berg */
7c5aaa8beSWang Xuerui #include <asm/unaligned.h>
8a399f980SJohannes Berg #include <linux/etherdevice.h>
9e705c121SKalle Valo #include <linux/skbuff.h>
10e705c121SKalle Valo #include "iwl-trans.h"
11e705c121SKalle Valo #include "mvm.h"
12e705c121SKalle Valo #include "fw-api.h"
13e705c121SKalle Valo
14e705c121SKalle Valo /*
15e705c121SKalle Valo * iwl_mvm_rx_rx_phy_cmd - REPLY_RX_PHY_CMD handler
16e705c121SKalle Valo *
17e705c121SKalle Valo * Copies the phy information in mvm->last_phy_info, it will be used when the
18e705c121SKalle Valo * actual data will come from the fw in the next packet.
19e705c121SKalle Valo */
iwl_mvm_rx_rx_phy_cmd(struct iwl_mvm * mvm,struct iwl_rx_cmd_buffer * rxb)20e705c121SKalle Valo void iwl_mvm_rx_rx_phy_cmd(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
21e705c121SKalle Valo {
22e705c121SKalle Valo struct iwl_rx_packet *pkt = rxb_addr(rxb);
231e1a58beSJohannes Berg unsigned int pkt_len = iwl_rx_packet_payload_len(pkt);
241e1a58beSJohannes Berg
251e1a58beSJohannes Berg if (unlikely(pkt_len < sizeof(mvm->last_phy_info)))
261e1a58beSJohannes Berg return;
27e705c121SKalle Valo
28e705c121SKalle Valo memcpy(&mvm->last_phy_info, pkt->data, sizeof(mvm->last_phy_info));
29e705c121SKalle Valo mvm->ampdu_ref++;
30e705c121SKalle Valo
31e705c121SKalle Valo #ifdef CONFIG_IWLWIFI_DEBUGFS
32e705c121SKalle Valo if (mvm->last_phy_info.phy_flags & cpu_to_le16(RX_RES_PHY_FLAGS_AGG)) {
33e705c121SKalle Valo spin_lock(&mvm->drv_stats_lock);
34e705c121SKalle Valo mvm->drv_rx_stats.ampdu_count++;
35e705c121SKalle Valo spin_unlock(&mvm->drv_stats_lock);
36e705c121SKalle Valo }
37e705c121SKalle Valo #endif
38e705c121SKalle Valo }
39e705c121SKalle Valo
40e705c121SKalle Valo /*
41e705c121SKalle Valo * iwl_mvm_pass_packet_to_mac80211 - builds the packet for mac80211
42e705c121SKalle Valo *
43e705c121SKalle Valo * Adds the rxb to a new skb and give it to mac80211
44e705c121SKalle Valo */
iwl_mvm_pass_packet_to_mac80211(struct iwl_mvm * mvm,struct ieee80211_sta * sta,struct napi_struct * napi,struct sk_buff * skb,struct ieee80211_hdr * hdr,u16 len,u8 crypt_len,struct iwl_rx_cmd_buffer * rxb)45e705c121SKalle Valo static void iwl_mvm_pass_packet_to_mac80211(struct iwl_mvm *mvm,
4643ec72b7SJohannes Berg struct ieee80211_sta *sta,
47e705c121SKalle Valo struct napi_struct *napi,
48e705c121SKalle Valo struct sk_buff *skb,
49e705c121SKalle Valo struct ieee80211_hdr *hdr, u16 len,
50bdbc58abSDan Carpenter u8 crypt_len,
51e705c121SKalle Valo struct iwl_rx_cmd_buffer *rxb)
52e705c121SKalle Valo {
535cddd05cSJohannes Berg unsigned int hdrlen = ieee80211_hdrlen(hdr->frame_control);
545cddd05cSJohannes Berg unsigned int fraglen;
555cddd05cSJohannes Berg
565cddd05cSJohannes Berg /*
575cddd05cSJohannes Berg * The 'hdrlen' (plus the 8 bytes for the SNAP and the crypt_len,
585cddd05cSJohannes Berg * but those are all multiples of 4 long) all goes away, but we
595cddd05cSJohannes Berg * want the *end* of it, which is going to be the start of the IP
605cddd05cSJohannes Berg * header, to be aligned when it gets pulled in.
615cddd05cSJohannes Berg * The beginning of the skb->data is aligned on at least a 4-byte
625cddd05cSJohannes Berg * boundary after allocation. Everything here is aligned at least
635cddd05cSJohannes Berg * on a 2-byte boundary so we can just take hdrlen & 3 and pad by
645cddd05cSJohannes Berg * the result.
655cddd05cSJohannes Berg */
665cddd05cSJohannes Berg skb_reserve(skb, hdrlen & 3);
67e705c121SKalle Valo
68e705c121SKalle Valo /* If frame is small enough to fit in skb->head, pull it completely.
69e705c121SKalle Valo * If not, only pull ieee80211_hdr (including crypto if present, and
70e705c121SKalle Valo * an additional 8 bytes for SNAP/ethertype, see below) so that
71e705c121SKalle Valo * splice() or TCP coalesce are more efficient.
72e705c121SKalle Valo *
73e705c121SKalle Valo * Since, in addition, ieee80211_data_to_8023() always pull in at
74e705c121SKalle Valo * least 8 bytes (possibly more for mesh) we can do the same here
75e705c121SKalle Valo * to save the cost of doing it later. That still doesn't pull in
76e705c121SKalle Valo * the actual IP header since the typical case has a SNAP header.
77e705c121SKalle Valo * If the latter changes (there are efforts in the standards group
78e705c121SKalle Valo * to do so) we should revisit this and ieee80211_data_to_8023().
79e705c121SKalle Valo */
805cddd05cSJohannes Berg hdrlen = (len <= skb_tailroom(skb)) ? len : hdrlen + crypt_len + 8;
81e705c121SKalle Valo
8259ae1d12SJohannes Berg skb_put_data(skb, hdr, hdrlen);
83e705c121SKalle Valo fraglen = len - hdrlen;
84e705c121SKalle Valo
85e705c121SKalle Valo if (fraglen) {
863827cb59SJohannes Berg int offset = (u8 *)hdr + hdrlen -
873827cb59SJohannes Berg (u8 *)rxb_addr(rxb) + rxb_offset(rxb);
88e705c121SKalle Valo
89e705c121SKalle Valo skb_add_rx_frag(skb, 0, rxb_steal_page(rxb), offset,
90e705c121SKalle Valo fraglen, rxb->truesize);
91e705c121SKalle Valo }
92e705c121SKalle Valo
9343ec72b7SJohannes Berg ieee80211_rx_napi(mvm->hw, sta, skb, napi);
94e705c121SKalle Valo }
95e705c121SKalle Valo
96e705c121SKalle Valo /*
97e705c121SKalle Valo * iwl_mvm_get_signal_strength - use new rx PHY INFO API
98e705c121SKalle Valo * values are reported by the fw as positive values - need to negate
99e705c121SKalle Valo * to obtain their dBM. Account for missing antennas by replacing 0
100e705c121SKalle Valo * values by -256dBm: practically 0 power and a non-feasible 8 bit value.
101e705c121SKalle Valo */
iwl_mvm_get_signal_strength(struct iwl_mvm * mvm,struct iwl_rx_phy_info * phy_info,struct ieee80211_rx_status * rx_status)102e705c121SKalle Valo static void iwl_mvm_get_signal_strength(struct iwl_mvm *mvm,
103e705c121SKalle Valo struct iwl_rx_phy_info *phy_info,
104e705c121SKalle Valo struct ieee80211_rx_status *rx_status)
105e705c121SKalle Valo {
10657b7b345SMiri Korenblit int energy_a, energy_b, max_energy;
107e705c121SKalle Valo u32 val;
108e705c121SKalle Valo
109e705c121SKalle Valo val =
110e705c121SKalle Valo le32_to_cpu(phy_info->non_cfg_phy[IWL_RX_INFO_ENERGY_ANT_ABC_IDX]);
111e705c121SKalle Valo energy_a = (val & IWL_RX_INFO_ENERGY_ANT_A_MSK) >>
112e705c121SKalle Valo IWL_RX_INFO_ENERGY_ANT_A_POS;
113e705c121SKalle Valo energy_a = energy_a ? -energy_a : S8_MIN;
114e705c121SKalle Valo energy_b = (val & IWL_RX_INFO_ENERGY_ANT_B_MSK) >>
115e705c121SKalle Valo IWL_RX_INFO_ENERGY_ANT_B_POS;
116e705c121SKalle Valo energy_b = energy_b ? -energy_b : S8_MIN;
117e705c121SKalle Valo max_energy = max(energy_a, energy_b);
118e705c121SKalle Valo
11957b7b345SMiri Korenblit IWL_DEBUG_STATS(mvm, "energy In A %d B %d , and max %d\n",
12057b7b345SMiri Korenblit energy_a, energy_b, max_energy);
121e705c121SKalle Valo
122e705c121SKalle Valo rx_status->signal = max_energy;
123e705c121SKalle Valo rx_status->chains = (le16_to_cpu(phy_info->phy_flags) &
124e705c121SKalle Valo RX_RES_PHY_FLAGS_ANTENNA)
125e705c121SKalle Valo >> RX_RES_PHY_FLAGS_ANTENNA_POS;
126e705c121SKalle Valo rx_status->chain_signal[0] = energy_a;
127e705c121SKalle Valo rx_status->chain_signal[1] = energy_b;
128e705c121SKalle Valo }
129e705c121SKalle Valo
130e705c121SKalle Valo /*
131e705c121SKalle Valo * iwl_mvm_set_mac80211_rx_flag - translate fw status to mac80211 format
132e705c121SKalle Valo * @mvm: the mvm object
133e705c121SKalle Valo * @hdr: 80211 header
134e705c121SKalle Valo * @stats: status in mac80211's format
135e705c121SKalle Valo * @rx_pkt_status: status coming from fw
136e705c121SKalle Valo *
137e705c121SKalle Valo * returns non 0 value if the packet should be dropped
138e705c121SKalle Valo */
iwl_mvm_set_mac80211_rx_flag(struct iwl_mvm * mvm,struct ieee80211_hdr * hdr,struct ieee80211_rx_status * stats,u32 rx_pkt_status,u8 * crypt_len)139e705c121SKalle Valo static u32 iwl_mvm_set_mac80211_rx_flag(struct iwl_mvm *mvm,
140e705c121SKalle Valo struct ieee80211_hdr *hdr,
141e705c121SKalle Valo struct ieee80211_rx_status *stats,
142e705c121SKalle Valo u32 rx_pkt_status,
143e705c121SKalle Valo u8 *crypt_len)
144e705c121SKalle Valo {
145e705c121SKalle Valo if (!ieee80211_has_protected(hdr->frame_control) ||
146e705c121SKalle Valo (rx_pkt_status & RX_MPDU_RES_STATUS_SEC_ENC_MSK) ==
147e705c121SKalle Valo RX_MPDU_RES_STATUS_SEC_NO_ENC)
148e705c121SKalle Valo return 0;
149e705c121SKalle Valo
150e705c121SKalle Valo /* packet was encrypted with unknown alg */
151e705c121SKalle Valo if ((rx_pkt_status & RX_MPDU_RES_STATUS_SEC_ENC_MSK) ==
152e705c121SKalle Valo RX_MPDU_RES_STATUS_SEC_ENC_ERR)
153e705c121SKalle Valo return 0;
154e705c121SKalle Valo
155e705c121SKalle Valo switch (rx_pkt_status & RX_MPDU_RES_STATUS_SEC_ENC_MSK) {
156e705c121SKalle Valo case RX_MPDU_RES_STATUS_SEC_CCM_ENC:
157e705c121SKalle Valo /* alg is CCM: check MIC only */
158e705c121SKalle Valo if (!(rx_pkt_status & RX_MPDU_RES_STATUS_MIC_OK))
159e705c121SKalle Valo return -1;
160e705c121SKalle Valo
161e705c121SKalle Valo stats->flag |= RX_FLAG_DECRYPTED;
162e705c121SKalle Valo *crypt_len = IEEE80211_CCMP_HDR_LEN;
163e705c121SKalle Valo return 0;
164e705c121SKalle Valo
165e705c121SKalle Valo case RX_MPDU_RES_STATUS_SEC_TKIP_ENC:
166e705c121SKalle Valo /* Don't drop the frame and decrypt it in SW */
16757df3839SSara Sharon if (!fw_has_api(&mvm->fw->ucode_capa,
16857df3839SSara Sharon IWL_UCODE_TLV_API_DEPRECATE_TTAK) &&
16957df3839SSara Sharon !(rx_pkt_status & RX_MPDU_RES_STATUS_TTAK_OK))
170e705c121SKalle Valo return 0;
171e705c121SKalle Valo *crypt_len = IEEE80211_TKIP_IV_LEN;
1725a2abdcaSGustavo A. R. Silva fallthrough;
173e705c121SKalle Valo
174e705c121SKalle Valo case RX_MPDU_RES_STATUS_SEC_WEP_ENC:
175e705c121SKalle Valo if (!(rx_pkt_status & RX_MPDU_RES_STATUS_ICV_OK))
176e705c121SKalle Valo return -1;
177e705c121SKalle Valo
178e705c121SKalle Valo stats->flag |= RX_FLAG_DECRYPTED;
179e705c121SKalle Valo if ((rx_pkt_status & RX_MPDU_RES_STATUS_SEC_ENC_MSK) ==
180e705c121SKalle Valo RX_MPDU_RES_STATUS_SEC_WEP_ENC)
181e705c121SKalle Valo *crypt_len = IEEE80211_WEP_IV_LEN;
182e705c121SKalle Valo return 0;
183e705c121SKalle Valo
184e705c121SKalle Valo case RX_MPDU_RES_STATUS_SEC_EXT_ENC:
185e705c121SKalle Valo if (!(rx_pkt_status & RX_MPDU_RES_STATUS_MIC_OK))
186e705c121SKalle Valo return -1;
187e705c121SKalle Valo stats->flag |= RX_FLAG_DECRYPTED;
188e705c121SKalle Valo return 0;
189e705c121SKalle Valo
190e705c121SKalle Valo default:
191baf41bc3SShaul Triebitz /* Expected in monitor (not having the keys) */
192baf41bc3SShaul Triebitz if (!mvm->monitor_on)
193876882b5SGolan Ben Ami IWL_WARN(mvm, "Unhandled alg: 0x%x\n", rx_pkt_status);
194e705c121SKalle Valo }
195e705c121SKalle Valo
196e705c121SKalle Valo return 0;
197e705c121SKalle Valo }
198e705c121SKalle Valo
iwl_mvm_rx_handle_tcm(struct iwl_mvm * mvm,struct ieee80211_sta * sta,struct ieee80211_hdr * hdr,u32 len,struct iwl_rx_phy_info * phy_info,u32 rate_n_flags)1997d9d0d56SLuca Coelho static void iwl_mvm_rx_handle_tcm(struct iwl_mvm *mvm,
2007d9d0d56SLuca Coelho struct ieee80211_sta *sta,
2017d9d0d56SLuca Coelho struct ieee80211_hdr *hdr, u32 len,
2027d9d0d56SLuca Coelho struct iwl_rx_phy_info *phy_info,
2037d9d0d56SLuca Coelho u32 rate_n_flags)
2047d9d0d56SLuca Coelho {
2057d9d0d56SLuca Coelho struct iwl_mvm_sta *mvmsta;
2067d9d0d56SLuca Coelho struct iwl_mvm_tcm_mac *mdata;
2077d9d0d56SLuca Coelho int mac;
2087d9d0d56SLuca Coelho int ac = IEEE80211_AC_BE; /* treat non-QoS as BE */
209b0ffe455SJohannes Berg struct iwl_mvm_vif *mvmvif;
210b0ffe455SJohannes Berg /* expected throughput in 100Kbps, single stream, 20 MHz */
211b0ffe455SJohannes Berg static const u8 thresh_tpt[] = {
212b0ffe455SJohannes Berg 9, 18, 30, 42, 60, 78, 90, 96, 120, 135,
213b0ffe455SJohannes Berg };
214b0ffe455SJohannes Berg u16 thr;
2157d9d0d56SLuca Coelho
2167d9d0d56SLuca Coelho if (ieee80211_is_data_qos(hdr->frame_control)) {
2177d9d0d56SLuca Coelho u8 tid = ieee80211_get_tid(hdr);
2187d9d0d56SLuca Coelho
2197d9d0d56SLuca Coelho if (tid < IWL_MAX_TID_COUNT)
2207d9d0d56SLuca Coelho ac = tid_to_mac80211_ac[tid];
2217d9d0d56SLuca Coelho }
2227d9d0d56SLuca Coelho
2237d9d0d56SLuca Coelho mvmsta = iwl_mvm_sta_from_mac80211(sta);
2247d9d0d56SLuca Coelho mac = mvmsta->mac_id_n_color & FW_CTXT_ID_MSK;
2257d9d0d56SLuca Coelho
2267d9d0d56SLuca Coelho if (time_after(jiffies, mvm->tcm.ts + MVM_TCM_PERIOD))
2277d9d0d56SLuca Coelho schedule_delayed_work(&mvm->tcm.work, 0);
2287d9d0d56SLuca Coelho mdata = &mvm->tcm.data[mac];
2297d9d0d56SLuca Coelho mdata->rx.pkts[ac]++;
2307d9d0d56SLuca Coelho
2317d9d0d56SLuca Coelho /* count the airtime only once for each ampdu */
2327d9d0d56SLuca Coelho if (mdata->rx.last_ampdu_ref != mvm->ampdu_ref) {
23348c6ebc1SMiri Korenblit mdata->rx.last_ampdu_ref = mvm->ampdu_ref;
2347d9d0d56SLuca Coelho mdata->rx.airtime += le16_to_cpu(phy_info->frame_time);
2357d9d0d56SLuca Coelho }
236b0ffe455SJohannes Berg
237b0ffe455SJohannes Berg if (!(rate_n_flags & (RATE_MCS_HT_MSK_V1 | RATE_MCS_VHT_MSK_V1)))
238b0ffe455SJohannes Berg return;
239b0ffe455SJohannes Berg
240650cadb7SGregory Greenman mvmvif = iwl_mvm_vif_from_mac80211(mvmsta->vif);
241650cadb7SGregory Greenman
242650cadb7SGregory Greenman if (mdata->opened_rx_ba_sessions ||
243650cadb7SGregory Greenman mdata->uapsd_nonagg_detect.detected ||
244c8ee33e1SGregory Greenman (!mvmvif->deflink.queue_params[IEEE80211_AC_VO].uapsd &&
245b0ffe455SJohannes Berg !mvmvif->deflink.queue_params[IEEE80211_AC_VI].uapsd &&
246b0ffe455SJohannes Berg !mvmvif->deflink.queue_params[IEEE80211_AC_BE].uapsd &&
24748c6ebc1SMiri Korenblit !mvmvif->deflink.queue_params[IEEE80211_AC_BK].uapsd) ||
24848c6ebc1SMiri Korenblit mvmsta->deflink.sta_id != mvmvif->deflink.ap_sta_id)
24948c6ebc1SMiri Korenblit return;
25048c6ebc1SMiri Korenblit
251b0ffe455SJohannes Berg if (rate_n_flags & RATE_MCS_HT_MSK_V1) {
252b0ffe455SJohannes Berg thr = thresh_tpt[rate_n_flags & RATE_HT_MCS_RATE_CODE_MSK_V1];
253b0ffe455SJohannes Berg thr *= 1 + ((rate_n_flags & RATE_HT_MCS_NSS_MSK_V1) >>
254b0ffe455SJohannes Berg RATE_HT_MCS_NSS_POS_V1);
255b0ffe455SJohannes Berg } else {
256774302d2SMordechay Goodstein if (WARN_ON((rate_n_flags & RATE_VHT_MCS_RATE_CODE_MSK) >=
257b0ffe455SJohannes Berg ARRAY_SIZE(thresh_tpt)))
258b0ffe455SJohannes Berg return;
25948c6ebc1SMiri Korenblit thr = thresh_tpt[rate_n_flags & RATE_VHT_MCS_RATE_CODE_MSK];
260b0ffe455SJohannes Berg thr *= 1 + FIELD_GET(RATE_MCS_NSS_MSK, rate_n_flags);
261b0ffe455SJohannes Berg }
262b0ffe455SJohannes Berg
263b0ffe455SJohannes Berg thr <<= ((rate_n_flags & RATE_MCS_CHAN_WIDTH_MSK_V1) >>
2647d9d0d56SLuca Coelho RATE_MCS_CHAN_WIDTH_POS);
2657d9d0d56SLuca Coelho
266e705c121SKalle Valo mdata->uapsd_nonagg_detect.rx_bytes += len;
267e705c121SKalle Valo ewma_rate_add(&mdata->uapsd_nonagg_detect.rate, thr);
268e705c121SKalle Valo }
269e705c121SKalle Valo
iwl_mvm_rx_csum(struct ieee80211_sta * sta,struct sk_buff * skb,u32 status)270e705c121SKalle Valo static void iwl_mvm_rx_csum(struct ieee80211_sta *sta,
271e705c121SKalle Valo struct sk_buff *skb,
272e705c121SKalle Valo u32 status)
273e705c121SKalle Valo {
274e705c121SKalle Valo struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
275e705c121SKalle Valo struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(mvmsta->vif);
276e705c121SKalle Valo
277e705c121SKalle Valo if (mvmvif->features & NETIF_F_RXCSUM &&
278e705c121SKalle Valo status & RX_MPDU_RES_STATUS_CSUM_DONE &&
279e705c121SKalle Valo status & RX_MPDU_RES_STATUS_CSUM_OK)
280e705c121SKalle Valo skb->ip_summed = CHECKSUM_UNNECESSARY;
281e705c121SKalle Valo }
282e705c121SKalle Valo
283e705c121SKalle Valo /*
284e705c121SKalle Valo * iwl_mvm_rx_rx_mpdu - REPLY_RX_MPDU_CMD handler
285e705c121SKalle Valo *
286e705c121SKalle Valo * Handles the actual data of the Rx packet from the fw
287e705c121SKalle Valo */
iwl_mvm_rx_rx_mpdu(struct iwl_mvm * mvm,struct napi_struct * napi,struct iwl_rx_cmd_buffer * rxb)288e705c121SKalle Valo void iwl_mvm_rx_rx_mpdu(struct iwl_mvm *mvm, struct napi_struct *napi,
289e705c121SKalle Valo struct iwl_rx_cmd_buffer *rxb)
290e705c121SKalle Valo {
291e705c121SKalle Valo struct ieee80211_hdr *hdr;
292a399f980SJohannes Berg struct ieee80211_rx_status *rx_status;
293e705c121SKalle Valo struct iwl_rx_packet *pkt = rxb_addr(rxb);
294efc0ec5aSJohannes Berg struct iwl_rx_phy_info *phy_info;
295e705c121SKalle Valo struct iwl_rx_mpdu_res_start *rx_res;
296e705c121SKalle Valo struct ieee80211_sta *sta = NULL;
297e705c121SKalle Valo struct sk_buff *skb;
298e705c121SKalle Valo u32 len, pkt_len = iwl_rx_packet_payload_len(pkt);
299b8aba27cSJohannes Berg u32 rate_n_flags;
300b8aba27cSJohannes Berg u32 rx_pkt_status;
301b8aba27cSJohannes Berg u8 crypt_len = 0;
302b8aba27cSJohannes Berg
303b8aba27cSJohannes Berg if (unlikely(pkt_len < sizeof(*rx_res))) {
304e705c121SKalle Valo IWL_DEBUG_DROP(mvm, "Bad REPLY_RX_MPDU_CMD size\n");
305e705c121SKalle Valo return;
306e705c121SKalle Valo }
307e705c121SKalle Valo
308efc0ec5aSJohannes Berg phy_info = &mvm->last_phy_info;
309efc0ec5aSJohannes Berg rx_res = (struct iwl_rx_mpdu_res_start *)pkt->data;
310efc0ec5aSJohannes Berg hdr = (struct ieee80211_hdr *)(pkt->data + sizeof(*rx_res));
311efc0ec5aSJohannes Berg len = le16_to_cpu(rx_res->byte_count);
312efc0ec5aSJohannes Berg
313efc0ec5aSJohannes Berg if (unlikely(len + sizeof(*rx_res) + sizeof(__le32) > pkt_len)) {
314c5aaa8beSWang Xuerui IWL_DEBUG_DROP(mvm, "FW lied about packet len\n");
315e705c121SKalle Valo return;
316e705c121SKalle Valo }
317e705c121SKalle Valo
318e705c121SKalle Valo rx_pkt_status = get_unaligned_le32((__le32 *)
319e705c121SKalle Valo (pkt->data + sizeof(*rx_res) + len));
320e705c121SKalle Valo
321e705c121SKalle Valo /* Dont use dev_alloc_skb(), we'll have enough headroom once
322e705c121SKalle Valo * ieee80211_hdr pulled.
323e705c121SKalle Valo */
324e705c121SKalle Valo skb = alloc_skb(128, GFP_ATOMIC);
325e705c121SKalle Valo if (!skb) {
326e705c121SKalle Valo IWL_ERR(mvm, "alloc_skb failed\n");
327e705c121SKalle Valo return;
328e705c121SKalle Valo }
329e705c121SKalle Valo
330e705c121SKalle Valo rx_status = IEEE80211_SKB_RXCB(skb);
331e705c121SKalle Valo
332e705c121SKalle Valo /*
333e705c121SKalle Valo * Keep packets with CRC errors (and with overrun) for monitor mode
334e705c121SKalle Valo * (otherwise the firmware discards them) but mark them as bad.
335e705c121SKalle Valo */
336e705c121SKalle Valo if (!(rx_pkt_status & RX_MPDU_RES_STATUS_CRC_OK) ||
337e705c121SKalle Valo !(rx_pkt_status & RX_MPDU_RES_STATUS_OVERRUN_OK)) {
338e705c121SKalle Valo IWL_DEBUG_RX(mvm, "Bad CRC or FIFO: 0x%08X.\n", rx_pkt_status);
339e705c121SKalle Valo rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
340e705c121SKalle Valo }
341e705c121SKalle Valo
342e705c121SKalle Valo /* This will be used in several places later */
343e705c121SKalle Valo rate_n_flags = le32_to_cpu(phy_info->rate_n_flags);
344e705c121SKalle Valo
345e705c121SKalle Valo /* rx_status carries information about the packet to mac80211 */
34657fbcce3SJohannes Berg rx_status->mactime = le64_to_cpu(phy_info->timestamp);
347e705c121SKalle Valo rx_status->device_timestamp = le32_to_cpu(phy_info->system_timestamp);
348e705c121SKalle Valo rx_status->band =
349e705c121SKalle Valo (phy_info->phy_flags & cpu_to_le16(RX_RES_PHY_FLAGS_BAND_24)) ?
35077fe7395SSara Sharon NL80211_BAND_2GHZ : NL80211_BAND_5GHZ;
35177fe7395SSara Sharon rx_status->freq =
35277fe7395SSara Sharon ieee80211_channel_to_frequency(le16_to_cpu(phy_info->channel),
353e705c121SKalle Valo rx_status->band);
354e705c121SKalle Valo
355e705c121SKalle Valo /* TSF as indicated by the firmware is at INA time */
356e705c121SKalle Valo rx_status->flag |= RX_FLAG_MACTIME_PLCP_START;
357e705c121SKalle Valo
358e705c121SKalle Valo iwl_mvm_get_signal_strength(mvm, phy_info, rx_status);
359e705c121SKalle Valo
360a399f980SJohannes Berg IWL_DEBUG_STATS_LIMIT(mvm, "Rssi %d, TSF %llu\n", rx_status->signal,
361a399f980SJohannes Berg (unsigned long long)rx_status->mactime);
362a399f980SJohannes Berg
363a399f980SJohannes Berg rcu_read_lock();
364a399f980SJohannes Berg if (rx_pkt_status & RX_MPDU_RES_STATUS_SRC_STA_FOUND) {
365be9ae34eSNathan Errera u32 id = rx_pkt_status & RX_MPDU_RES_STATUS_STA_ID_MSK;
366a399f980SJohannes Berg
367a399f980SJohannes Berg id >>= RX_MDPU_RES_STATUS_STA_ID_SHIFT;
368a399f980SJohannes Berg
369a399f980SJohannes Berg if (!WARN_ON_ONCE(id >= mvm->fw->ucode_capa.num_stations)) {
370a399f980SJohannes Berg sta = rcu_dereference(mvm->fw_id_to_mac_id[id]);
371a399f980SJohannes Berg if (IS_ERR(sta))
372a399f980SJohannes Berg sta = NULL;
373e705c121SKalle Valo }
374a399f980SJohannes Berg } else if (!is_multicast_ether_addr(hdr->addr2)) {
375e705c121SKalle Valo /* This is fine since we prevent two stations with the same
376e705c121SKalle Valo * address from being added.
377e705c121SKalle Valo */
378e705c121SKalle Valo sta = ieee80211_find_sta_by_ifaddr(mvm->hw, hdr->addr2, NULL);
379147eb05fSEmmanuel Grumbach }
380147eb05fSEmmanuel Grumbach
381147eb05fSEmmanuel Grumbach if (sta) {
382147eb05fSEmmanuel Grumbach struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
383147eb05fSEmmanuel Grumbach struct ieee80211_vif *vif = mvmsta->vif;
384147eb05fSEmmanuel Grumbach struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
385147eb05fSEmmanuel Grumbach
386*8e5a2636SAyala Beker /*
387*8e5a2636SAyala Beker * Don't even try to decrypt a MCAST frame that was received
388147eb05fSEmmanuel Grumbach * before the managed vif is authorized, we'd fail anyway.
389*8e5a2636SAyala Beker */
390147eb05fSEmmanuel Grumbach if (is_multicast_ether_addr(hdr->addr1) &&
391147eb05fSEmmanuel Grumbach vif->type == NL80211_IFTYPE_STATION &&
392147eb05fSEmmanuel Grumbach !mvmvif->authorized &&
393147eb05fSEmmanuel Grumbach ieee80211_has_protected(hdr->frame_control)) {
394147eb05fSEmmanuel Grumbach IWL_DEBUG_DROP(mvm, "MCAST before the vif is authorized\n");
395147eb05fSEmmanuel Grumbach kfree_skb(skb);
396147eb05fSEmmanuel Grumbach rcu_read_unlock();
397147eb05fSEmmanuel Grumbach return;
398147eb05fSEmmanuel Grumbach }
399147eb05fSEmmanuel Grumbach }
400147eb05fSEmmanuel Grumbach
401147eb05fSEmmanuel Grumbach /*
402147eb05fSEmmanuel Grumbach * drop the packet if it has failed being decrypted by HW
403147eb05fSEmmanuel Grumbach */
404147eb05fSEmmanuel Grumbach if (iwl_mvm_set_mac80211_rx_flag(mvm, hdr, rx_status, rx_pkt_status,
405147eb05fSEmmanuel Grumbach &crypt_len)) {
406147eb05fSEmmanuel Grumbach IWL_DEBUG_DROP(mvm, "Bad decryption results 0x%08x\n",
407147eb05fSEmmanuel Grumbach rx_pkt_status);
408147eb05fSEmmanuel Grumbach kfree_skb(skb);
409147eb05fSEmmanuel Grumbach rcu_read_unlock();
410147eb05fSEmmanuel Grumbach return;
411d3a108a4SAndrei Otcheretianski }
412d3a108a4SAndrei Otcheretianski
4136c042d75SSara Sharon if (sta) {
4146c042d75SSara Sharon struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
415e705c121SKalle Valo struct ieee80211_vif *tx_blocked_vif =
416a399f980SJohannes Berg rcu_dereference(mvm->csa_tx_blocked_vif);
417a399f980SJohannes Berg struct iwl_fw_dbg_trigger_tlv *trig;
418a399f980SJohannes Berg struct ieee80211_vif *vif = mvmsta->vif;
419a399f980SJohannes Berg
4206c042d75SSara Sharon /* We have tx blocked stations (with CS bit). If we heard
421d3a108a4SAndrei Otcheretianski * frames from a blocked station on a new channel we can
422d3a108a4SAndrei Otcheretianski * TX to it again.
423d3a108a4SAndrei Otcheretianski */
424d3a108a4SAndrei Otcheretianski if (unlikely(tx_blocked_vif) && vif == tx_blocked_vif) {
425d3a108a4SAndrei Otcheretianski struct iwl_mvm_vif *mvmvif =
426d3a108a4SAndrei Otcheretianski iwl_mvm_vif_from_mac80211(tx_blocked_vif);
427d3a108a4SAndrei Otcheretianski
428a399f980SJohannes Berg if (mvmvif->csa_target_freq == rx_status->freq)
429ecaf71deSGregory Greenman iwl_mvm_sta_modify_disable_tx_ap(mvm, sta,
430e705c121SKalle Valo false);
4316c042d75SSara Sharon }
4326c042d75SSara Sharon
4336c042d75SSara Sharon rs_update_last_rssi(mvm, mvmsta, rx_status);
4346c042d75SSara Sharon
4356c042d75SSara Sharon trig = iwl_fw_dbg_trigger_on(&mvm->fwrt,
436e705c121SKalle Valo ieee80211_vif_to_wdev(vif),
437e705c121SKalle Valo FW_DBG_TRIGGER_RSSI);
438e705c121SKalle Valo
439e705c121SKalle Valo if (trig && ieee80211_is_beacon(hdr->frame_control)) {
440e705c121SKalle Valo struct iwl_fw_dbg_trigger_low_rssi *rssi_trig;
441e705c121SKalle Valo s32 rssi;
4426c042d75SSara Sharon
4437174beb6SJohannes Berg rssi_trig = (void *)trig->data;
4447174beb6SJohannes Berg rssi = le32_to_cpu(rssi_trig->rssi);
445e705c121SKalle Valo
446e705c121SKalle Valo if (rx_status->signal < rssi)
4477d9d0d56SLuca Coelho iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
4487d9d0d56SLuca Coelho NULL);
4497d9d0d56SLuca Coelho }
4507d9d0d56SLuca Coelho
4517d9d0d56SLuca Coelho if (!mvm->tcm.paused && len >= sizeof(*hdr) &&
4527d9d0d56SLuca Coelho !is_multicast_ether_addr(hdr->addr1) &&
453a399f980SJohannes Berg ieee80211_is_data(hdr->frame_control))
454e705c121SKalle Valo iwl_mvm_rx_handle_tcm(mvm, sta, hdr, len, phy_info,
455a399f980SJohannes Berg rate_n_flags);
456e705c121SKalle Valo
457e705c121SKalle Valo if (ieee80211_is_data(hdr->frame_control))
458e705c121SKalle Valo iwl_mvm_rx_csum(sta, skb, rx_pkt_status);
459e705c121SKalle Valo }
4607fdd69c5SJohannes Berg rcu_read_unlock();
461e705c121SKalle Valo
462e705c121SKalle Valo /* set the preamble flag if appropriate */
463e705c121SKalle Valo if (phy_info->phy_flags & cpu_to_le16(RX_RES_PHY_FLAGS_SHORT_PREAMBLE))
464e705c121SKalle Valo rx_status->enc_flags |= RX_ENC_FLAG_SHORTPRE;
465e705c121SKalle Valo
466e705c121SKalle Valo if (phy_info->phy_flags & cpu_to_le16(RX_RES_PHY_FLAGS_AGG)) {
467e705c121SKalle Valo /*
468e705c121SKalle Valo * We know which subframes of an A-MPDU belong
469e705c121SKalle Valo * together since we get a single PHY response
470e705c121SKalle Valo * from the firmware for all of them
471e705c121SKalle Valo */
472e705c121SKalle Valo rx_status->flag |= RX_FLAG_AMPDU_DETAILS;
47348c6ebc1SMiri Korenblit rx_status->ampdu_reference = mvm->ampdu_ref;
474e705c121SKalle Valo }
475e705c121SKalle Valo
476e705c121SKalle Valo /* Set up the HT phy flags */
477da6a4352SJohannes Berg switch (rate_n_flags & RATE_MCS_CHAN_WIDTH_MSK_V1) {
478e705c121SKalle Valo case RATE_MCS_CHAN_WIDTH_20:
479e705c121SKalle Valo break;
480da6a4352SJohannes Berg case RATE_MCS_CHAN_WIDTH_40:
481e705c121SKalle Valo rx_status->bw = RATE_INFO_BW_40;
482e705c121SKalle Valo break;
483da6a4352SJohannes Berg case RATE_MCS_CHAN_WIDTH_80:
484e705c121SKalle Valo rx_status->bw = RATE_INFO_BW_80;
485e705c121SKalle Valo break;
48648c6ebc1SMiri Korenblit case RATE_MCS_CHAN_WIDTH_160:
48748c6ebc1SMiri Korenblit rx_status->bw = RATE_INFO_BW_160;
4887fdd69c5SJohannes Berg break;
489e705c121SKalle Valo }
4907fdd69c5SJohannes Berg if (!(rate_n_flags & RATE_MCS_CCK_MSK_V1) &&
49148c6ebc1SMiri Korenblit rate_n_flags & RATE_MCS_SGI_MSK_V1)
4927fdd69c5SJohannes Berg rx_status->enc_flags |= RX_ENC_FLAG_SHORT_GI;
49348c6ebc1SMiri Korenblit if (rate_n_flags & RATE_HT_MCS_GF_MSK)
49477e40945SSara Sharon rx_status->enc_flags |= RX_ENC_FLAG_HT_GF;
495e705c121SKalle Valo if (rate_n_flags & RATE_MCS_LDPC_MSK_V1)
496da6a4352SJohannes Berg rx_status->enc_flags |= RX_ENC_FLAG_LDPC;
49748c6ebc1SMiri Korenblit if (rate_n_flags & RATE_MCS_HT_MSK_V1) {
4987fdd69c5SJohannes Berg u8 stbc = (rate_n_flags & RATE_MCS_STBC_MSK) >>
49948c6ebc1SMiri Korenblit RATE_MCS_STBC_POS;
50077e40945SSara Sharon rx_status->encoding = RX_ENC_HT;
501e705c121SKalle Valo rx_status->rate_idx = rate_n_flags & RATE_HT_MCS_INDEX_MSK_V1;
5028613c948SJohannes Berg rx_status->enc_flags |= stbc << RX_ENC_FLAG_STBC_SHIFT;
503774302d2SMordechay Goodstein } else if (rate_n_flags & RATE_MCS_VHT_MSK_V1) {
504e705c121SKalle Valo u8 stbc = (rate_n_flags & RATE_MCS_STBC_MSK) >>
505da6a4352SJohannes Berg RATE_MCS_STBC_POS;
5067fdd69c5SJohannes Berg rx_status->nss =
507e705c121SKalle Valo FIELD_GET(RATE_MCS_NSS_MSK, rate_n_flags) + 1;
5087fdd69c5SJohannes Berg rx_status->rate_idx = rate_n_flags & RATE_VHT_MCS_RATE_CODE_MSK;
509e705c121SKalle Valo rx_status->encoding = RX_ENC_VHT;
510cb2de6bbSSara Sharon rx_status->enc_flags |= stbc << RX_ENC_FLAG_STBC_SHIFT;
511e705c121SKalle Valo if (rate_n_flags & RATE_MCS_BF_MSK)
512cb2de6bbSSara Sharon rx_status->enc_flags |= RX_ENC_FLAG_BF;
513cb2de6bbSSara Sharon } else {
514cb2de6bbSSara Sharon int rate = iwl_mvm_legacy_rate_to_mac80211_idx(rate_n_flags,
515cb2de6bbSSara Sharon rx_status->band);
516cb2de6bbSSara Sharon
517cb2de6bbSSara Sharon if (WARN(rate < 0 || rate > 0xFF,
518cb2de6bbSSara Sharon "Invalid rate flags 0x%x, band %d,\n",
519cb2de6bbSSara Sharon rate_n_flags, rx_status->band)) {
520e705c121SKalle Valo kfree_skb(skb);
521e705c121SKalle Valo return;
522e705c121SKalle Valo }
523e705c121SKalle Valo rx_status->rate_idx = rate;
524e705c121SKalle Valo }
525e705c121SKalle Valo
526a339e918SLuca Coelho #ifdef CONFIG_IWLWIFI_DEBUGFS
527a339e918SLuca Coelho iwl_mvm_update_frame_stats(mvm, rate_n_flags,
528a339e918SLuca Coelho rx_status->flag & RX_FLAG_AMPDU_DETAILS);
529a339e918SLuca Coelho #endif
530a339e918SLuca Coelho
531a339e918SLuca Coelho if (unlikely((ieee80211_is_beacon(hdr->frame_control) ||
53243ec72b7SJohannes Berg ieee80211_is_probe_resp(hdr->frame_control)) &&
53343ec72b7SJohannes Berg mvm->sched_scan_pass_all == SCHED_SCAN_PASS_ALL_ENABLED))
5349285ec4cSJason A. Donenfeld mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_FOUND;
53543ec72b7SJohannes Berg
53643ec72b7SJohannes Berg if (unlikely(ieee80211_is_beacon(hdr->frame_control) ||
537bdbc58abSDan Carpenter ieee80211_is_probe_resp(hdr->frame_control)))
538e705c121SKalle Valo rx_status->boottime_ns = ktime_get_boottime_ns();
539e705c121SKalle Valo
540e705c121SKalle Valo iwl_mvm_pass_packet_to_mac80211(mvm, sta, napi, skb, hdr, len,
541e705c121SKalle Valo crypt_len, rxb);
542290d5e49SMordechay Goodstein }
543e705c121SKalle Valo
544e705c121SKalle Valo struct iwl_mvm_stat_data {
545853f4954SMordechay Goodstein struct iwl_mvm *mvm;
546853f4954SMordechay Goodstein __le32 flags;
547e705c121SKalle Valo __le32 mac_id;
548e705c121SKalle Valo u8 beacon_filter_average_energy;
5496324c173SMordechay Goodstein __le32 *beacon_counter;
5506324c173SMordechay Goodstein u8 *beacon_average_energy;
5516324c173SMordechay Goodstein };
5526324c173SMordechay Goodstein
5536324c173SMordechay Goodstein struct iwl_mvm_stat_data_all_macs {
5546324c173SMordechay Goodstein struct iwl_mvm *mvm;
5556324c173SMordechay Goodstein __le32 flags;
556e705c121SKalle Valo struct iwl_statistics_ntfy_per_mac *per_mac_stats;
5576324c173SMordechay Goodstein };
5586324c173SMordechay Goodstein
iwl_mvm_update_vif_sig(struct ieee80211_vif * vif,int sig)559e705c121SKalle Valo static void iwl_mvm_update_vif_sig(struct ieee80211_vif *vif, int sig)
560e705c121SKalle Valo {
5616324c173SMordechay Goodstein struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
562e705c121SKalle Valo struct iwl_mvm *mvm = mvmvif->mvm;
563e705c121SKalle Valo int thold = vif->bss_conf.cqm_rssi_thold;
564e705c121SKalle Valo int hyst = vif->bss_conf.cqm_rssi_hyst;
565e705c121SKalle Valo int last_event;
566e705c121SKalle Valo
567e705c121SKalle Valo if (sig == 0) {
568e705c121SKalle Valo IWL_DEBUG_RX(mvm, "RSSI is 0 - skip signal based decision\n");
569e705c121SKalle Valo return;
570e705c121SKalle Valo }
571e705c121SKalle Valo
572e705c121SKalle Valo mvmvif->bf_data.ave_beacon_signal = sig;
573e705c121SKalle Valo
574e705c121SKalle Valo /* BT Coex */
575e705c121SKalle Valo if (mvmvif->bf_data.bt_coex_min_thold !=
576e705c121SKalle Valo mvmvif->bf_data.bt_coex_max_thold) {
577e705c121SKalle Valo last_event = mvmvif->bf_data.last_bt_coex_event;
578e705c121SKalle Valo if (sig > mvmvif->bf_data.bt_coex_max_thold &&
579e705c121SKalle Valo (last_event <= mvmvif->bf_data.bt_coex_min_thold ||
580e705c121SKalle Valo last_event == 0)) {
581e705c121SKalle Valo mvmvif->bf_data.last_bt_coex_event = sig;
582e705c121SKalle Valo IWL_DEBUG_RX(mvm, "cqm_iterator bt coex high %d\n",
583e705c121SKalle Valo sig);
584e705c121SKalle Valo iwl_mvm_bt_rssi_event(mvm, vif, RSSI_EVENT_HIGH);
585e705c121SKalle Valo } else if (sig < mvmvif->bf_data.bt_coex_min_thold &&
586e705c121SKalle Valo (last_event >= mvmvif->bf_data.bt_coex_max_thold ||
587e705c121SKalle Valo last_event == 0)) {
588e705c121SKalle Valo mvmvif->bf_data.last_bt_coex_event = sig;
589e705c121SKalle Valo IWL_DEBUG_RX(mvm, "cqm_iterator bt coex low %d\n",
590e705c121SKalle Valo sig);
591e705c121SKalle Valo iwl_mvm_bt_rssi_event(mvm, vif, RSSI_EVENT_LOW);
592e705c121SKalle Valo }
593e705c121SKalle Valo }
594e705c121SKalle Valo
595e705c121SKalle Valo if (!(vif->driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI))
596e705c121SKalle Valo return;
597e705c121SKalle Valo
598e705c121SKalle Valo /* CQM Notification */
599e705c121SKalle Valo last_event = mvmvif->bf_data.last_cqm_event;
600e705c121SKalle Valo if (thold && sig < thold && (last_event == 0 ||
601e705c121SKalle Valo sig < last_event - hyst)) {
602e705c121SKalle Valo mvmvif->bf_data.last_cqm_event = sig;
603e705c121SKalle Valo IWL_DEBUG_RX(mvm, "cqm_iterator cqm low %d\n",
604769f07d8SAndrzej Zaborowski sig);
605e705c121SKalle Valo ieee80211_cqm_rssi_notify(
606e705c121SKalle Valo vif,
607e705c121SKalle Valo NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
608e705c121SKalle Valo sig,
609e705c121SKalle Valo GFP_KERNEL);
610e705c121SKalle Valo } else if (sig > thold &&
611e705c121SKalle Valo (last_event == 0 || sig > last_event + hyst)) {
612e705c121SKalle Valo mvmvif->bf_data.last_cqm_event = sig;
613e705c121SKalle Valo IWL_DEBUG_RX(mvm, "cqm_iterator cqm high %d\n",
614769f07d8SAndrzej Zaborowski sig);
615e705c121SKalle Valo ieee80211_cqm_rssi_notify(
616e705c121SKalle Valo vif,
617e705c121SKalle Valo NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
618e705c121SKalle Valo sig,
6196324c173SMordechay Goodstein GFP_KERNEL);
6206324c173SMordechay Goodstein }
6216324c173SMordechay Goodstein }
6226324c173SMordechay Goodstein
iwl_mvm_stat_iterator(void * _data,u8 * mac,struct ieee80211_vif * vif)6236324c173SMordechay Goodstein static void iwl_mvm_stat_iterator(void *_data, u8 *mac,
6246324c173SMordechay Goodstein struct ieee80211_vif *vif)
6256324c173SMordechay Goodstein {
6266324c173SMordechay Goodstein struct iwl_mvm_stat_data *data = _data;
6276324c173SMordechay Goodstein int sig = -data->beacon_filter_average_energy;
6286324c173SMordechay Goodstein u16 id = le32_to_cpu(data->mac_id);
6296324c173SMordechay Goodstein struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
6306324c173SMordechay Goodstein u16 vif_id = mvmvif->id;
6316324c173SMordechay Goodstein
632650cadb7SGregory Greenman /* This doesn't need the MAC ID check since it's not taking the
6336324c173SMordechay Goodstein * data copied into the "data" struct, but rather the data from
634650cadb7SGregory Greenman * the notification directly.
6356324c173SMordechay Goodstein */
6366324c173SMordechay Goodstein mvmvif->deflink.beacon_stats.num_beacons =
6376324c173SMordechay Goodstein le32_to_cpu(data->beacon_counter[vif_id]);
6386324c173SMordechay Goodstein mvmvif->deflink.beacon_stats.avg_signal =
6396324c173SMordechay Goodstein -data->beacon_average_energy[vif_id];
6406324c173SMordechay Goodstein
6416324c173SMordechay Goodstein if (mvmvif->id != id)
6426324c173SMordechay Goodstein return;
6436324c173SMordechay Goodstein
6446324c173SMordechay Goodstein if (vif->type != NL80211_IFTYPE_STATION)
6456324c173SMordechay Goodstein return;
6466324c173SMordechay Goodstein
647650cadb7SGregory Greenman /* make sure that beacon statistics don't go backwards with TCM
648650cadb7SGregory Greenman * request to clear statistics
6496324c173SMordechay Goodstein */
6506324c173SMordechay Goodstein if (le32_to_cpu(data->flags) & IWL_STATISTICS_REPLY_FLG_CLEAR)
6516324c173SMordechay Goodstein mvmvif->deflink.beacon_stats.accu_num_beacons +=
6526324c173SMordechay Goodstein mvmvif->deflink.beacon_stats.num_beacons;
6536324c173SMordechay Goodstein
6546324c173SMordechay Goodstein iwl_mvm_update_vif_sig(vif, sig);
6556324c173SMordechay Goodstein }
6566324c173SMordechay Goodstein
iwl_mvm_stat_iterator_all_macs(void * _data,u8 * mac,struct ieee80211_vif * vif)6576324c173SMordechay Goodstein static void iwl_mvm_stat_iterator_all_macs(void *_data, u8 *mac,
6586324c173SMordechay Goodstein struct ieee80211_vif *vif)
6596324c173SMordechay Goodstein {
6606324c173SMordechay Goodstein struct iwl_mvm_stat_data_all_macs *data = _data;
6616324c173SMordechay Goodstein struct iwl_statistics_ntfy_per_mac *mac_stats;
662f1cbb0a8SDan Carpenter int sig;
6636324c173SMordechay Goodstein struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
6646324c173SMordechay Goodstein u16 vif_id = mvmvif->id;
6656324c173SMordechay Goodstein
6666324c173SMordechay Goodstein if (WARN_ONCE(vif_id >= MAC_INDEX_AUX, "invalid vif id: %d", vif_id))
6676324c173SMordechay Goodstein return;
6686324c173SMordechay Goodstein
6696324c173SMordechay Goodstein if (vif->type != NL80211_IFTYPE_STATION)
670650cadb7SGregory Greenman return;
6716324c173SMordechay Goodstein
672650cadb7SGregory Greenman mac_stats = &data->per_mac_stats[vif_id];
6736324c173SMordechay Goodstein
6746324c173SMordechay Goodstein mvmvif->deflink.beacon_stats.num_beacons =
6756324c173SMordechay Goodstein le32_to_cpu(mac_stats->beacon_counter);
6766324c173SMordechay Goodstein mvmvif->deflink.beacon_stats.avg_signal =
6776324c173SMordechay Goodstein -le32_to_cpu(mac_stats->beacon_average_energy);
6786324c173SMordechay Goodstein
679650cadb7SGregory Greenman /* make sure that beacon statistics don't go backwards with TCM
680650cadb7SGregory Greenman * request to clear statistics
6816324c173SMordechay Goodstein */
6826324c173SMordechay Goodstein if (le32_to_cpu(data->flags) & IWL_STATISTICS_REPLY_FLG_CLEAR)
6836324c173SMordechay Goodstein mvmvif->deflink.beacon_stats.accu_num_beacons +=
6846324c173SMordechay Goodstein mvmvif->deflink.beacon_stats.num_beacons;
6856324c173SMordechay Goodstein
686e705c121SKalle Valo sig = -le32_to_cpu(mac_stats->beacon_filter_average_energy);
687e705c121SKalle Valo iwl_mvm_update_vif_sig(vif, sig);
688e705c121SKalle Valo }
689e705c121SKalle Valo
690e705c121SKalle Valo static inline void
iwl_mvm_rx_stats_check_trigger(struct iwl_mvm * mvm,struct iwl_rx_packet * pkt)691e705c121SKalle Valo iwl_mvm_rx_stats_check_trigger(struct iwl_mvm *mvm, struct iwl_rx_packet *pkt)
692e705c121SKalle Valo {
6936c042d75SSara Sharon struct iwl_fw_dbg_trigger_tlv *trig;
6946c042d75SSara Sharon struct iwl_fw_dbg_trigger_stats *trig_stats;
695e705c121SKalle Valo u32 trig_offset, trig_thold;
696e705c121SKalle Valo
697e705c121SKalle Valo trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, NULL, FW_DBG_TRIGGER_STATS);
698e705c121SKalle Valo if (!trig)
699e705c121SKalle Valo return;
700e705c121SKalle Valo
701e705c121SKalle Valo trig_stats = (void *)trig->data;
702e705c121SKalle Valo
703e705c121SKalle Valo trig_offset = le32_to_cpu(trig_stats->stop_offset);
704e705c121SKalle Valo trig_thold = le32_to_cpu(trig_stats->stop_threshold);
705e705c121SKalle Valo
706e705c121SKalle Valo if (WARN_ON_ONCE(trig_offset >= iwl_rx_packet_payload_len(pkt)))
707e705c121SKalle Valo return;
7087174beb6SJohannes Berg
709e705c121SKalle Valo if (le32_to_cpup((__le32 *) (pkt->data + trig_offset)) < trig_thold)
710e705c121SKalle Valo return;
711c6bae216SMordechay Goodstein
712c6bae216SMordechay Goodstein iwl_fw_dbg_collect_trig(&mvm->fwrt, trig, NULL);
713853f4954SMordechay Goodstein }
714c6bae216SMordechay Goodstein
iwl_mvm_stats_energy_iter(void * _data,struct ieee80211_sta * sta)715c6bae216SMordechay Goodstein static void iwl_mvm_stats_energy_iter(void *_data,
716c8ee33e1SGregory Greenman struct ieee80211_sta *sta)
717853f4954SMordechay Goodstein {
718c6bae216SMordechay Goodstein struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
719c6bae216SMordechay Goodstein u8 *energy = _data;
720853f4954SMordechay Goodstein u32 sta_id = mvmsta->deflink.sta_id;
721853f4954SMordechay Goodstein
722c6bae216SMordechay Goodstein if (WARN_ONCE(sta_id >= IWL_MVM_STATION_COUNT_MAX, "sta_id %d >= %d",
723c8ee33e1SGregory Greenman sta_id, IWL_MVM_STATION_COUNT_MAX))
724853f4954SMordechay Goodstein return;
725853f4954SMordechay Goodstein
726853f4954SMordechay Goodstein if (energy[sta_id])
727853f4954SMordechay Goodstein mvmsta->deflink.avg_energy = energy[sta_id];
728853f4954SMordechay Goodstein
729853f4954SMordechay Goodstein }
730853f4954SMordechay Goodstein
731853f4954SMordechay Goodstein static void
iwl_mvm_update_tcm_from_stats(struct iwl_mvm * mvm,__le32 * air_time_le,__le32 * rx_bytes_le)732853f4954SMordechay Goodstein iwl_mvm_update_tcm_from_stats(struct iwl_mvm *mvm, __le32 *air_time_le,
733853f4954SMordechay Goodstein __le32 *rx_bytes_le)
734853f4954SMordechay Goodstein {
735853f4954SMordechay Goodstein int i;
736853f4954SMordechay Goodstein
737853f4954SMordechay Goodstein spin_lock(&mvm->tcm.lock);
738853f4954SMordechay Goodstein for (i = 0; i < NUM_MAC_INDEX_DRIVER; i++) {
739853f4954SMordechay Goodstein struct iwl_mvm_tcm_mac *mdata = &mvm->tcm.data[i];
740853f4954SMordechay Goodstein u32 rx_bytes = le32_to_cpu(rx_bytes_le[i]);
741853f4954SMordechay Goodstein u32 airtime = le32_to_cpu(air_time_le[i]);
742853f4954SMordechay Goodstein
743853f4954SMordechay Goodstein mdata->rx.airtime += airtime;
744853f4954SMordechay Goodstein mdata->uapsd_nonagg_detect.rx_bytes += rx_bytes;
745853f4954SMordechay Goodstein if (airtime) {
746853f4954SMordechay Goodstein /* re-init every time to store rate from FW */
747853f4954SMordechay Goodstein ewma_rate_init(&mdata->uapsd_nonagg_detect.rate);
748853f4954SMordechay Goodstein ewma_rate_add(&mdata->uapsd_nonagg_detect.rate,
749853f4954SMordechay Goodstein rx_bytes * 8 / airtime);
750853f4954SMordechay Goodstein }
751853f4954SMordechay Goodstein }
7526324c173SMordechay Goodstein spin_unlock(&mvm->tcm.lock);
7536324c173SMordechay Goodstein }
7546324c173SMordechay Goodstein
7556324c173SMordechay Goodstein static void
iwl_mvm_stats_ver_15(struct iwl_mvm * mvm,struct iwl_statistics_operational_ntfy * stats)7566324c173SMordechay Goodstein iwl_mvm_stats_ver_15(struct iwl_mvm *mvm,
7576324c173SMordechay Goodstein struct iwl_statistics_operational_ntfy *stats)
7586324c173SMordechay Goodstein {
7596324c173SMordechay Goodstein struct iwl_mvm_stat_data_all_macs data = {
7606324c173SMordechay Goodstein .mvm = mvm,
7616324c173SMordechay Goodstein .flags = stats->flags,
7626324c173SMordechay Goodstein .per_mac_stats = stats->per_mac_stats,
7636324c173SMordechay Goodstein };
7646324c173SMordechay Goodstein
7656324c173SMordechay Goodstein ieee80211_iterate_active_interfaces(mvm->hw,
7666324c173SMordechay Goodstein IEEE80211_IFACE_ITER_NORMAL,
7676324c173SMordechay Goodstein iwl_mvm_stat_iterator_all_macs,
7686324c173SMordechay Goodstein &data);
7696324c173SMordechay Goodstein }
770853f4954SMordechay Goodstein
771853f4954SMordechay Goodstein static void
iwl_mvm_stats_ver_14(struct iwl_mvm * mvm,struct iwl_statistics_operational_ntfy_ver_14 * stats)772853f4954SMordechay Goodstein iwl_mvm_stats_ver_14(struct iwl_mvm *mvm,
773853f4954SMordechay Goodstein struct iwl_statistics_operational_ntfy_ver_14 *stats)
7746324c173SMordechay Goodstein {
775853f4954SMordechay Goodstein struct iwl_mvm_stat_data data = {
776853f4954SMordechay Goodstein .mvm = mvm,
777853f4954SMordechay Goodstein };
778853f4954SMordechay Goodstein
779853f4954SMordechay Goodstein u8 beacon_average_energy[MAC_INDEX_AUX];
780853f4954SMordechay Goodstein __le32 flags;
781853f4954SMordechay Goodstein int i;
782853f4954SMordechay Goodstein
783853f4954SMordechay Goodstein flags = stats->flags;
784853f4954SMordechay Goodstein
785853f4954SMordechay Goodstein data.mac_id = stats->mac_id;
7866324c173SMordechay Goodstein data.beacon_filter_average_energy =
787853f4954SMordechay Goodstein le32_to_cpu(stats->beacon_filter_average_energy);
788853f4954SMordechay Goodstein data.flags = flags;
789853f4954SMordechay Goodstein data.beacon_counter = stats->beacon_counter;
790853f4954SMordechay Goodstein
791853f4954SMordechay Goodstein for (i = 0; i < ARRAY_SIZE(beacon_average_energy); i++)
792853f4954SMordechay Goodstein beacon_average_energy[i] =
793853f4954SMordechay Goodstein le32_to_cpu(stats->beacon_average_energy[i]);
794853f4954SMordechay Goodstein
795853f4954SMordechay Goodstein data.beacon_average_energy = beacon_average_energy;
796853f4954SMordechay Goodstein
7976324c173SMordechay Goodstein ieee80211_iterate_active_interfaces(mvm->hw,
7986324c173SMordechay Goodstein IEEE80211_IFACE_ITER_NORMAL,
7996324c173SMordechay Goodstein iwl_mvm_stat_iterator,
8006324c173SMordechay Goodstein &data);
8016324c173SMordechay Goodstein }
8026324c173SMordechay Goodstein
iwl_mvm_verify_stats_len(struct iwl_mvm * mvm,struct iwl_rx_packet * pkt,u32 expected_size)8036324c173SMordechay Goodstein static bool iwl_mvm_verify_stats_len(struct iwl_mvm *mvm,
8046324c173SMordechay Goodstein struct iwl_rx_packet *pkt,
8056324c173SMordechay Goodstein u32 expected_size)
8066324c173SMordechay Goodstein {
8076324c173SMordechay Goodstein struct iwl_statistics_ntfy_hdr *hdr;
8086324c173SMordechay Goodstein
8096324c173SMordechay Goodstein if (WARN_ONCE(iwl_rx_packet_payload_len(pkt) < expected_size,
8106324c173SMordechay Goodstein "received invalid statistics size (%d)!, expected_size: %d\n",
8116324c173SMordechay Goodstein iwl_rx_packet_payload_len(pkt), expected_size))
8126324c173SMordechay Goodstein return false;
8136324c173SMordechay Goodstein
8146324c173SMordechay Goodstein hdr = (void *)&pkt->data;
8156324c173SMordechay Goodstein
8166324c173SMordechay Goodstein if (WARN_ONCE((hdr->type & IWL_STATISTICS_TYPE_MSK) != FW_STATISTICS_OPERATIONAL ||
8176324c173SMordechay Goodstein hdr->version !=
8186324c173SMordechay Goodstein iwl_fw_lookup_notif_ver(mvm->fw, LEGACY_GROUP, STATISTICS_NOTIFICATION, 0),
8196324c173SMordechay Goodstein "received unsupported hdr type %d, version %d\n",
8206324c173SMordechay Goodstein hdr->type, hdr->version))
8216324c173SMordechay Goodstein return false;
8226324c173SMordechay Goodstein
8236324c173SMordechay Goodstein if (WARN_ONCE(le16_to_cpu(hdr->size) != expected_size,
8246324c173SMordechay Goodstein "received invalid statistics size in header (%d)!, expected_size: %d\n",
8256324c173SMordechay Goodstein le16_to_cpu(hdr->size), expected_size))
8266324c173SMordechay Goodstein return false;
8276324c173SMordechay Goodstein
8286324c173SMordechay Goodstein return true;
8296324c173SMordechay Goodstein }
8306324c173SMordechay Goodstein
8316324c173SMordechay Goodstein static void
iwl_mvm_handle_rx_statistics_tlv(struct iwl_mvm * mvm,struct iwl_rx_packet * pkt)8326324c173SMordechay Goodstein iwl_mvm_handle_rx_statistics_tlv(struct iwl_mvm *mvm,
8336324c173SMordechay Goodstein struct iwl_rx_packet *pkt)
8346324c173SMordechay Goodstein {
8356324c173SMordechay Goodstein u8 average_energy[IWL_MVM_STATION_COUNT_MAX];
8366324c173SMordechay Goodstein __le32 air_time[MAC_INDEX_AUX];
8376324c173SMordechay Goodstein __le32 rx_bytes[MAC_INDEX_AUX];
8386324c173SMordechay Goodstein __le32 flags = 0;
8396324c173SMordechay Goodstein int i;
8406324c173SMordechay Goodstein u32 notif_ver = iwl_fw_lookup_notif_ver(mvm->fw, LEGACY_GROUP,
8416324c173SMordechay Goodstein STATISTICS_NOTIFICATION, 0);
8426324c173SMordechay Goodstein
8436324c173SMordechay Goodstein if (WARN_ONCE(notif_ver > 15,
8446324c173SMordechay Goodstein "invalid statistics version id: %d\n", notif_ver))
8456324c173SMordechay Goodstein return;
8466324c173SMordechay Goodstein
8476324c173SMordechay Goodstein if (notif_ver == 14) {
8486324c173SMordechay Goodstein struct iwl_statistics_operational_ntfy_ver_14 *stats =
8496324c173SMordechay Goodstein (void *)pkt->data;
8506324c173SMordechay Goodstein
8516324c173SMordechay Goodstein if (!iwl_mvm_verify_stats_len(mvm, pkt, sizeof(*stats)))
8526324c173SMordechay Goodstein return;
8536324c173SMordechay Goodstein
8546324c173SMordechay Goodstein iwl_mvm_stats_ver_14(mvm, stats);
8556324c173SMordechay Goodstein
8566324c173SMordechay Goodstein flags = stats->flags;
8576324c173SMordechay Goodstein mvm->radio_stats.rx_time = le64_to_cpu(stats->rx_time);
858853f4954SMordechay Goodstein mvm->radio_stats.tx_time = le64_to_cpu(stats->tx_time);
859853f4954SMordechay Goodstein mvm->radio_stats.on_time_rf = le64_to_cpu(stats->on_time_rf);
860853f4954SMordechay Goodstein mvm->radio_stats.on_time_scan =
8616324c173SMordechay Goodstein le64_to_cpu(stats->on_time_scan);
8626324c173SMordechay Goodstein
8636324c173SMordechay Goodstein for (i = 0; i < ARRAY_SIZE(average_energy); i++)
8646324c173SMordechay Goodstein average_energy[i] = le32_to_cpu(stats->average_energy[i]);
8656324c173SMordechay Goodstein
8666324c173SMordechay Goodstein for (i = 0; i < ARRAY_SIZE(air_time); i++) {
8676324c173SMordechay Goodstein air_time[i] = stats->air_time[i];
8686324c173SMordechay Goodstein rx_bytes[i] = stats->rx_bytes[i];
8696324c173SMordechay Goodstein }
8706324c173SMordechay Goodstein }
8716324c173SMordechay Goodstein
8726324c173SMordechay Goodstein if (notif_ver == 15) {
8736324c173SMordechay Goodstein struct iwl_statistics_operational_ntfy *stats =
8746324c173SMordechay Goodstein (void *)pkt->data;
8756324c173SMordechay Goodstein
8766324c173SMordechay Goodstein if (!iwl_mvm_verify_stats_len(mvm, pkt, sizeof(*stats)))
8776324c173SMordechay Goodstein return;
8786324c173SMordechay Goodstein
8796324c173SMordechay Goodstein iwl_mvm_stats_ver_15(mvm, stats);
8806324c173SMordechay Goodstein
8816324c173SMordechay Goodstein flags = stats->flags;
8826324c173SMordechay Goodstein mvm->radio_stats.rx_time = le64_to_cpu(stats->rx_time);
8836324c173SMordechay Goodstein mvm->radio_stats.tx_time = le64_to_cpu(stats->tx_time);
8846324c173SMordechay Goodstein mvm->radio_stats.on_time_rf = le64_to_cpu(stats->on_time_rf);
8856324c173SMordechay Goodstein mvm->radio_stats.on_time_scan =
8866324c173SMordechay Goodstein le64_to_cpu(stats->on_time_scan);
8876324c173SMordechay Goodstein
8886324c173SMordechay Goodstein for (i = 0; i < ARRAY_SIZE(average_energy); i++)
8896324c173SMordechay Goodstein average_energy[i] =
8906324c173SMordechay Goodstein le32_to_cpu(stats->per_sta_stats[i].average_energy);
8916324c173SMordechay Goodstein
8926324c173SMordechay Goodstein for (i = 0; i < ARRAY_SIZE(air_time); i++) {
8936324c173SMordechay Goodstein air_time[i] = stats->per_mac_stats[i].air_time;
8946324c173SMordechay Goodstein rx_bytes[i] = stats->per_mac_stats[i].rx_bytes;
8956324c173SMordechay Goodstein }
896c6bae216SMordechay Goodstein }
897c6bae216SMordechay Goodstein
898853f4954SMordechay Goodstein iwl_mvm_rx_stats_check_trigger(mvm, pkt);
899853f4954SMordechay Goodstein
900853f4954SMordechay Goodstein ieee80211_iterate_stations_atomic(mvm->hw, iwl_mvm_stats_energy_iter,
901853f4954SMordechay Goodstein average_energy);
902853f4954SMordechay Goodstein /*
903853f4954SMordechay Goodstein * Don't update in case the statistics are not cleared, since
9046324c173SMordechay Goodstein * we will end up counting twice the same airtime, once in TCM
905853f4954SMordechay Goodstein * request and once in statistics notification.
906853f4954SMordechay Goodstein */
907e705c121SKalle Valo if (le32_to_cpu(flags) & IWL_STATISTICS_REPLY_FLG_CLEAR)
908e705c121SKalle Valo iwl_mvm_update_tcm_from_stats(mvm, air_time, rx_bytes);
909e705c121SKalle Valo }
910e705c121SKalle Valo
iwl_mvm_handle_rx_statistics(struct iwl_mvm * mvm,struct iwl_rx_packet * pkt)911e705c121SKalle Valo void iwl_mvm_handle_rx_statistics(struct iwl_mvm *mvm,
912e705c121SKalle Valo struct iwl_rx_packet *pkt)
913853f4954SMordechay Goodstein {
9140e7ac018SSara Sharon struct iwl_mvm_stat_data data = {
915cfbeb598SGoodstein, Mordechay .mvm = mvm,
916853f4954SMordechay Goodstein };
917853f4954SMordechay Goodstein __le32 *bytes, *air_time, flags;
9186324c173SMordechay Goodstein int expected_size;
9196324c173SMordechay Goodstein u8 *energy;
920853f4954SMordechay Goodstein
9210e7ac018SSara Sharon /* From ver 14 and up we use TLV statistics format */
922678d9b6dSLiad Kaufman if (iwl_fw_lookup_notif_ver(mvm->fw, LEGACY_GROUP,
923678d9b6dSLiad Kaufman STATISTICS_NOTIFICATION, 0) >= 14)
9240e7ac018SSara Sharon return iwl_mvm_handle_rx_statistics_tlv(mvm, pkt);
9250e7ac018SSara Sharon
9260e7ac018SSara Sharon if (!iwl_mvm_has_new_rx_stats_api(mvm)) {
927678d9b6dSLiad Kaufman if (iwl_mvm_has_new_rx_api(mvm))
928606b9ab6SEmmanuel Grumbach expected_size = sizeof(struct iwl_notif_statistics_v11);
929678d9b6dSLiad Kaufman else
930e705c121SKalle Valo expected_size = sizeof(struct iwl_notif_statistics_v10);
93142fa5e0eSJohannes Berg } else {
93242fa5e0eSJohannes Berg expected_size = sizeof(struct iwl_notif_statistics);
93342fa5e0eSJohannes Berg }
9346a90f85aSLuca Coelho
935e705c121SKalle Valo if (WARN_ONCE(iwl_rx_packet_payload_len(pkt) != expected_size,
936678d9b6dSLiad Kaufman "received invalid statistics size (%d)!\n",
937678d9b6dSLiad Kaufman iwl_rx_packet_payload_len(pkt)))
938678d9b6dSLiad Kaufman return;
939e705c121SKalle Valo
940e705c121SKalle Valo if (!iwl_mvm_has_new_rx_stats_api(mvm)) {
9410e7ac018SSara Sharon struct iwl_notif_statistics_v11 *stats = (void *)&pkt->data;
942e705c121SKalle Valo
943678d9b6dSLiad Kaufman data.mac_id = stats->rx.general.mac_id;
944e705c121SKalle Valo data.beacon_filter_average_energy =
945678d9b6dSLiad Kaufman stats->general.common.beacon_filter_average_energy;
946678d9b6dSLiad Kaufman
947678d9b6dSLiad Kaufman mvm->rx_stats_v3 = stats->rx;
948678d9b6dSLiad Kaufman
949e705c121SKalle Valo mvm->radio_stats.rx_time =
9500e7ac018SSara Sharon le64_to_cpu(stats->general.common.rx_time);
951e705c121SKalle Valo mvm->radio_stats.tx_time =
9520e7ac018SSara Sharon le64_to_cpu(stats->general.common.tx_time);
953e705c121SKalle Valo mvm->radio_stats.on_time_rf =
954853f4954SMordechay Goodstein le64_to_cpu(stats->general.common.on_time_rf);
955853f4954SMordechay Goodstein mvm->radio_stats.on_time_scan =
956853f4954SMordechay Goodstein le64_to_cpu(stats->general.common.on_time_scan);
957678d9b6dSLiad Kaufman
958678d9b6dSLiad Kaufman data.beacon_counter = stats->general.beacon_counter;
959606b9ab6SEmmanuel Grumbach data.beacon_average_energy =
960678d9b6dSLiad Kaufman stats->general.beacon_average_energy;
961678d9b6dSLiad Kaufman flags = stats->flag;
962678d9b6dSLiad Kaufman } else {
963678d9b6dSLiad Kaufman struct iwl_notif_statistics *stats = (void *)&pkt->data;
964678d9b6dSLiad Kaufman
965678d9b6dSLiad Kaufman data.mac_id = stats->rx.general.mac_id;
966678d9b6dSLiad Kaufman data.beacon_filter_average_energy =
967678d9b6dSLiad Kaufman stats->general.common.beacon_filter_average_energy;
968678d9b6dSLiad Kaufman
969678d9b6dSLiad Kaufman mvm->rx_stats = stats->rx;
970678d9b6dSLiad Kaufman
971678d9b6dSLiad Kaufman mvm->radio_stats.rx_time =
972678d9b6dSLiad Kaufman le64_to_cpu(stats->general.common.rx_time);
973678d9b6dSLiad Kaufman mvm->radio_stats.tx_time =
974678d9b6dSLiad Kaufman le64_to_cpu(stats->general.common.tx_time);
975678d9b6dSLiad Kaufman mvm->radio_stats.on_time_rf =
976853f4954SMordechay Goodstein le64_to_cpu(stats->general.common.on_time_rf);
977853f4954SMordechay Goodstein mvm->radio_stats.on_time_scan =
978853f4954SMordechay Goodstein le64_to_cpu(stats->general.common.on_time_scan);
979678d9b6dSLiad Kaufman
980678d9b6dSLiad Kaufman data.beacon_counter = stats->general.beacon_counter;
981290d5e49SMordechay Goodstein data.beacon_average_energy =
982678d9b6dSLiad Kaufman stats->general.beacon_average_energy;
983cfbeb598SGoodstein, Mordechay flags = stats->flag;
984cfbeb598SGoodstein, Mordechay }
985cfbeb598SGoodstein, Mordechay data.flags = flags;
986cfbeb598SGoodstein, Mordechay
987cfbeb598SGoodstein, Mordechay iwl_mvm_rx_stats_check_trigger(mvm, pkt);
988cfbeb598SGoodstein, Mordechay
989cfbeb598SGoodstein, Mordechay ieee80211_iterate_active_interfaces(mvm->hw,
990cfbeb598SGoodstein, Mordechay IEEE80211_IFACE_ITER_NORMAL,
991cfbeb598SGoodstein, Mordechay iwl_mvm_stat_iterator,
992988b5968SSara Sharon &data);
993678d9b6dSLiad Kaufman
994678d9b6dSLiad Kaufman if (!iwl_mvm_has_new_rx_api(mvm))
9950e7ac018SSara Sharon return;
9960e7ac018SSara Sharon
997b0ffe455SJohannes Berg if (!iwl_mvm_has_new_rx_stats_api(mvm)) {
9980e7ac018SSara Sharon struct iwl_notif_statistics_v11 *v11 = (void *)&pkt->data;
9990e7ac018SSara Sharon
1000606b9ab6SEmmanuel Grumbach energy = (void *)&v11->load_stats.avg_energy;
1001678d9b6dSLiad Kaufman bytes = (void *)&v11->load_stats.byte_count;
10020e7ac018SSara Sharon air_time = (void *)&v11->load_stats.air_time;
1003b0ffe455SJohannes Berg } else {
10040e7ac018SSara Sharon struct iwl_notif_statistics *stats = (void *)&pkt->data;
10050e7ac018SSara Sharon
1006c6bae216SMordechay Goodstein energy = (void *)&stats->load_stats.avg_energy;
1007c6bae216SMordechay Goodstein bytes = (void *)&stats->load_stats.byte_count;
10087d9d0d56SLuca Coelho air_time = (void *)&stats->load_stats.air_time;
10097d9d0d56SLuca Coelho }
10107d9d0d56SLuca Coelho ieee80211_iterate_stations_atomic(mvm->hw, iwl_mvm_stats_energy_iter,
10117d9d0d56SLuca Coelho energy);
10127d9d0d56SLuca Coelho
10137d9d0d56SLuca Coelho /*
1014853f4954SMordechay Goodstein * Don't update in case the statistics are not cleared, since
1015853f4954SMordechay Goodstein * we will end up counting twice the same airtime, once in TCM
10167d9d0d56SLuca Coelho * request and once in statistics notification.
1017e705c121SKalle Valo */
1018e705c121SKalle Valo if (le32_to_cpu(flags) & IWL_STATISTICS_REPLY_FLG_CLEAR)
1019e705c121SKalle Valo iwl_mvm_update_tcm_from_stats(mvm, air_time, bytes);
1020e705c121SKalle Valo
1021e705c121SKalle Valo }
1022e705c121SKalle Valo
iwl_mvm_rx_statistics(struct iwl_mvm * mvm,struct iwl_rx_cmd_buffer * rxb)10233af512d6SSara Sharon void iwl_mvm_rx_statistics(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
10243af512d6SSara Sharon {
10253af512d6SSara Sharon iwl_mvm_handle_rx_statistics(mvm, rxb_addr(rxb));
10263af512d6SSara Sharon }
10273af512d6SSara Sharon
iwl_mvm_window_status_notif(struct iwl_mvm * mvm,struct iwl_rx_cmd_buffer * rxb)10283af512d6SSara Sharon void iwl_mvm_window_status_notif(struct iwl_mvm *mvm,
10293af512d6SSara Sharon struct iwl_rx_cmd_buffer *rxb)
10303af512d6SSara Sharon {
1031afc857bcSJohannes Berg struct iwl_rx_packet *pkt = rxb_addr(rxb);
1032afc857bcSJohannes Berg struct iwl_ba_window_status_notif *notif = (void *)pkt->data;
1033afc857bcSJohannes Berg int i;
1034afc857bcSJohannes Berg
10353af512d6SSara Sharon BUILD_BUG_ON(ARRAY_SIZE(notif->ra_tid) != BA_WINDOW_STREAMS_MAX);
10363af512d6SSara Sharon BUILD_BUG_ON(ARRAY_SIZE(notif->mpdu_rx_count) != BA_WINDOW_STREAMS_MAX);
10373af512d6SSara Sharon BUILD_BUG_ON(ARRAY_SIZE(notif->bitmap) != BA_WINDOW_STREAMS_MAX);
10383af512d6SSara Sharon BUILD_BUG_ON(ARRAY_SIZE(notif->start_seq_num) != BA_WINDOW_STREAMS_MAX);
10393af512d6SSara Sharon
10403af512d6SSara Sharon rcu_read_lock();
10413af512d6SSara Sharon for (i = 0; i < BA_WINDOW_STREAMS_MAX; i++) {
10423af512d6SSara Sharon struct ieee80211_sta *sta;
10433af512d6SSara Sharon u8 sta_id, tid;
10443af512d6SSara Sharon u64 bitmap;
10453af512d6SSara Sharon u32 ssn;
10463af512d6SSara Sharon u16 ratid;
10473af512d6SSara Sharon u16 received_mpdu;
10483af512d6SSara Sharon
10493af512d6SSara Sharon ratid = le16_to_cpu(notif->ra_tid[i]);
10503af512d6SSara Sharon /* check that this TID is valid */
10513af512d6SSara Sharon if (!(ratid & BA_WINDOW_STATUS_VALID_MSK))
10523af512d6SSara Sharon continue;
10533af512d6SSara Sharon
10543af512d6SSara Sharon received_mpdu = le16_to_cpu(notif->mpdu_rx_count[i]);
10553af512d6SSara Sharon if (received_mpdu == 0)
10563af512d6SSara Sharon continue;
10573af512d6SSara Sharon
10583af512d6SSara Sharon tid = ratid & BA_WINDOW_STATUS_TID_MSK;
10593af512d6SSara Sharon /* get the station */
10603af512d6SSara Sharon sta_id = (ratid & BA_WINDOW_STATUS_STA_ID_MSK)
10613af512d6SSara Sharon >> BA_WINDOW_STATUS_STA_ID_POS;
10623af512d6SSara Sharon sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
10633af512d6SSara Sharon if (IS_ERR_OR_NULL(sta))
10643af512d6SSara Sharon continue;
10653af512d6SSara Sharon bitmap = le64_to_cpu(notif->bitmap[i]);
10663af512d6SSara Sharon ssn = le32_to_cpu(notif->start_seq_num[i]);
10673af512d6SSara Sharon
10683af512d6SSara Sharon /* update mac80211 with the bitmap for the reordering buffer */
10693af512d6SSara Sharon ieee80211_mark_rx_ba_filtered_frames(sta, tid, ssn, bitmap,
1070 received_mpdu);
1071 }
1072 rcu_read_unlock();
1073 }
1074