iwlwifi: remove un-necessary return
[pandora-kernel.git] / drivers / net / wireless / iwlwifi / iwl-core.c
1 /******************************************************************************
2  *
3  * GPL LICENSE SUMMARY
4  *
5  * Copyright(c) 2008 - 2012 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-debug.h"
38 #include "iwl-core.h"
39 #include "iwl-io.h"
40 #include "iwl-power.h"
41 #include "iwl-agn.h"
42 #include "iwl-shared.h"
43 #include "iwl-agn.h"
44 #include "iwl-trans.h"
45
46 const u8 iwl_bcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
47
48 #define MAX_BIT_RATE_40_MHZ 150 /* Mbps */
49 #define MAX_BIT_RATE_20_MHZ 72 /* Mbps */
50 static void iwl_init_ht_hw_capab(const struct iwl_priv *priv,
51                               struct ieee80211_sta_ht_cap *ht_info,
52                               enum ieee80211_band band)
53 {
54         u16 max_bit_rate = 0;
55         u8 rx_chains_num = hw_params(priv).rx_chains_num;
56         u8 tx_chains_num = hw_params(priv).tx_chains_num;
57
58         ht_info->cap = 0;
59         memset(&ht_info->mcs, 0, sizeof(ht_info->mcs));
60
61         ht_info->ht_supported = true;
62
63         if (cfg(priv)->ht_params &&
64             cfg(priv)->ht_params->ht_greenfield_support)
65                 ht_info->cap |= IEEE80211_HT_CAP_GRN_FLD;
66         ht_info->cap |= IEEE80211_HT_CAP_SGI_20;
67         max_bit_rate = MAX_BIT_RATE_20_MHZ;
68         if (hw_params(priv).ht40_channel & BIT(band)) {
69                 ht_info->cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
70                 ht_info->cap |= IEEE80211_HT_CAP_SGI_40;
71                 ht_info->mcs.rx_mask[4] = 0x01;
72                 max_bit_rate = MAX_BIT_RATE_40_MHZ;
73         }
74
75         if (iwlagn_mod_params.amsdu_size_8K)
76                 ht_info->cap |= IEEE80211_HT_CAP_MAX_AMSDU;
77
78         ht_info->ampdu_factor = CFG_HT_RX_AMPDU_FACTOR_DEF;
79         ht_info->ampdu_density = CFG_HT_MPDU_DENSITY_DEF;
80
81         ht_info->mcs.rx_mask[0] = 0xFF;
82         if (rx_chains_num >= 2)
83                 ht_info->mcs.rx_mask[1] = 0xFF;
84         if (rx_chains_num >= 3)
85                 ht_info->mcs.rx_mask[2] = 0xFF;
86
87         /* Highest supported Rx data rate */
88         max_bit_rate *= rx_chains_num;
89         WARN_ON(max_bit_rate & ~IEEE80211_HT_MCS_RX_HIGHEST_MASK);
90         ht_info->mcs.rx_highest = cpu_to_le16(max_bit_rate);
91
92         /* Tx MCS capabilities */
93         ht_info->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
94         if (tx_chains_num != rx_chains_num) {
95                 ht_info->mcs.tx_params |= IEEE80211_HT_MCS_TX_RX_DIFF;
96                 ht_info->mcs.tx_params |= ((tx_chains_num - 1) <<
97                                 IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT);
98         }
99 }
100
101 /**
102  * iwl_init_geos - Initialize mac80211's geo/channel info based from eeprom
103  */
104 int iwl_init_geos(struct iwl_priv *priv)
105 {
106         struct iwl_channel_info *ch;
107         struct ieee80211_supported_band *sband;
108         struct ieee80211_channel *channels;
109         struct ieee80211_channel *geo_ch;
110         struct ieee80211_rate *rates;
111         int i = 0;
112         s8 max_tx_power = IWLAGN_TX_POWER_TARGET_POWER_MIN;
113
114         if (priv->bands[IEEE80211_BAND_2GHZ].n_bitrates ||
115             priv->bands[IEEE80211_BAND_5GHZ].n_bitrates) {
116                 IWL_DEBUG_INFO(priv, "Geography modes already initialized.\n");
117                 set_bit(STATUS_GEO_CONFIGURED, &priv->shrd->status);
118                 return 0;
119         }
120
121         channels = kcalloc(priv->channel_count,
122                            sizeof(struct ieee80211_channel), GFP_KERNEL);
123         if (!channels)
124                 return -ENOMEM;
125
126         rates = kcalloc(IWL_RATE_COUNT_LEGACY, sizeof(struct ieee80211_rate),
127                         GFP_KERNEL);
128         if (!rates) {
129                 kfree(channels);
130                 return -ENOMEM;
131         }
132
133         /* 5.2GHz channels start after the 2.4GHz channels */
134         sband = &priv->bands[IEEE80211_BAND_5GHZ];
135         sband->channels = &channels[ARRAY_SIZE(iwl_eeprom_band_1)];
136         /* just OFDM */
137         sband->bitrates = &rates[IWL_FIRST_OFDM_RATE];
138         sband->n_bitrates = IWL_RATE_COUNT_LEGACY - IWL_FIRST_OFDM_RATE;
139
140         if (cfg(priv)->sku & EEPROM_SKU_CAP_11N_ENABLE)
141                 iwl_init_ht_hw_capab(priv, &sband->ht_cap,
142                                          IEEE80211_BAND_5GHZ);
143
144         sband = &priv->bands[IEEE80211_BAND_2GHZ];
145         sband->channels = channels;
146         /* OFDM & CCK */
147         sband->bitrates = rates;
148         sband->n_bitrates = IWL_RATE_COUNT_LEGACY;
149
150         if (cfg(priv)->sku & EEPROM_SKU_CAP_11N_ENABLE)
151                 iwl_init_ht_hw_capab(priv, &sband->ht_cap,
152                                          IEEE80211_BAND_2GHZ);
153
154         priv->ieee_channels = channels;
155         priv->ieee_rates = rates;
156
157         for (i = 0;  i < priv->channel_count; i++) {
158                 ch = &priv->channel_info[i];
159
160                 /* FIXME: might be removed if scan is OK */
161                 if (!is_channel_valid(ch))
162                         continue;
163
164                 sband =  &priv->bands[ch->band];
165
166                 geo_ch = &sband->channels[sband->n_channels++];
167
168                 geo_ch->center_freq =
169                         ieee80211_channel_to_frequency(ch->channel, ch->band);
170                 geo_ch->max_power = ch->max_power_avg;
171                 geo_ch->max_antenna_gain = 0xff;
172                 geo_ch->hw_value = ch->channel;
173
174                 if (is_channel_valid(ch)) {
175                         if (!(ch->flags & EEPROM_CHANNEL_IBSS))
176                                 geo_ch->flags |= IEEE80211_CHAN_NO_IBSS;
177
178                         if (!(ch->flags & EEPROM_CHANNEL_ACTIVE))
179                                 geo_ch->flags |= IEEE80211_CHAN_PASSIVE_SCAN;
180
181                         if (ch->flags & EEPROM_CHANNEL_RADAR)
182                                 geo_ch->flags |= IEEE80211_CHAN_RADAR;
183
184                         geo_ch->flags |= ch->ht40_extension_channel;
185
186                         if (ch->max_power_avg > max_tx_power)
187                                 max_tx_power = ch->max_power_avg;
188                 } else {
189                         geo_ch->flags |= IEEE80211_CHAN_DISABLED;
190                 }
191
192                 IWL_DEBUG_INFO(priv, "Channel %d Freq=%d[%sGHz] %s flag=0x%X\n",
193                                 ch->channel, geo_ch->center_freq,
194                                 is_channel_a_band(ch) ?  "5.2" : "2.4",
195                                 geo_ch->flags & IEEE80211_CHAN_DISABLED ?
196                                 "restricted" : "valid",
197                                  geo_ch->flags);
198         }
199
200         priv->tx_power_device_lmt = max_tx_power;
201         priv->tx_power_user_lmt = max_tx_power;
202         priv->tx_power_next = max_tx_power;
203
204         if ((priv->bands[IEEE80211_BAND_5GHZ].n_channels == 0) &&
205              cfg(priv)->sku & EEPROM_SKU_CAP_BAND_52GHZ) {
206                 IWL_INFO(priv, "Incorrectly detected BG card as ABG. "
207                         "Please send your %s to maintainer.\n",
208                         trans(priv)->hw_id_str);
209                 cfg(priv)->sku &= ~EEPROM_SKU_CAP_BAND_52GHZ;
210         }
211
212         IWL_INFO(priv, "Tunable channels: %d 802.11bg, %d 802.11a channels\n",
213                    priv->bands[IEEE80211_BAND_2GHZ].n_channels,
214                    priv->bands[IEEE80211_BAND_5GHZ].n_channels);
215
216         set_bit(STATUS_GEO_CONFIGURED, &priv->shrd->status);
217
218         return 0;
219 }
220
221 /*
222  * iwl_free_geos - undo allocations in iwl_init_geos
223  */
224 void iwl_free_geos(struct iwl_priv *priv)
225 {
226         kfree(priv->ieee_channels);
227         kfree(priv->ieee_rates);
228         clear_bit(STATUS_GEO_CONFIGURED, &priv->shrd->status);
229 }
230
231 static bool iwl_is_channel_extension(struct iwl_priv *priv,
232                                      enum ieee80211_band band,
233                                      u16 channel, u8 extension_chan_offset)
234 {
235         const struct iwl_channel_info *ch_info;
236
237         ch_info = iwl_get_channel_info(priv, band, channel);
238         if (!is_channel_valid(ch_info))
239                 return false;
240
241         if (extension_chan_offset == IEEE80211_HT_PARAM_CHA_SEC_ABOVE)
242                 return !(ch_info->ht40_extension_channel &
243                                         IEEE80211_CHAN_NO_HT40PLUS);
244         else if (extension_chan_offset == IEEE80211_HT_PARAM_CHA_SEC_BELOW)
245                 return !(ch_info->ht40_extension_channel &
246                                         IEEE80211_CHAN_NO_HT40MINUS);
247
248         return false;
249 }
250
251 bool iwl_is_ht40_tx_allowed(struct iwl_priv *priv,
252                             struct iwl_rxon_context *ctx,
253                             struct ieee80211_sta_ht_cap *ht_cap)
254 {
255         if (!ctx->ht.enabled || !ctx->ht.is_40mhz)
256                 return false;
257
258         /*
259          * We do not check for IEEE80211_HT_CAP_SUP_WIDTH_20_40
260          * the bit will not set if it is pure 40MHz case
261          */
262         if (ht_cap && !ht_cap->ht_supported)
263                 return false;
264
265 #ifdef CONFIG_IWLWIFI_DEBUGFS
266         if (priv->disable_ht40)
267                 return false;
268 #endif
269
270         return iwl_is_channel_extension(priv, priv->band,
271                         le16_to_cpu(ctx->staging.channel),
272                         ctx->ht.extension_chan_offset);
273 }
274
275 static u16 iwl_adjust_beacon_interval(u16 beacon_val, u16 max_beacon_val)
276 {
277         u16 new_val;
278         u16 beacon_factor;
279
280         /*
281          * If mac80211 hasn't given us a beacon interval, program
282          * the default into the device (not checking this here
283          * would cause the adjustment below to return the maximum
284          * value, which may break PAN.)
285          */
286         if (!beacon_val)
287                 return DEFAULT_BEACON_INTERVAL;
288
289         /*
290          * If the beacon interval we obtained from the peer
291          * is too large, we'll have to wake up more often
292          * (and in IBSS case, we'll beacon too much)
293          *
294          * For example, if max_beacon_val is 4096, and the
295          * requested beacon interval is 7000, we'll have to
296          * use 3500 to be able to wake up on the beacons.
297          *
298          * This could badly influence beacon detection stats.
299          */
300
301         beacon_factor = (beacon_val + max_beacon_val) / max_beacon_val;
302         new_val = beacon_val / beacon_factor;
303
304         if (!new_val)
305                 new_val = max_beacon_val;
306
307         return new_val;
308 }
309
310 int iwl_send_rxon_timing(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
311 {
312         u64 tsf;
313         s32 interval_tm, rem;
314         struct ieee80211_conf *conf = NULL;
315         u16 beacon_int;
316         struct ieee80211_vif *vif = ctx->vif;
317
318         conf = &priv->hw->conf;
319
320         lockdep_assert_held(&priv->shrd->mutex);
321
322         memset(&ctx->timing, 0, sizeof(struct iwl_rxon_time_cmd));
323
324         ctx->timing.timestamp = cpu_to_le64(priv->timestamp);
325         ctx->timing.listen_interval = cpu_to_le16(conf->listen_interval);
326
327         beacon_int = vif ? vif->bss_conf.beacon_int : 0;
328
329         /*
330          * TODO: For IBSS we need to get atim_window from mac80211,
331          *       for now just always use 0
332          */
333         ctx->timing.atim_window = 0;
334
335         if (ctx->ctxid == IWL_RXON_CTX_PAN &&
336             (!ctx->vif || ctx->vif->type != NL80211_IFTYPE_STATION) &&
337             iwl_is_associated(priv, IWL_RXON_CTX_BSS) &&
338             priv->contexts[IWL_RXON_CTX_BSS].vif &&
339             priv->contexts[IWL_RXON_CTX_BSS].vif->bss_conf.beacon_int) {
340                 ctx->timing.beacon_interval =
341                         priv->contexts[IWL_RXON_CTX_BSS].timing.beacon_interval;
342                 beacon_int = le16_to_cpu(ctx->timing.beacon_interval);
343         } else if (ctx->ctxid == IWL_RXON_CTX_BSS &&
344                    iwl_is_associated(priv, IWL_RXON_CTX_PAN) &&
345                    priv->contexts[IWL_RXON_CTX_PAN].vif &&
346                    priv->contexts[IWL_RXON_CTX_PAN].vif->bss_conf.beacon_int &&
347                    (!iwl_is_associated_ctx(ctx) || !ctx->vif ||
348                     !ctx->vif->bss_conf.beacon_int)) {
349                 ctx->timing.beacon_interval =
350                         priv->contexts[IWL_RXON_CTX_PAN].timing.beacon_interval;
351                 beacon_int = le16_to_cpu(ctx->timing.beacon_interval);
352         } else {
353                 beacon_int = iwl_adjust_beacon_interval(beacon_int,
354                         IWL_MAX_UCODE_BEACON_INTERVAL * TIME_UNIT);
355                 ctx->timing.beacon_interval = cpu_to_le16(beacon_int);
356         }
357
358         ctx->beacon_int = beacon_int;
359
360         tsf = priv->timestamp; /* tsf is modifed by do_div: copy it */
361         interval_tm = beacon_int * TIME_UNIT;
362         rem = do_div(tsf, interval_tm);
363         ctx->timing.beacon_init_val = cpu_to_le32(interval_tm - rem);
364
365         ctx->timing.dtim_period = vif ? (vif->bss_conf.dtim_period ?: 1) : 1;
366
367         IWL_DEBUG_ASSOC(priv,
368                         "beacon interval %d beacon timer %d beacon tim %d\n",
369                         le16_to_cpu(ctx->timing.beacon_interval),
370                         le32_to_cpu(ctx->timing.beacon_init_val),
371                         le16_to_cpu(ctx->timing.atim_window));
372
373         return iwl_trans_send_cmd_pdu(trans(priv), ctx->rxon_timing_cmd,
374                                 CMD_SYNC, sizeof(ctx->timing), &ctx->timing);
375 }
376
377 void iwl_set_rxon_hwcrypto(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
378                            int hw_decrypt)
379 {
380         struct iwl_rxon_cmd *rxon = &ctx->staging;
381
382         if (hw_decrypt)
383                 rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
384         else
385                 rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
386
387 }
388
389 /* validate RXON structure is valid */
390 int iwl_check_rxon_cmd(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
391 {
392         struct iwl_rxon_cmd *rxon = &ctx->staging;
393         u32 errors = 0;
394
395         if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
396                 if (rxon->flags & RXON_FLG_TGJ_NARROW_BAND_MSK) {
397                         IWL_WARN(priv, "check 2.4G: wrong narrow\n");
398                         errors |= BIT(0);
399                 }
400                 if (rxon->flags & RXON_FLG_RADAR_DETECT_MSK) {
401                         IWL_WARN(priv, "check 2.4G: wrong radar\n");
402                         errors |= BIT(1);
403                 }
404         } else {
405                 if (!(rxon->flags & RXON_FLG_SHORT_SLOT_MSK)) {
406                         IWL_WARN(priv, "check 5.2G: not short slot!\n");
407                         errors |= BIT(2);
408                 }
409                 if (rxon->flags & RXON_FLG_CCK_MSK) {
410                         IWL_WARN(priv, "check 5.2G: CCK!\n");
411                         errors |= BIT(3);
412                 }
413         }
414         if ((rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1) {
415                 IWL_WARN(priv, "mac/bssid mcast!\n");
416                 errors |= BIT(4);
417         }
418
419         /* make sure basic rates 6Mbps and 1Mbps are supported */
420         if ((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0 &&
421             (rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0) {
422                 IWL_WARN(priv, "neither 1 nor 6 are basic\n");
423                 errors |= BIT(5);
424         }
425
426         if (le16_to_cpu(rxon->assoc_id) > 2007) {
427                 IWL_WARN(priv, "aid > 2007\n");
428                 errors |= BIT(6);
429         }
430
431         if ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
432                         == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK)) {
433                 IWL_WARN(priv, "CCK and short slot\n");
434                 errors |= BIT(7);
435         }
436
437         if ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
438                         == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK)) {
439                 IWL_WARN(priv, "CCK and auto detect");
440                 errors |= BIT(8);
441         }
442
443         if ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
444                             RXON_FLG_TGG_PROTECT_MSK)) ==
445                             RXON_FLG_TGG_PROTECT_MSK) {
446                 IWL_WARN(priv, "TGg but no auto-detect\n");
447                 errors |= BIT(9);
448         }
449
450         if (rxon->channel == 0) {
451                 IWL_WARN(priv, "zero channel is invalid\n");
452                 errors |= BIT(10);
453         }
454
455         WARN(errors, "Invalid RXON (%#x), channel %d",
456              errors, le16_to_cpu(rxon->channel));
457
458         return errors ? -EINVAL : 0;
459 }
460
461 /**
462  * iwl_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
463  * @priv: staging_rxon is compared to active_rxon
464  *
465  * If the RXON structure is changing enough to require a new tune,
466  * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
467  * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
468  */
469 int iwl_full_rxon_required(struct iwl_priv *priv,
470                            struct iwl_rxon_context *ctx)
471 {
472         const struct iwl_rxon_cmd *staging = &ctx->staging;
473         const struct iwl_rxon_cmd *active = &ctx->active;
474
475 #define CHK(cond)                                                       \
476         if ((cond)) {                                                   \
477                 IWL_DEBUG_INFO(priv, "need full RXON - " #cond "\n");   \
478                 return 1;                                               \
479         }
480
481 #define CHK_NEQ(c1, c2)                                         \
482         if ((c1) != (c2)) {                                     \
483                 IWL_DEBUG_INFO(priv, "need full RXON - "        \
484                                #c1 " != " #c2 " - %d != %d\n",  \
485                                (c1), (c2));                     \
486                 return 1;                                       \
487         }
488
489         /* These items are only settable from the full RXON command */
490         CHK(!iwl_is_associated_ctx(ctx));
491         CHK(compare_ether_addr(staging->bssid_addr, active->bssid_addr));
492         CHK(compare_ether_addr(staging->node_addr, active->node_addr));
493         CHK(compare_ether_addr(staging->wlap_bssid_addr,
494                                 active->wlap_bssid_addr));
495         CHK_NEQ(staging->dev_type, active->dev_type);
496         CHK_NEQ(staging->channel, active->channel);
497         CHK_NEQ(staging->air_propagation, active->air_propagation);
498         CHK_NEQ(staging->ofdm_ht_single_stream_basic_rates,
499                 active->ofdm_ht_single_stream_basic_rates);
500         CHK_NEQ(staging->ofdm_ht_dual_stream_basic_rates,
501                 active->ofdm_ht_dual_stream_basic_rates);
502         CHK_NEQ(staging->ofdm_ht_triple_stream_basic_rates,
503                 active->ofdm_ht_triple_stream_basic_rates);
504         CHK_NEQ(staging->assoc_id, active->assoc_id);
505
506         /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
507          * be updated with the RXON_ASSOC command -- however only some
508          * flag transitions are allowed using RXON_ASSOC */
509
510         /* Check if we are not switching bands */
511         CHK_NEQ(staging->flags & RXON_FLG_BAND_24G_MSK,
512                 active->flags & RXON_FLG_BAND_24G_MSK);
513
514         /* Check if we are switching association toggle */
515         CHK_NEQ(staging->filter_flags & RXON_FILTER_ASSOC_MSK,
516                 active->filter_flags & RXON_FILTER_ASSOC_MSK);
517
518 #undef CHK
519 #undef CHK_NEQ
520
521         return 0;
522 }
523
524 static void _iwl_set_rxon_ht(struct iwl_priv *priv,
525                              struct iwl_ht_config *ht_conf,
526                              struct iwl_rxon_context *ctx)
527 {
528         struct iwl_rxon_cmd *rxon = &ctx->staging;
529
530         if (!ctx->ht.enabled) {
531                 rxon->flags &= ~(RXON_FLG_CHANNEL_MODE_MSK |
532                         RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK |
533                         RXON_FLG_HT40_PROT_MSK |
534                         RXON_FLG_HT_PROT_MSK);
535                 return;
536         }
537
538         /* FIXME: if the definition of ht.protection changed, the "translation"
539          * will be needed for rxon->flags
540          */
541         rxon->flags |= cpu_to_le32(ctx->ht.protection << RXON_FLG_HT_OPERATING_MODE_POS);
542
543         /* Set up channel bandwidth:
544          * 20 MHz only, 20/40 mixed or pure 40 if ht40 ok */
545         /* clear the HT channel mode before set the mode */
546         rxon->flags &= ~(RXON_FLG_CHANNEL_MODE_MSK |
547                          RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK);
548         if (iwl_is_ht40_tx_allowed(priv, ctx, NULL)) {
549                 /* pure ht40 */
550                 if (ctx->ht.protection == IEEE80211_HT_OP_MODE_PROTECTION_20MHZ) {
551                         rxon->flags |= RXON_FLG_CHANNEL_MODE_PURE_40;
552                         /* Note: control channel is opposite of extension channel */
553                         switch (ctx->ht.extension_chan_offset) {
554                         case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
555                                 rxon->flags &= ~RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
556                                 break;
557                         case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
558                                 rxon->flags |= RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
559                                 break;
560                         }
561                 } else {
562                         /* Note: control channel is opposite of extension channel */
563                         switch (ctx->ht.extension_chan_offset) {
564                         case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
565                                 rxon->flags &= ~(RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK);
566                                 rxon->flags |= RXON_FLG_CHANNEL_MODE_MIXED;
567                                 break;
568                         case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
569                                 rxon->flags |= RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
570                                 rxon->flags |= RXON_FLG_CHANNEL_MODE_MIXED;
571                                 break;
572                         case IEEE80211_HT_PARAM_CHA_SEC_NONE:
573                         default:
574                                 /* channel location only valid if in Mixed mode */
575                                 IWL_ERR(priv, "invalid extension channel offset\n");
576                                 break;
577                         }
578                 }
579         } else {
580                 rxon->flags |= RXON_FLG_CHANNEL_MODE_LEGACY;
581         }
582
583         iwlagn_set_rxon_chain(priv, ctx);
584
585         IWL_DEBUG_ASSOC(priv, "rxon flags 0x%X operation mode :0x%X "
586                         "extension channel offset 0x%x\n",
587                         le32_to_cpu(rxon->flags), ctx->ht.protection,
588                         ctx->ht.extension_chan_offset);
589 }
590
591 void iwl_set_rxon_ht(struct iwl_priv *priv, struct iwl_ht_config *ht_conf)
592 {
593         struct iwl_rxon_context *ctx;
594
595         for_each_context(priv, ctx)
596                 _iwl_set_rxon_ht(priv, ht_conf, ctx);
597 }
598
599 /* Return valid, unused, channel for a passive scan to reset the RF */
600 u8 iwl_get_single_channel_number(struct iwl_priv *priv,
601                                  enum ieee80211_band band)
602 {
603         const struct iwl_channel_info *ch_info;
604         int i;
605         u8 channel = 0;
606         u8 min, max;
607         struct iwl_rxon_context *ctx;
608
609         if (band == IEEE80211_BAND_5GHZ) {
610                 min = 14;
611                 max = priv->channel_count;
612         } else {
613                 min = 0;
614                 max = 14;
615         }
616
617         for (i = min; i < max; i++) {
618                 bool busy = false;
619
620                 for_each_context(priv, ctx) {
621                         busy = priv->channel_info[i].channel ==
622                                 le16_to_cpu(ctx->staging.channel);
623                         if (busy)
624                                 break;
625                 }
626
627                 if (busy)
628                         continue;
629
630                 channel = priv->channel_info[i].channel;
631                 ch_info = iwl_get_channel_info(priv, band, channel);
632                 if (is_channel_valid(ch_info))
633                         break;
634         }
635
636         return channel;
637 }
638
639 /**
640  * iwl_set_rxon_channel - Set the band and channel values in staging RXON
641  * @ch: requested channel as a pointer to struct ieee80211_channel
642
643  * NOTE:  Does not commit to the hardware; it sets appropriate bit fields
644  * in the staging RXON flag structure based on the ch->band
645  */
646 void iwl_set_rxon_channel(struct iwl_priv *priv, struct ieee80211_channel *ch,
647                          struct iwl_rxon_context *ctx)
648 {
649         enum ieee80211_band band = ch->band;
650         u16 channel = ch->hw_value;
651
652         if ((le16_to_cpu(ctx->staging.channel) == channel) &&
653             (priv->band == band))
654                 return;
655
656         ctx->staging.channel = cpu_to_le16(channel);
657         if (band == IEEE80211_BAND_5GHZ)
658                 ctx->staging.flags &= ~RXON_FLG_BAND_24G_MSK;
659         else
660                 ctx->staging.flags |= RXON_FLG_BAND_24G_MSK;
661
662         priv->band = band;
663
664         IWL_DEBUG_INFO(priv, "Staging channel set to %d [%d]\n", channel, band);
665
666 }
667
668 void iwl_set_flags_for_band(struct iwl_priv *priv,
669                             struct iwl_rxon_context *ctx,
670                             enum ieee80211_band band,
671                             struct ieee80211_vif *vif)
672 {
673         if (band == IEEE80211_BAND_5GHZ) {
674                 ctx->staging.flags &=
675                     ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
676                       | RXON_FLG_CCK_MSK);
677                 ctx->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
678         } else {
679                 /* Copied from iwl_post_associate() */
680                 if (vif && vif->bss_conf.use_short_slot)
681                         ctx->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
682                 else
683                         ctx->staging.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
684
685                 ctx->staging.flags |= RXON_FLG_BAND_24G_MSK;
686                 ctx->staging.flags |= RXON_FLG_AUTO_DETECT_MSK;
687                 ctx->staging.flags &= ~RXON_FLG_CCK_MSK;
688         }
689 }
690
691 /*
692  * initialize rxon structure with default values from eeprom
693  */
694 void iwl_connection_init_rx_config(struct iwl_priv *priv,
695                                    struct iwl_rxon_context *ctx)
696 {
697         const struct iwl_channel_info *ch_info;
698
699         memset(&ctx->staging, 0, sizeof(ctx->staging));
700
701         if (!ctx->vif) {
702                 ctx->staging.dev_type = ctx->unused_devtype;
703         } else switch (ctx->vif->type) {
704         case NL80211_IFTYPE_AP:
705                 ctx->staging.dev_type = ctx->ap_devtype;
706                 break;
707
708         case NL80211_IFTYPE_STATION:
709                 ctx->staging.dev_type = ctx->station_devtype;
710                 ctx->staging.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
711                 break;
712
713         case NL80211_IFTYPE_ADHOC:
714                 ctx->staging.dev_type = ctx->ibss_devtype;
715                 ctx->staging.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
716                 ctx->staging.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
717                                                   RXON_FILTER_ACCEPT_GRP_MSK;
718                 break;
719
720         default:
721                 IWL_ERR(priv, "Unsupported interface type %d\n",
722                         ctx->vif->type);
723                 break;
724         }
725
726 #if 0
727         /* TODO:  Figure out when short_preamble would be set and cache from
728          * that */
729         if (!hw_to_local(priv->hw)->short_preamble)
730                 ctx->staging.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
731         else
732                 ctx->staging.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
733 #endif
734
735         ch_info = iwl_get_channel_info(priv, priv->band,
736                                        le16_to_cpu(ctx->active.channel));
737
738         if (!ch_info)
739                 ch_info = &priv->channel_info[0];
740
741         ctx->staging.channel = cpu_to_le16(ch_info->channel);
742         priv->band = ch_info->band;
743
744         iwl_set_flags_for_band(priv, ctx, priv->band, ctx->vif);
745
746         ctx->staging.ofdm_basic_rates =
747             (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
748         ctx->staging.cck_basic_rates =
749             (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
750
751         /* clear both MIX and PURE40 mode flag */
752         ctx->staging.flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED |
753                                         RXON_FLG_CHANNEL_MODE_PURE_40);
754         if (ctx->vif)
755                 memcpy(ctx->staging.node_addr, ctx->vif->addr, ETH_ALEN);
756
757         ctx->staging.ofdm_ht_single_stream_basic_rates = 0xff;
758         ctx->staging.ofdm_ht_dual_stream_basic_rates = 0xff;
759         ctx->staging.ofdm_ht_triple_stream_basic_rates = 0xff;
760 }
761
762 void iwl_set_rate(struct iwl_priv *priv)
763 {
764         const struct ieee80211_supported_band *hw = NULL;
765         struct ieee80211_rate *rate;
766         struct iwl_rxon_context *ctx;
767         int i;
768
769         hw = iwl_get_hw_mode(priv, priv->band);
770         if (!hw) {
771                 IWL_ERR(priv, "Failed to set rate: unable to get hw mode\n");
772                 return;
773         }
774
775         priv->active_rate = 0;
776
777         for (i = 0; i < hw->n_bitrates; i++) {
778                 rate = &(hw->bitrates[i]);
779                 if (rate->hw_value < IWL_RATE_COUNT_LEGACY)
780                         priv->active_rate |= (1 << rate->hw_value);
781         }
782
783         IWL_DEBUG_RATE(priv, "Set active_rate = %0x\n", priv->active_rate);
784
785         for_each_context(priv, ctx) {
786                 ctx->staging.cck_basic_rates =
787                     (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
788
789                 ctx->staging.ofdm_basic_rates =
790                    (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
791         }
792 }
793
794 void iwl_chswitch_done(struct iwl_priv *priv, bool is_success)
795 {
796         /*
797          * MULTI-FIXME
798          * See iwlagn_mac_channel_switch.
799          */
800         struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
801
802         if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
803                 return;
804
805         if (test_and_clear_bit(STATUS_CHANNEL_SWITCH_PENDING,
806                                 &priv->shrd->status))
807                 ieee80211_chswitch_done(ctx->vif, is_success);
808 }
809
810 #ifdef CONFIG_IWLWIFI_DEBUG
811 void iwl_print_rx_config_cmd(struct iwl_priv *priv,
812                              enum iwl_rxon_context_id ctxid)
813 {
814         struct iwl_rxon_context *ctx = &priv->contexts[ctxid];
815         struct iwl_rxon_cmd *rxon = &ctx->staging;
816
817         IWL_DEBUG_RADIO(priv, "RX CONFIG:\n");
818         iwl_print_hex_dump(priv, IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
819         IWL_DEBUG_RADIO(priv, "u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
820         IWL_DEBUG_RADIO(priv, "u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
821         IWL_DEBUG_RADIO(priv, "u32 filter_flags: 0x%08x\n",
822                         le32_to_cpu(rxon->filter_flags));
823         IWL_DEBUG_RADIO(priv, "u8 dev_type: 0x%x\n", rxon->dev_type);
824         IWL_DEBUG_RADIO(priv, "u8 ofdm_basic_rates: 0x%02x\n",
825                         rxon->ofdm_basic_rates);
826         IWL_DEBUG_RADIO(priv, "u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
827         IWL_DEBUG_RADIO(priv, "u8[6] node_addr: %pM\n", rxon->node_addr);
828         IWL_DEBUG_RADIO(priv, "u8[6] bssid_addr: %pM\n", rxon->bssid_addr);
829         IWL_DEBUG_RADIO(priv, "u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
830 }
831 #endif
832
833 void iwlagn_fw_error(struct iwl_priv *priv, bool ondemand)
834 {
835         unsigned int reload_msec;
836         unsigned long reload_jiffies;
837
838         /* Set the FW error flag -- cleared on iwl_down */
839         set_bit(STATUS_FW_ERROR, &priv->shrd->status);
840
841         /* Cancel currently queued command. */
842         clear_bit(STATUS_HCMD_ACTIVE, &priv->shrd->status);
843
844         iwl_abort_notification_waits(priv->shrd);
845
846         /* Keep the restart process from trying to send host
847          * commands by clearing the ready bit */
848         clear_bit(STATUS_READY, &priv->shrd->status);
849
850         wake_up(&priv->shrd->wait_command_queue);
851
852         if (!ondemand) {
853                 /*
854                  * If firmware keep reloading, then it indicate something
855                  * serious wrong and firmware having problem to recover
856                  * from it. Instead of keep trying which will fill the syslog
857                  * and hang the system, let's just stop it
858                  */
859                 reload_jiffies = jiffies;
860                 reload_msec = jiffies_to_msecs((long) reload_jiffies -
861                                         (long) priv->reload_jiffies);
862                 priv->reload_jiffies = reload_jiffies;
863                 if (reload_msec <= IWL_MIN_RELOAD_DURATION) {
864                         priv->reload_count++;
865                         if (priv->reload_count >= IWL_MAX_CONTINUE_RELOAD_CNT) {
866                                 IWL_ERR(priv, "BUG_ON, Stop restarting\n");
867                                 return;
868                         }
869                 } else
870                         priv->reload_count = 0;
871         }
872
873         if (!test_bit(STATUS_EXIT_PENDING, &priv->shrd->status)) {
874                 if (iwlagn_mod_params.restart_fw) {
875                         IWL_DEBUG_FW_ERRORS(priv,
876                                   "Restarting adapter due to uCode error.\n");
877                         queue_work(priv->workqueue, &priv->restart);
878                 } else
879                         IWL_DEBUG_FW_ERRORS(priv,
880                                   "Detected FW error, but not restarting\n");
881         }
882 }
883
884 int iwl_set_tx_power(struct iwl_priv *priv, s8 tx_power, bool force)
885 {
886         int ret;
887         s8 prev_tx_power;
888         bool defer;
889         struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
890
891         lockdep_assert_held(&priv->shrd->mutex);
892
893         if (priv->tx_power_user_lmt == tx_power && !force)
894                 return 0;
895
896         if (tx_power < IWLAGN_TX_POWER_TARGET_POWER_MIN) {
897                 IWL_WARN(priv,
898                          "Requested user TXPOWER %d below lower limit %d.\n",
899                          tx_power,
900                          IWLAGN_TX_POWER_TARGET_POWER_MIN);
901                 return -EINVAL;
902         }
903
904         if (tx_power > priv->tx_power_device_lmt) {
905                 IWL_WARN(priv,
906                         "Requested user TXPOWER %d above upper limit %d.\n",
907                          tx_power, priv->tx_power_device_lmt);
908                 return -EINVAL;
909         }
910
911         if (!iwl_is_ready_rf(priv->shrd))
912                 return -EIO;
913
914         /* scan complete and commit_rxon use tx_power_next value,
915          * it always need to be updated for newest request */
916         priv->tx_power_next = tx_power;
917
918         /* do not set tx power when scanning or channel changing */
919         defer = test_bit(STATUS_SCANNING, &priv->shrd->status) ||
920                 memcmp(&ctx->active, &ctx->staging, sizeof(ctx->staging));
921         if (defer && !force) {
922                 IWL_DEBUG_INFO(priv, "Deferring tx power set\n");
923                 return 0;
924         }
925
926         prev_tx_power = priv->tx_power_user_lmt;
927         priv->tx_power_user_lmt = tx_power;
928
929         ret = iwlagn_send_tx_power(priv);
930
931         /* if fail to set tx_power, restore the orig. tx power */
932         if (ret) {
933                 priv->tx_power_user_lmt = prev_tx_power;
934                 priv->tx_power_next = prev_tx_power;
935         }
936         return ret;
937 }
938
939 void iwl_send_bt_config(struct iwl_priv *priv)
940 {
941         struct iwl_bt_cmd bt_cmd = {
942                 .lead_time = BT_LEAD_TIME_DEF,
943                 .max_kill = BT_MAX_KILL_DEF,
944                 .kill_ack_mask = 0,
945                 .kill_cts_mask = 0,
946         };
947
948         if (!iwlagn_mod_params.bt_coex_active)
949                 bt_cmd.flags = BT_COEX_DISABLE;
950         else
951                 bt_cmd.flags = BT_COEX_ENABLE;
952
953         priv->bt_enable_flag = bt_cmd.flags;
954         IWL_DEBUG_INFO(priv, "BT coex %s\n",
955                 (bt_cmd.flags == BT_COEX_DISABLE) ? "disable" : "active");
956
957         if (iwl_trans_send_cmd_pdu(trans(priv), REPLY_BT_CONFIG,
958                              CMD_SYNC, sizeof(struct iwl_bt_cmd), &bt_cmd))
959                 IWL_ERR(priv, "failed to send BT Coex Config\n");
960 }
961
962 int iwl_send_statistics_request(struct iwl_priv *priv, u8 flags, bool clear)
963 {
964         struct iwl_statistics_cmd statistics_cmd = {
965                 .configuration_flags =
966                         clear ? IWL_STATS_CONF_CLEAR_STATS : 0,
967         };
968
969         if (flags & CMD_ASYNC)
970                 return iwl_trans_send_cmd_pdu(trans(priv), REPLY_STATISTICS_CMD,
971                                               CMD_ASYNC,
972                                                sizeof(struct iwl_statistics_cmd),
973                                                &statistics_cmd);
974         else
975                 return iwl_trans_send_cmd_pdu(trans(priv), REPLY_STATISTICS_CMD,
976                                         CMD_SYNC,
977                                         sizeof(struct iwl_statistics_cmd),
978                                         &statistics_cmd);
979 }
980
981
982
983
984 #ifdef CONFIG_IWLWIFI_DEBUGFS
985
986 #define IWL_TRAFFIC_DUMP_SIZE   (IWL_TRAFFIC_ENTRY_SIZE * IWL_TRAFFIC_ENTRIES)
987
988 void iwl_reset_traffic_log(struct iwl_priv *priv)
989 {
990         priv->tx_traffic_idx = 0;
991         priv->rx_traffic_idx = 0;
992         if (priv->tx_traffic)
993                 memset(priv->tx_traffic, 0, IWL_TRAFFIC_DUMP_SIZE);
994         if (priv->rx_traffic)
995                 memset(priv->rx_traffic, 0, IWL_TRAFFIC_DUMP_SIZE);
996 }
997
998 int iwl_alloc_traffic_mem(struct iwl_priv *priv)
999 {
1000         u32 traffic_size = IWL_TRAFFIC_DUMP_SIZE;
1001
1002         if (iwl_get_debug_level(priv->shrd) & IWL_DL_TX) {
1003                 if (!priv->tx_traffic) {
1004                         priv->tx_traffic =
1005                                 kzalloc(traffic_size, GFP_KERNEL);
1006                         if (!priv->tx_traffic)
1007                                 return -ENOMEM;
1008                 }
1009         }
1010         if (iwl_get_debug_level(priv->shrd) & IWL_DL_RX) {
1011                 if (!priv->rx_traffic) {
1012                         priv->rx_traffic =
1013                                 kzalloc(traffic_size, GFP_KERNEL);
1014                         if (!priv->rx_traffic)
1015                                 return -ENOMEM;
1016                 }
1017         }
1018         iwl_reset_traffic_log(priv);
1019         return 0;
1020 }
1021
1022 void iwl_free_traffic_mem(struct iwl_priv *priv)
1023 {
1024         kfree(priv->tx_traffic);
1025         priv->tx_traffic = NULL;
1026
1027         kfree(priv->rx_traffic);
1028         priv->rx_traffic = NULL;
1029 }
1030
1031 void iwl_dbg_log_tx_data_frame(struct iwl_priv *priv,
1032                       u16 length, struct ieee80211_hdr *header)
1033 {
1034         __le16 fc;
1035         u16 len;
1036
1037         if (likely(!(iwl_get_debug_level(priv->shrd) & IWL_DL_TX)))
1038                 return;
1039
1040         if (!priv->tx_traffic)
1041                 return;
1042
1043         fc = header->frame_control;
1044         if (ieee80211_is_data(fc)) {
1045                 len = (length > IWL_TRAFFIC_ENTRY_SIZE)
1046                        ? IWL_TRAFFIC_ENTRY_SIZE : length;
1047                 memcpy((priv->tx_traffic +
1048                        (priv->tx_traffic_idx * IWL_TRAFFIC_ENTRY_SIZE)),
1049                        header, len);
1050                 priv->tx_traffic_idx =
1051                         (priv->tx_traffic_idx + 1) % IWL_TRAFFIC_ENTRIES;
1052         }
1053 }
1054
1055 void iwl_dbg_log_rx_data_frame(struct iwl_priv *priv,
1056                       u16 length, struct ieee80211_hdr *header)
1057 {
1058         __le16 fc;
1059         u16 len;
1060
1061         if (likely(!(iwl_get_debug_level(priv->shrd) & IWL_DL_RX)))
1062                 return;
1063
1064         if (!priv->rx_traffic)
1065                 return;
1066
1067         fc = header->frame_control;
1068         if (ieee80211_is_data(fc)) {
1069                 len = (length > IWL_TRAFFIC_ENTRY_SIZE)
1070                        ? IWL_TRAFFIC_ENTRY_SIZE : length;
1071                 memcpy((priv->rx_traffic +
1072                        (priv->rx_traffic_idx * IWL_TRAFFIC_ENTRY_SIZE)),
1073                        header, len);
1074                 priv->rx_traffic_idx =
1075                         (priv->rx_traffic_idx + 1) % IWL_TRAFFIC_ENTRIES;
1076         }
1077 }
1078
1079 const char *get_mgmt_string(int cmd)
1080 {
1081         switch (cmd) {
1082                 IWL_CMD(MANAGEMENT_ASSOC_REQ);
1083                 IWL_CMD(MANAGEMENT_ASSOC_RESP);
1084                 IWL_CMD(MANAGEMENT_REASSOC_REQ);
1085                 IWL_CMD(MANAGEMENT_REASSOC_RESP);
1086                 IWL_CMD(MANAGEMENT_PROBE_REQ);
1087                 IWL_CMD(MANAGEMENT_PROBE_RESP);
1088                 IWL_CMD(MANAGEMENT_BEACON);
1089                 IWL_CMD(MANAGEMENT_ATIM);
1090                 IWL_CMD(MANAGEMENT_DISASSOC);
1091                 IWL_CMD(MANAGEMENT_AUTH);
1092                 IWL_CMD(MANAGEMENT_DEAUTH);
1093                 IWL_CMD(MANAGEMENT_ACTION);
1094         default:
1095                 return "UNKNOWN";
1096
1097         }
1098 }
1099
1100 const char *get_ctrl_string(int cmd)
1101 {
1102         switch (cmd) {
1103                 IWL_CMD(CONTROL_BACK_REQ);
1104                 IWL_CMD(CONTROL_BACK);
1105                 IWL_CMD(CONTROL_PSPOLL);
1106                 IWL_CMD(CONTROL_RTS);
1107                 IWL_CMD(CONTROL_CTS);
1108                 IWL_CMD(CONTROL_ACK);
1109                 IWL_CMD(CONTROL_CFEND);
1110                 IWL_CMD(CONTROL_CFENDACK);
1111         default:
1112                 return "UNKNOWN";
1113
1114         }
1115 }
1116
1117 void iwl_clear_traffic_stats(struct iwl_priv *priv)
1118 {
1119         memset(&priv->tx_stats, 0, sizeof(struct traffic_stats));
1120         memset(&priv->rx_stats, 0, sizeof(struct traffic_stats));
1121 }
1122
1123 /*
1124  * if CONFIG_IWLWIFI_DEBUGFS defined, iwl_update_stats function will
1125  * record all the MGMT, CTRL and DATA pkt for both TX and Rx pass.
1126  * Use debugFs to display the rx/rx_statistics
1127  * if CONFIG_IWLWIFI_DEBUGFS not being defined, then no MGMT and CTRL
1128  * information will be recorded, but DATA pkt still will be recorded
1129  * for the reason of iwl_led.c need to control the led blinking based on
1130  * number of tx and rx data.
1131  *
1132  */
1133 void iwl_update_stats(struct iwl_priv *priv, bool is_tx, __le16 fc, u16 len)
1134 {
1135         struct traffic_stats    *stats;
1136
1137         if (is_tx)
1138                 stats = &priv->tx_stats;
1139         else
1140                 stats = &priv->rx_stats;
1141
1142         if (ieee80211_is_mgmt(fc)) {
1143                 switch (fc & cpu_to_le16(IEEE80211_FCTL_STYPE)) {
1144                 case cpu_to_le16(IEEE80211_STYPE_ASSOC_REQ):
1145                         stats->mgmt[MANAGEMENT_ASSOC_REQ]++;
1146                         break;
1147                 case cpu_to_le16(IEEE80211_STYPE_ASSOC_RESP):
1148                         stats->mgmt[MANAGEMENT_ASSOC_RESP]++;
1149                         break;
1150                 case cpu_to_le16(IEEE80211_STYPE_REASSOC_REQ):
1151                         stats->mgmt[MANAGEMENT_REASSOC_REQ]++;
1152                         break;
1153                 case cpu_to_le16(IEEE80211_STYPE_REASSOC_RESP):
1154                         stats->mgmt[MANAGEMENT_REASSOC_RESP]++;
1155                         break;
1156                 case cpu_to_le16(IEEE80211_STYPE_PROBE_REQ):
1157                         stats->mgmt[MANAGEMENT_PROBE_REQ]++;
1158                         break;
1159                 case cpu_to_le16(IEEE80211_STYPE_PROBE_RESP):
1160                         stats->mgmt[MANAGEMENT_PROBE_RESP]++;
1161                         break;
1162                 case cpu_to_le16(IEEE80211_STYPE_BEACON):
1163                         stats->mgmt[MANAGEMENT_BEACON]++;
1164                         break;
1165                 case cpu_to_le16(IEEE80211_STYPE_ATIM):
1166                         stats->mgmt[MANAGEMENT_ATIM]++;
1167                         break;
1168                 case cpu_to_le16(IEEE80211_STYPE_DISASSOC):
1169                         stats->mgmt[MANAGEMENT_DISASSOC]++;
1170                         break;
1171                 case cpu_to_le16(IEEE80211_STYPE_AUTH):
1172                         stats->mgmt[MANAGEMENT_AUTH]++;
1173                         break;
1174                 case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
1175                         stats->mgmt[MANAGEMENT_DEAUTH]++;
1176                         break;
1177                 case cpu_to_le16(IEEE80211_STYPE_ACTION):
1178                         stats->mgmt[MANAGEMENT_ACTION]++;
1179                         break;
1180                 }
1181         } else if (ieee80211_is_ctl(fc)) {
1182                 switch (fc & cpu_to_le16(IEEE80211_FCTL_STYPE)) {
1183                 case cpu_to_le16(IEEE80211_STYPE_BACK_REQ):
1184                         stats->ctrl[CONTROL_BACK_REQ]++;
1185                         break;
1186                 case cpu_to_le16(IEEE80211_STYPE_BACK):
1187                         stats->ctrl[CONTROL_BACK]++;
1188                         break;
1189                 case cpu_to_le16(IEEE80211_STYPE_PSPOLL):
1190                         stats->ctrl[CONTROL_PSPOLL]++;
1191                         break;
1192                 case cpu_to_le16(IEEE80211_STYPE_RTS):
1193                         stats->ctrl[CONTROL_RTS]++;
1194                         break;
1195                 case cpu_to_le16(IEEE80211_STYPE_CTS):
1196                         stats->ctrl[CONTROL_CTS]++;
1197                         break;
1198                 case cpu_to_le16(IEEE80211_STYPE_ACK):
1199                         stats->ctrl[CONTROL_ACK]++;
1200                         break;
1201                 case cpu_to_le16(IEEE80211_STYPE_CFEND):
1202                         stats->ctrl[CONTROL_CFEND]++;
1203                         break;
1204                 case cpu_to_le16(IEEE80211_STYPE_CFENDACK):
1205                         stats->ctrl[CONTROL_CFENDACK]++;
1206                         break;
1207                 }
1208         } else {
1209                 /* data */
1210                 stats->data_cnt++;
1211                 stats->data_bytes += len;
1212         }
1213 }
1214 #endif
1215
1216 static void iwl_force_rf_reset(struct iwl_priv *priv)
1217 {
1218         if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
1219                 return;
1220
1221         if (!iwl_is_any_associated(priv)) {
1222                 IWL_DEBUG_SCAN(priv, "force reset rejected: not associated\n");
1223                 return;
1224         }
1225         /*
1226          * There is no easy and better way to force reset the radio,
1227          * the only known method is switching channel which will force to
1228          * reset and tune the radio.
1229          * Use internal short scan (single channel) operation to should
1230          * achieve this objective.
1231          * Driver should reset the radio when number of consecutive missed
1232          * beacon, or any other uCode error condition detected.
1233          */
1234         IWL_DEBUG_INFO(priv, "perform radio reset.\n");
1235         iwl_internal_short_hw_scan(priv);
1236 }
1237
1238
1239 int iwl_force_reset(struct iwl_priv *priv, int mode, bool external)
1240 {
1241         struct iwl_force_reset *force_reset;
1242
1243         if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
1244                 return -EINVAL;
1245
1246         if (mode >= IWL_MAX_FORCE_RESET) {
1247                 IWL_DEBUG_INFO(priv, "invalid reset request.\n");
1248                 return -EINVAL;
1249         }
1250         force_reset = &priv->force_reset[mode];
1251         force_reset->reset_request_count++;
1252         if (!external) {
1253                 if (force_reset->last_force_reset_jiffies &&
1254                     time_after(force_reset->last_force_reset_jiffies +
1255                     force_reset->reset_duration, jiffies)) {
1256                         IWL_DEBUG_INFO(priv, "force reset rejected\n");
1257                         force_reset->reset_reject_count++;
1258                         return -EAGAIN;
1259                 }
1260         }
1261         force_reset->reset_success_count++;
1262         force_reset->last_force_reset_jiffies = jiffies;
1263         IWL_DEBUG_INFO(priv, "perform force reset (%d)\n", mode);
1264         switch (mode) {
1265         case IWL_RF_RESET:
1266                 iwl_force_rf_reset(priv);
1267                 break;
1268         case IWL_FW_RESET:
1269                 /*
1270                  * if the request is from external(ex: debugfs),
1271                  * then always perform the request in regardless the module
1272                  * parameter setting
1273                  * if the request is from internal (uCode error or driver
1274                  * detect failure), then fw_restart module parameter
1275                  * need to be check before performing firmware reload
1276                  */
1277                 if (!external && !iwlagn_mod_params.restart_fw) {
1278                         IWL_DEBUG_INFO(priv, "Cancel firmware reload based on "
1279                                        "module parameter setting\n");
1280                         break;
1281                 }
1282                 IWL_ERR(priv, "On demand firmware reload\n");
1283                 iwlagn_fw_error(priv, true);
1284                 break;
1285         }
1286         return 0;
1287 }
1288
1289
1290 int iwl_cmd_echo_test(struct iwl_priv *priv)
1291 {
1292         int ret;
1293         struct iwl_host_cmd cmd = {
1294                 .id = REPLY_ECHO,
1295                 .len = { 0 },
1296                 .flags = CMD_SYNC,
1297         };
1298
1299         ret = iwl_trans_send_cmd(trans(priv), &cmd);
1300         if (ret)
1301                 IWL_ERR(priv, "echo testing fail: 0X%x\n", ret);
1302         else
1303                 IWL_DEBUG_INFO(priv, "echo testing pass\n");
1304         return ret;
1305 }
1306
1307 static inline int iwl_check_stuck_queue(struct iwl_priv *priv, int txq)
1308 {
1309         if (iwl_trans_check_stuck_queue(trans(priv), txq)) {
1310                 int ret;
1311                 ret = iwl_force_reset(priv, IWL_FW_RESET, false);
1312                 return (ret == -EAGAIN) ? 0 : 1;
1313         }
1314         return 0;
1315 }
1316
1317 /*
1318  * Making watchdog tick be a quarter of timeout assure we will
1319  * discover the queue hung between timeout and 1.25*timeout
1320  */
1321 #define IWL_WD_TICK(timeout) ((timeout) / 4)
1322
1323 /*
1324  * Watchdog timer callback, we check each tx queue for stuck, if if hung
1325  * we reset the firmware. If everything is fine just rearm the timer.
1326  */
1327 void iwl_bg_watchdog(unsigned long data)
1328 {
1329         struct iwl_priv *priv = (struct iwl_priv *)data;
1330         int cnt;
1331         unsigned long timeout;
1332
1333         if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
1334                 return;
1335
1336         if (iwl_is_rfkill(priv->shrd))
1337                 return;
1338
1339         timeout = cfg(priv)->base_params->wd_timeout;
1340         if (timeout == 0)
1341                 return;
1342
1343         /* monitor and check for stuck cmd queue */
1344         if (iwl_check_stuck_queue(priv, priv->shrd->cmd_queue))
1345                 return;
1346
1347         /* monitor and check for other stuck queues */
1348         if (iwl_is_any_associated(priv)) {
1349                 for (cnt = 0; cnt < hw_params(priv).max_txq_num; cnt++) {
1350                         /* skip as we already checked the command queue */
1351                         if (cnt == priv->shrd->cmd_queue)
1352                                 continue;
1353                         if (iwl_check_stuck_queue(priv, cnt))
1354                                 return;
1355                 }
1356         }
1357
1358         mod_timer(&priv->watchdog, jiffies +
1359                   msecs_to_jiffies(IWL_WD_TICK(timeout)));
1360 }
1361
1362 void iwl_setup_watchdog(struct iwl_priv *priv)
1363 {
1364         unsigned int timeout = cfg(priv)->base_params->wd_timeout;
1365
1366         if (!iwlagn_mod_params.wd_disable) {
1367                 /* use system default */
1368                 if (timeout && !cfg(priv)->base_params->wd_disable)
1369                         mod_timer(&priv->watchdog,
1370                                 jiffies +
1371                                 msecs_to_jiffies(IWL_WD_TICK(timeout)));
1372                 else
1373                         del_timer(&priv->watchdog);
1374         } else {
1375                 /* module parameter overwrite default configuration */
1376                 if (timeout && iwlagn_mod_params.wd_disable == 2)
1377                         mod_timer(&priv->watchdog,
1378                                 jiffies +
1379                                 msecs_to_jiffies(IWL_WD_TICK(timeout)));
1380                 else
1381                         del_timer(&priv->watchdog);
1382         }
1383 }
1384
1385 /**
1386  * iwl_beacon_time_mask_low - mask of lower 32 bit of beacon time
1387  * @priv -- pointer to iwl_priv data structure
1388  * @tsf_bits -- number of bits need to shift for masking)
1389  */
1390 static inline u32 iwl_beacon_time_mask_low(struct iwl_priv *priv,
1391                                            u16 tsf_bits)
1392 {
1393         return (1 << tsf_bits) - 1;
1394 }
1395
1396 /**
1397  * iwl_beacon_time_mask_high - mask of higher 32 bit of beacon time
1398  * @priv -- pointer to iwl_priv data structure
1399  * @tsf_bits -- number of bits need to shift for masking)
1400  */
1401 static inline u32 iwl_beacon_time_mask_high(struct iwl_priv *priv,
1402                                             u16 tsf_bits)
1403 {
1404         return ((1 << (32 - tsf_bits)) - 1) << tsf_bits;
1405 }
1406
1407 /*
1408  * extended beacon time format
1409  * time in usec will be changed into a 32-bit value in extended:internal format
1410  * the extended part is the beacon counts
1411  * the internal part is the time in usec within one beacon interval
1412  */
1413 u32 iwl_usecs_to_beacons(struct iwl_priv *priv, u32 usec, u32 beacon_interval)
1414 {
1415         u32 quot;
1416         u32 rem;
1417         u32 interval = beacon_interval * TIME_UNIT;
1418
1419         if (!interval || !usec)
1420                 return 0;
1421
1422         quot = (usec / interval) &
1423                 (iwl_beacon_time_mask_high(priv, IWLAGN_EXT_BEACON_TIME_POS) >>
1424                 IWLAGN_EXT_BEACON_TIME_POS);
1425         rem = (usec % interval) & iwl_beacon_time_mask_low(priv,
1426                                    IWLAGN_EXT_BEACON_TIME_POS);
1427
1428         return (quot << IWLAGN_EXT_BEACON_TIME_POS) + rem;
1429 }
1430
1431 /* base is usually what we get from ucode with each received frame,
1432  * the same as HW timer counter counting down
1433  */
1434 __le32 iwl_add_beacon_time(struct iwl_priv *priv, u32 base,
1435                            u32 addon, u32 beacon_interval)
1436 {
1437         u32 base_low = base & iwl_beacon_time_mask_low(priv,
1438                                 IWLAGN_EXT_BEACON_TIME_POS);
1439         u32 addon_low = addon & iwl_beacon_time_mask_low(priv,
1440                                 IWLAGN_EXT_BEACON_TIME_POS);
1441         u32 interval = beacon_interval * TIME_UNIT;
1442         u32 res = (base & iwl_beacon_time_mask_high(priv,
1443                                 IWLAGN_EXT_BEACON_TIME_POS)) +
1444                                 (addon & iwl_beacon_time_mask_high(priv,
1445                                 IWLAGN_EXT_BEACON_TIME_POS));
1446
1447         if (base_low > addon_low)
1448                 res += base_low - addon_low;
1449         else if (base_low < addon_low) {
1450                 res += interval + base_low - addon_low;
1451                 res += (1 << IWLAGN_EXT_BEACON_TIME_POS);
1452         } else
1453                 res += (1 << IWLAGN_EXT_BEACON_TIME_POS);
1454
1455         return cpu_to_le32(res);
1456 }
1457
1458 void iwl_set_hw_rfkill_state(struct iwl_priv *priv, bool state)
1459 {
1460         wiphy_rfkill_set_hw_state(priv->hw->wiphy, state);
1461 }
1462
1463 void iwl_nic_config(struct iwl_priv *priv)
1464 {
1465         cfg(priv)->lib->nic_config(priv);
1466 }
1467
1468 void iwl_free_skb(struct iwl_priv *priv, struct sk_buff *skb)
1469 {
1470         struct ieee80211_tx_info *info;
1471
1472         info = IEEE80211_SKB_CB(skb);
1473         kmem_cache_free(priv->tx_cmd_pool, (info->driver_data[1]));
1474         dev_kfree_skb_any(skb);
1475 }
1476
1477 void iwl_stop_sw_queue(struct iwl_priv *priv, u8 ac)
1478 {
1479         ieee80211_stop_queue(priv->hw, ac);
1480 }
1481
1482 void iwl_wake_sw_queue(struct iwl_priv *priv, u8 ac)
1483 {
1484         ieee80211_wake_queue(priv->hw, ac);
1485 }