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