main.c 58.9 KB
Newer Older
1
/*
2
 * Copyright (c) 2008-2011 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>
18
#include <linux/delay.h>
Sujith's avatar
Sujith committed
19
#include "ath9k.h"
20
#include "btcoex.h"
21

Sujith's avatar
Sujith committed
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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;
	}
}

57
58
59
60
61
62
63
64
65
66
67
68
69
static bool ath9k_has_pending_frames(struct ath_softc *sc, struct ath_txq *txq)
{
	bool pending = false;

	spin_lock_bh(&txq->axq_lock);

	if (txq->axq_depth || !list_empty(&txq->axq_acq))
		pending = true;

	spin_unlock_bh(&txq->axq_lock);
	return pending;
}

70
static bool ath9k_setpower(struct ath_softc *sc, enum ath9k_power_mode mode)
71
72
73
74
{
	unsigned long flags;
	bool ret;

75
76
77
	spin_lock_irqsave(&sc->sc_pm_lock, flags);
	ret = ath9k_hw_setpower(sc->sc_ah, mode);
	spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
78
79
80
81

	return ret;
}

82
83
void ath9k_ps_wakeup(struct ath_softc *sc)
{
84
	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
85
	unsigned long flags;
86
	enum ath9k_power_mode power_mode;
87
88
89
90
91

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

92
	power_mode = sc->sc_ah->power_mode;
93
	ath9k_hw_setpower(sc->sc_ah, ATH9K_PM_AWAKE);
94

95
96
97
98
99
	/*
	 * While the hardware is asleep, the cycle counters contain no
	 * useful data. Better clear them now so that they don't mess up
	 * survey data results.
	 */
100
101
102
103
104
105
	if (power_mode != ATH9K_PM_AWAKE) {
		spin_lock(&common->cc_lock);
		ath_hw_cycle_counters_update(common);
		memset(&common->cc_survey, 0, sizeof(common->cc_survey));
		spin_unlock(&common->cc_lock);
	}
106

107
108
109
110
111
112
 unlock:
	spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
}

