blob: dad726585637146a62b282f6facce9636cfab57b [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"
Jiri Slabyfa1c1142007-08-12 17:33:16 +020065
Bob Copeland9ad9a262008-10-29 08:30:54 -040066static int modparam_nohwcrypt;
Bob Copeland46802a42009-04-15 07:57:34 -040067module_param_named(nohwcrypt, modparam_nohwcrypt, bool, S_IRUGO);
Bob Copeland9ad9a262008-10-29 08:30:54 -040068MODULE_PARM_DESC(nohwcrypt, "Disable hardware encryption.");
Jiri Slabyfa1c1142007-08-12 17:33:16 +020069
Bob Copeland42639fc2009-03-30 08:05:29 -040070static int modparam_all_channels;
Bob Copeland46802a42009-04-15 07:57:34 -040071module_param_named(all_channels, modparam_all_channels, bool, S_IRUGO);
Bob Copeland42639fc2009-03-30 08:05:29 -040072MODULE_PARM_DESC(all_channels, "Expose all channels the device can use.");
73
Jiri Slabyfa1c1142007-08-12 17:33:16 +020074/* Module info */
75MODULE_AUTHOR("Jiri Slaby");
76MODULE_AUTHOR("Nick Kossifidis");
77MODULE_DESCRIPTION("Support for 5xxx series of Atheros 802.11 wireless LAN cards.");
78MODULE_SUPPORTED_DEVICE("Atheros 5xxx WLAN cards");
79MODULE_LICENSE("Dual BSD/GPL");
Nick Kossifidis0d5f0312008-09-29 01:27:27 +030080MODULE_VERSION("0.6.0 (EXPERIMENTAL)");
Jiri Slabyfa1c1142007-08-12 17:33:16 +020081
Bob Copeland8a63fac2010-09-17 12:45:07 +090082static int ath5k_reset(struct ath5k_softc *sc, struct ieee80211_channel *chan);
83static int ath5k_beacon_update(struct ieee80211_hw *hw,
84 struct ieee80211_vif *vif);
85static void ath5k_beacon_update_timers(struct ath5k_softc *sc, u64 bc_tsf);
Jiri Slabyfa1c1142007-08-12 17:33:16 +020086
87/* Known PCI ids */
Alexey Dobriyana3aa1882010-01-07 11:58:11 +000088static DEFINE_PCI_DEVICE_TABLE(ath5k_pci_id_table) = {
Pavel Roskin97a81f52009-08-26 22:30:09 -040089 { PCI_VDEVICE(ATHEROS, 0x0207) }, /* 5210 early */
90 { PCI_VDEVICE(ATHEROS, 0x0007) }, /* 5210 */
91 { PCI_VDEVICE(ATHEROS, 0x0011) }, /* 5311 - this is on AHB bus !*/
92 { PCI_VDEVICE(ATHEROS, 0x0012) }, /* 5211 */
93 { PCI_VDEVICE(ATHEROS, 0x0013) }, /* 5212 */
94 { PCI_VDEVICE(3COM_2, 0x0013) }, /* 3com 5212 */
95 { PCI_VDEVICE(3COM, 0x0013) }, /* 3com 3CRDAG675 5212 */
96 { PCI_VDEVICE(ATHEROS, 0x1014) }, /* IBM minipci 5212 */
97 { PCI_VDEVICE(ATHEROS, 0x0014) }, /* 5212 combatible */
98 { PCI_VDEVICE(ATHEROS, 0x0015) }, /* 5212 combatible */
99 { PCI_VDEVICE(ATHEROS, 0x0016) }, /* 5212 combatible */
100 { PCI_VDEVICE(ATHEROS, 0x0017) }, /* 5212 combatible */
101 { PCI_VDEVICE(ATHEROS, 0x0018) }, /* 5212 combatible */
102 { PCI_VDEVICE(ATHEROS, 0x0019) }, /* 5212 combatible */
103 { PCI_VDEVICE(ATHEROS, 0x001a) }, /* 2413 Griffin-lite */
104 { PCI_VDEVICE(ATHEROS, 0x001b) }, /* 5413 Eagle */
105 { PCI_VDEVICE(ATHEROS, 0x001c) }, /* PCI-E cards */
106 { PCI_VDEVICE(ATHEROS, 0x001d) }, /* 2417 Nala */
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200107 { 0 }
108};
109MODULE_DEVICE_TABLE(pci, ath5k_pci_id_table);
110
111/* Known SREVs */
Jiri Slaby2c91108c2009-03-07 10:26:41 +0100112static const struct ath5k_srev_name srev_names[] = {
Nick Kossifidis1bef0162008-09-29 02:09:09 +0300113 { "5210", AR5K_VERSION_MAC, AR5K_SREV_AR5210 },
114 { "5311", AR5K_VERSION_MAC, AR5K_SREV_AR5311 },
115 { "5311A", AR5K_VERSION_MAC, AR5K_SREV_AR5311A },
116 { "5311B", AR5K_VERSION_MAC, AR5K_SREV_AR5311B },
117 { "5211", AR5K_VERSION_MAC, AR5K_SREV_AR5211 },
118 { "5212", AR5K_VERSION_MAC, AR5K_SREV_AR5212 },
119 { "5213", AR5K_VERSION_MAC, AR5K_SREV_AR5213 },
120 { "5213A", AR5K_VERSION_MAC, AR5K_SREV_AR5213A },
121 { "2413", AR5K_VERSION_MAC, AR5K_SREV_AR2413 },
122 { "2414", AR5K_VERSION_MAC, AR5K_SREV_AR2414 },
123 { "5424", AR5K_VERSION_MAC, AR5K_SREV_AR5424 },
124 { "5413", AR5K_VERSION_MAC, AR5K_SREV_AR5413 },
125 { "5414", AR5K_VERSION_MAC, AR5K_SREV_AR5414 },
126 { "2415", AR5K_VERSION_MAC, AR5K_SREV_AR2415 },
127 { "5416", AR5K_VERSION_MAC, AR5K_SREV_AR5416 },
128 { "5418", AR5K_VERSION_MAC, AR5K_SREV_AR5418 },
129 { "2425", AR5K_VERSION_MAC, AR5K_SREV_AR2425 },
130 { "2417", AR5K_VERSION_MAC, AR5K_SREV_AR2417 },
131 { "xxxxx", AR5K_VERSION_MAC, AR5K_SREV_UNKNOWN },
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200132 { "5110", AR5K_VERSION_RAD, AR5K_SREV_RAD_5110 },
133 { "5111", AR5K_VERSION_RAD, AR5K_SREV_RAD_5111 },
Nick Kossifidis1bef0162008-09-29 02:09:09 +0300134 { "5111A", AR5K_VERSION_RAD, AR5K_SREV_RAD_5111A },
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200135 { "2111", AR5K_VERSION_RAD, AR5K_SREV_RAD_2111 },
136 { "5112", AR5K_VERSION_RAD, AR5K_SREV_RAD_5112 },
137 { "5112A", AR5K_VERSION_RAD, AR5K_SREV_RAD_5112A },
Nick Kossifidis1bef0162008-09-29 02:09:09 +0300138 { "5112B", AR5K_VERSION_RAD, AR5K_SREV_RAD_5112B },
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200139 { "2112", AR5K_VERSION_RAD, AR5K_SREV_RAD_2112 },
140 { "2112A", AR5K_VERSION_RAD, AR5K_SREV_RAD_2112A },
Nick Kossifidis1bef0162008-09-29 02:09:09 +0300141 { "2112B", AR5K_VERSION_RAD, AR5K_SREV_RAD_2112B },
142 { "2413", AR5K_VERSION_RAD, AR5K_SREV_RAD_2413 },
143 { "5413", AR5K_VERSION_RAD, AR5K_SREV_RAD_5413 },
144 { "2316", AR5K_VERSION_RAD, AR5K_SREV_RAD_2316 },
145 { "2317", AR5K_VERSION_RAD, AR5K_SREV_RAD_2317 },
146 { "5424", AR5K_VERSION_RAD, AR5K_SREV_RAD_5424 },
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200147 { "5133", AR5K_VERSION_RAD, AR5K_SREV_RAD_5133 },
148 { "xxxxx", AR5K_VERSION_RAD, AR5K_SREV_UNKNOWN },
149};
150
Jiri Slaby2c91108c2009-03-07 10:26:41 +0100151static const struct ieee80211_rate ath5k_rates[] = {
Bruno Randolf63266a62008-07-30 17:12:58 +0200152 { .bitrate = 10,
153 .hw_value = ATH5K_RATE_CODE_1M, },
154 { .bitrate = 20,
155 .hw_value = ATH5K_RATE_CODE_2M,
156 .hw_value_short = ATH5K_RATE_CODE_2M | AR5K_SET_SHORT_PREAMBLE,
157 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
158 { .bitrate = 55,
159 .hw_value = ATH5K_RATE_CODE_5_5M,
160 .hw_value_short = ATH5K_RATE_CODE_5_5M | AR5K_SET_SHORT_PREAMBLE,
161 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
162 { .bitrate = 110,
163 .hw_value = ATH5K_RATE_CODE_11M,
164 .hw_value_short = ATH5K_RATE_CODE_11M | AR5K_SET_SHORT_PREAMBLE,
165 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
166 { .bitrate = 60,
167 .hw_value = ATH5K_RATE_CODE_6M,
168 .flags = 0 },
169 { .bitrate = 90,
170 .hw_value = ATH5K_RATE_CODE_9M,
171 .flags = 0 },
172 { .bitrate = 120,
173 .hw_value = ATH5K_RATE_CODE_12M,
174 .flags = 0 },
175 { .bitrate = 180,
176 .hw_value = ATH5K_RATE_CODE_18M,
177 .flags = 0 },
178 { .bitrate = 240,
179 .hw_value = ATH5K_RATE_CODE_24M,
180 .flags = 0 },
181 { .bitrate = 360,
182 .hw_value = ATH5K_RATE_CODE_36M,
183 .flags = 0 },
184 { .bitrate = 480,
185 .hw_value = ATH5K_RATE_CODE_48M,
186 .flags = 0 },
187 { .bitrate = 540,
188 .hw_value = ATH5K_RATE_CODE_54M,
189 .flags = 0 },
190 /* XR missing */
191};
192
Bruno Randolf9e4e43f2010-06-16 19:11:17 +0900193static inline void ath5k_txbuf_free_skb(struct ath5k_softc *sc,
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200194 struct ath5k_buf *bf)
195{
196 BUG_ON(!bf);
197 if (!bf->skb)
198 return;
199 pci_unmap_single(sc->pdev, bf->skbaddr, bf->skb->len,
200 PCI_DMA_TODEVICE);
Jiri Slaby00482972008-08-18 21:45:27 +0200201 dev_kfree_skb_any(bf->skb);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200202 bf->skb = NULL;
Bruno Randolf39d63f22010-06-16 19:11:41 +0900203 bf->skbaddr = 0;
204 bf->desc->ds_data = 0;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200205}
206
Bruno Randolf9e4e43f2010-06-16 19:11:17 +0900207static inline void ath5k_rxbuf_free_skb(struct ath5k_softc *sc,
Felix Fietkaua6c8d372009-01-30 01:36:48 +0100208 struct ath5k_buf *bf)
209{
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -0800210 struct ath5k_hw *ah = sc->ah;
211 struct ath_common *common = ath5k_hw_common(ah);
212
Felix Fietkaua6c8d372009-01-30 01:36:48 +0100213 BUG_ON(!bf);
214 if (!bf->skb)
215 return;
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -0800216 pci_unmap_single(sc->pdev, bf->skbaddr, common->rx_bufsize,
Felix Fietkaua6c8d372009-01-30 01:36:48 +0100217 PCI_DMA_FROMDEVICE);
218 dev_kfree_skb_any(bf->skb);
219 bf->skb = NULL;
Bruno Randolf39d63f22010-06-16 19:11:41 +0900220 bf->skbaddr = 0;
221 bf->desc->ds_data = 0;
Felix Fietkaua6c8d372009-01-30 01:36:48 +0100222}
223
224
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200225static inline u64 ath5k_extend_tsf(struct ath5k_hw *ah, u32 rstamp)
226{
227 u64 tsf = ath5k_hw_get_tsf64(ah);
228
229 if ((tsf & 0x7fff) < rstamp)
230 tsf -= 0x8000;
231
232 return (tsf & ~0x7fff) | rstamp;
233}
234
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200235static const char *
236ath5k_chip_name(enum ath5k_srev_type type, u_int16_t val)
237{
238 const char *name = "xxxxx";
239 unsigned int i;
240
241 for (i = 0; i < ARRAY_SIZE(srev_names); i++) {
242 if (srev_names[i].sr_type != type)
243 continue;
Nick Kossifidis75d0edb2008-09-29 01:24:44 +0300244
245 if ((val & 0xf0) == srev_names[i].sr_val)
246 name = srev_names[i].sr_name;
247
248 if ((val & 0xff) == srev_names[i].sr_val) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200249 name = srev_names[i].sr_name;
250 break;
251 }
252 }
253
254 return name;
255}
Luis R. Rodrigueze5aa8472009-09-10 16:55:11 -0700256static unsigned int ath5k_ioread32(void *hw_priv, u32 reg_offset)
257{
258 struct ath5k_hw *ah = (struct ath5k_hw *) hw_priv;
259 return ath5k_hw_reg_read(ah, reg_offset);
260}
261
262static void ath5k_iowrite32(void *hw_priv, u32 val, u32 reg_offset)
263{
264 struct ath5k_hw *ah = (struct ath5k_hw *) hw_priv;
265 ath5k_hw_reg_write(ah, val, reg_offset);
266}
267
268static const struct ath_ops ath5k_common_ops = {
269 .read = ath5k_ioread32,
270 .write = ath5k_iowrite32,
271};
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200272
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200273/***********************\
274* Driver Initialization *
275\***********************/
276
Bob Copelandf769c362009-03-30 22:30:31 -0400277static int ath5k_reg_notifier(struct wiphy *wiphy, struct regulatory_request *request)
278{
279 struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
280 struct ath5k_softc *sc = hw->priv;
Luis R. Rodriguezdb719712009-09-10 11:20:57 -0700281 struct ath_regulatory *regulatory = ath5k_hw_regulatory(sc->ah);
Bob Copelandf769c362009-03-30 22:30:31 -0400282
Luis R. Rodriguez608b88c2009-08-17 18:07:23 -0700283 return ath_reg_notifier_apply(wiphy, request, regulatory);
Bob Copelandf769c362009-03-30 22:30:31 -0400284}
285
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200286/********************\
287* Channel/mode setup *
288\********************/
289
290/*
291 * Convert IEEE channel number to MHz frequency.
292 */
293static inline short
294ath5k_ieee2mhz(short chan)
295{
296 if (chan <= 14 || chan >= 27)
297 return ieee80211chan2mhz(chan);
298 else
299 return 2212 + chan * 20;
300}
301
Bob Copeland42639fc2009-03-30 08:05:29 -0400302/*
303 * Returns true for the channel numbers used without all_channels modparam.
304 */
305static bool ath5k_is_standard_channel(short chan)
306{
307 return ((chan <= 14) ||
308 /* UNII 1,2 */
309 ((chan & 3) == 0 && chan >= 36 && chan <= 64) ||
310 /* midband */
311 ((chan & 3) == 0 && chan >= 100 && chan <= 140) ||
312 /* UNII-3 */
313 ((chan & 3) == 1 && chan >= 149 && chan <= 165));
314}
315
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200316static unsigned int
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200317ath5k_copy_channels(struct ath5k_hw *ah,
318 struct ieee80211_channel *channels,
319 unsigned int mode,
320 unsigned int max)
321{
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500322 unsigned int i, count, size, chfreq, freq, ch;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200323
324 if (!test_bit(mode, ah->ah_modes))
325 return 0;
326
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200327 switch (mode) {
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500328 case AR5K_MODE_11A:
329 case AR5K_MODE_11A_TURBO:
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200330 /* 1..220, but 2GHz frequencies are filtered by check_channel */
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500331 size = 220 ;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200332 chfreq = CHANNEL_5GHZ;
333 break;
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500334 case AR5K_MODE_11B:
335 case AR5K_MODE_11G:
336 case AR5K_MODE_11G_TURBO:
337 size = 26;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200338 chfreq = CHANNEL_2GHZ;
339 break;
340 default:
341 ATH5K_WARN(ah->ah_sc, "bad mode, not copying channels\n");
342 return 0;
343 }
344
345 for (i = 0, count = 0; i < size && max > 0; i++) {
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500346 ch = i + 1 ;
347 freq = ath5k_ieee2mhz(ch);
348
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200349 /* Check if channel is supported by the chipset */
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500350 if (!ath5k_channel_ok(ah, freq, chfreq))
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200351 continue;
352
Bob Copeland42639fc2009-03-30 08:05:29 -0400353 if (!modparam_all_channels && !ath5k_is_standard_channel(ch))
354 continue;
355
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500356 /* Write channel info and increment counter */
357 channels[count].center_freq = freq;
Luis R. Rodrigueza3f4b912008-02-03 21:52:10 -0500358 channels[count].band = (chfreq == CHANNEL_2GHZ) ?
359 IEEE80211_BAND_2GHZ : IEEE80211_BAND_5GHZ;
Luis R. Rodriguez400ec452008-02-03 21:51:49 -0500360 switch (mode) {
361 case AR5K_MODE_11A:
362 case AR5K_MODE_11G:
363 channels[count].hw_value = chfreq | CHANNEL_OFDM;
364 break;
365 case AR5K_MODE_11A_TURBO:
366 case AR5K_MODE_11G_TURBO:
367 channels[count].hw_value = chfreq |
368 CHANNEL_OFDM | CHANNEL_TURBO;
369 break;
370 case AR5K_MODE_11B:
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500371 channels[count].hw_value = CHANNEL_B;
372 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200373
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200374 count++;
375 max--;
376 }
377
378 return count;
379}
380
Bruno Randolf63266a62008-07-30 17:12:58 +0200381static void
382ath5k_setup_rate_idx(struct ath5k_softc *sc, struct ieee80211_supported_band *b)
383{
384 u8 i;
385
386 for (i = 0; i < AR5K_MAX_RATES; i++)
387 sc->rate_idx[b->band][i] = -1;
388
389 for (i = 0; i < b->n_bitrates; i++) {
390 sc->rate_idx[b->band][b->bitrates[i].hw_value] = i;
391 if (b->bitrates[i].hw_value_short)
392 sc->rate_idx[b->band][b->bitrates[i].hw_value_short] = i;
393 }
394}
395
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200396static int
Bruno Randolf63266a62008-07-30 17:12:58 +0200397ath5k_setup_bands(struct ieee80211_hw *hw)
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200398{
399 struct ath5k_softc *sc = hw->priv;
400 struct ath5k_hw *ah = sc->ah;
Bruno Randolf63266a62008-07-30 17:12:58 +0200401 struct ieee80211_supported_band *sband;
402 int max_c, count_c = 0;
403 int i;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200404
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500405 BUILD_BUG_ON(ARRAY_SIZE(sc->sbands) < IEEE80211_NUM_BANDS);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200406 max_c = ARRAY_SIZE(sc->channels);
407
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500408 /* 2GHz band */
Bruno Randolf63266a62008-07-30 17:12:58 +0200409 sband = &sc->sbands[IEEE80211_BAND_2GHZ];
410 sband->band = IEEE80211_BAND_2GHZ;
411 sband->bitrates = &sc->rates[IEEE80211_BAND_2GHZ][0];
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200412
Bruno Randolf63266a62008-07-30 17:12:58 +0200413 if (test_bit(AR5K_MODE_11G, sc->ah->ah_capabilities.cap_mode)) {
414 /* G mode */
415 memcpy(sband->bitrates, &ath5k_rates[0],
416 sizeof(struct ieee80211_rate) * 12);
417 sband->n_bitrates = 12;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200418
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500419 sband->channels = sc->channels;
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500420 sband->n_channels = ath5k_copy_channels(ah, sband->channels,
Bruno Randolf63266a62008-07-30 17:12:58 +0200421 AR5K_MODE_11G, max_c);
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500422
423 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = sband;
Bruno Randolf63266a62008-07-30 17:12:58 +0200424 count_c = sband->n_channels;
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500425 max_c -= count_c;
Bruno Randolf63266a62008-07-30 17:12:58 +0200426 } else if (test_bit(AR5K_MODE_11B, sc->ah->ah_capabilities.cap_mode)) {
427 /* B mode */
428 memcpy(sband->bitrates, &ath5k_rates[0],
429 sizeof(struct ieee80211_rate) * 4);
430 sband->n_bitrates = 4;
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500431
Bruno Randolf63266a62008-07-30 17:12:58 +0200432 /* 5211 only supports B rates and uses 4bit rate codes
433 * (e.g normally we have 0x1B for 1M, but on 5211 we have 0x0B)
434 * fix them up here:
435 */
436 if (ah->ah_version == AR5K_AR5211) {
437 for (i = 0; i < 4; i++) {
438 sband->bitrates[i].hw_value =
439 sband->bitrates[i].hw_value & 0xF;
440 sband->bitrates[i].hw_value_short =
441 sband->bitrates[i].hw_value_short & 0xF;
442 }
443 }
444
445 sband->channels = sc->channels;
446 sband->n_channels = ath5k_copy_channels(ah, sband->channels,
447 AR5K_MODE_11B, max_c);
448
449 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = sband;
450 count_c = sband->n_channels;
451 max_c -= count_c;
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500452 }
Bruno Randolf63266a62008-07-30 17:12:58 +0200453 ath5k_setup_rate_idx(sc, sband);
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500454
Bruno Randolf63266a62008-07-30 17:12:58 +0200455 /* 5GHz band, A mode */
Luis R. Rodriguez400ec452008-02-03 21:51:49 -0500456 if (test_bit(AR5K_MODE_11A, sc->ah->ah_capabilities.cap_mode)) {
Bruno Randolf63266a62008-07-30 17:12:58 +0200457 sband = &sc->sbands[IEEE80211_BAND_5GHZ];
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500458 sband->band = IEEE80211_BAND_5GHZ;
Bruno Randolf63266a62008-07-30 17:12:58 +0200459 sband->bitrates = &sc->rates[IEEE80211_BAND_5GHZ][0];
460
461 memcpy(sband->bitrates, &ath5k_rates[4],
462 sizeof(struct ieee80211_rate) * 8);
463 sband->n_bitrates = 8;
464
465 sband->channels = &sc->channels[count_c];
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500466 sband->n_channels = ath5k_copy_channels(ah, sband->channels,
467 AR5K_MODE_11A, max_c);
468
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500469 hw->wiphy->bands[IEEE80211_BAND_5GHZ] = sband;
470 }
Bruno Randolf63266a62008-07-30 17:12:58 +0200471 ath5k_setup_rate_idx(sc, sband);
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500472
Luis R. Rodriguezb4461972008-02-04 10:03:54 -0500473 ath5k_debug_dump_bands(sc);
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500474
475 return 0;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200476}
477
478/*
Joerg Alberte30eb4a2009-08-05 01:52:07 +0200479 * Set/change channels. We always reset the chip.
480 * To accomplish this we must first cleanup any pending DMA,
481 * then restart stuff after a la ath5k_init.
Bob Copelandbe009372009-01-22 08:44:16 -0500482 *
483 * Called with sc->lock.
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200484 */
485static int
486ath5k_chan_set(struct ath5k_softc *sc, struct ieee80211_channel *chan)
487{
Bruno Randolf8d67a032010-06-16 19:11:12 +0900488 ATH5K_DBG(sc, ATH5K_DEBUG_RESET,
489 "channel set, resetting (%u -> %u MHz)\n",
490 sc->curchan->center_freq, chan->center_freq);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200491
Joerg Alberte30eb4a2009-08-05 01:52:07 +0200492 /*
493 * To switch channels clear any pending DMA operations;
494 * wait long enough for the RX fifo to drain, reset the
495 * hardware at the new frequency, and then re-enable
496 * the relevant bits of the h/w.
497 */
498 return ath5k_reset(sc, chan);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200499}
500
501static void
502ath5k_setcurmode(struct ath5k_softc *sc, unsigned int mode)
503{
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200504 sc->curmode = mode;
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500505
Luis R. Rodriguez400ec452008-02-03 21:51:49 -0500506 if (mode == AR5K_MODE_11A) {
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500507 sc->curband = &sc->sbands[IEEE80211_BAND_5GHZ];
508 } else {
509 sc->curband = &sc->sbands[IEEE80211_BAND_2GHZ];
510 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200511}
512
Ben Greearb1ae1ed2010-09-30 12:22:58 -0700513struct ath_vif_iter_data {
514 const u8 *hw_macaddr;
515 u8 mask[ETH_ALEN];
516 u8 active_mac[ETH_ALEN]; /* first active MAC */
517 bool need_set_hw_addr;
518 bool found_active;
519 bool any_assoc;
520};
521
522static void ath_vif_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
523{
524 struct ath_vif_iter_data *iter_data = data;
525 int i;
526
527 if (iter_data->hw_macaddr)
528 for (i = 0; i < ETH_ALEN; i++)
529 iter_data->mask[i] &=
530 ~(iter_data->hw_macaddr[i] ^ mac[i]);
531
532 if (!iter_data->found_active) {
533 iter_data->found_active = true;
534 memcpy(iter_data->active_mac, mac, ETH_ALEN);
535 }
536
537 if (iter_data->need_set_hw_addr && iter_data->hw_macaddr)
538 if (compare_ether_addr(iter_data->hw_macaddr, mac) == 0)
539 iter_data->need_set_hw_addr = false;
540
541 if (!iter_data->any_assoc) {
542 struct ath5k_vif *avf = (void *)vif->drv_priv;
543 if (avf->assoc)
544 iter_data->any_assoc = true;
545 }
546}
547
548void ath5k_update_bssid_mask(struct ath5k_softc *sc, struct ieee80211_vif *vif)
549{
550 struct ath_common *common = ath5k_hw_common(sc->ah);
551 struct ath_vif_iter_data iter_data;
552
553 /*
554 * Use the hardware MAC address as reference, the hardware uses it
555 * together with the BSSID mask when matching addresses.
556 */
557 iter_data.hw_macaddr = common->macaddr;
558 memset(&iter_data.mask, 0xff, ETH_ALEN);
559 iter_data.found_active = false;
560 iter_data.need_set_hw_addr = true;
561
562 if (vif)
563 ath_vif_iter(&iter_data, vif->addr, vif);
564
565 /* Get list of all active MAC addresses */
566 ieee80211_iterate_active_interfaces_atomic(sc->hw, ath_vif_iter,
567 &iter_data);
568 memcpy(sc->bssidmask, iter_data.mask, ETH_ALEN);
569
570 if (iter_data.need_set_hw_addr && iter_data.found_active)
571 ath5k_hw_set_lladdr(sc->ah, iter_data.active_mac);
572
573 ath5k_hw_set_bssid_mask(sc->ah, sc->bssidmask);
574}
575
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200576static void
Ben Greearb1ae1ed2010-09-30 12:22:58 -0700577ath5k_mode_setup(struct ath5k_softc *sc, struct ieee80211_vif *vif)
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200578{
579 struct ath5k_hw *ah = sc->ah;
580 u32 rfilt;
581
582 /* configure rx filter */
583 rfilt = sc->filter_flags;
584 ath5k_hw_set_rx_filter(ah, rfilt);
585
586 if (ath5k_hw_hasbssidmask(ah))
Ben Greearb1ae1ed2010-09-30 12:22:58 -0700587 ath5k_update_bssid_mask(sc, vif);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200588
589 /* configure operational mode */
Bruno Randolfccfe5552010-03-09 16:55:38 +0900590 ath5k_hw_set_opmode(ah, sc->opmode);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200591
Bruno Randolfccfe5552010-03-09 16:55:38 +0900592 ATH5K_DBG(sc, ATH5K_DEBUG_MODE, "mode setup opmode %d\n", sc->opmode);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200593 ATH5K_DBG(sc, ATH5K_DEBUG_MODE, "RX filter 0x%x\n", rfilt);
594}
595
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500596static inline int
Bruno Randolf63266a62008-07-30 17:12:58 +0200597ath5k_hw_to_driver_rix(struct ath5k_softc *sc, int hw_rix)
598{
Bob Copelandb7266042009-03-02 21:55:18 -0500599 int rix;
600
601 /* return base rate on errors */
602 if (WARN(hw_rix < 0 || hw_rix >= AR5K_MAX_RATES,
603 "hw_rix out of bounds: %x\n", hw_rix))
604 return 0;
605
606 rix = sc->rate_idx[sc->curband->band][hw_rix];
607 if (WARN(rix < 0, "invalid hw_rix: %x\n", hw_rix))
608 rix = 0;
609
610 return rix;
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500611}
612
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200613/***************\
614* Buffers setup *
615\***************/
616
Bob Copelandb6ea0352009-01-10 14:42:54 -0500617static
618struct sk_buff *ath5k_rx_skb_alloc(struct ath5k_softc *sc, dma_addr_t *skb_addr)
619{
Luis R. Rodriguezdb719712009-09-10 11:20:57 -0700620 struct ath_common *common = ath5k_hw_common(sc->ah);
Bob Copelandb6ea0352009-01-10 14:42:54 -0500621 struct sk_buff *skb;
Bob Copelandb6ea0352009-01-10 14:42:54 -0500622
623 /*
624 * Allocate buffer with headroom_needed space for the
625 * fake physical layer header at the start.
626 */
Luis R. Rodriguezdb719712009-09-10 11:20:57 -0700627 skb = ath_rxbuf_alloc(common,
Luis R. Rodriguezdd849782009-11-04 09:44:50 -0800628 common->rx_bufsize,
Luis R. Rodriguezaeb63cf2009-08-12 09:57:00 -0700629 GFP_ATOMIC);
Bob Copelandb6ea0352009-01-10 14:42:54 -0500630
631 if (!skb) {
632 ATH5K_ERR(sc, "can't alloc skbuff of size %u\n",
Luis R. Rodriguezdd849782009-11-04 09:44:50 -0800633 common->rx_bufsize);
Bob Copelandb6ea0352009-01-10 14:42:54 -0500634 return NULL;
635 }
Bob Copelandb6ea0352009-01-10 14:42:54 -0500636
637 *skb_addr = pci_map_single(sc->pdev,
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -0800638 skb->data, common->rx_bufsize,
639 PCI_DMA_FROMDEVICE);
Bob Copelandb6ea0352009-01-10 14:42:54 -0500640 if (unlikely(pci_dma_mapping_error(sc->pdev, *skb_addr))) {
641 ATH5K_ERR(sc, "%s: DMA mapping failed\n", __func__);
642 dev_kfree_skb(skb);
643 return NULL;
644 }
645 return skb;
646}
647
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200648static int
649ath5k_rxbuf_setup(struct ath5k_softc *sc, struct ath5k_buf *bf)
650{
651 struct ath5k_hw *ah = sc->ah;
652 struct sk_buff *skb = bf->skb;
653 struct ath5k_desc *ds;
Bruno Randolfb5eae9f2010-05-19 10:18:16 +0900654 int ret;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200655
Bob Copelandb6ea0352009-01-10 14:42:54 -0500656 if (!skb) {
657 skb = ath5k_rx_skb_alloc(sc, &bf->skbaddr);
658 if (!skb)
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200659 return -ENOMEM;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200660 bf->skb = skb;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200661 }
662
663 /*
664 * Setup descriptors. For receive we always terminate
665 * the descriptor list with a self-linked entry so we'll
666 * not get overrun under high load (as can happen with a
667 * 5212 when ANI processing enables PHY error frames).
668 *
Bruno Randolfbeade632010-06-16 19:11:25 +0900669 * To ensure the last descriptor is self-linked we create
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200670 * each descriptor as self-linked and add it to the end. As
671 * each additional descriptor is added the previous self-linked
Bruno Randolfbeade632010-06-16 19:11:25 +0900672 * entry is "fixed" naturally. This should be safe even
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200673 * if DMA is happening. When processing RX interrupts we
674 * never remove/process the last, self-linked, entry on the
Bruno Randolfbeade632010-06-16 19:11:25 +0900675 * descriptor list. This ensures the hardware always has
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200676 * someplace to write a new frame.
677 */
678 ds = bf->desc;
679 ds->ds_link = bf->daddr; /* link to self */
680 ds->ds_data = bf->skbaddr;
Bruno Randolfa6668192010-06-16 19:12:01 +0900681 ret = ath5k_hw_setup_rx_desc(ah, ds, ah->common.rx_bufsize, 0);
Bruno Randolf0452d4a2010-06-16 19:11:35 +0900682 if (ret) {
683 ATH5K_ERR(sc, "%s: could not setup RX desc\n", __func__);
Bruno Randolfb5eae9f2010-05-19 10:18:16 +0900684 return ret;
Bruno Randolf0452d4a2010-06-16 19:11:35 +0900685 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200686
687 if (sc->rxlink != NULL)
688 *sc->rxlink = bf->daddr;
689 sc->rxlink = &ds->ds_link;
690 return 0;
691}
692
Bob Copeland2ac29272010-02-09 13:06:54 -0500693static enum ath5k_pkt_type get_hw_packet_type(struct sk_buff *skb)
694{
695 struct ieee80211_hdr *hdr;
696 enum ath5k_pkt_type htype;
697 __le16 fc;
698
699 hdr = (struct ieee80211_hdr *)skb->data;
700 fc = hdr->frame_control;
701
702 if (ieee80211_is_beacon(fc))
703 htype = AR5K_PKT_TYPE_BEACON;
704 else if (ieee80211_is_probe_resp(fc))
705 htype = AR5K_PKT_TYPE_PROBE_RESP;
706 else if (ieee80211_is_atim(fc))
707 htype = AR5K_PKT_TYPE_ATIM;
708 else if (ieee80211_is_pspoll(fc))
709 htype = AR5K_PKT_TYPE_PSPOLL;
710 else
711 htype = AR5K_PKT_TYPE_NORMAL;
712
713 return htype;
714}
715
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200716static int
Bob Copelandcec8db22009-07-04 12:59:51 -0400717ath5k_txbuf_setup(struct ath5k_softc *sc, struct ath5k_buf *bf,
Benoit Papillault8127fbd2010-02-27 23:05:26 +0100718 struct ath5k_txq *txq, int padsize)
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200719{
720 struct ath5k_hw *ah = sc->ah;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200721 struct ath5k_desc *ds = bf->desc;
722 struct sk_buff *skb = bf->skb;
Johannes Berga888d522008-05-26 16:43:39 +0200723 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200724 unsigned int pktlen, flags, keyidx = AR5K_TXKEYIX_INVALID;
Felix Fietkau2f7fe872008-10-05 18:05:48 +0200725 struct ieee80211_rate *rate;
726 unsigned int mrr_rate[3], mrr_tries[3];
727 int i, ret;
Bob Copeland8902ff42009-01-22 08:44:20 -0500728 u16 hw_rate;
Bob Copeland07c1e852009-01-22 08:44:21 -0500729 u16 cts_rate = 0;
730 u16 duration = 0;
Bob Copeland8902ff42009-01-22 08:44:20 -0500731 u8 rc_flags;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200732
733 flags = AR5K_TXDESC_INTREQ | AR5K_TXDESC_CLRDMASK;
Johannes Berge039fa42008-05-15 12:55:29 +0200734
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200735 /* XXX endianness */
736 bf->skbaddr = pci_map_single(sc->pdev, skb->data, skb->len,
737 PCI_DMA_TODEVICE);
738
Bob Copeland8902ff42009-01-22 08:44:20 -0500739 rate = ieee80211_get_tx_rate(sc->hw, info);
John W. Linvilled8e1ba72010-08-24 15:27:34 -0400740 if (!rate) {
741 ret = -EINVAL;
742 goto err_unmap;
743 }
Bob Copeland8902ff42009-01-22 08:44:20 -0500744
Johannes Berge039fa42008-05-15 12:55:29 +0200745 if (info->flags & IEEE80211_TX_CTL_NO_ACK)
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200746 flags |= AR5K_TXDESC_NOACK;
747
Bob Copeland8902ff42009-01-22 08:44:20 -0500748 rc_flags = info->control.rates[0].flags;
749 hw_rate = (rc_flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE) ?
750 rate->hw_value_short : rate->hw_value;
751
Bruno Randolf281c56d2008-02-05 18:44:55 +0900752 pktlen = skb->len;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200753
Nick Kossifidis8f655dd2009-03-15 22:20:35 +0200754 /* FIXME: If we are in g mode and rate is a CCK rate
755 * subtract ah->ah_txpower.txp_cck_ofdm_pwr_delta
756 * from tx power (value is in dB units already) */
Bob Copeland362695e2009-02-15 12:06:12 -0500757 if (info->control.hw_key) {
758 keyidx = info->control.hw_key->hw_key_idx;
759 pktlen += info->control.hw_key->icv_len;
760 }
Bob Copeland07c1e852009-01-22 08:44:21 -0500761 if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
762 flags |= AR5K_TXDESC_RTSENA;
763 cts_rate = ieee80211_get_rts_cts_rate(sc->hw, info)->hw_value;
764 duration = le16_to_cpu(ieee80211_rts_duration(sc->hw,
Ben Greearb1ae1ed2010-09-30 12:22:58 -0700765 info->control.vif, pktlen, info));
Bob Copeland07c1e852009-01-22 08:44:21 -0500766 }
767 if (rc_flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
768 flags |= AR5K_TXDESC_CTSENA;
769 cts_rate = ieee80211_get_rts_cts_rate(sc->hw, info)->hw_value;
770 duration = le16_to_cpu(ieee80211_ctstoself_duration(sc->hw,
Ben Greearb1ae1ed2010-09-30 12:22:58 -0700771 info->control.vif, pktlen, info));
Bob Copeland07c1e852009-01-22 08:44:21 -0500772 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200773 ret = ah->ah_setup_tx_desc(ah, ds, pktlen,
Benoit Papillault8127fbd2010-02-27 23:05:26 +0100774 ieee80211_get_hdrlen_from_skb(skb), padsize,
Bob Copeland2ac29272010-02-09 13:06:54 -0500775 get_hw_packet_type(skb),
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200776 (sc->power_level * 2),
Bob Copeland8902ff42009-01-22 08:44:20 -0500777 hw_rate,
Nick Kossifidis2bed03e2009-04-30 15:55:49 -0400778 info->control.rates[0].count, keyidx, ah->ah_tx_ant, flags,
Bob Copeland07c1e852009-01-22 08:44:21 -0500779 cts_rate, duration);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200780 if (ret)
781 goto err_unmap;
782
Felix Fietkau2f7fe872008-10-05 18:05:48 +0200783 memset(mrr_rate, 0, sizeof(mrr_rate));
784 memset(mrr_tries, 0, sizeof(mrr_tries));
785 for (i = 0; i < 3; i++) {
786 rate = ieee80211_get_alt_retry_rate(sc->hw, info, i);
787 if (!rate)
788 break;
789
790 mrr_rate[i] = rate->hw_value;
Johannes Berge6a98542008-10-21 12:40:02 +0200791 mrr_tries[i] = info->control.rates[i + 1].count;
Felix Fietkau2f7fe872008-10-05 18:05:48 +0200792 }
793
Bruno Randolfa6668192010-06-16 19:12:01 +0900794 ath5k_hw_setup_mrr_tx_desc(ah, ds,
Felix Fietkau2f7fe872008-10-05 18:05:48 +0200795 mrr_rate[0], mrr_tries[0],
796 mrr_rate[1], mrr_tries[1],
797 mrr_rate[2], mrr_tries[2]);
798
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200799 ds->ds_link = 0;
800 ds->ds_data = bf->skbaddr;
801
802 spin_lock_bh(&txq->lock);
803 list_add_tail(&bf->list, &txq->q);
Bruno Randolf925e0b02010-09-17 11:36:35 +0900804 txq->txq_len++;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200805 if (txq->link == NULL) /* is this first packet? */
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300806 ath5k_hw_set_txdp(ah, txq->qnum, bf->daddr);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200807 else /* no, so only link it */
808 *txq->link = bf->daddr;
809
810 txq->link = &ds->ds_link;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300811 ath5k_hw_start_tx_dma(ah, txq->qnum);
Jiri Slaby274c7c32008-07-15 17:44:20 +0200812 mmiowb();
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200813 spin_unlock_bh(&txq->lock);
814
815 return 0;
816err_unmap:
817 pci_unmap_single(sc->pdev, bf->skbaddr, skb->len, PCI_DMA_TODEVICE);
818 return ret;
819}
820
821/*******************\
822* Descriptors setup *
823\*******************/
824
825static int
826ath5k_desc_alloc(struct ath5k_softc *sc, struct pci_dev *pdev)
827{
828 struct ath5k_desc *ds;
829 struct ath5k_buf *bf;
830 dma_addr_t da;
831 unsigned int i;
832 int ret;
833
834 /* allocate descriptors */
835 sc->desc_len = sizeof(struct ath5k_desc) *
836 (ATH_TXBUF + ATH_RXBUF + ATH_BCBUF + 1);
837 sc->desc = pci_alloc_consistent(pdev, sc->desc_len, &sc->desc_daddr);
838 if (sc->desc == NULL) {
839 ATH5K_ERR(sc, "can't allocate descriptors\n");
840 ret = -ENOMEM;
841 goto err;
842 }
843 ds = sc->desc;
844 da = sc->desc_daddr;
845 ATH5K_DBG(sc, ATH5K_DEBUG_ANY, "DMA map: %p (%zu) -> %llx\n",
846 ds, sc->desc_len, (unsigned long long)sc->desc_daddr);
847
848 bf = kcalloc(1 + ATH_TXBUF + ATH_RXBUF + ATH_BCBUF,
849 sizeof(struct ath5k_buf), GFP_KERNEL);
850 if (bf == NULL) {
851 ATH5K_ERR(sc, "can't allocate bufptr\n");
852 ret = -ENOMEM;
853 goto err_free;
854 }
855 sc->bufptr = bf;
856
857 INIT_LIST_HEAD(&sc->rxbuf);
858 for (i = 0; i < ATH_RXBUF; i++, bf++, ds++, da += sizeof(*ds)) {
859 bf->desc = ds;
860 bf->daddr = da;
861 list_add_tail(&bf->list, &sc->rxbuf);
862 }
863
864 INIT_LIST_HEAD(&sc->txbuf);
865 sc->txbuf_len = ATH_TXBUF;
866 for (i = 0; i < ATH_TXBUF; i++, bf++, ds++,
867 da += sizeof(*ds)) {
868 bf->desc = ds;
869 bf->daddr = da;
870 list_add_tail(&bf->list, &sc->txbuf);
871 }
872
Ben Greearb1ae1ed2010-09-30 12:22:58 -0700873 /* beacon buffers */
874 INIT_LIST_HEAD(&sc->bcbuf);
875 for (i = 0; i < ATH_BCBUF; i++, bf++, ds++, da += sizeof(*ds)) {
876 bf->desc = ds;
877 bf->daddr = da;
878 list_add_tail(&bf->list, &sc->bcbuf);
879 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200880
881 return 0;
882err_free:
883 pci_free_consistent(pdev, sc->desc_len, sc->desc, sc->desc_daddr);
884err:
885 sc->desc = NULL;
886 return ret;
887}
888
889static void
890ath5k_desc_free(struct ath5k_softc *sc, struct pci_dev *pdev)
891{
892 struct ath5k_buf *bf;
893
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200894 list_for_each_entry(bf, &sc->txbuf, list)
Bruno Randolf9e4e43f2010-06-16 19:11:17 +0900895 ath5k_txbuf_free_skb(sc, bf);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200896 list_for_each_entry(bf, &sc->rxbuf, list)
Bruno Randolf9e4e43f2010-06-16 19:11:17 +0900897 ath5k_rxbuf_free_skb(sc, bf);
Ben Greearb1ae1ed2010-09-30 12:22:58 -0700898 list_for_each_entry(bf, &sc->bcbuf, list)
899 ath5k_txbuf_free_skb(sc, bf);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200900
901 /* Free memory associated with all descriptors */
902 pci_free_consistent(pdev, sc->desc_len, sc->desc, sc->desc_daddr);
Bruno Randolf39d63f22010-06-16 19:11:41 +0900903 sc->desc = NULL;
904 sc->desc_daddr = 0;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200905
906 kfree(sc->bufptr);
907 sc->bufptr = NULL;
908}
909
910
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200911/**************\
912* Queues setup *
913\**************/
914
915static struct ath5k_txq *
916ath5k_txq_setup(struct ath5k_softc *sc,
917 int qtype, int subtype)
918{
919 struct ath5k_hw *ah = sc->ah;
920 struct ath5k_txq *txq;
921 struct ath5k_txq_info qi = {
922 .tqi_subtype = subtype,
Bruno Randolfde8af452010-09-17 11:37:12 +0900923 /* XXX: default values not correct for B and XR channels,
924 * but who cares? */
925 .tqi_aifs = AR5K_TUNE_AIFS,
926 .tqi_cw_min = AR5K_TUNE_CWMIN,
927 .tqi_cw_max = AR5K_TUNE_CWMAX
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200928 };
929 int qnum;
930
931 /*
932 * Enable interrupts only for EOL and DESC conditions.
933 * We mark tx descriptors to receive a DESC interrupt
Bob Copelanda180a132010-08-15 13:03:12 -0400934 * when a tx queue gets deep; otherwise we wait for the
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200935 * EOL to reap descriptors. Note that this is done to
936 * reduce interrupt load and this only defers reaping
937 * descriptors, never transmitting frames. Aside from
938 * reducing interrupts this also permits more concurrency.
939 * The only potential downside is if the tx queue backs
940 * up in which case the top half of the kernel may backup
941 * due to a lack of tx descriptors.
942 */
943 qi.tqi_flags = AR5K_TXQ_FLAG_TXEOLINT_ENABLE |
944 AR5K_TXQ_FLAG_TXDESCINT_ENABLE;
945 qnum = ath5k_hw_setup_tx_queue(ah, qtype, &qi);
946 if (qnum < 0) {
947 /*
948 * NB: don't print a message, this happens
949 * normally on parts with too few tx queues
950 */
951 return ERR_PTR(qnum);
952 }
953 if (qnum >= ARRAY_SIZE(sc->txqs)) {
954 ATH5K_ERR(sc, "hw qnum %u out of range, max %tu!\n",
955 qnum, ARRAY_SIZE(sc->txqs));
956 ath5k_hw_release_tx_queue(ah, qnum);
957 return ERR_PTR(-EINVAL);
958 }
959 txq = &sc->txqs[qnum];
960 if (!txq->setup) {
961 txq->qnum = qnum;
962 txq->link = NULL;
963 INIT_LIST_HEAD(&txq->q);
964 spin_lock_init(&txq->lock);
965 txq->setup = true;
Bruno Randolf925e0b02010-09-17 11:36:35 +0900966 txq->txq_len = 0;
Bruno Randolf4edd7612010-09-17 11:36:56 +0900967 txq->txq_poll_mark = false;
Bruno Randolf923e5b32010-09-17 11:37:02 +0900968 txq->txq_stuck = 0;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200969 }
970 return &sc->txqs[qnum];
971}
972
973static int
974ath5k_beaconq_setup(struct ath5k_hw *ah)
975{
976 struct ath5k_txq_info qi = {
Bruno Randolfde8af452010-09-17 11:37:12 +0900977 /* XXX: default values not correct for B and XR channels,
978 * but who cares? */
979 .tqi_aifs = AR5K_TUNE_AIFS,
980 .tqi_cw_min = AR5K_TUNE_CWMIN,
981 .tqi_cw_max = AR5K_TUNE_CWMAX,
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200982 /* NB: for dynamic turbo, don't enable any other interrupts */
983 .tqi_flags = AR5K_TXQ_FLAG_TXDESCINT_ENABLE
984 };
985
986 return ath5k_hw_setup_tx_queue(ah, AR5K_TX_QUEUE_BEACON, &qi);
987}
988
989static int
990ath5k_beaconq_config(struct ath5k_softc *sc)
991{
992 struct ath5k_hw *ah = sc->ah;
993 struct ath5k_txq_info qi;
994 int ret;
995
996 ret = ath5k_hw_get_tx_queueprops(ah, sc->bhalq, &qi);
997 if (ret)
Bob Copelanda951ae22010-01-20 23:51:04 -0500998 goto err;
999
Johannes Berg05c914f2008-09-11 00:01:58 +02001000 if (sc->opmode == NL80211_IFTYPE_AP ||
1001 sc->opmode == NL80211_IFTYPE_MESH_POINT) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001002 /*
1003 * Always burst out beacon and CAB traffic
1004 * (aifs = cwmin = cwmax = 0)
1005 */
1006 qi.tqi_aifs = 0;
1007 qi.tqi_cw_min = 0;
1008 qi.tqi_cw_max = 0;
Johannes Berg05c914f2008-09-11 00:01:58 +02001009 } else if (sc->opmode == NL80211_IFTYPE_ADHOC) {
Bruno Randolf6d91e1d2008-01-19 18:18:41 +09001010 /*
1011 * Adhoc mode; backoff between 0 and (2 * cw_min).
1012 */
1013 qi.tqi_aifs = 0;
1014 qi.tqi_cw_min = 0;
Bruno Randolfde8af452010-09-17 11:37:12 +09001015 qi.tqi_cw_max = 2 * AR5K_TUNE_CWMIN;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001016 }
1017
Bruno Randolf6d91e1d2008-01-19 18:18:41 +09001018 ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
1019 "beacon queueprops tqi_aifs:%d tqi_cw_min:%d tqi_cw_max:%d\n",
1020 qi.tqi_aifs, qi.tqi_cw_min, qi.tqi_cw_max);
1021
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001022 ret = ath5k_hw_set_tx_queueprops(ah, sc->bhalq, &qi);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001023 if (ret) {
1024 ATH5K_ERR(sc, "%s: unable to update parameters for beacon "
1025 "hardware queue!\n", __func__);
Bob Copelanda951ae22010-01-20 23:51:04 -05001026 goto err;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001027 }
Bob Copelanda951ae22010-01-20 23:51:04 -05001028 ret = ath5k_hw_reset_tx_queue(ah, sc->bhalq); /* push to h/w */
1029 if (ret)
1030 goto err;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001031
Bob Copelanda951ae22010-01-20 23:51:04 -05001032 /* reconfigure cabq with ready time to 80% of beacon_interval */
1033 ret = ath5k_hw_get_tx_queueprops(ah, AR5K_TX_QUEUE_ID_CAB, &qi);
1034 if (ret)
1035 goto err;
1036
1037 qi.tqi_ready_time = (sc->bintval * 80) / 100;
1038 ret = ath5k_hw_set_tx_queueprops(ah, AR5K_TX_QUEUE_ID_CAB, &qi);
1039 if (ret)
1040 goto err;
1041
1042 ret = ath5k_hw_reset_tx_queue(ah, AR5K_TX_QUEUE_ID_CAB);
1043err:
1044 return ret;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001045}
1046
1047static void
1048ath5k_txq_drainq(struct ath5k_softc *sc, struct ath5k_txq *txq)
1049{
1050 struct ath5k_buf *bf, *bf0;
1051
1052 /*
1053 * NB: this assumes output has been stopped and
1054 * we do not need to block ath5k_tx_tasklet
1055 */
1056 spin_lock_bh(&txq->lock);
1057 list_for_each_entry_safe(bf, bf0, &txq->q, list) {
Bruno Randolfb47f4072008-03-05 18:35:45 +09001058 ath5k_debug_printtxbuf(sc, bf);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001059
Bruno Randolf9e4e43f2010-06-16 19:11:17 +09001060 ath5k_txbuf_free_skb(sc, bf);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001061
1062 spin_lock_bh(&sc->txbuflock);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001063 list_move_tail(&bf->list, &sc->txbuf);
1064 sc->txbuf_len++;
Bruno Randolf925e0b02010-09-17 11:36:35 +09001065 txq->txq_len--;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001066 spin_unlock_bh(&sc->txbuflock);
1067 }
1068 txq->link = NULL;
Bruno Randolf4edd7612010-09-17 11:36:56 +09001069 txq->txq_poll_mark = false;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001070 spin_unlock_bh(&txq->lock);
1071}
1072
1073/*
1074 * Drain the transmit queues and reclaim resources.
1075 */
1076static void
1077ath5k_txq_cleanup(struct ath5k_softc *sc)
1078{
1079 struct ath5k_hw *ah = sc->ah;
1080 unsigned int i;
1081
1082 /* XXX return value */
1083 if (likely(!test_bit(ATH_STAT_INVALID, sc->status))) {
1084 /* don't touch the hardware if marked invalid */
1085 ath5k_hw_stop_tx_dma(ah, sc->bhalq);
1086 ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "beacon queue %x\n",
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001087 ath5k_hw_get_txdp(ah, sc->bhalq));
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001088 for (i = 0; i < ARRAY_SIZE(sc->txqs); i++)
1089 if (sc->txqs[i].setup) {
1090 ath5k_hw_stop_tx_dma(ah, sc->txqs[i].qnum);
1091 ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "txq [%u] %x, "
1092 "link %p\n",
1093 sc->txqs[i].qnum,
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001094 ath5k_hw_get_txdp(ah,
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001095 sc->txqs[i].qnum),
1096 sc->txqs[i].link);
1097 }
1098 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001099
1100 for (i = 0; i < ARRAY_SIZE(sc->txqs); i++)
1101 if (sc->txqs[i].setup)
1102 ath5k_txq_drainq(sc, &sc->txqs[i]);
1103}
1104
1105static void
1106ath5k_txq_release(struct ath5k_softc *sc)
1107{
1108 struct ath5k_txq *txq = sc->txqs;
1109 unsigned int i;
1110
1111 for (i = 0; i < ARRAY_SIZE(sc->txqs); i++, txq++)
1112 if (txq->setup) {
1113 ath5k_hw_release_tx_queue(sc->ah, txq->qnum);
1114 txq->setup = false;
1115 }
1116}
1117
1118
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001119/*************\
1120* RX Handling *
1121\*************/
1122
1123/*
1124 * Enable the receive h/w following a reset.
1125 */
1126static int
1127ath5k_rx_start(struct ath5k_softc *sc)
1128{
1129 struct ath5k_hw *ah = sc->ah;
Luis R. Rodriguezdb719712009-09-10 11:20:57 -07001130 struct ath_common *common = ath5k_hw_common(ah);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001131 struct ath5k_buf *bf;
1132 int ret;
1133
Nick Kossifidisb6127982010-08-15 13:03:11 -04001134 common->rx_bufsize = roundup(IEEE80211_MAX_FRAME_LEN, common->cachelsz);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001135
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -08001136 ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "cachelsz %u rx_bufsize %u\n",
1137 common->cachelsz, common->rx_bufsize);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001138
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001139 spin_lock_bh(&sc->rxbuflock);
Bob Copeland26925042009-04-15 07:57:36 -04001140 sc->rxlink = NULL;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001141 list_for_each_entry(bf, &sc->rxbuf, list) {
1142 ret = ath5k_rxbuf_setup(sc, bf);
1143 if (ret != 0) {
1144 spin_unlock_bh(&sc->rxbuflock);
1145 goto err;
1146 }
1147 }
1148 bf = list_first_entry(&sc->rxbuf, struct ath5k_buf, list);
Bob Copeland26925042009-04-15 07:57:36 -04001149 ath5k_hw_set_rxdp(ah, bf->daddr);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001150 spin_unlock_bh(&sc->rxbuflock);
1151
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001152 ath5k_hw_start_rx_dma(ah); /* enable recv descriptors */
Ben Greearb1ae1ed2010-09-30 12:22:58 -07001153 ath5k_mode_setup(sc, NULL); /* set filters, etc. */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001154 ath5k_hw_start_rx_pcu(ah); /* re-enable PCU/DMA engine */
1155
1156 return 0;
1157err:
1158 return ret;
1159}
1160
1161/*
1162 * Disable the receive h/w in preparation for a reset.
1163 */
1164static void
1165ath5k_rx_stop(struct ath5k_softc *sc)
1166{
1167 struct ath5k_hw *ah = sc->ah;
1168
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001169 ath5k_hw_stop_rx_pcu(ah); /* disable PCU */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001170 ath5k_hw_set_rx_filter(ah, 0); /* clear recv filter */
1171 ath5k_hw_stop_rx_dma(ah); /* disable DMA engine */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001172
1173 ath5k_debug_printrxbuffs(sc, ah);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001174}
1175
1176static unsigned int
Bruno Randolf8a89f062010-06-16 19:11:51 +09001177ath5k_rx_decrypted(struct ath5k_softc *sc, struct sk_buff *skb,
1178 struct ath5k_rx_status *rs)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001179{
Luis R. Rodriguezdc1e0012009-11-04 17:47:31 -08001180 struct ath5k_hw *ah = sc->ah;
1181 struct ath_common *common = ath5k_hw_common(ah);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001182 struct ieee80211_hdr *hdr = (void *)skb->data;
Harvey Harrison798ee982008-07-15 18:44:02 -07001183 unsigned int keyix, hlen;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001184
Bruno Randolfb47f4072008-03-05 18:35:45 +09001185 if (!(rs->rs_status & AR5K_RXERR_DECRYPT) &&
1186 rs->rs_keyix != AR5K_RXKEYIX_INVALID)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001187 return RX_FLAG_DECRYPTED;
1188
1189 /* Apparently when a default key is used to decrypt the packet
1190 the hw does not set the index used to decrypt. In such cases
1191 get the index from the packet. */
Harvey Harrison798ee982008-07-15 18:44:02 -07001192 hlen = ieee80211_hdrlen(hdr->frame_control);
Harvey Harrison24b56e72008-06-14 23:33:38 -07001193 if (ieee80211_has_protected(hdr->frame_control) &&
1194 !(rs->rs_status & AR5K_RXERR_DECRYPT) &&
1195 skb->len >= hlen + 4) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001196 keyix = skb->data[hlen + 3] >> 6;
1197
Luis R. Rodriguezdc1e0012009-11-04 17:47:31 -08001198 if (test_bit(keyix, common->keymap))
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001199 return RX_FLAG_DECRYPTED;
1200 }
1201
1202 return 0;
1203}
1204
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001205
1206static void
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001207ath5k_check_ibss_tsf(struct ath5k_softc *sc, struct sk_buff *skb,
1208 struct ieee80211_rx_status *rxs)
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001209{
Luis R. Rodriguez954fece2009-09-10 10:51:33 -07001210 struct ath_common *common = ath5k_hw_common(sc->ah);
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001211 u64 tsf, bc_tstamp;
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001212 u32 hw_tu;
1213 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)skb->data;
1214
Harvey Harrison24b56e72008-06-14 23:33:38 -07001215 if (ieee80211_is_beacon(mgmt->frame_control) &&
Pavel Roskin38c07b42008-02-26 17:59:14 -05001216 le16_to_cpu(mgmt->u.beacon.capab_info) & WLAN_CAPABILITY_IBSS &&
Luis R. Rodriguez954fece2009-09-10 10:51:33 -07001217 memcmp(mgmt->bssid, common->curbssid, ETH_ALEN) == 0) {
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001218 /*
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001219 * Received an IBSS beacon with the same BSSID. Hardware *must*
1220 * have updated the local TSF. We have to work around various
1221 * hardware bugs, though...
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001222 */
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001223 tsf = ath5k_hw_get_tsf64(sc->ah);
1224 bc_tstamp = le64_to_cpu(mgmt->u.beacon.timestamp);
1225 hw_tu = TSF_TO_TU(tsf);
1226
1227 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
1228 "beacon %llx mactime %llx (diff %lld) tsf now %llx\n",
John W. Linville06501d22008-04-01 17:38:47 -04001229 (unsigned long long)bc_tstamp,
1230 (unsigned long long)rxs->mactime,
1231 (unsigned long long)(rxs->mactime - bc_tstamp),
1232 (unsigned long long)tsf);
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001233
1234 /*
1235 * Sometimes the HW will give us a wrong tstamp in the rx
1236 * status, causing the timestamp extension to go wrong.
1237 * (This seems to happen especially with beacon frames bigger
1238 * than 78 byte (incl. FCS))
1239 * But we know that the receive timestamp must be later than the
1240 * timestamp of the beacon since HW must have synced to that.
1241 *
1242 * NOTE: here we assume mactime to be after the frame was
1243 * received, not like mac80211 which defines it at the start.
1244 */
1245 if (bc_tstamp > rxs->mactime) {
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001246 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001247 "fixing mactime from %llx to %llx\n",
John W. Linville06501d22008-04-01 17:38:47 -04001248 (unsigned long long)rxs->mactime,
1249 (unsigned long long)tsf);
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001250 rxs->mactime = tsf;
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001251 }
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001252
1253 /*
1254 * Local TSF might have moved higher than our beacon timers,
1255 * in that case we have to update them to continue sending
1256 * beacons. This also takes care of synchronizing beacon sending
1257 * times with other stations.
1258 */
1259 if (hw_tu >= sc->nexttbtt)
1260 ath5k_beacon_update_timers(sc, bc_tstamp);
Bruno Randolf7f896122010-09-27 12:22:21 +09001261
1262 /* Check if the beacon timers are still correct, because a TSF
1263 * update might have created a window between them - for a
1264 * longer description see the comment of this function: */
1265 if (!ath5k_hw_check_beacon_timers(sc->ah, sc->bintval)) {
1266 ath5k_beacon_update_timers(sc, bc_tstamp);
1267 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
1268 "fixed beacon timers after beacon receive\n");
1269 }
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001270 }
1271}
1272
Bruno Randolfb4ea4492010-03-25 14:49:25 +09001273static void
1274ath5k_update_beacon_rssi(struct ath5k_softc *sc, struct sk_buff *skb, int rssi)
1275{
1276 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)skb->data;
1277 struct ath5k_hw *ah = sc->ah;
1278 struct ath_common *common = ath5k_hw_common(ah);
1279
1280 /* only beacons from our BSSID */
1281 if (!ieee80211_is_beacon(mgmt->frame_control) ||
1282 memcmp(mgmt->bssid, common->curbssid, ETH_ALEN) != 0)
1283 return;
1284
1285 ah->ah_beacon_rssi_avg = ath5k_moving_average(ah->ah_beacon_rssi_avg,
1286 rssi);
1287
1288 /* in IBSS mode we should keep RSSI statistics per neighbour */
1289 /* le16_to_cpu(mgmt->u.beacon.capab_info) & WLAN_CAPABILITY_IBSS */
1290}
1291
Benoit Papillault8127fbd2010-02-27 23:05:26 +01001292/*
Bob Copelanda180a132010-08-15 13:03:12 -04001293 * Compute padding position. skb must contain an IEEE 802.11 frame
Benoit Papillault8127fbd2010-02-27 23:05:26 +01001294 */
1295static int ath5k_common_padpos(struct sk_buff *skb)
1296{
1297 struct ieee80211_hdr * hdr = (struct ieee80211_hdr *)skb->data;
1298 __le16 frame_control = hdr->frame_control;
1299 int padpos = 24;
1300
1301 if (ieee80211_has_a4(frame_control)) {
1302 padpos += ETH_ALEN;
1303 }
1304 if (ieee80211_is_data_qos(frame_control)) {
1305 padpos += IEEE80211_QOS_CTL_LEN;
1306 }
1307
1308 return padpos;
1309}
1310
1311/*
Bob Copelanda180a132010-08-15 13:03:12 -04001312 * This function expects an 802.11 frame and returns the number of
1313 * bytes added, or -1 if we don't have enough header room.
Benoit Papillault8127fbd2010-02-27 23:05:26 +01001314 */
Benoit Papillault8127fbd2010-02-27 23:05:26 +01001315static int ath5k_add_padding(struct sk_buff *skb)
1316{
1317 int padpos = ath5k_common_padpos(skb);
1318 int padsize = padpos & 3;
1319
1320 if (padsize && skb->len>padpos) {
1321
1322 if (skb_headroom(skb) < padsize)
1323 return -1;
1324
1325 skb_push(skb, padsize);
1326 memmove(skb->data, skb->data+padsize, padpos);
1327 return padsize;
1328 }
1329
1330 return 0;
1331}
1332
1333/*
Bob Copelanda180a132010-08-15 13:03:12 -04001334 * The MAC header is padded to have 32-bit boundary if the
1335 * packet payload is non-zero. The general calculation for
1336 * padsize would take into account odd header lengths:
1337 * padsize = 4 - (hdrlen & 3); however, since only
1338 * even-length headers are used, padding can only be 0 or 2
1339 * bytes and we can optimize this a bit. We must not try to
1340 * remove padding from short control frames that do not have a
1341 * payload.
1342 *
1343 * This function expects an 802.11 frame and returns the number of
1344 * bytes removed.
Benoit Papillault8127fbd2010-02-27 23:05:26 +01001345 */
Benoit Papillault8127fbd2010-02-27 23:05:26 +01001346static int ath5k_remove_padding(struct sk_buff *skb)
1347{
1348 int padpos = ath5k_common_padpos(skb);
1349 int padsize = padpos & 3;
1350
1351 if (padsize && skb->len>=padpos+padsize) {
1352 memmove(skb->data + padsize, skb->data, padpos);
1353 skb_pull(skb, padsize);
1354 return padsize;
1355 }
1356
1357 return 0;
1358}
1359
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001360static void
Bruno Randolf8a89f062010-06-16 19:11:51 +09001361ath5k_receive_frame(struct ath5k_softc *sc, struct sk_buff *skb,
1362 struct ath5k_rx_status *rs)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001363{
Bob Copeland1c5256b2009-08-24 23:00:32 -04001364 struct ieee80211_rx_status *rxs;
Bruno Randolf8a89f062010-06-16 19:11:51 +09001365
Bruno Randolf8a89f062010-06-16 19:11:51 +09001366 ath5k_remove_padding(skb);
1367
1368 rxs = IEEE80211_SKB_RXCB(skb);
1369
1370 rxs->flag = 0;
1371 if (unlikely(rs->rs_status & AR5K_RXERR_MIC))
1372 rxs->flag |= RX_FLAG_MMIC_ERROR;
1373
1374 /*
1375 * always extend the mac timestamp, since this information is
1376 * also needed for proper IBSS merging.
1377 *
1378 * XXX: it might be too late to do it here, since rs_tstamp is
1379 * 15bit only. that means TSF extension has to be done within
1380 * 32768usec (about 32ms). it might be necessary to move this to
1381 * the interrupt handler, like it is done in madwifi.
1382 *
1383 * Unfortunately we don't know when the hardware takes the rx
1384 * timestamp (beginning of phy frame, data frame, end of rx?).
1385 * The only thing we know is that it is hardware specific...
1386 * On AR5213 it seems the rx timestamp is at the end of the
1387 * frame, but i'm not sure.
1388 *
1389 * NOTE: mac80211 defines mactime at the beginning of the first
1390 * data symbol. Since we don't have any time references it's
1391 * impossible to comply to that. This affects IBSS merge only
1392 * right now, so it's not too bad...
1393 */
1394 rxs->mactime = ath5k_extend_tsf(sc->ah, rs->rs_tstamp);
1395 rxs->flag |= RX_FLAG_TSFT;
1396
1397 rxs->freq = sc->curchan->center_freq;
1398 rxs->band = sc->curband->band;
1399
1400 rxs->signal = sc->ah->ah_noise_floor + rs->rs_rssi;
1401
1402 rxs->antenna = rs->rs_antenna;
1403
1404 if (rs->rs_antenna > 0 && rs->rs_antenna < 5)
1405 sc->stats.antenna_rx[rs->rs_antenna]++;
1406 else
1407 sc->stats.antenna_rx[0]++; /* invalid */
1408
1409 rxs->rate_idx = ath5k_hw_to_driver_rix(sc, rs->rs_rate);
1410 rxs->flag |= ath5k_rx_decrypted(sc, skb, rs);
1411
1412 if (rxs->rate_idx >= 0 && rs->rs_rate ==
1413 sc->curband->bitrates[rxs->rate_idx].hw_value_short)
1414 rxs->flag |= RX_FLAG_SHORTPRE;
1415
1416 ath5k_debug_dump_skb(sc, skb, "RX ", 0);
1417
1418 ath5k_update_beacon_rssi(sc, skb, rs->rs_rssi);
1419
1420 /* check beacons in IBSS mode */
1421 if (sc->opmode == NL80211_IFTYPE_ADHOC)
1422 ath5k_check_ibss_tsf(sc, skb, rxs);
1423
1424 ieee80211_rx(sc->hw, skb);
1425}
1426
Bruno Randolf02a78b42010-06-16 19:11:56 +09001427/** ath5k_frame_receive_ok() - Do we want to receive this frame or not?
1428 *
1429 * Check if we want to further process this frame or not. Also update
1430 * statistics. Return true if we want this frame, false if not.
1431 */
1432static bool
1433ath5k_receive_frame_ok(struct ath5k_softc *sc, struct ath5k_rx_status *rs)
1434{
1435 sc->stats.rx_all_count++;
Ben Greearb72acdd2010-10-01 10:54:04 -07001436 sc->stats.rx_bytes_count += rs->rs_datalen;
Bruno Randolf02a78b42010-06-16 19:11:56 +09001437
1438 if (unlikely(rs->rs_status)) {
1439 if (rs->rs_status & AR5K_RXERR_CRC)
1440 sc->stats.rxerr_crc++;
1441 if (rs->rs_status & AR5K_RXERR_FIFO)
1442 sc->stats.rxerr_fifo++;
1443 if (rs->rs_status & AR5K_RXERR_PHY) {
1444 sc->stats.rxerr_phy++;
1445 if (rs->rs_phyerr > 0 && rs->rs_phyerr < 32)
1446 sc->stats.rxerr_phy_code[rs->rs_phyerr]++;
1447 return false;
1448 }
1449 if (rs->rs_status & AR5K_RXERR_DECRYPT) {
1450 /*
1451 * Decrypt error. If the error occurred
1452 * because there was no hardware key, then
1453 * let the frame through so the upper layers
1454 * can process it. This is necessary for 5210
1455 * parts which have no way to setup a ``clear''
1456 * key cache entry.
1457 *
1458 * XXX do key cache faulting
1459 */
1460 sc->stats.rxerr_decrypt++;
1461 if (rs->rs_keyix == AR5K_RXKEYIX_INVALID &&
1462 !(rs->rs_status & AR5K_RXERR_CRC))
1463 return true;
1464 }
1465 if (rs->rs_status & AR5K_RXERR_MIC) {
1466 sc->stats.rxerr_mic++;
1467 return true;
1468 }
1469
Bob Copeland23538c22010-08-15 13:03:13 -04001470 /* reject any frames with non-crypto errors */
1471 if (rs->rs_status & ~(AR5K_RXERR_DECRYPT))
Bruno Randolf02a78b42010-06-16 19:11:56 +09001472 return false;
1473 }
1474
1475 if (unlikely(rs->rs_more)) {
1476 sc->stats.rxerr_jumbo++;
1477 return false;
1478 }
1479 return true;
1480}
1481
Bruno Randolf8a89f062010-06-16 19:11:51 +09001482static void
1483ath5k_tasklet_rx(unsigned long data)
1484{
Bruno Randolfb47f4072008-03-05 18:35:45 +09001485 struct ath5k_rx_status rs = {};
Bob Copelandb6ea0352009-01-10 14:42:54 -05001486 struct sk_buff *skb, *next_skb;
1487 dma_addr_t next_skb_addr;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001488 struct ath5k_softc *sc = (void *)data;
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -08001489 struct ath5k_hw *ah = sc->ah;
1490 struct ath_common *common = ath5k_hw_common(ah);
Bob Copelandc57ca812009-04-15 07:57:35 -04001491 struct ath5k_buf *bf;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001492 struct ath5k_desc *ds;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001493 int ret;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001494
1495 spin_lock(&sc->rxbuflock);
Jiri Slaby3a0f2c82008-07-15 17:44:18 +02001496 if (list_empty(&sc->rxbuf)) {
1497 ATH5K_WARN(sc, "empty rx buf pool\n");
1498 goto unlock;
1499 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001500 do {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001501 bf = list_first_entry(&sc->rxbuf, struct ath5k_buf, list);
1502 BUG_ON(bf->skb == NULL);
1503 skb = bf->skb;
1504 ds = bf->desc;
1505
Bob Copelandc57ca812009-04-15 07:57:35 -04001506 /* bail if HW is still using self-linked descriptor */
1507 if (ath5k_hw_get_rxdp(sc->ah) == bf->daddr)
1508 break;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001509
Bruno Randolfb47f4072008-03-05 18:35:45 +09001510 ret = sc->ah->ah_proc_rx_desc(sc->ah, ds, &rs);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001511 if (unlikely(ret == -EINPROGRESS))
1512 break;
1513 else if (unlikely(ret)) {
1514 ATH5K_ERR(sc, "error in processing rx descriptor\n");
Bruno Randolf76443952010-03-09 16:56:00 +09001515 sc->stats.rxerr_proc++;
Bruno Randolfb16062f2010-06-16 19:11:46 +09001516 break;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001517 }
1518
Bruno Randolf02a78b42010-06-16 19:11:56 +09001519 if (ath5k_receive_frame_ok(sc, &rs)) {
1520 next_skb = ath5k_rx_skb_alloc(sc, &next_skb_addr);
Bruno Randolf76443952010-03-09 16:56:00 +09001521
Bruno Randolf02a78b42010-06-16 19:11:56 +09001522 /*
1523 * If we can't replace bf->skb with a new skb under
1524 * memory pressure, just skip this packet
1525 */
1526 if (!next_skb)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001527 goto next;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001528
Bruno Randolf02a78b42010-06-16 19:11:56 +09001529 pci_unmap_single(sc->pdev, bf->skbaddr,
1530 common->rx_bufsize,
1531 PCI_DMA_FROMDEVICE);
1532
1533 skb_put(skb, rs.rs_datalen);
1534
1535 ath5k_receive_frame(sc, skb, &rs);
1536
1537 bf->skb = next_skb;
1538 bf->skbaddr = next_skb_addr;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001539 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001540next:
1541 list_move_tail(&bf->list, &sc->rxbuf);
1542 } while (ath5k_rxbuf_setup(sc, bf) == 0);
Jiri Slaby3a0f2c82008-07-15 17:44:18 +02001543unlock:
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001544 spin_unlock(&sc->rxbuflock);
1545}
1546
1547
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001548/*************\
1549* TX Handling *
1550\*************/
1551
Bob Copeland8a63fac2010-09-17 12:45:07 +09001552static int ath5k_tx_queue(struct ieee80211_hw *hw, struct sk_buff *skb,
1553 struct ath5k_txq *txq)
1554{
1555 struct ath5k_softc *sc = hw->priv;
1556 struct ath5k_buf *bf;
1557 unsigned long flags;
1558 int padsize;
1559
1560 ath5k_debug_dump_skb(sc, skb, "TX ", 1);
1561
1562 /*
1563 * The hardware expects the header padded to 4 byte boundaries.
1564 * If this is not the case, we add the padding after the header.
1565 */
1566 padsize = ath5k_add_padding(skb);
1567 if (padsize < 0) {
1568 ATH5K_ERR(sc, "tx hdrlen not %%4: not enough"
1569 " headroom to pad");
1570 goto drop_packet;
1571 }
1572
Bruno Randolf925e0b02010-09-17 11:36:35 +09001573 if (txq->txq_len >= ATH5K_TXQ_LEN_MAX)
1574 ieee80211_stop_queue(hw, txq->qnum);
1575
Bob Copeland8a63fac2010-09-17 12:45:07 +09001576 spin_lock_irqsave(&sc->txbuflock, flags);
1577 if (list_empty(&sc->txbuf)) {
1578 ATH5K_ERR(sc, "no further txbuf available, dropping packet\n");
1579 spin_unlock_irqrestore(&sc->txbuflock, flags);
Bruno Randolf651d9372010-09-17 11:36:46 +09001580 ieee80211_stop_queues(hw);
Bob Copeland8a63fac2010-09-17 12:45:07 +09001581 goto drop_packet;
1582 }
1583 bf = list_first_entry(&sc->txbuf, struct ath5k_buf, list);
1584 list_del(&bf->list);
1585 sc->txbuf_len--;
1586 if (list_empty(&sc->txbuf))
1587 ieee80211_stop_queues(hw);
1588 spin_unlock_irqrestore(&sc->txbuflock, flags);
1589
1590 bf->skb = skb;
1591
1592 if (ath5k_txbuf_setup(sc, bf, txq, padsize)) {
1593 bf->skb = NULL;
1594 spin_lock_irqsave(&sc->txbuflock, flags);
1595 list_add_tail(&bf->list, &sc->txbuf);
1596 sc->txbuf_len++;
1597 spin_unlock_irqrestore(&sc->txbuflock, flags);
1598 goto drop_packet;
1599 }
1600 return NETDEV_TX_OK;
1601
1602drop_packet:
1603 dev_kfree_skb_any(skb);
1604 return NETDEV_TX_OK;
1605}
1606
Bruno Randolf14404012010-09-17 11:36:51 +09001607static void
1608ath5k_tx_frame_completed(struct ath5k_softc *sc, struct sk_buff *skb,
1609 struct ath5k_tx_status *ts)
1610{
1611 struct ieee80211_tx_info *info;
1612 int i;
1613
1614 sc->stats.tx_all_count++;
Ben Greearb72acdd2010-10-01 10:54:04 -07001615 sc->stats.tx_bytes_count += skb->len;
Bruno Randolf14404012010-09-17 11:36:51 +09001616 info = IEEE80211_SKB_CB(skb);
1617
1618 ieee80211_tx_info_clear_status(info);
1619 for (i = 0; i < 4; i++) {
1620 struct ieee80211_tx_rate *r =
1621 &info->status.rates[i];
1622
1623 if (ts->ts_rate[i]) {
1624 r->idx = ath5k_hw_to_driver_rix(sc, ts->ts_rate[i]);
1625 r->count = ts->ts_retry[i];
1626 } else {
1627 r->idx = -1;
1628 r->count = 0;
1629 }
1630 }
1631
1632 /* count the successful attempt as well */
1633 info->status.rates[ts->ts_final_idx].count++;
1634
1635 if (unlikely(ts->ts_status)) {
1636 sc->stats.ack_fail++;
1637 if (ts->ts_status & AR5K_TXERR_FILT) {
1638 info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
1639 sc->stats.txerr_filt++;
1640 }
1641 if (ts->ts_status & AR5K_TXERR_XRETRY)
1642 sc->stats.txerr_retry++;
1643 if (ts->ts_status & AR5K_TXERR_FIFO)
1644 sc->stats.txerr_fifo++;
1645 } else {
1646 info->flags |= IEEE80211_TX_STAT_ACK;
1647 info->status.ack_signal = ts->ts_rssi;
1648 }
1649
1650 /*
1651 * Remove MAC header padding before giving the frame
1652 * back to mac80211.
1653 */
1654 ath5k_remove_padding(skb);
1655
1656 if (ts->ts_antenna > 0 && ts->ts_antenna < 5)
1657 sc->stats.antenna_tx[ts->ts_antenna]++;
1658 else
1659 sc->stats.antenna_tx[0]++; /* invalid */
1660
1661 ieee80211_tx_status(sc->hw, skb);
1662}
Bob Copeland8a63fac2010-09-17 12:45:07 +09001663
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001664static void
1665ath5k_tx_processq(struct ath5k_softc *sc, struct ath5k_txq *txq)
1666{
Bruno Randolfb47f4072008-03-05 18:35:45 +09001667 struct ath5k_tx_status ts = {};
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001668 struct ath5k_buf *bf, *bf0;
1669 struct ath5k_desc *ds;
1670 struct sk_buff *skb;
Bruno Randolf14404012010-09-17 11:36:51 +09001671 int ret;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001672
1673 spin_lock(&txq->lock);
1674 list_for_each_entry_safe(bf, bf0, &txq->q, list) {
Bruno Randolf23413292010-09-17 11:37:07 +09001675
1676 txq->txq_poll_mark = false;
1677
1678 /* skb might already have been processed last time. */
1679 if (bf->skb != NULL) {
1680 ds = bf->desc;
1681
1682 ret = sc->ah->ah_proc_tx_desc(sc->ah, ds, &ts);
1683 if (unlikely(ret == -EINPROGRESS))
1684 break;
1685 else if (unlikely(ret)) {
1686 ATH5K_ERR(sc,
1687 "error %d while processing "
1688 "queue %u\n", ret, txq->qnum);
1689 break;
1690 }
1691
1692 skb = bf->skb;
1693 bf->skb = NULL;
1694 pci_unmap_single(sc->pdev, bf->skbaddr, skb->len,
1695 PCI_DMA_TODEVICE);
1696 ath5k_tx_frame_completed(sc, skb, &ts);
1697 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001698
Bob Copelanda05988b2010-04-07 23:55:58 -04001699 /*
1700 * It's possible that the hardware can say the buffer is
1701 * completed when it hasn't yet loaded the ds_link from
Bruno Randolf23413292010-09-17 11:37:07 +09001702 * host memory and moved on.
1703 * Always keep the last descriptor to avoid HW races...
Bob Copelanda05988b2010-04-07 23:55:58 -04001704 */
Bruno Randolf23413292010-09-17 11:37:07 +09001705 if (ath5k_hw_get_txdp(sc->ah, txq->qnum) != bf->daddr) {
1706 spin_lock(&sc->txbuflock);
1707 list_move_tail(&bf->list, &sc->txbuf);
1708 sc->txbuf_len++;
1709 txq->txq_len--;
1710 spin_unlock(&sc->txbuflock);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001711 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001712 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001713 spin_unlock(&txq->lock);
Bruno Randolf4198a8d2010-10-05 13:27:17 +09001714 if (txq->txq_len < ATH5K_TXQ_LEN_LOW && txq->qnum < 4)
Bruno Randolf925e0b02010-09-17 11:36:35 +09001715 ieee80211_wake_queue(sc->hw, txq->qnum);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001716}
1717
1718static void
1719ath5k_tasklet_tx(unsigned long data)
1720{
Bob Copeland8784d2e2009-07-29 17:32:28 -04001721 int i;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001722 struct ath5k_softc *sc = (void *)data;
1723
Bob Copeland8784d2e2009-07-29 17:32:28 -04001724 for (i=0; i < AR5K_NUM_TX_QUEUES; i++)
1725 if (sc->txqs[i].setup && (sc->ah->ah_txq_isr & BIT(i)))
1726 ath5k_tx_processq(sc, &sc->txqs[i]);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001727}
1728
1729
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001730/*****************\
1731* Beacon handling *
1732\*****************/
1733
1734/*
1735 * Setup the beacon frame for transmit.
1736 */
1737static int
Johannes Berge039fa42008-05-15 12:55:29 +02001738ath5k_beacon_setup(struct ath5k_softc *sc, struct ath5k_buf *bf)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001739{
1740 struct sk_buff *skb = bf->skb;
Johannes Berga888d522008-05-26 16:43:39 +02001741 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001742 struct ath5k_hw *ah = sc->ah;
1743 struct ath5k_desc *ds;
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04001744 int ret = 0;
1745 u8 antenna;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001746 u32 flags;
Benoit Papillault8127fbd2010-02-27 23:05:26 +01001747 const int padsize = 0;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001748
1749 bf->skbaddr = pci_map_single(sc->pdev, skb->data, skb->len,
1750 PCI_DMA_TODEVICE);
1751 ATH5K_DBG(sc, ATH5K_DEBUG_BEACON, "skb %p [data %p len %u] "
1752 "skbaddr %llx\n", skb, skb->data, skb->len,
1753 (unsigned long long)bf->skbaddr);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -07001754 if (pci_dma_mapping_error(sc->pdev, bf->skbaddr)) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001755 ATH5K_ERR(sc, "beacon DMA mapping failed\n");
1756 return -EIO;
1757 }
1758
1759 ds = bf->desc;
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04001760 antenna = ah->ah_tx_ant;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001761
1762 flags = AR5K_TXDESC_NOACK;
Johannes Berg05c914f2008-09-11 00:01:58 +02001763 if (sc->opmode == NL80211_IFTYPE_ADHOC && ath5k_hw_hasveol(ah)) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001764 ds->ds_link = bf->daddr; /* self-linked */
1765 flags |= AR5K_TXDESC_VEOL;
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04001766 } else
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001767 ds->ds_link = 0;
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04001768
1769 /*
1770 * If we use multiple antennas on AP and use
1771 * the Sectored AP scenario, switch antenna every
1772 * 4 beacons to make sure everybody hears our AP.
1773 * When a client tries to associate, hw will keep
1774 * track of the tx antenna to be used for this client
1775 * automaticaly, based on ACKed packets.
1776 *
1777 * Note: AP still listens and transmits RTS on the
1778 * default antenna which is supposed to be an omni.
1779 *
1780 * Note2: On sectored scenarios it's possible to have
Bob Copelanda180a132010-08-15 13:03:12 -04001781 * multiple antennas (1 omni -- the default -- and 14
1782 * sectors), so if we choose to actually support this
1783 * mode, we need to allow the user to set how many antennas
1784 * we have and tweak the code below to send beacons
1785 * on all of them.
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04001786 */
1787 if (ah->ah_ant_mode == AR5K_ANTMODE_SECTOR_AP)
1788 antenna = sc->bsent & 4 ? 2 : 1;
1789
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001790
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02001791 /* FIXME: If we are in g mode and rate is a CCK rate
1792 * subtract ah->ah_txpower.txp_cck_ofdm_pwr_delta
1793 * from tx power (value is in dB units already) */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001794 ds->ds_data = bf->skbaddr;
Bruno Randolf281c56d2008-02-05 18:44:55 +09001795 ret = ah->ah_setup_tx_desc(ah, ds, skb->len,
Benoit Papillault8127fbd2010-02-27 23:05:26 +01001796 ieee80211_get_hdrlen_from_skb(skb), padsize,
Luis R. Rodriguez400ec452008-02-03 21:51:49 -05001797 AR5K_PKT_TYPE_BEACON, (sc->power_level * 2),
Johannes Berge039fa42008-05-15 12:55:29 +02001798 ieee80211_get_tx_rate(sc->hw, info)->hw_value,
Johannes Berg2e92e6f2008-05-15 12:55:27 +02001799 1, AR5K_TXKEYIX_INVALID,
Luis R. Rodriguez400ec452008-02-03 21:51:49 -05001800 antenna, flags, 0, 0);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001801 if (ret)
1802 goto err_unmap;
1803
1804 return 0;
1805err_unmap:
1806 pci_unmap_single(sc->pdev, bf->skbaddr, skb->len, PCI_DMA_TODEVICE);
1807 return ret;
1808}
1809
1810/*
Bob Copeland8a63fac2010-09-17 12:45:07 +09001811 * Updates the beacon that is sent by ath5k_beacon_send. For adhoc,
1812 * this is called only once at config_bss time, for AP we do it every
1813 * SWBA interrupt so that the TIM will reflect buffered frames.
1814 *
1815 * Called with the beacon lock.
1816 */
1817static int
1818ath5k_beacon_update(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1819{
1820 int ret;
1821 struct ath5k_softc *sc = hw->priv;
Ben Greearb1ae1ed2010-09-30 12:22:58 -07001822 struct ath5k_vif *avf = (void *)vif->drv_priv;
Bob Copeland8a63fac2010-09-17 12:45:07 +09001823 struct sk_buff *skb;
1824
1825 if (WARN_ON(!vif)) {
1826 ret = -EINVAL;
1827 goto out;
1828 }
1829
1830 skb = ieee80211_beacon_get(hw, vif);
1831
1832 if (!skb) {
1833 ret = -ENOMEM;
1834 goto out;
1835 }
1836
1837 ath5k_debug_dump_skb(sc, skb, "BC ", 1);
1838
Ben Greearb1ae1ed2010-09-30 12:22:58 -07001839 ath5k_txbuf_free_skb(sc, avf->bbuf);
1840 avf->bbuf->skb = skb;
1841 ret = ath5k_beacon_setup(sc, avf->bbuf);
Bob Copeland8a63fac2010-09-17 12:45:07 +09001842 if (ret)
Ben Greearb1ae1ed2010-09-30 12:22:58 -07001843 avf->bbuf->skb = NULL;
Bob Copeland8a63fac2010-09-17 12:45:07 +09001844out:
1845 return ret;
1846}
1847
1848/*
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001849 * Transmit a beacon frame at SWBA. Dynamic updates to the
1850 * frame contents are done as needed and the slot time is
1851 * also adjusted based on current state.
1852 *
Bob Copeland5faaff72010-07-13 11:32:40 -04001853 * This is called from software irq context (beacontq tasklets)
1854 * or user context from ath5k_beacon_config.
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001855 */
1856static void
1857ath5k_beacon_send(struct ath5k_softc *sc)
1858{
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001859 struct ath5k_hw *ah = sc->ah;
Ben Greearb1ae1ed2010-09-30 12:22:58 -07001860 struct ieee80211_vif *vif;
1861 struct ath5k_vif *avf;
1862 struct ath5k_buf *bf;
Bob Copelandcec8db22009-07-04 12:59:51 -04001863 struct sk_buff *skb;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001864
Bruno Randolfbe9b7252008-01-23 10:27:51 +09001865 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON, "in beacon_send\n");
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001866
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001867 /*
1868 * Check if the previous beacon has gone out. If
Bob Copelanda180a132010-08-15 13:03:12 -04001869 * not, don't don't try to post another: skip this
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001870 * period and wait for the next. Missed beacons
1871 * indicate a problem and should not occur. If we
1872 * miss too many consecutive beacons reset the device.
1873 */
1874 if (unlikely(ath5k_hw_num_tx_pending(ah, sc->bhalq) != 0)) {
1875 sc->bmisscount++;
Bruno Randolfbe9b7252008-01-23 10:27:51 +09001876 ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001877 "missed %u consecutive beacons\n", sc->bmisscount);
Nick Kossifidis428cbd42009-04-30 15:55:47 -04001878 if (sc->bmisscount > 10) { /* NB: 10 is a guess */
Bruno Randolfbe9b7252008-01-23 10:27:51 +09001879 ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001880 "stuck beacon time (%u missed)\n",
1881 sc->bmisscount);
Bruno Randolf8d67a032010-06-16 19:11:12 +09001882 ATH5K_DBG(sc, ATH5K_DEBUG_RESET,
1883 "stuck beacon, resetting\n");
Bob Copeland5faaff72010-07-13 11:32:40 -04001884 ieee80211_queue_work(sc->hw, &sc->reset_work);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001885 }
1886 return;
1887 }
1888 if (unlikely(sc->bmisscount != 0)) {
Bruno Randolfbe9b7252008-01-23 10:27:51 +09001889 ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001890 "resume beacon xmit after %u misses\n",
1891 sc->bmisscount);
1892 sc->bmisscount = 0;
1893 }
1894
Ben Greearb1ae1ed2010-09-30 12:22:58 -07001895 if (sc->opmode == NL80211_IFTYPE_AP && sc->num_ap_vifs > 1) {
1896 u64 tsf = ath5k_hw_get_tsf64(ah);
1897 u32 tsftu = TSF_TO_TU(tsf);
1898 int slot = ((tsftu % sc->bintval) * ATH_BCBUF) / sc->bintval;
1899 vif = sc->bslot[(slot + 1) % ATH_BCBUF];
1900 ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
1901 "tsf %llx tsftu %x intval %u slot %u vif %p\n",
1902 (unsigned long long)tsf, tsftu, sc->bintval, slot, vif);
1903 } else /* only one interface */
1904 vif = sc->bslot[0];
1905
1906 if (!vif)
1907 return;
1908
1909 avf = (void *)vif->drv_priv;
1910 bf = avf->bbuf;
1911 if (unlikely(bf->skb == NULL || sc->opmode == NL80211_IFTYPE_STATION ||
1912 sc->opmode == NL80211_IFTYPE_MONITOR)) {
1913 ATH5K_WARN(sc, "bf=%p bf_skb=%p\n", bf, bf ? bf->skb : NULL);
1914 return;
1915 }
1916
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001917 /*
1918 * Stop any current dma and put the new frame on the queue.
1919 * This should never fail since we check above that no frames
1920 * are still pending on the queue.
1921 */
1922 if (unlikely(ath5k_hw_stop_tx_dma(ah, sc->bhalq))) {
Nick Kossifidis428cbd42009-04-30 15:55:47 -04001923 ATH5K_WARN(sc, "beacon queue %u didn't start/stop ?\n", sc->bhalq);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001924 /* NB: hw still stops DMA, so proceed */
1925 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001926
Bob Copeland1071db82009-05-18 10:59:52 -04001927 /* refresh the beacon for AP mode */
1928 if (sc->opmode == NL80211_IFTYPE_AP)
Ben Greearb1ae1ed2010-09-30 12:22:58 -07001929 ath5k_beacon_update(sc->hw, vif);
Bob Copeland1071db82009-05-18 10:59:52 -04001930
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001931 ath5k_hw_set_txdp(ah, sc->bhalq, bf->daddr);
1932 ath5k_hw_start_tx_dma(ah, sc->bhalq);
Bruno Randolfbe9b7252008-01-23 10:27:51 +09001933 ATH5K_DBG(sc, ATH5K_DEBUG_BEACON, "TXDP[%u] = %llx (%p)\n",
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001934 sc->bhalq, (unsigned long long)bf->daddr, bf->desc);
1935
Ben Greearb1ae1ed2010-09-30 12:22:58 -07001936 skb = ieee80211_get_buffered_bc(sc->hw, vif);
Bob Copelandcec8db22009-07-04 12:59:51 -04001937 while (skb) {
1938 ath5k_tx_queue(sc->hw, skb, sc->cabq);
Ben Greearb1ae1ed2010-09-30 12:22:58 -07001939 skb = ieee80211_get_buffered_bc(sc->hw, vif);
Bob Copelandcec8db22009-07-04 12:59:51 -04001940 }
1941
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001942 sc->bsent++;
1943}
1944
Bruno Randolf9804b982008-01-19 18:17:59 +09001945/**
1946 * ath5k_beacon_update_timers - update beacon timers
1947 *
1948 * @sc: struct ath5k_softc pointer we are operating on
1949 * @bc_tsf: the timestamp of the beacon. 0 to reset the TSF. -1 to perform a
1950 * beacon timer update based on the current HW TSF.
1951 *
1952 * Calculate the next target beacon transmit time (TBTT) based on the timestamp
1953 * of a received beacon or the current local hardware TSF and write it to the
1954 * beacon timer registers.
1955 *
1956 * This is called in a variety of situations, e.g. when a beacon is received,
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001957 * when a TSF update has been detected, but also when an new IBSS is created or
Bruno Randolf9804b982008-01-19 18:17:59 +09001958 * when we otherwise know we have to update the timers, but we keep it in this
1959 * function to have it all together in one place.
1960 */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001961static void
Bruno Randolf9804b982008-01-19 18:17:59 +09001962ath5k_beacon_update_timers(struct ath5k_softc *sc, u64 bc_tsf)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001963{
1964 struct ath5k_hw *ah = sc->ah;
Bruno Randolf9804b982008-01-19 18:17:59 +09001965 u32 nexttbtt, intval, hw_tu, bc_tu;
1966 u64 hw_tsf;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001967
1968 intval = sc->bintval & AR5K_BEACON_PERIOD;
Ben Greearb1ae1ed2010-09-30 12:22:58 -07001969 if (sc->opmode == NL80211_IFTYPE_AP && sc->num_ap_vifs > 1) {
1970 intval /= ATH_BCBUF; /* staggered multi-bss beacons */
1971 if (intval < 15)
1972 ATH5K_WARN(sc, "intval %u is too low, min 15\n",
1973 intval);
1974 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001975 if (WARN_ON(!intval))
1976 return;
1977
Bruno Randolf9804b982008-01-19 18:17:59 +09001978 /* beacon TSF converted to TU */
1979 bc_tu = TSF_TO_TU(bc_tsf);
1980
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001981 /* current TSF converted to TU */
Bruno Randolf9804b982008-01-19 18:17:59 +09001982 hw_tsf = ath5k_hw_get_tsf64(ah);
1983 hw_tu = TSF_TO_TU(hw_tsf);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001984
Bruno Randolf11f21df2010-09-27 12:22:26 +09001985#define FUDGE AR5K_TUNE_SW_BEACON_RESP + 3
1986 /* We use FUDGE to make sure the next TBTT is ahead of the current TU.
1987 * Since we later substract AR5K_TUNE_SW_BEACON_RESP (10) in the timer
1988 * configuration we need to make sure it is bigger than that. */
1989
Bruno Randolf9804b982008-01-19 18:17:59 +09001990 if (bc_tsf == -1) {
1991 /*
1992 * no beacons received, called internally.
1993 * just need to refresh timers based on HW TSF.
1994 */
1995 nexttbtt = roundup(hw_tu + FUDGE, intval);
1996 } else if (bc_tsf == 0) {
1997 /*
1998 * no beacon received, probably called by ath5k_reset_tsf().
1999 * reset TSF to start with 0.
2000 */
2001 nexttbtt = intval;
2002 intval |= AR5K_BEACON_RESET_TSF;
2003 } else if (bc_tsf > hw_tsf) {
2004 /*
2005 * beacon received, SW merge happend but HW TSF not yet updated.
2006 * not possible to reconfigure timers yet, but next time we
2007 * receive a beacon with the same BSSID, the hardware will
2008 * automatically update the TSF and then we need to reconfigure
2009 * the timers.
2010 */
2011 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
2012 "need to wait for HW TSF sync\n");
2013 return;
2014 } else {
2015 /*
2016 * most important case for beacon synchronization between STA.
2017 *
2018 * beacon received and HW TSF has been already updated by HW.
2019 * update next TBTT based on the TSF of the beacon, but make
2020 * sure it is ahead of our local TSF timer.
2021 */
2022 nexttbtt = bc_tu + roundup(hw_tu + FUDGE - bc_tu, intval);
2023 }
2024#undef FUDGE
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002025
Bruno Randolf036cd1e2008-01-19 18:18:21 +09002026 sc->nexttbtt = nexttbtt;
2027
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002028 intval |= AR5K_BEACON_ENA;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002029 ath5k_hw_init_beacon(ah, nexttbtt, intval);
Bruno Randolf9804b982008-01-19 18:17:59 +09002030
2031 /*
2032 * debugging output last in order to preserve the time critical aspect
2033 * of this function
2034 */
2035 if (bc_tsf == -1)
2036 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
2037 "reconfigured timers based on HW TSF\n");
2038 else if (bc_tsf == 0)
2039 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
2040 "reset HW TSF and timers\n");
2041 else
2042 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
2043 "updated timers based on beacon TSF\n");
2044
2045 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
David Miller04f93a82008-02-15 16:08:59 -08002046 "bc_tsf %llx hw_tsf %llx bc_tu %u hw_tu %u nexttbtt %u\n",
2047 (unsigned long long) bc_tsf,
2048 (unsigned long long) hw_tsf, bc_tu, hw_tu, nexttbtt);
Bruno Randolf9804b982008-01-19 18:17:59 +09002049 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON, "intval %u %s %s\n",
2050 intval & AR5K_BEACON_PERIOD,
2051 intval & AR5K_BEACON_ENA ? "AR5K_BEACON_ENA" : "",
2052 intval & AR5K_BEACON_RESET_TSF ? "AR5K_BEACON_RESET_TSF" : "");
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002053}
2054
Bruno Randolf036cd1e2008-01-19 18:18:21 +09002055/**
2056 * ath5k_beacon_config - Configure the beacon queues and interrupts
2057 *
2058 * @sc: struct ath5k_softc pointer we are operating on
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002059 *
Bruno Randolf036cd1e2008-01-19 18:18:21 +09002060 * In IBSS mode we use a self-linked tx descriptor if possible. We enable SWBA
Bruno Randolf6ba81c22008-03-05 18:36:26 +09002061 * interrupts to detect TSF updates only.
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002062 */
2063static void
2064ath5k_beacon_config(struct ath5k_softc *sc)
2065{
2066 struct ath5k_hw *ah = sc->ah;
Bob Copelandb5f03952009-02-15 12:06:10 -05002067 unsigned long flags;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002068
Bob Copeland21800492009-07-04 12:59:52 -04002069 spin_lock_irqsave(&sc->block, flags);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002070 sc->bmisscount = 0;
Jiri Slabydc1968e2008-07-23 13:17:34 +02002071 sc->imask &= ~(AR5K_INT_BMISS | AR5K_INT_SWBA);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002072
Bob Copeland21800492009-07-04 12:59:52 -04002073 if (sc->enable_beacon) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002074 /*
Bruno Randolf036cd1e2008-01-19 18:18:21 +09002075 * In IBSS mode we use a self-linked tx descriptor and let the
2076 * hardware send the beacons automatically. We have to load it
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002077 * only once here.
Bruno Randolf036cd1e2008-01-19 18:18:21 +09002078 * We use the SWBA interrupt only to keep track of the beacon
Bruno Randolf6ba81c22008-03-05 18:36:26 +09002079 * timers in order to detect automatic TSF updates.
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002080 */
2081 ath5k_beaconq_config(sc);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002082
Bruno Randolf036cd1e2008-01-19 18:18:21 +09002083 sc->imask |= AR5K_INT_SWBA;
2084
Jiri Slabyda966bc2008-10-12 22:54:10 +02002085 if (sc->opmode == NL80211_IFTYPE_ADHOC) {
Bob Copeland21800492009-07-04 12:59:52 -04002086 if (ath5k_hw_hasveol(ah))
Jiri Slabyda966bc2008-10-12 22:54:10 +02002087 ath5k_beacon_send(sc);
Jiri Slabyda966bc2008-10-12 22:54:10 +02002088 } else
2089 ath5k_beacon_update_timers(sc, -1);
Bob Copeland21800492009-07-04 12:59:52 -04002090 } else {
2091 ath5k_hw_stop_tx_dma(sc->ah, sc->bhalq);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002092 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002093
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03002094 ath5k_hw_set_imr(ah, sc->imask);
Bob Copeland21800492009-07-04 12:59:52 -04002095 mmiowb();
2096 spin_unlock_irqrestore(&sc->block, flags);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002097}
2098
Nick Kossifidis428cbd42009-04-30 15:55:47 -04002099static void ath5k_tasklet_beacon(unsigned long data)
2100{
2101 struct ath5k_softc *sc = (struct ath5k_softc *) data;
2102
2103 /*
2104 * Software beacon alert--time to send a beacon.
2105 *
2106 * In IBSS mode we use this interrupt just to
2107 * keep track of the next TBTT (target beacon
2108 * transmission time) in order to detect wether
2109 * automatic TSF updates happened.
2110 */
2111 if (sc->opmode == NL80211_IFTYPE_ADHOC) {
2112 /* XXX: only if VEOL suppported */
2113 u64 tsf = ath5k_hw_get_tsf64(sc->ah);
2114 sc->nexttbtt += sc->bintval;
2115 ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
2116 "SWBA nexttbtt: %x hw_tu: %x "
2117 "TSF: %llx\n",
2118 sc->nexttbtt,
2119 TSF_TO_TU(tsf),
2120 (unsigned long long) tsf);
2121 } else {
2122 spin_lock(&sc->block);
2123 ath5k_beacon_send(sc);
2124 spin_unlock(&sc->block);
2125 }
2126}
2127
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002128
2129/********************\
2130* Interrupt handling *
2131\********************/
2132
Bruno Randolf6a8a3f62010-03-25 14:49:19 +09002133static void
2134ath5k_intr_calibration_poll(struct ath5k_hw *ah)
2135{
Bruno Randolf2111ac02010-04-02 18:44:08 +09002136 if (time_is_before_eq_jiffies(ah->ah_cal_next_ani) &&
2137 !(ah->ah_cal_mask & AR5K_CALIBRATION_FULL)) {
2138 /* run ANI only when full calibration is not active */
2139 ah->ah_cal_next_ani = jiffies +
2140 msecs_to_jiffies(ATH5K_TUNE_CALIBRATION_INTERVAL_ANI);
2141 tasklet_schedule(&ah->ah_sc->ani_tasklet);
2142
2143 } else if (time_is_before_eq_jiffies(ah->ah_cal_next_full)) {
Bruno Randolf6a8a3f62010-03-25 14:49:19 +09002144 ah->ah_cal_next_full = jiffies +
2145 msecs_to_jiffies(ATH5K_TUNE_CALIBRATION_INTERVAL_FULL);
2146 tasklet_schedule(&ah->ah_sc->calib);
2147 }
2148 /* we could use SWI to generate enough interrupts to meet our
2149 * calibration interval requirements, if necessary:
2150 * AR5K_REG_ENABLE_BITS(ah, AR5K_CR, AR5K_CR_SWI); */
2151}
2152
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002153static irqreturn_t
2154ath5k_intr(int irq, void *dev_id)
2155{
2156 struct ath5k_softc *sc = dev_id;
2157 struct ath5k_hw *ah = sc->ah;
2158 enum ath5k_int status;
2159 unsigned int counter = 1000;
2160
2161 if (unlikely(test_bit(ATH_STAT_INVALID, sc->status) ||
2162 !ath5k_hw_is_intr_pending(ah)))
2163 return IRQ_NONE;
2164
2165 do {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002166 ath5k_hw_get_isr(ah, &status); /* NB: clears IRQ too */
2167 ATH5K_DBG(sc, ATH5K_DEBUG_INTR, "status 0x%x/0x%x\n",
2168 status, sc->imask);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002169 if (unlikely(status & AR5K_INT_FATAL)) {
2170 /*
2171 * Fatal errors are unrecoverable.
2172 * Typically these are caused by DMA errors.
2173 */
Bruno Randolf8d67a032010-06-16 19:11:12 +09002174 ATH5K_DBG(sc, ATH5K_DEBUG_RESET,
2175 "fatal int, resetting\n");
Bob Copeland5faaff72010-07-13 11:32:40 -04002176 ieee80211_queue_work(sc->hw, &sc->reset_work);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002177 } else if (unlikely(status & AR5K_INT_RXORN)) {
Bruno Randolf87d77c42010-04-12 16:38:52 +09002178 /*
2179 * Receive buffers are full. Either the bus is busy or
2180 * the CPU is not fast enough to process all received
2181 * frames.
2182 * Older chipsets need a reset to come out of this
2183 * condition, but we treat it as RX for newer chips.
2184 * We don't know exactly which versions need a reset -
2185 * this guess is copied from the HAL.
2186 */
2187 sc->stats.rxorn_intr++;
Bruno Randolf8d67a032010-06-16 19:11:12 +09002188 if (ah->ah_mac_srev < AR5K_SREV_AR5212) {
2189 ATH5K_DBG(sc, ATH5K_DEBUG_RESET,
2190 "rx overrun, resetting\n");
Bob Copeland5faaff72010-07-13 11:32:40 -04002191 ieee80211_queue_work(sc->hw, &sc->reset_work);
Bruno Randolf8d67a032010-06-16 19:11:12 +09002192 }
Bruno Randolf87d77c42010-04-12 16:38:52 +09002193 else
2194 tasklet_schedule(&sc->rxtq);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002195 } else {
2196 if (status & AR5K_INT_SWBA) {
Bob Copeland56d2ac72009-04-15 07:57:33 -04002197 tasklet_hi_schedule(&sc->beacontq);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002198 }
2199 if (status & AR5K_INT_RXEOL) {
2200 /*
2201 * NB: the hardware should re-read the link when
2202 * RXE bit is written, but it doesn't work at
2203 * least on older hardware revs.
2204 */
Bruno Randolfb3f194e2010-07-14 10:53:29 +09002205 sc->stats.rxeol_intr++;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002206 }
2207 if (status & AR5K_INT_TXURN) {
2208 /* bump tx trigger level */
2209 ath5k_hw_update_tx_triglevel(ah, true);
2210 }
Nick Kossifidis4c674c62008-10-26 20:40:25 +02002211 if (status & (AR5K_INT_RXOK | AR5K_INT_RXERR))
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002212 tasklet_schedule(&sc->rxtq);
Nick Kossifidis4c674c62008-10-26 20:40:25 +02002213 if (status & (AR5K_INT_TXOK | AR5K_INT_TXDESC
2214 | AR5K_INT_TXERR | AR5K_INT_TXEOL))
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002215 tasklet_schedule(&sc->txtq);
2216 if (status & AR5K_INT_BMISS) {
Nick Kossifidis1e3e6e82009-02-09 06:15:42 +02002217 /* TODO */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002218 }
2219 if (status & AR5K_INT_MIB) {
Bruno Randolf2111ac02010-04-02 18:44:08 +09002220 sc->stats.mib_intr++;
Bruno Randolf495391d2010-03-25 14:49:36 +09002221 ath5k_hw_update_mib_counters(ah);
Bruno Randolf2111ac02010-04-02 18:44:08 +09002222 ath5k_ani_mib_intr(ah);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002223 }
Tobias Doerffele6a3b612009-06-09 17:33:27 +02002224 if (status & AR5K_INT_GPIO)
Tobias Doerffele6a3b612009-06-09 17:33:27 +02002225 tasklet_schedule(&sc->rf_kill.toggleq);
Bob Copelanda6ae0712009-06-09 23:43:11 -04002226
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002227 }
Bob Copeland2516baa2009-04-27 22:18:10 -04002228 } while (ath5k_hw_is_intr_pending(ah) && --counter > 0);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002229
2230 if (unlikely(!counter))
2231 ATH5K_WARN(sc, "too many interrupts, giving up for now\n");
2232
Bruno Randolf6a8a3f62010-03-25 14:49:19 +09002233 ath5k_intr_calibration_poll(ah);
Nick Kossifidis6e220662009-08-10 03:31:31 +03002234
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002235 return IRQ_HANDLED;
2236}
2237
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002238/*
2239 * Periodically recalibrate the PHY to account
2240 * for temperature/environment changes.
2241 */
2242static void
Nick Kossifidis6e220662009-08-10 03:31:31 +03002243ath5k_tasklet_calibrate(unsigned long data)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002244{
2245 struct ath5k_softc *sc = (void *)data;
2246 struct ath5k_hw *ah = sc->ah;
2247
Nick Kossifidis6e220662009-08-10 03:31:31 +03002248 /* Only full calibration for now */
Bruno Randolfe65e1d72010-03-25 14:49:09 +09002249 ah->ah_cal_mask |= AR5K_CALIBRATION_FULL;
Nick Kossifidis6e220662009-08-10 03:31:31 +03002250
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002251 ATH5K_DBG(sc, ATH5K_DEBUG_CALIBRATE, "channel %u/%x\n",
Luis R. Rodriguez400ec452008-02-03 21:51:49 -05002252 ieee80211_frequency_to_channel(sc->curchan->center_freq),
2253 sc->curchan->hw_value);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002254
Nick Kossifidis6f3b4142009-02-09 06:03:41 +02002255 if (ath5k_hw_gainf_calibrate(ah) == AR5K_RFGAIN_NEED_CHANGE) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002256 /*
2257 * Rfgain is out of bounds, reset the chip
2258 * to load new gain values.
2259 */
2260 ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "calibration, resetting\n");
Bob Copeland5faaff72010-07-13 11:32:40 -04002261 ieee80211_queue_work(sc->hw, &sc->reset_work);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002262 }
2263 if (ath5k_hw_phy_calibrate(ah, sc->curchan))
2264 ATH5K_ERR(sc, "calibration of channel %u failed\n",
Luis R. Rodriguez400ec452008-02-03 21:51:49 -05002265 ieee80211_frequency_to_channel(
2266 sc->curchan->center_freq));
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002267
Bruno Randolf0e8e02d2010-05-19 10:31:05 +09002268 /* Noise floor calibration interrupts rx/tx path while I/Q calibration
Bruno Randolf651d9372010-09-17 11:36:46 +09002269 * doesn't.
2270 * TODO: We should stop TX here, so that it doesn't interfere.
2271 * Note that stopping the queues is not enough to stop TX! */
Bruno Randolfafe86282010-05-19 10:31:10 +09002272 if (time_is_before_eq_jiffies(ah->ah_cal_next_nf)) {
2273 ah->ah_cal_next_nf = jiffies +
2274 msecs_to_jiffies(ATH5K_TUNE_CALIBRATION_INTERVAL_NF);
Bruno Randolfafe86282010-05-19 10:31:10 +09002275 ath5k_hw_update_noise_floor(ah);
Bruno Randolfafe86282010-05-19 10:31:10 +09002276 }
Nick Kossifidis6e220662009-08-10 03:31:31 +03002277
Bruno Randolfe65e1d72010-03-25 14:49:09 +09002278 ah->ah_cal_mask &= ~AR5K_CALIBRATION_FULL;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002279}
2280
2281
Bruno Randolf2111ac02010-04-02 18:44:08 +09002282static void
2283ath5k_tasklet_ani(unsigned long data)
2284{
2285 struct ath5k_softc *sc = (void *)data;
2286 struct ath5k_hw *ah = sc->ah;
2287
2288 ah->ah_cal_mask |= AR5K_CALIBRATION_ANI;
2289 ath5k_ani_calibration(ah);
2290 ah->ah_cal_mask &= ~AR5K_CALIBRATION_ANI;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002291}
2292
2293
Bruno Randolf4edd7612010-09-17 11:36:56 +09002294static void
2295ath5k_tx_complete_poll_work(struct work_struct *work)
2296{
2297 struct ath5k_softc *sc = container_of(work, struct ath5k_softc,
2298 tx_complete_work.work);
2299 struct ath5k_txq *txq;
2300 int i;
2301 bool needreset = false;
2302
2303 for (i = 0; i < ARRAY_SIZE(sc->txqs); i++) {
2304 if (sc->txqs[i].setup) {
2305 txq = &sc->txqs[i];
2306 spin_lock_bh(&txq->lock);
Bruno Randolf23413292010-09-17 11:37:07 +09002307 if (txq->txq_len > 1) {
Bruno Randolf4edd7612010-09-17 11:36:56 +09002308 if (txq->txq_poll_mark) {
2309 ATH5K_DBG(sc, ATH5K_DEBUG_XMIT,
2310 "TX queue stuck %d\n",
2311 txq->qnum);
2312 needreset = true;
Bruno Randolf923e5b32010-09-17 11:37:02 +09002313 txq->txq_stuck++;
Bruno Randolf4edd7612010-09-17 11:36:56 +09002314 spin_unlock_bh(&txq->lock);
2315 break;
2316 } else {
2317 txq->txq_poll_mark = true;
2318 }
2319 }
2320 spin_unlock_bh(&txq->lock);
2321 }
2322 }
2323
2324 if (needreset) {
2325 ATH5K_DBG(sc, ATH5K_DEBUG_RESET,
2326 "TX queues stuck, resetting\n");
2327 ath5k_reset(sc, sc->curchan);
2328 }
2329
2330 ieee80211_queue_delayed_work(sc->hw, &sc->tx_complete_work,
2331 msecs_to_jiffies(ATH5K_TX_COMPLETE_POLL_INT));
2332}
2333
2334
Bob Copeland8a63fac2010-09-17 12:45:07 +09002335/*************************\
2336* Initialization routines *
2337\*************************/
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002338
2339static int
Bob Copeland8a63fac2010-09-17 12:45:07 +09002340ath5k_stop_locked(struct ath5k_softc *sc)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002341{
Bob Copeland8a63fac2010-09-17 12:45:07 +09002342 struct ath5k_hw *ah = sc->ah;
Bob Copelandcec8db22009-07-04 12:59:51 -04002343
Bob Copeland8a63fac2010-09-17 12:45:07 +09002344 ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "invalid %u\n",
2345 test_bit(ATH_STAT_INVALID, sc->status));
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002346
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002347 /*
Bob Copeland8a63fac2010-09-17 12:45:07 +09002348 * Shutdown the hardware and driver:
2349 * stop output from above
2350 * disable interrupts
2351 * turn off timers
2352 * turn off the radio
2353 * clear transmit machinery
2354 * clear receive machinery
2355 * drain and release tx queues
2356 * reclaim beacon resources
2357 * power down hardware
2358 *
2359 * Note that some of this work is not possible if the
2360 * hardware is gone (invalid).
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002361 */
Bob Copeland8a63fac2010-09-17 12:45:07 +09002362 ieee80211_stop_queues(sc->hw);
2363
2364 if (!test_bit(ATH_STAT_INVALID, sc->status)) {
2365 ath5k_led_off(sc);
2366 ath5k_hw_set_imr(ah, 0);
2367 synchronize_irq(sc->pdev->irq);
2368 }
2369 ath5k_txq_cleanup(sc);
2370 if (!test_bit(ATH_STAT_INVALID, sc->status)) {
2371 ath5k_rx_stop(sc);
2372 ath5k_hw_phy_disable(ah);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002373 }
2374
Bob Copeland8a63fac2010-09-17 12:45:07 +09002375 return 0;
2376}
2377
2378static int
2379ath5k_init(struct ath5k_softc *sc)
2380{
2381 struct ath5k_hw *ah = sc->ah;
2382 struct ath_common *common = ath5k_hw_common(ah);
2383 int ret, i;
2384
2385 mutex_lock(&sc->lock);
2386
2387 ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "mode %d\n", sc->opmode);
2388
2389 /*
2390 * Stop anything previously setup. This is safe
2391 * no matter this is the first time through or not.
2392 */
2393 ath5k_stop_locked(sc);
2394
2395 /*
2396 * The basic interface to setting the hardware in a good
2397 * state is ``reset''. On return the hardware is known to
2398 * be powered up and with interrupts disabled. This must
2399 * be followed by initialization of the appropriate bits
2400 * and then setup of the interrupt mask.
2401 */
2402 sc->curchan = sc->hw->conf.channel;
2403 sc->curband = &sc->sbands[sc->curchan->band];
2404 sc->imask = AR5K_INT_RXOK | AR5K_INT_RXERR | AR5K_INT_RXEOL |
2405 AR5K_INT_RXORN | AR5K_INT_TXDESC | AR5K_INT_TXEOL |
2406 AR5K_INT_FATAL | AR5K_INT_GLOBAL | AR5K_INT_MIB;
2407
2408 ret = ath5k_reset(sc, NULL);
2409 if (ret)
2410 goto done;
2411
2412 ath5k_rfkill_hw_start(ah);
2413
2414 /*
2415 * Reset the key cache since some parts do not reset the
2416 * contents on initial power up or resume from suspend.
2417 */
2418 for (i = 0; i < common->keymax; i++)
2419 ath_hw_keyreset(common, (u16) i);
2420
2421 ath5k_hw_set_ack_bitrate_high(ah, true);
Ben Greearb1ae1ed2010-09-30 12:22:58 -07002422
2423 for (i = 0; i < ARRAY_SIZE(sc->bslot); i++)
2424 sc->bslot[i] = NULL;
2425
Bob Copeland8a63fac2010-09-17 12:45:07 +09002426 ret = 0;
2427done:
2428 mmiowb();
2429 mutex_unlock(&sc->lock);
Bruno Randolf4edd7612010-09-17 11:36:56 +09002430
2431 ieee80211_queue_delayed_work(sc->hw, &sc->tx_complete_work,
2432 msecs_to_jiffies(ATH5K_TX_COMPLETE_POLL_INT));
2433
Bob Copeland8a63fac2010-09-17 12:45:07 +09002434 return ret;
2435}
2436
2437static void stop_tasklets(struct ath5k_softc *sc)
2438{
2439 tasklet_kill(&sc->rxtq);
2440 tasklet_kill(&sc->txtq);
2441 tasklet_kill(&sc->calib);
2442 tasklet_kill(&sc->beacontq);
2443 tasklet_kill(&sc->ani_tasklet);
2444}
2445
2446/*
2447 * Stop the device, grabbing the top-level lock to protect
2448 * against concurrent entry through ath5k_init (which can happen
2449 * if another thread does a system call and the thread doing the
2450 * stop is preempted).
2451 */
2452static int
2453ath5k_stop_hw(struct ath5k_softc *sc)
2454{
2455 int ret;
2456
2457 mutex_lock(&sc->lock);
2458 ret = ath5k_stop_locked(sc);
2459 if (ret == 0 && !test_bit(ATH_STAT_INVALID, sc->status)) {
2460 /*
2461 * Don't set the card in full sleep mode!
2462 *
2463 * a) When the device is in this state it must be carefully
2464 * woken up or references to registers in the PCI clock
2465 * domain may freeze the bus (and system). This varies
2466 * by chip and is mostly an issue with newer parts
2467 * (madwifi sources mentioned srev >= 0x78) that go to
2468 * sleep more quickly.
2469 *
2470 * b) On older chips full sleep results a weird behaviour
2471 * during wakeup. I tested various cards with srev < 0x78
2472 * and they don't wake up after module reload, a second
2473 * module reload is needed to bring the card up again.
2474 *
2475 * Until we figure out what's going on don't enable
2476 * full chip reset on any chip (this is what Legacy HAL
2477 * and Sam's HAL do anyway). Instead Perform a full reset
2478 * on the device (same as initial state after attach) and
2479 * leave it idle (keep MAC/BB on warm reset) */
2480 ret = ath5k_hw_on_hold(sc->ah);
2481
2482 ATH5K_DBG(sc, ATH5K_DEBUG_RESET,
2483 "putting device to sleep\n");
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002484 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002485
Bob Copeland8a63fac2010-09-17 12:45:07 +09002486 mmiowb();
2487 mutex_unlock(&sc->lock);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002488
Bob Copeland8a63fac2010-09-17 12:45:07 +09002489 stop_tasklets(sc);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002490
Bruno Randolf4edd7612010-09-17 11:36:56 +09002491 cancel_delayed_work_sync(&sc->tx_complete_work);
2492
Bob Copeland8a63fac2010-09-17 12:45:07 +09002493 ath5k_rfkill_hw_stop(sc->ah);
2494
2495 return ret;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002496}
2497
Bob Copeland209d8892009-05-07 08:09:08 -04002498/*
2499 * Reset the hardware. If chan is not NULL, then also pause rx/tx
2500 * and change to the given channel.
Bob Copeland5faaff72010-07-13 11:32:40 -04002501 *
2502 * This should be called with sc->lock.
Bob Copeland209d8892009-05-07 08:09:08 -04002503 */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002504static int
Bob Copeland209d8892009-05-07 08:09:08 -04002505ath5k_reset(struct ath5k_softc *sc, struct ieee80211_channel *chan)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002506{
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002507 struct ath5k_hw *ah = sc->ah;
2508 int ret;
2509
2510 ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "resetting\n");
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002511
Bob Copeland450464d2010-07-13 11:32:41 -04002512 ath5k_hw_set_imr(ah, 0);
2513 synchronize_irq(sc->pdev->irq);
2514 stop_tasklets(sc);
2515
Bob Copeland209d8892009-05-07 08:09:08 -04002516 if (chan) {
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002517 ath5k_txq_cleanup(sc);
2518 ath5k_rx_stop(sc);
Bob Copeland209d8892009-05-07 08:09:08 -04002519
2520 sc->curchan = chan;
2521 sc->curband = &sc->sbands[chan->band];
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002522 }
Bob Copeland33554432009-07-04 21:03:13 -04002523 ret = ath5k_hw_reset(ah, sc->opmode, sc->curchan, chan != NULL);
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002524 if (ret) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002525 ATH5K_ERR(sc, "can't reset hardware (%d)\n", ret);
2526 goto err;
2527 }
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002528
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002529 ret = ath5k_rx_start(sc);
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002530 if (ret) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002531 ATH5K_ERR(sc, "can't start recv logic\n");
2532 goto err;
2533 }
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002534
Bruno Randolf2111ac02010-04-02 18:44:08 +09002535 ath5k_ani_init(ah, ah->ah_sc->ani_state.ani_mode);
2536
Bruno Randolfac559522010-05-19 10:30:55 +09002537 ah->ah_cal_next_full = jiffies;
2538 ah->ah_cal_next_ani = jiffies;
Bruno Randolfafe86282010-05-19 10:31:10 +09002539 ah->ah_cal_next_nf = jiffies;
2540
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002541 /*
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002542 * Change channels and update the h/w rate map if we're switching;
2543 * e.g. 11a to 11b/g.
2544 *
2545 * We may be doing a reset in response to an ioctl that changes the
2546 * channel so update any state that might change as a result.
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002547 *
2548 * XXX needed?
2549 */
2550/* ath5k_chan_change(sc, c); */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002551
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002552 ath5k_beacon_config(sc);
2553 /* intrs are enabled by ath5k_beacon_config */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002554
Bruno Randolf397f3852010-05-19 10:30:49 +09002555 ieee80211_wake_queues(sc->hw);
2556
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002557 return 0;
2558err:
2559 return ret;
2560}
2561
Bob Copeland5faaff72010-07-13 11:32:40 -04002562static void ath5k_reset_work(struct work_struct *work)
2563{
2564 struct ath5k_softc *sc = container_of(work, struct ath5k_softc,
2565 reset_work);
2566
2567 mutex_lock(&sc->lock);
2568 ath5k_reset(sc, sc->curchan);
2569 mutex_unlock(&sc->lock);
2570}
2571
Bob Copeland8a63fac2010-09-17 12:45:07 +09002572static int
2573ath5k_attach(struct pci_dev *pdev, struct ieee80211_hw *hw)
2574{
2575 struct ath5k_softc *sc = hw->priv;
2576 struct ath5k_hw *ah = sc->ah;
2577 struct ath_regulatory *regulatory = ath5k_hw_regulatory(ah);
Bruno Randolf925e0b02010-09-17 11:36:35 +09002578 struct ath5k_txq *txq;
Bob Copeland8a63fac2010-09-17 12:45:07 +09002579 u8 mac[ETH_ALEN] = {};
2580 int ret;
2581
2582 ATH5K_DBG(sc, ATH5K_DEBUG_ANY, "devid 0x%x\n", pdev->device);
2583
2584 /*
2585 * Check if the MAC has multi-rate retry support.
2586 * We do this by trying to setup a fake extended
2587 * descriptor. MACs that don't have support will
2588 * return false w/o doing anything. MACs that do
2589 * support it will return true w/o doing anything.
2590 */
2591 ret = ath5k_hw_setup_mrr_tx_desc(ah, NULL, 0, 0, 0, 0, 0, 0);
2592
2593 if (ret < 0)
2594 goto err;
2595 if (ret > 0)
2596 __set_bit(ATH_STAT_MRRETRY, sc->status);
2597
2598 /*
2599 * Collect the channel list. The 802.11 layer
2600 * is resposible for filtering this list based
2601 * on settings like the phy mode and regulatory
2602 * domain restrictions.
2603 */
2604 ret = ath5k_setup_bands(hw);
2605 if (ret) {
2606 ATH5K_ERR(sc, "can't get channels\n");
2607 goto err;
2608 }
2609
2610 /* NB: setup here so ath5k_rate_update is happy */
2611 if (test_bit(AR5K_MODE_11A, ah->ah_modes))
2612 ath5k_setcurmode(sc, AR5K_MODE_11A);
2613 else
2614 ath5k_setcurmode(sc, AR5K_MODE_11B);
2615
2616 /*
2617 * Allocate tx+rx descriptors and populate the lists.
2618 */
2619 ret = ath5k_desc_alloc(sc, pdev);
2620 if (ret) {
2621 ATH5K_ERR(sc, "can't allocate descriptors\n");
2622 goto err;
2623 }
2624
2625 /*
2626 * Allocate hardware transmit queues: one queue for
2627 * beacon frames and one data queue for each QoS
2628 * priority. Note that hw functions handle resetting
2629 * these queues at the needed time.
2630 */
2631 ret = ath5k_beaconq_setup(ah);
2632 if (ret < 0) {
2633 ATH5K_ERR(sc, "can't setup a beacon xmit queue\n");
2634 goto err_desc;
2635 }
2636 sc->bhalq = ret;
2637 sc->cabq = ath5k_txq_setup(sc, AR5K_TX_QUEUE_CAB, 0);
2638 if (IS_ERR(sc->cabq)) {
2639 ATH5K_ERR(sc, "can't setup cab queue\n");
2640 ret = PTR_ERR(sc->cabq);
2641 goto err_bhal;
2642 }
2643
Bruno Randolf925e0b02010-09-17 11:36:35 +09002644 /* This order matches mac80211's queue priority, so we can
2645 * directly use the mac80211 queue number without any mapping */
2646 txq = ath5k_txq_setup(sc, AR5K_TX_QUEUE_DATA, AR5K_WME_AC_VO);
2647 if (IS_ERR(txq)) {
Bob Copeland8a63fac2010-09-17 12:45:07 +09002648 ATH5K_ERR(sc, "can't setup xmit queue\n");
Bruno Randolf925e0b02010-09-17 11:36:35 +09002649 ret = PTR_ERR(txq);
Bob Copeland8a63fac2010-09-17 12:45:07 +09002650 goto err_queues;
2651 }
Bruno Randolf925e0b02010-09-17 11:36:35 +09002652 txq = ath5k_txq_setup(sc, AR5K_TX_QUEUE_DATA, AR5K_WME_AC_VI);
2653 if (IS_ERR(txq)) {
2654 ATH5K_ERR(sc, "can't setup xmit queue\n");
2655 ret = PTR_ERR(txq);
2656 goto err_queues;
2657 }
2658 txq = ath5k_txq_setup(sc, AR5K_TX_QUEUE_DATA, AR5K_WME_AC_BE);
2659 if (IS_ERR(txq)) {
2660 ATH5K_ERR(sc, "can't setup xmit queue\n");
2661 ret = PTR_ERR(txq);
2662 goto err_queues;
2663 }
2664 txq = ath5k_txq_setup(sc, AR5K_TX_QUEUE_DATA, AR5K_WME_AC_BK);
2665 if (IS_ERR(txq)) {
2666 ATH5K_ERR(sc, "can't setup xmit queue\n");
2667 ret = PTR_ERR(txq);
2668 goto err_queues;
2669 }
2670 hw->queues = 4;
Bob Copeland8a63fac2010-09-17 12:45:07 +09002671
2672 tasklet_init(&sc->rxtq, ath5k_tasklet_rx, (unsigned long)sc);
2673 tasklet_init(&sc->txtq, ath5k_tasklet_tx, (unsigned long)sc);
2674 tasklet_init(&sc->calib, ath5k_tasklet_calibrate, (unsigned long)sc);
2675 tasklet_init(&sc->beacontq, ath5k_tasklet_beacon, (unsigned long)sc);
2676 tasklet_init(&sc->ani_tasklet, ath5k_tasklet_ani, (unsigned long)sc);
2677
2678 INIT_WORK(&sc->reset_work, ath5k_reset_work);
Bruno Randolf4edd7612010-09-17 11:36:56 +09002679 INIT_DELAYED_WORK(&sc->tx_complete_work, ath5k_tx_complete_poll_work);
Bob Copeland8a63fac2010-09-17 12:45:07 +09002680
2681 ret = ath5k_eeprom_read_mac(ah, mac);
2682 if (ret) {
2683 ATH5K_ERR(sc, "unable to read address from EEPROM: 0x%04x\n",
2684 sc->pdev->device);
2685 goto err_queues;
2686 }
2687
2688 SET_IEEE80211_PERM_ADDR(hw, mac);
Ben Greearb1ae1ed2010-09-30 12:22:58 -07002689 memcpy(&sc->lladdr, mac, ETH_ALEN);
Bob Copeland8a63fac2010-09-17 12:45:07 +09002690 /* All MAC address bits matter for ACKs */
Ben Greearb1ae1ed2010-09-30 12:22:58 -07002691 ath5k_update_bssid_mask(sc, NULL);
Bob Copeland8a63fac2010-09-17 12:45:07 +09002692
2693 regulatory->current_rd = ah->ah_capabilities.cap_eeprom.ee_regdomain;
2694 ret = ath_regd_init(regulatory, hw->wiphy, ath5k_reg_notifier);
2695 if (ret) {
2696 ATH5K_ERR(sc, "can't initialize regulatory system\n");
2697 goto err_queues;
2698 }
2699
2700 ret = ieee80211_register_hw(hw);
2701 if (ret) {
2702 ATH5K_ERR(sc, "can't register ieee80211 hw\n");
2703 goto err_queues;
2704 }
2705
2706 if (!ath_is_world_regd(regulatory))
2707 regulatory_hint(hw->wiphy, regulatory->alpha2);
2708
2709 ath5k_init_leds(sc);
2710
2711 ath5k_sysfs_register(sc);
2712
2713 return 0;
2714err_queues:
2715 ath5k_txq_release(sc);
2716err_bhal:
2717 ath5k_hw_release_tx_queue(ah, sc->bhalq);
2718err_desc:
2719 ath5k_desc_free(sc, pdev);
2720err:
2721 return ret;
2722}
2723
2724static void
2725ath5k_detach(struct pci_dev *pdev, struct ieee80211_hw *hw)
2726{
2727 struct ath5k_softc *sc = hw->priv;
2728
2729 /*
2730 * NB: the order of these is important:
2731 * o call the 802.11 layer before detaching ath5k_hw to
2732 * ensure callbacks into the driver to delete global
2733 * key cache entries can be handled
2734 * o reclaim the tx queue data structures after calling
2735 * the 802.11 layer as we'll get called back to reclaim
2736 * node state and potentially want to use them
2737 * o to cleanup the tx queues the hal is called, so detach
2738 * it last
2739 * XXX: ??? detach ath5k_hw ???
2740 * Other than that, it's straightforward...
2741 */
2742 ieee80211_unregister_hw(hw);
2743 ath5k_desc_free(sc, pdev);
2744 ath5k_txq_release(sc);
2745 ath5k_hw_release_tx_queue(sc->ah, sc->bhalq);
2746 ath5k_unregister_leds(sc);
2747
2748 ath5k_sysfs_unregister(sc);
2749 /*
2750 * NB: can't reclaim these until after ieee80211_ifdetach
2751 * returns because we'll get called back to reclaim node
2752 * state and potentially want to use them.
2753 */
2754}
2755
2756/********************\
2757* Mac80211 functions *
2758\********************/
2759
2760static int
2761ath5k_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
2762{
2763 struct ath5k_softc *sc = hw->priv;
Bruno Randolf925e0b02010-09-17 11:36:35 +09002764 u16 qnum = skb_get_queue_mapping(skb);
Bob Copeland8a63fac2010-09-17 12:45:07 +09002765
Bruno Randolf925e0b02010-09-17 11:36:35 +09002766 if (WARN_ON(qnum >= sc->ah->ah_capabilities.cap_queues.q_tx_num)) {
2767 dev_kfree_skb_any(skb);
2768 return 0;
2769 }
2770
2771 return ath5k_tx_queue(hw, skb, &sc->txqs[qnum]);
Bob Copeland8a63fac2010-09-17 12:45:07 +09002772}
2773
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002774static int ath5k_start(struct ieee80211_hw *hw)
2775{
Bob Copelandbb2beca2009-01-19 11:20:54 -05002776 return ath5k_init(hw->priv);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002777}
2778
2779static void ath5k_stop(struct ieee80211_hw *hw)
2780{
Bob Copelandbb2beca2009-01-19 11:20:54 -05002781 ath5k_stop_hw(hw->priv);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002782}
2783
2784static int ath5k_add_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +01002785 struct ieee80211_vif *vif)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002786{
2787 struct ath5k_softc *sc = hw->priv;
2788 int ret;
Ben Greearb1ae1ed2010-09-30 12:22:58 -07002789 struct ath5k_hw *ah = sc->ah;
2790 struct ath5k_vif *avf = (void *)vif->drv_priv;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002791
2792 mutex_lock(&sc->lock);
Ben Greearb1ae1ed2010-09-30 12:22:58 -07002793
2794 if ((vif->type == NL80211_IFTYPE_AP ||
2795 vif->type == NL80211_IFTYPE_ADHOC)
2796 && (sc->num_ap_vifs + sc->num_adhoc_vifs) >= ATH_BCBUF) {
2797 ret = -ELNRNG;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002798 goto end;
2799 }
2800
Ben Greearb1ae1ed2010-09-30 12:22:58 -07002801 /* Don't allow other interfaces if one ad-hoc is configured.
2802 * TODO: Fix the problems with ad-hoc and multiple other interfaces.
2803 * We would need to operate the HW in ad-hoc mode to allow TSF updates
2804 * for the IBSS, but this breaks with additional AP or STA interfaces
2805 * at the moment. */
2806 if (sc->num_adhoc_vifs ||
2807 (sc->nvifs && vif->type == NL80211_IFTYPE_ADHOC)) {
2808 ATH5K_ERR(sc, "Only one single ad-hoc interface is allowed.\n");
2809 ret = -ELNRNG;
2810 goto end;
2811 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002812
Johannes Berg1ed32e42009-12-23 13:15:45 +01002813 switch (vif->type) {
Jiri Slabyda966bc2008-10-12 22:54:10 +02002814 case NL80211_IFTYPE_AP:
Johannes Berg05c914f2008-09-11 00:01:58 +02002815 case NL80211_IFTYPE_STATION:
2816 case NL80211_IFTYPE_ADHOC:
Andrey Yurovskyb706e652008-10-13 18:23:07 -07002817 case NL80211_IFTYPE_MESH_POINT:
Ben Greearb1ae1ed2010-09-30 12:22:58 -07002818 avf->opmode = vif->type;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002819 break;
2820 default:
2821 ret = -EOPNOTSUPP;
2822 goto end;
2823 }
Jiri Slaby67d2e2d2008-08-18 21:45:28 +02002824
Ben Greearb1ae1ed2010-09-30 12:22:58 -07002825 sc->nvifs++;
2826 ATH5K_DBG(sc, ATH5K_DEBUG_MODE, "add interface mode %d\n", avf->opmode);
Bruno Randolfccfe5552010-03-09 16:55:38 +09002827
Ben Greearb1ae1ed2010-09-30 12:22:58 -07002828 /* Assign the vap/adhoc to a beacon xmit slot. */
2829 if ((avf->opmode == NL80211_IFTYPE_AP) ||
2830 (avf->opmode == NL80211_IFTYPE_ADHOC)) {
2831 int slot;
2832
2833 WARN_ON(list_empty(&sc->bcbuf));
2834 avf->bbuf = list_first_entry(&sc->bcbuf, struct ath5k_buf,
2835 list);
2836 list_del(&avf->bbuf->list);
2837
2838 avf->bslot = 0;
2839 for (slot = 0; slot < ATH_BCBUF; slot++) {
2840 if (!sc->bslot[slot]) {
2841 avf->bslot = slot;
2842 break;
2843 }
2844 }
2845 BUG_ON(sc->bslot[avf->bslot] != NULL);
2846 sc->bslot[avf->bslot] = vif;
2847 if (avf->opmode == NL80211_IFTYPE_AP)
2848 sc->num_ap_vifs++;
2849 else
2850 sc->num_adhoc_vifs++;
2851 }
2852
2853 /* Set combined mode - when APs are configured, operate in AP mode.
2854 * Otherwise use the mode of the new interface. This can currently
2855 * only deal with combinations of APs and STAs. Only one ad-hoc
2856 * interfaces is allowed above.
2857 */
2858 if (sc->num_ap_vifs)
2859 sc->opmode = NL80211_IFTYPE_AP;
2860 else
2861 sc->opmode = vif->type;
2862
2863 ath5k_hw_set_opmode(ah, sc->opmode);
2864
2865 /* Any MAC address is fine, all others are included through the
2866 * filter.
2867 */
2868 memcpy(&sc->lladdr, vif->addr, ETH_ALEN);
Johannes Berg1ed32e42009-12-23 13:15:45 +01002869 ath5k_hw_set_lladdr(sc->ah, vif->addr);
Ben Greearb1ae1ed2010-09-30 12:22:58 -07002870
2871 memcpy(&avf->lladdr, vif->addr, ETH_ALEN);
2872
2873 ath5k_mode_setup(sc, vif);
Jiri Slaby67d2e2d2008-08-18 21:45:28 +02002874
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002875 ret = 0;
2876end:
2877 mutex_unlock(&sc->lock);
2878 return ret;
2879}
2880
2881static void
2882ath5k_remove_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +01002883 struct ieee80211_vif *vif)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002884{
2885 struct ath5k_softc *sc = hw->priv;
Ben Greearb1ae1ed2010-09-30 12:22:58 -07002886 struct ath5k_vif *avf = (void *)vif->drv_priv;
2887 unsigned int i;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002888
2889 mutex_lock(&sc->lock);
Ben Greearb1ae1ed2010-09-30 12:22:58 -07002890 sc->nvifs--;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002891
Ben Greearb1ae1ed2010-09-30 12:22:58 -07002892 if (avf->bbuf) {
2893 ath5k_txbuf_free_skb(sc, avf->bbuf);
2894 list_add_tail(&avf->bbuf->list, &sc->bcbuf);
2895 for (i = 0; i < ATH_BCBUF; i++) {
2896 if (sc->bslot[i] == vif) {
2897 sc->bslot[i] = NULL;
2898 break;
2899 }
2900 }
2901 avf->bbuf = NULL;
2902 }
2903 if (avf->opmode == NL80211_IFTYPE_AP)
2904 sc->num_ap_vifs--;
2905 else if (avf->opmode == NL80211_IFTYPE_ADHOC)
2906 sc->num_adhoc_vifs--;
2907
2908 ath5k_update_bssid_mask(sc, NULL);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002909 mutex_unlock(&sc->lock);
2910}
2911
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -05002912/*
2913 * TODO: Phy disable/diversity etc
2914 */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002915static int
Johannes Berge8975582008-10-09 12:18:51 +02002916ath5k_config(struct ieee80211_hw *hw, u32 changed)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002917{
2918 struct ath5k_softc *sc = hw->priv;
Nick Kossifidisa0823812009-04-30 15:55:44 -04002919 struct ath5k_hw *ah = sc->ah;
Johannes Berge8975582008-10-09 12:18:51 +02002920 struct ieee80211_conf *conf = &hw->conf;
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04002921 int ret = 0;
Bob Copelandbe009372009-01-22 08:44:16 -05002922
2923 mutex_lock(&sc->lock);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002924
Joerg Alberte30eb4a2009-08-05 01:52:07 +02002925 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
2926 ret = ath5k_chan_set(sc, conf->channel);
2927 if (ret < 0)
2928 goto unlock;
2929 }
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04002930
Nick Kossifidisa0823812009-04-30 15:55:44 -04002931 if ((changed & IEEE80211_CONF_CHANGE_POWER) &&
2932 (sc->power_level != conf->power_level)) {
2933 sc->power_level = conf->power_level;
2934
2935 /* Half dB steps */
2936 ath5k_hw_set_txpower_limit(ah, (conf->power_level * 2));
2937 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002938
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04002939 /* TODO:
2940 * 1) Move this on config_interface and handle each case
2941 * separately eg. when we have only one STA vif, use
2942 * AR5K_ANTMODE_SINGLE_AP
2943 *
2944 * 2) Allow the user to change antenna mode eg. when only
2945 * one antenna is present
2946 *
2947 * 3) Allow the user to set default/tx antenna when possible
2948 *
2949 * 4) Default mode should handle 90% of the cases, together
2950 * with fixed a/b and single AP modes we should be able to
2951 * handle 99%. Sectored modes are extreme cases and i still
2952 * haven't found a usage for them. If we decide to support them,
2953 * then we must allow the user to set how many tx antennas we
2954 * have available
2955 */
Bruno Randolfcaec9112010-03-09 16:55:28 +09002956 ath5k_hw_set_antenna_mode(ah, ah->ah_ant_mode);
Bob Copelandbe009372009-01-22 08:44:16 -05002957
John W. Linville55aa4e02009-05-25 21:28:47 +02002958unlock:
Bob Copelandbe009372009-01-22 08:44:16 -05002959 mutex_unlock(&sc->lock);
John W. Linville55aa4e02009-05-25 21:28:47 +02002960 return ret;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002961}
2962
Johannes Berg3ac64be2009-08-17 16:16:53 +02002963static u64 ath5k_prepare_multicast(struct ieee80211_hw *hw,
Jiri Pirko22bedad2010-04-01 21:22:57 +00002964 struct netdev_hw_addr_list *mc_list)
Johannes Berg3ac64be2009-08-17 16:16:53 +02002965{
2966 u32 mfilt[2], val;
Johannes Berg3ac64be2009-08-17 16:16:53 +02002967 u8 pos;
Jiri Pirko22bedad2010-04-01 21:22:57 +00002968 struct netdev_hw_addr *ha;
Johannes Berg3ac64be2009-08-17 16:16:53 +02002969
2970 mfilt[0] = 0;
2971 mfilt[1] = 1;
2972
Jiri Pirko22bedad2010-04-01 21:22:57 +00002973 netdev_hw_addr_list_for_each(ha, mc_list) {
Johannes Berg3ac64be2009-08-17 16:16:53 +02002974 /* calculate XOR of eight 6-bit values */
Jiri Pirko22bedad2010-04-01 21:22:57 +00002975 val = get_unaligned_le32(ha->addr + 0);
Johannes Berg3ac64be2009-08-17 16:16:53 +02002976 pos = (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
Jiri Pirko22bedad2010-04-01 21:22:57 +00002977 val = get_unaligned_le32(ha->addr + 3);
Johannes Berg3ac64be2009-08-17 16:16:53 +02002978 pos ^= (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
2979 pos &= 0x3f;
2980 mfilt[pos / 32] |= (1 << (pos % 32));
2981 /* XXX: we might be able to just do this instead,
2982 * but not sure, needs testing, if we do use this we'd
2983 * neet to inform below to not reset the mcast */
2984 /* ath5k_hw_set_mcast_filterindex(ah,
Jiri Pirko22bedad2010-04-01 21:22:57 +00002985 * ha->addr[5]); */
Johannes Berg3ac64be2009-08-17 16:16:53 +02002986 }
2987
2988 return ((u64)(mfilt[1]) << 32) | mfilt[0];
2989}
2990
Ben Greearb1ae1ed2010-09-30 12:22:58 -07002991static bool ath_any_vif_assoc(struct ath5k_softc *sc)
2992{
2993 struct ath_vif_iter_data iter_data;
2994 iter_data.hw_macaddr = NULL;
2995 iter_data.any_assoc = false;
2996 iter_data.need_set_hw_addr = false;
2997 iter_data.found_active = true;
2998
2999 ieee80211_iterate_active_interfaces_atomic(sc->hw, ath_vif_iter,
3000 &iter_data);
3001 return iter_data.any_assoc;
3002}
3003
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003004#define SUPPORTED_FIF_FLAGS \
3005 FIF_PROMISC_IN_BSS | FIF_ALLMULTI | FIF_FCSFAIL | \
3006 FIF_PLCPFAIL | FIF_CONTROL | FIF_OTHER_BSS | \
3007 FIF_BCN_PRBRESP_PROMISC
3008/*
3009 * o always accept unicast, broadcast, and multicast traffic
3010 * o multicast traffic for all BSSIDs will be enabled if mac80211
3011 * says it should be
3012 * o maintain current state of phy ofdm or phy cck error reception.
3013 * If the hardware detects any of these type of errors then
3014 * ath5k_hw_get_rx_filter() will pass to us the respective
3015 * hardware filters to be able to receive these type of frames.
3016 * o probe request frames are accepted only when operating in
3017 * hostap, adhoc, or monitor modes
3018 * o enable promiscuous mode according to the interface state
3019 * o accept beacons:
3020 * - when operating in adhoc mode so the 802.11 layer creates
3021 * node table entries for peers,
3022 * - when operating in station mode for collecting rssi data when
3023 * the station is otherwise quiet, or
3024 * - when scanning
3025 */
3026static void ath5k_configure_filter(struct ieee80211_hw *hw,
3027 unsigned int changed_flags,
3028 unsigned int *new_flags,
Johannes Berg3ac64be2009-08-17 16:16:53 +02003029 u64 multicast)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003030{
3031 struct ath5k_softc *sc = hw->priv;
3032 struct ath5k_hw *ah = sc->ah;
Johannes Berg3ac64be2009-08-17 16:16:53 +02003033 u32 mfilt[2], rfilt;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003034
Bob Copeland56d1de02009-08-24 23:00:30 -04003035 mutex_lock(&sc->lock);
3036
Johannes Berg3ac64be2009-08-17 16:16:53 +02003037 mfilt[0] = multicast;
3038 mfilt[1] = multicast >> 32;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003039
3040 /* Only deal with supported flags */
3041 changed_flags &= SUPPORTED_FIF_FLAGS;
3042 *new_flags &= SUPPORTED_FIF_FLAGS;
3043
3044 /* If HW detects any phy or radar errors, leave those filters on.
3045 * Also, always enable Unicast, Broadcasts and Multicast
3046 * XXX: move unicast, bssid broadcasts and multicast to mac80211 */
3047 rfilt = (ath5k_hw_get_rx_filter(ah) & (AR5K_RX_FILTER_PHYERR)) |
3048 (AR5K_RX_FILTER_UCAST | AR5K_RX_FILTER_BCAST |
3049 AR5K_RX_FILTER_MCAST);
3050
3051 if (changed_flags & (FIF_PROMISC_IN_BSS | FIF_OTHER_BSS)) {
3052 if (*new_flags & FIF_PROMISC_IN_BSS) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003053 __set_bit(ATH_STAT_PROMISC, sc->status);
John Daiker0bbac082008-10-17 12:16:00 -07003054 } else {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003055 __clear_bit(ATH_STAT_PROMISC, sc->status);
John Daiker0bbac082008-10-17 12:16:00 -07003056 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003057 }
3058
Bob Copeland6b5dccc2010-06-04 08:14:14 -04003059 if (test_bit(ATH_STAT_PROMISC, sc->status))
3060 rfilt |= AR5K_RX_FILTER_PROM;
3061
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003062 /* Note, AR5K_RX_FILTER_MCAST is already enabled */
3063 if (*new_flags & FIF_ALLMULTI) {
3064 mfilt[0] = ~0;
3065 mfilt[1] = ~0;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003066 }
3067
3068 /* This is the best we can do */
3069 if (*new_flags & (FIF_FCSFAIL | FIF_PLCPFAIL))
3070 rfilt |= AR5K_RX_FILTER_PHYERR;
3071
3072 /* FIF_BCN_PRBRESP_PROMISC really means to enable beacons
Bob Copeland30bf4162010-08-15 13:03:15 -04003073 * and probes for any BSSID */
Ben Greearb1ae1ed2010-09-30 12:22:58 -07003074 if ((*new_flags & FIF_BCN_PRBRESP_PROMISC) || (sc->nvifs > 1))
Bob Copeland30bf4162010-08-15 13:03:15 -04003075 rfilt |= AR5K_RX_FILTER_BEACON;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003076
3077 /* FIF_CONTROL doc says that if FIF_PROMISC_IN_BSS is not
3078 * set we should only pass on control frames for this
3079 * station. This needs testing. I believe right now this
3080 * enables *all* control frames, which is OK.. but
3081 * but we should see if we can improve on granularity */
3082 if (*new_flags & FIF_CONTROL)
3083 rfilt |= AR5K_RX_FILTER_CONTROL;
3084
3085 /* Additional settings per mode -- this is per ath5k */
3086
3087 /* XXX move these to mac80211, and add a beacon IFF flag to mac80211 */
3088
Bob Copeland56d1de02009-08-24 23:00:30 -04003089 switch (sc->opmode) {
3090 case NL80211_IFTYPE_MESH_POINT:
Bob Copeland56d1de02009-08-24 23:00:30 -04003091 rfilt |= AR5K_RX_FILTER_CONTROL |
3092 AR5K_RX_FILTER_BEACON |
3093 AR5K_RX_FILTER_PROBEREQ |
3094 AR5K_RX_FILTER_PROM;
3095 break;
3096 case NL80211_IFTYPE_AP:
3097 case NL80211_IFTYPE_ADHOC:
3098 rfilt |= AR5K_RX_FILTER_PROBEREQ |
3099 AR5K_RX_FILTER_BEACON;
3100 break;
3101 case NL80211_IFTYPE_STATION:
3102 if (sc->assoc)
3103 rfilt |= AR5K_RX_FILTER_BEACON;
3104 default:
3105 break;
3106 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003107
3108 /* Set filters */
John Daiker0bbac082008-10-17 12:16:00 -07003109 ath5k_hw_set_rx_filter(ah, rfilt);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003110
3111 /* Set multicast bits */
3112 ath5k_hw_set_mcast_filter(ah, mfilt[0], mfilt[1]);
Bob Copelanda180a132010-08-15 13:03:12 -04003113 /* Set the cached hw filter flags, this will later actually
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003114 * be set in HW */
3115 sc->filter_flags = rfilt;
Bob Copeland56d1de02009-08-24 23:00:30 -04003116
3117 mutex_unlock(&sc->lock);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003118}
3119
3120static int
3121ath5k_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Johannes Bergdc822b52008-12-29 12:55:09 +01003122 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
3123 struct ieee80211_key_conf *key)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003124{
3125 struct ath5k_softc *sc = hw->priv;
Luis R. Rodriguezdc1e0012009-11-04 17:47:31 -08003126 struct ath5k_hw *ah = sc->ah;
3127 struct ath_common *common = ath5k_hw_common(ah);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003128 int ret = 0;
3129
Bob Copeland9ad9a262008-10-29 08:30:54 -04003130 if (modparam_nohwcrypt)
3131 return -EOPNOTSUPP;
3132
Johannes Berg97359d12010-08-10 09:46:38 +02003133 switch (key->cipher) {
3134 case WLAN_CIPHER_SUITE_WEP40:
3135 case WLAN_CIPHER_SUITE_WEP104:
3136 case WLAN_CIPHER_SUITE_TKIP:
Bob Copeland3f64b432008-10-29 23:19:14 -04003137 break;
Johannes Berg97359d12010-08-10 09:46:38 +02003138 case WLAN_CIPHER_SUITE_CCMP:
Bruno Randolf781f3132010-09-08 16:04:59 +09003139 if (common->crypt_caps & ATH_CRYPT_CAP_CIPHER_AESCCM)
Bob Copeland1c818742009-08-24 23:00:33 -04003140 break;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003141 return -EOPNOTSUPP;
3142 default:
3143 WARN_ON(1);
3144 return -EINVAL;
3145 }
3146
3147 mutex_lock(&sc->lock);
3148
3149 switch (cmd) {
3150 case SET_KEY:
Bruno Randolfe0f8c2a2010-09-08 16:04:43 +09003151 ret = ath_key_config(common, vif, sta, key);
3152 if (ret >= 0) {
3153 key->hw_key_idx = ret;
3154 /* push IV and Michael MIC generation to stack */
3155 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
3156 if (key->cipher == WLAN_CIPHER_SUITE_TKIP)
3157 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
3158 if (key->cipher == WLAN_CIPHER_SUITE_CCMP)
3159 key->flags |= IEEE80211_KEY_FLAG_SW_MGMT;
3160 ret = 0;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003161 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003162 break;
3163 case DISABLE_KEY:
Bruno Randolfe0f8c2a2010-09-08 16:04:43 +09003164 ath_key_delete(common, key);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003165 break;
3166 default:
3167 ret = -EINVAL;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003168 }
3169
Jiri Slaby274c7c32008-07-15 17:44:20 +02003170 mmiowb();
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003171 mutex_unlock(&sc->lock);
3172 return ret;
3173}
3174
3175static int
3176ath5k_get_stats(struct ieee80211_hw *hw,
3177 struct ieee80211_low_level_stats *stats)
3178{
3179 struct ath5k_softc *sc = hw->priv;
Nick Kossifidis194828a2008-04-16 18:49:02 +03003180
3181 /* Force update */
Bruno Randolf495391d2010-03-25 14:49:36 +09003182 ath5k_hw_update_mib_counters(sc->ah);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003183
Bruno Randolf495391d2010-03-25 14:49:36 +09003184 stats->dot11ACKFailureCount = sc->stats.ack_fail;
3185 stats->dot11RTSFailureCount = sc->stats.rts_fail;
3186 stats->dot11RTSSuccessCount = sc->stats.rts_ok;
3187 stats->dot11FCSErrorCount = sc->stats.fcs_error;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003188
3189 return 0;
3190}
3191
Holger Schurig55ee82b2010-04-19 10:24:22 +02003192static int ath5k_get_survey(struct ieee80211_hw *hw, int idx,
3193 struct survey_info *survey)
3194{
3195 struct ath5k_softc *sc = hw->priv;
3196 struct ieee80211_conf *conf = &hw->conf;
3197
3198 if (idx != 0)
3199 return -ENOENT;
3200
3201 survey->channel = conf->channel;
3202 survey->filled = SURVEY_INFO_NOISE_DBM;
3203 survey->noise = sc->ah->ah_noise_floor;
3204
3205 return 0;
3206}
3207
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003208static u64
3209ath5k_get_tsf(struct ieee80211_hw *hw)
3210{
3211 struct ath5k_softc *sc = hw->priv;
3212
3213 return ath5k_hw_get_tsf64(sc->ah);
3214}
3215
3216static void
Alina Friedrichsen3b5d6652009-01-24 07:09:59 +01003217ath5k_set_tsf(struct ieee80211_hw *hw, u64 tsf)
3218{
3219 struct ath5k_softc *sc = hw->priv;
3220
3221 ath5k_hw_set_tsf64(sc->ah, tsf);
3222}
3223
3224static void
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003225ath5k_reset_tsf(struct ieee80211_hw *hw)
3226{
3227 struct ath5k_softc *sc = hw->priv;
3228
Bruno Randolf9804b982008-01-19 18:17:59 +09003229 /*
3230 * in IBSS mode we need to update the beacon timers too.
3231 * this will also reset the TSF if we call it with 0
3232 */
Johannes Berg05c914f2008-09-11 00:01:58 +02003233 if (sc->opmode == NL80211_IFTYPE_ADHOC)
Bruno Randolf9804b982008-01-19 18:17:59 +09003234 ath5k_beacon_update_timers(sc, 0);
3235 else
3236 ath5k_hw_reset_tsf(sc->ah);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003237}
3238
Martin Xu02969b32008-11-24 10:49:27 +08003239static void
3240set_beacon_filter(struct ieee80211_hw *hw, bool enable)
3241{
3242 struct ath5k_softc *sc = hw->priv;
3243 struct ath5k_hw *ah = sc->ah;
3244 u32 rfilt;
3245 rfilt = ath5k_hw_get_rx_filter(ah);
3246 if (enable)
3247 rfilt |= AR5K_RX_FILTER_BEACON;
3248 else
3249 rfilt &= ~AR5K_RX_FILTER_BEACON;
3250 ath5k_hw_set_rx_filter(ah, rfilt);
3251 sc->filter_flags = rfilt;
3252}
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003253
Martin Xu02969b32008-11-24 10:49:27 +08003254static void ath5k_bss_info_changed(struct ieee80211_hw *hw,
3255 struct ieee80211_vif *vif,
3256 struct ieee80211_bss_conf *bss_conf,
3257 u32 changes)
3258{
Ben Greearb1ae1ed2010-09-30 12:22:58 -07003259 struct ath5k_vif *avf = (void *)vif->drv_priv;
Martin Xu02969b32008-11-24 10:49:27 +08003260 struct ath5k_softc *sc = hw->priv;
Johannes Berg2d0ddec2009-04-23 16:13:26 +02003261 struct ath5k_hw *ah = sc->ah;
Luis R. Rodriguez954fece2009-09-10 10:51:33 -07003262 struct ath_common *common = ath5k_hw_common(ah);
Bob Copeland21800492009-07-04 12:59:52 -04003263 unsigned long flags;
Johannes Berg2d0ddec2009-04-23 16:13:26 +02003264
3265 mutex_lock(&sc->lock);
Johannes Berg2d0ddec2009-04-23 16:13:26 +02003266
3267 if (changes & BSS_CHANGED_BSSID) {
3268 /* Cache for later use during resets */
Luis R. Rodriguez954fece2009-09-10 10:51:33 -07003269 memcpy(common->curbssid, bss_conf->bssid, ETH_ALEN);
Luis R. Rodriguez8ce54c52009-10-06 20:44:34 -04003270 common->curaid = 0;
Nick Kossifidis418de6d2010-08-15 13:03:10 -04003271 ath5k_hw_set_bssid(ah);
Johannes Berg2d0ddec2009-04-23 16:13:26 +02003272 mmiowb();
3273 }
Johannes Berg57c4d7b2009-04-23 16:10:04 +02003274
3275 if (changes & BSS_CHANGED_BEACON_INT)
3276 sc->bintval = bss_conf->beacon_int;
3277
Martin Xu02969b32008-11-24 10:49:27 +08003278 if (changes & BSS_CHANGED_ASSOC) {
Ben Greearb1ae1ed2010-09-30 12:22:58 -07003279 avf->assoc = bss_conf->assoc;
3280 if (bss_conf->assoc)
3281 sc->assoc = bss_conf->assoc;
3282 else
3283 sc->assoc = ath_any_vif_assoc(sc);
3284
Martin Xu02969b32008-11-24 10:49:27 +08003285 if (sc->opmode == NL80211_IFTYPE_STATION)
3286 set_beacon_filter(hw, sc->assoc);
Bob Copelandf0f3d382009-06-10 22:22:21 -04003287 ath5k_hw_set_ledstate(sc->ah, sc->assoc ?
3288 AR5K_LED_ASSOC : AR5K_LED_INIT);
Luis R. Rodriguez8ce54c52009-10-06 20:44:34 -04003289 if (bss_conf->assoc) {
3290 ATH5K_DBG(sc, ATH5K_DEBUG_ANY,
3291 "Bss Info ASSOC %d, bssid: %pM\n",
3292 bss_conf->aid, common->curbssid);
3293 common->curaid = bss_conf->aid;
Nick Kossifidis418de6d2010-08-15 13:03:10 -04003294 ath5k_hw_set_bssid(ah);
Luis R. Rodriguez8ce54c52009-10-06 20:44:34 -04003295 /* Once ANI is available you would start it here */
3296 }
Martin Xu02969b32008-11-24 10:49:27 +08003297 }
Johannes Berg2d0ddec2009-04-23 16:13:26 +02003298
Bob Copeland21800492009-07-04 12:59:52 -04003299 if (changes & BSS_CHANGED_BEACON) {
3300 spin_lock_irqsave(&sc->block, flags);
3301 ath5k_beacon_update(hw, vif);
3302 spin_unlock_irqrestore(&sc->block, flags);
Johannes Berg2d0ddec2009-04-23 16:13:26 +02003303 }
3304
Bob Copeland21800492009-07-04 12:59:52 -04003305 if (changes & BSS_CHANGED_BEACON_ENABLED)
3306 sc->enable_beacon = bss_conf->enable_beacon;
3307
3308 if (changes & (BSS_CHANGED_BEACON | BSS_CHANGED_BEACON_ENABLED |
3309 BSS_CHANGED_BEACON_INT))
3310 ath5k_beacon_config(sc);
3311
Johannes Berg2d0ddec2009-04-23 16:13:26 +02003312 mutex_unlock(&sc->lock);
Martin Xu02969b32008-11-24 10:49:27 +08003313}
Bob Copelandf0f3d382009-06-10 22:22:21 -04003314
3315static void ath5k_sw_scan_start(struct ieee80211_hw *hw)
3316{
3317 struct ath5k_softc *sc = hw->priv;
3318 if (!sc->assoc)
3319 ath5k_hw_set_ledstate(sc->ah, AR5K_LED_SCAN);
3320}
3321
3322static void ath5k_sw_scan_complete(struct ieee80211_hw *hw)
3323{
3324 struct ath5k_softc *sc = hw->priv;
3325 ath5k_hw_set_ledstate(sc->ah, sc->assoc ?
3326 AR5K_LED_ASSOC : AR5K_LED_INIT);
3327}
Lukáš Turek6e08d222009-12-21 22:50:51 +01003328
3329/**
3330 * ath5k_set_coverage_class - Set IEEE 802.11 coverage class
3331 *
3332 * @hw: struct ieee80211_hw pointer
3333 * @coverage_class: IEEE 802.11 coverage class number
3334 *
3335 * Mac80211 callback. Sets slot time, ACK timeout and CTS timeout for given
3336 * coverage class. The values are persistent, they are restored after device
3337 * reset.
3338 */
3339static void ath5k_set_coverage_class(struct ieee80211_hw *hw, u8 coverage_class)
3340{
3341 struct ath5k_softc *sc = hw->priv;
3342
3343 mutex_lock(&sc->lock);
3344 ath5k_hw_set_coverage_class(sc->ah, coverage_class);
3345 mutex_unlock(&sc->lock);
3346}
Bob Copeland8a63fac2010-09-17 12:45:07 +09003347
Bruno Randolfe0b1cc52010-09-17 11:37:18 +09003348static int ath5k_conf_tx(struct ieee80211_hw *hw, u16 queue,
3349 const struct ieee80211_tx_queue_params *params)
3350{
3351 struct ath5k_softc *sc = hw->priv;
3352 struct ath5k_hw *ah = sc->ah;
3353 struct ath5k_txq_info qi;
3354 int ret = 0;
3355
3356 if (queue >= ah->ah_capabilities.cap_queues.q_tx_num)
3357 return 0;
3358
3359 mutex_lock(&sc->lock);
3360
3361 ath5k_hw_get_tx_queueprops(ah, queue, &qi);
3362
3363 qi.tqi_aifs = params->aifs;
3364 qi.tqi_cw_min = params->cw_min;
3365 qi.tqi_cw_max = params->cw_max;
3366 qi.tqi_burst_time = params->txop;
3367
3368 ATH5K_DBG(sc, ATH5K_DEBUG_ANY,
3369 "Configure tx [queue %d], "
3370 "aifs: %d, cw_min: %d, cw_max: %d, txop: %d\n",
3371 queue, params->aifs, params->cw_min,
3372 params->cw_max, params->txop);
3373
3374 if (ath5k_hw_set_tx_queueprops(ah, queue, &qi)) {
3375 ATH5K_ERR(sc,
3376 "Unable to update hardware queue %u!\n", queue);
3377 ret = -EIO;
3378 } else
3379 ath5k_hw_reset_tx_queue(ah, queue);
3380
3381 mutex_unlock(&sc->lock);
3382
3383 return ret;
3384}
3385
Bob Copeland8a63fac2010-09-17 12:45:07 +09003386static const struct ieee80211_ops ath5k_hw_ops = {
3387 .tx = ath5k_tx,
3388 .start = ath5k_start,
3389 .stop = ath5k_stop,
3390 .add_interface = ath5k_add_interface,
3391 .remove_interface = ath5k_remove_interface,
3392 .config = ath5k_config,
3393 .prepare_multicast = ath5k_prepare_multicast,
3394 .configure_filter = ath5k_configure_filter,
3395 .set_key = ath5k_set_key,
3396 .get_stats = ath5k_get_stats,
3397 .get_survey = ath5k_get_survey,
Bruno Randolfe0b1cc52010-09-17 11:37:18 +09003398 .conf_tx = ath5k_conf_tx,
Bob Copeland8a63fac2010-09-17 12:45:07 +09003399 .get_tsf = ath5k_get_tsf,
3400 .set_tsf = ath5k_set_tsf,
3401 .reset_tsf = ath5k_reset_tsf,
3402 .bss_info_changed = ath5k_bss_info_changed,
3403 .sw_scan_start = ath5k_sw_scan_start,
3404 .sw_scan_complete = ath5k_sw_scan_complete,
3405 .set_coverage_class = ath5k_set_coverage_class,
3406};
3407
3408/********************\
3409* PCI Initialization *
3410\********************/
3411
3412static int __devinit
3413ath5k_pci_probe(struct pci_dev *pdev,
3414 const struct pci_device_id *id)
3415{
3416 void __iomem *mem;
3417 struct ath5k_softc *sc;
3418 struct ath_common *common;
3419 struct ieee80211_hw *hw;
3420 int ret;
3421 u8 csz;
3422
3423 /*
3424 * L0s needs to be disabled on all ath5k cards.
3425 *
3426 * For distributions shipping with CONFIG_PCIEASPM (this will be enabled
3427 * by default in the future in 2.6.36) this will also mean both L1 and
3428 * L0s will be disabled when a pre 1.1 PCIe device is detected. We do
3429 * know L1 works correctly even for all ath5k pre 1.1 PCIe devices
3430 * though but cannot currently undue the effect of a blacklist, for
3431 * details you can read pcie_aspm_sanity_check() and see how it adjusts
3432 * the device link capability.
3433 *
3434 * It may be possible in the future to implement some PCI API to allow
3435 * drivers to override blacklists for pre 1.1 PCIe but for now it is
3436 * best to accept that both L0s and L1 will be disabled completely for
3437 * distributions shipping with CONFIG_PCIEASPM rather than having this
3438 * issue present. Motivation for adding this new API will be to help
3439 * with power consumption for some of these devices.
3440 */
3441 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S);
3442
3443 ret = pci_enable_device(pdev);
3444 if (ret) {
3445 dev_err(&pdev->dev, "can't enable device\n");
3446 goto err;
3447 }
3448
3449 /* XXX 32-bit addressing only */
3450 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
3451 if (ret) {
3452 dev_err(&pdev->dev, "32-bit DMA not available\n");
3453 goto err_dis;
3454 }
3455
3456 /*
3457 * Cache line size is used to size and align various
3458 * structures used to communicate with the hardware.
3459 */
3460 pci_read_config_byte(pdev, PCI_CACHE_LINE_SIZE, &csz);
3461 if (csz == 0) {
3462 /*
3463 * Linux 2.4.18 (at least) writes the cache line size
3464 * register as a 16-bit wide register which is wrong.
3465 * We must have this setup properly for rx buffer
3466 * DMA to work so force a reasonable value here if it
3467 * comes up zero.
3468 */
3469 csz = L1_CACHE_BYTES >> 2;
3470 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, csz);
3471 }
3472 /*
3473 * The default setting of latency timer yields poor results,
3474 * set it to the value used by other systems. It may be worth
3475 * tweaking this setting more.
3476 */
3477 pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0xa8);
3478
3479 /* Enable bus mastering */
3480 pci_set_master(pdev);
3481
3482 /*
3483 * Disable the RETRY_TIMEOUT register (0x41) to keep
3484 * PCI Tx retries from interfering with C3 CPU state.
3485 */
3486 pci_write_config_byte(pdev, 0x41, 0);
3487
3488 ret = pci_request_region(pdev, 0, "ath5k");
3489 if (ret) {
3490 dev_err(&pdev->dev, "cannot reserve PCI memory region\n");
3491 goto err_dis;
3492 }
3493
3494 mem = pci_iomap(pdev, 0, 0);
3495 if (!mem) {
3496 dev_err(&pdev->dev, "cannot remap PCI memory region\n") ;
3497 ret = -EIO;
3498 goto err_reg;
3499 }
3500
3501 /*
3502 * Allocate hw (mac80211 main struct)
3503 * and hw->priv (driver private data)
3504 */
3505 hw = ieee80211_alloc_hw(sizeof(*sc), &ath5k_hw_ops);
3506 if (hw == NULL) {
3507 dev_err(&pdev->dev, "cannot allocate ieee80211_hw\n");
3508 ret = -ENOMEM;
3509 goto err_map;
3510 }
3511
3512 dev_info(&pdev->dev, "registered as '%s'\n", wiphy_name(hw->wiphy));
3513
3514 /* Initialize driver private data */
3515 SET_IEEE80211_DEV(hw, &pdev->dev);
3516 hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
3517 IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
3518 IEEE80211_HW_SIGNAL_DBM;
3519
3520 hw->wiphy->interface_modes =
3521 BIT(NL80211_IFTYPE_AP) |
3522 BIT(NL80211_IFTYPE_STATION) |
3523 BIT(NL80211_IFTYPE_ADHOC) |
3524 BIT(NL80211_IFTYPE_MESH_POINT);
3525
3526 hw->extra_tx_headroom = 2;
3527 hw->channel_change_time = 5000;
3528 sc = hw->priv;
3529 sc->hw = hw;
3530 sc->pdev = pdev;
3531
3532 ath5k_debug_init_device(sc);
3533
3534 /*
3535 * Mark the device as detached to avoid processing
3536 * interrupts until setup is complete.
3537 */
3538 __set_bit(ATH_STAT_INVALID, sc->status);
3539
3540 sc->iobase = mem; /* So we can unmap it on detach */
3541 sc->opmode = NL80211_IFTYPE_STATION;
3542 sc->bintval = 1000;
3543 mutex_init(&sc->lock);
3544 spin_lock_init(&sc->rxbuflock);
3545 spin_lock_init(&sc->txbuflock);
3546 spin_lock_init(&sc->block);
3547
3548 /* Set private data */
3549 pci_set_drvdata(pdev, sc);
3550
3551 /* Setup interrupt handler */
3552 ret = request_irq(pdev->irq, ath5k_intr, IRQF_SHARED, "ath", sc);
3553 if (ret) {
3554 ATH5K_ERR(sc, "request_irq failed\n");
3555 goto err_free;
3556 }
3557
3558 /* If we passed the test, malloc an ath5k_hw struct */
3559 sc->ah = kzalloc(sizeof(struct ath5k_hw), GFP_KERNEL);
3560 if (!sc->ah) {
3561 ret = -ENOMEM;
3562 ATH5K_ERR(sc, "out of memory\n");
3563 goto err_irq;
3564 }
3565
3566 sc->ah->ah_sc = sc;
3567 sc->ah->ah_iobase = sc->iobase;
3568 common = ath5k_hw_common(sc->ah);
3569 common->ops = &ath5k_common_ops;
3570 common->ah = sc->ah;
3571 common->hw = hw;
3572 common->cachelsz = csz << 2; /* convert to bytes */
3573
3574 /* Initialize device */
3575 ret = ath5k_hw_attach(sc);
3576 if (ret) {
3577 goto err_free_ah;
3578 }
3579
3580 /* set up multi-rate retry capabilities */
3581 if (sc->ah->ah_version == AR5K_AR5212) {
3582 hw->max_rates = 4;
3583 hw->max_rate_tries = 11;
3584 }
3585
Ben Greearb1ae1ed2010-09-30 12:22:58 -07003586 hw->vif_data_size = sizeof(struct ath5k_vif);
3587
Bob Copeland8a63fac2010-09-17 12:45:07 +09003588 /* Finish private driver data initialization */
3589 ret = ath5k_attach(pdev, hw);
3590 if (ret)
3591 goto err_ah;
3592
3593 ATH5K_INFO(sc, "Atheros AR%s chip found (MAC: 0x%x, PHY: 0x%x)\n",
3594 ath5k_chip_name(AR5K_VERSION_MAC, sc->ah->ah_mac_srev),
3595 sc->ah->ah_mac_srev,
3596 sc->ah->ah_phy_revision);
3597
3598 if (!sc->ah->ah_single_chip) {
3599 /* Single chip radio (!RF5111) */
3600 if (sc->ah->ah_radio_5ghz_revision &&
3601 !sc->ah->ah_radio_2ghz_revision) {
3602 /* No 5GHz support -> report 2GHz radio */
3603 if (!test_bit(AR5K_MODE_11A,
3604 sc->ah->ah_capabilities.cap_mode)) {
3605 ATH5K_INFO(sc, "RF%s 2GHz radio found (0x%x)\n",
3606 ath5k_chip_name(AR5K_VERSION_RAD,
3607 sc->ah->ah_radio_5ghz_revision),
3608 sc->ah->ah_radio_5ghz_revision);
3609 /* No 2GHz support (5110 and some
3610 * 5Ghz only cards) -> report 5Ghz radio */
3611 } else if (!test_bit(AR5K_MODE_11B,
3612 sc->ah->ah_capabilities.cap_mode)) {
3613 ATH5K_INFO(sc, "RF%s 5GHz radio found (0x%x)\n",
3614 ath5k_chip_name(AR5K_VERSION_RAD,
3615 sc->ah->ah_radio_5ghz_revision),
3616 sc->ah->ah_radio_5ghz_revision);
3617 /* Multiband radio */
3618 } else {
3619 ATH5K_INFO(sc, "RF%s multiband radio found"
3620 " (0x%x)\n",
3621 ath5k_chip_name(AR5K_VERSION_RAD,
3622 sc->ah->ah_radio_5ghz_revision),
3623 sc->ah->ah_radio_5ghz_revision);
3624 }
3625 }
3626 /* Multi chip radio (RF5111 - RF2111) ->
3627 * report both 2GHz/5GHz radios */
3628 else if (sc->ah->ah_radio_5ghz_revision &&
3629 sc->ah->ah_radio_2ghz_revision){
3630 ATH5K_INFO(sc, "RF%s 5GHz radio found (0x%x)\n",
3631 ath5k_chip_name(AR5K_VERSION_RAD,
3632 sc->ah->ah_radio_5ghz_revision),
3633 sc->ah->ah_radio_5ghz_revision);
3634 ATH5K_INFO(sc, "RF%s 2GHz radio found (0x%x)\n",
3635 ath5k_chip_name(AR5K_VERSION_RAD,
3636 sc->ah->ah_radio_2ghz_revision),
3637 sc->ah->ah_radio_2ghz_revision);
3638 }
3639 }
3640
3641
3642 /* ready to process interrupts */
3643 __clear_bit(ATH_STAT_INVALID, sc->status);
3644
3645 return 0;
3646err_ah:
3647 ath5k_hw_detach(sc->ah);
3648err_free_ah:
3649 kfree(sc->ah);
3650err_irq:
3651 free_irq(pdev->irq, sc);
3652err_free:
3653 ieee80211_free_hw(hw);
3654err_map:
3655 pci_iounmap(pdev, mem);
3656err_reg:
3657 pci_release_region(pdev, 0);
3658err_dis:
3659 pci_disable_device(pdev);
3660err:
3661 return ret;
3662}
3663
3664static void __devexit
3665ath5k_pci_remove(struct pci_dev *pdev)
3666{
3667 struct ath5k_softc *sc = pci_get_drvdata(pdev);
3668
3669 ath5k_debug_finish_device(sc);
3670 ath5k_detach(pdev, sc->hw);
3671 ath5k_hw_detach(sc->ah);
3672 kfree(sc->ah);
3673 free_irq(pdev->irq, sc);
3674 pci_iounmap(pdev, sc->iobase);
3675 pci_release_region(pdev, 0);
3676 pci_disable_device(pdev);
3677 ieee80211_free_hw(sc->hw);
3678}
3679
3680#ifdef CONFIG_PM_SLEEP
3681static int ath5k_pci_suspend(struct device *dev)
3682{
3683 struct ath5k_softc *sc = pci_get_drvdata(to_pci_dev(dev));
3684
3685 ath5k_led_off(sc);
3686 return 0;
3687}
3688
3689static int ath5k_pci_resume(struct device *dev)
3690{
3691 struct pci_dev *pdev = to_pci_dev(dev);
3692 struct ath5k_softc *sc = pci_get_drvdata(pdev);
3693
3694 /*
3695 * Suspend/Resume resets the PCI configuration space, so we have to
3696 * re-disable the RETRY_TIMEOUT register (0x41) to keep
3697 * PCI Tx retries from interfering with C3 CPU state
3698 */
3699 pci_write_config_byte(pdev, 0x41, 0);
3700
3701 ath5k_led_enable(sc);
3702 return 0;
3703}
3704
3705static SIMPLE_DEV_PM_OPS(ath5k_pm_ops, ath5k_pci_suspend, ath5k_pci_resume);
3706#define ATH5K_PM_OPS (&ath5k_pm_ops)
3707#else
3708#define ATH5K_PM_OPS NULL
3709#endif /* CONFIG_PM_SLEEP */
3710
3711static struct pci_driver ath5k_pci_driver = {
3712 .name = KBUILD_MODNAME,
3713 .id_table = ath5k_pci_id_table,
3714 .probe = ath5k_pci_probe,
3715 .remove = __devexit_p(ath5k_pci_remove),
3716 .driver.pm = ATH5K_PM_OPS,
3717};
3718
3719/*
3720 * Module init/exit functions
3721 */
3722static int __init
3723init_ath5k_pci(void)
3724{
3725 int ret;
3726
3727 ath5k_debug_init();
3728
3729 ret = pci_register_driver(&ath5k_pci_driver);
3730 if (ret) {
3731 printk(KERN_ERR "ath5k_pci: can't register pci driver\n");
3732 return ret;
3733 }
3734
3735 return 0;
3736}
3737
3738static void __exit
3739exit_ath5k_pci(void)
3740{
3741 pci_unregister_driver(&ath5k_pci_driver);
3742
3743 ath5k_debug_finish();
3744}
3745
3746module_init(init_ath5k_pci);
3747module_exit(exit_ath5k_pci);