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