void ath9k_ps_restore(struct ath_softc *sc)
{
113
	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
114
	enum ath9k_power_mode mode;
115
116
117
118
119
120
	unsigned long flags;

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

121
122
123
124
	if (sc->ps_flags & PS_WAIT_FOR_TX_ACK)
		goto unlock;

	if (sc->ps_idle)
125
		mode = ATH9K_PM_FULL_SLEEP;
126
127
	else if (sc->ps_enabled &&
		 !(sc->ps_flags & (PS_WAIT_FOR_BEACON |
Sujith's avatar
Sujith committed
128
			      PS_WAIT_FOR_CAB |
129
			      PS_WAIT_FOR_PSPOLL_DATA)))
130
131
132
133
134
135
136
137
		mode = ATH9K_PM_NETWORK_SLEEP;
	else
		goto unlock;

	spin_lock(&common->cc_lock);
	ath_hw_cycle_counters_update(common);
	spin_unlock(&common->cc_lock);

138
	ath9k_hw_setpower(sc->sc_ah, mode);
139
140
141
142
143

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

144
void ath_start_ani(struct ath_common *common)
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
{
	struct ath_hw *ah = common->ah;
	unsigned long timestamp = jiffies_to_msecs(jiffies);
	struct ath_softc *sc = (struct ath_softc *) common->priv;

	if (!(sc->sc_flags & SC_OP_ANI_RUN))
		return;

	if (sc->sc_flags & SC_OP_OFFCHANNEL)
		return;

	common->ani.longcal_timer = timestamp;
	common->ani.shortcal_timer = timestamp;
	common->ani.checkani_timer = timestamp;

	mod_timer(&common->ani.timer,
		  jiffies +
			msecs_to_jiffies((u32)ah->config.ani_poll_interval));
}

165
166
167
168
169
170
171
172
static void ath_update_survey_nf(struct ath_softc *sc, int channel)
{
	struct ath_hw *ah = sc->sc_ah;
	struct ath9k_channel *chan = &ah->channels[channel];
	struct survey_info *survey = &sc->survey[channel];

	if (chan->noisefloor) {
		survey->filled |= SURVEY_INFO_NOISE_DBM;
173
		survey->noise = ath9k_hw_getchan_noise(ah, chan);
174
175
176
	}
}

177
178
179
180
181
182
/*
 * Updates the survey statistics and returns the busy time since last
 * update in %, if the measurement duration was long enough for the
 * result to be useful, -1 otherwise.
 */
static int ath_update_survey_stats(struct ath_softc *sc)
183
184
185
186
187
188
189
{
	struct ath_hw *ah = sc->sc_ah;
	struct ath_common *common = ath9k_hw_common(ah);
	int pos = ah->curchan - &ah->channels[0];
	struct survey_info *survey = &sc->survey[pos];
	struct ath_cycle_counters *cc = &common->cc_survey;
	unsigned int div = common->clockrate * 1000;
190
	int ret = 0;
191

192
	if (!ah->curchan)
193
		return -1;
194

195
196
	if (ah->power_mode == ATH9K_PM_AWAKE)
		ath_hw_cycle_counters_update(common);
197
198
199
200
201
202
203
204
205
206
207

	if (cc->cycles > 0) {
		survey->filled |= SURVEY_INFO_CHANNEL_TIME |
			SURVEY_INFO_CHANNEL_TIME_BUSY |
			SURVEY_INFO_CHANNEL_TIME_RX |
			SURVEY_INFO_CHANNEL_TIME_TX;
		survey->channel_time += cc->cycles / div;
		survey->channel_time_busy += cc->rx_busy / div;
		survey->channel_time_rx += cc->rx_frame / div;
		survey->channel_time_tx += cc->tx_frame / div;
	}
208
209
210
211
212
213
214

	if (cc->cycles < div)
		return -1;

	if (cc->cycles > 0)
		ret = cc->rx_busy * 100 / cc->cycles;

215
216
217
	memset(cc, 0, sizeof(*cc));

	ath_update_survey_nf(sc, pos);
218
219

	return ret;
220
221
}

222
static void __ath_cancel_work(struct ath_softc *sc)
Sujith's avatar
Sujith committed
223
{
224
225
226
	cancel_work_sync(&sc->paprd_work);
	cancel_work_sync(&sc->hw_check_work);
	cancel_delayed_work_sync(&sc->tx_complete_work);
227
	cancel_delayed_work_sync(&sc->hw_pll_work);
228
}
229

230
231
232
233
234
static void ath_cancel_work(struct ath_softc *sc)
{
	__ath_cancel_work(sc);
	cancel_work_sync(&sc->hw_reset_work);
}
235

236
237
238
239
240
static bool ath_prepare_reset(struct ath_softc *sc, bool retry_tx, bool flush)
{
	struct ath_hw *ah = sc->sc_ah;
	struct ath_common *common = ath9k_hw_common(ah);
	bool ret;
241

242
	ieee80211_stop_queues(sc->hw);
243

244
245
	sc->hw_busy_count = 0;
	del_timer_sync(&common->ani.timer);
Sujith's avatar
Sujith committed
246

247
248
	ath9k_debug_samp_bb_mac(sc);
	ath9k_hw_disable_interrupts(ah);
249

250
	ret = ath_drain_all_txq(sc, retry_tx);
Sujith's avatar
Sujith committed
251

252
253
	if (!ath_stoprecv(sc))
		ret = false;
254

255
256
	if (!flush) {
		if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
257
258
			ath_rx_tasklet(sc, 1, true);
		ath_rx_tasklet(sc, 1, false);
259
260
261
	} else {
		ath_flushrecv(sc);
	}
262

263
264
	return ret;
}
Sujith's avatar
Sujith committed
265

266
267
268
269
static bool ath_complete_reset(struct ath_softc *sc, bool start)
{
	struct ath_hw *ah = sc->sc_ah;
	struct ath_common *common = ath9k_hw_common(ah);
270
271

	if (ath_startrecv(sc) != 0) {
272
		ath_err(common, "Unable to restart recv logic\n");
273
		return false;
274
275
	}

276
277
	ath9k_cmn_update_txpow(ah, sc->curtxpow,
			       sc->config.txpowlimit, &sc->curtxpow);
278
	ath9k_hw_set_interrupts(ah);
279
	ath9k_hw_enable_interrupts(ah);
280

281
	if (!(sc->sc_flags & (SC_OP_OFFCHANNEL)) && start) {
282
		if (sc->sc_flags & SC_OP_BEACONS)
283
			ath_set_beacon(sc);
284

285
		ieee80211_queue_delayed_work(sc->hw, &sc->tx_complete_work, 0);
286
		ieee80211_queue_delayed_work(sc->hw, &sc->hw_pll_work, HZ/2);
287
288
		if (!common->disable_ani)
			ath_start_ani(common);
289
290
	}

291
	if ((ah->caps.hw_caps & ATH9K_HW_CAP_ANT_DIV_COMB) && sc->ant_rx != 3) {
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
		struct ath_hw_antcomb_conf div_ant_conf;
		u8 lna_conf;

		ath9k_hw_antdiv_comb_conf_get(ah, &div_ant_conf);

		if (sc->ant_rx == 1)
			lna_conf = ATH_ANT_DIV_COMB_LNA1;
		else
			lna_conf = ATH_ANT_DIV_COMB_LNA2;
		div_ant_conf.main_lna_conf = lna_conf;
		div_ant_conf.alt_lna_conf = lna_conf;

		ath9k_hw_antdiv_comb_conf_set(ah, &div_ant_conf);
	}

307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
	ieee80211_wake_queues(sc->hw);

	return true;
}

static int ath_reset_internal(struct ath_softc *sc, struct ath9k_channel *hchan,
			      bool retry_tx)
{
	struct ath_hw *ah = sc->sc_ah;
	struct ath_common *common = ath9k_hw_common(ah);
	struct ath9k_hw_cal_data *caldata = NULL;
	bool fastcc = true;
	bool flush = false;
	int r;

	__ath_cancel_work(sc);

	spin_lock_bh(&sc->sc_pcu_lock);
325

326
327
328
329
330
331
332
333
334
335
336
	if (!(sc->sc_flags & SC_OP_OFFCHANNEL)) {
		fastcc = false;
		caldata = &sc->caldata;
	}

	if (!hchan) {
		fastcc = false;
		flush = true;
		hchan = ah->curchan;
	}

337
338
	if (fastcc && (ah->chip_fullsleep ||
	    !ath9k_hw_check_alive(ah)))
339
340
341
342
343
		fastcc = false;

	if (!ath_prepare_reset(sc, retry_tx, flush))
		fastcc = false;

344
	ath_dbg(common, CONFIG, "Reset to %u MHz, HT40: %d fastcc: %d\n",
345
		hchan->channel, IS_CHAN_HT40(hchan), fastcc);
346
347
348
349
350
351
352
353
354
355
356
357

	r = ath9k_hw_reset(ah, hchan, caldata, fastcc);
	if (r) {
		ath_err(common,
			"Unable to reset channel, reset status %d\n", r);
		goto out;
	}

	if (!ath_complete_reset(sc, true))
		r = -EIO;

out:
358
	spin_unlock_bh(&sc->sc_pcu_lock);
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
	return r;
}


/*
 * 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.
*/
static int ath_set_channel(struct ath_softc *sc, struct ieee80211_hw *hw,
		    struct ath9k_channel *hchan)
{
	int r;

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

	r = ath_reset_internal(sc, hchan, false);
377

378
	return r;
Sujith's avatar
Sujith committed
379
380
}

381
382
383
static void ath_paprd_activate(struct ath_softc *sc)
{
	struct ath_hw *ah = sc->sc_ah;
384
	struct ath9k_hw_cal_data *caldata = ah->caldata;
385
386
	int chain;

387
	if (!caldata || !caldata->paprd_done)
388
389
390
		return;

	ath9k_ps_wakeup(sc);
391
	ar9003_paprd_enable(ah, false);
392
	for (chain = 0; chain < AR9300_MAX_CHAINS; chain++) {
393
		if (!(ah->txchainmask & BIT(chain)))
394
395
			continue;

396
		ar9003_paprd_populate_single_table(ah, caldata, chain);
397
398
399
400
401
402
	}

	ar9003_paprd_enable(ah, true);
	ath9k_ps_restore(sc);
}

403
404
405
406
static bool ath_paprd_send_frame(struct ath_softc *sc, struct sk_buff *skb, int chain)
{
	struct ieee80211_hw *hw = sc->hw;
	struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
407
408
	struct ath_hw *ah = sc->sc_ah;
	struct ath_common *common = ath9k_hw_common(ah);
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
	struct ath_tx_control txctl;
	int time_left;

	memset(&txctl, 0, sizeof(txctl));
	txctl.txq = sc->tx.txq_map[WME_AC_BE];

	memset(tx_info, 0, sizeof(*tx_info));
	tx_info->band = hw->conf.channel->band;
	tx_info->flags |= IEEE80211_TX_CTL_NO_ACK;
	tx_info->control.rates[0].idx = 0;
	tx_info->control.rates[0].count = 1;
	tx_info->control.rates[0].flags = IEEE80211_TX_RC_MCS;
	tx_info->control.rates[1].idx = -1;

	init_completion(&sc->paprd_complete);
	txctl.paprd = BIT(chain);
425
426

	if (ath_tx_start(hw, skb, &txctl) != 0) {
427
		ath_dbg(common, CALIBRATE, "PAPRD TX failed\n");
428
		dev_kfree_skb_any(skb);
429
		return false;
430
	}
431
432
433
434
435

	time_left = wait_for_completion_timeout(&sc->paprd_complete,
			msecs_to_jiffies(ATH_PAPRD_TIMEOUT));

	if (!time_left)
436
		ath_dbg(common, CALIBRATE,
437
438
439
440
441
442
			"Timeout waiting for paprd training on TX chain %d\n",
			chain);

	return !!time_left;
}

443
444
445
446
447
448
449
void ath_paprd_calibrate(struct work_struct *work)
{
	struct ath_softc *sc = container_of(work, struct ath_softc, paprd_work);
	struct ieee80211_hw *hw = sc->hw;
	struct ath_hw *ah = sc->sc_ah;
	struct ieee80211_hdr *hdr;
	struct sk_buff *skb = NULL;
450
	struct ath9k_hw_cal_data *caldata = ah->caldata;
451
	struct ath_common *common = ath9k_hw_common(ah);
452
	int ftype;
453
454
455
456
	int chain_ok = 0;
	int chain;
	int len = 1800;

457
458
459
	if (!caldata)
		return;

460
461
	ath9k_ps_wakeup(sc);

462
	if (ar9003_paprd_init_table(ah) < 0)
463
		goto fail_paprd;
464

465
466
	skb = alloc_skb(len, GFP_KERNEL);
	if (!skb)
467
		goto fail_paprd;
468
469
470
471
472
473

	skb_put(skb, len);
	memset(skb->data, 0, len);
	hdr = (struct ieee80211_hdr *)skb->data;
	ftype = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC;
	hdr->frame_control = cpu_to_le16(ftype);
474
	hdr->duration_id = cpu_to_le16(10);
475
476
477
478
479
	memcpy(hdr->addr1, hw->wiphy->perm_addr, ETH_ALEN);
	memcpy(hdr->addr2, hw->wiphy->perm_addr, ETH_ALEN);
	memcpy(hdr->addr3, hw->wiphy->perm_addr, ETH_ALEN);

	for (chain = 0; chain < AR9300_MAX_CHAINS; chain++) {
480
		if (!(ah->txchainmask & BIT(chain)))
481
482
483
484
			continue;

		chain_ok = 0;

485
486
487
		ath_dbg(common, CALIBRATE,
			"Sending PAPRD frame for thermal measurement on chain %d\n",
			chain);
488
489
		if (!ath_paprd_send_frame(sc, skb, chain))
			goto fail_paprd;
490
491
492

		ar9003_paprd_setup_gain_table(ah, chain);

493
		ath_dbg(common, CALIBRATE,
494
495
			"Sending PAPRD training frame on chain %d\n", chain);
		if (!ath_paprd_send_frame(sc, skb, chain))
496
			goto fail_paprd;
497

498
		if (!ar9003_paprd_is_done(ah)) {
499
			ath_dbg(common, CALIBRATE,
500
				"PAPRD not yet done on chain %d\n", chain);
501
			break;
502
		}
503

504
		if (ar9003_paprd_create_curve(ah, caldata, chain)) {
505
			ath_dbg(common, CALIBRATE,
506
507
				"PAPRD create curve failed on chain %d\n",
								   chain);
508
			break;
509
		}
510
511
512
513
514
515

		chain_ok = 1;
	}
	kfree_skb(skb);

	if (chain_ok) {
516
		caldata->paprd_done = true;
517
518
519
		ath_paprd_activate(sc);
	}

520
fail_paprd:
521
522
523
	ath9k_ps_restore(sc);
}

Sujith's avatar
Sujith committed
524
525
526
527
528
529
530
/*
 *  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.
 */
Sujith's avatar
Sujith committed
531
void ath_ani_calibrate(unsigned long data)
Sujith's avatar
Sujith committed
532
{
533
534
	struct ath_softc *sc = (struct ath_softc *)data;
	struct ath_hw *ah = sc->sc_ah;
535
	struct ath_common *common = ath9k_hw_common(ah);
Sujith's avatar
Sujith committed
536
537
538
539
	bool longcal = false;
	bool shortcal = false;
	bool aniflag = false;
	unsigned int timestamp = jiffies_to_msecs(jiffies);
540
	u32 cal_interval, short_cal_interval, long_cal_interval;
541
	unsigned long flags;
542
543
544
545
546

	if (ah->caldata && ah->caldata->nfcal_interference)
		long_cal_interval = ATH_LONG_CALINTERVAL_INT;
	else
		long_cal_interval = ATH_LONG_CALINTERVAL;
Sujith's avatar
Sujith committed
547

548
549
	short_cal_interval = (ah->opmode == NL80211_IFTYPE_AP) ?
		ATH_AP_SHORT_CALINTERVAL : ATH_STA_SHORT_CALINTERVAL;
Sujith's avatar
Sujith committed
550

551
552
553
554
555
556
	/* 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
557
	/* Long calibration runs independently of short calibration. */
558
	if ((timestamp - common->ani.longcal_timer) >= long_cal_interval) {
Sujith's avatar
Sujith committed
559
		longcal = true;
560
		common->ani.longcal_timer = timestamp;
Sujith's avatar
Sujith committed
561
562
	}

Sujith's avatar
Sujith committed
563
	/* Short calibration applies only while caldone is false */
564
565
	if (!common->ani.caldone) {
		if ((timestamp - common->ani.shortcal_timer) >= short_cal_interval) {
Sujith's avatar
Sujith committed
566
			shortcal = true;
567
568
			common->ani.shortcal_timer = timestamp;
			common->ani.resetcal_timer = timestamp;
Sujith's avatar
Sujith committed
569
570
		}
	} else {
571
		if ((timestamp - common->ani.resetcal_timer) >=
Sujith's avatar
Sujith committed
572
		    ATH_RESTART_CALINTERVAL) {
573
574
575
			common->ani.caldone = ath9k_hw_reset_calvalid(ah);
			if (common->ani.caldone)
				common->ani.resetcal_timer = timestamp;
Sujith's avatar
Sujith committed
576
577
578
579
		}
	}

	/* Verify whether we must check ANI */
580
581
582
	if (sc->sc_ah->config.enable_ani
	    && (timestamp - common->ani.checkani_timer) >=
	    ah->config.ani_poll_interval) {
Sujith's avatar
Sujith committed
583
		aniflag = true;
584
		common->ani.checkani_timer = timestamp;
Sujith's avatar
Sujith committed
585
586
	}

587
588
589
590
591
592
593
	/* Call ANI routine if necessary */
	if (aniflag) {
		spin_lock_irqsave(&common->cc_lock, flags);
		ath9k_hw_ani_monitor(ah, ah->curchan);
		ath_update_survey_stats(sc);
		spin_unlock_irqrestore(&common->cc_lock, flags);
	}
Sujith's avatar
Sujith committed
594

595
596
597
598
	/* Perform calibration if necessary */
	if (longcal || shortcal) {
		common->ani.caldone =
			ath9k_hw_calibrate(ah, ah->curchan,
599
						ah->rxchainmask, longcal);
Sujith's avatar
Sujith committed
600
601
	}

602
603
604
	ath_dbg(common, ANI,
		"Calibration @%lu finished: %s %s %s, caldone: %s\n",
		jiffies,
605
606
607
		longcal ? "long" : "", shortcal ? "short" : "",
		aniflag ? "ani" : "", common->ani.caldone ? "true" : "false");

608
609
	ath9k_ps_restore(sc);

610
set_timer:
Sujith's avatar
Sujith committed
611
612
613
614
615
	/*
	* Set timer interval based on previous results.
	* The interval must be the shortest necessary to satisfy ANI,
	* short calibration and long calibration.
	*/
616
	ath9k_debug_samp_bb_mac(sc);
617
	cal_interval = ATH_LONG_CALINTERVAL;
618
	if (sc->sc_ah->config.enable_ani)
619
620
		cal_interval = min(cal_interval,
				   (u32)ah->config.ani_poll_interval);
621
	if (!common->ani.caldone)
622
		cal_interval = min(cal_interval, (u32)short_cal_interval);
Sujith's avatar
Sujith committed
623

624
	mod_timer(&common->ani.timer, jiffies + msecs_to_jiffies(cal_interval));
625
626
	if ((sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_PAPRD) && ah->caldata) {
		if (!ah->caldata->paprd_done)
627
			ieee80211_queue_work(sc->hw, &sc->paprd_work);
628
		else if (!ah->paprd_table_write_done)
629
630
			ath_paprd_activate(sc);
	}
Sujith's avatar
Sujith committed
631
632
}

633
634
static void ath_node_attach(struct ath_softc *sc, struct ieee80211_sta *sta,
			    struct ieee80211_vif *vif)
Sujith's avatar
Sujith committed
635
636
637
638
{
	struct ath_node *an;
	an = (struct ath_node *)sta->drv_priv;

639
640
641
642
#ifdef CONFIG_ATH9K_DEBUGFS
	spin_lock(&sc->nodes_lock);
	list_add(&an->list, &sc->nodes);
	spin_unlock(&sc->nodes_lock);
643
#endif
644
	an->sta = sta;
645
	an->vif = vif;
646
	if (sc->sc_flags & SC_OP_TXAGGR) {
Sujith's avatar
Sujith committed
647
		ath_tx_node_init(sc, an);
Sujith's avatar
Sujith committed
648
		an->maxampdu = 1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
649
650
651
				     sta->ht_cap.ampdu_factor);
		an->mpdudensity = parse_mpdudensity(sta->ht_cap.ampdu_density);
	}
Sujith's avatar
Sujith committed
652
653
654
655
656
657
}

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

