linux/drivers/net/wireless/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 - 2014 Intel Corporation. All rights reserved.
   9 *
  10 * This program is free software; you can redistribute it and/or modify
  11 * it under the terms of version 2 of the GNU General Public License as
  12 * published by the Free Software Foundation.
  13 *
  14 * This program is distributed in the hope that it will be useful, but
  15 * WITHOUT ANY WARRANTY; without even the implied warranty of
  16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  17 * General Public License for more details.
  18 *
  19 * You should have received a copy of the GNU General Public License
  20 * along with this program; if not, write to the Free Software
  21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  22 * USA
  23 *
  24 * The full GNU General Public License is included in this distribution
  25 * in the file called COPYING.
  26 *
  27 * Contact Information:
  28 *  Intel Linux Wireless <ilw@linux.intel.com>
  29 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  30 *
  31 * BSD LICENSE
  32 *
  33 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
  34 * All rights reserved.
  35 *
  36 * Redistribution and use in source and binary forms, with or without
  37 * modification, are permitted provided that the following conditions
  38 * are met:
  39 *
  40 *  * Redistributions of source code must retain the above copyright
  41 *    notice, this list of conditions and the following disclaimer.
  42 *  * Redistributions in binary form must reproduce the above copyright
  43 *    notice, this list of conditions and the following disclaimer in
  44 *    the documentation and/or other materials provided with the
  45 *    distribution.
  46 *  * Neither the name Intel Corporation nor the names of its
  47 *    contributors may be used to endorse or promote products derived
  48 *    from this software without specific prior written permission.
  49 *
  50 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  51 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  52 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  53 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  54 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  55 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  56 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  57 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  58 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  59 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  60 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  61 *
  62 *****************************************************************************/
  63#include <net/mac80211.h>
  64
  65#include "mvm.h"
  66#include "sta.h"
  67#include "rs.h"
  68
  69static void iwl_mvm_add_sta_cmd_v7_to_v5(struct iwl_mvm_add_sta_cmd_v7 *cmd_v7,
  70                                         struct iwl_mvm_add_sta_cmd_v5 *cmd_v5)
  71{
  72        memset(cmd_v5, 0, sizeof(*cmd_v5));
  73
  74        cmd_v5->add_modify = cmd_v7->add_modify;
  75        cmd_v5->tid_disable_tx = cmd_v7->tid_disable_tx;
  76        cmd_v5->mac_id_n_color = cmd_v7->mac_id_n_color;
  77        memcpy(cmd_v5->addr, cmd_v7->addr, ETH_ALEN);
  78        cmd_v5->sta_id = cmd_v7->sta_id;
  79        cmd_v5->modify_mask = cmd_v7->modify_mask;
  80        cmd_v5->station_flags = cmd_v7->station_flags;
  81        cmd_v5->station_flags_msk = cmd_v7->station_flags_msk;
  82        cmd_v5->add_immediate_ba_tid = cmd_v7->add_immediate_ba_tid;
  83        cmd_v5->remove_immediate_ba_tid = cmd_v7->remove_immediate_ba_tid;
  84        cmd_v5->add_immediate_ba_ssn = cmd_v7->add_immediate_ba_ssn;
  85        cmd_v5->sleep_tx_count = cmd_v7->sleep_tx_count;
  86        cmd_v5->sleep_state_flags = cmd_v7->sleep_state_flags;
  87        cmd_v5->assoc_id = cmd_v7->assoc_id;
  88        cmd_v5->beamform_flags = cmd_v7->beamform_flags;
  89        cmd_v5->tfd_queue_msk = cmd_v7->tfd_queue_msk;
  90}
  91
  92static void
  93iwl_mvm_add_sta_key_to_add_sta_cmd_v5(struct iwl_mvm_add_sta_key_cmd *key_cmd,
  94                                      struct iwl_mvm_add_sta_cmd_v5 *sta_cmd,
  95                                      u32 mac_id_n_color)
  96{
  97        memset(sta_cmd, 0, sizeof(*sta_cmd));
  98
  99        sta_cmd->sta_id = key_cmd->sta_id;
 100        sta_cmd->add_modify = STA_MODE_MODIFY;
 101        sta_cmd->modify_mask = STA_MODIFY_KEY;
 102        sta_cmd->mac_id_n_color = cpu_to_le32(mac_id_n_color);
 103
 104        sta_cmd->key.key_offset = key_cmd->key_offset;
 105        sta_cmd->key.key_flags = key_cmd->key_flags;
 106        memcpy(sta_cmd->key.key, key_cmd->key, sizeof(sta_cmd->key.key));
 107        sta_cmd->key.tkip_rx_tsc_byte2 = key_cmd->tkip_rx_tsc_byte2;
 108        memcpy(sta_cmd->key.tkip_rx_ttak, key_cmd->tkip_rx_ttak,
 109               sizeof(sta_cmd->key.tkip_rx_ttak));
 110}
 111
 112static int iwl_mvm_send_add_sta_cmd_status(struct iwl_mvm *mvm,
 113                                           struct iwl_mvm_add_sta_cmd_v7 *cmd,
 114                                           int *status)
 115{
 116        struct iwl_mvm_add_sta_cmd_v5 cmd_v5;
 117
 118        if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_STA_KEY_CMD)
 119                return iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(*cmd),
 120                                                   cmd, status);
 121
 122        iwl_mvm_add_sta_cmd_v7_to_v5(cmd, &cmd_v5);
 123
 124        return iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd_v5),
 125                                           &cmd_v5, status);
 126}
 127
 128static int iwl_mvm_send_add_sta_cmd(struct iwl_mvm *mvm, u32 flags,
 129                                    struct iwl_mvm_add_sta_cmd_v7 *cmd)
 130{
 131        struct iwl_mvm_add_sta_cmd_v5 cmd_v5;
 132
 133        if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_STA_KEY_CMD)
 134                return iwl_mvm_send_cmd_pdu(mvm, ADD_STA, flags,
 135                                            sizeof(*cmd), cmd);
 136
 137        iwl_mvm_add_sta_cmd_v7_to_v5(cmd, &cmd_v5);
 138
 139        return iwl_mvm_send_cmd_pdu(mvm, ADD_STA, flags, sizeof(cmd_v5),
 140                                    &cmd_v5);
 141}
 142
 143static int
 144iwl_mvm_send_add_sta_key_cmd_status(struct iwl_mvm *mvm,
 145                                    struct iwl_mvm_add_sta_key_cmd *cmd,
 146                                    u32 mac_id_n_color,
 147                                    int *status)
 148{
 149        struct iwl_mvm_add_sta_cmd_v5 sta_cmd;
 150
 151        if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_STA_KEY_CMD)
 152                return iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA_KEY,
 153                                                   sizeof(*cmd), cmd, status);
 154
 155        iwl_mvm_add_sta_key_to_add_sta_cmd_v5(cmd, &sta_cmd, mac_id_n_color);
 156
 157        return iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(sta_cmd),
 158                                           &sta_cmd, status);
 159}
 160
 161static int iwl_mvm_send_add_sta_key_cmd(struct iwl_mvm *mvm,
 162                                        u32 flags,
 163                                        struct iwl_mvm_add_sta_key_cmd *cmd,
 164                                        u32 mac_id_n_color)
 165{
 166        struct iwl_mvm_add_sta_cmd_v5 sta_cmd;
 167
 168        if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_STA_KEY_CMD)
 169                return iwl_mvm_send_cmd_pdu(mvm, ADD_STA_KEY, flags,
 170                                            sizeof(*cmd), cmd);
 171
 172        iwl_mvm_add_sta_key_to_add_sta_cmd_v5(cmd, &sta_cmd, mac_id_n_color);
 173
 174        return iwl_mvm_send_cmd_pdu(mvm, ADD_STA, flags, sizeof(sta_cmd),
 175                                    &sta_cmd);
 176}
 177
 178static int iwl_mvm_find_free_sta_id(struct iwl_mvm *mvm,
 179                                    enum nl80211_iftype iftype)
 180{
 181        int sta_id;
 182        u32 reserved_ids = 0;
 183
 184        BUILD_BUG_ON(IWL_MVM_STATION_COUNT > 32);
 185        WARN_ON_ONCE(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status));
 186
 187        lockdep_assert_held(&mvm->mutex);
 188
 189        /* d0i3/d3 assumes the AP's sta_id (of sta vif) is 0. reserve it. */
 190        if (iftype != NL80211_IFTYPE_STATION)
 191                reserved_ids = BIT(0);
 192
 193        /* Don't take rcu_read_lock() since we are protected by mvm->mutex */
 194        for (sta_id = 0; sta_id < IWL_MVM_STATION_COUNT; sta_id++) {
 195                if (BIT(sta_id) & reserved_ids)
 196                        continue;
 197
 198                if (!rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
 199                                               lockdep_is_held(&mvm->mutex)))
 200                        return sta_id;
 201        }
 202        return IWL_MVM_STATION_COUNT;
 203}
 204
 205/* send station add/update command to firmware */
 206int iwl_mvm_sta_send_to_fw(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
 207                           bool update)
 208{
 209        struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
 210        struct iwl_mvm_add_sta_cmd_v7 add_sta_cmd;
 211        int ret;
 212        u32 status;
 213        u32 agg_size = 0, mpdu_dens = 0;
 214
 215        memset(&add_sta_cmd, 0, sizeof(add_sta_cmd));
 216
 217        add_sta_cmd.sta_id = mvm_sta->sta_id;
 218        add_sta_cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
 219        if (!update) {
 220                add_sta_cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
 221                memcpy(&add_sta_cmd.addr, sta->addr, ETH_ALEN);
 222        }
 223        add_sta_cmd.add_modify = update ? 1 : 0;
 224
 225        add_sta_cmd.station_flags_msk |= cpu_to_le32(STA_FLG_FAT_EN_MSK |
 226                                                     STA_FLG_MIMO_EN_MSK);
 227
 228        switch (sta->bandwidth) {
 229        case IEEE80211_STA_RX_BW_160:
 230                add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_160MHZ);
 231                /* fall through */
 232        case IEEE80211_STA_RX_BW_80:
 233                add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_80MHZ);
 234                /* fall through */
 235        case IEEE80211_STA_RX_BW_40:
 236                add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_FAT_EN_40MHZ);
 237                /* fall through */
 238        case IEEE80211_STA_RX_BW_20:
 239                if (sta->ht_cap.ht_supported)
 240                        add_sta_cmd.station_flags |=
 241                                cpu_to_le32(STA_FLG_FAT_EN_20MHZ);
 242                break;
 243        }
 244
 245        switch (sta->rx_nss) {
 246        case 1:
 247                add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
 248                break;
 249        case 2:
 250                add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO2);
 251                break;
 252        case 3 ... 8:
 253                add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_MIMO3);
 254                break;
 255        }
 256
 257        switch (sta->smps_mode) {
 258        case IEEE80211_SMPS_AUTOMATIC:
 259        case IEEE80211_SMPS_NUM_MODES:
 260                WARN_ON(1);
 261                break;
 262        case IEEE80211_SMPS_STATIC:
 263                /* override NSS */
 264                add_sta_cmd.station_flags &= ~cpu_to_le32(STA_FLG_MIMO_EN_MSK);
 265                add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_MIMO_EN_SISO);
 266                break;
 267        case IEEE80211_SMPS_DYNAMIC:
 268                add_sta_cmd.station_flags |= cpu_to_le32(STA_FLG_RTS_MIMO_PROT);
 269                break;
 270        case IEEE80211_SMPS_OFF:
 271                /* nothing */
 272                break;
 273        }
 274
 275        if (sta->ht_cap.ht_supported) {
 276                add_sta_cmd.station_flags_msk |=
 277                        cpu_to_le32(STA_FLG_MAX_AGG_SIZE_MSK |
 278                                    STA_FLG_AGG_MPDU_DENS_MSK);
 279
 280                mpdu_dens = sta->ht_cap.ampdu_density;
 281        }
 282
 283        if (sta->vht_cap.vht_supported) {
 284                agg_size = sta->vht_cap.cap &
 285                        IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
 286                agg_size >>=
 287                        IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
 288        } else if (sta->ht_cap.ht_supported) {
 289                agg_size = sta->ht_cap.ampdu_factor;
 290        }
 291
 292        add_sta_cmd.station_flags |=
 293                cpu_to_le32(agg_size << STA_FLG_MAX_AGG_SIZE_SHIFT);
 294        add_sta_cmd.station_flags |=
 295                cpu_to_le32(mpdu_dens << STA_FLG_AGG_MPDU_DENS_SHIFT);
 296
 297        status = ADD_STA_SUCCESS;
 298        ret = iwl_mvm_send_add_sta_cmd_status(mvm, &add_sta_cmd, &status);
 299        if (ret)
 300                return ret;
 301
 302        switch (status) {
 303        case ADD_STA_SUCCESS:
 304                IWL_DEBUG_ASSOC(mvm, "ADD_STA PASSED\n");
 305                break;
 306        default:
 307                ret = -EIO;
 308                IWL_ERR(mvm, "ADD_STA failed\n");
 309                break;
 310        }
 311
 312        return ret;
 313}
 314
 315int iwl_mvm_add_sta(struct iwl_mvm *mvm,
 316                    struct ieee80211_vif *vif,
 317                    struct ieee80211_sta *sta)
 318{
 319        struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
 320        struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
 321        int i, ret, sta_id;
 322
 323        lockdep_assert_held(&mvm->mutex);
 324
 325        if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
 326                sta_id = iwl_mvm_find_free_sta_id(mvm,
 327                                                  ieee80211_vif_type_p2p(vif));
 328        else
 329                sta_id = mvm_sta->sta_id;
 330
 331        if (WARN_ON_ONCE(sta_id == IWL_MVM_STATION_COUNT))
 332                return -ENOSPC;
 333
 334        spin_lock_init(&mvm_sta->lock);
 335
 336        mvm_sta->sta_id = sta_id;
 337        mvm_sta->mac_id_n_color = FW_CMD_ID_AND_COLOR(mvmvif->id,
 338                                                      mvmvif->color);
 339        mvm_sta->vif = vif;
 340        mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
 341        mvm_sta->tx_protection = 0;
 342        mvm_sta->tt_tx_protection = false;
 343
 344        /* HW restart, don't assume the memory has been zeroed */
 345        atomic_set(&mvm->pending_frames[sta_id], 0);
 346        mvm_sta->tid_disable_agg = 0;
 347        mvm_sta->tfd_queue_msk = 0;
 348        for (i = 0; i < IEEE80211_NUM_ACS; i++)
 349                if (vif->hw_queue[i] != IEEE80211_INVAL_HW_QUEUE)
 350                        mvm_sta->tfd_queue_msk |= BIT(vif->hw_queue[i]);
 351
 352        /* for HW restart - reset everything but the sequence number */
 353        for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
 354                u16 seq = mvm_sta->tid_data[i].seq_number;
 355                memset(&mvm_sta->tid_data[i], 0, sizeof(mvm_sta->tid_data[i]));
 356                mvm_sta->tid_data[i].seq_number = seq;
 357        }
 358
 359        ret = iwl_mvm_sta_send_to_fw(mvm, sta, false);
 360        if (ret)
 361                return ret;
 362
 363        /* The first station added is the AP, the others are TDLS STAs */
 364        if (vif->type == NL80211_IFTYPE_STATION &&
 365            mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT)
 366                mvmvif->ap_sta_id = sta_id;
 367
 368        rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], sta);
 369
 370        return 0;
 371}
 372
 373int iwl_mvm_update_sta(struct iwl_mvm *mvm,
 374                       struct ieee80211_vif *vif,
 375                       struct ieee80211_sta *sta)
 376{
 377        return iwl_mvm_sta_send_to_fw(mvm, sta, true);
 378}
 379
 380int iwl_mvm_drain_sta(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
 381                      bool drain)
 382{
 383        struct iwl_mvm_add_sta_cmd_v7 cmd = {};
 384        int ret;
 385        u32 status;
 386
 387        lockdep_assert_held(&mvm->mutex);
 388
 389        cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
 390        cmd.sta_id = mvmsta->sta_id;
 391        cmd.add_modify = STA_MODE_MODIFY;
 392        cmd.station_flags = drain ? cpu_to_le32(STA_FLG_DRAIN_FLOW) : 0;
 393        cmd.station_flags_msk = cpu_to_le32(STA_FLG_DRAIN_FLOW);
 394
 395        status = ADD_STA_SUCCESS;
 396        ret = iwl_mvm_send_add_sta_cmd_status(mvm, &cmd, &status);
 397        if (ret)
 398                return ret;
 399
 400        switch (status) {
 401        case ADD_STA_SUCCESS:
 402                IWL_DEBUG_INFO(mvm, "Frames for staid %d will drained in fw\n",
 403                               mvmsta->sta_id);
 404                break;
 405        default:
 406                ret = -EIO;
 407                IWL_ERR(mvm, "Couldn't drain frames for staid %d\n",
 408                        mvmsta->sta_id);
 409                break;
 410        }
 411
 412        return ret;
 413}
 414
 415/*
 416 * Remove a station from the FW table. Before sending the command to remove
 417 * the station validate that the station is indeed known to the driver (sanity
 418 * only).
 419 */
 420static int iwl_mvm_rm_sta_common(struct iwl_mvm *mvm, u8 sta_id)
 421{
 422        struct ieee80211_sta *sta;
 423        struct iwl_mvm_rm_sta_cmd rm_sta_cmd = {
 424                .sta_id = sta_id,
 425        };
 426        int ret;
 427
 428        sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
 429                                        lockdep_is_held(&mvm->mutex));
 430
 431        /* Note: internal stations are marked as error values */
 432        if (!sta) {
 433                IWL_ERR(mvm, "Invalid station id\n");
 434                return -EINVAL;
 435        }
 436
 437        ret = iwl_mvm_send_cmd_pdu(mvm, REMOVE_STA, CMD_SYNC,
 438                                   sizeof(rm_sta_cmd), &rm_sta_cmd);
 439        if (ret) {
 440                IWL_ERR(mvm, "Failed to remove station. Id=%d\n", sta_id);
 441                return ret;
 442        }
 443
 444        return 0;
 445}
 446
 447void iwl_mvm_sta_drained_wk(struct work_struct *wk)
 448{
 449        struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm, sta_drained_wk);
 450        u8 sta_id;
 451
 452        /*
 453         * The mutex is needed because of the SYNC cmd, but not only: if the
 454         * work would run concurrently with iwl_mvm_rm_sta, it would run before
 455         * iwl_mvm_rm_sta sets the station as busy, and exit. Then
 456         * iwl_mvm_rm_sta would set the station as busy, and nobody will clean
 457         * that later.
 458         */
 459        mutex_lock(&mvm->mutex);
 460
 461        for_each_set_bit(sta_id, mvm->sta_drained, IWL_MVM_STATION_COUNT) {
 462                int ret;
 463                struct ieee80211_sta *sta =
 464                        rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
 465                                                  lockdep_is_held(&mvm->mutex));
 466
 467                /*
 468                 * This station is in use or RCU-removed; the latter happens in
 469                 * managed mode, where mac80211 removes the station before we
 470                 * can remove it from firmware (we can only do that after the
 471                 * MAC is marked unassociated), and possibly while the deauth
 472                 * frame to disconnect from the AP is still queued. Then, the
 473                 * station pointer is -ENOENT when the last skb is reclaimed.
 474                 */
 475                if (!IS_ERR(sta) || PTR_ERR(sta) == -ENOENT)
 476                        continue;
 477
 478                if (PTR_ERR(sta) == -EINVAL) {
 479                        IWL_ERR(mvm, "Drained sta %d, but it is internal?\n",
 480                                sta_id);
 481                        continue;
 482                }
 483
 484                if (!sta) {
 485                        IWL_ERR(mvm, "Drained sta %d, but it was NULL?\n",
 486                                sta_id);
 487                        continue;
 488                }
 489
 490                WARN_ON(PTR_ERR(sta) != -EBUSY);
 491                /* This station was removed and we waited until it got drained,
 492                 * we can now proceed and remove it.
 493                 */
 494                ret = iwl_mvm_rm_sta_common(mvm, sta_id);
 495                if (ret) {
 496                        IWL_ERR(mvm,
 497                                "Couldn't remove sta %d after it was drained\n",
 498                                sta_id);
 499                        continue;
 500                }
 501                rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], NULL);
 502                clear_bit(sta_id, mvm->sta_drained);
 503        }
 504
 505        mutex_unlock(&mvm->mutex);
 506}
 507
 508int iwl_mvm_rm_sta(struct iwl_mvm *mvm,
 509                   struct ieee80211_vif *vif,
 510                   struct ieee80211_sta *sta)
 511{
 512        struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
 513        struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
 514        int ret;
 515
 516        lockdep_assert_held(&mvm->mutex);
 517
 518        if (vif->type == NL80211_IFTYPE_STATION &&
 519            mvmvif->ap_sta_id == mvm_sta->sta_id) {
 520                /* flush its queues here since we are freeing mvm_sta */
 521                ret = iwl_mvm_flush_tx_path(mvm, mvm_sta->tfd_queue_msk, true);
 522
 523                /*
 524                 * Put a non-NULL since the fw station isn't removed.
 525                 * It will be removed after the MAC will be set as
 526                 * unassoc.
 527                 */
 528                rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id],
 529                                   ERR_PTR(-EINVAL));
 530
 531                /* if we are associated - we can't remove the AP STA now */
 532                if (vif->bss_conf.assoc)
 533                        return ret;
 534
 535                /* unassoc - go ahead - remove the AP STA now */
 536                mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
 537
 538                /* clear d0i3_ap_sta_id if no longer relevant */
 539                if (mvm->d0i3_ap_sta_id == mvm_sta->sta_id)
 540                        mvm->d0i3_ap_sta_id = IWL_MVM_STATION_COUNT;
 541        }
 542
 543        /*
 544         * Make sure that the tx response code sees the station as -EBUSY and
 545         * calls the drain worker.
 546         */
 547        spin_lock_bh(&mvm_sta->lock);
 548        /*
 549         * There are frames pending on the AC queues for this station.
 550         * We need to wait until all the frames are drained...
 551         */
 552        if (atomic_read(&mvm->pending_frames[mvm_sta->sta_id])) {
 553                rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id],
 554                                   ERR_PTR(-EBUSY));
 555                spin_unlock_bh(&mvm_sta->lock);
 556                ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
 557        } else {
 558                spin_unlock_bh(&mvm_sta->lock);
 559                ret = iwl_mvm_rm_sta_common(mvm, mvm_sta->sta_id);
 560                rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id], NULL);
 561        }
 562
 563        return ret;
 564}
 565
 566int iwl_mvm_rm_sta_id(struct iwl_mvm *mvm,
 567                      struct ieee80211_vif *vif,
 568                      u8 sta_id)
 569{
 570        int ret = iwl_mvm_rm_sta_common(mvm, sta_id);
 571
 572        lockdep_assert_held(&mvm->mutex);
 573
 574        rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], NULL);
 575        return ret;
 576}
 577
 578int iwl_mvm_allocate_int_sta(struct iwl_mvm *mvm, struct iwl_mvm_int_sta *sta,
 579                             u32 qmask, enum nl80211_iftype iftype)
 580{
 581        if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
 582                sta->sta_id = iwl_mvm_find_free_sta_id(mvm, iftype);
 583                if (WARN_ON_ONCE(sta->sta_id == IWL_MVM_STATION_COUNT))
 584                        return -ENOSPC;
 585        }
 586
 587        sta->tfd_queue_msk = qmask;
 588
 589        /* put a non-NULL value so iterating over the stations won't stop */
 590        rcu_assign_pointer(mvm->fw_id_to_mac_id[sta->sta_id], ERR_PTR(-EINVAL));
 591        return 0;
 592}
 593
 594void iwl_mvm_dealloc_int_sta(struct iwl_mvm *mvm, struct iwl_mvm_int_sta *sta)
 595{
 596        rcu_assign_pointer(mvm->fw_id_to_mac_id[sta->sta_id], NULL);
 597        memset(sta, 0, sizeof(struct iwl_mvm_int_sta));
 598        sta->sta_id = IWL_MVM_STATION_COUNT;
 599}
 600
 601static int iwl_mvm_add_int_sta_common(struct iwl_mvm *mvm,
 602                                      struct iwl_mvm_int_sta *sta,
 603                                      const u8 *addr,
 604                                      u16 mac_id, u16 color)
 605{
 606        struct iwl_mvm_add_sta_cmd_v7 cmd;
 607        int ret;
 608        u32 status;
 609
 610        lockdep_assert_held(&mvm->mutex);
 611
 612        memset(&cmd, 0, sizeof(struct iwl_mvm_add_sta_cmd_v7));
 613        cmd.sta_id = sta->sta_id;
 614        cmd.mac_id_n_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mac_id,
 615                                                             color));
 616
 617        cmd.tfd_queue_msk = cpu_to_le32(sta->tfd_queue_msk);
 618
 619        if (addr)
 620                memcpy(cmd.addr, addr, ETH_ALEN);
 621
 622        ret = iwl_mvm_send_add_sta_cmd_status(mvm, &cmd, &status);
 623        if (ret)
 624                return ret;
 625
 626        switch (status) {
 627        case ADD_STA_SUCCESS:
 628                IWL_DEBUG_INFO(mvm, "Internal station added.\n");
 629                return 0;
 630        default:
 631                ret = -EIO;
 632                IWL_ERR(mvm, "Add internal station failed, status=0x%x\n",
 633                        status);
 634                break;
 635        }
 636        return ret;
 637}
 638
 639int iwl_mvm_add_aux_sta(struct iwl_mvm *mvm)
 640{
 641        int ret;
 642
 643        lockdep_assert_held(&mvm->mutex);
 644
 645        /* Add the aux station, but without any queues */
 646        ret = iwl_mvm_allocate_int_sta(mvm, &mvm->aux_sta, 0,
 647                                       NL80211_IFTYPE_UNSPECIFIED);
 648        if (ret)
 649                return ret;
 650
 651        ret = iwl_mvm_add_int_sta_common(mvm, &mvm->aux_sta, NULL,
 652                                         MAC_INDEX_AUX, 0);
 653
 654        if (ret)
 655                iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
 656        return ret;
 657}
 658
 659/*
 660 * Send the add station command for the vif's broadcast station.
 661 * Assumes that the station was already allocated.
 662 *
 663 * @mvm: the mvm component
 664 * @vif: the interface to which the broadcast station is added
 665 * @bsta: the broadcast station to add.
 666 */
 667int iwl_mvm_send_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
 668                           struct iwl_mvm_int_sta *bsta)
 669{
 670        struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
 671        static const u8 _baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
 672        const u8 *baddr = _baddr;
 673
 674        lockdep_assert_held(&mvm->mutex);
 675
 676        if (vif->type == NL80211_IFTYPE_ADHOC)
 677                baddr = vif->bss_conf.bssid;
 678
 679        if (WARN_ON_ONCE(bsta->sta_id == IWL_MVM_STATION_COUNT))
 680                return -ENOSPC;
 681
 682        return iwl_mvm_add_int_sta_common(mvm, bsta, baddr,
 683                                          mvmvif->id, mvmvif->color);
 684}
 685
 686/* Send the FW a request to remove the station from it's internal data
 687 * structures, but DO NOT remove the entry from the local data structures. */
 688int iwl_mvm_send_rm_bcast_sta(struct iwl_mvm *mvm,
 689                              struct iwl_mvm_int_sta *bsta)
 690{
 691        int ret;
 692
 693        lockdep_assert_held(&mvm->mutex);
 694
 695        ret = iwl_mvm_rm_sta_common(mvm, bsta->sta_id);
 696        if (ret)
 697                IWL_WARN(mvm, "Failed sending remove station\n");
 698        return ret;
 699}
 700
 701/* Allocate a new station entry for the broadcast station to the given vif,
 702 * and send it to the FW.
 703 * Note that each P2P mac should have its own broadcast station.
 704 *
 705 * @mvm: the mvm component
 706 * @vif: the interface to which the broadcast station is added
 707 * @bsta: the broadcast station to add. */
 708int iwl_mvm_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
 709                          struct iwl_mvm_int_sta *bsta)
 710{
 711        struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
 712        static const u8 baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
 713        u32 qmask;
 714        int ret;
 715
 716        lockdep_assert_held(&mvm->mutex);
 717
 718        qmask = iwl_mvm_mac_get_queues_mask(mvm, vif);
 719        ret = iwl_mvm_allocate_int_sta(mvm, bsta, qmask,
 720                                       ieee80211_vif_type_p2p(vif));
 721        if (ret)
 722                return ret;
 723
 724        ret = iwl_mvm_add_int_sta_common(mvm, bsta, baddr,
 725                                         mvmvif->id, mvmvif->color);
 726
 727        if (ret)
 728                iwl_mvm_dealloc_int_sta(mvm, bsta);
 729        return ret;
 730}
 731
 732/*
 733 * Send the FW a request to remove the station from it's internal data
 734 * structures, and in addition remove it from the local data structure.
 735 */
 736int iwl_mvm_rm_bcast_sta(struct iwl_mvm *mvm, struct iwl_mvm_int_sta *bsta)
 737{
 738        int ret;
 739
 740        lockdep_assert_held(&mvm->mutex);
 741
 742        ret = iwl_mvm_rm_sta_common(mvm, bsta->sta_id);
 743        if (ret)
 744                return ret;
 745
 746        iwl_mvm_dealloc_int_sta(mvm, bsta);
 747        return ret;
 748}
 749
 750#define IWL_MAX_RX_BA_SESSIONS 16
 751
 752int iwl_mvm_sta_rx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
 753                       int tid, u16 ssn, bool start)
 754{
 755        struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
 756        struct iwl_mvm_add_sta_cmd_v7 cmd = {};
 757        int ret;
 758        u32 status;
 759
 760        lockdep_assert_held(&mvm->mutex);
 761
 762        if (start && mvm->rx_ba_sessions >= IWL_MAX_RX_BA_SESSIONS) {
 763                IWL_WARN(mvm, "Not enough RX BA SESSIONS\n");
 764                return -ENOSPC;
 765        }
 766
 767        cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
 768        cmd.sta_id = mvm_sta->sta_id;
 769        cmd.add_modify = STA_MODE_MODIFY;
 770        if (start) {
 771                cmd.add_immediate_ba_tid = (u8) tid;
 772                cmd.add_immediate_ba_ssn = cpu_to_le16(ssn);
 773        } else {
 774                cmd.remove_immediate_ba_tid = (u8) tid;
 775        }
 776        cmd.modify_mask = start ? STA_MODIFY_ADD_BA_TID :
 777                                  STA_MODIFY_REMOVE_BA_TID;
 778
 779        status = ADD_STA_SUCCESS;
 780        ret = iwl_mvm_send_add_sta_cmd_status(mvm, &cmd, &status);
 781        if (ret)
 782                return ret;
 783
 784        switch (status) {
 785        case ADD_STA_SUCCESS:
 786                IWL_DEBUG_INFO(mvm, "RX BA Session %sed in fw\n",
 787                               start ? "start" : "stopp");
 788                break;
 789        case ADD_STA_IMMEDIATE_BA_FAILURE:
 790                IWL_WARN(mvm, "RX BA Session refused by fw\n");
 791                ret = -ENOSPC;
 792                break;
 793        default:
 794                ret = -EIO;
 795                IWL_ERR(mvm, "RX BA Session failed %sing, status 0x%x\n",
 796                        start ? "start" : "stopp", status);
 797                break;
 798        }
 799
 800        if (!ret) {
 801                if (start)
 802                        mvm->rx_ba_sessions++;
 803                else if (mvm->rx_ba_sessions > 0)
 804                        /* check that restart flow didn't zero the counter */
 805                        mvm->rx_ba_sessions--;
 806        }
 807
 808        return ret;
 809}
 810
 811static int iwl_mvm_sta_tx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
 812                              int tid, u8 queue, bool start)
 813{
 814        struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
 815        struct iwl_mvm_add_sta_cmd_v7 cmd = {};
 816        int ret;
 817        u32 status;
 818
 819        lockdep_assert_held(&mvm->mutex);
 820
 821        if (start) {
 822                mvm_sta->tfd_queue_msk |= BIT(queue);
 823                mvm_sta->tid_disable_agg &= ~BIT(tid);
 824        } else {
 825                mvm_sta->tfd_queue_msk &= ~BIT(queue);
 826                mvm_sta->tid_disable_agg |= BIT(tid);
 827        }
 828
 829        cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
 830        cmd.sta_id = mvm_sta->sta_id;
 831        cmd.add_modify = STA_MODE_MODIFY;
 832        cmd.modify_mask = STA_MODIFY_QUEUES | STA_MODIFY_TID_DISABLE_TX;
 833        cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
 834        cmd.tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg);
 835
 836        status = ADD_STA_SUCCESS;
 837        ret = iwl_mvm_send_add_sta_cmd_status(mvm, &cmd, &status);
 838        if (ret)
 839                return ret;
 840
 841        switch (status) {
 842        case ADD_STA_SUCCESS:
 843                break;
 844        default:
 845                ret = -EIO;
 846                IWL_ERR(mvm, "TX BA Session failed %sing, status 0x%x\n",
 847                        start ? "start" : "stopp", status);
 848                break;
 849        }
 850
 851        return ret;
 852}
 853
 854const u8 tid_to_mac80211_ac[] = {
 855        IEEE80211_AC_BE,
 856        IEEE80211_AC_BK,
 857        IEEE80211_AC_BK,
 858        IEEE80211_AC_BE,
 859        IEEE80211_AC_VI,
 860        IEEE80211_AC_VI,
 861        IEEE80211_AC_VO,
 862        IEEE80211_AC_VO,
 863};
 864
 865static const u8 tid_to_ucode_ac[] = {
 866        AC_BE,
 867        AC_BK,
 868        AC_BK,
 869        AC_BE,
 870        AC_VI,
 871        AC_VI,
 872        AC_VO,
 873        AC_VO,
 874};
 875
 876int iwl_mvm_sta_tx_agg_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
 877                             struct ieee80211_sta *sta, u16 tid, u16 *ssn)
 878{
 879        struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
 880        struct iwl_mvm_tid_data *tid_data;
 881        int txq_id;
 882
 883        if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
 884                return -EINVAL;
 885
 886        if (mvmsta->tid_data[tid].state != IWL_AGG_OFF) {
 887                IWL_ERR(mvm, "Start AGG when state is not IWL_AGG_OFF %d!\n",
 888                        mvmsta->tid_data[tid].state);
 889                return -ENXIO;
 890        }
 891
 892        lockdep_assert_held(&mvm->mutex);
 893
 894        for (txq_id = mvm->first_agg_queue;
 895             txq_id <= mvm->last_agg_queue; txq_id++)
 896                if (mvm->queue_to_mac80211[txq_id] ==
 897                    IWL_INVALID_MAC80211_QUEUE)
 898                        break;
 899
 900        if (txq_id > mvm->last_agg_queue) {
 901                IWL_ERR(mvm, "Failed to allocate agg queue\n");
 902                return -EIO;
 903        }
 904
 905        spin_lock_bh(&mvmsta->lock);
 906
 907        /* possible race condition - we entered D0i3 while starting agg */
 908        if (test_bit(IWL_MVM_STATUS_IN_D0I3, &mvm->status)) {
 909                spin_unlock_bh(&mvmsta->lock);
 910                IWL_ERR(mvm, "Entered D0i3 while starting Tx agg\n");
 911                return -EIO;
 912        }
 913
 914        /* the new tx queue is still connected to the same mac80211 queue */
 915        mvm->queue_to_mac80211[txq_id] = vif->hw_queue[tid_to_mac80211_ac[tid]];
 916
 917        tid_data = &mvmsta->tid_data[tid];
 918        tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
 919        tid_data->txq_id = txq_id;
 920        *ssn = tid_data->ssn;
 921
 922        IWL_DEBUG_TX_QUEUES(mvm,
 923                            "Start AGG: sta %d tid %d queue %d - ssn = %d, next_recl = %d\n",
 924                            mvmsta->sta_id, tid, txq_id, tid_data->ssn,
 925                            tid_data->next_reclaimed);
 926
 927        if (tid_data->ssn == tid_data->next_reclaimed) {
 928                tid_data->state = IWL_AGG_STARTING;
 929                ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
 930        } else {
 931                tid_data->state = IWL_EMPTYING_HW_QUEUE_ADDBA;
 932        }
 933
 934        spin_unlock_bh(&mvmsta->lock);
 935
 936        return 0;
 937}
 938
 939int iwl_mvm_sta_tx_agg_oper(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
 940                            struct ieee80211_sta *sta, u16 tid, u8 buf_size)
 941{
 942        struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
 943        struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
 944        int queue, fifo, ret;
 945        u16 ssn;
 946
 947        buf_size = min_t(int, buf_size, LINK_QUAL_AGG_FRAME_LIMIT_DEF);
 948
 949        spin_lock_bh(&mvmsta->lock);
 950        ssn = tid_data->ssn;
 951        queue = tid_data->txq_id;
 952        tid_data->state = IWL_AGG_ON;
 953        tid_data->ssn = 0xffff;
 954        spin_unlock_bh(&mvmsta->lock);
 955
 956        fifo = iwl_mvm_ac_to_tx_fifo[tid_to_mac80211_ac[tid]];
 957
 958        ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
 959        if (ret)
 960                return -EIO;
 961
 962        iwl_trans_txq_enable(mvm->trans, queue, fifo, mvmsta->sta_id, tid,
 963                             buf_size, ssn);
 964
 965        /*
 966         * Even though in theory the peer could have different
 967         * aggregation reorder buffer sizes for different sessions,
 968         * our ucode doesn't allow for that and has a global limit
 969         * for each station. Therefore, use the minimum of all the
 970         * aggregation sessions and our default value.
 971         */
 972        mvmsta->max_agg_bufsize =
 973                min(mvmsta->max_agg_bufsize, buf_size);
 974        mvmsta->lq_sta.lq.agg_frame_cnt_limit = mvmsta->max_agg_bufsize;
 975
 976        IWL_DEBUG_HT(mvm, "Tx aggregation enabled on ra = %pM tid = %d\n",
 977                     sta->addr, tid);
 978
 979        return iwl_mvm_send_lq_cmd(mvm, &mvmsta->lq_sta.lq, false);
 980}
 981
 982int iwl_mvm_sta_tx_agg_stop(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
 983                            struct ieee80211_sta *sta, u16 tid)
 984{
 985        struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
 986        struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
 987        u16 txq_id;
 988        int err;
 989
 990
 991        /*
 992         * If mac80211 is cleaning its state, then say that we finished since
 993         * our state has been cleared anyway.
 994         */
 995        if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
 996                ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
 997                return 0;
 998        }
 999
