linux/drivers/net/wireless/iwlwifi/dvm/mac80211.c
<<
>>
Prefs
   1/******************************************************************************
   2 *
   3 * Copyright(c) 2003 - 2013 Intel Corporation. All rights reserved.
   4 *
   5 * Portions of this file are derived from the ipw3945 project, as well
   6 * as portions of the ieee80211 subsystem header files.
   7 *
   8 * This program is free software; you can redistribute it and/or modify it
   9 * under the terms of version 2 of the GNU General Public License as
  10 * published by the Free Software Foundation.
  11 *
  12 * This program is distributed in the hope that it will be useful, but WITHOUT
  13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  15 * more details.
  16 *
  17 * You should have received a copy of the GNU General Public License along with
  18 * this program; if not, write to the Free Software Foundation, Inc.,
  19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
  20 *
  21 * The full GNU General Public License is included in this distribution in the
  22 * file called LICENSE.
  23 *
  24 * Contact Information:
  25 *  Intel Linux Wireless <ilw@linux.intel.com>
  26 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  27 *
  28 *****************************************************************************/
  29#include <linux/kernel.h>
  30#include <linux/module.h>
  31#include <linux/init.h>
  32#include <linux/slab.h>
  33#include <linux/dma-mapping.h>
  34#include <linux/delay.h>
  35#include <linux/sched.h>
  36#include <linux/skbuff.h>
  37#include <linux/netdevice.h>
  38#include <linux/etherdevice.h>
  39#include <linux/if_arp.h>
  40
  41#include <net/ieee80211_radiotap.h>
  42#include <net/mac80211.h>
  43
  44#include <asm/div64.h>
  45
  46#include "iwl-io.h"
  47#include "iwl-trans.h"
  48#include "iwl-op-mode.h"
  49#include "iwl-modparams.h"
  50
  51#include "dev.h"
  52#include "calib.h"
  53#include "agn.h"
  54
  55/*****************************************************************************
  56 *
  57 * mac80211 entry point functions
  58 *
  59 *****************************************************************************/
  60
  61static const struct ieee80211_iface_limit iwlagn_sta_ap_limits[] = {
  62        {
  63                .max = 1,
  64                .types = BIT(NL80211_IFTYPE_STATION),
  65        },
  66        {
  67                .max = 1,
  68                .types = BIT(NL80211_IFTYPE_AP),
  69        },
  70};
  71
  72static const struct ieee80211_iface_limit iwlagn_2sta_limits[] = {
  73        {
  74                .max = 2,
  75                .types = BIT(NL80211_IFTYPE_STATION),
  76        },
  77};
  78
  79static const struct ieee80211_iface_limit iwlagn_p2p_sta_go_limits[] = {
  80        {
  81                .max = 1,
  82                .types = BIT(NL80211_IFTYPE_STATION),
  83        },
  84        {
  85                .max = 1,
  86                .types = BIT(NL80211_IFTYPE_P2P_GO) |
  87                         BIT(NL80211_IFTYPE_AP),
  88        },
  89};
  90
  91static const struct ieee80211_iface_limit iwlagn_p2p_2sta_limits[] = {
  92        {
  93                .max = 2,
  94                .types = BIT(NL80211_IFTYPE_STATION),
  95        },
  96        {
  97                .max = 1,
  98                .types = BIT(NL80211_IFTYPE_P2P_CLIENT),
  99        },
 100};
 101
 102static const struct ieee80211_iface_combination
 103iwlagn_iface_combinations_dualmode[] = {
 104        { .num_different_channels = 1,
 105          .max_interfaces = 2,
 106          .beacon_int_infra_match = true,
 107          .limits = iwlagn_sta_ap_limits,
 108          .n_limits = ARRAY_SIZE(iwlagn_sta_ap_limits),
 109        },
 110        { .num_different_channels = 1,
 111          .max_interfaces = 2,
 112          .limits = iwlagn_2sta_limits,
 113          .n_limits = ARRAY_SIZE(iwlagn_2sta_limits),
 114        },
 115};
 116
 117static const struct ieee80211_iface_combination
 118iwlagn_iface_combinations_p2p[] = {
 119        { .num_different_channels = 1,
 120          .max_interfaces = 2,
 121          .beacon_int_infra_match = true,
 122          .limits = iwlagn_p2p_sta_go_limits,
 123          .n_limits = ARRAY_SIZE(iwlagn_p2p_sta_go_limits),
 124        },
 125        { .num_different_channels = 1,
 126          .max_interfaces = 2,
 127          .limits = iwlagn_p2p_2sta_limits,
 128          .n_limits = ARRAY_SIZE(iwlagn_p2p_2sta_limits),
 129        },
 130};
 131
 132/*
 133 * Not a mac80211 entry point function, but it fits in with all the
 134 * other mac80211 functions grouped here.
 135 */
 136int iwlagn_mac_setup_register(struct iwl_priv *priv,
 137                              const struct iwl_ucode_capabilities *capa)
 138{
 139        int ret;
 140        struct ieee80211_hw *hw = priv->hw;
 141        struct iwl_rxon_context *ctx;
 142
 143        hw->rate_control_algorithm = "iwl-agn-rs";
 144
 145        /* Tell mac80211 our characteristics */
 146        hw->flags = IEEE80211_HW_SIGNAL_DBM |
 147                    IEEE80211_HW_AMPDU_AGGREGATION |
 148                    IEEE80211_HW_NEED_DTIM_BEFORE_ASSOC |
 149                    IEEE80211_HW_SPECTRUM_MGMT |
 150                    IEEE80211_HW_REPORTS_TX_ACK_STATUS |
 151                    IEEE80211_HW_QUEUE_CONTROL |
 152                    IEEE80211_HW_SUPPORTS_PS |
 153                    IEEE80211_HW_SUPPORTS_DYNAMIC_PS |
 154                    IEEE80211_HW_WANT_MONITOR_VIF;
 155
 156        hw->offchannel_tx_hw_queue = IWL_AUX_QUEUE;
 157        hw->radiotap_mcs_details |= IEEE80211_RADIOTAP_MCS_HAVE_FMT;
 158
 159        /*
 160         * Including the following line will crash some AP's.  This
 161         * workaround removes the stimulus which causes the crash until
 162         * the AP software can be fixed.
 163        hw->max_tx_aggregation_subframes = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
 164         */
 165
 166        if (priv->nvm_data->sku_cap_11n_enable)
 167                hw->flags |= IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS |
 168                             IEEE80211_HW_SUPPORTS_STATIC_SMPS;
 169
 170        /*
 171         * Enable 11w if advertised by firmware and software crypto
 172         * is not enabled (as the firmware will interpret some mgmt
 173         * packets, so enabling it with software crypto isn't safe)
 174         */
 175        if (priv->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_MFP &&
 176            !iwlwifi_mod_params.sw_crypto)
 177                hw->flags |= IEEE80211_HW_MFP_CAPABLE;
 178
 179        hw->sta_data_size = sizeof(struct iwl_station_priv);
 180        hw->vif_data_size = sizeof(struct iwl_vif_priv);
 181
 182        for_each_context(priv, ctx) {
 183                hw->wiphy->interface_modes |= ctx->interface_modes;
 184                hw->wiphy->interface_modes |= ctx->exclusive_interface_modes;
 185        }
 186
 187        BUILD_BUG_ON(NUM_IWL_RXON_CTX != 2);
 188
 189        if (hw->wiphy->interface_modes & BIT(NL80211_IFTYPE_P2P_CLIENT)) {
 190                hw->wiphy->iface_combinations = iwlagn_iface_combinations_p2p;
 191                hw->wiphy->n_iface_combinations =
 192                        ARRAY_SIZE(iwlagn_iface_combinations_p2p);
 193        } else if (hw->wiphy->interface_modes & BIT(NL80211_IFTYPE_AP)) {
 194                hw->wiphy->iface_combinations =
 195                        iwlagn_iface_combinations_dualmode;
 196                hw->wiphy->n_iface_combinations =
 197                        ARRAY_SIZE(iwlagn_iface_combinations_dualmode);
 198        }
 199
 200        hw->wiphy->max_remain_on_channel_duration = 500;
 201
 202        hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY |
 203                            WIPHY_FLAG_DISABLE_BEACON_HINTS |
 204                            WIPHY_FLAG_IBSS_RSN;
 205
 206#ifdef CONFIG_PM_SLEEP
 207        if (priv->fw->img[IWL_UCODE_WOWLAN].sec[0].len &&
 208            priv->trans->ops->d3_suspend &&
 209            priv->trans->ops->d3_resume &&
 210            device_can_wakeup(priv->trans->dev)) {
 211                priv->wowlan_support.flags = WIPHY_WOWLAN_MAGIC_PKT |
 212                                             WIPHY_WOWLAN_DISCONNECT |
 213                                             WIPHY_WOWLAN_EAP_IDENTITY_REQ |
 214                                             WIPHY_WOWLAN_RFKILL_RELEASE;
 215                if (!iwlwifi_mod_params.sw_crypto)
 216                        priv->wowlan_support.flags |=
 217                                WIPHY_WOWLAN_SUPPORTS_GTK_REKEY |
 218                                WIPHY_WOWLAN_GTK_REKEY_FAILURE;
 219
 220                priv->wowlan_support.n_patterns = IWLAGN_WOWLAN_MAX_PATTERNS;
 221                priv->wowlan_support.pattern_min_len =
 222                                        IWLAGN_WOWLAN_MIN_PATTERN_LEN;
 223                priv->wowlan_support.pattern_max_len =
 224                                        IWLAGN_WOWLAN_MAX_PATTERN_LEN;
 225                hw->wiphy->wowlan = &priv->wowlan_support;
 226        }
 227#endif
 228
 229        if (iwlwifi_mod_params.power_save)
 230                hw->wiphy->flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT;
 231        else
 232                hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
 233
 234        hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX;
 235        /* we create the 802.11 header and a max-length SSID element */
 236        hw->wiphy->max_scan_ie_len = capa->max_probe_length - 24 - 34;
 237
 238        /*
 239         * We don't use all queues: 4 and 9 are unused and any
 240         * aggregation queue gets mapped down to the AC queue.
 241         */
 242        hw->queues = IWLAGN_FIRST_AMPDU_QUEUE;
 243
 244        hw->max_listen_interval = IWL_CONN_MAX_LISTEN_INTERVAL;
 245
 246        if (priv->nvm_data->bands[IEEE80211_BAND_2GHZ].n_channels)
 247                priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
 248                        &priv->nvm_data->bands[IEEE80211_BAND_2GHZ];
 249        if (priv->nvm_data->bands[IEEE80211_BAND_5GHZ].n_channels)
 250                priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
 251                        &priv->nvm_data->bands[IEEE80211_BAND_5GHZ];
 252
 253        hw->wiphy->hw_version = priv->trans->hw_id;
 254
 255        iwl_leds_init(priv);
 256
 257        ret = ieee80211_register_hw(priv->hw);
 258        if (ret) {
 259                IWL_ERR(priv, "Failed to register hw (error %d)\n", ret);
 260                iwl_leds_exit(priv);
 261                return ret;
 262        }
 263        priv->mac80211_registered = 1;
 264
 265        return 0;
 266}
 267
 268void iwlagn_mac_unregister(struct iwl_priv *priv)
 269{
 270        if (!priv->mac80211_registered)
 271                return;
 272        iwl_leds_exit(priv);
 273        ieee80211_unregister_hw(priv->hw);
 274        priv->mac80211_registered = 0;
 275}
 276
 277static int __iwl_up(struct iwl_priv *priv)
 278{
 279        struct iwl_rxon_context *ctx;
 280        int ret;
 281
 282        lockdep_assert_held(&priv->mutex);
 283
 284        if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
 285                IWL_WARN(priv, "Exit pending; will not bring the NIC up\n");
 286                return -EIO;
 287        }
 288
 289        for_each_context(priv, ctx) {
 290                ret = iwlagn_alloc_bcast_station(priv, ctx);
 291                if (ret) {
 292                        iwl_dealloc_bcast_stations(priv);
 293                        return ret;
 294                }
 295        }
 296
 297        ret = iwl_run_init_ucode(priv);
 298        if (ret) {
 299                IWL_ERR(priv, "Failed to run INIT ucode: %d\n", ret);
 300                goto error;
 301        }
 302
 303        ret = iwl_load_ucode_wait_alive(priv, IWL_UCODE_REGULAR);
 304        if (ret) {
 305                IWL_ERR(priv, "Failed to start RT ucode: %d\n", ret);
 306                goto error;
 307        }
 308
 309        ret = iwl_alive_start(priv);
 310        if (ret)
 311                goto error;
 312        return 0;
 313
 314 error:
 315        set_bit(STATUS_EXIT_PENDING, &priv->status);
 316        iwl_down(priv);
 317        clear_bit(STATUS_EXIT_PENDING, &priv->status);
 318
 319        IWL_ERR(priv, "Unable to initialize device.\n");
 320        return ret;
 321}
 322
 323static int iwlagn_mac_start(struct ieee80211_hw *hw)
 324{
 325        struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
 326        int ret;
 327
 328        IWL_DEBUG_MAC80211(priv, "enter\n");
 329
 330        /* we should be verifying the device is ready to be opened */
 331        mutex_lock(&priv->mutex);
 332        ret = __iwl_up(priv);
 333        mutex_unlock(&priv->mutex);
 334        if (ret)
 335                return ret;
 336
 337        IWL_DEBUG_INFO(priv, "Start UP work done.\n");
 338
 339        /* Now we should be done, and the READY bit should be set. */
 340        if (WARN_ON(!test_bit(STATUS_READY, &priv->status)))
 341                ret = -EIO;
 342
 343        iwlagn_led_enable(priv);
 344
 345        priv->is_open = 1;
 346        IWL_DEBUG_MAC80211(priv, "leave\n");
 347        return 0;
 348}
 349
 350static void iwlagn_mac_stop(struct ieee80211_hw *hw)
 351{
 352        struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
 353
 354        IWL_DEBUG_MAC80211(priv, "enter\n");
 355
 356        if (!priv->is_open)
 357                return;
 358
 359        priv->is_open = 0;
 360
 361        mutex_lock(&priv->mutex);
 362        iwl_down(priv);
 363        mutex_unlock(&priv->mutex);
 364
 365        iwl_cancel_deferred_work(priv);
 366
 367        flush_workqueue(priv->workqueue);
 368
 369        /* User space software may expect getting rfkill changes
 370         * even if interface is down, trans->down will leave the RF
 371         * kill interrupt enabled
 372         */
 373        iwl_trans_stop_hw(priv->trans, false);
 374
 375        IWL_DEBUG_MAC80211(priv, "leave\n");
 376}
 377
 378static void iwlagn_mac_set_rekey_data(struct ieee80211_hw *hw,
 379                                      struct ieee80211_vif *vif,
 380                                      struct cfg80211_gtk_rekey_data *data)
 381{
 382        struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
 383
 384        if (iwlwifi_mod_params.sw_crypto)
 385                return;
 386
 387        IWL_DEBUG_MAC80211(priv, "enter\n");
 388        mutex_lock(&priv->mutex);
 389
 390        if (priv->contexts[IWL_RXON_CTX_BSS].vif != vif)
 391                goto out;
 392
 393        memcpy(priv->kek, data->kek, NL80211_KEK_LEN);
 394        memcpy(priv->kck, data->kck, NL80211_KCK_LEN);
 395        priv->replay_ctr =
 396                cpu_to_le64(be64_to_cpup((__be64 *)&data->replay_ctr));
 397        priv->have_rekey_data = true;
 398
 399 out:
 400        mutex_unlock(&priv->mutex);
 401        IWL_DEBUG_MAC80211(priv, "leave\n");
 402}
 403
 404#ifdef CONFIG_PM_SLEEP
 405
 406static int iwlagn_mac_suspend(struct ieee80211_hw *hw,
 407                              struct cfg80211_wowlan *wowlan)
 408{
 409        struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
 410        struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
 411        int ret;
 412
 413        if (WARN_ON(!wowlan))
 414                return -EINVAL;
 415
 416        IWL_DEBUG_MAC80211(priv, "enter\n");
 417        mutex_lock(&priv->mutex);
 418
 419        /* Don't attempt WoWLAN when not associated, tear down instead. */
 420        if (!ctx->vif || ctx->vif->type != NL80211_IFTYPE_STATION ||
 421            !iwl_is_associated_ctx(ctx)) {
 422                ret = 1;
 423                goto out;
 424        }
 425
 426        ret = iwlagn_suspend(priv, wowlan);
 427        if (ret)
 428                goto error;
 429
 430        /* let the ucode operate on its own */
 431        iwl_write32(priv->trans, CSR_UCODE_DRV_GP1_SET,
 432                    CSR_UCODE_DRV_GP1_BIT_D3_CFG_COMPLETE);
 433
 434        iwl_trans_d3_suspend(priv->trans, false);
 435
 436        goto out;
 437
 438 error:
 439        priv->wowlan = false;
 440        iwlagn_prepare_restart(priv);
 441        ieee80211_restart_hw(priv->hw);
 442 out:
 443        mutex_unlock(&priv->mutex);
 444        IWL_DEBUG_MAC80211(priv, "leave\n");
 445
 446        return ret;
 447}
 448
 449struct iwl_resume_data {
 450        struct iwl_priv *priv;
 451        struct iwlagn_wowlan_status *cmd;
 452        bool valid;
 453};
 454
 455static bool iwl_resume_status_fn(struct iwl_notif_wait_data *notif_wait,
 456                                 struct iwl_rx_packet *pkt, void *data)
 457{
 458        struct iwl_resume_data *resume_data = data;
 459        struct iwl_priv *priv = resume_data->priv;
 460        u32 len = le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
 461
 462        if (len - 4 != sizeof(*resume_data->cmd)) {
 463                IWL_ERR(priv, "rx wrong size data\n");
 464                return true;
 465        }
 466        memcpy(resume_data->cmd, pkt->data, sizeof(*resume_data->cmd));
 467        resume_data->valid = true;
 468
 469        return true;
 470}
 471
 472static int iwlagn_mac_resume(struct ieee80211_hw *hw)
 473{
 474        struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
 475        struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
 476        struct ieee80211_vif *vif;
 477        u32 base;
 478        int ret;
 479        enum iwl_d3_status d3_status;
 480        struct error_table_start {
 481                /* cf. struct iwl_error_event_table */
 482                u32 valid;
 483                u32 error_id;
 484        } err_info;
 485        struct iwl_notification_wait status_wait;
 486        static const u8 status_cmd[] = {
 487                REPLY_WOWLAN_GET_STATUS,
 488        };
 489        struct iwlagn_wowlan_status status_data = {};
 490        struct iwl_resume_data resume_data = {
 491                .priv = priv,
 492                .cmd = &status_data,
 493                .valid = false,
 494        };
 495        struct cfg80211_wowlan_wakeup wakeup = {
 496                .pattern_idx = -1,
 497        };
 498#ifdef CONFIG_IWLWIFI_DEBUGFS
 499        const struct fw_img *img;
 500#endif
 501
 502        IWL_DEBUG_MAC80211(priv, "enter\n");
 503        mutex_lock(&priv->mutex);
 504
 505        /* we'll clear ctx->vif during iwlagn_prepare_restart() */
 506        vif = ctx->vif;
 507
 508        ret = iwl_trans_d3_resume(priv->trans, &d3_status, false);
 509        if (ret)
 510                goto out_unlock;
 511
 512        if (d3_status != IWL_D3_STATUS_ALIVE) {
 513                IWL_INFO(priv, "Device was reset during suspend\n");
 514                goto out_unlock;
 515        }
 516
 517        /* uCode is no longer operating by itself */
 518        iwl_write32(priv->trans, CSR_UCODE_DRV_GP1_CLR,
 519                    CSR_UCODE_DRV_GP1_BIT_D3_CFG_COMPLETE);
 520
 521        base = priv->device_pointers.error_event_table;
 522        if (!iwlagn_hw_valid_rtc_data_addr(base)) {
 523                IWL_WARN(priv, "Invalid error table during resume!\n");
 524                goto out_unlock;
 525        }
 526
 527        iwl_trans_read_mem_bytes(priv->trans, base,
 528                                 &err_info, sizeof(err_info));
 529
 530        if (err_info.valid) {
 531                IWL_INFO(priv, "error table is valid (%d, 0x%x)\n",
 532                         err_info.valid, err_info.error_id);
 533                if (err_info.error_id == RF_KILL_INDICATOR_FOR_WOWLAN) {
 534                        wakeup.rfkill_release = true;
 535                        ieee80211_report_wowlan_wakeup(vif, &wakeup,
 536                                                       GFP_KERNEL);
 537                }
 538                goto out_unlock;
 539        }
 540
 541#ifdef CONFIG_IWLWIFI_DEBUGFS
 542        img = &priv->fw->img[IWL_UCODE_WOWLAN];
 543        if (!priv->wowlan_sram)
 544                priv->wowlan_sram =
 545                        kzalloc(img->sec[IWL_UCODE_SECTION_DATA].len,
 546                                GFP_KERNEL);
 547
 548        if (priv->wowlan_sram)
 549                iwl_trans_read_mem(priv->trans, 0x800000,
 550                                   priv->wowlan_sram,
 551                                   img->sec[IWL_UCODE_SECTION_DATA].len / 4);
 552#endif
 553
 554        /*
 555         * This is very strange. The GET_STATUS command is sent but the device
 556         * doesn't reply properly, it seems it doesn't close the RBD so one is
 557         * always left open ... As a result, we need to send another command
 558         * and have to reset the driver afterwards. As we need to switch to
 559         * runtime firmware again that'll happen.
 560         */
 561
 562        iwl_init_notification_wait(&priv->notif_wait, &status_wait, status_cmd,
 563                                   ARRAY_SIZE(status_cmd), iwl_resume_status_fn,
 564                                   &resume_data);
 565
 566        iwl_dvm_send_cmd_pdu(priv, REPLY_WOWLAN_GET_STATUS, CMD_ASYNC, 0, NULL);
 567        iwl_dvm_send_cmd_pdu(priv, REPLY_ECHO, CMD_ASYNC, 0, NULL);
 568        /* an RBD is left open in the firmware now! */
 569
 570        ret = iwl_wait_notification(&priv->notif_wait, &status_wait, HZ/5);
 571        if (ret)
 572                goto out_unlock;
 573
 574        if (resume_data.valid && priv->contexts[IWL_RXON_CTX_BSS].vif) {
 575                u32 reasons = le32_to_cpu(status_data.wakeup_reason);
 576                struct cfg80211_wowlan_wakeup *wakeup_report;
 577
 578                IWL_INFO(priv, "WoWLAN wakeup reason(s): 0x%.8x\n", reasons);
 579
 580                if (reasons) {
 581                        if (reasons & IWLAGN_WOWLAN_WAKEUP_MAGIC_PACKET)
 582                                wakeup.magic_pkt = true;
 583                        if (reasons & IWLAGN_WOWLAN_WAKEUP_PATTERN_MATCH)
 584                                wakeup.pattern_idx = status_data.pattern_number;
 585                        if (reasons & (IWLAGN_WOWLAN_WAKEUP_BEACON_MISS |
 586                                       IWLAGN_WOWLAN_WAKEUP_LINK_CHANGE))
 587                                wakeup.disconnect = true;
 588                        if (reasons & IWLAGN_WOWLAN_WAKEUP_GTK_REKEY_FAIL)
 589                                wakeup.gtk_rekey_failure = true;
 590                        if (reasons & IWLAGN_WOWLAN_WAKEUP_EAP_IDENT_REQ)
 591                                wakeup.eap_identity_req = true;
 592                        if (reasons & IWLAGN_WOWLAN_WAKEUP_4WAY_HANDSHAKE)
 593                                wakeup.four_way_handshake = true;
 594                        wakeup_report = &wakeup;
 595                } else {
 596                        wakeup_report = NULL;
 597                }
 598
 599                ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
 600        }
 601
 602        priv->wowlan = false;
 603
 604        iwlagn_prepare_restart(priv);
 605
 606        memset((void *)&ctx->active, 0, sizeof(ctx->active));
 607        iwl_connection_init_rx_config(priv, ctx);
 608        iwlagn_set_rxon_chain(priv, ctx);
 609
 610 out_unlock:
 611        mutex_unlock(&priv->mutex);
 612        IWL_DEBUG_MAC80211(priv, "leave\n");
 613
 614        ieee80211_resume_disconnect(vif);
 615
 616        return 1;
 617}
 618
 619static void iwlagn_mac_set_wakeup(struct ieee80211_hw *hw, bool enabled)
 620{
 621        struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
 622
 623        device_set_wakeup_enable(priv->trans->dev, enabled);
 624}
 625#endif
 626
 627static void iwlagn_mac_tx(struct ieee80211_hw *hw,
 628                          struct ieee80211_tx_control *control,
 629                          struct sk_buff *skb)
 630{
 631        struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
 632
 633        if (iwlagn_tx_skb(priv, control->sta, skb))
 634                ieee80211_free_txskb(hw, skb);
 635}
 636
 637static void iwlagn_mac_update_tkip_key(struct ieee80211_hw *hw,
 638                                       struct ieee80211_vif *vif,
 639                                       struct ieee80211_key_conf *keyconf,
 640                                       struct ieee80211_sta *sta,
 641                                       u32 iv32, u16 *phase1key)
 642{
 643        struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
 644
 645        iwl_update_tkip_key(priv, vif, keyconf, sta, iv32, phase1key);
 646}
 647
 648static int iwlagn_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
 649                              struct ieee80211_vif *vif,
 650                              struct ieee80211_sta *sta,
 651                              struct ieee80211_key_conf *key)
 652{
 653        struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
 654        struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
 655        struct iwl_rxon_context *ctx = vif_priv->ctx;
 656        int ret;
 657        bool is_default_wep_key = false;
 658
 659        IWL_DEBUG_MAC80211(priv, "enter\n");
 660
 661        if (iwlwifi_mod_params.sw_crypto) {
 662                IWL_DEBUG_MAC80211(priv, "leave - hwcrypto disabled\n");
 663                return -EOPNOTSUPP;
 664        }
 665
 666        switch (key->cipher) {
 667        case WLAN_CIPHER_SUITE_TKIP:
 668                key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
 669                /* fall through */
 670        case WLAN_CIPHER_SUITE_CCMP:
 671                key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
 672                break;
 673        default:
 674                break;
 675        }
 676
 677        /*
 678         * We could program these keys into the hardware as well, but we
 679         * don't expect much multicast traffic in IBSS and having keys
 680         * for more stations is probably more useful.
 681         *
 682         * Mark key TX-only and return 0.
 683         */
 684        if (vif->type == NL80211_IFTYPE_ADHOC &&
 685            !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
 686                key->hw_key_idx = WEP_INVALID_OFFSET;
 687                return 0;
 688        }
 689
 690        /* If they key was TX-only, accept deletion */
 691        if (cmd == DISABLE_KEY && key->hw_key_idx == WEP_INVALID_OFFSET)
 692                return 0;
 693
 694        mutex_lock(&priv->mutex);
 695        iwl_scan_cancel_timeout(priv, 100);
 696
 697        BUILD_BUG_ON(WEP_INVALID_OFFSET == IWLAGN_HW_KEY_DEFAULT);
 698
 699        /*
 700         * If we are getting WEP group key and we didn't receive any key mapping
 701         * so far, we are in legacy wep mode (group key only), otherwise we are
 702         * in 1X mode.
 703         * In legacy wep mode, we use another host command to the uCode.
 704         */
 705        if ((key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
 706             key->cipher == WLAN_CIPHER_SUITE_WEP104) && !sta) {
 707                if (cmd == SET_KEY)
 708                        is_default_wep_key = !ctx->key_mapping_keys;
 709                else
 710                        is_default_wep_key =
 711                                key->hw_key_idx == IWLAGN_HW_KEY_DEFAULT;
 712        }
 713
 714
 715        switch (cmd) {
 716        case SET_KEY:
 717                if (is_default_wep_key) {
 718                        ret = iwl_set_default_wep_key(priv, vif_priv->ctx, key);
 719                        break;
 720                }
 721                ret = iwl_set_dynamic_key(priv, vif_priv->ctx, key, sta);
 722                if (ret) {
 723                        /*
 724                         * can't add key for RX, but we don't need it
 725                         * in the device for TX so still return 0
 726                         */
 727                        ret = 0;
 728                        key->hw_key_idx = WEP_INVALID_OFFSET;
 729                }
 730
 731                IWL_DEBUG_MAC80211(priv, "enable hwcrypto key\n");
 732                break;
 733        case DISABLE_KEY:
 734                if (is_default_wep_key)
 735                        ret = iwl_remove_default_wep_key(priv, ctx, key);
 736                else
 737                        ret = iwl_remove_dynamic_key(priv, ctx, key, sta);
 738
 739                IWL_DEBUG_MAC80211(priv, "disable hwcrypto key\n");
 740                break;
 741        default:
 742                ret = -EINVAL;
 743        }
 744
 745        mutex_unlock(&priv->mutex);
 746        IWL_DEBUG_MAC80211(priv, "leave\n");
 747
 748        return ret;
 749}
 750
 751static int iwlagn_mac_ampdu_action(struct ieee80211_hw *hw,
 752                                   struct ieee80211_vif *vif,
 753                                   enum ieee80211_ampdu_mlme_action action,
 754                                   struct ieee80211_sta *sta, u16 tid, u16 *ssn,
 755                                   u8 buf_size)
 756{
 757        struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
 758        int ret = -EINVAL;
 759        struct iwl_station_priv *sta_priv = (void *) sta->drv_priv;
 760
 761        IWL_DEBUG_HT(priv, "A-MPDU action on addr %pM tid %d\n",
 762                     sta->addr, tid);
 763
 764        if (!(priv->nvm_data->sku_cap_11n_enable))
 765                return -EACCES;
 766
 767        IWL_DEBUG_MAC80211(priv, "enter\n");
 768        mutex_lock(&priv->mutex);
 769
 770        switch (action) {
 771        case IEEE80211_AMPDU_RX_START:
 772                if (iwlwifi_mod_params.disable_11n & IWL_DISABLE_HT_RXAGG)
 773                        break;
 774                IWL_DEBUG_HT(priv, "start Rx\n");
 775                ret = iwl_sta_rx_agg_start(priv, sta, tid, *ssn);
 776                break;
 777        case IEEE80211_AMPDU_RX_STOP:
 778                IWL_DEBUG_HT(priv, "stop Rx\n");
 779                ret = iwl_sta_rx_agg_stop(priv, sta, tid);
 780                break;
 781        case IEEE80211_AMPDU_TX_START:
 782                if (!priv->trans->ops->txq_enable)
 783                        break;
 784                if (iwlwifi_mod_params.disable_11n & IWL_DISABLE_HT_TXAGG)
 785                        break;
 786                IWL_DEBUG_HT(priv, "start Tx\n");
 787                ret = iwlagn_tx_agg_start(priv, vif, sta, tid, ssn);
 788                break;
 789        case IEEE80211_AMPDU_TX_STOP_FLUSH:
 790        case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
 791                IWL_DEBUG_HT(priv, "Flush Tx\n");
 792                ret = iwlagn_tx_agg_flush(priv, vif, sta, tid);
 793                break;
 794        case IEEE80211_AMPDU_TX_STOP_CONT:
 795                IWL_DEBUG_HT(priv, "stop Tx\n");
 796                ret = iwlagn_tx_agg_stop(priv, vif, sta, tid);
 797                if ((ret == 0) && (priv->agg_tids_count > 0)) {
 798                        priv->agg_tids_count--;
 799                        IWL_DEBUG_HT(priv, "priv->agg_tids_count = %u\n",
 800                                     priv->agg_tids_count);
 801                }
 802                if (!priv->agg_tids_count &&
 803                    priv->hw_params.use_rts_for_aggregation) {
 804                        /*
 805                         * switch off RTS/CTS if it was previously enabled
 806                         */
 807                        sta_priv->lq_sta.lq.general_params.flags &=
 808                                ~LINK_QUAL_FLAGS_SET_STA_TLC_RTS_MSK;
 809                        iwl_send_lq_cmd(priv, iwl_rxon_ctx_from_vif(vif),
 810                                        &sta_priv->lq_sta.lq, CMD_ASYNC, false);
 811                }
 812                break;
 813        case IEEE80211_AMPDU_TX_OPERATIONAL:
 814                ret = iwlagn_tx_agg_oper(priv, vif, sta, tid, buf_size);
 815                break;
 816        }
 817        mutex_unlock(&priv->mutex);
 818        IWL_DEBUG_MAC80211(priv, "leave\n");
 819        return ret;
 820}
 821
 822static int iwlagn_mac_sta_add(struct ieee80211_hw *hw,
 823                              struct ieee80211_vif *vif,
 824                              struct ieee80211_sta *sta)
 825{
 826        struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
 827        struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
 828        struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
 829        bool is_ap = vif->type == NL80211_IFTYPE_STATION;
 830        int ret;
 831        u8 sta_id;
 832
 833        IWL_DEBUG_INFO(priv, "proceeding to add station %pM\n",
 834                        sta->addr);
 835        sta_priv->sta_id = IWL_INVALID_STATION;
 836
 837        atomic_set(&sta_priv->pending_frames, 0);
 838        if (vif->type == NL80211_IFTYPE_AP)
 839                sta_priv->client = true;
 840
 841        ret = iwl_add_station_common(priv, vif_priv->ctx, sta->addr,
 842                                     is_ap, sta, &sta_id);
 843        if (ret) {
 844                IWL_ERR(priv, "Unable to add station %pM (%d)\n",
 845                        sta->addr, ret);
 846                /* Should we return success if return code is EEXIST ? */
 847                return ret;
 848        }
 849
 850        sta_priv->sta_id = sta_id;
 851
 852        return 0;
 853}
 854
 855static int iwlagn_mac_sta_remove(struct ieee80211_hw *hw,
 856                                 struct ieee80211_vif *vif,
 857                                 struct ieee80211_sta *sta)
 858{
 859        struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
 860        struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
 861        int ret;
 862
 863        IWL_DEBUG_INFO(priv, "proceeding to remove station %pM\n", sta->addr);
 864
 865        if (vif->type == NL80211_IFTYPE_STATION) {
 866                /*
 867                 * Station will be removed from device when the RXON
 868                 * is set to unassociated -- just deactivate it here
 869                 * to avoid re-programming it.
 870                 */
 871                ret = 0;
 872                iwl_deactivate_station(priv, sta_priv->sta_id, sta->addr);
 873        } else {
 874                ret = iwl_remove_station(priv, sta_priv->sta_id, sta->addr);
 875                if (ret)
 876                        IWL_DEBUG_QUIET_RFKILL(priv,
 877                                "Error removing station %pM\n", sta->addr);
 878        }
 879        return ret;
 880}
 881
 882static int iwlagn_mac_sta_state(struct ieee80211_hw *hw,
 883                                struct ieee80211_vif *vif,
 884                                struct ieee80211_sta *sta,
 885                                enum ieee80211_sta_state old_state,
 886                                enum ieee80211_sta_state new_state)
 887{
 888        struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
 889        struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
 890        enum {
 891                NONE, ADD, REMOVE, HT_RATE_INIT, ADD_RATE_INIT,
 892        } op = NONE;
 893        int ret;
 894
 895        IWL_DEBUG_MAC80211(priv, "station %pM state change %d->%d\n",
 896                           sta->addr, old_state, new_state);
 897
 898        mutex_lock(&priv->mutex);
 899        if (vif->type == NL80211_IFTYPE_STATION) {
 900                if (old_state == IEEE80211_STA_NOTEXIST &&
 901                    new_state == IEEE80211_STA_NONE)
 902                        op = ADD;
 903                else if (old_state == IEEE80211_STA_NONE &&
 904                         new_state == IEEE80211_STA_NOTEXIST)
 905                        op = REMOVE;
 906                else if (old_state == IEEE80211_STA_AUTH &&
 907                         new_state == IEEE80211_STA_ASSOC)
 908                        op = HT_RATE_INIT;
 909        } else {
 910                if (old_state == IEEE80211_STA_AUTH &&
 911                    new_state == IEEE80211_STA_ASSOC)
 912                        op = ADD_RATE_INIT;
 913                else if (old_state == IEEE80211_STA_ASSOC &&
 914                         new_state == IEEE80211_STA_AUTH)
 915                        op = REMOVE;
 916        }
 917
 918        switch (op) {
 919        case ADD:
 920                ret = iwlagn_mac_sta_add(hw, vif, sta);
 921                if (ret)
 922                        break;
 923                /*
 924                 * Clear the in-progress flag, the AP station entry was added
 925                 * but we'll initialize LQ only when we've associated (which
 926                 * would also clear the in-progress flag). This is necessary
 927                 * in case we never initialize LQ because association fails.
 928                 */
 929                spin_lock_bh(&priv->sta_lock);
 930                priv->stations[iwl_sta_id(sta)].used &=
 931                        ~IWL_STA_UCODE_INPROGRESS;
 932                spin_unlock_bh(&priv->sta_lock);
 933                break;
 934        case REMOVE:
 935                ret = iwlagn_mac_sta_remove(hw, vif, sta);
 936                break;
 937        case ADD_RATE_INIT:
 938                ret = iwlagn_mac_sta_add(hw, vif, sta);
 939                if (ret)
 940                        break;
 941                /* Initialize rate scaling */
 942                IWL_DEBUG_INFO(priv,
 943                               "Initializing rate scaling for station %pM\n",
 944                               sta->addr);
 945                iwl_rs_rate_init(priv, sta, iwl_sta_id(sta));
 946                ret = 0;
 947                break;
 948        case HT_RATE_INIT:
 949                /* Initialize rate scaling */
 950                ret = iwl_sta_update_ht(priv, vif_priv->ctx, sta);
 951                if (ret)
 952                        break;
 953                IWL_DEBUG_INFO(priv,
 954                               "Initializing rate scaling for station %pM\n",
 955                               sta->addr);
 956                iwl_rs_rate_init(priv, sta, iwl_sta_id(sta));
 957                ret = 0;
 958                break;
 959        default:
 960                ret = 0;
 961                break;
 962        }
 963
 964        /*
 965         * mac80211 might WARN if we fail, but due the way we
 966         * (badly) handle hard rfkill, we might fail here
 967         */
 968        if (iwl_is_rfkill(priv))
 969                ret = 0;
 970
 971        mutex_unlock(&priv->mutex);
 972        IWL_DEBUG_MAC80211(priv, "leave\n");
 973
 974        return ret;
 975}
 976
 977static void iwlagn_mac_channel_switch(struct ieee80211_hw *hw,
 978                                      struct ieee80211_channel_switch *ch_switch)
 979{
 980        struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
 981        struct ieee80211_conf *conf = &hw->conf;
 982        struct ieee80211_channel *channel = ch_switch->chandef.chan;
 983        struct iwl_ht_config *ht_conf = &priv->current_ht_config;
 984        /*
 985         * MULTI-FIXME
 986         * When we add support for multiple interfaces, we need to
 987         * revisit this. The channel switch command in the device
 988         * only affects the BSS context, but what does that really
 989         * mean? And what if we get a CSA on the second interface?
 990         * This needs a lot of work.
 991         */
 992        struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
 993        u16 ch;
 994
 995        IWL_DEBUG_MAC80211(priv, "enter\n");
 996
 997        mutex_lock(&priv->mutex);
 998
 999        if (iwl_is_rfkill(priv))
1000                goto out;
1001
1002        if (test_bit(STATUS_EXIT_PENDING, &priv->status) ||
1003            test_bit(STATUS_SCANNING, &priv->status) ||
1004            test_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->status))
1005                goto out;
1006
1007        if (!iwl_is_associated_ctx(ctx))
1008                goto out;
1009
1010        if (!priv->lib->set_channel_switch)
1011                goto out;
1012
1013        ch = channel->hw_value;
1014        if (le16_to_cpu(ctx->active.channel) == ch)
1015                goto out;
1016
1017        priv->current_ht_config.smps = conf->smps_mode;
1018
1019        /* Configure HT40 channels */
1020        switch (cfg80211_get_chandef_type(&ch_switch->chandef)) {
1021        case NL80211_CHAN_NO_HT:
1022        case NL80211_CHAN_HT20:
1023                ctx->ht.is_40mhz = false;
1024                ctx->ht.extension_chan_offset = IEEE80211_HT_PARAM_CHA_SEC_NONE;
1025                break;
1026        case NL80211_CHAN_HT40MINUS:
1027                ctx->ht.extension_chan_offset = IEEE80211_HT_PARAM_CHA_SEC_BELOW;
1028                ctx->ht.is_40mhz = true;
1029                break;
1030        case NL80211_CHAN_HT40PLUS:
1031                ctx->ht.extension_chan_offset = IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
1032                ctx->ht.is_40mhz = true;
1033                break;
1034        }
1035
1036        if ((le16_to_cpu(ctx->staging.channel) != ch))
1037                ctx->staging.flags = 0;
1038
1039        iwl_set_rxon_channel(priv, channel, ctx);
1040        iwl_set_rxon_ht(priv, ht_conf);
1041        iwl_set_flags_for_band(priv, ctx, channel->band, ctx->vif);
1042
1043        /*
1044         * at this point, staging_rxon has the
1045         * configuration for channel switch
1046         */
1047        set_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->status);
1048        priv->switch_channel = cpu_to_le16(ch);
1049        if (priv->lib->set_channel_switch(priv, ch_switch)) {
1050                clear_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->status);
1051                priv->switch_channel = 0;
1052                ieee80211_chswitch_done(ctx->vif, false);
1053        }
1054
1055out:
1056        mutex_unlock(&priv->mutex);
1057        IWL_DEBUG_MAC80211(priv, "leave\n");
1058}
1059
1060void iwl_chswitch_done(struct iwl_priv *priv, bool is_success)
1061{
1062        /*
1063         * MULTI-FIXME
1064         * See iwlagn_mac_channel_switch.
1065         */
1066        struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
1067
1068        if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1069                return;
1070
1071        if (!test_and_clear_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->status))
1072                return;
1073
1074        if (ctx->vif)
1075                ieee80211_chswitch_done(ctx->vif, is_success);
1076}
1077
1078static void iwlagn_configure_filter(struct ieee80211_hw *hw,
1079                                    unsigned int changed_flags,
1080                                    unsigned int *total_flags,
1081                                    u64 multicast)
1082{
1083        struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
1084        __le32 filter_or = 0, filter_nand = 0;
1085        struct iwl_rxon_context *ctx;
1086
1087#define CHK(test, flag) do { \
1088        if (*total_flags & (test))              \
1089                filter_or |= (flag);            \
1090        else                                    \
1091                filter_nand |= (flag);          \
1092        } while (0)
1093
1094        IWL_DEBUG_MAC80211(priv, "Enter: changed: 0x%x, total: 0x%x\n",
1095                        changed_flags, *total_flags);
1096
1097        CHK(FIF_OTHER_BSS | FIF_PROMISC_IN_BSS, RXON_FILTER_PROMISC_MSK);
1098        /* Setting _just_ RXON_FILTER_CTL2HOST_MSK causes FH errors */
1099        CHK(FIF_CONTROL, RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_PROMISC_MSK);
1100        CHK(FIF_BCN_PRBRESP_PROMISC, RXON_FILTER_BCON_AWARE_MSK);
1101
1102#undef CHK
1103
1104        mutex_lock(&priv->mutex);
1105
1106        for_each_context(priv, ctx) {
1107                ctx->staging.filter_flags &= ~filter_nand;
1108                ctx->staging.filter_flags |= filter_or;
1109
1110                /*
1111                 * Not committing directly because hardware can perform a scan,
1112                 * but we'll eventually commit the filter flags change anyway.
1113                 */
1114        }
1115
1116        mutex_unlock(&priv->mutex);
1117
1118        /*
1119         * Receiving all multicast frames is always enabled by the
1120         * default flags setup in iwl_connection_init_rx_config()
1121         * since we currently do not support programming multicast
1122         * filters into the device.
1123         */
1124        *total_flags &= FIF_OTHER_BSS | FIF_ALLMULTI | FIF_PROMISC_IN_BSS |
1125                        FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL;
1126}
1127
1128static void iwlagn_mac_flush(struct ieee80211_hw *hw, u32 queues, bool drop)
1129{
1130        struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
1131
1132        mutex_lock(&priv->mutex);
1133        IWL_DEBUG_MAC80211(priv, "enter\n");
1134
1135        if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
1136                IWL_DEBUG_TX(priv, "Aborting flush due to device shutdown\n");
1137                goto done;
1138        }
1139        if (iwl_is_rfkill(priv)) {
1140                IWL_DEBUG_TX(priv, "Aborting flush due to RF Kill\n");
1141                goto done;
1142        }
1143
1144        /*
1145         * mac80211 will not push any more frames for transmit
1146         * until the flush is completed
1147         */
1148        if (drop) {
1149                IWL_DEBUG_MAC80211(priv, "send flush command\n");
1150                if (iwlagn_txfifo_flush(priv, 0)) {
1151                        IWL_ERR(priv, "flush request fail\n");
1152                        goto done;
1153                }
1154        }
1155        IWL_DEBUG_MAC80211(priv, "wait transmit/flush all frames\n");
1156        iwl_trans_wait_tx_queue_empty(priv->trans);
1157done:
1158        mutex_unlock(&priv->mutex);
1159        IWL_DEBUG_MAC80211(priv, "leave\n");
1160}
1161
1162static int iwlagn_mac_remain_on_channel(struct ieee80211_hw *hw,
1163                                     struct ieee80211_vif *vif,
1164                                     struct ieee80211_channel *channel,
1165                                     int duration,
1166                                     enum ieee80211_roc_type type)
1167{
1168        struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
1169        struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_PAN];
1170        int err = 0;
1171
1172        if (!(priv->valid_contexts & BIT(IWL_RXON_CTX_PAN)))
1173                return -EOPNOTSUPP;
1174
1175        if (!(ctx->interface_modes & BIT(NL80211_IFTYPE_P2P_CLIENT)))
1176                return -EOPNOTSUPP;
1177
1178        IWL_DEBUG_MAC80211(priv, "enter\n");
1179        mutex_lock(&priv->mutex);
1180
1181        if (test_bit(STATUS_SCAN_HW, &priv->status)) {
1182                /* mac80211 should not scan while ROC or ROC while scanning */
1183                if (WARN_ON_ONCE(priv->scan_type != IWL_SCAN_RADIO_RESET)) {
1184                        err = -EBUSY;
1185                        goto out;
1186                }
1187
1188                iwl_scan_cancel_timeout(priv, 100);
1189
1190                if (test_bit(STATUS_SCAN_HW, &priv->status)) {
1191                        err = -EBUSY;
1192                        goto out;
1193                }
1194        }
1195
1196        priv->hw_roc_channel = channel;
1197        /* convert from ms to TU */
1198        priv->hw_roc_duration = DIV_ROUND_UP(1000 * duration, 1024);
1199        priv->hw_roc_start_notified = false;
1200        cancel_delayed_work(&priv->hw_roc_disable_work);
1201
1202        if (!ctx->is_active) {
1203                static const struct iwl_qos_info default_qos_data = {
1204                        .def_qos_parm = {
1205                                .ac[0] = {
1206                                        .cw_min = cpu_to_le16(3),
1207                                        .cw_max = cpu_to_le16(7),
1208                                        .aifsn = 2,
1209                                        .edca_txop = cpu_to_le16(1504),
1210                                },
1211                                .ac[1] = {
1212                                        .cw_min = cpu_to_le16(7),
1213                                        .cw_max = cpu_to_le16(15),
1214                                        .aifsn = 2,
1215                                        .edca_txop = cpu_to_le16(3008),
1216                                },
1217                                .ac[2] = {
1218                                        .cw_min = cpu_to_le16(15),
1219                                        .cw_max = cpu_to_le16(1023),
1220                                        .aifsn = 3,
1221                                },
1222                                .ac[3] = {
1223                                        .cw_min = cpu_to_le16(15),
1224                                        .cw_max = cpu_to_le16(1023),
1225                                        .aifsn = 7,
1226                                },
1227                        },
1228                };
1229
1230                ctx->is_active = true;
1231                ctx->qos_data = default_qos_data;
1232                ctx->staging.dev_type = RXON_DEV_TYPE_P2P;
1233                memcpy(ctx->staging.node_addr,
1234                       priv->contexts[IWL_RXON_CTX_BSS].staging.node_addr,
1235                       ETH_ALEN);
1236                memcpy(ctx->staging.bssid_addr,
1237                       priv->contexts[IWL_RXON_CTX_BSS].staging.node_addr,
1238                       ETH_ALEN);
1239                err = iwlagn_commit_rxon(priv, ctx);
1240                if (err)
1241                        goto out;
1242                ctx->staging.filter_flags |= RXON_FILTER_ASSOC_MSK |
1243                                             RXON_FILTER_PROMISC_MSK |
1244                                             RXON_FILTER_CTL2HOST_MSK;
1245
1246                err = iwlagn_commit_rxon(priv, ctx);
1247                if (err) {
1248                        iwlagn_disable_roc(priv);
1249                        goto out;
1250                }
1251                priv->hw_roc_setup = true;
1252        }
1253
1254        err = iwl_scan_initiate(priv, ctx->vif, IWL_SCAN_ROC, channel->band);
1255        if (err)
1256                iwlagn_disable_roc(priv);
1257
1258 out:
1259        mutex_unlock(&priv->mutex);
1260        IWL_DEBUG_MAC80211(priv, "leave\n");
1261
1262        return err;
1263}
1264
1265static int iwlagn_mac_cancel_remain_on_channel(struct ieee80211_hw *hw)
1266{
1267        struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
1268
1269        if (!(priv->valid_contexts & BIT(IWL_RXON_CTX_PAN)))
1270                return -EOPNOTSUPP;
1271
1272        IWL_DEBUG_MAC80211(priv, "enter\n");
1273        mutex_lock(&priv->mutex);
1274        iwl_scan_cancel_timeout(priv, priv->hw_roc_duration);
1275        iwlagn_disable_roc(priv);
1276        mutex_unlock(&priv->mutex);
1277        IWL_DEBUG_MAC80211(priv, "leave\n");
1278
1279        return 0;
1280}
1281
1282static void iwlagn_mac_rssi_callback(struct ieee80211_hw *hw,
1283                                     struct ieee80211_vif *vif,
1284                                     enum ieee80211_rssi_event rssi_event)
1285{
1286        struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
1287
1288        IWL_DEBUG_MAC80211(priv, "enter\n");
1289        mutex_lock(&priv->mutex);
1290
1291        if (priv->lib->bt_params &&
1292            priv->lib->bt_params->advanced_bt_coexist) {
1293                if (rssi_event == RSSI_EVENT_LOW)
1294                        priv->bt_enable_pspoll = true;
1295                else if (rssi_event == RSSI_EVENT_HIGH)
1296                        priv->bt_enable_pspoll = false;
1297
1298                iwlagn_send_advance_bt_config(priv);
1299        } else {
1300                IWL_DEBUG_MAC80211(priv, "Advanced BT coex disabled,"
1301                                "ignoring RSSI callback\n");
1302        }
1303
1304        mutex_unlock(&priv->mutex);
1305        IWL_DEBUG_MAC80211(priv, "leave\n");
1306}
1307
1308static int iwlagn_mac_set_tim(struct ieee80211_hw *hw,
1309                              struct ieee80211_sta *sta, bool set)
1310{
1311        struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
1312
1313        queue_work(priv->workqueue, &priv->beacon_update);
1314
1315        return 0;
1316}
1317
1318static int iwlagn_mac_conf_tx(struct ieee80211_hw *hw,
1319                              struct ieee80211_vif *vif, u16 queue,
1320                              const struct ieee80211_tx_queue_params *params)
1321{
1322        struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
1323        struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
1324        struct iwl_rxon_context *ctx = vif_priv->ctx;
1325        int q;
1326
1327        if (WARN_ON(!ctx))
1328                return -EINVAL;
1329
1330        IWL_DEBUG_MAC80211(priv, "enter\n");
1331
1332        if (!iwl_is_ready_rf(priv)) {
1333                IWL_DEBUG_MAC80211(priv, "leave - RF not ready\n");
1334                return -EIO;
1335        }
1336
1337        if (queue >= AC_NUM) {
1338                IWL_DEBUG_MAC80211(priv, "leave - queue >= AC_NUM %d\n", queue);
1339                return 0;
1340        }
1341
1342        q = AC_NUM - 1 - queue;
1343
1344        mutex_lock(&priv->mutex);
1345
1346        ctx->qos_data.def_qos_parm.ac[q].cw_min =
1347                cpu_to_le16(params->cw_min);
1348        ctx->qos_data.def_qos_parm.ac[q].cw_max =
1349                cpu_to_le16(params->cw_max);
1350        ctx->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
1351        ctx->qos_data.def_qos_parm.ac[q].edca_txop =
1352                        cpu_to_le16((params->txop * 32));
1353
1354        ctx->qos_data.def_qos_parm.ac[q].reserved1 = 0;
1355
1356        mutex_unlock(&priv->mutex);
1357
1358        IWL_DEBUG_MAC80211(priv, "leave\n");
1359        return 0;
1360}
1361
1362static int iwlagn_mac_tx_last_beacon(struct ieee80211_hw *hw)
1363{
1364        struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
1365
1366        return priv->ibss_manager == IWL_IBSS_MANAGER;
1367}
1368
1369static int iwl_set_mode(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
1370{
1371        iwl_connection_init_rx_config(priv, ctx);
1372
1373        iwlagn_set_rxon_chain(priv, ctx);
1374
1375        return iwlagn_commit_rxon(priv, ctx);
1376}
1377
1378static int iwl_setup_interface(struct iwl_priv *priv,
1379                               struct iwl_rxon_context *ctx)
1380{
1381        struct ieee80211_vif *vif = ctx->vif;
1382        int err, ac;
1383
1384        lockdep_assert_held(&priv->mutex);
1385
1386        /*
1387         * This variable will be correct only when there's just
1388         * a single context, but all code using it is for hardware
1389         * that supports only one context.
1390         */
1391        priv->iw_mode = vif->type;
1392
1393        ctx->is_active = true;
1394
1395        err = iwl_set_mode(priv, ctx);
1396        if (err) {
1397                if (!ctx->always_active)
1398                        ctx->is_active = false;
1399                return err;
1400        }
1401
1402        if (priv->lib->bt_params && priv->lib->bt_params->advanced_bt_coexist &&
1403            vif->type == NL80211_IFTYPE_ADHOC) {
1404                /*
1405                 * pretend to have high BT traffic as long as we
1406                 * are operating in IBSS mode, as this will cause
1407                 * the rate scaling etc. to behave as intended.
1408                 */
1409                priv->bt_traffic_load = IWL_BT_COEX_TRAFFIC_LOAD_HIGH;
1410        }
1411
1412        /* set up queue mappings */
1413        for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
1414                vif->hw_queue[ac] = ctx->ac_to_queue[ac];
1415
1416        if (vif->type == NL80211_IFTYPE_AP)
1417                vif->cab_queue = ctx->mcast_queue;
1418        else
1419                vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
1420
1421        return 0;
1422}
1423
1424static int iwlagn_mac_add_interface(struct ieee80211_hw *hw,
1425                                    struct ieee80211_vif *vif)
1426{
1427        struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
1428        struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
1429        struct iwl_rxon_context *tmp, *ctx = NULL;
1430        int err;
1431        enum nl80211_iftype viftype = ieee80211_vif_type_p2p(vif);
1432        bool reset = false;
1433
1434        IWL_DEBUG_MAC80211(priv, "enter: type %d, addr %pM\n",
1435                           viftype, vif->addr);
1436
1437        cancel_delayed_work_sync(&priv->hw_roc_disable_work);
1438
1439        mutex_lock(&priv->mutex);
1440
1441        iwlagn_disable_roc(priv);
1442
1443        if (!iwl_is_ready_rf(priv)) {
1444                IWL_WARN(priv, "Try to add interface when device not ready\n");
1445                err = -EINVAL;
1446                goto out;
1447        }
1448
1449        for_each_context(priv, tmp) {
1450                u32 possible_modes =
1451                        tmp->interface_modes | tmp->exclusive_interface_modes;
1452
1453                if (tmp->vif) {
1454                        /* On reset we need to add the same interface again */
1455                        if (tmp->vif == vif) {
1456                                reset = true;
1457                                ctx = tmp;
1458                                break;
1459                        }
1460
1461                        /* check if this busy context is exclusive */
1462                        if (tmp->exclusive_interface_modes &
1463                                                BIT(tmp->vif->type)) {
1464                                err = -EINVAL;
1465                                goto out;
1466                        }
1467                        continue;
1468                }
1469
1470                if (!(possible_modes & BIT(viftype)))
1471                        continue;
1472
1473                /* have maybe usable context w/o interface */
1474                ctx = tmp;
1475                break;
1476        }
1477
1478        if (!ctx) {
1479                err = -EOPNOTSUPP;
1480                goto out;
1481        }
1482
1483        vif_priv->ctx = ctx;
1484        ctx->vif = vif;
1485
1486        /*
1487         * In SNIFFER device type, the firmware reports the FCS to
1488         * the host, rather than snipping it off. Unfortunately,
1489         * mac80211 doesn't (yet) provide a per-packet flag for
1490         * this, so that we have to set the hardware flag based
1491         * on the interfaces added. As the monitor interface can
1492         * only be present by itself, and will be removed before
1493         * other interfaces are added, this is safe.
1494         */
1495        if (vif->type == NL80211_IFTYPE_MONITOR)
1496                priv->hw->flags |= IEEE80211_HW_RX_INCLUDES_FCS;
1497        else
1498                priv->hw->flags &= ~IEEE80211_HW_RX_INCLUDES_FCS;
1499
1500        err = iwl_setup_interface(priv, ctx);
1501        if (!err || reset)
1502                goto out;
1503
1504        ctx->vif = NULL;
1505        priv->iw_mode = NL80211_IFTYPE_STATION;
1506 out:
1507        mutex_unlock(&priv->mutex);
1508
1509        IWL_DEBUG_MAC80211(priv, "leave\n");
1510        return err;
1511}
1512
1513static void iwl_teardown_interface(struct iwl_priv *priv,
1514                                   struct ieee80211_vif *vif,
1515                                   bool mode_change)
1516{
1517        struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
1518
1519        lockdep_assert_held(&priv->mutex);
1520
1521        if (priv->scan_vif == vif) {
1522                iwl_scan_cancel_timeout(priv, 200);
1523                iwl_force_scan_end(priv);
1524        }
1525
1526        if (!mode_change) {
1527                iwl_set_mode(priv, ctx);
1528                if (!ctx->always_active)
1529                        ctx->is_active = false;
1530        }
1531
1532        /*
1533         * When removing the IBSS interface, overwrite the
1534         * BT traffic load with the stored one from the last
1535         * notification, if any. If this is a device that
1536         * doesn't implement this, this has no effect since
1537         * both values are the same and zero.
1538         */
1539        if (vif->type == NL80211_IFTYPE_ADHOC)
1540                priv->bt_traffic_load = priv->last_bt_traffic_load;
1541}
1542
1543static void iwlagn_mac_remove_interface(struct ieee80211_hw *hw,
1544                              struct ieee80211_vif *vif)
1545{
1546        struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
1547        struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
1548
1549        IWL_DEBUG_MAC80211(priv, "enter\n");
1550
1551        mutex_lock(&priv->mutex);
1552
1553        if (WARN_ON(ctx->vif != vif)) {
1554                struct iwl_rxon_context *tmp;
1555                IWL_ERR(priv, "ctx->vif = %p, vif = %p\n", ctx->vif, vif);
1556                for_each_context(priv, tmp)
1557                        IWL_ERR(priv, "\tID = %d:\tctx = %p\tctx->vif = %p\n",
1558                                tmp->ctxid, tmp, tmp->vif);
1559        }
1560        ctx->vif = NULL;
1561
1562        iwl_teardown_interface(priv, vif, false);
1563
1564        mutex_unlock(&priv->mutex);
1565
1566        IWL_DEBUG_MAC80211(priv, "leave\n");
1567
1568}
1569
1570static int iwlagn_mac_change_interface(struct ieee80211_hw *hw,
1571                                       struct ieee80211_vif *vif,
1572                                       enum nl80211_iftype newtype, bool newp2p)
1573{
1574        struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
1575        struct iwl_rxon_context *ctx, *tmp;
1576        enum nl80211_iftype newviftype = newtype;
1577        u32 interface_modes;
1578        int err;
1579
1580        IWL_DEBUG_MAC80211(priv, "enter\n");
1581
1582        newtype = ieee80211_iftype_p2p(newtype, newp2p);
1583
1584        mutex_lock(&priv->mutex);
1585
1586        ctx = iwl_rxon_ctx_from_vif(vif);
1587
1588        /*
1589         * To simplify this code, only support changes on the
1590         * BSS context. The PAN context is usually reassigned
1591         * by creating/removing P2P interfaces anyway.
1592         */
1593        if (ctx->ctxid != IWL_RXON_CTX_BSS) {
1594                err = -EBUSY;
1595                goto out;
1596        }
1597
1598        if (!ctx->vif || !iwl_is_ready_rf(priv)) {
1599                /*
1600                 * Huh? But wait ... this can maybe happen when
1601                 * we're in the middle of a firmware restart!
1602                 */
1603                err = -EBUSY;
1604                goto out;
1605        }
1606
1607        /* Check if the switch is supported in the same context */
1608        interface_modes = ctx->interface_modes | ctx->exclusive_interface_modes;
1609        if (!(interface_modes & BIT(newtype))) {
1610                err = -EBUSY;
1611                goto out;
1612        }
1613
1614        if (ctx->exclusive_interface_modes & BIT(newtype)) {
1615                for_each_context(priv, tmp) {
1616                        if (ctx == tmp)
1617                                continue;
1618
1619                        if (!tmp->is_active)
1620                                continue;
1621
1622                        /*
1623                         * The current mode switch would be exclusive, but
1624                         * another context is active ... refuse the switch.
1625                         */
1626                        err = -EBUSY;
1627                        goto out;
1628                }
1629        }
1630
1631        /* success */
1632        iwl_teardown_interface(priv, vif, true);
1633        vif->type = newviftype;
1634        vif->p2p = newp2p;
1635        err = iwl_setup_interface(priv, ctx);
1636        WARN_ON(err);
1637        /*
1638         * We've switched internally, but submitting to the
1639         * device may have failed for some reason. Mask this
1640         * error, because otherwise mac80211 will not switch
1641         * (and set the interface type back) and we'll be
1642         * out of sync with it.
1643         */
1644        err = 0;
1645
1646 out:
1647        mutex_unlock(&priv->mutex);
1648        IWL_DEBUG_MAC80211(priv, "leave\n");
1649
1650        return err;
1651}
1652
1653static int iwlagn_mac_hw_scan(struct ieee80211_hw *hw,
1654                              struct ieee80211_vif *vif,
1655                              struct cfg80211_scan_request *req)
1656{
1657        struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
1658        int ret;
1659
1660        IWL_DEBUG_MAC80211(priv, "enter\n");
1661
1662        if (req->n_channels == 0)
1663                return -EINVAL;
1664
1665        mutex_lock(&priv->mutex);
1666
1667        /*
1668         * If an internal scan is in progress, just set
1669         * up the scan_request as per above.
1670         */
1671        if (priv->scan_type != IWL_SCAN_NORMAL) {
1672                IWL_DEBUG_SCAN(priv,
1673                               "SCAN request during internal scan - defer\n");
1674                priv->scan_request = req;
1675                priv->scan_vif = vif;
1676                ret = 0;
1677        } else {
1678                priv->scan_request = req;
1679                priv->scan_vif = vif;
1680                /*
1681                 * mac80211 will only ask for one band at a time
1682                 * so using channels[0] here is ok
1683                 */
1684                ret = iwl_scan_initiate(priv, vif, IWL_SCAN_NORMAL,
1685                                        req->channels[0]->band);
1686                if (ret) {
1687                        priv->scan_request = NULL;
1688                        priv->scan_vif = NULL;
1689                }
1690        }
1691
1692        IWL_DEBUG_MAC80211(priv, "leave\n");
1693
1694        mutex_unlock(&priv->mutex);
1695
1696        return ret;
1697}
1698
1699static void iwl_sta_modify_ps_wake(struct iwl_priv *priv, int sta_id)
1700{
1701        struct iwl_addsta_cmd cmd = {
1702                .mode = STA_CONTROL_MODIFY_MSK,
1703                .station_flags_msk = STA_FLG_PWR_SAVE_MSK,
1704                .sta.sta_id = sta_id,
1705        };
1706
1707        iwl_send_add_sta(priv, &cmd, CMD_ASYNC);
1708}
1709
1710static void iwlagn_mac_sta_notify(struct ieee80211_hw *hw,
1711                                  struct ieee80211_vif *vif,
1712                                  enum sta_notify_cmd cmd,
1713                                  struct ieee80211_sta *sta)
1714{
1715        struct iwl_priv *priv = IWL_MAC80211_GET_DVM(hw);
1716        struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
1717        int sta_id;
1718
1719        IWL_DEBUG_MAC80211(priv, "enter\n");
1720
1721        switch (cmd) {
1722        case STA_NOTIFY_SLEEP:
1723                WARN_ON(!sta_priv->client);
1724                sta_priv->asleep = true;
1725                if (atomic_read(&sta_priv->pending_frames) > 0)
1726                        ieee80211_sta_block_awake(hw, sta, true);
1727                break;
1728        case STA_NOTIFY_AWAKE:
1729                WARN_ON(!sta_priv->client);
1730                if (!sta_priv->asleep)
1731                        break;
1732                sta_priv->asleep = false;
1733                sta_id = iwl_sta_id(sta);
1734                if (sta_id != IWL_INVALID_STATION)
1735                        iwl_sta_modify_ps_wake(priv, sta_id);
1736                break;
1737        default:
1738                break;
1739        }
1740        IWL_DEBUG_MAC80211(priv, "leave\n");
1741}
1742
1743struct ieee80211_ops iwlagn_hw_ops = {
1744        .tx = iwlagn_mac_tx,
1745        .start = iwlagn_mac_start,
1746        .stop = iwlagn_mac_stop,
1747#ifdef CONFIG_PM_SLEEP
1748        .suspend = iwlagn_mac_suspend,
1749        .resume = iwlagn_mac_resume,
1750        .set_wakeup = iwlagn_mac_set_wakeup,
1751#endif
1752        .add_interface = iwlagn_mac_add_interface,
1753        .remove_interface = iwlagn_mac_remove_interface,
1754        .change_interface = iwlagn_mac_change_interface,
1755        .config = iwlagn_mac_config,
1756        .configure_filter = iwlagn_configure_filter,
1757        .set_key = iwlagn_mac_set_key,
1758        .update_tkip_key = iwlagn_mac_update_tkip_key,
1759        .set_rekey_data = iwlagn_mac_set_rekey_data,
1760        .conf_tx = iwlagn_mac_conf_tx,
1761        .bss_info_changed = iwlagn_bss_info_changed,
1762        .ampdu_action = iwlagn_mac_ampdu_action,
1763        .hw_scan = iwlagn_mac_hw_scan,
1764        .sta_notify = iwlagn_mac_sta_notify,
1765        .sta_state = iwlagn_mac_sta_state,
1766        .channel_switch = iwlagn_mac_channel_switch,
1767        .flush = iwlagn_mac_flush,
1768        .tx_last_beacon = iwlagn_mac_tx_last_beacon,
1769        .remain_on_channel = iwlagn_mac_remain_on_channel,
1770        .cancel_remain_on_channel = iwlagn_mac_cancel_remain_on_channel,
1771        .rssi_callback = iwlagn_mac_rssi_callback,
1772        .set_tim = iwlagn_mac_set_tim,
1773};
1774
1775/* This function both allocates and initializes hw and priv. */
1776struct ieee80211_hw *iwl_alloc_all(void)
1777{
1778        struct iwl_priv *priv;
1779        struct iwl_op_mode *op_mode;
1780        /* mac80211 allocates memory for this device instance, including
1781         *   space for this driver's private structure */
1782        struct ieee80211_hw *hw;
1783
1784        hw = ieee80211_alloc_hw(sizeof(struct iwl_priv) +
1785                                sizeof(struct iwl_op_mode), &iwlagn_hw_ops);
1786        if (!hw)
1787                goto out;
1788
1789        op_mode = hw->priv;
1790        priv = IWL_OP_MODE_GET_DVM(op_mode);
1791        priv->hw = hw;
1792
1793out:
1794        return hw;
1795}
1796