]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/net/wireless/iwlwifi/iwl-core.c
Merge branch 'packaging' of git://git.kernel.org/pub/scm/linux/kernel/git/mmarek...
[linux.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                 struct ieee80211_ops *hw_ops)
82 {
83         struct iwl_priv *priv;
84
85         /* mac80211 allocates memory for this device instance, including
86          *   space for this driver's private structure */
87         struct ieee80211_hw *hw =
88                 ieee80211_alloc_hw(sizeof(struct iwl_priv), hw_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 /*
104  * QoS  support
105 */
106 static void iwl_update_qos(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
107 {
108         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
109                 return;
110
111         if (!ctx->is_active)
112                 return;
113
114         ctx->qos_data.def_qos_parm.qos_flags = 0;
115
116         if (ctx->qos_data.qos_active)
117                 ctx->qos_data.def_qos_parm.qos_flags |=
118                         QOS_PARAM_FLG_UPDATE_EDCA_MSK;
119
120         if (ctx->ht.enabled)
121                 ctx->qos_data.def_qos_parm.qos_flags |= QOS_PARAM_FLG_TGN_MSK;
122
123         IWL_DEBUG_QOS(priv, "send QoS cmd with Qos active=%d FLAGS=0x%X\n",
124                       ctx->qos_data.qos_active,
125                       ctx->qos_data.def_qos_parm.qos_flags);
126
127         iwl_send_cmd_pdu_async(priv, ctx->qos_cmd,
128                                sizeof(struct iwl_qosparam_cmd),
129                                &ctx->qos_data.def_qos_parm, NULL);
130 }
131
132 #define MAX_BIT_RATE_40_MHZ 150 /* Mbps */
133 #define MAX_BIT_RATE_20_MHZ 72 /* Mbps */
134 static void iwlcore_init_ht_hw_capab(const struct iwl_priv *priv,
135                               struct ieee80211_sta_ht_cap *ht_info,
136                               enum ieee80211_band band)
137 {
138         u16 max_bit_rate = 0;
139         u8 rx_chains_num = priv->hw_params.rx_chains_num;
140         u8 tx_chains_num = priv->hw_params.tx_chains_num;
141
142         ht_info->cap = 0;
143         memset(&ht_info->mcs, 0, sizeof(ht_info->mcs));
144
145         ht_info->ht_supported = true;
146
147         if (priv->cfg->ht_params &&
148             priv->cfg->ht_params->ht_greenfield_support)
149                 ht_info->cap |= IEEE80211_HT_CAP_GRN_FLD;
150         ht_info->cap |= IEEE80211_HT_CAP_SGI_20;
151         max_bit_rate = MAX_BIT_RATE_20_MHZ;
152         if (priv->hw_params.ht40_channel & BIT(band)) {
153                 ht_info->cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
154                 ht_info->cap |= IEEE80211_HT_CAP_SGI_40;
155                 ht_info->mcs.rx_mask[4] = 0x01;
156                 max_bit_rate = MAX_BIT_RATE_40_MHZ;
157         }
158
159         if (priv->cfg->mod_params->amsdu_size_8K)
160                 ht_info->cap |= IEEE80211_HT_CAP_MAX_AMSDU;
161
162         ht_info->ampdu_factor = CFG_HT_RX_AMPDU_FACTOR_DEF;
163         if (priv->cfg->bt_params && priv->cfg->bt_params->ampdu_factor)
164                 ht_info->ampdu_factor = priv->cfg->bt_params->ampdu_factor;
165         ht_info->ampdu_density = CFG_HT_MPDU_DENSITY_DEF;
166         if (priv->cfg->bt_params && priv->cfg->bt_params->ampdu_density)
167                 ht_info->ampdu_density = priv->cfg->bt_params->ampdu_density;
168
169         ht_info->mcs.rx_mask[0] = 0xFF;
170         if (rx_chains_num >= 2)
171                 ht_info->mcs.rx_mask[1] = 0xFF;
172         if (rx_chains_num >= 3)
173                 ht_info->mcs.rx_mask[2] = 0xFF;
174
175         /* Highest supported Rx data rate */
176         max_bit_rate *= rx_chains_num;
177         WARN_ON(max_bit_rate & ~IEEE80211_HT_MCS_RX_HIGHEST_MASK);
178         ht_info->mcs.rx_highest = cpu_to_le16(max_bit_rate);
179
180         /* Tx MCS capabilities */
181         ht_info->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
182         if (tx_chains_num != rx_chains_num) {
183                 ht_info->mcs.tx_params |= IEEE80211_HT_MCS_TX_RX_DIFF;
184                 ht_info->mcs.tx_params |= ((tx_chains_num - 1) <<
185                                 IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT);
186         }
187 }
188
189 /**
190  * iwlcore_init_geos - Initialize mac80211's geo/channel info based from eeprom
191  */
192 int iwlcore_init_geos(struct iwl_priv *priv)
193 {
194         struct iwl_channel_info *ch;
195         struct ieee80211_supported_band *sband;
196         struct ieee80211_channel *channels;
197         struct ieee80211_channel *geo_ch;
198         struct ieee80211_rate *rates;
199         int i = 0;
200
201         if (priv->bands[IEEE80211_BAND_2GHZ].n_bitrates ||
202             priv->bands[IEEE80211_BAND_5GHZ].n_bitrates) {
203                 IWL_DEBUG_INFO(priv, "Geography modes already initialized.\n");
204                 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
205                 return 0;
206         }
207
208         channels = kzalloc(sizeof(struct ieee80211_channel) *
209                            priv->channel_count, GFP_KERNEL);
210         if (!channels)
211                 return -ENOMEM;
212
213         rates = kzalloc((sizeof(struct ieee80211_rate) * IWL_RATE_COUNT_LEGACY),
214                         GFP_KERNEL);
215         if (!rates) {
216                 kfree(channels);
217                 return -ENOMEM;
218         }
219
220         /* 5.2GHz channels start after the 2.4GHz channels */
221         sband = &priv->bands[IEEE80211_BAND_5GHZ];
222         sband->channels = &channels[ARRAY_SIZE(iwl_eeprom_band_1)];
223         /* just OFDM */
224         sband->bitrates = &rates[IWL_FIRST_OFDM_RATE];
225         sband->n_bitrates = IWL_RATE_COUNT_LEGACY - IWL_FIRST_OFDM_RATE;
226
227         if (priv->cfg->sku & IWL_SKU_N)
228                 iwlcore_init_ht_hw_capab(priv, &sband->ht_cap,
229                                          IEEE80211_BAND_5GHZ);
230
231         sband = &priv->bands[IEEE80211_BAND_2GHZ];
232         sband->channels = channels;
233         /* OFDM & CCK */
234         sband->bitrates = rates;
235         sband->n_bitrates = IWL_RATE_COUNT_LEGACY;
236
237         if (priv->cfg->sku & IWL_SKU_N)
238                 iwlcore_init_ht_hw_capab(priv, &sband->ht_cap,
239                                          IEEE80211_BAND_2GHZ);
240
241         priv->ieee_channels = channels;
242         priv->ieee_rates = rates;
243
244         for (i = 0;  i < priv->channel_count; i++) {
245                 ch = &priv->channel_info[i];
246
247                 /* FIXME: might be removed if scan is OK */
248                 if (!is_channel_valid(ch))
249                         continue;
250
251                 if (is_channel_a_band(ch))
252                         sband =  &priv->bands[IEEE80211_BAND_5GHZ];
253                 else
254                         sband =  &priv->bands[IEEE80211_BAND_2GHZ];
255
256                 geo_ch = &sband->channels[sband->n_channels++];
257
258                 geo_ch->center_freq =
259                                 ieee80211_channel_to_frequency(ch->channel);
260                 geo_ch->max_power = ch->max_power_avg;
261                 geo_ch->max_antenna_gain = 0xff;
262                 geo_ch->hw_value = ch->channel;
263
264                 if (is_channel_valid(ch)) {
265                         if (!(ch->flags & EEPROM_CHANNEL_IBSS))
266                                 geo_ch->flags |= IEEE80211_CHAN_NO_IBSS;
267
268                         if (!(ch->flags & EEPROM_CHANNEL_ACTIVE))
269                                 geo_ch->flags |= IEEE80211_CHAN_PASSIVE_SCAN;
270
271                         if (ch->flags & EEPROM_CHANNEL_RADAR)
272                                 geo_ch->flags |= IEEE80211_CHAN_RADAR;
273
274                         geo_ch->flags |= ch->ht40_extension_channel;
275
276                         if (ch->max_power_avg > priv->tx_power_device_lmt)
277                                 priv->tx_power_device_lmt = ch->max_power_avg;
278                 } else {
279                         geo_ch->flags |= IEEE80211_CHAN_DISABLED;
280                 }
281
282                 IWL_DEBUG_INFO(priv, "Channel %d Freq=%d[%sGHz] %s flag=0x%X\n",
283                                 ch->channel, geo_ch->center_freq,
284                                 is_channel_a_band(ch) ?  "5.2" : "2.4",
285                                 geo_ch->flags & IEEE80211_CHAN_DISABLED ?
286                                 "restricted" : "valid",
287                                  geo_ch->flags);
288         }
289
290         if ((priv->bands[IEEE80211_BAND_5GHZ].n_channels == 0) &&
291              priv->cfg->sku & IWL_SKU_A) {
292                 IWL_INFO(priv, "Incorrectly detected BG card as ABG. "
293                         "Please send your PCI ID 0x%04X:0x%04X to maintainer.\n",
294                            priv->pci_dev->device,
295                            priv->pci_dev->subsystem_device);
296                 priv->cfg->sku &= ~IWL_SKU_A;
297         }
298
299         IWL_INFO(priv, "Tunable channels: %d 802.11bg, %d 802.11a channels\n",
300                    priv->bands[IEEE80211_BAND_2GHZ].n_channels,
301                    priv->bands[IEEE80211_BAND_5GHZ].n_channels);
302
303         set_bit(STATUS_GEO_CONFIGURED, &priv->status);
304
305         return 0;
306 }
307 EXPORT_SYMBOL(iwlcore_init_geos);
308
309 /*
310  * iwlcore_free_geos - undo allocations in iwlcore_init_geos
311  */
312 void iwlcore_free_geos(struct iwl_priv *priv)
313 {
314         kfree(priv->ieee_channels);
315         kfree(priv->ieee_rates);
316         clear_bit(STATUS_GEO_CONFIGURED, &priv->status);
317 }
318 EXPORT_SYMBOL(iwlcore_free_geos);
319
320 /*
321  *  iwlcore_tx_cmd_protection: Set rts/cts. 3945 and 4965 only share this
322  *  function.
323  */
324 void iwlcore_tx_cmd_protection(struct iwl_priv *priv,
325                                struct ieee80211_tx_info *info,
326                                __le16 fc, __le32 *tx_flags)
327 {
328         if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS) {
329                 *tx_flags |= TX_CMD_FLG_RTS_MSK;
330                 *tx_flags &= ~TX_CMD_FLG_CTS_MSK;
331                 *tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
332
333                 if (!ieee80211_is_mgmt(fc))
334                         return;
335
336                 switch (fc & cpu_to_le16(IEEE80211_FCTL_STYPE)) {
337                 case cpu_to_le16(IEEE80211_STYPE_AUTH):
338                 case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
339                 case cpu_to_le16(IEEE80211_STYPE_ASSOC_REQ):
340                 case cpu_to_le16(IEEE80211_STYPE_REASSOC_REQ):
341                         *tx_flags &= ~TX_CMD_FLG_RTS_MSK;
342                         *tx_flags |= TX_CMD_FLG_CTS_MSK;
343                         break;
344                 }
345         } else if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
346                 *tx_flags &= ~TX_CMD_FLG_RTS_MSK;
347                 *tx_flags |= TX_CMD_FLG_CTS_MSK;
348                 *tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
349         }
350 }
351 EXPORT_SYMBOL(iwlcore_tx_cmd_protection);
352
353
354 static bool iwl_is_channel_extension(struct iwl_priv *priv,
355                                      enum ieee80211_band band,
356                                      u16 channel, u8 extension_chan_offset)
357 {
358         const struct iwl_channel_info *ch_info;
359
360         ch_info = iwl_get_channel_info(priv, band, channel);
361         if (!is_channel_valid(ch_info))
362                 return false;
363
364         if (extension_chan_offset == IEEE80211_HT_PARAM_CHA_SEC_ABOVE)
365                 return !(ch_info->ht40_extension_channel &
366                                         IEEE80211_CHAN_NO_HT40PLUS);
367         else if (extension_chan_offset == IEEE80211_HT_PARAM_CHA_SEC_BELOW)
368                 return !(ch_info->ht40_extension_channel &
369                                         IEEE80211_CHAN_NO_HT40MINUS);
370
371         return false;
372 }
373
374 bool iwl_is_ht40_tx_allowed(struct iwl_priv *priv,
375                             struct iwl_rxon_context *ctx,
376                             struct ieee80211_sta_ht_cap *ht_cap)
377 {
378         if (!ctx->ht.enabled || !ctx->ht.is_40mhz)
379                 return false;
380
381         /*
382          * We do not check for IEEE80211_HT_CAP_SUP_WIDTH_20_40
383          * the bit will not set if it is pure 40MHz case
384          */
385         if (ht_cap && !ht_cap->ht_supported)
386                 return false;
387
388 #ifdef CONFIG_IWLWIFI_DEBUGFS
389         if (priv->disable_ht40)
390                 return false;
391 #endif
392
393         return iwl_is_channel_extension(priv, priv->band,
394                         le16_to_cpu(ctx->staging.channel),
395                         ctx->ht.extension_chan_offset);
396 }
397 EXPORT_SYMBOL(iwl_is_ht40_tx_allowed);
398
399 static u16 iwl_adjust_beacon_interval(u16 beacon_val, u16 max_beacon_val)
400 {
401         u16 new_val;
402         u16 beacon_factor;
403
404         /*
405          * If mac80211 hasn't given us a beacon interval, program
406          * the default into the device (not checking this here
407          * would cause the adjustment below to return the maximum
408          * value, which may break PAN.)
409          */
410         if (!beacon_val)
411                 return DEFAULT_BEACON_INTERVAL;
412
413         /*
414          * If the beacon interval we obtained from the peer
415          * is too large, we'll have to wake up more often
416          * (and in IBSS case, we'll beacon too much)
417          *
418          * For example, if max_beacon_val is 4096, and the
419          * requested beacon interval is 7000, we'll have to
420          * use 3500 to be able to wake up on the beacons.
421          *
422          * This could badly influence beacon detection stats.
423          */
424
425         beacon_factor = (beacon_val + max_beacon_val) / max_beacon_val;
426         new_val = beacon_val / beacon_factor;
427
428         if (!new_val)
429                 new_val = max_beacon_val;
430
431         return new_val;
432 }
433
434 int iwl_send_rxon_timing(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
435 {
436         u64 tsf;
437         s32 interval_tm, rem;
438         struct ieee80211_conf *conf = NULL;
439         u16 beacon_int;
440         struct ieee80211_vif *vif = ctx->vif;
441
442         conf = ieee80211_get_hw_conf(priv->hw);
443
444         lockdep_assert_held(&priv->mutex);
445
446         memset(&ctx->timing, 0, sizeof(struct iwl_rxon_time_cmd));
447
448         ctx->timing.timestamp = cpu_to_le64(priv->timestamp);
449         ctx->timing.listen_interval = cpu_to_le16(conf->listen_interval);
450
451         beacon_int = vif ? vif->bss_conf.beacon_int : 0;
452
453         /*
454          * TODO: For IBSS we need to get atim_window from mac80211,
455          *       for now just always use 0
456          */
457         ctx->timing.atim_window = 0;
458
459         if (ctx->ctxid == IWL_RXON_CTX_PAN &&
460             (!ctx->vif || ctx->vif->type != NL80211_IFTYPE_STATION) &&
461             iwl_is_associated(priv, IWL_RXON_CTX_BSS) &&
462             priv->contexts[IWL_RXON_CTX_BSS].vif &&
463             priv->contexts[IWL_RXON_CTX_BSS].vif->bss_conf.beacon_int) {
464                 ctx->timing.beacon_interval =
465                         priv->contexts[IWL_RXON_CTX_BSS].timing.beacon_interval;
466                 beacon_int = le16_to_cpu(ctx->timing.beacon_interval);
467         } else if (ctx->ctxid == IWL_RXON_CTX_BSS &&
468                    iwl_is_associated(priv, IWL_RXON_CTX_PAN) &&
469                    priv->contexts[IWL_RXON_CTX_PAN].vif &&
470                    priv->contexts[IWL_RXON_CTX_PAN].vif->bss_conf.beacon_int &&
471                    (!iwl_is_associated_ctx(ctx) || !ctx->vif ||
472                     !ctx->vif->bss_conf.beacon_int)) {
473                 ctx->timing.beacon_interval =
474                         priv->contexts[IWL_RXON_CTX_PAN].timing.beacon_interval;
475                 beacon_int = le16_to_cpu(ctx->timing.beacon_interval);
476         } else {
477                 beacon_int = iwl_adjust_beacon_interval(beacon_int,
478                                 priv->hw_params.max_beacon_itrvl * TIME_UNIT);
479                 ctx->timing.beacon_interval = cpu_to_le16(beacon_int);
480         }
481
482         tsf = priv->timestamp; /* tsf is modifed by do_div: copy it */
483         interval_tm = beacon_int * TIME_UNIT;
484         rem = do_div(tsf, interval_tm);
485         ctx->timing.beacon_init_val = cpu_to_le32(interval_tm - rem);
486
487         ctx->timing.dtim_period = vif ? (vif->bss_conf.dtim_period ?: 1) : 1;
488
489         IWL_DEBUG_ASSOC(priv,
490                         "beacon interval %d beacon timer %d beacon tim %d\n",
491                         le16_to_cpu(ctx->timing.beacon_interval),
492                         le32_to_cpu(ctx->timing.beacon_init_val),
493                         le16_to_cpu(ctx->timing.atim_window));
494
495         return iwl_send_cmd_pdu(priv, ctx->rxon_timing_cmd,
496                                 sizeof(ctx->timing), &ctx->timing);
497 }
498 EXPORT_SYMBOL(iwl_send_rxon_timing);
499
500 void iwl_set_rxon_hwcrypto(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
501                            int hw_decrypt)
502 {
503         struct iwl_rxon_cmd *rxon = &ctx->staging;
504
505         if (hw_decrypt)
506                 rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
507         else
508                 rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
509
510 }
511 EXPORT_SYMBOL(iwl_set_rxon_hwcrypto);
512
513 /* validate RXON structure is valid */
514 int iwl_check_rxon_cmd(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
515 {
516         struct iwl_rxon_cmd *rxon = &ctx->staging;
517         bool error = false;
518
519         if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
520                 if (rxon->flags & RXON_FLG_TGJ_NARROW_BAND_MSK) {
521                         IWL_WARN(priv, "check 2.4G: wrong narrow\n");
522                         error = true;
523                 }
524                 if (rxon->flags & RXON_FLG_RADAR_DETECT_MSK) {
525                         IWL_WARN(priv, "check 2.4G: wrong radar\n");
526                         error = true;
527                 }
528         } else {
529                 if (!(rxon->flags & RXON_FLG_SHORT_SLOT_MSK)) {
530                         IWL_WARN(priv, "check 5.2G: not short slot!\n");
531                         error = true;
532                 }
533                 if (rxon->flags & RXON_FLG_CCK_MSK) {
534                         IWL_WARN(priv, "check 5.2G: CCK!\n");
535                         error = true;
536                 }
537         }
538         if ((rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1) {
539                 IWL_WARN(priv, "mac/bssid mcast!\n");
540                 error = true;
541         }
542
543         /* make sure basic rates 6Mbps and 1Mbps are supported */
544         if ((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0 &&
545             (rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0) {
546                 IWL_WARN(priv, "neither 1 nor 6 are basic\n");
547                 error = true;
548         }
549
550         if (le16_to_cpu(rxon->assoc_id) > 2007) {
551                 IWL_WARN(priv, "aid > 2007\n");
552                 error = true;
553         }
554
555         if ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
556                         == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK)) {
557                 IWL_WARN(priv, "CCK and short slot\n");
558                 error = true;
559         }
560
561         if ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
562                         == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK)) {
563                 IWL_WARN(priv, "CCK and auto detect");
564                 error = true;
565         }
566
567         if ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
568                             RXON_FLG_TGG_PROTECT_MSK)) ==
569                             RXON_FLG_TGG_PROTECT_MSK) {
570                 IWL_WARN(priv, "TGg but no auto-detect\n");
571                 error = true;
572         }
573
574         if (error)
575                 IWL_WARN(priv, "Tuning to channel %d\n",
576                             le16_to_cpu(rxon->channel));
577
578         if (error) {
579                 IWL_ERR(priv, "Invalid RXON\n");
580                 return -EINVAL;
581         }
582         return 0;
583 }
584 EXPORT_SYMBOL(iwl_check_rxon_cmd);
585
586 /**
587  * iwl_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
588  * @priv: staging_rxon is compared to active_rxon
589  *
590  * If the RXON structure is changing enough to require a new tune,
591  * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
592  * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
593  */
594 int iwl_full_rxon_required(struct iwl_priv *priv,
595                            struct iwl_rxon_context *ctx)
596 {
597         const struct iwl_rxon_cmd *staging = &ctx->staging;
598         const struct iwl_rxon_cmd *active = &ctx->active;
599
600 #define CHK(cond)                                                       \
601         if ((cond)) {                                                   \
602                 IWL_DEBUG_INFO(priv, "need full RXON - " #cond "\n");   \
603                 return 1;                                               \
604         }
605
606 #define CHK_NEQ(c1, c2)                                         \
607         if ((c1) != (c2)) {                                     \
608                 IWL_DEBUG_INFO(priv, "need full RXON - "        \
609                                #c1 " != " #c2 " - %d != %d\n",  \
610                                (c1), (c2));                     \
611                 return 1;                                       \
612         }
613
614         /* These items are only settable from the full RXON command */
615         CHK(!iwl_is_associated_ctx(ctx));
616         CHK(compare_ether_addr(staging->bssid_addr, active->bssid_addr));
617         CHK(compare_ether_addr(staging->node_addr, active->node_addr));
618         CHK(compare_ether_addr(staging->wlap_bssid_addr,
619                                 active->wlap_bssid_addr));
620         CHK_NEQ(staging->dev_type, active->dev_type);
621         CHK_NEQ(staging->channel, active->channel);
622         CHK_NEQ(staging->air_propagation, active->air_propagation);
623         CHK_NEQ(staging->ofdm_ht_single_stream_basic_rates,
624                 active->ofdm_ht_single_stream_basic_rates);
625         CHK_NEQ(staging->ofdm_ht_dual_stream_basic_rates,
626                 active->ofdm_ht_dual_stream_basic_rates);
627         CHK_NEQ(staging->ofdm_ht_triple_stream_basic_rates,
628                 active->ofdm_ht_triple_stream_basic_rates);
629         CHK_NEQ(staging->assoc_id, active->assoc_id);
630
631         /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
632          * be updated with the RXON_ASSOC command -- however only some
633          * flag transitions are allowed using RXON_ASSOC */
634
635         /* Check if we are not switching bands */
636         CHK_NEQ(staging->flags & RXON_FLG_BAND_24G_MSK,
637                 active->flags & RXON_FLG_BAND_24G_MSK);
638
639         /* Check if we are switching association toggle */
640         CHK_NEQ(staging->filter_flags & RXON_FILTER_ASSOC_MSK,
641                 active->filter_flags & RXON_FILTER_ASSOC_MSK);
642
643 #undef CHK
644 #undef CHK_NEQ
645
646         return 0;
647 }
648 EXPORT_SYMBOL(iwl_full_rxon_required);
649
650 u8 iwl_rate_get_lowest_plcp(struct iwl_priv *priv,
651                             struct iwl_rxon_context *ctx)
652 {
653         /*
654          * Assign the lowest rate -- should really get this from
655          * the beacon skb from mac80211.
656          */
657         if (ctx->staging.flags & RXON_FLG_BAND_24G_MSK)
658                 return IWL_RATE_1M_PLCP;
659         else
660                 return IWL_RATE_6M_PLCP;
661 }
662 EXPORT_SYMBOL(iwl_rate_get_lowest_plcp);
663
664 static void _iwl_set_rxon_ht(struct iwl_priv *priv,
665                              struct iwl_ht_config *ht_conf,
666                              struct iwl_rxon_context *ctx)
667 {
668         struct iwl_rxon_cmd *rxon = &ctx->staging;
669
670         if (!ctx->ht.enabled) {
671                 rxon->flags &= ~(RXON_FLG_CHANNEL_MODE_MSK |
672                         RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK |
673                         RXON_FLG_HT40_PROT_MSK |
674                         RXON_FLG_HT_PROT_MSK);
675                 return;
676         }
677
678         /* FIXME: if the definition of ht.protection changed, the "translation"
679          * will be needed for rxon->flags
680          */
681         rxon->flags |= cpu_to_le32(ctx->ht.protection << RXON_FLG_HT_OPERATING_MODE_POS);
682
683         /* Set up channel bandwidth:
684          * 20 MHz only, 20/40 mixed or pure 40 if ht40 ok */
685         /* clear the HT channel mode before set the mode */
686         rxon->flags &= ~(RXON_FLG_CHANNEL_MODE_MSK |
687                          RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK);
688         if (iwl_is_ht40_tx_allowed(priv, ctx, NULL)) {
689                 /* pure ht40 */
690                 if (ctx->ht.protection == IEEE80211_HT_OP_MODE_PROTECTION_20MHZ) {
691                         rxon->flags |= RXON_FLG_CHANNEL_MODE_PURE_40;
692                         /* Note: control channel is opposite of extension channel */
693                         switch (ctx->ht.extension_chan_offset) {
694                         case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
695                                 rxon->flags &= ~RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
696                                 break;
697                         case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
698                                 rxon->flags |= RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
699                                 break;
700                         }
701                 } else {
702                         /* Note: control channel is opposite of extension channel */
703                         switch (ctx->ht.extension_chan_offset) {
704                         case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
705                                 rxon->flags &= ~(RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK);
706                                 rxon->flags |= RXON_FLG_CHANNEL_MODE_MIXED;
707                                 break;
708                         case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
709                                 rxon->flags |= RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
710                                 rxon->flags |= RXON_FLG_CHANNEL_MODE_MIXED;
711                                 break;
712                         case IEEE80211_HT_PARAM_CHA_SEC_NONE:
713                         default:
714                                 /* channel location only valid if in Mixed mode */
715                                 IWL_ERR(priv, "invalid extension channel offset\n");
716                                 break;
717                         }
718                 }
719         } else {
720                 rxon->flags |= RXON_FLG_CHANNEL_MODE_LEGACY;
721         }
722
723         if (priv->cfg->ops->hcmd->set_rxon_chain)
724                 priv->cfg->ops->hcmd->set_rxon_chain(priv, ctx);
725
726         IWL_DEBUG_ASSOC(priv, "rxon flags 0x%X operation mode :0x%X "
727                         "extension channel offset 0x%x\n",
728                         le32_to_cpu(rxon->flags), ctx->ht.protection,
729                         ctx->ht.extension_chan_offset);
730 }
731
732 void iwl_set_rxon_ht(struct iwl_priv *priv, struct iwl_ht_config *ht_conf)
733 {
734         struct iwl_rxon_context *ctx;
735
736         for_each_context(priv, ctx)
737                 _iwl_set_rxon_ht(priv, ht_conf, ctx);
738 }
739 EXPORT_SYMBOL(iwl_set_rxon_ht);
740
741 /* Return valid, unused, channel for a passive scan to reset the RF */
742 u8 iwl_get_single_channel_number(struct iwl_priv *priv,
743                                  enum ieee80211_band band)
744 {
745         const struct iwl_channel_info *ch_info;
746         int i;
747         u8 channel = 0;
748         u8 min, max;
749         struct iwl_rxon_context *ctx;
750
751         if (band == IEEE80211_BAND_5GHZ) {
752                 min = 14;
753                 max = priv->channel_count;
754         } else {
755                 min = 0;
756                 max = 14;
757         }
758
759         for (i = min; i < max; i++) {
760                 bool busy = false;
761
762                 for_each_context(priv, ctx) {
763                         busy = priv->channel_info[i].channel ==
764                                 le16_to_cpu(ctx->staging.channel);
765                         if (busy)
766                                 break;
767                 }
768
769                 if (busy)
770                         continue;
771
772                 channel = priv->channel_info[i].channel;
773                 ch_info = iwl_get_channel_info(priv, band, channel);
774                 if (is_channel_valid(ch_info))
775                         break;
776         }
777
778         return channel;
779 }
780 EXPORT_SYMBOL(iwl_get_single_channel_number);
781
782 /**
783  * iwl_set_rxon_channel - Set the band and channel values in staging RXON
784  * @ch: requested channel as a pointer to struct ieee80211_channel
785
786  * NOTE:  Does not commit to the hardware; it sets appropriate bit fields
787  * in the staging RXON flag structure based on the ch->band
788  */
789 int iwl_set_rxon_channel(struct iwl_priv *priv, struct ieee80211_channel *ch,
790                          struct iwl_rxon_context *ctx)
791 {
792         enum ieee80211_band band = ch->band;
793         u16 channel = ch->hw_value;
794
795         if ((le16_to_cpu(ctx->staging.channel) == channel) &&
796             (priv->band == band))
797                 return 0;
798
799         ctx->staging.channel = cpu_to_le16(channel);
800         if (band == IEEE80211_BAND_5GHZ)
801                 ctx->staging.flags &= ~RXON_FLG_BAND_24G_MSK;
802         else
803                 ctx->staging.flags |= RXON_FLG_BAND_24G_MSK;
804
805         priv->band = band;
806
807         IWL_DEBUG_INFO(priv, "Staging channel set to %d [%d]\n", channel, band);
808
809         return 0;
810 }
811 EXPORT_SYMBOL(iwl_set_rxon_channel);
812
813 void iwl_set_flags_for_band(struct iwl_priv *priv,
814                             struct iwl_rxon_context *ctx,
815                             enum ieee80211_band band,
816                             struct ieee80211_vif *vif)
817 {
818         if (band == IEEE80211_BAND_5GHZ) {
819                 ctx->staging.flags &=
820                     ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
821                       | RXON_FLG_CCK_MSK);
822                 ctx->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
823         } else {
824                 /* Copied from iwl_post_associate() */
825                 if (vif && vif->bss_conf.use_short_slot)
826                         ctx->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
827                 else
828                         ctx->staging.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
829
830                 ctx->staging.flags |= RXON_FLG_BAND_24G_MSK;
831                 ctx->staging.flags |= RXON_FLG_AUTO_DETECT_MSK;
832                 ctx->staging.flags &= ~RXON_FLG_CCK_MSK;
833         }
834 }
835 EXPORT_SYMBOL(iwl_set_flags_for_band);
836
837 /*
838  * initialize rxon structure with default values from eeprom
839  */
840 void iwl_connection_init_rx_config(struct iwl_priv *priv,
841                                    struct iwl_rxon_context *ctx)
842 {
843         const struct iwl_channel_info *ch_info;
844
845         memset(&ctx->staging, 0, sizeof(ctx->staging));
846
847         if (!ctx->vif) {
848                 ctx->staging.dev_type = ctx->unused_devtype;
849         } else switch (ctx->vif->type) {
850         case NL80211_IFTYPE_AP:
851                 ctx->staging.dev_type = ctx->ap_devtype;
852                 break;
853
854         case NL80211_IFTYPE_STATION:
855                 ctx->staging.dev_type = ctx->station_devtype;
856                 ctx->staging.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
857                 break;
858
859         case NL80211_IFTYPE_ADHOC:
860                 ctx->staging.dev_type = ctx->ibss_devtype;
861                 ctx->staging.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
862                 ctx->staging.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
863                                                   RXON_FILTER_ACCEPT_GRP_MSK;
864                 break;
865
866         default:
867                 IWL_ERR(priv, "Unsupported interface type %d\n",
868                         ctx->vif->type);
869                 break;
870         }
871
872 #if 0
873         /* TODO:  Figure out when short_preamble would be set and cache from
874          * that */
875         if (!hw_to_local(priv->hw)->short_preamble)
876                 ctx->staging.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
877         else
878                 ctx->staging.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
879 #endif
880
881         ch_info = iwl_get_channel_info(priv, priv->band,
882                                        le16_to_cpu(ctx->active.channel));
883
884         if (!ch_info)
885                 ch_info = &priv->channel_info[0];
886
887         ctx->staging.channel = cpu_to_le16(ch_info->channel);
888         priv->band = ch_info->band;
889
890         iwl_set_flags_for_band(priv, ctx, priv->band, ctx->vif);
891
892         ctx->staging.ofdm_basic_rates =
893             (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
894         ctx->staging.cck_basic_rates =
895             (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
896
897         /* clear both MIX and PURE40 mode flag */
898         ctx->staging.flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED |
899                                         RXON_FLG_CHANNEL_MODE_PURE_40);
900         if (ctx->vif)
901                 memcpy(ctx->staging.node_addr, ctx->vif->addr, ETH_ALEN);
902
903         ctx->staging.ofdm_ht_single_stream_basic_rates = 0xff;
904         ctx->staging.ofdm_ht_dual_stream_basic_rates = 0xff;
905         ctx->staging.ofdm_ht_triple_stream_basic_rates = 0xff;
906 }
907 EXPORT_SYMBOL(iwl_connection_init_rx_config);
908
909 void iwl_set_rate(struct iwl_priv *priv)
910 {
911         const struct ieee80211_supported_band *hw = NULL;
912         struct ieee80211_rate *rate;
913         struct iwl_rxon_context *ctx;
914         int i;
915
916         hw = iwl_get_hw_mode(priv, priv->band);
917         if (!hw) {
918                 IWL_ERR(priv, "Failed to set rate: unable to get hw mode\n");
919                 return;
920         }
921
922         priv->active_rate = 0;
923
924         for (i = 0; i < hw->n_bitrates; i++) {
925                 rate = &(hw->bitrates[i]);
926                 if (rate->hw_value < IWL_RATE_COUNT_LEGACY)
927                         priv->active_rate |= (1 << rate->hw_value);
928         }
929
930         IWL_DEBUG_RATE(priv, "Set active_rate = %0x\n", priv->active_rate);
931
932         for_each_context(priv, ctx) {
933                 ctx->staging.cck_basic_rates =
934                     (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
935
936                 ctx->staging.ofdm_basic_rates =
937                    (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
938         }
939 }
940 EXPORT_SYMBOL(iwl_set_rate);
941
942 void iwl_chswitch_done(struct iwl_priv *priv, bool is_success)
943 {
944         /*
945          * MULTI-FIXME
946          * See iwl_mac_channel_switch.
947          */
948         struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
949
950         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
951                 return;
952
953         if (priv->switch_rxon.switch_in_progress) {
954                 ieee80211_chswitch_done(ctx->vif, is_success);
955                 mutex_lock(&priv->mutex);
956                 priv->switch_rxon.switch_in_progress = false;
957                 mutex_unlock(&priv->mutex);
958         }
959 }
960 EXPORT_SYMBOL(iwl_chswitch_done);
961
962 void iwl_rx_csa(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb)
963 {
964         struct iwl_rx_packet *pkt = rxb_addr(rxb);
965         struct iwl_csa_notification *csa = &(pkt->u.csa_notif);
966         /*
967          * MULTI-FIXME
968          * See iwl_mac_channel_switch.
969          */
970         struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
971         struct iwl_rxon_cmd *rxon = (void *)&ctx->active;
972
973         if (priv->switch_rxon.switch_in_progress) {
974                 if (!le32_to_cpu(csa->status) &&
975                     (csa->channel == priv->switch_rxon.channel)) {
976                         rxon->channel = csa->channel;
977                         ctx->staging.channel = csa->channel;
978                         IWL_DEBUG_11H(priv, "CSA notif: channel %d\n",
979                               le16_to_cpu(csa->channel));
980                         iwl_chswitch_done(priv, true);
981                 } else {
982                         IWL_ERR(priv, "CSA notif (fail) : channel %d\n",
983                               le16_to_cpu(csa->channel));
984                         iwl_chswitch_done(priv, false);
985                 }
986         }
987 }
988 EXPORT_SYMBOL(iwl_rx_csa);
989
990 #ifdef CONFIG_IWLWIFI_DEBUG
991 void iwl_print_rx_config_cmd(struct iwl_priv *priv,
992                              struct iwl_rxon_context *ctx)
993 {
994         struct iwl_rxon_cmd *rxon = &ctx->staging;
995
996         IWL_DEBUG_RADIO(priv, "RX CONFIG:\n");
997         iwl_print_hex_dump(priv, IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
998         IWL_DEBUG_RADIO(priv, "u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
999         IWL_DEBUG_RADIO(priv, "u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
1000         IWL_DEBUG_RADIO(priv, "u32 filter_flags: 0x%08x\n",
1001                         le32_to_cpu(rxon->filter_flags));
1002         IWL_DEBUG_RADIO(priv, "u8 dev_type: 0x%x\n", rxon->dev_type);
1003         IWL_DEBUG_RADIO(priv, "u8 ofdm_basic_rates: 0x%02x\n",
1004                         rxon->ofdm_basic_rates);
1005         IWL_DEBUG_RADIO(priv, "u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
1006         IWL_DEBUG_RADIO(priv, "u8[6] node_addr: %pM\n", rxon->node_addr);
1007         IWL_DEBUG_RADIO(priv, "u8[6] bssid_addr: %pM\n", rxon->bssid_addr);
1008         IWL_DEBUG_RADIO(priv, "u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
1009 }
1010 EXPORT_SYMBOL(iwl_print_rx_config_cmd);
1011 #endif
1012 /**
1013  * iwl_irq_handle_error - called for HW or SW error interrupt from card
1014  */
1015 void iwl_irq_handle_error(struct iwl_priv *priv)
1016 {
1017         /* Set the FW error flag -- cleared on iwl_down */
1018         set_bit(STATUS_FW_ERROR, &priv->status);
1019
1020         /* Cancel currently queued command. */
1021         clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
1022
1023         IWL_ERR(priv, "Loaded firmware version: %s\n",
1024                 priv->hw->wiphy->fw_version);
1025
1026         priv->cfg->ops->lib->dump_nic_error_log(priv);
1027         if (priv->cfg->ops->lib->dump_csr)
1028                 priv->cfg->ops->lib->dump_csr(priv);
1029         if (priv->cfg->ops->lib->dump_fh)
1030                 priv->cfg->ops->lib->dump_fh(priv, NULL, false);
1031         priv->cfg->ops->lib->dump_nic_event_log(priv, false, NULL, false);
1032 #ifdef CONFIG_IWLWIFI_DEBUG
1033         if (iwl_get_debug_level(priv) & IWL_DL_FW_ERRORS)
1034                 iwl_print_rx_config_cmd(priv,
1035                                         &priv->contexts[IWL_RXON_CTX_BSS]);
1036 #endif
1037
1038         wake_up_interruptible(&priv->wait_command_queue);
1039
1040         /* Keep the restart process from trying to send host
1041          * commands by clearing the INIT status bit */
1042         clear_bit(STATUS_READY, &priv->status);
1043
1044         if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
1045                 IWL_DEBUG(priv, IWL_DL_FW_ERRORS,
1046                           "Restarting adapter due to uCode error.\n");
1047
1048                 if (priv->cfg->mod_params->restart_fw)
1049                         queue_work(priv->workqueue, &priv->restart);
1050         }
1051 }
1052 EXPORT_SYMBOL(iwl_irq_handle_error);
1053
1054 static int iwl_apm_stop_master(struct iwl_priv *priv)
1055 {
1056         int ret = 0;
1057
1058         /* stop device's busmaster DMA activity */
1059         iwl_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_STOP_MASTER);
1060
1061         ret = iwl_poll_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_MASTER_DISABLED,
1062                         CSR_RESET_REG_FLAG_MASTER_DISABLED, 100);
1063         if (ret)
1064                 IWL_WARN(priv, "Master Disable Timed Out, 100 usec\n");
1065
1066         IWL_DEBUG_INFO(priv, "stop master\n");
1067
1068         return ret;
1069 }
1070
1071 void iwl_apm_stop(struct iwl_priv *priv)
1072 {
1073         IWL_DEBUG_INFO(priv, "Stop card, put in low power state\n");
1074
1075         /* Stop device's DMA activity */
1076         iwl_apm_stop_master(priv);
1077
1078         /* Reset the entire device */
1079         iwl_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
1080
1081         udelay(10);
1082
1083         /*
1084          * Clear "initialization complete" bit to move adapter from
1085          * D0A* (powered-up Active) --> D0U* (Uninitialized) state.
1086          */
1087         iwl_clear_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
1088 }
1089 EXPORT_SYMBOL(iwl_apm_stop);
1090
1091
1092 /*
1093  * Start up NIC's basic functionality after it has been reset
1094  * (e.g. after platform boot, or shutdown via iwl_apm_stop())
1095  * NOTE:  This does not load uCode nor start the embedded processor
1096  */
1097 int iwl_apm_init(struct iwl_priv *priv)
1098 {
1099         int ret = 0;
1100         u16 lctl;
1101
1102         IWL_DEBUG_INFO(priv, "Init card's basic functions\n");
1103
1104         /*
1105          * Use "set_bit" below rather than "write", to preserve any hardware
1106          * bits already set by default after reset.
1107          */
1108
1109         /* Disable L0S exit timer (platform NMI Work/Around) */
1110         iwl_set_bit(priv, CSR_GIO_CHICKEN_BITS,
1111                           CSR_GIO_CHICKEN_BITS_REG_BIT_DIS_L0S_EXIT_TIMER);
1112
1113         /*
1114          * Disable L0s without affecting L1;
1115          *  don't wait for ICH L0s (ICH bug W/A)
1116          */
1117         iwl_set_bit(priv, CSR_GIO_CHICKEN_BITS,
1118                           CSR_GIO_CHICKEN_BITS_REG_BIT_L1A_NO_L0S_RX);
1119
1120         /* Set FH wait threshold to maximum (HW error during stress W/A) */
1121         iwl_set_bit(priv, CSR_DBG_HPET_MEM_REG, CSR_DBG_HPET_MEM_REG_VAL);
1122
1123         /*
1124          * Enable HAP INTA (interrupt from management bus) to
1125          * wake device's PCI Express link L1a -> L0s
1126          * NOTE:  This is no-op for 3945 (non-existant bit)
1127          */
1128         iwl_set_bit(priv, CSR_HW_IF_CONFIG_REG,
1129                                     CSR_HW_IF_CONFIG_REG_BIT_HAP_WAKE_L1A);
1130
1131         /*
1132          * HW bug W/A for instability in PCIe bus L0->L0S->L1 transition.
1133          * Check if BIOS (or OS) enabled L1-ASPM on this device.
1134          * If so (likely), disable L0S, so device moves directly L0->L1;
1135          *    costs negligible amount of power savings.
1136          * If not (unlikely), enable L0S, so there is at least some
1137          *    power savings, even without L1.
1138          */
1139         if (priv->cfg->base_params->set_l0s) {
1140                 lctl = iwl_pcie_link_ctl(priv);
1141                 if ((lctl & PCI_CFG_LINK_CTRL_VAL_L1_EN) ==
1142                                         PCI_CFG_LINK_CTRL_VAL_L1_EN) {
1143                         /* L1-ASPM enabled; disable(!) L0S  */
1144                         iwl_set_bit(priv, CSR_GIO_REG,
1145                                         CSR_GIO_REG_VAL_L0S_ENABLED);
1146                         IWL_DEBUG_POWER(priv, "L1 Enabled; Disabling L0S\n");
1147                 } else {
1148                         /* L1-ASPM disabled; enable(!) L0S */
1149                         iwl_clear_bit(priv, CSR_GIO_REG,
1150                                         CSR_GIO_REG_VAL_L0S_ENABLED);
1151                         IWL_DEBUG_POWER(priv, "L1 Disabled; Enabling L0S\n");
1152                 }
1153         }
1154
1155         /* Configure analog phase-lock-loop before activating to D0A */
1156         if (priv->cfg->base_params->pll_cfg_val)
1157                 iwl_set_bit(priv, CSR_ANA_PLL_CFG,
1158                             priv->cfg->base_params->pll_cfg_val);
1159
1160         /*
1161          * Set "initialization complete" bit to move adapter from
1162          * D0U* --> D0A* (powered-up active) state.
1163          */
1164         iwl_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
1165
1166         /*
1167          * Wait for clock stabilization; once stabilized, access to
1168          * device-internal resources is supported, e.g. iwl_write_prph()
1169          * and accesses to uCode SRAM.
1170          */
1171         ret = iwl_poll_bit(priv, CSR_GP_CNTRL,
1172                         CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
1173                         CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25000);
1174         if (ret < 0) {
1175                 IWL_DEBUG_INFO(priv, "Failed to init the card\n");
1176                 goto out;
1177         }
1178
1179         /*
1180          * Enable DMA and BSM (if used) clocks, wait for them to stabilize.
1181          * BSM (Boostrap State Machine) is only in 3945 and 4965;
1182          * later devices (i.e. 5000 and later) have non-volatile SRAM,
1183          * and don't need BSM to restore data after power-saving sleep.
1184          *
1185          * Write to "CLK_EN_REG"; "1" bits enable clocks, while "0" bits
1186          * do not disable clocks.  This preserves any hardware bits already
1187          * set by default in "CLK_CTRL_REG" after reset.
1188          */
1189         if (priv->cfg->base_params->use_bsm)
1190                 iwl_write_prph(priv, APMG_CLK_EN_REG,
1191                         APMG_CLK_VAL_DMA_CLK_RQT | APMG_CLK_VAL_BSM_CLK_RQT);
1192         else
1193                 iwl_write_prph(priv, APMG_CLK_EN_REG,
1194                         APMG_CLK_VAL_DMA_CLK_RQT);
1195         udelay(20);
1196
1197         /* Disable L1-Active */
1198         iwl_set_bits_prph(priv, APMG_PCIDEV_STT_REG,
1199                           APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
1200
1201 out:
1202         return ret;
1203 }
1204 EXPORT_SYMBOL(iwl_apm_init);
1205
1206
1207 int iwl_set_tx_power(struct iwl_priv *priv, s8 tx_power, bool force)
1208 {
1209         int ret = 0;
1210         s8 prev_tx_power = priv->tx_power_user_lmt;
1211
1212         if (tx_power < IWLAGN_TX_POWER_TARGET_POWER_MIN) {
1213                 IWL_WARN(priv,
1214                          "Requested user TXPOWER %d below lower limit %d.\n",
1215                          tx_power,
1216                          IWLAGN_TX_POWER_TARGET_POWER_MIN);
1217                 return -EINVAL;
1218         }
1219
1220         if (tx_power > priv->tx_power_device_lmt) {
1221                 IWL_WARN(priv,
1222                         "Requested user TXPOWER %d above upper limit %d.\n",
1223                          tx_power, priv->tx_power_device_lmt);
1224                 return -EINVAL;
1225         }
1226
1227         if (priv->tx_power_user_lmt != tx_power)
1228                 force = true;
1229
1230         /* if nic is not up don't send command */
1231         if (iwl_is_ready_rf(priv)) {
1232                 priv->tx_power_user_lmt = tx_power;
1233                 if (force && priv->cfg->ops->lib->send_tx_power)
1234                         ret = priv->cfg->ops->lib->send_tx_power(priv);
1235                 else if (!priv->cfg->ops->lib->send_tx_power)
1236                         ret = -EOPNOTSUPP;
1237                 /*
1238                  * if fail to set tx_power, restore the orig. tx power
1239                  */
1240                 if (ret)
1241                         priv->tx_power_user_lmt = prev_tx_power;
1242         }
1243
1244         /*
1245          * Even this is an async host command, the command
1246          * will always report success from uCode
1247          * So once driver can placing the command into the queue
1248          * successfully, driver can use priv->tx_power_user_lmt
1249          * to reflect the current tx power
1250          */
1251         return ret;
1252 }
1253 EXPORT_SYMBOL(iwl_set_tx_power);
1254
1255 irqreturn_t iwl_isr_legacy(int irq, void *data)
1256 {
1257         struct iwl_priv *priv = data;
1258         u32 inta, inta_mask;
1259         u32 inta_fh;
1260         unsigned long flags;
1261         if (!priv)
1262                 return IRQ_NONE;
1263
1264         spin_lock_irqsave(&priv->lock, flags);
1265
1266         /* Disable (but don't clear!) interrupts here to avoid
1267          *    back-to-back ISRs and sporadic interrupts from our NIC.
1268          * If we have something to service, the tasklet will re-enable ints.
1269          * If we *don't* have something, we'll re-enable before leaving here. */
1270         inta_mask = iwl_read32(priv, CSR_INT_MASK);  /* just for debug */
1271         iwl_write32(priv, CSR_INT_MASK, 0x00000000);
1272
1273         /* Discover which interrupts are active/pending */
1274         inta = iwl_read32(priv, CSR_INT);
1275         inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
1276
1277         /* Ignore interrupt if there's nothing in NIC to service.
1278          * This may be due to IRQ shared with another device,
1279          * or due to sporadic interrupts thrown from our NIC. */
1280         if (!inta && !inta_fh) {
1281                 IWL_DEBUG_ISR(priv, "Ignore interrupt, inta == 0, inta_fh == 0\n");
1282                 goto none;
1283         }
1284
1285         if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
1286                 /* Hardware disappeared. It might have already raised
1287                  * an interrupt */
1288                 IWL_WARN(priv, "HARDWARE GONE?? INTA == 0x%08x\n", inta);
1289                 goto unplugged;
1290         }
1291
1292         IWL_DEBUG_ISR(priv, "ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
1293                       inta, inta_mask, inta_fh);
1294
1295         inta &= ~CSR_INT_BIT_SCD;
1296
1297         /* iwl_irq_tasklet() will service interrupts and re-enable them */
1298         if (likely(inta || inta_fh))
1299                 tasklet_schedule(&priv->irq_tasklet);
1300
1301  unplugged:
1302         spin_unlock_irqrestore(&priv->lock, flags);
1303         return IRQ_HANDLED;
1304
1305  none:
1306         /* re-enable interrupts here since we don't have anything to service. */
1307         /* only Re-enable if diabled by irq */
1308         if (test_bit(STATUS_INT_ENABLED, &priv->status))
1309                 iwl_enable_interrupts(priv);
1310         spin_unlock_irqrestore(&priv->lock, flags);
1311         return IRQ_NONE;
1312 }
1313 EXPORT_SYMBOL(iwl_isr_legacy);
1314
1315 void iwl_send_bt_config(struct iwl_priv *priv)
1316 {
1317         struct iwl_bt_cmd bt_cmd = {
1318                 .lead_time = BT_LEAD_TIME_DEF,
1319                 .max_kill = BT_MAX_KILL_DEF,
1320                 .kill_ack_mask = 0,
1321                 .kill_cts_mask = 0,
1322         };
1323
1324         if (!bt_coex_active)
1325                 bt_cmd.flags = BT_COEX_DISABLE;
1326         else
1327                 bt_cmd.flags = BT_COEX_ENABLE;
1328
1329         IWL_DEBUG_INFO(priv, "BT coex %s\n",
1330                 (bt_cmd.flags == BT_COEX_DISABLE) ? "disable" : "active");
1331
1332         if (iwl_send_cmd_pdu(priv, REPLY_BT_CONFIG,
1333                              sizeof(struct iwl_bt_cmd), &bt_cmd))
1334                 IWL_ERR(priv, "failed to send BT Coex Config\n");
1335 }
1336 EXPORT_SYMBOL(iwl_send_bt_config);
1337
1338 int iwl_send_statistics_request(struct iwl_priv *priv, u8 flags, bool clear)
1339 {
1340         struct iwl_statistics_cmd statistics_cmd = {
1341                 .configuration_flags =
1342                         clear ? IWL_STATS_CONF_CLEAR_STATS : 0,
1343         };
1344
1345         if (flags & CMD_ASYNC)
1346                 return iwl_send_cmd_pdu_async(priv, REPLY_STATISTICS_CMD,
1347                                                sizeof(struct iwl_statistics_cmd),
1348                                                &statistics_cmd, NULL);
1349         else
1350                 return iwl_send_cmd_pdu(priv, REPLY_STATISTICS_CMD,
1351                                         sizeof(struct iwl_statistics_cmd),
1352                                         &statistics_cmd);
1353 }
1354 EXPORT_SYMBOL(iwl_send_statistics_request);
1355
1356 void iwl_rx_pm_sleep_notif(struct iwl_priv *priv,
1357                            struct iwl_rx_mem_buffer *rxb)
1358 {
1359 #ifdef CONFIG_IWLWIFI_DEBUG
1360         struct iwl_rx_packet *pkt = rxb_addr(rxb);
1361         struct iwl_sleep_notification *sleep = &(pkt->u.sleep_notif);
1362         IWL_DEBUG_RX(priv, "sleep mode: %d, src: %d\n",
1363                      sleep->pm_sleep_mode, sleep->pm_wakeup_src);
1364 #endif
1365 }
1366 EXPORT_SYMBOL(iwl_rx_pm_sleep_notif);
1367
1368 void iwl_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
1369                                       struct iwl_rx_mem_buffer *rxb)
1370 {
1371         struct iwl_rx_packet *pkt = rxb_addr(rxb);
1372         u32 len = le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
1373         IWL_DEBUG_RADIO(priv, "Dumping %d bytes of unhandled "
1374                         "notification for %s:\n", len,
1375                         get_cmd_string(pkt->hdr.cmd));
1376         iwl_print_hex_dump(priv, IWL_DL_RADIO, pkt->u.raw, len);
1377 }
1378 EXPORT_SYMBOL(iwl_rx_pm_debug_statistics_notif);
1379
1380 void iwl_rx_reply_error(struct iwl_priv *priv,
1381                         struct iwl_rx_mem_buffer *rxb)
1382 {
1383         struct iwl_rx_packet *pkt = rxb_addr(rxb);
1384
1385         IWL_ERR(priv, "Error Reply type 0x%08X cmd %s (0x%02X) "
1386                 "seq 0x%04X ser 0x%08X\n",
1387                 le32_to_cpu(pkt->u.err_resp.error_type),
1388                 get_cmd_string(pkt->u.err_resp.cmd_id),
1389                 pkt->u.err_resp.cmd_id,
1390                 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
1391                 le32_to_cpu(pkt->u.err_resp.error_info));
1392 }
1393 EXPORT_SYMBOL(iwl_rx_reply_error);
1394
1395 void iwl_clear_isr_stats(struct iwl_priv *priv)
1396 {
1397         memset(&priv->isr_stats, 0, sizeof(priv->isr_stats));
1398 }
1399
1400 int iwl_mac_conf_tx(struct ieee80211_hw *hw, u16 queue,
1401                            const struct ieee80211_tx_queue_params *params)
1402 {
1403         struct iwl_priv *priv = hw->priv;
1404         struct iwl_rxon_context *ctx;
1405         unsigned long flags;
1406         int q;
1407
1408         IWL_DEBUG_MAC80211(priv, "enter\n");
1409
1410         if (!iwl_is_ready_rf(priv)) {
1411                 IWL_DEBUG_MAC80211(priv, "leave - RF not ready\n");
1412                 return -EIO;
1413         }
1414
1415         if (queue >= AC_NUM) {
1416                 IWL_DEBUG_MAC80211(priv, "leave - queue >= AC_NUM %d\n", queue);
1417                 return 0;
1418         }
1419
1420         q = AC_NUM - 1 - queue;
1421
1422         spin_lock_irqsave(&priv->lock, flags);
1423
1424         /*
1425          * MULTI-FIXME
1426          * This may need to be done per interface in nl80211/cfg80211/mac80211.
1427          */
1428         for_each_context(priv, ctx) {
1429                 ctx->qos_data.def_qos_parm.ac[q].cw_min =
1430                         cpu_to_le16(params->cw_min);
1431                 ctx->qos_data.def_qos_parm.ac[q].cw_max =
1432                         cpu_to_le16(params->cw_max);
1433                 ctx->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
1434                 ctx->qos_data.def_qos_parm.ac[q].edca_txop =
1435                                 cpu_to_le16((params->txop * 32));
1436
1437                 ctx->qos_data.def_qos_parm.ac[q].reserved1 = 0;
1438         }
1439
1440         spin_unlock_irqrestore(&priv->lock, flags);
1441
1442         IWL_DEBUG_MAC80211(priv, "leave\n");
1443         return 0;
1444 }
1445 EXPORT_SYMBOL(iwl_mac_conf_tx);
1446
1447 int iwl_mac_tx_last_beacon(struct ieee80211_hw *hw)
1448 {
1449         struct iwl_priv *priv = hw->priv;
1450
1451         return priv->ibss_manager == IWL_IBSS_MANAGER;
1452 }
1453 EXPORT_SYMBOL_GPL(iwl_mac_tx_last_beacon);
1454
1455 static void iwl_ht_conf(struct iwl_priv *priv,
1456                         struct ieee80211_vif *vif)
1457 {
1458         struct iwl_ht_config *ht_conf = &priv->current_ht_config;
1459         struct ieee80211_sta *sta;
1460         struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
1461         struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
1462
1463         IWL_DEBUG_MAC80211(priv, "enter:\n");
1464
1465         if (!ctx->ht.enabled)
1466                 return;
1467
1468         ctx->ht.protection =
1469                 bss_conf->ht_operation_mode & IEEE80211_HT_OP_MODE_PROTECTION;
1470         ctx->ht.non_gf_sta_present =
1471                 !!(bss_conf->ht_operation_mode & IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT);
1472
1473         ht_conf->single_chain_sufficient = false;
1474
1475         switch (vif->type) {
1476         case NL80211_IFTYPE_STATION:
1477                 rcu_read_lock();
1478                 sta = ieee80211_find_sta(vif, bss_conf->bssid);
1479                 if (sta) {
1480                         struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
1481                         int maxstreams;
1482
1483                         maxstreams = (ht_cap->mcs.tx_params &
1484                                       IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
1485                                         >> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT;
1486                         maxstreams += 1;
1487
1488                         if ((ht_cap->mcs.rx_mask[1] == 0) &&
1489                             (ht_cap->mcs.rx_mask[2] == 0))
1490                                 ht_conf->single_chain_sufficient = true;
1491                         if (maxstreams <= 1)
1492                                 ht_conf->single_chain_sufficient = true;
1493                 } else {
1494                         /*
1495                          * If at all, this can only happen through a race
1496                          * when the AP disconnects us while we're still
1497                          * setting up the connection, in that case mac80211
1498                          * will soon tell us about that.
1499                          */
1500                         ht_conf->single_chain_sufficient = true;
1501                 }
1502                 rcu_read_unlock();
1503                 break;
1504         case NL80211_IFTYPE_ADHOC:
1505                 ht_conf->single_chain_sufficient = true;
1506                 break;
1507         default:
1508                 break;
1509         }
1510
1511         IWL_DEBUG_MAC80211(priv, "leave\n");
1512 }
1513
1514 static inline void iwl_set_no_assoc(struct iwl_priv *priv,
1515                                     struct ieee80211_vif *vif)
1516 {
1517         struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
1518
1519         iwl_led_disassociate(priv);
1520         /*
1521          * inform the ucode that there is no longer an
1522          * association and that no more packets should be
1523          * sent
1524          */
1525         ctx->staging.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
1526         ctx->staging.assoc_id = 0;
1527         iwlcore_commit_rxon(priv, ctx);
1528 }
1529
1530 static void iwlcore_beacon_update(struct ieee80211_hw *hw,
1531                                   struct ieee80211_vif *vif)
1532 {
1533         struct iwl_priv *priv = hw->priv;
1534         unsigned long flags;
1535         __le64 timestamp;
1536         struct sk_buff *skb = ieee80211_beacon_get(hw, vif);
1537
1538         if (!skb)
1539                 return;
1540
1541         IWL_DEBUG_ASSOC(priv, "enter\n");
1542
1543         lockdep_assert_held(&priv->mutex);
1544
1545         if (!priv->beacon_ctx) {
1546                 IWL_ERR(priv, "update beacon but no beacon context!\n");
1547                 dev_kfree_skb(skb);
1548                 return;
1549         }
1550
1551         spin_lock_irqsave(&priv->lock, flags);
1552
1553         if (priv->beacon_skb)
1554                 dev_kfree_skb(priv->beacon_skb);
1555
1556         priv->beacon_skb = skb;
1557
1558         timestamp = ((struct ieee80211_mgmt *)skb->data)->u.beacon.timestamp;
1559         priv->timestamp = le64_to_cpu(timestamp);
1560
1561         IWL_DEBUG_ASSOC(priv, "leave\n");
1562
1563         spin_unlock_irqrestore(&priv->lock, flags);
1564
1565         if (!iwl_is_ready_rf(priv)) {
1566                 IWL_DEBUG_MAC80211(priv, "leave - RF not ready\n");
1567                 return;
1568         }
1569
1570         priv->cfg->ops->lib->post_associate(priv, priv->beacon_ctx->vif);
1571 }
1572
1573 void iwl_bss_info_changed(struct ieee80211_hw *hw,
1574                           struct ieee80211_vif *vif,
1575                           struct ieee80211_bss_conf *bss_conf,
1576                           u32 changes)
1577 {
1578         struct iwl_priv *priv = hw->priv;
1579         struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
1580         int ret;
1581
1582         IWL_DEBUG_MAC80211(priv, "changes = 0x%X\n", changes);
1583
1584         if (!iwl_is_alive(priv))
1585                 return;
1586
1587         mutex_lock(&priv->mutex);
1588
1589         if (changes & BSS_CHANGED_QOS) {
1590                 unsigned long flags;
1591
1592                 spin_lock_irqsave(&priv->lock, flags);
1593                 ctx->qos_data.qos_active = bss_conf->qos;
1594                 iwl_update_qos(priv, ctx);
1595                 spin_unlock_irqrestore(&priv->lock, flags);
1596         }
1597
1598         if (changes & BSS_CHANGED_BEACON_ENABLED) {
1599                 /*
1600                  * the add_interface code must make sure we only ever
1601                  * have a single interface that could be beaconing at
1602                  * any time.
1603                  */
1604                 if (vif->bss_conf.enable_beacon)
1605                         priv->beacon_ctx = ctx;
1606                 else
1607                         priv->beacon_ctx = NULL;
1608         }
1609
1610         if (changes & BSS_CHANGED_BEACON && vif->type == NL80211_IFTYPE_AP) {
1611                 dev_kfree_skb(priv->beacon_skb);
1612                 priv->beacon_skb = ieee80211_beacon_get(hw, vif);
1613         }
1614
1615         if (changes & BSS_CHANGED_BEACON_INT && vif->type == NL80211_IFTYPE_AP)
1616                 iwl_send_rxon_timing(priv, ctx);
1617
1618         if (changes & BSS_CHANGED_BSSID) {
1619                 IWL_DEBUG_MAC80211(priv, "BSSID %pM\n", bss_conf->bssid);
1620
1621                 /*
1622                  * If there is currently a HW scan going on in the
1623                  * background then we need to cancel it else the RXON
1624                  * below/in post_associate will fail.
1625                  */
1626                 if (iwl_scan_cancel_timeout(priv, 100)) {
1627                         IWL_WARN(priv, "Aborted scan still in progress after 100ms\n");
1628                         IWL_DEBUG_MAC80211(priv, "leaving - scan abort failed.\n");
1629                         mutex_unlock(&priv->mutex);
1630                         return;
1631                 }
1632
1633                 /* mac80211 only sets assoc when in STATION mode */
1634                 if (vif->type == NL80211_IFTYPE_ADHOC || bss_conf->assoc) {
1635                         memcpy(ctx->staging.bssid_addr,
1636                                bss_conf->bssid, ETH_ALEN);
1637
1638                         /* currently needed in a few places */
1639                         memcpy(priv->bssid, bss_conf->bssid, ETH_ALEN);
1640                 } else {
1641                         ctx->staging.filter_flags &=
1642                                 ~RXON_FILTER_ASSOC_MSK;
1643                 }
1644
1645         }
1646
1647         /*
1648          * This needs to be after setting the BSSID in case
1649          * mac80211 decides to do both changes at once because
1650          * it will invoke post_associate.
1651          */
1652         if (vif->type == NL80211_IFTYPE_ADHOC && changes & BSS_CHANGED_BEACON)
1653                 iwlcore_beacon_update(hw, vif);
1654
1655         if (changes & BSS_CHANGED_ERP_PREAMBLE) {
1656                 IWL_DEBUG_MAC80211(priv, "ERP_PREAMBLE %d\n",
1657                                    bss_conf->use_short_preamble);
1658                 if (bss_conf->use_short_preamble)
1659                         ctx->staging.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
1660                 else
1661                         ctx->staging.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
1662         }
1663
1664         if (changes & BSS_CHANGED_ERP_CTS_PROT) {
1665                 IWL_DEBUG_MAC80211(priv, "ERP_CTS %d\n", bss_conf->use_cts_prot);
1666                 if (bss_conf->use_cts_prot && (priv->band != IEEE80211_BAND_5GHZ))
1667                         ctx->staging.flags |= RXON_FLG_TGG_PROTECT_MSK;
1668                 else
1669                         ctx->staging.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
1670                 if (bss_conf->use_cts_prot)
1671                         ctx->staging.flags |= RXON_FLG_SELF_CTS_EN;
1672                 else
1673                         ctx->staging.flags &= ~RXON_FLG_SELF_CTS_EN;
1674         }
1675
1676         if (changes & BSS_CHANGED_BASIC_RATES) {
1677                 /* XXX use this information
1678                  *
1679                  * To do that, remove code from iwl_set_rate() and put something
1680                  * like this here:
1681                  *
1682                 if (A-band)
1683                         ctx->staging.ofdm_basic_rates =
1684                                 bss_conf->basic_rates;
1685                 else
1686                         ctx->staging.ofdm_basic_rates =
1687                                 bss_conf->basic_rates >> 4;
1688                         ctx->staging.cck_basic_rates =
1689                                 bss_conf->basic_rates & 0xF;
1690                  */
1691         }
1692
1693         if (changes & BSS_CHANGED_HT) {
1694                 iwl_ht_conf(priv, vif);
1695
1696                 if (priv->cfg->ops->hcmd->set_rxon_chain)
1697                         priv->cfg->ops->hcmd->set_rxon_chain(priv, ctx);
1698         }
1699
1700         if (changes & BSS_CHANGED_ASSOC) {
1701                 IWL_DEBUG_MAC80211(priv, "ASSOC %d\n", bss_conf->assoc);
1702                 if (bss_conf->assoc) {
1703                         priv->timestamp = bss_conf->timestamp;
1704
1705                         iwl_led_associate(priv);
1706
1707                         if (!iwl_is_rfkill(priv))
1708                                 priv->cfg->ops->lib->post_associate(priv, vif);
1709                 } else
1710                         iwl_set_no_assoc(priv, vif);
1711         }
1712
1713         if (changes && iwl_is_associated_ctx(ctx) && bss_conf->aid) {
1714                 IWL_DEBUG_MAC80211(priv, "Changes (%#x) while associated\n",
1715                                    changes);
1716                 ret = iwl_send_rxon_assoc(priv, ctx);
1717                 if (!ret) {
1718                         /* Sync active_rxon with latest change. */
1719                         memcpy((void *)&ctx->active,
1720                                 &ctx->staging,
1721                                 sizeof(struct iwl_rxon_cmd));
1722                 }
1723         }
1724
1725         if (changes & BSS_CHANGED_BEACON_ENABLED) {
1726                 if (vif->bss_conf.enable_beacon) {
1727                         memcpy(ctx->staging.bssid_addr,
1728                                bss_conf->bssid, ETH_ALEN);
1729                         memcpy(priv->bssid, bss_conf->bssid, ETH_ALEN);
1730                         iwl_led_associate(priv);
1731                         iwlcore_config_ap(priv, vif);
1732                 } else
1733                         iwl_set_no_assoc(priv, vif);
1734         }
1735
1736         if (changes & BSS_CHANGED_IBSS) {
1737                 ret = priv->cfg->ops->lib->manage_ibss_station(priv, vif,
1738                                                         bss_conf->ibss_joined);
1739                 if (ret)
1740                         IWL_ERR(priv, "failed to %s IBSS station %pM\n",
1741                                 bss_conf->ibss_joined ? "add" : "remove",
1742                                 bss_conf->bssid);
1743         }
1744
1745         if (changes & BSS_CHANGED_IDLE &&
1746             priv->cfg->ops->hcmd->set_pan_params) {
1747                 if (priv->cfg->ops->hcmd->set_pan_params(priv))
1748                         IWL_ERR(priv, "failed to update PAN params\n");
1749         }
1750
1751         mutex_unlock(&priv->mutex);
1752
1753         IWL_DEBUG_MAC80211(priv, "leave\n");
1754 }
1755 EXPORT_SYMBOL(iwl_bss_info_changed);
1756
1757 static int iwl_set_mode(struct iwl_priv *priv, struct ieee80211_vif *vif)
1758 {
1759         struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
1760
1761         iwl_connection_init_rx_config(priv, ctx);
1762
1763         if (priv->cfg->ops->hcmd->set_rxon_chain)
1764                 priv->cfg->ops->hcmd->set_rxon_chain(priv, ctx);
1765
1766         return iwlcore_commit_rxon(priv, ctx);
1767 }
1768
1769 int iwl_mac_add_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1770 {
1771         struct iwl_priv *priv = hw->priv;
1772         struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
1773         struct iwl_rxon_context *tmp, *ctx = NULL;
1774         int err = 0;
1775
1776         IWL_DEBUG_MAC80211(priv, "enter: type %d, addr %pM\n",
1777                            vif->type, vif->addr);
1778
1779         mutex_lock(&priv->mutex);
1780
1781         if (!iwl_is_ready_rf(priv)) {
1782                 IWL_WARN(priv, "Try to add interface when device not ready\n");
1783                 err = -EINVAL;
1784                 goto out;
1785         }
1786
1787         for_each_context(priv, tmp) {
1788                 u32 possible_modes =
1789                         tmp->interface_modes | tmp->exclusive_interface_modes;
1790
1791                 if (tmp->vif) {
1792                         /* check if this busy context is exclusive */
1793                         if (tmp->exclusive_interface_modes &
1794                                                 BIT(tmp->vif->type)) {
1795                                 err = -EINVAL;
1796                                 goto out;
1797                         }
1798                         continue;
1799                 }
1800
1801                 if (!(possible_modes & BIT(vif->type)))
1802                         continue;
1803
1804                 /* have maybe usable context w/o interface */
1805                 ctx = tmp;
1806                 break;
1807         }
1808
1809         if (!ctx) {
1810                 err = -EOPNOTSUPP;
1811                 goto out;
1812         }
1813
1814         vif_priv->ctx = ctx;
1815         ctx->vif = vif;
1816         /*
1817          * This variable will be correct only when there's just
1818          * a single context, but all code using it is for hardware
1819          * that supports only one context.
1820          */
1821         priv->iw_mode = vif->type;
1822
1823         ctx->is_active = true;
1824
1825         err = iwl_set_mode(priv, vif);
1826         if (err) {
1827                 if (!ctx->always_active)
1828                         ctx->is_active = false;
1829                 goto out_err;
1830         }
1831
1832         if (priv->cfg->bt_params &&
1833             priv->cfg->bt_params->advanced_bt_coexist &&
1834             vif->type == NL80211_IFTYPE_ADHOC) {
1835                 /*
1836                  * pretend to have high BT traffic as long as we
1837                  * are operating in IBSS mode, as this will cause
1838                  * the rate scaling etc. to behave as intended.
1839                  */
1840                 priv->bt_traffic_load = IWL_BT_COEX_TRAFFIC_LOAD_HIGH;
1841         }
1842
1843         goto out;
1844
1845  out_err:
1846         ctx->vif = NULL;
1847         priv->iw_mode = NL80211_IFTYPE_STATION;
1848  out:
1849         mutex_unlock(&priv->mutex);
1850
1851         IWL_DEBUG_MAC80211(priv, "leave\n");
1852         return err;
1853 }
1854 EXPORT_SYMBOL(iwl_mac_add_interface);
1855
1856 void iwl_mac_remove_interface(struct ieee80211_hw *hw,
1857                               struct ieee80211_vif *vif)
1858 {
1859         struct iwl_priv *priv = hw->priv;
1860         struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
1861
1862         IWL_DEBUG_MAC80211(priv, "enter\n");
1863
1864         mutex_lock(&priv->mutex);
1865
1866         WARN_ON(ctx->vif != vif);
1867         ctx->vif = NULL;
1868
1869         if (priv->scan_vif == vif) {
1870                 iwl_scan_cancel_timeout(priv, 200);
1871                 iwl_force_scan_end(priv);
1872         }
1873         iwl_set_mode(priv, vif);
1874
1875         if (!ctx->always_active)
1876                 ctx->is_active = false;
1877
1878         /*
1879          * When removing the IBSS interface, overwrite the
1880          * BT traffic load with the stored one from the last
1881          * notification, if any. If this is a device that
1882          * doesn't implement this, this has no effect since
1883          * both values are the same and zero.
1884          */
1885         if (vif->type == NL80211_IFTYPE_ADHOC)
1886                 priv->bt_traffic_load = priv->notif_bt_traffic_load;
1887
1888         memset(priv->bssid, 0, ETH_ALEN);
1889         mutex_unlock(&priv->mutex);
1890
1891         IWL_DEBUG_MAC80211(priv, "leave\n");
1892
1893 }
1894 EXPORT_SYMBOL(iwl_mac_remove_interface);
1895
1896 /**
1897  * iwl_mac_config - mac80211 config callback
1898  */
1899 int iwl_mac_config(struct ieee80211_hw *hw, u32 changed)
1900 {
1901         struct iwl_priv *priv = hw->priv;
1902         const struct iwl_channel_info *ch_info;
1903         struct ieee80211_conf *conf = &hw->conf;
1904         struct ieee80211_channel *channel = conf->channel;
1905         struct iwl_ht_config *ht_conf = &priv->current_ht_config;
1906         struct iwl_rxon_context *ctx;
1907         unsigned long flags = 0;
1908         int ret = 0;
1909         u16 ch;
1910         int scan_active = 0;
1911
1912         mutex_lock(&priv->mutex);
1913
1914         IWL_DEBUG_MAC80211(priv, "enter to channel %d changed 0x%X\n",
1915                                         channel->hw_value, changed);
1916
1917         if (unlikely(!priv->cfg->mod_params->disable_hw_scan &&
1918                         test_bit(STATUS_SCANNING, &priv->status))) {
1919                 scan_active = 1;
1920                 IWL_DEBUG_MAC80211(priv, "leave - scanning\n");
1921         }
1922
1923         if (changed & (IEEE80211_CONF_CHANGE_SMPS |
1924                        IEEE80211_CONF_CHANGE_CHANNEL)) {
1925                 /* mac80211 uses static for non-HT which is what we want */
1926                 priv->current_ht_config.smps = conf->smps_mode;
1927
1928                 /*
1929                  * Recalculate chain counts.
1930                  *
1931                  * If monitor mode is enabled then mac80211 will
1932                  * set up the SM PS mode to OFF if an HT channel is
1933                  * configured.
1934                  */
1935                 if (priv->cfg->ops->hcmd->set_rxon_chain)
1936                         for_each_context(priv, ctx)
1937                                 priv->cfg->ops->hcmd->set_rxon_chain(priv, ctx);
1938         }
1939
1940         /* during scanning mac80211 will delay channel setting until
1941          * scan finish with changed = 0
1942          */
1943         if (!changed || (changed & IEEE80211_CONF_CHANGE_CHANNEL)) {
1944                 if (scan_active)
1945                         goto set_ch_out;
1946
1947                 ch = channel->hw_value;
1948                 ch_info = iwl_get_channel_info(priv, channel->band, ch);
1949                 if (!is_channel_valid(ch_info)) {
1950                         IWL_DEBUG_MAC80211(priv, "leave - invalid channel\n");
1951                         ret = -EINVAL;
1952                         goto set_ch_out;
1953                 }
1954
1955                 spin_lock_irqsave(&priv->lock, flags);
1956
1957                 for_each_context(priv, ctx) {
1958                         /* Configure HT40 channels */
1959                         ctx->ht.enabled = conf_is_ht(conf);
1960                         if (ctx->ht.enabled) {
1961                                 if (conf_is_ht40_minus(conf)) {
1962                                         ctx->ht.extension_chan_offset =
1963                                                 IEEE80211_HT_PARAM_CHA_SEC_BELOW;
1964                                         ctx->ht.is_40mhz = true;
1965                                 } else if (conf_is_ht40_plus(conf)) {
1966                                         ctx->ht.extension_chan_offset =
1967                                                 IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
1968                                         ctx->ht.is_40mhz = true;
1969                                 } else {
1970                                         ctx->ht.extension_chan_offset =
1971                                                 IEEE80211_HT_PARAM_CHA_SEC_NONE;
1972                                         ctx->ht.is_40mhz = false;
1973                                 }
1974                         } else
1975                                 ctx->ht.is_40mhz = false;
1976
1977                         /*
1978                          * Default to no protection. Protection mode will
1979                          * later be set from BSS config in iwl_ht_conf
1980                          */
1981                         ctx->ht.protection = IEEE80211_HT_OP_MODE_PROTECTION_NONE;
1982
1983                         /* if we are switching from ht to 2.4 clear flags
1984                          * from any ht related info since 2.4 does not
1985                          * support ht */
1986                         if ((le16_to_cpu(ctx->staging.channel) != ch))
1987                                 ctx->staging.flags = 0;
1988
1989                         iwl_set_rxon_channel(priv, channel, ctx);
1990                         iwl_set_rxon_ht(priv, ht_conf);
1991
1992                         iwl_set_flags_for_band(priv, ctx, channel->band,
1993                                                ctx->vif);
1994                 }
1995
1996                 spin_unlock_irqrestore(&priv->lock, flags);
1997
1998                 if (priv->cfg->ops->lib->update_bcast_stations)
1999                         ret = priv->cfg->ops->lib->update_bcast_stations(priv);
2000
2001  set_ch_out:
2002                 /* The list of supported rates and rate mask can be different
2003                  * for each band; since the band may have changed, reset
2004                  * the rate mask to what mac80211 lists */
2005                 iwl_set_rate(priv);
2006         }
2007
2008         if (changed & (IEEE80211_CONF_CHANGE_PS |
2009                         IEEE80211_CONF_CHANGE_IDLE)) {
2010                 ret = iwl_power_update_mode(priv, false);
2011                 if (ret)
2012                         IWL_DEBUG_MAC80211(priv, "Error setting sleep level\n");
2013         }
2014
2015         if (changed & IEEE80211_CONF_CHANGE_POWER) {
2016                 IWL_DEBUG_MAC80211(priv, "TX Power old=%d new=%d\n",
2017                         priv->tx_power_user_lmt, conf->power_level);
2018
2019                 iwl_set_tx_power(priv, conf->power_level, false);
2020         }
2021
2022         if (!iwl_is_ready(priv)) {
2023                 IWL_DEBUG_MAC80211(priv, "leave - not ready\n");
2024                 goto out;
2025         }
2026
2027         if (scan_active)
2028                 goto out;
2029
2030         for_each_context(priv, ctx) {
2031                 if (memcmp(&ctx->active, &ctx->staging, sizeof(ctx->staging)))
2032                         iwlcore_commit_rxon(priv, ctx);
2033                 else
2034                         IWL_DEBUG_INFO(priv,
2035                                 "Not re-sending same RXON configuration.\n");
2036         }
2037
2038 out:
2039         IWL_DEBUG_MAC80211(priv, "leave\n");
2040         mutex_unlock(&priv->mutex);
2041         return ret;
2042 }
2043 EXPORT_SYMBOL(iwl_mac_config);
2044
2045 void iwl_mac_reset_tsf(struct ieee80211_hw *hw)
2046 {
2047         struct iwl_priv *priv = hw->priv;
2048         unsigned long flags;
2049         /* IBSS can only be the IWL_RXON_CTX_BSS context */
2050         struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
2051
2052         mutex_lock(&priv->mutex);
2053         IWL_DEBUG_MAC80211(priv, "enter\n");
2054
2055         spin_lock_irqsave(&priv->lock, flags);
2056         memset(&priv->current_ht_config, 0, sizeof(struct iwl_ht_config));
2057         spin_unlock_irqrestore(&priv->lock, flags);
2058
2059         spin_lock_irqsave(&priv->lock, flags);
2060
2061         /* new association get rid of ibss beacon skb */
2062         if (priv->beacon_skb)
2063                 dev_kfree_skb(priv->beacon_skb);
2064
2065         priv->beacon_skb = NULL;
2066
2067         priv->timestamp = 0;
2068
2069         spin_unlock_irqrestore(&priv->lock, flags);
2070
2071         iwl_scan_cancel_timeout(priv, 100);
2072         if (!iwl_is_ready_rf(priv)) {
2073                 IWL_DEBUG_MAC80211(priv, "leave - not ready\n");
2074                 mutex_unlock(&priv->mutex);
2075                 return;
2076         }
2077
2078         /* we are restarting association process
2079          * clear RXON_FILTER_ASSOC_MSK bit
2080          */
2081         ctx->staging.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
2082         iwlcore_commit_rxon(priv, ctx);
2083
2084         iwl_set_rate(priv);
2085
2086         mutex_unlock(&priv->mutex);
2087
2088         IWL_DEBUG_MAC80211(priv, "leave\n");
2089 }
2090 EXPORT_SYMBOL(iwl_mac_reset_tsf);
2091
2092 int iwl_alloc_txq_mem(struct iwl_priv *priv)
2093 {
2094         if (!priv->txq)
2095                 priv->txq = kzalloc(
2096                         sizeof(struct iwl_tx_queue) *
2097                                 priv->cfg->base_params->num_of_queues,
2098                         GFP_KERNEL);
2099         if (!priv->txq) {
2100                 IWL_ERR(priv, "Not enough memory for txq\n");
2101                 return -ENOMEM;
2102         }
2103         return 0;
2104 }
2105 EXPORT_SYMBOL(iwl_alloc_txq_mem);
2106
2107 void iwl_free_txq_mem(struct iwl_priv *priv)
2108 {
2109         kfree(priv->txq);
2110         priv->txq = NULL;
2111 }
2112 EXPORT_SYMBOL(iwl_free_txq_mem);
2113
2114 #ifdef CONFIG_IWLWIFI_DEBUGFS
2115
2116 #define IWL_TRAFFIC_DUMP_SIZE   (IWL_TRAFFIC_ENTRY_SIZE * IWL_TRAFFIC_ENTRIES)
2117
2118 void iwl_reset_traffic_log(struct iwl_priv *priv)
2119 {
2120         priv->tx_traffic_idx = 0;
2121         priv->rx_traffic_idx = 0;
2122         if (priv->tx_traffic)
2123                 memset(priv->tx_traffic, 0, IWL_TRAFFIC_DUMP_SIZE);
2124         if (priv->rx_traffic)
2125                 memset(priv->rx_traffic, 0, IWL_TRAFFIC_DUMP_SIZE);
2126 }
2127
2128 int iwl_alloc_traffic_mem(struct iwl_priv *priv)
2129 {
2130         u32 traffic_size = IWL_TRAFFIC_DUMP_SIZE;
2131
2132         if (iwl_debug_level & IWL_DL_TX) {
2133                 if (!priv->tx_traffic) {
2134                         priv->tx_traffic =
2135                                 kzalloc(traffic_size, GFP_KERNEL);
2136                         if (!priv->tx_traffic)
2137                                 return -ENOMEM;
2138                 }
2139         }
2140         if (iwl_debug_level & IWL_DL_RX) {
2141                 if (!priv->rx_traffic) {
2142                         priv->rx_traffic =
2143                                 kzalloc(traffic_size, GFP_KERNEL);
2144                         if (!priv->rx_traffic)
2145                                 return -ENOMEM;
2146                 }
2147         }
2148         iwl_reset_traffic_log(priv);
2149         return 0;
2150 }
2151 EXPORT_SYMBOL(iwl_alloc_traffic_mem);
2152
2153 void iwl_free_traffic_mem(struct iwl_priv *priv)
2154 {
2155         kfree(priv->tx_traffic);
2156         priv->tx_traffic = NULL;
2157
2158         kfree(priv->rx_traffic);
2159         priv->rx_traffic = NULL;
2160 }
2161 EXPORT_SYMBOL(iwl_free_traffic_mem);
2162
2163 void iwl_dbg_log_tx_data_frame(struct iwl_priv *priv,
2164                       u16 length, struct ieee80211_hdr *header)
2165 {
2166         __le16 fc;
2167         u16 len;
2168
2169         if (likely(!(iwl_debug_level & IWL_DL_TX)))
2170                 return;
2171
2172         if (!priv->tx_traffic)
2173                 return;
2174
2175         fc = header->frame_control;
2176         if (ieee80211_is_data(fc)) {
2177                 len = (length > IWL_TRAFFIC_ENTRY_SIZE)
2178                        ? IWL_TRAFFIC_ENTRY_SIZE : length;
2179                 memcpy((priv->tx_traffic +
2180                        (priv->tx_traffic_idx * IWL_TRAFFIC_ENTRY_SIZE)),
2181                        header, len);
2182                 priv->tx_traffic_idx =
2183                         (priv->tx_traffic_idx + 1) % IWL_TRAFFIC_ENTRIES;
2184         }
2185 }
2186 EXPORT_SYMBOL(iwl_dbg_log_tx_data_frame);
2187
2188 void iwl_dbg_log_rx_data_frame(struct iwl_priv *priv,
2189                       u16 length, struct ieee80211_hdr *header)
2190 {
2191         __le16 fc;
2192         u16 len;
2193
2194         if (likely(!(iwl_debug_level & IWL_DL_RX)))
2195                 return;
2196
2197         if (!priv->rx_traffic)
2198                 return;
2199
2200         fc = header->frame_control;
2201         if (ieee80211_is_data(fc)) {
2202                 len = (length > IWL_TRAFFIC_ENTRY_SIZE)
2203                        ? IWL_TRAFFIC_ENTRY_SIZE : length;
2204                 memcpy((priv->rx_traffic +
2205                        (priv->rx_traffic_idx * IWL_TRAFFIC_ENTRY_SIZE)),
2206                        header, len);
2207                 priv->rx_traffic_idx =
2208                         (priv->rx_traffic_idx + 1) % IWL_TRAFFIC_ENTRIES;
2209         }
2210 }
2211 EXPORT_SYMBOL(iwl_dbg_log_rx_data_frame);
2212
2213 const char *get_mgmt_string(int cmd)
2214 {
2215         switch (cmd) {
2216                 IWL_CMD(MANAGEMENT_ASSOC_REQ);
2217                 IWL_CMD(MANAGEMENT_ASSOC_RESP);
2218                 IWL_CMD(MANAGEMENT_REASSOC_REQ);
2219                 IWL_CMD(MANAGEMENT_REASSOC_RESP);
2220                 IWL_CMD(MANAGEMENT_PROBE_REQ);
2221                 IWL_CMD(MANAGEMENT_PROBE_RESP);
2222                 IWL_CMD(MANAGEMENT_BEACON);
2223                 IWL_CMD(MANAGEMENT_ATIM);
2224                 IWL_CMD(MANAGEMENT_DISASSOC);
2225                 IWL_CMD(MANAGEMENT_AUTH);
2226                 IWL_CMD(MANAGEMENT_DEAUTH);
2227                 IWL_CMD(MANAGEMENT_ACTION);
2228         default:
2229                 return "UNKNOWN";
2230
2231         }
2232 }
2233
2234 const char *get_ctrl_string(int cmd)
2235 {
2236         switch (cmd) {
2237                 IWL_CMD(CONTROL_BACK_REQ);
2238                 IWL_CMD(CONTROL_BACK);
2239                 IWL_CMD(CONTROL_PSPOLL);
2240                 IWL_CMD(CONTROL_RTS);
2241                 IWL_CMD(CONTROL_CTS);
2242                 IWL_CMD(CONTROL_ACK);
2243                 IWL_CMD(CONTROL_CFEND);
2244                 IWL_CMD(CONTROL_CFENDACK);
2245         default:
2246                 return "UNKNOWN";
2247
2248         }
2249 }
2250
2251 void iwl_clear_traffic_stats(struct iwl_priv *priv)
2252 {
2253         memset(&priv->tx_stats, 0, sizeof(struct traffic_stats));
2254         memset(&priv->rx_stats, 0, sizeof(struct traffic_stats));
2255         priv->led_tpt = 0;
2256 }
2257
2258 /*
2259  * if CONFIG_IWLWIFI_DEBUGFS defined, iwl_update_stats function will
2260  * record all the MGMT, CTRL and DATA pkt for both TX and Rx pass.
2261  * Use debugFs to display the rx/rx_statistics
2262  * if CONFIG_IWLWIFI_DEBUGFS not being defined, then no MGMT and CTRL
2263  * information will be recorded, but DATA pkt still will be recorded
2264  * for the reason of iwl_led.c need to control the led blinking based on
2265  * number of tx and rx data.
2266  *
2267  */
2268 void iwl_update_stats(struct iwl_priv *priv, bool is_tx, __le16 fc, u16 len)
2269 {
2270         struct traffic_stats    *stats;
2271
2272         if (is_tx)
2273                 stats = &priv->tx_stats;
2274         else
2275                 stats = &priv->rx_stats;
2276
2277         if (ieee80211_is_mgmt(fc)) {
2278                 switch (fc & cpu_to_le16(IEEE80211_FCTL_STYPE)) {
2279                 case cpu_to_le16(IEEE80211_STYPE_ASSOC_REQ):
2280                         stats->mgmt[MANAGEMENT_ASSOC_REQ]++;
2281                         break;
2282                 case cpu_to_le16(IEEE80211_STYPE_ASSOC_RESP):
2283                         stats->mgmt[MANAGEMENT_ASSOC_RESP]++;
2284                         break;
2285                 case cpu_to_le16(IEEE80211_STYPE_REASSOC_REQ):
2286                         stats->mgmt[MANAGEMENT_REASSOC_REQ]++;
2287                         break;
2288                 case cpu_to_le16(IEEE80211_STYPE_REASSOC_RESP):
2289                         stats->mgmt[MANAGEMENT_REASSOC_RESP]++;
2290                         break;
2291                 case cpu_to_le16(IEEE80211_STYPE_PROBE_REQ):
2292                         stats->mgmt[MANAGEMENT_PROBE_REQ]++;
2293                         break;
2294                 case cpu_to_le16(IEEE80211_STYPE_PROBE_RESP):
2295                         stats->mgmt[MANAGEMENT_PROBE_RESP]++;
2296                         break;
2297                 case cpu_to_le16(IEEE80211_STYPE_BEACON):
2298                         stats->mgmt[MANAGEMENT_BEACON]++;
2299                         break;
2300                 case cpu_to_le16(IEEE80211_STYPE_ATIM):
2301                         stats->mgmt[MANAGEMENT_ATIM]++;
2302                         break;
2303                 case cpu_to_le16(IEEE80211_STYPE_DISASSOC):
2304                         stats->mgmt[MANAGEMENT_DISASSOC]++;
2305                         break;
2306                 case cpu_to_le16(IEEE80211_STYPE_AUTH):
2307                         stats->mgmt[MANAGEMENT_AUTH]++;
2308                         break;
2309                 case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
2310                         stats->mgmt[MANAGEMENT_DEAUTH]++;
2311                         break;
2312                 case cpu_to_le16(IEEE80211_STYPE_ACTION):
2313                         stats->mgmt[MANAGEMENT_ACTION]++;
2314                         break;
2315                 }
2316         } else if (ieee80211_is_ctl(fc)) {
2317                 switch (fc & cpu_to_le16(IEEE80211_FCTL_STYPE)) {
2318                 case cpu_to_le16(IEEE80211_STYPE_BACK_REQ):
2319                         stats->ctrl[CONTROL_BACK_REQ]++;
2320                         break;
2321                 case cpu_to_le16(IEEE80211_STYPE_BACK):
2322                         stats->ctrl[CONTROL_BACK]++;
2323                         break;
2324                 case cpu_to_le16(IEEE80211_STYPE_PSPOLL):
2325                         stats->ctrl[CONTROL_PSPOLL]++;
2326                         break;
2327                 case cpu_to_le16(IEEE80211_STYPE_RTS):
2328                         stats->ctrl[CONTROL_RTS]++;
2329                         break;
2330                 case cpu_to_le16(IEEE80211_STYPE_CTS):
2331                         stats->ctrl[CONTROL_CTS]++;
2332                         break;
2333                 case cpu_to_le16(IEEE80211_STYPE_ACK):
2334                         stats->ctrl[CONTROL_ACK]++;
2335                         break;
2336                 case cpu_to_le16(IEEE80211_STYPE_CFEND):
2337                         stats->ctrl[CONTROL_CFEND]++;
2338                         break;
2339                 case cpu_to_le16(IEEE80211_STYPE_CFENDACK):
2340                         stats->ctrl[CONTROL_CFENDACK]++;
2341                         break;
2342                 }
2343         } else {
2344                 /* data */
2345                 stats->data_cnt++;
2346                 stats->data_bytes += len;
2347         }
2348         iwl_leds_background(priv);
2349 }
2350 EXPORT_SYMBOL(iwl_update_stats);
2351 #endif
2352
2353 static void iwl_force_rf_reset(struct iwl_priv *priv)
2354 {
2355         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2356                 return;
2357
2358         if (!iwl_is_any_associated(priv)) {
2359                 IWL_DEBUG_SCAN(priv, "force reset rejected: not associated\n");
2360                 return;
2361         }
2362         /*
2363          * There is no easy and better way to force reset the radio,
2364          * the only known method is switching channel which will force to
2365          * reset and tune the radio.
2366          * Use internal short scan (single channel) operation to should
2367          * achieve this objective.
2368          * Driver should reset the radio when number of consecutive missed
2369          * beacon, or any other uCode error condition detected.
2370          */
2371         IWL_DEBUG_INFO(priv, "perform radio reset.\n");
2372         iwl_internal_short_hw_scan(priv);
2373 }
2374
2375
2376 int iwl_force_reset(struct iwl_priv *priv, int mode, bool external)
2377 {
2378         struct iwl_force_reset *force_reset;
2379
2380         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2381                 return -EINVAL;
2382
2383         if (mode >= IWL_MAX_FORCE_RESET) {
2384                 IWL_DEBUG_INFO(priv, "invalid reset request.\n");
2385                 return -EINVAL;
2386         }
2387         force_reset = &priv->force_reset[mode];
2388         force_reset->reset_request_count++;
2389         if (!external) {
2390                 if (force_reset->last_force_reset_jiffies &&
2391                     time_after(force_reset->last_force_reset_jiffies +
2392                     force_reset->reset_duration, jiffies)) {
2393                         IWL_DEBUG_INFO(priv, "force reset rejected\n");
2394                         force_reset->reset_reject_count++;
2395                         return -EAGAIN;
2396                 }
2397         }
2398         force_reset->reset_success_count++;
2399         force_reset->last_force_reset_jiffies = jiffies;
2400         IWL_DEBUG_INFO(priv, "perform force reset (%d)\n", mode);
2401         switch (mode) {
2402         case IWL_RF_RESET:
2403                 iwl_force_rf_reset(priv);
2404                 break;
2405         case IWL_FW_RESET:
2406                 /*
2407                  * if the request is from external(ex: debugfs),
2408                  * then always perform the request in regardless the module
2409                  * parameter setting
2410                  * if the request is from internal (uCode error or driver
2411                  * detect failure), then fw_restart module parameter
2412                  * need to be check before performing firmware reload
2413                  */
2414                 if (!external && !priv->cfg->mod_params->restart_fw) {
2415                         IWL_DEBUG_INFO(priv, "Cancel firmware reload based on "
2416                                        "module parameter setting\n");
2417                         break;
2418                 }
2419                 IWL_ERR(priv, "On demand firmware reload\n");
2420                 /* Set the FW error flag -- cleared on iwl_down */
2421                 set_bit(STATUS_FW_ERROR, &priv->status);
2422                 wake_up_interruptible(&priv->wait_command_queue);
2423                 /*
2424                  * Keep the restart process from trying to send host
2425                  * commands by clearing the INIT status bit
2426                  */
2427                 clear_bit(STATUS_READY, &priv->status);
2428                 queue_work(priv->workqueue, &priv->restart);
2429                 break;
2430         }
2431         return 0;
2432 }
2433
2434 /**
2435  * iwl_bg_monitor_recover - Timer callback to check for stuck queue and recover
2436  *
2437  * During normal condition (no queue is stuck), the timer is continually set to
2438  * execute every monitor_recover_period milliseconds after the last timer
2439  * expired.  When the queue read_ptr is at the same place, the timer is
2440  * shorten to 100mSecs.  This is
2441  *      1) to reduce the chance that the read_ptr may wrap around (not stuck)
2442  *      2) to detect the stuck queues quicker before the station and AP can
2443  *      disassociate each other.
2444  *
2445  * This function monitors all the tx queues and recover from it if any
2446  * of the queues are stuck.
2447  * 1. It first check the cmd queue for stuck conditions.  If it is stuck,
2448  *      it will recover by resetting the firmware and return.
2449  * 2. Then, it checks for station association.  If it associates it will check
2450  *      other queues.  If any queue is stuck, it will recover by resetting
2451  *      the firmware.
2452  * Note: It the number of times the queue read_ptr to be at the same place to
2453  *      be MAX_REPEAT+1 in order to consider to be stuck.
2454  */
2455 /*
2456  * The maximum number of times the read pointer of the tx queue at the
2457  * same place without considering to be stuck.
2458  */
2459 #define MAX_REPEAT      (2)
2460 static int iwl_check_stuck_queue(struct iwl_priv *priv, int cnt)
2461 {
2462         struct iwl_tx_queue *txq;
2463         struct iwl_queue *q;
2464
2465         txq = &priv->txq[cnt];
2466         q = &txq->q;
2467         /* queue is empty, skip */
2468         if (q->read_ptr == q->write_ptr)
2469                 return 0;
2470
2471         if (q->read_ptr == q->last_read_ptr) {
2472                 /* a queue has not been read from last time */
2473                 if (q->repeat_same_read_ptr > MAX_REPEAT) {
2474                         IWL_ERR(priv,
2475                                 "queue %d stuck %d time. Fw reload.\n",
2476                                 q->id, q->repeat_same_read_ptr);
2477                         q->repeat_same_read_ptr = 0;
2478                         iwl_force_reset(priv, IWL_FW_RESET, false);
2479                 } else {
2480                         q->repeat_same_read_ptr++;
2481                         IWL_DEBUG_RADIO(priv,
2482                                         "queue %d, not read %d time\n",
2483                                         q->id,
2484                                         q->repeat_same_read_ptr);
2485                         mod_timer(&priv->monitor_recover,
2486                                 jiffies + msecs_to_jiffies(
2487                                 IWL_ONE_HUNDRED_MSECS));
2488                         return 1;
2489                 }
2490         } else {
2491                 q->last_read_ptr = q->read_ptr;
2492                 q->repeat_same_read_ptr = 0;
2493         }
2494         return 0;
2495 }
2496
2497 void iwl_bg_monitor_recover(unsigned long data)
2498 {
2499         struct iwl_priv *priv = (struct iwl_priv *)data;
2500         int cnt;
2501
2502         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2503                 return;
2504
2505         /* monitor and check for stuck cmd queue */
2506         if (iwl_check_stuck_queue(priv, priv->cmd_queue))
2507                 return;
2508
2509         /* monitor and check for other stuck queues */
2510         if (iwl_is_any_associated(priv)) {
2511                 for (cnt = 0; cnt < priv->hw_params.max_txq_num; cnt++) {
2512                         /* skip as we already checked the command queue */
2513                         if (cnt == priv->cmd_queue)
2514                                 continue;
2515                         if (iwl_check_stuck_queue(priv, cnt))
2516                                 return;
2517                 }
2518         }
2519         if (priv->cfg->base_params->monitor_recover_period) {
2520                 /*
2521                  * Reschedule the timer to occur in
2522                  * priv->cfg->base_params->monitor_recover_period
2523                  */
2524                 mod_timer(&priv->monitor_recover, jiffies + msecs_to_jiffies(
2525                           priv->cfg->base_params->monitor_recover_period));
2526         }
2527 }
2528 EXPORT_SYMBOL(iwl_bg_monitor_recover);
2529
2530
2531 /*
2532  * extended beacon time format
2533  * time in usec will be changed into a 32-bit value in extended:internal format
2534  * the extended part is the beacon counts
2535  * the internal part is the time in usec within one beacon interval
2536  */
2537 u32 iwl_usecs_to_beacons(struct iwl_priv *priv, u32 usec, u32 beacon_interval)
2538 {
2539         u32 quot;
2540         u32 rem;
2541         u32 interval = beacon_interval * TIME_UNIT;
2542
2543         if (!interval || !usec)
2544                 return 0;
2545
2546         quot = (usec / interval) &
2547                 (iwl_beacon_time_mask_high(priv,
2548                 priv->hw_params.beacon_time_tsf_bits) >>
2549                 priv->hw_params.beacon_time_tsf_bits);
2550         rem = (usec % interval) & iwl_beacon_time_mask_low(priv,
2551                                    priv->hw_params.beacon_time_tsf_bits);
2552
2553         return (quot << priv->hw_params.beacon_time_tsf_bits) + rem;
2554 }
2555 EXPORT_SYMBOL(iwl_usecs_to_beacons);
2556
2557 /* base is usually what we get from ucode with each received frame,
2558  * the same as HW timer counter counting down
2559  */
2560 __le32 iwl_add_beacon_time(struct iwl_priv *priv, u32 base,
2561                            u32 addon, u32 beacon_interval)
2562 {
2563         u32 base_low = base & iwl_beacon_time_mask_low(priv,
2564                                         priv->hw_params.beacon_time_tsf_bits);
2565         u32 addon_low = addon & iwl_beacon_time_mask_low(priv,
2566                                         priv->hw_params.beacon_time_tsf_bits);
2567         u32 interval = beacon_interval * TIME_UNIT;
2568         u32 res = (base & iwl_beacon_time_mask_high(priv,
2569                                 priv->hw_params.beacon_time_tsf_bits)) +
2570                                 (addon & iwl_beacon_time_mask_high(priv,
2571                                 priv->hw_params.beacon_time_tsf_bits));
2572
2573         if (base_low > addon_low)
2574                 res += base_low - addon_low;
2575         else if (base_low < addon_low) {
2576                 res += interval + base_low - addon_low;
2577                 res += (1 << priv->hw_params.beacon_time_tsf_bits);
2578         } else
2579                 res += (1 << priv->hw_params.beacon_time_tsf_bits);
2580
2581         return cpu_to_le32(res);
2582 }
2583 EXPORT_SYMBOL(iwl_add_beacon_time);
2584
2585 #ifdef CONFIG_PM
2586
2587 int iwl_pci_suspend(struct pci_dev *pdev, pm_message_t state)
2588 {
2589         struct iwl_priv *priv = pci_get_drvdata(pdev);
2590
2591         /*
2592          * This function is called when system goes into suspend state
2593          * mac80211 will call iwl_mac_stop() from the mac80211 suspend function
2594          * first but since iwl_mac_stop() has no knowledge of who the caller is,
2595          * it will not call apm_ops.stop() to stop the DMA operation.
2596          * Calling apm_ops.stop here to make sure we stop the DMA.
2597          */
2598         iwl_apm_stop(priv);
2599
2600         pci_save_state(pdev);
2601         pci_disable_device(pdev);
2602         pci_set_power_state(pdev, PCI_D3hot);
2603
2604         return 0;
2605 }
2606 EXPORT_SYMBOL(iwl_pci_suspend);
2607
2608 int iwl_pci_resume(struct pci_dev *pdev)
2609 {
2610         struct iwl_priv *priv = pci_get_drvdata(pdev);
2611         int ret;
2612         bool hw_rfkill = false;
2613
2614         /*
2615          * We disable the RETRY_TIMEOUT register (0x41) to keep
2616          * PCI Tx retries from interfering with C3 CPU state.
2617          */
2618         pci_write_config_byte(pdev, PCI_CFG_RETRY_TIMEOUT, 0x00);
2619
2620         pci_set_power_state(pdev, PCI_D0);
2621         ret = pci_enable_device(pdev);
2622         if (ret)
2623                 return ret;
2624         pci_restore_state(pdev);
2625         iwl_enable_interrupts(priv);
2626
2627         if (!(iwl_read32(priv, CSR_GP_CNTRL) &
2628                                 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
2629                 hw_rfkill = true;
2630
2631         if (hw_rfkill)
2632                 set_bit(STATUS_RF_KILL_HW, &priv->status);
2633         else
2634                 clear_bit(STATUS_RF_KILL_HW, &priv->status);
2635
2636         wiphy_rfkill_set_hw_state(priv->hw->wiphy, hw_rfkill);
2637
2638         return 0;
2639 }
2640 EXPORT_SYMBOL(iwl_pci_resume);
2641
2642 #endif /* CONFIG_PM */