1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /*
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
6 */
7 #include <asm/unaligned.h>
8 #include <linux/etherdevice.h>
9 #include <linux/skbuff.h>
10 #include "iwl-trans.h"
11 #include "mvm.h"
12 #include "fw-api.h"
13
14 /*
15 * iwl_mvm_rx_rx_phy_cmd - REPLY_RX_PHY_CMD handler
16 *
17 * Copies the phy information in mvm->last_phy_info, it will be used when the
18 * actual data will come from the fw in the next packet.
19 */
iwl_mvm_rx_rx_phy_cmd(struct iwl_mvm * mvm,struct iwl_rx_cmd_buffer * rxb)20 void iwl_mvm_rx_rx_phy_cmd(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
21 {
22 struct iwl_rx_packet *pkt = rxb_addr(rxb);
23 unsigned int pkt_len = iwl_rx_packet_payload_len(pkt);
24
25 if (unlikely(pkt_len < sizeof(mvm->last_phy_info)))
26 return;
27
28 memcpy(&mvm->last_phy_info, pkt->data, sizeof(mvm->last_phy_info));
29 mvm->ampdu_ref++;
30
31 #ifdef CONFIG_IWLWIFI_DEBUGFS
32 if (mvm->last_phy_info.phy_flags & cpu_to_le16(RX_RES_PHY_FLAGS_AGG)) {
33 spin_lock(&mvm->drv_stats_lock);
34 mvm->drv_rx_stats.ampdu_count++;
35 spin_unlock(&mvm->drv_stats_lock);
36 }
37 #endif
38 }
39
40 /*
41 * iwl_mvm_pass_packet_to_mac80211 - builds the packet for mac80211
42 *
43 * Adds the rxb to a new skb and give it to mac80211
44 */
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)45 static void iwl_mvm_pass_packet_to_mac80211(struct iwl_mvm *mvm,
46 struct ieee80211_sta *sta,
47 struct napi_struct *napi,
48 struct sk_buff *skb,
49 struct ieee80211_hdr *hdr, u16 len,
50 u8 crypt_len,
51 struct iwl_rx_cmd_buffer *rxb)
52 {
53 unsigned int hdrlen = ieee80211_hdrlen(hdr->frame_control);
54 unsigned int fraglen;
55
56 /*
57 * The 'hdrlen' (plus the 8 bytes for the SNAP and the crypt_len,
58 * but those are all multiples of 4 long) all goes away, but we
59 * want the *end* of it, which is going to be the start of the IP
60 * header, to be aligned when it gets pulled in.
61 * The beginning of the skb->data is aligned on at least a 4-byte
62 * boundary after allocation. Everything here is aligned at least
63 * on a 2-byte boundary so we can just take hdrlen & 3 and pad by
64 * the result.
65 */
66 skb_reserve(skb, hdrlen & 3);
67
68 /* If frame is small enough to fit in skb->head, pull it completely.
69 * If not, only pull ieee80211_hdr (including crypto if present, and
70 * an additional 8 bytes for SNAP/ethertype, see below) so that
71 * splice() or TCP coalesce are more efficient.
72 *
73 * Since, in addition, ieee80211_data_to_8023() always pull in at
74 * least 8 bytes (possibly more for mesh) we can do the same here
75 * to save the cost of doing it later. That still doesn't pull in
76 * the actual IP header since the typical case has a SNAP header.
77 * If the latter changes (there are efforts in the standards group
78 * to do so) we should revisit this and ieee80211_data_to_8023().
79 */
80 hdrlen = (len <= skb_tailroom(skb)) ? len : hdrlen + crypt_len + 8;
81
82 skb_put_data(skb, hdr, hdrlen);
83 fraglen = len - hdrlen;
84
85 if (fraglen) {
86 int offset = (u8 *)hdr + hdrlen -
87 (u8 *)rxb_addr(rxb) + rxb_offset(rxb);
88
89 skb_add_rx_frag(skb, 0, rxb_steal_page(rxb), offset,
90 fraglen, rxb->truesize);
91 }
92
93 ieee80211_rx_napi(mvm->hw, sta, skb, napi);
94 }
95
96 /*
97 * iwl_mvm_get_signal_strength - use new rx PHY INFO API
98 * values are reported by the fw as positive values - need to negate
99 * to obtain their dBM. Account for missing antennas by replacing 0
100 * values by -256dBm: practically 0 power and a non-feasible 8 bit value.
101 */
iwl_mvm_get_signal_strength(struct iwl_mvm * mvm,struct iwl_rx_phy_info * phy_info,struct ieee80211_rx_status * rx_status)102 static void iwl_mvm_get_signal_strength(struct iwl_mvm *mvm,
103 struct iwl_rx_phy_info *phy_info,
104 struct ieee80211_rx_status *rx_status)
105 {
106 int energy_a, energy_b, energy_c, max_energy;
107 u32 val;
108
109 val =
110 le32_to_cpu(phy_info->non_cfg_phy[IWL_RX_INFO_ENERGY_ANT_ABC_IDX]);
111 energy_a = (val & IWL_RX_INFO_ENERGY_ANT_A_MSK) >>
112 IWL_RX_INFO_ENERGY_ANT_A_POS;
113 energy_a = energy_a ? -energy_a : S8_MIN;
114 energy_b = (val & IWL_RX_INFO_ENERGY_ANT_B_MSK) >>
115 IWL_RX_INFO_ENERGY_ANT_B_POS;
116 energy_b = energy_b ? -energy_b : S8_MIN;
117 energy_c = (val & IWL_RX_INFO_ENERGY_ANT_C_MSK) >>
118 IWL_RX_INFO_ENERGY_ANT_C_POS;
119 energy_c = energy_c ? -energy_c : S8_MIN;
120 max_energy = max(energy_a, energy_b);
121 max_energy = max(max_energy, energy_c);
122
123 IWL_DEBUG_STATS(mvm, "energy In A %d B %d C %d , and max %d\n",
124 energy_a, energy_b, energy_c, max_energy);
125
126 rx_status->signal = max_energy;
127 rx_status->chains = (le16_to_cpu(phy_info->phy_flags) &
128 RX_RES_PHY_FLAGS_ANTENNA)
129 >> RX_RES_PHY_FLAGS_ANTENNA_POS;
130 rx_status->chain_signal[0] = energy_a;
131 rx_status->chain_signal[1] = energy_b;
132 rx_status->chain_signal[2] = energy_c;
133 }
134
135 /*
136 * iwl_mvm_set_mac80211_rx_flag - translate fw status to mac80211 format
137 * @mvm: the mvm object
138 * @hdr: 80211 header
139 * @stats: status in mac80211's format
140 * @rx_pkt_status: status coming from fw
141 *
142 * returns non 0 value if the packet should be dropped
143 */
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)144 static u32 iwl_mvm_set_mac80211_rx_flag(struct iwl_mvm *mvm,
145 struct ieee80211_hdr *hdr,
146 struct ieee80211_rx_status *stats,
147 u32 rx_pkt_status,
148 u8 *crypt_len)
149 {
150 if (!ieee80211_has_protected(hdr->frame_control) ||
151 (rx_pkt_status & RX_MPDU_RES_STATUS_SEC_ENC_MSK) ==
152 RX_MPDU_RES_STATUS_SEC_NO_ENC)
153 return 0;
154
155 /* packet was encrypted with unknown alg */
156 if ((rx_pkt_status & RX_MPDU_RES_STATUS_SEC_ENC_MSK) ==
157 RX_MPDU_RES_STATUS_SEC_ENC_ERR)
158 return 0;
159
160 switch (rx_pkt_status & RX_MPDU_RES_STATUS_SEC_ENC_MSK) {
161 case RX_MPDU_RES_STATUS_SEC_CCM_ENC:
162 /* alg is CCM: check MIC only */
163 if (!(rx_pkt_status & RX_MPDU_RES_STATUS_MIC_OK))
164 return -1;
165
166 stats->flag |= RX_FLAG_DECRYPTED;
167 *crypt_len = IEEE80211_CCMP_HDR_LEN;
168 return 0;
169
170 case RX_MPDU_RES_STATUS_SEC_TKIP_ENC:
171 /* Don't drop the frame and decrypt it in SW */
172 if (!fw_has_api(&mvm->fw->ucode_capa,
173 IWL_UCODE_TLV_API_DEPRECATE_TTAK) &&
174 !(rx_pkt_status & RX_MPDU_RES_STATUS_TTAK_OK))
175 return 0;
176 *crypt_len = IEEE80211_TKIP_IV_LEN;
177 fallthrough;
178
179 case RX_MPDU_RES_STATUS_SEC_WEP_ENC:
180 if (!(rx_pkt_status & RX_MPDU_RES_STATUS_ICV_OK))
181 return -1;
182
183 stats->flag |= RX_FLAG_DECRYPTED;
184 if ((rx_pkt_status & RX_MPDU_RES_STATUS_SEC_ENC_MSK) ==
185 RX_MPDU_RES_STATUS_SEC_WEP_ENC)
186 *crypt_len = IEEE80211_WEP_IV_LEN;
187 return 0;
188
189 case RX_MPDU_RES_STATUS_SEC_EXT_ENC:
190 if (!(rx_pkt_status & RX_MPDU_RES_STATUS_MIC_OK))
191 return -1;
192 stats->flag |= RX_FLAG_DECRYPTED;
193 return 0;
194
195 default:
196 /* Expected in monitor (not having the keys) */
197 if (!mvm->monitor_on)
198 IWL_ERR(mvm, "Unhandled alg: 0x%x\n", rx_pkt_status);
199 }
200
201 return 0;
202 }
203
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)204 static void iwl_mvm_rx_handle_tcm(struct iwl_mvm *mvm,
205 struct ieee80211_sta *sta,
206 struct ieee80211_hdr *hdr, u32 len,
207 struct iwl_rx_phy_info *phy_info,
208 u32 rate_n_flags)
209 {
210 struct iwl_mvm_sta *mvmsta;
211 struct iwl_mvm_tcm_mac *mdata;
212 int mac;
213 int ac = IEEE80211_AC_BE; /* treat non-QoS as BE */
214 struct iwl_mvm_vif *mvmvif;
215 /* expected throughput in 100Kbps, single stream, 20 MHz */
216 static const u8 thresh_tpt[] = {
217 9, 18, 30, 42, 60, 78, 90, 96, 120, 135,
218 };
219 u16 thr;
220
221 if (ieee80211_is_data_qos(hdr->frame_control))
222 ac = tid_to_mac80211_ac[ieee80211_get_tid(hdr)];
223
224 mvmsta = iwl_mvm_sta_from_mac80211(sta);
225 mac = mvmsta->mac_id_n_color & FW_CTXT_ID_MSK;
226
227 if (time_after(jiffies, mvm->tcm.ts + MVM_TCM_PERIOD))
228 schedule_delayed_work(&mvm->tcm.work, 0);
229 mdata = &mvm->tcm.data[mac];
230 mdata->rx.pkts[ac]++;
231
232 /* count the airtime only once for each ampdu */
233 if (mdata->rx.last_ampdu_ref != mvm->ampdu_ref) {
234 mdata->rx.last_ampdu_ref = mvm->ampdu_ref;
235 mdata->rx.airtime += le16_to_cpu(phy_info->frame_time);
236 }
237
238 if (!(rate_n_flags & (RATE_MCS_HT_MSK | RATE_MCS_VHT_MSK)))
239 return;
240
241 mvmvif = iwl_mvm_vif_from_mac80211(mvmsta->vif);
242
243 if (mdata->opened_rx_ba_sessions ||
244 mdata->uapsd_nonagg_detect.detected ||
245 (!mvmvif->queue_params[IEEE80211_AC_VO].uapsd &&
246 !mvmvif->queue_params[IEEE80211_AC_VI].uapsd &&
247 !mvmvif->queue_params[IEEE80211_AC_BE].uapsd &&
248 !mvmvif->queue_params[IEEE80211_AC_BK].uapsd) ||
249 mvmsta->sta_id != mvmvif->ap_sta_id)
250 return;
251
252 if (rate_n_flags & RATE_MCS_HT_MSK) {
253 thr = thresh_tpt[rate_n_flags & RATE_HT_MCS_RATE_CODE_MSK];
254 thr *= 1 + ((rate_n_flags & RATE_HT_MCS_NSS_MSK) >>
255 RATE_HT_MCS_NSS_POS);
256 } else {
257 if (WARN_ON((rate_n_flags & RATE_VHT_MCS_RATE_CODE_MSK) >=
258 ARRAY_SIZE(thresh_tpt)))
259 return;
260 thr = thresh_tpt[rate_n_flags & RATE_VHT_MCS_RATE_CODE_MSK];
261 thr *= 1 + ((rate_n_flags & RATE_VHT_MCS_NSS_MSK) >>
262 RATE_VHT_MCS_NSS_POS);
263 }
264
265 thr <<= ((rate_n_flags & RATE_MCS_CHAN_WIDTH_MSK) >>
266 RATE_MCS_CHAN_WIDTH_POS);
267
268 mdata->uapsd_nonagg_detect.rx_bytes += len;
269 ewma_rate_add(&mdata->uapsd_nonagg_detect.rate, thr);
270 }
271
iwl_mvm_rx_csum(struct ieee80211_sta * sta,struct sk_buff * skb,u32 status)272 static void iwl_mvm_rx_csum(struct ieee80211_sta *sta,
273 struct sk_buff *skb,
274 u32 status)
275 {
276 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
277 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(mvmsta->vif);
278
279 if (mvmvif->features & NETIF_F_RXCSUM &&
280 status & RX_MPDU_RES_STATUS_CSUM_DONE &&
281 status & RX_MPDU_RES_STATUS_CSUM_OK)
282 skb->ip_summed = CHECKSUM_UNNECESSARY;
283 }
284
285 /*
286 * iwl_mvm_rx_rx_mpdu - REPLY_RX_MPDU_CMD handler
287 *
288 * Handles the actual data of the Rx packet from the fw
289 */
iwl_mvm_rx_rx_mpdu(struct iwl_mvm * mvm,struct napi_struct * napi,struct iwl_rx_cmd_buffer * rxb)290 void iwl_mvm_rx_rx_mpdu(struct iwl_mvm *mvm, struct napi_struct *napi,
291 struct iwl_rx_cmd_buffer *rxb)
292 {
293 struct ieee80211_hdr *hdr;
294 struct ieee80211_rx_status *rx_status;
295 struct iwl_rx_packet *pkt = rxb_addr(rxb);
296 struct iwl_rx_phy_info *phy_info;
297 struct iwl_rx_mpdu_res_start *rx_res;
298 struct ieee80211_sta *sta = NULL;
299 struct sk_buff *skb;
300 u32 len, pkt_len = iwl_rx_packet_payload_len(pkt);
301 u32 rate_n_flags;
302 u32 rx_pkt_status;
303 u8 crypt_len = 0;
304
305 if (unlikely(pkt_len < sizeof(*rx_res))) {
306 IWL_DEBUG_DROP(mvm, "Bad REPLY_RX_MPDU_CMD size\n");
307 return;
308 }
309
310 phy_info = &mvm->last_phy_info;
311 rx_res = (struct iwl_rx_mpdu_res_start *)pkt->data;
312 hdr = (struct ieee80211_hdr *)(pkt->data + sizeof(*rx_res));
313 len = le16_to_cpu(rx_res->byte_count);
314
315 if (unlikely(len + sizeof(*rx_res) + sizeof(__le32) > pkt_len)) {
316 IWL_DEBUG_DROP(mvm, "FW lied about packet len\n");
317 return;
318 }
319
320 rx_pkt_status = get_unaligned_le32((__le32 *)
321 (pkt->data + sizeof(*rx_res) + len));
322
323 /* Dont use dev_alloc_skb(), we'll have enough headroom once
324 * ieee80211_hdr pulled.
325 */
326 skb = alloc_skb(128, GFP_ATOMIC);
327 if (!skb) {
328 IWL_ERR(mvm, "alloc_skb failed\n");
329 return;
330 }
331
332 rx_status = IEEE80211_SKB_RXCB(skb);
333
334 /*
335 * drop the packet if it has failed being decrypted by HW
336 */
337 if (iwl_mvm_set_mac80211_rx_flag(mvm, hdr, rx_status, rx_pkt_status,
338 &crypt_len)) {
339 IWL_DEBUG_DROP(mvm, "Bad decryption results 0x%08x\n",
340 rx_pkt_status);
341 kfree_skb(skb);
342 return;
343 }
344
345 /*
346 * Keep packets with CRC errors (and with overrun) for monitor mode
347 * (otherwise the firmware discards them) but mark them as bad.
348 */
349 if (!(rx_pkt_status & RX_MPDU_RES_STATUS_CRC_OK) ||
350 !(rx_pkt_status & RX_MPDU_RES_STATUS_OVERRUN_OK)) {
351 IWL_DEBUG_RX(mvm, "Bad CRC or FIFO: 0x%08X.\n", rx_pkt_status);
352 rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
353 }
354
355 /* This will be used in several places later */
356 rate_n_flags = le32_to_cpu(phy_info->rate_n_flags);
357
358 /* rx_status carries information about the packet to mac80211 */
359 rx_status->mactime = le64_to_cpu(phy_info->timestamp);
360 rx_status->device_timestamp = le32_to_cpu(phy_info->system_timestamp);
361 rx_status->band =
362 (phy_info->phy_flags & cpu_to_le16(RX_RES_PHY_FLAGS_BAND_24)) ?
363 NL80211_BAND_2GHZ : NL80211_BAND_5GHZ;
364 rx_status->freq =
365 ieee80211_channel_to_frequency(le16_to_cpu(phy_info->channel),
366 rx_status->band);
367
368 /* TSF as indicated by the firmware is at INA time */
369 rx_status->flag |= RX_FLAG_MACTIME_PLCP_START;
370
371 iwl_mvm_get_signal_strength(mvm, phy_info, rx_status);
372
373 IWL_DEBUG_STATS_LIMIT(mvm, "Rssi %d, TSF %llu\n", rx_status->signal,
374 (unsigned long long)rx_status->mactime);
375
376 rcu_read_lock();
377 if (rx_pkt_status & RX_MPDU_RES_STATUS_SRC_STA_FOUND) {
378 u32 id = rx_pkt_status & RX_MPDU_RES_STATUS_STA_ID_MSK;
379
380 id >>= RX_MDPU_RES_STATUS_STA_ID_SHIFT;
381
382 if (!WARN_ON_ONCE(id >= mvm->fw->ucode_capa.num_stations)) {
383 sta = rcu_dereference(mvm->fw_id_to_mac_id[id]);
384 if (IS_ERR(sta))
385 sta = NULL;
386 }
387 } else if (!is_multicast_ether_addr(hdr->addr2)) {
388 /* This is fine since we prevent two stations with the same
389 * address from being added.
390 */
391 sta = ieee80211_find_sta_by_ifaddr(mvm->hw, hdr->addr2, NULL);
392 }
393
394 if (sta) {
395 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
396 struct ieee80211_vif *tx_blocked_vif =
397 rcu_dereference(mvm->csa_tx_blocked_vif);
398 struct iwl_fw_dbg_trigger_tlv *trig;
399 struct ieee80211_vif *vif = mvmsta->vif;
400
401 /* We have tx blocked stations (with CS bit). If we heard
402 * frames from a blocked station on a new channel we can
403 * TX to it again.
404 */
405 if (unlikely(tx_blocked_vif) && vif == tx_blocked_vif) {
406 struct iwl_mvm_vif *mvmvif =
407 iwl_mvm_vif_from_mac80211(tx_blocked_vif);
408
409 if (mvmvif->csa_target_freq == rx_status->freq)
410 iwl_mvm_sta_modify_disable_tx_ap(mvm, sta,
411 false);
412 }
413
414 rs_update_last_rssi(mvm, mvmsta, rx_status);
415
416 trig = iwl_fw_dbg_trigger_on(&mvm->fwrt,
417 ieee80211_vif_to_wdev(vif),
418 FW_DBG_TRIGGER_RSSI);
419
420 if (trig && ieee80211_is_beacon(hdr->frame_control)) {
421 struct iwl_fw_dbg_trigger_low_rssi *rssi_trig;
422 s32 rssi;
423
424 rssi_trig = (void *)trig->data;
425 rssi = le32_to_cpu(rssi_trig->rssi);
426
427 if (rx_status->signal < rssi)
428 iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
429 NULL);
430 }
431
432 if (!mvm->tcm.paused && len >= sizeof(*hdr) &&
433 !is_multicast_ether_addr(hdr->addr1) &&
434 ieee80211_is_data(hdr->frame_control))
435 iwl_mvm_rx_handle_tcm(mvm, sta, hdr, len, phy_info,
436 rate_n_flags);
437
438 if (ieee80211_is_data(hdr->frame_control))
439 iwl_mvm_rx_csum(sta, skb, rx_pkt_status);
440 }
441 rcu_read_unlock();
442
443 /* set the preamble flag if appropriate */
444 if (phy_info->phy_flags & cpu_to_le16(RX_RES_PHY_FLAGS_SHORT_PREAMBLE))
445 rx_status->enc_flags |= RX_ENC_FLAG_SHORTPRE;
446
447 if (phy_info->phy_flags & cpu_to_le16(RX_RES_PHY_FLAGS_AGG)) {
448 /*
449 * We know which subframes of an A-MPDU belong
450 * together since we get a single PHY response
451 * from the firmware for all of them
452 */
453 rx_status->flag |= RX_FLAG_AMPDU_DETAILS;
454 rx_status->ampdu_reference = mvm->ampdu_ref;
455 }
456
457 /* Set up the HT phy flags */
458 switch (rate_n_flags & RATE_MCS_CHAN_WIDTH_MSK) {
459 case RATE_MCS_CHAN_WIDTH_20:
460 break;
461 case RATE_MCS_CHAN_WIDTH_40:
462 rx_status->bw = RATE_INFO_BW_40;
463 break;
464 case RATE_MCS_CHAN_WIDTH_80:
465 rx_status->bw = RATE_INFO_BW_80;
466 break;
467 case RATE_MCS_CHAN_WIDTH_160:
468 rx_status->bw = RATE_INFO_BW_160;
469 break;
470 }
471 if (!(rate_n_flags & RATE_MCS_CCK_MSK) &&
472 rate_n_flags & RATE_MCS_SGI_MSK)
473 rx_status->enc_flags |= RX_ENC_FLAG_SHORT_GI;
474 if (rate_n_flags & RATE_HT_MCS_GF_MSK)
475 rx_status->enc_flags |= RX_ENC_FLAG_HT_GF;
476 if (rate_n_flags & RATE_MCS_LDPC_MSK)
477 rx_status->enc_flags |= RX_ENC_FLAG_LDPC;
478 if (rate_n_flags & RATE_MCS_HT_MSK) {
479 u8 stbc = (rate_n_flags & RATE_MCS_STBC_MSK) >>
480 RATE_MCS_STBC_POS;
481 rx_status->encoding = RX_ENC_HT;
482 rx_status->rate_idx = rate_n_flags & RATE_HT_MCS_INDEX_MSK;
483 rx_status->enc_flags |= stbc << RX_ENC_FLAG_STBC_SHIFT;
484 } else if (rate_n_flags & RATE_MCS_VHT_MSK) {
485 u8 stbc = (rate_n_flags & RATE_MCS_STBC_MSK) >>
486 RATE_MCS_STBC_POS;
487 rx_status->nss =
488 ((rate_n_flags & RATE_VHT_MCS_NSS_MSK) >>
489 RATE_VHT_MCS_NSS_POS) + 1;
490 rx_status->rate_idx = rate_n_flags & RATE_VHT_MCS_RATE_CODE_MSK;
491 rx_status->encoding = RX_ENC_VHT;
492 rx_status->enc_flags |= stbc << RX_ENC_FLAG_STBC_SHIFT;
493 if (rate_n_flags & RATE_MCS_BF_MSK)
494 rx_status->enc_flags |= RX_ENC_FLAG_BF;
495 } else {
496 int rate = iwl_mvm_legacy_rate_to_mac80211_idx(rate_n_flags,
497 rx_status->band);
498
499 if (WARN(rate < 0 || rate > 0xFF,
500 "Invalid rate flags 0x%x, band %d,\n",
501 rate_n_flags, rx_status->band)) {
502 kfree_skb(skb);
503 return;
504 }
505 rx_status->rate_idx = rate;
506 }
507
508 #ifdef CONFIG_IWLWIFI_DEBUGFS
509 iwl_mvm_update_frame_stats(mvm, rate_n_flags,
510 rx_status->flag & RX_FLAG_AMPDU_DETAILS);
511 #endif
512
513 if (unlikely((ieee80211_is_beacon(hdr->frame_control) ||
514 ieee80211_is_probe_resp(hdr->frame_control)) &&
515 mvm->sched_scan_pass_all == SCHED_SCAN_PASS_ALL_ENABLED))
516 mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_FOUND;
517
518 if (unlikely(ieee80211_is_beacon(hdr->frame_control) ||
519 ieee80211_is_probe_resp(hdr->frame_control)))
520 rx_status->boottime_ns = ktime_get_boottime_ns();
521
522 iwl_mvm_pass_packet_to_mac80211(mvm, sta, napi, skb, hdr, len,
523 crypt_len, rxb);
524 }
525
526 struct iwl_mvm_stat_data {
527 struct iwl_mvm *mvm;
528 __le32 flags;
529 __le32 mac_id;
530 u8 beacon_filter_average_energy;
531 __le32 *beacon_counter;
532 u8 *beacon_average_energy;
533 };
534
iwl_mvm_stat_iterator(void * _data,u8 * mac,struct ieee80211_vif * vif)535 static void iwl_mvm_stat_iterator(void *_data, u8 *mac,
536 struct ieee80211_vif *vif)
537 {
538 struct iwl_mvm_stat_data *data = _data;
539 struct iwl_mvm *mvm = data->mvm;
540 int sig = -data->beacon_filter_average_energy;
541 int last_event;
542 int thold = vif->bss_conf.cqm_rssi_thold;
543 int hyst = vif->bss_conf.cqm_rssi_hyst;
544 u16 id = le32_to_cpu(data->mac_id);
545 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
546 u16 vif_id = mvmvif->id;
547
548 /* This doesn't need the MAC ID check since it's not taking the
549 * data copied into the "data" struct, but rather the data from
550 * the notification directly.
551 */
552 mvmvif->beacon_stats.num_beacons =
553 le32_to_cpu(data->beacon_counter[vif_id]);
554 mvmvif->beacon_stats.avg_signal =
555 -data->beacon_average_energy[vif_id];
556
557 /* make sure that beacon statistics don't go backwards with TCM
558 * request to clear statistics
559 */
560 if (le32_to_cpu(data->flags) & IWL_STATISTICS_REPLY_FLG_CLEAR)
561 mvmvif->beacon_stats.accu_num_beacons +=
562 mvmvif->beacon_stats.num_beacons;
563
564 if (mvmvif->id != id)
565 return;
566
567 if (vif->type != NL80211_IFTYPE_STATION)
568 return;
569
570 if (sig == 0) {
571 IWL_DEBUG_RX(mvm, "RSSI is 0 - skip signal based decision\n");
572 return;
573 }
574
575 mvmvif->bf_data.ave_beacon_signal = sig;
576
577 /* BT Coex */
578 if (mvmvif->bf_data.bt_coex_min_thold !=
579 mvmvif->bf_data.bt_coex_max_thold) {
580 last_event = mvmvif->bf_data.last_bt_coex_event;
581 if (sig > mvmvif->bf_data.bt_coex_max_thold &&
582 (last_event <= mvmvif->bf_data.bt_coex_min_thold ||
583 last_event == 0)) {
584 mvmvif->bf_data.last_bt_coex_event = sig;
585 IWL_DEBUG_RX(mvm, "cqm_iterator bt coex high %d\n",
586 sig);
587 iwl_mvm_bt_rssi_event(mvm, vif, RSSI_EVENT_HIGH);
588 } else if (sig < mvmvif->bf_data.bt_coex_min_thold &&
589 (last_event >= mvmvif->bf_data.bt_coex_max_thold ||
590 last_event == 0)) {
591 mvmvif->bf_data.last_bt_coex_event = sig;
592 IWL_DEBUG_RX(mvm, "cqm_iterator bt coex low %d\n",
593 sig);
594 iwl_mvm_bt_rssi_event(mvm, vif, RSSI_EVENT_LOW);
595 }
596 }
597
598 if (!(vif->driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI))
599 return;
600
601 /* CQM Notification */
602 last_event = mvmvif->bf_data.last_cqm_event;
603 if (thold && sig < thold && (last_event == 0 ||
604 sig < last_event - hyst)) {
605 mvmvif->bf_data.last_cqm_event = sig;
606 IWL_DEBUG_RX(mvm, "cqm_iterator cqm low %d\n",
607 sig);
608 ieee80211_cqm_rssi_notify(
609 vif,
610 NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW,
611 sig,
612 GFP_KERNEL);
613 } else if (sig > thold &&
614 (last_event == 0 || sig > last_event + hyst)) {
615 mvmvif->bf_data.last_cqm_event = sig;
616 IWL_DEBUG_RX(mvm, "cqm_iterator cqm high %d\n",
617 sig);
618 ieee80211_cqm_rssi_notify(
619 vif,
620 NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH,
621 sig,
622 GFP_KERNEL);
623 }
624 }
625
626 static inline void
iwl_mvm_rx_stats_check_trigger(struct iwl_mvm * mvm,struct iwl_rx_packet * pkt)627 iwl_mvm_rx_stats_check_trigger(struct iwl_mvm *mvm, struct iwl_rx_packet *pkt)
628 {
629 struct iwl_fw_dbg_trigger_tlv *trig;
630 struct iwl_fw_dbg_trigger_stats *trig_stats;
631 u32 trig_offset, trig_thold;
632
633 trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, NULL, FW_DBG_TRIGGER_STATS);
634 if (!trig)
635 return;
636
637 trig_stats = (void *)trig->data;
638
639 trig_offset = le32_to_cpu(trig_stats->stop_offset);
640 trig_thold = le32_to_cpu(trig_stats->stop_threshold);
641
642 if (WARN_ON_ONCE(trig_offset >= iwl_rx_packet_payload_len(pkt)))
643 return;
644
645 if (le32_to_cpup((__le32 *) (pkt->data + trig_offset)) < trig_thold)
646 return;
647
648 iwl_fw_dbg_collect_trig(&mvm->fwrt, trig, NULL);
649 }
650
iwl_mvm_stats_energy_iter(void * _data,struct ieee80211_sta * sta)651 static void iwl_mvm_stats_energy_iter(void *_data,
652 struct ieee80211_sta *sta)
653 {
654 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
655 u8 *energy = _data;
656 u32 sta_id = mvmsta->sta_id;
657
658 if (WARN_ONCE(sta_id >= IWL_MVM_STATION_COUNT_MAX, "sta_id %d >= %d",
659 sta_id, IWL_MVM_STATION_COUNT_MAX))
660 return;
661
662 if (energy[sta_id])
663 mvmsta->avg_energy = energy[sta_id];
664
665 }
666
667 static void
iwl_mvm_update_tcm_from_stats(struct iwl_mvm * mvm,__le32 * air_time_le,__le32 * rx_bytes_le)668 iwl_mvm_update_tcm_from_stats(struct iwl_mvm *mvm, __le32 *air_time_le,
669 __le32 *rx_bytes_le)
670 {
671 int i;
672
673 spin_lock(&mvm->tcm.lock);
674 for (i = 0; i < NUM_MAC_INDEX_DRIVER; i++) {
675 struct iwl_mvm_tcm_mac *mdata = &mvm->tcm.data[i];
676 u32 rx_bytes = le32_to_cpu(rx_bytes_le[i]);
677 u32 airtime = le32_to_cpu(air_time_le[i]);
678
679 mdata->rx.airtime += airtime;
680 mdata->uapsd_nonagg_detect.rx_bytes += rx_bytes;
681 if (airtime) {
682 /* re-init every time to store rate from FW */
683 ewma_rate_init(&mdata->uapsd_nonagg_detect.rate);
684 ewma_rate_add(&mdata->uapsd_nonagg_detect.rate,
685 rx_bytes * 8 / airtime);
686 }
687 }
688 spin_unlock(&mvm->tcm.lock);
689 }
690
691 static void
iwl_mvm_handle_rx_statistics_tlv(struct iwl_mvm * mvm,struct iwl_rx_packet * pkt)692 iwl_mvm_handle_rx_statistics_tlv(struct iwl_mvm *mvm,
693 struct iwl_rx_packet *pkt)
694 {
695 struct iwl_mvm_stat_data data = {
696 .mvm = mvm,
697 };
698 u8 beacon_average_energy[MAC_INDEX_AUX];
699 u8 average_energy[IWL_MVM_STATION_COUNT_MAX];
700 struct iwl_statistics_operational_ntfy *stats;
701 int expected_size;
702 __le32 flags;
703 int i;
704
705 expected_size = sizeof(*stats);
706 if (WARN_ONCE(iwl_rx_packet_payload_len(pkt) < expected_size,
707 "received invalid statistics size (%d)!, expected_size: %d\n",
708 iwl_rx_packet_payload_len(pkt), expected_size))
709 return;
710
711 stats = (void *)&pkt->data;
712
713 if (WARN_ONCE(stats->hdr.type != FW_STATISTICS_OPERATIONAL ||
714 stats->hdr.version !=
715 iwl_fw_lookup_notif_ver(mvm->fw, LONG_GROUP, STATISTICS_CMD, 0),
716 "received unsupported hdr type %d, version %d\n",
717 stats->hdr.type, stats->hdr.version))
718 return;
719
720 flags = stats->flags;
721 mvm->radio_stats.rx_time = le64_to_cpu(stats->rx_time);
722 mvm->radio_stats.tx_time = le64_to_cpu(stats->tx_time);
723 mvm->radio_stats.on_time_rf = le64_to_cpu(stats->on_time_rf);
724 mvm->radio_stats.on_time_scan = le64_to_cpu(stats->on_time_scan);
725
726 iwl_mvm_rx_stats_check_trigger(mvm, pkt);
727
728 data.mac_id = stats->mac_id;
729 data.beacon_filter_average_energy =
730 le32_to_cpu(stats->beacon_filter_average_energy);
731 data.flags = flags;
732 data.beacon_counter = stats->beacon_counter;
733 for (i = 0; i < ARRAY_SIZE(beacon_average_energy); i++)
734 beacon_average_energy[i] =
735 le32_to_cpu(stats->beacon_average_energy[i]);
736
737 data.beacon_average_energy = beacon_average_energy;
738
739 ieee80211_iterate_active_interfaces(mvm->hw,
740 IEEE80211_IFACE_ITER_NORMAL,
741 iwl_mvm_stat_iterator,
742 &data);
743
744 for (i = 0; i < ARRAY_SIZE(average_energy); i++)
745 average_energy[i] = le32_to_cpu(stats->average_energy[i]);
746 ieee80211_iterate_stations_atomic(mvm->hw, iwl_mvm_stats_energy_iter,
747 average_energy);
748 /*
749 * Don't update in case the statistics are not cleared, since
750 * we will end up counting twice the same airtime, once in TCM
751 * request and once in statistics notification.
752 */
753 if (le32_to_cpu(flags) & IWL_STATISTICS_REPLY_FLG_CLEAR)
754 iwl_mvm_update_tcm_from_stats(mvm, stats->air_time,
755 stats->rx_bytes);
756 }
757
iwl_mvm_handle_rx_statistics(struct iwl_mvm * mvm,struct iwl_rx_packet * pkt)758 void iwl_mvm_handle_rx_statistics(struct iwl_mvm *mvm,
759 struct iwl_rx_packet *pkt)
760 {
761 struct iwl_mvm_stat_data data = {
762 .mvm = mvm,
763 };
764 __le32 *bytes, *air_time, flags;
765 int expected_size;
766 u8 *energy;
767
768 /* From ver 14 and up we use TLV statistics format */
769 if (iwl_fw_lookup_notif_ver(mvm->fw, LONG_GROUP,
770 STATISTICS_CMD, 0) >= 14)
771 return iwl_mvm_handle_rx_statistics_tlv(mvm, pkt);
772
773 if (!iwl_mvm_has_new_rx_stats_api(mvm)) {
774 if (iwl_mvm_has_new_rx_api(mvm))
775 expected_size = sizeof(struct iwl_notif_statistics_v11);
776 else
777 expected_size = sizeof(struct iwl_notif_statistics_v10);
778 } else {
779 expected_size = sizeof(struct iwl_notif_statistics);
780 }
781
782 if (WARN_ONCE(iwl_rx_packet_payload_len(pkt) != expected_size,
783 "received invalid statistics size (%d)!\n",
784 iwl_rx_packet_payload_len(pkt)))
785 return;
786
787 if (!iwl_mvm_has_new_rx_stats_api(mvm)) {
788 struct iwl_notif_statistics_v11 *stats = (void *)&pkt->data;
789
790 data.mac_id = stats->rx.general.mac_id;
791 data.beacon_filter_average_energy =
792 stats->general.common.beacon_filter_average_energy;
793
794 mvm->rx_stats_v3 = stats->rx;
795
796 mvm->radio_stats.rx_time =
797 le64_to_cpu(stats->general.common.rx_time);
798 mvm->radio_stats.tx_time =
799 le64_to_cpu(stats->general.common.tx_time);
800 mvm->radio_stats.on_time_rf =
801 le64_to_cpu(stats->general.common.on_time_rf);
802 mvm->radio_stats.on_time_scan =
803 le64_to_cpu(stats->general.common.on_time_scan);
804
805 data.beacon_counter = stats->general.beacon_counter;
806 data.beacon_average_energy =
807 stats->general.beacon_average_energy;
808 flags = stats->flag;
809 } else {
810 struct iwl_notif_statistics *stats = (void *)&pkt->data;
811
812 data.mac_id = stats->rx.general.mac_id;
813 data.beacon_filter_average_energy =
814 stats->general.common.beacon_filter_average_energy;
815
816 mvm->rx_stats = stats->rx;
817
818 mvm->radio_stats.rx_time =
819 le64_to_cpu(stats->general.common.rx_time);
820 mvm->radio_stats.tx_time =
821 le64_to_cpu(stats->general.common.tx_time);
822 mvm->radio_stats.on_time_rf =
823 le64_to_cpu(stats->general.common.on_time_rf);
824 mvm->radio_stats.on_time_scan =
825 le64_to_cpu(stats->general.common.on_time_scan);
826
827 data.beacon_counter = stats->general.beacon_counter;
828 data.beacon_average_energy =
829 stats->general.beacon_average_energy;
830 flags = stats->flag;
831 }
832 data.flags = flags;
833
834 iwl_mvm_rx_stats_check_trigger(mvm, pkt);
835
836 ieee80211_iterate_active_interfaces(mvm->hw,
837 IEEE80211_IFACE_ITER_NORMAL,
838 iwl_mvm_stat_iterator,
839 &data);
840
841 if (!iwl_mvm_has_new_rx_api(mvm))
842 return;
843
844 if (!iwl_mvm_has_new_rx_stats_api(mvm)) {
845 struct iwl_notif_statistics_v11 *v11 = (void *)&pkt->data;
846
847 energy = (void *)&v11->load_stats.avg_energy;
848 bytes = (void *)&v11->load_stats.byte_count;
849 air_time = (void *)&v11->load_stats.air_time;
850 } else {
851 struct iwl_notif_statistics *stats = (void *)&pkt->data;
852
853 energy = (void *)&stats->load_stats.avg_energy;
854 bytes = (void *)&stats->load_stats.byte_count;
855 air_time = (void *)&stats->load_stats.air_time;
856 }
857 ieee80211_iterate_stations_atomic(mvm->hw, iwl_mvm_stats_energy_iter,
858 energy);
859
860 /*
861 * Don't update in case the statistics are not cleared, since
862 * we will end up counting twice the same airtime, once in TCM
863 * request and once in statistics notification.
864 */
865 if (le32_to_cpu(flags) & IWL_STATISTICS_REPLY_FLG_CLEAR)
866 iwl_mvm_update_tcm_from_stats(mvm, air_time, bytes);
867
868 }
869
iwl_mvm_rx_statistics(struct iwl_mvm * mvm,struct iwl_rx_cmd_buffer * rxb)870 void iwl_mvm_rx_statistics(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
871 {
872 iwl_mvm_handle_rx_statistics(mvm, rxb_addr(rxb));
873 }
874
iwl_mvm_window_status_notif(struct iwl_mvm * mvm,struct iwl_rx_cmd_buffer * rxb)875 void iwl_mvm_window_status_notif(struct iwl_mvm *mvm,
876 struct iwl_rx_cmd_buffer *rxb)
877 {
878 struct iwl_rx_packet *pkt = rxb_addr(rxb);
879 struct iwl_ba_window_status_notif *notif = (void *)pkt->data;
880 int i;
881
882 BUILD_BUG_ON(ARRAY_SIZE(notif->ra_tid) != BA_WINDOW_STREAMS_MAX);
883 BUILD_BUG_ON(ARRAY_SIZE(notif->mpdu_rx_count) != BA_WINDOW_STREAMS_MAX);
884 BUILD_BUG_ON(ARRAY_SIZE(notif->bitmap) != BA_WINDOW_STREAMS_MAX);
885 BUILD_BUG_ON(ARRAY_SIZE(notif->start_seq_num) != BA_WINDOW_STREAMS_MAX);
886
887 rcu_read_lock();
888 for (i = 0; i < BA_WINDOW_STREAMS_MAX; i++) {
889 struct ieee80211_sta *sta;
890 u8 sta_id, tid;
891 u64 bitmap;
892 u32 ssn;
893 u16 ratid;
894 u16 received_mpdu;
895
896 ratid = le16_to_cpu(notif->ra_tid[i]);
897 /* check that this TID is valid */
898 if (!(ratid & BA_WINDOW_STATUS_VALID_MSK))
899 continue;
900
901 received_mpdu = le16_to_cpu(notif->mpdu_rx_count[i]);
902 if (received_mpdu == 0)
903 continue;
904
905 tid = ratid & BA_WINDOW_STATUS_TID_MSK;
906 /* get the station */
907 sta_id = (ratid & BA_WINDOW_STATUS_STA_ID_MSK)
908 >> BA_WINDOW_STATUS_STA_ID_POS;
909 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
910 if (IS_ERR_OR_NULL(sta))
911 continue;
912 bitmap = le64_to_cpu(notif->bitmap[i]);
913 ssn = le32_to_cpu(notif->start_seq_num[i]);
914
915 /* update mac80211 with the bitmap for the reordering buffer */
916 ieee80211_mark_rx_ba_filtered_frames(sta, tid, ssn, bitmap,
917 received_mpdu);
918 }
919 rcu_read_unlock();
920 }
921