- Update to 2.6.25-rc3.
[linux-flexiantxendom0-3.2.10.git] / drivers / net / wireless / 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/version.h>
44 #include <linux/module.h>
45 #include <linux/delay.h>
46 #include <linux/if.h>
47 #include <linux/netdevice.h>
48 #include <linux/cache.h>
49 #include <linux/pci.h>
50 #include <linux/ethtool.h>
51 #include <linux/uaccess.h>
52
53 #include <net/ieee80211_radiotap.h>
54
55 #include <asm/unaligned.h>
56
57 #include "base.h"
58 #include "reg.h"
59 #include "debug.h"
60
61 /* unaligned little endian access */
62 #define LE_READ_2(_p) (le16_to_cpu(get_unaligned((__le16 *)(_p))))
63 #define LE_READ_4(_p) (le32_to_cpu(get_unaligned((__le32 *)(_p))))
64
65 enum {
66         ATH_LED_TX,
67         ATH_LED_RX,
68 };
69
70 static int ath5k_calinterval = 10; /* Calibrate PHY every 10 secs (TODO: Fixme) */
71
72
73 /******************\
74 * Internal defines *
75 \******************/
76
77 /* Module info */
78 MODULE_AUTHOR("Jiri Slaby");
79 MODULE_AUTHOR("Nick Kossifidis");
80 MODULE_DESCRIPTION("Support for 5xxx series of Atheros 802.11 wireless LAN cards.");
81 MODULE_SUPPORTED_DEVICE("Atheros 5xxx WLAN cards");
82 MODULE_LICENSE("Dual BSD/GPL");
83 MODULE_VERSION("0.1.1 (EXPERIMENTAL)");
84
85
86 /* Known PCI ids */
87 static struct pci_device_id ath5k_pci_id_table[] __devinitdata = {
88         { PCI_VDEVICE(ATHEROS, 0x0207), .driver_data = AR5K_AR5210 }, /* 5210 early */
89         { PCI_VDEVICE(ATHEROS, 0x0007), .driver_data = AR5K_AR5210 }, /* 5210 */
90         { PCI_VDEVICE(ATHEROS, 0x0011), .driver_data = AR5K_AR5211 }, /* 5311 - this is on AHB bus !*/
91         { PCI_VDEVICE(ATHEROS, 0x0012), .driver_data = AR5K_AR5211 }, /* 5211 */
92         { PCI_VDEVICE(ATHEROS, 0x0013), .driver_data = AR5K_AR5212 }, /* 5212 */
93         { PCI_VDEVICE(3COM_2,  0x0013), .driver_data = AR5K_AR5212 }, /* 3com 5212 */
94         { PCI_VDEVICE(3COM,    0x0013), .driver_data = AR5K_AR5212 }, /* 3com 3CRDAG675 5212 */
95         { PCI_VDEVICE(ATHEROS, 0x1014), .driver_data = AR5K_AR5212 }, /* IBM minipci 5212 */
96         { PCI_VDEVICE(ATHEROS, 0x0014), .driver_data = AR5K_AR5212 }, /* 5212 combatible */
97         { PCI_VDEVICE(ATHEROS, 0x0015), .driver_data = AR5K_AR5212 }, /* 5212 combatible */
98         { PCI_VDEVICE(ATHEROS, 0x0016), .driver_data = AR5K_AR5212 }, /* 5212 combatible */
99         { PCI_VDEVICE(ATHEROS, 0x0017), .driver_data = AR5K_AR5212 }, /* 5212 combatible */
100         { PCI_VDEVICE(ATHEROS, 0x0018), .driver_data = AR5K_AR5212 }, /* 5212 combatible */
101         { PCI_VDEVICE(ATHEROS, 0x0019), .driver_data = AR5K_AR5212 }, /* 5212 combatible */
102         { PCI_VDEVICE(ATHEROS, 0x001a), .driver_data = AR5K_AR5212 }, /* 2413 Griffin-lite */
103         { PCI_VDEVICE(ATHEROS, 0x001b), .driver_data = AR5K_AR5212 }, /* 5413 Eagle */
104         { PCI_VDEVICE(ATHEROS, 0x001c), .driver_data = AR5K_AR5212 }, /* 5424 Condor (PCI-E)*/
105         { PCI_VDEVICE(ATHEROS, 0x0023), .driver_data = AR5K_AR5212 }, /* 5416 */
106         { PCI_VDEVICE(ATHEROS, 0x0024), .driver_data = AR5K_AR5212 }, /* 5418 */
107         { 0 }
108 };
109 MODULE_DEVICE_TABLE(pci, ath5k_pci_id_table);
110
111 /* Known SREVs */
112 static struct ath5k_srev_name srev_names[] = {
113         { "5210",       AR5K_VERSION_VER,       AR5K_SREV_VER_AR5210 },
114         { "5311",       AR5K_VERSION_VER,       AR5K_SREV_VER_AR5311 },
115         { "5311A",      AR5K_VERSION_VER,       AR5K_SREV_VER_AR5311A },
116         { "5311B",      AR5K_VERSION_VER,       AR5K_SREV_VER_AR5311B },
117         { "5211",       AR5K_VERSION_VER,       AR5K_SREV_VER_AR5211 },
118         { "5212",       AR5K_VERSION_VER,       AR5K_SREV_VER_AR5212 },
119         { "5213",       AR5K_VERSION_VER,       AR5K_SREV_VER_AR5213 },
120         { "5213A",      AR5K_VERSION_VER,       AR5K_SREV_VER_AR5213A },
121         { "2424",       AR5K_VERSION_VER,       AR5K_SREV_VER_AR2424 },
122         { "5424",       AR5K_VERSION_VER,       AR5K_SREV_VER_AR5424 },
123         { "5413",       AR5K_VERSION_VER,       AR5K_SREV_VER_AR5413 },
124         { "5414",       AR5K_VERSION_VER,       AR5K_SREV_VER_AR5414 },
125         { "5416",       AR5K_VERSION_VER,       AR5K_SREV_VER_AR5416 },
126         { "5418",       AR5K_VERSION_VER,       AR5K_SREV_VER_AR5418 },
127         { "xxxxx",      AR5K_VERSION_VER,       AR5K_SREV_UNKNOWN },
128         { "5110",       AR5K_VERSION_RAD,       AR5K_SREV_RAD_5110 },
129         { "5111",       AR5K_VERSION_RAD,       AR5K_SREV_RAD_5111 },
130         { "2111",       AR5K_VERSION_RAD,       AR5K_SREV_RAD_2111 },
131         { "5112",       AR5K_VERSION_RAD,       AR5K_SREV_RAD_5112 },
132         { "5112A",      AR5K_VERSION_RAD,       AR5K_SREV_RAD_5112A },
133         { "2112",       AR5K_VERSION_RAD,       AR5K_SREV_RAD_2112 },
134         { "2112A",      AR5K_VERSION_RAD,       AR5K_SREV_RAD_2112A },
135         { "SChip",      AR5K_VERSION_RAD,       AR5K_SREV_RAD_SC1 },
136         { "SChip",      AR5K_VERSION_RAD,       AR5K_SREV_RAD_SC2 },
137         { "5133",       AR5K_VERSION_RAD,       AR5K_SREV_RAD_5133 },
138         { "xxxxx",      AR5K_VERSION_RAD,       AR5K_SREV_UNKNOWN },
139 };
140
141 /*
142  * Prototypes - PCI stack related functions
143  */
144 static int __devinit    ath5k_pci_probe(struct pci_dev *pdev,
145                                 const struct pci_device_id *id);
146 static void __devexit   ath5k_pci_remove(struct pci_dev *pdev);
147 #ifdef CONFIG_PM
148 static int              ath5k_pci_suspend(struct pci_dev *pdev,
149                                         pm_message_t state);
150 static int              ath5k_pci_resume(struct pci_dev *pdev);
151 #else
152 #define ath5k_pci_suspend NULL
153 #define ath5k_pci_resume NULL
154 #endif /* CONFIG_PM */
155
156 static struct pci_driver ath5k_pci_driver = {
157         .name           = "ath5k_pci",
158         .id_table       = ath5k_pci_id_table,
159         .probe          = ath5k_pci_probe,
160         .remove         = __devexit_p(ath5k_pci_remove),
161         .suspend        = ath5k_pci_suspend,
162         .resume         = ath5k_pci_resume,
163 };
164
165
166
167 /*
168  * Prototypes - MAC 802.11 stack related functions
169  */
170 static int ath5k_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
171                 struct ieee80211_tx_control *ctl);
172 static int ath5k_reset(struct ieee80211_hw *hw);
173 static int ath5k_start(struct ieee80211_hw *hw);
174 static void ath5k_stop(struct ieee80211_hw *hw);
175 static int ath5k_add_interface(struct ieee80211_hw *hw,
176                 struct ieee80211_if_init_conf *conf);
177 static void ath5k_remove_interface(struct ieee80211_hw *hw,
178                 struct ieee80211_if_init_conf *conf);
179 static int ath5k_config(struct ieee80211_hw *hw,
180                 struct ieee80211_conf *conf);
181 static int ath5k_config_interface(struct ieee80211_hw *hw,
182                 struct ieee80211_vif *vif,
183                 struct ieee80211_if_conf *conf);
184 static void ath5k_configure_filter(struct ieee80211_hw *hw,
185                 unsigned int changed_flags,
186                 unsigned int *new_flags,
187                 int mc_count, struct dev_mc_list *mclist);
188 static int ath5k_set_key(struct ieee80211_hw *hw,
189                 enum set_key_cmd cmd,
190                 const u8 *local_addr, const u8 *addr,
191                 struct ieee80211_key_conf *key);
192 static int ath5k_get_stats(struct ieee80211_hw *hw,
193                 struct ieee80211_low_level_stats *stats);
194 static int ath5k_get_tx_stats(struct ieee80211_hw *hw,
195                 struct ieee80211_tx_queue_stats *stats);
196 static u64 ath5k_get_tsf(struct ieee80211_hw *hw);
197 static void ath5k_reset_tsf(struct ieee80211_hw *hw);
198 static int ath5k_beacon_update(struct ieee80211_hw *hw,
199                 struct sk_buff *skb,
200                 struct ieee80211_tx_control *ctl);
201
202 static struct ieee80211_ops ath5k_hw_ops = {
203         .tx             = ath5k_tx,
204         .start          = ath5k_start,
205         .stop           = ath5k_stop,
206         .add_interface  = ath5k_add_interface,
207         .remove_interface = ath5k_remove_interface,
208         .config         = ath5k_config,
209         .config_interface = ath5k_config_interface,
210         .configure_filter = ath5k_configure_filter,
211         .set_key        = ath5k_set_key,
212         .get_stats      = ath5k_get_stats,
213         .conf_tx        = NULL,
214         .get_tx_stats   = ath5k_get_tx_stats,
215         .get_tsf        = ath5k_get_tsf,
216         .reset_tsf      = ath5k_reset_tsf,
217         .beacon_update  = ath5k_beacon_update,
218 };
219
220 /*
221  * Prototypes - Internal functions
222  */
223 /* Attach detach */
224 static int      ath5k_attach(struct pci_dev *pdev,
225                         struct ieee80211_hw *hw);
226 static void     ath5k_detach(struct pci_dev *pdev,
227                         struct ieee80211_hw *hw);
228 /* Channel/mode setup */
229 static inline short ath5k_ieee2mhz(short chan);
230 static unsigned int ath5k_copy_rates(struct ieee80211_rate *rates,
231                                 const struct ath5k_rate_table *rt,
232                                 unsigned int max);
233 static unsigned int ath5k_copy_channels(struct ath5k_hw *ah,
234                                 struct ieee80211_channel *channels,
235                                 unsigned int mode,
236                                 unsigned int max);
237 static int      ath5k_getchannels(struct ieee80211_hw *hw);
238 static int      ath5k_chan_set(struct ath5k_softc *sc,
239                                 struct ieee80211_channel *chan);
240 static void     ath5k_setcurmode(struct ath5k_softc *sc,
241                                 unsigned int mode);
242 static void     ath5k_mode_setup(struct ath5k_softc *sc);
243 /* Descriptor setup */
244 static int      ath5k_desc_alloc(struct ath5k_softc *sc,
245                                 struct pci_dev *pdev);
246 static void     ath5k_desc_free(struct ath5k_softc *sc,
247                                 struct pci_dev *pdev);
248 /* Buffers setup */
249 static int      ath5k_rxbuf_setup(struct ath5k_softc *sc,
250                                 struct ath5k_buf *bf);
251 static int      ath5k_txbuf_setup(struct ath5k_softc *sc,
252                                 struct ath5k_buf *bf,
253                                 struct ieee80211_tx_control *ctl);
254
255 static inline void ath5k_txbuf_free(struct ath5k_softc *sc,
256                                 struct ath5k_buf *bf)
257 {
258         BUG_ON(!bf);
259         if (!bf->skb)
260                 return;
261         pci_unmap_single(sc->pdev, bf->skbaddr, bf->skb->len,
262                         PCI_DMA_TODEVICE);
263         dev_kfree_skb(bf->skb);
264         bf->skb = NULL;
265 }
266
267 /* Queues setup */
268 static struct   ath5k_txq *ath5k_txq_setup(struct ath5k_softc *sc,
269                                 int qtype, int subtype);
270 static int      ath5k_beaconq_setup(struct ath5k_hw *ah);
271 static int      ath5k_beaconq_config(struct ath5k_softc *sc);
272 static void     ath5k_txq_drainq(struct ath5k_softc *sc,
273                                 struct ath5k_txq *txq);
274 static void     ath5k_txq_cleanup(struct ath5k_softc *sc);
275 static void     ath5k_txq_release(struct ath5k_softc *sc);
276 /* Rx handling */
277 static int      ath5k_rx_start(struct ath5k_softc *sc);
278 static void     ath5k_rx_stop(struct ath5k_softc *sc);
279 static unsigned int ath5k_rx_decrypted(struct ath5k_softc *sc,
280                                         struct ath5k_desc *ds,
281                                         struct sk_buff *skb);
282 static void     ath5k_tasklet_rx(unsigned long data);
283 /* Tx handling */
284 static void     ath5k_tx_processq(struct ath5k_softc *sc,
285                                 struct ath5k_txq *txq);
286 static void     ath5k_tasklet_tx(unsigned long data);
287 /* Beacon handling */
288 static int      ath5k_beacon_setup(struct ath5k_softc *sc,
289                                 struct ath5k_buf *bf,
290                                 struct ieee80211_tx_control *ctl);
291 static void     ath5k_beacon_send(struct ath5k_softc *sc);
292 static void     ath5k_beacon_config(struct ath5k_softc *sc);
293 static void     ath5k_beacon_update_timers(struct ath5k_softc *sc, u64 bc_tsf);
294
295 static inline u64 ath5k_extend_tsf(struct ath5k_hw *ah, u32 rstamp)
296 {
297         u64 tsf = ath5k_hw_get_tsf64(ah);
298
299         if ((tsf & 0x7fff) < rstamp)
300                 tsf -= 0x8000;
301
302         return (tsf & ~0x7fff) | rstamp;
303 }
304
305 /* Interrupt handling */
306 static int      ath5k_init(struct ath5k_softc *sc);
307 static int      ath5k_stop_locked(struct ath5k_softc *sc);
308 static int      ath5k_stop_hw(struct ath5k_softc *sc);
309 static irqreturn_t ath5k_intr(int irq, void *dev_id);
310 static void     ath5k_tasklet_reset(unsigned long data);
311
312 static void     ath5k_calibrate(unsigned long data);
313 /* LED functions */
314 static void     ath5k_led_off(unsigned long data);
315 static void     ath5k_led_blink(struct ath5k_softc *sc,
316                                 unsigned int on,
317                                 unsigned int off);
318 static void     ath5k_led_event(struct ath5k_softc *sc,
319                                 int event);
320
321
322 /*
323  * Module init/exit functions
324  */
325 static int __init
326 init_ath5k_pci(void)
327 {
328         int ret;
329
330         ath5k_debug_init();
331
332         ret = pci_register_driver(&ath5k_pci_driver);
333         if (ret) {
334                 printk(KERN_ERR "ath5k_pci: can't register pci driver\n");
335                 return ret;
336         }
337
338         return 0;
339 }
340
341 static void __exit
342 exit_ath5k_pci(void)
343 {
344         pci_unregister_driver(&ath5k_pci_driver);
345
346         ath5k_debug_finish();
347 }
348
349 module_init(init_ath5k_pci);
350 module_exit(exit_ath5k_pci);
351
352
353 /********************\
354 * PCI Initialization *
355 \********************/
356
357 static const char *
358 ath5k_chip_name(enum ath5k_srev_type type, u_int16_t val)
359 {
360         const char *name = "xxxxx";
361         unsigned int i;
362
363         for (i = 0; i < ARRAY_SIZE(srev_names); i++) {
364                 if (srev_names[i].sr_type != type)
365                         continue;
366                 if ((val & 0xff) < srev_names[i + 1].sr_val) {
367                         name = srev_names[i].sr_name;
368                         break;
369                 }
370         }
371
372         return name;
373 }
374
375 static int __devinit
376 ath5k_pci_probe(struct pci_dev *pdev,
377                 const struct pci_device_id *id)
378 {
379         void __iomem *mem;
380         struct ath5k_softc *sc;
381         struct ieee80211_hw *hw;
382         int ret;
383         u8 csz;
384
385         ret = pci_enable_device(pdev);
386         if (ret) {
387                 dev_err(&pdev->dev, "can't enable device\n");
388                 goto err;
389         }
390
391         /* XXX 32-bit addressing only */
392         ret = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
393         if (ret) {
394                 dev_err(&pdev->dev, "32-bit DMA not available\n");
395                 goto err_dis;
396         }
397
398         /*
399          * Cache line size is used to size and align various
400          * structures used to communicate with the hardware.
401          */
402         pci_read_config_byte(pdev, PCI_CACHE_LINE_SIZE, &csz);
403         if (csz == 0) {
404                 /*
405                  * Linux 2.4.18 (at least) writes the cache line size
406                  * register as a 16-bit wide register which is wrong.
407                  * We must have this setup properly for rx buffer
408                  * DMA to work so force a reasonable value here if it
409                  * comes up zero.
410                  */
411                 csz = L1_CACHE_BYTES / sizeof(u32);
412                 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, csz);
413         }
414         /*
415          * The default setting of latency timer yields poor results,
416          * set it to the value used by other systems.  It may be worth
417          * tweaking this setting more.
418          */
419         pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0xa8);
420
421         /* Enable bus mastering */
422         pci_set_master(pdev);
423
424         /*
425          * Disable the RETRY_TIMEOUT register (0x41) to keep
426          * PCI Tx retries from interfering with C3 CPU state.
427          */
428         pci_write_config_byte(pdev, 0x41, 0);
429
430         ret = pci_request_region(pdev, 0, "ath5k");
431         if (ret) {
432                 dev_err(&pdev->dev, "cannot reserve PCI memory region\n");
433                 goto err_dis;
434         }
435
436         mem = pci_iomap(pdev, 0, 0);
437         if (!mem) {
438                 dev_err(&pdev->dev, "cannot remap PCI memory region\n") ;
439                 ret = -EIO;
440                 goto err_reg;
441         }
442
443         /*
444          * Allocate hw (mac80211 main struct)
445          * and hw->priv (driver private data)
446          */
447         hw = ieee80211_alloc_hw(sizeof(*sc), &ath5k_hw_ops);
448         if (hw == NULL) {
449                 dev_err(&pdev->dev, "cannot allocate ieee80211_hw\n");
450                 ret = -ENOMEM;
451                 goto err_map;
452         }
453
454         dev_info(&pdev->dev, "registered as '%s'\n", wiphy_name(hw->wiphy));
455
456         /* Initialize driver private data */
457         SET_IEEE80211_DEV(hw, &pdev->dev);
458         hw->flags = IEEE80211_HW_RX_INCLUDES_FCS;
459         hw->extra_tx_headroom = 2;
460         hw->channel_change_time = 5000;
461         /* these names are misleading */
462         hw->max_rssi = -110; /* signal in dBm */
463         hw->max_noise = -110; /* noise in dBm */
464         hw->max_signal = 100; /* we will provide a percentage based on rssi */
465         sc = hw->priv;
466         sc->hw = hw;
467         sc->pdev = pdev;
468
469         ath5k_debug_init_device(sc);
470
471         /*
472          * Mark the device as detached to avoid processing
473          * interrupts until setup is complete.
474          */
475         __set_bit(ATH_STAT_INVALID, sc->status);
476
477         sc->iobase = mem; /* So we can unmap it on detach */
478         sc->cachelsz = csz * sizeof(u32); /* convert to bytes */
479         sc->opmode = IEEE80211_IF_TYPE_STA;
480         mutex_init(&sc->lock);
481         spin_lock_init(&sc->rxbuflock);
482         spin_lock_init(&sc->txbuflock);
483
484         /* Set private data */
485         pci_set_drvdata(pdev, hw);
486
487         /* Enable msi for devices that support it */
488         pci_enable_msi(pdev);
489
490         /* Setup interrupt handler */
491         ret = request_irq(pdev->irq, ath5k_intr, IRQF_SHARED, "ath", sc);
492         if (ret) {
493                 ATH5K_ERR(sc, "request_irq failed\n");
494                 goto err_free;
495         }
496
497         /* Initialize device */
498         sc->ah = ath5k_hw_attach(sc, id->driver_data);
499         if (IS_ERR(sc->ah)) {
500                 ret = PTR_ERR(sc->ah);
501                 goto err_irq;
502         }
503
504         /* Finish private driver data initialization */
505         ret = ath5k_attach(pdev, hw);
506         if (ret)
507                 goto err_ah;
508
509         ATH5K_INFO(sc, "Atheros AR%s chip found (MAC: 0x%x, PHY: 0x%x)\n",
510                         ath5k_chip_name(AR5K_VERSION_VER,sc->ah->ah_mac_srev),
511                                         sc->ah->ah_mac_srev,
512                                         sc->ah->ah_phy_revision);
513
514         if(!sc->ah->ah_single_chip){
515                 /* Single chip radio (!RF5111) */
516                 if(sc->ah->ah_radio_5ghz_revision && !sc->ah->ah_radio_2ghz_revision) {
517                         /* No 5GHz support -> report 2GHz radio */
518                         if(!test_bit(MODE_IEEE80211A, sc->ah->ah_capabilities.cap_mode)){
519                                 ATH5K_INFO(sc, "RF%s 2GHz radio found (0x%x)\n",
520                                         ath5k_chip_name(AR5K_VERSION_RAD,sc->ah->ah_radio_5ghz_revision),
521                                                         sc->ah->ah_radio_5ghz_revision);
522                         /* No 2GHz support (5110 and some 5Ghz only cards) -> report 5Ghz radio */
523                         } else if(!test_bit(MODE_IEEE80211B, sc->ah->ah_capabilities.cap_mode)){
524                                 ATH5K_INFO(sc, "RF%s 5GHz radio found (0x%x)\n",
525                                         ath5k_chip_name(AR5K_VERSION_RAD,sc->ah->ah_radio_5ghz_revision),
526                                                         sc->ah->ah_radio_5ghz_revision);
527                         /* Multiband radio */
528                         } else {
529                                 ATH5K_INFO(sc, "RF%s multiband radio found"
530                                         " (0x%x)\n",
531                                         ath5k_chip_name(AR5K_VERSION_RAD,sc->ah->ah_radio_5ghz_revision),
532                                                         sc->ah->ah_radio_5ghz_revision);
533                         }
534                 }
535                 /* Multi chip radio (RF5111 - RF2111) -> report both 2GHz/5GHz radios */
536                 else if(sc->ah->ah_radio_5ghz_revision && sc->ah->ah_radio_2ghz_revision){
537                         ATH5K_INFO(sc, "RF%s 5GHz radio found (0x%x)\n",
538                                 ath5k_chip_name(AR5K_VERSION_RAD,sc->ah->ah_radio_5ghz_revision),
539                                                 sc->ah->ah_radio_5ghz_revision);
540                         ATH5K_INFO(sc, "RF%s 2GHz radio found (0x%x)\n",
541                                 ath5k_chip_name(AR5K_VERSION_RAD,sc->ah->ah_radio_2ghz_revision),
542                                                 sc->ah->ah_radio_2ghz_revision);
543                 }
544         }
545
546
547         /* ready to process interrupts */
548         __clear_bit(ATH_STAT_INVALID, sc->status);
549
550         return 0;
551 err_ah:
552         ath5k_hw_detach(sc->ah);
553 err_irq:
554         free_irq(pdev->irq, sc);
555 err_free:
556         pci_disable_msi(pdev);
557         ieee80211_free_hw(hw);
558 err_map:
559         pci_iounmap(pdev, mem);
560 err_reg:
561         pci_release_region(pdev, 0);
562 err_dis:
563         pci_disable_device(pdev);
564 err:
565         return ret;
566 }
567
568 static void __devexit
569 ath5k_pci_remove(struct pci_dev *pdev)
570 {
571         struct ieee80211_hw *hw = pci_get_drvdata(pdev);
572         struct ath5k_softc *sc = hw->priv;
573
574         ath5k_debug_finish_device(sc);
575         ath5k_detach(pdev, hw);
576         ath5k_hw_detach(sc->ah);
577         free_irq(pdev->irq, sc);
578         pci_disable_msi(pdev);
579         pci_iounmap(pdev, sc->iobase);
580         pci_release_region(pdev, 0);
581         pci_disable_device(pdev);
582         ieee80211_free_hw(hw);
583 }
584
585 #ifdef CONFIG_PM
586 static int
587 ath5k_pci_suspend(struct pci_dev *pdev, pm_message_t state)
588 {
589         struct ieee80211_hw *hw = pci_get_drvdata(pdev);
590         struct ath5k_softc *sc = hw->priv;
591
592         if (test_bit(ATH_STAT_LEDSOFT, sc->status))
593                 ath5k_hw_set_gpio(sc->ah, sc->led_pin, 1);
594
595         ath5k_stop_hw(sc);
596         pci_save_state(pdev);
597         pci_disable_device(pdev);
598         pci_set_power_state(pdev, PCI_D3hot);
599
600         return 0;
601 }
602
603 static int
604 ath5k_pci_resume(struct pci_dev *pdev)
605 {
606         struct ieee80211_hw *hw = pci_get_drvdata(pdev);
607         struct ath5k_softc *sc = hw->priv;
608         struct ath5k_hw *ah = sc->ah;
609         int i, err;
610
611         err = pci_set_power_state(pdev, PCI_D0);
612         if (err)
613                 return err;
614
615         err = pci_enable_device(pdev);
616         if (err)
617                 return err;
618
619         pci_restore_state(pdev);
620         /*
621          * Suspend/Resume resets the PCI configuration space, so we have to
622          * re-disable the RETRY_TIMEOUT register (0x41) to keep
623          * PCI Tx retries from interfering with C3 CPU state
624          */
625         pci_write_config_byte(pdev, 0x41, 0);
626
627         ath5k_init(sc);
628         if (test_bit(ATH_STAT_LEDSOFT, sc->status)) {
629                 ath5k_hw_set_gpio_output(ah, sc->led_pin);
630                 ath5k_hw_set_gpio(ah, sc->led_pin, 0);
631         }
632
633         /*
634          * Reset the key cache since some parts do not
635          * reset the contents on initial power up or resume.
636          *
637          * FIXME: This may need to be revisited when mac80211 becomes
638          *        aware of suspend/resume.
639          */
640         for (i = 0; i < AR5K_KEYTABLE_SIZE; i++)
641                 ath5k_hw_reset_key(ah, i);
642
643         return 0;
644 }
645 #endif /* CONFIG_PM */
646
647
648
649 /***********************\
650 * Driver Initialization *
651 \***********************/
652
653 static int
654 ath5k_attach(struct pci_dev *pdev, struct ieee80211_hw *hw)
655 {
656         struct ath5k_softc *sc = hw->priv;
657         struct ath5k_hw *ah = sc->ah;
658         u8 mac[ETH_ALEN];
659         unsigned int i;
660         int ret;
661
662         ATH5K_DBG(sc, ATH5K_DEBUG_ANY, "devid 0x%x\n", pdev->device);
663
664         /*
665          * Check if the MAC has multi-rate retry support.
666          * We do this by trying to setup a fake extended
667          * descriptor.  MAC's that don't have support will
668          * return false w/o doing anything.  MAC's that do
669          * support it will return true w/o doing anything.
670          */
671         ret = ah->ah_setup_xtx_desc(ah, NULL, 0, 0, 0, 0, 0, 0);
672         if (ret < 0)
673                 goto err;
674         if (ret > 0)
675                 __set_bit(ATH_STAT_MRRETRY, sc->status);
676
677         /*
678          * Reset the key cache since some parts do not
679          * reset the contents on initial power up.
680          */
681         for (i = 0; i < AR5K_KEYTABLE_SIZE; i++)
682                 ath5k_hw_reset_key(ah, i);
683
684         /*
685          * Collect the channel list.  The 802.11 layer
686          * is resposible for filtering this list based
687          * on settings like the phy mode and regulatory
688          * domain restrictions.
689          */
690         ret = ath5k_getchannels(hw);
691         if (ret) {
692                 ATH5K_ERR(sc, "can't get channels\n");
693                 goto err;
694         }
695
696         /* NB: setup here so ath5k_rate_update is happy */
697         if (test_bit(MODE_IEEE80211A, ah->ah_modes))
698                 ath5k_setcurmode(sc, MODE_IEEE80211A);
699         else
700                 ath5k_setcurmode(sc, MODE_IEEE80211B);
701
702         /*
703          * Allocate tx+rx descriptors and populate the lists.
704          */
705         ret = ath5k_desc_alloc(sc, pdev);
706         if (ret) {
707                 ATH5K_ERR(sc, "can't allocate descriptors\n");
708                 goto err;
709         }
710
711         /*
712          * Allocate hardware transmit queues: one queue for
713          * beacon frames and one data queue for each QoS
714          * priority.  Note that hw functions handle reseting
715          * these queues at the needed time.
716          */
717         ret = ath5k_beaconq_setup(ah);
718         if (ret < 0) {
719                 ATH5K_ERR(sc, "can't setup a beacon xmit queue\n");
720                 goto err_desc;
721         }
722         sc->bhalq = ret;
723
724         sc->txq = ath5k_txq_setup(sc, AR5K_TX_QUEUE_DATA, AR5K_WME_AC_BK);
725         if (IS_ERR(sc->txq)) {
726                 ATH5K_ERR(sc, "can't setup xmit queue\n");
727                 ret = PTR_ERR(sc->txq);
728                 goto err_bhal;
729         }
730
731         tasklet_init(&sc->rxtq, ath5k_tasklet_rx, (unsigned long)sc);
732         tasklet_init(&sc->txtq, ath5k_tasklet_tx, (unsigned long)sc);
733         tasklet_init(&sc->restq, ath5k_tasklet_reset, (unsigned long)sc);
734         setup_timer(&sc->calib_tim, ath5k_calibrate, (unsigned long)sc);
735         setup_timer(&sc->led_tim, ath5k_led_off, (unsigned long)sc);
736
737         sc->led_on = 0; /* low true */
738         /*
739          * Auto-enable soft led processing for IBM cards and for
740          * 5211 minipci cards.
741          */
742         if (pdev->device == PCI_DEVICE_ID_ATHEROS_AR5212_IBM ||
743                         pdev->device == PCI_DEVICE_ID_ATHEROS_AR5211) {
744                 __set_bit(ATH_STAT_LEDSOFT, sc->status);
745                 sc->led_pin = 0;
746         }
747         /* Enable softled on PIN1 on HP Compaq nc6xx, nc4000 & nx5000 laptops */
748         if (pdev->subsystem_vendor == PCI_VENDOR_ID_COMPAQ) {
749                 __set_bit(ATH_STAT_LEDSOFT, sc->status);
750                 sc->led_pin = 0;
751         }
752         if (test_bit(ATH_STAT_LEDSOFT, sc->status)) {
753                 ath5k_hw_set_gpio_output(ah, sc->led_pin);
754                 ath5k_hw_set_gpio(ah, sc->led_pin, !sc->led_on);
755         }
756
757         ath5k_hw_get_lladdr(ah, mac);
758         SET_IEEE80211_PERM_ADDR(hw, mac);
759         /* All MAC address bits matter for ACKs */
760         memset(sc->bssidmask, 0xff, ETH_ALEN);
761         ath5k_hw_set_bssid_mask(sc->ah, sc->bssidmask);
762
763         ret = ieee80211_register_hw(hw);
764         if (ret) {
765                 ATH5K_ERR(sc, "can't register ieee80211 hw\n");
766                 goto err_queues;
767         }
768
769         return 0;
770 err_queues:
771         ath5k_txq_release(sc);
772 err_bhal:
773         ath5k_hw_release_tx_queue(ah, sc->bhalq);
774 err_desc:
775         ath5k_desc_free(sc, pdev);
776 err:
777         return ret;
778 }
779
780 static void
781 ath5k_detach(struct pci_dev *pdev, struct ieee80211_hw *hw)
782 {
783         struct ath5k_softc *sc = hw->priv;
784
785         /*
786          * NB: the order of these is important:
787          * o call the 802.11 layer before detaching ath5k_hw to
788          *   insure callbacks into the driver to delete global
789          *   key cache entries can be handled
790          * o reclaim the tx queue data structures after calling
791          *   the 802.11 layer as we'll get called back to reclaim
792          *   node state and potentially want to use them
793          * o to cleanup the tx queues the hal is called, so detach
794          *   it last
795          * XXX: ??? detach ath5k_hw ???
796          * Other than that, it's straightforward...
797          */
798         ieee80211_unregister_hw(hw);
799         ath5k_desc_free(sc, pdev);
800         ath5k_txq_release(sc);
801         ath5k_hw_release_tx_queue(sc->ah, sc->bhalq);
802
803         /*
804          * NB: can't reclaim these until after ieee80211_ifdetach
805          * returns because we'll get called back to reclaim node
806          * state and potentially want to use them.
807          */
808 }
809
810
811
812
813 /********************\
814 * Channel/mode setup *
815 \********************/
816
817 /*
818  * Convert IEEE channel number to MHz frequency.
819  */
820 static inline short
821 ath5k_ieee2mhz(short chan)
822 {
823         if (chan <= 14 || chan >= 27)
824                 return ieee80211chan2mhz(chan);
825         else
826                 return 2212 + chan * 20;
827 }
828
829 static unsigned int
830 ath5k_copy_rates(struct ieee80211_rate *rates,
831                 const struct ath5k_rate_table *rt,
832                 unsigned int max)
833 {
834         unsigned int i, count;
835
836         if (rt == NULL)
837                 return 0;
838
839         for (i = 0, count = 0; i < rt->rate_count && max > 0; i++) {
840                 if (!rt->rates[i].valid)
841                         continue;
842                 rates->rate = rt->rates[i].rate_kbps / 100;
843                 rates->val = rt->rates[i].rate_code;
844                 rates->flags = rt->rates[i].modulation;
845                 rates++;
846                 count++;
847                 max--;
848         }
849
850         return count;
851 }
852
853 static unsigned int
854 ath5k_copy_channels(struct ath5k_hw *ah,
855                 struct ieee80211_channel *channels,
856                 unsigned int mode,
857                 unsigned int max)
858 {
859         static const struct { unsigned int mode, mask, chan; } map[] = {
860                 [MODE_IEEE80211A] = { CHANNEL_OFDM, CHANNEL_OFDM | CHANNEL_TURBO, CHANNEL_A },
861                 [MODE_ATHEROS_TURBO] = { CHANNEL_OFDM|CHANNEL_TURBO, CHANNEL_OFDM | CHANNEL_TURBO, CHANNEL_T },
862                 [MODE_IEEE80211B] = { CHANNEL_CCK, CHANNEL_CCK, CHANNEL_B },
863                 [MODE_IEEE80211G] = { CHANNEL_OFDM, CHANNEL_OFDM, CHANNEL_G },
864                 [MODE_ATHEROS_TURBOG] = { CHANNEL_OFDM | CHANNEL_TURBO, CHANNEL_OFDM | CHANNEL_TURBO, CHANNEL_TG },
865         };
866         static const struct ath5k_regchannel chans_2ghz[] =
867                 IEEE80211_CHANNELS_2GHZ;
868         static const struct ath5k_regchannel chans_5ghz[] =
869                 IEEE80211_CHANNELS_5GHZ;
870         const struct ath5k_regchannel *chans;
871         enum ath5k_regdom dmn;
872         unsigned int i, count, size, chfreq, all, f, ch;
873
874         if (!test_bit(mode, ah->ah_modes))
875                 return 0;
876
877         all = ah->ah_regdomain == DMN_DEFAULT || CHAN_DEBUG == 1;
878
879         switch (mode) {
880         case MODE_IEEE80211A:
881         case MODE_ATHEROS_TURBO:
882                 /* 1..220, but 2GHz frequencies are filtered by check_channel */
883                 size = all ? 220 : ARRAY_SIZE(chans_5ghz);
884                 chans = chans_5ghz;
885                 dmn = ath5k_regdom2flag(ah->ah_regdomain,
886                                 IEEE80211_CHANNELS_5GHZ_MIN);
887                 chfreq = CHANNEL_5GHZ;
888                 break;
889         case MODE_IEEE80211B:
890         case MODE_IEEE80211G:
891         case MODE_ATHEROS_TURBOG:
892                 size = all ? 26 : ARRAY_SIZE(chans_2ghz);
893                 chans = chans_2ghz;
894                 dmn = ath5k_regdom2flag(ah->ah_regdomain,
895                                 IEEE80211_CHANNELS_2GHZ_MIN);
896                 chfreq = CHANNEL_2GHZ;
897                 break;
898         default:
899                 ATH5K_WARN(ah->ah_sc, "bad mode, not copying channels\n");
900                 return 0;
901         }
902
903         for (i = 0, count = 0; i < size && max > 0; i++) {
904                 ch = all ? i + 1 : chans[i].chan;
905                 f = ath5k_ieee2mhz(ch);
906                 /* Check if channel is supported by the chipset */
907                 if (!ath5k_channel_ok(ah, f, chfreq))
908                         continue;
909
910                 /* Match regulation domain */
911                 if (!all && !(IEEE80211_DMN(chans[i].domain) &
912                                                         IEEE80211_DMN(dmn)))
913                         continue;
914
915                 if (!all && (chans[i].mode & map[mode].mask) != map[mode].mode)
916                         continue;
917
918                 /* Write channel and increment counter */
919                 channels->chan = ch;
920                 channels->freq = f;
921                 channels->val = map[mode].chan;
922                 channels++;
923                 count++;
924                 max--;
925         }
926
927         return count;
928 }
929
930 /* Only tries to register modes our EEPROM says it can support */
931 #define REGISTER_MODE(m) do { \
932         ret = ath5k_register_mode(hw, m); \
933         if (ret) \
934                 return ret; \
935 } while (0) \
936
937 static inline int
938 ath5k_register_mode(struct ieee80211_hw *hw, u8 m)
939 {
940         struct ath5k_softc *sc = hw->priv;
941         struct ieee80211_hw_mode *modes = sc->modes;
942         unsigned int i;
943         int ret;
944
945         if (!test_bit(m, sc->ah->ah_capabilities.cap_mode))
946                 return 0;
947
948         for (i = 0; i < NUM_DRIVER_MODES; i++) {
949                 if (modes[i].mode != m || !modes[i].num_channels)
950                         continue;
951                 ret = ieee80211_register_hwmode(hw, &modes[i]);
952                 if (ret) {
953                         ATH5K_ERR(sc, "can't register hwmode %u\n", m);
954                         return ret;
955                 }
956                 return 0;
957         }
958         BUG();
959 }
960
961 static int
962 ath5k_getchannels(struct ieee80211_hw *hw)
963 {
964         struct ath5k_softc *sc = hw->priv;
965         struct ath5k_hw *ah = sc->ah;
966         struct ieee80211_hw_mode *modes = sc->modes;
967         unsigned int i, max_r, max_c;
968         int ret;
969
970         BUILD_BUG_ON(ARRAY_SIZE(sc->modes) < 3);
971
972         /* The order here does not matter */
973         modes[0].mode = MODE_IEEE80211G;
974         modes[1].mode = MODE_IEEE80211B;
975         modes[2].mode = MODE_IEEE80211A;
976
977         max_r = ARRAY_SIZE(sc->rates);
978         max_c = ARRAY_SIZE(sc->channels);
979
980         for (i = 0; i < NUM_DRIVER_MODES; i++) {
981                 struct ieee80211_hw_mode *mode = &modes[i];
982                 const struct ath5k_rate_table *hw_rates;
983
984                 if (i == 0) {
985                         modes[0].rates  = sc->rates;
986                         modes->channels = sc->channels;
987                 } else {
988                         struct ieee80211_hw_mode *prev_mode = &modes[i-1];
989                         int prev_num_r  = prev_mode->num_rates;
990                         int prev_num_c  = prev_mode->num_channels;
991                         mode->rates     = &prev_mode->rates[prev_num_r];
992                         mode->channels  = &prev_mode->channels[prev_num_c];
993                 }
994
995                 hw_rates = ath5k_hw_get_rate_table(ah, mode->mode);
996                 mode->num_rates    = ath5k_copy_rates(mode->rates, hw_rates,
997                         max_r);
998                 mode->num_channels = ath5k_copy_channels(ah, mode->channels,
999                         mode->mode, max_c);
1000                 max_r -= mode->num_rates;
1001                 max_c -= mode->num_channels;
1002         }
1003
1004         /* We try to register all modes this driver supports. We don't bother
1005          * with MODE_IEEE80211B for AR5212 as MODE_IEEE80211G already accounts
1006          * for that as per mac80211. Then, REGISTER_MODE() will will actually
1007          * check the eeprom reading for more reliable capability information.
1008          * Order matters here as per mac80211's latest preference. This will
1009          * all hopefullly soon go away. */
1010
1011         REGISTER_MODE(MODE_IEEE80211G);
1012         if (ah->ah_version != AR5K_AR5212)
1013                 REGISTER_MODE(MODE_IEEE80211B);
1014         REGISTER_MODE(MODE_IEEE80211A);
1015
1016         ath5k_debug_dump_modes(sc, modes);
1017
1018         return ret;
1019 }
1020
1021 /*
1022  * Set/change channels.  If the channel is really being changed,
1023  * it's done by reseting the chip.  To accomplish this we must
1024  * first cleanup any pending DMA, then restart stuff after a la
1025  * ath5k_init.
1026  */
1027 static int
1028 ath5k_chan_set(struct ath5k_softc *sc, struct ieee80211_channel *chan)
1029 {
1030         struct ath5k_hw *ah = sc->ah;
1031         int ret;
1032
1033         ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "%u (%u MHz) -> %u (%u MHz)\n",
1034                 sc->curchan->chan, sc->curchan->freq,
1035                 chan->chan, chan->freq);
1036
1037         if (chan->freq != sc->curchan->freq || chan->val != sc->curchan->val) {
1038                 /*
1039                  * To switch channels clear any pending DMA operations;
1040                  * wait long enough for the RX fifo to drain, reset the
1041                  * hardware at the new frequency, and then re-enable
1042                  * the relevant bits of the h/w.
1043                  */
1044                 ath5k_hw_set_intr(ah, 0);       /* disable interrupts */
1045                 ath5k_txq_cleanup(sc);          /* clear pending tx frames */
1046                 ath5k_rx_stop(sc);              /* turn off frame recv */
1047                 ret = ath5k_hw_reset(ah, sc->opmode, chan, true);
1048                 if (ret) {
1049                         ATH5K_ERR(sc, "%s: unable to reset channel %u "
1050                                 "(%u Mhz)\n", __func__, chan->chan, chan->freq);
1051                         return ret;
1052                 }
1053                 sc->curchan = chan;
1054                 ath5k_hw_set_txpower_limit(sc->ah, 0);
1055
1056                 /*
1057                  * Re-enable rx framework.
1058                  */
1059                 ret = ath5k_rx_start(sc);
1060                 if (ret) {
1061                         ATH5K_ERR(sc, "%s: unable to restart recv logic\n",
1062                                         __func__);
1063                         return ret;
1064                 }
1065
1066                 /*
1067                  * Change channels and update the h/w rate map
1068                  * if we're switching; e.g. 11a to 11b/g.
1069                  *
1070                  * XXX needed?
1071                  */
1072 /*              ath5k_chan_change(sc, chan); */
1073
1074                 ath5k_beacon_config(sc);
1075                 /*
1076                  * Re-enable interrupts.
1077                  */
1078                 ath5k_hw_set_intr(ah, sc->imask);
1079         }
1080
1081         return 0;
1082 }
1083
1084 static void
1085 ath5k_setcurmode(struct ath5k_softc *sc, unsigned int mode)
1086 {
1087         if (unlikely(test_bit(ATH_STAT_LEDSOFT, sc->status))) {
1088                 /* from Atheros NDIS driver, w/ permission */
1089                 static const struct {
1090                         u16 rate;       /* tx/rx 802.11 rate */
1091                         u16 timeOn;     /* LED on time (ms) */
1092                         u16 timeOff;    /* LED off time (ms) */
1093                 } blinkrates[] = {
1094                         { 108,  40,  10 },
1095                         {  96,  44,  11 },
1096                         {  72,  50,  13 },
1097                         {  48,  57,  14 },
1098                         {  36,  67,  16 },
1099                         {  24,  80,  20 },
1100                         {  22, 100,  25 },
1101                         {  18, 133,  34 },
1102                         {  12, 160,  40 },
1103                         {  10, 200,  50 },
1104                         {   6, 240,  58 },
1105                         {   4, 267,  66 },
1106                         {   2, 400, 100 },
1107                         {   0, 500, 130 }
1108                 };
1109                 const struct ath5k_rate_table *rt =
1110                                 ath5k_hw_get_rate_table(sc->ah, mode);
1111                 unsigned int i, j;
1112
1113                 BUG_ON(rt == NULL);
1114
1115                 memset(sc->hwmap, 0, sizeof(sc->hwmap));
1116                 for (i = 0; i < 32; i++) {
1117                         u8 ix = rt->rate_code_to_index[i];
1118                         if (ix == 0xff) {
1119                                 sc->hwmap[i].ledon = msecs_to_jiffies(500);
1120                                 sc->hwmap[i].ledoff = msecs_to_jiffies(130);
1121                                 continue;
1122                         }
1123                         sc->hwmap[i].txflags = IEEE80211_RADIOTAP_F_DATAPAD;
1124                         if (SHPREAMBLE_FLAG(ix) || rt->rates[ix].modulation ==
1125                                         IEEE80211_RATE_OFDM)
1126                                 sc->hwmap[i].txflags |=
1127                                                 IEEE80211_RADIOTAP_F_SHORTPRE;
1128                         /* receive frames include FCS */
1129                         sc->hwmap[i].rxflags = sc->hwmap[i].txflags |
1130                                         IEEE80211_RADIOTAP_F_FCS;
1131                         /* setup blink rate table to avoid per-packet lookup */
1132                         for (j = 0; j < ARRAY_SIZE(blinkrates) - 1; j++)
1133                                 if (blinkrates[j].rate == /* XXX why 7f? */
1134                                                 (rt->rates[ix].dot11_rate&0x7f))
1135                                         break;
1136
1137                         sc->hwmap[i].ledon = msecs_to_jiffies(blinkrates[j].
1138                                         timeOn);
1139                         sc->hwmap[i].ledoff = msecs_to_jiffies(blinkrates[j].
1140                                         timeOff);
1141                 }
1142         }
1143
1144         sc->curmode = mode;
1145 }
1146
1147 static void
1148 ath5k_mode_setup(struct ath5k_softc *sc)
1149 {
1150         struct ath5k_hw *ah = sc->ah;
1151         u32 rfilt;
1152
1153         /* configure rx filter */
1154         rfilt = sc->filter_flags;
1155         ath5k_hw_set_rx_filter(ah, rfilt);
1156
1157         if (ath5k_hw_hasbssidmask(ah))
1158                 ath5k_hw_set_bssid_mask(ah, sc->bssidmask);
1159
1160         /* configure operational mode */
1161         ath5k_hw_set_opmode(ah);
1162
1163         ath5k_hw_set_mcast_filter(ah, 0, 0);
1164         ATH5K_DBG(sc, ATH5K_DEBUG_MODE, "RX filter 0x%x\n", rfilt);
1165 }
1166
1167
1168
1169
1170 /***************\
1171 * Buffers setup *
1172 \***************/
1173
1174 static int
1175 ath5k_rxbuf_setup(struct ath5k_softc *sc, struct ath5k_buf *bf)
1176 {
1177         struct ath5k_hw *ah = sc->ah;
1178         struct sk_buff *skb = bf->skb;
1179         struct ath5k_desc *ds;
1180
1181         if (likely(skb == NULL)) {
1182                 unsigned int off;
1183
1184                 /*
1185                  * Allocate buffer with headroom_needed space for the
1186                  * fake physical layer header at the start.
1187                  */
1188                 skb = dev_alloc_skb(sc->rxbufsize + sc->cachelsz - 1);
1189                 if (unlikely(skb == NULL)) {
1190                         ATH5K_ERR(sc, "can't alloc skbuff of size %u\n",
1191                                         sc->rxbufsize + sc->cachelsz - 1);
1192                         return -ENOMEM;
1193                 }
1194                 /*
1195                  * Cache-line-align.  This is important (for the
1196                  * 5210 at least) as not doing so causes bogus data
1197                  * in rx'd frames.
1198                  */
1199                 off = ((unsigned long)skb->data) % sc->cachelsz;
1200                 if (off != 0)
1201                         skb_reserve(skb, sc->cachelsz - off);
1202
1203                 bf->skb = skb;
1204                 bf->skbaddr = pci_map_single(sc->pdev,
1205                         skb->data, sc->rxbufsize, PCI_DMA_FROMDEVICE);
1206                 if (unlikely(pci_dma_mapping_error(bf->skbaddr))) {
1207                         ATH5K_ERR(sc, "%s: DMA mapping failed\n", __func__);
1208                         dev_kfree_skb(skb);
1209                         bf->skb = NULL;
1210                         return -ENOMEM;
1211                 }
1212         }
1213
1214         /*
1215          * Setup descriptors.  For receive we always terminate
1216          * the descriptor list with a self-linked entry so we'll
1217          * not get overrun under high load (as can happen with a
1218          * 5212 when ANI processing enables PHY error frames).
1219          *
1220          * To insure the last descriptor is self-linked we create
1221          * each descriptor as self-linked and add it to the end.  As
1222          * each additional descriptor is added the previous self-linked
1223          * entry is ``fixed'' naturally.  This should be safe even
1224          * if DMA is happening.  When processing RX interrupts we
1225          * never remove/process the last, self-linked, entry on the
1226          * descriptor list.  This insures the hardware always has
1227          * someplace to write a new frame.
1228          */
1229         ds = bf->desc;
1230         ds->ds_link = bf->daddr;        /* link to self */
1231         ds->ds_data = bf->skbaddr;
1232         ath5k_hw_setup_rx_desc(ah, ds,
1233                 skb_tailroom(skb),      /* buffer size */
1234                 0);
1235
1236         if (sc->rxlink != NULL)
1237                 *sc->rxlink = bf->daddr;
1238         sc->rxlink = &ds->ds_link;
1239         return 0;
1240 }
1241
1242 static int
1243 ath5k_txbuf_setup(struct ath5k_softc *sc, struct ath5k_buf *bf,
1244                 struct ieee80211_tx_control *ctl)
1245 {
1246         struct ath5k_hw *ah = sc->ah;
1247         struct ath5k_txq *txq = sc->txq;
1248         struct ath5k_desc *ds = bf->desc;
1249         struct sk_buff *skb = bf->skb;
1250         unsigned int pktlen, flags, keyidx = AR5K_TXKEYIX_INVALID;
1251         int ret;
1252
1253         flags = AR5K_TXDESC_INTREQ | AR5K_TXDESC_CLRDMASK;
1254         bf->ctl = *ctl;
1255         /* XXX endianness */
1256         bf->skbaddr = pci_map_single(sc->pdev, skb->data, skb->len,
1257                         PCI_DMA_TODEVICE);
1258
1259         if (ctl->flags & IEEE80211_TXCTL_NO_ACK)
1260                 flags |= AR5K_TXDESC_NOACK;
1261
1262         pktlen = skb->len;
1263
1264         if (!(ctl->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT)) {
1265                 keyidx = ctl->key_idx;
1266                 pktlen += ctl->icv_len;
1267         }
1268
1269         ret = ah->ah_setup_tx_desc(ah, ds, pktlen,
1270                 ieee80211_get_hdrlen_from_skb(skb), AR5K_PKT_TYPE_NORMAL,
1271                 (ctl->power_level * 2), ctl->tx_rate, ctl->retry_limit, keyidx, 0, flags, 0, 0);
1272         if (ret)
1273                 goto err_unmap;
1274
1275         ds->ds_link = 0;
1276         ds->ds_data = bf->skbaddr;
1277
1278         spin_lock_bh(&txq->lock);
1279         list_add_tail(&bf->list, &txq->q);
1280         sc->tx_stats.data[txq->qnum].len++;
1281         if (txq->link == NULL) /* is this first packet? */
1282                 ath5k_hw_put_tx_buf(ah, txq->qnum, bf->daddr);
1283         else /* no, so only link it */
1284                 *txq->link = bf->daddr;
1285
1286         txq->link = &ds->ds_link;
1287         ath5k_hw_tx_start(ah, txq->qnum);
1288         spin_unlock_bh(&txq->lock);
1289
1290         return 0;
1291 err_unmap:
1292         pci_unmap_single(sc->pdev, bf->skbaddr, skb->len, PCI_DMA_TODEVICE);
1293         return ret;
1294 }
1295
1296 /*******************\
1297 * Descriptors setup *
1298 \*******************/
1299
1300 static int
1301 ath5k_desc_alloc(struct ath5k_softc *sc, struct pci_dev *pdev)
1302 {
1303         struct ath5k_desc *ds;
1304         struct ath5k_buf *bf;
1305         dma_addr_t da;
1306         unsigned int i;
1307         int ret;
1308
1309         /* allocate descriptors */
1310         sc->desc_len = sizeof(struct ath5k_desc) *
1311                         (ATH_TXBUF + ATH_RXBUF + ATH_BCBUF + 1);
1312         sc->desc = pci_alloc_consistent(pdev, sc->desc_len, &sc->desc_daddr);
1313         if (sc->desc == NULL) {
1314                 ATH5K_ERR(sc, "can't allocate descriptors\n");
1315                 ret = -ENOMEM;
1316                 goto err;
1317         }
1318         ds = sc->desc;
1319         da = sc->desc_daddr;
1320         ATH5K_DBG(sc, ATH5K_DEBUG_ANY, "DMA map: %p (%zu) -> %llx\n",
1321                 ds, sc->desc_len, (unsigned long long)sc->desc_daddr);
1322
1323         bf = kcalloc(1 + ATH_TXBUF + ATH_RXBUF + ATH_BCBUF,
1324                         sizeof(struct ath5k_buf), GFP_KERNEL);
1325         if (bf == NULL) {
1326                 ATH5K_ERR(sc, "can't allocate bufptr\n");
1327                 ret = -ENOMEM;
1328                 goto err_free;
1329         }
1330         sc->bufptr = bf;
1331
1332         INIT_LIST_HEAD(&sc->rxbuf);
1333         for (i = 0; i < ATH_RXBUF; i++, bf++, ds++, da += sizeof(*ds)) {
1334                 bf->desc = ds;
1335                 bf->daddr = da;
1336                 list_add_tail(&bf->list, &sc->rxbuf);
1337         }
1338
1339         INIT_LIST_HEAD(&sc->txbuf);
1340         sc->txbuf_len = ATH_TXBUF;
1341         for (i = 0; i < ATH_TXBUF; i++, bf++, ds++,
1342                         da += sizeof(*ds)) {
1343                 bf->desc = ds;
1344                 bf->daddr = da;
1345                 list_add_tail(&bf->list, &sc->txbuf);
1346         }
1347
1348         /* beacon buffer */
1349         bf->desc = ds;
1350         bf->daddr = da;
1351         sc->bbuf = bf;
1352
1353         return 0;
1354 err_free:
1355         pci_free_consistent(pdev, sc->desc_len, sc->desc, sc->desc_daddr);
1356 err:
1357         sc->desc = NULL;
1358         return ret;
1359 }
1360
1361 static void
1362 ath5k_desc_free(struct ath5k_softc *sc, struct pci_dev *pdev)
1363 {
1364         struct ath5k_buf *bf;
1365
1366         ath5k_txbuf_free(sc, sc->bbuf);
1367         list_for_each_entry(bf, &sc->txbuf, list)
1368                 ath5k_txbuf_free(sc, bf);
1369         list_for_each_entry(bf, &sc->rxbuf, list)
1370                 ath5k_txbuf_free(sc, bf);
1371
1372         /* Free memory associated with all descriptors */
1373         pci_free_consistent(pdev, sc->desc_len, sc->desc, sc->desc_daddr);
1374
1375         kfree(sc->bufptr);
1376         sc->bufptr = NULL;
1377 }
1378
1379
1380
1381
1382
1383 /**************\
1384 * Queues setup *
1385 \**************/
1386
1387 static struct ath5k_txq *
1388 ath5k_txq_setup(struct ath5k_softc *sc,
1389                 int qtype, int subtype)
1390 {
1391         struct ath5k_hw *ah = sc->ah;
1392         struct ath5k_txq *txq;
1393         struct ath5k_txq_info qi = {
1394                 .tqi_subtype = subtype,
1395                 .tqi_aifs = AR5K_TXQ_USEDEFAULT,
1396                 .tqi_cw_min = AR5K_TXQ_USEDEFAULT,
1397                 .tqi_cw_max = AR5K_TXQ_USEDEFAULT
1398         };
1399         int qnum;
1400
1401         /*
1402          * Enable interrupts only for EOL and DESC conditions.
1403          * We mark tx descriptors to receive a DESC interrupt
1404          * when a tx queue gets deep; otherwise waiting for the
1405          * EOL to reap descriptors.  Note that this is done to
1406          * reduce interrupt load and this only defers reaping
1407          * descriptors, never transmitting frames.  Aside from
1408          * reducing interrupts this also permits more concurrency.
1409          * The only potential downside is if the tx queue backs
1410          * up in which case the top half of the kernel may backup
1411          * due to a lack of tx descriptors.
1412          */
1413         qi.tqi_flags = AR5K_TXQ_FLAG_TXEOLINT_ENABLE |
1414                                 AR5K_TXQ_FLAG_TXDESCINT_ENABLE;
1415         qnum = ath5k_hw_setup_tx_queue(ah, qtype, &qi);
1416         if (qnum < 0) {
1417                 /*
1418                  * NB: don't print a message, this happens
1419                  * normally on parts with too few tx queues
1420                  */
1421                 return ERR_PTR(qnum);
1422         }
1423         if (qnum >= ARRAY_SIZE(sc->txqs)) {
1424                 ATH5K_ERR(sc, "hw qnum %u out of range, max %tu!\n",
1425                         qnum, ARRAY_SIZE(sc->txqs));
1426                 ath5k_hw_release_tx_queue(ah, qnum);
1427                 return ERR_PTR(-EINVAL);
1428         }
1429         txq = &sc->txqs[qnum];
1430         if (!txq->setup) {
1431                 txq->qnum = qnum;
1432                 txq->link = NULL;
1433                 INIT_LIST_HEAD(&txq->q);
1434                 spin_lock_init(&txq->lock);
1435                 txq->setup = true;
1436         }
1437         return &sc->txqs[qnum];
1438 }
1439
1440 static int
1441 ath5k_beaconq_setup(struct ath5k_hw *ah)
1442 {
1443         struct ath5k_txq_info qi = {
1444                 .tqi_aifs = AR5K_TXQ_USEDEFAULT,
1445                 .tqi_cw_min = AR5K_TXQ_USEDEFAULT,
1446                 .tqi_cw_max = AR5K_TXQ_USEDEFAULT,
1447                 /* NB: for dynamic turbo, don't enable any other interrupts */
1448                 .tqi_flags = AR5K_TXQ_FLAG_TXDESCINT_ENABLE
1449         };
1450
1451         return ath5k_hw_setup_tx_queue(ah, AR5K_TX_QUEUE_BEACON, &qi);
1452 }
1453
1454 static int
1455 ath5k_beaconq_config(struct ath5k_softc *sc)
1456 {
1457         struct ath5k_hw *ah = sc->ah;
1458         struct ath5k_txq_info qi;
1459         int ret;
1460
1461         ret = ath5k_hw_get_tx_queueprops(ah, sc->bhalq, &qi);
1462         if (ret)
1463                 return ret;
1464         if (sc->opmode == IEEE80211_IF_TYPE_AP) {
1465                 /*
1466                  * Always burst out beacon and CAB traffic
1467                  * (aifs = cwmin = cwmax = 0)
1468                  */
1469                 qi.tqi_aifs = 0;
1470                 qi.tqi_cw_min = 0;
1471                 qi.tqi_cw_max = 0;
1472         } else if (sc->opmode == IEEE80211_IF_TYPE_IBSS) {
1473                 /*
1474                  * Adhoc mode; backoff between 0 and (2 * cw_min).
1475                  */
1476                 qi.tqi_aifs = 0;
1477                 qi.tqi_cw_min = 0;
1478                 qi.tqi_cw_max = 2 * ah->ah_cw_min;
1479         }
1480
1481         ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
1482                 "beacon queueprops tqi_aifs:%d tqi_cw_min:%d tqi_cw_max:%d\n",
1483                 qi.tqi_aifs, qi.tqi_cw_min, qi.tqi_cw_max);
1484
1485         ret = ath5k_hw_setup_tx_queueprops(ah, sc->bhalq, &qi);
1486         if (ret) {
1487                 ATH5K_ERR(sc, "%s: unable to update parameters for beacon "
1488                         "hardware queue!\n", __func__);
1489                 return ret;
1490         }
1491
1492         return ath5k_hw_reset_tx_queue(ah, sc->bhalq); /* push to h/w */;
1493 }
1494
1495 static void
1496 ath5k_txq_drainq(struct ath5k_softc *sc, struct ath5k_txq *txq)
1497 {
1498         struct ath5k_buf *bf, *bf0;
1499
1500         /*
1501          * NB: this assumes output has been stopped and
1502          *     we do not need to block ath5k_tx_tasklet
1503          */
1504         spin_lock_bh(&txq->lock);
1505         list_for_each_entry_safe(bf, bf0, &txq->q, list) {
1506                 ath5k_debug_printtxbuf(sc, bf, !sc->ah->ah_proc_tx_desc(sc->ah,
1507                                         bf->desc));
1508
1509                 ath5k_txbuf_free(sc, bf);
1510
1511                 spin_lock_bh(&sc->txbuflock);
1512                 sc->tx_stats.data[txq->qnum].len--;
1513                 list_move_tail(&bf->list, &sc->txbuf);
1514                 sc->txbuf_len++;
1515                 spin_unlock_bh(&sc->txbuflock);
1516         }
1517         txq->link = NULL;
1518         spin_unlock_bh(&txq->lock);
1519 }
1520
1521 /*
1522  * Drain the transmit queues and reclaim resources.
1523  */
1524 static void
1525 ath5k_txq_cleanup(struct ath5k_softc *sc)
1526 {
1527         struct ath5k_hw *ah = sc->ah;
1528         unsigned int i;
1529
1530         /* XXX return value */
1531         if (likely(!test_bit(ATH_STAT_INVALID, sc->status))) {
1532                 /* don't touch the hardware if marked invalid */
1533                 ath5k_hw_stop_tx_dma(ah, sc->bhalq);
1534                 ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "beacon queue %x\n",
1535                         ath5k_hw_get_tx_buf(ah, sc->bhalq));
1536                 for (i = 0; i < ARRAY_SIZE(sc->txqs); i++)
1537                         if (sc->txqs[i].setup) {
1538                                 ath5k_hw_stop_tx_dma(ah, sc->txqs[i].qnum);
1539                                 ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "txq [%u] %x, "
1540                                         "link %p\n",
1541                                         sc->txqs[i].qnum,
1542                                         ath5k_hw_get_tx_buf(ah,
1543                                                         sc->txqs[i].qnum),
1544                                         sc->txqs[i].link);
1545                         }
1546         }
1547         ieee80211_start_queues(sc->hw); /* XXX move to callers */
1548
1549         for (i = 0; i < ARRAY_SIZE(sc->txqs); i++)
1550                 if (sc->txqs[i].setup)
1551                         ath5k_txq_drainq(sc, &sc->txqs[i]);
1552 }
1553
1554 static void
1555 ath5k_txq_release(struct ath5k_softc *sc)
1556 {
1557         struct ath5k_txq *txq = sc->txqs;
1558         unsigned int i;
1559
1560         for (i = 0; i < ARRAY_SIZE(sc->txqs); i++, txq++)
1561                 if (txq->setup) {
1562                         ath5k_hw_release_tx_queue(sc->ah, txq->qnum);
1563                         txq->setup = false;
1564                 }
1565 }
1566
1567
1568
1569
1570 /*************\
1571 * RX Handling *
1572 \*************/
1573
1574 /*
1575  * Enable the receive h/w following a reset.
1576  */
1577 static int
1578 ath5k_rx_start(struct ath5k_softc *sc)
1579 {
1580         struct ath5k_hw *ah = sc->ah;
1581         struct ath5k_buf *bf;
1582         int ret;
1583
1584         sc->rxbufsize = roundup(IEEE80211_MAX_LEN, sc->cachelsz);
1585
1586         ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "cachelsz %u rxbufsize %u\n",
1587                 sc->cachelsz, sc->rxbufsize);
1588
1589         sc->rxlink = NULL;
1590
1591         spin_lock_bh(&sc->rxbuflock);
1592         list_for_each_entry(bf, &sc->rxbuf, list) {
1593                 ret = ath5k_rxbuf_setup(sc, bf);
1594                 if (ret != 0) {
1595                         spin_unlock_bh(&sc->rxbuflock);
1596                         goto err;
1597                 }
1598         }
1599         bf = list_first_entry(&sc->rxbuf, struct ath5k_buf, list);
1600         spin_unlock_bh(&sc->rxbuflock);
1601
1602         ath5k_hw_put_rx_buf(ah, bf->daddr);
1603         ath5k_hw_start_rx(ah);          /* enable recv descriptors */
1604         ath5k_mode_setup(sc);           /* set filters, etc. */
1605         ath5k_hw_start_rx_pcu(ah);      /* re-enable PCU/DMA engine */
1606
1607         return 0;
1608 err:
1609         return ret;
1610 }
1611
1612 /*
1613  * Disable the receive h/w in preparation for a reset.
1614  */
1615 static void
1616 ath5k_rx_stop(struct ath5k_softc *sc)
1617 {
1618         struct ath5k_hw *ah = sc->ah;
1619
1620         ath5k_hw_stop_pcu_recv(ah);     /* disable PCU */
1621         ath5k_hw_set_rx_filter(ah, 0);  /* clear recv filter */
1622         ath5k_hw_stop_rx_dma(ah);       /* disable DMA engine */
1623         mdelay(3);                      /* 3ms is long enough for 1 frame */
1624
1625         ath5k_debug_printrxbuffs(sc, ah);
1626
1627         sc->rxlink = NULL;              /* just in case */
1628 }
1629
1630 static unsigned int
1631 ath5k_rx_decrypted(struct ath5k_softc *sc, struct ath5k_desc *ds,
1632                 struct sk_buff *skb)
1633 {
1634         struct ieee80211_hdr *hdr = (void *)skb->data;
1635         unsigned int keyix, hlen = ieee80211_get_hdrlen_from_skb(skb);
1636
1637         if (!(ds->ds_rxstat.rs_status & AR5K_RXERR_DECRYPT) &&
1638                         ds->ds_rxstat.rs_keyix != AR5K_RXKEYIX_INVALID)
1639                 return RX_FLAG_DECRYPTED;
1640
1641         /* Apparently when a default key is used to decrypt the packet
1642            the hw does not set the index used to decrypt.  In such cases
1643            get the index from the packet. */
1644         if ((le16_to_cpu(hdr->frame_control) & IEEE80211_FCTL_PROTECTED) &&
1645                         !(ds->ds_rxstat.rs_status & AR5K_RXERR_DECRYPT) &&
1646                         skb->len >= hlen + 4) {
1647                 keyix = skb->data[hlen + 3] >> 6;
1648
1649                 if (test_bit(keyix, sc->keymap))
1650                         return RX_FLAG_DECRYPTED;
1651         }
1652
1653         return 0;
1654 }
1655
1656
1657 static void
1658 ath5k_check_ibss_hw_merge(struct ath5k_softc *sc, struct sk_buff *skb)
1659 {
1660         u32 hw_tu;
1661         struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)skb->data;
1662
1663         if ((mgmt->frame_control & IEEE80211_FCTL_FTYPE) ==
1664                 IEEE80211_FTYPE_MGMT &&
1665             (mgmt->frame_control & IEEE80211_FCTL_STYPE) ==
1666                 IEEE80211_STYPE_BEACON &&
1667             mgmt->u.beacon.capab_info & WLAN_CAPABILITY_IBSS &&
1668             memcmp(mgmt->bssid, sc->ah->ah_bssid, ETH_ALEN) == 0) {
1669                 /*
1670                  * Received an IBSS beacon with the same BSSID. Hardware might
1671                  * have updated the TSF, check if we need to update timers.
1672                  */
1673                 hw_tu = TSF_TO_TU(ath5k_hw_get_tsf64(sc->ah));
1674                 if (hw_tu >= sc->nexttbtt) {
1675                         ath5k_beacon_update_timers(sc,
1676                                 mgmt->u.beacon.timestamp);
1677                         ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
1678                                 "detected HW merge from received beacon\n");
1679                 }
1680         }
1681 }
1682
1683
1684 static void
1685 ath5k_tasklet_rx(unsigned long data)
1686 {
1687         struct ieee80211_rx_status rxs = {};
1688         struct sk_buff *skb;
1689         struct ath5k_softc *sc = (void *)data;
1690         struct ath5k_buf *bf;
1691         struct ath5k_desc *ds;
1692         u16 len;
1693         u8 stat;
1694         int ret;
1695         int hdrlen;
1696         int pad;
1697
1698         spin_lock(&sc->rxbuflock);
1699         do {
1700                 if (unlikely(list_empty(&sc->rxbuf))) {
1701                         ATH5K_WARN(sc, "empty rx buf pool\n");
1702                         break;
1703                 }
1704                 bf = list_first_entry(&sc->rxbuf, struct ath5k_buf, list);
1705                 BUG_ON(bf->skb == NULL);
1706                 skb = bf->skb;
1707                 ds = bf->desc;
1708
1709                 /* TODO only one segment */
1710                 pci_dma_sync_single_for_cpu(sc->pdev, sc->desc_daddr,
1711                                 sc->desc_len, PCI_DMA_FROMDEVICE);
1712
1713                 if (unlikely(ds->ds_link == bf->daddr)) /* this is the end */
1714                         break;
1715
1716                 ret = sc->ah->ah_proc_rx_desc(sc->ah, ds);
1717                 if (unlikely(ret == -EINPROGRESS))
1718                         break;
1719                 else if (unlikely(ret)) {
1720                         ATH5K_ERR(sc, "error in processing rx descriptor\n");
1721                         spin_unlock(&sc->rxbuflock);
1722                         return;
1723                 }
1724
1725                 if (unlikely(ds->ds_rxstat.rs_more)) {
1726                         ATH5K_WARN(sc, "unsupported jumbo\n");
1727                         goto next;
1728                 }
1729
1730                 stat = ds->ds_rxstat.rs_status;
1731                 if (unlikely(stat)) {
1732                         if (stat & AR5K_RXERR_PHY)
1733                                 goto next;
1734                         if (stat & AR5K_RXERR_DECRYPT) {
1735                                 /*
1736                                  * Decrypt error.  If the error occurred
1737                                  * because there was no hardware key, then
1738                                  * let the frame through so the upper layers
1739                                  * can process it.  This is necessary for 5210
1740                                  * parts which have no way to setup a ``clear''
1741                                  * key cache entry.
1742                                  *
1743                                  * XXX do key cache faulting
1744                                  */
1745                                 if (ds->ds_rxstat.rs_keyix ==
1746                                                 AR5K_RXKEYIX_INVALID &&
1747                                                 !(stat & AR5K_RXERR_CRC))
1748                                         goto accept;
1749                         }
1750                         if (stat & AR5K_RXERR_MIC) {
1751                                 rxs.flag |= RX_FLAG_MMIC_ERROR;
1752                                 goto accept;
1753                         }
1754
1755                         /* let crypto-error packets fall through in MNTR */
1756                         if ((stat & ~(AR5K_RXERR_DECRYPT|AR5K_RXERR_MIC)) ||
1757                                         sc->opmode != IEEE80211_IF_TYPE_MNTR)
1758                                 goto next;
1759                 }
1760 accept:
1761                 len = ds->ds_rxstat.rs_datalen;
1762                 pci_dma_sync_single_for_cpu(sc->pdev, bf->skbaddr, len,
1763                                 PCI_DMA_FROMDEVICE);
1764                 pci_unmap_single(sc->pdev, bf->skbaddr, sc->rxbufsize,
1765                                 PCI_DMA_FROMDEVICE);
1766                 bf->skb = NULL;
1767
1768                 skb_put(skb, len);
1769
1770                 /*
1771                  * the hardware adds a padding to 4 byte boundaries between
1772                  * the header and the payload data if the header length is
1773                  * not multiples of 4 - remove it
1774                  */
1775                 hdrlen = ieee80211_get_hdrlen_from_skb(skb);
1776                 if (hdrlen & 3) {
1777                         pad = hdrlen % 4;
1778                         memmove(skb->data + pad, skb->data, hdrlen);
1779                         skb_pull(skb, pad);
1780                 }
1781
1782                 /*
1783                  * always extend the mac timestamp, since this information is
1784                  * also needed for proper IBSS merging.
1785                  *
1786                  * XXX: it might be too late to do it here, since rs_tstamp is
1787                  * 15bit only. that means TSF extension has to be done within
1788                  * 32768usec (about 32ms). it might be necessary to move this to
1789                  * the interrupt handler, like it is done in madwifi.
1790                  */
1791                 rxs.mactime = ath5k_extend_tsf(sc->ah, ds->ds_rxstat.rs_tstamp);
1792                 rxs.flag |= RX_FLAG_TSFT;
1793
1794                 rxs.freq = sc->curchan->freq;
1795                 rxs.channel = sc->curchan->chan;
1796                 rxs.phymode = sc->curmode;
1797
1798                 /*
1799                  * signal quality:
1800                  * the names here are misleading and the usage of these
1801                  * values by iwconfig makes it even worse
1802                  */
1803                 /* noise floor in dBm, from the last noise calibration */
1804                 rxs.noise = sc->ah->ah_noise_floor;
1805                 /* signal level in dBm */
1806                 rxs.ssi = rxs.noise + ds->ds_rxstat.rs_rssi;
1807                 /*
1808                  * "signal" is actually displayed as Link Quality by iwconfig
1809                  * we provide a percentage based on rssi (assuming max rssi 64)
1810                  */
1811                 rxs.signal = ds->ds_rxstat.rs_rssi * 100 / 64;
1812
1813                 rxs.antenna = ds->ds_rxstat.rs_antenna;
1814                 rxs.rate = ds->ds_rxstat.rs_rate;
1815                 rxs.flag |= ath5k_rx_decrypted(sc, ds, skb);
1816
1817                 ath5k_debug_dump_skb(sc, skb, "RX  ", 0);
1818
1819                 /* check beacons in IBSS mode */
1820                 if (sc->opmode == IEEE80211_IF_TYPE_IBSS)
1821                         ath5k_check_ibss_hw_merge(sc, skb);
1822
1823                 __ieee80211_rx(sc->hw, skb, &rxs);
1824                 sc->led_rxrate = ds->ds_rxstat.rs_rate;
1825                 ath5k_led_event(sc, ATH_LED_RX);
1826 next:
1827                 list_move_tail(&bf->list, &sc->rxbuf);
1828         } while (ath5k_rxbuf_setup(sc, bf) == 0);
1829         spin_unlock(&sc->rxbuflock);
1830 }
1831
1832
1833
1834
1835 /*************\
1836 * TX Handling *
1837 \*************/
1838
1839 static void
1840 ath5k_tx_processq(struct ath5k_softc *sc, struct ath5k_txq *txq)
1841 {
1842         struct ieee80211_tx_status txs = {};
1843         struct ath5k_buf *bf, *bf0;
1844         struct ath5k_desc *ds;
1845         struct sk_buff *skb;
1846         int ret;
1847
1848         spin_lock(&txq->lock);
1849         list_for_each_entry_safe(bf, bf0, &txq->q, list) {
1850                 ds = bf->desc;
1851
1852                 /* TODO only one segment */
1853                 pci_dma_sync_single_for_cpu(sc->pdev, sc->desc_daddr,
1854                                 sc->desc_len, PCI_DMA_FROMDEVICE);
1855                 ret = sc->ah->ah_proc_tx_desc(sc->ah, ds);
1856                 if (unlikely(ret == -EINPROGRESS))
1857                         break;
1858                 else if (unlikely(ret)) {
1859                         ATH5K_ERR(sc, "error %d while processing queue %u\n",
1860                                 ret, txq->qnum);
1861                         break;
1862                 }
1863
1864                 skb = bf->skb;
1865                 bf->skb = NULL;
1866                 pci_unmap_single(sc->pdev, bf->skbaddr, skb->len,
1867                                 PCI_DMA_TODEVICE);
1868
1869                 txs.control = bf->ctl;
1870                 txs.retry_count = ds->ds_txstat.ts_shortretry +
1871                         ds->ds_txstat.ts_longretry / 6;
1872                 if (unlikely(ds->ds_txstat.ts_status)) {
1873                         sc->ll_stats.dot11ACKFailureCount++;
1874                         if (ds->ds_txstat.ts_status & AR5K_TXERR_XRETRY)
1875                                 txs.excessive_retries = 1;
1876                         else if (ds->ds_txstat.ts_status & AR5K_TXERR_FILT)
1877                                 txs.flags |= IEEE80211_TX_STATUS_TX_FILTERED;
1878                 } else {
1879                         txs.flags |= IEEE80211_TX_STATUS_ACK;
1880                         txs.ack_signal = ds->ds_txstat.ts_rssi;
1881                 }
1882
1883                 ieee80211_tx_status(sc->hw, skb, &txs);
1884                 sc->tx_stats.data[txq->qnum].count++;
1885
1886                 spin_lock(&sc->txbuflock);
1887                 sc->tx_stats.data[txq->qnum].len--;
1888                 list_move_tail(&bf->list, &sc->txbuf);
1889                 sc->txbuf_len++;
1890                 spin_unlock(&sc->txbuflock);
1891         }
1892         if (likely(list_empty(&txq->q)))
1893                 txq->link = NULL;
1894         spin_unlock(&txq->lock);
1895         if (sc->txbuf_len > ATH_TXBUF / 5)
1896                 ieee80211_wake_queues(sc->hw);
1897 }
1898
1899 static void
1900 ath5k_tasklet_tx(unsigned long data)
1901 {
1902         struct ath5k_softc *sc = (void *)data;
1903
1904         ath5k_tx_processq(sc, sc->txq);
1905
1906         ath5k_led_event(sc, ATH_LED_TX);
1907 }
1908
1909
1910
1911
1912 /*****************\
1913 * Beacon handling *
1914 \*****************/
1915
1916 /*
1917  * Setup the beacon frame for transmit.
1918  */
1919 static int
1920 ath5k_beacon_setup(struct ath5k_softc *sc, struct ath5k_buf *bf,
1921                 struct ieee80211_tx_control *ctl)
1922 {
1923         struct sk_buff *skb = bf->skb;
1924         struct ath5k_hw *ah = sc->ah;
1925         struct ath5k_desc *ds;
1926         int ret, antenna = 0;
1927         u32 flags;
1928
1929         bf->skbaddr = pci_map_single(sc->pdev, skb->data, skb->len,
1930                         PCI_DMA_TODEVICE);
1931         ATH5K_DBG(sc, ATH5K_DEBUG_BEACON, "skb %p [data %p len %u] "
1932                         "skbaddr %llx\n", skb, skb->data, skb->len,
1933                         (unsigned long long)bf->skbaddr);
1934         if (pci_dma_mapping_error(bf->skbaddr)) {
1935                 ATH5K_ERR(sc, "beacon DMA mapping failed\n");
1936                 return -EIO;
1937         }
1938
1939         ds = bf->desc;
1940
1941         flags = AR5K_TXDESC_NOACK;
1942         if (sc->opmode == IEEE80211_IF_TYPE_IBSS && ath5k_hw_hasveol(ah)) {
1943                 ds->ds_link = bf->daddr;        /* self-linked */
1944                 flags |= AR5K_TXDESC_VEOL;
1945                 /*
1946                  * Let hardware handle antenna switching if txantenna is not set
1947                  */
1948         } else {
1949                 ds->ds_link = 0;
1950                 /*
1951                  * Switch antenna every 4 beacons if txantenna is not set
1952                  * XXX assumes two antennas
1953                  */
1954                 if (antenna == 0)
1955                         antenna = sc->bsent & 4 ? 2 : 1;
1956         }
1957
1958         ds->ds_data = bf->skbaddr;
1959         ret = ah->ah_setup_tx_desc(ah, ds, skb->len,
1960                         ieee80211_get_hdrlen_from_skb(skb),
1961                         AR5K_PKT_TYPE_BEACON, (ctl->power_level * 2), ctl->tx_rate, 1,
1962                         AR5K_TXKEYIX_INVALID, antenna, flags, 0, 0);
1963         if (ret)
1964                 goto err_unmap;
1965
1966         return 0;
1967 err_unmap:
1968         pci_unmap_single(sc->pdev, bf->skbaddr, skb->len, PCI_DMA_TODEVICE);
1969         return ret;
1970 }
1971
1972 /*
1973  * Transmit a beacon frame at SWBA.  Dynamic updates to the
1974  * frame contents are done as needed and the slot time is
1975  * also adjusted based on current state.
1976  *
1977  * this is usually called from interrupt context (ath5k_intr())
1978  * but also from ath5k_beacon_config() in IBSS mode which in turn
1979  * can be called from a tasklet and user context
1980  */
1981 static void
1982 ath5k_beacon_send(struct ath5k_softc *sc)
1983 {
1984         struct ath5k_buf *bf = sc->bbuf;
1985         struct ath5k_hw *ah = sc->ah;
1986
1987         ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON, "in beacon_send\n");
1988
1989         if (unlikely(bf->skb == NULL || sc->opmode == IEEE80211_IF_TYPE_STA ||
1990                         sc->opmode == IEEE80211_IF_TYPE_MNTR)) {
1991                 ATH5K_WARN(sc, "bf=%p bf_skb=%p\n", bf, bf ? bf->skb : NULL);
1992                 return;
1993         }
1994         /*
1995          * Check if the previous beacon has gone out.  If
1996          * not don't don't try to post another, skip this
1997          * period and wait for the next.  Missed beacons
1998          * indicate a problem and should not occur.  If we
1999          * miss too many consecutive beacons reset the device.
2000          */
2001         if (unlikely(ath5k_hw_num_tx_pending(ah, sc->bhalq) != 0)) {
2002                 sc->bmisscount++;
2003                 ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
2004                         "missed %u consecutive beacons\n", sc->bmisscount);
2005                 if (sc->bmisscount > 3) {               /* NB: 3 is a guess */
2006                         ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
2007                                 "stuck beacon time (%u missed)\n",
2008                                 sc->bmisscount);
2009                         tasklet_schedule(&sc->restq);
2010                 }
2011                 return;
2012         }
2013         if (unlikely(sc->bmisscount != 0)) {
2014                 ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
2015                         "resume beacon xmit after %u misses\n",
2016                         sc->bmisscount);
2017                 sc->bmisscount = 0;
2018         }
2019
2020         /*
2021          * Stop any current dma and put the new frame on the queue.
2022          * This should never fail since we check above that no frames
2023          * are still pending on the queue.
2024          */
2025         if (unlikely(ath5k_hw_stop_tx_dma(ah, sc->bhalq))) {
2026                 ATH5K_WARN(sc, "beacon queue %u didn't stop?\n", sc->bhalq);
2027                 /* NB: hw still stops DMA, so proceed */
2028         }
2029         pci_dma_sync_single_for_cpu(sc->pdev, bf->skbaddr, bf->skb->len,
2030                         PCI_DMA_TODEVICE);
2031
2032         ath5k_hw_put_tx_buf(ah, sc->bhalq, bf->daddr);
2033         ath5k_hw_tx_start(ah, sc->bhalq);
2034         ATH5K_DBG(sc, ATH5K_DEBUG_BEACON, "TXDP[%u] = %llx (%p)\n",
2035                 sc->bhalq, (unsigned long long)bf->daddr, bf->desc);
2036
2037         sc->bsent++;
2038 }
2039
2040
2041 /**
2042  * ath5k_beacon_update_timers - update beacon timers
2043  *
2044  * @sc: struct ath5k_softc pointer we are operating on
2045  * @bc_tsf: the timestamp of the beacon. 0 to reset the TSF. -1 to perform a
2046  *          beacon timer update based on the current HW TSF.
2047  *
2048  * Calculate the next target beacon transmit time (TBTT) based on the timestamp
2049  * of a received beacon or the current local hardware TSF and write it to the
2050  * beacon timer registers.
2051  *
2052  * This is called in a variety of situations, e.g. when a beacon is received,
2053  * when a HW merge has been detected, but also when an new IBSS is created or
2054  * when we otherwise know we have to update the timers, but we keep it in this
2055  * function to have it all together in one place.
2056  */
2057 static void
2058 ath5k_beacon_update_timers(struct ath5k_softc *sc, u64 bc_tsf)
2059 {
2060         struct ath5k_hw *ah = sc->ah;
2061         u32 nexttbtt, intval, hw_tu, bc_tu;
2062         u64 hw_tsf;
2063
2064         intval = sc->bintval & AR5K_BEACON_PERIOD;
2065         if (WARN_ON(!intval))
2066                 return;
2067
2068         /* beacon TSF converted to TU */
2069         bc_tu = TSF_TO_TU(bc_tsf);
2070
2071         /* current TSF converted to TU */
2072         hw_tsf = ath5k_hw_get_tsf64(ah);
2073         hw_tu = TSF_TO_TU(hw_tsf);
2074
2075 #define FUDGE 3
2076         /* we use FUDGE to make sure the next TBTT is ahead of the current TU */
2077         if (bc_tsf == -1) {
2078                 /*
2079                  * no beacons received, called internally.
2080                  * just need to refresh timers based on HW TSF.
2081                  */
2082                 nexttbtt = roundup(hw_tu + FUDGE, intval);
2083         } else if (bc_tsf == 0) {
2084                 /*
2085                  * no beacon received, probably called by ath5k_reset_tsf().
2086                  * reset TSF to start with 0.
2087                  */
2088                 nexttbtt = intval;
2089                 intval |= AR5K_BEACON_RESET_TSF;
2090         } else if (bc_tsf > hw_tsf) {
2091                 /*
2092                  * beacon received, SW merge happend but HW TSF not yet updated.
2093                  * not possible to reconfigure timers yet, but next time we
2094                  * receive a beacon with the same BSSID, the hardware will
2095                  * automatically update the TSF and then we need to reconfigure
2096                  * the timers.
2097                  */
2098                 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
2099                         "need to wait for HW TSF sync\n");
2100                 return;
2101         } else {
2102                 /*
2103                  * most important case for beacon synchronization between STA.
2104                  *
2105                  * beacon received and HW TSF has been already updated by HW.
2106                  * update next TBTT based on the TSF of the beacon, but make
2107                  * sure it is ahead of our local TSF timer.
2108                  */
2109                 nexttbtt = bc_tu + roundup(hw_tu + FUDGE - bc_tu, intval);
2110         }
2111 #undef FUDGE
2112
2113         sc->nexttbtt = nexttbtt;
2114
2115         intval |= AR5K_BEACON_ENA;
2116         ath5k_hw_init_beacon(ah, nexttbtt, intval);
2117
2118         /*
2119          * debugging output last in order to preserve the time critical aspect
2120          * of this function
2121          */
2122         if (bc_tsf == -1)
2123                 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
2124                         "reconfigured timers based on HW TSF\n");
2125         else if (bc_tsf == 0)
2126                 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
2127                         "reset HW TSF and timers\n");
2128         else
2129                 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
2130                         "updated timers based on beacon TSF\n");
2131
2132         ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
2133                           "bc_tsf %llx hw_tsf %llx bc_tu %u hw_tu %u nexttbtt %u\n",
2134                           (unsigned long long) bc_tsf,
2135                           (unsigned long long) hw_tsf, bc_tu, hw_tu, nexttbtt);
2136         ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON, "intval %u %s %s\n",
2137                 intval & AR5K_BEACON_PERIOD,
2138                 intval & AR5K_BEACON_ENA ? "AR5K_BEACON_ENA" : "",
2139                 intval & AR5K_BEACON_RESET_TSF ? "AR5K_BEACON_RESET_TSF" : "");
2140 }
2141
2142
2143 /**
2144  * ath5k_beacon_config - Configure the beacon queues and interrupts
2145  *
2146  * @sc: struct ath5k_softc pointer we are operating on
2147  *
2148  * When operating in station mode we want to receive a BMISS interrupt when we
2149  * stop seeing beacons from the AP we've associated with so we can look for
2150  * another AP to associate with.
2151  *
2152  * In IBSS mode we use a self-linked tx descriptor if possible. We enable SWBA
2153  * interrupts to detect HW merges only.
2154  *
2155  * AP mode is missing.
2156  */
2157 static void
2158 ath5k_beacon_config(struct ath5k_softc *sc)
2159 {
2160         struct ath5k_hw *ah = sc->ah;
2161
2162         ath5k_hw_set_intr(ah, 0);
2163         sc->bmisscount = 0;
2164
2165         if (sc->opmode == IEEE80211_IF_TYPE_STA) {
2166                 sc->imask |= AR5K_INT_BMISS;
2167         } else if (sc->opmode == IEEE80211_IF_TYPE_IBSS) {
2168                 /*
2169                  * In IBSS mode we use a self-linked tx descriptor and let the
2170                  * hardware send the beacons automatically. We have to load it
2171                  * only once here.
2172                  * We use the SWBA interrupt only to keep track of the beacon
2173                  * timers in order to detect HW merges (automatic TSF updates).
2174                  */
2175                 ath5k_beaconq_config(sc);
2176
2177                 sc->imask |= AR5K_INT_SWBA;
2178
2179                 if (ath5k_hw_hasveol(ah))
2180                         ath5k_beacon_send(sc);
2181         }
2182         /* TODO else AP */
2183
2184         ath5k_hw_set_intr(ah, sc->imask);
2185 }
2186
2187
2188 /********************\
2189 * Interrupt handling *
2190 \********************/
2191
2192 static int
2193 ath5k_init(struct ath5k_softc *sc)
2194 {
2195         int ret;
2196
2197         mutex_lock(&sc->lock);
2198
2199         ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "mode %d\n", sc->opmode);
2200
2201         /*
2202          * Stop anything previously setup.  This is safe
2203          * no matter this is the first time through or not.
2204          */
2205         ath5k_stop_locked(sc);
2206
2207         /*
2208          * The basic interface to setting the hardware in a good
2209          * state is ``reset''.  On return the hardware is known to
2210          * be powered up and with interrupts disabled.  This must
2211          * be followed by initialization of the appropriate bits
2212          * and then setup of the interrupt mask.
2213          */
2214         sc->curchan = sc->hw->conf.chan;
2215         ret = ath5k_hw_reset(sc->ah, sc->opmode, sc->curchan, false);
2216         if (ret) {
2217                 ATH5K_ERR(sc, "unable to reset hardware: %d\n", ret);
2218                 goto done;
2219         }
2220         /*
2221          * This is needed only to setup initial state
2222          * but it's best done after a reset.
2223          */
2224         ath5k_hw_set_txpower_limit(sc->ah, 0);
2225
2226         /*
2227          * Setup the hardware after reset: the key cache
2228          * is filled as needed and the receive engine is
2229          * set going.  Frame transmit is handled entirely
2230          * in the frame output path; there's nothing to do
2231          * here except setup the interrupt mask.
2232          */
2233         ret = ath5k_rx_start(sc);
2234         if (ret)
2235                 goto done;
2236
2237         /*
2238          * Enable interrupts.
2239          */
2240         sc->imask = AR5K_INT_RX | AR5K_INT_TX | AR5K_INT_RXEOL |
2241                 AR5K_INT_RXORN | AR5K_INT_FATAL | AR5K_INT_GLOBAL;
2242
2243         ath5k_hw_set_intr(sc->ah, sc->imask);
2244         /* Set ack to be sent at low bit-rates */
2245         ath5k_hw_set_ack_bitrate_high(sc->ah, false);
2246
2247         mod_timer(&sc->calib_tim, round_jiffies(jiffies +
2248                         msecs_to_jiffies(ath5k_calinterval * 1000)));
2249
2250         ret = 0;
2251 done:
2252         mutex_unlock(&sc->lock);
2253         return ret;
2254 }
2255
2256 static int
2257 ath5k_stop_locked(struct ath5k_softc *sc)
2258 {
2259         struct ath5k_hw *ah = sc->ah;
2260
2261         ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "invalid %u\n",
2262                         test_bit(ATH_STAT_INVALID, sc->status));
2263
2264         /*
2265          * Shutdown the hardware and driver:
2266          *    stop output from above
2267          *    disable interrupts
2268          *    turn off timers
2269          *    turn off the radio
2270          *    clear transmit machinery
2271          *    clear receive machinery
2272          *    drain and release tx queues
2273          *    reclaim beacon resources
2274          *    power down hardware
2275          *
2276          * Note that some of this work is not possible if the
2277          * hardware is gone (invalid).
2278          */
2279         ieee80211_stop_queues(sc->hw);
2280
2281         if (!test_bit(ATH_STAT_INVALID, sc->status)) {
2282                 if (test_bit(ATH_STAT_LEDSOFT, sc->status)) {
2283                         del_timer_sync(&sc->led_tim);
2284                         ath5k_hw_set_gpio(ah, sc->led_pin, !sc->led_on);
2285                         __clear_bit(ATH_STAT_LEDBLINKING, sc->status);
2286                 }
2287                 ath5k_hw_set_intr(ah, 0);
2288         }
2289         ath5k_txq_cleanup(sc);
2290         if (!test_bit(ATH_STAT_INVALID, sc->status)) {
2291                 ath5k_rx_stop(sc);
2292                 ath5k_hw_phy_disable(ah);
2293         } else
2294                 sc->rxlink = NULL;
2295
2296         return 0;
2297 }
2298
2299 /*
2300  * Stop the device, grabbing the top-level lock to protect
2301  * against concurrent entry through ath5k_init (which can happen
2302  * if another thread does a system call and the thread doing the
2303  * stop is preempted).
2304  */
2305 static int
2306 ath5k_stop_hw(struct ath5k_softc *sc)
2307 {
2308         int ret;
2309
2310         mutex_lock(&sc->lock);
2311         ret = ath5k_stop_locked(sc);
2312         if (ret == 0 && !test_bit(ATH_STAT_INVALID, sc->status)) {
2313                 /*
2314                  * Set the chip in full sleep mode.  Note that we are
2315                  * careful to do this only when bringing the interface
2316                  * completely to a stop.  When the chip is in this state
2317                  * it must be carefully woken up or references to
2318                  * registers in the PCI clock domain may freeze the bus
2319                  * (and system).  This varies by chip and is mostly an
2320                  * issue with newer parts that go to sleep more quickly.
2321                  */
2322                 if (sc->ah->ah_mac_srev >= 0x78) {
2323                         /*
2324                          * XXX
2325                          * don't put newer MAC revisions > 7.8 to sleep because
2326                          * of the above mentioned problems
2327                          */
2328                         ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "mac version > 7.8, "
2329                                 "not putting device to sleep\n");
2330                 } else {
2331                         ATH5K_DBG(sc, ATH5K_DEBUG_RESET,
2332                                 "putting device to full sleep\n");
2333                         ath5k_hw_set_power(sc->ah, AR5K_PM_FULL_SLEEP, true, 0);
2334                 }
2335         }
2336         ath5k_txbuf_free(sc, sc->bbuf);
2337         mutex_unlock(&sc->lock);
2338
2339         del_timer_sync(&sc->calib_tim);
2340
2341         return ret;
2342 }
2343
2344 static irqreturn_t
2345 ath5k_intr(int irq, void *dev_id)
2346 {
2347         struct ath5k_softc *sc = dev_id;
2348         struct ath5k_hw *ah = sc->ah;
2349         enum ath5k_int status;
2350         unsigned int counter = 1000;
2351
2352         if (unlikely(test_bit(ATH_STAT_INVALID, sc->status) ||
2353                                 !ath5k_hw_is_intr_pending(ah)))
2354                 return IRQ_NONE;
2355
2356         do {
2357                 /*
2358                  * Figure out the reason(s) for the interrupt.  Note
2359                  * that get_isr returns a pseudo-ISR that may include
2360                  * bits we haven't explicitly enabled so we mask the
2361                  * value to insure we only process bits we requested.
2362                  */
2363                 ath5k_hw_get_isr(ah, &status);          /* NB: clears IRQ too */
2364                 ATH5K_DBG(sc, ATH5K_DEBUG_INTR, "status 0x%x/0x%x\n",
2365                                 status, sc->imask);
2366                 status &= sc->imask; /* discard unasked for bits */
2367                 if (unlikely(status & AR5K_INT_FATAL)) {
2368                         /*
2369                          * Fatal errors are unrecoverable.
2370                          * Typically these are caused by DMA errors.
2371                          */
2372                         tasklet_schedule(&sc->restq);
2373                 } else if (unlikely(status & AR5K_INT_RXORN)) {
2374                         tasklet_schedule(&sc->restq);
2375                 } else {
2376                         if (status & AR5K_INT_SWBA) {
2377                                 /*
2378                                 * Software beacon alert--time to send a beacon.
2379                                 * Handle beacon transmission directly; deferring
2380                                 * this is too slow to meet timing constraints
2381                                 * under load.
2382                                 *
2383                                 * In IBSS mode we use this interrupt just to
2384                                 * keep track of the next TBTT (target beacon
2385                                 * transmission time) in order to detect hardware
2386                                 * merges (TSF updates).
2387                                 */
2388                                 if (sc->opmode == IEEE80211_IF_TYPE_IBSS) {
2389                                          /* XXX: only if VEOL suppported */
2390                                         u64 tsf = ath5k_hw_get_tsf64(ah);
2391                                         sc->nexttbtt += sc->bintval;
2392                                         ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
2393                                                   "SWBA nexttbtt: %x hw_tu: %x "
2394                                                   "TSF: %llx\n",
2395                                                   sc->nexttbtt,
2396                                                   TSF_TO_TU(tsf),
2397                                                   (unsigned long long) tsf);
2398                                 } else {
2399                                         ath5k_beacon_send(sc);
2400                                 }
2401                         }
2402                         if (status & AR5K_INT_RXEOL) {
2403                                 /*
2404                                 * NB: the hardware should re-read the link when
2405                                 *     RXE bit is written, but it doesn't work at
2406                                 *     least on older hardware revs.
2407                                 */
2408                                 sc->rxlink = NULL;
2409                         }
2410                         if (status & AR5K_INT_TXURN) {
2411                                 /* bump tx trigger level */
2412                                 ath5k_hw_update_tx_triglevel(ah, true);
2413                         }
2414                         if (status & AR5K_INT_RX)
2415                                 tasklet_schedule(&sc->rxtq);
2416                         if (status & AR5K_INT_TX)
2417                                 tasklet_schedule(&sc->txtq);
2418                         if (status & AR5K_INT_BMISS) {
2419                         }
2420                         if (status & AR5K_INT_MIB) {
2421                                 /* TODO */
2422                         }
2423                 }
2424         } while (ath5k_hw_is_intr_pending(ah) && counter-- > 0);
2425
2426         if (unlikely(!counter))
2427                 ATH5K_WARN(sc, "too many interrupts, giving up for now\n");
2428
2429         return IRQ_HANDLED;
2430 }
2431
2432 static void
2433 ath5k_tasklet_reset(unsigned long data)
2434 {
2435         struct ath5k_softc *sc = (void *)data;
2436
2437         ath5k_reset(sc->hw);
2438 }
2439
2440 /*
2441  * Periodically recalibrate the PHY to account
2442  * for temperature/environment changes.
2443  */
2444 static void
2445 ath5k_calibrate(unsigned long data)
2446 {
2447         struct ath5k_softc *sc = (void *)data;
2448         struct ath5k_hw *ah = sc->ah;
2449
2450         ATH5K_DBG(sc, ATH5K_DEBUG_CALIBRATE, "channel %u/%x\n",
2451                 sc->curchan->chan, sc->curchan->val);
2452
2453         if (ath5k_hw_get_rf_gain(ah) == AR5K_RFGAIN_NEED_CHANGE) {
2454                 /*
2455                  * Rfgain is out of bounds, reset the chip
2456                  * to load new gain values.
2457                  */
2458                 ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "calibration, resetting\n");
2459                 ath5k_reset(sc->hw);
2460         }
2461         if (ath5k_hw_phy_calibrate(ah, sc->curchan))
2462                 ATH5K_ERR(sc, "calibration of channel %u failed\n",
2463                                 sc->curchan->chan);
2464
2465         mod_timer(&sc->calib_tim, round_jiffies(jiffies +
2466                         msecs_to_jiffies(ath5k_calinterval * 1000)));
2467 }
2468
2469
2470
2471 /***************\
2472 * LED functions *
2473 \***************/
2474
2475 static void
2476 ath5k_led_off(unsigned long data)
2477 {
2478         struct ath5k_softc *sc = (void *)data;
2479
2480         if (test_bit(ATH_STAT_LEDENDBLINK, sc->status))
2481                 __clear_bit(ATH_STAT_LEDBLINKING, sc->status);
2482         else {
2483                 __set_bit(ATH_STAT_LEDENDBLINK, sc->status);
2484                 ath5k_hw_set_gpio(sc->ah, sc->led_pin, !sc->led_on);
2485                 mod_timer(&sc->led_tim, jiffies + sc->led_off);
2486         }
2487 }
2488
2489 /*
2490  * Blink the LED according to the specified on/off times.
2491  */
2492 static void
2493 ath5k_led_blink(struct ath5k_softc *sc, unsigned int on,
2494                 unsigned int off)
2495 {
2496         ATH5K_DBG(sc, ATH5K_DEBUG_LED, "on %u off %u\n", on, off);
2497         ath5k_hw_set_gpio(sc->ah, sc->led_pin, sc->led_on);
2498         __set_bit(ATH_STAT_LEDBLINKING, sc->status);
2499         __clear_bit(ATH_STAT_LEDENDBLINK, sc->status);
2500         sc->led_off = off;
2501         mod_timer(&sc->led_tim, jiffies + on);
2502 }
2503
2504 static void
2505 ath5k_led_event(struct ath5k_softc *sc, int event)
2506 {
2507         if (likely(!test_bit(ATH_STAT_LEDSOFT, sc->status)))
2508                 return;
2509         if (unlikely(test_bit(ATH_STAT_LEDBLINKING, sc->status)))
2510                 return; /* don't interrupt active blink */
2511         switch (event) {
2512         case ATH_LED_TX:
2513                 ath5k_led_blink(sc, sc->hwmap[sc->led_txrate].ledon,
2514                         sc->hwmap[sc->led_txrate].ledoff);
2515                 break;
2516         case ATH_LED_RX:
2517                 ath5k_led_blink(sc, sc->hwmap[sc->led_rxrate].ledon,
2518                         sc->hwmap[sc->led_rxrate].ledoff);
2519                 break;
2520         }
2521 }
2522
2523
2524
2525
2526 /********************\
2527 * Mac80211 functions *
2528 \********************/
2529
2530 static int
2531 ath5k_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
2532                         struct ieee80211_tx_control *ctl)
2533 {
2534         struct ath5k_softc *sc = hw->priv;
2535         struct ath5k_buf *bf;
2536         unsigned long flags;
2537         int hdrlen;
2538         int pad;
2539
2540         ath5k_debug_dump_skb(sc, skb, "TX  ", 1);
2541
2542         if (sc->opmode == IEEE80211_IF_TYPE_MNTR)
2543                 ATH5K_DBG(sc, ATH5K_DEBUG_XMIT, "tx in monitor (scan?)\n");
2544
2545         /*
2546          * the hardware expects the header padded to 4 byte boundaries
2547          * if this is not the case we add the padding after the header
2548          */
2549         hdrlen = ieee80211_get_hdrlen_from_skb(skb);
2550         if (hdrlen & 3) {
2551                 pad = hdrlen % 4;
2552                 if (skb_headroom(skb) < pad) {
2553                         ATH5K_ERR(sc, "tx hdrlen not %%4: %d not enough"
2554                                 " headroom to pad %d\n", hdrlen, pad);
2555                         return -1;
2556                 }
2557                 skb_push(skb, pad);
2558                 memmove(skb->data, skb->data+pad, hdrlen);
2559         }
2560
2561         sc->led_txrate = ctl->tx_rate;
2562
2563         spin_lock_irqsave(&sc->txbuflock, flags);
2564         if (list_empty(&sc->txbuf)) {
2565                 ATH5K_ERR(sc, "no further txbuf available, dropping packet\n");
2566                 spin_unlock_irqrestore(&sc->txbuflock, flags);
2567                 ieee80211_stop_queue(hw, ctl->queue);
2568                 return -1;
2569         }
2570         bf = list_first_entry(&sc->txbuf, struct ath5k_buf, list);
2571         list_del(&bf->list);
2572         sc->txbuf_len--;
2573         if (list_empty(&sc->txbuf))
2574                 ieee80211_stop_queues(hw);
2575         spin_unlock_irqrestore(&sc->txbuflock, flags);
2576
2577         bf->skb = skb;
2578
2579         if (ath5k_txbuf_setup(sc, bf, ctl)) {
2580                 bf->skb = NULL;
2581                 spin_lock_irqsave(&sc->txbuflock, flags);
2582                 list_add_tail(&bf->list, &sc->txbuf);
2583                 sc->txbuf_len++;
2584                 spin_unlock_irqrestore(&sc->txbuflock, flags);
2585                 dev_kfree_skb_any(skb);
2586                 return 0;
2587         }
2588
2589         return 0;
2590 }
2591
2592 static int
2593 ath5k_reset(struct ieee80211_hw *hw)
2594 {
2595         struct ath5k_softc *sc = hw->priv;
2596         struct ath5k_hw *ah = sc->ah;
2597         int ret;
2598
2599         ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "resetting\n");
2600         /*
2601          * Convert to a hw channel description with the flags
2602          * constrained to reflect the current operating mode.
2603          */
2604         sc->curchan = hw->conf.chan;
2605
2606         ath5k_hw_set_intr(ah, 0);
2607         ath5k_txq_cleanup(sc);
2608         ath5k_rx_stop(sc);
2609
2610         ret = ath5k_hw_reset(ah, sc->opmode, sc->curchan, true);
2611         if (unlikely(ret)) {
2612                 ATH5K_ERR(sc, "can't reset hardware (%d)\n", ret);
2613                 goto err;
2614         }
2615         ath5k_hw_set_txpower_limit(sc->ah, 0);
2616
2617         ret = ath5k_rx_start(sc);
2618         if (unlikely(ret)) {
2619                 ATH5K_ERR(sc, "can't start recv logic\n");
2620                 goto err;
2621         }
2622         /*
2623          * We may be doing a reset in response to an ioctl
2624          * that changes the channel so update any state that
2625          * might change as a result.
2626          *
2627          * XXX needed?
2628          */
2629 /*      ath5k_chan_change(sc, c); */
2630         ath5k_beacon_config(sc);
2631         /* intrs are started by ath5k_beacon_config */
2632
2633         ieee80211_wake_queues(hw);
2634
2635         return 0;
2636 err:
2637         return ret;
2638 }
2639
2640 static int ath5k_start(struct ieee80211_hw *hw)
2641 {
2642         return ath5k_init(hw->priv);
2643 }
2644
2645 static void ath5k_stop(struct ieee80211_hw *hw)
2646 {
2647         ath5k_stop_hw(hw->priv);
2648 }
2649
2650 static int ath5k_add_interface(struct ieee80211_hw *hw,
2651                 struct ieee80211_if_init_conf *conf)
2652 {
2653         struct ath5k_softc *sc = hw->priv;
2654         int ret;
2655
2656         mutex_lock(&sc->lock);
2657         if (sc->vif) {
2658                 ret = 0;
2659                 goto end;
2660         }
2661
2662         sc->vif = conf->vif;
2663
2664         switch (conf->type) {
2665         case IEEE80211_IF_TYPE_STA:
2666         case IEEE80211_IF_TYPE_IBSS:
2667         case IEEE80211_IF_TYPE_MNTR:
2668                 sc->opmode = conf->type;
2669                 break;
2670         default:
2671                 ret = -EOPNOTSUPP;
2672                 goto end;
2673         }
2674         ret = 0;
2675 end:
2676         mutex_unlock(&sc->lock);
2677         return ret;
2678 }
2679
2680 static void
2681 ath5k_remove_interface(struct ieee80211_hw *hw,
2682                         struct ieee80211_if_init_conf *conf)
2683 {
2684         struct ath5k_softc *sc = hw->priv;
2685
2686         mutex_lock(&sc->lock);
2687         if (sc->vif != conf->vif)
2688                 goto end;
2689
2690         sc->vif = NULL;
2691 end:
2692         mutex_unlock(&sc->lock);
2693 }
2694
2695 static int
2696 ath5k_config(struct ieee80211_hw *hw,
2697                         struct ieee80211_conf *conf)
2698 {
2699         struct ath5k_softc *sc = hw->priv;
2700
2701         sc->bintval = conf->beacon_int;
2702         ath5k_setcurmode(sc, conf->phymode);
2703
2704         return ath5k_chan_set(sc, conf->chan);
2705 }
2706
2707 static int
2708 ath5k_config_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
2709                         struct ieee80211_if_conf *conf)
2710 {
2711         struct ath5k_softc *sc = hw->priv;
2712         struct ath5k_hw *ah = sc->ah;
2713         int ret;
2714
2715         /* Set to a reasonable value. Note that this will
2716          * be set to mac80211's value at ath5k_config(). */
2717         sc->bintval = 1000;
2718         mutex_lock(&sc->lock);
2719         if (sc->vif != vif) {
2720                 ret = -EIO;
2721                 goto unlock;
2722         }
2723         if (conf->bssid) {
2724                 /* Cache for later use during resets */
2725                 memcpy(ah->ah_bssid, conf->bssid, ETH_ALEN);
2726                 /* XXX: assoc id is set to 0 for now, mac80211 doesn't have
2727                  * a clean way of letting us retrieve this yet. */
2728                 ath5k_hw_set_associd(ah, ah->ah_bssid, 0);
2729         }
2730         mutex_unlock(&sc->lock);
2731
2732         return ath5k_reset(hw);
2733 unlock:
2734         mutex_unlock(&sc->lock);
2735         return ret;
2736 }
2737
2738 #define SUPPORTED_FIF_FLAGS \
2739         FIF_PROMISC_IN_BSS |  FIF_ALLMULTI | FIF_FCSFAIL | \
2740         FIF_PLCPFAIL | FIF_CONTROL | FIF_OTHER_BSS | \
2741         FIF_BCN_PRBRESP_PROMISC
2742 /*
2743  * o always accept unicast, broadcast, and multicast traffic
2744  * o multicast traffic for all BSSIDs will be enabled if mac80211
2745  *   says it should be
2746  * o maintain current state of phy ofdm or phy cck error reception.
2747  *   If the hardware detects any of these type of errors then
2748  *   ath5k_hw_get_rx_filter() will pass to us the respective
2749  *   hardware filters to be able to receive these type of frames.
2750  * o probe request frames are accepted only when operating in
2751  *   hostap, adhoc, or monitor modes
2752  * o enable promiscuous mode according to the interface state
2753  * o accept beacons:
2754  *   - when operating in adhoc mode so the 802.11 layer creates
2755  *     node table entries for peers,
2756  *   - when operating in station mode for collecting rssi data when
2757  *     the station is otherwise quiet, or
2758  *   - when scanning
2759  */
2760 static void ath5k_configure_filter(struct ieee80211_hw *hw,
2761                 unsigned int changed_flags,
2762                 unsigned int *new_flags,
2763                 int mc_count, struct dev_mc_list *mclist)
2764 {
2765         struct ath5k_softc *sc = hw->priv;
2766         struct ath5k_hw *ah = sc->ah;
2767         u32 mfilt[2], val, rfilt;
2768         u8 pos;
2769         int i;
2770
2771         mfilt[0] = 0;
2772         mfilt[1] = 0;
2773
2774         /* Only deal with supported flags */
2775         changed_flags &= SUPPORTED_FIF_FLAGS;
2776         *new_flags &= SUPPORTED_FIF_FLAGS;
2777
2778         /* If HW detects any phy or radar errors, leave those filters on.
2779          * Also, always enable Unicast, Broadcasts and Multicast
2780          * XXX: move unicast, bssid broadcasts and multicast to mac80211 */
2781         rfilt = (ath5k_hw_get_rx_filter(ah) & (AR5K_RX_FILTER_PHYERR)) |
2782                 (AR5K_RX_FILTER_UCAST | AR5K_RX_FILTER_BCAST |
2783                 AR5K_RX_FILTER_MCAST);
2784
2785         if (changed_flags & (FIF_PROMISC_IN_BSS | FIF_OTHER_BSS)) {
2786                 if (*new_flags & FIF_PROMISC_IN_BSS) {
2787                         rfilt |= AR5K_RX_FILTER_PROM;
2788                         __set_bit(ATH_STAT_PROMISC, sc->status);
2789                 }
2790                 else
2791                         __clear_bit(ATH_STAT_PROMISC, sc->status);
2792         }
2793
2794         /* Note, AR5K_RX_FILTER_MCAST is already enabled */
2795         if (*new_flags & FIF_ALLMULTI) {
2796                 mfilt[0] =  ~0;
2797                 mfilt[1] =  ~0;
2798         } else {
2799                 for (i = 0; i < mc_count; i++) {
2800                         if (!mclist)
2801                                 break;
2802                         /* calculate XOR of eight 6-bit values */
2803                         val = LE_READ_4(mclist->dmi_addr + 0);
2804                         pos = (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
2805                         val = LE_READ_4(mclist->dmi_addr + 3);
2806                         pos ^= (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
2807                         pos &= 0x3f;
2808                         mfilt[pos / 32] |= (1 << (pos % 32));
2809                         /* XXX: we might be able to just do this instead,
2810                         * but not sure, needs testing, if we do use this we'd
2811                         * neet to inform below to not reset the mcast */
2812                         /* ath5k_hw_set_mcast_filterindex(ah,
2813                          *      mclist->dmi_addr[5]); */
2814                         mclist = mclist->next;
2815                 }
2816         }
2817
2818         /* This is the best we can do */
2819         if (*new_flags & (FIF_FCSFAIL | FIF_PLCPFAIL))
2820                 rfilt |= AR5K_RX_FILTER_PHYERR;
2821
2822         /* FIF_BCN_PRBRESP_PROMISC really means to enable beacons
2823         * and probes for any BSSID, this needs testing */
2824         if (*new_flags & FIF_BCN_PRBRESP_PROMISC)
2825                 rfilt |= AR5K_RX_FILTER_BEACON | AR5K_RX_FILTER_PROBEREQ;
2826
2827         /* FIF_CONTROL doc says that if FIF_PROMISC_IN_BSS is not
2828          * set we should only pass on control frames for this
2829          * station. This needs testing. I believe right now this
2830          * enables *all* control frames, which is OK.. but
2831          * but we should see if we can improve on granularity */
2832         if (*new_flags & FIF_CONTROL)
2833                 rfilt |= AR5K_RX_FILTER_CONTROL;
2834
2835         /* Additional settings per mode -- this is per ath5k */
2836
2837         /* XXX move these to mac80211, and add a beacon IFF flag to mac80211 */
2838
2839         if (sc->opmode == IEEE80211_IF_TYPE_MNTR)
2840                 rfilt |= AR5K_RX_FILTER_CONTROL | AR5K_RX_FILTER_BEACON |
2841                         AR5K_RX_FILTER_PROBEREQ | AR5K_RX_FILTER_PROM;
2842         if (sc->opmode != IEEE80211_IF_TYPE_STA)
2843                 rfilt |= AR5K_RX_FILTER_PROBEREQ;
2844         if (sc->opmode != IEEE80211_IF_TYPE_AP &&
2845                 test_bit(ATH_STAT_PROMISC, sc->status))
2846                 rfilt |= AR5K_RX_FILTER_PROM;
2847         if (sc->opmode == IEEE80211_IF_TYPE_STA ||
2848                 sc->opmode == IEEE80211_IF_TYPE_IBSS) {
2849                 rfilt |= AR5K_RX_FILTER_BEACON;
2850         }
2851
2852         /* Set filters */
2853         ath5k_hw_set_rx_filter(ah,rfilt);
2854
2855         /* Set multicast bits */
2856         ath5k_hw_set_mcast_filter(ah, mfilt[0], mfilt[1]);
2857         /* Set the cached hw filter flags, this will alter actually
2858          * be set in HW */
2859         sc->filter_flags = rfilt;
2860 }
2861
2862 static int
2863 ath5k_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
2864                 const u8 *local_addr, const u8 *addr,
2865                 struct ieee80211_key_conf *key)
2866 {
2867         struct ath5k_softc *sc = hw->priv;
2868         int ret = 0;
2869
2870         switch(key->alg) {
2871         case ALG_WEP:
2872                 break;
2873         case ALG_TKIP:
2874         case ALG_CCMP:
2875                 return -EOPNOTSUPP;
2876         default:
2877                 WARN_ON(1);
2878                 return -EINVAL;
2879         }
2880
2881         mutex_lock(&sc->lock);
2882
2883         switch (cmd) {
2884         case SET_KEY:
2885                 ret = ath5k_hw_set_key(sc->ah, key->keyidx, key, addr);
2886                 if (ret) {
2887                         ATH5K_ERR(sc, "can't set the key\n");
2888                         goto unlock;
2889                 }
2890                 __set_bit(key->keyidx, sc->keymap);
2891                 key->hw_key_idx = key->keyidx;
2892                 break;
2893         case DISABLE_KEY:
2894                 ath5k_hw_reset_key(sc->ah, key->keyidx);
2895                 __clear_bit(key->keyidx, sc->keymap);
2896                 break;
2897         default:
2898                 ret = -EINVAL;
2899                 goto unlock;
2900         }
2901
2902 unlock:
2903         mutex_unlock(&sc->lock);
2904         return ret;
2905 }
2906
2907 static int
2908 ath5k_get_stats(struct ieee80211_hw *hw,
2909                 struct ieee80211_low_level_stats *stats)
2910 {
2911         struct ath5k_softc *sc = hw->priv;
2912
2913         memcpy(stats, &sc->ll_stats, sizeof(sc->ll_stats));
2914
2915         return 0;
2916 }
2917
2918 static int
2919 ath5k_get_tx_stats(struct ieee80211_hw *hw,
2920                 struct ieee80211_tx_queue_stats *stats)
2921 {
2922         struct ath5k_softc *sc = hw->priv;
2923
2924         memcpy(stats, &sc->tx_stats, sizeof(sc->tx_stats));
2925
2926         return 0;
2927 }
2928
2929 static u64
2930 ath5k_get_tsf(struct ieee80211_hw *hw)
2931 {
2932         struct ath5k_softc *sc = hw->priv;
2933
2934         return ath5k_hw_get_tsf64(sc->ah);
2935 }
2936
2937 static void
2938 ath5k_reset_tsf(struct ieee80211_hw *hw)
2939 {
2940         struct ath5k_softc *sc = hw->priv;
2941
2942         /*
2943          * in IBSS mode we need to update the beacon timers too.
2944          * this will also reset the TSF if we call it with 0
2945          */
2946         if (sc->opmode == IEEE80211_IF_TYPE_IBSS)
2947                 ath5k_beacon_update_timers(sc, 0);
2948         else
2949                 ath5k_hw_reset_tsf(sc->ah);
2950 }
2951
2952 static int
2953 ath5k_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb,
2954                         struct ieee80211_tx_control *ctl)
2955 {
2956         struct ath5k_softc *sc = hw->priv;
2957         int ret;
2958
2959         ath5k_debug_dump_skb(sc, skb, "BC  ", 1);
2960
2961         mutex_lock(&sc->lock);
2962
2963         if (sc->opmode != IEEE80211_IF_TYPE_IBSS) {
2964                 ret = -EIO;
2965                 goto end;
2966         }
2967
2968         ath5k_txbuf_free(sc, sc->bbuf);
2969         sc->bbuf->skb = skb;
2970         ret = ath5k_beacon_setup(sc, sc->bbuf, ctl);
2971         if (ret)
2972                 sc->bbuf->skb = NULL;
2973         else
2974                 ath5k_beacon_config(sc);
2975
2976 end:
2977         mutex_unlock(&sc->lock);
2978         return ret;
2979 }
2980