main.c 83.2 KB
Newer Older
1
/*
2
 * Copyright (c) 2008-2009 Atheros Communications Inc.
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <linux/nl80211.h>
Sujith's avatar
Sujith committed
18
#include "ath9k.h"
19
#include "btcoex.h"
20
21
22
23
24
25
26
27

static char *dev_info = "ath9k";

MODULE_AUTHOR("Atheros Communications");
MODULE_DESCRIPTION("Support for Atheros 802.11n wireless LAN cards.");
MODULE_SUPPORTED_DEVICE("Atheros 802.11n WLAN cards");
MODULE_LICENSE("Dual BSD/GPL");

28
29
30
31
static int modparam_nohwcrypt;
module_param_named(nohwcrypt, modparam_nohwcrypt, int, 0444);
MODULE_PARM_DESC(nohwcrypt, "Disable hardware encryption");

32
33
static unsigned int ath9k_debug = ATH_DBG_DEFAULT;
module_param_named(debug, ath9k_debug, uint, 0);
34
MODULE_PARM_DESC(debug, "Debugging mask");
35

36
37
38
39
40
/* We use the hw_value as an index into our private channel structure */

#define CHAN2G(_freq, _idx)  { \
	.center_freq = (_freq), \
	.hw_value = (_idx), \
41
	.max_power = 20, \
42
43
44
45
46
47
}

#define CHAN5G(_freq, _idx) { \
	.band = IEEE80211_BAND_5GHZ, \
	.center_freq = (_freq), \
	.hw_value = (_idx), \
48
	.max_power = 20, \
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
}

/* Some 2 GHz radios are actually tunable on 2312-2732
 * on 5 MHz steps, we support the channels which we know
 * we have calibration data for all cards though to make
 * this static */
static struct ieee80211_channel ath9k_2ghz_chantable[] = {
	CHAN2G(2412, 0), /* Channel 1 */
	CHAN2G(2417, 1), /* Channel 2 */
	CHAN2G(2422, 2), /* Channel 3 */
	CHAN2G(2427, 3), /* Channel 4 */
	CHAN2G(2432, 4), /* Channel 5 */
	CHAN2G(2437, 5), /* Channel 6 */
	CHAN2G(2442, 6), /* Channel 7 */
	CHAN2G(2447, 7), /* Channel 8 */
	CHAN2G(2452, 8), /* Channel 9 */
	CHAN2G(2457, 9), /* Channel 10 */
	CHAN2G(2462, 10), /* Channel 11 */
	CHAN2G(2467, 11), /* Channel 12 */
	CHAN2G(2472, 12), /* Channel 13 */
	CHAN2G(2484, 13), /* Channel 14 */
};

/* Some 5 GHz radios are actually tunable on XXXX-YYYY
 * on 5 MHz steps, we support the channels which we know
 * we have calibration data for all cards though to make
 * this static */
static struct ieee80211_channel ath9k_5ghz_chantable[] = {
	/* _We_ call this UNII 1 */
	CHAN5G(5180, 14), /* Channel 36 */
	CHAN5G(5200, 15), /* Channel 40 */
	CHAN5G(5220, 16), /* Channel 44 */
	CHAN5G(5240, 17), /* Channel 48 */
	/* _We_ call this UNII 2 */
	CHAN5G(5260, 18), /* Channel 52 */
	CHAN5G(5280, 19), /* Channel 56 */
	CHAN5G(5300, 20), /* Channel 60 */
	CHAN5G(5320, 21), /* Channel 64 */
	/* _We_ call this "Middle band" */
	CHAN5G(5500, 22), /* Channel 100 */
	CHAN5G(5520, 23), /* Channel 104 */
	CHAN5G(5540, 24), /* Channel 108 */
	CHAN5G(5560, 25), /* Channel 112 */
	CHAN5G(5580, 26), /* Channel 116 */
	CHAN5G(5600, 27), /* Channel 120 */
	CHAN5G(5620, 28), /* Channel 124 */
	CHAN5G(5640, 29), /* Channel 128 */
	CHAN5G(5660, 30), /* Channel 132 */
	CHAN5G(5680, 31), /* Channel 136 */
	CHAN5G(5700, 32), /* Channel 140 */
	/* _We_ call this UNII 3 */
	CHAN5G(5745, 33), /* Channel 149 */
	CHAN5G(5765, 34), /* Channel 153 */
	CHAN5G(5785, 35), /* Channel 157 */
	CHAN5G(5805, 36), /* Channel 161 */
	CHAN5G(5825, 37), /* Channel 165 */
};

107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/* Atheros hardware rate code addition for short premble */
#define SHPCHECK(__hw_rate, __flags) \
	((__flags & IEEE80211_RATE_SHORT_PREAMBLE) ? (__hw_rate | 0x04 ) : 0)

#define RATE(_bitrate, _hw_rate, _flags) {              \
	.bitrate        = (_bitrate),                   \
	.flags          = (_flags),                     \
	.hw_value       = (_hw_rate),                   \
	.hw_value_short = (SHPCHECK(_hw_rate, _flags))  \
}

static struct ieee80211_rate ath9k_legacy_rates[] = {
	RATE(10, 0x1b, 0),
	RATE(20, 0x1a, IEEE80211_RATE_SHORT_PREAMBLE),
	RATE(55, 0x19, IEEE80211_RATE_SHORT_PREAMBLE),
	RATE(110, 0x18, IEEE80211_RATE_SHORT_PREAMBLE),
	RATE(60, 0x0b, 0),
	RATE(90, 0x0f, 0),
	RATE(120, 0x0a, 0),
	RATE(180, 0x0e, 0),
	RATE(240, 0x09, 0),
	RATE(360, 0x0d, 0),
	RATE(480, 0x08, 0),
	RATE(540, 0x0c, 0),
};

