linux/drivers/net/wireless/intel/iwlwifi/mvm/rxmq.c
<<
>>
Prefs
   1/******************************************************************************
   2 *
   3 * This file is provided under a dual BSD/GPLv2 license.  When using or
   4 * redistributing this file, you may do so under either license.
   5 *
   6 * GPL LICENSE SUMMARY
   7 *
   8 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
   9 * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
  10 * Copyright(c) 2015 - 2017 Intel Deutschland GmbH
  11 * Copyright(c) 2018 - 2019 Intel Corporation
  12 *
  13 * This program is free software; you can redistribute it and/or modify
  14 * it under the terms of version 2 of the GNU General Public License as
  15 * published by the Free Software Foundation.
  16 *
  17 * This program is distributed in the hope that it will be useful, but
  18 * WITHOUT ANY WARRANTY; without even the implied warranty of
  19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  20 * General Public License for more details.
  21 *
  22 * The full GNU General Public License is included in this distribution
  23 * in the file called COPYING.
  24 *
  25 * Contact Information:
  26 *  Intel Linux Wireless <linuxwifi@intel.com>
  27 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  28 *
  29 * BSD LICENSE
  30 *
  31 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
  32 * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
  33 * Copyright(c) 2015 - 2017 Intel Deutschland GmbH
  34 * Copyright(c) 2018 - 2019 Intel Corporation
  35 * All rights reserved.
  36 *
  37 * Redistribution and use in source and binary forms, with or without
  38 * modification, are permitted provided that the following conditions
  39 * are met:
  40 *
  41 *  * Redistributions of source code must retain the above copyright
  42 *    notice, this list of conditions and the following disclaimer.
  43 *  * Redistributions in binary form must reproduce the above copyright
  44 *    notice, this list of conditions and the following disclaimer in
  45 *    the documentation and/or other materials provided with the
  46 *    distribution.
  47 *  * Neither the name Intel Corporation nor the names of its
  48 *    contributors may be used to endorse or promote products derived
  49 *    from this software without specific prior written permission.
  50 *
  51 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  52 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  53 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  54 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  55 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  56 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  57 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  58 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  59 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  60 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  61 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  62 *****************************************************************************/
  63#include <linux/etherdevice.h>
  64#include <linux/skbuff.h>
  65#include "iwl-trans.h"
  66#include "mvm.h"
  67#include "fw-api.h"
  68
  69static void *iwl_mvm_skb_get_hdr(struct sk_buff *skb)
  70{
  71        struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
  72        u8 *data = skb->data;
  73
  74        /* Alignment concerns */
  75        BUILD_BUG_ON(sizeof(struct ieee80211_radiotap_he) % 4);
  76        BUILD_BUG_ON(sizeof(struct ieee80211_radiotap_he_mu) % 4);
  77        BUILD_BUG_ON(sizeof(struct ieee80211_radiotap_lsig) % 4);
  78        BUILD_BUG_ON(sizeof(struct ieee80211_vendor_radiotap) % 4);
  79
  80        if (rx_status->flag & RX_FLAG_RADIOTAP_HE)
  81                data += sizeof(struct ieee80211_radiotap_he);
  82        if (rx_status->flag & RX_FLAG_RADIOTAP_HE_MU)
  83                data += sizeof(struct ieee80211_radiotap_he_mu);
  84        if (rx_status->flag & RX_FLAG_RADIOTAP_LSIG)
  85                data += sizeof(struct ieee80211_radiotap_lsig);
  86        if (rx_status->flag & RX_FLAG_RADIOTAP_VENDOR_DATA) {
  87                struct ieee80211_vendor_radiotap *radiotap = (void *)data;
  88
  89                data += sizeof(*radiotap) + radiotap->len + radiotap->pad;
  90        }
  91
  92        return data;
  93}
  94
  95static inline int iwl_mvm_check_pn(struct iwl_mvm *mvm, struct sk_buff *skb,
  96                                   int queue, struct ieee80211_sta *sta)
  97{
  98        struct iwl_mvm_sta *mvmsta;
  99        struct ieee80211_hdr *hdr = iwl_mvm_skb_get_hdr(skb);
 100        struct ieee80211_rx_status *stats = IEEE80211_SKB_RXCB(skb);
 101        struct iwl_mvm_key_pn *ptk_pn;
 102        int res;
 103        u8 tid, keyidx;
 104        u8 pn[IEEE80211_CCMP_PN_LEN];
 105        u8 *extiv;
 106
 107        /* do PN checking */
 108
 109        /* multicast and non-data only arrives on default queue */
 110        if (!ieee80211_is_data(hdr->frame_control) ||
 111            is_multicast_ether_addr(hdr->addr1))
 112                return 0;
 113
 114        /* do not check PN for open AP */
 115        if (!(stats->flag & RX_FLAG_DECRYPTED))
 116                return 0;
 117
 118        /*
 119         * avoid checking for default queue - we don't want to replicate
 120         * all the logic that's necessary for checking the PN on fragmented
 121         * frames, leave that to mac80211
 122         */
 123        if (queue == 0)
 124                return 0;
 125
 126        /* if we are here - this for sure is either CCMP or GCMP */
 127        if (IS_ERR_OR_NULL(sta)) {
 128                IWL_ERR(mvm,
 129                        "expected hw-decrypted unicast frame for station\n");
 130                return -1;
 131        }
 132
 133        mvmsta = iwl_mvm_sta_from_mac80211(sta);
 134
 135        extiv = (u8 *)hdr + ieee80211_hdrlen(hdr->frame_control);
 136        keyidx = extiv[3] >> 6;
 137
 138        ptk_pn = rcu_dereference(mvmsta->ptk_pn[keyidx]);
 139        if (!ptk_pn)
 140                return -1;
 141
 142        if (ieee80211_is_data_qos(hdr->frame_control))
 143                tid = ieee80211_get_tid(hdr);
 144        else
 145                tid = 0;
 146
 147        /* we don't use HCCA/802.11 QoS TSPECs, so drop such frames */
 148        if (tid >= IWL_MAX_TID_COUNT)
 149                return -1;
 150
 151        /* load pn */
 152        pn[0] = extiv[7];
 153        pn[1] = extiv[6];
 154        pn[2] = extiv[5];
 155        pn[3] = extiv[4];
 156        pn[4] = extiv[1];
 157        pn[5] = extiv[0];
 158
 159        res = memcmp(pn, ptk_pn->q[queue].pn[tid], IEEE80211_CCMP_PN_LEN);
 160        if (res < 0)
 161                return -1;
 162        if (!res && !(stats->flag & RX_FLAG_ALLOW_SAME_PN))
 163                return -1;
 164
 165        memcpy(ptk_pn->q[queue].pn[tid], pn, IEEE80211_CCMP_PN_LEN);
 166        stats->flag |= RX_FLAG_PN_VALIDATED;
 167
 168        return 0;
 169}
 170
 171/* iwl_mvm_create_skb Adds the rxb to a new skb */
 172static int iwl_mvm_create_skb(struct iwl_mvm *mvm, struct sk_buff *skb,
 173                              struct ieee80211_hdr *hdr, u16 len, u8 crypt_len,
 174                              struct iwl_rx_cmd_buffer *rxb)
 175{
 176        struct iwl_rx_packet *pkt = rxb_addr(rxb);
 177        struct iwl_rx_mpdu_desc *desc = (void *)pkt->data;
 178        unsigned int headlen, fraglen, pad_len = 0;
 179        unsigned int hdrlen = ieee80211_hdrlen(hdr->frame_control);
 180
 181        if (desc->mac_flags2 & IWL_RX_MPDU_MFLG2_PAD) {
 182                len -= 2;
 183                pad_len = 2;
 184        }
 185
 186        /* If frame is small enough to fit in skb->head, pull it completely.
 187         * If not, only pull ieee80211_hdr (including crypto if present, and
 188         * an additional 8 bytes for SNAP/ethertype, see below) so that
 189         * splice() or TCP coalesce are more efficient.
 190         *
 191         * Since, in addition, ieee80211_data_to_8023() always pull in at
 192         * least 8 bytes (possibly more for mesh) we can do the same here
 193         * to save the cost of doing it later. That still doesn't pull in
 194         * the actual IP header since the typical case has a SNAP header.
 195         * If the latter changes (there are efforts in the standards group
 196         * to do so) we should revisit this and ieee80211_data_to_8023().
 197         */
 198        headlen = (len <= skb_tailroom(skb)) ? len :
 199                                               hdrlen + crypt_len + 8;
 200
 201        /* The firmware may align the packet to DWORD.
 202         * The padding is inserted after the IV.
 203         * After copying the header + IV skip the padding if
 204         * present before copying packet data.
 205         */
 206        hdrlen += crypt_len;
 207
 208        if (WARN_ONCE(headlen < hdrlen,
 209                      "invalid packet lengths (hdrlen=%d, len=%d, crypt_len=%d)\n",
 210                      hdrlen, len, crypt_len)) {
 211                /*
 212                 * We warn and trace because we want to be able to see
 213                 * it in trace-cmd as well.
 214                 */
 215                IWL_DEBUG_RX(mvm,
 216                             "invalid packet lengths (hdrlen=%d, len=%d, crypt_len=%d)\n",
 217                             hdrlen, len, crypt_len);
 218                return -EINVAL;
 219        }
 220
 221        skb_put_data(skb, hdr, hdrlen);
 222        skb_put_data(skb, (u8 *)hdr + hdrlen + pad_len, headlen - hdrlen);
 223
 224        fraglen = len - headlen;
 225
 226        if (fraglen) {
 227                int offset = (void *)hdr + headlen + pad_len -
 228                             rxb_addr(rxb) + rxb_offset(rxb);
 229
 230                skb_add_rx_frag(skb, 0, rxb_steal_page(rxb), offset,
 231                                fraglen, rxb->truesize);
 232        }
 233
 234        return 0;
 235}
 236
 237static void iwl_mvm_add_rtap_sniffer_config(struct iwl_mvm *mvm,
 238                                            struct sk_buff *skb)
 239{
 240        struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
 241        struct ieee80211_vendor_radiotap *radiotap;
 242        const int size = sizeof(*radiotap) + sizeof(__le16);
 243
 244        if (!mvm->cur_aid)
 245                return;
 246
 247        /* ensure alignment */
 248        BUILD_BUG_ON((size + 2) % 4);
 249
 250        radiotap = skb_put(skb, size + 2);
 251        radiotap->align = 1;
 252        /* Intel OUI */
 253        radiotap->oui[0] = 0xf6;
 254        radiotap->oui[1] = 0x54;
 255        radiotap->oui[2] = 0x25;
 256        /* radiotap sniffer config sub-namespace */
 257        radiotap->subns = 1;
 258        radiotap->present = 0x1;
 259        radiotap->len = size - sizeof(*radiotap);
 260        radiotap->pad = 2;
 261
 262        /* fill the data now */
 263        memcpy(radiotap->data, &mvm->cur_aid, sizeof(mvm->cur_aid));
 264        /* and clear the padding */
 265        memset(radiotap->data + sizeof(__le16), 0, radiotap->pad);
 266
 267        rx_status->flag |= RX_FLAG_RADIOTAP_VENDOR_DATA;
 268}
 269
 270/* iwl_mvm_pass_packet_to_mac80211 - passes the packet for mac80211 */
 271static void iwl_mvm_pass_packet_to_mac80211(struct iwl_mvm *mvm,
 272                                            struct napi_struct *napi,
 273                                            struct sk_buff *skb, int queue,
 274                                            struct ieee80211_sta *sta,
 275                                            bool csi)
 276{
 277        if (iwl_mvm_check_pn(mvm, skb, queue, sta))
 278                kfree_skb(skb);
 279        else
 280                ieee80211_rx_napi(mvm->hw, sta, skb, napi);
 281}
 282
 283static void iwl_mvm_get_signal_strength(struct iwl_mvm *mvm,
 284                                        struct ieee80211_rx_status *rx_status,
 285                                        u32 rate_n_flags, int energy_a,
 286                                        int energy_b)
 287{
 288        int max_energy;
 289        u32 rate_flags = rate_n_flags;
 290
 291        energy_a = energy_a ? -energy_a : S8_MIN;
 292        energy_b = energy_b ? -energy_b : S8_MIN;
 293        max_energy = max(energy_a, energy_b);
 294
 295        IWL_DEBUG_STATS(mvm, "energy In A %d B %d, and max %d\n",
 296                        energy_a, energy_b, max_energy);
 297
 298        rx_status->signal = max_energy;
 299        rx_status->chains =
 300                (rate_flags & RATE_MCS_ANT_AB_MSK) >> RATE_MCS_ANT_POS;
 301        rx_status->chain_signal[0] = energy_a;
 302        rx_status->chain_signal[1] = energy_b;
 303        rx_status->chain_signal[2] = S8_MIN;
 304}
 305
 306static int iwl_mvm_rx_crypto(struct iwl_mvm *mvm, struct ieee80211_hdr *hdr,
 307                             struct ieee80211_rx_status *stats, u16 phy_info,
 308                             struct iwl_rx_mpdu_desc *desc,
 309                             u32 pkt_flags, int queue, u8 *crypt_len)
 310{
 311        u16 status = le16_to_cpu(desc->status);
 312
 313        /*
 314         * Drop UNKNOWN frames in aggregation, unless in monitor mode
 315         * (where we don't have the keys).
 316         * We limit this to aggregation because in TKIP this is a valid
 317         * scenario, since we may not have the (correct) TTAK (phase 1
 318         * key) in the firmware.
 319         */
 320        if (phy_info & IWL_RX_MPDU_PHY_AMPDU &&
 321            (status & IWL_RX_MPDU_STATUS_SEC_MASK) ==
 322            IWL_RX_MPDU_STATUS_SEC_UNKNOWN && !mvm->monitor_on)
 323                return -1;
 324
 325        if (!ieee80211_has_protected(hdr->frame_control) ||
 326            (status & IWL_RX_MPDU_STATUS_SEC_MASK) ==
 327            IWL_RX_MPDU_STATUS_SEC_NONE)
 328                return 0;
 329
 330        /* TODO: handle packets encrypted with unknown alg */
 331
 332        switch (status & IWL_RX_MPDU_STATUS_SEC_MASK) {
 333        case IWL_RX_MPDU_STATUS_SEC_CCM:
 334        case IWL_RX_MPDU_STATUS_SEC_GCM:
 335                BUILD_BUG_ON(IEEE80211_CCMP_PN_LEN != IEEE80211_GCMP_PN_LEN);
 336                /* alg is CCM: check MIC only */
 337                if (!(status & IWL_RX_MPDU_STATUS_MIC_OK))
 338                        return -1;
 339
 340                stats->flag |= RX_FLAG_DECRYPTED;
 341                if (pkt_flags & FH_RSCSR_RADA_EN)
 342                        stats->flag |= RX_FLAG_MIC_STRIPPED;
 343                *crypt_len = IEEE80211_CCMP_HDR_LEN;
 344                return 0;
 345        case IWL_RX_MPDU_STATUS_SEC_TKIP:
 346                /* Don't drop the frame and decrypt it in SW */
 347                if (!fw_has_api(&mvm->fw->ucode_capa,
 348                                IWL_UCODE_TLV_API_DEPRECATE_TTAK) &&
 349                    !(status & IWL_RX_MPDU_RES_STATUS_TTAK_OK))
 350                        return 0;
 351
 352                if (mvm->trans->trans_cfg->gen2 &&
 353                    !(status & RX_MPDU_RES_STATUS_MIC_OK))
 354                        stats->flag |= RX_FLAG_MMIC_ERROR;
 355
 356                *crypt_len = IEEE80211_TKIP_IV_LEN;
 357                /* fall through */
 358        case IWL_RX_MPDU_STATUS_SEC_WEP:
 359                if (!(status & IWL_RX_MPDU_STATUS_ICV_OK))
 360                        return -1;
 361
 362                stats->flag |= RX_FLAG_DECRYPTED;
 363                if ((status & IWL_RX_MPDU_STATUS_SEC_MASK) ==
 364                                IWL_RX_MPDU_STATUS_SEC_WEP)
 365                        *crypt_len = IEEE80211_WEP_IV_LEN;
 366
 367                if (pkt_flags & FH_RSCSR_RADA_EN) {
 368                        stats->flag |= RX_FLAG_ICV_STRIPPED;
 369                        if (mvm->trans->trans_cfg->gen2)
 370                                stats->flag |= RX_FLAG_MMIC_STRIPPED;
 371                }
 372
 373                return 0;
 374        case IWL_RX_MPDU_STATUS_SEC_EXT_ENC:
 375                if (!(status & IWL_RX_MPDU_STATUS_MIC_OK))
 376                        return -1;
 377                stats->flag |= RX_FLAG_DECRYPTED;
 378                return 0;
 379        default:
 380                /*
 381                 * Sometimes we can get frames that were not decrypted
 382                 * because the firmware didn't have the keys yet. This can
 383                 * happen after connection where we can get multicast frames
 384                 * before the GTK is installed.
 385                 * Silently drop those frames.
 386                 * Also drop un-decrypted frames in monitor mode.
 387                 */
 388                if (!is_multicast_ether_addr(hdr->addr1) &&
 389                    !mvm->monitor_on && net_ratelimit())
 390                        IWL_ERR(mvm, "Unhandled alg: 0x%x\n", status);
 391        }
 392
 393        return 0;
 394}
 395
 396static void iwl_mvm_rx_csum(struct ieee80211_sta *sta,
 397                            struct sk_buff *skb,
 398                            struct iwl_rx_mpdu_desc *desc)
 399{
 400        struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
 401        struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(mvmsta->vif);
 402        u16 flags = le16_to_cpu(desc->l3l4_flags);
 403        u8 l3_prot = (u8)((flags & IWL_RX_L3L4_L3_PROTO_MASK) >>
 404                          IWL_RX_L3_PROTO_POS);
 405
 406        if (mvmvif->features & NETIF_F_RXCSUM &&
 407            flags & IWL_RX_L3L4_TCP_UDP_CSUM_OK &&
 408            (flags & IWL_RX_L3L4_IP_HDR_CSUM_OK ||
 409             l3_prot == IWL_RX_L3_TYPE_IPV6 ||
 410             l3_prot == IWL_RX_L3_TYPE_IPV6_FRAG))
 411                skb->ip_summed = CHECKSUM_UNNECESSARY;
 412}
 413
 414/*
 415 * returns true if a packet is a duplicate and should be dropped.
 416 * Updates AMSDU PN tracking info
 417 */
 418static bool iwl_mvm_is_dup(struct ieee80211_sta *sta, int queue,
 419                           struct ieee80211_rx_status *rx_status,
 420                           struct ieee80211_hdr *hdr,
 421                           struct iwl_rx_mpdu_desc *desc)
 422{
 423        struct iwl_mvm_sta *mvm_sta;
 424        struct iwl_mvm_rxq_dup_data *dup_data;
 425        u8 tid, sub_frame_idx;
 426
 427        if (WARN_ON(IS_ERR_OR_NULL(sta)))
 428                return false;
 429
 430        mvm_sta = iwl_mvm_sta_from_mac80211(sta);
 431        dup_data = &mvm_sta->dup_data[queue];
 432
 433        /*
 434         * Drop duplicate 802.11 retransmissions
 435         * (IEEE 802.11-2012: 9.3.2.10 "Duplicate detection and recovery")
 436         */
 437        if (ieee80211_is_ctl(hdr->frame_control) ||
 438            ieee80211_is_qos_nullfunc(hdr->frame_control) ||
 439            is_multicast_ether_addr(hdr->addr1)) {
 440                rx_status->flag |= RX_FLAG_DUP_VALIDATED;
 441                return false;
 442        }
 443
 444        if (ieee80211_is_data_qos(hdr->frame_control))
 445                /* frame has qos control */
 446                tid = ieee80211_get_tid(hdr);
 447        else
 448                tid = IWL_MAX_TID_COUNT;
 449
 450        /* If this wasn't a part of an A-MSDU the sub-frame index will be 0 */
 451        sub_frame_idx = desc->amsdu_info &
 452                IWL_RX_MPDU_AMSDU_SUBFRAME_IDX_MASK;
 453
 454        if (unlikely(ieee80211_has_retry(hdr->frame_control) &&
 455                     dup_data->last_seq[tid] == hdr->seq_ctrl &&
 456                     dup_data->last_sub_frame[tid] >= sub_frame_idx))
 457                return true;
 458
 459        /* Allow same PN as the first subframe for following sub frames */
 460        if (dup_data->last_seq[tid] == hdr->seq_ctrl &&
 461            sub_frame_idx > dup_data->last_sub_frame[tid] &&
 462            desc->mac_flags2 & IWL_RX_MPDU_MFLG2_AMSDU)
 463                rx_status->flag |= RX_FLAG_ALLOW_SAME_PN;
 464
 465        dup_data->last_seq[tid] = hdr->seq_ctrl;
 466        dup_data->last_sub_frame[tid] = sub_frame_idx;
 467
 468        rx_status->flag |= RX_FLAG_DUP_VALIDATED;
 469
 470        return false;
 471}
 472
 473int iwl_mvm_notify_rx_queue(struct iwl_mvm *mvm, u32 rxq_mask,
 474                            const u8 *data, u32 count, bool async)
 475{
 476        u8 buf[sizeof(struct iwl_rxq_sync_cmd) +
 477               sizeof(struct iwl_mvm_rss_sync_notif)];
 478        struct iwl_rxq_sync_cmd *cmd = (void *)buf;
 479        u32 data_size = sizeof(*cmd) + count;
 480        int ret;
 481
 482        /*
 483         * size must be a multiple of DWORD
 484         * Ensure we don't overflow buf
 485         */
 486        if (WARN_ON(count & 3 ||
 487                    count > sizeof(struct iwl_mvm_rss_sync_notif)))
 488                return -EINVAL;
 489
 490        cmd->rxq_mask = cpu_to_le32(rxq_mask);
 491        cmd->count =  cpu_to_le32(count);
 492        cmd->flags = 0;
 493        memcpy(cmd->payload, data, count);
 494
 495        ret = iwl_mvm_send_cmd_pdu(mvm,
 496                                   WIDE_ID(DATA_PATH_GROUP,
 497                                           TRIGGER_RX_QUEUES_NOTIF_CMD),
 498                                   async ? CMD_ASYNC : 0, data_size, cmd);
 499
 500        return ret;
 501}
 502
 503/*
 504 * Returns true if sn2 - buffer_size < sn1 < sn2.
 505 * To be used only in order to compare reorder buffer head with NSSN.
 506 * We fully trust NSSN unless it is behind us due to reorder timeout.
 507 * Reorder timeout can only bring us up to buffer_size SNs ahead of NSSN.
 508 */
 509static bool iwl_mvm_is_sn_less(u16 sn1, u16 sn2, u16 buffer_size)
 510{
 511        return ieee80211_sn_less(sn1, sn2) &&
 512               !ieee80211_sn_less(sn1, sn2 - buffer_size);
 513}
 514
 515static void iwl_mvm_sync_nssn(struct iwl_mvm *mvm, u8 baid, u16 nssn)
 516{
 517        if (IWL_MVM_USE_NSSN_SYNC) {
 518                struct iwl_mvm_rss_sync_notif notif = {
 519                        .metadata.type = IWL_MVM_RXQ_NSSN_SYNC,
 520                        .metadata.sync = 0,
 521                        .nssn_sync.baid = baid,
 522                        .nssn_sync.nssn = nssn,
 523                };
 524
 525                iwl_mvm_sync_rx_queues_internal(mvm, (void *)&notif,
 526                                                sizeof(notif));
 527        }
 528}
 529
 530#define RX_REORDER_BUF_TIMEOUT_MQ (HZ / 10)
 531
 532enum iwl_mvm_release_flags {
 533        IWL_MVM_RELEASE_SEND_RSS_SYNC = BIT(0),
 534        IWL_MVM_RELEASE_FROM_RSS_SYNC = BIT(1),
 535};
 536
 537static void iwl_mvm_release_frames(struct iwl_mvm *mvm,
 538                                   struct ieee80211_sta *sta,
 539                                   struct napi_struct *napi,
 540                                   struct iwl_mvm_baid_data *baid_data,
 541                                   struct iwl_mvm_reorder_buffer *reorder_buf,
 542                                   u16 nssn, u32 flags)
 543{
 544        struct iwl_mvm_reorder_buf_entry *entries =
 545                &baid_data->entries[reorder_buf->queue *
 546                                    baid_data->entries_per_queue];
 547        u16 ssn = reorder_buf->head_sn;
 548
 549        lockdep_assert_held(&reorder_buf->lock);
 550
 551        /*
 552         * We keep the NSSN not too far behind, if we are sync'ing it and it
 553         * is more than 2048 ahead of us, it must be behind us. Discard it.
 554         * This can happen if the queue that hit the 0 / 2048 seqno was lagging
 555         * behind and this queue already processed packets. The next if
 556         * would have caught cases where this queue would have processed less
 557         * than 64 packets, but it may have processed more than 64 packets.
 558         */
 559        if ((flags & IWL_MVM_RELEASE_FROM_RSS_SYNC) &&
 560            ieee80211_sn_less(nssn, ssn))
 561                goto set_timer;
 562
 563        /* ignore nssn smaller than head sn - this can happen due to timeout */
 564        if (iwl_mvm_is_sn_less(nssn, ssn, reorder_buf->buf_size))
 565                goto set_timer;
 566
 567        while (iwl_mvm_is_sn_less(ssn, nssn, reorder_buf->buf_size)) {
 568                int index = ssn % reorder_buf->buf_size;
 569                struct sk_buff_head *skb_list = &entries[index].e.frames;
 570                struct sk_buff *skb;
 571
 572                ssn = ieee80211_sn_inc(ssn);
 573                if ((flags & IWL_MVM_RELEASE_SEND_RSS_SYNC) &&
 574                    (ssn == 2048 || ssn == 0))
 575                        iwl_mvm_sync_nssn(mvm, baid_data->baid, ssn);
 576
 577                /*
 578                 * Empty the list. Will have more than one frame for A-MSDU.
 579                 * Empty list is valid as well since nssn indicates frames were
 580                 * received.
 581                 */
 582                while ((skb = __skb_dequeue(skb_list))) {
 583                        iwl_mvm_pass_packet_to_mac80211(mvm, napi, skb,
 584                                                        reorder_buf->queue,
 585                                                        sta, false);
 586                        reorder_buf->num_stored--;
 587                }
 588        }
 589        reorder_buf->head_sn = nssn;
 590
 591set_timer:
 592        if (reorder_buf->num_stored && !reorder_buf->removed) {
 593                u16 index = reorder_buf->head_sn % reorder_buf->buf_size;
 594
 595                while (skb_queue_empty(&entries[index].e.frames))
 596                        index = (index + 1) % reorder_buf->buf_size;
 597                /* modify timer to match next frame's expiration time */
 598                mod_timer(&reorder_buf->reorder_timer,
 599                          entries[index].e.reorder_time + 1 +
 600                          RX_REORDER_BUF_TIMEOUT_MQ);
 601        } else {
 602                del_timer(&reorder_buf->reorder_timer);
 603        }
 604}
 605
 606void iwl_mvm_reorder_timer_expired(struct timer_list *t)
 607{
 608        struct iwl_mvm_reorder_buffer *buf = from_timer(buf, t, reorder_timer);
 609        struct iwl_mvm_baid_data *baid_data =
 610                iwl_mvm_baid_data_from_reorder_buf(buf);
 611        struct iwl_mvm_reorder_buf_entry *entries =
 612                &baid_data->entries[buf->queue * baid_data->entries_per_queue];
 613        int i;
 614        u16 sn = 0, index = 0;
 615        bool expired = false;
 616        bool cont = false;
 617
 618        spin_lock(&buf->lock);
 619
 620        if (!buf->num_stored || buf->removed) {
 621                spin_unlock(&buf->lock);
 622                return;
 623        }
 624
 625        for (i = 0; i < buf->buf_size ; i++) {
 626                index = (buf->head_sn + i) % buf->buf_size;
 627
 628                if (skb_queue_empty(&entries[index].e.frames)) {
 629                        /*
 630                         * If there is a hole and the next frame didn't expire
 631                         * we want to break and not advance SN
 632                         */
 633                        cont = false;
 634                        continue;
 635                }
 636                if (!cont &&
 637                    !time_after(jiffies, entries[index].e.reorder_time +
 638                                         RX_REORDER_BUF_TIMEOUT_MQ))
 639                        break;
 640
 641                expired = true;
 642                /* continue until next hole after this expired frames */
 643                cont = true;
 644                sn = ieee80211_sn_add(buf->head_sn, i + 1);
 645        }
 646
 647        if (expired) {
 648                struct ieee80211_sta *sta;
 649                struct iwl_mvm_sta *mvmsta;
 650                u8 sta_id = baid_data->sta_id;
 651
 652                rcu_read_lock();
 653                sta = rcu_dereference(buf->mvm->fw_id_to_mac_id[sta_id]);
 654                mvmsta = iwl_mvm_sta_from_mac80211(sta);
 655
 656                /* SN is set to the last expired frame + 1 */
 657                IWL_DEBUG_HT(buf->mvm,
 658                             "Releasing expired frames for sta %u, sn %d\n",
 659                             sta_id, sn);
 660                iwl_mvm_event_frame_timeout_callback(buf->mvm, mvmsta->vif,
 661                                                     sta, baid_data->tid);
 662                iwl_mvm_release_frames(buf->mvm, sta, NULL, baid_data,
 663                                       buf, sn, IWL_MVM_RELEASE_SEND_RSS_SYNC);
 664                rcu_read_unlock();
 665        } else {
 666                /*
 667                 * If no frame expired and there are stored frames, index is now
 668                 * pointing to the first unexpired frame - modify timer
 669                 * accordingly to this frame.
 670                 */
 671                mod_timer(&buf->reorder_timer,
 672                          entries[index].e.reorder_time +
 673                          1 + RX_REORDER_BUF_TIMEOUT_MQ);
 674        }
 675        spin_unlock(&buf->lock);
 676}
 677
 678static void iwl_mvm_del_ba(struct iwl_mvm *mvm, int queue,
 679                           struct iwl_mvm_delba_data *data)
 680{
 681        struct iwl_mvm_baid_data *ba_data;
 682        struct ieee80211_sta *sta;
 683        struct iwl_mvm_reorder_buffer *reorder_buf;
 684        u8 baid = data->baid;
 685
 686        if (WARN_ONCE(baid >= IWL_MAX_BAID, "invalid BAID: %x\n", baid))
 687                return;
 688
 689        rcu_read_lock();
 690
 691        ba_data = rcu_dereference(mvm->baid_map[baid]);
 692        if (WARN_ON_ONCE(!ba_data))
 693                goto out;
 694
 695        sta = rcu_dereference(mvm->fw_id_to_mac_id[ba_data->sta_id]);
 696        if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta)))
 697                goto out;
 698
 699        reorder_buf = &ba_data->reorder_buf[queue];
 700
 701        /* release all frames that are in the reorder buffer to the stack */
 702        spin_lock_bh(&reorder_buf->lock);
 703        iwl_mvm_release_frames(mvm, sta, NULL, ba_data, reorder_buf,
 704                               ieee80211_sn_add(reorder_buf->head_sn,
 705                                                reorder_buf->buf_size),
 706                               0);
 707        spin_unlock_bh(&reorder_buf->lock);
 708        del_timer_sync(&reorder_buf->reorder_timer);
 709
 710out:
 711        rcu_read_unlock();
 712}
 713
 714static void iwl_mvm_release_frames_from_notif(struct iwl_mvm *mvm,
 715                                              struct napi_struct *napi,
 716                                              u8 baid, u16 nssn, int queue,
 717                                              u32 flags)
 718{
 719        struct ieee80211_sta *sta;
 720        struct iwl_mvm_reorder_buffer *reorder_buf;
 721        struct iwl_mvm_baid_data *ba_data;
 722
 723        IWL_DEBUG_HT(mvm, "Frame release notification for BAID %u, NSSN %d\n",
 724                     baid, nssn);
 725
 726        if (WARN_ON_ONCE(baid == IWL_RX_REORDER_DATA_INVALID_BAID ||
 727                         baid >= ARRAY_SIZE(mvm->baid_map)))
 728                return;
 729
 730        rcu_read_lock();
 731
 732        ba_data = rcu_dereference(mvm->baid_map[baid]);
 733        if (WARN_ON_ONCE(!ba_data))
 734                goto out;
 735
 736        sta = rcu_dereference(mvm->fw_id_to_mac_id[ba_data->sta_id]);
 737        if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta)))
 738                goto out;
 739
 740        reorder_buf = &ba_data->reorder_buf[queue];
 741
 742        spin_lock_bh(&reorder_buf->lock);
 743        iwl_mvm_release_frames(mvm, sta, napi, ba_data,
 744                               reorder_buf, nssn, flags);
 745        spin_unlock_bh(&reorder_buf->lock);
 746
 747out:
 748        rcu_read_unlock();
 749}
 750
 751static void iwl_mvm_nssn_sync(struct iwl_mvm *mvm,
 752                              struct napi_struct *napi, int queue,
 753                              const struct iwl_mvm_nssn_sync_data *data)
 754{
 755        iwl_mvm_release_frames_from_notif(mvm, napi, data->baid,
 756                                          data->nssn, queue,
 757                                          IWL_MVM_RELEASE_FROM_RSS_SYNC);
 758}
 759
 760void iwl_mvm_rx_queue_notif(struct iwl_mvm *mvm, struct napi_struct *napi,
 761                            struct iwl_rx_cmd_buffer *rxb, int queue)
 762{
 763        struct iwl_rx_packet *pkt = rxb_addr(rxb);
 764        struct iwl_rxq_sync_notification *notif;
 765        struct iwl_mvm_internal_rxq_notif *internal_notif;
 766
 767        notif = (void *)pkt->data;
 768        internal_notif = (void *)notif->payload;
 769
 770        if (internal_notif->sync &&
 771            mvm->queue_sync_cookie != internal_notif->cookie) {
 772                WARN_ONCE(1, "Received expired RX queue sync message\n");
 773                return;
 774        }
 775
 776        switch (internal_notif->type) {
 777        case IWL_MVM_RXQ_EMPTY:
 778                break;
 779        case IWL_MVM_RXQ_NOTIF_DEL_BA:
 780                iwl_mvm_del_ba(mvm, queue, (void *)internal_notif->data);
 781                break;
 782        case IWL_MVM_RXQ_NSSN_SYNC:
 783                iwl_mvm_nssn_sync(mvm, napi, queue,
 784                                  (void *)internal_notif->data);
 785                break;
 786        default:
 787                WARN_ONCE(1, "Invalid identifier %d", internal_notif->type);
 788        }
 789
 790        if (internal_notif->sync &&
 791            !atomic_dec_return(&mvm->queue_sync_counter))
 792                wake_up(&mvm->rx_sync_waitq);
 793}
 794
 795static void iwl_mvm_oldsn_workaround(struct iwl_mvm *mvm,
 796                                     struct ieee80211_sta *sta, int tid,
 797                                     struct iwl_mvm_reorder_buffer *buffer,
 798                                     u32 reorder, u32 gp2, int queue)
 799{
 800        struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
 801
 802        if (gp2 != buffer->consec_oldsn_ampdu_gp2) {
 803                /* we have a new (A-)MPDU ... */
 804
 805                /*
 806                 * reset counter to 0 if we didn't have any oldsn in
 807                 * the last A-MPDU (as detected by GP2 being identical)
 808                 */
 809                if (!buffer->consec_oldsn_prev_drop)
 810                        buffer->consec_oldsn_drops = 0;
 811
 812                /* either way, update our tracking state */
 813                buffer->consec_oldsn_ampdu_gp2 = gp2;
 814        } else if (buffer->consec_oldsn_prev_drop) {
 815                /*
 816                 * tracking state didn't change, and we had an old SN
 817                 * indication before - do nothing in this case, we
 818                 * already noted this one down and are waiting for the
 819                 * next A-MPDU (by GP2)
 820                 */
 821                return;
 822        }
 823
 824        /* return unless this MPDU has old SN */
 825        if (!(reorder & IWL_RX_MPDU_REORDER_BA_OLD_SN))
 826                return;
 827
 828        /* update state */
 829        buffer->consec_oldsn_prev_drop = 1;
 830        buffer->consec_oldsn_drops++;
 831
 832        /* if limit is reached, send del BA and reset state */
 833        if (buffer->consec_oldsn_drops == IWL_MVM_AMPDU_CONSEC_DROPS_DELBA) {
 834                IWL_WARN(mvm,
 835                         "reached %d old SN frames from %pM on queue %d, stopping BA session on TID %d\n",
 836                         IWL_MVM_AMPDU_CONSEC_DROPS_DELBA,
 837                         sta->addr, queue, tid);
 838                ieee80211_stop_rx_ba_session(mvmsta->vif, BIT(tid), sta->addr);
 839                buffer->consec_oldsn_prev_drop = 0;
 840                buffer->consec_oldsn_drops = 0;
 841        }
 842}
 843
 844/*
 845 * Returns true if the MPDU was buffered\dropped, false if it should be passed
 846 * to upper layer.
 847 */
 848static bool iwl_mvm_reorder(struct iwl_mvm *mvm,
 849                            struct napi_struct *napi,
 850                            int queue,
 851                            struct ieee80211_sta *sta,
 852                            struct sk_buff *skb,
 853                            struct iwl_rx_mpdu_desc *desc)
 854{
 855        struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
 856        struct ieee80211_hdr *hdr = iwl_mvm_skb_get_hdr(skb);
 857        struct iwl_mvm_sta *mvm_sta;
 858        struct iwl_mvm_baid_data *baid_data;
 859        struct iwl_mvm_reorder_buffer *buffer;
 860        struct sk_buff *tail;
 861        u32 reorder = le32_to_cpu(desc->reorder_data);
 862        bool amsdu = desc->mac_flags2 & IWL_RX_MPDU_MFLG2_AMSDU;
 863        bool last_subframe =
 864                desc->amsdu_info & IWL_RX_MPDU_AMSDU_LAST_SUBFRAME;
 865        u8 tid = ieee80211_get_tid(hdr);
 866        u8 sub_frame_idx = desc->amsdu_info &
 867                           IWL_RX_MPDU_AMSDU_SUBFRAME_IDX_MASK;
 868        struct iwl_mvm_reorder_buf_entry *entries;
 869        int index;
 870        u16 nssn, sn;
 871        u8 baid;
 872
 873        baid = (reorder & IWL_RX_MPDU_REORDER_BAID_MASK) >>
 874                IWL_RX_MPDU_REORDER_BAID_SHIFT;
 875
 876        /*
 877         * This also covers the case of receiving a Block Ack Request
 878         * outside a BA session; we'll pass it to mac80211 and that
 879         * then sends a delBA action frame.
 880         * This also covers pure monitor mode, in which case we won't
 881         * have any BA sessions.
 882         */
 883        if (baid == IWL_RX_REORDER_DATA_INVALID_BAID)
 884                return false;
 885
 886        /* no sta yet */
 887        if (WARN_ONCE(IS_ERR_OR_NULL(sta),
 888                      "Got valid BAID without a valid station assigned\n"))
 889                return false;
 890
 891        mvm_sta = iwl_mvm_sta_from_mac80211(sta);
 892
 893        /* not a data packet or a bar */
 894        if (!ieee80211_is_back_req(hdr->frame_control) &&
 895            (!ieee80211_is_data_qos(hdr->frame_control) ||
 896             is_multicast_ether_addr(hdr->addr1)))
 897                return false;
 898
 899        if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
 900                return false;
 901
 902        baid_data = rcu_dereference(mvm->baid_map[baid]);
 903        if (!baid_data) {
 904                IWL_DEBUG_RX(mvm,
 905                             "Got valid BAID but no baid allocated, bypass the re-ordering buffer. Baid %d reorder 0x%x\n",
 906                              baid, reorder);
 907                return false;
 908        }
 909
 910        if (WARN(tid != baid_data->tid || mvm_sta->sta_id != baid_data->sta_id,
 911                 "baid 0x%x is mapped to sta:%d tid:%d, but was received for sta:%d tid:%d\n",
 912                 baid, baid_data->sta_id, baid_data->tid, mvm_sta->sta_id,
 913                 tid))
 914                return false;
 915
 916        nssn = reorder & IWL_RX_MPDU_REORDER_NSSN_MASK;
 917        sn = (reorder & IWL_RX_MPDU_REORDER_SN_MASK) >>
 918                IWL_RX_MPDU_REORDER_SN_SHIFT;
 919
 920        buffer = &baid_data->reorder_buf[queue];
 921        entries = &baid_data->entries[queue * baid_data->entries_per_queue];
 922
 923        spin_lock_bh(&buffer->lock);
 924
 925        if (!buffer->valid) {
 926                if (reorder & IWL_RX_MPDU_REORDER_BA_OLD_SN) {
 927                        spin_unlock_bh(&buffer->lock);
 928                        return false;
 929                }
 930                buffer->valid = true;
 931        }
 932
 933        if (ieee80211_is_back_req(hdr->frame_control)) {
 934                iwl_mvm_release_frames(mvm, sta, napi, baid_data,
 935                                       buffer, nssn, 0);
 936                goto drop;
 937        }
 938
 939        /*
 940         * If there was a significant jump in the nssn - adjust.
 941         * If the SN is smaller than the NSSN it might need to first go into
 942         * the reorder buffer, in which case we just release up to it and the
 943         * rest of the function will take care of storing it and releasing up to
 944         * the nssn.
 945         * This should not happen. This queue has been lagging and it should
 946         * have been updated by a IWL_MVM_RXQ_NSSN_SYNC notification. Be nice
 947         * and update the other queues.
 948         */
 949        if (!iwl_mvm_is_sn_less(nssn, buffer->head_sn + buffer->buf_size,
 950                                buffer->buf_size) ||
 951            !ieee80211_sn_less(sn, buffer->head_sn + buffer->buf_size)) {
 952                u16 min_sn = ieee80211_sn_less(sn, nssn) ? sn : nssn;
 953
 954                iwl_mvm_release_frames(mvm, sta, napi, baid_data, buffer,
 955                                       min_sn, IWL_MVM_RELEASE_SEND_RSS_SYNC);
 956        }
 957
 958        iwl_mvm_oldsn_workaround(mvm, sta, tid, buffer, reorder,
 959                                 rx_status->device_timestamp, queue);
 960
 961        /* drop any oudated packets */
 962        if (ieee80211_sn_less(sn, buffer->head_sn))
 963                goto drop;
 964
 965        /* release immediately if allowed by nssn and no stored frames */
 966        if (!buffer->num_stored && ieee80211_sn_less(sn, nssn)) {
 967                if (iwl_mvm_is_sn_less(buffer->head_sn, nssn,
 968                                       buffer->buf_size) &&
 969                   (!amsdu || last_subframe)) {
 970                        /*
 971                         * If we crossed the 2048 or 0 SN, notify all the
 972                         * queues. This is done in order to avoid having a
 973                         * head_sn that lags behind for too long. When that
 974                         * happens, we can get to a situation where the head_sn
 975                         * is within the interval [nssn - buf_size : nssn]
 976                         * which will make us think that the nssn is a packet
 977                         * that we already freed because of the reordering
 978                         * buffer and we will ignore it. So maintain the
 979                         * head_sn somewhat updated across all the queues:
 980                         * when it crosses 0 and 2048.
 981                         */
 982                        if (sn == 2048 || sn == 0)
 983                                iwl_mvm_sync_nssn(mvm, baid, sn);
 984                        buffer->head_sn = nssn;
 985                }
 986                /* No need to update AMSDU last SN - we are moving the head */
 987                spin_unlock_bh(&buffer->lock);
 988                return false;
 989        }
 990
 991        /*
 992         * release immediately if there are no stored frames, and the sn is
 993         * equal to the head.
 994         * This can happen due to reorder timer, where NSSN is behind head_sn.
 995         * When we released everything, and we got the next frame in the
 996         * sequence, according to the NSSN we can't release immediately,
 997         * while technically there is no hole and we can move forward.
 998         */
 999        if (!buffer->num_stored && sn == buffer->head_sn) {
1000                if (!amsdu || last_subframe) {
1001                        if (sn == 2048 || sn == 0)
1002                                iwl_mvm_sync_nssn(mvm, baid, sn);
1003                        buffer->head_sn = ieee80211_sn_inc(buffer->head_sn);
1004                }
1005                /* No need to update AMSDU last SN - we are moving the head */
1006                spin_unlock_bh(&buffer->lock);
1007                return false;
1008        }
1009
1010        index = sn % buffer->buf_size;
1011
1012        /*
1013         * Check if we already stored this frame
1014         * As AMSDU is either received or not as whole, logic is simple:
1015         * If we have frames in that position in the buffer and the last frame
1016         * originated from AMSDU had a different SN then it is a retransmission.
1017         * If it is the same SN then if the subframe index is incrementing it
1018         * is the same AMSDU - otherwise it is a retransmission.
1019         */
1020        tail = skb_peek_tail(&entries[index].e.frames);
1021        if (tail && !amsdu)
1022                goto drop;
1023        else if (tail && (sn != buffer->last_amsdu ||
1024                          buffer->last_sub_index >= sub_frame_idx))
1025                goto drop;
1026
1027        /* put in reorder buffer */
1028        __skb_queue_tail(&entries[index].e.frames, skb);
1029        buffer->num_stored++;
1030        entries[index].e.reorder_time = jiffies;
1031
1032        if (amsdu) {
1033                buffer->last_amsdu = sn;
1034                buffer->last_sub_index = sub_frame_idx;
1035        }
1036
1037        /*
1038         * We cannot trust NSSN for AMSDU sub-frames that are not the last.
1039         * The reason is that NSSN advances on the first sub-frame, and may
1040         * cause the reorder buffer to advance before all the sub-frames arrive.
1041         * Example: reorder buffer contains SN 0 & 2, and we receive AMSDU with
1042         * SN 1. NSSN for first sub frame will be 3 with the result of driver
1043         * releasing SN 0,1, 2. When sub-frame 1 arrives - reorder buffer is
1044         * already ahead and it will be dropped.
1045         * If the last sub-frame is not on this queue - we will get frame
1046         * release notification with up to date NSSN.
1047         */
1048        if (!amsdu || last_subframe)
1049                iwl_mvm_release_frames(mvm, sta, napi, baid_data,
1050                                       buffer, nssn,
1051                                       IWL_MVM_RELEASE_SEND_RSS_SYNC);
1052
1053        spin_unlock_bh(&buffer->lock);
1054        return true;
1055
1056drop:
1057        kfree_skb(skb);
1058        spin_unlock_bh(&buffer->lock);
1059        return true;
1060}
1061
1062static void iwl_mvm_agg_rx_received(struct iwl_mvm *mvm,
1063                                    u32 reorder_data, u8 baid)
1064{
1065        unsigned long now = jiffies;
1066        unsigned long timeout;
1067        struct iwl_mvm_baid_data *data;
1068
1069        rcu_read_lock();
1070
1071        data = rcu_dereference(mvm->baid_map[baid]);
1072        if (!data) {
1073                IWL_DEBUG_RX(mvm,
1074                             "Got valid BAID but no baid allocated, bypass the re-ordering buffer. Baid %d reorder 0x%x\n",
1075                              baid, reorder_data);
1076                goto out;
1077        }
1078
1079        if (!data->timeout)
1080                goto out;
1081
1082        timeout = data->timeout;
1083        /*
1084         * Do not update last rx all the time to avoid cache bouncing
1085         * between the rx queues.
1086         * Update it every timeout. Worst case is the session will
1087         * expire after ~ 2 * timeout, which doesn't matter that much.
1088         */
1089        if (time_before(data->last_rx + TU_TO_JIFFIES(timeout), now))
1090                /* Update is atomic */
1091                data->last_rx = now;
1092
1093out:
1094        rcu_read_unlock();
1095}
1096
1097static void iwl_mvm_flip_address(u8 *addr)
1098{
1099        int i;
1100        u8 mac_addr[ETH_ALEN];
1101
1102        for (i = 0; i < ETH_ALEN; i++)
1103                mac_addr[i] = addr[ETH_ALEN - i - 1];
1104        ether_addr_copy(addr, mac_addr);
1105}
1106
1107struct iwl_mvm_rx_phy_data {
1108        enum iwl_rx_phy_info_type info_type;
1109        __le32 d0, d1, d2, d3;
1110        __le16 d4;
1111};
1112
1113static void iwl_mvm_decode_he_mu_ext(struct iwl_mvm *mvm,
1114                                     struct iwl_mvm_rx_phy_data *phy_data,
1115                                     u32 rate_n_flags,
1116                                     struct ieee80211_radiotap_he_mu *he_mu)
1117{
1118        u32 phy_data2 = le32_to_cpu(phy_data->d2);
1119        u32 phy_data3 = le32_to_cpu(phy_data->d3);
1120        u16 phy_data4 = le16_to_cpu(phy_data->d4);
1121
1122        if (FIELD_GET(IWL_RX_PHY_DATA4_HE_MU_EXT_CH1_CRC_OK, phy_data4)) {
1123                he_mu->flags1 |=
1124                        cpu_to_le16(IEEE80211_RADIOTAP_HE_MU_FLAGS1_CH1_RU_KNOWN |
1125                                    IEEE80211_RADIOTAP_HE_MU_FLAGS1_CH1_CTR_26T_RU_KNOWN);
1126
1127                he_mu->flags1 |=
1128                        le16_encode_bits(FIELD_GET(IWL_RX_PHY_DATA4_HE_MU_EXT_CH1_CTR_RU,
1129                                                   phy_data4),
1130                                         IEEE80211_RADIOTAP_HE_MU_FLAGS1_CH1_CTR_26T_RU);
1131
1132                he_mu->ru_ch1[0] = FIELD_GET(IWL_RX_PHY_DATA2_HE_MU_EXT_CH1_RU0,
1133                                             phy_data2);
1134                he_mu->ru_ch1[1] = FIELD_GET(IWL_RX_PHY_DATA3_HE_MU_EXT_CH1_RU1,
1135                                             phy_data3);
1136                he_mu->ru_ch1[2] = FIELD_GET(IWL_RX_PHY_DATA2_HE_MU_EXT_CH1_RU2,
1137                                             phy_data2);
1138                he_mu->ru_ch1[3] = FIELD_GET(IWL_RX_PHY_DATA3_HE_MU_EXT_CH1_RU3,
1139                                             phy_data3);
1140        }
1141
1142        if (FIELD_GET(IWL_RX_PHY_DATA4_HE_MU_EXT_CH2_CRC_OK, phy_data4) &&
1143            (rate_n_flags & RATE_MCS_CHAN_WIDTH_MSK) != RATE_MCS_CHAN_WIDTH_20) {
1144                he_mu->flags1 |=
1145                        cpu_to_le16(IEEE80211_RADIOTAP_HE_MU_FLAGS1_CH2_RU_KNOWN |
1146                                    IEEE80211_RADIOTAP_HE_MU_FLAGS1_CH2_CTR_26T_RU_KNOWN);
1147
1148                he_mu->flags2 |=
1149                        le16_encode_bits(FIELD_GET(IWL_RX_PHY_DATA4_HE_MU_EXT_CH2_CTR_RU,
1150                                                   phy_data4),
1151                                         IEEE80211_RADIOTAP_HE_MU_FLAGS2_CH2_CTR_26T_RU);
1152
1153                he_mu->ru_ch2[0] = FIELD_GET(IWL_RX_PHY_DATA2_HE_MU_EXT_CH2_RU0,
1154                                             phy_data2);
1155                he_mu->ru_ch2[1] = FIELD_GET(IWL_RX_PHY_DATA3_HE_MU_EXT_CH2_RU1,
1156                                             phy_data3);
1157                he_mu->ru_ch2[2] = FIELD_GET(IWL_RX_PHY_DATA2_HE_MU_EXT_CH2_RU2,
1158                                             phy_data2);
1159                he_mu->ru_ch2[3] = FIELD_GET(IWL_RX_PHY_DATA3_HE_MU_EXT_CH2_RU3,
1160                                             phy_data3);
1161        }
1162}
1163
1164static void
1165iwl_mvm_decode_he_phy_ru_alloc(struct iwl_mvm_rx_phy_data *phy_data,
1166                               u32 rate_n_flags,
1167                               struct ieee80211_radiotap_he *he,
1168                               struct ieee80211_radiotap_he_mu *he_mu,
1169                               struct ieee80211_rx_status *rx_status)
1170{
1171        /*
1172         * Unfortunately, we have to leave the mac80211 data
1173         * incorrect for the case that we receive an HE-MU
1174         * transmission and *don't* have the HE phy data (due
1175         * to the bits being used for TSF). This shouldn't
1176         * happen though as management frames where we need
1177         * the TSF/timers are not be transmitted in HE-MU.
1178         */
1179        u8 ru = le32_get_bits(phy_data->d1, IWL_RX_PHY_DATA1_HE_RU_ALLOC_MASK);
1180        u32 he_type = rate_n_flags & RATE_MCS_HE_TYPE_MSK;
1181        u8 offs = 0;
1182
1183        rx_status->bw = RATE_INFO_BW_HE_RU;
1184
1185        he->data1 |= cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA1_BW_RU_ALLOC_KNOWN);
1186
1187        switch (ru) {
1188        case 0 ... 36:
1189                rx_status->he_ru = NL80211_RATE_INFO_HE_RU_ALLOC_26;
1190                offs = ru;
1191                break;
1192        case 37 ... 52:
1193                rx_status->he_ru = NL80211_RATE_INFO_HE_RU_ALLOC_52;
1194                offs = ru - 37;
1195                break;
1196        case 53 ... 60:
1197                rx_status->he_ru = NL80211_RATE_INFO_HE_RU_ALLOC_106;
1198                offs = ru - 53;
1199                break;
1200        case 61 ... 64:
1201                rx_status->he_ru = NL80211_RATE_INFO_HE_RU_ALLOC_242;
1202                offs = ru - 61;
1203                break;
1204        case 65 ... 66:
1205                rx_status->he_ru = NL80211_RATE_INFO_HE_RU_ALLOC_484;
1206                offs = ru - 65;
1207                break;
1208        case 67:
1209                rx_status->he_ru = NL80211_RATE_INFO_HE_RU_ALLOC_996;
1210                break;
1211        case 68:
1212                rx_status->he_ru = NL80211_RATE_INFO_HE_RU_ALLOC_2x996;
1213                break;
1214        }
1215        he->data2 |= le16_encode_bits(offs,
1216                                      IEEE80211_RADIOTAP_HE_DATA2_RU_OFFSET);
1217        he->data2 |= cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA2_PRISEC_80_KNOWN |
1218                                 IEEE80211_RADIOTAP_HE_DATA2_RU_OFFSET_KNOWN);
1219        if (phy_data->d1 & cpu_to_le32(IWL_RX_PHY_DATA1_HE_RU_ALLOC_SEC80))
1220                he->data2 |=
1221                        cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA2_PRISEC_80_SEC);
1222
1223#define CHECK_BW(bw) \
1224        BUILD_BUG_ON(IEEE80211_RADIOTAP_HE_MU_FLAGS2_BW_FROM_SIG_A_BW_ ## bw ## MHZ != \
1225                     RATE_MCS_CHAN_WIDTH_##bw >> RATE_MCS_CHAN_WIDTH_POS); \
1226        BUILD_BUG_ON(IEEE80211_RADIOTAP_HE_DATA6_TB_PPDU_BW_ ## bw ## MHZ != \
1227                     RATE_MCS_CHAN_WIDTH_##bw >> RATE_MCS_CHAN_WIDTH_POS)
1228        CHECK_BW(20);
1229        CHECK_BW(40);
1230        CHECK_BW(80);
1231        CHECK_BW(160);
1232
1233        if (he_mu)
1234                he_mu->flags2 |=
1235                        le16_encode_bits(FIELD_GET(RATE_MCS_CHAN_WIDTH_MSK,
1236                                                   rate_n_flags),
1237                                         IEEE80211_RADIOTAP_HE_MU_FLAGS2_BW_FROM_SIG_A_BW);
1238        else if (he_type == RATE_MCS_HE_TYPE_TRIG)
1239                he->data6 |=
1240                        cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA6_TB_PPDU_BW_KNOWN) |
1241                        le16_encode_bits(FIELD_GET(RATE_MCS_CHAN_WIDTH_MSK,
1242                                                   rate_n_flags),
1243                                         IEEE80211_RADIOTAP_HE_DATA6_TB_PPDU_BW);
1244}
1245
1246static void iwl_mvm_decode_he_phy_data(struct iwl_mvm *mvm,
1247                                       struct iwl_mvm_rx_phy_data *phy_data,
1248                                       struct ieee80211_radiotap_he *he,
1249                                       struct ieee80211_radiotap_he_mu *he_mu,
1250                                       struct ieee80211_rx_status *rx_status,
1251                                       u32 rate_n_flags, int queue)
1252{
1253        switch (phy_data->info_type) {
1254        case IWL_RX_PHY_INFO_TYPE_NONE:
1255        case IWL_RX_PHY_INFO_TYPE_CCK:
1256        case IWL_RX_PHY_INFO_TYPE_OFDM_LGCY:
1257        case IWL_RX_PHY_INFO_TYPE_HT:
1258        case IWL_RX_PHY_INFO_TYPE_VHT_SU:
1259        case IWL_RX_PHY_INFO_TYPE_VHT_MU:
1260                return;
1261        case IWL_RX_PHY_INFO_TYPE_HE_TB_EXT:
1262                he->data1 |= cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA1_SPTL_REUSE_KNOWN |
1263                                         IEEE80211_RADIOTAP_HE_DATA1_SPTL_REUSE2_KNOWN |
1264                                         IEEE80211_RADIOTAP_HE_DATA1_SPTL_REUSE3_KNOWN |
1265                                         IEEE80211_RADIOTAP_HE_DATA1_SPTL_REUSE4_KNOWN);
1266                he->data4 |= le16_encode_bits(le32_get_bits(phy_data->d2,
1267                                                            IWL_RX_PHY_DATA2_HE_TB_EXT_SPTL_REUSE1),
1268                                              IEEE80211_RADIOTAP_HE_DATA4_TB_SPTL_REUSE1);
1269                he->data4 |= le16_encode_bits(le32_get_bits(phy_data->d2,
1270                                                            IWL_RX_PHY_DATA2_HE_TB_EXT_SPTL_REUSE2),
1271                                              IEEE80211_RADIOTAP_HE_DATA4_TB_SPTL_REUSE2);
1272                he->data4 |= le16_encode_bits(le32_get_bits(phy_data->d2,
1273                                                            IWL_RX_PHY_DATA2_HE_TB_EXT_SPTL_REUSE3),
1274                                              IEEE80211_RADIOTAP_HE_DATA4_TB_SPTL_REUSE3);
1275                he->data4 |= le16_encode_bits(le32_get_bits(phy_data->d2,
1276                                                            IWL_RX_PHY_DATA2_HE_TB_EXT_SPTL_REUSE4),
1277                                              IEEE80211_RADIOTAP_HE_DATA4_TB_SPTL_REUSE4);
1278                /* fall through */
1279        case IWL_RX_PHY_INFO_TYPE_HE_SU:
1280        case IWL_RX_PHY_INFO_TYPE_HE_MU:
1281        case IWL_RX_PHY_INFO_TYPE_HE_MU_EXT:
1282        case IWL_RX_PHY_INFO_TYPE_HE_TB:
1283                /* HE common */
1284                he->data1 |= cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA1_LDPC_XSYMSEG_KNOWN |
1285                                         IEEE80211_RADIOTAP_HE_DATA1_DOPPLER_KNOWN |
1286                                         IEEE80211_RADIOTAP_HE_DATA1_BSS_COLOR_KNOWN);
1287                he->data2 |= cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA2_PRE_FEC_PAD_KNOWN |
1288                                         IEEE80211_RADIOTAP_HE_DATA2_PE_DISAMBIG_KNOWN |
1289                                         IEEE80211_RADIOTAP_HE_DATA2_TXOP_KNOWN |
1290                                         IEEE80211_RADIOTAP_HE_DATA2_NUM_LTF_SYMS_KNOWN);
1291                he->data3 |= le16_encode_bits(le32_get_bits(phy_data->d0,
1292                                                            IWL_RX_PHY_DATA0_HE_BSS_COLOR_MASK),
1293                                              IEEE80211_RADIOTAP_HE_DATA3_BSS_COLOR);
1294                if (phy_data->info_type != IWL_RX_PHY_INFO_TYPE_HE_TB &&
1295                    phy_data->info_type != IWL_RX_PHY_INFO_TYPE_HE_TB_EXT) {
1296                        he->data1 |= cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA1_UL_DL_KNOWN);
1297                        he->data3 |= le16_encode_bits(le32_get_bits(phy_data->d0,
1298                                                            IWL_RX_PHY_DATA0_HE_UPLINK),
1299                                                      IEEE80211_RADIOTAP_HE_DATA3_UL_DL);
1300                }
1301                he->data3 |= le16_encode_bits(le32_get_bits(phy_data->d0,
1302                                                            IWL_RX_PHY_DATA0_HE_LDPC_EXT_SYM),
1303                                              IEEE80211_RADIOTAP_HE_DATA3_LDPC_XSYMSEG);
1304                he->data5 |= le16_encode_bits(le32_get_bits(phy_data->d0,
1305                                                            IWL_RX_PHY_DATA0_HE_PRE_FEC_PAD_MASK),
1306                                              IEEE80211_RADIOTAP_HE_DATA5_PRE_FEC_PAD);
1307                he->data5 |= le16_encode_bits(le32_get_bits(phy_data->d0,
1308                                                            IWL_RX_PHY_DATA0_HE_PE_DISAMBIG),
1309                                              IEEE80211_RADIOTAP_HE_DATA5_PE_DISAMBIG);
1310                he->data5 |= le16_encode_bits(le32_get_bits(phy_data->d1,
1311                                                            IWL_RX_PHY_DATA1_HE_LTF_NUM_MASK),
1312                                              IEEE80211_RADIOTAP_HE_DATA5_NUM_LTF_SYMS);
1313                he->data6 |= le16_encode_bits(le32_get_bits(phy_data->d0,
1314                                                            IWL_RX_PHY_DATA0_HE_TXOP_DUR_MASK),
1315                                              IEEE80211_RADIOTAP_HE_DATA6_TXOP);
1316                he->data6 |= le16_encode_bits(le32_get_bits(phy_data->d0,
1317                                                            IWL_RX_PHY_DATA0_HE_DOPPLER),
1318                                              IEEE80211_RADIOTAP_HE_DATA6_DOPPLER);
1319                break;
1320        }
1321
1322        switch (phy_data->info_type) {
1323        case IWL_RX_PHY_INFO_TYPE_HE_MU_EXT:
1324        case IWL_RX_PHY_INFO_TYPE_HE_MU:
1325        case IWL_RX_PHY_INFO_TYPE_HE_SU:
1326                he->data1 |= cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA1_SPTL_REUSE_KNOWN);
1327                he->data4 |= le16_encode_bits(le32_get_bits(phy_data->d0,
1328                                                            IWL_RX_PHY_DATA0_HE_SPATIAL_REUSE_MASK),
1329                                              IEEE80211_RADIOTAP_HE_DATA4_SU_MU_SPTL_REUSE);
1330                break;
1331        default:
1332                /* nothing here */
1333                break;
1334        }
1335
1336        switch (phy_data->info_type) {
1337        case IWL_RX_PHY_INFO_TYPE_HE_MU_EXT:
1338                he_mu->flags1 |=
1339                        le16_encode_bits(le16_get_bits(phy_data->d4,
1340                                                       IWL_RX_PHY_DATA4_HE_MU_EXT_SIGB_DCM),
1341                                         IEEE80211_RADIOTAP_HE_MU_FLAGS1_SIG_B_DCM);
1342                he_mu->flags1 |=
1343                        le16_encode_bits(le16_get_bits(phy_data->d4,
1344                                                       IWL_RX_PHY_DATA4_HE_MU_EXT_SIGB_MCS_MASK),
1345                                         IEEE80211_RADIOTAP_HE_MU_FLAGS1_SIG_B_MCS);
1346                he_mu->flags2 |=
1347                        le16_encode_bits(le16_get_bits(phy_data->d4,
1348                                                       IWL_RX_PHY_DATA4_HE_MU_EXT_PREAMBLE_PUNC_TYPE_MASK),
1349                                         IEEE80211_RADIOTAP_HE_MU_FLAGS2_PUNC_FROM_SIG_A_BW);
1350                iwl_mvm_decode_he_mu_ext(mvm, phy_data, rate_n_flags, he_mu);
1351                /* fall through */
1352        case IWL_RX_PHY_INFO_TYPE_HE_MU:
1353                he_mu->flags2 |=
1354                        le16_encode_bits(le32_get_bits(phy_data->d1,
1355                                                       IWL_RX_PHY_DATA1_HE_MU_SIBG_SYM_OR_USER_NUM_MASK),
1356                                         IEEE80211_RADIOTAP_HE_MU_FLAGS2_SIG_B_SYMS_USERS);
1357                he_mu->flags2 |=
1358                        le16_encode_bits(le32_get_bits(phy_data->d1,
1359                                                       IWL_RX_PHY_DATA1_HE_MU_SIGB_COMPRESSION),
1360                                         IEEE80211_RADIOTAP_HE_MU_FLAGS2_SIG_B_COMP);
1361                /* fall through */
1362        case IWL_RX_PHY_INFO_TYPE_HE_TB:
1363        case IWL_RX_PHY_INFO_TYPE_HE_TB_EXT:
1364                iwl_mvm_decode_he_phy_ru_alloc(phy_data, rate_n_flags,
1365                                               he, he_mu, rx_status);
1366                break;
1367        case IWL_RX_PHY_INFO_TYPE_HE_SU:
1368                he->data1 |= cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA1_BEAM_CHANGE_KNOWN);
1369                he->data3 |= le16_encode_bits(le32_get_bits(phy_data->d0,
1370                                                            IWL_RX_PHY_DATA0_HE_BEAM_CHNG),
1371                                              IEEE80211_RADIOTAP_HE_DATA3_BEAM_CHANGE);
1372                break;
1373        default:
1374                /* nothing */
1375                break;
1376        }
1377}
1378
1379static void iwl_mvm_rx_he(struct iwl_mvm *mvm, struct sk_buff *skb,
1380                          struct iwl_mvm_rx_phy_data *phy_data,
1381                          u32 rate_n_flags, u16 phy_info, int queue)
1382{
1383        struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
1384        struct ieee80211_radiotap_he *he = NULL;
1385        struct ieee80211_radiotap_he_mu *he_mu = NULL;
1386        u32 he_type = rate_n_flags & RATE_MCS_HE_TYPE_MSK;
1387        u8 stbc, ltf;
1388        static const struct ieee80211_radiotap_he known = {
1389                .data1 = cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA1_DATA_MCS_KNOWN |
1390                                     IEEE80211_RADIOTAP_HE_DATA1_DATA_DCM_KNOWN |
1391                                     IEEE80211_RADIOTAP_HE_DATA1_STBC_KNOWN |
1392                                     IEEE80211_RADIOTAP_HE_DATA1_CODING_KNOWN),
1393                .data2 = cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA2_GI_KNOWN |
1394                                     IEEE80211_RADIOTAP_HE_DATA2_TXBF_KNOWN),
1395        };
1396        static const struct ieee80211_radiotap_he_mu mu_known = {
1397                .flags1 = cpu_to_le16(IEEE80211_RADIOTAP_HE_MU_FLAGS1_SIG_B_MCS_KNOWN |
1398                                      IEEE80211_RADIOTAP_HE_MU_FLAGS1_SIG_B_DCM_KNOWN |
1399                                      IEEE80211_RADIOTAP_HE_MU_FLAGS1_SIG_B_SYMS_USERS_KNOWN |
1400                                      IEEE80211_RADIOTAP_HE_MU_FLAGS1_SIG_B_COMP_KNOWN),
1401                .flags2 = cpu_to_le16(IEEE80211_RADIOTAP_HE_MU_FLAGS2_PUNC_FROM_SIG_A_BW_KNOWN |
1402                                      IEEE80211_RADIOTAP_HE_MU_FLAGS2_BW_FROM_SIG_A_BW_KNOWN),
1403        };
1404
1405        he = skb_put_data(skb, &known, sizeof(known));
1406        rx_status->flag |= RX_FLAG_RADIOTAP_HE;
1407
1408        if (phy_data->info_type == IWL_RX_PHY_INFO_TYPE_HE_MU ||
1409            phy_data->info_type == IWL_RX_PHY_INFO_TYPE_HE_MU_EXT) {
1410                he_mu = skb_put_data(skb, &mu_known, sizeof(mu_known));
1411                rx_status->flag |= RX_FLAG_RADIOTAP_HE_MU;
1412        }
1413
1414        /* report the AMPDU-EOF bit on single frames */
1415        if (!queue && !(phy_info & IWL_RX_MPDU_PHY_AMPDU)) {
1416                rx_status->flag |= RX_FLAG_AMPDU_DETAILS;
1417                rx_status->flag |= RX_FLAG_AMPDU_EOF_BIT_KNOWN;
1418                if (phy_data->d0 & cpu_to_le32(IWL_RX_PHY_DATA0_HE_DELIM_EOF))
1419                        rx_status->flag |= RX_FLAG_AMPDU_EOF_BIT;
1420        }
1421
1422        if (phy_info & IWL_RX_MPDU_PHY_TSF_OVERLOAD)
1423                iwl_mvm_decode_he_phy_data(mvm, phy_data, he, he_mu, rx_status,
1424                                           rate_n_flags, queue);
1425
1426        /* update aggregation data for monitor sake on default queue */
1427        if (!queue && (phy_info & IWL_RX_MPDU_PHY_TSF_OVERLOAD) &&
1428            (phy_info & IWL_RX_MPDU_PHY_AMPDU)) {
1429                bool toggle_bit = phy_info & IWL_RX_MPDU_PHY_AMPDU_TOGGLE;
1430
1431                /* toggle is switched whenever new aggregation starts */
1432                if (toggle_bit != mvm->ampdu_toggle) {
1433                        rx_status->flag |= RX_FLAG_AMPDU_EOF_BIT_KNOWN;
1434                        if (phy_data->d0 & cpu_to_le32(IWL_RX_PHY_DATA0_HE_DELIM_EOF))
1435                                rx_status->flag |= RX_FLAG_AMPDU_EOF_BIT;
1436                }
1437        }
1438
1439        if (he_type == RATE_MCS_HE_TYPE_EXT_SU &&
1440            rate_n_flags & RATE_MCS_HE_106T_MSK) {
1441                rx_status->bw = RATE_INFO_BW_HE_RU;
1442                rx_status->he_ru = NL80211_RATE_INFO_HE_RU_ALLOC_106;
1443        }
1444
1445        /* actually data is filled in mac80211 */
1446        if (he_type == RATE_MCS_HE_TYPE_SU ||
1447            he_type == RATE_MCS_HE_TYPE_EXT_SU)
1448                he->data1 |=
1449                        cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA1_BW_RU_ALLOC_KNOWN);
1450
1451        stbc = (rate_n_flags & RATE_MCS_STBC_MSK) >> RATE_MCS_STBC_POS;
1452        rx_status->nss =
1453                ((rate_n_flags & RATE_VHT_MCS_NSS_MSK) >>
1454                                        RATE_VHT_MCS_NSS_POS) + 1;
1455        rx_status->rate_idx = rate_n_flags & RATE_VHT_MCS_RATE_CODE_MSK;
1456        rx_status->encoding = RX_ENC_HE;
1457        rx_status->enc_flags |= stbc << RX_ENC_FLAG_STBC_SHIFT;
1458        if (rate_n_flags & RATE_MCS_BF_MSK)
1459                rx_status->enc_flags |= RX_ENC_FLAG_BF;
1460
1461        rx_status->he_dcm =
1462                !!(rate_n_flags & RATE_HE_DUAL_CARRIER_MODE_MSK);
1463
1464#define CHECK_TYPE(F)                                                   \
1465        BUILD_BUG_ON(IEEE80211_RADIOTAP_HE_DATA1_FORMAT_ ## F !=        \
1466                     (RATE_MCS_HE_TYPE_ ## F >> RATE_MCS_HE_TYPE_POS))
1467
1468        CHECK_TYPE(SU);
1469        CHECK_TYPE(EXT_SU);
1470        CHECK_TYPE(MU);
1471        CHECK_TYPE(TRIG);
1472
1473        he->data1 |= cpu_to_le16(he_type >> RATE_MCS_HE_TYPE_POS);
1474
1475        if (rate_n_flags & RATE_MCS_BF_MSK)
1476                he->data5 |= cpu_to_le16(IEEE80211_RADIOTAP_HE_DATA5_TXBF);
1477
1478        switch ((rate_n_flags & RATE_MCS_HE_GI_LTF_MSK) >>
1479                RATE_MCS_HE_GI_LTF_POS) {
1480        case 0:
1481                if (he_type == RATE_MCS_HE_TYPE_TRIG)
1482                        rx_status->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
1483                else
1484                        rx_status->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
1485                if (he_type == RATE_MCS_HE_TYPE_MU)
1486                        ltf = IEEE80211_RADIOTAP_HE_DATA5_LTF_SIZE_4X;
1487                else
1488                        ltf = IEEE80211_RADIOTAP_HE_DATA5_LTF_SIZE_1X;
1489                break;
1490        case 1:
1491                if (he_type == RATE_MCS_HE_TYPE_TRIG)
1492                        rx_status->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
1493                else
1494                        rx_status->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
1495                ltf = IEEE80211_RADIOTAP_HE_DATA5_LTF_SIZE_2X;
1496                break;
1497        case 2:
1498                if (he_type == RATE_MCS_HE_TYPE_TRIG) {
1499                        rx_status->he_gi = NL80211_RATE_INFO_HE_GI_3_2;
1500                        ltf = IEEE80211_RADIOTAP_HE_DATA5_LTF_SIZE_4X;
1501                } else {
1502                        rx_status->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
1503                        ltf = IEEE80211_RADIOTAP_HE_DATA5_LTF_SIZE_2X;
1504                }
1505                break;
1506        case 3:
1507                if ((he_type == RATE_MCS_HE_TYPE_SU ||
1508                     he_type == RATE_MCS_HE_TYPE_EXT_SU) &&
1509                    rate_n_flags & RATE_MCS_SGI_MSK)
1510                        rx_status->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
1511                else
1512                        rx_status->he_gi = NL80211_RATE_INFO_HE_GI_3_2;
1513                ltf = IEEE80211_RADIOTAP_HE_DATA5_LTF_SIZE_4X;
1514                break;
1515        }
1516
1517        he->data5 |= le16_encode_bits(ltf,
1518                                      IEEE80211_RADIOTAP_HE_DATA5_LTF_SIZE);
1519}
1520
1521static void iwl_mvm_decode_lsig(struct sk_buff *skb,
1522                                struct iwl_mvm_rx_phy_data *phy_data)
1523{
1524        struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
1525        struct ieee80211_radiotap_lsig *lsig;
1526
1527        switch (phy_data->info_type) {
1528        case IWL_RX_PHY_INFO_TYPE_HT:
1529        case IWL_RX_PHY_INFO_TYPE_VHT_SU:
1530        case IWL_RX_PHY_INFO_TYPE_VHT_MU:
1531        case IWL_RX_PHY_INFO_TYPE_HE_TB_EXT:
1532        case IWL_RX_PHY_INFO_TYPE_HE_SU:
1533        case IWL_RX_PHY_INFO_TYPE_HE_MU:
1534        case IWL_RX_PHY_INFO_TYPE_HE_MU_EXT:
1535        case IWL_RX_PHY_INFO_TYPE_HE_TB:
1536                lsig = skb_put(skb, sizeof(*lsig));
1537                lsig->data1 = cpu_to_le16(IEEE80211_RADIOTAP_LSIG_DATA1_LENGTH_KNOWN);
1538                lsig->data2 = le16_encode_bits(le32_get_bits(phy_data->d1,
1539                                                             IWL_RX_PHY_DATA1_LSIG_LEN_MASK),
1540                                               IEEE80211_RADIOTAP_LSIG_DATA2_LENGTH);
1541                rx_status->flag |= RX_FLAG_RADIOTAP_LSIG;
1542                break;
1543        default:
1544                break;
1545        }
1546}
1547
1548static inline u8 iwl_mvm_nl80211_band_from_rx_msdu(u8 phy_band)
1549{
1550        switch (phy_band) {
1551        case PHY_BAND_24:
1552                return NL80211_BAND_2GHZ;
1553        case PHY_BAND_5:
1554                return NL80211_BAND_5GHZ;
1555        default:
1556                WARN_ONCE(1, "Unsupported phy band (%u)\n", phy_band);
1557                return NL80211_BAND_5GHZ;
1558        }
1559}
1560
1561void iwl_mvm_rx_mpdu_mq(struct iwl_mvm *mvm, struct napi_struct *napi,
1562                        struct iwl_rx_cmd_buffer *rxb, int queue)
1563{
1564        struct ieee80211_rx_status *rx_status;
1565        struct iwl_rx_packet *pkt = rxb_addr(rxb);
1566        struct iwl_rx_mpdu_desc *desc = (void *)pkt->data;
1567        struct ieee80211_hdr *hdr;
1568        u32 len = le16_to_cpu(desc->mpdu_len);
1569        u32 rate_n_flags, gp2_on_air_rise;
1570        u16 phy_info = le16_to_cpu(desc->phy_info);
1571        struct ieee80211_sta *sta = NULL;
1572        struct sk_buff *skb;
1573        u8 crypt_len = 0, channel, energy_a, energy_b;
1574        size_t desc_size;
1575        struct iwl_mvm_rx_phy_data phy_data = {
1576                .d4 = desc->phy_data4,
1577                .info_type = IWL_RX_PHY_INFO_TYPE_NONE,
1578        };
1579        bool csi = false;
1580
1581        if (unlikely(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)))
1582                return;
1583
1584        if (mvm->trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_AX210) {
1585                rate_n_flags = le32_to_cpu(desc->v3.rate_n_flags);
1586                channel = desc->v3.channel;
1587                gp2_on_air_rise = le32_to_cpu(desc->v3.gp2_on_air_rise);
1588                energy_a = desc->v3.energy_a;
1589                energy_b = desc->v3.energy_b;
1590                desc_size = sizeof(*desc);
1591
1592                phy_data.d0 = desc->v3.phy_data0;
1593                phy_data.d1 = desc->v3.phy_data1;
1594                phy_data.d2 = desc->v3.phy_data2;
1595                phy_data.d3 = desc->v3.phy_data3;
1596        } else {
1597                rate_n_flags = le32_to_cpu(desc->v1.rate_n_flags);
1598                channel = desc->v1.channel;
1599                gp2_on_air_rise = le32_to_cpu(desc->v1.gp2_on_air_rise);
1600                energy_a = desc->v1.energy_a;
1601                energy_b = desc->v1.energy_b;
1602                desc_size = IWL_RX_DESC_SIZE_V1;
1603
1604                phy_data.d0 = desc->v1.phy_data0;
1605                phy_data.d1 = desc->v1.phy_data1;
1606                phy_data.d2 = desc->v1.phy_data2;
1607                phy_data.d3 = desc->v1.phy_data3;
1608        }
1609
1610        if (phy_info & IWL_RX_MPDU_PHY_TSF_OVERLOAD)
1611                phy_data.info_type =
1612                        le32_get_bits(phy_data.d1,
1613                                      IWL_RX_PHY_DATA1_INFO_TYPE_MASK);
1614
1615        hdr = (void *)(pkt->data + desc_size);
1616        /* Dont use dev_alloc_skb(), we'll have enough headroom once
1617         * ieee80211_hdr pulled.
1618         */
1619        skb = alloc_skb(128, GFP_ATOMIC);
1620        if (!skb) {
1621                IWL_ERR(mvm, "alloc_skb failed\n");
1622                return;
1623        }
1624
1625        if (desc->mac_flags2 & IWL_RX_MPDU_MFLG2_PAD) {
1626                /*
1627                 * If the device inserted padding it means that (it thought)
1628                 * the 802.11 header wasn't a multiple of 4 bytes long. In
1629                 * this case, reserve two bytes at the start of the SKB to
1630                 * align the payload properly in case we end up copying it.
1631                 */
1632                skb_reserve(skb, 2);
1633        }
1634
1635        rx_status = IEEE80211_SKB_RXCB(skb);
1636
1637        /* This may be overridden by iwl_mvm_rx_he() to HE_RU */
1638        switch (rate_n_flags & RATE_MCS_CHAN_WIDTH_MSK) {
1639        case RATE_MCS_CHAN_WIDTH_20:
1640                break;
1641        case RATE_MCS_CHAN_WIDTH_40:
1642                rx_status->bw = RATE_INFO_BW_40;
1643                break;
1644        case RATE_MCS_CHAN_WIDTH_80:
1645                rx_status->bw = RATE_INFO_BW_80;
1646                break;
1647        case RATE_MCS_CHAN_WIDTH_160:
1648                rx_status->bw = RATE_INFO_BW_160;
1649                break;
1650        }
1651
1652        if (rate_n_flags & RATE_MCS_HE_MSK)
1653                iwl_mvm_rx_he(mvm, skb, &phy_data, rate_n_flags,
1654                              phy_info, queue);
1655
1656        iwl_mvm_decode_lsig(skb, &phy_data);
1657
1658        rx_status = IEEE80211_SKB_RXCB(skb);
1659
1660        if (iwl_mvm_rx_crypto(mvm, hdr, rx_status, phy_info, desc,
1661                              le32_to_cpu(pkt->len_n_flags), queue,
1662                              &crypt_len)) {
1663                kfree_skb(skb);
1664                return;
1665        }
1666
1667        /*
1668         * Keep packets with CRC errors (and with overrun) for monitor mode
1669         * (otherwise the firmware discards them) but mark them as bad.
1670         */
1671        if (!(desc->status & cpu_to_le16(IWL_RX_MPDU_STATUS_CRC_OK)) ||
1672            !(desc->status & cpu_to_le16(IWL_RX_MPDU_STATUS_OVERRUN_OK))) {
1673                IWL_DEBUG_RX(mvm, "Bad CRC or FIFO: 0x%08X.\n",
1674                             le16_to_cpu(desc->status));
1675                rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
1676        }
1677        /* set the preamble flag if appropriate */
1678        if (rate_n_flags & RATE_MCS_CCK_MSK &&
1679            phy_info & IWL_RX_MPDU_PHY_SHORT_PREAMBLE)
1680                rx_status->enc_flags |= RX_ENC_FLAG_SHORTPRE;
1681
1682        if (likely(!(phy_info & IWL_RX_MPDU_PHY_TSF_OVERLOAD))) {
1683                u64 tsf_on_air_rise;
1684
1685                if (mvm->trans->trans_cfg->device_family >=
1686                    IWL_DEVICE_FAMILY_AX210)
1687                        tsf_on_air_rise = le64_to_cpu(desc->v3.tsf_on_air_rise);
1688                else
1689                        tsf_on_air_rise = le64_to_cpu(desc->v1.tsf_on_air_rise);
1690
1691                rx_status->mactime = tsf_on_air_rise;
1692                /* TSF as indicated by the firmware is at INA time */
1693                rx_status->flag |= RX_FLAG_MACTIME_PLCP_START;
1694        }
1695
1696        rx_status->device_timestamp = gp2_on_air_rise;
1697        if (iwl_mvm_is_band_in_rx_supported(mvm)) {
1698                u8 band = BAND_IN_RX_STATUS(desc->mac_phy_idx);
1699
1700                rx_status->band = iwl_mvm_nl80211_band_from_rx_msdu(band);
1701        } else {
1702                rx_status->band = channel > 14 ? NL80211_BAND_5GHZ :
1703                        NL80211_BAND_2GHZ;
1704        }
1705        rx_status->freq = ieee80211_channel_to_frequency(channel,
1706                                                         rx_status->band);
1707        iwl_mvm_get_signal_strength(mvm, rx_status, rate_n_flags, energy_a,
1708                                    energy_b);
1709
1710        /* update aggregation data for monitor sake on default queue */
1711        if (!queue && (phy_info & IWL_RX_MPDU_PHY_AMPDU)) {
1712                bool toggle_bit = phy_info & IWL_RX_MPDU_PHY_AMPDU_TOGGLE;
1713
1714                rx_status->flag |= RX_FLAG_AMPDU_DETAILS;
1715                /*
1716                 * Toggle is switched whenever new aggregation starts. Make
1717                 * sure ampdu_reference is never 0 so we can later use it to
1718                 * see if the frame was really part of an A-MPDU or not.
1719                 */
1720                if (toggle_bit != mvm->ampdu_toggle) {
1721                        mvm->ampdu_ref++;
1722                        if (mvm->ampdu_ref == 0)
1723                                mvm->ampdu_ref++;
1724                        mvm->ampdu_toggle = toggle_bit;
1725                }
1726                rx_status->ampdu_reference = mvm->ampdu_ref;
1727        }
1728
1729        if (unlikely(mvm->monitor_on))
1730                iwl_mvm_add_rtap_sniffer_config(mvm, skb);
1731
1732        rcu_read_lock();
1733
1734        if (desc->status & cpu_to_le16(IWL_RX_MPDU_STATUS_SRC_STA_FOUND)) {
1735                u8 id = desc->sta_id_flags & IWL_RX_MPDU_SIF_STA_ID_MASK;
1736
1737                if (!WARN_ON_ONCE(id >= ARRAY_SIZE(mvm->fw_id_to_mac_id))) {
1738                        sta = rcu_dereference(mvm->fw_id_to_mac_id[id]);
1739                        if (IS_ERR(sta))
1740                                sta = NULL;
1741                }
1742        } else if (!is_multicast_ether_addr(hdr->addr2)) {
1743                /*
1744                 * This is fine since we prevent two stations with the same
1745                 * address from being added.
1746                 */
1747                sta = ieee80211_find_sta_by_ifaddr(mvm->hw, hdr->addr2, NULL);
1748        }
1749
1750        if (sta) {
1751                struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1752                struct ieee80211_vif *tx_blocked_vif =
1753                        rcu_dereference(mvm->csa_tx_blocked_vif);
1754                u8 baid = (u8)((le32_to_cpu(desc->reorder_data) &
1755                               IWL_RX_MPDU_REORDER_BAID_MASK) >>
1756                               IWL_RX_MPDU_REORDER_BAID_SHIFT);
1757                struct iwl_fw_dbg_trigger_tlv *trig;
1758                struct ieee80211_vif *vif = mvmsta->vif;
1759
1760                if (!mvm->tcm.paused && len >= sizeof(*hdr) &&
1761                    !is_multicast_ether_addr(hdr->addr1) &&
1762                    ieee80211_is_data(hdr->frame_control) &&
1763                    time_after(jiffies, mvm->tcm.ts + MVM_TCM_PERIOD))
1764                        schedule_delayed_work(&mvm->tcm.work, 0);
1765
1766                /*
1767                 * We have tx blocked stations (with CS bit). If we heard
1768                 * frames from a blocked station on a new channel we can
1769                 * TX to it again.
1770                 */
1771                if (unlikely(tx_blocked_vif) && tx_blocked_vif == vif) {
1772                        struct iwl_mvm_vif *mvmvif =
1773                                iwl_mvm_vif_from_mac80211(tx_blocked_vif);
1774
1775                        if (mvmvif->csa_target_freq == rx_status->freq)
1776                                iwl_mvm_sta_modify_disable_tx_ap(mvm, sta,
1777                                                                 false);
1778                }
1779
1780                rs_update_last_rssi(mvm, mvmsta, rx_status);
1781
1782                trig = iwl_fw_dbg_trigger_on(&mvm->fwrt,
1783                                             ieee80211_vif_to_wdev(vif),
1784                                             FW_DBG_TRIGGER_RSSI);
1785
1786                if (trig && ieee80211_is_beacon(hdr->frame_control)) {
1787                        struct iwl_fw_dbg_trigger_low_rssi *rssi_trig;
1788                        s32 rssi;
1789
1790                        rssi_trig = (void *)trig->data;
1791                        rssi = le32_to_cpu(rssi_trig->rssi);
1792
1793                        if (rx_status->signal < rssi)
1794                                iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
1795                                                        NULL);
1796                }
1797
1798                if (ieee80211_is_data(hdr->frame_control))
1799                        iwl_mvm_rx_csum(sta, skb, desc);
1800
1801                if (iwl_mvm_is_dup(sta, queue, rx_status, hdr, desc)) {
1802                        kfree_skb(skb);
1803                        goto out;
1804                }
1805
1806                /*
1807                 * Our hardware de-aggregates AMSDUs but copies the mac header
1808                 * as it to the de-aggregated MPDUs. We need to turn off the
1809                 * AMSDU bit in the QoS control ourselves.
1810                 * In addition, HW reverses addr3 and addr4 - reverse it back.
1811                 */
1812                if ((desc->mac_flags2 & IWL_RX_MPDU_MFLG2_AMSDU) &&
1813                    !WARN_ON(!ieee80211_is_data_qos(hdr->frame_control))) {
1814                        u8 *qc = ieee80211_get_qos_ctl(hdr);
1815
1816                        *qc &= ~IEEE80211_QOS_CTL_A_MSDU_PRESENT;
1817
1818                        if (mvm->trans->trans_cfg->device_family ==
1819                            IWL_DEVICE_FAMILY_9000) {
1820                                iwl_mvm_flip_address(hdr->addr3);
1821
1822                                if (ieee80211_has_a4(hdr->frame_control))
1823                                        iwl_mvm_flip_address(hdr->addr4);
1824                        }
1825                }
1826                if (baid != IWL_RX_REORDER_DATA_INVALID_BAID) {
1827                        u32 reorder_data = le32_to_cpu(desc->reorder_data);
1828
1829                        iwl_mvm_agg_rx_received(mvm, reorder_data, baid);
1830                }
1831        }
1832
1833        if (!(rate_n_flags & RATE_MCS_CCK_MSK) &&
1834            rate_n_flags & RATE_MCS_SGI_MSK)
1835                rx_status->enc_flags |= RX_ENC_FLAG_SHORT_GI;
1836        if (rate_n_flags & RATE_HT_MCS_GF_MSK)
1837                rx_status->enc_flags |= RX_ENC_FLAG_HT_GF;
1838        if (rate_n_flags & RATE_MCS_LDPC_MSK)
1839                rx_status->enc_flags |= RX_ENC_FLAG_LDPC;
1840        if (rate_n_flags & RATE_MCS_HT_MSK) {
1841                u8 stbc = (rate_n_flags & RATE_MCS_STBC_MSK) >>
1842                                RATE_MCS_STBC_POS;
1843                rx_status->encoding = RX_ENC_HT;
1844                rx_status->rate_idx = rate_n_flags & RATE_HT_MCS_INDEX_MSK;
1845                rx_status->enc_flags |= stbc << RX_ENC_FLAG_STBC_SHIFT;
1846        } else if (rate_n_flags & RATE_MCS_VHT_MSK) {
1847                u8 stbc = (rate_n_flags & RATE_MCS_STBC_MSK) >>
1848                                RATE_MCS_STBC_POS;
1849                rx_status->nss =
1850                        ((rate_n_flags & RATE_VHT_MCS_NSS_MSK) >>
1851                                                RATE_VHT_MCS_NSS_POS) + 1;
1852                rx_status->rate_idx = rate_n_flags & RATE_VHT_MCS_RATE_CODE_MSK;
1853                rx_status->encoding = RX_ENC_VHT;
1854                rx_status->enc_flags |= stbc << RX_ENC_FLAG_STBC_SHIFT;
1855                if (rate_n_flags & RATE_MCS_BF_MSK)
1856                        rx_status->enc_flags |= RX_ENC_FLAG_BF;
1857        } else if (!(rate_n_flags & RATE_MCS_HE_MSK)) {
1858                int rate = iwl_mvm_legacy_rate_to_mac80211_idx(rate_n_flags,
1859                                                               rx_status->band);
1860
1861                if (WARN(rate < 0 || rate > 0xFF,
1862                         "Invalid rate flags 0x%x, band %d,\n",
1863                         rate_n_flags, rx_status->band)) {
1864                        kfree_skb(skb);
1865                        goto out;
1866                }
1867                rx_status->rate_idx = rate;
1868        }
1869
1870        /* management stuff on default queue */
1871        if (!queue) {
1872                if (unlikely((ieee80211_is_beacon(hdr->frame_control) ||
1873                              ieee80211_is_probe_resp(hdr->frame_control)) &&
1874                             mvm->sched_scan_pass_all ==
1875                             SCHED_SCAN_PASS_ALL_ENABLED))
1876                        mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_FOUND;
1877
1878                if (unlikely(ieee80211_is_beacon(hdr->frame_control) ||
1879                             ieee80211_is_probe_resp(hdr->frame_control)))
1880                        rx_status->boottime_ns = ktime_get_boottime_ns();
1881        }
1882
1883        if (iwl_mvm_create_skb(mvm, skb, hdr, len, crypt_len, rxb)) {
1884                kfree_skb(skb);
1885                goto out;
1886        }
1887
1888        if (!iwl_mvm_reorder(mvm, napi, queue, sta, skb, desc))
1889                iwl_mvm_pass_packet_to_mac80211(mvm, napi, skb, queue,
1890                                                sta, csi);
1891out:
1892        rcu_read_unlock();
1893}
1894
1895void iwl_mvm_rx_monitor_no_data(struct iwl_mvm *mvm, struct napi_struct *napi,
1896                                struct iwl_rx_cmd_buffer *rxb, int queue)
1897{
1898        struct ieee80211_rx_status *rx_status;
1899        struct iwl_rx_packet *pkt = rxb_addr(rxb);
1900        struct iwl_rx_no_data *desc = (void *)pkt->data;
1901        u32 rate_n_flags = le32_to_cpu(desc->rate);
1902        u32 gp2_on_air_rise = le32_to_cpu(desc->on_air_rise_time);
1903        u32 rssi = le32_to_cpu(desc->rssi);
1904        u32 info_type = le32_to_cpu(desc->info) & RX_NO_DATA_INFO_TYPE_MSK;
1905        u16 phy_info = IWL_RX_MPDU_PHY_TSF_OVERLOAD;
1906        struct ieee80211_sta *sta = NULL;
1907        struct sk_buff *skb;
1908        u8 channel, energy_a, energy_b;
1909        struct iwl_mvm_rx_phy_data phy_data = {
1910                .d0 = desc->phy_info[0],
1911                .info_type = IWL_RX_PHY_INFO_TYPE_NONE,
1912        };
1913
1914        if (unlikely(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)))
1915                return;
1916
1917        energy_a = (rssi & RX_NO_DATA_CHAIN_A_MSK) >> RX_NO_DATA_CHAIN_A_POS;
1918        energy_b = (rssi & RX_NO_DATA_CHAIN_B_MSK) >> RX_NO_DATA_CHAIN_B_POS;
1919        channel = (rssi & RX_NO_DATA_CHANNEL_MSK) >> RX_NO_DATA_CHANNEL_POS;
1920
1921        phy_data.info_type =
1922                le32_get_bits(desc->phy_info[1],
1923                              IWL_RX_PHY_DATA1_INFO_TYPE_MASK);
1924
1925        /* Dont use dev_alloc_skb(), we'll have enough headroom once
1926         * ieee80211_hdr pulled.
1927         */
1928        skb = alloc_skb(128, GFP_ATOMIC);
1929        if (!skb) {
1930                IWL_ERR(mvm, "alloc_skb failed\n");
1931                return;
1932        }
1933
1934        rx_status = IEEE80211_SKB_RXCB(skb);
1935
1936        /* 0-length PSDU */
1937        rx_status->flag |= RX_FLAG_NO_PSDU;
1938
1939        switch (info_type) {
1940        case RX_NO_DATA_INFO_TYPE_NDP:
1941                rx_status->zero_length_psdu_type =
1942                        IEEE80211_RADIOTAP_ZERO_LEN_PSDU_SOUNDING;
1943                break;
1944        case RX_NO_DATA_INFO_TYPE_MU_UNMATCHED:
1945        case RX_NO_DATA_INFO_TYPE_HE_TB_UNMATCHED:
1946                rx_status->zero_length_psdu_type =
1947                        IEEE80211_RADIOTAP_ZERO_LEN_PSDU_NOT_CAPTURED;
1948                break;
1949        default:
1950                rx_status->zero_length_psdu_type =
1951                        IEEE80211_RADIOTAP_ZERO_LEN_PSDU_VENDOR;
1952                break;
1953        }
1954
1955        /* This may be overridden by iwl_mvm_rx_he() to HE_RU */
1956        switch (rate_n_flags & RATE_MCS_CHAN_WIDTH_MSK) {
1957        case RATE_MCS_CHAN_WIDTH_20:
1958                break;
1959        case RATE_MCS_CHAN_WIDTH_40:
1960                rx_status->bw = RATE_INFO_BW_40;
1961                break;
1962        case RATE_MCS_CHAN_WIDTH_80:
1963                rx_status->bw = RATE_INFO_BW_80;
1964                break;
1965        case RATE_MCS_CHAN_WIDTH_160:
1966                rx_status->bw = RATE_INFO_BW_160;
1967                break;
1968        }
1969
1970        if (rate_n_flags & RATE_MCS_HE_MSK)
1971                iwl_mvm_rx_he(mvm, skb, &phy_data, rate_n_flags,
1972                              phy_info, queue);
1973
1974        iwl_mvm_decode_lsig(skb, &phy_data);
1975
1976        rx_status->device_timestamp = gp2_on_air_rise;
1977        rx_status->band = channel > 14 ? NL80211_BAND_5GHZ :
1978                NL80211_BAND_2GHZ;
1979        rx_status->freq = ieee80211_channel_to_frequency(channel,
1980                                                         rx_status->band);
1981        iwl_mvm_get_signal_strength(mvm, rx_status, rate_n_flags, energy_a,
1982                                    energy_b);
1983
1984        rcu_read_lock();
1985
1986        if (!(rate_n_flags & RATE_MCS_CCK_MSK) &&
1987            rate_n_flags & RATE_MCS_SGI_MSK)
1988                rx_status->enc_flags |= RX_ENC_FLAG_SHORT_GI;
1989        if (rate_n_flags & RATE_HT_MCS_GF_MSK)
1990                rx_status->enc_flags |= RX_ENC_FLAG_HT_GF;
1991        if (rate_n_flags & RATE_MCS_LDPC_MSK)
1992                rx_status->enc_flags |= RX_ENC_FLAG_LDPC;
1993        if (rate_n_flags & RATE_MCS_HT_MSK) {
1994                u8 stbc = (rate_n_flags & RATE_MCS_STBC_MSK) >>
1995                                RATE_MCS_STBC_POS;
1996                rx_status->encoding = RX_ENC_HT;
1997                rx_status->rate_idx = rate_n_flags & RATE_HT_MCS_INDEX_MSK;
1998                rx_status->enc_flags |= stbc << RX_ENC_FLAG_STBC_SHIFT;
1999        } else if (rate_n_flags & RATE_MCS_VHT_MSK) {
2000                u8 stbc = (rate_n_flags & RATE_MCS_STBC_MSK) >>
2001                                RATE_MCS_STBC_POS;
2002                rx_status->rate_idx = rate_n_flags & RATE_VHT_MCS_RATE_CODE_MSK;
2003                rx_status->encoding = RX_ENC_VHT;
2004                rx_status->enc_flags |= stbc << RX_ENC_FLAG_STBC_SHIFT;
2005                if (rate_n_flags & RATE_MCS_BF_MSK)
2006                        rx_status->enc_flags |= RX_ENC_FLAG_BF;
2007                /*
2008                 * take the nss from the rx_vec since the rate_n_flags has
2009                 * only 2 bits for the nss which gives a max of 4 ss but
2010                 * there may be up to 8 spatial streams
2011                 */
2012                rx_status->nss =
2013                        le32_get_bits(desc->rx_vec[0],
2014                                      RX_NO_DATA_RX_VEC0_VHT_NSTS_MSK) + 1;
2015        } else if (rate_n_flags & RATE_MCS_HE_MSK) {
2016                rx_status->nss =
2017                        le32_get_bits(desc->rx_vec[0],
2018                                      RX_NO_DATA_RX_VEC0_HE_NSTS_MSK) + 1;
2019        } else {
2020                int rate = iwl_mvm_legacy_rate_to_mac80211_idx(rate_n_flags,
2021                                                               rx_status->band);
2022
2023                if (WARN(rate < 0 || rate > 0xFF,
2024                         "Invalid rate flags 0x%x, band %d,\n",
2025                         rate_n_flags, rx_status->band)) {
2026                        kfree_skb(skb);
2027                        goto out;
2028                }
2029                rx_status->rate_idx = rate;
2030        }
2031
2032        ieee80211_rx_napi(mvm->hw, sta, skb, napi);
2033out:
2034        rcu_read_unlock();
2035}
2036
2037void iwl_mvm_rx_frame_release(struct iwl_mvm *mvm, struct napi_struct *napi,
2038                              struct iwl_rx_cmd_buffer *rxb, int queue)
2039{
2040        struct iwl_rx_packet *pkt = rxb_addr(rxb);
2041        struct iwl_frame_release *release = (void *)pkt->data;
2042
2043        iwl_mvm_release_frames_from_notif(mvm, napi, release->baid,
2044                                          le16_to_cpu(release->nssn),
2045                                          queue, 0);
2046}
2047
2048void iwl_mvm_rx_bar_frame_release(struct iwl_mvm *mvm, struct napi_struct *napi,
2049                                  struct iwl_rx_cmd_buffer *rxb, int queue)
2050{
2051        struct iwl_rx_packet *pkt = rxb_addr(rxb);
2052        struct iwl_bar_frame_release *release = (void *)pkt->data;
2053        unsigned int baid = le32_get_bits(release->ba_info,
2054                                          IWL_BAR_FRAME_RELEASE_BAID_MASK);
2055        unsigned int nssn = le32_get_bits(release->ba_info,
2056                                          IWL_BAR_FRAME_RELEASE_NSSN_MASK);
2057        unsigned int sta_id = le32_get_bits(release->sta_tid,
2058                                            IWL_BAR_FRAME_RELEASE_STA_MASK);
2059        unsigned int tid = le32_get_bits(release->sta_tid,
2060                                         IWL_BAR_FRAME_RELEASE_TID_MASK);
2061        struct iwl_mvm_baid_data *baid_data;
2062
2063        if (WARN_ON_ONCE(baid == IWL_RX_REORDER_DATA_INVALID_BAID ||
2064                         baid >= ARRAY_SIZE(mvm->baid_map)))
2065                return;
2066
2067        rcu_read_lock();
2068        baid_data = rcu_dereference(mvm->baid_map[baid]);
2069        if (!baid_data) {
2070                IWL_DEBUG_RX(mvm,
2071                             "Got valid BAID %d but not allocated, invalid BAR release!\n",
2072                              baid);
2073                goto out;
2074        }
2075
2076        if (WARN(tid != baid_data->tid || sta_id != baid_data->sta_id,
2077                 "baid 0x%x is mapped to sta:%d tid:%d, but BAR release received for sta:%d tid:%d\n",
2078                 baid, baid_data->sta_id, baid_data->tid, sta_id,
2079                 tid))
2080                goto out;
2081
2082        iwl_mvm_release_frames_from_notif(mvm, napi, baid, nssn, queue, 0);
2083out:
2084        rcu_read_unlock();
2085}
2086