658
659
660
661
662
663
664
#ifdef CONFIG_ATH9K_DEBUGFS
	spin_lock(&sc->nodes_lock);
	list_del(&an->list);
	spin_unlock(&sc->nodes_lock);
	an->sta = NULL;
#endif

Sujith's avatar
Sujith committed
665
666
667
668
	if (sc->sc_flags & SC_OP_TXAGGR)
		ath_tx_node_cleanup(sc, an);
}

669

Sujith's avatar
Sujith committed
670
void ath9k_tasklet(unsigned long data)
Sujith's avatar
Sujith committed
671
672
{
	struct ath_softc *sc = (struct ath_softc *)data;
673
	struct ath_hw *ah = sc->sc_ah;
674
	struct ath_common *common = ath9k_hw_common(ah);
675

Sujith's avatar
Sujith committed
676
	u32 status = sc->intrstatus;
Felix Fietkau's avatar
Felix Fietkau committed
677
	u32 rxmask;
Sujith's avatar
Sujith committed
678

679
680
681
	ath9k_ps_wakeup(sc);
	spin_lock(&sc->sc_pcu_lock);

682
683
	if ((status & ATH9K_INT_FATAL) ||
	    (status & ATH9K_INT_BB_WATCHDOG)) {
684
685
686
687
688
689
690
691
692
693
#ifdef CONFIG_ATH9K_DEBUGFS
		enum ath_reset_type type;

		if (status & ATH9K_INT_FATAL)
			type = RESET_TYPE_FATAL_INT;
		else
			type = RESET_TYPE_BB_WATCHDOG;

		RESET_STAT_INC(sc, type);
#endif
694
		ieee80211_queue_work(sc->hw, &sc->hw_reset_work);
695
		goto out;
696
	}
Sujith's avatar
Sujith committed
697

698
699
700
701
702
703
704
705
706
	/*
	 * Only run the baseband hang check if beacons stop working in AP or
	 * IBSS mode, because it has a high false positive rate. For station
	 * mode it should not be necessary, since the upper layers will detect
	 * this through a beacon miss automatically and the following channel
	 * change will trigger a hardware reset anyway
	 */
	if (ath9k_hw_numtxpending(ah, sc->beacon.beaconq) != 0 &&
	    !ath9k_hw_check_alive(ah))
707
708
		ieee80211_queue_work(sc->hw, &sc->hw_check_work);

709
710
711
712
713
	if ((status & ATH9K_INT_TSFOOR) && sc->ps_enabled) {
		/*
		 * TSF sync does not look correct; remain awake to sync with
		 * the next Beacon.
		 */
714
		ath_dbg(common, PS, "TSFOOR - Sync with next Beacon\n");
715
		sc->ps_flags |= PS_WAIT_FOR_BEACON | PS_BEACON_SYNC;
716
717
	}

Felix Fietkau's avatar
Felix Fietkau committed
718
719
720
721
722
723
724
725
726
727
728
729
730
	if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
		rxmask = (ATH9K_INT_RXHP | ATH9K_INT_RXLP | ATH9K_INT_RXEOL |
			  ATH9K_INT_RXORN);
	else
		rxmask = (ATH9K_INT_RX | ATH9K_INT_RXEOL | ATH9K_INT_RXORN);

	if (status & rxmask) {
		/* Check for high priority Rx first */
		if ((ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) &&
		    (status & ATH9K_INT_RXHP))
			ath_rx_tasklet(sc, 0, true);

		ath_rx_tasklet(sc, 0, false);
Sujith's avatar
Sujith committed
731
732
	}

733
734
735
736
737
738
	if (status & ATH9K_INT_TX) {
		if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
			ath_tx_edma_tasklet(sc);
		else
			ath_tx_tasklet(sc);
	}
739

740
	ath9k_btcoex_handle_interrupt(sc, status);
741

742
out:
Sujith's avatar
Sujith committed
743
	/* re-enable hardware interrupt */
744
	ath9k_hw_enable_interrupts(ah);
745

746
	spin_unlock(&sc->sc_pcu_lock);
747
	ath9k_ps_restore(sc);
Sujith's avatar
Sujith committed
748
749
}

