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