blob: 9e7045dd73346b78fd4ce8699118e847be2826be [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
Sujithff37e332008-11-24 12:07:55 +053041/* return bus cachesize in 4B word units */
42
43static void bus_read_cachesize(struct ath_softc *sc, int *csz)
44{
45 u8 u8tmp;
46
47 pci_read_config_byte(sc->pdev, PCI_CACHE_LINE_SIZE, (u8 *)&u8tmp);
48 *csz = (int)u8tmp;
49
50 /*
51 * This check was put in to avoid "unplesant" consequences if
52 * the bootrom has not fully initialized all PCI devices.
53 * Sometimes the cache line size register is not set
54 */
55
56 if (*csz == 0)
57 *csz = DEFAULT_CACHELINE >> 2; /* Use the default size */
58}
59
60static void ath_setcurmode(struct ath_softc *sc, enum wireless_mode mode)
61{
62 sc->sc_curmode = mode;
63 /*
64 * All protection frames are transmited at 2Mb/s for
65 * 11g, otherwise at 1Mb/s.
66 * XXX select protection rate index from rate table.
67 */
68 sc->sc_protrix = (mode == ATH9K_MODE_11G ? 1 : 0);
69}
70
71static enum wireless_mode ath_chan2mode(struct ath9k_channel *chan)
72{
73 if (chan->chanmode == CHANNEL_A)
74 return ATH9K_MODE_11A;
75 else if (chan->chanmode == CHANNEL_G)
76 return ATH9K_MODE_11G;
77 else if (chan->chanmode == CHANNEL_B)
78 return ATH9K_MODE_11B;
79 else if (chan->chanmode == CHANNEL_A_HT20)
80 return ATH9K_MODE_11NA_HT20;
81 else if (chan->chanmode == CHANNEL_G_HT20)
82 return ATH9K_MODE_11NG_HT20;
83 else if (chan->chanmode == CHANNEL_A_HT40PLUS)
84 return ATH9K_MODE_11NA_HT40PLUS;
85 else if (chan->chanmode == CHANNEL_A_HT40MINUS)
86 return ATH9K_MODE_11NA_HT40MINUS;
87 else if (chan->chanmode == CHANNEL_G_HT40PLUS)
88 return ATH9K_MODE_11NG_HT40PLUS;
89 else if (chan->chanmode == CHANNEL_G_HT40MINUS)
90 return ATH9K_MODE_11NG_HT40MINUS;
91
92 WARN_ON(1); /* should not get here */
93
94 return ATH9K_MODE_11B;
95}
96
97static void ath_update_txpow(struct ath_softc *sc)
98{
99 struct ath_hal *ah = sc->sc_ah;
100 u32 txpow;
101
102 if (sc->sc_curtxpow != sc->sc_config.txpowlimit) {
103 ath9k_hw_set_txpowerlimit(ah, sc->sc_config.txpowlimit);
104 /* read back in case value is clamped */
105 ath9k_hw_getcapability(ah, ATH9K_CAP_TXPOW, 1, &txpow);
106 sc->sc_curtxpow = txpow;
107 }
108}
109
110static u8 parse_mpdudensity(u8 mpdudensity)
111{
112 /*
113 * 802.11n D2.0 defined values for "Minimum MPDU Start Spacing":
114 * 0 for no restriction
115 * 1 for 1/4 us
116 * 2 for 1/2 us
117 * 3 for 1 us
118 * 4 for 2 us
119 * 5 for 4 us
120 * 6 for 8 us
121 * 7 for 16 us
122 */
123 switch (mpdudensity) {
124 case 0:
125 return 0;
126 case 1:
127 case 2:
128 case 3:
129 /* Our lower layer calculations limit our precision to
130 1 microsecond */
131 return 1;
132 case 4:
133 return 2;
134 case 5:
135 return 4;
136 case 6:
137 return 8;
138 case 7:
139 return 16;
140 default:
141 return 0;
142 }
143}
144
145static void ath_setup_rates(struct ath_softc *sc, enum ieee80211_band band)
146{
147 struct ath_rate_table *rate_table = NULL;
148 struct ieee80211_supported_band *sband;
149 struct ieee80211_rate *rate;
150 int i, maxrates;
151
152 switch (band) {
153 case IEEE80211_BAND_2GHZ:
154 rate_table = sc->hw_rate_table[ATH9K_MODE_11G];
155 break;
156 case IEEE80211_BAND_5GHZ:
157 rate_table = sc->hw_rate_table[ATH9K_MODE_11A];
158 break;
159 default:
160 break;
161 }
162
163 if (rate_table == NULL)
164 return;
165
166 sband = &sc->sbands[band];
167 rate = sc->rates[band];
168
169 if (rate_table->rate_cnt > ATH_RATE_MAX)
170 maxrates = ATH_RATE_MAX;
171 else
172 maxrates = rate_table->rate_cnt;
173
174 for (i = 0; i < maxrates; i++) {
175 rate[i].bitrate = rate_table->info[i].ratekbps / 100;
176 rate[i].hw_value = rate_table->info[i].ratecode;
177 sband->n_bitrates++;
Sujith04bd4632008-11-28 22:18:05 +0530178 DPRINTF(sc, ATH_DBG_CONFIG, "Rate: %2dMbps, ratecode: %2d\n",
179 rate[i].bitrate / 10, rate[i].hw_value);
Sujithff37e332008-11-24 12:07:55 +0530180 }
181}
182
183static int ath_setup_channels(struct ath_softc *sc)
184{
185 struct ath_hal *ah = sc->sc_ah;
186 int nchan, i, a = 0, b = 0;
187 u8 regclassids[ATH_REGCLASSIDS_MAX];
188 u32 nregclass = 0;
189 struct ieee80211_supported_band *band_2ghz;
190 struct ieee80211_supported_band *band_5ghz;
191 struct ieee80211_channel *chan_2ghz;
192 struct ieee80211_channel *chan_5ghz;
193 struct ath9k_channel *c;
194
195 /* Fill in ah->ah_channels */
196 if (!ath9k_regd_init_channels(ah, ATH_CHAN_MAX, (u32 *)&nchan,
197 regclassids, ATH_REGCLASSIDS_MAX,
198 &nregclass, CTRY_DEFAULT, false, 1)) {
199 u32 rd = ah->ah_currentRD;
200 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd4632008-11-28 22:18:05 +0530201 "Unable to collect channel list; "
Sujithff37e332008-11-24 12:07:55 +0530202 "regdomain likely %u country code %u\n",
Sujith04bd4632008-11-28 22:18:05 +0530203 rd, CTRY_DEFAULT);
Sujithff37e332008-11-24 12:07:55 +0530204 return -EINVAL;
205 }
206
207 band_2ghz = &sc->sbands[IEEE80211_BAND_2GHZ];
208 band_5ghz = &sc->sbands[IEEE80211_BAND_5GHZ];
209 chan_2ghz = sc->channels[IEEE80211_BAND_2GHZ];
210 chan_5ghz = sc->channels[IEEE80211_BAND_5GHZ];
211
212 for (i = 0; i < nchan; i++) {
213 c = &ah->ah_channels[i];
214 if (IS_CHAN_2GHZ(c)) {
215 chan_2ghz[a].band = IEEE80211_BAND_2GHZ;
216 chan_2ghz[a].center_freq = c->channel;
217 chan_2ghz[a].max_power = c->maxTxPower;
218
219 if (c->privFlags & CHANNEL_DISALLOW_ADHOC)
220 chan_2ghz[a].flags |= IEEE80211_CHAN_NO_IBSS;
221 if (c->channelFlags & CHANNEL_PASSIVE)
222 chan_2ghz[a].flags |= IEEE80211_CHAN_PASSIVE_SCAN;
223
224 band_2ghz->n_channels = ++a;
225
Sujith04bd4632008-11-28 22:18:05 +0530226 DPRINTF(sc, ATH_DBG_CONFIG, "2MHz channel: %d, "
Sujithff37e332008-11-24 12:07:55 +0530227 "channelFlags: 0x%x\n",
Sujith04bd4632008-11-28 22:18:05 +0530228 c->channel, c->channelFlags);
Sujithff37e332008-11-24 12:07:55 +0530229 } else if (IS_CHAN_5GHZ(c)) {
230 chan_5ghz[b].band = IEEE80211_BAND_5GHZ;
231 chan_5ghz[b].center_freq = c->channel;
232 chan_5ghz[b].max_power = c->maxTxPower;
233
234 if (c->privFlags & CHANNEL_DISALLOW_ADHOC)
235 chan_5ghz[b].flags |= IEEE80211_CHAN_NO_IBSS;
236 if (c->channelFlags & CHANNEL_PASSIVE)
237 chan_5ghz[b].flags |= IEEE80211_CHAN_PASSIVE_SCAN;
238
239 band_5ghz->n_channels = ++b;
240
Sujith04bd4632008-11-28 22:18:05 +0530241 DPRINTF(sc, ATH_DBG_CONFIG, "5MHz 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 }
245 }
246
247 return 0;
248}
249
250/*
251 * Set/change channels. If the channel is really being changed, it's done
252 * by reseting the chip. To accomplish this we must first cleanup any pending
253 * DMA, then restart stuff.
254*/
255static int ath_set_channel(struct ath_softc *sc, struct ath9k_channel *hchan)
256{
257 struct ath_hal *ah = sc->sc_ah;
258 bool fastcc = true, stopped;
259
260 if (sc->sc_flags & SC_OP_INVALID)
261 return -EIO;
262
Sujithff37e332008-11-24 12:07:55 +0530263 if (hchan->channel != sc->sc_ah->ah_curchan->channel ||
264 hchan->channelFlags != sc->sc_ah->ah_curchan->channelFlags ||
265 (sc->sc_flags & SC_OP_CHAINMASK_UPDATE) ||
266 (sc->sc_flags & SC_OP_FULL_RESET)) {
267 int status;
268 /*
269 * This is only performed if the channel settings have
270 * actually changed.
271 *
272 * To switch channels clear any pending DMA operations;
273 * wait long enough for the RX fifo to drain, reset the
274 * hardware at the new frequency, and then re-enable
275 * the relevant bits of the h/w.
276 */
Sujith04bd4632008-11-28 22:18:05 +0530277 ath9k_hw_set_interrupts(ah, 0);
278 ath_draintxq(sc, false);
279 stopped = ath_stoprecv(sc);
Sujithff37e332008-11-24 12:07:55 +0530280
281 /* XXX: do not flush receive queue here. We don't want
282 * to flush data frames already in queue because of
283 * changing channel. */
284
285 if (!stopped || (sc->sc_flags & SC_OP_FULL_RESET))
286 fastcc = false;
287
Sujith99405f92008-11-24 12:08:35 +0530288 DPRINTF(sc, ATH_DBG_CONFIG,
Sujith04bd4632008-11-28 22:18:05 +0530289 "(%u MHz) -> (%u MHz), cflags:%x, chanwidth: %d\n",
Sujith99405f92008-11-24 12:08:35 +0530290 sc->sc_ah->ah_curchan->channel,
291 hchan->channel, hchan->channelFlags, sc->tx_chan_width);
292
Sujithff37e332008-11-24 12:07:55 +0530293 spin_lock_bh(&sc->sc_resetlock);
Sujith99405f92008-11-24 12:08:35 +0530294 if (!ath9k_hw_reset(ah, hchan, sc->tx_chan_width,
Sujithff37e332008-11-24 12:07:55 +0530295 sc->sc_tx_chainmask, sc->sc_rx_chainmask,
296 sc->sc_ht_extprotspacing, fastcc, &status)) {
297 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd4632008-11-28 22:18:05 +0530298 "Unable to reset channel %u (%uMhz) "
299 "flags 0x%x hal status %u\n",
Sujithff37e332008-11-24 12:07:55 +0530300 ath9k_hw_mhz2ieee(ah, hchan->channel,
301 hchan->channelFlags),
302 hchan->channel, hchan->channelFlags, status);
303 spin_unlock_bh(&sc->sc_resetlock);
304 return -EIO;
305 }
306 spin_unlock_bh(&sc->sc_resetlock);
307
308 sc->sc_flags &= ~SC_OP_CHAINMASK_UPDATE;
309 sc->sc_flags &= ~SC_OP_FULL_RESET;
310
311 if (ath_startrecv(sc) != 0) {
312 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd4632008-11-28 22:18:05 +0530313 "Unable to restart recv logic\n");
Sujithff37e332008-11-24 12:07:55 +0530314 return -EIO;
315 }
316
317 ath_setcurmode(sc, ath_chan2mode(hchan));
318 ath_update_txpow(sc);
319 ath9k_hw_set_interrupts(ah, sc->sc_imask);
320 }
321 return 0;
322}
323
324/*
325 * This routine performs the periodic noise floor calibration function
326 * that is used to adjust and optimize the chip performance. This
327 * takes environmental changes (location, temperature) into account.
328 * When the task is complete, it reschedules itself depending on the
329 * appropriate interval that was calculated.
330 */
331static void ath_ani_calibrate(unsigned long data)
332{
333 struct ath_softc *sc;
334 struct ath_hal *ah;
335 bool longcal = false;
336 bool shortcal = false;
337 bool aniflag = false;
338 unsigned int timestamp = jiffies_to_msecs(jiffies);
339 u32 cal_interval;
340
341 sc = (struct ath_softc *)data;
342 ah = sc->sc_ah;
343
344 /*
345 * don't calibrate when we're scanning.
346 * we are most likely not on our home channel.
347 */
348 if (sc->rx_filter & FIF_BCN_PRBRESP_PROMISC)
349 return;
350
351 /* Long calibration runs independently of short calibration. */
352 if ((timestamp - sc->sc_ani.sc_longcal_timer) >= ATH_LONG_CALINTERVAL) {
353 longcal = true;
Sujith04bd4632008-11-28 22:18:05 +0530354 DPRINTF(sc, ATH_DBG_ANI, "longcal @%lu\n", jiffies);
Sujithff37e332008-11-24 12:07:55 +0530355 sc->sc_ani.sc_longcal_timer = timestamp;
356 }
357
358 /* Short calibration applies only while sc_caldone is false */
359 if (!sc->sc_ani.sc_caldone) {
360 if ((timestamp - sc->sc_ani.sc_shortcal_timer) >=
361 ATH_SHORT_CALINTERVAL) {
362 shortcal = true;
Sujith04bd4632008-11-28 22:18:05 +0530363 DPRINTF(sc, ATH_DBG_ANI, "shortcal @%lu\n", jiffies);
Sujithff37e332008-11-24 12:07:55 +0530364 sc->sc_ani.sc_shortcal_timer = timestamp;
365 sc->sc_ani.sc_resetcal_timer = timestamp;
366 }
367 } else {
368 if ((timestamp - sc->sc_ani.sc_resetcal_timer) >=
369 ATH_RESTART_CALINTERVAL) {
370 ath9k_hw_reset_calvalid(ah, ah->ah_curchan,
371 &sc->sc_ani.sc_caldone);
372 if (sc->sc_ani.sc_caldone)
373 sc->sc_ani.sc_resetcal_timer = timestamp;
374 }
375 }
376
377 /* Verify whether we must check ANI */
378 if ((timestamp - sc->sc_ani.sc_checkani_timer) >=
379 ATH_ANI_POLLINTERVAL) {
380 aniflag = true;
381 sc->sc_ani.sc_checkani_timer = timestamp;
382 }
383
384 /* Skip all processing if there's nothing to do. */
385 if (longcal || shortcal || aniflag) {
386 /* Call ANI routine if necessary */
387 if (aniflag)
388 ath9k_hw_ani_monitor(ah, &sc->sc_halstats,
389 ah->ah_curchan);
390
391 /* Perform calibration if necessary */
392 if (longcal || shortcal) {
393 bool iscaldone = false;
394
395 if (ath9k_hw_calibrate(ah, ah->ah_curchan,
396 sc->sc_rx_chainmask, longcal,
397 &iscaldone)) {
398 if (longcal)
399 sc->sc_ani.sc_noise_floor =
400 ath9k_hw_getchan_noise(ah,
401 ah->ah_curchan);
402
403 DPRINTF(sc, ATH_DBG_ANI,
Sujith04bd4632008-11-28 22:18:05 +0530404 "calibrate chan %u/%x nf: %d\n",
Sujithff37e332008-11-24 12:07:55 +0530405 ah->ah_curchan->channel,
406 ah->ah_curchan->channelFlags,
407 sc->sc_ani.sc_noise_floor);
408 } else {
409 DPRINTF(sc, ATH_DBG_ANY,
Sujith04bd4632008-11-28 22:18:05 +0530410 "calibrate chan %u/%x failed\n",
Sujithff37e332008-11-24 12:07:55 +0530411 ah->ah_curchan->channel,
412 ah->ah_curchan->channelFlags);
413 }
414 sc->sc_ani.sc_caldone = iscaldone;
415 }
416 }
417
418 /*
419 * Set timer interval based on previous results.
420 * The interval must be the shortest necessary to satisfy ANI,
421 * short calibration and long calibration.
422 */
423
424 cal_interval = ATH_ANI_POLLINTERVAL;
425 if (!sc->sc_ani.sc_caldone)
426 cal_interval = min(cal_interval, (u32)ATH_SHORT_CALINTERVAL);
427
428 mod_timer(&sc->sc_ani.timer, jiffies + msecs_to_jiffies(cal_interval));
429}
430
431/*
432 * Update tx/rx chainmask. For legacy association,
433 * hard code chainmask to 1x1, for 11n association, use
434 * the chainmask configuration.
435 */
436static void ath_update_chainmask(struct ath_softc *sc, int is_ht)
437{
438 sc->sc_flags |= SC_OP_CHAINMASK_UPDATE;
439 if (is_ht) {
440 sc->sc_tx_chainmask = sc->sc_ah->ah_caps.tx_chainmask;
441 sc->sc_rx_chainmask = sc->sc_ah->ah_caps.rx_chainmask;
442 } else {
443 sc->sc_tx_chainmask = 1;
444 sc->sc_rx_chainmask = 1;
445 }
446
Sujith04bd4632008-11-28 22:18:05 +0530447 DPRINTF(sc, ATH_DBG_CONFIG, "tx chmask: %d, rx chmask: %d\n",
448 sc->sc_tx_chainmask, sc->sc_rx_chainmask);
Sujithff37e332008-11-24 12:07:55 +0530449}
450
451static void ath_node_attach(struct ath_softc *sc, struct ieee80211_sta *sta)
452{
453 struct ath_node *an;
454
455 an = (struct ath_node *)sta->drv_priv;
456
457 if (sc->sc_flags & SC_OP_TXAGGR)
458 ath_tx_node_init(sc, an);
459
460 an->maxampdu = 1 << (IEEE80211_HTCAP_MAXRXAMPDU_FACTOR +
461 sta->ht_cap.ampdu_factor);
462 an->mpdudensity = parse_mpdudensity(sta->ht_cap.ampdu_density);
463}
464
465static void ath_node_detach(struct ath_softc *sc, struct ieee80211_sta *sta)
466{
467 struct ath_node *an = (struct ath_node *)sta->drv_priv;
468
469 if (sc->sc_flags & SC_OP_TXAGGR)
470 ath_tx_node_cleanup(sc, an);
471}
472
473static void ath9k_tasklet(unsigned long data)
474{
475 struct ath_softc *sc = (struct ath_softc *)data;
476 u32 status = sc->sc_intrstatus;
477
478 if (status & ATH9K_INT_FATAL) {
479 /* need a chip reset */
480 ath_reset(sc, false);
481 return;
482 } else {
483
484 if (status &
485 (ATH9K_INT_RX | ATH9K_INT_RXEOL | ATH9K_INT_RXORN)) {
486 spin_lock_bh(&sc->sc_rxflushlock);
487 ath_rx_tasklet(sc, 0);
488 spin_unlock_bh(&sc->sc_rxflushlock);
489 }
490 /* XXX: optimize this */
491 if (status & ATH9K_INT_TX)
492 ath_tx_tasklet(sc);
493 }
494
495 /* re-enable hardware interrupt */
496 ath9k_hw_set_interrupts(sc->sc_ah, sc->sc_imask);
497}
498
499static irqreturn_t ath_isr(int irq, void *dev)
500{
501 struct ath_softc *sc = dev;
502 struct ath_hal *ah = sc->sc_ah;
503 enum ath9k_int status;
504 bool sched = false;
505
506 do {
507 if (sc->sc_flags & SC_OP_INVALID) {
508 /*
509 * The hardware is not ready/present, don't
510 * touch anything. Note this can happen early
511 * on if the IRQ is shared.
512 */
513 return IRQ_NONE;
514 }
515 if (!ath9k_hw_intrpend(ah)) { /* shared irq, not for us */
516 return IRQ_NONE;
517 }
518
519 /*
520 * Figure out the reason(s) for the interrupt. Note
521 * that the hal returns a pseudo-ISR that may include
522 * bits we haven't explicitly enabled so we mask the
523 * value to insure we only process bits we requested.
524 */
525 ath9k_hw_getisr(ah, &status); /* NB: clears ISR too */
526
527 status &= sc->sc_imask; /* discard unasked-for bits */
528
529 /*
530 * If there are no status bits set, then this interrupt was not
531 * for me (should have been caught above).
532 */
533 if (!status)
534 return IRQ_NONE;
535
536 sc->sc_intrstatus = status;
537
538 if (status & ATH9K_INT_FATAL) {
539 /* need a chip reset */
540 sched = true;
541 } else if (status & ATH9K_INT_RXORN) {
542 /* need a chip reset */
543 sched = true;
544 } else {
545 if (status & ATH9K_INT_SWBA) {
546 /* schedule a tasklet for beacon handling */
547 tasklet_schedule(&sc->bcon_tasklet);
548 }
549 if (status & ATH9K_INT_RXEOL) {
550 /*
551 * NB: the hardware should re-read the link when
552 * RXE bit is written, but it doesn't work
553 * at least on older hardware revs.
554 */
555 sched = true;
556 }
557
558 if (status & ATH9K_INT_TXURN)
559 /* bump tx trigger level */
560 ath9k_hw_updatetxtriglevel(ah, true);
561 /* XXX: optimize this */
562 if (status & ATH9K_INT_RX)
563 sched = true;
564 if (status & ATH9K_INT_TX)
565 sched = true;
566 if (status & ATH9K_INT_BMISS)
567 sched = true;
568 /* carrier sense timeout */
569 if (status & ATH9K_INT_CST)
570 sched = true;
571 if (status & ATH9K_INT_MIB) {
572 /*
573 * Disable interrupts until we service the MIB
574 * interrupt; otherwise it will continue to
575 * fire.
576 */
577 ath9k_hw_set_interrupts(ah, 0);
578 /*
579 * Let the hal handle the event. We assume
580 * it will clear whatever condition caused
581 * the interrupt.
582 */
583 ath9k_hw_procmibevent(ah, &sc->sc_halstats);
584 ath9k_hw_set_interrupts(ah, sc->sc_imask);
585 }
586 if (status & ATH9K_INT_TIM_TIMER) {
587 if (!(ah->ah_caps.hw_caps &
588 ATH9K_HW_CAP_AUTOSLEEP)) {
589 /* Clear RxAbort bit so that we can
590 * receive frames */
591 ath9k_hw_setrxabort(ah, 0);
592 sched = true;
593 }
594 }
595 }
596 } while (0);
597
598 if (sched) {
599 /* turn off every interrupt except SWBA */
600 ath9k_hw_set_interrupts(ah, (sc->sc_imask & ATH9K_INT_SWBA));
601 tasklet_schedule(&sc->intr_tq);
602 }
603
604 return IRQ_HANDLED;
605}
606
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700607static int ath_get_channel(struct ath_softc *sc,
608 struct ieee80211_channel *chan)
609{
610 int i;
611
612 for (i = 0; i < sc->sc_ah->ah_nchan; i++) {
613 if (sc->sc_ah->ah_channels[i].channel == chan->center_freq)
614 return i;
615 }
616
617 return -1;
618}
619
Sujithe11602b2008-11-27 09:46:27 +0530620/* ext_chan_offset: (-1, 0, 1) (below, none, above) */
621
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700622static u32 ath_get_extchanmode(struct ath_softc *sc,
Sujith99405f92008-11-24 12:08:35 +0530623 struct ieee80211_channel *chan,
Sujithe11602b2008-11-27 09:46:27 +0530624 int ext_chan_offset,
625 enum ath9k_ht_macmode tx_chan_width)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700626{
627 u32 chanmode = 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700628
629 switch (chan->band) {
630 case IEEE80211_BAND_2GHZ:
Sujithe11602b2008-11-27 09:46:27 +0530631 if ((ext_chan_offset == 0) &&
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700632 (tx_chan_width == ATH9K_HT_MACMODE_20))
633 chanmode = CHANNEL_G_HT20;
Sujithe11602b2008-11-27 09:46:27 +0530634 if ((ext_chan_offset == 1) &&
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700635 (tx_chan_width == ATH9K_HT_MACMODE_2040))
636 chanmode = CHANNEL_G_HT40PLUS;
Sujithe11602b2008-11-27 09:46:27 +0530637 if ((ext_chan_offset == -1) &&
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700638 (tx_chan_width == ATH9K_HT_MACMODE_2040))
639 chanmode = CHANNEL_G_HT40MINUS;
640 break;
641 case IEEE80211_BAND_5GHZ:
Sujithe11602b2008-11-27 09:46:27 +0530642 if ((ext_chan_offset == 0) &&
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700643 (tx_chan_width == ATH9K_HT_MACMODE_20))
644 chanmode = CHANNEL_A_HT20;
Sujithe11602b2008-11-27 09:46:27 +0530645 if ((ext_chan_offset == 1) &&
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700646 (tx_chan_width == ATH9K_HT_MACMODE_2040))
647 chanmode = CHANNEL_A_HT40PLUS;
Sujithe11602b2008-11-27 09:46:27 +0530648 if ((ext_chan_offset == -1) &&
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700649 (tx_chan_width == ATH9K_HT_MACMODE_2040))
650 chanmode = CHANNEL_A_HT40MINUS;
651 break;
652 default:
653 break;
654 }
655
656 return chanmode;
657}
658
Sujithff37e332008-11-24 12:07:55 +0530659static void ath_key_reset(struct ath_softc *sc, u16 keyix, int freeslot)
660{
661 ath9k_hw_keyreset(sc->sc_ah, keyix);
662 if (freeslot)
663 clear_bit(keyix, sc->sc_keymap);
664}
665
666static int ath_keyset(struct ath_softc *sc, u16 keyix,
667 struct ath9k_keyval *hk, const u8 mac[ETH_ALEN])
668{
669 bool status;
670
671 status = ath9k_hw_set_keycache_entry(sc->sc_ah,
672 keyix, hk, mac, false);
673
674 return status != false;
675}
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700676
677static int ath_setkey_tkip(struct ath_softc *sc,
678 struct ieee80211_key_conf *key,
679 struct ath9k_keyval *hk,
680 const u8 *addr)
681{
682 u8 *key_rxmic = NULL;
683 u8 *key_txmic = NULL;
684
685 key_txmic = key->key + NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY;
686 key_rxmic = key->key + NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY;
687
688 if (addr == NULL) {
689 /* Group key installation */
690 memcpy(hk->kv_mic, key_rxmic, sizeof(hk->kv_mic));
691 return ath_keyset(sc, key->keyidx, hk, addr);
692 }
693 if (!sc->sc_splitmic) {
694 /*
695 * data key goes at first index,
696 * the hal handles the MIC keys at index+64.
697 */
698 memcpy(hk->kv_mic, key_rxmic, sizeof(hk->kv_mic));
699 memcpy(hk->kv_txmic, key_txmic, sizeof(hk->kv_txmic));
700 return ath_keyset(sc, key->keyidx, hk, addr);
701 }
702 /*
703 * TX key goes at first index, RX key at +32.
704 * The hal handles the MIC keys at index+64.
705 */
706 memcpy(hk->kv_mic, key_txmic, sizeof(hk->kv_mic));
707 if (!ath_keyset(sc, key->keyidx, hk, NULL)) {
708 /* Txmic entry failed. No need to proceed further */
709 DPRINTF(sc, ATH_DBG_KEYCACHE,
Sujith04bd4632008-11-28 22:18:05 +0530710 "Setting TX MIC Key Failed\n");
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700711 return 0;
712 }
713
714 memcpy(hk->kv_mic, key_rxmic, sizeof(hk->kv_mic));
715 /* XXX delete tx key on failure? */
716 return ath_keyset(sc, key->keyidx+32, hk, addr);
717}
718
719static int ath_key_config(struct ath_softc *sc,
720 const u8 *addr,
721 struct ieee80211_key_conf *key)
722{
723 struct ieee80211_vif *vif;
724 struct ath9k_keyval hk;
725 const u8 *mac = NULL;
726 int ret = 0;
Johannes Berg05c914f2008-09-11 00:01:58 +0200727 enum nl80211_iftype opmode;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700728
729 memset(&hk, 0, sizeof(hk));
730
731 switch (key->alg) {
732 case ALG_WEP:
733 hk.kv_type = ATH9K_CIPHER_WEP;
734 break;
735 case ALG_TKIP:
736 hk.kv_type = ATH9K_CIPHER_TKIP;
737 break;
738 case ALG_CCMP:
739 hk.kv_type = ATH9K_CIPHER_AES_CCM;
740 break;
741 default:
742 return -EINVAL;
743 }
744
745 hk.kv_len = key->keylen;
746 memcpy(hk.kv_val, key->key, key->keylen);
747
748 if (!sc->sc_vaps[0])
749 return -EIO;
750
Sujith5640b082008-10-29 10:16:06 +0530751 vif = sc->sc_vaps[0];
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700752 opmode = vif->type;
753
754 /*
755 * Strategy:
756 * For _M_STA mc tx, we will not setup a key at all since we never
757 * tx mc.
758 * _M_STA mc rx, we will use the keyID.
759 * for _M_IBSS mc tx, we will use the keyID, and no macaddr.
760 * for _M_IBSS mc rx, we will alloc a slot and plumb the mac of the
761 * peer node. BUT we will plumb a cleartext key so that we can do
762 * perSta default key table lookup in software.
763 */
764 if (is_broadcast_ether_addr(addr)) {
765 switch (opmode) {
Johannes Berg05c914f2008-09-11 00:01:58 +0200766 case NL80211_IFTYPE_STATION:
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700767 /* default key: could be group WPA key
768 * or could be static WEP key */
769 mac = NULL;
770 break;
Johannes Berg05c914f2008-09-11 00:01:58 +0200771 case NL80211_IFTYPE_ADHOC:
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700772 break;
Johannes Berg05c914f2008-09-11 00:01:58 +0200773 case NL80211_IFTYPE_AP:
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700774 break;
775 default:
776 ASSERT(0);
777 break;
778 }
779 } else {
780 mac = addr;
781 }
782
783 if (key->alg == ALG_TKIP)
784 ret = ath_setkey_tkip(sc, key, &hk, mac);
785 else
786 ret = ath_keyset(sc, key->keyidx, &hk, mac);
787
788 if (!ret)
789 return -EIO;
790
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700791 return 0;
792}
793
794static void ath_key_delete(struct ath_softc *sc, struct ieee80211_key_conf *key)
795{
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700796 int freeslot;
797
Sujithff9b6622008-08-14 13:27:16 +0530798 freeslot = (key->keyidx >= 4) ? 1 : 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700799 ath_key_reset(sc, key->keyidx, freeslot);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700800}
801
Johannes Bergd9fe60d2008-10-09 12:13:49 +0200802static void setup_ht_cap(struct ieee80211_sta_ht_cap *ht_info)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700803{
Sujith60653672008-08-14 13:28:02 +0530804#define ATH9K_HT_CAP_MAXRXAMPDU_65536 0x3 /* 2 ^ 16 */
805#define ATH9K_HT_CAP_MPDUDENSITY_8 0x6 /* 8 usec */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700806
Johannes Bergd9fe60d2008-10-09 12:13:49 +0200807 ht_info->ht_supported = true;
808 ht_info->cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
809 IEEE80211_HT_CAP_SM_PS |
810 IEEE80211_HT_CAP_SGI_40 |
811 IEEE80211_HT_CAP_DSSSCCK40;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700812
Sujith60653672008-08-14 13:28:02 +0530813 ht_info->ampdu_factor = ATH9K_HT_CAP_MAXRXAMPDU_65536;
814 ht_info->ampdu_density = ATH9K_HT_CAP_MPDUDENSITY_8;
Johannes Bergd9fe60d2008-10-09 12:13:49 +0200815 /* set up supported mcs set */
816 memset(&ht_info->mcs, 0, sizeof(ht_info->mcs));
817 ht_info->mcs.rx_mask[0] = 0xff;
818 ht_info->mcs.rx_mask[1] = 0xff;
819 ht_info->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -0700820}
821
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530822static void ath9k_ht_conf(struct ath_softc *sc,
823 struct ieee80211_bss_conf *bss_conf)
824{
Johannes Bergae5eb022008-10-14 16:58:37 +0200825 if (sc->hw->conf.ht.enabled) {
Johannes Bergae5eb022008-10-14 16:58:37 +0200826 if (bss_conf->ht.width_40_ok)
Sujith99405f92008-11-24 12:08:35 +0530827 sc->tx_chan_width = ATH9K_HT_MACMODE_2040;
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530828 else
Sujith99405f92008-11-24 12:08:35 +0530829 sc->tx_chan_width = ATH9K_HT_MACMODE_20;
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530830
Sujith99405f92008-11-24 12:08:35 +0530831 ath9k_hw_set11nmac2040(sc->sc_ah, sc->tx_chan_width);
832
833 DPRINTF(sc, ATH_DBG_CONFIG,
Sujith04bd4632008-11-28 22:18:05 +0530834 "BSS Changed HT, chanwidth: %d\n", sc->tx_chan_width);
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530835 }
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530836}
837
Sujithe11602b2008-11-27 09:46:27 +0530838static inline int ath_sec_offset(u8 ext_offset)
839{
840 if (ext_offset == IEEE80211_HT_PARAM_CHA_SEC_NONE)
841 return 0;
842 else if (ext_offset == IEEE80211_HT_PARAM_CHA_SEC_ABOVE)
843 return 1;
844 else if (ext_offset == IEEE80211_HT_PARAM_CHA_SEC_BELOW)
845 return -1;
846
847 return 0;
848}
849
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530850static void ath9k_bss_assoc_info(struct ath_softc *sc,
Sujith5640b082008-10-29 10:16:06 +0530851 struct ieee80211_vif *vif,
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530852 struct ieee80211_bss_conf *bss_conf)
853{
854 struct ieee80211_hw *hw = sc->hw;
855 struct ieee80211_channel *curchan = hw->conf.channel;
Sujith5640b082008-10-29 10:16:06 +0530856 struct ath_vap *avp = (void *)vif->drv_priv;
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530857 int pos;
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530858
859 if (bss_conf->assoc) {
Sujith04bd4632008-11-28 22:18:05 +0530860 DPRINTF(sc, ATH_DBG_CONFIG, "Bss Info ASSOC %d\n", bss_conf->aid);
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530861
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530862 /* New association, store aid */
863 if (avp->av_opmode == ATH9K_M_STA) {
864 sc->sc_curaid = bss_conf->aid;
865 ath9k_hw_write_associd(sc->sc_ah, sc->sc_curbssid,
866 sc->sc_curaid);
867 }
868
869 /* Configure the beacon */
870 ath_beacon_config(sc, 0);
871 sc->sc_flags |= SC_OP_BEACONS;
872
873 /* Reset rssi stats */
874 sc->sc_halstats.ns_avgbrssi = ATH_RSSI_DUMMY_MARKER;
875 sc->sc_halstats.ns_avgrssi = ATH_RSSI_DUMMY_MARKER;
876 sc->sc_halstats.ns_avgtxrssi = ATH_RSSI_DUMMY_MARKER;
877 sc->sc_halstats.ns_avgtxrate = ATH_RATE_DUMMY_MARKER;
878
879 /* Update chainmask */
Johannes Bergae5eb022008-10-14 16:58:37 +0200880 ath_update_chainmask(sc, hw->conf.ht.enabled);
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530881
882 DPRINTF(sc, ATH_DBG_CONFIG,
Sujith04bd4632008-11-28 22:18:05 +0530883 "bssid %pM aid 0x%x\n",
Johannes Berge1749612008-10-27 15:59:26 -0700884 sc->sc_curbssid, sc->sc_curaid);
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530885
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530886 pos = ath_get_channel(sc, curchan);
887 if (pos == -1) {
888 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd4632008-11-28 22:18:05 +0530889 "Invalid channel: %d\n", curchan->center_freq);
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530890 return;
891 }
892
Sujith99405f92008-11-24 12:08:35 +0530893 if (hw->conf.ht.enabled) {
Sujithe11602b2008-11-27 09:46:27 +0530894 int offset =
895 ath_sec_offset(bss_conf->ht.secondary_channel_offset);
896 sc->tx_chan_width = (bss_conf->ht.width_40_ok) ?
897 ATH9K_HT_MACMODE_2040 : ATH9K_HT_MACMODE_20;
Sujith99405f92008-11-24 12:08:35 +0530898
Sujithe11602b2008-11-27 09:46:27 +0530899 sc->sc_ah->ah_channels[pos].chanmode =
900 ath_get_extchanmode(sc, curchan,
901 offset, sc->tx_chan_width);
Sujith99405f92008-11-24 12:08:35 +0530902 } else {
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530903 sc->sc_ah->ah_channels[pos].chanmode =
904 (curchan->band == IEEE80211_BAND_2GHZ) ?
905 CHANNEL_G : CHANNEL_A;
Sujith99405f92008-11-24 12:08:35 +0530906 }
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530907
908 /* set h/w channel */
909 if (ath_set_channel(sc, &sc->sc_ah->ah_channels[pos]) < 0)
Sujith04bd4632008-11-28 22:18:05 +0530910 DPRINTF(sc, ATH_DBG_FATAL, "Unable to set channel: %d\n",
911 curchan->center_freq);
912
Luis R. Rodriguez6f255422008-10-03 15:45:27 -0700913 /* Start ANI */
914 mod_timer(&sc->sc_ani.timer,
915 jiffies + msecs_to_jiffies(ATH_ANI_POLLINTERVAL));
916
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530917 } else {
Sujith04bd4632008-11-28 22:18:05 +0530918 DPRINTF(sc, ATH_DBG_CONFIG, "Bss Info DISSOC\n");
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530919 sc->sc_curaid = 0;
920 }
921}
922
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +0530923/********************************/
924/* LED functions */
925/********************************/
926
927static void ath_led_brightness(struct led_classdev *led_cdev,
928 enum led_brightness brightness)
929{
930 struct ath_led *led = container_of(led_cdev, struct ath_led, led_cdev);
931 struct ath_softc *sc = led->sc;
932
933 switch (brightness) {
934 case LED_OFF:
935 if (led->led_type == ATH_LED_ASSOC ||
936 led->led_type == ATH_LED_RADIO)
937 sc->sc_flags &= ~SC_OP_LED_ASSOCIATED;
938 ath9k_hw_set_gpio(sc->sc_ah, ATH_LED_PIN,
939 (led->led_type == ATH_LED_RADIO) ? 1 :
940 !!(sc->sc_flags & SC_OP_LED_ASSOCIATED));
941 break;
942 case LED_FULL:
943 if (led->led_type == ATH_LED_ASSOC)
944 sc->sc_flags |= SC_OP_LED_ASSOCIATED;
945 ath9k_hw_set_gpio(sc->sc_ah, ATH_LED_PIN, 0);
946 break;
947 default:
948 break;
949 }
950}
951
952static int ath_register_led(struct ath_softc *sc, struct ath_led *led,
953 char *trigger)
954{
955 int ret;
956
957 led->sc = sc;
958 led->led_cdev.name = led->name;
959 led->led_cdev.default_trigger = trigger;
960 led->led_cdev.brightness_set = ath_led_brightness;
961
962 ret = led_classdev_register(wiphy_dev(sc->hw->wiphy), &led->led_cdev);
963 if (ret)
964 DPRINTF(sc, ATH_DBG_FATAL,
965 "Failed to register led:%s", led->name);
966 else
967 led->registered = 1;
968 return ret;
969}
970
971static void ath_unregister_led(struct ath_led *led)
972{
973 if (led->registered) {
974 led_classdev_unregister(&led->led_cdev);
975 led->registered = 0;
976 }
977}
978
979static void ath_deinit_leds(struct ath_softc *sc)
980{
981 ath_unregister_led(&sc->assoc_led);
982 sc->sc_flags &= ~SC_OP_LED_ASSOCIATED;
983 ath_unregister_led(&sc->tx_led);
984 ath_unregister_led(&sc->rx_led);
985 ath_unregister_led(&sc->radio_led);
986 ath9k_hw_set_gpio(sc->sc_ah, ATH_LED_PIN, 1);
987}
988
989static void ath_init_leds(struct ath_softc *sc)
990{
991 char *trigger;
992 int ret;
993
994 /* Configure gpio 1 for output */
995 ath9k_hw_cfg_output(sc->sc_ah, ATH_LED_PIN,
996 AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
997 /* LED off, active low */
998 ath9k_hw_set_gpio(sc->sc_ah, ATH_LED_PIN, 1);
999
1000 trigger = ieee80211_get_radio_led_name(sc->hw);
1001 snprintf(sc->radio_led.name, sizeof(sc->radio_led.name),
1002 "ath9k-%s:radio", wiphy_name(sc->hw->wiphy));
1003 ret = ath_register_led(sc, &sc->radio_led, trigger);
1004 sc->radio_led.led_type = ATH_LED_RADIO;
1005 if (ret)
1006 goto fail;
1007
1008 trigger = ieee80211_get_assoc_led_name(sc->hw);
1009 snprintf(sc->assoc_led.name, sizeof(sc->assoc_led.name),
1010 "ath9k-%s:assoc", wiphy_name(sc->hw->wiphy));
1011 ret = ath_register_led(sc, &sc->assoc_led, trigger);
1012 sc->assoc_led.led_type = ATH_LED_ASSOC;
1013 if (ret)
1014 goto fail;
1015
1016 trigger = ieee80211_get_tx_led_name(sc->hw);
1017 snprintf(sc->tx_led.name, sizeof(sc->tx_led.name),
1018 "ath9k-%s:tx", wiphy_name(sc->hw->wiphy));
1019 ret = ath_register_led(sc, &sc->tx_led, trigger);
1020 sc->tx_led.led_type = ATH_LED_TX;
1021 if (ret)
1022 goto fail;
1023
1024 trigger = ieee80211_get_rx_led_name(sc->hw);
1025 snprintf(sc->rx_led.name, sizeof(sc->rx_led.name),
1026 "ath9k-%s:rx", wiphy_name(sc->hw->wiphy));
1027 ret = ath_register_led(sc, &sc->rx_led, trigger);
1028 sc->rx_led.led_type = ATH_LED_RX;
1029 if (ret)
1030 goto fail;
1031
1032 return;
1033
1034fail:
1035 ath_deinit_leds(sc);
1036}
1037
Senthil Balasubramaniane97275c2008-11-13 18:00:02 +05301038#if defined(CONFIG_RFKILL) || defined(CONFIG_RFKILL_MODULE)
Sujith9c84b792008-10-29 10:17:13 +05301039
Vasanthakumar Thiagarajan500c0642008-09-10 18:50:17 +05301040/*******************/
1041/* Rfkill */
1042/*******************/
1043
1044static void ath_radio_enable(struct ath_softc *sc)
1045{
1046 struct ath_hal *ah = sc->sc_ah;
1047 int status;
1048
1049 spin_lock_bh(&sc->sc_resetlock);
1050 if (!ath9k_hw_reset(ah, ah->ah_curchan,
Sujith99405f92008-11-24 12:08:35 +05301051 sc->tx_chan_width,
Vasanthakumar Thiagarajan500c0642008-09-10 18:50:17 +05301052 sc->sc_tx_chainmask,
1053 sc->sc_rx_chainmask,
1054 sc->sc_ht_extprotspacing,
1055 false, &status)) {
1056 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd4632008-11-28 22:18:05 +05301057 "Unable to reset channel %u (%uMhz) "
1058 "flags 0x%x hal status %u\n",
Vasanthakumar Thiagarajan500c0642008-09-10 18:50:17 +05301059 ath9k_hw_mhz2ieee(ah,
1060 ah->ah_curchan->channel,
1061 ah->ah_curchan->channelFlags),
1062 ah->ah_curchan->channel,
1063 ah->ah_curchan->channelFlags, status);
1064 }
1065 spin_unlock_bh(&sc->sc_resetlock);
1066
1067 ath_update_txpow(sc);
1068 if (ath_startrecv(sc) != 0) {
1069 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd4632008-11-28 22:18:05 +05301070 "Unable to restart recv logic\n");
Vasanthakumar Thiagarajan500c0642008-09-10 18:50:17 +05301071 return;
1072 }
1073
1074 if (sc->sc_flags & SC_OP_BEACONS)
1075 ath_beacon_config(sc, ATH_IF_ID_ANY); /* restart beacons */
1076
1077 /* Re-Enable interrupts */
1078 ath9k_hw_set_interrupts(ah, sc->sc_imask);
1079
1080 /* Enable LED */
1081 ath9k_hw_cfg_output(ah, ATH_LED_PIN,
1082 AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
1083 ath9k_hw_set_gpio(ah, ATH_LED_PIN, 0);
1084
1085 ieee80211_wake_queues(sc->hw);
1086}
1087
1088static void ath_radio_disable(struct ath_softc *sc)
1089{
1090 struct ath_hal *ah = sc->sc_ah;
1091 int status;
1092
1093
1094 ieee80211_stop_queues(sc->hw);
1095
1096 /* Disable LED */
1097 ath9k_hw_set_gpio(ah, ATH_LED_PIN, 1);
1098 ath9k_hw_cfg_gpio_input(ah, ATH_LED_PIN);
1099
1100 /* Disable interrupts */
1101 ath9k_hw_set_interrupts(ah, 0);
1102
1103 ath_draintxq(sc, false); /* clear pending tx frames */
1104 ath_stoprecv(sc); /* turn off frame recv */
1105 ath_flushrecv(sc); /* flush recv queue */
1106
1107 spin_lock_bh(&sc->sc_resetlock);
1108 if (!ath9k_hw_reset(ah, ah->ah_curchan,
Sujith99405f92008-11-24 12:08:35 +05301109 sc->tx_chan_width,
Vasanthakumar Thiagarajan500c0642008-09-10 18:50:17 +05301110 sc->sc_tx_chainmask,
1111 sc->sc_rx_chainmask,
1112 sc->sc_ht_extprotspacing,
1113 false, &status)) {
1114 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd4632008-11-28 22:18:05 +05301115 "Unable to reset channel %u (%uMhz) "
1116 "flags 0x%x hal status %u\n",
Vasanthakumar Thiagarajan500c0642008-09-10 18:50:17 +05301117 ath9k_hw_mhz2ieee(ah,
1118 ah->ah_curchan->channel,
1119 ah->ah_curchan->channelFlags),
1120 ah->ah_curchan->channel,
1121 ah->ah_curchan->channelFlags, status);
1122 }
1123 spin_unlock_bh(&sc->sc_resetlock);
1124
1125 ath9k_hw_phy_disable(ah);
1126 ath9k_hw_setpower(ah, ATH9K_PM_FULL_SLEEP);
1127}
1128
1129static bool ath_is_rfkill_set(struct ath_softc *sc)
1130{
1131 struct ath_hal *ah = sc->sc_ah;
1132
1133 return ath9k_hw_gpio_get(ah, ah->ah_rfkill_gpio) ==
1134 ah->ah_rfkill_polarity;
1135}
1136
1137/* h/w rfkill poll function */
1138static void ath_rfkill_poll(struct work_struct *work)
1139{
1140 struct ath_softc *sc = container_of(work, struct ath_softc,
1141 rf_kill.rfkill_poll.work);
1142 bool radio_on;
1143
1144 if (sc->sc_flags & SC_OP_INVALID)
1145 return;
1146
1147 radio_on = !ath_is_rfkill_set(sc);
1148
1149 /*
1150 * enable/disable radio only when there is a
1151 * state change in RF switch
1152 */
1153 if (radio_on == !!(sc->sc_flags & SC_OP_RFKILL_HW_BLOCKED)) {
1154 enum rfkill_state state;
1155
1156 if (sc->sc_flags & SC_OP_RFKILL_SW_BLOCKED) {
1157 state = radio_on ? RFKILL_STATE_SOFT_BLOCKED
1158 : RFKILL_STATE_HARD_BLOCKED;
1159 } else if (radio_on) {
1160 ath_radio_enable(sc);
1161 state = RFKILL_STATE_UNBLOCKED;
1162 } else {
1163 ath_radio_disable(sc);
1164 state = RFKILL_STATE_HARD_BLOCKED;
1165 }
1166
1167 if (state == RFKILL_STATE_HARD_BLOCKED)
1168 sc->sc_flags |= SC_OP_RFKILL_HW_BLOCKED;
1169 else
1170 sc->sc_flags &= ~SC_OP_RFKILL_HW_BLOCKED;
1171
1172 rfkill_force_state(sc->rf_kill.rfkill, state);
1173 }
1174
1175 queue_delayed_work(sc->hw->workqueue, &sc->rf_kill.rfkill_poll,
1176 msecs_to_jiffies(ATH_RFKILL_POLL_INTERVAL));
1177}
1178
1179/* s/w rfkill handler */
1180static int ath_sw_toggle_radio(void *data, enum rfkill_state state)
1181{
1182 struct ath_softc *sc = data;
1183
1184 switch (state) {
1185 case RFKILL_STATE_SOFT_BLOCKED:
1186 if (!(sc->sc_flags & (SC_OP_RFKILL_HW_BLOCKED |
1187 SC_OP_RFKILL_SW_BLOCKED)))
1188 ath_radio_disable(sc);
1189 sc->sc_flags |= SC_OP_RFKILL_SW_BLOCKED;
1190 return 0;
1191 case RFKILL_STATE_UNBLOCKED:
1192 if ((sc->sc_flags & SC_OP_RFKILL_SW_BLOCKED)) {
1193 sc->sc_flags &= ~SC_OP_RFKILL_SW_BLOCKED;
1194 if (sc->sc_flags & SC_OP_RFKILL_HW_BLOCKED) {
1195 DPRINTF(sc, ATH_DBG_FATAL, "Can't turn on the"
Sujith04bd4632008-11-28 22:18:05 +05301196 "radio as it is disabled by h/w\n");
Vasanthakumar Thiagarajan500c0642008-09-10 18:50:17 +05301197 return -EPERM;
1198 }
1199 ath_radio_enable(sc);
1200 }
1201 return 0;
1202 default:
1203 return -EINVAL;
1204 }
1205}
1206
1207/* Init s/w rfkill */
1208static int ath_init_sw_rfkill(struct ath_softc *sc)
1209{
1210 sc->rf_kill.rfkill = rfkill_allocate(wiphy_dev(sc->hw->wiphy),
1211 RFKILL_TYPE_WLAN);
1212 if (!sc->rf_kill.rfkill) {
1213 DPRINTF(sc, ATH_DBG_FATAL, "Failed to allocate rfkill\n");
1214 return -ENOMEM;
1215 }
1216
1217 snprintf(sc->rf_kill.rfkill_name, sizeof(sc->rf_kill.rfkill_name),
1218 "ath9k-%s:rfkill", wiphy_name(sc->hw->wiphy));
1219 sc->rf_kill.rfkill->name = sc->rf_kill.rfkill_name;
1220 sc->rf_kill.rfkill->data = sc;
1221 sc->rf_kill.rfkill->toggle_radio = ath_sw_toggle_radio;
1222 sc->rf_kill.rfkill->state = RFKILL_STATE_UNBLOCKED;
1223 sc->rf_kill.rfkill->user_claim_unsupported = 1;
1224
1225 return 0;
1226}
1227
1228/* Deinitialize rfkill */
1229static void ath_deinit_rfkill(struct ath_softc *sc)
1230{
1231 if (sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
1232 cancel_delayed_work_sync(&sc->rf_kill.rfkill_poll);
1233
1234 if (sc->sc_flags & SC_OP_RFKILL_REGISTERED) {
1235 rfkill_unregister(sc->rf_kill.rfkill);
1236 sc->sc_flags &= ~SC_OP_RFKILL_REGISTERED;
1237 sc->rf_kill.rfkill = NULL;
1238 }
1239}
Sujith9c84b792008-10-29 10:17:13 +05301240
1241static int ath_start_rfkill_poll(struct ath_softc *sc)
1242{
1243 if (sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
1244 queue_delayed_work(sc->hw->workqueue,
1245 &sc->rf_kill.rfkill_poll, 0);
1246
1247 if (!(sc->sc_flags & SC_OP_RFKILL_REGISTERED)) {
1248 if (rfkill_register(sc->rf_kill.rfkill)) {
1249 DPRINTF(sc, ATH_DBG_FATAL,
1250 "Unable to register rfkill\n");
1251 rfkill_free(sc->rf_kill.rfkill);
1252
1253 /* Deinitialize the device */
Senthil Balasubramanian306efdd2008-11-13 18:00:37 +05301254 ath_detach(sc);
Sujith9c84b792008-10-29 10:17:13 +05301255 if (sc->pdev->irq)
1256 free_irq(sc->pdev->irq, sc);
Sujith9c84b792008-10-29 10:17:13 +05301257 pci_iounmap(sc->pdev, sc->mem);
1258 pci_release_region(sc->pdev, 0);
1259 pci_disable_device(sc->pdev);
Sujith9757d552008-11-04 18:25:27 +05301260 ieee80211_free_hw(sc->hw);
Sujith9c84b792008-10-29 10:17:13 +05301261 return -EIO;
1262 } else {
1263 sc->sc_flags |= SC_OP_RFKILL_REGISTERED;
1264 }
1265 }
1266
1267 return 0;
1268}
Vasanthakumar Thiagarajan500c0642008-09-10 18:50:17 +05301269#endif /* CONFIG_RFKILL */
1270
Sujith9c84b792008-10-29 10:17:13 +05301271static void ath_detach(struct ath_softc *sc)
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +05301272{
1273 struct ieee80211_hw *hw = sc->hw;
Sujith9c84b792008-10-29 10:17:13 +05301274 int i = 0;
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +05301275
Sujith04bd4632008-11-28 22:18:05 +05301276 DPRINTF(sc, ATH_DBG_CONFIG, "Detach ATH hw\n");
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +05301277
Senthil Balasubramaniane97275c2008-11-13 18:00:02 +05301278#if defined(CONFIG_RFKILL) || defined(CONFIG_RFKILL_MODULE)
Vasanthakumar Thiagarajan500c0642008-09-10 18:50:17 +05301279 ath_deinit_rfkill(sc);
1280#endif
Vasanthakumar Thiagarajan3fcdfb42008-11-18 01:19:56 +05301281 ath_deinit_leds(sc);
1282
1283 ieee80211_unregister_hw(hw);
1284
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +05301285 ath_rate_control_unregister();
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +05301286
1287 ath_rx_cleanup(sc);
1288 ath_tx_cleanup(sc);
1289
Sujith9c84b792008-10-29 10:17:13 +05301290 tasklet_kill(&sc->intr_tq);
1291 tasklet_kill(&sc->bcon_tasklet);
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +05301292
Sujith9c84b792008-10-29 10:17:13 +05301293 if (!(sc->sc_flags & SC_OP_INVALID))
1294 ath9k_hw_setpower(sc->sc_ah, ATH9K_PM_AWAKE);
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +05301295
Sujith9c84b792008-10-29 10:17:13 +05301296 /* cleanup tx queues */
1297 for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++)
1298 if (ATH_TXQ_SETUP(sc, i))
1299 ath_tx_cleanupq(sc, &sc->sc_txq[i]);
1300
1301 ath9k_hw_detach(sc->sc_ah);
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +05301302}
1303
Sujithff37e332008-11-24 12:07:55 +05301304static int ath_init(u16 devid, struct ath_softc *sc)
1305{
1306 struct ath_hal *ah = NULL;
1307 int status;
1308 int error = 0, i;
1309 int csz = 0;
1310
1311 /* XXX: hardware will not be ready until ath_open() being called */
1312 sc->sc_flags |= SC_OP_INVALID;
Sujith88b126a2008-11-28 22:19:02 +05301313
1314 ath9k_init_debug(sc);
Sujithff37e332008-11-24 12:07:55 +05301315
1316 spin_lock_init(&sc->sc_resetlock);
1317 tasklet_init(&sc->intr_tq, ath9k_tasklet, (unsigned long)sc);
1318 tasklet_init(&sc->bcon_tasklet, ath9k_beacon_tasklet,
1319 (unsigned long)sc);
1320
1321 /*
1322 * Cache line size is used to size and align various
1323 * structures used to communicate with the hardware.
1324 */
1325 bus_read_cachesize(sc, &csz);
1326 /* XXX assert csz is non-zero */
1327 sc->sc_cachelsz = csz << 2; /* convert to bytes */
1328
1329 ah = ath9k_hw_attach(devid, sc, sc->mem, &status);
1330 if (ah == NULL) {
1331 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd4632008-11-28 22:18:05 +05301332 "Unable to attach hardware; HAL status %u\n", status);
Sujithff37e332008-11-24 12:07:55 +05301333 error = -ENXIO;
1334 goto bad;
1335 }
1336 sc->sc_ah = ah;
1337
1338 /* Get the hardware key cache size. */
1339 sc->sc_keymax = ah->ah_caps.keycache_size;
1340 if (sc->sc_keymax > ATH_KEYMAX) {
1341 DPRINTF(sc, ATH_DBG_KEYCACHE,
Sujith04bd4632008-11-28 22:18:05 +05301342 "Warning, using only %u entries in %u key cache\n",
1343 ATH_KEYMAX, sc->sc_keymax);
Sujithff37e332008-11-24 12:07:55 +05301344 sc->sc_keymax = ATH_KEYMAX;
1345 }
1346
1347 /*
1348 * Reset the key cache since some parts do not
1349 * reset the contents on initial power up.
1350 */
1351 for (i = 0; i < sc->sc_keymax; i++)
1352 ath9k_hw_keyreset(ah, (u16) i);
1353 /*
1354 * Mark key cache slots associated with global keys
1355 * as in use. If we knew TKIP was not to be used we
1356 * could leave the +32, +64, and +32+64 slots free.
1357 * XXX only for splitmic.
1358 */
1359 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
1360 set_bit(i, sc->sc_keymap);
1361 set_bit(i + 32, sc->sc_keymap);
1362 set_bit(i + 64, sc->sc_keymap);
1363 set_bit(i + 32 + 64, sc->sc_keymap);
1364 }
1365
1366 /* Collect the channel list using the default country code */
1367
1368 error = ath_setup_channels(sc);
1369 if (error)
1370 goto bad;
1371
1372 /* default to MONITOR mode */
1373 sc->sc_ah->ah_opmode = ATH9K_M_MONITOR;
1374
1375 /* Setup rate tables */
1376
1377 ath_rate_attach(sc);
1378 ath_setup_rates(sc, IEEE80211_BAND_2GHZ);
1379 ath_setup_rates(sc, IEEE80211_BAND_5GHZ);
1380
1381 /*
1382 * Allocate hardware transmit queues: one queue for
1383 * beacon frames and one data queue for each QoS
1384 * priority. Note that the hal handles reseting
1385 * these queues at the needed time.
1386 */
1387 sc->sc_bhalq = ath_beaconq_setup(ah);
1388 if (sc->sc_bhalq == -1) {
1389 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd4632008-11-28 22:18:05 +05301390 "Unable to setup a beacon xmit queue\n");
Sujithff37e332008-11-24 12:07:55 +05301391 error = -EIO;
1392 goto bad2;
1393 }
1394 sc->sc_cabq = ath_txq_setup(sc, ATH9K_TX_QUEUE_CAB, 0);
1395 if (sc->sc_cabq == NULL) {
1396 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd4632008-11-28 22:18:05 +05301397 "Unable to setup CAB xmit queue\n");
Sujithff37e332008-11-24 12:07:55 +05301398 error = -EIO;
1399 goto bad2;
1400 }
1401
1402 sc->sc_config.cabqReadytime = ATH_CABQ_READY_TIME;
1403 ath_cabq_update(sc);
1404
1405 for (i = 0; i < ARRAY_SIZE(sc->sc_haltype2q); i++)
1406 sc->sc_haltype2q[i] = -1;
1407
1408 /* Setup data queues */
1409 /* NB: ensure BK queue is the lowest priority h/w queue */
1410 if (!ath_tx_setup(sc, ATH9K_WME_AC_BK)) {
1411 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd4632008-11-28 22:18:05 +05301412 "Unable to setup xmit queue for BK traffic\n");
Sujithff37e332008-11-24 12:07:55 +05301413 error = -EIO;
1414 goto bad2;
1415 }
1416
1417 if (!ath_tx_setup(sc, ATH9K_WME_AC_BE)) {
1418 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd4632008-11-28 22:18:05 +05301419 "Unable to setup xmit queue for BE traffic\n");
Sujithff37e332008-11-24 12:07:55 +05301420 error = -EIO;
1421 goto bad2;
1422 }
1423 if (!ath_tx_setup(sc, ATH9K_WME_AC_VI)) {
1424 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd4632008-11-28 22:18:05 +05301425 "Unable to setup xmit queue for VI traffic\n");
Sujithff37e332008-11-24 12:07:55 +05301426 error = -EIO;
1427 goto bad2;
1428 }
1429 if (!ath_tx_setup(sc, ATH9K_WME_AC_VO)) {
1430 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd4632008-11-28 22:18:05 +05301431 "Unable to setup xmit queue for VO traffic\n");
Sujithff37e332008-11-24 12:07:55 +05301432 error = -EIO;
1433 goto bad2;
1434 }
1435
1436 /* Initializes the noise floor to a reasonable default value.
1437 * Later on this will be updated during ANI processing. */
1438
1439 sc->sc_ani.sc_noise_floor = ATH_DEFAULT_NOISE_FLOOR;
1440 setup_timer(&sc->sc_ani.timer, ath_ani_calibrate, (unsigned long)sc);
1441
1442 if (ath9k_hw_getcapability(ah, ATH9K_CAP_CIPHER,
1443 ATH9K_CIPHER_TKIP, NULL)) {
1444 /*
1445 * Whether we should enable h/w TKIP MIC.
1446 * XXX: if we don't support WME TKIP MIC, then we wouldn't
1447 * report WMM capable, so it's always safe to turn on
1448 * TKIP MIC in this case.
1449 */
1450 ath9k_hw_setcapability(sc->sc_ah, ATH9K_CAP_TKIP_MIC,
1451 0, 1, NULL);
1452 }
1453
1454 /*
1455 * Check whether the separate key cache entries
1456 * are required to handle both tx+rx MIC keys.
1457 * With split mic keys the number of stations is limited
1458 * to 27 otherwise 59.
1459 */
1460 if (ath9k_hw_getcapability(ah, ATH9K_CAP_CIPHER,
1461 ATH9K_CIPHER_TKIP, NULL)
1462 && ath9k_hw_getcapability(ah, ATH9K_CAP_CIPHER,
1463 ATH9K_CIPHER_MIC, NULL)
1464 && ath9k_hw_getcapability(ah, ATH9K_CAP_TKIP_SPLIT,
1465 0, NULL))
1466 sc->sc_splitmic = 1;
1467
1468 /* turn on mcast key search if possible */
1469 if (!ath9k_hw_getcapability(ah, ATH9K_CAP_MCAST_KEYSRCH, 0, NULL))
1470 (void)ath9k_hw_setcapability(ah, ATH9K_CAP_MCAST_KEYSRCH, 1,
1471 1, NULL);
1472
1473 sc->sc_config.txpowlimit = ATH_TXPOWER_MAX;
1474 sc->sc_config.txpowlimit_override = 0;
1475
1476 /* 11n Capabilities */
1477 if (ah->ah_caps.hw_caps & ATH9K_HW_CAP_HT) {
1478 sc->sc_flags |= SC_OP_TXAGGR;
1479 sc->sc_flags |= SC_OP_RXAGGR;
1480 }
1481
1482 sc->sc_tx_chainmask = ah->ah_caps.tx_chainmask;
1483 sc->sc_rx_chainmask = ah->ah_caps.rx_chainmask;
1484
1485 ath9k_hw_setcapability(ah, ATH9K_CAP_DIVERSITY, 1, true, NULL);
1486 sc->sc_defant = ath9k_hw_getdefantenna(ah);
1487
1488 ath9k_hw_getmac(ah, sc->sc_myaddr);
1489 if (ah->ah_caps.hw_caps & ATH9K_HW_CAP_BSSIDMASK) {
1490 ath9k_hw_getbssidmask(ah, sc->sc_bssidmask);
1491 ATH_SET_VAP_BSSID_MASK(sc->sc_bssidmask);
1492 ath9k_hw_setbssidmask(ah, sc->sc_bssidmask);
1493 }
1494
1495 sc->sc_slottime = ATH9K_SLOT_TIME_9; /* default to short slot time */
1496
1497 /* initialize beacon slots */
1498 for (i = 0; i < ARRAY_SIZE(sc->sc_bslot); i++)
1499 sc->sc_bslot[i] = ATH_IF_ID_ANY;
1500
1501 /* save MISC configurations */
1502 sc->sc_config.swBeaconProcess = 1;
1503
1504#ifdef CONFIG_SLOW_ANT_DIV
1505 /* range is 40 - 255, we use something in the middle */
1506 ath_slow_ant_div_init(&sc->sc_antdiv, sc, 0x127);
1507#endif
1508
1509 /* setup channels and rates */
1510
1511 sc->sbands[IEEE80211_BAND_2GHZ].channels =
1512 sc->channels[IEEE80211_BAND_2GHZ];
1513 sc->sbands[IEEE80211_BAND_2GHZ].bitrates =
1514 sc->rates[IEEE80211_BAND_2GHZ];
1515 sc->sbands[IEEE80211_BAND_2GHZ].band = IEEE80211_BAND_2GHZ;
1516
1517 if (test_bit(ATH9K_MODE_11A, sc->sc_ah->ah_caps.wireless_modes)) {
1518 sc->sbands[IEEE80211_BAND_5GHZ].channels =
1519 sc->channels[IEEE80211_BAND_5GHZ];
1520 sc->sbands[IEEE80211_BAND_5GHZ].bitrates =
1521 sc->rates[IEEE80211_BAND_5GHZ];
1522 sc->sbands[IEEE80211_BAND_5GHZ].band = IEEE80211_BAND_5GHZ;
1523 }
1524
1525 return 0;
1526bad2:
1527 /* cleanup tx queues */
1528 for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++)
1529 if (ATH_TXQ_SETUP(sc, i))
1530 ath_tx_cleanupq(sc, &sc->sc_txq[i]);
1531bad:
1532 if (ah)
1533 ath9k_hw_detach(ah);
1534
1535 return error;
1536}
1537
Sujith9c84b792008-10-29 10:17:13 +05301538static int ath_attach(u16 devid, struct ath_softc *sc)
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +05301539{
1540 struct ieee80211_hw *hw = sc->hw;
1541 int error = 0;
1542
Sujith04bd4632008-11-28 22:18:05 +05301543 DPRINTF(sc, ATH_DBG_CONFIG, "Attach ATH hw\n");
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +05301544
1545 error = ath_init(devid, sc);
1546 if (error != 0)
1547 return error;
1548
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +05301549 /* get mac address from hardware and set in mac80211 */
1550
1551 SET_IEEE80211_PERM_ADDR(hw, sc->sc_myaddr);
1552
Sujith9c84b792008-10-29 10:17:13 +05301553 hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
1554 IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
1555 IEEE80211_HW_SIGNAL_DBM |
1556 IEEE80211_HW_AMPDU_AGGREGATION;
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +05301557
Sujith9c84b792008-10-29 10:17:13 +05301558 hw->wiphy->interface_modes =
1559 BIT(NL80211_IFTYPE_AP) |
1560 BIT(NL80211_IFTYPE_STATION) |
1561 BIT(NL80211_IFTYPE_ADHOC);
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +05301562
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +05301563 hw->queues = 4;
Sujithe63835b2008-11-18 09:07:53 +05301564 hw->max_rates = 4;
1565 hw->max_rate_tries = ATH_11N_TXMAXTRY;
Sujith528f0c62008-10-29 10:14:26 +05301566 hw->sta_data_size = sizeof(struct ath_node);
Sujith5640b082008-10-29 10:16:06 +05301567 hw->vif_data_size = sizeof(struct ath_vap);
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +05301568
1569 /* Register rate control */
1570 hw->rate_control_algorithm = "ath9k_rate_control";
1571 error = ath_rate_control_register();
1572 if (error != 0) {
1573 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd4632008-11-28 22:18:05 +05301574 "Unable to register rate control algorithm: %d\n", error);
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +05301575 ath_rate_control_unregister();
1576 goto bad;
1577 }
1578
Sujith9c84b792008-10-29 10:17:13 +05301579 if (sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_HT) {
1580 setup_ht_cap(&sc->sbands[IEEE80211_BAND_2GHZ].ht_cap);
1581 if (test_bit(ATH9K_MODE_11A, sc->sc_ah->ah_caps.wireless_modes))
1582 setup_ht_cap(&sc->sbands[IEEE80211_BAND_5GHZ].ht_cap);
1583 }
1584
1585 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &sc->sbands[IEEE80211_BAND_2GHZ];
1586 if (test_bit(ATH9K_MODE_11A, sc->sc_ah->ah_caps.wireless_modes))
1587 hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
1588 &sc->sbands[IEEE80211_BAND_5GHZ];
1589
Senthil Balasubramaniandb93e7b2008-11-13 18:01:08 +05301590 /* initialize tx/rx engine */
1591 error = ath_tx_init(sc, ATH_TXBUF);
1592 if (error != 0)
1593 goto detach;
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +05301594
Senthil Balasubramaniandb93e7b2008-11-13 18:01:08 +05301595 error = ath_rx_init(sc, ATH_RXBUF);
1596 if (error != 0)
1597 goto detach;
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +05301598
Senthil Balasubramaniane97275c2008-11-13 18:00:02 +05301599#if defined(CONFIG_RFKILL) || defined(CONFIG_RFKILL_MODULE)
Vasanthakumar Thiagarajan500c0642008-09-10 18:50:17 +05301600 /* Initialze h/w Rfkill */
1601 if (sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
1602 INIT_DELAYED_WORK(&sc->rf_kill.rfkill_poll, ath_rfkill_poll);
1603
1604 /* Initialize s/w rfkill */
1605 if (ath_init_sw_rfkill(sc))
1606 goto detach;
1607#endif
1608
Senthil Balasubramaniandb93e7b2008-11-13 18:01:08 +05301609 error = ieee80211_register_hw(hw);
1610 if (error != 0) {
1611 ath_rate_control_unregister();
1612 goto bad;
1613 }
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +05301614
Senthil Balasubramaniandb93e7b2008-11-13 18:01:08 +05301615 /* Initialize LED control */
1616 ath_init_leds(sc);
Vasanthakumar Thiagarajan8feceb62008-09-10 18:49:27 +05301617
1618 return 0;
1619detach:
1620 ath_detach(sc);
1621bad:
1622 return error;
1623}
1624
Sujithff37e332008-11-24 12:07:55 +05301625int ath_reset(struct ath_softc *sc, bool retry_tx)
1626{
1627 struct ath_hal *ah = sc->sc_ah;
1628 int status;
1629 int error = 0;
1630
1631 ath9k_hw_set_interrupts(ah, 0);
1632 ath_draintxq(sc, retry_tx);
1633 ath_stoprecv(sc);
1634 ath_flushrecv(sc);
1635
1636 spin_lock_bh(&sc->sc_resetlock);
1637 if (!ath9k_hw_reset(ah, sc->sc_ah->ah_curchan,
Sujith99405f92008-11-24 12:08:35 +05301638 sc->tx_chan_width,
Sujithff37e332008-11-24 12:07:55 +05301639 sc->sc_tx_chainmask, sc->sc_rx_chainmask,
1640 sc->sc_ht_extprotspacing, false, &status)) {
1641 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd4632008-11-28 22:18:05 +05301642 "Unable to reset hardware; hal status %u\n", status);
Sujithff37e332008-11-24 12:07:55 +05301643 error = -EIO;
1644 }
1645 spin_unlock_bh(&sc->sc_resetlock);
1646
1647 if (ath_startrecv(sc) != 0)
Sujith04bd4632008-11-28 22:18:05 +05301648 DPRINTF(sc, ATH_DBG_FATAL, "Unable to start recv logic\n");
Sujithff37e332008-11-24 12:07:55 +05301649
1650 /*
1651 * We may be doing a reset in response to a request
1652 * that changes the channel so update any state that
1653 * might change as a result.
1654 */
1655 ath_setcurmode(sc, ath_chan2mode(sc->sc_ah->ah_curchan));
1656
1657 ath_update_txpow(sc);
1658
1659 if (sc->sc_flags & SC_OP_BEACONS)
1660 ath_beacon_config(sc, ATH_IF_ID_ANY); /* restart beacons */
1661
1662 ath9k_hw_set_interrupts(ah, sc->sc_imask);
1663
1664 if (retry_tx) {
1665 int i;
1666 for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
1667 if (ATH_TXQ_SETUP(sc, i)) {
1668 spin_lock_bh(&sc->sc_txq[i].axq_lock);
1669 ath_txq_schedule(sc, &sc->sc_txq[i]);
1670 spin_unlock_bh(&sc->sc_txq[i].axq_lock);
1671 }
1672 }
1673 }
1674
1675 return error;
1676}
1677
1678/*
1679 * This function will allocate both the DMA descriptor structure, and the
1680 * buffers it contains. These are used to contain the descriptors used
1681 * by the system.
1682*/
1683int ath_descdma_setup(struct ath_softc *sc, struct ath_descdma *dd,
1684 struct list_head *head, const char *name,
1685 int nbuf, int ndesc)
1686{
1687#define DS2PHYS(_dd, _ds) \
1688 ((_dd)->dd_desc_paddr + ((caddr_t)(_ds) - (caddr_t)(_dd)->dd_desc))
1689#define ATH_DESC_4KB_BOUND_CHECK(_daddr) ((((_daddr) & 0xFFF) > 0xF7F) ? 1 : 0)
1690#define ATH_DESC_4KB_BOUND_NUM_SKIPPED(_len) ((_len) / 4096)
1691
1692 struct ath_desc *ds;
1693 struct ath_buf *bf;
1694 int i, bsize, error;
1695
Sujith04bd4632008-11-28 22:18:05 +05301696 DPRINTF(sc, ATH_DBG_CONFIG, "%s DMA: %u buffers %u desc/buf\n",
1697 name, nbuf, ndesc);
Sujithff37e332008-11-24 12:07:55 +05301698
1699 /* ath_desc must be a multiple of DWORDs */
1700 if ((sizeof(struct ath_desc) % 4) != 0) {
Sujith04bd4632008-11-28 22:18:05 +05301701 DPRINTF(sc, ATH_DBG_FATAL, "ath_desc not DWORD aligned\n");
Sujithff37e332008-11-24 12:07:55 +05301702 ASSERT((sizeof(struct ath_desc) % 4) == 0);
1703 error = -ENOMEM;
1704 goto fail;
1705 }
1706
1707 dd->dd_name = name;
1708 dd->dd_desc_len = sizeof(struct ath_desc) * nbuf * ndesc;
1709
1710 /*
1711 * Need additional DMA memory because we can't use
1712 * descriptors that cross the 4K page boundary. Assume
1713 * one skipped descriptor per 4K page.
1714 */
1715 if (!(sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_4KB_SPLITTRANS)) {
1716 u32 ndesc_skipped =
1717 ATH_DESC_4KB_BOUND_NUM_SKIPPED(dd->dd_desc_len);
1718 u32 dma_len;
1719
1720 while (ndesc_skipped) {
1721 dma_len = ndesc_skipped * sizeof(struct ath_desc);
1722 dd->dd_desc_len += dma_len;
1723
1724 ndesc_skipped = ATH_DESC_4KB_BOUND_NUM_SKIPPED(dma_len);
1725 };
1726 }
1727
1728 /* allocate descriptors */
1729 dd->dd_desc = pci_alloc_consistent(sc->pdev,
1730 dd->dd_desc_len,
1731 &dd->dd_desc_paddr);
1732 if (dd->dd_desc == NULL) {
1733 error = -ENOMEM;
1734 goto fail;
1735 }
1736 ds = dd->dd_desc;
Sujith04bd4632008-11-28 22:18:05 +05301737 DPRINTF(sc, ATH_DBG_CONFIG, "%s DMA map: %p (%u) -> %llx (%u)\n",
1738 dd->dd_name, ds, (u32) dd->dd_desc_len,
Sujithff37e332008-11-24 12:07:55 +05301739 ito64(dd->dd_desc_paddr), /*XXX*/(u32) dd->dd_desc_len);
1740
1741 /* allocate buffers */
1742 bsize = sizeof(struct ath_buf) * nbuf;
1743 bf = kmalloc(bsize, GFP_KERNEL);
1744 if (bf == NULL) {
1745 error = -ENOMEM;
1746 goto fail2;
1747 }
1748 memset(bf, 0, bsize);
1749 dd->dd_bufptr = bf;
1750
1751 INIT_LIST_HEAD(head);
1752 for (i = 0; i < nbuf; i++, bf++, ds += ndesc) {
1753 bf->bf_desc = ds;
1754 bf->bf_daddr = DS2PHYS(dd, ds);
1755
1756 if (!(sc->sc_ah->ah_caps.hw_caps &
1757 ATH9K_HW_CAP_4KB_SPLITTRANS)) {
1758 /*
1759 * Skip descriptor addresses which can cause 4KB
1760 * boundary crossing (addr + length) with a 32 dword
1761 * descriptor fetch.
1762 */
1763 while (ATH_DESC_4KB_BOUND_CHECK(bf->bf_daddr)) {
1764 ASSERT((caddr_t) bf->bf_desc <
1765 ((caddr_t) dd->dd_desc +
1766 dd->dd_desc_len));
1767
1768 ds += ndesc;
1769 bf->bf_desc = ds;
1770 bf->bf_daddr = DS2PHYS(dd, ds);
1771 }
1772 }
1773 list_add_tail(&bf->list, head);
1774 }
1775 return 0;
1776fail2:
1777 pci_free_consistent(sc->pdev,
1778 dd->dd_desc_len, dd->dd_desc, dd->dd_desc_paddr);
1779fail:
1780 memset(dd, 0, sizeof(*dd));
1781 return error;
1782#undef ATH_DESC_4KB_BOUND_CHECK
1783#undef ATH_DESC_4KB_BOUND_NUM_SKIPPED
1784#undef DS2PHYS
1785}
1786
1787void ath_descdma_cleanup(struct ath_softc *sc,
1788 struct ath_descdma *dd,
1789 struct list_head *head)
1790{
1791 pci_free_consistent(sc->pdev,
1792 dd->dd_desc_len, dd->dd_desc, dd->dd_desc_paddr);
1793
1794 INIT_LIST_HEAD(head);
1795 kfree(dd->dd_bufptr);
1796 memset(dd, 0, sizeof(*dd));
1797}
1798
1799int ath_get_hal_qnum(u16 queue, struct ath_softc *sc)
1800{
1801 int qnum;
1802
1803 switch (queue) {
1804 case 0:
1805 qnum = sc->sc_haltype2q[ATH9K_WME_AC_VO];
1806 break;
1807 case 1:
1808 qnum = sc->sc_haltype2q[ATH9K_WME_AC_VI];
1809 break;
1810 case 2:
1811 qnum = sc->sc_haltype2q[ATH9K_WME_AC_BE];
1812 break;
1813 case 3:
1814 qnum = sc->sc_haltype2q[ATH9K_WME_AC_BK];
1815 break;
1816 default:
1817 qnum = sc->sc_haltype2q[ATH9K_WME_AC_BE];
1818 break;
1819 }
1820
1821 return qnum;
1822}
1823
1824int ath_get_mac80211_qnum(u32 queue, struct ath_softc *sc)
1825{
1826 int qnum;
1827
1828 switch (queue) {
1829 case ATH9K_WME_AC_VO:
1830 qnum = 0;
1831 break;
1832 case ATH9K_WME_AC_VI:
1833 qnum = 1;
1834 break;
1835 case ATH9K_WME_AC_BE:
1836 qnum = 2;
1837 break;
1838 case ATH9K_WME_AC_BK:
1839 qnum = 3;
1840 break;
1841 default:
1842 qnum = -1;
1843 break;
1844 }
1845
1846 return qnum;
1847}
1848
1849/**********************/
1850/* mac80211 callbacks */
1851/**********************/
1852
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001853static int ath9k_start(struct ieee80211_hw *hw)
1854{
1855 struct ath_softc *sc = hw->priv;
1856 struct ieee80211_channel *curchan = hw->conf.channel;
Sujithff37e332008-11-24 12:07:55 +05301857 struct ath9k_channel *init_channel;
1858 int error = 0, pos, status;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001859
Sujith04bd4632008-11-28 22:18:05 +05301860 DPRINTF(sc, ATH_DBG_CONFIG, "Starting driver with "
1861 "initial channel: %d MHz\n", curchan->center_freq);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001862
1863 /* setup initial channel */
1864
1865 pos = ath_get_channel(sc, curchan);
1866 if (pos == -1) {
Sujith04bd4632008-11-28 22:18:05 +05301867 DPRINTF(sc, ATH_DBG_FATAL, "Invalid channel: %d\n", curchan->center_freq);
Sujith9c84b792008-10-29 10:17:13 +05301868 error = -EINVAL;
Sujithff37e332008-11-24 12:07:55 +05301869 goto error;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001870 }
1871
Sujith99405f92008-11-24 12:08:35 +05301872 sc->tx_chan_width = ATH9K_HT_MACMODE_20;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001873 sc->sc_ah->ah_channels[pos].chanmode =
1874 (curchan->band == IEEE80211_BAND_2GHZ) ? CHANNEL_G : CHANNEL_A;
Sujithff37e332008-11-24 12:07:55 +05301875 init_channel = &sc->sc_ah->ah_channels[pos];
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001876
Sujithff37e332008-11-24 12:07:55 +05301877 /* Reset SERDES registers */
1878 ath9k_hw_configpcipowersave(sc->sc_ah, 0);
1879
1880 /*
1881 * The basic interface to setting the hardware in a good
1882 * state is ``reset''. On return the hardware is known to
1883 * be powered up and with interrupts disabled. This must
1884 * be followed by initialization of the appropriate bits
1885 * and then setup of the interrupt mask.
1886 */
1887 spin_lock_bh(&sc->sc_resetlock);
1888 if (!ath9k_hw_reset(sc->sc_ah, init_channel,
Sujith99405f92008-11-24 12:08:35 +05301889 sc->tx_chan_width,
Sujithff37e332008-11-24 12:07:55 +05301890 sc->sc_tx_chainmask, sc->sc_rx_chainmask,
1891 sc->sc_ht_extprotspacing, false, &status)) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001892 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd4632008-11-28 22:18:05 +05301893 "Unable to reset hardware; hal status %u "
1894 "(freq %u flags 0x%x)\n", status,
Sujithff37e332008-11-24 12:07:55 +05301895 init_channel->channel, init_channel->channelFlags);
1896 error = -EIO;
1897 spin_unlock_bh(&sc->sc_resetlock);
1898 goto error;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001899 }
Sujithff37e332008-11-24 12:07:55 +05301900 spin_unlock_bh(&sc->sc_resetlock);
1901
1902 /*
1903 * This is needed only to setup initial state
1904 * but it's best done after a reset.
1905 */
1906 ath_update_txpow(sc);
1907
1908 /*
1909 * Setup the hardware after reset:
1910 * The receive engine is set going.
1911 * Frame transmit is handled entirely
1912 * in the frame output path; there's nothing to do
1913 * here except setup the interrupt mask.
1914 */
1915 if (ath_startrecv(sc) != 0) {
1916 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd4632008-11-28 22:18:05 +05301917 "Unable to start recv logic\n");
Sujithff37e332008-11-24 12:07:55 +05301918 error = -EIO;
1919 goto error;
1920 }
1921
1922 /* Setup our intr mask. */
1923 sc->sc_imask = ATH9K_INT_RX | ATH9K_INT_TX
1924 | ATH9K_INT_RXEOL | ATH9K_INT_RXORN
1925 | ATH9K_INT_FATAL | ATH9K_INT_GLOBAL;
1926
1927 if (sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_GTT)
1928 sc->sc_imask |= ATH9K_INT_GTT;
1929
1930 if (sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_HT)
1931 sc->sc_imask |= ATH9K_INT_CST;
1932
1933 /*
1934 * Enable MIB interrupts when there are hardware phy counters.
1935 * Note we only do this (at the moment) for station mode.
1936 */
1937 if (ath9k_hw_phycounters(sc->sc_ah) &&
1938 ((sc->sc_ah->ah_opmode == ATH9K_M_STA) ||
1939 (sc->sc_ah->ah_opmode == ATH9K_M_IBSS)))
1940 sc->sc_imask |= ATH9K_INT_MIB;
1941 /*
1942 * Some hardware processes the TIM IE and fires an
1943 * interrupt when the TIM bit is set. For hardware
1944 * that does, if not overridden by configuration,
1945 * enable the TIM interrupt when operating as station.
1946 */
1947 if ((sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_ENHANCEDPM) &&
1948 (sc->sc_ah->ah_opmode == ATH9K_M_STA) &&
1949 !sc->sc_config.swBeaconProcess)
1950 sc->sc_imask |= ATH9K_INT_TIM;
1951
1952 ath_setcurmode(sc, ath_chan2mode(init_channel));
1953
1954 sc->sc_flags &= ~SC_OP_INVALID;
1955
1956 /* Disable BMISS interrupt when we're not associated */
1957 sc->sc_imask &= ~(ATH9K_INT_SWBA | ATH9K_INT_BMISS);
1958 ath9k_hw_set_interrupts(sc->sc_ah, sc->sc_imask);
1959
1960 ieee80211_wake_queues(sc->hw);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001961
Senthil Balasubramaniane97275c2008-11-13 18:00:02 +05301962#if defined(CONFIG_RFKILL) || defined(CONFIG_RFKILL_MODULE)
Sujith9c84b792008-10-29 10:17:13 +05301963 error = ath_start_rfkill_poll(sc);
Vasanthakumar Thiagarajan500c0642008-09-10 18:50:17 +05301964#endif
1965
Sujithff37e332008-11-24 12:07:55 +05301966error:
Sujith9c84b792008-10-29 10:17:13 +05301967 return error;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001968}
1969
1970static int ath9k_tx(struct ieee80211_hw *hw,
1971 struct sk_buff *skb)
1972{
Jouni Malinen147583c2008-08-11 14:01:50 +03001973 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Sujith528f0c62008-10-29 10:14:26 +05301974 struct ath_softc *sc = hw->priv;
1975 struct ath_tx_control txctl;
1976 int hdrlen, padsize;
1977
1978 memset(&txctl, 0, sizeof(struct ath_tx_control));
Jouni Malinen147583c2008-08-11 14:01:50 +03001979
1980 /*
1981 * As a temporary workaround, assign seq# here; this will likely need
1982 * to be cleaned up to work better with Beacon transmission and virtual
1983 * BSSes.
1984 */
1985 if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
1986 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1987 if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
1988 sc->seq_no += 0x10;
1989 hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
1990 hdr->seq_ctrl |= cpu_to_le16(sc->seq_no);
1991 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07001992
1993 /* Add the padding after the header if this is not already done */
1994 hdrlen = ieee80211_get_hdrlen_from_skb(skb);
1995 if (hdrlen & 3) {
1996 padsize = hdrlen % 4;
1997 if (skb_headroom(skb) < padsize)
1998 return -1;
1999 skb_push(skb, padsize);
2000 memmove(skb->data, skb->data + padsize, hdrlen);
2001 }
2002
Sujith528f0c62008-10-29 10:14:26 +05302003 /* Check if a tx queue is available */
2004
2005 txctl.txq = ath_test_get_txq(sc, skb);
2006 if (!txctl.txq)
2007 goto exit;
2008
Sujith04bd4632008-11-28 22:18:05 +05302009 DPRINTF(sc, ATH_DBG_XMIT, "transmitting packet, skb: %p\n", skb);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002010
Sujith528f0c62008-10-29 10:14:26 +05302011 if (ath_tx_start(sc, skb, &txctl) != 0) {
Sujith04bd4632008-11-28 22:18:05 +05302012 DPRINTF(sc, ATH_DBG_XMIT, "TX failed\n");
Sujith528f0c62008-10-29 10:14:26 +05302013 goto exit;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002014 }
2015
2016 return 0;
Sujith528f0c62008-10-29 10:14:26 +05302017exit:
2018 dev_kfree_skb_any(skb);
2019 return 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002020}
2021
2022static void ath9k_stop(struct ieee80211_hw *hw)
2023{
2024 struct ath_softc *sc = hw->priv;
Sujith9c84b792008-10-29 10:17:13 +05302025
2026 if (sc->sc_flags & SC_OP_INVALID) {
Sujith04bd4632008-11-28 22:18:05 +05302027 DPRINTF(sc, ATH_DBG_ANY, "Device not present\n");
Sujith9c84b792008-10-29 10:17:13 +05302028 return;
2029 }
2030
Sujith04bd4632008-11-28 22:18:05 +05302031 DPRINTF(sc, ATH_DBG_CONFIG, "Cleaning up\n");
Sujithff37e332008-11-24 12:07:55 +05302032
2033 ieee80211_stop_queues(sc->hw);
2034
2035 /* make sure h/w will not generate any interrupt
2036 * before setting the invalid flag. */
2037 ath9k_hw_set_interrupts(sc->sc_ah, 0);
2038
2039 if (!(sc->sc_flags & SC_OP_INVALID)) {
2040 ath_draintxq(sc, false);
2041 ath_stoprecv(sc);
2042 ath9k_hw_phy_disable(sc->sc_ah);
2043 } else
2044 sc->sc_rxlink = NULL;
2045
2046#if defined(CONFIG_RFKILL) || defined(CONFIG_RFKILL_MODULE)
2047 if (sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
2048 cancel_delayed_work_sync(&sc->rf_kill.rfkill_poll);
2049#endif
2050 /* disable HAL and put h/w to sleep */
2051 ath9k_hw_disable(sc->sc_ah);
2052 ath9k_hw_configpcipowersave(sc->sc_ah, 1);
2053
2054 sc->sc_flags |= SC_OP_INVALID;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002055
Sujith04bd4632008-11-28 22:18:05 +05302056 DPRINTF(sc, ATH_DBG_CONFIG, "Driver halt\n");
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002057}
2058
2059static int ath9k_add_interface(struct ieee80211_hw *hw,
2060 struct ieee80211_if_init_conf *conf)
2061{
2062 struct ath_softc *sc = hw->priv;
Sujith5640b082008-10-29 10:16:06 +05302063 struct ath_vap *avp = (void *)conf->vif->drv_priv;
2064 int ic_opmode = 0;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002065
2066 /* Support only vap for now */
2067
2068 if (sc->sc_nvaps)
2069 return -ENOBUFS;
2070
2071 switch (conf->type) {
Johannes Berg05c914f2008-09-11 00:01:58 +02002072 case NL80211_IFTYPE_STATION:
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002073 ic_opmode = ATH9K_M_STA;
2074 break;
Johannes Berg05c914f2008-09-11 00:01:58 +02002075 case NL80211_IFTYPE_ADHOC:
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002076 ic_opmode = ATH9K_M_IBSS;
2077 break;
Johannes Berg05c914f2008-09-11 00:01:58 +02002078 case NL80211_IFTYPE_AP:
Jouni Malinen2ad67de2008-08-11 14:01:47 +03002079 ic_opmode = ATH9K_M_HOSTAP;
2080 break;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002081 default:
2082 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd4632008-11-28 22:18:05 +05302083 "Interface type %d not yet supported\n", conf->type);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002084 return -EOPNOTSUPP;
2085 }
2086
Sujith04bd4632008-11-28 22:18:05 +05302087 DPRINTF(sc, ATH_DBG_CONFIG, "Attach a VAP of type: %d\n", ic_opmode);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002088
Sujith5640b082008-10-29 10:16:06 +05302089 /* Set the VAP opmode */
2090 avp->av_opmode = ic_opmode;
2091 avp->av_bslot = -1;
2092
2093 if (ic_opmode == ATH9K_M_HOSTAP)
2094 ath9k_hw_set_tsfadjust(sc->sc_ah, 1);
2095
2096 sc->sc_vaps[0] = conf->vif;
2097 sc->sc_nvaps++;
2098
2099 /* Set the device opmode */
2100 sc->sc_ah->ah_opmode = ic_opmode;
2101
Luis R. Rodriguez6f255422008-10-03 15:45:27 -07002102 if (conf->type == NL80211_IFTYPE_AP) {
2103 /* TODO: is this a suitable place to start ANI for AP mode? */
2104 /* Start ANI */
2105 mod_timer(&sc->sc_ani.timer,
2106 jiffies + msecs_to_jiffies(ATH_ANI_POLLINTERVAL));
2107 }
2108
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002109 return 0;
2110}
2111
2112static void ath9k_remove_interface(struct ieee80211_hw *hw,
2113 struct ieee80211_if_init_conf *conf)
2114{
2115 struct ath_softc *sc = hw->priv;
Sujith5640b082008-10-29 10:16:06 +05302116 struct ath_vap *avp = (void *)conf->vif->drv_priv;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002117
Sujith04bd4632008-11-28 22:18:05 +05302118 DPRINTF(sc, ATH_DBG_CONFIG, "Detach Interface\n");
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002119
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002120#ifdef CONFIG_SLOW_ANT_DIV
2121 ath_slow_ant_div_stop(&sc->sc_antdiv);
2122#endif
Luis R. Rodriguez6f255422008-10-03 15:45:27 -07002123 /* Stop ANI */
2124 del_timer_sync(&sc->sc_ani.timer);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002125
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002126 /* Reclaim beacon resources */
Sujithb4696c8b2008-08-11 14:04:52 +05302127 if (sc->sc_ah->ah_opmode == ATH9K_M_HOSTAP ||
2128 sc->sc_ah->ah_opmode == ATH9K_M_IBSS) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002129 ath9k_hw_stoptxdma(sc->sc_ah, sc->sc_bhalq);
2130 ath_beacon_return(sc, avp);
2131 }
2132
Sujith672840a2008-08-11 14:05:08 +05302133 sc->sc_flags &= ~SC_OP_BEACONS;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002134
Sujith5640b082008-10-29 10:16:06 +05302135 sc->sc_vaps[0] = NULL;
2136 sc->sc_nvaps--;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002137}
2138
Johannes Berge8975582008-10-09 12:18:51 +02002139static int ath9k_config(struct ieee80211_hw *hw, u32 changed)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002140{
2141 struct ath_softc *sc = hw->priv;
Johannes Berge8975582008-10-09 12:18:51 +02002142 struct ieee80211_conf *conf = &hw->conf;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002143
Sujith99405f92008-11-24 12:08:35 +05302144 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
2145 struct ieee80211_channel *curchan = hw->conf.channel;
2146 int pos;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002147
Sujith04bd4632008-11-28 22:18:05 +05302148 DPRINTF(sc, ATH_DBG_CONFIG, "Set channel: %d MHz\n",
2149 curchan->center_freq);
Johannes Bergae5eb022008-10-14 16:58:37 +02002150
Sujith99405f92008-11-24 12:08:35 +05302151 pos = ath_get_channel(sc, curchan);
2152 if (pos == -1) {
Sujith04bd4632008-11-28 22:18:05 +05302153 DPRINTF(sc, ATH_DBG_FATAL, "Invalid channel: %d\n",
2154 curchan->center_freq);
Sujith99405f92008-11-24 12:08:35 +05302155 return -EINVAL;
2156 }
2157
2158 sc->tx_chan_width = ATH9K_HT_MACMODE_20;
2159 sc->sc_ah->ah_channels[pos].chanmode =
2160 (curchan->band == IEEE80211_BAND_2GHZ) ?
2161 CHANNEL_G : CHANNEL_A;
2162
Sujithe11602b2008-11-27 09:46:27 +05302163 if ((sc->sc_ah->ah_opmode == ATH9K_M_HOSTAP) &&
2164 (conf->ht.enabled)) {
2165 sc->tx_chan_width = (!!conf->ht.sec_chan_offset) ?
2166 ATH9K_HT_MACMODE_2040 : ATH9K_HT_MACMODE_20;
2167
2168 sc->sc_ah->ah_channels[pos].chanmode =
2169 ath_get_extchanmode(sc, curchan,
2170 conf->ht.sec_chan_offset,
2171 sc->tx_chan_width);
2172 }
2173
2174 if (ath_set_channel(sc, &sc->sc_ah->ah_channels[pos]) < 0) {
Sujith04bd4632008-11-28 22:18:05 +05302175 DPRINTF(sc, ATH_DBG_FATAL, "Unable to set channel\n");
Sujithe11602b2008-11-27 09:46:27 +05302176 return -EINVAL;
2177 }
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002178 }
2179
Sujith99405f92008-11-24 12:08:35 +05302180 if (changed & IEEE80211_CONF_CHANGE_HT)
2181 ath_update_chainmask(sc, conf->ht.enabled);
Sujith86b89ee2008-08-07 10:54:57 +05302182
Luis R. Rodriguez5c020dc2008-10-22 13:28:45 -07002183 if (changed & IEEE80211_CONF_CHANGE_POWER)
2184 sc->sc_config.txpowlimit = 2 * conf->power_level;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002185
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002186 return 0;
2187}
2188
2189static int ath9k_config_interface(struct ieee80211_hw *hw,
2190 struct ieee80211_vif *vif,
2191 struct ieee80211_if_conf *conf)
2192{
2193 struct ath_softc *sc = hw->priv;
Jouni Malinen2ad67de2008-08-11 14:01:47 +03002194 struct ath_hal *ah = sc->sc_ah;
Sujith5640b082008-10-29 10:16:06 +05302195 struct ath_vap *avp = (void *)vif->drv_priv;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002196 u32 rfilt = 0;
2197 int error, i;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002198
Jouni Malinen2ad67de2008-08-11 14:01:47 +03002199 /* TODO: Need to decide which hw opmode to use for multi-interface
2200 * cases */
Johannes Berg05c914f2008-09-11 00:01:58 +02002201 if (vif->type == NL80211_IFTYPE_AP &&
Jouni Malinen2ad67de2008-08-11 14:01:47 +03002202 ah->ah_opmode != ATH9K_M_HOSTAP) {
2203 ah->ah_opmode = ATH9K_M_HOSTAP;
2204 ath9k_hw_setopmode(ah);
2205 ath9k_hw_write_associd(ah, sc->sc_myaddr, 0);
2206 /* Request full reset to get hw opmode changed properly */
2207 sc->sc_flags |= SC_OP_FULL_RESET;
2208 }
2209
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002210 if ((conf->changed & IEEE80211_IFCC_BSSID) &&
2211 !is_zero_ether_addr(conf->bssid)) {
2212 switch (vif->type) {
Johannes Berg05c914f2008-09-11 00:01:58 +02002213 case NL80211_IFTYPE_STATION:
2214 case NL80211_IFTYPE_ADHOC:
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002215 /* Set BSSID */
2216 memcpy(sc->sc_curbssid, conf->bssid, ETH_ALEN);
2217 sc->sc_curaid = 0;
2218 ath9k_hw_write_associd(sc->sc_ah, sc->sc_curbssid,
2219 sc->sc_curaid);
2220
2221 /* Set aggregation protection mode parameters */
2222 sc->sc_config.ath_aggr_prot = 0;
2223
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002224 DPRINTF(sc, ATH_DBG_CONFIG,
Sujith04bd4632008-11-28 22:18:05 +05302225 "RX filter 0x%x bssid %pM aid 0x%x\n",
2226 rfilt, sc->sc_curbssid, sc->sc_curaid);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002227
2228 /* need to reconfigure the beacon */
Sujith672840a2008-08-11 14:05:08 +05302229 sc->sc_flags &= ~SC_OP_BEACONS ;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002230
2231 break;
2232 default:
2233 break;
2234 }
2235 }
2236
2237 if ((conf->changed & IEEE80211_IFCC_BEACON) &&
Johannes Berg05c914f2008-09-11 00:01:58 +02002238 ((vif->type == NL80211_IFTYPE_ADHOC) ||
2239 (vif->type == NL80211_IFTYPE_AP))) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002240 /*
2241 * Allocate and setup the beacon frame.
2242 *
2243 * Stop any previous beacon DMA. This may be
2244 * necessary, for example, when an ibss merge
2245 * causes reconfiguration; we may be called
2246 * with beacon transmission active.
2247 */
2248 ath9k_hw_stoptxdma(sc->sc_ah, sc->sc_bhalq);
2249
2250 error = ath_beacon_alloc(sc, 0);
2251 if (error != 0)
2252 return error;
2253
2254 ath_beacon_sync(sc, 0);
2255 }
2256
2257 /* Check for WLAN_CAPABILITY_PRIVACY ? */
Sujith5640b082008-10-29 10:16:06 +05302258 if ((avp->av_opmode != ATH9K_M_STA)) {
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002259 for (i = 0; i < IEEE80211_WEP_NKID; i++)
2260 if (ath9k_hw_keyisvalid(sc->sc_ah, (u16)i))
2261 ath9k_hw_keysetmac(sc->sc_ah,
2262 (u16)i,
2263 sc->sc_curbssid);
2264 }
2265
2266 /* Only legacy IBSS for now */
Johannes Berg05c914f2008-09-11 00:01:58 +02002267 if (vif->type == NL80211_IFTYPE_ADHOC)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002268 ath_update_chainmask(sc, 0);
2269
2270 return 0;
2271}
2272
2273#define SUPPORTED_FILTERS \
2274 (FIF_PROMISC_IN_BSS | \
2275 FIF_ALLMULTI | \
2276 FIF_CONTROL | \
2277 FIF_OTHER_BSS | \
2278 FIF_BCN_PRBRESP_PROMISC | \
2279 FIF_FCSFAIL)
2280
Sujith7dcfdcd2008-08-11 14:03:13 +05302281/* FIXME: sc->sc_full_reset ? */
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002282static void ath9k_configure_filter(struct ieee80211_hw *hw,
2283 unsigned int changed_flags,
2284 unsigned int *total_flags,
2285 int mc_count,
2286 struct dev_mc_list *mclist)
2287{
2288 struct ath_softc *sc = hw->priv;
Sujith7dcfdcd2008-08-11 14:03:13 +05302289 u32 rfilt;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002290
2291 changed_flags &= SUPPORTED_FILTERS;
2292 *total_flags &= SUPPORTED_FILTERS;
2293
Sujith7dcfdcd2008-08-11 14:03:13 +05302294 sc->rx_filter = *total_flags;
2295 rfilt = ath_calcrxfilter(sc);
2296 ath9k_hw_setrxfilter(sc->sc_ah, rfilt);
2297
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002298 if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
2299 if (*total_flags & FIF_BCN_PRBRESP_PROMISC)
Sujith7dcfdcd2008-08-11 14:03:13 +05302300 ath9k_hw_write_associd(sc->sc_ah, ath_bcast_mac, 0);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002301 }
Sujith7dcfdcd2008-08-11 14:03:13 +05302302
Sujith04bd4632008-11-28 22:18:05 +05302303 DPRINTF(sc, ATH_DBG_CONFIG, "Set HW RX filter: 0x%x\n", sc->rx_filter);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002304}
2305
2306static void ath9k_sta_notify(struct ieee80211_hw *hw,
2307 struct ieee80211_vif *vif,
2308 enum sta_notify_cmd cmd,
Johannes Berg17741cd2008-09-11 00:02:02 +02002309 struct ieee80211_sta *sta)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002310{
2311 struct ath_softc *sc = hw->priv;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002312
2313 switch (cmd) {
2314 case STA_NOTIFY_ADD:
Sujith5640b082008-10-29 10:16:06 +05302315 ath_node_attach(sc, sta);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002316 break;
2317 case STA_NOTIFY_REMOVE:
Sujithb5aa9bf2008-10-29 10:13:31 +05302318 ath_node_detach(sc, sta);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002319 break;
2320 default:
2321 break;
2322 }
2323}
2324
2325static int ath9k_conf_tx(struct ieee80211_hw *hw,
2326 u16 queue,
2327 const struct ieee80211_tx_queue_params *params)
2328{
2329 struct ath_softc *sc = hw->priv;
Sujithea9880f2008-08-07 10:53:10 +05302330 struct ath9k_tx_queue_info qi;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002331 int ret = 0, qnum;
2332
2333 if (queue >= WME_NUM_AC)
2334 return 0;
2335
2336 qi.tqi_aifs = params->aifs;
2337 qi.tqi_cwmin = params->cw_min;
2338 qi.tqi_cwmax = params->cw_max;
2339 qi.tqi_burstTime = params->txop;
2340 qnum = ath_get_hal_qnum(queue, sc);
2341
2342 DPRINTF(sc, ATH_DBG_CONFIG,
Sujith04bd4632008-11-28 22:18:05 +05302343 "Configure tx [queue/halq] [%d/%d], "
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002344 "aifs: %d, cw_min: %d, cw_max: %d, txop: %d\n",
Sujith04bd4632008-11-28 22:18:05 +05302345 queue, qnum, params->aifs, params->cw_min,
2346 params->cw_max, params->txop);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002347
2348 ret = ath_txq_update(sc, qnum, &qi);
2349 if (ret)
Sujith04bd4632008-11-28 22:18:05 +05302350 DPRINTF(sc, ATH_DBG_FATAL, "TXQ Update failed\n");
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002351
2352 return ret;
2353}
2354
2355static int ath9k_set_key(struct ieee80211_hw *hw,
2356 enum set_key_cmd cmd,
2357 const u8 *local_addr,
2358 const u8 *addr,
2359 struct ieee80211_key_conf *key)
2360{
2361 struct ath_softc *sc = hw->priv;
2362 int ret = 0;
2363
Sujith04bd4632008-11-28 22:18:05 +05302364 DPRINTF(sc, ATH_DBG_KEYCACHE, "Set HW Key\n");
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002365
2366 switch (cmd) {
2367 case SET_KEY:
2368 ret = ath_key_config(sc, addr, key);
2369 if (!ret) {
2370 set_bit(key->keyidx, sc->sc_keymap);
2371 key->hw_key_idx = key->keyidx;
2372 /* push IV and Michael MIC generation to stack */
2373 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
Senthil Balasubramanian1b961752008-09-01 19:45:21 +05302374 if (key->alg == ALG_TKIP)
2375 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002376 }
2377 break;
2378 case DISABLE_KEY:
2379 ath_key_delete(sc, key);
2380 clear_bit(key->keyidx, sc->sc_keymap);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002381 break;
2382 default:
2383 ret = -EINVAL;
2384 }
2385
2386 return ret;
2387}
2388
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002389static void ath9k_bss_info_changed(struct ieee80211_hw *hw,
2390 struct ieee80211_vif *vif,
2391 struct ieee80211_bss_conf *bss_conf,
2392 u32 changed)
2393{
2394 struct ath_softc *sc = hw->priv;
2395
2396 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
Sujith04bd4632008-11-28 22:18:05 +05302397 DPRINTF(sc, ATH_DBG_CONFIG, "BSS Changed PREAMBLE %d\n",
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002398 bss_conf->use_short_preamble);
2399 if (bss_conf->use_short_preamble)
Sujith672840a2008-08-11 14:05:08 +05302400 sc->sc_flags |= SC_OP_PREAMBLE_SHORT;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002401 else
Sujith672840a2008-08-11 14:05:08 +05302402 sc->sc_flags &= ~SC_OP_PREAMBLE_SHORT;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002403 }
2404
2405 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
Sujith04bd4632008-11-28 22:18:05 +05302406 DPRINTF(sc, ATH_DBG_CONFIG, "BSS Changed CTS PROT %d\n",
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002407 bss_conf->use_cts_prot);
2408 if (bss_conf->use_cts_prot &&
2409 hw->conf.channel->band != IEEE80211_BAND_5GHZ)
Sujith672840a2008-08-11 14:05:08 +05302410 sc->sc_flags |= SC_OP_PROTECT_ENABLE;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002411 else
Sujith672840a2008-08-11 14:05:08 +05302412 sc->sc_flags &= ~SC_OP_PROTECT_ENABLE;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002413 }
2414
Sujith99405f92008-11-24 12:08:35 +05302415 if (changed & BSS_CHANGED_HT)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002416 ath9k_ht_conf(sc, bss_conf);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002417
2418 if (changed & BSS_CHANGED_ASSOC) {
Sujith04bd4632008-11-28 22:18:05 +05302419 DPRINTF(sc, ATH_DBG_CONFIG, "BSS Changed ASSOC %d\n",
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002420 bss_conf->assoc);
Sujith5640b082008-10-29 10:16:06 +05302421 ath9k_bss_assoc_info(sc, vif, bss_conf);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002422 }
2423}
2424
2425static u64 ath9k_get_tsf(struct ieee80211_hw *hw)
2426{
2427 u64 tsf;
2428 struct ath_softc *sc = hw->priv;
2429 struct ath_hal *ah = sc->sc_ah;
2430
2431 tsf = ath9k_hw_gettsf64(ah);
2432
2433 return tsf;
2434}
2435
2436static void ath9k_reset_tsf(struct ieee80211_hw *hw)
2437{
2438 struct ath_softc *sc = hw->priv;
2439 struct ath_hal *ah = sc->sc_ah;
2440
2441 ath9k_hw_reset_tsf(ah);
2442}
2443
2444static int ath9k_ampdu_action(struct ieee80211_hw *hw,
2445 enum ieee80211_ampdu_mlme_action action,
Johannes Berg17741cd2008-09-11 00:02:02 +02002446 struct ieee80211_sta *sta,
2447 u16 tid, u16 *ssn)
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002448{
2449 struct ath_softc *sc = hw->priv;
2450 int ret = 0;
2451
2452 switch (action) {
2453 case IEEE80211_AMPDU_RX_START:
Sujithdca3edb2008-10-29 10:19:01 +05302454 if (!(sc->sc_flags & SC_OP_RXAGGR))
2455 ret = -ENOTSUPP;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002456 break;
2457 case IEEE80211_AMPDU_RX_STOP:
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002458 break;
2459 case IEEE80211_AMPDU_TX_START:
Sujithb5aa9bf2008-10-29 10:13:31 +05302460 ret = ath_tx_aggr_start(sc, sta, tid, ssn);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002461 if (ret < 0)
2462 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd4632008-11-28 22:18:05 +05302463 "Unable to start TX aggregation\n");
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002464 else
Johannes Berg17741cd2008-09-11 00:02:02 +02002465 ieee80211_start_tx_ba_cb_irqsafe(hw, sta->addr, tid);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002466 break;
2467 case IEEE80211_AMPDU_TX_STOP:
Sujithb5aa9bf2008-10-29 10:13:31 +05302468 ret = ath_tx_aggr_stop(sc, sta, tid);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002469 if (ret < 0)
2470 DPRINTF(sc, ATH_DBG_FATAL,
Sujith04bd4632008-11-28 22:18:05 +05302471 "Unable to stop TX aggregation\n");
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002472
Johannes Berg17741cd2008-09-11 00:02:02 +02002473 ieee80211_stop_tx_ba_cb_irqsafe(hw, sta->addr, tid);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002474 break;
Sujith8469cde2008-10-29 10:19:28 +05302475 case IEEE80211_AMPDU_TX_RESUME:
2476 ath_tx_aggr_resume(sc, sta, tid);
2477 break;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002478 default:
Sujith04bd4632008-11-28 22:18:05 +05302479 DPRINTF(sc, ATH_DBG_FATAL, "Unknown AMPDU action\n");
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002480 }
2481
2482 return ret;
2483}
2484
Johannes Berg4233df62008-10-13 13:35:05 +02002485static int ath9k_no_fragmentation(struct ieee80211_hw *hw, u32 value)
2486{
2487 return -EOPNOTSUPP;
2488}
2489
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002490static struct ieee80211_ops ath9k_ops = {
2491 .tx = ath9k_tx,
2492 .start = ath9k_start,
2493 .stop = ath9k_stop,
2494 .add_interface = ath9k_add_interface,
2495 .remove_interface = ath9k_remove_interface,
2496 .config = ath9k_config,
2497 .config_interface = ath9k_config_interface,
2498 .configure_filter = ath9k_configure_filter,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002499 .sta_notify = ath9k_sta_notify,
2500 .conf_tx = ath9k_conf_tx,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002501 .bss_info_changed = ath9k_bss_info_changed,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002502 .set_key = ath9k_set_key,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002503 .get_tsf = ath9k_get_tsf,
2504 .reset_tsf = ath9k_reset_tsf,
Johannes Berg4233df62008-10-13 13:35:05 +02002505 .ampdu_action = ath9k_ampdu_action,
2506 .set_frag_threshold = ath9k_no_fragmentation,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002507};
2508
Benoit PAPILLAULT392dff82008-11-06 22:26:49 +01002509static struct {
2510 u32 version;
2511 const char * name;
2512} ath_mac_bb_names[] = {
2513 { AR_SREV_VERSION_5416_PCI, "5416" },
2514 { AR_SREV_VERSION_5416_PCIE, "5418" },
2515 { AR_SREV_VERSION_9100, "9100" },
2516 { AR_SREV_VERSION_9160, "9160" },
2517 { AR_SREV_VERSION_9280, "9280" },
2518 { AR_SREV_VERSION_9285, "9285" }
2519};
2520
2521static struct {
2522 u16 version;
2523 const char * name;
2524} ath_rf_names[] = {
2525 { 0, "5133" },
2526 { AR_RAD5133_SREV_MAJOR, "5133" },
2527 { AR_RAD5122_SREV_MAJOR, "5122" },
2528 { AR_RAD2133_SREV_MAJOR, "2133" },
2529 { AR_RAD2122_SREV_MAJOR, "2122" }
2530};
2531
2532/*
2533 * Return the MAC/BB name. "????" is returned if the MAC/BB is unknown.
2534 */
Benoit PAPILLAULT392dff82008-11-06 22:26:49 +01002535static const char *
2536ath_mac_bb_name(u32 mac_bb_version)
2537{
2538 int i;
2539
2540 for (i=0; i<ARRAY_SIZE(ath_mac_bb_names); i++) {
2541 if (ath_mac_bb_names[i].version == mac_bb_version) {
2542 return ath_mac_bb_names[i].name;
2543 }
2544 }
2545
2546 return "????";
2547}
2548
2549/*
2550 * Return the RF name. "????" is returned if the RF is unknown.
2551 */
Benoit PAPILLAULT392dff82008-11-06 22:26:49 +01002552static const char *
2553ath_rf_name(u16 rf_version)
2554{
2555 int i;
2556
2557 for (i=0; i<ARRAY_SIZE(ath_rf_names); i++) {
2558 if (ath_rf_names[i].version == rf_version) {
2559 return ath_rf_names[i].name;
2560 }
2561 }
2562
2563 return "????";
2564}
2565
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002566static int ath_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
2567{
2568 void __iomem *mem;
2569 struct ath_softc *sc;
2570 struct ieee80211_hw *hw;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002571 u8 csz;
2572 u32 val;
2573 int ret = 0;
Benoit PAPILLAULT392dff82008-11-06 22:26:49 +01002574 struct ath_hal *ah;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002575
2576 if (pci_enable_device(pdev))
2577 return -EIO;
2578
Luis R. Rodriguez97b777d2008-11-13 19:11:57 -08002579 ret = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
2580
2581 if (ret) {
Luis R. Rodriguez1d450cf2008-11-13 19:11:56 -08002582 printk(KERN_ERR "ath9k: 32-bit DMA not available\n");
Luis R. Rodriguez97b777d2008-11-13 19:11:57 -08002583 goto bad;
2584 }
2585
2586 ret = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
2587
2588 if (ret) {
2589 printk(KERN_ERR "ath9k: 32-bit DMA consistent "
Sujith04bd4632008-11-28 22:18:05 +05302590 "DMA enable failed\n");
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002591 goto bad;
2592 }
2593
2594 /*
2595 * Cache line size is used to size and align various
2596 * structures used to communicate with the hardware.
2597 */
2598 pci_read_config_byte(pdev, PCI_CACHE_LINE_SIZE, &csz);
2599 if (csz == 0) {
2600 /*
2601 * Linux 2.4.18 (at least) writes the cache line size
2602 * register as a 16-bit wide register which is wrong.
2603 * We must have this setup properly for rx buffer
2604 * DMA to work so force a reasonable value here if it
2605 * comes up zero.
2606 */
2607 csz = L1_CACHE_BYTES / sizeof(u32);
2608 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, csz);
2609 }
2610 /*
2611 * The default setting of latency timer yields poor results,
2612 * set it to the value used by other systems. It may be worth
2613 * tweaking this setting more.
2614 */
2615 pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0xa8);
2616
2617 pci_set_master(pdev);
2618
2619 /*
2620 * Disable the RETRY_TIMEOUT register (0x41) to keep
2621 * PCI Tx retries from interfering with C3 CPU state.
2622 */
2623 pci_read_config_dword(pdev, 0x40, &val);
2624 if ((val & 0x0000ff00) != 0)
2625 pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
2626
2627 ret = pci_request_region(pdev, 0, "ath9k");
2628 if (ret) {
2629 dev_err(&pdev->dev, "PCI memory region reserve error\n");
2630 ret = -ENODEV;
2631 goto bad;
2632 }
2633
2634 mem = pci_iomap(pdev, 0, 0);
2635 if (!mem) {
2636 printk(KERN_ERR "PCI memory map error\n") ;
2637 ret = -EIO;
2638 goto bad1;
2639 }
2640
2641 hw = ieee80211_alloc_hw(sizeof(struct ath_softc), &ath9k_ops);
2642 if (hw == NULL) {
2643 printk(KERN_ERR "ath_pci: no memory for ieee80211_hw\n");
2644 goto bad2;
2645 }
2646
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002647 SET_IEEE80211_DEV(hw, &pdev->dev);
2648 pci_set_drvdata(pdev, hw);
2649
2650 sc = hw->priv;
2651 sc->hw = hw;
2652 sc->pdev = pdev;
2653 sc->mem = mem;
2654
2655 if (ath_attach(id->device, sc) != 0) {
2656 ret = -ENODEV;
2657 goto bad3;
2658 }
2659
2660 /* setup interrupt service routine */
2661
2662 if (request_irq(pdev->irq, ath_isr, IRQF_SHARED, "ath", sc)) {
2663 printk(KERN_ERR "%s: request_irq failed\n",
2664 wiphy_name(hw->wiphy));
2665 ret = -EIO;
2666 goto bad4;
2667 }
2668
Benoit PAPILLAULT392dff82008-11-06 22:26:49 +01002669 ah = sc->sc_ah;
2670 printk(KERN_INFO
2671 "%s: Atheros AR%s MAC/BB Rev:%x "
2672 "AR%s RF Rev:%x: mem=0x%lx, irq=%d\n",
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002673 wiphy_name(hw->wiphy),
Benoit PAPILLAULT392dff82008-11-06 22:26:49 +01002674 ath_mac_bb_name(ah->ah_macVersion),
2675 ah->ah_macRev,
2676 ath_rf_name((ah->ah_analog5GhzRev & AR_RADIO_SREV_MAJOR)),
2677 ah->ah_phyRev,
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002678 (unsigned long)mem, pdev->irq);
2679
2680 return 0;
2681bad4:
2682 ath_detach(sc);
2683bad3:
2684 ieee80211_free_hw(hw);
2685bad2:
2686 pci_iounmap(pdev, mem);
2687bad1:
2688 pci_release_region(pdev, 0);
2689bad:
2690 pci_disable_device(pdev);
2691 return ret;
2692}
2693
2694static void ath_pci_remove(struct pci_dev *pdev)
2695{
2696 struct ieee80211_hw *hw = pci_get_drvdata(pdev);
2697 struct ath_softc *sc = hw->priv;
2698
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002699 ath_detach(sc);
Sujith9c84b792008-10-29 10:17:13 +05302700 if (pdev->irq)
2701 free_irq(pdev->irq, sc);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002702 pci_iounmap(pdev, sc->mem);
2703 pci_release_region(pdev, 0);
2704 pci_disable_device(pdev);
2705 ieee80211_free_hw(hw);
2706}
2707
2708#ifdef CONFIG_PM
2709
2710static int ath_pci_suspend(struct pci_dev *pdev, pm_message_t state)
2711{
Vasanthakumar Thiagarajanc83be682008-08-25 20:47:29 +05302712 struct ieee80211_hw *hw = pci_get_drvdata(pdev);
2713 struct ath_softc *sc = hw->priv;
2714
2715 ath9k_hw_set_gpio(sc->sc_ah, ATH_LED_PIN, 1);
Vasanthakumar Thiagarajan500c0642008-09-10 18:50:17 +05302716
Senthil Balasubramaniane97275c2008-11-13 18:00:02 +05302717#if defined(CONFIG_RFKILL) || defined(CONFIG_RFKILL_MODULE)
Vasanthakumar Thiagarajan500c0642008-09-10 18:50:17 +05302718 if (sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
2719 cancel_delayed_work_sync(&sc->rf_kill.rfkill_poll);
2720#endif
2721
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002722 pci_save_state(pdev);
2723 pci_disable_device(pdev);
2724 pci_set_power_state(pdev, 3);
2725
2726 return 0;
2727}
2728
2729static int ath_pci_resume(struct pci_dev *pdev)
2730{
Vasanthakumar Thiagarajanc83be682008-08-25 20:47:29 +05302731 struct ieee80211_hw *hw = pci_get_drvdata(pdev);
2732 struct ath_softc *sc = hw->priv;
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002733 u32 val;
2734 int err;
2735
2736 err = pci_enable_device(pdev);
2737 if (err)
2738 return err;
2739 pci_restore_state(pdev);
2740 /*
2741 * Suspend/Resume resets the PCI configuration space, so we have to
2742 * re-disable the RETRY_TIMEOUT register (0x41) to keep
2743 * PCI Tx retries from interfering with C3 CPU state
2744 */
2745 pci_read_config_dword(pdev, 0x40, &val);
2746 if ((val & 0x0000ff00) != 0)
2747 pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
2748
Vasanthakumar Thiagarajanc83be682008-08-25 20:47:29 +05302749 /* Enable LED */
2750 ath9k_hw_cfg_output(sc->sc_ah, ATH_LED_PIN,
2751 AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
2752 ath9k_hw_set_gpio(sc->sc_ah, ATH_LED_PIN, 1);
2753
Senthil Balasubramaniane97275c2008-11-13 18:00:02 +05302754#if defined(CONFIG_RFKILL) || defined(CONFIG_RFKILL_MODULE)
Vasanthakumar Thiagarajan500c0642008-09-10 18:50:17 +05302755 /*
2756 * check the h/w rfkill state on resume
2757 * and start the rfkill poll timer
2758 */
2759 if (sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
2760 queue_delayed_work(sc->hw->workqueue,
2761 &sc->rf_kill.rfkill_poll, 0);
2762#endif
2763
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002764 return 0;
2765}
2766
2767#endif /* CONFIG_PM */
2768
2769MODULE_DEVICE_TABLE(pci, ath_pci_id_table);
2770
2771static struct pci_driver ath_pci_driver = {
2772 .name = "ath9k",
2773 .id_table = ath_pci_id_table,
2774 .probe = ath_pci_probe,
2775 .remove = ath_pci_remove,
2776#ifdef CONFIG_PM
2777 .suspend = ath_pci_suspend,
2778 .resume = ath_pci_resume,
2779#endif /* CONFIG_PM */
2780};
2781
2782static int __init init_ath_pci(void)
2783{
2784 printk(KERN_INFO "%s: %s\n", dev_info, ATH_PCI_VERSION);
2785
2786 if (pci_register_driver(&ath_pci_driver) < 0) {
2787 printk(KERN_ERR
2788 "ath_pci: No devices found, driver not installed.\n");
2789 pci_unregister_driver(&ath_pci_driver);
2790 return -ENODEV;
2791 }
2792
2793 return 0;
2794}
2795module_init(init_ath_pci);
2796
2797static void __exit exit_ath_pci(void)
2798{
2799 pci_unregister_driver(&ath_pci_driver);
Sujith04bd4632008-11-28 22:18:05 +05302800 printk(KERN_INFO "%s: Driver unloaded\n", dev_info);
Luis R. Rodriguezf078f202008-08-04 00:16:41 -07002801}
2802module_exit(exit_ath_pci);