133
134
static void ath_cache_conf_rate(struct ath_softc *sc,
				struct ieee80211_conf *conf)
Sujith's avatar
Sujith committed
135
{
136
137
138
	switch (conf->channel->band) {
	case IEEE80211_BAND_2GHZ:
		if (conf_is_ht20(conf))
139
			sc->cur_rate_mode = ATH9K_MODE_11NG_HT20;
140
		else if (conf_is_ht40_minus(conf))
141
			sc->cur_rate_mode = ATH9K_MODE_11NG_HT40MINUS;
142
		else if (conf_is_ht40_plus(conf))
143
			sc->cur_rate_mode = ATH9K_MODE_11NG_HT40PLUS;
144
		else
145
			sc->cur_rate_mode = ATH9K_MODE_11G;
146
147
148
		break;
	case IEEE80211_BAND_5GHZ:
		if (conf_is_ht20(conf))
149
			sc->cur_rate_mode = ATH9K_MODE_11NA_HT20;
150
		else if (conf_is_ht40_minus(conf))
151
			sc->cur_rate_mode = ATH9K_MODE_11NA_HT40MINUS;
152
		else if (conf_is_ht40_plus(conf))
153
			sc->cur_rate_mode = ATH9K_MODE_11NA_HT40PLUS;
154
		else
155
			sc->cur_rate_mode = ATH9K_MODE_11A;
156
157
		break;
	default:
158
		BUG_ON(1);
159
160
		break;
	}
Sujith's avatar
Sujith committed
161
162
163
164
}

static void ath_update_txpow(struct ath_softc *sc)
{
165
	struct ath_hw *ah = sc->sc_ah;
Sujith's avatar
Sujith committed
166
167
	u32 txpow;

Sujith's avatar
Sujith committed
168
169
	if (sc->curtxpow != sc->config.txpowlimit) {
		ath9k_hw_set_txpowerlimit(ah, sc->config.txpowlimit);
Sujith's avatar
Sujith committed
170
171
		/* read back in case value is clamped */
		ath9k_hw_getcapability(ah, ATH9K_CAP_TXPOW, 1, &txpow);
Sujith's avatar
Sujith committed
172
		sc->curtxpow = txpow;
Sujith's avatar
Sujith committed
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
	}
}

static u8 parse_mpdudensity(u8 mpdudensity)
{
	/*
	 * 802.11n D2.0 defined values for "Minimum MPDU Start Spacing":
	 *   0 for no restriction
	 *   1 for 1/4 us
	 *   2 for 1/2 us
	 *   3 for 1 us
	 *   4 for 2 us
	 *   5 for 4 us
	 *   6 for 8 us
	 *   7 for 16 us
	 */
	switch (mpdudensity) {
	case 0:
		return 0;
	case 1:
	case 2:
	case 3:
		/* Our lower layer calculations limit our precision to
		   1 microsecond */
		return 1;
	case 4:
		return 2;
	case 5:
		return 4;
	case 6:
		return 8;
	case 7:
		return 16;
	default:
		return 0;
	}
}

211
212
213
214
215
216
217
218
219
220
221
222
223
static struct ath9k_channel *ath_get_curchannel(struct ath_softc *sc,
						struct ieee80211_hw *hw)
{
	struct ieee80211_channel *curchan = hw->conf.channel;
	struct ath9k_channel *channel;
	u8 chan_idx;

	chan_idx = curchan->hw_value;
	channel = &sc->sc_ah->channels[chan_idx];
	ath9k_update_ichannel(sc, hw, channel);
	return channel;
}

224
static bool ath9k_setpower(struct ath_softc *sc, enum ath9k_power_mode mode)
225
226
227
228
{
	unsigned long flags;
	bool ret;

229
230
231
	spin_lock_irqsave(&sc->sc_pm_lock, flags);
	ret = ath9k_hw_setpower(sc->sc_ah, mode);
	spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
232
233
234
235

	return ret;
}

236
237
238
239
240
241
242
243
void ath9k_ps_wakeup(struct ath_softc *sc)
{
	unsigned long flags;

	spin_lock_irqsave(&sc->sc_pm_lock, flags);
	if (++sc->ps_usecount != 1)
		goto unlock;

244
	ath9k_hw_setpower(sc->sc_ah, ATH9K_PM_AWAKE);
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262

 unlock:
	spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
}

void ath9k_ps_restore(struct ath_softc *sc)
{
	unsigned long flags;

	spin_lock_irqsave(&sc->sc_pm_lock, flags);
	if (--sc->ps_usecount != 0)
		goto unlock;

	if (sc->ps_enabled &&
	    !(sc->sc_flags & (SC_OP_WAIT_FOR_BEACON |
			      SC_OP_WAIT_FOR_CAB |
			      SC_OP_WAIT_FOR_PSPOLL_DATA |
			      SC_OP_WAIT_FOR_TX_ACK)))
263
		ath9k_hw_setpower(sc->sc_ah, ATH9K_PM_NETWORK_SLEEP);
264
265
266
267
268

 unlock:
	spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
}

Sujith's avatar
Sujith committed
269
270
271
272
273
/*
 * Set/change channels.  If the channel is really being changed, it's done
 * by reseting the chip.  To accomplish this we must first cleanup any pending
 * DMA, then restart stuff.
*/
274
275
int ath_set_channel(struct ath_softc *sc, struct ieee80211_hw *hw,
		    struct ath9k_channel *hchan)
