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