Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /******************************************************************************
0003  *
0004  * Copyright(c) 2003 - 2014, 2018 - 2022  Intel Corporation. All rights reserved.
0005  * Copyright(c) 2015 Intel Deutschland GmbH
0006  *
0007  * Portions of this file are derived from the ipw3945 project, as well
0008  * as portions of the ieee80211 subsystem header files.
0009  *****************************************************************************/
0010 
0011 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0012 
0013 #include <linux/kernel.h>
0014 #include <linux/module.h>
0015 #include <linux/init.h>
0016 #include <linux/slab.h>
0017 #include <linux/delay.h>
0018 #include <linux/sched.h>
0019 #include <linux/skbuff.h>
0020 #include <linux/netdevice.h>
0021 #include <linux/etherdevice.h>
0022 #include <linux/if_arp.h>
0023 
0024 #include <net/mac80211.h>
0025 
0026 #include <asm/div64.h>
0027 
0028 #include "iwl-eeprom-read.h"
0029 #include "iwl-eeprom-parse.h"
0030 #include "iwl-io.h"
0031 #include "iwl-trans.h"
0032 #include "iwl-op-mode.h"
0033 #include "iwl-drv.h"
0034 #include "iwl-modparams.h"
0035 #include "iwl-prph.h"
0036 
0037 #include "dev.h"
0038 #include "calib.h"
0039 #include "agn.h"
0040 
0041 
0042 /******************************************************************************
0043  *
0044  * module boiler plate
0045  *
0046  ******************************************************************************/
0047 
0048 #define DRV_DESCRIPTION "Intel(R) Wireless WiFi Link AGN driver for Linux"
0049 MODULE_DESCRIPTION(DRV_DESCRIPTION);
0050 MODULE_LICENSE("GPL");
0051 MODULE_IMPORT_NS(IWLWIFI);
0052 
0053 /* Please keep this array *SORTED* by hex value.
0054  * Access is done through binary search.
0055  * A warning will be triggered on violation.
0056  */
0057 static const struct iwl_hcmd_names iwl_dvm_cmd_names[] = {
0058     HCMD_NAME(REPLY_ALIVE),
0059     HCMD_NAME(REPLY_ERROR),
0060     HCMD_NAME(REPLY_ECHO),
0061     HCMD_NAME(REPLY_RXON),
0062     HCMD_NAME(REPLY_RXON_ASSOC),
0063     HCMD_NAME(REPLY_QOS_PARAM),
0064     HCMD_NAME(REPLY_RXON_TIMING),
0065     HCMD_NAME(REPLY_ADD_STA),
0066     HCMD_NAME(REPLY_REMOVE_STA),
0067     HCMD_NAME(REPLY_REMOVE_ALL_STA),
0068     HCMD_NAME(REPLY_TX),
0069     HCMD_NAME(REPLY_TXFIFO_FLUSH),
0070     HCMD_NAME(REPLY_WEPKEY),
0071     HCMD_NAME(REPLY_LEDS_CMD),
0072     HCMD_NAME(REPLY_TX_LINK_QUALITY_CMD),
0073     HCMD_NAME(COEX_PRIORITY_TABLE_CMD),
0074     HCMD_NAME(COEX_MEDIUM_NOTIFICATION),
0075     HCMD_NAME(COEX_EVENT_CMD),
0076     HCMD_NAME(TEMPERATURE_NOTIFICATION),
0077     HCMD_NAME(CALIBRATION_CFG_CMD),
0078     HCMD_NAME(CALIBRATION_RES_NOTIFICATION),
0079     HCMD_NAME(CALIBRATION_COMPLETE_NOTIFICATION),
0080     HCMD_NAME(REPLY_QUIET_CMD),
0081     HCMD_NAME(REPLY_CHANNEL_SWITCH),
0082     HCMD_NAME(CHANNEL_SWITCH_NOTIFICATION),
0083     HCMD_NAME(REPLY_SPECTRUM_MEASUREMENT_CMD),
0084     HCMD_NAME(SPECTRUM_MEASURE_NOTIFICATION),
0085     HCMD_NAME(POWER_TABLE_CMD),
0086     HCMD_NAME(PM_SLEEP_NOTIFICATION),
0087     HCMD_NAME(PM_DEBUG_STATISTIC_NOTIFIC),
0088     HCMD_NAME(REPLY_SCAN_CMD),
0089     HCMD_NAME(REPLY_SCAN_ABORT_CMD),
0090     HCMD_NAME(SCAN_START_NOTIFICATION),
0091     HCMD_NAME(SCAN_RESULTS_NOTIFICATION),
0092     HCMD_NAME(SCAN_COMPLETE_NOTIFICATION),
0093     HCMD_NAME(BEACON_NOTIFICATION),
0094     HCMD_NAME(REPLY_TX_BEACON),
0095     HCMD_NAME(WHO_IS_AWAKE_NOTIFICATION),
0096     HCMD_NAME(REPLY_TX_POWER_DBM_CMD),
0097     HCMD_NAME(QUIET_NOTIFICATION),
0098     HCMD_NAME(REPLY_TX_PWR_TABLE_CMD),
0099     HCMD_NAME(REPLY_TX_POWER_DBM_CMD_V1),
0100     HCMD_NAME(TX_ANT_CONFIGURATION_CMD),
0101     HCMD_NAME(MEASURE_ABORT_NOTIFICATION),
0102     HCMD_NAME(REPLY_BT_CONFIG),
0103     HCMD_NAME(REPLY_STATISTICS_CMD),
0104     HCMD_NAME(STATISTICS_NOTIFICATION),
0105     HCMD_NAME(REPLY_CARD_STATE_CMD),
0106     HCMD_NAME(CARD_STATE_NOTIFICATION),
0107     HCMD_NAME(MISSED_BEACONS_NOTIFICATION),
0108     HCMD_NAME(REPLY_CT_KILL_CONFIG_CMD),
0109     HCMD_NAME(SENSITIVITY_CMD),
0110     HCMD_NAME(REPLY_PHY_CALIBRATION_CMD),
0111     HCMD_NAME(REPLY_WIPAN_PARAMS),
0112     HCMD_NAME(REPLY_WIPAN_RXON),
0113     HCMD_NAME(REPLY_WIPAN_RXON_TIMING),
0114     HCMD_NAME(REPLY_WIPAN_RXON_ASSOC),
0115     HCMD_NAME(REPLY_WIPAN_QOS_PARAM),
0116     HCMD_NAME(REPLY_WIPAN_WEPKEY),
0117     HCMD_NAME(REPLY_WIPAN_P2P_CHANNEL_SWITCH),
0118     HCMD_NAME(REPLY_WIPAN_NOA_NOTIFICATION),
0119     HCMD_NAME(REPLY_WIPAN_DEACTIVATION_COMPLETE),
0120     HCMD_NAME(REPLY_RX_PHY_CMD),
0121     HCMD_NAME(REPLY_RX_MPDU_CMD),
0122     HCMD_NAME(REPLY_RX),
0123     HCMD_NAME(REPLY_COMPRESSED_BA),
0124     HCMD_NAME(REPLY_BT_COEX_PRIO_TABLE),
0125     HCMD_NAME(REPLY_BT_COEX_PROT_ENV),
0126     HCMD_NAME(REPLY_BT_COEX_PROFILE_NOTIF),
0127     HCMD_NAME(REPLY_D3_CONFIG),
0128     HCMD_NAME(REPLY_WOWLAN_PATTERNS),
0129     HCMD_NAME(REPLY_WOWLAN_WAKEUP_FILTER),
0130     HCMD_NAME(REPLY_WOWLAN_TSC_RSC_PARAMS),
0131     HCMD_NAME(REPLY_WOWLAN_TKIP_PARAMS),
0132     HCMD_NAME(REPLY_WOWLAN_KEK_KCK_MATERIAL),
0133     HCMD_NAME(REPLY_WOWLAN_GET_STATUS),
0134 };
0135 
0136 static const struct iwl_hcmd_arr iwl_dvm_groups[] = {
0137     [0x0] = HCMD_ARR(iwl_dvm_cmd_names),
0138 };
0139 
0140 static const struct iwl_op_mode_ops iwl_dvm_ops;
0141 
0142 void iwl_update_chain_flags(struct iwl_priv *priv)
0143 {
0144     struct iwl_rxon_context *ctx;
0145 
0146     for_each_context(priv, ctx) {
0147         iwlagn_set_rxon_chain(priv, ctx);
0148         if (ctx->active.rx_chain != ctx->staging.rx_chain)
0149             iwlagn_commit_rxon(priv, ctx);
0150     }
0151 }
0152 
0153 /* Parse the beacon frame to find the TIM element and set tim_idx & tim_size */
0154 static void iwl_set_beacon_tim(struct iwl_priv *priv,
0155                    struct iwl_tx_beacon_cmd *tx_beacon_cmd,
0156                    u8 *beacon, u32 frame_size)
0157 {
0158     u16 tim_idx;
0159     struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)beacon;
0160 
0161     /*
0162      * The index is relative to frame start but we start looking at the
0163      * variable-length part of the beacon.
0164      */
0165     tim_idx = mgmt->u.beacon.variable - beacon;
0166 
0167     /* Parse variable-length elements of beacon to find WLAN_EID_TIM */
0168     while ((tim_idx < (frame_size - 2)) &&
0169             (beacon[tim_idx] != WLAN_EID_TIM))
0170         tim_idx += beacon[tim_idx+1] + 2;
0171 
0172     /* If TIM field was found, set variables */
0173     if ((tim_idx < (frame_size - 1)) && (beacon[tim_idx] == WLAN_EID_TIM)) {
0174         tx_beacon_cmd->tim_idx = cpu_to_le16(tim_idx);
0175         tx_beacon_cmd->tim_size = beacon[tim_idx+1];
0176     } else
0177         IWL_WARN(priv, "Unable to find TIM Element in beacon\n");
0178 }
0179 
0180 int iwlagn_send_beacon_cmd(struct iwl_priv *priv)
0181 {
0182     struct iwl_tx_beacon_cmd *tx_beacon_cmd;
0183     struct iwl_host_cmd cmd = {
0184         .id = REPLY_TX_BEACON,
0185     };
0186     struct ieee80211_tx_info *info;
0187     u32 frame_size;
0188     u32 rate_flags;
0189     u32 rate;
0190 
0191     /*
0192      * We have to set up the TX command, the TX Beacon command, and the
0193      * beacon contents.
0194      */
0195 
0196     lockdep_assert_held(&priv->mutex);
0197 
0198     if (!priv->beacon_ctx) {
0199         IWL_ERR(priv, "trying to build beacon w/o beacon context!\n");
0200         return 0;
0201     }
0202 
0203     if (WARN_ON(!priv->beacon_skb))
0204         return -EINVAL;
0205 
0206     /* Allocate beacon command */
0207     if (!priv->beacon_cmd)
0208         priv->beacon_cmd = kzalloc(sizeof(*tx_beacon_cmd), GFP_KERNEL);
0209     tx_beacon_cmd = priv->beacon_cmd;
0210     if (!tx_beacon_cmd)
0211         return -ENOMEM;
0212 
0213     frame_size = priv->beacon_skb->len;
0214 
0215     /* Set up TX command fields */
0216     tx_beacon_cmd->tx.len = cpu_to_le16((u16)frame_size);
0217     tx_beacon_cmd->tx.sta_id = priv->beacon_ctx->bcast_sta_id;
0218     tx_beacon_cmd->tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
0219     tx_beacon_cmd->tx.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK |
0220         TX_CMD_FLG_TSF_MSK | TX_CMD_FLG_STA_RATE_MSK;
0221 
0222     /* Set up TX beacon command fields */
0223     iwl_set_beacon_tim(priv, tx_beacon_cmd, priv->beacon_skb->data,
0224                frame_size);
0225 
0226     /* Set up packet rate and flags */
0227     info = IEEE80211_SKB_CB(priv->beacon_skb);
0228 
0229     /*
0230      * Let's set up the rate at least somewhat correctly;
0231      * it will currently not actually be used by the uCode,
0232      * it uses the broadcast station's rate instead.
0233      */
0234     if (info->control.rates[0].idx < 0 ||
0235         info->control.rates[0].flags & IEEE80211_TX_RC_MCS)
0236         rate = 0;
0237     else
0238         rate = info->control.rates[0].idx;
0239 
0240     priv->mgmt_tx_ant = iwl_toggle_tx_ant(priv, priv->mgmt_tx_ant,
0241                           priv->nvm_data->valid_tx_ant);
0242     rate_flags = iwl_ant_idx_to_flags(priv->mgmt_tx_ant);
0243 
0244     /* In mac80211, rates for 5 GHz start at 0 */
0245     if (info->band == NL80211_BAND_5GHZ)
0246         rate += IWL_FIRST_OFDM_RATE;
0247     else if (rate >= IWL_FIRST_CCK_RATE && rate <= IWL_LAST_CCK_RATE)
0248         rate_flags |= RATE_MCS_CCK_MSK;
0249 
0250     tx_beacon_cmd->tx.rate_n_flags =
0251             iwl_hw_set_rate_n_flags(rate, rate_flags);
0252 
0253     /* Submit command */
0254     cmd.len[0] = sizeof(*tx_beacon_cmd);
0255     cmd.data[0] = tx_beacon_cmd;
0256     cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
0257     cmd.len[1] = frame_size;
0258     cmd.data[1] = priv->beacon_skb->data;
0259     cmd.dataflags[1] = IWL_HCMD_DFL_NOCOPY;
0260 
0261     return iwl_dvm_send_cmd(priv, &cmd);
0262 }
0263 
0264 static void iwl_bg_beacon_update(struct work_struct *work)
0265 {
0266     struct iwl_priv *priv =
0267         container_of(work, struct iwl_priv, beacon_update);
0268     struct sk_buff *beacon;
0269 
0270     mutex_lock(&priv->mutex);
0271     if (!priv->beacon_ctx) {
0272         IWL_ERR(priv, "updating beacon w/o beacon context!\n");
0273         goto out;
0274     }
0275 
0276     if (priv->beacon_ctx->vif->type != NL80211_IFTYPE_AP) {
0277         /*
0278          * The ucode will send beacon notifications even in
0279          * IBSS mode, but we don't want to process them. But
0280          * we need to defer the type check to here due to
0281          * requiring locking around the beacon_ctx access.
0282          */
0283         goto out;
0284     }
0285 
0286     /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
0287     beacon = ieee80211_beacon_get(priv->hw, priv->beacon_ctx->vif, 0);
0288     if (!beacon) {
0289         IWL_ERR(priv, "update beacon failed -- keeping old\n");
0290         goto out;
0291     }
0292 
0293     /* new beacon skb is allocated every time; dispose previous.*/
0294     dev_kfree_skb(priv->beacon_skb);
0295 
0296     priv->beacon_skb = beacon;
0297 
0298     iwlagn_send_beacon_cmd(priv);
0299  out:
0300     mutex_unlock(&priv->mutex);
0301 }
0302 
0303 static void iwl_bg_bt_runtime_config(struct work_struct *work)
0304 {
0305     struct iwl_priv *priv =
0306         container_of(work, struct iwl_priv, bt_runtime_config);
0307 
0308     mutex_lock(&priv->mutex);
0309     if (test_bit(STATUS_EXIT_PENDING, &priv->status))
0310         goto out;
0311 
0312     /* dont send host command if rf-kill is on */
0313     if (!iwl_is_ready_rf(priv))
0314         goto out;
0315 
0316     iwlagn_send_advance_bt_config(priv);
0317 out:
0318     mutex_unlock(&priv->mutex);
0319 }
0320 
0321 static void iwl_bg_bt_full_concurrency(struct work_struct *work)
0322 {
0323     struct iwl_priv *priv =
0324         container_of(work, struct iwl_priv, bt_full_concurrency);
0325     struct iwl_rxon_context *ctx;
0326 
0327     mutex_lock(&priv->mutex);
0328 
0329     if (test_bit(STATUS_EXIT_PENDING, &priv->status))
0330         goto out;
0331 
0332     /* dont send host command if rf-kill is on */
0333     if (!iwl_is_ready_rf(priv))
0334         goto out;
0335 
0336     IWL_DEBUG_INFO(priv, "BT coex in %s mode\n",
0337                priv->bt_full_concurrent ?
0338                "full concurrency" : "3-wire");
0339 
0340     /*
0341      * LQ & RXON updated cmds must be sent before BT Config cmd
0342      * to avoid 3-wire collisions
0343      */
0344     for_each_context(priv, ctx) {
0345         iwlagn_set_rxon_chain(priv, ctx);
0346         iwlagn_commit_rxon(priv, ctx);
0347     }
0348 
0349     iwlagn_send_advance_bt_config(priv);
0350 out:
0351     mutex_unlock(&priv->mutex);
0352 }
0353 
0354 int iwl_send_statistics_request(struct iwl_priv *priv, u8 flags, bool clear)
0355 {
0356     struct iwl_statistics_cmd statistics_cmd = {
0357         .configuration_flags =
0358             clear ? IWL_STATS_CONF_CLEAR_STATS : 0,
0359     };
0360 
0361     if (flags & CMD_ASYNC)
0362         return iwl_dvm_send_cmd_pdu(priv, REPLY_STATISTICS_CMD,
0363                     CMD_ASYNC,
0364                     sizeof(struct iwl_statistics_cmd),
0365                     &statistics_cmd);
0366     else
0367         return iwl_dvm_send_cmd_pdu(priv, REPLY_STATISTICS_CMD, 0,
0368                     sizeof(struct iwl_statistics_cmd),
0369                     &statistics_cmd);
0370 }
0371 
0372 /*
0373  * iwl_bg_statistics_periodic - Timer callback to queue statistics
0374  *
0375  * This callback is provided in order to send a statistics request.
0376  *
0377  * This timer function is continually reset to execute within
0378  * REG_RECALIB_PERIOD seconds since the last STATISTICS_NOTIFICATION
0379  * was received.  We need to ensure we receive the statistics in order
0380  * to update the temperature used for calibrating the TXPOWER.
0381  */
0382 static void iwl_bg_statistics_periodic(struct timer_list *t)
0383 {
0384     struct iwl_priv *priv = from_timer(priv, t, statistics_periodic);
0385 
0386     if (test_bit(STATUS_EXIT_PENDING, &priv->status))
0387         return;
0388 
0389     /* dont send host command if rf-kill is on */
0390     if (!iwl_is_ready_rf(priv))
0391         return;
0392 
0393     iwl_send_statistics_request(priv, CMD_ASYNC, false);
0394 }
0395 
0396 
0397 static void iwl_print_cont_event_trace(struct iwl_priv *priv, u32 base,
0398                     u32 start_idx, u32 num_events,
0399                     u32 capacity, u32 mode)
0400 {
0401     u32 i;
0402     u32 ptr;        /* SRAM byte address of log data */
0403     u32 ev, time, data; /* event log data */
0404 
0405     if (mode == 0)
0406         ptr = base + (4 * sizeof(u32)) + (start_idx * 2 * sizeof(u32));
0407     else
0408         ptr = base + (4 * sizeof(u32)) + (start_idx * 3 * sizeof(u32));
0409 
0410     /* Make sure device is powered up for SRAM reads */
0411     if (!iwl_trans_grab_nic_access(priv->trans))
0412         return;
0413 
0414     /* Set starting address; reads will auto-increment */
0415     iwl_write32(priv->trans, HBUS_TARG_MEM_RADDR, ptr);
0416 
0417     /*
0418      * Refuse to read more than would have fit into the log from
0419      * the current start_idx. This used to happen due to the race
0420      * described below, but now WARN because the code below should
0421      * prevent it from happening here.
0422      */
0423     if (WARN_ON(num_events > capacity - start_idx))
0424         num_events = capacity - start_idx;
0425 
0426     /*
0427      * "time" is actually "data" for mode 0 (no timestamp).
0428      * place event id # at far right for easier visual parsing.
0429      */
0430     for (i = 0; i < num_events; i++) {
0431         ev = iwl_read32(priv->trans, HBUS_TARG_MEM_RDAT);
0432         time = iwl_read32(priv->trans, HBUS_TARG_MEM_RDAT);
0433         if (mode == 0) {
0434             trace_iwlwifi_dev_ucode_cont_event(
0435                     priv->trans->dev, 0, time, ev);
0436         } else {
0437             data = iwl_read32(priv->trans, HBUS_TARG_MEM_RDAT);
0438             trace_iwlwifi_dev_ucode_cont_event(
0439                     priv->trans->dev, time, data, ev);
0440         }
0441     }
0442     /* Allow device to power down */
0443     iwl_trans_release_nic_access(priv->trans);
0444 }
0445 
0446 static void iwl_continuous_event_trace(struct iwl_priv *priv)
0447 {
0448     u32 capacity;   /* event log capacity in # entries */
0449     struct {
0450         u32 capacity;
0451         u32 mode;
0452         u32 wrap_counter;
0453         u32 write_counter;
0454     } __packed read;
0455     u32 base;       /* SRAM byte address of event log header */
0456     u32 mode;       /* 0 - no timestamp, 1 - timestamp recorded */
0457     u32 num_wraps;  /* # times uCode wrapped to top of log */
0458     u32 next_entry; /* index of next entry to be written by uCode */
0459 
0460     base = priv->device_pointers.log_event_table;
0461     if (iwlagn_hw_valid_rtc_data_addr(base)) {
0462         iwl_trans_read_mem_bytes(priv->trans, base,
0463                      &read, sizeof(read));
0464         capacity = read.capacity;
0465         mode = read.mode;
0466         num_wraps = read.wrap_counter;
0467         next_entry = read.write_counter;
0468     } else
0469         return;
0470 
0471     /*
0472      * Unfortunately, the uCode doesn't use temporary variables.
0473      * Therefore, it can happen that we read next_entry == capacity,
0474      * which really means next_entry == 0.
0475      */
0476     if (unlikely(next_entry == capacity))
0477         next_entry = 0;
0478     /*
0479      * Additionally, the uCode increases the write pointer before
0480      * the wraps counter, so if the write pointer is smaller than
0481      * the old write pointer (wrap occurred) but we read that no
0482      * wrap occurred, we actually read between the next_entry and
0483      * num_wraps update (this does happen in practice!!) -- take
0484      * that into account by increasing num_wraps.
0485      */
0486     if (unlikely(next_entry < priv->event_log.next_entry &&
0487              num_wraps == priv->event_log.num_wraps))
0488         num_wraps++;
0489 
0490     if (num_wraps == priv->event_log.num_wraps) {
0491         iwl_print_cont_event_trace(
0492             priv, base, priv->event_log.next_entry,
0493             next_entry - priv->event_log.next_entry,
0494             capacity, mode);
0495 
0496         priv->event_log.non_wraps_count++;
0497     } else {
0498         if (num_wraps - priv->event_log.num_wraps > 1)
0499             priv->event_log.wraps_more_count++;
0500         else
0501             priv->event_log.wraps_once_count++;
0502 
0503         trace_iwlwifi_dev_ucode_wrap_event(priv->trans->dev,
0504                 num_wraps - priv->event_log.num_wraps,
0505                 next_entry, priv->event_log.next_entry);
0506 
0507         if (next_entry < priv->event_log.next_entry) {
0508             iwl_print_cont_event_trace(
0509                 priv, base, priv->event_log.next_entry,
0510                 capacity - priv->event_log.next_entry,
0511                 capacity, mode);
0512 
0513             iwl_print_cont_event_trace(
0514                 priv, base, 0, next_entry, capacity, mode);
0515         } else {
0516             iwl_print_cont_event_trace(
0517                 priv, base, next_entry,
0518                 capacity - next_entry,
0519                 capacity, mode);
0520 
0521             iwl_print_cont_event_trace(
0522                 priv, base, 0, next_entry, capacity, mode);
0523         }
0524     }
0525 
0526     priv->event_log.num_wraps = num_wraps;
0527     priv->event_log.next_entry = next_entry;
0528 }
0529 
0530 /*
0531  * iwl_bg_ucode_trace - Timer callback to log ucode event
0532  *
0533  * The timer is continually set to execute every
0534  * UCODE_TRACE_PERIOD milliseconds after the last timer expired
0535  * this function is to perform continuous uCode event logging operation
0536  * if enabled
0537  */
0538 static void iwl_bg_ucode_trace(struct timer_list *t)
0539 {
0540     struct iwl_priv *priv = from_timer(priv, t, ucode_trace);
0541 
0542     if (test_bit(STATUS_EXIT_PENDING, &priv->status))
0543         return;
0544 
0545     if (priv->event_log.ucode_trace) {
0546         iwl_continuous_event_trace(priv);
0547         /* Reschedule the timer to occur in UCODE_TRACE_PERIOD */
0548         mod_timer(&priv->ucode_trace,
0549              jiffies + msecs_to_jiffies(UCODE_TRACE_PERIOD));
0550     }
0551 }
0552 
0553 static void iwl_bg_tx_flush(struct work_struct *work)
0554 {
0555     struct iwl_priv *priv =
0556         container_of(work, struct iwl_priv, tx_flush);
0557 
0558     if (test_bit(STATUS_EXIT_PENDING, &priv->status))
0559         return;
0560 
0561     /* do nothing if rf-kill is on */
0562     if (!iwl_is_ready_rf(priv))
0563         return;
0564 
0565     IWL_DEBUG_INFO(priv, "device request: flush all tx frames\n");
0566     iwlagn_dev_txfifo_flush(priv);
0567 }
0568 
0569 /*
0570  * queue/FIFO/AC mapping definitions
0571  */
0572 
0573 static const u8 iwlagn_bss_ac_to_fifo[] = {
0574     IWL_TX_FIFO_VO,
0575     IWL_TX_FIFO_VI,
0576     IWL_TX_FIFO_BE,
0577     IWL_TX_FIFO_BK,
0578 };
0579 
0580 static const u8 iwlagn_bss_ac_to_queue[] = {
0581     0, 1, 2, 3,
0582 };
0583 
0584 static const u8 iwlagn_pan_ac_to_fifo[] = {
0585     IWL_TX_FIFO_VO_IPAN,
0586     IWL_TX_FIFO_VI_IPAN,
0587     IWL_TX_FIFO_BE_IPAN,
0588     IWL_TX_FIFO_BK_IPAN,
0589 };
0590 
0591 static const u8 iwlagn_pan_ac_to_queue[] = {
0592     7, 6, 5, 4,
0593 };
0594 
0595 static void iwl_init_context(struct iwl_priv *priv, u32 ucode_flags)
0596 {
0597     int i;
0598 
0599     /*
0600      * The default context is always valid,
0601      * the PAN context depends on uCode.
0602      */
0603     priv->valid_contexts = BIT(IWL_RXON_CTX_BSS);
0604     if (ucode_flags & IWL_UCODE_TLV_FLAGS_PAN)
0605         priv->valid_contexts |= BIT(IWL_RXON_CTX_PAN);
0606 
0607     for (i = 0; i < NUM_IWL_RXON_CTX; i++)
0608         priv->contexts[i].ctxid = i;
0609 
0610     priv->contexts[IWL_RXON_CTX_BSS].always_active = true;
0611     priv->contexts[IWL_RXON_CTX_BSS].is_active = true;
0612     priv->contexts[IWL_RXON_CTX_BSS].rxon_cmd = REPLY_RXON;
0613     priv->contexts[IWL_RXON_CTX_BSS].rxon_timing_cmd = REPLY_RXON_TIMING;
0614     priv->contexts[IWL_RXON_CTX_BSS].rxon_assoc_cmd = REPLY_RXON_ASSOC;
0615     priv->contexts[IWL_RXON_CTX_BSS].qos_cmd = REPLY_QOS_PARAM;
0616     priv->contexts[IWL_RXON_CTX_BSS].ap_sta_id = IWL_AP_ID;
0617     priv->contexts[IWL_RXON_CTX_BSS].wep_key_cmd = REPLY_WEPKEY;
0618     priv->contexts[IWL_RXON_CTX_BSS].bcast_sta_id = IWLAGN_BROADCAST_ID;
0619     priv->contexts[IWL_RXON_CTX_BSS].exclusive_interface_modes =
0620         BIT(NL80211_IFTYPE_ADHOC) | BIT(NL80211_IFTYPE_MONITOR);
0621     priv->contexts[IWL_RXON_CTX_BSS].interface_modes =
0622         BIT(NL80211_IFTYPE_STATION);
0623     priv->contexts[IWL_RXON_CTX_BSS].ap_devtype = RXON_DEV_TYPE_AP;
0624     priv->contexts[IWL_RXON_CTX_BSS].ibss_devtype = RXON_DEV_TYPE_IBSS;
0625     priv->contexts[IWL_RXON_CTX_BSS].station_devtype = RXON_DEV_TYPE_ESS;
0626     priv->contexts[IWL_RXON_CTX_BSS].unused_devtype = RXON_DEV_TYPE_ESS;
0627     memcpy(priv->contexts[IWL_RXON_CTX_BSS].ac_to_queue,
0628            iwlagn_bss_ac_to_queue, sizeof(iwlagn_bss_ac_to_queue));
0629     memcpy(priv->contexts[IWL_RXON_CTX_BSS].ac_to_fifo,
0630            iwlagn_bss_ac_to_fifo, sizeof(iwlagn_bss_ac_to_fifo));
0631 
0632     priv->contexts[IWL_RXON_CTX_PAN].rxon_cmd = REPLY_WIPAN_RXON;
0633     priv->contexts[IWL_RXON_CTX_PAN].rxon_timing_cmd =
0634         REPLY_WIPAN_RXON_TIMING;
0635     priv->contexts[IWL_RXON_CTX_PAN].rxon_assoc_cmd =
0636         REPLY_WIPAN_RXON_ASSOC;
0637     priv->contexts[IWL_RXON_CTX_PAN].qos_cmd = REPLY_WIPAN_QOS_PARAM;
0638     priv->contexts[IWL_RXON_CTX_PAN].ap_sta_id = IWL_AP_ID_PAN;
0639     priv->contexts[IWL_RXON_CTX_PAN].wep_key_cmd = REPLY_WIPAN_WEPKEY;
0640     priv->contexts[IWL_RXON_CTX_PAN].bcast_sta_id = IWLAGN_PAN_BCAST_ID;
0641     priv->contexts[IWL_RXON_CTX_PAN].station_flags = STA_FLG_PAN_STATION;
0642     priv->contexts[IWL_RXON_CTX_PAN].interface_modes =
0643         BIT(NL80211_IFTYPE_STATION) | BIT(NL80211_IFTYPE_AP);
0644 
0645     priv->contexts[IWL_RXON_CTX_PAN].ap_devtype = RXON_DEV_TYPE_CP;
0646     priv->contexts[IWL_RXON_CTX_PAN].station_devtype = RXON_DEV_TYPE_2STA;
0647     priv->contexts[IWL_RXON_CTX_PAN].unused_devtype = RXON_DEV_TYPE_P2P;
0648     memcpy(priv->contexts[IWL_RXON_CTX_PAN].ac_to_queue,
0649            iwlagn_pan_ac_to_queue, sizeof(iwlagn_pan_ac_to_queue));
0650     memcpy(priv->contexts[IWL_RXON_CTX_PAN].ac_to_fifo,
0651            iwlagn_pan_ac_to_fifo, sizeof(iwlagn_pan_ac_to_fifo));
0652     priv->contexts[IWL_RXON_CTX_PAN].mcast_queue = IWL_IPAN_MCAST_QUEUE;
0653 
0654     BUILD_BUG_ON(NUM_IWL_RXON_CTX != 2);
0655 }
0656 
0657 static void iwl_rf_kill_ct_config(struct iwl_priv *priv)
0658 {
0659     struct iwl_ct_kill_config cmd;
0660     struct iwl_ct_kill_throttling_config adv_cmd;
0661     int ret = 0;
0662 
0663     iwl_write32(priv->trans, CSR_UCODE_DRV_GP1_CLR,
0664             CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
0665 
0666     priv->thermal_throttle.ct_kill_toggle = false;
0667 
0668     if (priv->lib->support_ct_kill_exit) {
0669         adv_cmd.critical_temperature_enter =
0670             cpu_to_le32(priv->hw_params.ct_kill_threshold);
0671         adv_cmd.critical_temperature_exit =
0672             cpu_to_le32(priv->hw_params.ct_kill_exit_threshold);
0673 
0674         ret = iwl_dvm_send_cmd_pdu(priv,
0675                        REPLY_CT_KILL_CONFIG_CMD,
0676                        0, sizeof(adv_cmd), &adv_cmd);
0677         if (ret)
0678             IWL_ERR(priv, "REPLY_CT_KILL_CONFIG_CMD failed\n");
0679         else
0680             IWL_DEBUG_INFO(priv, "REPLY_CT_KILL_CONFIG_CMD "
0681                 "succeeded, critical temperature enter is %d,"
0682                 "exit is %d\n",
0683                 priv->hw_params.ct_kill_threshold,
0684                 priv->hw_params.ct_kill_exit_threshold);
0685     } else {
0686         cmd.critical_temperature_R =
0687             cpu_to_le32(priv->hw_params.ct_kill_threshold);
0688 
0689         ret = iwl_dvm_send_cmd_pdu(priv,
0690                        REPLY_CT_KILL_CONFIG_CMD,
0691                        0, sizeof(cmd), &cmd);
0692         if (ret)
0693             IWL_ERR(priv, "REPLY_CT_KILL_CONFIG_CMD failed\n");
0694         else
0695             IWL_DEBUG_INFO(priv, "REPLY_CT_KILL_CONFIG_CMD "
0696                 "succeeded, "
0697                 "critical temperature is %d\n",
0698                 priv->hw_params.ct_kill_threshold);
0699     }
0700 }
0701 
0702 static int iwlagn_send_calib_cfg_rt(struct iwl_priv *priv, u32 cfg)
0703 {
0704     struct iwl_calib_cfg_cmd calib_cfg_cmd;
0705     struct iwl_host_cmd cmd = {
0706         .id = CALIBRATION_CFG_CMD,
0707         .len = { sizeof(struct iwl_calib_cfg_cmd), },
0708         .data = { &calib_cfg_cmd, },
0709     };
0710 
0711     memset(&calib_cfg_cmd, 0, sizeof(calib_cfg_cmd));
0712     calib_cfg_cmd.ucd_calib_cfg.once.is_enable = IWL_CALIB_RT_CFG_ALL;
0713     calib_cfg_cmd.ucd_calib_cfg.once.start = cpu_to_le32(cfg);
0714 
0715     return iwl_dvm_send_cmd(priv, &cmd);
0716 }
0717 
0718 
0719 static int iwlagn_send_tx_ant_config(struct iwl_priv *priv, u8 valid_tx_ant)
0720 {
0721     struct iwl_tx_ant_config_cmd tx_ant_cmd = {
0722       .valid = cpu_to_le32(valid_tx_ant),
0723     };
0724 
0725     if (IWL_UCODE_API(priv->fw->ucode_ver) > 1) {
0726         IWL_DEBUG_HC(priv, "select valid tx ant: %u\n", valid_tx_ant);
0727         return iwl_dvm_send_cmd_pdu(priv, TX_ANT_CONFIGURATION_CMD, 0,
0728                     sizeof(struct iwl_tx_ant_config_cmd),
0729                     &tx_ant_cmd);
0730     } else {
0731         IWL_DEBUG_HC(priv, "TX_ANT_CONFIGURATION_CMD not supported\n");
0732         return -EOPNOTSUPP;
0733     }
0734 }
0735 
0736 static void iwl_send_bt_config(struct iwl_priv *priv)
0737 {
0738     struct iwl_bt_cmd bt_cmd = {
0739         .lead_time = BT_LEAD_TIME_DEF,
0740         .max_kill = BT_MAX_KILL_DEF,
0741         .kill_ack_mask = 0,
0742         .kill_cts_mask = 0,
0743     };
0744 
0745     if (!iwlwifi_mod_params.bt_coex_active)
0746         bt_cmd.flags = BT_COEX_DISABLE;
0747     else
0748         bt_cmd.flags = BT_COEX_ENABLE;
0749 
0750     priv->bt_enable_flag = bt_cmd.flags;
0751     IWL_DEBUG_INFO(priv, "BT coex %s\n",
0752         (bt_cmd.flags == BT_COEX_DISABLE) ? "disable" : "active");
0753 
0754     if (iwl_dvm_send_cmd_pdu(priv, REPLY_BT_CONFIG,
0755                  0, sizeof(struct iwl_bt_cmd), &bt_cmd))
0756         IWL_ERR(priv, "failed to send BT Coex Config\n");
0757 }
0758 
0759 /*
0760  * iwl_alive_start - called after REPLY_ALIVE notification received
0761  *                   from protocol/runtime uCode (initialization uCode's
0762  *                   Alive gets handled by iwl_init_alive_start()).
0763  */
0764 int iwl_alive_start(struct iwl_priv *priv)
0765 {
0766     int ret = 0;
0767     struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
0768 
0769     IWL_DEBUG_INFO(priv, "Runtime Alive received.\n");
0770 
0771     /* After the ALIVE response, we can send host commands to the uCode */
0772     set_bit(STATUS_ALIVE, &priv->status);
0773 
0774     if (iwl_is_rfkill(priv))
0775         return -ERFKILL;
0776 
0777     if (priv->event_log.ucode_trace) {
0778         /* start collecting data now */
0779         mod_timer(&priv->ucode_trace, jiffies);
0780     }
0781 
0782     /* download priority table before any calibration request */
0783     if (priv->lib->bt_params &&
0784         priv->lib->bt_params->advanced_bt_coexist) {
0785         /* Configure Bluetooth device coexistence support */
0786         if (priv->lib->bt_params->bt_sco_disable)
0787             priv->bt_enable_pspoll = false;
0788         else
0789             priv->bt_enable_pspoll = true;
0790 
0791         priv->bt_valid = IWLAGN_BT_ALL_VALID_MSK;
0792         priv->kill_ack_mask = IWLAGN_BT_KILL_ACK_MASK_DEFAULT;
0793         priv->kill_cts_mask = IWLAGN_BT_KILL_CTS_MASK_DEFAULT;
0794         iwlagn_send_advance_bt_config(priv);
0795         priv->bt_valid = IWLAGN_BT_VALID_ENABLE_FLAGS;
0796         priv->cur_rssi_ctx = NULL;
0797 
0798         iwl_send_prio_tbl(priv);
0799 
0800         /* FIXME: w/a to force change uCode BT state machine */
0801         ret = iwl_send_bt_env(priv, IWL_BT_COEX_ENV_OPEN,
0802                      BT_COEX_PRIO_TBL_EVT_INIT_CALIB2);
0803         if (ret)
0804             return ret;
0805         ret = iwl_send_bt_env(priv, IWL_BT_COEX_ENV_CLOSE,
0806                      BT_COEX_PRIO_TBL_EVT_INIT_CALIB2);
0807         if (ret)
0808             return ret;
0809     } else if (priv->lib->bt_params) {
0810         /*
0811          * default is 2-wire BT coexexistence support
0812          */
0813         iwl_send_bt_config(priv);
0814     }
0815 
0816     /*
0817      * Perform runtime calibrations, including DC calibration.
0818      */
0819     iwlagn_send_calib_cfg_rt(priv, IWL_CALIB_CFG_DC_IDX);
0820 
0821     ieee80211_wake_queues(priv->hw);
0822 
0823     /* Configure Tx antenna selection based on H/W config */
0824     iwlagn_send_tx_ant_config(priv, priv->nvm_data->valid_tx_ant);
0825 
0826     if (iwl_is_associated_ctx(ctx) && !priv->wowlan) {
0827         struct iwl_rxon_cmd *active_rxon =
0828                 (struct iwl_rxon_cmd *)&ctx->active;
0829         /* apply any changes in staging */
0830         ctx->staging.filter_flags |= RXON_FILTER_ASSOC_MSK;
0831         active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
0832     } else {
0833         struct iwl_rxon_context *tmp;
0834         /* Initialize our rx_config data */
0835         for_each_context(priv, tmp)
0836             iwl_connection_init_rx_config(priv, tmp);
0837 
0838         iwlagn_set_rxon_chain(priv, ctx);
0839     }
0840 
0841     if (!priv->wowlan) {
0842         /* WoWLAN ucode will not reply in the same way, skip it */
0843         iwl_reset_run_time_calib(priv);
0844     }
0845 
0846     set_bit(STATUS_READY, &priv->status);
0847 
0848     /* Configure the adapter for unassociated operation */
0849     ret = iwlagn_commit_rxon(priv, ctx);
0850     if (ret)
0851         return ret;
0852 
0853     /* At this point, the NIC is initialized and operational */
0854     iwl_rf_kill_ct_config(priv);
0855 
0856     IWL_DEBUG_INFO(priv, "ALIVE processing complete.\n");
0857 
0858     return iwl_power_update_mode(priv, true);
0859 }
0860 
0861 /**
0862  * iwl_clear_driver_stations - clear knowledge of all stations from driver
0863  * @priv: iwl priv struct
0864  *
0865  * This is called during iwl_down() to make sure that in the case
0866  * we're coming there from a hardware restart mac80211 will be
0867  * able to reconfigure stations -- if we're getting there in the
0868  * normal down flow then the stations will already be cleared.
0869  */
0870 static void iwl_clear_driver_stations(struct iwl_priv *priv)
0871 {
0872     struct iwl_rxon_context *ctx;
0873 
0874     spin_lock_bh(&priv->sta_lock);
0875     memset(priv->stations, 0, sizeof(priv->stations));
0876     priv->num_stations = 0;
0877 
0878     priv->ucode_key_table = 0;
0879 
0880     for_each_context(priv, ctx) {
0881         /*
0882          * Remove all key information that is not stored as part
0883          * of station information since mac80211 may not have had
0884          * a chance to remove all the keys. When device is
0885          * reconfigured by mac80211 after an error all keys will
0886          * be reconfigured.
0887          */
0888         memset(ctx->wep_keys, 0, sizeof(ctx->wep_keys));
0889         ctx->key_mapping_keys = 0;
0890     }
0891 
0892     spin_unlock_bh(&priv->sta_lock);
0893 }
0894 
0895 void iwl_down(struct iwl_priv *priv)
0896 {
0897     int exit_pending;
0898 
0899     IWL_DEBUG_INFO(priv, DRV_NAME " is going down\n");
0900 
0901     lockdep_assert_held(&priv->mutex);
0902 
0903     iwl_scan_cancel_timeout(priv, 200);
0904 
0905     exit_pending =
0906         test_and_set_bit(STATUS_EXIT_PENDING, &priv->status);
0907 
0908     iwl_clear_ucode_stations(priv, NULL);
0909     iwl_dealloc_bcast_stations(priv);
0910     iwl_clear_driver_stations(priv);
0911 
0912     /* reset BT coex data */
0913     priv->bt_status = 0;
0914     priv->cur_rssi_ctx = NULL;
0915     priv->bt_is_sco = 0;
0916     if (priv->lib->bt_params)
0917         priv->bt_traffic_load =
0918              priv->lib->bt_params->bt_init_traffic_load;
0919     else
0920         priv->bt_traffic_load = 0;
0921     priv->bt_full_concurrent = false;
0922     priv->bt_ci_compliance = 0;
0923 
0924     /* Wipe out the EXIT_PENDING status bit if we are not actually
0925      * exiting the module */
0926     if (!exit_pending)
0927         clear_bit(STATUS_EXIT_PENDING, &priv->status);
0928 
0929     if (priv->mac80211_registered)
0930         ieee80211_stop_queues(priv->hw);
0931 
0932     priv->ucode_loaded = false;
0933     iwl_trans_stop_device(priv->trans);
0934 
0935     /* Set num_aux_in_flight must be done after the transport is stopped */
0936     atomic_set(&priv->num_aux_in_flight, 0);
0937 
0938     /* Clear out all status bits but a few that are stable across reset */
0939     priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
0940                 STATUS_RF_KILL_HW |
0941             test_bit(STATUS_FW_ERROR, &priv->status) <<
0942                 STATUS_FW_ERROR |
0943             test_bit(STATUS_EXIT_PENDING, &priv->status) <<
0944                 STATUS_EXIT_PENDING;
0945 
0946     dev_kfree_skb(priv->beacon_skb);
0947     priv->beacon_skb = NULL;
0948 }
0949 
0950 /*****************************************************************************
0951  *
0952  * Workqueue callbacks
0953  *
0954  *****************************************************************************/
0955 
0956 static void iwl_bg_run_time_calib_work(struct work_struct *work)
0957 {
0958     struct iwl_priv *priv = container_of(work, struct iwl_priv,
0959             run_time_calib_work);
0960 
0961     mutex_lock(&priv->mutex);
0962 
0963     if (test_bit(STATUS_EXIT_PENDING, &priv->status) ||
0964         test_bit(STATUS_SCANNING, &priv->status)) {
0965         mutex_unlock(&priv->mutex);
0966         return;
0967     }
0968 
0969     if (priv->start_calib) {
0970         iwl_chain_noise_calibration(priv);
0971         iwl_sensitivity_calibration(priv);
0972     }
0973 
0974     mutex_unlock(&priv->mutex);
0975 }
0976 
0977 void iwlagn_prepare_restart(struct iwl_priv *priv)
0978 {
0979     bool bt_full_concurrent;
0980     u8 bt_ci_compliance;
0981     u8 bt_load;
0982     u8 bt_status;
0983     bool bt_is_sco;
0984     int i;
0985 
0986     lockdep_assert_held(&priv->mutex);
0987 
0988     priv->is_open = 0;
0989 
0990     /*
0991      * __iwl_down() will clear the BT status variables,
0992      * which is correct, but when we restart we really
0993      * want to keep them so restore them afterwards.
0994      *
0995      * The restart process will later pick them up and
0996      * re-configure the hw when we reconfigure the BT
0997      * command.
0998      */
0999     bt_full_concurrent = priv->bt_full_concurrent;
1000     bt_ci_compliance = priv->bt_ci_compliance;
1001     bt_load = priv->bt_traffic_load;
1002     bt_status = priv->bt_status;
1003     bt_is_sco = priv->bt_is_sco;
1004 
1005     iwl_down(priv);
1006 
1007     priv->bt_full_concurrent = bt_full_concurrent;
1008     priv->bt_ci_compliance = bt_ci_compliance;
1009     priv->bt_traffic_load = bt_load;
1010     priv->bt_status = bt_status;
1011     priv->bt_is_sco = bt_is_sco;
1012 
1013     /* reset aggregation queues */
1014     for (i = IWLAGN_FIRST_AMPDU_QUEUE; i < IWL_MAX_HW_QUEUES; i++)
1015         priv->queue_to_mac80211[i] = IWL_INVALID_MAC80211_QUEUE;
1016     /* and stop counts */
1017     for (i = 0; i < IWL_MAX_HW_QUEUES; i++)
1018         atomic_set(&priv->queue_stop_count[i], 0);
1019 
1020     memset(priv->agg_q_alloc, 0, sizeof(priv->agg_q_alloc));
1021 }
1022 
1023 static void iwl_bg_restart(struct work_struct *data)
1024 {
1025     struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
1026 
1027     if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1028         return;
1029 
1030     if (test_and_clear_bit(STATUS_FW_ERROR, &priv->status)) {
1031         mutex_lock(&priv->mutex);
1032         iwlagn_prepare_restart(priv);
1033         mutex_unlock(&priv->mutex);
1034         iwl_cancel_deferred_work(priv);
1035         if (priv->mac80211_registered)
1036             ieee80211_restart_hw(priv->hw);
1037         else
1038             IWL_ERR(priv,
1039                 "Cannot request restart before registering with mac80211\n");
1040     } else {
1041         WARN_ON(1);
1042     }
1043 }
1044 
1045 /*****************************************************************************
1046  *
1047  * driver setup and teardown
1048  *
1049  *****************************************************************************/
1050 
1051 static void iwl_setup_deferred_work(struct iwl_priv *priv)
1052 {
1053     priv->workqueue = alloc_ordered_workqueue(DRV_NAME, 0);
1054 
1055     INIT_WORK(&priv->restart, iwl_bg_restart);
1056     INIT_WORK(&priv->beacon_update, iwl_bg_beacon_update);
1057     INIT_WORK(&priv->run_time_calib_work, iwl_bg_run_time_calib_work);
1058     INIT_WORK(&priv->tx_flush, iwl_bg_tx_flush);
1059     INIT_WORK(&priv->bt_full_concurrency, iwl_bg_bt_full_concurrency);
1060     INIT_WORK(&priv->bt_runtime_config, iwl_bg_bt_runtime_config);
1061 
1062     iwl_setup_scan_deferred_work(priv);
1063 
1064     if (priv->lib->bt_params)
1065         iwlagn_bt_setup_deferred_work(priv);
1066 
1067     timer_setup(&priv->statistics_periodic, iwl_bg_statistics_periodic, 0);
1068 
1069     timer_setup(&priv->ucode_trace, iwl_bg_ucode_trace, 0);
1070 }
1071 
1072 void iwl_cancel_deferred_work(struct iwl_priv *priv)
1073 {
1074     if (priv->lib->bt_params)
1075         iwlagn_bt_cancel_deferred_work(priv);
1076 
1077     cancel_work_sync(&priv->run_time_calib_work);
1078     cancel_work_sync(&priv->beacon_update);
1079 
1080     iwl_cancel_scan_deferred_work(priv);
1081 
1082     cancel_work_sync(&priv->bt_full_concurrency);
1083     cancel_work_sync(&priv->bt_runtime_config);
1084 
1085     del_timer_sync(&priv->statistics_periodic);
1086     del_timer_sync(&priv->ucode_trace);
1087 }
1088 
1089 static int iwl_init_drv(struct iwl_priv *priv)
1090 {
1091     spin_lock_init(&priv->sta_lock);
1092 
1093     mutex_init(&priv->mutex);
1094 
1095     INIT_LIST_HEAD(&priv->calib_results);
1096 
1097     priv->band = NL80211_BAND_2GHZ;
1098 
1099     priv->plcp_delta_threshold = priv->lib->plcp_delta_threshold;
1100 
1101     priv->iw_mode = NL80211_IFTYPE_STATION;
1102     priv->current_ht_config.smps = IEEE80211_SMPS_STATIC;
1103     priv->missed_beacon_threshold = IWL_MISSED_BEACON_THRESHOLD_DEF;
1104     priv->agg_tids_count = 0;
1105 
1106     priv->rx_statistics_jiffies = jiffies;
1107 
1108     /* Choose which receivers/antennas to use */
1109     iwlagn_set_rxon_chain(priv, &priv->contexts[IWL_RXON_CTX_BSS]);
1110 
1111     iwl_init_scan_params(priv);
1112 
1113     /* init bt coex */
1114     if (priv->lib->bt_params &&
1115         priv->lib->bt_params->advanced_bt_coexist) {
1116         priv->kill_ack_mask = IWLAGN_BT_KILL_ACK_MASK_DEFAULT;
1117         priv->kill_cts_mask = IWLAGN_BT_KILL_CTS_MASK_DEFAULT;
1118         priv->bt_valid = IWLAGN_BT_ALL_VALID_MSK;
1119         priv->bt_on_thresh = BT_ON_THRESHOLD_DEF;
1120         priv->bt_duration = BT_DURATION_LIMIT_DEF;
1121         priv->dynamic_frag_thresh = BT_FRAG_THRESHOLD_DEF;
1122     }
1123 
1124     return 0;
1125 }
1126 
1127 static void iwl_uninit_drv(struct iwl_priv *priv)
1128 {
1129     kfree(priv->scan_cmd);
1130     kfree(priv->beacon_cmd);
1131     kfree(rcu_dereference_raw(priv->noa_data));
1132     iwl_calib_free_results(priv);
1133 #ifdef CONFIG_IWLWIFI_DEBUGFS
1134     kfree(priv->wowlan_sram);
1135 #endif
1136 }
1137 
1138 static void iwl_set_hw_params(struct iwl_priv *priv)
1139 {
1140     if (priv->cfg->ht_params)
1141         priv->hw_params.use_rts_for_aggregation =
1142             priv->cfg->ht_params->use_rts_for_aggregation;
1143 
1144     /* Device-specific setup */
1145     priv->lib->set_hw_params(priv);
1146 }
1147 
1148 
1149 
1150 /* show what optional capabilities we have */
1151 static void iwl_option_config(struct iwl_priv *priv)
1152 {
1153 #ifdef CONFIG_IWLWIFI_DEBUG
1154     IWL_INFO(priv, "CONFIG_IWLWIFI_DEBUG enabled\n");
1155 #else
1156     IWL_INFO(priv, "CONFIG_IWLWIFI_DEBUG disabled\n");
1157 #endif
1158 
1159 #ifdef CONFIG_IWLWIFI_DEBUGFS
1160     IWL_INFO(priv, "CONFIG_IWLWIFI_DEBUGFS enabled\n");
1161 #else
1162     IWL_INFO(priv, "CONFIG_IWLWIFI_DEBUGFS disabled\n");
1163 #endif
1164 
1165 #ifdef CONFIG_IWLWIFI_DEVICE_TRACING
1166     IWL_INFO(priv, "CONFIG_IWLWIFI_DEVICE_TRACING enabled\n");
1167 #else
1168     IWL_INFO(priv, "CONFIG_IWLWIFI_DEVICE_TRACING disabled\n");
1169 #endif
1170 }
1171 
1172 static int iwl_eeprom_init_hw_params(struct iwl_priv *priv)
1173 {
1174     struct iwl_nvm_data *data = priv->nvm_data;
1175 
1176     if (data->sku_cap_11n_enable &&
1177         !priv->cfg->ht_params) {
1178         IWL_ERR(priv, "Invalid 11n configuration\n");
1179         return -EINVAL;
1180     }
1181 
1182     if (!data->sku_cap_11n_enable && !data->sku_cap_band_24ghz_enable &&
1183         !data->sku_cap_band_52ghz_enable) {
1184         IWL_ERR(priv, "Invalid device sku\n");
1185         return -EINVAL;
1186     }
1187 
1188     IWL_DEBUG_INFO(priv,
1189                "Device SKU: 24GHz %s %s, 52GHz %s %s, 11.n %s %s\n",
1190                data->sku_cap_band_24ghz_enable ? "" : "NOT", "enabled",
1191                data->sku_cap_band_52ghz_enable ? "" : "NOT", "enabled",
1192                data->sku_cap_11n_enable ? "" : "NOT", "enabled");
1193 
1194     priv->hw_params.tx_chains_num =
1195         num_of_ant(data->valid_tx_ant);
1196     if (priv->cfg->rx_with_siso_diversity)
1197         priv->hw_params.rx_chains_num = 1;
1198     else
1199         priv->hw_params.rx_chains_num =
1200             num_of_ant(data->valid_rx_ant);
1201 
1202     IWL_DEBUG_INFO(priv, "Valid Tx ant: 0x%X, Valid Rx ant: 0x%X\n",
1203                data->valid_tx_ant,
1204                data->valid_rx_ant);
1205 
1206     return 0;
1207 }
1208 
1209 static int iwl_nvm_check_version(struct iwl_nvm_data *data,
1210                  struct iwl_trans *trans)
1211 {
1212     if (data->nvm_version >= trans->cfg->nvm_ver ||
1213         data->calib_version >= trans->cfg->nvm_calib_ver) {
1214         IWL_DEBUG_INFO(trans, "device EEPROM VER=0x%x, CALIB=0x%x\n",
1215                    data->nvm_version, data->calib_version);
1216         return 0;
1217     }
1218 
1219     IWL_ERR(trans,
1220         "Unsupported (too old) EEPROM VER=0x%x < 0x%x CALIB=0x%x < 0x%x\n",
1221         data->nvm_version, trans->cfg->nvm_ver,
1222         data->calib_version,  trans->cfg->nvm_calib_ver);
1223     return -EINVAL;
1224 }
1225 
1226 static struct iwl_op_mode *iwl_op_mode_dvm_start(struct iwl_trans *trans,
1227                          const struct iwl_cfg *cfg,
1228                          const struct iwl_fw *fw,
1229                          struct dentry *dbgfs_dir)
1230 {
1231     struct iwl_priv *priv;
1232     struct ieee80211_hw *hw;
1233     struct iwl_op_mode *op_mode;
1234     u16 num_mac;
1235     u32 ucode_flags;
1236     struct iwl_trans_config trans_cfg = {};
1237     static const u8 no_reclaim_cmds[] = {
1238         REPLY_RX_PHY_CMD,
1239         REPLY_RX_MPDU_CMD,
1240         REPLY_COMPRESSED_BA,
1241         STATISTICS_NOTIFICATION,
1242         REPLY_TX,
1243     };
1244     int i;
1245 
1246     /************************
1247      * 1. Allocating HW data
1248      ************************/
1249     hw = iwl_alloc_all();
1250     if (!hw) {
1251         pr_err("%s: Cannot allocate network device\n", trans->name);
1252         goto out;
1253     }
1254 
1255     op_mode = hw->priv;
1256     op_mode->ops = &iwl_dvm_ops;
1257     priv = IWL_OP_MODE_GET_DVM(op_mode);
1258     priv->trans = trans;
1259     priv->dev = trans->dev;
1260     priv->cfg = cfg;
1261     priv->fw = fw;
1262 
1263     switch (priv->trans->trans_cfg->device_family) {
1264     case IWL_DEVICE_FAMILY_1000:
1265     case IWL_DEVICE_FAMILY_100:
1266         priv->lib = &iwl_dvm_1000_cfg;
1267         break;
1268     case IWL_DEVICE_FAMILY_2000:
1269         priv->lib = &iwl_dvm_2000_cfg;
1270         break;
1271     case IWL_DEVICE_FAMILY_105:
1272         priv->lib = &iwl_dvm_105_cfg;
1273         break;
1274     case IWL_DEVICE_FAMILY_2030:
1275     case IWL_DEVICE_FAMILY_135:
1276         priv->lib = &iwl_dvm_2030_cfg;
1277         break;
1278     case IWL_DEVICE_FAMILY_5000:
1279         priv->lib = &iwl_dvm_5000_cfg;
1280         break;
1281     case IWL_DEVICE_FAMILY_5150:
1282         priv->lib = &iwl_dvm_5150_cfg;
1283         break;
1284     case IWL_DEVICE_FAMILY_6000:
1285     case IWL_DEVICE_FAMILY_6000i:
1286         priv->lib = &iwl_dvm_6000_cfg;
1287         break;
1288     case IWL_DEVICE_FAMILY_6005:
1289         priv->lib = &iwl_dvm_6005_cfg;
1290         break;
1291     case IWL_DEVICE_FAMILY_6050:
1292     case IWL_DEVICE_FAMILY_6150:
1293         priv->lib = &iwl_dvm_6050_cfg;
1294         break;
1295     case IWL_DEVICE_FAMILY_6030:
1296         priv->lib = &iwl_dvm_6030_cfg;
1297         break;
1298     default:
1299         break;
1300     }
1301 
1302     if (WARN_ON(!priv->lib))
1303         goto out_free_hw;
1304 
1305     /*
1306      * Populate the state variables that the transport layer needs
1307      * to know about.
1308      */
1309     trans_cfg.op_mode = op_mode;
1310     trans_cfg.no_reclaim_cmds = no_reclaim_cmds;
1311     trans_cfg.n_no_reclaim_cmds = ARRAY_SIZE(no_reclaim_cmds);
1312 
1313     switch (iwlwifi_mod_params.amsdu_size) {
1314     case IWL_AMSDU_DEF:
1315     case IWL_AMSDU_4K:
1316         trans_cfg.rx_buf_size = IWL_AMSDU_4K;
1317         break;
1318     case IWL_AMSDU_8K:
1319         trans_cfg.rx_buf_size = IWL_AMSDU_8K;
1320         break;
1321     case IWL_AMSDU_12K:
1322     default:
1323         trans_cfg.rx_buf_size = IWL_AMSDU_4K;
1324         pr_err("Unsupported amsdu_size: %d\n",
1325                iwlwifi_mod_params.amsdu_size);
1326     }
1327 
1328     trans_cfg.cmd_q_wdg_timeout = IWL_WATCHDOG_DISABLED;
1329 
1330     trans_cfg.command_groups = iwl_dvm_groups;
1331     trans_cfg.command_groups_size = ARRAY_SIZE(iwl_dvm_groups);
1332 
1333     trans_cfg.cmd_fifo = IWLAGN_CMD_FIFO_NUM;
1334     trans_cfg.cb_data_offs = offsetof(struct ieee80211_tx_info,
1335                       driver_data[2]);
1336 
1337     WARN_ON(sizeof(priv->transport_queue_stop) * BITS_PER_BYTE <
1338         priv->trans->trans_cfg->base_params->num_of_queues);
1339 
1340     ucode_flags = fw->ucode_capa.flags;
1341 
1342     if (ucode_flags & IWL_UCODE_TLV_FLAGS_PAN) {
1343         priv->sta_key_max_num = STA_KEY_MAX_NUM_PAN;
1344         trans_cfg.cmd_queue = IWL_IPAN_CMD_QUEUE_NUM;
1345     } else {
1346         priv->sta_key_max_num = STA_KEY_MAX_NUM;
1347         trans_cfg.cmd_queue = IWL_DEFAULT_CMD_QUEUE_NUM;
1348     }
1349 
1350     /* Configure transport layer */
1351     iwl_trans_configure(priv->trans, &trans_cfg);
1352 
1353     trans->rx_mpdu_cmd = REPLY_RX_MPDU_CMD;
1354     trans->rx_mpdu_cmd_hdr_size = sizeof(struct iwl_rx_mpdu_res_start);
1355     trans->command_groups = trans_cfg.command_groups;
1356     trans->command_groups_size = trans_cfg.command_groups_size;
1357 
1358     /* At this point both hw and priv are allocated. */
1359 
1360     SET_IEEE80211_DEV(priv->hw, priv->trans->dev);
1361 
1362     iwl_option_config(priv);
1363 
1364     IWL_DEBUG_INFO(priv, "*** LOAD DRIVER ***\n");
1365 
1366     /* bt channel inhibition enabled*/
1367     priv->bt_ch_announce = true;
1368     IWL_DEBUG_INFO(priv, "BT channel inhibition is %s\n",
1369                (priv->bt_ch_announce) ? "On" : "Off");
1370 
1371     /* these spin locks will be used in apm_ops.init and EEPROM access
1372      * we should init now
1373      */
1374     spin_lock_init(&priv->statistics.lock);
1375 
1376     /***********************
1377      * 2. Read REV register
1378      ***********************/
1379     IWL_INFO(priv, "Detected %s, REV=0x%X\n",
1380         priv->trans->name, priv->trans->hw_rev);
1381 
1382     if (iwl_trans_start_hw(priv->trans))
1383         goto out_free_hw;
1384 
1385     /* Read the EEPROM */
1386     if (iwl_read_eeprom(priv->trans, &priv->eeprom_blob,
1387                 &priv->eeprom_blob_size)) {
1388         IWL_ERR(priv, "Unable to init EEPROM\n");
1389         goto out_free_hw;
1390     }
1391 
1392     /* Reset chip to save power until we load uCode during "up". */
1393     iwl_trans_stop_device(priv->trans);
1394 
1395     priv->nvm_data = iwl_parse_eeprom_data(priv->trans, priv->cfg,
1396                            priv->eeprom_blob,
1397                            priv->eeprom_blob_size);
1398     if (!priv->nvm_data)
1399         goto out_free_eeprom_blob;
1400 
1401     if (iwl_nvm_check_version(priv->nvm_data, priv->trans))
1402         goto out_free_eeprom;
1403 
1404     if (iwl_eeprom_init_hw_params(priv))
1405         goto out_free_eeprom;
1406 
1407     /* extract MAC Address */
1408     memcpy(priv->addresses[0].addr, priv->nvm_data->hw_addr, ETH_ALEN);
1409     IWL_DEBUG_INFO(priv, "MAC address: %pM\n", priv->addresses[0].addr);
1410     priv->hw->wiphy->addresses = priv->addresses;
1411     priv->hw->wiphy->n_addresses = 1;
1412     num_mac = priv->nvm_data->n_hw_addrs;
1413     if (num_mac > 1) {
1414         memcpy(priv->addresses[1].addr, priv->addresses[0].addr,
1415                ETH_ALEN);
1416         priv->addresses[1].addr[5]++;
1417         priv->hw->wiphy->n_addresses++;
1418     }
1419 
1420     /************************
1421      * 4. Setup HW constants
1422      ************************/
1423     iwl_set_hw_params(priv);
1424 
1425     if (!(priv->nvm_data->sku_cap_ipan_enable)) {
1426         IWL_DEBUG_INFO(priv, "Your EEPROM disabled PAN\n");
1427         ucode_flags &= ~IWL_UCODE_TLV_FLAGS_PAN;
1428         /*
1429          * if not PAN, then don't support P2P -- might be a uCode
1430          * packaging bug or due to the eeprom check above
1431          */
1432         priv->sta_key_max_num = STA_KEY_MAX_NUM;
1433         trans_cfg.cmd_queue = IWL_DEFAULT_CMD_QUEUE_NUM;
1434 
1435         /* Configure transport layer again*/
1436         iwl_trans_configure(priv->trans, &trans_cfg);
1437     }
1438 
1439     /*******************
1440      * 5. Setup priv
1441      *******************/
1442     for (i = 0; i < IWL_MAX_HW_QUEUES; i++) {
1443         priv->queue_to_mac80211[i] = IWL_INVALID_MAC80211_QUEUE;
1444         if (i < IWLAGN_FIRST_AMPDU_QUEUE &&
1445             i != IWL_DEFAULT_CMD_QUEUE_NUM &&
1446             i != IWL_IPAN_CMD_QUEUE_NUM)
1447             priv->queue_to_mac80211[i] = i;
1448         atomic_set(&priv->queue_stop_count[i], 0);
1449     }
1450 
1451     if (iwl_init_drv(priv))
1452         goto out_free_eeprom;
1453 
1454     /* At this point both hw and priv are initialized. */
1455 
1456     /********************
1457      * 6. Setup services
1458      ********************/
1459     iwl_setup_deferred_work(priv);
1460     iwl_setup_rx_handlers(priv);
1461 
1462     iwl_power_initialize(priv);
1463     iwl_tt_initialize(priv);
1464 
1465     snprintf(priv->hw->wiphy->fw_version,
1466          sizeof(priv->hw->wiphy->fw_version),
1467          "%s", fw->fw_version);
1468 
1469     priv->new_scan_threshold_behaviour =
1470         !!(ucode_flags & IWL_UCODE_TLV_FLAGS_NEWSCAN);
1471 
1472     priv->phy_calib_chain_noise_reset_cmd =
1473         fw->ucode_capa.standard_phy_calibration_size;
1474     priv->phy_calib_chain_noise_gain_cmd =
1475         fw->ucode_capa.standard_phy_calibration_size + 1;
1476 
1477     /* initialize all valid contexts */
1478     iwl_init_context(priv, ucode_flags);
1479 
1480     /**************************************************
1481      * This is still part of probe() in a sense...
1482      *
1483      * 7. Setup and register with mac80211 and debugfs
1484      **************************************************/
1485     if (iwlagn_mac_setup_register(priv, &fw->ucode_capa))
1486         goto out_destroy_workqueue;
1487 
1488     iwl_dbgfs_register(priv, dbgfs_dir);
1489 
1490     return op_mode;
1491 
1492 out_destroy_workqueue:
1493     iwl_tt_exit(priv);
1494     iwl_cancel_deferred_work(priv);
1495     destroy_workqueue(priv->workqueue);
1496     priv->workqueue = NULL;
1497     iwl_uninit_drv(priv);
1498 out_free_eeprom_blob:
1499     kfree(priv->eeprom_blob);
1500 out_free_eeprom:
1501     kfree(priv->nvm_data);
1502 out_free_hw:
1503     ieee80211_free_hw(priv->hw);
1504 out:
1505     op_mode = NULL;
1506     return op_mode;
1507 }
1508 
1509 static void iwl_op_mode_dvm_stop(struct iwl_op_mode *op_mode)
1510 {
1511     struct iwl_priv *priv = IWL_OP_MODE_GET_DVM(op_mode);
1512 
1513     IWL_DEBUG_INFO(priv, "*** UNLOAD DRIVER ***\n");
1514 
1515     iwlagn_mac_unregister(priv);
1516 
1517     iwl_tt_exit(priv);
1518 
1519     kfree(priv->eeprom_blob);
1520     kfree(priv->nvm_data);
1521 
1522     /*netif_stop_queue(dev); */
1523 
1524     /* ieee80211_unregister_hw calls iwlagn_mac_stop, which flushes
1525      * priv->workqueue... so we can't take down the workqueue
1526      * until now... */
1527     destroy_workqueue(priv->workqueue);
1528     priv->workqueue = NULL;
1529 
1530     iwl_uninit_drv(priv);
1531 
1532     dev_kfree_skb(priv->beacon_skb);
1533 
1534     iwl_trans_op_mode_leave(priv->trans);
1535     ieee80211_free_hw(priv->hw);
1536 }
1537 
1538 static const char * const desc_lookup_text[] = {
1539     "OK",
1540     "FAIL",
1541     "BAD_PARAM",
1542     "BAD_CHECKSUM",
1543     "NMI_INTERRUPT_WDG",
1544     "SYSASSERT",
1545     "FATAL_ERROR",
1546     "BAD_COMMAND",
1547     "HW_ERROR_TUNE_LOCK",
1548     "HW_ERROR_TEMPERATURE",
1549     "ILLEGAL_CHAN_FREQ",
1550     "VCC_NOT_STABLE",
1551     "FH_ERROR",
1552     "NMI_INTERRUPT_HOST",
1553     "NMI_INTERRUPT_ACTION_PT",
1554     "NMI_INTERRUPT_UNKNOWN",
1555     "UCODE_VERSION_MISMATCH",
1556     "HW_ERROR_ABS_LOCK",
1557     "HW_ERROR_CAL_LOCK_FAIL",
1558     "NMI_INTERRUPT_INST_ACTION_PT",
1559     "NMI_INTERRUPT_DATA_ACTION_PT",
1560     "NMI_TRM_HW_ER",
1561     "NMI_INTERRUPT_TRM",
1562     "NMI_INTERRUPT_BREAK_POINT",
1563     "DEBUG_0",
1564     "DEBUG_1",
1565     "DEBUG_2",
1566     "DEBUG_3",
1567 };
1568 
1569 static struct { char *name; u8 num; } advanced_lookup[] = {
1570     { "NMI_INTERRUPT_WDG", 0x34 },
1571     { "SYSASSERT", 0x35 },
1572     { "UCODE_VERSION_MISMATCH", 0x37 },
1573     { "BAD_COMMAND", 0x38 },
1574     { "NMI_INTERRUPT_DATA_ACTION_PT", 0x3C },
1575     { "FATAL_ERROR", 0x3D },
1576     { "NMI_TRM_HW_ERR", 0x46 },
1577     { "NMI_INTERRUPT_TRM", 0x4C },
1578     { "NMI_INTERRUPT_BREAK_POINT", 0x54 },
1579     { "NMI_INTERRUPT_WDG_RXF_FULL", 0x5C },
1580     { "NMI_INTERRUPT_WDG_NO_RBD_RXF_FULL", 0x64 },
1581     { "NMI_INTERRUPT_HOST", 0x66 },
1582     { "NMI_INTERRUPT_ACTION_PT", 0x7C },
1583     { "NMI_INTERRUPT_UNKNOWN", 0x84 },
1584     { "NMI_INTERRUPT_INST_ACTION_PT", 0x86 },
1585     { "ADVANCED_SYSASSERT", 0 },
1586 };
1587 
1588 static const char *desc_lookup(u32 num)
1589 {
1590     int i;
1591     int max = ARRAY_SIZE(desc_lookup_text);
1592 
1593     if (num < max)
1594         return desc_lookup_text[num];
1595 
1596     max = ARRAY_SIZE(advanced_lookup) - 1;
1597     for (i = 0; i < max; i++) {
1598         if (advanced_lookup[i].num == num)
1599             break;
1600     }
1601     return advanced_lookup[i].name;
1602 }
1603 
1604 #define ERROR_START_OFFSET  (1 * sizeof(u32))
1605 #define ERROR_ELEM_SIZE     (7 * sizeof(u32))
1606 
1607 static void iwl_dump_nic_error_log(struct iwl_priv *priv)
1608 {
1609     struct iwl_trans *trans = priv->trans;
1610     u32 base;
1611     struct iwl_error_event_table table;
1612 
1613     base = priv->device_pointers.error_event_table;
1614     if (priv->cur_ucode == IWL_UCODE_INIT) {
1615         if (!base)
1616             base = priv->fw->init_errlog_ptr;
1617     } else {
1618         if (!base)
1619             base = priv->fw->inst_errlog_ptr;
1620     }
1621 
1622     if (!iwlagn_hw_valid_rtc_data_addr(base)) {
1623         IWL_ERR(priv,
1624             "Not valid error log pointer 0x%08X for %s uCode\n",
1625             base,
1626             (priv->cur_ucode == IWL_UCODE_INIT)
1627                     ? "Init" : "RT");
1628         return;
1629     }
1630 
1631     /*TODO: Update dbgfs with ISR error stats obtained below */
1632     iwl_trans_read_mem_bytes(trans, base, &table, sizeof(table));
1633 
1634     if (ERROR_START_OFFSET <= table.valid * ERROR_ELEM_SIZE) {
1635         IWL_ERR(trans, "Start IWL Error Log Dump:\n");
1636         IWL_ERR(trans, "Status: 0x%08lX, count: %d\n",
1637             priv->status, table.valid);
1638     }
1639 
1640     IWL_ERR(priv, "0x%08X | %-28s\n", table.error_id,
1641         desc_lookup(table.error_id));
1642     IWL_ERR(priv, "0x%08X | uPc\n", table.pc);
1643     IWL_ERR(priv, "0x%08X | branchlink1\n", table.blink1);
1644     IWL_ERR(priv, "0x%08X | branchlink2\n", table.blink2);
1645     IWL_ERR(priv, "0x%08X | interruptlink1\n", table.ilink1);
1646     IWL_ERR(priv, "0x%08X | interruptlink2\n", table.ilink2);
1647     IWL_ERR(priv, "0x%08X | data1\n", table.data1);
1648     IWL_ERR(priv, "0x%08X | data2\n", table.data2);
1649     IWL_ERR(priv, "0x%08X | line\n", table.line);
1650     IWL_ERR(priv, "0x%08X | beacon time\n", table.bcon_time);
1651     IWL_ERR(priv, "0x%08X | tsf low\n", table.tsf_low);
1652     IWL_ERR(priv, "0x%08X | tsf hi\n", table.tsf_hi);
1653     IWL_ERR(priv, "0x%08X | time gp1\n", table.gp1);
1654     IWL_ERR(priv, "0x%08X | time gp2\n", table.gp2);
1655     IWL_ERR(priv, "0x%08X | time gp3\n", table.gp3);
1656     IWL_ERR(priv, "0x%08X | uCode version\n", table.ucode_ver);
1657     IWL_ERR(priv, "0x%08X | hw version\n", table.hw_ver);
1658     IWL_ERR(priv, "0x%08X | board version\n", table.brd_ver);
1659     IWL_ERR(priv, "0x%08X | hcmd\n", table.hcmd);
1660     IWL_ERR(priv, "0x%08X | isr0\n", table.isr0);
1661     IWL_ERR(priv, "0x%08X | isr1\n", table.isr1);
1662     IWL_ERR(priv, "0x%08X | isr2\n", table.isr2);
1663     IWL_ERR(priv, "0x%08X | isr3\n", table.isr3);
1664     IWL_ERR(priv, "0x%08X | isr4\n", table.isr4);
1665     IWL_ERR(priv, "0x%08X | isr_pref\n", table.isr_pref);
1666     IWL_ERR(priv, "0x%08X | wait_event\n", table.wait_event);
1667     IWL_ERR(priv, "0x%08X | l2p_control\n", table.l2p_control);
1668     IWL_ERR(priv, "0x%08X | l2p_duration\n", table.l2p_duration);
1669     IWL_ERR(priv, "0x%08X | l2p_mhvalid\n", table.l2p_mhvalid);
1670     IWL_ERR(priv, "0x%08X | l2p_addr_match\n", table.l2p_addr_match);
1671     IWL_ERR(priv, "0x%08X | lmpm_pmg_sel\n", table.lmpm_pmg_sel);
1672     IWL_ERR(priv, "0x%08X | timestamp\n", table.u_timestamp);
1673     IWL_ERR(priv, "0x%08X | flow_handler\n", table.flow_handler);
1674 }
1675 
1676 #define EVENT_START_OFFSET  (4 * sizeof(u32))
1677 
1678 /*
1679  * iwl_print_event_log - Dump error event log to syslog
1680  */
1681 static int iwl_print_event_log(struct iwl_priv *priv, u32 start_idx,
1682                    u32 num_events, u32 mode,
1683                    int pos, char **buf, size_t bufsz)
1684 {
1685     u32 i;
1686     u32 base;       /* SRAM byte address of event log header */
1687     u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
1688     u32 ptr;        /* SRAM byte address of log data */
1689     u32 ev, time, data; /* event log data */
1690 
1691     struct iwl_trans *trans = priv->trans;
1692 
1693     if (num_events == 0)
1694         return pos;
1695 
1696     base = priv->device_pointers.log_event_table;
1697     if (priv->cur_ucode == IWL_UCODE_INIT) {
1698         if (!base)
1699             base = priv->fw->init_evtlog_ptr;
1700     } else {
1701         if (!base)
1702             base = priv->fw->inst_evtlog_ptr;
1703     }
1704 
1705     if (mode == 0)
1706         event_size = 2 * sizeof(u32);
1707     else
1708         event_size = 3 * sizeof(u32);
1709 
1710     ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
1711 
1712     /* Make sure device is powered up for SRAM reads */
1713     if (!iwl_trans_grab_nic_access(trans))
1714         return pos;
1715 
1716     /* Set starting address; reads will auto-increment */
1717     iwl_write32(trans, HBUS_TARG_MEM_RADDR, ptr);
1718 
1719     /* "time" is actually "data" for mode 0 (no timestamp).
1720     * place event id # at far right for easier visual parsing. */
1721     for (i = 0; i < num_events; i++) {
1722         ev = iwl_read32(trans, HBUS_TARG_MEM_RDAT);
1723         time = iwl_read32(trans, HBUS_TARG_MEM_RDAT);
1724         if (mode == 0) {
1725             /* data, ev */
1726             if (bufsz) {
1727                 pos += scnprintf(*buf + pos, bufsz - pos,
1728                         "EVT_LOG:0x%08x:%04u\n",
1729                         time, ev);
1730             } else {
1731                 trace_iwlwifi_dev_ucode_event(trans->dev, 0,
1732                     time, ev);
1733                 IWL_ERR(priv, "EVT_LOG:0x%08x:%04u\n",
1734                     time, ev);
1735             }
1736         } else {
1737             data = iwl_read32(trans, HBUS_TARG_MEM_RDAT);
1738             if (bufsz) {
1739                 pos += scnprintf(*buf + pos, bufsz - pos,
1740                         "EVT_LOGT:%010u:0x%08x:%04u\n",
1741                          time, data, ev);
1742             } else {
1743                 IWL_ERR(priv, "EVT_LOGT:%010u:0x%08x:%04u\n",
1744                     time, data, ev);
1745                 trace_iwlwifi_dev_ucode_event(trans->dev, time,
1746                     data, ev);
1747             }
1748         }
1749     }
1750 
1751     /* Allow device to power down */
1752     iwl_trans_release_nic_access(trans);
1753     return pos;
1754 }
1755 
1756 /*
1757  * iwl_print_last_event_logs - Dump the newest # of event log to syslog
1758  */
1759 static int iwl_print_last_event_logs(struct iwl_priv *priv, u32 capacity,
1760                     u32 num_wraps, u32 next_entry,
1761                     u32 size, u32 mode,
1762                     int pos, char **buf, size_t bufsz)
1763 {
1764     /*
1765      * display the newest DEFAULT_LOG_ENTRIES entries
1766      * i.e the entries just before the next ont that uCode would fill.
1767      */
1768     if (num_wraps) {
1769         if (next_entry < size) {
1770             pos = iwl_print_event_log(priv,
1771                         capacity - (size - next_entry),
1772                         size - next_entry, mode,
1773                         pos, buf, bufsz);
1774             pos = iwl_print_event_log(priv, 0,
1775                           next_entry, mode,
1776                           pos, buf, bufsz);
1777         } else
1778             pos = iwl_print_event_log(priv, next_entry - size,
1779                           size, mode, pos, buf, bufsz);
1780     } else {
1781         if (next_entry < size) {
1782             pos = iwl_print_event_log(priv, 0, next_entry,
1783                           mode, pos, buf, bufsz);
1784         } else {
1785             pos = iwl_print_event_log(priv, next_entry - size,
1786                           size, mode, pos, buf, bufsz);
1787         }
1788     }
1789     return pos;
1790 }
1791 
1792 #define DEFAULT_DUMP_EVENT_LOG_ENTRIES (20)
1793 
1794 int iwl_dump_nic_event_log(struct iwl_priv *priv, bool full_log,
1795                 char **buf)
1796 {
1797     u32 base;       /* SRAM byte address of event log header */
1798     u32 capacity;   /* event log capacity in # entries */
1799     u32 mode;       /* 0 - no timestamp, 1 - timestamp recorded */
1800     u32 num_wraps;  /* # times uCode wrapped to top of log */
1801     u32 next_entry; /* index of next entry to be written by uCode */
1802     u32 size;       /* # entries that we'll print */
1803     u32 logsize;
1804     int pos = 0;
1805     size_t bufsz = 0;
1806     struct iwl_trans *trans = priv->trans;
1807 
1808     base = priv->device_pointers.log_event_table;
1809     if (priv->cur_ucode == IWL_UCODE_INIT) {
1810         logsize = priv->fw->init_evtlog_size;
1811         if (!base)
1812             base = priv->fw->init_evtlog_ptr;
1813     } else {
1814         logsize = priv->fw->inst_evtlog_size;
1815         if (!base)
1816             base = priv->fw->inst_evtlog_ptr;
1817     }
1818 
1819     if (!iwlagn_hw_valid_rtc_data_addr(base)) {
1820         IWL_ERR(priv,
1821             "Invalid event log pointer 0x%08X for %s uCode\n",
1822             base,
1823             (priv->cur_ucode == IWL_UCODE_INIT)
1824                     ? "Init" : "RT");
1825         return -EINVAL;
1826     }
1827 
1828     /* event log header */
1829     capacity = iwl_trans_read_mem32(trans, base);
1830     mode = iwl_trans_read_mem32(trans, base + (1 * sizeof(u32)));
1831     num_wraps = iwl_trans_read_mem32(trans, base + (2 * sizeof(u32)));
1832     next_entry = iwl_trans_read_mem32(trans, base + (3 * sizeof(u32)));
1833 
1834     if (capacity > logsize) {
1835         IWL_ERR(priv, "Log capacity %d is bogus, limit to %d "
1836             "entries\n", capacity, logsize);
1837         capacity = logsize;
1838     }
1839 
1840     if (next_entry > logsize) {
1841         IWL_ERR(priv, "Log write index %d is bogus, limit to %d\n",
1842             next_entry, logsize);
1843         next_entry = logsize;
1844     }
1845 
1846     size = num_wraps ? capacity : next_entry;
1847 
1848     /* bail out if nothing in log */
1849     if (size == 0) {
1850         IWL_ERR(trans, "Start IWL Event Log Dump: nothing in log\n");
1851         return pos;
1852     }
1853 
1854     if (!(iwl_have_debug_level(IWL_DL_FW)) && !full_log)
1855         size = (size > DEFAULT_DUMP_EVENT_LOG_ENTRIES)
1856             ? DEFAULT_DUMP_EVENT_LOG_ENTRIES : size;
1857     IWL_ERR(priv, "Start IWL Event Log Dump: display last %u entries\n",
1858         size);
1859 
1860 #ifdef CONFIG_IWLWIFI_DEBUG
1861     if (buf) {
1862         if (full_log)
1863             bufsz = capacity * 48;
1864         else
1865             bufsz = size * 48;
1866         *buf = kmalloc(bufsz, GFP_KERNEL);
1867         if (!*buf)
1868             return -ENOMEM;
1869     }
1870     if (iwl_have_debug_level(IWL_DL_FW) || full_log) {
1871         /*
1872          * if uCode has wrapped back to top of log,
1873          * start at the oldest entry,
1874          * i.e the next one that uCode would fill.
1875          */
1876         if (num_wraps)
1877             pos = iwl_print_event_log(priv, next_entry,
1878                         capacity - next_entry, mode,
1879                         pos, buf, bufsz);
1880         /* (then/else) start at top of log */
1881         pos = iwl_print_event_log(priv, 0,
1882                       next_entry, mode, pos, buf, bufsz);
1883     } else
1884         pos = iwl_print_last_event_logs(priv, capacity, num_wraps,
1885                         next_entry, size, mode,
1886                         pos, buf, bufsz);
1887 #else
1888     pos = iwl_print_last_event_logs(priv, capacity, num_wraps,
1889                     next_entry, size, mode,
1890                     pos, buf, bufsz);
1891 #endif
1892     return pos;
1893 }
1894 
1895 static void iwlagn_fw_error(struct iwl_priv *priv, bool ondemand)
1896 {
1897     unsigned int reload_msec;
1898     unsigned long reload_jiffies;
1899 
1900     if (iwl_have_debug_level(IWL_DL_FW))
1901         iwl_print_rx_config_cmd(priv, IWL_RXON_CTX_BSS);
1902 
1903     /* uCode is no longer loaded. */
1904     priv->ucode_loaded = false;
1905 
1906     /* Set the FW error flag -- cleared on iwl_down */
1907     set_bit(STATUS_FW_ERROR, &priv->status);
1908 
1909     iwl_abort_notification_waits(&priv->notif_wait);
1910 
1911     /* Keep the restart process from trying to send host
1912      * commands by clearing the ready bit */
1913     clear_bit(STATUS_READY, &priv->status);
1914 
1915     if (!ondemand) {
1916         /*
1917          * If firmware keep reloading, then it indicate something
1918          * serious wrong and firmware having problem to recover
1919          * from it. Instead of keep trying which will fill the syslog
1920          * and hang the system, let's just stop it
1921          */
1922         reload_jiffies = jiffies;
1923         reload_msec = jiffies_to_msecs((long) reload_jiffies -
1924                     (long) priv->reload_jiffies);
1925         priv->reload_jiffies = reload_jiffies;
1926         if (reload_msec <= IWL_MIN_RELOAD_DURATION) {
1927             priv->reload_count++;
1928             if (priv->reload_count >= IWL_MAX_CONTINUE_RELOAD_CNT) {
1929                 IWL_ERR(priv, "BUG_ON, Stop restarting\n");
1930                 return;
1931             }
1932         } else
1933             priv->reload_count = 0;
1934     }
1935 
1936     if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
1937         if (iwlwifi_mod_params.fw_restart) {
1938             IWL_DEBUG_FW(priv,
1939                      "Restarting adapter due to uCode error.\n");
1940             queue_work(priv->workqueue, &priv->restart);
1941         } else
1942             IWL_DEBUG_FW(priv,
1943                      "Detected FW error, but not restarting\n");
1944     }
1945 }
1946 
1947 static void iwl_nic_error(struct iwl_op_mode *op_mode, bool sync)
1948 {
1949     struct iwl_priv *priv = IWL_OP_MODE_GET_DVM(op_mode);
1950 
1951     IWL_ERR(priv, "Loaded firmware version: %s\n",
1952         priv->fw->fw_version);
1953 
1954     iwl_dump_nic_error_log(priv);
1955     iwl_dump_nic_event_log(priv, false, NULL);
1956 
1957     iwlagn_fw_error(priv, false);
1958 }
1959 
1960 static void iwl_cmd_queue_full(struct iwl_op_mode *op_mode)
1961 {
1962     struct iwl_priv *priv = IWL_OP_MODE_GET_DVM(op_mode);
1963 
1964     if (!iwl_check_for_ct_kill(priv)) {
1965         IWL_ERR(priv, "Restarting adapter queue is full\n");
1966         iwlagn_fw_error(priv, false);
1967     }
1968 }
1969 
1970 #define EEPROM_RF_CONFIG_TYPE_MAX      0x3
1971 
1972 static void iwl_nic_config(struct iwl_op_mode *op_mode)
1973 {
1974     struct iwl_priv *priv = IWL_OP_MODE_GET_DVM(op_mode);
1975 
1976     /* SKU Control */
1977     iwl_trans_set_bits_mask(priv->trans, CSR_HW_IF_CONFIG_REG,
1978                 CSR_HW_IF_CONFIG_REG_MSK_MAC_STEP_DASH,
1979                 CSR_HW_REV_STEP_DASH(priv->trans->hw_rev));
1980 
1981     /* write radio config values to register */
1982     if (priv->nvm_data->radio_cfg_type <= EEPROM_RF_CONFIG_TYPE_MAX) {
1983         u32 reg_val =
1984             priv->nvm_data->radio_cfg_type <<
1985                 CSR_HW_IF_CONFIG_REG_POS_PHY_TYPE |
1986             priv->nvm_data->radio_cfg_step <<
1987                 CSR_HW_IF_CONFIG_REG_POS_PHY_STEP |
1988             priv->nvm_data->radio_cfg_dash <<
1989                 CSR_HW_IF_CONFIG_REG_POS_PHY_DASH;
1990 
1991         iwl_trans_set_bits_mask(priv->trans, CSR_HW_IF_CONFIG_REG,
1992                     CSR_HW_IF_CONFIG_REG_MSK_PHY_TYPE |
1993                     CSR_HW_IF_CONFIG_REG_MSK_PHY_STEP |
1994                     CSR_HW_IF_CONFIG_REG_MSK_PHY_DASH,
1995                     reg_val);
1996 
1997         IWL_INFO(priv, "Radio type=0x%x-0x%x-0x%x\n",
1998              priv->nvm_data->radio_cfg_type,
1999              priv->nvm_data->radio_cfg_step,
2000              priv->nvm_data->radio_cfg_dash);
2001     } else {
2002         WARN_ON(1);
2003     }
2004 
2005     /* set CSR_HW_CONFIG_REG for uCode use */
2006     iwl_set_bit(priv->trans, CSR_HW_IF_CONFIG_REG,
2007             CSR_HW_IF_CONFIG_REG_BIT_RADIO_SI |
2008             CSR_HW_IF_CONFIG_REG_BIT_MAC_SI);
2009 
2010     /* W/A : NIC is stuck in a reset state after Early PCIe power off
2011      * (PCIe power is lost before PERST# is asserted),
2012      * causing ME FW to lose ownership and not being able to obtain it back.
2013      */
2014     iwl_set_bits_mask_prph(priv->trans, APMG_PS_CTRL_REG,
2015                    APMG_PS_CTRL_EARLY_PWR_OFF_RESET_DIS,
2016                    ~APMG_PS_CTRL_EARLY_PWR_OFF_RESET_DIS);
2017 
2018     if (priv->lib->nic_config)
2019         priv->lib->nic_config(priv);
2020 }
2021 
2022 static void iwl_wimax_active(struct iwl_op_mode *op_mode)
2023 {
2024     struct iwl_priv *priv = IWL_OP_MODE_GET_DVM(op_mode);
2025 
2026     clear_bit(STATUS_READY, &priv->status);
2027     IWL_ERR(priv, "RF is used by WiMAX\n");
2028 }
2029 
2030 static void iwl_stop_sw_queue(struct iwl_op_mode *op_mode, int queue)
2031 {
2032     struct iwl_priv *priv = IWL_OP_MODE_GET_DVM(op_mode);
2033     int mq = priv->queue_to_mac80211[queue];
2034 
2035     if (WARN_ON_ONCE(mq == IWL_INVALID_MAC80211_QUEUE))
2036         return;
2037 
2038     if (atomic_inc_return(&priv->queue_stop_count[mq]) > 1) {
2039         IWL_DEBUG_TX_QUEUES(priv,
2040             "queue %d (mac80211 %d) already stopped\n",
2041             queue, mq);
2042         return;
2043     }
2044 
2045     set_bit(mq, &priv->transport_queue_stop);
2046     ieee80211_stop_queue(priv->hw, mq);
2047 }
2048 
2049 static void iwl_wake_sw_queue(struct iwl_op_mode *op_mode, int queue)
2050 {
2051     struct iwl_priv *priv = IWL_OP_MODE_GET_DVM(op_mode);
2052     int mq = priv->queue_to_mac80211[queue];
2053 
2054     if (WARN_ON_ONCE(mq == IWL_INVALID_MAC80211_QUEUE))
2055         return;
2056 
2057     if (atomic_dec_return(&priv->queue_stop_count[mq]) > 0) {
2058         IWL_DEBUG_TX_QUEUES(priv,
2059             "queue %d (mac80211 %d) already awake\n",
2060             queue, mq);
2061         return;
2062     }
2063 
2064     clear_bit(mq, &priv->transport_queue_stop);
2065 
2066     if (!priv->passive_no_rx)
2067         ieee80211_wake_queue(priv->hw, mq);
2068 }
2069 
2070 void iwlagn_lift_passive_no_rx(struct iwl_priv *priv)
2071 {
2072     int mq;
2073 
2074     if (!priv->passive_no_rx)
2075         return;
2076 
2077     for (mq = 0; mq < IWLAGN_FIRST_AMPDU_QUEUE; mq++) {
2078         if (!test_bit(mq, &priv->transport_queue_stop)) {
2079             IWL_DEBUG_TX_QUEUES(priv, "Wake queue %d\n", mq);
2080             ieee80211_wake_queue(priv->hw, mq);
2081         } else {
2082             IWL_DEBUG_TX_QUEUES(priv, "Don't wake queue %d\n", mq);
2083         }
2084     }
2085 
2086     priv->passive_no_rx = false;
2087 }
2088 
2089 static void iwl_free_skb(struct iwl_op_mode *op_mode, struct sk_buff *skb)
2090 {
2091     struct iwl_priv *priv = IWL_OP_MODE_GET_DVM(op_mode);
2092     struct ieee80211_tx_info *info;
2093 
2094     info = IEEE80211_SKB_CB(skb);
2095     iwl_trans_free_tx_cmd(priv->trans, info->driver_data[1]);
2096     ieee80211_free_txskb(priv->hw, skb);
2097 }
2098 
2099 static bool iwl_set_hw_rfkill_state(struct iwl_op_mode *op_mode, bool state)
2100 {
2101     struct iwl_priv *priv = IWL_OP_MODE_GET_DVM(op_mode);
2102 
2103     if (state)
2104         set_bit(STATUS_RF_KILL_HW, &priv->status);
2105     else
2106         clear_bit(STATUS_RF_KILL_HW, &priv->status);
2107 
2108     wiphy_rfkill_set_hw_state(priv->hw->wiphy, state);
2109 
2110     return false;
2111 }
2112 
2113 static const struct iwl_op_mode_ops iwl_dvm_ops = {
2114     .start = iwl_op_mode_dvm_start,
2115     .stop = iwl_op_mode_dvm_stop,
2116     .rx = iwl_rx_dispatch,
2117     .queue_full = iwl_stop_sw_queue,
2118     .queue_not_full = iwl_wake_sw_queue,
2119     .hw_rf_kill = iwl_set_hw_rfkill_state,
2120     .free_skb = iwl_free_skb,
2121     .nic_error = iwl_nic_error,
2122     .cmd_queue_full = iwl_cmd_queue_full,
2123     .nic_config = iwl_nic_config,
2124     .wimax_active = iwl_wimax_active,
2125 };
2126 
2127 /*****************************************************************************
2128  *
2129  * driver and module entry point
2130  *
2131  *****************************************************************************/
2132 static int __init iwl_init(void)
2133 {
2134 
2135     int ret;
2136 
2137     ret = iwlagn_rate_control_register();
2138     if (ret) {
2139         pr_err("Unable to register rate control algorithm: %d\n", ret);
2140         return ret;
2141     }
2142 
2143     ret = iwl_opmode_register("iwldvm", &iwl_dvm_ops);
2144     if (ret) {
2145         pr_err("Unable to register op_mode: %d\n", ret);
2146         iwlagn_rate_control_unregister();
2147     }
2148 
2149     return ret;
2150 }
2151 module_init(iwl_init);
2152 
2153 static void __exit iwl_exit(void)
2154 {
2155     iwl_opmode_deregister("iwldvm");
2156     iwlagn_rate_control_unregister();
2157 }
2158 module_exit(iwl_exit);