iwlwifi: Legacy isr only used by legacy devices
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / net / wireless / iwlwifi / iwl-core.c
blobf8d801cc24fc27da0a910bc889c58cf7c9bbc99a
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 if (is_channel_a_band(ch))
223 sband = &priv->bands[IEEE80211_BAND_5GHZ];
224 else
225 sband = &priv->bands[IEEE80211_BAND_2GHZ];
227 geo_ch = &sband->channels[sband->n_channels++];
229 geo_ch->center_freq =
230 ieee80211_channel_to_frequency(ch->channel);
231 geo_ch->max_power = ch->max_power_avg;
232 geo_ch->max_antenna_gain = 0xff;
233 geo_ch->hw_value = ch->channel;
235 if (is_channel_valid(ch)) {
236 if (!(ch->flags & EEPROM_CHANNEL_IBSS))
237 geo_ch->flags |= IEEE80211_CHAN_NO_IBSS;
239 if (!(ch->flags & EEPROM_CHANNEL_ACTIVE))
240 geo_ch->flags |= IEEE80211_CHAN_PASSIVE_SCAN;
242 if (ch->flags & EEPROM_CHANNEL_RADAR)
243 geo_ch->flags |= IEEE80211_CHAN_RADAR;
245 geo_ch->flags |= ch->ht40_extension_channel;
247 if (ch->max_power_avg > priv->tx_power_device_lmt)
248 priv->tx_power_device_lmt = ch->max_power_avg;
249 } else {
250 geo_ch->flags |= IEEE80211_CHAN_DISABLED;
253 IWL_DEBUG_INFO(priv, "Channel %d Freq=%d[%sGHz] %s flag=0x%X\n",
254 ch->channel, geo_ch->center_freq,
255 is_channel_a_band(ch) ? "5.2" : "2.4",
256 geo_ch->flags & IEEE80211_CHAN_DISABLED ?
257 "restricted" : "valid",
258 geo_ch->flags);
261 if ((priv->bands[IEEE80211_BAND_5GHZ].n_channels == 0) &&
262 priv->cfg->sku & IWL_SKU_A) {
263 IWL_INFO(priv, "Incorrectly detected BG card as ABG. "
264 "Please send your PCI ID 0x%04X:0x%04X to maintainer.\n",
265 priv->pci_dev->device,
266 priv->pci_dev->subsystem_device);
267 priv->cfg->sku &= ~IWL_SKU_A;
270 IWL_INFO(priv, "Tunable channels: %d 802.11bg, %d 802.11a channels\n",
271 priv->bands[IEEE80211_BAND_2GHZ].n_channels,
272 priv->bands[IEEE80211_BAND_5GHZ].n_channels);
274 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
276 return 0;
278 EXPORT_SYMBOL(iwlcore_init_geos);
281 * iwlcore_free_geos - undo allocations in iwlcore_init_geos
283 void iwlcore_free_geos(struct iwl_priv *priv)
285 kfree(priv->ieee_channels);
286 kfree(priv->ieee_rates);
287 clear_bit(STATUS_GEO_CONFIGURED, &priv->status);
289 EXPORT_SYMBOL(iwlcore_free_geos);
292 * iwlcore_tx_cmd_protection: Set rts/cts. 3945 and 4965 only share this
293 * function.
295 void iwlcore_tx_cmd_protection(struct iwl_priv *priv,
296 struct ieee80211_tx_info *info,
297 __le16 fc, __le32 *tx_flags)
299 if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS) {
300 *tx_flags |= TX_CMD_FLG_RTS_MSK;
301 *tx_flags &= ~TX_CMD_FLG_CTS_MSK;
302 *tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
304 if (!ieee80211_is_mgmt(fc))
305 return;
307 switch (fc & cpu_to_le16(IEEE80211_FCTL_STYPE)) {
308 case cpu_to_le16(IEEE80211_STYPE_AUTH):
309 case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
310 case cpu_to_le16(IEEE80211_STYPE_ASSOC_REQ):
311 case cpu_to_le16(IEEE80211_STYPE_REASSOC_REQ):
312 *tx_flags &= ~TX_CMD_FLG_RTS_MSK;
313 *tx_flags |= TX_CMD_FLG_CTS_MSK;
314 break;
316 } else if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
317 *tx_flags &= ~TX_CMD_FLG_RTS_MSK;
318 *tx_flags |= TX_CMD_FLG_CTS_MSK;
319 *tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
322 EXPORT_SYMBOL(iwlcore_tx_cmd_protection);
325 static bool iwl_is_channel_extension(struct iwl_priv *priv,
326 enum ieee80211_band band,
327 u16 channel, u8 extension_chan_offset)
329 const struct iwl_channel_info *ch_info;
331 ch_info = iwl_get_channel_info(priv, band, channel);
332 if (!is_channel_valid(ch_info))
333 return false;
335 if (extension_chan_offset == IEEE80211_HT_PARAM_CHA_SEC_ABOVE)
336 return !(ch_info->ht40_extension_channel &
337 IEEE80211_CHAN_NO_HT40PLUS);
338 else if (extension_chan_offset == IEEE80211_HT_PARAM_CHA_SEC_BELOW)
339 return !(ch_info->ht40_extension_channel &
340 IEEE80211_CHAN_NO_HT40MINUS);
342 return false;
345 bool iwl_is_ht40_tx_allowed(struct iwl_priv *priv,
346 struct iwl_rxon_context *ctx,
347 struct ieee80211_sta_ht_cap *ht_cap)
349 if (!ctx->ht.enabled || !ctx->ht.is_40mhz)
350 return false;
353 * We do not check for IEEE80211_HT_CAP_SUP_WIDTH_20_40
354 * the bit will not set if it is pure 40MHz case
356 if (ht_cap && !ht_cap->ht_supported)
357 return false;
359 #ifdef CONFIG_IWLWIFI_DEBUGFS
360 if (priv->disable_ht40)
361 return false;
362 #endif
364 return iwl_is_channel_extension(priv, priv->band,
365 le16_to_cpu(ctx->staging.channel),
366 ctx->ht.extension_chan_offset);
368 EXPORT_SYMBOL(iwl_is_ht40_tx_allowed);
370 static u16 iwl_adjust_beacon_interval(u16 beacon_val, u16 max_beacon_val)
372 u16 new_val;
373 u16 beacon_factor;
376 * If mac80211 hasn't given us a beacon interval, program
377 * the default into the device (not checking this here
378 * would cause the adjustment below to return the maximum
379 * value, which may break PAN.)
381 if (!beacon_val)
382 return DEFAULT_BEACON_INTERVAL;
385 * If the beacon interval we obtained from the peer
386 * is too large, we'll have to wake up more often
387 * (and in IBSS case, we'll beacon too much)
389 * For example, if max_beacon_val is 4096, and the
390 * requested beacon interval is 7000, we'll have to
391 * use 3500 to be able to wake up on the beacons.
393 * This could badly influence beacon detection stats.
396 beacon_factor = (beacon_val + max_beacon_val) / max_beacon_val;
397 new_val = beacon_val / beacon_factor;
399 if (!new_val)
400 new_val = max_beacon_val;
402 return new_val;
405 int iwl_send_rxon_timing(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
407 u64 tsf;
408 s32 interval_tm, rem;
409 struct ieee80211_conf *conf = NULL;
410 u16 beacon_int;
411 struct ieee80211_vif *vif = ctx->vif;
413 conf = ieee80211_get_hw_conf(priv->hw);
415 lockdep_assert_held(&priv->mutex);
417 memset(&ctx->timing, 0, sizeof(struct iwl_rxon_time_cmd));
419 ctx->timing.timestamp = cpu_to_le64(priv->timestamp);
420 ctx->timing.listen_interval = cpu_to_le16(conf->listen_interval);
422 beacon_int = vif ? vif->bss_conf.beacon_int : 0;
425 * TODO: For IBSS we need to get atim_window from mac80211,
426 * for now just always use 0
428 ctx->timing.atim_window = 0;
430 if (ctx->ctxid == IWL_RXON_CTX_PAN &&
431 (!ctx->vif || ctx->vif->type != NL80211_IFTYPE_STATION) &&
432 iwl_is_associated(priv, IWL_RXON_CTX_BSS) &&
433 priv->contexts[IWL_RXON_CTX_BSS].vif &&
434 priv->contexts[IWL_RXON_CTX_BSS].vif->bss_conf.beacon_int) {
435 ctx->timing.beacon_interval =
436 priv->contexts[IWL_RXON_CTX_BSS].timing.beacon_interval;
437 beacon_int = le16_to_cpu(ctx->timing.beacon_interval);
438 } else if (ctx->ctxid == IWL_RXON_CTX_BSS &&
439 iwl_is_associated(priv, IWL_RXON_CTX_PAN) &&
440 priv->contexts[IWL_RXON_CTX_PAN].vif &&
441 priv->contexts[IWL_RXON_CTX_PAN].vif->bss_conf.beacon_int &&
442 (!iwl_is_associated_ctx(ctx) || !ctx->vif ||
443 !ctx->vif->bss_conf.beacon_int)) {
444 ctx->timing.beacon_interval =
445 priv->contexts[IWL_RXON_CTX_PAN].timing.beacon_interval;
446 beacon_int = le16_to_cpu(ctx->timing.beacon_interval);
447 } else {
448 beacon_int = iwl_adjust_beacon_interval(beacon_int,
449 priv->hw_params.max_beacon_itrvl * TIME_UNIT);
450 ctx->timing.beacon_interval = cpu_to_le16(beacon_int);
453 tsf = priv->timestamp; /* tsf is modifed by do_div: copy it */
454 interval_tm = beacon_int * TIME_UNIT;
455 rem = do_div(tsf, interval_tm);
456 ctx->timing.beacon_init_val = cpu_to_le32(interval_tm - rem);
458 ctx->timing.dtim_period = vif ? (vif->bss_conf.dtim_period ?: 1) : 1;
460 IWL_DEBUG_ASSOC(priv,
461 "beacon interval %d beacon timer %d beacon tim %d\n",
462 le16_to_cpu(ctx->timing.beacon_interval),
463 le32_to_cpu(ctx->timing.beacon_init_val),
464 le16_to_cpu(ctx->timing.atim_window));
466 return iwl_send_cmd_pdu(priv, ctx->rxon_timing_cmd,
467 sizeof(ctx->timing), &ctx->timing);
469 EXPORT_SYMBOL(iwl_send_rxon_timing);
471 void iwl_set_rxon_hwcrypto(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
472 int hw_decrypt)
474 struct iwl_rxon_cmd *rxon = &ctx->staging;
476 if (hw_decrypt)
477 rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
478 else
479 rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
482 EXPORT_SYMBOL(iwl_set_rxon_hwcrypto);
484 /* validate RXON structure is valid */
485 int iwl_check_rxon_cmd(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
487 struct iwl_rxon_cmd *rxon = &ctx->staging;
488 bool error = false;
490 if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
491 if (rxon->flags & RXON_FLG_TGJ_NARROW_BAND_MSK) {
492 IWL_WARN(priv, "check 2.4G: wrong narrow\n");
493 error = true;
495 if (rxon->flags & RXON_FLG_RADAR_DETECT_MSK) {
496 IWL_WARN(priv, "check 2.4G: wrong radar\n");
497 error = true;
499 } else {
500 if (!(rxon->flags & RXON_FLG_SHORT_SLOT_MSK)) {
501 IWL_WARN(priv, "check 5.2G: not short slot!\n");
502 error = true;
504 if (rxon->flags & RXON_FLG_CCK_MSK) {
505 IWL_WARN(priv, "check 5.2G: CCK!\n");
506 error = true;
509 if ((rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1) {
510 IWL_WARN(priv, "mac/bssid mcast!\n");
511 error = true;
514 /* make sure basic rates 6Mbps and 1Mbps are supported */
515 if ((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0 &&
516 (rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0) {
517 IWL_WARN(priv, "neither 1 nor 6 are basic\n");
518 error = true;
521 if (le16_to_cpu(rxon->assoc_id) > 2007) {
522 IWL_WARN(priv, "aid > 2007\n");
523 error = true;
526 if ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
527 == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK)) {
528 IWL_WARN(priv, "CCK and short slot\n");
529 error = true;
532 if ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
533 == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK)) {
534 IWL_WARN(priv, "CCK and auto detect");
535 error = true;
538 if ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
539 RXON_FLG_TGG_PROTECT_MSK)) ==
540 RXON_FLG_TGG_PROTECT_MSK) {
541 IWL_WARN(priv, "TGg but no auto-detect\n");
542 error = true;
545 if (error)
546 IWL_WARN(priv, "Tuning to channel %d\n",
547 le16_to_cpu(rxon->channel));
549 if (error) {
550 IWL_ERR(priv, "Invalid RXON\n");
551 return -EINVAL;
553 return 0;
555 EXPORT_SYMBOL(iwl_check_rxon_cmd);
558 * iwl_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
559 * @priv: staging_rxon is compared to active_rxon
561 * If the RXON structure is changing enough to require a new tune,
562 * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
563 * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
565 int iwl_full_rxon_required(struct iwl_priv *priv,
566 struct iwl_rxon_context *ctx)
568 const struct iwl_rxon_cmd *staging = &ctx->staging;
569 const struct iwl_rxon_cmd *active = &ctx->active;
571 #define CHK(cond) \
572 if ((cond)) { \
573 IWL_DEBUG_INFO(priv, "need full RXON - " #cond "\n"); \
574 return 1; \
577 #define CHK_NEQ(c1, c2) \
578 if ((c1) != (c2)) { \
579 IWL_DEBUG_INFO(priv, "need full RXON - " \
580 #c1 " != " #c2 " - %d != %d\n", \
581 (c1), (c2)); \
582 return 1; \
585 /* These items are only settable from the full RXON command */
586 CHK(!iwl_is_associated_ctx(ctx));
587 CHK(compare_ether_addr(staging->bssid_addr, active->bssid_addr));
588 CHK(compare_ether_addr(staging->node_addr, active->node_addr));
589 CHK(compare_ether_addr(staging->wlap_bssid_addr,
590 active->wlap_bssid_addr));
591 CHK_NEQ(staging->dev_type, active->dev_type);
592 CHK_NEQ(staging->channel, active->channel);
593 CHK_NEQ(staging->air_propagation, active->air_propagation);
594 CHK_NEQ(staging->ofdm_ht_single_stream_basic_rates,
595 active->ofdm_ht_single_stream_basic_rates);
596 CHK_NEQ(staging->ofdm_ht_dual_stream_basic_rates,
597 active->ofdm_ht_dual_stream_basic_rates);
598 CHK_NEQ(staging->ofdm_ht_triple_stream_basic_rates,
599 active->ofdm_ht_triple_stream_basic_rates);
600 CHK_NEQ(staging->assoc_id, active->assoc_id);
602 /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
603 * be updated with the RXON_ASSOC command -- however only some
604 * flag transitions are allowed using RXON_ASSOC */
606 /* Check if we are not switching bands */
607 CHK_NEQ(staging->flags & RXON_FLG_BAND_24G_MSK,
608 active->flags & RXON_FLG_BAND_24G_MSK);
610 /* Check if we are switching association toggle */
611 CHK_NEQ(staging->filter_flags & RXON_FILTER_ASSOC_MSK,
612 active->filter_flags & RXON_FILTER_ASSOC_MSK);
614 #undef CHK
615 #undef CHK_NEQ
617 return 0;
619 EXPORT_SYMBOL(iwl_full_rxon_required);
621 u8 iwl_rate_get_lowest_plcp(struct iwl_priv *priv,
622 struct iwl_rxon_context *ctx)
625 * Assign the lowest rate -- should really get this from
626 * the beacon skb from mac80211.
628 if (ctx->staging.flags & RXON_FLG_BAND_24G_MSK)
629 return IWL_RATE_1M_PLCP;
630 else
631 return IWL_RATE_6M_PLCP;
633 EXPORT_SYMBOL(iwl_rate_get_lowest_plcp);
635 static void _iwl_set_rxon_ht(struct iwl_priv *priv,
636 struct iwl_ht_config *ht_conf,
637 struct iwl_rxon_context *ctx)
639 struct iwl_rxon_cmd *rxon = &ctx->staging;
641 if (!ctx->ht.enabled) {
642 rxon->flags &= ~(RXON_FLG_CHANNEL_MODE_MSK |
643 RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK |
644 RXON_FLG_HT40_PROT_MSK |
645 RXON_FLG_HT_PROT_MSK);
646 return;
649 /* FIXME: if the definition of ht.protection changed, the "translation"
650 * will be needed for rxon->flags
652 rxon->flags |= cpu_to_le32(ctx->ht.protection << RXON_FLG_HT_OPERATING_MODE_POS);
654 /* Set up channel bandwidth:
655 * 20 MHz only, 20/40 mixed or pure 40 if ht40 ok */
656 /* clear the HT channel mode before set the mode */
657 rxon->flags &= ~(RXON_FLG_CHANNEL_MODE_MSK |
658 RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK);
659 if (iwl_is_ht40_tx_allowed(priv, ctx, NULL)) {
660 /* pure ht40 */
661 if (ctx->ht.protection == IEEE80211_HT_OP_MODE_PROTECTION_20MHZ) {
662 rxon->flags |= RXON_FLG_CHANNEL_MODE_PURE_40;
663 /* Note: control channel is opposite of extension channel */
664 switch (ctx->ht.extension_chan_offset) {
665 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
666 rxon->flags &= ~RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
667 break;
668 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
669 rxon->flags |= RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
670 break;
672 } else {
673 /* Note: control channel is opposite of extension channel */
674 switch (ctx->ht.extension_chan_offset) {
675 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
676 rxon->flags &= ~(RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK);
677 rxon->flags |= RXON_FLG_CHANNEL_MODE_MIXED;
678 break;
679 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
680 rxon->flags |= RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
681 rxon->flags |= RXON_FLG_CHANNEL_MODE_MIXED;
682 break;
683 case IEEE80211_HT_PARAM_CHA_SEC_NONE:
684 default:
685 /* channel location only valid if in Mixed mode */
686 IWL_ERR(priv, "invalid extension channel offset\n");
687 break;
690 } else {
691 rxon->flags |= RXON_FLG_CHANNEL_MODE_LEGACY;
694 if (priv->cfg->ops->hcmd->set_rxon_chain)
695 priv->cfg->ops->hcmd->set_rxon_chain(priv, ctx);
697 IWL_DEBUG_ASSOC(priv, "rxon flags 0x%X operation mode :0x%X "
698 "extension channel offset 0x%x\n",
699 le32_to_cpu(rxon->flags), ctx->ht.protection,
700 ctx->ht.extension_chan_offset);
703 void iwl_set_rxon_ht(struct iwl_priv *priv, struct iwl_ht_config *ht_conf)
705 struct iwl_rxon_context *ctx;
707 for_each_context(priv, ctx)
708 _iwl_set_rxon_ht(priv, ht_conf, ctx);
710 EXPORT_SYMBOL(iwl_set_rxon_ht);
712 /* Return valid, unused, channel for a passive scan to reset the RF */
713 u8 iwl_get_single_channel_number(struct iwl_priv *priv,
714 enum ieee80211_band band)
716 const struct iwl_channel_info *ch_info;
717 int i;
718 u8 channel = 0;
719 u8 min, max;
720 struct iwl_rxon_context *ctx;
722 if (band == IEEE80211_BAND_5GHZ) {
723 min = 14;
724 max = priv->channel_count;
725 } else {
726 min = 0;
727 max = 14;
730 for (i = min; i < max; i++) {
731 bool busy = false;
733 for_each_context(priv, ctx) {
734 busy = priv->channel_info[i].channel ==
735 le16_to_cpu(ctx->staging.channel);
736 if (busy)
737 break;
740 if (busy)
741 continue;
743 channel = priv->channel_info[i].channel;
744 ch_info = iwl_get_channel_info(priv, band, channel);
745 if (is_channel_valid(ch_info))
746 break;
749 return channel;
751 EXPORT_SYMBOL(iwl_get_single_channel_number);
754 * iwl_set_rxon_channel - Set the band and channel values in staging RXON
755 * @ch: requested channel as a pointer to struct ieee80211_channel
757 * NOTE: Does not commit to the hardware; it sets appropriate bit fields
758 * in the staging RXON flag structure based on the ch->band
760 int iwl_set_rxon_channel(struct iwl_priv *priv, struct ieee80211_channel *ch,
761 struct iwl_rxon_context *ctx)
763 enum ieee80211_band band = ch->band;
764 u16 channel = ch->hw_value;
766 if ((le16_to_cpu(ctx->staging.channel) == channel) &&
767 (priv->band == band))
768 return 0;
770 ctx->staging.channel = cpu_to_le16(channel);
771 if (band == IEEE80211_BAND_5GHZ)
772 ctx->staging.flags &= ~RXON_FLG_BAND_24G_MSK;
773 else
774 ctx->staging.flags |= RXON_FLG_BAND_24G_MSK;
776 priv->band = band;
778 IWL_DEBUG_INFO(priv, "Staging channel set to %d [%d]\n", channel, band);
780 return 0;
782 EXPORT_SYMBOL(iwl_set_rxon_channel);
784 void iwl_set_flags_for_band(struct iwl_priv *priv,
785 struct iwl_rxon_context *ctx,
786 enum ieee80211_band band,
787 struct ieee80211_vif *vif)
789 if (band == IEEE80211_BAND_5GHZ) {
790 ctx->staging.flags &=
791 ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
792 | RXON_FLG_CCK_MSK);
793 ctx->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
794 } else {
795 /* Copied from iwl_post_associate() */
796 if (vif && vif->bss_conf.use_short_slot)
797 ctx->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
798 else
799 ctx->staging.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
801 ctx->staging.flags |= RXON_FLG_BAND_24G_MSK;
802 ctx->staging.flags |= RXON_FLG_AUTO_DETECT_MSK;
803 ctx->staging.flags &= ~RXON_FLG_CCK_MSK;
806 EXPORT_SYMBOL(iwl_set_flags_for_band);
809 * initialize rxon structure with default values from eeprom
811 void iwl_connection_init_rx_config(struct iwl_priv *priv,
812 struct iwl_rxon_context *ctx)
814 const struct iwl_channel_info *ch_info;
816 memset(&ctx->staging, 0, sizeof(ctx->staging));
818 if (!ctx->vif) {
819 ctx->staging.dev_type = ctx->unused_devtype;
820 } else switch (ctx->vif->type) {
821 case NL80211_IFTYPE_AP:
822 ctx->staging.dev_type = ctx->ap_devtype;
823 break;
825 case NL80211_IFTYPE_STATION:
826 ctx->staging.dev_type = ctx->station_devtype;
827 ctx->staging.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
828 break;
830 case NL80211_IFTYPE_ADHOC:
831 ctx->staging.dev_type = ctx->ibss_devtype;
832 ctx->staging.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
833 ctx->staging.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
834 RXON_FILTER_ACCEPT_GRP_MSK;
835 break;
837 default:
838 IWL_ERR(priv, "Unsupported interface type %d\n",
839 ctx->vif->type);
840 break;
843 #if 0
844 /* TODO: Figure out when short_preamble would be set and cache from
845 * that */
846 if (!hw_to_local(priv->hw)->short_preamble)
847 ctx->staging.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
848 else
849 ctx->staging.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
850 #endif
852 ch_info = iwl_get_channel_info(priv, priv->band,
853 le16_to_cpu(ctx->active.channel));
855 if (!ch_info)
856 ch_info = &priv->channel_info[0];
858 ctx->staging.channel = cpu_to_le16(ch_info->channel);
859 priv->band = ch_info->band;
861 iwl_set_flags_for_band(priv, ctx, priv->band, ctx->vif);
863 ctx->staging.ofdm_basic_rates =
864 (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
865 ctx->staging.cck_basic_rates =
866 (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
868 /* clear both MIX and PURE40 mode flag */
869 ctx->staging.flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED |
870 RXON_FLG_CHANNEL_MODE_PURE_40);
871 if (ctx->vif)
872 memcpy(ctx->staging.node_addr, ctx->vif->addr, ETH_ALEN);
874 ctx->staging.ofdm_ht_single_stream_basic_rates = 0xff;
875 ctx->staging.ofdm_ht_dual_stream_basic_rates = 0xff;
876 ctx->staging.ofdm_ht_triple_stream_basic_rates = 0xff;
878 EXPORT_SYMBOL(iwl_connection_init_rx_config);
880 void iwl_set_rate(struct iwl_priv *priv)
882 const struct ieee80211_supported_band *hw = NULL;
883 struct ieee80211_rate *rate;
884 struct iwl_rxon_context *ctx;
885 int i;
887 hw = iwl_get_hw_mode(priv, priv->band);
888 if (!hw) {
889 IWL_ERR(priv, "Failed to set rate: unable to get hw mode\n");
890 return;
893 priv->active_rate = 0;
895 for (i = 0; i < hw->n_bitrates; i++) {
896 rate = &(hw->bitrates[i]);
897 if (rate->hw_value < IWL_RATE_COUNT_LEGACY)
898 priv->active_rate |= (1 << rate->hw_value);
901 IWL_DEBUG_RATE(priv, "Set active_rate = %0x\n", priv->active_rate);
903 for_each_context(priv, ctx) {
904 ctx->staging.cck_basic_rates =
905 (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
907 ctx->staging.ofdm_basic_rates =
908 (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
911 EXPORT_SYMBOL(iwl_set_rate);
913 void iwl_chswitch_done(struct iwl_priv *priv, bool is_success)
916 * MULTI-FIXME
917 * See iwl_mac_channel_switch.
919 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
921 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
922 return;
924 if (priv->switch_rxon.switch_in_progress) {
925 ieee80211_chswitch_done(ctx->vif, is_success);
926 mutex_lock(&priv->mutex);
927 priv->switch_rxon.switch_in_progress = false;
928 mutex_unlock(&priv->mutex);
931 EXPORT_SYMBOL(iwl_chswitch_done);
933 void iwl_rx_csa(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb)
935 struct iwl_rx_packet *pkt = rxb_addr(rxb);
936 struct iwl_csa_notification *csa = &(pkt->u.csa_notif);
938 * MULTI-FIXME
939 * See iwl_mac_channel_switch.
941 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
942 struct iwl_rxon_cmd *rxon = (void *)&ctx->active;
944 if (priv->switch_rxon.switch_in_progress) {
945 if (!le32_to_cpu(csa->status) &&
946 (csa->channel == priv->switch_rxon.channel)) {
947 rxon->channel = csa->channel;
948 ctx->staging.channel = csa->channel;
949 IWL_DEBUG_11H(priv, "CSA notif: channel %d\n",
950 le16_to_cpu(csa->channel));
951 iwl_chswitch_done(priv, true);
952 } else {
953 IWL_ERR(priv, "CSA notif (fail) : channel %d\n",
954 le16_to_cpu(csa->channel));
955 iwl_chswitch_done(priv, false);
959 EXPORT_SYMBOL(iwl_rx_csa);
961 #ifdef CONFIG_IWLWIFI_DEBUG
962 void iwl_print_rx_config_cmd(struct iwl_priv *priv,
963 struct iwl_rxon_context *ctx)
965 struct iwl_rxon_cmd *rxon = &ctx->staging;
967 IWL_DEBUG_RADIO(priv, "RX CONFIG:\n");
968 iwl_print_hex_dump(priv, IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
969 IWL_DEBUG_RADIO(priv, "u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
970 IWL_DEBUG_RADIO(priv, "u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
971 IWL_DEBUG_RADIO(priv, "u32 filter_flags: 0x%08x\n",
972 le32_to_cpu(rxon->filter_flags));
973 IWL_DEBUG_RADIO(priv, "u8 dev_type: 0x%x\n", rxon->dev_type);
974 IWL_DEBUG_RADIO(priv, "u8 ofdm_basic_rates: 0x%02x\n",
975 rxon->ofdm_basic_rates);
976 IWL_DEBUG_RADIO(priv, "u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
977 IWL_DEBUG_RADIO(priv, "u8[6] node_addr: %pM\n", rxon->node_addr);
978 IWL_DEBUG_RADIO(priv, "u8[6] bssid_addr: %pM\n", rxon->bssid_addr);
979 IWL_DEBUG_RADIO(priv, "u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
981 EXPORT_SYMBOL(iwl_print_rx_config_cmd);
982 #endif
984 * iwl_irq_handle_error - called for HW or SW error interrupt from card
986 void iwl_irq_handle_error(struct iwl_priv *priv)
988 /* Set the FW error flag -- cleared on iwl_down */
989 set_bit(STATUS_FW_ERROR, &priv->status);
991 /* Cancel currently queued command. */
992 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
994 IWL_ERR(priv, "Loaded firmware version: %s\n",
995 priv->hw->wiphy->fw_version);
997 priv->cfg->ops->lib->dump_nic_error_log(priv);
998 if (priv->cfg->ops->lib->dump_csr)
999 priv->cfg->ops->lib->dump_csr(priv);
1000 if (priv->cfg->ops->lib->dump_fh)
1001 priv->cfg->ops->lib->dump_fh(priv, NULL, false);
1002 priv->cfg->ops->lib->dump_nic_event_log(priv, false, NULL, false);
1003 #ifdef CONFIG_IWLWIFI_DEBUG
1004 if (iwl_get_debug_level(priv) & IWL_DL_FW_ERRORS)
1005 iwl_print_rx_config_cmd(priv,
1006 &priv->contexts[IWL_RXON_CTX_BSS]);
1007 #endif
1009 wake_up_interruptible(&priv->wait_command_queue);
1011 /* Keep the restart process from trying to send host
1012 * commands by clearing the INIT status bit */
1013 clear_bit(STATUS_READY, &priv->status);
1015 if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
1016 IWL_DEBUG(priv, IWL_DL_FW_ERRORS,
1017 "Restarting adapter due to uCode error.\n");
1019 if (priv->cfg->mod_params->restart_fw)
1020 queue_work(priv->workqueue, &priv->restart);
1023 EXPORT_SYMBOL(iwl_irq_handle_error);
1025 static int iwl_apm_stop_master(struct iwl_priv *priv)
1027 int ret = 0;
1029 /* stop device's busmaster DMA activity */
1030 iwl_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_STOP_MASTER);
1032 ret = iwl_poll_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_MASTER_DISABLED,
1033 CSR_RESET_REG_FLAG_MASTER_DISABLED, 100);
1034 if (ret)
1035 IWL_WARN(priv, "Master Disable Timed Out, 100 usec\n");
1037 IWL_DEBUG_INFO(priv, "stop master\n");
1039 return ret;
1042 void iwl_apm_stop(struct iwl_priv *priv)
1044 IWL_DEBUG_INFO(priv, "Stop card, put in low power state\n");
1046 /* Stop device's DMA activity */
1047 iwl_apm_stop_master(priv);
1049 /* Reset the entire device */
1050 iwl_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
1052 udelay(10);
1055 * Clear "initialization complete" bit to move adapter from
1056 * D0A* (powered-up Active) --> D0U* (Uninitialized) state.
1058 iwl_clear_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
1060 EXPORT_SYMBOL(iwl_apm_stop);
1064 * Start up NIC's basic functionality after it has been reset
1065 * (e.g. after platform boot, or shutdown via iwl_apm_stop())
1066 * NOTE: This does not load uCode nor start the embedded processor
1068 int iwl_apm_init(struct iwl_priv *priv)
1070 int ret = 0;
1071 u16 lctl;
1073 IWL_DEBUG_INFO(priv, "Init card's basic functions\n");
1076 * Use "set_bit" below rather than "write", to preserve any hardware
1077 * bits already set by default after reset.
1080 /* Disable L0S exit timer (platform NMI Work/Around) */
1081 iwl_set_bit(priv, CSR_GIO_CHICKEN_BITS,
1082 CSR_GIO_CHICKEN_BITS_REG_BIT_DIS_L0S_EXIT_TIMER);
1085 * Disable L0s without affecting L1;
1086 * don't wait for ICH L0s (ICH bug W/A)
1088 iwl_set_bit(priv, CSR_GIO_CHICKEN_BITS,
1089 CSR_GIO_CHICKEN_BITS_REG_BIT_L1A_NO_L0S_RX);
1091 /* Set FH wait threshold to maximum (HW error during stress W/A) */
1092 iwl_set_bit(priv, CSR_DBG_HPET_MEM_REG, CSR_DBG_HPET_MEM_REG_VAL);
1095 * Enable HAP INTA (interrupt from management bus) to
1096 * wake device's PCI Express link L1a -> L0s
1097 * NOTE: This is no-op for 3945 (non-existant bit)
1099 iwl_set_bit(priv, CSR_HW_IF_CONFIG_REG,
1100 CSR_HW_IF_CONFIG_REG_BIT_HAP_WAKE_L1A);
1103 * HW bug W/A for instability in PCIe bus L0->L0S->L1 transition.
1104 * Check if BIOS (or OS) enabled L1-ASPM on this device.
1105 * If so (likely), disable L0S, so device moves directly L0->L1;
1106 * costs negligible amount of power savings.
1107 * If not (unlikely), enable L0S, so there is at least some
1108 * power savings, even without L1.
1110 if (priv->cfg->base_params->set_l0s) {
1111 lctl = iwl_pcie_link_ctl(priv);
1112 if ((lctl & PCI_CFG_LINK_CTRL_VAL_L1_EN) ==
1113 PCI_CFG_LINK_CTRL_VAL_L1_EN) {
1114 /* L1-ASPM enabled; disable(!) L0S */
1115 iwl_set_bit(priv, CSR_GIO_REG,
1116 CSR_GIO_REG_VAL_L0S_ENABLED);
1117 IWL_DEBUG_POWER(priv, "L1 Enabled; Disabling L0S\n");
1118 } else {
1119 /* L1-ASPM disabled; enable(!) L0S */
1120 iwl_clear_bit(priv, CSR_GIO_REG,
1121 CSR_GIO_REG_VAL_L0S_ENABLED);
1122 IWL_DEBUG_POWER(priv, "L1 Disabled; Enabling L0S\n");
1126 /* Configure analog phase-lock-loop before activating to D0A */
1127 if (priv->cfg->base_params->pll_cfg_val)
1128 iwl_set_bit(priv, CSR_ANA_PLL_CFG,
1129 priv->cfg->base_params->pll_cfg_val);
1132 * Set "initialization complete" bit to move adapter from
1133 * D0U* --> D0A* (powered-up active) state.
1135 iwl_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
1138 * Wait for clock stabilization; once stabilized, access to
1139 * device-internal resources is supported, e.g. iwl_write_prph()
1140 * and accesses to uCode SRAM.
1142 ret = iwl_poll_bit(priv, CSR_GP_CNTRL,
1143 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
1144 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25000);
1145 if (ret < 0) {
1146 IWL_DEBUG_INFO(priv, "Failed to init the card\n");
1147 goto out;
1151 * Enable DMA and BSM (if used) clocks, wait for them to stabilize.
1152 * BSM (Boostrap State Machine) is only in 3945 and 4965;
1153 * later devices (i.e. 5000 and later) have non-volatile SRAM,
1154 * and don't need BSM to restore data after power-saving sleep.
1156 * Write to "CLK_EN_REG"; "1" bits enable clocks, while "0" bits
1157 * do not disable clocks. This preserves any hardware bits already
1158 * set by default in "CLK_CTRL_REG" after reset.
1160 if (priv->cfg->base_params->use_bsm)
1161 iwl_write_prph(priv, APMG_CLK_EN_REG,
1162 APMG_CLK_VAL_DMA_CLK_RQT | APMG_CLK_VAL_BSM_CLK_RQT);
1163 else
1164 iwl_write_prph(priv, APMG_CLK_EN_REG,
1165 APMG_CLK_VAL_DMA_CLK_RQT);
1166 udelay(20);
1168 /* Disable L1-Active */
1169 iwl_set_bits_prph(priv, APMG_PCIDEV_STT_REG,
1170 APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
1172 out:
1173 return ret;
1175 EXPORT_SYMBOL(iwl_apm_init);
1178 int iwl_set_tx_power(struct iwl_priv *priv, s8 tx_power, bool force)
1180 int ret;
1181 s8 prev_tx_power;
1183 lockdep_assert_held(&priv->mutex);
1185 if (priv->tx_power_user_lmt == tx_power && !force)
1186 return 0;
1188 if (!priv->cfg->ops->lib->send_tx_power)
1189 return -EOPNOTSUPP;
1191 if (tx_power < IWLAGN_TX_POWER_TARGET_POWER_MIN) {
1192 IWL_WARN(priv,
1193 "Requested user TXPOWER %d below lower limit %d.\n",
1194 tx_power,
1195 IWLAGN_TX_POWER_TARGET_POWER_MIN);
1196 return -EINVAL;
1199 if (tx_power > priv->tx_power_device_lmt) {
1200 IWL_WARN(priv,
1201 "Requested user TXPOWER %d above upper limit %d.\n",
1202 tx_power, priv->tx_power_device_lmt);
1203 return -EINVAL;
1206 if (!iwl_is_ready_rf(priv))
1207 return -EIO;
1209 /* scan complete use tx_power_next, need to be updated */
1210 priv->tx_power_next = tx_power;
1211 if (test_bit(STATUS_SCANNING, &priv->status) && !force) {
1212 IWL_DEBUG_INFO(priv, "Deferring tx power set while scanning\n");
1213 return 0;
1216 prev_tx_power = priv->tx_power_user_lmt;
1217 priv->tx_power_user_lmt = tx_power;
1219 ret = priv->cfg->ops->lib->send_tx_power(priv);
1221 /* if fail to set tx_power, restore the orig. tx power */
1222 if (ret) {
1223 priv->tx_power_user_lmt = prev_tx_power;
1224 priv->tx_power_next = prev_tx_power;
1226 return ret;
1228 EXPORT_SYMBOL(iwl_set_tx_power);
1230 void iwl_send_bt_config(struct iwl_priv *priv)
1232 struct iwl_bt_cmd bt_cmd = {
1233 .lead_time = BT_LEAD_TIME_DEF,
1234 .max_kill = BT_MAX_KILL_DEF,
1235 .kill_ack_mask = 0,
1236 .kill_cts_mask = 0,
1239 if (!bt_coex_active)
1240 bt_cmd.flags = BT_COEX_DISABLE;
1241 else
1242 bt_cmd.flags = BT_COEX_ENABLE;
1244 IWL_DEBUG_INFO(priv, "BT coex %s\n",
1245 (bt_cmd.flags == BT_COEX_DISABLE) ? "disable" : "active");
1247 if (iwl_send_cmd_pdu(priv, REPLY_BT_CONFIG,
1248 sizeof(struct iwl_bt_cmd), &bt_cmd))
1249 IWL_ERR(priv, "failed to send BT Coex Config\n");
1251 EXPORT_SYMBOL(iwl_send_bt_config);
1253 int iwl_send_statistics_request(struct iwl_priv *priv, u8 flags, bool clear)
1255 struct iwl_statistics_cmd statistics_cmd = {
1256 .configuration_flags =
1257 clear ? IWL_STATS_CONF_CLEAR_STATS : 0,
1260 if (flags & CMD_ASYNC)
1261 return iwl_send_cmd_pdu_async(priv, REPLY_STATISTICS_CMD,
1262 sizeof(struct iwl_statistics_cmd),
1263 &statistics_cmd, NULL);
1264 else
1265 return iwl_send_cmd_pdu(priv, REPLY_STATISTICS_CMD,
1266 sizeof(struct iwl_statistics_cmd),
1267 &statistics_cmd);
1269 EXPORT_SYMBOL(iwl_send_statistics_request);
1271 void iwl_rx_pm_sleep_notif(struct iwl_priv *priv,
1272 struct iwl_rx_mem_buffer *rxb)
1274 #ifdef CONFIG_IWLWIFI_DEBUG
1275 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1276 struct iwl_sleep_notification *sleep = &(pkt->u.sleep_notif);
1277 IWL_DEBUG_RX(priv, "sleep mode: %d, src: %d\n",
1278 sleep->pm_sleep_mode, sleep->pm_wakeup_src);
1279 #endif
1281 EXPORT_SYMBOL(iwl_rx_pm_sleep_notif);
1283 void iwl_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
1284 struct iwl_rx_mem_buffer *rxb)
1286 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1287 u32 len = le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
1288 IWL_DEBUG_RADIO(priv, "Dumping %d bytes of unhandled "
1289 "notification for %s:\n", len,
1290 get_cmd_string(pkt->hdr.cmd));
1291 iwl_print_hex_dump(priv, IWL_DL_RADIO, pkt->u.raw, len);
1293 EXPORT_SYMBOL(iwl_rx_pm_debug_statistics_notif);
1295 void iwl_rx_reply_error(struct iwl_priv *priv,
1296 struct iwl_rx_mem_buffer *rxb)
1298 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1300 IWL_ERR(priv, "Error Reply type 0x%08X cmd %s (0x%02X) "
1301 "seq 0x%04X ser 0x%08X\n",
1302 le32_to_cpu(pkt->u.err_resp.error_type),
1303 get_cmd_string(pkt->u.err_resp.cmd_id),
1304 pkt->u.err_resp.cmd_id,
1305 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
1306 le32_to_cpu(pkt->u.err_resp.error_info));
1308 EXPORT_SYMBOL(iwl_rx_reply_error);
1310 void iwl_clear_isr_stats(struct iwl_priv *priv)
1312 memset(&priv->isr_stats, 0, sizeof(priv->isr_stats));
1315 int iwl_mac_conf_tx(struct ieee80211_hw *hw, u16 queue,
1316 const struct ieee80211_tx_queue_params *params)
1318 struct iwl_priv *priv = hw->priv;
1319 struct iwl_rxon_context *ctx;
1320 unsigned long flags;
1321 int q;
1323 IWL_DEBUG_MAC80211(priv, "enter\n");
1325 if (!iwl_is_ready_rf(priv)) {
1326 IWL_DEBUG_MAC80211(priv, "leave - RF not ready\n");
1327 return -EIO;
1330 if (queue >= AC_NUM) {
1331 IWL_DEBUG_MAC80211(priv, "leave - queue >= AC_NUM %d\n", queue);
1332 return 0;
1335 q = AC_NUM - 1 - queue;
1337 spin_lock_irqsave(&priv->lock, flags);
1340 * MULTI-FIXME
1341 * This may need to be done per interface in nl80211/cfg80211/mac80211.
1343 for_each_context(priv, ctx) {
1344 ctx->qos_data.def_qos_parm.ac[q].cw_min =
1345 cpu_to_le16(params->cw_min);
1346 ctx->qos_data.def_qos_parm.ac[q].cw_max =
1347 cpu_to_le16(params->cw_max);
1348 ctx->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
1349 ctx->qos_data.def_qos_parm.ac[q].edca_txop =
1350 cpu_to_le16((params->txop * 32));
1352 ctx->qos_data.def_qos_parm.ac[q].reserved1 = 0;
1355 spin_unlock_irqrestore(&priv->lock, flags);
1357 IWL_DEBUG_MAC80211(priv, "leave\n");
1358 return 0;
1360 EXPORT_SYMBOL(iwl_mac_conf_tx);
1362 int iwl_mac_tx_last_beacon(struct ieee80211_hw *hw)
1364 struct iwl_priv *priv = hw->priv;
1366 return priv->ibss_manager == IWL_IBSS_MANAGER;
1368 EXPORT_SYMBOL_GPL(iwl_mac_tx_last_beacon);
1370 static int iwl_set_mode(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
1372 iwl_connection_init_rx_config(priv, ctx);
1374 if (priv->cfg->ops->hcmd->set_rxon_chain)
1375 priv->cfg->ops->hcmd->set_rxon_chain(priv, ctx);
1377 return iwlcore_commit_rxon(priv, ctx);
1380 static int iwl_setup_interface(struct iwl_priv *priv,
1381 struct iwl_rxon_context *ctx)
1383 struct ieee80211_vif *vif = ctx->vif;
1384 int err;
1386 lockdep_assert_held(&priv->mutex);
1389 * This variable will be correct only when there's just
1390 * a single context, but all code using it is for hardware
1391 * that supports only one context.
1393 priv->iw_mode = vif->type;
1395 ctx->is_active = true;
1397 err = iwl_set_mode(priv, ctx);
1398 if (err) {
1399 if (!ctx->always_active)
1400 ctx->is_active = false;
1401 return err;
1404 if (priv->cfg->bt_params && priv->cfg->bt_params->advanced_bt_coexist &&
1405 vif->type == NL80211_IFTYPE_ADHOC) {
1407 * pretend to have high BT traffic as long as we
1408 * are operating in IBSS mode, as this will cause
1409 * the rate scaling etc. to behave as intended.
1411 priv->bt_traffic_load = IWL_BT_COEX_TRAFFIC_LOAD_HIGH;
1414 return 0;
1417 int iwl_mac_add_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1419 struct iwl_priv *priv = hw->priv;
1420 struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
1421 struct iwl_rxon_context *tmp, *ctx = NULL;
1422 int err;
1424 IWL_DEBUG_MAC80211(priv, "enter: type %d, addr %pM\n",
1425 vif->type, vif->addr);
1427 mutex_lock(&priv->mutex);
1429 if (!iwl_is_ready_rf(priv)) {
1430 IWL_WARN(priv, "Try to add interface when device not ready\n");
1431 err = -EINVAL;
1432 goto out;
1435 for_each_context(priv, tmp) {
1436 u32 possible_modes =
1437 tmp->interface_modes | tmp->exclusive_interface_modes;
1439 if (tmp->vif) {
1440 /* check if this busy context is exclusive */
1441 if (tmp->exclusive_interface_modes &
1442 BIT(tmp->vif->type)) {
1443 err = -EINVAL;
1444 goto out;
1446 continue;
1449 if (!(possible_modes & BIT(vif->type)))
1450 continue;
1452 /* have maybe usable context w/o interface */
1453 ctx = tmp;
1454 break;
1457 if (!ctx) {
1458 err = -EOPNOTSUPP;
1459 goto out;
1462 vif_priv->ctx = ctx;
1463 ctx->vif = vif;
1465 err = iwl_setup_interface(priv, ctx);
1466 if (!err)
1467 goto out;
1469 ctx->vif = NULL;
1470 priv->iw_mode = NL80211_IFTYPE_STATION;
1471 out:
1472 mutex_unlock(&priv->mutex);
1474 IWL_DEBUG_MAC80211(priv, "leave\n");
1475 return err;
1477 EXPORT_SYMBOL(iwl_mac_add_interface);
1479 static void iwl_teardown_interface(struct iwl_priv *priv,
1480 struct ieee80211_vif *vif,
1481 bool mode_change)
1483 struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
1485 lockdep_assert_held(&priv->mutex);
1487 if (priv->scan_vif == vif) {
1488 iwl_scan_cancel_timeout(priv, 200);
1489 iwl_force_scan_end(priv);
1492 if (!mode_change) {
1493 iwl_set_mode(priv, ctx);
1494 if (!ctx->always_active)
1495 ctx->is_active = false;
1499 * When removing the IBSS interface, overwrite the
1500 * BT traffic load with the stored one from the last
1501 * notification, if any. If this is a device that
1502 * doesn't implement this, this has no effect since
1503 * both values are the same and zero.
1505 if (vif->type == NL80211_IFTYPE_ADHOC)
1506 priv->bt_traffic_load = priv->notif_bt_traffic_load;
1509 void iwl_mac_remove_interface(struct ieee80211_hw *hw,
1510 struct ieee80211_vif *vif)
1512 struct iwl_priv *priv = hw->priv;
1513 struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
1515 IWL_DEBUG_MAC80211(priv, "enter\n");
1517 mutex_lock(&priv->mutex);
1519 WARN_ON(ctx->vif != vif);
1520 ctx->vif = NULL;
1522 iwl_teardown_interface(priv, vif, false);
1524 memset(priv->bssid, 0, ETH_ALEN);
1525 mutex_unlock(&priv->mutex);
1527 IWL_DEBUG_MAC80211(priv, "leave\n");
1530 EXPORT_SYMBOL(iwl_mac_remove_interface);
1532 int iwl_alloc_txq_mem(struct iwl_priv *priv)
1534 if (!priv->txq)
1535 priv->txq = kzalloc(
1536 sizeof(struct iwl_tx_queue) *
1537 priv->cfg->base_params->num_of_queues,
1538 GFP_KERNEL);
1539 if (!priv->txq) {
1540 IWL_ERR(priv, "Not enough memory for txq\n");
1541 return -ENOMEM;
1543 return 0;
1545 EXPORT_SYMBOL(iwl_alloc_txq_mem);
1547 void iwl_free_txq_mem(struct iwl_priv *priv)
1549 kfree(priv->txq);
1550 priv->txq = NULL;
1552 EXPORT_SYMBOL(iwl_free_txq_mem);
1554 #ifdef CONFIG_IWLWIFI_DEBUGFS
1556 #define IWL_TRAFFIC_DUMP_SIZE (IWL_TRAFFIC_ENTRY_SIZE * IWL_TRAFFIC_ENTRIES)
1558 void iwl_reset_traffic_log(struct iwl_priv *priv)
1560 priv->tx_traffic_idx = 0;
1561 priv->rx_traffic_idx = 0;
1562 if (priv->tx_traffic)
1563 memset(priv->tx_traffic, 0, IWL_TRAFFIC_DUMP_SIZE);
1564 if (priv->rx_traffic)
1565 memset(priv->rx_traffic, 0, IWL_TRAFFIC_DUMP_SIZE);
1568 int iwl_alloc_traffic_mem(struct iwl_priv *priv)
1570 u32 traffic_size = IWL_TRAFFIC_DUMP_SIZE;
1572 if (iwl_debug_level & IWL_DL_TX) {
1573 if (!priv->tx_traffic) {
1574 priv->tx_traffic =
1575 kzalloc(traffic_size, GFP_KERNEL);
1576 if (!priv->tx_traffic)
1577 return -ENOMEM;
1580 if (iwl_debug_level & IWL_DL_RX) {
1581 if (!priv->rx_traffic) {
1582 priv->rx_traffic =
1583 kzalloc(traffic_size, GFP_KERNEL);
1584 if (!priv->rx_traffic)
1585 return -ENOMEM;
1588 iwl_reset_traffic_log(priv);
1589 return 0;
1591 EXPORT_SYMBOL(iwl_alloc_traffic_mem);
1593 void iwl_free_traffic_mem(struct iwl_priv *priv)
1595 kfree(priv->tx_traffic);
1596 priv->tx_traffic = NULL;
1598 kfree(priv->rx_traffic);
1599 priv->rx_traffic = NULL;
1601 EXPORT_SYMBOL(iwl_free_traffic_mem);
1603 void iwl_dbg_log_tx_data_frame(struct iwl_priv *priv,
1604 u16 length, struct ieee80211_hdr *header)
1606 __le16 fc;
1607 u16 len;
1609 if (likely(!(iwl_debug_level & IWL_DL_TX)))
1610 return;
1612 if (!priv->tx_traffic)
1613 return;
1615 fc = header->frame_control;
1616 if (ieee80211_is_data(fc)) {
1617 len = (length > IWL_TRAFFIC_ENTRY_SIZE)
1618 ? IWL_TRAFFIC_ENTRY_SIZE : length;
1619 memcpy((priv->tx_traffic +
1620 (priv->tx_traffic_idx * IWL_TRAFFIC_ENTRY_SIZE)),
1621 header, len);
1622 priv->tx_traffic_idx =
1623 (priv->tx_traffic_idx + 1) % IWL_TRAFFIC_ENTRIES;
1626 EXPORT_SYMBOL(iwl_dbg_log_tx_data_frame);
1628 void iwl_dbg_log_rx_data_frame(struct iwl_priv *priv,
1629 u16 length, struct ieee80211_hdr *header)
1631 __le16 fc;
1632 u16 len;
1634 if (likely(!(iwl_debug_level & IWL_DL_RX)))
1635 return;
1637 if (!priv->rx_traffic)
1638 return;
1640 fc = header->frame_control;
1641 if (ieee80211_is_data(fc)) {
1642 len = (length > IWL_TRAFFIC_ENTRY_SIZE)
1643 ? IWL_TRAFFIC_ENTRY_SIZE : length;
1644 memcpy((priv->rx_traffic +
1645 (priv->rx_traffic_idx * IWL_TRAFFIC_ENTRY_SIZE)),
1646 header, len);
1647 priv->rx_traffic_idx =
1648 (priv->rx_traffic_idx + 1) % IWL_TRAFFIC_ENTRIES;
1651 EXPORT_SYMBOL(iwl_dbg_log_rx_data_frame);
1653 const char *get_mgmt_string(int cmd)
1655 switch (cmd) {
1656 IWL_CMD(MANAGEMENT_ASSOC_REQ);
1657 IWL_CMD(MANAGEMENT_ASSOC_RESP);
1658 IWL_CMD(MANAGEMENT_REASSOC_REQ);
1659 IWL_CMD(MANAGEMENT_REASSOC_RESP);
1660 IWL_CMD(MANAGEMENT_PROBE_REQ);
1661 IWL_CMD(MANAGEMENT_PROBE_RESP);
1662 IWL_CMD(MANAGEMENT_BEACON);
1663 IWL_CMD(MANAGEMENT_ATIM);
1664 IWL_CMD(MANAGEMENT_DISASSOC);
1665 IWL_CMD(MANAGEMENT_AUTH);
1666 IWL_CMD(MANAGEMENT_DEAUTH);
1667 IWL_CMD(MANAGEMENT_ACTION);
1668 default:
1669 return "UNKNOWN";
1674 const char *get_ctrl_string(int cmd)
1676 switch (cmd) {
1677 IWL_CMD(CONTROL_BACK_REQ);
1678 IWL_CMD(CONTROL_BACK);
1679 IWL_CMD(CONTROL_PSPOLL);
1680 IWL_CMD(CONTROL_RTS);
1681 IWL_CMD(CONTROL_CTS);
1682 IWL_CMD(CONTROL_ACK);
1683 IWL_CMD(CONTROL_CFEND);
1684 IWL_CMD(CONTROL_CFENDACK);
1685 default:
1686 return "UNKNOWN";
1691 void iwl_clear_traffic_stats(struct iwl_priv *priv)
1693 memset(&priv->tx_stats, 0, sizeof(struct traffic_stats));
1694 memset(&priv->rx_stats, 0, sizeof(struct traffic_stats));
1695 priv->led_tpt = 0;
1699 * if CONFIG_IWLWIFI_DEBUGFS defined, iwl_update_stats function will
1700 * record all the MGMT, CTRL and DATA pkt for both TX and Rx pass.
1701 * Use debugFs to display the rx/rx_statistics
1702 * if CONFIG_IWLWIFI_DEBUGFS not being defined, then no MGMT and CTRL
1703 * information will be recorded, but DATA pkt still will be recorded
1704 * for the reason of iwl_led.c need to control the led blinking based on
1705 * number of tx and rx data.
1708 void iwl_update_stats(struct iwl_priv *priv, bool is_tx, __le16 fc, u16 len)
1710 struct traffic_stats *stats;
1712 if (is_tx)
1713 stats = &priv->tx_stats;
1714 else
1715 stats = &priv->rx_stats;
1717 if (ieee80211_is_mgmt(fc)) {
1718 switch (fc & cpu_to_le16(IEEE80211_FCTL_STYPE)) {
1719 case cpu_to_le16(IEEE80211_STYPE_ASSOC_REQ):
1720 stats->mgmt[MANAGEMENT_ASSOC_REQ]++;
1721 break;
1722 case cpu_to_le16(IEEE80211_STYPE_ASSOC_RESP):
1723 stats->mgmt[MANAGEMENT_ASSOC_RESP]++;
1724 break;
1725 case cpu_to_le16(IEEE80211_STYPE_REASSOC_REQ):
1726 stats->mgmt[MANAGEMENT_REASSOC_REQ]++;
1727 break;
1728 case cpu_to_le16(IEEE80211_STYPE_REASSOC_RESP):
1729 stats->mgmt[MANAGEMENT_REASSOC_RESP]++;
1730 break;
1731 case cpu_to_le16(IEEE80211_STYPE_PROBE_REQ):
1732 stats->mgmt[MANAGEMENT_PROBE_REQ]++;
1733 break;
1734 case cpu_to_le16(IEEE80211_STYPE_PROBE_RESP):
1735 stats->mgmt[MANAGEMENT_PROBE_RESP]++;
1736 break;
1737 case cpu_to_le16(IEEE80211_STYPE_BEACON):
1738 stats->mgmt[MANAGEMENT_BEACON]++;
1739 break;
1740 case cpu_to_le16(IEEE80211_STYPE_ATIM):
1741 stats->mgmt[MANAGEMENT_ATIM]++;
1742 break;
1743 case cpu_to_le16(IEEE80211_STYPE_DISASSOC):
1744 stats->mgmt[MANAGEMENT_DISASSOC]++;
1745 break;
1746 case cpu_to_le16(IEEE80211_STYPE_AUTH):
1747 stats->mgmt[MANAGEMENT_AUTH]++;
1748 break;
1749 case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
1750 stats->mgmt[MANAGEMENT_DEAUTH]++;
1751 break;
1752 case cpu_to_le16(IEEE80211_STYPE_ACTION):
1753 stats->mgmt[MANAGEMENT_ACTION]++;
1754 break;
1756 } else if (ieee80211_is_ctl(fc)) {
1757 switch (fc & cpu_to_le16(IEEE80211_FCTL_STYPE)) {
1758 case cpu_to_le16(IEEE80211_STYPE_BACK_REQ):
1759 stats->ctrl[CONTROL_BACK_REQ]++;
1760 break;
1761 case cpu_to_le16(IEEE80211_STYPE_BACK):
1762 stats->ctrl[CONTROL_BACK]++;
1763 break;
1764 case cpu_to_le16(IEEE80211_STYPE_PSPOLL):
1765 stats->ctrl[CONTROL_PSPOLL]++;
1766 break;
1767 case cpu_to_le16(IEEE80211_STYPE_RTS):
1768 stats->ctrl[CONTROL_RTS]++;
1769 break;
1770 case cpu_to_le16(IEEE80211_STYPE_CTS):
1771 stats->ctrl[CONTROL_CTS]++;
1772 break;
1773 case cpu_to_le16(IEEE80211_STYPE_ACK):
1774 stats->ctrl[CONTROL_ACK]++;
1775 break;
1776 case cpu_to_le16(IEEE80211_STYPE_CFEND):
1777 stats->ctrl[CONTROL_CFEND]++;
1778 break;
1779 case cpu_to_le16(IEEE80211_STYPE_CFENDACK):
1780 stats->ctrl[CONTROL_CFENDACK]++;
1781 break;
1783 } else {
1784 /* data */
1785 stats->data_cnt++;
1786 stats->data_bytes += len;
1788 iwl_leds_background(priv);
1790 EXPORT_SYMBOL(iwl_update_stats);
1791 #endif
1793 static void iwl_force_rf_reset(struct iwl_priv *priv)
1795 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1796 return;
1798 if (!iwl_is_any_associated(priv)) {
1799 IWL_DEBUG_SCAN(priv, "force reset rejected: not associated\n");
1800 return;
1803 * There is no easy and better way to force reset the radio,
1804 * the only known method is switching channel which will force to
1805 * reset and tune the radio.
1806 * Use internal short scan (single channel) operation to should
1807 * achieve this objective.
1808 * Driver should reset the radio when number of consecutive missed
1809 * beacon, or any other uCode error condition detected.
1811 IWL_DEBUG_INFO(priv, "perform radio reset.\n");
1812 iwl_internal_short_hw_scan(priv);
1816 int iwl_force_reset(struct iwl_priv *priv, int mode, bool external)
1818 struct iwl_force_reset *force_reset;
1820 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1821 return -EINVAL;
1823 if (mode >= IWL_MAX_FORCE_RESET) {
1824 IWL_DEBUG_INFO(priv, "invalid reset request.\n");
1825 return -EINVAL;
1827 force_reset = &priv->force_reset[mode];
1828 force_reset->reset_request_count++;
1829 if (!external) {
1830 if (force_reset->last_force_reset_jiffies &&
1831 time_after(force_reset->last_force_reset_jiffies +
1832 force_reset->reset_duration, jiffies)) {
1833 IWL_DEBUG_INFO(priv, "force reset rejected\n");
1834 force_reset->reset_reject_count++;
1835 return -EAGAIN;
1838 force_reset->reset_success_count++;
1839 force_reset->last_force_reset_jiffies = jiffies;
1840 IWL_DEBUG_INFO(priv, "perform force reset (%d)\n", mode);
1841 switch (mode) {
1842 case IWL_RF_RESET:
1843 iwl_force_rf_reset(priv);
1844 break;
1845 case IWL_FW_RESET:
1847 * if the request is from external(ex: debugfs),
1848 * then always perform the request in regardless the module
1849 * parameter setting
1850 * if the request is from internal (uCode error or driver
1851 * detect failure), then fw_restart module parameter
1852 * need to be check before performing firmware reload
1854 if (!external && !priv->cfg->mod_params->restart_fw) {
1855 IWL_DEBUG_INFO(priv, "Cancel firmware reload based on "
1856 "module parameter setting\n");
1857 break;
1859 IWL_ERR(priv, "On demand firmware reload\n");
1860 /* Set the FW error flag -- cleared on iwl_down */
1861 set_bit(STATUS_FW_ERROR, &priv->status);
1862 wake_up_interruptible(&priv->wait_command_queue);
1864 * Keep the restart process from trying to send host
1865 * commands by clearing the INIT status bit
1867 clear_bit(STATUS_READY, &priv->status);
1868 queue_work(priv->workqueue, &priv->restart);
1869 break;
1871 return 0;
1874 int iwl_mac_change_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1875 enum nl80211_iftype newtype, bool newp2p)
1877 struct iwl_priv *priv = hw->priv;
1878 struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
1879 struct iwl_rxon_context *tmp;
1880 u32 interface_modes;
1881 int err;
1883 newtype = ieee80211_iftype_p2p(newtype, newp2p);
1885 mutex_lock(&priv->mutex);
1887 interface_modes = ctx->interface_modes | ctx->exclusive_interface_modes;
1889 if (!(interface_modes & BIT(newtype))) {
1890 err = -EBUSY;
1891 goto out;
1894 if (ctx->exclusive_interface_modes & BIT(newtype)) {
1895 for_each_context(priv, tmp) {
1896 if (ctx == tmp)
1897 continue;
1899 if (!tmp->vif)
1900 continue;
1903 * The current mode switch would be exclusive, but
1904 * another context is active ... refuse the switch.
1906 err = -EBUSY;
1907 goto out;
1911 /* success */
1912 iwl_teardown_interface(priv, vif, true);
1913 vif->type = newtype;
1914 err = iwl_setup_interface(priv, ctx);
1915 WARN_ON(err);
1917 * We've switched internally, but submitting to the
1918 * device may have failed for some reason. Mask this
1919 * error, because otherwise mac80211 will not switch
1920 * (and set the interface type back) and we'll be
1921 * out of sync with it.
1923 err = 0;
1925 out:
1926 mutex_unlock(&priv->mutex);
1927 return err;
1929 EXPORT_SYMBOL(iwl_mac_change_interface);
1932 * iwl_bg_monitor_recover - Timer callback to check for stuck queue and recover
1934 * During normal condition (no queue is stuck), the timer is continually set to
1935 * execute every monitor_recover_period milliseconds after the last timer
1936 * expired. When the queue read_ptr is at the same place, the timer is
1937 * shorten to 100mSecs. This is
1938 * 1) to reduce the chance that the read_ptr may wrap around (not stuck)
1939 * 2) to detect the stuck queues quicker before the station and AP can
1940 * disassociate each other.
1942 * This function monitors all the tx queues and recover from it if any
1943 * of the queues are stuck.
1944 * 1. It first check the cmd queue for stuck conditions. If it is stuck,
1945 * it will recover by resetting the firmware and return.
1946 * 2. Then, it checks for station association. If it associates it will check
1947 * other queues. If any queue is stuck, it will recover by resetting
1948 * the firmware.
1949 * Note: It the number of times the queue read_ptr to be at the same place to
1950 * be MAX_REPEAT+1 in order to consider to be stuck.
1953 * The maximum number of times the read pointer of the tx queue at the
1954 * same place without considering to be stuck.
1956 #define MAX_REPEAT (2)
1957 static int iwl_check_stuck_queue(struct iwl_priv *priv, int cnt)
1959 struct iwl_tx_queue *txq;
1960 struct iwl_queue *q;
1962 txq = &priv->txq[cnt];
1963 q = &txq->q;
1964 /* queue is empty, skip */
1965 if (q->read_ptr == q->write_ptr)
1966 return 0;
1968 if (q->read_ptr == q->last_read_ptr) {
1969 /* a queue has not been read from last time */
1970 if (q->repeat_same_read_ptr > MAX_REPEAT) {
1971 IWL_ERR(priv,
1972 "queue %d stuck %d time. Fw reload.\n",
1973 q->id, q->repeat_same_read_ptr);
1974 q->repeat_same_read_ptr = 0;
1975 iwl_force_reset(priv, IWL_FW_RESET, false);
1976 } else {
1977 q->repeat_same_read_ptr++;
1978 IWL_DEBUG_RADIO(priv,
1979 "queue %d, not read %d time\n",
1980 q->id,
1981 q->repeat_same_read_ptr);
1982 mod_timer(&priv->monitor_recover,
1983 jiffies + msecs_to_jiffies(
1984 IWL_ONE_HUNDRED_MSECS));
1985 return 1;
1987 } else {
1988 q->last_read_ptr = q->read_ptr;
1989 q->repeat_same_read_ptr = 0;
1991 return 0;
1994 void iwl_bg_monitor_recover(unsigned long data)
1996 struct iwl_priv *priv = (struct iwl_priv *)data;
1997 int cnt;
1999 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2000 return;
2002 /* monitor and check for stuck cmd queue */
2003 if (iwl_check_stuck_queue(priv, priv->cmd_queue))
2004 return;
2006 /* monitor and check for other stuck queues */
2007 if (iwl_is_any_associated(priv)) {
2008 for (cnt = 0; cnt < priv->hw_params.max_txq_num; cnt++) {
2009 /* skip as we already checked the command queue */
2010 if (cnt == priv->cmd_queue)
2011 continue;
2012 if (iwl_check_stuck_queue(priv, cnt))
2013 return;
2016 if (priv->cfg->base_params->monitor_recover_period) {
2018 * Reschedule the timer to occur in
2019 * priv->cfg->base_params->monitor_recover_period
2021 mod_timer(&priv->monitor_recover, jiffies + msecs_to_jiffies(
2022 priv->cfg->base_params->monitor_recover_period));
2025 EXPORT_SYMBOL(iwl_bg_monitor_recover);
2029 * extended beacon time format
2030 * time in usec will be changed into a 32-bit value in extended:internal format
2031 * the extended part is the beacon counts
2032 * the internal part is the time in usec within one beacon interval
2034 u32 iwl_usecs_to_beacons(struct iwl_priv *priv, u32 usec, u32 beacon_interval)
2036 u32 quot;
2037 u32 rem;
2038 u32 interval = beacon_interval * TIME_UNIT;
2040 if (!interval || !usec)
2041 return 0;
2043 quot = (usec / interval) &
2044 (iwl_beacon_time_mask_high(priv,
2045 priv->hw_params.beacon_time_tsf_bits) >>
2046 priv->hw_params.beacon_time_tsf_bits);
2047 rem = (usec % interval) & iwl_beacon_time_mask_low(priv,
2048 priv->hw_params.beacon_time_tsf_bits);
2050 return (quot << priv->hw_params.beacon_time_tsf_bits) + rem;
2052 EXPORT_SYMBOL(iwl_usecs_to_beacons);
2054 /* base is usually what we get from ucode with each received frame,
2055 * the same as HW timer counter counting down
2057 __le32 iwl_add_beacon_time(struct iwl_priv *priv, u32 base,
2058 u32 addon, u32 beacon_interval)
2060 u32 base_low = base & iwl_beacon_time_mask_low(priv,
2061 priv->hw_params.beacon_time_tsf_bits);
2062 u32 addon_low = addon & iwl_beacon_time_mask_low(priv,
2063 priv->hw_params.beacon_time_tsf_bits);
2064 u32 interval = beacon_interval * TIME_UNIT;
2065 u32 res = (base & iwl_beacon_time_mask_high(priv,
2066 priv->hw_params.beacon_time_tsf_bits)) +
2067 (addon & iwl_beacon_time_mask_high(priv,
2068 priv->hw_params.beacon_time_tsf_bits));
2070 if (base_low > addon_low)
2071 res += base_low - addon_low;
2072 else if (base_low < addon_low) {
2073 res += interval + base_low - addon_low;
2074 res += (1 << priv->hw_params.beacon_time_tsf_bits);
2075 } else
2076 res += (1 << priv->hw_params.beacon_time_tsf_bits);
2078 return cpu_to_le32(res);
2080 EXPORT_SYMBOL(iwl_add_beacon_time);
2082 #ifdef CONFIG_PM
2084 int iwl_pci_suspend(struct device *device)
2086 struct pci_dev *pdev = to_pci_dev(device);
2087 struct iwl_priv *priv = pci_get_drvdata(pdev);
2090 * This function is called when system goes into suspend state
2091 * mac80211 will call iwl_mac_stop() from the mac80211 suspend function
2092 * first but since iwl_mac_stop() has no knowledge of who the caller is,
2093 * it will not call apm_ops.stop() to stop the DMA operation.
2094 * Calling apm_ops.stop here to make sure we stop the DMA.
2096 iwl_apm_stop(priv);
2098 return 0;
2100 EXPORT_SYMBOL(iwl_pci_suspend);
2102 int iwl_pci_resume(struct device *device)
2104 struct pci_dev *pdev = to_pci_dev(device);
2105 struct iwl_priv *priv = pci_get_drvdata(pdev);
2106 bool hw_rfkill = false;
2109 * We disable the RETRY_TIMEOUT register (0x41) to keep
2110 * PCI Tx retries from interfering with C3 CPU state.
2112 pci_write_config_byte(pdev, PCI_CFG_RETRY_TIMEOUT, 0x00);
2114 iwl_enable_interrupts(priv);
2116 if (!(iwl_read32(priv, CSR_GP_CNTRL) &
2117 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
2118 hw_rfkill = true;
2120 if (hw_rfkill)
2121 set_bit(STATUS_RF_KILL_HW, &priv->status);
2122 else
2123 clear_bit(STATUS_RF_KILL_HW, &priv->status);
2125 wiphy_rfkill_set_hw_state(priv->hw->wiphy, hw_rfkill);
2127 return 0;
2129 EXPORT_SYMBOL(iwl_pci_resume);
2131 const struct dev_pm_ops iwl_pm_ops = {
2132 .suspend = iwl_pci_suspend,
2133 .resume = iwl_pci_resume,
2134 .freeze = iwl_pci_suspend,
2135 .thaw = iwl_pci_resume,
2136 .poweroff = iwl_pci_suspend,
2137 .restore = iwl_pci_resume,
2139 EXPORT_SYMBOL(iwl_pm_ops);
2141 #endif /* CONFIG_PM */