Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/teigland/dlm
[pandora-kernel.git] / drivers / net / wireless / iwlwifi / iwl-core.c
1 /******************************************************************************
2  *
3  * GPL LICENSE SUMMARY
4  *
5  * Copyright(c) 2008 - 2011 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 #include "iwl-agn.h"
45
46
47 /*
48  * set bt_coex_active to true, uCode will do kill/defer
49  * every time the priority line is asserted (BT is sending signals on the
50  * priority line in the PCIx).
51  * set bt_coex_active to false, uCode will ignore the BT activity and
52  * perform the normal operation
53  *
54  * User might experience transmit issue on some platform due to WiFi/BT
55  * co-exist problem. The possible behaviors are:
56  *   Able to scan and finding all the available AP
57  *   Not able to associate with any AP
58  * On those platforms, WiFi communication can be restored by set
59  * "bt_coex_active" module parameter to "false"
60  *
61  * default: bt_coex_active = true (BT_COEX_ENABLE)
62  */
63 bool bt_coex_active = true;
64 module_param(bt_coex_active, bool, S_IRUGO);
65 MODULE_PARM_DESC(bt_coex_active, "enable wifi/bluetooth co-exist");
66
67 u32 iwl_debug_level;
68
69 const u8 iwl_bcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
70
71 #define MAX_BIT_RATE_40_MHZ 150 /* Mbps */
72 #define MAX_BIT_RATE_20_MHZ 72 /* Mbps */
73 static void iwlcore_init_ht_hw_capab(const struct iwl_priv *priv,
74                               struct ieee80211_sta_ht_cap *ht_info,
75                               enum ieee80211_band band)
76 {
77         u16 max_bit_rate = 0;
78         u8 rx_chains_num = priv->hw_params.rx_chains_num;
79         u8 tx_chains_num = priv->hw_params.tx_chains_num;
80
81         ht_info->cap = 0;
82         memset(&ht_info->mcs, 0, sizeof(ht_info->mcs));
83
84         ht_info->ht_supported = true;
85
86         if (priv->cfg->ht_params &&
87             priv->cfg->ht_params->ht_greenfield_support)
88                 ht_info->cap |= IEEE80211_HT_CAP_GRN_FLD;
89         ht_info->cap |= IEEE80211_HT_CAP_SGI_20;
90         max_bit_rate = MAX_BIT_RATE_20_MHZ;
91         if (priv->hw_params.ht40_channel & BIT(band)) {
92                 ht_info->cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
93                 ht_info->cap |= IEEE80211_HT_CAP_SGI_40;
94                 ht_info->mcs.rx_mask[4] = 0x01;
95                 max_bit_rate = MAX_BIT_RATE_40_MHZ;
96         }
97
98         if (iwlagn_mod_params.amsdu_size_8K)
99                 ht_info->cap |= IEEE80211_HT_CAP_MAX_AMSDU;
100
101         ht_info->ampdu_factor = CFG_HT_RX_AMPDU_FACTOR_DEF;
102         if (priv->cfg->bt_params && priv->cfg->bt_params->ampdu_factor)
103                 ht_info->ampdu_factor = priv->cfg->bt_params->ampdu_factor;
104         ht_info->ampdu_density = CFG_HT_MPDU_DENSITY_DEF;
105         if (priv->cfg->bt_params && priv->cfg->bt_params->ampdu_density)
106                 ht_info->ampdu_density = priv->cfg->bt_params->ampdu_density;
107
108         ht_info->mcs.rx_mask[0] = 0xFF;
109         if (rx_chains_num >= 2)
110                 ht_info->mcs.rx_mask[1] = 0xFF;
111         if (rx_chains_num >= 3)
112                 ht_info->mcs.rx_mask[2] = 0xFF;
113
114         /* Highest supported Rx data rate */
115         max_bit_rate *= rx_chains_num;
116         WARN_ON(max_bit_rate & ~IEEE80211_HT_MCS_RX_HIGHEST_MASK);
117         ht_info->mcs.rx_highest = cpu_to_le16(max_bit_rate);
118
119         /* Tx MCS capabilities */
120         ht_info->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
121         if (tx_chains_num != rx_chains_num) {
122                 ht_info->mcs.tx_params |= IEEE80211_HT_MCS_TX_RX_DIFF;
123                 ht_info->mcs.tx_params |= ((tx_chains_num - 1) <<
124                                 IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT);
125         }
126 }
127
128 /**
129  * iwlcore_init_geos - Initialize mac80211's geo/channel info based from eeprom
130  */
131 int iwlcore_init_geos(struct iwl_priv *priv)
132 {
133         struct iwl_channel_info *ch;
134         struct ieee80211_supported_band *sband;
135         struct ieee80211_channel *channels;
136         struct ieee80211_channel *geo_ch;
137         struct ieee80211_rate *rates;
138         int i = 0;
139         s8 max_tx_power = IWLAGN_TX_POWER_TARGET_POWER_MIN;
140
141         if (priv->bands[IEEE80211_BAND_2GHZ].n_bitrates ||
142             priv->bands[IEEE80211_BAND_5GHZ].n_bitrates) {
143                 IWL_DEBUG_INFO(priv, "Geography modes already initialized.\n");
144                 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
145                 return 0;
146         }
147
148         channels = kzalloc(sizeof(struct ieee80211_channel) *
149                            priv->channel_count, GFP_KERNEL);
150         if (!channels)
151                 return -ENOMEM;
152
153         rates = kzalloc((sizeof(struct ieee80211_rate) * IWL_RATE_COUNT_LEGACY),
154                         GFP_KERNEL);
155         if (!rates) {
156                 kfree(channels);
157                 return -ENOMEM;
158         }
159
160         /* 5.2GHz channels start after the 2.4GHz channels */
161         sband = &priv->bands[IEEE80211_BAND_5GHZ];
162         sband->channels = &channels[ARRAY_SIZE(iwl_eeprom_band_1)];
163         /* just OFDM */
164         sband->bitrates = &rates[IWL_FIRST_OFDM_RATE];
165         sband->n_bitrates = IWL_RATE_COUNT_LEGACY - IWL_FIRST_OFDM_RATE;
166
167         if (priv->cfg->sku & IWL_SKU_N)
168                 iwlcore_init_ht_hw_capab(priv, &sband->ht_cap,
169                                          IEEE80211_BAND_5GHZ);
170
171         sband = &priv->bands[IEEE80211_BAND_2GHZ];
172         sband->channels = channels;
173         /* OFDM & CCK */
174         sband->bitrates = rates;
175         sband->n_bitrates = IWL_RATE_COUNT_LEGACY;
176
177         if (priv->cfg->sku & IWL_SKU_N)
178                 iwlcore_init_ht_hw_capab(priv, &sband->ht_cap,
179                                          IEEE80211_BAND_2GHZ);
180
181         priv->ieee_channels = channels;
182         priv->ieee_rates = rates;
183
184         for (i = 0;  i < priv->channel_count; i++) {
185                 ch = &priv->channel_info[i];
186
187                 /* FIXME: might be removed if scan is OK */
188                 if (!is_channel_valid(ch))
189                         continue;
190
191                 sband =  &priv->bands[ch->band];
192
193                 geo_ch = &sband->channels[sband->n_channels++];
194
195                 geo_ch->center_freq =
196                         ieee80211_channel_to_frequency(ch->channel, ch->band);
197                 geo_ch->max_power = ch->max_power_avg;
198                 geo_ch->max_antenna_gain = 0xff;
199                 geo_ch->hw_value = ch->channel;
200
201                 if (is_channel_valid(ch)) {
202                         if (!(ch->flags & EEPROM_CHANNEL_IBSS))
203                                 geo_ch->flags |= IEEE80211_CHAN_NO_IBSS;
204
205                         if (!(ch->flags & EEPROM_CHANNEL_ACTIVE))
206                                 geo_ch->flags |= IEEE80211_CHAN_PASSIVE_SCAN;
207
208                         if (ch->flags & EEPROM_CHANNEL_RADAR)
209                                 geo_ch->flags |= IEEE80211_CHAN_RADAR;
210
211                         geo_ch->flags |= ch->ht40_extension_channel;
212
213                         if (ch->max_power_avg > max_tx_power)
214                                 max_tx_power = ch->max_power_avg;
215                 } else {
216                         geo_ch->flags |= IEEE80211_CHAN_DISABLED;
217                 }
218
219                 IWL_DEBUG_INFO(priv, "Channel %d Freq=%d[%sGHz] %s flag=0x%X\n",
220                                 ch->channel, geo_ch->center_freq,
221                                 is_channel_a_band(ch) ?  "5.2" : "2.4",
222                                 geo_ch->flags & IEEE80211_CHAN_DISABLED ?
223                                 "restricted" : "valid",
224                                  geo_ch->flags);
225         }
226
227         priv->tx_power_device_lmt = max_tx_power;
228         priv->tx_power_user_lmt = max_tx_power;
229         priv->tx_power_next = max_tx_power;
230
231         if ((priv->bands[IEEE80211_BAND_5GHZ].n_channels == 0) &&
232              priv->cfg->sku & IWL_SKU_A) {
233                 IWL_INFO(priv, "Incorrectly detected BG card as ABG. "
234                         "Please send your PCI ID 0x%04X:0x%04X to maintainer.\n",
235                            priv->pci_dev->device,
236                            priv->pci_dev->subsystem_device);
237                 priv->cfg->sku &= ~IWL_SKU_A;
238         }
239
240         IWL_INFO(priv, "Tunable channels: %d 802.11bg, %d 802.11a channels\n",
241                    priv->bands[IEEE80211_BAND_2GHZ].n_channels,
242                    priv->bands[IEEE80211_BAND_5GHZ].n_channels);
243
244         set_bit(STATUS_GEO_CONFIGURED, &priv->status);
245
246         return 0;
247 }
248
249 /*
250  * iwlcore_free_geos - undo allocations in iwlcore_init_geos
251  */
252 void iwlcore_free_geos(struct iwl_priv *priv)
253 {
254         kfree(priv->ieee_channels);
255         kfree(priv->ieee_rates);
256         clear_bit(STATUS_GEO_CONFIGURED, &priv->status);
257 }
258
259 static bool iwl_is_channel_extension(struct iwl_priv *priv,
260                                      enum ieee80211_band band,
261                                      u16 channel, u8 extension_chan_offset)
262 {
263         const struct iwl_channel_info *ch_info;
264
265         ch_info = iwl_get_channel_info(priv, band, channel);
266         if (!is_channel_valid(ch_info))
267                 return false;
268
269         if (extension_chan_offset == IEEE80211_HT_PARAM_CHA_SEC_ABOVE)
270                 return !(ch_info->ht40_extension_channel &
271                                         IEEE80211_CHAN_NO_HT40PLUS);
272         else if (extension_chan_offset == IEEE80211_HT_PARAM_CHA_SEC_BELOW)
273                 return !(ch_info->ht40_extension_channel &
274                                         IEEE80211_CHAN_NO_HT40MINUS);
275
276         return false;
277 }
278
279 bool iwl_is_ht40_tx_allowed(struct iwl_priv *priv,
280                             struct iwl_rxon_context *ctx,
281                             struct ieee80211_sta_ht_cap *ht_cap)
282 {
283         if (!ctx->ht.enabled || !ctx->ht.is_40mhz)
284                 return false;
285
286         /*
287          * We do not check for IEEE80211_HT_CAP_SUP_WIDTH_20_40
288          * the bit will not set if it is pure 40MHz case
289          */
290         if (ht_cap && !ht_cap->ht_supported)
291                 return false;
292
293 #ifdef CONFIG_IWLWIFI_DEBUGFS
294         if (priv->disable_ht40)
295                 return false;
296 #endif
297
298         return iwl_is_channel_extension(priv, priv->band,
299                         le16_to_cpu(ctx->staging.channel),
300                         ctx->ht.extension_chan_offset);
301 }
302
303 static u16 iwl_adjust_beacon_interval(u16 beacon_val, u16 max_beacon_val)
304 {
305         u16 new_val;
306         u16 beacon_factor;
307
308         /*
309          * If mac80211 hasn't given us a beacon interval, program
310          * the default into the device (not checking this here
311          * would cause the adjustment below to return the maximum
312          * value, which may break PAN.)
313          */
314         if (!beacon_val)
315                 return DEFAULT_BEACON_INTERVAL;
316
317         /*
318          * If the beacon interval we obtained from the peer
319          * is too large, we'll have to wake up more often
320          * (and in IBSS case, we'll beacon too much)
321          *
322          * For example, if max_beacon_val is 4096, and the
323          * requested beacon interval is 7000, we'll have to
324          * use 3500 to be able to wake up on the beacons.
325          *
326          * This could badly influence beacon detection stats.
327          */
328
329         beacon_factor = (beacon_val + max_beacon_val) / max_beacon_val;
330         new_val = beacon_val / beacon_factor;
331
332         if (!new_val)
333                 new_val = max_beacon_val;
334
335         return new_val;
336 }
337
338 int iwl_send_rxon_timing(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
339 {
340         u64 tsf;
341         s32 interval_tm, rem;
342         struct ieee80211_conf *conf = NULL;
343         u16 beacon_int;
344         struct ieee80211_vif *vif = ctx->vif;
345
346         conf = ieee80211_get_hw_conf(priv->hw);
347
348         lockdep_assert_held(&priv->mutex);
349
350         memset(&ctx->timing, 0, sizeof(struct iwl_rxon_time_cmd));
351
352         ctx->timing.timestamp = cpu_to_le64(priv->timestamp);
353         ctx->timing.listen_interval = cpu_to_le16(conf->listen_interval);
354
355         beacon_int = vif ? vif->bss_conf.beacon_int : 0;
356
357         /*
358          * TODO: For IBSS we need to get atim_window from mac80211,
359          *       for now just always use 0
360          */
361         ctx->timing.atim_window = 0;
362
363         if (ctx->ctxid == IWL_RXON_CTX_PAN &&
364             (!ctx->vif || ctx->vif->type != NL80211_IFTYPE_STATION) &&
365             iwl_is_associated(priv, IWL_RXON_CTX_BSS) &&
366             priv->contexts[IWL_RXON_CTX_BSS].vif &&
367             priv->contexts[IWL_RXON_CTX_BSS].vif->bss_conf.beacon_int) {
368                 ctx->timing.beacon_interval =
369                         priv->contexts[IWL_RXON_CTX_BSS].timing.beacon_interval;
370                 beacon_int = le16_to_cpu(ctx->timing.beacon_interval);
371         } else if (ctx->ctxid == IWL_RXON_CTX_BSS &&
372                    iwl_is_associated(priv, IWL_RXON_CTX_PAN) &&
373                    priv->contexts[IWL_RXON_CTX_PAN].vif &&
374                    priv->contexts[IWL_RXON_CTX_PAN].vif->bss_conf.beacon_int &&
375                    (!iwl_is_associated_ctx(ctx) || !ctx->vif ||
376                     !ctx->vif->bss_conf.beacon_int)) {
377                 ctx->timing.beacon_interval =
378                         priv->contexts[IWL_RXON_CTX_PAN].timing.beacon_interval;
379                 beacon_int = le16_to_cpu(ctx->timing.beacon_interval);
380         } else {
381                 beacon_int = iwl_adjust_beacon_interval(beacon_int,
382                                 priv->hw_params.max_beacon_itrvl * TIME_UNIT);
383                 ctx->timing.beacon_interval = cpu_to_le16(beacon_int);
384         }
385
386         tsf = priv->timestamp; /* tsf is modifed by do_div: copy it */
387         interval_tm = beacon_int * TIME_UNIT;
388         rem = do_div(tsf, interval_tm);
389         ctx->timing.beacon_init_val = cpu_to_le32(interval_tm - rem);
390
391         ctx->timing.dtim_period = vif ? (vif->bss_conf.dtim_period ?: 1) : 1;
392
393         IWL_DEBUG_ASSOC(priv,
394                         "beacon interval %d beacon timer %d beacon tim %d\n",
395                         le16_to_cpu(ctx->timing.beacon_interval),
396                         le32_to_cpu(ctx->timing.beacon_init_val),
397                         le16_to_cpu(ctx->timing.atim_window));
398
399         return iwl_send_cmd_pdu(priv, ctx->rxon_timing_cmd,
400                                 sizeof(ctx->timing), &ctx->timing);
401 }
402
403 void iwl_set_rxon_hwcrypto(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
404                            int hw_decrypt)
405 {
406         struct iwl_rxon_cmd *rxon = &ctx->staging;
407
408         if (hw_decrypt)
409                 rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
410         else
411                 rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
412
413 }
414
415 /* validate RXON structure is valid */
416 int iwl_check_rxon_cmd(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
417 {
418         struct iwl_rxon_cmd *rxon = &ctx->staging;
419         u32 errors = 0;
420
421         if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
422                 if (rxon->flags & RXON_FLG_TGJ_NARROW_BAND_MSK) {
423                         IWL_WARN(priv, "check 2.4G: wrong narrow\n");
424                         errors |= BIT(0);
425                 }
426                 if (rxon->flags & RXON_FLG_RADAR_DETECT_MSK) {
427                         IWL_WARN(priv, "check 2.4G: wrong radar\n");
428                         errors |= BIT(1);
429                 }
430         } else {
431                 if (!(rxon->flags & RXON_FLG_SHORT_SLOT_MSK)) {
432                         IWL_WARN(priv, "check 5.2G: not short slot!\n");
433                         errors |= BIT(2);
434                 }
435                 if (rxon->flags & RXON_FLG_CCK_MSK) {
436                         IWL_WARN(priv, "check 5.2G: CCK!\n");
437                         errors |= BIT(3);
438                 }
439         }
440         if ((rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1) {
441                 IWL_WARN(priv, "mac/bssid mcast!\n");
442                 errors |= BIT(4);
443         }
444
445         /* make sure basic rates 6Mbps and 1Mbps are supported */
446         if ((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0 &&
447             (rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0) {
448                 IWL_WARN(priv, "neither 1 nor 6 are basic\n");
449                 errors |= BIT(5);
450         }
451
452         if (le16_to_cpu(rxon->assoc_id) > 2007) {
453                 IWL_WARN(priv, "aid > 2007\n");
454                 errors |= BIT(6);
455         }
456
457         if ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
458                         == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK)) {
459                 IWL_WARN(priv, "CCK and short slot\n");
460                 errors |= BIT(7);
461         }
462
463         if ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
464                         == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK)) {
465                 IWL_WARN(priv, "CCK and auto detect");
466                 errors |= BIT(8);
467         }
468
469         if ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
470                             RXON_FLG_TGG_PROTECT_MSK)) ==
471                             RXON_FLG_TGG_PROTECT_MSK) {
472                 IWL_WARN(priv, "TGg but no auto-detect\n");
473                 errors |= BIT(9);
474         }
475
476         if (rxon->channel == 0) {
477                 IWL_WARN(priv, "zero channel is invalid\n");
478                 errors |= BIT(10);
479         }
480
481         WARN(errors, "Invalid RXON (%#x), channel %d",
482              errors, le16_to_cpu(rxon->channel));
483
484         return errors ? -EINVAL : 0;
485 }
486
487 /**
488  * iwl_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
489  * @priv: staging_rxon is compared to active_rxon
490  *
491  * If the RXON structure is changing enough to require a new tune,
492  * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
493  * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
494  */
495 int iwl_full_rxon_required(struct iwl_priv *priv,
496                            struct iwl_rxon_context *ctx)
497 {
498         const struct iwl_rxon_cmd *staging = &ctx->staging;
499         const struct iwl_rxon_cmd *active = &ctx->active;
500
501 #define CHK(cond)                                                       \
502         if ((cond)) {                                                   \
503                 IWL_DEBUG_INFO(priv, "need full RXON - " #cond "\n");   \
504                 return 1;                                               \
505         }
506
507 #define CHK_NEQ(c1, c2)                                         \
508         if ((c1) != (c2)) {                                     \
509                 IWL_DEBUG_INFO(priv, "need full RXON - "        \
510                                #c1 " != " #c2 " - %d != %d\n",  \
511                                (c1), (c2));                     \
512                 return 1;                                       \
513         }
514
515         /* These items are only settable from the full RXON command */
516         CHK(!iwl_is_associated_ctx(ctx));
517         CHK(compare_ether_addr(staging->bssid_addr, active->bssid_addr));
518         CHK(compare_ether_addr(staging->node_addr, active->node_addr));
519         CHK(compare_ether_addr(staging->wlap_bssid_addr,
520                                 active->wlap_bssid_addr));
521         CHK_NEQ(staging->dev_type, active->dev_type);
522         CHK_NEQ(staging->channel, active->channel);
523         CHK_NEQ(staging->air_propagation, active->air_propagation);
524         CHK_NEQ(staging->ofdm_ht_single_stream_basic_rates,
525                 active->ofdm_ht_single_stream_basic_rates);
526         CHK_NEQ(staging->ofdm_ht_dual_stream_basic_rates,
527                 active->ofdm_ht_dual_stream_basic_rates);
528         CHK_NEQ(staging->ofdm_ht_triple_stream_basic_rates,
529                 active->ofdm_ht_triple_stream_basic_rates);
530         CHK_NEQ(staging->assoc_id, active->assoc_id);
531
532         /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
533          * be updated with the RXON_ASSOC command -- however only some
534          * flag transitions are allowed using RXON_ASSOC */
535
536         /* Check if we are not switching bands */
537         CHK_NEQ(staging->flags & RXON_FLG_BAND_24G_MSK,
538                 active->flags & RXON_FLG_BAND_24G_MSK);
539
540         /* Check if we are switching association toggle */
541         CHK_NEQ(staging->filter_flags & RXON_FILTER_ASSOC_MSK,
542                 active->filter_flags & RXON_FILTER_ASSOC_MSK);
543
544 #undef CHK
545 #undef CHK_NEQ
546
547         return 0;
548 }
549
550 u8 iwl_rate_get_lowest_plcp(struct iwl_priv *priv,
551                             struct iwl_rxon_context *ctx)
552 {
553         /*
554          * Assign the lowest rate -- should really get this from
555          * the beacon skb from mac80211.
556          */
557         if (ctx->staging.flags & RXON_FLG_BAND_24G_MSK)
558                 return IWL_RATE_1M_PLCP;
559         else
560                 return IWL_RATE_6M_PLCP;
561 }
562
563 static void _iwl_set_rxon_ht(struct iwl_priv *priv,
564                              struct iwl_ht_config *ht_conf,
565                              struct iwl_rxon_context *ctx)
566 {
567         struct iwl_rxon_cmd *rxon = &ctx->staging;
568
569         if (!ctx->ht.enabled) {
570                 rxon->flags &= ~(RXON_FLG_CHANNEL_MODE_MSK |
571                         RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK |
572                         RXON_FLG_HT40_PROT_MSK |
573                         RXON_FLG_HT_PROT_MSK);
574                 return;
575         }
576
577         /* FIXME: if the definition of ht.protection changed, the "translation"
578          * will be needed for rxon->flags
579          */
580         rxon->flags |= cpu_to_le32(ctx->ht.protection << RXON_FLG_HT_OPERATING_MODE_POS);
581
582         /* Set up channel bandwidth:
583          * 20 MHz only, 20/40 mixed or pure 40 if ht40 ok */
584         /* clear the HT channel mode before set the mode */
585         rxon->flags &= ~(RXON_FLG_CHANNEL_MODE_MSK |
586                          RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK);
587         if (iwl_is_ht40_tx_allowed(priv, ctx, NULL)) {
588                 /* pure ht40 */
589                 if (ctx->ht.protection == IEEE80211_HT_OP_MODE_PROTECTION_20MHZ) {
590                         rxon->flags |= RXON_FLG_CHANNEL_MODE_PURE_40;
591                         /* Note: control channel is opposite of extension channel */
592                         switch (ctx->ht.extension_chan_offset) {
593                         case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
594                                 rxon->flags &= ~RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
595                                 break;
596                         case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
597                                 rxon->flags |= RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
598                                 break;
599                         }
600                 } else {
601                         /* Note: control channel is opposite of extension channel */
602                         switch (ctx->ht.extension_chan_offset) {
603                         case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
604                                 rxon->flags &= ~(RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK);
605                                 rxon->flags |= RXON_FLG_CHANNEL_MODE_MIXED;
606                                 break;
607                         case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
608                                 rxon->flags |= RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
609                                 rxon->flags |= RXON_FLG_CHANNEL_MODE_MIXED;
610                                 break;
611                         case IEEE80211_HT_PARAM_CHA_SEC_NONE:
612                         default:
613                                 /* channel location only valid if in Mixed mode */
614                                 IWL_ERR(priv, "invalid extension channel offset\n");
615                                 break;
616                         }
617                 }
618         } else {
619                 rxon->flags |= RXON_FLG_CHANNEL_MODE_LEGACY;
620         }
621
622         if (priv->cfg->ops->hcmd->set_rxon_chain)
623                 priv->cfg->ops->hcmd->set_rxon_chain(priv, ctx);
624
625         IWL_DEBUG_ASSOC(priv, "rxon flags 0x%X operation mode :0x%X "
626                         "extension channel offset 0x%x\n",
627                         le32_to_cpu(rxon->flags), ctx->ht.protection,
628                         ctx->ht.extension_chan_offset);
629 }
630
631 void iwl_set_rxon_ht(struct iwl_priv *priv, struct iwl_ht_config *ht_conf)
632 {
633         struct iwl_rxon_context *ctx;
634
635         for_each_context(priv, ctx)
636                 _iwl_set_rxon_ht(priv, ht_conf, ctx);
637 }
638
639 /* Return valid, unused, channel for a passive scan to reset the RF */
640 u8 iwl_get_single_channel_number(struct iwl_priv *priv,
641                                  enum ieee80211_band band)
642 {
643         const struct iwl_channel_info *ch_info;
644         int i;
645         u8 channel = 0;
646         u8 min, max;
647         struct iwl_rxon_context *ctx;
648
649         if (band == IEEE80211_BAND_5GHZ) {
650                 min = 14;
651                 max = priv->channel_count;
652         } else {
653                 min = 0;
654                 max = 14;
655         }
656
657         for (i = min; i < max; i++) {
658                 bool busy = false;
659
660                 for_each_context(priv, ctx) {
661                         busy = priv->channel_info[i].channel ==
662                                 le16_to_cpu(ctx->staging.channel);
663                         if (busy)
664                                 break;
665                 }
666
667                 if (busy)
668                         continue;
669
670                 channel = priv->channel_info[i].channel;
671                 ch_info = iwl_get_channel_info(priv, band, channel);
672                 if (is_channel_valid(ch_info))
673                         break;
674         }
675
676         return channel;
677 }
678
679 /**
680  * iwl_set_rxon_channel - Set the band and channel values in staging RXON
681  * @ch: requested channel as a pointer to struct ieee80211_channel
682
683  * NOTE:  Does not commit to the hardware; it sets appropriate bit fields
684  * in the staging RXON flag structure based on the ch->band
685  */
686 int iwl_set_rxon_channel(struct iwl_priv *priv, struct ieee80211_channel *ch,
687                          struct iwl_rxon_context *ctx)
688 {
689         enum ieee80211_band band = ch->band;
690         u16 channel = ch->hw_value;
691
692         if ((le16_to_cpu(ctx->staging.channel) == channel) &&
693             (priv->band == band))
694                 return 0;
695
696         ctx->staging.channel = cpu_to_le16(channel);
697         if (band == IEEE80211_BAND_5GHZ)
698                 ctx->staging.flags &= ~RXON_FLG_BAND_24G_MSK;
699         else
700                 ctx->staging.flags |= RXON_FLG_BAND_24G_MSK;
701
702         priv->band = band;
703
704         IWL_DEBUG_INFO(priv, "Staging channel set to %d [%d]\n", channel, band);
705
706         return 0;
707 }
708
709 void iwl_set_flags_for_band(struct iwl_priv *priv,
710                             struct iwl_rxon_context *ctx,
711                             enum ieee80211_band band,
712                             struct ieee80211_vif *vif)
713 {
714         if (band == IEEE80211_BAND_5GHZ) {
715                 ctx->staging.flags &=
716                     ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
717                       | RXON_FLG_CCK_MSK);
718                 ctx->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
719         } else {
720                 /* Copied from iwl_post_associate() */
721                 if (vif && vif->bss_conf.use_short_slot)
722                         ctx->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
723                 else
724                         ctx->staging.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
725
726                 ctx->staging.flags |= RXON_FLG_BAND_24G_MSK;
727                 ctx->staging.flags |= RXON_FLG_AUTO_DETECT_MSK;
728                 ctx->staging.flags &= ~RXON_FLG_CCK_MSK;
729         }
730 }
731
732 /*
733  * initialize rxon structure with default values from eeprom
734  */
735 void iwl_connection_init_rx_config(struct iwl_priv *priv,
736                                    struct iwl_rxon_context *ctx)
737 {
738         const struct iwl_channel_info *ch_info;
739
740         memset(&ctx->staging, 0, sizeof(ctx->staging));
741
742         if (!ctx->vif) {
743                 ctx->staging.dev_type = ctx->unused_devtype;
744         } else switch (ctx->vif->type) {
745         case NL80211_IFTYPE_AP:
746                 ctx->staging.dev_type = ctx->ap_devtype;
747                 break;
748
749         case NL80211_IFTYPE_STATION:
750                 ctx->staging.dev_type = ctx->station_devtype;
751                 ctx->staging.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
752                 break;
753
754         case NL80211_IFTYPE_ADHOC:
755                 ctx->staging.dev_type = ctx->ibss_devtype;
756                 ctx->staging.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
757                 ctx->staging.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
758                                                   RXON_FILTER_ACCEPT_GRP_MSK;
759                 break;
760
761         default:
762                 IWL_ERR(priv, "Unsupported interface type %d\n",
763                         ctx->vif->type);
764                 break;
765         }
766
767 #if 0
768         /* TODO:  Figure out when short_preamble would be set and cache from
769          * that */
770         if (!hw_to_local(priv->hw)->short_preamble)
771                 ctx->staging.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
772         else
773                 ctx->staging.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
774 #endif
775
776         ch_info = iwl_get_channel_info(priv, priv->band,
777                                        le16_to_cpu(ctx->active.channel));
778
779         if (!ch_info)
780                 ch_info = &priv->channel_info[0];
781
782         ctx->staging.channel = cpu_to_le16(ch_info->channel);
783         priv->band = ch_info->band;
784
785         iwl_set_flags_for_band(priv, ctx, priv->band, ctx->vif);
786
787         ctx->staging.ofdm_basic_rates =
788             (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
789         ctx->staging.cck_basic_rates =
790             (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
791
792         /* clear both MIX and PURE40 mode flag */
793         ctx->staging.flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED |
794                                         RXON_FLG_CHANNEL_MODE_PURE_40);
795         if (ctx->vif)
796                 memcpy(ctx->staging.node_addr, ctx->vif->addr, ETH_ALEN);
797
798         ctx->staging.ofdm_ht_single_stream_basic_rates = 0xff;
799         ctx->staging.ofdm_ht_dual_stream_basic_rates = 0xff;
800         ctx->staging.ofdm_ht_triple_stream_basic_rates = 0xff;
801 }
802
803 void iwl_set_rate(struct iwl_priv *priv)
804 {
805         const struct ieee80211_supported_band *hw = NULL;
806         struct ieee80211_rate *rate;
807         struct iwl_rxon_context *ctx;
808         int i;
809
810         hw = iwl_get_hw_mode(priv, priv->band);
811         if (!hw) {
812                 IWL_ERR(priv, "Failed to set rate: unable to get hw mode\n");
813                 return;
814         }
815
816         priv->active_rate = 0;
817
818         for (i = 0; i < hw->n_bitrates; i++) {
819                 rate = &(hw->bitrates[i]);
820                 if (rate->hw_value < IWL_RATE_COUNT_LEGACY)
821                         priv->active_rate |= (1 << rate->hw_value);
822         }
823
824         IWL_DEBUG_RATE(priv, "Set active_rate = %0x\n", priv->active_rate);
825
826         for_each_context(priv, ctx) {
827                 ctx->staging.cck_basic_rates =
828                     (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
829
830                 ctx->staging.ofdm_basic_rates =
831                    (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
832         }
833 }
834
835 void iwl_chswitch_done(struct iwl_priv *priv, bool is_success)
836 {
837         /*
838          * MULTI-FIXME
839          * See iwl_mac_channel_switch.
840          */
841         struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
842
843         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
844                 return;
845
846         if (test_and_clear_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->status))
847                 ieee80211_chswitch_done(ctx->vif, is_success);
848 }
849
850 #ifdef CONFIG_IWLWIFI_DEBUG
851 void iwl_print_rx_config_cmd(struct iwl_priv *priv,
852                              struct iwl_rxon_context *ctx)
853 {
854         struct iwl_rxon_cmd *rxon = &ctx->staging;
855
856         IWL_DEBUG_RADIO(priv, "RX CONFIG:\n");
857         iwl_print_hex_dump(priv, IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
858         IWL_DEBUG_RADIO(priv, "u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
859         IWL_DEBUG_RADIO(priv, "u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
860         IWL_DEBUG_RADIO(priv, "u32 filter_flags: 0x%08x\n",
861                         le32_to_cpu(rxon->filter_flags));
862         IWL_DEBUG_RADIO(priv, "u8 dev_type: 0x%x\n", rxon->dev_type);
863         IWL_DEBUG_RADIO(priv, "u8 ofdm_basic_rates: 0x%02x\n",
864                         rxon->ofdm_basic_rates);
865         IWL_DEBUG_RADIO(priv, "u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
866         IWL_DEBUG_RADIO(priv, "u8[6] node_addr: %pM\n", rxon->node_addr);
867         IWL_DEBUG_RADIO(priv, "u8[6] bssid_addr: %pM\n", rxon->bssid_addr);
868         IWL_DEBUG_RADIO(priv, "u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
869 }
870 #endif
871
872 static void iwlagn_abort_notification_waits(struct iwl_priv *priv)
873 {
874         unsigned long flags;
875         struct iwl_notification_wait *wait_entry;
876
877         spin_lock_irqsave(&priv->_agn.notif_wait_lock, flags);
878         list_for_each_entry(wait_entry, &priv->_agn.notif_waits, list)
879                 wait_entry->aborted = true;
880         spin_unlock_irqrestore(&priv->_agn.notif_wait_lock, flags);
881
882         wake_up_all(&priv->_agn.notif_waitq);
883 }
884
885 void iwlagn_fw_error(struct iwl_priv *priv, bool ondemand)
886 {
887         unsigned int reload_msec;
888         unsigned long reload_jiffies;
889
890         /* Set the FW error flag -- cleared on iwl_down */
891         set_bit(STATUS_FW_ERROR, &priv->status);
892
893         /* Cancel currently queued command. */
894         clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
895
896         iwlagn_abort_notification_waits(priv);
897
898         /* Keep the restart process from trying to send host
899          * commands by clearing the ready bit */
900         clear_bit(STATUS_READY, &priv->status);
901
902         wake_up_interruptible(&priv->wait_command_queue);
903
904         if (!ondemand) {
905                 /*
906                  * If firmware keep reloading, then it indicate something
907                  * serious wrong and firmware having problem to recover
908                  * from it. Instead of keep trying which will fill the syslog
909                  * and hang the system, let's just stop it
910                  */
911                 reload_jiffies = jiffies;
912                 reload_msec = jiffies_to_msecs((long) reload_jiffies -
913                                         (long) priv->reload_jiffies);
914                 priv->reload_jiffies = reload_jiffies;
915                 if (reload_msec <= IWL_MIN_RELOAD_DURATION) {
916                         priv->reload_count++;
917                         if (priv->reload_count >= IWL_MAX_CONTINUE_RELOAD_CNT) {
918                                 IWL_ERR(priv, "BUG_ON, Stop restarting\n");
919                                 return;
920                         }
921                 } else
922                         priv->reload_count = 0;
923         }
924
925         if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
926                 if (iwlagn_mod_params.restart_fw) {
927                         IWL_DEBUG(priv, IWL_DL_FW_ERRORS,
928                                   "Restarting adapter due to uCode error.\n");
929                         queue_work(priv->workqueue, &priv->restart);
930                 } else
931                         IWL_DEBUG(priv, IWL_DL_FW_ERRORS,
932                                   "Detected FW error, but not restarting\n");
933         }
934 }
935
936 /**
937  * iwl_irq_handle_error - called for HW or SW error interrupt from card
938  */
939 void iwl_irq_handle_error(struct iwl_priv *priv)
940 {
941         /* W/A for WiFi/WiMAX coex and WiMAX own the RF */
942         if (priv->cfg->internal_wimax_coex &&
943             (!(iwl_read_prph(priv, APMG_CLK_CTRL_REG) &
944                         APMS_CLK_VAL_MRB_FUNC_MODE) ||
945              (iwl_read_prph(priv, APMG_PS_CTRL_REG) &
946                         APMG_PS_CTRL_VAL_RESET_REQ))) {
947                 /*
948                  * Keep the restart process from trying to send host
949                  * commands by clearing the ready bit.
950                  */
951                 clear_bit(STATUS_READY, &priv->status);
952                 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
953                 wake_up_interruptible(&priv->wait_command_queue);
954                 IWL_ERR(priv, "RF is used by WiMAX\n");
955                 return;
956         }
957
958         IWL_ERR(priv, "Loaded firmware version: %s\n",
959                 priv->hw->wiphy->fw_version);
960
961         iwl_dump_nic_error_log(priv);
962         iwl_dump_csr(priv);
963         iwl_dump_fh(priv, NULL, false);
964         iwl_dump_nic_event_log(priv, false, NULL, false);
965 #ifdef CONFIG_IWLWIFI_DEBUG
966         if (iwl_get_debug_level(priv) & IWL_DL_FW_ERRORS)
967                 iwl_print_rx_config_cmd(priv,
968                                         &priv->contexts[IWL_RXON_CTX_BSS]);
969 #endif
970
971         iwlagn_fw_error(priv, false);
972 }
973
974 static int iwl_apm_stop_master(struct iwl_priv *priv)
975 {
976         int ret = 0;
977
978         /* stop device's busmaster DMA activity */
979         iwl_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_STOP_MASTER);
980
981         ret = iwl_poll_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_MASTER_DISABLED,
982                         CSR_RESET_REG_FLAG_MASTER_DISABLED, 100);
983         if (ret)
984                 IWL_WARN(priv, "Master Disable Timed Out, 100 usec\n");
985
986         IWL_DEBUG_INFO(priv, "stop master\n");
987
988         return ret;
989 }
990
991 void iwl_apm_stop(struct iwl_priv *priv)
992 {
993         IWL_DEBUG_INFO(priv, "Stop card, put in low power state\n");
994
995         clear_bit(STATUS_DEVICE_ENABLED, &priv->status);
996
997         /* Stop device's DMA activity */
998         iwl_apm_stop_master(priv);
999
1000         /* Reset the entire device */
1001         iwl_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
1002
1003         udelay(10);
1004
1005         /*
1006          * Clear "initialization complete" bit to move adapter from
1007          * D0A* (powered-up Active) --> D0U* (Uninitialized) state.
1008          */
1009         iwl_clear_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
1010 }
1011
1012
1013 /*
1014  * Start up NIC's basic functionality after it has been reset
1015  * (e.g. after platform boot, or shutdown via iwl_apm_stop())
1016  * NOTE:  This does not load uCode nor start the embedded processor
1017  */
1018 int iwl_apm_init(struct iwl_priv *priv)
1019 {
1020         int ret = 0;
1021         u16 lctl;
1022
1023         IWL_DEBUG_INFO(priv, "Init card's basic functions\n");
1024
1025         /*
1026          * Use "set_bit" below rather than "write", to preserve any hardware
1027          * bits already set by default after reset.
1028          */
1029
1030         /* Disable L0S exit timer (platform NMI Work/Around) */
1031         iwl_set_bit(priv, CSR_GIO_CHICKEN_BITS,
1032                           CSR_GIO_CHICKEN_BITS_REG_BIT_DIS_L0S_EXIT_TIMER);
1033
1034         /*
1035          * Disable L0s without affecting L1;
1036          *  don't wait for ICH L0s (ICH bug W/A)
1037          */
1038         iwl_set_bit(priv, CSR_GIO_CHICKEN_BITS,
1039                           CSR_GIO_CHICKEN_BITS_REG_BIT_L1A_NO_L0S_RX);
1040
1041         /* Set FH wait threshold to maximum (HW error during stress W/A) */
1042         iwl_set_bit(priv, CSR_DBG_HPET_MEM_REG, CSR_DBG_HPET_MEM_REG_VAL);
1043
1044         /*
1045          * Enable HAP INTA (interrupt from management bus) to
1046          * wake device's PCI Express link L1a -> L0s
1047          */
1048         iwl_set_bit(priv, CSR_HW_IF_CONFIG_REG,
1049                                     CSR_HW_IF_CONFIG_REG_BIT_HAP_WAKE_L1A);
1050
1051         /*
1052          * HW bug W/A for instability in PCIe bus L0->L0S->L1 transition.
1053          * Check if BIOS (or OS) enabled L1-ASPM on this device.
1054          * If so (likely), disable L0S, so device moves directly L0->L1;
1055          *    costs negligible amount of power savings.
1056          * If not (unlikely), enable L0S, so there is at least some
1057          *    power savings, even without L1.
1058          */
1059         lctl = iwl_pcie_link_ctl(priv);
1060         if ((lctl & PCI_CFG_LINK_CTRL_VAL_L1_EN) ==
1061                                 PCI_CFG_LINK_CTRL_VAL_L1_EN) {
1062                 /* L1-ASPM enabled; disable(!) L0S  */
1063                 iwl_set_bit(priv, CSR_GIO_REG,
1064                                 CSR_GIO_REG_VAL_L0S_ENABLED);
1065                 IWL_DEBUG_POWER(priv, "L1 Enabled; Disabling L0S\n");
1066         } else {
1067                 /* L1-ASPM disabled; enable(!) L0S */
1068                 iwl_clear_bit(priv, CSR_GIO_REG,
1069                                 CSR_GIO_REG_VAL_L0S_ENABLED);
1070                 IWL_DEBUG_POWER(priv, "L1 Disabled; Enabling L0S\n");
1071         }
1072
1073         /* Configure analog phase-lock-loop before activating to D0A */
1074         if (priv->cfg->base_params->pll_cfg_val)
1075                 iwl_set_bit(priv, CSR_ANA_PLL_CFG,
1076                             priv->cfg->base_params->pll_cfg_val);
1077
1078         /*
1079          * Set "initialization complete" bit to move adapter from
1080          * D0U* --> D0A* (powered-up active) state.
1081          */
1082         iwl_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
1083
1084         /*
1085          * Wait for clock stabilization; once stabilized, access to
1086          * device-internal resources is supported, e.g. iwl_write_prph()
1087          * and accesses to uCode SRAM.
1088          */
1089         ret = iwl_poll_bit(priv, CSR_GP_CNTRL,
1090                         CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
1091                         CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25000);
1092         if (ret < 0) {
1093                 IWL_DEBUG_INFO(priv, "Failed to init the card\n");
1094                 goto out;
1095         }
1096
1097         /*
1098          * Enable DMA clock and wait for it to stabilize.
1099          *
1100          * Write to "CLK_EN_REG"; "1" bits enable clocks, while "0" bits
1101          * do not disable clocks.  This preserves any hardware bits already
1102          * set by default in "CLK_CTRL_REG" after reset.
1103          */
1104         iwl_write_prph(priv, APMG_CLK_EN_REG, APMG_CLK_VAL_DMA_CLK_RQT);
1105         udelay(20);
1106
1107         /* Disable L1-Active */
1108         iwl_set_bits_prph(priv, APMG_PCIDEV_STT_REG,
1109                           APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
1110
1111         set_bit(STATUS_DEVICE_ENABLED, &priv->status);
1112
1113 out:
1114         return ret;
1115 }
1116
1117
1118 int iwl_set_tx_power(struct iwl_priv *priv, s8 tx_power, bool force)
1119 {
1120         int ret;
1121         s8 prev_tx_power;
1122         bool defer;
1123         struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
1124
1125         lockdep_assert_held(&priv->mutex);
1126
1127         if (priv->tx_power_user_lmt == tx_power && !force)
1128                 return 0;
1129
1130         if (!priv->cfg->ops->lib->send_tx_power)
1131                 return -EOPNOTSUPP;
1132
1133         if (tx_power < IWLAGN_TX_POWER_TARGET_POWER_MIN) {
1134                 IWL_WARN(priv,
1135                          "Requested user TXPOWER %d below lower limit %d.\n",
1136                          tx_power,
1137                          IWLAGN_TX_POWER_TARGET_POWER_MIN);
1138                 return -EINVAL;
1139         }
1140
1141         if (tx_power > priv->tx_power_device_lmt) {
1142                 IWL_WARN(priv,
1143                         "Requested user TXPOWER %d above upper limit %d.\n",
1144                          tx_power, priv->tx_power_device_lmt);
1145                 return -EINVAL;
1146         }
1147
1148         if (!iwl_is_ready_rf(priv))
1149                 return -EIO;
1150
1151         /* scan complete and commit_rxon use tx_power_next value,
1152          * it always need to be updated for newest request */
1153         priv->tx_power_next = tx_power;
1154
1155         /* do not set tx power when scanning or channel changing */
1156         defer = test_bit(STATUS_SCANNING, &priv->status) ||
1157                 memcmp(&ctx->active, &ctx->staging, sizeof(ctx->staging));
1158         if (defer && !force) {
1159                 IWL_DEBUG_INFO(priv, "Deferring tx power set\n");
1160                 return 0;
1161         }
1162
1163         prev_tx_power = priv->tx_power_user_lmt;
1164         priv->tx_power_user_lmt = tx_power;
1165
1166         ret = priv->cfg->ops->lib->send_tx_power(priv);
1167
1168         /* if fail to set tx_power, restore the orig. tx power */
1169         if (ret) {
1170                 priv->tx_power_user_lmt = prev_tx_power;
1171                 priv->tx_power_next = prev_tx_power;
1172         }
1173         return ret;
1174 }
1175
1176 void iwl_send_bt_config(struct iwl_priv *priv)
1177 {
1178         struct iwl_bt_cmd bt_cmd = {
1179                 .lead_time = BT_LEAD_TIME_DEF,
1180                 .max_kill = BT_MAX_KILL_DEF,
1181                 .kill_ack_mask = 0,
1182                 .kill_cts_mask = 0,
1183         };
1184
1185         if (!bt_coex_active)
1186                 bt_cmd.flags = BT_COEX_DISABLE;
1187         else
1188                 bt_cmd.flags = BT_COEX_ENABLE;
1189
1190         priv->bt_enable_flag = bt_cmd.flags;
1191         IWL_DEBUG_INFO(priv, "BT coex %s\n",
1192                 (bt_cmd.flags == BT_COEX_DISABLE) ? "disable" : "active");
1193
1194         if (iwl_send_cmd_pdu(priv, REPLY_BT_CONFIG,
1195                              sizeof(struct iwl_bt_cmd), &bt_cmd))
1196                 IWL_ERR(priv, "failed to send BT Coex Config\n");
1197 }
1198
1199 int iwl_send_statistics_request(struct iwl_priv *priv, u8 flags, bool clear)
1200 {
1201         struct iwl_statistics_cmd statistics_cmd = {
1202                 .configuration_flags =
1203                         clear ? IWL_STATS_CONF_CLEAR_STATS : 0,
1204         };
1205
1206         if (flags & CMD_ASYNC)
1207                 return iwl_send_cmd_pdu_async(priv, REPLY_STATISTICS_CMD,
1208                                                sizeof(struct iwl_statistics_cmd),
1209                                                &statistics_cmd, NULL);
1210         else
1211                 return iwl_send_cmd_pdu(priv, REPLY_STATISTICS_CMD,
1212                                         sizeof(struct iwl_statistics_cmd),
1213                                         &statistics_cmd);
1214 }
1215
1216 void iwl_clear_isr_stats(struct iwl_priv *priv)
1217 {
1218         memset(&priv->isr_stats, 0, sizeof(priv->isr_stats));
1219 }
1220
1221 int iwl_mac_conf_tx(struct ieee80211_hw *hw, u16 queue,
1222                            const struct ieee80211_tx_queue_params *params)
1223 {
1224         struct iwl_priv *priv = hw->priv;
1225         struct iwl_rxon_context *ctx;
1226         unsigned long flags;
1227         int q;
1228
1229         IWL_DEBUG_MAC80211(priv, "enter\n");
1230
1231         if (!iwl_is_ready_rf(priv)) {
1232                 IWL_DEBUG_MAC80211(priv, "leave - RF not ready\n");
1233                 return -EIO;
1234         }
1235
1236         if (queue >= AC_NUM) {
1237                 IWL_DEBUG_MAC80211(priv, "leave - queue >= AC_NUM %d\n", queue);
1238                 return 0;
1239         }
1240
1241         q = AC_NUM - 1 - queue;
1242
1243         spin_lock_irqsave(&priv->lock, flags);
1244
1245         /*
1246          * MULTI-FIXME
1247          * This may need to be done per interface in nl80211/cfg80211/mac80211.
1248          */
1249         for_each_context(priv, ctx) {
1250                 ctx->qos_data.def_qos_parm.ac[q].cw_min =
1251                         cpu_to_le16(params->cw_min);
1252                 ctx->qos_data.def_qos_parm.ac[q].cw_max =
1253                         cpu_to_le16(params->cw_max);
1254                 ctx->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
1255                 ctx->qos_data.def_qos_parm.ac[q].edca_txop =
1256                                 cpu_to_le16((params->txop * 32));
1257
1258                 ctx->qos_data.def_qos_parm.ac[q].reserved1 = 0;
1259         }
1260
1261         spin_unlock_irqrestore(&priv->lock, flags);
1262
1263         IWL_DEBUG_MAC80211(priv, "leave\n");
1264         return 0;
1265 }
1266
1267 int iwl_mac_tx_last_beacon(struct ieee80211_hw *hw)
1268 {
1269         struct iwl_priv *priv = hw->priv;
1270
1271         return priv->ibss_manager == IWL_IBSS_MANAGER;
1272 }
1273
1274 static int iwl_set_mode(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
1275 {
1276         iwl_connection_init_rx_config(priv, ctx);
1277
1278         if (priv->cfg->ops->hcmd->set_rxon_chain)
1279                 priv->cfg->ops->hcmd->set_rxon_chain(priv, ctx);
1280
1281         return iwlcore_commit_rxon(priv, ctx);
1282 }
1283
1284 static int iwl_setup_interface(struct iwl_priv *priv,
1285                                struct iwl_rxon_context *ctx)
1286 {
1287         struct ieee80211_vif *vif = ctx->vif;
1288         int err;
1289
1290         lockdep_assert_held(&priv->mutex);
1291
1292         /*
1293          * This variable will be correct only when there's just
1294          * a single context, but all code using it is for hardware
1295          * that supports only one context.
1296          */
1297         priv->iw_mode = vif->type;
1298
1299         ctx->is_active = true;
1300
1301         err = iwl_set_mode(priv, ctx);
1302         if (err) {
1303                 if (!ctx->always_active)
1304                         ctx->is_active = false;
1305                 return err;
1306         }
1307
1308         if (priv->cfg->bt_params && priv->cfg->bt_params->advanced_bt_coexist &&
1309             vif->type == NL80211_IFTYPE_ADHOC) {
1310                 /*
1311                  * pretend to have high BT traffic as long as we
1312                  * are operating in IBSS mode, as this will cause
1313                  * the rate scaling etc. to behave as intended.
1314                  */
1315                 priv->bt_traffic_load = IWL_BT_COEX_TRAFFIC_LOAD_HIGH;
1316         }
1317
1318         return 0;
1319 }
1320
1321 int iwl_mac_add_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1322 {
1323         struct iwl_priv *priv = hw->priv;
1324         struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
1325         struct iwl_rxon_context *tmp, *ctx = NULL;
1326         int err;
1327         enum nl80211_iftype viftype = ieee80211_vif_type_p2p(vif);
1328
1329         IWL_DEBUG_MAC80211(priv, "enter: type %d, addr %pM\n",
1330                            viftype, vif->addr);
1331
1332         mutex_lock(&priv->mutex);
1333
1334         if (!iwl_is_ready_rf(priv)) {
1335                 IWL_WARN(priv, "Try to add interface when device not ready\n");
1336                 err = -EINVAL;
1337                 goto out;
1338         }
1339
1340         for_each_context(priv, tmp) {
1341                 u32 possible_modes =
1342                         tmp->interface_modes | tmp->exclusive_interface_modes;
1343
1344                 if (tmp->vif) {
1345                         /* check if this busy context is exclusive */
1346                         if (tmp->exclusive_interface_modes &
1347                                                 BIT(tmp->vif->type)) {
1348                                 err = -EINVAL;
1349                                 goto out;
1350                         }
1351                         continue;
1352                 }
1353
1354                 if (!(possible_modes & BIT(viftype)))
1355                         continue;
1356
1357                 /* have maybe usable context w/o interface */
1358                 ctx = tmp;
1359                 break;
1360         }
1361
1362         if (!ctx) {
1363                 err = -EOPNOTSUPP;
1364                 goto out;
1365         }
1366
1367         vif_priv->ctx = ctx;
1368         ctx->vif = vif;
1369
1370         err = iwl_setup_interface(priv, ctx);
1371         if (!err)
1372                 goto out;
1373
1374         ctx->vif = NULL;
1375         priv->iw_mode = NL80211_IFTYPE_STATION;
1376  out:
1377         mutex_unlock(&priv->mutex);
1378
1379         IWL_DEBUG_MAC80211(priv, "leave\n");
1380         return err;
1381 }
1382
1383 static void iwl_teardown_interface(struct iwl_priv *priv,
1384                                    struct ieee80211_vif *vif,
1385                                    bool mode_change)
1386 {
1387         struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
1388
1389         lockdep_assert_held(&priv->mutex);
1390
1391         if (priv->scan_vif == vif) {
1392                 iwl_scan_cancel_timeout(priv, 200);
1393                 iwl_force_scan_end(priv);
1394         }
1395
1396         if (!mode_change) {
1397                 iwl_set_mode(priv, ctx);
1398                 if (!ctx->always_active)
1399                         ctx->is_active = false;
1400         }
1401
1402         /*
1403          * When removing the IBSS interface, overwrite the
1404          * BT traffic load with the stored one from the last
1405          * notification, if any. If this is a device that
1406          * doesn't implement this, this has no effect since
1407          * both values are the same and zero.
1408          */
1409         if (vif->type == NL80211_IFTYPE_ADHOC)
1410                 priv->bt_traffic_load = priv->last_bt_traffic_load;
1411 }
1412
1413 void iwl_mac_remove_interface(struct ieee80211_hw *hw,
1414                               struct ieee80211_vif *vif)
1415 {
1416         struct iwl_priv *priv = hw->priv;
1417         struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
1418
1419         IWL_DEBUG_MAC80211(priv, "enter\n");
1420
1421         mutex_lock(&priv->mutex);
1422
1423         WARN_ON(ctx->vif != vif);
1424         ctx->vif = NULL;
1425
1426         iwl_teardown_interface(priv, vif, false);
1427
1428         mutex_unlock(&priv->mutex);
1429
1430         IWL_DEBUG_MAC80211(priv, "leave\n");
1431
1432 }
1433
1434 int iwl_alloc_txq_mem(struct iwl_priv *priv)
1435 {
1436         if (!priv->txq)
1437                 priv->txq = kzalloc(
1438                         sizeof(struct iwl_tx_queue) *
1439                                 priv->cfg->base_params->num_of_queues,
1440                         GFP_KERNEL);
1441         if (!priv->txq) {
1442                 IWL_ERR(priv, "Not enough memory for txq\n");
1443                 return -ENOMEM;
1444         }
1445         return 0;
1446 }
1447
1448 void iwl_free_txq_mem(struct iwl_priv *priv)
1449 {
1450         kfree(priv->txq);
1451         priv->txq = NULL;
1452 }
1453
1454 #ifdef CONFIG_IWLWIFI_DEBUGFS
1455
1456 #define IWL_TRAFFIC_DUMP_SIZE   (IWL_TRAFFIC_ENTRY_SIZE * IWL_TRAFFIC_ENTRIES)
1457
1458 void iwl_reset_traffic_log(struct iwl_priv *priv)
1459 {
1460         priv->tx_traffic_idx = 0;
1461         priv->rx_traffic_idx = 0;
1462         if (priv->tx_traffic)
1463                 memset(priv->tx_traffic, 0, IWL_TRAFFIC_DUMP_SIZE);
1464         if (priv->rx_traffic)
1465                 memset(priv->rx_traffic, 0, IWL_TRAFFIC_DUMP_SIZE);
1466 }
1467
1468 int iwl_alloc_traffic_mem(struct iwl_priv *priv)
1469 {
1470         u32 traffic_size = IWL_TRAFFIC_DUMP_SIZE;
1471
1472         if (iwl_debug_level & IWL_DL_TX) {
1473                 if (!priv->tx_traffic) {
1474                         priv->tx_traffic =
1475                                 kzalloc(traffic_size, GFP_KERNEL);
1476                         if (!priv->tx_traffic)
1477                                 return -ENOMEM;
1478                 }
1479         }
1480         if (iwl_debug_level & IWL_DL_RX) {
1481                 if (!priv->rx_traffic) {
1482                         priv->rx_traffic =
1483                                 kzalloc(traffic_size, GFP_KERNEL);
1484                         if (!priv->rx_traffic)
1485                                 return -ENOMEM;
1486                 }
1487         }
1488         iwl_reset_traffic_log(priv);
1489         return 0;
1490 }
1491
1492 void iwl_free_traffic_mem(struct iwl_priv *priv)
1493 {
1494         kfree(priv->tx_traffic);
1495         priv->tx_traffic = NULL;
1496
1497         kfree(priv->rx_traffic);
1498         priv->rx_traffic = NULL;
1499 }
1500
1501 void iwl_dbg_log_tx_data_frame(struct iwl_priv *priv,
1502                       u16 length, struct ieee80211_hdr *header)
1503 {
1504         __le16 fc;
1505         u16 len;
1506
1507         if (likely(!(iwl_debug_level & IWL_DL_TX)))
1508                 return;
1509
1510         if (!priv->tx_traffic)
1511                 return;
1512
1513         fc = header->frame_control;
1514         if (ieee80211_is_data(fc)) {
1515                 len = (length > IWL_TRAFFIC_ENTRY_SIZE)
1516                        ? IWL_TRAFFIC_ENTRY_SIZE : length;
1517                 memcpy((priv->tx_traffic +
1518                        (priv->tx_traffic_idx * IWL_TRAFFIC_ENTRY_SIZE)),
1519                        header, len);
1520                 priv->tx_traffic_idx =
1521                         (priv->tx_traffic_idx + 1) % IWL_TRAFFIC_ENTRIES;
1522         }
1523 }
1524
1525 void iwl_dbg_log_rx_data_frame(struct iwl_priv *priv,
1526                       u16 length, struct ieee80211_hdr *header)
1527 {
1528         __le16 fc;
1529         u16 len;
1530
1531         if (likely(!(iwl_debug_level & IWL_DL_RX)))
1532                 return;
1533
1534         if (!priv->rx_traffic)
1535                 return;
1536
1537         fc = header->frame_control;
1538         if (ieee80211_is_data(fc)) {
1539                 len = (length > IWL_TRAFFIC_ENTRY_SIZE)
1540                        ? IWL_TRAFFIC_ENTRY_SIZE : length;
1541                 memcpy((priv->rx_traffic +
1542                        (priv->rx_traffic_idx * IWL_TRAFFIC_ENTRY_SIZE)),
1543                        header, len);
1544                 priv->rx_traffic_idx =
1545                         (priv->rx_traffic_idx + 1) % IWL_TRAFFIC_ENTRIES;
1546         }
1547 }
1548
1549 const char *get_mgmt_string(int cmd)
1550 {
1551         switch (cmd) {
1552                 IWL_CMD(MANAGEMENT_ASSOC_REQ);
1553                 IWL_CMD(MANAGEMENT_ASSOC_RESP);
1554                 IWL_CMD(MANAGEMENT_REASSOC_REQ);
1555                 IWL_CMD(MANAGEMENT_REASSOC_RESP);
1556                 IWL_CMD(MANAGEMENT_PROBE_REQ);
1557                 IWL_CMD(MANAGEMENT_PROBE_RESP);
1558                 IWL_CMD(MANAGEMENT_BEACON);
1559                 IWL_CMD(MANAGEMENT_ATIM);
1560                 IWL_CMD(MANAGEMENT_DISASSOC);
1561                 IWL_CMD(MANAGEMENT_AUTH);
1562                 IWL_CMD(MANAGEMENT_DEAUTH);
1563                 IWL_CMD(MANAGEMENT_ACTION);
1564         default:
1565                 return "UNKNOWN";
1566
1567         }
1568 }
1569
1570 const char *get_ctrl_string(int cmd)
1571 {
1572         switch (cmd) {
1573                 IWL_CMD(CONTROL_BACK_REQ);
1574                 IWL_CMD(CONTROL_BACK);
1575                 IWL_CMD(CONTROL_PSPOLL);
1576                 IWL_CMD(CONTROL_RTS);
1577                 IWL_CMD(CONTROL_CTS);
1578                 IWL_CMD(CONTROL_ACK);
1579                 IWL_CMD(CONTROL_CFEND);
1580                 IWL_CMD(CONTROL_CFENDACK);
1581         default:
1582                 return "UNKNOWN";
1583
1584         }
1585 }
1586
1587 void iwl_clear_traffic_stats(struct iwl_priv *priv)
1588 {
1589         memset(&priv->tx_stats, 0, sizeof(struct traffic_stats));
1590         memset(&priv->rx_stats, 0, sizeof(struct traffic_stats));
1591 }
1592
1593 /*
1594  * if CONFIG_IWLWIFI_DEBUGFS defined, iwl_update_stats function will
1595  * record all the MGMT, CTRL and DATA pkt for both TX and Rx pass.
1596  * Use debugFs to display the rx/rx_statistics
1597  * if CONFIG_IWLWIFI_DEBUGFS not being defined, then no MGMT and CTRL
1598  * information will be recorded, but DATA pkt still will be recorded
1599  * for the reason of iwl_led.c need to control the led blinking based on
1600  * number of tx and rx data.
1601  *
1602  */
1603 void iwl_update_stats(struct iwl_priv *priv, bool is_tx, __le16 fc, u16 len)
1604 {
1605         struct traffic_stats    *stats;
1606
1607         if (is_tx)
1608                 stats = &priv->tx_stats;
1609         else
1610                 stats = &priv->rx_stats;
1611
1612         if (ieee80211_is_mgmt(fc)) {
1613                 switch (fc & cpu_to_le16(IEEE80211_FCTL_STYPE)) {
1614                 case cpu_to_le16(IEEE80211_STYPE_ASSOC_REQ):
1615                         stats->mgmt[MANAGEMENT_ASSOC_REQ]++;
1616                         break;
1617                 case cpu_to_le16(IEEE80211_STYPE_ASSOC_RESP):
1618                         stats->mgmt[MANAGEMENT_ASSOC_RESP]++;
1619                         break;
1620                 case cpu_to_le16(IEEE80211_STYPE_REASSOC_REQ):
1621                         stats->mgmt[MANAGEMENT_REASSOC_REQ]++;
1622                         break;
1623                 case cpu_to_le16(IEEE80211_STYPE_REASSOC_RESP):
1624                         stats->mgmt[MANAGEMENT_REASSOC_RESP]++;
1625                         break;
1626                 case cpu_to_le16(IEEE80211_STYPE_PROBE_REQ):
1627                         stats->mgmt[MANAGEMENT_PROBE_REQ]++;
1628                         break;
1629                 case cpu_to_le16(IEEE80211_STYPE_PROBE_RESP):
1630                         stats->mgmt[MANAGEMENT_PROBE_RESP]++;
1631                         break;
1632                 case cpu_to_le16(IEEE80211_STYPE_BEACON):
1633                         stats->mgmt[MANAGEMENT_BEACON]++;
1634                         break;
1635                 case cpu_to_le16(IEEE80211_STYPE_ATIM):
1636                         stats->mgmt[MANAGEMENT_ATIM]++;
1637                         break;
1638                 case cpu_to_le16(IEEE80211_STYPE_DISASSOC):
1639                         stats->mgmt[MANAGEMENT_DISASSOC]++;
1640                         break;
1641                 case cpu_to_le16(IEEE80211_STYPE_AUTH):
1642                         stats->mgmt[MANAGEMENT_AUTH]++;
1643                         break;
1644                 case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
1645                         stats->mgmt[MANAGEMENT_DEAUTH]++;
1646                         break;
1647                 case cpu_to_le16(IEEE80211_STYPE_ACTION):
1648                         stats->mgmt[MANAGEMENT_ACTION]++;
1649                         break;
1650                 }
1651         } else if (ieee80211_is_ctl(fc)) {
1652                 switch (fc & cpu_to_le16(IEEE80211_FCTL_STYPE)) {
1653                 case cpu_to_le16(IEEE80211_STYPE_BACK_REQ):
1654                         stats->ctrl[CONTROL_BACK_REQ]++;
1655                         break;
1656                 case cpu_to_le16(IEEE80211_STYPE_BACK):
1657                         stats->ctrl[CONTROL_BACK]++;
1658                         break;
1659                 case cpu_to_le16(IEEE80211_STYPE_PSPOLL):
1660                         stats->ctrl[CONTROL_PSPOLL]++;
1661                         break;
1662                 case cpu_to_le16(IEEE80211_STYPE_RTS):
1663                         stats->ctrl[CONTROL_RTS]++;
1664                         break;
1665                 case cpu_to_le16(IEEE80211_STYPE_CTS):
1666                         stats->ctrl[CONTROL_CTS]++;
1667                         break;
1668                 case cpu_to_le16(IEEE80211_STYPE_ACK):
1669                         stats->ctrl[CONTROL_ACK]++;
1670                         break;
1671                 case cpu_to_le16(IEEE80211_STYPE_CFEND):
1672                         stats->ctrl[CONTROL_CFEND]++;
1673                         break;
1674                 case cpu_to_le16(IEEE80211_STYPE_CFENDACK):
1675                         stats->ctrl[CONTROL_CFENDACK]++;
1676                         break;
1677                 }
1678         } else {
1679                 /* data */
1680                 stats->data_cnt++;
1681                 stats->data_bytes += len;
1682         }
1683 }
1684 #endif
1685
1686 static void iwl_force_rf_reset(struct iwl_priv *priv)
1687 {
1688         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1689                 return;
1690
1691         if (!iwl_is_any_associated(priv)) {
1692                 IWL_DEBUG_SCAN(priv, "force reset rejected: not associated\n");
1693                 return;
1694         }
1695         /*
1696          * There is no easy and better way to force reset the radio,
1697          * the only known method is switching channel which will force to
1698          * reset and tune the radio.
1699          * Use internal short scan (single channel) operation to should
1700          * achieve this objective.
1701          * Driver should reset the radio when number of consecutive missed
1702          * beacon, or any other uCode error condition detected.
1703          */
1704         IWL_DEBUG_INFO(priv, "perform radio reset.\n");
1705         iwl_internal_short_hw_scan(priv);
1706 }
1707
1708
1709 int iwl_force_reset(struct iwl_priv *priv, int mode, bool external)
1710 {
1711         struct iwl_force_reset *force_reset;
1712
1713         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1714                 return -EINVAL;
1715
1716         if (mode >= IWL_MAX_FORCE_RESET) {
1717                 IWL_DEBUG_INFO(priv, "invalid reset request.\n");
1718                 return -EINVAL;
1719         }
1720         force_reset = &priv->force_reset[mode];
1721         force_reset->reset_request_count++;
1722         if (!external) {
1723                 if (force_reset->last_force_reset_jiffies &&
1724                     time_after(force_reset->last_force_reset_jiffies +
1725                     force_reset->reset_duration, jiffies)) {
1726                         IWL_DEBUG_INFO(priv, "force reset rejected\n");
1727                         force_reset->reset_reject_count++;
1728                         return -EAGAIN;
1729                 }
1730         }
1731         force_reset->reset_success_count++;
1732         force_reset->last_force_reset_jiffies = jiffies;
1733         IWL_DEBUG_INFO(priv, "perform force reset (%d)\n", mode);
1734         switch (mode) {
1735         case IWL_RF_RESET:
1736                 iwl_force_rf_reset(priv);
1737                 break;
1738         case IWL_FW_RESET:
1739                 /*
1740                  * if the request is from external(ex: debugfs),
1741                  * then always perform the request in regardless the module
1742                  * parameter setting
1743                  * if the request is from internal (uCode error or driver
1744                  * detect failure), then fw_restart module parameter
1745                  * need to be check before performing firmware reload
1746                  */
1747                 if (!external && !iwlagn_mod_params.restart_fw) {
1748                         IWL_DEBUG_INFO(priv, "Cancel firmware reload based on "
1749                                        "module parameter setting\n");
1750                         break;
1751                 }
1752                 IWL_ERR(priv, "On demand firmware reload\n");
1753                 iwlagn_fw_error(priv, true);
1754                 break;
1755         }
1756         return 0;
1757 }
1758
1759 int iwl_mac_change_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1760                              enum nl80211_iftype newtype, bool newp2p)
1761 {
1762         struct iwl_priv *priv = hw->priv;
1763         struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
1764         struct iwl_rxon_context *bss_ctx = &priv->contexts[IWL_RXON_CTX_BSS];
1765         struct iwl_rxon_context *tmp;
1766         enum nl80211_iftype newviftype = newtype;
1767         u32 interface_modes;
1768         int err;
1769
1770         newtype = ieee80211_iftype_p2p(newtype, newp2p);
1771
1772         mutex_lock(&priv->mutex);
1773
1774         if (!ctx->vif || !iwl_is_ready_rf(priv)) {
1775                 /*
1776                  * Huh? But wait ... this can maybe happen when
1777                  * we're in the middle of a firmware restart!
1778                  */
1779                 err = -EBUSY;
1780                 goto out;
1781         }
1782
1783         interface_modes = ctx->interface_modes | ctx->exclusive_interface_modes;
1784
1785         if (!(interface_modes & BIT(newtype))) {
1786                 err = -EBUSY;
1787                 goto out;
1788         }
1789
1790         /*
1791          * Refuse a change that should be done by moving from the PAN
1792          * context to the BSS context instead, if the BSS context is
1793          * available and can support the new interface type.
1794          */
1795         if (ctx->ctxid == IWL_RXON_CTX_PAN && !bss_ctx->vif &&
1796             (bss_ctx->interface_modes & BIT(newtype) ||
1797              bss_ctx->exclusive_interface_modes & BIT(newtype))) {
1798                 BUILD_BUG_ON(NUM_IWL_RXON_CTX != 2);
1799                 err = -EBUSY;
1800                 goto out;
1801         }
1802
1803         if (ctx->exclusive_interface_modes & BIT(newtype)) {
1804                 for_each_context(priv, tmp) {
1805                         if (ctx == tmp)
1806                                 continue;
1807
1808                         if (!tmp->vif)
1809                                 continue;
1810
1811                         /*
1812                          * The current mode switch would be exclusive, but
1813                          * another context is active ... refuse the switch.
1814                          */
1815                         err = -EBUSY;
1816                         goto out;
1817                 }
1818         }
1819
1820         /* success */
1821         iwl_teardown_interface(priv, vif, true);
1822         vif->type = newviftype;
1823         vif->p2p = newp2p;
1824         err = iwl_setup_interface(priv, ctx);
1825         WARN_ON(err);
1826         /*
1827          * We've switched internally, but submitting to the
1828          * device may have failed for some reason. Mask this
1829          * error, because otherwise mac80211 will not switch
1830          * (and set the interface type back) and we'll be
1831          * out of sync with it.
1832          */
1833         err = 0;
1834
1835  out:
1836         mutex_unlock(&priv->mutex);
1837         return err;
1838 }
1839
1840 /*
1841  * On every watchdog tick we check (latest) time stamp. If it does not
1842  * change during timeout period and queue is not empty we reset firmware.
1843  */
1844 static int iwl_check_stuck_queue(struct iwl_priv *priv, int cnt)
1845 {
1846         struct iwl_tx_queue *txq = &priv->txq[cnt];
1847         struct iwl_queue *q = &txq->q;
1848         unsigned long timeout;
1849         int ret;
1850
1851         if (q->read_ptr == q->write_ptr) {
1852                 txq->time_stamp = jiffies;
1853                 return 0;
1854         }
1855
1856         timeout = txq->time_stamp +
1857                   msecs_to_jiffies(priv->cfg->base_params->wd_timeout);
1858
1859         if (time_after(jiffies, timeout)) {
1860                 IWL_ERR(priv, "Queue %d stuck for %u ms.\n",
1861                                 q->id, priv->cfg->base_params->wd_timeout);
1862                 ret = iwl_force_reset(priv, IWL_FW_RESET, false);
1863                 return (ret == -EAGAIN) ? 0 : 1;
1864         }
1865
1866         return 0;
1867 }
1868
1869 /*
1870  * Making watchdog tick be a quarter of timeout assure we will
1871  * discover the queue hung between timeout and 1.25*timeout
1872  */
1873 #define IWL_WD_TICK(timeout) ((timeout) / 4)
1874
1875 /*
1876  * Watchdog timer callback, we check each tx queue for stuck, if if hung
1877  * we reset the firmware. If everything is fine just rearm the timer.
1878  */
1879 void iwl_bg_watchdog(unsigned long data)
1880 {
1881         struct iwl_priv *priv = (struct iwl_priv *)data;
1882         int cnt;
1883         unsigned long timeout;
1884
1885         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1886                 return;
1887
1888         timeout = priv->cfg->base_params->wd_timeout;
1889         if (timeout == 0)
1890                 return;
1891
1892         /* monitor and check for stuck cmd queue */
1893         if (iwl_check_stuck_queue(priv, priv->cmd_queue))
1894                 return;
1895
1896         /* monitor and check for other stuck queues */
1897         if (iwl_is_any_associated(priv)) {
1898                 for (cnt = 0; cnt < priv->hw_params.max_txq_num; cnt++) {
1899                         /* skip as we already checked the command queue */
1900                         if (cnt == priv->cmd_queue)
1901                                 continue;
1902                         if (iwl_check_stuck_queue(priv, cnt))
1903                                 return;
1904                 }
1905         }
1906
1907         mod_timer(&priv->watchdog, jiffies +
1908                   msecs_to_jiffies(IWL_WD_TICK(timeout)));
1909 }
1910
1911 void iwl_setup_watchdog(struct iwl_priv *priv)
1912 {
1913         unsigned int timeout = priv->cfg->base_params->wd_timeout;
1914
1915         if (timeout)
1916                 mod_timer(&priv->watchdog,
1917                           jiffies + msecs_to_jiffies(IWL_WD_TICK(timeout)));
1918         else
1919                 del_timer(&priv->watchdog);
1920 }
1921
1922 /*
1923  * extended beacon time format
1924  * time in usec will be changed into a 32-bit value in extended:internal format
1925  * the extended part is the beacon counts
1926  * the internal part is the time in usec within one beacon interval
1927  */
1928 u32 iwl_usecs_to_beacons(struct iwl_priv *priv, u32 usec, u32 beacon_interval)
1929 {
1930         u32 quot;
1931         u32 rem;
1932         u32 interval = beacon_interval * TIME_UNIT;
1933
1934         if (!interval || !usec)
1935                 return 0;
1936
1937         quot = (usec / interval) &
1938                 (iwl_beacon_time_mask_high(priv,
1939                 priv->hw_params.beacon_time_tsf_bits) >>
1940                 priv->hw_params.beacon_time_tsf_bits);
1941         rem = (usec % interval) & iwl_beacon_time_mask_low(priv,
1942                                    priv->hw_params.beacon_time_tsf_bits);
1943
1944         return (quot << priv->hw_params.beacon_time_tsf_bits) + rem;
1945 }
1946
1947 /* base is usually what we get from ucode with each received frame,
1948  * the same as HW timer counter counting down
1949  */
1950 __le32 iwl_add_beacon_time(struct iwl_priv *priv, u32 base,
1951                            u32 addon, u32 beacon_interval)
1952 {
1953         u32 base_low = base & iwl_beacon_time_mask_low(priv,
1954                                         priv->hw_params.beacon_time_tsf_bits);
1955         u32 addon_low = addon & iwl_beacon_time_mask_low(priv,
1956                                         priv->hw_params.beacon_time_tsf_bits);
1957         u32 interval = beacon_interval * TIME_UNIT;
1958         u32 res = (base & iwl_beacon_time_mask_high(priv,
1959                                 priv->hw_params.beacon_time_tsf_bits)) +
1960                                 (addon & iwl_beacon_time_mask_high(priv,
1961                                 priv->hw_params.beacon_time_tsf_bits));
1962
1963         if (base_low > addon_low)
1964                 res += base_low - addon_low;
1965         else if (base_low < addon_low) {
1966                 res += interval + base_low - addon_low;
1967                 res += (1 << priv->hw_params.beacon_time_tsf_bits);
1968         } else
1969                 res += (1 << priv->hw_params.beacon_time_tsf_bits);
1970
1971         return cpu_to_le32(res);
1972 }
1973
1974 #ifdef CONFIG_PM
1975
1976 int iwl_pci_suspend(struct device *device)
1977 {
1978         struct pci_dev *pdev = to_pci_dev(device);
1979         struct iwl_priv *priv = pci_get_drvdata(pdev);
1980
1981         /*
1982          * This function is called when system goes into suspend state
1983          * mac80211 will call iwl_mac_stop() from the mac80211 suspend function
1984          * first but since iwl_mac_stop() has no knowledge of who the caller is,
1985          * it will not call apm_ops.stop() to stop the DMA operation.
1986          * Calling apm_ops.stop here to make sure we stop the DMA.
1987          */
1988         iwl_apm_stop(priv);
1989
1990         return 0;
1991 }
1992
1993 int iwl_pci_resume(struct device *device)
1994 {
1995         struct pci_dev *pdev = to_pci_dev(device);
1996         struct iwl_priv *priv = pci_get_drvdata(pdev);
1997         bool hw_rfkill = false;
1998
1999         /*
2000          * We disable the RETRY_TIMEOUT register (0x41) to keep
2001          * PCI Tx retries from interfering with C3 CPU state.
2002          */
2003         pci_write_config_byte(pdev, PCI_CFG_RETRY_TIMEOUT, 0x00);
2004
2005         iwl_enable_interrupts(priv);
2006
2007         if (!(iwl_read32(priv, CSR_GP_CNTRL) &
2008                                 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
2009                 hw_rfkill = true;
2010
2011         if (hw_rfkill)
2012                 set_bit(STATUS_RF_KILL_HW, &priv->status);
2013         else
2014                 clear_bit(STATUS_RF_KILL_HW, &priv->status);
2015
2016         wiphy_rfkill_set_hw_state(priv->hw->wiphy, hw_rfkill);
2017
2018         return 0;
2019 }
2020
2021 const struct dev_pm_ops iwl_pm_ops = {
2022         .suspend = iwl_pci_suspend,
2023         .resume = iwl_pci_resume,
2024         .freeze = iwl_pci_suspend,
2025         .thaw = iwl_pci_resume,
2026         .poweroff = iwl_pci_suspend,
2027         .restore = iwl_pci_resume,
2028 };
2029
2030 #endif /* CONFIG_PM */