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