Linux Audio

Check our new training course

Linux BSP upgrade and security maintenance

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