750
irqreturn_t ath_isr(int irq, void *dev)
Sujith's avatar
Sujith committed
751
{
752
753
#define SCHED_INTR (				\
		ATH9K_INT_FATAL |		\
754
		ATH9K_INT_BB_WATCHDOG |		\
755
756
757
		ATH9K_INT_RXORN |		\
		ATH9K_INT_RXEOL |		\
		ATH9K_INT_RX |			\
Felix Fietkau's avatar
Felix Fietkau committed
758
759
		ATH9K_INT_RXLP |		\
		ATH9K_INT_RXHP |		\
760
761
762
		ATH9K_INT_TX |			\
		ATH9K_INT_BMISS |		\
		ATH9K_INT_CST |			\
763
		ATH9K_INT_TSFOOR |		\
764
765
		ATH9K_INT_GENTIMER |		\
		ATH9K_INT_MCI)
766

Sujith's avatar
Sujith committed
767
	struct ath_softc *sc = dev;
768
	struct ath_hw *ah = sc->sc_ah;
769
	struct ath_common *common = ath9k_hw_common(ah);
Sujith's avatar
Sujith committed
770
771
772
	enum ath9k_int status;
	bool sched = false;

773
774
775
776
777
778
779
	/*
	 * 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
780

781
782
783

	/* shared irq, not for us */

