blob: ba2fad23a7a5844defcddcaf50c7caf7bde7dea9 [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
Bob Copeland9ad9a262008-10-29 08:30:54 -040062static int modparam_nohwcrypt;
Bob Copeland46802a42009-04-15 07:57:34 -040063module_param_named(nohwcrypt, modparam_nohwcrypt, bool, S_IRUGO);
Bob Copeland9ad9a262008-10-29 08:30:54 -040064MODULE_PARM_DESC(nohwcrypt, "Disable hardware encryption.");
Jiri Slabyfa1c1142007-08-12 17:33:16 +020065
Bob Copeland42639fc2009-03-30 08:05:29 -040066static int modparam_all_channels;
Bob Copeland46802a42009-04-15 07:57:34 -040067module_param_named(all_channels, modparam_all_channels, bool, S_IRUGO);
Bob Copeland42639fc2009-03-30 08:05:29 -040068MODULE_PARM_DESC(all_channels, "Expose all channels the device can use.");
69
Jiri Slabyfa1c1142007-08-12 17:33:16 +020070
71/******************\
72* Internal defines *
73\******************/
74
75/* Module info */
76MODULE_AUTHOR("Jiri Slaby");
77MODULE_AUTHOR("Nick Kossifidis");
78MODULE_DESCRIPTION("Support for 5xxx series of Atheros 802.11 wireless LAN cards.");
79MODULE_SUPPORTED_DEVICE("Atheros 5xxx WLAN cards");
80MODULE_LICENSE("Dual BSD/GPL");
Nick Kossifidis0d5f0312008-09-29 01:27:27 +030081MODULE_VERSION("0.6.0 (EXPERIMENTAL)");
Jiri Slabyfa1c1142007-08-12 17:33:16 +020082
83
84/* Known PCI ids */
Alexey Dobriyana3aa1882010-01-07 11:58:11 +000085static DEFINE_PCI_DEVICE_TABLE(ath5k_pci_id_table) = {
Pavel Roskin97a81f52009-08-26 22:30:09 -040086 { PCI_VDEVICE(ATHEROS, 0x0207) }, /* 5210 early */
87 { PCI_VDEVICE(ATHEROS, 0x0007) }, /* 5210 */
88 { PCI_VDEVICE(ATHEROS, 0x0011) }, /* 5311 - this is on AHB bus !*/
89 { PCI_VDEVICE(ATHEROS, 0x0012) }, /* 5211 */
90 { PCI_VDEVICE(ATHEROS, 0x0013) }, /* 5212 */
91 { PCI_VDEVICE(3COM_2, 0x0013) }, /* 3com 5212 */
92 { PCI_VDEVICE(3COM, 0x0013) }, /* 3com 3CRDAG675 5212 */
93 { PCI_VDEVICE(ATHEROS, 0x1014) }, /* IBM minipci 5212 */
94 { PCI_VDEVICE(ATHEROS, 0x0014) }, /* 5212 combatible */
95 { PCI_VDEVICE(ATHEROS, 0x0015) }, /* 5212 combatible */
96 { PCI_VDEVICE(ATHEROS, 0x0016) }, /* 5212 combatible */
97 { PCI_VDEVICE(ATHEROS, 0x0017) }, /* 5212 combatible */
98 { PCI_VDEVICE(ATHEROS, 0x0018) }, /* 5212 combatible */
99 { PCI_VDEVICE(ATHEROS, 0x0019) }, /* 5212 combatible */
100 { PCI_VDEVICE(ATHEROS, 0x001a) }, /* 2413 Griffin-lite */
101 { PCI_VDEVICE(ATHEROS, 0x001b) }, /* 5413 Eagle */
102 { PCI_VDEVICE(ATHEROS, 0x001c) }, /* PCI-E cards */
103 { PCI_VDEVICE(ATHEROS, 0x001d) }, /* 2417 Nala */
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200104 { 0 }
105};
106MODULE_DEVICE_TABLE(pci, ath5k_pci_id_table);
107
108/* Known SREVs */
Jiri Slaby2c91108c2009-03-07 10:26:41 +0100109static const struct ath5k_srev_name srev_names[] = {
Nick Kossifidis1bef0162008-09-29 02:09:09 +0300110 { "5210", AR5K_VERSION_MAC, AR5K_SREV_AR5210 },
111 { "5311", AR5K_VERSION_MAC, AR5K_SREV_AR5311 },
112 { "5311A", AR5K_VERSION_MAC, AR5K_SREV_AR5311A },
113 { "5311B", AR5K_VERSION_MAC, AR5K_SREV_AR5311B },
114 { "5211", AR5K_VERSION_MAC, AR5K_SREV_AR5211 },
115 { "5212", AR5K_VERSION_MAC, AR5K_SREV_AR5212 },
116 { "5213", AR5K_VERSION_MAC, AR5K_SREV_AR5213 },
117 { "5213A", AR5K_VERSION_MAC, AR5K_SREV_AR5213A },
118 { "2413", AR5K_VERSION_MAC, AR5K_SREV_AR2413 },
119 { "2414", AR5K_VERSION_MAC, AR5K_SREV_AR2414 },
120 { "5424", AR5K_VERSION_MAC, AR5K_SREV_AR5424 },
121 { "5413", AR5K_VERSION_MAC, AR5K_SREV_AR5413 },
122 { "5414", AR5K_VERSION_MAC, AR5K_SREV_AR5414 },
123 { "2415", AR5K_VERSION_MAC, AR5K_SREV_AR2415 },
124 { "5416", AR5K_VERSION_MAC, AR5K_SREV_AR5416 },
125 { "5418", AR5K_VERSION_MAC, AR5K_SREV_AR5418 },
126 { "2425", AR5K_VERSION_MAC, AR5K_SREV_AR2425 },
127 { "2417", AR5K_VERSION_MAC, AR5K_SREV_AR2417 },
128 { "xxxxx", AR5K_VERSION_MAC, AR5K_SREV_UNKNOWN },
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200129 { "5110", AR5K_VERSION_RAD, AR5K_SREV_RAD_5110 },
130 { "5111", AR5K_VERSION_RAD, AR5K_SREV_RAD_5111 },
Nick Kossifidis1bef0162008-09-29 02:09:09 +0300131 { "5111A", AR5K_VERSION_RAD, AR5K_SREV_RAD_5111A },
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200132 { "2111", AR5K_VERSION_RAD, AR5K_SREV_RAD_2111 },
133 { "5112", AR5K_VERSION_RAD, AR5K_SREV_RAD_5112 },
134 { "5112A", AR5K_VERSION_RAD, AR5K_SREV_RAD_5112A },
Nick Kossifidis1bef0162008-09-29 02:09:09 +0300135 { "5112B", AR5K_VERSION_RAD, AR5K_SREV_RAD_5112B },
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200136 { "2112", AR5K_VERSION_RAD, AR5K_SREV_RAD_2112 },
137 { "2112A", AR5K_VERSION_RAD, AR5K_SREV_RAD_2112A },
Nick Kossifidis1bef0162008-09-29 02:09:09 +0300138 { "2112B", AR5K_VERSION_RAD, AR5K_SREV_RAD_2112B },
139 { "2413", AR5K_VERSION_RAD, AR5K_SREV_RAD_2413 },
140 { "5413", AR5K_VERSION_RAD, AR5K_SREV_RAD_5413 },
141 { "2316", AR5K_VERSION_RAD, AR5K_SREV_RAD_2316 },
142 { "2317", AR5K_VERSION_RAD, AR5K_SREV_RAD_2317 },
143 { "5424", AR5K_VERSION_RAD, AR5K_SREV_RAD_5424 },
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200144 { "5133", AR5K_VERSION_RAD, AR5K_SREV_RAD_5133 },
145 { "xxxxx", AR5K_VERSION_RAD, AR5K_SREV_UNKNOWN },
146};
147
Jiri Slaby2c91108c2009-03-07 10:26:41 +0100148static const struct ieee80211_rate ath5k_rates[] = {
Bruno Randolf63266a62008-07-30 17:12:58 +0200149 { .bitrate = 10,
150 .hw_value = ATH5K_RATE_CODE_1M, },
151 { .bitrate = 20,
152 .hw_value = ATH5K_RATE_CODE_2M,
153 .hw_value_short = ATH5K_RATE_CODE_2M | AR5K_SET_SHORT_PREAMBLE,
154 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
155 { .bitrate = 55,
156 .hw_value = ATH5K_RATE_CODE_5_5M,
157 .hw_value_short = ATH5K_RATE_CODE_5_5M | AR5K_SET_SHORT_PREAMBLE,
158 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
159 { .bitrate = 110,
160 .hw_value = ATH5K_RATE_CODE_11M,
161 .hw_value_short = ATH5K_RATE_CODE_11M | AR5K_SET_SHORT_PREAMBLE,
162 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
163 { .bitrate = 60,
164 .hw_value = ATH5K_RATE_CODE_6M,
165 .flags = 0 },
166 { .bitrate = 90,
167 .hw_value = ATH5K_RATE_CODE_9M,
168 .flags = 0 },
169 { .bitrate = 120,
170 .hw_value = ATH5K_RATE_CODE_12M,
171 .flags = 0 },
172 { .bitrate = 180,
173 .hw_value = ATH5K_RATE_CODE_18M,
174 .flags = 0 },
175 { .bitrate = 240,
176 .hw_value = ATH5K_RATE_CODE_24M,
177 .flags = 0 },
178 { .bitrate = 360,
179 .hw_value = ATH5K_RATE_CODE_36M,
180 .flags = 0 },
181 { .bitrate = 480,
182 .hw_value = ATH5K_RATE_CODE_48M,
183 .flags = 0 },
184 { .bitrate = 540,
185 .hw_value = ATH5K_RATE_CODE_54M,
186 .flags = 0 },
187 /* XR missing */
188};
189
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200190/*
191 * Prototypes - PCI stack related functions
192 */
193static int __devinit ath5k_pci_probe(struct pci_dev *pdev,
194 const struct pci_device_id *id);
195static void __devexit ath5k_pci_remove(struct pci_dev *pdev);
196#ifdef CONFIG_PM
Rafael J. Wysockibaee1f32009-10-05 00:52:09 +0200197static int ath5k_pci_suspend(struct device *dev);
198static int ath5k_pci_resume(struct device *dev);
199
Pavel Roskin626ede62010-02-18 20:28:02 -0500200static SIMPLE_DEV_PM_OPS(ath5k_pm_ops, ath5k_pci_suspend, ath5k_pci_resume);
Rafael J. Wysockibaee1f32009-10-05 00:52:09 +0200201#define ATH5K_PM_OPS (&ath5k_pm_ops)
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200202#else
Rafael J. Wysockibaee1f32009-10-05 00:52:09 +0200203#define ATH5K_PM_OPS NULL
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200204#endif /* CONFIG_PM */
205
John W. Linville04a9e452008-02-01 16:03:45 -0500206static struct pci_driver ath5k_pci_driver = {
Johannes Berg9764f3f2008-11-10 18:56:59 +0100207 .name = KBUILD_MODNAME,
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200208 .id_table = ath5k_pci_id_table,
209 .probe = ath5k_pci_probe,
210 .remove = __devexit_p(ath5k_pci_remove),
Rafael J. Wysockibaee1f32009-10-05 00:52:09 +0200211 .driver.pm = ATH5K_PM_OPS,
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200212};
213
214
215
216/*
217 * Prototypes - MAC 802.11 stack related functions
218 */
Johannes Berge039fa42008-05-15 12:55:29 +0200219static int ath5k_tx(struct ieee80211_hw *hw, struct sk_buff *skb);
Bob Copelandcec8db22009-07-04 12:59:51 -0400220static int ath5k_tx_queue(struct ieee80211_hw *hw, struct sk_buff *skb,
221 struct ath5k_txq *txq);
Bob Copeland209d8892009-05-07 08:09:08 -0400222static int ath5k_reset(struct ath5k_softc *sc, struct ieee80211_channel *chan);
Jiri Slabyd7dc1002008-07-23 13:17:35 +0200223static int ath5k_reset_wake(struct ath5k_softc *sc);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200224static int ath5k_start(struct ieee80211_hw *hw);
225static void ath5k_stop(struct ieee80211_hw *hw);
226static int ath5k_add_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +0100227 struct ieee80211_vif *vif);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200228static void ath5k_remove_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +0100229 struct ieee80211_vif *vif);
Johannes Berge8975582008-10-09 12:18:51 +0200230static int ath5k_config(struct ieee80211_hw *hw, u32 changed);
Johannes Berg3ac64be2009-08-17 16:16:53 +0200231static u64 ath5k_prepare_multicast(struct ieee80211_hw *hw,
232 int mc_count, struct dev_addr_list *mc_list);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200233static void ath5k_configure_filter(struct ieee80211_hw *hw,
234 unsigned int changed_flags,
235 unsigned int *new_flags,
Johannes Berg3ac64be2009-08-17 16:16:53 +0200236 u64 multicast);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200237static int ath5k_set_key(struct ieee80211_hw *hw,
238 enum set_key_cmd cmd,
Johannes Bergdc822b52008-12-29 12:55:09 +0100239 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200240 struct ieee80211_key_conf *key);
241static int ath5k_get_stats(struct ieee80211_hw *hw,
242 struct ieee80211_low_level_stats *stats);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200243static u64 ath5k_get_tsf(struct ieee80211_hw *hw);
Alina Friedrichsen3b5d6652009-01-24 07:09:59 +0100244static void ath5k_set_tsf(struct ieee80211_hw *hw, u64 tsf);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200245static void ath5k_reset_tsf(struct ieee80211_hw *hw);
Bob Copeland1071db82009-05-18 10:59:52 -0400246static int ath5k_beacon_update(struct ieee80211_hw *hw,
247 struct ieee80211_vif *vif);
Martin Xu02969b32008-11-24 10:49:27 +0800248static void ath5k_bss_info_changed(struct ieee80211_hw *hw,
249 struct ieee80211_vif *vif,
250 struct ieee80211_bss_conf *bss_conf,
251 u32 changes);
Bob Copelandf0f3d382009-06-10 22:22:21 -0400252static void ath5k_sw_scan_start(struct ieee80211_hw *hw);
253static void ath5k_sw_scan_complete(struct ieee80211_hw *hw);
Lukáš Turek6e08d222009-12-21 22:50:51 +0100254static void ath5k_set_coverage_class(struct ieee80211_hw *hw,
255 u8 coverage_class);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200256
Jiri Slaby2c91108c2009-03-07 10:26:41 +0100257static const struct ieee80211_ops ath5k_hw_ops = {
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200258 .tx = ath5k_tx,
259 .start = ath5k_start,
260 .stop = ath5k_stop,
261 .add_interface = ath5k_add_interface,
262 .remove_interface = ath5k_remove_interface,
263 .config = ath5k_config,
Johannes Berg3ac64be2009-08-17 16:16:53 +0200264 .prepare_multicast = ath5k_prepare_multicast,
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200265 .configure_filter = ath5k_configure_filter,
266 .set_key = ath5k_set_key,
267 .get_stats = ath5k_get_stats,
268 .conf_tx = NULL,
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200269 .get_tsf = ath5k_get_tsf,
Alina Friedrichsen3b5d6652009-01-24 07:09:59 +0100270 .set_tsf = ath5k_set_tsf,
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200271 .reset_tsf = ath5k_reset_tsf,
Martin Xu02969b32008-11-24 10:49:27 +0800272 .bss_info_changed = ath5k_bss_info_changed,
Bob Copelandf0f3d382009-06-10 22:22:21 -0400273 .sw_scan_start = ath5k_sw_scan_start,
274 .sw_scan_complete = ath5k_sw_scan_complete,
Lukáš Turek6e08d222009-12-21 22:50:51 +0100275 .set_coverage_class = ath5k_set_coverage_class,
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200276};
277
278/*
279 * Prototypes - Internal functions
280 */
281/* Attach detach */
282static int ath5k_attach(struct pci_dev *pdev,
283 struct ieee80211_hw *hw);
284static void ath5k_detach(struct pci_dev *pdev,
285 struct ieee80211_hw *hw);
286/* Channel/mode setup */
287static inline short ath5k_ieee2mhz(short chan);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200288static unsigned int ath5k_copy_channels(struct ath5k_hw *ah,
289 struct ieee80211_channel *channels,
290 unsigned int mode,
291 unsigned int max);
Bruno Randolf63266a62008-07-30 17:12:58 +0200292static int ath5k_setup_bands(struct ieee80211_hw *hw);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200293static int ath5k_chan_set(struct ath5k_softc *sc,
294 struct ieee80211_channel *chan);
295static void ath5k_setcurmode(struct ath5k_softc *sc,
296 unsigned int mode);
297static void ath5k_mode_setup(struct ath5k_softc *sc);
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500298
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200299/* Descriptor setup */
300static int ath5k_desc_alloc(struct ath5k_softc *sc,
301 struct pci_dev *pdev);
302static void ath5k_desc_free(struct ath5k_softc *sc,
303 struct pci_dev *pdev);
304/* Buffers setup */
305static int ath5k_rxbuf_setup(struct ath5k_softc *sc,
306 struct ath5k_buf *bf);
307static int ath5k_txbuf_setup(struct ath5k_softc *sc,
Bob Copelandcec8db22009-07-04 12:59:51 -0400308 struct ath5k_buf *bf,
Benoit Papillault8127fbd2010-02-27 23:05:26 +0100309 struct ath5k_txq *txq, int padsize);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200310static inline void ath5k_txbuf_free(struct ath5k_softc *sc,
311 struct ath5k_buf *bf)
312{
313 BUG_ON(!bf);
314 if (!bf->skb)
315 return;
316 pci_unmap_single(sc->pdev, bf->skbaddr, bf->skb->len,
317 PCI_DMA_TODEVICE);
Jiri Slaby00482972008-08-18 21:45:27 +0200318 dev_kfree_skb_any(bf->skb);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200319 bf->skb = NULL;
320}
321
Felix Fietkaua6c8d372009-01-30 01:36:48 +0100322static inline void ath5k_rxbuf_free(struct ath5k_softc *sc,
323 struct ath5k_buf *bf)
324{
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -0800325 struct ath5k_hw *ah = sc->ah;
326 struct ath_common *common = ath5k_hw_common(ah);
327
Felix Fietkaua6c8d372009-01-30 01:36:48 +0100328 BUG_ON(!bf);
329 if (!bf->skb)
330 return;
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -0800331 pci_unmap_single(sc->pdev, bf->skbaddr, common->rx_bufsize,
Felix Fietkaua6c8d372009-01-30 01:36:48 +0100332 PCI_DMA_FROMDEVICE);
333 dev_kfree_skb_any(bf->skb);
334 bf->skb = NULL;
335}
336
337
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200338/* Queues setup */
339static struct ath5k_txq *ath5k_txq_setup(struct ath5k_softc *sc,
340 int qtype, int subtype);
341static int ath5k_beaconq_setup(struct ath5k_hw *ah);
342static int ath5k_beaconq_config(struct ath5k_softc *sc);
343static void ath5k_txq_drainq(struct ath5k_softc *sc,
344 struct ath5k_txq *txq);
345static void ath5k_txq_cleanup(struct ath5k_softc *sc);
346static void ath5k_txq_release(struct ath5k_softc *sc);
347/* Rx handling */
348static int ath5k_rx_start(struct ath5k_softc *sc);
349static void ath5k_rx_stop(struct ath5k_softc *sc);
350static unsigned int ath5k_rx_decrypted(struct ath5k_softc *sc,
351 struct ath5k_desc *ds,
Bruno Randolfb47f4072008-03-05 18:35:45 +0900352 struct sk_buff *skb,
353 struct ath5k_rx_status *rs);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200354static void ath5k_tasklet_rx(unsigned long data);
355/* Tx handling */
356static void ath5k_tx_processq(struct ath5k_softc *sc,
357 struct ath5k_txq *txq);
358static void ath5k_tasklet_tx(unsigned long data);
359/* Beacon handling */
360static int ath5k_beacon_setup(struct ath5k_softc *sc,
Johannes Berge039fa42008-05-15 12:55:29 +0200361 struct ath5k_buf *bf);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200362static void ath5k_beacon_send(struct ath5k_softc *sc);
363static void ath5k_beacon_config(struct ath5k_softc *sc);
Bruno Randolf9804b982008-01-19 18:17:59 +0900364static void ath5k_beacon_update_timers(struct ath5k_softc *sc, u64 bc_tsf);
Bob Copelandacf3c1a2009-02-15 12:06:11 -0500365static void ath5k_tasklet_beacon(unsigned long data);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200366
367static inline u64 ath5k_extend_tsf(struct ath5k_hw *ah, u32 rstamp)
368{
369 u64 tsf = ath5k_hw_get_tsf64(ah);
370
371 if ((tsf & 0x7fff) < rstamp)
372 tsf -= 0x8000;
373
374 return (tsf & ~0x7fff) | rstamp;
375}
376
377/* Interrupt handling */
Bob Copelandbb2beca2009-01-19 11:20:54 -0500378static int ath5k_init(struct ath5k_softc *sc);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200379static int ath5k_stop_locked(struct ath5k_softc *sc);
Bob Copelandbb2beca2009-01-19 11:20:54 -0500380static int ath5k_stop_hw(struct ath5k_softc *sc);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200381static irqreturn_t ath5k_intr(int irq, void *dev_id);
382static void ath5k_tasklet_reset(unsigned long data);
383
Nick Kossifidis6e220662009-08-10 03:31:31 +0300384static void ath5k_tasklet_calibrate(unsigned long data);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200385
386/*
387 * Module init/exit functions
388 */
389static int __init
390init_ath5k_pci(void)
391{
392 int ret;
393
394 ath5k_debug_init();
395
John W. Linville04a9e452008-02-01 16:03:45 -0500396 ret = pci_register_driver(&ath5k_pci_driver);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200397 if (ret) {
398 printk(KERN_ERR "ath5k_pci: can't register pci driver\n");
399 return ret;
400 }
401
402 return 0;
403}
404
405static void __exit
406exit_ath5k_pci(void)
407{
John W. Linville04a9e452008-02-01 16:03:45 -0500408 pci_unregister_driver(&ath5k_pci_driver);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200409
410 ath5k_debug_finish();
411}
412
413module_init(init_ath5k_pci);
414module_exit(exit_ath5k_pci);
415
416
417/********************\
418* PCI Initialization *
419\********************/
420
421static const char *
422ath5k_chip_name(enum ath5k_srev_type type, u_int16_t val)
423{
424 const char *name = "xxxxx";
425 unsigned int i;
426
427 for (i = 0; i < ARRAY_SIZE(srev_names); i++) {
428 if (srev_names[i].sr_type != type)
429 continue;
Nick Kossifidis75d0edb2008-09-29 01:24:44 +0300430
431 if ((val & 0xf0) == srev_names[i].sr_val)
432 name = srev_names[i].sr_name;
433
434 if ((val & 0xff) == srev_names[i].sr_val) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200435 name = srev_names[i].sr_name;
436 break;
437 }
438 }
439
440 return name;
441}
Luis R. Rodrigueze5aa8472009-09-10 16:55:11 -0700442static unsigned int ath5k_ioread32(void *hw_priv, u32 reg_offset)
443{
444 struct ath5k_hw *ah = (struct ath5k_hw *) hw_priv;
445 return ath5k_hw_reg_read(ah, reg_offset);
446}
447
448static void ath5k_iowrite32(void *hw_priv, u32 val, u32 reg_offset)
449{
450 struct ath5k_hw *ah = (struct ath5k_hw *) hw_priv;
451 ath5k_hw_reg_write(ah, val, reg_offset);
452}
453
454static const struct ath_ops ath5k_common_ops = {
455 .read = ath5k_ioread32,
456 .write = ath5k_iowrite32,
457};
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200458
459static int __devinit
460ath5k_pci_probe(struct pci_dev *pdev,
461 const struct pci_device_id *id)
462{
463 void __iomem *mem;
464 struct ath5k_softc *sc;
Luis R. Rodriguezdb719712009-09-10 11:20:57 -0700465 struct ath_common *common;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200466 struct ieee80211_hw *hw;
467 int ret;
468 u8 csz;
469
470 ret = pci_enable_device(pdev);
471 if (ret) {
472 dev_err(&pdev->dev, "can't enable device\n");
473 goto err;
474 }
475
476 /* XXX 32-bit addressing only */
Yang Hongyang284901a2009-04-06 19:01:15 -0700477 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200478 if (ret) {
479 dev_err(&pdev->dev, "32-bit DMA not available\n");
480 goto err_dis;
481 }
482
483 /*
484 * Cache line size is used to size and align various
485 * structures used to communicate with the hardware.
486 */
487 pci_read_config_byte(pdev, PCI_CACHE_LINE_SIZE, &csz);
488 if (csz == 0) {
489 /*
490 * Linux 2.4.18 (at least) writes the cache line size
491 * register as a 16-bit wide register which is wrong.
492 * We must have this setup properly for rx buffer
493 * DMA to work so force a reasonable value here if it
494 * comes up zero.
495 */
Luis R. Rodriguez13311b02009-08-12 09:57:01 -0700496 csz = L1_CACHE_BYTES >> 2;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200497 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, csz);
498 }
499 /*
500 * The default setting of latency timer yields poor results,
501 * set it to the value used by other systems. It may be worth
502 * tweaking this setting more.
503 */
504 pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0xa8);
505
506 /* Enable bus mastering */
507 pci_set_master(pdev);
508
509 /*
510 * Disable the RETRY_TIMEOUT register (0x41) to keep
511 * PCI Tx retries from interfering with C3 CPU state.
512 */
513 pci_write_config_byte(pdev, 0x41, 0);
514
515 ret = pci_request_region(pdev, 0, "ath5k");
516 if (ret) {
517 dev_err(&pdev->dev, "cannot reserve PCI memory region\n");
518 goto err_dis;
519 }
520
521 mem = pci_iomap(pdev, 0, 0);
522 if (!mem) {
523 dev_err(&pdev->dev, "cannot remap PCI memory region\n") ;
524 ret = -EIO;
525 goto err_reg;
526 }
527
528 /*
529 * Allocate hw (mac80211 main struct)
530 * and hw->priv (driver private data)
531 */
532 hw = ieee80211_alloc_hw(sizeof(*sc), &ath5k_hw_ops);
533 if (hw == NULL) {
534 dev_err(&pdev->dev, "cannot allocate ieee80211_hw\n");
535 ret = -ENOMEM;
536 goto err_map;
537 }
538
539 dev_info(&pdev->dev, "registered as '%s'\n", wiphy_name(hw->wiphy));
540
541 /* Initialize driver private data */
542 SET_IEEE80211_DEV(hw, &pdev->dev);
Bruno Randolf566bfe52008-05-08 19:15:40 +0200543 hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
Bob Copelandcec8db22009-07-04 12:59:51 -0400544 IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
Bruno Randolf566bfe52008-05-08 19:15:40 +0200545 IEEE80211_HW_SIGNAL_DBM |
546 IEEE80211_HW_NOISE_DBM;
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700547
548 hw->wiphy->interface_modes =
Jiri Slaby6f5f39c2009-04-30 15:55:48 -0400549 BIT(NL80211_IFTYPE_AP) |
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -0700550 BIT(NL80211_IFTYPE_STATION) |
551 BIT(NL80211_IFTYPE_ADHOC) |
552 BIT(NL80211_IFTYPE_MESH_POINT);
553
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200554 hw->extra_tx_headroom = 2;
555 hw->channel_change_time = 5000;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200556 sc = hw->priv;
557 sc->hw = hw;
558 sc->pdev = pdev;
559
560 ath5k_debug_init_device(sc);
561
562 /*
563 * Mark the device as detached to avoid processing
564 * interrupts until setup is complete.
565 */
566 __set_bit(ATH_STAT_INVALID, sc->status);
567
568 sc->iobase = mem; /* So we can unmap it on detach */
Johannes Berg05c914f2008-09-11 00:01:58 +0200569 sc->opmode = NL80211_IFTYPE_STATION;
Jiri Slabyeab0cd42009-06-19 01:06:45 +0200570 sc->bintval = 1000;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200571 mutex_init(&sc->lock);
572 spin_lock_init(&sc->rxbuflock);
573 spin_lock_init(&sc->txbuflock);
Jiri Slaby00482972008-08-18 21:45:27 +0200574 spin_lock_init(&sc->block);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200575
576 /* Set private data */
577 pci_set_drvdata(pdev, hw);
578
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200579 /* Setup interrupt handler */
580 ret = request_irq(pdev->irq, ath5k_intr, IRQF_SHARED, "ath", sc);
581 if (ret) {
582 ATH5K_ERR(sc, "request_irq failed\n");
583 goto err_free;
584 }
585
Luis R. Rodriguez9adca122009-09-10 18:04:47 -0700586 /*If we passed the test malloc a ath5k_hw struct*/
587 sc->ah = kzalloc(sizeof(struct ath5k_hw), GFP_KERNEL);
588 if (!sc->ah) {
589 ret = -ENOMEM;
590 ATH5K_ERR(sc, "out of memory\n");
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200591 goto err_irq;
592 }
593
Luis R. Rodriguez9adca122009-09-10 18:04:47 -0700594 sc->ah->ah_sc = sc;
595 sc->ah->ah_iobase = sc->iobase;
Luis R. Rodriguezdb719712009-09-10 11:20:57 -0700596 common = ath5k_hw_common(sc->ah);
Luis R. Rodrigueze5aa8472009-09-10 16:55:11 -0700597 common->ops = &ath5k_common_ops;
Luis R. Rodriguez13b81552009-09-10 17:52:45 -0700598 common->ah = sc->ah;
Luis R. Rodriguezb002a4a2009-09-13 00:03:27 -0700599 common->hw = hw;
Luis R. Rodriguezdb719712009-09-10 11:20:57 -0700600 common->cachelsz = csz << 2; /* convert to bytes */
601
Luis R. Rodriguez9adca122009-09-10 18:04:47 -0700602 /* Initialize device */
603 ret = ath5k_hw_attach(sc);
604 if (ret) {
605 goto err_free_ah;
606 }
607
Felix Fietkau2f7fe872008-10-05 18:05:48 +0200608 /* set up multi-rate retry capabilities */
609 if (sc->ah->ah_version == AR5K_AR5212) {
Johannes Berge6a98542008-10-21 12:40:02 +0200610 hw->max_rates = 4;
611 hw->max_rate_tries = 11;
Felix Fietkau2f7fe872008-10-05 18:05:48 +0200612 }
613
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200614 /* Finish private driver data initialization */
615 ret = ath5k_attach(pdev, hw);
616 if (ret)
617 goto err_ah;
618
619 ATH5K_INFO(sc, "Atheros AR%s chip found (MAC: 0x%x, PHY: 0x%x)\n",
Nick Kossifidis1bef0162008-09-29 02:09:09 +0300620 ath5k_chip_name(AR5K_VERSION_MAC, sc->ah->ah_mac_srev),
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200621 sc->ah->ah_mac_srev,
622 sc->ah->ah_phy_revision);
623
Luis R. Rodriguez400ec452008-02-03 21:51:49 -0500624 if (!sc->ah->ah_single_chip) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200625 /* Single chip radio (!RF5111) */
Luis R. Rodriguez400ec452008-02-03 21:51:49 -0500626 if (sc->ah->ah_radio_5ghz_revision &&
627 !sc->ah->ah_radio_2ghz_revision) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200628 /* No 5GHz support -> report 2GHz radio */
Luis R. Rodriguez400ec452008-02-03 21:51:49 -0500629 if (!test_bit(AR5K_MODE_11A,
630 sc->ah->ah_capabilities.cap_mode)) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200631 ATH5K_INFO(sc, "RF%s 2GHz radio found (0x%x)\n",
Luis R. Rodriguez400ec452008-02-03 21:51:49 -0500632 ath5k_chip_name(AR5K_VERSION_RAD,
633 sc->ah->ah_radio_5ghz_revision),
634 sc->ah->ah_radio_5ghz_revision);
635 /* No 2GHz support (5110 and some
636 * 5Ghz only cards) -> report 5Ghz radio */
637 } else if (!test_bit(AR5K_MODE_11B,
638 sc->ah->ah_capabilities.cap_mode)) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200639 ATH5K_INFO(sc, "RF%s 5GHz radio found (0x%x)\n",
Luis R. Rodriguez400ec452008-02-03 21:51:49 -0500640 ath5k_chip_name(AR5K_VERSION_RAD,
641 sc->ah->ah_radio_5ghz_revision),
642 sc->ah->ah_radio_5ghz_revision);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200643 /* Multiband radio */
644 } else {
645 ATH5K_INFO(sc, "RF%s multiband radio found"
646 " (0x%x)\n",
Luis R. Rodriguez400ec452008-02-03 21:51:49 -0500647 ath5k_chip_name(AR5K_VERSION_RAD,
648 sc->ah->ah_radio_5ghz_revision),
649 sc->ah->ah_radio_5ghz_revision);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200650 }
651 }
Luis R. Rodriguez400ec452008-02-03 21:51:49 -0500652 /* Multi chip radio (RF5111 - RF2111) ->
653 * report both 2GHz/5GHz radios */
654 else if (sc->ah->ah_radio_5ghz_revision &&
655 sc->ah->ah_radio_2ghz_revision){
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200656 ATH5K_INFO(sc, "RF%s 5GHz radio found (0x%x)\n",
Luis R. Rodriguez400ec452008-02-03 21:51:49 -0500657 ath5k_chip_name(AR5K_VERSION_RAD,
658 sc->ah->ah_radio_5ghz_revision),
659 sc->ah->ah_radio_5ghz_revision);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200660 ATH5K_INFO(sc, "RF%s 2GHz radio found (0x%x)\n",
Luis R. Rodriguez400ec452008-02-03 21:51:49 -0500661 ath5k_chip_name(AR5K_VERSION_RAD,
662 sc->ah->ah_radio_2ghz_revision),
663 sc->ah->ah_radio_2ghz_revision);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200664 }
665 }
666
667
668 /* ready to process interrupts */
669 __clear_bit(ATH_STAT_INVALID, sc->status);
670
671 return 0;
672err_ah:
673 ath5k_hw_detach(sc->ah);
674err_irq:
675 free_irq(pdev->irq, sc);
Luis R. Rodriguez9adca122009-09-10 18:04:47 -0700676err_free_ah:
677 kfree(sc->ah);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200678err_free:
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200679 ieee80211_free_hw(hw);
680err_map:
681 pci_iounmap(pdev, mem);
682err_reg:
683 pci_release_region(pdev, 0);
684err_dis:
685 pci_disable_device(pdev);
686err:
687 return ret;
688}
689
690static void __devexit
691ath5k_pci_remove(struct pci_dev *pdev)
692{
693 struct ieee80211_hw *hw = pci_get_drvdata(pdev);
694 struct ath5k_softc *sc = hw->priv;
695
696 ath5k_debug_finish_device(sc);
697 ath5k_detach(pdev, hw);
698 ath5k_hw_detach(sc->ah);
Luis R. Rodriguez9adca122009-09-10 18:04:47 -0700699 kfree(sc->ah);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200700 free_irq(pdev->irq, sc);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200701 pci_iounmap(pdev, sc->iobase);
702 pci_release_region(pdev, 0);
703 pci_disable_device(pdev);
704 ieee80211_free_hw(hw);
705}
706
707#ifdef CONFIG_PM
Rafael J. Wysockibaee1f32009-10-05 00:52:09 +0200708static int ath5k_pci_suspend(struct device *dev)
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200709{
Rafael J. Wysockibaee1f32009-10-05 00:52:09 +0200710 struct ieee80211_hw *hw = pci_get_drvdata(to_pci_dev(dev));
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200711 struct ath5k_softc *sc = hw->priv;
712
Bob Copeland3a078872008-06-25 22:35:28 -0400713 ath5k_led_off(sc);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200714 return 0;
715}
716
Rafael J. Wysockibaee1f32009-10-05 00:52:09 +0200717static int ath5k_pci_resume(struct device *dev)
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200718{
Rafael J. Wysockibaee1f32009-10-05 00:52:09 +0200719 struct pci_dev *pdev = to_pci_dev(dev);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200720 struct ieee80211_hw *hw = pci_get_drvdata(pdev);
721 struct ath5k_softc *sc = hw->priv;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200722
Jouni Malinen8451d222009-06-16 11:59:23 +0300723 /*
724 * Suspend/Resume resets the PCI configuration space, so we have to
725 * re-disable the RETRY_TIMEOUT register (0x41) to keep
726 * PCI Tx retries from interfering with C3 CPU state
727 */
728 pci_write_config_byte(pdev, 0x41, 0);
729
Bob Copeland3a078872008-06-25 22:35:28 -0400730 ath5k_led_enable(sc);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200731 return 0;
732}
733#endif /* CONFIG_PM */
734
735
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200736/***********************\
737* Driver Initialization *
738\***********************/
739
Bob Copelandf769c362009-03-30 22:30:31 -0400740static int ath5k_reg_notifier(struct wiphy *wiphy, struct regulatory_request *request)
741{
742 struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
743 struct ath5k_softc *sc = hw->priv;
Luis R. Rodriguezdb719712009-09-10 11:20:57 -0700744 struct ath_regulatory *regulatory = ath5k_hw_regulatory(sc->ah);
Bob Copelandf769c362009-03-30 22:30:31 -0400745
Luis R. Rodriguez608b88c2009-08-17 18:07:23 -0700746 return ath_reg_notifier_apply(wiphy, request, regulatory);
Bob Copelandf769c362009-03-30 22:30:31 -0400747}
748
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200749static int
750ath5k_attach(struct pci_dev *pdev, struct ieee80211_hw *hw)
751{
752 struct ath5k_softc *sc = hw->priv;
753 struct ath5k_hw *ah = sc->ah;
Luis R. Rodriguezdb719712009-09-10 11:20:57 -0700754 struct ath_regulatory *regulatory = ath5k_hw_regulatory(ah);
Bob Copeland0e149cf2008-11-17 23:40:38 -0500755 u8 mac[ETH_ALEN] = {};
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200756 int ret;
757
758 ATH5K_DBG(sc, ATH5K_DEBUG_ANY, "devid 0x%x\n", pdev->device);
759
760 /*
761 * Check if the MAC has multi-rate retry support.
762 * We do this by trying to setup a fake extended
763 * descriptor. MAC's that don't have support will
764 * return false w/o doing anything. MAC's that do
765 * support it will return true w/o doing anything.
766 */
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300767 ret = ah->ah_setup_mrr_tx_desc(ah, NULL, 0, 0, 0, 0, 0, 0);
Jiri Slabyb9887632008-02-15 21:58:52 +0100768 if (ret < 0)
769 goto err;
770 if (ret > 0)
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200771 __set_bit(ATH_STAT_MRRETRY, sc->status);
772
773 /*
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200774 * Collect the channel list. The 802.11 layer
775 * is resposible for filtering this list based
776 * on settings like the phy mode and regulatory
777 * domain restrictions.
778 */
Bruno Randolf63266a62008-07-30 17:12:58 +0200779 ret = ath5k_setup_bands(hw);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200780 if (ret) {
781 ATH5K_ERR(sc, "can't get channels\n");
782 goto err;
783 }
784
785 /* NB: setup here so ath5k_rate_update is happy */
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500786 if (test_bit(AR5K_MODE_11A, ah->ah_modes))
787 ath5k_setcurmode(sc, AR5K_MODE_11A);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200788 else
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500789 ath5k_setcurmode(sc, AR5K_MODE_11B);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200790
791 /*
792 * Allocate tx+rx descriptors and populate the lists.
793 */
794 ret = ath5k_desc_alloc(sc, pdev);
795 if (ret) {
796 ATH5K_ERR(sc, "can't allocate descriptors\n");
797 goto err;
798 }
799
800 /*
801 * Allocate hardware transmit queues: one queue for
802 * beacon frames and one data queue for each QoS
803 * priority. Note that hw functions handle reseting
804 * these queues at the needed time.
805 */
806 ret = ath5k_beaconq_setup(ah);
807 if (ret < 0) {
808 ATH5K_ERR(sc, "can't setup a beacon xmit queue\n");
809 goto err_desc;
810 }
811 sc->bhalq = ret;
Bob Copelandcec8db22009-07-04 12:59:51 -0400812 sc->cabq = ath5k_txq_setup(sc, AR5K_TX_QUEUE_CAB, 0);
813 if (IS_ERR(sc->cabq)) {
814 ATH5K_ERR(sc, "can't setup cab queue\n");
815 ret = PTR_ERR(sc->cabq);
816 goto err_bhal;
817 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200818
819 sc->txq = ath5k_txq_setup(sc, AR5K_TX_QUEUE_DATA, AR5K_WME_AC_BK);
820 if (IS_ERR(sc->txq)) {
821 ATH5K_ERR(sc, "can't setup xmit queue\n");
822 ret = PTR_ERR(sc->txq);
Bob Copelandcec8db22009-07-04 12:59:51 -0400823 goto err_queues;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200824 }
825
826 tasklet_init(&sc->rxtq, ath5k_tasklet_rx, (unsigned long)sc);
827 tasklet_init(&sc->txtq, ath5k_tasklet_tx, (unsigned long)sc);
828 tasklet_init(&sc->restq, ath5k_tasklet_reset, (unsigned long)sc);
Nick Kossifidis6e220662009-08-10 03:31:31 +0300829 tasklet_init(&sc->calib, ath5k_tasklet_calibrate, (unsigned long)sc);
Bob Copelandacf3c1a2009-02-15 12:06:11 -0500830 tasklet_init(&sc->beacontq, ath5k_tasklet_beacon, (unsigned long)sc);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200831
Bob Copeland0e149cf2008-11-17 23:40:38 -0500832 ret = ath5k_eeprom_read_mac(ah, mac);
833 if (ret) {
834 ATH5K_ERR(sc, "unable to read address from EEPROM: 0x%04x\n",
835 sc->pdev->device);
836 goto err_queues;
837 }
838
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200839 SET_IEEE80211_PERM_ADDR(hw, mac);
840 /* All MAC address bits matter for ACKs */
Luis R. Rodriguez17753742009-09-09 22:19:26 -0700841 memcpy(sc->bssidmask, ath_bcast_mac, ETH_ALEN);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200842 ath5k_hw_set_bssid_mask(sc->ah, sc->bssidmask);
843
Luis R. Rodriguez608b88c2009-08-17 18:07:23 -0700844 regulatory->current_rd = ah->ah_capabilities.cap_eeprom.ee_regdomain;
845 ret = ath_regd_init(regulatory, hw->wiphy, ath5k_reg_notifier);
Bob Copelandf769c362009-03-30 22:30:31 -0400846 if (ret) {
847 ATH5K_ERR(sc, "can't initialize regulatory system\n");
848 goto err_queues;
849 }
850
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200851 ret = ieee80211_register_hw(hw);
852 if (ret) {
853 ATH5K_ERR(sc, "can't register ieee80211 hw\n");
854 goto err_queues;
855 }
856
Luis R. Rodriguez608b88c2009-08-17 18:07:23 -0700857 if (!ath_is_world_regd(regulatory))
858 regulatory_hint(hw->wiphy, regulatory->alpha2);
Bob Copelandf769c362009-03-30 22:30:31 -0400859
Bob Copeland3a078872008-06-25 22:35:28 -0400860 ath5k_init_leds(sc);
861
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200862 return 0;
863err_queues:
864 ath5k_txq_release(sc);
865err_bhal:
866 ath5k_hw_release_tx_queue(ah, sc->bhalq);
867err_desc:
868 ath5k_desc_free(sc, pdev);
869err:
870 return ret;
871}
872
873static void
874ath5k_detach(struct pci_dev *pdev, struct ieee80211_hw *hw)
875{
876 struct ath5k_softc *sc = hw->priv;
877
878 /*
879 * NB: the order of these is important:
880 * o call the 802.11 layer before detaching ath5k_hw to
881 * insure callbacks into the driver to delete global
882 * key cache entries can be handled
883 * o reclaim the tx queue data structures after calling
884 * the 802.11 layer as we'll get called back to reclaim
885 * node state and potentially want to use them
886 * o to cleanup the tx queues the hal is called, so detach
887 * it last
888 * XXX: ??? detach ath5k_hw ???
889 * Other than that, it's straightforward...
890 */
891 ieee80211_unregister_hw(hw);
892 ath5k_desc_free(sc, pdev);
893 ath5k_txq_release(sc);
894 ath5k_hw_release_tx_queue(sc->ah, sc->bhalq);
Bob Copeland3a078872008-06-25 22:35:28 -0400895 ath5k_unregister_leds(sc);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200896
897 /*
898 * NB: can't reclaim these until after ieee80211_ifdetach
899 * returns because we'll get called back to reclaim node
900 * state and potentially want to use them.
901 */
902}
903
904
905
906
907/********************\
908* Channel/mode setup *
909\********************/
910
911/*
912 * Convert IEEE channel number to MHz frequency.
913 */
914static inline short
915ath5k_ieee2mhz(short chan)
916{
917 if (chan <= 14 || chan >= 27)
918 return ieee80211chan2mhz(chan);
919 else
920 return 2212 + chan * 20;
921}
922
Bob Copeland42639fc2009-03-30 08:05:29 -0400923/*
924 * Returns true for the channel numbers used without all_channels modparam.
925 */
926static bool ath5k_is_standard_channel(short chan)
927{
928 return ((chan <= 14) ||
929 /* UNII 1,2 */
930 ((chan & 3) == 0 && chan >= 36 && chan <= 64) ||
931 /* midband */
932 ((chan & 3) == 0 && chan >= 100 && chan <= 140) ||
933 /* UNII-3 */
934 ((chan & 3) == 1 && chan >= 149 && chan <= 165));
935}
936
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200937static unsigned int
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200938ath5k_copy_channels(struct ath5k_hw *ah,
939 struct ieee80211_channel *channels,
940 unsigned int mode,
941 unsigned int max)
942{
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500943 unsigned int i, count, size, chfreq, freq, ch;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200944
945 if (!test_bit(mode, ah->ah_modes))
946 return 0;
947
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200948 switch (mode) {
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500949 case AR5K_MODE_11A:
950 case AR5K_MODE_11A_TURBO:
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200951 /* 1..220, but 2GHz frequencies are filtered by check_channel */
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500952 size = 220 ;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200953 chfreq = CHANNEL_5GHZ;
954 break;
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500955 case AR5K_MODE_11B:
956 case AR5K_MODE_11G:
957 case AR5K_MODE_11G_TURBO:
958 size = 26;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200959 chfreq = CHANNEL_2GHZ;
960 break;
961 default:
962 ATH5K_WARN(ah->ah_sc, "bad mode, not copying channels\n");
963 return 0;
964 }
965
966 for (i = 0, count = 0; i < size && max > 0; i++) {
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500967 ch = i + 1 ;
968 freq = ath5k_ieee2mhz(ch);
969
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200970 /* Check if channel is supported by the chipset */
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500971 if (!ath5k_channel_ok(ah, freq, chfreq))
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200972 continue;
973
Bob Copeland42639fc2009-03-30 08:05:29 -0400974 if (!modparam_all_channels && !ath5k_is_standard_channel(ch))
975 continue;
976
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500977 /* Write channel info and increment counter */
978 channels[count].center_freq = freq;
Luis R. Rodrigueza3f4b912008-02-03 21:52:10 -0500979 channels[count].band = (chfreq == CHANNEL_2GHZ) ?
980 IEEE80211_BAND_2GHZ : IEEE80211_BAND_5GHZ;
Luis R. Rodriguez400ec452008-02-03 21:51:49 -0500981 switch (mode) {
982 case AR5K_MODE_11A:
983 case AR5K_MODE_11G:
984 channels[count].hw_value = chfreq | CHANNEL_OFDM;
985 break;
986 case AR5K_MODE_11A_TURBO:
987 case AR5K_MODE_11G_TURBO:
988 channels[count].hw_value = chfreq |
989 CHANNEL_OFDM | CHANNEL_TURBO;
990 break;
991 case AR5K_MODE_11B:
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500992 channels[count].hw_value = CHANNEL_B;
993 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200994
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200995 count++;
996 max--;
997 }
998
999 return count;
1000}
1001
Bruno Randolf63266a62008-07-30 17:12:58 +02001002static void
1003ath5k_setup_rate_idx(struct ath5k_softc *sc, struct ieee80211_supported_band *b)
1004{
1005 u8 i;
1006
1007 for (i = 0; i < AR5K_MAX_RATES; i++)
1008 sc->rate_idx[b->band][i] = -1;
1009
1010 for (i = 0; i < b->n_bitrates; i++) {
1011 sc->rate_idx[b->band][b->bitrates[i].hw_value] = i;
1012 if (b->bitrates[i].hw_value_short)
1013 sc->rate_idx[b->band][b->bitrates[i].hw_value_short] = i;
1014 }
1015}
1016
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001017static int
Bruno Randolf63266a62008-07-30 17:12:58 +02001018ath5k_setup_bands(struct ieee80211_hw *hw)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001019{
1020 struct ath5k_softc *sc = hw->priv;
1021 struct ath5k_hw *ah = sc->ah;
Bruno Randolf63266a62008-07-30 17:12:58 +02001022 struct ieee80211_supported_band *sband;
1023 int max_c, count_c = 0;
1024 int i;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001025
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -05001026 BUILD_BUG_ON(ARRAY_SIZE(sc->sbands) < IEEE80211_NUM_BANDS);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001027 max_c = ARRAY_SIZE(sc->channels);
1028
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -05001029 /* 2GHz band */
Bruno Randolf63266a62008-07-30 17:12:58 +02001030 sband = &sc->sbands[IEEE80211_BAND_2GHZ];
1031 sband->band = IEEE80211_BAND_2GHZ;
1032 sband->bitrates = &sc->rates[IEEE80211_BAND_2GHZ][0];
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001033
Bruno Randolf63266a62008-07-30 17:12:58 +02001034 if (test_bit(AR5K_MODE_11G, sc->ah->ah_capabilities.cap_mode)) {
1035 /* G mode */
1036 memcpy(sband->bitrates, &ath5k_rates[0],
1037 sizeof(struct ieee80211_rate) * 12);
1038 sband->n_bitrates = 12;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001039
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -05001040 sband->channels = sc->channels;
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -05001041 sband->n_channels = ath5k_copy_channels(ah, sband->channels,
Bruno Randolf63266a62008-07-30 17:12:58 +02001042 AR5K_MODE_11G, max_c);
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -05001043
1044 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = sband;
Bruno Randolf63266a62008-07-30 17:12:58 +02001045 count_c = sband->n_channels;
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -05001046 max_c -= count_c;
Bruno Randolf63266a62008-07-30 17:12:58 +02001047 } else if (test_bit(AR5K_MODE_11B, sc->ah->ah_capabilities.cap_mode)) {
1048 /* B mode */
1049 memcpy(sband->bitrates, &ath5k_rates[0],
1050 sizeof(struct ieee80211_rate) * 4);
1051 sband->n_bitrates = 4;
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -05001052
Bruno Randolf63266a62008-07-30 17:12:58 +02001053 /* 5211 only supports B rates and uses 4bit rate codes
1054 * (e.g normally we have 0x1B for 1M, but on 5211 we have 0x0B)
1055 * fix them up here:
1056 */
1057 if (ah->ah_version == AR5K_AR5211) {
1058 for (i = 0; i < 4; i++) {
1059 sband->bitrates[i].hw_value =
1060 sband->bitrates[i].hw_value & 0xF;
1061 sband->bitrates[i].hw_value_short =
1062 sband->bitrates[i].hw_value_short & 0xF;
1063 }
1064 }
1065
1066 sband->channels = sc->channels;
1067 sband->n_channels = ath5k_copy_channels(ah, sband->channels,
1068 AR5K_MODE_11B, max_c);
1069
1070 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = sband;
1071 count_c = sband->n_channels;
1072 max_c -= count_c;
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -05001073 }
Bruno Randolf63266a62008-07-30 17:12:58 +02001074 ath5k_setup_rate_idx(sc, sband);
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -05001075
Bruno Randolf63266a62008-07-30 17:12:58 +02001076 /* 5GHz band, A mode */
Luis R. Rodriguez400ec452008-02-03 21:51:49 -05001077 if (test_bit(AR5K_MODE_11A, sc->ah->ah_capabilities.cap_mode)) {
Bruno Randolf63266a62008-07-30 17:12:58 +02001078 sband = &sc->sbands[IEEE80211_BAND_5GHZ];
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -05001079 sband->band = IEEE80211_BAND_5GHZ;
Bruno Randolf63266a62008-07-30 17:12:58 +02001080 sband->bitrates = &sc->rates[IEEE80211_BAND_5GHZ][0];
1081
1082 memcpy(sband->bitrates, &ath5k_rates[4],
1083 sizeof(struct ieee80211_rate) * 8);
1084 sband->n_bitrates = 8;
1085
1086 sband->channels = &sc->channels[count_c];
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -05001087 sband->n_channels = ath5k_copy_channels(ah, sband->channels,
1088 AR5K_MODE_11A, max_c);
1089
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -05001090 hw->wiphy->bands[IEEE80211_BAND_5GHZ] = sband;
1091 }
Bruno Randolf63266a62008-07-30 17:12:58 +02001092 ath5k_setup_rate_idx(sc, sband);
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -05001093
Luis R. Rodriguezb4461972008-02-04 10:03:54 -05001094 ath5k_debug_dump_bands(sc);
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -05001095
1096 return 0;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001097}
1098
1099/*
Joerg Alberte30eb4a2009-08-05 01:52:07 +02001100 * Set/change channels. We always reset the chip.
1101 * To accomplish this we must first cleanup any pending DMA,
1102 * then restart stuff after a la ath5k_init.
Bob Copelandbe009372009-01-22 08:44:16 -05001103 *
1104 * Called with sc->lock.
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001105 */
1106static int
1107ath5k_chan_set(struct ath5k_softc *sc, struct ieee80211_channel *chan)
1108{
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -05001109 ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "(%u MHz) -> (%u MHz)\n",
1110 sc->curchan->center_freq, chan->center_freq);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001111
Joerg Alberte30eb4a2009-08-05 01:52:07 +02001112 /*
1113 * To switch channels clear any pending DMA operations;
1114 * wait long enough for the RX fifo to drain, reset the
1115 * hardware at the new frequency, and then re-enable
1116 * the relevant bits of the h/w.
1117 */
1118 return ath5k_reset(sc, chan);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001119}
1120
1121static void
1122ath5k_setcurmode(struct ath5k_softc *sc, unsigned int mode)
1123{
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001124 sc->curmode = mode;
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -05001125
Luis R. Rodriguez400ec452008-02-03 21:51:49 -05001126 if (mode == AR5K_MODE_11A) {
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -05001127 sc->curband = &sc->sbands[IEEE80211_BAND_5GHZ];
1128 } else {
1129 sc->curband = &sc->sbands[IEEE80211_BAND_2GHZ];
1130 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001131}
1132
1133static void
1134ath5k_mode_setup(struct ath5k_softc *sc)
1135{
1136 struct ath5k_hw *ah = sc->ah;
1137 u32 rfilt;
1138
1139 /* configure rx filter */
1140 rfilt = sc->filter_flags;
1141 ath5k_hw_set_rx_filter(ah, rfilt);
1142
1143 if (ath5k_hw_hasbssidmask(ah))
1144 ath5k_hw_set_bssid_mask(ah, sc->bssidmask);
1145
1146 /* configure operational mode */
Bruno Randolfccfe5552010-03-09 16:55:38 +09001147 ath5k_hw_set_opmode(ah, sc->opmode);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001148
Bruno Randolfccfe5552010-03-09 16:55:38 +09001149 ATH5K_DBG(sc, ATH5K_DEBUG_MODE, "mode setup opmode %d\n", sc->opmode);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001150 ATH5K_DBG(sc, ATH5K_DEBUG_MODE, "RX filter 0x%x\n", rfilt);
1151}
1152
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -05001153static inline int
Bruno Randolf63266a62008-07-30 17:12:58 +02001154ath5k_hw_to_driver_rix(struct ath5k_softc *sc, int hw_rix)
1155{
Bob Copelandb7266042009-03-02 21:55:18 -05001156 int rix;
1157
1158 /* return base rate on errors */
1159 if (WARN(hw_rix < 0 || hw_rix >= AR5K_MAX_RATES,
1160 "hw_rix out of bounds: %x\n", hw_rix))
1161 return 0;
1162
1163 rix = sc->rate_idx[sc->curband->band][hw_rix];
1164 if (WARN(rix < 0, "invalid hw_rix: %x\n", hw_rix))
1165 rix = 0;
1166
1167 return rix;
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -05001168}
1169
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001170/***************\
1171* Buffers setup *
1172\***************/
1173
Bob Copelandb6ea0352009-01-10 14:42:54 -05001174static
1175struct sk_buff *ath5k_rx_skb_alloc(struct ath5k_softc *sc, dma_addr_t *skb_addr)
1176{
Luis R. Rodriguezdb719712009-09-10 11:20:57 -07001177 struct ath_common *common = ath5k_hw_common(sc->ah);
Bob Copelandb6ea0352009-01-10 14:42:54 -05001178 struct sk_buff *skb;
Bob Copelandb6ea0352009-01-10 14:42:54 -05001179
1180 /*
1181 * Allocate buffer with headroom_needed space for the
1182 * fake physical layer header at the start.
1183 */
Luis R. Rodriguezdb719712009-09-10 11:20:57 -07001184 skb = ath_rxbuf_alloc(common,
Luis R. Rodriguezdd849782009-11-04 09:44:50 -08001185 common->rx_bufsize,
Luis R. Rodriguezaeb63cf2009-08-12 09:57:00 -07001186 GFP_ATOMIC);
Bob Copelandb6ea0352009-01-10 14:42:54 -05001187
1188 if (!skb) {
1189 ATH5K_ERR(sc, "can't alloc skbuff of size %u\n",
Luis R. Rodriguezdd849782009-11-04 09:44:50 -08001190 common->rx_bufsize);
Bob Copelandb6ea0352009-01-10 14:42:54 -05001191 return NULL;
1192 }
Bob Copelandb6ea0352009-01-10 14:42:54 -05001193
1194 *skb_addr = pci_map_single(sc->pdev,
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -08001195 skb->data, common->rx_bufsize,
1196 PCI_DMA_FROMDEVICE);
Bob Copelandb6ea0352009-01-10 14:42:54 -05001197 if (unlikely(pci_dma_mapping_error(sc->pdev, *skb_addr))) {
1198 ATH5K_ERR(sc, "%s: DMA mapping failed\n", __func__);
1199 dev_kfree_skb(skb);
1200 return NULL;
1201 }
1202 return skb;
1203}
1204
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001205static int
1206ath5k_rxbuf_setup(struct ath5k_softc *sc, struct ath5k_buf *bf)
1207{
1208 struct ath5k_hw *ah = sc->ah;
1209 struct sk_buff *skb = bf->skb;
1210 struct ath5k_desc *ds;
1211
Bob Copelandb6ea0352009-01-10 14:42:54 -05001212 if (!skb) {
1213 skb = ath5k_rx_skb_alloc(sc, &bf->skbaddr);
1214 if (!skb)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001215 return -ENOMEM;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001216 bf->skb = skb;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001217 }
1218
1219 /*
1220 * Setup descriptors. For receive we always terminate
1221 * the descriptor list with a self-linked entry so we'll
1222 * not get overrun under high load (as can happen with a
1223 * 5212 when ANI processing enables PHY error frames).
1224 *
1225 * To insure the last descriptor is self-linked we create
1226 * each descriptor as self-linked and add it to the end. As
1227 * each additional descriptor is added the previous self-linked
1228 * entry is ``fixed'' naturally. This should be safe even
1229 * if DMA is happening. When processing RX interrupts we
1230 * never remove/process the last, self-linked, entry on the
1231 * descriptor list. This insures the hardware always has
1232 * someplace to write a new frame.
1233 */
1234 ds = bf->desc;
1235 ds->ds_link = bf->daddr; /* link to self */
1236 ds->ds_data = bf->skbaddr;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001237 ah->ah_setup_rx_desc(ah, ds,
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001238 skb_tailroom(skb), /* buffer size */
1239 0);
1240
1241 if (sc->rxlink != NULL)
1242 *sc->rxlink = bf->daddr;
1243 sc->rxlink = &ds->ds_link;
1244 return 0;
1245}
1246
Bob Copeland2ac29272010-02-09 13:06:54 -05001247static enum ath5k_pkt_type get_hw_packet_type(struct sk_buff *skb)
1248{
1249 struct ieee80211_hdr *hdr;
1250 enum ath5k_pkt_type htype;
1251 __le16 fc;
1252
1253 hdr = (struct ieee80211_hdr *)skb->data;
1254 fc = hdr->frame_control;
1255
1256 if (ieee80211_is_beacon(fc))
1257 htype = AR5K_PKT_TYPE_BEACON;
1258 else if (ieee80211_is_probe_resp(fc))
1259 htype = AR5K_PKT_TYPE_PROBE_RESP;
1260 else if (ieee80211_is_atim(fc))
1261 htype = AR5K_PKT_TYPE_ATIM;
1262 else if (ieee80211_is_pspoll(fc))
1263 htype = AR5K_PKT_TYPE_PSPOLL;
1264 else
1265 htype = AR5K_PKT_TYPE_NORMAL;
1266
1267 return htype;
1268}
1269
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001270static int
Bob Copelandcec8db22009-07-04 12:59:51 -04001271ath5k_txbuf_setup(struct ath5k_softc *sc, struct ath5k_buf *bf,
Benoit Papillault8127fbd2010-02-27 23:05:26 +01001272 struct ath5k_txq *txq, int padsize)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001273{
1274 struct ath5k_hw *ah = sc->ah;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001275 struct ath5k_desc *ds = bf->desc;
1276 struct sk_buff *skb = bf->skb;
Johannes Berga888d522008-05-26 16:43:39 +02001277 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001278 unsigned int pktlen, flags, keyidx = AR5K_TXKEYIX_INVALID;
Felix Fietkau2f7fe872008-10-05 18:05:48 +02001279 struct ieee80211_rate *rate;
1280 unsigned int mrr_rate[3], mrr_tries[3];
1281 int i, ret;
Bob Copeland8902ff42009-01-22 08:44:20 -05001282 u16 hw_rate;
Bob Copeland07c1e852009-01-22 08:44:21 -05001283 u16 cts_rate = 0;
1284 u16 duration = 0;
Bob Copeland8902ff42009-01-22 08:44:20 -05001285 u8 rc_flags;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001286
1287 flags = AR5K_TXDESC_INTREQ | AR5K_TXDESC_CLRDMASK;
Johannes Berge039fa42008-05-15 12:55:29 +02001288
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001289 /* XXX endianness */
1290 bf->skbaddr = pci_map_single(sc->pdev, skb->data, skb->len,
1291 PCI_DMA_TODEVICE);
1292
Bob Copeland8902ff42009-01-22 08:44:20 -05001293 rate = ieee80211_get_tx_rate(sc->hw, info);
1294
Johannes Berge039fa42008-05-15 12:55:29 +02001295 if (info->flags & IEEE80211_TX_CTL_NO_ACK)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001296 flags |= AR5K_TXDESC_NOACK;
1297
Bob Copeland8902ff42009-01-22 08:44:20 -05001298 rc_flags = info->control.rates[0].flags;
1299 hw_rate = (rc_flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE) ?
1300 rate->hw_value_short : rate->hw_value;
1301
Bruno Randolf281c56d2008-02-05 18:44:55 +09001302 pktlen = skb->len;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001303
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02001304 /* FIXME: If we are in g mode and rate is a CCK rate
1305 * subtract ah->ah_txpower.txp_cck_ofdm_pwr_delta
1306 * from tx power (value is in dB units already) */
Bob Copeland362695e2009-02-15 12:06:12 -05001307 if (info->control.hw_key) {
1308 keyidx = info->control.hw_key->hw_key_idx;
1309 pktlen += info->control.hw_key->icv_len;
1310 }
Bob Copeland07c1e852009-01-22 08:44:21 -05001311 if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
1312 flags |= AR5K_TXDESC_RTSENA;
1313 cts_rate = ieee80211_get_rts_cts_rate(sc->hw, info)->hw_value;
1314 duration = le16_to_cpu(ieee80211_rts_duration(sc->hw,
1315 sc->vif, pktlen, info));
1316 }
1317 if (rc_flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
1318 flags |= AR5K_TXDESC_CTSENA;
1319 cts_rate = ieee80211_get_rts_cts_rate(sc->hw, info)->hw_value;
1320 duration = le16_to_cpu(ieee80211_ctstoself_duration(sc->hw,
1321 sc->vif, pktlen, info));
1322 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001323 ret = ah->ah_setup_tx_desc(ah, ds, pktlen,
Benoit Papillault8127fbd2010-02-27 23:05:26 +01001324 ieee80211_get_hdrlen_from_skb(skb), padsize,
Bob Copeland2ac29272010-02-09 13:06:54 -05001325 get_hw_packet_type(skb),
Johannes Berg2e92e6f2008-05-15 12:55:27 +02001326 (sc->power_level * 2),
Bob Copeland8902ff42009-01-22 08:44:20 -05001327 hw_rate,
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04001328 info->control.rates[0].count, keyidx, ah->ah_tx_ant, flags,
Bob Copeland07c1e852009-01-22 08:44:21 -05001329 cts_rate, duration);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001330 if (ret)
1331 goto err_unmap;
1332
Felix Fietkau2f7fe872008-10-05 18:05:48 +02001333 memset(mrr_rate, 0, sizeof(mrr_rate));
1334 memset(mrr_tries, 0, sizeof(mrr_tries));
1335 for (i = 0; i < 3; i++) {
1336 rate = ieee80211_get_alt_retry_rate(sc->hw, info, i);
1337 if (!rate)
1338 break;
1339
1340 mrr_rate[i] = rate->hw_value;
Johannes Berge6a98542008-10-21 12:40:02 +02001341 mrr_tries[i] = info->control.rates[i + 1].count;
Felix Fietkau2f7fe872008-10-05 18:05:48 +02001342 }
1343
1344 ah->ah_setup_mrr_tx_desc(ah, ds,
1345 mrr_rate[0], mrr_tries[0],
1346 mrr_rate[1], mrr_tries[1],
1347 mrr_rate[2], mrr_tries[2]);
1348
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001349 ds->ds_link = 0;
1350 ds->ds_data = bf->skbaddr;
1351
1352 spin_lock_bh(&txq->lock);
1353 list_add_tail(&bf->list, &txq->q);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001354 if (txq->link == NULL) /* is this first packet? */
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001355 ath5k_hw_set_txdp(ah, txq->qnum, bf->daddr);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001356 else /* no, so only link it */
1357 *txq->link = bf->daddr;
1358
1359 txq->link = &ds->ds_link;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001360 ath5k_hw_start_tx_dma(ah, txq->qnum);
Jiri Slaby274c7c32008-07-15 17:44:20 +02001361 mmiowb();
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001362 spin_unlock_bh(&txq->lock);
1363
1364 return 0;
1365err_unmap:
1366 pci_unmap_single(sc->pdev, bf->skbaddr, skb->len, PCI_DMA_TODEVICE);
1367 return ret;
1368}
1369
1370/*******************\
1371* Descriptors setup *
1372\*******************/
1373
1374static int
1375ath5k_desc_alloc(struct ath5k_softc *sc, struct pci_dev *pdev)
1376{
1377 struct ath5k_desc *ds;
1378 struct ath5k_buf *bf;
1379 dma_addr_t da;
1380 unsigned int i;
1381 int ret;
1382
1383 /* allocate descriptors */
1384 sc->desc_len = sizeof(struct ath5k_desc) *
1385 (ATH_TXBUF + ATH_RXBUF + ATH_BCBUF + 1);
1386 sc->desc = pci_alloc_consistent(pdev, sc->desc_len, &sc->desc_daddr);
1387 if (sc->desc == NULL) {
1388 ATH5K_ERR(sc, "can't allocate descriptors\n");
1389 ret = -ENOMEM;
1390 goto err;
1391 }
1392 ds = sc->desc;
1393 da = sc->desc_daddr;
1394 ATH5K_DBG(sc, ATH5K_DEBUG_ANY, "DMA map: %p (%zu) -> %llx\n",
1395 ds, sc->desc_len, (unsigned long long)sc->desc_daddr);
1396
1397 bf = kcalloc(1 + ATH_TXBUF + ATH_RXBUF + ATH_BCBUF,
1398 sizeof(struct ath5k_buf), GFP_KERNEL);
1399 if (bf == NULL) {
1400 ATH5K_ERR(sc, "can't allocate bufptr\n");
1401 ret = -ENOMEM;
1402 goto err_free;
1403 }
1404 sc->bufptr = bf;
1405
1406 INIT_LIST_HEAD(&sc->rxbuf);
1407 for (i = 0; i < ATH_RXBUF; i++, bf++, ds++, da += sizeof(*ds)) {
1408 bf->desc = ds;
1409 bf->daddr = da;
1410 list_add_tail(&bf->list, &sc->rxbuf);
1411 }
1412
1413 INIT_LIST_HEAD(&sc->txbuf);
1414 sc->txbuf_len = ATH_TXBUF;
1415 for (i = 0; i < ATH_TXBUF; i++, bf++, ds++,
1416 da += sizeof(*ds)) {
1417 bf->desc = ds;
1418 bf->daddr = da;
1419 list_add_tail(&bf->list, &sc->txbuf);
1420 }
1421
1422 /* beacon buffer */
1423 bf->desc = ds;
1424 bf->daddr = da;
1425 sc->bbuf = bf;
1426
1427 return 0;
1428err_free:
1429 pci_free_consistent(pdev, sc->desc_len, sc->desc, sc->desc_daddr);
1430err:
1431 sc->desc = NULL;
1432 return ret;
1433}
1434
1435static void
1436ath5k_desc_free(struct ath5k_softc *sc, struct pci_dev *pdev)
1437{
1438 struct ath5k_buf *bf;
1439
1440 ath5k_txbuf_free(sc, sc->bbuf);
1441 list_for_each_entry(bf, &sc->txbuf, list)
1442 ath5k_txbuf_free(sc, bf);
1443 list_for_each_entry(bf, &sc->rxbuf, list)
Felix Fietkaua6c8d372009-01-30 01:36:48 +01001444 ath5k_rxbuf_free(sc, bf);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001445
1446 /* Free memory associated with all descriptors */
1447 pci_free_consistent(pdev, sc->desc_len, sc->desc, sc->desc_daddr);
1448
1449 kfree(sc->bufptr);
1450 sc->bufptr = NULL;
1451}
1452
1453
1454
1455
1456
1457/**************\
1458* Queues setup *
1459\**************/
1460
1461static struct ath5k_txq *
1462ath5k_txq_setup(struct ath5k_softc *sc,
1463 int qtype, int subtype)
1464{
1465 struct ath5k_hw *ah = sc->ah;
1466 struct ath5k_txq *txq;
1467 struct ath5k_txq_info qi = {
1468 .tqi_subtype = subtype,
1469 .tqi_aifs = AR5K_TXQ_USEDEFAULT,
1470 .tqi_cw_min = AR5K_TXQ_USEDEFAULT,
1471 .tqi_cw_max = AR5K_TXQ_USEDEFAULT
1472 };
1473 int qnum;
1474
1475 /*
1476 * Enable interrupts only for EOL and DESC conditions.
1477 * We mark tx descriptors to receive a DESC interrupt
1478 * when a tx queue gets deep; otherwise waiting for the
1479 * EOL to reap descriptors. Note that this is done to
1480 * reduce interrupt load and this only defers reaping
1481 * descriptors, never transmitting frames. Aside from
1482 * reducing interrupts this also permits more concurrency.
1483 * The only potential downside is if the tx queue backs
1484 * up in which case the top half of the kernel may backup
1485 * due to a lack of tx descriptors.
1486 */
1487 qi.tqi_flags = AR5K_TXQ_FLAG_TXEOLINT_ENABLE |
1488 AR5K_TXQ_FLAG_TXDESCINT_ENABLE;
1489 qnum = ath5k_hw_setup_tx_queue(ah, qtype, &qi);
1490 if (qnum < 0) {
1491 /*
1492 * NB: don't print a message, this happens
1493 * normally on parts with too few tx queues
1494 */
1495 return ERR_PTR(qnum);
1496 }
1497 if (qnum >= ARRAY_SIZE(sc->txqs)) {
1498 ATH5K_ERR(sc, "hw qnum %u out of range, max %tu!\n",
1499 qnum, ARRAY_SIZE(sc->txqs));
1500 ath5k_hw_release_tx_queue(ah, qnum);
1501 return ERR_PTR(-EINVAL);
1502 }
1503 txq = &sc->txqs[qnum];
1504 if (!txq->setup) {
1505 txq->qnum = qnum;
1506 txq->link = NULL;
1507 INIT_LIST_HEAD(&txq->q);
1508 spin_lock_init(&txq->lock);
1509 txq->setup = true;
1510 }
1511 return &sc->txqs[qnum];
1512}
1513
1514static int
1515ath5k_beaconq_setup(struct ath5k_hw *ah)
1516{
1517 struct ath5k_txq_info qi = {
1518 .tqi_aifs = AR5K_TXQ_USEDEFAULT,
1519 .tqi_cw_min = AR5K_TXQ_USEDEFAULT,
1520 .tqi_cw_max = AR5K_TXQ_USEDEFAULT,
1521 /* NB: for dynamic turbo, don't enable any other interrupts */
1522 .tqi_flags = AR5K_TXQ_FLAG_TXDESCINT_ENABLE
1523 };
1524
1525 return ath5k_hw_setup_tx_queue(ah, AR5K_TX_QUEUE_BEACON, &qi);
1526}
1527
1528static int
1529ath5k_beaconq_config(struct ath5k_softc *sc)
1530{
1531 struct ath5k_hw *ah = sc->ah;
1532 struct ath5k_txq_info qi;
1533 int ret;
1534
1535 ret = ath5k_hw_get_tx_queueprops(ah, sc->bhalq, &qi);
1536 if (ret)
Bob Copelanda951ae22010-01-20 23:51:04 -05001537 goto err;
1538
Johannes Berg05c914f2008-09-11 00:01:58 +02001539 if (sc->opmode == NL80211_IFTYPE_AP ||
1540 sc->opmode == NL80211_IFTYPE_MESH_POINT) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001541 /*
1542 * Always burst out beacon and CAB traffic
1543 * (aifs = cwmin = cwmax = 0)
1544 */
1545 qi.tqi_aifs = 0;
1546 qi.tqi_cw_min = 0;
1547 qi.tqi_cw_max = 0;
Johannes Berg05c914f2008-09-11 00:01:58 +02001548 } else if (sc->opmode == NL80211_IFTYPE_ADHOC) {
Bruno Randolf6d91e1d2008-01-19 18:18:41 +09001549 /*
1550 * Adhoc mode; backoff between 0 and (2 * cw_min).
1551 */
1552 qi.tqi_aifs = 0;
1553 qi.tqi_cw_min = 0;
1554 qi.tqi_cw_max = 2 * ah->ah_cw_min;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001555 }
1556
Bruno Randolf6d91e1d2008-01-19 18:18:41 +09001557 ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
1558 "beacon queueprops tqi_aifs:%d tqi_cw_min:%d tqi_cw_max:%d\n",
1559 qi.tqi_aifs, qi.tqi_cw_min, qi.tqi_cw_max);
1560
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001561 ret = ath5k_hw_set_tx_queueprops(ah, sc->bhalq, &qi);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001562 if (ret) {
1563 ATH5K_ERR(sc, "%s: unable to update parameters for beacon "
1564 "hardware queue!\n", __func__);
Bob Copelanda951ae22010-01-20 23:51:04 -05001565 goto err;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001566 }
Bob Copelanda951ae22010-01-20 23:51:04 -05001567 ret = ath5k_hw_reset_tx_queue(ah, sc->bhalq); /* push to h/w */
1568 if (ret)
1569 goto err;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001570
Bob Copelanda951ae22010-01-20 23:51:04 -05001571 /* reconfigure cabq with ready time to 80% of beacon_interval */
1572 ret = ath5k_hw_get_tx_queueprops(ah, AR5K_TX_QUEUE_ID_CAB, &qi);
1573 if (ret)
1574 goto err;
1575
1576 qi.tqi_ready_time = (sc->bintval * 80) / 100;
1577 ret = ath5k_hw_set_tx_queueprops(ah, AR5K_TX_QUEUE_ID_CAB, &qi);
1578 if (ret)
1579 goto err;
1580
1581 ret = ath5k_hw_reset_tx_queue(ah, AR5K_TX_QUEUE_ID_CAB);
1582err:
1583 return ret;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001584}
1585
1586static void
1587ath5k_txq_drainq(struct ath5k_softc *sc, struct ath5k_txq *txq)
1588{
1589 struct ath5k_buf *bf, *bf0;
1590
1591 /*
1592 * NB: this assumes output has been stopped and
1593 * we do not need to block ath5k_tx_tasklet
1594 */
1595 spin_lock_bh(&txq->lock);
1596 list_for_each_entry_safe(bf, bf0, &txq->q, list) {
Bruno Randolfb47f4072008-03-05 18:35:45 +09001597 ath5k_debug_printtxbuf(sc, bf);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001598
1599 ath5k_txbuf_free(sc, bf);
1600
1601 spin_lock_bh(&sc->txbuflock);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001602 list_move_tail(&bf->list, &sc->txbuf);
1603 sc->txbuf_len++;
1604 spin_unlock_bh(&sc->txbuflock);
1605 }
1606 txq->link = NULL;
1607 spin_unlock_bh(&txq->lock);
1608}
1609
1610/*
1611 * Drain the transmit queues and reclaim resources.
1612 */
1613static void
1614ath5k_txq_cleanup(struct ath5k_softc *sc)
1615{
1616 struct ath5k_hw *ah = sc->ah;
1617 unsigned int i;
1618
1619 /* XXX return value */
1620 if (likely(!test_bit(ATH_STAT_INVALID, sc->status))) {
1621 /* don't touch the hardware if marked invalid */
1622 ath5k_hw_stop_tx_dma(ah, sc->bhalq);
1623 ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "beacon queue %x\n",
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001624 ath5k_hw_get_txdp(ah, sc->bhalq));
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001625 for (i = 0; i < ARRAY_SIZE(sc->txqs); i++)
1626 if (sc->txqs[i].setup) {
1627 ath5k_hw_stop_tx_dma(ah, sc->txqs[i].qnum);
1628 ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "txq [%u] %x, "
1629 "link %p\n",
1630 sc->txqs[i].qnum,
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001631 ath5k_hw_get_txdp(ah,
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001632 sc->txqs[i].qnum),
1633 sc->txqs[i].link);
1634 }
1635 }
Johannes Berg36d68252008-05-15 12:55:26 +02001636 ieee80211_wake_queues(sc->hw); /* XXX move to callers */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001637
1638 for (i = 0; i < ARRAY_SIZE(sc->txqs); i++)
1639 if (sc->txqs[i].setup)
1640 ath5k_txq_drainq(sc, &sc->txqs[i]);
1641}
1642
1643static void
1644ath5k_txq_release(struct ath5k_softc *sc)
1645{
1646 struct ath5k_txq *txq = sc->txqs;
1647 unsigned int i;
1648
1649 for (i = 0; i < ARRAY_SIZE(sc->txqs); i++, txq++)
1650 if (txq->setup) {
1651 ath5k_hw_release_tx_queue(sc->ah, txq->qnum);
1652 txq->setup = false;
1653 }
1654}
1655
1656
1657
1658
1659/*************\
1660* RX Handling *
1661\*************/
1662
1663/*
1664 * Enable the receive h/w following a reset.
1665 */
1666static int
1667ath5k_rx_start(struct ath5k_softc *sc)
1668{
1669 struct ath5k_hw *ah = sc->ah;
Luis R. Rodriguezdb719712009-09-10 11:20:57 -07001670 struct ath_common *common = ath5k_hw_common(ah);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001671 struct ath5k_buf *bf;
1672 int ret;
1673
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -08001674 common->rx_bufsize = roundup(IEEE80211_MAX_LEN, common->cachelsz);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001675
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -08001676 ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "cachelsz %u rx_bufsize %u\n",
1677 common->cachelsz, common->rx_bufsize);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001678
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001679 spin_lock_bh(&sc->rxbuflock);
Bob Copeland26925042009-04-15 07:57:36 -04001680 sc->rxlink = NULL;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001681 list_for_each_entry(bf, &sc->rxbuf, list) {
1682 ret = ath5k_rxbuf_setup(sc, bf);
1683 if (ret != 0) {
1684 spin_unlock_bh(&sc->rxbuflock);
1685 goto err;
1686 }
1687 }
1688 bf = list_first_entry(&sc->rxbuf, struct ath5k_buf, list);
Bob Copeland26925042009-04-15 07:57:36 -04001689 ath5k_hw_set_rxdp(ah, bf->daddr);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001690 spin_unlock_bh(&sc->rxbuflock);
1691
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001692 ath5k_hw_start_rx_dma(ah); /* enable recv descriptors */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001693 ath5k_mode_setup(sc); /* set filters, etc. */
1694 ath5k_hw_start_rx_pcu(ah); /* re-enable PCU/DMA engine */
1695
1696 return 0;
1697err:
1698 return ret;
1699}
1700
1701/*
1702 * Disable the receive h/w in preparation for a reset.
1703 */
1704static void
1705ath5k_rx_stop(struct ath5k_softc *sc)
1706{
1707 struct ath5k_hw *ah = sc->ah;
1708
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001709 ath5k_hw_stop_rx_pcu(ah); /* disable PCU */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001710 ath5k_hw_set_rx_filter(ah, 0); /* clear recv filter */
1711 ath5k_hw_stop_rx_dma(ah); /* disable DMA engine */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001712
1713 ath5k_debug_printrxbuffs(sc, ah);
1714
1715 sc->rxlink = NULL; /* just in case */
1716}
1717
1718static unsigned int
1719ath5k_rx_decrypted(struct ath5k_softc *sc, struct ath5k_desc *ds,
Bruno Randolfb47f4072008-03-05 18:35:45 +09001720 struct sk_buff *skb, struct ath5k_rx_status *rs)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001721{
Luis R. Rodriguezdc1e0012009-11-04 17:47:31 -08001722 struct ath5k_hw *ah = sc->ah;
1723 struct ath_common *common = ath5k_hw_common(ah);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001724 struct ieee80211_hdr *hdr = (void *)skb->data;
Harvey Harrison798ee982008-07-15 18:44:02 -07001725 unsigned int keyix, hlen;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001726
Bruno Randolfb47f4072008-03-05 18:35:45 +09001727 if (!(rs->rs_status & AR5K_RXERR_DECRYPT) &&
1728 rs->rs_keyix != AR5K_RXKEYIX_INVALID)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001729 return RX_FLAG_DECRYPTED;
1730
1731 /* Apparently when a default key is used to decrypt the packet
1732 the hw does not set the index used to decrypt. In such cases
1733 get the index from the packet. */
Harvey Harrison798ee982008-07-15 18:44:02 -07001734 hlen = ieee80211_hdrlen(hdr->frame_control);
Harvey Harrison24b56e72008-06-14 23:33:38 -07001735 if (ieee80211_has_protected(hdr->frame_control) &&
1736 !(rs->rs_status & AR5K_RXERR_DECRYPT) &&
1737 skb->len >= hlen + 4) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001738 keyix = skb->data[hlen + 3] >> 6;
1739
Luis R. Rodriguezdc1e0012009-11-04 17:47:31 -08001740 if (test_bit(keyix, common->keymap))
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001741 return RX_FLAG_DECRYPTED;
1742 }
1743
1744 return 0;
1745}
1746
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001747
1748static void
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001749ath5k_check_ibss_tsf(struct ath5k_softc *sc, struct sk_buff *skb,
1750 struct ieee80211_rx_status *rxs)
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001751{
Luis R. Rodriguez954fece2009-09-10 10:51:33 -07001752 struct ath_common *common = ath5k_hw_common(sc->ah);
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001753 u64 tsf, bc_tstamp;
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001754 u32 hw_tu;
1755 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)skb->data;
1756
Harvey Harrison24b56e72008-06-14 23:33:38 -07001757 if (ieee80211_is_beacon(mgmt->frame_control) &&
Pavel Roskin38c07b42008-02-26 17:59:14 -05001758 le16_to_cpu(mgmt->u.beacon.capab_info) & WLAN_CAPABILITY_IBSS &&
Luis R. Rodriguez954fece2009-09-10 10:51:33 -07001759 memcmp(mgmt->bssid, common->curbssid, ETH_ALEN) == 0) {
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001760 /*
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001761 * Received an IBSS beacon with the same BSSID. Hardware *must*
1762 * have updated the local TSF. We have to work around various
1763 * hardware bugs, though...
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001764 */
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001765 tsf = ath5k_hw_get_tsf64(sc->ah);
1766 bc_tstamp = le64_to_cpu(mgmt->u.beacon.timestamp);
1767 hw_tu = TSF_TO_TU(tsf);
1768
1769 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
1770 "beacon %llx mactime %llx (diff %lld) tsf now %llx\n",
John W. Linville06501d22008-04-01 17:38:47 -04001771 (unsigned long long)bc_tstamp,
1772 (unsigned long long)rxs->mactime,
1773 (unsigned long long)(rxs->mactime - bc_tstamp),
1774 (unsigned long long)tsf);
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001775
1776 /*
1777 * Sometimes the HW will give us a wrong tstamp in the rx
1778 * status, causing the timestamp extension to go wrong.
1779 * (This seems to happen especially with beacon frames bigger
1780 * than 78 byte (incl. FCS))
1781 * But we know that the receive timestamp must be later than the
1782 * timestamp of the beacon since HW must have synced to that.
1783 *
1784 * NOTE: here we assume mactime to be after the frame was
1785 * received, not like mac80211 which defines it at the start.
1786 */
1787 if (bc_tstamp > rxs->mactime) {
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001788 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001789 "fixing mactime from %llx to %llx\n",
John W. Linville06501d22008-04-01 17:38:47 -04001790 (unsigned long long)rxs->mactime,
1791 (unsigned long long)tsf);
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001792 rxs->mactime = tsf;
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001793 }
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001794
1795 /*
1796 * Local TSF might have moved higher than our beacon timers,
1797 * in that case we have to update them to continue sending
1798 * beacons. This also takes care of synchronizing beacon sending
1799 * times with other stations.
1800 */
1801 if (hw_tu >= sc->nexttbtt)
1802 ath5k_beacon_update_timers(sc, bc_tstamp);
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001803 }
1804}
1805
Bruno Randolfb4ea4492010-03-25 14:49:25 +09001806static void
1807ath5k_update_beacon_rssi(struct ath5k_softc *sc, struct sk_buff *skb, int rssi)
1808{
1809 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)skb->data;
1810 struct ath5k_hw *ah = sc->ah;
1811 struct ath_common *common = ath5k_hw_common(ah);
1812
1813 /* only beacons from our BSSID */
1814 if (!ieee80211_is_beacon(mgmt->frame_control) ||
1815 memcmp(mgmt->bssid, common->curbssid, ETH_ALEN) != 0)
1816 return;
1817
1818 ah->ah_beacon_rssi_avg = ath5k_moving_average(ah->ah_beacon_rssi_avg,
1819 rssi);
1820
1821 /* in IBSS mode we should keep RSSI statistics per neighbour */
1822 /* le16_to_cpu(mgmt->u.beacon.capab_info) & WLAN_CAPABILITY_IBSS */
1823}
1824
Benoit Papillault8127fbd2010-02-27 23:05:26 +01001825/*
1826 * Compute padding position. skb must contains an IEEE 802.11 frame
1827 */
1828static int ath5k_common_padpos(struct sk_buff *skb)
1829{
1830 struct ieee80211_hdr * hdr = (struct ieee80211_hdr *)skb->data;
1831 __le16 frame_control = hdr->frame_control;
1832 int padpos = 24;
1833
1834 if (ieee80211_has_a4(frame_control)) {
1835 padpos += ETH_ALEN;
1836 }
1837 if (ieee80211_is_data_qos(frame_control)) {
1838 padpos += IEEE80211_QOS_CTL_LEN;
1839 }
1840
1841 return padpos;
1842}
1843
1844/*
1845 * This function expects a 802.11 frame and returns the number of
1846 * bytes added, or -1 if we don't have enought header room.
1847 */
1848
1849static int ath5k_add_padding(struct sk_buff *skb)
1850{
1851 int padpos = ath5k_common_padpos(skb);
1852 int padsize = padpos & 3;
1853
1854 if (padsize && skb->len>padpos) {
1855
1856 if (skb_headroom(skb) < padsize)
1857 return -1;
1858
1859 skb_push(skb, padsize);
1860 memmove(skb->data, skb->data+padsize, padpos);
1861 return padsize;
1862 }
1863
1864 return 0;
1865}
1866
1867/*
1868 * This function expects a 802.11 frame and returns the number of
1869 * bytes removed
1870 */
1871
1872static int ath5k_remove_padding(struct sk_buff *skb)
1873{
1874 int padpos = ath5k_common_padpos(skb);
1875 int padsize = padpos & 3;
1876
1877 if (padsize && skb->len>=padpos+padsize) {
1878 memmove(skb->data + padsize, skb->data, padpos);
1879 skb_pull(skb, padsize);
1880 return padsize;
1881 }
1882
1883 return 0;
1884}
1885
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001886static void
1887ath5k_tasklet_rx(unsigned long data)
1888{
Bob Copeland1c5256b2009-08-24 23:00:32 -04001889 struct ieee80211_rx_status *rxs;
Bruno Randolfb47f4072008-03-05 18:35:45 +09001890 struct ath5k_rx_status rs = {};
Bob Copelandb6ea0352009-01-10 14:42:54 -05001891 struct sk_buff *skb, *next_skb;
1892 dma_addr_t next_skb_addr;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001893 struct ath5k_softc *sc = (void *)data;
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -08001894 struct ath5k_hw *ah = sc->ah;
1895 struct ath_common *common = ath5k_hw_common(ah);
Bob Copelandc57ca812009-04-15 07:57:35 -04001896 struct ath5k_buf *bf;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001897 struct ath5k_desc *ds;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001898 int ret;
Bob Copeland1c5256b2009-08-24 23:00:32 -04001899 int rx_flag;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001900
1901 spin_lock(&sc->rxbuflock);
Jiri Slaby3a0f2c82008-07-15 17:44:18 +02001902 if (list_empty(&sc->rxbuf)) {
1903 ATH5K_WARN(sc, "empty rx buf pool\n");
1904 goto unlock;
1905 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001906 do {
Bob Copeland1c5256b2009-08-24 23:00:32 -04001907 rx_flag = 0;
Bob Copelandd6894b52008-05-12 21:16:44 -04001908
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001909 bf = list_first_entry(&sc->rxbuf, struct ath5k_buf, list);
1910 BUG_ON(bf->skb == NULL);
1911 skb = bf->skb;
1912 ds = bf->desc;
1913
Bob Copelandc57ca812009-04-15 07:57:35 -04001914 /* bail if HW is still using self-linked descriptor */
1915 if (ath5k_hw_get_rxdp(sc->ah) == bf->daddr)
1916 break;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001917
Bruno Randolfb47f4072008-03-05 18:35:45 +09001918 ret = sc->ah->ah_proc_rx_desc(sc->ah, ds, &rs);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001919 if (unlikely(ret == -EINPROGRESS))
1920 break;
1921 else if (unlikely(ret)) {
1922 ATH5K_ERR(sc, "error in processing rx descriptor\n");
Bruno Randolf76443952010-03-09 16:56:00 +09001923 sc->stats.rxerr_proc++;
Jiri Slaby65872e62008-02-15 21:58:51 +01001924 spin_unlock(&sc->rxbuflock);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001925 return;
1926 }
1927
Bruno Randolf76443952010-03-09 16:56:00 +09001928 sc->stats.rx_all_count++;
1929
Bruno Randolfb47f4072008-03-05 18:35:45 +09001930 if (unlikely(rs.rs_more)) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001931 ATH5K_WARN(sc, "unsupported jumbo\n");
Bruno Randolf76443952010-03-09 16:56:00 +09001932 sc->stats.rxerr_jumbo++;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001933 goto next;
1934 }
1935
Bruno Randolfb47f4072008-03-05 18:35:45 +09001936 if (unlikely(rs.rs_status)) {
Bruno Randolf76443952010-03-09 16:56:00 +09001937 if (rs.rs_status & AR5K_RXERR_CRC)
1938 sc->stats.rxerr_crc++;
1939 if (rs.rs_status & AR5K_RXERR_FIFO)
1940 sc->stats.rxerr_fifo++;
1941 if (rs.rs_status & AR5K_RXERR_PHY) {
1942 sc->stats.rxerr_phy++;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001943 goto next;
Bruno Randolf76443952010-03-09 16:56:00 +09001944 }
Bruno Randolfb47f4072008-03-05 18:35:45 +09001945 if (rs.rs_status & AR5K_RXERR_DECRYPT) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001946 /*
1947 * Decrypt error. If the error occurred
1948 * because there was no hardware key, then
1949 * let the frame through so the upper layers
1950 * can process it. This is necessary for 5210
1951 * parts which have no way to setup a ``clear''
1952 * key cache entry.
1953 *
1954 * XXX do key cache faulting
1955 */
Bruno Randolf76443952010-03-09 16:56:00 +09001956 sc->stats.rxerr_decrypt++;
Bruno Randolfb47f4072008-03-05 18:35:45 +09001957 if (rs.rs_keyix == AR5K_RXKEYIX_INVALID &&
1958 !(rs.rs_status & AR5K_RXERR_CRC))
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001959 goto accept;
1960 }
Bruno Randolfb47f4072008-03-05 18:35:45 +09001961 if (rs.rs_status & AR5K_RXERR_MIC) {
Bob Copeland1c5256b2009-08-24 23:00:32 -04001962 rx_flag |= RX_FLAG_MMIC_ERROR;
Bruno Randolf76443952010-03-09 16:56:00 +09001963 sc->stats.rxerr_mic++;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001964 goto accept;
1965 }
1966
1967 /* let crypto-error packets fall through in MNTR */
Bruno Randolfb47f4072008-03-05 18:35:45 +09001968 if ((rs.rs_status &
1969 ~(AR5K_RXERR_DECRYPT|AR5K_RXERR_MIC)) ||
Johannes Berg05c914f2008-09-11 00:01:58 +02001970 sc->opmode != NL80211_IFTYPE_MONITOR)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001971 goto next;
1972 }
1973accept:
Bob Copelandb6ea0352009-01-10 14:42:54 -05001974 next_skb = ath5k_rx_skb_alloc(sc, &next_skb_addr);
1975
1976 /*
1977 * If we can't replace bf->skb with a new skb under memory
1978 * pressure, just skip this packet
1979 */
1980 if (!next_skb)
1981 goto next;
1982
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -08001983 pci_unmap_single(sc->pdev, bf->skbaddr, common->rx_bufsize,
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001984 PCI_DMA_FROMDEVICE);
Bruno Randolfb47f4072008-03-05 18:35:45 +09001985 skb_put(skb, rs.rs_datalen);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001986
Benoit PAPILLAULT0fe45b12008-12-12 15:29:58 +01001987 /* The MAC header is padded to have 32-bit boundary if the
1988 * packet payload is non-zero. The general calculation for
1989 * padsize would take into account odd header lengths:
1990 * padsize = (4 - hdrlen % 4) % 4; However, since only
1991 * even-length headers are used, padding can only be 0 or 2
1992 * bytes and we can optimize this a bit. In addition, we must
1993 * not try to remove padding from short control frames that do
1994 * not have payload. */
Benoit Papillault8127fbd2010-02-27 23:05:26 +01001995 ath5k_remove_padding(skb);
1996
Bob Copeland1c5256b2009-08-24 23:00:32 -04001997 rxs = IEEE80211_SKB_RXCB(skb);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001998
Bruno Randolfc0e18992008-01-21 11:09:46 +09001999 /*
2000 * always extend the mac timestamp, since this information is
2001 * also needed for proper IBSS merging.
2002 *
2003 * XXX: it might be too late to do it here, since rs_tstamp is
2004 * 15bit only. that means TSF extension has to be done within
2005 * 32768usec (about 32ms). it might be necessary to move this to
2006 * the interrupt handler, like it is done in madwifi.
Bruno Randolfe14296c2008-03-05 18:36:05 +09002007 *
2008 * Unfortunately we don't know when the hardware takes the rx
2009 * timestamp (beginning of phy frame, data frame, end of rx?).
2010 * The only thing we know is that it is hardware specific...
2011 * On AR5213 it seems the rx timestamp is at the end of the
2012 * frame, but i'm not sure.
2013 *
2014 * NOTE: mac80211 defines mactime at the beginning of the first
2015 * data symbol. Since we don't have any time references it's
2016 * impossible to comply to that. This affects IBSS merge only
2017 * right now, so it's not too bad...
Bruno Randolfc0e18992008-01-21 11:09:46 +09002018 */
Bob Copeland1c5256b2009-08-24 23:00:32 -04002019 rxs->mactime = ath5k_extend_tsf(sc->ah, rs.rs_tstamp);
2020 rxs->flag = rx_flag | RX_FLAG_TSFT;
Bruno Randolfc0e18992008-01-21 11:09:46 +09002021
Bob Copeland1c5256b2009-08-24 23:00:32 -04002022 rxs->freq = sc->curchan->center_freq;
2023 rxs->band = sc->curband->band;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002024
Bob Copeland1c5256b2009-08-24 23:00:32 -04002025 rxs->noise = sc->ah->ah_noise_floor;
2026 rxs->signal = rxs->noise + rs.rs_rssi;
Luis R. Rodriguez6e0e0bf2008-10-13 14:08:10 -07002027
Bob Copeland1c5256b2009-08-24 23:00:32 -04002028 rxs->antenna = rs.rs_antenna;
Bruno Randolf604eead2010-03-09 16:55:17 +09002029
2030 if (rs.rs_antenna > 0 && rs.rs_antenna < 5)
2031 sc->stats.antenna_rx[rs.rs_antenna]++;
2032 else
2033 sc->stats.antenna_rx[0]++; /* invalid */
2034
Bob Copeland1c5256b2009-08-24 23:00:32 -04002035 rxs->rate_idx = ath5k_hw_to_driver_rix(sc, rs.rs_rate);
2036 rxs->flag |= ath5k_rx_decrypted(sc, ds, skb, &rs);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002037
Bob Copeland1c5256b2009-08-24 23:00:32 -04002038 if (rxs->rate_idx >= 0 && rs.rs_rate ==
2039 sc->curband->bitrates[rxs->rate_idx].hw_value_short)
2040 rxs->flag |= RX_FLAG_SHORTPRE;
Bruno Randolf06303352008-08-05 19:32:23 +02002041
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002042 ath5k_debug_dump_skb(sc, skb, "RX ", 0);
2043
Bruno Randolfb4ea4492010-03-25 14:49:25 +09002044 ath5k_update_beacon_rssi(sc, skb, rs.rs_rssi);
2045
Bruno Randolf036cd1e2008-01-19 18:18:21 +09002046 /* check beacons in IBSS mode */
Johannes Berg05c914f2008-09-11 00:01:58 +02002047 if (sc->opmode == NL80211_IFTYPE_ADHOC)
Bob Copeland1c5256b2009-08-24 23:00:32 -04002048 ath5k_check_ibss_tsf(sc, skb, rxs);
Bruno Randolf036cd1e2008-01-19 18:18:21 +09002049
Johannes Bergf1d58c22009-06-17 13:13:00 +02002050 ieee80211_rx(sc->hw, skb);
Bob Copelandb6ea0352009-01-10 14:42:54 -05002051
2052 bf->skb = next_skb;
2053 bf->skbaddr = next_skb_addr;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002054next:
2055 list_move_tail(&bf->list, &sc->rxbuf);
2056 } while (ath5k_rxbuf_setup(sc, bf) == 0);
Jiri Slaby3a0f2c82008-07-15 17:44:18 +02002057unlock:
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002058 spin_unlock(&sc->rxbuflock);
2059}
2060
2061
2062
2063
2064/*************\
2065* TX Handling *
2066\*************/
2067
2068static void
2069ath5k_tx_processq(struct ath5k_softc *sc, struct ath5k_txq *txq)
2070{
Bruno Randolfb47f4072008-03-05 18:35:45 +09002071 struct ath5k_tx_status ts = {};
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002072 struct ath5k_buf *bf, *bf0;
2073 struct ath5k_desc *ds;
2074 struct sk_buff *skb;
Johannes Berge039fa42008-05-15 12:55:29 +02002075 struct ieee80211_tx_info *info;
Felix Fietkau2f7fe872008-10-05 18:05:48 +02002076 int i, ret;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002077
2078 spin_lock(&txq->lock);
2079 list_for_each_entry_safe(bf, bf0, &txq->q, list) {
2080 ds = bf->desc;
2081
Bruno Randolfb47f4072008-03-05 18:35:45 +09002082 ret = sc->ah->ah_proc_tx_desc(sc->ah, ds, &ts);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002083 if (unlikely(ret == -EINPROGRESS))
2084 break;
2085 else if (unlikely(ret)) {
2086 ATH5K_ERR(sc, "error %d while processing queue %u\n",
2087 ret, txq->qnum);
2088 break;
2089 }
2090
Bruno Randolf76443952010-03-09 16:56:00 +09002091 sc->stats.tx_all_count++;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002092 skb = bf->skb;
Johannes Berga888d522008-05-26 16:43:39 +02002093 info = IEEE80211_SKB_CB(skb);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002094 bf->skb = NULL;
Johannes Berge039fa42008-05-15 12:55:29 +02002095
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002096 pci_unmap_single(sc->pdev, bf->skbaddr, skb->len,
2097 PCI_DMA_TODEVICE);
2098
Johannes Berge6a98542008-10-21 12:40:02 +02002099 ieee80211_tx_info_clear_status(info);
Felix Fietkau2f7fe872008-10-05 18:05:48 +02002100 for (i = 0; i < 4; i++) {
Johannes Berge6a98542008-10-21 12:40:02 +02002101 struct ieee80211_tx_rate *r =
2102 &info->status.rates[i];
Felix Fietkau2f7fe872008-10-05 18:05:48 +02002103
2104 if (ts.ts_rate[i]) {
Johannes Berge6a98542008-10-21 12:40:02 +02002105 r->idx = ath5k_hw_to_driver_rix(sc, ts.ts_rate[i]);
2106 r->count = ts.ts_retry[i];
Felix Fietkau2f7fe872008-10-05 18:05:48 +02002107 } else {
Johannes Berge6a98542008-10-21 12:40:02 +02002108 r->idx = -1;
2109 r->count = 0;
Felix Fietkau2f7fe872008-10-05 18:05:48 +02002110 }
2111 }
2112
Johannes Berge6a98542008-10-21 12:40:02 +02002113 /* count the successful attempt as well */
2114 info->status.rates[ts.ts_final_idx].count++;
2115
Bruno Randolfb47f4072008-03-05 18:35:45 +09002116 if (unlikely(ts.ts_status)) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002117 sc->ll_stats.dot11ACKFailureCount++;
Bruno Randolf76443952010-03-09 16:56:00 +09002118 if (ts.ts_status & AR5K_TXERR_FILT) {
Johannes Berge039fa42008-05-15 12:55:29 +02002119 info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
Bruno Randolf76443952010-03-09 16:56:00 +09002120 sc->stats.txerr_filt++;
2121 }
2122 if (ts.ts_status & AR5K_TXERR_XRETRY)
2123 sc->stats.txerr_retry++;
2124 if (ts.ts_status & AR5K_TXERR_FIFO)
2125 sc->stats.txerr_fifo++;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002126 } else {
Johannes Berge039fa42008-05-15 12:55:29 +02002127 info->flags |= IEEE80211_TX_STAT_ACK;
2128 info->status.ack_signal = ts.ts_rssi;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002129 }
2130
Benoit Papillault8127fbd2010-02-27 23:05:26 +01002131 /*
2132 * Remove MAC header padding before giving the frame
2133 * back to mac80211.
2134 */
2135 ath5k_remove_padding(skb);
2136
Bruno Randolf604eead2010-03-09 16:55:17 +09002137 if (ts.ts_antenna > 0 && ts.ts_antenna < 5)
2138 sc->stats.antenna_tx[ts.ts_antenna]++;
2139 else
2140 sc->stats.antenna_tx[0]++; /* invalid */
2141
Johannes Berge039fa42008-05-15 12:55:29 +02002142 ieee80211_tx_status(sc->hw, skb);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002143
2144 spin_lock(&sc->txbuflock);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002145 list_move_tail(&bf->list, &sc->txbuf);
2146 sc->txbuf_len++;
2147 spin_unlock(&sc->txbuflock);
2148 }
2149 if (likely(list_empty(&txq->q)))
2150 txq->link = NULL;
2151 spin_unlock(&txq->lock);
2152 if (sc->txbuf_len > ATH_TXBUF / 5)
2153 ieee80211_wake_queues(sc->hw);
2154}
2155
2156static void
2157ath5k_tasklet_tx(unsigned long data)
2158{
Bob Copeland8784d2e2009-07-29 17:32:28 -04002159 int i;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002160 struct ath5k_softc *sc = (void *)data;
2161
Bob Copeland8784d2e2009-07-29 17:32:28 -04002162 for (i=0; i < AR5K_NUM_TX_QUEUES; i++)
2163 if (sc->txqs[i].setup && (sc->ah->ah_txq_isr & BIT(i)))
2164 ath5k_tx_processq(sc, &sc->txqs[i]);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002165}
2166
2167
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002168/*****************\
2169* Beacon handling *
2170\*****************/
2171
2172/*
2173 * Setup the beacon frame for transmit.
2174 */
2175static int
Johannes Berge039fa42008-05-15 12:55:29 +02002176ath5k_beacon_setup(struct ath5k_softc *sc, struct ath5k_buf *bf)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002177{
2178 struct sk_buff *skb = bf->skb;
Johannes Berga888d522008-05-26 16:43:39 +02002179 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002180 struct ath5k_hw *ah = sc->ah;
2181 struct ath5k_desc *ds;
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04002182 int ret = 0;
2183 u8 antenna;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002184 u32 flags;
Benoit Papillault8127fbd2010-02-27 23:05:26 +01002185 const int padsize = 0;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002186
2187 bf->skbaddr = pci_map_single(sc->pdev, skb->data, skb->len,
2188 PCI_DMA_TODEVICE);
2189 ATH5K_DBG(sc, ATH5K_DEBUG_BEACON, "skb %p [data %p len %u] "
2190 "skbaddr %llx\n", skb, skb->data, skb->len,
2191 (unsigned long long)bf->skbaddr);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -07002192 if (pci_dma_mapping_error(sc->pdev, bf->skbaddr)) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002193 ATH5K_ERR(sc, "beacon DMA mapping failed\n");
2194 return -EIO;
2195 }
2196
2197 ds = bf->desc;
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04002198 antenna = ah->ah_tx_ant;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002199
2200 flags = AR5K_TXDESC_NOACK;
Johannes Berg05c914f2008-09-11 00:01:58 +02002201 if (sc->opmode == NL80211_IFTYPE_ADHOC && ath5k_hw_hasveol(ah)) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002202 ds->ds_link = bf->daddr; /* self-linked */
2203 flags |= AR5K_TXDESC_VEOL;
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04002204 } else
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002205 ds->ds_link = 0;
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04002206
2207 /*
2208 * If we use multiple antennas on AP and use
2209 * the Sectored AP scenario, switch antenna every
2210 * 4 beacons to make sure everybody hears our AP.
2211 * When a client tries to associate, hw will keep
2212 * track of the tx antenna to be used for this client
2213 * automaticaly, based on ACKed packets.
2214 *
2215 * Note: AP still listens and transmits RTS on the
2216 * default antenna which is supposed to be an omni.
2217 *
2218 * Note2: On sectored scenarios it's possible to have
2219 * multiple antennas (1omni -the default- and 14 sectors)
2220 * so if we choose to actually support this mode we need
2221 * to allow user to set how many antennas we have and tweak
2222 * the code below to send beacons on all of them.
2223 */
2224 if (ah->ah_ant_mode == AR5K_ANTMODE_SECTOR_AP)
2225 antenna = sc->bsent & 4 ? 2 : 1;
2226
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002227
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02002228 /* FIXME: If we are in g mode and rate is a CCK rate
2229 * subtract ah->ah_txpower.txp_cck_ofdm_pwr_delta
2230 * from tx power (value is in dB units already) */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002231 ds->ds_data = bf->skbaddr;
Bruno Randolf281c56d2008-02-05 18:44:55 +09002232 ret = ah->ah_setup_tx_desc(ah, ds, skb->len,
Benoit Papillault8127fbd2010-02-27 23:05:26 +01002233 ieee80211_get_hdrlen_from_skb(skb), padsize,
Luis R. Rodriguez400ec452008-02-03 21:51:49 -05002234 AR5K_PKT_TYPE_BEACON, (sc->power_level * 2),
Johannes Berge039fa42008-05-15 12:55:29 +02002235 ieee80211_get_tx_rate(sc->hw, info)->hw_value,
Johannes Berg2e92e6f2008-05-15 12:55:27 +02002236 1, AR5K_TXKEYIX_INVALID,
Luis R. Rodriguez400ec452008-02-03 21:51:49 -05002237 antenna, flags, 0, 0);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002238 if (ret)
2239 goto err_unmap;
2240
2241 return 0;
2242err_unmap:
2243 pci_unmap_single(sc->pdev, bf->skbaddr, skb->len, PCI_DMA_TODEVICE);
2244 return ret;
2245}
2246
2247/*
2248 * Transmit a beacon frame at SWBA. Dynamic updates to the
2249 * frame contents are done as needed and the slot time is
2250 * also adjusted based on current state.
2251 *
Bob Copelandacf3c1a2009-02-15 12:06:11 -05002252 * This is called from software irq context (beacontq or restq
2253 * tasklets) or user context from ath5k_beacon_config.
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002254 */
2255static void
2256ath5k_beacon_send(struct ath5k_softc *sc)
2257{
2258 struct ath5k_buf *bf = sc->bbuf;
2259 struct ath5k_hw *ah = sc->ah;
Bob Copelandcec8db22009-07-04 12:59:51 -04002260 struct sk_buff *skb;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002261
Bruno Randolfbe9b7252008-01-23 10:27:51 +09002262 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON, "in beacon_send\n");
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002263
Johannes Berg05c914f2008-09-11 00:01:58 +02002264 if (unlikely(bf->skb == NULL || sc->opmode == NL80211_IFTYPE_STATION ||
2265 sc->opmode == NL80211_IFTYPE_MONITOR)) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002266 ATH5K_WARN(sc, "bf=%p bf_skb=%p\n", bf, bf ? bf->skb : NULL);
2267 return;
2268 }
2269 /*
2270 * Check if the previous beacon has gone out. If
2271 * not don't don't try to post another, skip this
2272 * period and wait for the next. Missed beacons
2273 * indicate a problem and should not occur. If we
2274 * miss too many consecutive beacons reset the device.
2275 */
2276 if (unlikely(ath5k_hw_num_tx_pending(ah, sc->bhalq) != 0)) {
2277 sc->bmisscount++;
Bruno Randolfbe9b7252008-01-23 10:27:51 +09002278 ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002279 "missed %u consecutive beacons\n", sc->bmisscount);
Nick Kossifidis428cbd42009-04-30 15:55:47 -04002280 if (sc->bmisscount > 10) { /* NB: 10 is a guess */
Bruno Randolfbe9b7252008-01-23 10:27:51 +09002281 ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002282 "stuck beacon time (%u missed)\n",
2283 sc->bmisscount);
2284 tasklet_schedule(&sc->restq);
2285 }
2286 return;
2287 }
2288 if (unlikely(sc->bmisscount != 0)) {
Bruno Randolfbe9b7252008-01-23 10:27:51 +09002289 ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002290 "resume beacon xmit after %u misses\n",
2291 sc->bmisscount);
2292 sc->bmisscount = 0;
2293 }
2294
2295 /*
2296 * Stop any current dma and put the new frame on the queue.
2297 * This should never fail since we check above that no frames
2298 * are still pending on the queue.
2299 */
2300 if (unlikely(ath5k_hw_stop_tx_dma(ah, sc->bhalq))) {
Nick Kossifidis428cbd42009-04-30 15:55:47 -04002301 ATH5K_WARN(sc, "beacon queue %u didn't start/stop ?\n", sc->bhalq);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002302 /* NB: hw still stops DMA, so proceed */
2303 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002304
Bob Copeland1071db82009-05-18 10:59:52 -04002305 /* refresh the beacon for AP mode */
2306 if (sc->opmode == NL80211_IFTYPE_AP)
2307 ath5k_beacon_update(sc->hw, sc->vif);
2308
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03002309 ath5k_hw_set_txdp(ah, sc->bhalq, bf->daddr);
2310 ath5k_hw_start_tx_dma(ah, sc->bhalq);
Bruno Randolfbe9b7252008-01-23 10:27:51 +09002311 ATH5K_DBG(sc, ATH5K_DEBUG_BEACON, "TXDP[%u] = %llx (%p)\n",
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002312 sc->bhalq, (unsigned long long)bf->daddr, bf->desc);
2313
Bob Copelandcec8db22009-07-04 12:59:51 -04002314 skb = ieee80211_get_buffered_bc(sc->hw, sc->vif);
2315 while (skb) {
2316 ath5k_tx_queue(sc->hw, skb, sc->cabq);
2317 skb = ieee80211_get_buffered_bc(sc->hw, sc->vif);
2318 }
2319
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002320 sc->bsent++;
2321}
2322
2323
Bruno Randolf9804b982008-01-19 18:17:59 +09002324/**
2325 * ath5k_beacon_update_timers - update beacon timers
2326 *
2327 * @sc: struct ath5k_softc pointer we are operating on
2328 * @bc_tsf: the timestamp of the beacon. 0 to reset the TSF. -1 to perform a
2329 * beacon timer update based on the current HW TSF.
2330 *
2331 * Calculate the next target beacon transmit time (TBTT) based on the timestamp
2332 * of a received beacon or the current local hardware TSF and write it to the
2333 * beacon timer registers.
2334 *
2335 * This is called in a variety of situations, e.g. when a beacon is received,
Bruno Randolf6ba81c22008-03-05 18:36:26 +09002336 * when a TSF update has been detected, but also when an new IBSS is created or
Bruno Randolf9804b982008-01-19 18:17:59 +09002337 * when we otherwise know we have to update the timers, but we keep it in this
2338 * function to have it all together in one place.
2339 */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002340static void
Bruno Randolf9804b982008-01-19 18:17:59 +09002341ath5k_beacon_update_timers(struct ath5k_softc *sc, u64 bc_tsf)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002342{
2343 struct ath5k_hw *ah = sc->ah;
Bruno Randolf9804b982008-01-19 18:17:59 +09002344 u32 nexttbtt, intval, hw_tu, bc_tu;
2345 u64 hw_tsf;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002346
2347 intval = sc->bintval & AR5K_BEACON_PERIOD;
2348 if (WARN_ON(!intval))
2349 return;
2350
Bruno Randolf9804b982008-01-19 18:17:59 +09002351 /* beacon TSF converted to TU */
2352 bc_tu = TSF_TO_TU(bc_tsf);
2353
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002354 /* current TSF converted to TU */
Bruno Randolf9804b982008-01-19 18:17:59 +09002355 hw_tsf = ath5k_hw_get_tsf64(ah);
2356 hw_tu = TSF_TO_TU(hw_tsf);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002357
Bruno Randolf9804b982008-01-19 18:17:59 +09002358#define FUDGE 3
2359 /* we use FUDGE to make sure the next TBTT is ahead of the current TU */
2360 if (bc_tsf == -1) {
2361 /*
2362 * no beacons received, called internally.
2363 * just need to refresh timers based on HW TSF.
2364 */
2365 nexttbtt = roundup(hw_tu + FUDGE, intval);
2366 } else if (bc_tsf == 0) {
2367 /*
2368 * no beacon received, probably called by ath5k_reset_tsf().
2369 * reset TSF to start with 0.
2370 */
2371 nexttbtt = intval;
2372 intval |= AR5K_BEACON_RESET_TSF;
2373 } else if (bc_tsf > hw_tsf) {
2374 /*
2375 * beacon received, SW merge happend but HW TSF not yet updated.
2376 * not possible to reconfigure timers yet, but next time we
2377 * receive a beacon with the same BSSID, the hardware will
2378 * automatically update the TSF and then we need to reconfigure
2379 * the timers.
2380 */
2381 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
2382 "need to wait for HW TSF sync\n");
2383 return;
2384 } else {
2385 /*
2386 * most important case for beacon synchronization between STA.
2387 *
2388 * beacon received and HW TSF has been already updated by HW.
2389 * update next TBTT based on the TSF of the beacon, but make
2390 * sure it is ahead of our local TSF timer.
2391 */
2392 nexttbtt = bc_tu + roundup(hw_tu + FUDGE - bc_tu, intval);
2393 }
2394#undef FUDGE
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002395
Bruno Randolf036cd1e2008-01-19 18:18:21 +09002396 sc->nexttbtt = nexttbtt;
2397
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002398 intval |= AR5K_BEACON_ENA;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002399 ath5k_hw_init_beacon(ah, nexttbtt, intval);
Bruno Randolf9804b982008-01-19 18:17:59 +09002400
2401 /*
2402 * debugging output last in order to preserve the time critical aspect
2403 * of this function
2404 */
2405 if (bc_tsf == -1)
2406 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
2407 "reconfigured timers based on HW TSF\n");
2408 else if (bc_tsf == 0)
2409 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
2410 "reset HW TSF and timers\n");
2411 else
2412 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
2413 "updated timers based on beacon TSF\n");
2414
2415 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
David Miller04f93a82008-02-15 16:08:59 -08002416 "bc_tsf %llx hw_tsf %llx bc_tu %u hw_tu %u nexttbtt %u\n",
2417 (unsigned long long) bc_tsf,
2418 (unsigned long long) hw_tsf, bc_tu, hw_tu, nexttbtt);
Bruno Randolf9804b982008-01-19 18:17:59 +09002419 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON, "intval %u %s %s\n",
2420 intval & AR5K_BEACON_PERIOD,
2421 intval & AR5K_BEACON_ENA ? "AR5K_BEACON_ENA" : "",
2422 intval & AR5K_BEACON_RESET_TSF ? "AR5K_BEACON_RESET_TSF" : "");
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002423}
2424
2425
Bruno Randolf036cd1e2008-01-19 18:18:21 +09002426/**
2427 * ath5k_beacon_config - Configure the beacon queues and interrupts
2428 *
2429 * @sc: struct ath5k_softc pointer we are operating on
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002430 *
Bruno Randolf036cd1e2008-01-19 18:18:21 +09002431 * In IBSS mode we use a self-linked tx descriptor if possible. We enable SWBA
Bruno Randolf6ba81c22008-03-05 18:36:26 +09002432 * interrupts to detect TSF updates only.
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002433 */
2434static void
2435ath5k_beacon_config(struct ath5k_softc *sc)
2436{
2437 struct ath5k_hw *ah = sc->ah;
Bob Copelandb5f03952009-02-15 12:06:10 -05002438 unsigned long flags;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002439
Bob Copeland21800492009-07-04 12:59:52 -04002440 spin_lock_irqsave(&sc->block, flags);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002441 sc->bmisscount = 0;
Jiri Slabydc1968e2008-07-23 13:17:34 +02002442 sc->imask &= ~(AR5K_INT_BMISS | AR5K_INT_SWBA);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002443
Bob Copeland21800492009-07-04 12:59:52 -04002444 if (sc->enable_beacon) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002445 /*
Bruno Randolf036cd1e2008-01-19 18:18:21 +09002446 * In IBSS mode we use a self-linked tx descriptor and let the
2447 * hardware send the beacons automatically. We have to load it
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002448 * only once here.
Bruno Randolf036cd1e2008-01-19 18:18:21 +09002449 * We use the SWBA interrupt only to keep track of the beacon
Bruno Randolf6ba81c22008-03-05 18:36:26 +09002450 * timers in order to detect automatic TSF updates.
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002451 */
2452 ath5k_beaconq_config(sc);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002453
Bruno Randolf036cd1e2008-01-19 18:18:21 +09002454 sc->imask |= AR5K_INT_SWBA;
2455
Jiri Slabyda966bc2008-10-12 22:54:10 +02002456 if (sc->opmode == NL80211_IFTYPE_ADHOC) {
Bob Copeland21800492009-07-04 12:59:52 -04002457 if (ath5k_hw_hasveol(ah))
Jiri Slabyda966bc2008-10-12 22:54:10 +02002458 ath5k_beacon_send(sc);
Jiri Slabyda966bc2008-10-12 22:54:10 +02002459 } else
2460 ath5k_beacon_update_timers(sc, -1);
Bob Copeland21800492009-07-04 12:59:52 -04002461 } else {
2462 ath5k_hw_stop_tx_dma(sc->ah, sc->bhalq);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002463 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002464
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03002465 ath5k_hw_set_imr(ah, sc->imask);
Bob Copeland21800492009-07-04 12:59:52 -04002466 mmiowb();
2467 spin_unlock_irqrestore(&sc->block, flags);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002468}
2469
Nick Kossifidis428cbd42009-04-30 15:55:47 -04002470static void ath5k_tasklet_beacon(unsigned long data)
2471{
2472 struct ath5k_softc *sc = (struct ath5k_softc *) data;
2473
2474 /*
2475 * Software beacon alert--time to send a beacon.
2476 *
2477 * In IBSS mode we use this interrupt just to
2478 * keep track of the next TBTT (target beacon
2479 * transmission time) in order to detect wether
2480 * automatic TSF updates happened.
2481 */
2482 if (sc->opmode == NL80211_IFTYPE_ADHOC) {
2483 /* XXX: only if VEOL suppported */
2484 u64 tsf = ath5k_hw_get_tsf64(sc->ah);
2485 sc->nexttbtt += sc->bintval;
2486 ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
2487 "SWBA nexttbtt: %x hw_tu: %x "
2488 "TSF: %llx\n",
2489 sc->nexttbtt,
2490 TSF_TO_TU(tsf),
2491 (unsigned long long) tsf);
2492 } else {
2493 spin_lock(&sc->block);
2494 ath5k_beacon_send(sc);
2495 spin_unlock(&sc->block);
2496 }
2497}
2498
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002499
2500/********************\
2501* Interrupt handling *
2502\********************/
2503
2504static int
Bob Copelandbb2beca2009-01-19 11:20:54 -05002505ath5k_init(struct ath5k_softc *sc)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002506{
Elias Oltmannsbc1b32d2008-10-24 21:59:18 +02002507 struct ath5k_hw *ah = sc->ah;
2508 int ret, i;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002509
2510 mutex_lock(&sc->lock);
2511
2512 ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "mode %d\n", sc->opmode);
2513
2514 /*
2515 * Stop anything previously setup. This is safe
2516 * no matter this is the first time through or not.
2517 */
2518 ath5k_stop_locked(sc);
2519
2520 /*
2521 * The basic interface to setting the hardware in a good
2522 * state is ``reset''. On return the hardware is known to
2523 * be powered up and with interrupts disabled. This must
2524 * be followed by initialization of the appropriate bits
2525 * and then setup of the interrupt mask.
2526 */
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -05002527 sc->curchan = sc->hw->conf.channel;
2528 sc->curband = &sc->sbands[sc->curchan->band];
Nick Kossifidis6a53a8a2008-11-04 00:25:54 +02002529 sc->imask = AR5K_INT_RXOK | AR5K_INT_RXERR | AR5K_INT_RXEOL |
2530 AR5K_INT_RXORN | AR5K_INT_TXDESC | AR5K_INT_TXEOL |
Bruno Randolfe65e1d72010-03-25 14:49:09 +09002531 AR5K_INT_FATAL | AR5K_INT_GLOBAL;
Bob Copeland209d8892009-05-07 08:09:08 -04002532 ret = ath5k_reset(sc, NULL);
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002533 if (ret)
2534 goto done;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002535
Tobias Doerffele6a3b612009-06-09 17:33:27 +02002536 ath5k_rfkill_hw_start(ah);
2537
Elias Oltmannsbc1b32d2008-10-24 21:59:18 +02002538 /*
2539 * Reset the key cache since some parts do not reset the
2540 * contents on initial power up or resume from suspend.
2541 */
2542 for (i = 0; i < AR5K_KEYTABLE_SIZE; i++)
2543 ath5k_hw_reset_key(ah, i);
2544
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002545 /* Set ack to be sent at low bit-rates */
Elias Oltmannsbc1b32d2008-10-24 21:59:18 +02002546 ath5k_hw_set_ack_bitrate_high(ah, false);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002547 ret = 0;
2548done:
Jiri Slaby274c7c32008-07-15 17:44:20 +02002549 mmiowb();
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002550 mutex_unlock(&sc->lock);
2551 return ret;
2552}
2553
2554static int
2555ath5k_stop_locked(struct ath5k_softc *sc)
2556{
2557 struct ath5k_hw *ah = sc->ah;
2558
2559 ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "invalid %u\n",
2560 test_bit(ATH_STAT_INVALID, sc->status));
2561
2562 /*
2563 * Shutdown the hardware and driver:
2564 * stop output from above
2565 * disable interrupts
2566 * turn off timers
2567 * turn off the radio
2568 * clear transmit machinery
2569 * clear receive machinery
2570 * drain and release tx queues
2571 * reclaim beacon resources
2572 * power down hardware
2573 *
2574 * Note that some of this work is not possible if the
2575 * hardware is gone (invalid).
2576 */
2577 ieee80211_stop_queues(sc->hw);
2578
2579 if (!test_bit(ATH_STAT_INVALID, sc->status)) {
Bob Copeland3a078872008-06-25 22:35:28 -04002580 ath5k_led_off(sc);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03002581 ath5k_hw_set_imr(ah, 0);
Jiri Slaby274c7c32008-07-15 17:44:20 +02002582 synchronize_irq(sc->pdev->irq);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002583 }
2584 ath5k_txq_cleanup(sc);
2585 if (!test_bit(ATH_STAT_INVALID, sc->status)) {
2586 ath5k_rx_stop(sc);
2587 ath5k_hw_phy_disable(ah);
2588 } else
2589 sc->rxlink = NULL;
2590
2591 return 0;
2592}
2593
2594/*
2595 * Stop the device, grabbing the top-level lock to protect
2596 * against concurrent entry through ath5k_init (which can happen
2597 * if another thread does a system call and the thread doing the
2598 * stop is preempted).
2599 */
2600static int
Bob Copelandbb2beca2009-01-19 11:20:54 -05002601ath5k_stop_hw(struct ath5k_softc *sc)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002602{
2603 int ret;
2604
2605 mutex_lock(&sc->lock);
2606 ret = ath5k_stop_locked(sc);
2607 if (ret == 0 && !test_bit(ATH_STAT_INVALID, sc->status)) {
2608 /*
Nick Kossifidisedd7fc72009-08-10 03:29:02 +03002609 * Don't set the card in full sleep mode!
2610 *
2611 * a) When the device is in this state it must be carefully
2612 * woken up or references to registers in the PCI clock
2613 * domain may freeze the bus (and system). This varies
2614 * by chip and is mostly an issue with newer parts
2615 * (madwifi sources mentioned srev >= 0x78) that go to
2616 * sleep more quickly.
2617 *
2618 * b) On older chips full sleep results a weird behaviour
2619 * during wakeup. I tested various cards with srev < 0x78
2620 * and they don't wake up after module reload, a second
2621 * module reload is needed to bring the card up again.
2622 *
2623 * Until we figure out what's going on don't enable
2624 * full chip reset on any chip (this is what Legacy HAL
2625 * and Sam's HAL do anyway). Instead Perform a full reset
2626 * on the device (same as initial state after attach) and
2627 * leave it idle (keep MAC/BB on warm reset) */
2628 ret = ath5k_hw_on_hold(sc->ah);
2629
2630 ATH5K_DBG(sc, ATH5K_DEBUG_RESET,
2631 "putting device to sleep\n");
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002632 }
2633 ath5k_txbuf_free(sc, sc->bbuf);
Bob Copeland8bdd5b92008-10-16 11:02:06 -04002634
Jiri Slaby274c7c32008-07-15 17:44:20 +02002635 mmiowb();
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002636 mutex_unlock(&sc->lock);
2637
Jiri Slaby10488f82008-07-15 17:44:19 +02002638 tasklet_kill(&sc->rxtq);
2639 tasklet_kill(&sc->txtq);
2640 tasklet_kill(&sc->restq);
Nick Kossifidis6e220662009-08-10 03:31:31 +03002641 tasklet_kill(&sc->calib);
Bob Copelandacf3c1a2009-02-15 12:06:11 -05002642 tasklet_kill(&sc->beacontq);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002643
Tobias Doerffele6a3b612009-06-09 17:33:27 +02002644 ath5k_rfkill_hw_stop(sc->ah);
2645
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002646 return ret;
2647}
2648
Bruno Randolf6a8a3f62010-03-25 14:49:19 +09002649static void
2650ath5k_intr_calibration_poll(struct ath5k_hw *ah)
2651{
2652 if (time_is_before_eq_jiffies(ah->ah_cal_next_full)) {
2653 ah->ah_cal_next_full = jiffies +
2654 msecs_to_jiffies(ATH5K_TUNE_CALIBRATION_INTERVAL_FULL);
2655 tasklet_schedule(&ah->ah_sc->calib);
2656 }
2657 /* we could use SWI to generate enough interrupts to meet our
2658 * calibration interval requirements, if necessary:
2659 * AR5K_REG_ENABLE_BITS(ah, AR5K_CR, AR5K_CR_SWI); */
2660}
2661
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002662static irqreturn_t
2663ath5k_intr(int irq, void *dev_id)
2664{
2665 struct ath5k_softc *sc = dev_id;
2666 struct ath5k_hw *ah = sc->ah;
2667 enum ath5k_int status;
2668 unsigned int counter = 1000;
2669
2670 if (unlikely(test_bit(ATH_STAT_INVALID, sc->status) ||
2671 !ath5k_hw_is_intr_pending(ah)))
2672 return IRQ_NONE;
2673
2674 do {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002675 ath5k_hw_get_isr(ah, &status); /* NB: clears IRQ too */
2676 ATH5K_DBG(sc, ATH5K_DEBUG_INTR, "status 0x%x/0x%x\n",
2677 status, sc->imask);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002678 if (unlikely(status & AR5K_INT_FATAL)) {
2679 /*
2680 * Fatal errors are unrecoverable.
2681 * Typically these are caused by DMA errors.
2682 */
2683 tasklet_schedule(&sc->restq);
2684 } else if (unlikely(status & AR5K_INT_RXORN)) {
2685 tasklet_schedule(&sc->restq);
2686 } else {
2687 if (status & AR5K_INT_SWBA) {
Bob Copeland56d2ac72009-04-15 07:57:33 -04002688 tasklet_hi_schedule(&sc->beacontq);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002689 }
2690 if (status & AR5K_INT_RXEOL) {
2691 /*
2692 * NB: the hardware should re-read the link when
2693 * RXE bit is written, but it doesn't work at
2694 * least on older hardware revs.
2695 */
2696 sc->rxlink = NULL;
2697 }
2698 if (status & AR5K_INT_TXURN) {
2699 /* bump tx trigger level */
2700 ath5k_hw_update_tx_triglevel(ah, true);
2701 }
Nick Kossifidis4c674c62008-10-26 20:40:25 +02002702 if (status & (AR5K_INT_RXOK | AR5K_INT_RXERR))
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002703 tasklet_schedule(&sc->rxtq);
Nick Kossifidis4c674c62008-10-26 20:40:25 +02002704 if (status & (AR5K_INT_TXOK | AR5K_INT_TXDESC
2705 | AR5K_INT_TXERR | AR5K_INT_TXEOL))
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002706 tasklet_schedule(&sc->txtq);
2707 if (status & AR5K_INT_BMISS) {
Nick Kossifidis1e3e6e82009-02-09 06:15:42 +02002708 /* TODO */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002709 }
2710 if (status & AR5K_INT_MIB) {
Nick Kossifidis194828a2008-04-16 18:49:02 +03002711 /*
2712 * These stats are also used for ANI i think
2713 * so how about updating them more often ?
2714 */
2715 ath5k_hw_update_mib_counters(ah, &sc->ll_stats);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002716 }
Tobias Doerffele6a3b612009-06-09 17:33:27 +02002717 if (status & AR5K_INT_GPIO)
Tobias Doerffele6a3b612009-06-09 17:33:27 +02002718 tasklet_schedule(&sc->rf_kill.toggleq);
Bob Copelanda6ae0712009-06-09 23:43:11 -04002719
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002720 }
Bob Copeland2516baa2009-04-27 22:18:10 -04002721 } while (ath5k_hw_is_intr_pending(ah) && --counter > 0);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002722
2723 if (unlikely(!counter))
2724 ATH5K_WARN(sc, "too many interrupts, giving up for now\n");
2725
Bruno Randolf6a8a3f62010-03-25 14:49:19 +09002726 ath5k_intr_calibration_poll(ah);
Nick Kossifidis6e220662009-08-10 03:31:31 +03002727
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002728 return IRQ_HANDLED;
2729}
2730
2731static void
2732ath5k_tasklet_reset(unsigned long data)
2733{
2734 struct ath5k_softc *sc = (void *)data;
2735
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002736 ath5k_reset_wake(sc);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002737}
2738
2739/*
2740 * Periodically recalibrate the PHY to account
2741 * for temperature/environment changes.
2742 */
2743static void
Nick Kossifidis6e220662009-08-10 03:31:31 +03002744ath5k_tasklet_calibrate(unsigned long data)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002745{
2746 struct ath5k_softc *sc = (void *)data;
2747 struct ath5k_hw *ah = sc->ah;
2748
Nick Kossifidis6e220662009-08-10 03:31:31 +03002749 /* Only full calibration for now */
Bruno Randolfe65e1d72010-03-25 14:49:09 +09002750 ah->ah_cal_mask |= AR5K_CALIBRATION_FULL;
Nick Kossifidis6e220662009-08-10 03:31:31 +03002751
2752 /* Stop queues so that calibration
2753 * doesn't interfere with tx */
2754 ieee80211_stop_queues(sc->hw);
2755
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002756 ATH5K_DBG(sc, ATH5K_DEBUG_CALIBRATE, "channel %u/%x\n",
Luis R. Rodriguez400ec452008-02-03 21:51:49 -05002757 ieee80211_frequency_to_channel(sc->curchan->center_freq),
2758 sc->curchan->hw_value);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002759
Nick Kossifidis6f3b4142009-02-09 06:03:41 +02002760 if (ath5k_hw_gainf_calibrate(ah) == AR5K_RFGAIN_NEED_CHANGE) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002761 /*
2762 * Rfgain is out of bounds, reset the chip
2763 * to load new gain values.
2764 */
2765 ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "calibration, resetting\n");
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002766 ath5k_reset_wake(sc);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002767 }
2768 if (ath5k_hw_phy_calibrate(ah, sc->curchan))
2769 ATH5K_ERR(sc, "calibration of channel %u failed\n",
Luis R. Rodriguez400ec452008-02-03 21:51:49 -05002770 ieee80211_frequency_to_channel(
2771 sc->curchan->center_freq));
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002772
Nick Kossifidis6e220662009-08-10 03:31:31 +03002773 /* Wake queues */
2774 ieee80211_wake_queues(sc->hw);
2775
Bruno Randolfe65e1d72010-03-25 14:49:09 +09002776 ah->ah_cal_mask &= ~AR5K_CALIBRATION_FULL;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002777}
2778
2779
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002780/********************\
2781* Mac80211 functions *
2782\********************/
2783
2784static int
Johannes Berge039fa42008-05-15 12:55:29 +02002785ath5k_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002786{
2787 struct ath5k_softc *sc = hw->priv;
Bob Copelandcec8db22009-07-04 12:59:51 -04002788
2789 return ath5k_tx_queue(hw, skb, sc->txq);
2790}
2791
2792static int ath5k_tx_queue(struct ieee80211_hw *hw, struct sk_buff *skb,
2793 struct ath5k_txq *txq)
2794{
2795 struct ath5k_softc *sc = hw->priv;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002796 struct ath5k_buf *bf;
2797 unsigned long flags;
Benoit PAPILLAULT0fe45b12008-12-12 15:29:58 +01002798 int padsize;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002799
2800 ath5k_debug_dump_skb(sc, skb, "TX ", 1);
2801
Johannes Berg05c914f2008-09-11 00:01:58 +02002802 if (sc->opmode == NL80211_IFTYPE_MONITOR)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002803 ATH5K_DBG(sc, ATH5K_DEBUG_XMIT, "tx in monitor (scan?)\n");
2804
2805 /*
2806 * the hardware expects the header padded to 4 byte boundaries
2807 * if this is not the case we add the padding after the header
2808 */
Benoit Papillault8127fbd2010-02-27 23:05:26 +01002809 padsize = ath5k_add_padding(skb);
2810 if (padsize < 0) {
2811 ATH5K_ERR(sc, "tx hdrlen not %%4: not enough"
2812 " headroom to pad");
2813 goto drop_packet;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002814 }
2815
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002816 spin_lock_irqsave(&sc->txbuflock, flags);
2817 if (list_empty(&sc->txbuf)) {
2818 ATH5K_ERR(sc, "no further txbuf available, dropping packet\n");
2819 spin_unlock_irqrestore(&sc->txbuflock, flags);
Johannes Berge2530082008-05-17 00:57:14 +02002820 ieee80211_stop_queue(hw, skb_get_queue_mapping(skb));
Bob Copeland5a0fe8a2009-03-23 23:35:37 -04002821 goto drop_packet;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002822 }
2823 bf = list_first_entry(&sc->txbuf, struct ath5k_buf, list);
2824 list_del(&bf->list);
2825 sc->txbuf_len--;
2826 if (list_empty(&sc->txbuf))
2827 ieee80211_stop_queues(hw);
2828 spin_unlock_irqrestore(&sc->txbuflock, flags);
2829
2830 bf->skb = skb;
2831
Benoit Papillault8127fbd2010-02-27 23:05:26 +01002832 if (ath5k_txbuf_setup(sc, bf, txq, padsize)) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002833 bf->skb = NULL;
2834 spin_lock_irqsave(&sc->txbuflock, flags);
2835 list_add_tail(&bf->list, &sc->txbuf);
2836 sc->txbuf_len++;
2837 spin_unlock_irqrestore(&sc->txbuflock, flags);
Bob Copeland5a0fe8a2009-03-23 23:35:37 -04002838 goto drop_packet;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002839 }
Bob Copeland5a0fe8a2009-03-23 23:35:37 -04002840 return NETDEV_TX_OK;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002841
Bob Copeland5a0fe8a2009-03-23 23:35:37 -04002842drop_packet:
2843 dev_kfree_skb_any(skb);
Bob Copeland71ef99c2009-01-05 20:46:34 -05002844 return NETDEV_TX_OK;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002845}
2846
Bob Copeland209d8892009-05-07 08:09:08 -04002847/*
2848 * Reset the hardware. If chan is not NULL, then also pause rx/tx
2849 * and change to the given channel.
2850 */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002851static int
Bob Copeland209d8892009-05-07 08:09:08 -04002852ath5k_reset(struct ath5k_softc *sc, struct ieee80211_channel *chan)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002853{
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002854 struct ath5k_hw *ah = sc->ah;
2855 int ret;
2856
2857 ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "resetting\n");
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002858
Bob Copeland209d8892009-05-07 08:09:08 -04002859 if (chan) {
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03002860 ath5k_hw_set_imr(ah, 0);
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002861 ath5k_txq_cleanup(sc);
2862 ath5k_rx_stop(sc);
Bob Copeland209d8892009-05-07 08:09:08 -04002863
2864 sc->curchan = chan;
2865 sc->curband = &sc->sbands[chan->band];
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002866 }
Bob Copeland33554432009-07-04 21:03:13 -04002867 ret = ath5k_hw_reset(ah, sc->opmode, sc->curchan, chan != NULL);
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002868 if (ret) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002869 ATH5K_ERR(sc, "can't reset hardware (%d)\n", ret);
2870 goto err;
2871 }
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002872
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002873 ret = ath5k_rx_start(sc);
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002874 if (ret) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002875 ATH5K_ERR(sc, "can't start recv logic\n");
2876 goto err;
2877 }
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002878
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002879 /*
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002880 * Change channels and update the h/w rate map if we're switching;
2881 * e.g. 11a to 11b/g.
2882 *
2883 * We may be doing a reset in response to an ioctl that changes the
2884 * channel so update any state that might change as a result.
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002885 *
2886 * XXX needed?
2887 */
2888/* ath5k_chan_change(sc, c); */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002889
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002890 ath5k_beacon_config(sc);
2891 /* intrs are enabled by ath5k_beacon_config */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002892
2893 return 0;
2894err:
2895 return ret;
2896}
2897
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002898static int
2899ath5k_reset_wake(struct ath5k_softc *sc)
2900{
2901 int ret;
2902
Bob Copeland209d8892009-05-07 08:09:08 -04002903 ret = ath5k_reset(sc, sc->curchan);
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002904 if (!ret)
2905 ieee80211_wake_queues(sc->hw);
2906
2907 return ret;
2908}
2909
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002910static int ath5k_start(struct ieee80211_hw *hw)
2911{
Bob Copelandbb2beca2009-01-19 11:20:54 -05002912 return ath5k_init(hw->priv);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002913}
2914
2915static void ath5k_stop(struct ieee80211_hw *hw)
2916{
Bob Copelandbb2beca2009-01-19 11:20:54 -05002917 ath5k_stop_hw(hw->priv);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002918}
2919
2920static int ath5k_add_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +01002921 struct ieee80211_vif *vif)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002922{
2923 struct ath5k_softc *sc = hw->priv;
2924 int ret;
2925
2926 mutex_lock(&sc->lock);
Johannes Berg32bfd352007-12-19 01:31:26 +01002927 if (sc->vif) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002928 ret = 0;
2929 goto end;
2930 }
2931
Johannes Berg1ed32e42009-12-23 13:15:45 +01002932 sc->vif = vif;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002933
Johannes Berg1ed32e42009-12-23 13:15:45 +01002934 switch (vif->type) {
Jiri Slabyda966bc2008-10-12 22:54:10 +02002935 case NL80211_IFTYPE_AP:
Johannes Berg05c914f2008-09-11 00:01:58 +02002936 case NL80211_IFTYPE_STATION:
2937 case NL80211_IFTYPE_ADHOC:
Andrey Yurovskyb706e652008-10-13 18:23:07 -07002938 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg05c914f2008-09-11 00:01:58 +02002939 case NL80211_IFTYPE_MONITOR:
Johannes Berg1ed32e42009-12-23 13:15:45 +01002940 sc->opmode = vif->type;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002941 break;
2942 default:
2943 ret = -EOPNOTSUPP;
2944 goto end;
2945 }
Jiri Slaby67d2e2d2008-08-18 21:45:28 +02002946
Bruno Randolfccfe5552010-03-09 16:55:38 +09002947 ATH5K_DBG(sc, ATH5K_DEBUG_MODE, "add interface mode %d\n", sc->opmode);
2948
Johannes Berg1ed32e42009-12-23 13:15:45 +01002949 ath5k_hw_set_lladdr(sc->ah, vif->addr);
Bob Copelandae6f53f2009-07-29 10:29:03 -04002950 ath5k_mode_setup(sc);
Jiri Slaby67d2e2d2008-08-18 21:45:28 +02002951
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002952 ret = 0;
2953end:
2954 mutex_unlock(&sc->lock);
2955 return ret;
2956}
2957
2958static void
2959ath5k_remove_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +01002960 struct ieee80211_vif *vif)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002961{
2962 struct ath5k_softc *sc = hw->priv;
Bob Copeland0e149cf2008-11-17 23:40:38 -05002963 u8 mac[ETH_ALEN] = {};
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002964
2965 mutex_lock(&sc->lock);
Johannes Berg1ed32e42009-12-23 13:15:45 +01002966 if (sc->vif != vif)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002967 goto end;
2968
Bob Copeland0e149cf2008-11-17 23:40:38 -05002969 ath5k_hw_set_lladdr(sc->ah, mac);
Johannes Berg32bfd352007-12-19 01:31:26 +01002970 sc->vif = NULL;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002971end:
2972 mutex_unlock(&sc->lock);
2973}
2974
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -05002975/*
2976 * TODO: Phy disable/diversity etc
2977 */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002978static int
Johannes Berge8975582008-10-09 12:18:51 +02002979ath5k_config(struct ieee80211_hw *hw, u32 changed)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002980{
2981 struct ath5k_softc *sc = hw->priv;
Nick Kossifidisa0823812009-04-30 15:55:44 -04002982 struct ath5k_hw *ah = sc->ah;
Johannes Berge8975582008-10-09 12:18:51 +02002983 struct ieee80211_conf *conf = &hw->conf;
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04002984 int ret = 0;
Bob Copelandbe009372009-01-22 08:44:16 -05002985
2986 mutex_lock(&sc->lock);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002987
Joerg Alberte30eb4a2009-08-05 01:52:07 +02002988 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
2989 ret = ath5k_chan_set(sc, conf->channel);
2990 if (ret < 0)
2991 goto unlock;
2992 }
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04002993
Nick Kossifidisa0823812009-04-30 15:55:44 -04002994 if ((changed & IEEE80211_CONF_CHANGE_POWER) &&
2995 (sc->power_level != conf->power_level)) {
2996 sc->power_level = conf->power_level;
2997
2998 /* Half dB steps */
2999 ath5k_hw_set_txpower_limit(ah, (conf->power_level * 2));
3000 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003001
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04003002 /* TODO:
3003 * 1) Move this on config_interface and handle each case
3004 * separately eg. when we have only one STA vif, use
3005 * AR5K_ANTMODE_SINGLE_AP
3006 *
3007 * 2) Allow the user to change antenna mode eg. when only
3008 * one antenna is present
3009 *
3010 * 3) Allow the user to set default/tx antenna when possible
3011 *
3012 * 4) Default mode should handle 90% of the cases, together
3013 * with fixed a/b and single AP modes we should be able to
3014 * handle 99%. Sectored modes are extreme cases and i still
3015 * haven't found a usage for them. If we decide to support them,
3016 * then we must allow the user to set how many tx antennas we
3017 * have available
3018 */
Bruno Randolfcaec9112010-03-09 16:55:28 +09003019 ath5k_hw_set_antenna_mode(ah, ah->ah_ant_mode);
Bob Copelandbe009372009-01-22 08:44:16 -05003020
John W. Linville55aa4e02009-05-25 21:28:47 +02003021unlock:
Bob Copelandbe009372009-01-22 08:44:16 -05003022 mutex_unlock(&sc->lock);
John W. Linville55aa4e02009-05-25 21:28:47 +02003023 return ret;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003024}
3025
Johannes Berg3ac64be2009-08-17 16:16:53 +02003026static u64 ath5k_prepare_multicast(struct ieee80211_hw *hw,
3027 int mc_count, struct dev_addr_list *mclist)
3028{
3029 u32 mfilt[2], val;
3030 int i;
3031 u8 pos;
3032
3033 mfilt[0] = 0;
3034 mfilt[1] = 1;
3035
3036 for (i = 0; i < mc_count; i++) {
3037 if (!mclist)
3038 break;
3039 /* calculate XOR of eight 6-bit values */
3040 val = get_unaligned_le32(mclist->dmi_addr + 0);
3041 pos = (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
3042 val = get_unaligned_le32(mclist->dmi_addr + 3);
3043 pos ^= (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
3044 pos &= 0x3f;
3045 mfilt[pos / 32] |= (1 << (pos % 32));
3046 /* XXX: we might be able to just do this instead,
3047 * but not sure, needs testing, if we do use this we'd
3048 * neet to inform below to not reset the mcast */
3049 /* ath5k_hw_set_mcast_filterindex(ah,
3050 * mclist->dmi_addr[5]); */
3051 mclist = mclist->next;
3052 }
3053
3054 return ((u64)(mfilt[1]) << 32) | mfilt[0];
3055}
3056
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003057#define SUPPORTED_FIF_FLAGS \
3058 FIF_PROMISC_IN_BSS | FIF_ALLMULTI | FIF_FCSFAIL | \
3059 FIF_PLCPFAIL | FIF_CONTROL | FIF_OTHER_BSS | \
3060 FIF_BCN_PRBRESP_PROMISC
3061/*
3062 * o always accept unicast, broadcast, and multicast traffic
3063 * o multicast traffic for all BSSIDs will be enabled if mac80211
3064 * says it should be
3065 * o maintain current state of phy ofdm or phy cck error reception.
3066 * If the hardware detects any of these type of errors then
3067 * ath5k_hw_get_rx_filter() will pass to us the respective
3068 * hardware filters to be able to receive these type of frames.
3069 * o probe request frames are accepted only when operating in
3070 * hostap, adhoc, or monitor modes
3071 * o enable promiscuous mode according to the interface state
3072 * o accept beacons:
3073 * - when operating in adhoc mode so the 802.11 layer creates
3074 * node table entries for peers,
3075 * - when operating in station mode for collecting rssi data when
3076 * the station is otherwise quiet, or
3077 * - when scanning
3078 */
3079static void ath5k_configure_filter(struct ieee80211_hw *hw,
3080 unsigned int changed_flags,
3081 unsigned int *new_flags,
Johannes Berg3ac64be2009-08-17 16:16:53 +02003082 u64 multicast)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003083{
3084 struct ath5k_softc *sc = hw->priv;
3085 struct ath5k_hw *ah = sc->ah;
Johannes Berg3ac64be2009-08-17 16:16:53 +02003086 u32 mfilt[2], rfilt;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003087
Bob Copeland56d1de02009-08-24 23:00:30 -04003088 mutex_lock(&sc->lock);
3089
Johannes Berg3ac64be2009-08-17 16:16:53 +02003090 mfilt[0] = multicast;
3091 mfilt[1] = multicast >> 32;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003092
3093 /* Only deal with supported flags */
3094 changed_flags &= SUPPORTED_FIF_FLAGS;
3095 *new_flags &= SUPPORTED_FIF_FLAGS;
3096
3097 /* If HW detects any phy or radar errors, leave those filters on.
3098 * Also, always enable Unicast, Broadcasts and Multicast
3099 * XXX: move unicast, bssid broadcasts and multicast to mac80211 */
3100 rfilt = (ath5k_hw_get_rx_filter(ah) & (AR5K_RX_FILTER_PHYERR)) |
3101 (AR5K_RX_FILTER_UCAST | AR5K_RX_FILTER_BCAST |
3102 AR5K_RX_FILTER_MCAST);
3103
3104 if (changed_flags & (FIF_PROMISC_IN_BSS | FIF_OTHER_BSS)) {
3105 if (*new_flags & FIF_PROMISC_IN_BSS) {
3106 rfilt |= AR5K_RX_FILTER_PROM;
3107 __set_bit(ATH_STAT_PROMISC, sc->status);
John Daiker0bbac082008-10-17 12:16:00 -07003108 } else {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003109 __clear_bit(ATH_STAT_PROMISC, sc->status);
John Daiker0bbac082008-10-17 12:16:00 -07003110 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003111 }
3112
3113 /* Note, AR5K_RX_FILTER_MCAST is already enabled */
3114 if (*new_flags & FIF_ALLMULTI) {
3115 mfilt[0] = ~0;
3116 mfilt[1] = ~0;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003117 }
3118
3119 /* This is the best we can do */
3120 if (*new_flags & (FIF_FCSFAIL | FIF_PLCPFAIL))
3121 rfilt |= AR5K_RX_FILTER_PHYERR;
3122
3123 /* FIF_BCN_PRBRESP_PROMISC really means to enable beacons
3124 * and probes for any BSSID, this needs testing */
3125 if (*new_flags & FIF_BCN_PRBRESP_PROMISC)
3126 rfilt |= AR5K_RX_FILTER_BEACON | AR5K_RX_FILTER_PROBEREQ;
3127
3128 /* FIF_CONTROL doc says that if FIF_PROMISC_IN_BSS is not
3129 * set we should only pass on control frames for this
3130 * station. This needs testing. I believe right now this
3131 * enables *all* control frames, which is OK.. but
3132 * but we should see if we can improve on granularity */
3133 if (*new_flags & FIF_CONTROL)
3134 rfilt |= AR5K_RX_FILTER_CONTROL;
3135
3136 /* Additional settings per mode -- this is per ath5k */
3137
3138 /* XXX move these to mac80211, and add a beacon IFF flag to mac80211 */
3139
Bob Copeland56d1de02009-08-24 23:00:30 -04003140 switch (sc->opmode) {
3141 case NL80211_IFTYPE_MESH_POINT:
3142 case NL80211_IFTYPE_MONITOR:
3143 rfilt |= AR5K_RX_FILTER_CONTROL |
3144 AR5K_RX_FILTER_BEACON |
3145 AR5K_RX_FILTER_PROBEREQ |
3146 AR5K_RX_FILTER_PROM;
3147 break;
3148 case NL80211_IFTYPE_AP:
3149 case NL80211_IFTYPE_ADHOC:
3150 rfilt |= AR5K_RX_FILTER_PROBEREQ |
3151 AR5K_RX_FILTER_BEACON;
3152 break;
3153 case NL80211_IFTYPE_STATION:
3154 if (sc->assoc)
3155 rfilt |= AR5K_RX_FILTER_BEACON;
3156 default:
3157 break;
3158 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003159
3160 /* Set filters */
John Daiker0bbac082008-10-17 12:16:00 -07003161 ath5k_hw_set_rx_filter(ah, rfilt);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003162
3163 /* Set multicast bits */
3164 ath5k_hw_set_mcast_filter(ah, mfilt[0], mfilt[1]);
3165 /* Set the cached hw filter flags, this will alter actually
3166 * be set in HW */
3167 sc->filter_flags = rfilt;
Bob Copeland56d1de02009-08-24 23:00:30 -04003168
3169 mutex_unlock(&sc->lock);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003170}
3171
3172static int
3173ath5k_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Johannes Bergdc822b52008-12-29 12:55:09 +01003174 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
3175 struct ieee80211_key_conf *key)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003176{
3177 struct ath5k_softc *sc = hw->priv;
Luis R. Rodriguezdc1e0012009-11-04 17:47:31 -08003178 struct ath5k_hw *ah = sc->ah;
3179 struct ath_common *common = ath5k_hw_common(ah);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003180 int ret = 0;
3181
Bob Copeland9ad9a262008-10-29 08:30:54 -04003182 if (modparam_nohwcrypt)
3183 return -EOPNOTSUPP;
3184
Bob Copeland65b5a692009-07-13 21:57:39 -04003185 if (sc->opmode == NL80211_IFTYPE_AP)
3186 return -EOPNOTSUPP;
3187
John Daiker0bbac082008-10-17 12:16:00 -07003188 switch (key->alg) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003189 case ALG_WEP:
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003190 case ALG_TKIP:
Bob Copeland3f64b432008-10-29 23:19:14 -04003191 break;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003192 case ALG_CCMP:
Bob Copeland1c818742009-08-24 23:00:33 -04003193 if (sc->ah->ah_aes_support)
3194 break;
3195
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003196 return -EOPNOTSUPP;
3197 default:
3198 WARN_ON(1);
3199 return -EINVAL;
3200 }
3201
3202 mutex_lock(&sc->lock);
3203
3204 switch (cmd) {
3205 case SET_KEY:
Johannes Bergdc822b52008-12-29 12:55:09 +01003206 ret = ath5k_hw_set_key(sc->ah, key->keyidx, key,
3207 sta ? sta->addr : NULL);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003208 if (ret) {
3209 ATH5K_ERR(sc, "can't set the key\n");
3210 goto unlock;
3211 }
Luis R. Rodriguezdc1e0012009-11-04 17:47:31 -08003212 __set_bit(key->keyidx, common->keymap);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003213 key->hw_key_idx = key->keyidx;
Bob Copeland3f64b432008-10-29 23:19:14 -04003214 key->flags |= (IEEE80211_KEY_FLAG_GENERATE_IV |
3215 IEEE80211_KEY_FLAG_GENERATE_MMIC);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003216 break;
3217 case DISABLE_KEY:
3218 ath5k_hw_reset_key(sc->ah, key->keyidx);
Luis R. Rodriguezdc1e0012009-11-04 17:47:31 -08003219 __clear_bit(key->keyidx, common->keymap);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003220 break;
3221 default:
3222 ret = -EINVAL;
3223 goto unlock;
3224 }
3225
3226unlock:
Jiri Slaby274c7c32008-07-15 17:44:20 +02003227 mmiowb();
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003228 mutex_unlock(&sc->lock);
3229 return ret;
3230}
3231
3232static int
3233ath5k_get_stats(struct ieee80211_hw *hw,
3234 struct ieee80211_low_level_stats *stats)
3235{
3236 struct ath5k_softc *sc = hw->priv;
Nick Kossifidis194828a2008-04-16 18:49:02 +03003237 struct ath5k_hw *ah = sc->ah;
3238
3239 /* Force update */
3240 ath5k_hw_update_mib_counters(ah, &sc->ll_stats);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003241
3242 memcpy(stats, &sc->ll_stats, sizeof(sc->ll_stats));
3243
3244 return 0;
3245}
3246
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003247static u64
3248ath5k_get_tsf(struct ieee80211_hw *hw)
3249{
3250 struct ath5k_softc *sc = hw->priv;
3251
3252 return ath5k_hw_get_tsf64(sc->ah);
3253}
3254
3255static void
Alina Friedrichsen3b5d6652009-01-24 07:09:59 +01003256ath5k_set_tsf(struct ieee80211_hw *hw, u64 tsf)
3257{
3258 struct ath5k_softc *sc = hw->priv;
3259
3260 ath5k_hw_set_tsf64(sc->ah, tsf);
3261}
3262
3263static void
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003264ath5k_reset_tsf(struct ieee80211_hw *hw)
3265{
3266 struct ath5k_softc *sc = hw->priv;
3267
Bruno Randolf9804b982008-01-19 18:17:59 +09003268 /*
3269 * in IBSS mode we need to update the beacon timers too.
3270 * this will also reset the TSF if we call it with 0
3271 */
Johannes Berg05c914f2008-09-11 00:01:58 +02003272 if (sc->opmode == NL80211_IFTYPE_ADHOC)
Bruno Randolf9804b982008-01-19 18:17:59 +09003273 ath5k_beacon_update_timers(sc, 0);
3274 else
3275 ath5k_hw_reset_tsf(sc->ah);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003276}
3277
Bob Copeland1071db82009-05-18 10:59:52 -04003278/*
3279 * Updates the beacon that is sent by ath5k_beacon_send. For adhoc,
3280 * this is called only once at config_bss time, for AP we do it every
3281 * SWBA interrupt so that the TIM will reflect buffered frames.
3282 *
3283 * Called with the beacon lock.
3284 */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003285static int
Bob Copeland1071db82009-05-18 10:59:52 -04003286ath5k_beacon_update(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003287{
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003288 int ret;
Bob Copeland1071db82009-05-18 10:59:52 -04003289 struct ath5k_softc *sc = hw->priv;
Bob Copeland72828b12009-06-02 23:03:06 -04003290 struct sk_buff *skb;
3291
3292 if (WARN_ON(!vif)) {
3293 ret = -EINVAL;
3294 goto out;
3295 }
3296
3297 skb = ieee80211_beacon_get(hw, vif);
Bob Copeland1071db82009-05-18 10:59:52 -04003298
3299 if (!skb) {
3300 ret = -ENOMEM;
3301 goto out;
3302 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003303
3304 ath5k_debug_dump_skb(sc, skb, "BC ", 1);
3305
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003306 ath5k_txbuf_free(sc, sc->bbuf);
3307 sc->bbuf->skb = skb;
Johannes Berge039fa42008-05-15 12:55:29 +02003308 ret = ath5k_beacon_setup(sc, sc->bbuf);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003309 if (ret)
3310 sc->bbuf->skb = NULL;
Bob Copeland1071db82009-05-18 10:59:52 -04003311out:
3312 return ret;
3313}
3314
Martin Xu02969b32008-11-24 10:49:27 +08003315static void
3316set_beacon_filter(struct ieee80211_hw *hw, bool enable)
3317{
3318 struct ath5k_softc *sc = hw->priv;
3319 struct ath5k_hw *ah = sc->ah;
3320 u32 rfilt;
3321 rfilt = ath5k_hw_get_rx_filter(ah);
3322 if (enable)
3323 rfilt |= AR5K_RX_FILTER_BEACON;
3324 else
3325 rfilt &= ~AR5K_RX_FILTER_BEACON;
3326 ath5k_hw_set_rx_filter(ah, rfilt);
3327 sc->filter_flags = rfilt;
3328}
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003329
Martin Xu02969b32008-11-24 10:49:27 +08003330static void ath5k_bss_info_changed(struct ieee80211_hw *hw,
3331 struct ieee80211_vif *vif,
3332 struct ieee80211_bss_conf *bss_conf,
3333 u32 changes)
3334{
3335 struct ath5k_softc *sc = hw->priv;
Johannes Berg2d0ddec2009-04-23 16:13:26 +02003336 struct ath5k_hw *ah = sc->ah;
Luis R. Rodriguez954fece2009-09-10 10:51:33 -07003337 struct ath_common *common = ath5k_hw_common(ah);
Bob Copeland21800492009-07-04 12:59:52 -04003338 unsigned long flags;
Johannes Berg2d0ddec2009-04-23 16:13:26 +02003339
3340 mutex_lock(&sc->lock);
3341 if (WARN_ON(sc->vif != vif))
3342 goto unlock;
3343
3344 if (changes & BSS_CHANGED_BSSID) {
3345 /* Cache for later use during resets */
Luis R. Rodriguez954fece2009-09-10 10:51:33 -07003346 memcpy(common->curbssid, bss_conf->bssid, ETH_ALEN);
Luis R. Rodriguez8ce54c52009-10-06 20:44:34 -04003347 common->curaid = 0;
Luis R. Rodriguezbe5d6b72009-10-06 20:44:31 -04003348 ath5k_hw_set_associd(ah);
Johannes Berg2d0ddec2009-04-23 16:13:26 +02003349 mmiowb();
3350 }
Johannes Berg57c4d7b2009-04-23 16:10:04 +02003351
3352 if (changes & BSS_CHANGED_BEACON_INT)
3353 sc->bintval = bss_conf->beacon_int;
3354
Martin Xu02969b32008-11-24 10:49:27 +08003355 if (changes & BSS_CHANGED_ASSOC) {
Martin Xu02969b32008-11-24 10:49:27 +08003356 sc->assoc = bss_conf->assoc;
3357 if (sc->opmode == NL80211_IFTYPE_STATION)
3358 set_beacon_filter(hw, sc->assoc);
Bob Copelandf0f3d382009-06-10 22:22:21 -04003359 ath5k_hw_set_ledstate(sc->ah, sc->assoc ?
3360 AR5K_LED_ASSOC : AR5K_LED_INIT);
Luis R. Rodriguez8ce54c52009-10-06 20:44:34 -04003361 if (bss_conf->assoc) {
3362 ATH5K_DBG(sc, ATH5K_DEBUG_ANY,
3363 "Bss Info ASSOC %d, bssid: %pM\n",
3364 bss_conf->aid, common->curbssid);
3365 common->curaid = bss_conf->aid;
3366 ath5k_hw_set_associd(ah);
3367 /* Once ANI is available you would start it here */
3368 }
Martin Xu02969b32008-11-24 10:49:27 +08003369 }
Johannes Berg2d0ddec2009-04-23 16:13:26 +02003370
Bob Copeland21800492009-07-04 12:59:52 -04003371 if (changes & BSS_CHANGED_BEACON) {
3372 spin_lock_irqsave(&sc->block, flags);
3373 ath5k_beacon_update(hw, vif);
3374 spin_unlock_irqrestore(&sc->block, flags);
Johannes Berg2d0ddec2009-04-23 16:13:26 +02003375 }
3376
Bob Copeland21800492009-07-04 12:59:52 -04003377 if (changes & BSS_CHANGED_BEACON_ENABLED)
3378 sc->enable_beacon = bss_conf->enable_beacon;
3379
3380 if (changes & (BSS_CHANGED_BEACON | BSS_CHANGED_BEACON_ENABLED |
3381 BSS_CHANGED_BEACON_INT))
3382 ath5k_beacon_config(sc);
3383
Johannes Berg2d0ddec2009-04-23 16:13:26 +02003384 unlock:
3385 mutex_unlock(&sc->lock);
Martin Xu02969b32008-11-24 10:49:27 +08003386}
Bob Copelandf0f3d382009-06-10 22:22:21 -04003387
3388static void ath5k_sw_scan_start(struct ieee80211_hw *hw)
3389{
3390 struct ath5k_softc *sc = hw->priv;
3391 if (!sc->assoc)
3392 ath5k_hw_set_ledstate(sc->ah, AR5K_LED_SCAN);
3393}
3394
3395static void ath5k_sw_scan_complete(struct ieee80211_hw *hw)
3396{
3397 struct ath5k_softc *sc = hw->priv;
3398 ath5k_hw_set_ledstate(sc->ah, sc->assoc ?
3399 AR5K_LED_ASSOC : AR5K_LED_INIT);
3400}
Lukáš Turek6e08d222009-12-21 22:50:51 +01003401
3402/**
3403 * ath5k_set_coverage_class - Set IEEE 802.11 coverage class
3404 *
3405 * @hw: struct ieee80211_hw pointer
3406 * @coverage_class: IEEE 802.11 coverage class number
3407 *
3408 * Mac80211 callback. Sets slot time, ACK timeout and CTS timeout for given
3409 * coverage class. The values are persistent, they are restored after device
3410 * reset.
3411 */
3412static void ath5k_set_coverage_class(struct ieee80211_hw *hw, u8 coverage_class)
3413{
3414 struct ath5k_softc *sc = hw->priv;
3415
3416 mutex_lock(&sc->lock);
3417 ath5k_hw_set_coverage_class(sc->ah, coverage_class);
3418 mutex_unlock(&sc->lock);
3419}