linux/drivers/net/wireless/intel/iwlwifi/dvm/main.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-only
   2/******************************************************************************
   3 *
   4 * Copyright(c) 2003 - 2014, 2018 - 2020  Intel Corporation. All rights reserved.
   5 * Copyright(c) 2015 Intel Deutschland GmbH
   6 *
   7 * Portions of this file are derived from the ipw3945 project, as well
   8 * as portions of the ieee80211 subsystem header files.
   9 *
  10 * Contact Information:
  11 *  Intel Linux Wireless <linuxwifi@intel.com>
  12 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  13 *
  14 *****************************************************************************/
  15
  16#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  17
  18#include <linux/kernel.h>
  19#include <linux/module.h>
  20#include <linux/init.h>
  21#include <linux/slab.h>
  22#include <linux/delay.h>
  23#include <linux/sched.h>
  24#include <linux/skbuff.h>
  25#include <linux/netdevice.h>
  26#include <linux/etherdevice.h>
  27#include <linux/if_arp.h>
  28
  29#include <net/mac80211.h>
  30
  31#include <asm/div64.h>
  32
  33#include "iwl-eeprom-read.h"
  34#include "iwl-eeprom-parse.h"
  35#include "iwl-io.h"
  36#include "iwl-trans.h"
  37#include "iwl-op-mode.h"
  38#include "iwl-drv.h"
  39#include "iwl-modparams.h"
  40#include "iwl-prph.h"
  41
  42#include "dev.h"
  43#include "calib.h"
  44#include "agn.h"
  45
  46
  47/******************************************************************************
  48 *
  49 * module boiler plate
  50 *
  51 ******************************************************************************/
  52
  53#define DRV_DESCRIPTION "Intel(R) Wireless WiFi Link AGN driver for Linux"
  54MODULE_DESCRIPTION(DRV_DESCRIPTION);
  55MODULE_AUTHOR(DRV_AUTHOR);
  56MODULE_LICENSE("GPL");
  57
  58/* Please keep this array *SORTED* by hex value.
  59 * Access is done through binary search.
  60 * A warning will be triggered on violation.
  61 */
  62static const struct iwl_hcmd_names iwl_dvm_cmd_names[] = {
  63        HCMD_NAME(REPLY_ALIVE),
  64        HCMD_NAME(REPLY_ERROR),
  65        HCMD_NAME(REPLY_ECHO),
  66        HCMD_NAME(REPLY_RXON),
  67        HCMD_NAME(REPLY_RXON_ASSOC),
  68        HCMD_NAME(REPLY_QOS_PARAM),
  69        HCMD_NAME(REPLY_RXON_TIMING),
  70        HCMD_NAME(REPLY_ADD_STA),
  71        HCMD_NAME(REPLY_REMOVE_STA),
  72        HCMD_NAME(REPLY_REMOVE_ALL_STA),
  73        HCMD_NAME(REPLY_TX),
  74        HCMD_NAME(REPLY_TXFIFO_FLUSH),
  75        HCMD_NAME(REPLY_WEPKEY),
  76        HCMD_NAME(REPLY_LEDS_CMD),
  77        HCMD_NAME(REPLY_TX_LINK_QUALITY_CMD),
  78        HCMD_NAME(COEX_PRIORITY_TABLE_CMD),
  79        HCMD_NAME(COEX_MEDIUM_NOTIFICATION),
  80        HCMD_NAME(COEX_EVENT_CMD),
  81        HCMD_NAME(TEMPERATURE_NOTIFICATION),
  82        HCMD_NAME(CALIBRATION_CFG_CMD),
  83        HCMD_NAME(CALIBRATION_RES_NOTIFICATION),
  84        HCMD_NAME(CALIBRATION_COMPLETE_NOTIFICATION),
  85        HCMD_NAME(REPLY_QUIET_CMD),
  86        HCMD_NAME(REPLY_CHANNEL_SWITCH),
  87        HCMD_NAME(CHANNEL_SWITCH_NOTIFICATION),
  88        HCMD_NAME(REPLY_SPECTRUM_MEASUREMENT_CMD),
  89        HCMD_NAME(SPECTRUM_MEASURE_NOTIFICATION),
  90        HCMD_NAME(POWER_TABLE_CMD),
  91        HCMD_NAME(PM_SLEEP_NOTIFICATION),
  92        HCMD_NAME(PM_DEBUG_STATISTIC_NOTIFIC),
  93        HCMD_NAME(REPLY_SCAN_CMD),
  94        HCMD_NAME(REPLY_SCAN_ABORT_CMD),
  95        HCMD_NAME(SCAN_START_NOTIFICATION),
  96        HCMD_NAME(SCAN_RESULTS_NOTIFICATION),
  97        HCMD_NAME(SCAN_COMPLETE_NOTIFICATION),
  98        HCMD_NAME(BEACON_NOTIFICATION),
  99        HCMD_NAME(REPLY_TX_BEACON),
 100        HCMD_NAME(WHO_IS_AWAKE_NOTIFICATION),
 101        HCMD_NAME(REPLY_TX_POWER_DBM_CMD),
 102        HCMD_NAME(QUIET_NOTIFICATION),
 103        HCMD_NAME(REPLY_TX_PWR_TABLE_CMD),
 104        HCMD_NAME(REPLY_TX_POWER_DBM_CMD_V1),
 105        HCMD_NAME(TX_ANT_CONFIGURATION_CMD),
 106        HCMD_NAME(MEASURE_ABORT_NOTIFICATION),
 107        HCMD_NAME(REPLY_BT_CONFIG),
 108        HCMD_NAME(REPLY_STATISTICS_CMD),
 109        HCMD_NAME(STATISTICS_NOTIFICATION),
 110        HCMD_NAME(REPLY_CARD_STATE_CMD),
 111        HCMD_NAME(CARD_STATE_NOTIFICATION),
 112        HCMD_NAME(MISSED_BEACONS_NOTIFICATION),
 113        HCMD_NAME(REPLY_CT_KILL_CONFIG_CMD),
 114        HCMD_NAME(SENSITIVITY_CMD),
 115        HCMD_NAME(REPLY_PHY_CALIBRATION_CMD),
 116        HCMD_NAME(REPLY_WIPAN_PARAMS),
 117        HCMD_NAME(REPLY_WIPAN_RXON),
 118        HCMD_NAME(REPLY_WIPAN_RXON_TIMING),
 119        HCMD_NAME(REPLY_WIPAN_RXON_ASSOC),
 120        HCMD_NAME(REPLY_WIPAN_QOS_PARAM),
 121        HCMD_NAME(REPLY_WIPAN_WEPKEY),
 122        HCMD_NAME(REPLY_WIPAN_P2P_CHANNEL_SWITCH),
 123        HCMD_NAME(REPLY_WIPAN_NOA_NOTIFICATION),
 124        HCMD_NAME(REPLY_WIPAN_DEACTIVATION_COMPLETE),
 125        HCMD_NAME(REPLY_RX_PHY_CMD),
 126        HCMD_NAME(REPLY_RX_MPDU_CMD),
 127        HCMD_NAME(REPLY_RX),
 128        HCMD_NAME(REPLY_COMPRESSED_BA),
 129        HCMD_NAME(REPLY_BT_COEX_PRIO_TABLE),
 130        HCMD_NAME(REPLY_BT_COEX_PROT_ENV),
 131        HCMD_NAME(REPLY_BT_COEX_PROFILE_NOTIF),
 132        HCMD_NAME(REPLY_D3_CONFIG),
 133        HCMD_NAME(REPLY_WOWLAN_PATTERNS),
 134        HCMD_NAME(REPLY_WOWLAN_WAKEUP_FILTER),
 135        HCMD_NAME(REPLY_WOWLAN_TSC_RSC_PARAMS),
 136        HCMD_NAME(REPLY_WOWLAN_TKIP_PARAMS),
 137        HCMD_NAME(REPLY_WOWLAN_KEK_KCK_MATERIAL),
 138        HCMD_NAME(REPLY_WOWLAN_GET_STATUS),
 139};
 140
 141static const struct iwl_hcmd_arr iwl_dvm_groups[] = {
 142        [0x0] = HCMD_ARR(iwl_dvm_cmd_names),
 143};
 144
 145static const struct iwl_op_mode_ops iwl_dvm_ops;
 146
 147void iwl_update_chain_flags(struct iwl_priv *priv)
 148{
 149        struct iwl_rxon_context *ctx;
 150
 151        for_each_context(priv, ctx) {
 152                iwlagn_set_rxon_chain(priv, ctx);
 153                if (ctx->active.rx_chain != ctx->staging.rx_chain)
 154                        iwlagn_commit_rxon(priv, ctx);
 155        }
 156}
 157
 158/* Parse the beacon frame to find the TIM element and set tim_idx & tim_size */
 159static void iwl_set_beacon_tim(struct iwl_priv *priv,
 160                               struct iwl_tx_beacon_cmd *tx_beacon_cmd,
 161                               u8 *beacon, u32 frame_size)
 162{
 163        u16 tim_idx;
 164        struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)beacon;
 165
 166        /*
 167         * The index is relative to frame start but we start looking at the
 168         * variable-length part of the beacon.
 169         */
 170        tim_idx = mgmt->u.beacon.variable - beacon;
 171
 172        /* Parse variable-length elements of beacon to find WLAN_EID_TIM */
 173        while ((tim_idx < (frame_size - 2)) &&
 174                        (beacon[tim_idx] != WLAN_EID_TIM))
 175                tim_idx += beacon[tim_idx+1] + 2;
 176
 177        /* If TIM field was found, set variables */
 178        if ((tim_idx < (frame_size - 1)) && (beacon[tim_idx] == WLAN_EID_TIM)) {
 179                tx_beacon_cmd->tim_idx = cpu_to_le16(tim_idx);
 180                tx_beacon_cmd->tim_size = beacon[tim_idx+1];
 181        } else
 182                IWL_WARN(priv, "Unable to find TIM Element in beacon\n");
 183}
 184
 185int iwlagn_send_beacon_cmd(struct iwl_priv *priv)
 186{
 187        struct iwl_tx_beacon_cmd *tx_beacon_cmd;
 188        struct iwl_host_cmd cmd = {
 189                .id = REPLY_TX_BEACON,
 190        };
 191        struct ieee80211_tx_info *info;
 192        u32 frame_size;
 193        u32 rate_flags;
 194        u32 rate;
 195
 196        /*
 197         * We have to set up the TX command, the TX Beacon command, and the
 198         * beacon contents.
 199         */
 200
 201        lockdep_assert_held(&priv->mutex);
 202
 203        if (!priv->beacon_ctx) {
 204                IWL_ERR(priv, "trying to build beacon w/o beacon context!\n");
 205                return 0;
 206        }
 207
 208        if (WARN_ON(!priv->beacon_skb))
 209                return -EINVAL;
 210
 211        /* Allocate beacon command */
 212        if (!priv->beacon_cmd)
 213                priv->beacon_cmd = kzalloc(sizeof(*tx_beacon_cmd), GFP_KERNEL);
 214        tx_beacon_cmd = priv->beacon_cmd;
 215        if (!tx_beacon_cmd)
 216                return -ENOMEM;
 217
 218        frame_size = priv->beacon_skb->len;
 219
 220        /* Set up TX command fields */
 221        tx_beacon_cmd->tx.len = cpu_to_le16((u16)frame_size);
 222        tx_beacon_cmd->tx.sta_id = priv->beacon_ctx->bcast_sta_id;
 223        tx_beacon_cmd->tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
 224        tx_beacon_cmd->tx.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK |
 225                TX_CMD_FLG_TSF_MSK | TX_CMD_FLG_STA_RATE_MSK;
 226
 227        /* Set up TX beacon command fields */
 228        iwl_set_beacon_tim(priv, tx_beacon_cmd, priv->beacon_skb->data,
 229                           frame_size);
 230
 231        /* Set up packet rate and flags */
 232        info = IEEE80211_SKB_CB(priv->beacon_skb);
 233
 234        /*
 235         * Let's set up the rate at least somewhat correctly;
 236         * it will currently not actually be used by the uCode,
 237         * it uses the broadcast station's rate instead.
 238         */
 239        if (info->control.rates[0].idx < 0 ||
 240            info->control.rates[0].flags & IEEE80211_TX_RC_MCS)
 241                rate = 0;
 242        else
 243                rate = info->control.rates[0].idx;
 244
 245        priv->mgmt_tx_ant = iwl_toggle_tx_ant(priv, priv->mgmt_tx_ant,
 246                                              priv->nvm_data->valid_tx_ant);
 247        rate_flags = iwl_ant_idx_to_flags(priv->mgmt_tx_ant);
 248
 249        /* In mac80211, rates for 5 GHz start at 0 */
 250        if (info->band == NL80211_BAND_5GHZ)
 251                rate += IWL_FIRST_OFDM_RATE;
 252        else if (rate >= IWL_FIRST_CCK_RATE && rate <= IWL_LAST_CCK_RATE)
 253                rate_flags |= RATE_MCS_CCK_MSK;
 254
 255        tx_beacon_cmd->tx.rate_n_flags =
 256                        iwl_hw_set_rate_n_flags(rate, rate_flags);
 257
 258        /* Submit command */
 259        cmd.len[0] = sizeof(*tx_beacon_cmd);
 260        cmd.data[0] = tx_beacon_cmd;
 261        cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
 262        cmd.len[1] = frame_size;
 263        cmd.data[1] = priv->beacon_skb->data;
 264        cmd.dataflags[1] = IWL_HCMD_DFL_NOCOPY;
 265
 266        return iwl_dvm_send_cmd(priv, &cmd);
 267}
 268
 269static void iwl_bg_beacon_update(struct work_struct *work)
 270{
 271        struct iwl_priv *priv =
 272                container_of(work, struct iwl_priv, beacon_update);
 273        struct sk_buff *beacon;
 274
 275        mutex_lock(&priv->mutex);
 276        if (!priv->beacon_ctx) {
 277                IWL_ERR(priv, "updating beacon w/o beacon context!\n");
 278                goto out;
 279        }
 280
 281        if (priv->beacon_ctx->vif->type != NL80211_IFTYPE_AP) {
 282                /*
 283                 * The ucode will send beacon notifications even in
 284                 * IBSS mode, but we don't want to process them. But
 285                 * we need to defer the type check to here due to
 286                 * requiring locking around the beacon_ctx access.
 287                 */
 288                goto out;
 289        }
 290
 291        /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
 292        beacon = ieee80211_beacon_get(priv->hw, priv->beacon_ctx->vif);
 293        if (!beacon) {
 294                IWL_ERR(priv, "update beacon failed -- keeping old\n");
 295                goto out;
 296        }
 297
 298        /* new beacon skb is allocated every time; dispose previous.*/
 299        dev_kfree_skb(priv->beacon_skb);
 300
 301        priv->beacon_skb = beacon;
 302
 303        iwlagn_send_beacon_cmd(priv);
 304 out:
 305        mutex_unlock(&priv->mutex);
 306}
 307
 308static void iwl_bg_bt_runtime_config(struct work_struct *work)
 309{
 310        struct iwl_priv *priv =
 311                container_of(work, struct iwl_priv, bt_runtime_config);
 312
 313        mutex_lock(&priv->mutex);
 314        if (test_bit(STATUS_EXIT_PENDING, &priv->status))
 315                goto out;
 316
 317        /* dont send host command if rf-kill is on */
 318        if (!iwl_is_ready_rf(priv))
 319                goto out;
 320
 321        iwlagn_send_advance_bt_config(priv);
 322out:
 323        mutex_unlock(&priv->mutex);
 324}
 325
 326static void iwl_bg_bt_full_concurrency(struct work_struct *work)
 327{
 328        struct iwl_priv *priv =
 329                container_of(work, struct iwl_priv, bt_full_concurrency);
 330        struct iwl_rxon_context *ctx;
 331
 332        mutex_lock(&priv->mutex);
 333
 334        if (test_bit(STATUS_EXIT_PENDING, &priv->status))
 335                goto out;
 336
 337        /* dont send host command if rf-kill is on */
 338        if (!iwl_is_ready_rf(priv))
 339                goto out;
 340
 341        IWL_DEBUG_INFO(priv, "BT coex in %s mode\n",
 342                       priv->bt_full_concurrent ?
 343                       "full concurrency" : "3-wire");
 344
 345        /*
 346         * LQ & RXON updated cmds must be sent before BT Config cmd
 347         * to avoid 3-wire collisions
 348         */
 349        for_each_context(priv, ctx) {
 350                iwlagn_set_rxon_chain(priv, ctx);
 351                iwlagn_commit_rxon(priv, ctx);
 352        }
 353
 354        iwlagn_send_advance_bt_config(priv);
 355out:
 356        mutex_unlock(&priv->mutex);
 357}
 358
 359int iwl_send_statistics_request(struct iwl_priv *priv, u8 flags, bool clear)
 360{
 361        struct iwl_statistics_cmd statistics_cmd = {
 362                .configuration_flags =
 363                        clear ? IWL_STATS_CONF_CLEAR_STATS : 0,
 364        };
 365
 366        if (flags & CMD_ASYNC)
 367                return iwl_dvm_send_cmd_pdu(priv, REPLY_STATISTICS_CMD,
 368                                        CMD_ASYNC,
 369                                        sizeof(struct iwl_statistics_cmd),
 370                                        &statistics_cmd);
 371        else
 372                return iwl_dvm_send_cmd_pdu(priv, REPLY_STATISTICS_CMD, 0,
 373                                        sizeof(struct iwl_statistics_cmd),
 374                                        &statistics_cmd);
 375}
 376
 377/*
 378 * iwl_bg_statistics_periodic - Timer callback to queue statistics
 379 *
 380 * This callback is provided in order to send a statistics request.
 381 *
 382 * This timer function is continually reset to execute within
 383 * REG_RECALIB_PERIOD seconds since the last STATISTICS_NOTIFICATION
 384 * was received.  We need to ensure we receive the statistics in order
 385 * to update the temperature used for calibrating the TXPOWER.
 386 */
 387static void iwl_bg_statistics_periodic(struct timer_list *t)
 388{
 389        struct iwl_priv *priv = from_timer(priv, t, statistics_periodic);
 390
 391        if (test_bit(STATUS_EXIT_PENDING, &priv->status))
 392                return;
 393
 394        /* dont send host command if rf-kill is on */
 395        if (!iwl_is_ready_rf(priv))
 396                return;
 397
 398        iwl_send_statistics_request(priv, CMD_ASYNC, false);
 399}
 400
 401
 402static void iwl_print_cont_event_trace(struct iwl_priv *priv, u32 base,
 403                                        u32 start_idx, u32 num_events,
 404                                        u32 capacity, u32 mode)
 405{
 406        u32 i;
 407        u32 ptr;        /* SRAM byte address of log data */
 408        u32 ev, time, data; /* event log data */
 409
 410        if (mode == 0)
 411                ptr = base + (4 * sizeof(u32)) + (start_idx * 2 * sizeof(u32));
 412        else
 413                ptr = base + (4 * sizeof(u32)) + (start_idx * 3 * sizeof(u32));
 414
 415        /* Make sure device is powered up for SRAM reads */
 416        if (!iwl_trans_grab_nic_access(priv->trans))
 417                return;
 418
 419        /* Set starting address; reads will auto-increment */
 420        iwl_write32(priv->trans, HBUS_TARG_MEM_RADDR, ptr);
 421
 422        /*
 423         * Refuse to read more than would have fit into the log from
 424         * the current start_idx. This used to happen due to the race
 425         * described below, but now WARN because the code below should
 426         * prevent it from happening here.
 427         */
 428        if (WARN_ON(num_events > capacity - start_idx))
 429                num_events = capacity - start_idx;
 430
 431        /*
 432         * "time" is actually "data" for mode 0 (no timestamp).
 433         * place event id # at far right for easier visual parsing.
 434         */
 435        for (i = 0; i < num_events; i++) {
 436                ev = iwl_read32(priv->trans, HBUS_TARG_MEM_RDAT);
 437                time = iwl_read32(priv->trans, HBUS_TARG_MEM_RDAT);
 438                if (mode == 0) {
 439                        trace_iwlwifi_dev_ucode_cont_event(
 440                                        priv->trans->dev, 0, time, ev);
 441                } else {
 442                        data = iwl_read32(priv->trans, HBUS_TARG_MEM_RDAT);
 443                        trace_iwlwifi_dev_ucode_cont_event(
 444                                        priv->trans->dev, time, data, ev);
 445                }
 446        }
 447        /* Allow device to power down */
 448        iwl_trans_release_nic_access(priv->trans);
 449}
 450
 451static void iwl_continuous_event_trace(struct iwl_priv *priv)
 452{
 453        u32 capacity;   /* event log capacity in # entries */
 454        struct {
 455                u32 capacity;
 456                u32 mode;
 457                u32 wrap_counter;
 458                u32 write_counter;
 459        } __packed read;
 460        u32 base;       /* SRAM byte address of event log header */
 461        u32 mode;       /* 0 - no timestamp, 1 - timestamp recorded */
 462        u32 num_wraps;  /* # times uCode wrapped to top of log */
 463        u32 next_entry; /* index of next entry to be written by uCode */
 464
 465        base = priv->device_pointers.log_event_table;
 466        if (iwlagn_hw_valid_rtc_data_addr(base)) {
 467                iwl_trans_read_mem_bytes(priv->trans, base,
 468                                         &read, sizeof(read));
 469                capacity = read.capacity;
 470                mode = read.mode;
 471                num_wraps = read.wrap_counter;
 472                next_entry = read.write_counter;
 473        } else
 474                return;
 475
 476        /*
 477         * Unfortunately, the uCode doesn't use temporary variables.
 478         * Therefore, it can happen that we read next_entry == capacity,
 479         * which really means next_entry == 0.
 480         */
 481        if (unlikely(next_entry == capacity))
 482                next_entry = 0;
 483        /*
 484         * Additionally, the uCode increases the write pointer before
 485         * the wraps counter, so if the write pointer is smaller than
 486         * the old write pointer (wrap occurred) but we read that no
 487         * wrap occurred, we actually read between the next_entry and
 488         * num_wraps update (this does happen in practice!!) -- take
 489         * that into account by increasing num_wraps.
 490         */
 491        if (unlikely(next_entry < priv->event_log.next_entry &&
 492                     num_wraps == priv->event_log.num_wraps))
 493                num_wraps++;
 494
 495        if (num_wraps == priv->event_log.num_wraps) {
 496                iwl_print_cont_event_trace(
 497                        priv, base, priv->event_log.next_entry,
 498                        next_entry - priv->event_log.next_entry,
 499                        capacity, mode);
 500
 501                priv->event_log.non_wraps_count++;
 502        } else {
 503                if (num_wraps - priv->event_log.num_wraps > 1)
 504                        priv->event_log.wraps_more_count++;
 505                else
 506                        priv->event_log.wraps_once_count++;
 507
 508                trace_iwlwifi_dev_ucode_wrap_event(priv->trans->dev,
 509                                num_wraps - priv->event_log.num_wraps,
 510                                next_entry, priv->event_log.next_entry);
 511
 512                if (next_entry < priv->event_log.next_entry) {
 513                        iwl_print_cont_event_trace(
 514                                priv, base, priv->event_log.next_entry,
 515                                capacity - priv->event_log.next_entry,
 516                                capacity, mode);
 517
 518                        iwl_print_cont_event_trace(
 519                                priv, base, 0, next_entry, capacity, mode);
 520                } else {
 521                        iwl_print_cont_event_trace(
 522                                priv, base, next_entry,
 523                                capacity - next_entry,
 524                                capacity, mode);
 525
 526                        iwl_print_cont_event_trace(
 527                                priv, base, 0, next_entry, capacity, mode);
 528                }
 529        }
 530
 531        priv->event_log.num_wraps = num_wraps;
 532        priv->event_log.next_entry = next_entry;
 533}
 534
 535/*
 536 * iwl_bg_ucode_trace - Timer callback to log ucode event
 537 *
 538 * The timer is continually set to execute every
 539 * UCODE_TRACE_PERIOD milliseconds after the last timer expired
 540 * this function is to perform continuous uCode event logging operation
 541 * if enabled
 542 */
 543static void iwl_bg_ucode_trace(struct timer_list *t)
 544{
 545        struct iwl_priv *priv = from_timer(priv, t, ucode_trace);
 546
 547        if (test_bit(STATUS_EXIT_PENDING, &priv->status))
 548                return;
 549
 550        if (priv->event_log.ucode_trace) {
 551                iwl_continuous_event_trace(priv);
 552                /* Reschedule the timer to occur in UCODE_TRACE_PERIOD */
 553                mod_timer(&priv->ucode_trace,
 554                         jiffies + msecs_to_jiffies(UCODE_TRACE_PERIOD));
 555        }
 556}
 557
 558static void iwl_bg_tx_flush(struct work_struct *work)
 559{
 560        struct iwl_priv *priv =
 561                container_of(work, struct iwl_priv, tx_flush);
 562
 563        if (test_bit(STATUS_EXIT_PENDING, &priv->status))
 564                return;
 565
 566        /* do nothing if rf-kill is on */
 567        if (!iwl_is_ready_rf(priv))
 568                return;
 569
 570        IWL_DEBUG_INFO(priv, "device request: flush all tx frames\n");
 571        iwlagn_dev_txfifo_flush(priv);
 572}
 573
 574/*
 575 * queue/FIFO/AC mapping definitions
 576 */
 577
 578static const u8 iwlagn_bss_ac_to_fifo[] = {
 579        IWL_TX_FIFO_VO,
 580        IWL_TX_FIFO_VI,
 581        IWL_TX_FIFO_BE,
 582        IWL_TX_FIFO_BK,
 583};
 584
 585static const u8 iwlagn_bss_ac_to_queue[] = {
 586        0, 1, 2, 3,
 587};
 588
 589static const u8 iwlagn_pan_ac_to_fifo[] = {
 590        IWL_TX_FIFO_VO_IPAN,
 591        IWL_TX_FIFO_VI_IPAN,
 592        IWL_TX_FIFO_BE_IPAN,
 593        IWL_TX_FIFO_BK_IPAN,
 594};
 595
 596static const u8 iwlagn_pan_ac_to_queue[] = {
 597        7, 6, 5, 4,
 598};
 599
 600static void iwl_init_context(struct iwl_priv *priv, u32 ucode_flags)
 601{
 602        int i;
 603
 604        /*
 605         * The default context is always valid,
 606         * the PAN context depends on uCode.
 607         */
 608        priv->valid_contexts = BIT(IWL_RXON_CTX_BSS);
 609        if (ucode_flags & IWL_UCODE_TLV_FLAGS_PAN)
 610                priv->valid_contexts |= BIT(IWL_RXON_CTX_PAN);
 611
 612        for (i = 0; i < NUM_IWL_RXON_CTX; i++)
 613                priv->contexts[i].ctxid = i;
 614
 615        priv->contexts[IWL_RXON_CTX_BSS].always_active = true;
 616        priv->contexts[IWL_RXON_CTX_BSS].is_active = true;
 617        priv->contexts[IWL_RXON_CTX_BSS].rxon_cmd = REPLY_RXON;
 618        priv->contexts[IWL_RXON_CTX_BSS].rxon_timing_cmd = REPLY_RXON_TIMING;
 619        priv->contexts[IWL_RXON_CTX_BSS].rxon_assoc_cmd = REPLY_RXON_ASSOC;
 620        priv->contexts[IWL_RXON_CTX_BSS].qos_cmd = REPLY_QOS_PARAM;
 621        priv->contexts[IWL_RXON_CTX_BSS].ap_sta_id = IWL_AP_ID;
 622        priv->contexts[IWL_RXON_CTX_BSS].wep_key_cmd = REPLY_WEPKEY;
 623        priv->contexts[IWL_RXON_CTX_BSS].bcast_sta_id = IWLAGN_BROADCAST_ID;
 624        priv->contexts[IWL_RXON_CTX_BSS].exclusive_interface_modes =
 625                BIT(NL80211_IFTYPE_ADHOC) | BIT(NL80211_IFTYPE_MONITOR);
 626        priv->contexts[IWL_RXON_CTX_BSS].interface_modes =
 627                BIT(NL80211_IFTYPE_STATION);
 628        priv->contexts[IWL_RXON_CTX_BSS].ap_devtype = RXON_DEV_TYPE_AP;
 629        priv->contexts[IWL_RXON_CTX_BSS].ibss_devtype = RXON_DEV_TYPE_IBSS;
 630        priv->contexts[IWL_RXON_CTX_BSS].station_devtype = RXON_DEV_TYPE_ESS;
 631        priv->contexts[IWL_RXON_CTX_BSS].unused_devtype = RXON_DEV_TYPE_ESS;
 632        memcpy(priv->contexts[IWL_RXON_CTX_BSS].ac_to_queue,
 633               iwlagn_bss_ac_to_queue, sizeof(iwlagn_bss_ac_to_queue));
 634        memcpy(priv->contexts[IWL_RXON_CTX_BSS].ac_to_fifo,
 635               iwlagn_bss_ac_to_fifo, sizeof(iwlagn_bss_ac_to_fifo));
 636
 637        priv->contexts[IWL_RXON_CTX_PAN].rxon_cmd = REPLY_WIPAN_RXON;
 638        priv->contexts[IWL_RXON_CTX_PAN].rxon_timing_cmd =
 639                REPLY_WIPAN_RXON_TIMING;
 640        priv->contexts[IWL_RXON_CTX_PAN].rxon_assoc_cmd =
 641                REPLY_WIPAN_RXON_ASSOC;
 642        priv->contexts[IWL_RXON_CTX_PAN].qos_cmd = REPLY_WIPAN_QOS_PARAM;
 643        priv->contexts[IWL_RXON_CTX_PAN].ap_sta_id = IWL_AP_ID_PAN;
 644        priv->contexts[IWL_RXON_CTX_PAN].wep_key_cmd = REPLY_WIPAN_WEPKEY;
 645        priv->contexts[IWL_RXON_CTX_PAN].bcast_sta_id = IWLAGN_PAN_BCAST_ID;
 646        priv->contexts[IWL_RXON_CTX_PAN].station_flags = STA_FLG_PAN_STATION;
 647        priv->contexts[IWL_RXON_CTX_PAN].interface_modes =
 648                BIT(NL80211_IFTYPE_STATION) | BIT(NL80211_IFTYPE_AP);
 649
 650        priv->contexts[IWL_RXON_CTX_PAN].ap_devtype = RXON_DEV_TYPE_CP;
 651        priv->contexts[IWL_RXON_CTX_PAN].station_devtype = RXON_DEV_TYPE_2STA;
 652        priv->contexts[IWL_RXON_CTX_PAN].unused_devtype = RXON_DEV_TYPE_P2P;
 653        memcpy(priv->contexts[IWL_RXON_CTX_PAN].ac_to_queue,
 654               iwlagn_pan_ac_to_queue, sizeof(iwlagn_pan_ac_to_queue));
 655        memcpy(priv->contexts[IWL_RXON_CTX_PAN].ac_to_fifo,
 656               iwlagn_pan_ac_to_fifo, sizeof(iwlagn_pan_ac_to_fifo));
 657        priv->contexts[IWL_RXON_CTX_PAN].mcast_queue = IWL_IPAN_MCAST_QUEUE;
 658
 659        BUILD_BUG_ON(NUM_IWL_RXON_CTX != 2);
 660}
 661
 662static void iwl_rf_kill_ct_config(struct iwl_priv *priv)
 663{
 664        struct iwl_ct_kill_config cmd;
 665        struct iwl_ct_kill_throttling_config adv_cmd;
 666        int ret = 0;
 667
 668        iwl_write32(priv->trans, CSR_UCODE_DRV_GP1_CLR,
 669                    CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
 670
 671        priv->thermal_throttle.ct_kill_toggle = false;
 672
 673        if (priv->lib->support_ct_kill_exit) {
 674                adv_cmd.critical_temperature_enter =
 675                        cpu_to_le32(priv->hw_params.ct_kill_threshold);
 676                adv_cmd.critical_temperature_exit =
 677                        cpu_to_le32(priv->hw_params.ct_kill_exit_threshold);
 678
 679                ret = iwl_dvm_send_cmd_pdu(priv,
 680                                       REPLY_CT_KILL_CONFIG_CMD,
 681                                       0, sizeof(adv_cmd), &adv_cmd);
 682                if (ret)
 683                        IWL_ERR(priv, "REPLY_CT_KILL_CONFIG_CMD failed\n");
 684                else
 685                        IWL_DEBUG_INFO(priv, "REPLY_CT_KILL_CONFIG_CMD "
 686                                "succeeded, critical temperature enter is %d,"
 687                                "exit is %d\n",
 688                                priv->hw_params.ct_kill_threshold,
 689                                priv->hw_params.ct_kill_exit_threshold);
 690        } else {
 691                cmd.critical_temperature_R =
 692                        cpu_to_le32(priv->hw_params.ct_kill_threshold);
 693
 694                ret = iwl_dvm_send_cmd_pdu(priv,
 695                                       REPLY_CT_KILL_CONFIG_CMD,
 696                                       0, sizeof(cmd), &cmd);
 697                if (ret)
 698                        IWL_ERR(priv, "REPLY_CT_KILL_CONFIG_CMD failed\n");
 699                else
 700                        IWL_DEBUG_INFO(priv, "REPLY_CT_KILL_CONFIG_CMD "
 701                                "succeeded, "
 702                                "critical temperature is %d\n",
 703                                priv->hw_params.ct_kill_threshold);
 704        }
 705}
 706
 707static int iwlagn_send_calib_cfg_rt(struct iwl_priv *priv, u32 cfg)
 708{
 709        struct iwl_calib_cfg_cmd calib_cfg_cmd;
 710        struct iwl_host_cmd cmd = {
 711                .id = CALIBRATION_CFG_CMD,
 712                .len = { sizeof(struct iwl_calib_cfg_cmd), },
 713                .data = { &calib_cfg_cmd, },
 714        };
 715
 716        memset(&calib_cfg_cmd, 0, sizeof(calib_cfg_cmd));
 717        calib_cfg_cmd.ucd_calib_cfg.once.is_enable = IWL_CALIB_RT_CFG_ALL;
 718        calib_cfg_cmd.ucd_calib_cfg.once.start = cpu_to_le32(cfg);
 719
 720        return iwl_dvm_send_cmd(priv, &cmd);
 721}
 722
 723
 724static int iwlagn_send_tx_ant_config(struct iwl_priv *priv, u8 valid_tx_ant)
 725{
 726        struct iwl_tx_ant_config_cmd tx_ant_cmd = {
 727          .valid = cpu_to_le32(valid_tx_ant),
 728        };
 729
 730        if (IWL_UCODE_API(priv->fw->ucode_ver) > 1) {
 731                IWL_DEBUG_HC(priv, "select valid tx ant: %u\n", valid_tx_ant);
 732                return iwl_dvm_send_cmd_pdu(priv, TX_ANT_CONFIGURATION_CMD, 0,
 733                                        sizeof(struct iwl_tx_ant_config_cmd),
 734                                        &tx_ant_cmd);
 735        } else {
 736                IWL_DEBUG_HC(priv, "TX_ANT_CONFIGURATION_CMD not supported\n");
 737                return -EOPNOTSUPP;
 738        }
 739}
 740
 741static void iwl_send_bt_config(struct iwl_priv *priv)
 742{
 743        struct iwl_bt_cmd bt_cmd = {
 744                .lead_time = BT_LEAD_TIME_DEF,
 745                .max_kill = BT_MAX_KILL_DEF,
 746                .kill_ack_mask = 0,
 747                .kill_cts_mask = 0,
 748        };
 749
 750        if (!iwlwifi_mod_params.bt_coex_active)
 751                bt_cmd.flags = BT_COEX_DISABLE;
 752        else
 753                bt_cmd.flags = BT_COEX_ENABLE;
 754
 755        priv->bt_enable_flag = bt_cmd.flags;
 756        IWL_DEBUG_INFO(priv, "BT coex %s\n",
 757                (bt_cmd.flags == BT_COEX_DISABLE) ? "disable" : "active");
 758
 759        if (iwl_dvm_send_cmd_pdu(priv, REPLY_BT_CONFIG,
 760                             0, sizeof(struct iwl_bt_cmd), &bt_cmd))
 761                IWL_ERR(priv, "failed to send BT Coex Config\n");
 762}
 763
 764/*
 765 * iwl_alive_start - called after REPLY_ALIVE notification received
 766 *                   from protocol/runtime uCode (initialization uCode's
 767 *                   Alive gets handled by iwl_init_alive_start()).
 768 */
 769int iwl_alive_start(struct iwl_priv *priv)
 770{
 771        int ret = 0;
 772        struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
 773
 774        IWL_DEBUG_INFO(priv, "Runtime Alive received.\n");
 775
 776        /* After the ALIVE response, we can send host commands to the uCode */
 777        set_bit(STATUS_ALIVE, &priv->status);
 778
 779        if (iwl_is_rfkill(priv))
 780                return -ERFKILL;
 781
 782        if (priv->event_log.ucode_trace) {
 783                /* start collecting data now */
 784                mod_timer(&priv->ucode_trace, jiffies);
 785        }
 786
 787        /* download priority table before any calibration request */
 788        if (priv->lib->bt_params &&
 789            priv->lib->bt_params->advanced_bt_coexist) {
 790                /* Configure Bluetooth device coexistence support */
 791                if (priv->lib->bt_params->bt_sco_disable)
 792                        priv->bt_enable_pspoll = false;
 793                else
 794                        priv->bt_enable_pspoll = true;
 795
 796                priv->bt_valid = IWLAGN_BT_ALL_VALID_MSK;
 797                priv->kill_ack_mask = IWLAGN_BT_KILL_ACK_MASK_DEFAULT;
 798                priv->kill_cts_mask = IWLAGN_BT_KILL_CTS_MASK_DEFAULT;
 799                iwlagn_send_advance_bt_config(priv);
 800                priv->bt_valid = IWLAGN_BT_VALID_ENABLE_FLAGS;
 801                priv->cur_rssi_ctx = NULL;
 802
 803                iwl_send_prio_tbl(priv);
 804
 805                /* FIXME: w/a to force change uCode BT state machine */
 806                ret = iwl_send_bt_env(priv, IWL_BT_COEX_ENV_OPEN,
 807                                         BT_COEX_PRIO_TBL_EVT_INIT_CALIB2);
 808                if (ret)
 809                        return ret;
 810                ret = iwl_send_bt_env(priv, IWL_BT_COEX_ENV_CLOSE,
 811                                         BT_COEX_PRIO_TBL_EVT_INIT_CALIB2);
 812                if (ret)
 813                        return ret;
 814        } else if (priv->lib->bt_params) {
 815                /*
 816                 * default is 2-wire BT coexexistence support
 817                 */
 818                iwl_send_bt_config(priv);
 819        }
 820
 821        /*
 822         * Perform runtime calibrations, including DC calibration.
 823         */
 824        iwlagn_send_calib_cfg_rt(priv, IWL_CALIB_CFG_DC_IDX);
 825
 826        ieee80211_wake_queues(priv->hw);
 827
 828        /* Configure Tx antenna selection based on H/W config */
 829        iwlagn_send_tx_ant_config(priv, priv->nvm_data->valid_tx_ant);
 830
 831        if (iwl_is_associated_ctx(ctx) && !priv->wowlan) {
 832                struct iwl_rxon_cmd *active_rxon =
 833                                (struct iwl_rxon_cmd *)&ctx->active;
 834                /* apply any changes in staging */
 835                ctx->staging.filter_flags |= RXON_FILTER_ASSOC_MSK;
 836                active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
 837        } else {
 838                struct iwl_rxon_context *tmp;
 839                /* Initialize our rx_config data */
 840                for_each_context(priv, tmp)
 841                        iwl_connection_init_rx_config(priv, tmp);
 842
 843                iwlagn_set_rxon_chain(priv, ctx);
 844        }
 845
 846        if (!priv->wowlan) {
 847                /* WoWLAN ucode will not reply in the same way, skip it */
 848                iwl_reset_run_time_calib(priv);
 849        }
 850
 851        set_bit(STATUS_READY, &priv->status);
 852
 853        /* Configure the adapter for unassociated operation */
 854        ret = iwlagn_commit_rxon(priv, ctx);
 855        if (ret)
 856                return ret;
 857
 858        /* At this point, the NIC is initialized and operational */
 859        iwl_rf_kill_ct_config(priv);
 860
 861        IWL_DEBUG_INFO(priv, "ALIVE processing complete.\n");
 862
 863        return iwl_power_update_mode(priv, true);
 864}
 865
 866/**
 867 * iwl_clear_driver_stations - clear knowledge of all stations from driver
 868 * @priv: iwl priv struct
 869 *
 870 * This is called during iwl_down() to make sure that in the case
 871 * we're coming there from a hardware restart mac80211 will be
 872 * able to reconfigure stations -- if we're getting there in the
 873 * normal down flow then the stations will already be cleared.
 874 */
 875static void iwl_clear_driver_stations(struct iwl_priv *priv)
 876{
 877        struct iwl_rxon_context *ctx;
 878
 879        spin_lock_bh(&priv->sta_lock);
 880        memset(priv->stations, 0, sizeof(priv->stations));
 881        priv->num_stations = 0;
 882
 883        priv->ucode_key_table = 0;
 884
 885        for_each_context(priv, ctx) {
 886                /*
 887                 * Remove all key information that is not stored as part
 888                 * of station information since mac80211 may not have had
 889                 * a chance to remove all the keys. When device is
 890                 * reconfigured by mac80211 after an error all keys will
 891                 * be reconfigured.
 892                 */
 893                memset(ctx->wep_keys, 0, sizeof(ctx->wep_keys));
 894                ctx->key_mapping_keys = 0;
 895        }
 896
 897        spin_unlock_bh(&priv->sta_lock);
 898}
 899
 900void iwl_down(struct iwl_priv *priv)
 901{
 902        int exit_pending;
 903
 904        IWL_DEBUG_INFO(priv, DRV_NAME " is going down\n");
 905
 906        lockdep_assert_held(&priv->mutex);
 907
 908        iwl_scan_cancel_timeout(priv, 200);
 909
 910        exit_pending =
 911                test_and_set_bit(STATUS_EXIT_PENDING, &priv->status);
 912
 913        iwl_clear_ucode_stations(priv, NULL);
 914        iwl_dealloc_bcast_stations(priv);
 915        iwl_clear_driver_stations(priv);
 916
 917        /* reset BT coex data */
 918        priv->bt_status = 0;
 919        priv->cur_rssi_ctx = NULL;
 920        priv->bt_is_sco = 0;
 921        if (priv->lib->bt_params)
 922                priv->bt_traffic_load =
 923                         priv->lib->bt_params->bt_init_traffic_load;
 924        else
 925                priv->bt_traffic_load = 0;
 926        priv->bt_full_concurrent = false;
 927        priv->bt_ci_compliance = 0;
 928
 929        /* Wipe out the EXIT_PENDING status bit if we are not actually
 930         * exiting the module */
 931        if (!exit_pending)
 932                clear_bit(STATUS_EXIT_PENDING, &priv->status);
 933
 934        if (priv->mac80211_registered)
 935                ieee80211_stop_queues(priv->hw);
 936
 937        priv->ucode_loaded = false;
 938        iwl_trans_stop_device(priv->trans);
 939
 940        /* Set num_aux_in_flight must be done after the transport is stopped */
 941        atomic_set(&priv->num_aux_in_flight, 0);
 942
 943        /* Clear out all status bits but a few that are stable across reset */
 944        priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
 945                                STATUS_RF_KILL_HW |
 946                        test_bit(STATUS_FW_ERROR, &priv->status) <<
 947                                STATUS_FW_ERROR |
 948                        test_bit(STATUS_EXIT_PENDING, &priv->status) <<
 949                                STATUS_EXIT_PENDING;
 950
 951        dev_kfree_skb(priv->beacon_skb);
 952        priv->beacon_skb = NULL;
 953}
 954
 955/*****************************************************************************
 956 *
 957 * Workqueue callbacks
 958 *
 959 *****************************************************************************/
 960
 961static void iwl_bg_run_time_calib_work(struct work_struct *work)
 962{
 963        struct iwl_priv *priv = container_of(work, struct iwl_priv,
 964                        run_time_calib_work);
 965
 966        mutex_lock(&priv->mutex);
 967
 968        if (test_bit(STATUS_EXIT_PENDING, &priv->status) ||
 969            test_bit(STATUS_SCANNING, &priv->status)) {
 970                mutex_unlock(&priv->mutex);
 971                return;
 972        }
 973
 974        if (priv->start_calib) {
 975                iwl_chain_noise_calibration(priv);
 976                iwl_sensitivity_calibration(priv);
 977        }
 978
 979        mutex_unlock(&priv->mutex);
 980}
 981
 982void iwlagn_prepare_restart(struct iwl_priv *priv)
 983{
 984        bool bt_full_concurrent;
 985        u8 bt_ci_compliance;
 986        u8 bt_load;
 987        u8 bt_status;
 988        bool bt_is_sco;
 989        int i;
 990
 991        lockdep_assert_held(&priv->mutex);
 992
 993        priv->is_open = 0;
 994
 995        /*
 996         * __iwl_down() will clear the BT status variables,
 997         * which is correct, but when we restart we really
 998         * want to keep them so restore them afterwards.
 999         *
1000         * The restart process will later pick them up and
1001         * re-configure the hw when we reconfigure the BT
1002         * command.
1003         */
1004        bt_full_concurrent = priv->bt_full_concurrent;
1005        bt_ci_compliance = priv->bt_ci_compliance;
1006        bt_load = priv->bt_traffic_load;
1007        bt_status = priv->bt_status;
1008        bt_is_sco = priv->bt_is_sco;
1009
1010        iwl_down(priv);
1011
1012        priv->bt_full_concurrent = bt_full_concurrent;
1013        priv->bt_ci_compliance = bt_ci_compliance;
1014        priv->bt_traffic_load = bt_load;
1015        priv->bt_status = bt_status;
1016        priv->bt_is_sco = bt_is_sco;
1017
1018        /* reset aggregation queues */
1019        for (i = IWLAGN_FIRST_AMPDU_QUEUE; i < IWL_MAX_HW_QUEUES; i++)
1020                priv->queue_to_mac80211[i] = IWL_INVALID_MAC80211_QUEUE;
1021        /* and stop counts */
1022        for (i = 0; i < IWL_MAX_HW_QUEUES; i++)
1023                atomic_set(&priv->queue_stop_count[i], 0);
1024
1025        memset(priv->agg_q_alloc, 0, sizeof(priv->agg_q_alloc));
1026}
1027
1028static void iwl_bg_restart(struct work_struct *data)
1029{
1030        struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
1031
1032        if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1033                return;
1034
1035        if (test_and_clear_bit(STATUS_FW_ERROR, &priv->status)) {
1036                mutex_lock(&priv->mutex);
1037                iwlagn_prepare_restart(priv);
1038                mutex_unlock(&priv->mutex);
1039                iwl_cancel_deferred_work(priv);
1040                if (priv->mac80211_registered)
1041                        ieee80211_restart_hw(priv->hw);
1042                else
1043                        IWL_ERR(priv,
1044                                "Cannot request restart before registering with mac80211\n");
1045        } else {
1046                WARN_ON(1);
1047        }
1048}
1049
1050/*****************************************************************************
1051 *
1052 * driver setup and teardown
1053 *
1054 *****************************************************************************/
1055
1056static void iwl_setup_deferred_work(struct iwl_priv *priv)
1057{
1058        priv->workqueue = alloc_ordered_workqueue(DRV_NAME, 0);
1059
1060        INIT_WORK(&priv->restart, iwl_bg_restart);
1061        INIT_WORK(&priv->beacon_update, iwl_bg_beacon_update);
1062        INIT_WORK(&priv->run_time_calib_work, iwl_bg_run_time_calib_work);
1063        INIT_WORK(&priv->tx_flush, iwl_bg_tx_flush);
1064        INIT_WORK(&priv->bt_full_concurrency, iwl_bg_bt_full_concurrency);
1065        INIT_WORK(&priv->bt_runtime_config, iwl_bg_bt_runtime_config);
1066
1067        iwl_setup_scan_deferred_work(priv);
1068
1069        if (priv->lib->bt_params)
1070                iwlagn_bt_setup_deferred_work(priv);
1071
1072        timer_setup(&priv->statistics_periodic, iwl_bg_statistics_periodic, 0);
1073
1074        timer_setup(&priv->ucode_trace, iwl_bg_ucode_trace, 0);
1075}
1076
1077void iwl_cancel_deferred_work(struct iwl_priv *priv)
1078{
1079        if (priv->lib->bt_params)
1080                iwlagn_bt_cancel_deferred_work(priv);
1081
1082        cancel_work_sync(&priv->run_time_calib_work);
1083        cancel_work_sync(&priv->beacon_update);
1084
1085        iwl_cancel_scan_deferred_work(priv);
1086
1087        cancel_work_sync(&priv->bt_full_concurrency);
1088        cancel_work_sync(&priv->bt_runtime_config);
1089
1090        del_timer_sync(&priv->statistics_periodic);
1091        del_timer_sync(&priv->ucode_trace);
1092}
1093
1094static int iwl_init_drv(struct iwl_priv *priv)
1095{
1096        spin_lock_init(&priv->sta_lock);
1097
1098        mutex_init(&priv->mutex);
1099
1100        INIT_LIST_HEAD(&priv->calib_results);
1101
1102        priv->band = NL80211_BAND_2GHZ;
1103
1104        priv->plcp_delta_threshold = priv->lib->plcp_delta_threshold;
1105
1106        priv->iw_mode = NL80211_IFTYPE_STATION;
1107        priv->current_ht_config.smps = IEEE80211_SMPS_STATIC;
1108        priv->missed_beacon_threshold = IWL_MISSED_BEACON_THRESHOLD_DEF;
1109        priv->agg_tids_count = 0;
1110
1111        priv->rx_statistics_jiffies = jiffies;
1112
1113        /* Choose which receivers/antennas to use */
1114        iwlagn_set_rxon_chain(priv, &priv->contexts[IWL_RXON_CTX_BSS]);
1115
1116        iwl_init_scan_params(priv);
1117
1118        /* init bt coex */
1119        if (priv->lib->bt_params &&
1120            priv->lib->bt_params->advanced_bt_coexist) {
1121                priv->kill_ack_mask = IWLAGN_BT_KILL_ACK_MASK_DEFAULT;
1122                priv->kill_cts_mask = IWLAGN_BT_KILL_CTS_MASK_DEFAULT;
1123                priv->bt_valid = IWLAGN_BT_ALL_VALID_MSK;
1124                priv->bt_on_thresh = BT_ON_THRESHOLD_DEF;
1125                priv->bt_duration = BT_DURATION_LIMIT_DEF;
1126                priv->dynamic_frag_thresh = BT_FRAG_THRESHOLD_DEF;
1127        }
1128
1129        return 0;
1130}
1131
1132static void iwl_uninit_drv(struct iwl_priv *priv)
1133{
1134        kfree(priv->scan_cmd);
1135        kfree(priv->beacon_cmd);
1136        kfree(rcu_dereference_raw(priv->noa_data));
1137        iwl_calib_free_results(priv);
1138#ifdef CONFIG_IWLWIFI_DEBUGFS
1139        kfree(priv->wowlan_sram);
1140#endif
1141}
1142
1143static void iwl_set_hw_params(struct iwl_priv *priv)
1144{
1145        if (priv->cfg->ht_params)
1146                priv->hw_params.use_rts_for_aggregation =
1147                        priv->cfg->ht_params->use_rts_for_aggregation;
1148
1149        /* Device-specific setup */
1150        priv->lib->set_hw_params(priv);
1151}
1152
1153
1154
1155/* show what optional capabilities we have */
1156static void iwl_option_config(struct iwl_priv *priv)
1157{
1158#ifdef CONFIG_IWLWIFI_DEBUG
1159        IWL_INFO(priv, "CONFIG_IWLWIFI_DEBUG enabled\n");
1160#else
1161        IWL_INFO(priv, "CONFIG_IWLWIFI_DEBUG disabled\n");
1162#endif
1163
1164#ifdef CONFIG_IWLWIFI_DEBUGFS
1165        IWL_INFO(priv, "CONFIG_IWLWIFI_DEBUGFS enabled\n");
1166#else
1167        IWL_INFO(priv, "CONFIG_IWLWIFI_DEBUGFS disabled\n");
1168#endif
1169
1170#ifdef CONFIG_IWLWIFI_DEVICE_TRACING
1171        IWL_INFO(priv, "CONFIG_IWLWIFI_DEVICE_TRACING enabled\n");
1172#else
1173        IWL_INFO(priv, "CONFIG_IWLWIFI_DEVICE_TRACING disabled\n");
1174#endif
1175}
1176
1177static int iwl_eeprom_init_hw_params(struct iwl_priv *priv)
1178{
1179        struct iwl_nvm_data *data = priv->nvm_data;
1180
1181        if (data->sku_cap_11n_enable &&
1182            !priv->cfg->ht_params) {
1183                IWL_ERR(priv, "Invalid 11n configuration\n");
1184                return -EINVAL;
1185        }
1186
1187        if (!data->sku_cap_11n_enable && !data->sku_cap_band_24ghz_enable &&
1188            !data->sku_cap_band_52ghz_enable) {
1189                IWL_ERR(priv, "Invalid device sku\n");
1190                return -EINVAL;
1191        }
1192
1193        IWL_DEBUG_INFO(priv,
1194                       "Device SKU: 24GHz %s %s, 52GHz %s %s, 11.n %s %s\n",
1195                       data->sku_cap_band_24ghz_enable ? "" : "NOT", "enabled",
1196                       data->sku_cap_band_52ghz_enable ? "" : "NOT", "enabled",
1197                       data->sku_cap_11n_enable ? "" : "NOT", "enabled");
1198
1199        priv->hw_params.tx_chains_num =
1200                num_of_ant(data->valid_tx_ant);
1201        if (priv->cfg->rx_with_siso_diversity)
1202                priv->hw_params.rx_chains_num = 1;
1203        else
1204                priv->hw_params.rx_chains_num =
1205                        num_of_ant(data->valid_rx_ant);
1206
1207        IWL_DEBUG_INFO(priv, "Valid Tx ant: 0x%X, Valid Rx ant: 0x%X\n",
1208                       data->valid_tx_ant,
1209                       data->valid_rx_ant);
1210
1211        return 0;
1212}
1213
1214static int iwl_nvm_check_version(struct iwl_nvm_data *data,
1215                                 struct iwl_trans *trans)
1216{
1217        if (data->nvm_version >= trans->cfg->nvm_ver ||
1218            data->calib_version >= trans->cfg->nvm_calib_ver) {
1219                IWL_DEBUG_INFO(trans, "device EEPROM VER=0x%x, CALIB=0x%x\n",
1220                               data->nvm_version, data->calib_version);
1221                return 0;
1222        }
1223
1224        IWL_ERR(trans,
1225                "Unsupported (too old) EEPROM VER=0x%x < 0x%x CALIB=0x%x < 0x%x\n",
1226                data->nvm_version, trans->cfg->nvm_ver,
1227                data->calib_version,  trans->cfg->nvm_calib_ver);
1228        return -EINVAL;
1229}
1230
1231static struct iwl_op_mode *iwl_op_mode_dvm_start(struct iwl_trans *trans,
1232                                                 const struct iwl_cfg *cfg,
1233                                                 const struct iwl_fw *fw,
1234                                                 struct dentry *dbgfs_dir)
1235{
1236        struct iwl_priv *priv;
1237        struct ieee80211_hw *hw;
1238        struct iwl_op_mode *op_mode;
1239        u16 num_mac;
1240        u32 ucode_flags;
1241        struct iwl_trans_config trans_cfg = {};
1242        static const u8 no_reclaim_cmds[] = {
1243                REPLY_RX_PHY_CMD,
1244                REPLY_RX_MPDU_CMD,
1245                REPLY_COMPRESSED_BA,
1246                STATISTICS_NOTIFICATION,
1247                REPLY_TX,
1248        };
1249        int i;
1250
1251        /************************
1252         * 1. Allocating HW data
1253         ************************/
1254        hw = iwl_alloc_all();
1255        if (!hw) {
1256                pr_err("%s: Cannot allocate network device\n", trans->name);
1257                goto out;
1258        }
1259
1260        op_mode = hw->priv;
1261        op_mode->ops = &iwl_dvm_ops;
1262        priv = IWL_OP_MODE_GET_DVM(op_mode);
1263        priv->trans = trans;
1264        priv->dev = trans->dev;
1265        priv->cfg = cfg;
1266        priv->fw = fw;
1267
1268        switch (priv->trans->trans_cfg->device_family) {
1269        case IWL_DEVICE_FAMILY_1000:
1270        case IWL_DEVICE_FAMILY_100:
1271                priv->lib = &iwl_dvm_1000_cfg;
1272                break;
1273        case IWL_DEVICE_FAMILY_2000:
1274                priv->lib = &iwl_dvm_2000_cfg;
1275                break;
1276        case IWL_DEVICE_FAMILY_105:
1277                priv->lib = &iwl_dvm_105_cfg;
1278                break;
1279        case IWL_DEVICE_FAMILY_2030:
1280        case IWL_DEVICE_FAMILY_135:
1281                priv->lib = &iwl_dvm_2030_cfg;
1282                break;
1283        case IWL_DEVICE_FAMILY_5000:
1284                priv->lib = &iwl_dvm_5000_cfg;
1285                break;
1286        case IWL_DEVICE_FAMILY_5150:
1287                priv->lib = &iwl_dvm_5150_cfg;
1288                break;
1289        case IWL_DEVICE_FAMILY_6000:
1290        case IWL_DEVICE_FAMILY_6000i:
1291                priv->lib = &iwl_dvm_6000_cfg;
1292                break;
1293        case IWL_DEVICE_FAMILY_6005:
1294                priv->lib = &iwl_dvm_6005_cfg;
1295                break;
1296        case IWL_DEVICE_FAMILY_6050:
1297        case IWL_DEVICE_FAMILY_6150:
1298                priv->lib = &iwl_dvm_6050_cfg;
1299                break;
1300        case IWL_DEVICE_FAMILY_6030:
1301                priv->lib = &iwl_dvm_6030_cfg;
1302                break;
1303        default:
1304                break;
1305        }
1306
1307        if (WARN_ON(!priv->lib))
1308                goto out_free_hw;
1309
1310        /*
1311         * Populate the state variables that the transport layer needs
1312         * to know about.
1313         */
1314        trans_cfg.op_mode = op_mode;
1315        trans_cfg.no_reclaim_cmds = no_reclaim_cmds;
1316        trans_cfg.n_no_reclaim_cmds = ARRAY_SIZE(no_reclaim_cmds);
1317
1318        switch (iwlwifi_mod_params.amsdu_size) {
1319        case IWL_AMSDU_DEF:
1320        case IWL_AMSDU_4K:
1321                trans_cfg.rx_buf_size = IWL_AMSDU_4K;
1322                break;
1323        case IWL_AMSDU_8K:
1324                trans_cfg.rx_buf_size = IWL_AMSDU_8K;
1325                break;
1326        case IWL_AMSDU_12K:
1327        default:
1328                trans_cfg.rx_buf_size = IWL_AMSDU_4K;
1329                pr_err("Unsupported amsdu_size: %d\n",
1330                       iwlwifi_mod_params.amsdu_size);
1331        }
1332
1333        trans_cfg.cmd_q_wdg_timeout = IWL_WATCHDOG_DISABLED;
1334
1335        trans_cfg.command_groups = iwl_dvm_groups;
1336        trans_cfg.command_groups_size = ARRAY_SIZE(iwl_dvm_groups);
1337
1338        trans_cfg.cmd_fifo = IWLAGN_CMD_FIFO_NUM;
1339        trans_cfg.cb_data_offs = offsetof(struct ieee80211_tx_info,
1340                                          driver_data[2]);
1341
1342        WARN_ON(sizeof(priv->transport_queue_stop) * BITS_PER_BYTE <
1343                priv->trans->trans_cfg->base_params->num_of_queues);
1344
1345        ucode_flags = fw->ucode_capa.flags;
1346
1347        if (ucode_flags & IWL_UCODE_TLV_FLAGS_PAN) {
1348                priv->sta_key_max_num = STA_KEY_MAX_NUM_PAN;
1349                trans_cfg.cmd_queue = IWL_IPAN_CMD_QUEUE_NUM;
1350        } else {
1351                priv->sta_key_max_num = STA_KEY_MAX_NUM;
1352                trans_cfg.cmd_queue = IWL_DEFAULT_CMD_QUEUE_NUM;
1353        }
1354
1355        /* Configure transport layer */
1356        iwl_trans_configure(priv->trans, &trans_cfg);
1357
1358        trans->rx_mpdu_cmd = REPLY_RX_MPDU_CMD;
1359        trans->rx_mpdu_cmd_hdr_size = sizeof(struct iwl_rx_mpdu_res_start);
1360        trans->command_groups = trans_cfg.command_groups;
1361        trans->command_groups_size = trans_cfg.command_groups_size;
1362
1363        /* At this point both hw and priv are allocated. */
1364
1365        SET_IEEE80211_DEV(priv->hw, priv->trans->dev);
1366
1367        iwl_option_config(priv);
1368
1369        IWL_DEBUG_INFO(priv, "*** LOAD DRIVER ***\n");
1370
1371        /* bt channel inhibition enabled*/
1372        priv->bt_ch_announce = true;
1373        IWL_DEBUG_INFO(priv, "BT channel inhibition is %s\n",
1374                       (priv->bt_ch_announce) ? "On" : "Off");
1375
1376        /* these spin locks will be used in apm_ops.init and EEPROM access
1377         * we should init now
1378         */
1379        spin_lock_init(&priv->statistics.lock);
1380
1381        /***********************
1382         * 2. Read REV register
1383         ***********************/
1384        IWL_INFO(priv, "Detected %s, REV=0x%X\n",
1385                priv->trans->name, priv->trans->hw_rev);
1386
1387        if (iwl_trans_start_hw(priv->trans))
1388                goto out_free_hw;
1389
1390        /* Read the EEPROM */
1391        if (iwl_read_eeprom(priv->trans, &priv->eeprom_blob,
1392                            &priv->eeprom_blob_size)) {
1393                IWL_ERR(priv, "Unable to init EEPROM\n");
1394                goto out_free_hw;
1395        }
1396
1397        /* Reset chip to save power until we load uCode during "up". */
1398        iwl_trans_stop_device(priv->trans);
1399
1400        priv->nvm_data = iwl_parse_eeprom_data(priv->trans, priv->cfg,
1401                                               priv->eeprom_blob,
1402                                               priv->eeprom_blob_size);
1403        if (!priv->nvm_data)
1404                goto out_free_eeprom_blob;
1405
1406        if (iwl_nvm_check_version(priv->nvm_data, priv->trans))
1407                goto out_free_eeprom;
1408
1409        if (iwl_eeprom_init_hw_params(priv))
1410                goto out_free_eeprom;
1411
1412        /* extract MAC Address */
1413        memcpy(priv->addresses[0].addr, priv->nvm_data->hw_addr, ETH_ALEN);
1414        IWL_DEBUG_INFO(priv, "MAC address: %pM\n", priv->addresses[0].addr);
1415        priv->hw->wiphy->addresses = priv->addresses;
1416        priv->hw->wiphy->n_addresses = 1;
1417        num_mac = priv->nvm_data->n_hw_addrs;
1418        if (num_mac > 1) {
1419                memcpy(priv->addresses[1].addr, priv->addresses[0].addr,
1420                       ETH_ALEN);
1421                priv->addresses[1].addr[5]++;
1422                priv->hw->wiphy->n_addresses++;
1423        }
1424
1425        /************************
1426         * 4. Setup HW constants
1427         ************************/
1428        iwl_set_hw_params(priv);
1429
1430        if (!(priv->nvm_data->sku_cap_ipan_enable)) {
1431                IWL_DEBUG_INFO(priv, "Your EEPROM disabled PAN\n");
1432                ucode_flags &= ~IWL_UCODE_TLV_FLAGS_PAN;
1433                /*
1434                 * if not PAN, then don't support P2P -- might be a uCode
1435                 * packaging bug or due to the eeprom check above
1436                 */
1437                priv->sta_key_max_num = STA_KEY_MAX_NUM;
1438                trans_cfg.cmd_queue = IWL_DEFAULT_CMD_QUEUE_NUM;
1439
1440                /* Configure transport layer again*/
1441                iwl_trans_configure(priv->trans, &trans_cfg);
1442        }
1443
1444        /*******************
1445         * 5. Setup priv
1446         *******************/
1447        for (i = 0; i < IWL_MAX_HW_QUEUES; i++) {
1448                priv->queue_to_mac80211[i] = IWL_INVALID_MAC80211_QUEUE;
1449                if (i < IWLAGN_FIRST_AMPDU_QUEUE &&
1450                    i != IWL_DEFAULT_CMD_QUEUE_NUM &&
1451                    i != IWL_IPAN_CMD_QUEUE_NUM)
1452                        priv->queue_to_mac80211[i] = i;
1453                atomic_set(&priv->queue_stop_count[i], 0);
1454        }
1455
1456        if (iwl_init_drv(priv))
1457                goto out_free_eeprom;
1458
1459        /* At this point both hw and priv are initialized. */
1460
1461        /********************
1462         * 6. Setup services
1463         ********************/
1464        iwl_setup_deferred_work(priv);
1465        iwl_setup_rx_handlers(priv);
1466
1467        iwl_power_initialize(priv);
1468        iwl_tt_initialize(priv);
1469
1470        snprintf(priv->hw->wiphy->fw_version,
1471                 sizeof(priv->hw->wiphy->fw_version),
1472                 "%s", fw->fw_version);
1473
1474        priv->new_scan_threshold_behaviour =
1475                !!(ucode_flags & IWL_UCODE_TLV_FLAGS_NEWSCAN);
1476
1477        priv->phy_calib_chain_noise_reset_cmd =
1478                fw->ucode_capa.standard_phy_calibration_size;
1479        priv->phy_calib_chain_noise_gain_cmd =
1480                fw->ucode_capa.standard_phy_calibration_size + 1;
1481
1482        /* initialize all valid contexts */
1483        iwl_init_context(priv, ucode_flags);
1484
1485        /**************************************************
1486         * This is still part of probe() in a sense...
1487         *
1488         * 7. Setup and register with mac80211 and debugfs
1489         **************************************************/
1490        if (iwlagn_mac_setup_register(priv, &fw->ucode_capa))
1491                goto out_destroy_workqueue;
1492
1493        iwl_dbgfs_register(priv, dbgfs_dir);
1494
1495        return op_mode;
1496
1497out_destroy_workqueue:
1498        iwl_tt_exit(priv);
1499        iwl_cancel_deferred_work(priv);
1500        destroy_workqueue(priv->workqueue);
1501        priv->workqueue = NULL;
1502        iwl_uninit_drv(priv);
1503out_free_eeprom_blob:
1504        kfree(priv->eeprom_blob);
1505out_free_eeprom:
1506        kfree(priv->nvm_data);
1507out_free_hw:
1508        ieee80211_free_hw(priv->hw);
1509out:
1510        op_mode = NULL;
1511        return op_mode;
1512}
1513
1514static void iwl_op_mode_dvm_stop(struct iwl_op_mode *op_mode)
1515{
1516        struct iwl_priv *priv = IWL_OP_MODE_GET_DVM(op_mode);
1517
1518        IWL_DEBUG_INFO(priv, "*** UNLOAD DRIVER ***\n");
1519
1520        iwlagn_mac_unregister(priv);
1521
1522        iwl_tt_exit(priv);
1523
1524        kfree(priv->eeprom_blob);
1525        kfree(priv->nvm_data);
1526
1527        /*netif_stop_queue(dev); */
1528        flush_workqueue(priv->workqueue);
1529
1530        /* ieee80211_unregister_hw calls iwlagn_mac_stop, which flushes
1531         * priv->workqueue... so we can't take down the workqueue
1532         * until now... */
1533        destroy_workqueue(priv->workqueue);
1534        priv->workqueue = NULL;
1535
1536        iwl_uninit_drv(priv);
1537
1538        dev_kfree_skb(priv->beacon_skb);
1539
1540        iwl_trans_op_mode_leave(priv->trans);
1541        ieee80211_free_hw(priv->hw);
1542}
1543
1544static const char * const desc_lookup_text[] = {
1545        "OK",
1546        "FAIL",
1547        "BAD_PARAM",
1548        "BAD_CHECKSUM",
1549        "NMI_INTERRUPT_WDG",
1550        "SYSASSERT",
1551        "FATAL_ERROR",
1552        "BAD_COMMAND",
1553        "HW_ERROR_TUNE_LOCK",
1554        "HW_ERROR_TEMPERATURE",
1555        "ILLEGAL_CHAN_FREQ",
1556        "VCC_NOT_STABLE",
1557        "FH_ERROR",
1558        "NMI_INTERRUPT_HOST",
1559        "NMI_INTERRUPT_ACTION_PT",
1560        "NMI_INTERRUPT_UNKNOWN",
1561        "UCODE_VERSION_MISMATCH",
1562        "HW_ERROR_ABS_LOCK",
1563        "HW_ERROR_CAL_LOCK_FAIL",
1564        "NMI_INTERRUPT_INST_ACTION_PT",
1565        "NMI_INTERRUPT_DATA_ACTION_PT",
1566        "NMI_TRM_HW_ER",
1567        "NMI_INTERRUPT_TRM",
1568        "NMI_INTERRUPT_BREAK_POINT",
1569        "DEBUG_0",
1570        "DEBUG_1",
1571        "DEBUG_2",
1572        "DEBUG_3",
1573};
1574
1575static struct { char *name; u8 num; } advanced_lookup[] = {
1576        { "NMI_INTERRUPT_WDG", 0x34 },
1577        { "SYSASSERT", 0x35 },
1578        { "UCODE_VERSION_MISMATCH", 0x37 },
1579        { "BAD_COMMAND", 0x38 },
1580        { "NMI_INTERRUPT_DATA_ACTION_PT", 0x3C },
1581        { "FATAL_ERROR", 0x3D },
1582        { "NMI_TRM_HW_ERR", 0x46 },
1583        { "NMI_INTERRUPT_TRM", 0x4C },
1584        { "NMI_INTERRUPT_BREAK_POINT", 0x54 },
1585        { "NMI_INTERRUPT_WDG_RXF_FULL", 0x5C },
1586        { "NMI_INTERRUPT_WDG_NO_RBD_RXF_FULL", 0x64 },
1587        { "NMI_INTERRUPT_HOST", 0x66 },
1588        { "NMI_INTERRUPT_ACTION_PT", 0x7C },
1589        { "NMI_INTERRUPT_UNKNOWN", 0x84 },
1590        { "NMI_INTERRUPT_INST_ACTION_PT", 0x86 },
1591        { "ADVANCED_SYSASSERT", 0 },
1592};
1593
1594static const char *desc_lookup(u32 num)
1595{
1596        int i;
1597        int max = ARRAY_SIZE(desc_lookup_text);
1598
1599        if (num < max)
1600                return desc_lookup_text[num];
1601
1602        max = ARRAY_SIZE(advanced_lookup) - 1;
1603        for (i = 0; i < max; i++) {
1604                if (advanced_lookup[i].num == num)
1605                        break;
1606        }
1607        return advanced_lookup[i].name;
1608}
1609
1610#define ERROR_START_OFFSET  (1 * sizeof(u32))
1611#define ERROR_ELEM_SIZE     (7 * sizeof(u32))
1612
1613static void iwl_dump_nic_error_log(struct iwl_priv *priv)
1614{
1615        struct iwl_trans *trans = priv->trans;
1616        u32 base;
1617        struct iwl_error_event_table table;
1618
1619        base = priv->device_pointers.error_event_table;
1620        if (priv->cur_ucode == IWL_UCODE_INIT) {
1621                if (!base)
1622                        base = priv->fw->init_errlog_ptr;
1623        } else {
1624                if (!base)
1625                        base = priv->fw->inst_errlog_ptr;
1626        }
1627
1628        if (!iwlagn_hw_valid_rtc_data_addr(base)) {
1629                IWL_ERR(priv,
1630                        "Not valid error log pointer 0x%08X for %s uCode\n",
1631                        base,
1632                        (priv->cur_ucode == IWL_UCODE_INIT)
1633                                        ? "Init" : "RT");
1634                return;
1635        }
1636
1637        /*TODO: Update dbgfs with ISR error stats obtained below */
1638        iwl_trans_read_mem_bytes(trans, base, &table, sizeof(table));
1639
1640        if (ERROR_START_OFFSET <= table.valid * ERROR_ELEM_SIZE) {
1641                IWL_ERR(trans, "Start IWL Error Log Dump:\n");
1642                IWL_ERR(trans, "Status: 0x%08lX, count: %d\n",
1643                        priv->status, table.valid);
1644        }
1645
1646        IWL_ERR(priv, "0x%08X | %-28s\n", table.error_id,
1647                desc_lookup(table.error_id));
1648        IWL_ERR(priv, "0x%08X | uPc\n", table.pc);
1649        IWL_ERR(priv, "0x%08X | branchlink1\n", table.blink1);
1650        IWL_ERR(priv, "0x%08X | branchlink2\n", table.blink2);
1651        IWL_ERR(priv, "0x%08X | interruptlink1\n", table.ilink1);
1652        IWL_ERR(priv, "0x%08X | interruptlink2\n", table.ilink2);
1653        IWL_ERR(priv, "0x%08X | data1\n", table.data1);
1654        IWL_ERR(priv, "0x%08X | data2\n", table.data2);
1655        IWL_ERR(priv, "0x%08X | line\n", table.line);
1656        IWL_ERR(priv, "0x%08X | beacon time\n", table.bcon_time);
1657        IWL_ERR(priv, "0x%08X | tsf low\n", table.tsf_low);
1658        IWL_ERR(priv, "0x%08X | tsf hi\n", table.tsf_hi);
1659        IWL_ERR(priv, "0x%08X | time gp1\n", table.gp1);
1660        IWL_ERR(priv, "0x%08X | time gp2\n", table.gp2);
1661        IWL_ERR(priv, "0x%08X | time gp3\n", table.gp3);
1662        IWL_ERR(priv, "0x%08X | uCode version\n", table.ucode_ver);
1663        IWL_ERR(priv, "0x%08X | hw version\n", table.hw_ver);
1664        IWL_ERR(priv, "0x%08X | board version\n", table.brd_ver);
1665        IWL_ERR(priv, "0x%08X | hcmd\n", table.hcmd);
1666        IWL_ERR(priv, "0x%08X | isr0\n", table.isr0);
1667        IWL_ERR(priv, "0x%08X | isr1\n", table.isr1);
1668        IWL_ERR(priv, "0x%08X | isr2\n", table.isr2);
1669        IWL_ERR(priv, "0x%08X | isr3\n", table.isr3);
1670        IWL_ERR(priv, "0x%08X | isr4\n", table.isr4);
1671        IWL_ERR(priv, "0x%08X | isr_pref\n", table.isr_pref);
1672        IWL_ERR(priv, "0x%08X | wait_event\n", table.wait_event);
1673        IWL_ERR(priv, "0x%08X | l2p_control\n", table.l2p_control);
1674        IWL_ERR(priv, "0x%08X | l2p_duration\n", table.l2p_duration);
1675        IWL_ERR(priv, "0x%08X | l2p_mhvalid\n", table.l2p_mhvalid);
1676        IWL_ERR(priv, "0x%08X | l2p_addr_match\n", table.l2p_addr_match);
1677        IWL_ERR(priv, "0x%08X | lmpm_pmg_sel\n", table.lmpm_pmg_sel);
1678        IWL_ERR(priv, "0x%08X | timestamp\n", table.u_timestamp);
1679        IWL_ERR(priv, "0x%08X | flow_handler\n", table.flow_handler);
1680}
1681
1682#define EVENT_START_OFFSET  (4 * sizeof(u32))
1683
1684/*
1685 * iwl_print_event_log - Dump error event log to syslog
1686 */
1687static int iwl_print_event_log(struct iwl_priv *priv, u32 start_idx,
1688                               u32 num_events, u32 mode,
1689                               int pos, char **buf, size_t bufsz)
1690{
1691        u32 i;
1692        u32 base;       /* SRAM byte address of event log header */
1693        u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
1694        u32 ptr;        /* SRAM byte address of log data */
1695        u32 ev, time, data; /* event log data */
1696
1697        struct iwl_trans *trans = priv->trans;
1698
1699        if (num_events == 0)
1700                return pos;
1701
1702        base = priv->device_pointers.log_event_table;
1703        if (priv->cur_ucode == IWL_UCODE_INIT) {
1704                if (!base)
1705                        base = priv->fw->init_evtlog_ptr;
1706        } else {
1707                if (!base)
1708                        base = priv->fw->inst_evtlog_ptr;
1709        }
1710
1711        if (mode == 0)
1712                event_size = 2 * sizeof(u32);
1713        else
1714                event_size = 3 * sizeof(u32);
1715
1716        ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
1717
1718        /* Make sure device is powered up for SRAM reads */
1719        if (!iwl_trans_grab_nic_access(trans))
1720                return pos;
1721
1722        /* Set starting address; reads will auto-increment */
1723        iwl_write32(trans, HBUS_TARG_MEM_RADDR, ptr);
1724
1725        /* "time" is actually "data" for mode 0 (no timestamp).
1726        * place event id # at far right for easier visual parsing. */
1727        for (i = 0; i < num_events; i++) {
1728                ev = iwl_read32(trans, HBUS_TARG_MEM_RDAT);
1729                time = iwl_read32(trans, HBUS_TARG_MEM_RDAT);
1730                if (mode == 0) {
1731                        /* data, ev */
1732                        if (bufsz) {
1733                                pos += scnprintf(*buf + pos, bufsz - pos,
1734                                                "EVT_LOG:0x%08x:%04u\n",
1735                                                time, ev);
1736                        } else {
1737                                trace_iwlwifi_dev_ucode_event(trans->dev, 0,
1738                                        time, ev);
1739                                IWL_ERR(priv, "EVT_LOG:0x%08x:%04u\n",
1740                                        time, ev);
1741                        }
1742                } else {
1743                        data = iwl_read32(trans, HBUS_TARG_MEM_RDAT);
1744                        if (bufsz) {
1745                                pos += scnprintf(*buf + pos, bufsz - pos,
1746                                                "EVT_LOGT:%010u:0x%08x:%04u\n",
1747                                                 time, data, ev);
1748                        } else {
1749                                IWL_ERR(priv, "EVT_LOGT:%010u:0x%08x:%04u\n",
1750                                        time, data, ev);
1751                                trace_iwlwifi_dev_ucode_event(trans->dev, time,
1752                                        data, ev);
1753                        }
1754                }
1755        }
1756
1757        /* Allow device to power down */
1758        iwl_trans_release_nic_access(trans);
1759        return pos;
1760}
1761
1762/*
1763 * iwl_print_last_event_logs - Dump the newest # of event log to syslog
1764 */
1765static int iwl_print_last_event_logs(struct iwl_priv *priv, u32 capacity,
1766                                    u32 num_wraps, u32 next_entry,
1767                                    u32 size, u32 mode,
1768                                    int pos, char **buf, size_t bufsz)
1769{
1770        /*
1771         * display the newest DEFAULT_LOG_ENTRIES entries
1772         * i.e the entries just before the next ont that uCode would fill.
1773         */
1774        if (num_wraps) {
1775                if (next_entry < size) {
1776                        pos = iwl_print_event_log(priv,
1777                                                capacity - (size - next_entry),
1778                                                size - next_entry, mode,
1779                                                pos, buf, bufsz);
1780                        pos = iwl_print_event_log(priv, 0,
1781                                                  next_entry, mode,
1782                                                  pos, buf, bufsz);
1783                } else
1784                        pos = iwl_print_event_log(priv, next_entry - size,
1785                                                  size, mode, pos, buf, bufsz);
1786        } else {
1787                if (next_entry < size) {
1788                        pos = iwl_print_event_log(priv, 0, next_entry,
1789                                                  mode, pos, buf, bufsz);
1790                } else {
1791                        pos = iwl_print_event_log(priv, next_entry - size,
1792                                                  size, mode, pos, buf, bufsz);
1793                }
1794        }
1795        return pos;
1796}
1797
1798#define DEFAULT_DUMP_EVENT_LOG_ENTRIES (20)
1799
1800int iwl_dump_nic_event_log(struct iwl_priv *priv, bool full_log,
1801                            char **buf)
1802{
1803        u32 base;       /* SRAM byte address of event log header */
1804        u32 capacity;   /* event log capacity in # entries */
1805        u32 mode;       /* 0 - no timestamp, 1 - timestamp recorded */
1806        u32 num_wraps;  /* # times uCode wrapped to top of log */
1807        u32 next_entry; /* index of next entry to be written by uCode */
1808        u32 size;       /* # entries that we'll print */
1809        u32 logsize;
1810        int pos = 0;
1811        size_t bufsz = 0;
1812        struct iwl_trans *trans = priv->trans;
1813
1814        base = priv->device_pointers.log_event_table;
1815        if (priv->cur_ucode == IWL_UCODE_INIT) {
1816                logsize = priv->fw->init_evtlog_size;
1817                if (!base)
1818                        base = priv->fw->init_evtlog_ptr;
1819        } else {
1820                logsize = priv->fw->inst_evtlog_size;
1821                if (!base)
1822                        base = priv->fw->inst_evtlog_ptr;
1823        }
1824
1825        if (!iwlagn_hw_valid_rtc_data_addr(base)) {
1826                IWL_ERR(priv,
1827                        "Invalid event log pointer 0x%08X for %s uCode\n",
1828                        base,
1829                        (priv->cur_ucode == IWL_UCODE_INIT)
1830                                        ? "Init" : "RT");
1831                return -EINVAL;
1832        }
1833
1834        /* event log header */
1835        capacity = iwl_trans_read_mem32(trans, base);
1836        mode = iwl_trans_read_mem32(trans, base + (1 * sizeof(u32)));
1837        num_wraps = iwl_trans_read_mem32(trans, base + (2 * sizeof(u32)));
1838        next_entry = iwl_trans_read_mem32(trans, base + (3 * sizeof(u32)));
1839
1840        if (capacity > logsize) {
1841                IWL_ERR(priv, "Log capacity %d is bogus, limit to %d "
1842                        "entries\n", capacity, logsize);
1843                capacity = logsize;
1844        }
1845
1846        if (next_entry > logsize) {
1847                IWL_ERR(priv, "Log write index %d is bogus, limit to %d\n",
1848                        next_entry, logsize);
1849                next_entry = logsize;
1850        }
1851
1852        size = num_wraps ? capacity : next_entry;
1853
1854        /* bail out if nothing in log */
1855        if (size == 0) {
1856                IWL_ERR(trans, "Start IWL Event Log Dump: nothing in log\n");
1857                return pos;
1858        }
1859
1860        if (!(iwl_have_debug_level(IWL_DL_FW)) && !full_log)
1861                size = (size > DEFAULT_DUMP_EVENT_LOG_ENTRIES)
1862                        ? DEFAULT_DUMP_EVENT_LOG_ENTRIES : size;
1863        IWL_ERR(priv, "Start IWL Event Log Dump: display last %u entries\n",
1864                size);
1865
1866#ifdef CONFIG_IWLWIFI_DEBUG
1867        if (buf) {
1868                if (full_log)
1869                        bufsz = capacity * 48;
1870                else
1871                        bufsz = size * 48;
1872                *buf = kmalloc(bufsz, GFP_KERNEL);
1873                if (!*buf)
1874                        return -ENOMEM;
1875        }
1876        if (iwl_have_debug_level(IWL_DL_FW) || full_log) {
1877                /*
1878                 * if uCode has wrapped back to top of log,
1879                 * start at the oldest entry,
1880                 * i.e the next one that uCode would fill.
1881                 */
1882                if (num_wraps)
1883                        pos = iwl_print_event_log(priv, next_entry,
1884                                                capacity - next_entry, mode,
1885                                                pos, buf, bufsz);
1886                /* (then/else) start at top of log */
1887                pos = iwl_print_event_log(priv, 0,
1888                                          next_entry, mode, pos, buf, bufsz);
1889        } else
1890                pos = iwl_print_last_event_logs(priv, capacity, num_wraps,
1891                                                next_entry, size, mode,
1892                                                pos, buf, bufsz);
1893#else
1894        pos = iwl_print_last_event_logs(priv, capacity, num_wraps,
1895                                        next_entry, size, mode,
1896                                        pos, buf, bufsz);
1897#endif
1898        return pos;
1899}
1900
1901static void iwlagn_fw_error(struct iwl_priv *priv, bool ondemand)
1902{
1903        unsigned int reload_msec;
1904        unsigned long reload_jiffies;
1905
1906        if (iwl_have_debug_level(IWL_DL_FW))
1907                iwl_print_rx_config_cmd(priv, IWL_RXON_CTX_BSS);
1908
1909        /* uCode is no longer loaded. */
1910        priv->ucode_loaded = false;
1911
1912        /* Set the FW error flag -- cleared on iwl_down */
1913        set_bit(STATUS_FW_ERROR, &priv->status);
1914
1915        iwl_abort_notification_waits(&priv->notif_wait);
1916
1917        /* Keep the restart process from trying to send host
1918         * commands by clearing the ready bit */
1919        clear_bit(STATUS_READY, &priv->status);
1920
1921        if (!ondemand) {
1922                /*
1923                 * If firmware keep reloading, then it indicate something
1924                 * serious wrong and firmware having problem to recover
1925                 * from it. Instead of keep trying which will fill the syslog
1926                 * and hang the system, let's just stop it
1927                 */
1928                reload_jiffies = jiffies;
1929                reload_msec = jiffies_to_msecs((long) reload_jiffies -
1930                                        (long) priv->reload_jiffies);
1931                priv->reload_jiffies = reload_jiffies;
1932                if (reload_msec <= IWL_MIN_RELOAD_DURATION) {
1933                        priv->reload_count++;
1934                        if (priv->reload_count >= IWL_MAX_CONTINUE_RELOAD_CNT) {
1935                                IWL_ERR(priv, "BUG_ON, Stop restarting\n");
1936                                return;
1937                        }
1938                } else
1939                        priv->reload_count = 0;
1940        }
1941
1942        if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
1943                if (iwlwifi_mod_params.fw_restart) {
1944                        IWL_DEBUG_FW(priv,
1945                                     "Restarting adapter due to uCode error.\n");
1946                        queue_work(priv->workqueue, &priv->restart);
1947                } else
1948                        IWL_DEBUG_FW(priv,
1949                                     "Detected FW error, but not restarting\n");
1950        }
1951}
1952
1953static void iwl_nic_error(struct iwl_op_mode *op_mode)
1954{
1955        struct iwl_priv *priv = IWL_OP_MODE_GET_DVM(op_mode);
1956
1957        IWL_ERR(priv, "Loaded firmware version: %s\n",
1958                priv->fw->fw_version);
1959
1960        iwl_dump_nic_error_log(priv);
1961        iwl_dump_nic_event_log(priv, false, NULL);
1962
1963        iwlagn_fw_error(priv, false);
1964}
1965
1966static void iwl_cmd_queue_full(struct iwl_op_mode *op_mode)
1967{
1968        struct iwl_priv *priv = IWL_OP_MODE_GET_DVM(op_mode);
1969
1970        if (!iwl_check_for_ct_kill(priv)) {
1971                IWL_ERR(priv, "Restarting adapter queue is full\n");
1972                iwlagn_fw_error(priv, false);
1973        }
1974}
1975
1976#define EEPROM_RF_CONFIG_TYPE_MAX      0x3
1977
1978static void iwl_nic_config(struct iwl_op_mode *op_mode)
1979{
1980        struct iwl_priv *priv = IWL_OP_MODE_GET_DVM(op_mode);
1981
1982        /* SKU Control */
1983        iwl_trans_set_bits_mask(priv->trans, CSR_HW_IF_CONFIG_REG,
1984                                CSR_HW_IF_CONFIG_REG_MSK_MAC_DASH |
1985                                CSR_HW_IF_CONFIG_REG_MSK_MAC_STEP,
1986                                (CSR_HW_REV_STEP(priv->trans->hw_rev) <<
1987                                        CSR_HW_IF_CONFIG_REG_POS_MAC_STEP) |
1988                                (CSR_HW_REV_DASH(priv->trans->hw_rev) <<
1989                                        CSR_HW_IF_CONFIG_REG_POS_MAC_DASH));
1990
1991        /* write radio config values to register */
1992        if (priv->nvm_data->radio_cfg_type <= EEPROM_RF_CONFIG_TYPE_MAX) {
1993                u32 reg_val =
1994                        priv->nvm_data->radio_cfg_type <<
1995                                CSR_HW_IF_CONFIG_REG_POS_PHY_TYPE |
1996                        priv->nvm_data->radio_cfg_step <<
1997                                CSR_HW_IF_CONFIG_REG_POS_PHY_STEP |
1998                        priv->nvm_data->radio_cfg_dash <<
1999                                CSR_HW_IF_CONFIG_REG_POS_PHY_DASH;
2000
2001                iwl_trans_set_bits_mask(priv->trans, CSR_HW_IF_CONFIG_REG,
2002                                        CSR_HW_IF_CONFIG_REG_MSK_PHY_TYPE |
2003                                        CSR_HW_IF_CONFIG_REG_MSK_PHY_STEP |
2004                                        CSR_HW_IF_CONFIG_REG_MSK_PHY_DASH,
2005                                        reg_val);
2006
2007                IWL_INFO(priv, "Radio type=0x%x-0x%x-0x%x\n",
2008                         priv->nvm_data->radio_cfg_type,
2009                         priv->nvm_data->radio_cfg_step,
2010                         priv->nvm_data->radio_cfg_dash);
2011        } else {
2012                WARN_ON(1);
2013        }
2014
2015        /* set CSR_HW_CONFIG_REG for uCode use */
2016        iwl_set_bit(priv->trans, CSR_HW_IF_CONFIG_REG,
2017                    CSR_HW_IF_CONFIG_REG_BIT_RADIO_SI |
2018                    CSR_HW_IF_CONFIG_REG_BIT_MAC_SI);
2019
2020        /* W/A : NIC is stuck in a reset state after Early PCIe power off
2021         * (PCIe power is lost before PERST# is asserted),
2022         * causing ME FW to lose ownership and not being able to obtain it back.
2023         */
2024        iwl_set_bits_mask_prph(priv->trans, APMG_PS_CTRL_REG,
2025                               APMG_PS_CTRL_EARLY_PWR_OFF_RESET_DIS,
2026                               ~APMG_PS_CTRL_EARLY_PWR_OFF_RESET_DIS);
2027
2028        if (priv->lib->nic_config)
2029                priv->lib->nic_config(priv);
2030}
2031
2032static void iwl_wimax_active(struct iwl_op_mode *op_mode)
2033{
2034        struct iwl_priv *priv = IWL_OP_MODE_GET_DVM(op_mode);
2035
2036        clear_bit(STATUS_READY, &priv->status);
2037        IWL_ERR(priv, "RF is used by WiMAX\n");
2038}
2039
2040static void iwl_stop_sw_queue(struct iwl_op_mode *op_mode, int queue)
2041{
2042        struct iwl_priv *priv = IWL_OP_MODE_GET_DVM(op_mode);
2043        int mq = priv->queue_to_mac80211[queue];
2044
2045        if (WARN_ON_ONCE(mq == IWL_INVALID_MAC80211_QUEUE))
2046                return;
2047
2048        if (atomic_inc_return(&priv->queue_stop_count[mq]) > 1) {
2049                IWL_DEBUG_TX_QUEUES(priv,
2050                        "queue %d (mac80211 %d) already stopped\n",
2051                        queue, mq);
2052                return;
2053        }
2054
2055        set_bit(mq, &priv->transport_queue_stop);
2056        ieee80211_stop_queue(priv->hw, mq);
2057}
2058
2059static void iwl_wake_sw_queue(struct iwl_op_mode *op_mode, int queue)
2060{
2061        struct iwl_priv *priv = IWL_OP_MODE_GET_DVM(op_mode);
2062        int mq = priv->queue_to_mac80211[queue];
2063
2064        if (WARN_ON_ONCE(mq == IWL_INVALID_MAC80211_QUEUE))
2065                return;
2066
2067        if (atomic_dec_return(&priv->queue_stop_count[mq]) > 0) {
2068                IWL_DEBUG_TX_QUEUES(priv,
2069                        "queue %d (mac80211 %d) already awake\n",
2070                        queue, mq);
2071                return;
2072        }
2073
2074        clear_bit(mq, &priv->transport_queue_stop);
2075
2076        if (!priv->passive_no_rx)
2077                ieee80211_wake_queue(priv->hw, mq);
2078}
2079
2080void iwlagn_lift_passive_no_rx(struct iwl_priv *priv)
2081{
2082        int mq;
2083
2084        if (!priv->passive_no_rx)
2085                return;
2086
2087        for (mq = 0; mq < IWLAGN_FIRST_AMPDU_QUEUE; mq++) {
2088                if (!test_bit(mq, &priv->transport_queue_stop)) {
2089                        IWL_DEBUG_TX_QUEUES(priv, "Wake queue %d\n", mq);
2090                        ieee80211_wake_queue(priv->hw, mq);
2091                } else {
2092                        IWL_DEBUG_TX_QUEUES(priv, "Don't wake queue %d\n", mq);
2093                }
2094        }
2095
2096        priv->passive_no_rx = false;
2097}
2098
2099static void iwl_free_skb(struct iwl_op_mode *op_mode, struct sk_buff *skb)
2100{
2101        struct iwl_priv *priv = IWL_OP_MODE_GET_DVM(op_mode);
2102        struct ieee80211_tx_info *info;
2103
2104        info = IEEE80211_SKB_CB(skb);
2105        iwl_trans_free_tx_cmd(priv->trans, info->driver_data[1]);
2106        ieee80211_free_txskb(priv->hw, skb);
2107}
2108
2109static bool iwl_set_hw_rfkill_state(struct iwl_op_mode *op_mode, bool state)
2110{
2111        struct iwl_priv *priv = IWL_OP_MODE_GET_DVM(op_mode);
2112
2113        if (state)
2114                set_bit(STATUS_RF_KILL_HW, &priv->status);
2115        else
2116                clear_bit(STATUS_RF_KILL_HW, &priv->status);
2117
2118        wiphy_rfkill_set_hw_state(priv->hw->wiphy, state);
2119
2120        return false;
2121}
2122
2123static const struct iwl_op_mode_ops iwl_dvm_ops = {
2124        .start = iwl_op_mode_dvm_start,
2125        .stop = iwl_op_mode_dvm_stop,
2126        .rx = iwl_rx_dispatch,
2127        .queue_full = iwl_stop_sw_queue,
2128        .queue_not_full = iwl_wake_sw_queue,
2129        .hw_rf_kill = iwl_set_hw_rfkill_state,
2130        .free_skb = iwl_free_skb,
2131        .nic_error = iwl_nic_error,
2132        .cmd_queue_full = iwl_cmd_queue_full,
2133        .nic_config = iwl_nic_config,
2134        .wimax_active = iwl_wimax_active,
2135};
2136
2137/*****************************************************************************
2138 *
2139 * driver and module entry point
2140 *
2141 *****************************************************************************/
2142static int __init iwl_init(void)
2143{
2144
2145        int ret;
2146
2147        ret = iwlagn_rate_control_register();
2148        if (ret) {
2149                pr_err("Unable to register rate control algorithm: %d\n", ret);
2150                return ret;
2151        }
2152
2153        ret = iwl_opmode_register("iwldvm", &iwl_dvm_ops);
2154        if (ret) {
2155                pr_err("Unable to register op_mode: %d\n", ret);
2156                iwlagn_rate_control_unregister();
2157        }
2158
2159        return ret;
2160}
2161module_init(iwl_init);
2162
2163static void __exit iwl_exit(void)
2164{
2165        iwl_opmode_deregister("iwldvm");
2166        iwlagn_rate_control_unregister();
2167}
2168module_exit(iwl_exit);
2169