iwl-core.c 32.3 KB
Newer Older
T
Tomas Winkler 已提交
1 2 3 4
/******************************************************************************
 *
 * GPL LICENSE SUMMARY
 *
W
Wey-Yi Guy 已提交
5
 * Copyright(c) 2008 - 2012 Intel Corporation. All rights reserved.
T
Tomas Winkler 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
 * USA
 *
 * The full GNU General Public License is included in this distribution
 * in the file called LICENSE.GPL.
 *
 * Contact Information:
25
 *  Intel Linux Wireless <ilw@linux.intel.com>
T
Tomas Winkler 已提交
26 27 28 29 30
 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
 *****************************************************************************/

#include <linux/kernel.h>
#include <linux/module.h>
31
#include <linux/etherdevice.h>
32
#include <linux/sched.h>
33
#include <linux/slab.h>
34
#include <net/mac80211.h>
T
Tomas Winkler 已提交
35

A
Assaf Krauss 已提交
36
#include "iwl-eeprom.h"
W
Winkler, Tomas 已提交
37
#include "iwl-debug.h"
T
Tomas Winkler 已提交
38
#include "iwl-core.h"
39
#include "iwl-io.h"
M
Mohamed Abbas 已提交
40
#include "iwl-power.h"
41
#include "iwl-shared.h"
D
Don Fry 已提交
42
#include "iwl-agn.h"
43
#include "iwl-trans.h"
T
Tomas Winkler 已提交
44

45 46
const u8 iwl_bcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };

47 48 49
static bool iwl_is_channel_extension(struct iwl_priv *priv,
				     enum ieee80211_band band,
				     u16 channel, u8 extension_chan_offset)
50 51 52 53 54
{
	const struct iwl_channel_info *ch_info;

	ch_info = iwl_get_channel_info(priv, band, channel);
	if (!is_channel_valid(ch_info))
55
		return false;
56

J
Johannes Berg 已提交
57
	if (extension_chan_offset == IEEE80211_HT_PARAM_CHA_SEC_ABOVE)
58
		return !(ch_info->ht40_extension_channel &
59
					IEEE80211_CHAN_NO_HT40PLUS);
J
Johannes Berg 已提交
60
	else if (extension_chan_offset == IEEE80211_HT_PARAM_CHA_SEC_BELOW)
61
		return !(ch_info->ht40_extension_channel &
62
					IEEE80211_CHAN_NO_HT40MINUS);
63

64
	return false;
65 66
}

67 68 69
bool iwl_is_ht40_tx_allowed(struct iwl_priv *priv,
			    struct iwl_rxon_context *ctx,
			    struct ieee80211_sta_ht_cap *ht_cap)
70
{
71 72
	if (!ctx->ht.enabled || !ctx->ht.is_40mhz)
		return false;
73

74 75
	/*
	 * We do not check for IEEE80211_HT_CAP_SUP_WIDTH_20_40
76 77
	 * the bit will not set if it is pure 40MHz case
	 */
78 79 80
	if (ht_cap && !ht_cap->ht_supported)
		return false;

81
#ifdef CONFIG_IWLWIFI_DEBUGFS
82
	if (priv->disable_ht40)
83
		return false;
84
#endif
85

86
	return iwl_is_channel_extension(priv, priv->band,
87
			le16_to_cpu(ctx->staging.channel),
88
			ctx->ht.extension_chan_offset);
89 90
}

91 92
void iwl_set_rxon_hwcrypto(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
			   int hw_decrypt)
93
{
94
	struct iwl_rxon_cmd *rxon = &ctx->staging;
95 96 97 98 99 100 101 102

	if (hw_decrypt)
		rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
	else
		rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;

}

J
Johannes Berg 已提交
103
/* validate RXON structure is valid */
104
int iwl_check_rxon_cmd(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
105
{
106
	struct iwl_rxon_cmd *rxon = &ctx->staging;
J
Johannes Berg 已提交
107
	u32 errors = 0;
108 109

	if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
J
Johannes Berg 已提交
110 111
		if (rxon->flags & RXON_FLG_TGJ_NARROW_BAND_MSK) {
			IWL_WARN(priv, "check 2.4G: wrong narrow\n");
J
Johannes Berg 已提交
112
			errors |= BIT(0);
J
Johannes Berg 已提交
113 114 115
		}
		if (rxon->flags & RXON_FLG_RADAR_DETECT_MSK) {
			IWL_WARN(priv, "check 2.4G: wrong radar\n");
J
Johannes Berg 已提交
116
			errors |= BIT(1);
J
Johannes Berg 已提交
117
		}
118
	} else {
J
Johannes Berg 已提交
119 120
		if (!(rxon->flags & RXON_FLG_SHORT_SLOT_MSK)) {
			IWL_WARN(priv, "check 5.2G: not short slot!\n");
J
Johannes Berg 已提交
121
			errors |= BIT(2);
J
Johannes Berg 已提交
122 123 124
		}
		if (rxon->flags & RXON_FLG_CCK_MSK) {
			IWL_WARN(priv, "check 5.2G: CCK!\n");
J
Johannes Berg 已提交
125
			errors |= BIT(3);
J
Johannes Berg 已提交
126 127 128 129
		}
	}
	if ((rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1) {
		IWL_WARN(priv, "mac/bssid mcast!\n");
J
Johannes Berg 已提交
130
		errors |= BIT(4);
131 132 133
	}

	/* make sure basic rates 6Mbps and 1Mbps are supported */
J
Johannes Berg 已提交
134 135 136
	if ((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0 &&
	    (rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0) {
		IWL_WARN(priv, "neither 1 nor 6 are basic\n");
J
Johannes Berg 已提交
137
		errors |= BIT(5);
J
Johannes Berg 已提交
138
	}
139

J
Johannes Berg 已提交
140 141
	if (le16_to_cpu(rxon->assoc_id) > 2007) {
		IWL_WARN(priv, "aid > 2007\n");
J
Johannes Berg 已提交
142
		errors |= BIT(6);
J
Johannes Berg 已提交
143
	}
144

J
Johannes Berg 已提交
145 146 147
	if ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
			== (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK)) {
		IWL_WARN(priv, "CCK and short slot\n");
J
Johannes Berg 已提交
148
		errors |= BIT(7);
J
Johannes Berg 已提交
149
	}
150

J
Johannes Berg 已提交
151 152 153
	if ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
			== (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK)) {
		IWL_WARN(priv, "CCK and auto detect");
J
Johannes Berg 已提交
154
		errors |= BIT(8);
J
Johannes Berg 已提交
155
	}
156

J
Johannes Berg 已提交
157 158 159 160
	if ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
			    RXON_FLG_TGG_PROTECT_MSK)) ==
			    RXON_FLG_TGG_PROTECT_MSK) {
		IWL_WARN(priv, "TGg but no auto-detect\n");
J
Johannes Berg 已提交
161
		errors |= BIT(9);
J
Johannes Berg 已提交
162
	}
