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