56338693d38c35352bd1f4eb7250185bba3a5404
[firefly-linux-kernel-4.4.55.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-shared.h"
42 #include "iwl-agn.h"
43 #include "iwl-trans.h"
44
45 const u8 iwl_bcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
46
47 static bool iwl_is_channel_extension(struct iwl_priv *priv,
48                                      enum ieee80211_band band,
49                                      u16 channel, u8 extension_chan_offset)
50 {
51         const struct iwl_channel_info *ch_info;
52
53         ch_info = iwl_get_channel_info(priv, band, channel);
54         if (!is_channel_valid(ch_info))
55                 return false;
56
57         if (extension_chan_offset == IEEE80211_HT_PARAM_CHA_SEC_ABOVE)
58                 return !(ch_info->ht40_extension_channel &
59                                         IEEE80211_CHAN_NO_HT40PLUS);
60         else if (extension_chan_offset == IEEE80211_HT_PARAM_CHA_SEC_BELOW)
61                 return !(ch_info->ht40_extension_channel &
62                                         IEEE80211_CHAN_NO_HT40MINUS);
63
64         return false;
65 }
66
67 bool iwl_is_ht40_tx_allowed(struct iwl_priv *priv,
68                             struct iwl_rxon_context *ctx,
69                             struct ieee80211_sta_ht_cap *ht_cap)
70 {
71         if (!ctx->ht.enabled || !ctx->ht.is_40mhz)
72                 return false;
73
74         /*
75          * We do not check for IEEE80211_HT_CAP_SUP_WIDTH_20_40
76          * the bit will not set if it is pure 40MHz case
77          */
78         if (ht_cap && !ht_cap->ht_supported)
79                 return false;
80
81 #ifdef CONFIG_IWLWIFI_DEBUGFS
82         if (priv->disable_ht40)
83                 return false;
84 #endif
85
86         return iwl_is_channel_extension(priv, priv->band,
87                         le16_to_cpu(ctx->staging.channel),
88                         ctx->ht.extension_chan_offset);
89 }
90
91 /**
92  * iwl_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
93  * @priv: staging_rxon is compared to active_rxon
94  *
95  * If the RXON structure is changing enough to require a new tune,
96  * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
97  * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
98  */
99 int iwl_full_rxon_required(struct iwl_priv *priv,
100                            struct iwl_rxon_context *ctx)
101 {
102         const struct iwl_rxon_cmd *staging = &ctx->staging;
103         const struct iwl_rxon_cmd *active = &ctx->active;
104
105 #define CHK(cond)                                                       \
106         if ((cond)) {                                                   \
107                 IWL_DEBUG_INFO(priv, "need full RXON - " #cond "\n");   \
108                 return 1;                                               \
109         }
110
111 #define CHK_NEQ(c1, c2)                                         \
112         if ((c1) != (c2)) {                                     \
113                 IWL_DEBUG_INFO(priv, "need full RXON - "        \
114                                #c1 " != " #c2 " - %d != %d\n",  \
115                                (c1), (c2));                     \
116                 return 1;                                       \
117         }
118
119         /* These items are only settable from the full RXON command */
120         CHK(!iwl_is_associated_ctx(ctx));
121         CHK(compare_ether_addr(staging->bssid_addr, active->bssid_addr));
122         CHK(compare_ether_addr(staging->node_addr, active->node_addr));
123         CHK(compare_ether_addr(staging->wlap_bssid_addr,
124                                 active->wlap_bssid_addr));
125         CHK_NEQ(staging->dev_type, active->dev_type);
126         CHK_NEQ(staging->channel, active->channel);
127         CHK_NEQ(staging->air_propagation, active->air_propagation);
128         CHK_NEQ(staging->ofdm_ht_single_stream_basic_rates,
129                 active->ofdm_ht_single_stream_basic_rates);
130         CHK_NEQ(staging->ofdm_ht_dual_stream_basic_rates,
131                 active->ofdm_ht_dual_stream_basic_rates);
132         CHK_NEQ(staging->ofdm_ht_triple_stream_basic_rates,
133                 active->ofdm_ht_triple_stream_basic_rates);
134         CHK_NEQ(staging->assoc_id, active->assoc_id);
135
136         /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
137          * be updated with the RXON_ASSOC command -- however only some
138          * flag transitions are allowed using RXON_ASSOC */
139
140         /* Check if we are not switching bands */
141         CHK_NEQ(staging->flags & RXON_FLG_BAND_24G_MSK,
142                 active->flags & RXON_FLG_BAND_24G_MSK);
143
144         /* Check if we are switching association toggle */
145         CHK_NEQ(staging->filter_flags & RXON_FILTER_ASSOC_MSK,
146                 active->filter_flags & RXON_FILTER_ASSOC_MSK);
147
148 #undef CHK
149 #undef CHK_NEQ
150
151         return 0;
152 }
153
154 static void _iwl_set_rxon_ht(struct iwl_priv *priv,
155                              struct iwl_ht_config *ht_conf,
156                              struct iwl_rxon_context *ctx)
157 {
158         struct iwl_rxon_cmd *rxon = &ctx->staging;
159
160         if (!ctx->ht.enabled) {
161                 rxon->flags &= ~(RXON_FLG_CHANNEL_MODE_MSK |
162                         RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK |
163                         RXON_FLG_HT40_PROT_MSK |
164                         RXON_FLG_HT_PROT_MSK);
165                 return;
166         }
167
168         /* FIXME: if the definition of ht.protection changed, the "translation"
169          * will be needed for rxon->flags
170          */
171         rxon->flags |= cpu_to_le32(ctx->ht.protection << RXON_FLG_HT_OPERATING_MODE_POS);
172
173         /* Set up channel bandwidth:
174          * 20 MHz only, 20/40 mixed or pure 40 if ht40 ok */
175         /* clear the HT channel mode before set the mode */
176         rxon->flags &= ~(RXON_FLG_CHANNEL_MODE_MSK |
177                          RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK);
178         if (iwl_is_ht40_tx_allowed(priv, ctx, NULL)) {
179                 /* pure ht40 */
180                 if (ctx->ht.protection == IEEE80211_HT_OP_MODE_PROTECTION_20MHZ) {
181                         rxon->flags |= RXON_FLG_CHANNEL_MODE_PURE_40;
182                         /* Note: control channel is opposite of extension channel */
183                         switch (ctx->ht.extension_chan_offset) {
184                         case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
185                                 rxon->flags &= ~RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
186                                 break;
187                         case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
188                                 rxon->flags |= RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
189                                 break;
190                         }
191                 } else {
192                         /* Note: control channel is opposite of extension channel */
193                         switch (ctx->ht.extension_chan_offset) {
194                         case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
195                                 rxon->flags &= ~(RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK);
196                                 rxon->flags |= RXON_FLG_CHANNEL_MODE_MIXED;
197                                 break;
198                         case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
199                                 rxon->flags |= RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
200                                 rxon->flags |= RXON_FLG_CHANNEL_MODE_MIXED;
201                                 break;
202                         case IEEE80211_HT_PARAM_CHA_SEC_NONE:
203                         default:
204                                 /* channel location only valid if in Mixed mode */
205                                 IWL_ERR(priv, "invalid extension channel offset\n");
206                                 break;
207                         }
208                 }
209         } else {
210                 rxon->flags |= RXON_FLG_CHANNEL_MODE_LEGACY;
211         }
212
213         iwlagn_set_rxon_chain(priv, ctx);
214
215         IWL_DEBUG_ASSOC(priv, "rxon flags 0x%X operation mode :0x%X "
216                         "extension channel offset 0x%x\n",
217                         le32_to_cpu(rxon->flags), ctx->ht.protection,
218                         ctx->ht.extension_chan_offset);
219 }
220
221 void iwl_set_rxon_ht(struct iwl_priv *priv, struct iwl_ht_config *ht_conf)
222 {
223         struct iwl_rxon_context *ctx;
224
225         for_each_context(priv, ctx)
226                 _iwl_set_rxon_ht(priv, ht_conf, ctx);
227 }
228
229 /* Return valid, unused, channel for a passive scan to reset the RF */
230 u8 iwl_get_single_channel_number(struct iwl_priv *priv,
231                                  enum ieee80211_band band)
232 {
233         const struct iwl_channel_info *ch_info;
234         int i;
235         u8 channel = 0;
236         u8 min, max;
237         struct iwl_rxon_context *ctx;
238
239         if (band == IEEE80211_BAND_5GHZ) {
240                 min = 14;
241                 max = priv->channel_count;
242         } else {
243                 min = 0;
244                 max = 14;
245         }
246
247         for (i = min; i < max; i++) {
248                 bool busy = false;
249
250                 for_each_context(priv, ctx) {
251                         busy = priv->channel_info[i].channel ==
252                                 le16_to_cpu(ctx->staging.channel);
253                         if (busy)
254                                 break;
255                 }
256
257                 if (busy)
258                         continue;
259
260                 channel = priv->channel_info[i].channel;
261                 ch_info = iwl_get_channel_info(priv, band, channel);
262                 if (is_channel_valid(ch_info))
263                         break;
264         }
265
266         return channel;
267 }
268
269 /**
270  * iwl_set_rxon_channel - Set the band and channel values in staging RXON
271  * @ch: requested channel as a pointer to struct ieee80211_channel
272
273  * NOTE:  Does not commit to the hardware; it sets appropriate bit fields
274  * in the staging RXON flag structure based on the ch->band
275  */
276 void iwl_set_rxon_channel(struct iwl_priv *priv, struct ieee80211_channel *ch,
277                          struct iwl_rxon_context *ctx)
278 {
279         enum ieee80211_band band = ch->band;
280         u16 channel = ch->hw_value;
281
282         if ((le16_to_cpu(ctx->staging.channel) == channel) &&
283             (priv->band == band))
284                 return;
285
286         ctx->staging.channel = cpu_to_le16(channel);
287         if (band == IEEE80211_BAND_5GHZ)
288                 ctx->staging.flags &= ~RXON_FLG_BAND_24G_MSK;
289         else
290                 ctx->staging.flags |= RXON_FLG_BAND_24G_MSK;
291
292         priv->band = band;
293
294         IWL_DEBUG_INFO(priv, "Staging channel set to %d [%d]\n", channel, band);
295
296 }
297
298 void iwl_set_flags_for_band(struct iwl_priv *priv,
299                             struct iwl_rxon_context *ctx,
300                             enum ieee80211_band band,
301                             struct ieee80211_vif *vif)
302 {
303         if (band == IEEE80211_BAND_5GHZ) {
304                 ctx->staging.flags &=
305                     ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
306                       | RXON_FLG_CCK_MSK);
307                 ctx->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
308         } else {
309                 /* Copied from iwl_post_associate() */
310                 if (vif && vif->bss_conf.use_short_slot)
311                         ctx->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
312                 else
313                         ctx->staging.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
314
315                 ctx->staging.flags |= RXON_FLG_BAND_24G_MSK;
316                 ctx->staging.flags |= RXON_FLG_AUTO_DETECT_MSK;
317                 ctx->staging.flags &= ~RXON_FLG_CCK_MSK;
318         }
319 }
320
321 /*
322  * initialize rxon structure with default values from eeprom
323  */
324 void iwl_connection_init_rx_config(struct iwl_priv *priv,
325                                    struct iwl_rxon_context *ctx)
326 {
327         const struct iwl_channel_info *ch_info;
328
329         memset(&ctx->staging, 0, sizeof(ctx->staging));
330
331         if (!ctx->vif) {
332                 ctx->staging.dev_type = ctx->unused_devtype;
333         } else switch (ctx->vif->type) {
334         case NL80211_IFTYPE_AP:
335                 ctx->staging.dev_type = ctx->ap_devtype;
336                 break;
337
338         case NL80211_IFTYPE_STATION:
339                 ctx->staging.dev_type = ctx->station_devtype;
340                 ctx->staging.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
341                 break;
342
343         case NL80211_IFTYPE_ADHOC:
344                 ctx->staging.dev_type = ctx->ibss_devtype;
345                 ctx->staging.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
346                 ctx->staging.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
347                                                   RXON_FILTER_ACCEPT_GRP_MSK;
348                 break;
349
350         default:
351                 IWL_ERR(priv, "Unsupported interface type %d\n",
352                         ctx->vif->type);
353                 break;
354         }
355
356 #if 0
357         /* TODO:  Figure out when short_preamble would be set and cache from
358          * that */
359         if (!hw_to_local(priv->hw)->short_preamble)
360                 ctx->staging.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
361         else
362                 ctx->staging.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
363 #endif
364
365         ch_info = iwl_get_channel_info(priv, priv->band,
366                                        le16_to_cpu(ctx->active.channel));
367
368         if (!ch_info)
369                 ch_info = &priv->channel_info[0];
370
371         ctx->staging.channel = cpu_to_le16(ch_info->channel);
372         priv->band = ch_info->band;
373
374         iwl_set_flags_for_band(priv, ctx, priv->band, ctx->vif);
375
376         ctx->staging.ofdm_basic_rates =
377             (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
378         ctx->staging.cck_basic_rates =
379             (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
380
381         /* clear both MIX and PURE40 mode flag */
382         ctx->staging.flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED |
383                                         RXON_FLG_CHANNEL_MODE_PURE_40);
384         if (ctx->vif)
385                 memcpy(ctx->staging.node_addr, ctx->vif->addr, ETH_ALEN);
386
387         ctx->staging.ofdm_ht_single_stream_basic_rates = 0xff;
388         ctx->staging.ofdm_ht_dual_stream_basic_rates = 0xff;
389         ctx->staging.ofdm_ht_triple_stream_basic_rates = 0xff;
390 }
391
392 void iwl_set_rate(struct iwl_priv *priv)
393 {
394         const struct ieee80211_supported_band *hw = NULL;
395         struct ieee80211_rate *rate;
396         struct iwl_rxon_context *ctx;
397         int i;
398
399         hw = iwl_get_hw_mode(priv, priv->band);
400         if (!hw) {
401                 IWL_ERR(priv, "Failed to set rate: unable to get hw mode\n");
402                 return;
403         }
404
405         priv->active_rate = 0;
406
407         for (i = 0; i < hw->n_bitrates; i++) {
408                 rate = &(hw->bitrates[i]);
409                 if (rate->hw_value < IWL_RATE_COUNT_LEGACY)
410                         priv->active_rate |= (1 << rate->hw_value);
411         }
412
413         IWL_DEBUG_RATE(priv, "Set active_rate = %0x\n", priv->active_rate);
414
415         for_each_context(priv, ctx) {
416                 ctx->staging.cck_basic_rates =
417                     (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
418
419                 ctx->staging.ofdm_basic_rates =
420                    (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
421         }
422 }
423
424 void iwl_chswitch_done(struct iwl_priv *priv, bool is_success)
425 {
426         /*
427          * MULTI-FIXME
428          * See iwlagn_mac_channel_switch.
429          */
430         struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
431
432         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
433                 return;
434
435         if (test_and_clear_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->status))
436                 ieee80211_chswitch_done(ctx->vif, is_success);
437 }
438
439 #ifdef CONFIG_IWLWIFI_DEBUG
440 void iwl_print_rx_config_cmd(struct iwl_priv *priv,
441                              enum iwl_rxon_context_id ctxid)
442 {
443         struct iwl_rxon_context *ctx = &priv->contexts[ctxid];
444         struct iwl_rxon_cmd *rxon = &ctx->staging;
445
446         IWL_DEBUG_RADIO(priv, "RX CONFIG:\n");
447         iwl_print_hex_dump(priv, IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
448         IWL_DEBUG_RADIO(priv, "u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
449         IWL_DEBUG_RADIO(priv, "u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
450         IWL_DEBUG_RADIO(priv, "u32 filter_flags: 0x%08x\n",
451                         le32_to_cpu(rxon->filter_flags));
452         IWL_DEBUG_RADIO(priv, "u8 dev_type: 0x%x\n", rxon->dev_type);
453         IWL_DEBUG_RADIO(priv, "u8 ofdm_basic_rates: 0x%02x\n",
454                         rxon->ofdm_basic_rates);
455         IWL_DEBUG_RADIO(priv, "u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
456         IWL_DEBUG_RADIO(priv, "u8[6] node_addr: %pM\n", rxon->node_addr);
457         IWL_DEBUG_RADIO(priv, "u8[6] bssid_addr: %pM\n", rxon->bssid_addr);
458         IWL_DEBUG_RADIO(priv, "u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
459 }
460 #endif
461
462 void iwlagn_fw_error(struct iwl_priv *priv, bool ondemand)
463 {
464         unsigned int reload_msec;
465         unsigned long reload_jiffies;
466
467 #ifdef CONFIG_IWLWIFI_DEBUG
468         if (iwl_have_debug_level(IWL_DL_FW_ERRORS))
469                 iwl_print_rx_config_cmd(priv, IWL_RXON_CTX_BSS);
470 #endif
471
472         /* uCode is no longer loaded. */
473         priv->ucode_loaded = false;
474
475         /* Set the FW error flag -- cleared on iwl_down */
476         set_bit(STATUS_FW_ERROR, &priv->shrd->status);
477
478         /* Cancel currently queued command. */
479         clear_bit(STATUS_HCMD_ACTIVE, &priv->shrd->status);
480
481         iwl_abort_notification_waits(&priv->notif_wait);
482
483         /* Keep the restart process from trying to send host
484          * commands by clearing the ready bit */
485         clear_bit(STATUS_READY, &priv->status);
486
487         wake_up(&trans(priv)->wait_command_queue);
488
489         if (!ondemand) {
490                 /*
491                  * If firmware keep reloading, then it indicate something
492                  * serious wrong and firmware having problem to recover
493                  * from it. Instead of keep trying which will fill the syslog
494                  * and hang the system, let's just stop it
495                  */
496                 reload_jiffies = jiffies;
497                 reload_msec = jiffies_to_msecs((long) reload_jiffies -
498                                         (long) priv->reload_jiffies);
499                 priv->reload_jiffies = reload_jiffies;
500                 if (reload_msec <= IWL_MIN_RELOAD_DURATION) {
501                         priv->reload_count++;
502                         if (priv->reload_count >= IWL_MAX_CONTINUE_RELOAD_CNT) {
503                                 IWL_ERR(priv, "BUG_ON, Stop restarting\n");
504                                 return;
505                         }
506                 } else
507                         priv->reload_count = 0;
508         }
509
510         if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
511                 if (iwlagn_mod_params.restart_fw) {
512                         IWL_DEBUG_FW_ERRORS(priv,
513                                   "Restarting adapter due to uCode error.\n");
514                         queue_work(priv->workqueue, &priv->restart);
515                 } else
516                         IWL_DEBUG_FW_ERRORS(priv,
517                                   "Detected FW error, but not restarting\n");
518         }
519 }
520
521 int iwl_set_tx_power(struct iwl_priv *priv, s8 tx_power, bool force)
522 {
523         int ret;
524         s8 prev_tx_power;
525         bool defer;
526         struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
527
528         lockdep_assert_held(&priv->mutex);
529
530         if (priv->tx_power_user_lmt == tx_power && !force)
531                 return 0;
532
533         if (tx_power < IWLAGN_TX_POWER_TARGET_POWER_MIN) {
534                 IWL_WARN(priv,
535                          "Requested user TXPOWER %d below lower limit %d.\n",
536                          tx_power,
537                          IWLAGN_TX_POWER_TARGET_POWER_MIN);
538                 return -EINVAL;
539         }
540
541         if (tx_power > priv->tx_power_device_lmt) {
542                 IWL_WARN(priv,
543                         "Requested user TXPOWER %d above upper limit %d.\n",
544                          tx_power, priv->tx_power_device_lmt);
545                 return -EINVAL;
546         }
547
548         if (!iwl_is_ready_rf(priv))
549                 return -EIO;
550
551         /* scan complete and commit_rxon use tx_power_next value,
552          * it always need to be updated for newest request */
553         priv->tx_power_next = tx_power;
554
555         /* do not set tx power when scanning or channel changing */
556         defer = test_bit(STATUS_SCANNING, &priv->status) ||
557                 memcmp(&ctx->active, &ctx->staging, sizeof(ctx->staging));
558         if (defer && !force) {
559                 IWL_DEBUG_INFO(priv, "Deferring tx power set\n");
560                 return 0;
561         }
562
563         prev_tx_power = priv->tx_power_user_lmt;
564         priv->tx_power_user_lmt = tx_power;
565
566         ret = iwlagn_send_tx_power(priv);
567
568         /* if fail to set tx_power, restore the orig. tx power */
569         if (ret) {
570                 priv->tx_power_user_lmt = prev_tx_power;
571                 priv->tx_power_next = prev_tx_power;
572         }
573         return ret;
574 }
575
576 void iwl_send_bt_config(struct iwl_priv *priv)
577 {
578         struct iwl_bt_cmd bt_cmd = {
579                 .lead_time = BT_LEAD_TIME_DEF,
580                 .max_kill = BT_MAX_KILL_DEF,
581                 .kill_ack_mask = 0,
582                 .kill_cts_mask = 0,
583         };
584
585         if (!iwlagn_mod_params.bt_coex_active)
586                 bt_cmd.flags = BT_COEX_DISABLE;
587         else
588                 bt_cmd.flags = BT_COEX_ENABLE;
589
590         priv->bt_enable_flag = bt_cmd.flags;
591         IWL_DEBUG_INFO(priv, "BT coex %s\n",
592                 (bt_cmd.flags == BT_COEX_DISABLE) ? "disable" : "active");
593
594         if (iwl_dvm_send_cmd_pdu(priv, REPLY_BT_CONFIG,
595                              CMD_SYNC, sizeof(struct iwl_bt_cmd), &bt_cmd))
596                 IWL_ERR(priv, "failed to send BT Coex Config\n");
597 }
598
599 int iwl_send_statistics_request(struct iwl_priv *priv, u8 flags, bool clear)
600 {
601         struct iwl_statistics_cmd statistics_cmd = {
602                 .configuration_flags =
603                         clear ? IWL_STATS_CONF_CLEAR_STATS : 0,
604         };
605
606         if (flags & CMD_ASYNC)
607                 return iwl_dvm_send_cmd_pdu(priv, REPLY_STATISTICS_CMD,
608                                               CMD_ASYNC,
609                                                sizeof(struct iwl_statistics_cmd),
610                                                &statistics_cmd);
611         else
612                 return iwl_dvm_send_cmd_pdu(priv, REPLY_STATISTICS_CMD,
613                                         CMD_SYNC,
614                                         sizeof(struct iwl_statistics_cmd),
615                                         &statistics_cmd);
616 }
617
618
619
620
621 #ifdef CONFIG_IWLWIFI_DEBUGFS
622
623 #define IWL_TRAFFIC_DUMP_SIZE   (IWL_TRAFFIC_ENTRY_SIZE * IWL_TRAFFIC_ENTRIES)
624
625 void iwl_reset_traffic_log(struct iwl_priv *priv)
626 {
627         priv->tx_traffic_idx = 0;
628         priv->rx_traffic_idx = 0;
629         if (priv->tx_traffic)
630                 memset(priv->tx_traffic, 0, IWL_TRAFFIC_DUMP_SIZE);
631         if (priv->rx_traffic)
632                 memset(priv->rx_traffic, 0, IWL_TRAFFIC_DUMP_SIZE);
633 }
634
635 int iwl_alloc_traffic_mem(struct iwl_priv *priv)
636 {
637         u32 traffic_size = IWL_TRAFFIC_DUMP_SIZE;
638
639         if (iwl_have_debug_level(IWL_DL_TX)) {
640                 if (!priv->tx_traffic) {
641                         priv->tx_traffic =
642                                 kzalloc(traffic_size, GFP_KERNEL);
643                         if (!priv->tx_traffic)
644                                 return -ENOMEM;
645                 }
646         }
647         if (iwl_have_debug_level(IWL_DL_RX)) {
648                 if (!priv->rx_traffic) {
649                         priv->rx_traffic =
650                                 kzalloc(traffic_size, GFP_KERNEL);
651                         if (!priv->rx_traffic)
652                                 return -ENOMEM;
653                 }
654         }
655         iwl_reset_traffic_log(priv);
656         return 0;
657 }
658
659 void iwl_free_traffic_mem(struct iwl_priv *priv)
660 {
661         kfree(priv->tx_traffic);
662         priv->tx_traffic = NULL;
663
664         kfree(priv->rx_traffic);
665         priv->rx_traffic = NULL;
666 }
667
668 void iwl_dbg_log_tx_data_frame(struct iwl_priv *priv,
669                       u16 length, struct ieee80211_hdr *header)
670 {
671         __le16 fc;
672         u16 len;
673
674         if (likely(!iwl_have_debug_level(IWL_DL_TX)))
675                 return;
676
677         if (!priv->tx_traffic)
678                 return;
679
680         fc = header->frame_control;
681         if (ieee80211_is_data(fc)) {
682                 len = (length > IWL_TRAFFIC_ENTRY_SIZE)
683                        ? IWL_TRAFFIC_ENTRY_SIZE : length;
684                 memcpy((priv->tx_traffic +
685                        (priv->tx_traffic_idx * IWL_TRAFFIC_ENTRY_SIZE)),
686                        header, len);
687                 priv->tx_traffic_idx =
688                         (priv->tx_traffic_idx + 1) % IWL_TRAFFIC_ENTRIES;
689         }
690 }
691
692 void iwl_dbg_log_rx_data_frame(struct iwl_priv *priv,
693                       u16 length, struct ieee80211_hdr *header)
694 {
695         __le16 fc;
696         u16 len;
697
698         if (likely(!iwl_have_debug_level(IWL_DL_RX)))
699                 return;
700
701         if (!priv->rx_traffic)
702                 return;
703
704         fc = header->frame_control;
705         if (ieee80211_is_data(fc)) {
706                 len = (length > IWL_TRAFFIC_ENTRY_SIZE)
707                        ? IWL_TRAFFIC_ENTRY_SIZE : length;
708                 memcpy((priv->rx_traffic +
709                        (priv->rx_traffic_idx * IWL_TRAFFIC_ENTRY_SIZE)),
710                        header, len);
711                 priv->rx_traffic_idx =
712                         (priv->rx_traffic_idx + 1) % IWL_TRAFFIC_ENTRIES;
713         }
714 }
715
716 const char *get_mgmt_string(int cmd)
717 {
718         switch (cmd) {
719                 IWL_CMD(MANAGEMENT_ASSOC_REQ);
720                 IWL_CMD(MANAGEMENT_ASSOC_RESP);
721                 IWL_CMD(MANAGEMENT_REASSOC_REQ);
722                 IWL_CMD(MANAGEMENT_REASSOC_RESP);
723                 IWL_CMD(MANAGEMENT_PROBE_REQ);
724                 IWL_CMD(MANAGEMENT_PROBE_RESP);
725                 IWL_CMD(MANAGEMENT_BEACON);
726                 IWL_CMD(MANAGEMENT_ATIM);
727                 IWL_CMD(MANAGEMENT_DISASSOC);
728                 IWL_CMD(MANAGEMENT_AUTH);
729                 IWL_CMD(MANAGEMENT_DEAUTH);
730                 IWL_CMD(MANAGEMENT_ACTION);
731         default:
732                 return "UNKNOWN";
733
734         }
735 }
736
737 const char *get_ctrl_string(int cmd)
738 {
739         switch (cmd) {
740                 IWL_CMD(CONTROL_BACK_REQ);
741                 IWL_CMD(CONTROL_BACK);
742                 IWL_CMD(CONTROL_PSPOLL);
743                 IWL_CMD(CONTROL_RTS);
744                 IWL_CMD(CONTROL_CTS);
745                 IWL_CMD(CONTROL_ACK);
746                 IWL_CMD(CONTROL_CFEND);
747                 IWL_CMD(CONTROL_CFENDACK);
748         default:
749                 return "UNKNOWN";
750
751         }
752 }
753
754 void iwl_clear_traffic_stats(struct iwl_priv *priv)
755 {
756         memset(&priv->tx_stats, 0, sizeof(struct traffic_stats));
757         memset(&priv->rx_stats, 0, sizeof(struct traffic_stats));
758 }
759
760 /*
761  * if CONFIG_IWLWIFI_DEBUGFS defined, iwl_update_stats function will
762  * record all the MGMT, CTRL and DATA pkt for both TX and Rx pass.
763  * Use debugFs to display the rx/rx_statistics
764  * if CONFIG_IWLWIFI_DEBUGFS not being defined, then no MGMT and CTRL
765  * information will be recorded, but DATA pkt still will be recorded
766  * for the reason of iwl_led.c need to control the led blinking based on
767  * number of tx and rx data.
768  *
769  */
770 void iwl_update_stats(struct iwl_priv *priv, bool is_tx, __le16 fc, u16 len)
771 {
772         struct traffic_stats    *stats;
773
774         if (is_tx)
775                 stats = &priv->tx_stats;
776         else
777                 stats = &priv->rx_stats;
778
779         if (ieee80211_is_mgmt(fc)) {
780                 switch (fc & cpu_to_le16(IEEE80211_FCTL_STYPE)) {
781                 case cpu_to_le16(IEEE80211_STYPE_ASSOC_REQ):
782                         stats->mgmt[MANAGEMENT_ASSOC_REQ]++;
783                         break;
784                 case cpu_to_le16(IEEE80211_STYPE_ASSOC_RESP):
785                         stats->mgmt[MANAGEMENT_ASSOC_RESP]++;
786                         break;
787                 case cpu_to_le16(IEEE80211_STYPE_REASSOC_REQ):
788                         stats->mgmt[MANAGEMENT_REASSOC_REQ]++;
789                         break;
790                 case cpu_to_le16(IEEE80211_STYPE_REASSOC_RESP):
791                         stats->mgmt[MANAGEMENT_REASSOC_RESP]++;
792                         break;
793                 case cpu_to_le16(IEEE80211_STYPE_PROBE_REQ):
794                         stats->mgmt[MANAGEMENT_PROBE_REQ]++;
795                         break;
796                 case cpu_to_le16(IEEE80211_STYPE_PROBE_RESP):
797                         stats->mgmt[MANAGEMENT_PROBE_RESP]++;
798                         break;
799                 case cpu_to_le16(IEEE80211_STYPE_BEACON):
800                         stats->mgmt[MANAGEMENT_BEACON]++;
801                         break;
802                 case cpu_to_le16(IEEE80211_STYPE_ATIM):
803                         stats->mgmt[MANAGEMENT_ATIM]++;
804                         break;
805                 case cpu_to_le16(IEEE80211_STYPE_DISASSOC):
806                         stats->mgmt[MANAGEMENT_DISASSOC]++;
807                         break;
808                 case cpu_to_le16(IEEE80211_STYPE_AUTH):
809                         stats->mgmt[MANAGEMENT_AUTH]++;
810                         break;
811                 case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
812                         stats->mgmt[MANAGEMENT_DEAUTH]++;
813                         break;
814                 case cpu_to_le16(IEEE80211_STYPE_ACTION):
815                         stats->mgmt[MANAGEMENT_ACTION]++;
816                         break;
817                 }
818         } else if (ieee80211_is_ctl(fc)) {
819                 switch (fc & cpu_to_le16(IEEE80211_FCTL_STYPE)) {
820                 case cpu_to_le16(IEEE80211_STYPE_BACK_REQ):
821                         stats->ctrl[CONTROL_BACK_REQ]++;
822                         break;
823                 case cpu_to_le16(IEEE80211_STYPE_BACK):
824                         stats->ctrl[CONTROL_BACK]++;
825                         break;
826                 case cpu_to_le16(IEEE80211_STYPE_PSPOLL):
827                         stats->ctrl[CONTROL_PSPOLL]++;
828                         break;
829                 case cpu_to_le16(IEEE80211_STYPE_RTS):
830                         stats->ctrl[CONTROL_RTS]++;
831                         break;
832                 case cpu_to_le16(IEEE80211_STYPE_CTS):
833                         stats->ctrl[CONTROL_CTS]++;
834                         break;
835                 case cpu_to_le16(IEEE80211_STYPE_ACK):
836                         stats->ctrl[CONTROL_ACK]++;
837                         break;
838                 case cpu_to_le16(IEEE80211_STYPE_CFEND):
839                         stats->ctrl[CONTROL_CFEND]++;
840                         break;
841                 case cpu_to_le16(IEEE80211_STYPE_CFENDACK):
842                         stats->ctrl[CONTROL_CFENDACK]++;
843                         break;
844                 }
845         } else {
846                 /* data */
847                 stats->data_cnt++;
848                 stats->data_bytes += len;
849         }
850 }
851 #endif
852
853 static void iwl_force_rf_reset(struct iwl_priv *priv)
854 {
855         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
856                 return;
857
858         if (!iwl_is_any_associated(priv)) {
859                 IWL_DEBUG_SCAN(priv, "force reset rejected: not associated\n");
860                 return;
861         }
862         /*
863          * There is no easy and better way to force reset the radio,
864          * the only known method is switching channel which will force to
865          * reset and tune the radio.
866          * Use internal short scan (single channel) operation to should
867          * achieve this objective.
868          * Driver should reset the radio when number of consecutive missed
869          * beacon, or any other uCode error condition detected.
870          */
871         IWL_DEBUG_INFO(priv, "perform radio reset.\n");
872         iwl_internal_short_hw_scan(priv);
873 }
874
875
876 int iwl_force_reset(struct iwl_priv *priv, int mode, bool external)
877 {
878         struct iwl_force_reset *force_reset;
879
880         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
881                 return -EINVAL;
882
883         if (mode >= IWL_MAX_FORCE_RESET) {
884                 IWL_DEBUG_INFO(priv, "invalid reset request.\n");
885                 return -EINVAL;
886         }
887         force_reset = &priv->force_reset[mode];
888         force_reset->reset_request_count++;
889         if (!external) {
890                 if (force_reset->last_force_reset_jiffies &&
891                     time_after(force_reset->last_force_reset_jiffies +
892                     force_reset->reset_duration, jiffies)) {
893                         IWL_DEBUG_INFO(priv, "force reset rejected\n");
894                         force_reset->reset_reject_count++;
895                         return -EAGAIN;
896                 }
897         }
898         force_reset->reset_success_count++;
899         force_reset->last_force_reset_jiffies = jiffies;
900         IWL_DEBUG_INFO(priv, "perform force reset (%d)\n", mode);
901         switch (mode) {
902         case IWL_RF_RESET:
903                 iwl_force_rf_reset(priv);
904                 break;
905         case IWL_FW_RESET:
906                 /*
907                  * if the request is from external(ex: debugfs),
908                  * then always perform the request in regardless the module
909                  * parameter setting
910                  * if the request is from internal (uCode error or driver
911                  * detect failure), then fw_restart module parameter
912                  * need to be check before performing firmware reload
913                  */
914                 if (!external && !iwlagn_mod_params.restart_fw) {
915                         IWL_DEBUG_INFO(priv, "Cancel firmware reload based on "
916                                        "module parameter setting\n");
917                         break;
918                 }
919                 IWL_ERR(priv, "On demand firmware reload\n");
920                 iwlagn_fw_error(priv, true);
921                 break;
922         }
923         return 0;
924 }
925
926
927 int iwl_cmd_echo_test(struct iwl_priv *priv)
928 {
929         int ret;
930         struct iwl_host_cmd cmd = {
931                 .id = REPLY_ECHO,
932                 .len = { 0 },
933                 .flags = CMD_SYNC,
934         };
935
936         ret = iwl_dvm_send_cmd(priv, &cmd);
937         if (ret)
938                 IWL_ERR(priv, "echo testing fail: 0X%x\n", ret);
939         else
940                 IWL_DEBUG_INFO(priv, "echo testing pass\n");
941         return ret;
942 }
943
944 static inline int iwl_check_stuck_queue(struct iwl_priv *priv, int txq)
945 {
946         if (iwl_trans_check_stuck_queue(trans(priv), txq)) {
947                 int ret;
948                 ret = iwl_force_reset(priv, IWL_FW_RESET, false);
949                 return (ret == -EAGAIN) ? 0 : 1;
950         }
951         return 0;
952 }
953
954 /*
955  * Making watchdog tick be a quarter of timeout assure we will
956  * discover the queue hung between timeout and 1.25*timeout
957  */
958 #define IWL_WD_TICK(timeout) ((timeout) / 4)
959
960 /*
961  * Watchdog timer callback, we check each tx queue for stuck, if if hung
962  * we reset the firmware. If everything is fine just rearm the timer.
963  */
964 void iwl_bg_watchdog(unsigned long data)
965 {
966         struct iwl_priv *priv = (struct iwl_priv *)data;
967         int cnt;
968         unsigned long timeout;
969
970         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
971                 return;
972
973         if (iwl_is_rfkill(priv))
974                 return;
975
976         timeout = hw_params(priv).wd_timeout;
977         if (timeout == 0)
978                 return;
979
980         /* monitor and check for stuck queues */
981         for (cnt = 0; cnt < cfg(priv)->base_params->num_of_queues; cnt++)
982                 if (iwl_check_stuck_queue(priv, cnt))
983                         return;
984
985         mod_timer(&priv->watchdog, jiffies +
986                   msecs_to_jiffies(IWL_WD_TICK(timeout)));
987 }
988
989 void iwl_setup_watchdog(struct iwl_priv *priv)
990 {
991         unsigned int timeout = hw_params(priv).wd_timeout;
992
993         if (!iwlagn_mod_params.wd_disable) {
994                 /* use system default */
995                 if (timeout && !cfg(priv)->base_params->wd_disable)
996                         mod_timer(&priv->watchdog,
997                                 jiffies +
998                                 msecs_to_jiffies(IWL_WD_TICK(timeout)));
999                 else
1000                         del_timer(&priv->watchdog);
1001         } else {
1002                 /* module parameter overwrite default configuration */
1003                 if (timeout && iwlagn_mod_params.wd_disable == 2)
1004                         mod_timer(&priv->watchdog,
1005                                 jiffies +
1006                                 msecs_to_jiffies(IWL_WD_TICK(timeout)));
1007                 else
1008                         del_timer(&priv->watchdog);
1009         }
1010 }
1011
1012 /**
1013  * iwl_beacon_time_mask_low - mask of lower 32 bit of beacon time
1014  * @priv -- pointer to iwl_priv data structure
1015  * @tsf_bits -- number of bits need to shift for masking)
1016  */
1017 static inline u32 iwl_beacon_time_mask_low(struct iwl_priv *priv,
1018                                            u16 tsf_bits)
1019 {
1020         return (1 << tsf_bits) - 1;
1021 }
1022
1023 /**
1024  * iwl_beacon_time_mask_high - mask of higher 32 bit of beacon time
1025  * @priv -- pointer to iwl_priv data structure
1026  * @tsf_bits -- number of bits need to shift for masking)
1027  */
1028 static inline u32 iwl_beacon_time_mask_high(struct iwl_priv *priv,
1029                                             u16 tsf_bits)
1030 {
1031         return ((1 << (32 - tsf_bits)) - 1) << tsf_bits;
1032 }
1033
1034 /*
1035  * extended beacon time format
1036  * time in usec will be changed into a 32-bit value in extended:internal format
1037  * the extended part is the beacon counts
1038  * the internal part is the time in usec within one beacon interval
1039  */
1040 u32 iwl_usecs_to_beacons(struct iwl_priv *priv, u32 usec, u32 beacon_interval)
1041 {
1042         u32 quot;
1043         u32 rem;
1044         u32 interval = beacon_interval * TIME_UNIT;
1045
1046         if (!interval || !usec)
1047                 return 0;
1048
1049         quot = (usec / interval) &
1050                 (iwl_beacon_time_mask_high(priv, IWLAGN_EXT_BEACON_TIME_POS) >>
1051                 IWLAGN_EXT_BEACON_TIME_POS);
1052         rem = (usec % interval) & iwl_beacon_time_mask_low(priv,
1053                                    IWLAGN_EXT_BEACON_TIME_POS);
1054
1055         return (quot << IWLAGN_EXT_BEACON_TIME_POS) + rem;
1056 }
1057
1058 /* base is usually what we get from ucode with each received frame,
1059  * the same as HW timer counter counting down
1060  */
1061 __le32 iwl_add_beacon_time(struct iwl_priv *priv, u32 base,
1062                            u32 addon, u32 beacon_interval)
1063 {
1064         u32 base_low = base & iwl_beacon_time_mask_low(priv,
1065                                 IWLAGN_EXT_BEACON_TIME_POS);
1066         u32 addon_low = addon & iwl_beacon_time_mask_low(priv,
1067                                 IWLAGN_EXT_BEACON_TIME_POS);
1068         u32 interval = beacon_interval * TIME_UNIT;
1069         u32 res = (base & iwl_beacon_time_mask_high(priv,
1070                                 IWLAGN_EXT_BEACON_TIME_POS)) +
1071                                 (addon & iwl_beacon_time_mask_high(priv,
1072                                 IWLAGN_EXT_BEACON_TIME_POS));
1073
1074         if (base_low > addon_low)
1075                 res += base_low - addon_low;
1076         else if (base_low < addon_low) {
1077                 res += interval + base_low - addon_low;
1078                 res += (1 << IWLAGN_EXT_BEACON_TIME_POS);
1079         } else
1080                 res += (1 << IWLAGN_EXT_BEACON_TIME_POS);
1081
1082         return cpu_to_le32(res);
1083 }
1084
1085 void iwl_set_hw_rfkill_state(struct iwl_op_mode *op_mode, bool state)
1086 {
1087         struct iwl_priv *priv = IWL_OP_MODE_GET_DVM(op_mode);
1088
1089         if (state)
1090                 set_bit(STATUS_RF_KILL_HW, &priv->status);
1091         else
1092                 clear_bit(STATUS_RF_KILL_HW, &priv->status);
1093
1094         wiphy_rfkill_set_hw_state(priv->hw->wiphy, state);
1095 }
1096
1097 void iwl_free_skb(struct iwl_op_mode *op_mode, struct sk_buff *skb)
1098 {
1099         struct ieee80211_tx_info *info;
1100
1101         info = IEEE80211_SKB_CB(skb);
1102         kmem_cache_free(iwl_tx_cmd_pool, (info->driver_data[1]));
1103         dev_kfree_skb_any(skb);
1104 }