linux/drivers/net/wireless/ath/ath10k/mac.c
<<
>>
Prefs
   1// SPDX-License-Identifier: ISC
   2/*
   3 * Copyright (c) 2005-2011 Atheros Communications Inc.
   4 * Copyright (c) 2011-2017 Qualcomm Atheros, Inc.
   5 * Copyright (c) 2018-2019, The Linux Foundation. All rights reserved.
   6 */
   7
   8#include "mac.h"
   9
  10#include <net/cfg80211.h>
  11#include <net/mac80211.h>
  12#include <linux/etherdevice.h>
  13#include <linux/acpi.h>
  14#include <linux/of.h>
  15#include <linux/bitfield.h>
  16
  17#include "hif.h"
  18#include "core.h"
  19#include "debug.h"
  20#include "wmi.h"
  21#include "htt.h"
  22#include "txrx.h"
  23#include "testmode.h"
  24#include "wmi-tlv.h"
  25#include "wmi-ops.h"
  26#include "wow.h"
  27
  28/*********/
  29/* Rates */
  30/*********/
  31
  32static struct ieee80211_rate ath10k_rates[] = {
  33        { .bitrate = 10,
  34          .hw_value = ATH10K_HW_RATE_CCK_LP_1M },
  35        { .bitrate = 20,
  36          .hw_value = ATH10K_HW_RATE_CCK_LP_2M,
  37          .hw_value_short = ATH10K_HW_RATE_CCK_SP_2M,
  38          .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  39        { .bitrate = 55,
  40          .hw_value = ATH10K_HW_RATE_CCK_LP_5_5M,
  41          .hw_value_short = ATH10K_HW_RATE_CCK_SP_5_5M,
  42          .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  43        { .bitrate = 110,
  44          .hw_value = ATH10K_HW_RATE_CCK_LP_11M,
  45          .hw_value_short = ATH10K_HW_RATE_CCK_SP_11M,
  46          .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  47
  48        { .bitrate = 60, .hw_value = ATH10K_HW_RATE_OFDM_6M },
  49        { .bitrate = 90, .hw_value = ATH10K_HW_RATE_OFDM_9M },
  50        { .bitrate = 120, .hw_value = ATH10K_HW_RATE_OFDM_12M },
  51        { .bitrate = 180, .hw_value = ATH10K_HW_RATE_OFDM_18M },
  52        { .bitrate = 240, .hw_value = ATH10K_HW_RATE_OFDM_24M },
  53        { .bitrate = 360, .hw_value = ATH10K_HW_RATE_OFDM_36M },
  54        { .bitrate = 480, .hw_value = ATH10K_HW_RATE_OFDM_48M },
  55        { .bitrate = 540, .hw_value = ATH10K_HW_RATE_OFDM_54M },
  56};
  57
  58static struct ieee80211_rate ath10k_rates_rev2[] = {
  59        { .bitrate = 10,
  60          .hw_value = ATH10K_HW_RATE_REV2_CCK_LP_1M },
  61        { .bitrate = 20,
  62          .hw_value = ATH10K_HW_RATE_REV2_CCK_LP_2M,
  63          .hw_value_short = ATH10K_HW_RATE_REV2_CCK_SP_2M,
  64          .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  65        { .bitrate = 55,
  66          .hw_value = ATH10K_HW_RATE_REV2_CCK_LP_5_5M,
  67          .hw_value_short = ATH10K_HW_RATE_REV2_CCK_SP_5_5M,
  68          .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  69        { .bitrate = 110,
  70          .hw_value = ATH10K_HW_RATE_REV2_CCK_LP_11M,
  71          .hw_value_short = ATH10K_HW_RATE_REV2_CCK_SP_11M,
  72          .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  73
  74        { .bitrate = 60, .hw_value = ATH10K_HW_RATE_OFDM_6M },
  75        { .bitrate = 90, .hw_value = ATH10K_HW_RATE_OFDM_9M },
  76        { .bitrate = 120, .hw_value = ATH10K_HW_RATE_OFDM_12M },
  77        { .bitrate = 180, .hw_value = ATH10K_HW_RATE_OFDM_18M },
  78        { .bitrate = 240, .hw_value = ATH10K_HW_RATE_OFDM_24M },
  79        { .bitrate = 360, .hw_value = ATH10K_HW_RATE_OFDM_36M },
  80        { .bitrate = 480, .hw_value = ATH10K_HW_RATE_OFDM_48M },
  81        { .bitrate = 540, .hw_value = ATH10K_HW_RATE_OFDM_54M },
  82};
  83
  84static const struct cfg80211_sar_freq_ranges ath10k_sar_freq_ranges[] = {
  85        {.start_freq = 2402, .end_freq = 2494 },
  86        {.start_freq = 5170, .end_freq = 5875 },
  87};
  88
  89static const struct cfg80211_sar_capa ath10k_sar_capa = {
  90        .type = NL80211_SAR_TYPE_POWER,
  91        .num_freq_ranges = (ARRAY_SIZE(ath10k_sar_freq_ranges)),
  92        .freq_ranges = &ath10k_sar_freq_ranges[0],
  93};
  94
  95#define ATH10K_MAC_FIRST_OFDM_RATE_IDX 4
  96
  97#define ath10k_a_rates (ath10k_rates + ATH10K_MAC_FIRST_OFDM_RATE_IDX)
  98#define ath10k_a_rates_size (ARRAY_SIZE(ath10k_rates) - \
  99                             ATH10K_MAC_FIRST_OFDM_RATE_IDX)
 100#define ath10k_g_rates (ath10k_rates + 0)
 101#define ath10k_g_rates_size (ARRAY_SIZE(ath10k_rates))
 102
 103#define ath10k_g_rates_rev2 (ath10k_rates_rev2 + 0)
 104#define ath10k_g_rates_rev2_size (ARRAY_SIZE(ath10k_rates_rev2))
 105
 106#define ath10k_wmi_legacy_rates ath10k_rates
 107
 108static bool ath10k_mac_bitrate_is_cck(int bitrate)
 109{
 110        switch (bitrate) {
 111        case 10:
 112        case 20:
 113        case 55:
 114        case 110:
 115                return true;
 116        }
 117
 118        return false;
 119}
 120
 121static u8 ath10k_mac_bitrate_to_rate(int bitrate)
 122{
 123        return DIV_ROUND_UP(bitrate, 5) |
 124               (ath10k_mac_bitrate_is_cck(bitrate) ? BIT(7) : 0);
 125}
 126
 127u8 ath10k_mac_hw_rate_to_idx(const struct ieee80211_supported_band *sband,
 128                             u8 hw_rate, bool cck)
 129{
 130        const struct ieee80211_rate *rate;
 131        int i;
 132
 133        for (i = 0; i < sband->n_bitrates; i++) {
 134                rate = &sband->bitrates[i];
 135
 136                if (ath10k_mac_bitrate_is_cck(rate->bitrate) != cck)
 137                        continue;
 138
 139                if (rate->hw_value == hw_rate)
 140                        return i;
 141                else if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE &&
 142                         rate->hw_value_short == hw_rate)
 143                        return i;
 144        }
 145
 146        return 0;
 147}
 148
 149u8 ath10k_mac_bitrate_to_idx(const struct ieee80211_supported_band *sband,
 150                             u32 bitrate)
 151{
 152        int i;
 153
 154        for (i = 0; i < sband->n_bitrates; i++)
 155                if (sband->bitrates[i].bitrate == bitrate)
 156                        return i;
 157
 158        return 0;
 159}
 160
 161static int ath10k_mac_get_rate_hw_value(int bitrate)
 162{
 163        int i;
 164        u8 hw_value_prefix = 0;
 165
 166        if (ath10k_mac_bitrate_is_cck(bitrate))
 167                hw_value_prefix = WMI_RATE_PREAMBLE_CCK << 6;
 168
 169        for (i = 0; i < ARRAY_SIZE(ath10k_rates); i++) {
 170                if (ath10k_rates[i].bitrate == bitrate)
 171                        return hw_value_prefix | ath10k_rates[i].hw_value;
 172        }
 173
 174        return -EINVAL;
 175}
 176
 177static int ath10k_mac_get_max_vht_mcs_map(u16 mcs_map, int nss)
 178{
 179        switch ((mcs_map >> (2 * nss)) & 0x3) {
 180        case IEEE80211_VHT_MCS_SUPPORT_0_7: return BIT(8) - 1;
 181        case IEEE80211_VHT_MCS_SUPPORT_0_8: return BIT(9) - 1;
 182        case IEEE80211_VHT_MCS_SUPPORT_0_9: return BIT(10) - 1;
 183        }
 184        return 0;
 185}
 186
 187static u32
 188ath10k_mac_max_ht_nss(const u8 ht_mcs_mask[IEEE80211_HT_MCS_MASK_LEN])
 189{
 190        int nss;
 191
 192        for (nss = IEEE80211_HT_MCS_MASK_LEN - 1; nss >= 0; nss--)
 193                if (ht_mcs_mask[nss])
 194                        return nss + 1;
 195
 196        return 1;
 197}
 198
 199static u32
 200ath10k_mac_max_vht_nss(const u16 vht_mcs_mask[NL80211_VHT_NSS_MAX])
 201{
 202        int nss;
 203
 204        for (nss = NL80211_VHT_NSS_MAX - 1; nss >= 0; nss--)
 205                if (vht_mcs_mask[nss])
 206                        return nss + 1;
 207
 208        return 1;
 209}
 210
 211int ath10k_mac_ext_resource_config(struct ath10k *ar, u32 val)
 212{
 213        enum wmi_host_platform_type platform_type;
 214        int ret;
 215
 216        if (test_bit(WMI_SERVICE_TX_MODE_DYNAMIC, ar->wmi.svc_map))
 217                platform_type = WMI_HOST_PLATFORM_LOW_PERF;
 218        else
 219                platform_type = WMI_HOST_PLATFORM_HIGH_PERF;
 220
 221        ret = ath10k_wmi_ext_resource_config(ar, platform_type, val);
 222
 223        if (ret && ret != -EOPNOTSUPP) {
 224                ath10k_warn(ar, "failed to configure ext resource: %d\n", ret);
 225                return ret;
 226        }
 227
 228        return 0;
 229}
 230
 231/**********/
 232/* Crypto */
 233/**********/
 234
 235static int ath10k_send_key(struct ath10k_vif *arvif,
 236                           struct ieee80211_key_conf *key,
 237                           enum set_key_cmd cmd,
 238                           const u8 *macaddr, u32 flags)
 239{
 240        struct ath10k *ar = arvif->ar;
 241        struct wmi_vdev_install_key_arg arg = {
 242                .vdev_id = arvif->vdev_id,
 243                .key_idx = key->keyidx,
 244                .key_len = key->keylen,
 245                .key_data = key->key,
 246                .key_flags = flags,
 247                .macaddr = macaddr,
 248        };
 249
 250        lockdep_assert_held(&arvif->ar->conf_mutex);
 251
 252        switch (key->cipher) {
 253        case WLAN_CIPHER_SUITE_CCMP:
 254                arg.key_cipher = ar->wmi_key_cipher[WMI_CIPHER_AES_CCM];
 255                key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV_MGMT;
 256                break;
 257        case WLAN_CIPHER_SUITE_TKIP:
 258                arg.key_cipher = ar->wmi_key_cipher[WMI_CIPHER_TKIP];
 259                arg.key_txmic_len = 8;
 260                arg.key_rxmic_len = 8;
 261                break;
 262        case WLAN_CIPHER_SUITE_WEP40:
 263        case WLAN_CIPHER_SUITE_WEP104:
 264                arg.key_cipher = ar->wmi_key_cipher[WMI_CIPHER_WEP];
 265                break;
 266        case WLAN_CIPHER_SUITE_CCMP_256:
 267                arg.key_cipher = ar->wmi_key_cipher[WMI_CIPHER_AES_CCM];
 268                break;
 269        case WLAN_CIPHER_SUITE_GCMP:
 270        case WLAN_CIPHER_SUITE_GCMP_256:
 271                arg.key_cipher = ar->wmi_key_cipher[WMI_CIPHER_AES_GCM];
 272                key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV_MGMT;
 273                break;
 274        case WLAN_CIPHER_SUITE_BIP_GMAC_128:
 275        case WLAN_CIPHER_SUITE_BIP_GMAC_256:
 276        case WLAN_CIPHER_SUITE_BIP_CMAC_256:
 277        case WLAN_CIPHER_SUITE_AES_CMAC:
 278                WARN_ON(1);
 279                return -EINVAL;
 280        default:
 281                ath10k_warn(ar, "cipher %d is not supported\n", key->cipher);
 282                return -EOPNOTSUPP;
 283        }
 284
 285        if (test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags))
 286                key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
 287
 288        if (cmd == DISABLE_KEY) {
 289                arg.key_cipher = ar->wmi_key_cipher[WMI_CIPHER_NONE];
 290                arg.key_data = NULL;
 291        }
 292
 293        return ath10k_wmi_vdev_install_key(arvif->ar, &arg);
 294}
 295
 296static int ath10k_install_key(struct ath10k_vif *arvif,
 297                              struct ieee80211_key_conf *key,
 298                              enum set_key_cmd cmd,
 299                              const u8 *macaddr, u32 flags)
 300{
 301        struct ath10k *ar = arvif->ar;
 302        int ret;
 303        unsigned long time_left;
 304
 305        lockdep_assert_held(&ar->conf_mutex);
 306
 307        reinit_completion(&ar->install_key_done);
 308
 309        if (arvif->nohwcrypt)
 310                return 1;
 311
 312        ret = ath10k_send_key(arvif, key, cmd, macaddr, flags);
 313        if (ret)
 314                return ret;
 315
 316        time_left = wait_for_completion_timeout(&ar->install_key_done, 3 * HZ);
 317        if (time_left == 0)
 318                return -ETIMEDOUT;
 319
 320        return 0;
 321}
 322
 323static int ath10k_install_peer_wep_keys(struct ath10k_vif *arvif,
 324                                        const u8 *addr)
 325{
 326        struct ath10k *ar = arvif->ar;
 327        struct ath10k_peer *peer;
 328        int ret;
 329        int i;
 330        u32 flags;
 331
 332        lockdep_assert_held(&ar->conf_mutex);
 333
 334        if (WARN_ON(arvif->vif->type != NL80211_IFTYPE_AP &&
 335                    arvif->vif->type != NL80211_IFTYPE_ADHOC &&
 336                    arvif->vif->type != NL80211_IFTYPE_MESH_POINT))
 337                return -EINVAL;
 338
 339        spin_lock_bh(&ar->data_lock);
 340        peer = ath10k_peer_find(ar, arvif->vdev_id, addr);
 341        spin_unlock_bh(&ar->data_lock);
 342
 343        if (!peer)
 344                return -ENOENT;
 345
 346        for (i = 0; i < ARRAY_SIZE(arvif->wep_keys); i++) {
 347                if (arvif->wep_keys[i] == NULL)
 348                        continue;
 349
 350                switch (arvif->vif->type) {
 351                case NL80211_IFTYPE_AP:
 352                        flags = WMI_KEY_PAIRWISE;
 353
 354                        if (arvif->def_wep_key_idx == i)
 355                                flags |= WMI_KEY_TX_USAGE;
 356
 357                        ret = ath10k_install_key(arvif, arvif->wep_keys[i],
 358                                                 SET_KEY, addr, flags);
 359                        if (ret < 0)
 360                                return ret;
 361                        break;
 362                case NL80211_IFTYPE_ADHOC:
 363                        ret = ath10k_install_key(arvif, arvif->wep_keys[i],
 364                                                 SET_KEY, addr,
 365                                                 WMI_KEY_PAIRWISE);
 366                        if (ret < 0)
 367                                return ret;
 368
 369                        ret = ath10k_install_key(arvif, arvif->wep_keys[i],
 370                                                 SET_KEY, addr, WMI_KEY_GROUP);
 371                        if (ret < 0)
 372                                return ret;
 373                        break;
 374                default:
 375                        WARN_ON(1);
 376                        return -EINVAL;
 377                }
 378
 379                spin_lock_bh(&ar->data_lock);
 380                peer->keys[i] = arvif->wep_keys[i];
 381                spin_unlock_bh(&ar->data_lock);
 382        }
 383
 384        /* In some cases (notably with static WEP IBSS with multiple keys)
 385         * multicast Tx becomes broken. Both pairwise and groupwise keys are
 386         * installed already. Using WMI_KEY_TX_USAGE in different combinations
 387         * didn't seem help. Using def_keyid vdev parameter seems to be
 388         * effective so use that.
 389         *
 390         * FIXME: Revisit. Perhaps this can be done in a less hacky way.
 391         */
 392        if (arvif->vif->type != NL80211_IFTYPE_ADHOC)
 393                return 0;
 394
 395        if (arvif->def_wep_key_idx == -1)
 396                return 0;
 397
 398        ret = ath10k_wmi_vdev_set_param(arvif->ar,
 399                                        arvif->vdev_id,
 400                                        arvif->ar->wmi.vdev_param->def_keyid,
 401                                        arvif->def_wep_key_idx);
 402        if (ret) {
 403                ath10k_warn(ar, "failed to re-set def wpa key idxon vdev %i: %d\n",
 404                            arvif->vdev_id, ret);
 405                return ret;
 406        }
 407
 408        return 0;
 409}
 410
 411static int ath10k_clear_peer_keys(struct ath10k_vif *arvif,
 412                                  const u8 *addr)
 413{
 414        struct ath10k *ar = arvif->ar;
 415        struct ath10k_peer *peer;
 416        int first_errno = 0;
 417        int ret;
 418        int i;
 419        u32 flags = 0;
 420
 421        lockdep_assert_held(&ar->conf_mutex);
 422
 423        spin_lock_bh(&ar->data_lock);
 424        peer = ath10k_peer_find(ar, arvif->vdev_id, addr);
 425        spin_unlock_bh(&ar->data_lock);
 426
 427        if (!peer)
 428                return -ENOENT;
 429
 430        for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
 431                if (peer->keys[i] == NULL)
 432                        continue;
 433
 434                /* key flags are not required to delete the key */
 435                ret = ath10k_install_key(arvif, peer->keys[i],
 436                                         DISABLE_KEY, addr, flags);
 437                if (ret < 0 && first_errno == 0)
 438                        first_errno = ret;
 439
 440                if (ret < 0)
 441                        ath10k_warn(ar, "failed to remove peer wep key %d: %d\n",
 442                                    i, ret);
 443
 444                spin_lock_bh(&ar->data_lock);
 445                peer->keys[i] = NULL;
 446                spin_unlock_bh(&ar->data_lock);
 447        }
 448
 449        return first_errno;
 450}
 451
 452bool ath10k_mac_is_peer_wep_key_set(struct ath10k *ar, const u8 *addr,
 453                                    u8 keyidx)
 454{
 455        struct ath10k_peer *peer;
 456        int i;
 457
 458        lockdep_assert_held(&ar->data_lock);
 459
 460        /* We don't know which vdev this peer belongs to,
 461         * since WMI doesn't give us that information.
 462         *
 463         * FIXME: multi-bss needs to be handled.
 464         */
 465        peer = ath10k_peer_find(ar, 0, addr);
 466        if (!peer)
 467                return false;
 468
 469        for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
 470                if (peer->keys[i] && peer->keys[i]->keyidx == keyidx)
 471                        return true;
 472        }
 473
 474        return false;
 475}
 476
 477static int ath10k_clear_vdev_key(struct ath10k_vif *arvif,
 478                                 struct ieee80211_key_conf *key)
 479{
 480        struct ath10k *ar = arvif->ar;
 481        struct ath10k_peer *peer;
 482        u8 addr[ETH_ALEN];
 483        int first_errno = 0;
 484        int ret;
 485        int i;
 486        u32 flags = 0;
 487
 488        lockdep_assert_held(&ar->conf_mutex);
 489
 490        for (;;) {
 491                /* since ath10k_install_key we can't hold data_lock all the
 492                 * time, so we try to remove the keys incrementally
 493                 */
 494                spin_lock_bh(&ar->data_lock);
 495                i = 0;
 496                list_for_each_entry(peer, &ar->peers, list) {
 497                        for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
 498                                if (peer->keys[i] == key) {
 499                                        ether_addr_copy(addr, peer->addr);
 500                                        peer->keys[i] = NULL;
 501                                        break;
 502                                }
 503                        }
 504
 505                        if (i < ARRAY_SIZE(peer->keys))
 506                                break;
 507                }
 508                spin_unlock_bh(&ar->data_lock);
 509
 510                if (i == ARRAY_SIZE(peer->keys))
 511                        break;
 512                /* key flags are not required to delete the key */
 513                ret = ath10k_install_key(arvif, key, DISABLE_KEY, addr, flags);
 514                if (ret < 0 && first_errno == 0)
 515                        first_errno = ret;
 516
 517                if (ret)
 518                        ath10k_warn(ar, "failed to remove key for %pM: %d\n",
 519                                    addr, ret);
 520        }
 521
 522        return first_errno;
 523}
 524
 525static int ath10k_mac_vif_update_wep_key(struct ath10k_vif *arvif,
 526                                         struct ieee80211_key_conf *key)
 527{
 528        struct ath10k *ar = arvif->ar;
 529        struct ath10k_peer *peer;
 530        int ret;
 531
 532        lockdep_assert_held(&ar->conf_mutex);
 533
 534        list_for_each_entry(peer, &ar->peers, list) {
 535                if (ether_addr_equal(peer->addr, arvif->vif->addr))
 536                        continue;
 537
 538                if (ether_addr_equal(peer->addr, arvif->bssid))
 539                        continue;
 540
 541                if (peer->keys[key->keyidx] == key)
 542                        continue;
 543
 544                ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vif vdev %i update key %i needs update\n",
 545                           arvif->vdev_id, key->keyidx);
 546
 547                ret = ath10k_install_peer_wep_keys(arvif, peer->addr);
 548                if (ret) {
 549                        ath10k_warn(ar, "failed to update wep keys on vdev %i for peer %pM: %d\n",
 550                                    arvif->vdev_id, peer->addr, ret);
 551                        return ret;
 552                }
 553        }
 554
 555        return 0;
 556}
 557
 558/*********************/
 559/* General utilities */
 560/*********************/
 561
 562static inline enum wmi_phy_mode
 563chan_to_phymode(const struct cfg80211_chan_def *chandef)
 564{
 565        enum wmi_phy_mode phymode = MODE_UNKNOWN;
 566
 567        switch (chandef->chan->band) {
 568        case NL80211_BAND_2GHZ:
 569                switch (chandef->width) {
 570                case NL80211_CHAN_WIDTH_20_NOHT:
 571                        if (chandef->chan->flags & IEEE80211_CHAN_NO_OFDM)
 572                                phymode = MODE_11B;
 573                        else
 574                                phymode = MODE_11G;
 575                        break;
 576                case NL80211_CHAN_WIDTH_20:
 577                        phymode = MODE_11NG_HT20;
 578                        break;
 579                case NL80211_CHAN_WIDTH_40:
 580                        phymode = MODE_11NG_HT40;
 581                        break;
 582                default:
 583                        phymode = MODE_UNKNOWN;
 584                        break;
 585                }
 586                break;
 587        case NL80211_BAND_5GHZ:
 588                switch (chandef->width) {
 589                case NL80211_CHAN_WIDTH_20_NOHT:
 590                        phymode = MODE_11A;
 591                        break;
 592                case NL80211_CHAN_WIDTH_20:
 593                        phymode = MODE_11NA_HT20;
 594                        break;
 595                case NL80211_CHAN_WIDTH_40:
 596                        phymode = MODE_11NA_HT40;
 597                        break;
 598                case NL80211_CHAN_WIDTH_80:
 599                        phymode = MODE_11AC_VHT80;
 600                        break;
 601                case NL80211_CHAN_WIDTH_160:
 602                        phymode = MODE_11AC_VHT160;
 603                        break;
 604                case NL80211_CHAN_WIDTH_80P80:
 605                        phymode = MODE_11AC_VHT80_80;
 606                        break;
 607                default:
 608                        phymode = MODE_UNKNOWN;
 609                        break;
 610                }
 611                break;
 612        default:
 613                break;
 614        }
 615
 616        WARN_ON(phymode == MODE_UNKNOWN);
 617        return phymode;
 618}
 619
 620static u8 ath10k_parse_mpdudensity(u8 mpdudensity)
 621{
 622/*
 623 * 802.11n D2.0 defined values for "Minimum MPDU Start Spacing":
 624 *   0 for no restriction
 625 *   1 for 1/4 us
 626 *   2 for 1/2 us
 627 *   3 for 1 us
 628 *   4 for 2 us
 629 *   5 for 4 us
 630 *   6 for 8 us
 631 *   7 for 16 us
 632 */
 633        switch (mpdudensity) {
 634        case 0:
 635                return 0;
 636        case 1:
 637        case 2:
 638        case 3:
 639        /* Our lower layer calculations limit our precision to
 640         * 1 microsecond
 641         */
 642                return 1;
 643        case 4:
 644                return 2;
 645        case 5:
 646                return 4;
 647        case 6:
 648                return 8;
 649        case 7:
 650                return 16;
 651        default:
 652                return 0;
 653        }
 654}
 655
 656int ath10k_mac_vif_chan(struct ieee80211_vif *vif,
 657                        struct cfg80211_chan_def *def)
 658{
 659        struct ieee80211_chanctx_conf *conf;
 660
 661        rcu_read_lock();
 662        conf = rcu_dereference(vif->chanctx_conf);
 663        if (!conf) {
 664                rcu_read_unlock();
 665                return -ENOENT;
 666        }
 667
 668        *def = conf->def;
 669        rcu_read_unlock();
 670
 671        return 0;
 672}
 673
 674static void ath10k_mac_num_chanctxs_iter(struct ieee80211_hw *hw,
 675                                         struct ieee80211_chanctx_conf *conf,
 676                                         void *data)
 677{
 678        int *num = data;
 679
 680        (*num)++;
 681}
 682
 683static int ath10k_mac_num_chanctxs(struct ath10k *ar)
 684{
 685        int num = 0;
 686
 687        ieee80211_iter_chan_contexts_atomic(ar->hw,
 688                                            ath10k_mac_num_chanctxs_iter,
 689                                            &num);
 690
 691        return num;
 692}
 693
 694static void
 695ath10k_mac_get_any_chandef_iter(struct ieee80211_hw *hw,
 696                                struct ieee80211_chanctx_conf *conf,
 697                                void *data)
 698{
 699        struct cfg80211_chan_def **def = data;
 700
 701        *def = &conf->def;
 702}
 703
 704static void ath10k_wait_for_peer_delete_done(struct ath10k *ar, u32 vdev_id,
 705                                             const u8 *addr)
 706{
 707        unsigned long time_left;
 708        int ret;
 709
 710        if (test_bit(WMI_SERVICE_SYNC_DELETE_CMDS, ar->wmi.svc_map)) {
 711                ret = ath10k_wait_for_peer_deleted(ar, vdev_id, addr);
 712                if (ret) {
 713                        ath10k_warn(ar, "failed wait for peer deleted");
 714                        return;
 715                }
 716
 717                time_left = wait_for_completion_timeout(&ar->peer_delete_done,
 718                                                        5 * HZ);
 719                if (!time_left)
 720                        ath10k_warn(ar, "Timeout in receiving peer delete response\n");
 721        }
 722}
 723
 724static int ath10k_peer_create(struct ath10k *ar,
 725                              struct ieee80211_vif *vif,
 726                              struct ieee80211_sta *sta,
 727                              u32 vdev_id,
 728                              const u8 *addr,
 729                              enum wmi_peer_type peer_type)
 730{
 731        struct ath10k_vif *arvif;
 732        struct ath10k_peer *peer;
 733        int num_peers = 0;
 734        int ret;
 735
 736        lockdep_assert_held(&ar->conf_mutex);
 737
 738        num_peers = ar->num_peers;
 739
 740        /* Each vdev consumes a peer entry as well */
 741        list_for_each_entry(arvif, &ar->arvifs, list)
 742                num_peers++;
 743
 744        if (num_peers >= ar->max_num_peers)
 745                return -ENOBUFS;
 746
 747        ret = ath10k_wmi_peer_create(ar, vdev_id, addr, peer_type);
 748        if (ret) {
 749                ath10k_warn(ar, "failed to create wmi peer %pM on vdev %i: %i\n",
 750                            addr, vdev_id, ret);
 751                return ret;
 752        }
 753
 754        ret = ath10k_wait_for_peer_created(ar, vdev_id, addr);
 755        if (ret) {
 756                ath10k_warn(ar, "failed to wait for created wmi peer %pM on vdev %i: %i\n",
 757                            addr, vdev_id, ret);
 758                return ret;
 759        }
 760
 761        spin_lock_bh(&ar->data_lock);
 762
 763        peer = ath10k_peer_find(ar, vdev_id, addr);
 764        if (!peer) {
 765                spin_unlock_bh(&ar->data_lock);
 766                ath10k_warn(ar, "failed to find peer %pM on vdev %i after creation\n",
 767                            addr, vdev_id);
 768                ath10k_wait_for_peer_delete_done(ar, vdev_id, addr);
 769                return -ENOENT;
 770        }
 771
 772        peer->vif = vif;
 773        peer->sta = sta;
 774
 775        spin_unlock_bh(&ar->data_lock);
 776
 777        ar->num_peers++;
 778
 779        return 0;
 780}
 781
 782static int ath10k_mac_set_kickout(struct ath10k_vif *arvif)
 783{
 784        struct ath10k *ar = arvif->ar;
 785        u32 param;
 786        int ret;
 787
 788        param = ar->wmi.pdev_param->sta_kickout_th;
 789        ret = ath10k_wmi_pdev_set_param(ar, param,
 790                                        ATH10K_KICKOUT_THRESHOLD);
 791        if (ret) {
 792                ath10k_warn(ar, "failed to set kickout threshold on vdev %i: %d\n",
 793                            arvif->vdev_id, ret);
 794                return ret;
 795        }
 796
 797        param = ar->wmi.vdev_param->ap_keepalive_min_idle_inactive_time_secs;
 798        ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param,
 799                                        ATH10K_KEEPALIVE_MIN_IDLE);
 800        if (ret) {
 801                ath10k_warn(ar, "failed to set keepalive minimum idle time on vdev %i: %d\n",
 802                            arvif->vdev_id, ret);
 803                return ret;
 804        }
 805
 806        param = ar->wmi.vdev_param->ap_keepalive_max_idle_inactive_time_secs;
 807        ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param,
 808                                        ATH10K_KEEPALIVE_MAX_IDLE);
 809        if (ret) {
 810                ath10k_warn(ar, "failed to set keepalive maximum idle time on vdev %i: %d\n",
 811                            arvif->vdev_id, ret);
 812                return ret;
 813        }
 814
 815        param = ar->wmi.vdev_param->ap_keepalive_max_unresponsive_time_secs;
 816        ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param,
 817                                        ATH10K_KEEPALIVE_MAX_UNRESPONSIVE);
 818        if (ret) {
 819                ath10k_warn(ar, "failed to set keepalive maximum unresponsive time on vdev %i: %d\n",
 820                            arvif->vdev_id, ret);
 821                return ret;
 822        }
 823
 824        return 0;
 825}
 826
 827static int ath10k_mac_set_rts(struct ath10k_vif *arvif, u32 value)
 828{
 829        struct ath10k *ar = arvif->ar;
 830        u32 vdev_param;
 831
 832        vdev_param = ar->wmi.vdev_param->rts_threshold;
 833        return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, value);
 834}
 835
 836static int ath10k_peer_delete(struct ath10k *ar, u32 vdev_id, const u8 *addr)
 837{
 838        int ret;
 839
 840        lockdep_assert_held(&ar->conf_mutex);
 841
 842        ret = ath10k_wmi_peer_delete(ar, vdev_id, addr);
 843        if (ret)
 844                return ret;
 845
 846        ret = ath10k_wait_for_peer_deleted(ar, vdev_id, addr);
 847        if (ret)
 848                return ret;
 849
 850        if (test_bit(WMI_SERVICE_SYNC_DELETE_CMDS, ar->wmi.svc_map)) {
 851                unsigned long time_left;
 852
 853                time_left = wait_for_completion_timeout
 854                            (&ar->peer_delete_done, 5 * HZ);
 855
 856                if (!time_left) {
 857                        ath10k_warn(ar, "Timeout in receiving peer delete response\n");
 858                        return -ETIMEDOUT;
 859                }
 860        }
 861
 862        ar->num_peers--;
 863
 864        return 0;
 865}
 866
 867static void ath10k_peer_cleanup(struct ath10k *ar, u32 vdev_id)
 868{
 869        struct ath10k_peer *peer, *tmp;
 870        int peer_id;
 871        int i;
 872
 873        lockdep_assert_held(&ar->conf_mutex);
 874
 875        spin_lock_bh(&ar->data_lock);
 876        list_for_each_entry_safe(peer, tmp, &ar->peers, list) {
 877                if (peer->vdev_id != vdev_id)
 878                        continue;
 879
 880                ath10k_warn(ar, "removing stale peer %pM from vdev_id %d\n",
 881                            peer->addr, vdev_id);
 882
 883                for_each_set_bit(peer_id, peer->peer_ids,
 884                                 ATH10K_MAX_NUM_PEER_IDS) {
 885                        ar->peer_map[peer_id] = NULL;
 886                }
 887
 888                /* Double check that peer is properly un-referenced from
 889                 * the peer_map
 890                 */
 891                for (i = 0; i < ARRAY_SIZE(ar->peer_map); i++) {
 892                        if (ar->peer_map[i] == peer) {
 893                                ath10k_warn(ar, "removing stale peer_map entry for %pM (ptr %pK idx %d)\n",
 894                                            peer->addr, peer, i);
 895                                ar->peer_map[i] = NULL;
 896                        }
 897                }
 898
 899                list_del(&peer->list);
 900                kfree(peer);
 901                ar->num_peers--;
 902        }
 903        spin_unlock_bh(&ar->data_lock);
 904}
 905
 906static void ath10k_peer_cleanup_all(struct ath10k *ar)
 907{
 908        struct ath10k_peer *peer, *tmp;
 909        int i;
 910
 911        lockdep_assert_held(&ar->conf_mutex);
 912
 913        spin_lock_bh(&ar->data_lock);
 914        list_for_each_entry_safe(peer, tmp, &ar->peers, list) {
 915                list_del(&peer->list);
 916                kfree(peer);
 917        }
 918
 919        for (i = 0; i < ARRAY_SIZE(ar->peer_map); i++)
 920                ar->peer_map[i] = NULL;
 921
 922        spin_unlock_bh(&ar->data_lock);
 923
 924        ar->num_peers = 0;
 925        ar->num_stations = 0;
 926}
 927
 928static int ath10k_mac_tdls_peer_update(struct ath10k *ar, u32 vdev_id,
 929                                       struct ieee80211_sta *sta,
 930                                       enum wmi_tdls_peer_state state)
 931{
 932        int ret;
 933        struct wmi_tdls_peer_update_cmd_arg arg = {};
 934        struct wmi_tdls_peer_capab_arg cap = {};
 935        struct wmi_channel_arg chan_arg = {};
 936
 937        lockdep_assert_held(&ar->conf_mutex);
 938
 939        arg.vdev_id = vdev_id;
 940        arg.peer_state = state;
 941        ether_addr_copy(arg.addr, sta->addr);
 942
 943        cap.peer_max_sp = sta->max_sp;
 944        cap.peer_uapsd_queues = sta->uapsd_queues;
 945
 946        if (state == WMI_TDLS_PEER_STATE_CONNECTED &&
 947            !sta->tdls_initiator)
 948                cap.is_peer_responder = 1;
 949
 950        ret = ath10k_wmi_tdls_peer_update(ar, &arg, &cap, &chan_arg);
 951        if (ret) {
 952                ath10k_warn(ar, "failed to update tdls peer %pM on vdev %i: %i\n",
 953                            arg.addr, vdev_id, ret);
 954                return ret;
 955        }
 956
 957        return 0;
 958}
 959
 960/************************/
 961/* Interface management */
 962/************************/
 963
 964void ath10k_mac_vif_beacon_free(struct ath10k_vif *arvif)
 965{
 966        struct ath10k *ar = arvif->ar;
 967
 968        lockdep_assert_held(&ar->data_lock);
 969
 970        if (!arvif->beacon)
 971                return;
 972
 973        if (!arvif->beacon_buf)
 974                dma_unmap_single(ar->dev, ATH10K_SKB_CB(arvif->beacon)->paddr,
 975                                 arvif->beacon->len, DMA_TO_DEVICE);
 976
 977        if (WARN_ON(arvif->beacon_state != ATH10K_BEACON_SCHEDULED &&
 978                    arvif->beacon_state != ATH10K_BEACON_SENT))
 979                return;
 980
 981        dev_kfree_skb_any(arvif->beacon);
 982
 983        arvif->beacon = NULL;
 984        arvif->beacon_state = ATH10K_BEACON_SCHEDULED;
 985}
 986
 987static void ath10k_mac_vif_beacon_cleanup(struct ath10k_vif *arvif)
 988{
 989        struct ath10k *ar = arvif->ar;
 990
 991        lockdep_assert_held(&ar->data_lock);
 992
 993        ath10k_mac_vif_beacon_free(arvif);
 994
 995        if (arvif->beacon_buf) {
 996                dma_free_coherent(ar->dev, IEEE80211_MAX_FRAME_LEN,
 997                                  arvif->beacon_buf, arvif->beacon_paddr);
 998                arvif->beacon_buf = NULL;
 999        }
1000}
1001
1002static inline int ath10k_vdev_setup_sync(struct ath10k *ar)
1003{
1004        unsigned long time_left;
1005
1006        lockdep_assert_held(&ar->conf_mutex);
1007
1008        if (test_bit(ATH10K_FLAG_CRASH_FLUSH, &ar->dev_flags))
1009                return -ESHUTDOWN;
1010
1011        time_left = wait_for_completion_timeout(&ar->vdev_setup_done,
1012                                                ATH10K_VDEV_SETUP_TIMEOUT_HZ);
1013        if (time_left == 0)
1014                return -ETIMEDOUT;
1015
1016        return ar->last_wmi_vdev_start_status;
1017}
1018
1019static int ath10k_monitor_vdev_start(struct ath10k *ar, int vdev_id)
1020{
1021        struct cfg80211_chan_def *chandef = NULL;
1022        struct ieee80211_channel *channel = NULL;
1023        struct wmi_vdev_start_request_arg arg = {};
1024        int ret = 0;
1025
1026        lockdep_assert_held(&ar->conf_mutex);
1027
1028        ieee80211_iter_chan_contexts_atomic(ar->hw,
1029                                            ath10k_mac_get_any_chandef_iter,
1030                                            &chandef);
1031        if (WARN_ON_ONCE(!chandef))
1032                return -ENOENT;
1033
1034        channel = chandef->chan;
1035
1036        arg.vdev_id = vdev_id;
1037        arg.channel.freq = channel->center_freq;
1038        arg.channel.band_center_freq1 = chandef->center_freq1;
1039        arg.channel.band_center_freq2 = chandef->center_freq2;
1040
1041        /* TODO setup this dynamically, what in case we
1042         * don't have any vifs?
1043         */
1044        arg.channel.mode = chan_to_phymode(chandef);
1045        arg.channel.chan_radar =
1046                        !!(channel->flags & IEEE80211_CHAN_RADAR);
1047
1048        arg.channel.min_power = 0;
1049        arg.channel.max_power = channel->max_power * 2;
1050        arg.channel.max_reg_power = channel->max_reg_power * 2;
1051        arg.channel.max_antenna_gain = channel->max_antenna_gain * 2;
1052
1053        reinit_completion(&ar->vdev_setup_done);
1054        reinit_completion(&ar->vdev_delete_done);
1055
1056        ret = ath10k_wmi_vdev_start(ar, &arg);
1057        if (ret) {
1058                ath10k_warn(ar, "failed to request monitor vdev %i start: %d\n",
1059                            vdev_id, ret);
1060                return ret;
1061        }
1062
1063        ret = ath10k_vdev_setup_sync(ar);
1064        if (ret) {
1065                ath10k_warn(ar, "failed to synchronize setup for monitor vdev %i start: %d\n",
1066                            vdev_id, ret);
1067                return ret;
1068        }
1069
1070        ret = ath10k_wmi_vdev_up(ar, vdev_id, 0, ar->mac_addr);
1071        if (ret) {
1072                ath10k_warn(ar, "failed to put up monitor vdev %i: %d\n",
1073                            vdev_id, ret);
1074                goto vdev_stop;
1075        }
1076
1077        ar->monitor_vdev_id = vdev_id;
1078
1079        ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %i started\n",
1080                   ar->monitor_vdev_id);
1081        return 0;
1082
1083vdev_stop:
1084        ret = ath10k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
1085        if (ret)
1086                ath10k_warn(ar, "failed to stop monitor vdev %i after start failure: %d\n",
1087                            ar->monitor_vdev_id, ret);
1088
1089        return ret;
1090}
1091
1092static int ath10k_monitor_vdev_stop(struct ath10k *ar)
1093{
1094        int ret = 0;
1095
1096        lockdep_assert_held(&ar->conf_mutex);
1097
1098        ret = ath10k_wmi_vdev_down(ar, ar->monitor_vdev_id);
1099        if (ret)
1100                ath10k_warn(ar, "failed to put down monitor vdev %i: %d\n",
1101                            ar->monitor_vdev_id, ret);
1102
1103        reinit_completion(&ar->vdev_setup_done);
1104        reinit_completion(&ar->vdev_delete_done);
1105
1106        ret = ath10k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
1107        if (ret)
1108                ath10k_warn(ar, "failed to request monitor vdev %i stop: %d\n",
1109                            ar->monitor_vdev_id, ret);
1110
1111        ret = ath10k_vdev_setup_sync(ar);
1112        if (ret)
1113                ath10k_warn(ar, "failed to synchronize monitor vdev %i stop: %d\n",
1114                            ar->monitor_vdev_id, ret);
1115
1116        ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %i stopped\n",
1117                   ar->monitor_vdev_id);
1118        return ret;
1119}
1120
1121static int ath10k_monitor_vdev_create(struct ath10k *ar)
1122{
1123        int bit, ret = 0;
1124
1125        lockdep_assert_held(&ar->conf_mutex);
1126
1127        if (ar->free_vdev_map == 0) {
1128                ath10k_warn(ar, "failed to find free vdev id for monitor vdev\n");
1129                return -ENOMEM;
1130        }
1131
1132        bit = __ffs64(ar->free_vdev_map);
1133
1134        ar->monitor_vdev_id = bit;
1135
1136        ret = ath10k_wmi_vdev_create(ar, ar->monitor_vdev_id,
1137                                     WMI_VDEV_TYPE_MONITOR,
1138                                     0, ar->mac_addr);
1139        if (ret) {
1140                ath10k_warn(ar, "failed to request monitor vdev %i creation: %d\n",
1141                            ar->monitor_vdev_id, ret);
1142                return ret;
1143        }
1144
1145        ar->free_vdev_map &= ~(1LL << ar->monitor_vdev_id);
1146        ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %d created\n",
1147                   ar->monitor_vdev_id);
1148
1149        return 0;
1150}
1151
1152static int ath10k_monitor_vdev_delete(struct ath10k *ar)
1153{
1154        int ret = 0;
1155
1156        lockdep_assert_held(&ar->conf_mutex);
1157
1158        ret = ath10k_wmi_vdev_delete(ar, ar->monitor_vdev_id);
1159        if (ret) {
1160                ath10k_warn(ar, "failed to request wmi monitor vdev %i removal: %d\n",
1161                            ar->monitor_vdev_id, ret);
1162                return ret;
1163        }
1164
1165        ar->free_vdev_map |= 1LL << ar->monitor_vdev_id;
1166
1167        ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %d deleted\n",
1168                   ar->monitor_vdev_id);
1169        return ret;
1170}
1171
1172static int ath10k_monitor_start(struct ath10k *ar)
1173{
1174        int ret;
1175
1176        lockdep_assert_held(&ar->conf_mutex);
1177
1178        ret = ath10k_monitor_vdev_create(ar);
1179        if (ret) {
1180                ath10k_warn(ar, "failed to create monitor vdev: %d\n", ret);
1181                return ret;
1182        }
1183
1184        ret = ath10k_monitor_vdev_start(ar, ar->monitor_vdev_id);
1185        if (ret) {
1186                ath10k_warn(ar, "failed to start monitor vdev: %d\n", ret);
1187                ath10k_monitor_vdev_delete(ar);
1188                return ret;
1189        }
1190
1191        ar->monitor_started = true;
1192        ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor started\n");
1193
1194        return 0;
1195}
1196
1197static int ath10k_monitor_stop(struct ath10k *ar)
1198{
1199        int ret;
1200
1201        lockdep_assert_held(&ar->conf_mutex);
1202
1203        ret = ath10k_monitor_vdev_stop(ar);
1204        if (ret) {
1205                ath10k_warn(ar, "failed to stop monitor vdev: %d\n", ret);
1206                return ret;
1207        }
1208
1209        ret = ath10k_monitor_vdev_delete(ar);
1210        if (ret) {
1211                ath10k_warn(ar, "failed to delete monitor vdev: %d\n", ret);
1212                return ret;
1213        }
1214
1215        ar->monitor_started = false;
1216        ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor stopped\n");
1217
1218        return 0;
1219}
1220
1221static bool ath10k_mac_monitor_vdev_is_needed(struct ath10k *ar)
1222{
1223        int num_ctx;
1224
1225        /* At least one chanctx is required to derive a channel to start
1226         * monitor vdev on.
1227         */
1228        num_ctx = ath10k_mac_num_chanctxs(ar);
1229        if (num_ctx == 0)
1230                return false;
1231
1232        /* If there's already an existing special monitor interface then don't
1233         * bother creating another monitor vdev.
1234         */
1235        if (ar->monitor_arvif)
1236                return false;
1237
1238        return ar->monitor ||
1239               (!test_bit(ATH10K_FW_FEATURE_ALLOWS_MESH_BCAST,
1240                          ar->running_fw->fw_file.fw_features) &&
1241                (ar->filter_flags & FIF_OTHER_BSS)) ||
1242               test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
1243}
1244
1245static bool ath10k_mac_monitor_vdev_is_allowed(struct ath10k *ar)
1246{
1247        int num_ctx;
1248
1249        num_ctx = ath10k_mac_num_chanctxs(ar);
1250
1251        /* FIXME: Current interface combinations and cfg80211/mac80211 code
1252         * shouldn't allow this but make sure to prevent handling the following
1253         * case anyway since multi-channel DFS hasn't been tested at all.
1254         */
1255        if (test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags) && num_ctx > 1)
1256                return false;
1257
1258        return true;
1259}
1260
1261static int ath10k_monitor_recalc(struct ath10k *ar)
1262{
1263        bool needed;
1264        bool allowed;
1265        int ret;
1266
1267        lockdep_assert_held(&ar->conf_mutex);
1268
1269        needed = ath10k_mac_monitor_vdev_is_needed(ar);
1270        allowed = ath10k_mac_monitor_vdev_is_allowed(ar);
1271
1272        ath10k_dbg(ar, ATH10K_DBG_MAC,
1273                   "mac monitor recalc started? %d needed? %d allowed? %d\n",
1274                   ar->monitor_started, needed, allowed);
1275
1276        if (WARN_ON(needed && !allowed)) {
1277                if (ar->monitor_started) {
1278                        ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor stopping disallowed monitor\n");
1279
1280                        ret = ath10k_monitor_stop(ar);
1281                        if (ret)
1282                                ath10k_warn(ar, "failed to stop disallowed monitor: %d\n",
1283                                            ret);
1284                                /* not serious */
1285                }
1286
1287                return -EPERM;
1288        }
1289
1290        if (needed == ar->monitor_started)
1291                return 0;
1292
1293        if (needed)
1294                return ath10k_monitor_start(ar);
1295        else
1296                return ath10k_monitor_stop(ar);
1297}
1298
1299static bool ath10k_mac_can_set_cts_prot(struct ath10k_vif *arvif)
1300{
1301        struct ath10k *ar = arvif->ar;
1302
1303        lockdep_assert_held(&ar->conf_mutex);
1304
1305        if (!arvif->is_started) {
1306                ath10k_dbg(ar, ATH10K_DBG_MAC, "defer cts setup, vdev is not ready yet\n");
1307                return false;
1308        }
1309
1310        return true;
1311}
1312
1313static int ath10k_mac_set_cts_prot(struct ath10k_vif *arvif)
1314{
1315        struct ath10k *ar = arvif->ar;
1316        u32 vdev_param;
1317
1318        lockdep_assert_held(&ar->conf_mutex);
1319
1320        vdev_param = ar->wmi.vdev_param->protection_mode;
1321
1322        ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d cts_protection %d\n",
1323                   arvif->vdev_id, arvif->use_cts_prot);
1324
1325        return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
1326                                         arvif->use_cts_prot ? 1 : 0);
1327}
1328
1329static int ath10k_recalc_rtscts_prot(struct ath10k_vif *arvif)
1330{
1331        struct ath10k *ar = arvif->ar;
1332        u32 vdev_param, rts_cts = 0;
1333
1334        lockdep_assert_held(&ar->conf_mutex);
1335
1336        vdev_param = ar->wmi.vdev_param->enable_rtscts;
1337
1338        rts_cts |= SM(WMI_RTSCTS_ENABLED, WMI_RTSCTS_SET);
1339
1340        if (arvif->num_legacy_stations > 0)
1341                rts_cts |= SM(WMI_RTSCTS_ACROSS_SW_RETRIES,
1342                              WMI_RTSCTS_PROFILE);
1343        else
1344                rts_cts |= SM(WMI_RTSCTS_FOR_SECOND_RATESERIES,
1345                              WMI_RTSCTS_PROFILE);
1346
1347        ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d recalc rts/cts prot %d\n",
1348                   arvif->vdev_id, rts_cts);
1349
1350        return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
1351                                         rts_cts);
1352}
1353
1354static int ath10k_start_cac(struct ath10k *ar)
1355{
1356        int ret;
1357
1358        lockdep_assert_held(&ar->conf_mutex);
1359
1360        set_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
1361
1362        ret = ath10k_monitor_recalc(ar);
1363        if (ret) {
1364                ath10k_warn(ar, "failed to start monitor (cac): %d\n", ret);
1365                clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
1366                return ret;
1367        }
1368
1369        ath10k_dbg(ar, ATH10K_DBG_MAC, "mac cac start monitor vdev %d\n",
1370                   ar->monitor_vdev_id);
1371
1372        return 0;
1373}
1374
1375static int ath10k_stop_cac(struct ath10k *ar)
1376{
1377        lockdep_assert_held(&ar->conf_mutex);
1378
1379        /* CAC is not running - do nothing */
1380        if (!test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags))
1381                return 0;
1382
1383        clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
1384        ath10k_monitor_stop(ar);
1385
1386        ath10k_dbg(ar, ATH10K_DBG_MAC, "mac cac finished\n");
1387
1388        return 0;
1389}
1390
1391static void ath10k_mac_has_radar_iter(struct ieee80211_hw *hw,
1392                                      struct ieee80211_chanctx_conf *conf,
1393                                      void *data)
1394{
1395        bool *ret = data;
1396
1397        if (!*ret && conf->radar_enabled)
1398                *ret = true;
1399}
1400
1401static bool ath10k_mac_has_radar_enabled(struct ath10k *ar)
1402{
1403        bool has_radar = false;
1404
1405        ieee80211_iter_chan_contexts_atomic(ar->hw,
1406                                            ath10k_mac_has_radar_iter,
1407                                            &has_radar);
1408
1409        return has_radar;
1410}
1411
1412static void ath10k_recalc_radar_detection(struct ath10k *ar)
1413{
1414        int ret;
1415
1416        lockdep_assert_held(&ar->conf_mutex);
1417
1418        ath10k_stop_cac(ar);
1419
1420        if (!ath10k_mac_has_radar_enabled(ar))
1421                return;
1422
1423        if (ar->num_started_vdevs > 0)
1424                return;
1425
1426        ret = ath10k_start_cac(ar);
1427        if (ret) {
1428                /*
1429                 * Not possible to start CAC on current channel so starting
1430                 * radiation is not allowed, make this channel DFS_UNAVAILABLE
1431                 * by indicating that radar was detected.
1432                 */
1433                ath10k_warn(ar, "failed to start CAC: %d\n", ret);
1434                ieee80211_radar_detected(ar->hw);
1435        }
1436}
1437
1438static int ath10k_vdev_stop(struct ath10k_vif *arvif)
1439{
1440        struct ath10k *ar = arvif->ar;
1441        int ret;
1442
1443        lockdep_assert_held(&ar->conf_mutex);
1444
1445        reinit_completion(&ar->vdev_setup_done);
1446        reinit_completion(&ar->vdev_delete_done);
1447
1448        ret = ath10k_wmi_vdev_stop(ar, arvif->vdev_id);
1449        if (ret) {
1450                ath10k_warn(ar, "failed to stop WMI vdev %i: %d\n",
1451                            arvif->vdev_id, ret);
1452                return ret;
1453        }
1454
1455        ret = ath10k_vdev_setup_sync(ar);
1456        if (ret) {
1457                ath10k_warn(ar, "failed to synchronize setup for vdev %i: %d\n",
1458                            arvif->vdev_id, ret);
1459                return ret;
1460        }
1461
1462        WARN_ON(ar->num_started_vdevs == 0);
1463
1464        if (ar->num_started_vdevs != 0) {
1465                ar->num_started_vdevs--;
1466                ath10k_recalc_radar_detection(ar);
1467        }
1468
1469        return ret;
1470}
1471
1472static int ath10k_vdev_start_restart(struct ath10k_vif *arvif,
1473                                     const struct cfg80211_chan_def *chandef,
1474                                     bool restart)
1475{
1476        struct ath10k *ar = arvif->ar;
1477        struct wmi_vdev_start_request_arg arg = {};
1478        int ret = 0;
1479
1480        lockdep_assert_held(&ar->conf_mutex);
1481
1482        reinit_completion(&ar->vdev_setup_done);
1483        reinit_completion(&ar->vdev_delete_done);
1484
1485        arg.vdev_id = arvif->vdev_id;
1486        arg.dtim_period = arvif->dtim_period;
1487        arg.bcn_intval = arvif->beacon_interval;
1488
1489        arg.channel.freq = chandef->chan->center_freq;
1490        arg.channel.band_center_freq1 = chandef->center_freq1;
1491        arg.channel.band_center_freq2 = chandef->center_freq2;
1492        arg.channel.mode = chan_to_phymode(chandef);
1493
1494        arg.channel.min_power = 0;
1495        arg.channel.max_power = chandef->chan->max_power * 2;
1496        arg.channel.max_reg_power = chandef->chan->max_reg_power * 2;
1497        arg.channel.max_antenna_gain = chandef->chan->max_antenna_gain * 2;
1498
1499        if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
1500                arg.ssid = arvif->u.ap.ssid;
1501                arg.ssid_len = arvif->u.ap.ssid_len;
1502                arg.hidden_ssid = arvif->u.ap.hidden_ssid;
1503
1504                /* For now allow DFS for AP mode */
1505                arg.channel.chan_radar =
1506                        !!(chandef->chan->flags & IEEE80211_CHAN_RADAR);
1507        } else if (arvif->vdev_type == WMI_VDEV_TYPE_IBSS) {
1508                arg.ssid = arvif->vif->bss_conf.ssid;
1509                arg.ssid_len = arvif->vif->bss_conf.ssid_len;
1510        }
1511
1512        ath10k_dbg(ar, ATH10K_DBG_MAC,
1513                   "mac vdev %d start center_freq %d phymode %s\n",
1514                   arg.vdev_id, arg.channel.freq,
1515                   ath10k_wmi_phymode_str(arg.channel.mode));
1516
1517        if (restart)
1518                ret = ath10k_wmi_vdev_restart(ar, &arg);
1519        else
1520                ret = ath10k_wmi_vdev_start(ar, &arg);
1521
1522        if (ret) {
1523                ath10k_warn(ar, "failed to start WMI vdev %i: %d\n",
1524                            arg.vdev_id, ret);
1525                return ret;
1526        }
1527
1528        ret = ath10k_vdev_setup_sync(ar);
1529        if (ret) {
1530                ath10k_warn(ar,
1531                            "failed to synchronize setup for vdev %i restart %d: %d\n",
1532                            arg.vdev_id, restart, ret);
1533                return ret;
1534        }
1535
1536        ar->num_started_vdevs++;
1537        ath10k_recalc_radar_detection(ar);
1538
1539        return ret;
1540}
1541
1542static int ath10k_vdev_start(struct ath10k_vif *arvif,
1543                             const struct cfg80211_chan_def *def)
1544{
1545        return ath10k_vdev_start_restart(arvif, def, false);
1546}
1547
1548static int ath10k_vdev_restart(struct ath10k_vif *arvif,
1549                               const struct cfg80211_chan_def *def)
1550{
1551        return ath10k_vdev_start_restart(arvif, def, true);
1552}
1553
1554static int ath10k_mac_setup_bcn_p2p_ie(struct ath10k_vif *arvif,
1555                                       struct sk_buff *bcn)
1556{
1557        struct ath10k *ar = arvif->ar;
1558        struct ieee80211_mgmt *mgmt;
1559        const u8 *p2p_ie;
1560        int ret;
1561
1562        if (arvif->vif->type != NL80211_IFTYPE_AP || !arvif->vif->p2p)
1563                return 0;
1564
1565        mgmt = (void *)bcn->data;
1566        p2p_ie = cfg80211_find_vendor_ie(WLAN_OUI_WFA, WLAN_OUI_TYPE_WFA_P2P,
1567                                         mgmt->u.beacon.variable,
1568                                         bcn->len - (mgmt->u.beacon.variable -
1569                                                     bcn->data));
1570        if (!p2p_ie)
1571                return -ENOENT;
1572
1573        ret = ath10k_wmi_p2p_go_bcn_ie(ar, arvif->vdev_id, p2p_ie);
1574        if (ret) {
1575                ath10k_warn(ar, "failed to submit p2p go bcn ie for vdev %i: %d\n",
1576                            arvif->vdev_id, ret);
1577                return ret;
1578        }
1579
1580        return 0;
1581}
1582
1583static int ath10k_mac_remove_vendor_ie(struct sk_buff *skb, unsigned int oui,
1584                                       u8 oui_type, size_t ie_offset)
1585{
1586        size_t len;
1587        const u8 *next;
1588        const u8 *end;
1589        u8 *ie;
1590
1591        if (WARN_ON(skb->len < ie_offset))
1592                return -EINVAL;
1593
1594        ie = (u8 *)cfg80211_find_vendor_ie(oui, oui_type,
1595                                           skb->data + ie_offset,
1596                                           skb->len - ie_offset);
1597        if (!ie)
1598                return -ENOENT;
1599
1600        len = ie[1] + 2;
1601        end = skb->data + skb->len;
1602        next = ie + len;
1603
1604        if (WARN_ON(next > end))
1605                return -EINVAL;
1606
1607        memmove(ie, next, end - next);
1608        skb_trim(skb, skb->len - len);
1609
1610        return 0;
1611}
1612
1613static int ath10k_mac_setup_bcn_tmpl(struct ath10k_vif *arvif)
1614{
1615        struct ath10k *ar = arvif->ar;
1616        struct ieee80211_hw *hw = ar->hw;
1617        struct ieee80211_vif *vif = arvif->vif;
1618        struct ieee80211_mutable_offsets offs = {};
1619        struct sk_buff *bcn;
1620        int ret;
1621
1622        if (!test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map))
1623                return 0;
1624
1625        if (arvif->vdev_type != WMI_VDEV_TYPE_AP &&
1626            arvif->vdev_type != WMI_VDEV_TYPE_IBSS)
1627                return 0;
1628
1629        bcn = ieee80211_beacon_get_template(hw, vif, &offs);
1630        if (!bcn) {
1631                ath10k_warn(ar, "failed to get beacon template from mac80211\n");
1632                return -EPERM;
1633        }
1634
1635        ret = ath10k_mac_setup_bcn_p2p_ie(arvif, bcn);
1636        if (ret) {
1637                ath10k_warn(ar, "failed to setup p2p go bcn ie: %d\n", ret);
1638                kfree_skb(bcn);
1639                return ret;
1640        }
1641
1642        /* P2P IE is inserted by firmware automatically (as configured above)
1643         * so remove it from the base beacon template to avoid duplicate P2P
1644         * IEs in beacon frames.
1645         */
1646        ath10k_mac_remove_vendor_ie(bcn, WLAN_OUI_WFA, WLAN_OUI_TYPE_WFA_P2P,
1647                                    offsetof(struct ieee80211_mgmt,
1648                                             u.beacon.variable));
1649
1650        ret = ath10k_wmi_bcn_tmpl(ar, arvif->vdev_id, offs.tim_offset, bcn, 0,
1651                                  0, NULL, 0);
1652        kfree_skb(bcn);
1653
1654        if (ret) {
1655                ath10k_warn(ar, "failed to submit beacon template command: %d\n",
1656                            ret);
1657                return ret;
1658        }
1659
1660        return 0;
1661}
1662
1663static int ath10k_mac_setup_prb_tmpl(struct ath10k_vif *arvif)
1664{
1665        struct ath10k *ar = arvif->ar;
1666        struct ieee80211_hw *hw = ar->hw;
1667        struct ieee80211_vif *vif = arvif->vif;
1668        struct sk_buff *prb;
1669        int ret;
1670
1671        if (!test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map))
1672                return 0;
1673
1674        if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
1675                return 0;
1676
1677         /* For mesh, probe response and beacon share the same template */
1678        if (ieee80211_vif_is_mesh(vif))
1679                return 0;
1680
1681        prb = ieee80211_proberesp_get(hw, vif);
1682        if (!prb) {
1683                ath10k_warn(ar, "failed to get probe resp template from mac80211\n");
1684                return -EPERM;
1685        }
1686
1687        ret = ath10k_wmi_prb_tmpl(ar, arvif->vdev_id, prb);
1688        kfree_skb(prb);
1689
1690        if (ret) {
1691                ath10k_warn(ar, "failed to submit probe resp template command: %d\n",
1692                            ret);
1693                return ret;
1694        }
1695
1696        return 0;
1697}
1698
1699static int ath10k_mac_vif_fix_hidden_ssid(struct ath10k_vif *arvif)
1700{
1701        struct ath10k *ar = arvif->ar;
1702        struct cfg80211_chan_def def;
1703        int ret;
1704
1705        /* When originally vdev is started during assign_vif_chanctx() some
1706         * information is missing, notably SSID. Firmware revisions with beacon
1707         * offloading require the SSID to be provided during vdev (re)start to
1708         * handle hidden SSID properly.
1709         *
1710         * Vdev restart must be done after vdev has been both started and
1711         * upped. Otherwise some firmware revisions (at least 10.2) fail to
1712         * deliver vdev restart response event causing timeouts during vdev
1713         * syncing in ath10k.
1714         *
1715         * Note: The vdev down/up and template reinstallation could be skipped
1716         * since only wmi-tlv firmware are known to have beacon offload and
1717         * wmi-tlv doesn't seem to misbehave like 10.2 wrt vdev restart
1718         * response delivery. It's probably more robust to keep it as is.
1719         */
1720        if (!test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map))
1721                return 0;
1722
1723        if (WARN_ON(!arvif->is_started))
1724                return -EINVAL;
1725
1726        if (WARN_ON(!arvif->is_up))
1727                return -EINVAL;
1728
1729        if (WARN_ON(ath10k_mac_vif_chan(arvif->vif, &def)))
1730                return -EINVAL;
1731
1732        ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
1733        if (ret) {
1734                ath10k_warn(ar, "failed to bring down ap vdev %i: %d\n",
1735                            arvif->vdev_id, ret);
1736                return ret;
1737        }
1738
1739        /* Vdev down reset beacon & presp templates. Reinstall them. Otherwise
1740         * firmware will crash upon vdev up.
1741         */
1742
1743        ret = ath10k_mac_setup_bcn_tmpl(arvif);
1744        if (ret) {
1745                ath10k_warn(ar, "failed to update beacon template: %d\n", ret);
1746                return ret;
1747        }
1748
1749        ret = ath10k_mac_setup_prb_tmpl(arvif);
1750        if (ret) {
1751                ath10k_warn(ar, "failed to update presp template: %d\n", ret);
1752                return ret;
1753        }
1754
1755        ret = ath10k_vdev_restart(arvif, &def);
1756        if (ret) {
1757                ath10k_warn(ar, "failed to restart ap vdev %i: %d\n",
1758                            arvif->vdev_id, ret);
1759                return ret;
1760        }
1761
1762        ret = ath10k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
1763                                 arvif->bssid);
1764        if (ret) {
1765                ath10k_warn(ar, "failed to bring up ap vdev %i: %d\n",
1766                            arvif->vdev_id, ret);
1767                return ret;
1768        }
1769
1770        return 0;
1771}
1772
1773static void ath10k_control_beaconing(struct ath10k_vif *arvif,
1774                                     struct ieee80211_bss_conf *info)
1775{
1776        struct ath10k *ar = arvif->ar;
1777        int ret = 0;
1778
1779        lockdep_assert_held(&arvif->ar->conf_mutex);
1780
1781        if (!info->enable_beacon) {
1782                ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
1783                if (ret)
1784                        ath10k_warn(ar, "failed to down vdev_id %i: %d\n",
1785                                    arvif->vdev_id, ret);
1786
1787                arvif->is_up = false;
1788
1789                spin_lock_bh(&arvif->ar->data_lock);
1790                ath10k_mac_vif_beacon_free(arvif);
1791                spin_unlock_bh(&arvif->ar->data_lock);
1792
1793                return;
1794        }
1795
1796        arvif->tx_seq_no = 0x1000;
1797
1798        arvif->aid = 0;
1799        ether_addr_copy(arvif->bssid, info->bssid);
1800
1801        ret = ath10k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
1802                                 arvif->bssid);
1803        if (ret) {
1804                ath10k_warn(ar, "failed to bring up vdev %d: %i\n",
1805                            arvif->vdev_id, ret);
1806                return;
1807        }
1808
1809        arvif->is_up = true;
1810
1811        ret = ath10k_mac_vif_fix_hidden_ssid(arvif);
1812        if (ret) {
1813                ath10k_warn(ar, "failed to fix hidden ssid for vdev %i, expect trouble: %d\n",
1814                            arvif->vdev_id, ret);
1815                return;
1816        }
1817
1818        ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d up\n", arvif->vdev_id);
1819}
1820
1821static void ath10k_control_ibss(struct ath10k_vif *arvif,
1822                                struct ieee80211_bss_conf *info,
1823                                const u8 self_peer[ETH_ALEN])
1824{
1825        struct ath10k *ar = arvif->ar;
1826        u32 vdev_param;
1827        int ret = 0;
1828
1829        lockdep_assert_held(&arvif->ar->conf_mutex);
1830
1831        if (!info->ibss_joined) {
1832                if (is_zero_ether_addr(arvif->bssid))
1833                        return;
1834
1835                eth_zero_addr(arvif->bssid);
1836
1837                return;
1838        }
1839
1840        vdev_param = arvif->ar->wmi.vdev_param->atim_window;
1841        ret = ath10k_wmi_vdev_set_param(arvif->ar, arvif->vdev_id, vdev_param,
1842                                        ATH10K_DEFAULT_ATIM);
1843        if (ret)
1844                ath10k_warn(ar, "failed to set IBSS ATIM for vdev %d: %d\n",
1845                            arvif->vdev_id, ret);
1846}
1847
1848static int ath10k_mac_vif_recalc_ps_wake_threshold(struct ath10k_vif *arvif)
1849{
1850        struct ath10k *ar = arvif->ar;
1851        u32 param;
1852        u32 value;
1853        int ret;
1854
1855        lockdep_assert_held(&arvif->ar->conf_mutex);
1856
1857        if (arvif->u.sta.uapsd)
1858                value = WMI_STA_PS_TX_WAKE_THRESHOLD_NEVER;
1859        else
1860                value = WMI_STA_PS_TX_WAKE_THRESHOLD_ALWAYS;
1861
1862        param = WMI_STA_PS_PARAM_TX_WAKE_THRESHOLD;
1863        ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id, param, value);
1864        if (ret) {
1865                ath10k_warn(ar, "failed to submit ps wake threshold %u on vdev %i: %d\n",
1866                            value, arvif->vdev_id, ret);
1867                return ret;
1868        }
1869
1870        return 0;
1871}
1872
1873static int ath10k_mac_vif_recalc_ps_poll_count(struct ath10k_vif *arvif)
1874{
1875        struct ath10k *ar = arvif->ar;
1876        u32 param;
1877        u32 value;
1878        int ret;
1879
1880        lockdep_assert_held(&arvif->ar->conf_mutex);
1881
1882        if (arvif->u.sta.uapsd)
1883                value = WMI_STA_PS_PSPOLL_COUNT_UAPSD;
1884        else
1885                value = WMI_STA_PS_PSPOLL_COUNT_NO_MAX;
1886
1887        param = WMI_STA_PS_PARAM_PSPOLL_COUNT;
1888        ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
1889                                          param, value);
1890        if (ret) {
1891                ath10k_warn(ar, "failed to submit ps poll count %u on vdev %i: %d\n",
1892                            value, arvif->vdev_id, ret);
1893                return ret;
1894        }
1895
1896        return 0;
1897}
1898
1899static int ath10k_mac_num_vifs_started(struct ath10k *ar)
1900{
1901        struct ath10k_vif *arvif;
1902        int num = 0;
1903
1904        lockdep_assert_held(&ar->conf_mutex);
1905
1906        list_for_each_entry(arvif, &ar->arvifs, list)
1907                if (arvif->is_started)
1908                        num++;
1909
1910        return num;
1911}
1912
1913static int ath10k_mac_vif_setup_ps(struct ath10k_vif *arvif)
1914{
1915        struct ath10k *ar = arvif->ar;
1916        struct ieee80211_vif *vif = arvif->vif;
1917        struct ieee80211_conf *conf = &ar->hw->conf;
1918        enum wmi_sta_powersave_param param;
1919        enum wmi_sta_ps_mode psmode;
1920        int ret;
1921        int ps_timeout;
1922        bool enable_ps;
1923
1924        lockdep_assert_held(&arvif->ar->conf_mutex);
1925
1926        if (arvif->vif->type != NL80211_IFTYPE_STATION)
1927                return 0;
1928
1929        enable_ps = arvif->ps;
1930
1931        if (enable_ps && ath10k_mac_num_vifs_started(ar) > 1 &&
1932            !test_bit(ATH10K_FW_FEATURE_MULTI_VIF_PS_SUPPORT,
1933                      ar->running_fw->fw_file.fw_features)) {
1934                ath10k_warn(ar, "refusing to enable ps on vdev %i: not supported by fw\n",
1935                            arvif->vdev_id);
1936                enable_ps = false;
1937        }
1938
1939        if (!arvif->is_started) {
1940                /* mac80211 can update vif powersave state while disconnected.
1941                 * Firmware doesn't behave nicely and consumes more power than
1942                 * necessary if PS is disabled on a non-started vdev. Hence
1943                 * force-enable PS for non-running vdevs.
1944                 */
1945                psmode = WMI_STA_PS_MODE_ENABLED;
1946        } else if (enable_ps) {
1947                psmode = WMI_STA_PS_MODE_ENABLED;
1948                param = WMI_STA_PS_PARAM_INACTIVITY_TIME;
1949
1950                ps_timeout = conf->dynamic_ps_timeout;
1951                if (ps_timeout == 0) {
1952                        /* Firmware doesn't like 0 */
1953                        ps_timeout = ieee80211_tu_to_usec(
1954                                vif->bss_conf.beacon_int) / 1000;
1955                }
1956
1957                ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id, param,
1958                                                  ps_timeout);
1959                if (ret) {
1960                        ath10k_warn(ar, "failed to set inactivity time for vdev %d: %i\n",
1961                                    arvif->vdev_id, ret);
1962                        return ret;
1963                }
1964        } else {
1965                psmode = WMI_STA_PS_MODE_DISABLED;
1966        }
1967
1968        ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d psmode %s\n",
1969                   arvif->vdev_id, psmode ? "enable" : "disable");
1970
1971        ret = ath10k_wmi_set_psmode(ar, arvif->vdev_id, psmode);
1972        if (ret) {
1973                ath10k_warn(ar, "failed to set PS Mode %d for vdev %d: %d\n",
1974                            psmode, arvif->vdev_id, ret);
1975                return ret;
1976        }
1977
1978        return 0;
1979}
1980
1981static int ath10k_mac_vif_disable_keepalive(struct ath10k_vif *arvif)
1982{
1983        struct ath10k *ar = arvif->ar;
1984        struct wmi_sta_keepalive_arg arg = {};
1985        int ret;
1986
1987        lockdep_assert_held(&arvif->ar->conf_mutex);
1988
1989        if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
1990                return 0;
1991
1992        if (!test_bit(WMI_SERVICE_STA_KEEP_ALIVE, ar->wmi.svc_map))
1993                return 0;
1994
1995        /* Some firmware revisions have a bug and ignore the `enabled` field.
1996         * Instead use the interval to disable the keepalive.
1997         */
1998        arg.vdev_id = arvif->vdev_id;
1999        arg.enabled = 1;
2000        arg.method = WMI_STA_KEEPALIVE_METHOD_NULL_FRAME;
2001        arg.interval = WMI_STA_KEEPALIVE_INTERVAL_DISABLE;
2002
2003        ret = ath10k_wmi_sta_keepalive(ar, &arg);
2004        if (ret) {
2005                ath10k_warn(ar, "failed to submit keepalive on vdev %i: %d\n",
2006                            arvif->vdev_id, ret);
2007                return ret;
2008        }
2009
2010        return 0;
2011}
2012
2013static void ath10k_mac_vif_ap_csa_count_down(struct ath10k_vif *arvif)
2014{
2015        struct ath10k *ar = arvif->ar;
2016        struct ieee80211_vif *vif = arvif->vif;
2017        int ret;
2018
2019        lockdep_assert_held(&arvif->ar->conf_mutex);
2020
2021        if (WARN_ON(!test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map)))
2022                return;
2023
2024        if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
2025                return;
2026
2027        if (!vif->csa_active)
2028                return;
2029
2030        if (!arvif->is_up)
2031                return;
2032
2033        if (!ieee80211_beacon_cntdwn_is_complete(vif)) {
2034                ieee80211_beacon_update_cntdwn(vif);
2035
2036                ret = ath10k_mac_setup_bcn_tmpl(arvif);
2037                if (ret)
2038                        ath10k_warn(ar, "failed to update bcn tmpl during csa: %d\n",
2039                                    ret);
2040
2041                ret = ath10k_mac_setup_prb_tmpl(arvif);
2042                if (ret)
2043                        ath10k_warn(ar, "failed to update prb tmpl during csa: %d\n",
2044                                    ret);
2045        } else {
2046                ieee80211_csa_finish(vif);
2047        }
2048}
2049
2050static void ath10k_mac_vif_ap_csa_work(struct work_struct *work)
2051{
2052        struct ath10k_vif *arvif = container_of(work, struct ath10k_vif,
2053                                                ap_csa_work);
2054        struct ath10k *ar = arvif->ar;
2055
2056        mutex_lock(&ar->conf_mutex);
2057        ath10k_mac_vif_ap_csa_count_down(arvif);
2058        mutex_unlock(&ar->conf_mutex);
2059}
2060
2061static void ath10k_mac_handle_beacon_iter(void *data, u8 *mac,
2062                                          struct ieee80211_vif *vif)
2063{
2064        struct sk_buff *skb = data;
2065        struct ieee80211_mgmt *mgmt = (void *)skb->data;
2066        struct ath10k_vif *arvif = (void *)vif->drv_priv;
2067
2068        if (vif->type != NL80211_IFTYPE_STATION)
2069                return;
2070
2071        if (!ether_addr_equal(mgmt->bssid, vif->bss_conf.bssid))
2072                return;
2073
2074        cancel_delayed_work(&arvif->connection_loss_work);
2075}
2076
2077void ath10k_mac_handle_beacon(struct ath10k *ar, struct sk_buff *skb)
2078{
2079        ieee80211_iterate_active_interfaces_atomic(ar->hw,
2080                                                   ATH10K_ITER_NORMAL_FLAGS,
2081                                                   ath10k_mac_handle_beacon_iter,
2082                                                   skb);
2083}
2084
2085static void ath10k_mac_handle_beacon_miss_iter(void *data, u8 *mac,
2086                                               struct ieee80211_vif *vif)
2087{
2088        u32 *vdev_id = data;
2089        struct ath10k_vif *arvif = (void *)vif->drv_priv;
2090        struct ath10k *ar = arvif->ar;
2091        struct ieee80211_hw *hw = ar->hw;
2092
2093        if (arvif->vdev_id != *vdev_id)
2094                return;
2095
2096        if (!arvif->is_up)
2097                return;
2098
2099        ieee80211_beacon_loss(vif);
2100
2101        /* Firmware doesn't report beacon loss events repeatedly. If AP probe
2102         * (done by mac80211) succeeds but beacons do not resume then it
2103         * doesn't make sense to continue operation. Queue connection loss work
2104         * which can be cancelled when beacon is received.
2105         */
2106        ieee80211_queue_delayed_work(hw, &arvif->connection_loss_work,
2107                                     ATH10K_CONNECTION_LOSS_HZ);
2108}
2109
2110void ath10k_mac_handle_beacon_miss(struct ath10k *ar, u32 vdev_id)
2111{
2112        ieee80211_iterate_active_interfaces_atomic(ar->hw,
2113                                                   ATH10K_ITER_NORMAL_FLAGS,
2114                                                   ath10k_mac_handle_beacon_miss_iter,
2115                                                   &vdev_id);
2116}
2117
2118static void ath10k_mac_vif_sta_connection_loss_work(struct work_struct *work)
2119{
2120        struct ath10k_vif *arvif = container_of(work, struct ath10k_vif,
2121                                                connection_loss_work.work);
2122        struct ieee80211_vif *vif = arvif->vif;
2123
2124        if (!arvif->is_up)
2125                return;
2126
2127        ieee80211_connection_loss(vif);
2128}
2129
2130/**********************/
2131/* Station management */
2132/**********************/
2133
2134static u32 ath10k_peer_assoc_h_listen_intval(struct ath10k *ar,
2135                                             struct ieee80211_vif *vif)
2136{
2137        /* Some firmware revisions have unstable STA powersave when listen
2138         * interval is set too high (e.g. 5). The symptoms are firmware doesn't
2139         * generate NullFunc frames properly even if buffered frames have been
2140         * indicated in Beacon TIM. Firmware would seldom wake up to pull
2141         * buffered frames. Often pinging the device from AP would simply fail.
2142         *
2143         * As a workaround set it to 1.
2144         */
2145        if (vif->type == NL80211_IFTYPE_STATION)
2146                return 1;
2147
2148        return ar->hw->conf.listen_interval;
2149}
2150
2151static void ath10k_peer_assoc_h_basic(struct ath10k *ar,
2152                                      struct ieee80211_vif *vif,
2153                                      struct ieee80211_sta *sta,
2154                                      struct wmi_peer_assoc_complete_arg *arg)
2155{
2156        struct ath10k_vif *arvif = (void *)vif->drv_priv;
2157        u32 aid;
2158
2159        lockdep_assert_held(&ar->conf_mutex);
2160
2161        if (vif->type == NL80211_IFTYPE_STATION)
2162                aid = vif->bss_conf.aid;
2163        else
2164                aid = sta->aid;
2165
2166        ether_addr_copy(arg->addr, sta->addr);
2167        arg->vdev_id = arvif->vdev_id;
2168        arg->peer_aid = aid;
2169        arg->peer_flags |= arvif->ar->wmi.peer_flags->auth;
2170        arg->peer_listen_intval = ath10k_peer_assoc_h_listen_intval(ar, vif);
2171        arg->peer_num_spatial_streams = 1;
2172        arg->peer_caps = vif->bss_conf.assoc_capability;
2173}
2174
2175static void ath10k_peer_assoc_h_crypto(struct ath10k *ar,
2176                                       struct ieee80211_vif *vif,
2177                                       struct ieee80211_sta *sta,
2178                                       struct wmi_peer_assoc_complete_arg *arg)
2179{
2180        struct ieee80211_bss_conf *info = &vif->bss_conf;
2181        struct cfg80211_chan_def def;
2182        struct cfg80211_bss *bss;
2183        const u8 *rsnie = NULL;
2184        const u8 *wpaie = NULL;
2185
2186        lockdep_assert_held(&ar->conf_mutex);
2187
2188        if (WARN_ON(ath10k_mac_vif_chan(vif, &def)))
2189                return;
2190
2191        bss = cfg80211_get_bss(ar->hw->wiphy, def.chan, info->bssid,
2192                               info->ssid_len ? info->ssid : NULL, info->ssid_len,
2193                               IEEE80211_BSS_TYPE_ANY, IEEE80211_PRIVACY_ANY);
2194        if (bss) {
2195                const struct cfg80211_bss_ies *ies;
2196
2197                rcu_read_lock();
2198                rsnie = ieee80211_bss_get_ie(bss, WLAN_EID_RSN);
2199
2200                ies = rcu_dereference(bss->ies);
2201
2202                wpaie = cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
2203                                                WLAN_OUI_TYPE_MICROSOFT_WPA,
2204                                                ies->data,
2205                                                ies->len);
2206                rcu_read_unlock();
2207                cfg80211_put_bss(ar->hw->wiphy, bss);
2208        }
2209
2210        /* FIXME: base on RSN IE/WPA IE is a correct idea? */
2211        if (rsnie || wpaie) {
2212                ath10k_dbg(ar, ATH10K_DBG_WMI, "%s: rsn ie found\n", __func__);
2213                arg->peer_flags |= ar->wmi.peer_flags->need_ptk_4_way;
2214        }
2215
2216        if (wpaie) {
2217                ath10k_dbg(ar, ATH10K_DBG_WMI, "%s: wpa ie found\n", __func__);
2218                arg->peer_flags |= ar->wmi.peer_flags->need_gtk_2_way;
2219        }
2220
2221        if (sta->mfp &&
2222            test_bit(ATH10K_FW_FEATURE_MFP_SUPPORT,
2223                     ar->running_fw->fw_file.fw_features)) {
2224                arg->peer_flags |= ar->wmi.peer_flags->pmf;
2225        }
2226}
2227
2228static void ath10k_peer_assoc_h_rates(struct ath10k *ar,
2229                                      struct ieee80211_vif *vif,
2230                                      struct ieee80211_sta *sta,
2231                                      struct wmi_peer_assoc_complete_arg *arg)
2232{
2233        struct ath10k_vif *arvif = (void *)vif->drv_priv;
2234        struct wmi_rate_set_arg *rateset = &arg->peer_legacy_rates;
2235        struct cfg80211_chan_def def;
2236        const struct ieee80211_supported_band *sband;
2237        const struct ieee80211_rate *rates;
2238        enum nl80211_band band;
2239        u32 ratemask;
2240        u8 rate;
2241        int i;
2242
2243        lockdep_assert_held(&ar->conf_mutex);
2244
2245        if (WARN_ON(ath10k_mac_vif_chan(vif, &def)))
2246                return;
2247
2248        band = def.chan->band;
2249        sband = ar->hw->wiphy->bands[band];
2250        ratemask = sta->supp_rates[band];
2251        ratemask &= arvif->bitrate_mask.control[band].legacy;
2252        rates = sband->bitrates;
2253
2254        rateset->num_rates = 0;
2255
2256        for (i = 0; i < 32; i++, ratemask >>= 1, rates++) {
2257                if (!(ratemask & 1))
2258                        continue;
2259
2260                rate = ath10k_mac_bitrate_to_rate(rates->bitrate);
2261                rateset->rates[rateset->num_rates] = rate;
2262                rateset->num_rates++;
2263        }
2264}
2265
2266static bool
2267ath10k_peer_assoc_h_ht_masked(const u8 ht_mcs_mask[IEEE80211_HT_MCS_MASK_LEN])
2268{
2269        int nss;
2270
2271        for (nss = 0; nss < IEEE80211_HT_MCS_MASK_LEN; nss++)
2272                if (ht_mcs_mask[nss])
2273                        return false;
2274
2275        return true;
2276}
2277
2278static bool
2279ath10k_peer_assoc_h_vht_masked(const u16 vht_mcs_mask[NL80211_VHT_NSS_MAX])
2280{
2281        int nss;
2282
2283        for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++)
2284                if (vht_mcs_mask[nss])
2285                        return false;
2286
2287        return true;
2288}
2289
2290static void ath10k_peer_assoc_h_ht(struct ath10k *ar,
2291                                   struct ieee80211_vif *vif,
2292                                   struct ieee80211_sta *sta,
2293                                   struct wmi_peer_assoc_complete_arg *arg)
2294{
2295        const struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
2296        struct ath10k_vif *arvif = (void *)vif->drv_priv;
2297        struct cfg80211_chan_def def;
2298        enum nl80211_band band;
2299        const u8 *ht_mcs_mask;
2300        const u16 *vht_mcs_mask;
2301        int i, n;
2302        u8 max_nss;
2303        u32 stbc;
2304
2305        lockdep_assert_held(&ar->conf_mutex);
2306
2307        if (WARN_ON(ath10k_mac_vif_chan(vif, &def)))
2308                return;
2309
2310        if (!ht_cap->ht_supported)
2311                return;
2312
2313        band = def.chan->band;
2314        ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
2315        vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
2316
2317        if (ath10k_peer_assoc_h_ht_masked(ht_mcs_mask) &&
2318            ath10k_peer_assoc_h_vht_masked(vht_mcs_mask))
2319                return;
2320
2321        arg->peer_flags |= ar->wmi.peer_flags->ht;
2322        arg->peer_max_mpdu = (1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
2323                                    ht_cap->ampdu_factor)) - 1;
2324
2325        arg->peer_mpdu_density =
2326                ath10k_parse_mpdudensity(ht_cap->ampdu_density);
2327
2328        arg->peer_ht_caps = ht_cap->cap;
2329        arg->peer_rate_caps |= WMI_RC_HT_FLAG;
2330
2331        if (ht_cap->cap & IEEE80211_HT_CAP_LDPC_CODING)
2332                arg->peer_flags |= ar->wmi.peer_flags->ldbc;
2333
2334        if (sta->bandwidth >= IEEE80211_STA_RX_BW_40) {
2335                arg->peer_flags |= ar->wmi.peer_flags->bw40;
2336                arg->peer_rate_caps |= WMI_RC_CW40_FLAG;
2337        }
2338
2339        if (arvif->bitrate_mask.control[band].gi != NL80211_TXRATE_FORCE_LGI) {
2340                if (ht_cap->cap & IEEE80211_HT_CAP_SGI_20)
2341                        arg->peer_rate_caps |= WMI_RC_SGI_FLAG;
2342
2343                if (ht_cap->cap & IEEE80211_HT_CAP_SGI_40)
2344                        arg->peer_rate_caps |= WMI_RC_SGI_FLAG;
2345        }
2346
2347        if (ht_cap->cap & IEEE80211_HT_CAP_TX_STBC) {
2348                arg->peer_rate_caps |= WMI_RC_TX_STBC_FLAG;
2349                arg->peer_flags |= ar->wmi.peer_flags->stbc;
2350        }
2351
2352        if (ht_cap->cap & IEEE80211_HT_CAP_RX_STBC) {
2353                stbc = ht_cap->cap & IEEE80211_HT_CAP_RX_STBC;
2354                stbc = stbc >> IEEE80211_HT_CAP_RX_STBC_SHIFT;
2355                stbc = stbc << WMI_RC_RX_STBC_FLAG_S;
2356                arg->peer_rate_caps |= stbc;
2357                arg->peer_flags |= ar->wmi.peer_flags->stbc;
2358        }
2359
2360        if (ht_cap->mcs.rx_mask[1] && ht_cap->mcs.rx_mask[2])
2361                arg->peer_rate_caps |= WMI_RC_TS_FLAG;
2362        else if (ht_cap->mcs.rx_mask[1])
2363                arg->peer_rate_caps |= WMI_RC_DS_FLAG;
2364
2365        for (i = 0, n = 0, max_nss = 0; i < IEEE80211_HT_MCS_MASK_LEN * 8; i++)
2366                if ((ht_cap->mcs.rx_mask[i / 8] & BIT(i % 8)) &&
2367                    (ht_mcs_mask[i / 8] & BIT(i % 8))) {
2368                        max_nss = (i / 8) + 1;
2369                        arg->peer_ht_rates.rates[n++] = i;
2370                }
2371
2372        /*
2373         * This is a workaround for HT-enabled STAs which break the spec
2374         * and have no HT capabilities RX mask (no HT RX MCS map).
2375         *
2376         * As per spec, in section 20.3.5 Modulation and coding scheme (MCS),
2377         * MCS 0 through 7 are mandatory in 20MHz with 800 ns GI at all STAs.
2378         *
2379         * Firmware asserts if such situation occurs.
2380         */
2381        if (n == 0) {
2382                arg->peer_ht_rates.num_rates = 8;
2383                for (i = 0; i < arg->peer_ht_rates.num_rates; i++)
2384                        arg->peer_ht_rates.rates[i] = i;
2385        } else {
2386                arg->peer_ht_rates.num_rates = n;
2387                arg->peer_num_spatial_streams = min(sta->rx_nss, max_nss);
2388        }
2389
2390        ath10k_dbg(ar, ATH10K_DBG_MAC, "mac ht peer %pM mcs cnt %d nss %d\n",
2391                   arg->addr,
2392                   arg->peer_ht_rates.num_rates,
2393                   arg->peer_num_spatial_streams);
2394}
2395
2396static int ath10k_peer_assoc_qos_ap(struct ath10k *ar,
2397                                    struct ath10k_vif *arvif,
2398                                    struct ieee80211_sta *sta)
2399{
2400        u32 uapsd = 0;
2401        u32 max_sp = 0;
2402        int ret = 0;
2403
2404        lockdep_assert_held(&ar->conf_mutex);
2405
2406        if (sta->wme && sta->uapsd_queues) {
2407                ath10k_dbg(ar, ATH10K_DBG_MAC, "mac uapsd_queues 0x%x max_sp %d\n",
2408                           sta->uapsd_queues, sta->max_sp);
2409
2410                if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
2411                        uapsd |= WMI_AP_PS_UAPSD_AC3_DELIVERY_EN |
2412                                 WMI_AP_PS_UAPSD_AC3_TRIGGER_EN;
2413                if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
2414                        uapsd |= WMI_AP_PS_UAPSD_AC2_DELIVERY_EN |
2415                                 WMI_AP_PS_UAPSD_AC2_TRIGGER_EN;
2416                if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
2417                        uapsd |= WMI_AP_PS_UAPSD_AC1_DELIVERY_EN |
2418                                 WMI_AP_PS_UAPSD_AC1_TRIGGER_EN;
2419                if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
2420                        uapsd |= WMI_AP_PS_UAPSD_AC0_DELIVERY_EN |
2421                                 WMI_AP_PS_UAPSD_AC0_TRIGGER_EN;
2422
2423                if (sta->max_sp < MAX_WMI_AP_PS_PEER_PARAM_MAX_SP)
2424                        max_sp = sta->max_sp;
2425
2426                ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id,
2427                                                 sta->addr,
2428                                                 WMI_AP_PS_PEER_PARAM_UAPSD,
2429                                                 uapsd);
2430                if (ret) {
2431                        ath10k_warn(ar, "failed to set ap ps peer param uapsd for vdev %i: %d\n",
2432                                    arvif->vdev_id, ret);
2433                        return ret;
2434                }
2435
2436                ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id,
2437                                                 sta->addr,
2438                                                 WMI_AP_PS_PEER_PARAM_MAX_SP,
2439                                                 max_sp);
2440                if (ret) {
2441                        ath10k_warn(ar, "failed to set ap ps peer param max sp for vdev %i: %d\n",
2442                                    arvif->vdev_id, ret);
2443                        return ret;
2444                }
2445
2446                /* TODO setup this based on STA listen interval and
2447                 * beacon interval. Currently we don't know
2448                 * sta->listen_interval - mac80211 patch required.
2449                 * Currently use 10 seconds
2450                 */
2451                ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id, sta->addr,
2452                                                 WMI_AP_PS_PEER_PARAM_AGEOUT_TIME,
2453                                                 10);
2454                if (ret) {
2455                        ath10k_warn(ar, "failed to set ap ps peer param ageout time for vdev %i: %d\n",
2456                                    arvif->vdev_id, ret);
2457                        return ret;
2458                }
2459        }
2460
2461        return 0;
2462}
2463
2464static u16
2465ath10k_peer_assoc_h_vht_limit(u16 tx_mcs_set,
2466                              const u16 vht_mcs_limit[NL80211_VHT_NSS_MAX])
2467{
2468        int idx_limit;
2469        int nss;
2470        u16 mcs_map;
2471        u16 mcs;
2472
2473        for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++) {
2474                mcs_map = ath10k_mac_get_max_vht_mcs_map(tx_mcs_set, nss) &
2475                          vht_mcs_limit[nss];
2476
2477                if (mcs_map)
2478                        idx_limit = fls(mcs_map) - 1;
2479                else
2480                        idx_limit = -1;
2481
2482                switch (idx_limit) {
2483                case 0:
2484                case 1:
2485                case 2:
2486                case 3:
2487                case 4:
2488                case 5:
2489                case 6:
2490                default:
2491                        /* see ath10k_mac_can_set_bitrate_mask() */
2492                        WARN_ON(1);
2493                        fallthrough;
2494                case -1:
2495                        mcs = IEEE80211_VHT_MCS_NOT_SUPPORTED;
2496                        break;
2497                case 7:
2498                        mcs = IEEE80211_VHT_MCS_SUPPORT_0_7;
2499                        break;
2500                case 8:
2501                        mcs = IEEE80211_VHT_MCS_SUPPORT_0_8;
2502                        break;
2503                case 9:
2504                        mcs = IEEE80211_VHT_MCS_SUPPORT_0_9;
2505                        break;
2506                }
2507
2508                tx_mcs_set &= ~(0x3 << (nss * 2));
2509                tx_mcs_set |= mcs << (nss * 2);
2510        }
2511
2512        return tx_mcs_set;
2513}
2514
2515static u32 get_160mhz_nss_from_maxrate(int rate)
2516{
2517        u32 nss;
2518
2519        switch (rate) {
2520        case 780:
2521                nss = 1;
2522                break;
2523        case 1560:
2524                nss = 2;
2525                break;
2526        case 2106:
2527                nss = 3; /* not support MCS9 from spec*/
2528                break;
2529        case 3120:
2530                nss = 4;
2531                break;
2532        default:
2533                 nss = 1;
2534        }
2535
2536        return nss;
2537}
2538
2539static void ath10k_peer_assoc_h_vht(struct ath10k *ar,
2540                                    struct ieee80211_vif *vif,
2541                                    struct ieee80211_sta *sta,
2542                                    struct wmi_peer_assoc_complete_arg *arg)
2543{
2544        const struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap;
2545        struct ath10k_vif *arvif = (void *)vif->drv_priv;
2546        struct ath10k_hw_params *hw = &ar->hw_params;
2547        struct cfg80211_chan_def def;
2548        enum nl80211_band band;
2549        const u16 *vht_mcs_mask;
2550        u8 ampdu_factor;
2551        u8 max_nss, vht_mcs;
2552        int i;
2553
2554        if (WARN_ON(ath10k_mac_vif_chan(vif, &def)))
2555                return;
2556
2557        if (!vht_cap->vht_supported)
2558                return;
2559
2560        band = def.chan->band;
2561        vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
2562
2563        if (ath10k_peer_assoc_h_vht_masked(vht_mcs_mask))
2564                return;
2565
2566        arg->peer_flags |= ar->wmi.peer_flags->vht;
2567
2568        if (def.chan->band == NL80211_BAND_2GHZ)
2569                arg->peer_flags |= ar->wmi.peer_flags->vht_2g;
2570
2571        arg->peer_vht_caps = vht_cap->cap;
2572
2573        ampdu_factor = (vht_cap->cap &
2574                        IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK) >>
2575                       IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
2576
2577        /* Workaround: Some Netgear/Linksys 11ac APs set Rx A-MPDU factor to
2578         * zero in VHT IE. Using it would result in degraded throughput.
2579         * arg->peer_max_mpdu at this point contains HT max_mpdu so keep
2580         * it if VHT max_mpdu is smaller.
2581         */
2582        arg->peer_max_mpdu = max(arg->peer_max_mpdu,
2583                                 (1U << (IEEE80211_HT_MAX_AMPDU_FACTOR +
2584                                        ampdu_factor)) - 1);
2585
2586        if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
2587                arg->peer_flags |= ar->wmi.peer_flags->bw80;
2588
2589        if (sta->bandwidth == IEEE80211_STA_RX_BW_160)
2590                arg->peer_flags |= ar->wmi.peer_flags->bw160;
2591
2592        /* Calculate peer NSS capability from VHT capabilities if STA
2593         * supports VHT.
2594         */
2595        for (i = 0, max_nss = 0, vht_mcs = 0; i < NL80211_VHT_NSS_MAX; i++) {
2596                vht_mcs = __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map) >>
2597                          (2 * i) & 3;
2598
2599                if ((vht_mcs != IEEE80211_VHT_MCS_NOT_SUPPORTED) &&
2600                    vht_mcs_mask[i])
2601                        max_nss = i + 1;
2602        }
2603        arg->peer_num_spatial_streams = min(sta->rx_nss, max_nss);
2604        arg->peer_vht_rates.rx_max_rate =
2605                __le16_to_cpu(vht_cap->vht_mcs.rx_highest);
2606        arg->peer_vht_rates.rx_mcs_set =
2607                __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map);
2608        arg->peer_vht_rates.tx_max_rate =
2609                __le16_to_cpu(vht_cap->vht_mcs.tx_highest);
2610        arg->peer_vht_rates.tx_mcs_set = ath10k_peer_assoc_h_vht_limit(
2611                __le16_to_cpu(vht_cap->vht_mcs.tx_mcs_map), vht_mcs_mask);
2612
2613        /* Configure bandwidth-NSS mapping to FW
2614         * for the chip's tx chains setting on 160Mhz bw
2615         */
2616        if (arg->peer_phymode == MODE_11AC_VHT160 ||
2617            arg->peer_phymode == MODE_11AC_VHT80_80) {
2618                u32 rx_nss;
2619                u32 max_rate;
2620
2621                max_rate = arg->peer_vht_rates.rx_max_rate;
2622                rx_nss = get_160mhz_nss_from_maxrate(max_rate);
2623
2624                if (rx_nss == 0)
2625                        rx_nss = arg->peer_num_spatial_streams;
2626                else
2627                        rx_nss = min(arg->peer_num_spatial_streams, rx_nss);
2628
2629                max_rate = hw->vht160_mcs_tx_highest;
2630                rx_nss = min(rx_nss, get_160mhz_nss_from_maxrate(max_rate));
2631
2632                arg->peer_bw_rxnss_override =
2633                        FIELD_PREP(WMI_PEER_NSS_MAP_ENABLE, 1) |
2634                        FIELD_PREP(WMI_PEER_NSS_160MHZ_MASK, (rx_nss - 1));
2635
2636                if (arg->peer_phymode == MODE_11AC_VHT80_80) {
2637                        arg->peer_bw_rxnss_override |=
2638                        FIELD_PREP(WMI_PEER_NSS_80_80MHZ_MASK, (rx_nss - 1));
2639                }
2640        }
2641        ath10k_dbg(ar, ATH10K_DBG_MAC,
2642                   "mac vht peer %pM max_mpdu %d flags 0x%x peer_rx_nss_override 0x%x\n",
2643                   sta->addr, arg->peer_max_mpdu,
2644                   arg->peer_flags, arg->peer_bw_rxnss_override);
2645}
2646
2647static void ath10k_peer_assoc_h_qos(struct ath10k *ar,
2648                                    struct ieee80211_vif *vif,
2649                                    struct ieee80211_sta *sta,
2650                                    struct wmi_peer_assoc_complete_arg *arg)
2651{
2652        struct ath10k_vif *arvif = (void *)vif->drv_priv;
2653
2654        switch (arvif->vdev_type) {
2655        case WMI_VDEV_TYPE_AP:
2656                if (sta->wme)
2657                        arg->peer_flags |= arvif->ar->wmi.peer_flags->qos;
2658
2659                if (sta->wme && sta->uapsd_queues) {
2660                        arg->peer_flags |= arvif->ar->wmi.peer_flags->apsd;
2661                        arg->peer_rate_caps |= WMI_RC_UAPSD_FLAG;
2662                }
2663                break;
2664        case WMI_VDEV_TYPE_STA:
2665                if (sta->wme)
2666                        arg->peer_flags |= arvif->ar->wmi.peer_flags->qos;
2667                break;
2668        case WMI_VDEV_TYPE_IBSS:
2669                if (sta->wme)
2670                        arg->peer_flags |= arvif->ar->wmi.peer_flags->qos;
2671                break;
2672        default:
2673                break;
2674        }
2675
2676        ath10k_dbg(ar, ATH10K_DBG_MAC, "mac peer %pM qos %d\n",
2677                   sta->addr, !!(arg->peer_flags &
2678                   arvif->ar->wmi.peer_flags->qos));
2679}
2680
2681static bool ath10k_mac_sta_has_ofdm_only(struct ieee80211_sta *sta)
2682{
2683        return sta->supp_rates[NL80211_BAND_2GHZ] >>
2684               ATH10K_MAC_FIRST_OFDM_RATE_IDX;
2685}
2686
2687static enum wmi_phy_mode ath10k_mac_get_phymode_vht(struct ath10k *ar,
2688                                                    struct ieee80211_sta *sta)
2689{
2690        if (sta->bandwidth == IEEE80211_STA_RX_BW_160) {
2691                switch (sta->vht_cap.cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK) {
2692                case IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ:
2693                        return MODE_11AC_VHT160;
2694                case IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ:
2695                        return MODE_11AC_VHT80_80;
2696                default:
2697                        /* not sure if this is a valid case? */
2698                        return MODE_11AC_VHT160;
2699                }
2700        }
2701
2702        if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
2703                return MODE_11AC_VHT80;
2704
2705        if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
2706                return MODE_11AC_VHT40;
2707
2708        if (sta->bandwidth == IEEE80211_STA_RX_BW_20)
2709                return MODE_11AC_VHT20;
2710
2711        return MODE_UNKNOWN;
2712}
2713
2714static void ath10k_peer_assoc_h_phymode(struct ath10k *ar,
2715                                        struct ieee80211_vif *vif,
2716                                        struct ieee80211_sta *sta,
2717                                        struct wmi_peer_assoc_complete_arg *arg)
2718{
2719        struct ath10k_vif *arvif = (void *)vif->drv_priv;
2720        struct cfg80211_chan_def def;
2721        enum nl80211_band band;
2722        const u8 *ht_mcs_mask;
2723        const u16 *vht_mcs_mask;
2724        enum wmi_phy_mode phymode = MODE_UNKNOWN;
2725
2726        if (WARN_ON(ath10k_mac_vif_chan(vif, &def)))
2727                return;
2728
2729        band = def.chan->band;
2730        ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
2731        vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
2732
2733        switch (band) {
2734        case NL80211_BAND_2GHZ:
2735                if (sta->vht_cap.vht_supported &&
2736                    !ath10k_peer_assoc_h_vht_masked(vht_mcs_mask)) {
2737                        if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
2738                                phymode = MODE_11AC_VHT40;
2739                        else
2740                                phymode = MODE_11AC_VHT20;
2741                } else if (sta->ht_cap.ht_supported &&
2742                           !ath10k_peer_assoc_h_ht_masked(ht_mcs_mask)) {
2743                        if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
2744                                phymode = MODE_11NG_HT40;
2745                        else
2746                                phymode = MODE_11NG_HT20;
2747                } else if (ath10k_mac_sta_has_ofdm_only(sta)) {
2748                        phymode = MODE_11G;
2749                } else {
2750                        phymode = MODE_11B;
2751                }
2752
2753                break;
2754        case NL80211_BAND_5GHZ:
2755                /*
2756                 * Check VHT first.
2757                 */
2758                if (sta->vht_cap.vht_supported &&
2759                    !ath10k_peer_assoc_h_vht_masked(vht_mcs_mask)) {
2760                        phymode = ath10k_mac_get_phymode_vht(ar, sta);
2761                } else if (sta->ht_cap.ht_supported &&
2762                           !ath10k_peer_assoc_h_ht_masked(ht_mcs_mask)) {
2763                        if (sta->bandwidth >= IEEE80211_STA_RX_BW_40)
2764                                phymode = MODE_11NA_HT40;
2765                        else
2766                                phymode = MODE_11NA_HT20;
2767                } else {
2768                        phymode = MODE_11A;
2769                }
2770
2771                break;
2772        default:
2773                break;
2774        }
2775
2776        ath10k_dbg(ar, ATH10K_DBG_MAC, "mac peer %pM phymode %s\n",
2777                   sta->addr, ath10k_wmi_phymode_str(phymode));
2778
2779        arg->peer_phymode = phymode;
2780        WARN_ON(phymode == MODE_UNKNOWN);
2781}
2782
2783static int ath10k_peer_assoc_prepare(struct ath10k *ar,
2784                                     struct ieee80211_vif *vif,
2785                                     struct ieee80211_sta *sta,
2786                                     struct wmi_peer_assoc_complete_arg *arg)
2787{
2788        lockdep_assert_held(&ar->conf_mutex);
2789
2790        memset(arg, 0, sizeof(*arg));
2791
2792        ath10k_peer_assoc_h_basic(ar, vif, sta, arg);
2793        ath10k_peer_assoc_h_crypto(ar, vif, sta, arg);
2794        ath10k_peer_assoc_h_rates(ar, vif, sta, arg);
2795        ath10k_peer_assoc_h_ht(ar, vif, sta, arg);
2796        ath10k_peer_assoc_h_phymode(ar, vif, sta, arg);
2797        ath10k_peer_assoc_h_vht(ar, vif, sta, arg);
2798        ath10k_peer_assoc_h_qos(ar, vif, sta, arg);
2799
2800        return 0;
2801}
2802
2803static const u32 ath10k_smps_map[] = {
2804        [WLAN_HT_CAP_SM_PS_STATIC] = WMI_PEER_SMPS_STATIC,
2805        [WLAN_HT_CAP_SM_PS_DYNAMIC] = WMI_PEER_SMPS_DYNAMIC,
2806        [WLAN_HT_CAP_SM_PS_INVALID] = WMI_PEER_SMPS_PS_NONE,
2807        [WLAN_HT_CAP_SM_PS_DISABLED] = WMI_PEER_SMPS_PS_NONE,
2808};
2809
2810static int ath10k_setup_peer_smps(struct ath10k *ar, struct ath10k_vif *arvif,
2811                                  const u8 *addr,
2812                                  const struct ieee80211_sta_ht_cap *ht_cap)
2813{
2814        int smps;
2815
2816        if (!ht_cap->ht_supported)
2817                return 0;
2818
2819        smps = ht_cap->cap & IEEE80211_HT_CAP_SM_PS;
2820        smps >>= IEEE80211_HT_CAP_SM_PS_SHIFT;
2821
2822        if (smps >= ARRAY_SIZE(ath10k_smps_map))
2823                return -EINVAL;
2824
2825        return ath10k_wmi_peer_set_param(ar, arvif->vdev_id, addr,
2826                                         ar->wmi.peer_param->smps_state,
2827                                         ath10k_smps_map[smps]);
2828}
2829
2830static int ath10k_mac_vif_recalc_txbf(struct ath10k *ar,
2831                                      struct ieee80211_vif *vif,
2832                                      struct ieee80211_sta_vht_cap vht_cap)
2833{
2834        struct ath10k_vif *arvif = (void *)vif->drv_priv;
2835        int ret;
2836        u32 param;
2837        u32 value;
2838
2839        if (ath10k_wmi_get_txbf_conf_scheme(ar) != WMI_TXBF_CONF_AFTER_ASSOC)
2840                return 0;
2841
2842        if (!(ar->vht_cap_info &
2843              (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
2844               IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE |
2845               IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
2846               IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)))
2847                return 0;
2848
2849        param = ar->wmi.vdev_param->txbf;
2850        value = 0;
2851
2852        if (WARN_ON(param == WMI_VDEV_PARAM_UNSUPPORTED))
2853                return 0;
2854
2855        /* The following logic is correct. If a remote STA advertises support
2856         * for being a beamformer then we should enable us being a beamformee.
2857         */
2858
2859        if (ar->vht_cap_info &
2860            (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
2861             IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE)) {
2862                if (vht_cap.cap & IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)
2863                        value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFEE;
2864
2865                if (vht_cap.cap & IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)
2866                        value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFEE;
2867        }
2868
2869        if (ar->vht_cap_info &
2870            (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
2871             IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)) {
2872                if (vht_cap.cap & IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE)
2873                        value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFER;
2874
2875                if (vht_cap.cap & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE)
2876                        value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFER;
2877        }
2878
2879        if (value & WMI_VDEV_PARAM_TXBF_MU_TX_BFEE)
2880                value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFEE;
2881
2882        if (value & WMI_VDEV_PARAM_TXBF_MU_TX_BFER)
2883                value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFER;
2884
2885        ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param, value);
2886        if (ret) {
2887                ath10k_warn(ar, "failed to submit vdev param txbf 0x%x: %d\n",
2888                            value, ret);
2889                return ret;
2890        }
2891
2892        return 0;
2893}
2894
2895static bool ath10k_mac_is_connected(struct ath10k *ar)
2896{
2897        struct ath10k_vif *arvif;
2898
2899        list_for_each_entry(arvif, &ar->arvifs, list) {
2900                if (arvif->is_up && arvif->vdev_type == WMI_VDEV_TYPE_STA)
2901                        return true;
2902        }
2903
2904        return false;
2905}
2906
2907static int ath10k_mac_txpower_setup(struct ath10k *ar, int txpower)
2908{
2909        int ret;
2910        u32 param;
2911        int tx_power_2g, tx_power_5g;
2912        bool connected;
2913
2914        lockdep_assert_held(&ar->conf_mutex);
2915
2916        /* ath10k internally uses unit of 0.5 dBm so multiply by 2 */
2917        tx_power_2g = txpower * 2;
2918        tx_power_5g = txpower * 2;
2919
2920        connected = ath10k_mac_is_connected(ar);
2921
2922        if (connected && ar->tx_power_2g_limit)
2923                if (tx_power_2g > ar->tx_power_2g_limit)
2924                        tx_power_2g = ar->tx_power_2g_limit;
2925
2926        if (connected && ar->tx_power_5g_limit)
2927                if (tx_power_5g > ar->tx_power_5g_limit)
2928                        tx_power_5g = ar->tx_power_5g_limit;
2929
2930        ath10k_dbg(ar, ATH10K_DBG_MAC, "mac txpower 2g: %d, 5g: %d\n",
2931                   tx_power_2g, tx_power_5g);
2932
2933        param = ar->wmi.pdev_param->txpower_limit2g;
2934        ret = ath10k_wmi_pdev_set_param(ar, param, tx_power_2g);
2935        if (ret) {
2936                ath10k_warn(ar, "failed to set 2g txpower %d: %d\n",
2937                            tx_power_2g, ret);
2938                return ret;
2939        }
2940
2941        param = ar->wmi.pdev_param->txpower_limit5g;
2942        ret = ath10k_wmi_pdev_set_param(ar, param, tx_power_5g);
2943        if (ret) {
2944                ath10k_warn(ar, "failed to set 5g txpower %d: %d\n",
2945                            tx_power_5g, ret);
2946                return ret;
2947        }
2948
2949        return 0;
2950}
2951
2952static int ath10k_mac_txpower_recalc(struct ath10k *ar)
2953{
2954        struct ath10k_vif *arvif;
2955        int ret, txpower = -1;
2956
2957        lockdep_assert_held(&ar->conf_mutex);
2958
2959        list_for_each_entry(arvif, &ar->arvifs, list) {
2960                /* txpower not initialized yet? */
2961                if (arvif->txpower == INT_MIN)
2962                        continue;
2963
2964                if (txpower == -1)
2965                        txpower = arvif->txpower;
2966                else
2967                        txpower = min(txpower, arvif->txpower);
2968        }
2969
2970        if (txpower == -1)
2971                return 0;
2972
2973        ret = ath10k_mac_txpower_setup(ar, txpower);
2974        if (ret) {
2975                ath10k_warn(ar, "failed to setup tx power %d: %d\n",
2976                            txpower, ret);
2977                return ret;
2978        }
2979
2980        return 0;
2981}
2982
2983static int ath10k_mac_set_sar_power(struct ath10k *ar)
2984{
2985        if (!ar->hw_params.dynamic_sar_support)
2986                return -EOPNOTSUPP;
2987
2988        if (!ath10k_mac_is_connected(ar))
2989                return 0;
2990
2991        /* if connected, then arvif->txpower must be valid */
2992        return ath10k_mac_txpower_recalc(ar);
2993}
2994
2995static int ath10k_mac_set_sar_specs(struct ieee80211_hw *hw,
2996                                    const struct cfg80211_sar_specs *sar)
2997{
2998        const struct cfg80211_sar_sub_specs *sub_specs;
2999        struct ath10k *ar = hw->priv;
3000        u32 i;
3001        int ret;
3002
3003        mutex_lock(&ar->conf_mutex);
3004
3005        if (!ar->hw_params.dynamic_sar_support) {
3006                ret = -EOPNOTSUPP;
3007                goto err;
3008        }
3009
3010        if (!sar || sar->type != NL80211_SAR_TYPE_POWER ||
3011            sar->num_sub_specs == 0) {
3012                ret = -EINVAL;
3013                goto err;
3014        }
3015
3016        sub_specs = sar->sub_specs;
3017
3018        /* 0dbm is not a practical value for ath10k, so use 0
3019         * as no SAR limitation on it.
3020         */
3021        ar->tx_power_2g_limit = 0;
3022        ar->tx_power_5g_limit = 0;
3023
3024        /* note the power is in 0.25dbm unit, while ath10k uses
3025         * 0.5dbm unit.
3026         */
3027        for (i = 0; i < sar->num_sub_specs; i++) {
3028                if (sub_specs->freq_range_index == 0)
3029                        ar->tx_power_2g_limit = sub_specs->power / 2;
3030                else if (sub_specs->freq_range_index == 1)
3031                        ar->tx_power_5g_limit = sub_specs->power / 2;
3032
3033                sub_specs++;
3034        }
3035
3036        ret = ath10k_mac_set_sar_power(ar);
3037        if (ret) {
3038                ath10k_warn(ar, "failed to set sar power: %d", ret);
3039                goto err;
3040        }
3041
3042err:
3043        mutex_unlock(&ar->conf_mutex);
3044        return ret;
3045}
3046
3047/* can be called only in mac80211 callbacks due to `key_count` usage */
3048static void ath10k_bss_assoc(struct ieee80211_hw *hw,
3049                             struct ieee80211_vif *vif,
3050                             struct ieee80211_bss_conf *bss_conf)
3051{
3052        struct ath10k *ar = hw->priv;
3053        struct ath10k_vif *arvif = (void *)vif->drv_priv;
3054        struct ieee80211_sta_ht_cap ht_cap;
3055        struct ieee80211_sta_vht_cap vht_cap;
3056        struct wmi_peer_assoc_complete_arg peer_arg;
3057        struct ieee80211_sta *ap_sta;
3058        int ret;
3059
3060        lockdep_assert_held(&ar->conf_mutex);
3061
3062        ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i assoc bssid %pM aid %d\n",
3063                   arvif->vdev_id, arvif->bssid, arvif->aid);
3064
3065        rcu_read_lock();
3066
3067        ap_sta = ieee80211_find_sta(vif, bss_conf->bssid);
3068        if (!ap_sta) {
3069                ath10k_warn(ar, "failed to find station entry for bss %pM vdev %i\n",
3070                            bss_conf->bssid, arvif->vdev_id);
3071                rcu_read_unlock();
3072                return;
3073        }
3074
3075        /* ap_sta must be accessed only within rcu section which must be left
3076         * before calling ath10k_setup_peer_smps() which might sleep.
3077         */
3078        ht_cap = ap_sta->ht_cap;
3079        vht_cap = ap_sta->vht_cap;
3080
3081        ret = ath10k_peer_assoc_prepare(ar, vif, ap_sta, &peer_arg);
3082        if (ret) {
3083                ath10k_warn(ar, "failed to prepare peer assoc for %pM vdev %i: %d\n",
3084                            bss_conf->bssid, arvif->vdev_id, ret);
3085                rcu_read_unlock();
3086                return;
3087        }
3088
3089        rcu_read_unlock();
3090
3091        ret = ath10k_wmi_peer_assoc(ar, &peer_arg);
3092        if (ret) {
3093                ath10k_warn(ar, "failed to run peer assoc for %pM vdev %i: %d\n",
3094                            bss_conf->bssid, arvif->vdev_id, ret);
3095                return;
3096        }
3097
3098        ret = ath10k_setup_peer_smps(ar, arvif, bss_conf->bssid, &ht_cap);
3099        if (ret) {
3100                ath10k_warn(ar, "failed to setup peer SMPS for vdev %i: %d\n",
3101                            arvif->vdev_id, ret);
3102                return;
3103        }
3104
3105        ret = ath10k_mac_vif_recalc_txbf(ar, vif, vht_cap);
3106        if (ret) {
3107                ath10k_warn(ar, "failed to recalc txbf for vdev %i on bss %pM: %d\n",
3108                            arvif->vdev_id, bss_conf->bssid, ret);
3109                return;
3110        }
3111
3112        ath10k_dbg(ar, ATH10K_DBG_MAC,
3113                   "mac vdev %d up (associated) bssid %pM aid %d\n",
3114                   arvif->vdev_id, bss_conf->bssid, bss_conf->aid);
3115
3116        WARN_ON(arvif->is_up);
3117
3118        arvif->aid = bss_conf->aid;
3119        ether_addr_copy(arvif->bssid, bss_conf->bssid);
3120
3121        ret = ath10k_wmi_pdev_set_param(ar,
3122                                        ar->wmi.pdev_param->peer_stats_info_enable, 1);
3123        if (ret)
3124                ath10k_warn(ar, "failed to enable peer stats info: %d\n", ret);
3125
3126        ret = ath10k_wmi_vdev_up(ar, arvif->vdev_id, arvif->aid, arvif->bssid);
3127        if (ret) {
3128                ath10k_warn(ar, "failed to set vdev %d up: %d\n",
3129                            arvif->vdev_id, ret);
3130                return;
3131        }
3132
3133        arvif->is_up = true;
3134
3135        ath10k_mac_set_sar_power(ar);
3136
3137        /* Workaround: Some firmware revisions (tested with qca6174
3138         * WLAN.RM.2.0-00073) have buggy powersave state machine and must be
3139         * poked with peer param command.
3140         */
3141        ret = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, arvif->bssid,
3142                                        ar->wmi.peer_param->dummy_var, 1);
3143        if (ret) {
3144                ath10k_warn(ar, "failed to poke peer %pM param for ps workaround on vdev %i: %d\n",
3145                            arvif->bssid, arvif->vdev_id, ret);
3146                return;
3147        }
3148}
3149
3150static void ath10k_bss_disassoc(struct ieee80211_hw *hw,
3151                                struct ieee80211_vif *vif)
3152{
3153        struct ath10k *ar = hw->priv;
3154        struct ath10k_vif *arvif = (void *)vif->drv_priv;
3155        struct ieee80211_sta_vht_cap vht_cap = {};
3156        int ret;
3157
3158        lockdep_assert_held(&ar->conf_mutex);
3159
3160        ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i disassoc bssid %pM\n",
3161                   arvif->vdev_id, arvif->bssid);
3162
3163        ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
3164        if (ret)
3165                ath10k_warn(ar, "failed to down vdev %i: %d\n",
3166                            arvif->vdev_id, ret);
3167
3168        arvif->def_wep_key_idx = -1;
3169
3170        ret = ath10k_mac_vif_recalc_txbf(ar, vif, vht_cap);
3171        if (ret) {
3172                ath10k_warn(ar, "failed to recalc txbf for vdev %i: %d\n",
3173                            arvif->vdev_id, ret);
3174                return;
3175        }
3176
3177        arvif->is_up = false;
3178
3179        ath10k_mac_txpower_recalc(ar);
3180
3181        cancel_delayed_work_sync(&arvif->connection_loss_work);
3182}
3183
3184static int ath10k_new_peer_tid_config(struct ath10k *ar,
3185                                      struct ieee80211_sta *sta,
3186                                      struct ath10k_vif *arvif)
3187{
3188        struct wmi_per_peer_per_tid_cfg_arg arg = {};
3189        struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
3190        bool config_apply;
3191        int ret, i;
3192
3193        for (i = 0; i < ATH10K_TID_MAX; i++) {
3194                config_apply = false;
3195                if (arvif->retry_long[i] || arvif->ampdu[i] ||
3196                    arvif->rate_ctrl[i] || arvif->rtscts[i]) {
3197                        config_apply = true;
3198                        arg.tid = i;
3199                        arg.vdev_id = arvif->vdev_id;
3200                        arg.retry_count = arvif->retry_long[i];
3201                        arg.aggr_control = arvif->ampdu[i];
3202                        arg.rate_ctrl = arvif->rate_ctrl[i];
3203                        arg.rcode_flags = arvif->rate_code[i];
3204
3205                        if (arvif->rtscts[i])
3206                                arg.ext_tid_cfg_bitmap =
3207                                        WMI_EXT_TID_RTS_CTS_CONFIG;
3208                        else
3209                                arg.ext_tid_cfg_bitmap = 0;
3210
3211                        arg.rtscts_ctrl = arvif->rtscts[i];
3212                }
3213
3214                if (arvif->noack[i]) {
3215                        arg.ack_policy = arvif->noack[i];
3216                        arg.rate_ctrl = WMI_TID_CONFIG_RATE_CONTROL_DEFAULT_LOWEST_RATE;
3217                        arg.aggr_control = WMI_TID_CONFIG_AGGR_CONTROL_DISABLE;
3218                        config_apply = true;
3219                }
3220
3221                /* Assign default value(-1) to newly connected station.
3222                 * This is to identify station specific tid configuration not
3223                 * configured for the station.
3224                 */
3225                arsta->retry_long[i] = -1;
3226                arsta->noack[i] = -1;
3227                arsta->ampdu[i] = -1;
3228
3229                if (!config_apply)
3230                        continue;
3231
3232                ether_addr_copy(arg.peer_macaddr.addr, sta->addr);
3233
3234                ret = ath10k_wmi_set_per_peer_per_tid_cfg(ar, &arg);
3235                if (ret) {
3236                        ath10k_warn(ar, "failed to set per tid retry/aggr config for sta %pM: %d\n",
3237                                    sta->addr, ret);
3238                        return ret;
3239                }
3240
3241                memset(&arg, 0, sizeof(arg));
3242        }
3243
3244        return 0;
3245}
3246
3247static int ath10k_station_assoc(struct ath10k *ar,
3248                                struct ieee80211_vif *vif,
3249                                struct ieee80211_sta *sta,
3250                                bool reassoc)
3251{
3252        struct ath10k_vif *arvif = (void *)vif->drv_priv;
3253        struct wmi_peer_assoc_complete_arg peer_arg;
3254        int ret = 0;
3255
3256        lockdep_assert_held(&ar->conf_mutex);
3257
3258        ret = ath10k_peer_assoc_prepare(ar, vif, sta, &peer_arg);
3259        if (ret) {
3260                ath10k_warn(ar, "failed to prepare WMI peer assoc for %pM vdev %i: %i\n",
3261                            sta->addr, arvif->vdev_id, ret);
3262                return ret;
3263        }
3264
3265        ret = ath10k_wmi_peer_assoc(ar, &peer_arg);
3266        if (ret) {
3267                ath10k_warn(ar, "failed to run peer assoc for STA %pM vdev %i: %d\n",
3268                            sta->addr, arvif->vdev_id, ret);
3269                return ret;
3270        }
3271
3272        /* Re-assoc is run only to update supported rates for given station. It
3273         * doesn't make much sense to reconfigure the peer completely.
3274         */
3275        if (!reassoc) {
3276                ret = ath10k_setup_peer_smps(ar, arvif, sta->addr,
3277                                             &sta->ht_cap);
3278                if (ret) {
3279                        ath10k_warn(ar, "failed to setup peer SMPS for vdev %d: %d\n",
3280                                    arvif->vdev_id, ret);
3281                        return ret;
3282                }
3283
3284                ret = ath10k_peer_assoc_qos_ap(ar, arvif, sta);
3285                if (ret) {
3286                        ath10k_warn(ar, "failed to set qos params for STA %pM for vdev %i: %d\n",
3287                                    sta->addr, arvif->vdev_id, ret);
3288                        return ret;
3289                }
3290
3291                if (!sta->wme) {
3292                        arvif->num_legacy_stations++;
3293                        ret  = ath10k_recalc_rtscts_prot(arvif);
3294                        if (ret) {
3295                                ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
3296                                            arvif->vdev_id, ret);
3297                                return ret;
3298                        }
3299                }
3300
3301                /* Plumb cached keys only for static WEP */
3302                if ((arvif->def_wep_key_idx != -1) && (!sta->tdls)) {
3303                        ret = ath10k_install_peer_wep_keys(arvif, sta->addr);
3304                        if (ret) {
3305                                ath10k_warn(ar, "failed to install peer wep keys for vdev %i: %d\n",
3306                                            arvif->vdev_id, ret);
3307                                return ret;
3308                        }
3309                }
3310        }
3311
3312        if (!test_bit(WMI_SERVICE_PEER_TID_CONFIGS_SUPPORT, ar->wmi.svc_map))
3313                return ret;
3314
3315        return ath10k_new_peer_tid_config(ar, sta, arvif);
3316}
3317
3318static int ath10k_station_disassoc(struct ath10k *ar,
3319                                   struct ieee80211_vif *vif,
3320                                   struct ieee80211_sta *sta)
3321{
3322        struct ath10k_vif *arvif = (void *)vif->drv_priv;
3323        int ret = 0;
3324
3325        lockdep_assert_held(&ar->conf_mutex);
3326
3327        if (!sta->wme) {
3328                arvif->num_legacy_stations--;
3329                ret = ath10k_recalc_rtscts_prot(arvif);
3330                if (ret) {
3331                        ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
3332                                    arvif->vdev_id, ret);
3333                        return ret;
3334                }
3335        }
3336
3337        ret = ath10k_clear_peer_keys(arvif, sta->addr);
3338        if (ret) {
3339                ath10k_warn(ar, "failed to clear all peer wep keys for vdev %i: %d\n",
3340                            arvif->vdev_id, ret);
3341                return ret;
3342        }
3343
3344        return ret;
3345}
3346
3347/**************/
3348/* Regulatory */
3349/**************/
3350
3351static int ath10k_update_channel_list(struct ath10k *ar)
3352{
3353        struct ieee80211_hw *hw = ar->hw;
3354        struct ieee80211_supported_band **bands;
3355        enum nl80211_band band;
3356        struct ieee80211_channel *channel;
3357        struct wmi_scan_chan_list_arg arg = {0};
3358        struct wmi_channel_arg *ch;
3359        bool passive;
3360        int len;
3361        int ret;
3362        int i;
3363
3364        lockdep_assert_held(&ar->conf_mutex);
3365
3366        bands = hw->wiphy->bands;
3367        for (band = 0; band < NUM_NL80211_BANDS; band++) {
3368                if (!bands[band])
3369                        continue;
3370
3371                for (i = 0; i < bands[band]->n_channels; i++) {
3372                        if (bands[band]->channels[i].flags &
3373                            IEEE80211_CHAN_DISABLED)
3374                                continue;
3375
3376                        arg.n_channels++;
3377                }
3378        }
3379
3380        len = sizeof(struct wmi_channel_arg) * arg.n_channels;
3381        arg.channels = kzalloc(len, GFP_KERNEL);
3382        if (!arg.channels)
3383                return -ENOMEM;
3384
3385        ch = arg.channels;
3386        for (band = 0; band < NUM_NL80211_BANDS; band++) {
3387                if (!bands[band])
3388                        continue;
3389
3390                for (i = 0; i < bands[band]->n_channels; i++) {
3391                        channel = &bands[band]->channels[i];
3392
3393                        if (channel->flags & IEEE80211_CHAN_DISABLED)
3394                                continue;
3395
3396                        ch->allow_ht = true;
3397
3398                        /* FIXME: when should we really allow VHT? */
3399                        ch->allow_vht = true;
3400
3401                        ch->allow_ibss =
3402                                !(channel->flags & IEEE80211_CHAN_NO_IR);
3403
3404                        ch->ht40plus =
3405                                !(channel->flags & IEEE80211_CHAN_NO_HT40PLUS);
3406
3407                        ch->chan_radar =
3408                                !!(channel->flags & IEEE80211_CHAN_RADAR);
3409
3410                        passive = channel->flags & IEEE80211_CHAN_NO_IR;
3411                        ch->passive = passive;
3412
3413                        /* the firmware is ignoring the "radar" flag of the
3414                         * channel and is scanning actively using Probe Requests
3415                         * on "Radar detection"/DFS channels which are not
3416                         * marked as "available"
3417                         */
3418                        ch->passive |= ch->chan_radar;
3419
3420                        ch->freq = channel->center_freq;
3421                        ch->band_center_freq1 = channel->center_freq;
3422                        ch->min_power = 0;
3423                        ch->max_power = channel->max_power * 2;
3424                        ch->max_reg_power = channel->max_reg_power * 2;
3425                        ch->max_antenna_gain = channel->max_antenna_gain * 2;
3426                        ch->reg_class_id = 0; /* FIXME */
3427
3428                        /* FIXME: why use only legacy modes, why not any
3429                         * HT/VHT modes? Would that even make any
3430                         * difference?
3431                         */
3432                        if (channel->band == NL80211_BAND_2GHZ)
3433                                ch->mode = MODE_11G;
3434                        else
3435                                ch->mode = MODE_11A;
3436
3437                        if (WARN_ON_ONCE(ch->mode == MODE_UNKNOWN))
3438                                continue;
3439
3440                        ath10k_dbg(ar, ATH10K_DBG_WMI,
3441                                   "mac channel [%zd/%d] freq %d maxpower %d regpower %d antenna %d mode %d\n",
3442                                    ch - arg.channels, arg.n_channels,
3443                                   ch->freq, ch->max_power, ch->max_reg_power,
3444                                   ch->max_antenna_gain, ch->mode);
3445
3446                        ch++;
3447                }
3448        }
3449
3450        ret = ath10k_wmi_scan_chan_list(ar, &arg);
3451        kfree(arg.channels);
3452
3453        return ret;
3454}
3455
3456static enum wmi_dfs_region
3457ath10k_mac_get_dfs_region(enum nl80211_dfs_regions dfs_region)
3458{
3459        switch (dfs_region) {
3460        case NL80211_DFS_UNSET:
3461                return WMI_UNINIT_DFS_DOMAIN;
3462        case NL80211_DFS_FCC:
3463                return WMI_FCC_DFS_DOMAIN;
3464        case NL80211_DFS_ETSI:
3465                return WMI_ETSI_DFS_DOMAIN;
3466        case NL80211_DFS_JP:
3467                return WMI_MKK4_DFS_DOMAIN;
3468        }
3469        return WMI_UNINIT_DFS_DOMAIN;
3470}
3471
3472static void ath10k_regd_update(struct ath10k *ar)
3473{
3474        struct reg_dmn_pair_mapping *regpair;
3475        int ret;
3476        enum wmi_dfs_region wmi_dfs_reg;
3477        enum nl80211_dfs_regions nl_dfs_reg;
3478
3479        lockdep_assert_held(&ar->conf_mutex);
3480
3481        ret = ath10k_update_channel_list(ar);
3482        if (ret)
3483                ath10k_warn(ar, "failed to update channel list: %d\n", ret);
3484
3485        regpair = ar->ath_common.regulatory.regpair;
3486
3487        if (IS_ENABLED(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector) {
3488                nl_dfs_reg = ar->dfs_detector->region;
3489                wmi_dfs_reg = ath10k_mac_get_dfs_region(nl_dfs_reg);
3490        } else {
3491                wmi_dfs_reg = WMI_UNINIT_DFS_DOMAIN;
3492        }
3493
3494        /* Target allows setting up per-band regdomain but ath_common provides
3495         * a combined one only
3496         */
3497        ret = ath10k_wmi_pdev_set_regdomain(ar,
3498                                            regpair->reg_domain,
3499                                            regpair->reg_domain, /* 2ghz */
3500                                            regpair->reg_domain, /* 5ghz */
3501                                            regpair->reg_2ghz_ctl,
3502                                            regpair->reg_5ghz_ctl,
3503                                            wmi_dfs_reg);
3504        if (ret)
3505                ath10k_warn(ar, "failed to set pdev regdomain: %d\n", ret);
3506}
3507
3508static void ath10k_mac_update_channel_list(struct ath10k *ar,
3509                                           struct ieee80211_supported_band *band)
3510{
3511        int i;
3512
3513        if (ar->low_5ghz_chan && ar->high_5ghz_chan) {
3514                for (i = 0; i < band->n_channels; i++) {
3515                        if (band->channels[i].center_freq < ar->low_5ghz_chan ||
3516                            band->channels[i].center_freq > ar->high_5ghz_chan)
3517                                band->channels[i].flags |=
3518                                        IEEE80211_CHAN_DISABLED;
3519                }
3520        }
3521}
3522
3523static void ath10k_reg_notifier(struct wiphy *wiphy,
3524                                struct regulatory_request *request)
3525{
3526        struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
3527        struct ath10k *ar = hw->priv;
3528        bool result;
3529
3530        ath_reg_notifier_apply(wiphy, request, &ar->ath_common.regulatory);
3531
3532        if (IS_ENABLED(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector) {
3533                ath10k_dbg(ar, ATH10K_DBG_REGULATORY, "dfs region 0x%x\n",
3534                           request->dfs_region);
3535                result = ar->dfs_detector->set_dfs_domain(ar->dfs_detector,
3536                                                          request->dfs_region);
3537                if (!result)
3538                        ath10k_warn(ar, "DFS region 0x%X not supported, will trigger radar for every pulse\n",
3539                                    request->dfs_region);
3540        }
3541
3542        mutex_lock(&ar->conf_mutex);
3543        if (ar->state == ATH10K_STATE_ON)
3544                ath10k_regd_update(ar);
3545        mutex_unlock(&ar->conf_mutex);
3546
3547        if (ar->phy_capability & WHAL_WLAN_11A_CAPABILITY)
3548                ath10k_mac_update_channel_list(ar,
3549                                               ar->hw->wiphy->bands[NL80211_BAND_5GHZ]);
3550}
3551
3552static void ath10k_stop_radar_confirmation(struct ath10k *ar)
3553{
3554        spin_lock_bh(&ar->data_lock);
3555        ar->radar_conf_state = ATH10K_RADAR_CONFIRMATION_STOPPED;
3556        spin_unlock_bh(&ar->data_lock);
3557
3558        cancel_work_sync(&ar->radar_confirmation_work);
3559}
3560
3561/***************/
3562/* TX handlers */
3563/***************/
3564
3565enum ath10k_mac_tx_path {
3566        ATH10K_MAC_TX_HTT,
3567        ATH10K_MAC_TX_HTT_MGMT,
3568        ATH10K_MAC_TX_WMI_MGMT,
3569        ATH10K_MAC_TX_UNKNOWN,
3570};
3571
3572void ath10k_mac_tx_lock(struct ath10k *ar, int reason)
3573{
3574        lockdep_assert_held(&ar->htt.tx_lock);
3575
3576        WARN_ON(reason >= ATH10K_TX_PAUSE_MAX);
3577        ar->tx_paused |= BIT(reason);
3578        ieee80211_stop_queues(ar->hw);
3579}
3580
3581static void ath10k_mac_tx_unlock_iter(void *data, u8 *mac,
3582                                      struct ieee80211_vif *vif)
3583{
3584        struct ath10k *ar = data;
3585        struct ath10k_vif *arvif = (void *)vif->drv_priv;
3586
3587        if (arvif->tx_paused)
3588                return;
3589
3590        ieee80211_wake_queue(ar->hw, arvif->vdev_id);
3591}
3592
3593void ath10k_mac_tx_unlock(struct ath10k *ar, int reason)
3594{
3595        lockdep_assert_held(&ar->htt.tx_lock);
3596
3597        WARN_ON(reason >= ATH10K_TX_PAUSE_MAX);
3598        ar->tx_paused &= ~BIT(reason);
3599
3600        if (ar->tx_paused)
3601                return;
3602
3603        ieee80211_iterate_active_interfaces_atomic(ar->hw,
3604                                                   ATH10K_ITER_RESUME_FLAGS,
3605                                                   ath10k_mac_tx_unlock_iter,
3606                                                   ar);
3607
3608        ieee80211_wake_queue(ar->hw, ar->hw->offchannel_tx_hw_queue);
3609}
3610
3611void ath10k_mac_vif_tx_lock(struct ath10k_vif *arvif, int reason)
3612{
3613        struct ath10k *ar = arvif->ar;
3614
3615        lockdep_assert_held(&ar->htt.tx_lock);
3616
3617        WARN_ON(reason >= BITS_PER_LONG);
3618        arvif->tx_paused |= BIT(reason);
3619        ieee80211_stop_queue(ar->hw, arvif->vdev_id);
3620}
3621
3622void ath10k_mac_vif_tx_unlock(struct ath10k_vif *arvif, int reason)
3623{
3624        struct ath10k *ar = arvif->ar;
3625
3626        lockdep_assert_held(&ar->htt.tx_lock);
3627
3628        WARN_ON(reason >= BITS_PER_LONG);
3629        arvif->tx_paused &= ~BIT(reason);
3630
3631        if (ar->tx_paused)
3632                return;
3633
3634        if (arvif->tx_paused)
3635                return;
3636
3637        ieee80211_wake_queue(ar->hw, arvif->vdev_id);
3638}
3639
3640static void ath10k_mac_vif_handle_tx_pause(struct ath10k_vif *arvif,
3641                                           enum wmi_tlv_tx_pause_id pause_id,
3642                                           enum wmi_tlv_tx_pause_action action)
3643{
3644        struct ath10k *ar = arvif->ar;
3645
3646        lockdep_assert_held(&ar->htt.tx_lock);
3647
3648        switch (action) {
3649        case WMI_TLV_TX_PAUSE_ACTION_STOP:
3650                ath10k_mac_vif_tx_lock(arvif, pause_id);
3651                break;
3652        case WMI_TLV_TX_PAUSE_ACTION_WAKE:
3653                ath10k_mac_vif_tx_unlock(arvif, pause_id);
3654                break;
3655        default:
3656                ath10k_dbg(ar, ATH10K_DBG_BOOT,
3657                           "received unknown tx pause action %d on vdev %i, ignoring\n",
3658                            action, arvif->vdev_id);
3659                break;
3660        }
3661}
3662
3663struct ath10k_mac_tx_pause {
3664        u32 vdev_id;
3665        enum wmi_tlv_tx_pause_id pause_id;
3666        enum wmi_tlv_tx_pause_action action;
3667};
3668
3669static void ath10k_mac_handle_tx_pause_iter(void *data, u8 *mac,
3670                                            struct ieee80211_vif *vif)
3671{
3672        struct ath10k_vif *arvif = (void *)vif->drv_priv;
3673        struct ath10k_mac_tx_pause *arg = data;
3674
3675        if (arvif->vdev_id != arg->vdev_id)
3676                return;
3677
3678        ath10k_mac_vif_handle_tx_pause(arvif, arg->pause_id, arg->action);
3679}
3680
3681void ath10k_mac_handle_tx_pause_vdev(struct ath10k *ar, u32 vdev_id,
3682                                     enum wmi_tlv_tx_pause_id pause_id,
3683                                     enum wmi_tlv_tx_pause_action action)
3684{
3685        struct ath10k_mac_tx_pause arg = {
3686                .vdev_id = vdev_id,
3687                .pause_id = pause_id,
3688                .action = action,
3689        };
3690
3691        spin_lock_bh(&ar->htt.tx_lock);
3692        ieee80211_iterate_active_interfaces_atomic(ar->hw,
3693                                                   ATH10K_ITER_RESUME_FLAGS,
3694                                                   ath10k_mac_handle_tx_pause_iter,
3695                                                   &arg);
3696        spin_unlock_bh(&ar->htt.tx_lock);
3697}
3698
3699static enum ath10k_hw_txrx_mode
3700ath10k_mac_tx_h_get_txmode(struct ath10k *ar,
3701                           struct ieee80211_vif *vif,
3702                           struct ieee80211_sta *sta,
3703                           struct sk_buff *skb)
3704{
3705        const struct ieee80211_hdr *hdr = (void *)skb->data;
3706        const struct ath10k_skb_cb *skb_cb = ATH10K_SKB_CB(skb);
3707        __le16 fc = hdr->frame_control;
3708
3709        if (!vif || vif->type == NL80211_IFTYPE_MONITOR)
3710                return ATH10K_HW_TXRX_RAW;
3711
3712        if (ieee80211_is_mgmt(fc))
3713                return ATH10K_HW_TXRX_MGMT;
3714
3715        /* Workaround:
3716         *
3717         * NullFunc frames are mostly used to ping if a client or AP are still
3718         * reachable and responsive. This implies tx status reports must be
3719         * accurate - otherwise either mac80211 or userspace (e.g. hostapd) can
3720         * come to a conclusion that the other end disappeared and tear down
3721         * BSS connection or it can never disconnect from BSS/client (which is
3722         * the case).
3723         *
3724         * Firmware with HTT older than 3.0 delivers incorrect tx status for
3725         * NullFunc frames to driver. However there's a HTT Mgmt Tx command
3726         * which seems to deliver correct tx reports for NullFunc frames. The
3727         * downside of using it is it ignores client powersave state so it can
3728         * end up disconnecting sleeping clients in AP mode. It should fix STA
3729         * mode though because AP don't sleep.
3730         */
3731        if (ar->htt.target_version_major < 3 &&
3732            (ieee80211_is_nullfunc(fc) || ieee80211_is_qos_nullfunc(fc)) &&
3733            !test_bit(ATH10K_FW_FEATURE_HAS_WMI_MGMT_TX,
3734                      ar->running_fw->fw_file.fw_features))
3735                return ATH10K_HW_TXRX_MGMT;
3736
3737        /* Workaround:
3738         *
3739         * Some wmi-tlv firmwares for qca6174 have broken Tx key selection for
3740         * NativeWifi txmode - it selects AP key instead of peer key. It seems
3741         * to work with Ethernet txmode so use it.
3742         *
3743         * FIXME: Check if raw mode works with TDLS.
3744         */
3745        if (ieee80211_is_data_present(fc) && sta && sta->tdls)
3746                return ATH10K_HW_TXRX_ETHERNET;
3747
3748        if (test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags) ||
3749            skb_cb->flags & ATH10K_SKB_F_RAW_TX)
3750                return ATH10K_HW_TXRX_RAW;
3751
3752        return ATH10K_HW_TXRX_NATIVE_WIFI;
3753}
3754
3755static bool ath10k_tx_h_use_hwcrypto(struct ieee80211_vif *vif,
3756                                     struct sk_buff *skb)
3757{
3758        const struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
3759        const struct ieee80211_hdr *hdr = (void *)skb->data;
3760        const u32 mask = IEEE80211_TX_INTFL_DONT_ENCRYPT |
3761                         IEEE80211_TX_CTL_INJECTED;
3762
3763        if (!ieee80211_has_protected(hdr->frame_control))
3764                return false;
3765
3766        if ((info->flags & mask) == mask)
3767                return false;
3768
3769        if (vif)
3770                return !((struct ath10k_vif *)vif->drv_priv)->nohwcrypt;
3771
3772        return true;
3773}
3774
3775/* HTT Tx uses Native Wifi tx mode which expects 802.11 frames without QoS
3776 * Control in the header.
3777 */
3778static void ath10k_tx_h_nwifi(struct ieee80211_hw *hw, struct sk_buff *skb)
3779{
3780        struct ieee80211_hdr *hdr = (void *)skb->data;
3781        struct ath10k_skb_cb *cb = ATH10K_SKB_CB(skb);
3782        u8 *qos_ctl;
3783
3784        if (!ieee80211_is_data_qos(hdr->frame_control))
3785                return;
3786
3787        qos_ctl = ieee80211_get_qos_ctl(hdr);
3788        memmove(skb->data + IEEE80211_QOS_CTL_LEN,
3789                skb->data, (void *)qos_ctl - (void *)skb->data);
3790        skb_pull(skb, IEEE80211_QOS_CTL_LEN);
3791
3792        /* Some firmware revisions don't handle sending QoS NullFunc well.
3793         * These frames are mainly used for CQM purposes so it doesn't really
3794         * matter whether QoS NullFunc or NullFunc are sent.
3795         */
3796        hdr = (void *)skb->data;
3797        if (ieee80211_is_qos_nullfunc(hdr->frame_control))
3798                cb->flags &= ~ATH10K_SKB_F_QOS;
3799
3800        hdr->frame_control &= ~__cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
3801}
3802
3803static void ath10k_tx_h_8023(struct sk_buff *skb)
3804{
3805        struct ieee80211_hdr *hdr;
3806        struct rfc1042_hdr *rfc1042;
3807        struct ethhdr *eth;
3808        size_t hdrlen;
3809        u8 da[ETH_ALEN];
3810        u8 sa[ETH_ALEN];
3811        __be16 type;
3812
3813        hdr = (void *)skb->data;
3814        hdrlen = ieee80211_hdrlen(hdr->frame_control);
3815        rfc1042 = (void *)skb->data + hdrlen;
3816
3817        ether_addr_copy(da, ieee80211_get_DA(hdr));
3818        ether_addr_copy(sa, ieee80211_get_SA(hdr));
3819        type = rfc1042->snap_type;
3820
3821        skb_pull(skb, hdrlen + sizeof(*rfc1042));
3822        skb_push(skb, sizeof(*eth));
3823
3824        eth = (void *)skb->data;
3825        ether_addr_copy(eth->h_dest, da);
3826        ether_addr_copy(eth->h_source, sa);
3827        eth->h_proto = type;
3828}
3829
3830static void ath10k_tx_h_add_p2p_noa_ie(struct ath10k *ar,
3831                                       struct ieee80211_vif *vif,
3832                                       struct sk_buff *skb)
3833{
3834        struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
3835        struct ath10k_vif *arvif = (void *)vif->drv_priv;
3836
3837        /* This is case only for P2P_GO */
3838        if (vif->type != NL80211_IFTYPE_AP || !vif->p2p)
3839                return;
3840
3841        if (unlikely(ieee80211_is_probe_resp(hdr->frame_control))) {
3842                spin_lock_bh(&ar->data_lock);
3843                if (arvif->u.ap.noa_data)
3844                        if (!pskb_expand_head(skb, 0, arvif->u.ap.noa_len,
3845                                              GFP_ATOMIC))
3846                                skb_put_data(skb, arvif->u.ap.noa_data,
3847                                             arvif->u.ap.noa_len);
3848                spin_unlock_bh(&ar->data_lock);
3849        }
3850}
3851
3852static void ath10k_mac_tx_h_fill_cb(struct ath10k *ar,
3853                                    struct ieee80211_vif *vif,
3854                                    struct ieee80211_txq *txq,
3855                                    struct ieee80211_sta *sta,
3856                                    struct sk_buff *skb, u16 airtime)
3857{
3858        struct ieee80211_hdr *hdr = (void *)skb->data;
3859        struct ath10k_skb_cb *cb = ATH10K_SKB_CB(skb);
3860        const struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
3861        bool is_data = ieee80211_is_data(hdr->frame_control) ||
3862                        ieee80211_is_data_qos(hdr->frame_control);
3863        struct ath10k_vif *arvif = (void *)vif->drv_priv;
3864        struct ath10k_sta *arsta;
3865        u8 tid, *qos_ctl;
3866        bool noack = false;
3867
3868        cb->flags = 0;
3869        if (!ath10k_tx_h_use_hwcrypto(vif, skb))
3870                cb->flags |= ATH10K_SKB_F_NO_HWCRYPT;
3871
3872        if (ieee80211_is_mgmt(hdr->frame_control))
3873                cb->flags |= ATH10K_SKB_F_MGMT;
3874
3875        if (ieee80211_is_data_qos(hdr->frame_control)) {
3876                cb->flags |= ATH10K_SKB_F_QOS;
3877                qos_ctl = ieee80211_get_qos_ctl(hdr);
3878                tid = (*qos_ctl) & IEEE80211_QOS_CTL_TID_MASK;
3879
3880                if (arvif->noack[tid] == WMI_PEER_TID_CONFIG_NOACK)
3881                        noack = true;
3882
3883                if (sta) {
3884                        arsta = (struct ath10k_sta *)sta->drv_priv;
3885
3886                        if (arsta->noack[tid] == WMI_PEER_TID_CONFIG_NOACK)
3887                                noack = true;
3888
3889                        if (arsta->noack[tid] == WMI_PEER_TID_CONFIG_ACK)
3890                                noack = false;
3891                }
3892
3893                if (noack)
3894                        cb->flags |= ATH10K_SKB_F_NOACK_TID;
3895        }
3896
3897        /* Data frames encrypted in software will be posted to firmware
3898         * with tx encap mode set to RAW. Ex: Multicast traffic generated
3899         * for a specific VLAN group will always be encrypted in software.
3900         */
3901        if (is_data && ieee80211_has_protected(hdr->frame_control) &&
3902            !info->control.hw_key) {
3903                cb->flags |= ATH10K_SKB_F_NO_HWCRYPT;
3904                cb->flags |= ATH10K_SKB_F_RAW_TX;
3905        }
3906
3907        cb->vif = vif;
3908        cb->txq = txq;
3909        cb->airtime_est = airtime;
3910        if (sta) {
3911                arsta = (struct ath10k_sta *)sta->drv_priv;
3912                spin_lock_bh(&ar->data_lock);
3913                cb->ucast_cipher = arsta->ucast_cipher;
3914                spin_unlock_bh(&ar->data_lock);
3915        }
3916}
3917
3918bool ath10k_mac_tx_frm_has_freq(struct ath10k *ar)
3919{
3920        /* FIXME: Not really sure since when the behaviour changed. At some
3921         * point new firmware stopped requiring creation of peer entries for
3922         * offchannel tx (and actually creating them causes issues with wmi-htc
3923         * tx credit replenishment and reliability). Assuming it's at least 3.4
3924         * because that's when the `freq` was introduced to TX_FRM HTT command.
3925         */
3926        return (ar->htt.target_version_major >= 3 &&
3927                ar->htt.target_version_minor >= 4 &&
3928                ar->running_fw->fw_file.htt_op_version == ATH10K_FW_HTT_OP_VERSION_TLV);
3929}
3930
3931static int ath10k_mac_tx_wmi_mgmt(struct ath10k *ar, struct sk_buff *skb)
3932{
3933        struct sk_buff_head *q = &ar->wmi_mgmt_tx_queue;
3934
3935        if (skb_queue_len_lockless(q) >= ATH10K_MAX_NUM_MGMT_PENDING) {
3936                ath10k_warn(ar, "wmi mgmt tx queue is full\n");
3937                return -ENOSPC;
3938        }
3939
3940        skb_queue_tail(q, skb);
3941        ieee80211_queue_work(ar->hw, &ar->wmi_mgmt_tx_work);
3942
3943        return 0;
3944}
3945
3946static enum ath10k_mac_tx_path
3947ath10k_mac_tx_h_get_txpath(struct ath10k *ar,
3948                           struct sk_buff *skb,
3949                           enum ath10k_hw_txrx_mode txmode)
3950{
3951        switch (txmode) {
3952        case ATH10K_HW_TXRX_RAW:
3953        case ATH10K_HW_TXRX_NATIVE_WIFI:
3954        case ATH10K_HW_TXRX_ETHERNET:
3955                return ATH10K_MAC_TX_HTT;
3956        case ATH10K_HW_TXRX_MGMT:
3957                if (test_bit(ATH10K_FW_FEATURE_HAS_WMI_MGMT_TX,
3958                             ar->running_fw->fw_file.fw_features) ||
3959                             test_bit(WMI_SERVICE_MGMT_TX_WMI,
3960                                      ar->wmi.svc_map))
3961                        return ATH10K_MAC_TX_WMI_MGMT;
3962                else if (ar->htt.target_version_major >= 3)
3963                        return ATH10K_MAC_TX_HTT;
3964                else
3965                        return ATH10K_MAC_TX_HTT_MGMT;
3966        }
3967
3968        return ATH10K_MAC_TX_UNKNOWN;
3969}
3970
3971static int ath10k_mac_tx_submit(struct ath10k *ar,
3972                                enum ath10k_hw_txrx_mode txmode,
3973                                enum ath10k_mac_tx_path txpath,
3974                                struct sk_buff *skb)
3975{
3976        struct ath10k_htt *htt = &ar->htt;
3977        int ret = -EINVAL;
3978
3979        switch (txpath) {
3980        case ATH10K_MAC_TX_HTT:
3981                ret = ath10k_htt_tx(htt, txmode, skb);
3982                break;
3983        case ATH10K_MAC_TX_HTT_MGMT:
3984                ret = ath10k_htt_mgmt_tx(htt, skb);
3985                break;
3986        case ATH10K_MAC_TX_WMI_MGMT:
3987                ret = ath10k_mac_tx_wmi_mgmt(ar, skb);
3988                break;
3989        case ATH10K_MAC_TX_UNKNOWN:
3990                WARN_ON_ONCE(1);
3991                ret = -EINVAL;
3992                break;
3993        }
3994
3995        if (ret) {
3996                ath10k_warn(ar, "failed to transmit packet, dropping: %d\n",
3997                            ret);
3998                ieee80211_free_txskb(ar->hw, skb);
3999        }
4000
4001        return ret;
4002}
4003
4004/* This function consumes the sk_buff regardless of return value as far as
4005 * caller is concerned so no freeing is necessary afterwards.
4006 */
4007static int ath10k_mac_tx(struct ath10k *ar,
4008                         struct ieee80211_vif *vif,
4009                         enum ath10k_hw_txrx_mode txmode,
4010                         enum ath10k_mac_tx_path txpath,
4011                         struct sk_buff *skb, bool noque_offchan)
4012{
4013        struct ieee80211_hw *hw = ar->hw;
4014        struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
4015        const struct ath10k_skb_cb *skb_cb = ATH10K_SKB_CB(skb);
4016        int ret;
4017
4018        /* We should disable CCK RATE due to P2P */
4019        if (info->flags & IEEE80211_TX_CTL_NO_CCK_RATE)
4020                ath10k_dbg(ar, ATH10K_DBG_MAC, "IEEE80211_TX_CTL_NO_CCK_RATE\n");
4021
4022        switch (txmode) {
4023        case ATH10K_HW_TXRX_MGMT:
4024        case ATH10K_HW_TXRX_NATIVE_WIFI:
4025                ath10k_tx_h_nwifi(hw, skb);
4026                ath10k_tx_h_add_p2p_noa_ie(ar, vif, skb);
4027                ath10k_tx_h_seq_no(vif, skb);
4028                break;
4029        case ATH10K_HW_TXRX_ETHERNET:
4030                ath10k_tx_h_8023(skb);
4031                break;
4032        case ATH10K_HW_TXRX_RAW:
4033                if (!test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags) &&
4034                    !(skb_cb->flags & ATH10K_SKB_F_RAW_TX)) {
4035                        WARN_ON_ONCE(1);
4036                        ieee80211_free_txskb(hw, skb);
4037                        return -ENOTSUPP;
4038                }
4039        }
4040
4041        if (!noque_offchan && info->flags & IEEE80211_TX_CTL_TX_OFFCHAN) {
4042                if (!ath10k_mac_tx_frm_has_freq(ar)) {
4043                        ath10k_dbg(ar, ATH10K_DBG_MAC, "mac queued offchannel skb %pK len %d\n",
4044                                   skb, skb->len);
4045
4046                        skb_queue_tail(&ar->offchan_tx_queue, skb);
4047                        ieee80211_queue_work(hw, &ar->offchan_tx_work);
4048                        return 0;
4049                }
4050        }
4051
4052        ret = ath10k_mac_tx_submit(ar, txmode, txpath, skb);
4053        if (ret) {
4054                ath10k_warn(ar, "failed to submit frame: %d\n", ret);
4055                return ret;
4056        }
4057
4058        return 0;
4059}
4060
4061void ath10k_offchan_tx_purge(struct ath10k *ar)
4062{
4063        struct sk_buff *skb;
4064
4065        for (;;) {
4066                skb = skb_dequeue(&ar->offchan_tx_queue);
4067                if (!skb)
4068                        break;
4069
4070                ieee80211_free_txskb(ar->hw, skb);
4071        }
4072}
4073
4074void ath10k_offchan_tx_work(struct work_struct *work)
4075{
4076        struct ath10k *ar = container_of(work, struct ath10k, offchan_tx_work);
4077        struct ath10k_peer *peer;
4078        struct ath10k_vif *arvif;
4079        enum ath10k_hw_txrx_mode txmode;
4080        enum ath10k_mac_tx_path txpath;
4081        struct ieee80211_hdr *hdr;
4082        struct ieee80211_vif *vif;
4083        struct ieee80211_sta *sta;
4084        struct sk_buff *skb;
4085        const u8 *peer_addr;
4086        int vdev_id;
4087        int ret;
4088        unsigned long time_left;
4089        bool tmp_peer_created = false;
4090
4091        /* FW requirement: We must create a peer before FW will send out
4092         * an offchannel frame. Otherwise the frame will be stuck and
4093         * never transmitted. We delete the peer upon tx completion.
4094         * It is unlikely that a peer for offchannel tx will already be
4095         * present. However it may be in some rare cases so account for that.
4096         * Otherwise we might remove a legitimate peer and break stuff.
4097         */
4098
4099        for (;;) {
4100                skb = skb_dequeue(&ar->offchan_tx_queue);
4101                if (!skb)
4102                        break;
4103
4104                mutex_lock(&ar->conf_mutex);
4105
4106                ath10k_dbg(ar, ATH10K_DBG_MAC, "mac offchannel skb %pK len %d\n",
4107                           skb, skb->len);
4108
4109                hdr = (struct ieee80211_hdr *)skb->data;
4110                peer_addr = ieee80211_get_DA(hdr);
4111
4112                spin_lock_bh(&ar->data_lock);
4113                vdev_id = ar->scan.vdev_id;
4114                peer = ath10k_peer_find(ar, vdev_id, peer_addr);
4115                spin_unlock_bh(&ar->data_lock);
4116
4117                if (peer)
4118                        ath10k_warn(ar, "peer %pM on vdev %d already present\n",
4119                                    peer_addr, vdev_id);
4120
4121                if (!peer) {
4122                        ret = ath10k_peer_create(ar, NULL, NULL, vdev_id,
4123                                                 peer_addr,
4124                                                 WMI_PEER_TYPE_DEFAULT);
4125                        if (ret)
4126                                ath10k_warn(ar, "failed to create peer %pM on vdev %d: %d\n",
4127                                            peer_addr, vdev_id, ret);
4128                        tmp_peer_created = (ret == 0);
4129                }
4130
4131                spin_lock_bh(&ar->data_lock);
4132                reinit_completion(&ar->offchan_tx_completed);
4133                ar->offchan_tx_skb = skb;
4134                spin_unlock_bh(&ar->data_lock);
4135
4136                /* It's safe to access vif and sta - conf_mutex guarantees that
4137                 * sta_state() and remove_interface() are locked exclusively
4138                 * out wrt to this offchannel worker.
4139                 */
4140                arvif = ath10k_get_arvif(ar, vdev_id);
4141                if (arvif) {
4142                        vif = arvif->vif;
4143                        sta = ieee80211_find_sta(vif, peer_addr);
4144                } else {
4145                        vif = NULL;
4146                        sta = NULL;
4147                }
4148
4149                txmode = ath10k_mac_tx_h_get_txmode(ar, vif, sta, skb);
4150                txpath = ath10k_mac_tx_h_get_txpath(ar, skb, txmode);
4151
4152                ret = ath10k_mac_tx(ar, vif, txmode, txpath, skb, true);
4153                if (ret) {
4154                        ath10k_warn(ar, "failed to transmit offchannel frame: %d\n",
4155                                    ret);
4156                        /* not serious */
4157                }
4158
4159                time_left =
4160                wait_for_completion_timeout(&ar->offchan_tx_completed, 3 * HZ);
4161                if (time_left == 0)
4162                        ath10k_warn(ar, "timed out waiting for offchannel skb %pK, len: %d\n",
4163                                    skb, skb->len);
4164
4165                if (!peer && tmp_peer_created) {
4166                        ret = ath10k_peer_delete(ar, vdev_id, peer_addr);
4167                        if (ret)
4168                                ath10k_warn(ar, "failed to delete peer %pM on vdev %d: %d\n",
4169                                            peer_addr, vdev_id, ret);
4170                }
4171
4172                mutex_unlock(&ar->conf_mutex);
4173        }
4174}
4175
4176void ath10k_mgmt_over_wmi_tx_purge(struct ath10k *ar)
4177{
4178        struct sk_buff *skb;
4179