Sujith's avatar
Sujith committed
276
{
277
	struct ath_hw *ah = sc->sc_ah;
278
	struct ath_common *common = ath9k_hw_common(ah);
279
	struct ieee80211_conf *conf = &common->hw->conf;
Sujith's avatar
Sujith committed
280
	bool fastcc = true, stopped;
281
282
	struct ieee80211_channel *channel = hw->conf.channel;
	int r;
Sujith's avatar
Sujith committed
283
284
285
286

	if (sc->sc_flags & SC_OP_INVALID)
		return -EIO;

287
288
	ath9k_ps_wakeup(sc);

289
290
291
292
293
294
295
296
297
298
	/*
	 * This is only performed if the channel settings have
	 * actually changed.
	 *
	 * To switch channels clear any pending DMA operations;
	 * wait long enough for the RX fifo to drain, reset the
	 * hardware at the new frequency, and then re-enable
	 * the relevant bits of the h/w.
	 */
	ath9k_hw_set_interrupts(ah, 0);
Sujith's avatar
Sujith committed
299
	ath_drain_all_txq(sc, false);
300
	stopped = ath_stoprecv(sc);
Sujith's avatar
Sujith committed
301

302
303
304
	/* XXX: do not flush receive queue here. We don't want
	 * to flush data frames already in queue because of
	 * changing channel. */
Sujith's avatar
Sujith committed
305

306
307
308
	if (!stopped || (sc->sc_flags & SC_OP_FULL_RESET))
		fastcc = false;

309
	ath_print(common, ATH_DBG_CONFIG,
310
		  "(%u MHz) -> (%u MHz), conf_is_ht40: %d\n",
311
		  sc->sc_ah->curchan->channel,
312
		  channel->center_freq, conf_is_ht40(conf));
Sujith's avatar
Sujith committed
313

314
315
316
317
	spin_lock_bh(&sc->sc_resetlock);

	r = ath9k_hw_reset(ah, hchan, fastcc);
	if (r) {
318
319
320
321
		ath_print(common, ATH_DBG_FATAL,
			  "Unable to reset channel (%u Mhz) "
			  "reset status %d\n",
			  channel->center_freq, r);
322
		spin_unlock_bh(&sc->sc_resetlock);
323
		goto ps_restore;
Sujith's avatar
Sujith committed
324
	}
325
326
327
328
329
	spin_unlock_bh(&sc->sc_resetlock);

	sc->sc_flags &= ~SC_OP_FULL_RESET;

	if (ath_startrecv(sc) != 0) {
330
331
		ath_print(common, ATH_DBG_FATAL,
			  "Unable to restart recv logic\n");
332
333
		r = -EIO;
		goto ps_restore;
334
335
336
337
	}

	ath_cache_conf_rate(sc, &hw->conf);
	ath_update_txpow(sc);
Sujith's avatar
Sujith committed
338
	ath9k_hw_set_interrupts(ah, sc->imask);
339
340

 ps_restore:
341
	ath9k_ps_restore(sc);
342
	return r;
Sujith's avatar
Sujith committed
343
344
345
346
347
348
349
350
351
352
353
}

/*
 *  This routine performs the periodic noise floor calibration function
 *  that is used to adjust and optimize the chip performance.  This
 *  takes environmental changes (location, temperature) into account.
 *  When the task is complete, it reschedules itself depending on the
 *  appropriate interval that was calculated.
 */
static void ath_ani_calibrate(unsigned long data)
{
354
355
	struct ath_softc *sc = (struct ath_softc *)data;
	struct ath_hw *ah = sc->sc_ah;
356
	struct ath_common *common = ath9k_hw_common(ah);
Sujith's avatar
Sujith committed
357
358
359
360
	bool longcal = false;
	bool shortcal = false;
	bool aniflag = false;
	unsigned int timestamp = jiffies_to_msecs(jiffies);
361
	u32 cal_interval, short_cal_interval;
Sujith's avatar
Sujith committed
362

363
364
	short_cal_interval = (ah->opmode == NL80211_IFTYPE_AP) ?
		ATH_AP_SHORT_CALINTERVAL : ATH_STA_SHORT_CALINTERVAL;
Sujith's avatar
Sujith committed
365

366
367
368
369
370
371
	/* Only calibrate if awake */
	if (sc->sc_ah->power_mode != ATH9K_PM_AWAKE)
		goto set_timer;

	ath9k_ps_wakeup(sc);

Sujith's avatar
Sujith committed
372
	/* Long calibration runs independently of short calibration. */
373
	if ((timestamp - common->ani.longcal_timer) >= ATH_LONG_CALINTERVAL) {
Sujith's avatar
Sujith committed
374
		longcal = true;
375
		ath_print(common, ATH_DBG_ANI, "longcal @%lu\n", jiffies);
376
		common->ani.longcal_timer = timestamp;
Sujith's avatar
Sujith committed
377
378
	}

Sujith's avatar
Sujith committed
379
	/* Short calibration applies only while caldone is false */
380
381
	if (!common->ani.caldone) {
		if ((timestamp - common->ani.shortcal_timer) >= short_cal_interval) {
Sujith's avatar
Sujith committed
382
			shortcal = true;
383
384
			ath_print(common, ATH_DBG_ANI,
				  "shortcal @%lu\n", jiffies);
385
386
			common->ani.shortcal_timer = timestamp;
			common->ani.resetcal_timer = timestamp;
Sujith's avatar
Sujith committed
387
388
		}
	} else {
389
		if ((timestamp - common->ani.resetcal_timer) >=
Sujith's avatar
Sujith committed
390
		    ATH_RESTART_CALINTERVAL) {
391
392
393
			common->ani.caldone = ath9k_hw_reset_calvalid(ah);
			if (common->ani.caldone)
				common->ani.resetcal_timer = timestamp;
Sujith's avatar
Sujith committed
394
395
396
397
		}
	}

	/* Verify whether we must check ANI */
398
	if ((timestamp - common->ani.checkani_timer) >= ATH_ANI_POLLINTERVAL) {
Sujith's avatar
Sujith committed
399
		aniflag = true;
400
		common->ani.checkani_timer = timestamp;
Sujith's avatar
Sujith committed
401
402
403
404
405
406
	}

	/* Skip all processing if there's nothing to do. */
	if (longcal || shortcal || aniflag) {
		/* Call ANI routine if necessary */
		if (aniflag)
407
			ath9k_hw_ani_monitor(ah, ah->curchan);
Sujith's avatar
Sujith committed
408
409
410

		/* Perform calibration if necessary */
		if (longcal || shortcal) {
411
			common->ani.caldone =
412
413
414
415
				ath9k_hw_calibrate(ah,
						   ah->curchan,
						   common->rx_chainmask,
						   longcal);
Sujith's avatar
Sujith committed
416
417

			if (longcal)
418
				common->ani.noise_floor = ath9k_hw_getchan_noise(ah,
Sujith's avatar
Sujith committed
419
420
								     ah->curchan);

421
422
423
424
			ath_print(common, ATH_DBG_ANI,
				  " calibrate chan %u/%x nf: %d\n",
				  ah->curchan->channel,
				  ah->curchan->channelFlags,
425
				  common->ani.noise_floor);
Sujith's avatar
Sujith committed
426
427
428
		}
	}

429
430
	ath9k_ps_restore(sc);

431
set_timer:
Sujith's avatar
Sujith committed
432
433
434
435
436
	/*
	* Set timer interval based on previous results.
	* The interval must be the shortest necessary to satisfy ANI,
	* short calibration and long calibration.
	*/
437
	cal_interval = ATH_LONG_CALINTERVAL;
438
	if (sc->sc_ah->config.enable_ani)
439
		cal_interval = min(cal_interval, (u32)ATH_ANI_POLLINTERVAL);
440
	if (!common->ani.caldone)
441
		cal_interval = min(cal_interval, (u32)short_cal_interval);
Sujith's avatar
Sujith committed
442

443
	mod_timer(&common->ani.timer, jiffies + msecs_to_jiffies(cal_interval));
Sujith's avatar
Sujith committed
444
445
}

