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