784
	if (!ath9k_hw_intrpend(ah))
785
786
787
788
789
790
791
792
793
		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 */
Pavel Roskin's avatar
Pavel Roskin committed
794
	status &= ah->imask;	/* discard unasked-for bits */
Sujith's avatar
Sujith committed
795

796
797
798
799
	/*
	 * If there are no status bits set, then this interrupt was not
	 * for me (should have been caught above).
	 */
800
	if (!status)
801
		return IRQ_NONE;
Sujith's avatar
Sujith committed
802

803
804
805
806
807
808
809
810
811
812
	/* 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.
	 */
Felix Fietkau's avatar
Felix Fietkau committed
813
814
	if ((status & ATH9K_INT_FATAL) || ((status & ATH9K_INT_RXORN) &&
	    !(ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)))
815
816
		goto chip_reset;

817
818
	if ((ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) &&
	    (status & ATH9K_INT_BB_WATCHDOG)) {
819
820
821

		spin_lock(&common->cc_lock);
		ath_hw_cycle_counters_update(common);
822
		ar9003_hw_bb_watchdog_dbg_info(ah);
823
824
		spin_unlock(&common->cc_lock);

825
826
827
		goto chip_reset;
	}

828
829
830
831
832
833
	if (status & ATH9K_INT_SWBA)
		tasklet_schedule(&sc->bcon_tasklet);

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

