ath: Convert ath_print to ath_dbg
[linux-2.6-block.git] / drivers / net / wireless / ath / ath5k / base.c
1 /*-
2  * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
3  * Copyright (c) 2004-2005 Atheros Communications, Inc.
4  * Copyright (c) 2006 Devicescape Software, Inc.
5  * Copyright (c) 2007 Jiri Slaby <jirislaby@gmail.com>
6  * Copyright (c) 2007 Luis R. Rodriguez <mcgrof@winlab.rutgers.edu>
7  *
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer,
15  *    without modification.
16  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
17  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
18  *    redistribution must be conditioned upon including a substantially
19  *    similar Disclaimer requirement for further binary redistribution.
20  * 3. Neither the names of the above-listed copyright holders nor the names
21  *    of any contributors may be used to endorse or promote products derived
22  *    from this software without specific prior written permission.
23  *
24  * Alternatively, this software may be distributed under the terms of the
25  * GNU General Public License ("GPL") version 2 as published by the Free
26  * Software Foundation.
27  *
28  * NO WARRANTY
29  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
32  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
33  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
34  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
37  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
39  * THE POSSIBILITY OF SUCH DAMAGES.
40  *
41  */
42
43 #include <linux/module.h>
44 #include <linux/delay.h>
45 #include <linux/hardirq.h>
46 #include <linux/if.h>
47 #include <linux/io.h>
48 #include <linux/netdevice.h>
49 #include <linux/cache.h>
50 #include <linux/ethtool.h>
51 #include <linux/uaccess.h>
52 #include <linux/slab.h>
53 #include <linux/etherdevice.h>
54
55 #include <net/ieee80211_radiotap.h>
56
57 #include <asm/unaligned.h>
58
59 #include "base.h"
60 #include "reg.h"
61 #include "debug.h"
62 #include "ani.h"
63
64 static int modparam_nohwcrypt;
65 module_param_named(nohwcrypt, modparam_nohwcrypt, bool, S_IRUGO);
66 MODULE_PARM_DESC(nohwcrypt, "Disable hardware encryption.");
67
68 static int modparam_all_channels;
69 module_param_named(all_channels, modparam_all_channels, bool, S_IRUGO);
70 MODULE_PARM_DESC(all_channels, "Expose all channels the device can use.");
71
72 /* Module info */
73 MODULE_AUTHOR("Jiri Slaby");
74 MODULE_AUTHOR("Nick Kossifidis");
75 MODULE_DESCRIPTION("Support for 5xxx series of Atheros 802.11 wireless LAN cards.");
76 MODULE_SUPPORTED_DEVICE("Atheros 5xxx WLAN cards");
77 MODULE_LICENSE("Dual BSD/GPL");
78 MODULE_VERSION("0.6.0 (EXPERIMENTAL)");
79
80 static int ath5k_init(struct ieee80211_hw *hw);
81 static int ath5k_reset(struct ath5k_softc *sc, struct ieee80211_channel *chan,
82                                                                 bool skip_pcu);
83 static int ath5k_beacon_update(struct ieee80211_hw *hw,
84                 struct ieee80211_vif *vif);
85 static void ath5k_beacon_update_timers(struct ath5k_softc *sc, u64 bc_tsf);
86
87 /* Known SREVs */
88 static const struct ath5k_srev_name srev_names[] = {
89 #ifdef CONFIG_ATHEROS_AR231X
90         { "5312",       AR5K_VERSION_MAC,       AR5K_SREV_AR5312_R2 },
91         { "5312",       AR5K_VERSION_MAC,       AR5K_SREV_AR5312_R7 },
92         { "2313",       AR5K_VERSION_MAC,       AR5K_SREV_AR2313_R8 },
93         { "2315",       AR5K_VERSION_MAC,       AR5K_SREV_AR2315_R6 },
94         { "2315",       AR5K_VERSION_MAC,       AR5K_SREV_AR2315_R7 },
95         { "2317",       AR5K_VERSION_MAC,       AR5K_SREV_AR2317_R1 },
96         { "2317",       AR5K_VERSION_MAC,       AR5K_SREV_AR2317_R2 },
97 #else
98         { "5210",       AR5K_VERSION_MAC,       AR5K_SREV_AR5210 },
99         { "5311",       AR5K_VERSION_MAC,       AR5K_SREV_AR5311 },
100         { "5311A",      AR5K_VERSION_MAC,       AR5K_SREV_AR5311A },
101         { "5311B",      AR5K_VERSION_MAC,       AR5K_SREV_AR5311B },
102         { "5211",       AR5K_VERSION_MAC,       AR5K_SREV_AR5211 },
103         { "5212",       AR5K_VERSION_MAC,       AR5K_SREV_AR5212 },
104         { "5213",       AR5K_VERSION_MAC,       AR5K_SREV_AR5213 },
105         { "5213A",      AR5K_VERSION_MAC,       AR5K_SREV_AR5213A },
106         { "2413",       AR5K_VERSION_MAC,       AR5K_SREV_AR2413 },
107         { "2414",       AR5K_VERSION_MAC,       AR5K_SREV_AR2414 },
108         { "5424",       AR5K_VERSION_MAC,       AR5K_SREV_AR5424 },
109         { "5413",       AR5K_VERSION_MAC,       AR5K_SREV_AR5413 },
110         { "5414",       AR5K_VERSION_MAC,       AR5K_SREV_AR5414 },
111         { "2415",       AR5K_VERSION_MAC,       AR5K_SREV_AR2415 },
112         { "5416",       AR5K_VERSION_MAC,       AR5K_SREV_AR5416 },
113         { "5418",       AR5K_VERSION_MAC,       AR5K_SREV_AR5418 },
114         { "2425",       AR5K_VERSION_MAC,       AR5K_SREV_AR2425 },
115         { "2417",       AR5K_VERSION_MAC,       AR5K_SREV_AR2417 },
116 #endif
117         { "xxxxx",      AR5K_VERSION_MAC,       AR5K_SREV_UNKNOWN },
118         { "5110",       AR5K_VERSION_RAD,       AR5K_SREV_RAD_5110 },
119         { "5111",       AR5K_VERSION_RAD,       AR5K_SREV_RAD_5111 },
120         { "5111A",      AR5K_VERSION_RAD,       AR5K_SREV_RAD_5111A },
121         { "2111",       AR5K_VERSION_RAD,       AR5K_SREV_RAD_2111 },
122         { "5112",       AR5K_VERSION_RAD,       AR5K_SREV_RAD_5112 },
123         { "5112A",      AR5K_VERSION_RAD,       AR5K_SREV_RAD_5112A },
124         { "5112B",      AR5K_VERSION_RAD,       AR5K_SREV_RAD_5112B },
125         { "2112",       AR5K_VERSION_RAD,       AR5K_SREV_RAD_2112 },
126         { "2112A",      AR5K_VERSION_RAD,       AR5K_SREV_RAD_2112A },
127         { "2112B",      AR5K_VERSION_RAD,       AR5K_SREV_RAD_2112B },
128         { "2413",       AR5K_VERSION_RAD,       AR5K_SREV_RAD_2413 },
129         { "5413",       AR5K_VERSION_RAD,       AR5K_SREV_RAD_5413 },
130         { "5424",       AR5K_VERSION_RAD,       AR5K_SREV_RAD_5424 },
131         { "5133",       AR5K_VERSION_RAD,       AR5K_SREV_RAD_5133 },
132 #ifdef CONFIG_ATHEROS_AR231X
133         { "2316",       AR5K_VERSION_RAD,       AR5K_SREV_RAD_2316 },
134         { "2317",       AR5K_VERSION_RAD,       AR5K_SREV_RAD_2317 },
135 #endif
136         { "xxxxx",      AR5K_VERSION_RAD,       AR5K_SREV_UNKNOWN },
137 };
138
139 static const struct ieee80211_rate ath5k_rates[] = {
140         { .bitrate = 10,
141           .hw_value = ATH5K_RATE_CODE_1M, },
142         { .bitrate = 20,
143           .hw_value = ATH5K_RATE_CODE_2M,
144           .hw_value_short = ATH5K_RATE_CODE_2M | AR5K_SET_SHORT_PREAMBLE,
145           .flags = IEEE80211_RATE_SHORT_PREAMBLE },
146         { .bitrate = 55,
147           .hw_value = ATH5K_RATE_CODE_5_5M,
148           .hw_value_short = ATH5K_RATE_CODE_5_5M | AR5K_SET_SHORT_PREAMBLE,
149           .flags = IEEE80211_RATE_SHORT_PREAMBLE },
150         { .bitrate = 110,
151           .hw_value = ATH5K_RATE_CODE_11M,
152           .hw_value_short = ATH5K_RATE_CODE_11M | AR5K_SET_SHORT_PREAMBLE,
153           .flags = IEEE80211_RATE_SHORT_PREAMBLE },
154         { .bitrate = 60,
155           .hw_value = ATH5K_RATE_CODE_6M,
156           .flags = 0 },
157         { .bitrate = 90,
158           .hw_value = ATH5K_RATE_CODE_9M,
159           .flags = 0 },
160         { .bitrate = 120,
161           .hw_value = ATH5K_RATE_CODE_12M,
162           .flags = 0 },
163         { .bitrate = 180,
164           .hw_value = ATH5K_RATE_CODE_18M,
165           .flags = 0 },
166         { .bitrate = 240,
167           .hw_value = ATH5K_RATE_CODE_24M,
168           .flags = 0 },
169         { .bitrate = 360,
170           .hw_value = ATH5K_RATE_CODE_36M,
171           .flags = 0 },
172         { .bitrate = 480,
173           .hw_value = ATH5K_RATE_CODE_48M,
174           .flags = 0 },
175         { .bitrate = 540,
176           .hw_value = ATH5K_RATE_CODE_54M,
177           .flags = 0 },
178         /* XR missing */
179 };
180
181 static inline void ath5k_txbuf_free_skb(struct ath5k_softc *sc,
182                                 struct ath5k_buf *bf)
183 {
184         BUG_ON(!bf);
185         if (!bf->skb)
186                 return;
187         dma_unmap_single(sc->dev, bf->skbaddr, bf->skb->len,
188                         DMA_TO_DEVICE);
189         dev_kfree_skb_any(bf->skb);
190         bf->skb = NULL;
191         bf->skbaddr = 0;
192         bf->desc->ds_data = 0;
193 }
194
195 static inline void ath5k_rxbuf_free_skb(struct ath5k_softc *sc,
196                                 struct ath5k_buf *bf)
197 {
198         struct ath5k_hw *ah = sc->ah;
199         struct ath_common *common = ath5k_hw_common(ah);
200
201         BUG_ON(!bf);
202         if (!bf->skb)
203                 return;
204         dma_unmap_single(sc->dev, bf->skbaddr, common->rx_bufsize,
205                         DMA_FROM_DEVICE);
206         dev_kfree_skb_any(bf->skb);
207         bf->skb = NULL;
208         bf->skbaddr = 0;
209         bf->desc->ds_data = 0;
210 }
211
212
213 static inline u64 ath5k_extend_tsf(struct ath5k_hw *ah, u32 rstamp)
214 {
215         u64 tsf = ath5k_hw_get_tsf64(ah);
216
217         if ((tsf & 0x7fff) < rstamp)
218                 tsf -= 0x8000;
219
220         return (tsf & ~0x7fff) | rstamp;
221 }
222
223 const char *
224 ath5k_chip_name(enum ath5k_srev_type type, u_int16_t val)
225 {
226         const char *name = "xxxxx";
227         unsigned int i;
228
229         for (i = 0; i < ARRAY_SIZE(srev_names); i++) {
230                 if (srev_names[i].sr_type != type)
231                         continue;
232
233                 if ((val & 0xf0) == srev_names[i].sr_val)
234                         name = srev_names[i].sr_name;
235
236                 if ((val & 0xff) == srev_names[i].sr_val) {
237                         name = srev_names[i].sr_name;
238                         break;
239                 }
240         }
241
242         return name;
243 }
244 static unsigned int ath5k_ioread32(void *hw_priv, u32 reg_offset)
245 {
246         struct ath5k_hw *ah = (struct ath5k_hw *) hw_priv;
247         return ath5k_hw_reg_read(ah, reg_offset);
248 }
249
250 static void ath5k_iowrite32(void *hw_priv, u32 val, u32 reg_offset)
251 {
252         struct ath5k_hw *ah = (struct ath5k_hw *) hw_priv;
253         ath5k_hw_reg_write(ah, val, reg_offset);
254 }
255
256 static const struct ath_ops ath5k_common_ops = {
257         .read = ath5k_ioread32,
258         .write = ath5k_iowrite32,
259 };
260
261 /***********************\
262 * Driver Initialization *
263 \***********************/
264
265 static int ath5k_reg_notifier(struct wiphy *wiphy, struct regulatory_request *request)
266 {
267         struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
268         struct ath5k_softc *sc = hw->priv;
269         struct ath_regulatory *regulatory = ath5k_hw_regulatory(sc->ah);
270
271         return ath_reg_notifier_apply(wiphy, request, regulatory);
272 }
273
274 /********************\
275 * Channel/mode setup *
276 \********************/
277
278 /*
279  * Convert IEEE channel number to MHz frequency.
280  */
281 static inline short
282 ath5k_ieee2mhz(short chan)
283 {
284         if (chan <= 14 || chan >= 27)
285                 return ieee80211chan2mhz(chan);
286         else
287                 return 2212 + chan * 20;
288 }
289
290 /*
291  * Returns true for the channel numbers used without all_channels modparam.
292  */
293 static bool ath5k_is_standard_channel(short chan)
294 {
295         return ((chan <= 14) ||
296                 /* UNII 1,2 */
297                 ((chan & 3) == 0 && chan >= 36 && chan <= 64) ||
298                 /* midband */
299                 ((chan & 3) == 0 && chan >= 100 && chan <= 140) ||
300                 /* UNII-3 */
301                 ((chan & 3) == 1 && chan >= 149 && chan <= 165));
302 }
303
304 static unsigned int
305 ath5k_copy_channels(struct ath5k_hw *ah,
306                 struct ieee80211_channel *channels,
307                 unsigned int mode,
308                 unsigned int max)
309 {
310         unsigned int i, count, size, chfreq, freq, ch;
311
312         if (!test_bit(mode, ah->ah_modes))
313                 return 0;
314
315         switch (mode) {
316         case AR5K_MODE_11A:
317                 /* 1..220, but 2GHz frequencies are filtered by check_channel */
318                 size = 220 ;
319                 chfreq = CHANNEL_5GHZ;
320                 break;
321         case AR5K_MODE_11B:
322         case AR5K_MODE_11G:
323                 size = 26;
324                 chfreq = CHANNEL_2GHZ;
325                 break;
326         default:
327                 ATH5K_WARN(ah->ah_sc, "bad mode, not copying channels\n");
328                 return 0;
329         }
330
331         for (i = 0, count = 0; i < size && max > 0; i++) {
332                 ch = i + 1 ;
333                 freq = ath5k_ieee2mhz(ch);
334
335                 /* Check if channel is supported by the chipset */
336                 if (!ath5k_channel_ok(ah, freq, chfreq))
337                         continue;
338
339                 if (!modparam_all_channels && !ath5k_is_standard_channel(ch))
340                         continue;
341
342                 /* Write channel info and increment counter */
343                 channels[count].center_freq = freq;
344                 channels[count].band = (chfreq == CHANNEL_2GHZ) ?
345                         IEEE80211_BAND_2GHZ : IEEE80211_BAND_5GHZ;
346                 switch (mode) {
347                 case AR5K_MODE_11A:
348                 case AR5K_MODE_11G:
349                         channels[count].hw_value = chfreq | CHANNEL_OFDM;
350                         break;
351                 case AR5K_MODE_11B:
352                         channels[count].hw_value = CHANNEL_B;
353                 }
354
355                 count++;
356                 max--;
357         }
358
359         return count;
360 }
361
362 static void
363 ath5k_setup_rate_idx(struct ath5k_softc *sc, struct ieee80211_supported_band *b)
364 {
365         u8 i;
366
367         for (i = 0; i < AR5K_MAX_RATES; i++)
368                 sc->rate_idx[b->band][i] = -1;
369
370         for (i = 0; i < b->n_bitrates; i++) {
371                 sc->rate_idx[b->band][b->bitrates[i].hw_value] = i;
372                 if (b->bitrates[i].hw_value_short)
373                         sc->rate_idx[b->band][b->bitrates[i].hw_value_short] = i;
374         }
375 }
376
377 static int
378 ath5k_setup_bands(struct ieee80211_hw *hw)
379 {
380         struct ath5k_softc *sc = hw->priv;
381         struct ath5k_hw *ah = sc->ah;
382         struct ieee80211_supported_band *sband;
383         int max_c, count_c = 0;
384         int i;
385
386         BUILD_BUG_ON(ARRAY_SIZE(sc->sbands) < IEEE80211_NUM_BANDS);
387         max_c = ARRAY_SIZE(sc->channels);
388
389         /* 2GHz band */
390         sband = &sc->sbands[IEEE80211_BAND_2GHZ];
391         sband->band = IEEE80211_BAND_2GHZ;
392         sband->bitrates = &sc->rates[IEEE80211_BAND_2GHZ][0];
393
394         if (test_bit(AR5K_MODE_11G, sc->ah->ah_capabilities.cap_mode)) {
395                 /* G mode */
396                 memcpy(sband->bitrates, &ath5k_rates[0],
397                        sizeof(struct ieee80211_rate) * 12);
398                 sband->n_bitrates = 12;
399
400                 sband->channels = sc->channels;
401                 sband->n_channels = ath5k_copy_channels(ah, sband->channels,
402                                         AR5K_MODE_11G, max_c);
403
404                 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = sband;
405                 count_c = sband->n_channels;
406                 max_c -= count_c;
407         } else if (test_bit(AR5K_MODE_11B, sc->ah->ah_capabilities.cap_mode)) {
408                 /* B mode */
409                 memcpy(sband->bitrates, &ath5k_rates[0],
410                        sizeof(struct ieee80211_rate) * 4);
411                 sband->n_bitrates = 4;
412
413                 /* 5211 only supports B rates and uses 4bit rate codes
414                  * (e.g normally we have 0x1B for 1M, but on 5211 we have 0x0B)
415                  * fix them up here:
416                  */
417                 if (ah->ah_version == AR5K_AR5211) {
418                         for (i = 0; i < 4; i++) {
419                                 sband->bitrates[i].hw_value =
420                                         sband->bitrates[i].hw_value & 0xF;
421                                 sband->bitrates[i].hw_value_short =
422                                         sband->bitrates[i].hw_value_short & 0xF;
423                         }
424                 }
425
426                 sband->channels = sc->channels;
427                 sband->n_channels = ath5k_copy_channels(ah, sband->channels,
428                                         AR5K_MODE_11B, max_c);
429
430                 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = sband;
431                 count_c = sband->n_channels;
432                 max_c -= count_c;
433         }
434         ath5k_setup_rate_idx(sc, sband);
435
436         /* 5GHz band, A mode */
437         if (test_bit(AR5K_MODE_11A, sc->ah->ah_capabilities.cap_mode)) {
438                 sband = &sc->sbands[IEEE80211_BAND_5GHZ];
439                 sband->band = IEEE80211_BAND_5GHZ;
440                 sband->bitrates = &sc->rates[IEEE80211_BAND_5GHZ][0];
441
442                 memcpy(sband->bitrates, &ath5k_rates[4],
443                        sizeof(struct ieee80211_rate) * 8);
444                 sband->n_bitrates = 8;
445
446                 sband->channels = &sc->channels[count_c];
447                 sband->n_channels = ath5k_copy_channels(ah, sband->channels,
448                                         AR5K_MODE_11A, max_c);
449
450                 hw->wiphy->bands[IEEE80211_BAND_5GHZ] = sband;
451         }
452         ath5k_setup_rate_idx(sc, sband);
453
454         ath5k_debug_dump_bands(sc);
455
456         return 0;
457 }
458
459 /*
460  * Set/change channels. We always reset the chip.
461  * To accomplish this we must first cleanup any pending DMA,
462  * then restart stuff after a la  ath5k_init.
463  *
464  * Called with sc->lock.
465  */
466 static int
467 ath5k_chan_set(struct ath5k_softc *sc, struct ieee80211_channel *chan)
468 {
469         ATH5K_DBG(sc, ATH5K_DEBUG_RESET,
470                   "channel set, resetting (%u -> %u MHz)\n",
471                   sc->curchan->center_freq, chan->center_freq);
472
473         /*
474          * To switch channels clear any pending DMA operations;
475          * wait long enough for the RX fifo to drain, reset the
476          * hardware at the new frequency, and then re-enable
477          * the relevant bits of the h/w.
478          */
479         return ath5k_reset(sc, chan, true);
480 }
481
482 static void
483 ath5k_setcurmode(struct ath5k_softc *sc, unsigned int mode)
484 {
485         sc->curmode = mode;
486
487         if (mode == AR5K_MODE_11A) {
488                 sc->curband = &sc->sbands[IEEE80211_BAND_5GHZ];
489         } else {
490                 sc->curband = &sc->sbands[IEEE80211_BAND_2GHZ];
491         }
492 }
493
494 struct ath_vif_iter_data {
495         const u8        *hw_macaddr;
496         u8              mask[ETH_ALEN];
497         u8              active_mac[ETH_ALEN]; /* first active MAC */
498         bool            need_set_hw_addr;
499         bool            found_active;
500         bool            any_assoc;
501         enum nl80211_iftype opmode;
502 };
503
504 static void ath_vif_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
505 {
506         struct ath_vif_iter_data *iter_data = data;
507         int i;
508         struct ath5k_vif *avf = (void *)vif->drv_priv;
509
510         if (iter_data->hw_macaddr)
511                 for (i = 0; i < ETH_ALEN; i++)
512                         iter_data->mask[i] &=
513                                 ~(iter_data->hw_macaddr[i] ^ mac[i]);
514
515         if (!iter_data->found_active) {
516                 iter_data->found_active = true;
517                 memcpy(iter_data->active_mac, mac, ETH_ALEN);
518         }
519
520         if (iter_data->need_set_hw_addr && iter_data->hw_macaddr)
521                 if (compare_ether_addr(iter_data->hw_macaddr, mac) == 0)
522                         iter_data->need_set_hw_addr = false;
523
524         if (!iter_data->any_assoc) {
525                 if (avf->assoc)
526                         iter_data->any_assoc = true;
527         }
528
529         /* Calculate combined mode - when APs are active, operate in AP mode.
530          * Otherwise use the mode of the new interface. This can currently
531          * only deal with combinations of APs and STAs. Only one ad-hoc
532          * interfaces is allowed.
533          */
534         if (avf->opmode == NL80211_IFTYPE_AP)
535                 iter_data->opmode = NL80211_IFTYPE_AP;
536         else
537                 if (iter_data->opmode == NL80211_IFTYPE_UNSPECIFIED)
538                         iter_data->opmode = avf->opmode;
539 }
540
541 static void ath5k_update_bssid_mask_and_opmode(struct ath5k_softc *sc,
542                                                struct ieee80211_vif *vif)
543 {
544         struct ath_common *common = ath5k_hw_common(sc->ah);
545         struct ath_vif_iter_data iter_data;
546
547         /*
548          * Use the hardware MAC address as reference, the hardware uses it
549          * together with the BSSID mask when matching addresses.
550          */
551         iter_data.hw_macaddr = common->macaddr;
552         memset(&iter_data.mask, 0xff, ETH_ALEN);
553         iter_data.found_active = false;
554         iter_data.need_set_hw_addr = true;
555         iter_data.opmode = NL80211_IFTYPE_UNSPECIFIED;
556
557         if (vif)
558                 ath_vif_iter(&iter_data, vif->addr, vif);
559
560         /* Get list of all active MAC addresses */
561         ieee80211_iterate_active_interfaces_atomic(sc->hw, ath_vif_iter,
562                                                    &iter_data);
563         memcpy(sc->bssidmask, iter_data.mask, ETH_ALEN);
564
565         sc->opmode = iter_data.opmode;
566         if (sc->opmode == NL80211_IFTYPE_UNSPECIFIED)
567                 /* Nothing active, default to station mode */
568                 sc->opmode = NL80211_IFTYPE_STATION;
569
570         ath5k_hw_set_opmode(sc->ah, sc->opmode);
571         ATH5K_DBG(sc, ATH5K_DEBUG_MODE, "mode setup opmode %d (%s)\n",
572                   sc->opmode, ath_opmode_to_string(sc->opmode));
573
574         if (iter_data.need_set_hw_addr && iter_data.found_active)
575                 ath5k_hw_set_lladdr(sc->ah, iter_data.active_mac);
576
577         if (ath5k_hw_hasbssidmask(sc->ah))
578                 ath5k_hw_set_bssid_mask(sc->ah, sc->bssidmask);
579 }
580
581 static void
582 ath5k_mode_setup(struct ath5k_softc *sc, struct ieee80211_vif *vif)
583 {
584         struct ath5k_hw *ah = sc->ah;
585         u32 rfilt;
586
587         /* configure rx filter */
588         rfilt = sc->filter_flags;
589         ath5k_hw_set_rx_filter(ah, rfilt);
590         ATH5K_DBG(sc, ATH5K_DEBUG_MODE, "RX filter 0x%x\n", rfilt);
591
592         ath5k_update_bssid_mask_and_opmode(sc, vif);
593 }
594
595 static inline int
596 ath5k_hw_to_driver_rix(struct ath5k_softc *sc, int hw_rix)
597 {
598         int rix;
599
600         /* return base rate on errors */
601         if (WARN(hw_rix < 0 || hw_rix >= AR5K_MAX_RATES,
602                         "hw_rix out of bounds: %x\n", hw_rix))
603                 return 0;
604
605         rix = sc->rate_idx[sc->curband->band][hw_rix];
606         if (WARN(rix < 0, "invalid hw_rix: %x\n", hw_rix))
607                 rix = 0;
608
609         return rix;
610 }
611
612 /***************\
613 * Buffers setup *
614 \***************/
615
616 static
617 struct sk_buff *ath5k_rx_skb_alloc(struct ath5k_softc *sc, dma_addr_t *skb_addr)
618 {
619         struct ath_common *common = ath5k_hw_common(sc->ah);
620         struct sk_buff *skb;
621
622         /*
623          * Allocate buffer with headroom_needed space for the
624          * fake physical layer header at the start.
625          */
626         skb = ath_rxbuf_alloc(common,
627                               common->rx_bufsize,
628                               GFP_ATOMIC);
629
630         if (!skb) {
631                 ATH5K_ERR(sc, "can't alloc skbuff of size %u\n",
632                                 common->rx_bufsize);
633                 return NULL;
634         }
635
636         *skb_addr = dma_map_single(sc->dev,
637                                    skb->data, common->rx_bufsize,
638                                    DMA_FROM_DEVICE);
639
640         if (unlikely(dma_mapping_error(sc->dev, *skb_addr))) {
641                 ATH5K_ERR(sc, "%s: DMA mapping failed\n", __func__);
642                 dev_kfree_skb(skb);
643                 return NULL;
644         }
645         return skb;
646 }
647
648 static int
649 ath5k_rxbuf_setup(struct ath5k_softc *sc, struct ath5k_buf *bf)
650 {
651         struct ath5k_hw *ah = sc->ah;
652         struct sk_buff *skb = bf->skb;
653         struct ath5k_desc *ds;
654         int ret;
655
656         if (!skb) {
657                 skb = ath5k_rx_skb_alloc(sc, &bf->skbaddr);
658                 if (!skb)
659                         return -ENOMEM;
660                 bf->skb = skb;
661         }
662
663         /*
664          * Setup descriptors.  For receive we always terminate
665          * the descriptor list with a self-linked entry so we'll
666          * not get overrun under high load (as can happen with a
667          * 5212 when ANI processing enables PHY error frames).
668          *
669          * To ensure the last descriptor is self-linked we create
670          * each descriptor as self-linked and add it to the end.  As
671          * each additional descriptor is added the previous self-linked
672          * entry is "fixed" naturally.  This should be safe even
673          * if DMA is happening.  When processing RX interrupts we
674          * never remove/process the last, self-linked, entry on the
675          * descriptor list.  This ensures the hardware always has
676          * someplace to write a new frame.
677          */
678         ds = bf->desc;
679         ds->ds_link = bf->daddr;        /* link to self */
680         ds->ds_data = bf->skbaddr;
681         ret = ath5k_hw_setup_rx_desc(ah, ds, ah->common.rx_bufsize, 0);
682         if (ret) {
683                 ATH5K_ERR(sc, "%s: could not setup RX desc\n", __func__);
684                 return ret;
685         }
686
687         if (sc->rxlink != NULL)
688                 *sc->rxlink = bf->daddr;
689         sc->rxlink = &ds->ds_link;
690         return 0;
691 }
692
693 static enum ath5k_pkt_type get_hw_packet_type(struct sk_buff *skb)
694 {
695         struct ieee80211_hdr *hdr;
696         enum ath5k_pkt_type htype;
697         __le16 fc;
698
699         hdr = (struct ieee80211_hdr *)skb->data;
700         fc = hdr->frame_control;
701
702         if (ieee80211_is_beacon(fc))
703                 htype = AR5K_PKT_TYPE_BEACON;
704         else if (ieee80211_is_probe_resp(fc))
705                 htype = AR5K_PKT_TYPE_PROBE_RESP;
706         else if (ieee80211_is_atim(fc))
707                 htype = AR5K_PKT_TYPE_ATIM;
708         else if (ieee80211_is_pspoll(fc))
709                 htype = AR5K_PKT_TYPE_PSPOLL;
710         else
711                 htype = AR5K_PKT_TYPE_NORMAL;
712
713         return htype;
714 }
715
716 static int
717 ath5k_txbuf_setup(struct ath5k_softc *sc, struct ath5k_buf *bf,
718                   struct ath5k_txq *txq, int padsize)
719 {
720         struct ath5k_hw *ah = sc->ah;
721         struct ath5k_desc *ds = bf->desc;
722         struct sk_buff *skb = bf->skb;
723         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
724         unsigned int pktlen, flags, keyidx = AR5K_TXKEYIX_INVALID;
725         struct ieee80211_rate *rate;
726         unsigned int mrr_rate[3], mrr_tries[3];
727         int i, ret;
728         u16 hw_rate;
729         u16 cts_rate = 0;
730         u16 duration = 0;
731         u8 rc_flags;
732
733         flags = AR5K_TXDESC_INTREQ | AR5K_TXDESC_CLRDMASK;
734
735         /* XXX endianness */
736         bf->skbaddr = dma_map_single(sc->dev, skb->data, skb->len,
737                         DMA_TO_DEVICE);
738
739         rate = ieee80211_get_tx_rate(sc->hw, info);
740         if (!rate) {
741                 ret = -EINVAL;
742                 goto err_unmap;
743         }
744
745         if (info->flags & IEEE80211_TX_CTL_NO_ACK)
746                 flags |= AR5K_TXDESC_NOACK;
747
748         rc_flags = info->control.rates[0].flags;
749         hw_rate = (rc_flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE) ?
750                 rate->hw_value_short : rate->hw_value;
751
752         pktlen = skb->len;
753
754         /* FIXME: If we are in g mode and rate is a CCK rate
755          * subtract ah->ah_txpower.txp_cck_ofdm_pwr_delta
756          * from tx power (value is in dB units already) */
757         if (info->control.hw_key) {
758                 keyidx = info->control.hw_key->hw_key_idx;
759                 pktlen += info->control.hw_key->icv_len;
760         }
761         if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
762                 flags |= AR5K_TXDESC_RTSENA;
763                 cts_rate = ieee80211_get_rts_cts_rate(sc->hw, info)->hw_value;
764                 duration = le16_to_cpu(ieee80211_rts_duration(sc->hw,
765                         info->control.vif, pktlen, info));
766         }
767         if (rc_flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
768                 flags |= AR5K_TXDESC_CTSENA;
769                 cts_rate = ieee80211_get_rts_cts_rate(sc->hw, info)->hw_value;
770                 duration = le16_to_cpu(ieee80211_ctstoself_duration(sc->hw,
771                         info->control.vif, pktlen, info));
772         }
773         ret = ah->ah_setup_tx_desc(ah, ds, pktlen,
774                 ieee80211_get_hdrlen_from_skb(skb), padsize,
775                 get_hw_packet_type(skb),
776                 (sc->power_level * 2),
777                 hw_rate,
778                 info->control.rates[0].count, keyidx, ah->ah_tx_ant, flags,
779                 cts_rate, duration);
780         if (ret)
781                 goto err_unmap;
782
783         memset(mrr_rate, 0, sizeof(mrr_rate));
784         memset(mrr_tries, 0, sizeof(mrr_tries));
785         for (i = 0; i < 3; i++) {
786                 rate = ieee80211_get_alt_retry_rate(sc->hw, info, i);
787                 if (!rate)
788                         break;
789
790                 mrr_rate[i] = rate->hw_value;
791                 mrr_tries[i] = info->control.rates[i + 1].count;
792         }
793
794         ath5k_hw_setup_mrr_tx_desc(ah, ds,
795                 mrr_rate[0], mrr_tries[0],
796                 mrr_rate[1], mrr_tries[1],
797                 mrr_rate[2], mrr_tries[2]);
798
799         ds->ds_link = 0;
800         ds->ds_data = bf->skbaddr;
801
802         spin_lock_bh(&txq->lock);
803         list_add_tail(&bf->list, &txq->q);
804         txq->txq_len++;
805         if (txq->link == NULL) /* is this first packet? */
806                 ath5k_hw_set_txdp(ah, txq->qnum, bf->daddr);
807         else /* no, so only link it */
808                 *txq->link = bf->daddr;
809
810         txq->link = &ds->ds_link;
811         ath5k_hw_start_tx_dma(ah, txq->qnum);
812         mmiowb();
813         spin_unlock_bh(&txq->lock);
814
815         return 0;
816 err_unmap:
817         dma_unmap_single(sc->dev, bf->skbaddr, skb->len, DMA_TO_DEVICE);
818         return ret;
819 }
820
821 /*******************\
822 * Descriptors setup *
823 \*******************/
824
825 static int
826 ath5k_desc_alloc(struct ath5k_softc *sc)
827 {
828         struct ath5k_desc *ds;
829         struct ath5k_buf *bf;
830         dma_addr_t da;
831         unsigned int i;
832         int ret;
833
834         /* allocate descriptors */
835         sc->desc_len = sizeof(struct ath5k_desc) *
836                         (ATH_TXBUF + ATH_RXBUF + ATH_BCBUF + 1);
837
838         sc->desc = dma_alloc_coherent(sc->dev, sc->desc_len,
839                                 &sc->desc_daddr, GFP_KERNEL);
840         if (sc->desc == NULL) {
841                 ATH5K_ERR(sc, "can't allocate descriptors\n");
842                 ret = -ENOMEM;
843                 goto err;
844         }
845         ds = sc->desc;
846         da = sc->desc_daddr;
847         ATH5K_DBG(sc, ATH5K_DEBUG_ANY, "DMA map: %p (%zu) -> %llx\n",
848                 ds, sc->desc_len, (unsigned long long)sc->desc_daddr);
849
850         bf = kcalloc(1 + ATH_TXBUF + ATH_RXBUF + ATH_BCBUF,
851                         sizeof(struct ath5k_buf), GFP_KERNEL);
852         if (bf == NULL) {
853                 ATH5K_ERR(sc, "can't allocate bufptr\n");
854                 ret = -ENOMEM;
855                 goto err_free;
856         }
857         sc->bufptr = bf;
858
859         INIT_LIST_HEAD(&sc->rxbuf);
860         for (i = 0; i < ATH_RXBUF; i++, bf++, ds++, da += sizeof(*ds)) {
861                 bf->desc = ds;
862                 bf->daddr = da;
863                 list_add_tail(&bf->list, &sc->rxbuf);
864         }
865
866         INIT_LIST_HEAD(&sc->txbuf);
867         sc->txbuf_len = ATH_TXBUF;
868         for (i = 0; i < ATH_TXBUF; i++, bf++, ds++,
869                         da += sizeof(*ds)) {
870                 bf->desc = ds;
871                 bf->daddr = da;
872                 list_add_tail(&bf->list, &sc->txbuf);
873         }
874
875         /* beacon buffers */
876         INIT_LIST_HEAD(&sc->bcbuf);
877         for (i = 0; i < ATH_BCBUF; i++, bf++, ds++, da += sizeof(*ds)) {
878                 bf->desc = ds;
879                 bf->daddr = da;
880                 list_add_tail(&bf->list, &sc->bcbuf);
881         }
882
883         return 0;
884 err_free:
885         dma_free_coherent(sc->dev, sc->desc_len, sc->desc, sc->desc_daddr);
886 err:
887         sc->desc = NULL;
888         return ret;
889 }
890
891 static void
892 ath5k_desc_free(struct ath5k_softc *sc)
893 {
894         struct ath5k_buf *bf;
895
896         list_for_each_entry(bf, &sc->txbuf, list)
897                 ath5k_txbuf_free_skb(sc, bf);
898         list_for_each_entry(bf, &sc->rxbuf, list)
899                 ath5k_rxbuf_free_skb(sc, bf);
900         list_for_each_entry(bf, &sc->bcbuf, list)
901                 ath5k_txbuf_free_skb(sc, bf);
902
903         /* Free memory associated with all descriptors */
904         dma_free_coherent(sc->dev, sc->desc_len, sc->desc, sc->desc_daddr);
905         sc->desc = NULL;
906         sc->desc_daddr = 0;
907
908         kfree(sc->bufptr);
909         sc->bufptr = NULL;
910 }
911
912
913 /**************\
914 * Queues setup *
915 \**************/
916
917 static struct ath5k_txq *
918 ath5k_txq_setup(struct ath5k_softc *sc,
919                 int qtype, int subtype)
920 {
921         struct ath5k_hw *ah = sc->ah;
922         struct ath5k_txq *txq;
923         struct ath5k_txq_info qi = {
924                 .tqi_subtype = subtype,
925                 /* XXX: default values not correct for B and XR channels,
926                  * but who cares? */
927                 .tqi_aifs = AR5K_TUNE_AIFS,
928                 .tqi_cw_min = AR5K_TUNE_CWMIN,
929                 .tqi_cw_max = AR5K_TUNE_CWMAX
930         };
931         int qnum;
932
933         /*
934          * Enable interrupts only for EOL and DESC conditions.
935          * We mark tx descriptors to receive a DESC interrupt
936          * when a tx queue gets deep; otherwise we wait for the
937          * EOL to reap descriptors.  Note that this is done to
938          * reduce interrupt load and this only defers reaping
939          * descriptors, never transmitting frames.  Aside from
940          * reducing interrupts this also permits more concurrency.
941          * The only potential downside is if the tx queue backs
942          * up in which case the top half of the kernel may backup
943          * due to a lack of tx descriptors.
944          */
945         qi.tqi_flags = AR5K_TXQ_FLAG_TXEOLINT_ENABLE |
946                                 AR5K_TXQ_FLAG_TXDESCINT_ENABLE;
947         qnum = ath5k_hw_setup_tx_queue(ah, qtype, &qi);
948         if (qnum < 0) {
949                 /*
950                  * NB: don't print a message, this happens
951                  * normally on parts with too few tx queues
952                  */
953                 return ERR_PTR(qnum);
954         }
955         if (qnum >= ARRAY_SIZE(sc->txqs)) {
956                 ATH5K_ERR(sc, "hw qnum %u out of range, max %tu!\n",
957                         qnum, ARRAY_SIZE(sc->txqs));
958                 ath5k_hw_release_tx_queue(ah, qnum);
959                 return ERR_PTR(-EINVAL);
960         }
961         txq = &sc->txqs[qnum];
962         if (!txq->setup) {
963                 txq->qnum = qnum;
964                 txq->link = NULL;
965                 INIT_LIST_HEAD(&txq->q);
966                 spin_lock_init(&txq->lock);
967                 txq->setup = true;
968                 txq->txq_len = 0;
969                 txq->txq_poll_mark = false;
970                 txq->txq_stuck = 0;
971         }
972         return &sc->txqs[qnum];
973 }
974
975 static int
976 ath5k_beaconq_setup(struct ath5k_hw *ah)
977 {
978         struct ath5k_txq_info qi = {
979                 /* XXX: default values not correct for B and XR channels,
980                  * but who cares? */
981                 .tqi_aifs = AR5K_TUNE_AIFS,
982                 .tqi_cw_min = AR5K_TUNE_CWMIN,
983                 .tqi_cw_max = AR5K_TUNE_CWMAX,
984                 /* NB: for dynamic turbo, don't enable any other interrupts */
985                 .tqi_flags = AR5K_TXQ_FLAG_TXDESCINT_ENABLE
986         };
987
988         return ath5k_hw_setup_tx_queue(ah, AR5K_TX_QUEUE_BEACON, &qi);
989 }
990
991 static int
992 ath5k_beaconq_config(struct ath5k_softc *sc)
993 {
994         struct ath5k_hw *ah = sc->ah;
995         struct ath5k_txq_info qi;
996         int ret;
997
998         ret = ath5k_hw_get_tx_queueprops(ah, sc->bhalq, &qi);
999         if (ret)
1000                 goto err;
1001
1002         if (sc->opmode == NL80211_IFTYPE_AP ||
1003                 sc->opmode == NL80211_IFTYPE_MESH_POINT) {
1004                 /*
1005                  * Always burst out beacon and CAB traffic
1006                  * (aifs = cwmin = cwmax = 0)
1007                  */
1008                 qi.tqi_aifs = 0;
1009                 qi.tqi_cw_min = 0;
1010                 qi.tqi_cw_max = 0;
1011         } else if (sc->opmode == NL80211_IFTYPE_ADHOC) {
1012                 /*
1013                  * Adhoc mode; backoff between 0 and (2 * cw_min).
1014                  */
1015                 qi.tqi_aifs = 0;
1016                 qi.tqi_cw_min = 0;
1017                 qi.tqi_cw_max = 2 * AR5K_TUNE_CWMIN;
1018         }
1019
1020         ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
1021                 "beacon queueprops tqi_aifs:%d tqi_cw_min:%d tqi_cw_max:%d\n",
1022                 qi.tqi_aifs, qi.tqi_cw_min, qi.tqi_cw_max);
1023
1024         ret = ath5k_hw_set_tx_queueprops(ah, sc->bhalq, &qi);
1025         if (ret) {
1026                 ATH5K_ERR(sc, "%s: unable to update parameters for beacon "
1027                         "hardware queue!\n", __func__);
1028                 goto err;
1029         }
1030         ret = ath5k_hw_reset_tx_queue(ah, sc->bhalq); /* push to h/w */
1031         if (ret)
1032                 goto err;
1033
1034         /* reconfigure cabq with ready time to 80% of beacon_interval */
1035         ret = ath5k_hw_get_tx_queueprops(ah, AR5K_TX_QUEUE_ID_CAB, &qi);
1036         if (ret)
1037                 goto err;
1038
1039         qi.tqi_ready_time = (sc->bintval * 80) / 100;
1040         ret = ath5k_hw_set_tx_queueprops(ah, AR5K_TX_QUEUE_ID_CAB, &qi);
1041         if (ret)
1042                 goto err;
1043
1044         ret = ath5k_hw_reset_tx_queue(ah, AR5K_TX_QUEUE_ID_CAB);
1045 err:
1046         return ret;
1047 }
1048
1049 /**
1050  * ath5k_drain_tx_buffs - Empty tx buffers
1051  *
1052  * @sc The &struct ath5k_softc
1053  *
1054  * Empty tx buffers from all queues in preparation
1055  * of a reset or during shutdown.
1056  *
1057  * NB:  this assumes output has been stopped and
1058  *      we do not need to block ath5k_tx_tasklet
1059  */
1060 static void
1061 ath5k_drain_tx_buffs(struct ath5k_softc *sc)
1062 {
1063         struct ath5k_txq *txq;
1064         struct ath5k_buf *bf, *bf0;
1065         int i;
1066
1067         for (i = 0; i < ARRAY_SIZE(sc->txqs); i++) {
1068                 if (sc->txqs[i].setup) {
1069                         txq = &sc->txqs[i];
1070                         spin_lock_bh(&txq->lock);
1071                         list_for_each_entry_safe(bf, bf0, &txq->q, list) {
1072                                 ath5k_debug_printtxbuf(sc, bf);
1073
1074                                 ath5k_txbuf_free_skb(sc, bf);
1075
1076                                 spin_lock_bh(&sc->txbuflock);
1077                                 list_move_tail(&bf->list, &sc->txbuf);
1078                                 sc->txbuf_len++;
1079                                 txq->txq_len--;
1080                                 spin_unlock_bh(&sc->txbuflock);
1081                         }
1082                         txq->link = NULL;
1083                         txq->txq_poll_mark = false;
1084                         spin_unlock_bh(&txq->lock);
1085                 }
1086         }
1087 }
1088
1089 static void
1090 ath5k_txq_release(struct ath5k_softc *sc)
1091 {
1092         struct ath5k_txq *txq = sc->txqs;
1093         unsigned int i;
1094
1095         for (i = 0; i < ARRAY_SIZE(sc->txqs); i++, txq++)
1096                 if (txq->setup) {
1097                         ath5k_hw_release_tx_queue(sc->ah, txq->qnum);
1098                         txq->setup = false;
1099                 }
1100 }
1101
1102
1103 /*************\
1104 * RX Handling *
1105 \*************/
1106
1107 /*
1108  * Enable the receive h/w following a reset.
1109  */
1110 static int
1111 ath5k_rx_start(struct ath5k_softc *sc)
1112 {
1113         struct ath5k_hw *ah = sc->ah;
1114         struct ath_common *common = ath5k_hw_common(ah);
1115         struct ath5k_buf *bf;
1116         int ret;
1117
1118         common->rx_bufsize = roundup(IEEE80211_MAX_FRAME_LEN, common->cachelsz);
1119
1120         ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "cachelsz %u rx_bufsize %u\n",
1121                   common->cachelsz, common->rx_bufsize);
1122
1123         spin_lock_bh(&sc->rxbuflock);
1124         sc->rxlink = NULL;
1125         list_for_each_entry(bf, &sc->rxbuf, list) {
1126                 ret = ath5k_rxbuf_setup(sc, bf);
1127                 if (ret != 0) {
1128                         spin_unlock_bh(&sc->rxbuflock);
1129                         goto err;
1130                 }
1131         }
1132         bf = list_first_entry(&sc->rxbuf, struct ath5k_buf, list);
1133         ath5k_hw_set_rxdp(ah, bf->daddr);
1134         spin_unlock_bh(&sc->rxbuflock);
1135
1136         ath5k_hw_start_rx_dma(ah);      /* enable recv descriptors */
1137         ath5k_mode_setup(sc, NULL);             /* set filters, etc. */
1138         ath5k_hw_start_rx_pcu(ah);      /* re-enable PCU/DMA engine */
1139
1140         return 0;
1141 err:
1142         return ret;
1143 }
1144
1145 /*
1146  * Disable the receive logic on PCU (DRU)
1147  * In preparation for a shutdown.
1148  *
1149  * Note: Doesn't stop rx DMA, ath5k_hw_dma_stop
1150  * does.
1151  */
1152 static void
1153 ath5k_rx_stop(struct ath5k_softc *sc)
1154 {
1155         struct ath5k_hw *ah = sc->ah;
1156
1157         ath5k_hw_set_rx_filter(ah, 0);  /* clear recv filter */
1158         ath5k_hw_stop_rx_pcu(ah);       /* disable PCU */
1159
1160         ath5k_debug_printrxbuffs(sc, ah);
1161 }
1162
1163 static unsigned int
1164 ath5k_rx_decrypted(struct ath5k_softc *sc, struct sk_buff *skb,
1165                    struct ath5k_rx_status *rs)
1166 {
1167         struct ath5k_hw *ah = sc->ah;
1168         struct ath_common *common = ath5k_hw_common(ah);
1169         struct ieee80211_hdr *hdr = (void *)skb->data;
1170         unsigned int keyix, hlen;
1171
1172         if (!(rs->rs_status & AR5K_RXERR_DECRYPT) &&
1173                         rs->rs_keyix != AR5K_RXKEYIX_INVALID)
1174                 return RX_FLAG_DECRYPTED;
1175
1176         /* Apparently when a default key is used to decrypt the packet
1177            the hw does not set the index used to decrypt.  In such cases
1178            get the index from the packet. */
1179         hlen = ieee80211_hdrlen(hdr->frame_control);
1180         if (ieee80211_has_protected(hdr->frame_control) &&
1181             !(rs->rs_status & AR5K_RXERR_DECRYPT) &&
1182             skb->len >= hlen + 4) {
1183                 keyix = skb->data[hlen + 3] >> 6;
1184
1185                 if (test_bit(keyix, common->keymap))
1186                         return RX_FLAG_DECRYPTED;
1187         }
1188
1189         return 0;
1190 }
1191
1192
1193 static void
1194 ath5k_check_ibss_tsf(struct ath5k_softc *sc, struct sk_buff *skb,
1195                      struct ieee80211_rx_status *rxs)
1196 {
1197         struct ath_common *common = ath5k_hw_common(sc->ah);
1198         u64 tsf, bc_tstamp;
1199         u32 hw_tu;
1200         struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)skb->data;
1201
1202         if (ieee80211_is_beacon(mgmt->frame_control) &&
1203             le16_to_cpu(mgmt->u.beacon.capab_info) & WLAN_CAPABILITY_IBSS &&
1204             memcmp(mgmt->bssid, common->curbssid, ETH_ALEN) == 0) {
1205                 /*
1206                  * Received an IBSS beacon with the same BSSID. Hardware *must*
1207                  * have updated the local TSF. We have to work around various
1208                  * hardware bugs, though...
1209                  */
1210                 tsf = ath5k_hw_get_tsf64(sc->ah);
1211                 bc_tstamp = le64_to_cpu(mgmt->u.beacon.timestamp);
1212                 hw_tu = TSF_TO_TU(tsf);
1213
1214                 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
1215                         "beacon %llx mactime %llx (diff %lld) tsf now %llx\n",
1216                         (unsigned long long)bc_tstamp,
1217                         (unsigned long long)rxs->mactime,
1218                         (unsigned long long)(rxs->mactime - bc_tstamp),
1219                         (unsigned long long)tsf);
1220
1221                 /*
1222                  * Sometimes the HW will give us a wrong tstamp in the rx
1223                  * status, causing the timestamp extension to go wrong.
1224                  * (This seems to happen especially with beacon frames bigger
1225                  * than 78 byte (incl. FCS))
1226                  * But we know that the receive timestamp must be later than the
1227                  * timestamp of the beacon since HW must have synced to that.
1228                  *
1229                  * NOTE: here we assume mactime to be after the frame was
1230                  * received, not like mac80211 which defines it at the start.
1231                  */
1232                 if (bc_tstamp > rxs->mactime) {
1233                         ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
1234                                 "fixing mactime from %llx to %llx\n",
1235                                 (unsigned long long)rxs->mactime,
1236                                 (unsigned long long)tsf);
1237                         rxs->mactime = tsf;
1238                 }
1239
1240                 /*
1241                  * Local TSF might have moved higher than our beacon timers,
1242                  * in that case we have to update them to continue sending
1243                  * beacons. This also takes care of synchronizing beacon sending
1244                  * times with other stations.
1245                  */
1246                 if (hw_tu >= sc->nexttbtt)
1247                         ath5k_beacon_update_timers(sc, bc_tstamp);
1248
1249                 /* Check if the beacon timers are still correct, because a TSF
1250                  * update might have created a window between them - for a
1251                  * longer description see the comment of this function: */
1252                 if (!ath5k_hw_check_beacon_timers(sc->ah, sc->bintval)) {
1253                         ath5k_beacon_update_timers(sc, bc_tstamp);
1254                         ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
1255                                 "fixed beacon timers after beacon receive\n");
1256                 }
1257         }
1258 }
1259
1260 static void
1261 ath5k_update_beacon_rssi(struct ath5k_softc *sc, struct sk_buff *skb, int rssi)
1262 {
1263         struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)skb->data;
1264         struct ath5k_hw *ah = sc->ah;
1265         struct ath_common *common = ath5k_hw_common(ah);
1266
1267         /* only beacons from our BSSID */
1268         if (!ieee80211_is_beacon(mgmt->frame_control) ||
1269             memcmp(mgmt->bssid, common->curbssid, ETH_ALEN) != 0)
1270                 return;
1271
1272         ewma_add(&ah->ah_beacon_rssi_avg, rssi);
1273
1274         /* in IBSS mode we should keep RSSI statistics per neighbour */
1275         /* le16_to_cpu(mgmt->u.beacon.capab_info) & WLAN_CAPABILITY_IBSS */
1276 }
1277
1278 /*
1279  * Compute padding position. skb must contain an IEEE 802.11 frame
1280  */
1281 static int ath5k_common_padpos(struct sk_buff *skb)
1282 {
1283         struct ieee80211_hdr * hdr = (struct ieee80211_hdr *)skb->data;
1284         __le16 frame_control = hdr->frame_control;
1285         int padpos = 24;
1286
1287         if (ieee80211_has_a4(frame_control)) {
1288                 padpos += ETH_ALEN;
1289         }
1290         if (ieee80211_is_data_qos(frame_control)) {
1291                 padpos += IEEE80211_QOS_CTL_LEN;
1292         }
1293
1294         return padpos;
1295 }
1296
1297 /*
1298  * This function expects an 802.11 frame and returns the number of
1299  * bytes added, or -1 if we don't have enough header room.
1300  */
1301 static int ath5k_add_padding(struct sk_buff *skb)
1302 {
1303         int padpos = ath5k_common_padpos(skb);
1304         int padsize = padpos & 3;
1305
1306         if (padsize && skb->len>padpos) {
1307
1308                 if (skb_headroom(skb) < padsize)
1309                         return -1;
1310
1311                 skb_push(skb, padsize);
1312                 memmove(skb->data, skb->data+padsize, padpos);
1313                 return padsize;
1314         }
1315
1316         return 0;
1317 }
1318
1319 /*
1320  * The MAC header is padded to have 32-bit boundary if the
1321  * packet payload is non-zero. The general calculation for
1322  * padsize would take into account odd header lengths:
1323  * padsize = 4 - (hdrlen & 3); however, since only
1324  * even-length headers are used, padding can only be 0 or 2
1325  * bytes and we can optimize this a bit.  We must not try to
1326  * remove padding from short control frames that do not have a
1327  * payload.
1328  *
1329  * This function expects an 802.11 frame and returns the number of
1330  * bytes removed.
1331  */
1332 static int ath5k_remove_padding(struct sk_buff *skb)
1333 {
1334         int padpos = ath5k_common_padpos(skb);
1335         int padsize = padpos & 3;
1336
1337         if (padsize && skb->len>=padpos+padsize) {
1338                 memmove(skb->data + padsize, skb->data, padpos);
1339                 skb_pull(skb, padsize);
1340                 return padsize;
1341         }
1342
1343         return 0;
1344 }
1345
1346 static void
1347 ath5k_receive_frame(struct ath5k_softc *sc, struct sk_buff *skb,
1348                     struct ath5k_rx_status *rs)
1349 {
1350         struct ieee80211_rx_status *rxs;
1351
1352         ath5k_remove_padding(skb);
1353
1354         rxs = IEEE80211_SKB_RXCB(skb);
1355
1356         rxs->flag = 0;
1357         if (unlikely(rs->rs_status & AR5K_RXERR_MIC))
1358                 rxs->flag |= RX_FLAG_MMIC_ERROR;
1359
1360         /*
1361          * always extend the mac timestamp, since this information is
1362          * also needed for proper IBSS merging.
1363          *
1364          * XXX: it might be too late to do it here, since rs_tstamp is
1365          * 15bit only. that means TSF extension has to be done within
1366          * 32768usec (about 32ms). it might be necessary to move this to
1367          * the interrupt handler, like it is done in madwifi.
1368          *
1369          * Unfortunately we don't know when the hardware takes the rx
1370          * timestamp (beginning of phy frame, data frame, end of rx?).
1371          * The only thing we know is that it is hardware specific...
1372          * On AR5213 it seems the rx timestamp is at the end of the
1373          * frame, but i'm not sure.
1374          *
1375          * NOTE: mac80211 defines mactime at the beginning of the first
1376          * data symbol. Since we don't have any time references it's
1377          * impossible to comply to that. This affects IBSS merge only
1378          * right now, so it's not too bad...
1379          */
1380         rxs->mactime = ath5k_extend_tsf(sc->ah, rs->rs_tstamp);
1381         rxs->flag |= RX_FLAG_TSFT;
1382
1383         rxs->freq = sc->curchan->center_freq;
1384         rxs->band = sc->curband->band;
1385
1386         rxs->signal = sc->ah->ah_noise_floor + rs->rs_rssi;
1387
1388         rxs->antenna = rs->rs_antenna;
1389
1390         if (rs->rs_antenna > 0 && rs->rs_antenna < 5)
1391                 sc->stats.antenna_rx[rs->rs_antenna]++;
1392         else
1393                 sc->stats.antenna_rx[0]++; /* invalid */
1394
1395         rxs->rate_idx = ath5k_hw_to_driver_rix(sc, rs->rs_rate);
1396         rxs->flag |= ath5k_rx_decrypted(sc, skb, rs);
1397
1398         if (rxs->rate_idx >= 0 && rs->rs_rate ==
1399             sc->curband->bitrates[rxs->rate_idx].hw_value_short)
1400                 rxs->flag |= RX_FLAG_SHORTPRE;
1401
1402         ath5k_debug_dump_skb(sc, skb, "RX  ", 0);
1403
1404         ath5k_update_beacon_rssi(sc, skb, rs->rs_rssi);
1405
1406         /* check beacons in IBSS mode */
1407         if (sc->opmode == NL80211_IFTYPE_ADHOC)
1408                 ath5k_check_ibss_tsf(sc, skb, rxs);
1409
1410         ieee80211_rx(sc->hw, skb);
1411 }
1412
1413 /** ath5k_frame_receive_ok() - Do we want to receive this frame or not?
1414  *
1415  * Check if we want to further process this frame or not. Also update
1416  * statistics. Return true if we want this frame, false if not.
1417  */
1418 static bool
1419 ath5k_receive_frame_ok(struct ath5k_softc *sc, struct ath5k_rx_status *rs)
1420 {
1421         sc->stats.rx_all_count++;
1422         sc->stats.rx_bytes_count += rs->rs_datalen;
1423
1424         if (unlikely(rs->rs_status)) {
1425                 if (rs->rs_status & AR5K_RXERR_CRC)
1426                         sc->stats.rxerr_crc++;
1427                 if (rs->rs_status & AR5K_RXERR_FIFO)
1428                         sc->stats.rxerr_fifo++;
1429                 if (rs->rs_status & AR5K_RXERR_PHY) {
1430                         sc->stats.rxerr_phy++;
1431                         if (rs->rs_phyerr > 0 && rs->rs_phyerr < 32)
1432                                 sc->stats.rxerr_phy_code[rs->rs_phyerr]++;
1433                         return false;
1434                 }
1435                 if (rs->rs_status & AR5K_RXERR_DECRYPT) {
1436                         /*
1437                          * Decrypt error.  If the error occurred
1438                          * because there was no hardware key, then
1439                          * let the frame through so the upper layers
1440                          * can process it.  This is necessary for 5210
1441                          * parts which have no way to setup a ``clear''
1442                          * key cache entry.
1443                          *
1444                          * XXX do key cache faulting
1445                          */
1446                         sc->stats.rxerr_decrypt++;
1447                         if (rs->rs_keyix == AR5K_RXKEYIX_INVALID &&
1448                             !(rs->rs_status & AR5K_RXERR_CRC))
1449                                 return true;
1450                 }
1451                 if (rs->rs_status & AR5K_RXERR_MIC) {
1452                         sc->stats.rxerr_mic++;
1453                         return true;
1454                 }
1455
1456                 /* reject any frames with non-crypto errors */
1457                 if (rs->rs_status & ~(AR5K_RXERR_DECRYPT))
1458                         return false;
1459         }
1460
1461         if (unlikely(rs->rs_more)) {
1462                 sc->stats.rxerr_jumbo++;
1463                 return false;
1464         }
1465         return true;
1466 }
1467
1468 static void
1469 ath5k_tasklet_rx(unsigned long data)
1470 {
1471         struct ath5k_rx_status rs = {};
1472         struct sk_buff *skb, *next_skb;
1473         dma_addr_t next_skb_addr;
1474         struct ath5k_softc *sc = (void *)data;
1475         struct ath5k_hw *ah = sc->ah;
1476         struct ath_common *common = ath5k_hw_common(ah);
1477         struct ath5k_buf *bf;
1478         struct ath5k_desc *ds;
1479         int ret;
1480
1481         spin_lock(&sc->rxbuflock);
1482         if (list_empty(&sc->rxbuf)) {
1483                 ATH5K_WARN(sc, "empty rx buf pool\n");
1484                 goto unlock;
1485         }
1486         do {
1487                 bf = list_first_entry(&sc->rxbuf, struct ath5k_buf, list);
1488                 BUG_ON(bf->skb == NULL);
1489                 skb = bf->skb;
1490                 ds = bf->desc;
1491
1492                 /* bail if HW is still using self-linked descriptor */
1493                 if (ath5k_hw_get_rxdp(sc->ah) == bf->daddr)
1494                         break;
1495
1496                 ret = sc->ah->ah_proc_rx_desc(sc->ah, ds, &rs);
1497                 if (unlikely(ret == -EINPROGRESS))
1498                         break;
1499                 else if (unlikely(ret)) {
1500                         ATH5K_ERR(sc, "error in processing rx descriptor\n");
1501                         sc->stats.rxerr_proc++;
1502                         break;
1503                 }
1504
1505                 if (ath5k_receive_frame_ok(sc, &rs)) {
1506                         next_skb = ath5k_rx_skb_alloc(sc, &next_skb_addr);
1507
1508                         /*
1509                          * If we can't replace bf->skb with a new skb under
1510                          * memory pressure, just skip this packet
1511                          */
1512                         if (!next_skb)
1513                                 goto next;
1514
1515                         dma_unmap_single(sc->dev, bf->skbaddr,
1516                                          common->rx_bufsize,
1517                                          DMA_FROM_DEVICE);
1518
1519                         skb_put(skb, rs.rs_datalen);
1520
1521                         ath5k_receive_frame(sc, skb, &rs);
1522
1523                         bf->skb = next_skb;
1524                         bf->skbaddr = next_skb_addr;
1525                 }
1526 next:
1527                 list_move_tail(&bf->list, &sc->rxbuf);
1528         } while (ath5k_rxbuf_setup(sc, bf) == 0);
1529 unlock:
1530         spin_unlock(&sc->rxbuflock);
1531 }
1532
1533
1534 /*************\
1535 * TX Handling *
1536 \*************/
1537
1538 static int ath5k_tx_queue(struct ieee80211_hw *hw, struct sk_buff *skb,
1539                           struct ath5k_txq *txq)
1540 {
1541         struct ath5k_softc *sc = hw->priv;
1542         struct ath5k_buf *bf;
1543         unsigned long flags;
1544         int padsize;
1545
1546         ath5k_debug_dump_skb(sc, skb, "TX  ", 1);
1547
1548         /*
1549          * The hardware expects the header padded to 4 byte boundaries.
1550          * If this is not the case, we add the padding after the header.
1551          */
1552         padsize = ath5k_add_padding(skb);
1553         if (padsize < 0) {
1554                 ATH5K_ERR(sc, "tx hdrlen not %%4: not enough"
1555                           " headroom to pad");
1556                 goto drop_packet;
1557         }
1558
1559         if (txq->txq_len >= ATH5K_TXQ_LEN_MAX)
1560                 ieee80211_stop_queue(hw, txq->qnum);
1561
1562         spin_lock_irqsave(&sc->txbuflock, flags);
1563         if (list_empty(&sc->txbuf)) {
1564                 ATH5K_ERR(sc, "no further txbuf available, dropping packet\n");
1565                 spin_unlock_irqrestore(&sc->txbuflock, flags);
1566                 ieee80211_stop_queues(hw);
1567                 goto drop_packet;
1568         }
1569         bf = list_first_entry(&sc->txbuf, struct ath5k_buf, list);
1570         list_del(&bf->list);
1571         sc->txbuf_len--;
1572         if (list_empty(&sc->txbuf))
1573                 ieee80211_stop_queues(hw);
1574         spin_unlock_irqrestore(&sc->txbuflock, flags);
1575
1576         bf->skb = skb;
1577
1578         if (ath5k_txbuf_setup(sc, bf, txq, padsize)) {
1579                 bf->skb = NULL;
1580                 spin_lock_irqsave(&sc->txbuflock, flags);
1581                 list_add_tail(&bf->list, &sc->txbuf);
1582                 sc->txbuf_len++;
1583                 spin_unlock_irqrestore(&sc->txbuflock, flags);
1584                 goto drop_packet;
1585         }
1586         return NETDEV_TX_OK;
1587
1588 drop_packet:
1589         dev_kfree_skb_any(skb);
1590         return NETDEV_TX_OK;
1591 }
1592
1593 static void
1594 ath5k_tx_frame_completed(struct ath5k_softc *sc, struct sk_buff *skb,
1595                          struct ath5k_tx_status *ts)
1596 {
1597         struct ieee80211_tx_info *info;
1598         int i;
1599
1600         sc->stats.tx_all_count++;
1601         sc->stats.tx_bytes_count += skb->len;
1602         info = IEEE80211_SKB_CB(skb);
1603
1604         ieee80211_tx_info_clear_status(info);
1605         for (i = 0; i < 4; i++) {
1606                 struct ieee80211_tx_rate *r =
1607                         &info->status.rates[i];
1608
1609                 if (ts->ts_rate[i]) {
1610                         r->idx = ath5k_hw_to_driver_rix(sc, ts->ts_rate[i]);
1611                         r->count = ts->ts_retry[i];
1612                 } else {
1613                         r->idx = -1;
1614                         r->count = 0;
1615                 }
1616         }
1617
1618         /* count the successful attempt as well */
1619         info->status.rates[ts->ts_final_idx].count++;
1620
1621         if (unlikely(ts->ts_status)) {
1622                 sc->stats.ack_fail++;
1623                 if (ts->ts_status & AR5K_TXERR_FILT) {
1624                         info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
1625                         sc->stats.txerr_filt++;
1626                 }
1627                 if (ts->ts_status & AR5K_TXERR_XRETRY)
1628                         sc->stats.txerr_retry++;
1629                 if (ts->ts_status & AR5K_TXERR_FIFO)
1630                         sc->stats.txerr_fifo++;
1631         } else {
1632                 info->flags |= IEEE80211_TX_STAT_ACK;
1633                 info->status.ack_signal = ts->ts_rssi;
1634         }
1635
1636         /*
1637         * Remove MAC header padding before giving the frame
1638         * back to mac80211.
1639         */
1640         ath5k_remove_padding(skb);
1641
1642         if (ts->ts_antenna > 0 && ts->ts_antenna < 5)
1643                 sc->stats.antenna_tx[ts->ts_antenna]++;
1644         else
1645                 sc->stats.antenna_tx[0]++; /* invalid */
1646
1647         ieee80211_tx_status(sc->hw, skb);
1648 }
1649
1650 static void
1651 ath5k_tx_processq(struct ath5k_softc *sc, struct ath5k_txq *txq)
1652 {
1653         struct ath5k_tx_status ts = {};
1654         struct ath5k_buf *bf, *bf0;
1655         struct ath5k_desc *ds;
1656         struct sk_buff *skb;
1657         int ret;
1658
1659         spin_lock(&txq->lock);
1660         list_for_each_entry_safe(bf, bf0, &txq->q, list) {
1661
1662                 txq->txq_poll_mark = false;
1663
1664                 /* skb might already have been processed last time. */
1665                 if (bf->skb != NULL) {
1666                         ds = bf->desc;
1667
1668                         ret = sc->ah->ah_proc_tx_desc(sc->ah, ds, &ts);
1669                         if (unlikely(ret == -EINPROGRESS))
1670                                 break;
1671                         else if (unlikely(ret)) {
1672                                 ATH5K_ERR(sc,
1673                                         "error %d while processing "
1674                                         "queue %u\n", ret, txq->qnum);
1675                                 break;
1676                         }
1677
1678                         skb = bf->skb;
1679                         bf->skb = NULL;
1680
1681                         dma_unmap_single(sc->dev, bf->skbaddr, skb->len,
1682                                         DMA_TO_DEVICE);
1683                         ath5k_tx_frame_completed(sc, skb, &ts);
1684                 }
1685
1686                 /*
1687                  * It's possible that the hardware can say the buffer is
1688                  * completed when it hasn't yet loaded the ds_link from
1689                  * host memory and moved on.
1690                  * Always keep the last descriptor to avoid HW races...
1691                  */
1692                 if (ath5k_hw_get_txdp(sc->ah, txq->qnum) != bf->daddr) {
1693                         spin_lock(&sc->txbuflock);
1694                         list_move_tail(&bf->list, &sc->txbuf);
1695                         sc->txbuf_len++;
1696                         txq->txq_len--;
1697                         spin_unlock(&sc->txbuflock);
1698                 }
1699         }
1700         spin_unlock(&txq->lock);
1701         if (txq->txq_len < ATH5K_TXQ_LEN_LOW && txq->qnum < 4)
1702                 ieee80211_wake_queue(sc->hw, txq->qnum);
1703 }
1704
1705 static void
1706 ath5k_tasklet_tx(unsigned long data)
1707 {
1708         int i;
1709         struct ath5k_softc *sc = (void *)data;
1710
1711         for (i=0; i < AR5K_NUM_TX_QUEUES; i++)
1712                 if (sc->txqs[i].setup && (sc->ah->ah_txq_isr & BIT(i)))
1713                         ath5k_tx_processq(sc, &sc->txqs[i]);
1714 }
1715
1716
1717 /*****************\
1718 * Beacon handling *
1719 \*****************/
1720
1721 /*
1722  * Setup the beacon frame for transmit.
1723  */
1724 static int
1725 ath5k_beacon_setup(struct ath5k_softc *sc, struct ath5k_buf *bf)
1726 {
1727         struct sk_buff *skb = bf->skb;
1728         struct  ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1729         struct ath5k_hw *ah = sc->ah;
1730         struct ath5k_desc *ds;
1731         int ret = 0;
1732         u8 antenna;
1733         u32 flags;
1734         const int padsize = 0;
1735
1736         bf->skbaddr = dma_map_single(sc->dev, skb->data, skb->len,
1737                         DMA_TO_DEVICE);
1738         ATH5K_DBG(sc, ATH5K_DEBUG_BEACON, "skb %p [data %p len %u] "
1739                         "skbaddr %llx\n", skb, skb->data, skb->len,
1740                         (unsigned long long)bf->skbaddr);
1741
1742         if (dma_mapping_error(sc->dev, bf->skbaddr)) {
1743                 ATH5K_ERR(sc, "beacon DMA mapping failed\n");
1744                 return -EIO;
1745         }
1746
1747         ds = bf->desc;
1748         antenna = ah->ah_tx_ant;
1749
1750         flags = AR5K_TXDESC_NOACK;
1751         if (sc->opmode == NL80211_IFTYPE_ADHOC && ath5k_hw_hasveol(ah)) {
1752                 ds->ds_link = bf->daddr;        /* self-linked */
1753                 flags |= AR5K_TXDESC_VEOL;
1754         } else
1755                 ds->ds_link = 0;
1756
1757         /*
1758          * If we use multiple antennas on AP and use
1759          * the Sectored AP scenario, switch antenna every
1760          * 4 beacons to make sure everybody hears our AP.
1761          * When a client tries to associate, hw will keep
1762          * track of the tx antenna to be used for this client
1763          * automaticaly, based on ACKed packets.
1764          *
1765          * Note: AP still listens and transmits RTS on the
1766          * default antenna which is supposed to be an omni.
1767          *
1768          * Note2: On sectored scenarios it's possible to have
1769          * multiple antennas (1 omni -- the default -- and 14
1770          * sectors), so if we choose to actually support this
1771          * mode, we need to allow the user to set how many antennas
1772          * we have and tweak the code below to send beacons
1773          * on all of them.
1774          */
1775         if (ah->ah_ant_mode == AR5K_ANTMODE_SECTOR_AP)
1776                 antenna = sc->bsent & 4 ? 2 : 1;
1777
1778
1779         /* FIXME: If we are in g mode and rate is a CCK rate
1780          * subtract ah->ah_txpower.txp_cck_ofdm_pwr_delta
1781          * from tx power (value is in dB units already) */
1782         ds->ds_data = bf->skbaddr;
1783         ret = ah->ah_setup_tx_desc(ah, ds, skb->len,
1784                         ieee80211_get_hdrlen_from_skb(skb), padsize,
1785                         AR5K_PKT_TYPE_BEACON, (sc->power_level * 2),
1786                         ieee80211_get_tx_rate(sc->hw, info)->hw_value,
1787                         1, AR5K_TXKEYIX_INVALID,
1788                         antenna, flags, 0, 0);
1789         if (ret)
1790                 goto err_unmap;
1791
1792         return 0;
1793 err_unmap:
1794         dma_unmap_single(sc->dev, bf->skbaddr, skb->len, DMA_TO_DEVICE);
1795         return ret;
1796 }
1797
1798 /*
1799  * Updates the beacon that is sent by ath5k_beacon_send.  For adhoc,
1800  * this is called only once at config_bss time, for AP we do it every
1801  * SWBA interrupt so that the TIM will reflect buffered frames.
1802  *
1803  * Called with the beacon lock.
1804  */
1805 static int
1806 ath5k_beacon_update(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1807 {
1808         int ret;
1809         struct ath5k_softc *sc = hw->priv;
1810         struct ath5k_vif *avf = (void *)vif->drv_priv;
1811         struct sk_buff *skb;
1812
1813         if (WARN_ON(!vif)) {
1814                 ret = -EINVAL;
1815                 goto out;
1816         }
1817
1818         skb = ieee80211_beacon_get(hw, vif);
1819
1820         if (!skb) {
1821                 ret = -ENOMEM;
1822                 goto out;
1823         }
1824
1825         ath5k_debug_dump_skb(sc, skb, "BC  ", 1);
1826
1827         ath5k_txbuf_free_skb(sc, avf->bbuf);
1828         avf->bbuf->skb = skb;
1829         ret = ath5k_beacon_setup(sc, avf->bbuf);
1830         if (ret)
1831                 avf->bbuf->skb = NULL;
1832 out:
1833         return ret;
1834 }
1835
1836 /*
1837  * Transmit a beacon frame at SWBA.  Dynamic updates to the
1838  * frame contents are done as needed and the slot time is
1839  * also adjusted based on current state.
1840  *
1841  * This is called from software irq context (beacontq tasklets)
1842  * or user context from ath5k_beacon_config.
1843  */
1844 static void
1845 ath5k_beacon_send(struct ath5k_softc *sc)
1846 {
1847         struct ath5k_hw *ah = sc->ah;
1848         struct ieee80211_vif *vif;
1849         struct ath5k_vif *avf;
1850         struct ath5k_buf *bf;
1851         struct sk_buff *skb;
1852
1853         ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON, "in beacon_send\n");
1854
1855         /*
1856          * Check if the previous beacon has gone out.  If
1857          * not, don't don't try to post another: skip this
1858          * period and wait for the next.  Missed beacons
1859          * indicate a problem and should not occur.  If we
1860          * miss too many consecutive beacons reset the device.
1861          */
1862         if (unlikely(ath5k_hw_num_tx_pending(ah, sc->bhalq) != 0)) {
1863                 sc->bmisscount++;
1864                 ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
1865                         "missed %u consecutive beacons\n", sc->bmisscount);
1866                 if (sc->bmisscount > 10) {      /* NB: 10 is a guess */
1867                         ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
1868                                 "stuck beacon time (%u missed)\n",
1869                                 sc->bmisscount);
1870                         ATH5K_DBG(sc, ATH5K_DEBUG_RESET,
1871                                   "stuck beacon, resetting\n");
1872                         ieee80211_queue_work(sc->hw, &sc->reset_work);
1873                 }
1874                 return;
1875         }
1876         if (unlikely(sc->bmisscount != 0)) {
1877                 ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
1878                         "resume beacon xmit after %u misses\n",
1879                         sc->bmisscount);
1880                 sc->bmisscount = 0;
1881         }
1882
1883         if (sc->opmode == NL80211_IFTYPE_AP && sc->num_ap_vifs > 1) {
1884                 u64 tsf = ath5k_hw_get_tsf64(ah);
1885                 u32 tsftu = TSF_TO_TU(tsf);
1886                 int slot = ((tsftu % sc->bintval) * ATH_BCBUF) / sc->bintval;
1887                 vif = sc->bslot[(slot + 1) % ATH_BCBUF];
1888                 ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
1889                         "tsf %llx tsftu %x intval %u slot %u vif %p\n",
1890                         (unsigned long long)tsf, tsftu, sc->bintval, slot, vif);
1891         } else /* only one interface */
1892                 vif = sc->bslot[0];
1893
1894         if (!vif)
1895                 return;
1896
1897         avf = (void *)vif->drv_priv;
1898         bf = avf->bbuf;
1899         if (unlikely(bf->skb == NULL || sc->opmode == NL80211_IFTYPE_STATION ||
1900                         sc->opmode == NL80211_IFTYPE_MONITOR)) {
1901                 ATH5K_WARN(sc, "bf=%p bf_skb=%p\n", bf, bf ? bf->skb : NULL);
1902                 return;
1903         }
1904
1905         /*
1906          * Stop any current dma and put the new frame on the queue.
1907          * This should never fail since we check above that no frames
1908          * are still pending on the queue.
1909          */
1910         if (unlikely(ath5k_hw_stop_beacon_queue(ah, sc->bhalq))) {
1911                 ATH5K_WARN(sc, "beacon queue %u didn't start/stop ?\n", sc->bhalq);
1912                 /* NB: hw still stops DMA, so proceed */
1913         }
1914
1915         /* refresh the beacon for AP mode */
1916         if (sc->opmode == NL80211_IFTYPE_AP)
1917                 ath5k_beacon_update(sc->hw, vif);
1918
1919         ath5k_hw_set_txdp(ah, sc->bhalq, bf->daddr);
1920         ath5k_hw_start_tx_dma(ah, sc->bhalq);
1921         ATH5K_DBG(sc, ATH5K_DEBUG_BEACON, "TXDP[%u] = %llx (%p)\n",
1922                 sc->bhalq, (unsigned long long)bf->daddr, bf->desc);
1923
1924         skb = ieee80211_get_buffered_bc(sc->hw, vif);
1925         while (skb) {
1926                 ath5k_tx_queue(sc->hw, skb, sc->cabq);
1927                 skb = ieee80211_get_buffered_bc(sc->hw, vif);
1928         }
1929
1930         sc->bsent++;
1931 }
1932
1933 /**
1934  * ath5k_beacon_update_timers - update beacon timers
1935  *
1936  * @sc: struct ath5k_softc pointer we are operating on
1937  * @bc_tsf: the timestamp of the beacon. 0 to reset the TSF. -1 to perform a
1938  *          beacon timer update based on the current HW TSF.
1939  *
1940  * Calculate the next target beacon transmit time (TBTT) based on the timestamp
1941  * of a received beacon or the current local hardware TSF and write it to the
1942  * beacon timer registers.
1943  *
1944  * This is called in a variety of situations, e.g. when a beacon is received,
1945  * when a TSF update has been detected, but also when an new IBSS is created or
1946  * when we otherwise know we have to update the timers, but we keep it in this
1947  * function to have it all together in one place.
1948  */
1949 static void
1950 ath5k_beacon_update_timers(struct ath5k_softc *sc, u64 bc_tsf)
1951 {
1952         struct ath5k_hw *ah = sc->ah;
1953         u32 nexttbtt, intval, hw_tu, bc_tu;
1954         u64 hw_tsf;
1955
1956         intval = sc->bintval & AR5K_BEACON_PERIOD;
1957         if (sc->opmode == NL80211_IFTYPE_AP && sc->num_ap_vifs > 1) {
1958                 intval /= ATH_BCBUF;    /* staggered multi-bss beacons */
1959                 if (intval < 15)
1960                         ATH5K_WARN(sc, "intval %u is too low, min 15\n",
1961                                    intval);
1962         }
1963         if (WARN_ON(!intval))
1964                 return;
1965
1966         /* beacon TSF converted to TU */
1967         bc_tu = TSF_TO_TU(bc_tsf);
1968
1969         /* current TSF converted to TU */
1970         hw_tsf = ath5k_hw_get_tsf64(ah);
1971         hw_tu = TSF_TO_TU(hw_tsf);
1972
1973 #define FUDGE AR5K_TUNE_SW_BEACON_RESP + 3
1974         /* We use FUDGE to make sure the next TBTT is ahead of the current TU.
1975          * Since we later substract AR5K_TUNE_SW_BEACON_RESP (10) in the timer
1976          * configuration we need to make sure it is bigger than that. */
1977
1978         if (bc_tsf == -1) {
1979                 /*
1980                  * no beacons received, called internally.
1981                  * just need to refresh timers based on HW TSF.
1982                  */
1983                 nexttbtt = roundup(hw_tu + FUDGE, intval);
1984         } else if (bc_tsf == 0) {
1985                 /*
1986                  * no beacon received, probably called by ath5k_reset_tsf().
1987                  * reset TSF to start with 0.
1988                  */
1989                 nexttbtt = intval;
1990                 intval |= AR5K_BEACON_RESET_TSF;
1991         } else if (bc_tsf > hw_tsf) {
1992                 /*
1993                  * beacon received, SW merge happend but HW TSF not yet updated.
1994                  * not possible to reconfigure timers yet, but next time we
1995                  * receive a beacon with the same BSSID, the hardware will
1996                  * automatically update the TSF and then we need to reconfigure
1997                  * the timers.
1998                  */
1999                 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
2000                         "need to wait for HW TSF sync\n");
2001                 return;
2002         } else {
2003                 /*
2004                  * most important case for beacon synchronization between STA.
2005                  *
2006                  * beacon received and HW TSF has been already updated by HW.
2007                  * update next TBTT based on the TSF of the beacon, but make
2008                  * sure it is ahead of our local TSF timer.
2009                  */
2010                 nexttbtt = bc_tu + roundup(hw_tu + FUDGE - bc_tu, intval);
2011         }
2012 #undef FUDGE
2013
2014         sc->nexttbtt = nexttbtt;
2015
2016         intval |= AR5K_BEACON_ENA;
2017         ath5k_hw_init_beacon(ah, nexttbtt, intval);
2018
2019         /*
2020          * debugging output last in order to preserve the time critical aspect
2021          * of this function
2022          */
2023         if (bc_tsf == -1)
2024                 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
2025                         "reconfigured timers based on HW TSF\n");
2026         else if (bc_tsf == 0)
2027                 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
2028                         "reset HW TSF and timers\n");
2029         else
2030                 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
2031                         "updated timers based on beacon TSF\n");
2032
2033         ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
2034                           "bc_tsf %llx hw_tsf %llx bc_tu %u hw_tu %u nexttbtt %u\n",
2035                           (unsigned long long) bc_tsf,
2036                           (unsigned long long) hw_tsf, bc_tu, hw_tu, nexttbtt);
2037         ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON, "intval %u %s %s\n",
2038                 intval & AR5K_BEACON_PERIOD,
2039                 intval & AR5K_BEACON_ENA ? "AR5K_BEACON_ENA" : "",
2040                 intval & AR5K_BEACON_RESET_TSF ? "AR5K_BEACON_RESET_TSF" : "");
2041 }
2042
2043 /**
2044  * ath5k_beacon_config - Configure the beacon queues and interrupts
2045  *
2046  * @sc: struct ath5k_softc pointer we are operating on
2047  *
2048  * In IBSS mode we use a self-linked tx descriptor if possible. We enable SWBA
2049  * interrupts to detect TSF updates only.
2050  */
2051 static void
2052 ath5k_beacon_config(struct ath5k_softc *sc)
2053 {
2054         struct ath5k_hw *ah = sc->ah;
2055         unsigned long flags;
2056
2057         spin_lock_irqsave(&sc->block, flags);
2058         sc->bmisscount = 0;
2059         sc->imask &= ~(AR5K_INT_BMISS | AR5K_INT_SWBA);
2060
2061         if (sc->enable_beacon) {
2062                 /*
2063                  * In IBSS mode we use a self-linked tx descriptor and let the
2064                  * hardware send the beacons automatically. We have to load it
2065                  * only once here.
2066                  * We use the SWBA interrupt only to keep track of the beacon
2067                  * timers in order to detect automatic TSF updates.
2068                  */
2069                 ath5k_beaconq_config(sc);
2070
2071                 sc->imask |= AR5K_INT_SWBA;
2072
2073                 if (sc->opmode == NL80211_IFTYPE_ADHOC) {
2074                         if (ath5k_hw_hasveol(ah))
2075                                 ath5k_beacon_send(sc);
2076                 } else
2077                         ath5k_beacon_update_timers(sc, -1);
2078         } else {
2079                 ath5k_hw_stop_beacon_queue(sc->ah, sc->bhalq);
2080         }
2081
2082         ath5k_hw_set_imr(ah, sc->imask);
2083         mmiowb();
2084         spin_unlock_irqrestore(&sc->block, flags);
2085 }
2086
2087 static void ath5k_tasklet_beacon(unsigned long data)
2088 {
2089         struct ath5k_softc *sc = (struct ath5k_softc *) data;
2090
2091         /*
2092          * Software beacon alert--time to send a beacon.
2093          *
2094          * In IBSS mode we use this interrupt just to
2095          * keep track of the next TBTT (target beacon
2096          * transmission time) in order to detect wether
2097          * automatic TSF updates happened.
2098          */
2099         if (sc->opmode == NL80211_IFTYPE_ADHOC) {
2100                 /* XXX: only if VEOL suppported */
2101                 u64 tsf = ath5k_hw_get_tsf64(sc->ah);
2102                 sc->nexttbtt += sc->bintval;
2103                 ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
2104                                 "SWBA nexttbtt: %x hw_tu: %x "
2105                                 "TSF: %llx\n",
2106                                 sc->nexttbtt,
2107                                 TSF_TO_TU(tsf),
2108                                 (unsigned long long) tsf);
2109         } else {
2110                 spin_lock(&sc->block);
2111                 ath5k_beacon_send(sc);
2112                 spin_unlock(&sc->block);
2113         }
2114 }
2115
2116
2117 /********************\
2118 * Interrupt handling *
2119 \********************/
2120
2121 static void
2122 ath5k_intr_calibration_poll(struct ath5k_hw *ah)
2123 {
2124         if (time_is_before_eq_jiffies(ah->ah_cal_next_ani) &&
2125             !(ah->ah_cal_mask & AR5K_CALIBRATION_FULL)) {
2126                 /* run ANI only when full calibration is not active */
2127                 ah->ah_cal_next_ani = jiffies +
2128                         msecs_to_jiffies(ATH5K_TUNE_CALIBRATION_INTERVAL_ANI);
2129                 tasklet_schedule(&ah->ah_sc->ani_tasklet);
2130
2131         } else if (time_is_before_eq_jiffies(ah->ah_cal_next_full)) {
2132                 ah->ah_cal_next_full = jiffies +
2133                         msecs_to_jiffies(ATH5K_TUNE_CALIBRATION_INTERVAL_FULL);
2134                 tasklet_schedule(&ah->ah_sc->calib);
2135         }
2136         /* we could use SWI to generate enough interrupts to meet our
2137          * calibration interval requirements, if necessary:
2138          * AR5K_REG_ENABLE_BITS(ah, AR5K_CR, AR5K_CR_SWI); */
2139 }
2140
2141 irqreturn_t
2142 ath5k_intr(int irq, void *dev_id)
2143 {
2144         struct ath5k_softc *sc = dev_id;
2145         struct ath5k_hw *ah = sc->ah;
2146         enum ath5k_int status;
2147         unsigned int counter = 1000;
2148
2149         if (unlikely(test_bit(ATH_STAT_INVALID, sc->status) ||
2150                 ((ath5k_get_bus_type(ah) != ATH_AHB) &&
2151                                 !ath5k_hw_is_intr_pending(ah))))
2152                 return IRQ_NONE;
2153
2154         do {
2155                 ath5k_hw_get_isr(ah, &status);          /* NB: clears IRQ too */
2156                 ATH5K_DBG(sc, ATH5K_DEBUG_INTR, "status 0x%x/0x%x\n",
2157                                 status, sc->imask);
2158                 if (unlikely(status & AR5K_INT_FATAL)) {
2159                         /*
2160                          * Fatal errors are unrecoverable.
2161                          * Typically these are caused by DMA errors.
2162                          */
2163                         ATH5K_DBG(sc, ATH5K_DEBUG_RESET,
2164                                   "fatal int, resetting\n");
2165                         ieee80211_queue_work(sc->hw, &sc->reset_work);
2166                 } else if (unlikely(status & AR5K_INT_RXORN)) {
2167                         /*
2168                          * Receive buffers are full. Either the bus is busy or
2169                          * the CPU is not fast enough to process all received
2170                          * frames.
2171                          * Older chipsets need a reset to come out of this
2172                          * condition, but we treat it as RX for newer chips.
2173                          * We don't know exactly which versions need a reset -
2174                          * this guess is copied from the HAL.
2175                          */
2176                         sc->stats.rxorn_intr++;
2177                         if (ah->ah_mac_srev < AR5K_SREV_AR5212) {
2178                                 ATH5K_DBG(sc, ATH5K_DEBUG_RESET,
2179                                           "rx overrun, resetting\n");
2180                                 ieee80211_queue_work(sc->hw, &sc->reset_work);
2181                         }
2182                         else
2183                                 tasklet_schedule(&sc->rxtq);
2184                 } else {
2185                         if (status & AR5K_INT_SWBA) {
2186                                 tasklet_hi_schedule(&sc->beacontq);
2187                         }
2188                         if (status & AR5K_INT_RXEOL) {
2189                                 /*
2190                                 * NB: the hardware should re-read the link when
2191                                 *     RXE bit is written, but it doesn't work at
2192                                 *     least on older hardware revs.
2193                                 */
2194                                 sc->stats.rxeol_intr++;
2195                         }
2196                         if (status & AR5K_INT_TXURN) {
2197                                 /* bump tx trigger level */
2198                                 ath5k_hw_update_tx_triglevel(ah, true);
2199                         }
2200                         if (status & (AR5K_INT_RXOK | AR5K_INT_RXERR))
2201                                 tasklet_schedule(&sc->rxtq);
2202                         if (status & (AR5K_INT_TXOK | AR5K_INT_TXDESC
2203                                         | AR5K_INT_TXERR | AR5K_INT_TXEOL))
2204                                 tasklet_schedule(&sc->txtq);
2205                         if (status & AR5K_INT_BMISS) {
2206                                 /* TODO */
2207                         }
2208                         if (status & AR5K_INT_MIB) {
2209                                 sc->stats.mib_intr++;
2210                                 ath5k_hw_update_mib_counters(ah);
2211                                 ath5k_ani_mib_intr(ah);
2212                         }
2213                         if (status & AR5K_INT_GPIO)
2214                                 tasklet_schedule(&sc->rf_kill.toggleq);
2215
2216                 }
2217
2218                 if (ath5k_get_bus_type(ah) == ATH_AHB)
2219                         break;
2220
2221         } while (ath5k_hw_is_intr_pending(ah) && --counter > 0);
2222
2223         if (unlikely(!counter))
2224                 ATH5K_WARN(sc, "too many interrupts, giving up for now\n");
2225
2226         ath5k_intr_calibration_poll(ah);
2227
2228         return IRQ_HANDLED;
2229 }
2230
2231 /*
2232  * Periodically recalibrate the PHY to account
2233  * for temperature/environment changes.
2234  */
2235 static void
2236 ath5k_tasklet_calibrate(unsigned long data)
2237 {
2238         struct ath5k_softc *sc = (void *)data;
2239         struct ath5k_hw *ah = sc->ah;
2240
2241         /* Only full calibration for now */
2242         ah->ah_cal_mask |= AR5K_CALIBRATION_FULL;
2243
2244         ATH5K_DBG(sc, ATH5K_DEBUG_CALIBRATE, "channel %u/%x\n",
2245                 ieee80211_frequency_to_channel(sc->curchan->center_freq),
2246                 sc->curchan->hw_value);
2247
2248         if (ath5k_hw_gainf_calibrate(ah) == AR5K_RFGAIN_NEED_CHANGE) {
2249                 /*
2250                  * Rfgain is out of bounds, reset the chip
2251                  * to load new gain values.
2252                  */
2253                 ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "calibration, resetting\n");
2254                 ieee80211_queue_work(sc->hw, &sc->reset_work);
2255         }
2256         if (ath5k_hw_phy_calibrate(ah, sc->curchan))
2257                 ATH5K_ERR(sc, "calibration of channel %u failed\n",
2258                         ieee80211_frequency_to_channel(
2259                                 sc->curchan->center_freq));
2260
2261         /* Noise floor calibration interrupts rx/tx path while I/Q calibration
2262          * doesn't.
2263          * TODO: We should stop TX here, so that it doesn't interfere.
2264          * Note that stopping the queues is not enough to stop TX! */
2265         if (time_is_before_eq_jiffies(ah->ah_cal_next_nf)) {
2266                 ah->ah_cal_next_nf = jiffies +
2267                         msecs_to_jiffies(ATH5K_TUNE_CALIBRATION_INTERVAL_NF);
2268                 ath5k_hw_update_noise_floor(ah);
2269         }
2270
2271         ah->ah_cal_mask &= ~AR5K_CALIBRATION_FULL;
2272 }
2273
2274
2275 static void
2276 ath5k_tasklet_ani(unsigned long data)
2277 {
2278         struct ath5k_softc *sc = (void *)data;
2279         struct ath5k_hw *ah = sc->ah;
2280
2281         ah->ah_cal_mask |= AR5K_CALIBRATION_ANI;
2282         ath5k_ani_calibration(ah);
2283         ah->ah_cal_mask &= ~AR5K_CALIBRATION_ANI;
2284 }
2285
2286
2287 static void
2288 ath5k_tx_complete_poll_work(struct work_struct *work)
2289 {
2290         struct ath5k_softc *sc = container_of(work, struct ath5k_softc,
2291                         tx_complete_work.work);
2292         struct ath5k_txq *txq;
2293         int i;
2294         bool needreset = false;
2295
2296         for (i = 0; i < ARRAY_SIZE(sc->txqs); i++) {
2297                 if (sc->txqs[i].setup) {
2298                         txq = &sc->txqs[i];
2299                         spin_lock_bh(&txq->lock);
2300                         if (txq->txq_len > 1) {
2301                                 if (txq->txq_poll_mark) {
2302                                         ATH5K_DBG(sc, ATH5K_DEBUG_XMIT,
2303                                                   "TX queue stuck %d\n",
2304                                                   txq->qnum);
2305                                         needreset = true;
2306                                         txq->txq_stuck++;
2307                                         spin_unlock_bh(&txq->lock);
2308                                         break;
2309                                 } else {
2310                                         txq->txq_poll_mark = true;
2311                                 }
2312                         }
2313                         spin_unlock_bh(&txq->lock);
2314                 }
2315         }
2316
2317         if (needreset) {
2318                 ATH5K_DBG(sc, ATH5K_DEBUG_RESET,
2319                           "TX queues stuck, resetting\n");
2320                 ath5k_reset(sc, NULL, true);
2321         }
2322
2323         ieee80211_queue_delayed_work(sc->hw, &sc->tx_complete_work,
2324                 msecs_to_jiffies(ATH5K_TX_COMPLETE_POLL_INT));
2325 }
2326
2327
2328 /*************************\
2329 * Initialization routines *
2330 \*************************/
2331
2332 int
2333 ath5k_init_softc(struct ath5k_softc *sc, const struct ath_bus_ops *bus_ops)
2334 {
2335         struct ieee80211_hw *hw = sc->hw;
2336         struct ath_common *common;
2337         int ret;
2338         int csz;
2339
2340         /* Initialize driver private data */
2341         SET_IEEE80211_DEV(hw, sc->dev);
2342         hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
2343                         IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
2344                         IEEE80211_HW_SIGNAL_DBM |
2345                         IEEE80211_HW_REPORTS_TX_ACK_STATUS;
2346
2347         hw->wiphy->interface_modes =
2348                 BIT(NL80211_IFTYPE_AP) |
2349                 BIT(NL80211_IFTYPE_STATION) |
2350                 BIT(NL80211_IFTYPE_ADHOC) |
2351                 BIT(NL80211_IFTYPE_MESH_POINT);
2352
2353         hw->extra_tx_headroom = 2;
2354         hw->channel_change_time = 5000;
2355
2356         /*
2357          * Mark the device as detached to avoid processing
2358          * interrupts until setup is complete.
2359          */
2360         __set_bit(ATH_STAT_INVALID, sc->status);
2361
2362         sc->opmode = NL80211_IFTYPE_STATION;
2363         sc->bintval = 1000;
2364         mutex_init(&sc->lock);
2365         spin_lock_init(&sc->rxbuflock);
2366         spin_lock_init(&sc->txbuflock);
2367         spin_lock_init(&sc->block);
2368
2369
2370         /* Setup interrupt handler */
2371         ret = request_irq(sc->irq, ath5k_intr, IRQF_SHARED, "ath", sc);
2372         if (ret) {
2373                 ATH5K_ERR(sc, "request_irq failed\n");
2374                 goto err;
2375         }
2376
2377         /* If we passed the test, malloc an ath5k_hw struct */
2378         sc->ah = kzalloc(sizeof(struct ath5k_hw), GFP_KERNEL);
2379         if (!sc->ah) {
2380                 ret = -ENOMEM;
2381                 ATH5K_ERR(sc, "out of memory\n");
2382                 goto err_irq;
2383         }
2384
2385         sc->ah->ah_sc = sc;
2386         sc->ah->ah_iobase = sc->iobase;
2387         common = ath5k_hw_common(sc->ah);
2388         common->ops = &ath5k_common_ops;
2389         common->bus_ops = bus_ops;
2390         common->ah = sc->ah;
2391         common->hw = hw;
2392         common->priv = sc;
2393
2394         /*
2395          * Cache line size is used to size and align various
2396          * structures used to communicate with the hardware.
2397          */
2398         ath5k_read_cachesize(common, &csz);
2399         common->cachelsz = csz << 2; /* convert to bytes */
2400
2401         spin_lock_init(&common->cc_lock);
2402
2403         /* Initialize device */
2404         ret = ath5k_hw_init(sc);
2405         if (ret)
2406                 goto err_free_ah;
2407
2408         /* set up multi-rate retry capabilities */
2409         if (sc->ah->ah_version == AR5K_AR5212) {
2410                 hw->max_rates = 4;
2411                 hw->max_rate_tries = 11;
2412         }
2413
2414         hw->vif_data_size = sizeof(struct ath5k_vif);
2415
2416         /* Finish private driver data initialization */
2417         ret = ath5k_init(hw);
2418         if (ret)
2419                 goto err_ah;
2420
2421         ATH5K_INFO(sc, "Atheros AR%s chip found (MAC: 0x%x, PHY: 0x%x)\n",
2422                         ath5k_chip_name(AR5K_VERSION_MAC, sc->ah->ah_mac_srev),
2423                                         sc->ah->ah_mac_srev,
2424                                         sc->ah->ah_phy_revision);
2425
2426         if (!sc->ah->ah_single_chip) {
2427                 /* Single chip radio (!RF5111) */
2428                 if (sc->ah->ah_radio_5ghz_revision &&
2429                         !sc->ah->ah_radio_2ghz_revision) {
2430                         /* No 5GHz support -> report 2GHz radio */
2431                         if (!test_bit(AR5K_MODE_11A,
2432                                 sc->ah->ah_capabilities.cap_mode)) {
2433                                 ATH5K_INFO(sc, "RF%s 2GHz radio found (0x%x)\n",
2434                                         ath5k_chip_name(AR5K_VERSION_RAD,
2435                                                 sc->ah->ah_radio_5ghz_revision),
2436                                                 sc->ah->ah_radio_5ghz_revision);
2437                         /* No 2GHz support (5110 and some
2438                          * 5Ghz only cards) -> report 5Ghz radio */
2439                         } else if (!test_bit(AR5K_MODE_11B,
2440                                 sc->ah->ah_capabilities.cap_mode)) {
2441                                 ATH5K_INFO(sc, "RF%s 5GHz radio found (0x%x)\n",
2442                                         ath5k_chip_name(AR5K_VERSION_RAD,
2443                                                 sc->ah->ah_radio_5ghz_revision),
2444                                                 sc->ah->ah_radio_5ghz_revision);
2445                         /* Multiband radio */
2446                         } else {
2447                                 ATH5K_INFO(sc, "RF%s multiband radio found"
2448                                         " (0x%x)\n",
2449                                         ath5k_chip_name(AR5K_VERSION_RAD,
2450                                                 sc->ah->ah_radio_5ghz_revision),
2451                                                 sc->ah->ah_radio_5ghz_revision);
2452                         }
2453                 }
2454                 /* Multi chip radio (RF5111 - RF2111) ->
2455                  * report both 2GHz/5GHz radios */
2456                 else if (sc->ah->ah_radio_5ghz_revision &&
2457                                 sc->ah->ah_radio_2ghz_revision){
2458                         ATH5K_INFO(sc, "RF%s 5GHz radio found (0x%x)\n",
2459                                 ath5k_chip_name(AR5K_VERSION_RAD,
2460                                         sc->ah->ah_radio_5ghz_revision),
2461                                         sc->ah->ah_radio_5ghz_revision);
2462                         ATH5K_INFO(sc, "RF%s 2GHz radio found (0x%x)\n",
2463                                 ath5k_chip_name(AR5K_VERSION_RAD,
2464                                         sc->ah->ah_radio_2ghz_revision),
2465                                         sc->ah->ah_radio_2ghz_revision);
2466                 }
2467         }
2468
2469         ath5k_debug_init_device(sc);
2470
2471         /* ready to process interrupts */
2472         __clear_bit(ATH_STAT_INVALID, sc->status);
2473
2474         return 0;
2475 err_ah:
2476         ath5k_hw_deinit(sc->ah);
2477 err_free_ah:
2478         kfree(sc->ah);
2479 err_irq:
2480         free_irq(sc->irq, sc);
2481 err:
2482         return ret;
2483 }
2484
2485 static int
2486 ath5k_stop_locked(struct ath5k_softc *sc)
2487 {
2488         struct ath5k_hw *ah = sc->ah;
2489
2490         ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "invalid %u\n",
2491                         test_bit(ATH_STAT_INVALID, sc->status));
2492
2493         /*
2494          * Shutdown the hardware and driver:
2495          *    stop output from above
2496          *    disable interrupts
2497          *    turn off timers
2498          *    turn off the radio
2499          *    clear transmit machinery
2500          *    clear receive machinery
2501          *    drain and release tx queues
2502          *    reclaim beacon resources
2503          *    power down hardware
2504          *
2505          * Note that some of this work is not possible if the
2506          * hardware is gone (invalid).
2507          */
2508         ieee80211_stop_queues(sc->hw);
2509
2510         if (!test_bit(ATH_STAT_INVALID, sc->status)) {
2511                 ath5k_led_off(sc);
2512                 ath5k_hw_set_imr(ah, 0);
2513                 synchronize_irq(sc->irq);
2514                 ath5k_rx_stop(sc);
2515                 ath5k_hw_dma_stop(ah);
2516                 ath5k_drain_tx_buffs(sc);
2517                 ath5k_hw_phy_disable(ah);
2518         }
2519
2520         return 0;
2521 }
2522
2523 static int
2524 ath5k_init_hw(struct ath5k_softc *sc)
2525 {
2526         struct ath5k_hw *ah = sc->ah;
2527         struct ath_common *common = ath5k_hw_common(ah);
2528         int ret, i;
2529
2530         mutex_lock(&sc->lock);
2531
2532         ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "mode %d\n", sc->opmode);
2533
2534         /*
2535          * Stop anything previously setup.  This is safe
2536          * no matter this is the first time through or not.
2537          */
2538         ath5k_stop_locked(sc);
2539
2540         /*
2541          * The basic interface to setting the hardware in a good
2542          * state is ``reset''.  On return the hardware is known to
2543          * be powered up and with interrupts disabled.  This must
2544          * be followed by initialization of the appropriate bits
2545          * and then setup of the interrupt mask.
2546          */
2547         sc->curchan = sc->hw->conf.channel;
2548         sc->curband = &sc->sbands[sc->curchan->band];
2549         sc->imask = AR5K_INT_RXOK | AR5K_INT_RXERR | AR5K_INT_RXEOL |
2550                 AR5K_INT_RXORN | AR5K_INT_TXDESC | AR5K_INT_TXEOL |
2551                 AR5K_INT_FATAL | AR5K_INT_GLOBAL | AR5K_INT_MIB;
2552
2553         ret = ath5k_reset(sc, NULL, false);
2554         if (ret)
2555                 goto done;
2556
2557         ath5k_rfkill_hw_start(ah);
2558
2559         /*
2560          * Reset the key cache since some parts do not reset the
2561          * contents on initial power up or resume from suspend.
2562          */
2563         for (i = 0; i < common->keymax; i++)
2564                 ath_hw_keyreset(common, (u16) i);
2565
2566         /* Use higher rates for acks instead of base
2567          * rate */
2568         ah->ah_ack_bitrate_high = true;
2569
2570         for (i = 0; i < ARRAY_SIZE(sc->bslot); i++)
2571                 sc->bslot[i] = NULL;
2572
2573         ret = 0;
2574 done:
2575         mmiowb();
2576         mutex_unlock(&sc->lock);
2577
2578         ieee80211_queue_delayed_work(sc->hw, &sc->tx_complete_work,
2579                         msecs_to_jiffies(ATH5K_TX_COMPLETE_POLL_INT));
2580
2581         return ret;
2582 }
2583
2584 static void stop_tasklets(struct ath5k_softc *sc)
2585 {
2586         tasklet_kill(&sc->rxtq);
2587         tasklet_kill(&sc->txtq);
2588         tasklet_kill(&sc->calib);
2589         tasklet_kill(&sc->beacontq);
2590         tasklet_kill(&sc->ani_tasklet);
2591 }
2592
2593 /*
2594  * Stop the device, grabbing the top-level lock to protect
2595  * against concurrent entry through ath5k_init (which can happen
2596  * if another thread does a system call and the thread doing the
2597  * stop is preempted).
2598  */
2599 static int
2600 ath5k_stop_hw(struct ath5k_softc *sc)
2601 {
2602         int ret;
2603
2604         mutex_lock(&sc->lock);
2605         ret = ath5k_stop_locked(sc);
2606         if (ret == 0 && !test_bit(ATH_STAT_INVALID, sc->status)) {
2607                 /*
2608                  * Don't set the card in full sleep mode!
2609                  *
2610                  * a) When the device is in this state it must be carefully
2611                  * woken up or references to registers in the PCI clock
2612                  * domain may freeze the bus (and system).  This varies
2613                  * by chip and is mostly an issue with newer parts
2614                  * (madwifi sources mentioned srev >= 0x78) that go to
2615                  * sleep more quickly.
2616                  *
2617                  * b) On older chips full sleep results a weird behaviour
2618                  * during wakeup. I tested various cards with srev < 0x78
2619                  * and they don't wake up after module reload, a second
2620                  * module reload is needed to bring the card up again.
2621                  *
2622                  * Until we figure out what's going on don't enable
2623                  * full chip reset on any chip (this is what Legacy HAL
2624                  * and Sam's HAL do anyway). Instead Perform a full reset
2625                  * on the device (same as initial state after attach) and
2626                  * leave it idle (keep MAC/BB on warm reset) */
2627                 ret = ath5k_hw_on_hold(sc->ah);
2628
2629                 ATH5K_DBG(sc, ATH5K_DEBUG_RESET,
2630                                 "putting device to sleep\n");
2631         }
2632
2633         mmiowb();
2634         mutex_unlock(&sc->lock);
2635
2636         stop_tasklets(sc);
2637
2638         cancel_delayed_work_sync(&sc->tx_complete_work);
2639
2640         ath5k_rfkill_hw_stop(sc->ah);
2641
2642         return ret;
2643 }
2644
2645 /*
2646  * Reset the hardware.  If chan is not NULL, then also pause rx/tx
2647  * and change to the given channel.
2648  *
2649  * This should be called with sc->lock.
2650  */
2651 static int
2652 ath5k_reset(struct ath5k_softc *sc, struct ieee80211_channel *chan,
2653                                                         bool skip_pcu)
2654 {
2655         struct ath5k_hw *ah = sc->ah;
2656         int ret, ani_mode;
2657
2658         ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "resetting\n");
2659
2660         ath5k_hw_set_imr(ah, 0);
2661         synchronize_irq(sc->irq);
2662         stop_tasklets(sc);
2663
2664         /* Save ani mode and disable ANI durring
2665          * reset. If we don't we might get false
2666          * PHY error interrupts. */
2667         ani_mode = ah->ah_sc->ani_state.ani_mode;
2668         ath5k_ani_init(ah, ATH5K_ANI_MODE_OFF);
2669
2670         /* We are going to empty hw queues
2671          * so we should also free any remaining
2672          * tx buffers */
2673         ath5k_drain_tx_buffs(sc);
2674         if (chan) {
2675                 sc->curchan = chan;
2676                 sc->curband = &sc->sbands[chan->band];
2677         }
2678         ret = ath5k_hw_reset(ah, sc->opmode, sc->curchan, chan != NULL,
2679                                                                 skip_pcu);
2680         if (ret) {
2681                 ATH5K_ERR(sc, "can't reset hardware (%d)\n", ret);
2682                 goto err;
2683         }
2684
2685         ret = ath5k_rx_start(sc);
2686         if (ret) {
2687                 ATH5K_ERR(sc, "can't start recv logic\n");
2688                 goto err;
2689         }
2690
2691         ath5k_ani_init(ah, ani_mode);
2692
2693         ah->ah_cal_next_full = jiffies;
2694         ah->ah_cal_next_ani = jiffies;
2695         ah->ah_cal_next_nf = jiffies;
2696         ewma_init(&ah->ah_beacon_rssi_avg, 1024, 8);
2697
2698         /*
2699          * Change channels and update the h/w rate map if we're switching;
2700          * e.g. 11a to 11b/g.
2701          *
2702          * We may be doing a reset in response to an ioctl that changes the
2703          * channel so update any state that might change as a result.
2704          *
2705          * XXX needed?
2706          */
2707 /*      ath5k_chan_change(sc, c); */
2708
2709         ath5k_beacon_config(sc);
2710         /* intrs are enabled by ath5k_beacon_config */
2711
2712         ieee80211_wake_queues(sc->hw);
2713
2714         return 0;
2715 err:
2716         return ret;
2717 }
2718
2719 static void ath5k_reset_work(struct work_struct *work)
2720 {
2721         struct ath5k_softc *sc = container_of(work, struct ath5k_softc,
2722                 reset_work);
2723
2724         mutex_lock(&sc->lock);
2725         ath5k_reset(sc, NULL, true);
2726         mutex_unlock(&sc->lock);
2727 }
2728
2729 static int
2730 ath5k_init(struct ieee80211_hw *hw)
2731 {
2732
2733         struct ath5k_softc *sc = hw->priv;
2734         struct ath5k_hw *ah = sc->ah;
2735         struct ath_regulatory *regulatory = ath5k_hw_regulatory(ah);
2736         struct ath5k_txq *txq;
2737         u8 mac[ETH_ALEN] = {};
2738         int ret;
2739
2740
2741         /*
2742          * Check if the MAC has multi-rate retry support.
2743          * We do this by trying to setup a fake extended
2744          * descriptor.  MACs that don't have support will
2745          * return false w/o doing anything.  MACs that do
2746          * support it will return true w/o doing anything.
2747          */
2748         ret = ath5k_hw_setup_mrr_tx_desc(ah, NULL, 0, 0, 0, 0, 0, 0);
2749
2750         if (ret < 0)
2751                 goto err;
2752         if (ret > 0)
2753                 __set_bit(ATH_STAT_MRRETRY, sc->status);
2754
2755         /*
2756          * Collect the channel list.  The 802.11 layer
2757          * is resposible for filtering this list based
2758          * on settings like the phy mode and regulatory
2759          * domain restrictions.
2760          */
2761         ret = ath5k_setup_bands(hw);
2762         if (ret) {
2763                 ATH5K_ERR(sc, "can't get channels\n");
2764                 goto err;
2765         }
2766
2767         /* NB: setup here so ath5k_rate_update is happy */
2768         if (test_bit(AR5K_MODE_11A, ah->ah_modes))
2769                 ath5k_setcurmode(sc, AR5K_MODE_11A);
2770         else
2771                 ath5k_setcurmode(sc, AR5K_MODE_11B);
2772
2773         /*
2774          * Allocate tx+rx descriptors and populate the lists.
2775          */
2776         ret = ath5k_desc_alloc(sc);
2777         if (ret) {
2778                 ATH5K_ERR(sc, "can't allocate descriptors\n");
2779                 goto err;
2780         }
2781
2782         /*
2783          * Allocate hardware transmit queues: one queue for
2784          * beacon frames and one data queue for each QoS
2785          * priority.  Note that hw functions handle resetting
2786          * these queues at the needed time.
2787          */
2788         ret = ath5k_beaconq_setup(ah);
2789         if (ret < 0) {
2790                 ATH5K_ERR(sc, "can't setup a beacon xmit queue\n");
2791                 goto err_desc;
2792         }
2793         sc->bhalq = ret;
2794         sc->cabq = ath5k_txq_setup(sc, AR5K_TX_QUEUE_CAB, 0);
2795         if (IS_ERR(sc->cabq)) {
2796                 ATH5K_ERR(sc, "can't setup cab queue\n");
2797                 ret = PTR_ERR(sc->cabq);
2798                 goto err_bhal;
2799         }
2800
2801         /* This order matches mac80211's queue priority, so we can
2802          * directly use the mac80211 queue number without any mapping */
2803         txq = ath5k_txq_setup(sc, AR5K_TX_QUEUE_DATA, AR5K_WME_AC_VO);
2804         if (IS_ERR(txq)) {
2805                 ATH5K_ERR(sc, "can't setup xmit queue\n");
2806                 ret = PTR_ERR(txq);
2807                 goto err_queues;
2808         }
2809         txq = ath5k_txq_setup(sc, AR5K_TX_QUEUE_DATA, AR5K_WME_AC_VI);
2810         if (IS_ERR(txq)) {
2811                 ATH5K_ERR(sc, "can't setup xmit queue\n");
2812                 ret = PTR_ERR(txq);
2813                 goto err_queues;
2814         }
2815         txq = ath5k_txq_setup(sc, AR5K_TX_QUEUE_DATA, AR5K_WME_AC_BE);
2816         if (IS_ERR(txq)) {
2817                 ATH5K_ERR(sc, "can't setup xmit queue\n");
2818                 ret = PTR_ERR(txq);
2819                 goto err_queues;
2820         }
2821         txq = ath5k_txq_setup(sc, AR5K_TX_QUEUE_DATA, AR5K_WME_AC_BK);
2822         if (IS_ERR(txq)) {
2823                 ATH5K_ERR(sc, "can't setup xmit queue\n");
2824                 ret = PTR_ERR(txq);
2825                 goto err_queues;
2826         }
2827         hw->queues = 4;
2828
2829         tasklet_init(&sc->rxtq, ath5k_tasklet_rx, (unsigned long)sc);
2830         tasklet_init(&sc->txtq, ath5k_tasklet_tx, (unsigned long)sc);
2831         tasklet_init(&sc->calib, ath5k_tasklet_calibrate, (unsigned long)sc);
2832         tasklet_init(&sc->beacontq, ath5k_tasklet_beacon, (unsigned long)sc);
2833         tasklet_init(&sc->ani_tasklet, ath5k_tasklet_ani, (unsigned long)sc);
2834
2835         INIT_WORK(&sc->reset_work, ath5k_reset_work);
2836         INIT_DELAYED_WORK(&sc->tx_complete_work, ath5k_tx_complete_poll_work);
2837
2838         ret = ath5k_eeprom_read_mac(ah, mac);
2839         if (ret) {
2840                 ATH5K_ERR(sc, "unable to read address from EEPROM\n");
2841                 goto err_queues;
2842         }
2843
2844         SET_IEEE80211_PERM_ADDR(hw, mac);
2845         memcpy(&sc->lladdr, mac, ETH_ALEN);
2846         /* All MAC address bits matter for ACKs */
2847         ath5k_update_bssid_mask_and_opmode(sc, NULL);
2848
2849         regulatory->current_rd = ah->ah_capabilities.cap_eeprom.ee_regdomain;
2850         ret = ath_regd_init(regulatory, hw->wiphy, ath5k_reg_notifier);
2851         if (ret) {
2852                 ATH5K_ERR(sc, "can't initialize regulatory system\n");
2853                 goto err_queues;
2854         }
2855
2856         ret = ieee80211_register_hw(hw);
2857         if (ret) {
2858                 ATH5K_ERR(sc, "can't register ieee80211 hw\n");
2859                 goto err_queues;
2860         }
2861
2862         if (!ath_is_world_regd(regulatory))
2863                 regulatory_hint(hw->wiphy, regulatory->alpha2);
2864
2865         ath5k_init_leds(sc);
2866
2867         ath5k_sysfs_register(sc);
2868
2869         return 0;
2870 err_queues:
2871         ath5k_txq_release(sc);
2872 err_bhal:
2873         ath5k_hw_release_tx_queue(ah, sc->bhalq);
2874 err_desc:
2875         ath5k_desc_free(sc);
2876 err:
2877         return ret;
2878 }
2879
2880 void
2881 ath5k_deinit_softc(struct ath5k_softc *sc)
2882 {
2883         struct ieee80211_hw *hw = sc->hw;
2884
2885         /*
2886          * NB: the order of these is important:
2887          * o call the 802.11 layer before detaching ath5k_hw to
2888          *   ensure callbacks into the driver to delete global
2889          *   key cache entries can be handled
2890          * o reclaim the tx queue data structures after calling
2891          *   the 802.11 layer as we'll get called back to reclaim
2892          *   node state and potentially want to use them
2893          * o to cleanup the tx queues the hal is called, so detach
2894          *   it last
2895          * XXX: ??? detach ath5k_hw ???
2896          * Other than that, it's straightforward...
2897          */
2898         ath5k_debug_finish_device(sc);
2899         ieee80211_unregister_hw(hw);
2900         ath5k_desc_free(sc);
2901         ath5k_txq_release(sc);
2902         ath5k_hw_release_tx_queue(sc->ah, sc->bhalq);
2903         ath5k_unregister_leds(sc);
2904
2905         ath5k_sysfs_unregister(sc);
2906         /*
2907          * NB: can't reclaim these until after ieee80211_ifdetach
2908          * returns because we'll get called back to reclaim node
2909          * state and potentially want to use them.
2910          */
2911         ath5k_hw_deinit(sc->ah);
2912         free_irq(sc->irq, sc);
2913 }
2914
2915 /********************\
2916 * Mac80211 functions *
2917 \********************/
2918
2919 static int
2920 ath5k_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
2921 {
2922         struct ath5k_softc *sc = hw->priv;
2923         u16 qnum = skb_get_queue_mapping(skb);
2924
2925         if (WARN_ON(qnum >= sc->ah->ah_capabilities.cap_queues.q_tx_num)) {
2926                 dev_kfree_skb_any(skb);
2927                 return 0;
2928         }
2929
2930         return ath5k_tx_queue(hw, skb, &sc->txqs[qnum]);
2931 }
2932
2933 static int ath5k_start(struct ieee80211_hw *hw)
2934 {
2935         return ath5k_init_hw(hw->priv);
2936 }
2937
2938 static void ath5k_stop(struct ieee80211_hw *hw)
2939 {
2940         ath5k_stop_hw(hw->priv);
2941 }
2942
2943 static int ath5k_add_interface(struct ieee80211_hw *hw,
2944                 struct ieee80211_vif *vif)
2945 {
2946         struct ath5k_softc *sc = hw->priv;
2947         int ret;
2948         struct ath5k_vif *avf = (void *)vif->drv_priv;
2949
2950         mutex_lock(&sc->lock);
2951
2952         if ((vif->type == NL80211_IFTYPE_AP ||
2953              vif->type == NL80211_IFTYPE_ADHOC)
2954             && (sc->num_ap_vifs + sc->num_adhoc_vifs) >= ATH_BCBUF) {
2955                 ret = -ELNRNG;
2956                 goto end;
2957         }
2958
2959         /* Don't allow other interfaces if one ad-hoc is configured.
2960          * TODO: Fix the problems with ad-hoc and multiple other interfaces.
2961          * We would need to operate the HW in ad-hoc mode to allow TSF updates
2962          * for the IBSS, but this breaks with additional AP or STA interfaces
2963          * at the moment. */
2964         if (sc->num_adhoc_vifs ||
2965             (sc->nvifs && vif->type == NL80211_IFTYPE_ADHOC)) {
2966                 ATH5K_ERR(sc, "Only one single ad-hoc interface is allowed.\n");
2967                 ret = -ELNRNG;
2968                 goto end;
2969         }
2970
2971         switch (vif->type) {
2972         case NL80211_IFTYPE_AP:
2973         case NL80211_IFTYPE_STATION:
2974         case NL80211_IFTYPE_ADHOC:
2975         case NL80211_IFTYPE_MESH_POINT:
2976                 avf->opmode = vif->type;
2977                 break;
2978         default:
2979                 ret = -EOPNOTSUPP;
2980                 goto end;
2981         }
2982
2983         sc->nvifs++;
2984         ATH5K_DBG(sc, ATH5K_DEBUG_MODE, "add interface mode %d\n", avf->opmode);
2985
2986         /* Assign the vap/adhoc to a beacon xmit slot. */
2987         if ((avf->opmode == NL80211_IFTYPE_AP) ||
2988             (avf->opmode == NL80211_IFTYPE_ADHOC)) {
2989                 int slot;
2990
2991                 WARN_ON(list_empty(&sc->bcbuf));
2992                 avf->bbuf = list_first_entry(&sc->bcbuf, struct ath5k_buf,
2993                                              list);
2994                 list_del(&avf->bbuf->list);
2995
2996                 avf->bslot = 0;
2997                 for (slot = 0; slot < ATH_BCBUF; slot++) {
2998                         if (!sc->bslot[slot]) {
2999                                 avf->bslot = slot;
3000                                 break;
3001                         }
3002                 }
3003                 BUG_ON(sc->bslot[avf->bslot] != NULL);
3004                 sc->bslot[avf->bslot] = vif;
3005                 if (avf->opmode == NL80211_IFTYPE_AP)
3006                         sc->num_ap_vifs++;
3007                 else
3008                         sc->num_adhoc_vifs++;
3009         }
3010
3011         /* Any MAC address is fine, all others are included through the
3012          * filter.
3013          */
3014         memcpy(&sc->lladdr, vif->addr, ETH_ALEN);
3015         ath5k_hw_set_lladdr(sc->ah, vif->addr);
3016
3017         memcpy(&avf->lladdr, vif->addr, ETH_ALEN);
3018
3019         ath5k_mode_setup(sc, vif);
3020
3021         ret = 0;
3022 end:
3023         mutex_unlock(&sc->lock);
3024         return ret;
3025 }
3026
3027 static void
3028 ath5k_remove_interface(struct ieee80211_hw *hw,
3029                         struct ieee80211_vif *vif)
3030 {
3031         struct ath5k_softc *sc = hw->priv;
3032         struct ath5k_vif *avf = (void *)vif->drv_priv;
3033         unsigned int i;
3034
3035         mutex_lock(&sc->lock);
3036         sc->nvifs--;
3037
3038         if (avf->bbuf) {
3039                 ath5k_txbuf_free_skb(sc, avf->bbuf);
3040                 list_add_tail(&avf->bbuf->list, &sc->bcbuf);
3041                 for (i = 0; i < ATH_BCBUF; i++) {
3042                         if (sc->bslot[i] == vif) {
3043                                 sc->bslot[i] = NULL;
3044                                 break;
3045                         }
3046                 }
3047                 avf->bbuf = NULL;
3048         }
3049         if (avf->opmode == NL80211_IFTYPE_AP)
3050                 sc->num_ap_vifs--;
3051         else if (avf->opmode == NL80211_IFTYPE_ADHOC)
3052                 sc->num_adhoc_vifs--;
3053
3054         ath5k_update_bssid_mask_and_opmode(sc, NULL);
3055         mutex_unlock(&sc->lock);
3056 }
3057
3058 /*
3059  * TODO: Phy disable/diversity etc
3060  */
3061 static int
3062 ath5k_config(struct ieee80211_hw *hw, u32 changed)
3063 {
3064         struct ath5k_softc *sc = hw->priv;
3065         struct ath5k_hw *ah = sc->ah;
3066         struct ieee80211_conf *conf = &hw->conf;
3067         int ret = 0;
3068
3069         mutex_lock(&sc->lock);
3070
3071         if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
3072                 ret = ath5k_chan_set(sc, conf->channel);
3073                 if (ret < 0)
3074                         goto unlock;
3075         }
3076
3077         if ((changed & IEEE80211_CONF_CHANGE_POWER) &&
3078         (sc->power_level != conf->power_level)) {
3079                 sc->power_level = conf->power_level;
3080
3081                 /* Half dB steps */
3082                 ath5k_hw_set_txpower_limit(ah, (conf->power_level * 2));
3083         }
3084
3085         /* TODO:
3086          * 1) Move this on config_interface and handle each case
3087          * separately eg. when we have only one STA vif, use
3088          * AR5K_ANTMODE_SINGLE_AP
3089          *
3090          * 2) Allow the user to change antenna mode eg. when only
3091          * one antenna is present
3092          *
3093          * 3) Allow the user to set default/tx antenna when possible
3094          *
3095          * 4) Default mode should handle 90% of the cases, together
3096          * with fixed a/b and single AP modes we should be able to
3097          * handle 99%. Sectored modes are extreme cases and i still
3098          * haven't found a usage for them. If we decide to support them,
3099          * then we must allow the user to set how many tx antennas we
3100          * have available
3101          */
3102         ath5k_hw_set_antenna_mode(ah, ah->ah_ant_mode);
3103
3104 unlock:
3105         mutex_unlock(&sc->lock);
3106         return ret;
3107 }
3108
3109 static u64 ath5k_prepare_multicast(struct ieee80211_hw *hw,
3110                                    struct netdev_hw_addr_list *mc_list)
3111 {
3112         u32 mfilt[2], val;
3113         u8 pos;
3114         struct netdev_hw_addr *ha;
3115
3116         mfilt[0] = 0;
3117         mfilt[1] = 1;
3118
3119         netdev_hw_addr_list_for_each(ha, mc_list) {
3120                 /* calculate XOR of eight 6-bit values */
3121                 val = get_unaligned_le32(ha->addr + 0);
3122                 pos = (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
3123                 val = get_unaligned_le32(ha->addr + 3);
3124                 pos ^= (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
3125                 pos &= 0x3f;
3126                 mfilt[pos / 32] |= (1 << (pos % 32));
3127                 /* XXX: we might be able to just do this instead,
3128                 * but not sure, needs testing, if we do use this we'd
3129                 * neet to inform below to not reset the mcast */
3130                 /* ath5k_hw_set_mcast_filterindex(ah,
3131                  *      ha->addr[5]); */
3132         }
3133
3134         return ((u64)(mfilt[1]) << 32) | mfilt[0];
3135 }
3136
3137 static bool ath_any_vif_assoc(struct ath5k_softc *sc)
3138 {
3139         struct ath_vif_iter_data iter_data;
3140         iter_data.hw_macaddr = NULL;
3141         iter_data.any_assoc = false;
3142         iter_data.need_set_hw_addr = false;
3143         iter_data.found_active = true;
3144
3145         ieee80211_iterate_active_interfaces_atomic(sc->hw, ath_vif_iter,
3146                                                    &iter_data);
3147         return iter_data.any_assoc;
3148 }
3149
3150 #define SUPPORTED_FIF_FLAGS \
3151         FIF_PROMISC_IN_BSS |  FIF_ALLMULTI | FIF_FCSFAIL | \
3152         FIF_PLCPFAIL | FIF_CONTROL | FIF_OTHER_BSS | \
3153         FIF_BCN_PRBRESP_PROMISC
3154 /*
3155  * o always accept unicast, broadcast, and multicast traffic
3156  * o multicast traffic for all BSSIDs will be enabled if mac80211
3157  *   says it should be
3158  * o maintain current state of phy ofdm or phy cck error reception.
3159  *   If the hardware detects any of these type of errors then
3160  *   ath5k_hw_get_rx_filter() will pass to us the respective
3161  *   hardware filters to be able to receive these type of frames.
3162  * o probe request frames are accepted only when operating in
3163  *   hostap, adhoc, or monitor modes
3164  * o enable promiscuous mode according to the interface state
3165  * o accept beacons:
3166  *   - when operating in adhoc mode so the 802.11 layer creates
3167  *     node table entries for peers,
3168  *   - when operating in station mode for collecting rssi data when
3169  *     the station is otherwise quiet, or
3170  *   - when scanning
3171  */
3172 static void ath5k_configure_filter(struct ieee80211_hw *hw,
3173                 unsigned int changed_flags,
3174                 unsigned int *new_flags,
3175                 u64 multicast)
3176 {
3177         struct ath5k_softc *sc = hw->priv;
3178         struct ath5k_hw *ah = sc->ah;
3179         u32 mfilt[2], rfilt;
3180
3181         mutex_lock(&sc->lock);
3182
3183         mfilt[0] = multicast;
3184         mfilt[1] = multicast >> 32;
3185
3186         /* Only deal with supported flags */
3187         changed_flags &= SUPPORTED_FIF_FLAGS;
3188         *new_flags &= SUPPORTED_FIF_FLAGS;
3189
3190         /* If HW detects any phy or radar errors, leave those filters on.
3191          * Also, always enable Unicast, Broadcasts and Multicast
3192          * XXX: move unicast, bssid broadcasts and multicast to mac80211 */
3193         rfilt = (ath5k_hw_get_rx_filter(ah) & (AR5K_RX_FILTER_PHYERR)) |
3194                 (AR5K_RX_FILTER_UCAST | AR5K_RX_FILTER_BCAST |
3195                 AR5K_RX_FILTER_MCAST);
3196
3197         if (changed_flags & (FIF_PROMISC_IN_BSS | FIF_OTHER_BSS)) {
3198                 if (*new_flags & FIF_PROMISC_IN_BSS) {
3199                         __set_bit(ATH_STAT_PROMISC, sc->status);
3200                 } else {
3201                         __clear_bit(ATH_STAT_PROMISC, sc->status);
3202                 }
3203         }
3204
3205         if (test_bit(ATH_STAT_PROMISC, sc->status))
3206                 rfilt |= AR5K_RX_FILTER_PROM;
3207
3208         /* Note, AR5K_RX_FILTER_MCAST is already enabled */
3209         if (*new_flags & FIF_ALLMULTI) {
3210                 mfilt[0] =  ~0;
3211                 mfilt[1] =  ~0;
3212         }
3213
3214         /* This is the best we can do */
3215         if (*new_flags & (FIF_FCSFAIL | FIF_PLCPFAIL))
3216                 rfilt |= AR5K_RX_FILTER_PHYERR;
3217
3218         /* FIF_BCN_PRBRESP_PROMISC really means to enable beacons
3219         * and probes for any BSSID */
3220         if ((*new_flags & FIF_BCN_PRBRESP_PROMISC) || (sc->nvifs > 1))
3221                 rfilt |= AR5K_RX_FILTER_BEACON;
3222
3223         /* FIF_CONTROL doc says that if FIF_PROMISC_IN_BSS is not
3224          * set we should only pass on control frames for this
3225          * station. This needs testing. I believe right now this
3226          * enables *all* control frames, which is OK.. but
3227          * but we should see if we can improve on granularity */
3228         if (*new_flags & FIF_CONTROL)
3229                 rfilt |= AR5K_RX_FILTER_CONTROL;
3230
3231         /* Additional settings per mode -- this is per ath5k */
3232
3233         /* XXX move these to mac80211, and add a beacon IFF flag to mac80211 */
3234
3235         switch (sc->opmode) {
3236         case NL80211_IFTYPE_MESH_POINT:
3237                 rfilt |= AR5K_RX_FILTER_CONTROL |
3238                          AR5K_RX_FILTER_BEACON |
3239                          AR5K_RX_FILTER_PROBEREQ |
3240                          AR5K_RX_FILTER_PROM;
3241                 break;
3242         case NL80211_IFTYPE_AP:
3243         case NL80211_IFTYPE_ADHOC:
3244                 rfilt |= AR5K_RX_FILTER_PROBEREQ |
3245                          AR5K_RX_FILTER_BEACON;
3246                 break;
3247         case NL80211_IFTYPE_STATION:
3248                 if (sc->assoc)
3249                         rfilt |= AR5K_RX_FILTER_BEACON;
3250         default:
3251                 break;
3252         }
3253
3254         /* Set filters */
3255         ath5k_hw_set_rx_filter(ah, rfilt);
3256
3257         /* Set multicast bits */
3258         ath5k_hw_set_mcast_filter(ah, mfilt[0], mfilt[1]);
3259         /* Set the cached hw filter flags, this will later actually
3260          * be set in HW */
3261         sc->filter_flags = rfilt;
3262
3263         mutex_unlock(&sc->lock);
3264 }
3265
3266 static int
3267 ath5k_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
3268               struct ieee80211_vif *vif, struct ieee80211_sta *sta,
3269               struct ieee80211_key_conf *key)
3270 {
3271         struct ath5k_softc *sc = hw->priv;
3272         struct ath5k_hw *ah = sc->ah;
3273         struct ath_common *common = ath5k_hw_common(ah);
3274         int ret = 0;
3275
3276         if (modparam_nohwcrypt)
3277                 return -EOPNOTSUPP;
3278
3279         switch (key->cipher) {
3280         case WLAN_CIPHER_SUITE_WEP40:
3281         case WLAN_CIPHER_SUITE_WEP104:
3282         case WLAN_CIPHER_SUITE_TKIP:
3283                 break;
3284         case WLAN_CIPHER_SUITE_CCMP:
3285                 if (common->crypt_caps & ATH_CRYPT_CAP_CIPHER_AESCCM)
3286                         break;
3287                 return -EOPNOTSUPP;
3288         default:
3289                 WARN_ON(1);
3290                 return -EINVAL;
3291         }
3292
3293         mutex_lock(&sc->lock);
3294
3295         switch (cmd) {
3296         case SET_KEY:
3297                 ret = ath_key_config(common, vif, sta, key);
3298                 if (ret >= 0) {
3299                         key->hw_key_idx = ret;
3300                         /* push IV and Michael MIC generation to stack */
3301                         key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
3302                         if (key->cipher == WLAN_CIPHER_SUITE_TKIP)
3303                                 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
3304                         if (key->cipher == WLAN_CIPHER_SUITE_CCMP)
3305                                 key->flags |= IEEE80211_KEY_FLAG_SW_MGMT;
3306                         ret = 0;
3307                 }
3308                 break;
3309         case DISABLE_KEY:
3310                 ath_key_delete(common, key);
3311                 break;
3312         default:
3313                 ret = -EINVAL;
3314         }
3315
3316         mmiowb();
3317         mutex_unlock(&sc->lock);
3318         return ret;
3319 }
3320
3321 static int
3322 ath5k_get_stats(struct ieee80211_hw *hw,
3323                 struct ieee80211_low_level_stats *stats)
3324 {
3325         struct ath5k_softc *sc = hw->priv;
3326
3327         /* Force update */
3328         ath5k_hw_update_mib_counters(sc->ah);
3329
3330         stats->dot11ACKFailureCount = sc->stats.ack_fail;
3331         stats->dot11RTSFailureCount = sc->stats.rts_fail;
3332         stats->dot11RTSSuccessCount = sc->stats.rts_ok;
3333         stats->dot11FCSErrorCount = sc->stats.fcs_error;
3334
3335         return 0;
3336 }
3337
3338 static int ath5k_get_survey(struct ieee80211_hw *hw, int idx,
3339                 struct survey_info *survey)
3340 {
3341         struct ath5k_softc *sc = hw->priv;
3342         struct ieee80211_conf *conf = &hw->conf;
3343         struct ath_common *common = ath5k_hw_common(sc->ah);
3344         struct ath_cycle_counters *cc = &common->cc_survey;
3345         unsigned int div = common->clockrate * 1000;
3346
3347         if (idx != 0)
3348                 return -ENOENT;
3349
3350         survey->channel = conf->channel;
3351         survey->filled = SURVEY_INFO_NOISE_DBM;
3352         survey->noise = sc->ah->ah_noise_floor;
3353
3354         spin_lock_bh(&common->cc_lock);
3355         ath_hw_cycle_counters_update(common);
3356         if (cc->cycles > 0) {
3357                 survey->filled |= SURVEY_INFO_CHANNEL_TIME |
3358                         SURVEY_INFO_CHANNEL_TIME_BUSY |
3359                         SURVEY_INFO_CHANNEL_TIME_RX |
3360                         SURVEY_INFO_CHANNEL_TIME_TX;
3361                 survey->channel_time += cc->cycles / div;
3362                 survey->channel_time_busy += cc->rx_busy / div;
3363                 survey->channel_time_rx += cc->rx_frame / div;
3364                 survey->channel_time_tx += cc->tx_frame / div;
3365         }
3366         memset(cc, 0, sizeof(*cc));
3367         spin_unlock_bh(&common->cc_lock);
3368
3369         return 0;
3370 }
3371
3372 static u64
3373 ath5k_get_tsf(struct ieee80211_hw *hw)
3374 {
3375         struct ath5k_softc *sc = hw->priv;
3376
3377         return ath5k_hw_get_tsf64(sc->ah);
3378 }
3379
3380 static void
3381 ath5k_set_tsf(struct ieee80211_hw *hw, u64 tsf)
3382 {
3383         struct ath5k_softc *sc = hw->priv;
3384
3385         ath5k_hw_set_tsf64(sc->ah, tsf);
3386 }
3387
3388 static void
3389 ath5k_reset_tsf(struct ieee80211_hw *hw)
3390 {
3391         struct ath5k_softc *sc = hw->priv;
3392
3393         /*
3394          * in IBSS mode we need to update the beacon timers too.
3395          * this will also reset the TSF if we call it with 0
3396          */
3397         if (sc->opmode == NL80211_IFTYPE_ADHOC)
3398                 ath5k_beacon_update_timers(sc, 0);
3399         else
3400                 ath5k_hw_reset_tsf(sc->ah);
3401 }
3402
3403 static void
3404 set_beacon_filter(struct ieee80211_hw *hw, bool enable)
3405 {
3406         struct ath5k_softc *sc = hw->priv;
3407         struct ath5k_hw *ah = sc->ah;
3408         u32 rfilt;
3409         rfilt = ath5k_hw_get_rx_filter(ah);
3410         if (enable)
3411                 rfilt |= AR5K_RX_FILTER_BEACON;
3412         else
3413                 rfilt &= ~AR5K_RX_FILTER_BEACON;
3414         ath5k_hw_set_rx_filter(ah, rfilt);
3415         sc->filter_flags = rfilt;
3416 }
3417
3418 static void ath5k_bss_info_changed(struct ieee80211_hw *hw,
3419                                     struct ieee80211_vif *vif,
3420                                     struct ieee80211_bss_conf *bss_conf,
3421                                     u32 changes)
3422 {
3423         struct ath5k_vif *avf = (void *)vif->drv_priv;
3424         struct ath5k_softc *sc = hw->priv;
3425         struct ath5k_hw *ah = sc->ah;
3426         struct ath_common *common = ath5k_hw_common(ah);
3427         unsigned long flags;
3428
3429         mutex_lock(&sc->lock);
3430
3431         if (changes & BSS_CHANGED_BSSID) {
3432                 /* Cache for later use during resets */
3433                 memcpy(common->curbssid, bss_conf->bssid, ETH_ALEN);
3434                 common->curaid = 0;
3435                 ath5k_hw_set_bssid(ah);
3436                 mmiowb();
3437         }
3438
3439         if (changes & BSS_CHANGED_BEACON_INT)
3440                 sc->bintval = bss_conf->beacon_int;
3441
3442         if (changes & BSS_CHANGED_ASSOC) {
3443                 avf->assoc = bss_conf->assoc;
3444                 if (bss_conf->assoc)
3445                         sc->assoc = bss_conf->assoc;
3446                 else
3447                         sc->assoc = ath_any_vif_assoc(sc);
3448
3449                 if (sc->opmode == NL80211_IFTYPE_STATION)
3450                         set_beacon_filter(hw, sc->assoc);
3451                 ath5k_hw_set_ledstate(sc->ah, sc->assoc ?
3452                         AR5K_LED_ASSOC : AR5K_LED_INIT);
3453                 if (bss_conf->assoc) {
3454                         ATH5K_DBG(sc, ATH5K_DEBUG_ANY,
3455                                   "Bss Info ASSOC %d, bssid: %pM\n",
3456                                   bss_conf->aid, common->curbssid);
3457                         common->curaid = bss_conf->aid;
3458                         ath5k_hw_set_bssid(ah);
3459                         /* Once ANI is available you would start it here */
3460                 }
3461         }
3462
3463         if (changes & BSS_CHANGED_BEACON) {
3464                 spin_lock_irqsave(&sc->block, flags);
3465                 ath5k_beacon_update(hw, vif);
3466                 spin_unlock_irqrestore(&sc->block, flags);
3467         }
3468
3469         if (changes & BSS_CHANGED_BEACON_ENABLED)
3470                 sc->enable_beacon = bss_conf->enable_beacon;
3471
3472         if (changes & (BSS_CHANGED_BEACON | BSS_CHANGED_BEACON_ENABLED |
3473                        BSS_CHANGED_BEACON_INT))
3474                 ath5k_beacon_config(sc);
3475
3476         mutex_unlock(&sc->lock);
3477 }
3478
3479 static void ath5k_sw_scan_start(struct ieee80211_hw *hw)
3480 {
3481         struct ath5k_softc *sc = hw->priv;
3482         if (!sc->assoc)
3483                 ath5k_hw_set_ledstate(sc->ah, AR5K_LED_SCAN);
3484 }
3485
3486 static void ath5k_sw_scan_complete(struct ieee80211_hw *hw)
3487 {
3488         struct ath5k_softc *sc = hw->priv;
3489         ath5k_hw_set_ledstate(sc->ah, sc->assoc ?
3490                 AR5K_LED_ASSOC : AR5K_LED_INIT);
3491 }
3492
3493 /**
3494  * ath5k_set_coverage_class - Set IEEE 802.11 coverage class
3495  *
3496  * @hw: struct ieee80211_hw pointer
3497  * @coverage_class: IEEE 802.11 coverage class number
3498  *
3499  * Mac80211 callback. Sets slot time, ACK timeout and CTS timeout for given
3500  * coverage class. The values are persistent, they are restored after device
3501  * reset.
3502  */
3503 static void ath5k_set_coverage_class(struct ieee80211_hw *hw, u8 coverage_class)
3504 {
3505         struct ath5k_softc *sc = hw->priv;
3506
3507         mutex_lock(&sc->lock);
3508         ath5k_hw_set_coverage_class(sc->ah, coverage_class);
3509         mutex_unlock(&sc->lock);
3510 }
3511
3512 static int ath5k_conf_tx(struct ieee80211_hw *hw, u16 queue,
3513                          const struct ieee80211_tx_queue_params *params)
3514 {
3515         struct ath5k_softc *sc = hw->priv;
3516         struct ath5k_hw *ah = sc->ah;
3517         struct ath5k_txq_info qi;
3518         int ret = 0;
3519
3520         if (queue >= ah->ah_capabilities.cap_queues.q_tx_num)
3521                 return 0;
3522
3523         mutex_lock(&sc->lock);
3524
3525         ath5k_hw_get_tx_queueprops(ah, queue, &qi);
3526
3527         qi.tqi_aifs = params->aifs;
3528         qi.tqi_cw_min = params->cw_min;
3529         qi.tqi_cw_max = params->cw_max;
3530         qi.tqi_burst_time = params->txop;
3531
3532         ATH5K_DBG(sc, ATH5K_DEBUG_ANY,
3533                   "Configure tx [queue %d],  "
3534                   "aifs: %d, cw_min: %d, cw_max: %d, txop: %d\n",
3535                   queue, params->aifs, params->cw_min,
3536                   params->cw_max, params->txop);
3537
3538         if (ath5k_hw_set_tx_queueprops(ah, queue, &qi)) {
3539                 ATH5K_ERR(sc,
3540                           "Unable to update hardware queue %u!\n", queue);
3541                 ret = -EIO;
3542         } else
3543                 ath5k_hw_reset_tx_queue(ah, queue);
3544
3545         mutex_unlock(&sc->lock);
3546
3547         return ret;
3548 }
3549
3550 static int ath5k_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
3551 {
3552         struct ath5k_softc *sc = hw->priv;
3553
3554         if (tx_ant == 1 && rx_ant == 1)
3555                 ath5k_hw_set_antenna_mode(sc->ah, AR5K_ANTMODE_FIXED_A);
3556         else if (tx_ant == 2 && rx_ant == 2)
3557                 ath5k_hw_set_antenna_mode(sc->ah, AR5K_ANTMODE_FIXED_B);
3558         else if ((tx_ant & 3) == 3 && (rx_ant & 3) == 3)
3559                 ath5k_hw_set_antenna_mode(sc->ah, AR5K_ANTMODE_DEFAULT);
3560         else
3561                 return -EINVAL;
3562         return 0;
3563 }
3564
3565 static int ath5k_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
3566 {
3567         struct ath5k_softc *sc = hw->priv;
3568
3569         switch (sc->ah->ah_ant_mode) {
3570         case AR5K_ANTMODE_FIXED_A:
3571                 *tx_ant = 1; *rx_ant = 1; break;
3572         case AR5K_ANTMODE_FIXED_B:
3573                 *tx_ant = 2; *rx_ant = 2; break;
3574         case AR5K_ANTMODE_DEFAULT:
3575                 *tx_ant = 3; *rx_ant = 3; break;
3576         }
3577         return 0;
3578 }
3579
3580 const struct ieee80211_ops ath5k_hw_ops = {
3581         .tx             = ath5k_tx,
3582         .start          = ath5k_start,
3583         .stop           = ath5k_stop,
3584         .add_interface  = ath5k_add_interface,
3585         .remove_interface = ath5k_remove_interface,
3586         .config         = ath5k_config,
3587         .prepare_multicast = ath5k_prepare_multicast,
3588         .configure_filter = ath5k_configure_filter,
3589         .set_key        = ath5k_set_key,
3590         .get_stats      = ath5k_get_stats,
3591         .get_survey     = ath5k_get_survey,
3592         .conf_tx        = ath5k_conf_tx,
3593         .get_tsf        = ath5k_get_tsf,
3594         .set_tsf        = ath5k_set_tsf,
3595         .reset_tsf      = ath5k_reset_tsf,
3596         .bss_info_changed = ath5k_bss_info_changed,
3597         .sw_scan_start  = ath5k_sw_scan_start,
3598         .sw_scan_complete = ath5k_sw_scan_complete,
3599         .set_coverage_class = ath5k_set_coverage_class,
3600         .set_antenna    = ath5k_set_antenna,
3601         .get_antenna    = ath5k_get_antenna,
3602 };