blob: c9732a6ce87f6df573806046edd32c9a6c7aaaed [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>
Maxim Levitsky6ccf15a2010-08-13 11:27:28 -040051#include <linux/pci-aspm.h>
Jiri Slabyfa1c1142007-08-12 17:33:16 +020052#include <linux/ethtool.h>
53#include <linux/uaccess.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090054#include <linux/slab.h>
Ben Greearb1ae1ed2010-09-30 12:22:58 -070055#include <linux/etherdevice.h>
Jiri Slabyfa1c1142007-08-12 17:33:16 +020056
57#include <net/ieee80211_radiotap.h>
58
59#include <asm/unaligned.h>
60
61#include "base.h"
62#include "reg.h"
63#include "debug.h"
Bruno Randolf2111ac02010-04-02 18:44:08 +090064#include "ani.h"
Ben Greear62c58fb2010-10-08 12:01:15 -070065#include "../debug.h"
Jiri Slabyfa1c1142007-08-12 17:33:16 +020066
Bob Copeland9ad9a262008-10-29 08:30:54 -040067static int modparam_nohwcrypt;
Bob Copeland46802a42009-04-15 07:57:34 -040068module_param_named(nohwcrypt, modparam_nohwcrypt, bool, S_IRUGO);
Bob Copeland9ad9a262008-10-29 08:30:54 -040069MODULE_PARM_DESC(nohwcrypt, "Disable hardware encryption.");
Jiri Slabyfa1c1142007-08-12 17:33:16 +020070
Bob Copeland42639fc2009-03-30 08:05:29 -040071static int modparam_all_channels;
Bob Copeland46802a42009-04-15 07:57:34 -040072module_param_named(all_channels, modparam_all_channels, bool, S_IRUGO);
Bob Copeland42639fc2009-03-30 08:05:29 -040073MODULE_PARM_DESC(all_channels, "Expose all channels the device can use.");
74
Jiri Slabyfa1c1142007-08-12 17:33:16 +020075/* 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
Bob Copeland8a63fac2010-09-17 12:45:07 +090083static int ath5k_reset(struct ath5k_softc *sc, struct ieee80211_channel *chan);
84static int ath5k_beacon_update(struct ieee80211_hw *hw,
85 struct ieee80211_vif *vif);
86static void ath5k_beacon_update_timers(struct ath5k_softc *sc, u64 bc_tsf);
Jiri Slabyfa1c1142007-08-12 17:33:16 +020087
88/* Known PCI ids */
Alexey Dobriyana3aa1882010-01-07 11:58:11 +000089static DEFINE_PCI_DEVICE_TABLE(ath5k_pci_id_table) = {
Pavel Roskin97a81f52009-08-26 22:30:09 -040090 { PCI_VDEVICE(ATHEROS, 0x0207) }, /* 5210 early */
91 { PCI_VDEVICE(ATHEROS, 0x0007) }, /* 5210 */
92 { PCI_VDEVICE(ATHEROS, 0x0011) }, /* 5311 - this is on AHB bus !*/
93 { PCI_VDEVICE(ATHEROS, 0x0012) }, /* 5211 */
94 { PCI_VDEVICE(ATHEROS, 0x0013) }, /* 5212 */
95 { PCI_VDEVICE(3COM_2, 0x0013) }, /* 3com 5212 */
96 { PCI_VDEVICE(3COM, 0x0013) }, /* 3com 3CRDAG675 5212 */
97 { PCI_VDEVICE(ATHEROS, 0x1014) }, /* IBM minipci 5212 */
98 { PCI_VDEVICE(ATHEROS, 0x0014) }, /* 5212 combatible */
99 { PCI_VDEVICE(ATHEROS, 0x0015) }, /* 5212 combatible */
100 { PCI_VDEVICE(ATHEROS, 0x0016) }, /* 5212 combatible */
101 { PCI_VDEVICE(ATHEROS, 0x0017) }, /* 5212 combatible */
102 { PCI_VDEVICE(ATHEROS, 0x0018) }, /* 5212 combatible */
103 { PCI_VDEVICE(ATHEROS, 0x0019) }, /* 5212 combatible */
104 { PCI_VDEVICE(ATHEROS, 0x001a) }, /* 2413 Griffin-lite */
105 { PCI_VDEVICE(ATHEROS, 0x001b) }, /* 5413 Eagle */
106 { PCI_VDEVICE(ATHEROS, 0x001c) }, /* PCI-E cards */
107 { PCI_VDEVICE(ATHEROS, 0x001d) }, /* 2417 Nala */
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200108 { 0 }
109};
110MODULE_DEVICE_TABLE(pci, ath5k_pci_id_table);
111
112/* Known SREVs */
Jiri Slaby2c91108c2009-03-07 10:26:41 +0100113static const struct ath5k_srev_name srev_names[] = {
Nick Kossifidis1bef0162008-09-29 02:09:09 +0300114 { "5210", AR5K_VERSION_MAC, AR5K_SREV_AR5210 },
115 { "5311", AR5K_VERSION_MAC, AR5K_SREV_AR5311 },
116 { "5311A", AR5K_VERSION_MAC, AR5K_SREV_AR5311A },
117 { "5311B", AR5K_VERSION_MAC, AR5K_SREV_AR5311B },
118 { "5211", AR5K_VERSION_MAC, AR5K_SREV_AR5211 },
119 { "5212", AR5K_VERSION_MAC, AR5K_SREV_AR5212 },
120 { "5213", AR5K_VERSION_MAC, AR5K_SREV_AR5213 },
121 { "5213A", AR5K_VERSION_MAC, AR5K_SREV_AR5213A },
122 { "2413", AR5K_VERSION_MAC, AR5K_SREV_AR2413 },
123 { "2414", AR5K_VERSION_MAC, AR5K_SREV_AR2414 },
124 { "5424", AR5K_VERSION_MAC, AR5K_SREV_AR5424 },
125 { "5413", AR5K_VERSION_MAC, AR5K_SREV_AR5413 },
126 { "5414", AR5K_VERSION_MAC, AR5K_SREV_AR5414 },
127 { "2415", AR5K_VERSION_MAC, AR5K_SREV_AR2415 },
128 { "5416", AR5K_VERSION_MAC, AR5K_SREV_AR5416 },
129 { "5418", AR5K_VERSION_MAC, AR5K_SREV_AR5418 },
130 { "2425", AR5K_VERSION_MAC, AR5K_SREV_AR2425 },
131 { "2417", AR5K_VERSION_MAC, AR5K_SREV_AR2417 },
132 { "xxxxx", AR5K_VERSION_MAC, AR5K_SREV_UNKNOWN },
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200133 { "5110", AR5K_VERSION_RAD, AR5K_SREV_RAD_5110 },
134 { "5111", AR5K_VERSION_RAD, AR5K_SREV_RAD_5111 },
Nick Kossifidis1bef0162008-09-29 02:09:09 +0300135 { "5111A", AR5K_VERSION_RAD, AR5K_SREV_RAD_5111A },
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200136 { "2111", AR5K_VERSION_RAD, AR5K_SREV_RAD_2111 },
137 { "5112", AR5K_VERSION_RAD, AR5K_SREV_RAD_5112 },
138 { "5112A", AR5K_VERSION_RAD, AR5K_SREV_RAD_5112A },
Nick Kossifidis1bef0162008-09-29 02:09:09 +0300139 { "5112B", AR5K_VERSION_RAD, AR5K_SREV_RAD_5112B },
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200140 { "2112", AR5K_VERSION_RAD, AR5K_SREV_RAD_2112 },
141 { "2112A", AR5K_VERSION_RAD, AR5K_SREV_RAD_2112A },
Nick Kossifidis1bef0162008-09-29 02:09:09 +0300142 { "2112B", AR5K_VERSION_RAD, AR5K_SREV_RAD_2112B },
143 { "2413", AR5K_VERSION_RAD, AR5K_SREV_RAD_2413 },
144 { "5413", AR5K_VERSION_RAD, AR5K_SREV_RAD_5413 },
145 { "2316", AR5K_VERSION_RAD, AR5K_SREV_RAD_2316 },
146 { "2317", AR5K_VERSION_RAD, AR5K_SREV_RAD_2317 },
147 { "5424", AR5K_VERSION_RAD, AR5K_SREV_RAD_5424 },
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200148 { "5133", AR5K_VERSION_RAD, AR5K_SREV_RAD_5133 },
149 { "xxxxx", AR5K_VERSION_RAD, AR5K_SREV_UNKNOWN },
150};
151
Jiri Slaby2c91108c2009-03-07 10:26:41 +0100152static const struct ieee80211_rate ath5k_rates[] = {
Bruno Randolf63266a62008-07-30 17:12:58 +0200153 { .bitrate = 10,
154 .hw_value = ATH5K_RATE_CODE_1M, },
155 { .bitrate = 20,
156 .hw_value = ATH5K_RATE_CODE_2M,
157 .hw_value_short = ATH5K_RATE_CODE_2M | AR5K_SET_SHORT_PREAMBLE,
158 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
159 { .bitrate = 55,
160 .hw_value = ATH5K_RATE_CODE_5_5M,
161 .hw_value_short = ATH5K_RATE_CODE_5_5M | AR5K_SET_SHORT_PREAMBLE,
162 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
163 { .bitrate = 110,
164 .hw_value = ATH5K_RATE_CODE_11M,
165 .hw_value_short = ATH5K_RATE_CODE_11M | AR5K_SET_SHORT_PREAMBLE,
166 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
167 { .bitrate = 60,
168 .hw_value = ATH5K_RATE_CODE_6M,
169 .flags = 0 },
170 { .bitrate = 90,
171 .hw_value = ATH5K_RATE_CODE_9M,
172 .flags = 0 },
173 { .bitrate = 120,
174 .hw_value = ATH5K_RATE_CODE_12M,
175 .flags = 0 },
176 { .bitrate = 180,
177 .hw_value = ATH5K_RATE_CODE_18M,
178 .flags = 0 },
179 { .bitrate = 240,
180 .hw_value = ATH5K_RATE_CODE_24M,
181 .flags = 0 },
182 { .bitrate = 360,
183 .hw_value = ATH5K_RATE_CODE_36M,
184 .flags = 0 },
185 { .bitrate = 480,
186 .hw_value = ATH5K_RATE_CODE_48M,
187 .flags = 0 },
188 { .bitrate = 540,
189 .hw_value = ATH5K_RATE_CODE_54M,
190 .flags = 0 },
191 /* XR missing */
192};
193
Bruno Randolf9e4e43f2010-06-16 19:11:17 +0900194static inline void ath5k_txbuf_free_skb(struct ath5k_softc *sc,
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200195 struct ath5k_buf *bf)
196{
197 BUG_ON(!bf);
198 if (!bf->skb)
199 return;
200 pci_unmap_single(sc->pdev, bf->skbaddr, bf->skb->len,
201 PCI_DMA_TODEVICE);
Jiri Slaby00482972008-08-18 21:45:27 +0200202 dev_kfree_skb_any(bf->skb);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200203 bf->skb = NULL;
Bruno Randolf39d63f22010-06-16 19:11:41 +0900204 bf->skbaddr = 0;
205 bf->desc->ds_data = 0;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200206}
207
Bruno Randolf9e4e43f2010-06-16 19:11:17 +0900208static inline void ath5k_rxbuf_free_skb(struct ath5k_softc *sc,
Felix Fietkaua6c8d372009-01-30 01:36:48 +0100209 struct ath5k_buf *bf)
210{
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -0800211 struct ath5k_hw *ah = sc->ah;
212 struct ath_common *common = ath5k_hw_common(ah);
213
Felix Fietkaua6c8d372009-01-30 01:36:48 +0100214 BUG_ON(!bf);
215 if (!bf->skb)
216 return;
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -0800217 pci_unmap_single(sc->pdev, bf->skbaddr, common->rx_bufsize,
Felix Fietkaua6c8d372009-01-30 01:36:48 +0100218 PCI_DMA_FROMDEVICE);
219 dev_kfree_skb_any(bf->skb);
220 bf->skb = NULL;
Bruno Randolf39d63f22010-06-16 19:11:41 +0900221 bf->skbaddr = 0;
222 bf->desc->ds_data = 0;
Felix Fietkaua6c8d372009-01-30 01:36:48 +0100223}
224
225
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200226static inline u64 ath5k_extend_tsf(struct ath5k_hw *ah, u32 rstamp)
227{
228 u64 tsf = ath5k_hw_get_tsf64(ah);
229
230 if ((tsf & 0x7fff) < rstamp)
231 tsf -= 0x8000;
232
233 return (tsf & ~0x7fff) | rstamp;
234}
235
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200236static const char *
237ath5k_chip_name(enum ath5k_srev_type type, u_int16_t val)
238{
239 const char *name = "xxxxx";
240 unsigned int i;
241
242 for (i = 0; i < ARRAY_SIZE(srev_names); i++) {
243 if (srev_names[i].sr_type != type)
244 continue;
Nick Kossifidis75d0edb2008-09-29 01:24:44 +0300245
246 if ((val & 0xf0) == srev_names[i].sr_val)
247 name = srev_names[i].sr_name;
248
249 if ((val & 0xff) == srev_names[i].sr_val) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200250 name = srev_names[i].sr_name;
251 break;
252 }
253 }
254
255 return name;
256}
Luis R. Rodrigueze5aa8472009-09-10 16:55:11 -0700257static unsigned int ath5k_ioread32(void *hw_priv, u32 reg_offset)
258{
259 struct ath5k_hw *ah = (struct ath5k_hw *) hw_priv;
260 return ath5k_hw_reg_read(ah, reg_offset);
261}
262
263static void ath5k_iowrite32(void *hw_priv, u32 val, u32 reg_offset)
264{
265 struct ath5k_hw *ah = (struct ath5k_hw *) hw_priv;
266 ath5k_hw_reg_write(ah, val, reg_offset);
267}
268
269static const struct ath_ops ath5k_common_ops = {
270 .read = ath5k_ioread32,
271 .write = ath5k_iowrite32,
272};
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200273
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200274/***********************\
275* Driver Initialization *
276\***********************/
277
Bob Copelandf769c362009-03-30 22:30:31 -0400278static int ath5k_reg_notifier(struct wiphy *wiphy, struct regulatory_request *request)
279{
280 struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
281 struct ath5k_softc *sc = hw->priv;
Luis R. Rodriguezdb719712009-09-10 11:20:57 -0700282 struct ath_regulatory *regulatory = ath5k_hw_regulatory(sc->ah);
Bob Copelandf769c362009-03-30 22:30:31 -0400283
Luis R. Rodriguez608b88c2009-08-17 18:07:23 -0700284 return ath_reg_notifier_apply(wiphy, request, regulatory);
Bob Copelandf769c362009-03-30 22:30:31 -0400285}
286
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200287/********************\
288* Channel/mode setup *
289\********************/
290
291/*
292 * Convert IEEE channel number to MHz frequency.
293 */
294static inline short
295ath5k_ieee2mhz(short chan)
296{
297 if (chan <= 14 || chan >= 27)
298 return ieee80211chan2mhz(chan);
299 else
300 return 2212 + chan * 20;
301}
302
Bob Copeland42639fc2009-03-30 08:05:29 -0400303/*
304 * Returns true for the channel numbers used without all_channels modparam.
305 */
306static bool ath5k_is_standard_channel(short chan)
307{
308 return ((chan <= 14) ||
309 /* UNII 1,2 */
310 ((chan & 3) == 0 && chan >= 36 && chan <= 64) ||
311 /* midband */
312 ((chan & 3) == 0 && chan >= 100 && chan <= 140) ||
313 /* UNII-3 */
314 ((chan & 3) == 1 && chan >= 149 && chan <= 165));
315}
316
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200317static unsigned int
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200318ath5k_copy_channels(struct ath5k_hw *ah,
319 struct ieee80211_channel *channels,
320 unsigned int mode,
321 unsigned int max)
322{
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500323 unsigned int i, count, size, chfreq, freq, ch;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200324
325 if (!test_bit(mode, ah->ah_modes))
326 return 0;
327
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200328 switch (mode) {
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500329 case AR5K_MODE_11A:
330 case AR5K_MODE_11A_TURBO:
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200331 /* 1..220, but 2GHz frequencies are filtered by check_channel */
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500332 size = 220 ;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200333 chfreq = CHANNEL_5GHZ;
334 break;
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500335 case AR5K_MODE_11B:
336 case AR5K_MODE_11G:
337 case AR5K_MODE_11G_TURBO:
338 size = 26;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200339 chfreq = CHANNEL_2GHZ;
340 break;
341 default:
342 ATH5K_WARN(ah->ah_sc, "bad mode, not copying channels\n");
343 return 0;
344 }
345
346 for (i = 0, count = 0; i < size && max > 0; i++) {
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500347 ch = i + 1 ;
348 freq = ath5k_ieee2mhz(ch);
349
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200350 /* Check if channel is supported by the chipset */
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500351 if (!ath5k_channel_ok(ah, freq, chfreq))
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200352 continue;
353
Bob Copeland42639fc2009-03-30 08:05:29 -0400354 if (!modparam_all_channels && !ath5k_is_standard_channel(ch))
355 continue;
356
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500357 /* Write channel info and increment counter */
358 channels[count].center_freq = freq;
Luis R. Rodrigueza3f4b912008-02-03 21:52:10 -0500359 channels[count].band = (chfreq == CHANNEL_2GHZ) ?
360 IEEE80211_BAND_2GHZ : IEEE80211_BAND_5GHZ;
Luis R. Rodriguez400ec452008-02-03 21:51:49 -0500361 switch (mode) {
362 case AR5K_MODE_11A:
363 case AR5K_MODE_11G:
364 channels[count].hw_value = chfreq | CHANNEL_OFDM;
365 break;
366 case AR5K_MODE_11A_TURBO:
367 case AR5K_MODE_11G_TURBO:
368 channels[count].hw_value = chfreq |
369 CHANNEL_OFDM | CHANNEL_TURBO;
370 break;
371 case AR5K_MODE_11B:
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500372 channels[count].hw_value = CHANNEL_B;
373 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200374
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200375 count++;
376 max--;
377 }
378
379 return count;
380}
381
Bruno Randolf63266a62008-07-30 17:12:58 +0200382static void
383ath5k_setup_rate_idx(struct ath5k_softc *sc, struct ieee80211_supported_band *b)
384{
385 u8 i;
386
387 for (i = 0; i < AR5K_MAX_RATES; i++)
388 sc->rate_idx[b->band][i] = -1;
389
390 for (i = 0; i < b->n_bitrates; i++) {
391 sc->rate_idx[b->band][b->bitrates[i].hw_value] = i;
392 if (b->bitrates[i].hw_value_short)
393 sc->rate_idx[b->band][b->bitrates[i].hw_value_short] = i;
394 }
395}
396
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200397static int
Bruno Randolf63266a62008-07-30 17:12:58 +0200398ath5k_setup_bands(struct ieee80211_hw *hw)
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200399{
400 struct ath5k_softc *sc = hw->priv;
401 struct ath5k_hw *ah = sc->ah;
Bruno Randolf63266a62008-07-30 17:12:58 +0200402 struct ieee80211_supported_band *sband;
403 int max_c, count_c = 0;
404 int i;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200405
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500406 BUILD_BUG_ON(ARRAY_SIZE(sc->sbands) < IEEE80211_NUM_BANDS);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200407 max_c = ARRAY_SIZE(sc->channels);
408
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500409 /* 2GHz band */
Bruno Randolf63266a62008-07-30 17:12:58 +0200410 sband = &sc->sbands[IEEE80211_BAND_2GHZ];
411 sband->band = IEEE80211_BAND_2GHZ;
412 sband->bitrates = &sc->rates[IEEE80211_BAND_2GHZ][0];
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200413
Bruno Randolf63266a62008-07-30 17:12:58 +0200414 if (test_bit(AR5K_MODE_11G, sc->ah->ah_capabilities.cap_mode)) {
415 /* G mode */
416 memcpy(sband->bitrates, &ath5k_rates[0],
417 sizeof(struct ieee80211_rate) * 12);
418 sband->n_bitrates = 12;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200419
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500420 sband->channels = sc->channels;
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500421 sband->n_channels = ath5k_copy_channels(ah, sband->channels,
Bruno Randolf63266a62008-07-30 17:12:58 +0200422 AR5K_MODE_11G, max_c);
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500423
424 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = sband;
Bruno Randolf63266a62008-07-30 17:12:58 +0200425 count_c = sband->n_channels;
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500426 max_c -= count_c;
Bruno Randolf63266a62008-07-30 17:12:58 +0200427 } else if (test_bit(AR5K_MODE_11B, sc->ah->ah_capabilities.cap_mode)) {
428 /* B mode */
429 memcpy(sband->bitrates, &ath5k_rates[0],
430 sizeof(struct ieee80211_rate) * 4);
431 sband->n_bitrates = 4;
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500432
Bruno Randolf63266a62008-07-30 17:12:58 +0200433 /* 5211 only supports B rates and uses 4bit rate codes
434 * (e.g normally we have 0x1B for 1M, but on 5211 we have 0x0B)
435 * fix them up here:
436 */
437 if (ah->ah_version == AR5K_AR5211) {
438 for (i = 0; i < 4; i++) {
439 sband->bitrates[i].hw_value =
440 sband->bitrates[i].hw_value & 0xF;
441 sband->bitrates[i].hw_value_short =
442 sband->bitrates[i].hw_value_short & 0xF;
443 }
444 }
445
446 sband->channels = sc->channels;
447 sband->n_channels = ath5k_copy_channels(ah, sband->channels,
448 AR5K_MODE_11B, max_c);
449
450 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = sband;
451 count_c = sband->n_channels;
452 max_c -= count_c;
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500453 }
Bruno Randolf63266a62008-07-30 17:12:58 +0200454 ath5k_setup_rate_idx(sc, sband);
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500455
Bruno Randolf63266a62008-07-30 17:12:58 +0200456 /* 5GHz band, A mode */
Luis R. Rodriguez400ec452008-02-03 21:51:49 -0500457 if (test_bit(AR5K_MODE_11A, sc->ah->ah_capabilities.cap_mode)) {
Bruno Randolf63266a62008-07-30 17:12:58 +0200458 sband = &sc->sbands[IEEE80211_BAND_5GHZ];
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500459 sband->band = IEEE80211_BAND_5GHZ;
Bruno Randolf63266a62008-07-30 17:12:58 +0200460 sband->bitrates = &sc->rates[IEEE80211_BAND_5GHZ][0];
461
462 memcpy(sband->bitrates, &ath5k_rates[4],
463 sizeof(struct ieee80211_rate) * 8);
464 sband->n_bitrates = 8;
465
466 sband->channels = &sc->channels[count_c];
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500467 sband->n_channels = ath5k_copy_channels(ah, sband->channels,
468 AR5K_MODE_11A, max_c);
469
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500470 hw->wiphy->bands[IEEE80211_BAND_5GHZ] = sband;
471 }
Bruno Randolf63266a62008-07-30 17:12:58 +0200472 ath5k_setup_rate_idx(sc, sband);
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500473
Luis R. Rodriguezb4461972008-02-04 10:03:54 -0500474 ath5k_debug_dump_bands(sc);
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500475
476 return 0;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200477}
478
479/*
Joerg Alberte30eb4a2009-08-05 01:52:07 +0200480 * Set/change channels. We always reset the chip.
481 * To accomplish this we must first cleanup any pending DMA,
482 * then restart stuff after a la ath5k_init.
Bob Copelandbe009372009-01-22 08:44:16 -0500483 *
484 * Called with sc->lock.
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200485 */
486static int
487ath5k_chan_set(struct ath5k_softc *sc, struct ieee80211_channel *chan)
488{
Bruno Randolf8d67a032010-06-16 19:11:12 +0900489 ATH5K_DBG(sc, ATH5K_DEBUG_RESET,
490 "channel set, resetting (%u -> %u MHz)\n",
491 sc->curchan->center_freq, chan->center_freq);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200492
Joerg Alberte30eb4a2009-08-05 01:52:07 +0200493 /*
494 * To switch channels clear any pending DMA operations;
495 * wait long enough for the RX fifo to drain, reset the
496 * hardware at the new frequency, and then re-enable
497 * the relevant bits of the h/w.
498 */
499 return ath5k_reset(sc, chan);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200500}
501
502static void
503ath5k_setcurmode(struct ath5k_softc *sc, unsigned int mode)
504{
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200505 sc->curmode = mode;
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500506
Luis R. Rodriguez400ec452008-02-03 21:51:49 -0500507 if (mode == AR5K_MODE_11A) {
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500508 sc->curband = &sc->sbands[IEEE80211_BAND_5GHZ];
509 } else {
510 sc->curband = &sc->sbands[IEEE80211_BAND_2GHZ];
511 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200512}
513
Ben Greearb1ae1ed2010-09-30 12:22:58 -0700514struct ath_vif_iter_data {
515 const u8 *hw_macaddr;
516 u8 mask[ETH_ALEN];
517 u8 active_mac[ETH_ALEN]; /* first active MAC */
518 bool need_set_hw_addr;
519 bool found_active;
520 bool any_assoc;
Ben Greear62c58fb2010-10-08 12:01:15 -0700521 enum nl80211_iftype opmode;
Ben Greearb1ae1ed2010-09-30 12:22:58 -0700522};
523
524static void ath_vif_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
525{
526 struct ath_vif_iter_data *iter_data = data;
527 int i;
Ben Greear62c58fb2010-10-08 12:01:15 -0700528 struct ath5k_vif *avf = (void *)vif->drv_priv;
Ben Greearb1ae1ed2010-09-30 12:22:58 -0700529
530 if (iter_data->hw_macaddr)
531 for (i = 0; i < ETH_ALEN; i++)
532 iter_data->mask[i] &=
533 ~(iter_data->hw_macaddr[i] ^ mac[i]);
534
535 if (!iter_data->found_active) {
536 iter_data->found_active = true;
537 memcpy(iter_data->active_mac, mac, ETH_ALEN);
538 }
539
540 if (iter_data->need_set_hw_addr && iter_data->hw_macaddr)
541 if (compare_ether_addr(iter_data->hw_macaddr, mac) == 0)
542 iter_data->need_set_hw_addr = false;
543
544 if (!iter_data->any_assoc) {
Ben Greearb1ae1ed2010-09-30 12:22:58 -0700545 if (avf->assoc)
546 iter_data->any_assoc = true;
547 }
Ben Greear62c58fb2010-10-08 12:01:15 -0700548
549 /* Calculate combined mode - when APs are active, operate in AP mode.
550 * Otherwise use the mode of the new interface. This can currently
551 * only deal with combinations of APs and STAs. Only one ad-hoc
552 * interfaces is allowed above.
553 */
554 if (avf->opmode == NL80211_IFTYPE_AP)
555 iter_data->opmode = NL80211_IFTYPE_AP;
556 else
557 if (iter_data->opmode == NL80211_IFTYPE_UNSPECIFIED)
558 iter_data->opmode = avf->opmode;
Ben Greearb1ae1ed2010-09-30 12:22:58 -0700559}
560
Ben Greear62c58fb2010-10-08 12:01:15 -0700561static void ath_do_set_opmode(struct ath5k_softc *sc)
562{
563 struct ath5k_hw *ah = sc->ah;
564 ath5k_hw_set_opmode(ah, sc->opmode);
565 ATH5K_DBG(sc, ATH5K_DEBUG_MODE, "mode setup opmode %d (%s)\n",
566 sc->opmode,
567 ath_opmode_to_string(sc->opmode) ?
568 ath_opmode_to_string(sc->opmode) : "UKNOWN");
569}
570
571void ath5k_update_bssid_mask_and_opmode(struct ath5k_softc *sc,
572 struct ieee80211_vif *vif)
Ben Greearb1ae1ed2010-09-30 12:22:58 -0700573{
574 struct ath_common *common = ath5k_hw_common(sc->ah);
575 struct ath_vif_iter_data iter_data;
576
577 /*
578 * Use the hardware MAC address as reference, the hardware uses it
579 * together with the BSSID mask when matching addresses.
580 */
581 iter_data.hw_macaddr = common->macaddr;
582 memset(&iter_data.mask, 0xff, ETH_ALEN);
583 iter_data.found_active = false;
584 iter_data.need_set_hw_addr = true;
Ben Greear62c58fb2010-10-08 12:01:15 -0700585 iter_data.opmode = NL80211_IFTYPE_UNSPECIFIED;
Ben Greearb1ae1ed2010-09-30 12:22:58 -0700586
587 if (vif)
588 ath_vif_iter(&iter_data, vif->addr, vif);
589
590 /* Get list of all active MAC addresses */
591 ieee80211_iterate_active_interfaces_atomic(sc->hw, ath_vif_iter,
592 &iter_data);
593 memcpy(sc->bssidmask, iter_data.mask, ETH_ALEN);
594
Ben Greear62c58fb2010-10-08 12:01:15 -0700595 sc->opmode = iter_data.opmode;
596 if (sc->opmode == NL80211_IFTYPE_UNSPECIFIED)
597 /* Nothing active, default to station mode */
598 sc->opmode = NL80211_IFTYPE_STATION;
599
600 ath_do_set_opmode(sc);
601
Ben Greearb1ae1ed2010-09-30 12:22:58 -0700602 if (iter_data.need_set_hw_addr && iter_data.found_active)
603 ath5k_hw_set_lladdr(sc->ah, iter_data.active_mac);
604
Ben Greear62c58fb2010-10-08 12:01:15 -0700605 if (ath5k_hw_hasbssidmask(sc->ah))
606 ath5k_hw_set_bssid_mask(sc->ah, sc->bssidmask);
Ben Greearb1ae1ed2010-09-30 12:22:58 -0700607}
608
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200609static void
Ben Greearb1ae1ed2010-09-30 12:22:58 -0700610ath5k_mode_setup(struct ath5k_softc *sc, struct ieee80211_vif *vif)
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200611{
612 struct ath5k_hw *ah = sc->ah;
613 u32 rfilt;
614
615 /* configure rx filter */
616 rfilt = sc->filter_flags;
617 ath5k_hw_set_rx_filter(ah, rfilt);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200618 ATH5K_DBG(sc, ATH5K_DEBUG_MODE, "RX filter 0x%x\n", rfilt);
Ben Greear62c58fb2010-10-08 12:01:15 -0700619
620 ath5k_update_bssid_mask_and_opmode(sc, vif);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200621}
622
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500623static inline int
Bruno Randolf63266a62008-07-30 17:12:58 +0200624ath5k_hw_to_driver_rix(struct ath5k_softc *sc, int hw_rix)
625{
Bob Copelandb7266042009-03-02 21:55:18 -0500626 int rix;
627
628 /* return base rate on errors */
629 if (WARN(hw_rix < 0 || hw_rix >= AR5K_MAX_RATES,
630 "hw_rix out of bounds: %x\n", hw_rix))
631 return 0;
632
633 rix = sc->rate_idx[sc->curband->band][hw_rix];
634 if (WARN(rix < 0, "invalid hw_rix: %x\n", hw_rix))
635 rix = 0;
636
637 return rix;
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500638}
639
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200640/***************\
641* Buffers setup *
642\***************/
643
Bob Copelandb6ea0352009-01-10 14:42:54 -0500644static
645struct sk_buff *ath5k_rx_skb_alloc(struct ath5k_softc *sc, dma_addr_t *skb_addr)
646{
Luis R. Rodriguezdb719712009-09-10 11:20:57 -0700647 struct ath_common *common = ath5k_hw_common(sc->ah);
Bob Copelandb6ea0352009-01-10 14:42:54 -0500648 struct sk_buff *skb;
Bob Copelandb6ea0352009-01-10 14:42:54 -0500649
650 /*
651 * Allocate buffer with headroom_needed space for the
652 * fake physical layer header at the start.
653 */
Luis R. Rodriguezdb719712009-09-10 11:20:57 -0700654 skb = ath_rxbuf_alloc(common,
Luis R. Rodriguezdd849782009-11-04 09:44:50 -0800655 common->rx_bufsize,
Luis R. Rodriguezaeb63cf2009-08-12 09:57:00 -0700656 GFP_ATOMIC);
Bob Copelandb6ea0352009-01-10 14:42:54 -0500657
658 if (!skb) {
659 ATH5K_ERR(sc, "can't alloc skbuff of size %u\n",
Luis R. Rodriguezdd849782009-11-04 09:44:50 -0800660 common->rx_bufsize);
Bob Copelandb6ea0352009-01-10 14:42:54 -0500661 return NULL;
662 }
Bob Copelandb6ea0352009-01-10 14:42:54 -0500663
664 *skb_addr = pci_map_single(sc->pdev,
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -0800665 skb->data, common->rx_bufsize,
666 PCI_DMA_FROMDEVICE);
Bob Copelandb6ea0352009-01-10 14:42:54 -0500667 if (unlikely(pci_dma_mapping_error(sc->pdev, *skb_addr))) {
668 ATH5K_ERR(sc, "%s: DMA mapping failed\n", __func__);
669 dev_kfree_skb(skb);
670 return NULL;
671 }
672 return skb;
673}
674
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200675static int
676ath5k_rxbuf_setup(struct ath5k_softc *sc, struct ath5k_buf *bf)
677{
678 struct ath5k_hw *ah = sc->ah;
679 struct sk_buff *skb = bf->skb;
680 struct ath5k_desc *ds;
Bruno Randolfb5eae9f2010-05-19 10:18:16 +0900681 int ret;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200682
Bob Copelandb6ea0352009-01-10 14:42:54 -0500683 if (!skb) {
684 skb = ath5k_rx_skb_alloc(sc, &bf->skbaddr);
685 if (!skb)
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200686 return -ENOMEM;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200687 bf->skb = skb;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200688 }
689
690 /*
691 * Setup descriptors. For receive we always terminate
692 * the descriptor list with a self-linked entry so we'll
693 * not get overrun under high load (as can happen with a
694 * 5212 when ANI processing enables PHY error frames).
695 *
Bruno Randolfbeade632010-06-16 19:11:25 +0900696 * To ensure the last descriptor is self-linked we create
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200697 * each descriptor as self-linked and add it to the end. As
698 * each additional descriptor is added the previous self-linked
Bruno Randolfbeade632010-06-16 19:11:25 +0900699 * entry is "fixed" naturally. This should be safe even
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200700 * if DMA is happening. When processing RX interrupts we
701 * never remove/process the last, self-linked, entry on the
Bruno Randolfbeade632010-06-16 19:11:25 +0900702 * descriptor list. This ensures the hardware always has
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200703 * someplace to write a new frame.
704 */
705 ds = bf->desc;
706 ds->ds_link = bf->daddr; /* link to self */
707 ds->ds_data = bf->skbaddr;
Bruno Randolfa6668192010-06-16 19:12:01 +0900708 ret = ath5k_hw_setup_rx_desc(ah, ds, ah->common.rx_bufsize, 0);
Bruno Randolf0452d4a2010-06-16 19:11:35 +0900709 if (ret) {
710 ATH5K_ERR(sc, "%s: could not setup RX desc\n", __func__);
Bruno Randolfb5eae9f2010-05-19 10:18:16 +0900711 return ret;
Bruno Randolf0452d4a2010-06-16 19:11:35 +0900712 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200713
714 if (sc->rxlink != NULL)
715 *sc->rxlink = bf->daddr;
716 sc->rxlink = &ds->ds_link;
717 return 0;
718}
719
Bob Copeland2ac29272010-02-09 13:06:54 -0500720static enum ath5k_pkt_type get_hw_packet_type(struct sk_buff *skb)
721{
722 struct ieee80211_hdr *hdr;
723 enum ath5k_pkt_type htype;
724 __le16 fc;
725
726 hdr = (struct ieee80211_hdr *)skb->data;
727 fc = hdr->frame_control;
728
729 if (ieee80211_is_beacon(fc))
730 htype = AR5K_PKT_TYPE_BEACON;
731 else if (ieee80211_is_probe_resp(fc))
732 htype = AR5K_PKT_TYPE_PROBE_RESP;
733 else if (ieee80211_is_atim(fc))
734 htype = AR5K_PKT_TYPE_ATIM;
735 else if (ieee80211_is_pspoll(fc))
736 htype = AR5K_PKT_TYPE_PSPOLL;
737 else
738 htype = AR5K_PKT_TYPE_NORMAL;
739
740 return htype;
741}
742
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200743static int
Bob Copelandcec8db22009-07-04 12:59:51 -0400744ath5k_txbuf_setup(struct ath5k_softc *sc, struct ath5k_buf *bf,
Benoit Papillault8127fbd2010-02-27 23:05:26 +0100745 struct ath5k_txq *txq, int padsize)
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200746{
747 struct ath5k_hw *ah = sc->ah;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200748 struct ath5k_desc *ds = bf->desc;
749 struct sk_buff *skb = bf->skb;
Johannes Berga888d522008-05-26 16:43:39 +0200750 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200751 unsigned int pktlen, flags, keyidx = AR5K_TXKEYIX_INVALID;
Felix Fietkau2f7fe872008-10-05 18:05:48 +0200752 struct ieee80211_rate *rate;
753 unsigned int mrr_rate[3], mrr_tries[3];
754 int i, ret;
Bob Copeland8902ff42009-01-22 08:44:20 -0500755 u16 hw_rate;
Bob Copeland07c1e852009-01-22 08:44:21 -0500756 u16 cts_rate = 0;
757 u16 duration = 0;
Bob Copeland8902ff42009-01-22 08:44:20 -0500758 u8 rc_flags;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200759
760 flags = AR5K_TXDESC_INTREQ | AR5K_TXDESC_CLRDMASK;
Johannes Berge039fa42008-05-15 12:55:29 +0200761
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200762 /* XXX endianness */
763 bf->skbaddr = pci_map_single(sc->pdev, skb->data, skb->len,
764 PCI_DMA_TODEVICE);
765
Bob Copeland8902ff42009-01-22 08:44:20 -0500766 rate = ieee80211_get_tx_rate(sc->hw, info);
John W. Linvilled8e1ba72010-08-24 15:27:34 -0400767 if (!rate) {
768 ret = -EINVAL;
769 goto err_unmap;
770 }
Bob Copeland8902ff42009-01-22 08:44:20 -0500771
Johannes Berge039fa42008-05-15 12:55:29 +0200772 if (info->flags & IEEE80211_TX_CTL_NO_ACK)
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200773 flags |= AR5K_TXDESC_NOACK;
774
Bob Copeland8902ff42009-01-22 08:44:20 -0500775 rc_flags = info->control.rates[0].flags;
776 hw_rate = (rc_flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE) ?
777 rate->hw_value_short : rate->hw_value;
778
Bruno Randolf281c56d2008-02-05 18:44:55 +0900779 pktlen = skb->len;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200780
Nick Kossifidis8f655dd2009-03-15 22:20:35 +0200781 /* FIXME: If we are in g mode and rate is a CCK rate
782 * subtract ah->ah_txpower.txp_cck_ofdm_pwr_delta
783 * from tx power (value is in dB units already) */
Bob Copeland362695e2009-02-15 12:06:12 -0500784 if (info->control.hw_key) {
785 keyidx = info->control.hw_key->hw_key_idx;
786 pktlen += info->control.hw_key->icv_len;
787 }
Bob Copeland07c1e852009-01-22 08:44:21 -0500788 if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
789 flags |= AR5K_TXDESC_RTSENA;
790 cts_rate = ieee80211_get_rts_cts_rate(sc->hw, info)->hw_value;
791 duration = le16_to_cpu(ieee80211_rts_duration(sc->hw,
Ben Greearb1ae1ed2010-09-30 12:22:58 -0700792 info->control.vif, pktlen, info));
Bob Copeland07c1e852009-01-22 08:44:21 -0500793 }
794 if (rc_flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
795 flags |= AR5K_TXDESC_CTSENA;
796 cts_rate = ieee80211_get_rts_cts_rate(sc->hw, info)->hw_value;
797 duration = le16_to_cpu(ieee80211_ctstoself_duration(sc->hw,
Ben Greearb1ae1ed2010-09-30 12:22:58 -0700798 info->control.vif, pktlen, info));
Bob Copeland07c1e852009-01-22 08:44:21 -0500799 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200800 ret = ah->ah_setup_tx_desc(ah, ds, pktlen,
Benoit Papillault8127fbd2010-02-27 23:05:26 +0100801 ieee80211_get_hdrlen_from_skb(skb), padsize,
Bob Copeland2ac29272010-02-09 13:06:54 -0500802 get_hw_packet_type(skb),
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200803 (sc->power_level * 2),
Bob Copeland8902ff42009-01-22 08:44:20 -0500804 hw_rate,
Nick Kossifidis2bed03e2009-04-30 15:55:49 -0400805 info->control.rates[0].count, keyidx, ah->ah_tx_ant, flags,
Bob Copeland07c1e852009-01-22 08:44:21 -0500806 cts_rate, duration);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200807 if (ret)
808 goto err_unmap;
809
Felix Fietkau2f7fe872008-10-05 18:05:48 +0200810 memset(mrr_rate, 0, sizeof(mrr_rate));
811 memset(mrr_tries, 0, sizeof(mrr_tries));
812 for (i = 0; i < 3; i++) {
813 rate = ieee80211_get_alt_retry_rate(sc->hw, info, i);
814 if (!rate)
815 break;
816
817 mrr_rate[i] = rate->hw_value;
Johannes Berge6a98542008-10-21 12:40:02 +0200818 mrr_tries[i] = info->control.rates[i + 1].count;
Felix Fietkau2f7fe872008-10-05 18:05:48 +0200819 }
820
Bruno Randolfa6668192010-06-16 19:12:01 +0900821 ath5k_hw_setup_mrr_tx_desc(ah, ds,
Felix Fietkau2f7fe872008-10-05 18:05:48 +0200822 mrr_rate[0], mrr_tries[0],
823 mrr_rate[1], mrr_tries[1],
824 mrr_rate[2], mrr_tries[2]);
825
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200826 ds->ds_link = 0;
827 ds->ds_data = bf->skbaddr;
828
829 spin_lock_bh(&txq->lock);
830 list_add_tail(&bf->list, &txq->q);
Bruno Randolf925e0b02010-09-17 11:36:35 +0900831 txq->txq_len++;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200832 if (txq->link == NULL) /* is this first packet? */
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300833 ath5k_hw_set_txdp(ah, txq->qnum, bf->daddr);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200834 else /* no, so only link it */
835 *txq->link = bf->daddr;
836
837 txq->link = &ds->ds_link;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300838 ath5k_hw_start_tx_dma(ah, txq->qnum);
Jiri Slaby274c7c32008-07-15 17:44:20 +0200839 mmiowb();
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200840 spin_unlock_bh(&txq->lock);
841
842 return 0;
843err_unmap:
844 pci_unmap_single(sc->pdev, bf->skbaddr, skb->len, PCI_DMA_TODEVICE);
845 return ret;
846}
847
848/*******************\
849* Descriptors setup *
850\*******************/
851
852static int
853ath5k_desc_alloc(struct ath5k_softc *sc, struct pci_dev *pdev)
854{
855 struct ath5k_desc *ds;
856 struct ath5k_buf *bf;
857 dma_addr_t da;
858 unsigned int i;
859 int ret;
860
861 /* allocate descriptors */
862 sc->desc_len = sizeof(struct ath5k_desc) *
863 (ATH_TXBUF + ATH_RXBUF + ATH_BCBUF + 1);
864 sc->desc = pci_alloc_consistent(pdev, sc->desc_len, &sc->desc_daddr);
865 if (sc->desc == NULL) {
866 ATH5K_ERR(sc, "can't allocate descriptors\n");
867 ret = -ENOMEM;
868 goto err;
869 }
870 ds = sc->desc;
871 da = sc->desc_daddr;
872 ATH5K_DBG(sc, ATH5K_DEBUG_ANY, "DMA map: %p (%zu) -> %llx\n",
873 ds, sc->desc_len, (unsigned long long)sc->desc_daddr);
874
875 bf = kcalloc(1 + ATH_TXBUF + ATH_RXBUF + ATH_BCBUF,
876 sizeof(struct ath5k_buf), GFP_KERNEL);
877 if (bf == NULL) {
878 ATH5K_ERR(sc, "can't allocate bufptr\n");
879 ret = -ENOMEM;
880 goto err_free;
881 }
882 sc->bufptr = bf;
883
884 INIT_LIST_HEAD(&sc->rxbuf);
885 for (i = 0; i < ATH_RXBUF; i++, bf++, ds++, da += sizeof(*ds)) {
886 bf->desc = ds;
887 bf->daddr = da;
888 list_add_tail(&bf->list, &sc->rxbuf);
889 }
890
891 INIT_LIST_HEAD(&sc->txbuf);
892 sc->txbuf_len = ATH_TXBUF;
893 for (i = 0; i < ATH_TXBUF; i++, bf++, ds++,
894 da += sizeof(*ds)) {
895 bf->desc = ds;
896 bf->daddr = da;
897 list_add_tail(&bf->list, &sc->txbuf);
898 }
899
Ben Greearb1ae1ed2010-09-30 12:22:58 -0700900 /* beacon buffers */
901 INIT_LIST_HEAD(&sc->bcbuf);
902 for (i = 0; i < ATH_BCBUF; i++, bf++, ds++, da += sizeof(*ds)) {
903 bf->desc = ds;
904 bf->daddr = da;
905 list_add_tail(&bf->list, &sc->bcbuf);
906 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200907
908 return 0;
909err_free:
910 pci_free_consistent(pdev, sc->desc_len, sc->desc, sc->desc_daddr);
911err:
912 sc->desc = NULL;
913 return ret;
914}
915
916static void
917ath5k_desc_free(struct ath5k_softc *sc, struct pci_dev *pdev)
918{
919 struct ath5k_buf *bf;
920
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200921 list_for_each_entry(bf, &sc->txbuf, list)
Bruno Randolf9e4e43f2010-06-16 19:11:17 +0900922 ath5k_txbuf_free_skb(sc, bf);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200923 list_for_each_entry(bf, &sc->rxbuf, list)
Bruno Randolf9e4e43f2010-06-16 19:11:17 +0900924 ath5k_rxbuf_free_skb(sc, bf);
Ben Greearb1ae1ed2010-09-30 12:22:58 -0700925 list_for_each_entry(bf, &sc->bcbuf, list)
926 ath5k_txbuf_free_skb(sc, bf);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200927
928 /* Free memory associated with all descriptors */
929 pci_free_consistent(pdev, sc->desc_len, sc->desc, sc->desc_daddr);
Bruno Randolf39d63f22010-06-16 19:11:41 +0900930 sc->desc = NULL;
931 sc->desc_daddr = 0;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200932
933 kfree(sc->bufptr);
934 sc->bufptr = NULL;
935}
936
937
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200938/**************\
939* Queues setup *
940\**************/
941
942static struct ath5k_txq *
943ath5k_txq_setup(struct ath5k_softc *sc,
944 int qtype, int subtype)
945{
946 struct ath5k_hw *ah = sc->ah;
947 struct ath5k_txq *txq;
948 struct ath5k_txq_info qi = {
949 .tqi_subtype = subtype,
Bruno Randolfde8af452010-09-17 11:37:12 +0900950 /* XXX: default values not correct for B and XR channels,
951 * but who cares? */
952 .tqi_aifs = AR5K_TUNE_AIFS,
953 .tqi_cw_min = AR5K_TUNE_CWMIN,
954 .tqi_cw_max = AR5K_TUNE_CWMAX
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200955 };
956 int qnum;
957
958 /*
959 * Enable interrupts only for EOL and DESC conditions.
960 * We mark tx descriptors to receive a DESC interrupt
Bob Copelanda180a132010-08-15 13:03:12 -0400961 * when a tx queue gets deep; otherwise we wait for the
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200962 * EOL to reap descriptors. Note that this is done to
963 * reduce interrupt load and this only defers reaping
964 * descriptors, never transmitting frames. Aside from
965 * reducing interrupts this also permits more concurrency.
966 * The only potential downside is if the tx queue backs
967 * up in which case the top half of the kernel may backup
968 * due to a lack of tx descriptors.
969 */
970 qi.tqi_flags = AR5K_TXQ_FLAG_TXEOLINT_ENABLE |
971 AR5K_TXQ_FLAG_TXDESCINT_ENABLE;
972 qnum = ath5k_hw_setup_tx_queue(ah, qtype, &qi);
973 if (qnum < 0) {
974 /*
975 * NB: don't print a message, this happens
976 * normally on parts with too few tx queues
977 */
978 return ERR_PTR(qnum);
979 }
980 if (qnum >= ARRAY_SIZE(sc->txqs)) {
981 ATH5K_ERR(sc, "hw qnum %u out of range, max %tu!\n",
982 qnum, ARRAY_SIZE(sc->txqs));
983 ath5k_hw_release_tx_queue(ah, qnum);
984 return ERR_PTR(-EINVAL);
985 }
986 txq = &sc->txqs[qnum];
987 if (!txq->setup) {
988 txq->qnum = qnum;
989 txq->link = NULL;
990 INIT_LIST_HEAD(&txq->q);
991 spin_lock_init(&txq->lock);
992 txq->setup = true;
Bruno Randolf925e0b02010-09-17 11:36:35 +0900993 txq->txq_len = 0;
Bruno Randolf4edd7612010-09-17 11:36:56 +0900994 txq->txq_poll_mark = false;
Bruno Randolf923e5b32010-09-17 11:37:02 +0900995 txq->txq_stuck = 0;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200996 }
997 return &sc->txqs[qnum];
998}
999
1000static int
1001ath5k_beaconq_setup(struct ath5k_hw *ah)
1002{
1003 struct ath5k_txq_info qi = {
Bruno Randolfde8af452010-09-17 11:37:12 +09001004 /* XXX: default values not correct for B and XR channels,
1005 * but who cares? */
1006 .tqi_aifs = AR5K_TUNE_AIFS,
1007 .tqi_cw_min = AR5K_TUNE_CWMIN,
1008 .tqi_cw_max = AR5K_TUNE_CWMAX,
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001009 /* NB: for dynamic turbo, don't enable any other interrupts */
1010 .tqi_flags = AR5K_TXQ_FLAG_TXDESCINT_ENABLE
1011 };
1012
1013 return ath5k_hw_setup_tx_queue(ah, AR5K_TX_QUEUE_BEACON, &qi);
1014}
1015
1016static int
1017ath5k_beaconq_config(struct ath5k_softc *sc)
1018{
1019 struct ath5k_hw *ah = sc->ah;
1020 struct ath5k_txq_info qi;
1021 int ret;
1022
1023 ret = ath5k_hw_get_tx_queueprops(ah, sc->bhalq, &qi);
1024 if (ret)
Bob Copelanda951ae22010-01-20 23:51:04 -05001025 goto err;
1026
Johannes Berg05c914f2008-09-11 00:01:58 +02001027 if (sc->opmode == NL80211_IFTYPE_AP ||
1028 sc->opmode == NL80211_IFTYPE_MESH_POINT) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001029 /*
1030 * Always burst out beacon and CAB traffic
1031 * (aifs = cwmin = cwmax = 0)
1032 */
1033 qi.tqi_aifs = 0;
1034 qi.tqi_cw_min = 0;
1035 qi.tqi_cw_max = 0;
Johannes Berg05c914f2008-09-11 00:01:58 +02001036 } else if (sc->opmode == NL80211_IFTYPE_ADHOC) {
Bruno Randolf6d91e1d2008-01-19 18:18:41 +09001037 /*
1038 * Adhoc mode; backoff between 0 and (2 * cw_min).
1039 */
1040 qi.tqi_aifs = 0;
1041 qi.tqi_cw_min = 0;
Bruno Randolfde8af452010-09-17 11:37:12 +09001042 qi.tqi_cw_max = 2 * AR5K_TUNE_CWMIN;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001043 }
1044
Bruno Randolf6d91e1d2008-01-19 18:18:41 +09001045 ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
1046 "beacon queueprops tqi_aifs:%d tqi_cw_min:%d tqi_cw_max:%d\n",
1047 qi.tqi_aifs, qi.tqi_cw_min, qi.tqi_cw_max);
1048
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001049 ret = ath5k_hw_set_tx_queueprops(ah, sc->bhalq, &qi);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001050 if (ret) {
1051 ATH5K_ERR(sc, "%s: unable to update parameters for beacon "
1052 "hardware queue!\n", __func__);
Bob Copelanda951ae22010-01-20 23:51:04 -05001053 goto err;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001054 }
Bob Copelanda951ae22010-01-20 23:51:04 -05001055 ret = ath5k_hw_reset_tx_queue(ah, sc->bhalq); /* push to h/w */
1056 if (ret)
1057 goto err;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001058
Bob Copelanda951ae22010-01-20 23:51:04 -05001059 /* reconfigure cabq with ready time to 80% of beacon_interval */
1060 ret = ath5k_hw_get_tx_queueprops(ah, AR5K_TX_QUEUE_ID_CAB, &qi);
1061 if (ret)
1062 goto err;
1063
1064 qi.tqi_ready_time = (sc->bintval * 80) / 100;
1065 ret = ath5k_hw_set_tx_queueprops(ah, AR5K_TX_QUEUE_ID_CAB, &qi);
1066 if (ret)
1067 goto err;
1068
1069 ret = ath5k_hw_reset_tx_queue(ah, AR5K_TX_QUEUE_ID_CAB);
1070err:
1071 return ret;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001072}
1073
1074static void
1075ath5k_txq_drainq(struct ath5k_softc *sc, struct ath5k_txq *txq)
1076{
1077 struct ath5k_buf *bf, *bf0;
1078
1079 /*
1080 * NB: this assumes output has been stopped and
1081 * we do not need to block ath5k_tx_tasklet
1082 */
1083 spin_lock_bh(&txq->lock);
1084 list_for_each_entry_safe(bf, bf0, &txq->q, list) {
Bruno Randolfb47f4072008-03-05 18:35:45 +09001085 ath5k_debug_printtxbuf(sc, bf);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001086
Bruno Randolf9e4e43f2010-06-16 19:11:17 +09001087 ath5k_txbuf_free_skb(sc, bf);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001088
1089 spin_lock_bh(&sc->txbuflock);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001090 list_move_tail(&bf->list, &sc->txbuf);
1091 sc->txbuf_len++;
Bruno Randolf925e0b02010-09-17 11:36:35 +09001092 txq->txq_len--;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001093 spin_unlock_bh(&sc->txbuflock);
1094 }
1095 txq->link = NULL;
Bruno Randolf4edd7612010-09-17 11:36:56 +09001096 txq->txq_poll_mark = false;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001097 spin_unlock_bh(&txq->lock);
1098}
1099
1100/*
1101 * Drain the transmit queues and reclaim resources.
1102 */
1103static void
1104ath5k_txq_cleanup(struct ath5k_softc *sc)
1105{
1106 struct ath5k_hw *ah = sc->ah;
1107 unsigned int i;
1108
1109 /* XXX return value */
1110 if (likely(!test_bit(ATH_STAT_INVALID, sc->status))) {
1111 /* don't touch the hardware if marked invalid */
1112 ath5k_hw_stop_tx_dma(ah, sc->bhalq);
1113 ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "beacon queue %x\n",
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001114 ath5k_hw_get_txdp(ah, sc->bhalq));
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001115 for (i = 0; i < ARRAY_SIZE(sc->txqs); i++)
1116 if (sc->txqs[i].setup) {
1117 ath5k_hw_stop_tx_dma(ah, sc->txqs[i].qnum);
1118 ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "txq [%u] %x, "
1119 "link %p\n",
1120 sc->txqs[i].qnum,
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001121 ath5k_hw_get_txdp(ah,
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001122 sc->txqs[i].qnum),
1123 sc->txqs[i].link);
1124 }
1125 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001126
1127 for (i = 0; i < ARRAY_SIZE(sc->txqs); i++)
1128 if (sc->txqs[i].setup)
1129 ath5k_txq_drainq(sc, &sc->txqs[i]);
1130}
1131
1132static void
1133ath5k_txq_release(struct ath5k_softc *sc)
1134{
1135 struct ath5k_txq *txq = sc->txqs;
1136 unsigned int i;
1137
1138 for (i = 0; i < ARRAY_SIZE(sc->txqs); i++, txq++)
1139 if (txq->setup) {
1140 ath5k_hw_release_tx_queue(sc->ah, txq->qnum);
1141 txq->setup = false;
1142 }
1143}
1144
1145
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001146/*************\
1147* RX Handling *
1148\*************/
1149
1150/*
1151 * Enable the receive h/w following a reset.
1152 */
1153static int
1154ath5k_rx_start(struct ath5k_softc *sc)
1155{
1156 struct ath5k_hw *ah = sc->ah;
Luis R. Rodriguezdb719712009-09-10 11:20:57 -07001157 struct ath_common *common = ath5k_hw_common(ah);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001158 struct ath5k_buf *bf;
1159 int ret;
1160
Nick Kossifidisb6127982010-08-15 13:03:11 -04001161 common->rx_bufsize = roundup(IEEE80211_MAX_FRAME_LEN, common->cachelsz);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001162
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -08001163 ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "cachelsz %u rx_bufsize %u\n",
1164 common->cachelsz, common->rx_bufsize);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001165
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001166 spin_lock_bh(&sc->rxbuflock);
Bob Copeland26925042009-04-15 07:57:36 -04001167 sc->rxlink = NULL;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001168 list_for_each_entry(bf, &sc->rxbuf, list) {
1169 ret = ath5k_rxbuf_setup(sc, bf);
1170 if (ret != 0) {
1171 spin_unlock_bh(&sc->rxbuflock);
1172 goto err;
1173 }
1174 }
1175 bf = list_first_entry(&sc->rxbuf, struct ath5k_buf, list);
Bob Copeland26925042009-04-15 07:57:36 -04001176 ath5k_hw_set_rxdp(ah, bf->daddr);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001177 spin_unlock_bh(&sc->rxbuflock);
1178
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001179 ath5k_hw_start_rx_dma(ah); /* enable recv descriptors */
Ben Greearb1ae1ed2010-09-30 12:22:58 -07001180 ath5k_mode_setup(sc, NULL); /* set filters, etc. */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001181 ath5k_hw_start_rx_pcu(ah); /* re-enable PCU/DMA engine */
1182
1183 return 0;
1184err:
1185 return ret;
1186}
1187
1188/*
1189 * Disable the receive h/w in preparation for a reset.
1190 */
1191static void
1192ath5k_rx_stop(struct ath5k_softc *sc)
1193{
1194 struct ath5k_hw *ah = sc->ah;
1195
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001196 ath5k_hw_stop_rx_pcu(ah); /* disable PCU */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001197 ath5k_hw_set_rx_filter(ah, 0); /* clear recv filter */
1198 ath5k_hw_stop_rx_dma(ah); /* disable DMA engine */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001199
1200 ath5k_debug_printrxbuffs(sc, ah);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001201}
1202
1203static unsigned int
Bruno Randolf8a89f062010-06-16 19:11:51 +09001204ath5k_rx_decrypted(struct ath5k_softc *sc, struct sk_buff *skb,
1205 struct ath5k_rx_status *rs)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001206{
Luis R. Rodriguezdc1e0012009-11-04 17:47:31 -08001207 struct ath5k_hw *ah = sc->ah;
1208 struct ath_common *common = ath5k_hw_common(ah);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001209 struct ieee80211_hdr *hdr = (void *)skb->data;
Harvey Harrison798ee982008-07-15 18:44:02 -07001210 unsigned int keyix, hlen;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001211
Bruno Randolfb47f4072008-03-05 18:35:45 +09001212 if (!(rs->rs_status & AR5K_RXERR_DECRYPT) &&
1213 rs->rs_keyix != AR5K_RXKEYIX_INVALID)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001214 return RX_FLAG_DECRYPTED;
1215
1216 /* Apparently when a default key is used to decrypt the packet
1217 the hw does not set the index used to decrypt. In such cases
1218 get the index from the packet. */
Harvey Harrison798ee982008-07-15 18:44:02 -07001219 hlen = ieee80211_hdrlen(hdr->frame_control);
Harvey Harrison24b56e72008-06-14 23:33:38 -07001220 if (ieee80211_has_protected(hdr->frame_control) &&
1221 !(rs->rs_status & AR5K_RXERR_DECRYPT) &&
1222 skb->len >= hlen + 4) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001223 keyix = skb->data[hlen + 3] >> 6;
1224
Luis R. Rodriguezdc1e0012009-11-04 17:47:31 -08001225 if (test_bit(keyix, common->keymap))
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001226 return RX_FLAG_DECRYPTED;
1227 }
1228
1229 return 0;
1230}
1231
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001232
1233static void
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001234ath5k_check_ibss_tsf(struct ath5k_softc *sc, struct sk_buff *skb,
1235 struct ieee80211_rx_status *rxs)
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001236{
Luis R. Rodriguez954fece2009-09-10 10:51:33 -07001237 struct ath_common *common = ath5k_hw_common(sc->ah);
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001238 u64 tsf, bc_tstamp;
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001239 u32 hw_tu;
1240 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)skb->data;
1241
Harvey Harrison24b56e72008-06-14 23:33:38 -07001242 if (ieee80211_is_beacon(mgmt->frame_control) &&
Pavel Roskin38c07b42008-02-26 17:59:14 -05001243 le16_to_cpu(mgmt->u.beacon.capab_info) & WLAN_CAPABILITY_IBSS &&
Luis R. Rodriguez954fece2009-09-10 10:51:33 -07001244 memcmp(mgmt->bssid, common->curbssid, ETH_ALEN) == 0) {
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001245 /*
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001246 * Received an IBSS beacon with the same BSSID. Hardware *must*
1247 * have updated the local TSF. We have to work around various
1248 * hardware bugs, though...
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001249 */
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001250 tsf = ath5k_hw_get_tsf64(sc->ah);
1251 bc_tstamp = le64_to_cpu(mgmt->u.beacon.timestamp);
1252 hw_tu = TSF_TO_TU(tsf);
1253
1254 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
1255 "beacon %llx mactime %llx (diff %lld) tsf now %llx\n",
John W. Linville06501d22008-04-01 17:38:47 -04001256 (unsigned long long)bc_tstamp,
1257 (unsigned long long)rxs->mactime,
1258 (unsigned long long)(rxs->mactime - bc_tstamp),
1259 (unsigned long long)tsf);
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001260
1261 /*
1262 * Sometimes the HW will give us a wrong tstamp in the rx
1263 * status, causing the timestamp extension to go wrong.
1264 * (This seems to happen especially with beacon frames bigger
1265 * than 78 byte (incl. FCS))
1266 * But we know that the receive timestamp must be later than the
1267 * timestamp of the beacon since HW must have synced to that.
1268 *
1269 * NOTE: here we assume mactime to be after the frame was
1270 * received, not like mac80211 which defines it at the start.
1271 */
1272 if (bc_tstamp > rxs->mactime) {
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001273 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001274 "fixing mactime from %llx to %llx\n",
John W. Linville06501d22008-04-01 17:38:47 -04001275 (unsigned long long)rxs->mactime,
1276 (unsigned long long)tsf);
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001277 rxs->mactime = tsf;
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001278 }
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001279
1280 /*
1281 * Local TSF might have moved higher than our beacon timers,
1282 * in that case we have to update them to continue sending
1283 * beacons. This also takes care of synchronizing beacon sending
1284 * times with other stations.
1285 */
1286 if (hw_tu >= sc->nexttbtt)
1287 ath5k_beacon_update_timers(sc, bc_tstamp);
Bruno Randolf7f896122010-09-27 12:22:21 +09001288
1289 /* Check if the beacon timers are still correct, because a TSF
1290 * update might have created a window between them - for a
1291 * longer description see the comment of this function: */
1292 if (!ath5k_hw_check_beacon_timers(sc->ah, sc->bintval)) {
1293 ath5k_beacon_update_timers(sc, bc_tstamp);
1294 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
1295 "fixed beacon timers after beacon receive\n");
1296 }
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001297 }
1298}
1299
Bruno Randolfb4ea4492010-03-25 14:49:25 +09001300static void
1301ath5k_update_beacon_rssi(struct ath5k_softc *sc, struct sk_buff *skb, int rssi)
1302{
1303 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)skb->data;
1304 struct ath5k_hw *ah = sc->ah;
1305 struct ath_common *common = ath5k_hw_common(ah);
1306
1307 /* only beacons from our BSSID */
1308 if (!ieee80211_is_beacon(mgmt->frame_control) ||
1309 memcmp(mgmt->bssid, common->curbssid, ETH_ALEN) != 0)
1310 return;
1311
1312 ah->ah_beacon_rssi_avg = ath5k_moving_average(ah->ah_beacon_rssi_avg,
1313 rssi);
1314
1315 /* in IBSS mode we should keep RSSI statistics per neighbour */
1316 /* le16_to_cpu(mgmt->u.beacon.capab_info) & WLAN_CAPABILITY_IBSS */
1317}
1318
Benoit Papillault8127fbd2010-02-27 23:05:26 +01001319/*
Bob Copelanda180a132010-08-15 13:03:12 -04001320 * Compute padding position. skb must contain an IEEE 802.11 frame
Benoit Papillault8127fbd2010-02-27 23:05:26 +01001321 */
1322static int ath5k_common_padpos(struct sk_buff *skb)
1323{
1324 struct ieee80211_hdr * hdr = (struct ieee80211_hdr *)skb->data;
1325 __le16 frame_control = hdr->frame_control;
1326 int padpos = 24;
1327
1328 if (ieee80211_has_a4(frame_control)) {
1329 padpos += ETH_ALEN;
1330 }
1331 if (ieee80211_is_data_qos(frame_control)) {
1332 padpos += IEEE80211_QOS_CTL_LEN;
1333 }
1334
1335 return padpos;
1336}
1337
1338/*
Bob Copelanda180a132010-08-15 13:03:12 -04001339 * This function expects an 802.11 frame and returns the number of
1340 * bytes added, or -1 if we don't have enough header room.
Benoit Papillault8127fbd2010-02-27 23:05:26 +01001341 */
Benoit Papillault8127fbd2010-02-27 23:05:26 +01001342static int ath5k_add_padding(struct sk_buff *skb)
1343{
1344 int padpos = ath5k_common_padpos(skb);
1345 int padsize = padpos & 3;
1346
1347 if (padsize && skb->len>padpos) {
1348
1349 if (skb_headroom(skb) < padsize)
1350 return -1;
1351
1352 skb_push(skb, padsize);
1353 memmove(skb->data, skb->data+padsize, padpos);
1354 return padsize;
1355 }
1356
1357 return 0;
1358}
1359
1360/*
Bob Copelanda180a132010-08-15 13:03:12 -04001361 * The MAC header is padded to have 32-bit boundary if the
1362 * packet payload is non-zero. The general calculation for
1363 * padsize would take into account odd header lengths:
1364 * padsize = 4 - (hdrlen & 3); however, since only
1365 * even-length headers are used, padding can only be 0 or 2
1366 * bytes and we can optimize this a bit. We must not try to
1367 * remove padding from short control frames that do not have a
1368 * payload.
1369 *
1370 * This function expects an 802.11 frame and returns the number of
1371 * bytes removed.
Benoit Papillault8127fbd2010-02-27 23:05:26 +01001372 */
Benoit Papillault8127fbd2010-02-27 23:05:26 +01001373static int ath5k_remove_padding(struct sk_buff *skb)
1374{
1375 int padpos = ath5k_common_padpos(skb);
1376 int padsize = padpos & 3;
1377
1378 if (padsize && skb->len>=padpos+padsize) {
1379 memmove(skb->data + padsize, skb->data, padpos);
1380 skb_pull(skb, padsize);
1381 return padsize;
1382 }
1383
1384 return 0;
1385}
1386
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001387static void
Bruno Randolf8a89f062010-06-16 19:11:51 +09001388ath5k_receive_frame(struct ath5k_softc *sc, struct sk_buff *skb,
1389 struct ath5k_rx_status *rs)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001390{
Bob Copeland1c5256b2009-08-24 23:00:32 -04001391 struct ieee80211_rx_status *rxs;
Bruno Randolf8a89f062010-06-16 19:11:51 +09001392
Bruno Randolf8a89f062010-06-16 19:11:51 +09001393 ath5k_remove_padding(skb);
1394
1395 rxs = IEEE80211_SKB_RXCB(skb);
1396
1397 rxs->flag = 0;
1398 if (unlikely(rs->rs_status & AR5K_RXERR_MIC))
1399 rxs->flag |= RX_FLAG_MMIC_ERROR;
1400
1401 /*
1402 * always extend the mac timestamp, since this information is
1403 * also needed for proper IBSS merging.
1404 *
1405 * XXX: it might be too late to do it here, since rs_tstamp is
1406 * 15bit only. that means TSF extension has to be done within
1407 * 32768usec (about 32ms). it might be necessary to move this to
1408 * the interrupt handler, like it is done in madwifi.
1409 *
1410 * Unfortunately we don't know when the hardware takes the rx
1411 * timestamp (beginning of phy frame, data frame, end of rx?).
1412 * The only thing we know is that it is hardware specific...
1413 * On AR5213 it seems the rx timestamp is at the end of the
1414 * frame, but i'm not sure.
1415 *
1416 * NOTE: mac80211 defines mactime at the beginning of the first
1417 * data symbol. Since we don't have any time references it's
1418 * impossible to comply to that. This affects IBSS merge only
1419 * right now, so it's not too bad...
1420 */
1421 rxs->mactime = ath5k_extend_tsf(sc->ah, rs->rs_tstamp);
1422 rxs->flag |= RX_FLAG_TSFT;
1423
1424 rxs->freq = sc->curchan->center_freq;
1425 rxs->band = sc->curband->band;
1426
1427 rxs->signal = sc->ah->ah_noise_floor + rs->rs_rssi;
1428
1429 rxs->antenna = rs->rs_antenna;
1430
1431 if (rs->rs_antenna > 0 && rs->rs_antenna < 5)
1432 sc->stats.antenna_rx[rs->rs_antenna]++;
1433 else
1434 sc->stats.antenna_rx[0]++; /* invalid */
1435
1436 rxs->rate_idx = ath5k_hw_to_driver_rix(sc, rs->rs_rate);
1437 rxs->flag |= ath5k_rx_decrypted(sc, skb, rs);
1438
1439 if (rxs->rate_idx >= 0 && rs->rs_rate ==
1440 sc->curband->bitrates[rxs->rate_idx].hw_value_short)
1441 rxs->flag |= RX_FLAG_SHORTPRE;
1442
1443 ath5k_debug_dump_skb(sc, skb, "RX ", 0);
1444
1445 ath5k_update_beacon_rssi(sc, skb, rs->rs_rssi);
1446
1447 /* check beacons in IBSS mode */
1448 if (sc->opmode == NL80211_IFTYPE_ADHOC)
1449 ath5k_check_ibss_tsf(sc, skb, rxs);
1450
1451 ieee80211_rx(sc->hw, skb);
1452}
1453
Bruno Randolf02a78b42010-06-16 19:11:56 +09001454/** ath5k_frame_receive_ok() - Do we want to receive this frame or not?
1455 *
1456 * Check if we want to further process this frame or not. Also update
1457 * statistics. Return true if we want this frame, false if not.
1458 */
1459static bool
1460ath5k_receive_frame_ok(struct ath5k_softc *sc, struct ath5k_rx_status *rs)
1461{
1462 sc->stats.rx_all_count++;
Ben Greearb72acdd2010-10-01 10:54:04 -07001463 sc->stats.rx_bytes_count += rs->rs_datalen;
Bruno Randolf02a78b42010-06-16 19:11:56 +09001464
1465 if (unlikely(rs->rs_status)) {
1466 if (rs->rs_status & AR5K_RXERR_CRC)
1467 sc->stats.rxerr_crc++;
1468 if (rs->rs_status & AR5K_RXERR_FIFO)
1469 sc->stats.rxerr_fifo++;
1470 if (rs->rs_status & AR5K_RXERR_PHY) {
1471 sc->stats.rxerr_phy++;
1472 if (rs->rs_phyerr > 0 && rs->rs_phyerr < 32)
1473 sc->stats.rxerr_phy_code[rs->rs_phyerr]++;
1474 return false;
1475 }
1476 if (rs->rs_status & AR5K_RXERR_DECRYPT) {
1477 /*
1478 * Decrypt error. If the error occurred
1479 * because there was no hardware key, then
1480 * let the frame through so the upper layers
1481 * can process it. This is necessary for 5210
1482 * parts which have no way to setup a ``clear''
1483 * key cache entry.
1484 *
1485 * XXX do key cache faulting
1486 */
1487 sc->stats.rxerr_decrypt++;
1488 if (rs->rs_keyix == AR5K_RXKEYIX_INVALID &&
1489 !(rs->rs_status & AR5K_RXERR_CRC))
1490 return true;
1491 }
1492 if (rs->rs_status & AR5K_RXERR_MIC) {
1493 sc->stats.rxerr_mic++;
1494 return true;
1495 }
1496
Bob Copeland23538c22010-08-15 13:03:13 -04001497 /* reject any frames with non-crypto errors */
1498 if (rs->rs_status & ~(AR5K_RXERR_DECRYPT))
Bruno Randolf02a78b42010-06-16 19:11:56 +09001499 return false;
1500 }
1501
1502 if (unlikely(rs->rs_more)) {
1503 sc->stats.rxerr_jumbo++;
1504 return false;
1505 }
1506 return true;
1507}
1508
Bruno Randolf8a89f062010-06-16 19:11:51 +09001509static void
1510ath5k_tasklet_rx(unsigned long data)
1511{
Bruno Randolfb47f4072008-03-05 18:35:45 +09001512 struct ath5k_rx_status rs = {};
Bob Copelandb6ea0352009-01-10 14:42:54 -05001513 struct sk_buff *skb, *next_skb;
1514 dma_addr_t next_skb_addr;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001515 struct ath5k_softc *sc = (void *)data;
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -08001516 struct ath5k_hw *ah = sc->ah;
1517 struct ath_common *common = ath5k_hw_common(ah);
Bob Copelandc57ca812009-04-15 07:57:35 -04001518 struct ath5k_buf *bf;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001519 struct ath5k_desc *ds;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001520 int ret;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001521
1522 spin_lock(&sc->rxbuflock);
Jiri Slaby3a0f2c82008-07-15 17:44:18 +02001523 if (list_empty(&sc->rxbuf)) {
1524 ATH5K_WARN(sc, "empty rx buf pool\n");
1525 goto unlock;
1526 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001527 do {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001528 bf = list_first_entry(&sc->rxbuf, struct ath5k_buf, list);
1529 BUG_ON(bf->skb == NULL);
1530 skb = bf->skb;
1531 ds = bf->desc;
1532
Bob Copelandc57ca812009-04-15 07:57:35 -04001533 /* bail if HW is still using self-linked descriptor */
1534 if (ath5k_hw_get_rxdp(sc->ah) == bf->daddr)
1535 break;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001536
Bruno Randolfb47f4072008-03-05 18:35:45 +09001537 ret = sc->ah->ah_proc_rx_desc(sc->ah, ds, &rs);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001538 if (unlikely(ret == -EINPROGRESS))
1539 break;
1540 else if (unlikely(ret)) {
1541 ATH5K_ERR(sc, "error in processing rx descriptor\n");
Bruno Randolf76443952010-03-09 16:56:00 +09001542 sc->stats.rxerr_proc++;
Bruno Randolfb16062f2010-06-16 19:11:46 +09001543 break;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001544 }
1545
Bruno Randolf02a78b42010-06-16 19:11:56 +09001546 if (ath5k_receive_frame_ok(sc, &rs)) {
1547 next_skb = ath5k_rx_skb_alloc(sc, &next_skb_addr);
Bruno Randolf76443952010-03-09 16:56:00 +09001548
Bruno Randolf02a78b42010-06-16 19:11:56 +09001549 /*
1550 * If we can't replace bf->skb with a new skb under
1551 * memory pressure, just skip this packet
1552 */
1553 if (!next_skb)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001554 goto next;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001555
Bruno Randolf02a78b42010-06-16 19:11:56 +09001556 pci_unmap_single(sc->pdev, bf->skbaddr,
1557 common->rx_bufsize,
1558 PCI_DMA_FROMDEVICE);
1559
1560 skb_put(skb, rs.rs_datalen);
1561
1562 ath5k_receive_frame(sc, skb, &rs);
1563
1564 bf->skb = next_skb;
1565 bf->skbaddr = next_skb_addr;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001566 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001567next:
1568 list_move_tail(&bf->list, &sc->rxbuf);
1569 } while (ath5k_rxbuf_setup(sc, bf) == 0);
Jiri Slaby3a0f2c82008-07-15 17:44:18 +02001570unlock:
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001571 spin_unlock(&sc->rxbuflock);
1572}
1573
1574
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001575/*************\
1576* TX Handling *
1577\*************/
1578
Bob Copeland8a63fac2010-09-17 12:45:07 +09001579static int ath5k_tx_queue(struct ieee80211_hw *hw, struct sk_buff *skb,
1580 struct ath5k_txq *txq)
1581{
1582 struct ath5k_softc *sc = hw->priv;
1583 struct ath5k_buf *bf;
1584 unsigned long flags;
1585 int padsize;
1586
1587 ath5k_debug_dump_skb(sc, skb, "TX ", 1);
1588
1589 /*
1590 * The hardware expects the header padded to 4 byte boundaries.
1591 * If this is not the case, we add the padding after the header.
1592 */
1593 padsize = ath5k_add_padding(skb);
1594 if (padsize < 0) {
1595 ATH5K_ERR(sc, "tx hdrlen not %%4: not enough"
1596 " headroom to pad");
1597 goto drop_packet;
1598 }
1599
Bruno Randolf925e0b02010-09-17 11:36:35 +09001600 if (txq->txq_len >= ATH5K_TXQ_LEN_MAX)
1601 ieee80211_stop_queue(hw, txq->qnum);
1602
Bob Copeland8a63fac2010-09-17 12:45:07 +09001603 spin_lock_irqsave(&sc->txbuflock, flags);
1604 if (list_empty(&sc->txbuf)) {
1605 ATH5K_ERR(sc, "no further txbuf available, dropping packet\n");
1606 spin_unlock_irqrestore(&sc->txbuflock, flags);
Bruno Randolf651d9372010-09-17 11:36:46 +09001607 ieee80211_stop_queues(hw);
Bob Copeland8a63fac2010-09-17 12:45:07 +09001608 goto drop_packet;
1609 }
1610 bf = list_first_entry(&sc->txbuf, struct ath5k_buf, list);
1611 list_del(&bf->list);
1612 sc->txbuf_len--;
1613 if (list_empty(&sc->txbuf))
1614 ieee80211_stop_queues(hw);
1615 spin_unlock_irqrestore(&sc->txbuflock, flags);
1616
1617 bf->skb = skb;
1618
1619 if (ath5k_txbuf_setup(sc, bf, txq, padsize)) {
1620 bf->skb = NULL;
1621 spin_lock_irqsave(&sc->txbuflock, flags);
1622 list_add_tail(&bf->list, &sc->txbuf);
1623 sc->txbuf_len++;
1624 spin_unlock_irqrestore(&sc->txbuflock, flags);
1625 goto drop_packet;
1626 }
1627 return NETDEV_TX_OK;
1628
1629drop_packet:
1630 dev_kfree_skb_any(skb);
1631 return NETDEV_TX_OK;
1632}
1633
Bruno Randolf14404012010-09-17 11:36:51 +09001634static void
1635ath5k_tx_frame_completed(struct ath5k_softc *sc, struct sk_buff *skb,
1636 struct ath5k_tx_status *ts)
1637{
1638 struct ieee80211_tx_info *info;
1639 int i;
1640
1641 sc->stats.tx_all_count++;
Ben Greearb72acdd2010-10-01 10:54:04 -07001642 sc->stats.tx_bytes_count += skb->len;
Bruno Randolf14404012010-09-17 11:36:51 +09001643 info = IEEE80211_SKB_CB(skb);
1644
1645 ieee80211_tx_info_clear_status(info);
1646 for (i = 0; i < 4; i++) {
1647 struct ieee80211_tx_rate *r =
1648 &info->status.rates[i];
1649
1650 if (ts->ts_rate[i]) {
1651 r->idx = ath5k_hw_to_driver_rix(sc, ts->ts_rate[i]);
1652 r->count = ts->ts_retry[i];
1653 } else {
1654 r->idx = -1;
1655 r->count = 0;
1656 }
1657 }
1658
1659 /* count the successful attempt as well */
1660 info->status.rates[ts->ts_final_idx].count++;
1661
1662 if (unlikely(ts->ts_status)) {
1663 sc->stats.ack_fail++;
1664 if (ts->ts_status & AR5K_TXERR_FILT) {
1665 info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
1666 sc->stats.txerr_filt++;
1667 }
1668 if (ts->ts_status & AR5K_TXERR_XRETRY)
1669 sc->stats.txerr_retry++;
1670 if (ts->ts_status & AR5K_TXERR_FIFO)
1671 sc->stats.txerr_fifo++;
1672 } else {
1673 info->flags |= IEEE80211_TX_STAT_ACK;
1674 info->status.ack_signal = ts->ts_rssi;
1675 }
1676
1677 /*
1678 * Remove MAC header padding before giving the frame
1679 * back to mac80211.
1680 */
1681 ath5k_remove_padding(skb);
1682
1683 if (ts->ts_antenna > 0 && ts->ts_antenna < 5)
1684 sc->stats.antenna_tx[ts->ts_antenna]++;
1685 else
1686 sc->stats.antenna_tx[0]++; /* invalid */
1687
1688 ieee80211_tx_status(sc->hw, skb);
1689}
Bob Copeland8a63fac2010-09-17 12:45:07 +09001690
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001691static void
1692ath5k_tx_processq(struct ath5k_softc *sc, struct ath5k_txq *txq)
1693{
Bruno Randolfb47f4072008-03-05 18:35:45 +09001694 struct ath5k_tx_status ts = {};
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001695 struct ath5k_buf *bf, *bf0;
1696 struct ath5k_desc *ds;
1697 struct sk_buff *skb;
Bruno Randolf14404012010-09-17 11:36:51 +09001698 int ret;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001699
1700 spin_lock(&txq->lock);
1701 list_for_each_entry_safe(bf, bf0, &txq->q, list) {
Bruno Randolf23413292010-09-17 11:37:07 +09001702
1703 txq->txq_poll_mark = false;
1704
1705 /* skb might already have been processed last time. */
1706 if (bf->skb != NULL) {
1707 ds = bf->desc;
1708
1709 ret = sc->ah->ah_proc_tx_desc(sc->ah, ds, &ts);
1710 if (unlikely(ret == -EINPROGRESS))
1711 break;
1712 else if (unlikely(ret)) {
1713 ATH5K_ERR(sc,
1714 "error %d while processing "
1715 "queue %u\n", ret, txq->qnum);
1716 break;
1717 }
1718
1719 skb = bf->skb;
1720 bf->skb = NULL;
1721 pci_unmap_single(sc->pdev, bf->skbaddr, skb->len,
1722 PCI_DMA_TODEVICE);
1723 ath5k_tx_frame_completed(sc, skb, &ts);
1724 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001725
Bob Copelanda05988b2010-04-07 23:55:58 -04001726 /*
1727 * It's possible that the hardware can say the buffer is
1728 * completed when it hasn't yet loaded the ds_link from
Bruno Randolf23413292010-09-17 11:37:07 +09001729 * host memory and moved on.
1730 * Always keep the last descriptor to avoid HW races...
Bob Copelanda05988b2010-04-07 23:55:58 -04001731 */
Bruno Randolf23413292010-09-17 11:37:07 +09001732 if (ath5k_hw_get_txdp(sc->ah, txq->qnum) != bf->daddr) {
1733 spin_lock(&sc->txbuflock);
1734 list_move_tail(&bf->list, &sc->txbuf);
1735 sc->txbuf_len++;
1736 txq->txq_len--;
1737 spin_unlock(&sc->txbuflock);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001738 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001739 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001740 spin_unlock(&txq->lock);
Bruno Randolf4198a8d2010-10-05 13:27:17 +09001741 if (txq->txq_len < ATH5K_TXQ_LEN_LOW && txq->qnum < 4)
Bruno Randolf925e0b02010-09-17 11:36:35 +09001742 ieee80211_wake_queue(sc->hw, txq->qnum);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001743}
1744
1745static void
1746ath5k_tasklet_tx(unsigned long data)
1747{
Bob Copeland8784d2e2009-07-29 17:32:28 -04001748 int i;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001749 struct ath5k_softc *sc = (void *)data;
1750
Bob Copeland8784d2e2009-07-29 17:32:28 -04001751 for (i=0; i < AR5K_NUM_TX_QUEUES; i++)
1752 if (sc->txqs[i].setup && (sc->ah->ah_txq_isr & BIT(i)))
1753 ath5k_tx_processq(sc, &sc->txqs[i]);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001754}
1755
1756
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001757/*****************\
1758* Beacon handling *
1759\*****************/
1760
1761/*
1762 * Setup the beacon frame for transmit.
1763 */
1764static int
Johannes Berge039fa42008-05-15 12:55:29 +02001765ath5k_beacon_setup(struct ath5k_softc *sc, struct ath5k_buf *bf)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001766{
1767 struct sk_buff *skb = bf->skb;
Johannes Berga888d522008-05-26 16:43:39 +02001768 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001769 struct ath5k_hw *ah = sc->ah;
1770 struct ath5k_desc *ds;
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04001771 int ret = 0;
1772 u8 antenna;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001773 u32 flags;
Benoit Papillault8127fbd2010-02-27 23:05:26 +01001774 const int padsize = 0;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001775
1776 bf->skbaddr = pci_map_single(sc->pdev, skb->data, skb->len,
1777 PCI_DMA_TODEVICE);
1778 ATH5K_DBG(sc, ATH5K_DEBUG_BEACON, "skb %p [data %p len %u] "
1779 "skbaddr %llx\n", skb, skb->data, skb->len,
1780 (unsigned long long)bf->skbaddr);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -07001781 if (pci_dma_mapping_error(sc->pdev, bf->skbaddr)) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001782 ATH5K_ERR(sc, "beacon DMA mapping failed\n");
1783 return -EIO;
1784 }
1785
1786 ds = bf->desc;
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04001787 antenna = ah->ah_tx_ant;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001788
1789 flags = AR5K_TXDESC_NOACK;
Johannes Berg05c914f2008-09-11 00:01:58 +02001790 if (sc->opmode == NL80211_IFTYPE_ADHOC && ath5k_hw_hasveol(ah)) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001791 ds->ds_link = bf->daddr; /* self-linked */
1792 flags |= AR5K_TXDESC_VEOL;
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04001793 } else
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001794 ds->ds_link = 0;
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04001795
1796 /*
1797 * If we use multiple antennas on AP and use
1798 * the Sectored AP scenario, switch antenna every
1799 * 4 beacons to make sure everybody hears our AP.
1800 * When a client tries to associate, hw will keep
1801 * track of the tx antenna to be used for this client
1802 * automaticaly, based on ACKed packets.
1803 *
1804 * Note: AP still listens and transmits RTS on the
1805 * default antenna which is supposed to be an omni.
1806 *
1807 * Note2: On sectored scenarios it's possible to have
Bob Copelanda180a132010-08-15 13:03:12 -04001808 * multiple antennas (1 omni -- the default -- and 14
1809 * sectors), so if we choose to actually support this
1810 * mode, we need to allow the user to set how many antennas
1811 * we have and tweak the code below to send beacons
1812 * on all of them.
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04001813 */
1814 if (ah->ah_ant_mode == AR5K_ANTMODE_SECTOR_AP)
1815 antenna = sc->bsent & 4 ? 2 : 1;
1816
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001817
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02001818 /* FIXME: If we are in g mode and rate is a CCK rate
1819 * subtract ah->ah_txpower.txp_cck_ofdm_pwr_delta
1820 * from tx power (value is in dB units already) */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001821 ds->ds_data = bf->skbaddr;
Bruno Randolf281c56d2008-02-05 18:44:55 +09001822 ret = ah->ah_setup_tx_desc(ah, ds, skb->len,
Benoit Papillault8127fbd2010-02-27 23:05:26 +01001823 ieee80211_get_hdrlen_from_skb(skb), padsize,
Luis R. Rodriguez400ec452008-02-03 21:51:49 -05001824 AR5K_PKT_TYPE_BEACON, (sc->power_level * 2),
Johannes Berge039fa42008-05-15 12:55:29 +02001825 ieee80211_get_tx_rate(sc->hw, info)->hw_value,
Johannes Berg2e92e6f2008-05-15 12:55:27 +02001826 1, AR5K_TXKEYIX_INVALID,
Luis R. Rodriguez400ec452008-02-03 21:51:49 -05001827 antenna, flags, 0, 0);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001828 if (ret)
1829 goto err_unmap;
1830
1831 return 0;
1832err_unmap:
1833 pci_unmap_single(sc->pdev, bf->skbaddr, skb->len, PCI_DMA_TODEVICE);
1834 return ret;
1835}
1836
1837/*
Bob Copeland8a63fac2010-09-17 12:45:07 +09001838 * Updates the beacon that is sent by ath5k_beacon_send. For adhoc,
1839 * this is called only once at config_bss time, for AP we do it every
1840 * SWBA interrupt so that the TIM will reflect buffered frames.
1841 *
1842 * Called with the beacon lock.
1843 */
1844static int
1845ath5k_beacon_update(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1846{
1847 int ret;
1848 struct ath5k_softc *sc = hw->priv;
Ben Greearb1ae1ed2010-09-30 12:22:58 -07001849 struct ath5k_vif *avf = (void *)vif->drv_priv;
Bob Copeland8a63fac2010-09-17 12:45:07 +09001850 struct sk_buff *skb;
1851
1852 if (WARN_ON(!vif)) {
1853 ret = -EINVAL;
1854 goto out;
1855 }
1856
1857 skb = ieee80211_beacon_get(hw, vif);
1858
1859 if (!skb) {
1860 ret = -ENOMEM;
1861 goto out;
1862 }
1863
1864 ath5k_debug_dump_skb(sc, skb, "BC ", 1);
1865
Ben Greearb1ae1ed2010-09-30 12:22:58 -07001866 ath5k_txbuf_free_skb(sc, avf->bbuf);
1867 avf->bbuf->skb = skb;
1868 ret = ath5k_beacon_setup(sc, avf->bbuf);
Bob Copeland8a63fac2010-09-17 12:45:07 +09001869 if (ret)
Ben Greearb1ae1ed2010-09-30 12:22:58 -07001870 avf->bbuf->skb = NULL;
Bob Copeland8a63fac2010-09-17 12:45:07 +09001871out:
1872 return ret;
1873}
1874
1875/*
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001876 * Transmit a beacon frame at SWBA. Dynamic updates to the
1877 * frame contents are done as needed and the slot time is
1878 * also adjusted based on current state.
1879 *
Bob Copeland5faaff72010-07-13 11:32:40 -04001880 * This is called from software irq context (beacontq tasklets)
1881 * or user context from ath5k_beacon_config.
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001882 */
1883static void
1884ath5k_beacon_send(struct ath5k_softc *sc)
1885{
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001886 struct ath5k_hw *ah = sc->ah;
Ben Greearb1ae1ed2010-09-30 12:22:58 -07001887 struct ieee80211_vif *vif;
1888 struct ath5k_vif *avf;
1889 struct ath5k_buf *bf;
Bob Copelandcec8db22009-07-04 12:59:51 -04001890 struct sk_buff *skb;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001891
Bruno Randolfbe9b7252008-01-23 10:27:51 +09001892 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON, "in beacon_send\n");
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001893
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001894 /*
1895 * Check if the previous beacon has gone out. If
Bob Copelanda180a132010-08-15 13:03:12 -04001896 * not, don't don't try to post another: skip this
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001897 * period and wait for the next. Missed beacons
1898 * indicate a problem and should not occur. If we
1899 * miss too many consecutive beacons reset the device.
1900 */
1901 if (unlikely(ath5k_hw_num_tx_pending(ah, sc->bhalq) != 0)) {
1902 sc->bmisscount++;
Bruno Randolfbe9b7252008-01-23 10:27:51 +09001903 ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001904 "missed %u consecutive beacons\n", sc->bmisscount);
Nick Kossifidis428cbd42009-04-30 15:55:47 -04001905 if (sc->bmisscount > 10) { /* NB: 10 is a guess */
Bruno Randolfbe9b7252008-01-23 10:27:51 +09001906 ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001907 "stuck beacon time (%u missed)\n",
1908 sc->bmisscount);
Bruno Randolf8d67a032010-06-16 19:11:12 +09001909 ATH5K_DBG(sc, ATH5K_DEBUG_RESET,
1910 "stuck beacon, resetting\n");
Bob Copeland5faaff72010-07-13 11:32:40 -04001911 ieee80211_queue_work(sc->hw, &sc->reset_work);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001912 }
1913 return;
1914 }
1915 if (unlikely(sc->bmisscount != 0)) {
Bruno Randolfbe9b7252008-01-23 10:27:51 +09001916 ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001917 "resume beacon xmit after %u misses\n",
1918 sc->bmisscount);
1919 sc->bmisscount = 0;
1920 }
1921
Ben Greearb1ae1ed2010-09-30 12:22:58 -07001922 if (sc->opmode == NL80211_IFTYPE_AP && sc->num_ap_vifs > 1) {
1923 u64 tsf = ath5k_hw_get_tsf64(ah);
1924 u32 tsftu = TSF_TO_TU(tsf);
1925 int slot = ((tsftu % sc->bintval) * ATH_BCBUF) / sc->bintval;
1926 vif = sc->bslot[(slot + 1) % ATH_BCBUF];
1927 ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
1928 "tsf %llx tsftu %x intval %u slot %u vif %p\n",
1929 (unsigned long long)tsf, tsftu, sc->bintval, slot, vif);
1930 } else /* only one interface */
1931 vif = sc->bslot[0];
1932
1933 if (!vif)
1934 return;
1935
1936 avf = (void *)vif->drv_priv;
1937 bf = avf->bbuf;
1938 if (unlikely(bf->skb == NULL || sc->opmode == NL80211_IFTYPE_STATION ||
1939 sc->opmode == NL80211_IFTYPE_MONITOR)) {
1940 ATH5K_WARN(sc, "bf=%p bf_skb=%p\n", bf, bf ? bf->skb : NULL);
1941 return;
1942 }
1943
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001944 /*
1945 * Stop any current dma and put the new frame on the queue.
1946 * This should never fail since we check above that no frames
1947 * are still pending on the queue.
1948 */
1949 if (unlikely(ath5k_hw_stop_tx_dma(ah, sc->bhalq))) {
Nick Kossifidis428cbd42009-04-30 15:55:47 -04001950 ATH5K_WARN(sc, "beacon queue %u didn't start/stop ?\n", sc->bhalq);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001951 /* NB: hw still stops DMA, so proceed */
1952 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001953
Bob Copeland1071db82009-05-18 10:59:52 -04001954 /* refresh the beacon for AP mode */
1955 if (sc->opmode == NL80211_IFTYPE_AP)
Ben Greearb1ae1ed2010-09-30 12:22:58 -07001956 ath5k_beacon_update(sc->hw, vif);
Bob Copeland1071db82009-05-18 10:59:52 -04001957
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001958 ath5k_hw_set_txdp(ah, sc->bhalq, bf->daddr);
1959 ath5k_hw_start_tx_dma(ah, sc->bhalq);
Bruno Randolfbe9b7252008-01-23 10:27:51 +09001960 ATH5K_DBG(sc, ATH5K_DEBUG_BEACON, "TXDP[%u] = %llx (%p)\n",
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001961 sc->bhalq, (unsigned long long)bf->daddr, bf->desc);
1962
Ben Greearb1ae1ed2010-09-30 12:22:58 -07001963 skb = ieee80211_get_buffered_bc(sc->hw, vif);
Bob Copelandcec8db22009-07-04 12:59:51 -04001964 while (skb) {
1965 ath5k_tx_queue(sc->hw, skb, sc->cabq);
Ben Greearb1ae1ed2010-09-30 12:22:58 -07001966 skb = ieee80211_get_buffered_bc(sc->hw, vif);
Bob Copelandcec8db22009-07-04 12:59:51 -04001967 }
1968
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001969 sc->bsent++;
1970}
1971
Bruno Randolf9804b982008-01-19 18:17:59 +09001972/**
1973 * ath5k_beacon_update_timers - update beacon timers
1974 *
1975 * @sc: struct ath5k_softc pointer we are operating on
1976 * @bc_tsf: the timestamp of the beacon. 0 to reset the TSF. -1 to perform a
1977 * beacon timer update based on the current HW TSF.
1978 *
1979 * Calculate the next target beacon transmit time (TBTT) based on the timestamp
1980 * of a received beacon or the current local hardware TSF and write it to the
1981 * beacon timer registers.
1982 *
1983 * This is called in a variety of situations, e.g. when a beacon is received,
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001984 * when a TSF update has been detected, but also when an new IBSS is created or
Bruno Randolf9804b982008-01-19 18:17:59 +09001985 * when we otherwise know we have to update the timers, but we keep it in this
1986 * function to have it all together in one place.
1987 */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001988static void
Bruno Randolf9804b982008-01-19 18:17:59 +09001989ath5k_beacon_update_timers(struct ath5k_softc *sc, u64 bc_tsf)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001990{
1991 struct ath5k_hw *ah = sc->ah;
Bruno Randolf9804b982008-01-19 18:17:59 +09001992 u32 nexttbtt, intval, hw_tu, bc_tu;
1993 u64 hw_tsf;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001994
1995 intval = sc->bintval & AR5K_BEACON_PERIOD;
Ben Greearb1ae1ed2010-09-30 12:22:58 -07001996 if (sc->opmode == NL80211_IFTYPE_AP && sc->num_ap_vifs > 1) {
1997 intval /= ATH_BCBUF; /* staggered multi-bss beacons */
1998 if (intval < 15)
1999 ATH5K_WARN(sc, "intval %u is too low, min 15\n",
2000 intval);
2001 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002002 if (WARN_ON(!intval))
2003 return;
2004
Bruno Randolf9804b982008-01-19 18:17:59 +09002005 /* beacon TSF converted to TU */
2006 bc_tu = TSF_TO_TU(bc_tsf);
2007
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002008 /* current TSF converted to TU */
Bruno Randolf9804b982008-01-19 18:17:59 +09002009 hw_tsf = ath5k_hw_get_tsf64(ah);
2010 hw_tu = TSF_TO_TU(hw_tsf);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002011
Bruno Randolf11f21df2010-09-27 12:22:26 +09002012#define FUDGE AR5K_TUNE_SW_BEACON_RESP + 3
2013 /* We use FUDGE to make sure the next TBTT is ahead of the current TU.
2014 * Since we later substract AR5K_TUNE_SW_BEACON_RESP (10) in the timer
2015 * configuration we need to make sure it is bigger than that. */
2016
Bruno Randolf9804b982008-01-19 18:17:59 +09002017 if (bc_tsf == -1) {
2018 /*
2019 * no beacons received, called internally.
2020 * just need to refresh timers based on HW TSF.
2021 */
2022 nexttbtt = roundup(hw_tu + FUDGE, intval);
2023 } else if (bc_tsf == 0) {
2024 /*
2025 * no beacon received, probably called by ath5k_reset_tsf().
2026 * reset TSF to start with 0.
2027 */
2028 nexttbtt = intval;
2029 intval |= AR5K_BEACON_RESET_TSF;
2030 } else if (bc_tsf > hw_tsf) {
2031 /*
2032 * beacon received, SW merge happend but HW TSF not yet updated.
2033 * not possible to reconfigure timers yet, but next time we
2034 * receive a beacon with the same BSSID, the hardware will
2035 * automatically update the TSF and then we need to reconfigure
2036 * the timers.
2037 */
2038 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
2039 "need to wait for HW TSF sync\n");
2040 return;
2041 } else {
2042 /*
2043 * most important case for beacon synchronization between STA.
2044 *
2045 * beacon received and HW TSF has been already updated by HW.
2046 * update next TBTT based on the TSF of the beacon, but make
2047 * sure it is ahead of our local TSF timer.
2048 */
2049 nexttbtt = bc_tu + roundup(hw_tu + FUDGE - bc_tu, intval);
2050 }
2051#undef FUDGE
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002052
Bruno Randolf036cd1e2008-01-19 18:18:21 +09002053 sc->nexttbtt = nexttbtt;
2054
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002055 intval |= AR5K_BEACON_ENA;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002056 ath5k_hw_init_beacon(ah, nexttbtt, intval);
Bruno Randolf9804b982008-01-19 18:17:59 +09002057
2058 /*
2059 * debugging output last in order to preserve the time critical aspect
2060 * of this function
2061 */
2062 if (bc_tsf == -1)
2063 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
2064 "reconfigured timers based on HW TSF\n");
2065 else if (bc_tsf == 0)
2066 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
2067 "reset HW TSF and timers\n");
2068 else
2069 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
2070 "updated timers based on beacon TSF\n");
2071
2072 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
David Miller04f93a82008-02-15 16:08:59 -08002073 "bc_tsf %llx hw_tsf %llx bc_tu %u hw_tu %u nexttbtt %u\n",
2074 (unsigned long long) bc_tsf,
2075 (unsigned long long) hw_tsf, bc_tu, hw_tu, nexttbtt);
Bruno Randolf9804b982008-01-19 18:17:59 +09002076 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON, "intval %u %s %s\n",
2077 intval & AR5K_BEACON_PERIOD,
2078 intval & AR5K_BEACON_ENA ? "AR5K_BEACON_ENA" : "",
2079 intval & AR5K_BEACON_RESET_TSF ? "AR5K_BEACON_RESET_TSF" : "");
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002080}
2081
Bruno Randolf036cd1e2008-01-19 18:18:21 +09002082/**
2083 * ath5k_beacon_config - Configure the beacon queues and interrupts
2084 *
2085 * @sc: struct ath5k_softc pointer we are operating on
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002086 *
Bruno Randolf036cd1e2008-01-19 18:18:21 +09002087 * In IBSS mode we use a self-linked tx descriptor if possible. We enable SWBA
Bruno Randolf6ba81c22008-03-05 18:36:26 +09002088 * interrupts to detect TSF updates only.
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002089 */
2090static void
2091ath5k_beacon_config(struct ath5k_softc *sc)
2092{
2093 struct ath5k_hw *ah = sc->ah;
Bob Copelandb5f03952009-02-15 12:06:10 -05002094 unsigned long flags;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002095
Bob Copeland21800492009-07-04 12:59:52 -04002096 spin_lock_irqsave(&sc->block, flags);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002097 sc->bmisscount = 0;
Jiri Slabydc1968e2008-07-23 13:17:34 +02002098 sc->imask &= ~(AR5K_INT_BMISS | AR5K_INT_SWBA);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002099
Bob Copeland21800492009-07-04 12:59:52 -04002100 if (sc->enable_beacon) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002101 /*
Bruno Randolf036cd1e2008-01-19 18:18:21 +09002102 * In IBSS mode we use a self-linked tx descriptor and let the
2103 * hardware send the beacons automatically. We have to load it
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002104 * only once here.
Bruno Randolf036cd1e2008-01-19 18:18:21 +09002105 * We use the SWBA interrupt only to keep track of the beacon
Bruno Randolf6ba81c22008-03-05 18:36:26 +09002106 * timers in order to detect automatic TSF updates.
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002107 */
2108 ath5k_beaconq_config(sc);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002109
Bruno Randolf036cd1e2008-01-19 18:18:21 +09002110 sc->imask |= AR5K_INT_SWBA;
2111
Jiri Slabyda966bc2008-10-12 22:54:10 +02002112 if (sc->opmode == NL80211_IFTYPE_ADHOC) {
Bob Copeland21800492009-07-04 12:59:52 -04002113 if (ath5k_hw_hasveol(ah))
Jiri Slabyda966bc2008-10-12 22:54:10 +02002114 ath5k_beacon_send(sc);
Jiri Slabyda966bc2008-10-12 22:54:10 +02002115 } else
2116 ath5k_beacon_update_timers(sc, -1);
Bob Copeland21800492009-07-04 12:59:52 -04002117 } else {
2118 ath5k_hw_stop_tx_dma(sc->ah, sc->bhalq);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002119 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002120
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03002121 ath5k_hw_set_imr(ah, sc->imask);
Bob Copeland21800492009-07-04 12:59:52 -04002122 mmiowb();
2123 spin_unlock_irqrestore(&sc->block, flags);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002124}
2125
Nick Kossifidis428cbd42009-04-30 15:55:47 -04002126static void ath5k_tasklet_beacon(unsigned long data)
2127{
2128 struct ath5k_softc *sc = (struct ath5k_softc *) data;
2129
2130 /*
2131 * Software beacon alert--time to send a beacon.
2132 *
2133 * In IBSS mode we use this interrupt just to
2134 * keep track of the next TBTT (target beacon
2135 * transmission time) in order to detect wether
2136 * automatic TSF updates happened.
2137 */
2138 if (sc->opmode == NL80211_IFTYPE_ADHOC) {
2139 /* XXX: only if VEOL suppported */
2140 u64 tsf = ath5k_hw_get_tsf64(sc->ah);
2141 sc->nexttbtt += sc->bintval;
2142 ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
2143 "SWBA nexttbtt: %x hw_tu: %x "
2144 "TSF: %llx\n",
2145 sc->nexttbtt,
2146 TSF_TO_TU(tsf),
2147 (unsigned long long) tsf);
2148 } else {
2149 spin_lock(&sc->block);
2150 ath5k_beacon_send(sc);
2151 spin_unlock(&sc->block);
2152 }
2153}
2154
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002155
2156/********************\
2157* Interrupt handling *
2158\********************/
2159
Bruno Randolf6a8a3f62010-03-25 14:49:19 +09002160static void
2161ath5k_intr_calibration_poll(struct ath5k_hw *ah)
2162{
Bruno Randolf2111ac02010-04-02 18:44:08 +09002163 if (time_is_before_eq_jiffies(ah->ah_cal_next_ani) &&
2164 !(ah->ah_cal_mask & AR5K_CALIBRATION_FULL)) {
2165 /* run ANI only when full calibration is not active */
2166 ah->ah_cal_next_ani = jiffies +
2167 msecs_to_jiffies(ATH5K_TUNE_CALIBRATION_INTERVAL_ANI);
2168 tasklet_schedule(&ah->ah_sc->ani_tasklet);
2169
2170 } else if (time_is_before_eq_jiffies(ah->ah_cal_next_full)) {
Bruno Randolf6a8a3f62010-03-25 14:49:19 +09002171 ah->ah_cal_next_full = jiffies +
2172 msecs_to_jiffies(ATH5K_TUNE_CALIBRATION_INTERVAL_FULL);
2173 tasklet_schedule(&ah->ah_sc->calib);
2174 }
2175 /* we could use SWI to generate enough interrupts to meet our
2176 * calibration interval requirements, if necessary:
2177 * AR5K_REG_ENABLE_BITS(ah, AR5K_CR, AR5K_CR_SWI); */
2178}
2179
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002180static irqreturn_t
2181ath5k_intr(int irq, void *dev_id)
2182{
2183 struct ath5k_softc *sc = dev_id;
2184 struct ath5k_hw *ah = sc->ah;
2185 enum ath5k_int status;
2186 unsigned int counter = 1000;
2187
2188 if (unlikely(test_bit(ATH_STAT_INVALID, sc->status) ||
2189 !ath5k_hw_is_intr_pending(ah)))
2190 return IRQ_NONE;
2191
2192 do {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002193 ath5k_hw_get_isr(ah, &status); /* NB: clears IRQ too */
2194 ATH5K_DBG(sc, ATH5K_DEBUG_INTR, "status 0x%x/0x%x\n",
2195 status, sc->imask);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002196 if (unlikely(status & AR5K_INT_FATAL)) {
2197 /*
2198 * Fatal errors are unrecoverable.
2199 * Typically these are caused by DMA errors.
2200 */
Bruno Randolf8d67a032010-06-16 19:11:12 +09002201 ATH5K_DBG(sc, ATH5K_DEBUG_RESET,
2202 "fatal int, resetting\n");
Bob Copeland5faaff72010-07-13 11:32:40 -04002203 ieee80211_queue_work(sc->hw, &sc->reset_work);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002204 } else if (unlikely(status & AR5K_INT_RXORN)) {
Bruno Randolf87d77c42010-04-12 16:38:52 +09002205 /*
2206 * Receive buffers are full. Either the bus is busy or
2207 * the CPU is not fast enough to process all received
2208 * frames.
2209 * Older chipsets need a reset to come out of this
2210 * condition, but we treat it as RX for newer chips.
2211 * We don't know exactly which versions need a reset -
2212 * this guess is copied from the HAL.
2213 */
2214 sc->stats.rxorn_intr++;
Bruno Randolf8d67a032010-06-16 19:11:12 +09002215 if (ah->ah_mac_srev < AR5K_SREV_AR5212) {
2216 ATH5K_DBG(sc, ATH5K_DEBUG_RESET,
2217 "rx overrun, resetting\n");
Bob Copeland5faaff72010-07-13 11:32:40 -04002218 ieee80211_queue_work(sc->hw, &sc->reset_work);
Bruno Randolf8d67a032010-06-16 19:11:12 +09002219 }
Bruno Randolf87d77c42010-04-12 16:38:52 +09002220 else
2221 tasklet_schedule(&sc->rxtq);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002222 } else {
2223 if (status & AR5K_INT_SWBA) {
Bob Copeland56d2ac72009-04-15 07:57:33 -04002224 tasklet_hi_schedule(&sc->beacontq);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002225 }
2226 if (status & AR5K_INT_RXEOL) {
2227 /*
2228 * NB: the hardware should re-read the link when
2229 * RXE bit is written, but it doesn't work at
2230 * least on older hardware revs.
2231 */
Bruno Randolfb3f194e2010-07-14 10:53:29 +09002232 sc->stats.rxeol_intr++;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002233 }
2234 if (status & AR5K_INT_TXURN) {
2235 /* bump tx trigger level */
2236 ath5k_hw_update_tx_triglevel(ah, true);
2237 }
Nick Kossifidis4c674c62008-10-26 20:40:25 +02002238 if (status & (AR5K_INT_RXOK | AR5K_INT_RXERR))
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002239 tasklet_schedule(&sc->rxtq);
Nick Kossifidis4c674c62008-10-26 20:40:25 +02002240 if (status & (AR5K_INT_TXOK | AR5K_INT_TXDESC
2241 | AR5K_INT_TXERR | AR5K_INT_TXEOL))
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002242 tasklet_schedule(&sc->txtq);
2243 if (status & AR5K_INT_BMISS) {
Nick Kossifidis1e3e6e82009-02-09 06:15:42 +02002244 /* TODO */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002245 }
2246 if (status & AR5K_INT_MIB) {
Bruno Randolf2111ac02010-04-02 18:44:08 +09002247 sc->stats.mib_intr++;
Bruno Randolf495391d2010-03-25 14:49:36 +09002248 ath5k_hw_update_mib_counters(ah);
Bruno Randolf2111ac02010-04-02 18:44:08 +09002249 ath5k_ani_mib_intr(ah);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002250 }
Tobias Doerffele6a3b612009-06-09 17:33:27 +02002251 if (status & AR5K_INT_GPIO)
Tobias Doerffele6a3b612009-06-09 17:33:27 +02002252 tasklet_schedule(&sc->rf_kill.toggleq);
Bob Copelanda6ae0712009-06-09 23:43:11 -04002253
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002254 }
Bob Copeland2516baa2009-04-27 22:18:10 -04002255 } while (ath5k_hw_is_intr_pending(ah) && --counter > 0);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002256
2257 if (unlikely(!counter))
2258 ATH5K_WARN(sc, "too many interrupts, giving up for now\n");
2259
Bruno Randolf6a8a3f62010-03-25 14:49:19 +09002260 ath5k_intr_calibration_poll(ah);
Nick Kossifidis6e220662009-08-10 03:31:31 +03002261
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002262 return IRQ_HANDLED;
2263}
2264
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002265/*
2266 * Periodically recalibrate the PHY to account
2267 * for temperature/environment changes.
2268 */
2269static void
Nick Kossifidis6e220662009-08-10 03:31:31 +03002270ath5k_tasklet_calibrate(unsigned long data)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002271{
2272 struct ath5k_softc *sc = (void *)data;
2273 struct ath5k_hw *ah = sc->ah;
2274
Nick Kossifidis6e220662009-08-10 03:31:31 +03002275 /* Only full calibration for now */
Bruno Randolfe65e1d72010-03-25 14:49:09 +09002276 ah->ah_cal_mask |= AR5K_CALIBRATION_FULL;
Nick Kossifidis6e220662009-08-10 03:31:31 +03002277
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002278 ATH5K_DBG(sc, ATH5K_DEBUG_CALIBRATE, "channel %u/%x\n",
Luis R. Rodriguez400ec452008-02-03 21:51:49 -05002279 ieee80211_frequency_to_channel(sc->curchan->center_freq),
2280 sc->curchan->hw_value);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002281
Nick Kossifidis6f3b4142009-02-09 06:03:41 +02002282 if (ath5k_hw_gainf_calibrate(ah) == AR5K_RFGAIN_NEED_CHANGE) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002283 /*
2284 * Rfgain is out of bounds, reset the chip
2285 * to load new gain values.
2286 */
2287 ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "calibration, resetting\n");
Bob Copeland5faaff72010-07-13 11:32:40 -04002288 ieee80211_queue_work(sc->hw, &sc->reset_work);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002289 }
2290 if (ath5k_hw_phy_calibrate(ah, sc->curchan))
2291 ATH5K_ERR(sc, "calibration of channel %u failed\n",
Luis R. Rodriguez400ec452008-02-03 21:51:49 -05002292 ieee80211_frequency_to_channel(
2293 sc->curchan->center_freq));
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002294
Bruno Randolf0e8e02d2010-05-19 10:31:05 +09002295 /* Noise floor calibration interrupts rx/tx path while I/Q calibration
Bruno Randolf651d9372010-09-17 11:36:46 +09002296 * doesn't.
2297 * TODO: We should stop TX here, so that it doesn't interfere.
2298 * Note that stopping the queues is not enough to stop TX! */
Bruno Randolfafe86282010-05-19 10:31:10 +09002299 if (time_is_before_eq_jiffies(ah->ah_cal_next_nf)) {
2300 ah->ah_cal_next_nf = jiffies +
2301 msecs_to_jiffies(ATH5K_TUNE_CALIBRATION_INTERVAL_NF);
Bruno Randolfafe86282010-05-19 10:31:10 +09002302 ath5k_hw_update_noise_floor(ah);
Bruno Randolfafe86282010-05-19 10:31:10 +09002303 }
Nick Kossifidis6e220662009-08-10 03:31:31 +03002304
Bruno Randolfe65e1d72010-03-25 14:49:09 +09002305 ah->ah_cal_mask &= ~AR5K_CALIBRATION_FULL;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002306}
2307
2308
Bruno Randolf2111ac02010-04-02 18:44:08 +09002309static void
2310ath5k_tasklet_ani(unsigned long data)
2311{
2312 struct ath5k_softc *sc = (void *)data;
2313 struct ath5k_hw *ah = sc->ah;
2314
2315 ah->ah_cal_mask |= AR5K_CALIBRATION_ANI;
2316 ath5k_ani_calibration(ah);
2317 ah->ah_cal_mask &= ~AR5K_CALIBRATION_ANI;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002318}
2319
2320
Bruno Randolf4edd7612010-09-17 11:36:56 +09002321static void
2322ath5k_tx_complete_poll_work(struct work_struct *work)
2323{
2324 struct ath5k_softc *sc = container_of(work, struct ath5k_softc,
2325 tx_complete_work.work);
2326 struct ath5k_txq *txq;
2327 int i;
2328 bool needreset = false;
2329
2330 for (i = 0; i < ARRAY_SIZE(sc->txqs); i++) {
2331 if (sc->txqs[i].setup) {
2332 txq = &sc->txqs[i];
2333 spin_lock_bh(&txq->lock);
Bruno Randolf23413292010-09-17 11:37:07 +09002334 if (txq->txq_len > 1) {
Bruno Randolf4edd7612010-09-17 11:36:56 +09002335 if (txq->txq_poll_mark) {
2336 ATH5K_DBG(sc, ATH5K_DEBUG_XMIT,
2337 "TX queue stuck %d\n",
2338 txq->qnum);
2339 needreset = true;
Bruno Randolf923e5b32010-09-17 11:37:02 +09002340 txq->txq_stuck++;
Bruno Randolf4edd7612010-09-17 11:36:56 +09002341 spin_unlock_bh(&txq->lock);
2342 break;
2343 } else {
2344 txq->txq_poll_mark = true;
2345 }
2346 }
2347 spin_unlock_bh(&txq->lock);
2348 }
2349 }
2350
2351 if (needreset) {
2352 ATH5K_DBG(sc, ATH5K_DEBUG_RESET,
2353 "TX queues stuck, resetting\n");
2354 ath5k_reset(sc, sc->curchan);
2355 }
2356
2357 ieee80211_queue_delayed_work(sc->hw, &sc->tx_complete_work,
2358 msecs_to_jiffies(ATH5K_TX_COMPLETE_POLL_INT));
2359}
2360
2361
Bob Copeland8a63fac2010-09-17 12:45:07 +09002362/*************************\
2363* Initialization routines *
2364\*************************/
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002365
2366static int
Bob Copeland8a63fac2010-09-17 12:45:07 +09002367ath5k_stop_locked(struct ath5k_softc *sc)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002368{
Bob Copeland8a63fac2010-09-17 12:45:07 +09002369 struct ath5k_hw *ah = sc->ah;
Bob Copelandcec8db22009-07-04 12:59:51 -04002370
Bob Copeland8a63fac2010-09-17 12:45:07 +09002371 ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "invalid %u\n",
2372 test_bit(ATH_STAT_INVALID, sc->status));
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002373
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002374 /*
Bob Copeland8a63fac2010-09-17 12:45:07 +09002375 * Shutdown the hardware and driver:
2376 * stop output from above
2377 * disable interrupts
2378 * turn off timers
2379 * turn off the radio
2380 * clear transmit machinery
2381 * clear receive machinery
2382 * drain and release tx queues
2383 * reclaim beacon resources
2384 * power down hardware
2385 *
2386 * Note that some of this work is not possible if the
2387 * hardware is gone (invalid).
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002388 */
Bob Copeland8a63fac2010-09-17 12:45:07 +09002389 ieee80211_stop_queues(sc->hw);
2390
2391 if (!test_bit(ATH_STAT_INVALID, sc->status)) {
2392 ath5k_led_off(sc);
2393 ath5k_hw_set_imr(ah, 0);
2394 synchronize_irq(sc->pdev->irq);
2395 }
2396 ath5k_txq_cleanup(sc);
2397 if (!test_bit(ATH_STAT_INVALID, sc->status)) {
2398 ath5k_rx_stop(sc);
2399 ath5k_hw_phy_disable(ah);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002400 }
2401
Bob Copeland8a63fac2010-09-17 12:45:07 +09002402 return 0;
2403}
2404
2405static int
2406ath5k_init(struct ath5k_softc *sc)
2407{
2408 struct ath5k_hw *ah = sc->ah;
2409 struct ath_common *common = ath5k_hw_common(ah);
2410 int ret, i;
2411
2412 mutex_lock(&sc->lock);
2413
2414 ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "mode %d\n", sc->opmode);
2415
2416 /*
2417 * Stop anything previously setup. This is safe
2418 * no matter this is the first time through or not.
2419 */
2420 ath5k_stop_locked(sc);
2421
2422 /*
2423 * The basic interface to setting the hardware in a good
2424 * state is ``reset''. On return the hardware is known to
2425 * be powered up and with interrupts disabled. This must
2426 * be followed by initialization of the appropriate bits
2427 * and then setup of the interrupt mask.
2428 */
2429 sc->curchan = sc->hw->conf.channel;
2430 sc->curband = &sc->sbands[sc->curchan->band];
2431 sc->imask = AR5K_INT_RXOK | AR5K_INT_RXERR | AR5K_INT_RXEOL |
2432 AR5K_INT_RXORN | AR5K_INT_TXDESC | AR5K_INT_TXEOL |
2433 AR5K_INT_FATAL | AR5K_INT_GLOBAL | AR5K_INT_MIB;
2434
2435 ret = ath5k_reset(sc, NULL);
2436 if (ret)
2437 goto done;
2438
2439 ath5k_rfkill_hw_start(ah);
2440
2441 /*
2442 * Reset the key cache since some parts do not reset the
2443 * contents on initial power up or resume from suspend.
2444 */
2445 for (i = 0; i < common->keymax; i++)
2446 ath_hw_keyreset(common, (u16) i);
2447
2448 ath5k_hw_set_ack_bitrate_high(ah, true);
Ben Greearb1ae1ed2010-09-30 12:22:58 -07002449
2450 for (i = 0; i < ARRAY_SIZE(sc->bslot); i++)
2451 sc->bslot[i] = NULL;
2452
Bob Copeland8a63fac2010-09-17 12:45:07 +09002453 ret = 0;
2454done:
2455 mmiowb();
2456 mutex_unlock(&sc->lock);
Bruno Randolf4edd7612010-09-17 11:36:56 +09002457
2458 ieee80211_queue_delayed_work(sc->hw, &sc->tx_complete_work,
2459 msecs_to_jiffies(ATH5K_TX_COMPLETE_POLL_INT));
2460
Bob Copeland8a63fac2010-09-17 12:45:07 +09002461 return ret;
2462}
2463
2464static void stop_tasklets(struct ath5k_softc *sc)
2465{
2466 tasklet_kill(&sc->rxtq);
2467 tasklet_kill(&sc->txtq);
2468 tasklet_kill(&sc->calib);
2469 tasklet_kill(&sc->beacontq);
2470 tasklet_kill(&sc->ani_tasklet);
2471}
2472
2473/*
2474 * Stop the device, grabbing the top-level lock to protect
2475 * against concurrent entry through ath5k_init (which can happen
2476 * if another thread does a system call and the thread doing the
2477 * stop is preempted).
2478 */
2479static int
2480ath5k_stop_hw(struct ath5k_softc *sc)
2481{
2482 int ret;
2483
2484 mutex_lock(&sc->lock);
2485 ret = ath5k_stop_locked(sc);
2486 if (ret == 0 && !test_bit(ATH_STAT_INVALID, sc->status)) {
2487 /*
2488 * Don't set the card in full sleep mode!
2489 *
2490 * a) When the device is in this state it must be carefully
2491 * woken up or references to registers in the PCI clock
2492 * domain may freeze the bus (and system). This varies
2493 * by chip and is mostly an issue with newer parts
2494 * (madwifi sources mentioned srev >= 0x78) that go to
2495 * sleep more quickly.
2496 *
2497 * b) On older chips full sleep results a weird behaviour
2498 * during wakeup. I tested various cards with srev < 0x78
2499 * and they don't wake up after module reload, a second
2500 * module reload is needed to bring the card up again.
2501 *
2502 * Until we figure out what's going on don't enable
2503 * full chip reset on any chip (this is what Legacy HAL
2504 * and Sam's HAL do anyway). Instead Perform a full reset
2505 * on the device (same as initial state after attach) and
2506 * leave it idle (keep MAC/BB on warm reset) */
2507 ret = ath5k_hw_on_hold(sc->ah);
2508
2509 ATH5K_DBG(sc, ATH5K_DEBUG_RESET,
2510 "putting device to sleep\n");
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002511 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002512
Bob Copeland8a63fac2010-09-17 12:45:07 +09002513 mmiowb();
2514 mutex_unlock(&sc->lock);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002515
Bob Copeland8a63fac2010-09-17 12:45:07 +09002516 stop_tasklets(sc);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002517
Bruno Randolf4edd7612010-09-17 11:36:56 +09002518 cancel_delayed_work_sync(&sc->tx_complete_work);
2519
Bob Copeland8a63fac2010-09-17 12:45:07 +09002520 ath5k_rfkill_hw_stop(sc->ah);
2521
2522 return ret;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002523}
2524
Bob Copeland209d8892009-05-07 08:09:08 -04002525/*
2526 * Reset the hardware. If chan is not NULL, then also pause rx/tx
2527 * and change to the given channel.
Bob Copeland5faaff72010-07-13 11:32:40 -04002528 *
2529 * This should be called with sc->lock.
Bob Copeland209d8892009-05-07 08:09:08 -04002530 */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002531static int
Bob Copeland209d8892009-05-07 08:09:08 -04002532ath5k_reset(struct ath5k_softc *sc, struct ieee80211_channel *chan)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002533{
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002534 struct ath5k_hw *ah = sc->ah;
2535 int ret;
2536
2537 ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "resetting\n");
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002538
Bob Copeland450464d2010-07-13 11:32:41 -04002539 ath5k_hw_set_imr(ah, 0);
2540 synchronize_irq(sc->pdev->irq);
2541 stop_tasklets(sc);
2542
Bob Copeland209d8892009-05-07 08:09:08 -04002543 if (chan) {
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002544 ath5k_txq_cleanup(sc);
2545 ath5k_rx_stop(sc);
Bob Copeland209d8892009-05-07 08:09:08 -04002546
2547 sc->curchan = chan;
2548 sc->curband = &sc->sbands[chan->band];
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002549 }
Bob Copeland33554432009-07-04 21:03:13 -04002550 ret = ath5k_hw_reset(ah, sc->opmode, sc->curchan, chan != NULL);
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002551 if (ret) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002552 ATH5K_ERR(sc, "can't reset hardware (%d)\n", ret);
2553 goto err;
2554 }
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002555
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002556 ret = ath5k_rx_start(sc);
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002557 if (ret) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002558 ATH5K_ERR(sc, "can't start recv logic\n");
2559 goto err;
2560 }
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002561
Bruno Randolf2111ac02010-04-02 18:44:08 +09002562 ath5k_ani_init(ah, ah->ah_sc->ani_state.ani_mode);
2563
Bruno Randolfac559522010-05-19 10:30:55 +09002564 ah->ah_cal_next_full = jiffies;
2565 ah->ah_cal_next_ani = jiffies;
Bruno Randolfafe86282010-05-19 10:31:10 +09002566 ah->ah_cal_next_nf = jiffies;
2567
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002568 /*
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002569 * Change channels and update the h/w rate map if we're switching;
2570 * e.g. 11a to 11b/g.
2571 *
2572 * We may be doing a reset in response to an ioctl that changes the
2573 * channel so update any state that might change as a result.
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002574 *
2575 * XXX needed?
2576 */
2577/* ath5k_chan_change(sc, c); */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002578
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002579 ath5k_beacon_config(sc);
2580 /* intrs are enabled by ath5k_beacon_config */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002581
Bruno Randolf397f3852010-05-19 10:30:49 +09002582 ieee80211_wake_queues(sc->hw);
2583
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002584 return 0;
2585err:
2586 return ret;
2587}
2588
Bob Copeland5faaff72010-07-13 11:32:40 -04002589static void ath5k_reset_work(struct work_struct *work)
2590{
2591 struct ath5k_softc *sc = container_of(work, struct ath5k_softc,
2592 reset_work);
2593
2594 mutex_lock(&sc->lock);
2595 ath5k_reset(sc, sc->curchan);
2596 mutex_unlock(&sc->lock);
2597}
2598
Bob Copeland8a63fac2010-09-17 12:45:07 +09002599static int
2600ath5k_attach(struct pci_dev *pdev, struct ieee80211_hw *hw)
2601{
2602 struct ath5k_softc *sc = hw->priv;
2603 struct ath5k_hw *ah = sc->ah;
2604 struct ath_regulatory *regulatory = ath5k_hw_regulatory(ah);
Bruno Randolf925e0b02010-09-17 11:36:35 +09002605 struct ath5k_txq *txq;
Bob Copeland8a63fac2010-09-17 12:45:07 +09002606 u8 mac[ETH_ALEN] = {};
2607 int ret;
2608
2609 ATH5K_DBG(sc, ATH5K_DEBUG_ANY, "devid 0x%x\n", pdev->device);
2610
2611 /*
2612 * Check if the MAC has multi-rate retry support.
2613 * We do this by trying to setup a fake extended
2614 * descriptor. MACs that don't have support will
2615 * return false w/o doing anything. MACs that do
2616 * support it will return true w/o doing anything.
2617 */
2618 ret = ath5k_hw_setup_mrr_tx_desc(ah, NULL, 0, 0, 0, 0, 0, 0);
2619
2620 if (ret < 0)
2621 goto err;
2622 if (ret > 0)
2623 __set_bit(ATH_STAT_MRRETRY, sc->status);
2624
2625 /*
2626 * Collect the channel list. The 802.11 layer
2627 * is resposible for filtering this list based
2628 * on settings like the phy mode and regulatory
2629 * domain restrictions.
2630 */
2631 ret = ath5k_setup_bands(hw);
2632 if (ret) {
2633 ATH5K_ERR(sc, "can't get channels\n");
2634 goto err;
2635 }
2636
2637 /* NB: setup here so ath5k_rate_update is happy */
2638 if (test_bit(AR5K_MODE_11A, ah->ah_modes))
2639 ath5k_setcurmode(sc, AR5K_MODE_11A);
2640 else
2641 ath5k_setcurmode(sc, AR5K_MODE_11B);
2642
2643 /*
2644 * Allocate tx+rx descriptors and populate the lists.
2645 */
2646 ret = ath5k_desc_alloc(sc, pdev);
2647 if (ret) {
2648 ATH5K_ERR(sc, "can't allocate descriptors\n");
2649 goto err;
2650 }
2651
2652 /*
2653 * Allocate hardware transmit queues: one queue for
2654 * beacon frames and one data queue for each QoS
2655 * priority. Note that hw functions handle resetting
2656 * these queues at the needed time.
2657 */
2658 ret = ath5k_beaconq_setup(ah);
2659 if (ret < 0) {
2660 ATH5K_ERR(sc, "can't setup a beacon xmit queue\n");
2661 goto err_desc;
2662 }
2663 sc->bhalq = ret;
2664 sc->cabq = ath5k_txq_setup(sc, AR5K_TX_QUEUE_CAB, 0);
2665 if (IS_ERR(sc->cabq)) {
2666 ATH5K_ERR(sc, "can't setup cab queue\n");
2667 ret = PTR_ERR(sc->cabq);
2668 goto err_bhal;
2669 }
2670
Bruno Randolf925e0b02010-09-17 11:36:35 +09002671 /* This order matches mac80211's queue priority, so we can
2672 * directly use the mac80211 queue number without any mapping */
2673 txq = ath5k_txq_setup(sc, AR5K_TX_QUEUE_DATA, AR5K_WME_AC_VO);
2674 if (IS_ERR(txq)) {
Bob Copeland8a63fac2010-09-17 12:45:07 +09002675 ATH5K_ERR(sc, "can't setup xmit queue\n");
Bruno Randolf925e0b02010-09-17 11:36:35 +09002676 ret = PTR_ERR(txq);
Bob Copeland8a63fac2010-09-17 12:45:07 +09002677 goto err_queues;
2678 }
Bruno Randolf925e0b02010-09-17 11:36:35 +09002679 txq = ath5k_txq_setup(sc, AR5K_TX_QUEUE_DATA, AR5K_WME_AC_VI);
2680 if (IS_ERR(txq)) {
2681 ATH5K_ERR(sc, "can't setup xmit queue\n");
2682 ret = PTR_ERR(txq);
2683 goto err_queues;
2684 }
2685 txq = ath5k_txq_setup(sc, AR5K_TX_QUEUE_DATA, AR5K_WME_AC_BE);
2686 if (IS_ERR(txq)) {
2687 ATH5K_ERR(sc, "can't setup xmit queue\n");
2688 ret = PTR_ERR(txq);
2689 goto err_queues;
2690 }
2691 txq = ath5k_txq_setup(sc, AR5K_TX_QUEUE_DATA, AR5K_WME_AC_BK);
2692 if (IS_ERR(txq)) {
2693 ATH5K_ERR(sc, "can't setup xmit queue\n");
2694 ret = PTR_ERR(txq);
2695 goto err_queues;
2696 }
2697 hw->queues = 4;
Bob Copeland8a63fac2010-09-17 12:45:07 +09002698
2699 tasklet_init(&sc->rxtq, ath5k_tasklet_rx, (unsigned long)sc);
2700 tasklet_init(&sc->txtq, ath5k_tasklet_tx, (unsigned long)sc);
2701 tasklet_init(&sc->calib, ath5k_tasklet_calibrate, (unsigned long)sc);
2702 tasklet_init(&sc->beacontq, ath5k_tasklet_beacon, (unsigned long)sc);
2703 tasklet_init(&sc->ani_tasklet, ath5k_tasklet_ani, (unsigned long)sc);
2704
2705 INIT_WORK(&sc->reset_work, ath5k_reset_work);
Bruno Randolf4edd7612010-09-17 11:36:56 +09002706 INIT_DELAYED_WORK(&sc->tx_complete_work, ath5k_tx_complete_poll_work);
Bob Copeland8a63fac2010-09-17 12:45:07 +09002707
2708 ret = ath5k_eeprom_read_mac(ah, mac);
2709 if (ret) {
2710 ATH5K_ERR(sc, "unable to read address from EEPROM: 0x%04x\n",
2711 sc->pdev->device);
2712 goto err_queues;
2713 }
2714
2715 SET_IEEE80211_PERM_ADDR(hw, mac);
Ben Greearb1ae1ed2010-09-30 12:22:58 -07002716 memcpy(&sc->lladdr, mac, ETH_ALEN);
Bob Copeland8a63fac2010-09-17 12:45:07 +09002717 /* All MAC address bits matter for ACKs */
Ben Greear62c58fb2010-10-08 12:01:15 -07002718 ath5k_update_bssid_mask_and_opmode(sc, NULL);
Bob Copeland8a63fac2010-09-17 12:45:07 +09002719
2720 regulatory->current_rd = ah->ah_capabilities.cap_eeprom.ee_regdomain;
2721 ret = ath_regd_init(regulatory, hw->wiphy, ath5k_reg_notifier);
2722 if (ret) {
2723 ATH5K_ERR(sc, "can't initialize regulatory system\n");
2724 goto err_queues;
2725 }
2726
2727 ret = ieee80211_register_hw(hw);
2728 if (ret) {
2729 ATH5K_ERR(sc, "can't register ieee80211 hw\n");
2730 goto err_queues;
2731 }
2732
2733 if (!ath_is_world_regd(regulatory))
2734 regulatory_hint(hw->wiphy, regulatory->alpha2);
2735
2736 ath5k_init_leds(sc);
2737
2738 ath5k_sysfs_register(sc);
2739
2740 return 0;
2741err_queues:
2742 ath5k_txq_release(sc);
2743err_bhal:
2744 ath5k_hw_release_tx_queue(ah, sc->bhalq);
2745err_desc:
2746 ath5k_desc_free(sc, pdev);
2747err:
2748 return ret;
2749}
2750
2751static void
2752ath5k_detach(struct pci_dev *pdev, struct ieee80211_hw *hw)
2753{
2754 struct ath5k_softc *sc = hw->priv;
2755
2756 /*
2757 * NB: the order of these is important:
2758 * o call the 802.11 layer before detaching ath5k_hw to
2759 * ensure callbacks into the driver to delete global
2760 * key cache entries can be handled
2761 * o reclaim the tx queue data structures after calling
2762 * the 802.11 layer as we'll get called back to reclaim
2763 * node state and potentially want to use them
2764 * o to cleanup the tx queues the hal is called, so detach
2765 * it last
2766 * XXX: ??? detach ath5k_hw ???
2767 * Other than that, it's straightforward...
2768 */
2769 ieee80211_unregister_hw(hw);
2770 ath5k_desc_free(sc, pdev);
2771 ath5k_txq_release(sc);
2772 ath5k_hw_release_tx_queue(sc->ah, sc->bhalq);
2773 ath5k_unregister_leds(sc);
2774
2775 ath5k_sysfs_unregister(sc);
2776 /*
2777 * NB: can't reclaim these until after ieee80211_ifdetach
2778 * returns because we'll get called back to reclaim node
2779 * state and potentially want to use them.
2780 */
2781}
2782
2783/********************\
2784* Mac80211 functions *
2785\********************/
2786
2787static int
2788ath5k_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
2789{
2790 struct ath5k_softc *sc = hw->priv;
Bruno Randolf925e0b02010-09-17 11:36:35 +09002791 u16 qnum = skb_get_queue_mapping(skb);
Bob Copeland8a63fac2010-09-17 12:45:07 +09002792
Bruno Randolf925e0b02010-09-17 11:36:35 +09002793 if (WARN_ON(qnum >= sc->ah->ah_capabilities.cap_queues.q_tx_num)) {
2794 dev_kfree_skb_any(skb);
2795 return 0;
2796 }
2797
2798 return ath5k_tx_queue(hw, skb, &sc->txqs[qnum]);
Bob Copeland8a63fac2010-09-17 12:45:07 +09002799}
2800
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002801static int ath5k_start(struct ieee80211_hw *hw)
2802{
Bob Copelandbb2beca2009-01-19 11:20:54 -05002803 return ath5k_init(hw->priv);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002804}
2805
2806static void ath5k_stop(struct ieee80211_hw *hw)
2807{
Bob Copelandbb2beca2009-01-19 11:20:54 -05002808 ath5k_stop_hw(hw->priv);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002809}
2810
2811static int ath5k_add_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +01002812 struct ieee80211_vif *vif)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002813{
2814 struct ath5k_softc *sc = hw->priv;
2815 int ret;
Ben Greearb1ae1ed2010-09-30 12:22:58 -07002816 struct ath5k_vif *avf = (void *)vif->drv_priv;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002817
2818 mutex_lock(&sc->lock);
Ben Greearb1ae1ed2010-09-30 12:22:58 -07002819
2820 if ((vif->type == NL80211_IFTYPE_AP ||
2821 vif->type == NL80211_IFTYPE_ADHOC)
2822 && (sc->num_ap_vifs + sc->num_adhoc_vifs) >= ATH_BCBUF) {
2823 ret = -ELNRNG;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002824 goto end;
2825 }
2826
Ben Greearb1ae1ed2010-09-30 12:22:58 -07002827 /* Don't allow other interfaces if one ad-hoc is configured.
2828 * TODO: Fix the problems with ad-hoc and multiple other interfaces.
2829 * We would need to operate the HW in ad-hoc mode to allow TSF updates
2830 * for the IBSS, but this breaks with additional AP or STA interfaces
2831 * at the moment. */
2832 if (sc->num_adhoc_vifs ||
2833 (sc->nvifs && vif->type == NL80211_IFTYPE_ADHOC)) {
2834 ATH5K_ERR(sc, "Only one single ad-hoc interface is allowed.\n");
2835 ret = -ELNRNG;
2836 goto end;
2837 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002838
Johannes Berg1ed32e42009-12-23 13:15:45 +01002839 switch (vif->type) {
Jiri Slabyda966bc2008-10-12 22:54:10 +02002840 case NL80211_IFTYPE_AP:
Johannes Berg05c914f2008-09-11 00:01:58 +02002841 case NL80211_IFTYPE_STATION:
2842 case NL80211_IFTYPE_ADHOC:
Andrey Yurovskyb706e652008-10-13 18:23:07 -07002843 case NL80211_IFTYPE_MESH_POINT:
Ben Greearb1ae1ed2010-09-30 12:22:58 -07002844 avf->opmode = vif->type;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002845 break;
2846 default:
2847 ret = -EOPNOTSUPP;
2848 goto end;
2849 }
Jiri Slaby67d2e2d2008-08-18 21:45:28 +02002850
Ben Greearb1ae1ed2010-09-30 12:22:58 -07002851 sc->nvifs++;
2852 ATH5K_DBG(sc, ATH5K_DEBUG_MODE, "add interface mode %d\n", avf->opmode);
Bruno Randolfccfe5552010-03-09 16:55:38 +09002853
Ben Greearb1ae1ed2010-09-30 12:22:58 -07002854 /* Assign the vap/adhoc to a beacon xmit slot. */
2855 if ((avf->opmode == NL80211_IFTYPE_AP) ||
2856 (avf->opmode == NL80211_IFTYPE_ADHOC)) {
2857 int slot;
2858
2859 WARN_ON(list_empty(&sc->bcbuf));
2860 avf->bbuf = list_first_entry(&sc->bcbuf, struct ath5k_buf,
2861 list);
2862 list_del(&avf->bbuf->list);
2863
2864 avf->bslot = 0;
2865 for (slot = 0; slot < ATH_BCBUF; slot++) {
2866 if (!sc->bslot[slot]) {
2867 avf->bslot = slot;
2868 break;
2869 }
2870 }
2871 BUG_ON(sc->bslot[avf->bslot] != NULL);
2872 sc->bslot[avf->bslot] = vif;
2873 if (avf->opmode == NL80211_IFTYPE_AP)
2874 sc->num_ap_vifs++;
2875 else
2876 sc->num_adhoc_vifs++;
2877 }
2878
Ben Greearb1ae1ed2010-09-30 12:22:58 -07002879 /* Any MAC address is fine, all others are included through the
2880 * filter.
2881 */
2882 memcpy(&sc->lladdr, vif->addr, ETH_ALEN);
Johannes Berg1ed32e42009-12-23 13:15:45 +01002883 ath5k_hw_set_lladdr(sc->ah, vif->addr);
Ben Greearb1ae1ed2010-09-30 12:22:58 -07002884
2885 memcpy(&avf->lladdr, vif->addr, ETH_ALEN);
2886
2887 ath5k_mode_setup(sc, vif);
Jiri Slaby67d2e2d2008-08-18 21:45:28 +02002888
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002889 ret = 0;
2890end:
2891 mutex_unlock(&sc->lock);
2892 return ret;
2893}
2894
2895static void
2896ath5k_remove_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +01002897 struct ieee80211_vif *vif)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002898{
2899 struct ath5k_softc *sc = hw->priv;
Ben Greearb1ae1ed2010-09-30 12:22:58 -07002900 struct ath5k_vif *avf = (void *)vif->drv_priv;
2901 unsigned int i;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002902
2903 mutex_lock(&sc->lock);
Ben Greearb1ae1ed2010-09-30 12:22:58 -07002904 sc->nvifs--;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002905
Ben Greearb1ae1ed2010-09-30 12:22:58 -07002906 if (avf->bbuf) {
2907 ath5k_txbuf_free_skb(sc, avf->bbuf);
2908 list_add_tail(&avf->bbuf->list, &sc->bcbuf);
2909 for (i = 0; i < ATH_BCBUF; i++) {
2910 if (sc->bslot[i] == vif) {
2911 sc->bslot[i] = NULL;
2912 break;
2913 }
2914 }
2915 avf->bbuf = NULL;
2916 }
2917 if (avf->opmode == NL80211_IFTYPE_AP)
2918 sc->num_ap_vifs--;
2919 else if (avf->opmode == NL80211_IFTYPE_ADHOC)
2920 sc->num_adhoc_vifs--;
2921
Ben Greear62c58fb2010-10-08 12:01:15 -07002922 ath5k_update_bssid_mask_and_opmode(sc, NULL);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002923 mutex_unlock(&sc->lock);
2924}
2925
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -05002926/*
2927 * TODO: Phy disable/diversity etc
2928 */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002929static int
Johannes Berge8975582008-10-09 12:18:51 +02002930ath5k_config(struct ieee80211_hw *hw, u32 changed)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002931{
2932 struct ath5k_softc *sc = hw->priv;
Nick Kossifidisa0823812009-04-30 15:55:44 -04002933 struct ath5k_hw *ah = sc->ah;
Johannes Berge8975582008-10-09 12:18:51 +02002934 struct ieee80211_conf *conf = &hw->conf;
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04002935 int ret = 0;
Bob Copelandbe009372009-01-22 08:44:16 -05002936
2937 mutex_lock(&sc->lock);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002938
Joerg Alberte30eb4a2009-08-05 01:52:07 +02002939 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
2940 ret = ath5k_chan_set(sc, conf->channel);
2941 if (ret < 0)
2942 goto unlock;
2943 }
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04002944
Nick Kossifidisa0823812009-04-30 15:55:44 -04002945 if ((changed & IEEE80211_CONF_CHANGE_POWER) &&
2946 (sc->power_level != conf->power_level)) {
2947 sc->power_level = conf->power_level;
2948
2949 /* Half dB steps */
2950 ath5k_hw_set_txpower_limit(ah, (conf->power_level * 2));
2951 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002952
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04002953 /* TODO:
2954 * 1) Move this on config_interface and handle each case
2955 * separately eg. when we have only one STA vif, use
2956 * AR5K_ANTMODE_SINGLE_AP
2957 *
2958 * 2) Allow the user to change antenna mode eg. when only
2959 * one antenna is present
2960 *
2961 * 3) Allow the user to set default/tx antenna when possible
2962 *
2963 * 4) Default mode should handle 90% of the cases, together
2964 * with fixed a/b and single AP modes we should be able to
2965 * handle 99%. Sectored modes are extreme cases and i still
2966 * haven't found a usage for them. If we decide to support them,
2967 * then we must allow the user to set how many tx antennas we
2968 * have available
2969 */
Bruno Randolfcaec9112010-03-09 16:55:28 +09002970 ath5k_hw_set_antenna_mode(ah, ah->ah_ant_mode);
Bob Copelandbe009372009-01-22 08:44:16 -05002971
John W. Linville55aa4e02009-05-25 21:28:47 +02002972unlock:
Bob Copelandbe009372009-01-22 08:44:16 -05002973 mutex_unlock(&sc->lock);
John W. Linville55aa4e02009-05-25 21:28:47 +02002974 return ret;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002975}
2976
Johannes Berg3ac64be2009-08-17 16:16:53 +02002977static u64 ath5k_prepare_multicast(struct ieee80211_hw *hw,
Jiri Pirko22bedad2010-04-01 21:22:57 +00002978 struct netdev_hw_addr_list *mc_list)
Johannes Berg3ac64be2009-08-17 16:16:53 +02002979{
2980 u32 mfilt[2], val;
Johannes Berg3ac64be2009-08-17 16:16:53 +02002981 u8 pos;
Jiri Pirko22bedad2010-04-01 21:22:57 +00002982 struct netdev_hw_addr *ha;
Johannes Berg3ac64be2009-08-17 16:16:53 +02002983
2984 mfilt[0] = 0;
2985 mfilt[1] = 1;
2986
Jiri Pirko22bedad2010-04-01 21:22:57 +00002987 netdev_hw_addr_list_for_each(ha, mc_list) {
Johannes Berg3ac64be2009-08-17 16:16:53 +02002988 /* calculate XOR of eight 6-bit values */
Jiri Pirko22bedad2010-04-01 21:22:57 +00002989 val = get_unaligned_le32(ha->addr + 0);
Johannes Berg3ac64be2009-08-17 16:16:53 +02002990 pos = (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
Jiri Pirko22bedad2010-04-01 21:22:57 +00002991 val = get_unaligned_le32(ha->addr + 3);
Johannes Berg3ac64be2009-08-17 16:16:53 +02002992 pos ^= (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
2993 pos &= 0x3f;
2994 mfilt[pos / 32] |= (1 << (pos % 32));
2995 /* XXX: we might be able to just do this instead,
2996 * but not sure, needs testing, if we do use this we'd
2997 * neet to inform below to not reset the mcast */
2998 /* ath5k_hw_set_mcast_filterindex(ah,
Jiri Pirko22bedad2010-04-01 21:22:57 +00002999 * ha->addr[5]); */
Johannes Berg3ac64be2009-08-17 16:16:53 +02003000 }
3001
3002 return ((u64)(mfilt[1]) << 32) | mfilt[0];
3003}
3004
Ben Greearb1ae1ed2010-09-30 12:22:58 -07003005static bool ath_any_vif_assoc(struct ath5k_softc *sc)
3006{
3007 struct ath_vif_iter_data iter_data;
3008 iter_data.hw_macaddr = NULL;
3009 iter_data.any_assoc = false;
3010 iter_data.need_set_hw_addr = false;
3011 iter_data.found_active = true;
3012
3013 ieee80211_iterate_active_interfaces_atomic(sc->hw, ath_vif_iter,
3014 &iter_data);
3015 return iter_data.any_assoc;
3016}
3017
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003018#define SUPPORTED_FIF_FLAGS \
3019 FIF_PROMISC_IN_BSS | FIF_ALLMULTI | FIF_FCSFAIL | \
3020 FIF_PLCPFAIL | FIF_CONTROL | FIF_OTHER_BSS | \
3021 FIF_BCN_PRBRESP_PROMISC
3022/*
3023 * o always accept unicast, broadcast, and multicast traffic
3024 * o multicast traffic for all BSSIDs will be enabled if mac80211
3025 * says it should be
3026 * o maintain current state of phy ofdm or phy cck error reception.
3027 * If the hardware detects any of these type of errors then
3028 * ath5k_hw_get_rx_filter() will pass to us the respective
3029 * hardware filters to be able to receive these type of frames.
3030 * o probe request frames are accepted only when operating in
3031 * hostap, adhoc, or monitor modes
3032 * o enable promiscuous mode according to the interface state
3033 * o accept beacons:
3034 * - when operating in adhoc mode so the 802.11 layer creates
3035 * node table entries for peers,
3036 * - when operating in station mode for collecting rssi data when
3037 * the station is otherwise quiet, or
3038 * - when scanning
3039 */
3040static void ath5k_configure_filter(struct ieee80211_hw *hw,
3041 unsigned int changed_flags,
3042 unsigned int *new_flags,
Johannes Berg3ac64be2009-08-17 16:16:53 +02003043 u64 multicast)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003044{
3045 struct ath5k_softc *sc = hw->priv;
3046 struct ath5k_hw *ah = sc->ah;
Johannes Berg3ac64be2009-08-17 16:16:53 +02003047 u32 mfilt[2], rfilt;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003048
Bob Copeland56d1de02009-08-24 23:00:30 -04003049 mutex_lock(&sc->lock);
3050
Johannes Berg3ac64be2009-08-17 16:16:53 +02003051 mfilt[0] = multicast;
3052 mfilt[1] = multicast >> 32;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003053
3054 /* Only deal with supported flags */
3055 changed_flags &= SUPPORTED_FIF_FLAGS;
3056 *new_flags &= SUPPORTED_FIF_FLAGS;
3057
3058 /* If HW detects any phy or radar errors, leave those filters on.
3059 * Also, always enable Unicast, Broadcasts and Multicast
3060 * XXX: move unicast, bssid broadcasts and multicast to mac80211 */
3061 rfilt = (ath5k_hw_get_rx_filter(ah) & (AR5K_RX_FILTER_PHYERR)) |
3062 (AR5K_RX_FILTER_UCAST | AR5K_RX_FILTER_BCAST |
3063 AR5K_RX_FILTER_MCAST);
3064
3065 if (changed_flags & (FIF_PROMISC_IN_BSS | FIF_OTHER_BSS)) {
3066 if (*new_flags & FIF_PROMISC_IN_BSS) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003067 __set_bit(ATH_STAT_PROMISC, sc->status);
John Daiker0bbac082008-10-17 12:16:00 -07003068 } else {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003069 __clear_bit(ATH_STAT_PROMISC, sc->status);
John Daiker0bbac082008-10-17 12:16:00 -07003070 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003071 }
3072
Bob Copeland6b5dccc2010-06-04 08:14:14 -04003073 if (test_bit(ATH_STAT_PROMISC, sc->status))
3074 rfilt |= AR5K_RX_FILTER_PROM;
3075
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003076 /* Note, AR5K_RX_FILTER_MCAST is already enabled */
3077 if (*new_flags & FIF_ALLMULTI) {
3078 mfilt[0] = ~0;
3079 mfilt[1] = ~0;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003080 }
3081
3082 /* This is the best we can do */
3083 if (*new_flags & (FIF_FCSFAIL | FIF_PLCPFAIL))
3084 rfilt |= AR5K_RX_FILTER_PHYERR;
3085
3086 /* FIF_BCN_PRBRESP_PROMISC really means to enable beacons
Bob Copeland30bf4162010-08-15 13:03:15 -04003087 * and probes for any BSSID */
Ben Greearb1ae1ed2010-09-30 12:22:58 -07003088 if ((*new_flags & FIF_BCN_PRBRESP_PROMISC) || (sc->nvifs > 1))
Bob Copeland30bf4162010-08-15 13:03:15 -04003089 rfilt |= AR5K_RX_FILTER_BEACON;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003090
3091 /* FIF_CONTROL doc says that if FIF_PROMISC_IN_BSS is not
3092 * set we should only pass on control frames for this
3093 * station. This needs testing. I believe right now this
3094 * enables *all* control frames, which is OK.. but
3095 * but we should see if we can improve on granularity */
3096 if (*new_flags & FIF_CONTROL)
3097 rfilt |= AR5K_RX_FILTER_CONTROL;
3098
3099 /* Additional settings per mode -- this is per ath5k */
3100
3101 /* XXX move these to mac80211, and add a beacon IFF flag to mac80211 */
3102
Bob Copeland56d1de02009-08-24 23:00:30 -04003103 switch (sc->opmode) {
3104 case NL80211_IFTYPE_MESH_POINT:
Bob Copeland56d1de02009-08-24 23:00:30 -04003105 rfilt |= AR5K_RX_FILTER_CONTROL |
3106 AR5K_RX_FILTER_BEACON |
3107 AR5K_RX_FILTER_PROBEREQ |
3108 AR5K_RX_FILTER_PROM;
3109 break;
3110 case NL80211_IFTYPE_AP:
3111 case NL80211_IFTYPE_ADHOC:
3112 rfilt |= AR5K_RX_FILTER_PROBEREQ |
3113 AR5K_RX_FILTER_BEACON;
3114 break;
3115 case NL80211_IFTYPE_STATION:
3116 if (sc->assoc)
3117 rfilt |= AR5K_RX_FILTER_BEACON;
3118 default:
3119 break;
3120 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003121
3122 /* Set filters */
John Daiker0bbac082008-10-17 12:16:00 -07003123 ath5k_hw_set_rx_filter(ah, rfilt);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003124
3125 /* Set multicast bits */
3126 ath5k_hw_set_mcast_filter(ah, mfilt[0], mfilt[1]);
Bob Copelanda180a132010-08-15 13:03:12 -04003127 /* Set the cached hw filter flags, this will later actually
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003128 * be set in HW */
3129 sc->filter_flags = rfilt;
Bob Copeland56d1de02009-08-24 23:00:30 -04003130
3131 mutex_unlock(&sc->lock);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003132}
3133
3134static int
3135ath5k_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Johannes Bergdc822b52008-12-29 12:55:09 +01003136 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
3137 struct ieee80211_key_conf *key)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003138{
3139 struct ath5k_softc *sc = hw->priv;
Luis R. Rodriguezdc1e0012009-11-04 17:47:31 -08003140 struct ath5k_hw *ah = sc->ah;
3141 struct ath_common *common = ath5k_hw_common(ah);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003142 int ret = 0;
3143
Bob Copeland9ad9a262008-10-29 08:30:54 -04003144 if (modparam_nohwcrypt)
3145 return -EOPNOTSUPP;
3146
Johannes Berg97359d12010-08-10 09:46:38 +02003147 switch (key->cipher) {
3148 case WLAN_CIPHER_SUITE_WEP40:
3149 case WLAN_CIPHER_SUITE_WEP104:
3150 case WLAN_CIPHER_SUITE_TKIP:
Bob Copeland3f64b432008-10-29 23:19:14 -04003151 break;
Johannes Berg97359d12010-08-10 09:46:38 +02003152 case WLAN_CIPHER_SUITE_CCMP:
Bruno Randolf781f3132010-09-08 16:04:59 +09003153 if (common->crypt_caps & ATH_CRYPT_CAP_CIPHER_AESCCM)
Bob Copeland1c818742009-08-24 23:00:33 -04003154 break;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003155 return -EOPNOTSUPP;
3156 default:
3157 WARN_ON(1);
3158 return -EINVAL;
3159 }
3160
3161 mutex_lock(&sc->lock);
3162
3163 switch (cmd) {
3164 case SET_KEY:
Bruno Randolfe0f8c2a2010-09-08 16:04:43 +09003165 ret = ath_key_config(common, vif, sta, key);
3166 if (ret >= 0) {
3167 key->hw_key_idx = ret;
3168 /* push IV and Michael MIC generation to stack */
3169 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
3170 if (key->cipher == WLAN_CIPHER_SUITE_TKIP)
3171 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
3172 if (key->cipher == WLAN_CIPHER_SUITE_CCMP)
3173 key->flags |= IEEE80211_KEY_FLAG_SW_MGMT;
3174 ret = 0;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003175 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003176 break;
3177 case DISABLE_KEY:
Bruno Randolfe0f8c2a2010-09-08 16:04:43 +09003178 ath_key_delete(common, key);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003179 break;
3180 default:
3181 ret = -EINVAL;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003182 }
3183
Jiri Slaby274c7c32008-07-15 17:44:20 +02003184 mmiowb();
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003185 mutex_unlock(&sc->lock);
3186 return ret;
3187}
3188
3189static int
3190ath5k_get_stats(struct ieee80211_hw *hw,
3191 struct ieee80211_low_level_stats *stats)
3192{
3193 struct ath5k_softc *sc = hw->priv;
Nick Kossifidis194828a2008-04-16 18:49:02 +03003194
3195 /* Force update */
Bruno Randolf495391d2010-03-25 14:49:36 +09003196 ath5k_hw_update_mib_counters(sc->ah);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003197
Bruno Randolf495391d2010-03-25 14:49:36 +09003198 stats->dot11ACKFailureCount = sc->stats.ack_fail;
3199 stats->dot11RTSFailureCount = sc->stats.rts_fail;
3200 stats->dot11RTSSuccessCount = sc->stats.rts_ok;
3201 stats->dot11FCSErrorCount = sc->stats.fcs_error;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003202
3203 return 0;
3204}
3205
Holger Schurig55ee82b2010-04-19 10:24:22 +02003206static int ath5k_get_survey(struct ieee80211_hw *hw, int idx,
3207 struct survey_info *survey)
3208{
3209 struct ath5k_softc *sc = hw->priv;
3210 struct ieee80211_conf *conf = &hw->conf;
3211
3212 if (idx != 0)
3213 return -ENOENT;
3214
3215 survey->channel = conf->channel;
3216 survey->filled = SURVEY_INFO_NOISE_DBM;
3217 survey->noise = sc->ah->ah_noise_floor;
3218
3219 return 0;
3220}
3221
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003222static u64
3223ath5k_get_tsf(struct ieee80211_hw *hw)
3224{
3225 struct ath5k_softc *sc = hw->priv;
3226
3227 return ath5k_hw_get_tsf64(sc->ah);
3228}
3229
3230static void
Alina Friedrichsen3b5d6652009-01-24 07:09:59 +01003231ath5k_set_tsf(struct ieee80211_hw *hw, u64 tsf)
3232{
3233 struct ath5k_softc *sc = hw->priv;
3234
3235 ath5k_hw_set_tsf64(sc->ah, tsf);
3236}
3237
3238static void
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003239ath5k_reset_tsf(struct ieee80211_hw *hw)
3240{
3241 struct ath5k_softc *sc = hw->priv;
3242
Bruno Randolf9804b982008-01-19 18:17:59 +09003243 /*
3244 * in IBSS mode we need to update the beacon timers too.
3245 * this will also reset the TSF if we call it with 0
3246 */
Johannes Berg05c914f2008-09-11 00:01:58 +02003247 if (sc->opmode == NL80211_IFTYPE_ADHOC)
Bruno Randolf9804b982008-01-19 18:17:59 +09003248 ath5k_beacon_update_timers(sc, 0);
3249 else
3250 ath5k_hw_reset_tsf(sc->ah);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003251}
3252
Martin Xu02969b32008-11-24 10:49:27 +08003253static void
3254set_beacon_filter(struct ieee80211_hw *hw, bool enable)
3255{
3256 struct ath5k_softc *sc = hw->priv;
3257 struct ath5k_hw *ah = sc->ah;
3258 u32 rfilt;
3259 rfilt = ath5k_hw_get_rx_filter(ah);
3260 if (enable)
3261 rfilt |= AR5K_RX_FILTER_BEACON;
3262 else
3263 rfilt &= ~AR5K_RX_FILTER_BEACON;
3264 ath5k_hw_set_rx_filter(ah, rfilt);
3265 sc->filter_flags = rfilt;
3266}
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003267
Martin Xu02969b32008-11-24 10:49:27 +08003268static void ath5k_bss_info_changed(struct ieee80211_hw *hw,
3269 struct ieee80211_vif *vif,
3270 struct ieee80211_bss_conf *bss_conf,
3271 u32 changes)
3272{
Ben Greearb1ae1ed2010-09-30 12:22:58 -07003273 struct ath5k_vif *avf = (void *)vif->drv_priv;
Martin Xu02969b32008-11-24 10:49:27 +08003274 struct ath5k_softc *sc = hw->priv;
Johannes Berg2d0ddec2009-04-23 16:13:26 +02003275 struct ath5k_hw *ah = sc->ah;
Luis R. Rodriguez954fece2009-09-10 10:51:33 -07003276 struct ath_common *common = ath5k_hw_common(ah);
Bob Copeland21800492009-07-04 12:59:52 -04003277 unsigned long flags;
Johannes Berg2d0ddec2009-04-23 16:13:26 +02003278
3279 mutex_lock(&sc->lock);
Johannes Berg2d0ddec2009-04-23 16:13:26 +02003280
3281 if (changes & BSS_CHANGED_BSSID) {
3282 /* Cache for later use during resets */
Luis R. Rodriguez954fece2009-09-10 10:51:33 -07003283 memcpy(common->curbssid, bss_conf->bssid, ETH_ALEN);
Luis R. Rodriguez8ce54c52009-10-06 20:44:34 -04003284 common->curaid = 0;
Nick Kossifidis418de6d2010-08-15 13:03:10 -04003285 ath5k_hw_set_bssid(ah);
Johannes Berg2d0ddec2009-04-23 16:13:26 +02003286 mmiowb();
3287 }
Johannes Berg57c4d7b2009-04-23 16:10:04 +02003288
3289 if (changes & BSS_CHANGED_BEACON_INT)
3290 sc->bintval = bss_conf->beacon_int;
3291
Martin Xu02969b32008-11-24 10:49:27 +08003292 if (changes & BSS_CHANGED_ASSOC) {
Ben Greearb1ae1ed2010-09-30 12:22:58 -07003293 avf->assoc = bss_conf->assoc;
3294 if (bss_conf->assoc)
3295 sc->assoc = bss_conf->assoc;
3296 else
3297 sc->assoc = ath_any_vif_assoc(sc);
3298
Martin Xu02969b32008-11-24 10:49:27 +08003299 if (sc->opmode == NL80211_IFTYPE_STATION)
3300 set_beacon_filter(hw, sc->assoc);
Bob Copelandf0f3d382009-06-10 22:22:21 -04003301 ath5k_hw_set_ledstate(sc->ah, sc->assoc ?
3302 AR5K_LED_ASSOC : AR5K_LED_INIT);
Luis R. Rodriguez8ce54c52009-10-06 20:44:34 -04003303 if (bss_conf->assoc) {
3304 ATH5K_DBG(sc, ATH5K_DEBUG_ANY,
3305 "Bss Info ASSOC %d, bssid: %pM\n",
3306 bss_conf->aid, common->curbssid);
3307 common->curaid = bss_conf->aid;
Nick Kossifidis418de6d2010-08-15 13:03:10 -04003308 ath5k_hw_set_bssid(ah);
Luis R. Rodriguez8ce54c52009-10-06 20:44:34 -04003309 /* Once ANI is available you would start it here */
3310 }
Martin Xu02969b32008-11-24 10:49:27 +08003311 }
Johannes Berg2d0ddec2009-04-23 16:13:26 +02003312
Bob Copeland21800492009-07-04 12:59:52 -04003313 if (changes & BSS_CHANGED_BEACON) {
3314 spin_lock_irqsave(&sc->block, flags);
3315 ath5k_beacon_update(hw, vif);
3316 spin_unlock_irqrestore(&sc->block, flags);
Johannes Berg2d0ddec2009-04-23 16:13:26 +02003317 }
3318
Bob Copeland21800492009-07-04 12:59:52 -04003319 if (changes & BSS_CHANGED_BEACON_ENABLED)
3320 sc->enable_beacon = bss_conf->enable_beacon;
3321
3322 if (changes & (BSS_CHANGED_BEACON | BSS_CHANGED_BEACON_ENABLED |
3323 BSS_CHANGED_BEACON_INT))
3324 ath5k_beacon_config(sc);
3325
Johannes Berg2d0ddec2009-04-23 16:13:26 +02003326 mutex_unlock(&sc->lock);
Martin Xu02969b32008-11-24 10:49:27 +08003327}
Bob Copelandf0f3d382009-06-10 22:22:21 -04003328
3329static void ath5k_sw_scan_start(struct ieee80211_hw *hw)
3330{
3331 struct ath5k_softc *sc = hw->priv;
3332 if (!sc->assoc)
3333 ath5k_hw_set_ledstate(sc->ah, AR5K_LED_SCAN);
3334}
3335
3336static void ath5k_sw_scan_complete(struct ieee80211_hw *hw)
3337{
3338 struct ath5k_softc *sc = hw->priv;
3339 ath5k_hw_set_ledstate(sc->ah, sc->assoc ?
3340 AR5K_LED_ASSOC : AR5K_LED_INIT);
3341}
Lukáš Turek6e08d222009-12-21 22:50:51 +01003342
3343/**
3344 * ath5k_set_coverage_class - Set IEEE 802.11 coverage class
3345 *
3346 * @hw: struct ieee80211_hw pointer
3347 * @coverage_class: IEEE 802.11 coverage class number
3348 *
3349 * Mac80211 callback. Sets slot time, ACK timeout and CTS timeout for given
3350 * coverage class. The values are persistent, they are restored after device
3351 * reset.
3352 */
3353static void ath5k_set_coverage_class(struct ieee80211_hw *hw, u8 coverage_class)
3354{
3355 struct ath5k_softc *sc = hw->priv;
3356
3357 mutex_lock(&sc->lock);
3358 ath5k_hw_set_coverage_class(sc->ah, coverage_class);
3359 mutex_unlock(&sc->lock);
3360}
Bob Copeland8a63fac2010-09-17 12:45:07 +09003361
Bruno Randolfe0b1cc52010-09-17 11:37:18 +09003362static int ath5k_conf_tx(struct ieee80211_hw *hw, u16 queue,
3363 const struct ieee80211_tx_queue_params *params)
3364{
3365 struct ath5k_softc *sc = hw->priv;
3366 struct ath5k_hw *ah = sc->ah;
3367 struct ath5k_txq_info qi;
3368 int ret = 0;
3369
3370 if (queue >= ah->ah_capabilities.cap_queues.q_tx_num)
3371 return 0;
3372
3373 mutex_lock(&sc->lock);
3374
3375 ath5k_hw_get_tx_queueprops(ah, queue, &qi);
3376
3377 qi.tqi_aifs = params->aifs;
3378 qi.tqi_cw_min = params->cw_min;
3379 qi.tqi_cw_max = params->cw_max;
3380 qi.tqi_burst_time = params->txop;
3381
3382 ATH5K_DBG(sc, ATH5K_DEBUG_ANY,
3383 "Configure tx [queue %d], "
3384 "aifs: %d, cw_min: %d, cw_max: %d, txop: %d\n",
3385 queue, params->aifs, params->cw_min,
3386 params->cw_max, params->txop);
3387
3388 if (ath5k_hw_set_tx_queueprops(ah, queue, &qi)) {
3389 ATH5K_ERR(sc,
3390 "Unable to update hardware queue %u!\n", queue);
3391 ret = -EIO;
3392 } else
3393 ath5k_hw_reset_tx_queue(ah, queue);
3394
3395 mutex_unlock(&sc->lock);
3396
3397 return ret;
3398}
3399
Bob Copeland8a63fac2010-09-17 12:45:07 +09003400static const struct ieee80211_ops ath5k_hw_ops = {
3401 .tx = ath5k_tx,
3402 .start = ath5k_start,
3403 .stop = ath5k_stop,
3404 .add_interface = ath5k_add_interface,
3405 .remove_interface = ath5k_remove_interface,
3406 .config = ath5k_config,
3407 .prepare_multicast = ath5k_prepare_multicast,
3408 .configure_filter = ath5k_configure_filter,
3409 .set_key = ath5k_set_key,
3410 .get_stats = ath5k_get_stats,
3411 .get_survey = ath5k_get_survey,
Bruno Randolfe0b1cc52010-09-17 11:37:18 +09003412 .conf_tx = ath5k_conf_tx,
Bob Copeland8a63fac2010-09-17 12:45:07 +09003413 .get_tsf = ath5k_get_tsf,
3414 .set_tsf = ath5k_set_tsf,
3415 .reset_tsf = ath5k_reset_tsf,
3416 .bss_info_changed = ath5k_bss_info_changed,
3417 .sw_scan_start = ath5k_sw_scan_start,
3418 .sw_scan_complete = ath5k_sw_scan_complete,
3419 .set_coverage_class = ath5k_set_coverage_class,
3420};
3421
3422/********************\
3423* PCI Initialization *
3424\********************/
3425
3426static int __devinit
3427ath5k_pci_probe(struct pci_dev *pdev,
3428 const struct pci_device_id *id)
3429{
3430 void __iomem *mem;
3431 struct ath5k_softc *sc;
3432 struct ath_common *common;
3433 struct ieee80211_hw *hw;
3434 int ret;
3435 u8 csz;
3436
3437 /*
3438 * L0s needs to be disabled on all ath5k cards.
3439 *
3440 * For distributions shipping with CONFIG_PCIEASPM (this will be enabled
3441 * by default in the future in 2.6.36) this will also mean both L1 and
3442 * L0s will be disabled when a pre 1.1 PCIe device is detected. We do
3443 * know L1 works correctly even for all ath5k pre 1.1 PCIe devices
3444 * though but cannot currently undue the effect of a blacklist, for
3445 * details you can read pcie_aspm_sanity_check() and see how it adjusts
3446 * the device link capability.
3447 *
3448 * It may be possible in the future to implement some PCI API to allow
3449 * drivers to override blacklists for pre 1.1 PCIe but for now it is
3450 * best to accept that both L0s and L1 will be disabled completely for
3451 * distributions shipping with CONFIG_PCIEASPM rather than having this
3452 * issue present. Motivation for adding this new API will be to help
3453 * with power consumption for some of these devices.
3454 */
3455 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S);
3456
3457 ret = pci_enable_device(pdev);
3458 if (ret) {
3459 dev_err(&pdev->dev, "can't enable device\n");
3460 goto err;
3461 }
3462
3463 /* XXX 32-bit addressing only */
3464 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
3465 if (ret) {
3466 dev_err(&pdev->dev, "32-bit DMA not available\n");
3467 goto err_dis;
3468 }
3469
3470 /*
3471 * Cache line size is used to size and align various
3472 * structures used to communicate with the hardware.
3473 */
3474 pci_read_config_byte(pdev, PCI_CACHE_LINE_SIZE, &csz);
3475 if (csz == 0) {
3476 /*
3477 * Linux 2.4.18 (at least) writes the cache line size
3478 * register as a 16-bit wide register which is wrong.
3479 * We must have this setup properly for rx buffer
3480 * DMA to work so force a reasonable value here if it
3481 * comes up zero.
3482 */
3483 csz = L1_CACHE_BYTES >> 2;
3484 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, csz);
3485 }
3486 /*
3487 * The default setting of latency timer yields poor results,
3488 * set it to the value used by other systems. It may be worth
3489 * tweaking this setting more.
3490 */
3491 pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0xa8);
3492
3493 /* Enable bus mastering */
3494 pci_set_master(pdev);
3495
3496 /*
3497 * Disable the RETRY_TIMEOUT register (0x41) to keep
3498 * PCI Tx retries from interfering with C3 CPU state.
3499 */
3500 pci_write_config_byte(pdev, 0x41, 0);
3501
3502 ret = pci_request_region(pdev, 0, "ath5k");
3503 if (ret) {
3504 dev_err(&pdev->dev, "cannot reserve PCI memory region\n");
3505 goto err_dis;
3506 }
3507
3508 mem = pci_iomap(pdev, 0, 0);
3509 if (!mem) {
3510 dev_err(&pdev->dev, "cannot remap PCI memory region\n") ;
3511 ret = -EIO;
3512 goto err_reg;
3513 }
3514
3515 /*
3516 * Allocate hw (mac80211 main struct)
3517 * and hw->priv (driver private data)
3518 */
3519 hw = ieee80211_alloc_hw(sizeof(*sc), &ath5k_hw_ops);
3520 if (hw == NULL) {
3521 dev_err(&pdev->dev, "cannot allocate ieee80211_hw\n");
3522 ret = -ENOMEM;
3523 goto err_map;
3524 }
3525
3526 dev_info(&pdev->dev, "registered as '%s'\n", wiphy_name(hw->wiphy));
3527
3528 /* Initialize driver private data */
3529 SET_IEEE80211_DEV(hw, &pdev->dev);
3530 hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
3531 IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
3532 IEEE80211_HW_SIGNAL_DBM;
3533
3534 hw->wiphy->interface_modes =
3535 BIT(NL80211_IFTYPE_AP) |
3536 BIT(NL80211_IFTYPE_STATION) |
3537 BIT(NL80211_IFTYPE_ADHOC) |
3538 BIT(NL80211_IFTYPE_MESH_POINT);
3539
3540 hw->extra_tx_headroom = 2;
3541 hw->channel_change_time = 5000;
3542 sc = hw->priv;
3543 sc->hw = hw;
3544 sc->pdev = pdev;
3545
3546 ath5k_debug_init_device(sc);
3547
3548 /*
3549 * Mark the device as detached to avoid processing
3550 * interrupts until setup is complete.
3551 */
3552 __set_bit(ATH_STAT_INVALID, sc->status);
3553
3554 sc->iobase = mem; /* So we can unmap it on detach */
3555 sc->opmode = NL80211_IFTYPE_STATION;
3556 sc->bintval = 1000;
3557 mutex_init(&sc->lock);
3558 spin_lock_init(&sc->rxbuflock);
3559 spin_lock_init(&sc->txbuflock);
3560 spin_lock_init(&sc->block);
3561
3562 /* Set private data */
3563 pci_set_drvdata(pdev, sc);
3564
3565 /* Setup interrupt handler */
3566 ret = request_irq(pdev->irq, ath5k_intr, IRQF_SHARED, "ath", sc);
3567 if (ret) {
3568 ATH5K_ERR(sc, "request_irq failed\n");
3569 goto err_free;
3570 }
3571
3572 /* If we passed the test, malloc an ath5k_hw struct */
3573 sc->ah = kzalloc(sizeof(struct ath5k_hw), GFP_KERNEL);
3574 if (!sc->ah) {
3575 ret = -ENOMEM;
3576 ATH5K_ERR(sc, "out of memory\n");
3577 goto err_irq;
3578 }
3579
3580 sc->ah->ah_sc = sc;
3581 sc->ah->ah_iobase = sc->iobase;
3582 common = ath5k_hw_common(sc->ah);
3583 common->ops = &ath5k_common_ops;
3584 common->ah = sc->ah;
3585 common->hw = hw;
3586 common->cachelsz = csz << 2; /* convert to bytes */
3587
3588 /* Initialize device */
3589 ret = ath5k_hw_attach(sc);
3590 if (ret) {
3591 goto err_free_ah;
3592 }
3593
3594 /* set up multi-rate retry capabilities */
3595 if (sc->ah->ah_version == AR5K_AR5212) {
3596 hw->max_rates = 4;
3597 hw->max_rate_tries = 11;
3598 }
3599
Ben Greearb1ae1ed2010-09-30 12:22:58 -07003600 hw->vif_data_size = sizeof(struct ath5k_vif);
3601
Bob Copeland8a63fac2010-09-17 12:45:07 +09003602 /* Finish private driver data initialization */
3603 ret = ath5k_attach(pdev, hw);
3604 if (ret)
3605 goto err_ah;
3606
3607 ATH5K_INFO(sc, "Atheros AR%s chip found (MAC: 0x%x, PHY: 0x%x)\n",
3608 ath5k_chip_name(AR5K_VERSION_MAC, sc->ah->ah_mac_srev),
3609 sc->ah->ah_mac_srev,
3610 sc->ah->ah_phy_revision);
3611
3612 if (!sc->ah->ah_single_chip) {
3613 /* Single chip radio (!RF5111) */
3614 if (sc->ah->ah_radio_5ghz_revision &&
3615 !sc->ah->ah_radio_2ghz_revision) {
3616 /* No 5GHz support -> report 2GHz radio */
3617 if (!test_bit(AR5K_MODE_11A,
3618 sc->ah->ah_capabilities.cap_mode)) {
3619 ATH5K_INFO(sc, "RF%s 2GHz radio found (0x%x)\n",
3620 ath5k_chip_name(AR5K_VERSION_RAD,
3621 sc->ah->ah_radio_5ghz_revision),
3622 sc->ah->ah_radio_5ghz_revision);
3623 /* No 2GHz support (5110 and some
3624 * 5Ghz only cards) -> report 5Ghz radio */
3625 } else if (!test_bit(AR5K_MODE_11B,
3626 sc->ah->ah_capabilities.cap_mode)) {
3627 ATH5K_INFO(sc, "RF%s 5GHz radio found (0x%x)\n",
3628 ath5k_chip_name(AR5K_VERSION_RAD,
3629 sc->ah->ah_radio_5ghz_revision),
3630 sc->ah->ah_radio_5ghz_revision);
3631 /* Multiband radio */
3632 } else {
3633 ATH5K_INFO(sc, "RF%s multiband radio found"
3634 " (0x%x)\n",
3635 ath5k_chip_name(AR5K_VERSION_RAD,
3636 sc->ah->ah_radio_5ghz_revision),
3637 sc->ah->ah_radio_5ghz_revision);
3638 }
3639 }
3640 /* Multi chip radio (RF5111 - RF2111) ->
3641 * report both 2GHz/5GHz radios */
3642 else if (sc->ah->ah_radio_5ghz_revision &&
3643 sc->ah->ah_radio_2ghz_revision){
3644 ATH5K_INFO(sc, "RF%s 5GHz radio found (0x%x)\n",
3645 ath5k_chip_name(AR5K_VERSION_RAD,
3646 sc->ah->ah_radio_5ghz_revision),
3647 sc->ah->ah_radio_5ghz_revision);
3648 ATH5K_INFO(sc, "RF%s 2GHz radio found (0x%x)\n",
3649 ath5k_chip_name(AR5K_VERSION_RAD,
3650 sc->ah->ah_radio_2ghz_revision),
3651 sc->ah->ah_radio_2ghz_revision);
3652 }
3653 }
3654
3655
3656 /* ready to process interrupts */
3657 __clear_bit(ATH_STAT_INVALID, sc->status);
3658
3659 return 0;
3660err_ah:
3661 ath5k_hw_detach(sc->ah);
3662err_free_ah:
3663 kfree(sc->ah);
3664err_irq:
3665 free_irq(pdev->irq, sc);
3666err_free:
3667 ieee80211_free_hw(hw);
3668err_map:
3669 pci_iounmap(pdev, mem);
3670err_reg:
3671 pci_release_region(pdev, 0);
3672err_dis:
3673 pci_disable_device(pdev);
3674err:
3675 return ret;
3676}
3677
3678static void __devexit
3679ath5k_pci_remove(struct pci_dev *pdev)
3680{
3681 struct ath5k_softc *sc = pci_get_drvdata(pdev);
3682
3683 ath5k_debug_finish_device(sc);
3684 ath5k_detach(pdev, sc->hw);
3685 ath5k_hw_detach(sc->ah);
3686 kfree(sc->ah);
3687 free_irq(pdev->irq, sc);
3688 pci_iounmap(pdev, sc->iobase);
3689 pci_release_region(pdev, 0);
3690 pci_disable_device(pdev);
3691 ieee80211_free_hw(sc->hw);
3692}
3693
3694#ifdef CONFIG_PM_SLEEP
3695static int ath5k_pci_suspend(struct device *dev)
3696{
3697 struct ath5k_softc *sc = pci_get_drvdata(to_pci_dev(dev));
3698
3699 ath5k_led_off(sc);
3700 return 0;
3701}
3702
3703static int ath5k_pci_resume(struct device *dev)
3704{
3705 struct pci_dev *pdev = to_pci_dev(dev);
3706 struct ath5k_softc *sc = pci_get_drvdata(pdev);
3707
3708 /*
3709 * Suspend/Resume resets the PCI configuration space, so we have to
3710 * re-disable the RETRY_TIMEOUT register (0x41) to keep
3711 * PCI Tx retries from interfering with C3 CPU state
3712 */
3713 pci_write_config_byte(pdev, 0x41, 0);
3714
3715 ath5k_led_enable(sc);
3716 return 0;
3717}
3718
3719static SIMPLE_DEV_PM_OPS(ath5k_pm_ops, ath5k_pci_suspend, ath5k_pci_resume);
3720#define ATH5K_PM_OPS (&ath5k_pm_ops)
3721#else
3722#define ATH5K_PM_OPS NULL
3723#endif /* CONFIG_PM_SLEEP */
3724
3725static struct pci_driver ath5k_pci_driver = {
3726 .name = KBUILD_MODNAME,
3727 .id_table = ath5k_pci_id_table,
3728 .probe = ath5k_pci_probe,
3729 .remove = __devexit_p(ath5k_pci_remove),
3730 .driver.pm = ATH5K_PM_OPS,
3731};
3732
3733/*
3734 * Module init/exit functions
3735 */
3736static int __init
3737init_ath5k_pci(void)
3738{
3739 int ret;
3740
3741 ath5k_debug_init();
3742
3743 ret = pci_register_driver(&ath5k_pci_driver);
3744 if (ret) {
3745 printk(KERN_ERR "ath5k_pci: can't register pci driver\n");
3746 return ret;
3747 }
3748
3749 return 0;
3750}
3751
3752static void __exit
3753exit_ath5k_pci(void)
3754{
3755 pci_unregister_driver(&ath5k_pci_driver);
3756
3757 ath5k_debug_finish();
3758}
3759
3760module_init(init_ath5k_pci);
3761module_exit(exit_ath5k_pci);