834
835
	if (status & ATH9K_INT_RXEOL) {
		ah->imask &= ~(ATH9K_INT_RXEOL | ATH9K_INT_RXORN);
836
		ath9k_hw_set_interrupts(ah);
Felix Fietkau's avatar
Felix Fietkau committed
837
838
	}

839
	if (status & ATH9K_INT_MIB) {
Sujith's avatar
Sujith committed
840
		/*
841
842
843
		 * Disable interrupts until we service the MIB
		 * interrupt; otherwise it will continue to
		 * fire.
Sujith's avatar
Sujith committed
844
		 */
845
		ath9k_hw_disable_interrupts(ah);
846
847
848
849
850
		/*
		 * Let the hal handle the event. We assume
		 * it will clear whatever condition caused
		 * the interrupt.
		 */
851
		spin_lock(&common->cc_lock);
852
		ath9k_hw_proc_mib_event(ah);
853
		spin_unlock(&common->cc_lock);
854
		ath9k_hw_enable_interrupts(ah);
855
	}
Sujith's avatar
Sujith committed
856

857
858
	if (!(ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP))
		if (status & ATH9K_INT_TIM_TIMER) {
859
860
			if (ATH_DBG_WARN_ON_ONCE(sc->ps_idle))
				goto chip_reset;
861
862
			/* Clear RxAbort bit so that we can
			 * receive frames */
863
			ath9k_setpower(sc, ATH9K_PM_AWAKE);
864
			ath9k_hw_setrxabort(sc->sc_ah, 0);
Sujith's avatar
Sujith committed
865
			sc->ps_flags |= PS_WAIT_FOR_BEACON;
Sujith's avatar
Sujith committed
866
		}
