blob: fc0ff713401c1ac1b317f832472767e2f3ddb5a6 [file] [log] [blame]
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001/*-
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
Jiri Slabyfa1c1142007-08-12 17:33:16 +020043#include <linux/module.h>
44#include <linux/delay.h>
Jiri Slaby274c7c32008-07-15 17:44:20 +020045#include <linux/hardirq.h>
Jiri Slabyfa1c1142007-08-12 17:33:16 +020046#include <linux/if.h>
Jiri Slaby274c7c32008-07-15 17:44:20 +020047#include <linux/io.h>
Jiri Slabyfa1c1142007-08-12 17:33:16 +020048#include <linux/netdevice.h>
49#include <linux/cache.h>
50#include <linux/pci.h>
51#include <linux/ethtool.h>
52#include <linux/uaccess.h>
53
54#include <net/ieee80211_radiotap.h>
55
56#include <asm/unaligned.h>
57
58#include "base.h"
59#include "reg.h"
60#include "debug.h"
61
Jiri Slabyfa1c1142007-08-12 17:33:16 +020062static int ath5k_calinterval = 10; /* Calibrate PHY every 10 secs (TODO: Fixme) */
Bob Copeland9ad9a262008-10-29 08:30:54 -040063static int modparam_nohwcrypt;
64module_param_named(nohwcrypt, modparam_nohwcrypt, int, 0444);
65MODULE_PARM_DESC(nohwcrypt, "Disable hardware encryption.");
Jiri Slabyfa1c1142007-08-12 17:33:16 +020066
67
68/******************\
69* Internal defines *
70\******************/
71
72/* Module info */
73MODULE_AUTHOR("Jiri Slaby");
74MODULE_AUTHOR("Nick Kossifidis");
75MODULE_DESCRIPTION("Support for 5xxx series of Atheros 802.11 wireless LAN cards.");
76MODULE_SUPPORTED_DEVICE("Atheros 5xxx WLAN cards");
77MODULE_LICENSE("Dual BSD/GPL");
Nick Kossifidis0d5f0312008-09-29 01:27:27 +030078MODULE_VERSION("0.6.0 (EXPERIMENTAL)");
Jiri Slabyfa1c1142007-08-12 17:33:16 +020079
80
81/* Known PCI ids */
82static struct pci_device_id ath5k_pci_id_table[] __devinitdata = {
83 { PCI_VDEVICE(ATHEROS, 0x0207), .driver_data = AR5K_AR5210 }, /* 5210 early */
84 { PCI_VDEVICE(ATHEROS, 0x0007), .driver_data = AR5K_AR5210 }, /* 5210 */
85 { PCI_VDEVICE(ATHEROS, 0x0011), .driver_data = AR5K_AR5211 }, /* 5311 - this is on AHB bus !*/
86 { PCI_VDEVICE(ATHEROS, 0x0012), .driver_data = AR5K_AR5211 }, /* 5211 */
87 { PCI_VDEVICE(ATHEROS, 0x0013), .driver_data = AR5K_AR5212 }, /* 5212 */
88 { PCI_VDEVICE(3COM_2, 0x0013), .driver_data = AR5K_AR5212 }, /* 3com 5212 */
89 { PCI_VDEVICE(3COM, 0x0013), .driver_data = AR5K_AR5212 }, /* 3com 3CRDAG675 5212 */
90 { PCI_VDEVICE(ATHEROS, 0x1014), .driver_data = AR5K_AR5212 }, /* IBM minipci 5212 */
91 { PCI_VDEVICE(ATHEROS, 0x0014), .driver_data = AR5K_AR5212 }, /* 5212 combatible */
92 { PCI_VDEVICE(ATHEROS, 0x0015), .driver_data = AR5K_AR5212 }, /* 5212 combatible */
93 { PCI_VDEVICE(ATHEROS, 0x0016), .driver_data = AR5K_AR5212 }, /* 5212 combatible */
94 { PCI_VDEVICE(ATHEROS, 0x0017), .driver_data = AR5K_AR5212 }, /* 5212 combatible */
95 { PCI_VDEVICE(ATHEROS, 0x0018), .driver_data = AR5K_AR5212 }, /* 5212 combatible */
96 { PCI_VDEVICE(ATHEROS, 0x0019), .driver_data = AR5K_AR5212 }, /* 5212 combatible */
97 { PCI_VDEVICE(ATHEROS, 0x001a), .driver_data = AR5K_AR5212 }, /* 2413 Griffin-lite */
98 { PCI_VDEVICE(ATHEROS, 0x001b), .driver_data = AR5K_AR5212 }, /* 5413 Eagle */
Nick Kossifidis0d5f0312008-09-29 01:27:27 +030099 { PCI_VDEVICE(ATHEROS, 0x001c), .driver_data = AR5K_AR5212 }, /* PCI-E cards */
100 { PCI_VDEVICE(ATHEROS, 0x001d), .driver_data = AR5K_AR5212 }, /* 2417 Nala */
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200101 { 0 }
102};
103MODULE_DEVICE_TABLE(pci, ath5k_pci_id_table);
104
105/* Known SREVs */
106static struct ath5k_srev_name srev_names[] = {
Nick Kossifidis1bef0162008-09-29 02:09:09 +0300107 { "5210", AR5K_VERSION_MAC, AR5K_SREV_AR5210 },
108 { "5311", AR5K_VERSION_MAC, AR5K_SREV_AR5311 },
109 { "5311A", AR5K_VERSION_MAC, AR5K_SREV_AR5311A },
110 { "5311B", AR5K_VERSION_MAC, AR5K_SREV_AR5311B },
111 { "5211", AR5K_VERSION_MAC, AR5K_SREV_AR5211 },
112 { "5212", AR5K_VERSION_MAC, AR5K_SREV_AR5212 },
113 { "5213", AR5K_VERSION_MAC, AR5K_SREV_AR5213 },
114 { "5213A", AR5K_VERSION_MAC, AR5K_SREV_AR5213A },
115 { "2413", AR5K_VERSION_MAC, AR5K_SREV_AR2413 },
116 { "2414", AR5K_VERSION_MAC, AR5K_SREV_AR2414 },
117 { "5424", AR5K_VERSION_MAC, AR5K_SREV_AR5424 },
118 { "5413", AR5K_VERSION_MAC, AR5K_SREV_AR5413 },
119 { "5414", AR5K_VERSION_MAC, AR5K_SREV_AR5414 },
120 { "2415", AR5K_VERSION_MAC, AR5K_SREV_AR2415 },
121 { "5416", AR5K_VERSION_MAC, AR5K_SREV_AR5416 },
122 { "5418", AR5K_VERSION_MAC, AR5K_SREV_AR5418 },
123 { "2425", AR5K_VERSION_MAC, AR5K_SREV_AR2425 },
124 { "2417", AR5K_VERSION_MAC, AR5K_SREV_AR2417 },
125 { "xxxxx", AR5K_VERSION_MAC, AR5K_SREV_UNKNOWN },
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200126 { "5110", AR5K_VERSION_RAD, AR5K_SREV_RAD_5110 },
127 { "5111", AR5K_VERSION_RAD, AR5K_SREV_RAD_5111 },
Nick Kossifidis1bef0162008-09-29 02:09:09 +0300128 { "5111A", AR5K_VERSION_RAD, AR5K_SREV_RAD_5111A },
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200129 { "2111", AR5K_VERSION_RAD, AR5K_SREV_RAD_2111 },
130 { "5112", AR5K_VERSION_RAD, AR5K_SREV_RAD_5112 },
131 { "5112A", AR5K_VERSION_RAD, AR5K_SREV_RAD_5112A },
Nick Kossifidis1bef0162008-09-29 02:09:09 +0300132 { "5112B", AR5K_VERSION_RAD, AR5K_SREV_RAD_5112B },
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200133 { "2112", AR5K_VERSION_RAD, AR5K_SREV_RAD_2112 },
134 { "2112A", AR5K_VERSION_RAD, AR5K_SREV_RAD_2112A },
Nick Kossifidis1bef0162008-09-29 02:09:09 +0300135 { "2112B", AR5K_VERSION_RAD, AR5K_SREV_RAD_2112B },
136 { "2413", AR5K_VERSION_RAD, AR5K_SREV_RAD_2413 },
137 { "5413", AR5K_VERSION_RAD, AR5K_SREV_RAD_5413 },
138 { "2316", AR5K_VERSION_RAD, AR5K_SREV_RAD_2316 },
139 { "2317", AR5K_VERSION_RAD, AR5K_SREV_RAD_2317 },
140 { "5424", AR5K_VERSION_RAD, AR5K_SREV_RAD_5424 },
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200141 { "5133", AR5K_VERSION_RAD, AR5K_SREV_RAD_5133 },
142 { "xxxxx", AR5K_VERSION_RAD, AR5K_SREV_UNKNOWN },
143};
144
Bruno Randolf63266a62008-07-30 17:12:58 +0200145static struct ieee80211_rate ath5k_rates[] = {
146 { .bitrate = 10,
147 .hw_value = ATH5K_RATE_CODE_1M, },
148 { .bitrate = 20,
149 .hw_value = ATH5K_RATE_CODE_2M,
150 .hw_value_short = ATH5K_RATE_CODE_2M | AR5K_SET_SHORT_PREAMBLE,
151 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
152 { .bitrate = 55,
153 .hw_value = ATH5K_RATE_CODE_5_5M,
154 .hw_value_short = ATH5K_RATE_CODE_5_5M | AR5K_SET_SHORT_PREAMBLE,
155 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
156 { .bitrate = 110,
157 .hw_value = ATH5K_RATE_CODE_11M,
158 .hw_value_short = ATH5K_RATE_CODE_11M | AR5K_SET_SHORT_PREAMBLE,
159 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
160 { .bitrate = 60,
161 .hw_value = ATH5K_RATE_CODE_6M,
162 .flags = 0 },
163 { .bitrate = 90,
164 .hw_value = ATH5K_RATE_CODE_9M,
165 .flags = 0 },
166 { .bitrate = 120,
167 .hw_value = ATH5K_RATE_CODE_12M,
168 .flags = 0 },
169 { .bitrate = 180,
170 .hw_value = ATH5K_RATE_CODE_18M,
171 .flags = 0 },
172 { .bitrate = 240,
173 .hw_value = ATH5K_RATE_CODE_24M,
174 .flags = 0 },
175 { .bitrate = 360,
176 .hw_value = ATH5K_RATE_CODE_36M,
177 .flags = 0 },
178 { .bitrate = 480,
179 .hw_value = ATH5K_RATE_CODE_48M,
180 .flags = 0 },
181 { .bitrate = 540,
182 .hw_value = ATH5K_RATE_CODE_54M,
183 .flags = 0 },
184 /* XR missing */
185};
186
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200187/*
188 * Prototypes - PCI stack related functions
189 */
190static int __devinit ath5k_pci_probe(struct pci_dev *pdev,
191 const struct pci_device_id *id);
192static void __devexit ath5k_pci_remove(struct pci_dev *pdev);
193#ifdef CONFIG_PM
194static int ath5k_pci_suspend(struct pci_dev *pdev,
195 pm_message_t state);
196static int ath5k_pci_resume(struct pci_dev *pdev);
197#else
198#define ath5k_pci_suspend NULL
199#define ath5k_pci_resume NULL
200#endif /* CONFIG_PM */
201
John W. Linville04a9e452008-02-01 16:03:45 -0500202static struct pci_driver ath5k_pci_driver = {
Johannes Berg9764f3f2008-11-10 18:56:59 +0100203 .name = KBUILD_MODNAME,
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200204 .id_table = ath5k_pci_id_table,
205 .probe = ath5k_pci_probe,
206 .remove = __devexit_p(ath5k_pci_remove),
207 .suspend = ath5k_pci_suspend,
208 .resume = ath5k_pci_resume,
209};
210
211
212
213/*
214 * Prototypes - MAC 802.11 stack related functions
215 */
Johannes Berge039fa42008-05-15 12:55:29 +0200216static int ath5k_tx(struct ieee80211_hw *hw, struct sk_buff *skb);
Jiri Slabyd7dc1002008-07-23 13:17:35 +0200217static int ath5k_reset(struct ath5k_softc *sc, bool stop, bool change_channel);
218static int ath5k_reset_wake(struct ath5k_softc *sc);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200219static int ath5k_start(struct ieee80211_hw *hw);
220static void ath5k_stop(struct ieee80211_hw *hw);
221static int ath5k_add_interface(struct ieee80211_hw *hw,
222 struct ieee80211_if_init_conf *conf);
223static void ath5k_remove_interface(struct ieee80211_hw *hw,
224 struct ieee80211_if_init_conf *conf);
Johannes Berge8975582008-10-09 12:18:51 +0200225static int ath5k_config(struct ieee80211_hw *hw, u32 changed);
Johannes Berg32bfd352007-12-19 01:31:26 +0100226static int ath5k_config_interface(struct ieee80211_hw *hw,
227 struct ieee80211_vif *vif,
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200228 struct ieee80211_if_conf *conf);
229static void ath5k_configure_filter(struct ieee80211_hw *hw,
230 unsigned int changed_flags,
231 unsigned int *new_flags,
232 int mc_count, struct dev_mc_list *mclist);
233static int ath5k_set_key(struct ieee80211_hw *hw,
234 enum set_key_cmd cmd,
Johannes Bergdc822b52008-12-29 12:55:09 +0100235 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200236 struct ieee80211_key_conf *key);
237static int ath5k_get_stats(struct ieee80211_hw *hw,
238 struct ieee80211_low_level_stats *stats);
239static int ath5k_get_tx_stats(struct ieee80211_hw *hw,
240 struct ieee80211_tx_queue_stats *stats);
241static u64 ath5k_get_tsf(struct ieee80211_hw *hw);
242static void ath5k_reset_tsf(struct ieee80211_hw *hw);
David S. Miller5b9ab2e2008-11-26 23:48:40 -0800243static int ath5k_beacon_update(struct ath5k_softc *sc,
Johannes Berge039fa42008-05-15 12:55:29 +0200244 struct sk_buff *skb);
Martin Xu02969b32008-11-24 10:49:27 +0800245static void ath5k_bss_info_changed(struct ieee80211_hw *hw,
246 struct ieee80211_vif *vif,
247 struct ieee80211_bss_conf *bss_conf,
248 u32 changes);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200249
250static struct ieee80211_ops ath5k_hw_ops = {
251 .tx = ath5k_tx,
252 .start = ath5k_start,
253 .stop = ath5k_stop,
254 .add_interface = ath5k_add_interface,
255 .remove_interface = ath5k_remove_interface,
256 .config = ath5k_config,
257 .config_interface = ath5k_config_interface,
258 .configure_filter = ath5k_configure_filter,
259 .set_key = ath5k_set_key,
260 .get_stats = ath5k_get_stats,
261 .conf_tx = NULL,
262 .get_tx_stats = ath5k_get_tx_stats,
263 .get_tsf = ath5k_get_tsf,
264 .reset_tsf = ath5k_reset_tsf,
Martin Xu02969b32008-11-24 10:49:27 +0800265 .bss_info_changed = ath5k_bss_info_changed,
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200266};
267
268/*
269 * Prototypes - Internal functions
270 */
271/* Attach detach */
272static int ath5k_attach(struct pci_dev *pdev,
273 struct ieee80211_hw *hw);
274static void ath5k_detach(struct pci_dev *pdev,
275 struct ieee80211_hw *hw);
276/* Channel/mode setup */
277static inline short ath5k_ieee2mhz(short chan);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200278static unsigned int ath5k_copy_channels(struct ath5k_hw *ah,
279 struct ieee80211_channel *channels,
280 unsigned int mode,
281 unsigned int max);
Bruno Randolf63266a62008-07-30 17:12:58 +0200282static int ath5k_setup_bands(struct ieee80211_hw *hw);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200283static int ath5k_chan_set(struct ath5k_softc *sc,
284 struct ieee80211_channel *chan);
285static void ath5k_setcurmode(struct ath5k_softc *sc,
286 unsigned int mode);
287static void ath5k_mode_setup(struct ath5k_softc *sc);
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500288
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200289/* Descriptor setup */
290static int ath5k_desc_alloc(struct ath5k_softc *sc,
291 struct pci_dev *pdev);
292static void ath5k_desc_free(struct ath5k_softc *sc,
293 struct pci_dev *pdev);
294/* Buffers setup */
295static int ath5k_rxbuf_setup(struct ath5k_softc *sc,
296 struct ath5k_buf *bf);
297static int ath5k_txbuf_setup(struct ath5k_softc *sc,
Johannes Berge039fa42008-05-15 12:55:29 +0200298 struct ath5k_buf *bf);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200299static inline void ath5k_txbuf_free(struct ath5k_softc *sc,
300 struct ath5k_buf *bf)
301{
302 BUG_ON(!bf);
303 if (!bf->skb)
304 return;
305 pci_unmap_single(sc->pdev, bf->skbaddr, bf->skb->len,
306 PCI_DMA_TODEVICE);
Jiri Slaby00482972008-08-18 21:45:27 +0200307 dev_kfree_skb_any(bf->skb);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200308 bf->skb = NULL;
309}
310
311/* Queues setup */
312static struct ath5k_txq *ath5k_txq_setup(struct ath5k_softc *sc,
313 int qtype, int subtype);
314static int ath5k_beaconq_setup(struct ath5k_hw *ah);
315static int ath5k_beaconq_config(struct ath5k_softc *sc);
316static void ath5k_txq_drainq(struct ath5k_softc *sc,
317 struct ath5k_txq *txq);
318static void ath5k_txq_cleanup(struct ath5k_softc *sc);
319static void ath5k_txq_release(struct ath5k_softc *sc);
320/* Rx handling */
321static int ath5k_rx_start(struct ath5k_softc *sc);
322static void ath5k_rx_stop(struct ath5k_softc *sc);
323static unsigned int ath5k_rx_decrypted(struct ath5k_softc *sc,
324 struct ath5k_desc *ds,
Bruno Randolfb47f4072008-03-05 18:35:45 +0900325 struct sk_buff *skb,
326 struct ath5k_rx_status *rs);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200327static void ath5k_tasklet_rx(unsigned long data);
328/* Tx handling */
329static void ath5k_tx_processq(struct ath5k_softc *sc,
330 struct ath5k_txq *txq);
331static void ath5k_tasklet_tx(unsigned long data);
332/* Beacon handling */
333static int ath5k_beacon_setup(struct ath5k_softc *sc,
Johannes Berge039fa42008-05-15 12:55:29 +0200334 struct ath5k_buf *bf);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200335static void ath5k_beacon_send(struct ath5k_softc *sc);
336static void ath5k_beacon_config(struct ath5k_softc *sc);
Bruno Randolf9804b982008-01-19 18:17:59 +0900337static void ath5k_beacon_update_timers(struct ath5k_softc *sc, u64 bc_tsf);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200338
339static inline u64 ath5k_extend_tsf(struct ath5k_hw *ah, u32 rstamp)
340{
341 u64 tsf = ath5k_hw_get_tsf64(ah);
342
343 if ((tsf & 0x7fff) < rstamp)
344 tsf -= 0x8000;
345
346 return (tsf & ~0x7fff) | rstamp;
347}
348
349/* Interrupt handling */
Bob Copelandbb2beca2009-01-19 11:20:54 -0500350static int ath5k_init(struct ath5k_softc *sc);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200351static int ath5k_stop_locked(struct ath5k_softc *sc);
Bob Copelandbb2beca2009-01-19 11:20:54 -0500352static int ath5k_stop_hw(struct ath5k_softc *sc);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200353static irqreturn_t ath5k_intr(int irq, void *dev_id);
354static void ath5k_tasklet_reset(unsigned long data);
355
356static void ath5k_calibrate(unsigned long data);
357/* LED functions */
Bob Copeland3a078872008-06-25 22:35:28 -0400358static int ath5k_init_leds(struct ath5k_softc *sc);
359static void ath5k_led_enable(struct ath5k_softc *sc);
360static void ath5k_led_off(struct ath5k_softc *sc);
361static void ath5k_unregister_leds(struct ath5k_softc *sc);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200362
363/*
364 * Module init/exit functions
365 */
366static int __init
367init_ath5k_pci(void)
368{
369 int ret;
370
371 ath5k_debug_init();
372
John W. Linville04a9e452008-02-01 16:03:45 -0500373 ret = pci_register_driver(&ath5k_pci_driver);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200374 if (ret) {
375 printk(KERN_ERR "ath5k_pci: can't register pci driver\n");
376 return ret;
377 }
378
379 return 0;
380}
381
382static void __exit
383exit_ath5k_pci(void)
384{
John W. Linville04a9e452008-02-01 16:03:45 -0500385 pci_unregister_driver(&ath5k_pci_driver);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200386
387 ath5k_debug_finish();
388}
389
390module_init(init_ath5k_pci);
391module_exit(exit_ath5k_pci);
392
393
394/********************\
395* PCI Initialization *
396\********************/
397
398static const char *
399ath5k_chip_name(enum ath5k_srev_type type, u_int16_t val)
400{
401 const char *name = "xxxxx";
402 unsigned int i;
403
404 for (i = 0; i < ARRAY_SIZE(srev_names); i++) {
405 if (srev_names[i].sr_type != type)
406 continue;
Nick Kossifidis75d0edb2008-09-29 01:24:44 +0300407
408 if ((val & 0xf0) == srev_names[i].sr_val)
409 name = srev_names[i].sr_name;
410
411 if ((val & 0xff) == srev_names[i].sr_val) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200412 name = srev_names[i].sr_name;
413 break;
414 }
415 }
416
417 return name;
418}
419
420static int __devinit
421ath5k_pci_probe(struct pci_dev *pdev,
422 const struct pci_device_id *id)
423{
424 void __iomem *mem;
425 struct ath5k_softc *sc;
426 struct ieee80211_hw *hw;
427 int ret;
428 u8 csz;
429
430 ret = pci_enable_device(pdev);
431 if (ret) {
432 dev_err(&pdev->dev, "can't enable device\n");
433 goto err;
434 }
435
436 /* XXX 32-bit addressing only */
437 ret = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
438 if (ret) {
439 dev_err(&pdev->dev, "32-bit DMA not available\n");
440 goto err_dis;
441 }
442
443 /*
444 * Cache line size is used to size and align various
445 * structures used to communicate with the hardware.
446 */
447 pci_read_config_byte(pdev, PCI_CACHE_LINE_SIZE, &csz);
448 if (csz == 0) {
449 /*
450 * Linux 2.4.18 (at least) writes the cache line size
451 * register as a 16-bit wide register which is wrong.
452 * We must have this setup properly for rx buffer
453 * DMA to work so force a reasonable value here if it
454 * comes up zero.
455 */
456 csz = L1_CACHE_BYTES / sizeof(u32);
457 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, csz);
458 }
459 /*
460 * The default setting of latency timer yields poor results,
461 * set it to the value used by other systems. It may be worth
462 * tweaking this setting more.
463 */
464 pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0xa8);
465
466 /* Enable bus mastering */
467 pci_set_master(pdev);
468
469 /*
470 * Disable the RETRY_TIMEOUT register (0x41) to keep
471 * PCI Tx retries from interfering with C3 CPU state.
472 */
473 pci_write_config_byte(pdev, 0x41, 0);
474
475 ret = pci_request_region(pdev, 0, "ath5k");
476 if (ret) {
477 dev_err(&pdev->dev, "cannot reserve PCI memory region\n");
478 goto err_dis;
479 }
480
481 mem = pci_iomap(pdev, 0, 0);
482 if (!mem) {
483 dev_err(&pdev->dev, "cannot remap PCI memory region\n") ;
484 ret = -EIO;
485 goto err_reg;
486 }
487
488 /*
489 * Allocate hw (mac80211 main struct)
490 * and hw->priv (driver private data)
491 */
492 hw = ieee80211_alloc_hw(sizeof(*sc), &ath5k_hw_ops);
493 if (hw == NULL) {
494 dev_err(&pdev->dev, "cannot allocate ieee80211_hw\n");
495 ret = -ENOMEM;
496 goto err_map;
497 }
498
499 dev_info(&pdev->dev, "registered as '%s'\n", wiphy_name(hw->wiphy));
500
501 /* Initialize driver private data */
502 SET_IEEE80211_DEV(hw, &pdev->dev);
Bruno Randolf566bfe52008-05-08 19:15:40 +0200503 hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
504 IEEE80211_HW_SIGNAL_DBM |
505 IEEE80211_HW_NOISE_DBM;
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700506
507 hw->wiphy->interface_modes =
508 BIT(NL80211_IFTYPE_STATION) |
509 BIT(NL80211_IFTYPE_ADHOC) |
510 BIT(NL80211_IFTYPE_MESH_POINT);
511
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200512 hw->extra_tx_headroom = 2;
513 hw->channel_change_time = 5000;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200514 sc = hw->priv;
515 sc->hw = hw;
516 sc->pdev = pdev;
517
518 ath5k_debug_init_device(sc);
519
520 /*
521 * Mark the device as detached to avoid processing
522 * interrupts until setup is complete.
523 */
524 __set_bit(ATH_STAT_INVALID, sc->status);
525
526 sc->iobase = mem; /* So we can unmap it on detach */
527 sc->cachelsz = csz * sizeof(u32); /* convert to bytes */
Johannes Berg05c914f2008-09-11 00:01:58 +0200528 sc->opmode = NL80211_IFTYPE_STATION;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200529 mutex_init(&sc->lock);
530 spin_lock_init(&sc->rxbuflock);
531 spin_lock_init(&sc->txbuflock);
Jiri Slaby00482972008-08-18 21:45:27 +0200532 spin_lock_init(&sc->block);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200533
534 /* Set private data */
535 pci_set_drvdata(pdev, hw);
536
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200537 /* Setup interrupt handler */
538 ret = request_irq(pdev->irq, ath5k_intr, IRQF_SHARED, "ath", sc);
539 if (ret) {
540 ATH5K_ERR(sc, "request_irq failed\n");
541 goto err_free;
542 }
543
544 /* Initialize device */
545 sc->ah = ath5k_hw_attach(sc, id->driver_data);
546 if (IS_ERR(sc->ah)) {
547 ret = PTR_ERR(sc->ah);
548 goto err_irq;
549 }
550
Felix Fietkau2f7fe872008-10-05 18:05:48 +0200551 /* set up multi-rate retry capabilities */
552 if (sc->ah->ah_version == AR5K_AR5212) {
Johannes Berge6a98542008-10-21 12:40:02 +0200553 hw->max_rates = 4;
554 hw->max_rate_tries = 11;
Felix Fietkau2f7fe872008-10-05 18:05:48 +0200555 }
556
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200557 /* Finish private driver data initialization */
558 ret = ath5k_attach(pdev, hw);
559 if (ret)
560 goto err_ah;
561
562 ATH5K_INFO(sc, "Atheros AR%s chip found (MAC: 0x%x, PHY: 0x%x)\n",
Nick Kossifidis1bef0162008-09-29 02:09:09 +0300563 ath5k_chip_name(AR5K_VERSION_MAC, sc->ah->ah_mac_srev),
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200564 sc->ah->ah_mac_srev,
565 sc->ah->ah_phy_revision);
566
Luis R. Rodriguez400ec452008-02-03 21:51:49 -0500567 if (!sc->ah->ah_single_chip) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200568 /* Single chip radio (!RF5111) */
Luis R. Rodriguez400ec452008-02-03 21:51:49 -0500569 if (sc->ah->ah_radio_5ghz_revision &&
570 !sc->ah->ah_radio_2ghz_revision) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200571 /* No 5GHz support -> report 2GHz radio */
Luis R. Rodriguez400ec452008-02-03 21:51:49 -0500572 if (!test_bit(AR5K_MODE_11A,
573 sc->ah->ah_capabilities.cap_mode)) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200574 ATH5K_INFO(sc, "RF%s 2GHz radio found (0x%x)\n",
Luis R. Rodriguez400ec452008-02-03 21:51:49 -0500575 ath5k_chip_name(AR5K_VERSION_RAD,
576 sc->ah->ah_radio_5ghz_revision),
577 sc->ah->ah_radio_5ghz_revision);
578 /* No 2GHz support (5110 and some
579 * 5Ghz only cards) -> report 5Ghz radio */
580 } else if (!test_bit(AR5K_MODE_11B,
581 sc->ah->ah_capabilities.cap_mode)) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200582 ATH5K_INFO(sc, "RF%s 5GHz radio found (0x%x)\n",
Luis R. Rodriguez400ec452008-02-03 21:51:49 -0500583 ath5k_chip_name(AR5K_VERSION_RAD,
584 sc->ah->ah_radio_5ghz_revision),
585 sc->ah->ah_radio_5ghz_revision);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200586 /* Multiband radio */
587 } else {
588 ATH5K_INFO(sc, "RF%s multiband radio found"
589 " (0x%x)\n",
Luis R. Rodriguez400ec452008-02-03 21:51:49 -0500590 ath5k_chip_name(AR5K_VERSION_RAD,
591 sc->ah->ah_radio_5ghz_revision),
592 sc->ah->ah_radio_5ghz_revision);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200593 }
594 }
Luis R. Rodriguez400ec452008-02-03 21:51:49 -0500595 /* Multi chip radio (RF5111 - RF2111) ->
596 * report both 2GHz/5GHz radios */
597 else if (sc->ah->ah_radio_5ghz_revision &&
598 sc->ah->ah_radio_2ghz_revision){
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200599 ATH5K_INFO(sc, "RF%s 5GHz radio found (0x%x)\n",
Luis R. Rodriguez400ec452008-02-03 21:51:49 -0500600 ath5k_chip_name(AR5K_VERSION_RAD,
601 sc->ah->ah_radio_5ghz_revision),
602 sc->ah->ah_radio_5ghz_revision);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200603 ATH5K_INFO(sc, "RF%s 2GHz radio found (0x%x)\n",
Luis R. Rodriguez400ec452008-02-03 21:51:49 -0500604 ath5k_chip_name(AR5K_VERSION_RAD,
605 sc->ah->ah_radio_2ghz_revision),
606 sc->ah->ah_radio_2ghz_revision);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200607 }
608 }
609
610
611 /* ready to process interrupts */
612 __clear_bit(ATH_STAT_INVALID, sc->status);
613
614 return 0;
615err_ah:
616 ath5k_hw_detach(sc->ah);
617err_irq:
618 free_irq(pdev->irq, sc);
619err_free:
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200620 ieee80211_free_hw(hw);
621err_map:
622 pci_iounmap(pdev, mem);
623err_reg:
624 pci_release_region(pdev, 0);
625err_dis:
626 pci_disable_device(pdev);
627err:
628 return ret;
629}
630
631static void __devexit
632ath5k_pci_remove(struct pci_dev *pdev)
633{
634 struct ieee80211_hw *hw = pci_get_drvdata(pdev);
635 struct ath5k_softc *sc = hw->priv;
636
637 ath5k_debug_finish_device(sc);
638 ath5k_detach(pdev, hw);
639 ath5k_hw_detach(sc->ah);
640 free_irq(pdev->irq, sc);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200641 pci_iounmap(pdev, sc->iobase);
642 pci_release_region(pdev, 0);
643 pci_disable_device(pdev);
644 ieee80211_free_hw(hw);
645}
646
647#ifdef CONFIG_PM
648static int
649ath5k_pci_suspend(struct pci_dev *pdev, pm_message_t state)
650{
651 struct ieee80211_hw *hw = pci_get_drvdata(pdev);
652 struct ath5k_softc *sc = hw->priv;
653
Bob Copeland3a078872008-06-25 22:35:28 -0400654 ath5k_led_off(sc);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200655
Jiri Slaby3e4242b2008-07-15 17:44:21 +0200656 free_irq(pdev->irq, sc);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200657 pci_save_state(pdev);
658 pci_disable_device(pdev);
659 pci_set_power_state(pdev, PCI_D3hot);
660
661 return 0;
662}
663
664static int
665ath5k_pci_resume(struct pci_dev *pdev)
666{
667 struct ieee80211_hw *hw = pci_get_drvdata(pdev);
668 struct ath5k_softc *sc = hw->priv;
Elias Oltmannsbc1b32d2008-10-24 21:59:18 +0200669 int err;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200670
Jiri Slaby3e4242b2008-07-15 17:44:21 +0200671 pci_restore_state(pdev);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200672
673 err = pci_enable_device(pdev);
674 if (err)
675 return err;
676
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200677 /*
678 * Suspend/Resume resets the PCI configuration space, so we have to
679 * re-disable the RETRY_TIMEOUT register (0x41) to keep
680 * PCI Tx retries from interfering with C3 CPU state
681 */
682 pci_write_config_byte(pdev, 0x41, 0);
683
Jiri Slaby3e4242b2008-07-15 17:44:21 +0200684 err = request_irq(pdev->irq, ath5k_intr, IRQF_SHARED, "ath", sc);
685 if (err) {
686 ATH5K_ERR(sc, "request_irq failed\n");
Michael Karcher37465c82008-08-07 19:34:01 +0200687 goto err_no_irq;
Jiri Slaby3e4242b2008-07-15 17:44:21 +0200688 }
689
Bob Copeland3a078872008-06-25 22:35:28 -0400690 ath5k_led_enable(sc);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200691 return 0;
Bob Copelandbb2beca2009-01-19 11:20:54 -0500692
Michael Karcher37465c82008-08-07 19:34:01 +0200693err_no_irq:
Jiri Slaby3e4242b2008-07-15 17:44:21 +0200694 pci_disable_device(pdev);
695 return err;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200696}
697#endif /* CONFIG_PM */
698
699
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200700/***********************\
701* Driver Initialization *
702\***********************/
703
704static int
705ath5k_attach(struct pci_dev *pdev, struct ieee80211_hw *hw)
706{
707 struct ath5k_softc *sc = hw->priv;
708 struct ath5k_hw *ah = sc->ah;
Bob Copeland0e149cf2008-11-17 23:40:38 -0500709 u8 mac[ETH_ALEN] = {};
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200710 int ret;
711
712 ATH5K_DBG(sc, ATH5K_DEBUG_ANY, "devid 0x%x\n", pdev->device);
713
714 /*
715 * Check if the MAC has multi-rate retry support.
716 * We do this by trying to setup a fake extended
717 * descriptor. MAC's that don't have support will
718 * return false w/o doing anything. MAC's that do
719 * support it will return true w/o doing anything.
720 */
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300721 ret = ah->ah_setup_mrr_tx_desc(ah, NULL, 0, 0, 0, 0, 0, 0);
Jiri Slabyb9887632008-02-15 21:58:52 +0100722 if (ret < 0)
723 goto err;
724 if (ret > 0)
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200725 __set_bit(ATH_STAT_MRRETRY, sc->status);
726
727 /*
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200728 * Collect the channel list. The 802.11 layer
729 * is resposible for filtering this list based
730 * on settings like the phy mode and regulatory
731 * domain restrictions.
732 */
Bruno Randolf63266a62008-07-30 17:12:58 +0200733 ret = ath5k_setup_bands(hw);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200734 if (ret) {
735 ATH5K_ERR(sc, "can't get channels\n");
736 goto err;
737 }
738
739 /* NB: setup here so ath5k_rate_update is happy */
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500740 if (test_bit(AR5K_MODE_11A, ah->ah_modes))
741 ath5k_setcurmode(sc, AR5K_MODE_11A);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200742 else
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500743 ath5k_setcurmode(sc, AR5K_MODE_11B);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200744
745 /*
746 * Allocate tx+rx descriptors and populate the lists.
747 */
748 ret = ath5k_desc_alloc(sc, pdev);
749 if (ret) {
750 ATH5K_ERR(sc, "can't allocate descriptors\n");
751 goto err;
752 }
753
754 /*
755 * Allocate hardware transmit queues: one queue for
756 * beacon frames and one data queue for each QoS
757 * priority. Note that hw functions handle reseting
758 * these queues at the needed time.
759 */
760 ret = ath5k_beaconq_setup(ah);
761 if (ret < 0) {
762 ATH5K_ERR(sc, "can't setup a beacon xmit queue\n");
763 goto err_desc;
764 }
765 sc->bhalq = ret;
766
767 sc->txq = ath5k_txq_setup(sc, AR5K_TX_QUEUE_DATA, AR5K_WME_AC_BK);
768 if (IS_ERR(sc->txq)) {
769 ATH5K_ERR(sc, "can't setup xmit queue\n");
770 ret = PTR_ERR(sc->txq);
771 goto err_bhal;
772 }
773
774 tasklet_init(&sc->rxtq, ath5k_tasklet_rx, (unsigned long)sc);
775 tasklet_init(&sc->txtq, ath5k_tasklet_tx, (unsigned long)sc);
776 tasklet_init(&sc->restq, ath5k_tasklet_reset, (unsigned long)sc);
777 setup_timer(&sc->calib_tim, ath5k_calibrate, (unsigned long)sc);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200778
Bob Copeland0e149cf2008-11-17 23:40:38 -0500779 ret = ath5k_eeprom_read_mac(ah, mac);
780 if (ret) {
781 ATH5K_ERR(sc, "unable to read address from EEPROM: 0x%04x\n",
782 sc->pdev->device);
783 goto err_queues;
784 }
785
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200786 SET_IEEE80211_PERM_ADDR(hw, mac);
787 /* All MAC address bits matter for ACKs */
788 memset(sc->bssidmask, 0xff, ETH_ALEN);
789 ath5k_hw_set_bssid_mask(sc->ah, sc->bssidmask);
790
791 ret = ieee80211_register_hw(hw);
792 if (ret) {
793 ATH5K_ERR(sc, "can't register ieee80211 hw\n");
794 goto err_queues;
795 }
796
Bob Copeland3a078872008-06-25 22:35:28 -0400797 ath5k_init_leds(sc);
798
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200799 return 0;
800err_queues:
801 ath5k_txq_release(sc);
802err_bhal:
803 ath5k_hw_release_tx_queue(ah, sc->bhalq);
804err_desc:
805 ath5k_desc_free(sc, pdev);
806err:
807 return ret;
808}
809
810static void
811ath5k_detach(struct pci_dev *pdev, struct ieee80211_hw *hw)
812{
813 struct ath5k_softc *sc = hw->priv;
814
815 /*
816 * NB: the order of these is important:
817 * o call the 802.11 layer before detaching ath5k_hw to
818 * insure callbacks into the driver to delete global
819 * key cache entries can be handled
820 * o reclaim the tx queue data structures after calling
821 * the 802.11 layer as we'll get called back to reclaim
822 * node state and potentially want to use them
823 * o to cleanup the tx queues the hal is called, so detach
824 * it last
825 * XXX: ??? detach ath5k_hw ???
826 * Other than that, it's straightforward...
827 */
828 ieee80211_unregister_hw(hw);
829 ath5k_desc_free(sc, pdev);
830 ath5k_txq_release(sc);
831 ath5k_hw_release_tx_queue(sc->ah, sc->bhalq);
Bob Copeland3a078872008-06-25 22:35:28 -0400832 ath5k_unregister_leds(sc);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200833
834 /*
835 * NB: can't reclaim these until after ieee80211_ifdetach
836 * returns because we'll get called back to reclaim node
837 * state and potentially want to use them.
838 */
839}
840
841
842
843
844/********************\
845* Channel/mode setup *
846\********************/
847
848/*
849 * Convert IEEE channel number to MHz frequency.
850 */
851static inline short
852ath5k_ieee2mhz(short chan)
853{
854 if (chan <= 14 || chan >= 27)
855 return ieee80211chan2mhz(chan);
856 else
857 return 2212 + chan * 20;
858}
859
860static unsigned int
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200861ath5k_copy_channels(struct ath5k_hw *ah,
862 struct ieee80211_channel *channels,
863 unsigned int mode,
864 unsigned int max)
865{
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500866 unsigned int i, count, size, chfreq, freq, ch;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200867
868 if (!test_bit(mode, ah->ah_modes))
869 return 0;
870
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200871 switch (mode) {
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500872 case AR5K_MODE_11A:
873 case AR5K_MODE_11A_TURBO:
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200874 /* 1..220, but 2GHz frequencies are filtered by check_channel */
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500875 size = 220 ;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200876 chfreq = CHANNEL_5GHZ;
877 break;
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500878 case AR5K_MODE_11B:
879 case AR5K_MODE_11G:
880 case AR5K_MODE_11G_TURBO:
881 size = 26;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200882 chfreq = CHANNEL_2GHZ;
883 break;
884 default:
885 ATH5K_WARN(ah->ah_sc, "bad mode, not copying channels\n");
886 return 0;
887 }
888
889 for (i = 0, count = 0; i < size && max > 0; i++) {
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500890 ch = i + 1 ;
891 freq = ath5k_ieee2mhz(ch);
892
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200893 /* Check if channel is supported by the chipset */
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500894 if (!ath5k_channel_ok(ah, freq, chfreq))
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200895 continue;
896
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500897 /* Write channel info and increment counter */
898 channels[count].center_freq = freq;
Luis R. Rodrigueza3f4b912008-02-03 21:52:10 -0500899 channels[count].band = (chfreq == CHANNEL_2GHZ) ?
900 IEEE80211_BAND_2GHZ : IEEE80211_BAND_5GHZ;
Luis R. Rodriguez400ec452008-02-03 21:51:49 -0500901 switch (mode) {
902 case AR5K_MODE_11A:
903 case AR5K_MODE_11G:
904 channels[count].hw_value = chfreq | CHANNEL_OFDM;
905 break;
906 case AR5K_MODE_11A_TURBO:
907 case AR5K_MODE_11G_TURBO:
908 channels[count].hw_value = chfreq |
909 CHANNEL_OFDM | CHANNEL_TURBO;
910 break;
911 case AR5K_MODE_11B:
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500912 channels[count].hw_value = CHANNEL_B;
913 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200914
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200915 count++;
916 max--;
917 }
918
919 return count;
920}
921
Bruno Randolf63266a62008-07-30 17:12:58 +0200922static void
923ath5k_setup_rate_idx(struct ath5k_softc *sc, struct ieee80211_supported_band *b)
924{
925 u8 i;
926
927 for (i = 0; i < AR5K_MAX_RATES; i++)
928 sc->rate_idx[b->band][i] = -1;
929
930 for (i = 0; i < b->n_bitrates; i++) {
931 sc->rate_idx[b->band][b->bitrates[i].hw_value] = i;
932 if (b->bitrates[i].hw_value_short)
933 sc->rate_idx[b->band][b->bitrates[i].hw_value_short] = i;
934 }
935}
936
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200937static int
Bruno Randolf63266a62008-07-30 17:12:58 +0200938ath5k_setup_bands(struct ieee80211_hw *hw)
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200939{
940 struct ath5k_softc *sc = hw->priv;
941 struct ath5k_hw *ah = sc->ah;
Bruno Randolf63266a62008-07-30 17:12:58 +0200942 struct ieee80211_supported_band *sband;
943 int max_c, count_c = 0;
944 int i;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200945
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500946 BUILD_BUG_ON(ARRAY_SIZE(sc->sbands) < IEEE80211_NUM_BANDS);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200947 max_c = ARRAY_SIZE(sc->channels);
948
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500949 /* 2GHz band */
Bruno Randolf63266a62008-07-30 17:12:58 +0200950 sband = &sc->sbands[IEEE80211_BAND_2GHZ];
951 sband->band = IEEE80211_BAND_2GHZ;
952 sband->bitrates = &sc->rates[IEEE80211_BAND_2GHZ][0];
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200953
Bruno Randolf63266a62008-07-30 17:12:58 +0200954 if (test_bit(AR5K_MODE_11G, sc->ah->ah_capabilities.cap_mode)) {
955 /* G mode */
956 memcpy(sband->bitrates, &ath5k_rates[0],
957 sizeof(struct ieee80211_rate) * 12);
958 sband->n_bitrates = 12;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200959
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500960 sband->channels = sc->channels;
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500961 sband->n_channels = ath5k_copy_channels(ah, sband->channels,
Bruno Randolf63266a62008-07-30 17:12:58 +0200962 AR5K_MODE_11G, max_c);
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500963
964 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = sband;
Bruno Randolf63266a62008-07-30 17:12:58 +0200965 count_c = sband->n_channels;
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500966 max_c -= count_c;
Bruno Randolf63266a62008-07-30 17:12:58 +0200967 } else if (test_bit(AR5K_MODE_11B, sc->ah->ah_capabilities.cap_mode)) {
968 /* B mode */
969 memcpy(sband->bitrates, &ath5k_rates[0],
970 sizeof(struct ieee80211_rate) * 4);
971 sband->n_bitrates = 4;
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500972
Bruno Randolf63266a62008-07-30 17:12:58 +0200973 /* 5211 only supports B rates and uses 4bit rate codes
974 * (e.g normally we have 0x1B for 1M, but on 5211 we have 0x0B)
975 * fix them up here:
976 */
977 if (ah->ah_version == AR5K_AR5211) {
978 for (i = 0; i < 4; i++) {
979 sband->bitrates[i].hw_value =
980 sband->bitrates[i].hw_value & 0xF;
981 sband->bitrates[i].hw_value_short =
982 sband->bitrates[i].hw_value_short & 0xF;
983 }
984 }
985
986 sband->channels = sc->channels;
987 sband->n_channels = ath5k_copy_channels(ah, sband->channels,
988 AR5K_MODE_11B, max_c);
989
990 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = sband;
991 count_c = sband->n_channels;
992 max_c -= count_c;
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500993 }
Bruno Randolf63266a62008-07-30 17:12:58 +0200994 ath5k_setup_rate_idx(sc, sband);
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500995
Bruno Randolf63266a62008-07-30 17:12:58 +0200996 /* 5GHz band, A mode */
Luis R. Rodriguez400ec452008-02-03 21:51:49 -0500997 if (test_bit(AR5K_MODE_11A, sc->ah->ah_capabilities.cap_mode)) {
Bruno Randolf63266a62008-07-30 17:12:58 +0200998 sband = &sc->sbands[IEEE80211_BAND_5GHZ];
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500999 sband->band = IEEE80211_BAND_5GHZ;
Bruno Randolf63266a62008-07-30 17:12:58 +02001000 sband->bitrates = &sc->rates[IEEE80211_BAND_5GHZ][0];
1001
1002 memcpy(sband->bitrates, &ath5k_rates[4],
1003 sizeof(struct ieee80211_rate) * 8);
1004 sband->n_bitrates = 8;
1005
1006 sband->channels = &sc->channels[count_c];
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -05001007 sband->n_channels = ath5k_copy_channels(ah, sband->channels,
1008 AR5K_MODE_11A, max_c);
1009
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -05001010 hw->wiphy->bands[IEEE80211_BAND_5GHZ] = sband;
1011 }
Bruno Randolf63266a62008-07-30 17:12:58 +02001012 ath5k_setup_rate_idx(sc, sband);
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -05001013
Luis R. Rodriguezb4461972008-02-04 10:03:54 -05001014 ath5k_debug_dump_bands(sc);
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -05001015
1016 return 0;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001017}
1018
1019/*
1020 * Set/change channels. If the channel is really being changed,
1021 * it's done by reseting the chip. To accomplish this we must
1022 * first cleanup any pending DMA, then restart stuff after a la
1023 * ath5k_init.
1024 */
1025static int
1026ath5k_chan_set(struct ath5k_softc *sc, struct ieee80211_channel *chan)
1027{
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -05001028 ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "(%u MHz) -> (%u MHz)\n",
1029 sc->curchan->center_freq, chan->center_freq);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001030
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -05001031 if (chan->center_freq != sc->curchan->center_freq ||
1032 chan->hw_value != sc->curchan->hw_value) {
1033
1034 sc->curchan = chan;
1035 sc->curband = &sc->sbands[chan->band];
1036
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001037 /*
1038 * To switch channels clear any pending DMA operations;
1039 * wait long enough for the RX fifo to drain, reset the
1040 * hardware at the new frequency, and then re-enable
1041 * the relevant bits of the h/w.
1042 */
Jiri Slabyd7dc1002008-07-23 13:17:35 +02001043 return ath5k_reset(sc, true, true);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001044 }
1045
1046 return 0;
1047}
1048
1049static void
1050ath5k_setcurmode(struct ath5k_softc *sc, unsigned int mode)
1051{
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001052 sc->curmode = mode;
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -05001053
Luis R. Rodriguez400ec452008-02-03 21:51:49 -05001054 if (mode == AR5K_MODE_11A) {
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -05001055 sc->curband = &sc->sbands[IEEE80211_BAND_5GHZ];
1056 } else {
1057 sc->curband = &sc->sbands[IEEE80211_BAND_2GHZ];
1058 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001059}
1060
1061static void
1062ath5k_mode_setup(struct ath5k_softc *sc)
1063{
1064 struct ath5k_hw *ah = sc->ah;
1065 u32 rfilt;
1066
1067 /* configure rx filter */
1068 rfilt = sc->filter_flags;
1069 ath5k_hw_set_rx_filter(ah, rfilt);
1070
1071 if (ath5k_hw_hasbssidmask(ah))
1072 ath5k_hw_set_bssid_mask(ah, sc->bssidmask);
1073
1074 /* configure operational mode */
1075 ath5k_hw_set_opmode(ah);
1076
1077 ath5k_hw_set_mcast_filter(ah, 0, 0);
1078 ATH5K_DBG(sc, ATH5K_DEBUG_MODE, "RX filter 0x%x\n", rfilt);
1079}
1080
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -05001081static inline int
Bruno Randolf63266a62008-07-30 17:12:58 +02001082ath5k_hw_to_driver_rix(struct ath5k_softc *sc, int hw_rix)
1083{
1084 WARN_ON(hw_rix < 0 || hw_rix > AR5K_MAX_RATES);
1085 return sc->rate_idx[sc->curband->band][hw_rix];
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -05001086}
1087
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001088/***************\
1089* Buffers setup *
1090\***************/
1091
Bob Copelandb6ea0352009-01-10 14:42:54 -05001092static
1093struct sk_buff *ath5k_rx_skb_alloc(struct ath5k_softc *sc, dma_addr_t *skb_addr)
1094{
1095 struct sk_buff *skb;
1096 unsigned int off;
1097
1098 /*
1099 * Allocate buffer with headroom_needed space for the
1100 * fake physical layer header at the start.
1101 */
1102 skb = dev_alloc_skb(sc->rxbufsize + sc->cachelsz - 1);
1103
1104 if (!skb) {
1105 ATH5K_ERR(sc, "can't alloc skbuff of size %u\n",
1106 sc->rxbufsize + sc->cachelsz - 1);
1107 return NULL;
1108 }
1109 /*
1110 * Cache-line-align. This is important (for the
1111 * 5210 at least) as not doing so causes bogus data
1112 * in rx'd frames.
1113 */
1114 off = ((unsigned long)skb->data) % sc->cachelsz;
1115 if (off != 0)
1116 skb_reserve(skb, sc->cachelsz - off);
1117
1118 *skb_addr = pci_map_single(sc->pdev,
1119 skb->data, sc->rxbufsize, PCI_DMA_FROMDEVICE);
1120 if (unlikely(pci_dma_mapping_error(sc->pdev, *skb_addr))) {
1121 ATH5K_ERR(sc, "%s: DMA mapping failed\n", __func__);
1122 dev_kfree_skb(skb);
1123 return NULL;
1124 }
1125 return skb;
1126}
1127
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001128static int
1129ath5k_rxbuf_setup(struct ath5k_softc *sc, struct ath5k_buf *bf)
1130{
1131 struct ath5k_hw *ah = sc->ah;
1132 struct sk_buff *skb = bf->skb;
1133 struct ath5k_desc *ds;
1134
Bob Copelandb6ea0352009-01-10 14:42:54 -05001135 if (!skb) {
1136 skb = ath5k_rx_skb_alloc(sc, &bf->skbaddr);
1137 if (!skb)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001138 return -ENOMEM;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001139 bf->skb = skb;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001140 }
1141
1142 /*
1143 * Setup descriptors. For receive we always terminate
1144 * the descriptor list with a self-linked entry so we'll
1145 * not get overrun under high load (as can happen with a
1146 * 5212 when ANI processing enables PHY error frames).
1147 *
1148 * To insure the last descriptor is self-linked we create
1149 * each descriptor as self-linked and add it to the end. As
1150 * each additional descriptor is added the previous self-linked
1151 * entry is ``fixed'' naturally. This should be safe even
1152 * if DMA is happening. When processing RX interrupts we
1153 * never remove/process the last, self-linked, entry on the
1154 * descriptor list. This insures the hardware always has
1155 * someplace to write a new frame.
1156 */
1157 ds = bf->desc;
1158 ds->ds_link = bf->daddr; /* link to self */
1159 ds->ds_data = bf->skbaddr;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001160 ah->ah_setup_rx_desc(ah, ds,
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001161 skb_tailroom(skb), /* buffer size */
1162 0);
1163
1164 if (sc->rxlink != NULL)
1165 *sc->rxlink = bf->daddr;
1166 sc->rxlink = &ds->ds_link;
1167 return 0;
1168}
1169
1170static int
Johannes Berge039fa42008-05-15 12:55:29 +02001171ath5k_txbuf_setup(struct ath5k_softc *sc, struct ath5k_buf *bf)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001172{
1173 struct ath5k_hw *ah = sc->ah;
1174 struct ath5k_txq *txq = sc->txq;
1175 struct ath5k_desc *ds = bf->desc;
1176 struct sk_buff *skb = bf->skb;
Johannes Berga888d522008-05-26 16:43:39 +02001177 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001178 unsigned int pktlen, flags, keyidx = AR5K_TXKEYIX_INVALID;
Felix Fietkau2f7fe872008-10-05 18:05:48 +02001179 struct ieee80211_rate *rate;
1180 unsigned int mrr_rate[3], mrr_tries[3];
1181 int i, ret;
Bob Copeland8902ff42009-01-22 08:44:20 -05001182 u16 hw_rate;
1183 u8 rc_flags;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001184
1185 flags = AR5K_TXDESC_INTREQ | AR5K_TXDESC_CLRDMASK;
Johannes Berge039fa42008-05-15 12:55:29 +02001186
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001187 /* XXX endianness */
1188 bf->skbaddr = pci_map_single(sc->pdev, skb->data, skb->len,
1189 PCI_DMA_TODEVICE);
1190
Bob Copeland8902ff42009-01-22 08:44:20 -05001191 rate = ieee80211_get_tx_rate(sc->hw, info);
1192
Johannes Berge039fa42008-05-15 12:55:29 +02001193 if (info->flags & IEEE80211_TX_CTL_NO_ACK)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001194 flags |= AR5K_TXDESC_NOACK;
1195
Bob Copeland8902ff42009-01-22 08:44:20 -05001196 rc_flags = info->control.rates[0].flags;
1197 hw_rate = (rc_flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE) ?
1198 rate->hw_value_short : rate->hw_value;
1199
Bruno Randolf281c56d2008-02-05 18:44:55 +09001200 pktlen = skb->len;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001201
Johannes Bergd0f09802008-07-29 11:32:07 +02001202 if (info->control.hw_key) {
Johannes Berge039fa42008-05-15 12:55:29 +02001203 keyidx = info->control.hw_key->hw_key_idx;
Felix Fietkau76708de2008-10-05 18:02:48 +02001204 pktlen += info->control.hw_key->icv_len;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001205 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001206 ret = ah->ah_setup_tx_desc(ah, ds, pktlen,
1207 ieee80211_get_hdrlen_from_skb(skb), AR5K_PKT_TYPE_NORMAL,
Johannes Berg2e92e6f2008-05-15 12:55:27 +02001208 (sc->power_level * 2),
Bob Copeland8902ff42009-01-22 08:44:20 -05001209 hw_rate,
Johannes Berge6a98542008-10-21 12:40:02 +02001210 info->control.rates[0].count, keyidx, 0, flags, 0, 0);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001211 if (ret)
1212 goto err_unmap;
1213
Felix Fietkau2f7fe872008-10-05 18:05:48 +02001214 memset(mrr_rate, 0, sizeof(mrr_rate));
1215 memset(mrr_tries, 0, sizeof(mrr_tries));
1216 for (i = 0; i < 3; i++) {
1217 rate = ieee80211_get_alt_retry_rate(sc->hw, info, i);
1218 if (!rate)
1219 break;
1220
1221 mrr_rate[i] = rate->hw_value;
Johannes Berge6a98542008-10-21 12:40:02 +02001222 mrr_tries[i] = info->control.rates[i + 1].count;
Felix Fietkau2f7fe872008-10-05 18:05:48 +02001223 }
1224
1225 ah->ah_setup_mrr_tx_desc(ah, ds,
1226 mrr_rate[0], mrr_tries[0],
1227 mrr_rate[1], mrr_tries[1],
1228 mrr_rate[2], mrr_tries[2]);
1229
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001230 ds->ds_link = 0;
1231 ds->ds_data = bf->skbaddr;
1232
1233 spin_lock_bh(&txq->lock);
1234 list_add_tail(&bf->list, &txq->q);
Johannes Berg57ffc582008-04-29 17:18:59 +02001235 sc->tx_stats[txq->qnum].len++;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001236 if (txq->link == NULL) /* is this first packet? */
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001237 ath5k_hw_set_txdp(ah, txq->qnum, bf->daddr);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001238 else /* no, so only link it */
1239 *txq->link = bf->daddr;
1240
1241 txq->link = &ds->ds_link;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001242 ath5k_hw_start_tx_dma(ah, txq->qnum);
Jiri Slaby274c7c32008-07-15 17:44:20 +02001243 mmiowb();
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001244 spin_unlock_bh(&txq->lock);
1245
1246 return 0;
1247err_unmap:
1248 pci_unmap_single(sc->pdev, bf->skbaddr, skb->len, PCI_DMA_TODEVICE);
1249 return ret;
1250}
1251
1252/*******************\
1253* Descriptors setup *
1254\*******************/
1255
1256static int
1257ath5k_desc_alloc(struct ath5k_softc *sc, struct pci_dev *pdev)
1258{
1259 struct ath5k_desc *ds;
1260 struct ath5k_buf *bf;
1261 dma_addr_t da;
1262 unsigned int i;
1263 int ret;
1264
1265 /* allocate descriptors */
1266 sc->desc_len = sizeof(struct ath5k_desc) *
1267 (ATH_TXBUF + ATH_RXBUF + ATH_BCBUF + 1);
1268 sc->desc = pci_alloc_consistent(pdev, sc->desc_len, &sc->desc_daddr);
1269 if (sc->desc == NULL) {
1270 ATH5K_ERR(sc, "can't allocate descriptors\n");
1271 ret = -ENOMEM;
1272 goto err;
1273 }
1274 ds = sc->desc;
1275 da = sc->desc_daddr;
1276 ATH5K_DBG(sc, ATH5K_DEBUG_ANY, "DMA map: %p (%zu) -> %llx\n",
1277 ds, sc->desc_len, (unsigned long long)sc->desc_daddr);
1278
1279 bf = kcalloc(1 + ATH_TXBUF + ATH_RXBUF + ATH_BCBUF,
1280 sizeof(struct ath5k_buf), GFP_KERNEL);
1281 if (bf == NULL) {
1282 ATH5K_ERR(sc, "can't allocate bufptr\n");
1283 ret = -ENOMEM;
1284 goto err_free;
1285 }
1286 sc->bufptr = bf;
1287
1288 INIT_LIST_HEAD(&sc->rxbuf);
1289 for (i = 0; i < ATH_RXBUF; i++, bf++, ds++, da += sizeof(*ds)) {
1290 bf->desc = ds;
1291 bf->daddr = da;
1292 list_add_tail(&bf->list, &sc->rxbuf);
1293 }
1294
1295 INIT_LIST_HEAD(&sc->txbuf);
1296 sc->txbuf_len = ATH_TXBUF;
1297 for (i = 0; i < ATH_TXBUF; i++, bf++, ds++,
1298 da += sizeof(*ds)) {
1299 bf->desc = ds;
1300 bf->daddr = da;
1301 list_add_tail(&bf->list, &sc->txbuf);
1302 }
1303
1304 /* beacon buffer */
1305 bf->desc = ds;
1306 bf->daddr = da;
1307 sc->bbuf = bf;
1308
1309 return 0;
1310err_free:
1311 pci_free_consistent(pdev, sc->desc_len, sc->desc, sc->desc_daddr);
1312err:
1313 sc->desc = NULL;
1314 return ret;
1315}
1316
1317static void
1318ath5k_desc_free(struct ath5k_softc *sc, struct pci_dev *pdev)
1319{
1320 struct ath5k_buf *bf;
1321
1322 ath5k_txbuf_free(sc, sc->bbuf);
1323 list_for_each_entry(bf, &sc->txbuf, list)
1324 ath5k_txbuf_free(sc, bf);
1325 list_for_each_entry(bf, &sc->rxbuf, list)
1326 ath5k_txbuf_free(sc, bf);
1327
1328 /* Free memory associated with all descriptors */
1329 pci_free_consistent(pdev, sc->desc_len, sc->desc, sc->desc_daddr);
1330
1331 kfree(sc->bufptr);
1332 sc->bufptr = NULL;
1333}
1334
1335
1336
1337
1338
1339/**************\
1340* Queues setup *
1341\**************/
1342
1343static struct ath5k_txq *
1344ath5k_txq_setup(struct ath5k_softc *sc,
1345 int qtype, int subtype)
1346{
1347 struct ath5k_hw *ah = sc->ah;
1348 struct ath5k_txq *txq;
1349 struct ath5k_txq_info qi = {
1350 .tqi_subtype = subtype,
1351 .tqi_aifs = AR5K_TXQ_USEDEFAULT,
1352 .tqi_cw_min = AR5K_TXQ_USEDEFAULT,
1353 .tqi_cw_max = AR5K_TXQ_USEDEFAULT
1354 };
1355 int qnum;
1356
1357 /*
1358 * Enable interrupts only for EOL and DESC conditions.
1359 * We mark tx descriptors to receive a DESC interrupt
1360 * when a tx queue gets deep; otherwise waiting for the
1361 * EOL to reap descriptors. Note that this is done to
1362 * reduce interrupt load and this only defers reaping
1363 * descriptors, never transmitting frames. Aside from
1364 * reducing interrupts this also permits more concurrency.
1365 * The only potential downside is if the tx queue backs
1366 * up in which case the top half of the kernel may backup
1367 * due to a lack of tx descriptors.
1368 */
1369 qi.tqi_flags = AR5K_TXQ_FLAG_TXEOLINT_ENABLE |
1370 AR5K_TXQ_FLAG_TXDESCINT_ENABLE;
1371 qnum = ath5k_hw_setup_tx_queue(ah, qtype, &qi);
1372 if (qnum < 0) {
1373 /*
1374 * NB: don't print a message, this happens
1375 * normally on parts with too few tx queues
1376 */
1377 return ERR_PTR(qnum);
1378 }
1379 if (qnum >= ARRAY_SIZE(sc->txqs)) {
1380 ATH5K_ERR(sc, "hw qnum %u out of range, max %tu!\n",
1381 qnum, ARRAY_SIZE(sc->txqs));
1382 ath5k_hw_release_tx_queue(ah, qnum);
1383 return ERR_PTR(-EINVAL);
1384 }
1385 txq = &sc->txqs[qnum];
1386 if (!txq->setup) {
1387 txq->qnum = qnum;
1388 txq->link = NULL;
1389 INIT_LIST_HEAD(&txq->q);
1390 spin_lock_init(&txq->lock);
1391 txq->setup = true;
1392 }
1393 return &sc->txqs[qnum];
1394}
1395
1396static int
1397ath5k_beaconq_setup(struct ath5k_hw *ah)
1398{
1399 struct ath5k_txq_info qi = {
1400 .tqi_aifs = AR5K_TXQ_USEDEFAULT,
1401 .tqi_cw_min = AR5K_TXQ_USEDEFAULT,
1402 .tqi_cw_max = AR5K_TXQ_USEDEFAULT,
1403 /* NB: for dynamic turbo, don't enable any other interrupts */
1404 .tqi_flags = AR5K_TXQ_FLAG_TXDESCINT_ENABLE
1405 };
1406
1407 return ath5k_hw_setup_tx_queue(ah, AR5K_TX_QUEUE_BEACON, &qi);
1408}
1409
1410static int
1411ath5k_beaconq_config(struct ath5k_softc *sc)
1412{
1413 struct ath5k_hw *ah = sc->ah;
1414 struct ath5k_txq_info qi;
1415 int ret;
1416
1417 ret = ath5k_hw_get_tx_queueprops(ah, sc->bhalq, &qi);
1418 if (ret)
1419 return ret;
Johannes Berg05c914f2008-09-11 00:01:58 +02001420 if (sc->opmode == NL80211_IFTYPE_AP ||
1421 sc->opmode == NL80211_IFTYPE_MESH_POINT) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001422 /*
1423 * Always burst out beacon and CAB traffic
1424 * (aifs = cwmin = cwmax = 0)
1425 */
1426 qi.tqi_aifs = 0;
1427 qi.tqi_cw_min = 0;
1428 qi.tqi_cw_max = 0;
Johannes Berg05c914f2008-09-11 00:01:58 +02001429 } else if (sc->opmode == NL80211_IFTYPE_ADHOC) {
Bruno Randolf6d91e1d2008-01-19 18:18:41 +09001430 /*
1431 * Adhoc mode; backoff between 0 and (2 * cw_min).
1432 */
1433 qi.tqi_aifs = 0;
1434 qi.tqi_cw_min = 0;
1435 qi.tqi_cw_max = 2 * ah->ah_cw_min;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001436 }
1437
Bruno Randolf6d91e1d2008-01-19 18:18:41 +09001438 ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
1439 "beacon queueprops tqi_aifs:%d tqi_cw_min:%d tqi_cw_max:%d\n",
1440 qi.tqi_aifs, qi.tqi_cw_min, qi.tqi_cw_max);
1441
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001442 ret = ath5k_hw_set_tx_queueprops(ah, sc->bhalq, &qi);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001443 if (ret) {
1444 ATH5K_ERR(sc, "%s: unable to update parameters for beacon "
1445 "hardware queue!\n", __func__);
1446 return ret;
1447 }
1448
1449 return ath5k_hw_reset_tx_queue(ah, sc->bhalq); /* push to h/w */;
1450}
1451
1452static void
1453ath5k_txq_drainq(struct ath5k_softc *sc, struct ath5k_txq *txq)
1454{
1455 struct ath5k_buf *bf, *bf0;
1456
1457 /*
1458 * NB: this assumes output has been stopped and
1459 * we do not need to block ath5k_tx_tasklet
1460 */
1461 spin_lock_bh(&txq->lock);
1462 list_for_each_entry_safe(bf, bf0, &txq->q, list) {
Bruno Randolfb47f4072008-03-05 18:35:45 +09001463 ath5k_debug_printtxbuf(sc, bf);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001464
1465 ath5k_txbuf_free(sc, bf);
1466
1467 spin_lock_bh(&sc->txbuflock);
Johannes Berg57ffc582008-04-29 17:18:59 +02001468 sc->tx_stats[txq->qnum].len--;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001469 list_move_tail(&bf->list, &sc->txbuf);
1470 sc->txbuf_len++;
1471 spin_unlock_bh(&sc->txbuflock);
1472 }
1473 txq->link = NULL;
1474 spin_unlock_bh(&txq->lock);
1475}
1476
1477/*
1478 * Drain the transmit queues and reclaim resources.
1479 */
1480static void
1481ath5k_txq_cleanup(struct ath5k_softc *sc)
1482{
1483 struct ath5k_hw *ah = sc->ah;
1484 unsigned int i;
1485
1486 /* XXX return value */
1487 if (likely(!test_bit(ATH_STAT_INVALID, sc->status))) {
1488 /* don't touch the hardware if marked invalid */
1489 ath5k_hw_stop_tx_dma(ah, sc->bhalq);
1490 ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "beacon queue %x\n",
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001491 ath5k_hw_get_txdp(ah, sc->bhalq));
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001492 for (i = 0; i < ARRAY_SIZE(sc->txqs); i++)
1493 if (sc->txqs[i].setup) {
1494 ath5k_hw_stop_tx_dma(ah, sc->txqs[i].qnum);
1495 ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "txq [%u] %x, "
1496 "link %p\n",
1497 sc->txqs[i].qnum,
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001498 ath5k_hw_get_txdp(ah,
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001499 sc->txqs[i].qnum),
1500 sc->txqs[i].link);
1501 }
1502 }
Johannes Berg36d68252008-05-15 12:55:26 +02001503 ieee80211_wake_queues(sc->hw); /* XXX move to callers */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001504
1505 for (i = 0; i < ARRAY_SIZE(sc->txqs); i++)
1506 if (sc->txqs[i].setup)
1507 ath5k_txq_drainq(sc, &sc->txqs[i]);
1508}
1509
1510static void
1511ath5k_txq_release(struct ath5k_softc *sc)
1512{
1513 struct ath5k_txq *txq = sc->txqs;
1514 unsigned int i;
1515
1516 for (i = 0; i < ARRAY_SIZE(sc->txqs); i++, txq++)
1517 if (txq->setup) {
1518 ath5k_hw_release_tx_queue(sc->ah, txq->qnum);
1519 txq->setup = false;
1520 }
1521}
1522
1523
1524
1525
1526/*************\
1527* RX Handling *
1528\*************/
1529
1530/*
1531 * Enable the receive h/w following a reset.
1532 */
1533static int
1534ath5k_rx_start(struct ath5k_softc *sc)
1535{
1536 struct ath5k_hw *ah = sc->ah;
1537 struct ath5k_buf *bf;
1538 int ret;
1539
1540 sc->rxbufsize = roundup(IEEE80211_MAX_LEN, sc->cachelsz);
1541
1542 ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "cachelsz %u rxbufsize %u\n",
1543 sc->cachelsz, sc->rxbufsize);
1544
1545 sc->rxlink = NULL;
1546
1547 spin_lock_bh(&sc->rxbuflock);
1548 list_for_each_entry(bf, &sc->rxbuf, list) {
1549 ret = ath5k_rxbuf_setup(sc, bf);
1550 if (ret != 0) {
1551 spin_unlock_bh(&sc->rxbuflock);
1552 goto err;
1553 }
1554 }
1555 bf = list_first_entry(&sc->rxbuf, struct ath5k_buf, list);
1556 spin_unlock_bh(&sc->rxbuflock);
1557
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001558 ath5k_hw_set_rxdp(ah, bf->daddr);
1559 ath5k_hw_start_rx_dma(ah); /* enable recv descriptors */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001560 ath5k_mode_setup(sc); /* set filters, etc. */
1561 ath5k_hw_start_rx_pcu(ah); /* re-enable PCU/DMA engine */
1562
1563 return 0;
1564err:
1565 return ret;
1566}
1567
1568/*
1569 * Disable the receive h/w in preparation for a reset.
1570 */
1571static void
1572ath5k_rx_stop(struct ath5k_softc *sc)
1573{
1574 struct ath5k_hw *ah = sc->ah;
1575
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001576 ath5k_hw_stop_rx_pcu(ah); /* disable PCU */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001577 ath5k_hw_set_rx_filter(ah, 0); /* clear recv filter */
1578 ath5k_hw_stop_rx_dma(ah); /* disable DMA engine */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001579
1580 ath5k_debug_printrxbuffs(sc, ah);
1581
1582 sc->rxlink = NULL; /* just in case */
1583}
1584
1585static unsigned int
1586ath5k_rx_decrypted(struct ath5k_softc *sc, struct ath5k_desc *ds,
Bruno Randolfb47f4072008-03-05 18:35:45 +09001587 struct sk_buff *skb, struct ath5k_rx_status *rs)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001588{
1589 struct ieee80211_hdr *hdr = (void *)skb->data;
Harvey Harrison798ee982008-07-15 18:44:02 -07001590 unsigned int keyix, hlen;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001591
Bruno Randolfb47f4072008-03-05 18:35:45 +09001592 if (!(rs->rs_status & AR5K_RXERR_DECRYPT) &&
1593 rs->rs_keyix != AR5K_RXKEYIX_INVALID)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001594 return RX_FLAG_DECRYPTED;
1595
1596 /* Apparently when a default key is used to decrypt the packet
1597 the hw does not set the index used to decrypt. In such cases
1598 get the index from the packet. */
Harvey Harrison798ee982008-07-15 18:44:02 -07001599 hlen = ieee80211_hdrlen(hdr->frame_control);
Harvey Harrison24b56e72008-06-14 23:33:38 -07001600 if (ieee80211_has_protected(hdr->frame_control) &&
1601 !(rs->rs_status & AR5K_RXERR_DECRYPT) &&
1602 skb->len >= hlen + 4) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001603 keyix = skb->data[hlen + 3] >> 6;
1604
1605 if (test_bit(keyix, sc->keymap))
1606 return RX_FLAG_DECRYPTED;
1607 }
1608
1609 return 0;
1610}
1611
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001612
1613static void
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001614ath5k_check_ibss_tsf(struct ath5k_softc *sc, struct sk_buff *skb,
1615 struct ieee80211_rx_status *rxs)
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001616{
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001617 u64 tsf, bc_tstamp;
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001618 u32 hw_tu;
1619 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)skb->data;
1620
Harvey Harrison24b56e72008-06-14 23:33:38 -07001621 if (ieee80211_is_beacon(mgmt->frame_control) &&
Pavel Roskin38c07b42008-02-26 17:59:14 -05001622 le16_to_cpu(mgmt->u.beacon.capab_info) & WLAN_CAPABILITY_IBSS &&
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001623 memcmp(mgmt->bssid, sc->ah->ah_bssid, ETH_ALEN) == 0) {
1624 /*
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001625 * Received an IBSS beacon with the same BSSID. Hardware *must*
1626 * have updated the local TSF. We have to work around various
1627 * hardware bugs, though...
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001628 */
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001629 tsf = ath5k_hw_get_tsf64(sc->ah);
1630 bc_tstamp = le64_to_cpu(mgmt->u.beacon.timestamp);
1631 hw_tu = TSF_TO_TU(tsf);
1632
1633 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
1634 "beacon %llx mactime %llx (diff %lld) tsf now %llx\n",
John W. Linville06501d22008-04-01 17:38:47 -04001635 (unsigned long long)bc_tstamp,
1636 (unsigned long long)rxs->mactime,
1637 (unsigned long long)(rxs->mactime - bc_tstamp),
1638 (unsigned long long)tsf);
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001639
1640 /*
1641 * Sometimes the HW will give us a wrong tstamp in the rx
1642 * status, causing the timestamp extension to go wrong.
1643 * (This seems to happen especially with beacon frames bigger
1644 * than 78 byte (incl. FCS))
1645 * But we know that the receive timestamp must be later than the
1646 * timestamp of the beacon since HW must have synced to that.
1647 *
1648 * NOTE: here we assume mactime to be after the frame was
1649 * received, not like mac80211 which defines it at the start.
1650 */
1651 if (bc_tstamp > rxs->mactime) {
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001652 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001653 "fixing mactime from %llx to %llx\n",
John W. Linville06501d22008-04-01 17:38:47 -04001654 (unsigned long long)rxs->mactime,
1655 (unsigned long long)tsf);
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001656 rxs->mactime = tsf;
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001657 }
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001658
1659 /*
1660 * Local TSF might have moved higher than our beacon timers,
1661 * in that case we have to update them to continue sending
1662 * beacons. This also takes care of synchronizing beacon sending
1663 * times with other stations.
1664 */
1665 if (hw_tu >= sc->nexttbtt)
1666 ath5k_beacon_update_timers(sc, bc_tstamp);
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001667 }
1668}
1669
1670
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001671static void
1672ath5k_tasklet_rx(unsigned long data)
1673{
1674 struct ieee80211_rx_status rxs = {};
Bruno Randolfb47f4072008-03-05 18:35:45 +09001675 struct ath5k_rx_status rs = {};
Bob Copelandb6ea0352009-01-10 14:42:54 -05001676 struct sk_buff *skb, *next_skb;
1677 dma_addr_t next_skb_addr;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001678 struct ath5k_softc *sc = (void *)data;
Jiri Slaby3a0f2c82008-07-15 17:44:18 +02001679 struct ath5k_buf *bf, *bf_last;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001680 struct ath5k_desc *ds;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001681 int ret;
1682 int hdrlen;
Benoit PAPILLAULT0fe45b12008-12-12 15:29:58 +01001683 int padsize;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001684
1685 spin_lock(&sc->rxbuflock);
Jiri Slaby3a0f2c82008-07-15 17:44:18 +02001686 if (list_empty(&sc->rxbuf)) {
1687 ATH5K_WARN(sc, "empty rx buf pool\n");
1688 goto unlock;
1689 }
1690 bf_last = list_entry(sc->rxbuf.prev, struct ath5k_buf, list);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001691 do {
Bob Copelandd6894b52008-05-12 21:16:44 -04001692 rxs.flag = 0;
1693
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001694 bf = list_first_entry(&sc->rxbuf, struct ath5k_buf, list);
1695 BUG_ON(bf->skb == NULL);
1696 skb = bf->skb;
1697 ds = bf->desc;
1698
Jiri Slaby3a0f2c82008-07-15 17:44:18 +02001699 /*
1700 * last buffer must not be freed to ensure proper hardware
1701 * function. When the hardware finishes also a packet next to
1702 * it, we are sure, it doesn't use it anymore and we can go on.
1703 */
1704 if (bf_last == bf)
1705 bf->flags |= 1;
1706 if (bf->flags) {
1707 struct ath5k_buf *bf_next = list_entry(bf->list.next,
1708 struct ath5k_buf, list);
1709 ret = sc->ah->ah_proc_rx_desc(sc->ah, bf_next->desc,
1710 &rs);
1711 if (ret)
1712 break;
1713 bf->flags &= ~1;
1714 /* skip the overwritten one (even status is martian) */
1715 goto next;
1716 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001717
Bruno Randolfb47f4072008-03-05 18:35:45 +09001718 ret = sc->ah->ah_proc_rx_desc(sc->ah, ds, &rs);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001719 if (unlikely(ret == -EINPROGRESS))
1720 break;
1721 else if (unlikely(ret)) {
1722 ATH5K_ERR(sc, "error in processing rx descriptor\n");
Jiri Slaby65872e62008-02-15 21:58:51 +01001723 spin_unlock(&sc->rxbuflock);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001724 return;
1725 }
1726
Bruno Randolfb47f4072008-03-05 18:35:45 +09001727 if (unlikely(rs.rs_more)) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001728 ATH5K_WARN(sc, "unsupported jumbo\n");
1729 goto next;
1730 }
1731
Bruno Randolfb47f4072008-03-05 18:35:45 +09001732 if (unlikely(rs.rs_status)) {
1733 if (rs.rs_status & AR5K_RXERR_PHY)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001734 goto next;
Bruno Randolfb47f4072008-03-05 18:35:45 +09001735 if (rs.rs_status & AR5K_RXERR_DECRYPT) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001736 /*
1737 * Decrypt error. If the error occurred
1738 * because there was no hardware key, then
1739 * let the frame through so the upper layers
1740 * can process it. This is necessary for 5210
1741 * parts which have no way to setup a ``clear''
1742 * key cache entry.
1743 *
1744 * XXX do key cache faulting
1745 */
Bruno Randolfb47f4072008-03-05 18:35:45 +09001746 if (rs.rs_keyix == AR5K_RXKEYIX_INVALID &&
1747 !(rs.rs_status & AR5K_RXERR_CRC))
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001748 goto accept;
1749 }
Bruno Randolfb47f4072008-03-05 18:35:45 +09001750 if (rs.rs_status & AR5K_RXERR_MIC) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001751 rxs.flag |= RX_FLAG_MMIC_ERROR;
1752 goto accept;
1753 }
1754
1755 /* let crypto-error packets fall through in MNTR */
Bruno Randolfb47f4072008-03-05 18:35:45 +09001756 if ((rs.rs_status &
1757 ~(AR5K_RXERR_DECRYPT|AR5K_RXERR_MIC)) ||
Johannes Berg05c914f2008-09-11 00:01:58 +02001758 sc->opmode != NL80211_IFTYPE_MONITOR)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001759 goto next;
1760 }
1761accept:
Bob Copelandb6ea0352009-01-10 14:42:54 -05001762 next_skb = ath5k_rx_skb_alloc(sc, &next_skb_addr);
1763
1764 /*
1765 * If we can't replace bf->skb with a new skb under memory
1766 * pressure, just skip this packet
1767 */
1768 if (!next_skb)
1769 goto next;
1770
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001771 pci_unmap_single(sc->pdev, bf->skbaddr, sc->rxbufsize,
1772 PCI_DMA_FROMDEVICE);
Bruno Randolfb47f4072008-03-05 18:35:45 +09001773 skb_put(skb, rs.rs_datalen);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001774
Benoit PAPILLAULT0fe45b12008-12-12 15:29:58 +01001775 /* The MAC header is padded to have 32-bit boundary if the
1776 * packet payload is non-zero. The general calculation for
1777 * padsize would take into account odd header lengths:
1778 * padsize = (4 - hdrlen % 4) % 4; However, since only
1779 * even-length headers are used, padding can only be 0 or 2
1780 * bytes and we can optimize this a bit. In addition, we must
1781 * not try to remove padding from short control frames that do
1782 * not have payload. */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001783 hdrlen = ieee80211_get_hdrlen_from_skb(skb);
Bob Copelandfd6effc2008-12-18 23:23:05 -05001784 padsize = ath5k_pad_size(hdrlen);
1785 if (padsize) {
Benoit PAPILLAULT0fe45b12008-12-12 15:29:58 +01001786 memmove(skb->data + padsize, skb->data, hdrlen);
1787 skb_pull(skb, padsize);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001788 }
1789
Bruno Randolfc0e18992008-01-21 11:09:46 +09001790 /*
1791 * always extend the mac timestamp, since this information is
1792 * also needed for proper IBSS merging.
1793 *
1794 * XXX: it might be too late to do it here, since rs_tstamp is
1795 * 15bit only. that means TSF extension has to be done within
1796 * 32768usec (about 32ms). it might be necessary to move this to
1797 * the interrupt handler, like it is done in madwifi.
Bruno Randolfe14296c2008-03-05 18:36:05 +09001798 *
1799 * Unfortunately we don't know when the hardware takes the rx
1800 * timestamp (beginning of phy frame, data frame, end of rx?).
1801 * The only thing we know is that it is hardware specific...
1802 * On AR5213 it seems the rx timestamp is at the end of the
1803 * frame, but i'm not sure.
1804 *
1805 * NOTE: mac80211 defines mactime at the beginning of the first
1806 * data symbol. Since we don't have any time references it's
1807 * impossible to comply to that. This affects IBSS merge only
1808 * right now, so it's not too bad...
Bruno Randolfc0e18992008-01-21 11:09:46 +09001809 */
Bruno Randolfb47f4072008-03-05 18:35:45 +09001810 rxs.mactime = ath5k_extend_tsf(sc->ah, rs.rs_tstamp);
Bruno Randolfc0e18992008-01-21 11:09:46 +09001811 rxs.flag |= RX_FLAG_TSFT;
1812
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -05001813 rxs.freq = sc->curchan->center_freq;
1814 rxs.band = sc->curband->band;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001815
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001816 rxs.noise = sc->ah->ah_noise_floor;
Bruno Randolf566bfe52008-05-08 19:15:40 +02001817 rxs.signal = rxs.noise + rs.rs_rssi;
Luis R. Rodriguez6e0e0bf2008-10-13 14:08:10 -07001818
1819 /* An rssi of 35 indicates you should be able use
1820 * 54 Mbps reliably. A more elaborate scheme can be used
1821 * here but it requires a map of SNR/throughput for each
1822 * possible mode used */
1823 rxs.qual = rs.rs_rssi * 100 / 35;
1824
1825 /* rssi can be more than 35 though, anything above that
1826 * should be considered at 100% */
1827 if (rxs.qual > 100)
1828 rxs.qual = 100;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001829
Bruno Randolfb47f4072008-03-05 18:35:45 +09001830 rxs.antenna = rs.rs_antenna;
1831 rxs.rate_idx = ath5k_hw_to_driver_rix(sc, rs.rs_rate);
1832 rxs.flag |= ath5k_rx_decrypted(sc, ds, skb, &rs);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001833
Bruno Randolf06303352008-08-05 19:32:23 +02001834 if (rxs.rate_idx >= 0 && rs.rs_rate ==
1835 sc->curband->bitrates[rxs.rate_idx].hw_value_short)
Bruno Randolf63266a62008-07-30 17:12:58 +02001836 rxs.flag |= RX_FLAG_SHORTPRE;
Bruno Randolf06303352008-08-05 19:32:23 +02001837
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001838 ath5k_debug_dump_skb(sc, skb, "RX ", 0);
1839
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001840 /* check beacons in IBSS mode */
Johannes Berg05c914f2008-09-11 00:01:58 +02001841 if (sc->opmode == NL80211_IFTYPE_ADHOC)
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001842 ath5k_check_ibss_tsf(sc, skb, &rxs);
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001843
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001844 __ieee80211_rx(sc->hw, skb, &rxs);
Bob Copelandb6ea0352009-01-10 14:42:54 -05001845
1846 bf->skb = next_skb;
1847 bf->skbaddr = next_skb_addr;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001848next:
1849 list_move_tail(&bf->list, &sc->rxbuf);
1850 } while (ath5k_rxbuf_setup(sc, bf) == 0);
Jiri Slaby3a0f2c82008-07-15 17:44:18 +02001851unlock:
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001852 spin_unlock(&sc->rxbuflock);
1853}
1854
1855
1856
1857
1858/*************\
1859* TX Handling *
1860\*************/
1861
1862static void
1863ath5k_tx_processq(struct ath5k_softc *sc, struct ath5k_txq *txq)
1864{
Bruno Randolfb47f4072008-03-05 18:35:45 +09001865 struct ath5k_tx_status ts = {};
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001866 struct ath5k_buf *bf, *bf0;
1867 struct ath5k_desc *ds;
1868 struct sk_buff *skb;
Johannes Berge039fa42008-05-15 12:55:29 +02001869 struct ieee80211_tx_info *info;
Felix Fietkau2f7fe872008-10-05 18:05:48 +02001870 int i, ret;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001871
1872 spin_lock(&txq->lock);
1873 list_for_each_entry_safe(bf, bf0, &txq->q, list) {
1874 ds = bf->desc;
1875
Bruno Randolfb47f4072008-03-05 18:35:45 +09001876 ret = sc->ah->ah_proc_tx_desc(sc->ah, ds, &ts);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001877 if (unlikely(ret == -EINPROGRESS))
1878 break;
1879 else if (unlikely(ret)) {
1880 ATH5K_ERR(sc, "error %d while processing queue %u\n",
1881 ret, txq->qnum);
1882 break;
1883 }
1884
1885 skb = bf->skb;
Johannes Berga888d522008-05-26 16:43:39 +02001886 info = IEEE80211_SKB_CB(skb);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001887 bf->skb = NULL;
Johannes Berge039fa42008-05-15 12:55:29 +02001888
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001889 pci_unmap_single(sc->pdev, bf->skbaddr, skb->len,
1890 PCI_DMA_TODEVICE);
1891
Johannes Berge6a98542008-10-21 12:40:02 +02001892 ieee80211_tx_info_clear_status(info);
Felix Fietkau2f7fe872008-10-05 18:05:48 +02001893 for (i = 0; i < 4; i++) {
Johannes Berge6a98542008-10-21 12:40:02 +02001894 struct ieee80211_tx_rate *r =
1895 &info->status.rates[i];
Felix Fietkau2f7fe872008-10-05 18:05:48 +02001896
1897 if (ts.ts_rate[i]) {
Johannes Berge6a98542008-10-21 12:40:02 +02001898 r->idx = ath5k_hw_to_driver_rix(sc, ts.ts_rate[i]);
1899 r->count = ts.ts_retry[i];
Felix Fietkau2f7fe872008-10-05 18:05:48 +02001900 } else {
Johannes Berge6a98542008-10-21 12:40:02 +02001901 r->idx = -1;
1902 r->count = 0;
Felix Fietkau2f7fe872008-10-05 18:05:48 +02001903 }
1904 }
1905
Johannes Berge6a98542008-10-21 12:40:02 +02001906 /* count the successful attempt as well */
1907 info->status.rates[ts.ts_final_idx].count++;
1908
Bruno Randolfb47f4072008-03-05 18:35:45 +09001909 if (unlikely(ts.ts_status)) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001910 sc->ll_stats.dot11ACKFailureCount++;
Johannes Berge6a98542008-10-21 12:40:02 +02001911 if (ts.ts_status & AR5K_TXERR_FILT)
Johannes Berge039fa42008-05-15 12:55:29 +02001912 info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001913 } else {
Johannes Berge039fa42008-05-15 12:55:29 +02001914 info->flags |= IEEE80211_TX_STAT_ACK;
1915 info->status.ack_signal = ts.ts_rssi;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001916 }
1917
Johannes Berge039fa42008-05-15 12:55:29 +02001918 ieee80211_tx_status(sc->hw, skb);
Johannes Berg57ffc582008-04-29 17:18:59 +02001919 sc->tx_stats[txq->qnum].count++;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001920
1921 spin_lock(&sc->txbuflock);
Johannes Berg57ffc582008-04-29 17:18:59 +02001922 sc->tx_stats[txq->qnum].len--;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001923 list_move_tail(&bf->list, &sc->txbuf);
1924 sc->txbuf_len++;
1925 spin_unlock(&sc->txbuflock);
1926 }
1927 if (likely(list_empty(&txq->q)))
1928 txq->link = NULL;
1929 spin_unlock(&txq->lock);
1930 if (sc->txbuf_len > ATH_TXBUF / 5)
1931 ieee80211_wake_queues(sc->hw);
1932}
1933
1934static void
1935ath5k_tasklet_tx(unsigned long data)
1936{
1937 struct ath5k_softc *sc = (void *)data;
1938
1939 ath5k_tx_processq(sc, sc->txq);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001940}
1941
1942
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001943/*****************\
1944* Beacon handling *
1945\*****************/
1946
1947/*
1948 * Setup the beacon frame for transmit.
1949 */
1950static int
Johannes Berge039fa42008-05-15 12:55:29 +02001951ath5k_beacon_setup(struct ath5k_softc *sc, struct ath5k_buf *bf)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001952{
1953 struct sk_buff *skb = bf->skb;
Johannes Berga888d522008-05-26 16:43:39 +02001954 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001955 struct ath5k_hw *ah = sc->ah;
1956 struct ath5k_desc *ds;
1957 int ret, antenna = 0;
1958 u32 flags;
1959
1960 bf->skbaddr = pci_map_single(sc->pdev, skb->data, skb->len,
1961 PCI_DMA_TODEVICE);
1962 ATH5K_DBG(sc, ATH5K_DEBUG_BEACON, "skb %p [data %p len %u] "
1963 "skbaddr %llx\n", skb, skb->data, skb->len,
1964 (unsigned long long)bf->skbaddr);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -07001965 if (pci_dma_mapping_error(sc->pdev, bf->skbaddr)) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001966 ATH5K_ERR(sc, "beacon DMA mapping failed\n");
1967 return -EIO;
1968 }
1969
1970 ds = bf->desc;
1971
1972 flags = AR5K_TXDESC_NOACK;
Johannes Berg05c914f2008-09-11 00:01:58 +02001973 if (sc->opmode == NL80211_IFTYPE_ADHOC && ath5k_hw_hasveol(ah)) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001974 ds->ds_link = bf->daddr; /* self-linked */
1975 flags |= AR5K_TXDESC_VEOL;
1976 /*
1977 * Let hardware handle antenna switching if txantenna is not set
1978 */
1979 } else {
1980 ds->ds_link = 0;
1981 /*
1982 * Switch antenna every 4 beacons if txantenna is not set
1983 * XXX assumes two antennas
1984 */
1985 if (antenna == 0)
1986 antenna = sc->bsent & 4 ? 2 : 1;
1987 }
1988
1989 ds->ds_data = bf->skbaddr;
Bruno Randolf281c56d2008-02-05 18:44:55 +09001990 ret = ah->ah_setup_tx_desc(ah, ds, skb->len,
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001991 ieee80211_get_hdrlen_from_skb(skb),
Luis R. Rodriguez400ec452008-02-03 21:51:49 -05001992 AR5K_PKT_TYPE_BEACON, (sc->power_level * 2),
Johannes Berge039fa42008-05-15 12:55:29 +02001993 ieee80211_get_tx_rate(sc->hw, info)->hw_value,
Johannes Berg2e92e6f2008-05-15 12:55:27 +02001994 1, AR5K_TXKEYIX_INVALID,
Luis R. Rodriguez400ec452008-02-03 21:51:49 -05001995 antenna, flags, 0, 0);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001996 if (ret)
1997 goto err_unmap;
1998
1999 return 0;
2000err_unmap:
2001 pci_unmap_single(sc->pdev, bf->skbaddr, skb->len, PCI_DMA_TODEVICE);
2002 return ret;
2003}
2004
2005/*
2006 * Transmit a beacon frame at SWBA. Dynamic updates to the
2007 * frame contents are done as needed and the slot time is
2008 * also adjusted based on current state.
2009 *
2010 * this is usually called from interrupt context (ath5k_intr())
2011 * but also from ath5k_beacon_config() in IBSS mode which in turn
2012 * can be called from a tasklet and user context
2013 */
2014static void
2015ath5k_beacon_send(struct ath5k_softc *sc)
2016{
2017 struct ath5k_buf *bf = sc->bbuf;
2018 struct ath5k_hw *ah = sc->ah;
2019
Bruno Randolfbe9b7252008-01-23 10:27:51 +09002020 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON, "in beacon_send\n");
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002021
Johannes Berg05c914f2008-09-11 00:01:58 +02002022 if (unlikely(bf->skb == NULL || sc->opmode == NL80211_IFTYPE_STATION ||
2023 sc->opmode == NL80211_IFTYPE_MONITOR)) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002024 ATH5K_WARN(sc, "bf=%p bf_skb=%p\n", bf, bf ? bf->skb : NULL);
2025 return;
2026 }
2027 /*
2028 * Check if the previous beacon has gone out. If
2029 * not don't don't try to post another, skip this
2030 * period and wait for the next. Missed beacons
2031 * indicate a problem and should not occur. If we
2032 * miss too many consecutive beacons reset the device.
2033 */
2034 if (unlikely(ath5k_hw_num_tx_pending(ah, sc->bhalq) != 0)) {
2035 sc->bmisscount++;
Bruno Randolfbe9b7252008-01-23 10:27:51 +09002036 ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002037 "missed %u consecutive beacons\n", sc->bmisscount);
2038 if (sc->bmisscount > 3) { /* NB: 3 is a guess */
Bruno Randolfbe9b7252008-01-23 10:27:51 +09002039 ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002040 "stuck beacon time (%u missed)\n",
2041 sc->bmisscount);
2042 tasklet_schedule(&sc->restq);
2043 }
2044 return;
2045 }
2046 if (unlikely(sc->bmisscount != 0)) {
Bruno Randolfbe9b7252008-01-23 10:27:51 +09002047 ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002048 "resume beacon xmit after %u misses\n",
2049 sc->bmisscount);
2050 sc->bmisscount = 0;
2051 }
2052
2053 /*
2054 * Stop any current dma and put the new frame on the queue.
2055 * This should never fail since we check above that no frames
2056 * are still pending on the queue.
2057 */
2058 if (unlikely(ath5k_hw_stop_tx_dma(ah, sc->bhalq))) {
2059 ATH5K_WARN(sc, "beacon queue %u didn't stop?\n", sc->bhalq);
2060 /* NB: hw still stops DMA, so proceed */
2061 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002062
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03002063 ath5k_hw_set_txdp(ah, sc->bhalq, bf->daddr);
2064 ath5k_hw_start_tx_dma(ah, sc->bhalq);
Bruno Randolfbe9b7252008-01-23 10:27:51 +09002065 ATH5K_DBG(sc, ATH5K_DEBUG_BEACON, "TXDP[%u] = %llx (%p)\n",
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002066 sc->bhalq, (unsigned long long)bf->daddr, bf->desc);
2067
2068 sc->bsent++;
2069}
2070
2071
Bruno Randolf9804b982008-01-19 18:17:59 +09002072/**
2073 * ath5k_beacon_update_timers - update beacon timers
2074 *
2075 * @sc: struct ath5k_softc pointer we are operating on
2076 * @bc_tsf: the timestamp of the beacon. 0 to reset the TSF. -1 to perform a
2077 * beacon timer update based on the current HW TSF.
2078 *
2079 * Calculate the next target beacon transmit time (TBTT) based on the timestamp
2080 * of a received beacon or the current local hardware TSF and write it to the
2081 * beacon timer registers.
2082 *
2083 * This is called in a variety of situations, e.g. when a beacon is received,
Bruno Randolf6ba81c22008-03-05 18:36:26 +09002084 * when a TSF update has been detected, but also when an new IBSS is created or
Bruno Randolf9804b982008-01-19 18:17:59 +09002085 * when we otherwise know we have to update the timers, but we keep it in this
2086 * function to have it all together in one place.
2087 */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002088static void
Bruno Randolf9804b982008-01-19 18:17:59 +09002089ath5k_beacon_update_timers(struct ath5k_softc *sc, u64 bc_tsf)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002090{
2091 struct ath5k_hw *ah = sc->ah;
Bruno Randolf9804b982008-01-19 18:17:59 +09002092 u32 nexttbtt, intval, hw_tu, bc_tu;
2093 u64 hw_tsf;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002094
2095 intval = sc->bintval & AR5K_BEACON_PERIOD;
2096 if (WARN_ON(!intval))
2097 return;
2098
Bruno Randolf9804b982008-01-19 18:17:59 +09002099 /* beacon TSF converted to TU */
2100 bc_tu = TSF_TO_TU(bc_tsf);
2101
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002102 /* current TSF converted to TU */
Bruno Randolf9804b982008-01-19 18:17:59 +09002103 hw_tsf = ath5k_hw_get_tsf64(ah);
2104 hw_tu = TSF_TO_TU(hw_tsf);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002105
Bruno Randolf9804b982008-01-19 18:17:59 +09002106#define FUDGE 3
2107 /* we use FUDGE to make sure the next TBTT is ahead of the current TU */
2108 if (bc_tsf == -1) {
2109 /*
2110 * no beacons received, called internally.
2111 * just need to refresh timers based on HW TSF.
2112 */
2113 nexttbtt = roundup(hw_tu + FUDGE, intval);
2114 } else if (bc_tsf == 0) {
2115 /*
2116 * no beacon received, probably called by ath5k_reset_tsf().
2117 * reset TSF to start with 0.
2118 */
2119 nexttbtt = intval;
2120 intval |= AR5K_BEACON_RESET_TSF;
2121 } else if (bc_tsf > hw_tsf) {
2122 /*
2123 * beacon received, SW merge happend but HW TSF not yet updated.
2124 * not possible to reconfigure timers yet, but next time we
2125 * receive a beacon with the same BSSID, the hardware will
2126 * automatically update the TSF and then we need to reconfigure
2127 * the timers.
2128 */
2129 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
2130 "need to wait for HW TSF sync\n");
2131 return;
2132 } else {
2133 /*
2134 * most important case for beacon synchronization between STA.
2135 *
2136 * beacon received and HW TSF has been already updated by HW.
2137 * update next TBTT based on the TSF of the beacon, but make
2138 * sure it is ahead of our local TSF timer.
2139 */
2140 nexttbtt = bc_tu + roundup(hw_tu + FUDGE - bc_tu, intval);
2141 }
2142#undef FUDGE
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002143
Bruno Randolf036cd1e2008-01-19 18:18:21 +09002144 sc->nexttbtt = nexttbtt;
2145
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002146 intval |= AR5K_BEACON_ENA;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002147 ath5k_hw_init_beacon(ah, nexttbtt, intval);
Bruno Randolf9804b982008-01-19 18:17:59 +09002148
2149 /*
2150 * debugging output last in order to preserve the time critical aspect
2151 * of this function
2152 */
2153 if (bc_tsf == -1)
2154 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
2155 "reconfigured timers based on HW TSF\n");
2156 else if (bc_tsf == 0)
2157 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
2158 "reset HW TSF and timers\n");
2159 else
2160 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
2161 "updated timers based on beacon TSF\n");
2162
2163 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
David Miller04f93a82008-02-15 16:08:59 -08002164 "bc_tsf %llx hw_tsf %llx bc_tu %u hw_tu %u nexttbtt %u\n",
2165 (unsigned long long) bc_tsf,
2166 (unsigned long long) hw_tsf, bc_tu, hw_tu, nexttbtt);
Bruno Randolf9804b982008-01-19 18:17:59 +09002167 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON, "intval %u %s %s\n",
2168 intval & AR5K_BEACON_PERIOD,
2169 intval & AR5K_BEACON_ENA ? "AR5K_BEACON_ENA" : "",
2170 intval & AR5K_BEACON_RESET_TSF ? "AR5K_BEACON_RESET_TSF" : "");
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002171}
2172
2173
Bruno Randolf036cd1e2008-01-19 18:18:21 +09002174/**
2175 * ath5k_beacon_config - Configure the beacon queues and interrupts
2176 *
2177 * @sc: struct ath5k_softc pointer we are operating on
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002178 *
2179 * When operating in station mode we want to receive a BMISS interrupt when we
2180 * stop seeing beacons from the AP we've associated with so we can look for
2181 * another AP to associate with.
2182 *
Bruno Randolf036cd1e2008-01-19 18:18:21 +09002183 * In IBSS mode we use a self-linked tx descriptor if possible. We enable SWBA
Bruno Randolf6ba81c22008-03-05 18:36:26 +09002184 * interrupts to detect TSF updates only.
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002185 */
2186static void
2187ath5k_beacon_config(struct ath5k_softc *sc)
2188{
2189 struct ath5k_hw *ah = sc->ah;
2190
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03002191 ath5k_hw_set_imr(ah, 0);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002192 sc->bmisscount = 0;
Jiri Slabydc1968e2008-07-23 13:17:34 +02002193 sc->imask &= ~(AR5K_INT_BMISS | AR5K_INT_SWBA);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002194
Johannes Berg05c914f2008-09-11 00:01:58 +02002195 if (sc->opmode == NL80211_IFTYPE_STATION) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002196 sc->imask |= AR5K_INT_BMISS;
Jiri Slabyda966bc2008-10-12 22:54:10 +02002197 } else if (sc->opmode == NL80211_IFTYPE_ADHOC ||
Andrey Yurovskyb706e652008-10-13 18:23:07 -07002198 sc->opmode == NL80211_IFTYPE_MESH_POINT ||
Jiri Slabyda966bc2008-10-12 22:54:10 +02002199 sc->opmode == NL80211_IFTYPE_AP) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002200 /*
Bruno Randolf036cd1e2008-01-19 18:18:21 +09002201 * In IBSS mode we use a self-linked tx descriptor and let the
2202 * hardware send the beacons automatically. We have to load it
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002203 * only once here.
Bruno Randolf036cd1e2008-01-19 18:18:21 +09002204 * We use the SWBA interrupt only to keep track of the beacon
Bruno Randolf6ba81c22008-03-05 18:36:26 +09002205 * timers in order to detect automatic TSF updates.
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002206 */
2207 ath5k_beaconq_config(sc);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002208
Bruno Randolf036cd1e2008-01-19 18:18:21 +09002209 sc->imask |= AR5K_INT_SWBA;
2210
Jiri Slabyda966bc2008-10-12 22:54:10 +02002211 if (sc->opmode == NL80211_IFTYPE_ADHOC) {
2212 if (ath5k_hw_hasveol(ah)) {
2213 spin_lock(&sc->block);
2214 ath5k_beacon_send(sc);
2215 spin_unlock(&sc->block);
2216 }
2217 } else
2218 ath5k_beacon_update_timers(sc, -1);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002219 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002220
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03002221 ath5k_hw_set_imr(ah, sc->imask);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002222}
2223
2224
2225/********************\
2226* Interrupt handling *
2227\********************/
2228
2229static int
Bob Copelandbb2beca2009-01-19 11:20:54 -05002230ath5k_init(struct ath5k_softc *sc)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002231{
Elias Oltmannsbc1b32d2008-10-24 21:59:18 +02002232 struct ath5k_hw *ah = sc->ah;
2233 int ret, i;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002234
2235 mutex_lock(&sc->lock);
2236
2237 ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "mode %d\n", sc->opmode);
2238
2239 /*
2240 * Stop anything previously setup. This is safe
2241 * no matter this is the first time through or not.
2242 */
2243 ath5k_stop_locked(sc);
2244
2245 /*
2246 * The basic interface to setting the hardware in a good
2247 * state is ``reset''. On return the hardware is known to
2248 * be powered up and with interrupts disabled. This must
2249 * be followed by initialization of the appropriate bits
2250 * and then setup of the interrupt mask.
2251 */
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -05002252 sc->curchan = sc->hw->conf.channel;
2253 sc->curband = &sc->sbands[sc->curchan->band];
Nick Kossifidis6a53a8a2008-11-04 00:25:54 +02002254 sc->imask = AR5K_INT_RXOK | AR5K_INT_RXERR | AR5K_INT_RXEOL |
2255 AR5K_INT_RXORN | AR5K_INT_TXDESC | AR5K_INT_TXEOL |
2256 AR5K_INT_FATAL | AR5K_INT_GLOBAL | AR5K_INT_MIB;
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002257 ret = ath5k_reset(sc, false, false);
2258 if (ret)
2259 goto done;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002260
Elias Oltmannsbc1b32d2008-10-24 21:59:18 +02002261 /*
2262 * Reset the key cache since some parts do not reset the
2263 * contents on initial power up or resume from suspend.
2264 */
2265 for (i = 0; i < AR5K_KEYTABLE_SIZE; i++)
2266 ath5k_hw_reset_key(ah, i);
2267
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002268 /* Set ack to be sent at low bit-rates */
Elias Oltmannsbc1b32d2008-10-24 21:59:18 +02002269 ath5k_hw_set_ack_bitrate_high(ah, false);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002270
2271 mod_timer(&sc->calib_tim, round_jiffies(jiffies +
2272 msecs_to_jiffies(ath5k_calinterval * 1000)));
2273
2274 ret = 0;
2275done:
Jiri Slaby274c7c32008-07-15 17:44:20 +02002276 mmiowb();
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002277 mutex_unlock(&sc->lock);
2278 return ret;
2279}
2280
2281static int
2282ath5k_stop_locked(struct ath5k_softc *sc)
2283{
2284 struct ath5k_hw *ah = sc->ah;
2285
2286 ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "invalid %u\n",
2287 test_bit(ATH_STAT_INVALID, sc->status));
2288
2289 /*
2290 * Shutdown the hardware and driver:
2291 * stop output from above
2292 * disable interrupts
2293 * turn off timers
2294 * turn off the radio
2295 * clear transmit machinery
2296 * clear receive machinery
2297 * drain and release tx queues
2298 * reclaim beacon resources
2299 * power down hardware
2300 *
2301 * Note that some of this work is not possible if the
2302 * hardware is gone (invalid).
2303 */
2304 ieee80211_stop_queues(sc->hw);
2305
2306 if (!test_bit(ATH_STAT_INVALID, sc->status)) {
Bob Copeland3a078872008-06-25 22:35:28 -04002307 ath5k_led_off(sc);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03002308 ath5k_hw_set_imr(ah, 0);
Jiri Slaby274c7c32008-07-15 17:44:20 +02002309 synchronize_irq(sc->pdev->irq);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002310 }
2311 ath5k_txq_cleanup(sc);
2312 if (!test_bit(ATH_STAT_INVALID, sc->status)) {
2313 ath5k_rx_stop(sc);
2314 ath5k_hw_phy_disable(ah);
2315 } else
2316 sc->rxlink = NULL;
2317
2318 return 0;
2319}
2320
2321/*
2322 * Stop the device, grabbing the top-level lock to protect
2323 * against concurrent entry through ath5k_init (which can happen
2324 * if another thread does a system call and the thread doing the
2325 * stop is preempted).
2326 */
2327static int
Bob Copelandbb2beca2009-01-19 11:20:54 -05002328ath5k_stop_hw(struct ath5k_softc *sc)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002329{
2330 int ret;
2331
2332 mutex_lock(&sc->lock);
2333 ret = ath5k_stop_locked(sc);
2334 if (ret == 0 && !test_bit(ATH_STAT_INVALID, sc->status)) {
2335 /*
2336 * Set the chip in full sleep mode. Note that we are
2337 * careful to do this only when bringing the interface
2338 * completely to a stop. When the chip is in this state
2339 * it must be carefully woken up or references to
2340 * registers in the PCI clock domain may freeze the bus
2341 * (and system). This varies by chip and is mostly an
2342 * issue with newer parts that go to sleep more quickly.
2343 */
2344 if (sc->ah->ah_mac_srev >= 0x78) {
2345 /*
2346 * XXX
2347 * don't put newer MAC revisions > 7.8 to sleep because
2348 * of the above mentioned problems
2349 */
2350 ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "mac version > 7.8, "
2351 "not putting device to sleep\n");
2352 } else {
2353 ATH5K_DBG(sc, ATH5K_DEBUG_RESET,
2354 "putting device to full sleep\n");
2355 ath5k_hw_set_power(sc->ah, AR5K_PM_FULL_SLEEP, true, 0);
2356 }
2357 }
2358 ath5k_txbuf_free(sc, sc->bbuf);
Bob Copeland8bdd5b92008-10-16 11:02:06 -04002359
Jiri Slaby274c7c32008-07-15 17:44:20 +02002360 mmiowb();
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002361 mutex_unlock(&sc->lock);
2362
2363 del_timer_sync(&sc->calib_tim);
Jiri Slaby10488f82008-07-15 17:44:19 +02002364 tasklet_kill(&sc->rxtq);
2365 tasklet_kill(&sc->txtq);
2366 tasklet_kill(&sc->restq);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002367
2368 return ret;
2369}
2370
2371static irqreturn_t
2372ath5k_intr(int irq, void *dev_id)
2373{
2374 struct ath5k_softc *sc = dev_id;
2375 struct ath5k_hw *ah = sc->ah;
2376 enum ath5k_int status;
2377 unsigned int counter = 1000;
2378
2379 if (unlikely(test_bit(ATH_STAT_INVALID, sc->status) ||
2380 !ath5k_hw_is_intr_pending(ah)))
2381 return IRQ_NONE;
2382
2383 do {
2384 /*
2385 * Figure out the reason(s) for the interrupt. Note
2386 * that get_isr returns a pseudo-ISR that may include
2387 * bits we haven't explicitly enabled so we mask the
2388 * value to insure we only process bits we requested.
2389 */
2390 ath5k_hw_get_isr(ah, &status); /* NB: clears IRQ too */
2391 ATH5K_DBG(sc, ATH5K_DEBUG_INTR, "status 0x%x/0x%x\n",
2392 status, sc->imask);
2393 status &= sc->imask; /* discard unasked for bits */
2394 if (unlikely(status & AR5K_INT_FATAL)) {
2395 /*
2396 * Fatal errors are unrecoverable.
2397 * Typically these are caused by DMA errors.
2398 */
2399 tasklet_schedule(&sc->restq);
2400 } else if (unlikely(status & AR5K_INT_RXORN)) {
2401 tasklet_schedule(&sc->restq);
2402 } else {
2403 if (status & AR5K_INT_SWBA) {
2404 /*
2405 * Software beacon alert--time to send a beacon.
2406 * Handle beacon transmission directly; deferring
2407 * this is too slow to meet timing constraints
2408 * under load.
Bruno Randolf036cd1e2008-01-19 18:18:21 +09002409 *
2410 * In IBSS mode we use this interrupt just to
2411 * keep track of the next TBTT (target beacon
Bruno Randolf6ba81c22008-03-05 18:36:26 +09002412 * transmission time) in order to detect wether
2413 * automatic TSF updates happened.
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002414 */
Johannes Berg05c914f2008-09-11 00:01:58 +02002415 if (sc->opmode == NL80211_IFTYPE_ADHOC) {
Bruno Randolf036cd1e2008-01-19 18:18:21 +09002416 /* XXX: only if VEOL suppported */
2417 u64 tsf = ath5k_hw_get_tsf64(ah);
2418 sc->nexttbtt += sc->bintval;
2419 ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
David Miller04f93a82008-02-15 16:08:59 -08002420 "SWBA nexttbtt: %x hw_tu: %x "
2421 "TSF: %llx\n",
2422 sc->nexttbtt,
2423 TSF_TO_TU(tsf),
2424 (unsigned long long) tsf);
Bruno Randolf036cd1e2008-01-19 18:18:21 +09002425 } else {
Jiri Slaby00482972008-08-18 21:45:27 +02002426 spin_lock(&sc->block);
Bruno Randolf036cd1e2008-01-19 18:18:21 +09002427 ath5k_beacon_send(sc);
Jiri Slaby00482972008-08-18 21:45:27 +02002428 spin_unlock(&sc->block);
Bruno Randolf036cd1e2008-01-19 18:18:21 +09002429 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002430 }
2431 if (status & AR5K_INT_RXEOL) {
2432 /*
2433 * NB: the hardware should re-read the link when
2434 * RXE bit is written, but it doesn't work at
2435 * least on older hardware revs.
2436 */
2437 sc->rxlink = NULL;
2438 }
2439 if (status & AR5K_INT_TXURN) {
2440 /* bump tx trigger level */
2441 ath5k_hw_update_tx_triglevel(ah, true);
2442 }
Nick Kossifidis4c674c62008-10-26 20:40:25 +02002443 if (status & (AR5K_INT_RXOK | AR5K_INT_RXERR))
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002444 tasklet_schedule(&sc->rxtq);
Nick Kossifidis4c674c62008-10-26 20:40:25 +02002445 if (status & (AR5K_INT_TXOK | AR5K_INT_TXDESC
2446 | AR5K_INT_TXERR | AR5K_INT_TXEOL))
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002447 tasklet_schedule(&sc->txtq);
2448 if (status & AR5K_INT_BMISS) {
2449 }
2450 if (status & AR5K_INT_MIB) {
Nick Kossifidis194828a2008-04-16 18:49:02 +03002451 /*
2452 * These stats are also used for ANI i think
2453 * so how about updating them more often ?
2454 */
2455 ath5k_hw_update_mib_counters(ah, &sc->ll_stats);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002456 }
2457 }
2458 } while (ath5k_hw_is_intr_pending(ah) && counter-- > 0);
2459
2460 if (unlikely(!counter))
2461 ATH5K_WARN(sc, "too many interrupts, giving up for now\n");
2462
2463 return IRQ_HANDLED;
2464}
2465
2466static void
2467ath5k_tasklet_reset(unsigned long data)
2468{
2469 struct ath5k_softc *sc = (void *)data;
2470
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002471 ath5k_reset_wake(sc);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002472}
2473
2474/*
2475 * Periodically recalibrate the PHY to account
2476 * for temperature/environment changes.
2477 */
2478static void
2479ath5k_calibrate(unsigned long data)
2480{
2481 struct ath5k_softc *sc = (void *)data;
2482 struct ath5k_hw *ah = sc->ah;
2483
2484 ATH5K_DBG(sc, ATH5K_DEBUG_CALIBRATE, "channel %u/%x\n",
Luis R. Rodriguez400ec452008-02-03 21:51:49 -05002485 ieee80211_frequency_to_channel(sc->curchan->center_freq),
2486 sc->curchan->hw_value);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002487
2488 if (ath5k_hw_get_rf_gain(ah) == AR5K_RFGAIN_NEED_CHANGE) {
2489 /*
2490 * Rfgain is out of bounds, reset the chip
2491 * to load new gain values.
2492 */
2493 ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "calibration, resetting\n");
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002494 ath5k_reset_wake(sc);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002495 }
2496 if (ath5k_hw_phy_calibrate(ah, sc->curchan))
2497 ATH5K_ERR(sc, "calibration of channel %u failed\n",
Luis R. Rodriguez400ec452008-02-03 21:51:49 -05002498 ieee80211_frequency_to_channel(
2499 sc->curchan->center_freq));
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002500
2501 mod_timer(&sc->calib_tim, round_jiffies(jiffies +
2502 msecs_to_jiffies(ath5k_calinterval * 1000)));
2503}
2504
2505
2506
2507/***************\
2508* LED functions *
2509\***************/
2510
2511static void
Bob Copeland3a078872008-06-25 22:35:28 -04002512ath5k_led_enable(struct ath5k_softc *sc)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002513{
Bob Copeland3a078872008-06-25 22:35:28 -04002514 if (test_bit(ATH_STAT_LEDSOFT, sc->status)) {
2515 ath5k_hw_set_gpio_output(sc->ah, sc->led_pin);
2516 ath5k_led_off(sc);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002517 }
2518}
2519
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002520static void
Bob Copeland3a078872008-06-25 22:35:28 -04002521ath5k_led_on(struct ath5k_softc *sc)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002522{
Bob Copeland3a078872008-06-25 22:35:28 -04002523 if (!test_bit(ATH_STAT_LEDSOFT, sc->status))
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002524 return;
Bob Copeland3a078872008-06-25 22:35:28 -04002525 ath5k_hw_set_gpio(sc->ah, sc->led_pin, sc->led_on);
2526}
2527
2528static void
2529ath5k_led_off(struct ath5k_softc *sc)
2530{
2531 if (!test_bit(ATH_STAT_LEDSOFT, sc->status))
2532 return;
2533 ath5k_hw_set_gpio(sc->ah, sc->led_pin, !sc->led_on);
2534}
2535
2536static void
2537ath5k_led_brightness_set(struct led_classdev *led_dev,
2538 enum led_brightness brightness)
2539{
2540 struct ath5k_led *led = container_of(led_dev, struct ath5k_led,
2541 led_dev);
2542
2543 if (brightness == LED_OFF)
2544 ath5k_led_off(led->sc);
2545 else
2546 ath5k_led_on(led->sc);
2547}
2548
2549static int
2550ath5k_register_led(struct ath5k_softc *sc, struct ath5k_led *led,
2551 const char *name, char *trigger)
2552{
2553 int err;
2554
2555 led->sc = sc;
2556 strncpy(led->name, name, sizeof(led->name));
2557 led->led_dev.name = led->name;
2558 led->led_dev.default_trigger = trigger;
2559 led->led_dev.brightness_set = ath5k_led_brightness_set;
2560
2561 err = led_classdev_register(&sc->pdev->dev, &led->led_dev);
John Daiker0bbac082008-10-17 12:16:00 -07002562 if (err) {
Bob Copeland3a078872008-06-25 22:35:28 -04002563 ATH5K_WARN(sc, "could not register LED %s\n", name);
2564 led->sc = NULL;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002565 }
Bob Copeland3a078872008-06-25 22:35:28 -04002566 return err;
2567}
2568
2569static void
2570ath5k_unregister_led(struct ath5k_led *led)
2571{
2572 if (!led->sc)
2573 return;
2574 led_classdev_unregister(&led->led_dev);
2575 ath5k_led_off(led->sc);
2576 led->sc = NULL;
2577}
2578
2579static void
2580ath5k_unregister_leds(struct ath5k_softc *sc)
2581{
2582 ath5k_unregister_led(&sc->rx_led);
2583 ath5k_unregister_led(&sc->tx_led);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002584}
2585
2586
Bob Copeland3a078872008-06-25 22:35:28 -04002587static int
2588ath5k_init_leds(struct ath5k_softc *sc)
2589{
2590 int ret = 0;
2591 struct ieee80211_hw *hw = sc->hw;
2592 struct pci_dev *pdev = sc->pdev;
2593 char name[ATH5K_LED_MAX_NAME_LEN + 1];
2594
Bob Copeland3a078872008-06-25 22:35:28 -04002595 /*
2596 * Auto-enable soft led processing for IBM cards and for
2597 * 5211 minipci cards.
2598 */
2599 if (pdev->device == PCI_DEVICE_ID_ATHEROS_AR5212_IBM ||
2600 pdev->device == PCI_DEVICE_ID_ATHEROS_AR5211) {
2601 __set_bit(ATH_STAT_LEDSOFT, sc->status);
2602 sc->led_pin = 0;
Bob Copeland734b5aa2008-07-15 13:07:16 -04002603 sc->led_on = 0; /* active low */
Bob Copeland3a078872008-06-25 22:35:28 -04002604 }
2605 /* Enable softled on PIN1 on HP Compaq nc6xx, nc4000 & nx5000 laptops */
2606 if (pdev->subsystem_vendor == PCI_VENDOR_ID_COMPAQ) {
2607 __set_bit(ATH_STAT_LEDSOFT, sc->status);
2608 sc->led_pin = 1;
Bob Copeland734b5aa2008-07-15 13:07:16 -04002609 sc->led_on = 1; /* active high */
Bob Copeland3a078872008-06-25 22:35:28 -04002610 }
Bob Copeland63649b62009-01-01 15:01:44 -05002611 /* Pin 3 on Foxconn chips used in Acer Aspire One (0x105b:e008) */
2612 if (pdev->subsystem_vendor == PCI_VENDOR_ID_FOXCONN) {
2613 __set_bit(ATH_STAT_LEDSOFT, sc->status);
2614 sc->led_pin = 3;
2615 sc->led_on = 0; /* active low */
2616 }
2617
Bob Copeland3a078872008-06-25 22:35:28 -04002618 if (!test_bit(ATH_STAT_LEDSOFT, sc->status))
2619 goto out;
2620
2621 ath5k_led_enable(sc);
2622
2623 snprintf(name, sizeof(name), "ath5k-%s::rx", wiphy_name(hw->wiphy));
2624 ret = ath5k_register_led(sc, &sc->rx_led, name,
2625 ieee80211_get_rx_led_name(hw));
2626 if (ret)
2627 goto out;
2628
2629 snprintf(name, sizeof(name), "ath5k-%s::tx", wiphy_name(hw->wiphy));
2630 ret = ath5k_register_led(sc, &sc->tx_led, name,
2631 ieee80211_get_tx_led_name(hw));
2632out:
2633 return ret;
2634}
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002635
2636
2637/********************\
2638* Mac80211 functions *
2639\********************/
2640
2641static int
Johannes Berge039fa42008-05-15 12:55:29 +02002642ath5k_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002643{
2644 struct ath5k_softc *sc = hw->priv;
2645 struct ath5k_buf *bf;
2646 unsigned long flags;
2647 int hdrlen;
Benoit PAPILLAULT0fe45b12008-12-12 15:29:58 +01002648 int padsize;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002649
2650 ath5k_debug_dump_skb(sc, skb, "TX ", 1);
2651
Johannes Berg05c914f2008-09-11 00:01:58 +02002652 if (sc->opmode == NL80211_IFTYPE_MONITOR)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002653 ATH5K_DBG(sc, ATH5K_DEBUG_XMIT, "tx in monitor (scan?)\n");
2654
2655 /*
2656 * the hardware expects the header padded to 4 byte boundaries
2657 * if this is not the case we add the padding after the header
2658 */
2659 hdrlen = ieee80211_get_hdrlen_from_skb(skb);
Bob Copelandfd6effc2008-12-18 23:23:05 -05002660 padsize = ath5k_pad_size(hdrlen);
2661 if (padsize) {
Benoit PAPILLAULT0fe45b12008-12-12 15:29:58 +01002662
2663 if (skb_headroom(skb) < padsize) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002664 ATH5K_ERR(sc, "tx hdrlen not %%4: %d not enough"
Benoit PAPILLAULT0fe45b12008-12-12 15:29:58 +01002665 " headroom to pad %d\n", hdrlen, padsize);
Bob Copeland71ef99c2009-01-05 20:46:34 -05002666 return NETDEV_TX_BUSY;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002667 }
Benoit PAPILLAULT0fe45b12008-12-12 15:29:58 +01002668 skb_push(skb, padsize);
2669 memmove(skb->data, skb->data+padsize, hdrlen);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002670 }
2671
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002672 spin_lock_irqsave(&sc->txbuflock, flags);
2673 if (list_empty(&sc->txbuf)) {
2674 ATH5K_ERR(sc, "no further txbuf available, dropping packet\n");
2675 spin_unlock_irqrestore(&sc->txbuflock, flags);
Johannes Berge2530082008-05-17 00:57:14 +02002676 ieee80211_stop_queue(hw, skb_get_queue_mapping(skb));
Bob Copeland71ef99c2009-01-05 20:46:34 -05002677 return NETDEV_TX_BUSY;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002678 }
2679 bf = list_first_entry(&sc->txbuf, struct ath5k_buf, list);
2680 list_del(&bf->list);
2681 sc->txbuf_len--;
2682 if (list_empty(&sc->txbuf))
2683 ieee80211_stop_queues(hw);
2684 spin_unlock_irqrestore(&sc->txbuflock, flags);
2685
2686 bf->skb = skb;
2687
Johannes Berge039fa42008-05-15 12:55:29 +02002688 if (ath5k_txbuf_setup(sc, bf)) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002689 bf->skb = NULL;
2690 spin_lock_irqsave(&sc->txbuflock, flags);
2691 list_add_tail(&bf->list, &sc->txbuf);
2692 sc->txbuf_len++;
2693 spin_unlock_irqrestore(&sc->txbuflock, flags);
2694 dev_kfree_skb_any(skb);
Bob Copeland71ef99c2009-01-05 20:46:34 -05002695 return NETDEV_TX_OK;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002696 }
2697
Bob Copeland71ef99c2009-01-05 20:46:34 -05002698 return NETDEV_TX_OK;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002699}
2700
2701static int
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002702ath5k_reset(struct ath5k_softc *sc, bool stop, bool change_channel)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002703{
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002704 struct ath5k_hw *ah = sc->ah;
2705 int ret;
2706
2707 ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "resetting\n");
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002708
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002709 if (stop) {
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03002710 ath5k_hw_set_imr(ah, 0);
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002711 ath5k_txq_cleanup(sc);
2712 ath5k_rx_stop(sc);
2713 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002714 ret = ath5k_hw_reset(ah, sc->opmode, sc->curchan, true);
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002715 if (ret) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002716 ATH5K_ERR(sc, "can't reset hardware (%d)\n", ret);
2717 goto err;
2718 }
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002719
2720 /*
2721 * This is needed only to setup initial state
2722 * but it's best done after a reset.
2723 */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002724 ath5k_hw_set_txpower_limit(sc->ah, 0);
2725
2726 ret = ath5k_rx_start(sc);
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002727 if (ret) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002728 ATH5K_ERR(sc, "can't start recv logic\n");
2729 goto err;
2730 }
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002731
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002732 /*
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002733 * Change channels and update the h/w rate map if we're switching;
2734 * e.g. 11a to 11b/g.
2735 *
2736 * We may be doing a reset in response to an ioctl that changes the
2737 * channel so update any state that might change as a result.
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002738 *
2739 * XXX needed?
2740 */
2741/* ath5k_chan_change(sc, c); */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002742
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002743 ath5k_beacon_config(sc);
2744 /* intrs are enabled by ath5k_beacon_config */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002745
2746 return 0;
2747err:
2748 return ret;
2749}
2750
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002751static int
2752ath5k_reset_wake(struct ath5k_softc *sc)
2753{
2754 int ret;
2755
2756 ret = ath5k_reset(sc, true, true);
2757 if (!ret)
2758 ieee80211_wake_queues(sc->hw);
2759
2760 return ret;
2761}
2762
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002763static int ath5k_start(struct ieee80211_hw *hw)
2764{
Bob Copelandbb2beca2009-01-19 11:20:54 -05002765 return ath5k_init(hw->priv);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002766}
2767
2768static void ath5k_stop(struct ieee80211_hw *hw)
2769{
Bob Copelandbb2beca2009-01-19 11:20:54 -05002770 ath5k_stop_hw(hw->priv);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002771}
2772
2773static int ath5k_add_interface(struct ieee80211_hw *hw,
2774 struct ieee80211_if_init_conf *conf)
2775{
2776 struct ath5k_softc *sc = hw->priv;
2777 int ret;
2778
2779 mutex_lock(&sc->lock);
Johannes Berg32bfd352007-12-19 01:31:26 +01002780 if (sc->vif) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002781 ret = 0;
2782 goto end;
2783 }
2784
Johannes Berg32bfd352007-12-19 01:31:26 +01002785 sc->vif = conf->vif;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002786
2787 switch (conf->type) {
Jiri Slabyda966bc2008-10-12 22:54:10 +02002788 case NL80211_IFTYPE_AP:
Johannes Berg05c914f2008-09-11 00:01:58 +02002789 case NL80211_IFTYPE_STATION:
2790 case NL80211_IFTYPE_ADHOC:
Andrey Yurovskyb706e652008-10-13 18:23:07 -07002791 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg05c914f2008-09-11 00:01:58 +02002792 case NL80211_IFTYPE_MONITOR:
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002793 sc->opmode = conf->type;
2794 break;
2795 default:
2796 ret = -EOPNOTSUPP;
2797 goto end;
2798 }
Jiri Slaby67d2e2d2008-08-18 21:45:28 +02002799
2800 /* Set to a reasonable value. Note that this will
2801 * be set to mac80211's value at ath5k_config(). */
2802 sc->bintval = 1000;
Bob Copeland0e149cf2008-11-17 23:40:38 -05002803 ath5k_hw_set_lladdr(sc->ah, conf->mac_addr);
Jiri Slaby67d2e2d2008-08-18 21:45:28 +02002804
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002805 ret = 0;
2806end:
2807 mutex_unlock(&sc->lock);
2808 return ret;
2809}
2810
2811static void
2812ath5k_remove_interface(struct ieee80211_hw *hw,
2813 struct ieee80211_if_init_conf *conf)
2814{
2815 struct ath5k_softc *sc = hw->priv;
Bob Copeland0e149cf2008-11-17 23:40:38 -05002816 u8 mac[ETH_ALEN] = {};
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002817
2818 mutex_lock(&sc->lock);
Johannes Berg32bfd352007-12-19 01:31:26 +01002819 if (sc->vif != conf->vif)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002820 goto end;
2821
Bob Copeland0e149cf2008-11-17 23:40:38 -05002822 ath5k_hw_set_lladdr(sc->ah, mac);
Johannes Berg32bfd352007-12-19 01:31:26 +01002823 sc->vif = NULL;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002824end:
2825 mutex_unlock(&sc->lock);
2826}
2827
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -05002828/*
2829 * TODO: Phy disable/diversity etc
2830 */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002831static int
Johannes Berge8975582008-10-09 12:18:51 +02002832ath5k_config(struct ieee80211_hw *hw, u32 changed)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002833{
2834 struct ath5k_softc *sc = hw->priv;
Johannes Berge8975582008-10-09 12:18:51 +02002835 struct ieee80211_conf *conf = &hw->conf;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002836
Bruno Randolfe535c1a2008-01-18 21:51:40 +09002837 sc->bintval = conf->beacon_int;
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -05002838 sc->power_level = conf->power_level;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002839
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -05002840 return ath5k_chan_set(sc, conf->channel);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002841}
2842
2843static int
Johannes Berg32bfd352007-12-19 01:31:26 +01002844ath5k_config_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002845 struct ieee80211_if_conf *conf)
2846{
2847 struct ath5k_softc *sc = hw->priv;
2848 struct ath5k_hw *ah = sc->ah;
2849 int ret;
2850
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002851 mutex_lock(&sc->lock);
Johannes Berg32bfd352007-12-19 01:31:26 +01002852 if (sc->vif != vif) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002853 ret = -EIO;
2854 goto unlock;
2855 }
Jiri Slabyda966bc2008-10-12 22:54:10 +02002856 if (conf->changed & IEEE80211_IFCC_BSSID && conf->bssid) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002857 /* Cache for later use during resets */
2858 memcpy(ah->ah_bssid, conf->bssid, ETH_ALEN);
2859 /* XXX: assoc id is set to 0 for now, mac80211 doesn't have
2860 * a clean way of letting us retrieve this yet. */
2861 ath5k_hw_set_associd(ah, ah->ah_bssid, 0);
Jiri Slaby274c7c32008-07-15 17:44:20 +02002862 mmiowb();
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002863 }
Johannes Berg9d139c82008-07-09 14:40:37 +02002864 if (conf->changed & IEEE80211_IFCC_BEACON &&
Jiri Slabyda966bc2008-10-12 22:54:10 +02002865 (vif->type == NL80211_IFTYPE_ADHOC ||
Andrey Yurovskyb706e652008-10-13 18:23:07 -07002866 vif->type == NL80211_IFTYPE_MESH_POINT ||
Jiri Slabyda966bc2008-10-12 22:54:10 +02002867 vif->type == NL80211_IFTYPE_AP)) {
Johannes Berg9d139c82008-07-09 14:40:37 +02002868 struct sk_buff *beacon = ieee80211_beacon_get(hw, vif);
2869 if (!beacon) {
2870 ret = -ENOMEM;
2871 goto unlock;
2872 }
Jiri Slabyda966bc2008-10-12 22:54:10 +02002873 ath5k_beacon_update(sc, beacon);
Johannes Berg9d139c82008-07-09 14:40:37 +02002874 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002875 mutex_unlock(&sc->lock);
2876
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002877 return ath5k_reset_wake(sc);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002878unlock:
2879 mutex_unlock(&sc->lock);
2880 return ret;
2881}
2882
2883#define SUPPORTED_FIF_FLAGS \
2884 FIF_PROMISC_IN_BSS | FIF_ALLMULTI | FIF_FCSFAIL | \
2885 FIF_PLCPFAIL | FIF_CONTROL | FIF_OTHER_BSS | \
2886 FIF_BCN_PRBRESP_PROMISC
2887/*
2888 * o always accept unicast, broadcast, and multicast traffic
2889 * o multicast traffic for all BSSIDs will be enabled if mac80211
2890 * says it should be
2891 * o maintain current state of phy ofdm or phy cck error reception.
2892 * If the hardware detects any of these type of errors then
2893 * ath5k_hw_get_rx_filter() will pass to us the respective
2894 * hardware filters to be able to receive these type of frames.
2895 * o probe request frames are accepted only when operating in
2896 * hostap, adhoc, or monitor modes
2897 * o enable promiscuous mode according to the interface state
2898 * o accept beacons:
2899 * - when operating in adhoc mode so the 802.11 layer creates
2900 * node table entries for peers,
2901 * - when operating in station mode for collecting rssi data when
2902 * the station is otherwise quiet, or
2903 * - when scanning
2904 */
2905static void ath5k_configure_filter(struct ieee80211_hw *hw,
2906 unsigned int changed_flags,
2907 unsigned int *new_flags,
2908 int mc_count, struct dev_mc_list *mclist)
2909{
2910 struct ath5k_softc *sc = hw->priv;
2911 struct ath5k_hw *ah = sc->ah;
2912 u32 mfilt[2], val, rfilt;
2913 u8 pos;
2914 int i;
2915
2916 mfilt[0] = 0;
2917 mfilt[1] = 0;
2918
2919 /* Only deal with supported flags */
2920 changed_flags &= SUPPORTED_FIF_FLAGS;
2921 *new_flags &= SUPPORTED_FIF_FLAGS;
2922
2923 /* If HW detects any phy or radar errors, leave those filters on.
2924 * Also, always enable Unicast, Broadcasts and Multicast
2925 * XXX: move unicast, bssid broadcasts and multicast to mac80211 */
2926 rfilt = (ath5k_hw_get_rx_filter(ah) & (AR5K_RX_FILTER_PHYERR)) |
2927 (AR5K_RX_FILTER_UCAST | AR5K_RX_FILTER_BCAST |
2928 AR5K_RX_FILTER_MCAST);
2929
2930 if (changed_flags & (FIF_PROMISC_IN_BSS | FIF_OTHER_BSS)) {
2931 if (*new_flags & FIF_PROMISC_IN_BSS) {
2932 rfilt |= AR5K_RX_FILTER_PROM;
2933 __set_bit(ATH_STAT_PROMISC, sc->status);
John Daiker0bbac082008-10-17 12:16:00 -07002934 } else {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002935 __clear_bit(ATH_STAT_PROMISC, sc->status);
John Daiker0bbac082008-10-17 12:16:00 -07002936 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002937 }
2938
2939 /* Note, AR5K_RX_FILTER_MCAST is already enabled */
2940 if (*new_flags & FIF_ALLMULTI) {
2941 mfilt[0] = ~0;
2942 mfilt[1] = ~0;
2943 } else {
2944 for (i = 0; i < mc_count; i++) {
2945 if (!mclist)
2946 break;
2947 /* calculate XOR of eight 6-bit values */
Harvey Harrison533dd1b2008-04-29 01:03:36 -07002948 val = get_unaligned_le32(mclist->dmi_addr + 0);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002949 pos = (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
Harvey Harrison533dd1b2008-04-29 01:03:36 -07002950 val = get_unaligned_le32(mclist->dmi_addr + 3);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002951 pos ^= (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
2952 pos &= 0x3f;
2953 mfilt[pos / 32] |= (1 << (pos % 32));
2954 /* XXX: we might be able to just do this instead,
2955 * but not sure, needs testing, if we do use this we'd
2956 * neet to inform below to not reset the mcast */
2957 /* ath5k_hw_set_mcast_filterindex(ah,
2958 * mclist->dmi_addr[5]); */
2959 mclist = mclist->next;
2960 }
2961 }
2962
2963 /* This is the best we can do */
2964 if (*new_flags & (FIF_FCSFAIL | FIF_PLCPFAIL))
2965 rfilt |= AR5K_RX_FILTER_PHYERR;
2966
2967 /* FIF_BCN_PRBRESP_PROMISC really means to enable beacons
2968 * and probes for any BSSID, this needs testing */
2969 if (*new_flags & FIF_BCN_PRBRESP_PROMISC)
2970 rfilt |= AR5K_RX_FILTER_BEACON | AR5K_RX_FILTER_PROBEREQ;
2971
2972 /* FIF_CONTROL doc says that if FIF_PROMISC_IN_BSS is not
2973 * set we should only pass on control frames for this
2974 * station. This needs testing. I believe right now this
2975 * enables *all* control frames, which is OK.. but
2976 * but we should see if we can improve on granularity */
2977 if (*new_flags & FIF_CONTROL)
2978 rfilt |= AR5K_RX_FILTER_CONTROL;
2979
2980 /* Additional settings per mode -- this is per ath5k */
2981
2982 /* XXX move these to mac80211, and add a beacon IFF flag to mac80211 */
2983
Johannes Berg05c914f2008-09-11 00:01:58 +02002984 if (sc->opmode == NL80211_IFTYPE_MONITOR)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002985 rfilt |= AR5K_RX_FILTER_CONTROL | AR5K_RX_FILTER_BEACON |
2986 AR5K_RX_FILTER_PROBEREQ | AR5K_RX_FILTER_PROM;
Johannes Berg05c914f2008-09-11 00:01:58 +02002987 if (sc->opmode != NL80211_IFTYPE_STATION)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002988 rfilt |= AR5K_RX_FILTER_PROBEREQ;
Johannes Berg05c914f2008-09-11 00:01:58 +02002989 if (sc->opmode != NL80211_IFTYPE_AP &&
2990 sc->opmode != NL80211_IFTYPE_MESH_POINT &&
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002991 test_bit(ATH_STAT_PROMISC, sc->status))
2992 rfilt |= AR5K_RX_FILTER_PROM;
Martin Xu02969b32008-11-24 10:49:27 +08002993 if ((sc->opmode == NL80211_IFTYPE_STATION && sc->assoc) ||
Luis R. Rodriguez296bf2ae2008-11-03 14:43:00 -08002994 sc->opmode == NL80211_IFTYPE_ADHOC ||
2995 sc->opmode == NL80211_IFTYPE_AP)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002996 rfilt |= AR5K_RX_FILTER_BEACON;
Andrey Yurovskyb706e652008-10-13 18:23:07 -07002997 if (sc->opmode == NL80211_IFTYPE_MESH_POINT)
2998 rfilt |= AR5K_RX_FILTER_CONTROL | AR5K_RX_FILTER_BEACON |
2999 AR5K_RX_FILTER_PROBEREQ | AR5K_RX_FILTER_PROM;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003000
3001 /* Set filters */
John Daiker0bbac082008-10-17 12:16:00 -07003002 ath5k_hw_set_rx_filter(ah, rfilt);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003003
3004 /* Set multicast bits */
3005 ath5k_hw_set_mcast_filter(ah, mfilt[0], mfilt[1]);
3006 /* Set the cached hw filter flags, this will alter actually
3007 * be set in HW */
3008 sc->filter_flags = rfilt;
3009}
3010
3011static int
3012ath5k_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Johannes Bergdc822b52008-12-29 12:55:09 +01003013 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
3014 struct ieee80211_key_conf *key)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003015{
3016 struct ath5k_softc *sc = hw->priv;
3017 int ret = 0;
3018
Bob Copeland9ad9a262008-10-29 08:30:54 -04003019 if (modparam_nohwcrypt)
3020 return -EOPNOTSUPP;
3021
John Daiker0bbac082008-10-17 12:16:00 -07003022 switch (key->alg) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003023 case ALG_WEP:
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003024 case ALG_TKIP:
Bob Copeland3f64b432008-10-29 23:19:14 -04003025 break;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003026 case ALG_CCMP:
3027 return -EOPNOTSUPP;
3028 default:
3029 WARN_ON(1);
3030 return -EINVAL;
3031 }
3032
3033 mutex_lock(&sc->lock);
3034
3035 switch (cmd) {
3036 case SET_KEY:
Johannes Bergdc822b52008-12-29 12:55:09 +01003037 ret = ath5k_hw_set_key(sc->ah, key->keyidx, key,
3038 sta ? sta->addr : NULL);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003039 if (ret) {
3040 ATH5K_ERR(sc, "can't set the key\n");
3041 goto unlock;
3042 }
3043 __set_bit(key->keyidx, sc->keymap);
3044 key->hw_key_idx = key->keyidx;
Bob Copeland3f64b432008-10-29 23:19:14 -04003045 key->flags |= (IEEE80211_KEY_FLAG_GENERATE_IV |
3046 IEEE80211_KEY_FLAG_GENERATE_MMIC);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003047 break;
3048 case DISABLE_KEY:
3049 ath5k_hw_reset_key(sc->ah, key->keyidx);
3050 __clear_bit(key->keyidx, sc->keymap);
3051 break;
3052 default:
3053 ret = -EINVAL;
3054 goto unlock;
3055 }
3056
3057unlock:
Jiri Slaby274c7c32008-07-15 17:44:20 +02003058 mmiowb();
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003059 mutex_unlock(&sc->lock);
3060 return ret;
3061}
3062
3063static int
3064ath5k_get_stats(struct ieee80211_hw *hw,
3065 struct ieee80211_low_level_stats *stats)
3066{
3067 struct ath5k_softc *sc = hw->priv;
Nick Kossifidis194828a2008-04-16 18:49:02 +03003068 struct ath5k_hw *ah = sc->ah;
3069
3070 /* Force update */
3071 ath5k_hw_update_mib_counters(ah, &sc->ll_stats);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003072
3073 memcpy(stats, &sc->ll_stats, sizeof(sc->ll_stats));
3074
3075 return 0;
3076}
3077
3078static int
3079ath5k_get_tx_stats(struct ieee80211_hw *hw,
3080 struct ieee80211_tx_queue_stats *stats)
3081{
3082 struct ath5k_softc *sc = hw->priv;
3083
3084 memcpy(stats, &sc->tx_stats, sizeof(sc->tx_stats));
3085
3086 return 0;
3087}
3088
3089static u64
3090ath5k_get_tsf(struct ieee80211_hw *hw)
3091{
3092 struct ath5k_softc *sc = hw->priv;
3093
3094 return ath5k_hw_get_tsf64(sc->ah);
3095}
3096
3097static void
3098ath5k_reset_tsf(struct ieee80211_hw *hw)
3099{
3100 struct ath5k_softc *sc = hw->priv;
3101
Bruno Randolf9804b982008-01-19 18:17:59 +09003102 /*
3103 * in IBSS mode we need to update the beacon timers too.
3104 * this will also reset the TSF if we call it with 0
3105 */
Johannes Berg05c914f2008-09-11 00:01:58 +02003106 if (sc->opmode == NL80211_IFTYPE_ADHOC)
Bruno Randolf9804b982008-01-19 18:17:59 +09003107 ath5k_beacon_update_timers(sc, 0);
3108 else
3109 ath5k_hw_reset_tsf(sc->ah);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003110}
3111
3112static int
Jiri Slabyda966bc2008-10-12 22:54:10 +02003113ath5k_beacon_update(struct ath5k_softc *sc, struct sk_buff *skb)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003114{
Jiri Slaby00482972008-08-18 21:45:27 +02003115 unsigned long flags;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003116 int ret;
3117
3118 ath5k_debug_dump_skb(sc, skb, "BC ", 1);
3119
Jiri Slaby00482972008-08-18 21:45:27 +02003120 spin_lock_irqsave(&sc->block, flags);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003121 ath5k_txbuf_free(sc, sc->bbuf);
3122 sc->bbuf->skb = skb;
Johannes Berge039fa42008-05-15 12:55:29 +02003123 ret = ath5k_beacon_setup(sc, sc->bbuf);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003124 if (ret)
3125 sc->bbuf->skb = NULL;
Jiri Slaby00482972008-08-18 21:45:27 +02003126 spin_unlock_irqrestore(&sc->block, flags);
3127 if (!ret) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003128 ath5k_beacon_config(sc);
Jiri Slaby274c7c32008-07-15 17:44:20 +02003129 mmiowb();
3130 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003131
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003132 return ret;
3133}
Martin Xu02969b32008-11-24 10:49:27 +08003134static void
3135set_beacon_filter(struct ieee80211_hw *hw, bool enable)
3136{
3137 struct ath5k_softc *sc = hw->priv;
3138 struct ath5k_hw *ah = sc->ah;
3139 u32 rfilt;
3140 rfilt = ath5k_hw_get_rx_filter(ah);
3141 if (enable)
3142 rfilt |= AR5K_RX_FILTER_BEACON;
3143 else
3144 rfilt &= ~AR5K_RX_FILTER_BEACON;
3145 ath5k_hw_set_rx_filter(ah, rfilt);
3146 sc->filter_flags = rfilt;
3147}
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003148
Martin Xu02969b32008-11-24 10:49:27 +08003149static void ath5k_bss_info_changed(struct ieee80211_hw *hw,
3150 struct ieee80211_vif *vif,
3151 struct ieee80211_bss_conf *bss_conf,
3152 u32 changes)
3153{
3154 struct ath5k_softc *sc = hw->priv;
3155 if (changes & BSS_CHANGED_ASSOC) {
3156 mutex_lock(&sc->lock);
3157 sc->assoc = bss_conf->assoc;
3158 if (sc->opmode == NL80211_IFTYPE_STATION)
3159 set_beacon_filter(hw, sc->assoc);
3160 mutex_unlock(&sc->lock);
3161 }
3162}