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