163

J
Johannes Berg 已提交
164 165 166
	if (rxon->channel == 0) {
		IWL_WARN(priv, "zero channel is invalid\n");
		errors |= BIT(10);
167
	}
J
Johannes Berg 已提交
168 169 170 171 172

	WARN(errors, "Invalid RXON (%#x), channel %d",
	     errors, le16_to_cpu(rxon->channel));

	return errors ? -EINVAL : 0;
173 174 175 176 177 178 179 180 181 182
}

/**
 * iwl_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
 * @priv: staging_rxon is compared to active_rxon
 *
 * If the RXON structure is changing enough to require a new tune,
 * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
 * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
 */
183 184
int iwl_full_rxon_required(struct iwl_priv *priv,
			   struct iwl_rxon_context *ctx)
185
{
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
	const struct iwl_rxon_cmd *staging = &ctx->staging;
	const struct iwl_rxon_cmd *active = &ctx->active;

#define CHK(cond)							\
	if ((cond)) {							\
		IWL_DEBUG_INFO(priv, "need full RXON - " #cond "\n");	\
		return 1;						\
	}

#define CHK_NEQ(c1, c2)						\
	if ((c1) != (c2)) {					\
		IWL_DEBUG_INFO(priv, "need full RXON - "	\
			       #c1 " != " #c2 " - %d != %d\n",	\
			       (c1), (c2));			\
		return 1;					\
	}
202 203

	/* These items are only settable from the full RXON command */
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
	CHK(!iwl_is_associated_ctx(ctx));
	CHK(compare_ether_addr(staging->bssid_addr, active->bssid_addr));
	CHK(compare_ether_addr(staging->node_addr, active->node_addr));
	CHK(compare_ether_addr(staging->wlap_bssid_addr,
				active->wlap_bssid_addr));
	CHK_NEQ(staging->dev_type, active->dev_type);
	CHK_NEQ(staging->channel, active->channel);
	CHK_NEQ(staging->air_propagation, active->air_propagation);
	CHK_NEQ(staging->ofdm_ht_single_stream_basic_rates,
		active->ofdm_ht_single_stream_basic_rates);
	CHK_NEQ(staging->ofdm_ht_dual_stream_basic_rates,
		active->ofdm_ht_dual_stream_basic_rates);
	CHK_NEQ(staging->ofdm_ht_triple_stream_basic_rates,
		active->ofdm_ht_triple_stream_basic_rates);
	CHK_NEQ(staging->assoc_id, active->assoc_id);
219 220 221 222 223 224

	/* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
	 * be updated with the RXON_ASSOC command -- however only some
	 * flag transitions are allowed using RXON_ASSOC */

	/* Check if we are not switching bands */
225 226
	CHK_NEQ(staging->flags & RXON_FLG_BAND_24G_MSK,
		active->flags & RXON_FLG_BAND_24G_MSK);
227 228

	/* Check if we are switching association toggle */
229 230 231 232 233
	CHK_NEQ(staging->filter_flags & RXON_FILTER_ASSOC_MSK,
		active->filter_flags & RXON_FILTER_ASSOC_MSK);

#undef CHK
#undef CHK_NEQ
234 235 236 237

	return 0;
}

238 239 240
static void _iwl_set_rxon_ht(struct iwl_priv *priv,
			     struct iwl_ht_config *ht_conf,
			     struct iwl_rxon_context *ctx)
241
{
242
	struct iwl_rxon_cmd *rxon = &ctx->staging;
243

244
	if (!ctx->ht.enabled) {
245
		rxon->flags &= ~(RXON_FLG_CHANNEL_MODE_MSK |
246
			RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK |
247
			RXON_FLG_HT40_PROT_MSK |
248
			RXON_FLG_HT_PROT_MSK);
249
		return;
250
	}
251

252
	/* FIXME: if the definition of ht.protection changed, the "translation"
253 254
	 * will be needed for rxon->flags
	 */
255
	rxon->flags |= cpu_to_le32(ctx->ht.protection << RXON_FLG_HT_OPERATING_MODE_POS);
256 257

	/* Set up channel bandwidth:
258
	 * 20 MHz only, 20/40 mixed or pure 40 if ht40 ok */
259 260 261
	/* clear the HT channel mode before set the mode */
	rxon->flags &= ~(RXON_FLG_CHANNEL_MODE_MSK |
			 RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK);
262
	if (iwl_is_ht40_tx_allowed(priv, ctx, NULL)) {
263
		/* pure ht40 */
264
		if (ctx->ht.protection == IEEE80211_HT_OP_MODE_PROTECTION_20MHZ) {
265
			rxon->flags |= RXON_FLG_CHANNEL_MODE_PURE_40;
266
			/* Note: control channel is opposite of extension channel */
267
			switch (ctx->ht.extension_chan_offset) {
268 269 270 271 272 273 274 275
			case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
				rxon->flags &= ~RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
				break;
			case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
				rxon->flags |= RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
				break;
			}
		} else {
276
			/* Note: control channel is opposite of extension channel */
277
			switch (ctx->ht.extension_chan_offset) {
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
			case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
				rxon->flags &= ~(RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK);
				rxon->flags |= RXON_FLG_CHANNEL_MODE_MIXED;
				break;
			case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
				rxon->flags |= RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
				rxon->flags |= RXON_FLG_CHANNEL_MODE_MIXED;
				break;
			case IEEE80211_HT_PARAM_CHA_SEC_NONE:
			default:
				/* channel location only valid if in Mixed mode */
				IWL_ERR(priv, "invalid extension channel offset\n");
				break;
			}
		}
	} else {
		rxon->flags |= RXON_FLG_CHANNEL_MODE_LEGACY;
295 296
	}

W
Wey-Yi Guy 已提交
297
	iwlagn_set_rxon_chain(priv, ctx);
298

299
	IWL_DEBUG_ASSOC(priv, "rxon flags 0x%X operation mode :0x%X "
J
Johannes Berg 已提交
300
			"extension channel offset 0x%x\n",
301 302
			le32_to_cpu(rxon->flags), ctx->ht.protection,
			ctx->ht.extension_chan_offset);
303
}
304 305 306 307 308 309 310 311

void iwl_set_rxon_ht(struct iwl_priv *priv, struct iwl_ht_config *ht_conf)
{
	struct iwl_rxon_context *ctx;

	for_each_context(priv, ctx)
		_iwl_set_rxon_ht(priv, ht_conf, ctx);
}
312

313
/* Return valid, unused, channel for a passive scan to reset the RF */
A
Abhijeet Kolekar 已提交
314
u8 iwl_get_single_channel_number(struct iwl_priv *priv,
315
				 enum ieee80211_band band)
A
Abhijeet Kolekar 已提交
316 317 318 319
{
	const struct iwl_channel_info *ch_info;
	int i;
	u8 channel = 0;
320 321
	u8 min, max;
	struct iwl_rxon_context *ctx;
A
Abhijeet Kolekar 已提交
322 323

	if (band == IEEE80211_BAND_5GHZ) {
324 325
		min = 14;
		max = priv->channel_count;
A
Abhijeet Kolekar 已提交
326
	} else {
327 328 329 330 331 332 333 334 335 336 337 338
		min = 0;
		max = 14;
	}

	for (i = min; i < max; i++) {
		bool busy = false;

		for_each_context(priv, ctx) {
			busy = priv->channel_info[i].channel ==
				le16_to_cpu(ctx->staging.channel);
			if (busy)
				break;
A
Abhijeet Kolekar 已提交
339
		}
340 341 342 343 344 345 346 347

		if (busy)
			continue;

		channel = priv->channel_info[i].channel;
		ch_info = iwl_get_channel_info(priv, band, channel);
		if (is_channel_valid(ch_info))
			break;
A
Abhijeet Kolekar 已提交
348 349 350 351 352
	}

	return channel;
}

353
/**
354 355
 * iwl_set_rxon_channel - Set the band and channel values in staging RXON
 * @ch: requested channel as a pointer to struct ieee80211_channel
356 357

 * NOTE:  Does not commit to the hardware; it sets appropriate bit fields
358
 * in the staging RXON flag structure based on the ch->band
359
 */
W
Wey-Yi Guy 已提交
360
void iwl_set_rxon_channel(struct iwl_priv *priv, struct ieee80211_channel *ch,
361
			 struct iwl_rxon_context *ctx)
362
{
363
	enum ieee80211_band band = ch->band;
364
	u16 channel = ch->hw_value;
365

366
	if ((le16_to_cpu(ctx->staging.channel) == channel) &&
367
	    (priv->band == band))
W
Wey-Yi Guy 已提交
368
		return;
369

370
	ctx->staging.channel = cpu_to_le16(channel);
371
	if (band == IEEE80211_BAND_5GHZ)
372
		ctx->staging.flags &= ~RXON_FLG_BAND_24G_MSK;
373
	else
374
		ctx->staging.flags |= RXON_FLG_BAND_24G_MSK;
375 376 377

	priv->band = band;

378
	IWL_DEBUG_INFO(priv, "Staging channel set to %d [%d]\n", channel, band);
379 380 381

}

382
void iwl_set_flags_for_band(struct iwl_priv *priv,
383
			    struct iwl_rxon_context *ctx,
384 385
			    enum ieee80211_band band,
			    struct ieee80211_vif *vif)
386 387
{
	if (band == IEEE80211_BAND_5GHZ) {
388
		ctx->staging.flags &=
389 390
		    ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
		      | RXON_FLG_CCK_MSK);
391
		ctx->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
392 393
	} else {
		/* Copied from iwl_post_associate() */
394
		if (vif && vif->bss_conf.use_short_slot)
395
			ctx->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
396
		else
397
			ctx->staging.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
398

399 400 401
		ctx->staging.flags |= RXON_FLG_BAND_24G_MSK;
		ctx->staging.flags |= RXON_FLG_AUTO_DETECT_MSK;
		ctx->staging.flags &= ~RXON_FLG_CCK_MSK;
402 403 404 405 406 407
	}
}

