iwlwifi: rework broadcast station management
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / net / wireless / iwlwifi / iwl-core.c
blobf007b36ec54e8a014e8818f6c776aacd68d2f6f6
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 <net/mac80211.h>
35 #include "iwl-eeprom.h"
36 #include "iwl-dev.h" /* FIXME: remove */
37 #include "iwl-debug.h"
38 #include "iwl-core.h"
39 #include "iwl-io.h"
40 #include "iwl-power.h"
41 #include "iwl-sta.h"
42 #include "iwl-helpers.h"
45 MODULE_DESCRIPTION("iwl core");
46 MODULE_VERSION(IWLWIFI_VERSION);
47 MODULE_AUTHOR(DRV_COPYRIGHT " " DRV_AUTHOR);
48 MODULE_LICENSE("GPL");
51 * set bt_coex_active to true, uCode will do kill/defer
52 * every time the priority line is asserted (BT is sending signals on the
53 * priority line in the PCIx).
54 * set bt_coex_active to false, uCode will ignore the BT activity and
55 * perform the normal operation
57 * User might experience transmit issue on some platform due to WiFi/BT
58 * co-exist problem. The possible behaviors are:
59 * Able to scan and finding all the available AP
60 * Not able to associate with any AP
61 * On those platforms, WiFi communication can be restored by set
62 * "bt_coex_active" module parameter to "false"
64 * default: bt_coex_active = true (BT_COEX_ENABLE)
66 static bool bt_coex_active = true;
67 module_param(bt_coex_active, bool, S_IRUGO);
68 MODULE_PARM_DESC(bt_coex_active, "enable wifi/bluetooth co-exist");
70 #define IWL_DECLARE_RATE_INFO(r, s, ip, in, rp, rn, pp, np) \
71 [IWL_RATE_##r##M_INDEX] = { IWL_RATE_##r##M_PLCP, \
72 IWL_RATE_SISO_##s##M_PLCP, \
73 IWL_RATE_MIMO2_##s##M_PLCP,\
74 IWL_RATE_MIMO3_##s##M_PLCP,\
75 IWL_RATE_##r##M_IEEE, \
76 IWL_RATE_##ip##M_INDEX, \
77 IWL_RATE_##in##M_INDEX, \
78 IWL_RATE_##rp##M_INDEX, \
79 IWL_RATE_##rn##M_INDEX, \
80 IWL_RATE_##pp##M_INDEX, \
81 IWL_RATE_##np##M_INDEX }
83 u32 iwl_debug_level;
84 EXPORT_SYMBOL(iwl_debug_level);
87 * Parameter order:
88 * rate, ht rate, prev rate, next rate, prev tgg rate, next tgg rate
90 * If there isn't a valid next or previous rate then INV is used which
91 * maps to IWL_RATE_INVALID
94 const struct iwl_rate_info iwl_rates[IWL_RATE_COUNT] = {
95 IWL_DECLARE_RATE_INFO(1, INV, INV, 2, INV, 2, INV, 2), /* 1mbps */
96 IWL_DECLARE_RATE_INFO(2, INV, 1, 5, 1, 5, 1, 5), /* 2mbps */
97 IWL_DECLARE_RATE_INFO(5, INV, 2, 6, 2, 11, 2, 11), /*5.5mbps */
98 IWL_DECLARE_RATE_INFO(11, INV, 9, 12, 9, 12, 5, 18), /* 11mbps */
99 IWL_DECLARE_RATE_INFO(6, 6, 5, 9, 5, 11, 5, 11), /* 6mbps */
100 IWL_DECLARE_RATE_INFO(9, 6, 6, 11, 6, 11, 5, 11), /* 9mbps */
101 IWL_DECLARE_RATE_INFO(12, 12, 11, 18, 11, 18, 11, 18), /* 12mbps */
102 IWL_DECLARE_RATE_INFO(18, 18, 12, 24, 12, 24, 11, 24), /* 18mbps */
103 IWL_DECLARE_RATE_INFO(24, 24, 18, 36, 18, 36, 18, 36), /* 24mbps */
104 IWL_DECLARE_RATE_INFO(36, 36, 24, 48, 24, 48, 24, 48), /* 36mbps */
105 IWL_DECLARE_RATE_INFO(48, 48, 36, 54, 36, 54, 36, 54), /* 48mbps */
106 IWL_DECLARE_RATE_INFO(54, 54, 48, INV, 48, INV, 48, INV),/* 54mbps */
107 IWL_DECLARE_RATE_INFO(60, 60, 48, INV, 48, INV, 48, INV),/* 60mbps */
108 /* FIXME:RS: ^^ should be INV (legacy) */
110 EXPORT_SYMBOL(iwl_rates);
112 int iwl_hwrate_to_plcp_idx(u32 rate_n_flags)
114 int idx = 0;
116 /* HT rate format */
117 if (rate_n_flags & RATE_MCS_HT_MSK) {
118 idx = (rate_n_flags & 0xff);
120 if (idx >= IWL_RATE_MIMO3_6M_PLCP)
121 idx = idx - IWL_RATE_MIMO3_6M_PLCP;
122 else if (idx >= IWL_RATE_MIMO2_6M_PLCP)
123 idx = idx - IWL_RATE_MIMO2_6M_PLCP;
125 idx += IWL_FIRST_OFDM_RATE;
126 /* skip 9M not supported in ht*/
127 if (idx >= IWL_RATE_9M_INDEX)
128 idx += 1;
129 if ((idx >= IWL_FIRST_OFDM_RATE) && (idx <= IWL_LAST_OFDM_RATE))
130 return idx;
132 /* legacy rate format, search for match in table */
133 } else {
134 for (idx = 0; idx < ARRAY_SIZE(iwl_rates); idx++)
135 if (iwl_rates[idx].plcp == (rate_n_flags & 0xFF))
136 return idx;
139 return -1;
141 EXPORT_SYMBOL(iwl_hwrate_to_plcp_idx);
143 u8 iwl_toggle_tx_ant(struct iwl_priv *priv, u8 ant)
145 int i;
146 u8 ind = ant;
147 for (i = 0; i < RATE_ANT_NUM - 1; i++) {
148 ind = (ind + 1) < RATE_ANT_NUM ? ind + 1 : 0;
149 if (priv->hw_params.valid_tx_ant & BIT(ind))
150 return ind;
152 return ant;
154 EXPORT_SYMBOL(iwl_toggle_tx_ant);
156 const u8 iwl_bcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
157 EXPORT_SYMBOL(iwl_bcast_addr);
160 /* This function both allocates and initializes hw and priv. */
161 struct ieee80211_hw *iwl_alloc_all(struct iwl_cfg *cfg,
162 struct ieee80211_ops *hw_ops)
164 struct iwl_priv *priv;
166 /* mac80211 allocates memory for this device instance, including
167 * space for this driver's private structure */
168 struct ieee80211_hw *hw =
169 ieee80211_alloc_hw(sizeof(struct iwl_priv), hw_ops);
170 if (hw == NULL) {
171 printk(KERN_ERR "%s: Can not allocate network device\n",
172 cfg->name);
173 goto out;
176 priv = hw->priv;
177 priv->hw = hw;
179 out:
180 return hw;
182 EXPORT_SYMBOL(iwl_alloc_all);
184 void iwl_hw_detect(struct iwl_priv *priv)
186 priv->hw_rev = _iwl_read32(priv, CSR_HW_REV);
187 priv->hw_wa_rev = _iwl_read32(priv, CSR_HW_REV_WA_REG);
188 pci_read_config_byte(priv->pci_dev, PCI_REVISION_ID, &priv->rev_id);
190 EXPORT_SYMBOL(iwl_hw_detect);
193 * QoS support
195 static void iwl_update_qos(struct iwl_priv *priv)
197 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
198 return;
200 priv->qos_data.def_qos_parm.qos_flags = 0;
202 if (priv->qos_data.qos_active)
203 priv->qos_data.def_qos_parm.qos_flags |=
204 QOS_PARAM_FLG_UPDATE_EDCA_MSK;
206 if (priv->current_ht_config.is_ht)
207 priv->qos_data.def_qos_parm.qos_flags |= QOS_PARAM_FLG_TGN_MSK;
209 IWL_DEBUG_QOS(priv, "send QoS cmd with Qos active=%d FLAGS=0x%X\n",
210 priv->qos_data.qos_active,
211 priv->qos_data.def_qos_parm.qos_flags);
213 iwl_send_cmd_pdu_async(priv, REPLY_QOS_PARAM,
214 sizeof(struct iwl_qosparam_cmd),
215 &priv->qos_data.def_qos_parm, NULL);
218 #define MAX_BIT_RATE_40_MHZ 150 /* Mbps */
219 #define MAX_BIT_RATE_20_MHZ 72 /* Mbps */
220 static void iwlcore_init_ht_hw_capab(const struct iwl_priv *priv,
221 struct ieee80211_sta_ht_cap *ht_info,
222 enum ieee80211_band band)
224 u16 max_bit_rate = 0;
225 u8 rx_chains_num = priv->hw_params.rx_chains_num;
226 u8 tx_chains_num = priv->hw_params.tx_chains_num;
228 ht_info->cap = 0;
229 memset(&ht_info->mcs, 0, sizeof(ht_info->mcs));
231 ht_info->ht_supported = true;
233 if (priv->cfg->ht_greenfield_support)
234 ht_info->cap |= IEEE80211_HT_CAP_GRN_FLD;
235 ht_info->cap |= IEEE80211_HT_CAP_SGI_20;
236 max_bit_rate = MAX_BIT_RATE_20_MHZ;
237 if (priv->hw_params.ht40_channel & BIT(band)) {
238 ht_info->cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
239 ht_info->cap |= IEEE80211_HT_CAP_SGI_40;
240 ht_info->mcs.rx_mask[4] = 0x01;
241 max_bit_rate = MAX_BIT_RATE_40_MHZ;
244 if (priv->cfg->mod_params->amsdu_size_8K)
245 ht_info->cap |= IEEE80211_HT_CAP_MAX_AMSDU;
247 ht_info->ampdu_factor = CFG_HT_RX_AMPDU_FACTOR_DEF;
248 ht_info->ampdu_density = CFG_HT_MPDU_DENSITY_DEF;
250 ht_info->mcs.rx_mask[0] = 0xFF;
251 if (rx_chains_num >= 2)
252 ht_info->mcs.rx_mask[1] = 0xFF;
253 if (rx_chains_num >= 3)
254 ht_info->mcs.rx_mask[2] = 0xFF;
256 /* Highest supported Rx data rate */
257 max_bit_rate *= rx_chains_num;
258 WARN_ON(max_bit_rate & ~IEEE80211_HT_MCS_RX_HIGHEST_MASK);
259 ht_info->mcs.rx_highest = cpu_to_le16(max_bit_rate);
261 /* Tx MCS capabilities */
262 ht_info->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
263 if (tx_chains_num != rx_chains_num) {
264 ht_info->mcs.tx_params |= IEEE80211_HT_MCS_TX_RX_DIFF;
265 ht_info->mcs.tx_params |= ((tx_chains_num - 1) <<
266 IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT);
271 * iwlcore_init_geos - Initialize mac80211's geo/channel info based from eeprom
273 int iwlcore_init_geos(struct iwl_priv *priv)
275 struct iwl_channel_info *ch;
276 struct ieee80211_supported_band *sband;
277 struct ieee80211_channel *channels;
278 struct ieee80211_channel *geo_ch;
279 struct ieee80211_rate *rates;
280 int i = 0;
282 if (priv->bands[IEEE80211_BAND_2GHZ].n_bitrates ||
283 priv->bands[IEEE80211_BAND_5GHZ].n_bitrates) {
284 IWL_DEBUG_INFO(priv, "Geography modes already initialized.\n");
285 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
286 return 0;
289 channels = kzalloc(sizeof(struct ieee80211_channel) *
290 priv->channel_count, GFP_KERNEL);
291 if (!channels)
292 return -ENOMEM;
294 rates = kzalloc((sizeof(struct ieee80211_rate) * IWL_RATE_COUNT_LEGACY),
295 GFP_KERNEL);
296 if (!rates) {
297 kfree(channels);
298 return -ENOMEM;
301 /* 5.2GHz channels start after the 2.4GHz channels */
302 sband = &priv->bands[IEEE80211_BAND_5GHZ];
303 sband->channels = &channels[ARRAY_SIZE(iwl_eeprom_band_1)];
304 /* just OFDM */
305 sband->bitrates = &rates[IWL_FIRST_OFDM_RATE];
306 sband->n_bitrates = IWL_RATE_COUNT_LEGACY - IWL_FIRST_OFDM_RATE;
308 if (priv->cfg->sku & IWL_SKU_N)
309 iwlcore_init_ht_hw_capab(priv, &sband->ht_cap,
310 IEEE80211_BAND_5GHZ);
312 sband = &priv->bands[IEEE80211_BAND_2GHZ];
313 sband->channels = channels;
314 /* OFDM & CCK */
315 sband->bitrates = rates;
316 sband->n_bitrates = IWL_RATE_COUNT_LEGACY;
318 if (priv->cfg->sku & IWL_SKU_N)
319 iwlcore_init_ht_hw_capab(priv, &sband->ht_cap,
320 IEEE80211_BAND_2GHZ);
322 priv->ieee_channels = channels;
323 priv->ieee_rates = rates;
325 for (i = 0; i < priv->channel_count; i++) {
326 ch = &priv->channel_info[i];
328 /* FIXME: might be removed if scan is OK */
329 if (!is_channel_valid(ch))
330 continue;
332 if (is_channel_a_band(ch))
333 sband = &priv->bands[IEEE80211_BAND_5GHZ];
334 else
335 sband = &priv->bands[IEEE80211_BAND_2GHZ];
337 geo_ch = &sband->channels[sband->n_channels++];
339 geo_ch->center_freq =
340 ieee80211_channel_to_frequency(ch->channel);
341 geo_ch->max_power = ch->max_power_avg;
342 geo_ch->max_antenna_gain = 0xff;
343 geo_ch->hw_value = ch->channel;
345 if (is_channel_valid(ch)) {
346 if (!(ch->flags & EEPROM_CHANNEL_IBSS))
347 geo_ch->flags |= IEEE80211_CHAN_NO_IBSS;
349 if (!(ch->flags & EEPROM_CHANNEL_ACTIVE))
350 geo_ch->flags |= IEEE80211_CHAN_PASSIVE_SCAN;
352 if (ch->flags & EEPROM_CHANNEL_RADAR)
353 geo_ch->flags |= IEEE80211_CHAN_RADAR;
355 geo_ch->flags |= ch->ht40_extension_channel;
357 if (ch->max_power_avg > priv->tx_power_device_lmt)
358 priv->tx_power_device_lmt = ch->max_power_avg;
359 } else {
360 geo_ch->flags |= IEEE80211_CHAN_DISABLED;
363 IWL_DEBUG_INFO(priv, "Channel %d Freq=%d[%sGHz] %s flag=0x%X\n",
364 ch->channel, geo_ch->center_freq,
365 is_channel_a_band(ch) ? "5.2" : "2.4",
366 geo_ch->flags & IEEE80211_CHAN_DISABLED ?
367 "restricted" : "valid",
368 geo_ch->flags);
371 if ((priv->bands[IEEE80211_BAND_5GHZ].n_channels == 0) &&
372 priv->cfg->sku & IWL_SKU_A) {
373 IWL_INFO(priv, "Incorrectly detected BG card as ABG. "
374 "Please send your PCI ID 0x%04X:0x%04X to maintainer.\n",
375 priv->pci_dev->device,
376 priv->pci_dev->subsystem_device);
377 priv->cfg->sku &= ~IWL_SKU_A;
380 IWL_INFO(priv, "Tunable channels: %d 802.11bg, %d 802.11a channels\n",
381 priv->bands[IEEE80211_BAND_2GHZ].n_channels,
382 priv->bands[IEEE80211_BAND_5GHZ].n_channels);
384 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
386 return 0;
388 EXPORT_SYMBOL(iwlcore_init_geos);
391 * iwlcore_free_geos - undo allocations in iwlcore_init_geos
393 void iwlcore_free_geos(struct iwl_priv *priv)
395 kfree(priv->ieee_channels);
396 kfree(priv->ieee_rates);
397 clear_bit(STATUS_GEO_CONFIGURED, &priv->status);
399 EXPORT_SYMBOL(iwlcore_free_geos);
402 * iwlcore_rts_tx_cmd_flag: Set rts/cts. 3945 and 4965 only share this
403 * function.
405 void iwlcore_rts_tx_cmd_flag(struct ieee80211_tx_info *info,
406 __le32 *tx_flags)
408 if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS) {
409 *tx_flags |= TX_CMD_FLG_RTS_MSK;
410 *tx_flags &= ~TX_CMD_FLG_CTS_MSK;
411 } else if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
412 *tx_flags &= ~TX_CMD_FLG_RTS_MSK;
413 *tx_flags |= TX_CMD_FLG_CTS_MSK;
416 EXPORT_SYMBOL(iwlcore_rts_tx_cmd_flag);
418 static bool is_single_rx_stream(struct iwl_priv *priv)
420 return priv->current_ht_config.smps == IEEE80211_SMPS_STATIC ||
421 priv->current_ht_config.single_chain_sufficient;
424 static u8 iwl_is_channel_extension(struct iwl_priv *priv,
425 enum ieee80211_band band,
426 u16 channel, u8 extension_chan_offset)
428 const struct iwl_channel_info *ch_info;
430 ch_info = iwl_get_channel_info(priv, band, channel);
431 if (!is_channel_valid(ch_info))
432 return 0;
434 if (extension_chan_offset == IEEE80211_HT_PARAM_CHA_SEC_ABOVE)
435 return !(ch_info->ht40_extension_channel &
436 IEEE80211_CHAN_NO_HT40PLUS);
437 else if (extension_chan_offset == IEEE80211_HT_PARAM_CHA_SEC_BELOW)
438 return !(ch_info->ht40_extension_channel &
439 IEEE80211_CHAN_NO_HT40MINUS);
441 return 0;
444 u8 iwl_is_ht40_tx_allowed(struct iwl_priv *priv,
445 struct ieee80211_sta_ht_cap *sta_ht_inf)
447 struct iwl_ht_config *ht_conf = &priv->current_ht_config;
449 if (!ht_conf->is_ht || !ht_conf->is_40mhz)
450 return 0;
452 /* We do not check for IEEE80211_HT_CAP_SUP_WIDTH_20_40
453 * the bit will not set if it is pure 40MHz case
455 if (sta_ht_inf) {
456 if (!sta_ht_inf->ht_supported)
457 return 0;
459 #ifdef CONFIG_IWLWIFI_DEBUG
460 if (priv->disable_ht40)
461 return 0;
462 #endif
463 return iwl_is_channel_extension(priv, priv->band,
464 le16_to_cpu(priv->staging_rxon.channel),
465 ht_conf->extension_chan_offset);
467 EXPORT_SYMBOL(iwl_is_ht40_tx_allowed);
469 static u16 iwl_adjust_beacon_interval(u16 beacon_val, u16 max_beacon_val)
471 u16 new_val = 0;
472 u16 beacon_factor = 0;
474 beacon_factor = (beacon_val + max_beacon_val) / max_beacon_val;
475 new_val = beacon_val / beacon_factor;
477 if (!new_val)
478 new_val = max_beacon_val;
480 return new_val;
483 void iwl_setup_rxon_timing(struct iwl_priv *priv, struct ieee80211_vif *vif)
485 u64 tsf;
486 s32 interval_tm, rem;
487 unsigned long flags;
488 struct ieee80211_conf *conf = NULL;
489 u16 beacon_int;
491 conf = ieee80211_get_hw_conf(priv->hw);
493 spin_lock_irqsave(&priv->lock, flags);
494 priv->rxon_timing.timestamp = cpu_to_le64(priv->timestamp);
495 priv->rxon_timing.listen_interval = cpu_to_le16(conf->listen_interval);
497 beacon_int = vif->bss_conf.beacon_int;
499 if (vif->type == NL80211_IFTYPE_ADHOC) {
500 /* TODO: we need to get atim_window from upper stack
501 * for now we set to 0 */
502 priv->rxon_timing.atim_window = 0;
503 } else {
504 priv->rxon_timing.atim_window = 0;
507 beacon_int = iwl_adjust_beacon_interval(beacon_int,
508 priv->hw_params.max_beacon_itrvl * 1024);
509 priv->rxon_timing.beacon_interval = cpu_to_le16(beacon_int);
511 tsf = priv->timestamp; /* tsf is modifed by do_div: copy it */
512 interval_tm = beacon_int * 1024;
513 rem = do_div(tsf, interval_tm);
514 priv->rxon_timing.beacon_init_val = cpu_to_le32(interval_tm - rem);
516 spin_unlock_irqrestore(&priv->lock, flags);
517 IWL_DEBUG_ASSOC(priv,
518 "beacon interval %d beacon timer %d beacon tim %d\n",
519 le16_to_cpu(priv->rxon_timing.beacon_interval),
520 le32_to_cpu(priv->rxon_timing.beacon_init_val),
521 le16_to_cpu(priv->rxon_timing.atim_window));
523 EXPORT_SYMBOL(iwl_setup_rxon_timing);
525 void iwl_set_rxon_hwcrypto(struct iwl_priv *priv, int hw_decrypt)
527 struct iwl_rxon_cmd *rxon = &priv->staging_rxon;
529 if (hw_decrypt)
530 rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
531 else
532 rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
535 EXPORT_SYMBOL(iwl_set_rxon_hwcrypto);
538 * iwl_check_rxon_cmd - validate RXON structure is valid
540 * NOTE: This is really only useful during development and can eventually
541 * be #ifdef'd out once the driver is stable and folks aren't actively
542 * making changes
544 int iwl_check_rxon_cmd(struct iwl_priv *priv)
546 int error = 0;
547 int counter = 1;
548 struct iwl_rxon_cmd *rxon = &priv->staging_rxon;
550 if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
551 error |= le32_to_cpu(rxon->flags &
552 (RXON_FLG_TGJ_NARROW_BAND_MSK |
553 RXON_FLG_RADAR_DETECT_MSK));
554 if (error)
555 IWL_WARN(priv, "check 24G fields %d | %d\n",
556 counter++, error);
557 } else {
558 error |= (rxon->flags & RXON_FLG_SHORT_SLOT_MSK) ?
559 0 : le32_to_cpu(RXON_FLG_SHORT_SLOT_MSK);
560 if (error)
561 IWL_WARN(priv, "check 52 fields %d | %d\n",
562 counter++, error);
563 error |= le32_to_cpu(rxon->flags & RXON_FLG_CCK_MSK);
564 if (error)
565 IWL_WARN(priv, "check 52 CCK %d | %d\n",
566 counter++, error);
568 error |= (rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1;
569 if (error)
570 IWL_WARN(priv, "check mac addr %d | %d\n", counter++, error);
572 /* make sure basic rates 6Mbps and 1Mbps are supported */
573 error |= (((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0) &&
574 ((rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0));
575 if (error)
576 IWL_WARN(priv, "check basic rate %d | %d\n", counter++, error);
578 error |= (le16_to_cpu(rxon->assoc_id) > 2007);
579 if (error)
580 IWL_WARN(priv, "check assoc id %d | %d\n", counter++, error);
582 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
583 == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK));
584 if (error)
585 IWL_WARN(priv, "check CCK and short slot %d | %d\n",
586 counter++, error);
588 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
589 == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK));
590 if (error)
591 IWL_WARN(priv, "check CCK & auto detect %d | %d\n",
592 counter++, error);
594 error |= ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
595 RXON_FLG_TGG_PROTECT_MSK)) == RXON_FLG_TGG_PROTECT_MSK);
596 if (error)
597 IWL_WARN(priv, "check TGG and auto detect %d | %d\n",
598 counter++, error);
600 if (error)
601 IWL_WARN(priv, "Tuning to channel %d\n",
602 le16_to_cpu(rxon->channel));
604 if (error) {
605 IWL_ERR(priv, "Not a valid iwl_rxon_assoc_cmd field values\n");
606 return -1;
608 return 0;
610 EXPORT_SYMBOL(iwl_check_rxon_cmd);
613 * iwl_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
614 * @priv: staging_rxon is compared to active_rxon
616 * If the RXON structure is changing enough to require a new tune,
617 * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
618 * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
620 int iwl_full_rxon_required(struct iwl_priv *priv)
623 /* These items are only settable from the full RXON command */
624 if (!(iwl_is_associated(priv)) ||
625 compare_ether_addr(priv->staging_rxon.bssid_addr,
626 priv->active_rxon.bssid_addr) ||
627 compare_ether_addr(priv->staging_rxon.node_addr,
628 priv->active_rxon.node_addr) ||
629 compare_ether_addr(priv->staging_rxon.wlap_bssid_addr,
630 priv->active_rxon.wlap_bssid_addr) ||
631 (priv->staging_rxon.dev_type != priv->active_rxon.dev_type) ||
632 (priv->staging_rxon.channel != priv->active_rxon.channel) ||
633 (priv->staging_rxon.air_propagation !=
634 priv->active_rxon.air_propagation) ||
635 (priv->staging_rxon.ofdm_ht_single_stream_basic_rates !=
636 priv->active_rxon.ofdm_ht_single_stream_basic_rates) ||
637 (priv->staging_rxon.ofdm_ht_dual_stream_basic_rates !=
638 priv->active_rxon.ofdm_ht_dual_stream_basic_rates) ||
639 (priv->staging_rxon.ofdm_ht_triple_stream_basic_rates !=
640 priv->active_rxon.ofdm_ht_triple_stream_basic_rates) ||
641 (priv->staging_rxon.assoc_id != priv->active_rxon.assoc_id))
642 return 1;
644 /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
645 * be updated with the RXON_ASSOC command -- however only some
646 * flag transitions are allowed using RXON_ASSOC */
648 /* Check if we are not switching bands */
649 if ((priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) !=
650 (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK))
651 return 1;
653 /* Check if we are switching association toggle */
654 if ((priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) !=
655 (priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK))
656 return 1;
658 return 0;
660 EXPORT_SYMBOL(iwl_full_rxon_required);
662 u8 iwl_rate_get_lowest_plcp(struct iwl_priv *priv)
665 * Assign the lowest rate -- should really get this from
666 * the beacon skb from mac80211.
668 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK)
669 return IWL_RATE_1M_PLCP;
670 else
671 return IWL_RATE_6M_PLCP;
673 EXPORT_SYMBOL(iwl_rate_get_lowest_plcp);
675 void iwl_set_rxon_ht(struct iwl_priv *priv, struct iwl_ht_config *ht_conf)
677 struct iwl_rxon_cmd *rxon = &priv->staging_rxon;
679 if (!ht_conf->is_ht) {
680 rxon->flags &= ~(RXON_FLG_CHANNEL_MODE_MSK |
681 RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK |
682 RXON_FLG_HT40_PROT_MSK |
683 RXON_FLG_HT_PROT_MSK);
684 return;
687 /* FIXME: if the definition of ht_protection changed, the "translation"
688 * will be needed for rxon->flags
690 rxon->flags |= cpu_to_le32(ht_conf->ht_protection << RXON_FLG_HT_OPERATING_MODE_POS);
692 /* Set up channel bandwidth:
693 * 20 MHz only, 20/40 mixed or pure 40 if ht40 ok */
694 /* clear the HT channel mode before set the mode */
695 rxon->flags &= ~(RXON_FLG_CHANNEL_MODE_MSK |
696 RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK);
697 if (iwl_is_ht40_tx_allowed(priv, NULL)) {
698 /* pure ht40 */
699 if (ht_conf->ht_protection == IEEE80211_HT_OP_MODE_PROTECTION_20MHZ) {
700 rxon->flags |= RXON_FLG_CHANNEL_MODE_PURE_40;
701 /* Note: control channel is opposite of extension channel */
702 switch (ht_conf->extension_chan_offset) {
703 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
704 rxon->flags &= ~RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
705 break;
706 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
707 rxon->flags |= RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
708 break;
710 } else {
711 /* Note: control channel is opposite of extension channel */
712 switch (ht_conf->extension_chan_offset) {
713 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
714 rxon->flags &= ~(RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK);
715 rxon->flags |= RXON_FLG_CHANNEL_MODE_MIXED;
716 break;
717 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
718 rxon->flags |= RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
719 rxon->flags |= RXON_FLG_CHANNEL_MODE_MIXED;
720 break;
721 case IEEE80211_HT_PARAM_CHA_SEC_NONE:
722 default:
723 /* channel location only valid if in Mixed mode */
724 IWL_ERR(priv, "invalid extension channel offset\n");
725 break;
728 } else {
729 rxon->flags |= RXON_FLG_CHANNEL_MODE_LEGACY;
732 if (priv->cfg->ops->hcmd->set_rxon_chain)
733 priv->cfg->ops->hcmd->set_rxon_chain(priv);
735 IWL_DEBUG_ASSOC(priv, "rxon flags 0x%X operation mode :0x%X "
736 "extension channel offset 0x%x\n",
737 le32_to_cpu(rxon->flags), ht_conf->ht_protection,
738 ht_conf->extension_chan_offset);
739 return;
741 EXPORT_SYMBOL(iwl_set_rxon_ht);
743 #define IWL_NUM_RX_CHAINS_MULTIPLE 3
744 #define IWL_NUM_RX_CHAINS_SINGLE 2
745 #define IWL_NUM_IDLE_CHAINS_DUAL 2
746 #define IWL_NUM_IDLE_CHAINS_SINGLE 1
749 * Determine how many receiver/antenna chains to use.
751 * More provides better reception via diversity. Fewer saves power
752 * at the expense of throughput, but only when not in powersave to
753 * start with.
755 * MIMO (dual stream) requires at least 2, but works better with 3.
756 * This does not determine *which* chains to use, just how many.
758 static int iwl_get_active_rx_chain_count(struct iwl_priv *priv)
760 /* # of Rx chains to use when expecting MIMO. */
761 if (is_single_rx_stream(priv))
762 return IWL_NUM_RX_CHAINS_SINGLE;
763 else
764 return IWL_NUM_RX_CHAINS_MULTIPLE;
768 * When we are in power saving mode, unless device support spatial
769 * multiplexing power save, use the active count for rx chain count.
771 static int iwl_get_idle_rx_chain_count(struct iwl_priv *priv, int active_cnt)
773 /* # Rx chains when idling, depending on SMPS mode */
774 switch (priv->current_ht_config.smps) {
775 case IEEE80211_SMPS_STATIC:
776 case IEEE80211_SMPS_DYNAMIC:
777 return IWL_NUM_IDLE_CHAINS_SINGLE;
778 case IEEE80211_SMPS_OFF:
779 return active_cnt;
780 default:
781 WARN(1, "invalid SMPS mode %d",
782 priv->current_ht_config.smps);
783 return active_cnt;
787 /* up to 4 chains */
788 static u8 iwl_count_chain_bitmap(u32 chain_bitmap)
790 u8 res;
791 res = (chain_bitmap & BIT(0)) >> 0;
792 res += (chain_bitmap & BIT(1)) >> 1;
793 res += (chain_bitmap & BIT(2)) >> 2;
794 res += (chain_bitmap & BIT(3)) >> 3;
795 return res;
799 * iwl_set_rxon_chain - Set up Rx chain usage in "staging" RXON image
801 * Selects how many and which Rx receivers/antennas/chains to use.
802 * This should not be used for scan command ... it puts data in wrong place.
804 void iwl_set_rxon_chain(struct iwl_priv *priv)
806 bool is_single = is_single_rx_stream(priv);
807 bool is_cam = !test_bit(STATUS_POWER_PMI, &priv->status);
808 u8 idle_rx_cnt, active_rx_cnt, valid_rx_cnt;
809 u32 active_chains;
810 u16 rx_chain;
812 /* Tell uCode which antennas are actually connected.
813 * Before first association, we assume all antennas are connected.
814 * Just after first association, iwl_chain_noise_calibration()
815 * checks which antennas actually *are* connected. */
816 if (priv->chain_noise_data.active_chains)
817 active_chains = priv->chain_noise_data.active_chains;
818 else
819 active_chains = priv->hw_params.valid_rx_ant;
821 rx_chain = active_chains << RXON_RX_CHAIN_VALID_POS;
823 /* How many receivers should we use? */
824 active_rx_cnt = iwl_get_active_rx_chain_count(priv);
825 idle_rx_cnt = iwl_get_idle_rx_chain_count(priv, active_rx_cnt);
828 /* correct rx chain count according hw settings
829 * and chain noise calibration
831 valid_rx_cnt = iwl_count_chain_bitmap(active_chains);
832 if (valid_rx_cnt < active_rx_cnt)
833 active_rx_cnt = valid_rx_cnt;
835 if (valid_rx_cnt < idle_rx_cnt)
836 idle_rx_cnt = valid_rx_cnt;
838 rx_chain |= active_rx_cnt << RXON_RX_CHAIN_MIMO_CNT_POS;
839 rx_chain |= idle_rx_cnt << RXON_RX_CHAIN_CNT_POS;
841 priv->staging_rxon.rx_chain = cpu_to_le16(rx_chain);
843 if (!is_single && (active_rx_cnt >= IWL_NUM_RX_CHAINS_SINGLE) && is_cam)
844 priv->staging_rxon.rx_chain |= RXON_RX_CHAIN_MIMO_FORCE_MSK;
845 else
846 priv->staging_rxon.rx_chain &= ~RXON_RX_CHAIN_MIMO_FORCE_MSK;
848 IWL_DEBUG_ASSOC(priv, "rx_chain=0x%X active=%d idle=%d\n",
849 priv->staging_rxon.rx_chain,
850 active_rx_cnt, idle_rx_cnt);
852 WARN_ON(active_rx_cnt == 0 || idle_rx_cnt == 0 ||
853 active_rx_cnt < idle_rx_cnt);
855 EXPORT_SYMBOL(iwl_set_rxon_chain);
858 * iwl_set_rxon_channel - Set the phymode and channel values in staging RXON
859 * @phymode: MODE_IEEE80211A sets to 5.2GHz; all else set to 2.4GHz
860 * @channel: Any channel valid for the requested phymode
862 * In addition to setting the staging RXON, priv->phymode is also set.
864 * NOTE: Does not commit to the hardware; it sets appropriate bit fields
865 * in the staging RXON flag structure based on the phymode
867 int iwl_set_rxon_channel(struct iwl_priv *priv, struct ieee80211_channel *ch)
869 enum ieee80211_band band = ch->band;
870 u16 channel = ieee80211_frequency_to_channel(ch->center_freq);
872 if (!iwl_get_channel_info(priv, band, channel)) {
873 IWL_DEBUG_INFO(priv, "Could not set channel to %d [%d]\n",
874 channel, band);
875 return -EINVAL;
878 if ((le16_to_cpu(priv->staging_rxon.channel) == channel) &&
879 (priv->band == band))
880 return 0;
882 priv->staging_rxon.channel = cpu_to_le16(channel);
883 if (band == IEEE80211_BAND_5GHZ)
884 priv->staging_rxon.flags &= ~RXON_FLG_BAND_24G_MSK;
885 else
886 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
888 priv->band = band;
890 IWL_DEBUG_INFO(priv, "Staging channel set to %d [%d]\n", channel, band);
892 return 0;
894 EXPORT_SYMBOL(iwl_set_rxon_channel);
896 static void iwl_set_flags_for_band(struct iwl_priv *priv,
897 enum ieee80211_band band,
898 struct ieee80211_vif *vif)
900 if (band == IEEE80211_BAND_5GHZ) {
901 priv->staging_rxon.flags &=
902 ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
903 | RXON_FLG_CCK_MSK);
904 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
905 } else {
906 /* Copied from iwl_post_associate() */
907 if (vif && vif->bss_conf.assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
908 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
909 else
910 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
912 if (vif && vif->type == NL80211_IFTYPE_ADHOC)
913 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
915 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
916 priv->staging_rxon.flags |= RXON_FLG_AUTO_DETECT_MSK;
917 priv->staging_rxon.flags &= ~RXON_FLG_CCK_MSK;
922 * initialize rxon structure with default values from eeprom
924 void iwl_connection_init_rx_config(struct iwl_priv *priv,
925 struct ieee80211_vif *vif)
927 const struct iwl_channel_info *ch_info;
928 enum nl80211_iftype type = NL80211_IFTYPE_STATION;
930 if (vif)
931 type = vif->type;
933 memset(&priv->staging_rxon, 0, sizeof(priv->staging_rxon));
935 switch (type) {
936 case NL80211_IFTYPE_AP:
937 priv->staging_rxon.dev_type = RXON_DEV_TYPE_AP;
938 break;
940 case NL80211_IFTYPE_STATION:
941 priv->staging_rxon.dev_type = RXON_DEV_TYPE_ESS;
942 priv->staging_rxon.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
943 break;
945 case NL80211_IFTYPE_ADHOC:
946 priv->staging_rxon.dev_type = RXON_DEV_TYPE_IBSS;
947 priv->staging_rxon.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
948 priv->staging_rxon.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
949 RXON_FILTER_ACCEPT_GRP_MSK;
950 break;
952 default:
953 IWL_ERR(priv, "Unsupported interface type %d\n", type);
954 break;
957 #if 0
958 /* TODO: Figure out when short_preamble would be set and cache from
959 * that */
960 if (!hw_to_local(priv->hw)->short_preamble)
961 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
962 else
963 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
964 #endif
966 ch_info = iwl_get_channel_info(priv, priv->band,
967 le16_to_cpu(priv->active_rxon.channel));
969 if (!ch_info)
970 ch_info = &priv->channel_info[0];
972 priv->staging_rxon.channel = cpu_to_le16(ch_info->channel);
973 priv->band = ch_info->band;
975 iwl_set_flags_for_band(priv, priv->band, vif);
977 priv->staging_rxon.ofdm_basic_rates =
978 (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
979 priv->staging_rxon.cck_basic_rates =
980 (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
982 /* clear both MIX and PURE40 mode flag */
983 priv->staging_rxon.flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED |
984 RXON_FLG_CHANNEL_MODE_PURE_40);
985 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
986 memcpy(priv->staging_rxon.wlap_bssid_addr, priv->mac_addr, ETH_ALEN);
987 priv->staging_rxon.ofdm_ht_single_stream_basic_rates = 0xff;
988 priv->staging_rxon.ofdm_ht_dual_stream_basic_rates = 0xff;
989 priv->staging_rxon.ofdm_ht_triple_stream_basic_rates = 0xff;
991 EXPORT_SYMBOL(iwl_connection_init_rx_config);
993 static void iwl_set_rate(struct iwl_priv *priv)
995 const struct ieee80211_supported_band *hw = NULL;
996 struct ieee80211_rate *rate;
997 int i;
999 hw = iwl_get_hw_mode(priv, priv->band);
1000 if (!hw) {
1001 IWL_ERR(priv, "Failed to set rate: unable to get hw mode\n");
1002 return;
1005 priv->active_rate = 0;
1007 for (i = 0; i < hw->n_bitrates; i++) {
1008 rate = &(hw->bitrates[i]);
1009 if (rate->hw_value < IWL_RATE_COUNT_LEGACY)
1010 priv->active_rate |= (1 << rate->hw_value);
1013 IWL_DEBUG_RATE(priv, "Set active_rate = %0x\n", priv->active_rate);
1015 priv->staging_rxon.cck_basic_rates =
1016 (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
1018 priv->staging_rxon.ofdm_basic_rates =
1019 (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
1022 void iwl_rx_csa(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb)
1024 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1025 struct iwl_rxon_cmd *rxon = (void *)&priv->active_rxon;
1026 struct iwl_csa_notification *csa = &(pkt->u.csa_notif);
1028 if (priv->switch_rxon.switch_in_progress) {
1029 if (!le32_to_cpu(csa->status) &&
1030 (csa->channel == priv->switch_rxon.channel)) {
1031 rxon->channel = csa->channel;
1032 priv->staging_rxon.channel = csa->channel;
1033 IWL_DEBUG_11H(priv, "CSA notif: channel %d\n",
1034 le16_to_cpu(csa->channel));
1035 } else
1036 IWL_ERR(priv, "CSA notif (fail) : channel %d\n",
1037 le16_to_cpu(csa->channel));
1039 priv->switch_rxon.switch_in_progress = false;
1042 EXPORT_SYMBOL(iwl_rx_csa);
1044 #ifdef CONFIG_IWLWIFI_DEBUG
1045 void iwl_print_rx_config_cmd(struct iwl_priv *priv)
1047 struct iwl_rxon_cmd *rxon = &priv->staging_rxon;
1049 IWL_DEBUG_RADIO(priv, "RX CONFIG:\n");
1050 iwl_print_hex_dump(priv, IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
1051 IWL_DEBUG_RADIO(priv, "u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
1052 IWL_DEBUG_RADIO(priv, "u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
1053 IWL_DEBUG_RADIO(priv, "u32 filter_flags: 0x%08x\n",
1054 le32_to_cpu(rxon->filter_flags));
1055 IWL_DEBUG_RADIO(priv, "u8 dev_type: 0x%x\n", rxon->dev_type);
1056 IWL_DEBUG_RADIO(priv, "u8 ofdm_basic_rates: 0x%02x\n",
1057 rxon->ofdm_basic_rates);
1058 IWL_DEBUG_RADIO(priv, "u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
1059 IWL_DEBUG_RADIO(priv, "u8[6] node_addr: %pM\n", rxon->node_addr);
1060 IWL_DEBUG_RADIO(priv, "u8[6] bssid_addr: %pM\n", rxon->bssid_addr);
1061 IWL_DEBUG_RADIO(priv, "u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
1063 EXPORT_SYMBOL(iwl_print_rx_config_cmd);
1064 #endif
1066 * iwl_irq_handle_error - called for HW or SW error interrupt from card
1068 void iwl_irq_handle_error(struct iwl_priv *priv)
1070 /* Set the FW error flag -- cleared on iwl_down */
1071 set_bit(STATUS_FW_ERROR, &priv->status);
1073 /* Cancel currently queued command. */
1074 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
1076 IWL_ERR(priv, "Loaded firmware version: %s\n",
1077 priv->hw->wiphy->fw_version);
1079 priv->cfg->ops->lib->dump_nic_error_log(priv);
1080 if (priv->cfg->ops->lib->dump_csr)
1081 priv->cfg->ops->lib->dump_csr(priv);
1082 if (priv->cfg->ops->lib->dump_fh)
1083 priv->cfg->ops->lib->dump_fh(priv, NULL, false);
1084 priv->cfg->ops->lib->dump_nic_event_log(priv, false, NULL, false);
1085 #ifdef CONFIG_IWLWIFI_DEBUG
1086 if (iwl_get_debug_level(priv) & IWL_DL_FW_ERRORS)
1087 iwl_print_rx_config_cmd(priv);
1088 #endif
1090 wake_up_interruptible(&priv->wait_command_queue);
1092 /* Keep the restart process from trying to send host
1093 * commands by clearing the INIT status bit */
1094 clear_bit(STATUS_READY, &priv->status);
1096 if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
1097 IWL_DEBUG(priv, IWL_DL_FW_ERRORS,
1098 "Restarting adapter due to uCode error.\n");
1100 if (priv->cfg->mod_params->restart_fw)
1101 queue_work(priv->workqueue, &priv->restart);
1104 EXPORT_SYMBOL(iwl_irq_handle_error);
1106 static int iwl_apm_stop_master(struct iwl_priv *priv)
1108 int ret = 0;
1110 /* stop device's busmaster DMA activity */
1111 iwl_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_STOP_MASTER);
1113 ret = iwl_poll_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_MASTER_DISABLED,
1114 CSR_RESET_REG_FLAG_MASTER_DISABLED, 100);
1115 if (ret)
1116 IWL_WARN(priv, "Master Disable Timed Out, 100 usec\n");
1118 IWL_DEBUG_INFO(priv, "stop master\n");
1120 return ret;
1123 void iwl_apm_stop(struct iwl_priv *priv)
1125 IWL_DEBUG_INFO(priv, "Stop card, put in low power state\n");
1127 /* Stop device's DMA activity */
1128 iwl_apm_stop_master(priv);
1130 /* Reset the entire device */
1131 iwl_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
1133 udelay(10);
1136 * Clear "initialization complete" bit to move adapter from
1137 * D0A* (powered-up Active) --> D0U* (Uninitialized) state.
1139 iwl_clear_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
1141 EXPORT_SYMBOL(iwl_apm_stop);
1145 * Start up NIC's basic functionality after it has been reset
1146 * (e.g. after platform boot, or shutdown via iwl_apm_stop())
1147 * NOTE: This does not load uCode nor start the embedded processor
1149 int iwl_apm_init(struct iwl_priv *priv)
1151 int ret = 0;
1152 u16 lctl;
1154 IWL_DEBUG_INFO(priv, "Init card's basic functions\n");
1157 * Use "set_bit" below rather than "write", to preserve any hardware
1158 * bits already set by default after reset.
1161 /* Disable L0S exit timer (platform NMI Work/Around) */
1162 iwl_set_bit(priv, CSR_GIO_CHICKEN_BITS,
1163 CSR_GIO_CHICKEN_BITS_REG_BIT_DIS_L0S_EXIT_TIMER);
1166 * Disable L0s without affecting L1;
1167 * don't wait for ICH L0s (ICH bug W/A)
1169 iwl_set_bit(priv, CSR_GIO_CHICKEN_BITS,
1170 CSR_GIO_CHICKEN_BITS_REG_BIT_L1A_NO_L0S_RX);
1172 /* Set FH wait threshold to maximum (HW error during stress W/A) */
1173 iwl_set_bit(priv, CSR_DBG_HPET_MEM_REG, CSR_DBG_HPET_MEM_REG_VAL);
1176 * Enable HAP INTA (interrupt from management bus) to
1177 * wake device's PCI Express link L1a -> L0s
1178 * NOTE: This is no-op for 3945 (non-existant bit)
1180 iwl_set_bit(priv, CSR_HW_IF_CONFIG_REG,
1181 CSR_HW_IF_CONFIG_REG_BIT_HAP_WAKE_L1A);
1184 * HW bug W/A for instability in PCIe bus L0->L0S->L1 transition.
1185 * Check if BIOS (or OS) enabled L1-ASPM on this device.
1186 * If so (likely), disable L0S, so device moves directly L0->L1;
1187 * costs negligible amount of power savings.
1188 * If not (unlikely), enable L0S, so there is at least some
1189 * power savings, even without L1.
1191 if (priv->cfg->set_l0s) {
1192 lctl = iwl_pcie_link_ctl(priv);
1193 if ((lctl & PCI_CFG_LINK_CTRL_VAL_L1_EN) ==
1194 PCI_CFG_LINK_CTRL_VAL_L1_EN) {
1195 /* L1-ASPM enabled; disable(!) L0S */
1196 iwl_set_bit(priv, CSR_GIO_REG,
1197 CSR_GIO_REG_VAL_L0S_ENABLED);
1198 IWL_DEBUG_POWER(priv, "L1 Enabled; Disabling L0S\n");
1199 } else {
1200 /* L1-ASPM disabled; enable(!) L0S */
1201 iwl_clear_bit(priv, CSR_GIO_REG,
1202 CSR_GIO_REG_VAL_L0S_ENABLED);
1203 IWL_DEBUG_POWER(priv, "L1 Disabled; Enabling L0S\n");
1207 /* Configure analog phase-lock-loop before activating to D0A */
1208 if (priv->cfg->pll_cfg_val)
1209 iwl_set_bit(priv, CSR_ANA_PLL_CFG, priv->cfg->pll_cfg_val);
1212 * Set "initialization complete" bit to move adapter from
1213 * D0U* --> D0A* (powered-up active) state.
1215 iwl_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
1218 * Wait for clock stabilization; once stabilized, access to
1219 * device-internal resources is supported, e.g. iwl_write_prph()
1220 * and accesses to uCode SRAM.
1222 ret = iwl_poll_bit(priv, CSR_GP_CNTRL,
1223 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
1224 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25000);
1225 if (ret < 0) {
1226 IWL_DEBUG_INFO(priv, "Failed to init the card\n");
1227 goto out;
1231 * Enable DMA and BSM (if used) clocks, wait for them to stabilize.
1232 * BSM (Boostrap State Machine) is only in 3945 and 4965;
1233 * later devices (i.e. 5000 and later) have non-volatile SRAM,
1234 * and don't need BSM to restore data after power-saving sleep.
1236 * Write to "CLK_EN_REG"; "1" bits enable clocks, while "0" bits
1237 * do not disable clocks. This preserves any hardware bits already
1238 * set by default in "CLK_CTRL_REG" after reset.
1240 if (priv->cfg->use_bsm)
1241 iwl_write_prph(priv, APMG_CLK_EN_REG,
1242 APMG_CLK_VAL_DMA_CLK_RQT | APMG_CLK_VAL_BSM_CLK_RQT);
1243 else
1244 iwl_write_prph(priv, APMG_CLK_EN_REG,
1245 APMG_CLK_VAL_DMA_CLK_RQT);
1246 udelay(20);
1248 /* Disable L1-Active */
1249 iwl_set_bits_prph(priv, APMG_PCIDEV_STT_REG,
1250 APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
1252 out:
1253 return ret;
1255 EXPORT_SYMBOL(iwl_apm_init);
1259 void iwl_configure_filter(struct ieee80211_hw *hw,
1260 unsigned int changed_flags,
1261 unsigned int *total_flags,
1262 u64 multicast)
1264 struct iwl_priv *priv = hw->priv;
1265 __le32 filter_or = 0, filter_nand = 0;
1267 #define CHK(test, flag) do { \
1268 if (*total_flags & (test)) \
1269 filter_or |= (flag); \
1270 else \
1271 filter_nand |= (flag); \
1272 } while (0)
1274 IWL_DEBUG_MAC80211(priv, "Enter: changed: 0x%x, total: 0x%x\n",
1275 changed_flags, *total_flags);
1277 CHK(FIF_OTHER_BSS | FIF_PROMISC_IN_BSS, RXON_FILTER_PROMISC_MSK);
1278 CHK(FIF_ALLMULTI, RXON_FILTER_ACCEPT_GRP_MSK);
1279 CHK(FIF_CONTROL, RXON_FILTER_CTL2HOST_MSK);
1280 CHK(FIF_BCN_PRBRESP_PROMISC, RXON_FILTER_BCON_AWARE_MSK);
1282 #undef CHK
1284 mutex_lock(&priv->mutex);
1286 priv->staging_rxon.filter_flags &= ~filter_nand;
1287 priv->staging_rxon.filter_flags |= filter_or;
1289 iwlcore_commit_rxon(priv);
1291 mutex_unlock(&priv->mutex);
1293 *total_flags &= FIF_OTHER_BSS | FIF_ALLMULTI | FIF_PROMISC_IN_BSS |
1294 FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL;
1296 EXPORT_SYMBOL(iwl_configure_filter);
1298 int iwl_set_hw_params(struct iwl_priv *priv)
1300 priv->hw_params.max_rxq_size = RX_QUEUE_SIZE;
1301 priv->hw_params.max_rxq_log = RX_QUEUE_SIZE_LOG;
1302 if (priv->cfg->mod_params->amsdu_size_8K)
1303 priv->hw_params.rx_page_order = get_order(IWL_RX_BUF_SIZE_8K);
1304 else
1305 priv->hw_params.rx_page_order = get_order(IWL_RX_BUF_SIZE_4K);
1307 priv->hw_params.max_beacon_itrvl = IWL_MAX_UCODE_BEACON_INTERVAL;
1309 if (priv->cfg->mod_params->disable_11n)
1310 priv->cfg->sku &= ~IWL_SKU_N;
1312 /* Device-specific setup */
1313 return priv->cfg->ops->lib->set_hw_params(priv);
1315 EXPORT_SYMBOL(iwl_set_hw_params);
1317 int iwl_set_tx_power(struct iwl_priv *priv, s8 tx_power, bool force)
1319 int ret = 0;
1320 s8 prev_tx_power = priv->tx_power_user_lmt;
1322 if (tx_power < IWLAGN_TX_POWER_TARGET_POWER_MIN) {
1323 IWL_WARN(priv,
1324 "Requested user TXPOWER %d below lower limit %d.\n",
1325 tx_power,
1326 IWLAGN_TX_POWER_TARGET_POWER_MIN);
1327 return -EINVAL;
1330 if (tx_power > priv->tx_power_device_lmt) {
1331 IWL_WARN(priv,
1332 "Requested user TXPOWER %d above upper limit %d.\n",
1333 tx_power, priv->tx_power_device_lmt);
1334 return -EINVAL;
1337 if (priv->tx_power_user_lmt != tx_power)
1338 force = true;
1340 /* if nic is not up don't send command */
1341 if (iwl_is_ready_rf(priv)) {
1342 priv->tx_power_user_lmt = tx_power;
1343 if (force && priv->cfg->ops->lib->send_tx_power)
1344 ret = priv->cfg->ops->lib->send_tx_power(priv);
1345 else if (!priv->cfg->ops->lib->send_tx_power)
1346 ret = -EOPNOTSUPP;
1348 * if fail to set tx_power, restore the orig. tx power
1350 if (ret)
1351 priv->tx_power_user_lmt = prev_tx_power;
1355 * Even this is an async host command, the command
1356 * will always report success from uCode
1357 * So once driver can placing the command into the queue
1358 * successfully, driver can use priv->tx_power_user_lmt
1359 * to reflect the current tx power
1361 return ret;
1363 EXPORT_SYMBOL(iwl_set_tx_power);
1365 irqreturn_t iwl_isr_legacy(int irq, void *data)
1367 struct iwl_priv *priv = data;
1368 u32 inta, inta_mask;
1369 u32 inta_fh;
1370 unsigned long flags;
1371 if (!priv)
1372 return IRQ_NONE;
1374 spin_lock_irqsave(&priv->lock, flags);
1376 /* Disable (but don't clear!) interrupts here to avoid
1377 * back-to-back ISRs and sporadic interrupts from our NIC.
1378 * If we have something to service, the tasklet will re-enable ints.
1379 * If we *don't* have something, we'll re-enable before leaving here. */
1380 inta_mask = iwl_read32(priv, CSR_INT_MASK); /* just for debug */
1381 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
1383 /* Discover which interrupts are active/pending */
1384 inta = iwl_read32(priv, CSR_INT);
1385 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
1387 /* Ignore interrupt if there's nothing in NIC to service.
1388 * This may be due to IRQ shared with another device,
1389 * or due to sporadic interrupts thrown from our NIC. */
1390 if (!inta && !inta_fh) {
1391 IWL_DEBUG_ISR(priv, "Ignore interrupt, inta == 0, inta_fh == 0\n");
1392 goto none;
1395 if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
1396 /* Hardware disappeared. It might have already raised
1397 * an interrupt */
1398 IWL_WARN(priv, "HARDWARE GONE?? INTA == 0x%08x\n", inta);
1399 goto unplugged;
1402 IWL_DEBUG_ISR(priv, "ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
1403 inta, inta_mask, inta_fh);
1405 inta &= ~CSR_INT_BIT_SCD;
1407 /* iwl_irq_tasklet() will service interrupts and re-enable them */
1408 if (likely(inta || inta_fh))
1409 tasklet_schedule(&priv->irq_tasklet);
1411 unplugged:
1412 spin_unlock_irqrestore(&priv->lock, flags);
1413 return IRQ_HANDLED;
1415 none:
1416 /* re-enable interrupts here since we don't have anything to service. */
1417 /* only Re-enable if diabled by irq */
1418 if (test_bit(STATUS_INT_ENABLED, &priv->status))
1419 iwl_enable_interrupts(priv);
1420 spin_unlock_irqrestore(&priv->lock, flags);
1421 return IRQ_NONE;
1423 EXPORT_SYMBOL(iwl_isr_legacy);
1425 void iwl_send_bt_config(struct iwl_priv *priv)
1427 struct iwl_bt_cmd bt_cmd = {
1428 .lead_time = BT_LEAD_TIME_DEF,
1429 .max_kill = BT_MAX_KILL_DEF,
1430 .kill_ack_mask = 0,
1431 .kill_cts_mask = 0,
1434 if (!bt_coex_active)
1435 bt_cmd.flags = BT_COEX_DISABLE;
1436 else
1437 bt_cmd.flags = BT_COEX_ENABLE;
1439 IWL_DEBUG_INFO(priv, "BT coex %s\n",
1440 (bt_cmd.flags == BT_COEX_DISABLE) ? "disable" : "active");
1442 if (iwl_send_cmd_pdu(priv, REPLY_BT_CONFIG,
1443 sizeof(struct iwl_bt_cmd), &bt_cmd))
1444 IWL_ERR(priv, "failed to send BT Coex Config\n");
1446 EXPORT_SYMBOL(iwl_send_bt_config);
1448 int iwl_send_statistics_request(struct iwl_priv *priv, u8 flags, bool clear)
1450 struct iwl_statistics_cmd statistics_cmd = {
1451 .configuration_flags =
1452 clear ? IWL_STATS_CONF_CLEAR_STATS : 0,
1455 if (flags & CMD_ASYNC)
1456 return iwl_send_cmd_pdu_async(priv, REPLY_STATISTICS_CMD,
1457 sizeof(struct iwl_statistics_cmd),
1458 &statistics_cmd, NULL);
1459 else
1460 return iwl_send_cmd_pdu(priv, REPLY_STATISTICS_CMD,
1461 sizeof(struct iwl_statistics_cmd),
1462 &statistics_cmd);
1464 EXPORT_SYMBOL(iwl_send_statistics_request);
1467 * iwl_verify_inst_sparse - verify runtime uCode image in card vs. host,
1468 * using sample data 100 bytes apart. If these sample points are good,
1469 * it's a pretty good bet that everything between them is good, too.
1471 static int iwlcore_verify_inst_sparse(struct iwl_priv *priv, __le32 *image, u32 len)
1473 u32 val;
1474 int ret = 0;
1475 u32 errcnt = 0;
1476 u32 i;
1478 IWL_DEBUG_INFO(priv, "ucode inst image size is %u\n", len);
1480 for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
1481 /* read data comes through single port, auto-incr addr */
1482 /* NOTE: Use the debugless read so we don't flood kernel log
1483 * if IWL_DL_IO is set */
1484 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
1485 i + IWL49_RTC_INST_LOWER_BOUND);
1486 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
1487 if (val != le32_to_cpu(*image)) {
1488 ret = -EIO;
1489 errcnt++;
1490 if (errcnt >= 3)
1491 break;
1495 return ret;
1499 * iwlcore_verify_inst_full - verify runtime uCode image in card vs. host,
1500 * looking at all data.
1502 static int iwl_verify_inst_full(struct iwl_priv *priv, __le32 *image,
1503 u32 len)
1505 u32 val;
1506 u32 save_len = len;
1507 int ret = 0;
1508 u32 errcnt;
1510 IWL_DEBUG_INFO(priv, "ucode inst image size is %u\n", len);
1512 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
1513 IWL49_RTC_INST_LOWER_BOUND);
1515 errcnt = 0;
1516 for (; len > 0; len -= sizeof(u32), image++) {
1517 /* read data comes through single port, auto-incr addr */
1518 /* NOTE: Use the debugless read so we don't flood kernel log
1519 * if IWL_DL_IO is set */
1520 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
1521 if (val != le32_to_cpu(*image)) {
1522 IWL_ERR(priv, "uCode INST section is invalid at "
1523 "offset 0x%x, is 0x%x, s/b 0x%x\n",
1524 save_len - len, val, le32_to_cpu(*image));
1525 ret = -EIO;
1526 errcnt++;
1527 if (errcnt >= 20)
1528 break;
1532 if (!errcnt)
1533 IWL_DEBUG_INFO(priv,
1534 "ucode image in INSTRUCTION memory is good\n");
1536 return ret;
1540 * iwl_verify_ucode - determine which instruction image is in SRAM,
1541 * and verify its contents
1543 int iwl_verify_ucode(struct iwl_priv *priv)
1545 __le32 *image;
1546 u32 len;
1547 int ret;
1549 /* Try bootstrap */
1550 image = (__le32 *)priv->ucode_boot.v_addr;
1551 len = priv->ucode_boot.len;
1552 ret = iwlcore_verify_inst_sparse(priv, image, len);
1553 if (!ret) {
1554 IWL_DEBUG_INFO(priv, "Bootstrap uCode is good in inst SRAM\n");
1555 return 0;
1558 /* Try initialize */
1559 image = (__le32 *)priv->ucode_init.v_addr;
1560 len = priv->ucode_init.len;
1561 ret = iwlcore_verify_inst_sparse(priv, image, len);
1562 if (!ret) {
1563 IWL_DEBUG_INFO(priv, "Initialize uCode is good in inst SRAM\n");
1564 return 0;
1567 /* Try runtime/protocol */
1568 image = (__le32 *)priv->ucode_code.v_addr;
1569 len = priv->ucode_code.len;
1570 ret = iwlcore_verify_inst_sparse(priv, image, len);
1571 if (!ret) {
1572 IWL_DEBUG_INFO(priv, "Runtime uCode is good in inst SRAM\n");
1573 return 0;
1576 IWL_ERR(priv, "NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
1578 /* Since nothing seems to match, show first several data entries in
1579 * instruction SRAM, so maybe visual inspection will give a clue.
1580 * Selection of bootstrap image (vs. other images) is arbitrary. */
1581 image = (__le32 *)priv->ucode_boot.v_addr;
1582 len = priv->ucode_boot.len;
1583 ret = iwl_verify_inst_full(priv, image, len);
1585 return ret;
1587 EXPORT_SYMBOL(iwl_verify_ucode);
1590 void iwl_rf_kill_ct_config(struct iwl_priv *priv)
1592 struct iwl_ct_kill_config cmd;
1593 struct iwl_ct_kill_throttling_config adv_cmd;
1594 unsigned long flags;
1595 int ret = 0;
1597 spin_lock_irqsave(&priv->lock, flags);
1598 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
1599 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
1600 spin_unlock_irqrestore(&priv->lock, flags);
1601 priv->thermal_throttle.ct_kill_toggle = false;
1603 if (priv->cfg->support_ct_kill_exit) {
1604 adv_cmd.critical_temperature_enter =
1605 cpu_to_le32(priv->hw_params.ct_kill_threshold);
1606 adv_cmd.critical_temperature_exit =
1607 cpu_to_le32(priv->hw_params.ct_kill_exit_threshold);
1609 ret = iwl_send_cmd_pdu(priv, REPLY_CT_KILL_CONFIG_CMD,
1610 sizeof(adv_cmd), &adv_cmd);
1611 if (ret)
1612 IWL_ERR(priv, "REPLY_CT_KILL_CONFIG_CMD failed\n");
1613 else
1614 IWL_DEBUG_INFO(priv, "REPLY_CT_KILL_CONFIG_CMD "
1615 "succeeded, "
1616 "critical temperature enter is %d,"
1617 "exit is %d\n",
1618 priv->hw_params.ct_kill_threshold,
1619 priv->hw_params.ct_kill_exit_threshold);
1620 } else {
1621 cmd.critical_temperature_R =
1622 cpu_to_le32(priv->hw_params.ct_kill_threshold);
1624 ret = iwl_send_cmd_pdu(priv, REPLY_CT_KILL_CONFIG_CMD,
1625 sizeof(cmd), &cmd);
1626 if (ret)
1627 IWL_ERR(priv, "REPLY_CT_KILL_CONFIG_CMD failed\n");
1628 else
1629 IWL_DEBUG_INFO(priv, "REPLY_CT_KILL_CONFIG_CMD "
1630 "succeeded, "
1631 "critical temperature is %d\n",
1632 priv->hw_params.ct_kill_threshold);
1635 EXPORT_SYMBOL(iwl_rf_kill_ct_config);
1639 * CARD_STATE_CMD
1641 * Use: Sets the device's internal card state to enable, disable, or halt
1643 * When in the 'enable' state the card operates as normal.
1644 * When in the 'disable' state, the card enters into a low power mode.
1645 * When in the 'halt' state, the card is shut down and must be fully
1646 * restarted to come back on.
1648 int iwl_send_card_state(struct iwl_priv *priv, u32 flags, u8 meta_flag)
1650 struct iwl_host_cmd cmd = {
1651 .id = REPLY_CARD_STATE_CMD,
1652 .len = sizeof(u32),
1653 .data = &flags,
1654 .flags = meta_flag,
1657 return iwl_send_cmd(priv, &cmd);
1660 void iwl_rx_pm_sleep_notif(struct iwl_priv *priv,
1661 struct iwl_rx_mem_buffer *rxb)
1663 #ifdef CONFIG_IWLWIFI_DEBUG
1664 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1665 struct iwl_sleep_notification *sleep = &(pkt->u.sleep_notif);
1666 IWL_DEBUG_RX(priv, "sleep mode: %d, src: %d\n",
1667 sleep->pm_sleep_mode, sleep->pm_wakeup_src);
1668 #endif
1670 EXPORT_SYMBOL(iwl_rx_pm_sleep_notif);
1672 void iwl_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
1673 struct iwl_rx_mem_buffer *rxb)
1675 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1676 u32 len = le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
1677 IWL_DEBUG_RADIO(priv, "Dumping %d bytes of unhandled "
1678 "notification for %s:\n", len,
1679 get_cmd_string(pkt->hdr.cmd));
1680 iwl_print_hex_dump(priv, IWL_DL_RADIO, pkt->u.raw, len);
1682 EXPORT_SYMBOL(iwl_rx_pm_debug_statistics_notif);
1684 void iwl_rx_reply_error(struct iwl_priv *priv,
1685 struct iwl_rx_mem_buffer *rxb)
1687 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1689 IWL_ERR(priv, "Error Reply type 0x%08X cmd %s (0x%02X) "
1690 "seq 0x%04X ser 0x%08X\n",
1691 le32_to_cpu(pkt->u.err_resp.error_type),
1692 get_cmd_string(pkt->u.err_resp.cmd_id),
1693 pkt->u.err_resp.cmd_id,
1694 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
1695 le32_to_cpu(pkt->u.err_resp.error_info));
1697 EXPORT_SYMBOL(iwl_rx_reply_error);
1699 void iwl_clear_isr_stats(struct iwl_priv *priv)
1701 memset(&priv->isr_stats, 0, sizeof(priv->isr_stats));
1704 int iwl_mac_conf_tx(struct ieee80211_hw *hw, u16 queue,
1705 const struct ieee80211_tx_queue_params *params)
1707 struct iwl_priv *priv = hw->priv;
1708 unsigned long flags;
1709 int q;
1711 IWL_DEBUG_MAC80211(priv, "enter\n");
1713 if (!iwl_is_ready_rf(priv)) {
1714 IWL_DEBUG_MAC80211(priv, "leave - RF not ready\n");
1715 return -EIO;
1718 if (queue >= AC_NUM) {
1719 IWL_DEBUG_MAC80211(priv, "leave - queue >= AC_NUM %d\n", queue);
1720 return 0;
1723 q = AC_NUM - 1 - queue;
1725 spin_lock_irqsave(&priv->lock, flags);
1727 priv->qos_data.def_qos_parm.ac[q].cw_min = cpu_to_le16(params->cw_min);
1728 priv->qos_data.def_qos_parm.ac[q].cw_max = cpu_to_le16(params->cw_max);
1729 priv->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
1730 priv->qos_data.def_qos_parm.ac[q].edca_txop =
1731 cpu_to_le16((params->txop * 32));
1733 priv->qos_data.def_qos_parm.ac[q].reserved1 = 0;
1735 spin_unlock_irqrestore(&priv->lock, flags);
1737 IWL_DEBUG_MAC80211(priv, "leave\n");
1738 return 0;
1740 EXPORT_SYMBOL(iwl_mac_conf_tx);
1742 static void iwl_ht_conf(struct iwl_priv *priv,
1743 struct ieee80211_vif *vif)
1745 struct iwl_ht_config *ht_conf = &priv->current_ht_config;
1746 struct ieee80211_sta *sta;
1747 struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
1749 IWL_DEBUG_MAC80211(priv, "enter:\n");
1751 if (!ht_conf->is_ht)
1752 return;
1754 ht_conf->ht_protection =
1755 bss_conf->ht_operation_mode & IEEE80211_HT_OP_MODE_PROTECTION;
1756 ht_conf->non_GF_STA_present =
1757 !!(bss_conf->ht_operation_mode & IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT);
1759 ht_conf->single_chain_sufficient = false;
1761 switch (vif->type) {
1762 case NL80211_IFTYPE_STATION:
1763 rcu_read_lock();
1764 sta = ieee80211_find_sta(vif, bss_conf->bssid);
1765 if (sta) {
1766 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
1767 int maxstreams;
1769 maxstreams = (ht_cap->mcs.tx_params &
1770 IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
1771 >> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT;
1772 maxstreams += 1;
1774 if ((ht_cap->mcs.rx_mask[1] == 0) &&
1775 (ht_cap->mcs.rx_mask[2] == 0))
1776 ht_conf->single_chain_sufficient = true;
1777 if (maxstreams <= 1)
1778 ht_conf->single_chain_sufficient = true;
1779 } else {
1781 * If at all, this can only happen through a race
1782 * when the AP disconnects us while we're still
1783 * setting up the connection, in that case mac80211
1784 * will soon tell us about that.
1786 ht_conf->single_chain_sufficient = true;
1788 rcu_read_unlock();
1789 break;
1790 case NL80211_IFTYPE_ADHOC:
1791 ht_conf->single_chain_sufficient = true;
1792 break;
1793 default:
1794 break;
1797 IWL_DEBUG_MAC80211(priv, "leave\n");
1800 static inline void iwl_set_no_assoc(struct iwl_priv *priv)
1802 iwl_led_disassociate(priv);
1804 * inform the ucode that there is no longer an
1805 * association and that no more packets should be
1806 * sent
1808 priv->staging_rxon.filter_flags &=
1809 ~RXON_FILTER_ASSOC_MSK;
1810 priv->staging_rxon.assoc_id = 0;
1811 iwlcore_commit_rxon(priv);
1814 void iwl_bss_info_changed(struct ieee80211_hw *hw,
1815 struct ieee80211_vif *vif,
1816 struct ieee80211_bss_conf *bss_conf,
1817 u32 changes)
1819 struct iwl_priv *priv = hw->priv;
1820 int ret;
1822 IWL_DEBUG_MAC80211(priv, "changes = 0x%X\n", changes);
1824 if (!iwl_is_alive(priv))
1825 return;
1827 mutex_lock(&priv->mutex);
1829 if (changes & BSS_CHANGED_BEACON && vif->type == NL80211_IFTYPE_AP) {
1830 dev_kfree_skb(priv->ibss_beacon);
1831 priv->ibss_beacon = ieee80211_beacon_get(hw, vif);
1834 if (changes & BSS_CHANGED_BEACON_INT) {
1835 /* TODO: in AP mode, do something to make this take effect */
1838 if (changes & BSS_CHANGED_BSSID) {
1839 IWL_DEBUG_MAC80211(priv, "BSSID %pM\n", bss_conf->bssid);
1842 * If there is currently a HW scan going on in the
1843 * background then we need to cancel it else the RXON
1844 * below/in post_associate will fail.
1846 if (iwl_scan_cancel_timeout(priv, 100)) {
1847 IWL_WARN(priv, "Aborted scan still in progress after 100ms\n");
1848 IWL_DEBUG_MAC80211(priv, "leaving - scan abort failed.\n");
1849 mutex_unlock(&priv->mutex);
1850 return;
1853 /* mac80211 only sets assoc when in STATION mode */
1854 if (vif->type == NL80211_IFTYPE_ADHOC || bss_conf->assoc) {
1855 memcpy(priv->staging_rxon.bssid_addr,
1856 bss_conf->bssid, ETH_ALEN);
1858 /* currently needed in a few places */
1859 memcpy(priv->bssid, bss_conf->bssid, ETH_ALEN);
1860 } else {
1861 priv->staging_rxon.filter_flags &=
1862 ~RXON_FILTER_ASSOC_MSK;
1868 * This needs to be after setting the BSSID in case
1869 * mac80211 decides to do both changes at once because
1870 * it will invoke post_associate.
1872 if (vif->type == NL80211_IFTYPE_ADHOC &&
1873 changes & BSS_CHANGED_BEACON) {
1874 struct sk_buff *beacon = ieee80211_beacon_get(hw, vif);
1876 if (beacon)
1877 iwl_mac_beacon_update(hw, beacon);
1880 if (changes & BSS_CHANGED_ERP_PREAMBLE) {
1881 IWL_DEBUG_MAC80211(priv, "ERP_PREAMBLE %d\n",
1882 bss_conf->use_short_preamble);
1883 if (bss_conf->use_short_preamble)
1884 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
1885 else
1886 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
1889 if (changes & BSS_CHANGED_ERP_CTS_PROT) {
1890 IWL_DEBUG_MAC80211(priv, "ERP_CTS %d\n", bss_conf->use_cts_prot);
1891 if (bss_conf->use_cts_prot && (priv->band != IEEE80211_BAND_5GHZ))
1892 priv->staging_rxon.flags |= RXON_FLG_TGG_PROTECT_MSK;
1893 else
1894 priv->staging_rxon.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
1897 if (changes & BSS_CHANGED_BASIC_RATES) {
1898 /* XXX use this information
1900 * To do that, remove code from iwl_set_rate() and put something
1901 * like this here:
1903 if (A-band)
1904 priv->staging_rxon.ofdm_basic_rates =
1905 bss_conf->basic_rates;
1906 else
1907 priv->staging_rxon.ofdm_basic_rates =
1908 bss_conf->basic_rates >> 4;
1909 priv->staging_rxon.cck_basic_rates =
1910 bss_conf->basic_rates & 0xF;
1914 if (changes & BSS_CHANGED_HT) {
1915 iwl_ht_conf(priv, vif);
1917 if (priv->cfg->ops->hcmd->set_rxon_chain)
1918 priv->cfg->ops->hcmd->set_rxon_chain(priv);
1921 if (changes & BSS_CHANGED_ASSOC) {
1922 IWL_DEBUG_MAC80211(priv, "ASSOC %d\n", bss_conf->assoc);
1923 if (bss_conf->assoc) {
1924 priv->timestamp = bss_conf->timestamp;
1926 iwl_led_associate(priv);
1928 if (!iwl_is_rfkill(priv))
1929 priv->cfg->ops->lib->post_associate(priv, vif);
1930 } else
1931 iwl_set_no_assoc(priv);
1934 if (changes && iwl_is_associated(priv) && bss_conf->aid) {
1935 IWL_DEBUG_MAC80211(priv, "Changes (%#x) while associated\n",
1936 changes);
1937 ret = iwl_send_rxon_assoc(priv);
1938 if (!ret) {
1939 /* Sync active_rxon with latest change. */
1940 memcpy((void *)&priv->active_rxon,
1941 &priv->staging_rxon,
1942 sizeof(struct iwl_rxon_cmd));
1946 if (changes & BSS_CHANGED_BEACON_ENABLED) {
1947 if (vif->bss_conf.enable_beacon) {
1948 memcpy(priv->staging_rxon.bssid_addr,
1949 bss_conf->bssid, ETH_ALEN);
1950 memcpy(priv->bssid, bss_conf->bssid, ETH_ALEN);
1951 iwlcore_config_ap(priv, vif);
1952 } else
1953 iwl_set_no_assoc(priv);
1956 if (changes & BSS_CHANGED_IBSS) {
1957 ret = priv->cfg->ops->lib->manage_ibss_station(priv, vif,
1958 bss_conf->ibss_joined);
1959 if (ret)
1960 IWL_ERR(priv, "failed to %s IBSS station %pM\n",
1961 bss_conf->ibss_joined ? "add" : "remove",
1962 bss_conf->bssid);
1965 mutex_unlock(&priv->mutex);
1967 IWL_DEBUG_MAC80211(priv, "leave\n");
1969 EXPORT_SYMBOL(iwl_bss_info_changed);
1971 int iwl_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb)
1973 struct iwl_priv *priv = hw->priv;
1974 unsigned long flags;
1975 __le64 timestamp;
1977 IWL_DEBUG_MAC80211(priv, "enter\n");
1979 if (!iwl_is_ready_rf(priv)) {
1980 IWL_DEBUG_MAC80211(priv, "leave - RF not ready\n");
1981 return -EIO;
1984 spin_lock_irqsave(&priv->lock, flags);
1986 if (priv->ibss_beacon)
1987 dev_kfree_skb(priv->ibss_beacon);
1989 priv->ibss_beacon = skb;
1991 timestamp = ((struct ieee80211_mgmt *)skb->data)->u.beacon.timestamp;
1992 priv->timestamp = le64_to_cpu(timestamp);
1994 IWL_DEBUG_MAC80211(priv, "leave\n");
1995 spin_unlock_irqrestore(&priv->lock, flags);
1997 priv->cfg->ops->lib->post_associate(priv, priv->vif);
1999 return 0;
2001 EXPORT_SYMBOL(iwl_mac_beacon_update);
2003 static int iwl_set_mode(struct iwl_priv *priv, struct ieee80211_vif *vif)
2005 iwl_connection_init_rx_config(priv, vif);
2007 if (priv->cfg->ops->hcmd->set_rxon_chain)
2008 priv->cfg->ops->hcmd->set_rxon_chain(priv);
2010 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
2012 return iwlcore_commit_rxon(priv);
2015 int iwl_mac_add_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
2017 struct iwl_priv *priv = hw->priv;
2018 int err = 0;
2020 IWL_DEBUG_MAC80211(priv, "enter: type %d\n", vif->type);
2022 mutex_lock(&priv->mutex);
2024 if (WARN_ON(!iwl_is_ready_rf(priv))) {
2025 err = -EINVAL;
2026 goto out;
2029 if (priv->vif) {
2030 IWL_DEBUG_MAC80211(priv, "leave - vif != NULL\n");
2031 err = -EOPNOTSUPP;
2032 goto out;
2035 priv->vif = vif;
2036 priv->iw_mode = vif->type;
2038 IWL_DEBUG_MAC80211(priv, "Set %pM\n", vif->addr);
2039 memcpy(priv->mac_addr, vif->addr, ETH_ALEN);
2041 err = iwl_set_mode(priv, vif);
2042 if (err)
2043 goto out_err;
2045 goto out;
2047 out_err:
2048 priv->vif = NULL;
2049 priv->iw_mode = NL80211_IFTYPE_STATION;
2050 out:
2051 mutex_unlock(&priv->mutex);
2053 IWL_DEBUG_MAC80211(priv, "leave\n");
2054 return err;
2056 EXPORT_SYMBOL(iwl_mac_add_interface);
2058 void iwl_mac_remove_interface(struct ieee80211_hw *hw,
2059 struct ieee80211_vif *vif)
2061 struct iwl_priv *priv = hw->priv;
2063 IWL_DEBUG_MAC80211(priv, "enter\n");
2065 mutex_lock(&priv->mutex);
2067 if (iwl_is_ready_rf(priv)) {
2068 iwl_scan_cancel_timeout(priv, 100);
2069 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
2070 iwlcore_commit_rxon(priv);
2072 if (priv->vif == vif) {
2073 priv->vif = NULL;
2074 memset(priv->bssid, 0, ETH_ALEN);
2076 mutex_unlock(&priv->mutex);
2078 IWL_DEBUG_MAC80211(priv, "leave\n");
2081 EXPORT_SYMBOL(iwl_mac_remove_interface);
2084 * iwl_mac_config - mac80211 config callback
2086 int iwl_mac_config(struct ieee80211_hw *hw, u32 changed)
2088 struct iwl_priv *priv = hw->priv;
2089 const struct iwl_channel_info *ch_info;
2090 struct ieee80211_conf *conf = &hw->conf;
2091 struct iwl_ht_config *ht_conf = &priv->current_ht_config;
2092 unsigned long flags = 0;
2093 int ret = 0;
2094 u16 ch;
2095 int scan_active = 0;
2097 mutex_lock(&priv->mutex);
2099 IWL_DEBUG_MAC80211(priv, "enter to channel %d changed 0x%X\n",
2100 conf->channel->hw_value, changed);
2102 if (unlikely(!priv->cfg->mod_params->disable_hw_scan &&
2103 test_bit(STATUS_SCANNING, &priv->status))) {
2104 scan_active = 1;
2105 IWL_DEBUG_MAC80211(priv, "leave - scanning\n");
2108 if (changed & (IEEE80211_CONF_CHANGE_SMPS |
2109 IEEE80211_CONF_CHANGE_CHANNEL)) {
2110 /* mac80211 uses static for non-HT which is what we want */
2111 priv->current_ht_config.smps = conf->smps_mode;
2114 * Recalculate chain counts.
2116 * If monitor mode is enabled then mac80211 will
2117 * set up the SM PS mode to OFF if an HT channel is
2118 * configured.
2120 if (priv->cfg->ops->hcmd->set_rxon_chain)
2121 priv->cfg->ops->hcmd->set_rxon_chain(priv);
2124 /* during scanning mac80211 will delay channel setting until
2125 * scan finish with changed = 0
2127 if (!changed || (changed & IEEE80211_CONF_CHANGE_CHANNEL)) {
2128 if (scan_active)
2129 goto set_ch_out;
2131 ch = ieee80211_frequency_to_channel(conf->channel->center_freq);
2132 ch_info = iwl_get_channel_info(priv, conf->channel->band, ch);
2133 if (!is_channel_valid(ch_info)) {
2134 IWL_DEBUG_MAC80211(priv, "leave - invalid channel\n");
2135 ret = -EINVAL;
2136 goto set_ch_out;
2139 spin_lock_irqsave(&priv->lock, flags);
2141 /* Configure HT40 channels */
2142 ht_conf->is_ht = conf_is_ht(conf);
2143 if (ht_conf->is_ht) {
2144 if (conf_is_ht40_minus(conf)) {
2145 ht_conf->extension_chan_offset =
2146 IEEE80211_HT_PARAM_CHA_SEC_BELOW;
2147 ht_conf->is_40mhz = true;
2148 } else if (conf_is_ht40_plus(conf)) {
2149 ht_conf->extension_chan_offset =
2150 IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
2151 ht_conf->is_40mhz = true;
2152 } else {
2153 ht_conf->extension_chan_offset =
2154 IEEE80211_HT_PARAM_CHA_SEC_NONE;
2155 ht_conf->is_40mhz = false;
2157 } else
2158 ht_conf->is_40mhz = false;
2159 /* Default to no protection. Protection mode will later be set
2160 * from BSS config in iwl_ht_conf */
2161 ht_conf->ht_protection = IEEE80211_HT_OP_MODE_PROTECTION_NONE;
2163 /* if we are switching from ht to 2.4 clear flags
2164 * from any ht related info since 2.4 does not
2165 * support ht */
2166 if ((le16_to_cpu(priv->staging_rxon.channel) != ch))
2167 priv->staging_rxon.flags = 0;
2169 iwl_set_rxon_channel(priv, conf->channel);
2170 iwl_set_rxon_ht(priv, ht_conf);
2172 iwl_set_flags_for_band(priv, conf->channel->band, priv->vif);
2173 spin_unlock_irqrestore(&priv->lock, flags);
2174 if (iwl_is_associated(priv) &&
2175 (le16_to_cpu(priv->active_rxon.channel) != ch) &&
2176 priv->cfg->ops->lib->set_channel_switch) {
2177 iwl_set_rate(priv);
2179 * at this point, staging_rxon has the
2180 * configuration for channel switch
2182 ret = priv->cfg->ops->lib->set_channel_switch(priv,
2183 ch);
2184 if (!ret) {
2185 iwl_print_rx_config_cmd(priv);
2186 goto out;
2188 priv->switch_rxon.switch_in_progress = false;
2190 set_ch_out:
2191 /* The list of supported rates and rate mask can be different
2192 * for each band; since the band may have changed, reset
2193 * the rate mask to what mac80211 lists */
2194 iwl_set_rate(priv);
2197 if (changed & (IEEE80211_CONF_CHANGE_PS |
2198 IEEE80211_CONF_CHANGE_IDLE)) {
2199 ret = iwl_power_update_mode(priv, false);
2200 if (ret)
2201 IWL_DEBUG_MAC80211(priv, "Error setting sleep level\n");
2204 if (changed & IEEE80211_CONF_CHANGE_POWER) {
2205 IWL_DEBUG_MAC80211(priv, "TX Power old=%d new=%d\n",
2206 priv->tx_power_user_lmt, conf->power_level);
2208 iwl_set_tx_power(priv, conf->power_level, false);
2211 if (changed & IEEE80211_CONF_CHANGE_QOS) {
2212 bool qos_active = !!(conf->flags & IEEE80211_CONF_QOS);
2214 spin_lock_irqsave(&priv->lock, flags);
2215 priv->qos_data.qos_active = qos_active;
2216 iwl_update_qos(priv);
2217 spin_unlock_irqrestore(&priv->lock, flags);
2220 if (!iwl_is_ready(priv)) {
2221 IWL_DEBUG_MAC80211(priv, "leave - not ready\n");
2222 goto out;
2225 if (scan_active)
2226 goto out;
2228 if (memcmp(&priv->active_rxon,
2229 &priv->staging_rxon, sizeof(priv->staging_rxon)))
2230 iwlcore_commit_rxon(priv);
2231 else
2232 IWL_DEBUG_INFO(priv, "Not re-sending same RXON configuration.\n");
2235 out:
2236 IWL_DEBUG_MAC80211(priv, "leave\n");
2237 mutex_unlock(&priv->mutex);
2238 return ret;
2240 EXPORT_SYMBOL(iwl_mac_config);
2242 void iwl_mac_reset_tsf(struct ieee80211_hw *hw)
2244 struct iwl_priv *priv = hw->priv;
2245 unsigned long flags;
2247 mutex_lock(&priv->mutex);
2248 IWL_DEBUG_MAC80211(priv, "enter\n");
2250 spin_lock_irqsave(&priv->lock, flags);
2251 memset(&priv->current_ht_config, 0, sizeof(struct iwl_ht_config));
2252 spin_unlock_irqrestore(&priv->lock, flags);
2254 spin_lock_irqsave(&priv->lock, flags);
2256 /* new association get rid of ibss beacon skb */
2257 if (priv->ibss_beacon)
2258 dev_kfree_skb(priv->ibss_beacon);
2260 priv->ibss_beacon = NULL;
2262 priv->timestamp = 0;
2264 spin_unlock_irqrestore(&priv->lock, flags);
2266 if (!iwl_is_ready_rf(priv)) {
2267 IWL_DEBUG_MAC80211(priv, "leave - not ready\n");
2268 mutex_unlock(&priv->mutex);
2269 return;
2272 /* we are restarting association process
2273 * clear RXON_FILTER_ASSOC_MSK bit
2275 iwl_scan_cancel_timeout(priv, 100);
2276 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
2277 iwlcore_commit_rxon(priv);
2279 iwl_set_rate(priv);
2281 mutex_unlock(&priv->mutex);
2283 IWL_DEBUG_MAC80211(priv, "leave\n");
2285 EXPORT_SYMBOL(iwl_mac_reset_tsf);
2287 int iwl_alloc_txq_mem(struct iwl_priv *priv)
2289 if (!priv->txq)
2290 priv->txq = kzalloc(
2291 sizeof(struct iwl_tx_queue) * priv->cfg->num_of_queues,
2292 GFP_KERNEL);
2293 if (!priv->txq) {
2294 IWL_ERR(priv, "Not enough memory for txq\n");
2295 return -ENOMEM;
2297 return 0;
2299 EXPORT_SYMBOL(iwl_alloc_txq_mem);
2301 void iwl_free_txq_mem(struct iwl_priv *priv)
2303 kfree(priv->txq);
2304 priv->txq = NULL;
2306 EXPORT_SYMBOL(iwl_free_txq_mem);
2308 #ifdef CONFIG_IWLWIFI_DEBUGFS
2310 #define IWL_TRAFFIC_DUMP_SIZE (IWL_TRAFFIC_ENTRY_SIZE * IWL_TRAFFIC_ENTRIES)
2312 void iwl_reset_traffic_log(struct iwl_priv *priv)
2314 priv->tx_traffic_idx = 0;
2315 priv->rx_traffic_idx = 0;
2316 if (priv->tx_traffic)
2317 memset(priv->tx_traffic, 0, IWL_TRAFFIC_DUMP_SIZE);
2318 if (priv->rx_traffic)
2319 memset(priv->rx_traffic, 0, IWL_TRAFFIC_DUMP_SIZE);
2322 int iwl_alloc_traffic_mem(struct iwl_priv *priv)
2324 u32 traffic_size = IWL_TRAFFIC_DUMP_SIZE;
2326 if (iwl_debug_level & IWL_DL_TX) {
2327 if (!priv->tx_traffic) {
2328 priv->tx_traffic =
2329 kzalloc(traffic_size, GFP_KERNEL);
2330 if (!priv->tx_traffic)
2331 return -ENOMEM;
2334 if (iwl_debug_level & IWL_DL_RX) {
2335 if (!priv->rx_traffic) {
2336 priv->rx_traffic =
2337 kzalloc(traffic_size, GFP_KERNEL);
2338 if (!priv->rx_traffic)
2339 return -ENOMEM;
2342 iwl_reset_traffic_log(priv);
2343 return 0;
2345 EXPORT_SYMBOL(iwl_alloc_traffic_mem);
2347 void iwl_free_traffic_mem(struct iwl_priv *priv)
2349 kfree(priv->tx_traffic);
2350 priv->tx_traffic = NULL;
2352 kfree(priv->rx_traffic);
2353 priv->rx_traffic = NULL;
2355 EXPORT_SYMBOL(iwl_free_traffic_mem);
2357 void iwl_dbg_log_tx_data_frame(struct iwl_priv *priv,
2358 u16 length, struct ieee80211_hdr *header)
2360 __le16 fc;
2361 u16 len;
2363 if (likely(!(iwl_debug_level & IWL_DL_TX)))
2364 return;
2366 if (!priv->tx_traffic)
2367 return;
2369 fc = header->frame_control;
2370 if (ieee80211_is_data(fc)) {
2371 len = (length > IWL_TRAFFIC_ENTRY_SIZE)
2372 ? IWL_TRAFFIC_ENTRY_SIZE : length;
2373 memcpy((priv->tx_traffic +
2374 (priv->tx_traffic_idx * IWL_TRAFFIC_ENTRY_SIZE)),
2375 header, len);
2376 priv->tx_traffic_idx =
2377 (priv->tx_traffic_idx + 1) % IWL_TRAFFIC_ENTRIES;
2380 EXPORT_SYMBOL(iwl_dbg_log_tx_data_frame);
2382 void iwl_dbg_log_rx_data_frame(struct iwl_priv *priv,
2383 u16 length, struct ieee80211_hdr *header)
2385 __le16 fc;
2386 u16 len;
2388 if (likely(!(iwl_debug_level & IWL_DL_RX)))
2389 return;
2391 if (!priv->rx_traffic)
2392 return;
2394 fc = header->frame_control;
2395 if (ieee80211_is_data(fc)) {
2396 len = (length > IWL_TRAFFIC_ENTRY_SIZE)
2397 ? IWL_TRAFFIC_ENTRY_SIZE : length;
2398 memcpy((priv->rx_traffic +
2399 (priv->rx_traffic_idx * IWL_TRAFFIC_ENTRY_SIZE)),
2400 header, len);
2401 priv->rx_traffic_idx =
2402 (priv->rx_traffic_idx + 1) % IWL_TRAFFIC_ENTRIES;
2405 EXPORT_SYMBOL(iwl_dbg_log_rx_data_frame);
2407 const char *get_mgmt_string(int cmd)
2409 switch (cmd) {
2410 IWL_CMD(MANAGEMENT_ASSOC_REQ);
2411 IWL_CMD(MANAGEMENT_ASSOC_RESP);
2412 IWL_CMD(MANAGEMENT_REASSOC_REQ);
2413 IWL_CMD(MANAGEMENT_REASSOC_RESP);
2414 IWL_CMD(MANAGEMENT_PROBE_REQ);
2415 IWL_CMD(MANAGEMENT_PROBE_RESP);
2416 IWL_CMD(MANAGEMENT_BEACON);
2417 IWL_CMD(MANAGEMENT_ATIM);
2418 IWL_CMD(MANAGEMENT_DISASSOC);
2419 IWL_CMD(MANAGEMENT_AUTH);
2420 IWL_CMD(MANAGEMENT_DEAUTH);
2421 IWL_CMD(MANAGEMENT_ACTION);
2422 default:
2423 return "UNKNOWN";
2428 const char *get_ctrl_string(int cmd)
2430 switch (cmd) {
2431 IWL_CMD(CONTROL_BACK_REQ);
2432 IWL_CMD(CONTROL_BACK);
2433 IWL_CMD(CONTROL_PSPOLL);
2434 IWL_CMD(CONTROL_RTS);
2435 IWL_CMD(CONTROL_CTS);
2436 IWL_CMD(CONTROL_ACK);
2437 IWL_CMD(CONTROL_CFEND);
2438 IWL_CMD(CONTROL_CFENDACK);
2439 default:
2440 return "UNKNOWN";
2445 void iwl_clear_traffic_stats(struct iwl_priv *priv)
2447 memset(&priv->tx_stats, 0, sizeof(struct traffic_stats));
2448 memset(&priv->rx_stats, 0, sizeof(struct traffic_stats));
2449 priv->led_tpt = 0;
2453 * if CONFIG_IWLWIFI_DEBUGFS defined, iwl_update_stats function will
2454 * record all the MGMT, CTRL and DATA pkt for both TX and Rx pass.
2455 * Use debugFs to display the rx/rx_statistics
2456 * if CONFIG_IWLWIFI_DEBUGFS not being defined, then no MGMT and CTRL
2457 * information will be recorded, but DATA pkt still will be recorded
2458 * for the reason of iwl_led.c need to control the led blinking based on
2459 * number of tx and rx data.
2462 void iwl_update_stats(struct iwl_priv *priv, bool is_tx, __le16 fc, u16 len)
2464 struct traffic_stats *stats;
2466 if (is_tx)
2467 stats = &priv->tx_stats;
2468 else
2469 stats = &priv->rx_stats;
2471 if (ieee80211_is_mgmt(fc)) {
2472 switch (fc & cpu_to_le16(IEEE80211_FCTL_STYPE)) {
2473 case cpu_to_le16(IEEE80211_STYPE_ASSOC_REQ):
2474 stats->mgmt[MANAGEMENT_ASSOC_REQ]++;
2475 break;
2476 case cpu_to_le16(IEEE80211_STYPE_ASSOC_RESP):
2477 stats->mgmt[MANAGEMENT_ASSOC_RESP]++;
2478 break;
2479 case cpu_to_le16(IEEE80211_STYPE_REASSOC_REQ):
2480 stats->mgmt[MANAGEMENT_REASSOC_REQ]++;
2481 break;
2482 case cpu_to_le16(IEEE80211_STYPE_REASSOC_RESP):
2483 stats->mgmt[MANAGEMENT_REASSOC_RESP]++;
2484 break;
2485 case cpu_to_le16(IEEE80211_STYPE_PROBE_REQ):
2486 stats->mgmt[MANAGEMENT_PROBE_REQ]++;
2487 break;
2488 case cpu_to_le16(IEEE80211_STYPE_PROBE_RESP):
2489 stats->mgmt[MANAGEMENT_PROBE_RESP]++;
2490 break;
2491 case cpu_to_le16(IEEE80211_STYPE_BEACON):
2492 stats->mgmt[MANAGEMENT_BEACON]++;
2493 break;
2494 case cpu_to_le16(IEEE80211_STYPE_ATIM):
2495 stats->mgmt[MANAGEMENT_ATIM]++;
2496 break;
2497 case cpu_to_le16(IEEE80211_STYPE_DISASSOC):
2498 stats->mgmt[MANAGEMENT_DISASSOC]++;
2499 break;
2500 case cpu_to_le16(IEEE80211_STYPE_AUTH):
2501 stats->mgmt[MANAGEMENT_AUTH]++;
2502 break;
2503 case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
2504 stats->mgmt[MANAGEMENT_DEAUTH]++;
2505 break;
2506 case cpu_to_le16(IEEE80211_STYPE_ACTION):
2507 stats->mgmt[MANAGEMENT_ACTION]++;
2508 break;
2510 } else if (ieee80211_is_ctl(fc)) {
2511 switch (fc & cpu_to_le16(IEEE80211_FCTL_STYPE)) {
2512 case cpu_to_le16(IEEE80211_STYPE_BACK_REQ):
2513 stats->ctrl[CONTROL_BACK_REQ]++;
2514 break;
2515 case cpu_to_le16(IEEE80211_STYPE_BACK):
2516 stats->ctrl[CONTROL_BACK]++;
2517 break;
2518 case cpu_to_le16(IEEE80211_STYPE_PSPOLL):
2519 stats->ctrl[CONTROL_PSPOLL]++;
2520 break;
2521 case cpu_to_le16(IEEE80211_STYPE_RTS):
2522 stats->ctrl[CONTROL_RTS]++;
2523 break;
2524 case cpu_to_le16(IEEE80211_STYPE_CTS):
2525 stats->ctrl[CONTROL_CTS]++;
2526 break;
2527 case cpu_to_le16(IEEE80211_STYPE_ACK):
2528 stats->ctrl[CONTROL_ACK]++;
2529 break;
2530 case cpu_to_le16(IEEE80211_STYPE_CFEND):
2531 stats->ctrl[CONTROL_CFEND]++;
2532 break;
2533 case cpu_to_le16(IEEE80211_STYPE_CFENDACK):
2534 stats->ctrl[CONTROL_CFENDACK]++;
2535 break;
2537 } else {
2538 /* data */
2539 stats->data_cnt++;
2540 stats->data_bytes += len;
2542 iwl_leds_background(priv);
2544 EXPORT_SYMBOL(iwl_update_stats);
2545 #endif
2547 const static char *get_csr_string(int cmd)
2549 switch (cmd) {
2550 IWL_CMD(CSR_HW_IF_CONFIG_REG);
2551 IWL_CMD(CSR_INT_COALESCING);
2552 IWL_CMD(CSR_INT);
2553 IWL_CMD(CSR_INT_MASK);
2554 IWL_CMD(CSR_FH_INT_STATUS);
2555 IWL_CMD(CSR_GPIO_IN);
2556 IWL_CMD(CSR_RESET);
2557 IWL_CMD(CSR_GP_CNTRL);
2558 IWL_CMD(CSR_HW_REV);
2559 IWL_CMD(CSR_EEPROM_REG);
2560 IWL_CMD(CSR_EEPROM_GP);
2561 IWL_CMD(CSR_OTP_GP_REG);
2562 IWL_CMD(CSR_GIO_REG);
2563 IWL_CMD(CSR_GP_UCODE_REG);
2564 IWL_CMD(CSR_GP_DRIVER_REG);
2565 IWL_CMD(CSR_UCODE_DRV_GP1);
2566 IWL_CMD(CSR_UCODE_DRV_GP2);
2567 IWL_CMD(CSR_LED_REG);
2568 IWL_CMD(CSR_DRAM_INT_TBL_REG);
2569 IWL_CMD(CSR_GIO_CHICKEN_BITS);
2570 IWL_CMD(CSR_ANA_PLL_CFG);
2571 IWL_CMD(CSR_HW_REV_WA_REG);
2572 IWL_CMD(CSR_DBG_HPET_MEM_REG);
2573 default:
2574 return "UNKNOWN";
2579 void iwl_dump_csr(struct iwl_priv *priv)
2581 int i;
2582 u32 csr_tbl[] = {
2583 CSR_HW_IF_CONFIG_REG,
2584 CSR_INT_COALESCING,
2585 CSR_INT,
2586 CSR_INT_MASK,
2587 CSR_FH_INT_STATUS,
2588 CSR_GPIO_IN,
2589 CSR_RESET,
2590 CSR_GP_CNTRL,
2591 CSR_HW_REV,
2592 CSR_EEPROM_REG,
2593 CSR_EEPROM_GP,
2594 CSR_OTP_GP_REG,
2595 CSR_GIO_REG,
2596 CSR_GP_UCODE_REG,
2597 CSR_GP_DRIVER_REG,
2598 CSR_UCODE_DRV_GP1,
2599 CSR_UCODE_DRV_GP2,
2600 CSR_LED_REG,
2601 CSR_DRAM_INT_TBL_REG,
2602 CSR_GIO_CHICKEN_BITS,
2603 CSR_ANA_PLL_CFG,
2604 CSR_HW_REV_WA_REG,
2605 CSR_DBG_HPET_MEM_REG
2607 IWL_ERR(priv, "CSR values:\n");
2608 IWL_ERR(priv, "(2nd byte of CSR_INT_COALESCING is "
2609 "CSR_INT_PERIODIC_REG)\n");
2610 for (i = 0; i < ARRAY_SIZE(csr_tbl); i++) {
2611 IWL_ERR(priv, " %25s: 0X%08x\n",
2612 get_csr_string(csr_tbl[i]),
2613 iwl_read32(priv, csr_tbl[i]));
2616 EXPORT_SYMBOL(iwl_dump_csr);
2618 const static char *get_fh_string(int cmd)
2620 switch (cmd) {
2621 IWL_CMD(FH_RSCSR_CHNL0_STTS_WPTR_REG);
2622 IWL_CMD(FH_RSCSR_CHNL0_RBDCB_BASE_REG);
2623 IWL_CMD(FH_RSCSR_CHNL0_WPTR);
2624 IWL_CMD(FH_MEM_RCSR_CHNL0_CONFIG_REG);
2625 IWL_CMD(FH_MEM_RSSR_SHARED_CTRL_REG);
2626 IWL_CMD(FH_MEM_RSSR_RX_STATUS_REG);
2627 IWL_CMD(FH_MEM_RSSR_RX_ENABLE_ERR_IRQ2DRV);
2628 IWL_CMD(FH_TSSR_TX_STATUS_REG);
2629 IWL_CMD(FH_TSSR_TX_ERROR_REG);
2630 default:
2631 return "UNKNOWN";
2636 int iwl_dump_fh(struct iwl_priv *priv, char **buf, bool display)
2638 int i;
2639 #ifdef CONFIG_IWLWIFI_DEBUG
2640 int pos = 0;
2641 size_t bufsz = 0;
2642 #endif
2643 u32 fh_tbl[] = {
2644 FH_RSCSR_CHNL0_STTS_WPTR_REG,
2645 FH_RSCSR_CHNL0_RBDCB_BASE_REG,
2646 FH_RSCSR_CHNL0_WPTR,
2647 FH_MEM_RCSR_CHNL0_CONFIG_REG,
2648 FH_MEM_RSSR_SHARED_CTRL_REG,
2649 FH_MEM_RSSR_RX_STATUS_REG,
2650 FH_MEM_RSSR_RX_ENABLE_ERR_IRQ2DRV,
2651 FH_TSSR_TX_STATUS_REG,
2652 FH_TSSR_TX_ERROR_REG
2654 #ifdef CONFIG_IWLWIFI_DEBUG
2655 if (display) {
2656 bufsz = ARRAY_SIZE(fh_tbl) * 48 + 40;
2657 *buf = kmalloc(bufsz, GFP_KERNEL);
2658 if (!*buf)
2659 return -ENOMEM;
2660 pos += scnprintf(*buf + pos, bufsz - pos,
2661 "FH register values:\n");
2662 for (i = 0; i < ARRAY_SIZE(fh_tbl); i++) {
2663 pos += scnprintf(*buf + pos, bufsz - pos,
2664 " %34s: 0X%08x\n",
2665 get_fh_string(fh_tbl[i]),
2666 iwl_read_direct32(priv, fh_tbl[i]));
2668 return pos;
2670 #endif
2671 IWL_ERR(priv, "FH register values:\n");
2672 for (i = 0; i < ARRAY_SIZE(fh_tbl); i++) {
2673 IWL_ERR(priv, " %34s: 0X%08x\n",
2674 get_fh_string(fh_tbl[i]),
2675 iwl_read_direct32(priv, fh_tbl[i]));
2677 return 0;
2679 EXPORT_SYMBOL(iwl_dump_fh);
2681 static void iwl_force_rf_reset(struct iwl_priv *priv)
2683 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2684 return;
2686 if (!iwl_is_associated(priv)) {
2687 IWL_DEBUG_SCAN(priv, "force reset rejected: not associated\n");
2688 return;
2691 * There is no easy and better way to force reset the radio,
2692 * the only known method is switching channel which will force to
2693 * reset and tune the radio.
2694 * Use internal short scan (single channel) operation to should
2695 * achieve this objective.
2696 * Driver should reset the radio when number of consecutive missed
2697 * beacon, or any other uCode error condition detected.
2699 IWL_DEBUG_INFO(priv, "perform radio reset.\n");
2700 iwl_internal_short_hw_scan(priv);
2704 int iwl_force_reset(struct iwl_priv *priv, int mode)
2706 struct iwl_force_reset *force_reset;
2708 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2709 return -EINVAL;
2711 if (mode >= IWL_MAX_FORCE_RESET) {
2712 IWL_DEBUG_INFO(priv, "invalid reset request.\n");
2713 return -EINVAL;
2715 force_reset = &priv->force_reset[mode];
2716 force_reset->reset_request_count++;
2717 if (force_reset->last_force_reset_jiffies &&
2718 time_after(force_reset->last_force_reset_jiffies +
2719 force_reset->reset_duration, jiffies)) {
2720 IWL_DEBUG_INFO(priv, "force reset rejected\n");
2721 force_reset->reset_reject_count++;
2722 return -EAGAIN;
2724 force_reset->reset_success_count++;
2725 force_reset->last_force_reset_jiffies = jiffies;
2726 IWL_DEBUG_INFO(priv, "perform force reset (%d)\n", mode);
2727 switch (mode) {
2728 case IWL_RF_RESET:
2729 iwl_force_rf_reset(priv);
2730 break;
2731 case IWL_FW_RESET:
2732 IWL_ERR(priv, "On demand firmware reload\n");
2733 /* Set the FW error flag -- cleared on iwl_down */
2734 set_bit(STATUS_FW_ERROR, &priv->status);
2735 wake_up_interruptible(&priv->wait_command_queue);
2737 * Keep the restart process from trying to send host
2738 * commands by clearing the INIT status bit
2740 clear_bit(STATUS_READY, &priv->status);
2741 queue_work(priv->workqueue, &priv->restart);
2742 break;
2744 return 0;
2746 EXPORT_SYMBOL(iwl_force_reset);
2749 * iwl_bg_monitor_recover - Timer callback to check for stuck queue and recover
2751 * During normal condition (no queue is stuck), the timer is continually set to
2752 * execute every monitor_recover_period milliseconds after the last timer
2753 * expired. When the queue read_ptr is at the same place, the timer is
2754 * shorten to 100mSecs. This is
2755 * 1) to reduce the chance that the read_ptr may wrap around (not stuck)
2756 * 2) to detect the stuck queues quicker before the station and AP can
2757 * disassociate each other.
2759 * This function monitors all the tx queues and recover from it if any
2760 * of the queues are stuck.
2761 * 1. It first check the cmd queue for stuck conditions. If it is stuck,
2762 * it will recover by resetting the firmware and return.
2763 * 2. Then, it checks for station association. If it associates it will check
2764 * other queues. If any queue is stuck, it will recover by resetting
2765 * the firmware.
2766 * Note: It the number of times the queue read_ptr to be at the same place to
2767 * be MAX_REPEAT+1 in order to consider to be stuck.
2770 * The maximum number of times the read pointer of the tx queue at the
2771 * same place without considering to be stuck.
2773 #define MAX_REPEAT (2)
2774 static int iwl_check_stuck_queue(struct iwl_priv *priv, int cnt)
2776 struct iwl_tx_queue *txq;
2777 struct iwl_queue *q;
2779 txq = &priv->txq[cnt];
2780 q = &txq->q;
2781 /* queue is empty, skip */
2782 if (q->read_ptr != q->write_ptr) {
2783 if (q->read_ptr == q->last_read_ptr) {
2784 /* a queue has not been read from last time */
2785 if (q->repeat_same_read_ptr > MAX_REPEAT) {
2786 IWL_ERR(priv,
2787 "queue %d stuck %d time. Fw reload.\n",
2788 q->id, q->repeat_same_read_ptr);
2789 q->repeat_same_read_ptr = 0;
2790 iwl_force_reset(priv, IWL_FW_RESET);
2791 } else {
2792 q->repeat_same_read_ptr++;
2793 IWL_DEBUG_RADIO(priv,
2794 "queue %d, not read %d time\n",
2795 q->id,
2796 q->repeat_same_read_ptr);
2797 mod_timer(&priv->monitor_recover, jiffies +
2798 msecs_to_jiffies(IWL_ONE_HUNDRED_MSECS));
2800 return 1;
2801 } else {
2802 q->last_read_ptr = q->read_ptr;
2803 q->repeat_same_read_ptr = 0;
2806 return 0;
2809 void iwl_bg_monitor_recover(unsigned long data)
2811 struct iwl_priv *priv = (struct iwl_priv *)data;
2812 int cnt;
2814 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2815 return;
2817 /* monitor and check for stuck cmd queue */
2818 if (iwl_check_stuck_queue(priv, IWL_CMD_QUEUE_NUM))
2819 return;
2821 /* monitor and check for other stuck queues */
2822 if (iwl_is_associated(priv)) {
2823 for (cnt = 0; cnt < priv->hw_params.max_txq_num; cnt++) {
2824 /* skip as we already checked the command queue */
2825 if (cnt == IWL_CMD_QUEUE_NUM)
2826 continue;
2827 if (iwl_check_stuck_queue(priv, cnt))
2828 return;
2832 * Reschedule the timer to occur in
2833 * priv->cfg->monitor_recover_period
2835 mod_timer(&priv->monitor_recover,
2836 jiffies + msecs_to_jiffies(priv->cfg->monitor_recover_period));
2838 EXPORT_SYMBOL(iwl_bg_monitor_recover);
2840 #ifdef CONFIG_PM
2842 int iwl_pci_suspend(struct pci_dev *pdev, pm_message_t state)
2844 struct iwl_priv *priv = pci_get_drvdata(pdev);
2847 * This function is called when system goes into suspend state
2848 * mac80211 will call iwl_mac_stop() from the mac80211 suspend function
2849 * first but since iwl_mac_stop() has no knowledge of who the caller is,
2850 * it will not call apm_ops.stop() to stop the DMA operation.
2851 * Calling apm_ops.stop here to make sure we stop the DMA.
2853 priv->cfg->ops->lib->apm_ops.stop(priv);
2855 pci_save_state(pdev);
2856 pci_disable_device(pdev);
2857 pci_set_power_state(pdev, PCI_D3hot);
2859 return 0;
2861 EXPORT_SYMBOL(iwl_pci_suspend);
2863 int iwl_pci_resume(struct pci_dev *pdev)
2865 struct iwl_priv *priv = pci_get_drvdata(pdev);
2866 int ret;
2869 * We disable the RETRY_TIMEOUT register (0x41) to keep
2870 * PCI Tx retries from interfering with C3 CPU state.
2872 pci_write_config_byte(pdev, PCI_CFG_RETRY_TIMEOUT, 0x00);
2874 pci_set_power_state(pdev, PCI_D0);
2875 ret = pci_enable_device(pdev);
2876 if (ret)
2877 return ret;
2878 pci_restore_state(pdev);
2879 iwl_enable_interrupts(priv);
2881 return 0;
2883 EXPORT_SYMBOL(iwl_pci_resume);
2885 #endif /* CONFIG_PM */