OSDN Git Service

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