iwlwifi: correct frequency settings
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / net / wireless / iwlwifi / iwl-core.c
blob4ad89389a0a974f5cfb50bd77ce51c3dd10bdb24
1 /******************************************************************************
3 * GPL LICENSE SUMMARY
5 * Copyright(c) 2008 - 2010 Intel Corporation. All rights reserved.
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.
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.
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
21 * The full GNU General Public License is included in this distribution
22 * in the file called LICENSE.GPL.
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 *****************************************************************************/
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>
36 #include "iwl-eeprom.h"
37 #include "iwl-dev.h" /* FIXME: remove */
38 #include "iwl-debug.h"
39 #include "iwl-core.h"
40 #include "iwl-io.h"
41 #include "iwl-power.h"
42 #include "iwl-sta.h"
43 #include "iwl-helpers.h"
46 MODULE_DESCRIPTION("iwl core");
47 MODULE_VERSION(IWLWIFI_VERSION);
48 MODULE_AUTHOR(DRV_COPYRIGHT " " DRV_AUTHOR);
49 MODULE_LICENSE("GPL");
52 * set bt_coex_active to true, uCode will do kill/defer
53 * every time the priority line is asserted (BT is sending signals on the
54 * priority line in the PCIx).
55 * set bt_coex_active to false, uCode will ignore the BT activity and
56 * perform the normal operation
58 * User might experience transmit issue on some platform due to WiFi/BT
59 * co-exist problem. The possible behaviors are:
60 * Able to scan and finding all the available AP
61 * Not able to associate with any AP
62 * On those platforms, WiFi communication can be restored by set
63 * "bt_coex_active" module parameter to "false"
65 * default: bt_coex_active = true (BT_COEX_ENABLE)
67 bool bt_coex_active = true;
68 EXPORT_SYMBOL_GPL(bt_coex_active);
69 module_param(bt_coex_active, bool, S_IRUGO);
70 MODULE_PARM_DESC(bt_coex_active, "enable wifi/bluetooth co-exist");
72 u32 iwl_debug_level;
73 EXPORT_SYMBOL(iwl_debug_level);
75 const u8 iwl_bcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
76 EXPORT_SYMBOL(iwl_bcast_addr);
79 /* This function both allocates and initializes hw and priv. */
80 struct ieee80211_hw *iwl_alloc_all(struct iwl_cfg *cfg)
82 struct iwl_priv *priv;
83 /* mac80211 allocates memory for this device instance, including
84 * space for this driver's private structure */
85 struct ieee80211_hw *hw;
87 hw = ieee80211_alloc_hw(sizeof(struct iwl_priv),
88 cfg->ops->ieee80211_ops);
89 if (hw == NULL) {
90 pr_err("%s: Can not allocate network device\n",
91 cfg->name);
92 goto out;
95 priv = hw->priv;
96 priv->hw = hw;
98 out:
99 return hw;
101 EXPORT_SYMBOL(iwl_alloc_all);
103 #define MAX_BIT_RATE_40_MHZ 150 /* Mbps */
104 #define MAX_BIT_RATE_20_MHZ 72 /* Mbps */
105 static void iwlcore_init_ht_hw_capab(const struct iwl_priv *priv,
106 struct ieee80211_sta_ht_cap *ht_info,
107 enum ieee80211_band band)
109 u16 max_bit_rate = 0;
110 u8 rx_chains_num = priv->hw_params.rx_chains_num;
111 u8 tx_chains_num = priv->hw_params.tx_chains_num;
113 ht_info->cap = 0;
114 memset(&ht_info->mcs, 0, sizeof(ht_info->mcs));
116 ht_info->ht_supported = true;
118 if (priv->cfg->ht_params &&
119 priv->cfg->ht_params->ht_greenfield_support)
120 ht_info->cap |= IEEE80211_HT_CAP_GRN_FLD;
121 ht_info->cap |= IEEE80211_HT_CAP_SGI_20;
122 max_bit_rate = MAX_BIT_RATE_20_MHZ;
123 if (priv->hw_params.ht40_channel & BIT(band)) {
124 ht_info->cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
125 ht_info->cap |= IEEE80211_HT_CAP_SGI_40;
126 ht_info->mcs.rx_mask[4] = 0x01;
127 max_bit_rate = MAX_BIT_RATE_40_MHZ;
130 if (priv->cfg->mod_params->amsdu_size_8K)
131 ht_info->cap |= IEEE80211_HT_CAP_MAX_AMSDU;
133 ht_info->ampdu_factor = CFG_HT_RX_AMPDU_FACTOR_DEF;
134 if (priv->cfg->bt_params && priv->cfg->bt_params->ampdu_factor)
135 ht_info->ampdu_factor = priv->cfg->bt_params->ampdu_factor;
136 ht_info->ampdu_density = CFG_HT_MPDU_DENSITY_DEF;
137 if (priv->cfg->bt_params && priv->cfg->bt_params->ampdu_density)
138 ht_info->ampdu_density = priv->cfg->bt_params->ampdu_density;
140 ht_info->mcs.rx_mask[0] = 0xFF;
141 if (rx_chains_num >= 2)
142 ht_info->mcs.rx_mask[1] = 0xFF;
143 if (rx_chains_num >= 3)
144 ht_info->mcs.rx_mask[2] = 0xFF;
146 /* Highest supported Rx data rate */
147 max_bit_rate *= rx_chains_num;
148 WARN_ON(max_bit_rate & ~IEEE80211_HT_MCS_RX_HIGHEST_MASK);
149 ht_info->mcs.rx_highest = cpu_to_le16(max_bit_rate);
151 /* Tx MCS capabilities */
152 ht_info->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
153 if (tx_chains_num != rx_chains_num) {
154 ht_info->mcs.tx_params |= IEEE80211_HT_MCS_TX_RX_DIFF;
155 ht_info->mcs.tx_params |= ((tx_chains_num - 1) <<
156 IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT);
161 * iwlcore_init_geos - Initialize mac80211's geo/channel info based from eeprom
163 int iwlcore_init_geos(struct iwl_priv *priv)
165 struct iwl_channel_info *ch;
166 struct ieee80211_supported_band *sband;
167 struct ieee80211_channel *channels;
168 struct ieee80211_channel *geo_ch;
169 struct ieee80211_rate *rates;
170 int i = 0;
172 if (priv->bands[IEEE80211_BAND_2GHZ].n_bitrates ||
173 priv->bands[IEEE80211_BAND_5GHZ].n_bitrates) {
174 IWL_DEBUG_INFO(priv, "Geography modes already initialized.\n");
175 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
176 return 0;
179 channels = kzalloc(sizeof(struct ieee80211_channel) *
180 priv->channel_count, GFP_KERNEL);
181 if (!channels)
182 return -ENOMEM;
184 rates = kzalloc((sizeof(struct ieee80211_rate) * IWL_RATE_COUNT_LEGACY),
185 GFP_KERNEL);
186 if (!rates) {
187 kfree(channels);
188 return -ENOMEM;
191 /* 5.2GHz channels start after the 2.4GHz channels */
192 sband = &priv->bands[IEEE80211_BAND_5GHZ];
193 sband->channels = &channels[ARRAY_SIZE(iwl_eeprom_band_1)];
194 /* just OFDM */
195 sband->bitrates = &rates[IWL_FIRST_OFDM_RATE];
196 sband->n_bitrates = IWL_RATE_COUNT_LEGACY - IWL_FIRST_OFDM_RATE;
198 if (priv->cfg->sku & IWL_SKU_N)
199 iwlcore_init_ht_hw_capab(priv, &sband->ht_cap,
200 IEEE80211_BAND_5GHZ);
202 sband = &priv->bands[IEEE80211_BAND_2GHZ];
203 sband->channels = channels;
204 /* OFDM & CCK */
205 sband->bitrates = rates;
206 sband->n_bitrates = IWL_RATE_COUNT_LEGACY;
208 if (priv->cfg->sku & IWL_SKU_N)
209 iwlcore_init_ht_hw_capab(priv, &sband->ht_cap,
210 IEEE80211_BAND_2GHZ);
212 priv->ieee_channels = channels;
213 priv->ieee_rates = rates;
215 for (i = 0; i < priv->channel_count; i++) {
216 ch = &priv->channel_info[i];
218 /* FIXME: might be removed if scan is OK */
219 if (!is_channel_valid(ch))
220 continue;
222 sband = &priv->bands[ch->band];
224 geo_ch = &sband->channels[sband->n_channels++];
226 geo_ch->center_freq =
227 ieee80211_channel_to_frequency(ch->channel, ch->band);
228 geo_ch->max_power = ch->max_power_avg;
229 geo_ch->max_antenna_gain = 0xff;
230 geo_ch->hw_value = ch->channel;
232 if (is_channel_valid(ch)) {
233 if (!(ch->flags & EEPROM_CHANNEL_IBSS))
234 geo_ch->flags |= IEEE80211_CHAN_NO_IBSS;
236 if (!(ch->flags & EEPROM_CHANNEL_ACTIVE))
237 geo_ch->flags |= IEEE80211_CHAN_PASSIVE_SCAN;
239 if (ch->flags & EEPROM_CHANNEL_RADAR)
240 geo_ch->flags |= IEEE80211_CHAN_RADAR;
242 geo_ch->flags |= ch->ht40_extension_channel;
244 if (ch->max_power_avg > priv->tx_power_device_lmt)
245 priv->tx_power_device_lmt = ch->max_power_avg;
246 } else {
247 geo_ch->flags |= IEEE80211_CHAN_DISABLED;
250 IWL_DEBUG_INFO(priv, "Channel %d Freq=%d[%sGHz] %s flag=0x%X\n",
251 ch->channel, geo_ch->center_freq,
252 is_channel_a_band(ch) ? "5.2" : "2.4",
253 geo_ch->flags & IEEE80211_CHAN_DISABLED ?
254 "restricted" : "valid",
255 geo_ch->flags);
258 if ((priv->bands[IEEE80211_BAND_5GHZ].n_channels == 0) &&
259 priv->cfg->sku & IWL_SKU_A) {
260 IWL_INFO(priv, "Incorrectly detected BG card as ABG. "
261 "Please send your PCI ID 0x%04X:0x%04X to maintainer.\n",
262 priv->pci_dev->device,
263 priv->pci_dev->subsystem_device);
264 priv->cfg->sku &= ~IWL_SKU_A;
267 IWL_INFO(priv, "Tunable channels: %d 802.11bg, %d 802.11a channels\n",
268 priv->bands[IEEE80211_BAND_2GHZ].n_channels,
269 priv->bands[IEEE80211_BAND_5GHZ].n_channels);
271 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
273 return 0;
275 EXPORT_SYMBOL(iwlcore_init_geos);
278 * iwlcore_free_geos - undo allocations in iwlcore_init_geos
280 void iwlcore_free_geos(struct iwl_priv *priv)
282 kfree(priv->ieee_channels);
283 kfree(priv->ieee_rates);
284 clear_bit(STATUS_GEO_CONFIGURED, &priv->status);
286 EXPORT_SYMBOL(iwlcore_free_geos);
288 static bool iwl_is_channel_extension(struct iwl_priv *priv,
289 enum ieee80211_band band,
290 u16 channel, u8 extension_chan_offset)
292 const struct iwl_channel_info *ch_info;
294 ch_info = iwl_get_channel_info(priv, band, channel);
295 if (!is_channel_valid(ch_info))
296 return false;
298 if (extension_chan_offset == IEEE80211_HT_PARAM_CHA_SEC_ABOVE)
299 return !(ch_info->ht40_extension_channel &
300 IEEE80211_CHAN_NO_HT40PLUS);
301 else if (extension_chan_offset == IEEE80211_HT_PARAM_CHA_SEC_BELOW)
302 return !(ch_info->ht40_extension_channel &
303 IEEE80211_CHAN_NO_HT40MINUS);
305 return false;
308 bool iwl_is_ht40_tx_allowed(struct iwl_priv *priv,
309 struct iwl_rxon_context *ctx,
310 struct ieee80211_sta_ht_cap *ht_cap)
312 if (!ctx->ht.enabled || !ctx->ht.is_40mhz)
313 return false;
316 * We do not check for IEEE80211_HT_CAP_SUP_WIDTH_20_40
317 * the bit will not set if it is pure 40MHz case
319 if (ht_cap && !ht_cap->ht_supported)
320 return false;
322 #ifdef CONFIG_IWLWIFI_DEBUGFS
323 if (priv->disable_ht40)
324 return false;
325 #endif
327 return iwl_is_channel_extension(priv, priv->band,
328 le16_to_cpu(ctx->staging.channel),
329 ctx->ht.extension_chan_offset);
331 EXPORT_SYMBOL(iwl_is_ht40_tx_allowed);
333 static u16 iwl_adjust_beacon_interval(u16 beacon_val, u16 max_beacon_val)
335 u16 new_val;
336 u16 beacon_factor;
339 * If mac80211 hasn't given us a beacon interval, program
340 * the default into the device (not checking this here
341 * would cause the adjustment below to return the maximum
342 * value, which may break PAN.)
344 if (!beacon_val)
345 return DEFAULT_BEACON_INTERVAL;
348 * If the beacon interval we obtained from the peer
349 * is too large, we'll have to wake up more often
350 * (and in IBSS case, we'll beacon too much)
352 * For example, if max_beacon_val is 4096, and the
353 * requested beacon interval is 7000, we'll have to
354 * use 3500 to be able to wake up on the beacons.
356 * This could badly influence beacon detection stats.
359 beacon_factor = (beacon_val + max_beacon_val) / max_beacon_val;
360 new_val = beacon_val / beacon_factor;
362 if (!new_val)
363 new_val = max_beacon_val;
365 return new_val;
368 int iwl_send_rxon_timing(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
370 u64 tsf;
371 s32 interval_tm, rem;
372 struct ieee80211_conf *conf = NULL;
373 u16 beacon_int;
374 struct ieee80211_vif *vif = ctx->vif;
376 conf = ieee80211_get_hw_conf(priv->hw);
378 lockdep_assert_held(&priv->mutex);
380 memset(&ctx->timing, 0, sizeof(struct iwl_rxon_time_cmd));
382 ctx->timing.timestamp = cpu_to_le64(priv->timestamp);
383 ctx->timing.listen_interval = cpu_to_le16(conf->listen_interval);
385 beacon_int = vif ? vif->bss_conf.beacon_int : 0;
388 * TODO: For IBSS we need to get atim_window from mac80211,
389 * for now just always use 0
391 ctx->timing.atim_window = 0;
393 if (ctx->ctxid == IWL_RXON_CTX_PAN &&
394 (!ctx->vif || ctx->vif->type != NL80211_IFTYPE_STATION) &&
395 iwl_is_associated(priv, IWL_RXON_CTX_BSS) &&
396 priv->contexts[IWL_RXON_CTX_BSS].vif &&
397 priv->contexts[IWL_RXON_CTX_BSS].vif->bss_conf.beacon_int) {
398 ctx->timing.beacon_interval =
399 priv->contexts[IWL_RXON_CTX_BSS].timing.beacon_interval;
400 beacon_int = le16_to_cpu(ctx->timing.beacon_interval);
401 } else if (ctx->ctxid == IWL_RXON_CTX_BSS &&
402 iwl_is_associated(priv, IWL_RXON_CTX_PAN) &&
403 priv->contexts[IWL_RXON_CTX_PAN].vif &&
404 priv->contexts[IWL_RXON_CTX_PAN].vif->bss_conf.beacon_int &&
405 (!iwl_is_associated_ctx(ctx) || !ctx->vif ||
406 !ctx->vif->bss_conf.beacon_int)) {
407 ctx->timing.beacon_interval =
408 priv->contexts[IWL_RXON_CTX_PAN].timing.beacon_interval;
409 beacon_int = le16_to_cpu(ctx->timing.beacon_interval);
410 } else {
411 beacon_int = iwl_adjust_beacon_interval(beacon_int,
412 priv->hw_params.max_beacon_itrvl * TIME_UNIT);
413 ctx->timing.beacon_interval = cpu_to_le16(beacon_int);
416 tsf = priv->timestamp; /* tsf is modifed by do_div: copy it */
417 interval_tm = beacon_int * TIME_UNIT;
418 rem = do_div(tsf, interval_tm);
419 ctx->timing.beacon_init_val = cpu_to_le32(interval_tm - rem);
421 ctx->timing.dtim_period = vif ? (vif->bss_conf.dtim_period ?: 1) : 1;
423 IWL_DEBUG_ASSOC(priv,
424 "beacon interval %d beacon timer %d beacon tim %d\n",
425 le16_to_cpu(ctx->timing.beacon_interval),
426 le32_to_cpu(ctx->timing.beacon_init_val),
427 le16_to_cpu(ctx->timing.atim_window));
429 return iwl_send_cmd_pdu(priv, ctx->rxon_timing_cmd,
430 sizeof(ctx->timing), &ctx->timing);
432 EXPORT_SYMBOL(iwl_send_rxon_timing);
434 void iwl_set_rxon_hwcrypto(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
435 int hw_decrypt)
437 struct iwl_rxon_cmd *rxon = &ctx->staging;
439 if (hw_decrypt)
440 rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
441 else
442 rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
445 EXPORT_SYMBOL(iwl_set_rxon_hwcrypto);
447 /* validate RXON structure is valid */
448 int iwl_check_rxon_cmd(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
450 struct iwl_rxon_cmd *rxon = &ctx->staging;
451 bool error = false;
453 if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
454 if (rxon->flags & RXON_FLG_TGJ_NARROW_BAND_MSK) {
455 IWL_WARN(priv, "check 2.4G: wrong narrow\n");
456 error = true;
458 if (rxon->flags & RXON_FLG_RADAR_DETECT_MSK) {
459 IWL_WARN(priv, "check 2.4G: wrong radar\n");
460 error = true;
462 } else {
463 if (!(rxon->flags & RXON_FLG_SHORT_SLOT_MSK)) {
464 IWL_WARN(priv, "check 5.2G: not short slot!\n");
465 error = true;
467 if (rxon->flags & RXON_FLG_CCK_MSK) {
468 IWL_WARN(priv, "check 5.2G: CCK!\n");
469 error = true;
472 if ((rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1) {
473 IWL_WARN(priv, "mac/bssid mcast!\n");
474 error = true;
477 /* make sure basic rates 6Mbps and 1Mbps are supported */
478 if ((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0 &&
479 (rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0) {
480 IWL_WARN(priv, "neither 1 nor 6 are basic\n");
481 error = true;
484 if (le16_to_cpu(rxon->assoc_id) > 2007) {
485 IWL_WARN(priv, "aid > 2007\n");
486 error = true;
489 if ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
490 == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK)) {
491 IWL_WARN(priv, "CCK and short slot\n");
492 error = true;
495 if ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
496 == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK)) {
497 IWL_WARN(priv, "CCK and auto detect");
498 error = true;
501 if ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
502 RXON_FLG_TGG_PROTECT_MSK)) ==
503 RXON_FLG_TGG_PROTECT_MSK) {
504 IWL_WARN(priv, "TGg but no auto-detect\n");
505 error = true;
508 if (error)
509 IWL_WARN(priv, "Tuning to channel %d\n",
510 le16_to_cpu(rxon->channel));
512 if (error) {
513 IWL_ERR(priv, "Invalid RXON\n");
514 return -EINVAL;
516 return 0;
518 EXPORT_SYMBOL(iwl_check_rxon_cmd);
521 * iwl_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
522 * @priv: staging_rxon is compared to active_rxon
524 * If the RXON structure is changing enough to require a new tune,
525 * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
526 * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
528 int iwl_full_rxon_required(struct iwl_priv *priv,
529 struct iwl_rxon_context *ctx)
531 const struct iwl_rxon_cmd *staging = &ctx->staging;
532 const struct iwl_rxon_cmd *active = &ctx->active;
534 #define CHK(cond) \
535 if ((cond)) { \
536 IWL_DEBUG_INFO(priv, "need full RXON - " #cond "\n"); \
537 return 1; \
540 #define CHK_NEQ(c1, c2) \
541 if ((c1) != (c2)) { \
542 IWL_DEBUG_INFO(priv, "need full RXON - " \
543 #c1 " != " #c2 " - %d != %d\n", \
544 (c1), (c2)); \
545 return 1; \
548 /* These items are only settable from the full RXON command */
549 CHK(!iwl_is_associated_ctx(ctx));
550 CHK(compare_ether_addr(staging->bssid_addr, active->bssid_addr));
551 CHK(compare_ether_addr(staging->node_addr, active->node_addr));
552 CHK(compare_ether_addr(staging->wlap_bssid_addr,
553 active->wlap_bssid_addr));
554 CHK_NEQ(staging->dev_type, active->dev_type);
555 CHK_NEQ(staging->channel, active->channel);
556 CHK_NEQ(staging->air_propagation, active->air_propagation);
557 CHK_NEQ(staging->ofdm_ht_single_stream_basic_rates,
558 active->ofdm_ht_single_stream_basic_rates);
559 CHK_NEQ(staging->ofdm_ht_dual_stream_basic_rates,
560 active->ofdm_ht_dual_stream_basic_rates);
561 CHK_NEQ(staging->ofdm_ht_triple_stream_basic_rates,
562 active->ofdm_ht_triple_stream_basic_rates);
563 CHK_NEQ(staging->assoc_id, active->assoc_id);
565 /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
566 * be updated with the RXON_ASSOC command -- however only some
567 * flag transitions are allowed using RXON_ASSOC */
569 /* Check if we are not switching bands */
570 CHK_NEQ(staging->flags & RXON_FLG_BAND_24G_MSK,
571 active->flags & RXON_FLG_BAND_24G_MSK);
573 /* Check if we are switching association toggle */
574 CHK_NEQ(staging->filter_flags & RXON_FILTER_ASSOC_MSK,
575 active->filter_flags & RXON_FILTER_ASSOC_MSK);
577 #undef CHK
578 #undef CHK_NEQ
580 return 0;
582 EXPORT_SYMBOL(iwl_full_rxon_required);
584 u8 iwl_rate_get_lowest_plcp(struct iwl_priv *priv,
585 struct iwl_rxon_context *ctx)
588 * Assign the lowest rate -- should really get this from
589 * the beacon skb from mac80211.
591 if (ctx->staging.flags & RXON_FLG_BAND_24G_MSK)
592 return IWL_RATE_1M_PLCP;
593 else
594 return IWL_RATE_6M_PLCP;
596 EXPORT_SYMBOL(iwl_rate_get_lowest_plcp);
598 static void _iwl_set_rxon_ht(struct iwl_priv *priv,
599 struct iwl_ht_config *ht_conf,
600 struct iwl_rxon_context *ctx)
602 struct iwl_rxon_cmd *rxon = &ctx->staging;
604 if (!ctx->ht.enabled) {
605 rxon->flags &= ~(RXON_FLG_CHANNEL_MODE_MSK |
606 RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK |
607 RXON_FLG_HT40_PROT_MSK |
608 RXON_FLG_HT_PROT_MSK);
609 return;
612 /* FIXME: if the definition of ht.protection changed, the "translation"
613 * will be needed for rxon->flags
615 rxon->flags |= cpu_to_le32(ctx->ht.protection << RXON_FLG_HT_OPERATING_MODE_POS);
617 /* Set up channel bandwidth:
618 * 20 MHz only, 20/40 mixed or pure 40 if ht40 ok */
619 /* clear the HT channel mode before set the mode */
620 rxon->flags &= ~(RXON_FLG_CHANNEL_MODE_MSK |
621 RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK);
622 if (iwl_is_ht40_tx_allowed(priv, ctx, NULL)) {
623 /* pure ht40 */
624 if (ctx->ht.protection == IEEE80211_HT_OP_MODE_PROTECTION_20MHZ) {
625 rxon->flags |= RXON_FLG_CHANNEL_MODE_PURE_40;
626 /* Note: control channel is opposite of extension channel */
627 switch (ctx->ht.extension_chan_offset) {
628 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
629 rxon->flags &= ~RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
630 break;
631 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
632 rxon->flags |= RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
633 break;
635 } else {
636 /* Note: control channel is opposite of extension channel */
637 switch (ctx->ht.extension_chan_offset) {
638 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
639 rxon->flags &= ~(RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK);
640 rxon->flags |= RXON_FLG_CHANNEL_MODE_MIXED;
641 break;
642 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
643 rxon->flags |= RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
644 rxon->flags |= RXON_FLG_CHANNEL_MODE_MIXED;
645 break;
646 case IEEE80211_HT_PARAM_CHA_SEC_NONE:
647 default:
648 /* channel location only valid if in Mixed mode */
649 IWL_ERR(priv, "invalid extension channel offset\n");
650 break;
653 } else {
654 rxon->flags |= RXON_FLG_CHANNEL_MODE_LEGACY;
657 if (priv->cfg->ops->hcmd->set_rxon_chain)
658 priv->cfg->ops->hcmd->set_rxon_chain(priv, ctx);
660 IWL_DEBUG_ASSOC(priv, "rxon flags 0x%X operation mode :0x%X "
661 "extension channel offset 0x%x\n",
662 le32_to_cpu(rxon->flags), ctx->ht.protection,
663 ctx->ht.extension_chan_offset);
666 void iwl_set_rxon_ht(struct iwl_priv *priv, struct iwl_ht_config *ht_conf)
668 struct iwl_rxon_context *ctx;
670 for_each_context(priv, ctx)
671 _iwl_set_rxon_ht(priv, ht_conf, ctx);
673 EXPORT_SYMBOL(iwl_set_rxon_ht);
675 /* Return valid, unused, channel for a passive scan to reset the RF */
676 u8 iwl_get_single_channel_number(struct iwl_priv *priv,
677 enum ieee80211_band band)
679 const struct iwl_channel_info *ch_info;
680 int i;
681 u8 channel = 0;
682 u8 min, max;
683 struct iwl_rxon_context *ctx;
685 if (band == IEEE80211_BAND_5GHZ) {
686 min = 14;
687 max = priv->channel_count;
688 } else {
689 min = 0;
690 max = 14;
693 for (i = min; i < max; i++) {
694 bool busy = false;
696 for_each_context(priv, ctx) {
697 busy = priv->channel_info[i].channel ==
698 le16_to_cpu(ctx->staging.channel);
699 if (busy)
700 break;
703 if (busy)
704 continue;
706 channel = priv->channel_info[i].channel;
707 ch_info = iwl_get_channel_info(priv, band, channel);
708 if (is_channel_valid(ch_info))
709 break;
712 return channel;
714 EXPORT_SYMBOL(iwl_get_single_channel_number);
717 * iwl_set_rxon_channel - Set the band and channel values in staging RXON
718 * @ch: requested channel as a pointer to struct ieee80211_channel
720 * NOTE: Does not commit to the hardware; it sets appropriate bit fields
721 * in the staging RXON flag structure based on the ch->band
723 int iwl_set_rxon_channel(struct iwl_priv *priv, struct ieee80211_channel *ch,
724 struct iwl_rxon_context *ctx)
726 enum ieee80211_band band = ch->band;
727 u16 channel = ch->hw_value;
729 if ((le16_to_cpu(ctx->staging.channel) == channel) &&
730 (priv->band == band))
731 return 0;
733 ctx->staging.channel = cpu_to_le16(channel);
734 if (band == IEEE80211_BAND_5GHZ)
735 ctx->staging.flags &= ~RXON_FLG_BAND_24G_MSK;
736 else
737 ctx->staging.flags |= RXON_FLG_BAND_24G_MSK;
739 priv->band = band;
741 IWL_DEBUG_INFO(priv, "Staging channel set to %d [%d]\n", channel, band);
743 return 0;
745 EXPORT_SYMBOL(iwl_set_rxon_channel);
747 void iwl_set_flags_for_band(struct iwl_priv *priv,
748 struct iwl_rxon_context *ctx,
749 enum ieee80211_band band,
750 struct ieee80211_vif *vif)
752 if (band == IEEE80211_BAND_5GHZ) {
753 ctx->staging.flags &=
754 ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
755 | RXON_FLG_CCK_MSK);
756 ctx->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
757 } else {
758 /* Copied from iwl_post_associate() */
759 if (vif && vif->bss_conf.use_short_slot)
760 ctx->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
761 else
762 ctx->staging.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
764 ctx->staging.flags |= RXON_FLG_BAND_24G_MSK;
765 ctx->staging.flags |= RXON_FLG_AUTO_DETECT_MSK;
766 ctx->staging.flags &= ~RXON_FLG_CCK_MSK;
769 EXPORT_SYMBOL(iwl_set_flags_for_band);
772 * initialize rxon structure with default values from eeprom
774 void iwl_connection_init_rx_config(struct iwl_priv *priv,
775 struct iwl_rxon_context *ctx)
777 const struct iwl_channel_info *ch_info;
779 memset(&ctx->staging, 0, sizeof(ctx->staging));
781 if (!ctx->vif) {
782 ctx->staging.dev_type = ctx->unused_devtype;
783 } else switch (ctx->vif->type) {
784 case NL80211_IFTYPE_AP:
785 ctx->staging.dev_type = ctx->ap_devtype;
786 break;
788 case NL80211_IFTYPE_STATION:
789 ctx->staging.dev_type = ctx->station_devtype;
790 ctx->staging.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
791 break;
793 case NL80211_IFTYPE_ADHOC:
794 ctx->staging.dev_type = ctx->ibss_devtype;
795 ctx->staging.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
796 ctx->staging.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
797 RXON_FILTER_ACCEPT_GRP_MSK;
798 break;
800 default:
801 IWL_ERR(priv, "Unsupported interface type %d\n",
802 ctx->vif->type);
803 break;
806 #if 0
807 /* TODO: Figure out when short_preamble would be set and cache from
808 * that */
809 if (!hw_to_local(priv->hw)->short_preamble)
810 ctx->staging.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
811 else
812 ctx->staging.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
813 #endif
815 ch_info = iwl_get_channel_info(priv, priv->band,
816 le16_to_cpu(ctx->active.channel));
818 if (!ch_info)
819 ch_info = &priv->channel_info[0];
821 ctx->staging.channel = cpu_to_le16(ch_info->channel);
822 priv->band = ch_info->band;
824 iwl_set_flags_for_band(priv, ctx, priv->band, ctx->vif);
826 ctx->staging.ofdm_basic_rates =
827 (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
828 ctx->staging.cck_basic_rates =
829 (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
831 /* clear both MIX and PURE40 mode flag */
832 ctx->staging.flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED |
833 RXON_FLG_CHANNEL_MODE_PURE_40);
834 if (ctx->vif)
835 memcpy(ctx->staging.node_addr, ctx->vif->addr, ETH_ALEN);
837 ctx->staging.ofdm_ht_single_stream_basic_rates = 0xff;
838 ctx->staging.ofdm_ht_dual_stream_basic_rates = 0xff;
839 ctx->staging.ofdm_ht_triple_stream_basic_rates = 0xff;
841 EXPORT_SYMBOL(iwl_connection_init_rx_config);
843 void iwl_set_rate(struct iwl_priv *priv)
845 const struct ieee80211_supported_band *hw = NULL;
846 struct ieee80211_rate *rate;
847 struct iwl_rxon_context *ctx;
848 int i;
850 hw = iwl_get_hw_mode(priv, priv->band);
851 if (!hw) {
852 IWL_ERR(priv, "Failed to set rate: unable to get hw mode\n");
853 return;
856 priv->active_rate = 0;
858 for (i = 0; i < hw->n_bitrates; i++) {
859 rate = &(hw->bitrates[i]);
860 if (rate->hw_value < IWL_RATE_COUNT_LEGACY)
861 priv->active_rate |= (1 << rate->hw_value);
864 IWL_DEBUG_RATE(priv, "Set active_rate = %0x\n", priv->active_rate);
866 for_each_context(priv, ctx) {
867 ctx->staging.cck_basic_rates =
868 (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
870 ctx->staging.ofdm_basic_rates =
871 (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
874 EXPORT_SYMBOL(iwl_set_rate);
876 void iwl_chswitch_done(struct iwl_priv *priv, bool is_success)
879 * MULTI-FIXME
880 * See iwl_mac_channel_switch.
882 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
884 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
885 return;
887 if (priv->switch_rxon.switch_in_progress) {
888 ieee80211_chswitch_done(ctx->vif, is_success);
889 mutex_lock(&priv->mutex);
890 priv->switch_rxon.switch_in_progress = false;
891 mutex_unlock(&priv->mutex);
894 EXPORT_SYMBOL(iwl_chswitch_done);
896 void iwl_rx_csa(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb)
898 struct iwl_rx_packet *pkt = rxb_addr(rxb);
899 struct iwl_csa_notification *csa = &(pkt->u.csa_notif);
901 * MULTI-FIXME
902 * See iwl_mac_channel_switch.
904 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
905 struct iwl_rxon_cmd *rxon = (void *)&ctx->active;
907 if (priv->switch_rxon.switch_in_progress) {
908 if (!le32_to_cpu(csa->status) &&
909 (csa->channel == priv->switch_rxon.channel)) {
910 rxon->channel = csa->channel;
911 ctx->staging.channel = csa->channel;
912 IWL_DEBUG_11H(priv, "CSA notif: channel %d\n",
913 le16_to_cpu(csa->channel));
914 iwl_chswitch_done(priv, true);
915 } else {
916 IWL_ERR(priv, "CSA notif (fail) : channel %d\n",
917 le16_to_cpu(csa->channel));
918 iwl_chswitch_done(priv, false);
922 EXPORT_SYMBOL(iwl_rx_csa);
924 #ifdef CONFIG_IWLWIFI_DEBUG
925 void iwl_print_rx_config_cmd(struct iwl_priv *priv,
926 struct iwl_rxon_context *ctx)
928 struct iwl_rxon_cmd *rxon = &ctx->staging;
930 IWL_DEBUG_RADIO(priv, "RX CONFIG:\n");
931 iwl_print_hex_dump(priv, IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
932 IWL_DEBUG_RADIO(priv, "u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
933 IWL_DEBUG_RADIO(priv, "u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
934 IWL_DEBUG_RADIO(priv, "u32 filter_flags: 0x%08x\n",
935 le32_to_cpu(rxon->filter_flags));
936 IWL_DEBUG_RADIO(priv, "u8 dev_type: 0x%x\n", rxon->dev_type);
937 IWL_DEBUG_RADIO(priv, "u8 ofdm_basic_rates: 0x%02x\n",
938 rxon->ofdm_basic_rates);
939 IWL_DEBUG_RADIO(priv, "u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
940 IWL_DEBUG_RADIO(priv, "u8[6] node_addr: %pM\n", rxon->node_addr);
941 IWL_DEBUG_RADIO(priv, "u8[6] bssid_addr: %pM\n", rxon->bssid_addr);
942 IWL_DEBUG_RADIO(priv, "u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
944 EXPORT_SYMBOL(iwl_print_rx_config_cmd);
945 #endif
947 * iwl_irq_handle_error - called for HW or SW error interrupt from card
949 void iwl_irq_handle_error(struct iwl_priv *priv)
951 /* Set the FW error flag -- cleared on iwl_down */
952 set_bit(STATUS_FW_ERROR, &priv->status);
954 /* Cancel currently queued command. */
955 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
957 /* W/A for WiFi/WiMAX coex and WiMAX own the RF */
958 if (priv->cfg->internal_wimax_coex &&
959 (!(iwl_read_prph(priv, APMG_CLK_CTRL_REG) &
960 APMS_CLK_VAL_MRB_FUNC_MODE) ||
961 (iwl_read_prph(priv, APMG_PS_CTRL_REG) &
962 APMG_PS_CTRL_VAL_RESET_REQ))) {
963 wake_up_interruptible(&priv->wait_command_queue);
965 *Keep the restart process from trying to send host
966 * commands by clearing the INIT status bit
968 clear_bit(STATUS_READY, &priv->status);
969 IWL_ERR(priv, "RF is used by WiMAX\n");
970 return;
973 IWL_ERR(priv, "Loaded firmware version: %s\n",
974 priv->hw->wiphy->fw_version);
976 priv->cfg->ops->lib->dump_nic_error_log(priv);
977 if (priv->cfg->ops->lib->dump_csr)
978 priv->cfg->ops->lib->dump_csr(priv);
979 if (priv->cfg->ops->lib->dump_fh)
980 priv->cfg->ops->lib->dump_fh(priv, NULL, false);
981 priv->cfg->ops->lib->dump_nic_event_log(priv, false, NULL, false);
982 #ifdef CONFIG_IWLWIFI_DEBUG
983 if (iwl_get_debug_level(priv) & IWL_DL_FW_ERRORS)
984 iwl_print_rx_config_cmd(priv,
985 &priv->contexts[IWL_RXON_CTX_BSS]);
986 #endif
988 wake_up_interruptible(&priv->wait_command_queue);
990 /* Keep the restart process from trying to send host
991 * commands by clearing the INIT status bit */
992 clear_bit(STATUS_READY, &priv->status);
994 if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
995 IWL_DEBUG(priv, IWL_DL_FW_ERRORS,
996 "Restarting adapter due to uCode error.\n");
998 if (priv->cfg->mod_params->restart_fw)
999 queue_work(priv->workqueue, &priv->restart);
1002 EXPORT_SYMBOL(iwl_irq_handle_error);
1004 static int iwl_apm_stop_master(struct iwl_priv *priv)
1006 int ret = 0;
1008 /* stop device's busmaster DMA activity */
1009 iwl_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_STOP_MASTER);
1011 ret = iwl_poll_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_MASTER_DISABLED,
1012 CSR_RESET_REG_FLAG_MASTER_DISABLED, 100);
1013 if (ret)
1014 IWL_WARN(priv, "Master Disable Timed Out, 100 usec\n");
1016 IWL_DEBUG_INFO(priv, "stop master\n");
1018 return ret;
1021 void iwl_apm_stop(struct iwl_priv *priv)
1023 IWL_DEBUG_INFO(priv, "Stop card, put in low power state\n");
1025 /* Stop device's DMA activity */
1026 iwl_apm_stop_master(priv);
1028 /* Reset the entire device */
1029 iwl_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
1031 udelay(10);
1034 * Clear "initialization complete" bit to move adapter from
1035 * D0A* (powered-up Active) --> D0U* (Uninitialized) state.
1037 iwl_clear_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
1039 EXPORT_SYMBOL(iwl_apm_stop);
1043 * Start up NIC's basic functionality after it has been reset
1044 * (e.g. after platform boot, or shutdown via iwl_apm_stop())
1045 * NOTE: This does not load uCode nor start the embedded processor
1047 int iwl_apm_init(struct iwl_priv *priv)
1049 int ret = 0;
1050 u16 lctl;
1052 IWL_DEBUG_INFO(priv, "Init card's basic functions\n");
1055 * Use "set_bit" below rather than "write", to preserve any hardware
1056 * bits already set by default after reset.
1059 /* Disable L0S exit timer (platform NMI Work/Around) */
1060 iwl_set_bit(priv, CSR_GIO_CHICKEN_BITS,
1061 CSR_GIO_CHICKEN_BITS_REG_BIT_DIS_L0S_EXIT_TIMER);
1064 * Disable L0s without affecting L1;
1065 * don't wait for ICH L0s (ICH bug W/A)
1067 iwl_set_bit(priv, CSR_GIO_CHICKEN_BITS,
1068 CSR_GIO_CHICKEN_BITS_REG_BIT_L1A_NO_L0S_RX);
1070 /* Set FH wait threshold to maximum (HW error during stress W/A) */
1071 iwl_set_bit(priv, CSR_DBG_HPET_MEM_REG, CSR_DBG_HPET_MEM_REG_VAL);
1074 * Enable HAP INTA (interrupt from management bus) to
1075 * wake device's PCI Express link L1a -> L0s
1076 * NOTE: This is no-op for 3945 (non-existant bit)
1078 iwl_set_bit(priv, CSR_HW_IF_CONFIG_REG,
1079 CSR_HW_IF_CONFIG_REG_BIT_HAP_WAKE_L1A);
1082 * HW bug W/A for instability in PCIe bus L0->L0S->L1 transition.
1083 * Check if BIOS (or OS) enabled L1-ASPM on this device.
1084 * If so (likely), disable L0S, so device moves directly L0->L1;
1085 * costs negligible amount of power savings.
1086 * If not (unlikely), enable L0S, so there is at least some
1087 * power savings, even without L1.
1089 if (priv->cfg->base_params->set_l0s) {
1090 lctl = iwl_pcie_link_ctl(priv);
1091 if ((lctl & PCI_CFG_LINK_CTRL_VAL_L1_EN) ==
1092 PCI_CFG_LINK_CTRL_VAL_L1_EN) {
1093 /* L1-ASPM enabled; disable(!) L0S */
1094 iwl_set_bit(priv, CSR_GIO_REG,
1095 CSR_GIO_REG_VAL_L0S_ENABLED);
1096 IWL_DEBUG_POWER(priv, "L1 Enabled; Disabling L0S\n");
1097 } else {
1098 /* L1-ASPM disabled; enable(!) L0S */
1099 iwl_clear_bit(priv, CSR_GIO_REG,
1100 CSR_GIO_REG_VAL_L0S_ENABLED);
1101 IWL_DEBUG_POWER(priv, "L1 Disabled; Enabling L0S\n");
1105 /* Configure analog phase-lock-loop before activating to D0A */
1106 if (priv->cfg->base_params->pll_cfg_val)
1107 iwl_set_bit(priv, CSR_ANA_PLL_CFG,
1108 priv->cfg->base_params->pll_cfg_val);
1111 * Set "initialization complete" bit to move adapter from
1112 * D0U* --> D0A* (powered-up active) state.
1114 iwl_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
1117 * Wait for clock stabilization; once stabilized, access to
1118 * device-internal resources is supported, e.g. iwl_write_prph()
1119 * and accesses to uCode SRAM.
1121 ret = iwl_poll_bit(priv, CSR_GP_CNTRL,
1122 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
1123 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25000);
1124 if (ret < 0) {
1125 IWL_DEBUG_INFO(priv, "Failed to init the card\n");
1126 goto out;
1130 * Enable DMA and BSM (if used) clocks, wait for them to stabilize.
1131 * BSM (Boostrap State Machine) is only in 3945 and 4965;
1132 * later devices (i.e. 5000 and later) have non-volatile SRAM,
1133 * and don't need BSM to restore data after power-saving sleep.
1135 * Write to "CLK_EN_REG"; "1" bits enable clocks, while "0" bits
1136 * do not disable clocks. This preserves any hardware bits already
1137 * set by default in "CLK_CTRL_REG" after reset.
1139 if (priv->cfg->base_params->use_bsm)
1140 iwl_write_prph(priv, APMG_CLK_EN_REG,
1141 APMG_CLK_VAL_DMA_CLK_RQT | APMG_CLK_VAL_BSM_CLK_RQT);
1142 else
1143 iwl_write_prph(priv, APMG_CLK_EN_REG,
1144 APMG_CLK_VAL_DMA_CLK_RQT);
1145 udelay(20);
1147 /* Disable L1-Active */
1148 iwl_set_bits_prph(priv, APMG_PCIDEV_STT_REG,
1149 APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
1151 out:
1152 return ret;
1154 EXPORT_SYMBOL(iwl_apm_init);
1157 int iwl_set_tx_power(struct iwl_priv *priv, s8 tx_power, bool force)
1159 int ret;
1160 s8 prev_tx_power;
1161 bool defer;
1162 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
1164 lockdep_assert_held(&priv->mutex);
1166 if (priv->tx_power_user_lmt == tx_power && !force)
1167 return 0;
1169 if (!priv->cfg->ops->lib->send_tx_power)
1170 return -EOPNOTSUPP;
1172 if (tx_power < IWLAGN_TX_POWER_TARGET_POWER_MIN) {
1173 IWL_WARN(priv,
1174 "Requested user TXPOWER %d below lower limit %d.\n",
1175 tx_power,
1176 IWLAGN_TX_POWER_TARGET_POWER_MIN);
1177 return -EINVAL;
1180 if (tx_power > priv->tx_power_device_lmt) {
1181 IWL_WARN(priv,
1182 "Requested user TXPOWER %d above upper limit %d.\n",
1183 tx_power, priv->tx_power_device_lmt);
1184 return -EINVAL;
1187 if (!iwl_is_ready_rf(priv))
1188 return -EIO;
1190 /* scan complete and commit_rxon use tx_power_next value,
1191 * it always need to be updated for newest request */
1192 priv->tx_power_next = tx_power;
1194 /* do not set tx power when scanning or channel changing */
1195 defer = test_bit(STATUS_SCANNING, &priv->status) ||
1196 memcmp(&ctx->active, &ctx->staging, sizeof(ctx->staging));
1197 if (defer && !force) {
1198 IWL_DEBUG_INFO(priv, "Deferring tx power set\n");
1199 return 0;
1202 prev_tx_power = priv->tx_power_user_lmt;
1203 priv->tx_power_user_lmt = tx_power;
1205 ret = priv->cfg->ops->lib->send_tx_power(priv);
1207 /* if fail to set tx_power, restore the orig. tx power */
1208 if (ret) {
1209 priv->tx_power_user_lmt = prev_tx_power;
1210 priv->tx_power_next = prev_tx_power;
1212 return ret;
1214 EXPORT_SYMBOL(iwl_set_tx_power);
1216 void iwl_send_bt_config(struct iwl_priv *priv)
1218 struct iwl_bt_cmd bt_cmd = {
1219 .lead_time = BT_LEAD_TIME_DEF,
1220 .max_kill = BT_MAX_KILL_DEF,
1221 .kill_ack_mask = 0,
1222 .kill_cts_mask = 0,
1225 if (!bt_coex_active)
1226 bt_cmd.flags = BT_COEX_DISABLE;
1227 else
1228 bt_cmd.flags = BT_COEX_ENABLE;
1230 priv->bt_enable_flag = bt_cmd.flags;
1231 IWL_DEBUG_INFO(priv, "BT coex %s\n",
1232 (bt_cmd.flags == BT_COEX_DISABLE) ? "disable" : "active");
1234 if (iwl_send_cmd_pdu(priv, REPLY_BT_CONFIG,
1235 sizeof(struct iwl_bt_cmd), &bt_cmd))
1236 IWL_ERR(priv, "failed to send BT Coex Config\n");
1238 EXPORT_SYMBOL(iwl_send_bt_config);
1240 int iwl_send_statistics_request(struct iwl_priv *priv, u8 flags, bool clear)
1242 struct iwl_statistics_cmd statistics_cmd = {
1243 .configuration_flags =
1244 clear ? IWL_STATS_CONF_CLEAR_STATS : 0,
1247 if (flags & CMD_ASYNC)
1248 return iwl_send_cmd_pdu_async(priv, REPLY_STATISTICS_CMD,
1249 sizeof(struct iwl_statistics_cmd),
1250 &statistics_cmd, NULL);
1251 else
1252 return iwl_send_cmd_pdu(priv, REPLY_STATISTICS_CMD,
1253 sizeof(struct iwl_statistics_cmd),
1254 &statistics_cmd);
1256 EXPORT_SYMBOL(iwl_send_statistics_request);
1258 void iwl_rx_pm_sleep_notif(struct iwl_priv *priv,
1259 struct iwl_rx_mem_buffer *rxb)
1261 #ifdef CONFIG_IWLWIFI_DEBUG
1262 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1263 struct iwl_sleep_notification *sleep = &(pkt->u.sleep_notif);
1264 IWL_DEBUG_RX(priv, "sleep mode: %d, src: %d\n",
1265 sleep->pm_sleep_mode, sleep->pm_wakeup_src);
1266 #endif
1268 EXPORT_SYMBOL(iwl_rx_pm_sleep_notif);
1270 void iwl_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
1271 struct iwl_rx_mem_buffer *rxb)
1273 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1274 u32 len = le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
1275 IWL_DEBUG_RADIO(priv, "Dumping %d bytes of unhandled "
1276 "notification for %s:\n", len,
1277 get_cmd_string(pkt->hdr.cmd));
1278 iwl_print_hex_dump(priv, IWL_DL_RADIO, pkt->u.raw, len);
1280 EXPORT_SYMBOL(iwl_rx_pm_debug_statistics_notif);
1282 void iwl_rx_reply_error(struct iwl_priv *priv,
1283 struct iwl_rx_mem_buffer *rxb)
1285 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1287 IWL_ERR(priv, "Error Reply type 0x%08X cmd %s (0x%02X) "
1288 "seq 0x%04X ser 0x%08X\n",
1289 le32_to_cpu(pkt->u.err_resp.error_type),
1290 get_cmd_string(pkt->u.err_resp.cmd_id),
1291 pkt->u.err_resp.cmd_id,
1292 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
1293 le32_to_cpu(pkt->u.err_resp.error_info));
1295 EXPORT_SYMBOL(iwl_rx_reply_error);
1297 void iwl_clear_isr_stats(struct iwl_priv *priv)
1299 memset(&priv->isr_stats, 0, sizeof(priv->isr_stats));
1302 int iwl_mac_conf_tx(struct ieee80211_hw *hw, u16 queue,
1303 const struct ieee80211_tx_queue_params *params)
1305 struct iwl_priv *priv = hw->priv;
1306 struct iwl_rxon_context *ctx;
1307 unsigned long flags;
1308 int q;
1310 IWL_DEBUG_MAC80211(priv, "enter\n");
1312 if (!iwl_is_ready_rf(priv)) {
1313 IWL_DEBUG_MAC80211(priv, "leave - RF not ready\n");
1314 return -EIO;
1317 if (queue >= AC_NUM) {
1318 IWL_DEBUG_MAC80211(priv, "leave - queue >= AC_NUM %d\n", queue);
1319 return 0;
1322 q = AC_NUM - 1 - queue;
1324 spin_lock_irqsave(&priv->lock, flags);
1327 * MULTI-FIXME
1328 * This may need to be done per interface in nl80211/cfg80211/mac80211.
1330 for_each_context(priv, ctx) {
1331 ctx->qos_data.def_qos_parm.ac[q].cw_min =
1332 cpu_to_le16(params->cw_min);
1333 ctx->qos_data.def_qos_parm.ac[q].cw_max =
1334 cpu_to_le16(params->cw_max);
1335 ctx->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
1336 ctx->qos_data.def_qos_parm.ac[q].edca_txop =
1337 cpu_to_le16((params->txop * 32));
1339 ctx->qos_data.def_qos_parm.ac[q].reserved1 = 0;
1342 spin_unlock_irqrestore(&priv->lock, flags);
1344 IWL_DEBUG_MAC80211(priv, "leave\n");
1345 return 0;
1347 EXPORT_SYMBOL(iwl_mac_conf_tx);
1349 int iwl_mac_tx_last_beacon(struct ieee80211_hw *hw)
1351 struct iwl_priv *priv = hw->priv;
1353 return priv->ibss_manager == IWL_IBSS_MANAGER;
1355 EXPORT_SYMBOL_GPL(iwl_mac_tx_last_beacon);
1357 static int iwl_set_mode(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
1359 iwl_connection_init_rx_config(priv, ctx);
1361 if (priv->cfg->ops->hcmd->set_rxon_chain)
1362 priv->cfg->ops->hcmd->set_rxon_chain(priv, ctx);
1364 return iwlcore_commit_rxon(priv, ctx);
1367 static int iwl_setup_interface(struct iwl_priv *priv,
1368 struct iwl_rxon_context *ctx)
1370 struct ieee80211_vif *vif = ctx->vif;
1371 int err;
1373 lockdep_assert_held(&priv->mutex);
1376 * This variable will be correct only when there's just
1377 * a single context, but all code using it is for hardware
1378 * that supports only one context.
1380 priv->iw_mode = vif->type;
1382 ctx->is_active = true;
1384 err = iwl_set_mode(priv, ctx);
1385 if (err) {
1386 if (!ctx->always_active)
1387 ctx->is_active = false;
1388 return err;
1391 if (priv->cfg->bt_params && priv->cfg->bt_params->advanced_bt_coexist &&
1392 vif->type == NL80211_IFTYPE_ADHOC) {
1394 * pretend to have high BT traffic as long as we
1395 * are operating in IBSS mode, as this will cause
1396 * the rate scaling etc. to behave as intended.
1398 priv->bt_traffic_load = IWL_BT_COEX_TRAFFIC_LOAD_HIGH;
1401 return 0;
1404 int iwl_mac_add_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1406 struct iwl_priv *priv = hw->priv;
1407 struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
1408 struct iwl_rxon_context *tmp, *ctx = NULL;
1409 int err;
1410 enum nl80211_iftype viftype = ieee80211_vif_type_p2p(vif);
1412 IWL_DEBUG_MAC80211(priv, "enter: type %d, addr %pM\n",
1413 viftype, vif->addr);
1415 mutex_lock(&priv->mutex);
1417 if (!iwl_is_ready_rf(priv)) {
1418 IWL_WARN(priv, "Try to add interface when device not ready\n");
1419 err = -EINVAL;
1420 goto out;
1423 for_each_context(priv, tmp) {
1424 u32 possible_modes =
1425 tmp->interface_modes | tmp->exclusive_interface_modes;
1427 if (tmp->vif) {
1428 /* check if this busy context is exclusive */
1429 if (tmp->exclusive_interface_modes &
1430 BIT(tmp->vif->type)) {
1431 err = -EINVAL;
1432 goto out;
1434 continue;
1437 if (!(possible_modes & BIT(viftype)))
1438 continue;
1440 /* have maybe usable context w/o interface */
1441 ctx = tmp;
1442 break;
1445 if (!ctx) {
1446 err = -EOPNOTSUPP;
1447 goto out;
1450 vif_priv->ctx = ctx;
1451 ctx->vif = vif;
1453 err = iwl_setup_interface(priv, ctx);
1454 if (!err)
1455 goto out;
1457 ctx->vif = NULL;
1458 priv->iw_mode = NL80211_IFTYPE_STATION;
1459 out:
1460 mutex_unlock(&priv->mutex);
1462 IWL_DEBUG_MAC80211(priv, "leave\n");
1463 return err;
1465 EXPORT_SYMBOL(iwl_mac_add_interface);
1467 static void iwl_teardown_interface(struct iwl_priv *priv,
1468 struct ieee80211_vif *vif,
1469 bool mode_change)
1471 struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
1473 lockdep_assert_held(&priv->mutex);
1475 if (priv->scan_vif == vif) {
1476 iwl_scan_cancel_timeout(priv, 200);
1477 iwl_force_scan_end(priv);
1480 if (!mode_change) {
1481 iwl_set_mode(priv, ctx);
1482 if (!ctx->always_active)
1483 ctx->is_active = false;
1487 * When removing the IBSS interface, overwrite the
1488 * BT traffic load with the stored one from the last
1489 * notification, if any. If this is a device that
1490 * doesn't implement this, this has no effect since
1491 * both values are the same and zero.
1493 if (vif->type == NL80211_IFTYPE_ADHOC)
1494 priv->bt_traffic_load = priv->last_bt_traffic_load;
1497 void iwl_mac_remove_interface(struct ieee80211_hw *hw,
1498 struct ieee80211_vif *vif)
1500 struct iwl_priv *priv = hw->priv;
1501 struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
1503 IWL_DEBUG_MAC80211(priv, "enter\n");
1505 mutex_lock(&priv->mutex);
1507 WARN_ON(ctx->vif != vif);
1508 ctx->vif = NULL;
1510 iwl_teardown_interface(priv, vif, false);
1512 memset(priv->bssid, 0, ETH_ALEN);
1513 mutex_unlock(&priv->mutex);
1515 IWL_DEBUG_MAC80211(priv, "leave\n");
1518 EXPORT_SYMBOL(iwl_mac_remove_interface);
1520 int iwl_alloc_txq_mem(struct iwl_priv *priv)
1522 if (!priv->txq)
1523 priv->txq = kzalloc(
1524 sizeof(struct iwl_tx_queue) *
1525 priv->cfg->base_params->num_of_queues,
1526 GFP_KERNEL);
1527 if (!priv->txq) {
1528 IWL_ERR(priv, "Not enough memory for txq\n");
1529 return -ENOMEM;
1531 return 0;
1533 EXPORT_SYMBOL(iwl_alloc_txq_mem);
1535 void iwl_free_txq_mem(struct iwl_priv *priv)
1537 kfree(priv->txq);
1538 priv->txq = NULL;
1540 EXPORT_SYMBOL(iwl_free_txq_mem);
1542 #ifdef CONFIG_IWLWIFI_DEBUGFS
1544 #define IWL_TRAFFIC_DUMP_SIZE (IWL_TRAFFIC_ENTRY_SIZE * IWL_TRAFFIC_ENTRIES)
1546 void iwl_reset_traffic_log(struct iwl_priv *priv)
1548 priv->tx_traffic_idx = 0;
1549 priv->rx_traffic_idx = 0;
1550 if (priv->tx_traffic)
1551 memset(priv->tx_traffic, 0, IWL_TRAFFIC_DUMP_SIZE);
1552 if (priv->rx_traffic)
1553 memset(priv->rx_traffic, 0, IWL_TRAFFIC_DUMP_SIZE);
1556 int iwl_alloc_traffic_mem(struct iwl_priv *priv)
1558 u32 traffic_size = IWL_TRAFFIC_DUMP_SIZE;
1560 if (iwl_debug_level & IWL_DL_TX) {
1561 if (!priv->tx_traffic) {
1562 priv->tx_traffic =
1563 kzalloc(traffic_size, GFP_KERNEL);
1564 if (!priv->tx_traffic)
1565 return -ENOMEM;
1568 if (iwl_debug_level & IWL_DL_RX) {
1569 if (!priv->rx_traffic) {
1570 priv->rx_traffic =
1571 kzalloc(traffic_size, GFP_KERNEL);
1572 if (!priv->rx_traffic)
1573 return -ENOMEM;
1576 iwl_reset_traffic_log(priv);
1577 return 0;
1579 EXPORT_SYMBOL(iwl_alloc_traffic_mem);
1581 void iwl_free_traffic_mem(struct iwl_priv *priv)
1583 kfree(priv->tx_traffic);
1584 priv->tx_traffic = NULL;
1586 kfree(priv->rx_traffic);
1587 priv->rx_traffic = NULL;
1589 EXPORT_SYMBOL(iwl_free_traffic_mem);
1591 void iwl_dbg_log_tx_data_frame(struct iwl_priv *priv,
1592 u16 length, struct ieee80211_hdr *header)
1594 __le16 fc;
1595 u16 len;
1597 if (likely(!(iwl_debug_level & IWL_DL_TX)))
1598 return;
1600 if (!priv->tx_traffic)
1601 return;
1603 fc = header->frame_control;
1604 if (ieee80211_is_data(fc)) {
1605 len = (length > IWL_TRAFFIC_ENTRY_SIZE)
1606 ? IWL_TRAFFIC_ENTRY_SIZE : length;
1607 memcpy((priv->tx_traffic +
1608 (priv->tx_traffic_idx * IWL_TRAFFIC_ENTRY_SIZE)),
1609 header, len);
1610 priv->tx_traffic_idx =
1611 (priv->tx_traffic_idx + 1) % IWL_TRAFFIC_ENTRIES;
1614 EXPORT_SYMBOL(iwl_dbg_log_tx_data_frame);
1616 void iwl_dbg_log_rx_data_frame(struct iwl_priv *priv,
1617 u16 length, struct ieee80211_hdr *header)
1619 __le16 fc;
1620 u16 len;
1622 if (likely(!(iwl_debug_level & IWL_DL_RX)))
1623 return;
1625 if (!priv->rx_traffic)
1626 return;
1628 fc = header->frame_control;
1629 if (ieee80211_is_data(fc)) {
1630 len = (length > IWL_TRAFFIC_ENTRY_SIZE)
1631 ? IWL_TRAFFIC_ENTRY_SIZE : length;
1632 memcpy((priv->rx_traffic +
1633 (priv->rx_traffic_idx * IWL_TRAFFIC_ENTRY_SIZE)),
1634 header, len);
1635 priv->rx_traffic_idx =
1636 (priv->rx_traffic_idx + 1) % IWL_TRAFFIC_ENTRIES;
1639 EXPORT_SYMBOL(iwl_dbg_log_rx_data_frame);
1641 const char *get_mgmt_string(int cmd)
1643 switch (cmd) {
1644 IWL_CMD(MANAGEMENT_ASSOC_REQ);
1645 IWL_CMD(MANAGEMENT_ASSOC_RESP);
1646 IWL_CMD(MANAGEMENT_REASSOC_REQ);
1647 IWL_CMD(MANAGEMENT_REASSOC_RESP);
1648 IWL_CMD(MANAGEMENT_PROBE_REQ);
1649 IWL_CMD(MANAGEMENT_PROBE_RESP);
1650 IWL_CMD(MANAGEMENT_BEACON);
1651 IWL_CMD(MANAGEMENT_ATIM);
1652 IWL_CMD(MANAGEMENT_DISASSOC);
1653 IWL_CMD(MANAGEMENT_AUTH);
1654 IWL_CMD(MANAGEMENT_DEAUTH);
1655 IWL_CMD(MANAGEMENT_ACTION);
1656 default:
1657 return "UNKNOWN";
1662 const char *get_ctrl_string(int cmd)
1664 switch (cmd) {
1665 IWL_CMD(CONTROL_BACK_REQ);
1666 IWL_CMD(CONTROL_BACK);
1667 IWL_CMD(CONTROL_PSPOLL);
1668 IWL_CMD(CONTROL_RTS);
1669 IWL_CMD(CONTROL_CTS);
1670 IWL_CMD(CONTROL_ACK);
1671 IWL_CMD(CONTROL_CFEND);
1672 IWL_CMD(CONTROL_CFENDACK);
1673 default:
1674 return "UNKNOWN";
1679 void iwl_clear_traffic_stats(struct iwl_priv *priv)
1681 memset(&priv->tx_stats, 0, sizeof(struct traffic_stats));
1682 memset(&priv->rx_stats, 0, sizeof(struct traffic_stats));
1686 * if CONFIG_IWLWIFI_DEBUGFS defined, iwl_update_stats function will
1687 * record all the MGMT, CTRL and DATA pkt for both TX and Rx pass.
1688 * Use debugFs to display the rx/rx_statistics
1689 * if CONFIG_IWLWIFI_DEBUGFS not being defined, then no MGMT and CTRL
1690 * information will be recorded, but DATA pkt still will be recorded
1691 * for the reason of iwl_led.c need to control the led blinking based on
1692 * number of tx and rx data.
1695 void iwl_update_stats(struct iwl_priv *priv, bool is_tx, __le16 fc, u16 len)
1697 struct traffic_stats *stats;
1699 if (is_tx)
1700 stats = &priv->tx_stats;
1701 else
1702 stats = &priv->rx_stats;
1704 if (ieee80211_is_mgmt(fc)) {
1705 switch (fc & cpu_to_le16(IEEE80211_FCTL_STYPE)) {
1706 case cpu_to_le16(IEEE80211_STYPE_ASSOC_REQ):
1707 stats->mgmt[MANAGEMENT_ASSOC_REQ]++;
1708 break;
1709 case cpu_to_le16(IEEE80211_STYPE_ASSOC_RESP):
1710 stats->mgmt[MANAGEMENT_ASSOC_RESP]++;
1711 break;
1712 case cpu_to_le16(IEEE80211_STYPE_REASSOC_REQ):
1713 stats->mgmt[MANAGEMENT_REASSOC_REQ]++;
1714 break;
1715 case cpu_to_le16(IEEE80211_STYPE_REASSOC_RESP):
1716 stats->mgmt[MANAGEMENT_REASSOC_RESP]++;
1717 break;
1718 case cpu_to_le16(IEEE80211_STYPE_PROBE_REQ):
1719 stats->mgmt[MANAGEMENT_PROBE_REQ]++;
1720 break;
1721 case cpu_to_le16(IEEE80211_STYPE_PROBE_RESP):
1722 stats->mgmt[MANAGEMENT_PROBE_RESP]++;
1723 break;
1724 case cpu_to_le16(IEEE80211_STYPE_BEACON):
1725 stats->mgmt[MANAGEMENT_BEACON]++;
1726 break;
1727 case cpu_to_le16(IEEE80211_STYPE_ATIM):
1728 stats->mgmt[MANAGEMENT_ATIM]++;
1729 break;
1730 case cpu_to_le16(IEEE80211_STYPE_DISASSOC):
1731 stats->mgmt[MANAGEMENT_DISASSOC]++;
1732 break;
1733 case cpu_to_le16(IEEE80211_STYPE_AUTH):
1734 stats->mgmt[MANAGEMENT_AUTH]++;
1735 break;
1736 case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
1737 stats->mgmt[MANAGEMENT_DEAUTH]++;
1738 break;
1739 case cpu_to_le16(IEEE80211_STYPE_ACTION):
1740 stats->mgmt[MANAGEMENT_ACTION]++;
1741 break;
1743 } else if (ieee80211_is_ctl(fc)) {
1744 switch (fc & cpu_to_le16(IEEE80211_FCTL_STYPE)) {
1745 case cpu_to_le16(IEEE80211_STYPE_BACK_REQ):
1746 stats->ctrl[CONTROL_BACK_REQ]++;
1747 break;
1748 case cpu_to_le16(IEEE80211_STYPE_BACK):
1749 stats->ctrl[CONTROL_BACK]++;
1750 break;
1751 case cpu_to_le16(IEEE80211_STYPE_PSPOLL):
1752 stats->ctrl[CONTROL_PSPOLL]++;
1753 break;
1754 case cpu_to_le16(IEEE80211_STYPE_RTS):
1755 stats->ctrl[CONTROL_RTS]++;
1756 break;
1757 case cpu_to_le16(IEEE80211_STYPE_CTS):
1758 stats->ctrl[CONTROL_CTS]++;
1759 break;
1760 case cpu_to_le16(IEEE80211_STYPE_ACK):
1761 stats->ctrl[CONTROL_ACK]++;
1762 break;
1763 case cpu_to_le16(IEEE80211_STYPE_CFEND):
1764 stats->ctrl[CONTROL_CFEND]++;
1765 break;
1766 case cpu_to_le16(IEEE80211_STYPE_CFENDACK):
1767 stats->ctrl[CONTROL_CFENDACK]++;
1768 break;
1770 } else {
1771 /* data */
1772 stats->data_cnt++;
1773 stats->data_bytes += len;
1776 EXPORT_SYMBOL(iwl_update_stats);
1777 #endif
1779 static void iwl_force_rf_reset(struct iwl_priv *priv)
1781 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1782 return;
1784 if (!iwl_is_any_associated(priv)) {
1785 IWL_DEBUG_SCAN(priv, "force reset rejected: not associated\n");
1786 return;
1789 * There is no easy and better way to force reset the radio,
1790 * the only known method is switching channel which will force to
1791 * reset and tune the radio.
1792 * Use internal short scan (single channel) operation to should
1793 * achieve this objective.
1794 * Driver should reset the radio when number of consecutive missed
1795 * beacon, or any other uCode error condition detected.
1797 IWL_DEBUG_INFO(priv, "perform radio reset.\n");
1798 iwl_internal_short_hw_scan(priv);
1802 int iwl_force_reset(struct iwl_priv *priv, int mode, bool external)
1804 struct iwl_force_reset *force_reset;
1806 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1807 return -EINVAL;
1809 if (mode >= IWL_MAX_FORCE_RESET) {
1810 IWL_DEBUG_INFO(priv, "invalid reset request.\n");
1811 return -EINVAL;
1813 force_reset = &priv->force_reset[mode];
1814 force_reset->reset_request_count++;
1815 if (!external) {
1816 if (force_reset->last_force_reset_jiffies &&
1817 time_after(force_reset->last_force_reset_jiffies +
1818 force_reset->reset_duration, jiffies)) {
1819 IWL_DEBUG_INFO(priv, "force reset rejected\n");
1820 force_reset->reset_reject_count++;
1821 return -EAGAIN;
1824 force_reset->reset_success_count++;
1825 force_reset->last_force_reset_jiffies = jiffies;
1826 IWL_DEBUG_INFO(priv, "perform force reset (%d)\n", mode);
1827 switch (mode) {
1828 case IWL_RF_RESET:
1829 iwl_force_rf_reset(priv);
1830 break;
1831 case IWL_FW_RESET:
1833 * if the request is from external(ex: debugfs),
1834 * then always perform the request in regardless the module
1835 * parameter setting
1836 * if the request is from internal (uCode error or driver
1837 * detect failure), then fw_restart module parameter
1838 * need to be check before performing firmware reload
1840 if (!external && !priv->cfg->mod_params->restart_fw) {
1841 IWL_DEBUG_INFO(priv, "Cancel firmware reload based on "
1842 "module parameter setting\n");
1843 break;
1845 IWL_ERR(priv, "On demand firmware reload\n");
1846 /* Set the FW error flag -- cleared on iwl_down */
1847 set_bit(STATUS_FW_ERROR, &priv->status);
1848 wake_up_interruptible(&priv->wait_command_queue);
1850 * Keep the restart process from trying to send host
1851 * commands by clearing the INIT status bit
1853 clear_bit(STATUS_READY, &priv->status);
1854 queue_work(priv->workqueue, &priv->restart);
1855 break;
1857 return 0;
1860 int iwl_mac_change_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1861 enum nl80211_iftype newtype, bool newp2p)
1863 struct iwl_priv *priv = hw->priv;
1864 struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
1865 struct iwl_rxon_context *tmp;
1866 u32 interface_modes;
1867 int err;
1869 newtype = ieee80211_iftype_p2p(newtype, newp2p);
1871 mutex_lock(&priv->mutex);
1873 interface_modes = ctx->interface_modes | ctx->exclusive_interface_modes;
1875 if (!(interface_modes & BIT(newtype))) {
1876 err = -EBUSY;
1877 goto out;
1880 if (ctx->exclusive_interface_modes & BIT(newtype)) {
1881 for_each_context(priv, tmp) {
1882 if (ctx == tmp)
1883 continue;
1885 if (!tmp->vif)
1886 continue;
1889 * The current mode switch would be exclusive, but
1890 * another context is active ... refuse the switch.
1892 err = -EBUSY;
1893 goto out;
1897 /* success */
1898 iwl_teardown_interface(priv, vif, true);
1899 vif->type = newtype;
1900 err = iwl_setup_interface(priv, ctx);
1901 WARN_ON(err);
1903 * We've switched internally, but submitting to the
1904 * device may have failed for some reason. Mask this
1905 * error, because otherwise mac80211 will not switch
1906 * (and set the interface type back) and we'll be
1907 * out of sync with it.
1909 err = 0;
1911 out:
1912 mutex_unlock(&priv->mutex);
1913 return err;
1915 EXPORT_SYMBOL(iwl_mac_change_interface);
1918 * On every watchdog tick we check (latest) time stamp. If it does not
1919 * change during timeout period and queue is not empty we reset firmware.
1921 static int iwl_check_stuck_queue(struct iwl_priv *priv, int cnt)
1923 struct iwl_tx_queue *txq = &priv->txq[cnt];
1924 struct iwl_queue *q = &txq->q;
1925 unsigned long timeout;
1926 int ret;
1928 if (q->read_ptr == q->write_ptr) {
1929 txq->time_stamp = jiffies;
1930 return 0;
1933 timeout = txq->time_stamp +
1934 msecs_to_jiffies(priv->cfg->base_params->wd_timeout);
1936 if (time_after(jiffies, timeout)) {
1937 IWL_ERR(priv, "Queue %d stuck for %u ms.\n",
1938 q->id, priv->cfg->base_params->wd_timeout);
1939 ret = iwl_force_reset(priv, IWL_FW_RESET, false);
1940 return (ret == -EAGAIN) ? 0 : 1;
1943 return 0;
1947 * Making watchdog tick be a quarter of timeout assure we will
1948 * discover the queue hung between timeout and 1.25*timeout
1950 #define IWL_WD_TICK(timeout) ((timeout) / 4)
1953 * Watchdog timer callback, we check each tx queue for stuck, if if hung
1954 * we reset the firmware. If everything is fine just rearm the timer.
1956 void iwl_bg_watchdog(unsigned long data)
1958 struct iwl_priv *priv = (struct iwl_priv *)data;
1959 int cnt;
1960 unsigned long timeout;
1962 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1963 return;
1965 timeout = priv->cfg->base_params->wd_timeout;
1966 if (timeout == 0)
1967 return;
1969 /* monitor and check for stuck cmd queue */
1970 if (iwl_check_stuck_queue(priv, priv->cmd_queue))
1971 return;
1973 /* monitor and check for other stuck queues */
1974 if (iwl_is_any_associated(priv)) {
1975 for (cnt = 0; cnt < priv->hw_params.max_txq_num; cnt++) {
1976 /* skip as we already checked the command queue */
1977 if (cnt == priv->cmd_queue)
1978 continue;
1979 if (iwl_check_stuck_queue(priv, cnt))
1980 return;
1984 mod_timer(&priv->watchdog, jiffies +
1985 msecs_to_jiffies(IWL_WD_TICK(timeout)));
1987 EXPORT_SYMBOL(iwl_bg_watchdog);
1989 void iwl_setup_watchdog(struct iwl_priv *priv)
1991 unsigned int timeout = priv->cfg->base_params->wd_timeout;
1993 if (timeout)
1994 mod_timer(&priv->watchdog,
1995 jiffies + msecs_to_jiffies(IWL_WD_TICK(timeout)));
1996 else
1997 del_timer(&priv->watchdog);
1999 EXPORT_SYMBOL(iwl_setup_watchdog);
2002 * extended beacon time format
2003 * time in usec will be changed into a 32-bit value in extended:internal format
2004 * the extended part is the beacon counts
2005 * the internal part is the time in usec within one beacon interval
2007 u32 iwl_usecs_to_beacons(struct iwl_priv *priv, u32 usec, u32 beacon_interval)
2009 u32 quot;
2010 u32 rem;
2011 u32 interval = beacon_interval * TIME_UNIT;
2013 if (!interval || !usec)
2014 return 0;
2016 quot = (usec / interval) &
2017 (iwl_beacon_time_mask_high(priv,
2018 priv->hw_params.beacon_time_tsf_bits) >>
2019 priv->hw_params.beacon_time_tsf_bits);
2020 rem = (usec % interval) & iwl_beacon_time_mask_low(priv,
2021 priv->hw_params.beacon_time_tsf_bits);
2023 return (quot << priv->hw_params.beacon_time_tsf_bits) + rem;
2025 EXPORT_SYMBOL(iwl_usecs_to_beacons);
2027 /* base is usually what we get from ucode with each received frame,
2028 * the same as HW timer counter counting down
2030 __le32 iwl_add_beacon_time(struct iwl_priv *priv, u32 base,
2031 u32 addon, u32 beacon_interval)
2033 u32 base_low = base & iwl_beacon_time_mask_low(priv,
2034 priv->hw_params.beacon_time_tsf_bits);
2035 u32 addon_low = addon & iwl_beacon_time_mask_low(priv,
2036 priv->hw_params.beacon_time_tsf_bits);
2037 u32 interval = beacon_interval * TIME_UNIT;
2038 u32 res = (base & iwl_beacon_time_mask_high(priv,
2039 priv->hw_params.beacon_time_tsf_bits)) +
2040 (addon & iwl_beacon_time_mask_high(priv,
2041 priv->hw_params.beacon_time_tsf_bits));
2043 if (base_low > addon_low)
2044 res += base_low - addon_low;
2045 else if (base_low < addon_low) {
2046 res += interval + base_low - addon_low;
2047 res += (1 << priv->hw_params.beacon_time_tsf_bits);
2048 } else
2049 res += (1 << priv->hw_params.beacon_time_tsf_bits);
2051 return cpu_to_le32(res);
2053 EXPORT_SYMBOL(iwl_add_beacon_time);
2055 #ifdef CONFIG_PM
2057 int iwl_pci_suspend(struct device *device)
2059 struct pci_dev *pdev = to_pci_dev(device);
2060 struct iwl_priv *priv = pci_get_drvdata(pdev);
2063 * This function is called when system goes into suspend state
2064 * mac80211 will call iwl_mac_stop() from the mac80211 suspend function
2065 * first but since iwl_mac_stop() has no knowledge of who the caller is,
2066 * it will not call apm_ops.stop() to stop the DMA operation.
2067 * Calling apm_ops.stop here to make sure we stop the DMA.
2069 iwl_apm_stop(priv);
2071 return 0;
2073 EXPORT_SYMBOL(iwl_pci_suspend);
2075 int iwl_pci_resume(struct device *device)
2077 struct pci_dev *pdev = to_pci_dev(device);
2078 struct iwl_priv *priv = pci_get_drvdata(pdev);
2079 bool hw_rfkill = false;
2082 * We disable the RETRY_TIMEOUT register (0x41) to keep
2083 * PCI Tx retries from interfering with C3 CPU state.
2085 pci_write_config_byte(pdev, PCI_CFG_RETRY_TIMEOUT, 0x00);
2087 iwl_enable_interrupts(priv);
2089 if (!(iwl_read32(priv, CSR_GP_CNTRL) &
2090 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
2091 hw_rfkill = true;
2093 if (hw_rfkill)
2094 set_bit(STATUS_RF_KILL_HW, &priv->status);
2095 else
2096 clear_bit(STATUS_RF_KILL_HW, &priv->status);
2098 wiphy_rfkill_set_hw_state(priv->hw->wiphy, hw_rfkill);
2100 return 0;
2102 EXPORT_SYMBOL(iwl_pci_resume);
2104 const struct dev_pm_ops iwl_pm_ops = {
2105 .suspend = iwl_pci_suspend,
2106 .resume = iwl_pci_resume,
2107 .freeze = iwl_pci_suspend,
2108 .thaw = iwl_pci_resume,
2109 .poweroff = iwl_pci_suspend,
2110 .restore = iwl_pci_resume,
2112 EXPORT_SYMBOL(iwl_pm_ops);
2114 #endif /* CONFIG_PM */