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