/*
 * initialize rxon structure with default values from eeprom
 */
408
void iwl_connection_init_rx_config(struct iwl_priv *priv,
409
				   struct iwl_rxon_context *ctx)
410 411 412
{
	const struct iwl_channel_info *ch_info;

413
	memset(&ctx->staging, 0, sizeof(ctx->staging));
414

415 416 417
	if (!ctx->vif) {
		ctx->staging.dev_type = ctx->unused_devtype;
	} else switch (ctx->vif->type) {
418
	case NL80211_IFTYPE_AP:
419
		ctx->staging.dev_type = ctx->ap_devtype;
420 421 422
		break;

	case NL80211_IFTYPE_STATION:
423
		ctx->staging.dev_type = ctx->station_devtype;
424
		ctx->staging.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
425 426 427
		break;

	case NL80211_IFTYPE_ADHOC:
428
		ctx->staging.dev_type = ctx->ibss_devtype;
429 430
		ctx->staging.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
		ctx->staging.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
431 432 433 434
						  RXON_FILTER_ACCEPT_GRP_MSK;
		break;

	default:
435 436
		IWL_ERR(priv, "Unsupported interface type %d\n",
			ctx->vif->type);
437 438 439 440 441 442 443
		break;
	}

#if 0
	/* TODO:  Figure out when short_preamble would be set and cache from
	 * that */
	if (!hw_to_local(priv->hw)->short_preamble)
444
		ctx->staging.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
445
	else
446
		ctx->staging.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
447 448 449
#endif

	ch_info = iwl_get_channel_info(priv, priv->band,
450
				       le16_to_cpu(ctx->active.channel));
451 452 453 454

	if (!ch_info)
		ch_info = &priv->channel_info[0];

455
	ctx->staging.channel = cpu_to_le16(ch_info->channel);
456 457
	priv->band = ch_info->band;

458
	iwl_set_flags_for_band(priv, ctx, priv->band, ctx->vif);
459

460
	ctx->staging.ofdm_basic_rates =
461
	    (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
462
	ctx->staging.cck_basic_rates =
463 464
	    (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;

465
	/* clear both MIX and PURE40 mode flag */
466
	ctx->staging.flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED |
467
					RXON_FLG_CHANNEL_MODE_PURE_40);
468 469
	if (ctx->vif)
		memcpy(ctx->staging.node_addr, ctx->vif->addr, ETH_ALEN);
470

471 472 473
	ctx->staging.ofdm_ht_single_stream_basic_rates = 0xff;
	ctx->staging.ofdm_ht_dual_stream_basic_rates = 0xff;
	ctx->staging.ofdm_ht_triple_stream_basic_rates = 0xff;
474 475
}

476
void iwl_set_rate(struct iwl_priv *priv)
477 478 479
{
	const struct ieee80211_supported_band *hw = NULL;
	struct ieee80211_rate *rate;
480
	struct iwl_rxon_context *ctx;
481 482 483 484 485 486 487 488 489 490 491 492
	int i;

	hw = iwl_get_hw_mode(priv, priv->band);
	if (!hw) {
		IWL_ERR(priv, "Failed to set rate: unable to get hw mode\n");
		return;
	}

	priv->active_rate = 0;

	for (i = 0; i < hw->n_bitrates; i++) {
		rate = &(hw->bitrates[i]);
493
		if (rate->hw_value < IWL_RATE_COUNT_LEGACY)
494 495 496
			priv->active_rate |= (1 << rate->hw_value);
	}

497
	IWL_DEBUG_RATE(priv, "Set active_rate = %0x\n", priv->active_rate);
498

499 500 501
	for_each_context(priv, ctx) {
		ctx->staging.cck_basic_rates =
		    (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
502

503 504 505
		ctx->staging.ofdm_basic_rates =
		   (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
	}
506
}
507 508 509

void iwl_chswitch_done(struct iwl_priv *priv, bool is_success)
{
510 511
	/*
	 * MULTI-FIXME
512
	 * See iwlagn_mac_channel_switch.
513 514 515
	 */
	struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];

D
Don Fry 已提交
516
	if (test_bit(STATUS_EXIT_PENDING, &priv->status))
517 518
		return;

D
Don Fry 已提交
519
	if (test_and_clear_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->status))
520
		ieee80211_chswitch_done(ctx->vif, is_success);
521
}
522 523

#ifdef CONFIG_IWLWIFI_DEBUG
524 525
void iwl_print_rx_config_cmd(struct iwl_priv *priv,
			     enum iwl_rxon_context_id ctxid)
526
{
527
	struct iwl_rxon_context *ctx = &priv->contexts[ctxid];
528
	struct iwl_rxon_cmd *rxon = &ctx->staging;
529

530
	IWL_DEBUG_RADIO(priv, "RX CONFIG:\n");
531
	iwl_print_hex_dump(priv, IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
532 533 534
	IWL_DEBUG_RADIO(priv, "u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
	IWL_DEBUG_RADIO(priv, "u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
	IWL_DEBUG_RADIO(priv, "u32 filter_flags: 0x%08x\n",
535
			le32_to_cpu(rxon->filter_flags));
536 537
	IWL_DEBUG_RADIO(priv, "u8 dev_type: 0x%x\n", rxon->dev_type);
	IWL_DEBUG_RADIO(priv, "u8 ofdm_basic_rates: 0x%02x\n",
538
			rxon->ofdm_basic_rates);
539 540 541 542
	IWL_DEBUG_RADIO(priv, "u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
	IWL_DEBUG_RADIO(priv, "u8[6] node_addr: %pM\n", rxon->node_addr);
	IWL_DEBUG_RADIO(priv, "u8[6] bssid_addr: %pM\n", rxon->bssid_addr);
	IWL_DEBUG_RADIO(priv, "u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
543
}
544
#endif
545

546
void iwlagn_fw_error(struct iwl_priv *priv, bool ondemand)
547
{
548 549 550
	unsigned int reload_msec;
	unsigned long reload_jiffies;

551
#ifdef CONFIG_IWLWIFI_DEBUG
552
	if (iwl_have_debug_level(IWL_DL_FW_ERRORS))
553 554 555
		iwl_print_rx_config_cmd(priv, IWL_RXON_CTX_BSS);
#endif

556 557 558
	/* uCode is no longer loaded. */
	priv->ucode_loaded = false;

559
	/* Set the FW error flag -- cleared on iwl_down */
560
	set_bit(STATUS_FW_ERROR, &priv->shrd->status);
561 562

	/* Cancel currently queued command. */
563
	clear_bit(STATUS_HCMD_ACTIVE, &priv->shrd->status);
564

565
	iwl_abort_notification_waits(&priv->notif_wait);
566

567 568
	/* Keep the restart process from trying to send host
	 * commands by clearing the ready bit */
D
Don Fry 已提交
569
	clear_bit(STATUS_READY, &priv->status);
570

571
	wake_up(&trans(priv)->wait_command_queue);
572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593

	if (!ondemand) {
		/*
		 * If firmware keep reloading, then it indicate something
		 * serious wrong and firmware having problem to recover
		 * from it. Instead of keep trying which will fill the syslog
		 * and hang the system, let's just stop it
		 */
		reload_jiffies = jiffies;
		reload_msec = jiffies_to_msecs((long) reload_jiffies -
					(long) priv->reload_jiffies);
		priv->reload_jiffies = reload_jiffies;
		if (reload_msec <= IWL_MIN_RELOAD_DURATION) {
			priv->reload_count++;
			if (priv->reload_count >= IWL_MAX_CONTINUE_RELOAD_CNT) {
				IWL_ERR(priv, "BUG_ON, Stop restarting\n");
				return;
			}
		} else
			priv->reload_count = 0;
	}

D
Don Fry 已提交
594
	if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
D
Don Fry 已提交
595
		if (iwlagn_mod_params.restart_fw) {
596
			IWL_DEBUG_FW_ERRORS(priv,
597
				  "Restarting adapter due to uCode error.\n");
J
Johannes Berg 已提交
598
			queue_work(priv->workqueue, &priv->restart);
599
		} else
600
			IWL_DEBUG_FW_ERRORS(priv,
601 602 603 604
				  "Detected FW error, but not restarting\n");
	}
}

605 606
int iwl_set_tx_power(struct iwl_priv *priv, s8 tx_power, bool force)
{
607 608
	int ret;
	s8 prev_tx_power;
609 610
	bool defer;
	struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
611

612
	lockdep_assert_held(&priv->mutex);
613 614 615 616

	if (priv->tx_power_user_lmt == tx_power && !force)
		return 0;

617 618 619
	if (tx_power < IWLAGN_TX_POWER_TARGET_POWER_MIN) {
		IWL_WARN(priv,
			 "Requested user TXPOWER %d below lower limit %d.\n",
620
			 tx_power,
621
			 IWLAGN_TX_POWER_TARGET_POWER_MIN);
622 623 624
		return -EINVAL;
	}

625
	if (tx_power > priv->tx_power_device_lmt) {
626 627
		IWL_WARN(priv,
			"Requested user TXPOWER %d above upper limit %d.\n",
628
			 tx_power, priv->tx_power_device_lmt);
629 630 631
		return -EINVAL;
	}

D
Don Fry 已提交
632
	if (!iwl_is_ready_rf(priv))
633
		return -EIO;
634

635 636
	/* scan complete and commit_rxon use tx_power_next value,
	 * it always need to be updated for newest request */
637
	priv->tx_power_next = tx_power;
638 639

	/* do not set tx power when scanning or channel changing */
D
Don Fry 已提交
640
	defer = test_bit(STATUS_SCANNING, &priv->status) ||
641 642 643
		memcmp(&ctx->active, &ctx->staging, sizeof(ctx->staging));
	if (defer && !force) {
		IWL_DEBUG_INFO(priv, "Deferring tx power set\n");
644
		return 0;
645
	}
646

647 648 649
	prev_tx_power = priv->tx_power_user_lmt;
	priv->tx_power_user_lmt = tx_power;

650
	ret = iwlagn_send_tx_power(priv);
651 652 653 654 655 656

	/* if fail to set tx_power, restore the orig. tx power */
	if (ret) {
		priv->tx_power_user_lmt = prev_tx_power;
		priv->tx_power_next = prev_tx_power;
	}
657 658 659
	return ret;
}

660
void iwl_send_bt_config(struct iwl_priv *priv)
661 662
{
	struct iwl_bt_cmd bt_cmd = {
663 664
		.lead_time = BT_LEAD_TIME_DEF,
		.max_kill = BT_MAX_KILL_DEF,
665 666 667 668
		.kill_ack_mask = 0,
		.kill_cts_mask = 0,
	};

669
	if (!iwlagn_mod_params.bt_coex_active)
670 671 672 673
		bt_cmd.flags = BT_COEX_DISABLE;
	else
		bt_cmd.flags = BT_COEX_ENABLE;

674
	priv->bt_enable_flag = bt_cmd.flags;
675 676 677
	IWL_DEBUG_INFO(priv, "BT coex %s\n",
		(bt_cmd.flags == BT_COEX_DISABLE) ? "disable" : "active");

678
	if (iwl_dvm_send_cmd_pdu(priv, REPLY_BT_CONFIG,
679
			     CMD_SYNC, sizeof(struct iwl_bt_cmd), &bt_cmd))
680
		IWL_ERR(priv, "failed to send BT Coex Config\n");
681 682
}

683
int iwl_send_statistics_request(struct iwl_priv *priv, u8 flags, bool clear)
684
{
685 686 687
	struct iwl_statistics_cmd statistics_cmd = {
		.configuration_flags =
			clear ? IWL_STATS_CONF_CLEAR_STATS : 0,
688
	};
689 690

	if (flags & CMD_ASYNC)
691
		return iwl_dvm_send_cmd_pdu(priv, REPLY_STATISTICS_CMD,
692
					      CMD_ASYNC,
693
					       sizeof(struct iwl_statistics_cmd),
694
					       &statistics_cmd);
695
	else
696
		return iwl_dvm_send_cmd_pdu(priv, REPLY_STATISTICS_CMD,
697
					CMD_SYNC,
698 699
					sizeof(struct iwl_statistics_cmd),
					&statistics_cmd);
700
}
701

A
Abhijeet Kolekar 已提交
702

703

704

705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
#ifdef CONFIG_IWLWIFI_DEBUGFS

#define IWL_TRAFFIC_DUMP_SIZE	(IWL_TRAFFIC_ENTRY_SIZE * IWL_TRAFFIC_ENTRIES)

void iwl_reset_traffic_log(struct iwl_priv *priv)
{
	priv->tx_traffic_idx = 0;
	priv->rx_traffic_idx = 0;
	if (priv->tx_traffic)
		memset(priv->tx_traffic, 0, IWL_TRAFFIC_DUMP_SIZE);
	if (priv->rx_traffic)
		memset(priv->rx_traffic, 0, IWL_TRAFFIC_DUMP_SIZE);
}

int iwl_alloc_traffic_mem(struct iwl_priv *priv)
{
	u32 traffic_size = IWL_TRAFFIC_DUMP_SIZE;

723
	if (iwl_have_debug_level(IWL_DL_TX)) {
724 725 726 727 728 729 730
		if (!priv->tx_traffic) {
			priv->tx_traffic =
				kzalloc(traffic_size, GFP_KERNEL);
			if (!priv->tx_traffic)
				return -ENOMEM;
		}
	}
731
	if (iwl_have_debug_level(IWL_DL_RX)) {
732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757
		if (!priv->rx_traffic) {
			priv->rx_traffic =
				kzalloc(traffic_size, GFP_KERNEL);
			if (!priv->rx_traffic)
				return -ENOMEM;
		}
	}
	iwl_reset_traffic_log(priv);
	return 0;
}

void iwl_free_traffic_mem(struct iwl_priv *priv)
{
	kfree(priv->tx_traffic);
	priv->tx_traffic = NULL;

	kfree(priv->rx_traffic);
	priv->rx_traffic = NULL;
}

void iwl_dbg_log_tx_data_frame(struct iwl_priv *priv,
		      u16 length, struct ieee80211_hdr *header)
{
	__le16 fc;
	u16 len;

758
	if (likely(!iwl_have_debug_level(IWL_DL_TX)))
759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781
		return;

	if (!priv->tx_traffic)
		return;

	fc = header->frame_control;
	if (ieee80211_is_data(fc)) {
		len = (length > IWL_TRAFFIC_ENTRY_SIZE)
		       ? IWL_TRAFFIC_ENTRY_SIZE : length;
		memcpy((priv->tx_traffic +
		       (priv->tx_traffic_idx * IWL_TRAFFIC_ENTRY_SIZE)),
		       header, len);
		priv->tx_traffic_idx =
			(priv->tx_traffic_idx + 1) % IWL_TRAFFIC_ENTRIES;
	}
}

void iwl_dbg_log_rx_data_frame(struct iwl_priv *priv,
		      u16 length, struct ieee80211_hdr *header)
{
	__le16 fc;
	u16 len;

782
	if (likely(!iwl_have_debug_level(IWL_DL_RX)))
783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798
		return;

	if (!priv->rx_traffic)
		return;

	fc = header->frame_control;
	if (ieee80211_is_data(fc)) {
		len = (length > IWL_TRAFFIC_ENTRY_SIZE)
		       ? IWL_TRAFFIC_ENTRY_SIZE : length;
		memcpy((priv->rx_traffic +
		       (priv->rx_traffic_idx * IWL_TRAFFIC_ENTRY_SIZE)),
		       header, len);
		priv->rx_traffic_idx =
			(priv->rx_traffic_idx + 1) % IWL_TRAFFIC_ENTRIES;
	}
}
799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837

const char *get_mgmt_string(int cmd)
{
	switch (cmd) {
		IWL_CMD(MANAGEMENT_ASSOC_REQ);
		IWL_CMD(MANAGEMENT_ASSOC_RESP);
		IWL_CMD(MANAGEMENT_REASSOC_REQ);
		IWL_CMD(MANAGEMENT_REASSOC_RESP);
		IWL_CMD(MANAGEMENT_PROBE_REQ);
		IWL_CMD(MANAGEMENT_PROBE_RESP);
		IWL_CMD(MANAGEMENT_BEACON);
		IWL_CMD(MANAGEMENT_ATIM);
		IWL_CMD(MANAGEMENT_DISASSOC);
		IWL_CMD(MANAGEMENT_AUTH);
		IWL_CMD(MANAGEMENT_DEAUTH);
		IWL_CMD(MANAGEMENT_ACTION);
	default:
		return "UNKNOWN";

	}
}

const char *get_ctrl_string(int cmd)
{
	switch (cmd) {
		IWL_CMD(CONTROL_BACK_REQ);
		IWL_CMD(CONTROL_BACK);
		IWL_CMD(CONTROL_PSPOLL);
		IWL_CMD(CONTROL_RTS);
		IWL_CMD(CONTROL_CTS);
		IWL_CMD(CONTROL_ACK);
		IWL_CMD(CONTROL_CFEND);
		IWL_CMD(CONTROL_CFENDACK);
	default:
		return "UNKNOWN";

	}
}

838
void iwl_clear_traffic_stats(struct iwl_priv *priv)
839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934
{
	memset(&priv->tx_stats, 0, sizeof(struct traffic_stats));
	memset(&priv->rx_stats, 0, sizeof(struct traffic_stats));
}

/*
 * if CONFIG_IWLWIFI_DEBUGFS defined, iwl_update_stats function will
 * record all the MGMT, CTRL and DATA pkt for both TX and Rx pass.
 * Use debugFs to display the rx/rx_statistics
 * if CONFIG_IWLWIFI_DEBUGFS not being defined, then no MGMT and CTRL
 * information will be recorded, but DATA pkt still will be recorded
 * for the reason of iwl_led.c need to control the led blinking based on
 * number of tx and rx data.
 *
 */
void iwl_update_stats(struct iwl_priv *priv, bool is_tx, __le16 fc, u16 len)
{
	struct traffic_stats	*stats;

	if (is_tx)
		stats = &priv->tx_stats;
	else
		stats = &priv->rx_stats;

	if (ieee80211_is_mgmt(fc)) {
		switch (fc & cpu_to_le16(IEEE80211_FCTL_STYPE)) {
		case cpu_to_le16(IEEE80211_STYPE_ASSOC_REQ):
			stats->mgmt[MANAGEMENT_ASSOC_REQ]++;
			break;
		case cpu_to_le16(IEEE80211_STYPE_ASSOC_RESP):
			stats->mgmt[MANAGEMENT_ASSOC_RESP]++;
			break;
		case cpu_to_le16(IEEE80211_STYPE_REASSOC_REQ):
			stats->mgmt[MANAGEMENT_REASSOC_REQ]++;
			break;
		case cpu_to_le16(IEEE80211_STYPE_REASSOC_RESP):
			stats->mgmt[MANAGEMENT_REASSOC_RESP]++;
			break;
		case cpu_to_le16(IEEE80211_STYPE_PROBE_REQ):
			stats->mgmt[MANAGEMENT_PROBE_REQ]++;
			break;
		case cpu_to_le16(IEEE80211_STYPE_PROBE_RESP):
			stats->mgmt[MANAGEMENT_PROBE_RESP]++;
			break;
		case cpu_to_le16(IEEE80211_STYPE_BEACON):
			stats->mgmt[MANAGEMENT_BEACON]++;
			break;
		case cpu_to_le16(IEEE80211_STYPE_ATIM):
			stats->mgmt[MANAGEMENT_ATIM]++;
			break;
		case cpu_to_le16(IEEE80211_STYPE_DISASSOC):
			stats->mgmt[MANAGEMENT_DISASSOC]++;
			break;
		case cpu_to_le16(IEEE80211_STYPE_AUTH):
			stats->mgmt[MANAGEMENT_AUTH]++;
			break;
		case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
			stats->mgmt[MANAGEMENT_DEAUTH]++;
			break;
		case cpu_to_le16(IEEE80211_STYPE_ACTION):
			stats->mgmt[MANAGEMENT_ACTION]++;
			break;
		}
	} else if (ieee80211_is_ctl(fc)) {
		switch (fc & cpu_to_le16(IEEE80211_FCTL_STYPE)) {
		case cpu_to_le16(IEEE80211_STYPE_BACK_REQ):
			stats->ctrl[CONTROL_BACK_REQ]++;
			break;
		case cpu_to_le16(IEEE80211_STYPE_BACK):
			stats->ctrl[CONTROL_BACK]++;
			break;
		case cpu_to_le16(IEEE80211_STYPE_PSPOLL):
			stats->ctrl[CONTROL_PSPOLL]++;
			break;
		case cpu_to_le16(IEEE80211_STYPE_RTS):
			stats->ctrl[CONTROL_RTS]++;
			break;
		case cpu_to_le16(IEEE80211_STYPE_CTS):
			stats->ctrl[CONTROL_CTS]++;
			break;
		case cpu_to_le16(IEEE80211_STYPE_ACK):
			stats->ctrl[CONTROL_ACK]++;
			break;
		case cpu_to_le16(IEEE80211_STYPE_CFEND):
			stats->ctrl[CONTROL_CFEND]++;
			break;
		case cpu_to_le16(IEEE80211_STYPE_CFENDACK):
			stats->ctrl[CONTROL_CFENDACK]++;
			break;
		}
	} else {
		/* data */
		stats->data_cnt++;
		stats->data_bytes += len;
	}
}
935 936
#endif

W
Wey-Yi Guy 已提交
937
static void iwl_force_rf_reset(struct iwl_priv *priv)
938
{
D
Don Fry 已提交
939
	if (test_bit(STATUS_EXIT_PENDING, &priv->status))
940 941
		return;

942
	if (!iwl_is_any_associated(priv)) {
943 944 945 946 947 948 949 950 951 952 953 954 955 956 957
		IWL_DEBUG_SCAN(priv, "force reset rejected: not associated\n");
		return;
	}
	/*
	 * There is no easy and better way to force reset the radio,
	 * the only known method is switching channel which will force to
	 * reset and tune the radio.
	 * Use internal short scan (single channel) operation to should
	 * achieve this objective.
	 * Driver should reset the radio when number of consecutive missed
	 * beacon, or any other uCode error condition detected.
	 */
	IWL_DEBUG_INFO(priv, "perform radio reset.\n");
	iwl_internal_short_hw_scan(priv);
}
W
Wey-Yi Guy 已提交
958 959


960
int iwl_force_reset(struct iwl_priv *priv, int mode, bool external)
W
Wey-Yi Guy 已提交
961
{
962 963
	struct iwl_force_reset *force_reset;

D
Don Fry 已提交
964
	if (test_bit(STATUS_EXIT_PENDING, &priv->status))
W
Wey-Yi Guy 已提交
965 966
		return -EINVAL;

967 968 969 970 971 972
	if (mode >= IWL_MAX_FORCE_RESET) {
		IWL_DEBUG_INFO(priv, "invalid reset request.\n");
		return -EINVAL;
	}
	force_reset = &priv->force_reset[mode];
	force_reset->reset_request_count++;
973 974 975 976 977 978 979 980
	if (!external) {
		if (force_reset->last_force_reset_jiffies &&
		    time_after(force_reset->last_force_reset_jiffies +
		    force_reset->reset_duration, jiffies)) {
			IWL_DEBUG_INFO(priv, "force reset rejected\n");
			force_reset->reset_reject_count++;
			return -EAGAIN;
		}
W
Wey-Yi Guy 已提交
981
	}
982 983
	force_reset->reset_success_count++;
	force_reset->last_force_reset_jiffies = jiffies;
W
Wey-Yi Guy 已提交
984 985 986 987 988 989
	IWL_DEBUG_INFO(priv, "perform force reset (%d)\n", mode);
	switch (mode) {
	case IWL_RF_RESET:
		iwl_force_rf_reset(priv);
		break;
	case IWL_FW_RESET:
990 991 992 993 994 995 996 997
		/*
		 * if the request is from external(ex: debugfs),
		 * then always perform the request in regardless the module
		 * parameter setting
		 * if the request is from internal (uCode error or driver
		 * detect failure), then fw_restart module parameter
		 * need to be check before performing firmware reload
		 */
D
Don Fry 已提交
998
		if (!external && !iwlagn_mod_params.restart_fw) {
999 1000 1001 1002
			IWL_DEBUG_INFO(priv, "Cancel firmware reload based on "
				       "module parameter setting\n");
			break;
		}
W
Wey-Yi Guy 已提交
1003
		IWL_ERR(priv, "On demand firmware reload\n");
1004
		iwlagn_fw_error(priv, true);
W
Wey-Yi Guy 已提交
1005 1006 1007 1008
		break;
	}
	return 0;
}
1009

1010

1011 1012
int iwl_cmd_echo_test(struct iwl_priv *priv)
{
1013
	int ret;
1014 1015
	struct iwl_host_cmd cmd = {
		.id = REPLY_ECHO,
1016
		.len = { 0 },
1017 1018 1019
		.flags = CMD_SYNC,
	};

1020
	ret = iwl_dvm_send_cmd(priv, &cmd);
1021 1022 1023 1024 1025
	if (ret)
		IWL_ERR(priv, "echo testing fail: 0X%x\n", ret);
	else
		IWL_DEBUG_INFO(priv, "echo testing pass\n");
	return ret;
1026 1027
}

1028
static inline int iwl_check_stuck_queue(struct iwl_priv *priv, int txq)
1029
{
1030
	if (iwl_trans_check_stuck_queue(trans(priv), txq)) {
1031 1032
		int ret;
		ret = iwl_force_reset(priv, IWL_FW_RESET, false);
1033
		return (ret == -EAGAIN) ? 0 : 1;
1034 1035 1036 1037
	}
	return 0;
}

1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
/*
 * Making watchdog tick be a quarter of timeout assure we will
 * discover the queue hung between timeout and 1.25*timeout
 */
#define IWL_WD_TICK(timeout) ((timeout) / 4)

/*
 * Watchdog timer callback, we check each tx queue for stuck, if if hung
 * we reset the firmware. If everything is fine just rearm the timer.
 */
void iwl_bg_watchdog(unsigned long data)
1049 1050 1051
{
	struct iwl_priv *priv = (struct iwl_priv *)data;
	int cnt;
1052
	unsigned long timeout;
1053

D
Don Fry 已提交
1054
	if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1055 1056
		return;

D
Don Fry 已提交
1057
	if (iwl_is_rfkill(priv))
W
Wey-Yi Guy 已提交
1058 1059
		return;

1060
	timeout = hw_params(priv).wd_timeout;
1061 1062 1063
	if (timeout == 0)
		return;

1064
	/* monitor and check for stuck queues */
1065
	for (cnt = 0; cnt < cfg(priv)->base_params->num_of_queues; cnt++)
1066 1067
		if (iwl_check_stuck_queue(priv, cnt))
			return;
1068 1069 1070

	mod_timer(&priv->watchdog, jiffies +
		  msecs_to_jiffies(IWL_WD_TICK(timeout)));
1071
}
1072 1073 1074

void iwl_setup_watchdog(struct iwl_priv *priv)
{
1075
	unsigned int timeout = hw_params(priv).wd_timeout;
1076

1077 1078
	if (!iwlagn_mod_params.wd_disable) {
		/* use system default */
1079
		if (timeout && !cfg(priv)->base_params->wd_disable)
1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093
			mod_timer(&priv->watchdog,
				jiffies +
				msecs_to_jiffies(IWL_WD_TICK(timeout)));
		else
			del_timer(&priv->watchdog);
	} else {
		/* module parameter overwrite default configuration */
		if (timeout && iwlagn_mod_params.wd_disable == 2)
			mod_timer(&priv->watchdog,
				jiffies +
				msecs_to_jiffies(IWL_WD_TICK(timeout)));
		else
			del_timer(&priv->watchdog);
	}
1094
}
1095

1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
/**
 * iwl_beacon_time_mask_low - mask of lower 32 bit of beacon time
 * @priv -- pointer to iwl_priv data structure
 * @tsf_bits -- number of bits need to shift for masking)
 */
static inline u32 iwl_beacon_time_mask_low(struct iwl_priv *priv,
					   u16 tsf_bits)
{
	return (1 << tsf_bits) - 1;
}

/**
 * iwl_beacon_time_mask_high - mask of higher 32 bit of beacon time
 * @priv -- pointer to iwl_priv data structure
 * @tsf_bits -- number of bits need to shift for masking)
 */
static inline u32 iwl_beacon_time_mask_high(struct iwl_priv *priv,
					    u16 tsf_bits)
{
	return ((1 << (32 - tsf_bits)) - 1) << tsf_bits;
}

1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133
/*
 * extended beacon time format
 * time in usec will be changed into a 32-bit value in extended:internal format
 * the extended part is the beacon counts
 * the internal part is the time in usec within one beacon interval
 */
u32 iwl_usecs_to_beacons(struct iwl_priv *priv, u32 usec, u32 beacon_interval)
{
	u32 quot;
	u32 rem;
	u32 interval = beacon_interval * TIME_UNIT;

	if (!interval || !usec)
		return 0;

	quot = (usec / interval) &
1134 1135
		(iwl_beacon_time_mask_high(priv, IWLAGN_EXT_BEACON_TIME_POS) >>
		IWLAGN_EXT_BEACON_TIME_POS);
1136
	rem = (usec % interval) & iwl_beacon_time_mask_low(priv,
1137
				   IWLAGN_EXT_BEACON_TIME_POS);
1138

1139
	return (quot << IWLAGN_EXT_BEACON_TIME_POS) + rem;
1140 1141 1142 1143 1144 1145 1146 1147 1148
}

/* base is usually what we get from ucode with each received frame,
 * the same as HW timer counter counting down
 */
__le32 iwl_add_beacon_time(struct iwl_priv *priv, u32 base,
			   u32 addon, u32 beacon_interval)
{
	u32 base_low = base & iwl_beacon_time_mask_low(priv,
1149
				IWLAGN_EXT_BEACON_TIME_POS);
1150
	u32 addon_low = addon & iwl_beacon_time_mask_low(priv,
1151
				IWLAGN_EXT_BEACON_TIME_POS);
1152 1153
	u32 interval = beacon_interval * TIME_UNIT;
	u32 res = (base & iwl_beacon_time_mask_high(priv,
1154
				IWLAGN_EXT_BEACON_TIME_POS)) +
1155
				(addon & iwl_beacon_time_mask_high(priv,
1156
				IWLAGN_EXT_BEACON_TIME_POS));
1157 1158 1159 1160 1161

	if (base_low > addon_low)
		res += base_low - addon_low;
	else if (base_low < addon_low) {
		res += interval + base_low - addon_low;
1162
		res += (1 << IWLAGN_EXT_BEACON_TIME_POS);
1163
	} else
1164
		res += (1 << IWLAGN_EXT_BEACON_TIME_POS);
1165 1166 1167 1168

	return cpu_to_le32(res);
}

1169
void iwl_set_hw_rfkill_state(struct iwl_op_mode *op_mode, bool state)
1170
{
1171 1172
	struct iwl_priv *priv = IWL_OP_MODE_GET_DVM(op_mode);

1173
	if (state)
D
Don Fry 已提交
1174
		set_bit(STATUS_RF_KILL_HW, &priv->status);
1175
	else
D
Don Fry 已提交
1176
		clear_bit(STATUS_RF_KILL_HW, &priv->status);
1177

1178 1179
	wiphy_rfkill_set_hw_state(priv->hw->wiphy, state);
}
1180

1181
void iwl_free_skb(struct iwl_op_mode *op_mode, struct sk_buff *skb)
1182 1183 1184 1185
{
	struct ieee80211_tx_info *info;

	info = IEEE80211_SKB_CB(skb);
1186
	kmem_cache_free(iwl_tx_cmd_pool, (info->driver_data[1]));
1187 1188
	dev_kfree_skb_any(skb);
}