blob: eea5879575ba803f8a1eaf3f6fe9b72e6e1cb7bb [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
Ben Greear7afbb2f2010-11-10 11:43:51 -0800552 * interfaces is allowed.
Ben Greear62c58fb2010-10-08 12:01:15 -0700553 */
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
Luis R. Rodriguez14fb7c12010-10-20 06:59:38 -0700561static void ath5k_update_bssid_mask_and_opmode(struct ath5k_softc *sc,
562 struct ieee80211_vif *vif)
Ben Greearb1ae1ed2010-09-30 12:22:58 -0700563{
564 struct ath_common *common = ath5k_hw_common(sc->ah);
565 struct ath_vif_iter_data iter_data;
566
567 /*
568 * Use the hardware MAC address as reference, the hardware uses it
569 * together with the BSSID mask when matching addresses.
570 */
571 iter_data.hw_macaddr = common->macaddr;
572 memset(&iter_data.mask, 0xff, ETH_ALEN);
573 iter_data.found_active = false;
574 iter_data.need_set_hw_addr = true;
Ben Greear62c58fb2010-10-08 12:01:15 -0700575 iter_data.opmode = NL80211_IFTYPE_UNSPECIFIED;
Ben Greearb1ae1ed2010-09-30 12:22:58 -0700576
577 if (vif)
578 ath_vif_iter(&iter_data, vif->addr, vif);
579
580 /* Get list of all active MAC addresses */
581 ieee80211_iterate_active_interfaces_atomic(sc->hw, ath_vif_iter,
582 &iter_data);
583 memcpy(sc->bssidmask, iter_data.mask, ETH_ALEN);
584
Ben Greear62c58fb2010-10-08 12:01:15 -0700585 sc->opmode = iter_data.opmode;
586 if (sc->opmode == NL80211_IFTYPE_UNSPECIFIED)
587 /* Nothing active, default to station mode */
588 sc->opmode = NL80211_IFTYPE_STATION;
589
Ben Greear7afbb2f2010-11-10 11:43:51 -0800590 ath5k_hw_set_opmode(sc->ah, sc->opmode);
591 ATH5K_DBG(sc, ATH5K_DEBUG_MODE, "mode setup opmode %d (%s)\n",
592 sc->opmode, ath_opmode_to_string(sc->opmode));
Ben Greear62c58fb2010-10-08 12:01:15 -0700593
Ben Greearb1ae1ed2010-09-30 12:22:58 -0700594 if (iter_data.need_set_hw_addr && iter_data.found_active)
595 ath5k_hw_set_lladdr(sc->ah, iter_data.active_mac);
596
Ben Greear62c58fb2010-10-08 12:01:15 -0700597 if (ath5k_hw_hasbssidmask(sc->ah))
598 ath5k_hw_set_bssid_mask(sc->ah, sc->bssidmask);
Ben Greearb1ae1ed2010-09-30 12:22:58 -0700599}
600
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200601static void
Ben Greearb1ae1ed2010-09-30 12:22:58 -0700602ath5k_mode_setup(struct ath5k_softc *sc, struct ieee80211_vif *vif)
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200603{
604 struct ath5k_hw *ah = sc->ah;
605 u32 rfilt;
606
607 /* configure rx filter */
608 rfilt = sc->filter_flags;
609 ath5k_hw_set_rx_filter(ah, rfilt);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200610 ATH5K_DBG(sc, ATH5K_DEBUG_MODE, "RX filter 0x%x\n", rfilt);
Ben Greear62c58fb2010-10-08 12:01:15 -0700611
612 ath5k_update_bssid_mask_and_opmode(sc, vif);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200613}
614
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500615static inline int
Bruno Randolf63266a62008-07-30 17:12:58 +0200616ath5k_hw_to_driver_rix(struct ath5k_softc *sc, int hw_rix)
617{
Bob Copelandb7266042009-03-02 21:55:18 -0500618 int rix;
619
620 /* return base rate on errors */
621 if (WARN(hw_rix < 0 || hw_rix >= AR5K_MAX_RATES,
622 "hw_rix out of bounds: %x\n", hw_rix))
623 return 0;
624
625 rix = sc->rate_idx[sc->curband->band][hw_rix];
626 if (WARN(rix < 0, "invalid hw_rix: %x\n", hw_rix))
627 rix = 0;
628
629 return rix;
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500630}
631
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200632/***************\
633* Buffers setup *
634\***************/
635
Bob Copelandb6ea0352009-01-10 14:42:54 -0500636static
637struct sk_buff *ath5k_rx_skb_alloc(struct ath5k_softc *sc, dma_addr_t *skb_addr)
638{
Luis R. Rodriguezdb719712009-09-10 11:20:57 -0700639 struct ath_common *common = ath5k_hw_common(sc->ah);
Bob Copelandb6ea0352009-01-10 14:42:54 -0500640 struct sk_buff *skb;
Bob Copelandb6ea0352009-01-10 14:42:54 -0500641
642 /*
643 * Allocate buffer with headroom_needed space for the
644 * fake physical layer header at the start.
645 */
Luis R. Rodriguezdb719712009-09-10 11:20:57 -0700646 skb = ath_rxbuf_alloc(common,
Luis R. Rodriguezdd849782009-11-04 09:44:50 -0800647 common->rx_bufsize,
Luis R. Rodriguezaeb63cf2009-08-12 09:57:00 -0700648 GFP_ATOMIC);
Bob Copelandb6ea0352009-01-10 14:42:54 -0500649
650 if (!skb) {
651 ATH5K_ERR(sc, "can't alloc skbuff of size %u\n",
Luis R. Rodriguezdd849782009-11-04 09:44:50 -0800652 common->rx_bufsize);
Bob Copelandb6ea0352009-01-10 14:42:54 -0500653 return NULL;
654 }
Bob Copelandb6ea0352009-01-10 14:42:54 -0500655
656 *skb_addr = pci_map_single(sc->pdev,
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -0800657 skb->data, common->rx_bufsize,
658 PCI_DMA_FROMDEVICE);
Bob Copelandb6ea0352009-01-10 14:42:54 -0500659 if (unlikely(pci_dma_mapping_error(sc->pdev, *skb_addr))) {
660 ATH5K_ERR(sc, "%s: DMA mapping failed\n", __func__);
661 dev_kfree_skb(skb);
662 return NULL;
663 }
664 return skb;
665}
666
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200667static int
668ath5k_rxbuf_setup(struct ath5k_softc *sc, struct ath5k_buf *bf)
669{
670 struct ath5k_hw *ah = sc->ah;
671 struct sk_buff *skb = bf->skb;
672 struct ath5k_desc *ds;
Bruno Randolfb5eae9f2010-05-19 10:18:16 +0900673 int ret;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200674
Bob Copelandb6ea0352009-01-10 14:42:54 -0500675 if (!skb) {
676 skb = ath5k_rx_skb_alloc(sc, &bf->skbaddr);
677 if (!skb)
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200678 return -ENOMEM;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200679 bf->skb = skb;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200680 }
681
682 /*
683 * Setup descriptors. For receive we always terminate
684 * the descriptor list with a self-linked entry so we'll
685 * not get overrun under high load (as can happen with a
686 * 5212 when ANI processing enables PHY error frames).
687 *
Bruno Randolfbeade632010-06-16 19:11:25 +0900688 * To ensure the last descriptor is self-linked we create
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200689 * each descriptor as self-linked and add it to the end. As
690 * each additional descriptor is added the previous self-linked
Bruno Randolfbeade632010-06-16 19:11:25 +0900691 * entry is "fixed" naturally. This should be safe even
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200692 * if DMA is happening. When processing RX interrupts we
693 * never remove/process the last, self-linked, entry on the
Bruno Randolfbeade632010-06-16 19:11:25 +0900694 * descriptor list. This ensures the hardware always has
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200695 * someplace to write a new frame.
696 */
697 ds = bf->desc;
698 ds->ds_link = bf->daddr; /* link to self */
699 ds->ds_data = bf->skbaddr;
Bruno Randolfa6668192010-06-16 19:12:01 +0900700 ret = ath5k_hw_setup_rx_desc(ah, ds, ah->common.rx_bufsize, 0);
Bruno Randolf0452d4a2010-06-16 19:11:35 +0900701 if (ret) {
702 ATH5K_ERR(sc, "%s: could not setup RX desc\n", __func__);
Bruno Randolfb5eae9f2010-05-19 10:18:16 +0900703 return ret;
Bruno Randolf0452d4a2010-06-16 19:11:35 +0900704 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200705
706 if (sc->rxlink != NULL)
707 *sc->rxlink = bf->daddr;
708 sc->rxlink = &ds->ds_link;
709 return 0;
710}
711
Bob Copeland2ac29272010-02-09 13:06:54 -0500712static enum ath5k_pkt_type get_hw_packet_type(struct sk_buff *skb)
713{
714 struct ieee80211_hdr *hdr;
715 enum ath5k_pkt_type htype;
716 __le16 fc;
717
718 hdr = (struct ieee80211_hdr *)skb->data;
719 fc = hdr->frame_control;
720
721 if (ieee80211_is_beacon(fc))
722 htype = AR5K_PKT_TYPE_BEACON;
723 else if (ieee80211_is_probe_resp(fc))
724 htype = AR5K_PKT_TYPE_PROBE_RESP;
725 else if (ieee80211_is_atim(fc))
726 htype = AR5K_PKT_TYPE_ATIM;
727 else if (ieee80211_is_pspoll(fc))
728 htype = AR5K_PKT_TYPE_PSPOLL;
729 else
730 htype = AR5K_PKT_TYPE_NORMAL;
731
732 return htype;
733}
734
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200735static int
Bob Copelandcec8db22009-07-04 12:59:51 -0400736ath5k_txbuf_setup(struct ath5k_softc *sc, struct ath5k_buf *bf,
Benoit Papillault8127fbd2010-02-27 23:05:26 +0100737 struct ath5k_txq *txq, int padsize)
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200738{
739 struct ath5k_hw *ah = sc->ah;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200740 struct ath5k_desc *ds = bf->desc;
741 struct sk_buff *skb = bf->skb;
Johannes Berga888d522008-05-26 16:43:39 +0200742 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200743 unsigned int pktlen, flags, keyidx = AR5K_TXKEYIX_INVALID;
Felix Fietkau2f7fe872008-10-05 18:05:48 +0200744 struct ieee80211_rate *rate;
745 unsigned int mrr_rate[3], mrr_tries[3];
746 int i, ret;
Bob Copeland8902ff42009-01-22 08:44:20 -0500747 u16 hw_rate;
Bob Copeland07c1e852009-01-22 08:44:21 -0500748 u16 cts_rate = 0;
749 u16 duration = 0;
Bob Copeland8902ff42009-01-22 08:44:20 -0500750 u8 rc_flags;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200751
752 flags = AR5K_TXDESC_INTREQ | AR5K_TXDESC_CLRDMASK;
Johannes Berge039fa42008-05-15 12:55:29 +0200753
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200754 /* XXX endianness */
755 bf->skbaddr = pci_map_single(sc->pdev, skb->data, skb->len,
756 PCI_DMA_TODEVICE);
757
Bob Copeland8902ff42009-01-22 08:44:20 -0500758 rate = ieee80211_get_tx_rate(sc->hw, info);
John W. Linvilled8e1ba72010-08-24 15:27:34 -0400759 if (!rate) {
760 ret = -EINVAL;
761 goto err_unmap;
762 }
Bob Copeland8902ff42009-01-22 08:44:20 -0500763
Johannes Berge039fa42008-05-15 12:55:29 +0200764 if (info->flags & IEEE80211_TX_CTL_NO_ACK)
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200765 flags |= AR5K_TXDESC_NOACK;
766
Bob Copeland8902ff42009-01-22 08:44:20 -0500767 rc_flags = info->control.rates[0].flags;
768 hw_rate = (rc_flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE) ?
769 rate->hw_value_short : rate->hw_value;
770
Bruno Randolf281c56d2008-02-05 18:44:55 +0900771 pktlen = skb->len;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200772
Nick Kossifidis8f655dd2009-03-15 22:20:35 +0200773 /* FIXME: If we are in g mode and rate is a CCK rate
774 * subtract ah->ah_txpower.txp_cck_ofdm_pwr_delta
775 * from tx power (value is in dB units already) */
Bob Copeland362695e2009-02-15 12:06:12 -0500776 if (info->control.hw_key) {
777 keyidx = info->control.hw_key->hw_key_idx;
778 pktlen += info->control.hw_key->icv_len;
779 }
Bob Copeland07c1e852009-01-22 08:44:21 -0500780 if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
781 flags |= AR5K_TXDESC_RTSENA;
782 cts_rate = ieee80211_get_rts_cts_rate(sc->hw, info)->hw_value;
783 duration = le16_to_cpu(ieee80211_rts_duration(sc->hw,
Ben Greearb1ae1ed2010-09-30 12:22:58 -0700784 info->control.vif, pktlen, info));
Bob Copeland07c1e852009-01-22 08:44:21 -0500785 }
786 if (rc_flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
787 flags |= AR5K_TXDESC_CTSENA;
788 cts_rate = ieee80211_get_rts_cts_rate(sc->hw, info)->hw_value;
789 duration = le16_to_cpu(ieee80211_ctstoself_duration(sc->hw,
Ben Greearb1ae1ed2010-09-30 12:22:58 -0700790 info->control.vif, pktlen, info));
Bob Copeland07c1e852009-01-22 08:44:21 -0500791 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200792 ret = ah->ah_setup_tx_desc(ah, ds, pktlen,
Benoit Papillault8127fbd2010-02-27 23:05:26 +0100793 ieee80211_get_hdrlen_from_skb(skb), padsize,
Bob Copeland2ac29272010-02-09 13:06:54 -0500794 get_hw_packet_type(skb),
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200795 (sc->power_level * 2),
Bob Copeland8902ff42009-01-22 08:44:20 -0500796 hw_rate,
Nick Kossifidis2bed03e2009-04-30 15:55:49 -0400797 info->control.rates[0].count, keyidx, ah->ah_tx_ant, flags,
Bob Copeland07c1e852009-01-22 08:44:21 -0500798 cts_rate, duration);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200799 if (ret)
800 goto err_unmap;
801
Felix Fietkau2f7fe872008-10-05 18:05:48 +0200802 memset(mrr_rate, 0, sizeof(mrr_rate));
803 memset(mrr_tries, 0, sizeof(mrr_tries));
804 for (i = 0; i < 3; i++) {
805 rate = ieee80211_get_alt_retry_rate(sc->hw, info, i);
806 if (!rate)
807 break;
808
809 mrr_rate[i] = rate->hw_value;
Johannes Berge6a98542008-10-21 12:40:02 +0200810 mrr_tries[i] = info->control.rates[i + 1].count;
Felix Fietkau2f7fe872008-10-05 18:05:48 +0200811 }
812
Bruno Randolfa6668192010-06-16 19:12:01 +0900813 ath5k_hw_setup_mrr_tx_desc(ah, ds,
Felix Fietkau2f7fe872008-10-05 18:05:48 +0200814 mrr_rate[0], mrr_tries[0],
815 mrr_rate[1], mrr_tries[1],
816 mrr_rate[2], mrr_tries[2]);
817
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200818 ds->ds_link = 0;
819 ds->ds_data = bf->skbaddr;
820
821 spin_lock_bh(&txq->lock);
822 list_add_tail(&bf->list, &txq->q);
Bruno Randolf925e0b02010-09-17 11:36:35 +0900823 txq->txq_len++;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200824 if (txq->link == NULL) /* is this first packet? */
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300825 ath5k_hw_set_txdp(ah, txq->qnum, bf->daddr);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200826 else /* no, so only link it */
827 *txq->link = bf->daddr;
828
829 txq->link = &ds->ds_link;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300830 ath5k_hw_start_tx_dma(ah, txq->qnum);
Jiri Slaby274c7c32008-07-15 17:44:20 +0200831 mmiowb();
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200832 spin_unlock_bh(&txq->lock);
833
834 return 0;
835err_unmap:
836 pci_unmap_single(sc->pdev, bf->skbaddr, skb->len, PCI_DMA_TODEVICE);
837 return ret;
838}
839
840/*******************\
841* Descriptors setup *
842\*******************/
843
844static int
845ath5k_desc_alloc(struct ath5k_softc *sc, struct pci_dev *pdev)
846{
847 struct ath5k_desc *ds;
848 struct ath5k_buf *bf;
849 dma_addr_t da;
850 unsigned int i;
851 int ret;
852
853 /* allocate descriptors */
854 sc->desc_len = sizeof(struct ath5k_desc) *
855 (ATH_TXBUF + ATH_RXBUF + ATH_BCBUF + 1);
856 sc->desc = pci_alloc_consistent(pdev, sc->desc_len, &sc->desc_daddr);
857 if (sc->desc == NULL) {
858 ATH5K_ERR(sc, "can't allocate descriptors\n");
859 ret = -ENOMEM;
860 goto err;
861 }
862 ds = sc->desc;
863 da = sc->desc_daddr;
864 ATH5K_DBG(sc, ATH5K_DEBUG_ANY, "DMA map: %p (%zu) -> %llx\n",
865 ds, sc->desc_len, (unsigned long long)sc->desc_daddr);
866
867 bf = kcalloc(1 + ATH_TXBUF + ATH_RXBUF + ATH_BCBUF,
868 sizeof(struct ath5k_buf), GFP_KERNEL);
869 if (bf == NULL) {
870 ATH5K_ERR(sc, "can't allocate bufptr\n");
871 ret = -ENOMEM;
872 goto err_free;
873 }
874 sc->bufptr = bf;
875
876 INIT_LIST_HEAD(&sc->rxbuf);
877 for (i = 0; i < ATH_RXBUF; i++, bf++, ds++, da += sizeof(*ds)) {
878 bf->desc = ds;
879 bf->daddr = da;
880 list_add_tail(&bf->list, &sc->rxbuf);
881 }
882
883 INIT_LIST_HEAD(&sc->txbuf);
884 sc->txbuf_len = ATH_TXBUF;
885 for (i = 0; i < ATH_TXBUF; i++, bf++, ds++,
886 da += sizeof(*ds)) {
887 bf->desc = ds;
888 bf->daddr = da;
889 list_add_tail(&bf->list, &sc->txbuf);
890 }
891
Ben Greearb1ae1ed2010-09-30 12:22:58 -0700892 /* beacon buffers */
893 INIT_LIST_HEAD(&sc->bcbuf);
894 for (i = 0; i < ATH_BCBUF; i++, bf++, ds++, da += sizeof(*ds)) {
895 bf->desc = ds;
896 bf->daddr = da;
897 list_add_tail(&bf->list, &sc->bcbuf);
898 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200899
900 return 0;
901err_free:
902 pci_free_consistent(pdev, sc->desc_len, sc->desc, sc->desc_daddr);
903err:
904 sc->desc = NULL;
905 return ret;
906}
907
908static void
909ath5k_desc_free(struct ath5k_softc *sc, struct pci_dev *pdev)
910{
911 struct ath5k_buf *bf;
912
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200913 list_for_each_entry(bf, &sc->txbuf, list)
Bruno Randolf9e4e43f2010-06-16 19:11:17 +0900914 ath5k_txbuf_free_skb(sc, bf);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200915 list_for_each_entry(bf, &sc->rxbuf, list)
Bruno Randolf9e4e43f2010-06-16 19:11:17 +0900916 ath5k_rxbuf_free_skb(sc, bf);
Ben Greearb1ae1ed2010-09-30 12:22:58 -0700917 list_for_each_entry(bf, &sc->bcbuf, list)
918 ath5k_txbuf_free_skb(sc, bf);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200919
920 /* Free memory associated with all descriptors */
921 pci_free_consistent(pdev, sc->desc_len, sc->desc, sc->desc_daddr);
Bruno Randolf39d63f22010-06-16 19:11:41 +0900922 sc->desc = NULL;
923 sc->desc_daddr = 0;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200924
925 kfree(sc->bufptr);
926 sc->bufptr = NULL;
927}
928
929
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200930/**************\
931* Queues setup *
932\**************/
933
934static struct ath5k_txq *
935ath5k_txq_setup(struct ath5k_softc *sc,
936 int qtype, int subtype)
937{
938 struct ath5k_hw *ah = sc->ah;
939 struct ath5k_txq *txq;
940 struct ath5k_txq_info qi = {
941 .tqi_subtype = subtype,
Bruno Randolfde8af452010-09-17 11:37:12 +0900942 /* XXX: default values not correct for B and XR channels,
943 * but who cares? */
944 .tqi_aifs = AR5K_TUNE_AIFS,
945 .tqi_cw_min = AR5K_TUNE_CWMIN,
946 .tqi_cw_max = AR5K_TUNE_CWMAX
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200947 };
948 int qnum;
949
950 /*
951 * Enable interrupts only for EOL and DESC conditions.
952 * We mark tx descriptors to receive a DESC interrupt
Bob Copelanda180a132010-08-15 13:03:12 -0400953 * when a tx queue gets deep; otherwise we wait for the
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200954 * EOL to reap descriptors. Note that this is done to
955 * reduce interrupt load and this only defers reaping
956 * descriptors, never transmitting frames. Aside from
957 * reducing interrupts this also permits more concurrency.
958 * The only potential downside is if the tx queue backs
959 * up in which case the top half of the kernel may backup
960 * due to a lack of tx descriptors.
961 */
962 qi.tqi_flags = AR5K_TXQ_FLAG_TXEOLINT_ENABLE |
963 AR5K_TXQ_FLAG_TXDESCINT_ENABLE;
964 qnum = ath5k_hw_setup_tx_queue(ah, qtype, &qi);
965 if (qnum < 0) {
966 /*
967 * NB: don't print a message, this happens
968 * normally on parts with too few tx queues
969 */
970 return ERR_PTR(qnum);
971 }
972 if (qnum >= ARRAY_SIZE(sc->txqs)) {
973 ATH5K_ERR(sc, "hw qnum %u out of range, max %tu!\n",
974 qnum, ARRAY_SIZE(sc->txqs));
975 ath5k_hw_release_tx_queue(ah, qnum);
976 return ERR_PTR(-EINVAL);
977 }
978 txq = &sc->txqs[qnum];
979 if (!txq->setup) {
980 txq->qnum = qnum;
981 txq->link = NULL;
982 INIT_LIST_HEAD(&txq->q);
983 spin_lock_init(&txq->lock);
984 txq->setup = true;
Bruno Randolf925e0b02010-09-17 11:36:35 +0900985 txq->txq_len = 0;
Bruno Randolf4edd7612010-09-17 11:36:56 +0900986 txq->txq_poll_mark = false;
Bruno Randolf923e5b32010-09-17 11:37:02 +0900987 txq->txq_stuck = 0;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200988 }
989 return &sc->txqs[qnum];
990}
991
992static int
993ath5k_beaconq_setup(struct ath5k_hw *ah)
994{
995 struct ath5k_txq_info qi = {
Bruno Randolfde8af452010-09-17 11:37:12 +0900996 /* XXX: default values not correct for B and XR channels,
997 * but who cares? */
998 .tqi_aifs = AR5K_TUNE_AIFS,
999 .tqi_cw_min = AR5K_TUNE_CWMIN,
1000 .tqi_cw_max = AR5K_TUNE_CWMAX,
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001001 /* NB: for dynamic turbo, don't enable any other interrupts */
1002 .tqi_flags = AR5K_TXQ_FLAG_TXDESCINT_ENABLE
1003 };
1004
1005 return ath5k_hw_setup_tx_queue(ah, AR5K_TX_QUEUE_BEACON, &qi);
1006}
1007
1008static int
1009ath5k_beaconq_config(struct ath5k_softc *sc)
1010{
1011 struct ath5k_hw *ah = sc->ah;
1012 struct ath5k_txq_info qi;
1013 int ret;
1014
1015 ret = ath5k_hw_get_tx_queueprops(ah, sc->bhalq, &qi);
1016 if (ret)
Bob Copelanda951ae22010-01-20 23:51:04 -05001017 goto err;
1018
Johannes Berg05c914f2008-09-11 00:01:58 +02001019 if (sc->opmode == NL80211_IFTYPE_AP ||
1020 sc->opmode == NL80211_IFTYPE_MESH_POINT) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001021 /*
1022 * Always burst out beacon and CAB traffic
1023 * (aifs = cwmin = cwmax = 0)
1024 */
1025 qi.tqi_aifs = 0;
1026 qi.tqi_cw_min = 0;
1027 qi.tqi_cw_max = 0;
Johannes Berg05c914f2008-09-11 00:01:58 +02001028 } else if (sc->opmode == NL80211_IFTYPE_ADHOC) {
Bruno Randolf6d91e1d2008-01-19 18:18:41 +09001029 /*
1030 * Adhoc mode; backoff between 0 and (2 * cw_min).
1031 */
1032 qi.tqi_aifs = 0;
1033 qi.tqi_cw_min = 0;
Bruno Randolfde8af452010-09-17 11:37:12 +09001034 qi.tqi_cw_max = 2 * AR5K_TUNE_CWMIN;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001035 }
1036
Bruno Randolf6d91e1d2008-01-19 18:18:41 +09001037 ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
1038 "beacon queueprops tqi_aifs:%d tqi_cw_min:%d tqi_cw_max:%d\n",
1039 qi.tqi_aifs, qi.tqi_cw_min, qi.tqi_cw_max);
1040
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001041 ret = ath5k_hw_set_tx_queueprops(ah, sc->bhalq, &qi);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001042 if (ret) {
1043 ATH5K_ERR(sc, "%s: unable to update parameters for beacon "
1044 "hardware queue!\n", __func__);
Bob Copelanda951ae22010-01-20 23:51:04 -05001045 goto err;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001046 }
Bob Copelanda951ae22010-01-20 23:51:04 -05001047 ret = ath5k_hw_reset_tx_queue(ah, sc->bhalq); /* push to h/w */
1048 if (ret)
1049 goto err;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001050
Bob Copelanda951ae22010-01-20 23:51:04 -05001051 /* reconfigure cabq with ready time to 80% of beacon_interval */
1052 ret = ath5k_hw_get_tx_queueprops(ah, AR5K_TX_QUEUE_ID_CAB, &qi);
1053 if (ret)
1054 goto err;
1055
1056 qi.tqi_ready_time = (sc->bintval * 80) / 100;
1057 ret = ath5k_hw_set_tx_queueprops(ah, AR5K_TX_QUEUE_ID_CAB, &qi);
1058 if (ret)
1059 goto err;
1060
1061 ret = ath5k_hw_reset_tx_queue(ah, AR5K_TX_QUEUE_ID_CAB);
1062err:
1063 return ret;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001064}
1065
Nick Kossifidis80dac9e2010-11-23 20:45:38 +02001066/**
1067 * ath5k_drain_tx_buffs - Empty tx buffers
1068 *
1069 * @sc The &struct ath5k_softc
1070 *
1071 * Empty tx buffers from all queues in preparation
1072 * of a reset or during shutdown.
1073 *
1074 * NB: this assumes output has been stopped and
1075 * we do not need to block ath5k_tx_tasklet
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001076 */
1077static void
Nick Kossifidis80dac9e2010-11-23 20:45:38 +02001078ath5k_drain_tx_buffs(struct ath5k_softc *sc)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001079{
Nick Kossifidis80dac9e2010-11-23 20:45:38 +02001080 struct ath5k_txq *txq;
1081 struct ath5k_buf *bf, *bf0;
1082 int i;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001083
Nick Kossifidis80dac9e2010-11-23 20:45:38 +02001084 for (i = 0; i < ARRAY_SIZE(sc->txqs); i++) {
1085 if (sc->txqs[i].setup) {
1086 txq = &sc->txqs[i];
1087 spin_lock_bh(&txq->lock);
1088 list_for_each_entry_safe(bf, bf0, &txq->q, list) {
1089 ath5k_debug_printtxbuf(sc, bf);
1090
1091 ath5k_txbuf_free_skb(sc, bf);
1092
1093 spin_lock_bh(&sc->txbuflock);
1094 list_move_tail(&bf->list, &sc->txbuf);
1095 sc->txbuf_len++;
1096 txq->txq_len--;
1097 spin_unlock_bh(&sc->txbuflock);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001098 }
Nick Kossifidis80dac9e2010-11-23 20:45:38 +02001099 txq->link = NULL;
1100 txq->txq_poll_mark = false;
1101 spin_unlock_bh(&txq->lock);
1102 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001103 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001104}
1105
1106static void
1107ath5k_txq_release(struct ath5k_softc *sc)
1108{
1109 struct ath5k_txq *txq = sc->txqs;
1110 unsigned int i;
1111
1112 for (i = 0; i < ARRAY_SIZE(sc->txqs); i++, txq++)
1113 if (txq->setup) {
1114 ath5k_hw_release_tx_queue(sc->ah, txq->qnum);
1115 txq->setup = false;
1116 }
1117}
1118
1119
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001120/*************\
1121* RX Handling *
1122\*************/
1123
1124/*
1125 * Enable the receive h/w following a reset.
1126 */
1127static int
1128ath5k_rx_start(struct ath5k_softc *sc)
1129{
1130 struct ath5k_hw *ah = sc->ah;
Luis R. Rodriguezdb719712009-09-10 11:20:57 -07001131 struct ath_common *common = ath5k_hw_common(ah);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001132 struct ath5k_buf *bf;
1133 int ret;
1134
Nick Kossifidisb6127982010-08-15 13:03:11 -04001135 common->rx_bufsize = roundup(IEEE80211_MAX_FRAME_LEN, common->cachelsz);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001136
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -08001137 ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "cachelsz %u rx_bufsize %u\n",
1138 common->cachelsz, common->rx_bufsize);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001139
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001140 spin_lock_bh(&sc->rxbuflock);
Bob Copeland26925042009-04-15 07:57:36 -04001141 sc->rxlink = NULL;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001142 list_for_each_entry(bf, &sc->rxbuf, list) {
1143 ret = ath5k_rxbuf_setup(sc, bf);
1144 if (ret != 0) {
1145 spin_unlock_bh(&sc->rxbuflock);
1146 goto err;
1147 }
1148 }
1149 bf = list_first_entry(&sc->rxbuf, struct ath5k_buf, list);
Bob Copeland26925042009-04-15 07:57:36 -04001150 ath5k_hw_set_rxdp(ah, bf->daddr);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001151 spin_unlock_bh(&sc->rxbuflock);
1152
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001153 ath5k_hw_start_rx_dma(ah); /* enable recv descriptors */
Ben Greearb1ae1ed2010-09-30 12:22:58 -07001154 ath5k_mode_setup(sc, NULL); /* set filters, etc. */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001155 ath5k_hw_start_rx_pcu(ah); /* re-enable PCU/DMA engine */
1156
1157 return 0;
1158err:
1159 return ret;
1160}
1161
1162/*
Nick Kossifidis80dac9e2010-11-23 20:45:38 +02001163 * Disable the receive logic on PCU (DRU)
1164 * In preparation for a shutdown.
1165 *
1166 * Note: Doesn't stop rx DMA, ath5k_hw_dma_stop
1167 * does.
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001168 */
1169static void
1170ath5k_rx_stop(struct ath5k_softc *sc)
1171{
1172 struct ath5k_hw *ah = sc->ah;
1173
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001174 ath5k_hw_set_rx_filter(ah, 0); /* clear recv filter */
Nick Kossifidis80dac9e2010-11-23 20:45:38 +02001175 ath5k_hw_stop_rx_pcu(ah); /* disable PCU */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001176
1177 ath5k_debug_printrxbuffs(sc, ah);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001178}
1179
1180static unsigned int
Bruno Randolf8a89f062010-06-16 19:11:51 +09001181ath5k_rx_decrypted(struct ath5k_softc *sc, struct sk_buff *skb,
1182 struct ath5k_rx_status *rs)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001183{
Luis R. Rodriguezdc1e0012009-11-04 17:47:31 -08001184 struct ath5k_hw *ah = sc->ah;
1185 struct ath_common *common = ath5k_hw_common(ah);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001186 struct ieee80211_hdr *hdr = (void *)skb->data;
Harvey Harrison798ee982008-07-15 18:44:02 -07001187 unsigned int keyix, hlen;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001188
Bruno Randolfb47f4072008-03-05 18:35:45 +09001189 if (!(rs->rs_status & AR5K_RXERR_DECRYPT) &&
1190 rs->rs_keyix != AR5K_RXKEYIX_INVALID)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001191 return RX_FLAG_DECRYPTED;
1192
1193 /* Apparently when a default key is used to decrypt the packet
1194 the hw does not set the index used to decrypt. In such cases
1195 get the index from the packet. */
Harvey Harrison798ee982008-07-15 18:44:02 -07001196 hlen = ieee80211_hdrlen(hdr->frame_control);
Harvey Harrison24b56e72008-06-14 23:33:38 -07001197 if (ieee80211_has_protected(hdr->frame_control) &&
1198 !(rs->rs_status & AR5K_RXERR_DECRYPT) &&
1199 skb->len >= hlen + 4) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001200 keyix = skb->data[hlen + 3] >> 6;
1201
Luis R. Rodriguezdc1e0012009-11-04 17:47:31 -08001202 if (test_bit(keyix, common->keymap))
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001203 return RX_FLAG_DECRYPTED;
1204 }
1205
1206 return 0;
1207}
1208
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001209
1210static void
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001211ath5k_check_ibss_tsf(struct ath5k_softc *sc, struct sk_buff *skb,
1212 struct ieee80211_rx_status *rxs)
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001213{
Luis R. Rodriguez954fece2009-09-10 10:51:33 -07001214 struct ath_common *common = ath5k_hw_common(sc->ah);
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001215 u64 tsf, bc_tstamp;
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001216 u32 hw_tu;
1217 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)skb->data;
1218
Harvey Harrison24b56e72008-06-14 23:33:38 -07001219 if (ieee80211_is_beacon(mgmt->frame_control) &&
Pavel Roskin38c07b42008-02-26 17:59:14 -05001220 le16_to_cpu(mgmt->u.beacon.capab_info) & WLAN_CAPABILITY_IBSS &&
Luis R. Rodriguez954fece2009-09-10 10:51:33 -07001221 memcmp(mgmt->bssid, common->curbssid, ETH_ALEN) == 0) {
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001222 /*
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001223 * Received an IBSS beacon with the same BSSID. Hardware *must*
1224 * have updated the local TSF. We have to work around various
1225 * hardware bugs, though...
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001226 */
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001227 tsf = ath5k_hw_get_tsf64(sc->ah);
1228 bc_tstamp = le64_to_cpu(mgmt->u.beacon.timestamp);
1229 hw_tu = TSF_TO_TU(tsf);
1230
1231 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
1232 "beacon %llx mactime %llx (diff %lld) tsf now %llx\n",
John W. Linville06501d22008-04-01 17:38:47 -04001233 (unsigned long long)bc_tstamp,
1234 (unsigned long long)rxs->mactime,
1235 (unsigned long long)(rxs->mactime - bc_tstamp),
1236 (unsigned long long)tsf);
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001237
1238 /*
1239 * Sometimes the HW will give us a wrong tstamp in the rx
1240 * status, causing the timestamp extension to go wrong.
1241 * (This seems to happen especially with beacon frames bigger
1242 * than 78 byte (incl. FCS))
1243 * But we know that the receive timestamp must be later than the
1244 * timestamp of the beacon since HW must have synced to that.
1245 *
1246 * NOTE: here we assume mactime to be after the frame was
1247 * received, not like mac80211 which defines it at the start.
1248 */
1249 if (bc_tstamp > rxs->mactime) {
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001250 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001251 "fixing mactime from %llx to %llx\n",
John W. Linville06501d22008-04-01 17:38:47 -04001252 (unsigned long long)rxs->mactime,
1253 (unsigned long long)tsf);
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001254 rxs->mactime = tsf;
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001255 }
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001256
1257 /*
1258 * Local TSF might have moved higher than our beacon timers,
1259 * in that case we have to update them to continue sending
1260 * beacons. This also takes care of synchronizing beacon sending
1261 * times with other stations.
1262 */
1263 if (hw_tu >= sc->nexttbtt)
1264 ath5k_beacon_update_timers(sc, bc_tstamp);
Bruno Randolf7f896122010-09-27 12:22:21 +09001265
1266 /* Check if the beacon timers are still correct, because a TSF
1267 * update might have created a window between them - for a
1268 * longer description see the comment of this function: */
1269 if (!ath5k_hw_check_beacon_timers(sc->ah, sc->bintval)) {
1270 ath5k_beacon_update_timers(sc, bc_tstamp);
1271 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
1272 "fixed beacon timers after beacon receive\n");
1273 }
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001274 }
1275}
1276
Bruno Randolfb4ea4492010-03-25 14:49:25 +09001277static void
1278ath5k_update_beacon_rssi(struct ath5k_softc *sc, struct sk_buff *skb, int rssi)
1279{
1280 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)skb->data;
1281 struct ath5k_hw *ah = sc->ah;
1282 struct ath_common *common = ath5k_hw_common(ah);
1283
1284 /* only beacons from our BSSID */
1285 if (!ieee80211_is_beacon(mgmt->frame_control) ||
1286 memcmp(mgmt->bssid, common->curbssid, ETH_ALEN) != 0)
1287 return;
1288
Bruno Randolfeef39be2010-11-16 10:58:43 +09001289 ewma_add(&ah->ah_beacon_rssi_avg, rssi);
Bruno Randolfb4ea4492010-03-25 14:49:25 +09001290
1291 /* in IBSS mode we should keep RSSI statistics per neighbour */
1292 /* le16_to_cpu(mgmt->u.beacon.capab_info) & WLAN_CAPABILITY_IBSS */
1293}
1294
Benoit Papillault8127fbd2010-02-27 23:05:26 +01001295/*
Bob Copelanda180a132010-08-15 13:03:12 -04001296 * Compute padding position. skb must contain an IEEE 802.11 frame
Benoit Papillault8127fbd2010-02-27 23:05:26 +01001297 */
1298static int ath5k_common_padpos(struct sk_buff *skb)
1299{
1300 struct ieee80211_hdr * hdr = (struct ieee80211_hdr *)skb->data;
1301 __le16 frame_control = hdr->frame_control;
1302 int padpos = 24;
1303
1304 if (ieee80211_has_a4(frame_control)) {
1305 padpos += ETH_ALEN;
1306 }
1307 if (ieee80211_is_data_qos(frame_control)) {
1308 padpos += IEEE80211_QOS_CTL_LEN;
1309 }
1310
1311 return padpos;
1312}
1313
1314/*
Bob Copelanda180a132010-08-15 13:03:12 -04001315 * This function expects an 802.11 frame and returns the number of
1316 * bytes added, or -1 if we don't have enough header room.
Benoit Papillault8127fbd2010-02-27 23:05:26 +01001317 */
Benoit Papillault8127fbd2010-02-27 23:05:26 +01001318static int ath5k_add_padding(struct sk_buff *skb)
1319{
1320 int padpos = ath5k_common_padpos(skb);
1321 int padsize = padpos & 3;
1322
1323 if (padsize && skb->len>padpos) {
1324
1325 if (skb_headroom(skb) < padsize)
1326 return -1;
1327
1328 skb_push(skb, padsize);
1329 memmove(skb->data, skb->data+padsize, padpos);
1330 return padsize;
1331 }
1332
1333 return 0;
1334}
1335
1336/*
Bob Copelanda180a132010-08-15 13:03:12 -04001337 * The MAC header is padded to have 32-bit boundary if the
1338 * packet payload is non-zero. The general calculation for
1339 * padsize would take into account odd header lengths:
1340 * padsize = 4 - (hdrlen & 3); however, since only
1341 * even-length headers are used, padding can only be 0 or 2
1342 * bytes and we can optimize this a bit. We must not try to
1343 * remove padding from short control frames that do not have a
1344 * payload.
1345 *
1346 * This function expects an 802.11 frame and returns the number of
1347 * bytes removed.
Benoit Papillault8127fbd2010-02-27 23:05:26 +01001348 */
Benoit Papillault8127fbd2010-02-27 23:05:26 +01001349static int ath5k_remove_padding(struct sk_buff *skb)
1350{
1351 int padpos = ath5k_common_padpos(skb);
1352 int padsize = padpos & 3;
1353
1354 if (padsize && skb->len>=padpos+padsize) {
1355 memmove(skb->data + padsize, skb->data, padpos);
1356 skb_pull(skb, padsize);
1357 return padsize;
1358 }
1359
1360 return 0;
1361}
1362
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001363static void
Bruno Randolf8a89f062010-06-16 19:11:51 +09001364ath5k_receive_frame(struct ath5k_softc *sc, struct sk_buff *skb,
1365 struct ath5k_rx_status *rs)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001366{
Bob Copeland1c5256b2009-08-24 23:00:32 -04001367 struct ieee80211_rx_status *rxs;
Bruno Randolf8a89f062010-06-16 19:11:51 +09001368
Bruno Randolf8a89f062010-06-16 19:11:51 +09001369 ath5k_remove_padding(skb);
1370
1371 rxs = IEEE80211_SKB_RXCB(skb);
1372
1373 rxs->flag = 0;
1374 if (unlikely(rs->rs_status & AR5K_RXERR_MIC))
1375 rxs->flag |= RX_FLAG_MMIC_ERROR;
1376
1377 /*
1378 * always extend the mac timestamp, since this information is
1379 * also needed for proper IBSS merging.
1380 *
1381 * XXX: it might be too late to do it here, since rs_tstamp is
1382 * 15bit only. that means TSF extension has to be done within
1383 * 32768usec (about 32ms). it might be necessary to move this to
1384 * the interrupt handler, like it is done in madwifi.
1385 *
1386 * Unfortunately we don't know when the hardware takes the rx
1387 * timestamp (beginning of phy frame, data frame, end of rx?).
1388 * The only thing we know is that it is hardware specific...
1389 * On AR5213 it seems the rx timestamp is at the end of the
1390 * frame, but i'm not sure.
1391 *
1392 * NOTE: mac80211 defines mactime at the beginning of the first
1393 * data symbol. Since we don't have any time references it's
1394 * impossible to comply to that. This affects IBSS merge only
1395 * right now, so it's not too bad...
1396 */
1397 rxs->mactime = ath5k_extend_tsf(sc->ah, rs->rs_tstamp);
1398 rxs->flag |= RX_FLAG_TSFT;
1399
1400 rxs->freq = sc->curchan->center_freq;
1401 rxs->band = sc->curband->band;
1402
1403 rxs->signal = sc->ah->ah_noise_floor + rs->rs_rssi;
1404
1405 rxs->antenna = rs->rs_antenna;
1406
1407 if (rs->rs_antenna > 0 && rs->rs_antenna < 5)
1408 sc->stats.antenna_rx[rs->rs_antenna]++;
1409 else
1410 sc->stats.antenna_rx[0]++; /* invalid */
1411
1412 rxs->rate_idx = ath5k_hw_to_driver_rix(sc, rs->rs_rate);
1413 rxs->flag |= ath5k_rx_decrypted(sc, skb, rs);
1414
1415 if (rxs->rate_idx >= 0 && rs->rs_rate ==
1416 sc->curband->bitrates[rxs->rate_idx].hw_value_short)
1417 rxs->flag |= RX_FLAG_SHORTPRE;
1418
1419 ath5k_debug_dump_skb(sc, skb, "RX ", 0);
1420
1421 ath5k_update_beacon_rssi(sc, skb, rs->rs_rssi);
1422
1423 /* check beacons in IBSS mode */
1424 if (sc->opmode == NL80211_IFTYPE_ADHOC)
1425 ath5k_check_ibss_tsf(sc, skb, rxs);
1426
1427 ieee80211_rx(sc->hw, skb);
1428}
1429
Bruno Randolf02a78b42010-06-16 19:11:56 +09001430/** ath5k_frame_receive_ok() - Do we want to receive this frame or not?
1431 *
1432 * Check if we want to further process this frame or not. Also update
1433 * statistics. Return true if we want this frame, false if not.
1434 */
1435static bool
1436ath5k_receive_frame_ok(struct ath5k_softc *sc, struct ath5k_rx_status *rs)
1437{
1438 sc->stats.rx_all_count++;
Ben Greearb72acdd2010-10-01 10:54:04 -07001439 sc->stats.rx_bytes_count += rs->rs_datalen;
Bruno Randolf02a78b42010-06-16 19:11:56 +09001440
1441 if (unlikely(rs->rs_status)) {
1442 if (rs->rs_status & AR5K_RXERR_CRC)
1443 sc->stats.rxerr_crc++;
1444 if (rs->rs_status & AR5K_RXERR_FIFO)
1445 sc->stats.rxerr_fifo++;
1446 if (rs->rs_status & AR5K_RXERR_PHY) {
1447 sc->stats.rxerr_phy++;
1448 if (rs->rs_phyerr > 0 && rs->rs_phyerr < 32)
1449 sc->stats.rxerr_phy_code[rs->rs_phyerr]++;
1450 return false;
1451 }
1452 if (rs->rs_status & AR5K_RXERR_DECRYPT) {
1453 /*
1454 * Decrypt error. If the error occurred
1455 * because there was no hardware key, then
1456 * let the frame through so the upper layers
1457 * can process it. This is necessary for 5210
1458 * parts which have no way to setup a ``clear''
1459 * key cache entry.
1460 *
1461 * XXX do key cache faulting
1462 */
1463 sc->stats.rxerr_decrypt++;
1464 if (rs->rs_keyix == AR5K_RXKEYIX_INVALID &&
1465 !(rs->rs_status & AR5K_RXERR_CRC))
1466 return true;
1467 }
1468 if (rs->rs_status & AR5K_RXERR_MIC) {
1469 sc->stats.rxerr_mic++;
1470 return true;
1471 }
1472
Bob Copeland23538c22010-08-15 13:03:13 -04001473 /* reject any frames with non-crypto errors */
1474 if (rs->rs_status & ~(AR5K_RXERR_DECRYPT))
Bruno Randolf02a78b42010-06-16 19:11:56 +09001475 return false;
1476 }
1477
1478 if (unlikely(rs->rs_more)) {
1479 sc->stats.rxerr_jumbo++;
1480 return false;
1481 }
1482 return true;
1483}
1484
Bruno Randolf8a89f062010-06-16 19:11:51 +09001485static void
1486ath5k_tasklet_rx(unsigned long data)
1487{
Bruno Randolfb47f4072008-03-05 18:35:45 +09001488 struct ath5k_rx_status rs = {};
Bob Copelandb6ea0352009-01-10 14:42:54 -05001489 struct sk_buff *skb, *next_skb;
1490 dma_addr_t next_skb_addr;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001491 struct ath5k_softc *sc = (void *)data;
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -08001492 struct ath5k_hw *ah = sc->ah;
1493 struct ath_common *common = ath5k_hw_common(ah);
Bob Copelandc57ca812009-04-15 07:57:35 -04001494 struct ath5k_buf *bf;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001495 struct ath5k_desc *ds;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001496 int ret;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001497
1498 spin_lock(&sc->rxbuflock);
Jiri Slaby3a0f2c82008-07-15 17:44:18 +02001499 if (list_empty(&sc->rxbuf)) {
1500 ATH5K_WARN(sc, "empty rx buf pool\n");
1501 goto unlock;
1502 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001503 do {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001504 bf = list_first_entry(&sc->rxbuf, struct ath5k_buf, list);
1505 BUG_ON(bf->skb == NULL);
1506 skb = bf->skb;
1507 ds = bf->desc;
1508
Bob Copelandc57ca812009-04-15 07:57:35 -04001509 /* bail if HW is still using self-linked descriptor */
1510 if (ath5k_hw_get_rxdp(sc->ah) == bf->daddr)
1511 break;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001512
Bruno Randolfb47f4072008-03-05 18:35:45 +09001513 ret = sc->ah->ah_proc_rx_desc(sc->ah, ds, &rs);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001514 if (unlikely(ret == -EINPROGRESS))
1515 break;
1516 else if (unlikely(ret)) {
1517 ATH5K_ERR(sc, "error in processing rx descriptor\n");
Bruno Randolf76443952010-03-09 16:56:00 +09001518 sc->stats.rxerr_proc++;
Bruno Randolfb16062f2010-06-16 19:11:46 +09001519 break;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001520 }
1521
Bruno Randolf02a78b42010-06-16 19:11:56 +09001522 if (ath5k_receive_frame_ok(sc, &rs)) {
1523 next_skb = ath5k_rx_skb_alloc(sc, &next_skb_addr);
Bruno Randolf76443952010-03-09 16:56:00 +09001524
Bruno Randolf02a78b42010-06-16 19:11:56 +09001525 /*
1526 * If we can't replace bf->skb with a new skb under
1527 * memory pressure, just skip this packet
1528 */
1529 if (!next_skb)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001530 goto next;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001531
Bruno Randolf02a78b42010-06-16 19:11:56 +09001532 pci_unmap_single(sc->pdev, bf->skbaddr,
1533 common->rx_bufsize,
1534 PCI_DMA_FROMDEVICE);
1535
1536 skb_put(skb, rs.rs_datalen);
1537
1538 ath5k_receive_frame(sc, skb, &rs);
1539
1540 bf->skb = next_skb;
1541 bf->skbaddr = next_skb_addr;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001542 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001543next:
1544 list_move_tail(&bf->list, &sc->rxbuf);
1545 } while (ath5k_rxbuf_setup(sc, bf) == 0);
Jiri Slaby3a0f2c82008-07-15 17:44:18 +02001546unlock:
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001547 spin_unlock(&sc->rxbuflock);
1548}
1549
1550
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001551/*************\
1552* TX Handling *
1553\*************/
1554
Bob Copeland8a63fac2010-09-17 12:45:07 +09001555static int ath5k_tx_queue(struct ieee80211_hw *hw, struct sk_buff *skb,
1556 struct ath5k_txq *txq)
1557{
1558 struct ath5k_softc *sc = hw->priv;
1559 struct ath5k_buf *bf;
1560 unsigned long flags;
1561 int padsize;
1562
1563 ath5k_debug_dump_skb(sc, skb, "TX ", 1);
1564
1565 /*
1566 * The hardware expects the header padded to 4 byte boundaries.
1567 * If this is not the case, we add the padding after the header.
1568 */
1569 padsize = ath5k_add_padding(skb);
1570 if (padsize < 0) {
1571 ATH5K_ERR(sc, "tx hdrlen not %%4: not enough"
1572 " headroom to pad");
1573 goto drop_packet;
1574 }
1575
Bruno Randolf925e0b02010-09-17 11:36:35 +09001576 if (txq->txq_len >= ATH5K_TXQ_LEN_MAX)
1577 ieee80211_stop_queue(hw, txq->qnum);
1578
Bob Copeland8a63fac2010-09-17 12:45:07 +09001579 spin_lock_irqsave(&sc->txbuflock, flags);
1580 if (list_empty(&sc->txbuf)) {
1581 ATH5K_ERR(sc, "no further txbuf available, dropping packet\n");
1582 spin_unlock_irqrestore(&sc->txbuflock, flags);
Bruno Randolf651d9372010-09-17 11:36:46 +09001583 ieee80211_stop_queues(hw);
Bob Copeland8a63fac2010-09-17 12:45:07 +09001584 goto drop_packet;
1585 }
1586 bf = list_first_entry(&sc->txbuf, struct ath5k_buf, list);
1587 list_del(&bf->list);
1588 sc->txbuf_len--;
1589 if (list_empty(&sc->txbuf))
1590 ieee80211_stop_queues(hw);
1591 spin_unlock_irqrestore(&sc->txbuflock, flags);
1592
1593 bf->skb = skb;
1594
1595 if (ath5k_txbuf_setup(sc, bf, txq, padsize)) {
1596 bf->skb = NULL;
1597 spin_lock_irqsave(&sc->txbuflock, flags);
1598 list_add_tail(&bf->list, &sc->txbuf);
1599 sc->txbuf_len++;
1600 spin_unlock_irqrestore(&sc->txbuflock, flags);
1601 goto drop_packet;
1602 }
1603 return NETDEV_TX_OK;
1604
1605drop_packet:
1606 dev_kfree_skb_any(skb);
1607 return NETDEV_TX_OK;
1608}
1609
Bruno Randolf14404012010-09-17 11:36:51 +09001610static void
1611ath5k_tx_frame_completed(struct ath5k_softc *sc, struct sk_buff *skb,
1612 struct ath5k_tx_status *ts)
1613{
1614 struct ieee80211_tx_info *info;
1615 int i;
1616
1617 sc->stats.tx_all_count++;
Ben Greearb72acdd2010-10-01 10:54:04 -07001618 sc->stats.tx_bytes_count += skb->len;
Bruno Randolf14404012010-09-17 11:36:51 +09001619 info = IEEE80211_SKB_CB(skb);
1620
1621 ieee80211_tx_info_clear_status(info);
1622 for (i = 0; i < 4; i++) {
1623 struct ieee80211_tx_rate *r =
1624 &info->status.rates[i];
1625
1626 if (ts->ts_rate[i]) {
1627 r->idx = ath5k_hw_to_driver_rix(sc, ts->ts_rate[i]);
1628 r->count = ts->ts_retry[i];
1629 } else {
1630 r->idx = -1;
1631 r->count = 0;
1632 }
1633 }
1634
1635 /* count the successful attempt as well */
1636 info->status.rates[ts->ts_final_idx].count++;
1637
1638 if (unlikely(ts->ts_status)) {
1639 sc->stats.ack_fail++;
1640 if (ts->ts_status & AR5K_TXERR_FILT) {
1641 info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
1642 sc->stats.txerr_filt++;
1643 }
1644 if (ts->ts_status & AR5K_TXERR_XRETRY)
1645 sc->stats.txerr_retry++;
1646 if (ts->ts_status & AR5K_TXERR_FIFO)
1647 sc->stats.txerr_fifo++;
1648 } else {
1649 info->flags |= IEEE80211_TX_STAT_ACK;
1650 info->status.ack_signal = ts->ts_rssi;
1651 }
1652
1653 /*
1654 * Remove MAC header padding before giving the frame
1655 * back to mac80211.
1656 */
1657 ath5k_remove_padding(skb);
1658
1659 if (ts->ts_antenna > 0 && ts->ts_antenna < 5)
1660 sc->stats.antenna_tx[ts->ts_antenna]++;
1661 else
1662 sc->stats.antenna_tx[0]++; /* invalid */
1663
1664 ieee80211_tx_status(sc->hw, skb);
1665}
Bob Copeland8a63fac2010-09-17 12:45:07 +09001666
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001667static void
1668ath5k_tx_processq(struct ath5k_softc *sc, struct ath5k_txq *txq)
1669{
Bruno Randolfb47f4072008-03-05 18:35:45 +09001670 struct ath5k_tx_status ts = {};
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001671 struct ath5k_buf *bf, *bf0;
1672 struct ath5k_desc *ds;
1673 struct sk_buff *skb;
Bruno Randolf14404012010-09-17 11:36:51 +09001674 int ret;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001675
1676 spin_lock(&txq->lock);
1677 list_for_each_entry_safe(bf, bf0, &txq->q, list) {
Bruno Randolf23413292010-09-17 11:37:07 +09001678
1679 txq->txq_poll_mark = false;
1680
1681 /* skb might already have been processed last time. */
1682 if (bf->skb != NULL) {
1683 ds = bf->desc;
1684
1685 ret = sc->ah->ah_proc_tx_desc(sc->ah, ds, &ts);
1686 if (unlikely(ret == -EINPROGRESS))
1687 break;
1688 else if (unlikely(ret)) {
1689 ATH5K_ERR(sc,
1690 "error %d while processing "
1691 "queue %u\n", ret, txq->qnum);
1692 break;
1693 }
1694
1695 skb = bf->skb;
1696 bf->skb = NULL;
1697 pci_unmap_single(sc->pdev, bf->skbaddr, skb->len,
1698 PCI_DMA_TODEVICE);
1699 ath5k_tx_frame_completed(sc, skb, &ts);
1700 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001701
Bob Copelanda05988b2010-04-07 23:55:58 -04001702 /*
1703 * It's possible that the hardware can say the buffer is
1704 * completed when it hasn't yet loaded the ds_link from
Bruno Randolf23413292010-09-17 11:37:07 +09001705 * host memory and moved on.
1706 * Always keep the last descriptor to avoid HW races...
Bob Copelanda05988b2010-04-07 23:55:58 -04001707 */
Bruno Randolf23413292010-09-17 11:37:07 +09001708 if (ath5k_hw_get_txdp(sc->ah, txq->qnum) != bf->daddr) {
1709 spin_lock(&sc->txbuflock);
1710 list_move_tail(&bf->list, &sc->txbuf);
1711 sc->txbuf_len++;
1712 txq->txq_len--;
1713 spin_unlock(&sc->txbuflock);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001714 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001715 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001716 spin_unlock(&txq->lock);
Bruno Randolf4198a8d2010-10-05 13:27:17 +09001717 if (txq->txq_len < ATH5K_TXQ_LEN_LOW && txq->qnum < 4)
Bruno Randolf925e0b02010-09-17 11:36:35 +09001718 ieee80211_wake_queue(sc->hw, txq->qnum);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001719}
1720
1721static void
1722ath5k_tasklet_tx(unsigned long data)
1723{
Bob Copeland8784d2e2009-07-29 17:32:28 -04001724 int i;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001725 struct ath5k_softc *sc = (void *)data;
1726
Bob Copeland8784d2e2009-07-29 17:32:28 -04001727 for (i=0; i < AR5K_NUM_TX_QUEUES; i++)
1728 if (sc->txqs[i].setup && (sc->ah->ah_txq_isr & BIT(i)))
1729 ath5k_tx_processq(sc, &sc->txqs[i]);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001730}
1731
1732
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001733/*****************\
1734* Beacon handling *
1735\*****************/
1736
1737/*
1738 * Setup the beacon frame for transmit.
1739 */
1740static int
Johannes Berge039fa42008-05-15 12:55:29 +02001741ath5k_beacon_setup(struct ath5k_softc *sc, struct ath5k_buf *bf)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001742{
1743 struct sk_buff *skb = bf->skb;
Johannes Berga888d522008-05-26 16:43:39 +02001744 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001745 struct ath5k_hw *ah = sc->ah;
1746 struct ath5k_desc *ds;
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04001747 int ret = 0;
1748 u8 antenna;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001749 u32 flags;
Benoit Papillault8127fbd2010-02-27 23:05:26 +01001750 const int padsize = 0;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001751
1752 bf->skbaddr = pci_map_single(sc->pdev, skb->data, skb->len,
1753 PCI_DMA_TODEVICE);
1754 ATH5K_DBG(sc, ATH5K_DEBUG_BEACON, "skb %p [data %p len %u] "
1755 "skbaddr %llx\n", skb, skb->data, skb->len,
1756 (unsigned long long)bf->skbaddr);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -07001757 if (pci_dma_mapping_error(sc->pdev, bf->skbaddr)) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001758 ATH5K_ERR(sc, "beacon DMA mapping failed\n");
1759 return -EIO;
1760 }
1761
1762 ds = bf->desc;
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04001763 antenna = ah->ah_tx_ant;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001764
1765 flags = AR5K_TXDESC_NOACK;
Johannes Berg05c914f2008-09-11 00:01:58 +02001766 if (sc->opmode == NL80211_IFTYPE_ADHOC && ath5k_hw_hasveol(ah)) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001767 ds->ds_link = bf->daddr; /* self-linked */
1768 flags |= AR5K_TXDESC_VEOL;
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04001769 } else
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001770 ds->ds_link = 0;
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04001771
1772 /*
1773 * If we use multiple antennas on AP and use
1774 * the Sectored AP scenario, switch antenna every
1775 * 4 beacons to make sure everybody hears our AP.
1776 * When a client tries to associate, hw will keep
1777 * track of the tx antenna to be used for this client
1778 * automaticaly, based on ACKed packets.
1779 *
1780 * Note: AP still listens and transmits RTS on the
1781 * default antenna which is supposed to be an omni.
1782 *
1783 * Note2: On sectored scenarios it's possible to have
Bob Copelanda180a132010-08-15 13:03:12 -04001784 * multiple antennas (1 omni -- the default -- and 14
1785 * sectors), so if we choose to actually support this
1786 * mode, we need to allow the user to set how many antennas
1787 * we have and tweak the code below to send beacons
1788 * on all of them.
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04001789 */
1790 if (ah->ah_ant_mode == AR5K_ANTMODE_SECTOR_AP)
1791 antenna = sc->bsent & 4 ? 2 : 1;
1792
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001793
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02001794 /* FIXME: If we are in g mode and rate is a CCK rate
1795 * subtract ah->ah_txpower.txp_cck_ofdm_pwr_delta
1796 * from tx power (value is in dB units already) */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001797 ds->ds_data = bf->skbaddr;
Bruno Randolf281c56d2008-02-05 18:44:55 +09001798 ret = ah->ah_setup_tx_desc(ah, ds, skb->len,
Benoit Papillault8127fbd2010-02-27 23:05:26 +01001799 ieee80211_get_hdrlen_from_skb(skb), padsize,
Luis R. Rodriguez400ec452008-02-03 21:51:49 -05001800 AR5K_PKT_TYPE_BEACON, (sc->power_level * 2),
Johannes Berge039fa42008-05-15 12:55:29 +02001801 ieee80211_get_tx_rate(sc->hw, info)->hw_value,
Johannes Berg2e92e6f2008-05-15 12:55:27 +02001802 1, AR5K_TXKEYIX_INVALID,
Luis R. Rodriguez400ec452008-02-03 21:51:49 -05001803 antenna, flags, 0, 0);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001804 if (ret)
1805 goto err_unmap;
1806
1807 return 0;
1808err_unmap:
1809 pci_unmap_single(sc->pdev, bf->skbaddr, skb->len, PCI_DMA_TODEVICE);
1810 return ret;
1811}
1812
1813/*
Bob Copeland8a63fac2010-09-17 12:45:07 +09001814 * Updates the beacon that is sent by ath5k_beacon_send. For adhoc,
1815 * this is called only once at config_bss time, for AP we do it every
1816 * SWBA interrupt so that the TIM will reflect buffered frames.
1817 *
1818 * Called with the beacon lock.
1819 */
1820static int
1821ath5k_beacon_update(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1822{
1823 int ret;
1824 struct ath5k_softc *sc = hw->priv;
Ben Greearb1ae1ed2010-09-30 12:22:58 -07001825 struct ath5k_vif *avf = (void *)vif->drv_priv;
Bob Copeland8a63fac2010-09-17 12:45:07 +09001826 struct sk_buff *skb;
1827
1828 if (WARN_ON(!vif)) {
1829 ret = -EINVAL;
1830 goto out;
1831 }
1832
1833 skb = ieee80211_beacon_get(hw, vif);
1834
1835 if (!skb) {
1836 ret = -ENOMEM;
1837 goto out;
1838 }
1839
1840 ath5k_debug_dump_skb(sc, skb, "BC ", 1);
1841
Ben Greearb1ae1ed2010-09-30 12:22:58 -07001842 ath5k_txbuf_free_skb(sc, avf->bbuf);
1843 avf->bbuf->skb = skb;
1844 ret = ath5k_beacon_setup(sc, avf->bbuf);
Bob Copeland8a63fac2010-09-17 12:45:07 +09001845 if (ret)
Ben Greearb1ae1ed2010-09-30 12:22:58 -07001846 avf->bbuf->skb = NULL;
Bob Copeland8a63fac2010-09-17 12:45:07 +09001847out:
1848 return ret;
1849}
1850
1851/*
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001852 * Transmit a beacon frame at SWBA. Dynamic updates to the
1853 * frame contents are done as needed and the slot time is
1854 * also adjusted based on current state.
1855 *
Bob Copeland5faaff72010-07-13 11:32:40 -04001856 * This is called from software irq context (beacontq tasklets)
1857 * or user context from ath5k_beacon_config.
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001858 */
1859static void
1860ath5k_beacon_send(struct ath5k_softc *sc)
1861{
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001862 struct ath5k_hw *ah = sc->ah;
Ben Greearb1ae1ed2010-09-30 12:22:58 -07001863 struct ieee80211_vif *vif;
1864 struct ath5k_vif *avf;
1865 struct ath5k_buf *bf;
Bob Copelandcec8db22009-07-04 12:59:51 -04001866 struct sk_buff *skb;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001867
Bruno Randolfbe9b7252008-01-23 10:27:51 +09001868 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON, "in beacon_send\n");
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001869
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001870 /*
1871 * Check if the previous beacon has gone out. If
Bob Copelanda180a132010-08-15 13:03:12 -04001872 * not, don't don't try to post another: skip this
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001873 * period and wait for the next. Missed beacons
1874 * indicate a problem and should not occur. If we
1875 * miss too many consecutive beacons reset the device.
1876 */
1877 if (unlikely(ath5k_hw_num_tx_pending(ah, sc->bhalq) != 0)) {
1878 sc->bmisscount++;
Bruno Randolfbe9b7252008-01-23 10:27:51 +09001879 ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001880 "missed %u consecutive beacons\n", sc->bmisscount);
Nick Kossifidis428cbd42009-04-30 15:55:47 -04001881 if (sc->bmisscount > 10) { /* NB: 10 is a guess */
Bruno Randolfbe9b7252008-01-23 10:27:51 +09001882 ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001883 "stuck beacon time (%u missed)\n",
1884 sc->bmisscount);
Bruno Randolf8d67a032010-06-16 19:11:12 +09001885 ATH5K_DBG(sc, ATH5K_DEBUG_RESET,
1886 "stuck beacon, resetting\n");
Bob Copeland5faaff72010-07-13 11:32:40 -04001887 ieee80211_queue_work(sc->hw, &sc->reset_work);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001888 }
1889 return;
1890 }
1891 if (unlikely(sc->bmisscount != 0)) {
Bruno Randolfbe9b7252008-01-23 10:27:51 +09001892 ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001893 "resume beacon xmit after %u misses\n",
1894 sc->bmisscount);
1895 sc->bmisscount = 0;
1896 }
1897
Ben Greearb1ae1ed2010-09-30 12:22:58 -07001898 if (sc->opmode == NL80211_IFTYPE_AP && sc->num_ap_vifs > 1) {
1899 u64 tsf = ath5k_hw_get_tsf64(ah);
1900 u32 tsftu = TSF_TO_TU(tsf);
1901 int slot = ((tsftu % sc->bintval) * ATH_BCBUF) / sc->bintval;
1902 vif = sc->bslot[(slot + 1) % ATH_BCBUF];
1903 ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
1904 "tsf %llx tsftu %x intval %u slot %u vif %p\n",
1905 (unsigned long long)tsf, tsftu, sc->bintval, slot, vif);
1906 } else /* only one interface */
1907 vif = sc->bslot[0];
1908
1909 if (!vif)
1910 return;
1911
1912 avf = (void *)vif->drv_priv;
1913 bf = avf->bbuf;
1914 if (unlikely(bf->skb == NULL || sc->opmode == NL80211_IFTYPE_STATION ||
1915 sc->opmode == NL80211_IFTYPE_MONITOR)) {
1916 ATH5K_WARN(sc, "bf=%p bf_skb=%p\n", bf, bf ? bf->skb : NULL);
1917 return;
1918 }
1919
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001920 /*
1921 * Stop any current dma and put the new frame on the queue.
1922 * This should never fail since we check above that no frames
1923 * are still pending on the queue.
1924 */
1925 if (unlikely(ath5k_hw_stop_tx_dma(ah, sc->bhalq))) {
Nick Kossifidis428cbd42009-04-30 15:55:47 -04001926 ATH5K_WARN(sc, "beacon queue %u didn't start/stop ?\n", sc->bhalq);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001927 /* NB: hw still stops DMA, so proceed */
1928 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001929
Bob Copeland1071db82009-05-18 10:59:52 -04001930 /* refresh the beacon for AP mode */
1931 if (sc->opmode == NL80211_IFTYPE_AP)
Ben Greearb1ae1ed2010-09-30 12:22:58 -07001932 ath5k_beacon_update(sc->hw, vif);
Bob Copeland1071db82009-05-18 10:59:52 -04001933
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001934 ath5k_hw_set_txdp(ah, sc->bhalq, bf->daddr);
1935 ath5k_hw_start_tx_dma(ah, sc->bhalq);
Bruno Randolfbe9b7252008-01-23 10:27:51 +09001936 ATH5K_DBG(sc, ATH5K_DEBUG_BEACON, "TXDP[%u] = %llx (%p)\n",
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001937 sc->bhalq, (unsigned long long)bf->daddr, bf->desc);
1938
Ben Greearb1ae1ed2010-09-30 12:22:58 -07001939 skb = ieee80211_get_buffered_bc(sc->hw, vif);
Bob Copelandcec8db22009-07-04 12:59:51 -04001940 while (skb) {
1941 ath5k_tx_queue(sc->hw, skb, sc->cabq);
Ben Greearb1ae1ed2010-09-30 12:22:58 -07001942 skb = ieee80211_get_buffered_bc(sc->hw, vif);
Bob Copelandcec8db22009-07-04 12:59:51 -04001943 }
1944
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001945 sc->bsent++;
1946}
1947
Bruno Randolf9804b982008-01-19 18:17:59 +09001948/**
1949 * ath5k_beacon_update_timers - update beacon timers
1950 *
1951 * @sc: struct ath5k_softc pointer we are operating on
1952 * @bc_tsf: the timestamp of the beacon. 0 to reset the TSF. -1 to perform a
1953 * beacon timer update based on the current HW TSF.
1954 *
1955 * Calculate the next target beacon transmit time (TBTT) based on the timestamp
1956 * of a received beacon or the current local hardware TSF and write it to the
1957 * beacon timer registers.
1958 *
1959 * This is called in a variety of situations, e.g. when a beacon is received,
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001960 * when a TSF update has been detected, but also when an new IBSS is created or
Bruno Randolf9804b982008-01-19 18:17:59 +09001961 * when we otherwise know we have to update the timers, but we keep it in this
1962 * function to have it all together in one place.
1963 */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001964static void
Bruno Randolf9804b982008-01-19 18:17:59 +09001965ath5k_beacon_update_timers(struct ath5k_softc *sc, u64 bc_tsf)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001966{
1967 struct ath5k_hw *ah = sc->ah;
Bruno Randolf9804b982008-01-19 18:17:59 +09001968 u32 nexttbtt, intval, hw_tu, bc_tu;
1969 u64 hw_tsf;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001970
1971 intval = sc->bintval & AR5K_BEACON_PERIOD;
Ben Greearb1ae1ed2010-09-30 12:22:58 -07001972 if (sc->opmode == NL80211_IFTYPE_AP && sc->num_ap_vifs > 1) {
1973 intval /= ATH_BCBUF; /* staggered multi-bss beacons */
1974 if (intval < 15)
1975 ATH5K_WARN(sc, "intval %u is too low, min 15\n",
1976 intval);
1977 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001978 if (WARN_ON(!intval))
1979 return;
1980
Bruno Randolf9804b982008-01-19 18:17:59 +09001981 /* beacon TSF converted to TU */
1982 bc_tu = TSF_TO_TU(bc_tsf);
1983
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001984 /* current TSF converted to TU */
Bruno Randolf9804b982008-01-19 18:17:59 +09001985 hw_tsf = ath5k_hw_get_tsf64(ah);
1986 hw_tu = TSF_TO_TU(hw_tsf);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001987
Bruno Randolf11f21df2010-09-27 12:22:26 +09001988#define FUDGE AR5K_TUNE_SW_BEACON_RESP + 3
1989 /* We use FUDGE to make sure the next TBTT is ahead of the current TU.
1990 * Since we later substract AR5K_TUNE_SW_BEACON_RESP (10) in the timer
1991 * configuration we need to make sure it is bigger than that. */
1992
Bruno Randolf9804b982008-01-19 18:17:59 +09001993 if (bc_tsf == -1) {
1994 /*
1995 * no beacons received, called internally.
1996 * just need to refresh timers based on HW TSF.
1997 */
1998 nexttbtt = roundup(hw_tu + FUDGE, intval);
1999 } else if (bc_tsf == 0) {
2000 /*
2001 * no beacon received, probably called by ath5k_reset_tsf().
2002 * reset TSF to start with 0.
2003 */
2004 nexttbtt = intval;
2005 intval |= AR5K_BEACON_RESET_TSF;
2006 } else if (bc_tsf > hw_tsf) {
2007 /*
2008 * beacon received, SW merge happend but HW TSF not yet updated.
2009 * not possible to reconfigure timers yet, but next time we
2010 * receive a beacon with the same BSSID, the hardware will
2011 * automatically update the TSF and then we need to reconfigure
2012 * the timers.
2013 */
2014 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
2015 "need to wait for HW TSF sync\n");
2016 return;
2017 } else {
2018 /*
2019 * most important case for beacon synchronization between STA.
2020 *
2021 * beacon received and HW TSF has been already updated by HW.
2022 * update next TBTT based on the TSF of the beacon, but make
2023 * sure it is ahead of our local TSF timer.
2024 */
2025 nexttbtt = bc_tu + roundup(hw_tu + FUDGE - bc_tu, intval);
2026 }
2027#undef FUDGE
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002028
Bruno Randolf036cd1e2008-01-19 18:18:21 +09002029 sc->nexttbtt = nexttbtt;
2030
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002031 intval |= AR5K_BEACON_ENA;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002032 ath5k_hw_init_beacon(ah, nexttbtt, intval);
Bruno Randolf9804b982008-01-19 18:17:59 +09002033
2034 /*
2035 * debugging output last in order to preserve the time critical aspect
2036 * of this function
2037 */
2038 if (bc_tsf == -1)
2039 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
2040 "reconfigured timers based on HW TSF\n");
2041 else if (bc_tsf == 0)
2042 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
2043 "reset HW TSF and timers\n");
2044 else
2045 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
2046 "updated timers based on beacon TSF\n");
2047
2048 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
David Miller04f93a82008-02-15 16:08:59 -08002049 "bc_tsf %llx hw_tsf %llx bc_tu %u hw_tu %u nexttbtt %u\n",
2050 (unsigned long long) bc_tsf,
2051 (unsigned long long) hw_tsf, bc_tu, hw_tu, nexttbtt);
Bruno Randolf9804b982008-01-19 18:17:59 +09002052 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON, "intval %u %s %s\n",
2053 intval & AR5K_BEACON_PERIOD,
2054 intval & AR5K_BEACON_ENA ? "AR5K_BEACON_ENA" : "",
2055 intval & AR5K_BEACON_RESET_TSF ? "AR5K_BEACON_RESET_TSF" : "");
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002056}
2057
Bruno Randolf036cd1e2008-01-19 18:18:21 +09002058/**
2059 * ath5k_beacon_config - Configure the beacon queues and interrupts
2060 *
2061 * @sc: struct ath5k_softc pointer we are operating on
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002062 *
Bruno Randolf036cd1e2008-01-19 18:18:21 +09002063 * In IBSS mode we use a self-linked tx descriptor if possible. We enable SWBA
Bruno Randolf6ba81c22008-03-05 18:36:26 +09002064 * interrupts to detect TSF updates only.
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002065 */
2066static void
2067ath5k_beacon_config(struct ath5k_softc *sc)
2068{
2069 struct ath5k_hw *ah = sc->ah;
Bob Copelandb5f03952009-02-15 12:06:10 -05002070 unsigned long flags;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002071
Bob Copeland21800492009-07-04 12:59:52 -04002072 spin_lock_irqsave(&sc->block, flags);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002073 sc->bmisscount = 0;
Jiri Slabydc1968e2008-07-23 13:17:34 +02002074 sc->imask &= ~(AR5K_INT_BMISS | AR5K_INT_SWBA);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002075
Bob Copeland21800492009-07-04 12:59:52 -04002076 if (sc->enable_beacon) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002077 /*
Bruno Randolf036cd1e2008-01-19 18:18:21 +09002078 * In IBSS mode we use a self-linked tx descriptor and let the
2079 * hardware send the beacons automatically. We have to load it
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002080 * only once here.
Bruno Randolf036cd1e2008-01-19 18:18:21 +09002081 * We use the SWBA interrupt only to keep track of the beacon
Bruno Randolf6ba81c22008-03-05 18:36:26 +09002082 * timers in order to detect automatic TSF updates.
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002083 */
2084 ath5k_beaconq_config(sc);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002085
Bruno Randolf036cd1e2008-01-19 18:18:21 +09002086 sc->imask |= AR5K_INT_SWBA;
2087
Jiri Slabyda966bc2008-10-12 22:54:10 +02002088 if (sc->opmode == NL80211_IFTYPE_ADHOC) {
Bob Copeland21800492009-07-04 12:59:52 -04002089 if (ath5k_hw_hasveol(ah))
Jiri Slabyda966bc2008-10-12 22:54:10 +02002090 ath5k_beacon_send(sc);
Jiri Slabyda966bc2008-10-12 22:54:10 +02002091 } else
2092 ath5k_beacon_update_timers(sc, -1);
Bob Copeland21800492009-07-04 12:59:52 -04002093 } else {
2094 ath5k_hw_stop_tx_dma(sc->ah, sc->bhalq);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002095 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002096
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03002097 ath5k_hw_set_imr(ah, sc->imask);
Bob Copeland21800492009-07-04 12:59:52 -04002098 mmiowb();
2099 spin_unlock_irqrestore(&sc->block, flags);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002100}
2101
Nick Kossifidis428cbd42009-04-30 15:55:47 -04002102static void ath5k_tasklet_beacon(unsigned long data)
2103{
2104 struct ath5k_softc *sc = (struct ath5k_softc *) data;
2105
2106 /*
2107 * Software beacon alert--time to send a beacon.
2108 *
2109 * In IBSS mode we use this interrupt just to
2110 * keep track of the next TBTT (target beacon
2111 * transmission time) in order to detect wether
2112 * automatic TSF updates happened.
2113 */
2114 if (sc->opmode == NL80211_IFTYPE_ADHOC) {
2115 /* XXX: only if VEOL suppported */
2116 u64 tsf = ath5k_hw_get_tsf64(sc->ah);
2117 sc->nexttbtt += sc->bintval;
2118 ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
2119 "SWBA nexttbtt: %x hw_tu: %x "
2120 "TSF: %llx\n",
2121 sc->nexttbtt,
2122 TSF_TO_TU(tsf),
2123 (unsigned long long) tsf);
2124 } else {
2125 spin_lock(&sc->block);
2126 ath5k_beacon_send(sc);
2127 spin_unlock(&sc->block);
2128 }
2129}
2130
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002131
2132/********************\
2133* Interrupt handling *
2134\********************/
2135
Bruno Randolf6a8a3f62010-03-25 14:49:19 +09002136static void
2137ath5k_intr_calibration_poll(struct ath5k_hw *ah)
2138{
Bruno Randolf2111ac02010-04-02 18:44:08 +09002139 if (time_is_before_eq_jiffies(ah->ah_cal_next_ani) &&
2140 !(ah->ah_cal_mask & AR5K_CALIBRATION_FULL)) {
2141 /* run ANI only when full calibration is not active */
2142 ah->ah_cal_next_ani = jiffies +
2143 msecs_to_jiffies(ATH5K_TUNE_CALIBRATION_INTERVAL_ANI);
2144 tasklet_schedule(&ah->ah_sc->ani_tasklet);
2145
2146 } else if (time_is_before_eq_jiffies(ah->ah_cal_next_full)) {
Bruno Randolf6a8a3f62010-03-25 14:49:19 +09002147 ah->ah_cal_next_full = jiffies +
2148 msecs_to_jiffies(ATH5K_TUNE_CALIBRATION_INTERVAL_FULL);
2149 tasklet_schedule(&ah->ah_sc->calib);
2150 }
2151 /* we could use SWI to generate enough interrupts to meet our
2152 * calibration interval requirements, if necessary:
2153 * AR5K_REG_ENABLE_BITS(ah, AR5K_CR, AR5K_CR_SWI); */
2154}
2155
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002156static irqreturn_t
2157ath5k_intr(int irq, void *dev_id)
2158{
2159 struct ath5k_softc *sc = dev_id;
2160 struct ath5k_hw *ah = sc->ah;
2161 enum ath5k_int status;
2162 unsigned int counter = 1000;
2163
2164 if (unlikely(test_bit(ATH_STAT_INVALID, sc->status) ||
2165 !ath5k_hw_is_intr_pending(ah)))
2166 return IRQ_NONE;
2167
2168 do {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002169 ath5k_hw_get_isr(ah, &status); /* NB: clears IRQ too */
2170 ATH5K_DBG(sc, ATH5K_DEBUG_INTR, "status 0x%x/0x%x\n",
2171 status, sc->imask);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002172 if (unlikely(status & AR5K_INT_FATAL)) {
2173 /*
2174 * Fatal errors are unrecoverable.
2175 * Typically these are caused by DMA errors.
2176 */
Bruno Randolf8d67a032010-06-16 19:11:12 +09002177 ATH5K_DBG(sc, ATH5K_DEBUG_RESET,
2178 "fatal int, resetting\n");
Bob Copeland5faaff72010-07-13 11:32:40 -04002179 ieee80211_queue_work(sc->hw, &sc->reset_work);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002180 } else if (unlikely(status & AR5K_INT_RXORN)) {
Bruno Randolf87d77c42010-04-12 16:38:52 +09002181 /*
2182 * Receive buffers are full. Either the bus is busy or
2183 * the CPU is not fast enough to process all received
2184 * frames.
2185 * Older chipsets need a reset to come out of this
2186 * condition, but we treat it as RX for newer chips.
2187 * We don't know exactly which versions need a reset -
2188 * this guess is copied from the HAL.
2189 */
2190 sc->stats.rxorn_intr++;
Bruno Randolf8d67a032010-06-16 19:11:12 +09002191 if (ah->ah_mac_srev < AR5K_SREV_AR5212) {
2192 ATH5K_DBG(sc, ATH5K_DEBUG_RESET,
2193 "rx overrun, resetting\n");
Bob Copeland5faaff72010-07-13 11:32:40 -04002194 ieee80211_queue_work(sc->hw, &sc->reset_work);
Bruno Randolf8d67a032010-06-16 19:11:12 +09002195 }
Bruno Randolf87d77c42010-04-12 16:38:52 +09002196 else
2197 tasklet_schedule(&sc->rxtq);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002198 } else {
2199 if (status & AR5K_INT_SWBA) {
Bob Copeland56d2ac72009-04-15 07:57:33 -04002200 tasklet_hi_schedule(&sc->beacontq);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002201 }
2202 if (status & AR5K_INT_RXEOL) {
2203 /*
2204 * NB: the hardware should re-read the link when
2205 * RXE bit is written, but it doesn't work at
2206 * least on older hardware revs.
2207 */
Bruno Randolfb3f194e2010-07-14 10:53:29 +09002208 sc->stats.rxeol_intr++;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002209 }
2210 if (status & AR5K_INT_TXURN) {
2211 /* bump tx trigger level */
2212 ath5k_hw_update_tx_triglevel(ah, true);
2213 }
Nick Kossifidis4c674c62008-10-26 20:40:25 +02002214 if (status & (AR5K_INT_RXOK | AR5K_INT_RXERR))
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002215 tasklet_schedule(&sc->rxtq);
Nick Kossifidis4c674c62008-10-26 20:40:25 +02002216 if (status & (AR5K_INT_TXOK | AR5K_INT_TXDESC
2217 | AR5K_INT_TXERR | AR5K_INT_TXEOL))
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002218 tasklet_schedule(&sc->txtq);
2219 if (status & AR5K_INT_BMISS) {
Nick Kossifidis1e3e6e82009-02-09 06:15:42 +02002220 /* TODO */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002221 }
2222 if (status & AR5K_INT_MIB) {
Bruno Randolf2111ac02010-04-02 18:44:08 +09002223 sc->stats.mib_intr++;
Bruno Randolf495391d2010-03-25 14:49:36 +09002224 ath5k_hw_update_mib_counters(ah);
Bruno Randolf2111ac02010-04-02 18:44:08 +09002225 ath5k_ani_mib_intr(ah);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002226 }
Tobias Doerffele6a3b612009-06-09 17:33:27 +02002227 if (status & AR5K_INT_GPIO)
Tobias Doerffele6a3b612009-06-09 17:33:27 +02002228 tasklet_schedule(&sc->rf_kill.toggleq);
Bob Copelanda6ae0712009-06-09 23:43:11 -04002229
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002230 }
Bob Copeland2516baa2009-04-27 22:18:10 -04002231 } while (ath5k_hw_is_intr_pending(ah) && --counter > 0);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002232
2233 if (unlikely(!counter))
2234 ATH5K_WARN(sc, "too many interrupts, giving up for now\n");
2235
Bruno Randolf6a8a3f62010-03-25 14:49:19 +09002236 ath5k_intr_calibration_poll(ah);
Nick Kossifidis6e2206622009-08-10 03:31:31 +03002237
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002238 return IRQ_HANDLED;
2239}
2240
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002241/*
2242 * Periodically recalibrate the PHY to account
2243 * for temperature/environment changes.
2244 */
2245static void
Nick Kossifidis6e2206622009-08-10 03:31:31 +03002246ath5k_tasklet_calibrate(unsigned long data)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002247{
2248 struct ath5k_softc *sc = (void *)data;
2249 struct ath5k_hw *ah = sc->ah;
2250
Nick Kossifidis6e2206622009-08-10 03:31:31 +03002251 /* Only full calibration for now */
Bruno Randolfe65e1d72010-03-25 14:49:09 +09002252 ah->ah_cal_mask |= AR5K_CALIBRATION_FULL;
Nick Kossifidis6e2206622009-08-10 03:31:31 +03002253
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002254 ATH5K_DBG(sc, ATH5K_DEBUG_CALIBRATE, "channel %u/%x\n",
Luis R. Rodriguez400ec452008-02-03 21:51:49 -05002255 ieee80211_frequency_to_channel(sc->curchan->center_freq),
2256 sc->curchan->hw_value);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002257
Nick Kossifidis6f3b4142009-02-09 06:03:41 +02002258 if (ath5k_hw_gainf_calibrate(ah) == AR5K_RFGAIN_NEED_CHANGE) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002259 /*
2260 * Rfgain is out of bounds, reset the chip
2261 * to load new gain values.
2262 */
2263 ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "calibration, resetting\n");
Bob Copeland5faaff72010-07-13 11:32:40 -04002264 ieee80211_queue_work(sc->hw, &sc->reset_work);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002265 }
2266 if (ath5k_hw_phy_calibrate(ah, sc->curchan))
2267 ATH5K_ERR(sc, "calibration of channel %u failed\n",
Luis R. Rodriguez400ec452008-02-03 21:51:49 -05002268 ieee80211_frequency_to_channel(
2269 sc->curchan->center_freq));
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002270
Bruno Randolf0e8e02d2010-05-19 10:31:05 +09002271 /* Noise floor calibration interrupts rx/tx path while I/Q calibration
Bruno Randolf651d9372010-09-17 11:36:46 +09002272 * doesn't.
2273 * TODO: We should stop TX here, so that it doesn't interfere.
2274 * Note that stopping the queues is not enough to stop TX! */
Bruno Randolfafe86282010-05-19 10:31:10 +09002275 if (time_is_before_eq_jiffies(ah->ah_cal_next_nf)) {
2276 ah->ah_cal_next_nf = jiffies +
2277 msecs_to_jiffies(ATH5K_TUNE_CALIBRATION_INTERVAL_NF);
Bruno Randolfafe86282010-05-19 10:31:10 +09002278 ath5k_hw_update_noise_floor(ah);
Bruno Randolfafe86282010-05-19 10:31:10 +09002279 }
Nick Kossifidis6e2206622009-08-10 03:31:31 +03002280
Bruno Randolfe65e1d72010-03-25 14:49:09 +09002281 ah->ah_cal_mask &= ~AR5K_CALIBRATION_FULL;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002282}
2283
2284
Bruno Randolf2111ac02010-04-02 18:44:08 +09002285static void
2286ath5k_tasklet_ani(unsigned long data)
2287{
2288 struct ath5k_softc *sc = (void *)data;
2289 struct ath5k_hw *ah = sc->ah;
2290
2291 ah->ah_cal_mask |= AR5K_CALIBRATION_ANI;
2292 ath5k_ani_calibration(ah);
2293 ah->ah_cal_mask &= ~AR5K_CALIBRATION_ANI;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002294}
2295
2296
Bruno Randolf4edd7612010-09-17 11:36:56 +09002297static void
2298ath5k_tx_complete_poll_work(struct work_struct *work)
2299{
2300 struct ath5k_softc *sc = container_of(work, struct ath5k_softc,
2301 tx_complete_work.work);
2302 struct ath5k_txq *txq;
2303 int i;
2304 bool needreset = false;
2305
2306 for (i = 0; i < ARRAY_SIZE(sc->txqs); i++) {
2307 if (sc->txqs[i].setup) {
2308 txq = &sc->txqs[i];
2309 spin_lock_bh(&txq->lock);
Bruno Randolf23413292010-09-17 11:37:07 +09002310 if (txq->txq_len > 1) {
Bruno Randolf4edd7612010-09-17 11:36:56 +09002311 if (txq->txq_poll_mark) {
2312 ATH5K_DBG(sc, ATH5K_DEBUG_XMIT,
2313 "TX queue stuck %d\n",
2314 txq->qnum);
2315 needreset = true;
Bruno Randolf923e5b32010-09-17 11:37:02 +09002316 txq->txq_stuck++;
Bruno Randolf4edd7612010-09-17 11:36:56 +09002317 spin_unlock_bh(&txq->lock);
2318 break;
2319 } else {
2320 txq->txq_poll_mark = true;
2321 }
2322 }
2323 spin_unlock_bh(&txq->lock);
2324 }
2325 }
2326
2327 if (needreset) {
2328 ATH5K_DBG(sc, ATH5K_DEBUG_RESET,
2329 "TX queues stuck, resetting\n");
2330 ath5k_reset(sc, sc->curchan);
2331 }
2332
2333 ieee80211_queue_delayed_work(sc->hw, &sc->tx_complete_work,
2334 msecs_to_jiffies(ATH5K_TX_COMPLETE_POLL_INT));
2335}
2336
2337
Bob Copeland8a63fac2010-09-17 12:45:07 +09002338/*************************\
2339* Initialization routines *
2340\*************************/
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002341
2342static int
Bob Copeland8a63fac2010-09-17 12:45:07 +09002343ath5k_stop_locked(struct ath5k_softc *sc)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002344{
Bob Copeland8a63fac2010-09-17 12:45:07 +09002345 struct ath5k_hw *ah = sc->ah;
Bob Copelandcec8db22009-07-04 12:59:51 -04002346
Bob Copeland8a63fac2010-09-17 12:45:07 +09002347 ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "invalid %u\n",
2348 test_bit(ATH_STAT_INVALID, sc->status));
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002349
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002350 /*
Bob Copeland8a63fac2010-09-17 12:45:07 +09002351 * Shutdown the hardware and driver:
2352 * stop output from above
2353 * disable interrupts
2354 * turn off timers
2355 * turn off the radio
2356 * clear transmit machinery
2357 * clear receive machinery
2358 * drain and release tx queues
2359 * reclaim beacon resources
2360 * power down hardware
2361 *
2362 * Note that some of this work is not possible if the
2363 * hardware is gone (invalid).
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002364 */
Bob Copeland8a63fac2010-09-17 12:45:07 +09002365 ieee80211_stop_queues(sc->hw);
2366
2367 if (!test_bit(ATH_STAT_INVALID, sc->status)) {
2368 ath5k_led_off(sc);
2369 ath5k_hw_set_imr(ah, 0);
2370 synchronize_irq(sc->pdev->irq);
Bob Copeland8a63fac2010-09-17 12:45:07 +09002371 ath5k_rx_stop(sc);
Nick Kossifidis80dac9e2010-11-23 20:45:38 +02002372 ath5k_hw_dma_stop(ah);
2373 ath5k_drain_tx_buffs(sc);
Bob Copeland8a63fac2010-09-17 12:45:07 +09002374 ath5k_hw_phy_disable(ah);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002375 }
2376
Bob Copeland8a63fac2010-09-17 12:45:07 +09002377 return 0;
2378}
2379
2380static int
2381ath5k_init(struct ath5k_softc *sc)
2382{
2383 struct ath5k_hw *ah = sc->ah;
2384 struct ath_common *common = ath5k_hw_common(ah);
2385 int ret, i;
2386
2387 mutex_lock(&sc->lock);
2388
2389 ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "mode %d\n", sc->opmode);
2390
2391 /*
2392 * Stop anything previously setup. This is safe
2393 * no matter this is the first time through or not.
2394 */
2395 ath5k_stop_locked(sc);
2396
2397 /*
2398 * The basic interface to setting the hardware in a good
2399 * state is ``reset''. On return the hardware is known to
2400 * be powered up and with interrupts disabled. This must
2401 * be followed by initialization of the appropriate bits
2402 * and then setup of the interrupt mask.
2403 */
2404 sc->curchan = sc->hw->conf.channel;
2405 sc->curband = &sc->sbands[sc->curchan->band];
2406 sc->imask = AR5K_INT_RXOK | AR5K_INT_RXERR | AR5K_INT_RXEOL |
2407 AR5K_INT_RXORN | AR5K_INT_TXDESC | AR5K_INT_TXEOL |
2408 AR5K_INT_FATAL | AR5K_INT_GLOBAL | AR5K_INT_MIB;
2409
2410 ret = ath5k_reset(sc, NULL);
2411 if (ret)
2412 goto done;
2413
2414 ath5k_rfkill_hw_start(ah);
2415
2416 /*
2417 * Reset the key cache since some parts do not reset the
2418 * contents on initial power up or resume from suspend.
2419 */
2420 for (i = 0; i < common->keymax; i++)
2421 ath_hw_keyreset(common, (u16) i);
2422
2423 ath5k_hw_set_ack_bitrate_high(ah, true);
Ben Greearb1ae1ed2010-09-30 12:22:58 -07002424
2425 for (i = 0; i < ARRAY_SIZE(sc->bslot); i++)
2426 sc->bslot[i] = NULL;
2427
Bob Copeland8a63fac2010-09-17 12:45:07 +09002428 ret = 0;
2429done:
2430 mmiowb();
2431 mutex_unlock(&sc->lock);
Bruno Randolf4edd7612010-09-17 11:36:56 +09002432
2433 ieee80211_queue_delayed_work(sc->hw, &sc->tx_complete_work,
2434 msecs_to_jiffies(ATH5K_TX_COMPLETE_POLL_INT));
2435
Bob Copeland8a63fac2010-09-17 12:45:07 +09002436 return ret;
2437}
2438
2439static void stop_tasklets(struct ath5k_softc *sc)
2440{
2441 tasklet_kill(&sc->rxtq);
2442 tasklet_kill(&sc->txtq);
2443 tasklet_kill(&sc->calib);
2444 tasklet_kill(&sc->beacontq);
2445 tasklet_kill(&sc->ani_tasklet);
2446}
2447
2448/*
2449 * Stop the device, grabbing the top-level lock to protect
2450 * against concurrent entry through ath5k_init (which can happen
2451 * if another thread does a system call and the thread doing the
2452 * stop is preempted).
2453 */
2454static int
2455ath5k_stop_hw(struct ath5k_softc *sc)
2456{
2457 int ret;
2458
2459 mutex_lock(&sc->lock);
2460 ret = ath5k_stop_locked(sc);
2461 if (ret == 0 && !test_bit(ATH_STAT_INVALID, sc->status)) {
2462 /*
2463 * Don't set the card in full sleep mode!
2464 *
2465 * a) When the device is in this state it must be carefully
2466 * woken up or references to registers in the PCI clock
2467 * domain may freeze the bus (and system). This varies
2468 * by chip and is mostly an issue with newer parts
2469 * (madwifi sources mentioned srev >= 0x78) that go to
2470 * sleep more quickly.
2471 *
2472 * b) On older chips full sleep results a weird behaviour
2473 * during wakeup. I tested various cards with srev < 0x78
2474 * and they don't wake up after module reload, a second
2475 * module reload is needed to bring the card up again.
2476 *
2477 * Until we figure out what's going on don't enable
2478 * full chip reset on any chip (this is what Legacy HAL
2479 * and Sam's HAL do anyway). Instead Perform a full reset
2480 * on the device (same as initial state after attach) and
2481 * leave it idle (keep MAC/BB on warm reset) */
2482 ret = ath5k_hw_on_hold(sc->ah);
2483
2484 ATH5K_DBG(sc, ATH5K_DEBUG_RESET,
2485 "putting device to sleep\n");
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002486 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002487
Bob Copeland8a63fac2010-09-17 12:45:07 +09002488 mmiowb();
2489 mutex_unlock(&sc->lock);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002490
Bob Copeland8a63fac2010-09-17 12:45:07 +09002491 stop_tasklets(sc);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002492
Bruno Randolf4edd7612010-09-17 11:36:56 +09002493 cancel_delayed_work_sync(&sc->tx_complete_work);
2494
Bob Copeland8a63fac2010-09-17 12:45:07 +09002495 ath5k_rfkill_hw_stop(sc->ah);
2496
2497 return ret;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002498}
2499
Bob Copeland209d889b2009-05-07 08:09:08 -04002500/*
2501 * Reset the hardware. If chan is not NULL, then also pause rx/tx
2502 * and change to the given channel.
Bob Copeland5faaff72010-07-13 11:32:40 -04002503 *
2504 * This should be called with sc->lock.
Bob Copeland209d889b2009-05-07 08:09:08 -04002505 */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002506static int
Bob Copeland209d889b2009-05-07 08:09:08 -04002507ath5k_reset(struct ath5k_softc *sc, struct ieee80211_channel *chan)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002508{
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002509 struct ath5k_hw *ah = sc->ah;
2510 int ret;
2511
2512 ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "resetting\n");
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002513
Bob Copeland450464d2010-07-13 11:32:41 -04002514 ath5k_hw_set_imr(ah, 0);
2515 synchronize_irq(sc->pdev->irq);
2516 stop_tasklets(sc);
2517
Bob Copeland209d889b2009-05-07 08:09:08 -04002518 if (chan) {
Nick Kossifidis80dac9e2010-11-23 20:45:38 +02002519 ath5k_drain_tx_buffs(sc);
Bob Copeland209d889b2009-05-07 08:09:08 -04002520
2521 sc->curchan = chan;
2522 sc->curband = &sc->sbands[chan->band];
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002523 }
Bob Copeland33554432009-07-04 21:03:13 -04002524 ret = ath5k_hw_reset(ah, sc->opmode, sc->curchan, chan != NULL);
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002525 if (ret) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002526 ATH5K_ERR(sc, "can't reset hardware (%d)\n", ret);
2527 goto err;
2528 }
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002529
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002530 ret = ath5k_rx_start(sc);
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002531 if (ret) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002532 ATH5K_ERR(sc, "can't start recv logic\n");
2533 goto err;
2534 }
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002535
Bruno Randolf2111ac02010-04-02 18:44:08 +09002536 ath5k_ani_init(ah, ah->ah_sc->ani_state.ani_mode);
2537
Bruno Randolfac559522010-05-19 10:30:55 +09002538 ah->ah_cal_next_full = jiffies;
2539 ah->ah_cal_next_ani = jiffies;
Bruno Randolfafe86282010-05-19 10:31:10 +09002540 ah->ah_cal_next_nf = jiffies;
Bruno Randolfeef39be2010-11-16 10:58:43 +09002541 ewma_init(&ah->ah_beacon_rssi_avg, 1000, 8);
Bruno Randolfafe86282010-05-19 10:31:10 +09002542
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002543 /*
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002544 * Change channels and update the h/w rate map if we're switching;
2545 * e.g. 11a to 11b/g.
2546 *
2547 * We may be doing a reset in response to an ioctl that changes the
2548 * channel so update any state that might change as a result.
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002549 *
2550 * XXX needed?
2551 */
2552/* ath5k_chan_change(sc, c); */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002553
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002554 ath5k_beacon_config(sc);
2555 /* intrs are enabled by ath5k_beacon_config */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002556
Bruno Randolf397f3852010-05-19 10:30:49 +09002557 ieee80211_wake_queues(sc->hw);
2558
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002559 return 0;
2560err:
2561 return ret;
2562}
2563
Bob Copeland5faaff72010-07-13 11:32:40 -04002564static void ath5k_reset_work(struct work_struct *work)
2565{
2566 struct ath5k_softc *sc = container_of(work, struct ath5k_softc,
2567 reset_work);
2568
2569 mutex_lock(&sc->lock);
2570 ath5k_reset(sc, sc->curchan);
2571 mutex_unlock(&sc->lock);
2572}
2573
Bob Copeland8a63fac2010-09-17 12:45:07 +09002574static int
2575ath5k_attach(struct pci_dev *pdev, struct ieee80211_hw *hw)
2576{
2577 struct ath5k_softc *sc = hw->priv;
2578 struct ath5k_hw *ah = sc->ah;
2579 struct ath_regulatory *regulatory = ath5k_hw_regulatory(ah);
Bruno Randolf925e0b02010-09-17 11:36:35 +09002580 struct ath5k_txq *txq;
Bob Copeland8a63fac2010-09-17 12:45:07 +09002581 u8 mac[ETH_ALEN] = {};
2582 int ret;
2583
2584 ATH5K_DBG(sc, ATH5K_DEBUG_ANY, "devid 0x%x\n", pdev->device);
2585
2586 /*
2587 * Check if the MAC has multi-rate retry support.
2588 * We do this by trying to setup a fake extended
2589 * descriptor. MACs that don't have support will
2590 * return false w/o doing anything. MACs that do
2591 * support it will return true w/o doing anything.
2592 */
2593 ret = ath5k_hw_setup_mrr_tx_desc(ah, NULL, 0, 0, 0, 0, 0, 0);
2594
2595 if (ret < 0)
2596 goto err;
2597 if (ret > 0)
2598 __set_bit(ATH_STAT_MRRETRY, sc->status);
2599
2600 /*
2601 * Collect the channel list. The 802.11 layer
2602 * is resposible for filtering this list based
2603 * on settings like the phy mode and regulatory
2604 * domain restrictions.
2605 */
2606 ret = ath5k_setup_bands(hw);
2607 if (ret) {
2608 ATH5K_ERR(sc, "can't get channels\n");
2609 goto err;
2610 }
2611
2612 /* NB: setup here so ath5k_rate_update is happy */
2613 if (test_bit(AR5K_MODE_11A, ah->ah_modes))
2614 ath5k_setcurmode(sc, AR5K_MODE_11A);
2615 else
2616 ath5k_setcurmode(sc, AR5K_MODE_11B);
2617
2618 /*
2619 * Allocate tx+rx descriptors and populate the lists.
2620 */
2621 ret = ath5k_desc_alloc(sc, pdev);
2622 if (ret) {
2623 ATH5K_ERR(sc, "can't allocate descriptors\n");
2624 goto err;
2625 }
2626
2627 /*
2628 * Allocate hardware transmit queues: one queue for
2629 * beacon frames and one data queue for each QoS
2630 * priority. Note that hw functions handle resetting
2631 * these queues at the needed time.
2632 */
2633 ret = ath5k_beaconq_setup(ah);
2634 if (ret < 0) {
2635 ATH5K_ERR(sc, "can't setup a beacon xmit queue\n");
2636 goto err_desc;
2637 }
2638 sc->bhalq = ret;
2639 sc->cabq = ath5k_txq_setup(sc, AR5K_TX_QUEUE_CAB, 0);
2640 if (IS_ERR(sc->cabq)) {
2641 ATH5K_ERR(sc, "can't setup cab queue\n");
2642 ret = PTR_ERR(sc->cabq);
2643 goto err_bhal;
2644 }
2645
Bruno Randolf925e0b02010-09-17 11:36:35 +09002646 /* This order matches mac80211's queue priority, so we can
2647 * directly use the mac80211 queue number without any mapping */
2648 txq = ath5k_txq_setup(sc, AR5K_TX_QUEUE_DATA, AR5K_WME_AC_VO);
2649 if (IS_ERR(txq)) {
Bob Copeland8a63fac2010-09-17 12:45:07 +09002650 ATH5K_ERR(sc, "can't setup xmit queue\n");
Bruno Randolf925e0b02010-09-17 11:36:35 +09002651 ret = PTR_ERR(txq);
Bob Copeland8a63fac2010-09-17 12:45:07 +09002652 goto err_queues;
2653 }
Bruno Randolf925e0b02010-09-17 11:36:35 +09002654 txq = ath5k_txq_setup(sc, AR5K_TX_QUEUE_DATA, AR5K_WME_AC_VI);
2655 if (IS_ERR(txq)) {
2656 ATH5K_ERR(sc, "can't setup xmit queue\n");
2657 ret = PTR_ERR(txq);
2658 goto err_queues;
2659 }
2660 txq = ath5k_txq_setup(sc, AR5K_TX_QUEUE_DATA, AR5K_WME_AC_BE);
2661 if (IS_ERR(txq)) {
2662 ATH5K_ERR(sc, "can't setup xmit queue\n");
2663 ret = PTR_ERR(txq);
2664 goto err_queues;
2665 }
2666 txq = ath5k_txq_setup(sc, AR5K_TX_QUEUE_DATA, AR5K_WME_AC_BK);
2667 if (IS_ERR(txq)) {
2668 ATH5K_ERR(sc, "can't setup xmit queue\n");
2669 ret = PTR_ERR(txq);
2670 goto err_queues;
2671 }
2672 hw->queues = 4;
Bob Copeland8a63fac2010-09-17 12:45:07 +09002673
2674 tasklet_init(&sc->rxtq, ath5k_tasklet_rx, (unsigned long)sc);
2675 tasklet_init(&sc->txtq, ath5k_tasklet_tx, (unsigned long)sc);
2676 tasklet_init(&sc->calib, ath5k_tasklet_calibrate, (unsigned long)sc);
2677 tasklet_init(&sc->beacontq, ath5k_tasklet_beacon, (unsigned long)sc);
2678 tasklet_init(&sc->ani_tasklet, ath5k_tasklet_ani, (unsigned long)sc);
2679
2680 INIT_WORK(&sc->reset_work, ath5k_reset_work);
Bruno Randolf4edd7612010-09-17 11:36:56 +09002681 INIT_DELAYED_WORK(&sc->tx_complete_work, ath5k_tx_complete_poll_work);
Bob Copeland8a63fac2010-09-17 12:45:07 +09002682
2683 ret = ath5k_eeprom_read_mac(ah, mac);
2684 if (ret) {
2685 ATH5K_ERR(sc, "unable to read address from EEPROM: 0x%04x\n",
2686 sc->pdev->device);
2687 goto err_queues;
2688 }
2689
2690 SET_IEEE80211_PERM_ADDR(hw, mac);
Ben Greearb1ae1ed2010-09-30 12:22:58 -07002691 memcpy(&sc->lladdr, mac, ETH_ALEN);
Bob Copeland8a63fac2010-09-17 12:45:07 +09002692 /* All MAC address bits matter for ACKs */
Ben Greear62c58fb2010-10-08 12:01:15 -07002693 ath5k_update_bssid_mask_and_opmode(sc, NULL);
Bob Copeland8a63fac2010-09-17 12:45:07 +09002694
2695 regulatory->current_rd = ah->ah_capabilities.cap_eeprom.ee_regdomain;
2696 ret = ath_regd_init(regulatory, hw->wiphy, ath5k_reg_notifier);
2697 if (ret) {
2698 ATH5K_ERR(sc, "can't initialize regulatory system\n");
2699 goto err_queues;
2700 }
2701
2702 ret = ieee80211_register_hw(hw);
2703 if (ret) {
2704 ATH5K_ERR(sc, "can't register ieee80211 hw\n");
2705 goto err_queues;
2706 }
2707
2708 if (!ath_is_world_regd(regulatory))
2709 regulatory_hint(hw->wiphy, regulatory->alpha2);
2710
2711 ath5k_init_leds(sc);
2712
2713 ath5k_sysfs_register(sc);
2714
2715 return 0;
2716err_queues:
2717 ath5k_txq_release(sc);
2718err_bhal:
2719 ath5k_hw_release_tx_queue(ah, sc->bhalq);
2720err_desc:
2721 ath5k_desc_free(sc, pdev);
2722err:
2723 return ret;
2724}
2725
2726static void
2727ath5k_detach(struct pci_dev *pdev, struct ieee80211_hw *hw)
2728{
2729 struct ath5k_softc *sc = hw->priv;
2730
2731 /*
2732 * NB: the order of these is important:
2733 * o call the 802.11 layer before detaching ath5k_hw to
2734 * ensure callbacks into the driver to delete global
2735 * key cache entries can be handled
2736 * o reclaim the tx queue data structures after calling
2737 * the 802.11 layer as we'll get called back to reclaim
2738 * node state and potentially want to use them
2739 * o to cleanup the tx queues the hal is called, so detach
2740 * it last
2741 * XXX: ??? detach ath5k_hw ???
2742 * Other than that, it's straightforward...
2743 */
2744 ieee80211_unregister_hw(hw);
2745 ath5k_desc_free(sc, pdev);
2746 ath5k_txq_release(sc);
2747 ath5k_hw_release_tx_queue(sc->ah, sc->bhalq);
2748 ath5k_unregister_leds(sc);
2749
2750 ath5k_sysfs_unregister(sc);
2751 /*
2752 * NB: can't reclaim these until after ieee80211_ifdetach
2753 * returns because we'll get called back to reclaim node
2754 * state and potentially want to use them.
2755 */
2756}
2757
2758/********************\
2759* Mac80211 functions *
2760\********************/
2761
2762static int
2763ath5k_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
2764{
2765 struct ath5k_softc *sc = hw->priv;
Bruno Randolf925e0b02010-09-17 11:36:35 +09002766 u16 qnum = skb_get_queue_mapping(skb);
Bob Copeland8a63fac2010-09-17 12:45:07 +09002767
Bruno Randolf925e0b02010-09-17 11:36:35 +09002768 if (WARN_ON(qnum >= sc->ah->ah_capabilities.cap_queues.q_tx_num)) {
2769 dev_kfree_skb_any(skb);
2770 return 0;
2771 }
2772
2773 return ath5k_tx_queue(hw, skb, &sc->txqs[qnum]);
Bob Copeland8a63fac2010-09-17 12:45:07 +09002774}
2775
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002776static int ath5k_start(struct ieee80211_hw *hw)
2777{
Bob Copelandbb2beca2009-01-19 11:20:54 -05002778 return ath5k_init(hw->priv);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002779}
2780
2781static void ath5k_stop(struct ieee80211_hw *hw)
2782{
Bob Copelandbb2beca2009-01-19 11:20:54 -05002783 ath5k_stop_hw(hw->priv);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002784}
2785
2786static int ath5k_add_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +01002787 struct ieee80211_vif *vif)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002788{
2789 struct ath5k_softc *sc = hw->priv;
2790 int ret;
Ben Greearb1ae1ed2010-09-30 12:22:58 -07002791 struct ath5k_vif *avf = (void *)vif->drv_priv;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002792
2793 mutex_lock(&sc->lock);
Ben Greearb1ae1ed2010-09-30 12:22:58 -07002794
2795 if ((vif->type == NL80211_IFTYPE_AP ||
2796 vif->type == NL80211_IFTYPE_ADHOC)
2797 && (sc->num_ap_vifs + sc->num_adhoc_vifs) >= ATH_BCBUF) {
2798 ret = -ELNRNG;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002799 goto end;
2800 }
2801
Ben Greearb1ae1ed2010-09-30 12:22:58 -07002802 /* Don't allow other interfaces if one ad-hoc is configured.
2803 * TODO: Fix the problems with ad-hoc and multiple other interfaces.
2804 * We would need to operate the HW in ad-hoc mode to allow TSF updates
2805 * for the IBSS, but this breaks with additional AP or STA interfaces
2806 * at the moment. */
2807 if (sc->num_adhoc_vifs ||
2808 (sc->nvifs && vif->type == NL80211_IFTYPE_ADHOC)) {
2809 ATH5K_ERR(sc, "Only one single ad-hoc interface is allowed.\n");
2810 ret = -ELNRNG;
2811 goto end;
2812 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002813
Johannes Berg1ed32e42009-12-23 13:15:45 +01002814 switch (vif->type) {
Jiri Slabyda966bc2008-10-12 22:54:10 +02002815 case NL80211_IFTYPE_AP:
Johannes Berg05c914f2008-09-11 00:01:58 +02002816 case NL80211_IFTYPE_STATION:
2817 case NL80211_IFTYPE_ADHOC:
Andrey Yurovskyb706e652008-10-13 18:23:07 -07002818 case NL80211_IFTYPE_MESH_POINT:
Ben Greearb1ae1ed2010-09-30 12:22:58 -07002819 avf->opmode = vif->type;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002820 break;
2821 default:
2822 ret = -EOPNOTSUPP;
2823 goto end;
2824 }
Jiri Slaby67d2e2d2008-08-18 21:45:28 +02002825
Ben Greearb1ae1ed2010-09-30 12:22:58 -07002826 sc->nvifs++;
2827 ATH5K_DBG(sc, ATH5K_DEBUG_MODE, "add interface mode %d\n", avf->opmode);
Bruno Randolfccfe5552010-03-09 16:55:38 +09002828
Ben Greearb1ae1ed2010-09-30 12:22:58 -07002829 /* Assign the vap/adhoc to a beacon xmit slot. */
2830 if ((avf->opmode == NL80211_IFTYPE_AP) ||
2831 (avf->opmode == NL80211_IFTYPE_ADHOC)) {
2832 int slot;
2833
2834 WARN_ON(list_empty(&sc->bcbuf));
2835 avf->bbuf = list_first_entry(&sc->bcbuf, struct ath5k_buf,
2836 list);
2837 list_del(&avf->bbuf->list);
2838
2839 avf->bslot = 0;
2840 for (slot = 0; slot < ATH_BCBUF; slot++) {
2841 if (!sc->bslot[slot]) {
2842 avf->bslot = slot;
2843 break;
2844 }
2845 }
2846 BUG_ON(sc->bslot[avf->bslot] != NULL);
2847 sc->bslot[avf->bslot] = vif;
2848 if (avf->opmode == NL80211_IFTYPE_AP)
2849 sc->num_ap_vifs++;
2850 else
2851 sc->num_adhoc_vifs++;
2852 }
2853
Ben Greearb1ae1ed2010-09-30 12:22:58 -07002854 /* Any MAC address is fine, all others are included through the
2855 * filter.
2856 */
2857 memcpy(&sc->lladdr, vif->addr, ETH_ALEN);
Johannes Berg1ed32e42009-12-23 13:15:45 +01002858 ath5k_hw_set_lladdr(sc->ah, vif->addr);
Ben Greearb1ae1ed2010-09-30 12:22:58 -07002859
2860 memcpy(&avf->lladdr, vif->addr, ETH_ALEN);
2861
2862 ath5k_mode_setup(sc, vif);
Jiri Slaby67d2e2d2008-08-18 21:45:28 +02002863
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002864 ret = 0;
2865end:
2866 mutex_unlock(&sc->lock);
2867 return ret;
2868}
2869
2870static void
2871ath5k_remove_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +01002872 struct ieee80211_vif *vif)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002873{
2874 struct ath5k_softc *sc = hw->priv;
Ben Greearb1ae1ed2010-09-30 12:22:58 -07002875 struct ath5k_vif *avf = (void *)vif->drv_priv;
2876 unsigned int i;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002877
2878 mutex_lock(&sc->lock);
Ben Greearb1ae1ed2010-09-30 12:22:58 -07002879 sc->nvifs--;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002880
Ben Greearb1ae1ed2010-09-30 12:22:58 -07002881 if (avf->bbuf) {
2882 ath5k_txbuf_free_skb(sc, avf->bbuf);
2883 list_add_tail(&avf->bbuf->list, &sc->bcbuf);
2884 for (i = 0; i < ATH_BCBUF; i++) {
2885 if (sc->bslot[i] == vif) {
2886 sc->bslot[i] = NULL;
2887 break;
2888 }
2889 }
2890 avf->bbuf = NULL;
2891 }
2892 if (avf->opmode == NL80211_IFTYPE_AP)
2893 sc->num_ap_vifs--;
2894 else if (avf->opmode == NL80211_IFTYPE_ADHOC)
2895 sc->num_adhoc_vifs--;
2896
Ben Greear62c58fb2010-10-08 12:01:15 -07002897 ath5k_update_bssid_mask_and_opmode(sc, NULL);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002898 mutex_unlock(&sc->lock);
2899}
2900
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -05002901/*
2902 * TODO: Phy disable/diversity etc
2903 */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002904static int
Johannes Berge8975582008-10-09 12:18:51 +02002905ath5k_config(struct ieee80211_hw *hw, u32 changed)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002906{
2907 struct ath5k_softc *sc = hw->priv;
Nick Kossifidisa0823812009-04-30 15:55:44 -04002908 struct ath5k_hw *ah = sc->ah;
Johannes Berge8975582008-10-09 12:18:51 +02002909 struct ieee80211_conf *conf = &hw->conf;
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04002910 int ret = 0;
Bob Copelandbe009372009-01-22 08:44:16 -05002911
2912 mutex_lock(&sc->lock);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002913
Joerg Alberte30eb4a2009-08-05 01:52:07 +02002914 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
2915 ret = ath5k_chan_set(sc, conf->channel);
2916 if (ret < 0)
2917 goto unlock;
2918 }
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04002919
Nick Kossifidisa0823812009-04-30 15:55:44 -04002920 if ((changed & IEEE80211_CONF_CHANGE_POWER) &&
2921 (sc->power_level != conf->power_level)) {
2922 sc->power_level = conf->power_level;
2923
2924 /* Half dB steps */
2925 ath5k_hw_set_txpower_limit(ah, (conf->power_level * 2));
2926 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002927
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04002928 /* TODO:
2929 * 1) Move this on config_interface and handle each case
2930 * separately eg. when we have only one STA vif, use
2931 * AR5K_ANTMODE_SINGLE_AP
2932 *
2933 * 2) Allow the user to change antenna mode eg. when only
2934 * one antenna is present
2935 *
2936 * 3) Allow the user to set default/tx antenna when possible
2937 *
2938 * 4) Default mode should handle 90% of the cases, together
2939 * with fixed a/b and single AP modes we should be able to
2940 * handle 99%. Sectored modes are extreme cases and i still
2941 * haven't found a usage for them. If we decide to support them,
2942 * then we must allow the user to set how many tx antennas we
2943 * have available
2944 */
Bruno Randolfcaec9112010-03-09 16:55:28 +09002945 ath5k_hw_set_antenna_mode(ah, ah->ah_ant_mode);
Bob Copelandbe009372009-01-22 08:44:16 -05002946
John W. Linville55aa4e02009-05-25 21:28:47 +02002947unlock:
Bob Copelandbe009372009-01-22 08:44:16 -05002948 mutex_unlock(&sc->lock);
John W. Linville55aa4e02009-05-25 21:28:47 +02002949 return ret;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002950}
2951
Johannes Berg3ac64be2009-08-17 16:16:53 +02002952static u64 ath5k_prepare_multicast(struct ieee80211_hw *hw,
Jiri Pirko22bedad32010-04-01 21:22:57 +00002953 struct netdev_hw_addr_list *mc_list)
Johannes Berg3ac64be2009-08-17 16:16:53 +02002954{
2955 u32 mfilt[2], val;
Johannes Berg3ac64be2009-08-17 16:16:53 +02002956 u8 pos;
Jiri Pirko22bedad32010-04-01 21:22:57 +00002957 struct netdev_hw_addr *ha;
Johannes Berg3ac64be2009-08-17 16:16:53 +02002958
2959 mfilt[0] = 0;
2960 mfilt[1] = 1;
2961
Jiri Pirko22bedad32010-04-01 21:22:57 +00002962 netdev_hw_addr_list_for_each(ha, mc_list) {
Johannes Berg3ac64be2009-08-17 16:16:53 +02002963 /* calculate XOR of eight 6-bit values */
Jiri Pirko22bedad32010-04-01 21:22:57 +00002964 val = get_unaligned_le32(ha->addr + 0);
Johannes Berg3ac64be2009-08-17 16:16:53 +02002965 pos = (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
Jiri Pirko22bedad32010-04-01 21:22:57 +00002966 val = get_unaligned_le32(ha->addr + 3);
Johannes Berg3ac64be2009-08-17 16:16:53 +02002967 pos ^= (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
2968 pos &= 0x3f;
2969 mfilt[pos / 32] |= (1 << (pos % 32));
2970 /* XXX: we might be able to just do this instead,
2971 * but not sure, needs testing, if we do use this we'd
2972 * neet to inform below to not reset the mcast */
2973 /* ath5k_hw_set_mcast_filterindex(ah,
Jiri Pirko22bedad32010-04-01 21:22:57 +00002974 * ha->addr[5]); */
Johannes Berg3ac64be2009-08-17 16:16:53 +02002975 }
2976
2977 return ((u64)(mfilt[1]) << 32) | mfilt[0];
2978}
2979
Ben Greearb1ae1ed2010-09-30 12:22:58 -07002980static bool ath_any_vif_assoc(struct ath5k_softc *sc)
2981{
2982 struct ath_vif_iter_data iter_data;
2983 iter_data.hw_macaddr = NULL;
2984 iter_data.any_assoc = false;
2985 iter_data.need_set_hw_addr = false;
2986 iter_data.found_active = true;
2987
2988 ieee80211_iterate_active_interfaces_atomic(sc->hw, ath_vif_iter,
2989 &iter_data);
2990 return iter_data.any_assoc;
2991}
2992
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002993#define SUPPORTED_FIF_FLAGS \
2994 FIF_PROMISC_IN_BSS | FIF_ALLMULTI | FIF_FCSFAIL | \
2995 FIF_PLCPFAIL | FIF_CONTROL | FIF_OTHER_BSS | \
2996 FIF_BCN_PRBRESP_PROMISC
2997/*
2998 * o always accept unicast, broadcast, and multicast traffic
2999 * o multicast traffic for all BSSIDs will be enabled if mac80211
3000 * says it should be
3001 * o maintain current state of phy ofdm or phy cck error reception.
3002 * If the hardware detects any of these type of errors then
3003 * ath5k_hw_get_rx_filter() will pass to us the respective
3004 * hardware filters to be able to receive these type of frames.
3005 * o probe request frames are accepted only when operating in
3006 * hostap, adhoc, or monitor modes
3007 * o enable promiscuous mode according to the interface state
3008 * o accept beacons:
3009 * - when operating in adhoc mode so the 802.11 layer creates
3010 * node table entries for peers,
3011 * - when operating in station mode for collecting rssi data when
3012 * the station is otherwise quiet, or
3013 * - when scanning
3014 */
3015static void ath5k_configure_filter(struct ieee80211_hw *hw,
3016 unsigned int changed_flags,
3017 unsigned int *new_flags,
Johannes Berg3ac64be2009-08-17 16:16:53 +02003018 u64 multicast)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003019{
3020 struct ath5k_softc *sc = hw->priv;
3021 struct ath5k_hw *ah = sc->ah;
Johannes Berg3ac64be2009-08-17 16:16:53 +02003022 u32 mfilt[2], rfilt;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003023
Bob Copeland56d1de02009-08-24 23:00:30 -04003024 mutex_lock(&sc->lock);
3025
Johannes Berg3ac64be2009-08-17 16:16:53 +02003026 mfilt[0] = multicast;
3027 mfilt[1] = multicast >> 32;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003028
3029 /* Only deal with supported flags */
3030 changed_flags &= SUPPORTED_FIF_FLAGS;
3031 *new_flags &= SUPPORTED_FIF_FLAGS;
3032
3033 /* If HW detects any phy or radar errors, leave those filters on.
3034 * Also, always enable Unicast, Broadcasts and Multicast
3035 * XXX: move unicast, bssid broadcasts and multicast to mac80211 */
3036 rfilt = (ath5k_hw_get_rx_filter(ah) & (AR5K_RX_FILTER_PHYERR)) |
3037 (AR5K_RX_FILTER_UCAST | AR5K_RX_FILTER_BCAST |
3038 AR5K_RX_FILTER_MCAST);
3039
3040 if (changed_flags & (FIF_PROMISC_IN_BSS | FIF_OTHER_BSS)) {
3041 if (*new_flags & FIF_PROMISC_IN_BSS) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003042 __set_bit(ATH_STAT_PROMISC, sc->status);
John Daiker0bbac082008-10-17 12:16:00 -07003043 } else {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003044 __clear_bit(ATH_STAT_PROMISC, sc->status);
John Daiker0bbac082008-10-17 12:16:00 -07003045 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003046 }
3047
Bob Copeland6b5dcccb2010-06-04 08:14:14 -04003048 if (test_bit(ATH_STAT_PROMISC, sc->status))
3049 rfilt |= AR5K_RX_FILTER_PROM;
3050
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003051 /* Note, AR5K_RX_FILTER_MCAST is already enabled */
3052 if (*new_flags & FIF_ALLMULTI) {
3053 mfilt[0] = ~0;
3054 mfilt[1] = ~0;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003055 }
3056
3057 /* This is the best we can do */
3058 if (*new_flags & (FIF_FCSFAIL | FIF_PLCPFAIL))
3059 rfilt |= AR5K_RX_FILTER_PHYERR;
3060
3061 /* FIF_BCN_PRBRESP_PROMISC really means to enable beacons
Bob Copeland30bf4162010-08-15 13:03:15 -04003062 * and probes for any BSSID */
Ben Greearb1ae1ed2010-09-30 12:22:58 -07003063 if ((*new_flags & FIF_BCN_PRBRESP_PROMISC) || (sc->nvifs > 1))
Bob Copeland30bf4162010-08-15 13:03:15 -04003064 rfilt |= AR5K_RX_FILTER_BEACON;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003065
3066 /* FIF_CONTROL doc says that if FIF_PROMISC_IN_BSS is not
3067 * set we should only pass on control frames for this
3068 * station. This needs testing. I believe right now this
3069 * enables *all* control frames, which is OK.. but
3070 * but we should see if we can improve on granularity */
3071 if (*new_flags & FIF_CONTROL)
3072 rfilt |= AR5K_RX_FILTER_CONTROL;
3073
3074 /* Additional settings per mode -- this is per ath5k */
3075
3076 /* XXX move these to mac80211, and add a beacon IFF flag to mac80211 */
3077
Bob Copeland56d1de02009-08-24 23:00:30 -04003078 switch (sc->opmode) {
3079 case NL80211_IFTYPE_MESH_POINT:
Bob Copeland56d1de02009-08-24 23:00:30 -04003080 rfilt |= AR5K_RX_FILTER_CONTROL |
3081 AR5K_RX_FILTER_BEACON |
3082 AR5K_RX_FILTER_PROBEREQ |
3083 AR5K_RX_FILTER_PROM;
3084 break;
3085 case NL80211_IFTYPE_AP:
3086 case NL80211_IFTYPE_ADHOC:
3087 rfilt |= AR5K_RX_FILTER_PROBEREQ |
3088 AR5K_RX_FILTER_BEACON;
3089 break;
3090 case NL80211_IFTYPE_STATION:
3091 if (sc->assoc)
3092 rfilt |= AR5K_RX_FILTER_BEACON;
3093 default:
3094 break;
3095 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003096
3097 /* Set filters */
John Daiker0bbac082008-10-17 12:16:00 -07003098 ath5k_hw_set_rx_filter(ah, rfilt);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003099
3100 /* Set multicast bits */
3101 ath5k_hw_set_mcast_filter(ah, mfilt[0], mfilt[1]);
Bob Copelanda180a132010-08-15 13:03:12 -04003102 /* Set the cached hw filter flags, this will later actually
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003103 * be set in HW */
3104 sc->filter_flags = rfilt;
Bob Copeland56d1de02009-08-24 23:00:30 -04003105
3106 mutex_unlock(&sc->lock);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003107}
3108
3109static int
3110ath5k_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Johannes Bergdc822b52008-12-29 12:55:09 +01003111 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
3112 struct ieee80211_key_conf *key)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003113{
3114 struct ath5k_softc *sc = hw->priv;
Luis R. Rodriguezdc1e0012009-11-04 17:47:31 -08003115 struct ath5k_hw *ah = sc->ah;
3116 struct ath_common *common = ath5k_hw_common(ah);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003117 int ret = 0;
3118
Bob Copeland9ad9a262008-10-29 08:30:54 -04003119 if (modparam_nohwcrypt)
3120 return -EOPNOTSUPP;
3121
Johannes Berg97359d12010-08-10 09:46:38 +02003122 switch (key->cipher) {
3123 case WLAN_CIPHER_SUITE_WEP40:
3124 case WLAN_CIPHER_SUITE_WEP104:
3125 case WLAN_CIPHER_SUITE_TKIP:
Bob Copeland3f64b432008-10-29 23:19:14 -04003126 break;
Johannes Berg97359d12010-08-10 09:46:38 +02003127 case WLAN_CIPHER_SUITE_CCMP:
Bruno Randolf781f3132010-09-08 16:04:59 +09003128 if (common->crypt_caps & ATH_CRYPT_CAP_CIPHER_AESCCM)
Bob Copeland1c818742009-08-24 23:00:33 -04003129 break;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003130 return -EOPNOTSUPP;
3131 default:
3132 WARN_ON(1);
3133 return -EINVAL;
3134 }
3135
3136 mutex_lock(&sc->lock);
3137
3138 switch (cmd) {
3139 case SET_KEY:
Bruno Randolfe0f8c2a2010-09-08 16:04:43 +09003140 ret = ath_key_config(common, vif, sta, key);
3141 if (ret >= 0) {
3142 key->hw_key_idx = ret;
3143 /* push IV and Michael MIC generation to stack */
3144 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
3145 if (key->cipher == WLAN_CIPHER_SUITE_TKIP)
3146 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
3147 if (key->cipher == WLAN_CIPHER_SUITE_CCMP)
3148 key->flags |= IEEE80211_KEY_FLAG_SW_MGMT;
3149 ret = 0;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003150 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003151 break;
3152 case DISABLE_KEY:
Bruno Randolfe0f8c2a2010-09-08 16:04:43 +09003153 ath_key_delete(common, key);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003154 break;
3155 default:
3156 ret = -EINVAL;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003157 }
3158
Jiri Slaby274c7c32008-07-15 17:44:20 +02003159 mmiowb();
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003160 mutex_unlock(&sc->lock);
3161 return ret;
3162}
3163
3164static int
3165ath5k_get_stats(struct ieee80211_hw *hw,
3166 struct ieee80211_low_level_stats *stats)
3167{
3168 struct ath5k_softc *sc = hw->priv;
Nick Kossifidis194828a2008-04-16 18:49:02 +03003169
3170 /* Force update */
Bruno Randolf495391d2010-03-25 14:49:36 +09003171 ath5k_hw_update_mib_counters(sc->ah);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003172
Bruno Randolf495391d2010-03-25 14:49:36 +09003173 stats->dot11ACKFailureCount = sc->stats.ack_fail;
3174 stats->dot11RTSFailureCount = sc->stats.rts_fail;
3175 stats->dot11RTSSuccessCount = sc->stats.rts_ok;
3176 stats->dot11FCSErrorCount = sc->stats.fcs_error;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003177
3178 return 0;
3179}
3180
Holger Schurig55ee82b2010-04-19 10:24:22 +02003181static int ath5k_get_survey(struct ieee80211_hw *hw, int idx,
3182 struct survey_info *survey)
3183{
3184 struct ath5k_softc *sc = hw->priv;
3185 struct ieee80211_conf *conf = &hw->conf;
Bruno Randolfedb40a22010-10-19 16:56:54 +09003186 struct ath_common *common = ath5k_hw_common(sc->ah);
3187 struct ath_cycle_counters *cc = &common->cc_survey;
3188 unsigned int div = common->clockrate * 1000;
Holger Schurig55ee82b2010-04-19 10:24:22 +02003189
Bruno Randolfedb40a22010-10-19 16:56:54 +09003190 if (idx != 0)
Holger Schurig55ee82b2010-04-19 10:24:22 +02003191 return -ENOENT;
3192
3193 survey->channel = conf->channel;
3194 survey->filled = SURVEY_INFO_NOISE_DBM;
3195 survey->noise = sc->ah->ah_noise_floor;
3196
Bruno Randolfedb40a22010-10-19 16:56:54 +09003197 spin_lock_bh(&common->cc_lock);
3198 ath_hw_cycle_counters_update(common);
3199 if (cc->cycles > 0) {
3200 survey->filled |= SURVEY_INFO_CHANNEL_TIME |
3201 SURVEY_INFO_CHANNEL_TIME_BUSY |
3202 SURVEY_INFO_CHANNEL_TIME_RX |
3203 SURVEY_INFO_CHANNEL_TIME_TX;
3204 survey->channel_time += cc->cycles / div;
3205 survey->channel_time_busy += cc->rx_busy / div;
3206 survey->channel_time_rx += cc->rx_frame / div;
3207 survey->channel_time_tx += cc->tx_frame / div;
3208 }
3209 memset(cc, 0, sizeof(*cc));
3210 spin_unlock_bh(&common->cc_lock);
3211
Holger Schurig55ee82b2010-04-19 10:24:22 +02003212 return 0;
3213}
3214
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003215static u64
3216ath5k_get_tsf(struct ieee80211_hw *hw)
3217{
3218 struct ath5k_softc *sc = hw->priv;
3219
3220 return ath5k_hw_get_tsf64(sc->ah);
3221}
3222
3223static void
Alina Friedrichsen3b5d6652009-01-24 07:09:59 +01003224ath5k_set_tsf(struct ieee80211_hw *hw, u64 tsf)
3225{
3226 struct ath5k_softc *sc = hw->priv;
3227
3228 ath5k_hw_set_tsf64(sc->ah, tsf);
3229}
3230
3231static void
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003232ath5k_reset_tsf(struct ieee80211_hw *hw)
3233{
3234 struct ath5k_softc *sc = hw->priv;
3235
Bruno Randolf9804b982008-01-19 18:17:59 +09003236 /*
3237 * in IBSS mode we need to update the beacon timers too.
3238 * this will also reset the TSF if we call it with 0
3239 */
Johannes Berg05c914f2008-09-11 00:01:58 +02003240 if (sc->opmode == NL80211_IFTYPE_ADHOC)
Bruno Randolf9804b982008-01-19 18:17:59 +09003241 ath5k_beacon_update_timers(sc, 0);
3242 else
3243 ath5k_hw_reset_tsf(sc->ah);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003244}
3245
Martin Xu02969b32008-11-24 10:49:27 +08003246static void
3247set_beacon_filter(struct ieee80211_hw *hw, bool enable)
3248{
3249 struct ath5k_softc *sc = hw->priv;
3250 struct ath5k_hw *ah = sc->ah;
3251 u32 rfilt;
3252 rfilt = ath5k_hw_get_rx_filter(ah);
3253 if (enable)
3254 rfilt |= AR5K_RX_FILTER_BEACON;
3255 else
3256 rfilt &= ~AR5K_RX_FILTER_BEACON;
3257 ath5k_hw_set_rx_filter(ah, rfilt);
3258 sc->filter_flags = rfilt;
3259}
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003260
Martin Xu02969b32008-11-24 10:49:27 +08003261static void ath5k_bss_info_changed(struct ieee80211_hw *hw,
3262 struct ieee80211_vif *vif,
3263 struct ieee80211_bss_conf *bss_conf,
3264 u32 changes)
3265{
Ben Greearb1ae1ed2010-09-30 12:22:58 -07003266 struct ath5k_vif *avf = (void *)vif->drv_priv;
Martin Xu02969b32008-11-24 10:49:27 +08003267 struct ath5k_softc *sc = hw->priv;
Johannes Berg2d0ddec2009-04-23 16:13:26 +02003268 struct ath5k_hw *ah = sc->ah;
Luis R. Rodriguez954fece2009-09-10 10:51:33 -07003269 struct ath_common *common = ath5k_hw_common(ah);
Bob Copeland21800492009-07-04 12:59:52 -04003270 unsigned long flags;
Johannes Berg2d0ddec2009-04-23 16:13:26 +02003271
3272 mutex_lock(&sc->lock);
Johannes Berg2d0ddec2009-04-23 16:13:26 +02003273
3274 if (changes & BSS_CHANGED_BSSID) {
3275 /* Cache for later use during resets */
Luis R. Rodriguez954fece2009-09-10 10:51:33 -07003276 memcpy(common->curbssid, bss_conf->bssid, ETH_ALEN);
Luis R. Rodriguez8ce54c52009-10-06 20:44:34 -04003277 common->curaid = 0;
Nick Kossifidis418de6d2010-08-15 13:03:10 -04003278 ath5k_hw_set_bssid(ah);
Johannes Berg2d0ddec2009-04-23 16:13:26 +02003279 mmiowb();
3280 }
Johannes Berg57c4d7b2009-04-23 16:10:04 +02003281
3282 if (changes & BSS_CHANGED_BEACON_INT)
3283 sc->bintval = bss_conf->beacon_int;
3284
Martin Xu02969b32008-11-24 10:49:27 +08003285 if (changes & BSS_CHANGED_ASSOC) {
Ben Greearb1ae1ed2010-09-30 12:22:58 -07003286 avf->assoc = bss_conf->assoc;
3287 if (bss_conf->assoc)
3288 sc->assoc = bss_conf->assoc;
3289 else
3290 sc->assoc = ath_any_vif_assoc(sc);
3291
Martin Xu02969b32008-11-24 10:49:27 +08003292 if (sc->opmode == NL80211_IFTYPE_STATION)
3293 set_beacon_filter(hw, sc->assoc);
Bob Copelandf0f3d382009-06-10 22:22:21 -04003294 ath5k_hw_set_ledstate(sc->ah, sc->assoc ?
3295 AR5K_LED_ASSOC : AR5K_LED_INIT);
Luis R. Rodriguez8ce54c52009-10-06 20:44:34 -04003296 if (bss_conf->assoc) {
3297 ATH5K_DBG(sc, ATH5K_DEBUG_ANY,
3298 "Bss Info ASSOC %d, bssid: %pM\n",
3299 bss_conf->aid, common->curbssid);
3300 common->curaid = bss_conf->aid;
Nick Kossifidis418de6d2010-08-15 13:03:10 -04003301 ath5k_hw_set_bssid(ah);
Luis R. Rodriguez8ce54c52009-10-06 20:44:34 -04003302 /* Once ANI is available you would start it here */
3303 }
Martin Xu02969b32008-11-24 10:49:27 +08003304 }
Johannes Berg2d0ddec2009-04-23 16:13:26 +02003305
Bob Copeland21800492009-07-04 12:59:52 -04003306 if (changes & BSS_CHANGED_BEACON) {
3307 spin_lock_irqsave(&sc->block, flags);
3308 ath5k_beacon_update(hw, vif);
3309 spin_unlock_irqrestore(&sc->block, flags);
Johannes Berg2d0ddec2009-04-23 16:13:26 +02003310 }
3311
Bob Copeland21800492009-07-04 12:59:52 -04003312 if (changes & BSS_CHANGED_BEACON_ENABLED)
3313 sc->enable_beacon = bss_conf->enable_beacon;
3314
3315 if (changes & (BSS_CHANGED_BEACON | BSS_CHANGED_BEACON_ENABLED |
3316 BSS_CHANGED_BEACON_INT))
3317 ath5k_beacon_config(sc);
3318
Johannes Berg2d0ddec2009-04-23 16:13:26 +02003319 mutex_unlock(&sc->lock);
Martin Xu02969b32008-11-24 10:49:27 +08003320}
Bob Copelandf0f3d382009-06-10 22:22:21 -04003321
3322static void ath5k_sw_scan_start(struct ieee80211_hw *hw)
3323{
3324 struct ath5k_softc *sc = hw->priv;
3325 if (!sc->assoc)
3326 ath5k_hw_set_ledstate(sc->ah, AR5K_LED_SCAN);
3327}
3328
3329static void ath5k_sw_scan_complete(struct ieee80211_hw *hw)
3330{
3331 struct ath5k_softc *sc = hw->priv;
3332 ath5k_hw_set_ledstate(sc->ah, sc->assoc ?
3333 AR5K_LED_ASSOC : AR5K_LED_INIT);
3334}
Lukáš Turek6e08d222009-12-21 22:50:51 +01003335
3336/**
3337 * ath5k_set_coverage_class - Set IEEE 802.11 coverage class
3338 *
3339 * @hw: struct ieee80211_hw pointer
3340 * @coverage_class: IEEE 802.11 coverage class number
3341 *
3342 * Mac80211 callback. Sets slot time, ACK timeout and CTS timeout for given
3343 * coverage class. The values are persistent, they are restored after device
3344 * reset.
3345 */
3346static void ath5k_set_coverage_class(struct ieee80211_hw *hw, u8 coverage_class)
3347{
3348 struct ath5k_softc *sc = hw->priv;
3349
3350 mutex_lock(&sc->lock);
3351 ath5k_hw_set_coverage_class(sc->ah, coverage_class);
3352 mutex_unlock(&sc->lock);
3353}
Bob Copeland8a63fac2010-09-17 12:45:07 +09003354
Bruno Randolfe0b1cc52010-09-17 11:37:18 +09003355static int ath5k_conf_tx(struct ieee80211_hw *hw, u16 queue,
3356 const struct ieee80211_tx_queue_params *params)
3357{
3358 struct ath5k_softc *sc = hw->priv;
3359 struct ath5k_hw *ah = sc->ah;
3360 struct ath5k_txq_info qi;
3361 int ret = 0;
3362
3363 if (queue >= ah->ah_capabilities.cap_queues.q_tx_num)
3364 return 0;
3365
3366 mutex_lock(&sc->lock);
3367
3368 ath5k_hw_get_tx_queueprops(ah, queue, &qi);
3369
3370 qi.tqi_aifs = params->aifs;
3371 qi.tqi_cw_min = params->cw_min;
3372 qi.tqi_cw_max = params->cw_max;
3373 qi.tqi_burst_time = params->txop;
3374
3375 ATH5K_DBG(sc, ATH5K_DEBUG_ANY,
3376 "Configure tx [queue %d], "
3377 "aifs: %d, cw_min: %d, cw_max: %d, txop: %d\n",
3378 queue, params->aifs, params->cw_min,
3379 params->cw_max, params->txop);
3380
3381 if (ath5k_hw_set_tx_queueprops(ah, queue, &qi)) {
3382 ATH5K_ERR(sc,
3383 "Unable to update hardware queue %u!\n", queue);
3384 ret = -EIO;
3385 } else
3386 ath5k_hw_reset_tx_queue(ah, queue);
3387
3388 mutex_unlock(&sc->lock);
3389
3390 return ret;
3391}
3392
Bruno Randolf72a80112010-11-10 12:51:01 +09003393static int ath5k_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
3394{
3395 struct ath5k_softc *sc = hw->priv;
3396
3397 if (tx_ant == 1 && rx_ant == 1)
3398 ath5k_hw_set_antenna_mode(sc->ah, AR5K_ANTMODE_FIXED_A);
3399 else if (tx_ant == 2 && rx_ant == 2)
3400 ath5k_hw_set_antenna_mode(sc->ah, AR5K_ANTMODE_FIXED_B);
3401 else if ((tx_ant & 3) == 3 && (rx_ant & 3) == 3)
3402 ath5k_hw_set_antenna_mode(sc->ah, AR5K_ANTMODE_DEFAULT);
3403 else
3404 return -EINVAL;
3405 return 0;
3406}
3407
3408static int ath5k_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
3409{
3410 struct ath5k_softc *sc = hw->priv;
3411
3412 switch (sc->ah->ah_ant_mode) {
3413 case AR5K_ANTMODE_FIXED_A:
3414 *tx_ant = 1; *rx_ant = 1; break;
3415 case AR5K_ANTMODE_FIXED_B:
3416 *tx_ant = 2; *rx_ant = 2; break;
3417 case AR5K_ANTMODE_DEFAULT:
3418 *tx_ant = 3; *rx_ant = 3; break;
3419 }
3420 return 0;
3421}
3422
Bob Copeland8a63fac2010-09-17 12:45:07 +09003423static const struct ieee80211_ops ath5k_hw_ops = {
3424 .tx = ath5k_tx,
3425 .start = ath5k_start,
3426 .stop = ath5k_stop,
3427 .add_interface = ath5k_add_interface,
3428 .remove_interface = ath5k_remove_interface,
3429 .config = ath5k_config,
3430 .prepare_multicast = ath5k_prepare_multicast,
3431 .configure_filter = ath5k_configure_filter,
3432 .set_key = ath5k_set_key,
3433 .get_stats = ath5k_get_stats,
3434 .get_survey = ath5k_get_survey,
Bruno Randolfe0b1cc52010-09-17 11:37:18 +09003435 .conf_tx = ath5k_conf_tx,
Bob Copeland8a63fac2010-09-17 12:45:07 +09003436 .get_tsf = ath5k_get_tsf,
3437 .set_tsf = ath5k_set_tsf,
3438 .reset_tsf = ath5k_reset_tsf,
3439 .bss_info_changed = ath5k_bss_info_changed,
3440 .sw_scan_start = ath5k_sw_scan_start,
3441 .sw_scan_complete = ath5k_sw_scan_complete,
3442 .set_coverage_class = ath5k_set_coverage_class,
Bruno Randolf72a80112010-11-10 12:51:01 +09003443 .set_antenna = ath5k_set_antenna,
3444 .get_antenna = ath5k_get_antenna,
Bob Copeland8a63fac2010-09-17 12:45:07 +09003445};
3446
3447/********************\
3448* PCI Initialization *
3449\********************/
3450
3451static int __devinit
3452ath5k_pci_probe(struct pci_dev *pdev,
3453 const struct pci_device_id *id)
3454{
3455 void __iomem *mem;
3456 struct ath5k_softc *sc;
3457 struct ath_common *common;
3458 struct ieee80211_hw *hw;
3459 int ret;
3460 u8 csz;
3461
3462 /*
3463 * L0s needs to be disabled on all ath5k cards.
3464 *
3465 * For distributions shipping with CONFIG_PCIEASPM (this will be enabled
3466 * by default in the future in 2.6.36) this will also mean both L1 and
3467 * L0s will be disabled when a pre 1.1 PCIe device is detected. We do
3468 * know L1 works correctly even for all ath5k pre 1.1 PCIe devices
3469 * though but cannot currently undue the effect of a blacklist, for
3470 * details you can read pcie_aspm_sanity_check() and see how it adjusts
3471 * the device link capability.
3472 *
3473 * It may be possible in the future to implement some PCI API to allow
3474 * drivers to override blacklists for pre 1.1 PCIe but for now it is
3475 * best to accept that both L0s and L1 will be disabled completely for
3476 * distributions shipping with CONFIG_PCIEASPM rather than having this
3477 * issue present. Motivation for adding this new API will be to help
3478 * with power consumption for some of these devices.
3479 */
3480 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S);
3481
3482 ret = pci_enable_device(pdev);
3483 if (ret) {
3484 dev_err(&pdev->dev, "can't enable device\n");
3485 goto err;
3486 }
3487
3488 /* XXX 32-bit addressing only */
3489 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
3490 if (ret) {
3491 dev_err(&pdev->dev, "32-bit DMA not available\n");
3492 goto err_dis;
3493 }
3494
3495 /*
3496 * Cache line size is used to size and align various
3497 * structures used to communicate with the hardware.
3498 */
3499 pci_read_config_byte(pdev, PCI_CACHE_LINE_SIZE, &csz);
3500 if (csz == 0) {
3501 /*
3502 * Linux 2.4.18 (at least) writes the cache line size
3503 * register as a 16-bit wide register which is wrong.
3504 * We must have this setup properly for rx buffer
3505 * DMA to work so force a reasonable value here if it
3506 * comes up zero.
3507 */
3508 csz = L1_CACHE_BYTES >> 2;
3509 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, csz);
3510 }
3511 /*
3512 * The default setting of latency timer yields poor results,
3513 * set it to the value used by other systems. It may be worth
3514 * tweaking this setting more.
3515 */
3516 pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0xa8);
3517
3518 /* Enable bus mastering */
3519 pci_set_master(pdev);
3520
3521 /*
3522 * Disable the RETRY_TIMEOUT register (0x41) to keep
3523 * PCI Tx retries from interfering with C3 CPU state.
3524 */
3525 pci_write_config_byte(pdev, 0x41, 0);
3526
3527 ret = pci_request_region(pdev, 0, "ath5k");
3528 if (ret) {
3529 dev_err(&pdev->dev, "cannot reserve PCI memory region\n");
3530 goto err_dis;
3531 }
3532
3533 mem = pci_iomap(pdev, 0, 0);
3534 if (!mem) {
3535 dev_err(&pdev->dev, "cannot remap PCI memory region\n") ;
3536 ret = -EIO;
3537 goto err_reg;
3538 }
3539
3540 /*
3541 * Allocate hw (mac80211 main struct)
3542 * and hw->priv (driver private data)
3543 */
3544 hw = ieee80211_alloc_hw(sizeof(*sc), &ath5k_hw_ops);
3545 if (hw == NULL) {
3546 dev_err(&pdev->dev, "cannot allocate ieee80211_hw\n");
3547 ret = -ENOMEM;
3548 goto err_map;
3549 }
3550
3551 dev_info(&pdev->dev, "registered as '%s'\n", wiphy_name(hw->wiphy));
3552
3553 /* Initialize driver private data */
3554 SET_IEEE80211_DEV(hw, &pdev->dev);
3555 hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
3556 IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
3557 IEEE80211_HW_SIGNAL_DBM;
3558
3559 hw->wiphy->interface_modes =
3560 BIT(NL80211_IFTYPE_AP) |
3561 BIT(NL80211_IFTYPE_STATION) |
3562 BIT(NL80211_IFTYPE_ADHOC) |
3563 BIT(NL80211_IFTYPE_MESH_POINT);
3564
3565 hw->extra_tx_headroom = 2;
3566 hw->channel_change_time = 5000;
3567 sc = hw->priv;
3568 sc->hw = hw;
3569 sc->pdev = pdev;
3570
Bob Copeland8a63fac2010-09-17 12:45:07 +09003571 /*
3572 * Mark the device as detached to avoid processing
3573 * interrupts until setup is complete.
3574 */
3575 __set_bit(ATH_STAT_INVALID, sc->status);
3576
3577 sc->iobase = mem; /* So we can unmap it on detach */
3578 sc->opmode = NL80211_IFTYPE_STATION;
3579 sc->bintval = 1000;
3580 mutex_init(&sc->lock);
3581 spin_lock_init(&sc->rxbuflock);
3582 spin_lock_init(&sc->txbuflock);
3583 spin_lock_init(&sc->block);
3584
3585 /* Set private data */
3586 pci_set_drvdata(pdev, sc);
3587
3588 /* Setup interrupt handler */
3589 ret = request_irq(pdev->irq, ath5k_intr, IRQF_SHARED, "ath", sc);
3590 if (ret) {
3591 ATH5K_ERR(sc, "request_irq failed\n");
3592 goto err_free;
3593 }
3594
3595 /* If we passed the test, malloc an ath5k_hw struct */
3596 sc->ah = kzalloc(sizeof(struct ath5k_hw), GFP_KERNEL);
3597 if (!sc->ah) {
3598 ret = -ENOMEM;
3599 ATH5K_ERR(sc, "out of memory\n");
3600 goto err_irq;
3601 }
3602
3603 sc->ah->ah_sc = sc;
3604 sc->ah->ah_iobase = sc->iobase;
3605 common = ath5k_hw_common(sc->ah);
3606 common->ops = &ath5k_common_ops;
3607 common->ah = sc->ah;
3608 common->hw = hw;
3609 common->cachelsz = csz << 2; /* convert to bytes */
Ben Greear9192f712010-10-15 15:51:32 -07003610 spin_lock_init(&common->cc_lock);
Bob Copeland8a63fac2010-09-17 12:45:07 +09003611
3612 /* Initialize device */
3613 ret = ath5k_hw_attach(sc);
3614 if (ret) {
3615 goto err_free_ah;
3616 }
3617
3618 /* set up multi-rate retry capabilities */
3619 if (sc->ah->ah_version == AR5K_AR5212) {
3620 hw->max_rates = 4;
3621 hw->max_rate_tries = 11;
3622 }
3623
Ben Greearb1ae1ed2010-09-30 12:22:58 -07003624 hw->vif_data_size = sizeof(struct ath5k_vif);
3625
Bob Copeland8a63fac2010-09-17 12:45:07 +09003626 /* Finish private driver data initialization */
3627 ret = ath5k_attach(pdev, hw);
3628 if (ret)
3629 goto err_ah;
3630
3631 ATH5K_INFO(sc, "Atheros AR%s chip found (MAC: 0x%x, PHY: 0x%x)\n",
3632 ath5k_chip_name(AR5K_VERSION_MAC, sc->ah->ah_mac_srev),
3633 sc->ah->ah_mac_srev,
3634 sc->ah->ah_phy_revision);
3635
3636 if (!sc->ah->ah_single_chip) {
3637 /* Single chip radio (!RF5111) */
3638 if (sc->ah->ah_radio_5ghz_revision &&
3639 !sc->ah->ah_radio_2ghz_revision) {
3640 /* No 5GHz support -> report 2GHz radio */
3641 if (!test_bit(AR5K_MODE_11A,
3642 sc->ah->ah_capabilities.cap_mode)) {
3643 ATH5K_INFO(sc, "RF%s 2GHz radio found (0x%x)\n",
3644 ath5k_chip_name(AR5K_VERSION_RAD,
3645 sc->ah->ah_radio_5ghz_revision),
3646 sc->ah->ah_radio_5ghz_revision);
3647 /* No 2GHz support (5110 and some
3648 * 5Ghz only cards) -> report 5Ghz radio */
3649 } else if (!test_bit(AR5K_MODE_11B,
3650 sc->ah->ah_capabilities.cap_mode)) {
3651 ATH5K_INFO(sc, "RF%s 5GHz radio found (0x%x)\n",
3652 ath5k_chip_name(AR5K_VERSION_RAD,
3653 sc->ah->ah_radio_5ghz_revision),
3654 sc->ah->ah_radio_5ghz_revision);
3655 /* Multiband radio */
3656 } else {
3657 ATH5K_INFO(sc, "RF%s multiband radio found"
3658 " (0x%x)\n",
3659 ath5k_chip_name(AR5K_VERSION_RAD,
3660 sc->ah->ah_radio_5ghz_revision),
3661 sc->ah->ah_radio_5ghz_revision);
3662 }
3663 }
3664 /* Multi chip radio (RF5111 - RF2111) ->
3665 * report both 2GHz/5GHz radios */
3666 else if (sc->ah->ah_radio_5ghz_revision &&
3667 sc->ah->ah_radio_2ghz_revision){
3668 ATH5K_INFO(sc, "RF%s 5GHz radio found (0x%x)\n",
3669 ath5k_chip_name(AR5K_VERSION_RAD,
3670 sc->ah->ah_radio_5ghz_revision),
3671 sc->ah->ah_radio_5ghz_revision);
3672 ATH5K_INFO(sc, "RF%s 2GHz radio found (0x%x)\n",
3673 ath5k_chip_name(AR5K_VERSION_RAD,
3674 sc->ah->ah_radio_2ghz_revision),
3675 sc->ah->ah_radio_2ghz_revision);
3676 }
3677 }
3678
Ben Greeard84a35d2010-10-12 10:55:38 -07003679 ath5k_debug_init_device(sc);
Bob Copeland8a63fac2010-09-17 12:45:07 +09003680
3681 /* ready to process interrupts */
3682 __clear_bit(ATH_STAT_INVALID, sc->status);
3683
3684 return 0;
3685err_ah:
3686 ath5k_hw_detach(sc->ah);
3687err_free_ah:
3688 kfree(sc->ah);
3689err_irq:
3690 free_irq(pdev->irq, sc);
3691err_free:
3692 ieee80211_free_hw(hw);
3693err_map:
3694 pci_iounmap(pdev, mem);
3695err_reg:
3696 pci_release_region(pdev, 0);
3697err_dis:
3698 pci_disable_device(pdev);
3699err:
3700 return ret;
3701}
3702
3703static void __devexit
3704ath5k_pci_remove(struct pci_dev *pdev)
3705{
3706 struct ath5k_softc *sc = pci_get_drvdata(pdev);
3707
3708 ath5k_debug_finish_device(sc);
3709 ath5k_detach(pdev, sc->hw);
3710 ath5k_hw_detach(sc->ah);
3711 kfree(sc->ah);
3712 free_irq(pdev->irq, sc);
3713 pci_iounmap(pdev, sc->iobase);
3714 pci_release_region(pdev, 0);
3715 pci_disable_device(pdev);
3716 ieee80211_free_hw(sc->hw);
3717}
3718
3719#ifdef CONFIG_PM_SLEEP
3720static int ath5k_pci_suspend(struct device *dev)
3721{
3722 struct ath5k_softc *sc = pci_get_drvdata(to_pci_dev(dev));
3723
3724 ath5k_led_off(sc);
3725 return 0;
3726}
3727
3728static int ath5k_pci_resume(struct device *dev)
3729{
3730 struct pci_dev *pdev = to_pci_dev(dev);
3731 struct ath5k_softc *sc = pci_get_drvdata(pdev);
3732
3733 /*
3734 * Suspend/Resume resets the PCI configuration space, so we have to
3735 * re-disable the RETRY_TIMEOUT register (0x41) to keep
3736 * PCI Tx retries from interfering with C3 CPU state
3737 */
3738 pci_write_config_byte(pdev, 0x41, 0);
3739
3740 ath5k_led_enable(sc);
3741 return 0;
3742}
3743
3744static SIMPLE_DEV_PM_OPS(ath5k_pm_ops, ath5k_pci_suspend, ath5k_pci_resume);
3745#define ATH5K_PM_OPS (&ath5k_pm_ops)
3746#else
3747#define ATH5K_PM_OPS NULL
3748#endif /* CONFIG_PM_SLEEP */
3749
3750static struct pci_driver ath5k_pci_driver = {
3751 .name = KBUILD_MODNAME,
3752 .id_table = ath5k_pci_id_table,
3753 .probe = ath5k_pci_probe,
3754 .remove = __devexit_p(ath5k_pci_remove),
3755 .driver.pm = ATH5K_PM_OPS,
3756};
3757
3758/*
3759 * Module init/exit functions
3760 */
3761static int __init
3762init_ath5k_pci(void)
3763{
3764 int ret;
3765
Bob Copeland8a63fac2010-09-17 12:45:07 +09003766 ret = pci_register_driver(&ath5k_pci_driver);
3767 if (ret) {
3768 printk(KERN_ERR "ath5k_pci: can't register pci driver\n");
3769 return ret;
3770 }
3771
3772 return 0;
3773}
3774
3775static void __exit
3776exit_ath5k_pci(void)
3777{
3778 pci_unregister_driver(&ath5k_pci_driver);
Bob Copeland8a63fac2010-09-17 12:45:07 +09003779}
3780
3781module_init(init_ath5k_pci);
3782module_exit(exit_ath5k_pci);