446
static void ath_start_ani(struct ath_common *common)
Sujith's avatar
Sujith committed
447
448
449
{
	unsigned long timestamp = jiffies_to_msecs(jiffies);

450
451
452
	common->ani.longcal_timer = timestamp;
	common->ani.shortcal_timer = timestamp;
	common->ani.checkani_timer = timestamp;
Sujith's avatar
Sujith committed
453

454
	mod_timer(&common->ani.timer,
Sujith's avatar
Sujith committed
455
456
457
		  jiffies + msecs_to_jiffies(ATH_ANI_POLLINTERVAL));
}

Sujith's avatar
Sujith committed
458
459
460
/*
 * Update tx/rx chainmask. For legacy association,
 * hard code chainmask to 1x1, for 11n association, use
461
462
 * the chainmask configuration, for bt coexistence, use
 * the chainmask configuration even in legacy mode.
Sujith's avatar
Sujith committed
463
 */
464
void ath_update_chainmask(struct ath_softc *sc, int is_ht)
Sujith's avatar
Sujith committed
465
{
466
	struct ath_hw *ah = sc->sc_ah;
467
	struct ath_common *common = ath9k_hw_common(ah);
468

469
	if ((sc->sc_flags & SC_OP_SCANNING) || is_ht ||
470
	    (ah->btcoex_hw.scheme != ATH_BTCOEX_CFG_NONE)) {
471
472
		common->tx_chainmask = ah->caps.tx_chainmask;
		common->rx_chainmask = ah->caps.rx_chainmask;
Sujith's avatar
Sujith committed
473
	} else {
474
475
		common->tx_chainmask = 1;
		common->rx_chainmask = 1;
Sujith's avatar
Sujith committed
476
477
	}

478
	ath_print(common, ATH_DBG_CONFIG,
479
		  "tx chmask: %d, rx chmask: %d\n",
480
481
		  common->tx_chainmask,
		  common->rx_chainmask);
Sujith's avatar
Sujith committed
482
483
484
485
486
487
488
489
}

static void ath_node_attach(struct ath_softc *sc, struct ieee80211_sta *sta)
{
	struct ath_node *an;

	an = (struct ath_node *)sta->drv_priv;

490
	if (sc->sc_flags & SC_OP_TXAGGR) {
Sujith's avatar
Sujith committed
491
		ath_tx_node_init(sc, an);
Sujith's avatar
Sujith committed
492
		an->maxampdu = 1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
493
494
				     sta->ht_cap.ampdu_factor);
		an->mpdudensity = parse_mpdudensity(sta->ht_cap.ampdu_density);
495
		an->last_rssi = ATH_RSSI_DUMMY_MARKER;
496
	}
Sujith's avatar
Sujith committed
497
498
499
500
501
502
503
504
505
506
507
508
509
}

static void ath_node_detach(struct ath_softc *sc, struct ieee80211_sta *sta)
{
	struct ath_node *an = (struct ath_node *)sta->drv_priv;

	if (sc->sc_flags & SC_OP_TXAGGR)
		ath_tx_node_cleanup(sc, an);
}

