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