linux/drivers/net/wireless/ath/ath11k/mac.c
<<
>>
Prefs
   1// SPDX-License-Identifier: BSD-3-Clause-Clear
   2/*
   3 * Copyright (c) 2018-2019 The Linux Foundation. All rights reserved.
   4 */
   5
   6#include <net/mac80211.h>
   7#include <linux/etherdevice.h>
   8#include "mac.h"
   9#include "core.h"
  10#include "debug.h"
  11#include "wmi.h"
  12#include "hw.h"
  13#include "dp_tx.h"
  14#include "dp_rx.h"
  15#include "testmode.h"
  16#include "peer.h"
  17#include "debugfs_sta.h"
  18
  19#define CHAN2G(_channel, _freq, _flags) { \
  20        .band                   = NL80211_BAND_2GHZ, \
  21        .hw_value               = (_channel), \
  22        .center_freq            = (_freq), \
  23        .flags                  = (_flags), \
  24        .max_antenna_gain       = 0, \
  25        .max_power              = 30, \
  26}
  27
  28#define CHAN5G(_channel, _freq, _flags) { \
  29        .band                   = NL80211_BAND_5GHZ, \
  30        .hw_value               = (_channel), \
  31        .center_freq            = (_freq), \
  32        .flags                  = (_flags), \
  33        .max_antenna_gain       = 0, \
  34        .max_power              = 30, \
  35}
  36
  37#define CHAN6G(_channel, _freq, _flags) { \
  38        .band                   = NL80211_BAND_6GHZ, \
  39        .hw_value               = (_channel), \
  40        .center_freq            = (_freq), \
  41        .flags                  = (_flags), \
  42        .max_antenna_gain       = 0, \
  43        .max_power              = 30, \
  44}
  45
  46static const struct ieee80211_channel ath11k_2ghz_channels[] = {
  47        CHAN2G(1, 2412, 0),
  48        CHAN2G(2, 2417, 0),
  49        CHAN2G(3, 2422, 0),
  50        CHAN2G(4, 2427, 0),
  51        CHAN2G(5, 2432, 0),
  52        CHAN2G(6, 2437, 0),
  53        CHAN2G(7, 2442, 0),
  54        CHAN2G(8, 2447, 0),
  55        CHAN2G(9, 2452, 0),
  56        CHAN2G(10, 2457, 0),
  57        CHAN2G(11, 2462, 0),
  58        CHAN2G(12, 2467, 0),
  59        CHAN2G(13, 2472, 0),
  60        CHAN2G(14, 2484, 0),
  61};
  62
  63static const struct ieee80211_channel ath11k_5ghz_channels[] = {
  64        CHAN5G(36, 5180, 0),
  65        CHAN5G(40, 5200, 0),
  66        CHAN5G(44, 5220, 0),
  67        CHAN5G(48, 5240, 0),
  68        CHAN5G(52, 5260, 0),
  69        CHAN5G(56, 5280, 0),
  70        CHAN5G(60, 5300, 0),
  71        CHAN5G(64, 5320, 0),
  72        CHAN5G(100, 5500, 0),
  73        CHAN5G(104, 5520, 0),
  74        CHAN5G(108, 5540, 0),
  75        CHAN5G(112, 5560, 0),
  76        CHAN5G(116, 5580, 0),
  77        CHAN5G(120, 5600, 0),
  78        CHAN5G(124, 5620, 0),
  79        CHAN5G(128, 5640, 0),
  80        CHAN5G(132, 5660, 0),
  81        CHAN5G(136, 5680, 0),
  82        CHAN5G(140, 5700, 0),
  83        CHAN5G(144, 5720, 0),
  84        CHAN5G(149, 5745, 0),
  85        CHAN5G(153, 5765, 0),
  86        CHAN5G(157, 5785, 0),
  87        CHAN5G(161, 5805, 0),
  88        CHAN5G(165, 5825, 0),
  89        CHAN5G(169, 5845, 0),
  90        CHAN5G(173, 5865, 0),
  91};
  92
  93static const struct ieee80211_channel ath11k_6ghz_channels[] = {
  94        CHAN6G(1, 5955, 0),
  95        CHAN6G(5, 5975, 0),
  96        CHAN6G(9, 5995, 0),
  97        CHAN6G(13, 6015, 0),
  98        CHAN6G(17, 6035, 0),
  99        CHAN6G(21, 6055, 0),
 100        CHAN6G(25, 6075, 0),
 101        CHAN6G(29, 6095, 0),
 102        CHAN6G(33, 6115, 0),
 103        CHAN6G(37, 6135, 0),
 104        CHAN6G(41, 6155, 0),
 105        CHAN6G(45, 6175, 0),
 106        CHAN6G(49, 6195, 0),
 107        CHAN6G(53, 6215, 0),
 108        CHAN6G(57, 6235, 0),
 109        CHAN6G(61, 6255, 0),
 110        CHAN6G(65, 6275, 0),
 111        CHAN6G(69, 6295, 0),
 112        CHAN6G(73, 6315, 0),
 113        CHAN6G(77, 6335, 0),
 114        CHAN6G(81, 6355, 0),
 115        CHAN6G(85, 6375, 0),
 116        CHAN6G(89, 6395, 0),
 117        CHAN6G(93, 6415, 0),
 118        CHAN6G(97, 6435, 0),
 119        CHAN6G(101, 6455, 0),
 120        CHAN6G(105, 6475, 0),
 121        CHAN6G(109, 6495, 0),
 122        CHAN6G(113, 6515, 0),
 123        CHAN6G(117, 6535, 0),
 124        CHAN6G(121, 6555, 0),
 125        CHAN6G(125, 6575, 0),
 126        CHAN6G(129, 6595, 0),
 127        CHAN6G(133, 6615, 0),
 128        CHAN6G(137, 6635, 0),
 129        CHAN6G(141, 6655, 0),
 130        CHAN6G(145, 6675, 0),
 131        CHAN6G(149, 6695, 0),
 132        CHAN6G(153, 6715, 0),
 133        CHAN6G(157, 6735, 0),
 134        CHAN6G(161, 6755, 0),
 135        CHAN6G(165, 6775, 0),
 136        CHAN6G(169, 6795, 0),
 137        CHAN6G(173, 6815, 0),
 138        CHAN6G(177, 6835, 0),
 139        CHAN6G(181, 6855, 0),
 140        CHAN6G(185, 6875, 0),
 141        CHAN6G(189, 6895, 0),
 142        CHAN6G(193, 6915, 0),
 143        CHAN6G(197, 6935, 0),
 144        CHAN6G(201, 6955, 0),
 145        CHAN6G(205, 6975, 0),
 146        CHAN6G(209, 6995, 0),
 147        CHAN6G(213, 7015, 0),
 148        CHAN6G(217, 7035, 0),
 149        CHAN6G(221, 7055, 0),
 150        CHAN6G(225, 7075, 0),
 151        CHAN6G(229, 7095, 0),
 152        CHAN6G(233, 7115, 0),
 153};
 154
 155static struct ieee80211_rate ath11k_legacy_rates[] = {
 156        { .bitrate = 10,
 157          .hw_value = ATH11K_HW_RATE_CCK_LP_1M },
 158        { .bitrate = 20,
 159          .hw_value = ATH11K_HW_RATE_CCK_LP_2M,
 160          .hw_value_short = ATH11K_HW_RATE_CCK_SP_2M,
 161          .flags = IEEE80211_RATE_SHORT_PREAMBLE },
 162        { .bitrate = 55,
 163          .hw_value = ATH11K_HW_RATE_CCK_LP_5_5M,
 164          .hw_value_short = ATH11K_HW_RATE_CCK_SP_5_5M,
 165          .flags = IEEE80211_RATE_SHORT_PREAMBLE },
 166        { .bitrate = 110,
 167          .hw_value = ATH11K_HW_RATE_CCK_LP_11M,
 168          .hw_value_short = ATH11K_HW_RATE_CCK_SP_11M,
 169          .flags = IEEE80211_RATE_SHORT_PREAMBLE },
 170
 171        { .bitrate = 60, .hw_value = ATH11K_HW_RATE_OFDM_6M },
 172        { .bitrate = 90, .hw_value = ATH11K_HW_RATE_OFDM_9M },
 173        { .bitrate = 120, .hw_value = ATH11K_HW_RATE_OFDM_12M },
 174        { .bitrate = 180, .hw_value = ATH11K_HW_RATE_OFDM_18M },
 175        { .bitrate = 240, .hw_value = ATH11K_HW_RATE_OFDM_24M },
 176        { .bitrate = 360, .hw_value = ATH11K_HW_RATE_OFDM_36M },
 177        { .bitrate = 480, .hw_value = ATH11K_HW_RATE_OFDM_48M },
 178        { .bitrate = 540, .hw_value = ATH11K_HW_RATE_OFDM_54M },
 179};
 180
 181static const int
 182ath11k_phymodes[NUM_NL80211_BANDS][ATH11K_CHAN_WIDTH_NUM] = {
 183        [NL80211_BAND_2GHZ] = {
 184                        [NL80211_CHAN_WIDTH_5] = MODE_UNKNOWN,
 185                        [NL80211_CHAN_WIDTH_10] = MODE_UNKNOWN,
 186                        [NL80211_CHAN_WIDTH_20_NOHT] = MODE_11AX_HE20_2G,
 187                        [NL80211_CHAN_WIDTH_20] = MODE_11AX_HE20_2G,
 188                        [NL80211_CHAN_WIDTH_40] = MODE_11AX_HE40_2G,
 189                        [NL80211_CHAN_WIDTH_80] = MODE_11AX_HE80_2G,
 190                        [NL80211_CHAN_WIDTH_80P80] = MODE_UNKNOWN,
 191                        [NL80211_CHAN_WIDTH_160] = MODE_UNKNOWN,
 192        },
 193        [NL80211_BAND_5GHZ] = {
 194                        [NL80211_CHAN_WIDTH_5] = MODE_UNKNOWN,
 195                        [NL80211_CHAN_WIDTH_10] = MODE_UNKNOWN,
 196                        [NL80211_CHAN_WIDTH_20_NOHT] = MODE_11AX_HE20,
 197                        [NL80211_CHAN_WIDTH_20] = MODE_11AX_HE20,
 198                        [NL80211_CHAN_WIDTH_40] = MODE_11AX_HE40,
 199                        [NL80211_CHAN_WIDTH_80] = MODE_11AX_HE80,
 200                        [NL80211_CHAN_WIDTH_160] = MODE_11AX_HE160,
 201                        [NL80211_CHAN_WIDTH_80P80] = MODE_11AX_HE80_80,
 202        },
 203        [NL80211_BAND_6GHZ] = {
 204                        [NL80211_CHAN_WIDTH_5] = MODE_UNKNOWN,
 205                        [NL80211_CHAN_WIDTH_10] = MODE_UNKNOWN,
 206                        [NL80211_CHAN_WIDTH_20_NOHT] = MODE_11AX_HE20,
 207                        [NL80211_CHAN_WIDTH_20] = MODE_11AX_HE20,
 208                        [NL80211_CHAN_WIDTH_40] = MODE_11AX_HE40,
 209                        [NL80211_CHAN_WIDTH_80] = MODE_11AX_HE80,
 210                        [NL80211_CHAN_WIDTH_160] = MODE_11AX_HE160,
 211                        [NL80211_CHAN_WIDTH_80P80] = MODE_11AX_HE80_80,
 212        },
 213
 214};
 215
 216const struct htt_rx_ring_tlv_filter ath11k_mac_mon_status_filter_default = {
 217        .rx_filter = HTT_RX_FILTER_TLV_FLAGS_MPDU_START |
 218                     HTT_RX_FILTER_TLV_FLAGS_PPDU_END |
 219                     HTT_RX_FILTER_TLV_FLAGS_PPDU_END_STATUS_DONE,
 220        .pkt_filter_flags0 = HTT_RX_FP_MGMT_FILTER_FLAGS0,
 221        .pkt_filter_flags1 = HTT_RX_FP_MGMT_FILTER_FLAGS1,
 222        .pkt_filter_flags2 = HTT_RX_FP_CTRL_FILTER_FLASG2,
 223        .pkt_filter_flags3 = HTT_RX_FP_DATA_FILTER_FLASG3 |
 224                             HTT_RX_FP_CTRL_FILTER_FLASG3
 225};
 226
 227#define ATH11K_MAC_FIRST_OFDM_RATE_IDX 4
 228#define ath11k_g_rates ath11k_legacy_rates
 229#define ath11k_g_rates_size (ARRAY_SIZE(ath11k_legacy_rates))
 230#define ath11k_a_rates (ath11k_legacy_rates + 4)
 231#define ath11k_a_rates_size (ARRAY_SIZE(ath11k_legacy_rates) - 4)
 232
 233#define ATH11K_MAC_SCAN_TIMEOUT_MSECS 200 /* in msecs */
 234
 235static const u32 ath11k_smps_map[] = {
 236        [WLAN_HT_CAP_SM_PS_STATIC] = WMI_PEER_SMPS_STATIC,
 237        [WLAN_HT_CAP_SM_PS_DYNAMIC] = WMI_PEER_SMPS_DYNAMIC,
 238        [WLAN_HT_CAP_SM_PS_INVALID] = WMI_PEER_SMPS_PS_NONE,
 239        [WLAN_HT_CAP_SM_PS_DISABLED] = WMI_PEER_SMPS_PS_NONE,
 240};
 241
 242static int ath11k_start_vdev_delay(struct ieee80211_hw *hw,
 243                                   struct ieee80211_vif *vif);
 244
 245u8 ath11k_mac_bw_to_mac80211_bw(u8 bw)
 246{
 247        u8 ret = 0;
 248
 249        switch (bw) {
 250        case ATH11K_BW_20:
 251                ret = RATE_INFO_BW_20;
 252                break;
 253        case ATH11K_BW_40:
 254                ret = RATE_INFO_BW_40;
 255                break;
 256        case ATH11K_BW_80:
 257                ret = RATE_INFO_BW_80;
 258                break;
 259        case ATH11K_BW_160:
 260                ret = RATE_INFO_BW_160;
 261                break;
 262        }
 263
 264        return ret;
 265}
 266
 267enum ath11k_supported_bw ath11k_mac_mac80211_bw_to_ath11k_bw(enum rate_info_bw bw)
 268{
 269        switch (bw) {
 270        case RATE_INFO_BW_20:
 271                return ATH11K_BW_20;
 272        case RATE_INFO_BW_40:
 273                return ATH11K_BW_40;
 274        case RATE_INFO_BW_80:
 275                return ATH11K_BW_80;
 276        case RATE_INFO_BW_160:
 277                return ATH11K_BW_160;
 278        default:
 279                return ATH11K_BW_20;
 280        }
 281}
 282
 283int ath11k_mac_hw_ratecode_to_legacy_rate(u8 hw_rc, u8 preamble, u8 *rateidx,
 284                                          u16 *rate)
 285{
 286        /* As default, it is OFDM rates */
 287        int i = ATH11K_MAC_FIRST_OFDM_RATE_IDX;
 288        int max_rates_idx = ath11k_g_rates_size;
 289
 290        if (preamble == WMI_RATE_PREAMBLE_CCK) {
 291                hw_rc &= ~ATH11k_HW_RATECODE_CCK_SHORT_PREAM_MASK;
 292                i = 0;
 293                max_rates_idx = ATH11K_MAC_FIRST_OFDM_RATE_IDX;
 294        }
 295
 296        while (i < max_rates_idx) {
 297                if (hw_rc == ath11k_legacy_rates[i].hw_value) {
 298                        *rateidx = i;
 299                        *rate = ath11k_legacy_rates[i].bitrate;
 300                        return 0;
 301                }
 302                i++;
 303        }
 304
 305        return -EINVAL;
 306}
 307
 308static int get_num_chains(u32 mask)
 309{
 310        int num_chains = 0;
 311
 312        while (mask) {
 313                if (mask & BIT(0))
 314                        num_chains++;
 315                mask >>= 1;
 316        }
 317
 318        return num_chains;
 319}
 320
 321u8 ath11k_mac_bitrate_to_idx(const struct ieee80211_supported_band *sband,
 322                             u32 bitrate)
 323{
 324        int i;
 325
 326        for (i = 0; i < sband->n_bitrates; i++)
 327                if (sband->bitrates[i].bitrate == bitrate)
 328                        return i;
 329
 330        return 0;
 331}
 332
 333static u32
 334ath11k_mac_max_ht_nss(const u8 ht_mcs_mask[IEEE80211_HT_MCS_MASK_LEN])
 335{
 336        int nss;
 337
 338        for (nss = IEEE80211_HT_MCS_MASK_LEN - 1; nss >= 0; nss--)
 339                if (ht_mcs_mask[nss])
 340                        return nss + 1;
 341
 342        return 1;
 343}
 344
 345static u32
 346ath11k_mac_max_vht_nss(const u16 vht_mcs_mask[NL80211_VHT_NSS_MAX])
 347{
 348        int nss;
 349
 350        for (nss = NL80211_VHT_NSS_MAX - 1; nss >= 0; nss--)
 351                if (vht_mcs_mask[nss])
 352                        return nss + 1;
 353
 354        return 1;
 355}
 356
 357static u8 ath11k_parse_mpdudensity(u8 mpdudensity)
 358{
 359/* 802.11n D2.0 defined values for "Minimum MPDU Start Spacing":
 360 *   0 for no restriction
 361 *   1 for 1/4 us
 362 *   2 for 1/2 us
 363 *   3 for 1 us
 364 *   4 for 2 us
 365 *   5 for 4 us
 366 *   6 for 8 us
 367 *   7 for 16 us
 368 */
 369        switch (mpdudensity) {
 370        case 0:
 371                return 0;
 372        case 1:
 373        case 2:
 374        case 3:
 375        /* Our lower layer calculations limit our precision to
 376         * 1 microsecond
 377         */
 378                return 1;
 379        case 4:
 380                return 2;
 381        case 5:
 382                return 4;
 383        case 6:
 384                return 8;
 385        case 7:
 386                return 16;
 387        default:
 388                return 0;
 389        }
 390}
 391
 392static int ath11k_mac_vif_chan(struct ieee80211_vif *vif,
 393                               struct cfg80211_chan_def *def)
 394{
 395        struct ieee80211_chanctx_conf *conf;
 396
 397        rcu_read_lock();
 398        conf = rcu_dereference(vif->chanctx_conf);
 399        if (!conf) {
 400                rcu_read_unlock();
 401                return -ENOENT;
 402        }
 403
 404        *def = conf->def;
 405        rcu_read_unlock();
 406
 407        return 0;
 408}
 409
 410static bool ath11k_mac_bitrate_is_cck(int bitrate)
 411{
 412        switch (bitrate) {
 413        case 10:
 414        case 20:
 415        case 55:
 416        case 110:
 417                return true;
 418        }
 419
 420        return false;
 421}
 422
 423u8 ath11k_mac_hw_rate_to_idx(const struct ieee80211_supported_band *sband,
 424                             u8 hw_rate, bool cck)
 425{
 426        const struct ieee80211_rate *rate;
 427        int i;
 428
 429        for (i = 0; i < sband->n_bitrates; i++) {
 430                rate = &sband->bitrates[i];
 431
 432                if (ath11k_mac_bitrate_is_cck(rate->bitrate) != cck)
 433                        continue;
 434
 435                if (rate->hw_value == hw_rate)
 436                        return i;
 437                else if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE &&
 438                         rate->hw_value_short == hw_rate)
 439                        return i;
 440        }
 441
 442        return 0;
 443}
 444
 445static u8 ath11k_mac_bitrate_to_rate(int bitrate)
 446{
 447        return DIV_ROUND_UP(bitrate, 5) |
 448               (ath11k_mac_bitrate_is_cck(bitrate) ? BIT(7) : 0);
 449}
 450
 451static void ath11k_get_arvif_iter(void *data, u8 *mac,
 452                                  struct ieee80211_vif *vif)
 453{
 454        struct ath11k_vif_iter *arvif_iter = data;
 455        struct ath11k_vif *arvif = (void *)vif->drv_priv;
 456
 457        if (arvif->vdev_id == arvif_iter->vdev_id)
 458                arvif_iter->arvif = arvif;
 459}
 460
 461struct ath11k_vif *ath11k_mac_get_arvif(struct ath11k *ar, u32 vdev_id)
 462{
 463        struct ath11k_vif_iter arvif_iter;
 464        u32 flags;
 465
 466        memset(&arvif_iter, 0, sizeof(struct ath11k_vif_iter));
 467        arvif_iter.vdev_id = vdev_id;
 468
 469        flags = IEEE80211_IFACE_ITER_RESUME_ALL;
 470        ieee80211_iterate_active_interfaces_atomic(ar->hw,
 471                                                   flags,
 472                                                   ath11k_get_arvif_iter,
 473                                                   &arvif_iter);
 474        if (!arvif_iter.arvif) {
 475                ath11k_warn(ar->ab, "No VIF found for vdev %d\n", vdev_id);
 476                return NULL;
 477        }
 478
 479        return arvif_iter.arvif;
 480}
 481
 482struct ath11k_vif *ath11k_mac_get_arvif_by_vdev_id(struct ath11k_base *ab,
 483                                                   u32 vdev_id)
 484{
 485        int i;
 486        struct ath11k_pdev *pdev;
 487        struct ath11k_vif *arvif;
 488
 489        for (i = 0; i < ab->num_radios; i++) {
 490                pdev = rcu_dereference(ab->pdevs_active[i]);
 491                if (pdev && pdev->ar) {
 492                        arvif = ath11k_mac_get_arvif(pdev->ar, vdev_id);
 493                        if (arvif)
 494                                return arvif;
 495                }
 496        }
 497
 498        return NULL;
 499}
 500
 501struct ath11k *ath11k_mac_get_ar_by_vdev_id(struct ath11k_base *ab, u32 vdev_id)
 502{
 503        int i;
 504        struct ath11k_pdev *pdev;
 505
 506        for (i = 0; i < ab->num_radios; i++) {
 507                pdev = rcu_dereference(ab->pdevs_active[i]);
 508                if (pdev && pdev->ar) {
 509                        if (pdev->ar->allocated_vdev_map & (1LL << vdev_id))
 510                                return pdev->ar;
 511                }
 512        }
 513
 514        return NULL;
 515}
 516
 517struct ath11k *ath11k_mac_get_ar_by_pdev_id(struct ath11k_base *ab, u32 pdev_id)
 518{
 519        int i;
 520        struct ath11k_pdev *pdev;
 521
 522        if (ab->hw_params.single_pdev_only) {
 523                pdev = rcu_dereference(ab->pdevs_active[0]);
 524                return pdev ? pdev->ar : NULL;
 525        }
 526
 527        if (WARN_ON(pdev_id > ab->num_radios))
 528                return NULL;
 529
 530        for (i = 0; i < ab->num_radios; i++) {
 531                pdev = rcu_dereference(ab->pdevs_active[i]);
 532
 533                if (pdev && pdev->pdev_id == pdev_id)
 534                        return (pdev->ar ? pdev->ar : NULL);
 535        }
 536
 537        return NULL;
 538}
 539
 540static void ath11k_pdev_caps_update(struct ath11k *ar)
 541{
 542        struct ath11k_base *ab = ar->ab;
 543
 544        ar->max_tx_power = ab->target_caps.hw_max_tx_power;
 545
 546        /* FIXME Set min_tx_power to ab->target_caps.hw_min_tx_power.
 547         * But since the received value in svcrdy is same as hw_max_tx_power,
 548         * we can set ar->min_tx_power to 0 currently until
 549         * this is fixed in firmware
 550         */
 551        ar->min_tx_power = 0;
 552
 553        ar->txpower_limit_2g = ar->max_tx_power;
 554        ar->txpower_limit_5g = ar->max_tx_power;
 555        ar->txpower_scale = WMI_HOST_TP_SCALE_MAX;
 556}
 557
 558static int ath11k_mac_txpower_recalc(struct ath11k *ar)
 559{
 560        struct ath11k_pdev *pdev = ar->pdev;
 561        struct ath11k_vif *arvif;
 562        int ret, txpower = -1;
 563        u32 param;
 564
 565        lockdep_assert_held(&ar->conf_mutex);
 566
 567        list_for_each_entry(arvif, &ar->arvifs, list) {
 568                if (arvif->txpower <= 0)
 569                        continue;
 570
 571                if (txpower == -1)
 572                        txpower = arvif->txpower;
 573                else
 574                        txpower = min(txpower, arvif->txpower);
 575        }
 576
 577        if (txpower == -1)
 578                return 0;
 579
 580        /* txpwr is set as 2 units per dBm in FW*/
 581        txpower = min_t(u32, max_t(u32, ar->min_tx_power, txpower),
 582                        ar->max_tx_power) * 2;
 583
 584        ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "txpower to set in hw %d\n",
 585                   txpower / 2);
 586
 587        if ((pdev->cap.supported_bands & WMI_HOST_WLAN_2G_CAP) &&
 588            ar->txpower_limit_2g != txpower) {
 589                param = WMI_PDEV_PARAM_TXPOWER_LIMIT2G;
 590                ret = ath11k_wmi_pdev_set_param(ar, param,
 591                                                txpower, ar->pdev->pdev_id);
 592                if (ret)
 593                        goto fail;
 594                ar->txpower_limit_2g = txpower;
 595        }
 596
 597        if ((pdev->cap.supported_bands & WMI_HOST_WLAN_5G_CAP) &&
 598            ar->txpower_limit_5g != txpower) {
 599                param = WMI_PDEV_PARAM_TXPOWER_LIMIT5G;
 600                ret = ath11k_wmi_pdev_set_param(ar, param,
 601                                                txpower, ar->pdev->pdev_id);
 602                if (ret)
 603                        goto fail;
 604                ar->txpower_limit_5g = txpower;
 605        }
 606
 607        return 0;
 608
 609fail:
 610        ath11k_warn(ar->ab, "failed to recalc txpower limit %d using pdev param %d: %d\n",
 611                    txpower / 2, param, ret);
 612        return ret;
 613}
 614
 615static int ath11k_recalc_rtscts_prot(struct ath11k_vif *arvif)
 616{
 617        struct ath11k *ar = arvif->ar;
 618        u32 vdev_param, rts_cts = 0;
 619        int ret;
 620
 621        lockdep_assert_held(&ar->conf_mutex);
 622
 623        vdev_param = WMI_VDEV_PARAM_ENABLE_RTSCTS;
 624
 625        /* Enable RTS/CTS protection for sw retries (when legacy stations
 626         * are in BSS) or by default only for second rate series.
 627         * TODO: Check if we need to enable CTS 2 Self in any case
 628         */
 629        rts_cts = WMI_USE_RTS_CTS;
 630
 631        if (arvif->num_legacy_stations > 0)
 632                rts_cts |= WMI_RTSCTS_ACROSS_SW_RETRIES << 4;
 633        else
 634                rts_cts |= WMI_RTSCTS_FOR_SECOND_RATESERIES << 4;
 635
 636        /* Need not send duplicate param value to firmware */
 637        if (arvif->rtscts_prot_mode == rts_cts)
 638                return 0;
 639
 640        arvif->rtscts_prot_mode = rts_cts;
 641
 642        ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac vdev %d recalc rts/cts prot %d\n",
 643                   arvif->vdev_id, rts_cts);
 644
 645        ret =  ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
 646                                             vdev_param, rts_cts);
 647        if (ret)
 648                ath11k_warn(ar->ab, "failed to recalculate rts/cts prot for vdev %d: %d\n",
 649                            arvif->vdev_id, ret);
 650
 651        return ret;
 652}
 653
 654static int ath11k_mac_set_kickout(struct ath11k_vif *arvif)
 655{
 656        struct ath11k *ar = arvif->ar;
 657        u32 param;
 658        int ret;
 659
 660        ret = ath11k_wmi_pdev_set_param(ar, WMI_PDEV_PARAM_STA_KICKOUT_TH,
 661                                        ATH11K_KICKOUT_THRESHOLD,
 662                                        ar->pdev->pdev_id);
 663        if (ret) {
 664                ath11k_warn(ar->ab, "failed to set kickout threshold on vdev %i: %d\n",
 665                            arvif->vdev_id, ret);
 666                return ret;
 667        }
 668
 669        param = WMI_VDEV_PARAM_AP_KEEPALIVE_MIN_IDLE_INACTIVE_TIME_SECS;
 670        ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param,
 671                                            ATH11K_KEEPALIVE_MIN_IDLE);
 672        if (ret) {
 673                ath11k_warn(ar->ab, "failed to set keepalive minimum idle time on vdev %i: %d\n",
 674                            arvif->vdev_id, ret);
 675                return ret;
 676        }
 677
 678        param = WMI_VDEV_PARAM_AP_KEEPALIVE_MAX_IDLE_INACTIVE_TIME_SECS;
 679        ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param,
 680                                            ATH11K_KEEPALIVE_MAX_IDLE);
 681        if (ret) {
 682                ath11k_warn(ar->ab, "failed to set keepalive maximum idle time on vdev %i: %d\n",
 683                            arvif->vdev_id, ret);
 684                return ret;
 685        }
 686
 687        param = WMI_VDEV_PARAM_AP_KEEPALIVE_MAX_UNRESPONSIVE_TIME_SECS;
 688        ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, param,
 689                                            ATH11K_KEEPALIVE_MAX_UNRESPONSIVE);
 690        if (ret) {
 691                ath11k_warn(ar->ab, "failed to set keepalive maximum unresponsive time on vdev %i: %d\n",
 692                            arvif->vdev_id, ret);
 693                return ret;
 694        }
 695
 696        return 0;
 697}
 698
 699void ath11k_mac_peer_cleanup_all(struct ath11k *ar)
 700{
 701        struct ath11k_peer *peer, *tmp;
 702        struct ath11k_base *ab = ar->ab;
 703
 704        lockdep_assert_held(&ar->conf_mutex);
 705
 706        spin_lock_bh(&ab->base_lock);
 707        list_for_each_entry_safe(peer, tmp, &ab->peers, list) {
 708                ath11k_peer_rx_tid_cleanup(ar, peer);
 709                list_del(&peer->list);
 710                kfree(peer);
 711        }
 712        spin_unlock_bh(&ab->base_lock);
 713
 714        ar->num_peers = 0;
 715        ar->num_stations = 0;
 716}
 717
 718static int ath11k_monitor_vdev_up(struct ath11k *ar, int vdev_id)
 719{
 720        int ret = 0;
 721
 722        ret = ath11k_wmi_vdev_up(ar, vdev_id, 0, ar->mac_addr);
 723        if (ret) {
 724                ath11k_warn(ar->ab, "failed to put up monitor vdev %i: %d\n",
 725                            vdev_id, ret);
 726                return ret;
 727        }
 728
 729        ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac monitor vdev %i started\n",
 730                   vdev_id);
 731        return 0;
 732}
 733
 734static int ath11k_mac_op_config(struct ieee80211_hw *hw, u32 changed)
 735{
 736        /* mac80211 requires this op to be present and that's why
 737         * there's an empty function, this can be extended when
 738         * required.
 739         */
 740
 741        return 0;
 742}
 743
 744static int ath11k_mac_setup_bcn_tmpl(struct ath11k_vif *arvif)
 745{
 746        struct ath11k *ar = arvif->ar;
 747        struct ath11k_base *ab = ar->ab;
 748        struct ieee80211_hw *hw = ar->hw;
 749        struct ieee80211_vif *vif = arvif->vif;
 750        struct ieee80211_mutable_offsets offs = {};
 751        struct sk_buff *bcn;
 752        struct ieee80211_mgmt *mgmt;
 753        u8 *ies;
 754        int ret;
 755
 756        if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
 757                return 0;
 758
 759        bcn = ieee80211_beacon_get_template(hw, vif, &offs);
 760        if (!bcn) {
 761                ath11k_warn(ab, "failed to get beacon template from mac80211\n");
 762                return -EPERM;
 763        }
 764
 765        ies = bcn->data + ieee80211_get_hdrlen_from_skb(bcn);
 766        ies += sizeof(mgmt->u.beacon);
 767
 768        if (cfg80211_find_ie(WLAN_EID_RSN, ies, (skb_tail_pointer(bcn) - ies)))
 769                arvif->rsnie_present = true;
 770
 771        if (cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
 772                                    WLAN_OUI_TYPE_MICROSOFT_WPA,
 773                                    ies, (skb_tail_pointer(bcn) - ies)))
 774                arvif->wpaie_present = true;
 775
 776        ret = ath11k_wmi_bcn_tmpl(ar, arvif->vdev_id, &offs, bcn);
 777
 778        kfree_skb(bcn);
 779
 780        if (ret)
 781                ath11k_warn(ab, "failed to submit beacon template command: %d\n",
 782                            ret);
 783
 784        return ret;
 785}
 786
 787static void ath11k_control_beaconing(struct ath11k_vif *arvif,
 788                                     struct ieee80211_bss_conf *info)
 789{
 790        struct ath11k *ar = arvif->ar;
 791        int ret = 0;
 792
 793        lockdep_assert_held(&arvif->ar->conf_mutex);
 794
 795        if (!info->enable_beacon) {
 796                ret = ath11k_wmi_vdev_down(ar, arvif->vdev_id);
 797                if (ret)
 798                        ath11k_warn(ar->ab, "failed to down vdev_id %i: %d\n",
 799                                    arvif->vdev_id, ret);
 800
 801                arvif->is_up = false;
 802                return;
 803        }
 804
 805        /* Install the beacon template to the FW */
 806        ret = ath11k_mac_setup_bcn_tmpl(arvif);
 807        if (ret) {
 808                ath11k_warn(ar->ab, "failed to update bcn tmpl during vdev up: %d\n",
 809                            ret);
 810                return;
 811        }
 812
 813        arvif->tx_seq_no = 0x1000;
 814
 815        arvif->aid = 0;
 816
 817        ether_addr_copy(arvif->bssid, info->bssid);
 818
 819        ret = ath11k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
 820                                 arvif->bssid);
 821        if (ret) {
 822                ath11k_warn(ar->ab, "failed to bring up vdev %d: %i\n",
 823                            arvif->vdev_id, ret);
 824                return;
 825        }
 826
 827        arvif->is_up = true;
 828
 829        ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac vdev %d up\n", arvif->vdev_id);
 830}
 831
 832static void ath11k_mac_handle_beacon_iter(void *data, u8 *mac,
 833                                          struct ieee80211_vif *vif)
 834{
 835        struct sk_buff *skb = data;
 836        struct ieee80211_mgmt *mgmt = (void *)skb->data;
 837        struct ath11k_vif *arvif = (void *)vif->drv_priv;
 838
 839        if (vif->type != NL80211_IFTYPE_STATION)
 840                return;
 841
 842        if (!ether_addr_equal(mgmt->bssid, vif->bss_conf.bssid))
 843                return;
 844
 845        cancel_delayed_work(&arvif->connection_loss_work);
 846}
 847
 848void ath11k_mac_handle_beacon(struct ath11k *ar, struct sk_buff *skb)
 849{
 850        ieee80211_iterate_active_interfaces_atomic(ar->hw,
 851                                                   IEEE80211_IFACE_ITER_NORMAL,
 852                                                   ath11k_mac_handle_beacon_iter,
 853                                                   skb);
 854}
 855
 856static void ath11k_mac_handle_beacon_miss_iter(void *data, u8 *mac,
 857                                               struct ieee80211_vif *vif)
 858{
 859        u32 *vdev_id = data;
 860        struct ath11k_vif *arvif = (void *)vif->drv_priv;
 861        struct ath11k *ar = arvif->ar;
 862        struct ieee80211_hw *hw = ar->hw;
 863
 864        if (arvif->vdev_id != *vdev_id)
 865                return;
 866
 867        if (!arvif->is_up)
 868                return;
 869
 870        ieee80211_beacon_loss(vif);
 871
 872        /* Firmware doesn't report beacon loss events repeatedly. If AP probe
 873         * (done by mac80211) succeeds but beacons do not resume then it
 874         * doesn't make sense to continue operation. Queue connection loss work
 875         * which can be cancelled when beacon is received.
 876         */
 877        ieee80211_queue_delayed_work(hw, &arvif->connection_loss_work,
 878                                     ATH11K_CONNECTION_LOSS_HZ);
 879}
 880
 881void ath11k_mac_handle_beacon_miss(struct ath11k *ar, u32 vdev_id)
 882{
 883        ieee80211_iterate_active_interfaces_atomic(ar->hw,
 884                                                   IEEE80211_IFACE_ITER_NORMAL,
 885                                                   ath11k_mac_handle_beacon_miss_iter,
 886                                                   &vdev_id);
 887}
 888
 889static void ath11k_mac_vif_sta_connection_loss_work(struct work_struct *work)
 890{
 891        struct ath11k_vif *arvif = container_of(work, struct ath11k_vif,
 892                                                connection_loss_work.work);
 893        struct ieee80211_vif *vif = arvif->vif;
 894
 895        if (!arvif->is_up)
 896                return;
 897
 898        ieee80211_connection_loss(vif);
 899}
 900
 901static void ath11k_peer_assoc_h_basic(struct ath11k *ar,
 902                                      struct ieee80211_vif *vif,
 903                                      struct ieee80211_sta *sta,
 904                                      struct peer_assoc_params *arg)
 905{
 906        struct ath11k_vif *arvif = (void *)vif->drv_priv;
 907        u32 aid;
 908
 909        lockdep_assert_held(&ar->conf_mutex);
 910
 911        if (vif->type == NL80211_IFTYPE_STATION)
 912                aid = vif->bss_conf.aid;
 913        else
 914                aid = sta->aid;
 915
 916        ether_addr_copy(arg->peer_mac, sta->addr);
 917        arg->vdev_id = arvif->vdev_id;
 918        arg->peer_associd = aid;
 919        arg->auth_flag = true;
 920        /* TODO: STA WAR in ath10k for listen interval required? */
 921        arg->peer_listen_intval = ar->hw->conf.listen_interval;
 922        arg->peer_nss = 1;
 923        arg->peer_caps = vif->bss_conf.assoc_capability;
 924}
 925
 926static void ath11k_peer_assoc_h_crypto(struct ath11k *ar,
 927                                       struct ieee80211_vif *vif,
 928                                       struct ieee80211_sta *sta,
 929                                       struct peer_assoc_params *arg)
 930{
 931        struct ieee80211_bss_conf *info = &vif->bss_conf;
 932        struct cfg80211_chan_def def;
 933        struct cfg80211_bss *bss;
 934        struct ath11k_vif *arvif = (struct ath11k_vif *)vif->drv_priv;
 935        const u8 *rsnie = NULL;
 936        const u8 *wpaie = NULL;
 937
 938        lockdep_assert_held(&ar->conf_mutex);
 939
 940        if (WARN_ON(ath11k_mac_vif_chan(vif, &def)))
 941                return;
 942
 943        bss = cfg80211_get_bss(ar->hw->wiphy, def.chan, info->bssid, NULL, 0,
 944                               IEEE80211_BSS_TYPE_ANY, IEEE80211_PRIVACY_ANY);
 945
 946        if (arvif->rsnie_present || arvif->wpaie_present) {
 947                arg->need_ptk_4_way = true;
 948                if (arvif->wpaie_present)
 949                        arg->need_gtk_2_way = true;
 950        } else if (bss) {
 951                const struct cfg80211_bss_ies *ies;
 952
 953                rcu_read_lock();
 954                rsnie = ieee80211_bss_get_ie(bss, WLAN_EID_RSN);
 955
 956                ies = rcu_dereference(bss->ies);
 957
 958                wpaie = cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
 959                                                WLAN_OUI_TYPE_MICROSOFT_WPA,
 960                                                ies->data,
 961                                                ies->len);
 962                rcu_read_unlock();
 963                cfg80211_put_bss(ar->hw->wiphy, bss);
 964        }
 965
 966        /* FIXME: base on RSN IE/WPA IE is a correct idea? */
 967        if (rsnie || wpaie) {
 968                ath11k_dbg(ar->ab, ATH11K_DBG_WMI,
 969                           "%s: rsn ie found\n", __func__);
 970                arg->need_ptk_4_way = true;
 971        }
 972
 973        if (wpaie) {
 974                ath11k_dbg(ar->ab, ATH11K_DBG_WMI,
 975                           "%s: wpa ie found\n", __func__);
 976                arg->need_gtk_2_way = true;
 977        }
 978
 979        if (sta->mfp) {
 980                /* TODO: Need to check if FW supports PMF? */
 981                arg->is_pmf_enabled = true;
 982        }
 983
 984        /* TODO: safe_mode_enabled (bypass 4-way handshake) flag req? */
 985}
 986
 987static void ath11k_peer_assoc_h_rates(struct ath11k *ar,
 988                                      struct ieee80211_vif *vif,
 989                                      struct ieee80211_sta *sta,
 990                                      struct peer_assoc_params *arg)
 991{
 992        struct ath11k_vif *arvif = (void *)vif->drv_priv;
 993        struct wmi_rate_set_arg *rateset = &arg->peer_legacy_rates;
 994        struct cfg80211_chan_def def;
 995        const struct ieee80211_supported_band *sband;
 996        const struct ieee80211_rate *rates;
 997        enum nl80211_band band;
 998        u32 ratemask;
 999        u8 rate;
1000        int i;
1001
1002        lockdep_assert_held(&ar->conf_mutex);
1003
1004        if (WARN_ON(ath11k_mac_vif_chan(vif, &def)))
1005                return;
1006
1007        band = def.chan->band;
1008        sband = ar->hw->wiphy->bands[band];
1009        ratemask = sta->supp_rates[band];
1010        ratemask &= arvif->bitrate_mask.control[band].legacy;
1011        rates = sband->bitrates;
1012
1013        rateset->num_rates = 0;
1014
1015        for (i = 0; i < 32; i++, ratemask >>= 1, rates++) {
1016                if (!(ratemask & 1))
1017                        continue;
1018
1019                rate = ath11k_mac_bitrate_to_rate(rates->bitrate);
1020                rateset->rates[rateset->num_rates] = rate;
1021                rateset->num_rates++;
1022        }
1023}
1024
1025static bool
1026ath11k_peer_assoc_h_ht_masked(const u8 ht_mcs_mask[IEEE80211_HT_MCS_MASK_LEN])
1027{
1028        int nss;
1029
1030        for (nss = 0; nss < IEEE80211_HT_MCS_MASK_LEN; nss++)
1031                if (ht_mcs_mask[nss])
1032                        return false;
1033
1034        return true;
1035}
1036
1037static bool
1038ath11k_peer_assoc_h_vht_masked(const u16 vht_mcs_mask[NL80211_VHT_NSS_MAX])
1039{
1040        int nss;
1041
1042        for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++)
1043                if (vht_mcs_mask[nss])
1044                        return false;
1045
1046        return true;
1047}
1048
1049static void ath11k_peer_assoc_h_ht(struct ath11k *ar,
1050                                   struct ieee80211_vif *vif,
1051                                   struct ieee80211_sta *sta,
1052                                   struct peer_assoc_params *arg)
1053{
1054        const struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
1055        struct ath11k_vif *arvif = (void *)vif->drv_priv;
1056        struct cfg80211_chan_def def;
1057        enum nl80211_band band;
1058        const u8 *ht_mcs_mask;
1059        int i, n;
1060        u8 max_nss;
1061        u32 stbc;
1062
1063        lockdep_assert_held(&ar->conf_mutex);
1064
1065        if (WARN_ON(ath11k_mac_vif_chan(vif, &def)))
1066                return;
1067
1068        if (!ht_cap->ht_supported)
1069                return;
1070
1071        band = def.chan->band;
1072        ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
1073
1074        if (ath11k_peer_assoc_h_ht_masked(ht_mcs_mask))
1075                return;
1076
1077        arg->ht_flag = true;
1078
1079        arg->peer_max_mpdu = (1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
1080                                    ht_cap->ampdu_factor)) - 1;
1081
1082        arg->peer_mpdu_density =
1083                ath11k_parse_mpdudensity(ht_cap->ampdu_density);
1084
1085        arg->peer_ht_caps = ht_cap->cap;
1086        arg->peer_rate_caps |= WMI_HOST_RC_HT_FLAG;
1087
1088        if (ht_cap->cap & IEEE80211_HT_CAP_LDPC_CODING)
1089                arg->ldpc_flag = true;
1090
1091        if (sta->bandwidth >= IEEE80211_STA_RX_BW_40) {
1092                arg->bw_40 = true;
1093                arg->peer_rate_caps |= WMI_HOST_RC_CW40_FLAG;
1094        }
1095
1096        if (arvif->bitrate_mask.control[band].gi != NL80211_TXRATE_FORCE_LGI) {
1097                if (ht_cap->cap & (IEEE80211_HT_CAP_SGI_20 |
1098                    IEEE80211_HT_CAP_SGI_40))
1099                        arg->peer_rate_caps |= WMI_HOST_RC_SGI_FLAG;
1100        }
1101
1102        if (ht_cap->cap & IEEE80211_HT_CAP_TX_STBC) {
1103                arg->peer_rate_caps |= WMI_HOST_RC_TX_STBC_FLAG;
1104                arg->stbc_flag = true;
1105        }
1106
1107        if (ht_cap->cap & IEEE80211_HT_CAP_RX_STBC) {
1108                stbc = ht_cap->cap & IEEE80211_HT_CAP_RX_STBC;
1109                stbc = stbc >> IEEE80211_HT_CAP_RX_STBC_SHIFT;
1110                stbc = stbc << WMI_HOST_RC_RX_STBC_FLAG_S;
1111                arg->peer_rate_caps |= stbc;
1112                arg->stbc_flag = true;
1113        }
1114
1115        if (ht_cap->mcs.rx_mask[1] && ht_cap->mcs.rx_mask[2])
1116                arg->peer_rate_caps |= WMI_HOST_RC_TS_FLAG;
1117        else if (ht_cap->mcs.rx_mask[1])
1118                arg->peer_rate_caps |= WMI_HOST_RC_DS_FLAG;
1119
1120        for (i = 0, n = 0, max_nss = 0; i < IEEE80211_HT_MCS_MASK_LEN * 8; i++)
1121                if ((ht_cap->mcs.rx_mask[i / 8] & BIT(i % 8)) &&
1122                    (ht_mcs_mask[i / 8] & BIT(i % 8))) {
1123                        max_nss = (i / 8) + 1;
1124                        arg->peer_ht_rates.rates[n++] = i;
1125                }
1126
1127        /* This is a workaround for HT-enabled STAs which break the spec
1128         * and have no HT capabilities RX mask (no HT RX MCS map).
1129         *
1130         * As per spec, in section 20.3.5 Modulation and coding scheme (MCS),
1131         * MCS 0 through 7 are mandatory in 20MHz with 800 ns GI at all STAs.
1132         *
1133         * Firmware asserts if such situation occurs.
1134         */
1135        if (n == 0) {
1136                arg->peer_ht_rates.num_rates = 8;
1137                for (i = 0; i < arg->peer_ht_rates.num_rates; i++)
1138                        arg->peer_ht_rates.rates[i] = i;
1139        } else {
1140                arg->peer_ht_rates.num_rates = n;
1141                arg->peer_nss = min(sta->rx_nss, max_nss);
1142        }
1143
1144        ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac ht peer %pM mcs cnt %d nss %d\n",
1145                   arg->peer_mac,
1146                   arg->peer_ht_rates.num_rates,
1147                   arg->peer_nss);
1148}
1149
1150static int ath11k_mac_get_max_vht_mcs_map(u16 mcs_map, int nss)
1151{
1152        switch ((mcs_map >> (2 * nss)) & 0x3) {
1153        case IEEE80211_VHT_MCS_SUPPORT_0_7: return BIT(8) - 1;
1154        case IEEE80211_VHT_MCS_SUPPORT_0_8: return BIT(9) - 1;
1155        case IEEE80211_VHT_MCS_SUPPORT_0_9: return BIT(10) - 1;
1156        }
1157        return 0;
1158}
1159
1160static u16
1161ath11k_peer_assoc_h_vht_limit(u16 tx_mcs_set,
1162                              const u16 vht_mcs_limit[NL80211_VHT_NSS_MAX])
1163{
1164        int idx_limit;
1165        int nss;
1166        u16 mcs_map;
1167        u16 mcs;
1168
1169        for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++) {
1170                mcs_map = ath11k_mac_get_max_vht_mcs_map(tx_mcs_set, nss) &
1171                          vht_mcs_limit[nss];
1172
1173                if (mcs_map)
1174                        idx_limit = fls(mcs_map) - 1;
1175                else
1176                        idx_limit = -1;
1177
1178                switch (idx_limit) {
1179                case 0:
1180                case 1:
1181                case 2:
1182                case 3:
1183                case 4:
1184                case 5:
1185                case 6:
1186                case 7:
1187                        mcs = IEEE80211_VHT_MCS_SUPPORT_0_7;
1188                        break;
1189                case 8:
1190                        mcs = IEEE80211_VHT_MCS_SUPPORT_0_8;
1191                        break;
1192                case 9:
1193                        mcs = IEEE80211_VHT_MCS_SUPPORT_0_9;
1194                        break;
1195                default:
1196                        WARN_ON(1);
1197                        fallthrough;
1198                case -1:
1199                        mcs = IEEE80211_VHT_MCS_NOT_SUPPORTED;
1200                        break;
1201                }
1202
1203                tx_mcs_set &= ~(0x3 << (nss * 2));
1204                tx_mcs_set |= mcs << (nss * 2);
1205        }
1206
1207        return tx_mcs_set;
1208}
1209
1210static void ath11k_peer_assoc_h_vht(struct ath11k *ar,
1211                                    struct ieee80211_vif *vif,
1212                                    struct ieee80211_sta *sta,
1213                                    struct peer_assoc_params *arg)
1214{
1215        const struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap;
1216        struct ath11k_vif *arvif = (void *)vif->drv_priv;
1217        struct cfg80211_chan_def def;
1218        enum nl80211_band band;
1219        const u16 *vht_mcs_mask;
1220        u8 ampdu_factor;
1221        u8 max_nss, vht_mcs;
1222        int i;
1223
1224        if (WARN_ON(ath11k_mac_vif_chan(vif, &def)))
1225                return;
1226
1227        if (!vht_cap->vht_supported)
1228                return;
1229
1230        band = def.chan->band;
1231        vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
1232
1233        if (ath11k_peer_assoc_h_vht_masked(vht_mcs_mask))
1234                return;
1235
1236        arg->vht_flag = true;
1237
1238        /* TODO: similar flags required? */
1239        arg->vht_capable = true;
1240
1241        if (def.chan->band == NL80211_BAND_2GHZ)
1242                arg->vht_ng_flag = true;
1243
1244        arg->peer_vht_caps = vht_cap->cap;
1245
1246        ampdu_factor = (vht_cap->cap &
1247                        IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK) >>
1248                       IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
1249
1250        /* Workaround: Some Netgear/Linksys 11ac APs set Rx A-MPDU factor to
1251         * zero in VHT IE. Using it would result in degraded throughput.
1252         * arg->peer_max_mpdu at this point contains HT max_mpdu so keep
1253         * it if VHT max_mpdu is smaller.
1254         */
1255        arg->peer_max_mpdu = max(arg->peer_max_mpdu,
1256                                 (1U << (IEEE80211_HT_MAX_AMPDU_FACTOR +
1257                                        ampdu_factor)) - 1);
1258
1259        if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
1260                arg->bw_80 = true;
1261
1262        if (sta->bandwidth == IEEE80211_STA_RX_BW_160)
1263                arg->bw_160 = true;
1264
1265        /* Calculate peer NSS capability from VHT capabilities if STA
1266         * supports VHT.
1267         */
1268        for (i = 0, max_nss = 0, vht_mcs = 0; i < NL80211_VHT_NSS_MAX; i++) {
1269                vht_mcs = __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map) >>
1270                          (2 * i) & 3;
1271
1272                if (vht_mcs != IEEE80211_VHT_MCS_NOT_SUPPORTED &&
1273                    vht_mcs_mask[i])
1274                        max_nss = i + 1;
1275        }
1276        arg->peer_nss = min(sta->rx_nss, max_nss);
1277        arg->rx_max_rate = __le16_to_cpu(vht_cap->vht_mcs.rx_highest);
1278        arg->rx_mcs_set = __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map);
1279        arg->tx_max_rate = __le16_to_cpu(vht_cap->vht_mcs.tx_highest);
1280        arg->tx_mcs_set = ath11k_peer_assoc_h_vht_limit(
1281                __le16_to_cpu(vht_cap->vht_mcs.tx_mcs_map), vht_mcs_mask);
1282
1283        /* In IPQ8074 platform, VHT mcs rate 10 and 11 is enabled by default.
1284         * VHT mcs rate 10 and 11 is not suppoerted in 11ac standard.
1285         * so explicitly disable the VHT MCS rate 10 and 11 in 11ac mode.
1286         */
1287        arg->tx_mcs_set &= ~IEEE80211_VHT_MCS_SUPPORT_0_11_MASK;
1288        arg->tx_mcs_set |= IEEE80211_DISABLE_VHT_MCS_SUPPORT_0_11;
1289
1290        if ((arg->tx_mcs_set & IEEE80211_VHT_MCS_NOT_SUPPORTED) ==
1291                        IEEE80211_VHT_MCS_NOT_SUPPORTED)
1292                arg->peer_vht_caps &= ~IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE;
1293
1294        /* TODO:  Check */
1295        arg->tx_max_mcs_nss = 0xFF;
1296
1297        ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac vht peer %pM max_mpdu %d flags 0x%x\n",
1298                   sta->addr, arg->peer_max_mpdu, arg->peer_flags);
1299
1300        /* TODO: rxnss_override */
1301}
1302
1303static void ath11k_peer_assoc_h_he(struct ath11k *ar,
1304                                   struct ieee80211_vif *vif,
1305                                   struct ieee80211_sta *sta,
1306                                   struct peer_assoc_params *arg)
1307{
1308        const struct ieee80211_sta_he_cap *he_cap = &sta->he_cap;
1309        u8 ampdu_factor;
1310        u16 v;
1311
1312        if (!he_cap->has_he)
1313                return;
1314
1315        arg->he_flag = true;
1316
1317        memcpy_and_pad(&arg->peer_he_cap_macinfo,
1318                       sizeof(arg->peer_he_cap_macinfo),
1319                       he_cap->he_cap_elem.mac_cap_info,
1320                       sizeof(he_cap->he_cap_elem.mac_cap_info),
1321                       0);
1322        memcpy_and_pad(&arg->peer_he_cap_phyinfo,
1323                       sizeof(arg->peer_he_cap_phyinfo),
1324                       he_cap->he_cap_elem.phy_cap_info,
1325                       sizeof(he_cap->he_cap_elem.phy_cap_info),
1326                       0);
1327        arg->peer_he_ops = vif->bss_conf.he_oper.params;
1328
1329        /* the top most byte is used to indicate BSS color info */
1330        arg->peer_he_ops &= 0xffffff;
1331
1332        /* As per section 26.6.1 11ax Draft5.0, if the Max AMPDU Exponent Extension
1333         * in HE cap is zero, use the arg->peer_max_mpdu as calculated while parsing
1334         * VHT caps(if VHT caps is present) or HT caps (if VHT caps is not present).
1335         *
1336         * For non-zero value of Max AMPDU Extponent Extension in HE MAC caps,
1337         * if a HE STA sends VHT cap and HE cap IE in assoc request then, use
1338         * MAX_AMPDU_LEN_FACTOR as 20 to calculate max_ampdu length.
1339         * If a HE STA that does not send VHT cap, but HE and HT cap in assoc
1340         * request, then use MAX_AMPDU_LEN_FACTOR as 16 to calculate max_ampdu
1341         * length.
1342         */
1343        ampdu_factor = u8_get_bits(he_cap->he_cap_elem.mac_cap_info[3],
1344                                   IEEE80211_HE_MAC_CAP3_MAX_AMPDU_LEN_EXP_MASK);
1345
1346        if (ampdu_factor) {
1347                if (sta->vht_cap.vht_supported)
1348                        arg->peer_max_mpdu = (1 << (IEEE80211_HE_VHT_MAX_AMPDU_FACTOR +
1349                                                    ampdu_factor)) - 1;
1350                else if (sta->ht_cap.ht_supported)
1351                        arg->peer_max_mpdu = (1 << (IEEE80211_HE_HT_MAX_AMPDU_FACTOR +
1352                                                    ampdu_factor)) - 1;
1353        }
1354
1355        if (he_cap->he_cap_elem.phy_cap_info[6] &
1356            IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
1357                int bit = 7;
1358                int nss, ru;
1359
1360                arg->peer_ppet.numss_m1 = he_cap->ppe_thres[0] &
1361                                          IEEE80211_PPE_THRES_NSS_MASK;
1362                arg->peer_ppet.ru_bit_mask =
1363                        (he_cap->ppe_thres[0] &
1364                         IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK) >>
1365                        IEEE80211_PPE_THRES_RU_INDEX_BITMASK_POS;
1366
1367                for (nss = 0; nss <= arg->peer_ppet.numss_m1; nss++) {
1368                        for (ru = 0; ru < 4; ru++) {
1369                                u32 val = 0;
1370                                int i;
1371
1372                                if ((arg->peer_ppet.ru_bit_mask & BIT(ru)) == 0)
1373                                        continue;
1374                                for (i = 0; i < 6; i++) {
1375                                        val >>= 1;
1376                                        val |= ((he_cap->ppe_thres[bit / 8] >>
1377                                                 (bit % 8)) & 0x1) << 5;
1378                                        bit++;
1379                                }
1380                                arg->peer_ppet.ppet16_ppet8_ru3_ru0[nss] |=
1381                                                                val << (ru * 6);
1382                        }
1383                }
1384        }
1385
1386        if (he_cap->he_cap_elem.mac_cap_info[0] & IEEE80211_HE_MAC_CAP0_TWT_RES)
1387                arg->twt_responder = true;
1388        if (he_cap->he_cap_elem.mac_cap_info[0] & IEEE80211_HE_MAC_CAP0_TWT_REQ)
1389                arg->twt_requester = true;
1390
1391        switch (sta->bandwidth) {
1392        case IEEE80211_STA_RX_BW_160:
1393                if (he_cap->he_cap_elem.phy_cap_info[0] &
1394                    IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G) {
1395                        v = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_80p80);
1396                        arg->peer_he_rx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_80_80] = v;
1397
1398                        v = le16_to_cpu(he_cap->he_mcs_nss_supp.tx_mcs_80p80);
1399                        arg->peer_he_tx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_80_80] = v;
1400
1401                        arg->peer_he_mcs_count++;
1402                }
1403                v = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_160);
1404                arg->peer_he_rx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_160] = v;
1405
1406                v = le16_to_cpu(he_cap->he_mcs_nss_supp.tx_mcs_160);
1407                arg->peer_he_tx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_160] = v;
1408
1409                arg->peer_he_mcs_count++;
1410                fallthrough;
1411
1412        default:
1413                v = le16_to_cpu(he_cap->he_mcs_nss_supp.rx_mcs_80);
1414                arg->peer_he_rx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_80] = v;
1415
1416                v = le16_to_cpu(he_cap->he_mcs_nss_supp.tx_mcs_80);
1417                arg->peer_he_tx_mcs_set[WMI_HECAP_TXRX_MCS_NSS_IDX_80] = v;
1418
1419                arg->peer_he_mcs_count++;
1420                break;
1421        }
1422}
1423
1424static void ath11k_peer_assoc_h_smps(struct ieee80211_sta *sta,
1425                                     struct peer_assoc_params *arg)
1426{
1427        const struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
1428        int smps;
1429
1430        if (!ht_cap->ht_supported)
1431                return;
1432
1433        smps = ht_cap->cap & IEEE80211_HT_CAP_SM_PS;
1434        smps >>= IEEE80211_HT_CAP_SM_PS_SHIFT;
1435
1436        switch (smps) {
1437        case WLAN_HT_CAP_SM_PS_STATIC:
1438                arg->static_mimops_flag = true;
1439                break;
1440        case WLAN_HT_CAP_SM_PS_DYNAMIC:
1441                arg->dynamic_mimops_flag = true;
1442                break;
1443        case WLAN_HT_CAP_SM_PS_DISABLED:
1444                arg->spatial_mux_flag = true;
1445                break;
1446        default:
1447                break;
1448        }
1449}
1450
1451static void ath11k_peer_assoc_h_qos(struct ath11k *ar,
1452                                    struct ieee80211_vif *vif,
1453                                    struct ieee80211_sta *sta,
1454                                    struct peer_assoc_params *arg)
1455{
1456        struct ath11k_vif *arvif = (void *)vif->drv_priv;
1457
1458        switch (arvif->vdev_type) {
1459        case WMI_VDEV_TYPE_AP:
1460                if (sta->wme) {
1461                        /* TODO: Check WME vs QoS */
1462                        arg->is_wme_set = true;
1463                        arg->qos_flag = true;
1464                }
1465
1466                if (sta->wme && sta->uapsd_queues) {
1467                        /* TODO: Check WME vs QoS */
1468                        arg->is_wme_set = true;
1469                        arg->apsd_flag = true;
1470                        arg->peer_rate_caps |= WMI_HOST_RC_UAPSD_FLAG;
1471                }
1472                break;
1473        case WMI_VDEV_TYPE_STA:
1474                if (sta->wme) {
1475                        arg->is_wme_set = true;
1476                        arg->qos_flag = true;
1477                }
1478                break;
1479        default:
1480                break;
1481        }
1482
1483        ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac peer %pM qos %d\n",
1484                   sta->addr, arg->qos_flag);
1485}
1486
1487static int ath11k_peer_assoc_qos_ap(struct ath11k *ar,
1488                                    struct ath11k_vif *arvif,
1489                                    struct ieee80211_sta *sta)
1490{
1491        struct ap_ps_params params;
1492        u32 max_sp;
1493        u32 uapsd;
1494        int ret;
1495
1496        lockdep_assert_held(&ar->conf_mutex);
1497
1498        params.vdev_id = arvif->vdev_id;
1499
1500        ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac uapsd_queues 0x%x max_sp %d\n",
1501                   sta->uapsd_queues, sta->max_sp);
1502
1503        uapsd = 0;
1504        if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
1505                uapsd |= WMI_AP_PS_UAPSD_AC3_DELIVERY_EN |
1506                         WMI_AP_PS_UAPSD_AC3_TRIGGER_EN;
1507        if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
1508                uapsd |= WMI_AP_PS_UAPSD_AC2_DELIVERY_EN |
1509                         WMI_AP_PS_UAPSD_AC2_TRIGGER_EN;
1510        if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
1511                uapsd |= WMI_AP_PS_UAPSD_AC1_DELIVERY_EN |
1512                         WMI_AP_PS_UAPSD_AC1_TRIGGER_EN;
1513        if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
1514                uapsd |= WMI_AP_PS_UAPSD_AC0_DELIVERY_EN |
1515                         WMI_AP_PS_UAPSD_AC0_TRIGGER_EN;
1516
1517        max_sp = 0;
1518        if (sta->max_sp < MAX_WMI_AP_PS_PEER_PARAM_MAX_SP)
1519                max_sp = sta->max_sp;
1520
1521        params.param = WMI_AP_PS_PEER_PARAM_UAPSD;
1522        params.value = uapsd;
1523        ret = ath11k_wmi_send_set_ap_ps_param_cmd(ar, sta->addr, &params);
1524        if (ret)
1525                goto err;
1526
1527        params.param = WMI_AP_PS_PEER_PARAM_MAX_SP;
1528        params.value = max_sp;
1529        ret = ath11k_wmi_send_set_ap_ps_param_cmd(ar, sta->addr, &params);
1530        if (ret)
1531                goto err;
1532
1533        /* TODO revisit during testing */
1534        params.param = WMI_AP_PS_PEER_PARAM_SIFS_RESP_FRMTYPE;
1535        params.value = DISABLE_SIFS_RESPONSE_TRIGGER;
1536        ret = ath11k_wmi_send_set_ap_ps_param_cmd(ar, sta->addr, &params);
1537        if (ret)
1538                goto err;
1539
1540        params.param = WMI_AP_PS_PEER_PARAM_SIFS_RESP_UAPSD;
1541        params.value = DISABLE_SIFS_RESPONSE_TRIGGER;
1542        ret = ath11k_wmi_send_set_ap_ps_param_cmd(ar, sta->addr, &params);
1543        if (ret)
1544                goto err;
1545
1546        return 0;
1547
1548err:
1549        ath11k_warn(ar->ab, "failed to set ap ps peer param %d for vdev %i: %d\n",
1550                    params.param, arvif->vdev_id, ret);
1551        return ret;
1552}
1553
1554static bool ath11k_mac_sta_has_ofdm_only(struct ieee80211_sta *sta)
1555{
1556        return sta->supp_rates[NL80211_BAND_2GHZ] >>
1557               ATH11K_MAC_FIRST_OFDM_RATE_IDX;
1558}
1559
1560static enum wmi_phy_mode ath11k_mac_get_phymode_vht(struct ath11k *ar,
1561                                                    struct ieee80211_sta *sta)
1562{
1563        if (sta->bandwidth == IEEE80211_STA_RX_BW_160) {
1564                switch (sta->vht_cap.cap &
1565                        IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK) {
1566                case IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ:
1567                        return MODE_11AC_VHT160;
1568                case IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ:
1569                        return MODE_11AC_VHT80_80;
1570                default:
1571                        /* not sure if this is a valid case? */
1572                        return MODE_11AC_VHT160;
1573                }
1574        }
1575
1576        if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
1577                return MODE_11AC_VHT80;
1578
1579        if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1580                return MODE_11AC_VHT40;
1581
1582        if (sta->bandwidth == IEEE80211_STA_RX_BW_20)
1583                return MODE_11AC_VHT20;
1584
1585        return MODE_UNKNOWN;
1586}
1587
1588static enum wmi_phy_mode ath11k_mac_get_phymode_he(struct ath11k *ar,
1589                                                   struct ieee80211_sta *sta)
1590{
1591        if (sta->bandwidth == IEEE80211_STA_RX_BW_160) {
1592                if (sta->he_cap.he_cap_elem.phy_cap_info[0] &
1593                     IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_160MHZ_IN_5G)
1594                        return MODE_11AX_HE160;
1595                else if (sta->he_cap.he_cap_elem.phy_cap_info[0] &
1596                     IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_80PLUS80_MHZ_IN_5G)
1597                        return MODE_11AX_HE80_80;
1598                /* not sure if this is a valid case? */
1599                return MODE_11AX_HE160;
1600        }
1601
1602        if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
1603                return MODE_11AX_HE80;
1604
1605        if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1606                return MODE_11AX_HE40;
1607
1608        if (sta->bandwidth == IEEE80211_STA_RX_BW_20)
1609                return MODE_11AX_HE20;
1610
1611        return MODE_UNKNOWN;
1612}
1613
1614static void ath11k_peer_assoc_h_phymode(struct ath11k *ar,
1615                                        struct ieee80211_vif *vif,
1616                                        struct ieee80211_sta *sta,
1617                                        struct peer_assoc_params *arg)
1618{
1619        struct ath11k_vif *arvif = (void *)vif->drv_priv;
1620        struct cfg80211_chan_def def;
1621        enum nl80211_band band;
1622        const u8 *ht_mcs_mask;
1623        const u16 *vht_mcs_mask;
1624        enum wmi_phy_mode phymode = MODE_UNKNOWN;
1625
1626        if (WARN_ON(ath11k_mac_vif_chan(vif, &def)))
1627                return;
1628
1629        band = def.chan->band;
1630        ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
1631        vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
1632
1633        switch (band) {
1634        case NL80211_BAND_2GHZ:
1635                if (sta->he_cap.has_he) {
1636                        if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
1637                                phymode = MODE_11AX_HE80_2G;
1638                        else if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1639                                phymode = MODE_11AX_HE40_2G;
1640                        else
1641                                phymode = MODE_11AX_HE20_2G;
1642                } else if (sta->vht_cap.vht_supported &&
1643                    !ath11k_peer_assoc_h_vht_masked(vht_mcs_mask)) {
1644                        if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1645                                phymode = MODE_11AC_VHT40;
1646                        else
1647                                phymode = MODE_11AC_VHT20;
1648                } else if (sta->ht_cap.ht_supported &&
1649                           !ath11k_peer_assoc_h_ht_masked(ht_mcs_mask)) {
1650                        if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1651                                phymode = MODE_11NG_HT40;
1652                        else
1653                                phymode = MODE_11NG_HT20;
1654                } else if (ath11k_mac_sta_has_ofdm_only(sta)) {
1655                        phymode = MODE_11G;
1656                } else {
1657                        phymode = MODE_11B;
1658                }
1659                break;
1660        case NL80211_BAND_5GHZ:
1661        case NL80211_BAND_6GHZ:
1662                /* Check HE first */
1663                if (sta->he_cap.has_he) {
1664                        phymode = ath11k_mac_get_phymode_he(ar, sta);
1665                } else if (sta->vht_cap.vht_supported &&
1666                    !ath11k_peer_assoc_h_vht_masked(vht_mcs_mask)) {
1667                        phymode = ath11k_mac_get_phymode_vht(ar, sta);
1668                } else if (sta->ht_cap.ht_supported &&
1669                           !ath11k_peer_assoc_h_ht_masked(ht_mcs_mask)) {
1670                        if (sta->bandwidth >= IEEE80211_STA_RX_BW_40)
1671                                phymode = MODE_11NA_HT40;
1672                        else
1673                                phymode = MODE_11NA_HT20;
1674                } else {
1675                        phymode = MODE_11A;
1676                }
1677                break;
1678        default:
1679                break;
1680        }
1681
1682        ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac peer %pM phymode %s\n",
1683                   sta->addr, ath11k_wmi_phymode_str(phymode));
1684
1685        arg->peer_phymode = phymode;
1686        WARN_ON(phymode == MODE_UNKNOWN);
1687}
1688
1689static void ath11k_peer_assoc_prepare(struct ath11k *ar,
1690                                      struct ieee80211_vif *vif,
1691                                      struct ieee80211_sta *sta,
1692                                      struct peer_assoc_params *arg,
1693                                      bool reassoc)
1694{
1695        lockdep_assert_held(&ar->conf_mutex);
1696
1697        memset(arg, 0, sizeof(*arg));
1698
1699        reinit_completion(&ar->peer_assoc_done);
1700
1701        arg->peer_new_assoc = !reassoc;
1702        ath11k_peer_assoc_h_basic(ar, vif, sta, arg);
1703        ath11k_peer_assoc_h_crypto(ar, vif, sta, arg);
1704        ath11k_peer_assoc_h_rates(ar, vif, sta, arg);
1705        ath11k_peer_assoc_h_ht(ar, vif, sta, arg);
1706        ath11k_peer_assoc_h_vht(ar, vif, sta, arg);
1707        ath11k_peer_assoc_h_he(ar, vif, sta, arg);
1708        ath11k_peer_assoc_h_qos(ar, vif, sta, arg);
1709        ath11k_peer_assoc_h_phymode(ar, vif, sta, arg);
1710        ath11k_peer_assoc_h_smps(sta, arg);
1711
1712        /* TODO: amsdu_disable req? */
1713}
1714
1715static int ath11k_setup_peer_smps(struct ath11k *ar, struct ath11k_vif *arvif,
1716                                  const u8 *addr,
1717                                  const struct ieee80211_sta_ht_cap *ht_cap)
1718{
1719        int smps;
1720
1721        if (!ht_cap->ht_supported)
1722                return 0;
1723
1724        smps = ht_cap->cap & IEEE80211_HT_CAP_SM_PS;
1725        smps >>= IEEE80211_HT_CAP_SM_PS_SHIFT;
1726
1727        if (smps >= ARRAY_SIZE(ath11k_smps_map))
1728                return -EINVAL;
1729
1730        return ath11k_wmi_set_peer_param(ar, addr, arvif->vdev_id,
1731                                         WMI_PEER_MIMO_PS_STATE,
1732                                         ath11k_smps_map[smps]);
1733}
1734
1735static void ath11k_bss_assoc(struct ieee80211_hw *hw,
1736                             struct ieee80211_vif *vif,
1737                             struct ieee80211_bss_conf *bss_conf)
1738{
1739        struct ath11k *ar = hw->priv;
1740        struct ath11k_vif *arvif = (void *)vif->drv_priv;
1741        struct peer_assoc_params peer_arg;
1742        struct ieee80211_sta *ap_sta;
1743        int ret;
1744
1745        lockdep_assert_held(&ar->conf_mutex);
1746
1747        ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac vdev %i assoc bssid %pM aid %d\n",
1748                   arvif->vdev_id, arvif->bssid, arvif->aid);
1749
1750        rcu_read_lock();
1751
1752        ap_sta = ieee80211_find_sta(vif, bss_conf->bssid);
1753        if (!ap_sta) {
1754                ath11k_warn(ar->ab, "failed to find station entry for bss %pM vdev %i\n",
1755                            bss_conf->bssid, arvif->vdev_id);
1756                rcu_read_unlock();
1757                return;
1758        }
1759
1760        ath11k_peer_assoc_prepare(ar, vif, ap_sta, &peer_arg, false);
1761
1762        rcu_read_unlock();
1763
1764        ret = ath11k_wmi_send_peer_assoc_cmd(ar, &peer_arg);
1765        if (ret) {
1766                ath11k_warn(ar->ab, "failed to run peer assoc for %pM vdev %i: %d\n",
1767                            bss_conf->bssid, arvif->vdev_id, ret);
1768                return;
1769        }
1770
1771        if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ)) {
1772                ath11k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
1773                            bss_conf->bssid, arvif->vdev_id);
1774                return;
1775        }
1776
1777        ret = ath11k_setup_peer_smps(ar, arvif, bss_conf->bssid,
1778                                     &ap_sta->ht_cap);
1779        if (ret) {
1780                ath11k_warn(ar->ab, "failed to setup peer SMPS for vdev %d: %d\n",
1781                            arvif->vdev_id, ret);
1782                return;
1783        }
1784
1785        WARN_ON(arvif->is_up);
1786
1787        arvif->aid = bss_conf->aid;
1788        ether_addr_copy(arvif->bssid, bss_conf->bssid);
1789
1790        ret = ath11k_wmi_vdev_up(ar, arvif->vdev_id, arvif->aid, arvif->bssid);
1791        if (ret) {
1792                ath11k_warn(ar->ab, "failed to set vdev %d up: %d\n",
1793                            arvif->vdev_id, ret);
1794                return;
1795        }
1796
1797        arvif->is_up = true;
1798
1799        ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
1800                   "mac vdev %d up (associated) bssid %pM aid %d\n",
1801                   arvif->vdev_id, bss_conf->bssid, bss_conf->aid);
1802
1803        /* Authorize BSS Peer */
1804        ret = ath11k_wmi_set_peer_param(ar, arvif->bssid,
1805                                        arvif->vdev_id,
1806                                        WMI_PEER_AUTHORIZE,
1807                                        1);
1808        if (ret)
1809                ath11k_warn(ar->ab, "Unable to authorize BSS peer: %d\n", ret);
1810
1811        ret = ath11k_wmi_send_obss_spr_cmd(ar, arvif->vdev_id,
1812                                           &bss_conf->he_obss_pd);
1813        if (ret)
1814                ath11k_warn(ar->ab, "failed to set vdev %i OBSS PD parameters: %d\n",
1815                            arvif->vdev_id, ret);
1816}
1817
1818static void ath11k_bss_disassoc(struct ieee80211_hw *hw,
1819                                struct ieee80211_vif *vif)
1820{
1821        struct ath11k *ar = hw->priv;
1822        struct ath11k_vif *arvif = (void *)vif->drv_priv;
1823        int ret;
1824
1825        lockdep_assert_held(&ar->conf_mutex);
1826
1827        ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac vdev %i disassoc bssid %pM\n",
1828                   arvif->vdev_id, arvif->bssid);
1829
1830        ret = ath11k_wmi_vdev_down(ar, arvif->vdev_id);
1831        if (ret)
1832                ath11k_warn(ar->ab, "failed to down vdev %i: %d\n",
1833                            arvif->vdev_id, ret);
1834
1835        arvif->is_up = false;
1836
1837        cancel_delayed_work_sync(&arvif->connection_loss_work);
1838}
1839
1840static u32 ath11k_mac_get_rate_hw_value(int bitrate)
1841{
1842        u32 preamble;
1843        u16 hw_value;
1844        int rate;
1845        size_t i;
1846
1847        if (ath11k_mac_bitrate_is_cck(bitrate))
1848                preamble = WMI_RATE_PREAMBLE_CCK;
1849        else
1850                preamble = WMI_RATE_PREAMBLE_OFDM;
1851
1852        for (i = 0; i < ARRAY_SIZE(ath11k_legacy_rates); i++) {
1853                if (ath11k_legacy_rates[i].bitrate != bitrate)
1854                        continue;
1855
1856                hw_value = ath11k_legacy_rates[i].hw_value;
1857                rate = ATH11K_HW_RATE_CODE(hw_value, 0, preamble);
1858
1859                return rate;
1860        }
1861
1862        return -EINVAL;
1863}
1864
1865static void ath11k_recalculate_mgmt_rate(struct ath11k *ar,
1866                                         struct ieee80211_vif *vif,
1867                                         struct cfg80211_chan_def *def)
1868{
1869        struct ath11k_vif *arvif = (void *)vif->drv_priv;
1870        const struct ieee80211_supported_band *sband;
1871        u8 basic_rate_idx;
1872        int hw_rate_code;
1873        u32 vdev_param;
1874        u16 bitrate;
1875        int ret;
1876
1877        lockdep_assert_held(&ar->conf_mutex);
1878
1879        sband = ar->hw->wiphy->bands[def->chan->band];
1880        basic_rate_idx = ffs(vif->bss_conf.basic_rates) - 1;
1881        bitrate = sband->bitrates[basic_rate_idx].bitrate;
1882
1883        hw_rate_code = ath11k_mac_get_rate_hw_value(bitrate);
1884        if (hw_rate_code < 0) {
1885                ath11k_warn(ar->ab, "bitrate not supported %d\n", bitrate);
1886                return;
1887        }
1888
1889        vdev_param = WMI_VDEV_PARAM_MGMT_RATE;
1890        ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, vdev_param,
1891                                            hw_rate_code);
1892        if (ret)
1893                ath11k_warn(ar->ab, "failed to set mgmt tx rate %d\n", ret);
1894
1895        vdev_param = WMI_VDEV_PARAM_BEACON_RATE;
1896        ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id, vdev_param,
1897                                            hw_rate_code);
1898        if (ret)
1899                ath11k_warn(ar->ab, "failed to set beacon tx rate %d\n", ret);
1900}
1901
1902static int ath11k_mac_fils_discovery(struct ath11k_vif *arvif,
1903                                     struct ieee80211_bss_conf *info)
1904{
1905        struct ath11k *ar = arvif->ar;
1906        struct sk_buff *tmpl;
1907        int ret;
1908        u32 interval;
1909        bool unsol_bcast_probe_resp_enabled = false;
1910
1911        if (info->fils_discovery.max_interval) {
1912                interval = info->fils_discovery.max_interval;
1913
1914                tmpl = ieee80211_get_fils_discovery_tmpl(ar->hw, arvif->vif);
1915                if (tmpl)
1916                        ret = ath11k_wmi_fils_discovery_tmpl(ar, arvif->vdev_id,
1917                                                             tmpl);
1918        } else if (info->unsol_bcast_probe_resp_interval) {
1919                unsol_bcast_probe_resp_enabled = 1;
1920                interval = info->unsol_bcast_probe_resp_interval;
1921
1922                tmpl = ieee80211_get_unsol_bcast_probe_resp_tmpl(ar->hw,
1923                                                                 arvif->vif);
1924                if (tmpl)
1925                        ret = ath11k_wmi_probe_resp_tmpl(ar, arvif->vdev_id,
1926                                                         tmpl);
1927        } else { /* Disable */
1928                return ath11k_wmi_fils_discovery(ar, arvif->vdev_id, 0, false);
1929        }
1930
1931        if (!tmpl) {
1932                ath11k_warn(ar->ab,
1933                            "mac vdev %i failed to retrieve %s template\n",
1934                            arvif->vdev_id, (unsol_bcast_probe_resp_enabled ?
1935                            "unsolicited broadcast probe response" :
1936                            "FILS discovery"));
1937                return -EPERM;
1938        }
1939        kfree_skb(tmpl);
1940
1941        if (!ret)
1942                ret = ath11k_wmi_fils_discovery(ar, arvif->vdev_id, interval,
1943                                                unsol_bcast_probe_resp_enabled);
1944
1945        return ret;
1946}
1947
1948static int ath11k_mac_config_obss_pd(struct ath11k *ar,
1949                                     struct ieee80211_he_obss_pd *he_obss_pd)
1950{
1951        u32 bitmap[2], param_id, param_val, pdev_id;
1952        int ret;
1953        s8 non_srg_th = 0, srg_th = 0;
1954
1955        pdev_id = ar->pdev->pdev_id;
1956
1957        /* Set and enable SRG/non-SRG OBSS PD Threshold */
1958        param_id = WMI_PDEV_PARAM_SET_CMD_OBSS_PD_THRESHOLD;
1959        if (test_bit(ATH11K_FLAG_MONITOR_ENABLED, &ar->monitor_flags)) {
1960                ret = ath11k_wmi_pdev_set_param(ar, param_id, 0, pdev_id);
1961                if (ret)
1962                        ath11k_warn(ar->ab,
1963                                    "failed to set obss_pd_threshold for pdev: %u\n",
1964                                    pdev_id);
1965                return ret;
1966        }
1967
1968        ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
1969                   "mac obss pd sr_ctrl %x non_srg_thres %u srg_max %u\n",
1970                   he_obss_pd->sr_ctrl, he_obss_pd->non_srg_max_offset,
1971                   he_obss_pd->max_offset);
1972
1973        param_val = 0;
1974
1975        if (he_obss_pd->sr_ctrl &
1976            IEEE80211_HE_SPR_NON_SRG_OBSS_PD_SR_DISALLOWED) {
1977                non_srg_th = ATH11K_OBSS_PD_MAX_THRESHOLD;
1978        } else {
1979                if (he_obss_pd->sr_ctrl & IEEE80211_HE_SPR_NON_SRG_OFFSET_PRESENT)
1980                        non_srg_th = (ATH11K_OBSS_PD_MAX_THRESHOLD +
1981                                      he_obss_pd->non_srg_max_offset);
1982                else
1983                        non_srg_th = ATH11K_OBSS_PD_NON_SRG_MAX_THRESHOLD;
1984
1985                param_val |= ATH11K_OBSS_PD_NON_SRG_EN;
1986        }
1987
1988        if (he_obss_pd->sr_ctrl & IEEE80211_HE_SPR_SRG_INFORMATION_PRESENT) {
1989                srg_th = ATH11K_OBSS_PD_MAX_THRESHOLD + he_obss_pd->max_offset;
1990                param_val |= ATH11K_OBSS_PD_SRG_EN;
1991        }
1992
1993        if (test_bit(WMI_TLV_SERVICE_SRG_SRP_SPATIAL_REUSE_SUPPORT,
1994                     ar->ab->wmi_ab.svc_map)) {
1995                param_val |= ATH11K_OBSS_PD_THRESHOLD_IN_DBM;
1996                param_val |= FIELD_PREP(GENMASK(15, 8), srg_th);
1997        } else {
1998                non_srg_th -= ATH11K_DEFAULT_NOISE_FLOOR;
1999                /* SRG not supported and threshold in dB */
2000                param_val &= ~(ATH11K_OBSS_PD_SRG_EN |
2001                               ATH11K_OBSS_PD_THRESHOLD_IN_DBM);
2002        }
2003
2004        param_val |= (non_srg_th & GENMASK(7, 0));
2005        ret = ath11k_wmi_pdev_set_param(ar, param_id, param_val, pdev_id);
2006        if (ret) {
2007                ath11k_warn(ar->ab,
2008                            "failed to set obss_pd_threshold for pdev: %u\n",
2009                            pdev_id);
2010                return ret;
2011        }
2012
2013        /* Enable OBSS PD for all access category */
2014        param_id  = WMI_PDEV_PARAM_SET_CMD_OBSS_PD_PER_AC;
2015        param_val = 0xf;
2016        ret = ath11k_wmi_pdev_set_param(ar, param_id, param_val, pdev_id);
2017        if (ret) {
2018                ath11k_warn(ar->ab,
2019                            "failed to set obss_pd_per_ac for pdev: %u\n",
2020                            pdev_id);
2021                return ret;
2022        }
2023
2024        /* Set SR Prohibit */
2025        param_id  = WMI_PDEV_PARAM_ENABLE_SR_PROHIBIT;
2026        param_val = !!(he_obss_pd->sr_ctrl &
2027                       IEEE80211_HE_SPR_HESIGA_SR_VAL15_ALLOWED);
2028        ret = ath11k_wmi_pdev_set_param(ar, param_id, param_val, pdev_id);
2029        if (ret) {
2030                ath11k_warn(ar->ab, "failed to set sr_prohibit for pdev: %u\n",
2031                            pdev_id);
2032                return ret;
2033        }
2034
2035        if (!test_bit(WMI_TLV_SERVICE_SRG_SRP_SPATIAL_REUSE_SUPPORT,
2036                      ar->ab->wmi_ab.svc_map))
2037                return 0;
2038
2039        /* Set SRG BSS Color Bitmap */
2040        memcpy(bitmap, he_obss_pd->bss_color_bitmap, sizeof(bitmap));
2041        ret = ath11k_wmi_pdev_set_srg_bss_color_bitmap(ar, bitmap);
2042        if (ret) {
2043                ath11k_warn(ar->ab,
2044                            "failed to set bss_color_bitmap for pdev: %u\n",
2045                            pdev_id);
2046                return ret;
2047        }
2048
2049        /* Set SRG Partial BSSID Bitmap */
2050        memcpy(bitmap, he_obss_pd->partial_bssid_bitmap, sizeof(bitmap));
2051        ret = ath11k_wmi_pdev_set_srg_patial_bssid_bitmap(ar, bitmap);
2052        if (ret) {
2053                ath11k_warn(ar->ab,
2054                            "failed to set partial_bssid_bitmap for pdev: %u\n",
2055                            pdev_id);
2056                return ret;
2057        }
2058
2059        memset(bitmap, 0xff, sizeof(bitmap));
2060
2061        /* Enable all BSS Colors for SRG */
2062        ret = ath11k_wmi_pdev_srg_obss_color_enable_bitmap(ar, bitmap);
2063        if (ret) {
2064                ath11k_warn(ar->ab,
2065                            "failed to set srg_color_en_bitmap pdev: %u\n",
2066                            pdev_id);
2067                return ret;
2068        }
2069
2070        /* Enable all patial BSSID mask for SRG */
2071        ret = ath11k_wmi_pdev_srg_obss_bssid_enable_bitmap(ar, bitmap);
2072        if (ret) {
2073                ath11k_warn(ar->ab,
2074                            "failed to set srg_bssid_en_bitmap pdev: %u\n",
2075                            pdev_id);
2076                return ret;
2077        }
2078
2079        /* Enable all BSS Colors for non-SRG */
2080        ret = ath11k_wmi_pdev_non_srg_obss_color_enable_bitmap(ar, bitmap);
2081        if (ret) {
2082                ath11k_warn(ar->ab,
2083                            "failed to set non_srg_color_en_bitmap pdev: %u\n",
2084                            pdev_id);
2085                return ret;
2086        }
2087
2088        /* Enable all patial BSSID mask for non-SRG */
2089        ret = ath11k_wmi_pdev_non_srg_obss_bssid_enable_bitmap(ar, bitmap);
2090        if (ret) {
2091                ath11k_warn(ar->ab,
2092                            "failed to set non_srg_bssid_en_bitmap pdev: %u\n",
2093                            pdev_id);
2094                return ret;
2095        }
2096
2097        return 0;
2098}
2099
2100static void ath11k_mac_op_bss_info_changed(struct ieee80211_hw *hw,
2101                                           struct ieee80211_vif *vif,
2102                                           struct ieee80211_bss_conf *info,
2103                                           u32 changed)
2104{
2105        struct ath11k *ar = hw->priv;
2106        struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
2107        struct cfg80211_chan_def def;
2108        u32 param_id, param_value;
2109        enum nl80211_band band;
2110        u32 vdev_param;
2111        int mcast_rate;
2112        u32 preamble;
2113        u16 hw_value;
2114        u16 bitrate;
2115        int ret = 0;
2116        u8 rateidx;
2117        u32 rate;
2118
2119        mutex_lock(&ar->conf_mutex);
2120
2121        if (changed & BSS_CHANGED_BEACON_INT) {
2122                arvif->beacon_interval = info->beacon_int;
2123
2124                param_id = WMI_VDEV_PARAM_BEACON_INTERVAL;
2125                ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
2126                                                    param_id,
2127                                                    arvif->beacon_interval);
2128                if (ret)
2129                        ath11k_warn(ar->ab, "Failed to set beacon interval for VDEV: %d\n",
2130                                    arvif->vdev_id);
2131                else
2132                        ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
2133                                   "Beacon interval: %d set for VDEV: %d\n",
2134                                   arvif->beacon_interval, arvif->vdev_id);
2135        }
2136
2137        if (changed & BSS_CHANGED_BEACON) {
2138                param_id = WMI_PDEV_PARAM_BEACON_TX_MODE;
2139                param_value = WMI_BEACON_STAGGERED_MODE;
2140                ret = ath11k_wmi_pdev_set_param(ar, param_id,
2141                                                param_value, ar->pdev->pdev_id);
2142                if (ret)
2143                        ath11k_warn(ar->ab, "Failed to set beacon mode for VDEV: %d\n",
2144                                    arvif->vdev_id);
2145                else
2146                        ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
2147                                   "Set staggered beacon mode for VDEV: %d\n",
2148                                   arvif->vdev_id);
2149
2150                ret = ath11k_mac_setup_bcn_tmpl(arvif);
2151                if (ret)
2152                        ath11k_warn(ar->ab, "failed to update bcn template: %d\n",
2153                                    ret);
2154        }
2155
2156        if (changed & (BSS_CHANGED_BEACON_INFO | BSS_CHANGED_BEACON)) {
2157                arvif->dtim_period = info->dtim_period;
2158
2159                param_id = WMI_VDEV_PARAM_DTIM_PERIOD;
2160                ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
2161                                                    param_id,
2162                                                    arvif->dtim_period);
2163
2164                if (ret)
2165                        ath11k_warn(ar->ab, "Failed to set dtim period for VDEV %d: %i\n",
2166                                    arvif->vdev_id, ret);
2167                else
2168                        ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
2169                                   "DTIM period: %d set for VDEV: %d\n",
2170                                   arvif->dtim_period, arvif->vdev_id);
2171        }
2172
2173        if (changed & BSS_CHANGED_SSID &&
2174            vif->type == NL80211_IFTYPE_AP) {
2175                arvif->u.ap.ssid_len = info->ssid_len;
2176                if (info->ssid_len)
2177                        memcpy(arvif->u.ap.ssid, info->ssid, info->ssid_len);
2178                arvif->u.ap.hidden_ssid = info->hidden_ssid;
2179        }
2180
2181        if (changed & BSS_CHANGED_BSSID && !is_zero_ether_addr(info->bssid))
2182                ether_addr_copy(arvif->bssid, info->bssid);
2183
2184        if (changed & BSS_CHANGED_BEACON_ENABLED) {
2185                ath11k_control_beaconing(arvif, info);
2186
2187                if (arvif->is_up && vif->bss_conf.he_support &&
2188                    vif->bss_conf.he_oper.params) {
2189                        ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
2190                                                            WMI_VDEV_PARAM_BA_MODE,
2191                                                            WMI_BA_MODE_BUFFER_SIZE_256);
2192                        if (ret)
2193                                ath11k_warn(ar->ab,
2194                                            "failed to set BA BUFFER SIZE 256 for vdev: %d\n",
2195                                            arvif->vdev_id);
2196
2197                        param_id = WMI_VDEV_PARAM_HEOPS_0_31;
2198                        param_value = vif->bss_conf.he_oper.params;
2199                        ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
2200                                                            param_id, param_value);
2201                        ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
2202                                   "he oper param: %x set for VDEV: %d\n",
2203                                   param_value, arvif->vdev_id);
2204
2205                        if (ret)
2206                                ath11k_warn(ar->ab, "Failed to set he oper params %x for VDEV %d: %i\n",
2207                                            param_value, arvif->vdev_id, ret);
2208                }
2209        }
2210
2211        if (changed & BSS_CHANGED_ERP_CTS_PROT) {
2212                u32 cts_prot;
2213
2214                cts_prot = !!(info->use_cts_prot);
2215                param_id = WMI_VDEV_PARAM_PROTECTION_MODE;
2216
2217                if (arvif->is_started) {
2218                        ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
2219                                                            param_id, cts_prot);
2220                        if (ret)
2221                                ath11k_warn(ar->ab, "Failed to set CTS prot for VDEV: %d\n",
2222                                            arvif->vdev_id);
2223                        else
2224                                ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "Set CTS prot: %d for VDEV: %d\n",
2225                                           cts_prot, arvif->vdev_id);
2226                } else {
2227                        ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "defer protection mode setup, vdev is not ready yet\n");
2228                }
2229        }
2230
2231        if (changed & BSS_CHANGED_ERP_SLOT) {
2232                u32 slottime;
2233
2234                if (info->use_short_slot)
2235                        slottime = WMI_VDEV_SLOT_TIME_SHORT; /* 9us */
2236
2237                else
2238                        slottime = WMI_VDEV_SLOT_TIME_LONG; /* 20us */
2239
2240                param_id = WMI_VDEV_PARAM_SLOT_TIME;
2241                ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
2242                                                    param_id, slottime);
2243                if (ret)
2244                        ath11k_warn(ar->ab, "Failed to set erp slot for VDEV: %d\n",
2245                                    arvif->vdev_id);
2246                else
2247                        ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
2248                                   "Set slottime: %d for VDEV: %d\n",
2249                                   slottime, arvif->vdev_id);
2250        }
2251
2252        if (changed & BSS_CHANGED_ERP_PREAMBLE) {
2253                u32 preamble;
2254
2255                if (info->use_short_preamble)
2256                        preamble = WMI_VDEV_PREAMBLE_SHORT;
2257                else
2258                        preamble = WMI_VDEV_PREAMBLE_LONG;
2259
2260                param_id = WMI_VDEV_PARAM_PREAMBLE;
2261                ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
2262                                                    param_id, preamble);
2263                if (ret)
2264                        ath11k_warn(ar->ab, "Failed to set preamble for VDEV: %d\n",
2265                                    arvif->vdev_id);
2266                else
2267                        ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
2268                                   "Set preamble: %d for VDEV: %d\n",
2269                                   preamble, arvif->vdev_id);
2270        }
2271
2272        if (changed & BSS_CHANGED_ASSOC) {
2273                if (info->assoc)
2274                        ath11k_bss_assoc(hw, vif, info);
2275                else
2276                        ath11k_bss_disassoc(hw, vif);
2277        }
2278
2279        if (changed & BSS_CHANGED_TXPOWER) {
2280                ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac vdev_id %i txpower %d\n",
2281                           arvif->vdev_id, info->txpower);
2282
2283                arvif->txpower = info->txpower;
2284                ath11k_mac_txpower_recalc(ar);
2285        }
2286
2287        if (changed & BSS_CHANGED_MCAST_RATE &&
2288            !ath11k_mac_vif_chan(arvif->vif, &def)) {
2289                band = def.chan->band;
2290                mcast_rate = vif->bss_conf.mcast_rate[band];
2291
2292                if (mcast_rate > 0)
2293                        rateidx = mcast_rate - 1;
2294                else
2295                        rateidx = ffs(vif->bss_conf.basic_rates) - 1;
2296
2297                if (ar->pdev->cap.supported_bands & WMI_HOST_WLAN_5G_CAP)
2298                        rateidx += ATH11K_MAC_FIRST_OFDM_RATE_IDX;
2299
2300                bitrate = ath11k_legacy_rates[rateidx].bitrate;
2301                hw_value = ath11k_legacy_rates[rateidx].hw_value;
2302
2303                if (ath11k_mac_bitrate_is_cck(bitrate))
2304                        preamble = WMI_RATE_PREAMBLE_CCK;
2305                else
2306                        preamble = WMI_RATE_PREAMBLE_OFDM;
2307
2308                rate = ATH11K_HW_RATE_CODE(hw_value, 0, preamble);
2309
2310                ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
2311                           "mac vdev %d mcast_rate %x\n",
2312                           arvif->vdev_id, rate);
2313
2314                vdev_param = WMI_VDEV_PARAM_MCAST_DATA_RATE;
2315                ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
2316                                                    vdev_param, rate);
2317                if (ret)
2318                        ath11k_warn(ar->ab,
2319                                    "failed to set mcast rate on vdev %i: %d\n",
2320                                    arvif->vdev_id,  ret);
2321
2322                vdev_param = WMI_VDEV_PARAM_BCAST_DATA_RATE;
2323                ret = ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
2324                                                    vdev_param, rate);
2325                if (ret)
2326                        ath11k_warn(ar->ab,
2327                                    "failed to set bcast rate on vdev %i: %d\n",
2328                                    arvif->vdev_id,  ret);
2329        }
2330
2331        if (changed & BSS_CHANGED_BASIC_RATES &&
2332            !ath11k_mac_vif_chan(arvif->vif, &def))
2333                ath11k_recalculate_mgmt_rate(ar, vif, &def);
2334
2335        if (changed & BSS_CHANGED_TWT) {
2336                if (info->twt_requester || info->twt_responder)
2337                        ath11k_wmi_send_twt_enable_cmd(ar, ar->pdev->pdev_id);
2338                else
2339                        ath11k_wmi_send_twt_disable_cmd(ar, ar->pdev->pdev_id);
2340        }
2341
2342        if (changed & BSS_CHANGED_HE_OBSS_PD)
2343                ath11k_mac_config_obss_pd(ar, &info->he_obss_pd);
2344
2345        if (changed & BSS_CHANGED_HE_BSS_COLOR) {
2346                if (vif->type == NL80211_IFTYPE_AP) {
2347                        ret = ath11k_wmi_send_obss_color_collision_cfg_cmd(
2348                                ar, arvif->vdev_id, info->he_bss_color.color,
2349                                ATH11K_BSS_COLOR_COLLISION_DETECTION_AP_PERIOD_MS,
2350                                info->he_bss_color.enabled);
2351                        if (ret)
2352                                ath11k_warn(ar->ab, "failed to set bss color collision on vdev %i: %d\n",
2353                                            arvif->vdev_id,  ret);
2354                } else if (vif->type == NL80211_IFTYPE_STATION) {
2355                        ret = ath11k_wmi_send_bss_color_change_enable_cmd(ar,
2356                                                                          arvif->vdev_id,
2357                                                                          1);
2358                        if (ret)
2359                                ath11k_warn(ar->ab, "failed to enable bss color change on vdev %i: %d\n",
2360                                            arvif->vdev_id,  ret);
2361                        ret = ath11k_wmi_send_obss_color_collision_cfg_cmd(
2362                                ar, arvif->vdev_id, 0,
2363                                ATH11K_BSS_COLOR_COLLISION_DETECTION_STA_PERIOD_MS, 1);
2364                        if (ret)
2365                                ath11k_warn(ar->ab, "failed to set bss color collision on vdev %i: %d\n",
2366                                            arvif->vdev_id,  ret);
2367                }
2368        }
2369
2370        if (changed & BSS_CHANGED_FILS_DISCOVERY ||
2371            changed & BSS_CHANGED_UNSOL_BCAST_PROBE_RESP)
2372                ath11k_mac_fils_discovery(arvif, info);
2373
2374        mutex_unlock(&ar->conf_mutex);
2375}
2376
2377void __ath11k_mac_scan_finish(struct ath11k *ar)
2378{
2379        lockdep_assert_held(&ar->data_lock);
2380
2381        switch (ar->scan.state) {
2382        case ATH11K_SCAN_IDLE:
2383                break;
2384        case ATH11K_SCAN_RUNNING:
2385        case ATH11K_SCAN_ABORTING:
2386                if (!ar->scan.is_roc) {
2387                        struct cfg80211_scan_info info = {
2388                                .aborted = (ar->scan.state ==
2389                                            ATH11K_SCAN_ABORTING),
2390                        };
2391
2392                        ieee80211_scan_completed(ar->hw, &info);
2393                } else if (ar->scan.roc_notify) {
2394                        ieee80211_remain_on_channel_expired(ar->hw);
2395                }
2396                fallthrough;
2397        case ATH11K_SCAN_STARTING:
2398                ar->scan.state = ATH11K_SCAN_IDLE;
2399                ar->scan_channel = NULL;
2400                ar->scan.roc_freq = 0;
2401                cancel_delayed_work(&ar->scan.timeout);
2402                complete(&ar->scan.completed);
2403                break;
2404        }
2405}
2406
2407void ath11k_mac_scan_finish(struct ath11k *ar)
2408{
2409        spin_lock_bh(&ar->data_lock);
2410        __ath11k_mac_scan_finish(ar);
2411        spin_unlock_bh(&ar->data_lock);
2412}
2413
2414static int ath11k_scan_stop(struct ath11k *ar)
2415{
2416        struct scan_cancel_param arg = {
2417                .req_type = WLAN_SCAN_CANCEL_SINGLE,
2418                .scan_id = ATH11K_SCAN_ID,
2419        };
2420        int ret;
2421
2422        lockdep_assert_held(&ar->conf_mutex);
2423
2424        /* TODO: Fill other STOP Params */
2425        arg.pdev_id = ar->pdev->pdev_id;
2426
2427        ret = ath11k_wmi_send_scan_stop_cmd(ar, &arg);
2428        if (ret) {
2429                ath11k_warn(ar->ab, "failed to stop wmi scan: %d\n", ret);
2430                goto out;
2431        }
2432
2433        ret = wait_for_completion_timeout(&ar->scan.completed, 3 * HZ);
2434        if (ret == 0) {
2435                ath11k_warn(ar->ab,
2436                            "failed to receive scan abort comple: timed out\n");
2437                ret = -ETIMEDOUT;
2438        } else if (ret > 0) {
2439                ret = 0;
2440        }
2441
2442out:
2443        /* Scan state should be updated upon scan completion but in case
2444         * firmware fails to deliver the event (for whatever reason) it is
2445         * desired to clean up scan state anyway. Firmware may have just
2446         * dropped the scan completion event delivery due to transport pipe
2447         * being overflown with data and/or it can recover on its own before
2448         * next scan request is submitted.
2449         */
2450        spin_lock_bh(&ar->data_lock);
2451        if (ar->scan.state != ATH11K_SCAN_IDLE)
2452                __ath11k_mac_scan_finish(ar);
2453        spin_unlock_bh(&ar->data_lock);
2454
2455        return ret;
2456}
2457
2458static void ath11k_scan_abort(struct ath11k *ar)
2459{
2460        int ret;
2461
2462        lockdep_assert_held(&ar->conf_mutex);
2463
2464        spin_lock_bh(&ar->data_lock);
2465
2466        switch (ar->scan.state) {
2467        case ATH11K_SCAN_IDLE:
2468                /* This can happen if timeout worker kicked in and called
2469                 * abortion while scan completion was being processed.
2470                 */
2471                break;
2472        case ATH11K_SCAN_STARTING:
2473        case ATH11K_SCAN_ABORTING:
2474                ath11k_warn(ar->ab, "refusing scan abortion due to invalid scan state: %d\n",
2475                            ar->scan.state);
2476                break;
2477        case ATH11K_SCAN_RUNNING:
2478                ar->scan.state = ATH11K_SCAN_ABORTING;
2479                spin_unlock_bh(&ar->data_lock);
2480
2481                ret = ath11k_scan_stop(ar);
2482                if (ret)
2483                        ath11k_warn(ar->ab, "failed to abort scan: %d\n", ret);
2484
2485                spin_lock_bh(&ar->data_lock);
2486                break;
2487        }
2488
2489        spin_unlock_bh(&ar->data_lock);
2490}
2491
2492static void ath11k_scan_timeout_work(struct work_struct *work)
2493{
2494        struct ath11k *ar = container_of(work, struct ath11k,
2495                                         scan.timeout.work);
2496
2497        mutex_lock(&ar->conf_mutex);
2498        ath11k_scan_abort(ar);
2499        mutex_unlock(&ar->conf_mutex);
2500}
2501
2502static int ath11k_start_scan(struct ath11k *ar,
2503                             struct scan_req_params *arg)
2504{
2505        int ret;
2506
2507        lockdep_assert_held(&ar->conf_mutex);
2508
2509        if (ath11k_spectral_get_mode(ar) == ATH11K_SPECTRAL_BACKGROUND)
2510                ath11k_spectral_reset_buffer(ar);
2511
2512        ret = ath11k_wmi_send_scan_start_cmd(ar, arg);
2513        if (ret)
2514                return ret;
2515
2516        ret = wait_for_completion_timeout(&ar->scan.started, 1 * HZ);
2517        if (ret == 0) {
2518                ret = ath11k_scan_stop(ar);
2519                if (ret)
2520                        ath11k_warn(ar->ab, "failed to stop scan: %d\n", ret);
2521
2522                return -ETIMEDOUT;
2523        }
2524
2525        /* If we failed to start the scan, return error code at
2526         * this point.  This is probably due to some issue in the
2527         * firmware, but no need to wedge the driver due to that...
2528         */
2529        spin_lock_bh(&ar->data_lock);
2530        if (ar->scan.state == ATH11K_SCAN_IDLE) {
2531                spin_unlock_bh(&ar->data_lock);
2532                return -EINVAL;
2533        }
2534        spin_unlock_bh(&ar->data_lock);
2535
2536        return 0;
2537}
2538
2539static int ath11k_mac_op_hw_scan(struct ieee80211_hw *hw,
2540                                 struct ieee80211_vif *vif,
2541                                 struct ieee80211_scan_request *hw_req)
2542{
2543        struct ath11k *ar = hw->priv;
2544        struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
2545        struct cfg80211_scan_request *req = &hw_req->req;
2546        struct scan_req_params arg;
2547        int ret = 0;
2548        int i;
2549
2550        mutex_lock(&ar->conf_mutex);
2551
2552        spin_lock_bh(&ar->data_lock);
2553        switch (ar->scan.state) {
2554        case ATH11K_SCAN_IDLE:
2555                reinit_completion(&ar->scan.started);
2556                reinit_completion(&ar->scan.completed);
2557                ar->scan.state = ATH11K_SCAN_STARTING;
2558                ar->scan.is_roc = false;
2559                ar->scan.vdev_id = arvif->vdev_id;
2560                ret = 0;
2561                break;
2562        case ATH11K_SCAN_STARTING:
2563        case ATH11K_SCAN_RUNNING:
2564        case ATH11K_SCAN_ABORTING:
2565                ret = -EBUSY;
2566                break;
2567        }
2568        spin_unlock_bh(&ar->data_lock);
2569
2570        if (ret)
2571                goto exit;
2572
2573        memset(&arg, 0, sizeof(arg));
2574        ath11k_wmi_start_scan_init(ar, &arg);
2575        arg.vdev_id = arvif->vdev_id;
2576        arg.scan_id = ATH11K_SCAN_ID;
2577
2578        if (req->ie_len) {
2579                arg.extraie.len = req->ie_len;
2580                arg.extraie.ptr = kzalloc(req->ie_len, GFP_KERNEL);
2581                memcpy(arg.extraie.ptr, req->ie, req->ie_len);
2582        }
2583
2584        if (req->n_ssids) {
2585                arg.num_ssids = req->n_ssids;
2586                for (i = 0; i < arg.num_ssids; i++) {
2587                        arg.ssid[i].length  = req->ssids[i].ssid_len;
2588                        memcpy(&arg.ssid[i].ssid, req->ssids[i].ssid,
2589                               req->ssids[i].ssid_len);
2590                }
2591        } else {
2592                arg.scan_flags |= WMI_SCAN_FLAG_PASSIVE;
2593        }
2594
2595        if (req->n_channels) {
2596                arg.num_chan = req->n_channels;
2597                for (i = 0; i < arg.num_chan; i++)
2598                        arg.chan_list[i] = req->channels[i]->center_freq;
2599        }
2600
2601        ret = ath11k_start_scan(ar, &arg);
2602        if (ret) {
2603                ath11k_warn(ar->ab, "failed to start hw scan: %d\n", ret);
2604                spin_lock_bh(&ar->data_lock);
2605                ar->scan.state = ATH11K_SCAN_IDLE;
2606                spin_unlock_bh(&ar->data_lock);
2607        }
2608
2609        /* Add a 200ms margin to account for event/command processing */
2610        ieee80211_queue_delayed_work(ar->hw, &ar->scan.timeout,
2611                                     msecs_to_jiffies(arg.max_scan_time +
2612                                                      ATH11K_MAC_SCAN_TIMEOUT_MSECS));
2613
2614exit:
2615        if (req->ie_len)
2616                kfree(arg.extraie.ptr);
2617
2618        mutex_unlock(&ar->conf_mutex);
2619        return ret;
2620}
2621
2622static void ath11k_mac_op_cancel_hw_scan(struct ieee80211_hw *hw,
2623                                         struct ieee80211_vif *vif)
2624{
2625        struct ath11k *ar = hw->priv;
2626
2627        mutex_lock(&ar->conf_mutex);
2628        ath11k_scan_abort(ar);
2629        mutex_unlock(&ar->conf_mutex);
2630
2631        cancel_delayed_work_sync(&ar->scan.timeout);
2632}
2633
2634static int ath11k_install_key(struct ath11k_vif *arvif,
2635                              struct ieee80211_key_conf *key,
2636                              enum set_key_cmd cmd,
2637                              const u8 *macaddr, u32 flags)
2638{
2639        int ret;
2640        struct ath11k *ar = arvif->ar;
2641        struct wmi_vdev_install_key_arg arg = {
2642                .vdev_id = arvif->vdev_id,
2643                .key_idx = key->keyidx,
2644                .key_len = key->keylen,
2645                .key_data = key->key,
2646                .key_flags = flags,
2647                .macaddr = macaddr,
2648        };
2649
2650        lockdep_assert_held(&arvif->ar->conf_mutex);
2651
2652        reinit_completion(&ar->install_key_done);
2653
2654        if (test_bit(ATH11K_FLAG_HW_CRYPTO_DISABLED, &ar->ab->dev_flags))
2655                return 0;
2656
2657        if (cmd == DISABLE_KEY) {
2658                /* TODO: Check if FW expects  value other than NONE for del */
2659                /* arg.key_cipher = WMI_CIPHER_NONE; */
2660                arg.key_len = 0;
2661                arg.key_data = NULL;
2662                goto install;
2663        }
2664
2665        switch (key->cipher) {
2666        case WLAN_CIPHER_SUITE_CCMP:
2667                arg.key_cipher = WMI_CIPHER_AES_CCM;
2668                /* TODO: Re-check if flag is valid */
2669                key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV_MGMT;
2670                break;
2671        case WLAN_CIPHER_SUITE_TKIP:
2672                arg.key_cipher = WMI_CIPHER_TKIP;
2673                arg.key_txmic_len = 8;
2674                arg.key_rxmic_len = 8;
2675                break;
2676        case WLAN_CIPHER_SUITE_CCMP_256:
2677                arg.key_cipher = WMI_CIPHER_AES_CCM;
2678                break;
2679        case WLAN_CIPHER_SUITE_GCMP:
2680        case WLAN_CIPHER_SUITE_GCMP_256:
2681                arg.key_cipher = WMI_CIPHER_AES_GCM;
2682                break;
2683        default:
2684                ath11k_warn(ar->ab, "cipher %d is not supported\n", key->cipher);
2685                return -EOPNOTSUPP;
2686        }
2687
2688        if (test_bit(ATH11K_FLAG_RAW_MODE, &ar->ab->dev_flags))
2689                key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV |
2690                              IEEE80211_KEY_FLAG_RESERVE_TAILROOM;
2691
2692install:
2693        ret = ath11k_wmi_vdev_install_key(arvif->ar, &arg);
2694
2695        if (ret)
2696                return ret;
2697
2698        if (!wait_for_completion_timeout(&ar->install_key_done, 1 * HZ))
2699                return -ETIMEDOUT;
2700
2701        return ar->install_key_status ? -EINVAL : 0;
2702}
2703
2704static int ath11k_clear_peer_keys(struct ath11k_vif *arvif,
2705                                  const u8 *addr)
2706{
2707        struct ath11k *ar = arvif->ar;
2708        struct ath11k_base *ab = ar->ab;
2709        struct ath11k_peer *peer;
2710        int first_errno = 0;
2711        int ret;
2712        int i;
2713        u32 flags = 0;
2714
2715        lockdep_assert_held(&ar->conf_mutex);
2716
2717        spin_lock_bh(&ab->base_lock);
2718        peer = ath11k_peer_find(ab, arvif->vdev_id, addr);
2719        spin_unlock_bh(&ab->base_lock);
2720
2721        if (!peer)
2722                return -ENOENT;
2723
2724        for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
2725                if (!peer->keys[i])
2726                        continue;
2727
2728                /* key flags are not required to delete the key */
2729                ret = ath11k_install_key(arvif, peer->keys[i],
2730                                         DISABLE_KEY, addr, flags);
2731                if (ret < 0 && first_errno == 0)
2732                        first_errno = ret;
2733
2734                if (ret < 0)
2735                        ath11k_warn(ab, "failed to remove peer key %d: %d\n",
2736                                    i, ret);
2737
2738                spin_lock_bh(&ab->base_lock);
2739                peer->keys[i] = NULL;
2740                spin_unlock_bh(&ab->base_lock);
2741        }
2742
2743        return first_errno;
2744}
2745
2746static int ath11k_mac_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
2747                                 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
2748                                 struct ieee80211_key_conf *key)
2749{
2750        struct ath11k *ar = hw->priv;
2751        struct ath11k_base *ab = ar->ab;
2752        struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
2753        struct ath11k_peer *peer;
2754        struct ath11k_sta *arsta;
2755        const u8 *peer_addr;
2756        int ret = 0;
2757        u32 flags = 0;
2758
2759        /* BIP needs to be done in software */
2760        if (key->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
2761            key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
2762            key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256 ||
2763            key->cipher == WLAN_CIPHER_SUITE_BIP_CMAC_256)
2764                return 1;
2765
2766        if (test_bit(ATH11K_FLAG_HW_CRYPTO_DISABLED, &ar->ab->dev_flags))
2767                return 1;
2768
2769        if (key->keyidx > WMI_MAX_KEY_INDEX)
2770                return -ENOSPC;
2771
2772        mutex_lock(&ar->conf_mutex);
2773
2774        if (sta)
2775                peer_addr = sta->addr;
2776        else if (arvif->vdev_type == WMI_VDEV_TYPE_STA)
2777                peer_addr = vif->bss_conf.bssid;
2778        else
2779                peer_addr = vif->addr;
2780
2781        key->hw_key_idx = key->keyidx;
2782
2783        /* the peer should not disappear in mid-way (unless FW goes awry) since
2784         * we already hold conf_mutex. we just make sure its there now.
2785         */
2786        spin_lock_bh(&ab->base_lock);
2787        peer = ath11k_peer_find(ab, arvif->vdev_id, peer_addr);
2788
2789        /* flush the fragments cache during key (re)install to
2790         * ensure all frags in the new frag list belong to the same key.
2791         */
2792        if (peer && cmd == SET_KEY)
2793                ath11k_peer_frags_flush(ar, peer);
2794        spin_unlock_bh(&ab->base_lock);
2795
2796        if (!peer) {
2797                if (cmd == SET_KEY) {
2798                        ath11k_warn(ab, "cannot install key for non-existent peer %pM\n",
2799                                    peer_addr);
2800                        ret = -EOPNOTSUPP;
2801                        goto exit;
2802                } else {
2803                        /* if the peer doesn't exist there is no key to disable
2804                         * anymore
2805                         */
2806                        goto exit;
2807                }
2808        }
2809
2810        if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
2811                flags |= WMI_KEY_PAIRWISE;
2812        else
2813                flags |= WMI_KEY_GROUP;
2814
2815        ret = ath11k_install_key(arvif, key, cmd, peer_addr, flags);
2816        if (ret) {
2817                ath11k_warn(ab, "ath11k_install_key failed (%d)\n", ret);
2818                goto exit;
2819        }
2820
2821        ret = ath11k_dp_peer_rx_pn_replay_config(arvif, peer_addr, cmd, key);
2822        if (ret) {
2823                ath11k_warn(ab, "failed to offload PN replay detection %d\n", ret);
2824                goto exit;
2825        }
2826
2827        spin_lock_bh(&ab->base_lock);
2828        peer = ath11k_peer_find(ab, arvif->vdev_id, peer_addr);
2829        if (peer && cmd == SET_KEY) {
2830                peer->keys[key->keyidx] = key;
2831                if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
2832                        peer->ucast_keyidx = key->keyidx;
2833                        peer->sec_type = ath11k_dp_tx_get_encrypt_type(key->cipher);
2834                } else {
2835                        peer->mcast_keyidx = key->keyidx;
2836                        peer->sec_type_grp = ath11k_dp_tx_get_encrypt_type(key->cipher);
2837                }
2838        } else if (peer && cmd == DISABLE_KEY) {
2839                peer->keys[key->keyidx] = NULL;
2840                if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
2841                        peer->ucast_keyidx = 0;
2842                else
2843                        peer->mcast_keyidx = 0;
2844        } else if (!peer)
2845                /* impossible unless FW goes crazy */
2846                ath11k_warn(ab, "peer %pM disappeared!\n", peer_addr);
2847
2848        if (sta) {
2849                arsta = (struct ath11k_sta *)sta->drv_priv;
2850
2851                switch (key->cipher) {
2852                case WLAN_CIPHER_SUITE_TKIP:
2853                case WLAN_CIPHER_SUITE_CCMP:
2854                case WLAN_CIPHER_SUITE_CCMP_256:
2855                case WLAN_CIPHER_SUITE_GCMP:
2856                case WLAN_CIPHER_SUITE_GCMP_256:
2857                        if (cmd == SET_KEY)
2858                                arsta->pn_type = HAL_PN_TYPE_WPA;
2859                        else
2860                                arsta->pn_type = HAL_PN_TYPE_NONE;
2861                        break;
2862                default:
2863                        arsta->pn_type = HAL_PN_TYPE_NONE;
2864                        break;
2865                }
2866        }
2867
2868        spin_unlock_bh(&ab->base_lock);
2869
2870exit:
2871        mutex_unlock(&ar->conf_mutex);
2872        return ret;
2873}
2874
2875static int
2876ath11k_mac_bitrate_mask_num_vht_rates(struct ath11k *ar,
2877                                      enum nl80211_band band,
2878                                      const struct cfg80211_bitrate_mask *mask)
2879{
2880        int num_rates = 0;
2881        int i;
2882
2883        for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++)
2884                num_rates += hweight16(mask->control[band].vht_mcs[i]);
2885
2886        return num_rates;
2887}
2888
2889static int
2890ath11k_mac_set_peer_vht_fixed_rate(struct ath11k_vif *arvif,
2891                                   struct ieee80211_sta *sta,
2892                                   const struct cfg80211_bitrate_mask *mask,
2893                                   enum nl80211_band band)
2894{
2895        struct ath11k *ar = arvif->ar;
2896        u8 vht_rate, nss;
2897        u32 rate_code;
2898        int ret, i;
2899
2900        lockdep_assert_held(&ar->conf_mutex);
2901
2902        nss = 0;
2903
2904        for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
2905                if (hweight16(mask->control[band].vht_mcs[i]) == 1) {
2906                        nss = i + 1;
2907                        vht_rate = ffs(mask->control[band].vht_mcs[i]) - 1;
2908                }
2909        }
2910
2911        if (!nss) {
2912                ath11k_warn(ar->ab, "No single VHT Fixed rate found to set for %pM",
2913                            sta->addr);
2914                return -EINVAL;
2915        }
2916
2917        ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
2918                   "Setting Fixed VHT Rate for peer %pM. Device will not switch to any other selected rates",
2919                   sta->addr);
2920
2921        rate_code = ATH11K_HW_RATE_CODE(vht_rate, nss - 1,
2922                                        WMI_RATE_PREAMBLE_VHT);
2923        ret = ath11k_wmi_set_peer_param(ar, sta->addr,
2924                                        arvif->vdev_id,
2925                                        WMI_PEER_PARAM_FIXED_RATE,
2926                                        rate_code);
2927        if (ret)
2928                ath11k_warn(ar->ab,
2929                            "failed to update STA %pM Fixed Rate %d: %d\n",
2930                             sta->addr, rate_code, ret);
2931
2932        return ret;
2933}
2934
2935static int ath11k_station_assoc(struct ath11k *ar,
2936                                struct ieee80211_vif *vif,
2937                                struct ieee80211_sta *sta,
2938                                bool reassoc)
2939{
2940        struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
2941        struct peer_assoc_params peer_arg;
2942        int ret = 0;
2943        struct cfg80211_chan_def def;
2944        enum nl80211_band band;
2945        struct cfg80211_bitrate_mask *mask;
2946        u8 num_vht_rates;
2947
2948        lockdep_assert_held(&ar->conf_mutex);
2949
2950        if (WARN_ON(ath11k_mac_vif_chan(vif, &def)))
2951                return -EPERM;
2952
2953        band = def.chan->band;
2954        mask = &arvif->bitrate_mask;
2955
2956        ath11k_peer_assoc_prepare(ar, vif, sta, &peer_arg, reassoc);
2957
2958        ret = ath11k_wmi_send_peer_assoc_cmd(ar, &peer_arg);
2959        if (ret) {
2960                ath11k_warn(ar->ab, "failed to run peer assoc for STA %pM vdev %i: %d\n",
2961                            sta->addr, arvif->vdev_id, ret);
2962                return ret;
2963        }
2964
2965        if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ)) {
2966                ath11k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
2967                            sta->addr, arvif->vdev_id);
2968                return -ETIMEDOUT;
2969        }
2970
2971        num_vht_rates = ath11k_mac_bitrate_mask_num_vht_rates(ar, band, mask);
2972
2973        /* If single VHT rate is configured (by set_bitrate_mask()),
2974         * peer_assoc will disable VHT. This is now enabled by a peer specific
2975         * fixed param.
2976         * Note that all other rates and NSS will be disabled for this peer.
2977         */
2978        if (sta->vht_cap.vht_supported && num_vht_rates == 1) {
2979                ret = ath11k_mac_set_peer_vht_fixed_rate(arvif, sta, mask,
2980                                                         band);
2981                if (ret)
2982                        return ret;
2983        }
2984
2985        /* Re-assoc is run only to update supported rates for given station. It
2986         * doesn't make much sense to reconfigure the peer completely.
2987         */
2988        if (reassoc)
2989                return 0;
2990
2991        ret = ath11k_setup_peer_smps(ar, arvif, sta->addr,
2992                                     &sta->ht_cap);
2993        if (ret) {
2994                ath11k_warn(ar->ab, "failed to setup peer SMPS for vdev %d: %d\n",
2995                            arvif->vdev_id, ret);
2996                return ret;
2997        }
2998
2999        if (!sta->wme) {
3000                arvif->num_legacy_stations++;
3001                ret = ath11k_recalc_rtscts_prot(arvif);
3002                if (ret)
3003                        return ret;
3004        }
3005
3006        if (sta->wme && sta->uapsd_queues) {
3007                ret = ath11k_peer_assoc_qos_ap(ar, arvif, sta);
3008                if (ret) {
3009                        ath11k_warn(ar->ab, "failed to set qos params for STA %pM for vdev %i: %d\n",
3010                                    sta->addr, arvif->vdev_id, ret);
3011                        return ret;
3012                }
3013        }
3014
3015        return 0;
3016}
3017
3018static int ath11k_station_disassoc(struct ath11k *ar,
3019                                   struct ieee80211_vif *vif,
3020                                   struct ieee80211_sta *sta)
3021{
3022        struct ath11k_vif *arvif = (void *)vif->drv_priv;
3023        int ret = 0;
3024
3025        lockdep_assert_held(&ar->conf_mutex);
3026
3027        if (!sta->wme) {
3028                arvif->num_legacy_stations--;
3029                ret = ath11k_recalc_rtscts_prot(arvif);
3030                if (ret)
3031                        return ret;
3032        }
3033
3034        ret = ath11k_clear_peer_keys(arvif, sta->addr);
3035        if (ret) {
3036                ath11k_warn(ar->ab, "failed to clear all peer keys for vdev %i: %d\n",
3037                            arvif->vdev_id, ret);
3038                return ret;
3039        }
3040        return 0;
3041}
3042
3043static void ath11k_sta_rc_update_wk(struct work_struct *wk)
3044{
3045        struct ath11k *ar;
3046        struct ath11k_vif *arvif;
3047        struct ath11k_sta *arsta;
3048        struct ieee80211_sta *sta;
3049        struct cfg80211_chan_def def;
3050        enum nl80211_band band;
3051        const u8 *ht_mcs_mask;
3052        const u16 *vht_mcs_mask;
3053        u32 changed, bw, nss, smps;
3054        int err, num_vht_rates;
3055        const struct cfg80211_bitrate_mask *mask;
3056        struct peer_assoc_params peer_arg;
3057
3058        arsta = container_of(wk, struct ath11k_sta, update_wk);
3059        sta = container_of((void *)arsta, struct ieee80211_sta, drv_priv);
3060        arvif = arsta->arvif;
3061        ar = arvif->ar;
3062
3063        if (WARN_ON(ath11k_mac_vif_chan(arvif->vif, &def)))
3064                return;
3065
3066        band = def.chan->band;
3067        ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
3068        vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
3069
3070        spin_lock_bh(&ar->data_lock);
3071
3072        changed = arsta->changed;
3073        arsta->changed = 0;
3074
3075        bw = arsta->bw;
3076        nss = arsta->nss;
3077        smps = arsta->smps;
3078
3079        spin_unlock_bh(&ar->data_lock);
3080
3081        mutex_lock(&ar->conf_mutex);
3082
3083        nss = max_t(u32, 1, nss);
3084        nss = min(nss, max(ath11k_mac_max_ht_nss(ht_mcs_mask),
3085                           ath11k_mac_max_vht_nss(vht_mcs_mask)));
3086
3087        if (changed & IEEE80211_RC_BW_CHANGED) {
3088                err = ath11k_wmi_set_peer_param(ar, sta->addr, arvif->vdev_id,
3089                                                WMI_PEER_CHWIDTH, bw);
3090                if (err)
3091                        ath11k_warn(ar->ab, "failed to update STA %pM peer bw %d: %d\n",
3092                                    sta->addr, bw, err);
3093        }
3094
3095        if (changed & IEEE80211_RC_NSS_CHANGED) {
3096                ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac update sta %pM nss %d\n",
3097                           sta->addr, nss);
3098
3099                err = ath11k_wmi_set_peer_param(ar, sta->addr, arvif->vdev_id,
3100                                                WMI_PEER_NSS, nss);
3101                if (err)
3102                        ath11k_warn(ar->ab, "failed to update STA %pM nss %d: %d\n",
3103                                    sta->addr, nss, err);
3104        }
3105
3106        if (changed & IEEE80211_RC_SMPS_CHANGED) {
3107                ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "mac update sta %pM smps %d\n",
3108                           sta->addr, smps);
3109
3110                err = ath11k_wmi_set_peer_param(ar, sta->addr, arvif->vdev_id,
3111                                                WMI_PEER_MIMO_PS_STATE, smps);
3112                if (err)
3113                        ath11k_warn(ar->ab, "failed to update STA %pM smps %d: %d\n",
3114                                    sta->addr, smps, err);
3115        }
3116
3117        if (changed & IEEE80211_RC_SUPP_RATES_CHANGED) {
3118                mask = &arvif->bitrate_mask;
3119                num_vht_rates = ath11k_mac_bitrate_mask_num_vht_rates(ar, band,
3120                                                                      mask);
3121
3122                /* Peer_assoc_prepare will reject vht rates in
3123                 * bitrate_mask if its not available in range format and
3124                 * sets vht tx_rateset as unsupported. So multiple VHT MCS
3125                 * setting(eg. MCS 4,5,6) per peer is not supported here.
3126                 * But, Single rate in VHT mask can be set as per-peer
3127                 * fixed rate. But even if any HT rates are configured in
3128                 * the bitrate mask, device will not switch to those rates
3129                 * when per-peer Fixed rate is set.
3130                 * TODO: Check RATEMASK_CMDID to support auto rates selection
3131                 * across HT/VHT and for multiple VHT MCS support.
3132                 */
3133                if (sta->vht_cap.vht_supported && num_vht_rates == 1) {
3134                        ath11k_mac_set_peer_vht_fixed_rate(arvif, sta, mask,
3135                                                           band);
3136                } else {
3137                        /* If the peer is non-VHT or no fixed VHT rate
3138                         * is provided in the new bitrate mask we set the
3139                         * other rates using peer_assoc command.
3140                         */
3141                        ath11k_peer_assoc_prepare(ar, arvif->vif, sta,
3142                                                  &peer_arg, true);
3143
3144                        err = ath11k_wmi_send_peer_assoc_cmd(ar, &peer_arg);
3145                        if (err)
3146                                ath11k_warn(ar->ab, "failed to run peer assoc for STA %pM vdev %i: %d\n",
3147                                            sta->addr, arvif->vdev_id, err);
3148
3149                        if (!wait_for_completion_timeout(&ar->peer_assoc_done, 1 * HZ))
3150                                ath11k_warn(ar->ab, "failed to get peer assoc conf event for %pM vdev %i\n",
3151                                            sta->addr, arvif->vdev_id);
3152                }
3153        }
3154
3155        mutex_unlock(&ar->conf_mutex);
3156}
3157
3158static int ath11k_mac_inc_num_stations(struct ath11k_vif *arvif,
3159                                       struct ieee80211_sta *sta)
3160{
3161        struct ath11k *ar = arvif->ar;
3162
3163        lockdep_assert_held(&ar->conf_mutex);
3164
3165        if (arvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
3166                return 0;
3167
3168        if (ar->num_stations >= ar->max_num_stations)
3169                return -ENOBUFS;
3170
3171        ar->num_stations++;
3172
3173        return 0;
3174}
3175
3176static void ath11k_mac_dec_num_stations(struct ath11k_vif *arvif,
3177                                        struct ieee80211_sta *sta)
3178{
3179        struct ath11k *ar = arvif->ar;
3180
3181        lockdep_assert_held(&ar->conf_mutex);
3182
3183        if (arvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
3184                return;
3185
3186        ar->num_stations--;
3187}
3188
3189static int ath11k_mac_station_add(struct ath11k *ar,
3190                                  struct ieee80211_vif *vif,
3191                                  struct ieee80211_sta *sta)
3192{
3193        struct ath11k_base *ab = ar->ab;
3194        struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
3195        struct ath11k_sta *arsta = (struct ath11k_sta *)sta->drv_priv;
3196        struct peer_create_params peer_param;
3197        int ret;
3198
3199        lockdep_assert_held(&ar->conf_mutex);
3200
3201        ret = ath11k_mac_inc_num_stations(arvif, sta);
3202        if (ret) {
3203                ath11k_warn(ab, "refusing to associate station: too many connected already (%d)\n",
3204                            ar->max_num_stations);
3205                goto exit;
3206        }
3207
3208        arsta->rx_stats = kzalloc(sizeof(*arsta->rx_stats), GFP_KERNEL);
3209        if (!arsta->rx_stats) {
3210                ret = -ENOMEM;
3211                goto dec_num_station;
3212        }
3213
3214        peer_param.vdev_id = arvif->vdev_id;
3215        peer_param.peer_addr = sta->addr;
3216        peer_param.peer_type = WMI_PEER_TYPE_DEFAULT;
3217
3218        ret = ath11k_peer_create(ar, arvif, sta, &peer_param);
3219        if (ret) {
3220                ath11k_warn(ab, "Failed to add peer: %pM for VDEV: %d\n",
3221                            sta->addr, arvif->vdev_id);
3222                goto free_rx_stats;
3223        }
3224
3225        ath11k_dbg(ab, ATH11K_DBG_MAC, "Added peer: %pM for VDEV: %d\n",
3226                   sta->addr, arvif->vdev_id);
3227
3228        if (ath11k_debugfs_is_extd_tx_stats_enabled(ar)) {
3229                arsta->tx_stats = kzalloc(sizeof(*arsta->tx_stats), GFP_KERNEL);
3230                if (!arsta->tx_stats) {
3231                        ret = -ENOMEM;
3232                        goto free_peer;
3233                }
3234        }
3235
3236        if (ieee80211_vif_is_mesh(vif)) {
3237                ret = ath11k_wmi_set_peer_param(ar, sta->addr,
3238                                                arvif->vdev_id,
3239                                                WMI_PEER_USE_4ADDR, 1);
3240                if (ret) {
3241                        ath11k_warn(ab, "failed to STA %pM 4addr capability: %d\n",
3242                                    sta->addr, ret);
3243                        goto free_tx_stats;
3244                }
3245        }
3246
3247        ret = ath11k_dp_peer_setup(ar, arvif->vdev_id, sta->addr);
3248        if (ret) {
3249                ath11k_warn(ab, "failed to setup dp for peer %pM on vdev %i (%d)\n",
3250                            sta->addr, arvif->vdev_id, ret);
3251                goto free_tx_stats;
3252        }
3253
3254        if (ab->hw_params.vdev_start_delay &&
3255            !arvif->is_started &&
3256            arvif->vdev_type != WMI_VDEV_TYPE_AP) {
3257                ret = ath11k_start_vdev_delay(ar->hw, vif);
3258                if (ret) {
3259                        ath11k_warn(ab, "failed to delay vdev start: %d\n", ret);
3260                        goto free_tx_stats;
3261                }
3262        }
3263
3264        return 0;
3265
3266free_tx_stats:
3267        kfree(arsta->tx_stats);
3268        arsta->tx_stats = NULL;
3269free_peer:
3270        ath11k_peer_delete(ar, arvif->vdev_id, sta->addr);
3271free_rx_stats:
3272        kfree(arsta->rx_stats);
3273        arsta->rx_stats = NULL;
3274dec_num_station:
3275        ath11k_mac_dec_num_stations(arvif, sta);
3276exit:
3277        return ret;
3278}
3279
3280static int ath11k_mac_op_sta_state(struct ieee80211_hw *hw,
3281                                   struct ieee80211_vif *vif,
3282                                   struct ieee80211_sta *sta,
3283                                   enum ieee80211_sta_state old_state,
3284                                   enum ieee80211_sta_state new_state)
3285{
3286        struct ath11k *ar = hw->priv;
3287        struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
3288        struct ath11k_sta *arsta = (struct ath11k_sta *)sta->drv_priv;
3289        struct ath11k_peer *peer;
3290        int ret = 0;
3291
3292        /* cancel must be done outside the mutex to avoid deadlock */
3293        if ((old_state == IEEE80211_STA_NONE &&
3294             new_state == IEEE80211_STA_NOTEXIST))
3295                cancel_work_sync(&arsta->update_wk);
3296
3297        mutex_lock(&ar->conf_mutex);
3298
3299        if (old_state == IEEE80211_STA_NOTEXIST &&
3300            new_state == IEEE80211_STA_NONE) {
3301                memset(arsta, 0, sizeof(*arsta));
3302                arsta->arvif = arvif;
3303                INIT_WORK(&arsta->update_wk, ath11k_sta_rc_update_wk);
3304
3305                ret = ath11k_mac_station_add(ar, vif, sta);
3306                if (ret)
3307                        ath11k_warn(ar->ab, "Failed to add station: %pM for VDEV: %d\n",
3308                                    sta->addr, arvif->vdev_id);
3309        } else if ((old_state == IEEE80211_STA_NONE &&
3310                    new_state == IEEE80211_STA_NOTEXIST)) {
3311                ath11k_dp_peer_cleanup(ar, arvif->vdev_id, sta->addr);
3312
3313                ret = ath11k_peer_delete(ar, arvif->vdev_id, sta->addr);
3314                if (ret)
3315                        ath11k_warn(ar->ab, "Failed to delete peer: %pM for VDEV: %d\n",
3316                                    sta->addr, arvif->vdev_id);
3317                else
3318                        ath11k_dbg(ar->ab, ATH11K_DBG_MAC, "Removed peer: %pM for VDEV: %d\n",
3319                                   sta->addr, arvif->vdev_id);
3320
3321                ath11k_mac_dec_num_stations(arvif, sta);
3322                spin_lock_bh(&ar->ab->base_lock);
3323                peer = ath11k_peer_find(ar->ab, arvif->vdev_id, sta->addr);
3324                if (peer && peer->sta == sta) {
3325                        ath11k_warn(ar->ab, "Found peer entry %pM n vdev %i after it was supposedly removed\n",
3326                                    vif->addr, arvif->vdev_id);
3327                        peer->sta = NULL;
3328                        list_del(&peer->list);
3329                        kfree(peer);
3330                        ar->num_peers--;
3331                }
3332                spin_unlock_bh(&ar->ab->base_lock);
3333
3334                kfree(arsta->tx_stats);
3335                arsta->tx_stats = NULL;
3336
3337                kfree(arsta->rx_stats);
3338                arsta->rx_stats = NULL;
3339        } else if (old_state == IEEE80211_STA_AUTH &&
3340                   new_state == IEEE80211_STA_ASSOC &&
3341                   (vif->type == NL80211_IFTYPE_AP ||
3342                    vif->type == NL80211_IFTYPE_MESH_POINT ||
3343                    vif->type == NL80211_IFTYPE_ADHOC)) {
3344                ret = ath11k_station_assoc(ar, vif, sta, false);
3345                if (ret)
3346                        ath11k_warn(ar->ab, "Failed to associate station: %pM\n",
3347                                    sta->addr);
3348        } else if (old_state == IEEE80211_STA_ASSOC &&
3349                   new_state == IEEE80211_STA_AUTH &&
3350                   (vif->type == NL80211_IFTYPE_AP ||
3351                    vif->type == NL80211_IFTYPE_MESH_POINT ||
3352                    vif->type == NL80211_IFTYPE_ADHOC)) {
3353                ret = ath11k_station_disassoc(ar, vif, sta);
3354                if (ret)
3355                        ath11k_warn(ar->ab, "Failed to disassociate station: %pM\n",
3356                                    sta->addr);
3357        }
3358
3359        mutex_unlock(&ar->conf_mutex);
3360        return ret;
3361}
3362
3363static int ath11k_mac_op_sta_set_txpwr(struct ieee80211_hw *hw,
3364                                       struct ieee80211_vif *vif,
3365                                       struct ieee80211_sta *sta)
3366{
3367        struct ath11k *ar = hw->priv;
3368        struct ath11k_vif *arvif = (void *)vif->drv_priv;
3369        int ret = 0;
3370        s16 txpwr;
3371
3372        if (sta->txpwr.type == NL80211_TX_POWER_AUTOMATIC) {
3373                txpwr = 0;
3374        } else {
3375                txpwr = sta->txpwr.power;
3376                if (!txpwr)
3377                        return -EINVAL;
3378        }
3379
3380        if (txpwr > ATH11K_TX_POWER_MAX_VAL || txpwr < ATH11K_TX_POWER_MIN_VAL)
3381                return -EINVAL;
3382
3383        mutex_lock(&ar->conf_mutex);
3384
3385        ret = ath11k_wmi_set_peer_param(ar, sta->addr, arvif->vdev_id,
3386                                        WMI_PEER_USE_FIXED_PWR, txpwr);
3387        if (ret) {
3388                ath11k_warn(ar->ab, "failed to set tx power for station ret: %d\n",
3389                            ret);
3390                goto out;
3391        }
3392
3393out:
3394        mutex_unlock(&ar->conf_mutex);
3395        return ret;
3396}
3397
3398static void ath11k_mac_op_sta_rc_update(struct ieee80211_hw *hw,
3399                                        struct ieee80211_vif *vif,
3400                                        struct ieee80211_sta *sta,
3401                                        u32 changed)
3402{
3403        struct ath11k *ar = hw->priv;
3404        struct ath11k_sta *arsta = (struct ath11k_sta *)sta->drv_priv;
3405        struct ath11k_vif *arvif = (void *)vif->drv_priv;
3406        struct ath11k_peer *peer;
3407        u32 bw, smps;
3408
3409        spin_lock_bh(&ar->ab->base_lock);
3410
3411        peer = ath11k_peer_find(ar->ab, arvif->vdev_id, sta->addr);
3412        if (!peer) {
3413                spin_unlock_bh(&ar->ab->base_lock);
3414                ath11k_warn(ar->ab, "mac sta rc update failed to find peer %pM on vdev %i\n",
3415                            sta->addr, arvif->vdev_id);
3416                return;
3417        }
3418
3419        spin_unlock_bh(&ar->ab->base_lock);
3420
3421        ath11k_dbg(ar->ab, ATH11K_DBG_MAC,
3422                   "mac sta rc update for %pM changed %08x bw %d nss %d smps %d\n",
3423                   sta->addr, changed, sta->bandwidth, sta->rx_nss,
3424                   sta->smps_mode);
3425
3426        spin_lock_bh(&ar->data_lock);
3427
3428        if (changed & IEEE80211_RC_BW_CHANGED) {
3429                bw = WMI_PEER_CHWIDTH_20MHZ;
3430
3431                switch (sta->bandwidth) {
3432                case IEEE80211_STA_RX_BW_20:
3433                        bw = WMI_PEER_CHWIDTH_20MHZ;
3434                        break;
3435                case IEEE80211_STA_RX_BW_40:
3436                        bw = WMI_PEER_CHWIDTH_40MHZ;
3437                        break;
3438                case IEEE80211_STA_RX_BW_80:
3439                        bw = WMI_PEER_CHWIDTH_80MHZ;
3440                        break;
3441                case IEEE80211_STA_RX_BW_160:
3442                        bw = WMI_PEER_CHWIDTH_160MHZ;
3443                        break;
3444                default:
3445                        ath11k_warn(ar->ab, "Invalid bandwidth %d in rc update for %pM\n",
3446                                    sta->bandwidth, sta->addr);
3447                        bw = WMI_PEER_CHWIDTH_20MHZ;
3448                        break;
3449                }
3450
3451                arsta->bw = bw;
3452        }
3453
3454        if (changed & IEEE80211_RC_NSS_CHANGED)
3455                arsta->nss = sta->rx_nss;
3456
3457        if (changed & IEEE80211_RC_SMPS_CHANGED) {
3458                smps = WMI_PEER_SMPS_PS_NONE;
3459
3460                switch (sta->smps_mode) {
3461                case IEEE80211_SMPS_AUTOMATIC:
3462                case IEEE80211_SMPS_OFF:
3463                        smps = WMI_PEER_SMPS_PS_NONE;
3464                        break;
3465                case IEEE80211_SMPS_STATIC:
3466                        smps = WMI_PEER_SMPS_STATIC;
3467                        break;
3468                case IEEE80211_SMPS_DYNAMIC:
3469                        smps = WMI_PEER_SMPS_DYNAMIC;
3470                        break;
3471                default:
3472                        ath11k_warn(ar->ab, "Invalid smps %d in sta rc update for %pM\n",
3473                                    sta->smps_mode, sta->addr);
3474                        smps = WMI_PEER_SMPS_PS_NONE;
3475                        break;
3476                }
3477
3478                arsta->smps = smps;
3479        }
3480
3481        arsta->changed |= changed;
3482
3483        spin_unlock_bh(&ar->data_lock);
3484
3485        ieee80211_queue_work(hw, &arsta->update_wk);
3486}
3487
3488static int ath11k_conf_tx_uapsd(struct ath11k *ar, struct ieee80211_vif *vif,
3489                                u16 ac, bool enable)
3490{
3491        struct ath11k_vif *arvif = ath11k_vif_to_arvif(vif);
3492        u32 value = 0;
3493        int ret = 0;
3494
3495        if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
3496                return 0;
3497
3498        switch (ac) {
3499        case IEEE80211_AC_VO:
3500                value = WMI_STA_PS_UAPSD_AC3_DELIVERY_EN |
3501                        WMI_STA_PS_UAPSD_AC3_TRIGGER_EN;
3502                break;
3503        case IEEE80211_AC_VI:
3504                value = WMI_STA_PS_UAPSD_AC2_DELIVERY_EN |
3505                        WMI_STA_PS_UAPSD_AC2_TRIGGER_EN;
3506                break;
3507        case IEEE80211_AC_BE:
3508                value = WMI_STA_PS_UAPSD_AC1_DELIVERY_EN |
3509                        WMI_STA_PS_UAPSD_AC1_TRIGGER_EN;
3510                break;
3511        case IEEE80211_AC_BK:
3512                value = WMI_STA_PS_UAPSD_AC0_DELIVERY_EN |
3513                        WMI_STA_PS_UAPSD_AC0_TRIGGER_EN;
3514                break;
3515        }
3516
3517        if (enable)
3518                arvif->u.sta.uapsd |= value;
3519        else
3520                arvif->u.sta.uapsd &= ~value;
3521
3522        ret = ath11k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
3523                                          WMI_STA_PS_PARAM_UAPSD,
3524                                          arvif->u.sta.uapsd);
3525        if (ret) {
3526                ath11k_warn(ar->ab, "could not set uapsd params %d\n", ret);
3527                goto exit;
3528        }
3529
3530        if (arvif->u.sta.uapsd)
3531                value = WMI_STA_PS_RX_WAKE_POLICY_POLL_UAPSD;
3532        else
3533                value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
3534
3535        ret = ath11k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
3536                                          WMI_STA_PS_PARAM_RX_WAKE_POLICY,
3537                                          value);
3538        if (ret)
3539                ath11k_warn(ar->ab, "could not set rx wake param %d\n", ret);
3540
3541exit:
3542        return ret;
3543}
3544
3545static int ath11k_mac_op_conf_tx(struct ieee80211_hw *hw,
3546                                 struct ieee80211_vif *vif, u16 ac,
3547                                 const struct ieee80211_tx_queue_params *params)
3548{
3549        struct ath11k *ar = hw->priv;
3550        struct ath11k_vif *arvif = (void *)vif->drv_priv;
3551        struct wmi_wmm_params_arg *p = NULL;
3552        int ret;
3553
3554        mutex_lock(&ar->conf_mutex);
3555
3556        switch (ac) {
3557        case IEEE80211_AC_VO:
3558                p = &arvif->wmm_params.ac_vo;
3559                break;
3560        case IEEE80211_AC_VI:
3561                p = &arvif->wmm_params.ac_vi;
3562                break;
3563        case IEEE80211_AC_BE:
3564                p = &arvif->wmm_params.ac_be;
3565                break;
3566        case IEEE80211_AC_BK:
3567                p = &arvif->wmm_params.ac_bk;
3568                break;
3569        }
3570
3571        if (WARN_ON(!p)) {
3572                ret = -EINVAL;
3573                goto exit;
3574        }
3575
3576        p->cwmin = params->cw_min;
3577        p->cwmax = params->cw_max;
3578        p->aifs = params->aifs;
3579        p->txop = params->txop;
3580
3581        ret = ath11k_wmi_send_wmm_update_cmd_tlv(ar, arvif->vdev_id,
3582                                                 &arvif->wmm_params);
3583        if (ret) {
3584                ath11k_warn(ar->ab, "failed to set wmm params: %d\n", ret);
3585                goto exit;
3586        }
3587
3588        ret = ath11k_conf_tx_uapsd(ar, vif, ac, params->uapsd);
3589
3590        if (ret)
3591                ath11k_warn(ar->ab, "failed to set sta uapsd: %d\n", ret);
3592
3593exit:
3594        mutex_unlock(&ar->conf_mutex);
3595        return ret;
3596}
3597
3598static struct ieee80211_sta_ht_cap
3599ath11k_create_ht_cap(struct ath11k *ar, u32 ar_ht_cap, u32 rate_cap_rx_chainmask)
3600{
3601        int i;
3602        struct ieee80211_sta_ht_cap ht_cap = {0};
3603        u32 ar_vht_cap = ar->pdev->cap.vht_cap;
3604
3605        if (!(ar_ht_cap & WMI_HT_CAP_ENABLED))
3606                return ht_cap;
3607
3608        ht_cap.ht_supported = 1;
3609        ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
3610        ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
3611        ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
3612        ht_cap.cap |= IEEE80211_HT_CAP_DSSSCCK40;
3613        ht_cap.cap |= WLAN_HT_CAP_SM_PS_STATIC << IEEE80211_HT_CAP_SM_PS_SHIFT;
3614
3615        if (ar_ht_cap & WMI_HT_CAP_HT20_SGI)
3616                ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
3617
3618        if (ar_ht_cap & WMI_HT_CAP_HT40_SGI)
3619                ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
3620
3621        if (ar_ht_cap & WMI_HT_CAP_DYNAMIC_SMPS) {
3622                u32 smps;
3623
3624                smps   = WLAN_HT_CAP_SM_PS_DYNAMIC;
3625                smps <<= IEEE80211_HT_CAP_SM_PS_SHIFT;
3626
3627                ht_cap.cap |= smps;
3628        }
3629
3630        if (ar_ht_cap & WMI_HT_CAP_TX_STBC)
3631                ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
3632
3633        if (ar_ht_cap & WMI_HT_CAP_RX_STBC) {
3634                u32 stbc;
3635
3636                stbc   = ar_ht_cap;
3637                stbc  &= WMI_HT_CAP_RX_STBC;
3638                stbc >>= WMI_HT_CAP_RX_STBC_MASK_SHIFT;
3639                stbc <<= IEEE80211_HT_CAP_RX_STBC_SHIFT;
3640                stbc  &= IEEE80211_HT_CAP_RX_STBC;
3641
3642                ht_cap.cap |= stbc;
3643        }
3644
3645        if (ar_ht_cap & WMI_HT_CAP_RX_LDPC)
3646                ht_cap.cap |= IEEE80211_HT_CAP_LDPC_CODING;
3647
3648        if (ar_ht_cap & WMI_HT_CAP_L_SIG_TXOP_PROT)
3649                ht_cap.cap |= IEEE80211_HT_CAP_LSIG_TXOP_PROT;
3650
3651        if (ar_vht_cap & WMI_VHT_CAP_MAX_MPDU_LEN_MASK)
3652                ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
3653
3654        for (i = 0; i < ar->num_rx_chains; i++) {
3655                if (rate_cap_rx_chainmask & BIT(i))
3656                        ht_cap.mcs.rx_mask[i] = 0xFF;
3657        }
3658
3659        ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
3660
3661        return ht_cap;
3662}
3663
3664static int ath11k_mac_set_txbf_conf(struct ath11k_vif *arvif)
3665{
3666        u32 value = 0;
3667        struct ath11k *ar = arvif->ar;
3668        int nsts;
3669        int sound_dim;
3670        u32 vht_cap = ar->pdev->cap.vht_cap;
3671        u32 vdev_param = WMI_VDEV_PARAM_TXBF;
3672
3673        if (vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE)) {
3674                nsts = vht_cap & IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
3675                nsts >>= IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT;
3676                value |= SM(nsts, WMI_TXBF_STS_CAP_OFFSET);
3677        }
3678
3679        if (vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)) {
3680                sound_dim = vht_cap &
3681                            IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
3682                sound_dim >>= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
3683                if (sound_dim > (ar->num_tx_chains - 1))
3684                        sound_dim = ar->num_tx_chains - 1;
3685                value |= SM(sound_dim, WMI_BF_SOUND_DIM_OFFSET);
3686        }
3687
3688        if (!value)
3689                return 0;
3690
3691        if (vht_cap & IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE) {
3692                value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFER;
3693
3694                if ((vht_cap & IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE) &&
3695                    arvif->vdev_type == WMI_VDEV_TYPE_AP)
3696                        value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFER;
3697        }
3698
3699        /* TODO: SUBFEE not validated in HK, disable here until validated? */
3700
3701        if (vht_cap & IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE) {
3702                value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFEE;
3703
3704                if ((vht_cap & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE) &&
3705                    arvif->vdev_type == WMI_VDEV_TYPE_STA)
3706                        value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFEE;
3707        }
3708
3709        return ath11k_wmi_vdev_set_param_cmd(ar, arvif->vdev_id,
3710                                             vdev_param, value);
3711}
3712
3713static void ath11k_set_vht_txbf_cap(struct ath11k *ar, u32 *vht_cap)
3714{
3715        bool subfer, subfee;
3716        int sound_dim = 0;
3717
3718        subfer = !!(*vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE));
3719        subfee = !!(*vht_cap & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE));
3720
3721        if (ar->num_tx_chains < 2) {
3722                *vht_cap &= ~(IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE);
3723                subfer = false;
3724        }
3725
3726        /* If SU Beaformer is not set, then disable MU Beamformer Capability */
3727        if (!subfer)
3728                *vht_cap &= ~(IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE);
3729
3730        /* If SU Beaformee is not set, then disable MU Beamformee Capability */
3731        if (!subfee)
3732                *vht_cap &= ~(IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE);
3733
3734        sound_dim = (*vht_cap & IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK);
3735        sound_dim >>= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
3736        *vht_cap &= ~IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
3737
3738        /* TODO: Need to check invalid STS and Sound_dim values set by FW? */
3739
3740        /* Enable Sounding Dimension Field only if SU BF is enabled */
3741        if (subfer) {
3742                if (sound_dim > (ar->num_tx_chains - 1))
3743                        sound_dim = ar->num_tx_chains - 1;
3744
3745                sound_dim <<= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
3746                sound_dim &=  IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
3747                *vht_cap |= sound_dim;
3748        }
3749
3750        /* Use the STS advertised by FW unless SU Beamformee is not supported*/
3751        if (!subfee)
3752                *vht_cap &= ~(IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK);
3753}
3754
3755static struct ieee80211_sta_vht_cap
3756ath11k_create_vht_cap(struct ath11k *ar, u32 rate_cap_tx_chainmask,
3757                      u32 rate_cap_rx_chainmask)
3758{
3759        struct ieee80211_sta_vht_cap vht_cap = {0};
3760        u16 txmcs_map, rxmcs_map;
3761        int i;
3762
3763        vht_cap.vht_supported = 1;
3764        vht_cap.cap = ar->pdev->cap.vht_cap;
3765
3766        ath11k_set_vht_txbf_cap(ar, &vht_cap.cap);
3767
3768        /* TODO: Enable back VHT160 mode once association issues are fixed */
3769        /* Disabling VHT160 and VHT80+80 modes */
3770        vht_cap.cap &= ~IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK;
3771        vht_cap.cap &= ~IEEE80211_VHT_CAP_SHORT_GI_160;
3772
3773        rxmcs_map = 0;
3774        txmcs_map = 0;
3775        for (i = 0; i < 8; i++) {
3776                if (i < ar->num_tx_chains && rate_cap_tx_chainmask & BIT(i))
3777                        txmcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
3778                else
3779                        txmcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
3780
3781                if (i < ar->num_rx_chains && rate_cap_rx_chainmask & BIT(i))
3782                        rxmcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
3783                else
3784                        rxmcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
3785        }
3786
3787        if (rate_cap_tx_chainmask <= 1)
3788                vht_cap.cap &= ~IEEE80211_VHT_CAP_TXSTBC;
3789
3790        vht_cap.vht_mcs.rx_mcs_map = cpu_to_le16(rxmcs_map);
3791        vht_cap.vht_mcs.tx_mcs_map = cpu_to_le16(txmcs_map);
3792
3793        return vht_cap;
3794}
3795
3796static void ath11k_mac_setup_ht_vht_cap(struct ath11k *ar,
3797                                        struct ath11k_pdev_cap *cap,
3798                                        u32 *ht_cap_info)
3799{
3800        struct ieee80211_supported_band *band;
3801        u32 rate_cap_tx_chainmask;
3802        u32 rate_cap_rx_chainmask;
3803        u32 ht_cap;
3804
3805        rate_cap_tx_chainmask = ar->cfg_tx_chainmask >> cap->tx_chain_mask_shift;
3806        rate_cap_rx_chainmask = ar->cfg_rx_chainmask >> cap->rx_chain_mask_shift;
3807
3808        if (cap->supported_bands & WMI_HOST_WLAN_2G_CAP) {
3809                band = &ar->mac.sbands[NL80211_BAND_2GHZ];
3810                ht_cap = cap->band[NL80211_BAND_2GHZ].ht_cap_info;
3811                if (ht_cap_info)
3812                        *ht_cap_info = ht_cap;
3813                band->ht_cap = ath11k_create_ht_cap(ar, ht_cap,
3814                                                    rate_cap_rx_chainmask);
3815        }
3816
3817        if (cap->supported_bands & WMI_HOST_WLAN_5G_CAP && !ar->supports_6ghz) {
3818                band = &ar->mac.sbands[NL80211_BAND_5GHZ];
3819                ht_cap = cap->band[NL80211_BAND_5GHZ].ht_cap_info;
3820                if (ht_cap_info)
3821                        *ht_cap_info = ht_cap;
3822                band->ht_cap = ath11k_create_ht_cap(ar, ht_cap,
3823                                                    rate_cap_rx_chainmask);
3824                band->vht_cap = ath11k_create_vht_cap(ar, rate_cap_tx_chainmask,
3825                                                      rate_cap_rx_chainmask);
3826        }
3827}
3828
3829static int ath11k_check_chain_mask(struct ath11k *ar, u32 ant, bool is_tx_ant)
3830{
3831        /* TODO: Check the request chainmask against the supported
3832         * chainmask table which is advertised in extented_service_ready event
3833         */
3834
3835        return 0;
3836}
3837
3838static void ath11k_gen_ppe_thresh(struct ath11k_ppe_threshold *fw_ppet,
3839                                  u8 *he_ppet)
3840{
3841        int nss, ru;
3842        u8 bit = 7;
3843
3844        he_ppet[0] = fw_ppet->numss_m1 & IEEE80211_PPE_THRES_NSS_MASK;
3845        he_ppet[0] |= (fw_ppet->ru_bit_mask <<
3846                       IEEE80211_PPE_THRES_RU_INDEX_BITMASK_POS) &
3847                      IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK;
3848        for (nss = 0; nss <= fw_ppet->numss_m1; nss++) {
3849                for (ru = 0; ru < 4; ru++) {
3850                        u8 val;
3851                        int i;
3852
3853                        if ((fw_ppet->ru_bit_mask & BIT(ru)) == 0)
3854                                continue;
3855                        val = (fw_ppet->ppet16_ppet8_ru3_ru0[nss] >> (ru * 6)) &
3856                               0x3f;
3857                        val = ((val >> 3) & 0x7) | ((val & 0x7) << 3);
3858                        for (i = 5; i >= 0; i--) {
3859                                he_ppet[bit / 8] |=
3860                                        ((val >> i) & 0x1) << ((bit % 8));
3861                                bit++;
3862                        }
3863                }
3864        }
3865}
3866
3867static void
3868ath11k_mac_filter_he_cap_mesh(struct ieee80211_he_cap_elem *he_cap_elem)
3869{
3870        u8 m;
3871
3872        m = IEEE80211_HE_MAC_CAP0_TWT_RES |
3873            IEEE80211_HE_MAC_CAP0_TWT_REQ;
3874        he_cap_elem->mac_cap_info[0] &= ~m;
3875
3876        m = IEEE80211_HE_MAC_CAP2_TRS |
3877            IEEE80211_HE_MAC_CAP2_BCAST_TWT |
3878            IEEE80211_HE_MAC_CAP2_MU_CASCADING;
3879        he_cap_elem->mac_cap_info[2] &= ~m;
3880
3881        m = IEEE80211_HE_MAC_CAP3_FLEX_TWT_SCHED |
3882            IEEE80211_HE_MAC_CAP2_BCAST_TWT |
3883            IEEE80211_HE_MAC_CAP2_MU_CASCADING;
3884        he_cap_elem->mac_cap_info[3] &= ~m;
3885
3886        m = IEEE80211_HE_MAC_CAP4_BSRP_BQRP_A_MPDU_AGG |
3887            IEEE80211_HE_MAC_CAP4_BQR;
3888        he_cap_elem->mac_cap_info[4] &= ~m;
3889
3890        m = IEEE80211_HE_MAC_CAP5_SUBCHAN_SELECTIVE_TRANSMISSION |
3891            IEEE80211_HE_MAC_CAP5_UL_2x996_TONE_RU |
3892            IEEE80211_HE_MAC_CAP5_PUNCTURED_SOUNDING |
3893            IEEE80211_HE_MAC_CAP5_HT_VHT_TRIG_FRAME_RX;
3894        he_cap_elem->mac_cap_info[5] &= ~m;
3895
3896        m = IEEE80211_HE_PHY_CAP2_UL_MU_FULL_MU_MIMO |
3897            IEEE80211_HE_PHY_CAP2_UL_MU_PARTIAL_MU_MIMO;
3898        he_cap_elem->phy_cap_info[2] &= ~m;
3899
3900        m = IEEE80211_HE_PHY_CAP3_RX_PARTIAL_BW_SU_IN_20MHZ_MU |
3901            IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_MASK |
3902            IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_RX_MASK;
3903        he_cap_elem->phy_cap_info[3] &= ~m;
3904
3905        m = IEEE80211_HE_PHY_CAP4_MU_BEAMFORMER;
3906        he_cap_elem->phy_cap_info[4] &= ~m;
3907
3908        m = IEEE80211_HE_PHY_CAP5_NG16_MU_FEEDBACK;
3909        he_cap_elem->phy_cap_info[5] &= ~m;
3910
3911        m = IEEE80211_HE_PHY_CAP6_CODEBOOK_SIZE_75_MU |
3912            IEEE80211_HE_PHY_CAP6_TRIG_MU_BEAMFORMING_PARTIAL_BW_FB |
3913            IEEE80211_HE_PHY_CAP6_TRIG_CQI_FB |
3914            IEEE80211_HE_PHY_CAP6_PARTIAL_BANDWIDTH_DL_MUMIMO;
3915        he_cap_elem->phy_cap_info[6] &= ~m;
3916
3917        m = IEEE80211_HE_PHY_CAP7_PSR_BASED_SR |
3918            IEEE80211_HE_PHY_CAP7_POWER_BOOST_FACTOR_SUPP |
3919            IEEE80211_HE_PHY_CAP7_STBC_TX_ABOVE_80MHZ |
3920            IEEE80211_HE_PHY_CAP7_STBC_RX_ABOVE_80MHZ;
3921        he_cap_elem->phy_cap_info[7] &= ~m;
3922
3923        m = IEEE80211_HE_PHY_CAP8_HE_ER_SU_PPDU_4XLTF_AND_08_US_GI |
3924            IEEE80211_HE_PHY_CAP8_20MHZ_IN_40MHZ_HE_PPDU_IN_2G |
3925            IEEE80211_HE_PHY_CAP8_20MHZ_IN_160MHZ_HE_PPDU |
3926            IEEE80211_HE_PHY_CAP8_80MHZ_IN_160MHZ_HE_PPDU;
3927        he_cap_elem->phy_cap_info[8] &= ~m;
3928
3929        m = IEEE80211_HE_PHY_CAP9_LONGER_THAN_16_SIGB_OFDM_SYM |
3930            IEEE80211_HE_PHY_CAP9_NON_TRIGGERED_CQI_FEEDBACK |
3931            IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU |
3932            IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU |
3933            IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_COMP_SIGB |
3934            IEEE80211_HE_PHY_CAP9_RX_FULL_BW_SU_USING_MU_WITH_NON_COMP_SIGB;
3935        he_cap_elem->phy_cap_info[9] &= ~m;
3936}
3937
3938static __le16 ath11k_mac_setup_he_6ghz_cap(struct ath11k_pdev_cap *pcap,
3939                                           struct ath11k_band_cap *bcap)
3940{
3941        u8 val;
3942
3943        bcap->he_6ghz_capa = IEEE80211_HT_MPDU_DENSITY_NONE;
3944        if (bcap->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS)
3945                bcap->he_6ghz_capa |=
3946                        FIELD_PREP(IEEE80211_HE_6GHZ_CAP_SM_PS,
3947                                   WLAN_HT_CAP_SM_PS_DYNAMIC);
3948        else
3949                bcap->he_6ghz_capa |=
3950                        FIELD_PREP(IEEE80211_HE_6GHZ_CAP_SM_PS,
3951                                   WLAN_HT_CAP_SM_PS_DISABLED);
3952        val = FIELD_GET(IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK,
3953                        pcap->vht_cap);
3954        bcap->he_6ghz_capa |=
3955                FIELD_PREP(IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP, val);
3956        val = FIELD_GET(IEEE80211_VHT_CAP_MAX_MPDU_MASK, pcap->vht_cap);
3957        bcap->he_6ghz_capa |=
3958                FIELD_PREP(IEEE80211_HE_6GHZ_CAP_MAX_MPDU_LEN, val);
3959        if (pcap->vht_cap & IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN)
3960                bcap->he_6ghz_capa |= IEEE80211_HE_6GHZ_CAP_RX_ANTPAT_CONS;
3961        if (pcap->vht_cap & IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN)
3962                bcap->he_6ghz_capa |= IEEE80211_HE_6GHZ_CAP_TX_ANTPAT_CONS;
3963
3964        return cpu_to_le16(bcap->he_6ghz_capa);
3965}
3966
3967static int ath11k_mac_copy_he_cap(struct ath11k *ar,
3968                                  struct ath11k_pdev_cap *cap,
3969                                  struct ieee80211_sband_iftype_data *data,
3970                                  int band)
3971{
3972        int i, idx = 0;
3973
3974        for (i = 0; i < NUM_NL80211_IFTYPES; i++) {
3975                struct ieee80211_sta_he_cap *he_cap = &data[idx].he_cap;
3976                struct ath11k_band_cap *band_cap = &cap->band[band];
3977                struct ieee80211_he_cap_elem *he_cap_elem =
3978                                &he_cap->he_cap_elem;
3979
3980                switch (i) {
3981                case NL80211_IFTYPE_STATION:
3982                case NL80211_IFTYPE_AP:
3983                case NL80211_IFTYPE_MESH_POINT:
3984                        break;
3985
3986                default:
3987                        continue;
3988                }
3989
3990                data[idx].types_mask = BIT(i);
3991                he_cap->has_he = true;
3992                memcpy(he_cap_elem->mac_cap_info, band_cap->he_cap_info,
3993                       sizeof(he_cap_elem->mac_cap_info));
3994                memcpy(he_cap_elem->phy_cap_info, band_cap->he_cap_phy_info,
3995                       sizeof(he_cap_elem->phy_cap_info));
3996
3997                he_cap_elem->mac_cap_info[1] &=
3998                        IEEE80211_HE_MAC_CAP1_TF_MAC_PAD_DUR_MASK;
3999
4000                he_cap_elem->phy_cap_info[5] &=
4001                        ~IEEE80211_HE_PHY_CAP5_BEAMFORMEE_NUM_SND_DIM_UNDER_80MHZ_MASK;
4002                he_cap_elem->phy_cap_info[5] |= ar->num_tx_chains - 1;
4003
4004                switch (i) {
4005                case NL80211_IFTYPE_AP:
4006                        he_cap_elem->phy_cap_info[3] &=
4007                                ~IEEE80211_HE_PHY_CAP3_DCM_MAX_CONST_TX_MASK;
4008                        he_cap_elem->phy_cap_info[9] |=
4009                                IEEE80211_HE_PHY_CAP9_RX_1024_QAM_LESS_THAN_242_TONE_RU;
4010                        break;
4011                case NL80211_IFTYPE_STATION:
4012                        he_cap_elem->mac_cap_info[0] &=
4013                                ~IEEE80211_HE_MAC_CAP0_TWT_RES;
4014                        he_cap_elem->mac_cap_info[0] |=
4015                                IEEE80211_HE_MAC_CAP0_TWT_REQ;
4016                        he_cap_elem->phy_cap_info[9] |=
4017                                IEEE80211_HE_PHY_CAP9_TX_1024_QAM_LESS_THAN_242_TONE_RU;
4018                        break;
4019                case NL80211_IFTYPE_MESH_POINT:
4020                        ath11k_mac_filter_he_cap_mesh(he_cap_elem);
4021                        break;
4022                }
4023
4024                he_cap->he_mcs_nss_supp.rx_mcs_80 =
4025                        cpu_to_le16(band_cap->he_mcs & 0xffff);
4026                he_cap->he_mcs_nss_supp.tx_mcs_80 =
4027                        cpu_to_le16(band_cap->he_mcs & 0xffff);
4028                he_cap->he_mcs_nss_supp.rx_mcs_160 =
4029                        cpu_to_le16((band_cap->he_mcs >> 16) & 0xffff);
4030                he_cap->he_mcs_nss_supp.tx_mcs_160 =
4031                        cpu_to_le16((band_cap->he_mcs >> 16) & 0xffff);
4032                he_cap->he_mcs_nss_supp.rx_mcs_80p80 =
4033                        cpu_to_le16((band_cap->he_mcs >> 16) & 0xffff);
4034                he_cap->he_mcs_nss_supp.tx_mcs_80p80 =
4035                        cpu_to_le16((band_cap->he_mcs >> 16) & 0xffff);
4036
4037                memset(he_cap->ppe_thres, 0, sizeof(he_cap->ppe_thres));
4038                if (he_cap_elem->phy_cap_info[6] &
4039                    IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT)
4040                        ath11k_gen_ppe_thresh(&band_cap->he_ppet,
4041                                              he_cap->ppe_thres);
4042
4043                if (band == NL80211_BAND_6GHZ) {
4044                        data[idx].he_6ghz_capa.capa =
4045                                ath11k_mac_setup_he_6ghz_cap(cap, band_cap);
4046                }
4047                idx++;
4048        }
4049
4050        return idx;
4051}
4052
4053static void ath11k_mac_setup_he_cap(struct ath11k *ar,
4054                                    struct ath11k_pdev_cap *cap)
4055{
4056        struct