static void ath9k_tasklet(unsigned long data)
{
	struct ath_softc *sc = (struct ath_softc *)data;
510
	struct ath_hw *ah = sc->sc_ah;
511
	struct ath_common *common = ath9k_hw_common(ah);
512

Sujith's avatar
Sujith committed
513
	u32 status = sc->intrstatus;
Sujith's avatar
Sujith committed
514

515
516
	ath9k_ps_wakeup(sc);

Sujith's avatar
Sujith committed
517
518
	if (status & ATH9K_INT_FATAL) {
		ath_reset(sc, false);
519
		ath9k_ps_restore(sc);
Sujith's avatar
Sujith committed
520
		return;
521
	}
Sujith's avatar
Sujith committed
522

523
524
525
526
	if (status & (ATH9K_INT_RX | ATH9K_INT_RXEOL | ATH9K_INT_RXORN)) {
		spin_lock_bh(&sc->rx.rxflushlock);
		ath_rx_tasklet(sc, 0);
		spin_unlock_bh(&sc->rx.rxflushlock);
Sujith's avatar
Sujith committed
527
528
	}

529
530
531
	if (status & ATH9K_INT_TX)
		ath_tx_tasklet(sc);

532
	if ((status & ATH9K_INT_TSFOOR) && sc->ps_enabled) {
533
534
535
536
		/*
		 * TSF sync does not look correct; remain awake to sync with
		 * the next Beacon.
		 */
537
538
		ath_print(common, ATH_DBG_PS,
			  "TSFOOR - Sync with next Beacon\n");
539
		sc->sc_flags |= SC_OP_WAIT_FOR_BEACON | SC_OP_BEACON_SYNC;
540
541
	}

542
	if (ah->btcoex_hw.scheme == ATH_BTCOEX_CFG_3WIRE)
543
544
545
		if (status & ATH9K_INT_GENTIMER)
			ath_gen_timer_isr(sc->sc_ah);

Sujith's avatar
Sujith committed
546
	/* re-enable hardware interrupt */
547
	ath9k_hw_set_interrupts(ah, sc->imask);
548
	ath9k_ps_restore(sc);
Sujith's avatar
Sujith committed
549
550
}

551
irqreturn_t ath_isr(int irq, void *dev)
Sujith's avatar
Sujith committed
552
{
553
554
555
556
557
558
559
560
#define SCHED_INTR (				\
		ATH9K_INT_FATAL |		\
		ATH9K_INT_RXORN |		\
		ATH9K_INT_RXEOL |		\
		ATH9K_INT_RX |			\
		ATH9K_INT_TX |			\
		ATH9K_INT_BMISS |		\
		ATH9K_INT_CST |			\
561
562
		ATH9K_INT_TSFOOR |		\
		ATH9K_INT_GENTIMER)
563

Sujith's avatar
Sujith committed
564
	struct ath_softc *sc = dev;
565
	struct ath_hw *ah = sc->sc_ah;
Sujith's avatar
Sujith committed
566
567
568
	enum ath9k_int status;
	bool sched = false;

569
570
571
572
573
574
575
	/*
	 * The hardware is not ready/present, don't
	 * touch anything. Note this can happen early
	 * on if the IRQ is shared.
	 */
	if (sc->sc_flags & SC_OP_INVALID)
		return IRQ_NONE;
Sujith's avatar
Sujith committed
576

577
578
579

	/* shared irq, not for us */

580
	if (!ath9k_hw_intrpend(ah))
581
582
583
584
585
586
587
588
589
590
		return IRQ_NONE;

	/*
	 * Figure out the reason(s) for the interrupt.  Note
	 * that the hal returns a pseudo-ISR that may include
	 * bits we haven't explicitly enabled so we mask the
	 * value to insure we only process bits we requested.
	 */
	ath9k_hw_getisr(ah, &status);	/* NB: clears ISR too */
	status &= sc->imask;	/* discard unasked-for bits */
Sujith's avatar
Sujith committed
591

592
593
594
595
	/*
	 * If there are no status bits set, then this interrupt was not
	 * for me (should have been caught above).
	 */
596
	if (!status)
597
		return IRQ_NONE;
Sujith's avatar
Sujith committed
598

599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
	/* Cache the status */
	sc->intrstatus = status;

	if (status & SCHED_INTR)
		sched = true;

	/*
	 * If a FATAL or RXORN interrupt is received, we have to reset the
	 * chip immediately.
	 */
	if (status & (ATH9K_INT_FATAL | ATH9K_INT_RXORN))
		goto chip_reset;

	if (status & ATH9K_INT_SWBA)
		tasklet_schedule(&sc->bcon_tasklet);

	if (status & ATH9K_INT_TXURN)
		ath9k_hw_updatetxtriglevel(ah, true);

	if (status & ATH9K_INT_MIB) {
Sujith's avatar
Sujith committed
619
		/*
620
621
622
		 * Disable interrupts until we service the MIB
		 * interrupt; otherwise it will continue to
		 * fire.
Sujith's avatar
Sujith committed
623
		 */
624
625
626
627
628
629
		ath9k_hw_set_interrupts(ah, 0);
		/*
		 * Let the hal handle the event. We assume
		 * it will clear whatever condition caused
		 * the interrupt.
		 */
630
		ath9k_hw_procmibevent(ah);
631
632
		ath9k_hw_set_interrupts(ah, sc->imask);
	}
Sujith's avatar
Sujith committed
633

634
635
	if (!(ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP))
		if (status & ATH9K_INT_TIM_TIMER) {
636
637
			/* Clear RxAbort bit so that we can
			 * receive frames */
638
			ath9k_setpower(sc, ATH9K_PM_AWAKE);
639
			ath9k_hw_setrxabort(sc->sc_ah, 0);
640
			sc->sc_flags |= SC_OP_WAIT_FOR_BEACON;
Sujith's avatar
Sujith committed
641
		}
642
643

chip_reset:
Sujith's avatar
Sujith committed
644

645
646
	ath_debug_stat_interrupt(sc, status);

Sujith's avatar
Sujith committed
647
648
	if (sched) {
		/* turn off every interrupt except SWBA */
Sujith's avatar
Sujith committed
649
		ath9k_hw_set_interrupts(ah, (sc->imask & ATH9K_INT_SWBA));
Sujith's avatar
Sujith committed
650
651
652
653
		tasklet_schedule(&sc->intr_tq);
	}

	return IRQ_HANDLED;
654
655

#undef SCHED_INTR
Sujith's avatar
Sujith committed
656
657
}

658
static u32 ath_get_extchanmode(struct ath_softc *sc,
659
			       struct ieee80211_channel *chan,
Sujith's avatar
Sujith committed
660
			       enum nl80211_channel_type channel_type)
