6c872e704b93bdc1f6f21d30ab370f7eb20e77e3
[deliverable/linux.git] / drivers / net / wireless / ath9k / core.c
1 /*
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
17 #include "core.h"
18 #include "regd.h"
19
20 static u32 ath_chainmask_sel_up_rssi_thres =
21 ATH_CHAINMASK_SEL_UP_RSSI_THRES;
22 static u32 ath_chainmask_sel_down_rssi_thres =
23 ATH_CHAINMASK_SEL_DOWN_RSSI_THRES;
24 static u32 ath_chainmask_sel_period =
25 ATH_CHAINMASK_SEL_TIMEOUT;
26
27 /* return bus cachesize in 4B word units */
28
29 static void bus_read_cachesize(struct ath_softc *sc, int *csz)
30 {
31 u8 u8tmp;
32
33 pci_read_config_byte(sc->pdev, PCI_CACHE_LINE_SIZE, (u8 *)&u8tmp);
34 *csz = (int)u8tmp;
35
36 /*
37 * This check was put in to avoid "unplesant" consequences if
38 * the bootrom has not fully initialized all PCI devices.
39 * Sometimes the cache line size register is not set
40 */
41
42 if (*csz == 0)
43 *csz = DEFAULT_CACHELINE >> 2; /* Use the default size */
44 }
45
46 static u8 parse_mpdudensity(u8 mpdudensity)
47 {
48 /*
49 * 802.11n D2.0 defined values for "Minimum MPDU Start Spacing":
50 * 0 for no restriction
51 * 1 for 1/4 us
52 * 2 for 1/2 us
53 * 3 for 1 us
54 * 4 for 2 us
55 * 5 for 4 us
56 * 6 for 8 us
57 * 7 for 16 us
58 */
59 switch (mpdudensity) {
60 case 0:
61 return 0;
62 case 1:
63 case 2:
64 case 3:
65 /* Our lower layer calculations limit our precision to
66 1 microsecond */
67 return 1;
68 case 4:
69 return 2;
70 case 5:
71 return 4;
72 case 6:
73 return 8;
74 case 7:
75 return 16;
76 default:
77 return 0;
78 }
79 }
80
81 /*
82 * Set current operating mode
83 *
84 * This function initializes and fills the rate table in the ATH object based
85 * on the operating mode.
86 */
87 static void ath_setcurmode(struct ath_softc *sc, enum wireless_mode mode)
88 {
89 const struct ath9k_rate_table *rt;
90 int i;
91
92 rt = ath9k_hw_getratetable(sc->sc_ah, mode);
93 BUG_ON(!rt);
94
95 memset(sc->sc_hwmap, 0, sizeof(sc->sc_hwmap));
96 for (i = 0; i < 256; i++) {
97 u8 ix = rt->rateCodeToIndex[i];
98
99 if (ix == 0xff)
100 continue;
101
102 sc->sc_hwmap[i].ieeerate =
103 rt->info[ix].dot11Rate & IEEE80211_RATE_VAL;
104 sc->sc_hwmap[i].rateKbps = rt->info[ix].rateKbps;
105
106 if (rt->info[ix].shortPreamble ||
107 rt->info[ix].phy == PHY_OFDM) {
108 /* XXX: Handle this */
109 }
110
111 /* NB: this uses the last entry if the rate isn't found */
112 /* XXX beware of overlow */
113 }
114 sc->sc_currates = rt;
115 sc->sc_curmode = mode;
116 /*
117 * All protection frames are transmited at 2Mb/s for
118 * 11g, otherwise at 1Mb/s.
119 * XXX select protection rate index from rate table.
120 */
121 sc->sc_protrix = (mode == ATH9K_MODE_11G ? 1 : 0);
122 }
123
124 /*
125 * Set up rate table (legacy rates)
126 */
127 static void ath_setup_rates(struct ath_softc *sc, enum ieee80211_band band)
128 {
129 struct ath_hal *ah = sc->sc_ah;
130 const struct ath9k_rate_table *rt = NULL;
131 struct ieee80211_supported_band *sband;
132 struct ieee80211_rate *rate;
133 int i, maxrates;
134
135 switch (band) {
136 case IEEE80211_BAND_2GHZ:
137 rt = ath9k_hw_getratetable(ah, ATH9K_MODE_11G);
138 break;
139 case IEEE80211_BAND_5GHZ:
140 rt = ath9k_hw_getratetable(ah, ATH9K_MODE_11A);
141 break;
142 default:
143 break;
144 }
145
146 if (rt == NULL)
147 return;
148
149 sband = &sc->sbands[band];
150 rate = sc->rates[band];
151
152 if (rt->rateCount > ATH_RATE_MAX)
153 maxrates = ATH_RATE_MAX;
154 else
155 maxrates = rt->rateCount;
156
157 for (i = 0; i < maxrates; i++) {
158 rate[i].bitrate = rt->info[i].rateKbps / 100;
159 rate[i].hw_value = rt->info[i].rateCode;
160 sband->n_bitrates++;
161 DPRINTF(sc, ATH_DBG_CONFIG,
162 "%s: Rate: %2dMbps, ratecode: %2d\n",
163 __func__,
164 rate[i].bitrate / 10,
165 rate[i].hw_value);
166 }
167 }
168
169 /*
170 * Set up channel list
171 */
172 static int ath_setup_channels(struct ath_softc *sc)
173 {
174 struct ath_hal *ah = sc->sc_ah;
175 int nchan, i, a = 0, b = 0;
176 u8 regclassids[ATH_REGCLASSIDS_MAX];
177 u32 nregclass = 0;
178 struct ieee80211_supported_band *band_2ghz;
179 struct ieee80211_supported_band *band_5ghz;
180 struct ieee80211_channel *chan_2ghz;
181 struct ieee80211_channel *chan_5ghz;
182 struct ath9k_channel *c;
183
184 /* Fill in ah->ah_channels */
185 if (!ath9k_regd_init_channels(ah, ATH_CHAN_MAX, (u32 *)&nchan,
186 regclassids, ATH_REGCLASSIDS_MAX,
187 &nregclass, CTRY_DEFAULT, false, 1)) {
188 u32 rd = ah->ah_currentRD;
189 DPRINTF(sc, ATH_DBG_FATAL,
190 "%s: unable to collect channel list; "
191 "regdomain likely %u country code %u\n",
192 __func__, rd, CTRY_DEFAULT);
193 return -EINVAL;
194 }
195
196 band_2ghz = &sc->sbands[IEEE80211_BAND_2GHZ];
197 band_5ghz = &sc->sbands[IEEE80211_BAND_5GHZ];
198 chan_2ghz = sc->channels[IEEE80211_BAND_2GHZ];
199 chan_5ghz = sc->channels[IEEE80211_BAND_5GHZ];
200
201 for (i = 0; i < nchan; i++) {
202 c = &ah->ah_channels[i];
203 if (IS_CHAN_2GHZ(c)) {
204 chan_2ghz[a].band = IEEE80211_BAND_2GHZ;
205 chan_2ghz[a].center_freq = c->channel;
206 chan_2ghz[a].max_power = c->maxTxPower;
207
208 if (c->privFlags & CHANNEL_DISALLOW_ADHOC)
209 chan_2ghz[a].flags |= IEEE80211_CHAN_NO_IBSS;
210 if (c->channelFlags & CHANNEL_PASSIVE)
211 chan_2ghz[a].flags |= IEEE80211_CHAN_PASSIVE_SCAN;
212
213 band_2ghz->n_channels = ++a;
214
215 DPRINTF(sc, ATH_DBG_CONFIG,
216 "%s: 2MHz channel: %d, "
217 "channelFlags: 0x%x\n",
218 __func__, c->channel, c->channelFlags);
219 } else if (IS_CHAN_5GHZ(c)) {
220 chan_5ghz[b].band = IEEE80211_BAND_5GHZ;
221 chan_5ghz[b].center_freq = c->channel;
222 chan_5ghz[b].max_power = c->maxTxPower;
223
224 if (c->privFlags & CHANNEL_DISALLOW_ADHOC)
225 chan_5ghz[b].flags |= IEEE80211_CHAN_NO_IBSS;
226 if (c->channelFlags & CHANNEL_PASSIVE)
227 chan_5ghz[b].flags |= IEEE80211_CHAN_PASSIVE_SCAN;
228
229 band_5ghz->n_channels = ++b;
230
231 DPRINTF(sc, ATH_DBG_CONFIG,
232 "%s: 5MHz channel: %d, "
233 "channelFlags: 0x%x\n",
234 __func__, c->channel, c->channelFlags);
235 }
236 }
237
238 return 0;
239 }
240
241 /*
242 * Determine mode from channel flags
243 *
244 * This routine will provide the enumerated WIRELESSS_MODE value based
245 * on the settings of the channel flags. If no valid set of flags
246 * exist, the lowest mode (11b) is selected.
247 */
248
249 static enum wireless_mode ath_chan2mode(struct ath9k_channel *chan)
250 {
251 if (chan->chanmode == CHANNEL_A)
252 return ATH9K_MODE_11A;
253 else if (chan->chanmode == CHANNEL_G)
254 return ATH9K_MODE_11G;
255 else if (chan->chanmode == CHANNEL_B)
256 return ATH9K_MODE_11B;
257 else if (chan->chanmode == CHANNEL_A_HT20)
258 return ATH9K_MODE_11NA_HT20;
259 else if (chan->chanmode == CHANNEL_G_HT20)
260 return ATH9K_MODE_11NG_HT20;
261 else if (chan->chanmode == CHANNEL_A_HT40PLUS)
262 return ATH9K_MODE_11NA_HT40PLUS;
263 else if (chan->chanmode == CHANNEL_A_HT40MINUS)
264 return ATH9K_MODE_11NA_HT40MINUS;
265 else if (chan->chanmode == CHANNEL_G_HT40PLUS)
266 return ATH9K_MODE_11NG_HT40PLUS;
267 else if (chan->chanmode == CHANNEL_G_HT40MINUS)
268 return ATH9K_MODE_11NG_HT40MINUS;
269
270 WARN_ON(1); /* should not get here */
271
272 return ATH9K_MODE_11B;
273 }
274
275 /*
276 * Set the current channel
277 *
278 * Set/change channels. If the channel is really being changed, it's done
279 * by reseting the chip. To accomplish this we must first cleanup any pending
280 * DMA, then restart stuff after a la ath_init.
281 */
282 int ath_set_channel(struct ath_softc *sc, struct ath9k_channel *hchan)
283 {
284 struct ath_hal *ah = sc->sc_ah;
285 bool fastcc = true, stopped;
286
287 if (sc->sc_flags & SC_OP_INVALID) /* the device is invalid or removed */
288 return -EIO;
289
290 DPRINTF(sc, ATH_DBG_CONFIG,
291 "%s: %u (%u MHz) -> %u (%u MHz), cflags:%x\n",
292 __func__,
293 ath9k_hw_mhz2ieee(ah, sc->sc_ah->ah_curchan->channel,
294 sc->sc_ah->ah_curchan->channelFlags),
295 sc->sc_ah->ah_curchan->channel,
296 ath9k_hw_mhz2ieee(ah, hchan->channel, hchan->channelFlags),
297 hchan->channel, hchan->channelFlags);
298
299 if (hchan->channel != sc->sc_ah->ah_curchan->channel ||
300 hchan->channelFlags != sc->sc_ah->ah_curchan->channelFlags ||
301 (sc->sc_flags & SC_OP_CHAINMASK_UPDATE) ||
302 (sc->sc_flags & SC_OP_FULL_RESET)) {
303 int status;
304 /*
305 * This is only performed if the channel settings have
306 * actually changed.
307 *
308 * To switch channels clear any pending DMA operations;
309 * wait long enough for the RX fifo to drain, reset the
310 * hardware at the new frequency, and then re-enable
311 * the relevant bits of the h/w.
312 */
313 ath9k_hw_set_interrupts(ah, 0); /* disable interrupts */
314 ath_draintxq(sc, false); /* clear pending tx frames */
315 stopped = ath_stoprecv(sc); /* turn off frame recv */
316
317 /* XXX: do not flush receive queue here. We don't want
318 * to flush data frames already in queue because of
319 * changing channel. */
320
321 if (!stopped || (sc->sc_flags & SC_OP_FULL_RESET))
322 fastcc = false;
323
324 spin_lock_bh(&sc->sc_resetlock);
325 if (!ath9k_hw_reset(ah, hchan,
326 sc->sc_ht_info.tx_chan_width,
327 sc->sc_tx_chainmask,
328 sc->sc_rx_chainmask,
329 sc->sc_ht_extprotspacing,
330 fastcc, &status)) {
331 DPRINTF(sc, ATH_DBG_FATAL,
332 "%s: unable to reset channel %u (%uMhz) "
333 "flags 0x%x hal status %u\n", __func__,
334 ath9k_hw_mhz2ieee(ah, hchan->channel,
335 hchan->channelFlags),
336 hchan->channel, hchan->channelFlags, status);
337 spin_unlock_bh(&sc->sc_resetlock);
338 return -EIO;
339 }
340 spin_unlock_bh(&sc->sc_resetlock);
341
342 sc->sc_flags &= ~SC_OP_CHAINMASK_UPDATE;
343 sc->sc_flags &= ~SC_OP_FULL_RESET;
344
345 /* Re-enable rx framework */
346 if (ath_startrecv(sc) != 0) {
347 DPRINTF(sc, ATH_DBG_FATAL,
348 "%s: unable to restart recv logic\n", __func__);
349 return -EIO;
350 }
351 /*
352 * Change channels and update the h/w rate map
353 * if we're switching; e.g. 11a to 11b/g.
354 */
355 ath_setcurmode(sc, ath_chan2mode(hchan));
356
357 ath_update_txpow(sc); /* update tx power state */
358 /*
359 * Re-enable interrupts.
360 */
361 ath9k_hw_set_interrupts(ah, sc->sc_imask);
362 }
363 return 0;
364 }
365
366 /**********************/
367 /* Chainmask Handling */
368 /**********************/
369
370 static void ath_chainmask_sel_timertimeout(unsigned long data)
371 {
372 struct ath_chainmask_sel *cm = (struct ath_chainmask_sel *)data;
373 cm->switch_allowed = 1;
374 }
375
376 /* Start chainmask select timer */
377 static void ath_chainmask_sel_timerstart(struct ath_chainmask_sel *cm)
378 {
379 cm->switch_allowed = 0;
380 mod_timer(&cm->timer, ath_chainmask_sel_period);
381 }
382
383 /* Stop chainmask select timer */
384 static void ath_chainmask_sel_timerstop(struct ath_chainmask_sel *cm)
385 {
386 cm->switch_allowed = 0;
387 del_timer_sync(&cm->timer);
388 }
389
390 static void ath_chainmask_sel_init(struct ath_softc *sc, struct ath_node *an)
391 {
392 struct ath_chainmask_sel *cm = &an->an_chainmask_sel;
393
394 memset(cm, 0, sizeof(struct ath_chainmask_sel));
395
396 cm->cur_tx_mask = sc->sc_tx_chainmask;
397 cm->cur_rx_mask = sc->sc_rx_chainmask;
398 cm->tx_avgrssi = ATH_RSSI_DUMMY_MARKER;
399 setup_timer(&cm->timer,
400 ath_chainmask_sel_timertimeout, (unsigned long) cm);
401 }
402
403 int ath_chainmask_sel_logic(struct ath_softc *sc, struct ath_node *an)
404 {
405 struct ath_chainmask_sel *cm = &an->an_chainmask_sel;
406
407 /*
408 * Disable auto-swtiching in one of the following if conditions.
409 * sc_chainmask_auto_sel is used for internal global auto-switching
410 * enabled/disabled setting
411 */
412 if (sc->sc_ah->ah_caps.tx_chainmask != ATH_CHAINMASK_SEL_3X3) {
413 cm->cur_tx_mask = sc->sc_tx_chainmask;
414 return cm->cur_tx_mask;
415 }
416
417 if (cm->tx_avgrssi == ATH_RSSI_DUMMY_MARKER)
418 return cm->cur_tx_mask;
419
420 if (cm->switch_allowed) {
421 /* Switch down from tx 3 to tx 2. */
422 if (cm->cur_tx_mask == ATH_CHAINMASK_SEL_3X3 &&
423 ATH_RSSI_OUT(cm->tx_avgrssi) >=
424 ath_chainmask_sel_down_rssi_thres) {
425 cm->cur_tx_mask = sc->sc_tx_chainmask;
426
427 /* Don't let another switch happen until
428 * this timer expires */
429 ath_chainmask_sel_timerstart(cm);
430 }
431 /* Switch up from tx 2 to 3. */
432 else if (cm->cur_tx_mask == sc->sc_tx_chainmask &&
433 ATH_RSSI_OUT(cm->tx_avgrssi) <=
434 ath_chainmask_sel_up_rssi_thres) {
435 cm->cur_tx_mask = ATH_CHAINMASK_SEL_3X3;
436
437 /* Don't let another switch happen
438 * until this timer expires */
439 ath_chainmask_sel_timerstart(cm);
440 }
441 }
442
443 return cm->cur_tx_mask;
444 }
445
446 /*
447 * Update tx/rx chainmask. For legacy association,
448 * hard code chainmask to 1x1, for 11n association, use
449 * the chainmask configuration.
450 */
451
452 void ath_update_chainmask(struct ath_softc *sc, int is_ht)
453 {
454 sc->sc_flags |= SC_OP_CHAINMASK_UPDATE;
455 if (is_ht) {
456 sc->sc_tx_chainmask = sc->sc_ah->ah_caps.tx_chainmask;
457 sc->sc_rx_chainmask = sc->sc_ah->ah_caps.rx_chainmask;
458 } else {
459 sc->sc_tx_chainmask = 1;
460 sc->sc_rx_chainmask = 1;
461 }
462
463 DPRINTF(sc, ATH_DBG_CONFIG, "%s: tx chmask: %d, rx chmask: %d\n",
464 __func__, sc->sc_tx_chainmask, sc->sc_rx_chainmask);
465 }
466
467 /*******/
468 /* ANI */
469 /*******/
470
471 /*
472 * This routine performs the periodic noise floor calibration function
473 * that is used to adjust and optimize the chip performance. This
474 * takes environmental changes (location, temperature) into account.
475 * When the task is complete, it reschedules itself depending on the
476 * appropriate interval that was calculated.
477 */
478
479 static void ath_ani_calibrate(unsigned long data)
480 {
481 struct ath_softc *sc;
482 struct ath_hal *ah;
483 bool longcal = false;
484 bool shortcal = false;
485 bool aniflag = false;
486 unsigned int timestamp = jiffies_to_msecs(jiffies);
487 u32 cal_interval;
488
489 sc = (struct ath_softc *)data;
490 ah = sc->sc_ah;
491
492 /*
493 * don't calibrate when we're scanning.
494 * we are most likely not on our home channel.
495 */
496 if (sc->rx_filter & FIF_BCN_PRBRESP_PROMISC)
497 return;
498
499 /* Long calibration runs independently of short calibration. */
500 if ((timestamp - sc->sc_ani.sc_longcal_timer) >= ATH_LONG_CALINTERVAL) {
501 longcal = true;
502 DPRINTF(sc, ATH_DBG_ANI, "%s: longcal @%lu\n",
503 __func__, jiffies);
504 sc->sc_ani.sc_longcal_timer = timestamp;
505 }
506
507 /* Short calibration applies only while sc_caldone is false */
508 if (!sc->sc_ani.sc_caldone) {
509 if ((timestamp - sc->sc_ani.sc_shortcal_timer) >=
510 ATH_SHORT_CALINTERVAL) {
511 shortcal = true;
512 DPRINTF(sc, ATH_DBG_ANI, "%s: shortcal @%lu\n",
513 __func__, jiffies);
514 sc->sc_ani.sc_shortcal_timer = timestamp;
515 sc->sc_ani.sc_resetcal_timer = timestamp;
516 }
517 } else {
518 if ((timestamp - sc->sc_ani.sc_resetcal_timer) >=
519 ATH_RESTART_CALINTERVAL) {
520 ath9k_hw_reset_calvalid(ah, ah->ah_curchan,
521 &sc->sc_ani.sc_caldone);
522 if (sc->sc_ani.sc_caldone)
523 sc->sc_ani.sc_resetcal_timer = timestamp;
524 }
525 }
526
527 /* Verify whether we must check ANI */
528 if ((timestamp - sc->sc_ani.sc_checkani_timer) >=
529 ATH_ANI_POLLINTERVAL) {
530 aniflag = true;
531 sc->sc_ani.sc_checkani_timer = timestamp;
532 }
533
534 /* Skip all processing if there's nothing to do. */
535 if (longcal || shortcal || aniflag) {
536 /* Call ANI routine if necessary */
537 if (aniflag)
538 ath9k_hw_ani_monitor(ah, &sc->sc_halstats,
539 ah->ah_curchan);
540
541 /* Perform calibration if necessary */
542 if (longcal || shortcal) {
543 bool iscaldone = false;
544
545 if (ath9k_hw_calibrate(ah, ah->ah_curchan,
546 sc->sc_rx_chainmask, longcal,
547 &iscaldone)) {
548 if (longcal)
549 sc->sc_ani.sc_noise_floor =
550 ath9k_hw_getchan_noise(ah,
551 ah->ah_curchan);
552
553 DPRINTF(sc, ATH_DBG_ANI,
554 "%s: calibrate chan %u/%x nf: %d\n",
555 __func__,
556 ah->ah_curchan->channel,
557 ah->ah_curchan->channelFlags,
558 sc->sc_ani.sc_noise_floor);
559 } else {
560 DPRINTF(sc, ATH_DBG_ANY,
561 "%s: calibrate chan %u/%x failed\n",
562 __func__,
563 ah->ah_curchan->channel,
564 ah->ah_curchan->channelFlags);
565 }
566 sc->sc_ani.sc_caldone = iscaldone;
567 }
568 }
569
570 /*
571 * Set timer interval based on previous results.
572 * The interval must be the shortest necessary to satisfy ANI,
573 * short calibration and long calibration.
574 */
575
576 cal_interval = ATH_ANI_POLLINTERVAL;
577 if (!sc->sc_ani.sc_caldone)
578 cal_interval = min(cal_interval, (u32)ATH_SHORT_CALINTERVAL);
579
580 mod_timer(&sc->sc_ani.timer, jiffies + msecs_to_jiffies(cal_interval));
581 }
582
583 /********/
584 /* Core */
585 /********/
586
587 int ath_open(struct ath_softc *sc, struct ath9k_channel *initial_chan)
588 {
589 struct ath_hal *ah = sc->sc_ah;
590 int status;
591 int error = 0;
592
593 DPRINTF(sc, ATH_DBG_CONFIG, "%s: mode %d\n",
594 __func__, sc->sc_ah->ah_opmode);
595
596 /* Reset SERDES registers */
597 ath9k_hw_configpcipowersave(ah, 0);
598
599 /*
600 * The basic interface to setting the hardware in a good
601 * state is ``reset''. On return the hardware is known to
602 * be powered up and with interrupts disabled. This must
603 * be followed by initialization of the appropriate bits
604 * and then setup of the interrupt mask.
605 */
606
607 spin_lock_bh(&sc->sc_resetlock);
608 if (!ath9k_hw_reset(ah, initial_chan,
609 sc->sc_ht_info.tx_chan_width,
610 sc->sc_tx_chainmask, sc->sc_rx_chainmask,
611 sc->sc_ht_extprotspacing, false, &status)) {
612 DPRINTF(sc, ATH_DBG_FATAL,
613 "%s: unable to reset hardware; hal status %u "
614 "(freq %u flags 0x%x)\n", __func__, status,
615 initial_chan->channel, initial_chan->channelFlags);
616 error = -EIO;
617 spin_unlock_bh(&sc->sc_resetlock);
618 goto done;
619 }
620 spin_unlock_bh(&sc->sc_resetlock);
621
622 /*
623 * This is needed only to setup initial state
624 * but it's best done after a reset.
625 */
626 ath_update_txpow(sc);
627
628 /*
629 * Setup the hardware after reset:
630 * The receive engine is set going.
631 * Frame transmit is handled entirely
632 * in the frame output path; there's nothing to do
633 * here except setup the interrupt mask.
634 */
635 if (ath_startrecv(sc) != 0) {
636 DPRINTF(sc, ATH_DBG_FATAL,
637 "%s: unable to start recv logic\n", __func__);
638 error = -EIO;
639 goto done;
640 }
641
642 /* Setup our intr mask. */
643 sc->sc_imask = ATH9K_INT_RX | ATH9K_INT_TX
644 | ATH9K_INT_RXEOL | ATH9K_INT_RXORN
645 | ATH9K_INT_FATAL | ATH9K_INT_GLOBAL;
646
647 if (ah->ah_caps.hw_caps & ATH9K_HW_CAP_GTT)
648 sc->sc_imask |= ATH9K_INT_GTT;
649
650 if (ah->ah_caps.hw_caps & ATH9K_HW_CAP_HT)
651 sc->sc_imask |= ATH9K_INT_CST;
652
653 /*
654 * Enable MIB interrupts when there are hardware phy counters.
655 * Note we only do this (at the moment) for station mode.
656 */
657 if (ath9k_hw_phycounters(ah) &&
658 ((sc->sc_ah->ah_opmode == ATH9K_M_STA) ||
659 (sc->sc_ah->ah_opmode == ATH9K_M_IBSS)))
660 sc->sc_imask |= ATH9K_INT_MIB;
661 /*
662 * Some hardware processes the TIM IE and fires an
663 * interrupt when the TIM bit is set. For hardware
664 * that does, if not overridden by configuration,
665 * enable the TIM interrupt when operating as station.
666 */
667 if ((ah->ah_caps.hw_caps & ATH9K_HW_CAP_ENHANCEDPM) &&
668 (sc->sc_ah->ah_opmode == ATH9K_M_STA) &&
669 !sc->sc_config.swBeaconProcess)
670 sc->sc_imask |= ATH9K_INT_TIM;
671
672 ath_setcurmode(sc, ath_chan2mode(initial_chan));
673
674 sc->sc_flags &= ~SC_OP_INVALID;
675
676 /* Disable BMISS interrupt when we're not associated */
677 sc->sc_imask &= ~(ATH9K_INT_SWBA | ATH9K_INT_BMISS);
678 ath9k_hw_set_interrupts(sc->sc_ah,sc->sc_imask);
679
680 ieee80211_wake_queues(sc->hw);
681 done:
682 return error;
683 }
684
685 void ath_stop(struct ath_softc *sc)
686 {
687 struct ath_hal *ah = sc->sc_ah;
688
689 DPRINTF(sc, ATH_DBG_CONFIG, "%s: Cleaning up\n", __func__);
690
691 ieee80211_stop_queues(sc->hw);
692
693 /* make sure h/w will not generate any interrupt
694 * before setting the invalid flag. */
695 ath9k_hw_set_interrupts(ah, 0);
696
697 if (!(sc->sc_flags & SC_OP_INVALID)) {
698 ath_draintxq(sc, false);
699 ath_stoprecv(sc);
700 ath9k_hw_phy_disable(ah);
701 } else
702 sc->sc_rxlink = NULL;
703
704 #if defined(CONFIG_RFKILL) || defined(CONFIG_RFKILL_MODULE)
705 if (sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
706 cancel_delayed_work_sync(&sc->rf_kill.rfkill_poll);
707 #endif
708 /* disable HAL and put h/w to sleep */
709 ath9k_hw_disable(sc->sc_ah);
710 ath9k_hw_configpcipowersave(sc->sc_ah, 1);
711
712 sc->sc_flags |= SC_OP_INVALID;
713 }
714
715 int ath_reset(struct ath_softc *sc, bool retry_tx)
716 {
717 struct ath_hal *ah = sc->sc_ah;
718 int status;
719 int error = 0;
720
721 ath9k_hw_set_interrupts(ah, 0);
722 ath_draintxq(sc, retry_tx);
723 ath_stoprecv(sc);
724 ath_flushrecv(sc);
725
726 /* Reset chip */
727 spin_lock_bh(&sc->sc_resetlock);
728 if (!ath9k_hw_reset(ah, sc->sc_ah->ah_curchan,
729 sc->sc_ht_info.tx_chan_width,
730 sc->sc_tx_chainmask, sc->sc_rx_chainmask,
731 sc->sc_ht_extprotspacing, false, &status)) {
732 DPRINTF(sc, ATH_DBG_FATAL,
733 "%s: unable to reset hardware; hal status %u\n",
734 __func__, status);
735 error = -EIO;
736 }
737 spin_unlock_bh(&sc->sc_resetlock);
738
739 if (ath_startrecv(sc) != 0)
740 DPRINTF(sc, ATH_DBG_FATAL,
741 "%s: unable to start recv logic\n", __func__);
742
743 /*
744 * We may be doing a reset in response to a request
745 * that changes the channel so update any state that
746 * might change as a result.
747 */
748 ath_setcurmode(sc, ath_chan2mode(sc->sc_ah->ah_curchan));
749
750 ath_update_txpow(sc);
751
752 if (sc->sc_flags & SC_OP_BEACONS)
753 ath_beacon_config(sc, ATH_IF_ID_ANY); /* restart beacons */
754
755 ath9k_hw_set_interrupts(ah, sc->sc_imask);
756
757 /* Restart the txq */
758 if (retry_tx) {
759 int i;
760 for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
761 if (ATH_TXQ_SETUP(sc, i)) {
762 spin_lock_bh(&sc->sc_txq[i].axq_lock);
763 ath_txq_schedule(sc, &sc->sc_txq[i]);
764 spin_unlock_bh(&sc->sc_txq[i].axq_lock);
765 }
766 }
767 }
768
769 return error;
770 }
771
772 /* Interrupt handler. Most of the actual processing is deferred.
773 * It's the caller's responsibility to ensure the chip is awake. */
774
775 irqreturn_t ath_isr(int irq, void *dev)
776 {
777 struct ath_softc *sc = dev;
778 struct ath_hal *ah = sc->sc_ah;
779 enum ath9k_int status;
780 bool sched = false;
781
782 do {
783 if (sc->sc_flags & SC_OP_INVALID) {
784 /*
785 * The hardware is not ready/present, don't
786 * touch anything. Note this can happen early
787 * on if the IRQ is shared.
788 */
789 return IRQ_NONE;
790 }
791 if (!ath9k_hw_intrpend(ah)) { /* shared irq, not for us */
792 return IRQ_NONE;
793 }
794
795 /*
796 * Figure out the reason(s) for the interrupt. Note
797 * that the hal returns a pseudo-ISR that may include
798 * bits we haven't explicitly enabled so we mask the
799 * value to insure we only process bits we requested.
800 */
801 ath9k_hw_getisr(ah, &status); /* NB: clears ISR too */
802
803 status &= sc->sc_imask; /* discard unasked-for bits */
804
805 /*
806 * If there are no status bits set, then this interrupt was not
807 * for me (should have been caught above).
808 */
809
810 if (!status)
811 return IRQ_NONE;
812
813 sc->sc_intrstatus = status;
814
815 if (status & ATH9K_INT_FATAL) {
816 /* need a chip reset */
817 sched = true;
818 } else if (status & ATH9K_INT_RXORN) {
819 /* need a chip reset */
820 sched = true;
821 } else {
822 if (status & ATH9K_INT_SWBA) {
823 /* schedule a tasklet for beacon handling */
824 tasklet_schedule(&sc->bcon_tasklet);
825 }
826 if (status & ATH9K_INT_RXEOL) {
827 /*
828 * NB: the hardware should re-read the link when
829 * RXE bit is written, but it doesn't work
830 * at least on older hardware revs.
831 */
832 sched = true;
833 }
834
835 if (status & ATH9K_INT_TXURN)
836 /* bump tx trigger level */
837 ath9k_hw_updatetxtriglevel(ah, true);
838 /* XXX: optimize this */
839 if (status & ATH9K_INT_RX)
840 sched = true;
841 if (status & ATH9K_INT_TX)
842 sched = true;
843 if (status & ATH9K_INT_BMISS)
844 sched = true;
845 /* carrier sense timeout */
846 if (status & ATH9K_INT_CST)
847 sched = true;
848 if (status & ATH9K_INT_MIB) {
849 /*
850 * Disable interrupts until we service the MIB
851 * interrupt; otherwise it will continue to
852 * fire.
853 */
854 ath9k_hw_set_interrupts(ah, 0);
855 /*
856 * Let the hal handle the event. We assume
857 * it will clear whatever condition caused
858 * the interrupt.
859 */
860 ath9k_hw_procmibevent(ah, &sc->sc_halstats);
861 ath9k_hw_set_interrupts(ah, sc->sc_imask);
862 }
863 if (status & ATH9K_INT_TIM_TIMER) {
864 if (!(ah->ah_caps.hw_caps &
865 ATH9K_HW_CAP_AUTOSLEEP)) {
866 /* Clear RxAbort bit so that we can
867 * receive frames */
868 ath9k_hw_setrxabort(ah, 0);
869 sched = true;
870 }
871 }
872 }
873 } while (0);
874
875 if (sched) {
876 /* turn off every interrupt except SWBA */
877 ath9k_hw_set_interrupts(ah, (sc->sc_imask & ATH9K_INT_SWBA));
878 tasklet_schedule(&sc->intr_tq);
879 }
880
881 return IRQ_HANDLED;
882 }
883
884 /* Deferred interrupt processing */
885
886 static void ath9k_tasklet(unsigned long data)
887 {
888 struct ath_softc *sc = (struct ath_softc *)data;
889 u32 status = sc->sc_intrstatus;
890
891 if (status & ATH9K_INT_FATAL) {
892 /* need a chip reset */
893 ath_reset(sc, false);
894 return;
895 } else {
896
897 if (status &
898 (ATH9K_INT_RX | ATH9K_INT_RXEOL | ATH9K_INT_RXORN)) {
899 /* XXX: fill me in */
900 /*
901 if (status & ATH9K_INT_RXORN) {
902 }
903 if (status & ATH9K_INT_RXEOL) {
904 }
905 */
906 spin_lock_bh(&sc->sc_rxflushlock);
907 ath_rx_tasklet(sc, 0);
908 spin_unlock_bh(&sc->sc_rxflushlock);
909 }
910 /* XXX: optimize this */
911 if (status & ATH9K_INT_TX)
912 ath_tx_tasklet(sc);
913 /* XXX: fill me in */
914 /*
915 if (status & ATH9K_INT_BMISS) {
916 }
917 if (status & (ATH9K_INT_TIM | ATH9K_INT_DTIMSYNC)) {
918 if (status & ATH9K_INT_TIM) {
919 }
920 if (status & ATH9K_INT_DTIMSYNC) {
921 }
922 }
923 */
924 }
925
926 /* re-enable hardware interrupt */
927 ath9k_hw_set_interrupts(sc->sc_ah, sc->sc_imask);
928 }
929
930 int ath_init(u16 devid, struct ath_softc *sc)
931 {
932 struct ath_hal *ah = NULL;
933 int status;
934 int error = 0, i;
935 int csz = 0;
936
937 /* XXX: hardware will not be ready until ath_open() being called */
938 sc->sc_flags |= SC_OP_INVALID;
939 sc->sc_debug = DBG_DEFAULT;
940
941 spin_lock_init(&sc->sc_resetlock);
942 tasklet_init(&sc->intr_tq, ath9k_tasklet, (unsigned long)sc);
943 tasklet_init(&sc->bcon_tasklet, ath9k_beacon_tasklet,
944 (unsigned long)sc);
945
946 /*
947 * Cache line size is used to size and align various
948 * structures used to communicate with the hardware.
949 */
950 bus_read_cachesize(sc, &csz);
951 /* XXX assert csz is non-zero */
952 sc->sc_cachelsz = csz << 2; /* convert to bytes */
953
954 ah = ath9k_hw_attach(devid, sc, sc->mem, &status);
955 if (ah == NULL) {
956 DPRINTF(sc, ATH_DBG_FATAL,
957 "%s: unable to attach hardware; HAL status %u\n",
958 __func__, status);
959 error = -ENXIO;
960 goto bad;
961 }
962 sc->sc_ah = ah;
963
964 /* Get the hardware key cache size. */
965 sc->sc_keymax = ah->ah_caps.keycache_size;
966 if (sc->sc_keymax > ATH_KEYMAX) {
967 DPRINTF(sc, ATH_DBG_KEYCACHE,
968 "%s: Warning, using only %u entries in %u key cache\n",
969 __func__, ATH_KEYMAX, sc->sc_keymax);
970 sc->sc_keymax = ATH_KEYMAX;
971 }
972
973 /*
974 * Reset the key cache since some parts do not
975 * reset the contents on initial power up.
976 */
977 for (i = 0; i < sc->sc_keymax; i++)
978 ath9k_hw_keyreset(ah, (u16) i);
979 /*
980 * Mark key cache slots associated with global keys
981 * as in use. If we knew TKIP was not to be used we
982 * could leave the +32, +64, and +32+64 slots free.
983 * XXX only for splitmic.
984 */
985 for (i = 0; i < IEEE80211_WEP_NKID; i++) {
986 set_bit(i, sc->sc_keymap);
987 set_bit(i + 32, sc->sc_keymap);
988 set_bit(i + 64, sc->sc_keymap);
989 set_bit(i + 32 + 64, sc->sc_keymap);
990 }
991
992 /* Collect the channel list using the default country code */
993
994 error = ath_setup_channels(sc);
995 if (error)
996 goto bad;
997
998 /* default to MONITOR mode */
999 sc->sc_ah->ah_opmode = ATH9K_M_MONITOR;
1000
1001 /* Setup rate tables */
1002
1003 ath_setup_rates(sc, IEEE80211_BAND_2GHZ);
1004 ath_setup_rates(sc, IEEE80211_BAND_5GHZ);
1005
1006 /* NB: setup here so ath_rate_update is happy */
1007 ath_setcurmode(sc, ATH9K_MODE_11A);
1008
1009 /*
1010 * Allocate hardware transmit queues: one queue for
1011 * beacon frames and one data queue for each QoS
1012 * priority. Note that the hal handles reseting
1013 * these queues at the needed time.
1014 */
1015 sc->sc_bhalq = ath_beaconq_setup(ah);
1016 if (sc->sc_bhalq == -1) {
1017 DPRINTF(sc, ATH_DBG_FATAL,
1018 "%s: unable to setup a beacon xmit queue\n", __func__);
1019 error = -EIO;
1020 goto bad2;
1021 }
1022 sc->sc_cabq = ath_txq_setup(sc, ATH9K_TX_QUEUE_CAB, 0);
1023 if (sc->sc_cabq == NULL) {
1024 DPRINTF(sc, ATH_DBG_FATAL,
1025 "%s: unable to setup CAB xmit queue\n", __func__);
1026 error = -EIO;
1027 goto bad2;
1028 }
1029
1030 sc->sc_config.cabqReadytime = ATH_CABQ_READY_TIME;
1031 ath_cabq_update(sc);
1032
1033 for (i = 0; i < ARRAY_SIZE(sc->sc_haltype2q); i++)
1034 sc->sc_haltype2q[i] = -1;
1035
1036 /* Setup data queues */
1037 /* NB: ensure BK queue is the lowest priority h/w queue */
1038 if (!ath_tx_setup(sc, ATH9K_WME_AC_BK)) {
1039 DPRINTF(sc, ATH_DBG_FATAL,
1040 "%s: unable to setup xmit queue for BK traffic\n",
1041 __func__);
1042 error = -EIO;
1043 goto bad2;
1044 }
1045
1046 if (!ath_tx_setup(sc, ATH9K_WME_AC_BE)) {
1047 DPRINTF(sc, ATH_DBG_FATAL,
1048 "%s: unable to setup xmit queue for BE traffic\n",
1049 __func__);
1050 error = -EIO;
1051 goto bad2;
1052 }
1053 if (!ath_tx_setup(sc, ATH9K_WME_AC_VI)) {
1054 DPRINTF(sc, ATH_DBG_FATAL,
1055 "%s: unable to setup xmit queue for VI traffic\n",
1056 __func__);
1057 error = -EIO;
1058 goto bad2;
1059 }
1060 if (!ath_tx_setup(sc, ATH9K_WME_AC_VO)) {
1061 DPRINTF(sc, ATH_DBG_FATAL,
1062 "%s: unable to setup xmit queue for VO traffic\n",
1063 __func__);
1064 error = -EIO;
1065 goto bad2;
1066 }
1067
1068 /* Initializes the noise floor to a reasonable default value.
1069 * Later on this will be updated during ANI processing. */
1070
1071 sc->sc_ani.sc_noise_floor = ATH_DEFAULT_NOISE_FLOOR;
1072 setup_timer(&sc->sc_ani.timer, ath_ani_calibrate, (unsigned long)sc);
1073
1074 ath_rate_attach(sc);
1075
1076 if (ath9k_hw_getcapability(ah, ATH9K_CAP_CIPHER,
1077 ATH9K_CIPHER_TKIP, NULL)) {
1078 /*
1079 * Whether we should enable h/w TKIP MIC.
1080 * XXX: if we don't support WME TKIP MIC, then we wouldn't
1081 * report WMM capable, so it's always safe to turn on
1082 * TKIP MIC in this case.
1083 */
1084 ath9k_hw_setcapability(sc->sc_ah, ATH9K_CAP_TKIP_MIC,
1085 0, 1, NULL);
1086 }
1087
1088 /*
1089 * Check whether the separate key cache entries
1090 * are required to handle both tx+rx MIC keys.
1091 * With split mic keys the number of stations is limited
1092 * to 27 otherwise 59.
1093 */
1094 if (ath9k_hw_getcapability(ah, ATH9K_CAP_CIPHER,
1095 ATH9K_CIPHER_TKIP, NULL)
1096 && ath9k_hw_getcapability(ah, ATH9K_CAP_CIPHER,
1097 ATH9K_CIPHER_MIC, NULL)
1098 && ath9k_hw_getcapability(ah, ATH9K_CAP_TKIP_SPLIT,
1099 0, NULL))
1100 sc->sc_splitmic = 1;
1101
1102 /* turn on mcast key search if possible */
1103 if (!ath9k_hw_getcapability(ah, ATH9K_CAP_MCAST_KEYSRCH, 0, NULL))
1104 (void)ath9k_hw_setcapability(ah, ATH9K_CAP_MCAST_KEYSRCH, 1,
1105 1, NULL);
1106
1107 sc->sc_config.txpowlimit = ATH_TXPOWER_MAX;
1108 sc->sc_config.txpowlimit_override = 0;
1109
1110 /* 11n Capabilities */
1111 if (ah->ah_caps.hw_caps & ATH9K_HW_CAP_HT) {
1112 sc->sc_flags |= SC_OP_TXAGGR;
1113 sc->sc_flags |= SC_OP_RXAGGR;
1114 }
1115
1116 sc->sc_tx_chainmask = ah->ah_caps.tx_chainmask;
1117 sc->sc_rx_chainmask = ah->ah_caps.rx_chainmask;
1118
1119 ath9k_hw_setcapability(ah, ATH9K_CAP_DIVERSITY, 1, true, NULL);
1120 sc->sc_defant = ath9k_hw_getdefantenna(ah);
1121
1122 ath9k_hw_getmac(ah, sc->sc_myaddr);
1123 if (ah->ah_caps.hw_caps & ATH9K_HW_CAP_BSSIDMASK) {
1124 ath9k_hw_getbssidmask(ah, sc->sc_bssidmask);
1125 ATH_SET_VAP_BSSID_MASK(sc->sc_bssidmask);
1126 ath9k_hw_setbssidmask(ah, sc->sc_bssidmask);
1127 }
1128
1129 sc->sc_slottime = ATH9K_SLOT_TIME_9; /* default to short slot time */
1130
1131 /* initialize beacon slots */
1132 for (i = 0; i < ARRAY_SIZE(sc->sc_bslot); i++)
1133 sc->sc_bslot[i] = ATH_IF_ID_ANY;
1134
1135 /* save MISC configurations */
1136 sc->sc_config.swBeaconProcess = 1;
1137
1138 #ifdef CONFIG_SLOW_ANT_DIV
1139 /* range is 40 - 255, we use something in the middle */
1140 ath_slow_ant_div_init(&sc->sc_antdiv, sc, 0x127);
1141 #endif
1142
1143 /* setup channels and rates */
1144
1145 sc->sbands[IEEE80211_BAND_2GHZ].channels =
1146 sc->channels[IEEE80211_BAND_2GHZ];
1147 sc->sbands[IEEE80211_BAND_2GHZ].bitrates =
1148 sc->rates[IEEE80211_BAND_2GHZ];
1149 sc->sbands[IEEE80211_BAND_2GHZ].band = IEEE80211_BAND_2GHZ;
1150
1151 if (test_bit(ATH9K_MODE_11A, sc->sc_ah->ah_caps.wireless_modes)) {
1152 sc->sbands[IEEE80211_BAND_5GHZ].channels =
1153 sc->channels[IEEE80211_BAND_5GHZ];
1154 sc->sbands[IEEE80211_BAND_5GHZ].bitrates =
1155 sc->rates[IEEE80211_BAND_5GHZ];
1156 sc->sbands[IEEE80211_BAND_5GHZ].band = IEEE80211_BAND_5GHZ;
1157 }
1158
1159 return 0;
1160 bad2:
1161 /* cleanup tx queues */
1162 for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++)
1163 if (ATH_TXQ_SETUP(sc, i))
1164 ath_tx_cleanupq(sc, &sc->sc_txq[i]);
1165 bad:
1166 if (ah)
1167 ath9k_hw_detach(ah);
1168
1169 return error;
1170 }
1171
1172 /*******************/
1173 /* Node Management */
1174 /*******************/
1175
1176 void ath_node_attach(struct ath_softc *sc, struct ieee80211_sta *sta)
1177 {
1178 struct ath_node *an;
1179
1180 an = (struct ath_node *)sta->drv_priv;
1181
1182 if (sc->sc_flags & SC_OP_TXAGGR)
1183 ath_tx_node_init(sc, an);
1184
1185 an->maxampdu = 1 << (IEEE80211_HTCAP_MAXRXAMPDU_FACTOR +
1186 sta->ht_cap.ampdu_factor);
1187 an->mpdudensity = parse_mpdudensity(sta->ht_cap.ampdu_density);
1188
1189 ath_chainmask_sel_init(sc, an);
1190 ath_chainmask_sel_timerstart(&an->an_chainmask_sel);
1191 }
1192
1193 void ath_node_detach(struct ath_softc *sc, struct ieee80211_sta *sta)
1194 {
1195 struct ath_node *an = (struct ath_node *)sta->drv_priv;
1196
1197 ath_chainmask_sel_timerstop(&an->an_chainmask_sel);
1198
1199 if (sc->sc_flags & SC_OP_TXAGGR)
1200 ath_tx_node_cleanup(sc, an);
1201 }
1202
1203 /*
1204 * Set up New Node
1205 *
1206 * Setup driver-specific state for a newly associated node. This routine
1207 * really only applies if compression or XR are enabled, there is no code
1208 * covering any other cases.
1209 */
1210
1211 void ath_newassoc(struct ath_softc *sc,
1212 struct ath_node *an, int isnew, int isuapsd)
1213 {
1214 int tidno;
1215
1216 /* if station reassociates, tear down the aggregation state. */
1217 if (!isnew) {
1218 for (tidno = 0; tidno < WME_NUM_TID; tidno++) {
1219 if (sc->sc_flags & SC_OP_TXAGGR)
1220 ath_tx_aggr_teardown(sc, an, tidno);
1221 }
1222 }
1223 }
1224
1225 /**************/
1226 /* Encryption */
1227 /**************/
1228
1229 void ath_key_reset(struct ath_softc *sc, u16 keyix, int freeslot)
1230 {
1231 ath9k_hw_keyreset(sc->sc_ah, keyix);
1232 if (freeslot)
1233 clear_bit(keyix, sc->sc_keymap);
1234 }
1235
1236 int ath_keyset(struct ath_softc *sc,
1237 u16 keyix,
1238 struct ath9k_keyval *hk,
1239 const u8 mac[ETH_ALEN])
1240 {
1241 bool status;
1242
1243 status = ath9k_hw_set_keycache_entry(sc->sc_ah,
1244 keyix, hk, mac, false);
1245
1246 return status != false;
1247 }
1248
1249 /***********************/
1250 /* TX Power/Regulatory */
1251 /***********************/
1252
1253 /*
1254 * Set Transmit power in HAL
1255 *
1256 * This routine makes the actual HAL calls to set the new transmit power
1257 * limit.
1258 */
1259
1260 void ath_update_txpow(struct ath_softc *sc)
1261 {
1262 struct ath_hal *ah = sc->sc_ah;
1263 u32 txpow;
1264
1265 if (sc->sc_curtxpow != sc->sc_config.txpowlimit) {
1266 ath9k_hw_set_txpowerlimit(ah, sc->sc_config.txpowlimit);
1267 /* read back in case value is clamped */
1268 ath9k_hw_getcapability(ah, ATH9K_CAP_TXPOW, 1, &txpow);
1269 sc->sc_curtxpow = txpow;
1270 }
1271 }
1272
1273 /**************************/
1274 /* Slow Antenna Diversity */
1275 /**************************/
1276
1277 void ath_slow_ant_div_init(struct ath_antdiv *antdiv,
1278 struct ath_softc *sc,
1279 int32_t rssitrig)
1280 {
1281 int trig;
1282
1283 /* antdivf_rssitrig can range from 40 - 0xff */
1284 trig = (rssitrig > 0xff) ? 0xff : rssitrig;
1285 trig = (rssitrig < 40) ? 40 : rssitrig;
1286
1287 antdiv->antdiv_sc = sc;
1288 antdiv->antdivf_rssitrig = trig;
1289 }
1290
1291 void ath_slow_ant_div_start(struct ath_antdiv *antdiv,
1292 u8 num_antcfg,
1293 const u8 *bssid)
1294 {
1295 antdiv->antdiv_num_antcfg =
1296 num_antcfg < ATH_ANT_DIV_MAX_CFG ?
1297 num_antcfg : ATH_ANT_DIV_MAX_CFG;
1298 antdiv->antdiv_state = ATH_ANT_DIV_IDLE;
1299 antdiv->antdiv_curcfg = 0;
1300 antdiv->antdiv_bestcfg = 0;
1301 antdiv->antdiv_laststatetsf = 0;
1302
1303 memcpy(antdiv->antdiv_bssid, bssid, sizeof(antdiv->antdiv_bssid));
1304
1305 antdiv->antdiv_start = 1;
1306 }
1307
1308 void ath_slow_ant_div_stop(struct ath_antdiv *antdiv)
1309 {
1310 antdiv->antdiv_start = 0;
1311 }
1312
1313 static int32_t ath_find_max_val(int32_t *val,
1314 u8 num_val, u8 *max_index)
1315 {
1316 u32 MaxVal = *val++;
1317 u32 cur_index = 0;
1318
1319 *max_index = 0;
1320 while (++cur_index < num_val) {
1321 if (*val > MaxVal) {
1322 MaxVal = *val;
1323 *max_index = cur_index;
1324 }
1325
1326 val++;
1327 }
1328
1329 return MaxVal;
1330 }
1331
1332 void ath_slow_ant_div(struct ath_antdiv *antdiv,
1333 struct ieee80211_hdr *hdr,
1334 struct ath_rx_status *rx_stats)
1335 {
1336 struct ath_softc *sc = antdiv->antdiv_sc;
1337 struct ath_hal *ah = sc->sc_ah;
1338 u64 curtsf = 0;
1339 u8 bestcfg, curcfg = antdiv->antdiv_curcfg;
1340 __le16 fc = hdr->frame_control;
1341
1342 if (antdiv->antdiv_start && ieee80211_is_beacon(fc)
1343 && !compare_ether_addr(hdr->addr3, antdiv->antdiv_bssid)) {
1344 antdiv->antdiv_lastbrssi[curcfg] = rx_stats->rs_rssi;
1345 antdiv->antdiv_lastbtsf[curcfg] = ath9k_hw_gettsf64(sc->sc_ah);
1346 curtsf = antdiv->antdiv_lastbtsf[curcfg];
1347 } else {
1348 return;
1349 }
1350
1351 switch (antdiv->antdiv_state) {
1352 case ATH_ANT_DIV_IDLE:
1353 if ((antdiv->antdiv_lastbrssi[curcfg] <
1354 antdiv->antdivf_rssitrig)
1355 && ((curtsf - antdiv->antdiv_laststatetsf) >
1356 ATH_ANT_DIV_MIN_IDLE_US)) {
1357
1358 curcfg++;
1359 if (curcfg == antdiv->antdiv_num_antcfg)
1360 curcfg = 0;
1361
1362 if (!ath9k_hw_select_antconfig(ah, curcfg)) {
1363 antdiv->antdiv_bestcfg = antdiv->antdiv_curcfg;
1364 antdiv->antdiv_curcfg = curcfg;
1365 antdiv->antdiv_laststatetsf = curtsf;
1366 antdiv->antdiv_state = ATH_ANT_DIV_SCAN;
1367 }
1368 }
1369 break;
1370
1371 case ATH_ANT_DIV_SCAN:
1372 if ((curtsf - antdiv->antdiv_laststatetsf) <
1373 ATH_ANT_DIV_MIN_SCAN_US)
1374 break;
1375
1376 curcfg++;
1377 if (curcfg == antdiv->antdiv_num_antcfg)
1378 curcfg = 0;
1379
1380 if (curcfg == antdiv->antdiv_bestcfg) {
1381 ath_find_max_val(antdiv->antdiv_lastbrssi,
1382 antdiv->antdiv_num_antcfg, &bestcfg);
1383 if (!ath9k_hw_select_antconfig(ah, bestcfg)) {
1384 antdiv->antdiv_bestcfg = bestcfg;
1385 antdiv->antdiv_curcfg = bestcfg;
1386 antdiv->antdiv_laststatetsf = curtsf;
1387 antdiv->antdiv_state = ATH_ANT_DIV_IDLE;
1388 }
1389 } else {
1390 if (!ath9k_hw_select_antconfig(ah, curcfg)) {
1391 antdiv->antdiv_curcfg = curcfg;
1392 antdiv->antdiv_laststatetsf = curtsf;
1393 antdiv->antdiv_state = ATH_ANT_DIV_SCAN;
1394 }
1395 }
1396
1397 break;
1398 }
1399 }
1400
1401 /***********************/
1402 /* Descriptor Handling */
1403 /***********************/
1404
1405 /*
1406 * Set up DMA descriptors
1407 *
1408 * This function will allocate both the DMA descriptor structure, and the
1409 * buffers it contains. These are used to contain the descriptors used
1410 * by the system.
1411 */
1412
1413 int ath_descdma_setup(struct ath_softc *sc,
1414 struct ath_descdma *dd,
1415 struct list_head *head,
1416 const char *name,
1417 int nbuf,
1418 int ndesc)
1419 {
1420 #define DS2PHYS(_dd, _ds) \
1421 ((_dd)->dd_desc_paddr + ((caddr_t)(_ds) - (caddr_t)(_dd)->dd_desc))
1422 #define ATH_DESC_4KB_BOUND_CHECK(_daddr) ((((_daddr) & 0xFFF) > 0xF7F) ? 1 : 0)
1423 #define ATH_DESC_4KB_BOUND_NUM_SKIPPED(_len) ((_len) / 4096)
1424
1425 struct ath_desc *ds;
1426 struct ath_buf *bf;
1427 int i, bsize, error;
1428
1429 DPRINTF(sc, ATH_DBG_CONFIG, "%s: %s DMA: %u buffers %u desc/buf\n",
1430 __func__, name, nbuf, ndesc);
1431
1432 /* ath_desc must be a multiple of DWORDs */
1433 if ((sizeof(struct ath_desc) % 4) != 0) {
1434 DPRINTF(sc, ATH_DBG_FATAL, "%s: ath_desc not DWORD aligned\n",
1435 __func__);
1436 ASSERT((sizeof(struct ath_desc) % 4) == 0);
1437 error = -ENOMEM;
1438 goto fail;
1439 }
1440
1441 dd->dd_name = name;
1442 dd->dd_desc_len = sizeof(struct ath_desc) * nbuf * ndesc;
1443
1444 /*
1445 * Need additional DMA memory because we can't use
1446 * descriptors that cross the 4K page boundary. Assume
1447 * one skipped descriptor per 4K page.
1448 */
1449 if (!(sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_4KB_SPLITTRANS)) {
1450 u32 ndesc_skipped =
1451 ATH_DESC_4KB_BOUND_NUM_SKIPPED(dd->dd_desc_len);
1452 u32 dma_len;
1453
1454 while (ndesc_skipped) {
1455 dma_len = ndesc_skipped * sizeof(struct ath_desc);
1456 dd->dd_desc_len += dma_len;
1457
1458 ndesc_skipped = ATH_DESC_4KB_BOUND_NUM_SKIPPED(dma_len);
1459 };
1460 }
1461
1462 /* allocate descriptors */
1463 dd->dd_desc = pci_alloc_consistent(sc->pdev,
1464 dd->dd_desc_len,
1465 &dd->dd_desc_paddr);
1466 if (dd->dd_desc == NULL) {
1467 error = -ENOMEM;
1468 goto fail;
1469 }
1470 ds = dd->dd_desc;
1471 DPRINTF(sc, ATH_DBG_CONFIG, "%s: %s DMA map: %p (%u) -> %llx (%u)\n",
1472 __func__, dd->dd_name, ds, (u32) dd->dd_desc_len,
1473 ito64(dd->dd_desc_paddr), /*XXX*/(u32) dd->dd_desc_len);
1474
1475 /* allocate buffers */
1476 bsize = sizeof(struct ath_buf) * nbuf;
1477 bf = kmalloc(bsize, GFP_KERNEL);
1478 if (bf == NULL) {
1479 error = -ENOMEM;
1480 goto fail2;
1481 }
1482 memset(bf, 0, bsize);
1483 dd->dd_bufptr = bf;
1484
1485 INIT_LIST_HEAD(head);
1486 for (i = 0; i < nbuf; i++, bf++, ds += ndesc) {
1487 bf->bf_desc = ds;
1488 bf->bf_daddr = DS2PHYS(dd, ds);
1489
1490 if (!(sc->sc_ah->ah_caps.hw_caps &
1491 ATH9K_HW_CAP_4KB_SPLITTRANS)) {
1492 /*
1493 * Skip descriptor addresses which can cause 4KB
1494 * boundary crossing (addr + length) with a 32 dword
1495 * descriptor fetch.
1496 */
1497 while (ATH_DESC_4KB_BOUND_CHECK(bf->bf_daddr)) {
1498 ASSERT((caddr_t) bf->bf_desc <
1499 ((caddr_t) dd->dd_desc +
1500 dd->dd_desc_len));
1501
1502 ds += ndesc;
1503 bf->bf_desc = ds;
1504 bf->bf_daddr = DS2PHYS(dd, ds);
1505 }
1506 }
1507 list_add_tail(&bf->list, head);
1508 }
1509 return 0;
1510 fail2:
1511 pci_free_consistent(sc->pdev,
1512 dd->dd_desc_len, dd->dd_desc, dd->dd_desc_paddr);
1513 fail:
1514 memset(dd, 0, sizeof(*dd));
1515 return error;
1516 #undef ATH_DESC_4KB_BOUND_CHECK
1517 #undef ATH_DESC_4KB_BOUND_NUM_SKIPPED
1518 #undef DS2PHYS
1519 }
1520
1521 /*
1522 * Cleanup DMA descriptors
1523 *
1524 * This function will free the DMA block that was allocated for the descriptor
1525 * pool. Since this was allocated as one "chunk", it is freed in the same
1526 * manner.
1527 */
1528
1529 void ath_descdma_cleanup(struct ath_softc *sc,
1530 struct ath_descdma *dd,
1531 struct list_head *head)
1532 {
1533 /* Free memory associated with descriptors */
1534 pci_free_consistent(sc->pdev,
1535 dd->dd_desc_len, dd->dd_desc, dd->dd_desc_paddr);
1536
1537 INIT_LIST_HEAD(head);
1538 kfree(dd->dd_bufptr);
1539 memset(dd, 0, sizeof(*dd));
1540 }
1541
1542 /*************/
1543 /* Utilities */
1544 /*************/
1545
1546 int ath_get_hal_qnum(u16 queue, struct ath_softc *sc)
1547 {
1548 int qnum;
1549
1550 switch (queue) {
1551 case 0:
1552 qnum = sc->sc_haltype2q[ATH9K_WME_AC_VO];
1553 break;
1554 case 1:
1555 qnum = sc->sc_haltype2q[ATH9K_WME_AC_VI];
1556 break;
1557 case 2:
1558 qnum = sc->sc_haltype2q[ATH9K_WME_AC_BE];
1559 break;
1560 case 3:
1561 qnum = sc->sc_haltype2q[ATH9K_WME_AC_BK];
1562 break;
1563 default:
1564 qnum = sc->sc_haltype2q[ATH9K_WME_AC_BE];
1565 break;
1566 }
1567
1568 return qnum;
1569 }
1570
1571 int ath_get_mac80211_qnum(u32 queue, struct ath_softc *sc)
1572 {
1573 int qnum;
1574
1575 switch (queue) {
1576 case ATH9K_WME_AC_VO:
1577 qnum = 0;
1578 break;
1579 case ATH9K_WME_AC_VI:
1580 qnum = 1;
1581 break;
1582 case ATH9K_WME_AC_BE:
1583 qnum = 2;
1584 break;
1585 case ATH9K_WME_AC_BK:
1586 qnum = 3;
1587 break;
1588 default:
1589 qnum = -1;
1590 break;
1591 }
1592
1593 return qnum;
1594 }
1595
1596
1597 /*
1598 * Expand time stamp to TSF
1599 *
1600 * Extend 15-bit time stamp from rx descriptor to
1601 * a full 64-bit TSF using the current h/w TSF.
1602 */
1603
1604 u64 ath_extend_tsf(struct ath_softc *sc, u32 rstamp)
1605 {
1606 u64 tsf;
1607
1608 tsf = ath9k_hw_gettsf64(sc->sc_ah);
1609 if ((tsf & 0x7fff) < rstamp)
1610 tsf -= 0x8000;
1611 return (tsf & ~0x7fff) | rstamp;
1612 }
1613
1614 /*
1615 * Set Default Antenna
1616 *
1617 * Call into the HAL to set the default antenna to use. Not really valid for
1618 * MIMO technology.
1619 */
1620
1621 void ath_setdefantenna(void *context, u32 antenna)
1622 {
1623 struct ath_softc *sc = (struct ath_softc *)context;
1624 struct ath_hal *ah = sc->sc_ah;
1625
1626 /* XXX block beacon interrupts */
1627 ath9k_hw_setantenna(ah, antenna);
1628 sc->sc_defant = antenna;
1629 sc->sc_rxotherant = 0;
1630 }
1631
1632 /*
1633 * Set Slot Time
1634 *
1635 * This will wake up the chip if required, and set the slot time for the
1636 * frame (maximum transmit time). Slot time is assumed to be already set
1637 * in the ATH object member sc_slottime
1638 */
1639
1640 void ath_setslottime(struct ath_softc *sc)
1641 {
1642 ath9k_hw_setslottime(sc->sc_ah, sc->sc_slottime);
1643 sc->sc_updateslot = OK;
1644 }
This page took 0.062717 seconds and 4 git commands to generate.