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
4180        for (;;) {
4181                skb = skb_dequeue(&ar->wmi_mgmt_tx_queue);
4182                if (!skb)
4183                        break;
4184
4185                ieee80211_free_txskb(ar->hw, skb);
4186        }
4187}
4188
4189void ath10k_mgmt_over_wmi_tx_work(struct work_struct *work)
4190{
4191        struct ath10k *ar = container_of(work, struct ath10k, wmi_mgmt_tx_work);
4192        struct sk_buff *skb;
4193        dma_addr_t paddr;
4194        int ret;
4195
4196        for (;;) {
4197                skb = skb_dequeue(&ar->wmi_mgmt_tx_queue);
4198                if (!skb)
4199                        break;
4200
4201                if (test_bit(ATH10K_FW_FEATURE_MGMT_TX_BY_REF,
4202                             ar->running_fw->fw_file.fw_features)) {
4203                        paddr = dma_map_single(ar->dev, skb->data,
4204                                               skb->len, DMA_TO_DEVICE);
4205                        if (dma_mapping_error(ar->dev, paddr)) {
4206                                ieee80211_free_txskb(ar->hw, skb);
4207                                continue;
4208                        }
4209                        ret = ath10k_wmi_mgmt_tx_send(ar, skb, paddr);
4210                        if (ret) {
4211                                ath10k_warn(ar, "failed to transmit management frame by ref via WMI: %d\n",
4212                                            ret);
4213                                /* remove this msdu from idr tracking */
4214                                ath10k_wmi_cleanup_mgmt_tx_send(ar, skb);
4215
4216                                dma_unmap_single(ar->dev, paddr, skb->len,
4217                                                 DMA_TO_DEVICE);
4218                                ieee80211_free_txskb(ar->hw, skb);
4219                        }
4220                } else {
4221                        ret = ath10k_wmi_mgmt_tx(ar, skb);
4222                        if (ret) {
4223                                ath10k_warn(ar, "failed to transmit management frame via WMI: %d\n",
4224                                            ret);
4225                                ieee80211_free_txskb(ar->hw, skb);
4226                        }
4227                }
4228        }
4229}
4230
4231static void ath10k_mac_txq_init(struct ieee80211_txq *txq)
4232{
4233        struct ath10k_txq *artxq;
4234
4235        if (!txq)
4236                return;
4237
4238        artxq = (void *)txq->drv_priv;
4239        INIT_LIST_HEAD(&artxq->list);
4240}
4241
4242static void ath10k_mac_txq_unref(struct ath10k *ar, struct ieee80211_txq *txq)
4243{
4244        struct ath10k_skb_cb *cb;
4245        struct sk_buff *msdu;
4246        int msdu_id;
4247
4248        if (!txq)
4249                return;
4250
4251        spin_lock_bh(&ar->htt.tx_lock);
4252        idr_for_each_entry(&ar->htt.pending_tx, msdu, msdu_id) {
4253                cb = ATH10K_SKB_CB(msdu);
4254                if (cb->txq == txq)
4255                        cb->txq = NULL;
4256        }
4257        spin_unlock_bh(&ar->htt.tx_lock);
4258}
4259
4260struct ieee80211_txq *ath10k_mac_txq_lookup(struct ath10k *ar,
4261                                            u16 peer_id,
4262                                            u8 tid)
4263{
4264        struct ath10k_peer *peer;
4265
4266        lockdep_assert_held(&ar->data_lock);
4267
4268        peer = ar->peer_map[peer_id];
4269        if (!peer)
4270                return NULL;
4271
4272        if (peer->removed)
4273                return NULL;
4274
4275        if (peer->sta)
4276                return peer->sta->txq[tid];
4277        else if (peer->vif)
4278                return peer->vif->txq;
4279        else
4280                return NULL;
4281}
4282
4283static bool ath10k_mac_tx_can_push(struct ieee80211_hw *hw,
4284                                   struct ieee80211_txq *txq)
4285{
4286        struct ath10k *ar = hw->priv;
4287        struct ath10k_txq *artxq = (void *)txq->drv_priv;
4288
4289        /* No need to get locks */
4290        if (ar->htt.tx_q_state.mode == HTT_TX_MODE_SWITCH_PUSH)
4291                return true;
4292
4293        if (ar->htt.num_pending_tx < ar->htt.tx_q_state.num_push_allowed)
4294                return true;
4295
4296        if (artxq->num_fw_queued < artxq->num_push_allowed)
4297                return true;
4298
4299        return false;
4300}
4301
4302/* Return estimated airtime in microsecond, which is calculated using last
4303 * reported TX rate. This is just a rough estimation because host driver has no
4304 * knowledge of the actual transmit rate, retries or aggregation. If actual
4305 * airtime can be reported by firmware, then delta between estimated and actual
4306 * airtime can be adjusted from deficit.
4307 */
4308#define IEEE80211_ATF_OVERHEAD          100     /* IFS + some slot time */
4309#define IEEE80211_ATF_OVERHEAD_IFS      16      /* IFS only */
4310static u16 ath10k_mac_update_airtime(struct ath10k *ar,
4311                                     struct ieee80211_txq *txq,
4312                                     struct sk_buff *skb)
4313{
4314        struct ath10k_sta *arsta;
4315        u32 pktlen;
4316        u16 airtime = 0;
4317
4318        if (!txq || !txq->sta)
4319                return airtime;
4320
4321        if (test_bit(WMI_SERVICE_REPORT_AIRTIME, ar->wmi.svc_map))
4322                return airtime;
4323
4324        spin_lock_bh(&ar->data_lock);
4325        arsta = (struct ath10k_sta *)txq->sta->drv_priv;
4326
4327        pktlen = skb->len + 38; /* Assume MAC header 30, SNAP 8 for most case */
4328        if (arsta->last_tx_bitrate) {
4329                /* airtime in us, last_tx_bitrate in 100kbps */
4330                airtime = (pktlen * 8 * (1000 / 100))
4331                                / arsta->last_tx_bitrate;
4332                /* overhead for media access time and IFS */
4333                airtime += IEEE80211_ATF_OVERHEAD_IFS;
4334        } else {
4335                /* This is mostly for throttle excessive BC/MC frames, and the
4336                 * airtime/rate doesn't need be exact. Airtime of BC/MC frames
4337                 * in 2G get some discount, which helps prevent very low rate
4338                 * frames from being blocked for too long.
4339                 */
4340                airtime = (pktlen * 8 * (1000 / 100)) / 60; /* 6M */
4341                airtime += IEEE80211_ATF_OVERHEAD;
4342        }
4343        spin_unlock_bh(&ar->data_lock);
4344
4345        return airtime;
4346}
4347
4348int ath10k_mac_tx_push_txq(struct ieee80211_hw *hw,
4349                           struct ieee80211_txq *txq)
4350{
4351        struct ath10k *ar = hw->priv;
4352        struct ath10k_htt *htt = &ar->htt;
4353        struct ath10k_txq *artxq = (void *)txq->drv_priv;
4354        struct ieee80211_vif *vif = txq->vif;
4355        struct ieee80211_sta *sta = txq->sta;
4356        enum ath10k_hw_txrx_mode txmode;
4357        enum ath10k_mac_tx_path txpath;
4358        struct sk_buff *skb;
4359        struct ieee80211_hdr *hdr;
4360        size_t skb_len;
4361        bool is_mgmt, is_presp;
4362        int ret;
4363        u16 airtime;
4364
4365        spin_lock_bh(&ar->htt.tx_lock);
4366        ret = ath10k_htt_tx_inc_pending(htt);
4367        spin_unlock_bh(&ar->htt.tx_lock);
4368
4369        if (ret)
4370                return ret;
4371
4372        skb = ieee80211_tx_dequeue_ni(hw, txq);
4373        if (!skb) {
4374                spin_lock_bh(&ar->htt.tx_lock);
4375                ath10k_htt_tx_dec_pending(htt);
4376                spin_unlock_bh(&ar->htt.tx_lock);
4377
4378                return -ENOENT;
4379        }
4380
4381        airtime = ath10k_mac_update_airtime(ar, txq, skb);
4382        ath10k_mac_tx_h_fill_cb(ar, vif, txq, sta, skb, airtime);
4383
4384        skb_len = skb->len;
4385        txmode = ath10k_mac_tx_h_get_txmode(ar, vif, sta, skb);
4386        txpath = ath10k_mac_tx_h_get_txpath(ar, skb, txmode);
4387        is_mgmt = (txpath == ATH10K_MAC_TX_HTT_MGMT);
4388
4389        if (is_mgmt) {
4390                hdr = (struct ieee80211_hdr *)skb->data;
4391                is_presp = ieee80211_is_probe_resp(hdr->frame_control);
4392
4393                spin_lock_bh(&ar->htt.tx_lock);
4394                ret = ath10k_htt_tx_mgmt_inc_pending(htt, is_mgmt, is_presp);
4395
4396                if (ret) {
4397                        ath10k_htt_tx_dec_pending(htt);
4398                        spin_unlock_bh(&ar->htt.tx_lock);
4399                        return ret;
4400                }
4401                spin_unlock_bh(&ar->htt.tx_lock);
4402        }
4403
4404        ret = ath10k_mac_tx(ar, vif, txmode, txpath, skb, false);
4405        if (unlikely(ret)) {
4406                ath10k_warn(ar, "failed to push frame: %d\n", ret);
4407
4408                spin_lock_bh(&ar->htt.tx_lock);
4409                ath10k_htt_tx_dec_pending(htt);
4410                if (is_mgmt)
4411                        ath10k_htt_tx_mgmt_dec_pending(htt);
4412                spin_unlock_bh(&ar->htt.tx_lock);
4413
4414                return ret;
4415        }
4416
4417        spin_lock_bh(&ar->htt.tx_lock);
4418        artxq->num_fw_queued++;
4419        spin_unlock_bh(&ar->htt.tx_lock);
4420
4421        return skb_len;
4422}
4423
4424static int ath10k_mac_schedule_txq(struct ieee80211_hw *hw, u32 ac)
4425{
4426        struct ieee80211_txq *txq;
4427        int ret = 0;
4428
4429        ieee80211_txq_schedule_start(hw, ac);
4430        while ((txq = ieee80211_next_txq(hw, ac))) {
4431                while (ath10k_mac_tx_can_push(hw, txq)) {
4432                        ret = ath10k_mac_tx_push_txq(hw, txq);
4433                        if (ret < 0)
4434                                break;
4435                }
4436                ieee80211_return_txq(hw, txq, false);
4437                ath10k_htt_tx_txq_update(hw, txq);
4438                if (ret == -EBUSY)
4439                        break;
4440        }
4441        ieee80211_txq_schedule_end(hw, ac);
4442
4443        return ret;
4444}
4445
4446void ath10k_mac_tx_push_pending(struct ath10k *ar)
4447{
4448        struct ieee80211_hw *hw = ar->hw;
4449        u32 ac;
4450
4451        if (ar->htt.tx_q_state.mode != HTT_TX_MODE_SWITCH_PUSH)
4452                return;
4453
4454        if (ar->htt.num_pending_tx >= (ar->htt.max_num_pending_tx / 2))
4455                return;
4456
4457        rcu_read_lock();
4458        for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
4459                if (ath10k_mac_schedule_txq(hw, ac) == -EBUSY)
4460                        break;
4461        }
4462        rcu_read_unlock();
4463}
4464EXPORT_SYMBOL(ath10k_mac_tx_push_pending);
4465
4466/************/
4467/* Scanning */
4468/************/
4469
4470void __ath10k_scan_finish(struct ath10k *ar)
4471{
4472        lockdep_assert_held(&ar->data_lock);
4473
4474        switch (ar->scan.state) {
4475        case ATH10K_SCAN_IDLE:
4476                break;
4477        case ATH10K_SCAN_RUNNING:
4478        case ATH10K_SCAN_ABORTING:
4479                if (!ar->scan.is_roc) {
4480                        struct cfg80211_scan_info info = {
4481                                .aborted = (ar->scan.state ==
4482                                            ATH10K_SCAN_ABORTING),
4483                        };
4484
4485                        ieee80211_scan_completed(ar->hw, &info);
4486                } else if (ar->scan.roc_notify) {
4487                        ieee80211_remain_on_channel_expired(ar->hw);
4488                }
4489                fallthrough;
4490        case ATH10K_SCAN_STARTING:
4491                ar->scan.state = ATH10K_SCAN_IDLE;
4492                ar->scan_channel = NULL;
4493                ar->scan.roc_freq = 0;
4494                ath10k_offchan_tx_purge(ar);
4495                cancel_delayed_work(&ar->scan.timeout);
4496                complete(&ar->scan.completed);
4497                break;
4498        }
4499}
4500
4501void ath10k_scan_finish(struct ath10k *ar)
4502{
4503        spin_lock_bh(&ar->data_lock);
4504        __ath10k_scan_finish(ar);
4505        spin_unlock_bh(&ar->data_lock);
4506}
4507
4508static int ath10k_scan_stop(struct ath10k *ar)
4509{
4510        struct wmi_stop_scan_arg arg = {
4511                .req_id = 1, /* FIXME */
4512                .req_type = WMI_SCAN_STOP_ONE,
4513                .u.scan_id = ATH10K_SCAN_ID,
4514        };
4515        int ret;
4516
4517        lockdep_assert_held(&ar->conf_mutex);
4518
4519        ret = ath10k_wmi_stop_scan(ar, &arg);
4520        if (ret) {
4521                ath10k_warn(ar, "failed to stop wmi scan: %d\n", ret);
4522                goto out;
4523        }
4524
4525        ret = wait_for_completion_timeout(&ar->scan.completed, 3 * HZ);
4526        if (ret == 0) {
4527                ath10k_warn(ar, "failed to receive scan abortion completion: timed out\n");
4528                ret = -ETIMEDOUT;
4529        } else if (ret > 0) {
4530                ret = 0;
4531        }
4532
4533out:
4534        /* Scan state should be updated upon scan completion but in case
4535         * firmware fails to deliver the event (for whatever reason) it is
4536         * desired to clean up scan state anyway. Firmware may have just
4537         * dropped the scan completion event delivery due to transport pipe
4538         * being overflown with data and/or it can recover on its own before
4539         * next scan request is submitted.
4540         */
4541        spin_lock_bh(&ar->data_lock);
4542        if (ar->scan.state != ATH10K_SCAN_IDLE)
4543                __ath10k_scan_finish(ar);
4544        spin_unlock_bh(&ar->data_lock);
4545
4546        return ret;
4547}
4548
4549static void ath10k_scan_abort(struct ath10k *ar)
4550{
4551        int ret;
4552
4553        lockdep_assert_held(&ar->conf_mutex);
4554
4555        spin_lock_bh(&ar->data_lock);
4556
4557        switch (ar->scan.state) {
4558        case ATH10K_SCAN_IDLE:
4559                /* This can happen if timeout worker kicked in and called
4560                 * abortion while scan completion was being processed.
4561                 */
4562                break;
4563        case ATH10K_SCAN_STARTING:
4564        case ATH10K_SCAN_ABORTING:
4565                ath10k_warn(ar, "refusing scan abortion due to invalid scan state: %s (%d)\n",
4566                            ath10k_scan_state_str(ar->scan.state),
4567                            ar->scan.state);
4568                break;
4569        case ATH10K_SCAN_RUNNING:
4570                ar->scan.state = ATH10K_SCAN_ABORTING;
4571                spin_unlock_bh(&ar->data_lock);
4572
4573                ret = ath10k_scan_stop(ar);
4574                if (ret)
4575                        ath10k_warn(ar, "failed to abort scan: %d\n", ret);
4576
4577                spin_lock_bh(&ar->data_lock);
4578                break;
4579        }
4580
4581        spin_unlock_bh(&ar->data_lock);
4582}
4583
4584void ath10k_scan_timeout_work(struct work_struct *work)
4585{
4586        struct ath10k *ar = container_of(work, struct ath10k,
4587                                         scan.timeout.work);
4588
4589        mutex_lock(&ar->conf_mutex);
4590        ath10k_scan_abort(ar);
4591        mutex_unlock(&ar->conf_mutex);
4592}
4593
4594static int ath10k_start_scan(struct ath10k *ar,
4595                             const struct wmi_start_scan_arg *arg)
4596{
4597        int ret;
4598
4599        lockdep_assert_held(&ar->conf_mutex);
4600
4601        ret = ath10k_wmi_start_scan(ar, arg);
4602        if (ret)
4603                return ret;
4604
4605        ret = wait_for_completion_timeout(&ar->scan.started, 1 * HZ);
4606        if (ret == 0) {
4607                ret = ath10k_scan_stop(ar);
4608                if (ret)
4609                        ath10k_warn(ar, "failed to stop scan: %d\n", ret);
4610
4611                return -ETIMEDOUT;
4612        }
4613
4614        /* If we failed to start the scan, return error code at
4615         * this point.  This is probably due to some issue in the
4616         * firmware, but no need to wedge the driver due to that...
4617         */
4618        spin_lock_bh(&ar->data_lock);
4619        if (ar->scan.state == ATH10K_SCAN_IDLE) {
4620                spin_unlock_bh(&ar->data_lock);
4621                return -EINVAL;
4622        }
4623        spin_unlock_bh(&ar->data_lock);
4624
4625        return 0;
4626}
4627
4628/**********************/
4629/* mac80211 callbacks */
4630/**********************/
4631
4632static void ath10k_mac_op_tx(struct ieee80211_hw *hw,
4633                             struct ieee80211_tx_control *control,
4634                             struct sk_buff *skb)
4635{
4636        struct ath10k *ar = hw->priv;
4637        struct ath10k_htt *htt = &ar->htt;
4638        struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
4639        struct ieee80211_vif *vif = info->control.vif;
4640        struct ieee80211_sta *sta = control->sta;
4641        struct ieee80211_txq *txq = NULL;
4642        struct ieee80211_hdr *hdr = (void *)skb->data;
4643        enum ath10k_hw_txrx_mode txmode;
4644        enum ath10k_mac_tx_path txpath;
4645        bool is_htt;
4646        bool is_mgmt;
4647        bool is_presp;
4648        int ret;
4649        u16 airtime;
4650
4651        airtime = ath10k_mac_update_airtime(ar, txq, skb);
4652        ath10k_mac_tx_h_fill_cb(ar, vif, txq, sta, skb, airtime);
4653
4654        txmode = ath10k_mac_tx_h_get_txmode(ar, vif, sta, skb);
4655        txpath = ath10k_mac_tx_h_get_txpath(ar, skb, txmode);
4656        is_htt = (txpath == ATH10K_MAC_TX_HTT ||
4657                  txpath == ATH10K_MAC_TX_HTT_MGMT);
4658        is_mgmt = (txpath == ATH10K_MAC_TX_HTT_MGMT);
4659
4660        if (is_htt) {
4661                spin_lock_bh(&ar->htt.tx_lock);
4662                is_presp = ieee80211_is_probe_resp(hdr->frame_control);
4663
4664                ret = ath10k_htt_tx_inc_pending(htt);
4665                if (ret) {
4666                        ath10k_warn(ar, "failed to increase tx pending count: %d, dropping\n",
4667                                    ret);
4668                        spin_unlock_bh(&ar->htt.tx_lock);
4669                        ieee80211_free_txskb(ar->hw, skb);
4670                        return;
4671                }
4672
4673                ret = ath10k_htt_tx_mgmt_inc_pending(htt, is_mgmt, is_presp);
4674                if (ret) {
4675                        ath10k_dbg(ar, ATH10K_DBG_MAC, "failed to increase tx mgmt pending count: %d, dropping\n",
4676                                   ret);
4677                        ath10k_htt_tx_dec_pending(htt);
4678                        spin_unlock_bh(&ar->htt.tx_lock);
4679                        ieee80211_free_txskb(ar->hw, skb);
4680                        return;
4681                }
4682                spin_unlock_bh(&ar->htt.tx_lock);
4683        }
4684
4685        ret = ath10k_mac_tx(ar, vif, txmode, txpath, skb, false);
4686        if (ret) {
4687                ath10k_warn(ar, "failed to transmit frame: %d\n", ret);
4688                if (is_htt) {
4689                        spin_lock_bh(&ar->htt.tx_lock);
4690                        ath10k_htt_tx_dec_pending(htt);
4691                        if (is_mgmt)
4692                                ath10k_htt_tx_mgmt_dec_pending(htt);
4693                        spin_unlock_bh(&ar->htt.tx_lock);
4694                }
4695                return;
4696        }
4697}
4698
4699static void ath10k_mac_op_wake_tx_queue(struct ieee80211_hw *hw,
4700                                        struct ieee80211_txq *txq)
4701{
4702        struct ath10k *ar = hw->priv;
4703        int ret;
4704        u8 ac;
4705
4706        ath10k_htt_tx_txq_update(hw, txq);
4707        if (ar->htt.tx_q_state.mode != HTT_TX_MODE_SWITCH_PUSH)
4708                return;
4709
4710        ac = txq->ac;
4711        ieee80211_txq_schedule_start(hw, ac);
4712        txq = ieee80211_next_txq(hw, ac);
4713        if (!txq)
4714                goto out;
4715
4716        while (ath10k_mac_tx_can_push(hw, txq)) {
4717                ret = ath10k_mac_tx_push_txq(hw, txq);
4718                if (ret < 0)
4719                        break;
4720        }
4721        ieee80211_return_txq(hw, txq, false);
4722        ath10k_htt_tx_txq_update(hw, txq);
4723out:
4724        ieee80211_txq_schedule_end(hw, ac);
4725}
4726
4727/* Must not be called with conf_mutex held as workers can use that also. */
4728void ath10k_drain_tx(struct ath10k *ar)
4729{
4730        lockdep_assert_not_held(&ar->conf_mutex);
4731
4732        /* make sure rcu-protected mac80211 tx path itself is drained */
4733        synchronize_net();
4734
4735        ath10k_offchan_tx_purge(ar);
4736        ath10k_mgmt_over_wmi_tx_purge(ar);
4737
4738        cancel_work_sync(&ar->offchan_tx_work);
4739        cancel_work_sync(&ar->wmi_mgmt_tx_work);
4740}
4741
4742void ath10k_halt(struct ath10k *ar)
4743{
4744        struct ath10k_vif *arvif;
4745
4746        lockdep_assert_held(&ar->conf_mutex);
4747
4748        clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
4749        ar->filter_flags = 0;
4750        ar->monitor = false;
4751        ar->monitor_arvif = NULL;
4752
4753        if (ar->monitor_started)
4754                ath10k_monitor_stop(ar);
4755
4756        ar->monitor_started = false;
4757        ar->tx_paused = 0;
4758
4759        ath10k_scan_finish(ar);
4760        ath10k_peer_cleanup_all(ar);
4761        ath10k_stop_radar_confirmation(ar);
4762        ath10k_core_stop(ar);
4763        ath10k_hif_power_down(ar);
4764
4765        spin_lock_bh(&ar->data_lock);
4766        list_for_each_entry(arvif, &ar->arvifs, list)
4767                ath10k_mac_vif_beacon_cleanup(arvif);
4768        spin_unlock_bh(&ar->data_lock);
4769}
4770
4771static int ath10k_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
4772{
4773        struct ath10k *ar = hw->priv;
4774
4775        mutex_lock(&ar->conf_mutex);
4776
4777        *tx_ant = ar->cfg_tx_chainmask;
4778        *rx_ant = ar->cfg_rx_chainmask;
4779
4780        mutex_unlock(&ar->conf_mutex);
4781
4782        return 0;
4783}
4784
4785static bool ath10k_check_chain_mask(struct ath10k *ar, u32 cm, const char *dbg)
4786{
4787        /* It is not clear that allowing gaps in chainmask
4788         * is helpful.  Probably it will not do what user
4789         * is hoping for, so warn in that case.
4790         */
4791        if (cm == 15 || cm == 7 || cm == 3 || cm == 1 || cm == 0)
4792                return true;
4793
4794        ath10k_warn(ar, "mac %s antenna chainmask is invalid: 0x%x.  Suggested values: 15, 7, 3, 1 or 0.\n",
4795                    dbg, cm);
4796        return false;
4797}
4798
4799static int ath10k_mac_get_vht_cap_bf_sts(struct ath10k *ar)
4800{
4801        int nsts = ar->vht_cap_info;
4802
4803        nsts &= IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
4804        nsts >>= IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT;
4805
4806        /* If firmware does not deliver to host number of space-time
4807         * streams supported, assume it support up to 4 BF STS and return
4808         * the value for VHT CAP: nsts-1)
4809         */
4810        if (nsts == 0)
4811                return 3;
4812
4813        return nsts;
4814}
4815
4816static int ath10k_mac_get_vht_cap_bf_sound_dim(struct ath10k *ar)
4817{
4818        int sound_dim = ar->vht_cap_info;
4819
4820        sound_dim &= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
4821        sound_dim >>= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
4822
4823        /* If the sounding dimension is not advertised by the firmware,
4824         * let's use a default value of 1
4825         */
4826        if (sound_dim == 0)
4827                return 1;
4828
4829        return sound_dim;
4830}
4831
4832static struct ieee80211_sta_vht_cap ath10k_create_vht_cap(struct ath10k *ar)
4833{
4834        struct ieee80211_sta_vht_cap vht_cap = {0};
4835        struct ath10k_hw_params *hw = &ar->hw_params;
4836        u16 mcs_map;
4837        u32 val;
4838        int i;
4839
4840        vht_cap.vht_supported = 1;
4841        vht_cap.cap = ar->vht_cap_info;
4842
4843        if (ar->vht_cap_info & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
4844                                IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE)) {
4845                val = ath10k_mac_get_vht_cap_bf_sts(ar);
4846                val <<= IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT;
4847                val &= IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
4848
4849                vht_cap.cap |= val;
4850        }
4851
4852        if (ar->vht_cap_info & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
4853                                IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)) {
4854                val = ath10k_mac_get_vht_cap_bf_sound_dim(ar);
4855                val <<= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
4856                val &= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
4857
4858                vht_cap.cap |= val;
4859        }
4860
4861        mcs_map = 0;
4862        for (i = 0; i < 8; i++) {
4863                if ((i < ar->num_rf_chains) && (ar->cfg_tx_chainmask & BIT(i)))
4864                        mcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
4865                else
4866                        mcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
4867        }
4868
4869        if (ar->cfg_tx_chainmask <= 1)
4870                vht_cap.cap &= ~IEEE80211_VHT_CAP_TXSTBC;
4871
4872        vht_cap.vht_mcs.rx_mcs_map = cpu_to_le16(mcs_map);
4873        vht_cap.vht_mcs.tx_mcs_map = cpu_to_le16(mcs_map);
4874
4875        /* If we are supporting 160Mhz or 80+80, then the NIC may be able to do
4876         * a restricted NSS for 160 or 80+80 vs what it can do for 80Mhz.  Give
4877         * user-space a clue if that is the case.
4878         */
4879        if ((vht_cap.cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK) &&
4880            (hw->vht160_mcs_rx_highest != 0 ||
4881             hw->vht160_mcs_tx_highest != 0)) {
4882                vht_cap.vht_mcs.rx_highest = cpu_to_le16(hw->vht160_mcs_rx_highest);
4883                vht_cap.vht_mcs.tx_highest = cpu_to_le16(hw->vht160_mcs_tx_highest);
4884        }
4885
4886        return vht_cap;
4887}
4888
4889static struct ieee80211_sta_ht_cap ath10k_get_ht_cap(struct ath10k *ar)
4890{
4891        int i;
4892        struct ieee80211_sta_ht_cap ht_cap = {0};
4893
4894        if (!(ar->ht_cap_info & WMI_HT_CAP_ENABLED))
4895                return ht_cap;
4896
4897        ht_cap.ht_supported = 1;
4898        ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
4899        ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_8;
4900        ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
4901        ht_cap.cap |= IEEE80211_HT_CAP_DSSSCCK40;
4902        ht_cap.cap |=
4903                WLAN_HT_CAP_SM_PS_DISABLED << IEEE80211_HT_CAP_SM_PS_SHIFT;
4904
4905        if (ar->ht_cap_info & WMI_HT_CAP_HT20_SGI)
4906                ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
4907
4908        if (ar->ht_cap_info & WMI_HT_CAP_HT40_SGI)
4909                ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
4910
4911        if (ar->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS) {
4912                u32 smps;
4913
4914                smps   = WLAN_HT_CAP_SM_PS_DYNAMIC;
4915                smps <<= IEEE80211_HT_CAP_SM_PS_SHIFT;
4916
4917                ht_cap.cap |= smps;
4918        }
4919
4920        if (ar->ht_cap_info & WMI_HT_CAP_TX_STBC && (ar->cfg_tx_chainmask > 1))
4921                ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
4922
4923        if (ar->ht_cap_info & WMI_HT_CAP_RX_STBC) {
4924                u32 stbc;
4925
4926                stbc   = ar->ht_cap_info;
4927                stbc  &= WMI_HT_CAP_RX_STBC;
4928                stbc >>= WMI_HT_CAP_RX_STBC_MASK_SHIFT;
4929                stbc <<= IEEE80211_HT_CAP_RX_STBC_SHIFT;
4930                stbc  &= IEEE80211_HT_CAP_RX_STBC;
4931
4932                ht_cap.cap |= stbc;
4933        }
4934
4935        if (ar->ht_cap_info & WMI_HT_CAP_LDPC || (ar->ht_cap_info &
4936            WMI_HT_CAP_RX_LDPC && (ar->ht_cap_info & WMI_HT_CAP_TX_LDPC)))
4937                ht_cap.cap |= IEEE80211_HT_CAP_LDPC_CODING;
4938
4939        if (ar->ht_cap_info & WMI_HT_CAP_L_SIG_TXOP_PROT)
4940                ht_cap.cap |= IEEE80211_HT_CAP_LSIG_TXOP_PROT;
4941
4942        /* max AMSDU is implicitly taken from vht_cap_info */
4943        if (ar->vht_cap_info & WMI_VHT_CAP_MAX_MPDU_LEN_MASK)
4944                ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
4945
4946        for (i = 0; i < ar->num_rf_chains; i++) {
4947                if (ar->cfg_rx_chainmask & BIT(i))
4948                        ht_cap.mcs.rx_mask[i] = 0xFF;
4949        }
4950
4951        ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
4952
4953        return ht_cap;
4954}
4955
4956static void ath10k_mac_setup_ht_vht_cap(struct ath10k *ar)
4957{
4958        struct ieee80211_supported_band *band;
4959        struct ieee80211_sta_vht_cap vht_cap;
4960        struct ieee80211_sta_ht_cap ht_cap;
4961
4962        ht_cap = ath10k_get_ht_cap(ar);
4963        vht_cap = ath10k_create_vht_cap(ar);
4964
4965        if (ar->phy_capability & WHAL_WLAN_11G_CAPABILITY) {
4966                band = &ar->mac.sbands[NL80211_BAND_2GHZ];
4967                band->ht_cap = ht_cap;
4968        }
4969        if (ar->phy_capability & WHAL_WLAN_11A_CAPABILITY) {
4970                band = &ar->mac.sbands[NL80211_BAND_5GHZ];
4971                band->ht_cap = ht_cap;
4972                band->vht_cap = vht_cap;
4973        }
4974}
4975
4976static int __ath10k_set_antenna(struct ath10k *ar, u32 tx_ant, u32 rx_ant)
4977{
4978        int ret;
4979        bool is_valid_tx_chain_mask, is_valid_rx_chain_mask;
4980
4981        lockdep_assert_held(&ar->conf_mutex);
4982
4983        is_valid_tx_chain_mask = ath10k_check_chain_mask(ar, tx_ant, "tx");
4984        is_valid_rx_chain_mask = ath10k_check_chain_mask(ar, rx_ant, "rx");
4985
4986        if (!is_valid_tx_chain_mask || !is_valid_rx_chain_mask)
4987                return -EINVAL;
4988
4989        ar->cfg_tx_chainmask = tx_ant;
4990        ar->cfg_rx_chainmask = rx_ant;
4991
4992        if ((ar->state != ATH10K_STATE_ON) &&
4993            (ar->state != ATH10K_STATE_RESTARTED))
4994                return 0;
4995
4996        ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->tx_chain_mask,
4997                                        tx_ant);
4998        if (ret) {
4999                ath10k_warn(ar, "failed to set tx-chainmask: %d, req 0x%x\n",
5000                            ret, tx_ant);
5001                return ret;
5002        }
5003
5004        ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->rx_chain_mask,
5005                                        rx_ant);
5006        if (ret) {
5007                ath10k_warn(ar, "failed to set rx-chainmask: %d, req 0x%x\n",
5008                            ret, rx_ant);
5009                return ret;
5010        }
5011
5012        /* Reload HT/VHT capability */
5013        ath10k_mac_setup_ht_vht_cap(ar);
5014
5015        return 0;
5016}
5017
5018static int ath10k_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
5019{
5020        struct ath10k *ar = hw->priv;
5021        int ret;
5022
5023        mutex_lock(&ar->conf_mutex);
5024        ret = __ath10k_set_antenna(ar, tx_ant, rx_ant);
5025        mutex_unlock(&ar->conf_mutex);
5026        return ret;
5027}
5028
5029static int __ath10k_fetch_bb_timing_dt(struct ath10k *ar,
5030                                       struct wmi_bb_timing_cfg_arg *bb_timing)
5031{
5032        struct device_node *node;
5033        const char *fem_name;
5034        int ret;
5035
5036        node = ar->dev->of_node;
5037        if (!node)
5038                return -ENOENT;
5039
5040        ret = of_property_read_string_index(node, "ext-fem-name", 0, &fem_name);
5041        if (ret)
5042                return -ENOENT;
5043
5044        /*
5045         * If external Front End module used in hardware, then default base band timing
5046         * parameter cannot be used since they were fine tuned for reference hardware,
5047         * so choosing different value suitable for that external FEM.
5048         */
5049        if (!strcmp("microsemi-lx5586", fem_name)) {
5050                bb_timing->bb_tx_timing = 0x00;
5051                bb_timing->bb_xpa_timing = 0x0101;
5052        } else {
5053                return -ENOENT;
5054        }
5055
5056        ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot bb_tx_timing 0x%x bb_xpa_timing 0x%x\n",
5057                   bb_timing->bb_tx_timing, bb_timing->bb_xpa_timing);
5058        return 0;
5059}
5060
5061static int ath10k_mac_rfkill_config(struct ath10k *ar)
5062{
5063        u32 param;
5064        int ret;
5065
5066        if (ar->hw_values->rfkill_pin == 0) {
5067                ath10k_warn(ar, "ath10k does not support hardware rfkill with this device\n");
5068                return -EOPNOTSUPP;
5069        }
5070
5071        ath10k_dbg(ar, ATH10K_DBG_MAC,
5072                   "mac rfkill_pin %d rfkill_cfg %d rfkill_on_level %d",
5073                   ar->hw_values->rfkill_pin, ar->hw_values->rfkill_cfg,
5074                   ar->hw_values->rfkill_on_level);
5075
5076        param = FIELD_PREP(WMI_TLV_RFKILL_CFG_RADIO_LEVEL,
5077                           ar->hw_values->rfkill_on_level) |
5078                FIELD_PREP(WMI_TLV_RFKILL_CFG_GPIO_PIN_NUM,
5079                           ar->hw_values->rfkill_pin) |
5080                FIELD_PREP(WMI_TLV_RFKILL_CFG_PIN_AS_GPIO,
5081                           ar->hw_values->rfkill_cfg);
5082
5083        ret = ath10k_wmi_pdev_set_param(ar,
5084                                        ar->wmi.pdev_param->rfkill_config,
5085                                        param);
5086        if (ret) {
5087                ath10k_warn(ar,
5088                            "failed to set rfkill config 0x%x: %d\n",
5089                            param, ret);
5090                return ret;
5091        }
5092        return 0;
5093}
5094
5095int ath10k_mac_rfkill_enable_radio(struct ath10k *ar, bool enable)
5096{
5097        enum wmi_tlv_rfkill_enable_radio param;
5098        int ret;
5099
5100        if (enable)
5101                param = WMI_TLV_RFKILL_ENABLE_RADIO_ON;
5102        else
5103                param = WMI_TLV_RFKILL_ENABLE_RADIO_OFF;
5104
5105        ath10k_dbg(ar, ATH10K_DBG_MAC, "mac rfkill enable %d", param);
5106
5107        ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->rfkill_enable,
5108                                        param);
5109        if (ret) {
5110                ath10k_warn(ar, "failed to set rfkill enable param %d: %d\n",
5111                            param, ret);
5112                return ret;
5113        }
5114
5115        return 0;
5116}
5117
5118static int ath10k_start(struct ieee80211_hw *hw)
5119{
5120        struct ath10k *ar = hw->priv;
5121        u32 param;
5122        int ret = 0;
5123        struct wmi_bb_timing_cfg_arg bb_timing = {0};
5124
5125        /*
5126         * This makes sense only when restarting hw. It is harmless to call
5127         * unconditionally. This is necessary to make sure no HTT/WMI tx
5128         * commands will be submitted while restarting.
5129         */
5130        ath10k_drain_tx(ar);
5131
5132        mutex_lock(&ar->conf_mutex);
5133
5134        switch (ar->state) {
5135        case ATH10K_STATE_OFF:
5136                ar->state = ATH10K_STATE_ON;
5137                break;
5138        case ATH10K_STATE_RESTARTING:
5139                ar->state = ATH10K_STATE_RESTARTED;
5140                break;
5141        case ATH10K_STATE_ON:
5142        case ATH10K_STATE_RESTARTED:
5143        case ATH10K_STATE_WEDGED:
5144                WARN_ON(1);
5145                ret = -EINVAL;
5146                goto err;
5147        case ATH10K_STATE_UTF:
5148                ret = -EBUSY;
5149                goto err;
5150        }
5151
5152        spin_lock_bh(&ar->data_lock);
5153
5154        if (ar->hw_rfkill_on) {
5155                ar->hw_rfkill_on = false;
5156                spin_unlock_bh(&ar->data_lock);
5157                goto err;
5158        }
5159
5160        spin_unlock_bh(&ar->data_lock);
5161
5162        ret = ath10k_hif_power_up(ar, ATH10K_FIRMWARE_MODE_NORMAL);
5163        if (ret) {
5164                ath10k_err(ar, "Could not init hif: %d\n", ret);
5165                goto err_off;
5166        }
5167
5168        ret = ath10k_core_start(ar, ATH10K_FIRMWARE_MODE_NORMAL,
5169                                &ar->normal_mode_fw);
5170        if (ret) {
5171                ath10k_err(ar, "Could not init core: %d\n", ret);
5172                goto err_power_down;
5173        }
5174
5175        if (ar->sys_cap_info & WMI_TLV_SYS_CAP_INFO_RFKILL) {
5176                ret = ath10k_mac_rfkill_config(ar);
5177                if (ret && ret != -EOPNOTSUPP) {
5178                        ath10k_warn(ar, "failed to configure rfkill: %d", ret);
5179                        goto err_core_stop;
5180                }
5181        }
5182
5183        param = ar->wmi.pdev_param->pmf_qos;
5184        ret = ath10k_wmi_pdev_set_param(ar, param, 1);
5185        if (ret) {
5186                ath10k_warn(ar, "failed to enable PMF QOS: %d\n", ret);
5187                goto err_core_stop;
5188        }
5189
5190        param = ar->wmi.pdev_param->dynamic_bw;
5191        ret = ath10k_wmi_pdev_set_param(ar, param, 1);
5192        if (ret) {
5193                ath10k_warn(ar, "failed to enable dynamic BW: %d\n", ret);
5194                goto err_core_stop;
5195        }
5196
5197        if (test_bit(WMI_SERVICE_SPOOF_MAC_SUPPORT, ar->wmi.svc_map)) {
5198                ret = ath10k_wmi_scan_prob_req_oui(ar, ar->mac_addr);
5199                if (ret) {
5200                        ath10k_err(ar, "failed to set prob req oui: %i\n", ret);
5201                        goto err_core_stop;
5202                }
5203        }
5204
5205        if (test_bit(WMI_SERVICE_ADAPTIVE_OCS, ar->wmi.svc_map)) {
5206                ret = ath10k_wmi_adaptive_qcs(ar, true);
5207                if (ret) {
5208                        ath10k_warn(ar, "failed to enable adaptive qcs: %d\n",
5209                                    ret);
5210                        goto err_core_stop;
5211                }
5212        }
5213
5214        if (test_bit(WMI_SERVICE_BURST, ar->wmi.svc_map)) {
5215                param = ar->wmi.pdev_param->burst_enable;
5216                ret = ath10k_wmi_pdev_set_param(ar, param, 0);
5217                if (ret) {
5218                        ath10k_warn(ar, "failed to disable burst: %d\n", ret);
5219                        goto err_core_stop;
5220                }
5221        }
5222
5223        param = ar->wmi.pdev_param->idle_ps_config;
5224        ret = ath10k_wmi_pdev_set_param(ar, param, 1);
5225        if (ret && ret != -EOPNOTSUPP) {
5226                ath10k_warn(ar, "failed to enable idle_ps_config: %d\n", ret);
5227                goto err_core_stop;
5228        }
5229
5230        __ath10k_set_antenna(ar, ar->cfg_tx_chainmask, ar->cfg_rx_chainmask);
5231
5232        /*
5233         * By default FW set ARP frames ac to voice (6). In that case ARP
5234         * exchange is not working properly for UAPSD enabled AP. ARP requests
5235         * which arrives with access category 0 are processed by network stack
5236         * and send back with access category 0, but FW changes access category
5237         * to 6. Set ARP frames access category to best effort (0) solves
5238         * this problem.
5239         */
5240
5241        param = ar->wmi.pdev_param->arp_ac_override;
5242        ret = ath10k_wmi_pdev_set_param(ar, param, 0);
5243        if (ret) {
5244                ath10k_warn(ar, "failed to set arp ac override parameter: %d\n",
5245                            ret);
5246                goto err_core_stop;
5247        }
5248
5249        if (test_bit(ATH10K_FW_FEATURE_SUPPORTS_ADAPTIVE_CCA,
5250                     ar->running_fw->fw_file.fw_features)) {
5251                ret = ath10k_wmi_pdev_enable_adaptive_cca(ar, 1,
5252                                                          WMI_CCA_DETECT_LEVEL_AUTO,
5253                                                          WMI_CCA_DETECT_MARGIN_AUTO);
5254                if (ret) {
5255                        ath10k_warn(ar, "failed to enable adaptive cca: %d\n",
5256                                    ret);
5257                        goto err_core_stop;
5258                }
5259        }
5260
5261        param = ar->wmi.pdev_param->ani_enable;
5262        ret = ath10k_wmi_pdev_set_param(ar, param, 1);
5263        if (ret) {
5264                ath10k_warn(ar, "failed to enable ani by default: %d\n",
5265                            ret);
5266                goto err_core_stop;
5267        }
5268
5269        ar->ani_enabled = true;
5270
5271        if (ath10k_peer_stats_enabled(ar)) {
5272                param = ar->wmi.pdev_param->peer_stats_update_period;
5273                ret = ath10k_wmi_pdev_set_param(ar, param,
5274                                                PEER_DEFAULT_STATS_UPDATE_PERIOD);
5275                if (ret) {
5276                        ath10k_warn(ar,
5277                                    "failed to set peer stats period : %d\n",
5278                                    ret);
5279                        goto err_core_stop;
5280                }
5281        }
5282
5283        param = ar->wmi.pdev_param->enable_btcoex;
5284        if (test_bit(WMI_SERVICE_COEX_GPIO, ar->wmi.svc_map) &&
5285            test_bit(ATH10K_FW_FEATURE_BTCOEX_PARAM,
5286                     ar->running_fw->fw_file.fw_features) &&
5287            ar->coex_support) {
5288                ret = ath10k_wmi_pdev_set_param(ar, param, 0);
5289                if (ret) {
5290                        ath10k_warn(ar,
5291                                    "failed to set btcoex param: %d\n", ret);
5292                        goto err_core_stop;
5293                }
5294                clear_bit(ATH10K_FLAG_BTCOEX, &ar->dev_flags);
5295        }
5296
5297        if (test_bit(WMI_SERVICE_BB_TIMING_CONFIG_SUPPORT, ar->wmi.svc_map)) {
5298                ret = __ath10k_fetch_bb_timing_dt(ar, &bb_timing);
5299                if (!ret) {
5300                        ret = ath10k_wmi_pdev_bb_timing(ar, &bb_timing);
5301                        if (ret) {
5302                                ath10k_warn(ar,
5303                                            "failed to set bb timings: %d\n",
5304                                            ret);
5305                                goto err_core_stop;
5306                        }
5307                }
5308        }
5309
5310        ar->num_started_vdevs = 0;
5311        ath10k_regd_update(ar);
5312
5313        ath10k_spectral_start(ar);
5314        ath10k_thermal_set_throttling(ar);
5315
5316        ar->radar_conf_state = ATH10K_RADAR_CONFIRMATION_IDLE;
5317
5318        mutex_unlock(&ar->conf_mutex);
5319        return 0;
5320
5321err_core_stop:
5322        ath10k_core_stop(ar);
5323
5324err_power_down:
5325        ath10k_hif_power_down(ar);
5326
5327err_off:
5328        ar->state = ATH10K_STATE_OFF;
5329
5330err:
5331        mutex_unlock(&ar->conf_mutex);
5332        return ret;
5333}
5334
5335static void ath10k_stop(struct ieee80211_hw *hw)
5336{
5337        struct ath10k *ar = hw->priv;
5338
5339        ath10k_drain_tx(ar);
5340
5341        mutex_lock(&ar->conf_mutex);
5342        if (ar->state != ATH10K_STATE_OFF) {
5343                if (!ar->hw_rfkill_on)
5344                        ath10k_halt(ar);
5345                ar->state = ATH10K_STATE_OFF;
5346        }
5347        mutex_unlock(&ar->conf_mutex);
5348
5349        cancel_work_sync(&ar->set_coverage_class_work);
5350        cancel_delayed_work_sync(&ar->scan.timeout);
5351        cancel_work_sync(&ar->restart_work);
5352}
5353
5354static int ath10k_config_ps(struct ath10k *ar)
5355{
5356        struct ath10k_vif *arvif;
5357        int ret = 0;
5358
5359        lockdep_assert_held(&ar->conf_mutex);
5360
5361        list_for_each_entry(arvif, &ar->arvifs, list) {
5362                ret = ath10k_mac_vif_setup_ps(arvif);
5363                if (ret) {
5364                        ath10k_warn(ar, "failed to setup powersave: %d\n", ret);
5365                        break;
5366                }
5367        }
5368
5369        return ret;
5370}
5371
5372static int ath10k_config(struct ieee80211_hw *hw, u32 changed)
5373{
5374        struct ath10k *ar = hw->priv;
5375        struct ieee80211_conf *conf = &hw->conf;
5376        int ret = 0;
5377
5378        mutex_lock(&ar->conf_mutex);
5379
5380        if (changed & IEEE80211_CONF_CHANGE_PS)
5381                ath10k_config_ps(ar);
5382
5383        if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
5384                ar->monitor = conf->flags & IEEE80211_CONF_MONITOR;
5385                ret = ath10k_monitor_recalc(ar);
5386                if (ret)
5387                        ath10k_warn(ar, "failed to recalc monitor: %d\n", ret);
5388        }
5389
5390        mutex_unlock(&ar->conf_mutex);
5391        return ret;
5392}
5393
5394static u32 get_nss_from_chainmask(u16 chain_mask)
5395{
5396        if ((chain_mask & 0xf) == 0xf)
5397                return 4;
5398        else if ((chain_mask & 0x7) == 0x7)
5399                return 3;
5400        else if ((chain_mask & 0x3) == 0x3)
5401                return 2;
5402        return 1;
5403}
5404
5405static int ath10k_mac_set_txbf_conf(struct ath10k_vif *arvif)
5406{
5407        u32 value = 0;
5408        struct ath10k *ar = arvif->ar;
5409        int nsts;
5410        int sound_dim;
5411
5412        if (ath10k_wmi_get_txbf_conf_scheme(ar) != WMI_TXBF_CONF_BEFORE_ASSOC)
5413                return 0;
5414
5415        nsts = ath10k_mac_get_vht_cap_bf_sts(ar);
5416        if (ar->vht_cap_info & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
5417                                IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE))
5418                value |= SM(nsts, WMI_TXBF_STS_CAP_OFFSET);
5419
5420        sound_dim = ath10k_mac_get_vht_cap_bf_sound_dim(ar);
5421        if (ar->vht_cap_info & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
5422                                IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE))
5423                value |= SM(sound_dim, WMI_BF_SOUND_DIM_OFFSET);
5424
5425        if (!value)
5426                return 0;
5427
5428        if (ar->vht_cap_info & IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)
5429                value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFER;
5430
5431        if (ar->vht_cap_info & IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)
5432                value |= (WMI_VDEV_PARAM_TXBF_MU_TX_BFER |
5433                          WMI_VDEV_PARAM_TXBF_SU_TX_BFER);
5434
5435        if (ar->vht_cap_info & IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE)
5436                value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFEE;
5437
5438        if (ar->vht_cap_info & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE)
5439                value |= (WMI_VDEV_PARAM_TXBF_MU_TX_BFEE |
5440                          WMI_VDEV_PARAM_TXBF_SU_TX_BFEE);
5441
5442        return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
5443                                         ar->wmi.vdev_param->txbf, value);
5444}
5445
5446/*
5447 * TODO:
5448 * Figure out how to handle WMI_VDEV_SUBTYPE_P2P_DEVICE,
5449 * because we will send mgmt frames without CCK. This requirement
5450 * for P2P_FIND/GO_NEG should be handled by checking CCK flag
5451 * in the TX packet.
5452 */
5453static int ath10k_add_interface(struct ieee80211_hw *hw,
5454                                struct ieee80211_vif *vif)
5455{
5456        struct ath10k *ar = hw->priv;
5457        struct ath10k_vif *arvif = (void *)vif->drv_priv;
5458        struct ath10k_peer *peer;
5459        enum wmi_sta_powersave_param param;
5460        int ret = 0;
5461        u32 value;
5462        int bit;
5463        int i;
5464        u32 vdev_param;
5465
5466        vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
5467
5468        mutex_lock(&ar->conf_mutex);
5469
5470        memset(arvif, 0, sizeof(*arvif));
5471        ath10k_mac_txq_init(vif->txq);
5472
5473        arvif->ar = ar;
5474        arvif->vif = vif;
5475
5476        INIT_LIST_HEAD(&arvif->list);
5477        INIT_WORK(&arvif->ap_csa_work, ath10k_mac_vif_ap_csa_work);
5478        INIT_DELAYED_WORK(&arvif->connection_loss_work,
5479                          ath10k_mac_vif_sta_connection_loss_work);
5480
5481        for (i = 0; i < ARRAY_SIZE(arvif->bitrate_mask.control); i++) {
5482                arvif->bitrate_mask.control[i].legacy = 0xffffffff;
5483                memset(arvif->bitrate_mask.control[i].ht_mcs, 0xff,
5484                       sizeof(arvif->bitrate_mask.control[i].ht_mcs));
5485                memset(arvif->bitrate_mask.control[i].vht_mcs, 0xff,
5486                       sizeof(arvif->bitrate_mask.control[i].vht_mcs));
5487        }
5488
5489        if (ar->num_peers >= ar->max_num_peers) {
5490                ath10k_warn(ar, "refusing vdev creation due to insufficient peer entry resources in firmware\n");
5491                ret = -ENOBUFS;
5492                goto err;
5493        }
5494
5495        if (ar->free_vdev_map == 0) {
5496                ath10k_warn(ar, "Free vdev map is empty, no more interfaces allowed.\n");
5497                ret = -EBUSY;
5498                goto err;
5499        }
5500        bit = __ffs64(ar->free_vdev_map);
5501
5502        ath10k_dbg(ar, ATH10K_DBG_MAC, "mac create vdev %i map %llx\n",
5503                   bit, ar->free_vdev_map);
5504
5505        arvif->vdev_id = bit;
5506        arvif->vdev_subtype =
5507                ath10k_wmi_get_vdev_subtype(ar, WMI_VDEV_SUBTYPE_NONE);
5508
5509        switch (vif->type) {
5510        case NL80211_IFTYPE_P2P_DEVICE:
5511                arvif->vdev_type = WMI_VDEV_TYPE_STA;
5512                arvif->vdev_subtype = ath10k_wmi_get_vdev_subtype
5513                                        (ar, WMI_VDEV_SUBTYPE_P2P_DEVICE);
5514                break;
5515        case NL80211_IFTYPE_UNSPECIFIED:
5516        case NL80211_IFTYPE_STATION:
5517                arvif->vdev_type = WMI_VDEV_TYPE_STA;
5518                if (vif->p2p)
5519                        arvif->vdev_subtype = ath10k_wmi_get_vdev_subtype
5520                                        (ar, WMI_VDEV_SUBTYPE_P2P_CLIENT);
5521                break;
5522        case NL80211_IFTYPE_ADHOC:
5523                arvif->vdev_type = WMI_VDEV_TYPE_IBSS;
5524                break;
5525        case NL80211_IFTYPE_MESH_POINT:
5526                if (test_bit(WMI_SERVICE_MESH_11S, ar->wmi.svc_map)) {
5527                        arvif->vdev_subtype = ath10k_wmi_get_vdev_subtype
5528                                                (ar, WMI_VDEV_SUBTYPE_MESH_11S);
5529                } else if (!test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags)) {
5530                        ret = -EINVAL;
5531                        ath10k_warn(ar, "must load driver with rawmode=1 to add mesh interfaces\n");
5532                        goto err;
5533                }
5534                arvif->vdev_type = WMI_VDEV_TYPE_AP;
5535                break;
5536        case NL80211_IFTYPE_AP:
5537                arvif->vdev_type = WMI_VDEV_TYPE_AP;
5538
5539                if (vif->p2p)
5540                        arvif->vdev_subtype = ath10k_wmi_get_vdev_subtype
5541                                                (ar, WMI_VDEV_SUBTYPE_P2P_GO);
5542                break;
5543        case NL80211_IFTYPE_MONITOR:
5544                arvif->vdev_type = WMI_VDEV_TYPE_MONITOR;
5545                break;
5546        default:
5547                WARN_ON(1);
5548                break;
5549        }
5550
5551        /* Using vdev_id as queue number will make it very easy to do per-vif
5552         * tx queue locking. This shouldn't wrap due to interface combinations
5553         * but do a modulo for correctness sake and prevent using offchannel tx
5554         * queues for regular vif tx.
5555         */
5556        vif->cab_queue = arvif->vdev_id % (IEEE80211_MAX_QUEUES - 1);
5557        for (i = 0; i < ARRAY_SIZE(vif->hw_queue); i++)
5558                vif->hw_queue[i] = arvif->vdev_id % (IEEE80211_MAX_QUEUES - 1);
5559
5560        /* Some firmware revisions don't wait for beacon tx completion before
5561         * sending another SWBA event. This could lead to hardware using old
5562         * (freed) beacon data in some cases, e.g. tx credit starvation
5563         * combined with missed TBTT. This is very rare.
5564         *
5565         * On non-IOMMU-enabled hosts this could be a possible security issue
5566         * because hw could beacon some random data on the air.  On
5567         * IOMMU-enabled hosts DMAR faults would occur in most cases and target
5568         * device would crash.
5569         *
5570         * Since there are no beacon tx completions (implicit nor explicit)
5571         * propagated to host the only workaround for this is to allocate a
5572         * DMA-coherent buffer for a lifetime of a vif and use it for all
5573         * beacon tx commands. Worst case for this approach is some beacons may
5574         * become corrupted, e.g. have garbled IEs or out-of-date TIM bitmap.
5575         */
5576        if (vif->type == NL80211_IFTYPE_ADHOC ||
5577            vif->type == NL80211_IFTYPE_MESH_POINT ||
5578            vif->type == NL80211_IFTYPE_AP) {
5579                arvif->beacon_buf = dma_alloc_coherent(ar->dev,
5580                                                       IEEE80211_MAX_FRAME_LEN,
5581                                                       &arvif->beacon_paddr,
5582                                                       GFP_ATOMIC);
5583                if (!arvif->beacon_buf) {
5584                        ret = -ENOMEM;
5585                        ath10k_warn(ar, "failed to allocate beacon buffer: %d\n",
5586                                    ret);
5587                        goto err;
5588                }
5589        }
5590        if (test_bit(ATH10K_FLAG_HW_CRYPTO_DISABLED, &ar->dev_flags))
5591                arvif->nohwcrypt = true;
5592
5593        if (arvif->nohwcrypt &&
5594            !test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags)) {
5595                ret = -EINVAL;
5596                ath10k_warn(ar, "cryptmode module param needed for sw crypto\n");
5597                goto err;
5598        }
5599
5600        ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev create %d (add interface) type %d subtype %d bcnmode %s\n",
5601                   arvif->vdev_id, arvif->vdev_type, arvif->vdev_subtype,
5602                   arvif->beacon_buf ? "single-buf" : "per-skb");
5603
5604        ret = ath10k_wmi_vdev_create(ar, arvif->vdev_id, arvif->vdev_type,
5605                                     arvif->vdev_subtype, vif->addr);
5606        if (ret) {
5607                ath10k_warn(ar, "failed to create WMI vdev %i: %d\n",
5608                            arvif->vdev_id, ret);
5609                goto err;
5610        }
5611
5612        if (test_bit(WMI_SERVICE_VDEV_DISABLE_4_ADDR_SRC_LRN_SUPPORT,
5613                     ar->wmi.svc_map)) {
5614                vdev_param = ar->wmi.vdev_param->disable_4addr_src_lrn;
5615                ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
5616                                                WMI_VDEV_DISABLE_4_ADDR_SRC_LRN);
5617                if (ret && ret != -EOPNOTSUPP) {
5618                        ath10k_warn(ar, "failed to disable 4addr src lrn vdev %i: %d\n",
5619                                    arvif->vdev_id, ret);
5620                }
5621        }
5622
5623        ar->free_vdev_map &= ~(1LL << arvif->vdev_id);
5624        spin_lock_bh(&ar->data_lock);
5625        list_add(&arvif->list, &ar->arvifs);
5626        spin_unlock_bh(&ar->data_lock);
5627
5628        /* It makes no sense to have firmware do keepalives. mac80211 already
5629         * takes care of this with idle connection polling.
5630         */
5631        ret = ath10k_mac_vif_disable_keepalive(arvif);
5632        if (ret) {
5633                ath10k_warn(ar, "failed to disable keepalive on vdev %i: %d\n",
5634                            arvif->vdev_id, ret);
5635                goto err_vdev_delete;
5636        }
5637
5638        arvif->def_wep_key_idx = -1;
5639
5640        vdev_param = ar->wmi.vdev_param->tx_encap_type;
5641        ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
5642                                        ATH10K_HW_TXRX_NATIVE_WIFI);
5643        /* 10.X firmware does not support this VDEV parameter. Do not warn */
5644        if (ret && ret != -EOPNOTSUPP) {
5645                ath10k_warn(ar, "failed to set vdev %i TX encapsulation: %d\n",
5646                            arvif->vdev_id, ret);
5647                goto err_vdev_delete;
5648        }
5649
5650        /* Configuring number of spatial stream for monitor interface is causing
5651         * target assert in qca9888 and qca6174.
5652         */
5653        if (ar->cfg_tx_chainmask && (vif->type != NL80211_IFTYPE_MONITOR)) {
5654                u16 nss = get_nss_from_chainmask(ar->cfg_tx_chainmask);
5655
5656                vdev_param = ar->wmi.vdev_param->nss;
5657                ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
5658                                                nss);
5659                if (ret) {
5660                        ath10k_warn(ar, "failed to set vdev %i chainmask 0x%x, nss %i: %d\n",
5661                                    arvif->vdev_id, ar->cfg_tx_chainmask, nss,
5662                                    ret);
5663                        goto err_vdev_delete;
5664                }
5665        }
5666
5667        if (arvif->vdev_type == WMI_VDEV_TYPE_AP ||
5668            arvif->vdev_type == WMI_VDEV_TYPE_IBSS) {
5669                ret = ath10k_peer_create(ar, vif, NULL, arvif->vdev_id,
5670                                         vif->addr, WMI_PEER_TYPE_DEFAULT);
5671                if (ret) {
5672                        ath10k_warn(ar, "failed to create vdev %i peer for AP/IBSS: %d\n",
5673                                    arvif->vdev_id, ret);
5674                        goto err_vdev_delete;
5675                }
5676
5677                spin_lock_bh(&ar->data_lock);
5678
5679                peer = ath10k_peer_find(ar, arvif->vdev_id, vif->addr);
5680                if (!peer) {
5681                        ath10k_warn(ar, "failed to lookup peer %pM on vdev %i\n",
5682                                    vif->addr, arvif->vdev_id);
5683                        spin_unlock_bh(&ar->data_lock);
5684                        ret = -ENOENT;
5685                        goto err_peer_delete;
5686                }
5687
5688                arvif->peer_id = find_first_bit(peer->peer_ids,
5689                                                ATH10K_MAX_NUM_PEER_IDS);
5690
5691                spin_unlock_bh(&ar->data_lock);
5692        } else {
5693                arvif->peer_id = HTT_INVALID_PEERID;
5694        }
5695
5696        if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
5697                ret = ath10k_mac_set_kickout(arvif);
5698                if (ret) {
5699                        ath10k_warn(ar, "failed to set vdev %i kickout parameters: %d\n",
5700                                    arvif->vdev_id, ret);
5701                        goto err_peer_delete;
5702                }
5703        }
5704
5705        if (arvif->vdev_type == WMI_VDEV_TYPE_STA) {
5706                param = WMI_STA_PS_PARAM_RX_WAKE_POLICY;
5707                value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
5708                ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
5709                                                  param, value);
5710                if (ret) {
5711                        ath10k_warn(ar, "failed to set vdev %i RX wake policy: %d\n",
5712                                    arvif->vdev_id, ret);
5713                        goto err_peer_delete;
5714                }
5715
5716                ret = ath10k_mac_vif_recalc_ps_wake_threshold(arvif);
5717                if (ret) {
5718                        ath10k_warn(ar, "failed to recalc ps wake threshold on vdev %i: %d\n",
5719                                    arvif->vdev_id, ret);
5720                        goto err_peer_delete;
5721                }
5722
5723                ret = ath10k_mac_vif_recalc_ps_poll_count(arvif);
5724                if (ret) {
5725                        ath10k_warn(ar, "failed to recalc ps poll count on vdev %i: %d\n",
5726                                    arvif->vdev_id, ret);
5727                        goto err_peer_delete;
5728                }
5729        }
5730
5731        ret = ath10k_mac_set_txbf_conf(arvif);
5732        if (ret) {
5733                ath10k_warn(ar, "failed to set txbf for vdev %d: %d\n",
5734                            arvif->vdev_id, ret);
5735                goto err_peer_delete;
5736        }
5737
5738        ret = ath10k_mac_set_rts(arvif, ar->hw->wiphy->rts_threshold);
5739        if (ret) {
5740                ath10k_warn(ar, "failed to set rts threshold for vdev %d: %d\n",
5741                            arvif->vdev_id, ret);
5742                goto err_peer_delete;
5743        }
5744
5745        arvif->txpower = vif->bss_conf.txpower;
5746        ret = ath10k_mac_txpower_recalc(ar);
5747        if (ret) {
5748                ath10k_warn(ar, "failed to recalc tx power: %d\n", ret);
5749                goto err_peer_delete;
5750        }
5751
5752        if (test_bit(WMI_SERVICE_RTT_RESPONDER_ROLE, ar->wmi.svc_map)) {
5753                vdev_param = ar->wmi.vdev_param->rtt_responder_role;
5754                ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
5755                                                arvif->ftm_responder);
5756
5757                /* It is harmless to not set FTM role. Do not warn */
5758                if (ret && ret != -EOPNOTSUPP)
5759                        ath10k_warn(ar, "failed to set vdev %i FTM Responder: %d\n",
5760                                    arvif->vdev_id, ret);
5761        }
5762
5763        if (vif->type == NL80211_IFTYPE_MONITOR) {
5764                ar->monitor_arvif = arvif;
5765                ret = ath10k_monitor_recalc(ar);
5766                if (ret) {
5767                        ath10k_warn(ar, "failed to recalc monitor: %d\n", ret);
5768                        goto err_peer_delete;
5769                }
5770        }
5771
5772        spin_lock_bh(&ar->htt.tx_lock);
5773        if (!ar->tx_paused)
5774                ieee80211_wake_queue(ar->hw, arvif->vdev_id);
5775        spin_unlock_bh(&ar->htt.tx_lock);
5776
5777        mutex_unlock(&ar->conf_mutex);
5778        return 0;
5779
5780err_peer_delete:
5781        if (arvif->vdev_type == WMI_VDEV_TYPE_AP ||
5782            arvif->vdev_type == WMI_VDEV_TYPE_IBSS) {
5783                ath10k_wmi_peer_delete(ar, arvif->vdev_id, vif->addr);
5784                ath10k_wait_for_peer_delete_done(ar, arvif->vdev_id,
5785                                                 vif->addr);
5786        }
5787
5788err_vdev_delete:
5789        ath10k_wmi_vdev_delete(ar, arvif->vdev_id);
5790        ar->free_vdev_map |= 1LL << arvif->vdev_id;
5791        spin_lock_bh(&ar->data_lock);
5792        list_del(&arvif->list);
5793        spin_unlock_bh(&ar->data_lock);
5794
5795err:
5796        if (arvif->beacon_buf) {
5797                dma_free_coherent(ar->dev, IEEE80211_MAX_FRAME_LEN,
5798                                  arvif->beacon_buf, arvif->beacon_paddr);
5799                arvif->beacon_buf = NULL;
5800        }
5801
5802        mutex_unlock(&ar->conf_mutex);
5803
5804        return ret;
5805}
5806
5807static void ath10k_mac_vif_tx_unlock_all(struct ath10k_vif *arvif)
5808{
5809        int i;
5810
5811        for (i = 0; i < BITS_PER_LONG; i++)
5812                ath10k_mac_vif_tx_unlock(arvif, i);
5813}
5814
5815static void ath10k_remove_interface(struct ieee80211_hw *hw,
5816                                    struct ieee80211_vif *vif)
5817{
5818        struct ath10k *ar = hw->priv;
5819        struct ath10k_vif *arvif = (void *)vif->drv_priv;
5820        struct ath10k_peer *peer;
5821        unsigned long time_left;
5822        int ret;
5823        int i;
5824
5825        cancel_work_sync(&arvif->ap_csa_work);
5826        cancel_delayed_work_sync(&arvif->connection_loss_work);
5827
5828        mutex_lock(&ar->conf_mutex);
5829
5830        ret = ath10k_spectral_vif_stop(arvif);
5831        if (ret)
5832                ath10k_warn(ar, "failed to stop spectral for vdev %i: %d\n",
5833                            arvif->vdev_id, ret);
5834
5835        ar->free_vdev_map |= 1LL << arvif->vdev_id;
5836        spin_lock_bh(&ar->data_lock);
5837        list_del(&arvif->list);
5838        spin_unlock_bh(&ar->data_lock);
5839
5840        if (arvif->vdev_type == WMI_VDEV_TYPE_AP ||
5841            arvif->vdev_type == WMI_VDEV_TYPE_IBSS) {
5842                ret = ath10k_wmi_peer_delete(arvif->ar, arvif->vdev_id,
5843                                             vif->addr);
5844                if (ret)
5845                        ath10k_warn(ar, "failed to submit AP/IBSS self-peer removal on vdev %i: %d\n",
5846                                    arvif->vdev_id, ret);
5847
5848                ath10k_wait_for_peer_delete_done(ar, arvif->vdev_id,
5849                                                 vif->addr);
5850                kfree(arvif->u.ap.noa_data);
5851        }
5852
5853        ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i delete (remove interface)\n",
5854                   arvif->vdev_id);
5855
5856        ret = ath10k_wmi_vdev_delete(ar, arvif->vdev_id);
5857        if (ret)
5858                ath10k_warn(ar, "failed to delete WMI vdev %i: %d\n",
5859                            arvif->vdev_id, ret);
5860
5861        if (test_bit(WMI_SERVICE_SYNC_DELETE_CMDS, ar->wmi.svc_map)) {
5862                time_left = wait_for_completion_timeout(&ar->vdev_delete_done,
5863                                                        ATH10K_VDEV_DELETE_TIMEOUT_HZ);
5864                if (time_left == 0) {
5865                        ath10k_warn(ar, "Timeout in receiving vdev delete response\n");
5866                        goto out;
5867                }
5868        }
5869
5870        /* Some firmware revisions don't notify host about self-peer removal
5871         * until after associated vdev is deleted.
5872         */
5873        if (arvif->vdev_type == WMI_VDEV_TYPE_AP ||
5874            arvif->vdev_type == WMI_VDEV_TYPE_IBSS) {
5875                ret = ath10k_wait_for_peer_deleted(ar, arvif->vdev_id,
5876                                                   vif->addr);
5877                if (ret)
5878                        ath10k_warn(ar, "failed to remove AP self-peer on vdev %i: %d\n",
5879                                    arvif->vdev_id, ret);
5880
5881                spin_lock_bh(&ar->data_lock);
5882                ar->num_peers--;
5883                spin_unlock_bh(&ar->data_lock);
5884        }
5885
5886        spin_lock_bh(&ar->data_lock);
5887        for (i = 0; i < ARRAY_SIZE(ar->peer_map); i++) {
5888                peer = ar->peer_map[i];
5889                if (!peer)
5890                        continue;
5891
5892                if (peer->vif == vif) {
5893                        ath10k_warn(ar, "found vif peer %pM entry on vdev %i after it was supposedly removed\n",
5894                                    vif->addr, arvif->vdev_id);
5895                        peer->vif = NULL;
5896                }
5897        }
5898
5899        /* Clean this up late, less opportunity for firmware to access
5900         * DMA memory we have deleted.
5901         */
5902        ath10k_mac_vif_beacon_cleanup(arvif);
5903        spin_unlock_bh(&ar->data_lock);
5904
5905        ath10k_peer_cleanup(ar, arvif->vdev_id);
5906        ath10k_mac_txq_unref(ar, vif->txq);
5907
5908        if (vif->type == NL80211_IFTYPE_MONITOR) {
5909                ar->monitor_arvif = NULL;
5910                ret = ath10k_monitor_recalc(ar);
5911                if (ret)
5912                        ath10k_warn(ar, "failed to recalc monitor: %d\n", ret);
5913        }
5914
5915        ret = ath10k_mac_txpower_recalc(ar);
5916        if (ret)
5917                ath10k_warn(ar, "failed to recalc tx power: %d\n", ret);
5918
5919        spin_lock_bh(&ar->htt.tx_lock);
5920        ath10k_mac_vif_tx_unlock_all(arvif);
5921        spin_unlock_bh(&ar->htt.tx_lock);
5922
5923        ath10k_mac_txq_unref(ar, vif->txq);
5924
5925out:
5926        mutex_unlock(&ar->conf_mutex);
5927}
5928
5929/*
5930 * FIXME: Has to be verified.
5931 */
5932#define SUPPORTED_FILTERS                       \
5933        (FIF_ALLMULTI |                         \
5934        FIF_CONTROL |                           \
5935        FIF_PSPOLL |                            \
5936        FIF_OTHER_BSS |                         \
5937        FIF_BCN_PRBRESP_PROMISC |               \
5938        FIF_PROBE_REQ |                         \
5939        FIF_FCSFAIL)
5940
5941static void ath10k_configure_filter(struct ieee80211_hw *hw,
5942                                    unsigned int changed_flags,
5943                                    unsigned int *total_flags,
5944                                    u64 multicast)
5945{
5946        struct ath10k *ar = hw->priv;
5947        int ret;
5948
5949        mutex_lock(&ar->conf_mutex);
5950
5951        changed_flags &= SUPPORTED_FILTERS;
5952        *total_flags &= SUPPORTED_FILTERS;
5953        ar->filter_flags = *total_flags;
5954
5955        ret = ath10k_monitor_recalc(ar);
5956        if (ret)
5957                ath10k_warn(ar, "failed to recalc monitor: %d\n", ret);
5958
5959        mutex_unlock(&ar->conf_mutex);
5960}
5961
5962static void ath10k_recalculate_mgmt_rate(struct ath10k *ar,
5963                                         struct ieee80211_vif *vif,
5964                                         struct cfg80211_chan_def *def)
5965{
5966        struct ath10k_vif *arvif = (void *)vif->drv_priv;
5967        const struct ieee80211_supported_band *sband;
5968        u8 basic_rate_idx;
5969        int hw_rate_code;
5970        u32 vdev_param;
5971        u16 bitrate;
5972        int ret;
5973
5974        lockdep_assert_held(&ar->conf_mutex);
5975
5976        sband = ar->hw->wiphy->bands[def->chan->band];
5977        basic_rate_idx = ffs(vif->bss_conf.basic_rates) - 1;
5978        bitrate = sband->bitrates[basic_rate_idx].bitrate;
5979
5980        hw_rate_code = ath10k_mac_get_rate_hw_value(bitrate);
5981        if (hw_rate_code < 0) {
5982                ath10k_warn(ar, "bitrate not supported %d\n", bitrate);
5983                return;
5984        }
5985
5986        vdev_param = ar->wmi.vdev_param->mgmt_rate;
5987        ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
5988                                        hw_rate_code);
5989        if (ret)
5990                ath10k_warn(ar, "failed to set mgmt tx rate %d\n", ret);
5991}
5992
5993static void ath10k_bss_info_changed(struct ieee80211_hw *hw,
5994                                    struct ieee80211_vif *vif,
5995                                    struct ieee80211_bss_conf *info,
5996                                    u32 changed)
5997{
5998        struct ath10k *ar = hw->priv;
5999        struct ath10k_vif *arvif = (void *)vif->drv_priv;
6000        struct cfg80211_chan_def def;
6001        u32 vdev_param, pdev_param, slottime, preamble;
6002        u16 bitrate, hw_value;
6003        u8 rate, rateidx;
6004        int ret = 0, mcast_rate;
6005        enum nl80211_band band;
6006
6007        mutex_lock(&ar->conf_mutex);
6008
6009        if (changed & BSS_CHANGED_IBSS)
6010                ath10k_control_ibss(arvif, info, vif->addr);
6011
6012        if (changed & BSS_CHANGED_BEACON_INT) {
6013                arvif->beacon_interval = info->beacon_int;
6014                vdev_param = ar->wmi.vdev_param->beacon_interval;
6015                ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
6016                                                arvif->beacon_interval);
6017                ath10k_dbg(ar, ATH10K_DBG_MAC,
6018                           "mac vdev %d beacon_interval %d\n",
6019                           arvif->vdev_id, arvif->beacon_interval);
6020
6021                if (ret)
6022                        ath10k_warn(ar, "failed to set beacon interval for vdev %d: %i\n",
6023                                    arvif->vdev_id, ret);
6024        }
6025
6026        if (changed & BSS_CHANGED_BEACON) {
6027                ath10k_dbg(ar, ATH10K_DBG_MAC,
6028                           "vdev %d set beacon tx mode to staggered\n",
6029                           arvif->vdev_id);
6030
6031                pdev_param = ar->wmi.pdev_param->beacon_tx_mode;
6032                ret = ath10k_wmi_pdev_set_param(ar, pdev_param,
6033                                                WMI_BEACON_STAGGERED_MODE);
6034                if (ret)
6035                        ath10k_warn(ar, "failed to set beacon mode for vdev %d: %i\n",
6036                                    arvif->vdev_id, ret);
6037
6038                ret = ath10k_mac_setup_bcn_tmpl(arvif);
6039                if (ret)
6040                        ath10k_warn(ar, "failed to update beacon template: %d\n",
6041                                    ret);
6042
6043                if (ieee80211_vif_is_mesh(vif)) {
6044                        /* mesh doesn't use SSID but firmware needs it */
6045                        strncpy(arvif->u.ap.ssid, "mesh",
6046                                sizeof(arvif->u.ap.ssid));
6047                        arvif->u.ap.ssid_len = 4;
6048                }
6049        }
6050
6051        if (changed & BSS_CHANGED_AP_PROBE_RESP) {
6052                ret = ath10k_mac_setup_prb_tmpl(arvif);
6053                if (ret)
6054                        ath10k_warn(ar, "failed to setup probe resp template on vdev %i: %d\n",
6055                                    arvif->vdev_id, ret);
6056        }
6057
6058        if (changed & (BSS_CHANGED_BEACON_INFO | BSS_CHANGED_BEACON)) {
6059                arvif->dtim_period = info->dtim_period;
6060
6061                ath10k_dbg(ar, ATH10K_DBG_MAC,
6062                           "mac vdev %d dtim_period %d\n",
6063                           arvif->vdev_id, arvif->dtim_period);
6064
6065                vdev_param = ar->wmi.vdev_param->dtim_period;
6066                ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
6067                                                arvif->dtim_period);
6068                if (ret)
6069                        ath10k_warn(ar, "failed to set dtim period for vdev %d: %i\n",
6070                                    arvif->vdev_id, ret);
6071        }
6072
6073        if (changed & BSS_CHANGED_SSID &&
6074            vif->type == NL80211_IFTYPE_AP) {
6075                arvif->u.ap.ssid_len = info->ssid_len;
6076                if (info->ssid_len)
6077                        memcpy(arvif->u.ap.ssid, info->ssid, info->ssid_len);
6078                arvif->u.ap.hidden_ssid = info->hidden_ssid;
6079        }
6080
6081        if (changed & BSS_CHANGED_BSSID && !is_zero_ether_addr(info->bssid))
6082                ether_addr_copy(arvif->bssid, info->bssid);
6083
6084        if (changed & BSS_CHANGED_FTM_RESPONDER &&
6085            arvif->ftm_responder != info->ftm_responder &&
6086            test_bit(WMI_SERVICE_RTT_RESPONDER_ROLE, ar->wmi.svc_map)) {
6087                arvif->ftm_responder = info->ftm_responder;
6088
6089                vdev_param = ar->wmi.vdev_param->rtt_responder_role;
6090                ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
6091                                                arvif->ftm_responder);
6092
6093                ath10k_dbg(ar, ATH10K_DBG_MAC,
6094                           "mac vdev %d ftm_responder %d:ret %d\n",
6095                           arvif->vdev_id, arvif->ftm_responder, ret);
6096        }
6097
6098        if (changed & BSS_CHANGED_BEACON_ENABLED)
6099                ath10k_control_beaconing(arvif, info);
6100
6101        if (changed & BSS_CHANGED_ERP_CTS_PROT) {
6102                arvif->use_cts_prot = info->use_cts_prot;
6103
6104                ret = ath10k_recalc_rtscts_prot(arvif);
6105                if (ret)
6106                        ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
6107                                    arvif->vdev_id, ret);
6108
6109                if (ath10k_mac_can_set_cts_prot(arvif)) {
6110                        ret = ath10k_mac_set_cts_prot(arvif);
6111                        if (ret)
6112                                ath10k_warn(ar, "failed to set cts protection for vdev %d: %d\n",
6113                                            arvif->vdev_id, ret);
6114                }
6115        }
6116
6117        if (changed & BSS_CHANGED_ERP_SLOT) {
6118                if (info->use_short_slot)
6119                        slottime = WMI_VDEV_SLOT_TIME_SHORT; /* 9us */
6120
6121                else
6122                        slottime = WMI_VDEV_SLOT_TIME_LONG; /* 20us */
6123
6124                ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d slot_time %d\n",
6125                           arvif->vdev_id, slottime);
6126
6127                vdev_param = ar->wmi.vdev_param->slot_time;
6128                ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
6129                                                slottime);
6130                if (ret)
6131                        ath10k_warn(ar, "failed to set erp slot for vdev %d: %i\n",
6132                                    arvif->vdev_id, ret);
6133        }
6134
6135        if (changed & BSS_CHANGED_ERP_PREAMBLE) {
6136                if (info->use_short_preamble)
6137                        preamble = WMI_VDEV_PREAMBLE_SHORT;
6138                else
6139                        preamble = WMI_VDEV_PREAMBLE_LONG;
6140
6141                ath10k_dbg(ar, ATH10K_DBG_MAC,
6142                           "mac vdev %d preamble %dn",
6143                           arvif->vdev_id, preamble);
6144
6145                vdev_param = ar->wmi.vdev_param->preamble;
6146                ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
6147                                                preamble);
6148                if (ret)
6149                        ath10k_warn(ar, "failed to set preamble for vdev %d: %i\n",
6150                                    arvif->vdev_id, ret);
6151        }
6152
6153        if (changed & BSS_CHANGED_ASSOC) {
6154                if (info->assoc) {
6155                        /* Workaround: Make sure monitor vdev is not running
6156                         * when associating to prevent some firmware revisions
6157                         * (e.g. 10.1 and 10.2) from crashing.
6158                         */
6159                        if (ar->monitor_started)
6160                                ath10k_monitor_stop(ar);
6161                        ath10k_bss_assoc(hw, vif, info);
6162                        ath10k_monitor_recalc(ar);
6163                } else {
6164                        ath10k_bss_disassoc(hw, vif);
6165                }
6166        }
6167
6168        if (changed & BSS_CHANGED_TXPOWER) {
6169                ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev_id %i txpower %d\n",
6170                           arvif->vdev_id, info->txpower);
6171
6172                arvif->txpower = info->txpower;
6173                ret = ath10k_mac_txpower_recalc(ar);
6174                if (ret)
6175                        ath10k_warn(ar, "failed to recalc tx power: %d\n", ret);
6176        }
6177
6178        if (changed & BSS_CHANGED_PS) {
6179                arvif->ps = vif->bss_conf.ps;
6180
6181                ret = ath10k_config_ps(ar);
6182                if (ret)
6183                        ath10k_warn(ar, "failed to setup ps on vdev %i: %d\n",
6184                                    arvif->vdev_id, ret);
6185        }
6186
6187        if (changed & BSS_CHANGED_MCAST_RATE &&
6188            !ath10k_mac_vif_chan(arvif->vif, &def)) {
6189                band = def.chan->band;
6190                mcast_rate = vif->bss_conf.mcast_rate[band];
6191                if (mcast_rate > 0)
6192                        rateidx = mcast_rate - 1;
6193                else
6194                        rateidx = ffs(vif->bss_conf.basic_rates) - 1;
6195
6196                if (ar->phy_capability & WHAL_WLAN_11A_CAPABILITY)
6197                        rateidx += ATH10K_MAC_FIRST_OFDM_RATE_IDX;
6198
6199                bitrate = ath10k_wmi_legacy_rates[rateidx].bitrate;
6200                hw_value = ath10k_wmi_legacy_rates[rateidx].hw_value;
6201                if (ath10k_mac_bitrate_is_cck(bitrate))
6202                        preamble = WMI_RATE_PREAMBLE_CCK;
6203                else
6204                        preamble = WMI_RATE_PREAMBLE_OFDM;
6205
6206                rate = ATH10K_HW_RATECODE(hw_value, 0, preamble);
6207
6208                ath10k_dbg(ar, ATH10K_DBG_MAC,
6209                           "mac vdev %d mcast_rate %x\n",
6210                           arvif->vdev_id, rate);
6211
6212                vdev_param = ar->wmi.vdev_param->mcast_data_rate;
6213                ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
6214                                                vdev_param, rate);
6215                if (ret)
6216                        ath10k_warn(ar,
6217                                    "failed to set mcast rate on vdev %i: %d\n",
6218                                    arvif->vdev_id,  ret);
6219
6220                vdev_param = ar->wmi.vdev_param->bcast_data_rate;
6221                ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
6222                                                vdev_param, rate);
6223                if (ret)
6224                        ath10k_warn(ar,
6225                                    "failed to set bcast rate on vdev %i: %d\n",
6226                                    arvif->vdev_id,  ret);
6227        }
6228
6229        if (changed & BSS_CHANGED_BASIC_RATES &&
6230            !ath10k_mac_vif_chan(arvif->vif, &def))
6231                ath10k_recalculate_mgmt_rate(ar, vif, &def);
6232
6233        mutex_unlock(&ar->conf_mutex);
6234}
6235
6236static void ath10k_mac_op_set_coverage_class(struct ieee80211_hw *hw, s16 value)
6237{
6238        struct ath10k *ar = hw->priv;
6239
6240        /* This function should never be called if setting the coverage class
6241         * is not supported on this hardware.
6242         */
6243        if (!ar->hw_params.hw_ops->set_coverage_class) {
6244                WARN_ON_ONCE(1);
6245                return;
6246        }
6247        ar->hw_params.hw_ops->set_coverage_class(ar, value);
6248}
6249
6250struct ath10k_mac_tdls_iter_data {
6251        u32 num_tdls_stations;
6252        struct ieee80211_vif *curr_vif;
6253};
6254
6255static void ath10k_mac_tdls_vif_stations_count_iter(void *data,
6256                                                    struct ieee80211_sta *sta)
6257{
6258        struct ath10k_mac_tdls_iter_data *iter_data = data;
6259        struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
6260        struct ieee80211_vif *sta_vif = arsta->arvif->vif;
6261
6262        if (sta->tdls && sta_vif == iter_data->curr_vif)
6263                iter_data->num_tdls_stations++;
6264}
6265
6266static int ath10k_mac_tdls_vif_stations_count(struct ieee80211_hw *hw,
6267                                              struct ieee80211_vif *vif)
6268{
6269        struct ath10k_mac_tdls_iter_data data = {};
6270
6271        data.curr_vif = vif;
6272
6273        ieee80211_iterate_stations_atomic(hw,
6274                                          ath10k_mac_tdls_vif_stations_count_iter,
6275                                          &data);
6276        return data.num_tdls_stations;
6277}
6278
6279static int ath10k_hw_scan(struct ieee80211_hw *hw,
6280                          struct ieee80211_vif *vif,
6281                          struct ieee80211_scan_request *hw_req)
6282{
6283        struct ath10k *ar = hw->priv;
6284        struct ath10k_vif *arvif = (void *)vif->drv_priv;
6285        struct cfg80211_scan_request *req = &hw_req->req;
6286        struct wmi_start_scan_arg arg;
6287        int ret = 0;
6288        int i;
6289        u32 scan_timeout;
6290
6291        mutex_lock(&ar->conf_mutex);
6292
6293        if (ath10k_mac_tdls_vif_stations_count(hw, vif) > 0) {
6294                ret = -EBUSY;
6295                goto exit;
6296        }
6297
6298        spin_lock_bh(&ar->data_lock);
6299        switch (ar->scan.state) {
6300        case ATH10K_SCAN_IDLE:
6301                reinit_completion(&ar->scan.started);
6302                reinit_completion(&ar->scan.completed);
6303                ar->scan.state = ATH10K_SCAN_STARTING;
6304                ar->scan.is_roc = false;
6305                ar->scan.vdev_id = arvif->vdev_id;
6306                ret = 0;
6307                break;
6308        case ATH10K_SCAN_STARTING:
6309        case ATH10K_SCAN_RUNNING:
6310        case ATH10K_SCAN_ABORTING:
6311                ret = -EBUSY;
6312                break;
6313        }
6314        spin_unlock_bh(&ar->data_lock);
6315
6316        if (ret)
6317                goto exit;
6318
6319        memset(&arg, 0, sizeof(arg));
6320        ath10k_wmi_start_scan_init(ar, &arg);
6321        arg.vdev_id = arvif->vdev_id;
6322        arg.scan_id = ATH10K_SCAN_ID;
6323
6324        if (req->ie_len) {
6325                arg.ie_len = req->ie_len;
6326                memcpy(arg.ie, req->ie, arg.ie_len);
6327        }
6328
6329        if (req->n_ssids) {
6330                arg.n_ssids = req->n_ssids;
6331                for (i = 0; i < arg.n_ssids; i++) {
6332                        arg.ssids[i].len  = req->ssids[i].ssid_len;
6333                        arg.ssids[i].ssid = req->ssids[i].ssid;
6334                }
6335        } else {
6336                arg.scan_ctrl_flags |= WMI_SCAN_FLAG_PASSIVE;
6337        }
6338
6339        if (req->flags & NL80211_SCAN_FLAG_RANDOM_ADDR) {
6340                arg.scan_ctrl_flags |=  WMI_SCAN_ADD_SPOOFED_MAC_IN_PROBE_REQ;
6341                ether_addr_copy(arg.mac_addr.addr, req->mac_addr);
6342                ether_addr_copy(arg.mac_mask.addr, req->mac_addr_mask);
6343        }
6344
6345        if (req->n_channels) {
6346                arg.n_channels = req->n_channels;
6347                for (i = 0; i < arg.n_channels; i++)
6348                        arg.channels[i] = req->channels[i]->center_freq;
6349        }
6350
6351        /* if duration is set, default dwell times will be overwritten */
6352        if (req->duration) {
6353                arg.dwell_time_active = req->duration;
6354                arg.dwell_time_passive = req->duration;
6355                arg.burst_duration_ms = req->duration;
6356
6357                scan_timeout = min_t(u32, arg.max_rest_time *
6358                                (arg.n_channels - 1) + (req->duration +
6359                                ATH10K_SCAN_CHANNEL_SWITCH_WMI_EVT_OVERHEAD) *
6360                                arg.n_channels, arg.max_scan_time + 200);
6361
6362        } else {
6363                /* Add a 200ms margin to account for event/command processing */
6364                scan_timeout = arg.max_scan_time + 200;
6365        }
6366
6367        ret = ath10k_start_scan(ar, &arg);
6368        if (ret) {
6369                ath10k_warn(ar, "failed to start hw scan: %d\n", ret);
6370                spin_lock_bh(&ar->data_lock);
6371                ar->scan.state = ATH10K_SCAN_IDLE;
6372                spin_unlock_bh(&ar->data_lock);
6373        }
6374
6375        ieee80211_queue_delayed_work(ar->hw, &ar->scan.timeout,
6376                                     msecs_to_jiffies(scan_timeout));
6377
6378exit:
6379        mutex_unlock(&ar->conf_mutex);
6380        return ret;
6381}
6382
6383static void ath10k_cancel_hw_scan(struct ieee80211_hw *hw,
6384                                  struct ieee80211_vif *vif)
6385{
6386        struct ath10k *ar = hw->priv;
6387
6388        mutex_lock(&ar->conf_mutex);
6389        ath10k_scan_abort(ar);
6390        mutex_unlock(&ar->conf_mutex);
6391
6392        cancel_delayed_work_sync(&ar->scan.timeout);
6393}
6394
6395static void ath10k_set_key_h_def_keyidx(struct ath10k *ar,
6396                                        struct ath10k_vif *arvif,
6397                                        enum set_key_cmd cmd,
6398                                        struct ieee80211_key_conf *key)
6399{
6400        u32 vdev_param = arvif->ar->wmi.vdev_param->def_keyid;
6401        int ret;
6402
6403        /* 10.1 firmware branch requires default key index to be set to group
6404         * key index after installing it. Otherwise FW/HW Txes corrupted
6405         * frames with multi-vif APs. This is not required for main firmware
6406         * branch (e.g. 636).
6407         *
6408         * This is also needed for 636 fw for IBSS-RSN to work more reliably.
6409         *
6410         * FIXME: It remains unknown if this is required for multi-vif STA
6411         * interfaces on 10.1.
6412         */
6413
6414        if (arvif->vdev_type != WMI_VDEV_TYPE_AP &&
6415            arvif->vdev_type != WMI_VDEV_TYPE_IBSS)
6416                return;
6417
6418        if (key->cipher == WLAN_CIPHER_SUITE_WEP40)
6419                return;
6420
6421        if (key->cipher == WLAN_CIPHER_SUITE_WEP104)
6422                return;
6423
6424        if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
6425                return;
6426
6427        if (cmd != SET_KEY)
6428                return;
6429
6430        ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
6431                                        key->keyidx);
6432        if (ret)
6433                ath10k_warn(ar, "failed to set vdev %i group key as default key: %d\n",
6434                            arvif->vdev_id, ret);
6435}
6436
6437static int ath10k_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
6438                          struct ieee80211_vif *vif, struct ieee80211_sta *sta,
6439                          struct ieee80211_key_conf *key)
6440{
6441        struct ath10k *ar = hw->priv;
6442        struct ath10k_vif *arvif = (void *)vif->drv_priv;
6443        struct ath10k_sta *arsta;
6444        struct ath10k_peer *peer;
6445        const u8 *peer_addr;
6446        bool is_wep = key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
6447                      key->cipher == WLAN_CIPHER_SUITE_WEP104;
6448        int ret = 0;
6449        int ret2;
6450        u32 flags = 0;
6451        u32 flags2;
6452
6453        /* this one needs to be done in software */
6454        if (key->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
6455            key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
6456            key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256 ||
6457            key->cipher == WLAN_CIPHER_SUITE_BIP_CMAC_256)
6458                return 1;
6459
6460        if (arvif->nohwcrypt)
6461                return 1;
6462
6463        if (key->keyidx > WMI_MAX_KEY_INDEX)
6464                return -ENOSPC;
6465
6466        mutex_lock(&ar->conf_mutex);
6467
6468        if (sta) {
6469                arsta = (struct ath10k_sta *)sta->drv_priv;
6470                peer_addr = sta->addr;
6471                spin_lock_bh(&ar->data_lock);
6472                arsta->ucast_cipher = key->cipher;
6473                spin_unlock_bh(&ar->data_lock);
6474        } else if (arvif->vdev_type == WMI_VDEV_TYPE_STA) {
6475                peer_addr = vif->bss_conf.bssid;
6476        } else {
6477                peer_addr = vif->addr;
6478        }
6479
6480        key->hw_key_idx = key->keyidx;
6481
6482        if (is_wep) {
6483                if (cmd == SET_KEY)
6484                        arvif->wep_keys[key->keyidx] = key;
6485                else
6486                        arvif->wep_keys[key->keyidx] = NULL;
6487        }
6488
6489        /* the peer should not disappear in mid-way (unless FW goes awry) since
6490         * we already hold conf_mutex. we just make sure its there now.
6491         */
6492        spin_lock_bh(&ar->data_lock);
6493        peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
6494        spin_unlock_bh(&ar->data_lock);
6495
6496        if (!peer) {
6497                if (cmd == SET_KEY) {
6498                        ath10k_warn(ar, "failed to install key for non-existent peer %pM\n",
6499                                    peer_addr);
6500                        ret = -EOPNOTSUPP;
6501                        goto exit;
6502                } else {
6503                        /* if the peer doesn't exist there is no key to disable anymore */
6504                        goto exit;
6505                }
6506        }
6507
6508        if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
6509                flags |= WMI_KEY_PAIRWISE;
6510        else
6511                flags |= WMI_KEY_GROUP;
6512
6513        if (is_wep) {
6514                if (cmd == DISABLE_KEY)
6515                        ath10k_clear_vdev_key(arvif, key);
6516
6517                /* When WEP keys are uploaded it's possible that there are
6518                 * stations associated already (e.g. when merging) without any
6519                 * keys. Static WEP needs an explicit per-peer key upload.
6520                 */
6521                if (vif->type == NL80211_IFTYPE_ADHOC &&
6522                    cmd == SET_KEY)
6523                        ath10k_mac_vif_update_wep_key(arvif, key);
6524
6525                /* 802.1x never sets the def_wep_key_idx so each set_key()
6526                 * call changes default tx key.
6527                 *
6528                 * Static WEP sets def_wep_key_idx via .set_default_unicast_key
6529                 * after first set_key().
6530                 */
6531                if (cmd == SET_KEY && arvif->def_wep_key_idx == -1)
6532                        flags |= WMI_KEY_TX_USAGE;
6533        }
6534
6535        ret = ath10k_install_key(arvif, key, cmd, peer_addr, flags);
6536        if (ret) {
6537                WARN_ON(ret > 0);
6538                ath10k_warn(ar, "failed to install key for vdev %i peer %pM: %d\n",
6539                            arvif->vdev_id, peer_addr, ret);
6540                goto exit;
6541        }
6542
6543        /* mac80211 sets static WEP keys as groupwise while firmware requires
6544         * them to be installed twice as both pairwise and groupwise.
6545         */
6546        if (is_wep && !sta && vif->type == NL80211_IFTYPE_STATION) {
6547                flags2 = flags;
6548                flags2 &= ~WMI_KEY_GROUP;
6549                flags2 |= WMI_KEY_PAIRWISE;
6550
6551                ret = ath10k_install_key(arvif, key, cmd, peer_addr, flags2);
6552                if (ret) {
6553                        WARN_ON(ret > 0);
6554                        ath10k_warn(ar, "failed to install (ucast) key for vdev %i peer %pM: %d\n",
6555                                    arvif->vdev_id, peer_addr, ret);
6556                        ret2 = ath10k_install_key(arvif, key, DISABLE_KEY,
6557                                                  peer_addr, flags);
6558                        if (ret2) {
6559                                WARN_ON(ret2 > 0);
6560                                ath10k_warn(ar, "failed to disable (mcast) key for vdev %i peer %pM: %d\n",
6561                                            arvif->vdev_id, peer_addr, ret2);
6562                        }
6563                        goto exit;
6564                }
6565        }
6566
6567        ath10k_set_key_h_def_keyidx(ar, arvif, cmd, key);
6568
6569        spin_lock_bh(&ar->data_lock);
6570        peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
6571        if (peer && cmd == SET_KEY)
6572                peer->keys[key->keyidx] = key;
6573        else if (peer && cmd == DISABLE_KEY)
6574                peer->keys[key->keyidx] = NULL;
6575        else if (peer == NULL)
6576                /* impossible unless FW goes crazy */
6577                ath10k_warn(ar, "Peer %pM disappeared!\n", peer_addr);
6578        spin_unlock_bh(&ar->data_lock);
6579
6580        if (sta && sta->tdls)
6581                ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
6582                                          ar->wmi.peer_param->authorize, 1);
6583        else if (sta && cmd == SET_KEY && (key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
6584                ath10k_wmi_peer_set_param(ar, arvif->vdev_id, peer_addr,
6585                                          ar->wmi.peer_param->authorize, 1);
6586
6587exit:
6588        mutex_unlock(&ar->conf_mutex);
6589        return ret;
6590}
6591
6592static void ath10k_set_default_unicast_key(struct ieee80211_hw *hw,
6593                                           struct ieee80211_vif *vif,
6594                                           int keyidx)
6595{
6596        struct ath10k *ar = hw->priv;
6597        struct ath10k_vif *arvif = (void *)vif->drv_priv;
6598        int ret;
6599
6600        mutex_lock(&arvif->ar->conf_mutex);
6601
6602        if (arvif->ar->state != ATH10K_STATE_ON)
6603                goto unlock;
6604
6605        ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d set keyidx %d\n",
6606                   arvif->vdev_id, keyidx);
6607
6608        ret = ath10k_wmi_vdev_set_param(arvif->ar,
6609                                        arvif->vdev_id,
6610                                        arvif->ar->wmi.vdev_param->def_keyid,
6611                                        keyidx);
6612
6613        if (ret) {
6614                ath10k_warn(ar, "failed to update wep key index for vdev %d: %d\n",
6615                            arvif->vdev_id,
6616                            ret);
6617                goto unlock;
6618        }
6619
6620        arvif->def_wep_key_idx = keyidx;
6621
6622unlock:
6623        mutex_unlock(&arvif->ar->conf_mutex);
6624}
6625
6626static void ath10k_sta_rc_update_wk(struct work_struct *wk)
6627{
6628        struct ath10k *ar;
6629        struct ath10k_vif *arvif;
6630        struct ath10k_sta *arsta;
6631        struct ieee80211_sta *sta;
6632        struct cfg80211_chan_def def;
6633        enum nl80211_band band;
6634        const u8 *ht_mcs_mask;
6635        const u16 *vht_mcs_mask;
6636        u32 changed, bw, nss, smps;
6637        int err;
6638
6639        arsta = container_of(wk, struct ath10k_sta, update_wk);
6640        sta = container_of((void *)arsta, struct ieee80211_sta, drv_priv);
6641        arvif = arsta->arvif;
6642        ar = arvif->ar;
6643
6644        if (WARN_ON(ath10k_mac_vif_chan(arvif->vif, &def)))
6645                return;
6646
6647        band = def.chan->band;
6648        ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
6649        vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
6650
6651        spin_lock_bh(&ar->data_lock);
6652
6653        changed = arsta->changed;
6654        arsta->changed = 0;
6655
6656        bw = arsta->bw;
6657        nss = arsta->nss;
6658        smps = arsta->smps;
6659
6660        spin_unlock_bh(&ar->data_lock);
6661
6662        mutex_lock(&ar->conf_mutex);
6663
6664        nss = max_t(u32, 1, nss);
6665        nss = min(nss, max(ath10k_mac_max_ht_nss(ht_mcs_mask),
6666                           ath10k_mac_max_vht_nss(vht_mcs_mask)));
6667
6668        if (changed & IEEE80211_RC_BW_CHANGED) {
6669                enum wmi_phy_mode mode;
6670
6671                mode = chan_to_phymode(&def);
6672                ath10k_dbg(ar, ATH10K_DBG_STA, "mac update sta %pM peer bw %d phymode %d\n",
6673                           sta->addr, bw, mode);
6674
6675                err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
6676                                                ar->wmi.peer_param->phymode, mode);
6677                if (err) {
6678                        ath10k_warn(ar, "failed to update STA %pM peer phymode %d: %d\n",
6679                                    sta->addr, mode, err);
6680                        goto exit;
6681                }
6682
6683                err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
6684                                                ar->wmi.peer_param->chan_width, bw);
6685                if (err)
6686                        ath10k_warn(ar, "failed to update STA %pM peer bw %d: %d\n",
6687                                    sta->addr, bw, err);
6688        }
6689
6690        if (changed & IEEE80211_RC_NSS_CHANGED) {
6691                ath10k_dbg(ar, ATH10K_DBG_STA, "mac update sta %pM nss %d\n",
6692                           sta->addr, nss);
6693
6694                err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
6695                                                ar->wmi.peer_param->nss, nss);
6696                if (err)
6697                        ath10k_warn(ar, "failed to update STA %pM nss %d: %d\n",
6698                                    sta->addr, nss, err);
6699        }
6700
6701        if (changed & IEEE80211_RC_SMPS_CHANGED) {
6702                ath10k_dbg(ar, ATH10K_DBG_STA, "mac update sta %pM smps %d\n",
6703                           sta->addr, smps);
6704
6705                err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
6706                                                ar->wmi.peer_param->smps_state, smps);
6707                if (err)
6708                        ath10k_warn(ar, "failed to update STA %pM smps %d: %d\n",
6709                                    sta->addr, smps, err);
6710        }
6711
6712        if (changed & IEEE80211_RC_SUPP_RATES_CHANGED) {
6713                ath10k_dbg(ar, ATH10K_DBG_STA, "mac update sta %pM supp rates\n",
6714                           sta->addr);
6715
6716                err = ath10k_station_assoc(ar, arvif->vif, sta, true);
6717                if (err)
6718                        ath10k_warn(ar, "failed to reassociate station: %pM\n",
6719                                    sta->addr);
6720        }
6721
6722exit:
6723        mutex_unlock(&ar->conf_mutex);
6724}
6725
6726static int ath10k_mac_inc_num_stations(struct ath10k_vif *arvif,
6727                                       struct ieee80211_sta *sta)
6728{
6729        struct ath10k *ar = arvif->ar;
6730
6731        lockdep_assert_held(&ar->conf_mutex);
6732
6733        if (arvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
6734                return 0;
6735
6736        if (ar->num_stations >= ar->max_num_stations)
6737                return -ENOBUFS;
6738
6739        ar->num_stations++;
6740
6741        return 0;
6742}
6743
6744static void ath10k_mac_dec_num_stations(struct ath10k_vif *arvif,
6745                                        struct ieee80211_sta *sta)
6746{
6747        struct ath10k *ar = arvif->ar;
6748
6749        lockdep_assert_held(&ar->conf_mutex);
6750
6751        if (arvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
6752                return;
6753
6754        ar->num_stations--;
6755}
6756
6757static int ath10k_sta_set_txpwr(struct ieee80211_hw *hw,
6758                                struct ieee80211_vif *vif,
6759                                struct ieee80211_sta *sta)
6760{
6761        struct ath10k *ar = hw->priv;
6762        struct ath10k_vif *arvif = (void *)vif->drv_priv;
6763        int ret = 0;
6764        s16 txpwr;
6765
6766        if (sta->txpwr.type == NL80211_TX_POWER_AUTOMATIC) {
6767                txpwr = 0;
6768        } else {
6769                txpwr = sta->txpwr.power;
6770                if (!txpwr)
6771                        return -EINVAL;
6772        }
6773
6774        if (txpwr > ATH10K_TX_POWER_MAX_VAL || txpwr < ATH10K_TX_POWER_MIN_VAL)
6775                return -EINVAL;
6776
6777        mutex_lock(&ar->conf_mutex);
6778
6779        ret = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
6780                                        ar->wmi.peer_param->use_fixed_power, txpwr);
6781        if (ret) {
6782                ath10k_warn(ar, "failed to set tx power for station ret: %d\n",
6783                            ret);
6784                goto out;
6785        }
6786
6787out:
6788        mutex_unlock(&ar->conf_mutex);
6789        return ret;
6790}
6791
6792struct ath10k_mac_iter_tid_conf_data {
6793        struct ieee80211_vif *curr_vif;
6794        struct ath10k *ar;
6795        bool reset_config;
6796};
6797
6798static bool
6799ath10k_mac_bitrate_mask_has_single_rate(struct ath10k *ar,
6800                                        enum nl80211_band band,
6801                                        const struct cfg80211_bitrate_mask *mask,
6802                                        int *vht_num_rates)
6803{
6804        int num_rates = 0;
6805        int i, tmp;
6806
6807        num_rates += hweight32(mask->control[band].legacy);
6808
6809        for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++)
6810                num_rates += hweight8(mask->control[band].ht_mcs[i]);
6811
6812        *vht_num_rates = 0;
6813        for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
6814                tmp = hweight16(mask->control[band].vht_mcs[i]);
6815                num_rates += tmp;
6816                *vht_num_rates += tmp;
6817        }
6818
6819        return num_rates == 1;
6820}
6821
6822static int
6823ath10k_mac_bitrate_mask_get_single_rate(struct ath10k *ar,
6824                                        enum nl80211_band band,
6825                                        const struct cfg80211_bitrate_mask *mask,
6826                                        u8 *rate, u8 *nss, bool vht_only)
6827{
6828        int rate_idx;
6829        int i;
6830        u16 bitrate;
6831        u8 preamble;
6832        u8 hw_rate;
6833
6834        if (vht_only)
6835                goto next;
6836
6837        if (hweight32(mask->control[band].legacy) == 1) {
6838                rate_idx = ffs(mask->control[band].legacy) - 1;
6839
6840                if (ar->phy_capability & WHAL_WLAN_11A_CAPABILITY)
6841                        rate_idx += ATH10K_MAC_FIRST_OFDM_RATE_IDX;
6842
6843                hw_rate = ath10k_wmi_legacy_rates[rate_idx].hw_value;
6844                bitrate = ath10k_wmi_legacy_rates[rate_idx].bitrate;
6845
6846                if (ath10k_mac_bitrate_is_cck(bitrate))
6847                        preamble = WMI_RATE_PREAMBLE_CCK;
6848                else
6849                        preamble = WMI_RATE_PREAMBLE_OFDM;
6850
6851                *nss = 1;
6852                *rate = preamble << 6 |
6853                        (*nss - 1) << 4 |
6854                        hw_rate << 0;
6855
6856                return 0;
6857        }
6858
6859        for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++) {
6860                if (hweight8(mask->control[band].ht_mcs[i]) == 1) {
6861                        *nss = i + 1;
6862                        *rate = WMI_RATE_PREAMBLE_HT << 6 |
6863                                (*nss - 1) << 4 |
6864                                (ffs(mask->control[band].ht_mcs[i]) - 1);
6865
6866                        return 0;
6867                }
6868        }
6869
6870next:
6871        for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
6872                if (hweight16(mask->control[band].vht_mcs[i]) == 1) {
6873                        *nss = i + 1;
6874                        *rate = WMI_RATE_PREAMBLE_VHT << 6 |
6875                                (*nss - 1) << 4 |
6876                                (ffs(mask->control[band].vht_mcs[i]) - 1);
6877
6878                        return 0;
6879                }
6880        }
6881
6882        return -EINVAL;
6883}
6884
6885static int ath10k_mac_validate_rate_mask(struct ath10k *ar,
6886                                         struct ieee80211_sta *sta,
6887                                         u32 rate_ctrl_flag, u8 nss)
6888{
6889        if (nss > sta->rx_nss) {
6890                ath10k_warn(ar, "Invalid nss field, configured %u limit %u\n",
6891                            nss, sta->rx_nss);
6892                return -EINVAL;
6893        }
6894
6895        if (ATH10K_HW_PREAMBLE(rate_ctrl_flag) == WMI_RATE_PREAMBLE_VHT) {
6896                if (!sta->vht_cap.vht_supported) {
6897                        ath10k_warn(ar, "Invalid VHT rate for sta %pM\n",
6898                                    sta->addr);
6899                        return -EINVAL;
6900                }
6901        } else if (ATH10K_HW_PREAMBLE(rate_ctrl_flag) == WMI_RATE_PREAMBLE_HT) {
6902                if (!sta->ht_cap.ht_supported || sta->vht_cap.vht_supported) {
6903                        ath10k_warn(ar, "Invalid HT rate for sta %pM\n",
6904                                    sta->addr);
6905                        return -EINVAL;
6906                }
6907        } else {
6908                if (sta->ht_cap.ht_supported || sta->vht_cap.vht_supported)
6909                        return -EINVAL;
6910        }
6911
6912        return 0;
6913}
6914
6915static int
6916ath10k_mac_tid_bitrate_config(struct ath10k *ar,
6917                              struct ieee80211_vif *vif,
6918                              struct ieee80211_sta *sta,
6919                              u32 *rate_ctrl_flag, u8 *rate_ctrl,
6920                              enum nl80211_tx_rate_setting txrate_type,
6921                              const struct cfg80211_bitrate_mask *mask)
6922{
6923        struct cfg80211_chan_def def;
6924        enum nl80211_band band;
6925        u8 nss, rate;
6926        int vht_num_rates, ret;
6927
6928        if (WARN_ON(ath10k_mac_vif_chan(vif, &def)))
6929                return -EINVAL;
6930
6931        if (txrate_type == NL80211_TX_RATE_AUTOMATIC) {
6932                *rate_ctrl = WMI_TID_CONFIG_RATE_CONTROL_AUTO;
6933                *rate_ctrl_flag = 0;
6934                return 0;
6935        }
6936
6937        band = def.chan->band;
6938
6939        if (!ath10k_mac_bitrate_mask_has_single_rate(ar, band, mask,
6940                                                     &vht_num_rates)) {
6941                return -EINVAL;
6942        }
6943
6944        ret = ath10k_mac_bitrate_mask_get_single_rate(ar, band, mask,
6945                                                      &rate, &nss, false);
6946        if (ret) {
6947                ath10k_warn(ar, "failed to get single rate: %d\n",
6948                            ret);
6949                return ret;
6950        }
6951
6952        *rate_ctrl_flag = rate;
6953
6954        if (sta && ath10k_mac_validate_rate_mask(ar, sta, *rate_ctrl_flag, nss))
6955                return -EINVAL;
6956
6957        if (txrate_type == NL80211_TX_RATE_FIXED)
6958                *rate_ctrl = WMI_TID_CONFIG_RATE_CONTROL_FIXED_RATE;
6959        else if (txrate_type == NL80211_TX_RATE_LIMITED &&
6960                 (test_bit(WMI_SERVICE_EXT_PEER_TID_CONFIGS_SUPPORT,
6961                           ar->wmi.svc_map)))
6962                *rate_ctrl = WMI_PEER_TID_CONFIG_RATE_UPPER_CAP;
6963        else
6964                return -EOPNOTSUPP;
6965
6966        return 0;
6967}
6968
6969static int ath10k_mac_set_tid_config(struct ath10k *ar, struct ieee80211_sta *sta,
6970                                     struct ieee80211_vif *vif, u32 changed,
6971                                     struct wmi_per_peer_per_tid_cfg_arg *arg)
6972{
6973        struct ath10k_vif *arvif = (void *)vif->drv_priv;
6974        struct ath10k_sta *arsta;
6975        int ret;
6976
6977        if (sta) {
6978                if (!sta->wme)
6979                        return -ENOTSUPP;
6980
6981                arsta = (struct ath10k_sta *)sta->drv_priv;
6982
6983                if (changed & BIT(NL80211_TID_CONFIG_ATTR_NOACK)) {
6984                        if ((arsta->retry_long[arg->tid] > 0 ||
6985                             arsta->rate_code[arg->tid] > 0 ||
6986                             arsta->ampdu[arg->tid] ==
6987                                        WMI_TID_CONFIG_AGGR_CONTROL_ENABLE) &&
6988                             arg->ack_policy == WMI_PEER_TID_CONFIG_NOACK) {
6989                                changed &= ~BIT(NL80211_TID_CONFIG_ATTR_NOACK);
6990                                arg->ack_policy = 0;
6991                                arg->aggr_control = 0;
6992                                arg->rate_ctrl = 0;
6993                                arg->rcode_flags = 0;
6994                        }
6995                }
6996
6997                if (changed & BIT(NL80211_TID_CONFIG_ATTR_AMPDU_CTRL)) {
6998                        if (arsta->noack[arg->tid] == WMI_PEER_TID_CONFIG_NOACK ||
6999                            arvif->noack[arg->tid] == WMI_PEER_TID_CONFIG_NOACK) {
7000                                arg->aggr_control = 0;
7001                                changed &= ~BIT(NL80211_TID_CONFIG_ATTR_RETRY_LONG);
7002                        }
7003                }
7004
7005                if (changed & (BIT(NL80211_TID_CONFIG_ATTR_TX_RATE) |
7006                    BIT(NL80211_TID_CONFIG_ATTR_TX_RATE_TYPE))) {
7007                        if (arsta->noack[arg->tid] == WMI_PEER_TID_CONFIG_NOACK ||
7008                            arvif->noack[arg->tid] == WMI_PEER_TID_CONFIG_NOACK) {
7009                                arg->rate_ctrl = 0;
7010                                arg->rcode_flags = 0;
7011                        }
7012                }
7013
7014                ether_addr_copy(arg->peer_macaddr.addr, sta->addr);
7015
7016                ret = ath10k_wmi_set_per_peer_per_tid_cfg(ar, arg);
7017                if (ret)
7018                        return ret;
7019
7020                /* Store the configured parameters in success case */
7021                if (changed & BIT(NL80211_TID_CONFIG_ATTR_NOACK)) {
7022                        arsta->noack[arg->tid] = arg->ack_policy;
7023                        arg->ack_policy = 0;
7024                        arg->aggr_control = 0;
7025                        arg->rate_ctrl = 0;
7026                        arg->rcode_flags = 0;
7027                }
7028
7029                if (changed & BIT(NL80211_TID_CONFIG_ATTR_RETRY_LONG)) {
7030                        arsta->retry_long[arg->tid] = arg->retry_count;
7031                        arg->retry_count = 0;
7032                }
7033
7034                if (changed & BIT(NL80211_TID_CONFIG_ATTR_AMPDU_CTRL)) {
7035                        arsta->ampdu[arg->tid] = arg->aggr_control;
7036                        arg->aggr_control = 0;
7037                }
7038
7039                if (changed & (BIT(NL80211_TID_CONFIG_ATTR_TX_RATE) |
7040                    BIT(NL80211_TID_CONFIG_ATTR_TX_RATE_TYPE))) {
7041                        arsta->rate_ctrl[arg->tid] = arg->rate_ctrl;
7042                        arg->rate_ctrl = 0;
7043                        arg->rcode_flags = 0;
7044                }
7045
7046                if (changed & BIT(NL80211_TID_CONFIG_ATTR_RTSCTS_CTRL)) {
7047                        arsta->rtscts[arg->tid] = arg->rtscts_ctrl;
7048                        arg->ext_tid_cfg_bitmap = 0;
7049                }
7050        } else {
7051                if (changed & BIT(NL80211_TID_CONFIG_ATTR_NOACK)) {
7052                        if ((arvif->retry_long[arg->tid] ||
7053                             arvif->rate_code[arg->tid] ||
7054                             arvif->ampdu[arg->tid] ==
7055                                        WMI_TID_CONFIG_AGGR_CONTROL_ENABLE) &&
7056                             arg->ack_policy == WMI_PEER_TID_CONFIG_NOACK) {
7057                                changed &= ~BIT(NL80211_TID_CONFIG_ATTR_NOACK);
7058                        } else {
7059                                arvif->noack[arg->tid] = arg->ack_policy;
7060                                arvif->ampdu[arg->tid] = arg->aggr_control;
7061                                arvif->rate_ctrl[arg->tid] = arg->rate_ctrl;
7062                        }
7063                }
7064
7065                if (changed & BIT(NL80211_TID_CONFIG_ATTR_RETRY_LONG)) {
7066                        if (arvif->noack[arg->tid] == WMI_PEER_TID_CONFIG_NOACK)
7067                                changed &= ~BIT(NL80211_TID_CONFIG_ATTR_RETRY_LONG);
7068                        else
7069                                arvif->retry_long[arg->tid] = arg->retry_count;
7070                }
7071
7072                if (changed & BIT(NL80211_TID_CONFIG_ATTR_AMPDU_CTRL)) {
7073                        if (arvif->noack[arg->tid] == WMI_PEER_TID_CONFIG_NOACK)
7074                                changed &= ~BIT(NL80211_TID_CONFIG_ATTR_AMPDU_CTRL);
7075                        else
7076                                arvif->ampdu[arg->tid] = arg->aggr_control;
7077                }
7078
7079                if (changed & (BIT(NL80211_TID_CONFIG_ATTR_TX_RATE) |
7080                    BIT(NL80211_TID_CONFIG_ATTR_TX_RATE_TYPE))) {
7081                        if (arvif->noack[arg->tid] == WMI_PEER_TID_CONFIG_NOACK) {
7082                                changed &= ~(BIT(NL80211_TID_CONFIG_ATTR_TX_RATE) |
7083                                             BIT(NL80211_TID_CONFIG_ATTR_TX_RATE_TYPE));
7084                        } else {
7085                                arvif->rate_ctrl[arg->tid] = arg->rate_ctrl;
7086                                arvif->rate_code[arg->tid] = arg->rcode_flags;
7087                        }
7088                }
7089
7090                if (changed & BIT(NL80211_TID_CONFIG_ATTR_RTSCTS_CTRL)) {
7091                        arvif->rtscts[arg->tid] = arg->rtscts_ctrl;
7092                        arg->ext_tid_cfg_bitmap = 0;
7093                }
7094
7095                if (changed)
7096                        arvif->tid_conf_changed[arg->tid] |= changed;
7097        }
7098
7099        return 0;
7100}
7101
7102static int
7103ath10k_mac_parse_tid_config(struct ath10k *ar,
7104                            struct ieee80211_sta *sta,
7105                            struct ieee80211_vif *vif,
7106                            struct cfg80211_tid_cfg *tid_conf,
7107                            struct wmi_per_peer_per_tid_cfg_arg *arg)
7108{
7109        u32 changed = tid_conf->mask;
7110        int ret = 0, i = 0;
7111
7112        if (!changed)
7113                return -EINVAL;
7114
7115        while (i < ATH10K_TID_MAX) {
7116                if (!(tid_conf->tids & BIT(i))) {
7117                        i++;
7118                        continue;
7119                }
7120
7121                arg->tid = i;
7122
7123                if (changed & BIT(NL80211_TID_CONFIG_ATTR_NOACK)) {
7124                        if (tid_conf->noack == NL80211_TID_CONFIG_ENABLE) {
7125                                arg->ack_policy = WMI_PEER_TID_CONFIG_NOACK;
7126                                arg->rate_ctrl =
7127                                WMI_TID_CONFIG_RATE_CONTROL_DEFAULT_LOWEST_RATE;
7128                                arg->aggr_control =
7129                                        WMI_TID_CONFIG_AGGR_CONTROL_DISABLE;
7130                        } else {
7131                                arg->ack_policy =
7132                                        WMI_PEER_TID_CONFIG_ACK;
7133                                arg->rate_ctrl =
7134                                        WMI_TID_CONFIG_RATE_CONTROL_AUTO;
7135                                arg->aggr_control =
7136                                        WMI_TID_CONFIG_AGGR_CONTROL_ENABLE;
7137                        }
7138                }
7139
7140                if (changed & BIT(NL80211_TID_CONFIG_ATTR_RETRY_LONG))
7141                        arg->retry_count = tid_conf->retry_long;
7142
7143                if (changed & BIT(NL80211_TID_CONFIG_ATTR_AMPDU_CTRL)) {
7144                        if (tid_conf->noack == NL80211_TID_CONFIG_ENABLE)
7145                                arg->aggr_control = WMI_TID_CONFIG_AGGR_CONTROL_ENABLE;
7146                        else
7147                                arg->aggr_control = WMI_TID_CONFIG_AGGR_CONTROL_DISABLE;
7148                }
7149
7150                if (changed & (BIT(NL80211_TID_CONFIG_ATTR_TX_RATE) |
7151                    BIT(NL80211_TID_CONFIG_ATTR_TX_RATE_TYPE))) {
7152                        ret = ath10k_mac_tid_bitrate_config(ar, vif, sta,
7153                                                            &arg->rcode_flags,
7154                                                            &arg->rate_ctrl,
7155                                                            tid_conf->txrate_type,
7156                                                        &tid_conf->txrate_mask);
7157                        if (ret) {
7158                                ath10k_warn(ar, "failed to configure bitrate mask %d\n",
7159                                            ret);
7160                                arg->rcode_flags = 0;
7161                                arg->rate_ctrl = 0;
7162                        }
7163                }
7164
7165                if (changed & BIT(NL80211_TID_CONFIG_ATTR_RTSCTS_CTRL)) {
7166                        if (tid_conf->rtscts)
7167                                arg->rtscts_ctrl = tid_conf->rtscts;
7168
7169                        arg->ext_tid_cfg_bitmap = WMI_EXT_TID_RTS_CTS_CONFIG;
7170                }
7171
7172                ret = ath10k_mac_set_tid_config(ar, sta, vif, changed, arg);
7173                if (ret)
7174                        return ret;
7175                i++;
7176        }
7177
7178        return ret;
7179}
7180
7181static int ath10k_mac_reset_tid_config(struct ath10k *ar,
7182                                       struct ieee80211_sta *sta,
7183                                       struct ath10k_vif *arvif,
7184                                       u8 tids)
7185{
7186        struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
7187        struct wmi_per_peer_per_tid_cfg_arg arg;
7188        int ret = 0, i = 0;
7189
7190        arg.vdev_id = arvif->vdev_id;
7191        while (i < ATH10K_TID_MAX) {
7192                if (!(tids & BIT(i))) {
7193                        i++;
7194                        continue;
7195                }
7196
7197                arg.tid = i;
7198                arg.ack_policy = WMI_PEER_TID_CONFIG_ACK;
7199                arg.retry_count = ATH10K_MAX_RETRY_COUNT;
7200                arg.rate_ctrl = WMI_TID_CONFIG_RATE_CONTROL_AUTO;
7201                arg.aggr_control = WMI_TID_CONFIG_AGGR_CONTROL_ENABLE;
7202                arg.rtscts_ctrl = WMI_TID_CONFIG_RTSCTS_CONTROL_ENABLE;
7203                arg.ext_tid_cfg_bitmap = WMI_EXT_TID_RTS_CTS_CONFIG;
7204
7205                ether_addr_copy(arg.peer_macaddr.addr, sta->addr);
7206
7207                ret = ath10k_wmi_set_per_peer_per_tid_cfg(ar, &arg);
7208                if (ret)
7209                        return ret;
7210
7211                if (!arvif->tids_rst) {
7212                        arsta->retry_long[i] = -1;
7213                        arsta->noack[i] = -1;
7214                        arsta->ampdu[i] = -1;
7215                        arsta->rate_code[i] = -1;
7216                        arsta->rate_ctrl[i] = 0;
7217                        arsta->rtscts[i] = -1;
7218                } else {
7219                        arvif->retry_long[i] = 0;
7220                        arvif->noack[i] = 0;
7221                        arvif->ampdu[i] = 0;
7222                        arvif->rate_code[i] = 0;
7223                        arvif->rate_ctrl[i] = 0;
7224                        arvif->rtscts[i] = 0;
7225                }
7226
7227                i++;
7228        }
7229
7230        return ret;
7231}
7232
7233static void ath10k_sta_tid_cfg_wk(struct work_struct *wk)
7234{
7235        struct wmi_per_peer_per_tid_cfg_arg arg = {};
7236        struct ieee80211_sta *sta;
7237        struct ath10k_sta *arsta;
7238        struct ath10k_vif *arvif;
7239        struct ath10k *ar;
7240        bool config_apply;
7241        int ret, i;
7242        u32 changed;
7243        u8 nss;
7244
7245        arsta = container_of(wk, struct ath10k_sta, tid_config_wk);
7246        sta = container_of((void *)arsta, struct ieee80211_sta, drv_priv);
7247        arvif = arsta->arvif;
7248        ar = arvif->ar;
7249
7250        mutex_lock(&ar->conf_mutex);
7251
7252        if (arvif->tids_rst) {
7253                ret = ath10k_mac_reset_tid_config(ar, sta, arvif,
7254                                                  arvif->tids_rst);
7255                goto exit;
7256        }
7257
7258        ether_addr_copy(arg.peer_macaddr.addr, sta->addr);
7259
7260        for (i = 0; i < ATH10K_TID_MAX; i++) {
7261                config_apply = false;
7262                changed = arvif->tid_conf_changed[i];
7263
7264                if (changed & BIT(NL80211_TID_CONFIG_ATTR_NOACK)) {
7265                        if (arsta->noack[i] != -1) {
7266                                arg.ack_policy  = 0;
7267                        } else {
7268                                config_apply = true;
7269                                arg.ack_policy = arvif->noack[i];
7270                                arg.aggr_control = arvif->ampdu[i];
7271                                arg.rate_ctrl = arvif->rate_ctrl[i];
7272                        }
7273                }
7274
7275                if (changed & BIT(NL80211_TID_CONFIG_ATTR_RETRY_LONG)) {
7276                        if (arsta->retry_long[i] != -1 ||
7277                            arsta->noack[i] == WMI_PEER_TID_CONFIG_NOACK ||
7278                            arvif->noack[i] == WMI_PEER_TID_CONFIG_NOACK) {
7279                                arg.retry_count = 0;
7280                        } else {
7281                                arg.retry_count = arvif->retry_long[i];
7282                                config_apply = true;
7283                        }
7284                }
7285
7286                if (changed & BIT(NL80211_TID_CONFIG_ATTR_AMPDU_CTRL)) {
7287                        if (arsta->ampdu[i] != -1 ||
7288                            arsta->noack[i] == WMI_PEER_TID_CONFIG_NOACK ||
7289                            arvif->noack[i] == WMI_PEER_TID_CONFIG_NOACK) {
7290                                arg.aggr_control = 0;
7291                        } else {
7292                                arg.aggr_control = arvif->ampdu[i];
7293                                config_apply = true;
7294                        }
7295                }
7296
7297                if (changed & (BIT(NL80211_TID_CONFIG_ATTR_TX_RATE) |
7298                    BIT(NL80211_TID_CONFIG_ATTR_TX_RATE_TYPE))) {
7299                        nss = ATH10K_HW_NSS(arvif->rate_code[i]);
7300                        ret = ath10k_mac_validate_rate_mask(ar, sta,
7301                                                            arvif->rate_code[i],
7302                                                            nss);
7303                        if (ret &&
7304                            arvif->rate_ctrl[i] > WMI_TID_CONFIG_RATE_CONTROL_AUTO) {
7305                                arg.rate_ctrl = 0;
7306                                arg.rcode_flags = 0;
7307                        }
7308
7309                        if (arsta->rate_ctrl[i] >
7310                            WMI_TID_CONFIG_RATE_CONTROL_AUTO ||
7311                            arsta->noack[i] == WMI_PEER_TID_CONFIG_NOACK ||
7312                            arvif->noack[i] == WMI_PEER_TID_CONFIG_NOACK) {
7313                                arg.rate_ctrl = 0;
7314                                arg.rcode_flags = 0;
7315                        } else {
7316                                arg.rate_ctrl = arvif->rate_ctrl[i];
7317                                arg.rcode_flags = arvif->rate_code[i];
7318                                config_apply = true;
7319                        }
7320                }
7321
7322                if (changed & BIT(NL80211_TID_CONFIG_ATTR_RTSCTS_CTRL)) {
7323                        if (arsta->rtscts[i]) {
7324                                arg.rtscts_ctrl = 0;
7325                                arg.ext_tid_cfg_bitmap = 0;
7326                        } else {
7327                                arg.rtscts_ctrl = arvif->rtscts[i] - 1;
7328                                arg.ext_tid_cfg_bitmap =
7329                                        WMI_EXT_TID_RTS_CTS_CONFIG;
7330                                config_apply = true;
7331                        }
7332                }
7333
7334                arg.tid = i;
7335
7336                if (config_apply) {
7337                        ret = ath10k_wmi_set_per_peer_per_tid_cfg(ar, &arg);
7338                        if (ret)
7339                                ath10k_warn(ar, "failed to set per tid config for sta %pM: %d\n",
7340                                            sta->addr, ret);
7341                }
7342
7343                arg.ack_policy  = 0;
7344                arg.retry_count  = 0;
7345                arg.aggr_control  = 0;
7346                arg.rate_ctrl = 0;
7347                arg.rcode_flags = 0;
7348        }
7349
7350exit:
7351        mutex_unlock(&ar->conf_mutex);
7352}
7353
7354static void ath10k_mac_vif_stations_tid_conf(void *data,
7355                                             struct ieee80211_sta *sta)
7356{
7357        struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
7358        struct ath10k_mac_iter_tid_conf_data *iter_data = data;
7359        struct ieee80211_vif *sta_vif = arsta->arvif->vif;
7360
7361        if (sta_vif != iter_data->curr_vif || !sta->wme)
7362                return;
7363
7364        ieee80211_queue_work(iter_data->ar->hw, &arsta->tid_config_wk);
7365}
7366
7367static int ath10k_sta_state(struct ieee80211_hw *hw,
7368                            struct ieee80211_vif *vif,
7369                            struct ieee80211_sta *sta,
7370                            enum ieee80211_sta_state old_state,
7371                            enum ieee80211_sta_state new_state)
7372{
7373        struct ath10k *ar = hw->priv;
7374        struct ath10k_vif *arvif = (void *)vif->drv_priv;
7375        struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
7376        struct ath10k_peer *peer;
7377        int ret = 0;
7378        int i;
7379
7380        if (old_state == IEEE80211_STA_NOTEXIST &&
7381            new_state == IEEE80211_STA_NONE) {
7382                memset(arsta, 0, sizeof(*arsta));
7383                arsta->arvif = arvif;
7384                arsta->peer_ps_state = WMI_PEER_PS_STATE_DISABLED;
7385                INIT_WORK(&arsta->update_wk, ath10k_sta_rc_update_wk);
7386                INIT_WORK(&arsta->tid_config_wk, ath10k_sta_tid_cfg_wk);
7387
7388                for (i = 0; i < ARRAY_SIZE(sta->txq); i++)
7389                        ath10k_mac_txq_init(sta->txq[i]);
7390        }
7391
7392        /* cancel must be done outside the mutex to avoid deadlock */
7393        if ((old_state == IEEE80211_STA_NONE &&
7394             new_state == IEEE80211_STA_NOTEXIST)) {
7395                cancel_work_sync(&arsta->update_wk);
7396                cancel_work_sync(&arsta->tid_config_wk);
7397        }
7398
7399        mutex_lock(&ar->conf_mutex);
7400
7401        if (old_state == IEEE80211_STA_NOTEXIST &&
7402            new_state == IEEE80211_STA_NONE) {
7403                /*
7404                 * New station addition.
7405                 */
7406                enum wmi_peer_type peer_type = WMI_PEER_TYPE_DEFAULT;
7407                u32 num_tdls_stations;
7408
7409                ath10k_dbg(ar, ATH10K_DBG_STA,
7410                           "mac vdev %d peer create %pM (new sta) sta %d / %d peer %d / %d\n",
7411                           arvif->vdev_id, sta->addr,
7412                           ar->num_stations + 1, ar->max_num_stations,
7413                           ar->num_peers + 1, ar->max_num_peers);
7414
7415                num_tdls_stations = ath10k_mac_tdls_vif_stations_count(hw, vif);
7416
7417                if (sta->tdls) {
7418                        if (num_tdls_stations >= ar->max_num_tdls_vdevs) {
7419                                ath10k_warn(ar, "vdev %i exceeded maximum number of tdls vdevs %i\n",
7420                                            arvif->vdev_id,
7421                                            ar->max_num_tdls_vdevs);
7422                                ret = -ELNRNG;
7423                                goto exit;
7424                        }
7425                        peer_type = WMI_PEER_TYPE_TDLS;
7426                }
7427
7428                ret = ath10k_mac_inc_num_stations(arvif, sta);
7429                if (ret) {
7430                        ath10k_warn(ar, "refusing to associate station: too many connected already (%d)\n",
7431                                    ar->max_num_stations);
7432                        goto exit;
7433                }
7434
7435                if (ath10k_debug_is_extd_tx_stats_enabled(ar)) {
7436                        arsta->tx_stats = kzalloc(sizeof(*arsta->tx_stats),
7437                                                  GFP_KERNEL);
7438                        if (!arsta->tx_stats) {
7439                                ath10k_mac_dec_num_stations(arvif, sta);
7440                                ret = -ENOMEM;
7441                                goto exit;
7442                        }
7443                }
7444
7445                ret = ath10k_peer_create(ar, vif, sta, arvif->vdev_id,
7446                                         sta->addr, peer_type);
7447                if (ret) {
7448                        ath10k_warn(ar, "failed to add peer %pM for vdev %d when adding a new sta: %i\n",
7449                                    sta->addr, arvif->vdev_id, ret);
7450                        ath10k_mac_dec_num_stations(arvif, sta);
7451                        kfree(arsta->tx_stats);
7452                        goto exit;
7453                }
7454
7455                spin_lock_bh(&ar->data_lock);
7456
7457                peer = ath10k_peer_find(ar, arvif->vdev_id, sta->addr);
7458                if (!peer) {
7459                        ath10k_warn(ar, "failed to lookup peer %pM on vdev %i\n",
7460                                    vif->addr, arvif->vdev_id);
7461                        spin_unlock_bh(&ar->data_lock);
7462                        ath10k_peer_delete(ar, arvif->vdev_id, sta->addr);
7463                        ath10k_mac_dec_num_stations(arvif, sta);
7464                        kfree(arsta->tx_stats);
7465                        ret = -ENOENT;
7466                        goto exit;
7467                }
7468
7469                arsta->peer_id = find_first_bit(peer->peer_ids,
7470                                                ATH10K_MAX_NUM_PEER_IDS);
7471
7472                spin_unlock_bh(&ar->data_lock);
7473
7474                if (!sta->tdls)
7475                        goto exit;
7476
7477                ret = ath10k_wmi_update_fw_tdls_state(ar, arvif->vdev_id,
7478                                                      WMI_TDLS_ENABLE_ACTIVE);
7479                if (ret) {
7480                        ath10k_warn(ar, "failed to update fw tdls state on vdev %i: %i\n",
7481                                    arvif->vdev_id, ret);
7482                        ath10k_peer_delete(ar, arvif->vdev_id,
7483                                           sta->addr);
7484                        ath10k_mac_dec_num_stations(arvif, sta);
7485                        kfree(arsta->tx_stats);
7486                        goto exit;
7487                }
7488
7489                ret = ath10k_mac_tdls_peer_update(ar, arvif->vdev_id, sta,
7490                                                  WMI_TDLS_PEER_STATE_PEERING);
7491                if (ret) {
7492                        ath10k_warn(ar,
7493                                    "failed to update tdls peer %pM for vdev %d when adding a new sta: %i\n",
7494                                    sta->addr, arvif->vdev_id, ret);
7495                        ath10k_peer_delete(ar, arvif->vdev_id, sta->addr);
7496                        ath10k_mac_dec_num_stations(arvif, sta);
7497                        kfree(arsta->tx_stats);
7498
7499                        if (num_tdls_stations != 0)
7500                                goto exit;
7501                        ath10k_wmi_update_fw_tdls_state(ar, arvif->vdev_id,
7502                                                        WMI_TDLS_DISABLE);
7503                }
7504        } else if ((old_state == IEEE80211_STA_NONE &&
7505                    new_state == IEEE80211_STA_NOTEXIST)) {
7506                /*
7507                 * Existing station deletion.
7508                 */
7509                ath10k_dbg(ar, ATH10K_DBG_STA,
7510                           "mac vdev %d peer delete %pM sta %pK (sta gone)\n",
7511                           arvif->vdev_id, sta->addr, sta);
7512
7513                if (sta->tdls) {
7514                        ret = ath10k_mac_tdls_peer_update(ar, arvif->vdev_id,
7515                                                          sta,
7516                                                          WMI_TDLS_PEER_STATE_TEARDOWN);
7517                        if (ret)
7518                                ath10k_warn(ar, "failed to update tdls peer state for %pM state %d: %i\n",
7519                                            sta->addr,
7520                                            WMI_TDLS_PEER_STATE_TEARDOWN, ret);
7521                }
7522
7523                ret = ath10k_peer_delete(ar, arvif->vdev_id, sta->addr);
7524                if (ret)
7525                        ath10k_warn(ar, "failed to delete peer %pM for vdev %d: %i\n",
7526                                    sta->addr, arvif->vdev_id, ret);
7527
7528                ath10k_mac_dec_num_stations(arvif, sta);
7529
7530                spin_lock_bh(&ar->data_lock);
7531                for (i = 0; i < ARRAY_SIZE(ar->peer_map); i++) {
7532                        peer = ar->peer_map[i];
7533                        if (!peer)
7534                                continue;
7535
7536                        if (peer->sta == sta) {
7537                                ath10k_warn(ar, "found sta peer %pM (ptr %pK id %d) entry on vdev %i after it was supposedly removed\n",
7538                                            sta->addr, peer, i, arvif->vdev_id);
7539                                peer->sta = NULL;
7540
7541                                /* Clean up the peer object as well since we
7542                                 * must have failed to do this above.
7543                                 */
7544                                list_del(&peer->list);
7545                                ar->peer_map[i] = NULL;
7546                                kfree(peer);
7547                                ar->num_peers--;
7548                        }
7549                }
7550                spin_unlock_bh(&ar->data_lock);
7551
7552                if (ath10k_debug_is_extd_tx_stats_enabled(ar)) {
7553                        kfree(arsta->tx_stats);
7554                        arsta->tx_stats = NULL;
7555                }
7556
7557                for (i = 0; i < ARRAY_SIZE(sta->txq); i++)
7558                        ath10k_mac_txq_unref(ar, sta->txq[i]);
7559
7560                if (!sta->tdls)
7561                        goto exit;
7562
7563                if (ath10k_mac_tdls_vif_stations_count(hw, vif))
7564                        goto exit;
7565
7566                /* This was the last tdls peer in current vif */
7567                ret = ath10k_wmi_update_fw_tdls_state(ar, arvif->vdev_id,
7568                                                      WMI_TDLS_DISABLE);
7569                if (ret) {
7570                        ath10k_warn(ar, "failed to update fw tdls state on vdev %i: %i\n",
7571                                    arvif->vdev_id, ret);
7572                }
7573        } else if (old_state == IEEE80211_STA_AUTH &&
7574                   new_state == IEEE80211_STA_ASSOC &&
7575                   (vif->type == NL80211_IFTYPE_AP ||
7576                    vif->type == NL80211_IFTYPE_MESH_POINT ||
7577                    vif->type == NL80211_IFTYPE_ADHOC)) {
7578                /*
7579                 * New association.
7580                 */
7581                ath10k_dbg(ar, ATH10K_DBG_STA, "mac sta %pM associated\n",
7582                           sta->addr);
7583
7584                ret = ath10k_station_assoc(ar, vif, sta, false);
7585                if (ret)
7586                        ath10k_warn(ar, "failed to associate station %pM for vdev %i: %i\n",
7587                                    sta->addr, arvif->vdev_id, ret);
7588        } else if (old_state == IEEE80211_STA_ASSOC &&
7589                   new_state == IEEE80211_STA_AUTHORIZED &&
7590                   sta->tdls) {
7591                /*
7592                 * Tdls station authorized.
7593                 */
7594                ath10k_dbg(ar, ATH10K_DBG_STA, "mac tdls sta %pM authorized\n",
7595                           sta->addr);
7596
7597                ret = ath10k_station_assoc(ar, vif, sta, false);
7598                if (ret) {
7599                        ath10k_warn(ar, "failed to associate tdls station %pM for vdev %i: %i\n",
7600                                    sta->addr, arvif->vdev_id, ret);
7601                        goto exit;
7602                }
7603
7604                ret = ath10k_mac_tdls_peer_update(ar, arvif->vdev_id, sta,
7605                                                  WMI_TDLS_PEER_STATE_CONNECTED);
7606                if (ret)
7607                        ath10k_warn(ar, "failed to update tdls peer %pM for vdev %i: %i\n",
7608                                    sta->addr, arvif->vdev_id, ret);
7609        } else if (old_state == IEEE80211_STA_ASSOC &&
7610                    new_state == IEEE80211_STA_AUTH &&
7611                    (vif->type == NL80211_IFTYPE_AP ||
7612                     vif->type == NL80211_IFTYPE_MESH_POINT ||
7613                     vif->type == NL80211_IFTYPE_ADHOC)) {
7614                /*
7615                 * Disassociation.
7616                 */
7617                ath10k_dbg(ar, ATH10K_DBG_STA, "mac sta %pM disassociated\n",
7618                           sta->addr);
7619
7620                ret = ath10k_station_disassoc(ar, vif, sta);
7621                if (ret)
7622                        ath10k_warn(ar, "failed to disassociate station: %pM vdev %i: %i\n",
7623                                    sta->addr, arvif->vdev_id, ret);
7624        }
7625exit:
7626        mutex_unlock(&ar->conf_mutex);
7627        return ret;
7628}
7629
7630static int ath10k_conf_tx_uapsd(struct ath10k *ar, struct ieee80211_vif *vif,
7631                                u16 ac, bool enable)
7632{
7633        struct ath10k_vif *arvif = (void *)vif->drv_priv;
7634        struct wmi_sta_uapsd_auto_trig_arg arg = {};
7635        u32 prio = 0, acc = 0;
7636        u32 value = 0;
7637        int ret = 0;
7638
7639        lockdep_assert_held(&ar->conf_mutex);
7640
7641        if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
7642                return 0;
7643
7644        switch (ac) {
7645        case IEEE80211_AC_VO:
7646                value = WMI_STA_PS_UAPSD_AC3_DELIVERY_EN |
7647                        WMI_STA_PS_UAPSD_AC3_TRIGGER_EN;
7648                prio = 7;
7649                acc = 3;
7650                break;
7651        case IEEE80211_AC_VI:
7652                value = WMI_STA_PS_UAPSD_AC2_DELIVERY_EN |
7653                        WMI_STA_PS_UAPSD_AC2_TRIGGER_EN;
7654                prio = 5;
7655                acc = 2;
7656                break;
7657        case IEEE80211_AC_BE:
7658                value = WMI_STA_PS_UAPSD_AC1_DELIVERY_EN |
7659                        WMI_STA_PS_UAPSD_AC1_TRIGGER_EN;
7660                prio = 2;
7661                acc = 1;
7662                break;
7663        case IEEE80211_AC_BK:
7664                value = WMI_STA_PS_UAPSD_AC0_DELIVERY_EN |
7665                        WMI_STA_PS_UAPSD_AC0_TRIGGER_EN;
7666                prio = 0;
7667                acc = 0;
7668                break;
7669        }
7670
7671        if (enable)
7672                arvif->u.sta.uapsd |= value;
7673        else
7674                arvif->u.sta.uapsd &= ~value;
7675
7676        ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
7677                                          WMI_STA_PS_PARAM_UAPSD,
7678                                          arvif->u.sta.uapsd);
7679        if (ret) {
7680                ath10k_warn(ar, "failed to set uapsd params: %d\n", ret);
7681                goto exit;
7682        }
7683
7684        if (arvif->u.sta.uapsd)
7685                value = WMI_STA_PS_RX_WAKE_POLICY_POLL_UAPSD;
7686        else
7687                value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
7688
7689        ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
7690                                          WMI_STA_PS_PARAM_RX_WAKE_POLICY,
7691                                          value);
7692        if (ret)
7693                ath10k_warn(ar, "failed to set rx wake param: %d\n", ret);
7694
7695        ret = ath10k_mac_vif_recalc_ps_wake_threshold(arvif);
7696        if (ret) {
7697                ath10k_warn(ar, "failed to recalc ps wake threshold on vdev %i: %d\n",
7698                            arvif->vdev_id, ret);
7699                return ret;
7700        }
7701
7702        ret = ath10k_mac_vif_recalc_ps_poll_count(arvif);
7703        if (ret) {
7704                ath10k_warn(ar, "failed to recalc ps poll count on vdev %i: %d\n",
7705                            arvif->vdev_id, ret);
7706                return ret;
7707        }
7708
7709        if (test_bit(WMI_SERVICE_STA_UAPSD_BASIC_AUTO_TRIG, ar->wmi.svc_map) ||
7710            test_bit(WMI_SERVICE_STA_UAPSD_VAR_AUTO_TRIG, ar->wmi.svc_map)) {
7711                /* Only userspace can make an educated decision when to send
7712                 * trigger frame. The following effectively disables u-UAPSD
7713                 * autotrigger in firmware (which is enabled by default
7714                 * provided the autotrigger service is available).
7715                 */
7716
7717                arg.wmm_ac = acc;
7718                arg.user_priority = prio;
7719                arg.service_interval = 0;
7720                arg.suspend_interval = WMI_STA_UAPSD_MAX_INTERVAL_MSEC;
7721                arg.delay_interval = WMI_STA_UAPSD_MAX_INTERVAL_MSEC;
7722
7723                ret = ath10k_wmi_vdev_sta_uapsd(ar, arvif->vdev_id,
7724                                                arvif->bssid, &arg, 1);
7725                if (ret) {
7726                        ath10k_warn(ar, "failed to set uapsd auto trigger %d\n",
7727                                    ret);
7728                        return ret;
7729                }
7730        }
7731
7732exit:
7733        return ret;
7734}
7735
7736static int ath10k_conf_tx(struct ieee80211_hw *hw,
7737                          struct ieee80211_vif *vif, u16 ac,
7738                          const struct ieee80211_tx_queue_params *params)
7739{
7740        struct ath10k *ar = hw->priv;
7741        struct ath10k_vif *arvif = (void *)vif->drv_priv;
7742        struct wmi_wmm_params_arg *p = NULL;
7743        int ret;
7744
7745        mutex_lock(&ar->conf_mutex);
7746
7747        switch (ac) {
7748        case IEEE80211_AC_VO:
7749                p = &arvif->wmm_params.ac_vo;
7750                break;
7751        case IEEE80211_AC_VI:
7752                p = &arvif->wmm_params.ac_vi;
7753                break;
7754        case IEEE80211_AC_BE:
7755                p = &arvif->wmm_params.ac_be;
7756                break;
7757        case IEEE80211_AC_BK:
7758                p = &arvif->wmm_params.ac_bk;
7759                break;
7760        }
7761
7762        if (WARN_ON(!p)) {
7763                ret = -EINVAL;
7764                goto exit;
7765        }
7766
7767        p->cwmin = params->cw_min;
7768        p->cwmax = params->cw_max;
7769        p->aifs = params->aifs;
7770
7771        /*
7772         * The channel time duration programmed in the HW is in absolute
7773         * microseconds, while mac80211 gives the txop in units of
7774         * 32 microseconds.
7775         */
7776        p->txop = params->txop * 32;
7777
7778        if (ar->wmi.ops->gen_vdev_wmm_conf) {
7779                ret = ath10k_wmi_vdev_wmm_conf(ar, arvif->vdev_id,
7780                                               &arvif->wmm_params);
7781                if (ret) {
7782                        ath10k_warn(ar, "failed to set vdev wmm params on vdev %i: %d\n",
7783                                    arvif->vdev_id, ret);
7784                        goto exit;
7785                }
7786        } else {
7787                /* This won't work well with multi-interface cases but it's
7788                 * better than nothing.
7789                 */
7790                ret = ath10k_wmi_pdev_set_wmm_params(ar, &arvif->wmm_params);
7791                if (ret) {
7792                        ath10k_warn(ar, "failed to set wmm params: %d\n", ret);
7793                        goto exit;
7794                }
7795        }
7796
7797        ret = ath10k_conf_tx_uapsd(ar, vif, ac, params->uapsd);
7798        if (ret)
7799                ath10k_warn(ar, "failed to set sta uapsd: %d\n", ret);
7800
7801exit:
7802        mutex_unlock(&ar->conf_mutex);
7803        return ret;
7804}
7805
7806static int ath10k_remain_on_channel(struct ieee80211_hw *hw,
7807                                    struct ieee80211_vif *vif,
7808                                    struct ieee80211_channel *chan,
7809                                    int duration,
7810                                    enum ieee80211_roc_type type)
7811{
7812        struct ath10k *ar = hw->priv;
7813        struct ath10k_vif *arvif = (void *)vif->drv_priv;
7814        struct wmi_start_scan_arg arg;
7815        int ret = 0;
7816        u32 scan_time_msec;
7817
7818        mutex_lock(&ar->conf_mutex);
7819
7820        if (ath10k_mac_tdls_vif_stations_count(hw, vif) > 0) {
7821                ret = -EBUSY;
7822                goto exit;
7823        }
7824
7825        spin_lock_bh(&ar->data_lock);
7826        switch (ar->scan.state) {
7827        case ATH10K_SCAN_IDLE:
7828                reinit_completion(&ar->scan.started);
7829                reinit_completion(&ar->scan.completed);
7830                reinit_completion(&ar->scan.on_channel);
7831                ar->scan.state = ATH10K_SCAN_STARTING;
7832                ar->scan.is_roc = true;
7833                ar->scan.vdev_id = arvif->vdev_id;
7834                ar->scan.roc_freq = chan->center_freq;
7835                ar->scan.roc_notify = true;
7836                ret = 0;
7837                break;
7838        case ATH10K_SCAN_STARTING:
7839        case ATH10K_SCAN_RUNNING:
7840        case ATH10K_SCAN_ABORTING:
7841                ret = -EBUSY;
7842                break;
7843        }
7844        spin_unlock_bh(&ar->data_lock);
7845
7846        if (ret)
7847                goto exit;
7848
7849        scan_time_msec = ar->hw->wiphy->max_remain_on_channel_duration * 2;
7850
7851        memset(&arg, 0, sizeof(arg));
7852        ath10k_wmi_start_scan_init(ar, &arg);
7853        arg.vdev_id = arvif->vdev_id;
7854        arg.scan_id = ATH10K_SCAN_ID;
7855        arg.n_channels = 1;
7856        arg.channels[0] = chan->center_freq;
7857        arg.dwell_time_active = scan_time_msec;
7858        arg.dwell_time_passive = scan_time_msec;
7859        arg.max_scan_time = scan_time_msec;
7860        arg.scan_ctrl_flags |= WMI_SCAN_FLAG_PASSIVE;
7861        arg.scan_ctrl_flags |= WMI_SCAN_FILTER_PROBE_REQ;
7862        arg.burst_duration_ms = duration;
7863
7864        ret = ath10k_start_scan(ar, &arg);
7865        if (ret) {
7866                ath10k_warn(ar, "failed to start roc scan: %d\n", ret);
7867                spin_lock_bh(&ar->data_lock);
7868                ar->scan.state = ATH10K_SCAN_IDLE;
7869                spin_unlock_bh(&ar->data_lock);
7870                goto exit;
7871        }
7872
7873        ret = wait_for_completion_timeout(&ar->scan.on_channel, 3 * HZ);
7874        if (ret == 0) {
7875                ath10k_warn(ar, "failed to switch to channel for roc scan\n");
7876
7877                ret = ath10k_scan_stop(ar);
7878                if (ret)
7879                        ath10k_warn(ar, "failed to stop scan: %d\n", ret);
7880
7881                ret = -ETIMEDOUT;
7882                goto exit;
7883        }
7884
7885        ieee80211_queue_delayed_work(ar->hw, &ar->scan.timeout,
7886                                     msecs_to_jiffies(duration));
7887
7888        ret = 0;
7889exit:
7890        mutex_unlock(&ar->conf_mutex);
7891        return ret;
7892}
7893
7894static int ath10k_cancel_remain_on_channel(struct ieee80211_hw *hw,
7895                                           struct ieee80211_vif *vif)
7896{
7897        struct ath10k *ar = hw->priv;
7898
7899        mutex_lock(&ar->conf_mutex);
7900
7901        spin_lock_bh(&ar->data_lock);
7902        ar->scan.roc_notify = false;
7903        spin_unlock_bh(&ar->data_lock);
7904
7905        ath10k_scan_abort(ar);
7906
7907        mutex_unlock(&ar->conf_mutex);
7908
7909        cancel_delayed_work_sync(&ar->scan.timeout);
7910
7911        return 0;
7912}
7913
7914/*
7915 * Both RTS and Fragmentation threshold are interface-specific
7916 * in ath10k, but device-specific in mac80211.
7917 */
7918
7919static int ath10k_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
7920{
7921        struct ath10k *ar = hw->priv;
7922        struct ath10k_vif *arvif;
7923        int ret = 0;
7924
7925        mutex_lock(&ar->conf_mutex);
7926        list_for_each_entry(arvif, &ar->arvifs, list) {
7927                ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d rts threshold %d\n",
7928                           arvif->vdev_id, value);
7929
7930                ret = ath10k_mac_set_rts(arvif, value);
7931                if (ret) {
7932                        ath10k_warn(ar, "failed to set rts threshold for vdev %d: %d\n",
7933                                    arvif->vdev_id, ret);
7934                        break;
7935                }
7936        }
7937        mutex_unlock(&ar->conf_mutex);
7938
7939        return ret;
7940}
7941
7942static int ath10k_mac_op_set_frag_threshold(struct ieee80211_hw *hw, u32 value)
7943{
7944        /* Even though there's a WMI enum for fragmentation threshold no known
7945         * firmware actually implements it. Moreover it is not possible to rely
7946         * frame fragmentation to mac80211 because firmware clears the "more
7947         * fragments" bit in frame control making it impossible for remote
7948         * devices to reassemble frames.
7949         *
7950         * Hence implement a dummy callback just to say fragmentation isn't
7951         * supported. This effectively prevents mac80211 from doing frame
7952         * fragmentation in software.
7953         */
7954        return -EOPNOTSUPP;
7955}
7956
7957void ath10k_mac_wait_tx_complete(struct ath10k *ar)
7958{
7959        bool skip;
7960        long time_left;
7961
7962        /* mac80211 doesn't care if we really xmit queued frames or not
7963         * we'll collect those frames either way if we stop/delete vdevs
7964         */
7965
7966        if (ar->state == ATH10K_STATE_WEDGED)
7967                return;
7968
7969        time_left = wait_event_timeout(ar->htt.empty_tx_wq, ({
7970                        bool empty;
7971
7972                        spin_lock_bh(&ar->htt.tx_lock);
7973                        empty = (ar->htt.num_pending_tx == 0);
7974                        spin_unlock_bh(&ar->htt.tx_lock);
7975
7976                        skip = (ar->state == ATH10K_STATE_WEDGED) ||
7977                               test_bit(ATH10K_FLAG_CRASH_FLUSH,
7978                                        &ar->dev_flags);
7979
7980                        (empty || skip);
7981                }), ATH10K_FLUSH_TIMEOUT_HZ);
7982
7983        if (time_left == 0 || skip)
7984                ath10k_warn(ar, "failed to flush transmit queue (skip %i ar-state %i): %ld\n",
7985                            skip, ar->state, time_left);
7986}
7987
7988static void ath10k_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
7989                         u32 queues, bool drop)
7990{
7991        struct ath10k *ar = hw->priv;
7992        struct ath10k_vif *arvif;
7993        u32 bitmap;
7994
7995        if (drop) {
7996                if (vif && vif->type == NL80211_IFTYPE_STATION) {
7997                        bitmap = ~(1 << WMI_MGMT_TID);
7998                        list_for_each_entry(arvif, &ar->arvifs, list) {
7999                                if (arvif->vdev_type == WMI_VDEV_TYPE_STA)
8000                                        ath10k_wmi_peer_flush(ar, arvif->vdev_id,
8001                                                              arvif->bssid, bitmap);
8002                        }
8003                        ath10k_htt_flush_tx(&ar->htt);
8004                }
8005                return;
8006        }
8007
8008        mutex_lock(&ar->conf_mutex);
8009        ath10k_mac_wait_tx_complete(ar);
8010        mutex_unlock(&ar->conf_mutex);
8011}
8012
8013/* TODO: Implement this function properly
8014 * For now it is needed to reply to Probe Requests in IBSS mode.
8015 * Propably we need this information from FW.
8016 */
8017static int ath10k_tx_last_beacon(struct ieee80211_hw *hw)
8018{
8019        return 1;
8020}
8021
8022static void ath10k_reconfig_complete(struct ieee80211_hw *hw,
8023                                     enum ieee80211_reconfig_type reconfig_type)
8024{
8025        struct ath10k *ar = hw->priv;
8026
8027        if (reconfig_type != IEEE80211_RECONFIG_TYPE_RESTART)
8028                return;
8029
8030        mutex_lock(&ar->conf_mutex);
8031
8032        /* If device failed to restart it will be in a different state, e.g.
8033         * ATH10K_STATE_WEDGED
8034         */
8035        if (ar->state == ATH10K_STATE_RESTARTED) {
8036                ath10k_info(ar, "device successfully recovered\n");
8037                ar->state = ATH10K_STATE_ON;
8038                ieee80211_wake_queues(ar->hw);
8039                clear_bit(ATH10K_FLAG_RESTARTING, &ar->dev_flags);
8040        }
8041
8042        mutex_unlock(&ar->conf_mutex);
8043}
8044
8045static void
8046ath10k_mac_update_bss_chan_survey(struct ath10k *ar,
8047                                  struct ieee80211_channel *channel)
8048{
8049        int ret;
8050        enum wmi_bss_survey_req_type type = WMI_BSS_SURVEY_REQ_TYPE_READ;
8051
8052        lockdep_assert_held(&ar->conf_mutex);
8053
8054        if (!test_bit(WMI_SERVICE_BSS_CHANNEL_INFO_64, ar->wmi.svc_map) ||
8055            (ar->rx_channel != channel))
8056                return;
8057
8058        if (ar->scan.state != ATH10K_SCAN_IDLE) {
8059                ath10k_dbg(ar, ATH10K_DBG_MAC, "ignoring bss chan info request while scanning..\n");
8060                return;
8061        }
8062
8063        reinit_completion(&ar->bss_survey_done);
8064
8065        ret = ath10k_wmi_pdev_bss_chan_info_request(ar, type);
8066        if (ret) {
8067                ath10k_warn(ar, "failed to send pdev bss chan info request\n");
8068                return;
8069        }
8070
8071        ret = wait_for_completion_timeout(&ar->bss_survey_done, 3 * HZ);
8072        if (!ret) {
8073                ath10k_warn(ar, "bss channel survey timed out\n");
8074                return;
8075        }
8076}
8077
8078static int ath10k_get_survey(struct ieee80211_hw *hw, int idx,
8079                             struct survey_info *survey)
8080{
8081        struct ath10k *ar = hw->priv;
8082        struct ieee80211_supported_band *sband;
8083        struct survey_info *ar_survey = &ar->survey[idx];
8084        int ret = 0;
8085
8086        mutex_lock(&ar->conf_mutex);
8087
8088        sband = hw->wiphy->bands[NL80211_BAND_2GHZ];
8089        if (sband && idx >= sband->n_channels) {
8090                idx -= sband->n_channels;
8091                sband = NULL;
8092        }
8093
8094        if (!sband)
8095                sband = hw->wiphy->bands[NL80211_BAND_5GHZ];
8096
8097        if (!sband || idx >= sband->n_channels) {
8098                ret = -ENOENT;
8099                goto exit;
8100        }
8101
8102        ath10k_mac_update_bss_chan_survey(ar, &sband->channels[idx]);
8103
8104        spin_lock_bh(&ar->data_lock);
8105        memcpy(survey, ar_survey, sizeof(*survey));
8106        spin_unlock_bh(&ar->data_lock);
8107
8108        survey->channel = &sband->channels[idx];
8109
8110        if (ar->rx_channel == survey->channel)
8111                survey->filled |= SURVEY_INFO_IN_USE;
8112
8113exit:
8114        mutex_unlock(&ar->conf_mutex);
8115        return ret;
8116}
8117
8118static bool
8119ath10k_mac_bitrate_mask_get_single_nss(struct ath10k *ar,
8120                                       enum nl80211_band band,
8121                                       const struct cfg80211_bitrate_mask *mask,
8122                                       int *nss)
8123{
8124        struct ieee80211_supported_band *sband = &ar->mac.sbands[band];
8125        u16 vht_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
8126        u8 ht_nss_mask = 0;
8127        u8 vht_nss_mask = 0;
8128        int i;
8129
8130        if (mask->control[band].legacy)
8131                return false;
8132
8133        for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++) {
8134                if (mask->control[band].ht_mcs[i] == 0)
8135                        continue;
8136                else if (mask->control[band].ht_mcs[i] ==
8137                         sband->ht_cap.mcs.rx_mask[i])
8138                        ht_nss_mask |= BIT(i);
8139                else
8140                        return false;
8141        }
8142
8143        for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
8144                if (mask->control[band].vht_mcs[i] == 0)
8145                        continue;
8146                else if (mask->control[band].vht_mcs[i] ==
8147                         ath10k_mac_get_max_vht_mcs_map(vht_mcs_map, i))
8148                        vht_nss_mask |= BIT(i);
8149                else
8150                        return false;
8151        }
8152
8153        if (ht_nss_mask != vht_nss_mask)
8154                return false;
8155
8156        if (ht_nss_mask == 0)
8157                return false;
8158
8159        if (BIT(fls(ht_nss_mask)) - 1 != ht_nss_mask)
8160                return false;
8161
8162        *nss = fls(ht_nss_mask);
8163
8164        return true;
8165}
8166
8167static int ath10k_mac_set_fixed_rate_params(struct ath10k_vif *arvif,
8168                                            u8 rate, u8 nss, u8 sgi, u8 ldpc)
8169{
8170        struct ath10k *ar = arvif->ar;
8171        u32 vdev_param;
8172        int ret;
8173
8174        lockdep_assert_held(&ar->conf_mutex);
8175
8176        ath10k_dbg(ar, ATH10K_DBG_MAC, "mac set fixed rate params vdev %i rate 0x%02x nss %u sgi %u\n",
8177                   arvif->vdev_id, rate, nss, sgi);
8178
8179        vdev_param = ar->wmi.vdev_param->fixed_rate;
8180        ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, rate);
8181        if (ret) {
8182                ath10k_warn(ar, "failed to set fixed rate param 0x%02x: %d\n",
8183                            rate, ret);
8184                return ret;
8185        }
8186
8187        vdev_param = ar->wmi.vdev_param->nss;
8188        ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, nss);
8189        if (ret) {
8190                ath10k_warn(ar, "failed to set nss param %d: %d\n", nss, ret);
8191                return ret;
8192        }
8193
8194        vdev_param = ar->wmi.vdev_param->sgi;
8195        ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, sgi);
8196        if (ret) {
8197                ath10k_warn(ar, "failed to set sgi param %d: %d\n", sgi, ret);
8198                return ret;
8199        }
8200
8201        vdev_param = ar->wmi.vdev_param->ldpc;
8202        ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, ldpc);
8203        if (ret) {
8204                ath10k_warn(ar, "failed to set ldpc param %d: %d\n", ldpc, ret);
8205                return ret;
8206        }
8207
8208        return 0;
8209}
8210
8211static bool
8212ath10k_mac_can_set_bitrate_mask(struct ath10k *ar,
8213                                enum nl80211_band band,
8214                                const struct cfg80211_bitrate_mask *mask,
8215                                bool allow_pfr)
8216{
8217        int i;
8218        u16 vht_mcs;
8219
8220        /* Due to firmware limitation in WMI_PEER_ASSOC_CMDID it is impossible
8221         * to express all VHT MCS rate masks. Effectively only the following
8222         * ranges can be used: none, 0-7, 0-8 and 0-9.
8223         */
8224        for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
8225                vht_mcs = mask->control[band].vht_mcs[i];
8226
8227                switch (vht_mcs) {
8228                case 0:
8229                case BIT(8) - 1:
8230                case BIT(9) - 1:
8231                case BIT(10) - 1:
8232                        break;
8233                default:
8234                        if (!allow_pfr)
8235                                ath10k_warn(ar, "refusing bitrate mask with missing 0-7 VHT MCS rates\n");
8236                        return false;
8237                }
8238        }
8239
8240        return true;
8241}
8242
8243static bool ath10k_mac_set_vht_bitrate_mask_fixup(struct ath10k *ar,
8244                                                  struct ath10k_vif *arvif,
8245                                                  struct ieee80211_sta *sta)
8246{
8247        int err;
8248        u8 rate = arvif->vht_pfr;
8249
8250        /* skip non vht and multiple rate peers */
8251        if (!sta->vht_cap.vht_supported || arvif->vht_num_rates != 1)
8252                return false;
8253
8254        err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
8255                                        WMI_PEER_PARAM_FIXED_RATE, rate);
8256        if (err)
8257                ath10k_warn(ar, "failed to enable STA %pM peer fixed rate: %d\n",
8258                            sta->addr, err);
8259
8260        return true;
8261}
8262
8263static void ath10k_mac_set_bitrate_mask_iter(void *data,
8264                                             struct ieee80211_sta *sta)
8265{
8266        struct ath10k_vif *arvif = data;
8267        struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
8268        struct ath10k *ar = arvif->ar;
8269
8270        if (arsta->arvif != arvif)
8271                return;
8272
8273        if (ath10k_mac_set_vht_bitrate_mask_fixup(ar, arvif, sta))
8274                return;
8275
8276        spin_lock_bh(&ar->data_lock);
8277        arsta->changed |= IEEE80211_RC_SUPP_RATES_CHANGED;
8278        spin_unlock_bh(&ar->data_lock);
8279
8280        ieee80211_queue_work(ar->hw, &arsta->update_wk);
8281}
8282
8283static void ath10k_mac_clr_bitrate_mask_iter(void *data,
8284                                             struct ieee80211_sta *sta)
8285{
8286        struct ath10k_vif *arvif = data;
8287        struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
8288        struct ath10k *ar = arvif->ar;
8289        int err;
8290
8291        /* clear vht peers only */
8292        if (arsta->arvif != arvif || !sta->vht_cap.vht_supported)
8293                return;
8294
8295        err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
8296                                        WMI_PEER_PARAM_FIXED_RATE,
8297                                        WMI_FIXED_RATE_NONE);
8298        if (err)
8299                ath10k_warn(ar, "failed to clear STA %pM peer fixed rate: %d\n",
8300                            sta->addr, err);
8301}
8302
8303static int ath10k_mac_op_set_bitrate_mask(struct ieee80211_hw *hw,
8304                                          struct ieee80211_vif *vif,
8305                                          const struct cfg80211_bitrate_mask *mask)
8306{
8307        struct ath10k_vif *arvif = (void *)vif->drv_priv;
8308        struct cfg80211_chan_def def;
8309        struct ath10k *ar = arvif->ar;
8310        enum nl80211_band band;
8311        const u8 *ht_mcs_mask;
8312        const u16 *vht_mcs_mask;
8313        u8 rate;
8314        u8 nss;
8315        u8 sgi;
8316        u8 ldpc;
8317        int single_nss;
8318        int ret;
8319        int vht_num_rates, allow_pfr;
8320        u8 vht_pfr;
8321        bool update_bitrate_mask = true;
8322
8323        if (ath10k_mac_vif_chan(vif, &def))
8324                return -EPERM;
8325
8326        band = def.chan->band;
8327        ht_mcs_mask = mask->control[band].ht_mcs;
8328        vht_mcs_mask = mask->control[band].vht_mcs;
8329        ldpc = !!(ar->ht_cap_info & WMI_HT_CAP_LDPC);
8330
8331        sgi = mask->control[band].gi;
8332        if (sgi == NL80211_TXRATE_FORCE_LGI)
8333                return -EINVAL;
8334
8335        allow_pfr = test_bit(ATH10K_FW_FEATURE_PEER_FIXED_RATE,
8336                             ar->normal_mode_fw.fw_file.fw_features);
8337        if (allow_pfr) {
8338                mutex_lock(&ar->conf_mutex);
8339                ieee80211_iterate_stations_atomic(ar->hw,
8340                                                  ath10k_mac_clr_bitrate_mask_iter,
8341                                                  arvif);
8342                mutex_unlock(&ar->conf_mutex);
8343        }
8344
8345        if (ath10k_mac_bitrate_mask_has_single_rate(ar, band, mask,
8346                                                    &vht_num_rates)) {
8347                ret = ath10k_mac_bitrate_mask_get_single_rate(ar, band, mask,
8348                                                              &rate, &nss,
8349                                                              false);
8350                if (ret) {
8351                        ath10k_warn(ar, "failed to get single rate for vdev %i: %d\n",
8352                                    arvif->vdev_id, ret);
8353                        return ret;
8354                }
8355        } else if (ath10k_mac_bitrate_mask_get_single_nss(ar, band, mask,
8356                                                          &single_nss)) {
8357                rate = WMI_FIXED_RATE_NONE;
8358                nss = single_nss;
8359        } else {
8360                rate = WMI_FIXED_RATE_NONE;
8361                nss = min(ar->num_rf_chains,
8362                          max(ath10k_mac_max_ht_nss(ht_mcs_mask),
8363                              ath10k_mac_max_vht_nss(vht_mcs_mask)));
8364
8365                if (!ath10k_mac_can_set_bitrate_mask(ar, band, mask,
8366                                                     allow_pfr)) {
8367                        u8 vht_nss;
8368
8369                        if (!allow_pfr || vht_num_rates != 1)
8370                                return -EINVAL;
8371
8372                        /* Reach here, firmware supports peer fixed rate and has
8373                         * single vht rate, and don't update vif birate_mask, as
8374                         * the rate only for specific peer.
8375                         */
8376                        ath10k_mac_bitrate_mask_get_single_rate(ar, band, mask,
8377                                                                &vht_pfr,
8378                                                                &vht_nss,
8379                                                                true);
8380                        update_bitrate_mask = false;
8381                } else {
8382                        vht_pfr = 0;
8383                }
8384
8385                mutex_lock(&ar->conf_mutex);
8386
8387                if (update_bitrate_mask)
8388                        arvif->bitrate_mask = *mask;
8389                arvif->vht_num_rates = vht_num_rates;
8390                arvif->vht_pfr = vht_pfr;
8391                ieee80211_iterate_stations_atomic(ar->hw,
8392                                                  ath10k_mac_set_bitrate_mask_iter,
8393                                                  arvif);
8394
8395                mutex_unlock(&ar->conf_mutex);
8396        }
8397
8398        mutex_lock(&ar->conf_mutex);
8399
8400        ret = ath10k_mac_set_fixed_rate_params(arvif, rate, nss, sgi, ldpc);
8401        if (ret) {
8402                ath10k_warn(ar, "failed to set fixed rate params on vdev %i: %d\n",
8403                            arvif->vdev_id, ret);
8404                goto exit;
8405        }
8406
8407exit:
8408        mutex_unlock(&ar->conf_mutex);
8409
8410        return ret;
8411}
8412
8413static void ath10k_sta_rc_update(struct ieee80211_hw *hw,
8414                                 struct ieee80211_vif *vif,
8415                                 struct ieee80211_sta *sta,
8416                                 u32 changed)
8417{
8418        struct ath10k *ar = hw->priv;
8419        struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
8420        struct ath10k_vif *arvif = (void *)vif->drv_priv;
8421        struct ath10k_peer *peer;
8422        u32 bw, smps;
8423
8424        spin_lock_bh(&ar->data_lock);
8425
8426        peer = ath10k_peer_find(ar, arvif->vdev_id, sta->addr);
8427        if (!peer) {
8428                spin_unlock_bh(&ar->data_lock);
8429                ath10k_warn(ar, "mac sta rc update failed to find peer %pM on vdev %i\n",
8430                            sta->addr, arvif->vdev_id);
8431                return;
8432        }
8433
8434        ath10k_dbg(ar, ATH10K_DBG_STA,
8435                   "mac sta rc update for %pM changed %08x bw %d nss %d smps %d\n",
8436                   sta->addr, changed, sta->bandwidth, sta->rx_nss,
8437                   sta->smps_mode);
8438
8439        if (changed & IEEE80211_RC_BW_CHANGED) {
8440                bw = WMI_PEER_CHWIDTH_20MHZ;
8441
8442                switch (sta->bandwidth) {
8443                case IEEE80211_STA_RX_BW_20:
8444                        bw = WMI_PEER_CHWIDTH_20MHZ;
8445                        break;
8446                case IEEE80211_STA_RX_BW_40:
8447                        bw = WMI_PEER_CHWIDTH_40MHZ;
8448                        break;
8449                case IEEE80211_STA_RX_BW_80:
8450                        bw = WMI_PEER_CHWIDTH_80MHZ;
8451                        break;
8452                case IEEE80211_STA_RX_BW_160:
8453                        bw = WMI_PEER_CHWIDTH_160MHZ;
8454                        break;
8455                default:
8456                        ath10k_warn(ar, "Invalid bandwidth %d in rc update for %pM\n",
8457                                    sta->bandwidth, sta->addr);
8458                        bw = WMI_PEER_CHWIDTH_20MHZ;
8459                        break;
8460                }
8461
8462                arsta->bw = bw;
8463        }
8464
8465        if (changed & IEEE80211_RC_NSS_CHANGED)
8466                arsta->nss = sta->rx_nss;
8467
8468        if (changed & IEEE80211_RC_SMPS_CHANGED) {
8469                smps = WMI_PEER_SMPS_PS_NONE;
8470
8471                switch (sta->smps_mode) {
8472                case IEEE80211_SMPS_AUTOMATIC:
8473                case IEEE80211_SMPS_OFF:
8474                        smps = WMI_PEER_SMPS_PS_NONE;
8475                        break;
8476                case IEEE80211_SMPS_STATIC:
8477                        smps = WMI_PEER_SMPS_STATIC;
8478                        break;
8479                case IEEE80211_SMPS_DYNAMIC:
8480                        smps = WMI_PEER_SMPS_DYNAMIC;
8481                        break;
8482                case IEEE80211_SMPS_NUM_MODES:
8483                        ath10k_warn(ar, "Invalid smps %d in sta rc update for %pM\n",
8484                                    sta->smps_mode, sta->addr);
8485                        smps = WMI_PEER_SMPS_PS_NONE;
8486                        break;
8487                }
8488
8489                arsta->smps = smps;
8490        }
8491
8492        arsta->changed |= changed;
8493
8494        spin_unlock_bh(&ar->data_lock);
8495
8496        ieee80211_queue_work(hw, &arsta->update_wk);
8497}
8498
8499static void ath10k_offset_tsf(struct ieee80211_hw *hw,
8500                              struct ieee80211_vif *vif, s64 tsf_offset)
8501{
8502        struct ath10k *ar = hw->priv;
8503        struct ath10k_vif *arvif = (void *)vif->drv_priv;
8504        u32 offset, vdev_param;
8505        int ret;
8506
8507        if (tsf_offset < 0) {
8508                vdev_param = ar->wmi.vdev_param->dec_tsf;
8509                offset = -tsf_offset;
8510        } else {
8511                vdev_param = ar->wmi.vdev_param->inc_tsf;
8512                offset = tsf_offset;
8513        }
8514
8515        ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
8516                                        vdev_param, offset);
8517
8518        if (ret && ret != -EOPNOTSUPP)
8519                ath10k_warn(ar, "failed to set tsf offset %d cmd %d: %d\n",
8520                            offset, vdev_param, ret);
8521}
8522
8523static int ath10k_ampdu_action(struct ieee80211_hw *hw,
8524                               struct ieee80211_vif *vif,
8525                               struct ieee80211_ampdu_params *params)
8526{
8527        struct ath10k *ar = hw->priv;
8528        struct ath10k_vif *arvif = (void *)vif->drv_priv;
8529        struct ieee80211_sta *sta = params->sta;
8530        enum ieee80211_ampdu_mlme_action action = params->action;
8531        u16 tid = params->tid;
8532
8533        ath10k_dbg(ar, ATH10K_DBG_MAC, "mac ampdu vdev_id %i sta %pM tid %u action %d\n",
8534                   arvif->vdev_id, sta->addr, tid, action);
8535
8536        switch (action) {
8537        case IEEE80211_AMPDU_RX_START:
8538        case IEEE80211_AMPDU_RX_STOP:
8539                /* HTT AddBa/DelBa events trigger mac80211 Rx BA session
8540                 * creation/removal. Do we need to verify this?
8541                 */
8542                return 0;
8543        case IEEE80211_AMPDU_TX_START:
8544        case IEEE80211_AMPDU_TX_STOP_CONT:
8545        case IEEE80211_AMPDU_TX_STOP_FLUSH:
8546        case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
8547        case IEEE80211_AMPDU_TX_OPERATIONAL:
8548                /* Firmware offloads Tx aggregation entirely so deny mac80211
8549                 * Tx aggregation requests.
8550                 */
8551                return -EOPNOTSUPP;
8552        }
8553
8554        return -EINVAL;
8555}
8556
8557static void
8558ath10k_mac_update_rx_channel(struct ath10k *ar,
8559                             struct ieee80211_chanctx_conf *ctx,
8560                             struct ieee80211_vif_chanctx_switch *vifs,
8561                             int n_vifs)
8562{
8563        struct cfg80211_chan_def *def = NULL;
8564
8565        /* Both locks are required because ar->rx_channel is modified. This
8566         * allows readers to hold either lock.
8567         */
8568        lockdep_assert_held(&ar->conf_mutex);
8569        lockdep_assert_held(&ar->data_lock);
8570
8571        WARN_ON(ctx && vifs);
8572        WARN_ON(vifs && !n_vifs);
8573
8574        /* FIXME: Sort of an optimization and a workaround. Peers and vifs are
8575         * on a linked list now. Doing a lookup peer -> vif -> chanctx for each
8576         * ppdu on Rx may reduce performance on low-end systems. It should be
8577         * possible to make tables/hashmaps to speed the lookup up (be vary of
8578         * cpu data cache lines though regarding sizes) but to keep the initial
8579         * implementation simple and less intrusive fallback to the slow lookup
8580         * only for multi-channel cases. Single-channel cases will remain to
8581         * use the old channel derival and thus performance should not be
8582         * affected much.
8583         */
8584        rcu_read_lock();
8585        if (!ctx && ath10k_mac_num_chanctxs(ar) == 1) {
8586                ieee80211_iter_chan_contexts_atomic(ar->hw,
8587                                                    ath10k_mac_get_any_chandef_iter,
8588                                                    &def);
8589
8590                if (vifs)
8591                        def = &vifs[0].new_ctx->def;
8592
8593                ar->rx_channel = def->chan;
8594        } else if ((ctx && ath10k_mac_num_chanctxs(ar) == 0) ||
8595                   (ctx && (ar->state == ATH10K_STATE_RESTARTED))) {
8596                /* During driver restart due to firmware assert, since mac80211
8597                 * already has valid channel context for given radio, channel
8598                 * context iteration return num_chanctx > 0. So fix rx_channel
8599                 * when restart is in progress.
8600                 */
8601                ar->rx_channel = ctx->def.chan;
8602        } else {
8603                ar->rx_channel = NULL;
8604        }
8605        rcu_read_unlock();
8606}
8607
8608static void
8609ath10k_mac_update_vif_chan(struct ath10k *ar,
8610                           struct ieee80211_vif_chanctx_switch *vifs,
8611                           int n_vifs)
8612{
8613        struct ath10k_vif *arvif;
8614        int ret;
8615        int i;
8616
8617        lockdep_assert_held(&ar->conf_mutex);
8618
8619        /* First stop monitor interface. Some FW versions crash if there's a
8620         * lone monitor interface.
8621         */
8622        if (ar->monitor_started)
8623                ath10k_monitor_stop(ar);
8624
8625        for (i = 0; i < n_vifs; i++) {
8626                arvif = (void *)vifs[i].vif->drv_priv;
8627
8628                ath10k_dbg(ar, ATH10K_DBG_MAC,
8629                           "mac chanctx switch vdev_id %i freq %u->%u width %d->%d\n",
8630                           arvif->vdev_id,
8631                           vifs[i].old_ctx->def.chan->center_freq,
8632                           vifs[i].new_ctx->def.chan->center_freq,
8633                           vifs[i].old_ctx->def.width,
8634                           vifs[i].new_ctx->def.width);
8635
8636                if (WARN_ON(!arvif->is_started))
8637                        continue;
8638
8639                if (WARN_ON(!arvif->is_up))
8640                        continue;
8641
8642                ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
8643                if (ret) {
8644                        ath10k_warn(ar, "failed to down vdev %d: %d\n",
8645                                    arvif->vdev_id, ret);
8646                        continue;
8647                }
8648        }
8649
8650        /* All relevant vdevs are downed and associated channel resources
8651         * should be available for the channel switch now.
8652         */
8653
8654        spin_lock_bh(&ar->data_lock);
8655        ath10k_mac_update_rx_channel(ar, NULL, vifs, n_vifs);
8656        spin_unlock_bh(&ar->data_lock);
8657
8658        for (i = 0; i < n_vifs; i++) {
8659                arvif = (void *)vifs[i].vif->drv_priv;
8660
8661                if (WARN_ON(!arvif->is_started))
8662                        continue;
8663
8664                if (WARN_ON(!arvif->is_up))
8665                        continue;
8666
8667                ret = ath10k_mac_setup_bcn_tmpl(arvif);
8668                if (ret)
8669                        ath10k_warn(ar, "failed to update bcn tmpl during csa: %d\n",
8670                                    ret);
8671
8672                ret = ath10k_mac_setup_prb_tmpl(arvif);
8673                if (ret)
8674                        ath10k_warn(ar, "failed to update prb tmpl during csa: %d\n",
8675                                    ret);
8676
8677                ret = ath10k_vdev_restart(arvif, &vifs[i].new_ctx->def);
8678                if (ret) {
8679                        ath10k_warn(ar, "failed to restart vdev %d: %d\n",
8680                                    arvif->vdev_id, ret);
8681                        continue;
8682                }
8683
8684                ret = ath10k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
8685                                         arvif->bssid);
8686                if (ret) {
8687                        ath10k_warn(ar, "failed to bring vdev up %d: %d\n",
8688                                    arvif->vdev_id, ret);
8689                        continue;
8690                }
8691        }
8692
8693        ath10k_monitor_recalc(ar);
8694}
8695
8696static int
8697ath10k_mac_op_add_chanctx(struct ieee80211_hw *hw,
8698                          struct ieee80211_chanctx_conf *ctx)
8699{
8700        struct ath10k *ar = hw->priv;
8701
8702        ath10k_dbg(ar, ATH10K_DBG_MAC,
8703                   "mac chanctx add freq %u width %d ptr %pK\n",
8704                   ctx->def.chan->center_freq, ctx->def.width, ctx);
8705
8706        mutex_lock(&ar->conf_mutex);
8707
8708        spin_lock_bh(&ar->data_lock);
8709        ath10k_mac_update_rx_channel(ar, ctx, NULL, 0);
8710        spin_unlock_bh(&ar->data_lock);
8711
8712        ath10k_recalc_radar_detection(ar);
8713        ath10k_monitor_recalc(ar);
8714
8715        mutex_unlock(&ar->conf_mutex);
8716
8717        return 0;
8718}
8719
8720static void
8721ath10k_mac_op_remove_chanctx(struct ieee80211_hw *hw,
8722                             struct ieee80211_chanctx_conf *ctx)
8723{
8724        struct ath10k *ar = hw->priv;
8725
8726        ath10k_dbg(ar, ATH10K_DBG_MAC,
8727                   "mac chanctx remove freq %u width %d ptr %pK\n",
8728                   ctx->def.chan->center_freq, ctx->def.width, ctx);
8729
8730        mutex_lock(&ar->conf_mutex);
8731
8732        spin_lock_bh(&ar->data_lock);
8733        ath10k_mac_update_rx_channel(ar, NULL, NULL, 0);
8734        spin_unlock_bh(&ar->data_lock);
8735
8736        ath10k_recalc_radar_detection(ar);
8737        ath10k_monitor_recalc(ar);
8738
8739        mutex_unlock(&ar->conf_mutex);
8740}
8741
8742struct ath10k_mac_change_chanctx_arg {
8743        struct ieee80211_chanctx_conf *ctx;
8744        struct ieee80211_vif_chanctx_switch *vifs;
8745        int n_vifs;
8746        int next_vif;
8747};
8748
8749static void
8750ath10k_mac_change_chanctx_cnt_iter(void *data, u8 *mac,
8751                                   struct ieee80211_vif *vif)
8752{
8753        struct ath10k_mac_change_chanctx_arg *arg = data;
8754
8755        if (rcu_access_pointer(vif->chanctx_conf) != arg->ctx)
8756                return;
8757
8758        arg->n_vifs++;
8759}
8760
8761static void
8762ath10k_mac_change_chanctx_fill_iter(void *data, u8 *mac,
8763                                    struct ieee80211_vif *vif)
8764{
8765        struct ath10k_mac_change_chanctx_arg *arg = data;
8766        struct ieee80211_chanctx_conf *ctx;
8767
8768        ctx = rcu_access_pointer(vif->chanctx_conf);
8769        if (ctx != arg->ctx)
8770                return;
8771
8772        if (WARN_ON(arg->next_vif == arg->n_vifs))
8773                return;
8774
8775        arg->vifs[arg->next_vif].vif = vif;
8776        arg->vifs[arg->next_vif].old_ctx = ctx;
8777        arg->vifs[arg->next_vif].new_ctx = ctx;
8778        arg->next_vif++;
8779}
8780
8781static void
8782ath10k_mac_op_change_chanctx(struct ieee80211_hw *hw,
8783                             struct ieee80211_chanctx_conf *ctx,
8784                             u32 changed)
8785{
8786        struct ath10k *ar = hw->priv;
8787        struct ath10k_mac_change_chanctx_arg arg = { .ctx = ctx };
8788
8789        mutex_lock(&ar->conf_mutex);
8790
8791        ath10k_dbg(ar, ATH10K_DBG_MAC,
8792                   "mac chanctx change freq %u width %d ptr %pK changed %x\n",
8793                   ctx->def.chan->center_freq, ctx->def.width, ctx, changed);
8794
8795        /* This shouldn't really happen because channel switching should use
8796         * switch_vif_chanctx().
8797         */
8798        if (WARN_ON(changed & IEEE80211_CHANCTX_CHANGE_CHANNEL))
8799                goto unlock;
8800
8801        if (changed & IEEE80211_CHANCTX_CHANGE_WIDTH) {
8802                ieee80211_iterate_active_interfaces_atomic(
8803                                        hw,
8804                                        ATH10K_ITER_NORMAL_FLAGS,
8805                                        ath10k_mac_change_chanctx_cnt_iter,
8806                                        &arg);
8807                if (arg.n_vifs == 0)
8808                        goto radar;
8809
8810                arg.vifs = kcalloc(arg.n_vifs, sizeof(arg.vifs[0]),
8811                                   GFP_KERNEL);
8812                if (!arg.vifs)
8813                        goto radar;
8814
8815                ieee80211_iterate_active_interfaces_atomic(
8816                                        hw,
8817                                        ATH10K_ITER_NORMAL_FLAGS,
8818                                        ath10k_mac_change_chanctx_fill_iter,
8819                                        &arg);
8820                ath10k_mac_update_vif_chan(ar, arg.vifs, arg.n_vifs);
8821                kfree(arg.vifs);
8822        }
8823
8824radar:
8825        ath10k_recalc_radar_detection(ar);
8826
8827        /* FIXME: How to configure Rx chains properly? */
8828
8829        /* No other actions are actually necessary. Firmware maintains channel
8830         * definitions per vdev internally and there's no host-side channel
8831         * context abstraction to configure, e.g. channel width.
8832         */
8833
8834unlock:
8835        mutex_unlock(&ar->conf_mutex);
8836}
8837
8838static int
8839ath10k_mac_op_assign_vif_chanctx(struct ieee80211_hw *hw,
8840                                 struct ieee80211_vif *vif,
8841                                 struct ieee80211_chanctx_conf *ctx)
8842{
8843        struct ath10k *ar = hw->priv;
8844        struct ath10k_vif *arvif = (void *)vif->drv_priv;
8845        int ret;
8846
8847        mutex_lock(&ar->conf_mutex);
8848
8849        ath10k_dbg(ar, ATH10K_DBG_MAC,
8850                   "mac chanctx assign ptr %pK vdev_id %i\n",
8851                   ctx, arvif->vdev_id);
8852
8853        if (WARN_ON(arvif->is_started)) {
8854                mutex_unlock(&ar->conf_mutex);
8855                return -EBUSY;
8856        }
8857
8858        ret = ath10k_vdev_start(arvif, &ctx->def);
8859        if (ret) {
8860                ath10k_warn(ar, "failed to start vdev %i addr %pM on freq %d: %d\n",
8861                            arvif->vdev_id, vif->addr,
8862                            ctx->def.chan->center_freq, ret);
8863                goto err;
8864        }
8865
8866        arvif->is_started = true;
8867
8868        ret = ath10k_mac_vif_setup_ps(arvif);
8869        if (ret) {
8870                ath10k_warn(ar, "failed to update vdev %i ps: %d\n",
8871                            arvif->vdev_id, ret);
8872                goto err_stop;
8873        }
8874
8875        if (vif->type == NL80211_IFTYPE_MONITOR) {
8876                ret = ath10k_wmi_vdev_up(ar, arvif->vdev_id, 0, vif->addr);
8877                if (ret) {
8878                        ath10k_warn(ar, "failed to up monitor vdev %i: %d\n",
8879                                    arvif->vdev_id, ret);
8880                        goto err_stop;
8881                }
8882
8883                arvif->is_up = true;
8884        }
8885
8886        if (ath10k_mac_can_set_cts_prot(arvif)) {
8887                ret = ath10k_mac_set_cts_prot(arvif);
8888                if (ret)
8889                        ath10k_warn(ar, "failed to set cts protection for vdev %d: %d\n",
8890                                    arvif->vdev_id, ret);
8891        }
8892
8893        if (ath10k_peer_stats_enabled(ar) &&
8894            ar->hw_params.tx_stats_over_pktlog) {
8895                ar->pktlog_filter |= ATH10K_PKTLOG_PEER_STATS;
8896                ret = ath10k_wmi_pdev_pktlog_enable(ar,
8897                                                    ar->pktlog_filter);
8898                if (ret) {
8899                        ath10k_warn(ar, "failed to enable pktlog %d\n", ret);
8900                        goto err_stop;
8901                }
8902        }
8903
8904        mutex_unlock(&ar->conf_mutex);
8905        return 0;
8906
8907err_stop:
8908        ath10k_vdev_stop(arvif);
8909        arvif->is_started = false;
8910        ath10k_mac_vif_setup_ps(arvif);
8911
8912err:
8913        mutex_unlock(&ar->conf_mutex);
8914        return ret;
8915}
8916
8917static void
8918ath10k_mac_op_unassign_vif_chanctx(struct ieee80211_hw *hw,
8919                                   struct ieee80211_vif *vif,
8920                                   struct ieee80211_chanctx_conf *ctx)
8921{
8922        struct ath10k *ar = hw->priv;
8923        struct ath10k_vif *arvif = (void *)vif->drv_priv;
8924        int ret;
8925
8926        mutex_lock(&ar->conf_mutex);
8927
8928        ath10k_dbg(ar, ATH10K_DBG_MAC,
8929                   "mac chanctx unassign ptr %pK vdev_id %i\n",
8930                   ctx, arvif->vdev_id);
8931
8932        WARN_ON(!arvif->is_started);
8933
8934        if (vif->type == NL80211_IFTYPE_MONITOR) {
8935                WARN_ON(!arvif->is_up);
8936
8937                ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
8938                if (ret)
8939                        ath10k_warn(ar, "failed to down monitor vdev %i: %d\n",
8940                                    arvif->vdev_id, ret);
8941
8942                arvif->is_up = false;
8943        }
8944
8945        ret = ath10k_vdev_stop(arvif);
8946        if (ret)
8947                ath10k_warn(ar, "failed to stop vdev %i: %d\n",
8948                            arvif->vdev_id, ret);
8949
8950        arvif->is_started = false;
8951
8952        mutex_unlock(&ar->conf_mutex);
8953}
8954
8955static int
8956ath10k_mac_op_switch_vif_chanctx(struct ieee80211_hw *hw,
8957                                 struct ieee80211_vif_chanctx_switch *vifs,
8958                                 int n_vifs,
8959                                 enum ieee80211_chanctx_switch_mode mode)
8960{
8961        struct ath10k *ar = hw->priv;
8962
8963        mutex_lock(&ar->conf_mutex);
8964
8965        ath10k_dbg(ar, ATH10K_DBG_MAC,
8966                   "mac chanctx switch n_vifs %d mode %d\n",
8967                   n_vifs, mode);
8968        ath10k_mac_update_vif_chan(ar, vifs, n_vifs);
8969
8970        mutex_unlock(&ar->conf_mutex);
8971        return 0;
8972}
8973
8974static void ath10k_mac_op_sta_pre_rcu_remove(struct ieee80211_hw *hw,
8975                                             struct ieee80211_vif *vif,
8976                                             struct ieee80211_sta *sta)
8977{
8978        struct ath10k *ar;
8979        struct ath10k_peer *peer;
8980
8981        ar = hw->priv;
8982
8983        list_for_each_entry(peer, &ar->peers, list)
8984                if (peer->sta == sta)
8985                        peer->removed = true;
8986}
8987
8988/* HT MCS parameters with Nss = 1 */
8989static const struct ath10k_index_ht_data_rate_type supported_ht_mcs_rate_nss1[] = {
8990        /* MCS  L20   L40   S20  S40 */
8991        {0,  { 65,  135,  72,  150} },
8992        {1,  { 130, 270,  144, 300} },
8993        {2,  { 195, 405,  217, 450} },
8994        {3,  { 260, 540,  289, 600} },
8995        {4,  { 390, 810,  433, 900} },
8996        {5,  { 520, 1080, 578, 1200} },
8997        {6,  { 585, 1215, 650, 1350} },
8998        {7,  { 650, 1350, 722, 1500} }
8999};
9000
9001/* HT MCS parameters with Nss = 2 */
9002static const struct ath10k_index_ht_data_rate_type supported_ht_mcs_rate_nss2[] = {
9003        /* MCS  L20    L40   S20   S40 */
9004        {0,  {130,  270,  144,  300} },
9005        {1,  {260,  540,  289,  600} },
9006        {2,  {390,  810,  433,  900} },
9007        {3,  {520,  1080, 578,  1200} },
9008        {4,  {780,  1620, 867,  1800} },
9009        {5,  {1040, 2160, 1156, 2400} },
9010        {6,  {1170, 2430, 1300, 2700} },
9011        {7,  {1300, 2700, 1444, 3000} }
9012};
9013
9014/* MCS parameters with Nss = 1 */
9015static const struct ath10k_index_vht_data_rate_type supported_vht_mcs_rate_nss1[] = {
9016        /* MCS  L80    S80     L40   S40    L20   S20 */
9017        {0,  {293,  325},  {135,  150},  {65,   72} },
9018        {1,  {585,  650},  {270,  300},  {130,  144} },
9019        {2,  {878,  975},  {405,  450},  {195,  217} },
9020        {3,  {1170, 1300}, {540,  600},  {260,  289} },
9021        {4,  {1755, 1950}, {810,  900},  {390,  433} },
9022        {5,  {2340, 2600}, {1080, 1200}, {520,  578} },
9023        {6,  {2633, 2925}, {1215, 1350}, {585,  650} },
9024        {7,  {2925, 3250}, {1350, 1500}, {650,  722} },
9025        {8,  {3510, 3900}, {1620, 1800}, {780,  867} },
9026        {9,  {3900, 4333}, {1800, 2000}, {780,  867} }
9027};
9028
9029/*MCS parameters with Nss = 2 */
9030static const struct ath10k_index_vht_data_rate_type supported_vht_mcs_rate_nss2[] = {
9031        /* MCS  L80    S80     L40   S40    L20   S20 */
9032        {0,  {585,  650},  {270,  300},  {130,  144} },
9033        {1,  {1170, 1300}, {540,  600},  {260,  289} },
9034        {2,  {1755, 1950}, {810,  900},  {390,  433} },
9035        {3,  {2340, 2600}, {1080, 1200}, {520,  578} },
9036        {4,  {3510, 3900}, {1620, 1800}, {780,  867} },
9037        {5,  {4680, 5200}, {2160, 2400}, {1040, 1156} },
9038        {6,  {5265, 5850}, {2430, 2700}, {1170, 1300} },
9039        {7,  {5850, 6500}, {2700, 3000}, {1300, 1444} },
9040        {8,  {7020, 7800}, {3240, 3600}, {1560, 1733} },
9041        {9,  {7800, 8667}, {3600, 4000}, {1560, 1733} }
9042};
9043
9044static void ath10k_mac_get_rate_flags_ht(struct ath10k *ar, u32 rate, u8 nss, u8 mcs,
9045                                         u8 *flags, u8 *bw)
9046{
9047        struct ath10k_index_ht_data_rate_type *mcs_rate;
9048        u8 index;
9049        size_t len_nss1 = ARRAY_SIZE(supported_ht_mcs_rate_nss1);
9050        size_t len_nss2 = ARRAY_SIZE(supported_ht_mcs_rate_nss2);
9051
9052        if (mcs >= (len_nss1 + len_nss2)) {
9053                ath10k_warn(ar, "not supported mcs %d in current rate table", mcs);
9054                return;
9055        }
9056
9057        mcs_rate = (struct ath10k_index_ht_data_rate_type *)
9058                   ((nss == 1) ? &supported_ht_mcs_rate_nss1 :
9059                   &supported_ht_mcs_rate_nss2);
9060
9061        if (mcs >= len_nss1)
9062                index = mcs - len_nss1;
9063        else
9064                index = mcs;
9065
9066        if (rate == mcs_rate[index].supported_rate[0]) {
9067                *bw = RATE_INFO_BW_20;
9068        } else if (rate == mcs_rate[index].supported_rate[1]) {
9069                *bw |= RATE_INFO_BW_40;
9070        } else if (rate == mcs_rate[index].supported_rate[2]) {
9071                *bw |= RATE_INFO_BW_20;
9072                *flags |= RATE_INFO_FLAGS_SHORT_GI;
9073        } else if (rate == mcs_rate[index].supported_rate[3]) {
9074                *bw |= RATE_INFO_BW_40;
9075                *flags |= RATE_INFO_FLAGS_SHORT_GI;
9076        } else {
9077                ath10k_warn(ar, "invalid ht params rate %d 100kbps nss %d mcs %d",
9078                            rate, nss, mcs);
9079        }
9080}
9081
9082static void ath10k_mac_get_rate_flags_vht(struct ath10k *ar, u32 rate, u8 nss, u8 mcs,
9083                                          u8 *flags, u8 *bw)
9084{
9085        struct ath10k_index_vht_data_rate_type *mcs_rate;
9086
9087        mcs_rate = (struct ath10k_index_vht_data_rate_type *)
9088                   ((nss == 1) ? &supported_vht_mcs_rate_nss1 :
9089                   &supported_vht_mcs_rate_nss2);
9090
9091        if (rate == mcs_rate[mcs].supported_VHT80_rate[0]) {
9092                *bw = RATE_INFO_BW_80;
9093        } else if (rate == mcs_rate[mcs].supported_VHT80_rate[1]) {
9094                *bw = RATE_INFO_BW_80;
9095                *flags |= RATE_INFO_FLAGS_SHORT_GI;
9096        } else if (rate == mcs_rate[mcs].supported_VHT40_rate[0]) {
9097                *bw = RATE_INFO_BW_40;
9098        } else if (rate == mcs_rate[mcs].supported_VHT40_rate[1]) {
9099                *bw = RATE_INFO_BW_40;
9100                *flags |= RATE_INFO_FLAGS_SHORT_GI;
9101        } else if (rate == mcs_rate[mcs].supported_VHT20_rate[0]) {
9102                *bw = RATE_INFO_BW_20;
9103        } else if (rate == mcs_rate[mcs].supported_VHT20_rate[1]) {
9104                *bw = RATE_INFO_BW_20;
9105                *flags |= RATE_INFO_FLAGS_SHORT_GI;
9106        } else {
9107                ath10k_warn(ar, "invalid vht params rate %d 100kbps nss %d mcs %d",
9108                            rate, nss, mcs);
9109        }
9110}
9111
9112static void ath10k_mac_get_rate_flags(struct ath10k *ar, u32 rate,
9113                                      enum ath10k_phy_mode mode, u8 nss, u8 mcs,
9114                                      u8 *flags, u8 *bw)
9115{
9116        if (mode == ATH10K_PHY_MODE_HT) {
9117                *flags = RATE_INFO_FLAGS_MCS;
9118                ath10k_mac_get_rate_flags_ht(ar, rate, nss, mcs, flags, bw);
9119        } else if (mode == ATH10K_PHY_MODE_VHT) {
9120                *flags = RATE_INFO_FLAGS_VHT_MCS;
9121                ath10k_mac_get_rate_flags_vht(ar, rate, nss, mcs, flags, bw);
9122        }
9123}
9124
9125static void ath10k_mac_parse_bitrate(struct ath10k *ar, u32 rate_code,
9126                                     u32 bitrate_kbps, struct rate_info *rate)
9127{
9128        enum ath10k_phy_mode mode = ATH10K_PHY_MODE_LEGACY;
9129        enum wmi_rate_preamble preamble = WMI_TLV_GET_HW_RC_PREAM_V1(rate_code);
9130        u8 nss = WMI_TLV_GET_HW_RC_NSS_V1(rate_code) + 1;
9131        u8 mcs = WMI_TLV_GET_HW_RC_RATE_V1(rate_code);
9132        u8 flags = 0, bw = 0;
9133
9134        ath10k_dbg(ar, ATH10K_DBG_MAC, "mac parse rate code 0x%x bitrate %d kbps\n",
9135                   rate_code, bitrate_kbps);
9136
9137        if (preamble == WMI_RATE_PREAMBLE_HT)
9138                mode = ATH10K_PHY_MODE_HT;
9139        else if (preamble == WMI_RATE_PREAMBLE_VHT)
9140                mode = ATH10K_PHY_MODE_VHT;
9141
9142        ath10k_mac_get_rate_flags(ar, bitrate_kbps / 100, mode, nss, mcs, &flags, &bw);
9143
9144        ath10k_dbg(ar, ATH10K_DBG_MAC,
9145                   "mac parse bitrate preamble %d mode %d nss %d mcs %d flags %x bw %d\n",
9146                   preamble, mode, nss, mcs, flags, bw);
9147
9148        rate->flags = flags;
9149        rate->bw = bw;
9150        rate->legacy = bitrate_kbps / 100;
9151        rate->nss = nss;
9152        rate->mcs = mcs;
9153}
9154
9155static void ath10k_mac_sta_get_peer_stats_info(struct ath10k *ar,
9156                                               struct ieee80211_sta *sta,
9157                                               struct station_info *sinfo)
9158{
9159        struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
9160        struct ath10k_peer *peer;
9161        unsigned long time_left;
9162        int ret;
9163
9164        if (!(ar->hw_params.supports_peer_stats_info &&
9165              arsta->arvif->vdev_type == WMI_VDEV_TYPE_STA))
9166                return;
9167
9168        spin_lock_bh(&ar->data_lock);
9169        peer = ath10k_peer_find(ar, arsta->arvif->vdev_id, sta->addr);
9170        spin_unlock_bh(&ar->data_lock);
9171        if (!peer)
9172                return;
9173
9174        reinit_completion(&ar->peer_stats_info_complete);
9175
9176        ret = ath10k_wmi_request_peer_stats_info(ar,
9177                                                 arsta->arvif->vdev_id,
9178                                                 WMI_REQUEST_ONE_PEER_STATS_INFO,
9179                                                 arsta->arvif->bssid,
9180                                                 0);
9181        if (ret && ret != -EOPNOTSUPP) {
9182                ath10k_warn(ar, "could not request peer stats info: %d\n", ret);
9183                return;
9184        }
9185
9186        time_left = wait_for_completion_timeout(&ar->peer_stats_info_complete, 3 * HZ);
9187        if (time_left == 0) {
9188                ath10k_warn(ar, "timed out waiting peer stats info\n");
9189                return;
9190        }
9191
9192        if (arsta->rx_rate_code != 0 && arsta->rx_bitrate_kbps != 0) {
9193                ath10k_mac_parse_bitrate(ar, arsta->rx_rate_code,
9194                                         arsta->rx_bitrate_kbps,
9195                                         &sinfo->rxrate);
9196
9197                sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE);
9198                arsta->rx_rate_code = 0;
9199                arsta->rx_bitrate_kbps = 0;
9200        }
9201
9202        if (arsta->tx_rate_code != 0 && arsta->tx_bitrate_kbps != 0) {
9203                ath10k_mac_parse_bitrate(ar, arsta->tx_rate_code,
9204                                         arsta->tx_bitrate_kbps,
9205                                         &sinfo->txrate);
9206
9207                sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
9208                arsta->tx_rate_code = 0;
9209                arsta->tx_bitrate_kbps = 0;
9210        }
9211}
9212
9213static void ath10k_sta_statistics(struct ieee80211_hw *hw,
9214                                  struct ieee80211_vif *vif,
9215                                  struct ieee80211_sta *sta,
9216                                  struct station_info *sinfo)
9217{
9218        struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
9219        struct ath10k *ar = arsta->arvif->ar;
9220
9221        if (!ath10k_peer_stats_enabled(ar))
9222                return;
9223
9224        mutex_lock(&ar->conf_mutex);
9225        ath10k_debug_fw_stats_request(ar);
9226        mutex_unlock(&ar->conf_mutex);
9227
9228        sinfo->rx_duration = arsta->rx_duration;
9229        sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
9230
9231        if (arsta->txrate.legacy || arsta->txrate.nss) {
9232                if (arsta->txrate.legacy) {
9233                        sinfo->txrate.legacy = arsta->txrate.legacy;
9234                } else {
9235                        sinfo->txrate.mcs = arsta->txrate.mcs;
9236                        sinfo->txrate.nss = arsta->txrate.nss;
9237                        sinfo->txrate.bw = arsta->txrate.bw;
9238                }
9239                sinfo->txrate.flags = arsta->txrate.flags;
9240                sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
9241        }
9242
9243        if (ar->htt.disable_tx_comp) {
9244                sinfo->tx_failed = arsta->tx_failed;
9245                sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
9246        }
9247
9248        sinfo->tx_retries = arsta->tx_retries;
9249        sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
9250
9251        ath10k_mac_sta_get_peer_stats_info(ar, sta, sinfo);
9252}
9253
9254static int ath10k_mac_op_set_tid_config(struct ieee80211_hw *hw,
9255                                        struct ieee80211_vif *vif,
9256                                        struct ieee80211_sta *sta,
9257                                        struct cfg80211_tid_config *tid_config)
9258{
9259        struct ath10k *ar = hw->priv;
9260        struct ath10k_vif *arvif = (void *)vif->drv_priv;
9261        struct ath10k_mac_iter_tid_conf_data data = {};
9262        struct wmi_per_peer_per_tid_cfg_arg arg = {};
9263        int ret, i;
9264
9265        mutex_lock(&ar->conf_mutex);
9266        arg.vdev_id = arvif->vdev_id;
9267
9268        arvif->tids_rst = 0;
9269        memset(arvif->tid_conf_changed, 0, sizeof(arvif->tid_conf_changed));
9270
9271        for (i = 0; i < tid_config->n_tid_conf; i++) {
9272                ret = ath10k_mac_parse_tid_config(ar, sta, vif,
9273                                                  &tid_config->tid_conf[i],
9274                                                  &arg);
9275                if (ret)
9276                        goto exit;
9277        }
9278
9279        ret = 0;
9280
9281        if (sta)
9282                goto exit;
9283
9284        arvif->tids_rst = 0;
9285        data.curr_vif = vif;
9286        data.ar = ar;
9287
9288        ieee80211_iterate_stations_atomic(hw, ath10k_mac_vif_stations_tid_conf,
9289                                          &data);
9290
9291exit:
9292        mutex_unlock(&ar->conf_mutex);
9293        return ret;
9294}
9295
9296static int ath10k_mac_op_reset_tid_config(struct ieee80211_hw *hw,
9297                                          struct ieee80211_vif *vif,
9298                                          struct ieee80211_sta *sta,
9299                                          u8 tids)
9300{
9301        struct ath10k_vif *arvif = (void *)vif->drv_priv;
9302        struct ath10k_mac_iter_tid_conf_data data = {};
9303        struct ath10k *ar = hw->priv;
9304        int ret = 0;
9305
9306        mutex_lock(&ar->conf_mutex);
9307
9308        if (sta) {
9309                arvif->tids_rst = 0;
9310                ret = ath10k_mac_reset_tid_config(ar, sta, arvif, tids);
9311                goto exit;
9312        }
9313
9314        arvif->tids_rst = tids;
9315        data.curr_vif = vif;
9316        data.ar = ar;
9317        ieee80211_iterate_stations_atomic(hw, ath10k_mac_vif_stations_tid_conf,
9318                                          &data);
9319
9320exit:
9321        mutex_unlock(&ar->conf_mutex);
9322        return ret;
9323}
9324
9325static const struct ieee80211_ops ath10k_ops = {
9326        .tx                             = ath10k_mac_op_tx,
9327        .wake_tx_queue                  = ath10k_mac_op_wake_tx_queue,
9328        .start                          = ath10k_start,
9329        .stop                           = ath10k_stop,
9330        .config                         = ath10k_config,
9331        .add_interface                  = ath10k_add_interface,
9332        .remove_interface               = ath10k_remove_interface,
9333        .configure_filter               = ath10k_configure_filter,
9334        .bss_info_changed               = ath10k_bss_info_changed,
9335        .set_coverage_class             = ath10k_mac_op_set_coverage_class,
9336        .hw_scan                        = ath10k_hw_scan,
9337        .cancel_hw_scan                 = ath10k_cancel_hw_scan,
9338        .set_key                        = ath10k_set_key,
9339        .set_default_unicast_key        = ath10k_set_default_unicast_key,
9340        .sta_state                      = ath10k_sta_state,
9341        .sta_set_txpwr                  = ath10k_sta_set_txpwr,
9342        .conf_tx                        = ath10k_conf_tx,
9343        .remain_on_channel              = ath10k_remain_on_channel,
9344        .cancel_remain_on_channel       = ath10k_cancel_remain_on_channel,
9345        .set_rts_threshold              = ath10k_set_rts_threshold,
9346        .set_frag_threshold             = ath10k_mac_op_set_frag_threshold,
9347        .flush                          = ath10k_flush,
9348        .tx_last_beacon                 = ath10k_tx_last_beacon,
9349        .set_antenna                    = ath10k_set_antenna,
9350        .get_antenna                    = ath10k_get_antenna,
9351        .reconfig_complete              = ath10k_reconfig_complete,
9352        .get_survey                     = ath10k_get_survey,
9353        .set_bitrate_mask               = ath10k_mac_op_set_bitrate_mask,
9354        .sta_rc_update                  = ath10k_sta_rc_update,
9355        .offset_tsf                     = ath10k_offset_tsf,
9356        .ampdu_action                   = ath10k_ampdu_action,
9357        .get_et_sset_count              = ath10k_debug_get_et_sset_count,
9358        .get_et_stats                   = ath10k_debug_get_et_stats,
9359        .get_et_strings                 = ath10k_debug_get_et_strings,
9360        .add_chanctx                    = ath10k_mac_op_add_chanctx,
9361        .remove_chanctx                 = ath10k_mac_op_remove_chanctx,
9362        .change_chanctx                 = ath10k_mac_op_change_chanctx,
9363        .assign_vif_chanctx             = ath10k_mac_op_assign_vif_chanctx,
9364        .unassign_vif_chanctx           = ath10k_mac_op_unassign_vif_chanctx,
9365        .switch_vif_chanctx             = ath10k_mac_op_switch_vif_chanctx,
9366        .sta_pre_rcu_remove             = ath10k_mac_op_sta_pre_rcu_remove,
9367        .sta_statistics                 = ath10k_sta_statistics,
9368        .set_tid_config                 = ath10k_mac_op_set_tid_config,
9369        .reset_tid_config               = ath10k_mac_op_reset_tid_config,
9370
9371        CFG80211_TESTMODE_CMD(ath10k_tm_cmd)
9372
9373#ifdef CONFIG_PM
9374        .suspend                        = ath10k_wow_op_suspend,
9375        .resume                         = ath10k_wow_op_resume,
9376        .set_wakeup                     = ath10k_wow_op_set_wakeup,
9377#endif
9378#ifdef CONFIG_MAC80211_DEBUGFS
9379        .sta_add_debugfs                = ath10k_sta_add_debugfs,
9380#endif
9381        .set_sar_specs                  = ath10k_mac_set_sar_specs,
9382};
9383
9384#define CHAN2G(_channel, _freq, _flags) { \
9385        .band                   = NL80211_BAND_2GHZ, \
9386        .hw_value               = (_channel), \
9387        .center_freq            = (_freq), \
9388        .flags                  = (_flags), \
9389        .max_antenna_gain       = 0, \
9390        .max_power              = 30, \
9391}
9392
9393#define CHAN5G(_channel, _freq, _flags) { \
9394        .band                   = NL80211_BAND_5GHZ, \
9395        .hw_value               = (_channel), \
9396        .center_freq            = (_freq), \
9397        .flags                  = (_flags), \
9398        .max_antenna_gain       = 0, \
9399        .max_power              = 30, \
9400}
9401
9402static const struct ieee80211_channel ath10k_2ghz_channels[] = {
9403        CHAN2G(1, 2412, 0),
9404        CHAN2G(2, 2417, 0),
9405        CHAN2G(3, 2422, 0),
9406        CHAN2G(4, 2427, 0),
9407        CHAN2G(5, 2432, 0),
9408        CHAN2G(6, 2437, 0),
9409        CHAN2G(7, 2442, 0),
9410        CHAN2G(8, 2447, 0),
9411        CHAN2G(9, 2452, 0),
9412        CHAN2G(10, 2457, 0),
9413        CHAN2G(11, 2462, 0),
9414        CHAN2G(12, 2467, 0),
9415        CHAN2G(13, 2472, 0),
9416        CHAN2G(14, 2484, 0),
9417};
9418
9419static const struct ieee80211_channel ath10k_5ghz_channels[] = {
9420        CHAN5G(36, 5180, 0),
9421        CHAN5G(40, 5200, 0),
9422        CHAN5G(44, 5220, 0),
9423        CHAN5G(48, 5240, 0),
9424        CHAN5G(52, 5260, 0),
9425        CHAN5G(56, 5280, 0),
9426        CHAN5G(60, 5300, 0),
9427        CHAN5G(64, 5320, 0),
9428        CHAN5G(100, 5500, 0),
9429        CHAN5G(104, 5520, 0),
9430        CHAN5G(108, 5540, 0),
9431        CHAN5G(112, 5560, 0),
9432        CHAN5G(116, 5580, 0),
9433        CHAN5G(120, 5600, 0),
9434        CHAN5G(124, 5620, 0),
9435        CHAN5G(128, 5640, 0),
9436        CHAN5G(132, 5660, 0),
9437        CHAN5G(136, 5680, 0),
9438        CHAN5G(140, 5700, 0),
9439        CHAN5G(144, 5720, 0),
9440        CHAN5G(149, 5745, 0),
9441        CHAN5G(153, 5765, 0),
9442        CHAN5G(157, 5785, 0),
9443        CHAN5G(161, 5805, 0),
9444        CHAN5G(165, 5825, 0),
9445        CHAN5G(169, 5845, 0),
9446        CHAN5G(173, 5865, 0),
9447        /* If you add more, you may need to change ATH10K_MAX_5G_CHAN */
9448        /* And you will definitely need to change ATH10K_NUM_CHANS in core.h */
9449};
9450
9451struct ath10k *ath10k_mac_create(size_t priv_size)
9452{
9453        struct ieee80211_hw *hw;
9454        struct ieee80211_ops *ops;
9455        struct ath10k *ar;
9456
9457        ops = kmemdup(&ath10k_ops, sizeof(ath10k_ops), GFP_KERNEL);
9458        if (!ops)
9459                return NULL;
9460
9461        hw = ieee80211_alloc_hw(sizeof(struct ath10k) + priv_size, ops);
9462        if (!hw) {
9463                kfree(ops);
9464                return NULL;
9465        }
9466
9467        ar = hw->priv;
9468        ar->hw = hw;
9469        ar->ops = ops;
9470
9471        return ar;
9472}
9473
9474void ath10k_mac_destroy(struct ath10k *ar)
9475{
9476        struct ieee80211_ops *ops = ar->ops;
9477
9478        ieee80211_free_hw(ar->hw);
9479        kfree(ops);
9480}
9481
9482static const struct ieee80211_iface_limit ath10k_if_limits[] = {
9483        {
9484                .max    = 8,
9485                .types  = BIT(NL80211_IFTYPE_STATION)
9486                        | BIT(NL80211_IFTYPE_P2P_CLIENT)
9487        },
9488        {
9489                .max    = 3,
9490                .types  = BIT(NL80211_IFTYPE_P2P_GO)
9491        },
9492        {
9493                .max    = 1,
9494                .types  = BIT(NL80211_IFTYPE_P2P_DEVICE)
9495        },
9496        {
9497                .max    = 7,
9498                .types  = BIT(NL80211_IFTYPE_AP)
9499#ifdef CONFIG_MAC80211_MESH
9500                        | BIT(NL80211_IFTYPE_MESH_POINT)
9501#endif
9502        },
9503};
9504
9505static const struct ieee80211_iface_limit ath10k_10x_if_limits[] = {
9506        {
9507                .max    = 8,
9508                .types  = BIT(NL80211_IFTYPE_AP)
9509#ifdef CONFIG_MAC80211_MESH
9510                        | BIT(NL80211_IFTYPE_MESH_POINT)
9511#endif
9512        },
9513        {
9514                .max    = 1,
9515                .types  = BIT(NL80211_IFTYPE_STATION)
9516        },
9517};
9518
9519static const struct ieee80211_iface_combination ath10k_if_comb[] = {
9520        {
9521                .limits = ath10k_if_limits,
9522                .n_limits = ARRAY_SIZE(ath10k_if_limits),
9523                .max_interfaces = 8,
9524                .num_different_channels = 1,
9525                .beacon_int_infra_match = true,
9526        },
9527};
9528
9529static const struct ieee80211_iface_combination ath10k_10x_if_comb[] = {
9530        {
9531                .limits = ath10k_10x_if_limits,
9532                .n_limits = ARRAY_SIZE(ath10k_10x_if_limits),
9533                .max_interfaces = 8,
9534                .num_different_channels = 1,
9535                .beacon_int_infra_match = true,
9536                .beacon_int_min_gcd = 1,
9537#ifdef CONFIG_ATH10K_DFS_CERTIFIED
9538                .radar_detect_widths =  BIT(NL80211_CHAN_WIDTH_20_NOHT) |
9539                                        BIT(NL80211_CHAN_WIDTH_20) |
9540                                        BIT(NL80211_CHAN_WIDTH_40) |
9541                                        BIT(NL80211_CHAN_WIDTH_80),
9542#endif
9543        },
9544};
9545
9546static const struct ieee80211_iface_limit ath10k_tlv_if_limit[] = {
9547        {
9548                .max = 2,
9549                .types = BIT(NL80211_IFTYPE_STATION),
9550        },
9551        {
9552                .max = 2,
9553                .types = BIT(NL80211_IFTYPE_AP) |
9554#ifdef CONFIG_MAC80211_MESH
9555                         BIT(NL80211_IFTYPE_MESH_POINT) |
9556#endif
9557                         BIT(NL80211_IFTYPE_P2P_CLIENT) |
9558                         BIT(NL80211_IFTYPE_P2P_GO),
9559        },
9560        {
9561                .max = 1,
9562                .types = BIT(NL80211_IFTYPE_P2P_DEVICE),
9563        },
9564};
9565
9566static const struct ieee80211_iface_limit ath10k_tlv_qcs_if_limit[] = {
9567        {
9568                .max = 2,
9569                .types = BIT(NL80211_IFTYPE_STATION),
9570        },
9571        {
9572                .max = 2,
9573                .types = BIT(NL80211_IFTYPE_P2P_CLIENT),
9574        },
9575        {
9576                .max = 1,
9577                .types = BIT(NL80211_IFTYPE_AP) |
9578#ifdef CONFIG_MAC80211_MESH
9579                         BIT(NL80211_IFTYPE_MESH_POINT) |
9580#endif
9581                         BIT(NL80211_IFTYPE_P2P_GO),
9582        },
9583        {
9584                .max = 1,
9585                .types = BIT(NL80211_IFTYPE_P2P_DEVICE),
9586        },
9587};
9588
9589static const struct ieee80211_iface_limit ath10k_tlv_if_limit_ibss[] = {
9590        {
9591                .max = 1,
9592                .types = BIT(NL80211_IFTYPE_STATION),
9593        },
9594        {
9595                .max = 1,
9596                .types = BIT(NL80211_IFTYPE_ADHOC),
9597        },
9598};
9599
9600/* FIXME: This is not thouroughly tested. These combinations may over- or
9601 * underestimate hw/fw capabilities.
9602 */
9603static struct ieee80211_iface_combination ath10k_tlv_if_comb[] = {
9604        {
9605                .limits = ath10k_tlv_if_limit,
9606                .num_different_channels = 1,
9607                .max_interfaces = 4,
9608                .n_limits = ARRAY_SIZE(ath10k_tlv_if_limit),
9609        },
9610        {
9611                .limits = ath10k_tlv_if_limit_ibss,
9612                .num_different_channels = 1,
9613                .max_interfaces = 2,
9614                .n_limits = ARRAY_SIZE(ath10k_tlv_if_limit_ibss),
9615        },
9616};
9617
9618static struct ieee80211_iface_combination ath10k_tlv_qcs_if_comb[] = {
9619        {
9620                .limits = ath10k_tlv_if_limit,
9621                .num_different_channels = 1,
9622                .max_interfaces = 4,
9623                .n_limits = ARRAY_SIZE(ath10k_tlv_if_limit),
9624        },
9625        {
9626                .limits = ath10k_tlv_qcs_if_limit,
9627                .num_different_channels = 2,
9628                .max_interfaces = 4,
9629                .n_limits = ARRAY_SIZE(ath10k_tlv_qcs_if_limit),
9630        },
9631        {
9632                .limits = ath10k_tlv_if_limit_ibss,
9633                .num_different_channels = 1,
9634                .max_interfaces = 2,
9635                .n_limits = ARRAY_SIZE(ath10k_tlv_if_limit_ibss),
9636        },
9637};
9638
9639static const struct ieee80211_iface_limit ath10k_10_4_if_limits[] = {
9640        {
9641                .max = 1,
9642                .types = BIT(NL80211_IFTYPE_STATION),
9643        },
9644        {
9645                .max    = 16,
9646                .types  = BIT(NL80211_IFTYPE_AP)
9647#ifdef CONFIG_MAC80211_MESH
9648                        | BIT(NL80211_IFTYPE_MESH_POINT)
9649#endif
9650        },
9651};
9652
9653static const struct ieee80211_iface_combination ath10k_10_4_if_comb[] = {
9654        {
9655                .limits = ath10k_10_4_if_limits,
9656                .n_limits = ARRAY_SIZE(ath10k_10_4_if_limits),
9657                .max_interfaces = 16,
9658                .num_different_channels = 1,
9659                .beacon_int_infra_match = true,
9660                .beacon_int_min_gcd = 1,
9661#ifdef CONFIG_ATH10K_DFS_CERTIFIED
9662                .radar_detect_widths =  BIT(NL80211_CHAN_WIDTH_20_NOHT) |
9663                                        BIT(NL80211_CHAN_WIDTH_20) |
9664                                        BIT(NL80211_CHAN_WIDTH_40) |
9665                                        BIT(NL80211_CHAN_WIDTH_80) |
9666                                        BIT(NL80211_CHAN_WIDTH_80P80) |
9667                                        BIT(NL80211_CHAN_WIDTH_160),
9668#endif
9669        },
9670};
9671
9672static const struct
9673ieee80211_iface_combination ath10k_10_4_bcn_int_if_comb[] = {
9674        {
9675                .limits = ath10k_10_4_if_limits,
9676                .n_limits = ARRAY_SIZE(ath10k_10_4_if_limits),
9677                .max_interfaces = 16,
9678                .num_different_channels = 1,
9679                .beacon_int_infra_match = true,
9680                .beacon_int_min_gcd = 100,
9681#ifdef CONFIG_ATH10K_DFS_CERTIFIED
9682                .radar_detect_widths =  BIT(NL80211_CHAN_WIDTH_20_NOHT) |
9683                                        BIT(NL80211_CHAN_WIDTH_20) |
9684                                        BIT(NL80211_CHAN_WIDTH_40) |
9685                                        BIT(NL80211_CHAN_WIDTH_80) |
9686                                        BIT(NL80211_CHAN_WIDTH_80P80) |
9687                                        BIT(NL80211_CHAN_WIDTH_160),
9688#endif
9689        },
9690};
9691
9692static void ath10k_get_arvif_iter(void *data, u8 *mac,
9693                                  struct ieee80211_vif *vif)
9694{
9695        struct ath10k_vif_iter *arvif_iter = data;
9696        struct ath10k_vif *arvif = (void *)vif->drv_priv;
9697
9698        if (arvif->vdev_id == arvif_iter->vdev_id)
9699                arvif_iter->arvif = arvif;
9700}
9701
9702struct ath10k_vif *ath10k_get_arvif(struct ath10k *ar, u32 vdev_id)
9703{
9704        struct ath10k_vif_iter arvif_iter;
9705
9706        memset(&arvif_iter, 0, sizeof(struct ath10k_vif_iter));
9707        arvif_iter.vdev_id = vdev_id;
9708
9709        ieee80211_iterate_active_interfaces_atomic(ar->hw,
9710                                                   ATH10K_ITER_RESUME_FLAGS,
9711                                                   ath10k_get_arvif_iter,
9712                                                   &arvif_iter);
9713        if (!arvif_iter.arvif) {
9714                ath10k_warn(ar, "No VIF found for vdev %d\n", vdev_id);
9715                return NULL;
9716        }
9717
9718        return arvif_iter.arvif;
9719}
9720
9721#define WRD_METHOD "WRDD"
9722#define WRDD_WIFI  (0x07)
9723
9724static u32 ath10k_mac_wrdd_get_mcc(struct ath10k *ar, union acpi_object *wrdd)
9725{
9726        union acpi_object *mcc_pkg;
9727        union acpi_object *domain_type;
9728        union acpi_object *mcc_value;
9729        u32 i;
9730
9731        if (wrdd->type != ACPI_TYPE_PACKAGE ||
9732            wrdd->package.count < 2 ||
9733            wrdd->package.elements[0].type != ACPI_TYPE_INTEGER ||
9734            wrdd->package.elements[0].integer.value != 0) {
9735                ath10k_warn(ar, "ignoring malformed/unsupported wrdd structure\n");
9736                return 0;
9737        }
9738
9739        for (i = 1; i < wrdd->package.count; ++i) {
9740                mcc_pkg = &wrdd->package.elements[i];
9741
9742                if (mcc_pkg->type != ACPI_TYPE_PACKAGE)
9743                        continue;
9744                if (mcc_pkg->package.count < 2)
9745                        continue;
9746                if (mcc_pkg->package.elements[0].type != ACPI_TYPE_INTEGER ||
9747                    mcc_pkg->package.elements[1].type != ACPI_TYPE_INTEGER)
9748                        continue;
9749
9750                domain_type = &mcc_pkg->package.elements[0];
9751                if (domain_type->integer.value != WRDD_WIFI)
9752                        continue;
9753
9754                mcc_value = &mcc_pkg->package.elements[1];
9755                return mcc_value->integer.value;
9756        }
9757        return 0;
9758}
9759
9760static int ath10k_mac_get_wrdd_regulatory(struct ath10k *ar, u16 *rd)
9761{
9762        acpi_handle root_handle;
9763        acpi_handle handle;
9764        struct acpi_buffer wrdd = {ACPI_ALLOCATE_BUFFER, NULL};
9765        acpi_status status;
9766        u32 alpha2_code;
9767        char alpha2[3];
9768
9769        root_handle = ACPI_HANDLE(ar->dev);
9770        if (!root_handle)
9771                return -EOPNOTSUPP;
9772
9773        status = acpi_get_handle(root_handle, (acpi_string)WRD_METHOD, &handle);
9774        if (ACPI_FAILURE(status)) {
9775                ath10k_dbg(ar, ATH10K_DBG_BOOT,
9776                           "failed to get wrd method %d\n", status);
9777                return -EIO;
9778        }
9779
9780        status = acpi_evaluate_object(handle, NULL, NULL, &wrdd);
9781        if (ACPI_FAILURE(status)) {
9782                ath10k_dbg(ar, ATH10K_DBG_BOOT,
9783                           "failed to call wrdc %d\n", status);
9784                return -EIO;
9785        }
9786
9787        alpha2_code = ath10k_mac_wrdd_get_mcc(ar, wrdd.pointer);
9788        kfree(wrdd.pointer);
9789        if (!alpha2_code)
9790                return -EIO;
9791
9792        alpha2[0] = (alpha2_code >> 8) & 0xff;
9793        alpha2[1] = (alpha2_code >> 0) & 0xff;
9794        alpha2[2] = '\0';
9795
9796        ath10k_dbg(ar, ATH10K_DBG_BOOT,
9797                   "regulatory hint from WRDD (alpha2-code): %s\n", alpha2);
9798
9799        *rd = ath_regd_find_country_by_name(alpha2);
9800        if (*rd == 0xffff)
9801                return -EIO;
9802
9803        *rd |= COUNTRY_ERD_FLAG;
9804        return 0;
9805}
9806
9807static int ath10k_mac_init_rd(struct ath10k *ar)
9808{
9809        int ret;
9810        u16 rd;
9811
9812        ret = ath10k_mac_get_wrdd_regulatory(ar, &rd);
9813        if (ret) {
9814                ath10k_dbg(ar, ATH10K_DBG_BOOT,
9815                           "fallback to eeprom programmed regulatory settings\n");
9816                rd = ar->hw_eeprom_rd;
9817        }
9818
9819        ar->ath_common.regulatory.current_rd = rd;
9820        return 0;
9821}
9822
9823int ath10k_mac_register(struct ath10k *ar)
9824{
9825        static const u32 cipher_suites[] = {
9826                WLAN_CIPHER_SUITE_WEP40,
9827                WLAN_CIPHER_SUITE_WEP104,
9828                WLAN_CIPHER_SUITE_TKIP,
9829                WLAN_CIPHER_SUITE_CCMP,
9830
9831                /* Do not add hardware supported ciphers before this line.
9832                 * Allow software encryption for all chips. Don't forget to
9833                 * update n_cipher_suites below.
9834                 */
9835                WLAN_CIPHER_SUITE_AES_CMAC,
9836                WLAN_CIPHER_SUITE_BIP_CMAC_256,
9837                WLAN_CIPHER_SUITE_BIP_GMAC_128,
9838                WLAN_CIPHER_SUITE_BIP_GMAC_256,
9839
9840                /* Only QCA99x0 and QCA4019 varients support GCMP-128, GCMP-256
9841                 * and CCMP-256 in hardware.
9842                 */
9843                WLAN_CIPHER_SUITE_GCMP,
9844                WLAN_CIPHER_SUITE_GCMP_256,
9845                WLAN_CIPHER_SUITE_CCMP_256,
9846        };
9847        struct ieee80211_supported_band *band;
9848        void *channels;
9849        int ret;
9850
9851        if (!is_valid_ether_addr(ar->mac_addr)) {
9852                ath10k_warn(ar, "invalid MAC address; choosing random\n");
9853                eth_random_addr(ar->mac_addr);
9854        }
9855        SET_IEEE80211_PERM_ADDR(ar->hw, ar->mac_addr);
9856
9857        SET_IEEE80211_DEV(ar->hw, ar->dev);
9858
9859        BUILD_BUG_ON((ARRAY_SIZE(ath10k_2ghz_channels) +
9860                      ARRAY_SIZE(ath10k_5ghz_channels)) !=
9861                     ATH10K_NUM_CHANS);
9862
9863        if (ar->phy_capability & WHAL_WLAN_11G_CAPABILITY) {
9864                channels = kmemdup(ath10k_2ghz_channels,
9865                                   sizeof(ath10k_2ghz_channels),
9866                                   GFP_KERNEL);
9867                if (!channels) {
9868                        ret = -ENOMEM;
9869                        goto err_free;
9870                }
9871
9872                band = &ar->mac.sbands[NL80211_BAND_2GHZ];
9873                band->n_channels = ARRAY_SIZE(ath10k_2ghz_channels);
9874                band->channels = channels;
9875
9876                if (ar->hw_params.cck_rate_map_rev2) {
9877                        band->n_bitrates = ath10k_g_rates_rev2_size;
9878                        band->bitrates = ath10k_g_rates_rev2;
9879                } else {
9880                        band->n_bitrates = ath10k_g_rates_size;
9881                        band->bitrates = ath10k_g_rates;
9882                }
9883
9884                ar->hw->wiphy->bands[NL80211_BAND_2GHZ] = band;
9885        }
9886
9887        if (ar->phy_capability & WHAL_WLAN_11A_CAPABILITY) {
9888                channels = kmemdup(ath10k_5ghz_channels,
9889                                   sizeof(ath10k_5ghz_channels),
9890                                   GFP_KERNEL);
9891                if (!channels) {
9892                        ret = -ENOMEM;
9893                        goto err_free;
9894                }
9895
9896                band = &ar->mac.sbands[NL80211_BAND_5GHZ];
9897                band->n_channels = ARRAY_SIZE(ath10k_5ghz_channels);
9898                band->channels = channels;
9899                band->n_bitrates = ath10k_a_rates_size;
9900                band->bitrates = ath10k_a_rates;
9901                ar->hw->wiphy->bands[NL80211_BAND_5GHZ] = band;
9902        }
9903
9904        wiphy_read_of_freq_limits(ar->hw->wiphy);
9905        ath10k_mac_setup_ht_vht_cap(ar);
9906
9907        ar->hw->wiphy->interface_modes =
9908                BIT(NL80211_IFTYPE_STATION) |
9909                BIT(NL80211_IFTYPE_AP) |
9910                BIT(NL80211_IFTYPE_MESH_POINT);
9911
9912        ar->hw->wiphy->available_antennas_rx = ar->cfg_rx_chainmask;
9913        ar->hw->wiphy->available_antennas_tx = ar->cfg_tx_chainmask;
9914
9915        if (!test_bit(ATH10K_FW_FEATURE_NO_P2P, ar->normal_mode_fw.fw_file.fw_features))
9916                ar->hw->wiphy->interface_modes |=
9917                        BIT(NL80211_IFTYPE_P2P_DEVICE) |
9918                        BIT(NL80211_IFTYPE_P2P_CLIENT) |
9919                        BIT(NL80211_IFTYPE_P2P_GO);
9920
9921        ieee80211_hw_set(ar->hw, SIGNAL_DBM);
9922
9923        if (!test_bit(ATH10K_FW_FEATURE_NO_PS,
9924                      ar->running_fw->fw_file.fw_features)) {
9925                ieee80211_hw_set(ar->hw, SUPPORTS_PS);
9926                ieee80211_hw_set(ar->hw, SUPPORTS_DYNAMIC_PS);
9927        }
9928
9929        ieee80211_hw_set(ar->hw, MFP_CAPABLE);
9930        ieee80211_hw_set(ar->hw, REPORTS_TX_ACK_STATUS);
9931        ieee80211_hw_set(ar->hw, HAS_RATE_CONTROL);
9932        ieee80211_hw_set(ar->hw, AP_LINK_PS);
9933        ieee80211_hw_set(ar->hw, SPECTRUM_MGMT);
9934        ieee80211_hw_set(ar->hw, SUPPORT_FAST_XMIT);
9935        ieee80211_hw_set(ar->hw, CONNECTION_MONITOR);
9936        ieee80211_hw_set(ar->hw, SUPPORTS_PER_STA_GTK);
9937        ieee80211_hw_set(ar->hw, WANT_MONITOR_VIF);
9938        ieee80211_hw_set(ar->hw, CHANCTX_STA_CSA);
9939        ieee80211_hw_set(ar->hw, QUEUE_CONTROL);
9940        ieee80211_hw_set(ar->hw, SUPPORTS_TX_FRAG);
9941        ieee80211_hw_set(ar->hw, REPORTS_LOW_ACK);
9942
9943        if (!test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags))
9944                ieee80211_hw_set(ar->hw, SW_CRYPTO_CONTROL);
9945
9946        ar->hw->wiphy->features |= NL80211_FEATURE_STATIC_SMPS;
9947        ar->hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
9948
9949        if (ar->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS)
9950                ar->hw->wiphy->features |= NL80211_FEATURE_DYNAMIC_SMPS;
9951
9952        if (ar->ht_cap_info & WMI_HT_CAP_ENABLED) {
9953                ieee80211_hw_set(ar->hw, AMPDU_AGGREGATION);
9954                ieee80211_hw_set(ar->hw, TX_AMPDU_SETUP_IN_HW);
9955        }
9956
9957        ar->hw->wiphy->max_scan_ssids = WLAN_SCAN_PARAMS_MAX_SSID;
9958        ar->hw->wiphy->max_scan_ie_len = WLAN_SCAN_PARAMS_MAX_IE_LEN;
9959
9960        if (test_bit(WMI_SERVICE_NLO, ar->wmi.svc_map)) {
9961                ar->hw->wiphy->max_sched_scan_ssids = WMI_PNO_MAX_SUPP_NETWORKS;
9962                ar->hw->wiphy->max_match_sets = WMI_PNO_MAX_SUPP_NETWORKS;
9963                ar->hw->wiphy->max_sched_scan_ie_len = WMI_PNO_MAX_IE_LENGTH;
9964                ar->hw->wiphy->max_sched_scan_plans = WMI_PNO_MAX_SCHED_SCAN_PLANS;
9965                ar->hw->wiphy->max_sched_scan_plan_interval =
9966                        WMI_PNO_MAX_SCHED_SCAN_PLAN_INT;
9967                ar->hw->wiphy->max_sched_scan_plan_iterations =
9968                        WMI_PNO_MAX_SCHED_SCAN_PLAN_ITRNS;
9969                ar->hw->wiphy->features |= NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
9970        }
9971
9972        ar->hw->vif_data_size = sizeof(struct ath10k_vif);
9973        ar->hw->sta_data_size = sizeof(struct ath10k_sta);
9974        ar->hw->txq_data_size = sizeof(struct ath10k_txq);
9975
9976        ar->hw->max_listen_interval = ATH10K_MAX_HW_LISTEN_INTERVAL;
9977
9978        if (test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map)) {
9979                ar->hw->wiphy->flags |= WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD;
9980
9981                /* Firmware delivers WPS/P2P Probe Requests frames to driver so
9982                 * that userspace (e.g. wpa_supplicant/hostapd) can generate
9983                 * correct Probe Responses. This is more of a hack advert..
9984                 */
9985                ar->hw->wiphy->probe_resp_offload |=
9986                        NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS |
9987                        NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2 |
9988                        NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P;
9989        }
9990
9991        if (test_bit(WMI_SERVICE_TDLS, ar->wmi.svc_map) ||
9992            test_bit(WMI_SERVICE_TDLS_EXPLICIT_MODE_ONLY, ar->wmi.svc_map)) {
9993                ar->hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS;
9994                if (test_bit(WMI_SERVICE_TDLS_WIDER_BANDWIDTH, ar->wmi.svc_map))
9995                        ieee80211_hw_set(ar->hw, TDLS_WIDER_BW);
9996        }
9997
9998        if (test_bit(WMI_SERVICE_TDLS_UAPSD_BUFFER_STA, ar->wmi.svc_map))
9999                ieee80211_hw_set(ar->hw, SUPPORTS_TDLS_BUFFER_STA);
10000
10001        ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
10002        ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
10003        ar->hw->wiphy->max_remain_on_channel_duration = 5000;
10004
10005        ar->hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
10006        ar->hw->wiphy->features |= NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
10007                                   NL80211_FEATURE_AP_SCAN;
10008
10009        ar->hw->wiphy->max_ap_assoc_sta = ar->max_num_stations;
10010
10011        ret = ath10k_wow_init(ar);
10012        if (ret) {
10013                ath10k_warn(ar, "failed to init wow: %d\n", ret);
10014                goto err_free;
10015        }
10016
10017        wiphy_ext_feature_set(ar->hw->wiphy, NL80211_EXT_FEATURE_VHT_IBSS);
10018        wiphy_ext_feature_set(ar->hw->wiphy,
10019                              NL80211_EXT_FEATURE_SET_SCAN_DWELL);
10020        wiphy_ext_feature_set(ar->hw->wiphy, NL80211_EXT_FEATURE_AQL);
10021
10022        if (test_bit(WMI_SERVICE_TX_DATA_ACK_RSSI, ar->wmi.svc_map) ||
10023            test_bit(WMI_SERVICE_HTT_MGMT_TX_COMP_VALID_FLAGS, ar->wmi.svc_map))
10024                wiphy_ext_feature_set(ar->hw->wiphy,
10025                                      NL80211_EXT_FEATURE_ACK_SIGNAL_SUPPORT);
10026
10027        if (ath10k_peer_stats_enabled(ar) ||
10028            test_bit(WMI_SERVICE_REPORT_AIRTIME, ar->wmi.svc_map))
10029                wiphy_ext_feature_set(ar->hw->wiphy,
10030                                      NL80211_EXT_FEATURE_AIRTIME_FAIRNESS);
10031
10032        if (test_bit(WMI_SERVICE_RTT_RESPONDER_ROLE, ar->wmi.svc_map))
10033                wiphy_ext_feature_set(ar->hw->wiphy,
10034                                      NL80211_EXT_FEATURE_ENABLE_FTM_RESPONDER);
10035
10036        if (test_bit(WMI_SERVICE_TX_PWR_PER_PEER, ar->wmi.svc_map))
10037                wiphy_ext_feature_set(ar->hw->wiphy,
10038                                      NL80211_EXT_FEATURE_STA_TX_PWR);
10039
10040        if (test_bit(WMI_SERVICE_PEER_TID_CONFIGS_SUPPORT, ar->wmi.svc_map)) {
10041                ar->hw->wiphy->tid_config_support.vif |=
10042                                BIT(NL80211_TID_CONFIG_ATTR_NOACK) |
10043                                BIT(NL80211_TID_CONFIG_ATTR_RETRY_SHORT) |
10044                                BIT(NL80211_TID_CONFIG_ATTR_RETRY_LONG) |
10045                                BIT(NL80211_TID_CONFIG_ATTR_AMPDU_CTRL) |
10046                                BIT(NL80211_TID_CONFIG_ATTR_TX_RATE) |
10047                                BIT(NL80211_TID_CONFIG_ATTR_TX_RATE_TYPE);
10048
10049                if (test_bit(WMI_SERVICE_EXT_PEER_TID_CONFIGS_SUPPORT,
10050                             ar->wmi.svc_map)) {
10051                        ar->hw->wiphy->tid_config_support.vif |=
10052                                BIT(NL80211_TID_CONFIG_ATTR_RTSCTS_CTRL);
10053                }
10054
10055                ar->hw->wiphy->tid_config_support.peer =
10056                                ar->hw->wiphy->tid_config_support.vif;
10057                ar->hw->wiphy->max_data_retry_count = ATH10K_MAX_RETRY_COUNT;
10058        } else {
10059                ar->ops->set_tid_config = NULL;
10060        }
10061        /*
10062         * on LL hardware queues are managed entirely by the FW
10063         * so we only advertise to mac we can do the queues thing
10064         */
10065        ar->hw->queues = IEEE80211_MAX_QUEUES;
10066
10067        /* vdev_ids are used as hw queue numbers. Make sure offchan tx queue is
10068         * something that vdev_ids can't reach so that we don't stop the queue
10069         * accidentally.
10070         */
10071        ar->hw->offchannel_tx_hw_queue = IEEE80211_MAX_QUEUES - 1;
10072
10073        switch (ar->running_fw->fw_file.wmi_op_version) {
10074        case ATH10K_FW_WMI_OP_VERSION_MAIN:
10075                ar->hw->wiphy->iface_combinations = ath10k_if_comb;
10076                ar->hw->wiphy->n_iface_combinations =
10077                        ARRAY_SIZE(ath10k_if_comb);
10078                ar->hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_ADHOC);
10079                break;
10080        case ATH10K_FW_WMI_OP_VERSION_TLV:
10081                if (test_bit(WMI_SERVICE_ADAPTIVE_OCS, ar->wmi.svc_map)) {
10082                        ar->hw->wiphy->iface_combinations =
10083                                ath10k_tlv_qcs_if_comb;
10084                        ar->hw->wiphy->n_iface_combinations =
10085                                ARRAY_SIZE(ath10k_tlv_qcs_if_comb);
10086                } else {
10087                        ar->hw->wiphy->iface_combinations = ath10k_tlv_if_comb;
10088                        ar->hw->wiphy->n_iface_combinations =
10089                                ARRAY_SIZE(ath10k_tlv_if_comb);
10090                }
10091                ar->hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_ADHOC);
10092                break;
10093        case ATH10K_FW_WMI_OP_VERSION_10_1:
10094        case ATH10K_FW_WMI_OP_VERSION_10_2:
10095        case ATH10K_FW_WMI_OP_VERSION_10_2_4:
10096                ar->hw->wiphy->iface_combinations = ath10k_10x_if_comb;
10097                ar->hw->wiphy->n_iface_combinations =
10098                        ARRAY_SIZE(ath10k_10x_if_comb);
10099                break;
10100        case ATH10K_FW_WMI_OP_VERSION_10_4:
10101                ar->hw->wiphy->iface_combinations = ath10k_10_4_if_comb;
10102                ar->hw->wiphy->n_iface_combinations =
10103                        ARRAY_SIZE(ath10k_10_4_if_comb);
10104                if (test_bit(WMI_SERVICE_VDEV_DIFFERENT_BEACON_INTERVAL_SUPPORT,
10105                             ar->wmi.svc_map)) {
10106                        ar->hw->wiphy->iface_combinations =
10107                                ath10k_10_4_bcn_int_if_comb;
10108                        ar->hw->wiphy->n_iface_combinations =
10109                                ARRAY_SIZE(ath10k_10_4_bcn_int_if_comb);
10110                }
10111                break;
10112        case ATH10K_FW_WMI_OP_VERSION_UNSET:
10113        case ATH10K_FW_WMI_OP_VERSION_MAX:
10114                WARN_ON(1);
10115                ret = -EINVAL;
10116                goto err_free;
10117        }
10118
10119        if (ar->hw_params.dynamic_sar_support)
10120                ar->hw->wiphy->sar_capa = &ath10k_sar_capa;
10121
10122        if (!test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags))
10123                ar->hw->netdev_features = NETIF_F_HW_CSUM;
10124
10125        if (IS_ENABLED(CONFIG_ATH10K_DFS_CERTIFIED)) {
10126                /* Init ath dfs pattern detector */
10127                ar->ath_common.debug_mask = ATH_DBG_DFS;
10128                ar->dfs_detector = dfs_pattern_detector_init(&ar->ath_common,
10129                                                             NL80211_DFS_UNSET);
10130
10131                if (!ar->dfs_detector)
10132                        ath10k_warn(ar, "failed to initialise DFS pattern detector\n");
10133        }
10134
10135        ret = ath10k_mac_init_rd(ar);
10136        if (ret) {
10137                ath10k_err(ar, "failed to derive regdom: %d\n", ret);
10138                goto err_dfs_detector_exit;
10139        }
10140
10141        /* Disable set_coverage_class for chipsets that do not support it. */
10142        if (!ar->hw_params.hw_ops->set_coverage_class)
10143                ar->ops->set_coverage_class = NULL;
10144
10145        ret = ath_regd_init(&ar->ath_common.regulatory, ar->hw->wiphy,
10146                            ath10k_reg_notifier);
10147        if (ret) {
10148                ath10k_err(ar, "failed to initialise regulatory: %i\n", ret);
10149                goto err_dfs_detector_exit;
10150        }
10151
10152        if (test_bit(WMI_SERVICE_SPOOF_MAC_SUPPORT, ar->wmi.svc_map)) {
10153                ar->hw->wiphy->features |=
10154                        NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR;
10155        }
10156
10157        ar->hw->wiphy->cipher_suites = cipher_suites;
10158
10159        /* QCA988x and QCA6174 family chips do not support CCMP-256, GCMP-128
10160         * and GCMP-256 ciphers in hardware. Fetch number of ciphers supported
10161         * from chip specific hw_param table.
10162         */
10163        if (!ar->hw_params.n_cipher_suites ||
10164            ar->hw_params.n_cipher_suites > ARRAY_SIZE(cipher_suites)) {
10165                ath10k_err(ar, "invalid hw_params.n_cipher_suites %d\n",
10166                           ar->hw_params.n_cipher_suites);
10167                ar->hw_params.n_cipher_suites = 8;
10168        }
10169        ar->hw->wiphy->n_cipher_suites = ar->hw_params.n_cipher_suites;
10170
10171        wiphy_ext_feature_set(ar->hw->wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
10172
10173        ar->hw->weight_multiplier = ATH10K_AIRTIME_WEIGHT_MULTIPLIER;
10174
10175        ret = ieee80211_register_hw(ar->hw);
10176        if (ret) {
10177                ath10k_err(ar, "failed to register ieee80211: %d\n", ret);
10178                goto err_dfs_detector_exit;
10179        }
10180
10181        if (test_bit(WMI_SERVICE_PER_PACKET_SW_ENCRYPT, ar->wmi.svc_map)) {
10182                ar->hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_AP_VLAN);
10183                ar->hw->wiphy->software_iftypes |= BIT(NL80211_IFTYPE_AP_VLAN);
10184        }
10185
10186        if (!ath_is_world_regd(&ar->ath_common.regulatory)) {
10187                ret = regulatory_hint(ar->hw->wiphy,
10188                                      ar->ath_common.regulatory.alpha2);
10189                if (ret)
10190                        goto err_unregister;
10191        }
10192
10193        return 0;
10194
10195err_unregister:
10196        ieee80211_unregister_hw(ar->hw);
10197
10198err_dfs_detector_exit:
10199        if (IS_ENABLED(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector)
10200                ar->dfs_detector->exit(ar->dfs_detector);
10201
10202err_free:
10203        kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
10204        kfree(ar->mac.sbands[NL80211_BAND_5GHZ].channels);
10205
10206        SET_IEEE80211_DEV(ar->hw, NULL);
10207        return ret;
10208}
10209
10210void ath10k_mac_unregister(struct ath10k *ar)
10211{
10212        ieee80211_unregister_hw(ar->hw);
10213
10214        if (IS_ENABLED(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector)
10215                ar->dfs_detector->exit(ar->dfs_detector);
10216
10217        kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
10218        kfree(ar->mac.sbands[NL80211_BAND_5GHZ].channels);
10219
10220        SET_IEEE80211_DEV(ar->hw, NULL);
10221}
10222