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