Linux Audio

Check our new training course

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