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