iwlwifi: do not set tx power when channel is changing
[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         bool defer;
1166         struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
1167
1168         lockdep_assert_held(&priv->mutex);
1169
1170         if (priv->tx_power_user_lmt == tx_power && !force)
1171                 return 0;
1172
1173         if (!priv->cfg->ops->lib->send_tx_power)
1174                 return -EOPNOTSUPP;
1175
1176         if (tx_power < IWLAGN_TX_POWER_TARGET_POWER_MIN) {
1177                 IWL_WARN(priv,
1178                          "Requested user TXPOWER %d below lower limit %d.\n",
1179                          tx_power,
1180                          IWLAGN_TX_POWER_TARGET_POWER_MIN);
1181                 return -EINVAL;
1182         }
1183
1184         if (tx_power > priv->tx_power_device_lmt) {
1185                 IWL_WARN(priv,
1186                         "Requested user TXPOWER %d above upper limit %d.\n",
1187                          tx_power, priv->tx_power_device_lmt);
1188                 return -EINVAL;
1189         }
1190
1191         if (!iwl_is_ready_rf(priv))
1192                 return -EIO;
1193
1194         /* scan complete and commit_rxon use tx_power_next value,
1195          * it always need to be updated for newest request */
1196         priv->tx_power_next = tx_power;
1197
1198         /* do not set tx power when scanning or channel changing */
1199         defer = test_bit(STATUS_SCANNING, &priv->status) ||
1200                 memcmp(&ctx->active, &ctx->staging, sizeof(ctx->staging));
1201         if (defer && !force) {
1202                 IWL_DEBUG_INFO(priv, "Deferring tx power set\n");
1203                 return 0;
1204         }
1205
1206         prev_tx_power = priv->tx_power_user_lmt;
1207         priv->tx_power_user_lmt = tx_power;
1208
1209         ret = priv->cfg->ops->lib->send_tx_power(priv);
1210
1211         /* if fail to set tx_power, restore the orig. tx power */
1212         if (ret) {
1213                 priv->tx_power_user_lmt = prev_tx_power;
1214                 priv->tx_power_next = prev_tx_power;
1215         }
1216         return ret;
1217 }
1218 EXPORT_SYMBOL(iwl_set_tx_power);
1219
1220 void iwl_send_bt_config(struct iwl_priv *priv)
1221 {
1222         struct iwl_bt_cmd bt_cmd = {
1223                 .lead_time = BT_LEAD_TIME_DEF,
1224                 .max_kill = BT_MAX_KILL_DEF,
1225                 .kill_ack_mask = 0,
1226                 .kill_cts_mask = 0,
1227         };
1228
1229         if (!bt_coex_active)
1230                 bt_cmd.flags = BT_COEX_DISABLE;
1231         else
1232                 bt_cmd.flags = BT_COEX_ENABLE;
1233
1234         priv->bt_enable_flag = bt_cmd.flags;
1235         IWL_DEBUG_INFO(priv, "BT coex %s\n",
1236                 (bt_cmd.flags == BT_COEX_DISABLE) ? "disable" : "active");
1237
1238         if (iwl_send_cmd_pdu(priv, REPLY_BT_CONFIG,
1239                              sizeof(struct iwl_bt_cmd), &bt_cmd))
1240                 IWL_ERR(priv, "failed to send BT Coex Config\n");
1241 }
1242 EXPORT_SYMBOL(iwl_send_bt_config);
1243
1244 int iwl_send_statistics_request(struct iwl_priv *priv, u8 flags, bool clear)
1245 {
1246         struct iwl_statistics_cmd statistics_cmd = {
1247                 .configuration_flags =
1248                         clear ? IWL_STATS_CONF_CLEAR_STATS : 0,
1249         };
1250
1251         if (flags & CMD_ASYNC)
1252                 return iwl_send_cmd_pdu_async(priv, REPLY_STATISTICS_CMD,
1253                                                sizeof(struct iwl_statistics_cmd),
1254                                                &statistics_cmd, NULL);
1255         else
1256                 return iwl_send_cmd_pdu(priv, REPLY_STATISTICS_CMD,
1257                                         sizeof(struct iwl_statistics_cmd),
1258                                         &statistics_cmd);
1259 }
1260 EXPORT_SYMBOL(iwl_send_statistics_request);
1261
1262 void iwl_rx_pm_sleep_notif(struct iwl_priv *priv,
1263                            struct iwl_rx_mem_buffer *rxb)
1264 {
1265 #ifdef CONFIG_IWLWIFI_DEBUG
1266         struct iwl_rx_packet *pkt = rxb_addr(rxb);
1267         struct iwl_sleep_notification *sleep = &(pkt->u.sleep_notif);
1268         IWL_DEBUG_RX(priv, "sleep mode: %d, src: %d\n",
1269                      sleep->pm_sleep_mode, sleep->pm_wakeup_src);
1270 #endif
1271 }
1272 EXPORT_SYMBOL(iwl_rx_pm_sleep_notif);
1273
1274 void iwl_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
1275                                       struct iwl_rx_mem_buffer *rxb)
1276 {
1277         struct iwl_rx_packet *pkt = rxb_addr(rxb);
1278         u32 len = le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
1279         IWL_DEBUG_RADIO(priv, "Dumping %d bytes of unhandled "
1280                         "notification for %s:\n", len,
1281                         get_cmd_string(pkt->hdr.cmd));
1282         iwl_print_hex_dump(priv, IWL_DL_RADIO, pkt->u.raw, len);
1283 }
1284 EXPORT_SYMBOL(iwl_rx_pm_debug_statistics_notif);
1285
1286 void iwl_rx_reply_error(struct iwl_priv *priv,
1287                         struct iwl_rx_mem_buffer *rxb)
1288 {
1289         struct iwl_rx_packet *pkt = rxb_addr(rxb);
1290
1291         IWL_ERR(priv, "Error Reply type 0x%08X cmd %s (0x%02X) "
1292                 "seq 0x%04X ser 0x%08X\n",
1293                 le32_to_cpu(pkt->u.err_resp.error_type),
1294                 get_cmd_string(pkt->u.err_resp.cmd_id),
1295                 pkt->u.err_resp.cmd_id,
1296                 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
1297                 le32_to_cpu(pkt->u.err_resp.error_info));
1298 }
1299 EXPORT_SYMBOL(iwl_rx_reply_error);
1300
1301 void iwl_clear_isr_stats(struct iwl_priv *priv)
1302 {
1303         memset(&priv->isr_stats, 0, sizeof(priv->isr_stats));
1304 }
1305
1306 int iwl_mac_conf_tx(struct ieee80211_hw *hw, u16 queue,
1307                            const struct ieee80211_tx_queue_params *params)
1308 {
1309         struct iwl_priv *priv = hw->priv;
1310         struct iwl_rxon_context *ctx;
1311         unsigned long flags;
1312         int q;
1313
1314         IWL_DEBUG_MAC80211(priv, "enter\n");
1315
1316         if (!iwl_is_ready_rf(priv)) {
1317                 IWL_DEBUG_MAC80211(priv, "leave - RF not ready\n");
1318                 return -EIO;
1319         }
1320
1321         if (queue >= AC_NUM) {
1322                 IWL_DEBUG_MAC80211(priv, "leave - queue >= AC_NUM %d\n", queue);
1323                 return 0;
1324         }
1325
1326         q = AC_NUM - 1 - queue;
1327
1328         spin_lock_irqsave(&priv->lock, flags);
1329
1330         /*
1331          * MULTI-FIXME
1332          * This may need to be done per interface in nl80211/cfg80211/mac80211.
1333          */
1334         for_each_context(priv, ctx) {
1335                 ctx->qos_data.def_qos_parm.ac[q].cw_min =
1336                         cpu_to_le16(params->cw_min);
1337                 ctx->qos_data.def_qos_parm.ac[q].cw_max =
1338                         cpu_to_le16(params->cw_max);
1339                 ctx->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
1340                 ctx->qos_data.def_qos_parm.ac[q].edca_txop =
1341                                 cpu_to_le16((params->txop * 32));
1342
1343                 ctx->qos_data.def_qos_parm.ac[q].reserved1 = 0;
1344         }
1345
1346         spin_unlock_irqrestore(&priv->lock, flags);
1347
1348         IWL_DEBUG_MAC80211(priv, "leave\n");
1349         return 0;
1350 }
1351 EXPORT_SYMBOL(iwl_mac_conf_tx);
1352
1353 int iwl_mac_tx_last_beacon(struct ieee80211_hw *hw)
1354 {
1355         struct iwl_priv *priv = hw->priv;
1356
1357         return priv->ibss_manager == IWL_IBSS_MANAGER;
1358 }
1359 EXPORT_SYMBOL_GPL(iwl_mac_tx_last_beacon);
1360
1361 static int iwl_set_mode(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
1362 {
1363         iwl_connection_init_rx_config(priv, ctx);
1364
1365         if (priv->cfg->ops->hcmd->set_rxon_chain)
1366                 priv->cfg->ops->hcmd->set_rxon_chain(priv, ctx);
1367
1368         return iwlcore_commit_rxon(priv, ctx);
1369 }
1370
1371 static int iwl_setup_interface(struct iwl_priv *priv,
1372                                struct iwl_rxon_context *ctx)
1373 {
1374         struct ieee80211_vif *vif = ctx->vif;
1375         int err;
1376
1377         lockdep_assert_held(&priv->mutex);
1378
1379         /*
1380          * This variable will be correct only when there's just
1381          * a single context, but all code using it is for hardware
1382          * that supports only one context.
1383          */
1384         priv->iw_mode = vif->type;
1385
1386         ctx->is_active = true;
1387
1388         err = iwl_set_mode(priv, ctx);
1389         if (err) {
1390                 if (!ctx->always_active)
1391                         ctx->is_active = false;
1392                 return err;
1393         }
1394
1395         if (priv->cfg->bt_params && priv->cfg->bt_params->advanced_bt_coexist &&
1396             vif->type == NL80211_IFTYPE_ADHOC) {
1397                 /*
1398                  * pretend to have high BT traffic as long as we
1399                  * are operating in IBSS mode, as this will cause
1400                  * the rate scaling etc. to behave as intended.
1401                  */
1402                 priv->bt_traffic_load = IWL_BT_COEX_TRAFFIC_LOAD_HIGH;
1403         }
1404
1405         return 0;
1406 }
1407
1408 int iwl_mac_add_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1409 {
1410         struct iwl_priv *priv = hw->priv;
1411         struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
1412         struct iwl_rxon_context *tmp, *ctx = NULL;
1413         int err;
1414         enum nl80211_iftype viftype = ieee80211_vif_type_p2p(vif);
1415
1416         IWL_DEBUG_MAC80211(priv, "enter: type %d, addr %pM\n",
1417                            viftype, vif->addr);
1418
1419         mutex_lock(&priv->mutex);
1420
1421         if (!iwl_is_ready_rf(priv)) {
1422                 IWL_WARN(priv, "Try to add interface when device not ready\n");
1423                 err = -EINVAL;
1424                 goto out;
1425         }
1426
1427         for_each_context(priv, tmp) {
1428                 u32 possible_modes =
1429                         tmp->interface_modes | tmp->exclusive_interface_modes;
1430
1431                 if (tmp->vif) {
1432                         /* check if this busy context is exclusive */
1433                         if (tmp->exclusive_interface_modes &
1434                                                 BIT(tmp->vif->type)) {
1435                                 err = -EINVAL;
1436                                 goto out;
1437                         }
1438                         continue;
1439                 }
1440
1441                 if (!(possible_modes & BIT(viftype)))
1442                         continue;
1443
1444                 /* have maybe usable context w/o interface */
1445                 ctx = tmp;
1446                 break;
1447         }
1448
1449         if (!ctx) {
1450                 err = -EOPNOTSUPP;
1451                 goto out;
1452         }
1453
1454         vif_priv->ctx = ctx;
1455         ctx->vif = vif;
1456
1457         err = iwl_setup_interface(priv, ctx);
1458         if (!err)
1459                 goto out;
1460
1461         ctx->vif = NULL;
1462         priv->iw_mode = NL80211_IFTYPE_STATION;
1463  out:
1464         mutex_unlock(&priv->mutex);
1465
1466         IWL_DEBUG_MAC80211(priv, "leave\n");
1467         return err;
1468 }
1469 EXPORT_SYMBOL(iwl_mac_add_interface);
1470
1471 static void iwl_teardown_interface(struct iwl_priv *priv,
1472                                    struct ieee80211_vif *vif,
1473                                    bool mode_change)
1474 {
1475         struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
1476
1477         lockdep_assert_held(&priv->mutex);
1478
1479         if (priv->scan_vif == vif) {
1480                 iwl_scan_cancel_timeout(priv, 200);
1481                 iwl_force_scan_end(priv);
1482         }
1483
1484         if (!mode_change) {
1485                 iwl_set_mode(priv, ctx);
1486                 if (!ctx->always_active)
1487                         ctx->is_active = false;
1488         }
1489
1490         /*
1491          * When removing the IBSS interface, overwrite the
1492          * BT traffic load with the stored one from the last
1493          * notification, if any. If this is a device that
1494          * doesn't implement this, this has no effect since
1495          * both values are the same and zero.
1496          */
1497         if (vif->type == NL80211_IFTYPE_ADHOC)
1498                 priv->bt_traffic_load = priv->last_bt_traffic_load;
1499 }
1500
1501 void iwl_mac_remove_interface(struct ieee80211_hw *hw,
1502                               struct ieee80211_vif *vif)
1503 {
1504         struct iwl_priv *priv = hw->priv;
1505         struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
1506
1507         IWL_DEBUG_MAC80211(priv, "enter\n");
1508
1509         mutex_lock(&priv->mutex);
1510
1511         WARN_ON(ctx->vif != vif);
1512         ctx->vif = NULL;
1513
1514         iwl_teardown_interface(priv, vif, false);
1515
1516         memset(priv->bssid, 0, ETH_ALEN);
1517         mutex_unlock(&priv->mutex);
1518
1519         IWL_DEBUG_MAC80211(priv, "leave\n");
1520
1521 }
1522 EXPORT_SYMBOL(iwl_mac_remove_interface);
1523
1524 int iwl_alloc_txq_mem(struct iwl_priv *priv)
1525 {
1526         if (!priv->txq)
1527                 priv->txq = kzalloc(
1528                         sizeof(struct iwl_tx_queue) *
1529                                 priv->cfg->base_params->num_of_queues,
1530                         GFP_KERNEL);
1531         if (!priv->txq) {
1532                 IWL_ERR(priv, "Not enough memory for txq\n");
1533                 return -ENOMEM;
1534         }
1535         return 0;
1536 }
1537 EXPORT_SYMBOL(iwl_alloc_txq_mem);
1538
1539 void iwl_free_txq_mem(struct iwl_priv *priv)
1540 {
1541         kfree(priv->txq);
1542         priv->txq = NULL;
1543 }
1544 EXPORT_SYMBOL(iwl_free_txq_mem);
1545
1546 #ifdef CONFIG_IWLWIFI_DEBUGFS
1547
1548 #define IWL_TRAFFIC_DUMP_SIZE   (IWL_TRAFFIC_ENTRY_SIZE * IWL_TRAFFIC_ENTRIES)
1549
1550 void iwl_reset_traffic_log(struct iwl_priv *priv)
1551 {
1552         priv->tx_traffic_idx = 0;
1553         priv->rx_traffic_idx = 0;
1554         if (priv->tx_traffic)
1555                 memset(priv->tx_traffic, 0, IWL_TRAFFIC_DUMP_SIZE);
1556         if (priv->rx_traffic)
1557                 memset(priv->rx_traffic, 0, IWL_TRAFFIC_DUMP_SIZE);
1558 }
1559
1560 int iwl_alloc_traffic_mem(struct iwl_priv *priv)
1561 {
1562         u32 traffic_size = IWL_TRAFFIC_DUMP_SIZE;
1563
1564         if (iwl_debug_level & IWL_DL_TX) {
1565                 if (!priv->tx_traffic) {
1566                         priv->tx_traffic =
1567                                 kzalloc(traffic_size, GFP_KERNEL);
1568                         if (!priv->tx_traffic)
1569                                 return -ENOMEM;
1570                 }
1571         }
1572         if (iwl_debug_level & IWL_DL_RX) {
1573                 if (!priv->rx_traffic) {
1574                         priv->rx_traffic =
1575                                 kzalloc(traffic_size, GFP_KERNEL);
1576                         if (!priv->rx_traffic)
1577                                 return -ENOMEM;
1578                 }
1579         }
1580         iwl_reset_traffic_log(priv);
1581         return 0;
1582 }
1583 EXPORT_SYMBOL(iwl_alloc_traffic_mem);
1584
1585 void iwl_free_traffic_mem(struct iwl_priv *priv)
1586 {
1587         kfree(priv->tx_traffic);
1588         priv->tx_traffic = NULL;
1589
1590         kfree(priv->rx_traffic);
1591         priv->rx_traffic = NULL;
1592 }
1593 EXPORT_SYMBOL(iwl_free_traffic_mem);
1594
1595 void iwl_dbg_log_tx_data_frame(struct iwl_priv *priv,
1596                       u16 length, struct ieee80211_hdr *header)
1597 {
1598         __le16 fc;
1599         u16 len;
1600
1601         if (likely(!(iwl_debug_level & IWL_DL_TX)))
1602                 return;
1603
1604         if (!priv->tx_traffic)
1605                 return;
1606
1607         fc = header->frame_control;
1608         if (ieee80211_is_data(fc)) {
1609                 len = (length > IWL_TRAFFIC_ENTRY_SIZE)
1610                        ? IWL_TRAFFIC_ENTRY_SIZE : length;
1611                 memcpy((priv->tx_traffic +
1612                        (priv->tx_traffic_idx * IWL_TRAFFIC_ENTRY_SIZE)),
1613                        header, len);
1614                 priv->tx_traffic_idx =
1615                         (priv->tx_traffic_idx + 1) % IWL_TRAFFIC_ENTRIES;
1616         }
1617 }
1618 EXPORT_SYMBOL(iwl_dbg_log_tx_data_frame);
1619
1620 void iwl_dbg_log_rx_data_frame(struct iwl_priv *priv,
1621                       u16 length, struct ieee80211_hdr *header)
1622 {
1623         __le16 fc;
1624         u16 len;
1625
1626         if (likely(!(iwl_debug_level & IWL_DL_RX)))
1627                 return;
1628
1629         if (!priv->rx_traffic)
1630                 return;
1631
1632         fc = header->frame_control;
1633         if (ieee80211_is_data(fc)) {
1634                 len = (length > IWL_TRAFFIC_ENTRY_SIZE)
1635                        ? IWL_TRAFFIC_ENTRY_SIZE : length;
1636                 memcpy((priv->rx_traffic +
1637                        (priv->rx_traffic_idx * IWL_TRAFFIC_ENTRY_SIZE)),
1638                        header, len);
1639                 priv->rx_traffic_idx =
1640                         (priv->rx_traffic_idx + 1) % IWL_TRAFFIC_ENTRIES;
1641         }
1642 }
1643 EXPORT_SYMBOL(iwl_dbg_log_rx_data_frame);
1644
1645 const char *get_mgmt_string(int cmd)
1646 {
1647         switch (cmd) {
1648                 IWL_CMD(MANAGEMENT_ASSOC_REQ);
1649                 IWL_CMD(MANAGEMENT_ASSOC_RESP);
1650                 IWL_CMD(MANAGEMENT_REASSOC_REQ);
1651                 IWL_CMD(MANAGEMENT_REASSOC_RESP);
1652                 IWL_CMD(MANAGEMENT_PROBE_REQ);
1653                 IWL_CMD(MANAGEMENT_PROBE_RESP);
1654                 IWL_CMD(MANAGEMENT_BEACON);
1655                 IWL_CMD(MANAGEMENT_ATIM);
1656                 IWL_CMD(MANAGEMENT_DISASSOC);
1657                 IWL_CMD(MANAGEMENT_AUTH);
1658                 IWL_CMD(MANAGEMENT_DEAUTH);
1659                 IWL_CMD(MANAGEMENT_ACTION);
1660         default:
1661                 return "UNKNOWN";
1662
1663         }
1664 }
1665
1666 const char *get_ctrl_string(int cmd)
1667 {
1668         switch (cmd) {
1669                 IWL_CMD(CONTROL_BACK_REQ);
1670                 IWL_CMD(CONTROL_BACK);
1671                 IWL_CMD(CONTROL_PSPOLL);
1672                 IWL_CMD(CONTROL_RTS);
1673                 IWL_CMD(CONTROL_CTS);
1674                 IWL_CMD(CONTROL_ACK);
1675                 IWL_CMD(CONTROL_CFEND);
1676                 IWL_CMD(CONTROL_CFENDACK);
1677         default:
1678                 return "UNKNOWN";
1679
1680         }
1681 }
1682
1683 void iwl_clear_traffic_stats(struct iwl_priv *priv)
1684 {
1685         memset(&priv->tx_stats, 0, sizeof(struct traffic_stats));
1686         memset(&priv->rx_stats, 0, sizeof(struct traffic_stats));
1687 }
1688
1689 /*
1690  * if CONFIG_IWLWIFI_DEBUGFS defined, iwl_update_stats function will
1691  * record all the MGMT, CTRL and DATA pkt for both TX and Rx pass.
1692  * Use debugFs to display the rx/rx_statistics
1693  * if CONFIG_IWLWIFI_DEBUGFS not being defined, then no MGMT and CTRL
1694  * information will be recorded, but DATA pkt still will be recorded
1695  * for the reason of iwl_led.c need to control the led blinking based on
1696  * number of tx and rx data.
1697  *
1698  */
1699 void iwl_update_stats(struct iwl_priv *priv, bool is_tx, __le16 fc, u16 len)
1700 {
1701         struct traffic_stats    *stats;
1702
1703         if (is_tx)
1704                 stats = &priv->tx_stats;
1705         else
1706                 stats = &priv->rx_stats;
1707
1708         if (ieee80211_is_mgmt(fc)) {
1709                 switch (fc & cpu_to_le16(IEEE80211_FCTL_STYPE)) {
1710                 case cpu_to_le16(IEEE80211_STYPE_ASSOC_REQ):
1711                         stats->mgmt[MANAGEMENT_ASSOC_REQ]++;
1712                         break;
1713                 case cpu_to_le16(IEEE80211_STYPE_ASSOC_RESP):
1714                         stats->mgmt[MANAGEMENT_ASSOC_RESP]++;
1715                         break;
1716                 case cpu_to_le16(IEEE80211_STYPE_REASSOC_REQ):
1717                         stats->mgmt[MANAGEMENT_REASSOC_REQ]++;
1718                         break;
1719                 case cpu_to_le16(IEEE80211_STYPE_REASSOC_RESP):
1720                         stats->mgmt[MANAGEMENT_REASSOC_RESP]++;
1721                         break;
1722                 case cpu_to_le16(IEEE80211_STYPE_PROBE_REQ):
1723                         stats->mgmt[MANAGEMENT_PROBE_REQ]++;
1724                         break;
1725                 case cpu_to_le16(IEEE80211_STYPE_PROBE_RESP):
1726                         stats->mgmt[MANAGEMENT_PROBE_RESP]++;
1727                         break;
1728                 case cpu_to_le16(IEEE80211_STYPE_BEACON):
1729                         stats->mgmt[MANAGEMENT_BEACON]++;
1730                         break;
1731                 case cpu_to_le16(IEEE80211_STYPE_ATIM):
1732                         stats->mgmt[MANAGEMENT_ATIM]++;
1733                         break;
1734                 case cpu_to_le16(IEEE80211_STYPE_DISASSOC):
1735                         stats->mgmt[MANAGEMENT_DISASSOC]++;
1736                         break;
1737                 case cpu_to_le16(IEEE80211_STYPE_AUTH):
1738                         stats->mgmt[MANAGEMENT_AUTH]++;
1739                         break;
1740                 case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
1741                         stats->mgmt[MANAGEMENT_DEAUTH]++;
1742                         break;
1743                 case cpu_to_le16(IEEE80211_STYPE_ACTION):
1744                         stats->mgmt[MANAGEMENT_ACTION]++;
1745                         break;
1746                 }
1747         } else if (ieee80211_is_ctl(fc)) {
1748                 switch (fc & cpu_to_le16(IEEE80211_FCTL_STYPE)) {
1749                 case cpu_to_le16(IEEE80211_STYPE_BACK_REQ):
1750                         stats->ctrl[CONTROL_BACK_REQ]++;
1751                         break;
1752                 case cpu_to_le16(IEEE80211_STYPE_BACK):
1753                         stats->ctrl[CONTROL_BACK]++;
1754                         break;
1755                 case cpu_to_le16(IEEE80211_STYPE_PSPOLL):
1756                         stats->ctrl[CONTROL_PSPOLL]++;
1757                         break;
1758                 case cpu_to_le16(IEEE80211_STYPE_RTS):
1759                         stats->ctrl[CONTROL_RTS]++;
1760                         break;
1761                 case cpu_to_le16(IEEE80211_STYPE_CTS):
1762                         stats->ctrl[CONTROL_CTS]++;
1763                         break;
1764                 case cpu_to_le16(IEEE80211_STYPE_ACK):
1765                         stats->ctrl[CONTROL_ACK]++;
1766                         break;
1767                 case cpu_to_le16(IEEE80211_STYPE_CFEND):
1768                         stats->ctrl[CONTROL_CFEND]++;
1769                         break;
1770                 case cpu_to_le16(IEEE80211_STYPE_CFENDACK):
1771                         stats->ctrl[CONTROL_CFENDACK]++;
1772                         break;
1773                 }
1774         } else {
1775                 /* data */
1776                 stats->data_cnt++;
1777                 stats->data_bytes += len;
1778         }
1779 }
1780 EXPORT_SYMBOL(iwl_update_stats);
1781 #endif
1782
1783 static void iwl_force_rf_reset(struct iwl_priv *priv)
1784 {
1785         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1786                 return;
1787
1788         if (!iwl_is_any_associated(priv)) {
1789                 IWL_DEBUG_SCAN(priv, "force reset rejected: not associated\n");
1790                 return;
1791         }
1792         /*
1793          * There is no easy and better way to force reset the radio,
1794          * the only known method is switching channel which will force to
1795          * reset and tune the radio.
1796          * Use internal short scan (single channel) operation to should
1797          * achieve this objective.
1798          * Driver should reset the radio when number of consecutive missed
1799          * beacon, or any other uCode error condition detected.
1800          */
1801         IWL_DEBUG_INFO(priv, "perform radio reset.\n");
1802         iwl_internal_short_hw_scan(priv);
1803 }
1804
1805
1806 int iwl_force_reset(struct iwl_priv *priv, int mode, bool external)
1807 {
1808         struct iwl_force_reset *force_reset;
1809
1810         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1811                 return -EINVAL;
1812
1813         if (mode >= IWL_MAX_FORCE_RESET) {
1814                 IWL_DEBUG_INFO(priv, "invalid reset request.\n");
1815                 return -EINVAL;
1816         }
1817         force_reset = &priv->force_reset[mode];
1818         force_reset->reset_request_count++;
1819         if (!external) {
1820                 if (force_reset->last_force_reset_jiffies &&
1821                     time_after(force_reset->last_force_reset_jiffies +
1822                     force_reset->reset_duration, jiffies)) {
1823                         IWL_DEBUG_INFO(priv, "force reset rejected\n");
1824                         force_reset->reset_reject_count++;
1825                         return -EAGAIN;
1826                 }
1827         }
1828         force_reset->reset_success_count++;
1829         force_reset->last_force_reset_jiffies = jiffies;
1830         IWL_DEBUG_INFO(priv, "perform force reset (%d)\n", mode);
1831         switch (mode) {
1832         case IWL_RF_RESET:
1833                 iwl_force_rf_reset(priv);
1834                 break;
1835         case IWL_FW_RESET:
1836                 /*
1837                  * if the request is from external(ex: debugfs),
1838                  * then always perform the request in regardless the module
1839                  * parameter setting
1840                  * if the request is from internal (uCode error or driver
1841                  * detect failure), then fw_restart module parameter
1842                  * need to be check before performing firmware reload
1843                  */
1844                 if (!external && !priv->cfg->mod_params->restart_fw) {
1845                         IWL_DEBUG_INFO(priv, "Cancel firmware reload based on "
1846                                        "module parameter setting\n");
1847                         break;
1848                 }
1849                 IWL_ERR(priv, "On demand firmware reload\n");
1850                 /* Set the FW error flag -- cleared on iwl_down */
1851                 set_bit(STATUS_FW_ERROR, &priv->status);
1852                 wake_up_interruptible(&priv->wait_command_queue);
1853                 /*
1854                  * Keep the restart process from trying to send host
1855                  * commands by clearing the INIT status bit
1856                  */
1857                 clear_bit(STATUS_READY, &priv->status);
1858                 queue_work(priv->workqueue, &priv->restart);
1859                 break;
1860         }
1861         return 0;
1862 }
1863
1864 int iwl_mac_change_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1865                              enum nl80211_iftype newtype, bool newp2p)
1866 {
1867         struct iwl_priv *priv = hw->priv;
1868         struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
1869         struct iwl_rxon_context *tmp;
1870         u32 interface_modes;
1871         int err;
1872
1873         newtype = ieee80211_iftype_p2p(newtype, newp2p);
1874
1875         mutex_lock(&priv->mutex);
1876
1877         interface_modes = ctx->interface_modes | ctx->exclusive_interface_modes;
1878
1879         if (!(interface_modes & BIT(newtype))) {
1880                 err = -EBUSY;
1881                 goto out;
1882         }
1883
1884         if (ctx->exclusive_interface_modes & BIT(newtype)) {
1885                 for_each_context(priv, tmp) {
1886                         if (ctx == tmp)
1887                                 continue;
1888
1889                         if (!tmp->vif)
1890                                 continue;
1891
1892                         /*
1893                          * The current mode switch would be exclusive, but
1894                          * another context is active ... refuse the switch.
1895                          */
1896                         err = -EBUSY;
1897                         goto out;
1898                 }
1899         }
1900
1901         /* success */
1902         iwl_teardown_interface(priv, vif, true);
1903         vif->type = newtype;
1904         err = iwl_setup_interface(priv, ctx);
1905         WARN_ON(err);
1906         /*
1907          * We've switched internally, but submitting to the
1908          * device may have failed for some reason. Mask this
1909          * error, because otherwise mac80211 will not switch
1910          * (and set the interface type back) and we'll be
1911          * out of sync with it.
1912          */
1913         err = 0;
1914
1915  out:
1916         mutex_unlock(&priv->mutex);
1917         return err;
1918 }
1919 EXPORT_SYMBOL(iwl_mac_change_interface);
1920
1921 /*
1922  * On every watchdog tick we check (latest) time stamp. If it does not
1923  * change during timeout period and queue is not empty we reset firmware.
1924  */
1925 static int iwl_check_stuck_queue(struct iwl_priv *priv, int cnt)
1926 {
1927         struct iwl_tx_queue *txq = &priv->txq[cnt];
1928         struct iwl_queue *q = &txq->q;
1929         unsigned long timeout;
1930         int ret;
1931
1932         if (q->read_ptr == q->write_ptr) {
1933                 txq->time_stamp = jiffies;
1934                 return 0;
1935         }
1936
1937         timeout = txq->time_stamp +
1938                   msecs_to_jiffies(priv->cfg->base_params->wd_timeout);
1939
1940         if (time_after(jiffies, timeout)) {
1941                 IWL_ERR(priv, "Queue %d stuck for %u ms.\n",
1942                                 q->id, priv->cfg->base_params->wd_timeout);
1943                 ret = iwl_force_reset(priv, IWL_FW_RESET, false);
1944                 return (ret == -EAGAIN) ? 0 : 1;
1945         }
1946
1947         return 0;
1948 }
1949
1950 /*
1951  * Making watchdog tick be a quarter of timeout assure we will
1952  * discover the queue hung between timeout and 1.25*timeout
1953  */
1954 #define IWL_WD_TICK(timeout) ((timeout) / 4)
1955
1956 /*
1957  * Watchdog timer callback, we check each tx queue for stuck, if if hung
1958  * we reset the firmware. If everything is fine just rearm the timer.
1959  */
1960 void iwl_bg_watchdog(unsigned long data)
1961 {
1962         struct iwl_priv *priv = (struct iwl_priv *)data;
1963         int cnt;
1964         unsigned long timeout;
1965
1966         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1967                 return;
1968
1969         timeout = priv->cfg->base_params->wd_timeout;
1970         if (timeout == 0)
1971                 return;
1972
1973         /* monitor and check for stuck cmd queue */
1974         if (iwl_check_stuck_queue(priv, priv->cmd_queue))
1975                 return;
1976
1977         /* monitor and check for other stuck queues */
1978         if (iwl_is_any_associated(priv)) {
1979                 for (cnt = 0; cnt < priv->hw_params.max_txq_num; cnt++) {
1980                         /* skip as we already checked the command queue */
1981                         if (cnt == priv->cmd_queue)
1982                                 continue;
1983                         if (iwl_check_stuck_queue(priv, cnt))
1984                                 return;
1985                 }
1986         }
1987
1988         mod_timer(&priv->watchdog, jiffies +
1989                   msecs_to_jiffies(IWL_WD_TICK(timeout)));
1990 }
1991 EXPORT_SYMBOL(iwl_bg_watchdog);
1992
1993 void iwl_setup_watchdog(struct iwl_priv *priv)
1994 {
1995         unsigned int timeout = priv->cfg->base_params->wd_timeout;
1996
1997         if (timeout)
1998                 mod_timer(&priv->watchdog,
1999                           jiffies + msecs_to_jiffies(IWL_WD_TICK(timeout)));
2000         else
2001                 del_timer(&priv->watchdog);
2002 }
2003 EXPORT_SYMBOL(iwl_setup_watchdog);
2004
2005 /*
2006  * extended beacon time format
2007  * time in usec will be changed into a 32-bit value in extended:internal format
2008  * the extended part is the beacon counts
2009  * the internal part is the time in usec within one beacon interval
2010  */
2011 u32 iwl_usecs_to_beacons(struct iwl_priv *priv, u32 usec, u32 beacon_interval)
2012 {
2013         u32 quot;
2014         u32 rem;
2015         u32 interval = beacon_interval * TIME_UNIT;
2016
2017         if (!interval || !usec)
2018                 return 0;
2019
2020         quot = (usec / interval) &
2021                 (iwl_beacon_time_mask_high(priv,
2022                 priv->hw_params.beacon_time_tsf_bits) >>
2023                 priv->hw_params.beacon_time_tsf_bits);
2024         rem = (usec % interval) & iwl_beacon_time_mask_low(priv,
2025                                    priv->hw_params.beacon_time_tsf_bits);
2026
2027         return (quot << priv->hw_params.beacon_time_tsf_bits) + rem;
2028 }
2029 EXPORT_SYMBOL(iwl_usecs_to_beacons);
2030
2031 /* base is usually what we get from ucode with each received frame,
2032  * the same as HW timer counter counting down
2033  */
2034 __le32 iwl_add_beacon_time(struct iwl_priv *priv, u32 base,
2035                            u32 addon, u32 beacon_interval)
2036 {
2037         u32 base_low = base & iwl_beacon_time_mask_low(priv,
2038                                         priv->hw_params.beacon_time_tsf_bits);
2039         u32 addon_low = addon & iwl_beacon_time_mask_low(priv,
2040                                         priv->hw_params.beacon_time_tsf_bits);
2041         u32 interval = beacon_interval * TIME_UNIT;
2042         u32 res = (base & iwl_beacon_time_mask_high(priv,
2043                                 priv->hw_params.beacon_time_tsf_bits)) +
2044                                 (addon & iwl_beacon_time_mask_high(priv,
2045                                 priv->hw_params.beacon_time_tsf_bits));
2046
2047         if (base_low > addon_low)
2048                 res += base_low - addon_low;
2049         else if (base_low < addon_low) {
2050                 res += interval + base_low - addon_low;
2051                 res += (1 << priv->hw_params.beacon_time_tsf_bits);
2052         } else
2053                 res += (1 << priv->hw_params.beacon_time_tsf_bits);
2054
2055         return cpu_to_le32(res);
2056 }
2057 EXPORT_SYMBOL(iwl_add_beacon_time);
2058
2059 #ifdef CONFIG_PM
2060
2061 int iwl_pci_suspend(struct device *device)
2062 {
2063         struct pci_dev *pdev = to_pci_dev(device);
2064         struct iwl_priv *priv = pci_get_drvdata(pdev);
2065
2066         /*
2067          * This function is called when system goes into suspend state
2068          * mac80211 will call iwl_mac_stop() from the mac80211 suspend function
2069          * first but since iwl_mac_stop() has no knowledge of who the caller is,
2070          * it will not call apm_ops.stop() to stop the DMA operation.
2071          * Calling apm_ops.stop here to make sure we stop the DMA.
2072          */
2073         iwl_apm_stop(priv);
2074
2075         return 0;
2076 }
2077 EXPORT_SYMBOL(iwl_pci_suspend);
2078
2079 int iwl_pci_resume(struct device *device)
2080 {
2081         struct pci_dev *pdev = to_pci_dev(device);
2082         struct iwl_priv *priv = pci_get_drvdata(pdev);
2083         bool hw_rfkill = false;
2084
2085         /*
2086          * We disable the RETRY_TIMEOUT register (0x41) to keep
2087          * PCI Tx retries from interfering with C3 CPU state.
2088          */
2089         pci_write_config_byte(pdev, PCI_CFG_RETRY_TIMEOUT, 0x00);
2090
2091         iwl_enable_interrupts(priv);
2092
2093         if (!(iwl_read32(priv, CSR_GP_CNTRL) &
2094                                 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
2095                 hw_rfkill = true;
2096
2097         if (hw_rfkill)
2098                 set_bit(STATUS_RF_KILL_HW, &priv->status);
2099         else
2100                 clear_bit(STATUS_RF_KILL_HW, &priv->status);
2101
2102         wiphy_rfkill_set_hw_state(priv->hw->wiphy, hw_rfkill);
2103
2104         return 0;
2105 }
2106 EXPORT_SYMBOL(iwl_pci_resume);
2107
2108 const struct dev_pm_ops iwl_pm_ops = {
2109         .suspend = iwl_pci_suspend,
2110         .resume = iwl_pci_resume,
2111         .freeze = iwl_pci_suspend,
2112         .thaw = iwl_pci_resume,
2113         .poweroff = iwl_pci_suspend,
2114         .restore = iwl_pci_resume,
2115 };
2116 EXPORT_SYMBOL(iwl_pm_ops);
2117
2118 #endif /* CONFIG_PM */