blob: de059c38467d54320ffa6030a17e6f31cd6c82e2 [file] [log] [blame]
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001/*
2 * Copyright (c) 2008 Atheros Communications Inc.
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070017#include <linux/nl80211.h>
18#include "core.h"
Benoit PAPILLAULT392dff82008-11-06 22:26:49 +010019#include "reg.h"
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070020
21#define ATH_PCI_VERSION "0.1"
22
Luis R. Rodriguezf078f202008-08-04 00:16:41 -070023static char *dev_info = "ath9k";
24
25MODULE_AUTHOR("Atheros Communications");
26MODULE_DESCRIPTION("Support for Atheros 802.11n wireless LAN cards.");
27MODULE_SUPPORTED_DEVICE("Atheros 802.11n WLAN cards");
28MODULE_LICENSE("Dual BSD/GPL");
29
30static struct pci_device_id ath_pci_id_table[] __devinitdata = {
31 { PCI_VDEVICE(ATHEROS, 0x0023) }, /* PCI */
32 { PCI_VDEVICE(ATHEROS, 0x0024) }, /* PCI-E */
33 { PCI_VDEVICE(ATHEROS, 0x0027) }, /* PCI */
34 { PCI_VDEVICE(ATHEROS, 0x0029) }, /* PCI */
35 { PCI_VDEVICE(ATHEROS, 0x002A) }, /* PCI-E */
36 { 0 }
37};
38
Sujith9757d552008-11-04 18:25:27 +053039static void ath_detach(struct ath_softc *sc);
40
Sujith04bd4632008-11-28 22:18:05 +053041void DPRINTF(struct ath_softc *sc, int dbg_mask, const char *fmt, ...)
42{
43 if (!sc)
44 return;
45
46 if (sc->sc_debug & dbg_mask) {
47 va_list args;
48
49 va_start(args, fmt);
50 printk(KERN_DEBUG "ath9k: ");
51 vprintk(fmt, args);
52 va_end(args);
53 }
54}
55
Sujithff37e332008-11-24 12:07:55 +053056/* return bus cachesize in 4B word units */
57
58static void bus_read_cachesize(struct ath_softc *sc, int *csz)
59{
60 u8 u8tmp;
61
62 pci_read_config_byte(sc->pdev, PCI_CACHE_LINE_SIZE, (u8 *)&u8tmp);
63 *csz = (int)u8tmp;
64
65 /*
66 * This check was put in to avoid "unplesant" consequences if
67 * the bootrom has not fully initialized all PCI devices.
68 * Sometimes the cache line size register is not set
69 */
70
71 if (*csz == 0)
72 *csz = DEFAULT_CACHELINE >> 2; /* Use the default size */
73}
74
75static void ath_setcurmode(struct ath_softc *sc, enum wireless_mode mode)
76{
77 sc->sc_curmode = mode;
78 /*
79 * All protection frames are transmited at 2Mb/s for
80 * 11g, otherwise at 1Mb/s.
81 * XXX select protection rate index from rate table.
82 */
83 sc->sc_protrix = (mode == ATH9K_MODE_11G ? 1 : 0);
84}
85
86static enum wireless_mode ath_chan2mode(struct ath9k_channel *chan)
87{
88 if (chan->chanmode == CHANNEL_A)
89 return ATH9K_MODE_11A;
90 else if (chan->chanmode == CHANNEL_G)
91 return ATH9K_MODE_11G;
92 else if (chan->chanmode == CHANNEL_B)
93 return ATH9K_MODE_11B;
94 else if (chan->chanmode == CHANNEL_A_HT20)
95 return ATH9K_MODE_11NA_HT20;
96 else if (chan->chanmode == CHANNEL_G_HT20)
97 return ATH9K_MODE_11NG_HT20;
98 else if (chan->chanmode == CHANNEL_A_HT40PLUS)
99 return ATH9K_MODE_11NA_HT40PLUS;
100 else if (chan->chanmode == CHANNEL_A_HT40MINUS)
101 return ATH9K_MODE_11NA_HT40MINUS;
102 else if (chan->chanmode == CHANNEL_G_HT40PLUS)
103 return ATH9K_MODE_11NG_HT40PLUS;
104 else if (chan->chanmode == CHANNEL_G_HT40MINUS)
105 return ATH9K_MODE_11NG_HT40MINUS;
106
107 WARN_ON(1); /* should not get here */
108
109 return ATH9K_MODE_11B;
110}
111
112static void ath_update_txpow(struct ath_softc *sc)
113{
114 struct ath_hal *ah = sc->sc_ah;
115 u32 txpow;
116
117 if (sc->sc_curtxpow != sc->sc_config.txpowlimit) {
118 ath9k_hw_set_txpowerlimit(ah, sc->sc_config.txpowlimit);
119 /* read back in case value is clamped */
120 ath9k_hw_getcapability(ah, ATH9K_CAP_TXPOW, 1, &txpow);
121 sc->sc_curtxpow = txpow;
122 }
123}
124
125static u8 parse_mpdudensity(u8 mpdudensity)
126{
127 /*
128 * 802.11n D2.0 defined values for "Minimum MPDU Start Spacing":
129 * 0 for no restriction
130 * 1 for 1/4 us
131 * 2 for 1/2 us
132 * 3 for 1 us
133 * 4 for 2 us
134 * 5 for 4 us
135 * 6 for 8 us
136 * 7 for 16 us
137 */
138 switch (mpdudensity) {
139 case 0:
140 return 0;
141 case 1:
142 case 2:
143 case 3:
144 /* Our lower layer calculations limit our precision to
145 1 microsecond */
146 return 1;
147 case 4:
148 return 2;
149 case 5:
150 return 4;
151 case 6:
152 return 8;
153 case 7:
154 return 16;
155 default:
156 return 0;
157 }
158}
159
160static void ath_setup_rates(struct ath_softc *sc, enum ieee80211_band band)
161{
162 struct ath_rate_table *rate_table = NULL;
163 struct ieee80211_supported_band *sband;
164 struct ieee80211_rate *rate;
165 int i, maxrates;
166
167 switch (band) {
168 case IEEE80211_BAND_2GHZ:
169 rate_table = sc->hw_rate_table[ATH9K_MODE_11G];
170 break;
171 case IEEE80211_BAND_5GHZ:
172 rate_table = sc->hw_rate_table[ATH9K_MODE_11A];
173 break;
174 default:
175 break;
176 }
177
178 if (rate_table == NULL)
179 return;
180
181 sband = &sc->sbands[band];
182 rate = sc->rates[band];
183
184 if (rate_table->rate_cnt > ATH_RATE_MAX)
185 maxrates = ATH_RATE_MAX;
186 else
187 maxrates = rate_table->rate_cnt;
188
189 for (i = 0; i < maxrates; i++) {
190 rate[i].bitrate = rate_table->info[i].ratekbps / 100;
191 rate[i].hw_value = rate_table->info[i].ratecode;
192 sband->n_bitrates++;
Sujith04bd4632008-11-28 22:18:05 +0530193 DPRINTF(sc, ATH_DBG_CONFIG, "Rate: %2dMbps, ratecode: %2d\n",
194 rate[i].bitrate / 10, rate[i].hw_value);
Sujithff37e332008-11-24 12:07:55 +0530195 }
196}
197
198static int ath_setup_channels(struct ath_softc *sc)
199{
200 struct ath_hal *ah = sc->sc_ah;
201 int nchan, i, a = 0, b = 0;
202 u8 regclassids[ATH_REGCLASSIDS_MAX];
203 u32 nregclass = 0;
204 struct ieee80211_supported_band *band_2ghz;
205 struct ieee80211_supported_band *band_5ghz;
206 struct ieee80211_channel *chan_2ghz;
207 struct ieee80211_channel *chan_5ghz;
208 struct ath9k_channel *c;
209
210 /* Fill in ah->ah_channels */
211 if (!ath9k_regd_init_channels(ah, ATH_CHAN_MAX, (u32 *)&nchan,
212 regclassids, ATH_REGCLASSIDS_MAX,
213 &nregclass, CTRY_DEFAULT, false, 1)) {
214 u32 rd = ah->ah_currentRD;
215 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd4632008-11-28 22:18:05 +0530216 "Unable to collect channel list; "
Sujithff37e332008-11-24 12:07:55 +0530217 "regdomain likely %u country code %u\n",
Sujith04bd4632008-11-28 22:18:05 +0530218 rd, CTRY_DEFAULT);
Sujithff37e332008-11-24 12:07:55 +0530219 return -EINVAL;
220 }
221
222 band_2ghz = &sc->sbands[IEEE80211_BAND_2GHZ];
223 band_5ghz = &sc->sbands[IEEE80211_BAND_5GHZ];
224 chan_2ghz = sc->channels[IEEE80211_BAND_2GHZ];
225 chan_5ghz = sc->channels[IEEE80211_BAND_5GHZ];
226
227 for (i = 0; i < nchan; i++) {
228 c = &ah->ah_channels[i];
229 if (IS_CHAN_2GHZ(c)) {
230 chan_2ghz[a].band = IEEE80211_BAND_2GHZ;
231 chan_2ghz[a].center_freq = c->channel;
232 chan_2ghz[a].max_power = c->maxTxPower;
233
234 if (c->privFlags & CHANNEL_DISALLOW_ADHOC)
235 chan_2ghz[a].flags |= IEEE80211_CHAN_NO_IBSS;
236 if (c->channelFlags & CHANNEL_PASSIVE)
237 chan_2ghz[a].flags |= IEEE80211_CHAN_PASSIVE_SCAN;
238
239 band_2ghz->n_channels = ++a;
240
Sujith04bd4632008-11-28 22:18:05 +0530241 DPRINTF(sc, ATH_DBG_CONFIG, "2MHz channel: %d, "
Sujithff37e332008-11-24 12:07:55 +0530242 "channelFlags: 0x%x\n",
Sujith04bd4632008-11-28 22:18:05 +0530243 c->channel, c->channelFlags);
Sujithff37e332008-11-24 12:07:55 +0530244 } else if (IS_CHAN_5GHZ(c)) {
245 chan_5ghz[b].band = IEEE80211_BAND_5GHZ;
246 chan_5ghz[b].center_freq = c->channel;
247 chan_5ghz[b].max_power = c->maxTxPower;
248
249 if (c->privFlags & CHANNEL_DISALLOW_ADHOC)
250 chan_5ghz[b].flags |= IEEE80211_CHAN_NO_IBSS;
251 if (c->channelFlags & CHANNEL_PASSIVE)
252 chan_5ghz[b].flags |= IEEE80211_CHAN_PASSIVE_SCAN;
253
254 band_5ghz->n_channels = ++b;
255
Sujith04bd4632008-11-28 22:18:05 +0530256 DPRINTF(sc, ATH_DBG_CONFIG, "5MHz channel: %d, "
Sujithff37e332008-11-24 12:07:55 +0530257 "channelFlags: 0x%x\n",
Sujith04bd4632008-11-28 22:18:05 +0530258 c->channel, c->channelFlags);
Sujithff37e332008-11-24 12:07:55 +0530259 }
260 }
261
262 return 0;
263}
264
265/*
266 * Set/change channels. If the channel is really being changed, it's done
267 * by reseting the chip. To accomplish this we must first cleanup any pending
268 * DMA, then restart stuff.
269*/
270static int ath_set_channel(struct ath_softc *sc, struct ath9k_channel *hchan)
271{
272 struct ath_hal *ah = sc->sc_ah;
273 bool fastcc = true, stopped;
274
275 if (sc->sc_flags & SC_OP_INVALID)
276 return -EIO;
277
Sujithff37e332008-11-24 12:07:55 +0530278 if (hchan->channel != sc->sc_ah->ah_curchan->channel ||
279 hchan->channelFlags != sc->sc_ah->ah_curchan->channelFlags ||
280 (sc->sc_flags & SC_OP_CHAINMASK_UPDATE) ||
281 (sc->sc_flags & SC_OP_FULL_RESET)) {
282 int status;
283 /*
284 * This is only performed if the channel settings have
285 * actually changed.
286 *
287 * To switch channels clear any pending DMA operations;
288 * wait long enough for the RX fifo to drain, reset the
289 * hardware at the new frequency, and then re-enable
290 * the relevant bits of the h/w.
291 */
Sujith04bd4632008-11-28 22:18:05 +0530292 ath9k_hw_set_interrupts(ah, 0);
293 ath_draintxq(sc, false);
294 stopped = ath_stoprecv(sc);
Sujithff37e332008-11-24 12:07:55 +0530295
296 /* XXX: do not flush receive queue here. We don't want
297 * to flush data frames already in queue because of
298 * changing channel. */
299
300 if (!stopped || (sc->sc_flags & SC_OP_FULL_RESET))
301 fastcc = false;
302
Sujith99405f92008-11-24 12:08:35 +0530303 DPRINTF(sc, ATH_DBG_CONFIG,
Sujith04bd4632008-11-28 22:18:05 +0530304 "(%u MHz) -> (%u MHz), cflags:%x, chanwidth: %d\n",
Sujith99405f92008-11-24 12:08:35 +0530305 sc->sc_ah->ah_curchan->channel,
306 hchan->channel, hchan->channelFlags, sc->tx_chan_width);
307
Sujithff37e332008-11-24 12:07:55 +0530308 spin_lock_bh(&sc->sc_resetlock);
Sujith99405f92008-11-24 12:08:35 +0530309 if (!ath9k_hw_reset(ah, hchan, sc->tx_chan_width,
Sujithff37e332008-11-24 12:07:55 +0530310 sc->sc_tx_chainmask, sc->sc_rx_chainmask,
311 sc->sc_ht_extprotspacing, fastcc, &status)) {
312 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd4632008-11-28 22:18:05 +0530313 "Unable to reset channel %u (%uMhz) "
314 "flags 0x%x hal status %u\n",
Sujithff37e332008-11-24 12:07:55 +0530315 ath9k_hw_mhz2ieee(ah, hchan->channel,
316 hchan->channelFlags),
317 hchan->channel, hchan->channelFlags, status);
318 spin_unlock_bh(&sc->sc_resetlock);
319 return -EIO;
320 }
321 spin_unlock_bh(&sc->sc_resetlock);
322
323 sc->sc_flags &= ~SC_OP_CHAINMASK_UPDATE;
324 sc->sc_flags &= ~SC_OP_FULL_RESET;
325
326 if (ath_startrecv(sc) != 0) {
327 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd4632008-11-28 22:18:05 +0530328 "Unable to restart recv logic\n");
Sujithff37e332008-11-24 12:07:55 +0530329 return -EIO;
330 }
331
332 ath_setcurmode(sc, ath_chan2mode(hchan));
333 ath_update_txpow(sc);
334 ath9k_hw_set_interrupts(ah, sc->sc_imask);
335 }
336 return 0;
337}
338
339/*
340 * This routine performs the periodic noise floor calibration function
341 * that is used to adjust and optimize the chip performance. This
342 * takes environmental changes (location, temperature) into account.
343 * When the task is complete, it reschedules itself depending on the
344 * appropriate interval that was calculated.
345 */
346static void ath_ani_calibrate(unsigned long data)
347{
348 struct ath_softc *sc;
349 struct ath_hal *ah;
350 bool longcal = false;
351 bool shortcal = false;
352 bool aniflag = false;
353 unsigned int timestamp = jiffies_to_msecs(jiffies);
354 u32 cal_interval;
355
356 sc = (struct ath_softc *)data;
357 ah = sc->sc_ah;
358
359 /*
360 * don't calibrate when we're scanning.
361 * we are most likely not on our home channel.
362 */
363 if (sc->rx_filter & FIF_BCN_PRBRESP_PROMISC)
364 return;
365
366 /* Long calibration runs independently of short calibration. */
367 if ((timestamp - sc->sc_ani.sc_longcal_timer) >= ATH_LONG_CALINTERVAL) {
368 longcal = true;
Sujith04bd4632008-11-28 22:18:05 +0530369 DPRINTF(sc, ATH_DBG_ANI, "longcal @%lu\n", jiffies);
Sujithff37e332008-11-24 12:07:55 +0530370 sc->sc_ani.sc_longcal_timer = timestamp;
371 }
372
373 /* Short calibration applies only while sc_caldone is false */
374 if (!sc->sc_ani.sc_caldone) {
375 if ((timestamp - sc->sc_ani.sc_shortcal_timer) >=
376 ATH_SHORT_CALINTERVAL) {
377 shortcal = true;
Sujith04bd4632008-11-28 22:18:05 +0530378 DPRINTF(sc, ATH_DBG_ANI, "shortcal @%lu\n", jiffies);
Sujithff37e332008-11-24 12:07:55 +0530379 sc->sc_ani.sc_shortcal_timer = timestamp;
380 sc->sc_ani.sc_resetcal_timer = timestamp;
381 }
382 } else {
383 if ((timestamp - sc->sc_ani.sc_resetcal_timer) >=
384 ATH_RESTART_CALINTERVAL) {
385 ath9k_hw_reset_calvalid(ah, ah->ah_curchan,
386 &sc->sc_ani.sc_caldone);
387 if (sc->sc_ani.sc_caldone)
388 sc->sc_ani.sc_resetcal_timer = timestamp;
389 }
390 }
391
392 /* Verify whether we must check ANI */
393 if ((timestamp - sc->sc_ani.sc_checkani_timer) >=
394 ATH_ANI_POLLINTERVAL) {
395 aniflag = true;
396 sc->sc_ani.sc_checkani_timer = timestamp;
397 }
398
399 /* Skip all processing if there's nothing to do. */
400 if (longcal || shortcal || aniflag) {
401 /* Call ANI routine if necessary */
402 if (aniflag)
403 ath9k_hw_ani_monitor(ah, &sc->sc_halstats,
404 ah->ah_curchan);
405
406 /* Perform calibration if necessary */
407 if (longcal || shortcal) {
408 bool iscaldone = false;
409
410 if (ath9k_hw_calibrate(ah, ah->ah_curchan,
411 sc->sc_rx_chainmask, longcal,
412 &iscaldone)) {
413 if (longcal)
414 sc->sc_ani.sc_noise_floor =
415 ath9k_hw_getchan_noise(ah,
416 ah->ah_curchan);
417
418 DPRINTF(sc, ATH_DBG_ANI,
Sujith04bd4632008-11-28 22:18:05 +0530419 "calibrate chan %u/%x nf: %d\n",
Sujithff37e332008-11-24 12:07:55 +0530420 ah->ah_curchan->channel,
421 ah->ah_curchan->channelFlags,
422 sc->sc_ani.sc_noise_floor);
423 } else {
424 DPRINTF(sc, ATH_DBG_ANY,
Sujith04bd4632008-11-28 22:18:05 +0530425 "calibrate chan %u/%x failed\n",
Sujithff37e332008-11-24 12:07:55 +0530426 ah->ah_curchan->channel,
427 ah->ah_curchan->channelFlags);
428 }
429 sc->sc_ani.sc_caldone = iscaldone;
430 }
431 }
432
433 /*
434 * Set timer interval based on previous results.
435 * The interval must be the shortest necessary to satisfy ANI,
436 * short calibration and long calibration.
437 */
438
439 cal_interval = ATH_ANI_POLLINTERVAL;
440 if (!sc->sc_ani.sc_caldone)
441 cal_interval = min(cal_interval, (u32)ATH_SHORT_CALINTERVAL);
442
443 mod_timer(&sc->sc_ani.timer, jiffies + msecs_to_jiffies(cal_interval));
444}
445
446/*
447 * Update tx/rx chainmask. For legacy association,
448 * hard code chainmask to 1x1, for 11n association, use
449 * the chainmask configuration.
450 */
451static void ath_update_chainmask(struct ath_softc *sc, int is_ht)
452{
453 sc->sc_flags |= SC_OP_CHAINMASK_UPDATE;
454 if (is_ht) {
455 sc->sc_tx_chainmask = sc->sc_ah->ah_caps.tx_chainmask;
456 sc->sc_rx_chainmask = sc->sc_ah->ah_caps.rx_chainmask;
457 } else {
458 sc->sc_tx_chainmask = 1;
459 sc->sc_rx_chainmask = 1;
460 }
461
Sujith04bd4632008-11-28 22:18:05 +0530462 DPRINTF(sc, ATH_DBG_CONFIG, "tx chmask: %d, rx chmask: %d\n",
463 sc->sc_tx_chainmask, sc->sc_rx_chainmask);
Sujithff37e332008-11-24 12:07:55 +0530464}
465
466static void ath_node_attach(struct ath_softc *sc, struct ieee80211_sta *sta)
467{
468 struct ath_node *an;
469
470 an = (struct ath_node *)sta->drv_priv;
471
472 if (sc->sc_flags & SC_OP_TXAGGR)
473 ath_tx_node_init(sc, an);
474
475 an->maxampdu = 1 << (IEEE80211_HTCAP_MAXRXAMPDU_FACTOR +
476 sta->ht_cap.ampdu_factor);
477 an->mpdudensity = parse_mpdudensity(sta->ht_cap.ampdu_density);
478}
479
480static void ath_node_detach(struct ath_softc *sc, struct ieee80211_sta *sta)
481{
482 struct ath_node *an = (struct ath_node *)sta->drv_priv;
483
484 if (sc->sc_flags & SC_OP_TXAGGR)
485 ath_tx_node_cleanup(sc, an);
486}
487
488static void ath9k_tasklet(unsigned long data)
489{
490 struct ath_softc *sc = (struct ath_softc *)data;
491 u32 status = sc->sc_intrstatus;
492
493 if (status & ATH9K_INT_FATAL) {
494 /* need a chip reset */
495 ath_reset(sc, false);
496 return;
497 } else {
498
499 if (status &
500 (ATH9K_INT_RX | ATH9K_INT_RXEOL | ATH9K_INT_RXORN)) {
501 spin_lock_bh(&sc->sc_rxflushlock);
502 ath_rx_tasklet(sc, 0);
503 spin_unlock_bh(&sc->sc_rxflushlock);
504 }
505 /* XXX: optimize this */
506 if (status & ATH9K_INT_TX)
507 ath_tx_tasklet(sc);
508 }
509
510 /* re-enable hardware interrupt */
511 ath9k_hw_set_interrupts(sc->sc_ah, sc->sc_imask);
512}
513
514static irqreturn_t ath_isr(int irq, void *dev)
515{
516 struct ath_softc *sc = dev;
517 struct ath_hal *ah = sc->sc_ah;
518 enum ath9k_int status;
519 bool sched = false;
520
521 do {
522 if (sc->sc_flags & SC_OP_INVALID) {
523 /*
524 * The hardware is not ready/present, don't
525 * touch anything. Note this can happen early
526 * on if the IRQ is shared.
527 */
528 return IRQ_NONE;
529 }
530 if (!ath9k_hw_intrpend(ah)) { /* shared irq, not for us */
531 return IRQ_NONE;
532 }
533
534 /*
535 * Figure out the reason(s) for the interrupt. Note
536 * that the hal returns a pseudo-ISR that may include
537 * bits we haven't explicitly enabled so we mask the
538 * value to insure we only process bits we requested.
539 */
540 ath9k_hw_getisr(ah, &status); /* NB: clears ISR too */
541
542 status &= sc->sc_imask; /* discard unasked-for bits */
543
544 /*
545 * If there are no status bits set, then this interrupt was not
546 * for me (should have been caught above).
547 */
548 if (!status)
549 return IRQ_NONE;
550
551 sc->sc_intrstatus = status;
552
553 if (status & ATH9K_INT_FATAL) {
554 /* need a chip reset */
555 sched = true;
556 } else if (status & ATH9K_INT_RXORN) {
557 /* need a chip reset */
558 sched = true;
559 } else {
560 if (status & ATH9K_INT_SWBA) {
561 /* schedule a tasklet for beacon handling */
562 tasklet_schedule(&sc->bcon_tasklet);
563 }
564 if (status & ATH9K_INT_RXEOL) {
565 /*
566 * NB: the hardware should re-read the link when
567 * RXE bit is written, but it doesn't work
568 * at least on older hardware revs.
569 */
570 sched = true;
571 }
572
573 if (status & ATH9K_INT_TXURN)
574 /* bump tx trigger level */
575 ath9k_hw_updatetxtriglevel(ah, true);
576 /* XXX: optimize this */
577 if (status & ATH9K_INT_RX)
578 sched = true;
579 if (status & ATH9K_INT_TX)
580 sched = true;
581 if (status & ATH9K_INT_BMISS)
582 sched = true;
583 /* carrier sense timeout */
584 if (status & ATH9K_INT_CST)
585 sched = true;
586 if (status & ATH9K_INT_MIB) {
587 /*
588 * Disable interrupts until we service the MIB
589 * interrupt; otherwise it will continue to
590 * fire.
591 */
592 ath9k_hw_set_interrupts(ah, 0);
593 /*
594 * Let the hal handle the event. We assume
595 * it will clear whatever condition caused
596 * the interrupt.
597 */
598 ath9k_hw_procmibevent(ah, &sc->sc_halstats);
599 ath9k_hw_set_interrupts(ah, sc->sc_imask);
600 }
601 if (status & ATH9K_INT_TIM_TIMER) {
602 if (!(ah->ah_caps.hw_caps &
603 ATH9K_HW_CAP_AUTOSLEEP)) {
604 /* Clear RxAbort bit so that we can
605 * receive frames */
606 ath9k_hw_setrxabort(ah, 0);
607 sched = true;
608 }
609 }
610 }
611 } while (0);
612
613 if (sched) {
614 /* turn off every interrupt except SWBA */
615 ath9k_hw_set_interrupts(ah, (sc->sc_imask & ATH9K_INT_SWBA));
616 tasklet_schedule(&sc->intr_tq);
617 }
618
619 return IRQ_HANDLED;
620}
621
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700622static int ath_get_channel(struct ath_softc *sc,
623 struct ieee80211_channel *chan)
624{
625 int i;
626
627 for (i = 0; i < sc->sc_ah->ah_nchan; i++) {
628 if (sc->sc_ah->ah_channels[i].channel == chan->center_freq)
629 return i;
630 }
631
632 return -1;
633}
634
Sujithe11602b2008-11-27 09:46:27 +0530635/* ext_chan_offset: (-1, 0, 1) (below, none, above) */
636
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700637static u32 ath_get_extchanmode(struct ath_softc *sc,
Sujith99405f92008-11-24 12:08:35 +0530638 struct ieee80211_channel *chan,
Sujithe11602b2008-11-27 09:46:27 +0530639 int ext_chan_offset,
640 enum ath9k_ht_macmode tx_chan_width)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700641{
642 u32 chanmode = 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700643
644 switch (chan->band) {
645 case IEEE80211_BAND_2GHZ:
Sujithe11602b2008-11-27 09:46:27 +0530646 if ((ext_chan_offset == 0) &&
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700647 (tx_chan_width == ATH9K_HT_MACMODE_20))
648 chanmode = CHANNEL_G_HT20;
Sujithe11602b2008-11-27 09:46:27 +0530649 if ((ext_chan_offset == 1) &&
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700650 (tx_chan_width == ATH9K_HT_MACMODE_2040))
651 chanmode = CHANNEL_G_HT40PLUS;
Sujithe11602b2008-11-27 09:46:27 +0530652 if ((ext_chan_offset == -1) &&
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700653 (tx_chan_width == ATH9K_HT_MACMODE_2040))
654 chanmode = CHANNEL_G_HT40MINUS;
655 break;
656 case IEEE80211_BAND_5GHZ:
Sujithe11602b2008-11-27 09:46:27 +0530657 if ((ext_chan_offset == 0) &&
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700658 (tx_chan_width == ATH9K_HT_MACMODE_20))
659 chanmode = CHANNEL_A_HT20;
Sujithe11602b2008-11-27 09:46:27 +0530660 if ((ext_chan_offset == 1) &&
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700661 (tx_chan_width == ATH9K_HT_MACMODE_2040))
662 chanmode = CHANNEL_A_HT40PLUS;
Sujithe11602b2008-11-27 09:46:27 +0530663 if ((ext_chan_offset == -1) &&
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700664 (tx_chan_width == ATH9K_HT_MACMODE_2040))
665 chanmode = CHANNEL_A_HT40MINUS;
666 break;
667 default:
668 break;
669 }
670
671 return chanmode;
672}
673
Sujithff37e332008-11-24 12:07:55 +0530674static void ath_key_reset(struct ath_softc *sc, u16 keyix, int freeslot)
675{
676 ath9k_hw_keyreset(sc->sc_ah, keyix);
677 if (freeslot)
678 clear_bit(keyix, sc->sc_keymap);
679}
680
681static int ath_keyset(struct ath_softc *sc, u16 keyix,
682 struct ath9k_keyval *hk, const u8 mac[ETH_ALEN])
683{
684 bool status;
685
686 status = ath9k_hw_set_keycache_entry(sc->sc_ah,
687 keyix, hk, mac, false);
688
689 return status != false;
690}
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700691
692static int ath_setkey_tkip(struct ath_softc *sc,
693 struct ieee80211_key_conf *key,
694 struct ath9k_keyval *hk,
695 const u8 *addr)
696{
697 u8 *key_rxmic = NULL;
698 u8 *key_txmic = NULL;
699
700 key_txmic = key->key + NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY;
701 key_rxmic = key->key + NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY;
702
703 if (addr == NULL) {
704 /* Group key installation */
705 memcpy(hk->kv_mic, key_rxmic, sizeof(hk->kv_mic));
706 return ath_keyset(sc, key->keyidx, hk, addr);
707 }
708 if (!sc->sc_splitmic) {
709 /*
710 * data key goes at first index,
711 * the hal handles the MIC keys at index+64.
712 */
713 memcpy(hk->kv_mic, key_rxmic, sizeof(hk->kv_mic));
714 memcpy(hk->kv_txmic, key_txmic, sizeof(hk->kv_txmic));
715 return ath_keyset(sc, key->keyidx, hk, addr);
716 }
717 /*
718 * TX key goes at first index, RX key at +32.
719 * The hal handles the MIC keys at index+64.
720 */
721 memcpy(hk->kv_mic, key_txmic, sizeof(hk->kv_mic));
722 if (!ath_keyset(sc, key->keyidx, hk, NULL)) {
723 /* Txmic entry failed. No need to proceed further */
724 DPRINTF(sc, ATH_DBG_KEYCACHE,
Sujith04bd4632008-11-28 22:18:05 +0530725 "Setting TX MIC Key Failed\n");
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700726 return 0;
727 }
728
729 memcpy(hk->kv_mic, key_rxmic, sizeof(hk->kv_mic));
730 /* XXX delete tx key on failure? */
731 return ath_keyset(sc, key->keyidx+32, hk, addr);
732}
733
734static int ath_key_config(struct ath_softc *sc,
735 const u8 *addr,
736 struct ieee80211_key_conf *key)
737{
738 struct ieee80211_vif *vif;
739 struct ath9k_keyval hk;
740 const u8 *mac = NULL;
741 int ret = 0;
Johannes Berg05c914f2008-09-11 00:01:58 +0200742 enum nl80211_iftype opmode;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700743
744 memset(&hk, 0, sizeof(hk));
745
746 switch (key->alg) {
747 case ALG_WEP:
748 hk.kv_type = ATH9K_CIPHER_WEP;
749 break;
750 case ALG_TKIP:
751 hk.kv_type = ATH9K_CIPHER_TKIP;
752 break;
753 case ALG_CCMP:
754 hk.kv_type = ATH9K_CIPHER_AES_CCM;
755 break;
756 default:
757 return -EINVAL;
758 }
759
760 hk.kv_len = key->keylen;
761 memcpy(hk.kv_val, key->key, key->keylen);
762
763 if (!sc->sc_vaps[0])
764 return -EIO;
765
Sujith5640b082008-10-29 10:16:06 +0530766 vif = sc->sc_vaps[0];
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700767 opmode = vif->type;
768
769 /*
770 * Strategy:
771 * For _M_STA mc tx, we will not setup a key at all since we never
772 * tx mc.
773 * _M_STA mc rx, we will use the keyID.
774 * for _M_IBSS mc tx, we will use the keyID, and no macaddr.
775 * for _M_IBSS mc rx, we will alloc a slot and plumb the mac of the
776 * peer node. BUT we will plumb a cleartext key so that we can do
777 * perSta default key table lookup in software.
778 */
779 if (is_broadcast_ether_addr(addr)) {
780 switch (opmode) {
Johannes Berg05c914f2008-09-11 00:01:58 +0200781 case NL80211_IFTYPE_STATION:
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700782 /* default key: could be group WPA key
783 * or could be static WEP key */
784 mac = NULL;
785 break;
Johannes Berg05c914f2008-09-11 00:01:58 +0200786 case NL80211_IFTYPE_ADHOC:
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700787 break;
Johannes Berg05c914f2008-09-11 00:01:58 +0200788 case NL80211_IFTYPE_AP:
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700789 break;
790 default:
791 ASSERT(0);
792 break;
793 }
794 } else {
795 mac = addr;
796 }
797
798 if (key->alg == ALG_TKIP)
799 ret = ath_setkey_tkip(sc, key, &hk, mac);
800 else
801 ret = ath_keyset(sc, key->keyidx, &hk, mac);
802
803 if (!ret)
804 return -EIO;
805
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700806 return 0;
807}
808
809static void ath_key_delete(struct ath_softc *sc, struct ieee80211_key_conf *key)
810{
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700811 int freeslot;
812
Sujithff9b6622008-08-14 13:27:16 +0530813 freeslot = (key->keyidx >= 4) ? 1 : 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700814 ath_key_reset(sc, key->keyidx, freeslot);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700815}
816
Johannes Bergd9fe60d2008-10-09 12:13:49 +0200817static void setup_ht_cap(struct ieee80211_sta_ht_cap *ht_info)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700818{
Sujith60653672008-08-14 13:28:02 +0530819#define ATH9K_HT_CAP_MAXRXAMPDU_65536 0x3 /* 2 ^ 16 */
820#define ATH9K_HT_CAP_MPDUDENSITY_8 0x6 /* 8 usec */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700821
Johannes Bergd9fe60d2008-10-09 12:13:49 +0200822 ht_info->ht_supported = true;
823 ht_info->cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
824 IEEE80211_HT_CAP_SM_PS |
825 IEEE80211_HT_CAP_SGI_40 |
826 IEEE80211_HT_CAP_DSSSCCK40;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700827
Sujith60653672008-08-14 13:28:02 +0530828 ht_info->ampdu_factor = ATH9K_HT_CAP_MAXRXAMPDU_65536;
829 ht_info->ampdu_density = ATH9K_HT_CAP_MPDUDENSITY_8;
Johannes Bergd9fe60d2008-10-09 12:13:49 +0200830 /* set up supported mcs set */
831 memset(&ht_info->mcs, 0, sizeof(ht_info->mcs));
832 ht_info->mcs.rx_mask[0] = 0xff;
833 ht_info->mcs.rx_mask[1] = 0xff;
834 ht_info->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700835}
836
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530837static void ath9k_ht_conf(struct ath_softc *sc,
838 struct ieee80211_bss_conf *bss_conf)
839{
Johannes Bergae5eb022008-10-14 16:58:37 +0200840 if (sc->hw->conf.ht.enabled) {
Johannes Bergae5eb022008-10-14 16:58:37 +0200841 if (bss_conf->ht.width_40_ok)
Sujith99405f92008-11-24 12:08:35 +0530842 sc->tx_chan_width = ATH9K_HT_MACMODE_2040;
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530843 else
Sujith99405f92008-11-24 12:08:35 +0530844 sc->tx_chan_width = ATH9K_HT_MACMODE_20;
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530845
Sujith99405f92008-11-24 12:08:35 +0530846 ath9k_hw_set11nmac2040(sc->sc_ah, sc->tx_chan_width);
847
848 DPRINTF(sc, ATH_DBG_CONFIG,
Sujith04bd4632008-11-28 22:18:05 +0530849 "BSS Changed HT, chanwidth: %d\n", sc->tx_chan_width);
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530850 }
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530851}
852
Sujithe11602b2008-11-27 09:46:27 +0530853static inline int ath_sec_offset(u8 ext_offset)
854{
855 if (ext_offset == IEEE80211_HT_PARAM_CHA_SEC_NONE)
856 return 0;
857 else if (ext_offset == IEEE80211_HT_PARAM_CHA_SEC_ABOVE)
858 return 1;
859 else if (ext_offset == IEEE80211_HT_PARAM_CHA_SEC_BELOW)
860 return -1;
861
862 return 0;
863}
864
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530865static void ath9k_bss_assoc_info(struct ath_softc *sc,
Sujith5640b082008-10-29 10:16:06 +0530866 struct ieee80211_vif *vif,
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530867 struct ieee80211_bss_conf *bss_conf)
868{
869 struct ieee80211_hw *hw = sc->hw;
870 struct ieee80211_channel *curchan = hw->conf.channel;
Sujith5640b082008-10-29 10:16:06 +0530871 struct ath_vap *avp = (void *)vif->drv_priv;
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530872 int pos;
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530873
874 if (bss_conf->assoc) {
Sujith04bd4632008-11-28 22:18:05 +0530875 DPRINTF(sc, ATH_DBG_CONFIG, "Bss Info ASSOC %d\n", bss_conf->aid);
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530876
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530877 /* New association, store aid */
878 if (avp->av_opmode == ATH9K_M_STA) {
879 sc->sc_curaid = bss_conf->aid;
880 ath9k_hw_write_associd(sc->sc_ah, sc->sc_curbssid,
881 sc->sc_curaid);
882 }
883
884 /* Configure the beacon */
885 ath_beacon_config(sc, 0);
886 sc->sc_flags |= SC_OP_BEACONS;
887
888 /* Reset rssi stats */
889 sc->sc_halstats.ns_avgbrssi = ATH_RSSI_DUMMY_MARKER;
890 sc->sc_halstats.ns_avgrssi = ATH_RSSI_DUMMY_MARKER;
891 sc->sc_halstats.ns_avgtxrssi = ATH_RSSI_DUMMY_MARKER;
892 sc->sc_halstats.ns_avgtxrate = ATH_RATE_DUMMY_MARKER;
893
894 /* Update chainmask */
Johannes Bergae5eb022008-10-14 16:58:37 +0200895 ath_update_chainmask(sc, hw->conf.ht.enabled);
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530896
897 DPRINTF(sc, ATH_DBG_CONFIG,
Sujith04bd4632008-11-28 22:18:05 +0530898 "bssid %pM aid 0x%x\n",
Johannes Berge1749612008-10-27 15:59:26 -0700899 sc->sc_curbssid, sc->sc_curaid);
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530900
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530901 pos = ath_get_channel(sc, curchan);
902 if (pos == -1) {
903 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd4632008-11-28 22:18:05 +0530904 "Invalid channel: %d\n", curchan->center_freq);
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530905 return;
906 }
907
Sujith99405f92008-11-24 12:08:35 +0530908 if (hw->conf.ht.enabled) {
Sujithe11602b2008-11-27 09:46:27 +0530909 int offset =
910 ath_sec_offset(bss_conf->ht.secondary_channel_offset);
911 sc->tx_chan_width = (bss_conf->ht.width_40_ok) ?
912 ATH9K_HT_MACMODE_2040 : ATH9K_HT_MACMODE_20;
Sujith99405f92008-11-24 12:08:35 +0530913
Sujithe11602b2008-11-27 09:46:27 +0530914 sc->sc_ah->ah_channels[pos].chanmode =
915 ath_get_extchanmode(sc, curchan,
916 offset, sc->tx_chan_width);
Sujith99405f92008-11-24 12:08:35 +0530917 } else {
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530918 sc->sc_ah->ah_channels[pos].chanmode =
919 (curchan->band == IEEE80211_BAND_2GHZ) ?
920 CHANNEL_G : CHANNEL_A;
Sujith99405f92008-11-24 12:08:35 +0530921 }
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530922
923 /* set h/w channel */
924 if (ath_set_channel(sc, &sc->sc_ah->ah_channels[pos]) < 0)
Sujith04bd4632008-11-28 22:18:05 +0530925 DPRINTF(sc, ATH_DBG_FATAL, "Unable to set channel: %d\n",
926 curchan->center_freq);
927
Luis R. Rodriguez6f255422008-10-03 15:45:27 -0700928 /* Start ANI */
929 mod_timer(&sc->sc_ani.timer,
930 jiffies + msecs_to_jiffies(ATH_ANI_POLLINTERVAL));
931
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530932 } else {
Sujith04bd4632008-11-28 22:18:05 +0530933 DPRINTF(sc, ATH_DBG_CONFIG, "Bss Info DISSOC\n");
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530934 sc->sc_curaid = 0;
935 }
936}
937
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530938/********************************/
939/* LED functions */
940/********************************/
941
942static void ath_led_brightness(struct led_classdev *led_cdev,
943 enum led_brightness brightness)
944{
945 struct ath_led *led = container_of(led_cdev, struct ath_led, led_cdev);
946 struct ath_softc *sc = led->sc;
947
948 switch (brightness) {
949 case LED_OFF:
950 if (led->led_type == ATH_LED_ASSOC ||
951 led->led_type == ATH_LED_RADIO)
952 sc->sc_flags &= ~SC_OP_LED_ASSOCIATED;
953 ath9k_hw_set_gpio(sc->sc_ah, ATH_LED_PIN,
954 (led->led_type == ATH_LED_RADIO) ? 1 :
955 !!(sc->sc_flags & SC_OP_LED_ASSOCIATED));
956 break;
957 case LED_FULL:
958 if (led->led_type == ATH_LED_ASSOC)
959 sc->sc_flags |= SC_OP_LED_ASSOCIATED;
960 ath9k_hw_set_gpio(sc->sc_ah, ATH_LED_PIN, 0);
961 break;
962 default:
963 break;
964 }
965}
966
967static int ath_register_led(struct ath_softc *sc, struct ath_led *led,
968 char *trigger)
969{
970 int ret;
971
972 led->sc = sc;
973 led->led_cdev.name = led->name;
974 led->led_cdev.default_trigger = trigger;
975 led->led_cdev.brightness_set = ath_led_brightness;
976
977 ret = led_classdev_register(wiphy_dev(sc->hw->wiphy), &led->led_cdev);
978 if (ret)
979 DPRINTF(sc, ATH_DBG_FATAL,
980 "Failed to register led:%s", led->name);
981 else
982 led->registered = 1;
983 return ret;
984}
985
986static void ath_unregister_led(struct ath_led *led)
987{
988 if (led->registered) {
989 led_classdev_unregister(&led->led_cdev);
990 led->registered = 0;
991 }
992}
993
994static void ath_deinit_leds(struct ath_softc *sc)
995{
996 ath_unregister_led(&sc->assoc_led);
997 sc->sc_flags &= ~SC_OP_LED_ASSOCIATED;
998 ath_unregister_led(&sc->tx_led);
999 ath_unregister_led(&sc->rx_led);
1000 ath_unregister_led(&sc->radio_led);
1001 ath9k_hw_set_gpio(sc->sc_ah, ATH_LED_PIN, 1);
1002}
1003
1004static void ath_init_leds(struct ath_softc *sc)
1005{
1006 char *trigger;
1007 int ret;
1008
1009 /* Configure gpio 1 for output */
1010 ath9k_hw_cfg_output(sc->sc_ah, ATH_LED_PIN,
1011 AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
1012 /* LED off, active low */
1013 ath9k_hw_set_gpio(sc->sc_ah, ATH_LED_PIN, 1);
1014
1015 trigger = ieee80211_get_radio_led_name(sc->hw);
1016 snprintf(sc->radio_led.name, sizeof(sc->radio_led.name),
1017 "ath9k-%s:radio", wiphy_name(sc->hw->wiphy));
1018 ret = ath_register_led(sc, &sc->radio_led, trigger);
1019 sc->radio_led.led_type = ATH_LED_RADIO;
1020 if (ret)
1021 goto fail;
1022
1023 trigger = ieee80211_get_assoc_led_name(sc->hw);
1024 snprintf(sc->assoc_led.name, sizeof(sc->assoc_led.name),
1025 "ath9k-%s:assoc", wiphy_name(sc->hw->wiphy));
1026 ret = ath_register_led(sc, &sc->assoc_led, trigger);
1027 sc->assoc_led.led_type = ATH_LED_ASSOC;
1028 if (ret)
1029 goto fail;
1030
1031 trigger = ieee80211_get_tx_led_name(sc->hw);
1032 snprintf(sc->tx_led.name, sizeof(sc->tx_led.name),
1033 "ath9k-%s:tx", wiphy_name(sc->hw->wiphy));
1034 ret = ath_register_led(sc, &sc->tx_led, trigger);
1035 sc->tx_led.led_type = ATH_LED_TX;
1036 if (ret)
1037 goto fail;
1038
1039 trigger = ieee80211_get_rx_led_name(sc->hw);
1040 snprintf(sc->rx_led.name, sizeof(sc->rx_led.name),
1041 "ath9k-%s:rx", wiphy_name(sc->hw->wiphy));
1042 ret = ath_register_led(sc, &sc->rx_led, trigger);
1043 sc->rx_led.led_type = ATH_LED_RX;
1044 if (ret)
1045 goto fail;
1046
1047 return;
1048
1049fail:
1050 ath_deinit_leds(sc);
1051}
1052
Senthil Balasubramaniane97275c2008-11-13 18:00:02 +05301053#if defined(CONFIG_RFKILL) || defined(CONFIG_RFKILL_MODULE)
Sujith9c84b792008-10-29 10:17:13 +05301054
Vasanthakumar Thiagarajan500c0642008-09-10 18:50:17 +05301055/*******************/
1056/* Rfkill */
1057/*******************/
1058
1059static void ath_radio_enable(struct ath_softc *sc)
1060{
1061 struct ath_hal *ah = sc->sc_ah;
1062 int status;
1063
1064 spin_lock_bh(&sc->sc_resetlock);
1065 if (!ath9k_hw_reset(ah, ah->ah_curchan,
Sujith99405f92008-11-24 12:08:35 +05301066 sc->tx_chan_width,
Vasanthakumar Thiagarajan500c0642008-09-10 18:50:17 +05301067 sc->sc_tx_chainmask,
1068 sc->sc_rx_chainmask,
1069 sc->sc_ht_extprotspacing,
1070 false, &status)) {
1071 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd4632008-11-28 22:18:05 +05301072 "Unable to reset channel %u (%uMhz) "
1073 "flags 0x%x hal status %u\n",
Vasanthakumar Thiagarajan500c0642008-09-10 18:50:17 +05301074 ath9k_hw_mhz2ieee(ah,
1075 ah->ah_curchan->channel,
1076 ah->ah_curchan->channelFlags),
1077 ah->ah_curchan->channel,
1078 ah->ah_curchan->channelFlags, status);
1079 }
1080 spin_unlock_bh(&sc->sc_resetlock);
1081
1082 ath_update_txpow(sc);
1083 if (ath_startrecv(sc) != 0) {
1084 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd4632008-11-28 22:18:05 +05301085 "Unable to restart recv logic\n");
Vasanthakumar Thiagarajan500c0642008-09-10 18:50:17 +05301086 return;
1087 }
1088
1089 if (sc->sc_flags & SC_OP_BEACONS)
1090 ath_beacon_config(sc, ATH_IF_ID_ANY); /* restart beacons */
1091
1092 /* Re-Enable interrupts */
1093 ath9k_hw_set_interrupts(ah, sc->sc_imask);
1094
1095 /* Enable LED */
1096 ath9k_hw_cfg_output(ah, ATH_LED_PIN,
1097 AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
1098 ath9k_hw_set_gpio(ah, ATH_LED_PIN, 0);
1099
1100 ieee80211_wake_queues(sc->hw);
1101}
1102
1103static void ath_radio_disable(struct ath_softc *sc)
1104{
1105 struct ath_hal *ah = sc->sc_ah;
1106 int status;
1107
1108
1109 ieee80211_stop_queues(sc->hw);
1110
1111 /* Disable LED */
1112 ath9k_hw_set_gpio(ah, ATH_LED_PIN, 1);
1113 ath9k_hw_cfg_gpio_input(ah, ATH_LED_PIN);
1114
1115 /* Disable interrupts */
1116 ath9k_hw_set_interrupts(ah, 0);
1117
1118 ath_draintxq(sc, false); /* clear pending tx frames */
1119 ath_stoprecv(sc); /* turn off frame recv */
1120 ath_flushrecv(sc); /* flush recv queue */
1121
1122 spin_lock_bh(&sc->sc_resetlock);
1123 if (!ath9k_hw_reset(ah, ah->ah_curchan,
Sujith99405f92008-11-24 12:08:35 +05301124 sc->tx_chan_width,
Vasanthakumar Thiagarajan500c0642008-09-10 18:50:17 +05301125 sc->sc_tx_chainmask,
1126 sc->sc_rx_chainmask,
1127 sc->sc_ht_extprotspacing,
1128 false, &status)) {
1129 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd4632008-11-28 22:18:05 +05301130 "Unable to reset channel %u (%uMhz) "
1131 "flags 0x%x hal status %u\n",
Vasanthakumar Thiagarajan500c0642008-09-10 18:50:17 +05301132 ath9k_hw_mhz2ieee(ah,
1133 ah->ah_curchan->channel,
1134 ah->ah_curchan->channelFlags),
1135 ah->ah_curchan->channel,
1136 ah->ah_curchan->channelFlags, status);
1137 }
1138 spin_unlock_bh(&sc->sc_resetlock);
1139
1140 ath9k_hw_phy_disable(ah);
1141 ath9k_hw_setpower(ah, ATH9K_PM_FULL_SLEEP);
1142}
1143
1144static bool ath_is_rfkill_set(struct ath_softc *sc)
1145{
1146 struct ath_hal *ah = sc->sc_ah;
1147
1148 return ath9k_hw_gpio_get(ah, ah->ah_rfkill_gpio) ==
1149 ah->ah_rfkill_polarity;
1150}
1151
1152/* h/w rfkill poll function */
1153static void ath_rfkill_poll(struct work_struct *work)
1154{
1155 struct ath_softc *sc = container_of(work, struct ath_softc,
1156 rf_kill.rfkill_poll.work);
1157 bool radio_on;
1158
1159 if (sc->sc_flags & SC_OP_INVALID)
1160 return;
1161
1162 radio_on = !ath_is_rfkill_set(sc);
1163
1164 /*
1165 * enable/disable radio only when there is a
1166 * state change in RF switch
1167 */
1168 if (radio_on == !!(sc->sc_flags & SC_OP_RFKILL_HW_BLOCKED)) {
1169 enum rfkill_state state;
1170
1171 if (sc->sc_flags & SC_OP_RFKILL_SW_BLOCKED) {
1172 state = radio_on ? RFKILL_STATE_SOFT_BLOCKED
1173 : RFKILL_STATE_HARD_BLOCKED;
1174 } else if (radio_on) {
1175 ath_radio_enable(sc);
1176 state = RFKILL_STATE_UNBLOCKED;
1177 } else {
1178 ath_radio_disable(sc);
1179 state = RFKILL_STATE_HARD_BLOCKED;
1180 }
1181
1182 if (state == RFKILL_STATE_HARD_BLOCKED)
1183 sc->sc_flags |= SC_OP_RFKILL_HW_BLOCKED;
1184 else
1185 sc->sc_flags &= ~SC_OP_RFKILL_HW_BLOCKED;
1186
1187 rfkill_force_state(sc->rf_kill.rfkill, state);
1188 }
1189
1190 queue_delayed_work(sc->hw->workqueue, &sc->rf_kill.rfkill_poll,
1191 msecs_to_jiffies(ATH_RFKILL_POLL_INTERVAL));
1192}
1193
1194/* s/w rfkill handler */
1195static int ath_sw_toggle_radio(void *data, enum rfkill_state state)
1196{
1197 struct ath_softc *sc = data;
1198
1199 switch (state) {
1200 case RFKILL_STATE_SOFT_BLOCKED:
1201 if (!(sc->sc_flags & (SC_OP_RFKILL_HW_BLOCKED |
1202 SC_OP_RFKILL_SW_BLOCKED)))
1203 ath_radio_disable(sc);
1204 sc->sc_flags |= SC_OP_RFKILL_SW_BLOCKED;
1205 return 0;
1206 case RFKILL_STATE_UNBLOCKED:
1207 if ((sc->sc_flags & SC_OP_RFKILL_SW_BLOCKED)) {
1208 sc->sc_flags &= ~SC_OP_RFKILL_SW_BLOCKED;
1209 if (sc->sc_flags & SC_OP_RFKILL_HW_BLOCKED) {
1210 DPRINTF(sc, ATH_DBG_FATAL, "Can't turn on the"
Sujith04bd4632008-11-28 22:18:05 +05301211 "radio as it is disabled by h/w\n");
Vasanthakumar Thiagarajan500c0642008-09-10 18:50:17 +05301212 return -EPERM;
1213 }
1214 ath_radio_enable(sc);
1215 }
1216 return 0;
1217 default:
1218 return -EINVAL;
1219 }
1220}
1221
1222/* Init s/w rfkill */
1223static int ath_init_sw_rfkill(struct ath_softc *sc)
1224{
1225 sc->rf_kill.rfkill = rfkill_allocate(wiphy_dev(sc->hw->wiphy),
1226 RFKILL_TYPE_WLAN);
1227 if (!sc->rf_kill.rfkill) {
1228 DPRINTF(sc, ATH_DBG_FATAL, "Failed to allocate rfkill\n");
1229 return -ENOMEM;
1230 }
1231
1232 snprintf(sc->rf_kill.rfkill_name, sizeof(sc->rf_kill.rfkill_name),
1233 "ath9k-%s:rfkill", wiphy_name(sc->hw->wiphy));
1234 sc->rf_kill.rfkill->name = sc->rf_kill.rfkill_name;
1235 sc->rf_kill.rfkill->data = sc;
1236 sc->rf_kill.rfkill->toggle_radio = ath_sw_toggle_radio;
1237 sc->rf_kill.rfkill->state = RFKILL_STATE_UNBLOCKED;
1238 sc->rf_kill.rfkill->user_claim_unsupported = 1;
1239
1240 return 0;
1241}
1242
1243/* Deinitialize rfkill */
1244static void ath_deinit_rfkill(struct ath_softc *sc)
1245{
1246 if (sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
1247 cancel_delayed_work_sync(&sc->rf_kill.rfkill_poll);
1248
1249 if (sc->sc_flags & SC_OP_RFKILL_REGISTERED) {
1250 rfkill_unregister(sc->rf_kill.rfkill);
1251 sc->sc_flags &= ~SC_OP_RFKILL_REGISTERED;
1252 sc->rf_kill.rfkill = NULL;
1253 }
1254}
Sujith9c84b792008-10-29 10:17:13 +05301255
1256static int ath_start_rfkill_poll(struct ath_softc *sc)
1257{
1258 if (sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
1259 queue_delayed_work(sc->hw->workqueue,
1260 &sc->rf_kill.rfkill_poll, 0);
1261
1262 if (!(sc->sc_flags & SC_OP_RFKILL_REGISTERED)) {
1263 if (rfkill_register(sc->rf_kill.rfkill)) {
1264 DPRINTF(sc, ATH_DBG_FATAL,
1265 "Unable to register rfkill\n");
1266 rfkill_free(sc->rf_kill.rfkill);
1267
1268 /* Deinitialize the device */
Senthil Balasubramanian306efdd2008-11-13 18:00:37 +05301269 ath_detach(sc);
Sujith9c84b792008-10-29 10:17:13 +05301270 if (sc->pdev->irq)
1271 free_irq(sc->pdev->irq, sc);
Sujith9c84b792008-10-29 10:17:13 +05301272 pci_iounmap(sc->pdev, sc->mem);
1273 pci_release_region(sc->pdev, 0);
1274 pci_disable_device(sc->pdev);
Sujith9757d552008-11-04 18:25:27 +05301275 ieee80211_free_hw(sc->hw);
Sujith9c84b792008-10-29 10:17:13 +05301276 return -EIO;
1277 } else {
1278 sc->sc_flags |= SC_OP_RFKILL_REGISTERED;
1279 }
1280 }
1281
1282 return 0;
1283}
Vasanthakumar Thiagarajan500c0642008-09-10 18:50:17 +05301284#endif /* CONFIG_RFKILL */
1285
Sujith9c84b792008-10-29 10:17:13 +05301286static void ath_detach(struct ath_softc *sc)
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +05301287{
1288 struct ieee80211_hw *hw = sc->hw;
Sujith9c84b792008-10-29 10:17:13 +05301289 int i = 0;
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +05301290
Sujith04bd4632008-11-28 22:18:05 +05301291 DPRINTF(sc, ATH_DBG_CONFIG, "Detach ATH hw\n");
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +05301292
Senthil Balasubramaniane97275c2008-11-13 18:00:02 +05301293#if defined(CONFIG_RFKILL) || defined(CONFIG_RFKILL_MODULE)
Vasanthakumar Thiagarajan500c0642008-09-10 18:50:17 +05301294 ath_deinit_rfkill(sc);
1295#endif
Vasanthakumar Thiagarajan3fcdfb42008-11-18 01:19:56 +05301296 ath_deinit_leds(sc);
1297
1298 ieee80211_unregister_hw(hw);
1299
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +05301300 ath_rate_control_unregister();
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +05301301
1302 ath_rx_cleanup(sc);
1303 ath_tx_cleanup(sc);
1304
Sujith9c84b792008-10-29 10:17:13 +05301305 tasklet_kill(&sc->intr_tq);
1306 tasklet_kill(&sc->bcon_tasklet);
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +05301307
Sujith9c84b792008-10-29 10:17:13 +05301308 if (!(sc->sc_flags & SC_OP_INVALID))
1309 ath9k_hw_setpower(sc->sc_ah, ATH9K_PM_AWAKE);
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +05301310
Sujith9c84b792008-10-29 10:17:13 +05301311 /* cleanup tx queues */
1312 for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++)
1313 if (ATH_TXQ_SETUP(sc, i))
1314 ath_tx_cleanupq(sc, &sc->sc_txq[i]);
1315
1316 ath9k_hw_detach(sc->sc_ah);
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +05301317}
1318
Sujithff37e332008-11-24 12:07:55 +05301319static int ath_init(u16 devid, struct ath_softc *sc)
1320{
1321 struct ath_hal *ah = NULL;
1322 int status;
1323 int error = 0, i;
1324 int csz = 0;
1325
1326 /* XXX: hardware will not be ready until ath_open() being called */
1327 sc->sc_flags |= SC_OP_INVALID;
1328 sc->sc_debug = DBG_DEFAULT;
1329
1330 spin_lock_init(&sc->sc_resetlock);
1331 tasklet_init(&sc->intr_tq, ath9k_tasklet, (unsigned long)sc);
1332 tasklet_init(&sc->bcon_tasklet, ath9k_beacon_tasklet,
1333 (unsigned long)sc);
1334
1335 /*
1336 * Cache line size is used to size and align various
1337 * structures used to communicate with the hardware.
1338 */
1339 bus_read_cachesize(sc, &csz);
1340 /* XXX assert csz is non-zero */
1341 sc->sc_cachelsz = csz << 2; /* convert to bytes */
1342
1343 ah = ath9k_hw_attach(devid, sc, sc->mem, &status);
1344 if (ah == NULL) {
1345 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd4632008-11-28 22:18:05 +05301346 "Unable to attach hardware; HAL status %u\n", status);
Sujithff37e332008-11-24 12:07:55 +05301347 error = -ENXIO;
1348 goto bad;
1349 }
1350 sc->sc_ah = ah;
1351
1352 /* Get the hardware key cache size. */
1353 sc->sc_keymax = ah->ah_caps.keycache_size;
1354 if (sc->sc_keymax > ATH_KEYMAX) {
1355 DPRINTF(sc, ATH_DBG_KEYCACHE,
Sujith04bd4632008-11-28 22:18:05 +05301356 "Warning, using only %u entries in %u key cache\n",
1357 ATH_KEYMAX, sc->sc_keymax);
Sujithff37e332008-11-24 12:07:55 +05301358 sc->sc_keymax = ATH_KEYMAX;
1359 }
1360
1361 /*
1362 * Reset the key cache since some parts do not
1363 * reset the contents on initial power up.
1364 */
1365 for (i = 0; i < sc->sc_keymax; i++)
1366 ath9k_hw_keyreset(ah, (u16) i);
1367 /*
1368 * Mark key cache slots associated with global keys
1369 * as in use. If we knew TKIP was not to be used we
1370 * could leave the +32, +64, and +32+64 slots free.
1371 * XXX only for splitmic.
1372 */
1373 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
1374 set_bit(i, sc->sc_keymap);
1375 set_bit(i + 32, sc->sc_keymap);
1376 set_bit(i + 64, sc->sc_keymap);
1377 set_bit(i + 32 + 64, sc->sc_keymap);
1378 }
1379
1380 /* Collect the channel list using the default country code */
1381
1382 error = ath_setup_channels(sc);
1383 if (error)
1384 goto bad;
1385
1386 /* default to MONITOR mode */
1387 sc->sc_ah->ah_opmode = ATH9K_M_MONITOR;
1388
1389 /* Setup rate tables */
1390
1391 ath_rate_attach(sc);
1392 ath_setup_rates(sc, IEEE80211_BAND_2GHZ);
1393 ath_setup_rates(sc, IEEE80211_BAND_5GHZ);
1394
1395 /*
1396 * Allocate hardware transmit queues: one queue for
1397 * beacon frames and one data queue for each QoS
1398 * priority. Note that the hal handles reseting
1399 * these queues at the needed time.
1400 */
1401 sc->sc_bhalq = ath_beaconq_setup(ah);
1402 if (sc->sc_bhalq == -1) {
1403 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd4632008-11-28 22:18:05 +05301404 "Unable to setup a beacon xmit queue\n");
Sujithff37e332008-11-24 12:07:55 +05301405 error = -EIO;
1406 goto bad2;
1407 }
1408 sc->sc_cabq = ath_txq_setup(sc, ATH9K_TX_QUEUE_CAB, 0);
1409 if (sc->sc_cabq == NULL) {
1410 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd4632008-11-28 22:18:05 +05301411 "Unable to setup CAB xmit queue\n");
Sujithff37e332008-11-24 12:07:55 +05301412 error = -EIO;
1413 goto bad2;
1414 }
1415
1416 sc->sc_config.cabqReadytime = ATH_CABQ_READY_TIME;
1417 ath_cabq_update(sc);
1418
1419 for (i = 0; i < ARRAY_SIZE(sc->sc_haltype2q); i++)
1420 sc->sc_haltype2q[i] = -1;
1421
1422 /* Setup data queues */
1423 /* NB: ensure BK queue is the lowest priority h/w queue */
1424 if (!ath_tx_setup(sc, ATH9K_WME_AC_BK)) {
1425 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd4632008-11-28 22:18:05 +05301426 "Unable to setup xmit queue for BK traffic\n");
Sujithff37e332008-11-24 12:07:55 +05301427 error = -EIO;
1428 goto bad2;
1429 }
1430
1431 if (!ath_tx_setup(sc, ATH9K_WME_AC_BE)) {
1432 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd4632008-11-28 22:18:05 +05301433 "Unable to setup xmit queue for BE traffic\n");
Sujithff37e332008-11-24 12:07:55 +05301434 error = -EIO;
1435 goto bad2;
1436 }
1437 if (!ath_tx_setup(sc, ATH9K_WME_AC_VI)) {
1438 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd4632008-11-28 22:18:05 +05301439 "Unable to setup xmit queue for VI traffic\n");
Sujithff37e332008-11-24 12:07:55 +05301440 error = -EIO;
1441 goto bad2;
1442 }
1443 if (!ath_tx_setup(sc, ATH9K_WME_AC_VO)) {
1444 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd4632008-11-28 22:18:05 +05301445 "Unable to setup xmit queue for VO traffic\n");
Sujithff37e332008-11-24 12:07:55 +05301446 error = -EIO;
1447 goto bad2;
1448 }
1449
1450 /* Initializes the noise floor to a reasonable default value.
1451 * Later on this will be updated during ANI processing. */
1452
1453 sc->sc_ani.sc_noise_floor = ATH_DEFAULT_NOISE_FLOOR;
1454 setup_timer(&sc->sc_ani.timer, ath_ani_calibrate, (unsigned long)sc);
1455
1456 if (ath9k_hw_getcapability(ah, ATH9K_CAP_CIPHER,
1457 ATH9K_CIPHER_TKIP, NULL)) {
1458 /*
1459 * Whether we should enable h/w TKIP MIC.
1460 * XXX: if we don't support WME TKIP MIC, then we wouldn't
1461 * report WMM capable, so it's always safe to turn on
1462 * TKIP MIC in this case.
1463 */
1464 ath9k_hw_setcapability(sc->sc_ah, ATH9K_CAP_TKIP_MIC,
1465 0, 1, NULL);
1466 }
1467
1468 /*
1469 * Check whether the separate key cache entries
1470 * are required to handle both tx+rx MIC keys.
1471 * With split mic keys the number of stations is limited
1472 * to 27 otherwise 59.
1473 */
1474 if (ath9k_hw_getcapability(ah, ATH9K_CAP_CIPHER,
1475 ATH9K_CIPHER_TKIP, NULL)
1476 && ath9k_hw_getcapability(ah, ATH9K_CAP_CIPHER,
1477 ATH9K_CIPHER_MIC, NULL)
1478 && ath9k_hw_getcapability(ah, ATH9K_CAP_TKIP_SPLIT,
1479 0, NULL))
1480 sc->sc_splitmic = 1;
1481
1482 /* turn on mcast key search if possible */
1483 if (!ath9k_hw_getcapability(ah, ATH9K_CAP_MCAST_KEYSRCH, 0, NULL))
1484 (void)ath9k_hw_setcapability(ah, ATH9K_CAP_MCAST_KEYSRCH, 1,
1485 1, NULL);
1486
1487 sc->sc_config.txpowlimit = ATH_TXPOWER_MAX;
1488 sc->sc_config.txpowlimit_override = 0;
1489
1490 /* 11n Capabilities */
1491 if (ah->ah_caps.hw_caps & ATH9K_HW_CAP_HT) {
1492 sc->sc_flags |= SC_OP_TXAGGR;
1493 sc->sc_flags |= SC_OP_RXAGGR;
1494 }
1495
1496 sc->sc_tx_chainmask = ah->ah_caps.tx_chainmask;
1497 sc->sc_rx_chainmask = ah->ah_caps.rx_chainmask;
1498
1499 ath9k_hw_setcapability(ah, ATH9K_CAP_DIVERSITY, 1, true, NULL);
1500 sc->sc_defant = ath9k_hw_getdefantenna(ah);
1501
1502 ath9k_hw_getmac(ah, sc->sc_myaddr);
1503 if (ah->ah_caps.hw_caps & ATH9K_HW_CAP_BSSIDMASK) {
1504 ath9k_hw_getbssidmask(ah, sc->sc_bssidmask);
1505 ATH_SET_VAP_BSSID_MASK(sc->sc_bssidmask);
1506 ath9k_hw_setbssidmask(ah, sc->sc_bssidmask);
1507 }
1508
1509 sc->sc_slottime = ATH9K_SLOT_TIME_9; /* default to short slot time */
1510
1511 /* initialize beacon slots */
1512 for (i = 0; i < ARRAY_SIZE(sc->sc_bslot); i++)
1513 sc->sc_bslot[i] = ATH_IF_ID_ANY;
1514
1515 /* save MISC configurations */
1516 sc->sc_config.swBeaconProcess = 1;
1517
1518#ifdef CONFIG_SLOW_ANT_DIV
1519 /* range is 40 - 255, we use something in the middle */
1520 ath_slow_ant_div_init(&sc->sc_antdiv, sc, 0x127);
1521#endif
1522
1523 /* setup channels and rates */
1524
1525 sc->sbands[IEEE80211_BAND_2GHZ].channels =
1526 sc->channels[IEEE80211_BAND_2GHZ];
1527 sc->sbands[IEEE80211_BAND_2GHZ].bitrates =
1528 sc->rates[IEEE80211_BAND_2GHZ];
1529 sc->sbands[IEEE80211_BAND_2GHZ].band = IEEE80211_BAND_2GHZ;
1530
1531 if (test_bit(ATH9K_MODE_11A, sc->sc_ah->ah_caps.wireless_modes)) {
1532 sc->sbands[IEEE80211_BAND_5GHZ].channels =
1533 sc->channels[IEEE80211_BAND_5GHZ];
1534 sc->sbands[IEEE80211_BAND_5GHZ].bitrates =
1535 sc->rates[IEEE80211_BAND_5GHZ];
1536 sc->sbands[IEEE80211_BAND_5GHZ].band = IEEE80211_BAND_5GHZ;
1537 }
1538
1539 return 0;
1540bad2:
1541 /* cleanup tx queues */
1542 for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++)
1543 if (ATH_TXQ_SETUP(sc, i))
1544 ath_tx_cleanupq(sc, &sc->sc_txq[i]);
1545bad:
1546 if (ah)
1547 ath9k_hw_detach(ah);
1548
1549 return error;
1550}
1551
Sujith9c84b792008-10-29 10:17:13 +05301552static int ath_attach(u16 devid, struct ath_softc *sc)
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +05301553{
1554 struct ieee80211_hw *hw = sc->hw;
1555 int error = 0;
1556
Sujith04bd4632008-11-28 22:18:05 +05301557 DPRINTF(sc, ATH_DBG_CONFIG, "Attach ATH hw\n");
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +05301558
1559 error = ath_init(devid, sc);
1560 if (error != 0)
1561 return error;
1562
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +05301563 /* get mac address from hardware and set in mac80211 */
1564
1565 SET_IEEE80211_PERM_ADDR(hw, sc->sc_myaddr);
1566
Sujith9c84b792008-10-29 10:17:13 +05301567 hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
1568 IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
1569 IEEE80211_HW_SIGNAL_DBM |
1570 IEEE80211_HW_AMPDU_AGGREGATION;
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +05301571
Sujith9c84b792008-10-29 10:17:13 +05301572 hw->wiphy->interface_modes =
1573 BIT(NL80211_IFTYPE_AP) |
1574 BIT(NL80211_IFTYPE_STATION) |
1575 BIT(NL80211_IFTYPE_ADHOC);
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +05301576
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +05301577 hw->queues = 4;
Sujithe63835b2008-11-18 09:07:53 +05301578 hw->max_rates = 4;
1579 hw->max_rate_tries = ATH_11N_TXMAXTRY;
Sujith528f0c62008-10-29 10:14:26 +05301580 hw->sta_data_size = sizeof(struct ath_node);
Sujith5640b082008-10-29 10:16:06 +05301581 hw->vif_data_size = sizeof(struct ath_vap);
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +05301582
1583 /* Register rate control */
1584 hw->rate_control_algorithm = "ath9k_rate_control";
1585 error = ath_rate_control_register();
1586 if (error != 0) {
1587 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd4632008-11-28 22:18:05 +05301588 "Unable to register rate control algorithm: %d\n", error);
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +05301589 ath_rate_control_unregister();
1590 goto bad;
1591 }
1592
Sujith9c84b792008-10-29 10:17:13 +05301593 if (sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_HT) {
1594 setup_ht_cap(&sc->sbands[IEEE80211_BAND_2GHZ].ht_cap);
1595 if (test_bit(ATH9K_MODE_11A, sc->sc_ah->ah_caps.wireless_modes))
1596 setup_ht_cap(&sc->sbands[IEEE80211_BAND_5GHZ].ht_cap);
1597 }
1598
1599 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &sc->sbands[IEEE80211_BAND_2GHZ];
1600 if (test_bit(ATH9K_MODE_11A, sc->sc_ah->ah_caps.wireless_modes))
1601 hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
1602 &sc->sbands[IEEE80211_BAND_5GHZ];
1603
Senthil Balasubramaniandb93e7b2008-11-13 18:01:08 +05301604 /* initialize tx/rx engine */
1605 error = ath_tx_init(sc, ATH_TXBUF);
1606 if (error != 0)
1607 goto detach;
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +05301608
Senthil Balasubramaniandb93e7b2008-11-13 18:01:08 +05301609 error = ath_rx_init(sc, ATH_RXBUF);
1610 if (error != 0)
1611 goto detach;
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +05301612
Senthil Balasubramaniane97275c2008-11-13 18:00:02 +05301613#if defined(CONFIG_RFKILL) || defined(CONFIG_RFKILL_MODULE)
Vasanthakumar Thiagarajan500c0642008-09-10 18:50:17 +05301614 /* Initialze h/w Rfkill */
1615 if (sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
1616 INIT_DELAYED_WORK(&sc->rf_kill.rfkill_poll, ath_rfkill_poll);
1617
1618 /* Initialize s/w rfkill */
1619 if (ath_init_sw_rfkill(sc))
1620 goto detach;
1621#endif
1622
Senthil Balasubramaniandb93e7b2008-11-13 18:01:08 +05301623 error = ieee80211_register_hw(hw);
1624 if (error != 0) {
1625 ath_rate_control_unregister();
1626 goto bad;
1627 }
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +05301628
Senthil Balasubramaniandb93e7b2008-11-13 18:01:08 +05301629 /* Initialize LED control */
1630 ath_init_leds(sc);
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +05301631
1632 return 0;
1633detach:
1634 ath_detach(sc);
1635bad:
1636 return error;
1637}
1638
Sujithff37e332008-11-24 12:07:55 +05301639int ath_reset(struct ath_softc *sc, bool retry_tx)
1640{
1641 struct ath_hal *ah = sc->sc_ah;
1642 int status;
1643 int error = 0;
1644
1645 ath9k_hw_set_interrupts(ah, 0);
1646 ath_draintxq(sc, retry_tx);
1647 ath_stoprecv(sc);
1648 ath_flushrecv(sc);
1649
1650 spin_lock_bh(&sc->sc_resetlock);
1651 if (!ath9k_hw_reset(ah, sc->sc_ah->ah_curchan,
Sujith99405f92008-11-24 12:08:35 +05301652 sc->tx_chan_width,
Sujithff37e332008-11-24 12:07:55 +05301653 sc->sc_tx_chainmask, sc->sc_rx_chainmask,
1654 sc->sc_ht_extprotspacing, false, &status)) {
1655 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd4632008-11-28 22:18:05 +05301656 "Unable to reset hardware; hal status %u\n", status);
Sujithff37e332008-11-24 12:07:55 +05301657 error = -EIO;
1658 }
1659 spin_unlock_bh(&sc->sc_resetlock);
1660
1661 if (ath_startrecv(sc) != 0)
Sujith04bd4632008-11-28 22:18:05 +05301662 DPRINTF(sc, ATH_DBG_FATAL, "Unable to start recv logic\n");
Sujithff37e332008-11-24 12:07:55 +05301663
1664 /*
1665 * We may be doing a reset in response to a request
1666 * that changes the channel so update any state that
1667 * might change as a result.
1668 */
1669 ath_setcurmode(sc, ath_chan2mode(sc->sc_ah->ah_curchan));
1670
1671 ath_update_txpow(sc);
1672
1673 if (sc->sc_flags & SC_OP_BEACONS)
1674 ath_beacon_config(sc, ATH_IF_ID_ANY); /* restart beacons */
1675
1676 ath9k_hw_set_interrupts(ah, sc->sc_imask);
1677
1678 if (retry_tx) {
1679 int i;
1680 for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
1681 if (ATH_TXQ_SETUP(sc, i)) {
1682 spin_lock_bh(&sc->sc_txq[i].axq_lock);
1683 ath_txq_schedule(sc, &sc->sc_txq[i]);
1684 spin_unlock_bh(&sc->sc_txq[i].axq_lock);
1685 }
1686 }
1687 }
1688
1689 return error;
1690}
1691
1692/*
1693 * This function will allocate both the DMA descriptor structure, and the
1694 * buffers it contains. These are used to contain the descriptors used
1695 * by the system.
1696*/
1697int ath_descdma_setup(struct ath_softc *sc, struct ath_descdma *dd,
1698 struct list_head *head, const char *name,
1699 int nbuf, int ndesc)
1700{
1701#define DS2PHYS(_dd, _ds) \
1702 ((_dd)->dd_desc_paddr + ((caddr_t)(_ds) - (caddr_t)(_dd)->dd_desc))
1703#define ATH_DESC_4KB_BOUND_CHECK(_daddr) ((((_daddr) & 0xFFF) > 0xF7F) ? 1 : 0)
1704#define ATH_DESC_4KB_BOUND_NUM_SKIPPED(_len) ((_len) / 4096)
1705
1706 struct ath_desc *ds;
1707 struct ath_buf *bf;
1708 int i, bsize, error;
1709
Sujith04bd4632008-11-28 22:18:05 +05301710 DPRINTF(sc, ATH_DBG_CONFIG, "%s DMA: %u buffers %u desc/buf\n",
1711 name, nbuf, ndesc);
Sujithff37e332008-11-24 12:07:55 +05301712
1713 /* ath_desc must be a multiple of DWORDs */
1714 if ((sizeof(struct ath_desc) % 4) != 0) {
Sujith04bd4632008-11-28 22:18:05 +05301715 DPRINTF(sc, ATH_DBG_FATAL, "ath_desc not DWORD aligned\n");
Sujithff37e332008-11-24 12:07:55 +05301716 ASSERT((sizeof(struct ath_desc) % 4) == 0);
1717 error = -ENOMEM;
1718 goto fail;
1719 }
1720
1721 dd->dd_name = name;
1722 dd->dd_desc_len = sizeof(struct ath_desc) * nbuf * ndesc;
1723
1724 /*
1725 * Need additional DMA memory because we can't use
1726 * descriptors that cross the 4K page boundary. Assume
1727 * one skipped descriptor per 4K page.
1728 */
1729 if (!(sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_4KB_SPLITTRANS)) {
1730 u32 ndesc_skipped =
1731 ATH_DESC_4KB_BOUND_NUM_SKIPPED(dd->dd_desc_len);
1732 u32 dma_len;
1733
1734 while (ndesc_skipped) {
1735 dma_len = ndesc_skipped * sizeof(struct ath_desc);
1736 dd->dd_desc_len += dma_len;
1737
1738 ndesc_skipped = ATH_DESC_4KB_BOUND_NUM_SKIPPED(dma_len);
1739 };
1740 }
1741
1742 /* allocate descriptors */
1743 dd->dd_desc = pci_alloc_consistent(sc->pdev,
1744 dd->dd_desc_len,
1745 &dd->dd_desc_paddr);
1746 if (dd->dd_desc == NULL) {
1747 error = -ENOMEM;
1748 goto fail;
1749 }
1750 ds = dd->dd_desc;
Sujith04bd4632008-11-28 22:18:05 +05301751 DPRINTF(sc, ATH_DBG_CONFIG, "%s DMA map: %p (%u) -> %llx (%u)\n",
1752 dd->dd_name, ds, (u32) dd->dd_desc_len,
Sujithff37e332008-11-24 12:07:55 +05301753 ito64(dd->dd_desc_paddr), /*XXX*/(u32) dd->dd_desc_len);
1754
1755 /* allocate buffers */
1756 bsize = sizeof(struct ath_buf) * nbuf;
1757 bf = kmalloc(bsize, GFP_KERNEL);
1758 if (bf == NULL) {
1759 error = -ENOMEM;
1760 goto fail2;
1761 }
1762 memset(bf, 0, bsize);
1763 dd->dd_bufptr = bf;
1764
1765 INIT_LIST_HEAD(head);
1766 for (i = 0; i < nbuf; i++, bf++, ds += ndesc) {
1767 bf->bf_desc = ds;
1768 bf->bf_daddr = DS2PHYS(dd, ds);
1769
1770 if (!(sc->sc_ah->ah_caps.hw_caps &
1771 ATH9K_HW_CAP_4KB_SPLITTRANS)) {
1772 /*
1773 * Skip descriptor addresses which can cause 4KB
1774 * boundary crossing (addr + length) with a 32 dword
1775 * descriptor fetch.
1776 */
1777 while (ATH_DESC_4KB_BOUND_CHECK(bf->bf_daddr)) {
1778 ASSERT((caddr_t) bf->bf_desc <
1779 ((caddr_t) dd->dd_desc +
1780 dd->dd_desc_len));
1781
1782 ds += ndesc;
1783 bf->bf_desc = ds;
1784 bf->bf_daddr = DS2PHYS(dd, ds);
1785 }
1786 }
1787 list_add_tail(&bf->list, head);
1788 }
1789 return 0;
1790fail2:
1791 pci_free_consistent(sc->pdev,
1792 dd->dd_desc_len, dd->dd_desc, dd->dd_desc_paddr);
1793fail:
1794 memset(dd, 0, sizeof(*dd));
1795 return error;
1796#undef ATH_DESC_4KB_BOUND_CHECK
1797#undef ATH_DESC_4KB_BOUND_NUM_SKIPPED
1798#undef DS2PHYS
1799}
1800
1801void ath_descdma_cleanup(struct ath_softc *sc,
1802 struct ath_descdma *dd,
1803 struct list_head *head)
1804{
1805 pci_free_consistent(sc->pdev,
1806 dd->dd_desc_len, dd->dd_desc, dd->dd_desc_paddr);
1807
1808 INIT_LIST_HEAD(head);
1809 kfree(dd->dd_bufptr);
1810 memset(dd, 0, sizeof(*dd));
1811}
1812
1813int ath_get_hal_qnum(u16 queue, struct ath_softc *sc)
1814{
1815 int qnum;
1816
1817 switch (queue) {
1818 case 0:
1819 qnum = sc->sc_haltype2q[ATH9K_WME_AC_VO];
1820 break;
1821 case 1:
1822 qnum = sc->sc_haltype2q[ATH9K_WME_AC_VI];
1823 break;
1824 case 2:
1825 qnum = sc->sc_haltype2q[ATH9K_WME_AC_BE];
1826 break;
1827 case 3:
1828 qnum = sc->sc_haltype2q[ATH9K_WME_AC_BK];
1829 break;
1830 default:
1831 qnum = sc->sc_haltype2q[ATH9K_WME_AC_BE];
1832 break;
1833 }
1834
1835 return qnum;
1836}
1837
1838int ath_get_mac80211_qnum(u32 queue, struct ath_softc *sc)
1839{
1840 int qnum;
1841
1842 switch (queue) {
1843 case ATH9K_WME_AC_VO:
1844 qnum = 0;
1845 break;
1846 case ATH9K_WME_AC_VI:
1847 qnum = 1;
1848 break;
1849 case ATH9K_WME_AC_BE:
1850 qnum = 2;
1851 break;
1852 case ATH9K_WME_AC_BK:
1853 qnum = 3;
1854 break;
1855 default:
1856 qnum = -1;
1857 break;
1858 }
1859
1860 return qnum;
1861}
1862
1863/**********************/
1864/* mac80211 callbacks */
1865/**********************/
1866
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001867static int ath9k_start(struct ieee80211_hw *hw)
1868{
1869 struct ath_softc *sc = hw->priv;
1870 struct ieee80211_channel *curchan = hw->conf.channel;
Sujithff37e332008-11-24 12:07:55 +05301871 struct ath9k_channel *init_channel;
1872 int error = 0, pos, status;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001873
Sujith04bd4632008-11-28 22:18:05 +05301874 DPRINTF(sc, ATH_DBG_CONFIG, "Starting driver with "
1875 "initial channel: %d MHz\n", curchan->center_freq);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001876
1877 /* setup initial channel */
1878
1879 pos = ath_get_channel(sc, curchan);
1880 if (pos == -1) {
Sujith04bd4632008-11-28 22:18:05 +05301881 DPRINTF(sc, ATH_DBG_FATAL, "Invalid channel: %d\n", curchan->center_freq);
Sujith9c84b792008-10-29 10:17:13 +05301882 error = -EINVAL;
Sujithff37e332008-11-24 12:07:55 +05301883 goto error;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001884 }
1885
Sujith99405f92008-11-24 12:08:35 +05301886 sc->tx_chan_width = ATH9K_HT_MACMODE_20;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001887 sc->sc_ah->ah_channels[pos].chanmode =
1888 (curchan->band == IEEE80211_BAND_2GHZ) ? CHANNEL_G : CHANNEL_A;
Sujithff37e332008-11-24 12:07:55 +05301889 init_channel = &sc->sc_ah->ah_channels[pos];
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001890
Sujithff37e332008-11-24 12:07:55 +05301891 /* Reset SERDES registers */
1892 ath9k_hw_configpcipowersave(sc->sc_ah, 0);
1893
1894 /*
1895 * The basic interface to setting the hardware in a good
1896 * state is ``reset''. On return the hardware is known to
1897 * be powered up and with interrupts disabled. This must
1898 * be followed by initialization of the appropriate bits
1899 * and then setup of the interrupt mask.
1900 */
1901 spin_lock_bh(&sc->sc_resetlock);
1902 if (!ath9k_hw_reset(sc->sc_ah, init_channel,
Sujith99405f92008-11-24 12:08:35 +05301903 sc->tx_chan_width,
Sujithff37e332008-11-24 12:07:55 +05301904 sc->sc_tx_chainmask, sc->sc_rx_chainmask,
1905 sc->sc_ht_extprotspacing, false, &status)) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001906 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd4632008-11-28 22:18:05 +05301907 "Unable to reset hardware; hal status %u "
1908 "(freq %u flags 0x%x)\n", status,
Sujithff37e332008-11-24 12:07:55 +05301909 init_channel->channel, init_channel->channelFlags);
1910 error = -EIO;
1911 spin_unlock_bh(&sc->sc_resetlock);
1912 goto error;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001913 }
Sujithff37e332008-11-24 12:07:55 +05301914 spin_unlock_bh(&sc->sc_resetlock);
1915
1916 /*
1917 * This is needed only to setup initial state
1918 * but it's best done after a reset.
1919 */
1920 ath_update_txpow(sc);
1921
1922 /*
1923 * Setup the hardware after reset:
1924 * The receive engine is set going.
1925 * Frame transmit is handled entirely
1926 * in the frame output path; there's nothing to do
1927 * here except setup the interrupt mask.
1928 */
1929 if (ath_startrecv(sc) != 0) {
1930 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd4632008-11-28 22:18:05 +05301931 "Unable to start recv logic\n");
Sujithff37e332008-11-24 12:07:55 +05301932 error = -EIO;
1933 goto error;
1934 }
1935
1936 /* Setup our intr mask. */
1937 sc->sc_imask = ATH9K_INT_RX | ATH9K_INT_TX
1938 | ATH9K_INT_RXEOL | ATH9K_INT_RXORN
1939 | ATH9K_INT_FATAL | ATH9K_INT_GLOBAL;
1940
1941 if (sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_GTT)
1942 sc->sc_imask |= ATH9K_INT_GTT;
1943
1944 if (sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_HT)
1945 sc->sc_imask |= ATH9K_INT_CST;
1946
1947 /*
1948 * Enable MIB interrupts when there are hardware phy counters.
1949 * Note we only do this (at the moment) for station mode.
1950 */
1951 if (ath9k_hw_phycounters(sc->sc_ah) &&
1952 ((sc->sc_ah->ah_opmode == ATH9K_M_STA) ||
1953 (sc->sc_ah->ah_opmode == ATH9K_M_IBSS)))
1954 sc->sc_imask |= ATH9K_INT_MIB;
1955 /*
1956 * Some hardware processes the TIM IE and fires an
1957 * interrupt when the TIM bit is set. For hardware
1958 * that does, if not overridden by configuration,
1959 * enable the TIM interrupt when operating as station.
1960 */
1961 if ((sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_ENHANCEDPM) &&
1962 (sc->sc_ah->ah_opmode == ATH9K_M_STA) &&
1963 !sc->sc_config.swBeaconProcess)
1964 sc->sc_imask |= ATH9K_INT_TIM;
1965
1966 ath_setcurmode(sc, ath_chan2mode(init_channel));
1967
1968 sc->sc_flags &= ~SC_OP_INVALID;
1969
1970 /* Disable BMISS interrupt when we're not associated */
1971 sc->sc_imask &= ~(ATH9K_INT_SWBA | ATH9K_INT_BMISS);
1972 ath9k_hw_set_interrupts(sc->sc_ah, sc->sc_imask);
1973
1974 ieee80211_wake_queues(sc->hw);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001975
Senthil Balasubramaniane97275c2008-11-13 18:00:02 +05301976#if defined(CONFIG_RFKILL) || defined(CONFIG_RFKILL_MODULE)
Sujith9c84b792008-10-29 10:17:13 +05301977 error = ath_start_rfkill_poll(sc);
Vasanthakumar Thiagarajan500c0642008-09-10 18:50:17 +05301978#endif
1979
Sujithff37e332008-11-24 12:07:55 +05301980error:
Sujith9c84b792008-10-29 10:17:13 +05301981 return error;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001982}
1983
1984static int ath9k_tx(struct ieee80211_hw *hw,
1985 struct sk_buff *skb)
1986{
Jouni Malinen147583c2008-08-11 14:01:50 +03001987 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Sujith528f0c62008-10-29 10:14:26 +05301988 struct ath_softc *sc = hw->priv;
1989 struct ath_tx_control txctl;
1990 int hdrlen, padsize;
1991
1992 memset(&txctl, 0, sizeof(struct ath_tx_control));
Jouni Malinen147583c2008-08-11 14:01:50 +03001993
1994 /*
1995 * As a temporary workaround, assign seq# here; this will likely need
1996 * to be cleaned up to work better with Beacon transmission and virtual
1997 * BSSes.
1998 */
1999 if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
2000 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
2001 if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
2002 sc->seq_no += 0x10;
2003 hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
2004 hdr->seq_ctrl |= cpu_to_le16(sc->seq_no);
2005 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002006
2007 /* Add the padding after the header if this is not already done */
2008 hdrlen = ieee80211_get_hdrlen_from_skb(skb);
2009 if (hdrlen & 3) {
2010 padsize = hdrlen % 4;
2011 if (skb_headroom(skb) < padsize)
2012 return -1;
2013 skb_push(skb, padsize);
2014 memmove(skb->data, skb->data + padsize, hdrlen);
2015 }
2016
Sujith528f0c62008-10-29 10:14:26 +05302017 /* Check if a tx queue is available */
2018
2019 txctl.txq = ath_test_get_txq(sc, skb);
2020 if (!txctl.txq)
2021 goto exit;
2022
Sujith04bd4632008-11-28 22:18:05 +05302023 DPRINTF(sc, ATH_DBG_XMIT, "transmitting packet, skb: %p\n", skb);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002024
Sujith528f0c62008-10-29 10:14:26 +05302025 if (ath_tx_start(sc, skb, &txctl) != 0) {
Sujith04bd4632008-11-28 22:18:05 +05302026 DPRINTF(sc, ATH_DBG_XMIT, "TX failed\n");
Sujith528f0c62008-10-29 10:14:26 +05302027 goto exit;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002028 }
2029
2030 return 0;
Sujith528f0c62008-10-29 10:14:26 +05302031exit:
2032 dev_kfree_skb_any(skb);
2033 return 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002034}
2035
2036static void ath9k_stop(struct ieee80211_hw *hw)
2037{
2038 struct ath_softc *sc = hw->priv;
Sujith9c84b792008-10-29 10:17:13 +05302039
2040 if (sc->sc_flags & SC_OP_INVALID) {
Sujith04bd4632008-11-28 22:18:05 +05302041 DPRINTF(sc, ATH_DBG_ANY, "Device not present\n");
Sujith9c84b792008-10-29 10:17:13 +05302042 return;
2043 }
2044
Sujith04bd4632008-11-28 22:18:05 +05302045 DPRINTF(sc, ATH_DBG_CONFIG, "Cleaning up\n");
Sujithff37e332008-11-24 12:07:55 +05302046
2047 ieee80211_stop_queues(sc->hw);
2048
2049 /* make sure h/w will not generate any interrupt
2050 * before setting the invalid flag. */
2051 ath9k_hw_set_interrupts(sc->sc_ah, 0);
2052
2053 if (!(sc->sc_flags & SC_OP_INVALID)) {
2054 ath_draintxq(sc, false);
2055 ath_stoprecv(sc);
2056 ath9k_hw_phy_disable(sc->sc_ah);
2057 } else
2058 sc->sc_rxlink = NULL;
2059
2060#if defined(CONFIG_RFKILL) || defined(CONFIG_RFKILL_MODULE)
2061 if (sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
2062 cancel_delayed_work_sync(&sc->rf_kill.rfkill_poll);
2063#endif
2064 /* disable HAL and put h/w to sleep */
2065 ath9k_hw_disable(sc->sc_ah);
2066 ath9k_hw_configpcipowersave(sc->sc_ah, 1);
2067
2068 sc->sc_flags |= SC_OP_INVALID;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002069
Sujith04bd4632008-11-28 22:18:05 +05302070 DPRINTF(sc, ATH_DBG_CONFIG, "Driver halt\n");
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002071}
2072
2073static int ath9k_add_interface(struct ieee80211_hw *hw,
2074 struct ieee80211_if_init_conf *conf)
2075{
2076 struct ath_softc *sc = hw->priv;
Sujith5640b082008-10-29 10:16:06 +05302077 struct ath_vap *avp = (void *)conf->vif->drv_priv;
2078 int ic_opmode = 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002079
2080 /* Support only vap for now */
2081
2082 if (sc->sc_nvaps)
2083 return -ENOBUFS;
2084
2085 switch (conf->type) {
Johannes Berg05c914f2008-09-11 00:01:58 +02002086 case NL80211_IFTYPE_STATION:
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002087 ic_opmode = ATH9K_M_STA;
2088 break;
Johannes Berg05c914f2008-09-11 00:01:58 +02002089 case NL80211_IFTYPE_ADHOC:
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002090 ic_opmode = ATH9K_M_IBSS;
2091 break;
Johannes Berg05c914f2008-09-11 00:01:58 +02002092 case NL80211_IFTYPE_AP:
Jouni Malinen2ad67de2008-08-11 14:01:47 +03002093 ic_opmode = ATH9K_M_HOSTAP;
2094 break;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002095 default:
2096 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd4632008-11-28 22:18:05 +05302097 "Interface type %d not yet supported\n", conf->type);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002098 return -EOPNOTSUPP;
2099 }
2100
Sujith04bd4632008-11-28 22:18:05 +05302101 DPRINTF(sc, ATH_DBG_CONFIG, "Attach a VAP of type: %d\n", ic_opmode);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002102
Sujith5640b082008-10-29 10:16:06 +05302103 /* Set the VAP opmode */
2104 avp->av_opmode = ic_opmode;
2105 avp->av_bslot = -1;
2106
2107 if (ic_opmode == ATH9K_M_HOSTAP)
2108 ath9k_hw_set_tsfadjust(sc->sc_ah, 1);
2109
2110 sc->sc_vaps[0] = conf->vif;
2111 sc->sc_nvaps++;
2112
2113 /* Set the device opmode */
2114 sc->sc_ah->ah_opmode = ic_opmode;
2115
Luis R. Rodriguez6f255422008-10-03 15:45:27 -07002116 if (conf->type == NL80211_IFTYPE_AP) {
2117 /* TODO: is this a suitable place to start ANI for AP mode? */
2118 /* Start ANI */
2119 mod_timer(&sc->sc_ani.timer,
2120 jiffies + msecs_to_jiffies(ATH_ANI_POLLINTERVAL));
2121 }
2122
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002123 return 0;
2124}
2125
2126static void ath9k_remove_interface(struct ieee80211_hw *hw,
2127 struct ieee80211_if_init_conf *conf)
2128{
2129 struct ath_softc *sc = hw->priv;
Sujith5640b082008-10-29 10:16:06 +05302130 struct ath_vap *avp = (void *)conf->vif->drv_priv;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002131
Sujith04bd4632008-11-28 22:18:05 +05302132 DPRINTF(sc, ATH_DBG_CONFIG, "Detach Interface\n");
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002133
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002134#ifdef CONFIG_SLOW_ANT_DIV
2135 ath_slow_ant_div_stop(&sc->sc_antdiv);
2136#endif
Luis R. Rodriguez6f255422008-10-03 15:45:27 -07002137 /* Stop ANI */
2138 del_timer_sync(&sc->sc_ani.timer);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002139
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002140 /* Reclaim beacon resources */
Sujithb4696c8b2008-08-11 14:04:52 +05302141 if (sc->sc_ah->ah_opmode == ATH9K_M_HOSTAP ||
2142 sc->sc_ah->ah_opmode == ATH9K_M_IBSS) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002143 ath9k_hw_stoptxdma(sc->sc_ah, sc->sc_bhalq);
2144 ath_beacon_return(sc, avp);
2145 }
2146
Sujith672840a2008-08-11 14:05:08 +05302147 sc->sc_flags &= ~SC_OP_BEACONS;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002148
Sujith5640b082008-10-29 10:16:06 +05302149 sc->sc_vaps[0] = NULL;
2150 sc->sc_nvaps--;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002151}
2152
Johannes Berge8975582008-10-09 12:18:51 +02002153static int ath9k_config(struct ieee80211_hw *hw, u32 changed)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002154{
2155 struct ath_softc *sc = hw->priv;
Johannes Berge8975582008-10-09 12:18:51 +02002156 struct ieee80211_conf *conf = &hw->conf;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002157
Sujith99405f92008-11-24 12:08:35 +05302158 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
2159 struct ieee80211_channel *curchan = hw->conf.channel;
2160 int pos;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002161
Sujith04bd4632008-11-28 22:18:05 +05302162 DPRINTF(sc, ATH_DBG_CONFIG, "Set channel: %d MHz\n",
2163 curchan->center_freq);
Johannes Bergae5eb022008-10-14 16:58:37 +02002164
Sujith99405f92008-11-24 12:08:35 +05302165 pos = ath_get_channel(sc, curchan);
2166 if (pos == -1) {
Sujith04bd4632008-11-28 22:18:05 +05302167 DPRINTF(sc, ATH_DBG_FATAL, "Invalid channel: %d\n",
2168 curchan->center_freq);
Sujith99405f92008-11-24 12:08:35 +05302169 return -EINVAL;
2170 }
2171
2172 sc->tx_chan_width = ATH9K_HT_MACMODE_20;
2173 sc->sc_ah->ah_channels[pos].chanmode =
2174 (curchan->band == IEEE80211_BAND_2GHZ) ?
2175 CHANNEL_G : CHANNEL_A;
2176
Sujithe11602b2008-11-27 09:46:27 +05302177 if ((sc->sc_ah->ah_opmode == ATH9K_M_HOSTAP) &&
2178 (conf->ht.enabled)) {
2179 sc->tx_chan_width = (!!conf->ht.sec_chan_offset) ?
2180 ATH9K_HT_MACMODE_2040 : ATH9K_HT_MACMODE_20;
2181
2182 sc->sc_ah->ah_channels[pos].chanmode =
2183 ath_get_extchanmode(sc, curchan,
2184 conf->ht.sec_chan_offset,
2185 sc->tx_chan_width);
2186 }
2187
2188 if (ath_set_channel(sc, &sc->sc_ah->ah_channels[pos]) < 0) {
Sujith04bd4632008-11-28 22:18:05 +05302189 DPRINTF(sc, ATH_DBG_FATAL, "Unable to set channel\n");
Sujithe11602b2008-11-27 09:46:27 +05302190 return -EINVAL;
2191 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002192 }
2193
Sujith99405f92008-11-24 12:08:35 +05302194 if (changed & IEEE80211_CONF_CHANGE_HT)
2195 ath_update_chainmask(sc, conf->ht.enabled);
Sujith86b89ee2008-08-07 10:54:57 +05302196
Luis R. Rodriguez5c020dc2008-10-22 13:28:45 -07002197 if (changed & IEEE80211_CONF_CHANGE_POWER)
2198 sc->sc_config.txpowlimit = 2 * conf->power_level;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002199
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002200 return 0;
2201}
2202
2203static int ath9k_config_interface(struct ieee80211_hw *hw,
2204 struct ieee80211_vif *vif,
2205 struct ieee80211_if_conf *conf)
2206{
2207 struct ath_softc *sc = hw->priv;
Jouni Malinen2ad67de2008-08-11 14:01:47 +03002208 struct ath_hal *ah = sc->sc_ah;
Sujith5640b082008-10-29 10:16:06 +05302209 struct ath_vap *avp = (void *)vif->drv_priv;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002210 u32 rfilt = 0;
2211 int error, i;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002212
Jouni Malinen2ad67de2008-08-11 14:01:47 +03002213 /* TODO: Need to decide which hw opmode to use for multi-interface
2214 * cases */
Johannes Berg05c914f2008-09-11 00:01:58 +02002215 if (vif->type == NL80211_IFTYPE_AP &&
Jouni Malinen2ad67de2008-08-11 14:01:47 +03002216 ah->ah_opmode != ATH9K_M_HOSTAP) {
2217 ah->ah_opmode = ATH9K_M_HOSTAP;
2218 ath9k_hw_setopmode(ah);
2219 ath9k_hw_write_associd(ah, sc->sc_myaddr, 0);
2220 /* Request full reset to get hw opmode changed properly */
2221 sc->sc_flags |= SC_OP_FULL_RESET;
2222 }
2223
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002224 if ((conf->changed & IEEE80211_IFCC_BSSID) &&
2225 !is_zero_ether_addr(conf->bssid)) {
2226 switch (vif->type) {
Johannes Berg05c914f2008-09-11 00:01:58 +02002227 case NL80211_IFTYPE_STATION:
2228 case NL80211_IFTYPE_ADHOC:
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002229 /* Set BSSID */
2230 memcpy(sc->sc_curbssid, conf->bssid, ETH_ALEN);
2231 sc->sc_curaid = 0;
2232 ath9k_hw_write_associd(sc->sc_ah, sc->sc_curbssid,
2233 sc->sc_curaid);
2234
2235 /* Set aggregation protection mode parameters */
2236 sc->sc_config.ath_aggr_prot = 0;
2237
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002238 DPRINTF(sc, ATH_DBG_CONFIG,
Sujith04bd4632008-11-28 22:18:05 +05302239 "RX filter 0x%x bssid %pM aid 0x%x\n",
2240 rfilt, sc->sc_curbssid, sc->sc_curaid);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002241
2242 /* need to reconfigure the beacon */
Sujith672840a2008-08-11 14:05:08 +05302243 sc->sc_flags &= ~SC_OP_BEACONS ;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002244
2245 break;
2246 default:
2247 break;
2248 }
2249 }
2250
2251 if ((conf->changed & IEEE80211_IFCC_BEACON) &&
Johannes Berg05c914f2008-09-11 00:01:58 +02002252 ((vif->type == NL80211_IFTYPE_ADHOC) ||
2253 (vif->type == NL80211_IFTYPE_AP))) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002254 /*
2255 * Allocate and setup the beacon frame.
2256 *
2257 * Stop any previous beacon DMA. This may be
2258 * necessary, for example, when an ibss merge
2259 * causes reconfiguration; we may be called
2260 * with beacon transmission active.
2261 */
2262 ath9k_hw_stoptxdma(sc->sc_ah, sc->sc_bhalq);
2263
2264 error = ath_beacon_alloc(sc, 0);
2265 if (error != 0)
2266 return error;
2267
2268 ath_beacon_sync(sc, 0);
2269 }
2270
2271 /* Check for WLAN_CAPABILITY_PRIVACY ? */
Sujith5640b082008-10-29 10:16:06 +05302272 if ((avp->av_opmode != ATH9K_M_STA)) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002273 for (i = 0; i < IEEE80211_WEP_NKID; i++)
2274 if (ath9k_hw_keyisvalid(sc->sc_ah, (u16)i))
2275 ath9k_hw_keysetmac(sc->sc_ah,
2276 (u16)i,
2277 sc->sc_curbssid);
2278 }
2279
2280 /* Only legacy IBSS for now */
Johannes Berg05c914f2008-09-11 00:01:58 +02002281 if (vif->type == NL80211_IFTYPE_ADHOC)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002282 ath_update_chainmask(sc, 0);
2283
2284 return 0;
2285}
2286
2287#define SUPPORTED_FILTERS \
2288 (FIF_PROMISC_IN_BSS | \
2289 FIF_ALLMULTI | \
2290 FIF_CONTROL | \
2291 FIF_OTHER_BSS | \
2292 FIF_BCN_PRBRESP_PROMISC | \
2293 FIF_FCSFAIL)
2294
Sujith7dcfdcd2008-08-11 14:03:13 +05302295/* FIXME: sc->sc_full_reset ? */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002296static void ath9k_configure_filter(struct ieee80211_hw *hw,
2297 unsigned int changed_flags,
2298 unsigned int *total_flags,
2299 int mc_count,
2300 struct dev_mc_list *mclist)
2301{
2302 struct ath_softc *sc = hw->priv;
Sujith7dcfdcd2008-08-11 14:03:13 +05302303 u32 rfilt;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002304
2305 changed_flags &= SUPPORTED_FILTERS;
2306 *total_flags &= SUPPORTED_FILTERS;
2307
Sujith7dcfdcd2008-08-11 14:03:13 +05302308 sc->rx_filter = *total_flags;
2309 rfilt = ath_calcrxfilter(sc);
2310 ath9k_hw_setrxfilter(sc->sc_ah, rfilt);
2311
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002312 if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
2313 if (*total_flags & FIF_BCN_PRBRESP_PROMISC)
Sujith7dcfdcd2008-08-11 14:03:13 +05302314 ath9k_hw_write_associd(sc->sc_ah, ath_bcast_mac, 0);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002315 }
Sujith7dcfdcd2008-08-11 14:03:13 +05302316
Sujith04bd4632008-11-28 22:18:05 +05302317 DPRINTF(sc, ATH_DBG_CONFIG, "Set HW RX filter: 0x%x\n", sc->rx_filter);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002318}
2319
2320static void ath9k_sta_notify(struct ieee80211_hw *hw,
2321 struct ieee80211_vif *vif,
2322 enum sta_notify_cmd cmd,
Johannes Berg17741cd2008-09-11 00:02:02 +02002323 struct ieee80211_sta *sta)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002324{
2325 struct ath_softc *sc = hw->priv;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002326
2327 switch (cmd) {
2328 case STA_NOTIFY_ADD:
Sujith5640b082008-10-29 10:16:06 +05302329 ath_node_attach(sc, sta);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002330 break;
2331 case STA_NOTIFY_REMOVE:
Sujithb5aa9bf2008-10-29 10:13:31 +05302332 ath_node_detach(sc, sta);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002333 break;
2334 default:
2335 break;
2336 }
2337}
2338
2339static int ath9k_conf_tx(struct ieee80211_hw *hw,
2340 u16 queue,
2341 const struct ieee80211_tx_queue_params *params)
2342{
2343 struct ath_softc *sc = hw->priv;
Sujithea9880f2008-08-07 10:53:10 +05302344 struct ath9k_tx_queue_info qi;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002345 int ret = 0, qnum;
2346
2347 if (queue >= WME_NUM_AC)
2348 return 0;
2349
2350 qi.tqi_aifs = params->aifs;
2351 qi.tqi_cwmin = params->cw_min;
2352 qi.tqi_cwmax = params->cw_max;
2353 qi.tqi_burstTime = params->txop;
2354 qnum = ath_get_hal_qnum(queue, sc);
2355
2356 DPRINTF(sc, ATH_DBG_CONFIG,
Sujith04bd4632008-11-28 22:18:05 +05302357 "Configure tx [queue/halq] [%d/%d], "
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002358 "aifs: %d, cw_min: %d, cw_max: %d, txop: %d\n",
Sujith04bd4632008-11-28 22:18:05 +05302359 queue, qnum, params->aifs, params->cw_min,
2360 params->cw_max, params->txop);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002361
2362 ret = ath_txq_update(sc, qnum, &qi);
2363 if (ret)
Sujith04bd4632008-11-28 22:18:05 +05302364 DPRINTF(sc, ATH_DBG_FATAL, "TXQ Update failed\n");
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002365
2366 return ret;
2367}
2368
2369static int ath9k_set_key(struct ieee80211_hw *hw,
2370 enum set_key_cmd cmd,
2371 const u8 *local_addr,
2372 const u8 *addr,
2373 struct ieee80211_key_conf *key)
2374{
2375 struct ath_softc *sc = hw->priv;
2376 int ret = 0;
2377
Sujith04bd4632008-11-28 22:18:05 +05302378 DPRINTF(sc, ATH_DBG_KEYCACHE, "Set HW Key\n");
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002379
2380 switch (cmd) {
2381 case SET_KEY:
2382 ret = ath_key_config(sc, addr, key);
2383 if (!ret) {
2384 set_bit(key->keyidx, sc->sc_keymap);
2385 key->hw_key_idx = key->keyidx;
2386 /* push IV and Michael MIC generation to stack */
2387 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
Senthil Balasubramanian1b961752008-09-01 19:45:21 +05302388 if (key->alg == ALG_TKIP)
2389 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002390 }
2391 break;
2392 case DISABLE_KEY:
2393 ath_key_delete(sc, key);
2394 clear_bit(key->keyidx, sc->sc_keymap);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002395 break;
2396 default:
2397 ret = -EINVAL;
2398 }
2399
2400 return ret;
2401}
2402
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002403static void ath9k_bss_info_changed(struct ieee80211_hw *hw,
2404 struct ieee80211_vif *vif,
2405 struct ieee80211_bss_conf *bss_conf,
2406 u32 changed)
2407{
2408 struct ath_softc *sc = hw->priv;
2409
2410 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
Sujith04bd4632008-11-28 22:18:05 +05302411 DPRINTF(sc, ATH_DBG_CONFIG, "BSS Changed PREAMBLE %d\n",
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002412 bss_conf->use_short_preamble);
2413 if (bss_conf->use_short_preamble)
Sujith672840a2008-08-11 14:05:08 +05302414 sc->sc_flags |= SC_OP_PREAMBLE_SHORT;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002415 else
Sujith672840a2008-08-11 14:05:08 +05302416 sc->sc_flags &= ~SC_OP_PREAMBLE_SHORT;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002417 }
2418
2419 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
Sujith04bd4632008-11-28 22:18:05 +05302420 DPRINTF(sc, ATH_DBG_CONFIG, "BSS Changed CTS PROT %d\n",
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002421 bss_conf->use_cts_prot);
2422 if (bss_conf->use_cts_prot &&
2423 hw->conf.channel->band != IEEE80211_BAND_5GHZ)
Sujith672840a2008-08-11 14:05:08 +05302424 sc->sc_flags |= SC_OP_PROTECT_ENABLE;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002425 else
Sujith672840a2008-08-11 14:05:08 +05302426 sc->sc_flags &= ~SC_OP_PROTECT_ENABLE;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002427 }
2428
Sujith99405f92008-11-24 12:08:35 +05302429 if (changed & BSS_CHANGED_HT)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002430 ath9k_ht_conf(sc, bss_conf);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002431
2432 if (changed & BSS_CHANGED_ASSOC) {
Sujith04bd4632008-11-28 22:18:05 +05302433 DPRINTF(sc, ATH_DBG_CONFIG, "BSS Changed ASSOC %d\n",
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002434 bss_conf->assoc);
Sujith5640b082008-10-29 10:16:06 +05302435 ath9k_bss_assoc_info(sc, vif, bss_conf);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002436 }
2437}
2438
2439static u64 ath9k_get_tsf(struct ieee80211_hw *hw)
2440{
2441 u64 tsf;
2442 struct ath_softc *sc = hw->priv;
2443 struct ath_hal *ah = sc->sc_ah;
2444
2445 tsf = ath9k_hw_gettsf64(ah);
2446
2447 return tsf;
2448}
2449
2450static void ath9k_reset_tsf(struct ieee80211_hw *hw)
2451{
2452 struct ath_softc *sc = hw->priv;
2453 struct ath_hal *ah = sc->sc_ah;
2454
2455 ath9k_hw_reset_tsf(ah);
2456}
2457
2458static int ath9k_ampdu_action(struct ieee80211_hw *hw,
2459 enum ieee80211_ampdu_mlme_action action,
Johannes Berg17741cd2008-09-11 00:02:02 +02002460 struct ieee80211_sta *sta,
2461 u16 tid, u16 *ssn)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002462{
2463 struct ath_softc *sc = hw->priv;
2464 int ret = 0;
2465
2466 switch (action) {
2467 case IEEE80211_AMPDU_RX_START:
Sujithdca3edb2008-10-29 10:19:01 +05302468 if (!(sc->sc_flags & SC_OP_RXAGGR))
2469 ret = -ENOTSUPP;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002470 break;
2471 case IEEE80211_AMPDU_RX_STOP:
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002472 break;
2473 case IEEE80211_AMPDU_TX_START:
Sujithb5aa9bf2008-10-29 10:13:31 +05302474 ret = ath_tx_aggr_start(sc, sta, tid, ssn);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002475 if (ret < 0)
2476 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd4632008-11-28 22:18:05 +05302477 "Unable to start TX aggregation\n");
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002478 else
Johannes Berg17741cd2008-09-11 00:02:02 +02002479 ieee80211_start_tx_ba_cb_irqsafe(hw, sta->addr, tid);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002480 break;
2481 case IEEE80211_AMPDU_TX_STOP:
Sujithb5aa9bf2008-10-29 10:13:31 +05302482 ret = ath_tx_aggr_stop(sc, sta, tid);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002483 if (ret < 0)
2484 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd4632008-11-28 22:18:05 +05302485 "Unable to stop TX aggregation\n");
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002486
Johannes Berg17741cd2008-09-11 00:02:02 +02002487 ieee80211_stop_tx_ba_cb_irqsafe(hw, sta->addr, tid);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002488 break;
Sujith8469cde2008-10-29 10:19:28 +05302489 case IEEE80211_AMPDU_TX_RESUME:
2490 ath_tx_aggr_resume(sc, sta, tid);
2491 break;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002492 default:
Sujith04bd4632008-11-28 22:18:05 +05302493 DPRINTF(sc, ATH_DBG_FATAL, "Unknown AMPDU action\n");
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002494 }
2495
2496 return ret;
2497}
2498
Johannes Berg4233df62008-10-13 13:35:05 +02002499static int ath9k_no_fragmentation(struct ieee80211_hw *hw, u32 value)
2500{
2501 return -EOPNOTSUPP;
2502}
2503
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002504static struct ieee80211_ops ath9k_ops = {
2505 .tx = ath9k_tx,
2506 .start = ath9k_start,
2507 .stop = ath9k_stop,
2508 .add_interface = ath9k_add_interface,
2509 .remove_interface = ath9k_remove_interface,
2510 .config = ath9k_config,
2511 .config_interface = ath9k_config_interface,
2512 .configure_filter = ath9k_configure_filter,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002513 .sta_notify = ath9k_sta_notify,
2514 .conf_tx = ath9k_conf_tx,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002515 .bss_info_changed = ath9k_bss_info_changed,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002516 .set_key = ath9k_set_key,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002517 .get_tsf = ath9k_get_tsf,
2518 .reset_tsf = ath9k_reset_tsf,
Johannes Berg4233df62008-10-13 13:35:05 +02002519 .ampdu_action = ath9k_ampdu_action,
2520 .set_frag_threshold = ath9k_no_fragmentation,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002521};
2522
Benoit PAPILLAULT392dff82008-11-06 22:26:49 +01002523static struct {
2524 u32 version;
2525 const char * name;
2526} ath_mac_bb_names[] = {
2527 { AR_SREV_VERSION_5416_PCI, "5416" },
2528 { AR_SREV_VERSION_5416_PCIE, "5418" },
2529 { AR_SREV_VERSION_9100, "9100" },
2530 { AR_SREV_VERSION_9160, "9160" },
2531 { AR_SREV_VERSION_9280, "9280" },
2532 { AR_SREV_VERSION_9285, "9285" }
2533};
2534
2535static struct {
2536 u16 version;
2537 const char * name;
2538} ath_rf_names[] = {
2539 { 0, "5133" },
2540 { AR_RAD5133_SREV_MAJOR, "5133" },
2541 { AR_RAD5122_SREV_MAJOR, "5122" },
2542 { AR_RAD2133_SREV_MAJOR, "2133" },
2543 { AR_RAD2122_SREV_MAJOR, "2122" }
2544};
2545
2546/*
2547 * Return the MAC/BB name. "????" is returned if the MAC/BB is unknown.
2548 */
Benoit PAPILLAULT392dff82008-11-06 22:26:49 +01002549static const char *
2550ath_mac_bb_name(u32 mac_bb_version)
2551{
2552 int i;
2553
2554 for (i=0; i<ARRAY_SIZE(ath_mac_bb_names); i++) {
2555 if (ath_mac_bb_names[i].version == mac_bb_version) {
2556 return ath_mac_bb_names[i].name;
2557 }
2558 }
2559
2560 return "????";
2561}
2562
2563/*
2564 * Return the RF name. "????" is returned if the RF is unknown.
2565 */
Benoit PAPILLAULT392dff82008-11-06 22:26:49 +01002566static const char *
2567ath_rf_name(u16 rf_version)
2568{
2569 int i;
2570
2571 for (i=0; i<ARRAY_SIZE(ath_rf_names); i++) {
2572 if (ath_rf_names[i].version == rf_version) {
2573 return ath_rf_names[i].name;
2574 }
2575 }
2576
2577 return "????";
2578}
2579
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002580static int ath_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
2581{
2582 void __iomem *mem;
2583 struct ath_softc *sc;
2584 struct ieee80211_hw *hw;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002585 u8 csz;
2586 u32 val;
2587 int ret = 0;
Benoit PAPILLAULT392dff82008-11-06 22:26:49 +01002588 struct ath_hal *ah;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002589
2590 if (pci_enable_device(pdev))
2591 return -EIO;
2592
Luis R. Rodriguez97b777d2008-11-13 19:11:57 -08002593 ret = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
2594
2595 if (ret) {
Luis R. Rodriguez1d450cf2008-11-13 19:11:56 -08002596 printk(KERN_ERR "ath9k: 32-bit DMA not available\n");
Luis R. Rodriguez97b777d2008-11-13 19:11:57 -08002597 goto bad;
2598 }
2599
2600 ret = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
2601
2602 if (ret) {
2603 printk(KERN_ERR "ath9k: 32-bit DMA consistent "
Sujith04bd4632008-11-28 22:18:05 +05302604 "DMA enable failed\n");
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002605 goto bad;
2606 }
2607
2608 /*
2609 * Cache line size is used to size and align various
2610 * structures used to communicate with the hardware.
2611 */
2612 pci_read_config_byte(pdev, PCI_CACHE_LINE_SIZE, &csz);
2613 if (csz == 0) {
2614 /*
2615 * Linux 2.4.18 (at least) writes the cache line size
2616 * register as a 16-bit wide register which is wrong.
2617 * We must have this setup properly for rx buffer
2618 * DMA to work so force a reasonable value here if it
2619 * comes up zero.
2620 */
2621 csz = L1_CACHE_BYTES / sizeof(u32);
2622 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, csz);
2623 }
2624 /*
2625 * The default setting of latency timer yields poor results,
2626 * set it to the value used by other systems. It may be worth
2627 * tweaking this setting more.
2628 */
2629 pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0xa8);
2630
2631 pci_set_master(pdev);
2632
2633 /*
2634 * Disable the RETRY_TIMEOUT register (0x41) to keep
2635 * PCI Tx retries from interfering with C3 CPU state.
2636 */
2637 pci_read_config_dword(pdev, 0x40, &val);
2638 if ((val & 0x0000ff00) != 0)
2639 pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
2640
2641 ret = pci_request_region(pdev, 0, "ath9k");
2642 if (ret) {
2643 dev_err(&pdev->dev, "PCI memory region reserve error\n");
2644 ret = -ENODEV;
2645 goto bad;
2646 }
2647
2648 mem = pci_iomap(pdev, 0, 0);
2649 if (!mem) {
2650 printk(KERN_ERR "PCI memory map error\n") ;
2651 ret = -EIO;
2652 goto bad1;
2653 }
2654
2655 hw = ieee80211_alloc_hw(sizeof(struct ath_softc), &ath9k_ops);
2656 if (hw == NULL) {
2657 printk(KERN_ERR "ath_pci: no memory for ieee80211_hw\n");
2658 goto bad2;
2659 }
2660
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002661 SET_IEEE80211_DEV(hw, &pdev->dev);
2662 pci_set_drvdata(pdev, hw);
2663
2664 sc = hw->priv;
2665 sc->hw = hw;
2666 sc->pdev = pdev;
2667 sc->mem = mem;
2668
2669 if (ath_attach(id->device, sc) != 0) {
2670 ret = -ENODEV;
2671 goto bad3;
2672 }
2673
2674 /* setup interrupt service routine */
2675
2676 if (request_irq(pdev->irq, ath_isr, IRQF_SHARED, "ath", sc)) {
2677 printk(KERN_ERR "%s: request_irq failed\n",
2678 wiphy_name(hw->wiphy));
2679 ret = -EIO;
2680 goto bad4;
2681 }
2682
Benoit PAPILLAULT392dff82008-11-06 22:26:49 +01002683 ah = sc->sc_ah;
2684 printk(KERN_INFO
2685 "%s: Atheros AR%s MAC/BB Rev:%x "
2686 "AR%s RF Rev:%x: mem=0x%lx, irq=%d\n",
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002687 wiphy_name(hw->wiphy),
Benoit PAPILLAULT392dff82008-11-06 22:26:49 +01002688 ath_mac_bb_name(ah->ah_macVersion),
2689 ah->ah_macRev,
2690 ath_rf_name((ah->ah_analog5GhzRev & AR_RADIO_SREV_MAJOR)),
2691 ah->ah_phyRev,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002692 (unsigned long)mem, pdev->irq);
2693
2694 return 0;
2695bad4:
2696 ath_detach(sc);
2697bad3:
2698 ieee80211_free_hw(hw);
2699bad2:
2700 pci_iounmap(pdev, mem);
2701bad1:
2702 pci_release_region(pdev, 0);
2703bad:
2704 pci_disable_device(pdev);
2705 return ret;
2706}
2707
2708static void ath_pci_remove(struct pci_dev *pdev)
2709{
2710 struct ieee80211_hw *hw = pci_get_drvdata(pdev);
2711 struct ath_softc *sc = hw->priv;
2712
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002713 ath_detach(sc);
Sujith9c84b792008-10-29 10:17:13 +05302714 if (pdev->irq)
2715 free_irq(pdev->irq, sc);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002716 pci_iounmap(pdev, sc->mem);
2717 pci_release_region(pdev, 0);
2718 pci_disable_device(pdev);
2719 ieee80211_free_hw(hw);
2720}
2721
2722#ifdef CONFIG_PM
2723
2724static int ath_pci_suspend(struct pci_dev *pdev, pm_message_t state)
2725{
Vasanthakumar Thiagarajanc83be682008-08-25 20:47:29 +05302726 struct ieee80211_hw *hw = pci_get_drvdata(pdev);
2727 struct ath_softc *sc = hw->priv;
2728
2729 ath9k_hw_set_gpio(sc->sc_ah, ATH_LED_PIN, 1);
Vasanthakumar Thiagarajan500c0642008-09-10 18:50:17 +05302730
Senthil Balasubramaniane97275c2008-11-13 18:00:02 +05302731#if defined(CONFIG_RFKILL) || defined(CONFIG_RFKILL_MODULE)
Vasanthakumar Thiagarajan500c0642008-09-10 18:50:17 +05302732 if (sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
2733 cancel_delayed_work_sync(&sc->rf_kill.rfkill_poll);
2734#endif
2735
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002736 pci_save_state(pdev);
2737 pci_disable_device(pdev);
2738 pci_set_power_state(pdev, 3);
2739
2740 return 0;
2741}
2742
2743static int ath_pci_resume(struct pci_dev *pdev)
2744{
Vasanthakumar Thiagarajanc83be682008-08-25 20:47:29 +05302745 struct ieee80211_hw *hw = pci_get_drvdata(pdev);
2746 struct ath_softc *sc = hw->priv;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002747 u32 val;
2748 int err;
2749
2750 err = pci_enable_device(pdev);
2751 if (err)
2752 return err;
2753 pci_restore_state(pdev);
2754 /*
2755 * Suspend/Resume resets the PCI configuration space, so we have to
2756 * re-disable the RETRY_TIMEOUT register (0x41) to keep
2757 * PCI Tx retries from interfering with C3 CPU state
2758 */
2759 pci_read_config_dword(pdev, 0x40, &val);
2760 if ((val & 0x0000ff00) != 0)
2761 pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
2762
Vasanthakumar Thiagarajanc83be682008-08-25 20:47:29 +05302763 /* Enable LED */
2764 ath9k_hw_cfg_output(sc->sc_ah, ATH_LED_PIN,
2765 AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
2766 ath9k_hw_set_gpio(sc->sc_ah, ATH_LED_PIN, 1);
2767
Senthil Balasubramaniane97275c2008-11-13 18:00:02 +05302768#if defined(CONFIG_RFKILL) || defined(CONFIG_RFKILL_MODULE)
Vasanthakumar Thiagarajan500c0642008-09-10 18:50:17 +05302769 /*
2770 * check the h/w rfkill state on resume
2771 * and start the rfkill poll timer
2772 */
2773 if (sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
2774 queue_delayed_work(sc->hw->workqueue,
2775 &sc->rf_kill.rfkill_poll, 0);
2776#endif
2777
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002778 return 0;
2779}
2780
2781#endif /* CONFIG_PM */
2782
2783MODULE_DEVICE_TABLE(pci, ath_pci_id_table);
2784
2785static struct pci_driver ath_pci_driver = {
2786 .name = "ath9k",
2787 .id_table = ath_pci_id_table,
2788 .probe = ath_pci_probe,
2789 .remove = ath_pci_remove,
2790#ifdef CONFIG_PM
2791 .suspend = ath_pci_suspend,
2792 .resume = ath_pci_resume,
2793#endif /* CONFIG_PM */
2794};
2795
2796static int __init init_ath_pci(void)
2797{
2798 printk(KERN_INFO "%s: %s\n", dev_info, ATH_PCI_VERSION);
2799
2800 if (pci_register_driver(&ath_pci_driver) < 0) {
2801 printk(KERN_ERR
2802 "ath_pci: No devices found, driver not installed.\n");
2803 pci_unregister_driver(&ath_pci_driver);
2804 return -ENODEV;
2805 }
2806
2807 return 0;
2808}
2809module_init(init_ath_pci);
2810
2811static void __exit exit_ath_pci(void)
2812{
2813 pci_unregister_driver(&ath_pci_driver);
Sujith04bd4632008-11-28 22:18:05 +05302814 printk(KERN_INFO "%s: Driver unloaded\n", dev_info);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002815}
2816module_exit(exit_ath_pci);