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