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