867
868

chip_reset:
Sujith's avatar
Sujith committed
869

870
871
	ath_debug_stat_interrupt(sc, status);

Sujith's avatar
Sujith committed
872
	if (sched) {
873
874
		/* turn off every interrupt */
		ath9k_hw_disable_interrupts(ah);
Sujith's avatar
Sujith committed
875
876
877
878
		tasklet_schedule(&sc->intr_tq);
	}

	return IRQ_HANDLED;
879
880

#undef SCHED_INTR
Sujith's avatar
Sujith committed
881
882
}

883
static int ath_reset(struct ath_softc *sc, bool retry_tx)
Sujith's avatar
Sujith committed
884
{
885
	int r;
Sujith's avatar
Sujith committed
886

887
	ath9k_ps_wakeup(sc);
888

889
	r = ath_reset_internal(sc, NULL, retry_tx);
Sujith's avatar
Sujith committed
890
891
892
893
894

	if (retry_tx) {
		int i;
		for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
			if (ATH_TXQ_SETUP(sc, i)) {
Sujith's avatar
Sujith committed
895
896
897
				spin_lock_bh(&sc->tx.txq[i].axq_lock);
				ath_txq_schedule(sc, &sc->tx.txq[i]);
				spin_unlock_bh(&sc->tx.txq[i].axq_lock);
Sujith's avatar
Sujith committed
898
899
900
901
			}
		}
	}

902
	ath9k_ps_restore(sc);
Sujith's avatar
Sujith committed
903

904
	return r;
Sujith's avatar
Sujith committed
905
906
}

907
908
909
910
911
912
913
void ath_reset_work(struct work_struct *work)
{
	struct ath_softc *sc = container_of(work, struct ath_softc, hw_reset_work);

	ath_reset(sc, true);
}

914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
void ath_hw_check(struct work_struct *work)
{
	struct ath_softc *sc = container_of(work, struct ath_softc, hw_check_work);
	struct ath_common *common = ath9k_hw_common(sc->sc_ah);
	unsigned long flags;
	int busy;

	ath9k_ps_wakeup(sc);
	if (ath9k_hw_check_alive(sc->sc_ah))
		goto out;

	spin_lock_irqsave(&common->cc_lock, flags);
	busy = ath_update_survey_stats(sc);
	spin_unlock_irqrestore(&common->cc_lock, flags);

929
930
	ath_dbg(common, RESET, "Possible baseband hang, busy=%d (try %d)\n",
		busy, sc->hw_busy_count + 1);
931
	if (busy >= 99) {
932
933
		if (++sc->hw_busy_count >= 3) {
			RESET_STAT_INC(sc, RESET_TYPE_BB_HANG);
934
			ieee80211_queue_work(sc->hw, &sc->hw_reset_work);
935
		}
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952

	} else if (busy >= 0)
		sc->hw_busy_count = 0;

out:
	ath9k_ps_restore(sc);
}

static void ath_hw_pll_rx_hang_check(struct ath_softc *sc, u32 pll_sqsum)
{
	static int count;
	struct ath_common *common = ath9k_hw_common(sc->sc_ah);

	if (pll_sqsum >= 0x40000) {
		count++;
		if (count == 3) {
			/* Rx is hung for more than 500ms. Reset it */
953
			ath_dbg(common, RESET, "Possible RX hang, resetting\n");
954
			RESET_STAT_INC(sc, RESET_TYPE_PLL_HANG);
955
			ieee80211_queue_work(sc->hw, &sc->hw_reset_work);
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
			count = 0;
		}
	} else
		count = 0;
}