1000        spin_lock_bh(&mvmsta->lock);
1001
1002        txq_id = tid_data->txq_id;
1003
1004        IWL_DEBUG_TX_QUEUES(mvm, "Stop AGG: sta %d tid %d q %d state %d\n",
1005                            mvmsta->sta_id, tid, txq_id, tid_data->state);
1006
1007        switch (tid_data->state) {
1008        case IWL_AGG_ON:
1009                tid_data->ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
1010
1011                IWL_DEBUG_TX_QUEUES(mvm,
1012                                    "ssn = %d, next_recl = %d\n",
1013                                    tid_data->ssn, tid_data->next_reclaimed);
1014
1015                /* There are still packets for this RA / TID in the HW */
1016                if (tid_data->ssn != tid_data->next_reclaimed) {
1017                        tid_data->state = IWL_EMPTYING_HW_QUEUE_DELBA;
1018                        err = 0;
1019                        break;
1020                }
1021
1022                tid_data->ssn = 0xffff;
1023                iwl_trans_txq_disable(mvm->trans, txq_id);
1024                /* fall through */
1025        case IWL_AGG_STARTING:
1026        case IWL_EMPTYING_HW_QUEUE_ADDBA:
1027                /*
1028                 * The agg session has been stopped before it was set up. This
1029                 * can happen when the AddBA timer times out for example.
1030                 */
1031
1032                /* No barriers since we are under mutex */
1033                lockdep_assert_held(&mvm->mutex);
1034                mvm->queue_to_mac80211[txq_id] = IWL_INVALID_MAC80211_QUEUE;
1035
1036                ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1037                tid_data->state = IWL_AGG_OFF;
1038                err = 0;
1039                break;
1040        default:
1041                IWL_ERR(mvm,
1042                        "Stopping AGG while state not ON or starting for %d on %d (%d)\n",
1043                        mvmsta->sta_id, tid, tid_data->state);
1044                IWL_ERR(mvm,
1045                        "\ttid_data->txq_id = %d\n", tid_data->txq_id);
1046                err = -EINVAL;
1047        }
1048
1049        spin_unlock_bh(&mvmsta->lock);
1050
1051        return err;
1052}
1053
1054int iwl_mvm_sta_tx_agg_flush(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1055                            struct ieee80211_sta *sta, u16 tid)
1056{
1057        struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1058        struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
1059        u16 txq_id;
1060        enum iwl_mvm_agg_state old_state;
1061
1062        /*
1063         * First set the agg state to OFF to avoid calling
1064         * ieee80211_stop_tx_ba_cb in iwl_mvm_check_ratid_empty.
1065         */
1066        spin_lock_bh(&mvmsta->lock);
1067        txq_id = tid_data->txq_id;
1068        IWL_DEBUG_TX_QUEUES(mvm, "Flush AGG: sta %d tid %d q %d state %d\n",
1069                            mvmsta->sta_id, tid, txq_id, tid_data->state);
1070        old_state = tid_data->state;
1071        tid_data->state = IWL_AGG_OFF;
1072        spin_unlock_bh(&mvmsta->lock);
1073
1074        if (old_state >= IWL_AGG_ON) {
1075                if (iwl_mvm_flush_tx_path(mvm, BIT(txq_id), true))
1076                        IWL_ERR(mvm, "Couldn't flush the AGG queue\n");
1077
1078                iwl_trans_txq_disable(mvm->trans, tid_data->txq_id);
1079        }
1080
1081        mvm->queue_to_mac80211[tid_data->txq_id] =
1082                                IWL_INVALID_MAC80211_QUEUE;
1083
1084        return 0;
1085}
1086
1087static int iwl_mvm_set_fw_key_idx(struct iwl_mvm *mvm)
1088{
1089        int i;
1090
1091        lockdep_assert_held(&mvm->mutex);
1092
1093        i = find_first_zero_bit(mvm->fw_key_table, STA_KEY_MAX_NUM);
1094
1095        if (i == STA_KEY_MAX_NUM)
1096                return STA_KEY_IDX_INVALID;
1097
1098        __set_bit(i, mvm->fw_key_table);
1099
1100        return i;
1101}
1102
1103static u8 iwl_mvm_get_key_sta_id(struct ieee80211_vif *vif,
1104                                 struct ieee80211_sta *sta)
1105{
1106        struct iwl_mvm_vif *mvmvif = (void *)vif->drv_priv;
1107
1108        if (sta) {
1109                struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
1110
1111                return mvm_sta->sta_id;
1112        }
1113
1114        /*
1115         * The device expects GTKs for station interfaces to be
1116         * installed as GTKs for the AP station. If we have no
1117         * station ID, then use AP's station ID.
1118         */
1119        if (vif->type == NL80211_IFTYPE_STATION &&
1120            mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT)
1121                return mvmvif->ap_sta_id;
1122
1123        return IWL_MVM_STATION_COUNT;
1124}
1125
1126static int iwl_mvm_send_sta_key(struct iwl_mvm *mvm,
1127                                struct iwl_mvm_sta *mvm_sta,
1128                                struct ieee80211_key_conf *keyconf,
1129                                u8 sta_id, u32 tkip_iv32, u16 *tkip_p1k,
1130                                u32 cmd_flags)
1131{
1132        __le16 key_flags;
1133        struct iwl_mvm_add_sta_key_cmd cmd = {};
1134        int ret, status;
1135        u16 keyidx;
1136        int i;
1137        u32 mac_id_n_color = mvm_sta->mac_id_n_color;
1138
1139        keyidx = (keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
1140                 STA_KEY_FLG_KEYID_MSK;
1141        key_flags = cpu_to_le16(keyidx);
1142        key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_KEY_MAP);
1143
1144        switch (keyconf->cipher) {
1145        case WLAN_CIPHER_SUITE_TKIP:
1146                key_flags |= cpu_to_le16(STA_KEY_FLG_TKIP);
1147                cmd.tkip_rx_tsc_byte2 = tkip_iv32;
1148                for (i = 0; i < 5; i++)
1149                        cmd.tkip_rx_ttak[i] = cpu_to_le16(tkip_p1k[i]);
1150                memcpy(cmd.key, keyconf->key, keyconf->keylen);
1151                break;
1152        case WLAN_CIPHER_SUITE_CCMP:
1153                key_flags |= cpu_to_le16(STA_KEY_FLG_CCM);
1154                memcpy(cmd.key, keyconf->key, keyconf->keylen);
1155                break;
1156        default:
1157                key_flags |= cpu_to_le16(STA_KEY_FLG_EXT);
1158                memcpy(cmd.key, keyconf->key, keyconf->keylen);
1159        }
1160
1161        if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
1162                key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
1163
1164        cmd.key_offset = keyconf->hw_key_idx;
1165        cmd.key_flags = key_flags;
1166        cmd.sta_id = sta_id;
1167
1168        status = ADD_STA_SUCCESS;
1169        if (cmd_flags == CMD_SYNC)
1170                ret = iwl_mvm_send_add_sta_key_cmd_status(mvm, &cmd,
1171                                                          mac_id_n_color,
1172                                                          &status);
1173        else
1174                ret = iwl_mvm_send_add_sta_key_cmd(mvm, CMD_ASYNC, &cmd,
1175                                                   mac_id_n_color);
1176
1177        switch (status) {
1178        case ADD_STA_SUCCESS:
1179                IWL_DEBUG_WEP(mvm, "MODIFY_STA: set dynamic key passed\n");
1180                break;
1181        default:
1182                ret = -EIO;
1183                IWL_ERR(mvm, "MODIFY_STA: set dynamic key failed\n");
1184                break;
1185        }
1186
1187        return ret;
1188}
1189
1190static int iwl_mvm_send_sta_igtk(struct iwl_mvm *mvm,
1191                                 struct ieee80211_key_conf *keyconf,
1192                                 u8 sta_id, bool remove_key)
1193{
1194        struct iwl_mvm_mgmt_mcast_key_cmd igtk_cmd = {};
1195
1196        /* verify the key details match the required command's expectations */
1197        if (WARN_ON((keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC) ||
1198                    (keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) ||
1199                    (keyconf->keyidx != 4 && keyconf->keyidx != 5)))
1200                return -EINVAL;
1201
1202        igtk_cmd.key_id = cpu_to_le32(keyconf->keyidx);
1203        igtk_cmd.sta_id = cpu_to_le32(sta_id);
1204
1205        if (remove_key) {
1206                igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_NOT_VALID);
1207        } else {
1208                struct ieee80211_key_seq seq;
1209                const u8 *pn;
1210
1211                memcpy(igtk_cmd.IGTK, keyconf->key, keyconf->keylen);
1212                ieee80211_aes_cmac_calculate_k1_k2(keyconf,
1213                                                   igtk_cmd.K1, igtk_cmd.K2);
1214                ieee80211_get_key_rx_seq(keyconf, 0, &seq);
1215                pn = seq.aes_cmac.pn;
1216                igtk_cmd.receive_seq_cnt = cpu_to_le64(((u64) pn[5] << 0) |
1217                                                       ((u64) pn[4] << 8) |
1218                                                       ((u64) pn[3] << 16) |
1219                                                       ((u64) pn[2] << 24) |
1220                                                       ((u64) pn[1] << 32) |
1221                                                       ((u64) pn[0] << 40));
1222        }
1223
1224        IWL_DEBUG_INFO(mvm, "%s igtk for sta %u\n",
1225                       remove_key ? "removing" : "installing",
1226                       igtk_cmd.sta_id);
1227
1228        return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, CMD_SYNC,
1229                                    sizeof(igtk_cmd), &igtk_cmd);
1230}
1231
1232
1233static inline u8 *iwl_mvm_get_mac_addr(struct iwl_mvm *mvm,
1234                                       struct ieee80211_vif *vif,
1235                                       struct ieee80211_sta *sta)
1236{
1237        struct iwl_mvm_vif *mvmvif = (void *)vif->drv_priv;
1238
1239        if (sta)
1240                return sta->addr;
1241
1242        if (vif->type == NL80211_IFTYPE_STATION &&
1243            mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) {
1244                u8 sta_id = mvmvif->ap_sta_id;
1245                sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1246                                                lockdep_is_held(&mvm->mutex));
1247                return sta->addr;
1248        }
1249
1250
1251        return NULL;
1252}
1253
1254int iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
1255                        struct ieee80211_vif *vif,
1256                        struct ieee80211_sta *sta,
1257                        struct ieee80211_key_conf *keyconf,
1258                        bool have_key_offset)
1259{
1260        struct iwl_mvm_sta *mvm_sta;
1261        int ret;
1262        u8 *addr, sta_id;
1263        struct ieee80211_key_seq seq;
1264        u16 p1k[5];
1265
1266        lockdep_assert_held(&mvm->mutex);
1267
1268        /* Get the station id from the mvm local station table */
1269        sta_id = iwl_mvm_get_key_sta_id(vif, sta);
1270        if (sta_id == IWL_MVM_STATION_COUNT) {
1271                IWL_ERR(mvm, "Failed to find station id\n");
1272                return -EINVAL;
1273        }
1274
1275        if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC) {
1276                ret = iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, false);
1277                goto end;
1278        }
1279
1280        /*
1281         * It is possible that the 'sta' parameter is NULL, and thus
1282         * there is a need to retrieve  the sta from the local station table.
1283         */
1284        if (!sta) {
1285                sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1286                                                lockdep_is_held(&mvm->mutex));
1287                if (IS_ERR_OR_NULL(sta)) {
1288                        IWL_ERR(mvm, "Invalid station id\n");
1289                        return -EINVAL;
1290                }
1291        }
1292
1293        mvm_sta = (struct iwl_mvm_sta *)sta->drv_priv;
1294        if (WARN_ON_ONCE(mvm_sta->vif != vif))
1295                return -EINVAL;
1296
1297        if (!have_key_offset) {
1298                /*
1299                 * The D3 firmware hardcodes the PTK offset to 0, so we have to
1300                 * configure it there. As a result, this workaround exists to
1301                 * let the caller set the key offset (hw_key_idx), see d3.c.
1302                 */
1303                keyconf->hw_key_idx = iwl_mvm_set_fw_key_idx(mvm);
1304                if (keyconf->hw_key_idx == STA_KEY_IDX_INVALID)
1305                        return -ENOSPC;
1306        }
1307
1308        switch (keyconf->cipher) {
1309        case WLAN_CIPHER_SUITE_TKIP:
1310                addr = iwl_mvm_get_mac_addr(mvm, vif, sta);
1311                /* get phase 1 key from mac80211 */
1312                ieee80211_get_key_rx_seq(keyconf, 0, &seq);
1313                ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
1314                ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, sta_id,
1315                                           seq.tkip.iv32, p1k, CMD_SYNC);
1316                break;
1317        case WLAN_CIPHER_SUITE_CCMP:
1318                ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, sta_id,
1319                                           0, NULL, CMD_SYNC);
1320                break;
1321        default:
1322                ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf,
1323                                           sta_id, 0, NULL, CMD_SYNC);
1324        }
1325
1326        if (ret)
1327                __clear_bit(keyconf->hw_key_idx, mvm->fw_key_table);
1328
1329end:
1330        IWL_DEBUG_WEP(mvm, "key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
1331                      keyconf->cipher, keyconf->keylen, keyconf->keyidx,
1332                      sta->addr, ret);
1333        return ret;
1334}
1335
1336int iwl_mvm_remove_sta_key(struct iwl_mvm *mvm,
1337                           struct ieee80211_vif *vif,
1338                           struct ieee80211_sta *sta,
1339                           struct ieee80211_key_conf *keyconf)
1340{
1341        struct iwl_mvm_sta *mvm_sta;
1342        struct iwl_mvm_add_sta_key_cmd cmd = {};
1343        __le16 key_flags;
1344        int ret, status;
1345        u8 sta_id;
1346
1347        lockdep_assert_held(&mvm->mutex);
1348
1349        /* Get the station id from the mvm local station table */
1350        sta_id = iwl_mvm_get_key_sta_id(vif, sta);
1351
1352        IWL_DEBUG_WEP(mvm, "mvm remove dynamic key: idx=%d sta=%d\n",
1353                      keyconf->keyidx, sta_id);
1354
1355        if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC)
1356                return iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, true);
1357
1358        ret = __test_and_clear_bit(keyconf->hw_key_idx, mvm->fw_key_table);
1359        if (!ret) {
1360                IWL_ERR(mvm, "offset %d not used in fw key table.\n",
1361                        keyconf->hw_key_idx);
1362                return -ENOENT;
1363        }
1364
1365        if (sta_id == IWL_MVM_STATION_COUNT) {
1366                IWL_DEBUG_WEP(mvm, "station non-existent, early return.\n");
1367                return 0;
1368        }
1369
1370        /*
1371         * It is possible that the 'sta' parameter is NULL, and thus
1372         * there is a need to retrieve the sta from the local station table,
1373         * for example when a GTK is removed (where the sta_id will then be
1374         * the AP ID, and no station was passed by mac80211.)
1375         */
1376        if (!sta) {
1377                sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1378                                                lockdep_is_held(&mvm->mutex));
1379                if (!sta) {
1380                        IWL_ERR(mvm, "Invalid station id\n");
1381                        return -EINVAL;
1382                }
1383        }
1384
1385        mvm_sta = (struct iwl_mvm_sta *)sta->drv_priv;
1386        if (WARN_ON_ONCE(mvm_sta->vif != vif))
1387                return -EINVAL;
1388
1389        key_flags = cpu_to_le16((keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
1390                                 STA_KEY_FLG_KEYID_MSK);
1391        key_flags |= cpu_to_le16(STA_KEY_FLG_NO_ENC | STA_KEY_FLG_WEP_KEY_MAP);
1392        key_flags |= cpu_to_le16(STA_KEY_NOT_VALID);
1393
1394        if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
1395                key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
1396
1397        cmd.key_flags = key_flags;
1398        cmd.key_offset = keyconf->hw_key_idx;
1399        cmd.sta_id = sta_id;
1400
1401        status = ADD_STA_SUCCESS;
1402        ret = iwl_mvm_send_add_sta_key_cmd_status(mvm, &cmd,
1403                                                  mvm_sta->mac_id_n_color,
1404                                                  &status);
1405
1406        switch (status) {
1407        case ADD_STA_SUCCESS:
1408                IWL_DEBUG_WEP(mvm, "MODIFY_STA: remove sta key passed\n");
1409                break;
1410        default:
1411                ret = -EIO;
1412                IWL_ERR(mvm, "MODIFY_STA: remove sta key failed\n");
1413                break;
1414        }
1415
1416        return ret;
1417}
1418
1419void iwl_mvm_update_tkip_key(struct iwl_mvm *mvm,
1420                             struct ieee80211_vif *vif,
1421                             struct ieee80211_key_conf *keyconf,
1422                             struct ieee80211_sta *sta, u32 iv32,
1423                             u16 *phase1key)
1424{
1425        struct iwl_mvm_sta *mvm_sta;
1426        u8 sta_id = iwl_mvm_get_key_sta_id(vif, sta);
1427
1428        if (WARN_ON_ONCE(sta_id == IWL_MVM_STATION_COUNT))
1429                return;
1430
1431        rcu_read_lock();
1432
1433        if (!sta) {
1434                sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
1435                if (WARN_ON(IS_ERR_OR_NULL(sta))) {
1436                        rcu_read_unlock();
1437                        return;
1438                }
1439        }
1440
1441        mvm_sta = (void *)sta->drv_priv;
1442        iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, sta_id,
1443                             iv32, phase1key, CMD_ASYNC);
1444        rcu_read_unlock();
1445}
1446
1447void iwl_mvm_sta_modify_ps_wake(struct iwl_mvm *mvm,
1448                                struct ieee80211_sta *sta)
1449{
1450        struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1451        struct iwl_mvm_add_sta_cmd_v7 cmd = {
1452                .add_modify = STA_MODE_MODIFY,
1453                .sta_id = mvmsta->sta_id,
1454                .station_flags_msk = cpu_to_le32(STA_FLG_PS),
1455                .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
1456        };
1457        int ret;
1458
1459        ret = iwl_mvm_send_add_sta_cmd(mvm, CMD_ASYNC, &cmd);
1460        if (ret)
1461                IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
1462}
1463
1464void iwl_mvm_sta_modify_sleep_tx_count(struct iwl_mvm *mvm,
1465                                       struct ieee80211_sta *sta,
1466                                       enum ieee80211_frame_release_type reason,
1467                                       u16 cnt, u16 tids, bool more_data,
1468                                       bool agg)
1469{
1470        struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1471        struct iwl_mvm_add_sta_cmd_v7 cmd = {
1472                .add_modify = STA_MODE_MODIFY,
1473                .sta_id = mvmsta->sta_id,
1474                .modify_mask = STA_MODIFY_SLEEPING_STA_TX_COUNT,
1475                .sleep_tx_count = cpu_to_le16(cnt),
1476                .mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color),
1477        };
1478        int tid, ret;
1479        unsigned long _tids = tids;
1480
1481        /* convert TIDs to ACs - we don't support TSPEC so that's OK
1482         * Note that this field is reserved and unused by firmware not
1483         * supporting GO uAPSD, so it's safe to always do this.
1484         */
1485        for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT)
1486                cmd.awake_acs |= BIT(tid_to_ucode_ac[tid]);
1487
1488        /* If we're releasing frames from aggregation queues then check if the
1489         * all queues combined that we're releasing frames from have
1490         *  - more frames than the service period, in which case more_data
1491         *    needs to be set
1492         *  - fewer than 'cnt' frames, in which case we need to adjust the
1493         *    firmware command (but do that unconditionally)
1494         */
1495        if (agg) {
1496                int remaining = cnt;
1497
1498                spin_lock_bh(&mvmsta->lock);
1499                for_each_set_bit(tid, &_tids, IWL_MAX_TID_COUNT) {
1500                        struct iwl_mvm_tid_data *tid_data;
1501                        u16 n_queued;
1502
1503                        tid_data = &mvmsta->tid_data[tid];
1504                        if (WARN(tid_data->state != IWL_AGG_ON &&
1505                                 tid_data->state != IWL_EMPTYING_HW_QUEUE_DELBA,
1506                                 "TID %d state is %d\n",
1507                                 tid, tid_data->state)) {
1508                                spin_unlock_bh(&mvmsta->lock);
1509                                ieee80211_sta_eosp(sta);
1510                                return;
1511                        }
1512
1513                        n_queued = iwl_mvm_tid_queued(tid_data);
1514                        if (n_queued > remaining) {
1515                                more_data = true;
1516                                remaining = 0;
1517                                break;
1518                        }
1519                        remaining -= n_queued;
1520                }
1521                spin_unlock_bh(&mvmsta->lock);
1522
1523                cmd.sleep_tx_count = cpu_to_le16(cnt - remaining);
1524                if (WARN_ON(cnt - remaining == 0)) {
1525                        ieee80211_sta_eosp(sta);
1526                        return;
1527                }
1528        }
1529
1530        /* Note: this is ignored by firmware not supporting GO uAPSD */
1531        if (more_data)
1532                cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_MOREDATA);
1533
1534        if (reason == IEEE80211_FRAME_RELEASE_PSPOLL) {
1535                mvmsta->next_status_eosp = true;
1536                cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_PS_POLL);
1537        } else {
1538                cmd.sleep_state_flags |= cpu_to_le16(STA_SLEEP_STATE_UAPSD);
1539        }
1540
1541        ret = iwl_mvm_send_add_sta_cmd(mvm, CMD_ASYNC, &cmd);
1542        if (ret)
1543                IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
1544}
1545
1546int iwl_mvm_rx_eosp_notif(struct iwl_mvm *mvm,
1547                          struct iwl_rx_cmd_buffer *rxb,
1548                          struct iwl_device_cmd *cmd)
1549{
1550        struct iwl_rx_packet *pkt = rxb_addr(rxb);
1551        struct iwl_mvm_eosp_notification *notif = (void *)pkt->data;
1552        struct ieee80211_sta *sta;
1553        u32 sta_id = le32_to_cpu(notif->sta_id);
1554
1555        if (WARN_ON_ONCE(sta_id >= IWL_MVM_STATION_COUNT))
1556                return 0;
1557
1558        rcu_read_lock();
1559        sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]);
1560        if (!IS_ERR_OR_NULL(sta))
1561                ieee80211_sta_eosp(sta);
1562        rcu_read_unlock();
1563
1564        return 0;
1565}
1566