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