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