661
662
663
664
665
{
	u32 chanmode = 0;

	switch (chan->band) {
	case IEEE80211_BAND_2GHZ:
Sujith's avatar
Sujith committed
666
667
668
		switch(channel_type) {
		case NL80211_CHAN_NO_HT:
		case NL80211_CHAN_HT20:
669
			chanmode = CHANNEL_G_HT20;
Sujith's avatar
Sujith committed
670
671
			break;
		case NL80211_CHAN_HT40PLUS:
672
			chanmode = CHANNEL_G_HT40PLUS;
Sujith's avatar
Sujith committed
673
674
			break;
		case NL80211_CHAN_HT40MINUS:
675
			chanmode = CHANNEL_G_HT40MINUS;
Sujith's avatar
Sujith committed
676
677
			break;
		}
678
679
		break;
	case IEEE80211_BAND_5GHZ:
Sujith's avatar
Sujith committed
680
681
682
		switch(channel_type) {
		case NL80211_CHAN_NO_HT:
		case NL80211_CHAN_HT20:
683
			chanmode = CHANNEL_A_HT20;
Sujith's avatar
Sujith committed
684
685
			break;
		case NL80211_CHAN_HT40PLUS:
686
			chanmode = CHANNEL_A_HT40PLUS;
Sujith's avatar
Sujith committed
687
688
			break;
		case NL80211_CHAN_HT40MINUS:
689
			chanmode = CHANNEL_A_HT40MINUS;
Sujith's avatar
Sujith committed
690
691
			break;
		}
692
693
694
695
696
697
698
699
		break;
	default:
		break;
	}

	return chanmode;
}

700
static int ath_setkey_tkip(struct ath_common *common, u16 keyix, const u8 *key,
701
702
			   struct ath9k_keyval *hk, const u8 *addr,
			   bool authenticator)
703
{
704
	struct ath_hw *ah = common->ah;
705
706
	const u8 *key_rxmic;
	const u8 *key_txmic;
707

708
709
	key_txmic = key + NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY;
	key_rxmic = key + NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY;
710
711

	if (addr == NULL) {
712
713
714
715
716
		/*
		 * Group key installation - only two key cache entries are used
		 * regardless of splitmic capability since group key is only
		 * used either for TX or RX.
		 */
717
718
719
720
721
722
723
		if (authenticator) {
			memcpy(hk->kv_mic, key_txmic, sizeof(hk->kv_mic));
			memcpy(hk->kv_txmic, key_txmic, sizeof(hk->kv_mic));
		} else {
			memcpy(hk->kv_mic, key_rxmic, sizeof(hk->kv_mic));
			memcpy(hk->kv_txmic, key_rxmic, sizeof(hk->kv_mic));
		}
724
		return ath9k_hw_set_keycache_entry(ah, keyix, hk, addr);
725
	}
726
	if (!common->splitmic) {
727
		/* TX and RX keys share the same key cache entry. */
728
729
		memcpy(hk->kv_mic, key_rxmic, sizeof(hk->kv_mic));
		memcpy(hk->kv_txmic, key_txmic, sizeof(hk->kv_txmic));
730
		return ath9k_hw_set_keycache_entry(ah, keyix, hk, addr);
731
	}
732
733
734
735

	/* Separate key cache entries for TX and RX */

	/* TX key goes at first index, RX key at +32. */
736
	memcpy(hk->kv_mic, key_txmic, sizeof(hk->kv_mic));
737
	if (!ath9k_hw_set_keycache_entry(ah, keyix, hk, NULL)) {
738
		/* TX MIC entry failed. No need to proceed further */
739
		ath_print(common, ATH_DBG_FATAL,
740
			  "Setting TX MIC Key Failed\n");
741
742
743
744
745
		return 0;
	}

	memcpy(hk->kv_mic, key_rxmic, sizeof(hk->kv_mic));
	/* XXX delete tx key on failure? */
746
	return ath9k_hw_set_keycache_entry(ah, keyix + 32, hk, addr);
747
748
}

749
static int ath_reserve_key_cache_slot_tkip(struct ath_common *common)
750
751
752
{
	int i;

753
754
755
	for (i = IEEE80211_WEP_NKID; i < common->keymax / 2; i++) {
		if (test_bit(i, common->keymap) ||
		    test_bit(i + 64, common->keymap))
756
			continue; /* At least one part of TKIP key allocated */
757
758
759
		if (common->splitmic &&
		    (test_bit(i + 32, common->keymap) ||
		     test_bit(i + 64 + 32, common->keymap)))
760
761
762
763
764
765
766
767
			continue; /* At least one part of TKIP key allocated */

		/* Found a free slot for a TKIP key */
		return i;
	}
	return -1;
}

