iwlwifi: move chain settings to agn
[deliverable/linux.git] / drivers / net / wireless / iwlwifi / iwl-core.c
CommitLineData
df48c323 1/******************************************************************************
df48c323
TW
2 *
3 * GPL LICENSE SUMMARY
4 *
1f447808 5 * Copyright(c) 2008 - 2010 Intel Corporation. All rights reserved.
df48c323
TW
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of version 2 of the GNU General Public License as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
19 * USA
20 *
21 * The full GNU General Public License is included in this distribution
22 * in the file called LICENSE.GPL.
23 *
24 * Contact Information:
759ef89f 25 * Intel Linux Wireless <ilw@linux.intel.com>
df48c323
TW
26 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27 *****************************************************************************/
28
29#include <linux/kernel.h>
30#include <linux/module.h>
8ccde88a 31#include <linux/etherdevice.h>
d43c36dc 32#include <linux/sched.h>
5a0e3ad6 33#include <linux/slab.h>
1d0a082d 34#include <net/mac80211.h>
df48c323 35
6bc913bd 36#include "iwl-eeprom.h"
3e0d4cb1 37#include "iwl-dev.h" /* FIXME: remove */
19335774 38#include "iwl-debug.h"
df48c323 39#include "iwl-core.h"
b661c819 40#include "iwl-io.h"
5da4b55f 41#include "iwl-power.h"
83dde8c9 42#include "iwl-sta.h"
ef850d7c 43#include "iwl-helpers.h"
df48c323 44
1d0a082d 45
df48c323
TW
46MODULE_DESCRIPTION("iwl core");
47MODULE_VERSION(IWLWIFI_VERSION);
a7b75207 48MODULE_AUTHOR(DRV_COPYRIGHT " " DRV_AUTHOR);
712b6cf5 49MODULE_LICENSE("GPL");
df48c323 50
06702a73
WYG
51/*
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
57 *
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"
64 *
65 * default: bt_coex_active = true (BT_COEX_ENABLE)
66 */
670245ed
JB
67bool bt_coex_active = true;
68EXPORT_SYMBOL_GPL(bt_coex_active);
06702a73 69module_param(bt_coex_active, bool, S_IRUGO);
6c69d121 70MODULE_PARM_DESC(bt_coex_active, "enable wifi/bluetooth co-exist");
06702a73 71
c7de35cd
RR
72#define IWL_DECLARE_RATE_INFO(r, s, ip, in, rp, rn, pp, np) \
73 [IWL_RATE_##r##M_INDEX] = { IWL_RATE_##r##M_PLCP, \
74 IWL_RATE_SISO_##s##M_PLCP, \
75 IWL_RATE_MIMO2_##s##M_PLCP,\
76 IWL_RATE_MIMO3_##s##M_PLCP,\
77 IWL_RATE_##r##M_IEEE, \
78 IWL_RATE_##ip##M_INDEX, \
79 IWL_RATE_##in##M_INDEX, \
80 IWL_RATE_##rp##M_INDEX, \
81 IWL_RATE_##rn##M_INDEX, \
82 IWL_RATE_##pp##M_INDEX, \
83 IWL_RATE_##np##M_INDEX }
84
a562a9dd
RC
85u32 iwl_debug_level;
86EXPORT_SYMBOL(iwl_debug_level);
87
c7de35cd
RR
88/*
89 * Parameter order:
90 * rate, ht rate, prev rate, next rate, prev tgg rate, next tgg rate
91 *
92 * If there isn't a valid next or previous rate then INV is used which
93 * maps to IWL_RATE_INVALID
94 *
95 */
1826dcc0 96const struct iwl_rate_info iwl_rates[IWL_RATE_COUNT] = {
c7de35cd
RR
97 IWL_DECLARE_RATE_INFO(1, INV, INV, 2, INV, 2, INV, 2), /* 1mbps */
98 IWL_DECLARE_RATE_INFO(2, INV, 1, 5, 1, 5, 1, 5), /* 2mbps */
99 IWL_DECLARE_RATE_INFO(5, INV, 2, 6, 2, 11, 2, 11), /*5.5mbps */
100 IWL_DECLARE_RATE_INFO(11, INV, 9, 12, 9, 12, 5, 18), /* 11mbps */
101 IWL_DECLARE_RATE_INFO(6, 6, 5, 9, 5, 11, 5, 11), /* 6mbps */
102 IWL_DECLARE_RATE_INFO(9, 6, 6, 11, 6, 11, 5, 11), /* 9mbps */
103 IWL_DECLARE_RATE_INFO(12, 12, 11, 18, 11, 18, 11, 18), /* 12mbps */
104 IWL_DECLARE_RATE_INFO(18, 18, 12, 24, 12, 24, 11, 24), /* 18mbps */
105 IWL_DECLARE_RATE_INFO(24, 24, 18, 36, 18, 36, 18, 36), /* 24mbps */
106 IWL_DECLARE_RATE_INFO(36, 36, 24, 48, 24, 48, 24, 48), /* 36mbps */
107 IWL_DECLARE_RATE_INFO(48, 48, 36, 54, 36, 54, 36, 54), /* 48mbps */
108 IWL_DECLARE_RATE_INFO(54, 54, 48, INV, 48, INV, 48, INV),/* 54mbps */
109 IWL_DECLARE_RATE_INFO(60, 60, 48, INV, 48, INV, 48, INV),/* 60mbps */
110 /* FIXME:RS: ^^ should be INV (legacy) */
111};
1826dcc0 112EXPORT_SYMBOL(iwl_rates);
c7de35cd 113
e7d326ac
TW
114int iwl_hwrate_to_plcp_idx(u32 rate_n_flags)
115{
116 int idx = 0;
117
118 /* HT rate format */
119 if (rate_n_flags & RATE_MCS_HT_MSK) {
120 idx = (rate_n_flags & 0xff);
121
60d32215
DH
122 if (idx >= IWL_RATE_MIMO3_6M_PLCP)
123 idx = idx - IWL_RATE_MIMO3_6M_PLCP;
124 else if (idx >= IWL_RATE_MIMO2_6M_PLCP)
e7d326ac
TW
125 idx = idx - IWL_RATE_MIMO2_6M_PLCP;
126
127 idx += IWL_FIRST_OFDM_RATE;
128 /* skip 9M not supported in ht*/
129 if (idx >= IWL_RATE_9M_INDEX)
130 idx += 1;
131 if ((idx >= IWL_FIRST_OFDM_RATE) && (idx <= IWL_LAST_OFDM_RATE))
132 return idx;
133
134 /* legacy rate format, search for match in table */
135 } else {
136 for (idx = 0; idx < ARRAY_SIZE(iwl_rates); idx++)
137 if (iwl_rates[idx].plcp == (rate_n_flags & 0xFF))
138 return idx;
139 }
140
141 return -1;
142}
143EXPORT_SYMBOL(iwl_hwrate_to_plcp_idx);
144
0e1654fa 145u8 iwl_toggle_tx_ant(struct iwl_priv *priv, u8 ant, u8 valid)
76eff18b
TW
146{
147 int i;
148 u8 ind = ant;
0e1654fa 149
bd6e2d57
JB
150 if (priv->band == IEEE80211_BAND_2GHZ &&
151 priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH)
152 return 0;
153
76eff18b
TW
154 for (i = 0; i < RATE_ANT_NUM - 1; i++) {
155 ind = (ind + 1) < RATE_ANT_NUM ? ind + 1 : 0;
0e1654fa 156 if (valid & BIT(ind))
76eff18b
TW
157 return ind;
158 }
159 return ant;
160}
47ff65c4 161EXPORT_SYMBOL(iwl_toggle_tx_ant);
57bd1bea
TW
162
163const u8 iwl_bcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
164EXPORT_SYMBOL(iwl_bcast_addr);
165
166
1d0a082d
AK
167/* This function both allocates and initializes hw and priv. */
168struct ieee80211_hw *iwl_alloc_all(struct iwl_cfg *cfg,
169 struct ieee80211_ops *hw_ops)
170{
171 struct iwl_priv *priv;
172
173 /* mac80211 allocates memory for this device instance, including
174 * space for this driver's private structure */
175 struct ieee80211_hw *hw =
176 ieee80211_alloc_hw(sizeof(struct iwl_priv), hw_ops);
177 if (hw == NULL) {
c96c31e4 178 pr_err("%s: Can not allocate network device\n",
a3139c59 179 cfg->name);
1d0a082d
AK
180 goto out;
181 }
182
183 priv = hw->priv;
184 priv->hw = hw;
185
186out:
187 return hw;
188}
189EXPORT_SYMBOL(iwl_alloc_all);
190
14d2aac5
AK
191/*
192 * QoS support
193*/
8dfdb9d5 194static void iwl_update_qos(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
14d2aac5
AK
195{
196 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
197 return;
198
763cc3bf
JB
199 if (!ctx->is_active)
200 return;
201
8dfdb9d5 202 ctx->qos_data.def_qos_parm.qos_flags = 0;
14d2aac5 203
8dfdb9d5
JB
204 if (ctx->qos_data.qos_active)
205 ctx->qos_data.def_qos_parm.qos_flags |=
14d2aac5
AK
206 QOS_PARAM_FLG_UPDATE_EDCA_MSK;
207
7e6a5886 208 if (ctx->ht.enabled)
8dfdb9d5 209 ctx->qos_data.def_qos_parm.qos_flags |= QOS_PARAM_FLG_TGN_MSK;
14d2aac5 210
e61146e3 211 IWL_DEBUG_QOS(priv, "send QoS cmd with Qos active=%d FLAGS=0x%X\n",
8dfdb9d5
JB
212 ctx->qos_data.qos_active,
213 ctx->qos_data.def_qos_parm.qos_flags);
14d2aac5 214
8dfdb9d5 215 iwl_send_cmd_pdu_async(priv, ctx->qos_cmd,
e61146e3 216 sizeof(struct iwl_qosparam_cmd),
8dfdb9d5 217 &ctx->qos_data.def_qos_parm, NULL);
14d2aac5 218}
c7de35cd 219
d9fe60de
JB
220#define MAX_BIT_RATE_40_MHZ 150 /* Mbps */
221#define MAX_BIT_RATE_20_MHZ 72 /* Mbps */
c7de35cd 222static void iwlcore_init_ht_hw_capab(const struct iwl_priv *priv,
d9fe60de 223 struct ieee80211_sta_ht_cap *ht_info,
c7de35cd
RR
224 enum ieee80211_band band)
225{
39130df3
RR
226 u16 max_bit_rate = 0;
227 u8 rx_chains_num = priv->hw_params.rx_chains_num;
228 u8 tx_chains_num = priv->hw_params.tx_chains_num;
229
c7de35cd 230 ht_info->cap = 0;
d9fe60de 231 memset(&ht_info->mcs, 0, sizeof(ht_info->mcs));
c7de35cd 232
d9fe60de 233 ht_info->ht_supported = true;
c7de35cd 234
7cb1b088
WYG
235 if (priv->cfg->ht_params &&
236 priv->cfg->ht_params->ht_greenfield_support)
b261793d 237 ht_info->cap |= IEEE80211_HT_CAP_GRN_FLD;
d9fe60de 238 ht_info->cap |= IEEE80211_HT_CAP_SGI_20;
39130df3 239 max_bit_rate = MAX_BIT_RATE_20_MHZ;
7aafef1c 240 if (priv->hw_params.ht40_channel & BIT(band)) {
d9fe60de
JB
241 ht_info->cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
242 ht_info->cap |= IEEE80211_HT_CAP_SGI_40;
243 ht_info->mcs.rx_mask[4] = 0x01;
39130df3 244 max_bit_rate = MAX_BIT_RATE_40_MHZ;
c7de35cd 245 }
c7de35cd
RR
246
247 if (priv->cfg->mod_params->amsdu_size_8K)
d9fe60de 248 ht_info->cap |= IEEE80211_HT_CAP_MAX_AMSDU;
c7de35cd
RR
249
250 ht_info->ampdu_factor = CFG_HT_RX_AMPDU_FACTOR_DEF;
7cb1b088
WYG
251 if (priv->cfg->bt_params && priv->cfg->bt_params->ampdu_factor)
252 ht_info->ampdu_factor = priv->cfg->bt_params->ampdu_factor;
c7de35cd 253 ht_info->ampdu_density = CFG_HT_MPDU_DENSITY_DEF;
7cb1b088
WYG
254 if (priv->cfg->bt_params && priv->cfg->bt_params->ampdu_density)
255 ht_info->ampdu_density = priv->cfg->bt_params->ampdu_density;
c7de35cd 256
d9fe60de 257 ht_info->mcs.rx_mask[0] = 0xFF;
39130df3 258 if (rx_chains_num >= 2)
d9fe60de 259 ht_info->mcs.rx_mask[1] = 0xFF;
39130df3 260 if (rx_chains_num >= 3)
d9fe60de 261 ht_info->mcs.rx_mask[2] = 0xFF;
39130df3
RR
262
263 /* Highest supported Rx data rate */
264 max_bit_rate *= rx_chains_num;
d9fe60de
JB
265 WARN_ON(max_bit_rate & ~IEEE80211_HT_MCS_RX_HIGHEST_MASK);
266 ht_info->mcs.rx_highest = cpu_to_le16(max_bit_rate);
39130df3
RR
267
268 /* Tx MCS capabilities */
d9fe60de 269 ht_info->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
39130df3 270 if (tx_chains_num != rx_chains_num) {
d9fe60de
JB
271 ht_info->mcs.tx_params |= IEEE80211_HT_MCS_TX_RX_DIFF;
272 ht_info->mcs.tx_params |= ((tx_chains_num - 1) <<
273 IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT);
39130df3 274 }
c7de35cd 275}
c7de35cd 276
c7de35cd
RR
277/**
278 * iwlcore_init_geos - Initialize mac80211's geo/channel info based from eeprom
279 */
534166de 280int iwlcore_init_geos(struct iwl_priv *priv)
c7de35cd
RR
281{
282 struct iwl_channel_info *ch;
283 struct ieee80211_supported_band *sband;
284 struct ieee80211_channel *channels;
285 struct ieee80211_channel *geo_ch;
286 struct ieee80211_rate *rates;
287 int i = 0;
288
289 if (priv->bands[IEEE80211_BAND_2GHZ].n_bitrates ||
290 priv->bands[IEEE80211_BAND_5GHZ].n_bitrates) {
e1623446 291 IWL_DEBUG_INFO(priv, "Geography modes already initialized.\n");
c7de35cd
RR
292 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
293 return 0;
294 }
295
296 channels = kzalloc(sizeof(struct ieee80211_channel) *
297 priv->channel_count, GFP_KERNEL);
298 if (!channels)
299 return -ENOMEM;
300
5027309b 301 rates = kzalloc((sizeof(struct ieee80211_rate) * IWL_RATE_COUNT_LEGACY),
c7de35cd
RR
302 GFP_KERNEL);
303 if (!rates) {
304 kfree(channels);
305 return -ENOMEM;
306 }
307
308 /* 5.2GHz channels start after the 2.4GHz channels */
309 sband = &priv->bands[IEEE80211_BAND_5GHZ];
310 sband->channels = &channels[ARRAY_SIZE(iwl_eeprom_band_1)];
311 /* just OFDM */
312 sband->bitrates = &rates[IWL_FIRST_OFDM_RATE];
5027309b 313 sband->n_bitrates = IWL_RATE_COUNT_LEGACY - IWL_FIRST_OFDM_RATE;
c7de35cd 314
49779293 315 if (priv->cfg->sku & IWL_SKU_N)
d9fe60de 316 iwlcore_init_ht_hw_capab(priv, &sband->ht_cap,
49779293 317 IEEE80211_BAND_5GHZ);
c7de35cd
RR
318
319 sband = &priv->bands[IEEE80211_BAND_2GHZ];
320 sband->channels = channels;
321 /* OFDM & CCK */
322 sband->bitrates = rates;
5027309b 323 sband->n_bitrates = IWL_RATE_COUNT_LEGACY;
c7de35cd 324
49779293 325 if (priv->cfg->sku & IWL_SKU_N)
d9fe60de 326 iwlcore_init_ht_hw_capab(priv, &sband->ht_cap,
49779293 327 IEEE80211_BAND_2GHZ);
c7de35cd
RR
328
329 priv->ieee_channels = channels;
330 priv->ieee_rates = rates;
331
c7de35cd
RR
332 for (i = 0; i < priv->channel_count; i++) {
333 ch = &priv->channel_info[i];
334
335 /* FIXME: might be removed if scan is OK */
336 if (!is_channel_valid(ch))
337 continue;
338
339 if (is_channel_a_band(ch))
340 sband = &priv->bands[IEEE80211_BAND_5GHZ];
341 else
342 sband = &priv->bands[IEEE80211_BAND_2GHZ];
343
344 geo_ch = &sband->channels[sband->n_channels++];
345
346 geo_ch->center_freq =
347 ieee80211_channel_to_frequency(ch->channel);
348 geo_ch->max_power = ch->max_power_avg;
349 geo_ch->max_antenna_gain = 0xff;
350 geo_ch->hw_value = ch->channel;
351
352 if (is_channel_valid(ch)) {
353 if (!(ch->flags & EEPROM_CHANNEL_IBSS))
354 geo_ch->flags |= IEEE80211_CHAN_NO_IBSS;
355
356 if (!(ch->flags & EEPROM_CHANNEL_ACTIVE))
357 geo_ch->flags |= IEEE80211_CHAN_PASSIVE_SCAN;
358
359 if (ch->flags & EEPROM_CHANNEL_RADAR)
360 geo_ch->flags |= IEEE80211_CHAN_RADAR;
361
7aafef1c 362 geo_ch->flags |= ch->ht40_extension_channel;
4d38c2e8 363
dc1b0973
WYG
364 if (ch->max_power_avg > priv->tx_power_device_lmt)
365 priv->tx_power_device_lmt = ch->max_power_avg;
c7de35cd
RR
366 } else {
367 geo_ch->flags |= IEEE80211_CHAN_DISABLED;
368 }
369
e1623446 370 IWL_DEBUG_INFO(priv, "Channel %d Freq=%d[%sGHz] %s flag=0x%X\n",
c7de35cd
RR
371 ch->channel, geo_ch->center_freq,
372 is_channel_a_band(ch) ? "5.2" : "2.4",
373 geo_ch->flags & IEEE80211_CHAN_DISABLED ?
374 "restricted" : "valid",
375 geo_ch->flags);
376 }
377
378 if ((priv->bands[IEEE80211_BAND_5GHZ].n_channels == 0) &&
379 priv->cfg->sku & IWL_SKU_A) {
978785a3
TW
380 IWL_INFO(priv, "Incorrectly detected BG card as ABG. "
381 "Please send your PCI ID 0x%04X:0x%04X to maintainer.\n",
a3139c59
SO
382 priv->pci_dev->device,
383 priv->pci_dev->subsystem_device);
c7de35cd
RR
384 priv->cfg->sku &= ~IWL_SKU_A;
385 }
386
978785a3 387 IWL_INFO(priv, "Tunable channels: %d 802.11bg, %d 802.11a channels\n",
a3139c59
SO
388 priv->bands[IEEE80211_BAND_2GHZ].n_channels,
389 priv->bands[IEEE80211_BAND_5GHZ].n_channels);
c7de35cd
RR
390
391 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
392
393 return 0;
394}
534166de 395EXPORT_SYMBOL(iwlcore_init_geos);
c7de35cd
RR
396
397/*
398 * iwlcore_free_geos - undo allocations in iwlcore_init_geos
399 */
534166de 400void iwlcore_free_geos(struct iwl_priv *priv)
c7de35cd
RR
401{
402 kfree(priv->ieee_channels);
403 kfree(priv->ieee_rates);
404 clear_bit(STATUS_GEO_CONFIGURED, &priv->status);
405}
534166de 406EXPORT_SYMBOL(iwlcore_free_geos);
c7de35cd 407
37dc70fe 408/*
94597ab2 409 * iwlcore_tx_cmd_protection: Set rts/cts. 3945 and 4965 only share this
37dc70fe
AK
410 * function.
411 */
94597ab2
JB
412void iwlcore_tx_cmd_protection(struct iwl_priv *priv,
413 struct ieee80211_tx_info *info,
414 __le16 fc, __le32 *tx_flags)
37dc70fe
AK
415{
416 if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS) {
417 *tx_flags |= TX_CMD_FLG_RTS_MSK;
418 *tx_flags &= ~TX_CMD_FLG_CTS_MSK;
94597ab2
JB
419 *tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
420
421 if (!ieee80211_is_mgmt(fc))
422 return;
423
424 switch (fc & cpu_to_le16(IEEE80211_FCTL_STYPE)) {
425 case cpu_to_le16(IEEE80211_STYPE_AUTH):
426 case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
427 case cpu_to_le16(IEEE80211_STYPE_ASSOC_REQ):
428 case cpu_to_le16(IEEE80211_STYPE_REASSOC_REQ):
429 *tx_flags &= ~TX_CMD_FLG_RTS_MSK;
430 *tx_flags |= TX_CMD_FLG_CTS_MSK;
431 break;
432 }
37dc70fe
AK
433 } else if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
434 *tx_flags &= ~TX_CMD_FLG_RTS_MSK;
435 *tx_flags |= TX_CMD_FLG_CTS_MSK;
94597ab2 436 *tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
37dc70fe
AK
437 }
438}
94597ab2
JB
439EXPORT_SYMBOL(iwlcore_tx_cmd_protection);
440
37dc70fe 441
7e6a5886
JB
442static bool iwl_is_channel_extension(struct iwl_priv *priv,
443 enum ieee80211_band band,
444 u16 channel, u8 extension_chan_offset)
47c5196e
TW
445{
446 const struct iwl_channel_info *ch_info;
447
448 ch_info = iwl_get_channel_info(priv, band, channel);
449 if (!is_channel_valid(ch_info))
7e6a5886 450 return false;
47c5196e 451
d9fe60de 452 if (extension_chan_offset == IEEE80211_HT_PARAM_CHA_SEC_ABOVE)
7aafef1c 453 return !(ch_info->ht40_extension_channel &
689da1b3 454 IEEE80211_CHAN_NO_HT40PLUS);
d9fe60de 455 else if (extension_chan_offset == IEEE80211_HT_PARAM_CHA_SEC_BELOW)
7aafef1c 456 return !(ch_info->ht40_extension_channel &
689da1b3 457 IEEE80211_CHAN_NO_HT40MINUS);
47c5196e 458
7e6a5886 459 return false;
47c5196e
TW
460}
461
7e6a5886
JB
462bool iwl_is_ht40_tx_allowed(struct iwl_priv *priv,
463 struct iwl_rxon_context *ctx,
464 struct ieee80211_sta_ht_cap *ht_cap)
47c5196e 465{
7e6a5886
JB
466 if (!ctx->ht.enabled || !ctx->ht.is_40mhz)
467 return false;
47c5196e 468
7e6a5886
JB
469 /*
470 * We do not check for IEEE80211_HT_CAP_SUP_WIDTH_20_40
a2b0f02e
WYG
471 * the bit will not set if it is pure 40MHz case
472 */
7e6a5886
JB
473 if (ht_cap && !ht_cap->ht_supported)
474 return false;
475
d73e4923 476#ifdef CONFIG_IWLWIFI_DEBUGFS
1e4247d4 477 if (priv->disable_ht40)
7e6a5886 478 return false;
1e4247d4 479#endif
7e6a5886 480
611d3eb7 481 return iwl_is_channel_extension(priv, priv->band,
246ed355 482 le16_to_cpu(ctx->staging.channel),
7e6a5886 483 ctx->ht.extension_chan_offset);
47c5196e 484}
7aafef1c 485EXPORT_SYMBOL(iwl_is_ht40_tx_allowed);
47c5196e 486
2c2f3b33
TW
487static u16 iwl_adjust_beacon_interval(u16 beacon_val, u16 max_beacon_val)
488{
ea196fdb
JB
489 u16 new_val;
490 u16 beacon_factor;
491
492 /*
493 * If mac80211 hasn't given us a beacon interval, program
494 * the default into the device (not checking this here
495 * would cause the adjustment below to return the maximum
496 * value, which may break PAN.)
497 */
498 if (!beacon_val)
499 return DEFAULT_BEACON_INTERVAL;
500
501 /*
502 * If the beacon interval we obtained from the peer
503 * is too large, we'll have to wake up more often
504 * (and in IBSS case, we'll beacon too much)
505 *
506 * For example, if max_beacon_val is 4096, and the
507 * requested beacon interval is 7000, we'll have to
508 * use 3500 to be able to wake up on the beacons.
509 *
510 * This could badly influence beacon detection stats.
511 */
2c2f3b33
TW
512
513 beacon_factor = (beacon_val + max_beacon_val) / max_beacon_val;
514 new_val = beacon_val / beacon_factor;
515
516 if (!new_val)
517 new_val = max_beacon_val;
518
519 return new_val;
520}
521
47313e34 522int iwl_send_rxon_timing(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
2c2f3b33
TW
523{
524 u64 tsf;
525 s32 interval_tm, rem;
2c2f3b33
TW
526 struct ieee80211_conf *conf = NULL;
527 u16 beacon_int;
47313e34 528 struct ieee80211_vif *vif = ctx->vif;
2c2f3b33
TW
529
530 conf = ieee80211_get_hw_conf(priv->hw);
531
948f5a2f
JB
532 lockdep_assert_held(&priv->mutex);
533
246ed355 534 memset(&ctx->timing, 0, sizeof(struct iwl_rxon_time_cmd));
948f5a2f 535
246ed355
JB
536 ctx->timing.timestamp = cpu_to_le64(priv->timestamp);
537 ctx->timing.listen_interval = cpu_to_le16(conf->listen_interval);
2c2f3b33 538
47313e34 539 beacon_int = vif ? vif->bss_conf.beacon_int : 0;
2c2f3b33 540
47313e34
JB
541 /*
542 * TODO: For IBSS we need to get atim_window from mac80211,
543 * for now just always use 0
544 */
545 ctx->timing.atim_window = 0;
2c2f3b33 546
bde4530e 547 if (ctx->ctxid == IWL_RXON_CTX_PAN &&
f1f270b2
JB
548 (!ctx->vif || ctx->vif->type != NL80211_IFTYPE_STATION) &&
549 iwl_is_associated(priv, IWL_RXON_CTX_BSS) &&
550 priv->contexts[IWL_RXON_CTX_BSS].vif &&
551 priv->contexts[IWL_RXON_CTX_BSS].vif->bss_conf.beacon_int) {
bde4530e
JB
552 ctx->timing.beacon_interval =
553 priv->contexts[IWL_RXON_CTX_BSS].timing.beacon_interval;
554 beacon_int = le16_to_cpu(ctx->timing.beacon_interval);
f1f270b2
JB
555 } else if (ctx->ctxid == IWL_RXON_CTX_BSS &&
556 iwl_is_associated(priv, IWL_RXON_CTX_PAN) &&
557 priv->contexts[IWL_RXON_CTX_PAN].vif &&
558 priv->contexts[IWL_RXON_CTX_PAN].vif->bss_conf.beacon_int &&
559 (!iwl_is_associated_ctx(ctx) || !ctx->vif ||
560 !ctx->vif->bss_conf.beacon_int)) {
561 ctx->timing.beacon_interval =
562 priv->contexts[IWL_RXON_CTX_PAN].timing.beacon_interval;
563 beacon_int = le16_to_cpu(ctx->timing.beacon_interval);
bde4530e
JB
564 } else {
565 beacon_int = iwl_adjust_beacon_interval(beacon_int,
f8525e55 566 priv->hw_params.max_beacon_itrvl * TIME_UNIT);
bde4530e
JB
567 ctx->timing.beacon_interval = cpu_to_le16(beacon_int);
568 }
2c2f3b33
TW
569
570 tsf = priv->timestamp; /* tsf is modifed by do_div: copy it */
f8525e55 571 interval_tm = beacon_int * TIME_UNIT;
2c2f3b33 572 rem = do_div(tsf, interval_tm);
246ed355 573 ctx->timing.beacon_init_val = cpu_to_le32(interval_tm - rem);
2c2f3b33 574
47313e34 575 ctx->timing.dtim_period = vif ? (vif->bss_conf.dtim_period ?: 1) : 1;
2491fa42 576
2c2f3b33
TW
577 IWL_DEBUG_ASSOC(priv,
578 "beacon interval %d beacon timer %d beacon tim %d\n",
246ed355
JB
579 le16_to_cpu(ctx->timing.beacon_interval),
580 le32_to_cpu(ctx->timing.beacon_init_val),
581 le16_to_cpu(ctx->timing.atim_window));
948f5a2f 582
8f2d3d2a 583 return iwl_send_cmd_pdu(priv, ctx->rxon_timing_cmd,
246ed355 584 sizeof(ctx->timing), &ctx->timing);
2c2f3b33 585}
948f5a2f 586EXPORT_SYMBOL(iwl_send_rxon_timing);
2c2f3b33 587
246ed355
JB
588void iwl_set_rxon_hwcrypto(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
589 int hw_decrypt)
8ccde88a 590{
246ed355 591 struct iwl_rxon_cmd *rxon = &ctx->staging;
8ccde88a
SO
592
593 if (hw_decrypt)
594 rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
595 else
596 rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
597
598}
599EXPORT_SYMBOL(iwl_set_rxon_hwcrypto);
600
601/**
602 * iwl_check_rxon_cmd - validate RXON structure is valid
603 *
604 * NOTE: This is really only useful during development and can eventually
605 * be #ifdef'd out once the driver is stable and folks aren't actively
606 * making changes
607 */
246ed355 608int iwl_check_rxon_cmd(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
8ccde88a
SO
609{
610 int error = 0;
611 int counter = 1;
246ed355 612 struct iwl_rxon_cmd *rxon = &ctx->staging;
8ccde88a
SO
613
614 if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
615 error |= le32_to_cpu(rxon->flags &
616 (RXON_FLG_TGJ_NARROW_BAND_MSK |
617 RXON_FLG_RADAR_DETECT_MSK));
618 if (error)
619 IWL_WARN(priv, "check 24G fields %d | %d\n",
620 counter++, error);
621 } else {
622 error |= (rxon->flags & RXON_FLG_SHORT_SLOT_MSK) ?
623 0 : le32_to_cpu(RXON_FLG_SHORT_SLOT_MSK);
624 if (error)
625 IWL_WARN(priv, "check 52 fields %d | %d\n",
626 counter++, error);
627 error |= le32_to_cpu(rxon->flags & RXON_FLG_CCK_MSK);
628 if (error)
629 IWL_WARN(priv, "check 52 CCK %d | %d\n",
630 counter++, error);
631 }
632 error |= (rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1;
633 if (error)
634 IWL_WARN(priv, "check mac addr %d | %d\n", counter++, error);
635
636 /* make sure basic rates 6Mbps and 1Mbps are supported */
637 error |= (((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0) &&
638 ((rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0));
639 if (error)
640 IWL_WARN(priv, "check basic rate %d | %d\n", counter++, error);
641
642 error |= (le16_to_cpu(rxon->assoc_id) > 2007);
643 if (error)
644 IWL_WARN(priv, "check assoc id %d | %d\n", counter++, error);
645
646 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
647 == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK));
648 if (error)
649 IWL_WARN(priv, "check CCK and short slot %d | %d\n",
650 counter++, error);
651
652 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
653 == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK));
654 if (error)
655 IWL_WARN(priv, "check CCK & auto detect %d | %d\n",
656 counter++, error);
657
658 error |= ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
659 RXON_FLG_TGG_PROTECT_MSK)) == RXON_FLG_TGG_PROTECT_MSK);
660 if (error)
661 IWL_WARN(priv, "check TGG and auto detect %d | %d\n",
662 counter++, error);
663
664 if (error)
665 IWL_WARN(priv, "Tuning to channel %d\n",
666 le16_to_cpu(rxon->channel));
667
668 if (error) {
669 IWL_ERR(priv, "Not a valid iwl_rxon_assoc_cmd field values\n");
670 return -1;
671 }
672 return 0;
673}
674EXPORT_SYMBOL(iwl_check_rxon_cmd);
675
676/**
677 * iwl_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
678 * @priv: staging_rxon is compared to active_rxon
679 *
680 * If the RXON structure is changing enough to require a new tune,
681 * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
682 * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
683 */
246ed355
JB
684int iwl_full_rxon_required(struct iwl_priv *priv,
685 struct iwl_rxon_context *ctx)
8ccde88a 686{
246ed355
JB
687 const struct iwl_rxon_cmd *staging = &ctx->staging;
688 const struct iwl_rxon_cmd *active = &ctx->active;
689
690#define CHK(cond) \
691 if ((cond)) { \
692 IWL_DEBUG_INFO(priv, "need full RXON - " #cond "\n"); \
693 return 1; \
694 }
695
696#define CHK_NEQ(c1, c2) \
697 if ((c1) != (c2)) { \
698 IWL_DEBUG_INFO(priv, "need full RXON - " \
699 #c1 " != " #c2 " - %d != %d\n", \
700 (c1), (c2)); \
701 return 1; \
702 }
8ccde88a
SO
703
704 /* These items are only settable from the full RXON command */
246ed355
JB
705 CHK(!iwl_is_associated_ctx(ctx));
706 CHK(compare_ether_addr(staging->bssid_addr, active->bssid_addr));
707 CHK(compare_ether_addr(staging->node_addr, active->node_addr));
708 CHK(compare_ether_addr(staging->wlap_bssid_addr,
709 active->wlap_bssid_addr));
710 CHK_NEQ(staging->dev_type, active->dev_type);
711 CHK_NEQ(staging->channel, active->channel);
712 CHK_NEQ(staging->air_propagation, active->air_propagation);
713 CHK_NEQ(staging->ofdm_ht_single_stream_basic_rates,
714 active->ofdm_ht_single_stream_basic_rates);
715 CHK_NEQ(staging->ofdm_ht_dual_stream_basic_rates,
716 active->ofdm_ht_dual_stream_basic_rates);
717 CHK_NEQ(staging->ofdm_ht_triple_stream_basic_rates,
718 active->ofdm_ht_triple_stream_basic_rates);
719 CHK_NEQ(staging->assoc_id, active->assoc_id);
8ccde88a
SO
720
721 /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
722 * be updated with the RXON_ASSOC command -- however only some
723 * flag transitions are allowed using RXON_ASSOC */
724
725 /* Check if we are not switching bands */
246ed355
JB
726 CHK_NEQ(staging->flags & RXON_FLG_BAND_24G_MSK,
727 active->flags & RXON_FLG_BAND_24G_MSK);
8ccde88a
SO
728
729 /* Check if we are switching association toggle */
246ed355
JB
730 CHK_NEQ(staging->filter_flags & RXON_FILTER_ASSOC_MSK,
731 active->filter_flags & RXON_FILTER_ASSOC_MSK);
732
733#undef CHK
734#undef CHK_NEQ
8ccde88a
SO
735
736 return 0;
737}
738EXPORT_SYMBOL(iwl_full_rxon_required);
739
76d04815
JB
740u8 iwl_rate_get_lowest_plcp(struct iwl_priv *priv,
741 struct iwl_rxon_context *ctx)
8ccde88a 742{
4a02886b
JB
743 /*
744 * Assign the lowest rate -- should really get this from
745 * the beacon skb from mac80211.
746 */
246ed355 747 if (ctx->staging.flags & RXON_FLG_BAND_24G_MSK)
8ccde88a
SO
748 return IWL_RATE_1M_PLCP;
749 else
750 return IWL_RATE_6M_PLCP;
751}
752EXPORT_SYMBOL(iwl_rate_get_lowest_plcp);
753
246ed355
JB
754static void _iwl_set_rxon_ht(struct iwl_priv *priv,
755 struct iwl_ht_config *ht_conf,
756 struct iwl_rxon_context *ctx)
47c5196e 757{
246ed355 758 struct iwl_rxon_cmd *rxon = &ctx->staging;
47c5196e 759
7e6a5886 760 if (!ctx->ht.enabled) {
a2b0f02e 761 rxon->flags &= ~(RXON_FLG_CHANNEL_MODE_MSK |
42eb7c64 762 RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK |
7aafef1c 763 RXON_FLG_HT40_PROT_MSK |
42eb7c64 764 RXON_FLG_HT_PROT_MSK);
47c5196e 765 return;
42eb7c64 766 }
47c5196e 767
7e6a5886 768 /* FIXME: if the definition of ht.protection changed, the "translation"
a2b0f02e
WYG
769 * will be needed for rxon->flags
770 */
7e6a5886 771 rxon->flags |= cpu_to_le32(ctx->ht.protection << RXON_FLG_HT_OPERATING_MODE_POS);
a2b0f02e
WYG
772
773 /* Set up channel bandwidth:
7aafef1c 774 * 20 MHz only, 20/40 mixed or pure 40 if ht40 ok */
a2b0f02e
WYG
775 /* clear the HT channel mode before set the mode */
776 rxon->flags &= ~(RXON_FLG_CHANNEL_MODE_MSK |
777 RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK);
7e6a5886 778 if (iwl_is_ht40_tx_allowed(priv, ctx, NULL)) {
7aafef1c 779 /* pure ht40 */
7e6a5886 780 if (ctx->ht.protection == IEEE80211_HT_OP_MODE_PROTECTION_20MHZ) {
a2b0f02e 781 rxon->flags |= RXON_FLG_CHANNEL_MODE_PURE_40;
508b08e7 782 /* Note: control channel is opposite of extension channel */
7e6a5886 783 switch (ctx->ht.extension_chan_offset) {
508b08e7
WYG
784 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
785 rxon->flags &= ~RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
786 break;
787 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
788 rxon->flags |= RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
789 break;
790 }
791 } else {
a2b0f02e 792 /* Note: control channel is opposite of extension channel */
7e6a5886 793 switch (ctx->ht.extension_chan_offset) {
a2b0f02e
WYG
794 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
795 rxon->flags &= ~(RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK);
796 rxon->flags |= RXON_FLG_CHANNEL_MODE_MIXED;
797 break;
798 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
799 rxon->flags |= RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
800 rxon->flags |= RXON_FLG_CHANNEL_MODE_MIXED;
801 break;
802 case IEEE80211_HT_PARAM_CHA_SEC_NONE:
803 default:
804 /* channel location only valid if in Mixed mode */
805 IWL_ERR(priv, "invalid extension channel offset\n");
806 break;
807 }
808 }
809 } else {
810 rxon->flags |= RXON_FLG_CHANNEL_MODE_LEGACY;
47c5196e
TW
811 }
812
45823531 813 if (priv->cfg->ops->hcmd->set_rxon_chain)
246ed355 814 priv->cfg->ops->hcmd->set_rxon_chain(priv, ctx);
47c5196e 815
02bb1bea 816 IWL_DEBUG_ASSOC(priv, "rxon flags 0x%X operation mode :0x%X "
ae5eb026 817 "extension channel offset 0x%x\n",
7e6a5886
JB
818 le32_to_cpu(rxon->flags), ctx->ht.protection,
819 ctx->ht.extension_chan_offset);
47c5196e 820}
246ed355
JB
821
822void iwl_set_rxon_ht(struct iwl_priv *priv, struct iwl_ht_config *ht_conf)
823{
824 struct iwl_rxon_context *ctx;
825
826 for_each_context(priv, ctx)
827 _iwl_set_rxon_ht(priv, ht_conf, ctx);
828}
47c5196e
TW
829EXPORT_SYMBOL(iwl_set_rxon_ht);
830
246ed355 831/* Return valid, unused, channel for a passive scan to reset the RF */
14023641 832u8 iwl_get_single_channel_number(struct iwl_priv *priv,
246ed355 833 enum ieee80211_band band)
14023641
AK
834{
835 const struct iwl_channel_info *ch_info;
836 int i;
837 u8 channel = 0;
246ed355
JB
838 u8 min, max;
839 struct iwl_rxon_context *ctx;
14023641 840
14023641 841 if (band == IEEE80211_BAND_5GHZ) {
246ed355
JB
842 min = 14;
843 max = priv->channel_count;
14023641 844 } else {
246ed355
JB
845 min = 0;
846 max = 14;
847 }
848
849 for (i = min; i < max; i++) {
850 bool busy = false;
851
852 for_each_context(priv, ctx) {
853 busy = priv->channel_info[i].channel ==
854 le16_to_cpu(ctx->staging.channel);
855 if (busy)
856 break;
14023641 857 }
246ed355
JB
858
859 if (busy)
860 continue;
861
862 channel = priv->channel_info[i].channel;
863 ch_info = iwl_get_channel_info(priv, band, channel);
864 if (is_channel_valid(ch_info))
865 break;
14023641
AK
866 }
867
868 return channel;
869}
870EXPORT_SYMBOL(iwl_get_single_channel_number);
871
bf85ea4f 872/**
3edb5fd6
SZ
873 * iwl_set_rxon_channel - Set the band and channel values in staging RXON
874 * @ch: requested channel as a pointer to struct ieee80211_channel
bf85ea4f 875
bf85ea4f 876 * NOTE: Does not commit to the hardware; it sets appropriate bit fields
3edb5fd6 877 * in the staging RXON flag structure based on the ch->band
bf85ea4f 878 */
246ed355
JB
879int iwl_set_rxon_channel(struct iwl_priv *priv, struct ieee80211_channel *ch,
880 struct iwl_rxon_context *ctx)
bf85ea4f 881{
17e72782 882 enum ieee80211_band band = ch->band;
81e95430 883 u16 channel = ch->hw_value;
17e72782 884
246ed355 885 if ((le16_to_cpu(ctx->staging.channel) == channel) &&
bf85ea4f
AK
886 (priv->band == band))
887 return 0;
888
246ed355 889 ctx->staging.channel = cpu_to_le16(channel);
bf85ea4f 890 if (band == IEEE80211_BAND_5GHZ)
246ed355 891 ctx->staging.flags &= ~RXON_FLG_BAND_24G_MSK;
bf85ea4f 892 else
246ed355 893 ctx->staging.flags |= RXON_FLG_BAND_24G_MSK;
bf85ea4f
AK
894
895 priv->band = band;
896
e1623446 897 IWL_DEBUG_INFO(priv, "Staging channel set to %d [%d]\n", channel, band);
bf85ea4f
AK
898
899 return 0;
900}
c7de35cd 901EXPORT_SYMBOL(iwl_set_rxon_channel);
bf85ea4f 902
79d07325 903void iwl_set_flags_for_band(struct iwl_priv *priv,
246ed355 904 struct iwl_rxon_context *ctx,
79d07325
WYG
905 enum ieee80211_band band,
906 struct ieee80211_vif *vif)
8ccde88a
SO
907{
908 if (band == IEEE80211_BAND_5GHZ) {
246ed355 909 ctx->staging.flags &=
8ccde88a
SO
910 ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
911 | RXON_FLG_CCK_MSK);
246ed355 912 ctx->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
8ccde88a
SO
913 } else {
914 /* Copied from iwl_post_associate() */
c213d745 915 if (vif && vif->bss_conf.use_short_slot)
246ed355 916 ctx->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
8ccde88a 917 else
246ed355 918 ctx->staging.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
8ccde88a 919
246ed355
JB
920 ctx->staging.flags |= RXON_FLG_BAND_24G_MSK;
921 ctx->staging.flags |= RXON_FLG_AUTO_DETECT_MSK;
922 ctx->staging.flags &= ~RXON_FLG_CCK_MSK;
8ccde88a
SO
923 }
924}
79d07325 925EXPORT_SYMBOL(iwl_set_flags_for_band);
8ccde88a
SO
926
927/*
928 * initialize rxon structure with default values from eeprom
929 */
1dda6d28 930void iwl_connection_init_rx_config(struct iwl_priv *priv,
d0fe478c 931 struct iwl_rxon_context *ctx)
8ccde88a
SO
932{
933 const struct iwl_channel_info *ch_info;
934
246ed355 935 memset(&ctx->staging, 0, sizeof(ctx->staging));
8ccde88a 936
d0fe478c
JB
937 if (!ctx->vif) {
938 ctx->staging.dev_type = ctx->unused_devtype;
939 } else switch (ctx->vif->type) {
8ccde88a 940 case NL80211_IFTYPE_AP:
d0fe478c 941 ctx->staging.dev_type = ctx->ap_devtype;
8ccde88a
SO
942 break;
943
944 case NL80211_IFTYPE_STATION:
d0fe478c 945 ctx->staging.dev_type = ctx->station_devtype;
246ed355 946 ctx->staging.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
8ccde88a
SO
947 break;
948
949 case NL80211_IFTYPE_ADHOC:
d0fe478c 950 ctx->staging.dev_type = ctx->ibss_devtype;
246ed355
JB
951 ctx->staging.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
952 ctx->staging.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
8ccde88a
SO
953 RXON_FILTER_ACCEPT_GRP_MSK;
954 break;
955
8ccde88a 956 default:
d0fe478c
JB
957 IWL_ERR(priv, "Unsupported interface type %d\n",
958 ctx->vif->type);
8ccde88a
SO
959 break;
960 }
961
962#if 0
963 /* TODO: Figure out when short_preamble would be set and cache from
964 * that */
965 if (!hw_to_local(priv->hw)->short_preamble)
246ed355 966 ctx->staging.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
8ccde88a 967 else
246ed355 968 ctx->staging.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
8ccde88a
SO
969#endif
970
971 ch_info = iwl_get_channel_info(priv, priv->band,
246ed355 972 le16_to_cpu(ctx->active.channel));
8ccde88a
SO
973
974 if (!ch_info)
975 ch_info = &priv->channel_info[0];
976
246ed355 977 ctx->staging.channel = cpu_to_le16(ch_info->channel);
8ccde88a
SO
978 priv->band = ch_info->band;
979
d0fe478c 980 iwl_set_flags_for_band(priv, ctx, priv->band, ctx->vif);
8ccde88a 981
246ed355 982 ctx->staging.ofdm_basic_rates =
8ccde88a 983 (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
246ed355 984 ctx->staging.cck_basic_rates =
8ccde88a
SO
985 (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
986
a2b0f02e 987 /* clear both MIX and PURE40 mode flag */
246ed355 988 ctx->staging.flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED |
a2b0f02e 989 RXON_FLG_CHANNEL_MODE_PURE_40);
d0fe478c
JB
990 if (ctx->vif)
991 memcpy(ctx->staging.node_addr, ctx->vif->addr, ETH_ALEN);
7684c408 992
246ed355
JB
993 ctx->staging.ofdm_ht_single_stream_basic_rates = 0xff;
994 ctx->staging.ofdm_ht_dual_stream_basic_rates = 0xff;
995 ctx->staging.ofdm_ht_triple_stream_basic_rates = 0xff;
8ccde88a
SO
996}
997EXPORT_SYMBOL(iwl_connection_init_rx_config);
998
79d07325 999void iwl_set_rate(struct iwl_priv *priv)
8ccde88a
SO
1000{
1001 const struct ieee80211_supported_band *hw = NULL;
1002 struct ieee80211_rate *rate;
246ed355 1003 struct iwl_rxon_context *ctx;
8ccde88a
SO
1004 int i;
1005
1006 hw = iwl_get_hw_mode(priv, priv->band);
1007 if (!hw) {
1008 IWL_ERR(priv, "Failed to set rate: unable to get hw mode\n");
1009 return;
1010 }
1011
1012 priv->active_rate = 0;
8ccde88a
SO
1013
1014 for (i = 0; i < hw->n_bitrates; i++) {
1015 rate = &(hw->bitrates[i]);
5027309b 1016 if (rate->hw_value < IWL_RATE_COUNT_LEGACY)
8ccde88a
SO
1017 priv->active_rate |= (1 << rate->hw_value);
1018 }
1019
4a02886b 1020 IWL_DEBUG_RATE(priv, "Set active_rate = %0x\n", priv->active_rate);
8ccde88a 1021
246ed355
JB
1022 for_each_context(priv, ctx) {
1023 ctx->staging.cck_basic_rates =
1024 (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
4a02886b 1025
246ed355
JB
1026 ctx->staging.ofdm_basic_rates =
1027 (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
1028 }
8ccde88a 1029}
79d07325
WYG
1030EXPORT_SYMBOL(iwl_set_rate);
1031
1032void iwl_chswitch_done(struct iwl_priv *priv, bool is_success)
1033{
8bd413e6
JB
1034 /*
1035 * MULTI-FIXME
1036 * See iwl_mac_channel_switch.
1037 */
1038 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
1039
79d07325
WYG
1040 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1041 return;
1042
1043 if (priv->switch_rxon.switch_in_progress) {
8bd413e6 1044 ieee80211_chswitch_done(ctx->vif, is_success);
79d07325
WYG
1045 mutex_lock(&priv->mutex);
1046 priv->switch_rxon.switch_in_progress = false;
1047 mutex_unlock(&priv->mutex);
1048 }
1049}
1050EXPORT_SYMBOL(iwl_chswitch_done);
8ccde88a
SO
1051
1052void iwl_rx_csa(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb)
1053{
2f301227 1054 struct iwl_rx_packet *pkt = rxb_addr(rxb);
8ccde88a 1055 struct iwl_csa_notification *csa = &(pkt->u.csa_notif);
8bd413e6
JB
1056 /*
1057 * MULTI-FIXME
1058 * See iwl_mac_channel_switch.
1059 */
246ed355 1060 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
246ed355 1061 struct iwl_rxon_cmd *rxon = (void *)&ctx->active;
4a56e965 1062
0924e519
WYG
1063 if (priv->switch_rxon.switch_in_progress) {
1064 if (!le32_to_cpu(csa->status) &&
1065 (csa->channel == priv->switch_rxon.channel)) {
1066 rxon->channel = csa->channel;
246ed355 1067 ctx->staging.channel = csa->channel;
0924e519
WYG
1068 IWL_DEBUG_11H(priv, "CSA notif: channel %d\n",
1069 le16_to_cpu(csa->channel));
79d07325
WYG
1070 iwl_chswitch_done(priv, true);
1071 } else {
0924e519
WYG
1072 IWL_ERR(priv, "CSA notif (fail) : channel %d\n",
1073 le16_to_cpu(csa->channel));
79d07325
WYG
1074 iwl_chswitch_done(priv, false);
1075 }
0924e519 1076 }
8ccde88a
SO
1077}
1078EXPORT_SYMBOL(iwl_rx_csa);
1079
1080#ifdef CONFIG_IWLWIFI_DEBUG
246ed355
JB
1081void iwl_print_rx_config_cmd(struct iwl_priv *priv,
1082 struct iwl_rxon_context *ctx)
8ccde88a 1083{
246ed355 1084 struct iwl_rxon_cmd *rxon = &ctx->staging;
8ccde88a 1085
e1623446 1086 IWL_DEBUG_RADIO(priv, "RX CONFIG:\n");
3d816c77 1087 iwl_print_hex_dump(priv, IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
e1623446
TW
1088 IWL_DEBUG_RADIO(priv, "u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
1089 IWL_DEBUG_RADIO(priv, "u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
1090 IWL_DEBUG_RADIO(priv, "u32 filter_flags: 0x%08x\n",
8ccde88a 1091 le32_to_cpu(rxon->filter_flags));
e1623446
TW
1092 IWL_DEBUG_RADIO(priv, "u8 dev_type: 0x%x\n", rxon->dev_type);
1093 IWL_DEBUG_RADIO(priv, "u8 ofdm_basic_rates: 0x%02x\n",
8ccde88a 1094 rxon->ofdm_basic_rates);
e1623446
TW
1095 IWL_DEBUG_RADIO(priv, "u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
1096 IWL_DEBUG_RADIO(priv, "u8[6] node_addr: %pM\n", rxon->node_addr);
1097 IWL_DEBUG_RADIO(priv, "u8[6] bssid_addr: %pM\n", rxon->bssid_addr);
1098 IWL_DEBUG_RADIO(priv, "u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
8ccde88a 1099}
a643565e 1100EXPORT_SYMBOL(iwl_print_rx_config_cmd);
6686d17e 1101#endif
8ccde88a
SO
1102/**
1103 * iwl_irq_handle_error - called for HW or SW error interrupt from card
1104 */
1105void iwl_irq_handle_error(struct iwl_priv *priv)
1106{
1107 /* Set the FW error flag -- cleared on iwl_down */
1108 set_bit(STATUS_FW_ERROR, &priv->status);
1109
1110 /* Cancel currently queued command. */
1111 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
1112
459bc732
SZ
1113 IWL_ERR(priv, "Loaded firmware version: %s\n",
1114 priv->hw->wiphy->fw_version);
1115
3a3ff72c 1116 priv->cfg->ops->lib->dump_nic_error_log(priv);
696bdee3
WYG
1117 if (priv->cfg->ops->lib->dump_csr)
1118 priv->cfg->ops->lib->dump_csr(priv);
1b3eb823
WYG
1119 if (priv->cfg->ops->lib->dump_fh)
1120 priv->cfg->ops->lib->dump_fh(priv, NULL, false);
b03d7d0f 1121 priv->cfg->ops->lib->dump_nic_event_log(priv, false, NULL, false);
8ccde88a 1122#ifdef CONFIG_IWLWIFI_DEBUG
c341ddb2 1123 if (iwl_get_debug_level(priv) & IWL_DL_FW_ERRORS)
246ed355
JB
1124 iwl_print_rx_config_cmd(priv,
1125 &priv->contexts[IWL_RXON_CTX_BSS]);
8ccde88a
SO
1126#endif
1127
1128 wake_up_interruptible(&priv->wait_command_queue);
1129
1130 /* Keep the restart process from trying to send host
1131 * commands by clearing the INIT status bit */
1132 clear_bit(STATUS_READY, &priv->status);
1133
1134 if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
e1623446 1135 IWL_DEBUG(priv, IWL_DL_FW_ERRORS,
8ccde88a
SO
1136 "Restarting adapter due to uCode error.\n");
1137
8ccde88a
SO
1138 if (priv->cfg->mod_params->restart_fw)
1139 queue_work(priv->workqueue, &priv->restart);
1140 }
1141}
1142EXPORT_SYMBOL(iwl_irq_handle_error);
1143
f8e200de 1144static int iwl_apm_stop_master(struct iwl_priv *priv)
d68b603c 1145{
5220af0c 1146 int ret = 0;
d68b603c 1147
5220af0c 1148 /* stop device's busmaster DMA activity */
d68b603c
AK
1149 iwl_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_STOP_MASTER);
1150
5220af0c 1151 ret = iwl_poll_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_MASTER_DISABLED,
d68b603c 1152 CSR_RESET_REG_FLAG_MASTER_DISABLED, 100);
5220af0c
BC
1153 if (ret)
1154 IWL_WARN(priv, "Master Disable Timed Out, 100 usec\n");
d68b603c 1155
d68b603c
AK
1156 IWL_DEBUG_INFO(priv, "stop master\n");
1157
5220af0c 1158 return ret;
d68b603c 1159}
d68b603c
AK
1160
1161void iwl_apm_stop(struct iwl_priv *priv)
1162{
fadb3582
BC
1163 IWL_DEBUG_INFO(priv, "Stop card, put in low power state\n");
1164
5220af0c 1165 /* Stop device's DMA activity */
d68b603c
AK
1166 iwl_apm_stop_master(priv);
1167
5220af0c 1168 /* Reset the entire device */
d68b603c
AK
1169 iwl_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
1170
1171 udelay(10);
5220af0c
BC
1172
1173 /*
1174 * Clear "initialization complete" bit to move adapter from
1175 * D0A* (powered-up Active) --> D0U* (Uninitialized) state.
1176 */
d68b603c 1177 iwl_clear_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
d68b603c
AK
1178}
1179EXPORT_SYMBOL(iwl_apm_stop);
1180
fadb3582
BC
1181
1182/*
1183 * Start up NIC's basic functionality after it has been reset
1184 * (e.g. after platform boot, or shutdown via iwl_apm_stop())
1185 * NOTE: This does not load uCode nor start the embedded processor
1186 */
1187int iwl_apm_init(struct iwl_priv *priv)
1188{
1189 int ret = 0;
1190 u16 lctl;
1191
1192 IWL_DEBUG_INFO(priv, "Init card's basic functions\n");
1193
1194 /*
1195 * Use "set_bit" below rather than "write", to preserve any hardware
1196 * bits already set by default after reset.
1197 */
1198
1199 /* Disable L0S exit timer (platform NMI Work/Around) */
1200 iwl_set_bit(priv, CSR_GIO_CHICKEN_BITS,
1201 CSR_GIO_CHICKEN_BITS_REG_BIT_DIS_L0S_EXIT_TIMER);
1202
1203 /*
1204 * Disable L0s without affecting L1;
1205 * don't wait for ICH L0s (ICH bug W/A)
1206 */
1207 iwl_set_bit(priv, CSR_GIO_CHICKEN_BITS,
1208 CSR_GIO_CHICKEN_BITS_REG_BIT_L1A_NO_L0S_RX);
1209
1210 /* Set FH wait threshold to maximum (HW error during stress W/A) */
1211 iwl_set_bit(priv, CSR_DBG_HPET_MEM_REG, CSR_DBG_HPET_MEM_REG_VAL);
1212
1213 /*
1214 * Enable HAP INTA (interrupt from management bus) to
1215 * wake device's PCI Express link L1a -> L0s
1216 * NOTE: This is no-op for 3945 (non-existant bit)
1217 */
1218 iwl_set_bit(priv, CSR_HW_IF_CONFIG_REG,
1219 CSR_HW_IF_CONFIG_REG_BIT_HAP_WAKE_L1A);
1220
1221 /*
a6c5c731
BC
1222 * HW bug W/A for instability in PCIe bus L0->L0S->L1 transition.
1223 * Check if BIOS (or OS) enabled L1-ASPM on this device.
1224 * If so (likely), disable L0S, so device moves directly L0->L1;
1225 * costs negligible amount of power savings.
1226 * If not (unlikely), enable L0S, so there is at least some
1227 * power savings, even without L1.
fadb3582 1228 */
7cb1b088 1229 if (priv->cfg->base_params->set_l0s) {
fadb3582
BC
1230 lctl = iwl_pcie_link_ctl(priv);
1231 if ((lctl & PCI_CFG_LINK_CTRL_VAL_L1_EN) ==
1232 PCI_CFG_LINK_CTRL_VAL_L1_EN) {
1233 /* L1-ASPM enabled; disable(!) L0S */
1234 iwl_set_bit(priv, CSR_GIO_REG,
1235 CSR_GIO_REG_VAL_L0S_ENABLED);
1236 IWL_DEBUG_POWER(priv, "L1 Enabled; Disabling L0S\n");
1237 } else {
1238 /* L1-ASPM disabled; enable(!) L0S */
1239 iwl_clear_bit(priv, CSR_GIO_REG,
1240 CSR_GIO_REG_VAL_L0S_ENABLED);
1241 IWL_DEBUG_POWER(priv, "L1 Disabled; Enabling L0S\n");
1242 }
1243 }
1244
1245 /* Configure analog phase-lock-loop before activating to D0A */
7cb1b088
WYG
1246 if (priv->cfg->base_params->pll_cfg_val)
1247 iwl_set_bit(priv, CSR_ANA_PLL_CFG,
1248 priv->cfg->base_params->pll_cfg_val);
fadb3582
BC
1249
1250 /*
1251 * Set "initialization complete" bit to move adapter from
1252 * D0U* --> D0A* (powered-up active) state.
1253 */
1254 iwl_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
1255
1256 /*
1257 * Wait for clock stabilization; once stabilized, access to
1258 * device-internal resources is supported, e.g. iwl_write_prph()
1259 * and accesses to uCode SRAM.
1260 */
1261 ret = iwl_poll_bit(priv, CSR_GP_CNTRL,
1262 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
1263 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25000);
1264 if (ret < 0) {
1265 IWL_DEBUG_INFO(priv, "Failed to init the card\n");
1266 goto out;
1267 }
1268
1269 /*
1270 * Enable DMA and BSM (if used) clocks, wait for them to stabilize.
1271 * BSM (Boostrap State Machine) is only in 3945 and 4965;
1272 * later devices (i.e. 5000 and later) have non-volatile SRAM,
1273 * and don't need BSM to restore data after power-saving sleep.
1274 *
1275 * Write to "CLK_EN_REG"; "1" bits enable clocks, while "0" bits
1276 * do not disable clocks. This preserves any hardware bits already
1277 * set by default in "CLK_CTRL_REG" after reset.
1278 */
7cb1b088 1279 if (priv->cfg->base_params->use_bsm)
fadb3582
BC
1280 iwl_write_prph(priv, APMG_CLK_EN_REG,
1281 APMG_CLK_VAL_DMA_CLK_RQT | APMG_CLK_VAL_BSM_CLK_RQT);
1282 else
1283 iwl_write_prph(priv, APMG_CLK_EN_REG,
1284 APMG_CLK_VAL_DMA_CLK_RQT);
1285 udelay(20);
1286
1287 /* Disable L1-Active */
1288 iwl_set_bits_prph(priv, APMG_PCIDEV_STT_REG,
1289 APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
1290
1291out:
1292 return ret;
1293}
1294EXPORT_SYMBOL(iwl_apm_init);
1295
1296
630fe9b6
TW
1297int iwl_set_tx_power(struct iwl_priv *priv, s8 tx_power, bool force)
1298{
1299 int ret = 0;
5eadd94b
WYG
1300 s8 prev_tx_power = priv->tx_power_user_lmt;
1301
b744cb79
WYG
1302 if (tx_power < IWLAGN_TX_POWER_TARGET_POWER_MIN) {
1303 IWL_WARN(priv,
1304 "Requested user TXPOWER %d below lower limit %d.\n",
daf518de 1305 tx_power,
b744cb79 1306 IWLAGN_TX_POWER_TARGET_POWER_MIN);
630fe9b6
TW
1307 return -EINVAL;
1308 }
1309
dc1b0973 1310 if (tx_power > priv->tx_power_device_lmt) {
08f2d58d
WYG
1311 IWL_WARN(priv,
1312 "Requested user TXPOWER %d above upper limit %d.\n",
dc1b0973 1313 tx_power, priv->tx_power_device_lmt);
630fe9b6
TW
1314 return -EINVAL;
1315 }
1316
1317 if (priv->tx_power_user_lmt != tx_power)
1318 force = true;
1319
019fb97d 1320 /* if nic is not up don't send command */
5eadd94b
WYG
1321 if (iwl_is_ready_rf(priv)) {
1322 priv->tx_power_user_lmt = tx_power;
1323 if (force && priv->cfg->ops->lib->send_tx_power)
1324 ret = priv->cfg->ops->lib->send_tx_power(priv);
1325 else if (!priv->cfg->ops->lib->send_tx_power)
1326 ret = -EOPNOTSUPP;
1327 /*
1328 * if fail to set tx_power, restore the orig. tx power
1329 */
1330 if (ret)
1331 priv->tx_power_user_lmt = prev_tx_power;
1332 }
630fe9b6 1333
5eadd94b
WYG
1334 /*
1335 * Even this is an async host command, the command
1336 * will always report success from uCode
1337 * So once driver can placing the command into the queue
1338 * successfully, driver can use priv->tx_power_user_lmt
1339 * to reflect the current tx power
1340 */
630fe9b6
TW
1341 return ret;
1342}
1343EXPORT_SYMBOL(iwl_set_tx_power);
1344
ef850d7c 1345irqreturn_t iwl_isr_legacy(int irq, void *data)
f17d08a6
AK
1346{
1347 struct iwl_priv *priv = data;
1348 u32 inta, inta_mask;
1349 u32 inta_fh;
6e8cc38d 1350 unsigned long flags;
f17d08a6
AK
1351 if (!priv)
1352 return IRQ_NONE;
1353
6e8cc38d 1354 spin_lock_irqsave(&priv->lock, flags);
f17d08a6
AK
1355
1356 /* Disable (but don't clear!) interrupts here to avoid
1357 * back-to-back ISRs and sporadic interrupts from our NIC.
1358 * If we have something to service, the tasklet will re-enable ints.
1359 * If we *don't* have something, we'll re-enable before leaving here. */
1360 inta_mask = iwl_read32(priv, CSR_INT_MASK); /* just for debug */
1361 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
1362
1363 /* Discover which interrupts are active/pending */
1364 inta = iwl_read32(priv, CSR_INT);
1365 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
1366
1367 /* Ignore interrupt if there's nothing in NIC to service.
1368 * This may be due to IRQ shared with another device,
1369 * or due to sporadic interrupts thrown from our NIC. */
1370 if (!inta && !inta_fh) {
1371 IWL_DEBUG_ISR(priv, "Ignore interrupt, inta == 0, inta_fh == 0\n");
1372 goto none;
1373 }
1374
1375 if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
1376 /* Hardware disappeared. It might have already raised
1377 * an interrupt */
1378 IWL_WARN(priv, "HARDWARE GONE?? INTA == 0x%08x\n", inta);
1379 goto unplugged;
1380 }
1381
1382 IWL_DEBUG_ISR(priv, "ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
1383 inta, inta_mask, inta_fh);
1384
1385 inta &= ~CSR_INT_BIT_SCD;
1386
1387 /* iwl_irq_tasklet() will service interrupts and re-enable them */
1388 if (likely(inta || inta_fh))
1389 tasklet_schedule(&priv->irq_tasklet);
1390
1391 unplugged:
6e8cc38d 1392 spin_unlock_irqrestore(&priv->lock, flags);
f17d08a6
AK
1393 return IRQ_HANDLED;
1394
1395 none:
1396 /* re-enable interrupts here since we don't have anything to service. */
1397 /* only Re-enable if diabled by irq */
1398 if (test_bit(STATUS_INT_ENABLED, &priv->status))
1399 iwl_enable_interrupts(priv);
6e8cc38d 1400 spin_unlock_irqrestore(&priv->lock, flags);
f17d08a6
AK
1401 return IRQ_NONE;
1402}
ef850d7c 1403EXPORT_SYMBOL(iwl_isr_legacy);
f17d08a6 1404
65b52bde 1405void iwl_send_bt_config(struct iwl_priv *priv)
17f841cd
SO
1406{
1407 struct iwl_bt_cmd bt_cmd = {
456d0f76
WYG
1408 .lead_time = BT_LEAD_TIME_DEF,
1409 .max_kill = BT_MAX_KILL_DEF,
17f841cd
SO
1410 .kill_ack_mask = 0,
1411 .kill_cts_mask = 0,
1412 };
1413
06702a73
WYG
1414 if (!bt_coex_active)
1415 bt_cmd.flags = BT_COEX_DISABLE;
1416 else
1417 bt_cmd.flags = BT_COEX_ENABLE;
1418
1419 IWL_DEBUG_INFO(priv, "BT coex %s\n",
1420 (bt_cmd.flags == BT_COEX_DISABLE) ? "disable" : "active");
1421
65b52bde
JB
1422 if (iwl_send_cmd_pdu(priv, REPLY_BT_CONFIG,
1423 sizeof(struct iwl_bt_cmd), &bt_cmd))
1424 IWL_ERR(priv, "failed to send BT Coex Config\n");
17f841cd
SO
1425}
1426EXPORT_SYMBOL(iwl_send_bt_config);
1427
ef8d5529 1428int iwl_send_statistics_request(struct iwl_priv *priv, u8 flags, bool clear)
49ea8596 1429{
ef8d5529
WYG
1430 struct iwl_statistics_cmd statistics_cmd = {
1431 .configuration_flags =
1432 clear ? IWL_STATS_CONF_CLEAR_STATS : 0,
49ea8596 1433 };
ef8d5529
WYG
1434
1435 if (flags & CMD_ASYNC)
1436 return iwl_send_cmd_pdu_async(priv, REPLY_STATISTICS_CMD,
1437 sizeof(struct iwl_statistics_cmd),
1438 &statistics_cmd, NULL);
1439 else
1440 return iwl_send_cmd_pdu(priv, REPLY_STATISTICS_CMD,
1441 sizeof(struct iwl_statistics_cmd),
1442 &statistics_cmd);
49ea8596
EG
1443}
1444EXPORT_SYMBOL(iwl_send_statistics_request);
7e8c519e 1445
030f05ed
AK
1446void iwl_rx_pm_sleep_notif(struct iwl_priv *priv,
1447 struct iwl_rx_mem_buffer *rxb)
1448{
1449#ifdef CONFIG_IWLWIFI_DEBUG
2f301227 1450 struct iwl_rx_packet *pkt = rxb_addr(rxb);
030f05ed
AK
1451 struct iwl_sleep_notification *sleep = &(pkt->u.sleep_notif);
1452 IWL_DEBUG_RX(priv, "sleep mode: %d, src: %d\n",
1453 sleep->pm_sleep_mode, sleep->pm_wakeup_src);
1454#endif
1455}
1456EXPORT_SYMBOL(iwl_rx_pm_sleep_notif);
1457
1458void iwl_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
1459 struct iwl_rx_mem_buffer *rxb)
1460{
2f301227 1461 struct iwl_rx_packet *pkt = rxb_addr(rxb);
396887a2 1462 u32 len = le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
030f05ed 1463 IWL_DEBUG_RADIO(priv, "Dumping %d bytes of unhandled "
396887a2
DH
1464 "notification for %s:\n", len,
1465 get_cmd_string(pkt->hdr.cmd));
1466 iwl_print_hex_dump(priv, IWL_DL_RADIO, pkt->u.raw, len);
030f05ed
AK
1467}
1468EXPORT_SYMBOL(iwl_rx_pm_debug_statistics_notif);
261b9c33
AK
1469
1470void iwl_rx_reply_error(struct iwl_priv *priv,
1471 struct iwl_rx_mem_buffer *rxb)
1472{
2f301227 1473 struct iwl_rx_packet *pkt = rxb_addr(rxb);
261b9c33
AK
1474
1475 IWL_ERR(priv, "Error Reply type 0x%08X cmd %s (0x%02X) "
1476 "seq 0x%04X ser 0x%08X\n",
1477 le32_to_cpu(pkt->u.err_resp.error_type),
1478 get_cmd_string(pkt->u.err_resp.cmd_id),
1479 pkt->u.err_resp.cmd_id,
1480 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
1481 le32_to_cpu(pkt->u.err_resp.error_info));
1482}
1483EXPORT_SYMBOL(iwl_rx_reply_error);
1484
a83b9141
WYG
1485void iwl_clear_isr_stats(struct iwl_priv *priv)
1486{
1487 memset(&priv->isr_stats, 0, sizeof(priv->isr_stats));
1488}
a83b9141 1489
488829f1
AK
1490int iwl_mac_conf_tx(struct ieee80211_hw *hw, u16 queue,
1491 const struct ieee80211_tx_queue_params *params)
1492{
1493 struct iwl_priv *priv = hw->priv;
8dfdb9d5 1494 struct iwl_rxon_context *ctx;
488829f1
AK
1495 unsigned long flags;
1496 int q;
1497
1498 IWL_DEBUG_MAC80211(priv, "enter\n");
1499
1500 if (!iwl_is_ready_rf(priv)) {
1501 IWL_DEBUG_MAC80211(priv, "leave - RF not ready\n");
1502 return -EIO;
1503 }
1504
1505 if (queue >= AC_NUM) {
1506 IWL_DEBUG_MAC80211(priv, "leave - queue >= AC_NUM %d\n", queue);
1507 return 0;
1508 }
1509
1510 q = AC_NUM - 1 - queue;
1511
1512 spin_lock_irqsave(&priv->lock, flags);
1513
8dfdb9d5
JB
1514 /*
1515 * MULTI-FIXME
1516 * This may need to be done per interface in nl80211/cfg80211/mac80211.
1517 */
1518 for_each_context(priv, ctx) {
1519 ctx->qos_data.def_qos_parm.ac[q].cw_min =
1520 cpu_to_le16(params->cw_min);
1521 ctx->qos_data.def_qos_parm.ac[q].cw_max =
1522 cpu_to_le16(params->cw_max);
1523 ctx->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
1524 ctx->qos_data.def_qos_parm.ac[q].edca_txop =
1525 cpu_to_le16((params->txop * 32));
1526
1527 ctx->qos_data.def_qos_parm.ac[q].reserved1 = 0;
1528 }
488829f1
AK
1529
1530 spin_unlock_irqrestore(&priv->lock, flags);
1531
1532 IWL_DEBUG_MAC80211(priv, "leave\n");
1533 return 0;
1534}
1535EXPORT_SYMBOL(iwl_mac_conf_tx);
5bbe233b 1536
a85d7cca
JB
1537int iwl_mac_tx_last_beacon(struct ieee80211_hw *hw)
1538{
1539 struct iwl_priv *priv = hw->priv;
1540
1541 return priv->ibss_manager == IWL_IBSS_MANAGER;
1542}
1543EXPORT_SYMBOL_GPL(iwl_mac_tx_last_beacon);
1544
5bbe233b 1545static void iwl_ht_conf(struct iwl_priv *priv,
ca3c1f59 1546 struct ieee80211_vif *vif)
5bbe233b 1547{
fad95bf5 1548 struct iwl_ht_config *ht_conf = &priv->current_ht_config;
5bbe233b 1549 struct ieee80211_sta *sta;
ca3c1f59 1550 struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
7e6a5886 1551 struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
5bbe233b 1552
91dd6c27 1553 IWL_DEBUG_MAC80211(priv, "enter:\n");
5bbe233b 1554
7e6a5886 1555 if (!ctx->ht.enabled)
5bbe233b
AK
1556 return;
1557
7e6a5886 1558 ctx->ht.protection =
9ed6bcce 1559 bss_conf->ht_operation_mode & IEEE80211_HT_OP_MODE_PROTECTION;
7e6a5886 1560 ctx->ht.non_gf_sta_present =
9ed6bcce 1561 !!(bss_conf->ht_operation_mode & IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT);
5bbe233b 1562
02bb1bea
JB
1563 ht_conf->single_chain_sufficient = false;
1564
ca3c1f59 1565 switch (vif->type) {
02bb1bea
JB
1566 case NL80211_IFTYPE_STATION:
1567 rcu_read_lock();
ca3c1f59 1568 sta = ieee80211_find_sta(vif, bss_conf->bssid);
02bb1bea
JB
1569 if (sta) {
1570 struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
1571 int maxstreams;
1572
1573 maxstreams = (ht_cap->mcs.tx_params &
1574 IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
1575 >> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT;
1576 maxstreams += 1;
1577
1578 if ((ht_cap->mcs.rx_mask[1] == 0) &&
1579 (ht_cap->mcs.rx_mask[2] == 0))
1580 ht_conf->single_chain_sufficient = true;
1581 if (maxstreams <= 1)
1582 ht_conf->single_chain_sufficient = true;
1583 } else {
1584 /*
1585 * If at all, this can only happen through a race
1586 * when the AP disconnects us while we're still
1587 * setting up the connection, in that case mac80211
1588 * will soon tell us about that.
1589 */
1590 ht_conf->single_chain_sufficient = true;
1591 }
1592 rcu_read_unlock();
1593 break;
1594 case NL80211_IFTYPE_ADHOC:
1595 ht_conf->single_chain_sufficient = true;
1596 break;
1597 default:
1598 break;
1599 }
5bbe233b
AK
1600
1601 IWL_DEBUG_MAC80211(priv, "leave\n");
1602}
1603
246ed355
JB
1604static inline void iwl_set_no_assoc(struct iwl_priv *priv,
1605 struct ieee80211_vif *vif)
c91c3efc 1606{
246ed355
JB
1607 struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
1608
c91c3efc
AK
1609 iwl_led_disassociate(priv);
1610 /*
1611 * inform the ucode that there is no longer an
1612 * association and that no more packets should be
1613 * sent
1614 */
246ed355
JB
1615 ctx->staging.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
1616 ctx->staging.assoc_id = 0;
1617 iwlcore_commit_rxon(priv, ctx);
c91c3efc
AK
1618}
1619
0bc5774f
JB
1620static int iwl_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb)
1621{
1622 struct iwl_priv *priv = hw->priv;
1623 unsigned long flags;
1624 __le64 timestamp;
1625
1626 IWL_DEBUG_MAC80211(priv, "enter\n");
1627
76d04815
JB
1628 lockdep_assert_held(&priv->mutex);
1629
1630 if (!priv->beacon_ctx) {
1631 IWL_ERR(priv, "update beacon but no beacon context!\n");
1632 dev_kfree_skb(skb);
1633 return -EINVAL;
1634 }
1635
0bc5774f
JB
1636 if (!iwl_is_ready_rf(priv)) {
1637 IWL_DEBUG_MAC80211(priv, "leave - RF not ready\n");
1638 return -EIO;
1639 }
1640
1641 spin_lock_irqsave(&priv->lock, flags);
1642
1643 if (priv->ibss_beacon)
1644 dev_kfree_skb(priv->ibss_beacon);
1645
1646 priv->ibss_beacon = skb;
1647
1648 timestamp = ((struct ieee80211_mgmt *)skb->data)->u.beacon.timestamp;
1649 priv->timestamp = le64_to_cpu(timestamp);
1650
1651 IWL_DEBUG_MAC80211(priv, "leave\n");
1652 spin_unlock_irqrestore(&priv->lock, flags);
1653
76d04815 1654 priv->cfg->ops->lib->post_associate(priv, priv->beacon_ctx->vif);
0bc5774f
JB
1655
1656 return 0;
1657}
1658
5bbe233b 1659void iwl_bss_info_changed(struct ieee80211_hw *hw,
2d0ddec5
JB
1660 struct ieee80211_vif *vif,
1661 struct ieee80211_bss_conf *bss_conf,
1662 u32 changes)
5bbe233b
AK
1663{
1664 struct iwl_priv *priv = hw->priv;
246ed355 1665 struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
3a650292 1666 int ret;
5bbe233b
AK
1667
1668 IWL_DEBUG_MAC80211(priv, "changes = 0x%X\n", changes);
1669
2d0ddec5
JB
1670 if (!iwl_is_alive(priv))
1671 return;
1672
1673 mutex_lock(&priv->mutex);
1674
4ced3f74
JB
1675 if (changes & BSS_CHANGED_QOS) {
1676 unsigned long flags;
1677
1678 spin_lock_irqsave(&priv->lock, flags);
8dfdb9d5
JB
1679 ctx->qos_data.qos_active = bss_conf->qos;
1680 iwl_update_qos(priv, ctx);
4ced3f74
JB
1681 spin_unlock_irqrestore(&priv->lock, flags);
1682 }
1683
76d04815
JB
1684 if (changes & BSS_CHANGED_BEACON_ENABLED) {
1685 /*
1686 * the add_interface code must make sure we only ever
1687 * have a single interface that could be beaconing at
1688 * any time.
1689 */
1690 if (vif->bss_conf.enable_beacon)
1691 priv->beacon_ctx = ctx;
1692 else
1693 priv->beacon_ctx = NULL;
1694 }
1695
92445c95 1696 if (changes & BSS_CHANGED_BEACON && vif->type == NL80211_IFTYPE_AP) {
2d0ddec5
JB
1697 dev_kfree_skb(priv->ibss_beacon);
1698 priv->ibss_beacon = ieee80211_beacon_get(hw, vif);
1699 }
1700
2a3aeb44
JB
1701 if (changes & BSS_CHANGED_BEACON_INT && vif->type == NL80211_IFTYPE_AP)
1702 iwl_send_rxon_timing(priv, ctx);
d7129e19
JB
1703
1704 if (changes & BSS_CHANGED_BSSID) {
1705 IWL_DEBUG_MAC80211(priv, "BSSID %pM\n", bss_conf->bssid);
1706
1707 /*
1708 * If there is currently a HW scan going on in the
1709 * background then we need to cancel it else the RXON
1710 * below/in post_associate will fail.
1711 */
2d0ddec5 1712 if (iwl_scan_cancel_timeout(priv, 100)) {
d7129e19 1713 IWL_WARN(priv, "Aborted scan still in progress after 100ms\n");
2d0ddec5
JB
1714 IWL_DEBUG_MAC80211(priv, "leaving - scan abort failed.\n");
1715 mutex_unlock(&priv->mutex);
1716 return;
1717 }
2d0ddec5 1718
d7129e19 1719 /* mac80211 only sets assoc when in STATION mode */
92445c95 1720 if (vif->type == NL80211_IFTYPE_ADHOC || bss_conf->assoc) {
246ed355 1721 memcpy(ctx->staging.bssid_addr,
d7129e19 1722 bss_conf->bssid, ETH_ALEN);
2d0ddec5 1723
d7129e19
JB
1724 /* currently needed in a few places */
1725 memcpy(priv->bssid, bss_conf->bssid, ETH_ALEN);
1726 } else {
246ed355 1727 ctx->staging.filter_flags &=
d7129e19 1728 ~RXON_FILTER_ASSOC_MSK;
2d0ddec5 1729 }
d7129e19 1730
2d0ddec5
JB
1731 }
1732
d7129e19
JB
1733 /*
1734 * This needs to be after setting the BSSID in case
1735 * mac80211 decides to do both changes at once because
1736 * it will invoke post_associate.
1737 */
92445c95 1738 if (vif->type == NL80211_IFTYPE_ADHOC &&
2d0ddec5
JB
1739 changes & BSS_CHANGED_BEACON) {
1740 struct sk_buff *beacon = ieee80211_beacon_get(hw, vif);
1741
1742 if (beacon)
1743 iwl_mac_beacon_update(hw, beacon);
1744 }
1745
5bbe233b
AK
1746 if (changes & BSS_CHANGED_ERP_PREAMBLE) {
1747 IWL_DEBUG_MAC80211(priv, "ERP_PREAMBLE %d\n",
1748 bss_conf->use_short_preamble);
1749 if (bss_conf->use_short_preamble)
246ed355 1750 ctx->staging.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
5bbe233b 1751 else
246ed355 1752 ctx->staging.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
5bbe233b
AK
1753 }
1754
1755 if (changes & BSS_CHANGED_ERP_CTS_PROT) {
1756 IWL_DEBUG_MAC80211(priv, "ERP_CTS %d\n", bss_conf->use_cts_prot);
1757 if (bss_conf->use_cts_prot && (priv->band != IEEE80211_BAND_5GHZ))
246ed355 1758 ctx->staging.flags |= RXON_FLG_TGG_PROTECT_MSK;
5bbe233b 1759 else
246ed355 1760 ctx->staging.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
94597ab2 1761 if (bss_conf->use_cts_prot)
246ed355 1762 ctx->staging.flags |= RXON_FLG_SELF_CTS_EN;
94597ab2 1763 else
246ed355 1764 ctx->staging.flags &= ~RXON_FLG_SELF_CTS_EN;
5bbe233b
AK
1765 }
1766
d7129e19
JB
1767 if (changes & BSS_CHANGED_BASIC_RATES) {
1768 /* XXX use this information
1769 *
1770 * To do that, remove code from iwl_set_rate() and put something
1771 * like this here:
1772 *
1773 if (A-band)
246ed355 1774 ctx->staging.ofdm_basic_rates =
d7129e19
JB
1775 bss_conf->basic_rates;
1776 else
246ed355 1777 ctx->staging.ofdm_basic_rates =
d7129e19 1778 bss_conf->basic_rates >> 4;
246ed355 1779 ctx->staging.cck_basic_rates =
d7129e19
JB
1780 bss_conf->basic_rates & 0xF;
1781 */
1782 }
1783
5bbe233b 1784 if (changes & BSS_CHANGED_HT) {
ca3c1f59 1785 iwl_ht_conf(priv, vif);
45823531
AK
1786
1787 if (priv->cfg->ops->hcmd->set_rxon_chain)
246ed355 1788 priv->cfg->ops->hcmd->set_rxon_chain(priv, ctx);
5bbe233b
AK
1789 }
1790
1791 if (changes & BSS_CHANGED_ASSOC) {
1792 IWL_DEBUG_MAC80211(priv, "ASSOC %d\n", bss_conf->assoc);
5bbe233b 1793 if (bss_conf->assoc) {
5bbe233b 1794 priv->timestamp = bss_conf->timestamp;
5bbe233b 1795
e932a609
JB
1796 iwl_led_associate(priv);
1797
d7129e19 1798 if (!iwl_is_rfkill(priv))
1dda6d28 1799 priv->cfg->ops->lib->post_associate(priv, vif);
c91c3efc 1800 } else
246ed355 1801 iwl_set_no_assoc(priv, vif);
d7129e19
JB
1802 }
1803
246ed355 1804 if (changes && iwl_is_associated_ctx(ctx) && bss_conf->aid) {
d7129e19
JB
1805 IWL_DEBUG_MAC80211(priv, "Changes (%#x) while associated\n",
1806 changes);
246ed355 1807 ret = iwl_send_rxon_assoc(priv, ctx);
d7129e19
JB
1808 if (!ret) {
1809 /* Sync active_rxon with latest change. */
246ed355
JB
1810 memcpy((void *)&ctx->active,
1811 &ctx->staging,
d7129e19 1812 sizeof(struct iwl_rxon_cmd));
5bbe233b 1813 }
5bbe233b 1814 }
d7129e19 1815
c91c3efc
AK
1816 if (changes & BSS_CHANGED_BEACON_ENABLED) {
1817 if (vif->bss_conf.enable_beacon) {
246ed355 1818 memcpy(ctx->staging.bssid_addr,
c91c3efc
AK
1819 bss_conf->bssid, ETH_ALEN);
1820 memcpy(priv->bssid, bss_conf->bssid, ETH_ALEN);
1dda6d28 1821 iwlcore_config_ap(priv, vif);
c91c3efc 1822 } else
246ed355 1823 iwl_set_no_assoc(priv, vif);
f513dfff
DH
1824 }
1825
1fa61b2e
JB
1826 if (changes & BSS_CHANGED_IBSS) {
1827 ret = priv->cfg->ops->lib->manage_ibss_station(priv, vif,
1828 bss_conf->ibss_joined);
1829 if (ret)
1830 IWL_ERR(priv, "failed to %s IBSS station %pM\n",
1831 bss_conf->ibss_joined ? "add" : "remove",
1832 bss_conf->bssid);
1833 }
1834
52a02d15
JB
1835 if (changes & BSS_CHANGED_IDLE &&
1836 priv->cfg->ops->hcmd->set_pan_params) {
1837 if (priv->cfg->ops->hcmd->set_pan_params(priv))
1838 IWL_ERR(priv, "failed to update PAN params\n");
1839 }
1840
d7129e19
JB
1841 mutex_unlock(&priv->mutex);
1842
2d0ddec5 1843 IWL_DEBUG_MAC80211(priv, "leave\n");
5bbe233b
AK
1844}
1845EXPORT_SYMBOL(iwl_bss_info_changed);
1846
b55e75ed 1847static int iwl_set_mode(struct iwl_priv *priv, struct ieee80211_vif *vif)
727882d6 1848{
246ed355
JB
1849 struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
1850
d0fe478c 1851 iwl_connection_init_rx_config(priv, ctx);
727882d6
AK
1852
1853 if (priv->cfg->ops->hcmd->set_rxon_chain)
246ed355 1854 priv->cfg->ops->hcmd->set_rxon_chain(priv, ctx);
727882d6 1855
246ed355 1856 return iwlcore_commit_rxon(priv, ctx);
727882d6 1857}
727882d6 1858
b55e75ed 1859int iwl_mac_add_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
cbb6ab94
AK
1860{
1861 struct iwl_priv *priv = hw->priv;
246ed355 1862 struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
d0fe478c 1863 struct iwl_rxon_context *tmp, *ctx = NULL;
47e28f41 1864 int err = 0;
cbb6ab94 1865
3779db10
JB
1866 IWL_DEBUG_MAC80211(priv, "enter: type %d, addr %pM\n",
1867 vif->type, vif->addr);
cbb6ab94 1868
47e28f41
JB
1869 mutex_lock(&priv->mutex);
1870
4bd530f3
SG
1871 if (!iwl_is_ready_rf(priv)) {
1872 IWL_WARN(priv, "Try to add interface when device not ready\n");
b55e75ed
JB
1873 err = -EINVAL;
1874 goto out;
1875 }
1876
d0fe478c
JB
1877 for_each_context(priv, tmp) {
1878 u32 possible_modes =
1879 tmp->interface_modes | tmp->exclusive_interface_modes;
1880
1881 if (tmp->vif) {
1882 /* check if this busy context is exclusive */
1883 if (tmp->exclusive_interface_modes &
1884 BIT(tmp->vif->type)) {
1885 err = -EINVAL;
1886 goto out;
1887 }
1888 continue;
1889 }
1890
1891 if (!(possible_modes & BIT(vif->type)))
1892 continue;
1893
1894 /* have maybe usable context w/o interface */
1895 ctx = tmp;
1896 break;
1897 }
1898
1899 if (!ctx) {
47e28f41
JB
1900 err = -EOPNOTSUPP;
1901 goto out;
cbb6ab94
AK
1902 }
1903
d0fe478c 1904 vif_priv->ctx = ctx;
8bd413e6 1905 ctx->vif = vif;
d0fe478c
JB
1906 /*
1907 * This variable will be correct only when there's just
1908 * a single context, but all code using it is for hardware
1909 * that supports only one context.
1910 */
1ed32e4f 1911 priv->iw_mode = vif->type;
cbb6ab94 1912
763cc3bf
JB
1913 ctx->is_active = true;
1914
b55e75ed 1915 err = iwl_set_mode(priv, vif);
763cc3bf
JB
1916 if (err) {
1917 if (!ctx->always_active)
1918 ctx->is_active = false;
b55e75ed 1919 goto out_err;
763cc3bf 1920 }
7e246191 1921
7cb1b088
WYG
1922 if (priv->cfg->bt_params &&
1923 priv->cfg->bt_params->advanced_bt_coexist &&
59079949
JB
1924 vif->type == NL80211_IFTYPE_ADHOC) {
1925 /*
1926 * pretend to have high BT traffic as long as we
1927 * are operating in IBSS mode, as this will cause
1928 * the rate scaling etc. to behave as intended.
1929 */
1930 priv->bt_traffic_load = IWL_BT_COEX_TRAFFIC_LOAD_HIGH;
1931 }
1932
b55e75ed 1933 goto out;
cbb6ab94 1934
b55e75ed 1935 out_err:
8bd413e6 1936 ctx->vif = NULL;
b55e75ed 1937 priv->iw_mode = NL80211_IFTYPE_STATION;
47e28f41 1938 out:
cbb6ab94
AK
1939 mutex_unlock(&priv->mutex);
1940
1941 IWL_DEBUG_MAC80211(priv, "leave\n");
47e28f41 1942 return err;
cbb6ab94
AK
1943}
1944EXPORT_SYMBOL(iwl_mac_add_interface);
1945
d8052319 1946void iwl_mac_remove_interface(struct ieee80211_hw *hw,
b55e75ed 1947 struct ieee80211_vif *vif)
d8052319
AK
1948{
1949 struct iwl_priv *priv = hw->priv;
246ed355 1950 struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
d8052319
AK
1951
1952 IWL_DEBUG_MAC80211(priv, "enter\n");
1953
1954 mutex_lock(&priv->mutex);
1955
d0fe478c
JB
1956 WARN_ON(ctx->vif != vif);
1957 ctx->vif = NULL;
1958
e7e16b90
SG
1959 if (priv->scan_vif == vif) {
1960 iwl_scan_cancel_timeout(priv, 200);
1961 iwl_force_scan_end(priv);
1962 }
d0fe478c 1963 iwl_set_mode(priv, vif);
8bd413e6 1964
763cc3bf
JB
1965 if (!ctx->always_active)
1966 ctx->is_active = false;
1967
59079949
JB
1968 /*
1969 * When removing the IBSS interface, overwrite the
1970 * BT traffic load with the stored one from the last
1971 * notification, if any. If this is a device that
1972 * doesn't implement this, this has no effect since
1973 * both values are the same and zero.
1974 */
1975 if (vif->type == NL80211_IFTYPE_ADHOC)
1976 priv->bt_traffic_load = priv->notif_bt_traffic_load;
1977
8bd413e6 1978 memset(priv->bssid, 0, ETH_ALEN);
d8052319
AK
1979 mutex_unlock(&priv->mutex);
1980
1981 IWL_DEBUG_MAC80211(priv, "leave\n");
1982
1983}
1984EXPORT_SYMBOL(iwl_mac_remove_interface);
1985
4808368d
AK
1986/**
1987 * iwl_mac_config - mac80211 config callback
4808368d
AK
1988 */
1989int iwl_mac_config(struct ieee80211_hw *hw, u32 changed)
1990{
1991 struct iwl_priv *priv = hw->priv;
1992 const struct iwl_channel_info *ch_info;
1993 struct ieee80211_conf *conf = &hw->conf;
aa2dc6b5 1994 struct ieee80211_channel *channel = conf->channel;
fad95bf5 1995 struct iwl_ht_config *ht_conf = &priv->current_ht_config;
246ed355 1996 struct iwl_rxon_context *ctx;
4808368d
AK
1997 unsigned long flags = 0;
1998 int ret = 0;
1999 u16 ch;
2000 int scan_active = 0;
2001
2002 mutex_lock(&priv->mutex);
2003
4808368d 2004 IWL_DEBUG_MAC80211(priv, "enter to channel %d changed 0x%X\n",
aa2dc6b5 2005 channel->hw_value, changed);
4808368d
AK
2006
2007 if (unlikely(!priv->cfg->mod_params->disable_hw_scan &&
2008 test_bit(STATUS_SCANNING, &priv->status))) {
2009 scan_active = 1;
2010 IWL_DEBUG_MAC80211(priv, "leave - scanning\n");
2011 }
2012
ba37a3d0
JB
2013 if (changed & (IEEE80211_CONF_CHANGE_SMPS |
2014 IEEE80211_CONF_CHANGE_CHANNEL)) {
2015 /* mac80211 uses static for non-HT which is what we want */
2016 priv->current_ht_config.smps = conf->smps_mode;
2017
2018 /*
2019 * Recalculate chain counts.
2020 *
2021 * If monitor mode is enabled then mac80211 will
2022 * set up the SM PS mode to OFF if an HT channel is
2023 * configured.
2024 */
2025 if (priv->cfg->ops->hcmd->set_rxon_chain)
246ed355
JB
2026 for_each_context(priv, ctx)
2027 priv->cfg->ops->hcmd->set_rxon_chain(priv, ctx);
ba37a3d0 2028 }
4808368d
AK
2029
2030 /* during scanning mac80211 will delay channel setting until
2031 * scan finish with changed = 0
2032 */
2033 if (!changed || (changed & IEEE80211_CONF_CHANGE_CHANNEL)) {
2034 if (scan_active)
2035 goto set_ch_out;
2036
aa2dc6b5
SZ
2037 ch = channel->hw_value;
2038 ch_info = iwl_get_channel_info(priv, channel->band, ch);
4808368d
AK
2039 if (!is_channel_valid(ch_info)) {
2040 IWL_DEBUG_MAC80211(priv, "leave - invalid channel\n");
2041 ret = -EINVAL;
2042 goto set_ch_out;
2043 }
2044
4808368d
AK
2045 spin_lock_irqsave(&priv->lock, flags);
2046
246ed355 2047 for_each_context(priv, ctx) {
7e6a5886
JB
2048 /* Configure HT40 channels */
2049 ctx->ht.enabled = conf_is_ht(conf);
2050 if (ctx->ht.enabled) {
2051 if (conf_is_ht40_minus(conf)) {
2052 ctx->ht.extension_chan_offset =
2053 IEEE80211_HT_PARAM_CHA_SEC_BELOW;
2054 ctx->ht.is_40mhz = true;
2055 } else if (conf_is_ht40_plus(conf)) {
2056 ctx->ht.extension_chan_offset =
2057 IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
2058 ctx->ht.is_40mhz = true;
2059 } else {
2060 ctx->ht.extension_chan_offset =
2061 IEEE80211_HT_PARAM_CHA_SEC_NONE;
2062 ctx->ht.is_40mhz = false;
2063 }
2064 } else
2065 ctx->ht.is_40mhz = false;
2066
2067 /*
2068 * Default to no protection. Protection mode will
2069 * later be set from BSS config in iwl_ht_conf
2070 */
2071 ctx->ht.protection = IEEE80211_HT_OP_MODE_PROTECTION_NONE;
2072
246ed355
JB
2073 /* if we are switching from ht to 2.4 clear flags
2074 * from any ht related info since 2.4 does not
2075 * support ht */
2076 if ((le16_to_cpu(ctx->staging.channel) != ch))
2077 ctx->staging.flags = 0;
4808368d 2078
246ed355
JB
2079 iwl_set_rxon_channel(priv, channel, ctx);
2080 iwl_set_rxon_ht(priv, ht_conf);
2081
2082 iwl_set_flags_for_band(priv, ctx, channel->band,
8bd413e6 2083 ctx->vif);
246ed355 2084 }
4808368d 2085
4808368d 2086 spin_unlock_irqrestore(&priv->lock, flags);
79d07325 2087
a194e324
JB
2088 if (priv->cfg->ops->lib->update_bcast_stations)
2089 ret = priv->cfg->ops->lib->update_bcast_stations(priv);
278c2f6f 2090
4808368d
AK
2091 set_ch_out:
2092 /* The list of supported rates and rate mask can be different
2093 * for each band; since the band may have changed, reset
2094 * the rate mask to what mac80211 lists */
2095 iwl_set_rate(priv);
2096 }
2097
78f5fb7f
JB
2098 if (changed & (IEEE80211_CONF_CHANGE_PS |
2099 IEEE80211_CONF_CHANGE_IDLE)) {
e312c24c 2100 ret = iwl_power_update_mode(priv, false);
4808368d 2101 if (ret)
e312c24c 2102 IWL_DEBUG_MAC80211(priv, "Error setting sleep level\n");
4808368d
AK
2103 }
2104
2105 if (changed & IEEE80211_CONF_CHANGE_POWER) {
2106 IWL_DEBUG_MAC80211(priv, "TX Power old=%d new=%d\n",
2107 priv->tx_power_user_lmt, conf->power_level);
2108
2109 iwl_set_tx_power(priv, conf->power_level, false);
2110 }
2111
0cf4c01e
MA
2112 if (!iwl_is_ready(priv)) {
2113 IWL_DEBUG_MAC80211(priv, "leave - not ready\n");
2114 goto out;
2115 }
2116
4808368d
AK
2117 if (scan_active)
2118 goto out;
2119
246ed355
JB
2120 for_each_context(priv, ctx) {
2121 if (memcmp(&ctx->active, &ctx->staging, sizeof(ctx->staging)))
2122 iwlcore_commit_rxon(priv, ctx);
2123 else
2124 IWL_DEBUG_INFO(priv,
2125 "Not re-sending same RXON configuration.\n");
2126 }
4808368d
AK
2127
2128out:
2129 IWL_DEBUG_MAC80211(priv, "leave\n");
2130 mutex_unlock(&priv->mutex);
2131 return ret;
2132}
2133EXPORT_SYMBOL(iwl_mac_config);
2134
bd564261
AK
2135void iwl_mac_reset_tsf(struct ieee80211_hw *hw)
2136{
2137 struct iwl_priv *priv = hw->priv;
2138 unsigned long flags;
246ed355
JB
2139 /* IBSS can only be the IWL_RXON_CTX_BSS context */
2140 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
bd564261
AK
2141
2142 mutex_lock(&priv->mutex);
2143 IWL_DEBUG_MAC80211(priv, "enter\n");
2144
2145 spin_lock_irqsave(&priv->lock, flags);
fad95bf5 2146 memset(&priv->current_ht_config, 0, sizeof(struct iwl_ht_config));
bd564261
AK
2147 spin_unlock_irqrestore(&priv->lock, flags);
2148
bd564261 2149 spin_lock_irqsave(&priv->lock, flags);
bd564261
AK
2150
2151 /* new association get rid of ibss beacon skb */
2152 if (priv->ibss_beacon)
2153 dev_kfree_skb(priv->ibss_beacon);
2154
2155 priv->ibss_beacon = NULL;
2156
bd564261 2157 priv->timestamp = 0;
bd564261
AK
2158
2159 spin_unlock_irqrestore(&priv->lock, flags);
2160
f5354c17 2161 iwl_scan_cancel_timeout(priv, 100);
bd564261
AK
2162 if (!iwl_is_ready_rf(priv)) {
2163 IWL_DEBUG_MAC80211(priv, "leave - not ready\n");
2164 mutex_unlock(&priv->mutex);
2165 return;
2166 }
2167
2168 /* we are restarting association process
2169 * clear RXON_FILTER_ASSOC_MSK bit
2170 */
246ed355
JB
2171 ctx->staging.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
2172 iwlcore_commit_rxon(priv, ctx);
bd564261
AK
2173
2174 iwl_set_rate(priv);
2175
2176 mutex_unlock(&priv->mutex);
2177
2178 IWL_DEBUG_MAC80211(priv, "leave\n");
2179}
2180EXPORT_SYMBOL(iwl_mac_reset_tsf);
2181
88804e2b
WYG
2182int iwl_alloc_txq_mem(struct iwl_priv *priv)
2183{
2184 if (!priv->txq)
2185 priv->txq = kzalloc(
7cb1b088
WYG
2186 sizeof(struct iwl_tx_queue) *
2187 priv->cfg->base_params->num_of_queues,
88804e2b
WYG
2188 GFP_KERNEL);
2189 if (!priv->txq) {
91dd6c27 2190 IWL_ERR(priv, "Not enough memory for txq\n");
88804e2b
WYG
2191 return -ENOMEM;
2192 }
2193 return 0;
2194}
2195EXPORT_SYMBOL(iwl_alloc_txq_mem);
2196
2197void iwl_free_txq_mem(struct iwl_priv *priv)
2198{
2199 kfree(priv->txq);
2200 priv->txq = NULL;
2201}
2202EXPORT_SYMBOL(iwl_free_txq_mem);
2203
20594eb0
WYG
2204#ifdef CONFIG_IWLWIFI_DEBUGFS
2205
2206#define IWL_TRAFFIC_DUMP_SIZE (IWL_TRAFFIC_ENTRY_SIZE * IWL_TRAFFIC_ENTRIES)
2207
2208void iwl_reset_traffic_log(struct iwl_priv *priv)
2209{
2210 priv->tx_traffic_idx = 0;
2211 priv->rx_traffic_idx = 0;
2212 if (priv->tx_traffic)
2213 memset(priv->tx_traffic, 0, IWL_TRAFFIC_DUMP_SIZE);
2214 if (priv->rx_traffic)
2215 memset(priv->rx_traffic, 0, IWL_TRAFFIC_DUMP_SIZE);
2216}
2217
2218int iwl_alloc_traffic_mem(struct iwl_priv *priv)
2219{
2220 u32 traffic_size = IWL_TRAFFIC_DUMP_SIZE;
2221
2222 if (iwl_debug_level & IWL_DL_TX) {
2223 if (!priv->tx_traffic) {
2224 priv->tx_traffic =
2225 kzalloc(traffic_size, GFP_KERNEL);
2226 if (!priv->tx_traffic)
2227 return -ENOMEM;
2228 }
2229 }
2230 if (iwl_debug_level & IWL_DL_RX) {
2231 if (!priv->rx_traffic) {
2232 priv->rx_traffic =
2233 kzalloc(traffic_size, GFP_KERNEL);
2234 if (!priv->rx_traffic)
2235 return -ENOMEM;
2236 }
2237 }
2238 iwl_reset_traffic_log(priv);
2239 return 0;
2240}
2241EXPORT_SYMBOL(iwl_alloc_traffic_mem);
2242
2243void iwl_free_traffic_mem(struct iwl_priv *priv)
2244{
2245 kfree(priv->tx_traffic);
2246 priv->tx_traffic = NULL;
2247
2248 kfree(priv->rx_traffic);
2249 priv->rx_traffic = NULL;
2250}
2251EXPORT_SYMBOL(iwl_free_traffic_mem);
2252
2253void iwl_dbg_log_tx_data_frame(struct iwl_priv *priv,
2254 u16 length, struct ieee80211_hdr *header)
2255{
2256 __le16 fc;
2257 u16 len;
2258
2259 if (likely(!(iwl_debug_level & IWL_DL_TX)))
2260 return;
2261
2262 if (!priv->tx_traffic)
2263 return;
2264
2265 fc = header->frame_control;
2266 if (ieee80211_is_data(fc)) {
2267 len = (length > IWL_TRAFFIC_ENTRY_SIZE)
2268 ? IWL_TRAFFIC_ENTRY_SIZE : length;
2269 memcpy((priv->tx_traffic +
2270 (priv->tx_traffic_idx * IWL_TRAFFIC_ENTRY_SIZE)),
2271 header, len);
2272 priv->tx_traffic_idx =
2273 (priv->tx_traffic_idx + 1) % IWL_TRAFFIC_ENTRIES;
2274 }
2275}
2276EXPORT_SYMBOL(iwl_dbg_log_tx_data_frame);
2277
2278void iwl_dbg_log_rx_data_frame(struct iwl_priv *priv,
2279 u16 length, struct ieee80211_hdr *header)
2280{
2281 __le16 fc;
2282 u16 len;
2283
2284 if (likely(!(iwl_debug_level & IWL_DL_RX)))
2285 return;
2286
2287 if (!priv->rx_traffic)
2288 return;
2289
2290 fc = header->frame_control;
2291 if (ieee80211_is_data(fc)) {
2292 len = (length > IWL_TRAFFIC_ENTRY_SIZE)
2293 ? IWL_TRAFFIC_ENTRY_SIZE : length;
2294 memcpy((priv->rx_traffic +
2295 (priv->rx_traffic_idx * IWL_TRAFFIC_ENTRY_SIZE)),
2296 header, len);
2297 priv->rx_traffic_idx =
2298 (priv->rx_traffic_idx + 1) % IWL_TRAFFIC_ENTRIES;
2299 }
2300}
2301EXPORT_SYMBOL(iwl_dbg_log_rx_data_frame);
22fdf3c9
WYG
2302
2303const char *get_mgmt_string(int cmd)
2304{
2305 switch (cmd) {
2306 IWL_CMD(MANAGEMENT_ASSOC_REQ);
2307 IWL_CMD(MANAGEMENT_ASSOC_RESP);
2308 IWL_CMD(MANAGEMENT_REASSOC_REQ);
2309 IWL_CMD(MANAGEMENT_REASSOC_RESP);
2310 IWL_CMD(MANAGEMENT_PROBE_REQ);
2311 IWL_CMD(MANAGEMENT_PROBE_RESP);
2312 IWL_CMD(MANAGEMENT_BEACON);
2313 IWL_CMD(MANAGEMENT_ATIM);
2314 IWL_CMD(MANAGEMENT_DISASSOC);
2315 IWL_CMD(MANAGEMENT_AUTH);
2316 IWL_CMD(MANAGEMENT_DEAUTH);
2317 IWL_CMD(MANAGEMENT_ACTION);
2318 default:
2319 return "UNKNOWN";
2320
2321 }
2322}
2323
2324const char *get_ctrl_string(int cmd)
2325{
2326 switch (cmd) {
2327 IWL_CMD(CONTROL_BACK_REQ);
2328 IWL_CMD(CONTROL_BACK);
2329 IWL_CMD(CONTROL_PSPOLL);
2330 IWL_CMD(CONTROL_RTS);
2331 IWL_CMD(CONTROL_CTS);
2332 IWL_CMD(CONTROL_ACK);
2333 IWL_CMD(CONTROL_CFEND);
2334 IWL_CMD(CONTROL_CFENDACK);
2335 default:
2336 return "UNKNOWN";
2337
2338 }
2339}
2340
7163b8a4 2341void iwl_clear_traffic_stats(struct iwl_priv *priv)
22fdf3c9
WYG
2342{
2343 memset(&priv->tx_stats, 0, sizeof(struct traffic_stats));
22fdf3c9 2344 memset(&priv->rx_stats, 0, sizeof(struct traffic_stats));
7163b8a4 2345 priv->led_tpt = 0;
22fdf3c9
WYG
2346}
2347
2348/*
2349 * if CONFIG_IWLWIFI_DEBUGFS defined, iwl_update_stats function will
2350 * record all the MGMT, CTRL and DATA pkt for both TX and Rx pass.
2351 * Use debugFs to display the rx/rx_statistics
2352 * if CONFIG_IWLWIFI_DEBUGFS not being defined, then no MGMT and CTRL
2353 * information will be recorded, but DATA pkt still will be recorded
2354 * for the reason of iwl_led.c need to control the led blinking based on
2355 * number of tx and rx data.
2356 *
2357 */
2358void iwl_update_stats(struct iwl_priv *priv, bool is_tx, __le16 fc, u16 len)
2359{
2360 struct traffic_stats *stats;
2361
2362 if (is_tx)
2363 stats = &priv->tx_stats;
2364 else
2365 stats = &priv->rx_stats;
2366
2367 if (ieee80211_is_mgmt(fc)) {
2368 switch (fc & cpu_to_le16(IEEE80211_FCTL_STYPE)) {
2369 case cpu_to_le16(IEEE80211_STYPE_ASSOC_REQ):
2370 stats->mgmt[MANAGEMENT_ASSOC_REQ]++;
2371 break;
2372 case cpu_to_le16(IEEE80211_STYPE_ASSOC_RESP):
2373 stats->mgmt[MANAGEMENT_ASSOC_RESP]++;
2374 break;
2375 case cpu_to_le16(IEEE80211_STYPE_REASSOC_REQ):
2376 stats->mgmt[MANAGEMENT_REASSOC_REQ]++;
2377 break;
2378 case cpu_to_le16(IEEE80211_STYPE_REASSOC_RESP):
2379 stats->mgmt[MANAGEMENT_REASSOC_RESP]++;
2380 break;
2381 case cpu_to_le16(IEEE80211_STYPE_PROBE_REQ):
2382 stats->mgmt[MANAGEMENT_PROBE_REQ]++;
2383 break;
2384 case cpu_to_le16(IEEE80211_STYPE_PROBE_RESP):
2385 stats->mgmt[MANAGEMENT_PROBE_RESP]++;
2386 break;
2387 case cpu_to_le16(IEEE80211_STYPE_BEACON):
2388 stats->mgmt[MANAGEMENT_BEACON]++;
2389 break;
2390 case cpu_to_le16(IEEE80211_STYPE_ATIM):
2391 stats->mgmt[MANAGEMENT_ATIM]++;
2392 break;
2393 case cpu_to_le16(IEEE80211_STYPE_DISASSOC):
2394 stats->mgmt[MANAGEMENT_DISASSOC]++;
2395 break;
2396 case cpu_to_le16(IEEE80211_STYPE_AUTH):
2397 stats->mgmt[MANAGEMENT_AUTH]++;
2398 break;
2399 case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
2400 stats->mgmt[MANAGEMENT_DEAUTH]++;
2401 break;
2402 case cpu_to_le16(IEEE80211_STYPE_ACTION):
2403 stats->mgmt[MANAGEMENT_ACTION]++;
2404 break;
2405 }
2406 } else if (ieee80211_is_ctl(fc)) {
2407 switch (fc & cpu_to_le16(IEEE80211_FCTL_STYPE)) {
2408 case cpu_to_le16(IEEE80211_STYPE_BACK_REQ):
2409 stats->ctrl[CONTROL_BACK_REQ]++;
2410 break;
2411 case cpu_to_le16(IEEE80211_STYPE_BACK):
2412 stats->ctrl[CONTROL_BACK]++;
2413 break;
2414 case cpu_to_le16(IEEE80211_STYPE_PSPOLL):
2415 stats->ctrl[CONTROL_PSPOLL]++;
2416 break;
2417 case cpu_to_le16(IEEE80211_STYPE_RTS):
2418 stats->ctrl[CONTROL_RTS]++;
2419 break;
2420 case cpu_to_le16(IEEE80211_STYPE_CTS):
2421 stats->ctrl[CONTROL_CTS]++;
2422 break;
2423 case cpu_to_le16(IEEE80211_STYPE_ACK):
2424 stats->ctrl[CONTROL_ACK]++;
2425 break;
2426 case cpu_to_le16(IEEE80211_STYPE_CFEND):
2427 stats->ctrl[CONTROL_CFEND]++;
2428 break;
2429 case cpu_to_le16(IEEE80211_STYPE_CFENDACK):
2430 stats->ctrl[CONTROL_CFENDACK]++;
2431 break;
2432 }
2433 } else {
2434 /* data */
2435 stats->data_cnt++;
2436 stats->data_bytes += len;
2437 }
d5f4cf71 2438 iwl_leds_background(priv);
22fdf3c9
WYG
2439}
2440EXPORT_SYMBOL(iwl_update_stats);
20594eb0
WYG
2441#endif
2442
a0ea9493 2443static const char *get_csr_string(int cmd)
696bdee3
WYG
2444{
2445 switch (cmd) {
2446 IWL_CMD(CSR_HW_IF_CONFIG_REG);
2447 IWL_CMD(CSR_INT_COALESCING);
2448 IWL_CMD(CSR_INT);
2449 IWL_CMD(CSR_INT_MASK);
2450 IWL_CMD(CSR_FH_INT_STATUS);
2451 IWL_CMD(CSR_GPIO_IN);
2452 IWL_CMD(CSR_RESET);
2453 IWL_CMD(CSR_GP_CNTRL);
2454 IWL_CMD(CSR_HW_REV);
2455 IWL_CMD(CSR_EEPROM_REG);
2456 IWL_CMD(CSR_EEPROM_GP);
2457 IWL_CMD(CSR_OTP_GP_REG);
2458 IWL_CMD(CSR_GIO_REG);
2459 IWL_CMD(CSR_GP_UCODE_REG);
2460 IWL_CMD(CSR_GP_DRIVER_REG);
2461 IWL_CMD(CSR_UCODE_DRV_GP1);
2462 IWL_CMD(CSR_UCODE_DRV_GP2);
2463 IWL_CMD(CSR_LED_REG);
2464 IWL_CMD(CSR_DRAM_INT_TBL_REG);
2465 IWL_CMD(CSR_GIO_CHICKEN_BITS);
2466 IWL_CMD(CSR_ANA_PLL_CFG);
2467 IWL_CMD(CSR_HW_REV_WA_REG);
2468 IWL_CMD(CSR_DBG_HPET_MEM_REG);
2469 default:
2470 return "UNKNOWN";
2471
2472 }
2473}
2474
2475void iwl_dump_csr(struct iwl_priv *priv)
2476{
2477 int i;
2478 u32 csr_tbl[] = {
2479 CSR_HW_IF_CONFIG_REG,
2480 CSR_INT_COALESCING,
2481 CSR_INT,
2482 CSR_INT_MASK,
2483 CSR_FH_INT_STATUS,
2484 CSR_GPIO_IN,
2485 CSR_RESET,
2486 CSR_GP_CNTRL,
2487 CSR_HW_REV,
2488 CSR_EEPROM_REG,
2489 CSR_EEPROM_GP,
2490 CSR_OTP_GP_REG,
2491 CSR_GIO_REG,
2492 CSR_GP_UCODE_REG,
2493 CSR_GP_DRIVER_REG,
2494 CSR_UCODE_DRV_GP1,
2495 CSR_UCODE_DRV_GP2,
2496 CSR_LED_REG,
2497 CSR_DRAM_INT_TBL_REG,
2498 CSR_GIO_CHICKEN_BITS,
2499 CSR_ANA_PLL_CFG,
2500 CSR_HW_REV_WA_REG,
2501 CSR_DBG_HPET_MEM_REG
2502 };
2503 IWL_ERR(priv, "CSR values:\n");
2504 IWL_ERR(priv, "(2nd byte of CSR_INT_COALESCING is "
2505 "CSR_INT_PERIODIC_REG)\n");
2506 for (i = 0; i < ARRAY_SIZE(csr_tbl); i++) {
2507 IWL_ERR(priv, " %25s: 0X%08x\n",
2508 get_csr_string(csr_tbl[i]),
2509 iwl_read32(priv, csr_tbl[i]));
2510 }
2511}
2512EXPORT_SYMBOL(iwl_dump_csr);
2513
a0ea9493 2514static const char *get_fh_string(int cmd)
1b3eb823
WYG
2515{
2516 switch (cmd) {
2517 IWL_CMD(FH_RSCSR_CHNL0_STTS_WPTR_REG);
2518 IWL_CMD(FH_RSCSR_CHNL0_RBDCB_BASE_REG);
2519 IWL_CMD(FH_RSCSR_CHNL0_WPTR);
2520 IWL_CMD(FH_MEM_RCSR_CHNL0_CONFIG_REG);
2521 IWL_CMD(FH_MEM_RSSR_SHARED_CTRL_REG);
2522 IWL_CMD(FH_MEM_RSSR_RX_STATUS_REG);
2523 IWL_CMD(FH_MEM_RSSR_RX_ENABLE_ERR_IRQ2DRV);
2524 IWL_CMD(FH_TSSR_TX_STATUS_REG);
2525 IWL_CMD(FH_TSSR_TX_ERROR_REG);
2526 default:
2527 return "UNKNOWN";
2528
2529 }
2530}
2531
2532int iwl_dump_fh(struct iwl_priv *priv, char **buf, bool display)
2533{
2534 int i;
2535#ifdef CONFIG_IWLWIFI_DEBUG
2536 int pos = 0;
2537 size_t bufsz = 0;
2538#endif
2539 u32 fh_tbl[] = {
2540 FH_RSCSR_CHNL0_STTS_WPTR_REG,
2541 FH_RSCSR_CHNL0_RBDCB_BASE_REG,
2542 FH_RSCSR_CHNL0_WPTR,
2543 FH_MEM_RCSR_CHNL0_CONFIG_REG,
2544 FH_MEM_RSSR_SHARED_CTRL_REG,
2545 FH_MEM_RSSR_RX_STATUS_REG,
2546 FH_MEM_RSSR_RX_ENABLE_ERR_IRQ2DRV,
2547 FH_TSSR_TX_STATUS_REG,
2548 FH_TSSR_TX_ERROR_REG
2549 };
2550#ifdef CONFIG_IWLWIFI_DEBUG
2551 if (display) {
2552 bufsz = ARRAY_SIZE(fh_tbl) * 48 + 40;
2553 *buf = kmalloc(bufsz, GFP_KERNEL);
2554 if (!*buf)
2555 return -ENOMEM;
2556 pos += scnprintf(*buf + pos, bufsz - pos,
2557 "FH register values:\n");
2558 for (i = 0; i < ARRAY_SIZE(fh_tbl); i++) {
2559 pos += scnprintf(*buf + pos, bufsz - pos,
2560 " %34s: 0X%08x\n",
2561 get_fh_string(fh_tbl[i]),
2562 iwl_read_direct32(priv, fh_tbl[i]));
2563 }
2564 return pos;
2565 }
2566#endif
2567 IWL_ERR(priv, "FH register values:\n");
2568 for (i = 0; i < ARRAY_SIZE(fh_tbl); i++) {
2569 IWL_ERR(priv, " %34s: 0X%08x\n",
2570 get_fh_string(fh_tbl[i]),
2571 iwl_read_direct32(priv, fh_tbl[i]));
2572 }
2573 return 0;
2574}
2575EXPORT_SYMBOL(iwl_dump_fh);
2576
a93e7973 2577static void iwl_force_rf_reset(struct iwl_priv *priv)
afbdd69a
WYG
2578{
2579 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2580 return;
2581
246ed355 2582 if (!iwl_is_any_associated(priv)) {
afbdd69a
WYG
2583 IWL_DEBUG_SCAN(priv, "force reset rejected: not associated\n");
2584 return;
2585 }
2586 /*
2587 * There is no easy and better way to force reset the radio,
2588 * the only known method is switching channel which will force to
2589 * reset and tune the radio.
2590 * Use internal short scan (single channel) operation to should
2591 * achieve this objective.
2592 * Driver should reset the radio when number of consecutive missed
2593 * beacon, or any other uCode error condition detected.
2594 */
2595 IWL_DEBUG_INFO(priv, "perform radio reset.\n");
2596 iwl_internal_short_hw_scan(priv);
afbdd69a 2597}
a93e7973 2598
a93e7973 2599
c04f9f22 2600int iwl_force_reset(struct iwl_priv *priv, int mode, bool external)
a93e7973 2601{
8a472da4
WYG
2602 struct iwl_force_reset *force_reset;
2603
a93e7973
WYG
2604 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2605 return -EINVAL;
2606
8a472da4
WYG
2607 if (mode >= IWL_MAX_FORCE_RESET) {
2608 IWL_DEBUG_INFO(priv, "invalid reset request.\n");
2609 return -EINVAL;
2610 }
2611 force_reset = &priv->force_reset[mode];
2612 force_reset->reset_request_count++;
c04f9f22
WYG
2613 if (!external) {
2614 if (force_reset->last_force_reset_jiffies &&
2615 time_after(force_reset->last_force_reset_jiffies +
2616 force_reset->reset_duration, jiffies)) {
2617 IWL_DEBUG_INFO(priv, "force reset rejected\n");
2618 force_reset->reset_reject_count++;
2619 return -EAGAIN;
2620 }
a93e7973 2621 }
8a472da4
WYG
2622 force_reset->reset_success_count++;
2623 force_reset->last_force_reset_jiffies = jiffies;
a93e7973 2624 IWL_DEBUG_INFO(priv, "perform force reset (%d)\n", mode);
a93e7973
WYG
2625 switch (mode) {
2626 case IWL_RF_RESET:
2627 iwl_force_rf_reset(priv);
2628 break;
2629 case IWL_FW_RESET:
c04f9f22
WYG
2630 /*
2631 * if the request is from external(ex: debugfs),
2632 * then always perform the request in regardless the module
2633 * parameter setting
2634 * if the request is from internal (uCode error or driver
2635 * detect failure), then fw_restart module parameter
2636 * need to be check before performing firmware reload
2637 */
2638 if (!external && !priv->cfg->mod_params->restart_fw) {
2639 IWL_DEBUG_INFO(priv, "Cancel firmware reload based on "
2640 "module parameter setting\n");
2641 break;
2642 }
a93e7973
WYG
2643 IWL_ERR(priv, "On demand firmware reload\n");
2644 /* Set the FW error flag -- cleared on iwl_down */
2645 set_bit(STATUS_FW_ERROR, &priv->status);
2646 wake_up_interruptible(&priv->wait_command_queue);
2647 /*
2648 * Keep the restart process from trying to send host
2649 * commands by clearing the INIT status bit
2650 */
2651 clear_bit(STATUS_READY, &priv->status);
2652 queue_work(priv->workqueue, &priv->restart);
2653 break;
a93e7973 2654 }
a93e7973
WYG
2655 return 0;
2656}
b74e31a9
WYG
2657EXPORT_SYMBOL(iwl_force_reset);
2658
2659/**
2660 * iwl_bg_monitor_recover - Timer callback to check for stuck queue and recover
2661 *
2662 * During normal condition (no queue is stuck), the timer is continually set to
2663 * execute every monitor_recover_period milliseconds after the last timer
2664 * expired. When the queue read_ptr is at the same place, the timer is
2665 * shorten to 100mSecs. This is
2666 * 1) to reduce the chance that the read_ptr may wrap around (not stuck)
2667 * 2) to detect the stuck queues quicker before the station and AP can
2668 * disassociate each other.
2669 *
2670 * This function monitors all the tx queues and recover from it if any
2671 * of the queues are stuck.
2672 * 1. It first check the cmd queue for stuck conditions. If it is stuck,
2673 * it will recover by resetting the firmware and return.
2674 * 2. Then, it checks for station association. If it associates it will check
2675 * other queues. If any queue is stuck, it will recover by resetting
2676 * the firmware.
2677 * Note: It the number of times the queue read_ptr to be at the same place to
2678 * be MAX_REPEAT+1 in order to consider to be stuck.
2679 */
2680/*
2681 * The maximum number of times the read pointer of the tx queue at the
2682 * same place without considering to be stuck.
2683 */
2684#define MAX_REPEAT (2)
2685static int iwl_check_stuck_queue(struct iwl_priv *priv, int cnt)
2686{
2687 struct iwl_tx_queue *txq;
2688 struct iwl_queue *q;
2689
2690 txq = &priv->txq[cnt];
2691 q = &txq->q;
2692 /* queue is empty, skip */
7cb1b088
WYG
2693 if (q->read_ptr == q->write_ptr)
2694 return 0;
2695
2696 if (q->read_ptr == q->last_read_ptr) {
2697 /* a queue has not been read from last time */
2698 if (q->repeat_same_read_ptr > MAX_REPEAT) {
2699 IWL_ERR(priv,
2700 "queue %d stuck %d time. Fw reload.\n",
2701 q->id, q->repeat_same_read_ptr);
b74e31a9 2702 q->repeat_same_read_ptr = 0;
7cb1b088
WYG
2703 iwl_force_reset(priv, IWL_FW_RESET, false);
2704 } else {
2705 q->repeat_same_read_ptr++;
2706 IWL_DEBUG_RADIO(priv,
2707 "queue %d, not read %d time\n",
2708 q->id,
2709 q->repeat_same_read_ptr);
2710 if (priv->cfg->bt_params &&
2711 !priv->cfg->bt_params->advanced_bt_coexist) {
2712 mod_timer(&priv->monitor_recover,
2713 jiffies + msecs_to_jiffies(
2714 IWL_ONE_HUNDRED_MSECS));
2715 return 1;
2716 }
b74e31a9 2717 }
7cb1b088
WYG
2718 } else {
2719 q->last_read_ptr = q->read_ptr;
2720 q->repeat_same_read_ptr = 0;
b74e31a9
WYG
2721 }
2722 return 0;
2723}
2724
2725void iwl_bg_monitor_recover(unsigned long data)
2726{
2727 struct iwl_priv *priv = (struct iwl_priv *)data;
2728 int cnt;
2729
2730 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2731 return;
2732
2733 /* monitor and check for stuck cmd queue */
13bb9483 2734 if (iwl_check_stuck_queue(priv, priv->cmd_queue))
b74e31a9
WYG
2735 return;
2736
2737 /* monitor and check for other stuck queues */
246ed355 2738 if (iwl_is_any_associated(priv)) {
b74e31a9
WYG
2739 for (cnt = 0; cnt < priv->hw_params.max_txq_num; cnt++) {
2740 /* skip as we already checked the command queue */
13bb9483 2741 if (cnt == priv->cmd_queue)
b74e31a9
WYG
2742 continue;
2743 if (iwl_check_stuck_queue(priv, cnt))
2744 return;
2745 }
2746 }
7cb1b088 2747 if (priv->cfg->base_params->monitor_recover_period) {
7bdc473c
WYG
2748 /*
2749 * Reschedule the timer to occur in
7cb1b088 2750 * priv->cfg->base_params->monitor_recover_period
7bdc473c
WYG
2751 */
2752 mod_timer(&priv->monitor_recover, jiffies + msecs_to_jiffies(
7cb1b088 2753 priv->cfg->base_params->monitor_recover_period));
7bdc473c 2754 }
b74e31a9
WYG
2755}
2756EXPORT_SYMBOL(iwl_bg_monitor_recover);
afbdd69a 2757
a0ee74cf
WYG
2758
2759/*
2760 * extended beacon time format
2761 * time in usec will be changed into a 32-bit value in extended:internal format
2762 * the extended part is the beacon counts
2763 * the internal part is the time in usec within one beacon interval
2764 */
2765u32 iwl_usecs_to_beacons(struct iwl_priv *priv, u32 usec, u32 beacon_interval)
2766{
2767 u32 quot;
2768 u32 rem;
2769 u32 interval = beacon_interval * TIME_UNIT;
2770
2771 if (!interval || !usec)
2772 return 0;
2773
2774 quot = (usec / interval) &
2775 (iwl_beacon_time_mask_high(priv,
2776 priv->hw_params.beacon_time_tsf_bits) >>
2777 priv->hw_params.beacon_time_tsf_bits);
2778 rem = (usec % interval) & iwl_beacon_time_mask_low(priv,
2779 priv->hw_params.beacon_time_tsf_bits);
2780
2781 return (quot << priv->hw_params.beacon_time_tsf_bits) + rem;
2782}
2783EXPORT_SYMBOL(iwl_usecs_to_beacons);
2784
2785/* base is usually what we get from ucode with each received frame,
2786 * the same as HW timer counter counting down
2787 */
2788__le32 iwl_add_beacon_time(struct iwl_priv *priv, u32 base,
2789 u32 addon, u32 beacon_interval)
2790{
2791 u32 base_low = base & iwl_beacon_time_mask_low(priv,
2792 priv->hw_params.beacon_time_tsf_bits);
2793 u32 addon_low = addon & iwl_beacon_time_mask_low(priv,
2794 priv->hw_params.beacon_time_tsf_bits);
2795 u32 interval = beacon_interval * TIME_UNIT;
2796 u32 res = (base & iwl_beacon_time_mask_high(priv,
2797 priv->hw_params.beacon_time_tsf_bits)) +
2798 (addon & iwl_beacon_time_mask_high(priv,
2799 priv->hw_params.beacon_time_tsf_bits));
2800
2801 if (base_low > addon_low)
2802 res += base_low - addon_low;
2803 else if (base_low < addon_low) {
2804 res += interval + base_low - addon_low;
2805 res += (1 << priv->hw_params.beacon_time_tsf_bits);
2806 } else
2807 res += (1 << priv->hw_params.beacon_time_tsf_bits);
2808
2809 return cpu_to_le32(res);
2810}
2811EXPORT_SYMBOL(iwl_add_beacon_time);
2812
6da3a13e
WYG
2813#ifdef CONFIG_PM
2814
2815int iwl_pci_suspend(struct pci_dev *pdev, pm_message_t state)
2816{
2817 struct iwl_priv *priv = pci_get_drvdata(pdev);
2818
2819 /*
2820 * This function is called when system goes into suspend state
2821 * mac80211 will call iwl_mac_stop() from the mac80211 suspend function
2822 * first but since iwl_mac_stop() has no knowledge of who the caller is,
2823 * it will not call apm_ops.stop() to stop the DMA operation.
2824 * Calling apm_ops.stop here to make sure we stop the DMA.
2825 */
2826 priv->cfg->ops->lib->apm_ops.stop(priv);
2827
2828 pci_save_state(pdev);
2829 pci_disable_device(pdev);
2830 pci_set_power_state(pdev, PCI_D3hot);
2831
2832 return 0;
2833}
2834EXPORT_SYMBOL(iwl_pci_suspend);
2835
2836int iwl_pci_resume(struct pci_dev *pdev)
2837{
2838 struct iwl_priv *priv = pci_get_drvdata(pdev);
2839 int ret;
0ab84cff 2840 bool hw_rfkill = false;
6da3a13e 2841
cd398c31
AK
2842 /*
2843 * We disable the RETRY_TIMEOUT register (0x41) to keep
2844 * PCI Tx retries from interfering with C3 CPU state.
2845 */
2846 pci_write_config_byte(pdev, PCI_CFG_RETRY_TIMEOUT, 0x00);
2847
6da3a13e
WYG
2848 pci_set_power_state(pdev, PCI_D0);
2849 ret = pci_enable_device(pdev);
2850 if (ret)
2851 return ret;
2852 pci_restore_state(pdev);
2853 iwl_enable_interrupts(priv);
2854
0ab84cff
JB
2855 if (!(iwl_read32(priv, CSR_GP_CNTRL) &
2856 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
2857 hw_rfkill = true;
2858
2859 if (hw_rfkill)
2860 set_bit(STATUS_RF_KILL_HW, &priv->status);
2861 else
2862 clear_bit(STATUS_RF_KILL_HW, &priv->status);
2863
2864 wiphy_rfkill_set_hw_state(priv->hw->wiphy, hw_rfkill);
2865
6da3a13e
WYG
2866 return 0;
2867}
2868EXPORT_SYMBOL(iwl_pci_resume);
2869
2870#endif /* CONFIG_PM */
This page took 1.203005 seconds and 5 git commands to generate.