iwlagn: remove un-necessary tx power ops
[deliverable/linux.git] / drivers / net / wireless / iwlwifi / iwl-core.c
1 /******************************************************************************
2 *
3 * GPL LICENSE SUMMARY
4 *
5 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of version 2 of the GNU General Public License as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
19 * USA
20 *
21 * The full GNU General Public License is included in this distribution
22 * in the file called LICENSE.GPL.
23 *
24 * Contact Information:
25 * Intel Linux Wireless <ilw@linux.intel.com>
26 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27 *****************************************************************************/
28
29 #include <linux/kernel.h>
30 #include <linux/module.h>
31 #include <linux/etherdevice.h>
32 #include <linux/sched.h>
33 #include <linux/slab.h>
34 #include <net/mac80211.h>
35
36 #include "iwl-eeprom.h"
37 #include "iwl-dev.h" /* FIXME: remove */
38 #include "iwl-debug.h"
39 #include "iwl-core.h"
40 #include "iwl-io.h"
41 #include "iwl-power.h"
42 #include "iwl-sta.h"
43 #include "iwl-helpers.h"
44 #include "iwl-agn.h"
45
46
47 /*
48 * set bt_coex_active to true, uCode will do kill/defer
49 * every time the priority line is asserted (BT is sending signals on the
50 * priority line in the PCIx).
51 * set bt_coex_active to false, uCode will ignore the BT activity and
52 * perform the normal operation
53 *
54 * User might experience transmit issue on some platform due to WiFi/BT
55 * co-exist problem. The possible behaviors are:
56 * Able to scan and finding all the available AP
57 * Not able to associate with any AP
58 * On those platforms, WiFi communication can be restored by set
59 * "bt_coex_active" module parameter to "false"
60 *
61 * default: bt_coex_active = true (BT_COEX_ENABLE)
62 */
63 bool bt_coex_active = true;
64 module_param(bt_coex_active, bool, S_IRUGO);
65 MODULE_PARM_DESC(bt_coex_active, "enable wifi/bluetooth co-exist");
66
67 u32 iwl_debug_level;
68
69 const u8 iwl_bcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
70
71 #define MAX_BIT_RATE_40_MHZ 150 /* Mbps */
72 #define MAX_BIT_RATE_20_MHZ 72 /* Mbps */
73 static void iwlcore_init_ht_hw_capab(const struct iwl_priv *priv,
74 struct ieee80211_sta_ht_cap *ht_info,
75 enum ieee80211_band band)
76 {
77 u16 max_bit_rate = 0;
78 u8 rx_chains_num = priv->hw_params.rx_chains_num;
79 u8 tx_chains_num = priv->hw_params.tx_chains_num;
80
81 ht_info->cap = 0;
82 memset(&ht_info->mcs, 0, sizeof(ht_info->mcs));
83
84 ht_info->ht_supported = true;
85
86 if (priv->cfg->ht_params &&
87 priv->cfg->ht_params->ht_greenfield_support)
88 ht_info->cap |= IEEE80211_HT_CAP_GRN_FLD;
89 ht_info->cap |= IEEE80211_HT_CAP_SGI_20;
90 max_bit_rate = MAX_BIT_RATE_20_MHZ;
91 if (priv->hw_params.ht40_channel & BIT(band)) {
92 ht_info->cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
93 ht_info->cap |= IEEE80211_HT_CAP_SGI_40;
94 ht_info->mcs.rx_mask[4] = 0x01;
95 max_bit_rate = MAX_BIT_RATE_40_MHZ;
96 }
97
98 if (iwlagn_mod_params.amsdu_size_8K)
99 ht_info->cap |= IEEE80211_HT_CAP_MAX_AMSDU;
100
101 ht_info->ampdu_factor = CFG_HT_RX_AMPDU_FACTOR_DEF;
102 if (priv->cfg->bt_params && priv->cfg->bt_params->ampdu_factor)
103 ht_info->ampdu_factor = priv->cfg->bt_params->ampdu_factor;
104 ht_info->ampdu_density = CFG_HT_MPDU_DENSITY_DEF;
105 if (priv->cfg->bt_params && priv->cfg->bt_params->ampdu_density)
106 ht_info->ampdu_density = priv->cfg->bt_params->ampdu_density;
107
108 ht_info->mcs.rx_mask[0] = 0xFF;
109 if (rx_chains_num >= 2)
110 ht_info->mcs.rx_mask[1] = 0xFF;
111 if (rx_chains_num >= 3)
112 ht_info->mcs.rx_mask[2] = 0xFF;
113
114 /* Highest supported Rx data rate */
115 max_bit_rate *= rx_chains_num;
116 WARN_ON(max_bit_rate & ~IEEE80211_HT_MCS_RX_HIGHEST_MASK);
117 ht_info->mcs.rx_highest = cpu_to_le16(max_bit_rate);
118
119 /* Tx MCS capabilities */
120 ht_info->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
121 if (tx_chains_num != rx_chains_num) {
122 ht_info->mcs.tx_params |= IEEE80211_HT_MCS_TX_RX_DIFF;
123 ht_info->mcs.tx_params |= ((tx_chains_num - 1) <<
124 IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT);
125 }
126 }
127
128 /**
129 * iwlcore_init_geos - Initialize mac80211's geo/channel info based from eeprom
130 */
131 int iwlcore_init_geos(struct iwl_priv *priv)
132 {
133 struct iwl_channel_info *ch;
134 struct ieee80211_supported_band *sband;
135 struct ieee80211_channel *channels;
136 struct ieee80211_channel *geo_ch;
137 struct ieee80211_rate *rates;
138 int i = 0;
139 s8 max_tx_power = IWLAGN_TX_POWER_TARGET_POWER_MIN;
140
141 if (priv->bands[IEEE80211_BAND_2GHZ].n_bitrates ||
142 priv->bands[IEEE80211_BAND_5GHZ].n_bitrates) {
143 IWL_DEBUG_INFO(priv, "Geography modes already initialized.\n");
144 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
145 return 0;
146 }
147
148 channels = kzalloc(sizeof(struct ieee80211_channel) *
149 priv->channel_count, GFP_KERNEL);
150 if (!channels)
151 return -ENOMEM;
152
153 rates = kzalloc((sizeof(struct ieee80211_rate) * IWL_RATE_COUNT_LEGACY),
154 GFP_KERNEL);
155 if (!rates) {
156 kfree(channels);
157 return -ENOMEM;
158 }
159
160 /* 5.2GHz channels start after the 2.4GHz channels */
161 sband = &priv->bands[IEEE80211_BAND_5GHZ];
162 sband->channels = &channels[ARRAY_SIZE(iwl_eeprom_band_1)];
163 /* just OFDM */
164 sband->bitrates = &rates[IWL_FIRST_OFDM_RATE];
165 sband->n_bitrates = IWL_RATE_COUNT_LEGACY - IWL_FIRST_OFDM_RATE;
166
167 if (priv->cfg->sku & IWL_SKU_N)
168 iwlcore_init_ht_hw_capab(priv, &sband->ht_cap,
169 IEEE80211_BAND_5GHZ);
170
171 sband = &priv->bands[IEEE80211_BAND_2GHZ];
172 sband->channels = channels;
173 /* OFDM & CCK */
174 sband->bitrates = rates;
175 sband->n_bitrates = IWL_RATE_COUNT_LEGACY;
176
177 if (priv->cfg->sku & IWL_SKU_N)
178 iwlcore_init_ht_hw_capab(priv, &sband->ht_cap,
179 IEEE80211_BAND_2GHZ);
180
181 priv->ieee_channels = channels;
182 priv->ieee_rates = rates;
183
184 for (i = 0; i < priv->channel_count; i++) {
185 ch = &priv->channel_info[i];
186
187 /* FIXME: might be removed if scan is OK */
188 if (!is_channel_valid(ch))
189 continue;
190
191 sband = &priv->bands[ch->band];
192
193 geo_ch = &sband->channels[sband->n_channels++];
194
195 geo_ch->center_freq =
196 ieee80211_channel_to_frequency(ch->channel, ch->band);
197 geo_ch->max_power = ch->max_power_avg;
198 geo_ch->max_antenna_gain = 0xff;
199 geo_ch->hw_value = ch->channel;
200
201 if (is_channel_valid(ch)) {
202 if (!(ch->flags & EEPROM_CHANNEL_IBSS))
203 geo_ch->flags |= IEEE80211_CHAN_NO_IBSS;
204
205 if (!(ch->flags & EEPROM_CHANNEL_ACTIVE))
206 geo_ch->flags |= IEEE80211_CHAN_PASSIVE_SCAN;
207
208 if (ch->flags & EEPROM_CHANNEL_RADAR)
209 geo_ch->flags |= IEEE80211_CHAN_RADAR;
210
211 geo_ch->flags |= ch->ht40_extension_channel;
212
213 if (ch->max_power_avg > max_tx_power)
214 max_tx_power = ch->max_power_avg;
215 } else {
216 geo_ch->flags |= IEEE80211_CHAN_DISABLED;
217 }
218
219 IWL_DEBUG_INFO(priv, "Channel %d Freq=%d[%sGHz] %s flag=0x%X\n",
220 ch->channel, geo_ch->center_freq,
221 is_channel_a_band(ch) ? "5.2" : "2.4",
222 geo_ch->flags & IEEE80211_CHAN_DISABLED ?
223 "restricted" : "valid",
224 geo_ch->flags);
225 }
226
227 priv->tx_power_device_lmt = max_tx_power;
228 priv->tx_power_user_lmt = max_tx_power;
229 priv->tx_power_next = max_tx_power;
230
231 if ((priv->bands[IEEE80211_BAND_5GHZ].n_channels == 0) &&
232 priv->cfg->sku & IWL_SKU_A) {
233 IWL_INFO(priv, "Incorrectly detected BG card as ABG. "
234 "Please send your PCI ID 0x%04X:0x%04X to maintainer.\n",
235 priv->pci_dev->device,
236 priv->pci_dev->subsystem_device);
237 priv->cfg->sku &= ~IWL_SKU_A;
238 }
239
240 IWL_INFO(priv, "Tunable channels: %d 802.11bg, %d 802.11a channels\n",
241 priv->bands[IEEE80211_BAND_2GHZ].n_channels,
242 priv->bands[IEEE80211_BAND_5GHZ].n_channels);
243
244 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
245
246 return 0;
247 }
248
249 /*
250 * iwlcore_free_geos - undo allocations in iwlcore_init_geos
251 */
252 void iwlcore_free_geos(struct iwl_priv *priv)
253 {
254 kfree(priv->ieee_channels);
255 kfree(priv->ieee_rates);
256 clear_bit(STATUS_GEO_CONFIGURED, &priv->status);
257 }
258
259 static bool iwl_is_channel_extension(struct iwl_priv *priv,
260 enum ieee80211_band band,
261 u16 channel, u8 extension_chan_offset)
262 {
263 const struct iwl_channel_info *ch_info;
264
265 ch_info = iwl_get_channel_info(priv, band, channel);
266 if (!is_channel_valid(ch_info))
267 return false;
268
269 if (extension_chan_offset == IEEE80211_HT_PARAM_CHA_SEC_ABOVE)
270 return !(ch_info->ht40_extension_channel &
271 IEEE80211_CHAN_NO_HT40PLUS);
272 else if (extension_chan_offset == IEEE80211_HT_PARAM_CHA_SEC_BELOW)
273 return !(ch_info->ht40_extension_channel &
274 IEEE80211_CHAN_NO_HT40MINUS);
275
276 return false;
277 }
278
279 bool iwl_is_ht40_tx_allowed(struct iwl_priv *priv,
280 struct iwl_rxon_context *ctx,
281 struct ieee80211_sta_ht_cap *ht_cap)
282 {
283 if (!ctx->ht.enabled || !ctx->ht.is_40mhz)
284 return false;
285
286 /*
287 * We do not check for IEEE80211_HT_CAP_SUP_WIDTH_20_40
288 * the bit will not set if it is pure 40MHz case
289 */
290 if (ht_cap && !ht_cap->ht_supported)
291 return false;
292
293 #ifdef CONFIG_IWLWIFI_DEBUGFS
294 if (priv->disable_ht40)
295 return false;
296 #endif
297
298 return iwl_is_channel_extension(priv, priv->band,
299 le16_to_cpu(ctx->staging.channel),
300 ctx->ht.extension_chan_offset);
301 }
302
303 static u16 iwl_adjust_beacon_interval(u16 beacon_val, u16 max_beacon_val)
304 {
305 u16 new_val;
306 u16 beacon_factor;
307
308 /*
309 * If mac80211 hasn't given us a beacon interval, program
310 * the default into the device (not checking this here
311 * would cause the adjustment below to return the maximum
312 * value, which may break PAN.)
313 */
314 if (!beacon_val)
315 return DEFAULT_BEACON_INTERVAL;
316
317 /*
318 * If the beacon interval we obtained from the peer
319 * is too large, we'll have to wake up more often
320 * (and in IBSS case, we'll beacon too much)
321 *
322 * For example, if max_beacon_val is 4096, and the
323 * requested beacon interval is 7000, we'll have to
324 * use 3500 to be able to wake up on the beacons.
325 *
326 * This could badly influence beacon detection stats.
327 */
328
329 beacon_factor = (beacon_val + max_beacon_val) / max_beacon_val;
330 new_val = beacon_val / beacon_factor;
331
332 if (!new_val)
333 new_val = max_beacon_val;
334
335 return new_val;
336 }
337
338 int iwl_send_rxon_timing(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
339 {
340 u64 tsf;
341 s32 interval_tm, rem;
342 struct ieee80211_conf *conf = NULL;
343 u16 beacon_int;
344 struct ieee80211_vif *vif = ctx->vif;
345
346 conf = ieee80211_get_hw_conf(priv->hw);
347
348 lockdep_assert_held(&priv->mutex);
349
350 memset(&ctx->timing, 0, sizeof(struct iwl_rxon_time_cmd));
351
352 ctx->timing.timestamp = cpu_to_le64(priv->timestamp);
353 ctx->timing.listen_interval = cpu_to_le16(conf->listen_interval);
354
355 beacon_int = vif ? vif->bss_conf.beacon_int : 0;
356
357 /*
358 * TODO: For IBSS we need to get atim_window from mac80211,
359 * for now just always use 0
360 */
361 ctx->timing.atim_window = 0;
362
363 if (ctx->ctxid == IWL_RXON_CTX_PAN &&
364 (!ctx->vif || ctx->vif->type != NL80211_IFTYPE_STATION) &&
365 iwl_is_associated(priv, IWL_RXON_CTX_BSS) &&
366 priv->contexts[IWL_RXON_CTX_BSS].vif &&
367 priv->contexts[IWL_RXON_CTX_BSS].vif->bss_conf.beacon_int) {
368 ctx->timing.beacon_interval =
369 priv->contexts[IWL_RXON_CTX_BSS].timing.beacon_interval;
370 beacon_int = le16_to_cpu(ctx->timing.beacon_interval);
371 } else if (ctx->ctxid == IWL_RXON_CTX_BSS &&
372 iwl_is_associated(priv, IWL_RXON_CTX_PAN) &&
373 priv->contexts[IWL_RXON_CTX_PAN].vif &&
374 priv->contexts[IWL_RXON_CTX_PAN].vif->bss_conf.beacon_int &&
375 (!iwl_is_associated_ctx(ctx) || !ctx->vif ||
376 !ctx->vif->bss_conf.beacon_int)) {
377 ctx->timing.beacon_interval =
378 priv->contexts[IWL_RXON_CTX_PAN].timing.beacon_interval;
379 beacon_int = le16_to_cpu(ctx->timing.beacon_interval);
380 } else {
381 beacon_int = iwl_adjust_beacon_interval(beacon_int,
382 priv->hw_params.max_beacon_itrvl * TIME_UNIT);
383 ctx->timing.beacon_interval = cpu_to_le16(beacon_int);
384 }
385
386 tsf = priv->timestamp; /* tsf is modifed by do_div: copy it */
387 interval_tm = beacon_int * TIME_UNIT;
388 rem = do_div(tsf, interval_tm);
389 ctx->timing.beacon_init_val = cpu_to_le32(interval_tm - rem);
390
391 ctx->timing.dtim_period = vif ? (vif->bss_conf.dtim_period ?: 1) : 1;
392
393 IWL_DEBUG_ASSOC(priv,
394 "beacon interval %d beacon timer %d beacon tim %d\n",
395 le16_to_cpu(ctx->timing.beacon_interval),
396 le32_to_cpu(ctx->timing.beacon_init_val),
397 le16_to_cpu(ctx->timing.atim_window));
398
399 return iwl_send_cmd_pdu(priv, ctx->rxon_timing_cmd,
400 sizeof(ctx->timing), &ctx->timing);
401 }
402
403 void iwl_set_rxon_hwcrypto(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
404 int hw_decrypt)
405 {
406 struct iwl_rxon_cmd *rxon = &ctx->staging;
407
408 if (hw_decrypt)
409 rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
410 else
411 rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
412
413 }
414
415 /* validate RXON structure is valid */
416 int iwl_check_rxon_cmd(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
417 {
418 struct iwl_rxon_cmd *rxon = &ctx->staging;
419 u32 errors = 0;
420
421 if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
422 if (rxon->flags & RXON_FLG_TGJ_NARROW_BAND_MSK) {
423 IWL_WARN(priv, "check 2.4G: wrong narrow\n");
424 errors |= BIT(0);
425 }
426 if (rxon->flags & RXON_FLG_RADAR_DETECT_MSK) {
427 IWL_WARN(priv, "check 2.4G: wrong radar\n");
428 errors |= BIT(1);
429 }
430 } else {
431 if (!(rxon->flags & RXON_FLG_SHORT_SLOT_MSK)) {
432 IWL_WARN(priv, "check 5.2G: not short slot!\n");
433 errors |= BIT(2);
434 }
435 if (rxon->flags & RXON_FLG_CCK_MSK) {
436 IWL_WARN(priv, "check 5.2G: CCK!\n");
437 errors |= BIT(3);
438 }
439 }
440 if ((rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1) {
441 IWL_WARN(priv, "mac/bssid mcast!\n");
442 errors |= BIT(4);
443 }
444
445 /* make sure basic rates 6Mbps and 1Mbps are supported */
446 if ((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0 &&
447 (rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0) {
448 IWL_WARN(priv, "neither 1 nor 6 are basic\n");
449 errors |= BIT(5);
450 }
451
452 if (le16_to_cpu(rxon->assoc_id) > 2007) {
453 IWL_WARN(priv, "aid > 2007\n");
454 errors |= BIT(6);
455 }
456
457 if ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
458 == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK)) {
459 IWL_WARN(priv, "CCK and short slot\n");
460 errors |= BIT(7);
461 }
462
463 if ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
464 == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK)) {
465 IWL_WARN(priv, "CCK and auto detect");
466 errors |= BIT(8);
467 }
468
469 if ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
470 RXON_FLG_TGG_PROTECT_MSK)) ==
471 RXON_FLG_TGG_PROTECT_MSK) {
472 IWL_WARN(priv, "TGg but no auto-detect\n");
473 errors |= BIT(9);
474 }
475
476 if (rxon->channel == 0) {
477 IWL_WARN(priv, "zero channel is invalid\n");
478 errors |= BIT(10);
479 }
480
481 WARN(errors, "Invalid RXON (%#x), channel %d",
482 errors, le16_to_cpu(rxon->channel));
483
484 return errors ? -EINVAL : 0;
485 }
486
487 /**
488 * iwl_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
489 * @priv: staging_rxon is compared to active_rxon
490 *
491 * If the RXON structure is changing enough to require a new tune,
492 * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
493 * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
494 */
495 int iwl_full_rxon_required(struct iwl_priv *priv,
496 struct iwl_rxon_context *ctx)
497 {
498 const struct iwl_rxon_cmd *staging = &ctx->staging;
499 const struct iwl_rxon_cmd *active = &ctx->active;
500
501 #define CHK(cond) \
502 if ((cond)) { \
503 IWL_DEBUG_INFO(priv, "need full RXON - " #cond "\n"); \
504 return 1; \
505 }
506
507 #define CHK_NEQ(c1, c2) \
508 if ((c1) != (c2)) { \
509 IWL_DEBUG_INFO(priv, "need full RXON - " \
510 #c1 " != " #c2 " - %d != %d\n", \
511 (c1), (c2)); \
512 return 1; \
513 }
514
515 /* These items are only settable from the full RXON command */
516 CHK(!iwl_is_associated_ctx(ctx));
517 CHK(compare_ether_addr(staging->bssid_addr, active->bssid_addr));
518 CHK(compare_ether_addr(staging->node_addr, active->node_addr));
519 CHK(compare_ether_addr(staging->wlap_bssid_addr,
520 active->wlap_bssid_addr));
521 CHK_NEQ(staging->dev_type, active->dev_type);
522 CHK_NEQ(staging->channel, active->channel);
523 CHK_NEQ(staging->air_propagation, active->air_propagation);
524 CHK_NEQ(staging->ofdm_ht_single_stream_basic_rates,
525 active->ofdm_ht_single_stream_basic_rates);
526 CHK_NEQ(staging->ofdm_ht_dual_stream_basic_rates,
527 active->ofdm_ht_dual_stream_basic_rates);
528 CHK_NEQ(staging->ofdm_ht_triple_stream_basic_rates,
529 active->ofdm_ht_triple_stream_basic_rates);
530 CHK_NEQ(staging->assoc_id, active->assoc_id);
531
532 /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
533 * be updated with the RXON_ASSOC command -- however only some
534 * flag transitions are allowed using RXON_ASSOC */
535
536 /* Check if we are not switching bands */
537 CHK_NEQ(staging->flags & RXON_FLG_BAND_24G_MSK,
538 active->flags & RXON_FLG_BAND_24G_MSK);
539
540 /* Check if we are switching association toggle */
541 CHK_NEQ(staging->filter_flags & RXON_FILTER_ASSOC_MSK,
542 active->filter_flags & RXON_FILTER_ASSOC_MSK);
543
544 #undef CHK
545 #undef CHK_NEQ
546
547 return 0;
548 }
549
550 u8 iwl_rate_get_lowest_plcp(struct iwl_priv *priv,
551 struct iwl_rxon_context *ctx)
552 {
553 /*
554 * Assign the lowest rate -- should really get this from
555 * the beacon skb from mac80211.
556 */
557 if (ctx->staging.flags & RXON_FLG_BAND_24G_MSK)
558 return IWL_RATE_1M_PLCP;
559 else
560 return IWL_RATE_6M_PLCP;
561 }
562
563 static void _iwl_set_rxon_ht(struct iwl_priv *priv,
564 struct iwl_ht_config *ht_conf,
565 struct iwl_rxon_context *ctx)
566 {
567 struct iwl_rxon_cmd *rxon = &ctx->staging;
568
569 if (!ctx->ht.enabled) {
570 rxon->flags &= ~(RXON_FLG_CHANNEL_MODE_MSK |
571 RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK |
572 RXON_FLG_HT40_PROT_MSK |
573 RXON_FLG_HT_PROT_MSK);
574 return;
575 }
576
577 /* FIXME: if the definition of ht.protection changed, the "translation"
578 * will be needed for rxon->flags
579 */
580 rxon->flags |= cpu_to_le32(ctx->ht.protection << RXON_FLG_HT_OPERATING_MODE_POS);
581
582 /* Set up channel bandwidth:
583 * 20 MHz only, 20/40 mixed or pure 40 if ht40 ok */
584 /* clear the HT channel mode before set the mode */
585 rxon->flags &= ~(RXON_FLG_CHANNEL_MODE_MSK |
586 RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK);
587 if (iwl_is_ht40_tx_allowed(priv, ctx, NULL)) {
588 /* pure ht40 */
589 if (ctx->ht.protection == IEEE80211_HT_OP_MODE_PROTECTION_20MHZ) {
590 rxon->flags |= RXON_FLG_CHANNEL_MODE_PURE_40;
591 /* Note: control channel is opposite of extension channel */
592 switch (ctx->ht.extension_chan_offset) {
593 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
594 rxon->flags &= ~RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
595 break;
596 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
597 rxon->flags |= RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
598 break;
599 }
600 } else {
601 /* Note: control channel is opposite of extension channel */
602 switch (ctx->ht.extension_chan_offset) {
603 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
604 rxon->flags &= ~(RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK);
605 rxon->flags |= RXON_FLG_CHANNEL_MODE_MIXED;
606 break;
607 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
608 rxon->flags |= RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
609 rxon->flags |= RXON_FLG_CHANNEL_MODE_MIXED;
610 break;
611 case IEEE80211_HT_PARAM_CHA_SEC_NONE:
612 default:
613 /* channel location only valid if in Mixed mode */
614 IWL_ERR(priv, "invalid extension channel offset\n");
615 break;
616 }
617 }
618 } else {
619 rxon->flags |= RXON_FLG_CHANNEL_MODE_LEGACY;
620 }
621
622 if (priv->cfg->ops->hcmd->set_rxon_chain)
623 priv->cfg->ops->hcmd->set_rxon_chain(priv, ctx);
624
625 IWL_DEBUG_ASSOC(priv, "rxon flags 0x%X operation mode :0x%X "
626 "extension channel offset 0x%x\n",
627 le32_to_cpu(rxon->flags), ctx->ht.protection,
628 ctx->ht.extension_chan_offset);
629 }
630
631 void iwl_set_rxon_ht(struct iwl_priv *priv, struct iwl_ht_config *ht_conf)
632 {
633 struct iwl_rxon_context *ctx;
634
635 for_each_context(priv, ctx)
636 _iwl_set_rxon_ht(priv, ht_conf, ctx);
637 }
638
639 /* Return valid, unused, channel for a passive scan to reset the RF */
640 u8 iwl_get_single_channel_number(struct iwl_priv *priv,
641 enum ieee80211_band band)
642 {
643 const struct iwl_channel_info *ch_info;
644 int i;
645 u8 channel = 0;
646 u8 min, max;
647 struct iwl_rxon_context *ctx;
648
649 if (band == IEEE80211_BAND_5GHZ) {
650 min = 14;
651 max = priv->channel_count;
652 } else {
653 min = 0;
654 max = 14;
655 }
656
657 for (i = min; i < max; i++) {
658 bool busy = false;
659
660 for_each_context(priv, ctx) {
661 busy = priv->channel_info[i].channel ==
662 le16_to_cpu(ctx->staging.channel);
663 if (busy)
664 break;
665 }
666
667 if (busy)
668 continue;
669
670 channel = priv->channel_info[i].channel;
671 ch_info = iwl_get_channel_info(priv, band, channel);
672 if (is_channel_valid(ch_info))
673 break;
674 }
675
676 return channel;
677 }
678
679 /**
680 * iwl_set_rxon_channel - Set the band and channel values in staging RXON
681 * @ch: requested channel as a pointer to struct ieee80211_channel
682
683 * NOTE: Does not commit to the hardware; it sets appropriate bit fields
684 * in the staging RXON flag structure based on the ch->band
685 */
686 int iwl_set_rxon_channel(struct iwl_priv *priv, struct ieee80211_channel *ch,
687 struct iwl_rxon_context *ctx)
688 {
689 enum ieee80211_band band = ch->band;
690 u16 channel = ch->hw_value;
691
692 if ((le16_to_cpu(ctx->staging.channel) == channel) &&
693 (priv->band == band))
694 return 0;
695
696 ctx->staging.channel = cpu_to_le16(channel);
697 if (band == IEEE80211_BAND_5GHZ)
698 ctx->staging.flags &= ~RXON_FLG_BAND_24G_MSK;
699 else
700 ctx->staging.flags |= RXON_FLG_BAND_24G_MSK;
701
702 priv->band = band;
703
704 IWL_DEBUG_INFO(priv, "Staging channel set to %d [%d]\n", channel, band);
705
706 return 0;
707 }
708
709 void iwl_set_flags_for_band(struct iwl_priv *priv,
710 struct iwl_rxon_context *ctx,
711 enum ieee80211_band band,
712 struct ieee80211_vif *vif)
713 {
714 if (band == IEEE80211_BAND_5GHZ) {
715 ctx->staging.flags &=
716 ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
717 | RXON_FLG_CCK_MSK);
718 ctx->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
719 } else {
720 /* Copied from iwl_post_associate() */
721 if (vif && vif->bss_conf.use_short_slot)
722 ctx->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
723 else
724 ctx->staging.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
725
726 ctx->staging.flags |= RXON_FLG_BAND_24G_MSK;
727 ctx->staging.flags |= RXON_FLG_AUTO_DETECT_MSK;
728 ctx->staging.flags &= ~RXON_FLG_CCK_MSK;
729 }
730 }
731
732 /*
733 * initialize rxon structure with default values from eeprom
734 */
735 void iwl_connection_init_rx_config(struct iwl_priv *priv,
736 struct iwl_rxon_context *ctx)
737 {
738 const struct iwl_channel_info *ch_info;
739
740 memset(&ctx->staging, 0, sizeof(ctx->staging));
741
742 if (!ctx->vif) {
743 ctx->staging.dev_type = ctx->unused_devtype;
744 } else switch (ctx->vif->type) {
745 case NL80211_IFTYPE_AP:
746 ctx->staging.dev_type = ctx->ap_devtype;
747 break;
748
749 case NL80211_IFTYPE_STATION:
750 ctx->staging.dev_type = ctx->station_devtype;
751 ctx->staging.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
752 break;
753
754 case NL80211_IFTYPE_ADHOC:
755 ctx->staging.dev_type = ctx->ibss_devtype;
756 ctx->staging.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
757 ctx->staging.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
758 RXON_FILTER_ACCEPT_GRP_MSK;
759 break;
760
761 default:
762 IWL_ERR(priv, "Unsupported interface type %d\n",
763 ctx->vif->type);
764 break;
765 }
766
767 #if 0
768 /* TODO: Figure out when short_preamble would be set and cache from
769 * that */
770 if (!hw_to_local(priv->hw)->short_preamble)
771 ctx->staging.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
772 else
773 ctx->staging.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
774 #endif
775
776 ch_info = iwl_get_channel_info(priv, priv->band,
777 le16_to_cpu(ctx->active.channel));
778
779 if (!ch_info)
780 ch_info = &priv->channel_info[0];
781
782 ctx->staging.channel = cpu_to_le16(ch_info->channel);
783 priv->band = ch_info->band;
784
785 iwl_set_flags_for_band(priv, ctx, priv->band, ctx->vif);
786
787 ctx->staging.ofdm_basic_rates =
788 (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
789 ctx->staging.cck_basic_rates =
790 (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
791
792 /* clear both MIX and PURE40 mode flag */
793 ctx->staging.flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED |
794 RXON_FLG_CHANNEL_MODE_PURE_40);
795 if (ctx->vif)
796 memcpy(ctx->staging.node_addr, ctx->vif->addr, ETH_ALEN);
797
798 ctx->staging.ofdm_ht_single_stream_basic_rates = 0xff;
799 ctx->staging.ofdm_ht_dual_stream_basic_rates = 0xff;
800 ctx->staging.ofdm_ht_triple_stream_basic_rates = 0xff;
801 }
802
803 void iwl_set_rate(struct iwl_priv *priv)
804 {
805 const struct ieee80211_supported_band *hw = NULL;
806 struct ieee80211_rate *rate;
807 struct iwl_rxon_context *ctx;
808 int i;
809
810 hw = iwl_get_hw_mode(priv, priv->band);
811 if (!hw) {
812 IWL_ERR(priv, "Failed to set rate: unable to get hw mode\n");
813 return;
814 }
815
816 priv->active_rate = 0;
817
818 for (i = 0; i < hw->n_bitrates; i++) {
819 rate = &(hw->bitrates[i]);
820 if (rate->hw_value < IWL_RATE_COUNT_LEGACY)
821 priv->active_rate |= (1 << rate->hw_value);
822 }
823
824 IWL_DEBUG_RATE(priv, "Set active_rate = %0x\n", priv->active_rate);
825
826 for_each_context(priv, ctx) {
827 ctx->staging.cck_basic_rates =
828 (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
829
830 ctx->staging.ofdm_basic_rates =
831 (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
832 }
833 }
834
835 void iwl_chswitch_done(struct iwl_priv *priv, bool is_success)
836 {
837 /*
838 * MULTI-FIXME
839 * See iwl_mac_channel_switch.
840 */
841 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
842
843 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
844 return;
845
846 if (priv->switch_rxon.switch_in_progress) {
847 ieee80211_chswitch_done(ctx->vif, is_success);
848 mutex_lock(&priv->mutex);
849 priv->switch_rxon.switch_in_progress = false;
850 mutex_unlock(&priv->mutex);
851 }
852 }
853
854 #ifdef CONFIG_IWLWIFI_DEBUG
855 void iwl_print_rx_config_cmd(struct iwl_priv *priv,
856 struct iwl_rxon_context *ctx)
857 {
858 struct iwl_rxon_cmd *rxon = &ctx->staging;
859
860 IWL_DEBUG_RADIO(priv, "RX CONFIG:\n");
861 iwl_print_hex_dump(priv, IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
862 IWL_DEBUG_RADIO(priv, "u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
863 IWL_DEBUG_RADIO(priv, "u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
864 IWL_DEBUG_RADIO(priv, "u32 filter_flags: 0x%08x\n",
865 le32_to_cpu(rxon->filter_flags));
866 IWL_DEBUG_RADIO(priv, "u8 dev_type: 0x%x\n", rxon->dev_type);
867 IWL_DEBUG_RADIO(priv, "u8 ofdm_basic_rates: 0x%02x\n",
868 rxon->ofdm_basic_rates);
869 IWL_DEBUG_RADIO(priv, "u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
870 IWL_DEBUG_RADIO(priv, "u8[6] node_addr: %pM\n", rxon->node_addr);
871 IWL_DEBUG_RADIO(priv, "u8[6] bssid_addr: %pM\n", rxon->bssid_addr);
872 IWL_DEBUG_RADIO(priv, "u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
873 }
874 #endif
875
876 static void iwlagn_abort_notification_waits(struct iwl_priv *priv)
877 {
878 unsigned long flags;
879 struct iwl_notification_wait *wait_entry;
880
881 spin_lock_irqsave(&priv->_agn.notif_wait_lock, flags);
882 list_for_each_entry(wait_entry, &priv->_agn.notif_waits, list)
883 wait_entry->aborted = true;
884 spin_unlock_irqrestore(&priv->_agn.notif_wait_lock, flags);
885
886 wake_up_all(&priv->_agn.notif_waitq);
887 }
888
889 void iwlagn_fw_error(struct iwl_priv *priv, bool ondemand)
890 {
891 unsigned int reload_msec;
892 unsigned long reload_jiffies;
893
894 /* Set the FW error flag -- cleared on iwl_down */
895 set_bit(STATUS_FW_ERROR, &priv->status);
896
897 /* Cancel currently queued command. */
898 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
899
900 iwlagn_abort_notification_waits(priv);
901
902 /* Keep the restart process from trying to send host
903 * commands by clearing the ready bit */
904 clear_bit(STATUS_READY, &priv->status);
905
906 wake_up_interruptible(&priv->wait_command_queue);
907
908 if (!ondemand) {
909 /*
910 * If firmware keep reloading, then it indicate something
911 * serious wrong and firmware having problem to recover
912 * from it. Instead of keep trying which will fill the syslog
913 * and hang the system, let's just stop it
914 */
915 reload_jiffies = jiffies;
916 reload_msec = jiffies_to_msecs((long) reload_jiffies -
917 (long) priv->reload_jiffies);
918 priv->reload_jiffies = reload_jiffies;
919 if (reload_msec <= IWL_MIN_RELOAD_DURATION) {
920 priv->reload_count++;
921 if (priv->reload_count >= IWL_MAX_CONTINUE_RELOAD_CNT) {
922 IWL_ERR(priv, "BUG_ON, Stop restarting\n");
923 return;
924 }
925 } else
926 priv->reload_count = 0;
927 }
928
929 if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
930 if (iwlagn_mod_params.restart_fw) {
931 IWL_DEBUG(priv, IWL_DL_FW_ERRORS,
932 "Restarting adapter due to uCode error.\n");
933 queue_work(priv->workqueue, &priv->restart);
934 } else
935 IWL_DEBUG(priv, IWL_DL_FW_ERRORS,
936 "Detected FW error, but not restarting\n");
937 }
938 }
939
940 /**
941 * iwl_irq_handle_error - called for HW or SW error interrupt from card
942 */
943 void iwl_irq_handle_error(struct iwl_priv *priv)
944 {
945 /* W/A for WiFi/WiMAX coex and WiMAX own the RF */
946 if (priv->cfg->internal_wimax_coex &&
947 (!(iwl_read_prph(priv, APMG_CLK_CTRL_REG) &
948 APMS_CLK_VAL_MRB_FUNC_MODE) ||
949 (iwl_read_prph(priv, APMG_PS_CTRL_REG) &
950 APMG_PS_CTRL_VAL_RESET_REQ))) {
951 /*
952 * Keep the restart process from trying to send host
953 * commands by clearing the ready bit.
954 */
955 clear_bit(STATUS_READY, &priv->status);
956 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
957 wake_up_interruptible(&priv->wait_command_queue);
958 IWL_ERR(priv, "RF is used by WiMAX\n");
959 return;
960 }
961
962 IWL_ERR(priv, "Loaded firmware version: %s\n",
963 priv->hw->wiphy->fw_version);
964
965 iwl_dump_nic_error_log(priv);
966 iwl_dump_csr(priv);
967 iwl_dump_fh(priv, NULL, false);
968 iwl_dump_nic_event_log(priv, false, NULL, false);
969 #ifdef CONFIG_IWLWIFI_DEBUG
970 if (iwl_get_debug_level(priv) & IWL_DL_FW_ERRORS)
971 iwl_print_rx_config_cmd(priv,
972 &priv->contexts[IWL_RXON_CTX_BSS]);
973 #endif
974
975 iwlagn_fw_error(priv, false);
976 }
977
978 static int iwl_apm_stop_master(struct iwl_priv *priv)
979 {
980 int ret = 0;
981
982 /* stop device's busmaster DMA activity */
983 iwl_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_STOP_MASTER);
984
985 ret = iwl_poll_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_MASTER_DISABLED,
986 CSR_RESET_REG_FLAG_MASTER_DISABLED, 100);
987 if (ret)
988 IWL_WARN(priv, "Master Disable Timed Out, 100 usec\n");
989
990 IWL_DEBUG_INFO(priv, "stop master\n");
991
992 return ret;
993 }
994
995 void iwl_apm_stop(struct iwl_priv *priv)
996 {
997 IWL_DEBUG_INFO(priv, "Stop card, put in low power state\n");
998
999 clear_bit(STATUS_DEVICE_ENABLED, &priv->status);
1000
1001 /* Stop device's DMA activity */
1002 iwl_apm_stop_master(priv);
1003
1004 /* Reset the entire device */
1005 iwl_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
1006
1007 udelay(10);
1008
1009 /*
1010 * Clear "initialization complete" bit to move adapter from
1011 * D0A* (powered-up Active) --> D0U* (Uninitialized) state.
1012 */
1013 iwl_clear_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
1014 }
1015
1016
1017 /*
1018 * Start up NIC's basic functionality after it has been reset
1019 * (e.g. after platform boot, or shutdown via iwl_apm_stop())
1020 * NOTE: This does not load uCode nor start the embedded processor
1021 */
1022 int iwl_apm_init(struct iwl_priv *priv)
1023 {
1024 int ret = 0;
1025 u16 lctl;
1026
1027 IWL_DEBUG_INFO(priv, "Init card's basic functions\n");
1028
1029 /*
1030 * Use "set_bit" below rather than "write", to preserve any hardware
1031 * bits already set by default after reset.
1032 */
1033
1034 /* Disable L0S exit timer (platform NMI Work/Around) */
1035 iwl_set_bit(priv, CSR_GIO_CHICKEN_BITS,
1036 CSR_GIO_CHICKEN_BITS_REG_BIT_DIS_L0S_EXIT_TIMER);
1037
1038 /*
1039 * Disable L0s without affecting L1;
1040 * don't wait for ICH L0s (ICH bug W/A)
1041 */
1042 iwl_set_bit(priv, CSR_GIO_CHICKEN_BITS,
1043 CSR_GIO_CHICKEN_BITS_REG_BIT_L1A_NO_L0S_RX);
1044
1045 /* Set FH wait threshold to maximum (HW error during stress W/A) */
1046 iwl_set_bit(priv, CSR_DBG_HPET_MEM_REG, CSR_DBG_HPET_MEM_REG_VAL);
1047
1048 /*
1049 * Enable HAP INTA (interrupt from management bus) to
1050 * wake device's PCI Express link L1a -> L0s
1051 */
1052 iwl_set_bit(priv, CSR_HW_IF_CONFIG_REG,
1053 CSR_HW_IF_CONFIG_REG_BIT_HAP_WAKE_L1A);
1054
1055 /*
1056 * HW bug W/A for instability in PCIe bus L0->L0S->L1 transition.
1057 * Check if BIOS (or OS) enabled L1-ASPM on this device.
1058 * If so (likely), disable L0S, so device moves directly L0->L1;
1059 * costs negligible amount of power savings.
1060 * If not (unlikely), enable L0S, so there is at least some
1061 * power savings, even without L1.
1062 */
1063 lctl = iwl_pcie_link_ctl(priv);
1064 if ((lctl & PCI_CFG_LINK_CTRL_VAL_L1_EN) ==
1065 PCI_CFG_LINK_CTRL_VAL_L1_EN) {
1066 /* L1-ASPM enabled; disable(!) L0S */
1067 iwl_set_bit(priv, CSR_GIO_REG,
1068 CSR_GIO_REG_VAL_L0S_ENABLED);
1069 IWL_DEBUG_POWER(priv, "L1 Enabled; Disabling L0S\n");
1070 } else {
1071 /* L1-ASPM disabled; enable(!) L0S */
1072 iwl_clear_bit(priv, CSR_GIO_REG,
1073 CSR_GIO_REG_VAL_L0S_ENABLED);
1074 IWL_DEBUG_POWER(priv, "L1 Disabled; Enabling L0S\n");
1075 }
1076
1077 /* Configure analog phase-lock-loop before activating to D0A */
1078 if (priv->cfg->base_params->pll_cfg_val)
1079 iwl_set_bit(priv, CSR_ANA_PLL_CFG,
1080 priv->cfg->base_params->pll_cfg_val);
1081
1082 /*
1083 * Set "initialization complete" bit to move adapter from
1084 * D0U* --> D0A* (powered-up active) state.
1085 */
1086 iwl_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
1087
1088 /*
1089 * Wait for clock stabilization; once stabilized, access to
1090 * device-internal resources is supported, e.g. iwl_write_prph()
1091 * and accesses to uCode SRAM.
1092 */
1093 ret = iwl_poll_bit(priv, CSR_GP_CNTRL,
1094 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
1095 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25000);
1096 if (ret < 0) {
1097 IWL_DEBUG_INFO(priv, "Failed to init the card\n");
1098 goto out;
1099 }
1100
1101 /*
1102 * Enable DMA clock and wait for it to stabilize.
1103 *
1104 * Write to "CLK_EN_REG"; "1" bits enable clocks, while "0" bits
1105 * do not disable clocks. This preserves any hardware bits already
1106 * set by default in "CLK_CTRL_REG" after reset.
1107 */
1108 iwl_write_prph(priv, APMG_CLK_EN_REG, APMG_CLK_VAL_DMA_CLK_RQT);
1109 udelay(20);
1110
1111 /* Disable L1-Active */
1112 iwl_set_bits_prph(priv, APMG_PCIDEV_STT_REG,
1113 APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
1114
1115 set_bit(STATUS_DEVICE_ENABLED, &priv->status);
1116
1117 out:
1118 return ret;
1119 }
1120
1121
1122 int iwl_set_tx_power(struct iwl_priv *priv, s8 tx_power, bool force)
1123 {
1124 int ret;
1125 s8 prev_tx_power;
1126 bool defer;
1127 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
1128
1129 lockdep_assert_held(&priv->mutex);
1130
1131 if (priv->tx_power_user_lmt == tx_power && !force)
1132 return 0;
1133
1134 if (tx_power < IWLAGN_TX_POWER_TARGET_POWER_MIN) {
1135 IWL_WARN(priv,
1136 "Requested user TXPOWER %d below lower limit %d.\n",
1137 tx_power,
1138 IWLAGN_TX_POWER_TARGET_POWER_MIN);
1139 return -EINVAL;
1140 }
1141
1142 if (tx_power > priv->tx_power_device_lmt) {
1143 IWL_WARN(priv,
1144 "Requested user TXPOWER %d above upper limit %d.\n",
1145 tx_power, priv->tx_power_device_lmt);
1146 return -EINVAL;
1147 }
1148
1149 if (!iwl_is_ready_rf(priv))
1150 return -EIO;
1151
1152 /* scan complete and commit_rxon use tx_power_next value,
1153 * it always need to be updated for newest request */
1154 priv->tx_power_next = tx_power;
1155
1156 /* do not set tx power when scanning or channel changing */
1157 defer = test_bit(STATUS_SCANNING, &priv->status) ||
1158 memcmp(&ctx->active, &ctx->staging, sizeof(ctx->staging));
1159 if (defer && !force) {
1160 IWL_DEBUG_INFO(priv, "Deferring tx power set\n");
1161 return 0;
1162 }
1163
1164 prev_tx_power = priv->tx_power_user_lmt;
1165 priv->tx_power_user_lmt = tx_power;
1166
1167 ret = iwlagn_send_tx_power(priv);
1168
1169 /* if fail to set tx_power, restore the orig. tx power */
1170 if (ret) {
1171 priv->tx_power_user_lmt = prev_tx_power;
1172 priv->tx_power_next = prev_tx_power;
1173 }
1174 return ret;
1175 }
1176
1177 void iwl_send_bt_config(struct iwl_priv *priv)
1178 {
1179 struct iwl_bt_cmd bt_cmd = {
1180 .lead_time = BT_LEAD_TIME_DEF,
1181 .max_kill = BT_MAX_KILL_DEF,
1182 .kill_ack_mask = 0,
1183 .kill_cts_mask = 0,
1184 };
1185
1186 if (!bt_coex_active)
1187 bt_cmd.flags = BT_COEX_DISABLE;
1188 else
1189 bt_cmd.flags = BT_COEX_ENABLE;
1190
1191 priv->bt_enable_flag = bt_cmd.flags;
1192 IWL_DEBUG_INFO(priv, "BT coex %s\n",
1193 (bt_cmd.flags == BT_COEX_DISABLE) ? "disable" : "active");
1194
1195 if (iwl_send_cmd_pdu(priv, REPLY_BT_CONFIG,
1196 sizeof(struct iwl_bt_cmd), &bt_cmd))
1197 IWL_ERR(priv, "failed to send BT Coex Config\n");
1198 }
1199
1200 int iwl_send_statistics_request(struct iwl_priv *priv, u8 flags, bool clear)
1201 {
1202 struct iwl_statistics_cmd statistics_cmd = {
1203 .configuration_flags =
1204 clear ? IWL_STATS_CONF_CLEAR_STATS : 0,
1205 };
1206
1207 if (flags & CMD_ASYNC)
1208 return iwl_send_cmd_pdu_async(priv, REPLY_STATISTICS_CMD,
1209 sizeof(struct iwl_statistics_cmd),
1210 &statistics_cmd, NULL);
1211 else
1212 return iwl_send_cmd_pdu(priv, REPLY_STATISTICS_CMD,
1213 sizeof(struct iwl_statistics_cmd),
1214 &statistics_cmd);
1215 }
1216
1217 void iwl_clear_isr_stats(struct iwl_priv *priv)
1218 {
1219 memset(&priv->isr_stats, 0, sizeof(priv->isr_stats));
1220 }
1221
1222 int iwl_mac_conf_tx(struct ieee80211_hw *hw, u16 queue,
1223 const struct ieee80211_tx_queue_params *params)
1224 {
1225 struct iwl_priv *priv = hw->priv;
1226 struct iwl_rxon_context *ctx;
1227 unsigned long flags;
1228 int q;
1229
1230 IWL_DEBUG_MAC80211(priv, "enter\n");
1231
1232 if (!iwl_is_ready_rf(priv)) {
1233 IWL_DEBUG_MAC80211(priv, "leave - RF not ready\n");
1234 return -EIO;
1235 }
1236
1237 if (queue >= AC_NUM) {
1238 IWL_DEBUG_MAC80211(priv, "leave - queue >= AC_NUM %d\n", queue);
1239 return 0;
1240 }
1241
1242 q = AC_NUM - 1 - queue;
1243
1244 spin_lock_irqsave(&priv->lock, flags);
1245
1246 /*
1247 * MULTI-FIXME
1248 * This may need to be done per interface in nl80211/cfg80211/mac80211.
1249 */
1250 for_each_context(priv, ctx) {
1251 ctx->qos_data.def_qos_parm.ac[q].cw_min =
1252 cpu_to_le16(params->cw_min);
1253 ctx->qos_data.def_qos_parm.ac[q].cw_max =
1254 cpu_to_le16(params->cw_max);
1255 ctx->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
1256 ctx->qos_data.def_qos_parm.ac[q].edca_txop =
1257 cpu_to_le16((params->txop * 32));
1258
1259 ctx->qos_data.def_qos_parm.ac[q].reserved1 = 0;
1260 }
1261
1262 spin_unlock_irqrestore(&priv->lock, flags);
1263
1264 IWL_DEBUG_MAC80211(priv, "leave\n");
1265 return 0;
1266 }
1267
1268 int iwl_mac_tx_last_beacon(struct ieee80211_hw *hw)
1269 {
1270 struct iwl_priv *priv = hw->priv;
1271
1272 return priv->ibss_manager == IWL_IBSS_MANAGER;
1273 }
1274
1275 static int iwl_set_mode(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
1276 {
1277 iwl_connection_init_rx_config(priv, ctx);
1278
1279 if (priv->cfg->ops->hcmd->set_rxon_chain)
1280 priv->cfg->ops->hcmd->set_rxon_chain(priv, ctx);
1281
1282 return iwlcore_commit_rxon(priv, ctx);
1283 }
1284
1285 static int iwl_setup_interface(struct iwl_priv *priv,
1286 struct iwl_rxon_context *ctx)
1287 {
1288 struct ieee80211_vif *vif = ctx->vif;
1289 int err;
1290
1291 lockdep_assert_held(&priv->mutex);
1292
1293 /*
1294 * This variable will be correct only when there's just
1295 * a single context, but all code using it is for hardware
1296 * that supports only one context.
1297 */
1298 priv->iw_mode = vif->type;
1299
1300 ctx->is_active = true;
1301
1302 err = iwl_set_mode(priv, ctx);
1303 if (err) {
1304 if (!ctx->always_active)
1305 ctx->is_active = false;
1306 return err;
1307 }
1308
1309 if (priv->cfg->bt_params && priv->cfg->bt_params->advanced_bt_coexist &&
1310 vif->type == NL80211_IFTYPE_ADHOC) {
1311 /*
1312 * pretend to have high BT traffic as long as we
1313 * are operating in IBSS mode, as this will cause
1314 * the rate scaling etc. to behave as intended.
1315 */
1316 priv->bt_traffic_load = IWL_BT_COEX_TRAFFIC_LOAD_HIGH;
1317 }
1318
1319 return 0;
1320 }
1321
1322 int iwl_mac_add_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1323 {
1324 struct iwl_priv *priv = hw->priv;
1325 struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
1326 struct iwl_rxon_context *tmp, *ctx = NULL;
1327 int err;
1328 enum nl80211_iftype viftype = ieee80211_vif_type_p2p(vif);
1329
1330 IWL_DEBUG_MAC80211(priv, "enter: type %d, addr %pM\n",
1331 viftype, vif->addr);
1332
1333 mutex_lock(&priv->mutex);
1334
1335 if (!iwl_is_ready_rf(priv)) {
1336 IWL_WARN(priv, "Try to add interface when device not ready\n");
1337 err = -EINVAL;
1338 goto out;
1339 }
1340
1341 for_each_context(priv, tmp) {
1342 u32 possible_modes =
1343 tmp->interface_modes | tmp->exclusive_interface_modes;
1344
1345 if (tmp->vif) {
1346 /* check if this busy context is exclusive */
1347 if (tmp->exclusive_interface_modes &
1348 BIT(tmp->vif->type)) {
1349 err = -EINVAL;
1350 goto out;
1351 }
1352 continue;
1353 }
1354
1355 if (!(possible_modes & BIT(viftype)))
1356 continue;
1357
1358 /* have maybe usable context w/o interface */
1359 ctx = tmp;
1360 break;
1361 }
1362
1363 if (!ctx) {
1364 err = -EOPNOTSUPP;
1365 goto out;
1366 }
1367
1368 vif_priv->ctx = ctx;
1369 ctx->vif = vif;
1370
1371 err = iwl_setup_interface(priv, ctx);
1372 if (!err)
1373 goto out;
1374
1375 ctx->vif = NULL;
1376 priv->iw_mode = NL80211_IFTYPE_STATION;
1377 out:
1378 mutex_unlock(&priv->mutex);
1379
1380 IWL_DEBUG_MAC80211(priv, "leave\n");
1381 return err;
1382 }
1383
1384 static void iwl_teardown_interface(struct iwl_priv *priv,
1385 struct ieee80211_vif *vif,
1386 bool mode_change)
1387 {
1388 struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
1389
1390 lockdep_assert_held(&priv->mutex);
1391
1392 if (priv->scan_vif == vif) {
1393 iwl_scan_cancel_timeout(priv, 200);
1394 iwl_force_scan_end(priv);
1395 }
1396
1397 if (!mode_change) {
1398 iwl_set_mode(priv, ctx);
1399 if (!ctx->always_active)
1400 ctx->is_active = false;
1401 }
1402
1403 /*
1404 * When removing the IBSS interface, overwrite the
1405 * BT traffic load with the stored one from the last
1406 * notification, if any. If this is a device that
1407 * doesn't implement this, this has no effect since
1408 * both values are the same and zero.
1409 */
1410 if (vif->type == NL80211_IFTYPE_ADHOC)
1411 priv->bt_traffic_load = priv->last_bt_traffic_load;
1412 }
1413
1414 void iwl_mac_remove_interface(struct ieee80211_hw *hw,
1415 struct ieee80211_vif *vif)
1416 {
1417 struct iwl_priv *priv = hw->priv;
1418 struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
1419
1420 IWL_DEBUG_MAC80211(priv, "enter\n");
1421
1422 mutex_lock(&priv->mutex);
1423
1424 WARN_ON(ctx->vif != vif);
1425 ctx->vif = NULL;
1426
1427 iwl_teardown_interface(priv, vif, false);
1428
1429 mutex_unlock(&priv->mutex);
1430
1431 IWL_DEBUG_MAC80211(priv, "leave\n");
1432
1433 }
1434
1435 int iwl_alloc_txq_mem(struct iwl_priv *priv)
1436 {
1437 if (!priv->txq)
1438 priv->txq = kzalloc(
1439 sizeof(struct iwl_tx_queue) *
1440 priv->cfg->base_params->num_of_queues,
1441 GFP_KERNEL);
1442 if (!priv->txq) {
1443 IWL_ERR(priv, "Not enough memory for txq\n");
1444 return -ENOMEM;
1445 }
1446 return 0;
1447 }
1448
1449 void iwl_free_txq_mem(struct iwl_priv *priv)
1450 {
1451 kfree(priv->txq);
1452 priv->txq = NULL;
1453 }
1454
1455 #ifdef CONFIG_IWLWIFI_DEBUGFS
1456
1457 #define IWL_TRAFFIC_DUMP_SIZE (IWL_TRAFFIC_ENTRY_SIZE * IWL_TRAFFIC_ENTRIES)
1458
1459 void iwl_reset_traffic_log(struct iwl_priv *priv)
1460 {
1461 priv->tx_traffic_idx = 0;
1462 priv->rx_traffic_idx = 0;
1463 if (priv->tx_traffic)
1464 memset(priv->tx_traffic, 0, IWL_TRAFFIC_DUMP_SIZE);
1465 if (priv->rx_traffic)
1466 memset(priv->rx_traffic, 0, IWL_TRAFFIC_DUMP_SIZE);
1467 }
1468
1469 int iwl_alloc_traffic_mem(struct iwl_priv *priv)
1470 {
1471 u32 traffic_size = IWL_TRAFFIC_DUMP_SIZE;
1472
1473 if (iwl_debug_level & IWL_DL_TX) {
1474 if (!priv->tx_traffic) {
1475 priv->tx_traffic =
1476 kzalloc(traffic_size, GFP_KERNEL);
1477 if (!priv->tx_traffic)
1478 return -ENOMEM;
1479 }
1480 }
1481 if (iwl_debug_level & IWL_DL_RX) {
1482 if (!priv->rx_traffic) {
1483 priv->rx_traffic =
1484 kzalloc(traffic_size, GFP_KERNEL);
1485 if (!priv->rx_traffic)
1486 return -ENOMEM;
1487 }
1488 }
1489 iwl_reset_traffic_log(priv);
1490 return 0;
1491 }
1492
1493 void iwl_free_traffic_mem(struct iwl_priv *priv)
1494 {
1495 kfree(priv->tx_traffic);
1496 priv->tx_traffic = NULL;
1497
1498 kfree(priv->rx_traffic);
1499 priv->rx_traffic = NULL;
1500 }
1501
1502 void iwl_dbg_log_tx_data_frame(struct iwl_priv *priv,
1503 u16 length, struct ieee80211_hdr *header)
1504 {
1505 __le16 fc;
1506 u16 len;
1507
1508 if (likely(!(iwl_debug_level & IWL_DL_TX)))
1509 return;
1510
1511 if (!priv->tx_traffic)
1512 return;
1513
1514 fc = header->frame_control;
1515 if (ieee80211_is_data(fc)) {
1516 len = (length > IWL_TRAFFIC_ENTRY_SIZE)
1517 ? IWL_TRAFFIC_ENTRY_SIZE : length;
1518 memcpy((priv->tx_traffic +
1519 (priv->tx_traffic_idx * IWL_TRAFFIC_ENTRY_SIZE)),
1520 header, len);
1521 priv->tx_traffic_idx =
1522 (priv->tx_traffic_idx + 1) % IWL_TRAFFIC_ENTRIES;
1523 }
1524 }
1525
1526 void iwl_dbg_log_rx_data_frame(struct iwl_priv *priv,
1527 u16 length, struct ieee80211_hdr *header)
1528 {
1529 __le16 fc;
1530 u16 len;
1531
1532 if (likely(!(iwl_debug_level & IWL_DL_RX)))
1533 return;
1534
1535 if (!priv->rx_traffic)
1536 return;
1537
1538 fc = header->frame_control;
1539 if (ieee80211_is_data(fc)) {
1540 len = (length > IWL_TRAFFIC_ENTRY_SIZE)
1541 ? IWL_TRAFFIC_ENTRY_SIZE : length;
1542 memcpy((priv->rx_traffic +
1543 (priv->rx_traffic_idx * IWL_TRAFFIC_ENTRY_SIZE)),
1544 header, len);
1545 priv->rx_traffic_idx =
1546 (priv->rx_traffic_idx + 1) % IWL_TRAFFIC_ENTRIES;
1547 }
1548 }
1549
1550 const char *get_mgmt_string(int cmd)
1551 {
1552 switch (cmd) {
1553 IWL_CMD(MANAGEMENT_ASSOC_REQ);
1554 IWL_CMD(MANAGEMENT_ASSOC_RESP);
1555 IWL_CMD(MANAGEMENT_REASSOC_REQ);
1556 IWL_CMD(MANAGEMENT_REASSOC_RESP);
1557 IWL_CMD(MANAGEMENT_PROBE_REQ);
1558 IWL_CMD(MANAGEMENT_PROBE_RESP);
1559 IWL_CMD(MANAGEMENT_BEACON);
1560 IWL_CMD(MANAGEMENT_ATIM);
1561 IWL_CMD(MANAGEMENT_DISASSOC);
1562 IWL_CMD(MANAGEMENT_AUTH);
1563 IWL_CMD(MANAGEMENT_DEAUTH);
1564 IWL_CMD(MANAGEMENT_ACTION);
1565 default:
1566 return "UNKNOWN";
1567
1568 }
1569 }
1570
1571 const char *get_ctrl_string(int cmd)
1572 {
1573 switch (cmd) {
1574 IWL_CMD(CONTROL_BACK_REQ);
1575 IWL_CMD(CONTROL_BACK);
1576 IWL_CMD(CONTROL_PSPOLL);
1577 IWL_CMD(CONTROL_RTS);
1578 IWL_CMD(CONTROL_CTS);
1579 IWL_CMD(CONTROL_ACK);
1580 IWL_CMD(CONTROL_CFEND);
1581 IWL_CMD(CONTROL_CFENDACK);
1582 default:
1583 return "UNKNOWN";
1584
1585 }
1586 }
1587
1588 void iwl_clear_traffic_stats(struct iwl_priv *priv)
1589 {
1590 memset(&priv->tx_stats, 0, sizeof(struct traffic_stats));
1591 memset(&priv->rx_stats, 0, sizeof(struct traffic_stats));
1592 }
1593
1594 /*
1595 * if CONFIG_IWLWIFI_DEBUGFS defined, iwl_update_stats function will
1596 * record all the MGMT, CTRL and DATA pkt for both TX and Rx pass.
1597 * Use debugFs to display the rx/rx_statistics
1598 * if CONFIG_IWLWIFI_DEBUGFS not being defined, then no MGMT and CTRL
1599 * information will be recorded, but DATA pkt still will be recorded
1600 * for the reason of iwl_led.c need to control the led blinking based on
1601 * number of tx and rx data.
1602 *
1603 */
1604 void iwl_update_stats(struct iwl_priv *priv, bool is_tx, __le16 fc, u16 len)
1605 {
1606 struct traffic_stats *stats;
1607
1608 if (is_tx)
1609 stats = &priv->tx_stats;
1610 else
1611 stats = &priv->rx_stats;
1612
1613 if (ieee80211_is_mgmt(fc)) {
1614 switch (fc & cpu_to_le16(IEEE80211_FCTL_STYPE)) {
1615 case cpu_to_le16(IEEE80211_STYPE_ASSOC_REQ):
1616 stats->mgmt[MANAGEMENT_ASSOC_REQ]++;
1617 break;
1618 case cpu_to_le16(IEEE80211_STYPE_ASSOC_RESP):
1619 stats->mgmt[MANAGEMENT_ASSOC_RESP]++;
1620 break;
1621 case cpu_to_le16(IEEE80211_STYPE_REASSOC_REQ):
1622 stats->mgmt[MANAGEMENT_REASSOC_REQ]++;
1623 break;
1624 case cpu_to_le16(IEEE80211_STYPE_REASSOC_RESP):
1625 stats->mgmt[MANAGEMENT_REASSOC_RESP]++;
1626 break;
1627 case cpu_to_le16(IEEE80211_STYPE_PROBE_REQ):
1628 stats->mgmt[MANAGEMENT_PROBE_REQ]++;
1629 break;
1630 case cpu_to_le16(IEEE80211_STYPE_PROBE_RESP):
1631 stats->mgmt[MANAGEMENT_PROBE_RESP]++;
1632 break;
1633 case cpu_to_le16(IEEE80211_STYPE_BEACON):
1634 stats->mgmt[MANAGEMENT_BEACON]++;
1635 break;
1636 case cpu_to_le16(IEEE80211_STYPE_ATIM):
1637 stats->mgmt[MANAGEMENT_ATIM]++;
1638 break;
1639 case cpu_to_le16(IEEE80211_STYPE_DISASSOC):
1640 stats->mgmt[MANAGEMENT_DISASSOC]++;
1641 break;
1642 case cpu_to_le16(IEEE80211_STYPE_AUTH):
1643 stats->mgmt[MANAGEMENT_AUTH]++;
1644 break;
1645 case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
1646 stats->mgmt[MANAGEMENT_DEAUTH]++;
1647 break;
1648 case cpu_to_le16(IEEE80211_STYPE_ACTION):
1649 stats->mgmt[MANAGEMENT_ACTION]++;
1650 break;
1651 }
1652 } else if (ieee80211_is_ctl(fc)) {
1653 switch (fc & cpu_to_le16(IEEE80211_FCTL_STYPE)) {
1654 case cpu_to_le16(IEEE80211_STYPE_BACK_REQ):
1655 stats->ctrl[CONTROL_BACK_REQ]++;
1656 break;
1657 case cpu_to_le16(IEEE80211_STYPE_BACK):
1658 stats->ctrl[CONTROL_BACK]++;
1659 break;
1660 case cpu_to_le16(IEEE80211_STYPE_PSPOLL):
1661 stats->ctrl[CONTROL_PSPOLL]++;
1662 break;
1663 case cpu_to_le16(IEEE80211_STYPE_RTS):
1664 stats->ctrl[CONTROL_RTS]++;
1665 break;
1666 case cpu_to_le16(IEEE80211_STYPE_CTS):
1667 stats->ctrl[CONTROL_CTS]++;
1668 break;
1669 case cpu_to_le16(IEEE80211_STYPE_ACK):
1670 stats->ctrl[CONTROL_ACK]++;
1671 break;
1672 case cpu_to_le16(IEEE80211_STYPE_CFEND):
1673 stats->ctrl[CONTROL_CFEND]++;
1674 break;
1675 case cpu_to_le16(IEEE80211_STYPE_CFENDACK):
1676 stats->ctrl[CONTROL_CFENDACK]++;
1677 break;
1678 }
1679 } else {
1680 /* data */
1681 stats->data_cnt++;
1682 stats->data_bytes += len;
1683 }
1684 }
1685 #endif
1686
1687 static void iwl_force_rf_reset(struct iwl_priv *priv)
1688 {
1689 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1690 return;
1691
1692 if (!iwl_is_any_associated(priv)) {
1693 IWL_DEBUG_SCAN(priv, "force reset rejected: not associated\n");
1694 return;
1695 }
1696 /*
1697 * There is no easy and better way to force reset the radio,
1698 * the only known method is switching channel which will force to
1699 * reset and tune the radio.
1700 * Use internal short scan (single channel) operation to should
1701 * achieve this objective.
1702 * Driver should reset the radio when number of consecutive missed
1703 * beacon, or any other uCode error condition detected.
1704 */
1705 IWL_DEBUG_INFO(priv, "perform radio reset.\n");
1706 iwl_internal_short_hw_scan(priv);
1707 }
1708
1709
1710 int iwl_force_reset(struct iwl_priv *priv, int mode, bool external)
1711 {
1712 struct iwl_force_reset *force_reset;
1713
1714 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1715 return -EINVAL;
1716
1717 if (mode >= IWL_MAX_FORCE_RESET) {
1718 IWL_DEBUG_INFO(priv, "invalid reset request.\n");
1719 return -EINVAL;
1720 }
1721 force_reset = &priv->force_reset[mode];
1722 force_reset->reset_request_count++;
1723 if (!external) {
1724 if (force_reset->last_force_reset_jiffies &&
1725 time_after(force_reset->last_force_reset_jiffies +
1726 force_reset->reset_duration, jiffies)) {
1727 IWL_DEBUG_INFO(priv, "force reset rejected\n");
1728 force_reset->reset_reject_count++;
1729 return -EAGAIN;
1730 }
1731 }
1732 force_reset->reset_success_count++;
1733 force_reset->last_force_reset_jiffies = jiffies;
1734 IWL_DEBUG_INFO(priv, "perform force reset (%d)\n", mode);
1735 switch (mode) {
1736 case IWL_RF_RESET:
1737 iwl_force_rf_reset(priv);
1738 break;
1739 case IWL_FW_RESET:
1740 /*
1741 * if the request is from external(ex: debugfs),
1742 * then always perform the request in regardless the module
1743 * parameter setting
1744 * if the request is from internal (uCode error or driver
1745 * detect failure), then fw_restart module parameter
1746 * need to be check before performing firmware reload
1747 */
1748 if (!external && !iwlagn_mod_params.restart_fw) {
1749 IWL_DEBUG_INFO(priv, "Cancel firmware reload based on "
1750 "module parameter setting\n");
1751 break;
1752 }
1753 IWL_ERR(priv, "On demand firmware reload\n");
1754 iwlagn_fw_error(priv, true);
1755 break;
1756 }
1757 return 0;
1758 }
1759
1760 int iwl_mac_change_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1761 enum nl80211_iftype newtype, bool newp2p)
1762 {
1763 struct iwl_priv *priv = hw->priv;
1764 struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
1765 struct iwl_rxon_context *bss_ctx = &priv->contexts[IWL_RXON_CTX_BSS];
1766 struct iwl_rxon_context *tmp;
1767 u32 interface_modes;
1768 int err;
1769
1770 newtype = ieee80211_iftype_p2p(newtype, newp2p);
1771
1772 mutex_lock(&priv->mutex);
1773
1774 if (!ctx->vif || !iwl_is_ready_rf(priv)) {
1775 /*
1776 * Huh? But wait ... this can maybe happen when
1777 * we're in the middle of a firmware restart!
1778 */
1779 err = -EBUSY;
1780 goto out;
1781 }
1782
1783 interface_modes = ctx->interface_modes | ctx->exclusive_interface_modes;
1784
1785 if (!(interface_modes & BIT(newtype))) {
1786 err = -EBUSY;
1787 goto out;
1788 }
1789
1790 /*
1791 * Refuse a change that should be done by moving from the PAN
1792 * context to the BSS context instead, if the BSS context is
1793 * available and can support the new interface type.
1794 */
1795 if (ctx->ctxid == IWL_RXON_CTX_PAN && !bss_ctx->vif &&
1796 (bss_ctx->interface_modes & BIT(newtype) ||
1797 bss_ctx->exclusive_interface_modes & BIT(newtype))) {
1798 BUILD_BUG_ON(NUM_IWL_RXON_CTX != 2);
1799 err = -EBUSY;
1800 goto out;
1801 }
1802
1803 if (ctx->exclusive_interface_modes & BIT(newtype)) {
1804 for_each_context(priv, tmp) {
1805 if (ctx == tmp)
1806 continue;
1807
1808 if (!tmp->vif)
1809 continue;
1810
1811 /*
1812 * The current mode switch would be exclusive, but
1813 * another context is active ... refuse the switch.
1814 */
1815 err = -EBUSY;
1816 goto out;
1817 }
1818 }
1819
1820 /* success */
1821 iwl_teardown_interface(priv, vif, true);
1822 vif->type = newtype;
1823 vif->p2p = newp2p;
1824 err = iwl_setup_interface(priv, ctx);
1825 WARN_ON(err);
1826 /*
1827 * We've switched internally, but submitting to the
1828 * device may have failed for some reason. Mask this
1829 * error, because otherwise mac80211 will not switch
1830 * (and set the interface type back) and we'll be
1831 * out of sync with it.
1832 */
1833 err = 0;
1834
1835 out:
1836 mutex_unlock(&priv->mutex);
1837 return err;
1838 }
1839
1840 /*
1841 * On every watchdog tick we check (latest) time stamp. If it does not
1842 * change during timeout period and queue is not empty we reset firmware.
1843 */
1844 static int iwl_check_stuck_queue(struct iwl_priv *priv, int cnt)
1845 {
1846 struct iwl_tx_queue *txq = &priv->txq[cnt];
1847 struct iwl_queue *q = &txq->q;
1848 unsigned long timeout;
1849 int ret;
1850
1851 if (q->read_ptr == q->write_ptr) {
1852 txq->time_stamp = jiffies;
1853 return 0;
1854 }
1855
1856 timeout = txq->time_stamp +
1857 msecs_to_jiffies(priv->cfg->base_params->wd_timeout);
1858
1859 if (time_after(jiffies, timeout)) {
1860 IWL_ERR(priv, "Queue %d stuck for %u ms.\n",
1861 q->id, priv->cfg->base_params->wd_timeout);
1862 ret = iwl_force_reset(priv, IWL_FW_RESET, false);
1863 return (ret == -EAGAIN) ? 0 : 1;
1864 }
1865
1866 return 0;
1867 }
1868
1869 /*
1870 * Making watchdog tick be a quarter of timeout assure we will
1871 * discover the queue hung between timeout and 1.25*timeout
1872 */
1873 #define IWL_WD_TICK(timeout) ((timeout) / 4)
1874
1875 /*
1876 * Watchdog timer callback, we check each tx queue for stuck, if if hung
1877 * we reset the firmware. If everything is fine just rearm the timer.
1878 */
1879 void iwl_bg_watchdog(unsigned long data)
1880 {
1881 struct iwl_priv *priv = (struct iwl_priv *)data;
1882 int cnt;
1883 unsigned long timeout;
1884
1885 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1886 return;
1887
1888 timeout = priv->cfg->base_params->wd_timeout;
1889 if (timeout == 0)
1890 return;
1891
1892 /* monitor and check for stuck cmd queue */
1893 if (iwl_check_stuck_queue(priv, priv->cmd_queue))
1894 return;
1895
1896 /* monitor and check for other stuck queues */
1897 if (iwl_is_any_associated(priv)) {
1898 for (cnt = 0; cnt < priv->hw_params.max_txq_num; cnt++) {
1899 /* skip as we already checked the command queue */
1900 if (cnt == priv->cmd_queue)
1901 continue;
1902 if (iwl_check_stuck_queue(priv, cnt))
1903 return;
1904 }
1905 }
1906
1907 mod_timer(&priv->watchdog, jiffies +
1908 msecs_to_jiffies(IWL_WD_TICK(timeout)));
1909 }
1910
1911 void iwl_setup_watchdog(struct iwl_priv *priv)
1912 {
1913 unsigned int timeout = priv->cfg->base_params->wd_timeout;
1914
1915 if (timeout)
1916 mod_timer(&priv->watchdog,
1917 jiffies + msecs_to_jiffies(IWL_WD_TICK(timeout)));
1918 else
1919 del_timer(&priv->watchdog);
1920 }
1921
1922 /*
1923 * extended beacon time format
1924 * time in usec will be changed into a 32-bit value in extended:internal format
1925 * the extended part is the beacon counts
1926 * the internal part is the time in usec within one beacon interval
1927 */
1928 u32 iwl_usecs_to_beacons(struct iwl_priv *priv, u32 usec, u32 beacon_interval)
1929 {
1930 u32 quot;
1931 u32 rem;
1932 u32 interval = beacon_interval * TIME_UNIT;
1933
1934 if (!interval || !usec)
1935 return 0;
1936
1937 quot = (usec / interval) &
1938 (iwl_beacon_time_mask_high(priv,
1939 priv->hw_params.beacon_time_tsf_bits) >>
1940 priv->hw_params.beacon_time_tsf_bits);
1941 rem = (usec % interval) & iwl_beacon_time_mask_low(priv,
1942 priv->hw_params.beacon_time_tsf_bits);
1943
1944 return (quot << priv->hw_params.beacon_time_tsf_bits) + rem;
1945 }
1946
1947 /* base is usually what we get from ucode with each received frame,
1948 * the same as HW timer counter counting down
1949 */
1950 __le32 iwl_add_beacon_time(struct iwl_priv *priv, u32 base,
1951 u32 addon, u32 beacon_interval)
1952 {
1953 u32 base_low = base & iwl_beacon_time_mask_low(priv,
1954 priv->hw_params.beacon_time_tsf_bits);
1955 u32 addon_low = addon & iwl_beacon_time_mask_low(priv,
1956 priv->hw_params.beacon_time_tsf_bits);
1957 u32 interval = beacon_interval * TIME_UNIT;
1958 u32 res = (base & iwl_beacon_time_mask_high(priv,
1959 priv->hw_params.beacon_time_tsf_bits)) +
1960 (addon & iwl_beacon_time_mask_high(priv,
1961 priv->hw_params.beacon_time_tsf_bits));
1962
1963 if (base_low > addon_low)
1964 res += base_low - addon_low;
1965 else if (base_low < addon_low) {
1966 res += interval + base_low - addon_low;
1967 res += (1 << priv->hw_params.beacon_time_tsf_bits);
1968 } else
1969 res += (1 << priv->hw_params.beacon_time_tsf_bits);
1970
1971 return cpu_to_le32(res);
1972 }
1973
1974 #ifdef CONFIG_PM
1975
1976 int iwl_pci_suspend(struct device *device)
1977 {
1978 struct pci_dev *pdev = to_pci_dev(device);
1979 struct iwl_priv *priv = pci_get_drvdata(pdev);
1980
1981 /*
1982 * This function is called when system goes into suspend state
1983 * mac80211 will call iwl_mac_stop() from the mac80211 suspend function
1984 * first but since iwl_mac_stop() has no knowledge of who the caller is,
1985 * it will not call apm_ops.stop() to stop the DMA operation.
1986 * Calling apm_ops.stop here to make sure we stop the DMA.
1987 */
1988 iwl_apm_stop(priv);
1989
1990 return 0;
1991 }
1992
1993 int iwl_pci_resume(struct device *device)
1994 {
1995 struct pci_dev *pdev = to_pci_dev(device);
1996 struct iwl_priv *priv = pci_get_drvdata(pdev);
1997 bool hw_rfkill = false;
1998
1999 /*
2000 * We disable the RETRY_TIMEOUT register (0x41) to keep
2001 * PCI Tx retries from interfering with C3 CPU state.
2002 */
2003 pci_write_config_byte(pdev, PCI_CFG_RETRY_TIMEOUT, 0x00);
2004
2005 iwl_enable_interrupts(priv);
2006
2007 if (!(iwl_read32(priv, CSR_GP_CNTRL) &
2008 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
2009 hw_rfkill = true;
2010
2011 if (hw_rfkill)
2012 set_bit(STATUS_RF_KILL_HW, &priv->status);
2013 else
2014 clear_bit(STATUS_RF_KILL_HW, &priv->status);
2015
2016 wiphy_rfkill_set_hw_state(priv->hw->wiphy, hw_rfkill);
2017
2018 return 0;
2019 }
2020
2021 const struct dev_pm_ops iwl_pm_ops = {
2022 .suspend = iwl_pci_suspend,
2023 .resume = iwl_pci_resume,
2024 .freeze = iwl_pci_suspend,
2025 .thaw = iwl_pci_resume,
2026 .poweroff = iwl_pci_suspend,
2027 .restore = iwl_pci_resume,
2028 };
2029
2030 #endif /* CONFIG_PM */
This page took 0.109475 seconds and 5 git commands to generate.