768
static int ath_reserve_key_cache_slot(struct ath_common *common)
769
770
771
772
{
	int i;

	/* First, try to find slots that would not be available for TKIP. */
773
774
775
776
777
778
	if (common->splitmic) {
		for (i = IEEE80211_WEP_NKID; i < common->keymax / 4; i++) {
			if (!test_bit(i, common->keymap) &&
			    (test_bit(i + 32, common->keymap) ||
			     test_bit(i + 64, common->keymap) ||
			     test_bit(i + 64 + 32, common->keymap)))
779
				return i;
780
781
782
783
			if (!test_bit(i + 32, common->keymap) &&
			    (test_bit(i, common->keymap) ||
			     test_bit(i + 64, common->keymap) ||
			     test_bit(i + 64 + 32, common->keymap)))
784
				return i + 32;
785
786
787
788
			if (!test_bit(i + 64, common->keymap) &&
			    (test_bit(i , common->keymap) ||
			     test_bit(i + 32, common->keymap) ||
			     test_bit(i + 64 + 32, common->keymap)))
789
				return i + 64;
790
791
792
793
			if (!test_bit(i + 64 + 32, common->keymap) &&
			    (test_bit(i, common->keymap) ||
			     test_bit(i + 32, common->keymap) ||
			     test_bit(i + 64, common->keymap)))
794
				return i + 64 + 32;
795
796
		}
	} else {
797
798
799
		for (i = IEEE80211_WEP_NKID; i < common->keymax / 2; i++) {
			if (!test_bit(i, common->keymap) &&
			    test_bit(i + 64, common->keymap))
800
				return i;
801
802
			if (test_bit(i, common->keymap) &&
			    !test_bit(i + 64, common->keymap))
803
804
805
806
807
				return i + 64;
		}
	}

	/* No partially used TKIP slots, pick any available slot */
808
	for (i = IEEE80211_WEP_NKID; i < common->keymax; i++) {
809
810
811
812
813
		/* Do not allow slots that could be needed for TKIP group keys
		 * to be used. This limitation could be removed if we know that
		 * TKIP will not be used. */
		if (i >= 64 && i < 64 + IEEE80211_WEP_NKID)
			continue;
814
		if (common->splitmic) {
815
816
817
818
819
820
			if (i >= 32 && i < 32 + IEEE80211_WEP_NKID)
				continue;
			if (i >= 64 + 32 && i < 64 + 32 + IEEE80211_WEP_NKID)
				continue;
		}

821
		if (!test_bit(i, common->keymap))
822
823
824
825
826
			return i; /* Found a free slot for a key */
	}

	/* No free slot found */
	return -1;
827
828
}

829
static int ath_key_config(struct ath_common *common,
830
			  struct ieee80211_vif *vif,
831
			  struct ieee80211_sta *sta,
832
833
			  struct ieee80211_key_conf *key)
{
834
	struct ath_hw *ah = common->ah;
835
836
837
	struct ath9k_keyval hk;
	const u8 *mac = NULL;
	int ret = 0;
838
	int idx;
839
840
841
842
843
844
845
846
847
848
849
850
851
852

	memset(&hk, 0, sizeof(hk));

