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