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