	switch (key->alg) {
	case ALG_WEP:
		hk.kv_type = ATH9K_CIPHER_WEP;
		break;
	case ALG_TKIP:
		hk.kv_type = ATH9K_CIPHER_TKIP;
		break;
	case ALG_CCMP:
		hk.kv_type = ATH9K_CIPHER_AES_CCM;
		break;
	default:
Jouni Malinen's avatar
Jouni Malinen committed
853
		return -EOPNOTSUPP;
854
855
	}

856
	hk.kv_len = key->keylen;
857
858
	memcpy(hk.kv_val, key->key, key->keylen);

859
860
861
862
863
	if (!(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
		/* For now, use the default keys for broadcast keys. This may
		 * need to change with virtual interfaces. */
		idx = key->keyidx;
	} else if (key->keyidx) {
864
865
866
867
		if (WARN_ON(!sta))
			return -EOPNOTSUPP;
		mac = sta->addr;

868
869
870
871
872
873
		if (vif->type != NL80211_IFTYPE_AP) {
			/* Only keyidx 0 should be used with unicast key, but
			 * allow this for client mode for now. */
			idx = key->keyidx;
		} else
			return -EIO;
874
	} else {
875
876
877
878
		if (WARN_ON(!sta))
			return -EOPNOTSUPP;
		mac = sta->addr;

879
		if (key->alg == ALG_TKIP)
880
			idx = ath_reserve_key_cache_slot_tkip(common);
881
		else
882
			idx = ath_reserve_key_cache_slot(common);
883
		if (idx < 0)
Jouni Malinen's avatar
Jouni Malinen committed
884
			return -ENOSPC; /* no free key cache entries */
885
886
887
	}

	if (key->alg == ALG_TKIP)
888
		ret = ath_setkey_tkip(common, idx, key->key, &hk, mac,
889
				      vif->type == NL80211_IFTYPE_AP);
890
	else
891
		ret = ath9k_hw_set_keycache_entry(ah, idx, &hk, mac);
892
893
894
895

	if (!ret)
		return -EIO;

896
	set_bit(idx, common->keymap);
897
	if (key->alg == ALG_TKIP) {
898
899
900
901
		set_bit(idx + 64, common->keymap);
		if (common->splitmic) {
			set_bit(idx + 32, common->keymap);
			set_bit(idx + 64 + 32, common->keymap);
902
903
904
905
		}
	}

	return idx;
906
907
}

908
static void ath_key_delete(struct ath_common *common, struct ieee80211_key_conf *key)
909
{
910
911
912
	struct ath_hw *ah = common->ah;

	ath9k_hw_keyreset(ah, key->hw_key_idx);
913
914
915
	if (key->hw_key_idx < IEEE80211_WEP_NKID)
		return;

916
	clear_bit(key->hw_key_idx, common->keymap);
917
918
	if (key->alg != ALG_TKIP)
		return;
919

920
921
922
923
	clear_bit(key->hw_key_idx + 64, common->keymap);
	if (common->splitmic) {
		clear_bit(key->hw_key_idx + 32, common->keymap);
		clear_bit(key->hw_key_idx + 64 + 32, common->keymap);
924
	}
925
926
}

927
928
static void setup_ht_cap(struct ath_softc *sc,
			 struct ieee80211_sta_ht_cap *ht_info)
929
{
930
	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
931
	u8 tx_streams, rx_streams;
932

933
934
935
936
937
	ht_info->ht_supported = true;
	ht_info->cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
		       IEEE80211_HT_CAP_SM_PS |
		       IEEE80211_HT_CAP_SGI_40 |
		       IEEE80211_HT_CAP_DSSSCCK40;
938

Sujith's avatar
Sujith committed
939
940
	ht_info->ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
	ht_info->ampdu_density = IEEE80211_HT_MPDU_DENSITY_8;
941

942
943
	/* set up supported mcs set */
	memset(&ht_info->mcs, 0, sizeof(ht_info->mcs));
944
945
946
947
	tx_streams = !(common->tx_chainmask & (common->tx_chainmask - 1)) ?
		     1 : 2;
	rx_streams = !(common->rx_chainmask & (common->rx_chainmask - 1)) ?
		     1 : 2;
948
949

	if (tx_streams != rx_streams) {
950
		ath_print(common, ATH_DBG_CONFIG,
951
952
			  "TX streams %d, RX streams: %d\n",
			  tx_streams, rx_streams);
953
954
955
956
		ht_info->mcs.tx_params |= IEEE80211_HT_MCS_TX_RX_DIFF;
		ht_info->mcs.tx_params |= ((tx_streams - 1) <<
				IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT);
	}
957

958
959
	ht_info->mcs.rx_mask[0] = 0xff;
	if (rx_streams >= 2)
960
961
		ht_info->mcs.rx_mask[1] = 0xff;

962
	ht_info->mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
963
964
}

965
static void ath9k_bss_assoc_info(struct ath_softc *sc,
Sujith's avatar
Sujith committed
966
				 struct ieee80211_vif *vif,
967
				 struct ieee80211_bss_conf *bss_conf)
968
{
969
	struct ath_hw *ah = sc->sc_ah;
970
	struct ath_common *common = ath9k_hw_common(ah);
971

972
	if (bss_conf->assoc) {
973
974
975
		ath_print(common, ATH_DBG_CONFIG,
			  "Bss Info ASSOC %d, bssid: %pM\n",
			   bss_conf->aid, common->curbssid);
976

977
		/* New association, store aid */
978
		common->curaid = bss_conf->aid;
979
		ath9k_hw_write_associd(ah);
980
981
982
983
984
985
986

		/*
		 * Request a re-configuration of Beacon related timers
		 * on the receipt of the first Beacon frame (i.e.,
		 * after time sync with the AP).
		 */
		sc->sc_flags |= SC_OP_BEACON_SYNC;
987

988
		/* Configure the beacon */
989
		ath_beacon_config(sc, vif);
990

991
		/* Reset rssi stats */
992
		sc->sc_ah->stats.avgbrssi = ATH_RSSI_DUMMY_MARKER;
993

994
		ath_start_ani(common);
995
	} else {
996
		ath_print(common, ATH_DBG_CONFIG, "Bss Info DISASSOC\n");
997
		common->curaid = 0;
998
		/* Stop ANI */
999
		del_timer_sync(&common->ani.timer);
1000
	}
1001
}
1002

1003
1004
1005
/********************************/
/*	 LED functions		*/
/********************************/
1006

1007
1008
1009
1010
1011
1012
1013
static void ath_led_blink_work(struct work_struct *work)
{
	struct ath_softc *sc = container_of(work, struct ath_softc,
					    ath_led_blink_work.work);

	if (!(sc->sc_flags & SC_OP_LED_ASSOCIATED))
		return;
1014
1015
1016

	if ((sc->led_on_duration == ATH_LED_ON_DURATION_IDLE) ||
	    (sc->led_off_duration == ATH_LED_OFF_DURATION_IDLE))
1017
		ath9k_hw_set_gpio(sc->sc_ah, sc->sc_ah->led_pin, 0);
1018
	else
1019
		ath9k_hw_set_gpio(sc->sc_ah, sc->sc_ah->led_pin,
1020
				  (sc->sc_flags & SC_OP_LED_ON) ? 1 : 0);
1021

1022
1023
1024
1025
1026
	ieee80211_queue_delayed_work(sc->hw,
				     &sc->ath_led_blink_work,
				     (sc->sc_flags & SC_OP_LED_ON) ?
					msecs_to_jiffies(sc->led_off_duration) :
					msecs_to_jiffies(sc->led_on_duration));
1027

1028
1029
1030
1031
1032
1033
	sc->led_on_duration = sc->led_on_cnt ?
			max((ATH_LED_ON_DURATION_IDLE - sc->led_on_cnt), 25) :
			ATH_LED_ON_DURATION_IDLE;
	sc->led_off_duration = sc->led_off_cnt ?
			max((ATH_LED_OFF_DURATION_IDLE - sc->led_off_cnt), 10) :
			ATH_LED_OFF_DURATION_IDLE;
1034
1035
1036
1037
1038
1039
1040
	sc->led_on_cnt = sc->led_off_cnt = 0;
	if (sc->sc_flags & SC_OP_LED_ON)
		sc->sc_flags &= ~SC_OP_LED_ON;
	else
		sc->sc_flags |= SC_OP_LED_ON;
}

1041
1042
1043
1044
1045
static void ath_led_brightness(struct led_classdev *led_cdev,
			       enum led_brightness brightness)
{
	struct ath_led *led = container_of(led_cdev, struct ath_led, led_cdev);
	struct ath_softc *sc = led->sc;
1046

1047
1048
1049
	switch (brightness) {
	case LED_OFF:
		if (led->led_type == ATH_LED_ASSOC ||
1050
		    led->led_type == ATH_LED_RADIO) {
1051
			ath9k_hw_set_gpio(sc->sc_ah, sc->sc_ah->led_pin,
1052
				(led->led_type == ATH_LED_RADIO));
1053
			sc->sc_flags &= ~SC_OP_LED_ASSOCIATED;
1054
1055
1056
1057
1058
			if (led->led_type == ATH_LED_RADIO)
				sc->sc_flags &= ~SC_OP_LED_ON;
		} else {
			sc->led_off_cnt++;
		}
1059
1060
		break;
	case LED_FULL:
1061
		if (led->led_type == ATH_LED_ASSOC) {
1062
			sc->sc_flags |= SC_OP_LED_ASSOCIATED;