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