void ath_hw_pll_work(struct work_struct *work)
{
	struct ath_softc *sc = container_of(work, struct ath_softc,
					    hw_pll_work.work);
	u32 pll_sqsum;

	if (AR_SREV_9485(sc->sc_ah)) {

		ath9k_ps_wakeup(sc);
		pll_sqsum = ar9003_get_pll_sqsum_dvc(sc->sc_ah);
		ath9k_ps_restore(sc);

		ath_hw_pll_rx_hang_check(sc, pll_sqsum);

		ieee80211_queue_delayed_work(sc->hw, &sc->hw_pll_work, HZ/5);
	}
}

Sujith's avatar
Sujith committed
980
981
982
983
/**********************/
/* mac80211 callbacks */
/**********************/

984
static int ath9k_start(struct ieee80211_hw *hw)
985
{
986
	struct ath_softc *sc = hw->priv;
987
	struct ath_hw *ah = sc->sc_ah;
988
	struct ath_common *common = ath9k_hw_common(ah);
989
	struct ieee80211_channel *curchan = hw->conf.channel;
Sujith's avatar
Sujith committed
990
	struct ath9k_channel *init_channel;
991
	int r;
992

993
	ath_dbg(common, CONFIG,
994
995
		"Starting driver with initial channel: %d MHz\n",
		curchan->center_freq);
996

997
998
	ath9k_ps_wakeup(sc);

999
1000
	mutex_lock(&sc->mutex);

1001
	/* setup initial channel */
1002
	sc->chan_idx = curchan->hw_value;
1003

1004
	init_channel = ath9k_cmn_get_curchannel(hw, ah);
Sujith's avatar
Sujith committed
1005
1006

	/* Reset SERDES registers */
1007
	ath9k_hw_configpcipowersave(ah, false);
Sujith's avatar
Sujith committed
1008
1009
1010
1011
1012
1013
1014
1015

	/*
	 * The basic interface to setting the hardware in a good
	 * state is ``reset''.  On return the hardware is known to
	 * be powered up and with interrupts disabled.  This must
	 * be followed by initialization of the appropriate bits
	 * and then setup of the interrupt mask.
	 */
1016
	spin_lock_bh(&sc->sc_pcu_lock);
1017
1018
1019

	atomic_set(&ah->intr_ref_cnt, -1);

1020
	r = ath9k_hw_reset(ah, init_channel, ah->caldata, false);
1021
	if (r) {
1022
1023
1024
		ath_err(common,
			"Unable to reset hardware; reset status %d (freq %u MHz)\n",
			r, curchan->center_freq);
1025
		spin_unlock_bh(&sc->sc_pcu_lock);
1026
		goto mutex_unlock;
Sujith's avatar
Sujith committed
1027
1028
1029
	}

	/* Setup our intr mask. */
Felix Fietkau's avatar
Felix Fietkau committed
1030
1031
1032
1033
1034
	ah->imask = ATH9K_INT_TX | ATH9K_INT_RXEOL |
		    ATH9K_INT_RXORN | ATH9K_INT_FATAL |
		    ATH9K_INT_GLOBAL;

	if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
1035
1036
1037
		ah->imask |= ATH9K_INT_RXHP |
			     ATH9K_INT_RXLP |
			     ATH9K_INT_BB_WATCHDOG;
Felix Fietkau's avatar
Felix Fietkau committed
1038
1039
	else
		ah->imask |= ATH9K_INT_RX;
Sujith's avatar
Sujith committed
1040

1041
	ah->imask |= ATH9K_INT_GTT;
Sujith's avatar
Sujith committed
1042

1043
	if (ah->caps.hw_caps & ATH9K_HW_CAP_HT)
Pavel Roskin's avatar
Pavel Roskin committed
1044
		ah->imask |= ATH9K_INT_CST;
Sujith's avatar
Sujith committed
1045

1046
1047
1048
	if (ah->caps.hw_caps & ATH9K_HW_CAP_MCI)
		ah->imask |= ATH9K_INT_MCI;

Sujith's avatar
Sujith committed
1049
	sc->sc_flags &= ~SC_OP_INVALID;
1050
	sc->sc_ah->is_monitoring = false;
Sujith's avatar
Sujith committed
1051
1052

	/* Disable BMISS interrupt when we're not associated */
Pavel Roskin's avatar
Pavel Roskin committed
1053
	ah->imask &= ~(ATH9K_INT_SWBA | ATH9K_INT_BMISS);
Sujith's avatar
Sujith committed
1054

1055
1056
1057
1058
1059
	if (!ath_complete_reset(sc, false)) {
		r = -EIO;
		spin_unlock_bh(&sc->sc_pcu_lock);
		goto mutex_unlock;
	}
Sujith's avatar
Sujith committed
1060

Felix Fietkau's avatar