a46ad60216a0c6a317769526c9f85cff6cbd78f4
[pandora-kernel.git] / drivers / net / wireless / iwlwifi / iwl-core.c
1 /******************************************************************************
2  *
3  * GPL LICENSE SUMMARY
4  *
5  * Copyright(c) 2008 - 2010 Intel Corporation. All rights reserved.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of version 2 of the GNU General Public License as
9  * published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
19  * USA
20  *
21  * The full GNU General Public License is included in this distribution
22  * in the file called LICENSE.GPL.
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 #include <linux/kernel.h>
30 #include <linux/module.h>
31 #include <linux/etherdevice.h>
32 #include <linux/sched.h>
33 #include <linux/slab.h>
34 #include <net/mac80211.h>
35
36 #include "iwl-eeprom.h"
37 #include "iwl-dev.h" /* FIXME: remove */
38 #include "iwl-debug.h"
39 #include "iwl-core.h"
40 #include "iwl-io.h"
41 #include "iwl-power.h"
42 #include "iwl-sta.h"
43 #include "iwl-helpers.h"
44
45
46 MODULE_DESCRIPTION("iwl core");
47 MODULE_VERSION(IWLWIFI_VERSION);
48 MODULE_AUTHOR(DRV_COPYRIGHT " " DRV_AUTHOR);
49 MODULE_LICENSE("GPL");
50
51 /*
52  * set bt_coex_active to true, uCode will do kill/defer
53  * every time the priority line is asserted (BT is sending signals on the
54  * priority line in the PCIx).
55  * set bt_coex_active to false, uCode will ignore the BT activity and
56  * perform the normal operation
57  *
58  * User might experience transmit issue on some platform due to WiFi/BT
59  * co-exist problem. The possible behaviors are:
60  *   Able to scan and finding all the available AP
61  *   Not able to associate with any AP
62  * On those platforms, WiFi communication can be restored by set
63  * "bt_coex_active" module parameter to "false"
64  *
65  * default: bt_coex_active = true (BT_COEX_ENABLE)
66  */
67 bool bt_coex_active = true;
68 EXPORT_SYMBOL_GPL(bt_coex_active);
69 module_param(bt_coex_active, bool, S_IRUGO);
70 MODULE_PARM_DESC(bt_coex_active, "enable wifi/bluetooth co-exist");
71
72 u32 iwl_debug_level;
73 EXPORT_SYMBOL(iwl_debug_level);
74
75 const u8 iwl_bcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
76 EXPORT_SYMBOL(iwl_bcast_addr);
77
78
79 /* This function both allocates and initializes hw and priv. */
80 struct ieee80211_hw *iwl_alloc_all(struct iwl_cfg *cfg)
81 {
82         struct iwl_priv *priv;
83         /* mac80211 allocates memory for this device instance, including
84          *   space for this driver's private structure */
85         struct ieee80211_hw *hw;
86
87         hw = ieee80211_alloc_hw(sizeof(struct iwl_priv),
88                                 cfg->ops->ieee80211_ops);
89         if (hw == NULL) {
90                 pr_err("%s: Can not allocate network device\n",
91                        cfg->name);
92                 goto out;
93         }
94
95         priv = hw->priv;
96         priv->hw = hw;
97
98 out:
99         return hw;
100 }
101 EXPORT_SYMBOL(iwl_alloc_all);
102
103 #define MAX_BIT_RATE_40_MHZ 150 /* Mbps */
104 #define MAX_BIT_RATE_20_MHZ 72 /* Mbps */
105 static void iwlcore_init_ht_hw_capab(const struct iwl_priv *priv,
106                               struct ieee80211_sta_ht_cap *ht_info,
107                               enum ieee80211_band band)
108 {
109         u16 max_bit_rate = 0;
110         u8 rx_chains_num = priv->hw_params.rx_chains_num;
111         u8 tx_chains_num = priv->hw_params.tx_chains_num;
112
113         ht_info->cap = 0;
114         memset(&ht_info->mcs, 0, sizeof(ht_info->mcs));
115
116         ht_info->ht_supported = true;
117
118         if (priv->cfg->ht_params &&
119             priv->cfg->ht_params->ht_greenfield_support)
120                 ht_info->cap |= IEEE80211_HT_CAP_GRN_FLD;
121         ht_info->cap |= IEEE80211_HT_CAP_SGI_20;
122         max_bit_rate = MAX_BIT_RATE_20_MHZ;
123         if (priv->hw_params.ht40_channel & BIT(band)) {
124                 ht_info->cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
125                 ht_info->cap |= IEEE80211_HT_CAP_SGI_40;
126                 ht_info->mcs.rx_mask[4] = 0x01;
127                 max_bit_rate = MAX_BIT_RATE_40_MHZ;
128         }
129
130         if (priv->cfg->mod_params->amsdu_size_8K)
131                 ht_info->cap |= IEEE80211_HT_CAP_MAX_AMSDU;
132
133         ht_info->ampdu_factor = CFG_HT_RX_AMPDU_FACTOR_DEF;
134         if (priv->cfg->bt_params && priv->cfg->bt_params->ampdu_factor)
135                 ht_info->ampdu_factor = priv->cfg->bt_params->ampdu_factor;
136         ht_info->ampdu_density = CFG_HT_MPDU_DENSITY_DEF;
137         if (priv->cfg->bt_params && priv->cfg->bt_params->ampdu_density)
138                 ht_info->ampdu_density = priv->cfg->bt_params->ampdu_density;
139
140         ht_info->mcs.rx_mask[0] = 0xFF;
141         if (rx_chains_num >= 2)
142                 ht_info->mcs.rx_mask[1] = 0xFF;
143         if (rx_chains_num >= 3)
144                 ht_info->mcs.rx_mask[2] = 0xFF;
145
146         /* Highest supported Rx data rate */
147         max_bit_rate *= rx_chains_num;
148         WARN_ON(max_bit_rate & ~IEEE80211_HT_MCS_RX_HIGHEST_MASK);
149         ht_info->mcs.rx_highest = cpu_to_le16(max_bit_rate);
150
151         /* Tx MCS capabilities */
152         ht_info->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
153         if (tx_chains_num != rx_chains_num) {
154                 ht_info->mcs.tx_params |= IEEE80211_HT_MCS_TX_RX_DIFF;
155                 ht_info->mcs.tx_params |= ((tx_chains_num - 1) <<
156                                 IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT);
157         }
158 }
159
160 /**
161  * iwlcore_init_geos - Initialize mac80211's geo/channel info based from eeprom
162  */
163 int iwlcore_init_geos(struct iwl_priv *priv)
164 {
165         struct iwl_channel_info *ch;
166         struct ieee80211_supported_band *sband;
167         struct ieee80211_channel *channels;
168         struct ieee80211_channel *geo_ch;
169         struct ieee80211_rate *rates;
170         int i = 0;
171
172         if (priv->bands[IEEE80211_BAND_2GHZ].n_bitrates ||
173             priv->bands[IEEE80211_BAND_5GHZ].n_bitrates) {
174                 IWL_DEBUG_INFO(priv, "Geography modes already initialized.\n");
175                 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
176                 return 0;
177         }
178
179         channels = kzalloc(sizeof(struct ieee80211_channel) *
180                            priv->channel_count, GFP_KERNEL);
181         if (!channels)
182                 return -ENOMEM;
183
184         rates = kzalloc((sizeof(struct ieee80211_rate) * IWL_RATE_COUNT_LEGACY),
185                         GFP_KERNEL);
186         if (!rates) {
187                 kfree(channels);
188                 return -ENOMEM;
189         }
190
191         /* 5.2GHz channels start after the 2.4GHz channels */
192         sband = &priv->bands[IEEE80211_BAND_5GHZ];
193         sband->channels = &channels[ARRAY_SIZE(iwl_eeprom_band_1)];
194         /* just OFDM */
195         sband->bitrates = &rates[IWL_FIRST_OFDM_RATE];
196         sband->n_bitrates = IWL_RATE_COUNT_LEGACY - IWL_FIRST_OFDM_RATE;
197
198         if (priv->cfg->sku & IWL_SKU_N)
199                 iwlcore_init_ht_hw_capab(priv, &sband->ht_cap,
200                                          IEEE80211_BAND_5GHZ);
201
202         sband = &priv->bands[IEEE80211_BAND_2GHZ];
203         sband->channels = channels;
204         /* OFDM & CCK */
205         sband->bitrates = rates;
206         sband->n_bitrates = IWL_RATE_COUNT_LEGACY;
207
208         if (priv->cfg->sku & IWL_SKU_N)
209                 iwlcore_init_ht_hw_capab(priv, &sband->ht_cap,
210                                          IEEE80211_BAND_2GHZ);
211
212         priv->ieee_channels = channels;
213         priv->ieee_rates = rates;
214
215         for (i = 0;  i < priv->channel_count; i++) {
216                 ch = &priv->channel_info[i];
217
218                 /* FIXME: might be removed if scan is OK */
219                 if (!is_channel_valid(ch))
220                         continue;
221
222                 if (is_channel_a_band(ch))
223                         sband =  &priv->bands[IEEE80211_BAND_5GHZ];
224                 else
225                         sband =  &priv->bands[IEEE80211_BAND_2GHZ];
226
227                 geo_ch = &sband->channels[sband->n_channels++];
228
229                 geo_ch->center_freq =
230                                 ieee80211_channel_to_frequency(ch->channel,
231                                                                sband->band);
232                 geo_ch->max_power = ch->max_power_avg;
233                 geo_ch->max_antenna_gain = 0xff;
234                 geo_ch->hw_value = ch->channel;
235
236                 if (is_channel_valid(ch)) {
237                         if (!(ch->flags & EEPROM_CHANNEL_IBSS))
238                                 geo_ch->flags |= IEEE80211_CHAN_NO_IBSS;
239
240                         if (!(ch->flags & EEPROM_CHANNEL_ACTIVE))
241                                 geo_ch->flags |= IEEE80211_CHAN_PASSIVE_SCAN;
242
243                         if (ch->flags & EEPROM_CHANNEL_RADAR)
244                                 geo_ch->flags |= IEEE80211_CHAN_RADAR;
245
246                         geo_ch->flags |= ch->ht40_extension_channel;
247
248                         if (ch->max_power_avg > priv->tx_power_device_lmt)
249                                 priv->tx_power_device_lmt = ch->max_power_avg;
250                 } else {
251                         geo_ch->flags |= IEEE80211_CHAN_DISABLED;
252                 }
253
254                 IWL_DEBUG_INFO(priv, "Channel %d Freq=%d[%sGHz] %s flag=0x%X\n",
255                                 ch->channel, geo_ch->center_freq,
256                                 is_channel_a_band(ch) ?  "5.2" : "2.4",
257                                 geo_ch->flags & IEEE80211_CHAN_DISABLED ?
258                                 "restricted" : "valid",
259                                  geo_ch->flags);
260         }
261
262         if ((priv->bands[IEEE80211_BAND_5GHZ].n_channels == 0) &&
263              priv->cfg->sku & IWL_SKU_A) {
264                 IWL_INFO(priv, "Incorrectly detected BG card as ABG. "
265                         "Please send your PCI ID 0x%04X:0x%04X to maintainer.\n",
266                            priv->pci_dev->device,
267                            priv->pci_dev->subsystem_device);
268                 priv->cfg->sku &= ~IWL_SKU_A;
269         }
270
271         IWL_INFO(priv, "Tunable channels: %d 802.11bg, %d 802.11a channels\n",
272                    priv->bands[IEEE80211_BAND_2GHZ].n_channels,
273                    priv->bands[IEEE80211_BAND_5GHZ].n_channels);
274
275         set_bit(STATUS_GEO_CONFIGURED, &priv->status);
276
277         return 0;
278 }
279 EXPORT_SYMBOL(iwlcore_init_geos);
280
281 /*
282  * iwlcore_free_geos - undo allocations in iwlcore_init_geos
283  */
284 void iwlcore_free_geos(struct iwl_priv *priv)
285 {
286         kfree(priv->ieee_channels);
287         kfree(priv->ieee_rates);
288         clear_bit(STATUS_GEO_CONFIGURED, &priv->status);
289 }
290 EXPORT_SYMBOL(iwlcore_free_geos);
291
292 static bool iwl_is_channel_extension(struct iwl_priv *priv,
293                                      enum ieee80211_band band,
294                                      u16 channel, u8 extension_chan_offset)
295 {
296         const struct iwl_channel_info *ch_info;
297
298         ch_info = iwl_get_channel_info(priv, band, channel);
299         if (!is_channel_valid(ch_info))
300                 return false;
301
302         if (extension_chan_offset == IEEE80211_HT_PARAM_CHA_SEC_ABOVE)
303                 return !(ch_info->ht40_extension_channel &
304                                         IEEE80211_CHAN_NO_HT40PLUS);
305         else if (extension_chan_offset == IEEE80211_HT_PARAM_CHA_SEC_BELOW)
306                 return !(ch_info->ht40_extension_channel &
307                                         IEEE80211_CHAN_NO_HT40MINUS);
308
309         return false;
310 }
311
312 bool iwl_is_ht40_tx_allowed(struct iwl_priv *priv,
313                             struct iwl_rxon_context *ctx,
314                             struct ieee80211_sta_ht_cap *ht_cap)
315 {
316         if (!ctx->ht.enabled || !ctx->ht.is_40mhz)
317                 return false;
318
319         /*
320          * We do not check for IEEE80211_HT_CAP_SUP_WIDTH_20_40
321          * the bit will not set if it is pure 40MHz case
322          */
323         if (ht_cap && !ht_cap->ht_supported)
324                 return false;
325
326 #ifdef CONFIG_IWLWIFI_DEBUGFS
327         if (priv->disable_ht40)
328                 return false;
329 #endif
330
331         return iwl_is_channel_extension(priv, priv->band,
332                         le16_to_cpu(ctx->staging.channel),
333                         ctx->ht.extension_chan_offset);
334 }
335 EXPORT_SYMBOL(iwl_is_ht40_tx_allowed);
336
337 static u16 iwl_adjust_beacon_interval(u16 beacon_val, u16 max_beacon_val)
338 {
339         u16 new_val;
340         u16 beacon_factor;
341
342         /*
343          * If mac80211 hasn't given us a beacon interval, program
344          * the default into the device (not checking this here
345          * would cause the adjustment below to return the maximum
346          * value, which may break PAN.)
347          */
348         if (!beacon_val)
349                 return DEFAULT_BEACON_INTERVAL;
350
351         /*
352          * If the beacon interval we obtained from the peer
353          * is too large, we'll have to wake up more often
354          * (and in IBSS case, we'll beacon too much)
355          *
356          * For example, if max_beacon_val is 4096, and the
357          * requested beacon interval is 7000, we'll have to
358          * use 3500 to be able to wake up on the beacons.
359          *
360          * This could badly influence beacon detection stats.
361          */
362
363         beacon_factor = (beacon_val + max_beacon_val) / max_beacon_val;
364         new_val = beacon_val / beacon_factor;
365
366         if (!new_val)
367                 new_val = max_beacon_val;
368
369         return new_val;
370 }
371
372 int iwl_send_rxon_timing(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
373 {
374         u64 tsf;
375         s32 interval_tm, rem;
376         struct ieee80211_conf *conf = NULL;
377         u16 beacon_int;
378         struct ieee80211_vif *vif = ctx->vif;
379
380         conf = ieee80211_get_hw_conf(priv->hw);
381
382         lockdep_assert_held(&priv->mutex);
383
384         memset(&ctx->timing, 0, sizeof(struct iwl_rxon_time_cmd));
385
386         ctx->timing.timestamp = cpu_to_le64(priv->timestamp);
387         ctx->timing.listen_interval = cpu_to_le16(conf->listen_interval);
388
389         beacon_int = vif ? vif->bss_conf.beacon_int : 0;
390
391         /*
392          * TODO: For IBSS we need to get atim_window from mac80211,
393          *       for now just always use 0
394          */
395         ctx->timing.atim_window = 0;
396
397         if (ctx->ctxid == IWL_RXON_CTX_PAN &&
398             (!ctx->vif || ctx->vif->type != NL80211_IFTYPE_STATION) &&
399             iwl_is_associated(priv, IWL_RXON_CTX_BSS) &&
400             priv->contexts[IWL_RXON_CTX_BSS].vif &&
401             priv->contexts[IWL_RXON_CTX_BSS].vif->bss_conf.beacon_int) {
402                 ctx->timing.beacon_interval =
403                         priv->contexts[IWL_RXON_CTX_BSS].timing.beacon_interval;
404                 beacon_int = le16_to_cpu(ctx->timing.beacon_interval);
405         } else if (ctx->ctxid == IWL_RXON_CTX_BSS &&
406                    iwl_is_associated(priv, IWL_RXON_CTX_PAN) &&
407                    priv->contexts[IWL_RXON_CTX_PAN].vif &&
408                    priv->contexts[IWL_RXON_CTX_PAN].vif->bss_conf.beacon_int &&
409                    (!iwl_is_associated_ctx(ctx) || !ctx->vif ||
410                     !ctx->vif->bss_conf.beacon_int)) {
411                 ctx->timing.beacon_interval =
412                         priv->contexts[IWL_RXON_CTX_PAN].timing.beacon_interval;
413                 beacon_int = le16_to_cpu(ctx->timing.beacon_interval);
414         } else {
415                 beacon_int = iwl_adjust_beacon_interval(beacon_int,
416                                 priv->hw_params.max_beacon_itrvl * TIME_UNIT);
417                 ctx->timing.beacon_interval = cpu_to_le16(beacon_int);
418         }
419
420         tsf = priv->timestamp; /* tsf is modifed by do_div: copy it */
421         interval_tm = beacon_int * TIME_UNIT;
422         rem = do_div(tsf, interval_tm);
423         ctx->timing.beacon_init_val = cpu_to_le32(interval_tm - rem);
424
425         ctx->timing.dtim_period = vif ? (vif->bss_conf.dtim_period ?: 1) : 1;
426
427         IWL_DEBUG_ASSOC(priv,
428                         "beacon interval %d beacon timer %d beacon tim %d\n",
429                         le16_to_cpu(ctx->timing.beacon_interval),
430                         le32_to_cpu(ctx->timing.beacon_init_val),
431                         le16_to_cpu(ctx->timing.atim_window));
432
433         return iwl_send_cmd_pdu(priv, ctx->rxon_timing_cmd,
434                                 sizeof(ctx->timing), &ctx->timing);
435 }
436 EXPORT_SYMBOL(iwl_send_rxon_timing);
437
438 void iwl_set_rxon_hwcrypto(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
439                            int hw_decrypt)
440 {
441         struct iwl_rxon_cmd *rxon = &ctx->staging;
442
443         if (hw_decrypt)
444                 rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
445         else
446                 rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
447
448 }
449 EXPORT_SYMBOL(iwl_set_rxon_hwcrypto);
450
451 /* validate RXON structure is valid */
452 int iwl_check_rxon_cmd(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
453 {
454         struct iwl_rxon_cmd *rxon = &ctx->staging;
455         bool error = false;
456
457         if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
458                 if (rxon->flags & RXON_FLG_TGJ_NARROW_BAND_MSK) {
459                         IWL_WARN(priv, "check 2.4G: wrong narrow\n");
460                         error = true;
461                 }
462                 if (rxon->flags & RXON_FLG_RADAR_DETECT_MSK) {
463                         IWL_WARN(priv, "check 2.4G: wrong radar\n");
464                         error = true;
465                 }
466         } else {
467                 if (!(rxon->flags & RXON_FLG_SHORT_SLOT_MSK)) {
468                         IWL_WARN(priv, "check 5.2G: not short slot!\n");
469                         error = true;
470                 }
471                 if (rxon->flags & RXON_FLG_CCK_MSK) {
472                         IWL_WARN(priv, "check 5.2G: CCK!\n");
473                         error = true;
474                 }
475         }
476         if ((rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1) {
477                 IWL_WARN(priv, "mac/bssid mcast!\n");
478                 error = true;
479         }
480
481         /* make sure basic rates 6Mbps and 1Mbps are supported */
482         if ((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0 &&
483             (rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0) {
484                 IWL_WARN(priv, "neither 1 nor 6 are basic\n");
485                 error = true;
486         }
487
488         if (le16_to_cpu(rxon->assoc_id) > 2007) {
489                 IWL_WARN(priv, "aid > 2007\n");
490                 error = true;
491         }
492
493         if ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
494                         == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK)) {
495                 IWL_WARN(priv, "CCK and short slot\n");
496                 error = true;
497         }
498
499         if ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
500                         == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK)) {
501                 IWL_WARN(priv, "CCK and auto detect");
502                 error = true;
503         }
504
505         if ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
506                             RXON_FLG_TGG_PROTECT_MSK)) ==
507                             RXON_FLG_TGG_PROTECT_MSK) {
508                 IWL_WARN(priv, "TGg but no auto-detect\n");
509                 error = true;
510         }
511
512         if (error)
513                 IWL_WARN(priv, "Tuning to channel %d\n",
514                             le16_to_cpu(rxon->channel));
515
516         if (error) {
517                 IWL_ERR(priv, "Invalid RXON\n");
518                 return -EINVAL;
519         }
520         return 0;
521 }
522 EXPORT_SYMBOL(iwl_check_rxon_cmd);
523
524 /**
525  * iwl_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
526  * @priv: staging_rxon is compared to active_rxon
527  *
528  * If the RXON structure is changing enough to require a new tune,
529  * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
530  * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
531  */
532 int iwl_full_rxon_required(struct iwl_priv *priv,
533                            struct iwl_rxon_context *ctx)
534 {
535         const struct iwl_rxon_cmd *staging = &ctx->staging;
536         const struct iwl_rxon_cmd *active = &ctx->active;
537
538 #define CHK(cond)                                                       \
539         if ((cond)) {                                                   \
540                 IWL_DEBUG_INFO(priv, "need full RXON - " #cond "\n");   \
541                 return 1;                                               \
542         }
543
544 #define CHK_NEQ(c1, c2)                                         \
545         if ((c1) != (c2)) {                                     \
546                 IWL_DEBUG_INFO(priv, "need full RXON - "        \
547                                #c1 " != " #c2 " - %d != %d\n",  \
548                                (c1), (c2));                     \
549                 return 1;                                       \
550         }
551
552         /* These items are only settable from the full RXON command */
553         CHK(!iwl_is_associated_ctx(ctx));
554         CHK(compare_ether_addr(staging->bssid_addr, active->bssid_addr));
555         CHK(compare_ether_addr(staging->node_addr, active->node_addr));
556         CHK(compare_ether_addr(staging->wlap_bssid_addr,
557                                 active->wlap_bssid_addr));
558         CHK_NEQ(staging->dev_type, active->dev_type);
559         CHK_NEQ(staging->channel, active->channel);
560         CHK_NEQ(staging->air_propagation, active->air_propagation);
561         CHK_NEQ(staging->ofdm_ht_single_stream_basic_rates,
562                 active->ofdm_ht_single_stream_basic_rates);
563         CHK_NEQ(staging->ofdm_ht_dual_stream_basic_rates,
564                 active->ofdm_ht_dual_stream_basic_rates);
565         CHK_NEQ(staging->ofdm_ht_triple_stream_basic_rates,
566                 active->ofdm_ht_triple_stream_basic_rates);
567         CHK_NEQ(staging->assoc_id, active->assoc_id);
568
569         /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
570          * be updated with the RXON_ASSOC command -- however only some
571          * flag transitions are allowed using RXON_ASSOC */
572
573         /* Check if we are not switching bands */
574         CHK_NEQ(staging->flags & RXON_FLG_BAND_24G_MSK,
575                 active->flags & RXON_FLG_BAND_24G_MSK);
576
577         /* Check if we are switching association toggle */
578         CHK_NEQ(staging->filter_flags & RXON_FILTER_ASSOC_MSK,
579                 active->filter_flags & RXON_FILTER_ASSOC_MSK);
580
581 #undef CHK
582 #undef CHK_NEQ
583
584         return 0;
585 }
586 EXPORT_SYMBOL(iwl_full_rxon_required);
587
588 u8 iwl_rate_get_lowest_plcp(struct iwl_priv *priv,
589                             struct iwl_rxon_context *ctx)
590 {
591         /*
592          * Assign the lowest rate -- should really get this from
593          * the beacon skb from mac80211.
594          */
595         if (ctx->staging.flags & RXON_FLG_BAND_24G_MSK)
596                 return IWL_RATE_1M_PLCP;
597         else
598                 return IWL_RATE_6M_PLCP;
599 }
600 EXPORT_SYMBOL(iwl_rate_get_lowest_plcp);
601
602 static void _iwl_set_rxon_ht(struct iwl_priv *priv,
603                              struct iwl_ht_config *ht_conf,
604                              struct iwl_rxon_context *ctx)
605 {
606         struct iwl_rxon_cmd *rxon = &ctx->staging;
607
608         if (!ctx->ht.enabled) {
609                 rxon->flags &= ~(RXON_FLG_CHANNEL_MODE_MSK |
610                         RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK |
611                         RXON_FLG_HT40_PROT_MSK |
612                         RXON_FLG_HT_PROT_MSK);
613                 return;
614         }
615
616         /* FIXME: if the definition of ht.protection changed, the "translation"
617          * will be needed for rxon->flags
618          */
619         rxon->flags |= cpu_to_le32(ctx->ht.protection << RXON_FLG_HT_OPERATING_MODE_POS);
620
621         /* Set up channel bandwidth:
622          * 20 MHz only, 20/40 mixed or pure 40 if ht40 ok */
623         /* clear the HT channel mode before set the mode */
624         rxon->flags &= ~(RXON_FLG_CHANNEL_MODE_MSK |
625                          RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK);
626         if (iwl_is_ht40_tx_allowed(priv, ctx, NULL)) {
627                 /* pure ht40 */
628                 if (ctx->ht.protection == IEEE80211_HT_OP_MODE_PROTECTION_20MHZ) {
629                         rxon->flags |= RXON_FLG_CHANNEL_MODE_PURE_40;
630                         /* Note: control channel is opposite of extension channel */
631                         switch (ctx->ht.extension_chan_offset) {
632                         case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
633                                 rxon->flags &= ~RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
634                                 break;
635                         case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
636                                 rxon->flags |= RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
637                                 break;
638                         }
639                 } else {
640                         /* Note: control channel is opposite of extension channel */
641                         switch (ctx->ht.extension_chan_offset) {
642                         case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
643                                 rxon->flags &= ~(RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK);
644                                 rxon->flags |= RXON_FLG_CHANNEL_MODE_MIXED;
645                                 break;
646                         case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
647                                 rxon->flags |= RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
648                                 rxon->flags |= RXON_FLG_CHANNEL_MODE_MIXED;
649                                 break;
650                         case IEEE80211_HT_PARAM_CHA_SEC_NONE:
651                         default:
652                                 /* channel location only valid if in Mixed mode */
653                                 IWL_ERR(priv, "invalid extension channel offset\n");
654                                 break;
655                         }
656                 }
657         } else {
658                 rxon->flags |= RXON_FLG_CHANNEL_MODE_LEGACY;
659         }
660
661         if (priv->cfg->ops->hcmd->set_rxon_chain)
662                 priv->cfg->ops->hcmd->set_rxon_chain(priv, ctx);
663
664         IWL_DEBUG_ASSOC(priv, "rxon flags 0x%X operation mode :0x%X "
665                         "extension channel offset 0x%x\n",
666                         le32_to_cpu(rxon->flags), ctx->ht.protection,
667                         ctx->ht.extension_chan_offset);
668 }
669
670 void iwl_set_rxon_ht(struct iwl_priv *priv, struct iwl_ht_config *ht_conf)
671 {
672         struct iwl_rxon_context *ctx;
673
674         for_each_context(priv, ctx)
675                 _iwl_set_rxon_ht(priv, ht_conf, ctx);
676 }
677 EXPORT_SYMBOL(iwl_set_rxon_ht);
678
679 /* Return valid, unused, channel for a passive scan to reset the RF */
680 u8 iwl_get_single_channel_number(struct iwl_priv *priv,
681                                  enum ieee80211_band band)
682 {
683         const struct iwl_channel_info *ch_info;
684         int i;
685         u8 channel = 0;
686         u8 min, max;
687         struct iwl_rxon_context *ctx;
688
689         if (band == IEEE80211_BAND_5GHZ) {
690                 min = 14;
691                 max = priv->channel_count;
692         } else {
693                 min = 0;
694                 max = 14;
695         }
696
697         for (i = min; i < max; i++) {
698                 bool busy = false;
699
700                 for_each_context(priv, ctx) {
701                         busy = priv->channel_info[i].channel ==
702                                 le16_to_cpu(ctx->staging.channel);
703                         if (busy)
704                                 break;
705                 }
706
707                 if (busy)
708                         continue;
709
710                 channel = priv->channel_info[i].channel;
711                 ch_info = iwl_get_channel_info(priv, band, channel);
712                 if (is_channel_valid(ch_info))
713                         break;
714         }
715
716         return channel;
717 }
718 EXPORT_SYMBOL(iwl_get_single_channel_number);
719
720 /**
721  * iwl_set_rxon_channel - Set the band and channel values in staging RXON
722  * @ch: requested channel as a pointer to struct ieee80211_channel
723
724  * NOTE:  Does not commit to the hardware; it sets appropriate bit fields
725  * in the staging RXON flag structure based on the ch->band
726  */
727 int iwl_set_rxon_channel(struct iwl_priv *priv, struct ieee80211_channel *ch,
728                          struct iwl_rxon_context *ctx)
729 {
730         enum ieee80211_band band = ch->band;
731         u16 channel = ch->hw_value;
732
733         if ((le16_to_cpu(ctx->staging.channel) == channel) &&
734             (priv->band == band))
735                 return 0;
736
737         ctx->staging.channel = cpu_to_le16(channel);
738         if (band == IEEE80211_BAND_5GHZ)
739                 ctx->staging.flags &= ~RXON_FLG_BAND_24G_MSK;
740         else
741                 ctx->staging.flags |= RXON_FLG_BAND_24G_MSK;
742
743         priv->band = band;
744
745         IWL_DEBUG_INFO(priv, "Staging channel set to %d [%d]\n", channel, band);
746
747         return 0;
748 }
749 EXPORT_SYMBOL(iwl_set_rxon_channel);
750
751 void iwl_set_flags_for_band(struct iwl_priv *priv,
752                             struct iwl_rxon_context *ctx,
753                             enum ieee80211_band band,
754                             struct ieee80211_vif *vif)
755 {
756         if (band == IEEE80211_BAND_5GHZ) {
757                 ctx->staging.flags &=
758                     ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
759                       | RXON_FLG_CCK_MSK);
760                 ctx->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
761         } else {
762                 /* Copied from iwl_post_associate() */
763                 if (vif && vif->bss_conf.use_short_slot)
764                         ctx->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
765                 else
766                         ctx->staging.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
767
768                 ctx->staging.flags |= RXON_FLG_BAND_24G_MSK;
769                 ctx->staging.flags |= RXON_FLG_AUTO_DETECT_MSK;
770                 ctx->staging.flags &= ~RXON_FLG_CCK_MSK;
771         }
772 }
773 EXPORT_SYMBOL(iwl_set_flags_for_band);
774
775 /*
776  * initialize rxon structure with default values from eeprom
777  */
778 void iwl_connection_init_rx_config(struct iwl_priv *priv,
779                                    struct iwl_rxon_context *ctx)
780 {
781         const struct iwl_channel_info *ch_info;
782
783         memset(&ctx->staging, 0, sizeof(ctx->staging));
784
785         if (!ctx->vif) {
786                 ctx->staging.dev_type = ctx->unused_devtype;
787         } else switch (ctx->vif->type) {
788         case NL80211_IFTYPE_AP:
789                 ctx->staging.dev_type = ctx->ap_devtype;
790                 break;
791
792         case NL80211_IFTYPE_STATION:
793                 ctx->staging.dev_type = ctx->station_devtype;
794                 ctx->staging.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
795                 break;
796
797         case NL80211_IFTYPE_ADHOC:
798                 ctx->staging.dev_type = ctx->ibss_devtype;
799                 ctx->staging.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
800                 ctx->staging.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
801                                                   RXON_FILTER_ACCEPT_GRP_MSK;
802                 break;
803
804         default:
805                 IWL_ERR(priv, "Unsupported interface type %d\n",
806                         ctx->vif->type);
807                 break;
808         }
809
810 #if 0
811         /* TODO:  Figure out when short_preamble would be set and cache from
812          * that */
813         if (!hw_to_local(priv->hw)->short_preamble)
814                 ctx->staging.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
815         else
816                 ctx->staging.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
817 #endif
818
819         ch_info = iwl_get_channel_info(priv, priv->band,
820                                        le16_to_cpu(ctx->active.channel));
821
822         if (!ch_info)
823                 ch_info = &priv->channel_info[0];
824
825         ctx->staging.channel = cpu_to_le16(ch_info->channel);
826         priv->band = ch_info->band;
827
828         iwl_set_flags_for_band(priv, ctx, priv->band, ctx->vif);
829
830         ctx->staging.ofdm_basic_rates =
831             (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
832         ctx->staging.cck_basic_rates =
833             (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
834
835         /* clear both MIX and PURE40 mode flag */
836         ctx->staging.flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED |
837                                         RXON_FLG_CHANNEL_MODE_PURE_40);
838         if (ctx->vif)
839                 memcpy(ctx->staging.node_addr, ctx->vif->addr, ETH_ALEN);
840
841         ctx->staging.ofdm_ht_single_stream_basic_rates = 0xff;
842         ctx->staging.ofdm_ht_dual_stream_basic_rates = 0xff;
843         ctx->staging.ofdm_ht_triple_stream_basic_rates = 0xff;
844 }
845 EXPORT_SYMBOL(iwl_connection_init_rx_config);
846
847 void iwl_set_rate(struct iwl_priv *priv)
848 {
849         const struct ieee80211_supported_band *hw = NULL;
850         struct ieee80211_rate *rate;
851         struct iwl_rxon_context *ctx;
852         int i;
853
854         hw = iwl_get_hw_mode(priv, priv->band);
855         if (!hw) {
856                 IWL_ERR(priv, "Failed to set rate: unable to get hw mode\n");
857                 return;
858         }
859
860         priv->active_rate = 0;
861
862         for (i = 0; i < hw->n_bitrates; i++) {
863                 rate = &(hw->bitrates[i]);
864                 if (rate->hw_value < IWL_RATE_COUNT_LEGACY)
865                         priv->active_rate |= (1 << rate->hw_value);
866         }
867
868         IWL_DEBUG_RATE(priv, "Set active_rate = %0x\n", priv->active_rate);
869
870         for_each_context(priv, ctx) {
871                 ctx->staging.cck_basic_rates =
872                     (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
873
874                 ctx->staging.ofdm_basic_rates =
875                    (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
876         }
877 }
878 EXPORT_SYMBOL(iwl_set_rate);
879
880 void iwl_chswitch_done(struct iwl_priv *priv, bool is_success)
881 {
882         /*
883          * MULTI-FIXME
884          * See iwl_mac_channel_switch.
885          */
886         struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
887
888         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
889                 return;
890
891         if (priv->switch_rxon.switch_in_progress) {
892                 ieee80211_chswitch_done(ctx->vif, is_success);
893                 mutex_lock(&priv->mutex);
894                 priv->switch_rxon.switch_in_progress = false;
895                 mutex_unlock(&priv->mutex);
896         }
897 }
898 EXPORT_SYMBOL(iwl_chswitch_done);
899
900 void iwl_rx_csa(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb)
901 {
902         struct iwl_rx_packet *pkt = rxb_addr(rxb);
903         struct iwl_csa_notification *csa = &(pkt->u.csa_notif);
904         /*
905          * MULTI-FIXME
906          * See iwl_mac_channel_switch.
907          */
908         struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
909         struct iwl_rxon_cmd *rxon = (void *)&ctx->active;
910
911         if (priv->switch_rxon.switch_in_progress) {
912                 if (!le32_to_cpu(csa->status) &&
913                     (csa->channel == priv->switch_rxon.channel)) {
914                         rxon->channel = csa->channel;
915                         ctx->staging.channel = csa->channel;
916                         IWL_DEBUG_11H(priv, "CSA notif: channel %d\n",
917                               le16_to_cpu(csa->channel));
918                         iwl_chswitch_done(priv, true);
919                 } else {
920                         IWL_ERR(priv, "CSA notif (fail) : channel %d\n",
921                               le16_to_cpu(csa->channel));
922                         iwl_chswitch_done(priv, false);
923                 }
924         }
925 }
926 EXPORT_SYMBOL(iwl_rx_csa);
927
928 #ifdef CONFIG_IWLWIFI_DEBUG
929 void iwl_print_rx_config_cmd(struct iwl_priv *priv,
930                              struct iwl_rxon_context *ctx)
931 {
932         struct iwl_rxon_cmd *rxon = &ctx->staging;
933
934         IWL_DEBUG_RADIO(priv, "RX CONFIG:\n");
935         iwl_print_hex_dump(priv, IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
936         IWL_DEBUG_RADIO(priv, "u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
937         IWL_DEBUG_RADIO(priv, "u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
938         IWL_DEBUG_RADIO(priv, "u32 filter_flags: 0x%08x\n",
939                         le32_to_cpu(rxon->filter_flags));
940         IWL_DEBUG_RADIO(priv, "u8 dev_type: 0x%x\n", rxon->dev_type);
941         IWL_DEBUG_RADIO(priv, "u8 ofdm_basic_rates: 0x%02x\n",
942                         rxon->ofdm_basic_rates);
943         IWL_DEBUG_RADIO(priv, "u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
944         IWL_DEBUG_RADIO(priv, "u8[6] node_addr: %pM\n", rxon->node_addr);
945         IWL_DEBUG_RADIO(priv, "u8[6] bssid_addr: %pM\n", rxon->bssid_addr);
946         IWL_DEBUG_RADIO(priv, "u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
947 }
948 EXPORT_SYMBOL(iwl_print_rx_config_cmd);
949 #endif
950 /**
951  * iwl_irq_handle_error - called for HW or SW error interrupt from card
952  */
953 void iwl_irq_handle_error(struct iwl_priv *priv)
954 {
955         /* Set the FW error flag -- cleared on iwl_down */
956         set_bit(STATUS_FW_ERROR, &priv->status);
957
958         /* Cancel currently queued command. */
959         clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
960
961         /* W/A for WiFi/WiMAX coex and WiMAX own the RF */
962         if (priv->cfg->internal_wimax_coex &&
963             (!(iwl_read_prph(priv, APMG_CLK_CTRL_REG) &
964                         APMS_CLK_VAL_MRB_FUNC_MODE) ||
965              (iwl_read_prph(priv, APMG_PS_CTRL_REG) &
966                         APMG_PS_CTRL_VAL_RESET_REQ))) {
967                 wake_up_interruptible(&priv->wait_command_queue);
968                 /*
969                  *Keep the restart process from trying to send host
970                  * commands by clearing the INIT status bit
971                  */
972                 clear_bit(STATUS_READY, &priv->status);
973                 IWL_ERR(priv, "RF is used by WiMAX\n");
974                 return;
975         }
976
977         IWL_ERR(priv, "Loaded firmware version: %s\n",
978                 priv->hw->wiphy->fw_version);
979
980         priv->cfg->ops->lib->dump_nic_error_log(priv);
981         if (priv->cfg->ops->lib->dump_csr)
982                 priv->cfg->ops->lib->dump_csr(priv);
983         if (priv->cfg->ops->lib->dump_fh)
984                 priv->cfg->ops->lib->dump_fh(priv, NULL, false);
985         priv->cfg->ops->lib->dump_nic_event_log(priv, false, NULL, false);
986 #ifdef CONFIG_IWLWIFI_DEBUG
987         if (iwl_get_debug_level(priv) & IWL_DL_FW_ERRORS)
988                 iwl_print_rx_config_cmd(priv,
989                                         &priv->contexts[IWL_RXON_CTX_BSS]);
990 #endif
991
992         wake_up_interruptible(&priv->wait_command_queue);
993
994         /* Keep the restart process from trying to send host
995          * commands by clearing the INIT status bit */
996         clear_bit(STATUS_READY, &priv->status);
997
998         if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
999                 IWL_DEBUG(priv, IWL_DL_FW_ERRORS,
1000                           "Restarting adapter due to uCode error.\n");
1001
1002                 if (priv->cfg->mod_params->restart_fw)
1003                         queue_work(priv->workqueue, &priv->restart);
1004         }
1005 }
1006 EXPORT_SYMBOL(iwl_irq_handle_error);
1007
1008 static int iwl_apm_stop_master(struct iwl_priv *priv)
1009 {
1010         int ret = 0;
1011
1012         /* stop device's busmaster DMA activity */
1013         iwl_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_STOP_MASTER);
1014
1015         ret = iwl_poll_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_MASTER_DISABLED,
1016                         CSR_RESET_REG_FLAG_MASTER_DISABLED, 100);
1017         if (ret)
1018                 IWL_WARN(priv, "Master Disable Timed Out, 100 usec\n");
1019
1020         IWL_DEBUG_INFO(priv, "stop master\n");
1021
1022         return ret;
1023 }
1024
1025 void iwl_apm_stop(struct iwl_priv *priv)
1026 {
1027         IWL_DEBUG_INFO(priv, "Stop card, put in low power state\n");
1028
1029         /* Stop device's DMA activity */
1030         iwl_apm_stop_master(priv);
1031
1032         /* Reset the entire device */
1033         iwl_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
1034
1035         udelay(10);
1036
1037         /*
1038          * Clear "initialization complete" bit to move adapter from
1039          * D0A* (powered-up Active) --> D0U* (Uninitialized) state.
1040          */
1041         iwl_clear_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
1042 }
1043 EXPORT_SYMBOL(iwl_apm_stop);
1044
1045
1046 /*
1047  * Start up NIC's basic functionality after it has been reset
1048  * (e.g. after platform boot, or shutdown via iwl_apm_stop())
1049  * NOTE:  This does not load uCode nor start the embedded processor
1050  */
1051 int iwl_apm_init(struct iwl_priv *priv)
1052 {
1053         int ret = 0;
1054         u16 lctl;
1055
1056         IWL_DEBUG_INFO(priv, "Init card's basic functions\n");
1057
1058         /*
1059          * Use "set_bit" below rather than "write", to preserve any hardware
1060          * bits already set by default after reset.
1061          */
1062
1063         /* Disable L0S exit timer (platform NMI Work/Around) */
1064         iwl_set_bit(priv, CSR_GIO_CHICKEN_BITS,
1065                           CSR_GIO_CHICKEN_BITS_REG_BIT_DIS_L0S_EXIT_TIMER);
1066
1067         /*
1068          * Disable L0s without affecting L1;
1069          *  don't wait for ICH L0s (ICH bug W/A)
1070          */
1071         iwl_set_bit(priv, CSR_GIO_CHICKEN_BITS,
1072                           CSR_GIO_CHICKEN_BITS_REG_BIT_L1A_NO_L0S_RX);
1073
1074         /* Set FH wait threshold to maximum (HW error during stress W/A) */
1075         iwl_set_bit(priv, CSR_DBG_HPET_MEM_REG, CSR_DBG_HPET_MEM_REG_VAL);
1076
1077         /*
1078          * Enable HAP INTA (interrupt from management bus) to
1079          * wake device's PCI Express link L1a -> L0s
1080          * NOTE:  This is no-op for 3945 (non-existant bit)
1081          */
1082         iwl_set_bit(priv, CSR_HW_IF_CONFIG_REG,
1083                                     CSR_HW_IF_CONFIG_REG_BIT_HAP_WAKE_L1A);
1084
1085         /*
1086          * HW bug W/A for instability in PCIe bus L0->L0S->L1 transition.
1087          * Check if BIOS (or OS) enabled L1-ASPM on this device.
1088          * If so (likely), disable L0S, so device moves directly L0->L1;
1089          *    costs negligible amount of power savings.
1090          * If not (unlikely), enable L0S, so there is at least some
1091          *    power savings, even without L1.
1092          */
1093         if (priv->cfg->base_params->set_l0s) {
1094                 lctl = iwl_pcie_link_ctl(priv);
1095                 if ((lctl & PCI_CFG_LINK_CTRL_VAL_L1_EN) ==
1096                                         PCI_CFG_LINK_CTRL_VAL_L1_EN) {
1097                         /* L1-ASPM enabled; disable(!) L0S  */
1098                         iwl_set_bit(priv, CSR_GIO_REG,
1099                                         CSR_GIO_REG_VAL_L0S_ENABLED);
1100                         IWL_DEBUG_POWER(priv, "L1 Enabled; Disabling L0S\n");
1101                 } else {
1102                         /* L1-ASPM disabled; enable(!) L0S */
1103                         iwl_clear_bit(priv, CSR_GIO_REG,
1104                                         CSR_GIO_REG_VAL_L0S_ENABLED);
1105                         IWL_DEBUG_POWER(priv, "L1 Disabled; Enabling L0S\n");
1106                 }
1107         }
1108
1109         /* Configure analog phase-lock-loop before activating to D0A */
1110         if (priv->cfg->base_params->pll_cfg_val)
1111                 iwl_set_bit(priv, CSR_ANA_PLL_CFG,
1112                             priv->cfg->base_params->pll_cfg_val);
1113
1114         /*
1115          * Set "initialization complete" bit to move adapter from
1116          * D0U* --> D0A* (powered-up active) state.
1117          */
1118         iwl_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
1119
1120         /*
1121          * Wait for clock stabilization; once stabilized, access to
1122          * device-internal resources is supported, e.g. iwl_write_prph()
1123          * and accesses to uCode SRAM.
1124          */
1125         ret = iwl_poll_bit(priv, CSR_GP_CNTRL,
1126                         CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
1127                         CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25000);
1128         if (ret < 0) {
1129                 IWL_DEBUG_INFO(priv, "Failed to init the card\n");
1130                 goto out;
1131         }
1132
1133         /*
1134          * Enable DMA and BSM (if used) clocks, wait for them to stabilize.
1135          * BSM (Boostrap State Machine) is only in 3945 and 4965;
1136          * later devices (i.e. 5000 and later) have non-volatile SRAM,
1137          * and don't need BSM to restore data after power-saving sleep.
1138          *
1139          * Write to "CLK_EN_REG"; "1" bits enable clocks, while "0" bits
1140          * do not disable clocks.  This preserves any hardware bits already
1141          * set by default in "CLK_CTRL_REG" after reset.
1142          */
1143         if (priv->cfg->base_params->use_bsm)
1144                 iwl_write_prph(priv, APMG_CLK_EN_REG,
1145                         APMG_CLK_VAL_DMA_CLK_RQT | APMG_CLK_VAL_BSM_CLK_RQT);
1146         else
1147                 iwl_write_prph(priv, APMG_CLK_EN_REG,
1148                         APMG_CLK_VAL_DMA_CLK_RQT);
1149         udelay(20);
1150
1151         /* Disable L1-Active */
1152         iwl_set_bits_prph(priv, APMG_PCIDEV_STT_REG,
1153                           APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
1154
1155 out:
1156         return ret;
1157 }
1158 EXPORT_SYMBOL(iwl_apm_init);
1159
1160
1161 int iwl_set_tx_power(struct iwl_priv *priv, s8 tx_power, bool force)
1162 {
1163         int ret;
1164         s8 prev_tx_power;
1165
1166         lockdep_assert_held(&priv->mutex);
1167
1168         if (priv->tx_power_user_lmt == tx_power && !force)
1169                 return 0;
1170
1171         if (!priv->cfg->ops->lib->send_tx_power)
1172                 return -EOPNOTSUPP;
1173
1174         if (tx_power < IWLAGN_TX_POWER_TARGET_POWER_MIN) {
1175                 IWL_WARN(priv,
1176                          "Requested user TXPOWER %d below lower limit %d.\n",
1177                          tx_power,
1178                          IWLAGN_TX_POWER_TARGET_POWER_MIN);
1179                 return -EINVAL;
1180         }
1181
1182         if (tx_power > priv->tx_power_device_lmt) {
1183                 IWL_WARN(priv,
1184                         "Requested user TXPOWER %d above upper limit %d.\n",
1185                          tx_power, priv->tx_power_device_lmt);
1186                 return -EINVAL;
1187         }
1188
1189         if (!iwl_is_ready_rf(priv))
1190                 return -EIO;
1191
1192         /* scan complete use tx_power_next, need to be updated */
1193         priv->tx_power_next = tx_power;
1194         if (test_bit(STATUS_SCANNING, &priv->status) && !force) {
1195                 IWL_DEBUG_INFO(priv, "Deferring tx power set while scanning\n");
1196                 return 0;
1197         }
1198
1199         prev_tx_power = priv->tx_power_user_lmt;
1200         priv->tx_power_user_lmt = tx_power;
1201
1202         ret = priv->cfg->ops->lib->send_tx_power(priv);
1203
1204         /* if fail to set tx_power, restore the orig. tx power */
1205         if (ret) {
1206                 priv->tx_power_user_lmt = prev_tx_power;
1207                 priv->tx_power_next = prev_tx_power;
1208         }
1209         return ret;
1210 }
1211 EXPORT_SYMBOL(iwl_set_tx_power);
1212
1213 void iwl_send_bt_config(struct iwl_priv *priv)
1214 {
1215         struct iwl_bt_cmd bt_cmd = {
1216                 .lead_time = BT_LEAD_TIME_DEF,
1217                 .max_kill = BT_MAX_KILL_DEF,
1218                 .kill_ack_mask = 0,
1219                 .kill_cts_mask = 0,
1220         };
1221
1222         if (!bt_coex_active)
1223                 bt_cmd.flags = BT_COEX_DISABLE;
1224         else
1225                 bt_cmd.flags = BT_COEX_ENABLE;
1226
1227         priv->bt_enable_flag = bt_cmd.flags;
1228         IWL_DEBUG_INFO(priv, "BT coex %s\n",
1229                 (bt_cmd.flags == BT_COEX_DISABLE) ? "disable" : "active");
1230
1231         if (iwl_send_cmd_pdu(priv, REPLY_BT_CONFIG,
1232                              sizeof(struct iwl_bt_cmd), &bt_cmd))
1233                 IWL_ERR(priv, "failed to send BT Coex Config\n");
1234 }
1235 EXPORT_SYMBOL(iwl_send_bt_config);
1236
1237 int iwl_send_statistics_request(struct iwl_priv *priv, u8 flags, bool clear)
1238 {
1239         struct iwl_statistics_cmd statistics_cmd = {
1240                 .configuration_flags =
1241                         clear ? IWL_STATS_CONF_CLEAR_STATS : 0,
1242         };
1243
1244         if (flags & CMD_ASYNC)
1245                 return iwl_send_cmd_pdu_async(priv, REPLY_STATISTICS_CMD,
1246                                                sizeof(struct iwl_statistics_cmd),
1247                                                &statistics_cmd, NULL);
1248         else
1249                 return iwl_send_cmd_pdu(priv, REPLY_STATISTICS_CMD,
1250                                         sizeof(struct iwl_statistics_cmd),
1251                                         &statistics_cmd);
1252 }
1253 EXPORT_SYMBOL(iwl_send_statistics_request);
1254
1255 void iwl_rx_pm_sleep_notif(struct iwl_priv *priv,
1256                            struct iwl_rx_mem_buffer *rxb)
1257 {
1258 #ifdef CONFIG_IWLWIFI_DEBUG
1259         struct iwl_rx_packet *pkt = rxb_addr(rxb);
1260         struct iwl_sleep_notification *sleep = &(pkt->u.sleep_notif);
1261         IWL_DEBUG_RX(priv, "sleep mode: %d, src: %d\n",
1262                      sleep->pm_sleep_mode, sleep->pm_wakeup_src);
1263 #endif
1264 }
1265 EXPORT_SYMBOL(iwl_rx_pm_sleep_notif);
1266
1267 void iwl_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
1268                                       struct iwl_rx_mem_buffer *rxb)
1269 {
1270         struct iwl_rx_packet *pkt = rxb_addr(rxb);
1271         u32 len = le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
1272         IWL_DEBUG_RADIO(priv, "Dumping %d bytes of unhandled "
1273                         "notification for %s:\n", len,
1274                         get_cmd_string(pkt->hdr.cmd));
1275         iwl_print_hex_dump(priv, IWL_DL_RADIO, pkt->u.raw, len);
1276 }
1277 EXPORT_SYMBOL(iwl_rx_pm_debug_statistics_notif);
1278
1279 void iwl_rx_reply_error(struct iwl_priv *priv,
1280                         struct iwl_rx_mem_buffer *rxb)
1281 {
1282         struct iwl_rx_packet *pkt = rxb_addr(rxb);
1283
1284         IWL_ERR(priv, "Error Reply type 0x%08X cmd %s (0x%02X) "
1285                 "seq 0x%04X ser 0x%08X\n",
1286                 le32_to_cpu(pkt->u.err_resp.error_type),
1287                 get_cmd_string(pkt->u.err_resp.cmd_id),
1288                 pkt->u.err_resp.cmd_id,
1289                 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
1290                 le32_to_cpu(pkt->u.err_resp.error_info));
1291 }
1292 EXPORT_SYMBOL(iwl_rx_reply_error);
1293
1294 void iwl_clear_isr_stats(struct iwl_priv *priv)
1295 {
1296         memset(&priv->isr_stats, 0, sizeof(priv->isr_stats));
1297 }
1298
1299 int iwl_mac_conf_tx(struct ieee80211_hw *hw, u16 queue,
1300                            const struct ieee80211_tx_queue_params *params)
1301 {
1302         struct iwl_priv *priv = hw->priv;
1303         struct iwl_rxon_context *ctx;
1304         unsigned long flags;
1305         int q;
1306
1307         IWL_DEBUG_MAC80211(priv, "enter\n");
1308
1309         if (!iwl_is_ready_rf(priv)) {
1310                 IWL_DEBUG_MAC80211(priv, "leave - RF not ready\n");
1311                 return -EIO;
1312         }
1313
1314         if (queue >= AC_NUM) {
1315                 IWL_DEBUG_MAC80211(priv, "leave - queue >= AC_NUM %d\n", queue);
1316                 return 0;
1317         }
1318
1319         q = AC_NUM - 1 - queue;
1320
1321         spin_lock_irqsave(&priv->lock, flags);
1322
1323         /*
1324          * MULTI-FIXME
1325          * This may need to be done per interface in nl80211/cfg80211/mac80211.
1326          */
1327         for_each_context(priv, ctx) {
1328                 ctx->qos_data.def_qos_parm.ac[q].cw_min =
1329                         cpu_to_le16(params->cw_min);
1330                 ctx->qos_data.def_qos_parm.ac[q].cw_max =
1331                         cpu_to_le16(params->cw_max);
1332                 ctx->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
1333                 ctx->qos_data.def_qos_parm.ac[q].edca_txop =
1334                                 cpu_to_le16((params->txop * 32));
1335
1336                 ctx->qos_data.def_qos_parm.ac[q].reserved1 = 0;
1337         }
1338
1339         spin_unlock_irqrestore(&priv->lock, flags);
1340
1341         IWL_DEBUG_MAC80211(priv, "leave\n");
1342         return 0;
1343 }
1344 EXPORT_SYMBOL(iwl_mac_conf_tx);
1345
1346 int iwl_mac_tx_last_beacon(struct ieee80211_hw *hw)
1347 {
1348         struct iwl_priv *priv = hw->priv;
1349
1350         return priv->ibss_manager == IWL_IBSS_MANAGER;
1351 }
1352 EXPORT_SYMBOL_GPL(iwl_mac_tx_last_beacon);
1353
1354 static int iwl_set_mode(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
1355 {
1356         iwl_connection_init_rx_config(priv, ctx);
1357
1358         if (priv->cfg->ops->hcmd->set_rxon_chain)
1359                 priv->cfg->ops->hcmd->set_rxon_chain(priv, ctx);
1360
1361         return iwlcore_commit_rxon(priv, ctx);
1362 }
1363
1364 static int iwl_setup_interface(struct iwl_priv *priv,
1365                                struct iwl_rxon_context *ctx)
1366 {
1367         struct ieee80211_vif *vif = ctx->vif;
1368         int err;
1369
1370         lockdep_assert_held(&priv->mutex);
1371
1372         /*
1373          * This variable will be correct only when there's just
1374          * a single context, but all code using it is for hardware
1375          * that supports only one context.
1376          */
1377         priv->iw_mode = vif->type;
1378
1379         ctx->is_active = true;
1380
1381         err = iwl_set_mode(priv, ctx);
1382         if (err) {
1383                 if (!ctx->always_active)
1384                         ctx->is_active = false;
1385                 return err;
1386         }
1387
1388         if (priv->cfg->bt_params && priv->cfg->bt_params->advanced_bt_coexist &&
1389             vif->type == NL80211_IFTYPE_ADHOC) {
1390                 /*
1391                  * pretend to have high BT traffic as long as we
1392                  * are operating in IBSS mode, as this will cause
1393                  * the rate scaling etc. to behave as intended.
1394                  */
1395                 priv->bt_traffic_load = IWL_BT_COEX_TRAFFIC_LOAD_HIGH;
1396         }
1397
1398         return 0;
1399 }
1400
1401 int iwl_mac_add_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1402 {
1403         struct iwl_priv *priv = hw->priv;
1404         struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
1405         struct iwl_rxon_context *tmp, *ctx = NULL;
1406         int err;
1407         enum nl80211_iftype viftype = ieee80211_vif_type_p2p(vif);
1408
1409         IWL_DEBUG_MAC80211(priv, "enter: type %d, addr %pM\n",
1410                            viftype, vif->addr);
1411
1412         mutex_lock(&priv->mutex);
1413
1414         if (!iwl_is_ready_rf(priv)) {
1415                 IWL_WARN(priv, "Try to add interface when device not ready\n");
1416                 err = -EINVAL;
1417                 goto out;
1418         }
1419
1420         for_each_context(priv, tmp) {
1421                 u32 possible_modes =
1422                         tmp->interface_modes | tmp->exclusive_interface_modes;
1423
1424                 if (tmp->vif) {
1425                         /* check if this busy context is exclusive */
1426                         if (tmp->exclusive_interface_modes &
1427                                                 BIT(tmp->vif->type)) {
1428                                 err = -EINVAL;
1429                                 goto out;
1430                         }
1431                         continue;
1432                 }
1433
1434                 if (!(possible_modes & BIT(viftype)))
1435                         continue;
1436
1437                 /* have maybe usable context w/o interface */
1438                 ctx = tmp;
1439                 break;
1440         }
1441
1442         if (!ctx) {
1443                 err = -EOPNOTSUPP;
1444                 goto out;
1445         }
1446
1447         vif_priv->ctx = ctx;
1448         ctx->vif = vif;
1449
1450         err = iwl_setup_interface(priv, ctx);
1451         if (!err)
1452                 goto out;
1453
1454         ctx->vif = NULL;
1455         priv->iw_mode = NL80211_IFTYPE_STATION;
1456  out:
1457         mutex_unlock(&priv->mutex);
1458
1459         IWL_DEBUG_MAC80211(priv, "leave\n");
1460         return err;
1461 }
1462 EXPORT_SYMBOL(iwl_mac_add_interface);
1463
1464 static void iwl_teardown_interface(struct iwl_priv *priv,
1465                                    struct ieee80211_vif *vif,
1466                                    bool mode_change)
1467 {
1468         struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
1469
1470         lockdep_assert_held(&priv->mutex);
1471
1472         if (priv->scan_vif == vif) {
1473                 iwl_scan_cancel_timeout(priv, 200);
1474                 iwl_force_scan_end(priv);
1475         }
1476
1477         if (!mode_change) {
1478                 iwl_set_mode(priv, ctx);
1479                 if (!ctx->always_active)
1480                         ctx->is_active = false;
1481         }
1482
1483         /*
1484          * When removing the IBSS interface, overwrite the
1485          * BT traffic load with the stored one from the last
1486          * notification, if any. If this is a device that
1487          * doesn't implement this, this has no effect since
1488          * both values are the same and zero.
1489          */
1490         if (vif->type == NL80211_IFTYPE_ADHOC)
1491                 priv->bt_traffic_load = priv->last_bt_traffic_load;
1492 }
1493
1494 void iwl_mac_remove_interface(struct ieee80211_hw *hw,
1495                               struct ieee80211_vif *vif)
1496 {
1497         struct iwl_priv *priv = hw->priv;
1498         struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
1499
1500         IWL_DEBUG_MAC80211(priv, "enter\n");
1501
1502         mutex_lock(&priv->mutex);
1503
1504         WARN_ON(ctx->vif != vif);
1505         ctx->vif = NULL;
1506
1507         iwl_teardown_interface(priv, vif, false);
1508
1509         memset(priv->bssid, 0, ETH_ALEN);
1510         mutex_unlock(&priv->mutex);
1511
1512         IWL_DEBUG_MAC80211(priv, "leave\n");
1513
1514 }
1515 EXPORT_SYMBOL(iwl_mac_remove_interface);
1516
1517 int iwl_alloc_txq_mem(struct iwl_priv *priv)
1518 {
1519         if (!priv->txq)
1520                 priv->txq = kzalloc(
1521                         sizeof(struct iwl_tx_queue) *
1522                                 priv->cfg->base_params->num_of_queues,
1523                         GFP_KERNEL);
1524         if (!priv->txq) {
1525                 IWL_ERR(priv, "Not enough memory for txq\n");
1526                 return -ENOMEM;
1527         }
1528         return 0;
1529 }
1530 EXPORT_SYMBOL(iwl_alloc_txq_mem);
1531
1532 void iwl_free_txq_mem(struct iwl_priv *priv)
1533 {
1534         kfree(priv->txq);
1535         priv->txq = NULL;
1536 }
1537 EXPORT_SYMBOL(iwl_free_txq_mem);
1538
1539 #ifdef CONFIG_IWLWIFI_DEBUGFS
1540
1541 #define IWL_TRAFFIC_DUMP_SIZE   (IWL_TRAFFIC_ENTRY_SIZE * IWL_TRAFFIC_ENTRIES)
1542
1543 void iwl_reset_traffic_log(struct iwl_priv *priv)
1544 {
1545         priv->tx_traffic_idx = 0;
1546         priv->rx_traffic_idx = 0;
1547         if (priv->tx_traffic)
1548                 memset(priv->tx_traffic, 0, IWL_TRAFFIC_DUMP_SIZE);
1549         if (priv->rx_traffic)
1550                 memset(priv->rx_traffic, 0, IWL_TRAFFIC_DUMP_SIZE);
1551 }
1552
1553 int iwl_alloc_traffic_mem(struct iwl_priv *priv)
1554 {
1555         u32 traffic_size = IWL_TRAFFIC_DUMP_SIZE;
1556
1557         if (iwl_debug_level & IWL_DL_TX) {
1558                 if (!priv->tx_traffic) {
1559                         priv->tx_traffic =
1560                                 kzalloc(traffic_size, GFP_KERNEL);
1561                         if (!priv->tx_traffic)
1562                                 return -ENOMEM;
1563                 }
1564         }
1565         if (iwl_debug_level & IWL_DL_RX) {
1566                 if (!priv->rx_traffic) {
1567                         priv->rx_traffic =
1568                                 kzalloc(traffic_size, GFP_KERNEL);
1569                         if (!priv->rx_traffic)
1570                                 return -ENOMEM;
1571                 }
1572         }
1573         iwl_reset_traffic_log(priv);
1574         return 0;
1575 }
1576 EXPORT_SYMBOL(iwl_alloc_traffic_mem);
1577
1578 void iwl_free_traffic_mem(struct iwl_priv *priv)
1579 {
1580         kfree(priv->tx_traffic);
1581         priv->tx_traffic = NULL;
1582
1583         kfree(priv->rx_traffic);
1584         priv->rx_traffic = NULL;
1585 }
1586 EXPORT_SYMBOL(iwl_free_traffic_mem);
1587
1588 void iwl_dbg_log_tx_data_frame(struct iwl_priv *priv,
1589                       u16 length, struct ieee80211_hdr *header)
1590 {
1591         __le16 fc;
1592         u16 len;
1593
1594         if (likely(!(iwl_debug_level & IWL_DL_TX)))
1595                 return;
1596
1597         if (!priv->tx_traffic)
1598                 return;
1599
1600         fc = header->frame_control;
1601         if (ieee80211_is_data(fc)) {
1602                 len = (length > IWL_TRAFFIC_ENTRY_SIZE)
1603                        ? IWL_TRAFFIC_ENTRY_SIZE : length;
1604                 memcpy((priv->tx_traffic +
1605                        (priv->tx_traffic_idx * IWL_TRAFFIC_ENTRY_SIZE)),
1606                        header, len);
1607                 priv->tx_traffic_idx =
1608                         (priv->tx_traffic_idx + 1) % IWL_TRAFFIC_ENTRIES;
1609         }
1610 }
1611 EXPORT_SYMBOL(iwl_dbg_log_tx_data_frame);
1612
1613 void iwl_dbg_log_rx_data_frame(struct iwl_priv *priv,
1614                       u16 length, struct ieee80211_hdr *header)
1615 {
1616         __le16 fc;
1617         u16 len;
1618
1619         if (likely(!(iwl_debug_level & IWL_DL_RX)))
1620                 return;
1621
1622         if (!priv->rx_traffic)
1623                 return;
1624
1625         fc = header->frame_control;
1626         if (ieee80211_is_data(fc)) {
1627                 len = (length > IWL_TRAFFIC_ENTRY_SIZE)
1628                        ? IWL_TRAFFIC_ENTRY_SIZE : length;
1629                 memcpy((priv->rx_traffic +
1630                        (priv->rx_traffic_idx * IWL_TRAFFIC_ENTRY_SIZE)),
1631                        header, len);
1632                 priv->rx_traffic_idx =
1633                         (priv->rx_traffic_idx + 1) % IWL_TRAFFIC_ENTRIES;
1634         }
1635 }
1636 EXPORT_SYMBOL(iwl_dbg_log_rx_data_frame);
1637
1638 const char *get_mgmt_string(int cmd)
1639 {
1640         switch (cmd) {
1641                 IWL_CMD(MANAGEMENT_ASSOC_REQ);
1642                 IWL_CMD(MANAGEMENT_ASSOC_RESP);
1643                 IWL_CMD(MANAGEMENT_REASSOC_REQ);
1644                 IWL_CMD(MANAGEMENT_REASSOC_RESP);
1645                 IWL_CMD(MANAGEMENT_PROBE_REQ);
1646                 IWL_CMD(MANAGEMENT_PROBE_RESP);
1647                 IWL_CMD(MANAGEMENT_BEACON);
1648                 IWL_CMD(MANAGEMENT_ATIM);
1649                 IWL_CMD(MANAGEMENT_DISASSOC);
1650                 IWL_CMD(MANAGEMENT_AUTH);
1651                 IWL_CMD(MANAGEMENT_DEAUTH);
1652                 IWL_CMD(MANAGEMENT_ACTION);
1653         default:
1654                 return "UNKNOWN";
1655
1656         }
1657 }
1658
1659 const char *get_ctrl_string(int cmd)
1660 {
1661         switch (cmd) {
1662                 IWL_CMD(CONTROL_BACK_REQ);
1663                 IWL_CMD(CONTROL_BACK);
1664                 IWL_CMD(CONTROL_PSPOLL);
1665                 IWL_CMD(CONTROL_RTS);
1666                 IWL_CMD(CONTROL_CTS);
1667                 IWL_CMD(CONTROL_ACK);
1668                 IWL_CMD(CONTROL_CFEND);
1669                 IWL_CMD(CONTROL_CFENDACK);
1670         default:
1671                 return "UNKNOWN";
1672
1673         }
1674 }
1675
1676 void iwl_clear_traffic_stats(struct iwl_priv *priv)
1677 {
1678         memset(&priv->tx_stats, 0, sizeof(struct traffic_stats));
1679         memset(&priv->rx_stats, 0, sizeof(struct traffic_stats));
1680 }
1681
1682 /*
1683  * if CONFIG_IWLWIFI_DEBUGFS defined, iwl_update_stats function will
1684  * record all the MGMT, CTRL and DATA pkt for both TX and Rx pass.
1685  * Use debugFs to display the rx/rx_statistics
1686  * if CONFIG_IWLWIFI_DEBUGFS not being defined, then no MGMT and CTRL
1687  * information will be recorded, but DATA pkt still will be recorded
1688  * for the reason of iwl_led.c need to control the led blinking based on
1689  * number of tx and rx data.
1690  *
1691  */
1692 void iwl_update_stats(struct iwl_priv *priv, bool is_tx, __le16 fc, u16 len)
1693 {
1694         struct traffic_stats    *stats;
1695
1696         if (is_tx)
1697                 stats = &priv->tx_stats;
1698         else
1699                 stats = &priv->rx_stats;
1700
1701         if (ieee80211_is_mgmt(fc)) {
1702                 switch (fc & cpu_to_le16(IEEE80211_FCTL_STYPE)) {
1703                 case cpu_to_le16(IEEE80211_STYPE_ASSOC_REQ):
1704                         stats->mgmt[MANAGEMENT_ASSOC_REQ]++;
1705                         break;
1706                 case cpu_to_le16(IEEE80211_STYPE_ASSOC_RESP):
1707                         stats->mgmt[MANAGEMENT_ASSOC_RESP]++;
1708                         break;
1709                 case cpu_to_le16(IEEE80211_STYPE_REASSOC_REQ):
1710                         stats->mgmt[MANAGEMENT_REASSOC_REQ]++;
1711                         break;
1712                 case cpu_to_le16(IEEE80211_STYPE_REASSOC_RESP):
1713                         stats->mgmt[MANAGEMENT_REASSOC_RESP]++;
1714                         break;
1715                 case cpu_to_le16(IEEE80211_STYPE_PROBE_REQ):
1716                         stats->mgmt[MANAGEMENT_PROBE_REQ]++;
1717                         break;
1718                 case cpu_to_le16(IEEE80211_STYPE_PROBE_RESP):
1719                         stats->mgmt[MANAGEMENT_PROBE_RESP]++;
1720                         break;
1721                 case cpu_to_le16(IEEE80211_STYPE_BEACON):
1722                         stats->mgmt[MANAGEMENT_BEACON]++;
1723                         break;
1724                 case cpu_to_le16(IEEE80211_STYPE_ATIM):
1725                         stats->mgmt[MANAGEMENT_ATIM]++;
1726                         break;
1727                 case cpu_to_le16(IEEE80211_STYPE_DISASSOC):
1728                         stats->mgmt[MANAGEMENT_DISASSOC]++;
1729                         break;
1730                 case cpu_to_le16(IEEE80211_STYPE_AUTH):
1731                         stats->mgmt[MANAGEMENT_AUTH]++;
1732                         break;
1733                 case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
1734                         stats->mgmt[MANAGEMENT_DEAUTH]++;
1735                         break;
1736                 case cpu_to_le16(IEEE80211_STYPE_ACTION):
1737                         stats->mgmt[MANAGEMENT_ACTION]++;
1738                         break;
1739                 }
1740         } else if (ieee80211_is_ctl(fc)) {
1741                 switch (fc & cpu_to_le16(IEEE80211_FCTL_STYPE)) {
1742                 case cpu_to_le16(IEEE80211_STYPE_BACK_REQ):
1743                         stats->ctrl[CONTROL_BACK_REQ]++;
1744                         break;
1745                 case cpu_to_le16(IEEE80211_STYPE_BACK):
1746                         stats->ctrl[CONTROL_BACK]++;
1747                         break;
1748                 case cpu_to_le16(IEEE80211_STYPE_PSPOLL):
1749                         stats->ctrl[CONTROL_PSPOLL]++;
1750                         break;
1751                 case cpu_to_le16(IEEE80211_STYPE_RTS):
1752                         stats->ctrl[CONTROL_RTS]++;
1753                         break;
1754                 case cpu_to_le16(IEEE80211_STYPE_CTS):
1755                         stats->ctrl[CONTROL_CTS]++;
1756                         break;
1757                 case cpu_to_le16(IEEE80211_STYPE_ACK):
1758                         stats->ctrl[CONTROL_ACK]++;
1759                         break;
1760                 case cpu_to_le16(IEEE80211_STYPE_CFEND):
1761                         stats->ctrl[CONTROL_CFEND]++;
1762                         break;
1763                 case cpu_to_le16(IEEE80211_STYPE_CFENDACK):
1764                         stats->ctrl[CONTROL_CFENDACK]++;
1765                         break;
1766                 }
1767         } else {
1768                 /* data */
1769                 stats->data_cnt++;
1770                 stats->data_bytes += len;
1771         }
1772 }
1773 EXPORT_SYMBOL(iwl_update_stats);
1774 #endif
1775
1776 static void iwl_force_rf_reset(struct iwl_priv *priv)
1777 {
1778         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1779                 return;
1780
1781         if (!iwl_is_any_associated(priv)) {
1782                 IWL_DEBUG_SCAN(priv, "force reset rejected: not associated\n");
1783                 return;
1784         }
1785         /*
1786          * There is no easy and better way to force reset the radio,
1787          * the only known method is switching channel which will force to
1788          * reset and tune the radio.
1789          * Use internal short scan (single channel) operation to should
1790          * achieve this objective.
1791          * Driver should reset the radio when number of consecutive missed
1792          * beacon, or any other uCode error condition detected.
1793          */
1794         IWL_DEBUG_INFO(priv, "perform radio reset.\n");
1795         iwl_internal_short_hw_scan(priv);
1796 }
1797
1798
1799 int iwl_force_reset(struct iwl_priv *priv, int mode, bool external)
1800 {
1801         struct iwl_force_reset *force_reset;
1802
1803         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1804                 return -EINVAL;
1805
1806         if (mode >= IWL_MAX_FORCE_RESET) {
1807                 IWL_DEBUG_INFO(priv, "invalid reset request.\n");
1808                 return -EINVAL;
1809         }
1810         force_reset = &priv->force_reset[mode];
1811         force_reset->reset_request_count++;
1812         if (!external) {
1813                 if (force_reset->last_force_reset_jiffies &&
1814                     time_after(force_reset->last_force_reset_jiffies +
1815                     force_reset->reset_duration, jiffies)) {
1816                         IWL_DEBUG_INFO(priv, "force reset rejected\n");
1817                         force_reset->reset_reject_count++;
1818                         return -EAGAIN;
1819                 }
1820         }
1821         force_reset->reset_success_count++;
1822         force_reset->last_force_reset_jiffies = jiffies;
1823         IWL_DEBUG_INFO(priv, "perform force reset (%d)\n", mode);
1824         switch (mode) {
1825         case IWL_RF_RESET:
1826                 iwl_force_rf_reset(priv);
1827                 break;
1828         case IWL_FW_RESET:
1829                 /*
1830                  * if the request is from external(ex: debugfs),
1831                  * then always perform the request in regardless the module
1832                  * parameter setting
1833                  * if the request is from internal (uCode error or driver
1834                  * detect failure), then fw_restart module parameter
1835                  * need to be check before performing firmware reload
1836                  */
1837                 if (!external && !priv->cfg->mod_params->restart_fw) {
1838                         IWL_DEBUG_INFO(priv, "Cancel firmware reload based on "
1839                                        "module parameter setting\n");
1840                         break;
1841                 }
1842                 IWL_ERR(priv, "On demand firmware reload\n");
1843                 /* Set the FW error flag -- cleared on iwl_down */
1844                 set_bit(STATUS_FW_ERROR, &priv->status);
1845                 wake_up_interruptible(&priv->wait_command_queue);
1846                 /*
1847                  * Keep the restart process from trying to send host
1848                  * commands by clearing the INIT status bit
1849                  */
1850                 clear_bit(STATUS_READY, &priv->status);
1851                 queue_work(priv->workqueue, &priv->restart);
1852                 break;
1853         }
1854         return 0;
1855 }
1856
1857 int iwl_mac_change_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1858                              enum nl80211_iftype newtype, bool newp2p)
1859 {
1860         struct iwl_priv *priv = hw->priv;
1861         struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
1862         struct iwl_rxon_context *tmp;
1863         u32 interface_modes;
1864         int err;
1865
1866         newtype = ieee80211_iftype_p2p(newtype, newp2p);
1867
1868         mutex_lock(&priv->mutex);
1869
1870         interface_modes = ctx->interface_modes | ctx->exclusive_interface_modes;
1871
1872         if (!(interface_modes & BIT(newtype))) {
1873                 err = -EBUSY;
1874                 goto out;
1875         }
1876
1877         if (ctx->exclusive_interface_modes & BIT(newtype)) {
1878                 for_each_context(priv, tmp) {
1879                         if (ctx == tmp)
1880                                 continue;
1881
1882                         if (!tmp->vif)
1883                                 continue;
1884
1885                         /*
1886                          * The current mode switch would be exclusive, but
1887                          * another context is active ... refuse the switch.
1888                          */
1889                         err = -EBUSY;
1890                         goto out;
1891                 }
1892         }
1893
1894         /* success */
1895         iwl_teardown_interface(priv, vif, true);
1896         vif->type = newtype;
1897         err = iwl_setup_interface(priv, ctx);
1898         WARN_ON(err);
1899         /*
1900          * We've switched internally, but submitting to the
1901          * device may have failed for some reason. Mask this
1902          * error, because otherwise mac80211 will not switch
1903          * (and set the interface type back) and we'll be
1904          * out of sync with it.
1905          */
1906         err = 0;
1907
1908  out:
1909         mutex_unlock(&priv->mutex);
1910         return err;
1911 }
1912 EXPORT_SYMBOL(iwl_mac_change_interface);
1913
1914 /*
1915  * On every watchdog tick we check (latest) time stamp. If it does not
1916  * change during timeout period and queue is not empty we reset firmware.
1917  */
1918 static int iwl_check_stuck_queue(struct iwl_priv *priv, int cnt)
1919 {
1920         struct iwl_tx_queue *txq = &priv->txq[cnt];
1921         struct iwl_queue *q = &txq->q;
1922         unsigned long timeout;
1923         int ret;
1924
1925         if (q->read_ptr == q->write_ptr) {
1926                 txq->time_stamp = jiffies;
1927                 return 0;
1928         }
1929
1930         timeout = txq->time_stamp +
1931                   msecs_to_jiffies(priv->cfg->base_params->wd_timeout);
1932
1933         if (time_after(jiffies, timeout)) {
1934                 IWL_ERR(priv, "Queue %d stuck for %u ms.\n",
1935                                 q->id, priv->cfg->base_params->wd_timeout);
1936                 ret = iwl_force_reset(priv, IWL_FW_RESET, false);
1937                 return (ret == -EAGAIN) ? 0 : 1;
1938         }
1939
1940         return 0;
1941 }
1942
1943 /*
1944  * Making watchdog tick be a quarter of timeout assure we will
1945  * discover the queue hung between timeout and 1.25*timeout
1946  */
1947 #define IWL_WD_TICK(timeout) ((timeout) / 4)
1948
1949 /*
1950  * Watchdog timer callback, we check each tx queue for stuck, if if hung
1951  * we reset the firmware. If everything is fine just rearm the timer.
1952  */
1953 void iwl_bg_watchdog(unsigned long data)
1954 {
1955         struct iwl_priv *priv = (struct iwl_priv *)data;
1956         int cnt;
1957         unsigned long timeout;
1958
1959         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1960                 return;
1961
1962         timeout = priv->cfg->base_params->wd_timeout;
1963         if (timeout == 0)
1964                 return;
1965
1966         /* monitor and check for stuck cmd queue */
1967         if (iwl_check_stuck_queue(priv, priv->cmd_queue))
1968                 return;
1969
1970         /* monitor and check for other stuck queues */
1971         if (iwl_is_any_associated(priv)) {
1972                 for (cnt = 0; cnt < priv->hw_params.max_txq_num; cnt++) {
1973                         /* skip as we already checked the command queue */
1974                         if (cnt == priv->cmd_queue)
1975                                 continue;
1976                         if (iwl_check_stuck_queue(priv, cnt))
1977                                 return;
1978                 }
1979         }
1980
1981         mod_timer(&priv->watchdog, jiffies +
1982                   msecs_to_jiffies(IWL_WD_TICK(timeout)));
1983 }
1984 EXPORT_SYMBOL(iwl_bg_watchdog);
1985
1986 void iwl_setup_watchdog(struct iwl_priv *priv)
1987 {
1988         unsigned int timeout = priv->cfg->base_params->wd_timeout;
1989
1990         if (timeout)
1991                 mod_timer(&priv->watchdog,
1992                           jiffies + msecs_to_jiffies(IWL_WD_TICK(timeout)));
1993         else
1994                 del_timer(&priv->watchdog);
1995 }
1996 EXPORT_SYMBOL(iwl_setup_watchdog);
1997
1998 /*
1999  * extended beacon time format
2000  * time in usec will be changed into a 32-bit value in extended:internal format
2001  * the extended part is the beacon counts
2002  * the internal part is the time in usec within one beacon interval
2003  */
2004 u32 iwl_usecs_to_beacons(struct iwl_priv *priv, u32 usec, u32 beacon_interval)
2005 {
2006         u32 quot;
2007         u32 rem;
2008         u32 interval = beacon_interval * TIME_UNIT;
2009
2010         if (!interval || !usec)
2011                 return 0;
2012
2013         quot = (usec / interval) &
2014                 (iwl_beacon_time_mask_high(priv,
2015                 priv->hw_params.beacon_time_tsf_bits) >>
2016                 priv->hw_params.beacon_time_tsf_bits);
2017         rem = (usec % interval) & iwl_beacon_time_mask_low(priv,
2018                                    priv->hw_params.beacon_time_tsf_bits);
2019
2020         return (quot << priv->hw_params.beacon_time_tsf_bits) + rem;
2021 }
2022 EXPORT_SYMBOL(iwl_usecs_to_beacons);
2023
2024 /* base is usually what we get from ucode with each received frame,
2025  * the same as HW timer counter counting down
2026  */
2027 __le32 iwl_add_beacon_time(struct iwl_priv *priv, u32 base,
2028                            u32 addon, u32 beacon_interval)
2029 {
2030         u32 base_low = base & iwl_beacon_time_mask_low(priv,
2031                                         priv->hw_params.beacon_time_tsf_bits);
2032         u32 addon_low = addon & iwl_beacon_time_mask_low(priv,
2033                                         priv->hw_params.beacon_time_tsf_bits);
2034         u32 interval = beacon_interval * TIME_UNIT;
2035         u32 res = (base & iwl_beacon_time_mask_high(priv,
2036                                 priv->hw_params.beacon_time_tsf_bits)) +
2037                                 (addon & iwl_beacon_time_mask_high(priv,
2038                                 priv->hw_params.beacon_time_tsf_bits));
2039
2040         if (base_low > addon_low)
2041                 res += base_low - addon_low;
2042         else if (base_low < addon_low) {
2043                 res += interval + base_low - addon_low;
2044                 res += (1 << priv->hw_params.beacon_time_tsf_bits);
2045         } else
2046                 res += (1 << priv->hw_params.beacon_time_tsf_bits);
2047
2048         return cpu_to_le32(res);
2049 }
2050 EXPORT_SYMBOL(iwl_add_beacon_time);
2051
2052 #ifdef CONFIG_PM
2053
2054 int iwl_pci_suspend(struct device *device)
2055 {
2056         struct pci_dev *pdev = to_pci_dev(device);
2057         struct iwl_priv *priv = pci_get_drvdata(pdev);
2058
2059         /*
2060          * This function is called when system goes into suspend state
2061          * mac80211 will call iwl_mac_stop() from the mac80211 suspend function
2062          * first but since iwl_mac_stop() has no knowledge of who the caller is,
2063          * it will not call apm_ops.stop() to stop the DMA operation.
2064          * Calling apm_ops.stop here to make sure we stop the DMA.
2065          */
2066         iwl_apm_stop(priv);
2067
2068         return 0;
2069 }
2070 EXPORT_SYMBOL(iwl_pci_suspend);
2071
2072 int iwl_pci_resume(struct device *device)
2073 {
2074         struct pci_dev *pdev = to_pci_dev(device);
2075         struct iwl_priv *priv = pci_get_drvdata(pdev);
2076         bool hw_rfkill = false;
2077
2078         /*
2079          * We disable the RETRY_TIMEOUT register (0x41) to keep
2080          * PCI Tx retries from interfering with C3 CPU state.
2081          */
2082         pci_write_config_byte(pdev, PCI_CFG_RETRY_TIMEOUT, 0x00);
2083
2084         iwl_enable_interrupts(priv);
2085
2086         if (!(iwl_read32(priv, CSR_GP_CNTRL) &
2087                                 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
2088                 hw_rfkill = true;
2089
2090         if (hw_rfkill)
2091                 set_bit(STATUS_RF_KILL_HW, &priv->status);
2092         else
2093                 clear_bit(STATUS_RF_KILL_HW, &priv->status);
2094
2095         wiphy_rfkill_set_hw_state(priv->hw->wiphy, hw_rfkill);
2096
2097         return 0;
2098 }
2099 EXPORT_SYMBOL(iwl_pci_resume);
2100
2101 const struct dev_pm_ops iwl_pm_ops = {
2102         .suspend = iwl_pci_suspend,
2103         .resume = iwl_pci_resume,
2104         .freeze = iwl_pci_suspend,
2105         .thaw = iwl_pci_resume,
2106         .poweroff = iwl_pci_suspend,
2107         .restore = iwl_pci_resume,
2108 };
2109 EXPORT_SYMBOL(iwl_pm_ops);
2110
2111 #endif /* CONFIG_PM */