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