linux/drivers/net/wireless/intel/iwlwifi/mvm/sta.c
<<
>>
Prefs
   1/******************************************************************************
   2 *
   3 * This file is provided under a dual BSD/GPLv2 license.  When using or
   4 * redistributing this file, you may do so under either license.
   5 *
   6 * GPL LICENSE SUMMARY
   7 *
   8 * Copyright(c) 2012 - 2015 Intel Corporation. All rights reserved.
   9 * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
  10 * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
  11 * Copyright(c) 2018 - 2019 Intel Corporation
  12 *
  13 * This program is free software; you can redistribute it and/or modify
  14 * it under the terms of version 2 of the GNU General Public License as
  15 * published by the Free Software Foundation.
  16 *
  17 * This program is distributed in the hope that it will be useful, but
  18 * WITHOUT ANY WARRANTY; without even the implied warranty of
  19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  20 * General Public License for more details.
  21 *
  22 * The full GNU General Public License is included in this distribution
  23 * in the file called COPYING.
  24 *
  25 * Contact Information:
  26 *  Intel Linux Wireless <linuxwifi@intel.com>
  27 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  28 *
  29 * BSD LICENSE
  30 *
  31 * Copyright(c) 2012 - 2015 Intel Corporation. All rights reserved.
  32 * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
  33 * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
  34 * Copyright(c) 2018 - 2019 Intel Corporation
  35 * All rights reserved.
  36 *
  37 * Redistribution and use in source and binary forms, with or without
  38 * modification, are permitted provided that the following conditions
  39 * are met:
  40 *
  41 *  * Redistributions of source code must retain the above copyright
  42 *    notice, this list of conditions and the following disclaimer.
  43 *  * Redistributions in binary form must reproduce the above copyright
  44 *    notice, this list of conditions and the following disclaimer in
  45 *    the documentation and/or other materials provided with the
  46 *    distribution.
  47 *  * Neither the name Intel Corporation nor the names of its
  48 *    contributors may be used to endorse or promote products derived
  49 *    from this software without specific prior written permission.
  50 *
  51 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  52 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  53 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  54 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  55 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  56 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  57 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  58 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  59 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  60 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  61 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  62 *
  63 *****************************************************************************/
  64#include <net/mac80211.h>
  65
  66#include "mvm.h"
  67#include "sta.h"
  68#include "rs.h"
  69
  70/*
  71 * New version of ADD_STA_sta command added new fields at the end of the
  72 * structure, so sending the size of the relevant API's structure is enough to
  73 * support both API versions.
  74 */
  75static inline int iwl_mvm_add_sta_cmd_size(struct iwl_mvm *mvm)
  76{
  77        if (iwl_mvm_has_new_rx_api(mvm) ||
  78            fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
  79                return sizeof(struct iwl_mvm_add_sta_cmd);
  80        else
  81                return sizeof(struct iwl_mvm_add_sta_cmd_v7);
  82}
  83
  84static int iwl_mvm_find_free_sta_id(struct iwl_mvm *mvm,
  85                                    enum nl80211_iftype iftype)
  86{
  87        int sta_id;
  88        u32 reserved_ids = 0;
  89
  90        BUILD_BUG_ON(IWL_MVM_STATION_COUNT > 32);
  91        WARN_ON_ONCE(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status));
  92
  93        lockdep_assert_held(&mvm->mutex);
  94
  95        /* d0i3/d3 assumes the AP's sta_id (of sta vif) is 0. reserve it. */
  96        if (iftype != NL80211_IFTYPE_STATION)
  97                reserved_ids = BIT(0);
  98
  99        /* Don't take rcu_read_lock() since we are protected by mvm->mutex */
 100        for (sta_id = 0; sta_id < ARRAY_SIZE(mvm->fw_id_to_mac_id); sta_id++) {
 101                if (BIT(sta_id) & reserved_ids)
 102                        continue;
 103
 104                if (!rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
 105                                               lockdep_is_held(&mvm->mutex)))
 106                        return sta_id;
 107        }
 108        return IWL_MVM_INVALID_STA;
 109}
 110
 111/* send station add/update command to firmware */
 112int iwl_mvm_sta_send_to_fw(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
 113                           bool update, unsigned int flags)
 114{
 115        struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
 116        struct iwl_mvm_add_sta_cmd add_sta_cmd = {
 117                .sta_id = mvm_sta->sta_id,
 118                .mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color),
 119                .add_modify = update ? 1 : 0,
 120                .station_flags_msk = cpu_to_le32(STA_FLG_FAT_EN_MSK |
 121                                                 STA_FLG_MIMO_EN_MSK |
 122                                                 STA_FLG_RTS_MIMO_PROT),
 123                .tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg),
 124        };
 125        int ret;
 126        u32 status;
 127        u32 agg_size = 0, mpdu_dens = 0;
 128
 129        if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
 130                add_sta_cmd.station_type = mvm_sta->sta_type;
 131
 132        if (!update || (flags & STA_MODIFY_QUEUES)) {
 133                memcpy(&add_sta_cmd.addr, sta->addr, ETH_ALEN);
 134
 135                if (!iwl_mvm_has_new_tx_api(mvm)) {
 136                        add_sta_cmd.tfd_queue_msk =
 137                                cpu_to_le32(mvm_sta->tfd_queue_msk);
 138
 139                        if (flags & STA_MODIFY_QUEUES)
 140                                add_sta_cmd.modify_mask |= STA_MODIFY_QUEUES;
 141                } else {
 142                        WARN_ON(flags & STA_MODIFY_QUEUES);
 143                }
 144        }
 145
 146        switch (sta->bandwidth) {
 147        case IEEE80211_STA_RX_BW_160:
 148                add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_160MHZ);
 149                /* fall through */
 150        case IEEE80211_STA_RX_BW_80:
 151                add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_80MHZ);
 152                /* fall through */
 153        case IEEE80211_STA_RX_BW_40:
 154                add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_40MHZ);
 155                /* fall through */
 156        case IEEE80211_STA_RX_BW_20:
 157                if (sta->ht_cap.ht_supported)
 158                        add_sta_cmd.station_flags |=
 159                                cpu_to_le32(STA_FLG_FAT_EN_20MHZ);
 160                break;
 161        }
 162
 163        switch (sta->rx_nss) {
 164        case 1:
 165                add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
 166                break;
 167        case 2:
 168                add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO2);
 169                break;
 170        case 3 ... 8:
 171                add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO3);
 172                break;
 173        }
 174
 175        switch (sta->smps_mode) {
 176        case IEEE80211_SMPS_AUTOMATIC:
 177        case IEEE80211_SMPS_NUM_MODES:
 178                WARN_ON(1);
 179                break;
 180        case IEEE80211_SMPS_STATIC:
 181                /* override NSS */
 182                add_sta_cmd.station_flags &= ~cpu_to_le32(STA_FLG_MIMO_EN_MSK);
 183                add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
 184                break;
 185        case IEEE80211_SMPS_DYNAMIC:
 186                add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_RTS_MIMO_PROT);
 187                break;
 188        case IEEE80211_SMPS_OFF:
 189                /* nothing */
 190                break;
 191        }
 192
 193        if (sta->ht_cap.ht_supported) {
 194                add_sta_cmd.station_flags_msk |=
 195                        cpu_to_le32(STA_FLG_MAX_AGG_SIZE_MSK |
 196                                    STA_FLG_AGG_MPDU_DENS_MSK);
 197
 198                mpdu_dens = sta->ht_cap.ampdu_density;
 199        }
 200
 201        if (sta->vht_cap.vht_supported) {
 202                agg_size = sta->vht_cap.cap &
 203                        IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
 204                agg_size >>=
 205                        IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
 206        } else if (sta->ht_cap.ht_supported) {
 207                agg_size = sta->ht_cap.ampdu_factor;
 208        }
 209
 210        add_sta_cmd.station_flags |=
 211                cpu_to_le32(agg_size << STA_FLG_MAX_AGG_SIZE_SHIFT);
 212        add_sta_cmd.station_flags |=
 213                cpu_to_le32(mpdu_dens << STA_FLG_AGG_MPDU_DENS_SHIFT);
 214        if (mvm_sta->sta_state >= IEEE80211_STA_ASSOC)
 215                add_sta_cmd.assoc_id = cpu_to_le16(sta->aid);
 216
 217        if (sta->wme) {
 218                add_sta_cmd.modify_mask |= STA_MODIFY_UAPSD_ACS;
 219
 220                if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
 221                        add_sta_cmd.uapsd_acs |= BIT(AC_BK);
 222                if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
 223                        add_sta_cmd.uapsd_acs |= BIT(AC_BE);
 224                if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
 225                        add_sta_cmd.uapsd_acs |= BIT(AC_VI);
 226                if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
 227                        add_sta_cmd.uapsd_acs |= BIT(AC_VO);
 228                add_sta_cmd.uapsd_acs |= add_sta_cmd.uapsd_acs << 4;
 229                add_sta_cmd.sp_length = sta->max_sp ? sta->max_sp * 2 : 128;
 230        }
 231
 232        status = ADD_STA_SUCCESS;
 233        ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
 234                                          iwl_mvm_add_sta_cmd_size(mvm),
 235                                          &add_sta_cmd, &status);
 236        if (ret)
 237                return ret;
 238
 239        switch (status & IWL_ADD_STA_STATUS_MASK) {
 240        case ADD_STA_SUCCESS:
 241                IWL_DEBUG_ASSOC(mvm, "ADD_STA PASSED\n");
 242                break;
 243        default:
 244                ret = -EIO;
 245                IWL_ERR(mvm, "ADD_STA failed\n");
 246                break;
 247        }
 248
 249        return ret;
 250}
 251
 252static void iwl_mvm_rx_agg_session_expired(struct timer_list *t)
 253{
 254        struct iwl_mvm_baid_data *data =
 255                from_timer(data, t, session_timer);
 256        struct iwl_mvm_baid_data __rcu **rcu_ptr = data->rcu_ptr;
 257        struct iwl_mvm_baid_data *ba_data;
 258        struct ieee80211_sta *sta;
 259        struct iwl_mvm_sta *mvm_sta;
 260        unsigned long timeout;
 261
 262        rcu_read_lock();
 263
 264        ba_data = rcu_dereference(*rcu_ptr);
 265
 266        if (WARN_ON(!ba_data))
 267                goto unlock;
 268
 269        if (!ba_data->timeout)
 270                goto unlock;
 271
 272        timeout = ba_data->last_rx + TU_TO_JIFFIES(ba_data->timeout * 2);
 273        if (time_is_after_jiffies(timeout)) {
 274                mod_timer(&ba_data->session_timer, timeout);
 275                goto unlock;
 276        }
 277
 278        /* Timer expired */
 279        sta = rcu_dereference(ba_data->mvm->fw_id_to_mac_id[ba_data->sta_id]);
 280
 281        /*
 282         * sta should be valid unless the following happens:
 283         * The firmware asserts which triggers a reconfig flow, but
 284         * the reconfig fails before we set the pointer to sta into
 285         * the fw_id_to_mac_id pointer table. Mac80211 can't stop
 286         * A-MDPU and hence the timer continues to run. Then, the
 287         * timer expires and sta is NULL.
 288         */
 289        if (!sta)
 290                goto unlock;
 291
 292        mvm_sta = iwl_mvm_sta_from_mac80211(sta);
 293        ieee80211_rx_ba_timer_expired(mvm_sta->vif,
 294                                      sta->addr, ba_data->tid);
 295unlock:
 296        rcu_read_unlock();
 297}
 298
 299/* Disable aggregations for a bitmap of TIDs for a given station */
 300static int iwl_mvm_invalidate_sta_queue(struct iwl_mvm *mvm, int queue,
 301                                        unsigned long disable_agg_tids,
 302                                        bool remove_queue)
 303{
 304        struct iwl_mvm_add_sta_cmd cmd = {};
 305        struct ieee80211_sta *sta;
 306        struct iwl_mvm_sta *mvmsta;
 307        u32 status;
 308        u8 sta_id;
 309
 310        if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
 311                return -EINVAL;
 312
 313        sta_id = mvm->queue_info[queue].ra_sta_id;
 314
 315        rcu_read_lock();
 316
 317        sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
 318
 319        if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) {
 320                rcu_read_unlock();
 321                return -EINVAL;
 322        }
 323
 324        mvmsta = iwl_mvm_sta_from_mac80211(sta);
 325
 326        mvmsta->tid_disable_agg |= disable_agg_tids;
 327
 328        cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
 329        cmd.sta_id = mvmsta->sta_id;
 330        cmd.add_modify = STA_MODE_MODIFY;
 331        cmd.modify_mask = STA_MODIFY_QUEUES;
 332        if (disable_agg_tids)
 333                cmd.modify_mask |= STA_MODIFY_TID_DISABLE_TX;
 334        if (remove_queue)
 335                cmd.modify_mask |= STA_MODIFY_QUEUE_REMOVAL;
 336        cmd.tfd_queue_msk = cpu_to_le32(mvmsta->tfd_queue_msk);
 337        cmd.tid_disable_tx = cpu_to_le16(mvmsta->tid_disable_agg);
 338
 339        rcu_read_unlock();
 340
 341        /* Notify FW of queue removal from the STA queues */
 342        status = ADD_STA_SUCCESS;
 343        return iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
 344                                           iwl_mvm_add_sta_cmd_size(mvm),
 345                                           &cmd, &status);
 346}
 347
 348static int iwl_mvm_disable_txq(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
 349                               int queue, u8 tid, u8 flags)
 350{
 351        struct iwl_scd_txq_cfg_cmd cmd = {
 352                .scd_queue = queue,
 353                .action = SCD_CFG_DISABLE_QUEUE,
 354        };
 355        int ret;
 356
 357        if (iwl_mvm_has_new_tx_api(mvm)) {
 358                iwl_trans_txq_free(mvm->trans, queue);
 359
 360                return 0;
 361        }
 362
 363        if (WARN_ON(mvm->queue_info[queue].tid_bitmap == 0))
 364                return 0;
 365
 366        mvm->queue_info[queue].tid_bitmap &= ~BIT(tid);
 367
 368        cmd.action = mvm->queue_info[queue].tid_bitmap ?
 369                SCD_CFG_ENABLE_QUEUE : SCD_CFG_DISABLE_QUEUE;
 370        if (cmd.action == SCD_CFG_DISABLE_QUEUE)
 371                mvm->queue_info[queue].status = IWL_MVM_QUEUE_FREE;
 372
 373        IWL_DEBUG_TX_QUEUES(mvm,
 374                            "Disabling TXQ #%d tids=0x%x\n",
 375                            queue,
 376                            mvm->queue_info[queue].tid_bitmap);
 377
 378        /* If the queue is still enabled - nothing left to do in this func */
 379        if (cmd.action == SCD_CFG_ENABLE_QUEUE)
 380                return 0;
 381
 382        cmd.sta_id = mvm->queue_info[queue].ra_sta_id;
 383        cmd.tid = mvm->queue_info[queue].txq_tid;
 384
 385        /* Make sure queue info is correct even though we overwrite it */
 386        WARN(mvm->queue_info[queue].tid_bitmap,
 387             "TXQ #%d info out-of-sync - tids=0x%x\n",
 388             queue, mvm->queue_info[queue].tid_bitmap);
 389
 390        /* If we are here - the queue is freed and we can zero out these vals */
 391        mvm->queue_info[queue].tid_bitmap = 0;
 392
 393        if (sta) {
 394                struct iwl_mvm_txq *mvmtxq =
 395                        iwl_mvm_txq_from_tid(sta, tid);
 396
 397                mvmtxq->txq_id = IWL_MVM_INVALID_QUEUE;
 398        }
 399
 400        /* Regardless if this is a reserved TXQ for a STA - mark it as false */
 401        mvm->queue_info[queue].reserved = false;
 402
 403        iwl_trans_txq_disable(mvm->trans, queue, false);
 404        ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, flags,
 405                                   sizeof(struct iwl_scd_txq_cfg_cmd), &cmd);
 406
 407        if (ret)
 408                IWL_ERR(mvm, "Failed to disable queue %d (ret=%d)\n",
 409                        queue, ret);
 410        return ret;
 411}
 412
 413static int iwl_mvm_get_queue_agg_tids(struct iwl_mvm *mvm, int queue)
 414{
 415        struct ieee80211_sta *sta;
 416        struct iwl_mvm_sta *mvmsta;
 417        unsigned long tid_bitmap;
 418        unsigned long agg_tids = 0;
 419        u8 sta_id;
 420        int tid;
 421
 422        lockdep_assert_held(&mvm->mutex);
 423
 424        if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
 425                return -EINVAL;
 426
 427        sta_id = mvm->queue_info[queue].ra_sta_id;
 428        tid_bitmap = mvm->queue_info[queue].tid_bitmap;
 429
 430        sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
 431                                        lockdep_is_held(&mvm->mutex));
 432
 433        if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta)))
 434                return -EINVAL;
 435
 436        mvmsta = iwl_mvm_sta_from_mac80211(sta);
 437
 438        spin_lock_bh(&mvmsta->lock);
 439        for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
 440                if (mvmsta->tid_data[tid].state == IWL_AGG_ON)
 441                        agg_tids |= BIT(tid);
 442        }
 443        spin_unlock_bh(&mvmsta->lock);
 444
 445        return agg_tids;
 446}
 447
 448/*
 449 * Remove a queue from a station's resources.
 450 * Note that this only marks as free. It DOESN'T delete a BA agreement, and
 451 * doesn't disable the queue
 452 */
 453static int iwl_mvm_remove_sta_queue_marking(struct iwl_mvm *mvm, int queue)
 454{
 455        struct ieee80211_sta *sta;
 456        struct iwl_mvm_sta *mvmsta;
 457        unsigned long tid_bitmap;
 458        unsigned long disable_agg_tids = 0;
 459        u8 sta_id;
 460        int tid;
 461
 462        lockdep_assert_held(&mvm->mutex);
 463
 464        if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
 465                return -EINVAL;
 466
 467        sta_id = mvm->queue_info[queue].ra_sta_id;
 468        tid_bitmap = mvm->queue_info[queue].tid_bitmap;
 469
 470        rcu_read_lock();
 471
 472        sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
 473
 474        if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) {
 475                rcu_read_unlock();
 476                return 0;
 477        }
 478
 479        mvmsta = iwl_mvm_sta_from_mac80211(sta);
 480
 481        spin_lock_bh(&mvmsta->lock);
 482        /* Unmap MAC queues and TIDs from this queue */
 483        for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
 484                struct iwl_mvm_txq *mvmtxq =
 485                        iwl_mvm_txq_from_tid(sta, tid);
 486
 487                if (mvmsta->tid_data[tid].state == IWL_AGG_ON)
 488                        disable_agg_tids |= BIT(tid);
 489                mvmsta->tid_data[tid].txq_id = IWL_MVM_INVALID_QUEUE;
 490
 491                mvmtxq->txq_id = IWL_MVM_INVALID_QUEUE;
 492        }
 493
 494        mvmsta->tfd_queue_msk &= ~BIT(queue); /* Don't use this queue anymore */
 495        spin_unlock_bh(&mvmsta->lock);
 496
 497        rcu_read_unlock();
 498
 499        /*
 500         * The TX path may have been using this TXQ_ID from the tid_data,
 501         * so make sure it's no longer running so that we can safely reuse
 502         * this TXQ later. We've set all the TIDs to IWL_MVM_INVALID_QUEUE
 503         * above, but nothing guarantees we've stopped using them. Thus,
 504         * without this, we could get to iwl_mvm_disable_txq() and remove
 505         * the queue while still sending frames to it.
 506         */
 507        synchronize_net();
 508
 509        return disable_agg_tids;
 510}
 511
 512static int iwl_mvm_free_inactive_queue(struct iwl_mvm *mvm, int queue,
 513                                       struct ieee80211_sta *old_sta,
 514                                       u8 new_sta_id)
 515{
 516        struct iwl_mvm_sta *mvmsta;
 517        u8 sta_id, tid;
 518        unsigned long disable_agg_tids = 0;
 519        bool same_sta;
 520        int ret;
 521
 522        lockdep_assert_held(&mvm->mutex);
 523
 524        if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
 525                return -EINVAL;
 526
 527        sta_id = mvm->queue_info[queue].ra_sta_id;
 528        tid = mvm->queue_info[queue].txq_tid;
 529
 530        same_sta = sta_id == new_sta_id;
 531
 532        mvmsta = iwl_mvm_sta_from_staid_protected(mvm, sta_id);
 533        if (WARN_ON(!mvmsta))
 534                return -EINVAL;
 535
 536        disable_agg_tids = iwl_mvm_remove_sta_queue_marking(mvm, queue);
 537        /* Disable the queue */
 538        if (disable_agg_tids)
 539                iwl_mvm_invalidate_sta_queue(mvm, queue,
 540                                             disable_agg_tids, false);
 541
 542        ret = iwl_mvm_disable_txq(mvm, old_sta, queue, tid, 0);
 543        if (ret) {
 544                IWL_ERR(mvm,
 545                        "Failed to free inactive queue %d (ret=%d)\n",
 546                        queue, ret);
 547
 548                return ret;
 549        }
 550
 551        /* If TXQ is allocated to another STA, update removal in FW */
 552        if (!same_sta)
 553                iwl_mvm_invalidate_sta_queue(mvm, queue, 0, true);
 554
 555        return 0;
 556}
 557
 558static int iwl_mvm_get_shared_queue(struct iwl_mvm *mvm,
 559                                    unsigned long tfd_queue_mask, u8 ac)
 560{
 561        int queue = 0;
 562        u8 ac_to_queue[IEEE80211_NUM_ACS];
 563        int i;
 564
 565        /*
 566         * This protects us against grabbing a queue that's being reconfigured
 567         * by the inactivity checker.
 568         */
 569        lockdep_assert_held(&mvm->mutex);
 570
 571        if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
 572                return -EINVAL;
 573
 574        memset(&ac_to_queue, IEEE80211_INVAL_HW_QUEUE, sizeof(ac_to_queue));
 575
 576        /* See what ACs the existing queues for this STA have */
 577        for_each_set_bit(i, &tfd_queue_mask, IWL_MVM_DQA_MAX_DATA_QUEUE) {
 578                /* Only DATA queues can be shared */
 579                if (i < IWL_MVM_DQA_MIN_DATA_QUEUE &&
 580                    i != IWL_MVM_DQA_BSS_CLIENT_QUEUE)
 581                        continue;
 582
 583                ac_to_queue[mvm->queue_info[i].mac80211_ac] = i;
 584        }
 585
 586        /*
 587         * The queue to share is chosen only from DATA queues as follows (in
 588         * descending priority):
 589         * 1. An AC_BE queue
 590         * 2. Same AC queue
 591         * 3. Highest AC queue that is lower than new AC
 592         * 4. Any existing AC (there always is at least 1 DATA queue)
 593         */
 594
 595        /* Priority 1: An AC_BE queue */
 596        if (ac_to_queue[IEEE80211_AC_BE] != IEEE80211_INVAL_HW_QUEUE)
 597                queue = ac_to_queue[IEEE80211_AC_BE];
 598        /* Priority 2: Same AC queue */
 599        else if (ac_to_queue[ac] != IEEE80211_INVAL_HW_QUEUE)
 600                queue = ac_to_queue[ac];
 601        /* Priority 3a: If new AC is VO and VI exists - use VI */
 602        else if (ac == IEEE80211_AC_VO &&
 603                 ac_to_queue[IEEE80211_AC_VI] != IEEE80211_INVAL_HW_QUEUE)
 604                queue = ac_to_queue[IEEE80211_AC_VI];
 605        /* Priority 3b: No BE so only AC less than the new one is BK */
 606        else if (ac_to_queue[IEEE80211_AC_BK] != IEEE80211_INVAL_HW_QUEUE)
 607                queue = ac_to_queue[IEEE80211_AC_BK];
 608        /* Priority 4a: No BE nor BK - use VI if exists */
 609        else if (ac_to_queue[IEEE80211_AC_VI] != IEEE80211_INVAL_HW_QUEUE)
 610                queue = ac_to_queue[IEEE80211_AC_VI];
 611        /* Priority 4b: No BE, BK nor VI - use VO if exists */
 612        else if (ac_to_queue[IEEE80211_AC_VO] != IEEE80211_INVAL_HW_QUEUE)
 613                queue = ac_to_queue[IEEE80211_AC_VO];
 614
 615        /* Make sure queue found (or not) is legal */
 616        if (!iwl_mvm_is_dqa_data_queue(mvm, queue) &&
 617            !iwl_mvm_is_dqa_mgmt_queue(mvm, queue) &&
 618            (queue != IWL_MVM_DQA_BSS_CLIENT_QUEUE)) {
 619                IWL_ERR(mvm, "No DATA queues available to share\n");
 620                return -ENOSPC;
 621        }
 622
 623        return queue;
 624}
 625
 626/*
 627 * If a given queue has a higher AC than the TID stream that is being compared
 628 * to, the queue needs to be redirected to the lower AC. This function does that
 629 * in such a case, otherwise - if no redirection required - it does nothing,
 630 * unless the %force param is true.
 631 */
 632static int iwl_mvm_redirect_queue(struct iwl_mvm *mvm, int queue, int tid,
 633                                  int ac, int ssn, unsigned int wdg_timeout,
 634                                  bool force, struct iwl_mvm_txq *txq)
 635{
 636        struct iwl_scd_txq_cfg_cmd cmd = {
 637                .scd_queue = queue,
 638                .action = SCD_CFG_DISABLE_QUEUE,
 639        };
 640        bool shared_queue;
 641        int ret;
 642
 643        if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
 644                return -EINVAL;
 645
 646        /*
 647         * If the AC is lower than current one - FIFO needs to be redirected to
 648         * the lowest one of the streams in the queue. Check if this is needed
 649         * here.
 650         * Notice that the enum ieee80211_ac_numbers is "flipped", so BK is with
 651         * value 3 and VO with value 0, so to check if ac X is lower than ac Y
 652         * we need to check if the numerical value of X is LARGER than of Y.
 653         */
 654        if (ac <= mvm->queue_info[queue].mac80211_ac && !force) {
 655                IWL_DEBUG_TX_QUEUES(mvm,
 656                                    "No redirection needed on TXQ #%d\n",
 657                                    queue);
 658                return 0;
 659        }
 660
 661        cmd.sta_id = mvm->queue_info[queue].ra_sta_id;
 662        cmd.tx_fifo = iwl_mvm_ac_to_tx_fifo[mvm->queue_info[queue].mac80211_ac];
 663        cmd.tid = mvm->queue_info[queue].txq_tid;
 664        shared_queue = hweight16(mvm->queue_info[queue].tid_bitmap) > 1;
 665
 666        IWL_DEBUG_TX_QUEUES(mvm, "Redirecting TXQ #%d to FIFO #%d\n",
 667                            queue, iwl_mvm_ac_to_tx_fifo[ac]);
 668
 669        /* Stop the queue and wait for it to empty */
 670        txq->stopped = true;
 671
 672        ret = iwl_trans_wait_tx_queues_empty(mvm->trans, BIT(queue));
 673        if (ret) {
 674                IWL_ERR(mvm, "Error draining queue %d before reconfig\n",
 675                        queue);
 676                ret = -EIO;
 677                goto out;
 678        }
 679
 680        /* Before redirecting the queue we need to de-activate it */
 681        iwl_trans_txq_disable(mvm->trans, queue, false);
 682        ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd);
 683        if (ret)
 684                IWL_ERR(mvm, "Failed SCD disable TXQ %d (ret=%d)\n", queue,
 685                        ret);
 686
 687        /* Make sure the SCD wrptr is correctly set before reconfiguring */
 688        iwl_trans_txq_enable_cfg(mvm->trans, queue, ssn, NULL, wdg_timeout);
 689
 690        /* Update the TID "owner" of the queue */
 691        mvm->queue_info[queue].txq_tid = tid;
 692
 693        /* TODO: Work-around SCD bug when moving back by multiples of 0x40 */
 694
 695        /* Redirect to lower AC */
 696        iwl_mvm_reconfig_scd(mvm, queue, iwl_mvm_ac_to_tx_fifo[ac],
 697                             cmd.sta_id, tid, IWL_FRAME_LIMIT, ssn);
 698
 699        /* Update AC marking of the queue */
 700        mvm->queue_info[queue].mac80211_ac = ac;
 701
 702        /*
 703         * Mark queue as shared in transport if shared
 704         * Note this has to be done after queue enablement because enablement
 705         * can also set this value, and there is no indication there to shared
 706         * queues
 707         */
 708        if (shared_queue)
 709                iwl_trans_txq_set_shared_mode(mvm->trans, queue, true);
 710
 711out:
 712        /* Continue using the queue */
 713        txq->stopped = false;
 714
 715        return ret;
 716}
 717
 718static int iwl_mvm_find_free_queue(struct iwl_mvm *mvm, u8 sta_id,
 719                                   u8 minq, u8 maxq)
 720{
 721        int i;
 722
 723        lockdep_assert_held(&mvm->mutex);
 724
 725        /* This should not be hit with new TX path */
 726        if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
 727                return -ENOSPC;
 728
 729        /* Start by looking for a free queue */
 730        for (i = minq; i <= maxq; i++)
 731                if (mvm->queue_info[i].tid_bitmap == 0 &&
 732                    mvm->queue_info[i].status == IWL_MVM_QUEUE_FREE)
 733                        return i;
 734
 735        return -ENOSPC;
 736}
 737
 738static int iwl_mvm_tvqm_enable_txq(struct iwl_mvm *mvm,
 739                                   u8 sta_id, u8 tid, unsigned int timeout)
 740{
 741        int queue, size = max_t(u32, IWL_DEFAULT_QUEUE_SIZE,
 742                                mvm->trans->cfg->min_256_ba_txq_size);
 743
 744        if (tid == IWL_MAX_TID_COUNT) {
 745                tid = IWL_MGMT_TID;
 746                size = max_t(u32, IWL_MGMT_QUEUE_SIZE,
 747                             mvm->trans->cfg->min_txq_size);
 748        }
 749        queue = iwl_trans_txq_alloc(mvm->trans,
 750                                    cpu_to_le16(TX_QUEUE_CFG_ENABLE_QUEUE),
 751                                    sta_id, tid, SCD_QUEUE_CFG, size, timeout);
 752
 753        if (queue < 0) {
 754                IWL_DEBUG_TX_QUEUES(mvm,
 755                                    "Failed allocating TXQ for sta %d tid %d, ret: %d\n",
 756                                    sta_id, tid, queue);
 757                return queue;
 758        }
 759
 760        IWL_DEBUG_TX_QUEUES(mvm, "Enabling TXQ #%d for sta %d tid %d\n",
 761                            queue, sta_id, tid);
 762
 763        IWL_DEBUG_TX_QUEUES(mvm, "Enabling TXQ #%d\n", queue);
 764
 765        return queue;
 766}
 767
 768static int iwl_mvm_sta_alloc_queue_tvqm(struct iwl_mvm *mvm,
 769                                        struct ieee80211_sta *sta, u8 ac,
 770                                        int tid)
 771{
 772        struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
 773        struct iwl_mvm_txq *mvmtxq =
 774                iwl_mvm_txq_from_tid(sta, tid);
 775        unsigned int wdg_timeout =
 776                iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
 777        int queue = -1;
 778
 779        lockdep_assert_held(&mvm->mutex);
 780
 781        IWL_DEBUG_TX_QUEUES(mvm,
 782                            "Allocating queue for sta %d on tid %d\n",
 783                            mvmsta->sta_id, tid);
 784        queue = iwl_mvm_tvqm_enable_txq(mvm, mvmsta->sta_id, tid, wdg_timeout);
 785        if (queue < 0)
 786                return queue;
 787
 788        mvmtxq->txq_id = queue;
 789        mvm->tvqm_info[queue].txq_tid = tid;
 790        mvm->tvqm_info[queue].sta_id = mvmsta->sta_id;
 791
 792        IWL_DEBUG_TX_QUEUES(mvm, "Allocated queue is %d\n", queue);
 793
 794        spin_lock_bh(&mvmsta->lock);
 795        mvmsta->tid_data[tid].txq_id = queue;
 796        spin_unlock_bh(&mvmsta->lock);
 797
 798        return 0;
 799}
 800
 801static bool iwl_mvm_update_txq_mapping(struct iwl_mvm *mvm,
 802                                       struct ieee80211_sta *sta,
 803                                       int queue, u8 sta_id, u8 tid)
 804{
 805        bool enable_queue = true;
 806
 807        /* Make sure this TID isn't already enabled */
 808        if (mvm->queue_info[queue].tid_bitmap & BIT(tid)) {
 809                IWL_ERR(mvm, "Trying to enable TXQ %d with existing TID %d\n",
 810                        queue, tid);
 811                return false;
 812        }
 813
 814        /* Update mappings and refcounts */
 815        if (mvm->queue_info[queue].tid_bitmap)
 816                enable_queue = false;
 817
 818        mvm->queue_info[queue].tid_bitmap |= BIT(tid);
 819        mvm->queue_info[queue].ra_sta_id = sta_id;
 820
 821        if (enable_queue) {
 822                if (tid != IWL_MAX_TID_COUNT)
 823                        mvm->queue_info[queue].mac80211_ac =
 824                                tid_to_mac80211_ac[tid];
 825                else
 826                        mvm->queue_info[queue].mac80211_ac = IEEE80211_AC_VO;
 827
 828                mvm->queue_info[queue].txq_tid = tid;
 829        }
 830
 831        if (sta) {
 832                struct iwl_mvm_txq *mvmtxq =
 833                        iwl_mvm_txq_from_tid(sta, tid);
 834
 835                mvmtxq->txq_id = queue;
 836        }
 837
 838        IWL_DEBUG_TX_QUEUES(mvm,
 839                            "Enabling TXQ #%d tids=0x%x\n",
 840                            queue, mvm->queue_info[queue].tid_bitmap);
 841
 842        return enable_queue;
 843}
 844
 845static bool iwl_mvm_enable_txq(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
 846                               int queue, u16 ssn,
 847                               const struct iwl_trans_txq_scd_cfg *cfg,
 848                               unsigned int wdg_timeout)
 849{
 850        struct iwl_scd_txq_cfg_cmd cmd = {
 851                .scd_queue = queue,
 852                .action = SCD_CFG_ENABLE_QUEUE,
 853                .window = cfg->frame_limit,
 854                .sta_id = cfg->sta_id,
 855                .ssn = cpu_to_le16(ssn),
 856                .tx_fifo = cfg->fifo,
 857                .aggregate = cfg->aggregate,
 858                .tid = cfg->tid,
 859        };
 860        bool inc_ssn;
 861
 862        if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
 863                return false;
 864
 865        /* Send the enabling command if we need to */
 866        if (!iwl_mvm_update_txq_mapping(mvm, sta, queue, cfg->sta_id, cfg->tid))
 867                return false;
 868
 869        inc_ssn = iwl_trans_txq_enable_cfg(mvm->trans, queue, ssn,
 870                                           NULL, wdg_timeout);
 871        if (inc_ssn)
 872                le16_add_cpu(&cmd.ssn, 1);
 873
 874        WARN(iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd),
 875             "Failed to configure queue %d on FIFO %d\n", queue, cfg->fifo);
 876
 877        return inc_ssn;
 878}
 879
 880static void iwl_mvm_change_queue_tid(struct iwl_mvm *mvm, int queue)
 881{
 882        struct iwl_scd_txq_cfg_cmd cmd = {
 883                .scd_queue = queue,
 884                .action = SCD_CFG_UPDATE_QUEUE_TID,
 885        };
 886        int tid;
 887        unsigned long tid_bitmap;
 888        int ret;
 889
 890        lockdep_assert_held(&mvm->mutex);
 891
 892        if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
 893                return;
 894
 895        tid_bitmap = mvm->queue_info[queue].tid_bitmap;
 896
 897        if (WARN(!tid_bitmap, "TXQ %d has no tids assigned to it\n", queue))
 898                return;
 899
 900        /* Find any TID for queue */
 901        tid = find_first_bit(&tid_bitmap, IWL_MAX_TID_COUNT + 1);
 902        cmd.tid = tid;
 903        cmd.tx_fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
 904
 905        ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd);
 906        if (ret) {
 907                IWL_ERR(mvm, "Failed to update owner of TXQ %d (ret=%d)\n",
 908                        queue, ret);
 909                return;
 910        }
 911
 912        mvm->queue_info[queue].txq_tid = tid;
 913        IWL_DEBUG_TX_QUEUES(mvm, "Changed TXQ %d ownership to tid %d\n",
 914                            queue, tid);
 915}
 916
 917static void iwl_mvm_unshare_queue(struct iwl_mvm *mvm, int queue)
 918{
 919        struct ieee80211_sta *sta;
 920        struct iwl_mvm_sta *mvmsta;
 921        u8 sta_id;
 922        int tid = -1;
 923        unsigned long tid_bitmap;
 924        unsigned int wdg_timeout;
 925        int ssn;
 926        int ret = true;
 927
 928        /* queue sharing is disabled on new TX path */
 929        if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
 930                return;
 931
 932        lockdep_assert_held(&mvm->mutex);
 933
 934        sta_id = mvm->queue_info[queue].ra_sta_id;
 935        tid_bitmap = mvm->queue_info[queue].tid_bitmap;
 936
 937        /* Find TID for queue, and make sure it is the only one on the queue */
 938        tid = find_first_bit(&tid_bitmap, IWL_MAX_TID_COUNT + 1);
 939        if (tid_bitmap != BIT(tid)) {
 940                IWL_ERR(mvm, "Failed to unshare q %d, active tids=0x%lx\n",
 941                        queue, tid_bitmap);
 942                return;
 943        }
 944
 945        IWL_DEBUG_TX_QUEUES(mvm, "Unsharing TXQ %d, keeping tid %d\n", queue,
 946                            tid);
 947
 948        sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
 949                                        lockdep_is_held(&mvm->mutex));
 950
 951        if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta)))
 952                return;
 953
 954        mvmsta = iwl_mvm_sta_from_mac80211(sta);
 955        wdg_timeout = iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
 956
 957        ssn = IEEE80211_SEQ_TO_SN(mvmsta->tid_data[tid].seq_number);
 958
 959        ret = iwl_mvm_redirect_queue(mvm, queue, tid,
 960                                     tid_to_mac80211_ac[tid], ssn,
 961                                     wdg_timeout, true,
 962                                     iwl_mvm_txq_from_tid(sta, tid));
 963        if (ret) {
 964                IWL_ERR(mvm, "Failed to redirect TXQ %d\n", queue);
 965                return;
 966        }
 967
 968        /* If aggs should be turned back on - do it */
 969        if (mvmsta->tid_data[tid].state == IWL_AGG_ON) {
 970                struct iwl_mvm_add_sta_cmd cmd = {0};
 971
 972                mvmsta->tid_disable_agg &= ~BIT(tid);
 973
 974                cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
 975                cmd.sta_id = mvmsta->sta_id;
 976                cmd.add_modify = STA_MODE_MODIFY;
 977                cmd.modify_mask = STA_MODIFY_TID_DISABLE_TX;
 978                cmd.tfd_queue_msk = cpu_to_le32(mvmsta->tfd_queue_msk);
 979                cmd.tid_disable_tx = cpu_to_le16(mvmsta->tid_disable_agg);
 980
 981                ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
 982                                           iwl_mvm_add_sta_cmd_size(mvm), &cmd);
 983                if (!ret) {
 984                        IWL_DEBUG_TX_QUEUES(mvm,
 985                                            "TXQ #%d is now aggregated again\n",
 986                                            queue);
 987
 988                        /* Mark queue intenally as aggregating again */
 989                        iwl_trans_txq_set_shared_mode(mvm->trans, queue, false);
 990                }
 991        }
 992
 993        mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
 994}
 995
 996/*
 997 * Remove inactive TIDs of a given queue.
 998 * If all queue TIDs are inactive - mark the queue as inactive
 999 * If only some the queue TIDs are inactive - unmap them from the queue
1000 *
1001 * Returns %true if all TIDs were removed and the queue could be reused.
1002 */
1003static bool iwl_mvm_remove_inactive_tids(struct iwl_mvm *mvm,
1004                                         struct iwl_mvm_sta *mvmsta, int queue,
1005                                         unsigned long tid_bitmap,
1006                                         unsigned long *unshare_queues,
1007                                         unsigned long *changetid_queues)
1008{
1009        int tid;
1010
1011        lockdep_assert_held(&mvmsta->lock);
1012        lockdep_assert_held(&mvm->mutex);
1013
1014        if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
1015                return false;
1016
1017        /* Go over all non-active TIDs, incl. IWL_MAX_TID_COUNT (for mgmt) */
1018        for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
1019                /* If some TFDs are still queued - don't mark TID as inactive */
1020                if (iwl_mvm_tid_queued(mvm, &mvmsta->tid_data[tid]))
1021                        tid_bitmap &= ~BIT(tid);
1022
1023                /* Don't mark as inactive any TID that has an active BA */
1024                if (mvmsta->tid_data[tid].state != IWL_AGG_OFF)
1025                        tid_bitmap &= ~BIT(tid);
1026        }
1027
1028        /* If all TIDs in the queue are inactive - return it can be reused */
1029        if (tid_bitmap == mvm->queue_info[queue].tid_bitmap) {
1030                IWL_DEBUG_TX_QUEUES(mvm, "Queue %d is inactive\n", queue);
1031                return true;
1032        }
1033
1034        /*
1035         * If we are here, this is a shared queue and not all TIDs timed-out.
1036         * Remove the ones that did.
1037         */
1038        for_each_set_bit(tid, &tid_bitmap, IWL_MAX_TID_COUNT + 1) {
1039                u16 tid_bitmap;
1040
1041                mvmsta->tid_data[tid].txq_id = IWL_MVM_INVALID_QUEUE;
1042                mvm->queue_info[queue].tid_bitmap &= ~BIT(tid);
1043
1044                tid_bitmap = mvm->queue_info[queue].tid_bitmap;
1045
1046                /*
1047                 * We need to take into account a situation in which a TXQ was
1048                 * allocated to TID x, and then turned shared by adding TIDs y
1049                 * and z. If TID x becomes inactive and is removed from the TXQ,
1050                 * ownership must be given to one of the remaining TIDs.
1051                 * This is mainly because if TID x continues - a new queue can't
1052                 * be allocated for it as long as it is an owner of another TXQ.
1053                 *
1054                 * Mark this queue in the right bitmap, we'll send the command
1055                 * to the firmware later.
1056                 */
1057                if (!(tid_bitmap & BIT(mvm->queue_info[queue].txq_tid)))
1058                        set_bit(queue, changetid_queues);
1059
1060                IWL_DEBUG_TX_QUEUES(mvm,
1061                                    "Removing inactive TID %d from shared Q:%d\n",
1062                                    tid, queue);
1063        }
1064
1065        IWL_DEBUG_TX_QUEUES(mvm,
1066                            "TXQ #%d left with tid bitmap 0x%x\n", queue,
1067                            mvm->queue_info[queue].tid_bitmap);
1068
1069        /*
1070         * There may be different TIDs with the same mac queues, so make
1071         * sure all TIDs have existing corresponding mac queues enabled
1072         */
1073        tid_bitmap = mvm->queue_info[queue].tid_bitmap;
1074
1075        /* If the queue is marked as shared - "unshare" it */
1076        if (hweight16(mvm->queue_info[queue].tid_bitmap) == 1 &&
1077            mvm->queue_info[queue].status == IWL_MVM_QUEUE_SHARED) {
1078                IWL_DEBUG_TX_QUEUES(mvm, "Marking Q:%d for reconfig\n",
1079                                    queue);
1080                set_bit(queue, unshare_queues);
1081        }
1082
1083        return false;
1084}
1085
1086/*
1087 * Check for inactivity - this includes checking if any queue
1088 * can be unshared and finding one (and only one) that can be
1089 * reused.
1090 * This function is also invoked as a sort of clean-up task,
1091 * in which case @alloc_for_sta is IWL_MVM_INVALID_STA.
1092 *
1093 * Returns the queue number, or -ENOSPC.
1094 */
1095static int iwl_mvm_inactivity_check(struct iwl_mvm *mvm, u8 alloc_for_sta)
1096{
1097        unsigned long now = jiffies;
1098        unsigned long unshare_queues = 0;
1099        unsigned long changetid_queues = 0;
1100        int i, ret, free_queue = -ENOSPC;
1101        struct ieee80211_sta *queue_owner  = NULL;
1102
1103        lockdep_assert_held(&mvm->mutex);
1104
1105        if (iwl_mvm_has_new_tx_api(mvm))
1106                return -ENOSPC;
1107
1108        rcu_read_lock();
1109
1110        /* we skip the CMD queue below by starting at 1 */
1111        BUILD_BUG_ON(IWL_MVM_DQA_CMD_QUEUE != 0);
1112
1113        for (i = 1; i < IWL_MAX_HW_QUEUES; i++) {
1114                struct ieee80211_sta *sta;
1115                struct iwl_mvm_sta *mvmsta;
1116                u8 sta_id;
1117                int tid;
1118                unsigned long inactive_tid_bitmap = 0;
1119                unsigned long queue_tid_bitmap;
1120
1121                queue_tid_bitmap = mvm->queue_info[i].tid_bitmap;
1122                if (!queue_tid_bitmap)
1123                        continue;
1124
1125                /* If TXQ isn't in active use anyway - nothing to do here... */
1126                if (mvm->queue_info[i].status != IWL_MVM_QUEUE_READY &&
1127                    mvm->queue_info[i].status != IWL_MVM_QUEUE_SHARED)
1128                        continue;
1129
1130                /* Check to see if there are inactive TIDs on this queue */
1131                for_each_set_bit(tid, &queue_tid_bitmap,
1132                                 IWL_MAX_TID_COUNT + 1) {
1133                        if (time_after(mvm->queue_info[i].last_frame_time[tid] +
1134                                       IWL_MVM_DQA_QUEUE_TIMEOUT, now))
1135                                continue;
1136
1137                        inactive_tid_bitmap |= BIT(tid);
1138                }
1139
1140                /* If all TIDs are active - finish check on this queue */
1141                if (!inactive_tid_bitmap)
1142                        continue;
1143
1144                /*
1145                 * If we are here - the queue hadn't been served recently and is
1146                 * in use
1147                 */
1148
1149                sta_id = mvm->queue_info[i].ra_sta_id;
1150                sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
1151
1152                /*
1153                 * If the STA doesn't exist anymore, it isn't an error. It could
1154                 * be that it was removed since getting the queues, and in this
1155                 * case it should've inactivated its queues anyway.
1156                 */
1157                if (IS_ERR_OR_NULL(sta))
1158                        continue;
1159
1160                mvmsta = iwl_mvm_sta_from_mac80211(sta);
1161
1162                spin_lock_bh(&mvmsta->lock);
1163                ret = iwl_mvm_remove_inactive_tids(mvm, mvmsta, i,
1164                                                   inactive_tid_bitmap,
1165                                                   &unshare_queues,
1166                                                   &changetid_queues);
1167                if (ret >= 0 && free_queue < 0) {
1168                        queue_owner = sta;
1169                        free_queue = ret;
1170                }
1171                /* only unlock sta lock - we still need the queue info lock */
1172                spin_unlock_bh(&mvmsta->lock);
1173        }
1174
1175
1176        /* Reconfigure queues requiring reconfiguation */
1177        for_each_set_bit(i, &unshare_queues, IWL_MAX_HW_QUEUES)
1178                iwl_mvm_unshare_queue(mvm, i);
1179        for_each_set_bit(i, &changetid_queues, IWL_MAX_HW_QUEUES)
1180                iwl_mvm_change_queue_tid(mvm, i);
1181
1182        if (free_queue >= 0 && alloc_for_sta != IWL_MVM_INVALID_STA) {
1183                ret = iwl_mvm_free_inactive_queue(mvm, free_queue, queue_owner,
1184                                                  alloc_for_sta);
1185                if (ret) {
1186                        rcu_read_unlock();
1187                        return ret;
1188                }
1189        }
1190
1191        rcu_read_unlock();
1192
1193        return free_queue;
1194}
1195
1196static int iwl_mvm_sta_alloc_queue(struct iwl_mvm *mvm,
1197                                   struct ieee80211_sta *sta, u8 ac, int tid)
1198{
1199        struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1200        struct iwl_trans_txq_scd_cfg cfg = {
1201                .fifo = iwl_mvm_mac_ac_to_tx_fifo(mvm, ac),
1202                .sta_id = mvmsta->sta_id,
1203                .tid = tid,
1204                .frame_limit = IWL_FRAME_LIMIT,
1205        };
1206        unsigned int wdg_timeout =
1207                iwl_mvm_get_wd_timeout(mvm, mvmsta->vif, false, false);
1208        int queue = -1;
1209        unsigned long disable_agg_tids = 0;
1210        enum iwl_mvm_agg_state queue_state;
1211        bool shared_queue = false, inc_ssn;
1212        int ssn;
1213        unsigned long tfd_queue_mask;
1214        int ret;
1215
1216        lockdep_assert_held(&mvm->mutex);
1217
1218        if (iwl_mvm_has_new_tx_api(mvm))
1219                return iwl_mvm_sta_alloc_queue_tvqm(mvm, sta, ac, tid);
1220
1221        spin_lock_bh(&mvmsta->lock);
1222        tfd_queue_mask = mvmsta->tfd_queue_msk;
1223        ssn = IEEE80211_SEQ_TO_SN(mvmsta->tid_data[tid].seq_number);
1224        spin_unlock_bh(&mvmsta->lock);
1225
1226        if (tid == IWL_MAX_TID_COUNT) {
1227                queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
1228                                                IWL_MVM_DQA_MIN_MGMT_QUEUE,
1229                                                IWL_MVM_DQA_MAX_MGMT_QUEUE);
1230                if (queue >= IWL_MVM_DQA_MIN_MGMT_QUEUE)
1231                        IWL_DEBUG_TX_QUEUES(mvm, "Found free MGMT queue #%d\n",
1232                                            queue);
1233
1234                /* If no such queue is found, we'll use a DATA queue instead */
1235        }
1236
1237        if ((queue < 0 && mvmsta->reserved_queue != IEEE80211_INVAL_HW_QUEUE) &&
1238            (mvm->queue_info[mvmsta->reserved_queue].status ==
1239                        IWL_MVM_QUEUE_RESERVED)) {
1240                queue = mvmsta->reserved_queue;
1241                mvm->queue_info[queue].reserved = true;
1242                IWL_DEBUG_TX_QUEUES(mvm, "Using reserved queue #%d\n", queue);
1243        }
1244
1245        if (queue < 0)
1246                queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
1247                                                IWL_MVM_DQA_MIN_DATA_QUEUE,
1248                                                IWL_MVM_DQA_MAX_DATA_QUEUE);
1249        if (queue < 0) {
1250                /* try harder - perhaps kill an inactive queue */
1251                queue = iwl_mvm_inactivity_check(mvm, mvmsta->sta_id);
1252        }
1253
1254        /* No free queue - we'll have to share */
1255        if (queue <= 0) {
1256                queue = iwl_mvm_get_shared_queue(mvm, tfd_queue_mask, ac);
1257                if (queue > 0) {
1258                        shared_queue = true;
1259                        mvm->queue_info[queue].status = IWL_MVM_QUEUE_SHARED;
1260                }
1261        }
1262
1263        /*
1264         * Mark TXQ as ready, even though it hasn't been fully configured yet,
1265         * to make sure no one else takes it.
1266         * This will allow avoiding re-acquiring the lock at the end of the
1267         * configuration. On error we'll mark it back as free.
1268         */
1269        if (queue > 0 && !shared_queue)
1270                mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
1271
1272        /* This shouldn't happen - out of queues */
1273        if (WARN_ON(queue <= 0)) {
1274                IWL_ERR(mvm, "No available queues for tid %d on sta_id %d\n",
1275                        tid, cfg.sta_id);
1276                return queue;
1277        }
1278
1279        /*
1280         * Actual en/disablement of aggregations is through the ADD_STA HCMD,
1281         * but for configuring the SCD to send A-MPDUs we need to mark the queue
1282         * as aggregatable.
1283         * Mark all DATA queues as allowing to be aggregated at some point
1284         */
1285        cfg.aggregate = (queue >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
1286                         queue == IWL_MVM_DQA_BSS_CLIENT_QUEUE);
1287
1288        IWL_DEBUG_TX_QUEUES(mvm,
1289                            "Allocating %squeue #%d to sta %d on tid %d\n",
1290                            shared_queue ? "shared " : "", queue,
1291                            mvmsta->sta_id, tid);
1292
1293        if (shared_queue) {
1294                /* Disable any open aggs on this queue */
1295                disable_agg_tids = iwl_mvm_get_queue_agg_tids(mvm, queue);
1296
1297                if (disable_agg_tids) {
1298                        IWL_DEBUG_TX_QUEUES(mvm, "Disabling aggs on queue %d\n",
1299                                            queue);
1300                        iwl_mvm_invalidate_sta_queue(mvm, queue,
1301                                                     disable_agg_tids, false);
1302                }
1303        }
1304
1305        inc_ssn = iwl_mvm_enable_txq(mvm, sta, queue, ssn, &cfg, wdg_timeout);
1306
1307        /*
1308         * Mark queue as shared in transport if shared
1309         * Note this has to be done after queue enablement because enablement
1310         * can also set this value, and there is no indication there to shared
1311         * queues
1312         */
1313        if (shared_queue)
1314                iwl_trans_txq_set_shared_mode(mvm->trans, queue, true);
1315
1316        spin_lock_bh(&mvmsta->lock);
1317        /*
1318         * This looks racy, but it is not. We have only one packet for
1319         * this ra/tid in our Tx path since we stop the Qdisc when we
1320         * need to allocate a new TFD queue.
1321         */
1322        if (inc_ssn) {
1323                mvmsta->tid_data[tid].seq_number += 0x10;
1324                ssn = (ssn + 1) & IEEE80211_SCTL_SEQ;
1325        }
1326        mvmsta->tid_data[tid].txq_id = queue;
1327        mvmsta->tfd_queue_msk |= BIT(queue);
1328        queue_state = mvmsta->tid_data[tid].state;
1329
1330        if (mvmsta->reserved_queue == queue)
1331                mvmsta->reserved_queue = IEEE80211_INVAL_HW_QUEUE;
1332        spin_unlock_bh(&mvmsta->lock);
1333
1334        if (!shared_queue) {
1335                ret = iwl_mvm_sta_send_to_fw(mvm, sta, true, STA_MODIFY_QUEUES);
1336                if (ret)
1337                        goto out_err;
1338
1339                /* If we need to re-enable aggregations... */
1340                if (queue_state == IWL_AGG_ON) {
1341                        ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
1342                        if (ret)
1343                                goto out_err;
1344                }
1345        } else {
1346                /* Redirect queue, if needed */
1347                ret = iwl_mvm_redirect_queue(mvm, queue, tid, ac, ssn,
1348                                             wdg_timeout, false,
1349                                             iwl_mvm_txq_from_tid(sta, tid));
1350                if (ret)
1351                        goto out_err;
1352        }
1353
1354        return 0;
1355
1356out_err:
1357        iwl_mvm_disable_txq(mvm, sta, queue, tid, 0);
1358
1359        return ret;
1360}
1361
1362static inline u8 iwl_mvm_tid_to_ac_queue(int tid)
1363{
1364        if (tid == IWL_MAX_TID_COUNT)
1365                return IEEE80211_AC_VO; /* MGMT */
1366
1367        return tid_to_mac80211_ac[tid];
1368}
1369
1370void iwl_mvm_add_new_dqa_stream_wk(struct work_struct *wk)
1371{
1372        struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm,
1373                                           add_stream_wk);
1374
1375        mutex_lock(&mvm->mutex);
1376
1377        iwl_mvm_inactivity_check(mvm, IWL_MVM_INVALID_STA);
1378
1379        while (!list_empty(&mvm->add_stream_txqs)) {
1380                struct iwl_mvm_txq *mvmtxq;
1381                struct ieee80211_txq *txq;
1382                u8 tid;
1383
1384                mvmtxq = list_first_entry(&mvm->add_stream_txqs,
1385                                          struct iwl_mvm_txq, list);
1386
1387                txq = container_of((void *)mvmtxq, struct ieee80211_txq,
1388                                   drv_priv);
1389                tid = txq->tid;
1390                if (tid == IEEE80211_NUM_TIDS)
1391                        tid = IWL_MAX_TID_COUNT;
1392
1393                iwl_mvm_sta_alloc_queue(mvm, txq->sta, txq->ac, tid);
1394                list_del_init(&mvmtxq->list);
1395                local_bh_disable();
1396                iwl_mvm_mac_itxq_xmit(mvm->hw, txq);
1397                local_bh_enable();
1398        }
1399
1400        mutex_unlock(&mvm->mutex);
1401}
1402
1403static int iwl_mvm_reserve_sta_stream(struct iwl_mvm *mvm,
1404                                      struct ieee80211_sta *sta,
1405                                      enum nl80211_iftype vif_type)
1406{
1407        struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1408        int queue;
1409
1410        /* queue reserving is disabled on new TX path */
1411        if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
1412                return 0;
1413
1414        /* run the general cleanup/unsharing of queues */
1415        iwl_mvm_inactivity_check(mvm, IWL_MVM_INVALID_STA);
1416
1417        /* Make sure we have free resources for this STA */
1418        if (vif_type == NL80211_IFTYPE_STATION && !sta->tdls &&
1419            !mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].tid_bitmap &&
1420            (mvm->queue_info[IWL_MVM_DQA_BSS_CLIENT_QUEUE].status ==
1421             IWL_MVM_QUEUE_FREE))
1422                queue = IWL_MVM_DQA_BSS_CLIENT_QUEUE;
1423        else
1424                queue = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
1425                                                IWL_MVM_DQA_MIN_DATA_QUEUE,
1426                                                IWL_MVM_DQA_MAX_DATA_QUEUE);
1427        if (queue < 0) {
1428                /* try again - this time kick out a queue if needed */
1429                queue = iwl_mvm_inactivity_check(mvm, mvmsta->sta_id);
1430                if (queue < 0) {
1431                        IWL_ERR(mvm, "No available queues for new station\n");
1432                        return -ENOSPC;
1433                }
1434        }
1435        mvm->queue_info[queue].status = IWL_MVM_QUEUE_RESERVED;
1436
1437        mvmsta->reserved_queue = queue;
1438
1439        IWL_DEBUG_TX_QUEUES(mvm, "Reserving data queue #%d for sta_id %d\n",
1440                            queue, mvmsta->sta_id);
1441
1442        return 0;
1443}
1444
1445/*
1446 * In DQA mode, after a HW restart the queues should be allocated as before, in
1447 * order to avoid race conditions when there are shared queues. This function
1448 * does the re-mapping and queue allocation.
1449 *
1450 * Note that re-enabling aggregations isn't done in this function.
1451 */
1452static void iwl_mvm_realloc_queues_after_restart(struct iwl_mvm *mvm,
1453                                                 struct ieee80211_sta *sta)
1454{
1455        struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1456        unsigned int wdg =
1457                iwl_mvm_get_wd_timeout(mvm, mvm_sta->vif, false, false);
1458        int i;
1459        struct iwl_trans_txq_scd_cfg cfg = {
1460                .sta_id = mvm_sta->sta_id,
1461                .frame_limit = IWL_FRAME_LIMIT,
1462        };
1463
1464        /* Make sure reserved queue is still marked as such (if allocated) */
1465        if (mvm_sta->reserved_queue != IEEE80211_INVAL_HW_QUEUE)
1466                mvm->queue_info[mvm_sta->reserved_queue].status =
1467                        IWL_MVM_QUEUE_RESERVED;
1468
1469        for (i = 0; i <= IWL_MAX_TID_COUNT; i++) {
1470                struct iwl_mvm_tid_data *tid_data = &mvm_sta->tid_data[i];
1471                int txq_id = tid_data->txq_id;
1472                int ac;
1473
1474                if (txq_id == IWL_MVM_INVALID_QUEUE)
1475                        continue;
1476
1477                ac = tid_to_mac80211_ac[i];
1478
1479                if (iwl_mvm_has_new_tx_api(mvm)) {
1480                        IWL_DEBUG_TX_QUEUES(mvm,
1481                                            "Re-mapping sta %d tid %d\n",
1482                                            mvm_sta->sta_id, i);
1483                        txq_id = iwl_mvm_tvqm_enable_txq(mvm, mvm_sta->sta_id,
1484                                                         i, wdg);
1485                        /*
1486                         * on failures, just set it to IWL_MVM_INVALID_QUEUE
1487                         * to try again later, we have no other good way of
1488                         * failing here
1489                         */
1490                        if (txq_id < 0)
1491                                txq_id = IWL_MVM_INVALID_QUEUE;
1492                        tid_data->txq_id = txq_id;
1493
1494                        /*
1495                         * Since we don't set the seq number after reset, and HW
1496                         * sets it now, FW reset will cause the seq num to start
1497                         * at 0 again, so driver will need to update it
1498                         * internally as well, so it keeps in sync with real val
1499                         */
1500                        tid_data->seq_number = 0;
1501                } else {
1502                        u16 seq = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
1503
1504                        cfg.tid = i;
1505                        cfg.fifo = iwl_mvm_mac_ac_to_tx_fifo(mvm, ac);
1506                        cfg.aggregate = (txq_id >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
1507                                         txq_id ==
1508                                         IWL_MVM_DQA_BSS_CLIENT_QUEUE);
1509
1510                        IWL_DEBUG_TX_QUEUES(mvm,
1511                                            "Re-mapping sta %d tid %d to queue %d\n",
1512                                            mvm_sta->sta_id, i, txq_id);
1513
1514                        iwl_mvm_enable_txq(mvm, sta, txq_id, seq, &cfg, wdg);
1515                        mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_READY;
1516                }
1517        }
1518}
1519
1520static int iwl_mvm_add_int_sta_common(struct iwl_mvm *mvm,
1521                                      struct iwl_mvm_int_sta *sta,
1522                                      const u8 *addr,
1523                                      u16 mac_id, u16 color)
1524{
1525        struct iwl_mvm_add_sta_cmd cmd;
1526        int ret;
1527        u32 status = ADD_STA_SUCCESS;
1528
1529        lockdep_assert_held(&mvm->mutex);
1530
1531        memset(&cmd, 0, sizeof(cmd));
1532        cmd.sta_id = sta->sta_id;
1533        cmd.mac_id_n_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mac_id,
1534                                                             color));
1535        if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
1536                cmd.station_type = sta->type;
1537
1538        if (!iwl_mvm_has_new_tx_api(mvm))
1539                cmd.tfd_queue_msk = cpu_to_le32(sta->tfd_queue_msk);
1540        cmd.tid_disable_tx = cpu_to_le16(0xffff);
1541
1542        if (addr)
1543                memcpy(cmd.addr, addr, ETH_ALEN);
1544
1545        ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1546                                          iwl_mvm_add_sta_cmd_size(mvm),
1547                                          &cmd, &status);
1548        if (ret)
1549                return ret;
1550
1551        switch (status & IWL_ADD_STA_STATUS_MASK) {
1552        case ADD_STA_SUCCESS:
1553                IWL_DEBUG_INFO(mvm, "Internal station added.\n");
1554                return 0;
1555        default:
1556                ret = -EIO;
1557                IWL_ERR(mvm, "Add internal station failed, status=0x%x\n",
1558                        status);
1559                break;
1560        }
1561        return ret;
1562}
1563
1564int iwl_mvm_add_sta(struct iwl_mvm *mvm,
1565                    struct ieee80211_vif *vif,
1566                    struct ieee80211_sta *sta)
1567{
1568        struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1569        struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1570        struct iwl_mvm_rxq_dup_data *dup_data;
1571        int i, ret, sta_id;
1572        bool sta_update = false;
1573        unsigned int sta_flags = 0;
1574
1575        lockdep_assert_held(&mvm->mutex);
1576
1577        if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1578                sta_id = iwl_mvm_find_free_sta_id(mvm,
1579                                                  ieee80211_vif_type_p2p(vif));
1580        else
1581                sta_id = mvm_sta->sta_id;
1582
1583        if (sta_id == IWL_MVM_INVALID_STA)
1584                return -ENOSPC;
1585
1586        spin_lock_init(&mvm_sta->lock);
1587
1588        /* if this is a HW restart re-alloc existing queues */
1589        if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1590                struct iwl_mvm_int_sta tmp_sta = {
1591                        .sta_id = sta_id,
1592                        .type = mvm_sta->sta_type,
1593                };
1594
1595                /*
1596                 * First add an empty station since allocating
1597                 * a queue requires a valid station
1598                 */
1599                ret = iwl_mvm_add_int_sta_common(mvm, &tmp_sta, sta->addr,
1600                                                 mvmvif->id, mvmvif->color);
1601                if (ret)
1602                        goto err;
1603
1604                iwl_mvm_realloc_queues_after_restart(mvm, sta);
1605                sta_update = true;
1606                sta_flags = iwl_mvm_has_new_tx_api(mvm) ? 0 : STA_MODIFY_QUEUES;
1607                goto update_fw;
1608        }
1609
1610        mvm_sta->sta_id = sta_id;
1611        mvm_sta->mac_id_n_color = FW_CMD_ID_AND_COLOR(mvmvif->id,
1612                                                      mvmvif->color);
1613        mvm_sta->vif = vif;
1614        if (!mvm->trans->trans_cfg->gen2)
1615                mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
1616        else
1617                mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_GEN2_DEF;
1618        mvm_sta->tx_protection = 0;
1619        mvm_sta->tt_tx_protection = false;
1620        mvm_sta->sta_type = sta->tdls ? IWL_STA_TDLS_LINK : IWL_STA_LINK;
1621
1622        /* HW restart, don't assume the memory has been zeroed */
1623        mvm_sta->tid_disable_agg = 0xffff; /* No aggs at first */
1624        mvm_sta->tfd_queue_msk = 0;
1625
1626        /* for HW restart - reset everything but the sequence number */
1627        for (i = 0; i <= IWL_MAX_TID_COUNT; i++) {
1628                u16 seq = mvm_sta->tid_data[i].seq_number;
1629                memset(&mvm_sta->tid_data[i], 0, sizeof(mvm_sta->tid_data[i]));
1630                mvm_sta->tid_data[i].seq_number = seq;
1631
1632                /*
1633                 * Mark all queues for this STA as unallocated and defer TX
1634                 * frames until the queue is allocated
1635                 */
1636                mvm_sta->tid_data[i].txq_id = IWL_MVM_INVALID_QUEUE;
1637        }
1638
1639        for (i = 0; i < ARRAY_SIZE(sta->txq); i++) {
1640                struct iwl_mvm_txq *mvmtxq =
1641                        iwl_mvm_txq_from_mac80211(sta->txq[i]);
1642
1643                mvmtxq->txq_id = IWL_MVM_INVALID_QUEUE;
1644                INIT_LIST_HEAD(&mvmtxq->list);
1645                atomic_set(&mvmtxq->tx_request, 0);
1646        }
1647
1648        mvm_sta->agg_tids = 0;
1649
1650        if (iwl_mvm_has_new_rx_api(mvm) &&
1651            !test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1652                int q;
1653
1654                dup_data = kcalloc(mvm->trans->num_rx_queues,
1655                                   sizeof(*dup_data), GFP_KERNEL);
1656                if (!dup_data)
1657                        return -ENOMEM;
1658                /*
1659                 * Initialize all the last_seq values to 0xffff which can never
1660                 * compare equal to the frame's seq_ctrl in the check in
1661                 * iwl_mvm_is_dup() since the lower 4 bits are the fragment
1662                 * number and fragmented packets don't reach that function.
1663                 *
1664                 * This thus allows receiving a packet with seqno 0 and the
1665                 * retry bit set as the very first packet on a new TID.
1666                 */
1667                for (q = 0; q < mvm->trans->num_rx_queues; q++)
1668                        memset(dup_data[q].last_seq, 0xff,
1669                               sizeof(dup_data[q].last_seq));
1670                mvm_sta->dup_data = dup_data;
1671        }
1672
1673        if (!iwl_mvm_has_new_tx_api(mvm)) {
1674                ret = iwl_mvm_reserve_sta_stream(mvm, sta,
1675                                                 ieee80211_vif_type_p2p(vif));
1676                if (ret)
1677                        goto err;
1678        }
1679
1680        /*
1681         * if rs is registered with mac80211, then "add station" will be handled
1682         * via the corresponding ops, otherwise need to notify rate scaling here
1683         */
1684        if (iwl_mvm_has_tlc_offload(mvm))
1685                iwl_mvm_rs_add_sta(mvm, mvm_sta);
1686        else
1687                spin_lock_init(&mvm_sta->lq_sta.rs_drv.pers.lock);
1688
1689        iwl_mvm_toggle_tx_ant(mvm, &mvm_sta->tx_ant);
1690
1691update_fw:
1692        ret = iwl_mvm_sta_send_to_fw(mvm, sta, sta_update, sta_flags);
1693        if (ret)
1694                goto err;
1695
1696        if (vif->type == NL80211_IFTYPE_STATION) {
1697                if (!sta->tdls) {
1698                        WARN_ON(mvmvif->ap_sta_id != IWL_MVM_INVALID_STA);
1699                        mvmvif->ap_sta_id = sta_id;
1700                } else {
1701                        WARN_ON(mvmvif->ap_sta_id == IWL_MVM_INVALID_STA);
1702                }
1703        }
1704
1705        rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], sta);
1706
1707        return 0;
1708
1709err:
1710        return ret;
1711}
1712
1713int iwl_mvm_drain_sta(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
1714                      bool drain)
1715{
1716        struct iwl_mvm_add_sta_cmd cmd = {};
1717        int ret;
1718        u32 status;
1719
1720        lockdep_assert_held(&mvm->mutex);
1721
1722        cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
1723        cmd.sta_id = mvmsta->sta_id;
1724        cmd.add_modify = STA_MODE_MODIFY;
1725        cmd.station_flags = drain ? cpu_to_le32(STA_FLG_DRAIN_FLOW) : 0;
1726        cmd.station_flags_msk = cpu_to_le32(STA_FLG_DRAIN_FLOW);
1727
1728        status = ADD_STA_SUCCESS;
1729        ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
1730                                          iwl_mvm_add_sta_cmd_size(mvm),
1731                                          &cmd, &status);
1732        if (ret)
1733                return ret;
1734
1735        switch (status & IWL_ADD_STA_STATUS_MASK) {
1736        case ADD_STA_SUCCESS:
1737                IWL_DEBUG_INFO(mvm, "Frames for staid %d will drained in fw\n",
1738                               mvmsta->sta_id);
1739                break;
1740        default:
1741                ret = -EIO;
1742                IWL_ERR(mvm, "Couldn't drain frames for staid %d\n",
1743                        mvmsta->sta_id);
1744                break;
1745        }
1746
1747        return ret;
1748}
1749
1750/*
1751 * Remove a station from the FW table. Before sending the command to remove
1752 * the station validate that the station is indeed known to the driver (sanity
1753 * only).
1754 */
1755static int iwl_mvm_rm_sta_common(struct iwl_mvm *mvm, u8 sta_id)
1756{
1757        struct ieee80211_sta *sta;
1758        struct iwl_mvm_rm_sta_cmd rm_sta_cmd = {
1759                .sta_id = sta_id,
1760        };
1761        int ret;
1762
1763        sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1764                                        lockdep_is_held(&mvm->mutex));
1765
1766        /* Note: internal stations are marked as error values */
1767        if (!sta) {
1768                IWL_ERR(mvm, "Invalid station id\n");
1769                return -EINVAL;
1770        }
1771
1772        ret = iwl_mvm_send_cmd_pdu(mvm, REMOVE_STA, 0,
1773                                   sizeof(rm_sta_cmd), &rm_sta_cmd);
1774        if (ret) {
1775                IWL_ERR(mvm, "Failed to remove station. Id=%d\n", sta_id);
1776                return ret;
1777        }
1778
1779        return 0;
1780}
1781
1782static void iwl_mvm_disable_sta_queues(struct iwl_mvm *mvm,
1783                                       struct ieee80211_vif *vif,
1784                                       struct ieee80211_sta *sta)
1785{
1786        struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1787        int i;
1788
1789        lockdep_assert_held(&mvm->mutex);
1790
1791        for (i = 0; i < ARRAY_SIZE(mvm_sta->tid_data); i++) {
1792                if (mvm_sta->tid_data[i].txq_id == IWL_MVM_INVALID_QUEUE)
1793                        continue;
1794
1795                iwl_mvm_disable_txq(mvm, sta, mvm_sta->tid_data[i].txq_id, i,
1796                                    0);
1797                mvm_sta->tid_data[i].txq_id = IWL_MVM_INVALID_QUEUE;
1798        }
1799
1800        for (i = 0; i < ARRAY_SIZE(sta->txq); i++) {
1801                struct iwl_mvm_txq *mvmtxq =
1802                        iwl_mvm_txq_from_mac80211(sta->txq[i]);
1803
1804                mvmtxq->txq_id = IWL_MVM_INVALID_QUEUE;
1805        }
1806}
1807
1808int iwl_mvm_wait_sta_queues_empty(struct iwl_mvm *mvm,
1809                                  struct iwl_mvm_sta *mvm_sta)
1810{
1811        int i;
1812
1813        for (i = 0; i < ARRAY_SIZE(mvm_sta->tid_data); i++) {
1814                u16 txq_id;
1815                int ret;
1816
1817                spin_lock_bh(&mvm_sta->lock);
1818                txq_id = mvm_sta->tid_data[i].txq_id;
1819                spin_unlock_bh(&mvm_sta->lock);
1820
1821                if (txq_id == IWL_MVM_INVALID_QUEUE)
1822                        continue;
1823
1824                ret = iwl_trans_wait_txq_empty(mvm->trans, txq_id);
1825                if (ret)
1826                        return ret;
1827        }
1828
1829        return 0;
1830}
1831
1832int iwl_mvm_rm_sta(struct iwl_mvm *mvm,
1833                   struct ieee80211_vif *vif,
1834                   struct ieee80211_sta *sta)
1835{
1836        struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1837        struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1838        u8 sta_id = mvm_sta->sta_id;
1839        int ret;
1840
1841        lockdep_assert_held(&mvm->mutex);
1842
1843        if (iwl_mvm_has_new_rx_api(mvm))
1844                kfree(mvm_sta->dup_data);
1845
1846        ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
1847        if (ret)
1848                return ret;
1849
1850        /* flush its queues here since we are freeing mvm_sta */
1851        ret = iwl_mvm_flush_sta(mvm, mvm_sta, false, 0);
1852        if (ret)
1853                return ret;
1854        if (iwl_mvm_has_new_tx_api(mvm)) {
1855                ret = iwl_mvm_wait_sta_queues_empty(mvm, mvm_sta);
1856        } else {
1857                u32 q_mask = mvm_sta->tfd_queue_msk;
1858
1859                ret = iwl_trans_wait_tx_queues_empty(mvm->trans,
1860                                                     q_mask);
1861        }
1862        if (ret)
1863                return ret;
1864
1865        ret = iwl_mvm_drain_sta(mvm, mvm_sta, false);
1866
1867        iwl_mvm_disable_sta_queues(mvm, vif, sta);
1868
1869        /* If there is a TXQ still marked as reserved - free it */
1870        if (mvm_sta->reserved_queue != IEEE80211_INVAL_HW_QUEUE) {
1871                u8 reserved_txq = mvm_sta->reserved_queue;
1872                enum iwl_mvm_queue_status *status;
1873
1874                /*
1875                 * If no traffic has gone through the reserved TXQ - it
1876                 * is still marked as IWL_MVM_QUEUE_RESERVED, and
1877                 * should be manually marked as free again
1878                 */
1879                status = &mvm->queue_info[reserved_txq].status;
1880                if (WARN((*status != IWL_MVM_QUEUE_RESERVED) &&
1881                         (*status != IWL_MVM_QUEUE_FREE),
1882                         "sta_id %d reserved txq %d status %d",
1883                         sta_id, reserved_txq, *status))
1884                        return -EINVAL;
1885
1886                *status = IWL_MVM_QUEUE_FREE;
1887        }
1888
1889        if (vif->type == NL80211_IFTYPE_STATION &&
1890            mvmvif->ap_sta_id == sta_id) {
1891                /* if associated - we can't remove the AP STA now */
1892                if (vif->bss_conf.assoc)
1893                        return ret;
1894
1895                /* unassoc - go ahead - remove the AP STA now */
1896                mvmvif->ap_sta_id = IWL_MVM_INVALID_STA;
1897        }
1898
1899        /*
1900         * This shouldn't happen - the TDLS channel switch should be canceled
1901         * before the STA is removed.
1902         */
1903        if (WARN_ON_ONCE(mvm->tdls_cs.peer.sta_id == sta_id)) {
1904                mvm->tdls_cs.peer.sta_id = IWL_MVM_INVALID_STA;
1905                cancel_delayed_work(&mvm->tdls_cs.dwork);
1906        }
1907
1908        /*
1909         * Make sure that the tx response code sees the station as -EBUSY and
1910         * calls the drain worker.
1911         */
1912        spin_lock_bh(&mvm_sta->lock);
1913        spin_unlock_bh(&mvm_sta->lock);
1914
1915        ret = iwl_mvm_rm_sta_common(mvm, mvm_sta->sta_id);
1916        RCU_INIT_POINTER(mvm->fw_id_to_mac_id[mvm_sta->sta_id], NULL);
1917
1918        return ret;
1919}
1920
1921int iwl_mvm_rm_sta_id(struct iwl_mvm *mvm,
1922                      struct ieee80211_vif *vif,
1923                      u8 sta_id)
1924{
1925        int ret = iwl_mvm_rm_sta_common(mvm, sta_id);
1926
1927        lockdep_assert_held(&mvm->mutex);
1928
1929        RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id], NULL);
1930        return ret;
1931}
1932
1933int iwl_mvm_allocate_int_sta(struct iwl_mvm *mvm,
1934                             struct iwl_mvm_int_sta *sta,
1935                             u32 qmask, enum nl80211_iftype iftype,
1936                             enum iwl_sta_type type)
1937{
1938        if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) ||
1939            sta->sta_id == IWL_MVM_INVALID_STA) {
1940                sta->sta_id = iwl_mvm_find_free_sta_id(mvm, iftype);
1941                if (WARN_ON_ONCE(sta->sta_id == IWL_MVM_INVALID_STA))
1942                        return -ENOSPC;
1943        }
1944
1945        sta->tfd_queue_msk = qmask;
1946        sta->type = type;
1947
1948        /* put a non-NULL value so iterating over the stations won't stop */
1949        rcu_assign_pointer(mvm->fw_id_to_mac_id[sta->sta_id], ERR_PTR(-EINVAL));
1950        return 0;
1951}
1952
1953void iwl_mvm_dealloc_int_sta(struct iwl_mvm *mvm, struct iwl_mvm_int_sta *sta)
1954{
1955        RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta->sta_id], NULL);
1956        memset(sta, 0, sizeof(struct iwl_mvm_int_sta));
1957        sta->sta_id = IWL_MVM_INVALID_STA;
1958}
1959
1960static void iwl_mvm_enable_aux_snif_queue(struct iwl_mvm *mvm, u16 queue,
1961                                          u8 sta_id, u8 fifo)
1962{
1963        unsigned int wdg_timeout = iwlmvm_mod_params.tfd_q_hang_detect ?
1964                mvm->trans->trans_cfg->base_params->wd_timeout :
1965                IWL_WATCHDOG_DISABLED;
1966        struct iwl_trans_txq_scd_cfg cfg = {
1967                .fifo = fifo,
1968                .sta_id = sta_id,
1969                .tid = IWL_MAX_TID_COUNT,
1970                .aggregate = false,
1971                .frame_limit = IWL_FRAME_LIMIT,
1972        };
1973
1974        WARN_ON(iwl_mvm_has_new_tx_api(mvm));
1975
1976        iwl_mvm_enable_txq(mvm, NULL, queue, 0, &cfg, wdg_timeout);
1977}
1978
1979static int iwl_mvm_enable_aux_snif_queue_tvqm(struct iwl_mvm *mvm, u8 sta_id)
1980{
1981        unsigned int wdg_timeout = iwlmvm_mod_params.tfd_q_hang_detect ?
1982                mvm->trans->trans_cfg->base_params->wd_timeout :
1983                IWL_WATCHDOG_DISABLED;
1984
1985        WARN_ON(!iwl_mvm_has_new_tx_api(mvm));
1986
1987        return iwl_mvm_tvqm_enable_txq(mvm, sta_id, IWL_MAX_TID_COUNT,
1988                                       wdg_timeout);
1989}
1990
1991static int iwl_mvm_add_int_sta_with_queue(struct iwl_mvm *mvm, int macidx,
1992                                          int maccolor,
1993                                          struct iwl_mvm_int_sta *sta,
1994                                          u16 *queue, int fifo)
1995{
1996        int ret;
1997
1998        /* Map queue to fifo - needs to happen before adding station */
1999        if (!iwl_mvm_has_new_tx_api(mvm))
2000                iwl_mvm_enable_aux_snif_queue(mvm, *queue, sta->sta_id, fifo);
2001
2002        ret = iwl_mvm_add_int_sta_common(mvm, sta, NULL, macidx, maccolor);
2003        if (ret) {
2004                if (!iwl_mvm_has_new_tx_api(mvm))
2005                        iwl_mvm_disable_txq(mvm, NULL, *queue,
2006                                            IWL_MAX_TID_COUNT, 0);
2007                return ret;
2008        }
2009
2010        /*
2011         * For 22000 firmware and on we cannot add queue to a station unknown
2012         * to firmware so enable queue here - after the station was added
2013         */
2014        if (iwl_mvm_has_new_tx_api(mvm)) {
2015                int txq;
2016
2017                txq = iwl_mvm_enable_aux_snif_queue_tvqm(mvm, sta->sta_id);
2018                if (txq < 0) {
2019                        iwl_mvm_rm_sta_common(mvm, sta->sta_id);
2020                        return txq;
2021                }
2022
2023                *queue = txq;
2024        }
2025
2026        return 0;
2027}
2028
2029int iwl_mvm_add_aux_sta(struct iwl_mvm *mvm)
2030{
2031        int ret;
2032
2033        lockdep_assert_held(&mvm->mutex);
2034
2035        /* Allocate aux station and assign to it the aux queue */
2036        ret = iwl_mvm_allocate_int_sta(mvm, &mvm->aux_sta, BIT(mvm->aux_queue),
2037                                       NL80211_IFTYPE_UNSPECIFIED,
2038                                       IWL_STA_AUX_ACTIVITY);
2039        if (ret)
2040                return ret;
2041
2042        ret = iwl_mvm_add_int_sta_with_queue(mvm, MAC_INDEX_AUX, 0,
2043                                             &mvm->aux_sta, &mvm->aux_queue,
2044                                             IWL_MVM_TX_FIFO_MCAST);
2045        if (ret) {
2046                iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
2047                return ret;
2048        }
2049
2050        return 0;
2051}
2052
2053int iwl_mvm_add_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2054{
2055        struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2056
2057        lockdep_assert_held(&mvm->mutex);
2058
2059        return iwl_mvm_add_int_sta_with_queue(mvm, mvmvif->id, mvmvif->color,
2060                                              &mvm->snif_sta, &mvm->snif_queue,
2061                                              IWL_MVM_TX_FIFO_BE);
2062}
2063
2064int iwl_mvm_rm_snif_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2065{
2066        int ret;
2067
2068        lockdep_assert_held(&mvm->mutex);
2069
2070        iwl_mvm_disable_txq(mvm, NULL, mvm->snif_queue, IWL_MAX_TID_COUNT, 0);
2071        ret = iwl_mvm_rm_sta_common(mvm, mvm->snif_sta.sta_id);
2072        if (ret)
2073                IWL_WARN(mvm, "Failed sending remove station\n");
2074
2075        return ret;
2076}
2077
2078void iwl_mvm_dealloc_snif_sta(struct iwl_mvm *mvm)
2079{
2080        iwl_mvm_dealloc_int_sta(mvm, &mvm->snif_sta);
2081}
2082
2083void iwl_mvm_del_aux_sta(struct iwl_mvm *mvm)
2084{
2085        lockdep_assert_held(&mvm->mutex);
2086
2087        iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
2088}
2089
2090/*
2091 * Send the add station command for the vif's broadcast station.
2092 * Assumes that the station was already allocated.
2093 *
2094 * @mvm: the mvm component
2095 * @vif: the interface to which the broadcast station is added
2096 * @bsta: the broadcast station to add.
2097 */
2098int iwl_mvm_send_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2099{
2100        struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2101        struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
2102        static const u8 _baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
2103        const u8 *baddr = _baddr;
2104        int queue;
2105        int ret;
2106        unsigned int wdg_timeout =
2107                iwl_mvm_get_wd_timeout(mvm, vif, false, false);
2108        struct iwl_trans_txq_scd_cfg cfg = {
2109                .fifo = IWL_MVM_TX_FIFO_VO,
2110                .sta_id = mvmvif->bcast_sta.sta_id,
2111                .tid = IWL_MAX_TID_COUNT,
2112                .aggregate = false,
2113                .frame_limit = IWL_FRAME_LIMIT,
2114        };
2115
2116        lockdep_assert_held(&mvm->mutex);
2117
2118        if (!iwl_mvm_has_new_tx_api(mvm)) {
2119                if (vif->type == NL80211_IFTYPE_AP ||
2120                    vif->type == NL80211_IFTYPE_ADHOC) {
2121                        queue = mvm->probe_queue;
2122                } else if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
2123                        queue = mvm->p2p_dev_queue;
2124                } else {
2125                        WARN(1, "Missing required TXQ for adding bcast STA\n");
2126                        return -EINVAL;
2127                }
2128
2129                bsta->tfd_queue_msk |= BIT(queue);
2130
2131                iwl_mvm_enable_txq(mvm, NULL, queue, 0, &cfg, wdg_timeout);
2132        }
2133
2134        if (vif->type == NL80211_IFTYPE_ADHOC)
2135                baddr = vif->bss_conf.bssid;
2136
2137        if (WARN_ON_ONCE(bsta->sta_id == IWL_MVM_INVALID_STA))
2138                return -ENOSPC;
2139
2140        ret = iwl_mvm_add_int_sta_common(mvm, bsta, baddr,
2141                                         mvmvif->id, mvmvif->color);
2142        if (ret)
2143                return ret;
2144
2145        /*
2146         * For 22000 firmware and on we cannot add queue to a station unknown
2147         * to firmware so enable queue here - after the station was added
2148         */
2149        if (iwl_mvm_has_new_tx_api(mvm)) {
2150                queue = iwl_mvm_tvqm_enable_txq(mvm, bsta->sta_id,
2151                                                IWL_MAX_TID_COUNT,
2152                                                wdg_timeout);
2153                if (queue < 0) {
2154                        iwl_mvm_rm_sta_common(mvm, bsta->sta_id);
2155                        return queue;
2156                }
2157
2158                if (vif->type == NL80211_IFTYPE_AP ||
2159                    vif->type == NL80211_IFTYPE_ADHOC)
2160                        mvm->probe_queue = queue;
2161                else if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
2162                        mvm->p2p_dev_queue = queue;
2163        }
2164
2165        return 0;
2166}
2167
2168static void iwl_mvm_free_bcast_sta_queues(struct iwl_mvm *mvm,
2169                                          struct ieee80211_vif *vif)
2170{
2171        struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2172        int queue;
2173
2174        lockdep_assert_held(&mvm->mutex);
2175
2176        iwl_mvm_flush_sta(mvm, &mvmvif->bcast_sta, true, 0);
2177
2178        switch (vif->type) {
2179        case NL80211_IFTYPE_AP:
2180        case NL80211_IFTYPE_ADHOC:
2181                queue = mvm->probe_queue;
2182                break;
2183        case NL80211_IFTYPE_P2P_DEVICE:
2184                queue = mvm->p2p_dev_queue;
2185                break;
2186        default:
2187                WARN(1, "Can't free bcast queue on vif type %d\n",
2188                     vif->type);
2189                return;
2190        }
2191
2192        iwl_mvm_disable_txq(mvm, NULL, queue, IWL_MAX_TID_COUNT, 0);
2193        if (iwl_mvm_has_new_tx_api(mvm))
2194                return;
2195
2196        WARN_ON(!(mvmvif->bcast_sta.tfd_queue_msk & BIT(queue)));
2197        mvmvif->bcast_sta.tfd_queue_msk &= ~BIT(queue);
2198}
2199
2200/* Send the FW a request to remove the station from it's internal data
2201 * structures, but DO NOT remove the entry from the local data structures. */
2202int iwl_mvm_send_rm_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2203{
2204        struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2205        int ret;
2206
2207        lockdep_assert_held(&mvm->mutex);
2208
2209        iwl_mvm_free_bcast_sta_queues(mvm, vif);
2210
2211        ret = iwl_mvm_rm_sta_common(mvm, mvmvif->bcast_sta.sta_id);
2212        if (ret)
2213                IWL_WARN(mvm, "Failed sending remove station\n");
2214        return ret;
2215}
2216
2217int iwl_mvm_alloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2218{
2219        struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2220
2221        lockdep_assert_held(&mvm->mutex);
2222
2223        return iwl_mvm_allocate_int_sta(mvm, &mvmvif->bcast_sta, 0,
2224                                        ieee80211_vif_type_p2p(vif),
2225                                        IWL_STA_GENERAL_PURPOSE);
2226}
2227
2228/* Allocate a new station entry for the broadcast station to the given vif,
2229 * and send it to the FW.
2230 * Note that each P2P mac should have its own broadcast station.
2231 *
2232 * @mvm: the mvm component
2233 * @vif: the interface to which the broadcast station is added
2234 * @bsta: the broadcast station to add. */
2235int iwl_mvm_add_p2p_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2236{
2237        struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2238        struct iwl_mvm_int_sta *bsta = &mvmvif->bcast_sta;
2239        int ret;
2240
2241        lockdep_assert_held(&mvm->mutex);
2242
2243        ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
2244        if (ret)
2245                return ret;
2246
2247        ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
2248
2249        if (ret)
2250                iwl_mvm_dealloc_int_sta(mvm, bsta);
2251
2252        return ret;
2253}
2254
2255void iwl_mvm_dealloc_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2256{
2257        struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2258
2259        iwl_mvm_dealloc_int_sta(mvm, &mvmvif->bcast_sta);
2260}
2261
2262/*
2263 * Send the FW a request to remove the station from it's internal data
2264 * structures, and in addition remove it from the local data structure.
2265 */
2266int iwl_mvm_rm_p2p_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2267{
2268        int ret;
2269
2270        lockdep_assert_held(&mvm->mutex);
2271
2272        ret = iwl_mvm_send_rm_bcast_sta(mvm, vif);
2273
2274        iwl_mvm_dealloc_bcast_sta(mvm, vif);
2275
2276        return ret;
2277}
2278
2279/*
2280 * Allocate a new station entry for the multicast station to the given vif,
2281 * and send it to the FW.
2282 * Note that each AP/GO mac should have its own multicast station.
2283 *
2284 * @mvm: the mvm component
2285 * @vif: the interface to which the multicast station is added
2286 */
2287int iwl_mvm_add_mcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2288{
2289        struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2290        struct iwl_mvm_int_sta *msta = &mvmvif->mcast_sta;
2291        static const u8 _maddr[] = {0x03, 0x00, 0x00, 0x00, 0x00, 0x00};
2292        const u8 *maddr = _maddr;
2293        struct iwl_trans_txq_scd_cfg cfg = {
2294                .fifo = vif->type == NL80211_IFTYPE_AP ?
2295                        IWL_MVM_TX_FIFO_MCAST : IWL_MVM_TX_FIFO_BE,
2296                .sta_id = msta->sta_id,
2297                .tid = 0,
2298                .aggregate = false,
2299                .frame_limit = IWL_FRAME_LIMIT,
2300        };
2301        unsigned int timeout = iwl_mvm_get_wd_timeout(mvm, vif, false, false);
2302        int ret;
2303
2304        lockdep_assert_held(&mvm->mutex);
2305
2306        if (WARN_ON(vif->type != NL80211_IFTYPE_AP &&
2307                    vif->type != NL80211_IFTYPE_ADHOC))
2308                return -ENOTSUPP;
2309
2310        /*
2311         * In IBSS, ieee80211_check_queues() sets the cab_queue to be
2312         * invalid, so make sure we use the queue we want.
2313         * Note that this is done here as we want to avoid making DQA
2314         * changes in mac80211 layer.
2315         */
2316        if (vif->type == NL80211_IFTYPE_ADHOC)
2317                mvmvif->cab_queue = IWL_MVM_DQA_GCAST_QUEUE;
2318
2319        /*
2320         * While in previous FWs we had to exclude cab queue from TFD queue
2321         * mask, now it is needed as any other queue.
2322         */
2323        if (!iwl_mvm_has_new_tx_api(mvm) &&
2324            fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE)) {
2325                iwl_mvm_enable_txq(mvm, NULL, mvmvif->cab_queue, 0, &cfg,
2326                                   timeout);
2327                msta->tfd_queue_msk |= BIT(mvmvif->cab_queue);
2328        }
2329        ret = iwl_mvm_add_int_sta_common(mvm, msta, maddr,
2330                                         mvmvif->id, mvmvif->color);
2331        if (ret)
2332                goto err;
2333
2334        /*
2335         * Enable cab queue after the ADD_STA command is sent.
2336         * This is needed for 22000 firmware which won't accept SCD_QUEUE_CFG
2337         * command with unknown station id, and for FW that doesn't support
2338         * station API since the cab queue is not included in the
2339         * tfd_queue_mask.
2340         */
2341        if (iwl_mvm_has_new_tx_api(mvm)) {
2342                int queue = iwl_mvm_tvqm_enable_txq(mvm, msta->sta_id,
2343                                                    0,
2344                                                    timeout);
2345                if (queue < 0) {
2346                        ret = queue;
2347                        goto err;
2348                }
2349                mvmvif->cab_queue = queue;
2350        } else if (!fw_has_api(&mvm->fw->ucode_capa,
2351                               IWL_UCODE_TLV_API_STA_TYPE))
2352                iwl_mvm_enable_txq(mvm, NULL, mvmvif->cab_queue, 0, &cfg,
2353                                   timeout);
2354
2355        return 0;
2356err:
2357        iwl_mvm_dealloc_int_sta(mvm, msta);
2358        return ret;
2359}
2360
2361static int __iwl_mvm_remove_sta_key(struct iwl_mvm *mvm, u8 sta_id,
2362                                    struct ieee80211_key_conf *keyconf,
2363                                    bool mcast)
2364{
2365        union {
2366                struct iwl_mvm_add_sta_key_cmd_v1 cmd_v1;
2367                struct iwl_mvm_add_sta_key_cmd cmd;
2368        } u = {};
2369        bool new_api = fw_has_api(&mvm->fw->ucode_capa,
2370                                  IWL_UCODE_TLV_API_TKIP_MIC_KEYS);
2371        __le16 key_flags;
2372        int ret, size;
2373        u32 status;
2374
2375        /* This is a valid situation for GTK removal */
2376        if (sta_id == IWL_MVM_INVALID_STA)
2377                return 0;
2378
2379        key_flags = cpu_to_le16((keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
2380                                 STA_KEY_FLG_KEYID_MSK);
2381        key_flags |= cpu_to_le16(STA_KEY_FLG_NO_ENC | STA_KEY_FLG_WEP_KEY_MAP);
2382        key_flags |= cpu_to_le16(STA_KEY_NOT_VALID);
2383
2384        if (mcast)
2385                key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
2386
2387        /*
2388         * The fields assigned here are in the same location at the start
2389         * of the command, so we can do this union trick.
2390         */
2391        u.cmd.common.key_flags = key_flags;
2392        u.cmd.common.key_offset = keyconf->hw_key_idx;
2393        u.cmd.common.sta_id = sta_id;
2394
2395        size = new_api ? sizeof(u.cmd) : sizeof(u.cmd_v1);
2396
2397        status = ADD_STA_SUCCESS;
2398        ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, size, &u.cmd,
2399                                          &status);
2400
2401        switch (status) {
2402        case ADD_STA_SUCCESS:
2403                IWL_DEBUG_WEP(mvm, "MODIFY_STA: remove sta key passed\n");
2404                break;
2405        default:
2406                ret = -EIO;
2407                IWL_ERR(mvm, "MODIFY_STA: remove sta key failed\n");
2408                break;
2409        }
2410
2411        return ret;
2412}
2413
2414/*
2415 * Send the FW a request to remove the station from it's internal data
2416 * structures, and in addition remove it from the local data structure.
2417 */
2418int iwl_mvm_rm_mcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2419{
2420        struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2421        int ret;
2422
2423        lockdep_assert_held(&mvm->mutex);
2424
2425        iwl_mvm_flush_sta(mvm, &mvmvif->mcast_sta, true, 0);
2426
2427        iwl_mvm_disable_txq(mvm, NULL, mvmvif->cab_queue, 0, 0);
2428
2429        ret = iwl_mvm_rm_sta_common(mvm, mvmvif->mcast_sta.sta_id);
2430        if (ret)
2431                IWL_WARN(mvm, "Failed sending remove station\n");
2432
2433        return ret;
2434}
2435
2436#define IWL_MAX_RX_BA_SESSIONS 16
2437
2438static void iwl_mvm_sync_rxq_del_ba(struct iwl_mvm *mvm, u8 baid)
2439{
2440        struct iwl_mvm_rss_sync_notif notif = {
2441                .metadata.type = IWL_MVM_RXQ_NOTIF_DEL_BA,
2442                .metadata.sync = 1,
2443                .delba.baid = baid,
2444        };
2445        iwl_mvm_sync_rx_queues_internal(mvm, (void *)&notif, sizeof(notif));
2446};
2447
2448static void iwl_mvm_free_reorder(struct iwl_mvm *mvm,
2449                                 struct iwl_mvm_baid_data *data)
2450{
2451        int i;
2452
2453        iwl_mvm_sync_rxq_del_ba(mvm, data->baid);
2454
2455        for (i = 0; i < mvm->trans->num_rx_queues; i++) {
2456                int j;
2457                struct iwl_mvm_reorder_buffer *reorder_buf =
2458                        &data->reorder_buf[i];
2459                struct iwl_mvm_reorder_buf_entry *entries =
2460                        &data->entries[i * data->entries_per_queue];
2461
2462                spin_lock_bh(&reorder_buf->lock);
2463                if (likely(!reorder_buf->num_stored)) {
2464                        spin_unlock_bh(&reorder_buf->lock);
2465                        continue;
2466                }
2467
2468                /*
2469                 * This shouldn't happen in regular DELBA since the internal
2470                 * delBA notification should trigger a release of all frames in
2471                 * the reorder buffer.
2472                 */
2473                WARN_ON(1);
2474
2475                for (j = 0; j < reorder_buf->buf_size; j++)
2476                        __skb_queue_purge(&entries[j].e.frames);
2477                /*
2478                 * Prevent timer re-arm. This prevents a very far fetched case
2479                 * where we timed out on the notification. There may be prior
2480                 * RX frames pending in the RX queue before the notification
2481                 * that might get processed between now and the actual deletion
2482                 * and we would re-arm the timer although we are deleting the
2483                 * reorder buffer.
2484                 */
2485                reorder_buf->removed = true;
2486                spin_unlock_bh(&reorder_buf->lock);
2487                del_timer_sync(&reorder_buf->reorder_timer);
2488        }
2489}
2490
2491static void iwl_mvm_init_reorder_buffer(struct iwl_mvm *mvm,
2492                                        struct iwl_mvm_baid_data *data,
2493                                        u16 ssn, u16 buf_size)
2494{
2495        int i;
2496
2497        for (i = 0; i < mvm->trans->num_rx_queues; i++) {
2498                struct iwl_mvm_reorder_buffer *reorder_buf =
2499                        &data->reorder_buf[i];
2500                struct iwl_mvm_reorder_buf_entry *entries =
2501                        &data->entries[i * data->entries_per_queue];
2502                int j;
2503
2504                reorder_buf->num_stored = 0;
2505                reorder_buf->head_sn = ssn;
2506                reorder_buf->buf_size = buf_size;
2507                /* rx reorder timer */
2508                timer_setup(&reorder_buf->reorder_timer,
2509                            iwl_mvm_reorder_timer_expired, 0);
2510                spin_lock_init(&reorder_buf->lock);
2511                reorder_buf->mvm = mvm;
2512                reorder_buf->queue = i;
2513                reorder_buf->valid = false;
2514                for (j = 0; j < reorder_buf->buf_size; j++)
2515                        __skb_queue_head_init(&entries[j].e.frames);
2516        }
2517}
2518
2519int iwl_mvm_sta_rx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
2520                       int tid, u16 ssn, bool start, u16 buf_size, u16 timeout)
2521{
2522        struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2523        struct iwl_mvm_add_sta_cmd cmd = {};
2524        struct iwl_mvm_baid_data *baid_data = NULL;
2525        int ret;
2526        u32 status;
2527
2528        lockdep_assert_held(&mvm->mutex);
2529
2530        if (start && mvm->rx_ba_sessions >= IWL_MAX_RX_BA_SESSIONS) {
2531                IWL_WARN(mvm, "Not enough RX BA SESSIONS\n");
2532                return -ENOSPC;
2533        }
2534
2535        if (iwl_mvm_has_new_rx_api(mvm) && start) {
2536                u16 reorder_buf_size = buf_size * sizeof(baid_data->entries[0]);
2537
2538                /* sparse doesn't like the __align() so don't check */
2539#ifndef __CHECKER__
2540                /*
2541                 * The division below will be OK if either the cache line size
2542                 * can be divided by the entry size (ALIGN will round up) or if
2543                 * if the entry size can be divided by the cache line size, in
2544                 * which case the ALIGN() will do nothing.
2545                 */
2546                BUILD_BUG_ON(SMP_CACHE_BYTES % sizeof(baid_data->entries[0]) &&
2547                             sizeof(baid_data->entries[0]) % SMP_CACHE_BYTES);
2548#endif
2549
2550                /*
2551                 * Upward align the reorder buffer size to fill an entire cache
2552                 * line for each queue, to avoid sharing cache lines between
2553                 * different queues.
2554                 */
2555                reorder_buf_size = ALIGN(reorder_buf_size, SMP_CACHE_BYTES);
2556
2557                /*
2558                 * Allocate here so if allocation fails we can bail out early
2559                 * before starting the BA session in the firmware
2560                 */
2561                baid_data = kzalloc(sizeof(*baid_data) +
2562                                    mvm->trans->num_rx_queues *
2563                                    reorder_buf_size,
2564                                    GFP_KERNEL);
2565                if (!baid_data)
2566                        return -ENOMEM;
2567
2568                /*
2569                 * This division is why we need the above BUILD_BUG_ON(),
2570                 * if that doesn't hold then this will not be right.
2571                 */
2572                baid_data->entries_per_queue =
2573                        reorder_buf_size / sizeof(baid_data->entries[0]);
2574        }
2575
2576        cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
2577        cmd.sta_id = mvm_sta->sta_id;
2578        cmd.add_modify = STA_MODE_MODIFY;
2579        if (start) {
2580                cmd.add_immediate_ba_tid = (u8) tid;
2581                cmd.add_immediate_ba_ssn = cpu_to_le16(ssn);
2582                cmd.rx_ba_window = cpu_to_le16(buf_size);
2583        } else {
2584                cmd.remove_immediate_ba_tid = (u8) tid;
2585        }
2586        cmd.modify_mask = start ? STA_MODIFY_ADD_BA_TID :
2587                                  STA_MODIFY_REMOVE_BA_TID;
2588
2589        status = ADD_STA_SUCCESS;
2590        ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
2591                                          iwl_mvm_add_sta_cmd_size(mvm),
2592                                          &cmd, &status);
2593        if (ret)
2594                goto out_free;
2595
2596        switch (status & IWL_ADD_STA_STATUS_MASK) {
2597        case ADD_STA_SUCCESS:
2598                IWL_DEBUG_HT(mvm, "RX BA Session %sed in fw\n",
2599                             start ? "start" : "stopp");
2600                break;
2601        case ADD_STA_IMMEDIATE_BA_FAILURE:
2602                IWL_WARN(mvm, "RX BA Session refused by fw\n");
2603                ret = -ENOSPC;
2604                break;
2605        default:
2606                ret = -EIO;
2607                IWL_ERR(mvm, "RX BA Session failed %sing, status 0x%x\n",
2608                        start ? "start" : "stopp", status);
2609                break;
2610        }
2611
2612        if (ret)
2613                goto out_free;
2614
2615        if (start) {
2616                u8 baid;
2617
2618                mvm->rx_ba_sessions++;
2619
2620                if (!iwl_mvm_has_new_rx_api(mvm))
2621                        return 0;
2622
2623                if (WARN_ON(!(status & IWL_ADD_STA_BAID_VALID_MASK))) {
2624                        ret = -EINVAL;
2625                        goto out_free;
2626                }
2627                baid = (u8)((status & IWL_ADD_STA_BAID_MASK) >>
2628                            IWL_ADD_STA_BAID_SHIFT);
2629                baid_data->baid = baid;
2630                baid_data->timeout = timeout;
2631                baid_data->last_rx = jiffies;
2632                baid_data->rcu_ptr = &mvm->baid_map[baid];
2633                timer_setup(&baid_data->session_timer,
2634                            iwl_mvm_rx_agg_session_expired, 0);
2635                baid_data->mvm = mvm;
2636                baid_data->tid = tid;
2637                baid_data->sta_id = mvm_sta->sta_id;
2638
2639                mvm_sta->tid_to_baid[tid] = baid;
2640                if (timeout)
2641                        mod_timer(&baid_data->session_timer,
2642                                  TU_TO_EXP_TIME(timeout * 2));
2643
2644                iwl_mvm_init_reorder_buffer(mvm, baid_data, ssn, buf_size);
2645                /*
2646                 * protect the BA data with RCU to cover a case where our
2647                 * internal RX sync mechanism will timeout (not that it's
2648                 * supposed to happen) and we will free the session data while
2649                 * RX is being processed in parallel
2650                 */
2651                IWL_DEBUG_HT(mvm, "Sta %d(%d) is assigned to BAID %d\n",
2652                             mvm_sta->sta_id, tid, baid);
2653                WARN_ON(rcu_access_pointer(mvm->baid_map[baid]));
2654                rcu_assign_pointer(mvm->baid_map[baid], baid_data);
2655        } else  {
2656                u8 baid = mvm_sta->tid_to_baid[tid];
2657
2658                if (mvm->rx_ba_sessions > 0)
2659                        /* check that restart flow didn't zero the counter */
2660                        mvm->rx_ba_sessions--;
2661                if (!iwl_mvm_has_new_rx_api(mvm))
2662                        return 0;
2663
2664                if (WARN_ON(baid == IWL_RX_REORDER_DATA_INVALID_BAID))
2665                        return -EINVAL;
2666
2667                baid_data = rcu_access_pointer(mvm->baid_map[baid]);
2668                if (WARN_ON(!baid_data))
2669                        return -EINVAL;
2670
2671                /* synchronize all rx queues so we can safely delete */
2672                iwl_mvm_free_reorder(mvm, baid_data);
2673                del_timer_sync(&baid_data->session_timer);
2674                RCU_INIT_POINTER(mvm->baid_map[baid], NULL);
2675                kfree_rcu(baid_data, rcu_head);
2676                IWL_DEBUG_HT(mvm, "BAID %d is free\n", baid);
2677        }
2678        return 0;
2679
2680out_free:
2681        kfree(baid_data);
2682        return ret;
2683}
2684
2685int iwl_mvm_sta_tx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
2686                       int tid, u8 queue, bool start)
2687{
2688        struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2689        struct iwl_mvm_add_sta_cmd cmd = {};
2690        int ret;
2691        u32 status;
2692
2693        lockdep_assert_held(&mvm->mutex);
2694
2695        if (start) {
2696                mvm_sta->tfd_queue_msk |= BIT(queue);
2697                mvm_sta->tid_disable_agg &= ~BIT(tid);
2698        } else {
2699                /* In DQA-mode the queue isn't removed on agg termination */
2700                mvm_sta->tid_disable_agg |= BIT(tid);
2701        }
2702
2703        cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
2704        cmd.sta_id = mvm_sta->sta_id;
2705        cmd.add_modify = STA_MODE_MODIFY;
2706        if (!iwl_mvm_has_new_tx_api(mvm))
2707                cmd.modify_mask = STA_MODIFY_QUEUES;
2708        cmd.modify_mask |= STA_MODIFY_TID_DISABLE_TX;
2709        cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
2710        cmd.tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg);
2711
2712        status = ADD_STA_SUCCESS;
2713        ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA,
2714                                          iwl_mvm_add_sta_cmd_size(mvm),
2715                                          &cmd, &status);
2716        if (ret)
2717                return ret;
2718
2719        switch (status & IWL_ADD_STA_STATUS_MASK) {
2720        case ADD_STA_SUCCESS:
2721                break;
2722        default:
2723                ret = -EIO;
2724                IWL_ERR(mvm, "TX BA Session failed %sing, status 0x%x\n",
2725                        start ? "start" : "stopp", status);
2726                break;
2727        }
2728
2729        return ret;
2730}
2731
2732const u8 tid_to_mac80211_ac[] = {
2733        IEEE80211_AC_BE,
2734        IEEE80211_AC_BK,
2735        IEEE80211_AC_BK,
2736        IEEE80211_AC_BE,
2737        IEEE80211_AC_VI,
2738        IEEE80211_AC_VI,
2739        IEEE80211_AC_VO,
2740        IEEE80211_AC_VO,
2741        IEEE80211_AC_VO, /* We treat MGMT as TID 8, which is set as AC_VO */
2742};
2743
2744static const u8 tid_to_ucode_ac[] = {
2745        AC_BE,
2746        AC_BK,
2747        AC_BK,
2748        AC_BE,
2749        AC_VI,
2750        AC_VI,
2751        AC_VO,
2752        AC_VO,
2753};
2754
2755int iwl_mvm_sta_tx_agg_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2756                             struct ieee80211_sta *sta, u16 tid, u16 *ssn)
2757{
2758        struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
2759        struct iwl_mvm_tid_data *tid_data;
2760        u16 normalized_ssn;
2761        u16 txq_id;
2762        int ret;
2763
2764        if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
2765                return -EINVAL;
2766
2767        if (mvmsta->tid_data[tid].state != IWL_AGG_QUEUED &&
2768            mvmsta->tid_data[tid].state != IWL_AGG_OFF) {
2769                IWL_ERR(mvm,
2770                        "Start AGG when state is not IWL_AGG_QUEUED or IWL_AGG_OFF %d!\n",
2771                        mvmsta->tid_data[tid].state);
2772                return -ENXIO;
2773        }
2774
2775        lockdep_assert_held(&mvm->mutex);
2776
2777        if (mvmsta->tid_data[tid].txq_id == IWL_MVM_INVALID_QUEUE &&
2778            iwl_mvm_has_new_tx_api(mvm)) {
2779                u8 ac = tid_to_mac80211_ac[tid];
2780
2781                ret = iwl_mvm_sta_alloc_queue_tvqm(mvm, sta, ac, tid);
2782                if (ret)
2783                        return ret;
2784        }
2785
2786        spin_lock_bh(&mvmsta->lock);
2787
2788        /*
2789         * Note the possible cases:
2790         *  1. An enabled TXQ - TXQ needs to become agg'ed
2791         *  2. The TXQ hasn't yet been enabled, so find a free one and mark
2792         *      it as reserved
2793         */
2794        txq_id = mvmsta->tid_data[tid].txq_id;
2795        if (txq_id == IWL_MVM_INVALID_QUEUE) {
2796                ret = iwl_mvm_find_free_queue(mvm, mvmsta->sta_id,
2797                                              IWL_MVM_DQA_MIN_DATA_QUEUE,
2798                                              IWL_MVM_DQA_MAX_DATA_QUEUE);
2799                if (ret < 0) {
2800                        IWL_ERR(mvm, "Failed to allocate agg queue\n");
2801                        goto out;
2802                }
2803
2804                txq_id = ret;
2805
2806                /* TXQ hasn't yet been enabled, so mark it only as reserved */
2807                mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_RESERVED;
2808        } else if (WARN_ON(txq_id >= IWL_MAX_HW_QUEUES)) {
2809                ret = -ENXIO;
2810                IWL_ERR(mvm, "tid_id %d out of range (0, %d)!\n",
2811                        tid, IWL_MAX_HW_QUEUES - 1);
2812                goto out;
2813
2814        } else if (unlikely(mvm->queue_info[txq_id].status ==
2815                            IWL_MVM_QUEUE_SHARED)) {
2816                ret = -ENXIO;
2817                IWL_DEBUG_TX_QUEUES(mvm,
2818                                    "Can't start tid %d agg on shared queue!\n",
2819                                    tid);
2820                goto out;
2821        }
2822
2823        IWL_DEBUG_TX_QUEUES(mvm,
2824                            "AGG for tid %d will be on queue #%d\n",
2825                            tid, txq_id);
2826
2827        tid_data = &mvmsta->tid_data[tid];
2828        tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
2829        tid_data->txq_id = txq_id;
2830        *ssn = tid_data->ssn;
2831
2832        IWL_DEBUG_TX_QUEUES(mvm,
2833                            "Start AGG: sta %d tid %d queue %d - ssn = %d, next_recl = %d\n",
2834                            mvmsta->sta_id, tid, txq_id, tid_data->ssn,
2835                            tid_data->next_reclaimed);
2836
2837        /*
2838         * In 22000 HW, the next_reclaimed index is only 8 bit, so we'll need
2839         * to align the wrap around of ssn so we compare relevant values.
2840         */
2841        normalized_ssn = tid_data->ssn;
2842        if (mvm->trans->trans_cfg->gen2)
2843                normalized_ssn &= 0xff;
2844
2845        if (normalized_ssn == tid_data->next_reclaimed) {
2846                tid_data->state = IWL_AGG_STARTING;
2847                ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
2848        } else {
2849                tid_data->state = IWL_EMPTYING_HW_QUEUE_ADDBA;
2850        }
2851
2852        ret = 0;
2853
2854out:
2855        spin_unlock_bh(&mvmsta->lock);
2856
2857        return ret;
2858}
2859
2860int iwl_mvm_sta_tx_agg_oper(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2861                            struct ieee80211_sta *sta, u16 tid, u16 buf_size,
2862                            bool amsdu)
2863{
2864        struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
2865        struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
2866        unsigned int wdg_timeout =
2867                iwl_mvm_get_wd_timeout(mvm, vif, sta->tdls, false);
2868        int queue, ret;
2869        bool alloc_queue = true;
2870        enum iwl_mvm_queue_status queue_status;
2871        u16 ssn;
2872
2873        struct iwl_trans_txq_scd_cfg cfg = {
2874                .sta_id = mvmsta->sta_id,
2875                .tid = tid,
2876                .frame_limit = buf_size,
2877                .aggregate = true,
2878        };
2879
2880        /*
2881         * When FW supports TLC_OFFLOAD, it also implements Tx aggregation
2882         * manager, so this function should never be called in this case.
2883         */
2884        if (WARN_ON_ONCE(iwl_mvm_has_tlc_offload(mvm)))
2885                return -EINVAL;
2886
2887        BUILD_BUG_ON((sizeof(mvmsta->agg_tids) * BITS_PER_BYTE)
2888                     != IWL_MAX_TID_COUNT);
2889
2890        spin_lock_bh(&mvmsta->lock);
2891        ssn = tid_data->ssn;
2892        queue = tid_data->txq_id;
2893        tid_data->state = IWL_AGG_ON;
2894        mvmsta->agg_tids |= BIT(tid);
2895        tid_data->ssn = 0xffff;
2896        tid_data->amsdu_in_ampdu_allowed = amsdu;
2897        spin_unlock_bh(&mvmsta->lock);
2898
2899        if (iwl_mvm_has_new_tx_api(mvm)) {
2900                /*
2901                 * If there is no queue for this tid, iwl_mvm_sta_tx_agg_start()
2902                 * would have failed, so if we are here there is no need to
2903                 * allocate a queue.
2904                 * However, if aggregation size is different than the default
2905                 * size, the scheduler should be reconfigured.
2906                 * We cannot do this with the new TX API, so return unsupported
2907                 * for now, until it will be offloaded to firmware..
2908                 * Note that if SCD default value changes - this condition
2909                 * should be updated as well.
2910                 */
2911                if (buf_size < IWL_FRAME_LIMIT)
2912                        return -ENOTSUPP;
2913
2914                ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
2915                if (ret)
2916                        return -EIO;
2917                goto out;
2918        }
2919
2920        cfg.fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
2921
2922        queue_status = mvm->queue_info[queue].status;
2923
2924        /* Maybe there is no need to even alloc a queue... */
2925        if (mvm->queue_info[queue].status == IWL_MVM_QUEUE_READY)
2926                alloc_queue = false;
2927
2928        /*
2929         * Only reconfig the SCD for the queue if the window size has
2930         * changed from current (become smaller)
2931         */
2932        if (!alloc_queue && buf_size < IWL_FRAME_LIMIT) {
2933                /*
2934                 * If reconfiguring an existing queue, it first must be
2935                 * drained
2936                 */
2937                ret = iwl_trans_wait_tx_queues_empty(mvm->trans,
2938                                                     BIT(queue));
2939                if (ret) {
2940                        IWL_ERR(mvm,
2941                                "Error draining queue before reconfig\n");
2942                        return ret;
2943                }
2944
2945                ret = iwl_mvm_reconfig_scd(mvm, queue, cfg.fifo,
2946                                           mvmsta->sta_id, tid,
2947                                           buf_size, ssn);
2948                if (ret) {
2949                        IWL_ERR(mvm,
2950                                "Error reconfiguring TXQ #%d\n", queue);
2951                        return ret;
2952                }
2953        }
2954
2955        if (alloc_queue)
2956                iwl_mvm_enable_txq(mvm, sta, queue, ssn,
2957                                   &cfg, wdg_timeout);
2958
2959        /* Send ADD_STA command to enable aggs only if the queue isn't shared */
2960        if (queue_status != IWL_MVM_QUEUE_SHARED) {
2961                ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
2962                if (ret)
2963                        return -EIO;
2964        }
2965
2966        /* No need to mark as reserved */
2967        mvm->queue_info[queue].status = IWL_MVM_QUEUE_READY;
2968
2969out:
2970        /*
2971         * Even though in theory the peer could have different
2972         * aggregation reorder buffer sizes for different sessions,
2973         * our ucode doesn't allow for that and has a global limit
2974         * for each station. Therefore, use the minimum of all the
2975         * aggregation sessions and our default value.
2976         */
2977        mvmsta->max_agg_bufsize =
2978                min(mvmsta->max_agg_bufsize, buf_size);
2979        mvmsta->lq_sta.rs_drv.lq.agg_frame_cnt_limit = mvmsta->max_agg_bufsize;
2980
2981        IWL_DEBUG_HT(mvm, "Tx aggregation enabled on ra = %pM tid = %d\n",
2982                     sta->addr, tid);
2983
2984        return iwl_mvm_send_lq_cmd(mvm, &mvmsta->lq_sta.rs_drv.lq);
2985}
2986
2987static void iwl_mvm_unreserve_agg_queue(struct iwl_mvm *mvm,
2988                                        struct iwl_mvm_sta *mvmsta,
2989                                        struct iwl_mvm_tid_data *tid_data)
2990{
2991        u16 txq_id = tid_data->txq_id;
2992
2993        lockdep_assert_held(&mvm->mutex);
2994
2995        if (iwl_mvm_has_new_tx_api(mvm))
2996                return;
2997
2998        /*
2999         * The TXQ is marked as reserved only if no traffic came through yet
3000         * This means no traffic has been sent on this TID (agg'd or not), so
3001         * we no longer have use for the queue. Since it hasn't even been
3002         * allocated through iwl_mvm_enable_txq, so we can just mark it back as
3003         * free.
3004         */
3005        if (mvm->queue_info[txq_id].status == IWL_MVM_QUEUE_RESERVED) {
3006                mvm->queue_info[txq_id].status = IWL_MVM_QUEUE_FREE;
3007                tid_data->txq_id = IWL_MVM_INVALID_QUEUE;
3008        }
3009}
3010
3011int iwl_mvm_sta_tx_agg_stop(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
3012                            struct ieee80211_sta *sta, u16 tid)
3013{
3014        struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3015        struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
3016        u16 txq_id;
3017        int err;
3018
3019        /*
3020         * If mac80211 is cleaning its state, then say that we finished since
3021         * our state has been cleared anyway.
3022         */
3023        if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
3024                ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
3025                return 0;
3026        }
3027
3028        spin_lock_bh(&mvmsta->lock);
3029
3030        txq_id = tid_data->txq_id;
3031
3032        IWL_DEBUG_TX_QUEUES(mvm, "Stop AGG: sta %d tid %d q %d state %d\n",
3033                            mvmsta->sta_id, tid, txq_id, tid_data->state);
3034
3035        mvmsta->agg_tids &= ~BIT(tid);
3036
3037        iwl_mvm_unreserve_agg_queue(mvm, mvmsta, tid_data);
3038
3039        switch (tid_data->state) {
3040        case IWL_AGG_ON:
3041                tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
3042
3043                IWL_DEBUG_TX_QUEUES(mvm,
3044                                    "ssn = %d, next_recl = %d\n",
3045                                    tid_data->ssn, tid_data->next_reclaimed);
3046
3047                tid_data->ssn = 0xffff;
3048                tid_data->state = IWL_AGG_OFF;
3049                spin_unlock_bh(&mvmsta->lock);
3050
3051                ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
3052
3053                iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
3054                return 0;
3055        case IWL_AGG_STARTING:
3056        case IWL_EMPTYING_HW_QUEUE_ADDBA:
3057                /*
3058                 * The agg session has been stopped before it was set up. This
3059                 * can happen when the AddBA timer times out for example.
3060                 */
3061
3062                /* No barriers since we are under mutex */
3063                lockdep_assert_held(&mvm->mutex);
3064
3065                ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
3066                tid_data->state = IWL_AGG_OFF;
3067                err = 0;
3068                break;
3069        default:
3070                IWL_ERR(mvm,
3071                        "Stopping AGG while state not ON or starting for %d on %d (%d)\n",
3072                        mvmsta->sta_id, tid, tid_data->state);
3073                IWL_ERR(mvm,
3074                        "\ttid_data->txq_id = %d\n", tid_data->txq_id);
3075                err = -EINVAL;
3076        }
3077
3078        spin_unlock_bh(&mvmsta->lock);
3079
3080        return err;
3081}
3082
3083int iwl_mvm_sta_tx_agg_flush(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
3084                            struct ieee80211_sta *sta, u16 tid)
3085{
3086        struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3087        struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
3088        u16 txq_id;
3089        enum iwl_mvm_agg_state old_state;
3090
3091        /*
3092         * First set the agg state to OFF to avoid calling
3093         * ieee80211_stop_tx_ba_cb in iwl_mvm_check_ratid_empty.
3094         */
3095        spin_lock_bh(&mvmsta->lock);
3096        txq_id = tid_data->txq_id;
3097        IWL_DEBUG_TX_QUEUES(mvm, "Flush AGG: sta %d tid %d q %d state %d\n",
3098                            mvmsta->sta_id, tid, txq_id, tid_data->state);
3099        old_state = tid_data->state;
3100        tid_data->state = IWL_AGG_OFF;
3101        mvmsta->agg_tids &= ~BIT(tid);
3102        spin_unlock_bh(&mvmsta->lock);
3103
3104        iwl_mvm_unreserve_agg_queue(mvm, mvmsta, tid_data);
3105
3106        if (old_state >= IWL_AGG_ON) {
3107                iwl_mvm_drain_sta(mvm, mvmsta, true);
3108
3109                if (iwl_mvm_has_new_tx_api(mvm)) {
3110                        if (iwl_mvm_flush_sta_tids(mvm, mvmsta->sta_id,
3111                                                   BIT(tid), 0))
3112                                IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
3113                        iwl_trans_wait_txq_empty(mvm->trans, txq_id);
3114                } else {
3115                        if (iwl_mvm_flush_tx_path(mvm, BIT(txq_id), 0))
3116                                IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
3117                        iwl_trans_wait_tx_queues_empty(mvm->trans, BIT(txq_id));
3118                }
3119
3120                iwl_mvm_drain_sta(mvm, mvmsta, false);
3121
3122                iwl_mvm_sta_tx_agg(mvm, sta, tid, txq_id, false);
3123        }
3124
3125        return 0;
3126}
3127
3128static int iwl_mvm_set_fw_key_idx(struct iwl_mvm *mvm)
3129{
3130        int i, max = -1, max_offs = -1;
3131
3132        lockdep_assert_held(&mvm->mutex);
3133
3134        /* Pick the unused key offset with the highest 'deleted'
3135         * counter. Every time a key is deleted, all the counters
3136         * are incremented and the one that was just deleted is
3137         * reset to zero. Thus, the highest counter is the one
3138         * that was deleted longest ago. Pick that one.
3139         */
3140        for (i = 0; i < STA_KEY_MAX_NUM; i++) {
3141                if (test_bit(i, mvm->fw_key_table))
3142                        continue;
3143                if (mvm->fw_key_deleted[i] > max) {
3144                        max = mvm->fw_key_deleted[i];
3145                        max_offs = i;
3146                }
3147        }
3148
3149        if (max_offs < 0)
3150                return STA_KEY_IDX_INVALID;
3151
3152        return max_offs;
3153}
3154
3155static struct iwl_mvm_sta *iwl_mvm_get_key_sta(struct iwl_mvm *mvm,
3156                                               struct ieee80211_vif *vif,
3157                                               struct ieee80211_sta *sta)
3158{
3159        struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3160
3161        if (sta)
3162                return iwl_mvm_sta_from_mac80211(sta);
3163
3164        /*
3165         * The device expects GTKs for station interfaces to be
3166         * installed as GTKs for the AP station. If we have no
3167         * station ID, then use AP's station ID.
3168         */
3169        if (vif->type == NL80211_IFTYPE_STATION &&
3170            mvmvif->ap_sta_id != IWL_MVM_INVALID_STA) {
3171                u8 sta_id = mvmvif->ap_sta_id;
3172
3173                sta = rcu_dereference_check(mvm->fw_id_to_mac_id[sta_id],
3174                                            lockdep_is_held(&mvm->mutex));
3175
3176                /*
3177                 * It is possible that the 'sta' parameter is NULL,
3178                 * for example when a GTK is removed - the sta_id will then
3179                 * be the AP ID, and no station was passed by mac80211.
3180                 */
3181                if (IS_ERR_OR_NULL(sta))
3182                        return NULL;
3183
3184                return iwl_mvm_sta_from_mac80211(sta);
3185        }
3186
3187        return NULL;
3188}
3189
3190static int iwl_mvm_send_sta_key(struct iwl_mvm *mvm,
3191                                u32 sta_id,
3192                                struct ieee80211_key_conf *key, bool mcast,
3193                                u32 tkip_iv32, u16 *tkip_p1k, u32 cmd_flags,
3194                                u8 key_offset, bool mfp)
3195{
3196        union {
3197                struct iwl_mvm_add_sta_key_cmd_v1 cmd_v1;
3198                struct iwl_mvm_add_sta_key_cmd cmd;
3199        } u = {};
3200        __le16 key_flags;
3201        int ret;
3202        u32 status;
3203        u16 keyidx;
3204        u64 pn = 0;
3205        int i, size;
3206        bool new_api = fw_has_api(&mvm->fw->ucode_capa,
3207                                  IWL_UCODE_TLV_API_TKIP_MIC_KEYS);
3208
3209        if (sta_id == IWL_MVM_INVALID_STA)
3210                return -EINVAL;
3211
3212        keyidx = (key->keyidx << STA_KEY_FLG_KEYID_POS) &
3213                 STA_KEY_FLG_KEYID_MSK;
3214        key_flags = cpu_to_le16(keyidx);
3215        key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_KEY_MAP);
3216
3217        switch (key->cipher) {
3218        case WLAN_CIPHER_SUITE_TKIP:
3219                key_flags |= cpu_to_le16(STA_KEY_FLG_TKIP);
3220                if (new_api) {
3221                        memcpy((void *)&u.cmd.tx_mic_key,
3222                               &key->key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY],
3223                               IWL_MIC_KEY_SIZE);
3224
3225                        memcpy((void *)&u.cmd.rx_mic_key,
3226                               &key->key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY],
3227                               IWL_MIC_KEY_SIZE);
3228                        pn = atomic64_read(&key->tx_pn);
3229
3230                } else {
3231                        u.cmd_v1.tkip_rx_tsc_byte2 = tkip_iv32;
3232                        for (i = 0; i < 5; i++)
3233                                u.cmd_v1.tkip_rx_ttak[i] =
3234                                        cpu_to_le16(tkip_p1k[i]);
3235                }
3236                memcpy(u.cmd.common.key, key->key, key->keylen);
3237                break;
3238        case WLAN_CIPHER_SUITE_CCMP:
3239                key_flags |= cpu_to_le16(STA_KEY_FLG_CCM);
3240                memcpy(u.cmd.common.key, key->key, key->keylen);
3241                if (new_api)
3242                        pn = atomic64_read(&key->tx_pn);
3243                break;
3244        case WLAN_CIPHER_SUITE_WEP104:
3245                key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_13BYTES);
3246                /* fall through */
3247        case WLAN_CIPHER_SUITE_WEP40:
3248                key_flags |= cpu_to_le16(STA_KEY_FLG_WEP);
3249                memcpy(u.cmd.common.key + 3, key->key, key->keylen);
3250                break;
3251        case WLAN_CIPHER_SUITE_GCMP_256:
3252                key_flags |= cpu_to_le16(STA_KEY_FLG_KEY_32BYTES);
3253                /* fall through */
3254        case WLAN_CIPHER_SUITE_GCMP:
3255                key_flags |= cpu_to_le16(STA_KEY_FLG_GCMP);
3256                memcpy(u.cmd.common.key, key->key, key->keylen);
3257                if (new_api)
3258                        pn = atomic64_read(&key->tx_pn);
3259                break;
3260        default:
3261                key_flags |= cpu_to_le16(STA_KEY_FLG_EXT);
3262                memcpy(u.cmd.common.key, key->key, key->keylen);
3263        }
3264
3265        if (mcast)
3266                key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
3267        if (mfp)
3268                key_flags |= cpu_to_le16(STA_KEY_MFP);
3269
3270        u.cmd.common.key_offset = key_offset;
3271        u.cmd.common.key_flags = key_flags;
3272        u.cmd.common.sta_id = sta_id;
3273
3274        if (new_api) {
3275                u.cmd.transmit_seq_cnt = cpu_to_le64(pn);
3276                size = sizeof(u.cmd);
3277        } else {
3278                size = sizeof(u.cmd_v1);
3279        }
3280
3281        status = ADD_STA_SUCCESS;
3282        if (cmd_flags & CMD_ASYNC)
3283                ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA_KEY, CMD_ASYNC, size,
3284                                           &u.cmd);
3285        else
3286                ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY, size,
3287                                                  &u.cmd, &status);
3288
3289        switch (status) {
3290        case ADD_STA_SUCCESS:
3291                IWL_DEBUG_WEP(mvm, "MODIFY_STA: set dynamic key passed\n");
3292                break;
3293        default:
3294                ret = -EIO;
3295                IWL_ERR(mvm, "MODIFY_STA: set dynamic key failed\n");
3296                break;
3297        }
3298
3299        return ret;
3300}
3301
3302static int iwl_mvm_send_sta_igtk(struct iwl_mvm *mvm,
3303                                 struct ieee80211_key_conf *keyconf,
3304                                 u8 sta_id, bool remove_key)
3305{
3306        struct iwl_mvm_mgmt_mcast_key_cmd igtk_cmd = {};
3307
3308        /* verify the key details match the required command's expectations */
3309        if (WARN_ON((keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) ||
3310                    (keyconf->keyidx != 4 && keyconf->keyidx != 5) ||
3311                    (keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC &&
3312                     keyconf->cipher != WLAN_CIPHER_SUITE_BIP_GMAC_128 &&
3313                     keyconf->cipher != WLAN_CIPHER_SUITE_BIP_GMAC_256)))
3314                return -EINVAL;
3315
3316        if (WARN_ON(!iwl_mvm_has_new_rx_api(mvm) &&
3317                    keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC))
3318                return -EINVAL;
3319
3320        igtk_cmd.key_id = cpu_to_le32(keyconf->keyidx);
3321        igtk_cmd.sta_id = cpu_to_le32(sta_id);
3322
3323        if (remove_key) {
3324                igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_NOT_VALID);
3325        } else {
3326                struct ieee80211_key_seq seq;
3327                const u8 *pn;
3328
3329                switch (keyconf->cipher) {
3330                case WLAN_CIPHER_SUITE_AES_CMAC:
3331                        igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_FLG_CCM);
3332                        break;
3333                case WLAN_CIPHER_SUITE_BIP_GMAC_128:
3334                case WLAN_CIPHER_SUITE_BIP_GMAC_256:
3335                        igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_FLG_GCMP);
3336                        break;
3337                default:
3338                        return -EINVAL;
3339                }
3340
3341                memcpy(igtk_cmd.igtk, keyconf->key, keyconf->keylen);
3342                if (keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)
3343                        igtk_cmd.ctrl_flags |=
3344                                cpu_to_le32(STA_KEY_FLG_KEY_32BYTES);
3345                ieee80211_get_key_rx_seq(keyconf, 0, &seq);
3346                pn = seq.aes_cmac.pn;
3347                igtk_cmd.receive_seq_cnt = cpu_to_le64(((u64) pn[5] << 0) |
3348                                                       ((u64) pn[4] << 8) |
3349                                                       ((u64) pn[3] << 16) |
3350                                                       ((u64) pn[2] << 24) |
3351                                                       ((u64) pn[1] << 32) |
3352                                                       ((u64) pn[0] << 40));
3353        }
3354
3355        IWL_DEBUG_INFO(mvm, "%s igtk for sta %u\n",
3356                       remove_key ? "removing" : "installing",
3357                       igtk_cmd.sta_id);
3358
3359        if (!iwl_mvm_has_new_rx_api(mvm)) {
3360                struct iwl_mvm_mgmt_mcast_key_cmd_v1 igtk_cmd_v1 = {
3361                        .ctrl_flags = igtk_cmd.ctrl_flags,
3362                        .key_id = igtk_cmd.key_id,
3363                        .sta_id = igtk_cmd.sta_id,
3364                        .receive_seq_cnt = igtk_cmd.receive_seq_cnt
3365                };
3366
3367                memcpy(igtk_cmd_v1.igtk, igtk_cmd.igtk,
3368                       ARRAY_SIZE(igtk_cmd_v1.igtk));
3369                return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
3370                                            sizeof(igtk_cmd_v1), &igtk_cmd_v1);
3371        }
3372        return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, 0,
3373                                    sizeof(igtk_cmd), &igtk_cmd);
3374}
3375
3376
3377static inline u8 *iwl_mvm_get_mac_addr(struct iwl_mvm *mvm,
3378                                       struct ieee80211_vif *vif,
3379                                       struct ieee80211_sta *sta)
3380{
3381        struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3382
3383        if (sta)
3384                return sta->addr;
3385
3386        if (vif->type == NL80211_IFTYPE_STATION &&
3387            mvmvif->ap_sta_id != IWL_MVM_INVALID_STA) {
3388                u8 sta_id = mvmvif->ap_sta_id;
3389                sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
3390                                                lockdep_is_held(&mvm->mutex));
3391                return sta->addr;
3392        }
3393
3394
3395        return NULL;
3396}
3397
3398static int __iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
3399                                 struct ieee80211_vif *vif,
3400                                 struct ieee80211_sta *sta,
3401                                 struct ieee80211_key_conf *keyconf,
3402                                 u8 key_offset,
3403                                 bool mcast)
3404{
3405        int ret;
3406        const u8 *addr;
3407        struct ieee80211_key_seq seq;
3408        u16 p1k[5];
3409        u32 sta_id;
3410        bool mfp = false;
3411
3412        if (sta) {
3413                struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3414
3415                sta_id = mvm_sta->sta_id;
3416                mfp = sta->mfp;
3417        } else if (vif->type == NL80211_IFTYPE_AP &&
3418                   !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
3419                struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3420
3421                sta_id = mvmvif->mcast_sta.sta_id;
3422        } else {
3423                IWL_ERR(mvm, "Failed to find station id\n");
3424                return -EINVAL;
3425        }
3426
3427        switch (keyconf->cipher) {
3428        case WLAN_CIPHER_SUITE_TKIP:
3429                addr = iwl_mvm_get_mac_addr(mvm, vif, sta);
3430                /* get phase 1 key from mac80211 */
3431                ieee80211_get_key_rx_seq(keyconf, 0, &seq);
3432                ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
3433                ret = iwl_mvm_send_sta_key(mvm, sta_id, keyconf, mcast,
3434                                           seq.tkip.iv32, p1k, 0, key_offset,
3435                                           mfp);
3436                break;
3437        case WLAN_CIPHER_SUITE_CCMP:
3438        case WLAN_CIPHER_SUITE_WEP40:
3439        case WLAN_CIPHER_SUITE_WEP104:
3440        case WLAN_CIPHER_SUITE_GCMP:
3441        case WLAN_CIPHER_SUITE_GCMP_256:
3442                ret = iwl_mvm_send_sta_key(mvm, sta_id, keyconf, mcast,
3443                                           0, NULL, 0, key_offset, mfp);
3444                break;
3445        default:
3446                ret = iwl_mvm_send_sta_key(mvm, sta_id, keyconf, mcast,
3447                                           0, NULL, 0, key_offset, mfp);
3448        }
3449
3450        return ret;
3451}
3452
3453int iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
3454                        struct ieee80211_vif *vif,
3455                        struct ieee80211_sta *sta,
3456                        struct ieee80211_key_conf *keyconf,
3457                        u8 key_offset)
3458{
3459        bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
3460        struct iwl_mvm_sta *mvm_sta;
3461        u8 sta_id = IWL_MVM_INVALID_STA;
3462        int ret;
3463        static const u8 __maybe_unused zero_addr[ETH_ALEN] = {0};
3464
3465        lockdep_assert_held(&mvm->mutex);
3466
3467        if (vif->type != NL80211_IFTYPE_AP ||
3468            keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
3469                /* Get the station id from the mvm local station table */
3470                mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
3471                if (!mvm_sta) {
3472                        IWL_ERR(mvm, "Failed to find station\n");
3473                        return -EINVAL;
3474                }
3475                sta_id = mvm_sta->sta_id;
3476
3477                /*
3478                 * It is possible that the 'sta' parameter is NULL, and thus
3479                 * there is a need to retrieve the sta from the local station
3480                 * table.
3481                 */
3482                if (!sta) {
3483                        sta = rcu_dereference_protected(
3484                                mvm->fw_id_to_mac_id[sta_id],
3485                                lockdep_is_held(&mvm->mutex));
3486                        if (IS_ERR_OR_NULL(sta)) {
3487                                IWL_ERR(mvm, "Invalid station id\n");
3488                                return -EINVAL;
3489                        }
3490                }
3491
3492                if (WARN_ON_ONCE(iwl_mvm_sta_from_mac80211(sta)->vif != vif))
3493                        return -EINVAL;
3494        } else {
3495                struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3496
3497                sta_id = mvmvif->mcast_sta.sta_id;
3498        }
3499
3500        if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
3501            keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
3502            keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256) {
3503                ret = iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, false);
3504                goto end;
3505        }
3506
3507        /* If the key_offset is not pre-assigned, we need to find a
3508         * new offset to use.  In normal cases, the offset is not
3509         * pre-assigned, but during HW_RESTART we want to reuse the
3510         * same indices, so we pass them when this function is called.
3511         *
3512         * In D3 entry, we need to hardcoded the indices (because the
3513         * firmware hardcodes the PTK offset to 0).  In this case, we
3514         * need to make sure we don't overwrite the hw_key_idx in the
3515         * keyconf structure, because otherwise we cannot configure
3516         * the original ones back when resuming.
3517         */
3518        if (key_offset == STA_KEY_IDX_INVALID) {
3519                key_offset  = iwl_mvm_set_fw_key_idx(mvm);
3520                if (key_offset == STA_KEY_IDX_INVALID)
3521                        return -ENOSPC;
3522                keyconf->hw_key_idx = key_offset;
3523        }
3524
3525        ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf, key_offset, mcast);
3526        if (ret)
3527                goto end;
3528
3529        /*
3530         * For WEP, the same key is used for multicast and unicast. Upload it
3531         * again, using the same key offset, and now pointing the other one
3532         * to the same key slot (offset).
3533         * If this fails, remove the original as well.
3534         */
3535        if ((keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
3536             keyconf->cipher == WLAN_CIPHER_SUITE_WEP104) &&
3537            sta) {
3538                ret = __iwl_mvm_set_sta_key(mvm, vif, sta, keyconf,
3539                                            key_offset, !mcast);
3540                if (ret) {
3541                        __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
3542                        goto end;
3543                }
3544        }
3545
3546        __set_bit(key_offset, mvm->fw_key_table);
3547
3548end:
3549        IWL_DEBUG_WEP(mvm, "key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
3550                      keyconf->cipher, keyconf->keylen, keyconf->keyidx,
3551                      sta ? sta->addr : zero_addr, ret);
3552        return ret;
3553}
3554
3555int iwl_mvm_remove_sta_key(struct iwl_mvm *mvm,
3556                           struct ieee80211_vif *vif,
3557                           struct ieee80211_sta *sta,
3558                           struct ieee80211_key_conf *keyconf)
3559{
3560        bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
3561        struct iwl_mvm_sta *mvm_sta;
3562        u8 sta_id = IWL_MVM_INVALID_STA;
3563        int ret, i;
3564
3565        lockdep_assert_held(&mvm->mutex);
3566
3567        /* Get the station from the mvm local station table */
3568        mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
3569        if (mvm_sta)
3570                sta_id = mvm_sta->sta_id;
3571        else if (!sta && vif->type == NL80211_IFTYPE_AP && mcast)
3572                sta_id = iwl_mvm_vif_from_mac80211(vif)->mcast_sta.sta_id;
3573
3574
3575        IWL_DEBUG_WEP(mvm, "mvm remove dynamic key: idx=%d sta=%d\n",
3576                      keyconf->keyidx, sta_id);
3577
3578        if (mvm_sta && (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
3579                        keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
3580                        keyconf->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256))
3581                return iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, true);
3582
3583        if (!__test_and_clear_bit(keyconf->hw_key_idx, mvm->fw_key_table)) {
3584                IWL_ERR(mvm, "offset %d not used in fw key table.\n",
3585                        keyconf->hw_key_idx);
3586                return -ENOENT;
3587        }
3588
3589        /* track which key was deleted last */
3590        for (i = 0; i < STA_KEY_MAX_NUM; i++) {
3591                if (mvm->fw_key_deleted[i] < U8_MAX)
3592                        mvm->fw_key_deleted[i]++;
3593        }
3594        mvm->fw_key_deleted[keyconf->hw_key_idx] = 0;
3595
3596        if (sta && !mvm_sta) {
3597                IWL_DEBUG_WEP(mvm, "station non-existent, early return.\n");
3598                return 0;
3599        }
3600
3601        ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, mcast);
3602        if (ret)
3603                return ret;
3604
3605        /* delete WEP key twice to get rid of (now useless) offset */
3606        if (keyconf->cipher == WLAN_CIPHER_SUITE_WEP40 ||
3607            keyconf->cipher == WLAN_CIPHER_SUITE_WEP104)
3608                ret = __iwl_mvm_remove_sta_key(mvm, sta_id, keyconf, !mcast);
3609
3610        return ret;
3611}
3612
3613void iwl_mvm_update_tkip_key(struct iwl_mvm *mvm,
3614                             struct ieee80211_vif *vif,
3615                             struct ieee80211_key_conf *keyconf,
3616                             struct ieee80211_sta *sta, u32 iv32,
3617                             u16 *phase1key)
3618{
3619        struct iwl_mvm_sta *mvm_sta;
3620        bool mcast = !(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE);
3621        bool mfp = sta ? sta->mfp : false;
3622
3623        rcu_read_lock();
3624
3625        mvm_sta = iwl_mvm_get_key_sta(mvm, vif, sta);
3626        if (WARN_ON_ONCE(!mvm_sta))
3627                goto unlock;
3628        iwl_mvm_send_sta_key(mvm, mvm_sta->sta_id, keyconf, mcast,
3629                             iv32, phase1key, CMD_ASYNC, keyconf->hw_key_idx,
3630                             mfp);
3631
3632 unlock:
3633        rcu_read_unlock();
3634}
3635
3636void iwl_mvm_sta_modify_ps_wake(struct iwl_mvm *mvm,
3637                                struct ieee80211_sta *sta)
3638{
3639        struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3640        struct iwl_mvm_add_sta_cmd cmd = {
3641                .add_modify = STA_MODE_MODIFY,
3642                .sta_id = mvmsta->sta_id,
3643                .station_flags_msk = cpu_to_le32(STA_FLG_PS),
3644                .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
3645        };
3646        int ret;
3647
3648        ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
3649                                   iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3650        if (ret)
3651                IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3652}
3653
3654void iwl_mvm_sta_modify_sleep_tx_count(struct iwl_mvm *mvm,
3655                                       struct ieee80211_sta *sta,
3656                                       enum ieee80211_frame_release_type reason,
3657                                       u16 cnt, u16 tids, bool more_data,
3658                                       bool single_sta_queue)
3659{
3660        struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3661        struct iwl_mvm_add_sta_cmd cmd = {
3662                .add_modify = STA_MODE_MODIFY,
3663                .sta_id = mvmsta->sta_id,
3664                .modify_mask = STA_MODIFY_SLEEPING_STA_TX_COUNT,
3665                .sleep_tx_count = cpu_to_le16(cnt),
3666                .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
3667        };
3668        int tid, ret;
3669        unsigned long _tids = tids;
3670
3671        /* convert TIDs to ACs - we don't support TSPEC so that's OK
3672         * Note that this field is reserved and unused by firmware not
3673         * supporting GO uAPSD, so it's safe to always do this.
3674         */
3675        for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT)
3676                cmd.awake_acs |= BIT(tid_to_ucode_ac[tid]);
3677
3678        /* If we're releasing frames from aggregation or dqa queues then check
3679         * if all the queues that we're releasing frames from, combined, have:
3680         *  - more frames than the service period, in which case more_data
3681         *    needs to be set
3682         *  - fewer than 'cnt' frames, in which case we need to adjust the
3683         *    firmware command (but do that unconditionally)
3684         */
3685        if (single_sta_queue) {
3686                int remaining = cnt;
3687                int sleep_tx_count;
3688
3689                spin_lock_bh(&mvmsta->lock);
3690                for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT) {
3691                        struct iwl_mvm_tid_data *tid_data;
3692                        u16 n_queued;
3693
3694                        tid_data = &mvmsta->tid_data[tid];
3695
3696                        n_queued = iwl_mvm_tid_queued(mvm, tid_data);
3697                        if (n_queued > remaining) {
3698                                more_data = true;
3699                                remaining = 0;
3700                                break;
3701                        }
3702                        remaining -= n_queued;
3703                }
3704                sleep_tx_count = cnt - remaining;
3705                if (reason == IEEE80211_FRAME_RELEASE_UAPSD)
3706                        mvmsta->sleep_tx_count = sleep_tx_count;
3707                spin_unlock_bh(&mvmsta->lock);
3708
3709                cmd.sleep_tx_count = cpu_to_le16(sleep_tx_count);
3710                if (WARN_ON(cnt - remaining == 0)) {
3711                        ieee80211_sta_eosp(sta);
3712                        return;
3713                }
3714        }
3715
3716        /* Note: this is ignored by firmware not supporting GO uAPSD */
3717        if (more_data)
3718                cmd.sleep_state_flags |= STA_SLEEP_STATE_MOREDATA;
3719
3720        if (reason == IEEE80211_FRAME_RELEASE_PSPOLL) {
3721                mvmsta->next_status_eosp = true;
3722                cmd.sleep_state_flags |= STA_SLEEP_STATE_PS_POLL;
3723        } else {
3724                cmd.sleep_state_flags |= STA_SLEEP_STATE_UAPSD;
3725        }
3726
3727        /* block the Tx queues until the FW updated the sleep Tx count */
3728        iwl_trans_block_txq_ptrs(mvm->trans, true);
3729
3730        ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA,
3731                                   CMD_ASYNC | CMD_WANT_ASYNC_CALLBACK,
3732                                   iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3733        if (ret)
3734                IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3735}
3736
3737void iwl_mvm_rx_eosp_notif(struct iwl_mvm *mvm,
3738                           struct iwl_rx_cmd_buffer *rxb)
3739{
3740        struct iwl_rx_packet *pkt = rxb_addr(rxb);
3741        struct iwl_mvm_eosp_notification *notif = (void *)pkt->data;
3742        struct ieee80211_sta *sta;
3743        u32 sta_id = le32_to_cpu(notif->sta_id);
3744
3745        if (WARN_ON_ONCE(sta_id >= IWL_MVM_STATION_COUNT))
3746                return;
3747
3748        rcu_read_lock();
3749        sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
3750        if (!IS_ERR_OR_NULL(sta))
3751                ieee80211_sta_eosp(sta);
3752        rcu_read_unlock();
3753}
3754
3755void iwl_mvm_sta_modify_disable_tx(struct iwl_mvm *mvm,
3756                                   struct iwl_mvm_sta *mvmsta, bool disable)
3757{
3758        struct iwl_mvm_add_sta_cmd cmd = {
3759                .add_modify = STA_MODE_MODIFY,
3760                .sta_id = mvmsta->sta_id,
3761                .station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
3762                .station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
3763                .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
3764        };
3765        int ret;
3766
3767        ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
3768                                   iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3769        if (ret)
3770                IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3771}
3772
3773void iwl_mvm_sta_modify_disable_tx_ap(struct iwl_mvm *mvm,
3774                                      struct ieee80211_sta *sta,
3775                                      bool disable)
3776{
3777        struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3778
3779        spin_lock_bh(&mvm_sta->lock);
3780
3781        if (mvm_sta->disable_tx == disable) {
3782                spin_unlock_bh(&mvm_sta->lock);
3783                return;
3784        }
3785
3786        mvm_sta->disable_tx = disable;
3787
3788        /* Tell mac80211 to start/stop queuing tx for this station */
3789        ieee80211_sta_block_awake(mvm->hw, sta, disable);
3790
3791        iwl_mvm_sta_modify_disable_tx(mvm, mvm_sta, disable);
3792
3793        spin_unlock_bh(&mvm_sta->lock);
3794}
3795
3796static void iwl_mvm_int_sta_modify_disable_tx(struct iwl_mvm *mvm,
3797                                              struct iwl_mvm_vif *mvmvif,
3798                                              struct iwl_mvm_int_sta *sta,
3799                                              bool disable)
3800{
3801        u32 id = FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color);
3802        struct iwl_mvm_add_sta_cmd cmd = {
3803                .add_modify = STA_MODE_MODIFY,
3804                .sta_id = sta->sta_id,
3805                .station_flags = disable ? cpu_to_le32(STA_FLG_DISABLE_TX) : 0,
3806                .station_flags_msk = cpu_to_le32(STA_FLG_DISABLE_TX),
3807                .mac_id_n_color = cpu_to_le32(id),
3808        };
3809        int ret;
3810
3811        ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, 0,
3812                                   iwl_mvm_add_sta_cmd_size(mvm), &cmd);
3813        if (ret)
3814                IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
3815}
3816
3817void iwl_mvm_modify_all_sta_disable_tx(struct iwl_mvm *mvm,
3818                                       struct iwl_mvm_vif *mvmvif,
3819                                       bool disable)
3820{
3821        struct ieee80211_sta *sta;
3822        struct iwl_mvm_sta *mvm_sta;
3823        int i;
3824
3825        lockdep_assert_held(&mvm->mutex);
3826
3827        /* Block/unblock all the stations of the given mvmvif */
3828        for (i = 0; i < ARRAY_SIZE(mvm->fw_id_to_mac_id); i++) {
3829                sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
3830                                                lockdep_is_held(&mvm->mutex));
3831                if (IS_ERR_OR_NULL(sta))
3832                        continue;
3833
3834                mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3835                if (mvm_sta->mac_id_n_color !=
3836                    FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color))
3837                        continue;
3838
3839                iwl_mvm_sta_modify_disable_tx_ap(mvm, sta, disable);
3840        }
3841
3842        if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
3843                return;
3844
3845        /* Need to block/unblock also multicast station */
3846        if (mvmvif->mcast_sta.sta_id != IWL_MVM_INVALID_STA)
3847                iwl_mvm_int_sta_modify_disable_tx(mvm, mvmvif,
3848                                                  &mvmvif->mcast_sta, disable);
3849
3850        /*
3851         * Only unblock the broadcast station (FW blocks it for immediate
3852         * quiet, not the driver)
3853         */
3854        if (!disable && mvmvif->bcast_sta.sta_id != IWL_MVM_INVALID_STA)
3855                iwl_mvm_int_sta_modify_disable_tx(mvm, mvmvif,
3856                                                  &mvmvif->bcast_sta, disable);
3857}
3858
3859void iwl_mvm_csa_client_absent(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
3860{
3861        struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3862        struct iwl_mvm_sta *mvmsta;
3863
3864        rcu_read_lock();
3865
3866        mvmsta = iwl_mvm_sta_from_staid_rcu(mvm, mvmvif->ap_sta_id);
3867
3868        if (!WARN_ON(!mvmsta))
3869                iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, true);
3870
3871        rcu_read_unlock();
3872}
3873
3874u16 iwl_mvm_tid_queued(struct iwl_mvm *mvm, struct iwl_mvm_tid_data *tid_data)
3875{
3876        u16 sn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
3877
3878        /*
3879         * In 22000 HW, the next_reclaimed index is only 8 bit, so we'll need
3880         * to align the wrap around of ssn so we compare relevant values.
3881         */
3882        if (mvm->trans->trans_cfg->gen2)
3883                sn &= 0xff;
3884
3885        return ieee80211_sn_sub(sn, tid_data->next_reclaimed);
3886}
3887