blob: cdd19232960c2541ad8eca48c5b008b397448f37 [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
Joe Perches516304b2012-03-18 17:30:52 -070043#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
44
Jiri Slabyfa1c1142007-08-12 17:33:16 +020045#include <linux/module.h>
46#include <linux/delay.h>
Alexey Dobriyanb7f080c2011-06-16 11:01:34 +000047#include <linux/dma-mapping.h>
Jiri Slaby274c7c32008-07-15 17:44:20 +020048#include <linux/hardirq.h>
Jiri Slabyfa1c1142007-08-12 17:33:16 +020049#include <linux/if.h>
Jiri Slaby274c7c32008-07-15 17:44:20 +020050#include <linux/io.h>
Jiri Slabyfa1c1142007-08-12 17:33:16 +020051#include <linux/netdevice.h>
52#include <linux/cache.h>
Jiri Slabyfa1c1142007-08-12 17:33:16 +020053#include <linux/ethtool.h>
54#include <linux/uaccess.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090055#include <linux/slab.h>
Ben Greearb1ae1ed2010-09-30 12:22:58 -070056#include <linux/etherdevice.h>
Pavel Roskin931be262011-07-26 22:26:59 -040057#include <linux/nl80211.h>
Jiri Slabyfa1c1142007-08-12 17:33:16 +020058
59#include <net/ieee80211_radiotap.h>
60
61#include <asm/unaligned.h>
62
63#include "base.h"
64#include "reg.h"
65#include "debug.h"
Bruno Randolf2111ac02010-04-02 18:44:08 +090066#include "ani.h"
Pavel Roskin931be262011-07-26 22:26:59 -040067#include "ath5k.h"
68#include "../regd.h"
Jiri Slabyfa1c1142007-08-12 17:33:16 +020069
Bob Copeland0e472252011-01-24 23:32:55 -050070#define CREATE_TRACE_POINTS
71#include "trace.h"
72
Rusty Russelleb939922011-12-19 14:08:01 +000073bool ath5k_modparam_nohwcrypt;
John W. Linville18cb6e32011-01-05 09:39:59 -050074module_param_named(nohwcrypt, ath5k_modparam_nohwcrypt, bool, S_IRUGO);
Bob Copeland9ad9a262008-10-29 08:30:54 -040075MODULE_PARM_DESC(nohwcrypt, "Disable hardware encryption.");
Jiri Slabyfa1c1142007-08-12 17:33:16 +020076
Rusty Russelleb939922011-12-19 14:08:01 +000077static bool modparam_fastchanswitch;
Nick Kossifidisa99168e2011-06-02 03:09:48 +030078module_param_named(fastchanswitch, modparam_fastchanswitch, bool, S_IRUGO);
79MODULE_PARM_DESC(fastchanswitch, "Enable fast channel switching for AR2413/AR5413 radios.");
80
John W. Linville11deb532012-01-24 14:58:47 -050081static bool ath5k_modparam_no_hw_rfkill_switch;
Nick Kossifidis84e1e732011-11-25 20:40:27 +020082module_param_named(no_hw_rfkill_switch, ath5k_modparam_no_hw_rfkill_switch,
83 bool, S_IRUGO);
84MODULE_PARM_DESC(no_hw_rfkill_switch, "Ignore the GPIO RFKill switch state");
85
Nick Kossifidisa99168e2011-06-02 03:09:48 +030086
Jiri Slabyfa1c1142007-08-12 17:33:16 +020087/* Module info */
88MODULE_AUTHOR("Jiri Slaby");
89MODULE_AUTHOR("Nick Kossifidis");
90MODULE_DESCRIPTION("Support for 5xxx series of Atheros 802.11 wireless LAN cards.");
91MODULE_SUPPORTED_DEVICE("Atheros 5xxx WLAN cards");
92MODULE_LICENSE("Dual BSD/GPL");
Jiri Slabyfa1c1142007-08-12 17:33:16 +020093
Felix Fietkau132b1c32010-12-02 10:26:56 +010094static int ath5k_init(struct ieee80211_hw *hw);
Pavel Roskine0d687b2011-07-14 20:21:55 -040095static int ath5k_reset(struct ath5k_hw *ah, struct ieee80211_channel *chan,
Nick Kossifidis8aec7af2010-11-23 21:39:28 +020096 bool skip_pcu);
Jiri Slabyfa1c1142007-08-12 17:33:16 +020097
Jiri Slabyfa1c1142007-08-12 17:33:16 +020098/* Known SREVs */
Jiri Slaby2c91108c2009-03-07 10:26:41 +010099static const struct ath5k_srev_name srev_names[] = {
Felix Fietkaua0b907e2010-12-02 10:27:16 +0100100#ifdef CONFIG_ATHEROS_AR231X
101 { "5312", AR5K_VERSION_MAC, AR5K_SREV_AR5312_R2 },
102 { "5312", AR5K_VERSION_MAC, AR5K_SREV_AR5312_R7 },
103 { "2313", AR5K_VERSION_MAC, AR5K_SREV_AR2313_R8 },
104 { "2315", AR5K_VERSION_MAC, AR5K_SREV_AR2315_R6 },
105 { "2315", AR5K_VERSION_MAC, AR5K_SREV_AR2315_R7 },
106 { "2317", AR5K_VERSION_MAC, AR5K_SREV_AR2317_R1 },
107 { "2317", AR5K_VERSION_MAC, AR5K_SREV_AR2317_R2 },
108#else
Nick Kossifidis1bef0162008-09-29 02:09:09 +0300109 { "5210", AR5K_VERSION_MAC, AR5K_SREV_AR5210 },
110 { "5311", AR5K_VERSION_MAC, AR5K_SREV_AR5311 },
111 { "5311A", AR5K_VERSION_MAC, AR5K_SREV_AR5311A },
112 { "5311B", AR5K_VERSION_MAC, AR5K_SREV_AR5311B },
113 { "5211", AR5K_VERSION_MAC, AR5K_SREV_AR5211 },
114 { "5212", AR5K_VERSION_MAC, AR5K_SREV_AR5212 },
115 { "5213", AR5K_VERSION_MAC, AR5K_SREV_AR5213 },
116 { "5213A", AR5K_VERSION_MAC, AR5K_SREV_AR5213A },
117 { "2413", AR5K_VERSION_MAC, AR5K_SREV_AR2413 },
118 { "2414", AR5K_VERSION_MAC, AR5K_SREV_AR2414 },
119 { "5424", AR5K_VERSION_MAC, AR5K_SREV_AR5424 },
120 { "5413", AR5K_VERSION_MAC, AR5K_SREV_AR5413 },
121 { "5414", AR5K_VERSION_MAC, AR5K_SREV_AR5414 },
122 { "2415", AR5K_VERSION_MAC, AR5K_SREV_AR2415 },
123 { "5416", AR5K_VERSION_MAC, AR5K_SREV_AR5416 },
124 { "5418", AR5K_VERSION_MAC, AR5K_SREV_AR5418 },
125 { "2425", AR5K_VERSION_MAC, AR5K_SREV_AR2425 },
126 { "2417", AR5K_VERSION_MAC, AR5K_SREV_AR2417 },
Felix Fietkaua0b907e2010-12-02 10:27:16 +0100127#endif
Nick Kossifidis1bef0162008-09-29 02:09:09 +0300128 { "xxxxx", AR5K_VERSION_MAC, AR5K_SREV_UNKNOWN },
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200129 { "5110", AR5K_VERSION_RAD, AR5K_SREV_RAD_5110 },
130 { "5111", AR5K_VERSION_RAD, AR5K_SREV_RAD_5111 },
Nick Kossifidis1bef0162008-09-29 02:09:09 +0300131 { "5111A", AR5K_VERSION_RAD, AR5K_SREV_RAD_5111A },
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200132 { "2111", AR5K_VERSION_RAD, AR5K_SREV_RAD_2111 },
133 { "5112", AR5K_VERSION_RAD, AR5K_SREV_RAD_5112 },
134 { "5112A", AR5K_VERSION_RAD, AR5K_SREV_RAD_5112A },
Nick Kossifidis1bef0162008-09-29 02:09:09 +0300135 { "5112B", AR5K_VERSION_RAD, AR5K_SREV_RAD_5112B },
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200136 { "2112", AR5K_VERSION_RAD, AR5K_SREV_RAD_2112 },
137 { "2112A", AR5K_VERSION_RAD, AR5K_SREV_RAD_2112A },
Nick Kossifidis1bef0162008-09-29 02:09:09 +0300138 { "2112B", AR5K_VERSION_RAD, AR5K_SREV_RAD_2112B },
139 { "2413", AR5K_VERSION_RAD, AR5K_SREV_RAD_2413 },
140 { "5413", AR5K_VERSION_RAD, AR5K_SREV_RAD_5413 },
Nick Kossifidis1bef0162008-09-29 02:09:09 +0300141 { "5424", AR5K_VERSION_RAD, AR5K_SREV_RAD_5424 },
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200142 { "5133", AR5K_VERSION_RAD, AR5K_SREV_RAD_5133 },
Felix Fietkaua0b907e2010-12-02 10:27:16 +0100143#ifdef CONFIG_ATHEROS_AR231X
144 { "2316", AR5K_VERSION_RAD, AR5K_SREV_RAD_2316 },
145 { "2317", AR5K_VERSION_RAD, AR5K_SREV_RAD_2317 },
146#endif
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200147 { "xxxxx", AR5K_VERSION_RAD, AR5K_SREV_UNKNOWN },
148};
149
Jiri Slaby2c91108c2009-03-07 10:26:41 +0100150static const struct ieee80211_rate ath5k_rates[] = {
Bruno Randolf63266a62008-07-30 17:12:58 +0200151 { .bitrate = 10,
152 .hw_value = ATH5K_RATE_CODE_1M, },
153 { .bitrate = 20,
154 .hw_value = ATH5K_RATE_CODE_2M,
155 .hw_value_short = ATH5K_RATE_CODE_2M | AR5K_SET_SHORT_PREAMBLE,
156 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
157 { .bitrate = 55,
158 .hw_value = ATH5K_RATE_CODE_5_5M,
159 .hw_value_short = ATH5K_RATE_CODE_5_5M | AR5K_SET_SHORT_PREAMBLE,
160 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
161 { .bitrate = 110,
162 .hw_value = ATH5K_RATE_CODE_11M,
163 .hw_value_short = ATH5K_RATE_CODE_11M | AR5K_SET_SHORT_PREAMBLE,
164 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
165 { .bitrate = 60,
166 .hw_value = ATH5K_RATE_CODE_6M,
167 .flags = 0 },
168 { .bitrate = 90,
169 .hw_value = ATH5K_RATE_CODE_9M,
170 .flags = 0 },
171 { .bitrate = 120,
172 .hw_value = ATH5K_RATE_CODE_12M,
173 .flags = 0 },
174 { .bitrate = 180,
175 .hw_value = ATH5K_RATE_CODE_18M,
176 .flags = 0 },
177 { .bitrate = 240,
178 .hw_value = ATH5K_RATE_CODE_24M,
179 .flags = 0 },
180 { .bitrate = 360,
181 .hw_value = ATH5K_RATE_CODE_36M,
182 .flags = 0 },
183 { .bitrate = 480,
184 .hw_value = ATH5K_RATE_CODE_48M,
185 .flags = 0 },
186 { .bitrate = 540,
187 .hw_value = ATH5K_RATE_CODE_54M,
188 .flags = 0 },
Bruno Randolf63266a62008-07-30 17:12:58 +0200189};
190
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200191static inline u64 ath5k_extend_tsf(struct ath5k_hw *ah, u32 rstamp)
192{
193 u64 tsf = ath5k_hw_get_tsf64(ah);
194
195 if ((tsf & 0x7fff) < rstamp)
196 tsf -= 0x8000;
197
198 return (tsf & ~0x7fff) | rstamp;
199}
200
Felix Fietkaue5b046d2010-12-02 10:27:01 +0100201const char *
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200202ath5k_chip_name(enum ath5k_srev_type type, u_int16_t val)
203{
204 const char *name = "xxxxx";
205 unsigned int i;
206
207 for (i = 0; i < ARRAY_SIZE(srev_names); i++) {
208 if (srev_names[i].sr_type != type)
209 continue;
Nick Kossifidis75d0edb2008-09-29 01:24:44 +0300210
211 if ((val & 0xf0) == srev_names[i].sr_val)
212 name = srev_names[i].sr_name;
213
214 if ((val & 0xff) == srev_names[i].sr_val) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200215 name = srev_names[i].sr_name;
216 break;
217 }
218 }
219
220 return name;
221}
Luis R. Rodrigueze5aa8472009-09-10 16:55:11 -0700222static unsigned int ath5k_ioread32(void *hw_priv, u32 reg_offset)
223{
224 struct ath5k_hw *ah = (struct ath5k_hw *) hw_priv;
225 return ath5k_hw_reg_read(ah, reg_offset);
226}
227
228static void ath5k_iowrite32(void *hw_priv, u32 val, u32 reg_offset)
229{
230 struct ath5k_hw *ah = (struct ath5k_hw *) hw_priv;
231 ath5k_hw_reg_write(ah, val, reg_offset);
232}
233
234static const struct ath_ops ath5k_common_ops = {
235 .read = ath5k_ioread32,
236 .write = ath5k_iowrite32,
237};
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200238
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200239/***********************\
240* Driver Initialization *
241\***********************/
242
Bob Copelandf769c362009-03-30 22:30:31 -0400243static int ath5k_reg_notifier(struct wiphy *wiphy, struct regulatory_request *request)
244{
245 struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
Pavel Roskine0d687b2011-07-14 20:21:55 -0400246 struct ath5k_hw *ah = hw->priv;
247 struct ath_regulatory *regulatory = ath5k_hw_regulatory(ah);
Bob Copelandf769c362009-03-30 22:30:31 -0400248
Luis R. Rodriguez608b88c2009-08-17 18:07:23 -0700249 return ath_reg_notifier_apply(wiphy, request, regulatory);
Bob Copelandf769c362009-03-30 22:30:31 -0400250}
251
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200252/********************\
253* Channel/mode setup *
254\********************/
255
256/*
Luis R. Rodriguez2f8684c2012-07-06 15:21:51 -0700257 * Returns true for the channel numbers used.
Bob Copeland42639fc2009-03-30 08:05:29 -0400258 */
Luis R. Rodriguez2f8684c2012-07-06 15:21:51 -0700259#ifdef CONFIG_ATH5K_TEST_CHANNELS
260static bool ath5k_is_standard_channel(short chan, enum ieee80211_band band)
261{
262 return true;
263}
264
265#else
Bruno Randolf410e6122011-01-19 18:20:57 +0900266static bool ath5k_is_standard_channel(short chan, enum ieee80211_band band)
Bob Copeland42639fc2009-03-30 08:05:29 -0400267{
Bruno Randolf410e6122011-01-19 18:20:57 +0900268 if (band == IEEE80211_BAND_2GHZ && chan <= 14)
269 return true;
270
271 return /* UNII 1,2 */
272 (((chan & 3) == 0 && chan >= 36 && chan <= 64) ||
Bob Copeland42639fc2009-03-30 08:05:29 -0400273 /* midband */
274 ((chan & 3) == 0 && chan >= 100 && chan <= 140) ||
275 /* UNII-3 */
Bruno Randolf410e6122011-01-19 18:20:57 +0900276 ((chan & 3) == 1 && chan >= 149 && chan <= 165) ||
277 /* 802.11j 5.030-5.080 GHz (20MHz) */
278 (chan == 8 || chan == 12 || chan == 16) ||
279 /* 802.11j 4.9GHz (20MHz) */
280 (chan == 184 || chan == 188 || chan == 192 || chan == 196));
Bob Copeland42639fc2009-03-30 08:05:29 -0400281}
Luis R. Rodriguez2f8684c2012-07-06 15:21:51 -0700282#endif
Bob Copeland42639fc2009-03-30 08:05:29 -0400283
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200284static unsigned int
Bruno Randolf97d9c3a2011-01-19 18:20:52 +0900285ath5k_setup_channels(struct ath5k_hw *ah, struct ieee80211_channel *channels,
286 unsigned int mode, unsigned int max)
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200287{
Pavel Roskin32c25462011-07-23 09:29:09 -0400288 unsigned int count, size, freq, ch;
Bruno Randolf90c02d72011-01-19 18:20:36 +0900289 enum ieee80211_band band;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200290
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200291 switch (mode) {
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500292 case AR5K_MODE_11A:
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200293 /* 1..220, but 2GHz frequencies are filtered by check_channel */
Bruno Randolf97d9c3a2011-01-19 18:20:52 +0900294 size = 220;
Bruno Randolf90c02d72011-01-19 18:20:36 +0900295 band = IEEE80211_BAND_5GHZ;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200296 break;
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500297 case AR5K_MODE_11B:
298 case AR5K_MODE_11G:
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500299 size = 26;
Bruno Randolf90c02d72011-01-19 18:20:36 +0900300 band = IEEE80211_BAND_2GHZ;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200301 break;
302 default:
Pavel Roskine0d687b2011-07-14 20:21:55 -0400303 ATH5K_WARN(ah, "bad mode, not copying channels\n");
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200304 return 0;
305 }
306
Bruno Randolf2b1351a2011-01-21 12:19:52 +0900307 count = 0;
308 for (ch = 1; ch <= size && count < max; ch++) {
Bruno Randolf90c02d72011-01-19 18:20:36 +0900309 freq = ieee80211_channel_to_frequency(ch, band);
310
311 if (freq == 0) /* mapping failed - not a standard channel */
312 continue;
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500313
Pavel Roskin32c25462011-07-23 09:29:09 -0400314 /* Write channel info, needed for ath5k_channel_ok() */
315 channels[count].center_freq = freq;
316 channels[count].band = band;
317 channels[count].hw_value = mode;
318
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200319 /* Check if channel is supported by the chipset */
Pavel Roskin32c25462011-07-23 09:29:09 -0400320 if (!ath5k_channel_ok(ah, &channels[count]))
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200321 continue;
322
Luis R. Rodriguez2f8684c2012-07-06 15:21:51 -0700323 if (!ath5k_is_standard_channel(ch, band))
Bob Copeland42639fc2009-03-30 08:05:29 -0400324 continue;
325
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200326 count++;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200327 }
328
329 return count;
330}
331
Bruno Randolf63266a62008-07-30 17:12:58 +0200332static void
Pavel Roskine0d687b2011-07-14 20:21:55 -0400333ath5k_setup_rate_idx(struct ath5k_hw *ah, struct ieee80211_supported_band *b)
Bruno Randolf63266a62008-07-30 17:12:58 +0200334{
335 u8 i;
336
337 for (i = 0; i < AR5K_MAX_RATES; i++)
Pavel Roskine0d687b2011-07-14 20:21:55 -0400338 ah->rate_idx[b->band][i] = -1;
Bruno Randolf63266a62008-07-30 17:12:58 +0200339
340 for (i = 0; i < b->n_bitrates; i++) {
Pavel Roskine0d687b2011-07-14 20:21:55 -0400341 ah->rate_idx[b->band][b->bitrates[i].hw_value] = i;
Bruno Randolf63266a62008-07-30 17:12:58 +0200342 if (b->bitrates[i].hw_value_short)
Pavel Roskine0d687b2011-07-14 20:21:55 -0400343 ah->rate_idx[b->band][b->bitrates[i].hw_value_short] = i;
Bruno Randolf63266a62008-07-30 17:12:58 +0200344 }
345}
346
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200347static int
Bruno Randolf63266a62008-07-30 17:12:58 +0200348ath5k_setup_bands(struct ieee80211_hw *hw)
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200349{
Pavel Roskine0d687b2011-07-14 20:21:55 -0400350 struct ath5k_hw *ah = hw->priv;
Bruno Randolf63266a62008-07-30 17:12:58 +0200351 struct ieee80211_supported_band *sband;
352 int max_c, count_c = 0;
353 int i;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200354
Pavel Roskine0d687b2011-07-14 20:21:55 -0400355 BUILD_BUG_ON(ARRAY_SIZE(ah->sbands) < IEEE80211_NUM_BANDS);
356 max_c = ARRAY_SIZE(ah->channels);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200357
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500358 /* 2GHz band */
Pavel Roskine0d687b2011-07-14 20:21:55 -0400359 sband = &ah->sbands[IEEE80211_BAND_2GHZ];
Bruno Randolf63266a62008-07-30 17:12:58 +0200360 sband->band = IEEE80211_BAND_2GHZ;
Pavel Roskine0d687b2011-07-14 20:21:55 -0400361 sband->bitrates = &ah->rates[IEEE80211_BAND_2GHZ][0];
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200362
Pavel Roskine0d687b2011-07-14 20:21:55 -0400363 if (test_bit(AR5K_MODE_11G, ah->ah_capabilities.cap_mode)) {
Bruno Randolf63266a62008-07-30 17:12:58 +0200364 /* G mode */
365 memcpy(sband->bitrates, &ath5k_rates[0],
366 sizeof(struct ieee80211_rate) * 12);
367 sband->n_bitrates = 12;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200368
Pavel Roskine0d687b2011-07-14 20:21:55 -0400369 sband->channels = ah->channels;
Bruno Randolf08105692011-01-19 18:20:47 +0900370 sband->n_channels = ath5k_setup_channels(ah, sband->channels,
Bruno Randolf63266a62008-07-30 17:12:58 +0200371 AR5K_MODE_11G, max_c);
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500372
373 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = sband;
Bruno Randolf63266a62008-07-30 17:12:58 +0200374 count_c = sband->n_channels;
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500375 max_c -= count_c;
Pavel Roskine0d687b2011-07-14 20:21:55 -0400376 } else if (test_bit(AR5K_MODE_11B, ah->ah_capabilities.cap_mode)) {
Bruno Randolf63266a62008-07-30 17:12:58 +0200377 /* B mode */
378 memcpy(sband->bitrates, &ath5k_rates[0],
379 sizeof(struct ieee80211_rate) * 4);
380 sband->n_bitrates = 4;
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500381
Bruno Randolf63266a62008-07-30 17:12:58 +0200382 /* 5211 only supports B rates and uses 4bit rate codes
383 * (e.g normally we have 0x1B for 1M, but on 5211 we have 0x0B)
384 * fix them up here:
385 */
386 if (ah->ah_version == AR5K_AR5211) {
387 for (i = 0; i < 4; i++) {
388 sband->bitrates[i].hw_value =
389 sband->bitrates[i].hw_value & 0xF;
390 sband->bitrates[i].hw_value_short =
391 sband->bitrates[i].hw_value_short & 0xF;
392 }
393 }
394
Pavel Roskine0d687b2011-07-14 20:21:55 -0400395 sband->channels = ah->channels;
Bruno Randolf08105692011-01-19 18:20:47 +0900396 sband->n_channels = ath5k_setup_channels(ah, sband->channels,
Bruno Randolf63266a62008-07-30 17:12:58 +0200397 AR5K_MODE_11B, max_c);
398
399 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = sband;
400 count_c = sband->n_channels;
401 max_c -= count_c;
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500402 }
Pavel Roskine0d687b2011-07-14 20:21:55 -0400403 ath5k_setup_rate_idx(ah, sband);
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500404
Bruno Randolf63266a62008-07-30 17:12:58 +0200405 /* 5GHz band, A mode */
Pavel Roskine0d687b2011-07-14 20:21:55 -0400406 if (test_bit(AR5K_MODE_11A, ah->ah_capabilities.cap_mode)) {
407 sband = &ah->sbands[IEEE80211_BAND_5GHZ];
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500408 sband->band = IEEE80211_BAND_5GHZ;
Pavel Roskine0d687b2011-07-14 20:21:55 -0400409 sband->bitrates = &ah->rates[IEEE80211_BAND_5GHZ][0];
Bruno Randolf63266a62008-07-30 17:12:58 +0200410
411 memcpy(sband->bitrates, &ath5k_rates[4],
412 sizeof(struct ieee80211_rate) * 8);
413 sband->n_bitrates = 8;
414
Pavel Roskine0d687b2011-07-14 20:21:55 -0400415 sband->channels = &ah->channels[count_c];
Bruno Randolf08105692011-01-19 18:20:47 +0900416 sband->n_channels = ath5k_setup_channels(ah, sband->channels,
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500417 AR5K_MODE_11A, max_c);
418
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500419 hw->wiphy->bands[IEEE80211_BAND_5GHZ] = sband;
420 }
Pavel Roskine0d687b2011-07-14 20:21:55 -0400421 ath5k_setup_rate_idx(ah, sband);
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500422
Pavel Roskine0d687b2011-07-14 20:21:55 -0400423 ath5k_debug_dump_bands(ah);
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500424
425 return 0;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200426}
427
428/*
Joerg Alberte30eb4a2009-08-05 01:52:07 +0200429 * Set/change channels. We always reset the chip.
430 * To accomplish this we must first cleanup any pending DMA,
431 * then restart stuff after a la ath5k_init.
Bob Copelandbe009372009-01-22 08:44:16 -0500432 *
Pavel Roskine0d687b2011-07-14 20:21:55 -0400433 * Called with ah->lock.
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200434 */
Bruno Randolfcd2c5482010-12-22 19:20:32 +0900435int
Pavel Roskine0d687b2011-07-14 20:21:55 -0400436ath5k_chan_set(struct ath5k_hw *ah, struct ieee80211_channel *chan)
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200437{
Pavel Roskine0d687b2011-07-14 20:21:55 -0400438 ATH5K_DBG(ah, ATH5K_DEBUG_RESET,
Bruno Randolf8d67a032010-06-16 19:11:12 +0900439 "channel set, resetting (%u -> %u MHz)\n",
Pavel Roskine0d687b2011-07-14 20:21:55 -0400440 ah->curchan->center_freq, chan->center_freq);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200441
Joerg Alberte30eb4a2009-08-05 01:52:07 +0200442 /*
443 * To switch channels clear any pending DMA operations;
444 * wait long enough for the RX fifo to drain, reset the
445 * hardware at the new frequency, and then re-enable
446 * the relevant bits of the h/w.
447 */
Pavel Roskine0d687b2011-07-14 20:21:55 -0400448 return ath5k_reset(ah, chan, true);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200449}
450
Ben Greeare4b0b322011-03-03 14:39:05 -0800451void ath5k_vif_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
Ben Greearb1ae1ed2010-09-30 12:22:58 -0700452{
Ben Greeare4b0b322011-03-03 14:39:05 -0800453 struct ath5k_vif_iter_data *iter_data = data;
Ben Greearb1ae1ed2010-09-30 12:22:58 -0700454 int i;
Ben Greear62c58fb2010-10-08 12:01:15 -0700455 struct ath5k_vif *avf = (void *)vif->drv_priv;
Ben Greearb1ae1ed2010-09-30 12:22:58 -0700456
457 if (iter_data->hw_macaddr)
458 for (i = 0; i < ETH_ALEN; i++)
459 iter_data->mask[i] &=
460 ~(iter_data->hw_macaddr[i] ^ mac[i]);
461
462 if (!iter_data->found_active) {
463 iter_data->found_active = true;
464 memcpy(iter_data->active_mac, mac, ETH_ALEN);
465 }
466
467 if (iter_data->need_set_hw_addr && iter_data->hw_macaddr)
Joe Perches2e42e472012-05-09 17:17:46 +0000468 if (ether_addr_equal(iter_data->hw_macaddr, mac))
Ben Greearb1ae1ed2010-09-30 12:22:58 -0700469 iter_data->need_set_hw_addr = false;
470
471 if (!iter_data->any_assoc) {
Ben Greearb1ae1ed2010-09-30 12:22:58 -0700472 if (avf->assoc)
473 iter_data->any_assoc = true;
474 }
Ben Greear62c58fb2010-10-08 12:01:15 -0700475
476 /* Calculate combined mode - when APs are active, operate in AP mode.
477 * Otherwise use the mode of the new interface. This can currently
478 * only deal with combinations of APs and STAs. Only one ad-hoc
Ben Greear7afbb2f2010-11-10 11:43:51 -0800479 * interfaces is allowed.
Ben Greear62c58fb2010-10-08 12:01:15 -0700480 */
481 if (avf->opmode == NL80211_IFTYPE_AP)
482 iter_data->opmode = NL80211_IFTYPE_AP;
Ben Greeare4b0b322011-03-03 14:39:05 -0800483 else {
484 if (avf->opmode == NL80211_IFTYPE_STATION)
485 iter_data->n_stas++;
Ben Greear62c58fb2010-10-08 12:01:15 -0700486 if (iter_data->opmode == NL80211_IFTYPE_UNSPECIFIED)
487 iter_data->opmode = avf->opmode;
Ben Greeare4b0b322011-03-03 14:39:05 -0800488 }
Ben Greearb1ae1ed2010-09-30 12:22:58 -0700489}
490
Bruno Randolfcd2c5482010-12-22 19:20:32 +0900491void
Pavel Roskine0d687b2011-07-14 20:21:55 -0400492ath5k_update_bssid_mask_and_opmode(struct ath5k_hw *ah,
Bruno Randolfcd2c5482010-12-22 19:20:32 +0900493 struct ieee80211_vif *vif)
Ben Greearb1ae1ed2010-09-30 12:22:58 -0700494{
Pavel Roskine0d687b2011-07-14 20:21:55 -0400495 struct ath_common *common = ath5k_hw_common(ah);
Ben Greeare4b0b322011-03-03 14:39:05 -0800496 struct ath5k_vif_iter_data iter_data;
497 u32 rfilt;
Ben Greearb1ae1ed2010-09-30 12:22:58 -0700498
499 /*
500 * Use the hardware MAC address as reference, the hardware uses it
501 * together with the BSSID mask when matching addresses.
502 */
503 iter_data.hw_macaddr = common->macaddr;
504 memset(&iter_data.mask, 0xff, ETH_ALEN);
505 iter_data.found_active = false;
506 iter_data.need_set_hw_addr = true;
Ben Greear62c58fb2010-10-08 12:01:15 -0700507 iter_data.opmode = NL80211_IFTYPE_UNSPECIFIED;
Ben Greeare4b0b322011-03-03 14:39:05 -0800508 iter_data.n_stas = 0;
Ben Greearb1ae1ed2010-09-30 12:22:58 -0700509
510 if (vif)
Ben Greeare4b0b322011-03-03 14:39:05 -0800511 ath5k_vif_iter(&iter_data, vif->addr, vif);
Ben Greearb1ae1ed2010-09-30 12:22:58 -0700512
513 /* Get list of all active MAC addresses */
Johannes Berg8b2c9822012-11-06 20:23:30 +0100514 ieee80211_iterate_active_interfaces_atomic(
515 ah->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
516 ath5k_vif_iter, &iter_data);
Pavel Roskine0d687b2011-07-14 20:21:55 -0400517 memcpy(ah->bssidmask, iter_data.mask, ETH_ALEN);
Ben Greearb1ae1ed2010-09-30 12:22:58 -0700518
Pavel Roskine0d687b2011-07-14 20:21:55 -0400519 ah->opmode = iter_data.opmode;
520 if (ah->opmode == NL80211_IFTYPE_UNSPECIFIED)
Ben Greear62c58fb2010-10-08 12:01:15 -0700521 /* Nothing active, default to station mode */
Pavel Roskine0d687b2011-07-14 20:21:55 -0400522 ah->opmode = NL80211_IFTYPE_STATION;
Ben Greear62c58fb2010-10-08 12:01:15 -0700523
Pavel Roskine0d687b2011-07-14 20:21:55 -0400524 ath5k_hw_set_opmode(ah, ah->opmode);
525 ATH5K_DBG(ah, ATH5K_DEBUG_MODE, "mode setup opmode %d (%s)\n",
526 ah->opmode, ath_opmode_to_string(ah->opmode));
Ben Greear62c58fb2010-10-08 12:01:15 -0700527
Ben Greearb1ae1ed2010-09-30 12:22:58 -0700528 if (iter_data.need_set_hw_addr && iter_data.found_active)
Pavel Roskine0d687b2011-07-14 20:21:55 -0400529 ath5k_hw_set_lladdr(ah, iter_data.active_mac);
Ben Greearb1ae1ed2010-09-30 12:22:58 -0700530
Pavel Roskine0d687b2011-07-14 20:21:55 -0400531 if (ath5k_hw_hasbssidmask(ah))
532 ath5k_hw_set_bssid_mask(ah, ah->bssidmask);
Ben Greearb1ae1ed2010-09-30 12:22:58 -0700533
Ben Greeare4b0b322011-03-03 14:39:05 -0800534 /* Set up RX Filter */
535 if (iter_data.n_stas > 1) {
536 /* If you have multiple STA interfaces connected to
537 * different APs, ARPs are not received (most of the time?)
Pavel Roskin6a2a0e72011-07-09 00:17:51 -0400538 * Enabling PROMISC appears to fix that problem.
Ben Greeare4b0b322011-03-03 14:39:05 -0800539 */
Pavel Roskine0d687b2011-07-14 20:21:55 -0400540 ah->filter_flags |= AR5K_RX_FILTER_PROM;
Ben Greeare4b0b322011-03-03 14:39:05 -0800541 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200542
Pavel Roskine0d687b2011-07-14 20:21:55 -0400543 rfilt = ah->filter_flags;
544 ath5k_hw_set_rx_filter(ah, rfilt);
545 ATH5K_DBG(ah, ATH5K_DEBUG_MODE, "RX filter 0x%x\n", rfilt);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200546}
547
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500548static inline int
Pavel Roskine0d687b2011-07-14 20:21:55 -0400549ath5k_hw_to_driver_rix(struct ath5k_hw *ah, int hw_rix)
Bruno Randolf63266a62008-07-30 17:12:58 +0200550{
Bob Copelandb7266042009-03-02 21:55:18 -0500551 int rix;
552
553 /* return base rate on errors */
554 if (WARN(hw_rix < 0 || hw_rix >= AR5K_MAX_RATES,
555 "hw_rix out of bounds: %x\n", hw_rix))
556 return 0;
557
Pavel Roskine0d687b2011-07-14 20:21:55 -0400558 rix = ah->rate_idx[ah->curchan->band][hw_rix];
Bob Copelandb7266042009-03-02 21:55:18 -0500559 if (WARN(rix < 0, "invalid hw_rix: %x\n", hw_rix))
560 rix = 0;
561
562 return rix;
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500563}
564
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200565/***************\
566* Buffers setup *
567\***************/
568
Bob Copelandb6ea0352009-01-10 14:42:54 -0500569static
Pavel Roskine0d687b2011-07-14 20:21:55 -0400570struct sk_buff *ath5k_rx_skb_alloc(struct ath5k_hw *ah, dma_addr_t *skb_addr)
Bob Copelandb6ea0352009-01-10 14:42:54 -0500571{
Pavel Roskine0d687b2011-07-14 20:21:55 -0400572 struct ath_common *common = ath5k_hw_common(ah);
Bob Copelandb6ea0352009-01-10 14:42:54 -0500573 struct sk_buff *skb;
Bob Copelandb6ea0352009-01-10 14:42:54 -0500574
575 /*
576 * Allocate buffer with headroom_needed space for the
577 * fake physical layer header at the start.
578 */
Luis R. Rodriguezdb719712009-09-10 11:20:57 -0700579 skb = ath_rxbuf_alloc(common,
Luis R. Rodriguezdd849782009-11-04 09:44:50 -0800580 common->rx_bufsize,
Luis R. Rodriguezaeb63cf2009-08-12 09:57:00 -0700581 GFP_ATOMIC);
Bob Copelandb6ea0352009-01-10 14:42:54 -0500582
583 if (!skb) {
Pavel Roskine0d687b2011-07-14 20:21:55 -0400584 ATH5K_ERR(ah, "can't alloc skbuff of size %u\n",
Luis R. Rodriguezdd849782009-11-04 09:44:50 -0800585 common->rx_bufsize);
Bob Copelandb6ea0352009-01-10 14:42:54 -0500586 return NULL;
587 }
Bob Copelandb6ea0352009-01-10 14:42:54 -0500588
Pavel Roskine0d687b2011-07-14 20:21:55 -0400589 *skb_addr = dma_map_single(ah->dev,
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -0800590 skb->data, common->rx_bufsize,
Felix Fietkauaeae4ac2010-12-02 10:26:51 +0100591 DMA_FROM_DEVICE);
592
Pavel Roskine0d687b2011-07-14 20:21:55 -0400593 if (unlikely(dma_mapping_error(ah->dev, *skb_addr))) {
594 ATH5K_ERR(ah, "%s: DMA mapping failed\n", __func__);
Bob Copelandb6ea0352009-01-10 14:42:54 -0500595 dev_kfree_skb(skb);
596 return NULL;
597 }
598 return skb;
599}
600
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200601static int
Pavel Roskine0d687b2011-07-14 20:21:55 -0400602ath5k_rxbuf_setup(struct ath5k_hw *ah, struct ath5k_buf *bf)
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200603{
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200604 struct sk_buff *skb = bf->skb;
605 struct ath5k_desc *ds;
Bruno Randolfb5eae9f2010-05-19 10:18:16 +0900606 int ret;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200607
Bob Copelandb6ea0352009-01-10 14:42:54 -0500608 if (!skb) {
Pavel Roskine0d687b2011-07-14 20:21:55 -0400609 skb = ath5k_rx_skb_alloc(ah, &bf->skbaddr);
Bob Copelandb6ea0352009-01-10 14:42:54 -0500610 if (!skb)
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200611 return -ENOMEM;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200612 bf->skb = skb;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200613 }
614
615 /*
616 * Setup descriptors. For receive we always terminate
617 * the descriptor list with a self-linked entry so we'll
618 * not get overrun under high load (as can happen with a
619 * 5212 when ANI processing enables PHY error frames).
620 *
Bruno Randolfbeade632010-06-16 19:11:25 +0900621 * To ensure the last descriptor is self-linked we create
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200622 * each descriptor as self-linked and add it to the end. As
623 * each additional descriptor is added the previous self-linked
Bruno Randolfbeade632010-06-16 19:11:25 +0900624 * entry is "fixed" naturally. This should be safe even
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200625 * if DMA is happening. When processing RX interrupts we
626 * never remove/process the last, self-linked, entry on the
Bruno Randolfbeade632010-06-16 19:11:25 +0900627 * descriptor list. This ensures the hardware always has
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200628 * someplace to write a new frame.
629 */
630 ds = bf->desc;
631 ds->ds_link = bf->daddr; /* link to self */
632 ds->ds_data = bf->skbaddr;
Bruno Randolfa6668192010-06-16 19:12:01 +0900633 ret = ath5k_hw_setup_rx_desc(ah, ds, ah->common.rx_bufsize, 0);
Bruno Randolf0452d4a2010-06-16 19:11:35 +0900634 if (ret) {
Pavel Roskine0d687b2011-07-14 20:21:55 -0400635 ATH5K_ERR(ah, "%s: could not setup RX desc\n", __func__);
Bruno Randolfb5eae9f2010-05-19 10:18:16 +0900636 return ret;
Bruno Randolf0452d4a2010-06-16 19:11:35 +0900637 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200638
Pavel Roskine0d687b2011-07-14 20:21:55 -0400639 if (ah->rxlink != NULL)
640 *ah->rxlink = bf->daddr;
641 ah->rxlink = &ds->ds_link;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200642 return 0;
643}
644
Bob Copeland2ac29272010-02-09 13:06:54 -0500645static enum ath5k_pkt_type get_hw_packet_type(struct sk_buff *skb)
646{
647 struct ieee80211_hdr *hdr;
648 enum ath5k_pkt_type htype;
649 __le16 fc;
650
651 hdr = (struct ieee80211_hdr *)skb->data;
652 fc = hdr->frame_control;
653
654 if (ieee80211_is_beacon(fc))
655 htype = AR5K_PKT_TYPE_BEACON;
656 else if (ieee80211_is_probe_resp(fc))
657 htype = AR5K_PKT_TYPE_PROBE_RESP;
658 else if (ieee80211_is_atim(fc))
659 htype = AR5K_PKT_TYPE_ATIM;
660 else if (ieee80211_is_pspoll(fc))
661 htype = AR5K_PKT_TYPE_PSPOLL;
662 else
663 htype = AR5K_PKT_TYPE_NORMAL;
664
665 return htype;
666}
667
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200668static int
Pavel Roskine0d687b2011-07-14 20:21:55 -0400669ath5k_txbuf_setup(struct ath5k_hw *ah, struct ath5k_buf *bf,
Benoit Papillault8127fbd2010-02-27 23:05:26 +0100670 struct ath5k_txq *txq, int padsize)
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200671{
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200672 struct ath5k_desc *ds = bf->desc;
673 struct sk_buff *skb = bf->skb;
Johannes Berga888d522008-05-26 16:43:39 +0200674 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200675 unsigned int pktlen, flags, keyidx = AR5K_TXKEYIX_INVALID;
Felix Fietkau2f7fe872008-10-05 18:05:48 +0200676 struct ieee80211_rate *rate;
677 unsigned int mrr_rate[3], mrr_tries[3];
678 int i, ret;
Bob Copeland8902ff42009-01-22 08:44:20 -0500679 u16 hw_rate;
Bob Copeland07c1e852009-01-22 08:44:21 -0500680 u16 cts_rate = 0;
681 u16 duration = 0;
Bob Copeland8902ff42009-01-22 08:44:20 -0500682 u8 rc_flags;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200683
684 flags = AR5K_TXDESC_INTREQ | AR5K_TXDESC_CLRDMASK;
Johannes Berge039fa42008-05-15 12:55:29 +0200685
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200686 /* XXX endianness */
Pavel Roskine0d687b2011-07-14 20:21:55 -0400687 bf->skbaddr = dma_map_single(ah->dev, skb->data, skb->len,
Felix Fietkauaeae4ac2010-12-02 10:26:51 +0100688 DMA_TO_DEVICE);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200689
Pavel Roskine0d687b2011-07-14 20:21:55 -0400690 rate = ieee80211_get_tx_rate(ah->hw, info);
John W. Linvilled8e1ba72010-08-24 15:27:34 -0400691 if (!rate) {
692 ret = -EINVAL;
693 goto err_unmap;
694 }
Bob Copeland8902ff42009-01-22 08:44:20 -0500695
Johannes Berge039fa42008-05-15 12:55:29 +0200696 if (info->flags & IEEE80211_TX_CTL_NO_ACK)
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200697 flags |= AR5K_TXDESC_NOACK;
698
Bob Copeland8902ff42009-01-22 08:44:20 -0500699 rc_flags = info->control.rates[0].flags;
700 hw_rate = (rc_flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE) ?
701 rate->hw_value_short : rate->hw_value;
702
Bruno Randolf281c56d2008-02-05 18:44:55 +0900703 pktlen = skb->len;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200704
Nick Kossifidis8f655dd2009-03-15 22:20:35 +0200705 /* FIXME: If we are in g mode and rate is a CCK rate
706 * subtract ah->ah_txpower.txp_cck_ofdm_pwr_delta
707 * from tx power (value is in dB units already) */
Bob Copeland362695e2009-02-15 12:06:12 -0500708 if (info->control.hw_key) {
709 keyidx = info->control.hw_key->hw_key_idx;
710 pktlen += info->control.hw_key->icv_len;
711 }
Bob Copeland07c1e852009-01-22 08:44:21 -0500712 if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
713 flags |= AR5K_TXDESC_RTSENA;
Pavel Roskine0d687b2011-07-14 20:21:55 -0400714 cts_rate = ieee80211_get_rts_cts_rate(ah->hw, info)->hw_value;
715 duration = le16_to_cpu(ieee80211_rts_duration(ah->hw,
Ben Greearb1ae1ed2010-09-30 12:22:58 -0700716 info->control.vif, pktlen, info));
Bob Copeland07c1e852009-01-22 08:44:21 -0500717 }
718 if (rc_flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
719 flags |= AR5K_TXDESC_CTSENA;
Pavel Roskine0d687b2011-07-14 20:21:55 -0400720 cts_rate = ieee80211_get_rts_cts_rate(ah->hw, info)->hw_value;
721 duration = le16_to_cpu(ieee80211_ctstoself_duration(ah->hw,
Ben Greearb1ae1ed2010-09-30 12:22:58 -0700722 info->control.vif, pktlen, info));
Bob Copeland07c1e852009-01-22 08:44:21 -0500723 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200724 ret = ah->ah_setup_tx_desc(ah, ds, pktlen,
Benoit Papillault8127fbd2010-02-27 23:05:26 +0100725 ieee80211_get_hdrlen_from_skb(skb), padsize,
Bob Copeland2ac29272010-02-09 13:06:54 -0500726 get_hw_packet_type(skb),
Nick Kossifidis987af542012-08-05 22:35:36 +0300727 (ah->ah_txpower.txp_requested * 2),
Bob Copeland8902ff42009-01-22 08:44:20 -0500728 hw_rate,
Nick Kossifidis2bed03e2009-04-30 15:55:49 -0400729 info->control.rates[0].count, keyidx, ah->ah_tx_ant, flags,
Bob Copeland07c1e852009-01-22 08:44:21 -0500730 cts_rate, duration);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200731 if (ret)
732 goto err_unmap;
733
Nick Kossifidis86f62d92011-11-25 20:40:28 +0200734 /* Set up MRR descriptor */
735 if (ah->ah_capabilities.cap_has_mrr_support) {
736 memset(mrr_rate, 0, sizeof(mrr_rate));
737 memset(mrr_tries, 0, sizeof(mrr_tries));
738 for (i = 0; i < 3; i++) {
739 rate = ieee80211_get_alt_retry_rate(ah->hw, info, i);
740 if (!rate)
741 break;
Felix Fietkau2f7fe872008-10-05 18:05:48 +0200742
Nick Kossifidis86f62d92011-11-25 20:40:28 +0200743 mrr_rate[i] = rate->hw_value;
744 mrr_tries[i] = info->control.rates[i + 1].count;
745 }
746
747 ath5k_hw_setup_mrr_tx_desc(ah, ds,
748 mrr_rate[0], mrr_tries[0],
749 mrr_rate[1], mrr_tries[1],
750 mrr_rate[2], mrr_tries[2]);
Felix Fietkau2f7fe872008-10-05 18:05:48 +0200751 }
752
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200753 ds->ds_link = 0;
754 ds->ds_data = bf->skbaddr;
755
756 spin_lock_bh(&txq->lock);
757 list_add_tail(&bf->list, &txq->q);
Bruno Randolf925e0b02010-09-17 11:36:35 +0900758 txq->txq_len++;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200759 if (txq->link == NULL) /* is this first packet? */
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300760 ath5k_hw_set_txdp(ah, txq->qnum, bf->daddr);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200761 else /* no, so only link it */
762 *txq->link = bf->daddr;
763
764 txq->link = &ds->ds_link;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300765 ath5k_hw_start_tx_dma(ah, txq->qnum);
Jiri Slaby274c7c32008-07-15 17:44:20 +0200766 mmiowb();
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200767 spin_unlock_bh(&txq->lock);
768
769 return 0;
770err_unmap:
Pavel Roskine0d687b2011-07-14 20:21:55 -0400771 dma_unmap_single(ah->dev, bf->skbaddr, skb->len, DMA_TO_DEVICE);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200772 return ret;
773}
774
775/*******************\
776* Descriptors setup *
777\*******************/
778
779static int
Pavel Roskine0d687b2011-07-14 20:21:55 -0400780ath5k_desc_alloc(struct ath5k_hw *ah)
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200781{
782 struct ath5k_desc *ds;
783 struct ath5k_buf *bf;
784 dma_addr_t da;
785 unsigned int i;
786 int ret;
787
788 /* allocate descriptors */
Pavel Roskine0d687b2011-07-14 20:21:55 -0400789 ah->desc_len = sizeof(struct ath5k_desc) *
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200790 (ATH_TXBUF + ATH_RXBUF + ATH_BCBUF + 1);
Felix Fietkauaeae4ac2010-12-02 10:26:51 +0100791
Pavel Roskine0d687b2011-07-14 20:21:55 -0400792 ah->desc = dma_alloc_coherent(ah->dev, ah->desc_len,
793 &ah->desc_daddr, GFP_KERNEL);
794 if (ah->desc == NULL) {
795 ATH5K_ERR(ah, "can't allocate descriptors\n");
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200796 ret = -ENOMEM;
797 goto err;
798 }
Pavel Roskine0d687b2011-07-14 20:21:55 -0400799 ds = ah->desc;
800 da = ah->desc_daddr;
801 ATH5K_DBG(ah, ATH5K_DEBUG_ANY, "DMA map: %p (%zu) -> %llx\n",
802 ds, ah->desc_len, (unsigned long long)ah->desc_daddr);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200803
804 bf = kcalloc(1 + ATH_TXBUF + ATH_RXBUF + ATH_BCBUF,
805 sizeof(struct ath5k_buf), GFP_KERNEL);
806 if (bf == NULL) {
Pavel Roskine0d687b2011-07-14 20:21:55 -0400807 ATH5K_ERR(ah, "can't allocate bufptr\n");
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200808 ret = -ENOMEM;
809 goto err_free;
810 }
Pavel Roskine0d687b2011-07-14 20:21:55 -0400811 ah->bufptr = bf;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200812
Pavel Roskine0d687b2011-07-14 20:21:55 -0400813 INIT_LIST_HEAD(&ah->rxbuf);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200814 for (i = 0; i < ATH_RXBUF; i++, bf++, ds++, da += sizeof(*ds)) {
815 bf->desc = ds;
816 bf->daddr = da;
Pavel Roskine0d687b2011-07-14 20:21:55 -0400817 list_add_tail(&bf->list, &ah->rxbuf);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200818 }
819
Pavel Roskine0d687b2011-07-14 20:21:55 -0400820 INIT_LIST_HEAD(&ah->txbuf);
821 ah->txbuf_len = ATH_TXBUF;
Pavel Roskine4bbf2f2011-07-07 18:14:13 -0400822 for (i = 0; i < ATH_TXBUF; i++, bf++, ds++, da += sizeof(*ds)) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200823 bf->desc = ds;
824 bf->daddr = da;
Pavel Roskine0d687b2011-07-14 20:21:55 -0400825 list_add_tail(&bf->list, &ah->txbuf);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200826 }
827
Ben Greearb1ae1ed2010-09-30 12:22:58 -0700828 /* beacon buffers */
Pavel Roskine0d687b2011-07-14 20:21:55 -0400829 INIT_LIST_HEAD(&ah->bcbuf);
Ben Greearb1ae1ed2010-09-30 12:22:58 -0700830 for (i = 0; i < ATH_BCBUF; i++, bf++, ds++, da += sizeof(*ds)) {
831 bf->desc = ds;
832 bf->daddr = da;
Pavel Roskine0d687b2011-07-14 20:21:55 -0400833 list_add_tail(&bf->list, &ah->bcbuf);
Ben Greearb1ae1ed2010-09-30 12:22:58 -0700834 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200835
836 return 0;
837err_free:
Pavel Roskine0d687b2011-07-14 20:21:55 -0400838 dma_free_coherent(ah->dev, ah->desc_len, ah->desc, ah->desc_daddr);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200839err:
Pavel Roskine0d687b2011-07-14 20:21:55 -0400840 ah->desc = NULL;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200841 return ret;
842}
843
Bruno Randolfcd2c5482010-12-22 19:20:32 +0900844void
Pavel Roskine0d687b2011-07-14 20:21:55 -0400845ath5k_txbuf_free_skb(struct ath5k_hw *ah, struct ath5k_buf *bf)
Bruno Randolfcd2c5482010-12-22 19:20:32 +0900846{
847 BUG_ON(!bf);
848 if (!bf->skb)
849 return;
Pavel Roskine0d687b2011-07-14 20:21:55 -0400850 dma_unmap_single(ah->dev, bf->skbaddr, bf->skb->len,
Bruno Randolfcd2c5482010-12-22 19:20:32 +0900851 DMA_TO_DEVICE);
852 dev_kfree_skb_any(bf->skb);
853 bf->skb = NULL;
854 bf->skbaddr = 0;
855 bf->desc->ds_data = 0;
856}
857
858void
Pavel Roskine0d687b2011-07-14 20:21:55 -0400859ath5k_rxbuf_free_skb(struct ath5k_hw *ah, struct ath5k_buf *bf)
Bruno Randolfcd2c5482010-12-22 19:20:32 +0900860{
Bruno Randolfcd2c5482010-12-22 19:20:32 +0900861 struct ath_common *common = ath5k_hw_common(ah);
862
863 BUG_ON(!bf);
864 if (!bf->skb)
865 return;
Pavel Roskine0d687b2011-07-14 20:21:55 -0400866 dma_unmap_single(ah->dev, bf->skbaddr, common->rx_bufsize,
Bruno Randolfcd2c5482010-12-22 19:20:32 +0900867 DMA_FROM_DEVICE);
868 dev_kfree_skb_any(bf->skb);
869 bf->skb = NULL;
870 bf->skbaddr = 0;
871 bf->desc->ds_data = 0;
872}
873
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200874static void
Pavel Roskine0d687b2011-07-14 20:21:55 -0400875ath5k_desc_free(struct ath5k_hw *ah)
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200876{
877 struct ath5k_buf *bf;
878
Pavel Roskine0d687b2011-07-14 20:21:55 -0400879 list_for_each_entry(bf, &ah->txbuf, list)
880 ath5k_txbuf_free_skb(ah, bf);
881 list_for_each_entry(bf, &ah->rxbuf, list)
882 ath5k_rxbuf_free_skb(ah, bf);
883 list_for_each_entry(bf, &ah->bcbuf, list)
884 ath5k_txbuf_free_skb(ah, bf);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200885
886 /* Free memory associated with all descriptors */
Pavel Roskine0d687b2011-07-14 20:21:55 -0400887 dma_free_coherent(ah->dev, ah->desc_len, ah->desc, ah->desc_daddr);
888 ah->desc = NULL;
889 ah->desc_daddr = 0;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200890
Pavel Roskine0d687b2011-07-14 20:21:55 -0400891 kfree(ah->bufptr);
892 ah->bufptr = NULL;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200893}
894
895
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200896/**************\
897* Queues setup *
898\**************/
899
900static struct ath5k_txq *
Pavel Roskine0d687b2011-07-14 20:21:55 -0400901ath5k_txq_setup(struct ath5k_hw *ah,
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200902 int qtype, int subtype)
903{
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200904 struct ath5k_txq *txq;
905 struct ath5k_txq_info qi = {
906 .tqi_subtype = subtype,
Bruno Randolfde8af452010-09-17 11:37:12 +0900907 /* XXX: default values not correct for B and XR channels,
908 * but who cares? */
909 .tqi_aifs = AR5K_TUNE_AIFS,
910 .tqi_cw_min = AR5K_TUNE_CWMIN,
911 .tqi_cw_max = AR5K_TUNE_CWMAX
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200912 };
913 int qnum;
914
915 /*
916 * Enable interrupts only for EOL and DESC conditions.
917 * We mark tx descriptors to receive a DESC interrupt
Bob Copelanda180a132010-08-15 13:03:12 -0400918 * when a tx queue gets deep; otherwise we wait for the
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200919 * EOL to reap descriptors. Note that this is done to
920 * reduce interrupt load and this only defers reaping
921 * descriptors, never transmitting frames. Aside from
922 * reducing interrupts this also permits more concurrency.
923 * The only potential downside is if the tx queue backs
924 * up in which case the top half of the kernel may backup
925 * due to a lack of tx descriptors.
926 */
927 qi.tqi_flags = AR5K_TXQ_FLAG_TXEOLINT_ENABLE |
928 AR5K_TXQ_FLAG_TXDESCINT_ENABLE;
929 qnum = ath5k_hw_setup_tx_queue(ah, qtype, &qi);
930 if (qnum < 0) {
931 /*
932 * NB: don't print a message, this happens
933 * normally on parts with too few tx queues
934 */
935 return ERR_PTR(qnum);
936 }
Pavel Roskine0d687b2011-07-14 20:21:55 -0400937 txq = &ah->txqs[qnum];
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200938 if (!txq->setup) {
939 txq->qnum = qnum;
940 txq->link = NULL;
941 INIT_LIST_HEAD(&txq->q);
942 spin_lock_init(&txq->lock);
943 txq->setup = true;
Bruno Randolf925e0b02010-09-17 11:36:35 +0900944 txq->txq_len = 0;
John W. Linville81266ba2011-03-07 16:32:59 -0500945 txq->txq_max = ATH5K_TXQ_LEN_MAX;
Bruno Randolf4edd7612010-09-17 11:36:56 +0900946 txq->txq_poll_mark = false;
Bruno Randolf923e5b32010-09-17 11:37:02 +0900947 txq->txq_stuck = 0;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200948 }
Pavel Roskine0d687b2011-07-14 20:21:55 -0400949 return &ah->txqs[qnum];
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200950}
951
952static int
953ath5k_beaconq_setup(struct ath5k_hw *ah)
954{
955 struct ath5k_txq_info qi = {
Bruno Randolfde8af452010-09-17 11:37:12 +0900956 /* XXX: default values not correct for B and XR channels,
957 * but who cares? */
958 .tqi_aifs = AR5K_TUNE_AIFS,
959 .tqi_cw_min = AR5K_TUNE_CWMIN,
960 .tqi_cw_max = AR5K_TUNE_CWMAX,
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200961 /* NB: for dynamic turbo, don't enable any other interrupts */
962 .tqi_flags = AR5K_TXQ_FLAG_TXDESCINT_ENABLE
963 };
964
965 return ath5k_hw_setup_tx_queue(ah, AR5K_TX_QUEUE_BEACON, &qi);
966}
967
968static int
Pavel Roskine0d687b2011-07-14 20:21:55 -0400969ath5k_beaconq_config(struct ath5k_hw *ah)
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200970{
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200971 struct ath5k_txq_info qi;
972 int ret;
973
Pavel Roskine0d687b2011-07-14 20:21:55 -0400974 ret = ath5k_hw_get_tx_queueprops(ah, ah->bhalq, &qi);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200975 if (ret)
Bob Copelanda951ae22010-01-20 23:51:04 -0500976 goto err;
977
Pavel Roskine0d687b2011-07-14 20:21:55 -0400978 if (ah->opmode == NL80211_IFTYPE_AP ||
979 ah->opmode == NL80211_IFTYPE_MESH_POINT) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200980 /*
981 * Always burst out beacon and CAB traffic
982 * (aifs = cwmin = cwmax = 0)
983 */
984 qi.tqi_aifs = 0;
985 qi.tqi_cw_min = 0;
986 qi.tqi_cw_max = 0;
Pavel Roskine0d687b2011-07-14 20:21:55 -0400987 } else if (ah->opmode == NL80211_IFTYPE_ADHOC) {
Bruno Randolf6d91e1d2008-01-19 18:18:41 +0900988 /*
989 * Adhoc mode; backoff between 0 and (2 * cw_min).
990 */
991 qi.tqi_aifs = 0;
992 qi.tqi_cw_min = 0;
Bruno Randolfde8af452010-09-17 11:37:12 +0900993 qi.tqi_cw_max = 2 * AR5K_TUNE_CWMIN;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200994 }
995
Pavel Roskine0d687b2011-07-14 20:21:55 -0400996 ATH5K_DBG(ah, ATH5K_DEBUG_BEACON,
Bruno Randolf6d91e1d2008-01-19 18:18:41 +0900997 "beacon queueprops tqi_aifs:%d tqi_cw_min:%d tqi_cw_max:%d\n",
998 qi.tqi_aifs, qi.tqi_cw_min, qi.tqi_cw_max);
999
Pavel Roskine0d687b2011-07-14 20:21:55 -04001000 ret = ath5k_hw_set_tx_queueprops(ah, ah->bhalq, &qi);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001001 if (ret) {
Pavel Roskine0d687b2011-07-14 20:21:55 -04001002 ATH5K_ERR(ah, "%s: unable to update parameters for beacon "
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001003 "hardware queue!\n", __func__);
Bob Copelanda951ae22010-01-20 23:51:04 -05001004 goto err;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001005 }
Pavel Roskine0d687b2011-07-14 20:21:55 -04001006 ret = ath5k_hw_reset_tx_queue(ah, ah->bhalq); /* push to h/w */
Bob Copelanda951ae22010-01-20 23:51:04 -05001007 if (ret)
1008 goto err;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001009
Bob Copelanda951ae22010-01-20 23:51:04 -05001010 /* reconfigure cabq with ready time to 80% of beacon_interval */
1011 ret = ath5k_hw_get_tx_queueprops(ah, AR5K_TX_QUEUE_ID_CAB, &qi);
1012 if (ret)
1013 goto err;
1014
Pavel Roskine0d687b2011-07-14 20:21:55 -04001015 qi.tqi_ready_time = (ah->bintval * 80) / 100;
Bob Copelanda951ae22010-01-20 23:51:04 -05001016 ret = ath5k_hw_set_tx_queueprops(ah, AR5K_TX_QUEUE_ID_CAB, &qi);
1017 if (ret)
1018 goto err;
1019
1020 ret = ath5k_hw_reset_tx_queue(ah, AR5K_TX_QUEUE_ID_CAB);
1021err:
1022 return ret;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001023}
1024
Nick Kossifidis80dac9e2010-11-23 20:45:38 +02001025/**
1026 * ath5k_drain_tx_buffs - Empty tx buffers
1027 *
Pavel Roskine0d687b2011-07-14 20:21:55 -04001028 * @ah The &struct ath5k_hw
Nick Kossifidis80dac9e2010-11-23 20:45:38 +02001029 *
1030 * Empty tx buffers from all queues in preparation
1031 * of a reset or during shutdown.
1032 *
1033 * NB: this assumes output has been stopped and
1034 * we do not need to block ath5k_tx_tasklet
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001035 */
1036static void
Pavel Roskine0d687b2011-07-14 20:21:55 -04001037ath5k_drain_tx_buffs(struct ath5k_hw *ah)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001038{
Nick Kossifidis80dac9e2010-11-23 20:45:38 +02001039 struct ath5k_txq *txq;
1040 struct ath5k_buf *bf, *bf0;
1041 int i;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001042
Pavel Roskine0d687b2011-07-14 20:21:55 -04001043 for (i = 0; i < ARRAY_SIZE(ah->txqs); i++) {
1044 if (ah->txqs[i].setup) {
1045 txq = &ah->txqs[i];
Nick Kossifidis80dac9e2010-11-23 20:45:38 +02001046 spin_lock_bh(&txq->lock);
1047 list_for_each_entry_safe(bf, bf0, &txq->q, list) {
Pavel Roskine0d687b2011-07-14 20:21:55 -04001048 ath5k_debug_printtxbuf(ah, bf);
Nick Kossifidis80dac9e2010-11-23 20:45:38 +02001049
Pavel Roskine0d687b2011-07-14 20:21:55 -04001050 ath5k_txbuf_free_skb(ah, bf);
Nick Kossifidis80dac9e2010-11-23 20:45:38 +02001051
Bob Copeland66179422012-06-15 16:03:29 -04001052 spin_lock(&ah->txbuflock);
Pavel Roskine0d687b2011-07-14 20:21:55 -04001053 list_move_tail(&bf->list, &ah->txbuf);
1054 ah->txbuf_len++;
Nick Kossifidis80dac9e2010-11-23 20:45:38 +02001055 txq->txq_len--;
Bob Copeland66179422012-06-15 16:03:29 -04001056 spin_unlock(&ah->txbuflock);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001057 }
Nick Kossifidis80dac9e2010-11-23 20:45:38 +02001058 txq->link = NULL;
1059 txq->txq_poll_mark = false;
1060 spin_unlock_bh(&txq->lock);
1061 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001062 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001063}
1064
1065static void
Pavel Roskine0d687b2011-07-14 20:21:55 -04001066ath5k_txq_release(struct ath5k_hw *ah)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001067{
Pavel Roskine0d687b2011-07-14 20:21:55 -04001068 struct ath5k_txq *txq = ah->txqs;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001069 unsigned int i;
1070
Pavel Roskine0d687b2011-07-14 20:21:55 -04001071 for (i = 0; i < ARRAY_SIZE(ah->txqs); i++, txq++)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001072 if (txq->setup) {
Pavel Roskine0d687b2011-07-14 20:21:55 -04001073 ath5k_hw_release_tx_queue(ah, txq->qnum);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001074 txq->setup = false;
1075 }
1076}
1077
1078
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001079/*************\
1080* RX Handling *
1081\*************/
1082
1083/*
1084 * Enable the receive h/w following a reset.
1085 */
1086static int
Pavel Roskine0d687b2011-07-14 20:21:55 -04001087ath5k_rx_start(struct ath5k_hw *ah)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001088{
Luis R. Rodriguezdb719712009-09-10 11:20:57 -07001089 struct ath_common *common = ath5k_hw_common(ah);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001090 struct ath5k_buf *bf;
1091 int ret;
1092
Nick Kossifidisb6127982010-08-15 13:03:11 -04001093 common->rx_bufsize = roundup(IEEE80211_MAX_FRAME_LEN, common->cachelsz);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001094
Pavel Roskine0d687b2011-07-14 20:21:55 -04001095 ATH5K_DBG(ah, ATH5K_DEBUG_RESET, "cachelsz %u rx_bufsize %u\n",
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -08001096 common->cachelsz, common->rx_bufsize);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001097
Pavel Roskine0d687b2011-07-14 20:21:55 -04001098 spin_lock_bh(&ah->rxbuflock);
1099 ah->rxlink = NULL;
1100 list_for_each_entry(bf, &ah->rxbuf, list) {
1101 ret = ath5k_rxbuf_setup(ah, bf);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001102 if (ret != 0) {
Pavel Roskine0d687b2011-07-14 20:21:55 -04001103 spin_unlock_bh(&ah->rxbuflock);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001104 goto err;
1105 }
1106 }
Pavel Roskine0d687b2011-07-14 20:21:55 -04001107 bf = list_first_entry(&ah->rxbuf, struct ath5k_buf, list);
Bob Copeland26925042009-04-15 07:57:36 -04001108 ath5k_hw_set_rxdp(ah, bf->daddr);
Pavel Roskine0d687b2011-07-14 20:21:55 -04001109 spin_unlock_bh(&ah->rxbuflock);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001110
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001111 ath5k_hw_start_rx_dma(ah); /* enable recv descriptors */
Pavel Roskine0d687b2011-07-14 20:21:55 -04001112 ath5k_update_bssid_mask_and_opmode(ah, NULL); /* set filters, etc. */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001113 ath5k_hw_start_rx_pcu(ah); /* re-enable PCU/DMA engine */
1114
1115 return 0;
1116err:
1117 return ret;
1118}
1119
1120/*
Nick Kossifidis80dac9e2010-11-23 20:45:38 +02001121 * Disable the receive logic on PCU (DRU)
1122 * In preparation for a shutdown.
1123 *
1124 * Note: Doesn't stop rx DMA, ath5k_hw_dma_stop
1125 * does.
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001126 */
1127static void
Pavel Roskine0d687b2011-07-14 20:21:55 -04001128ath5k_rx_stop(struct ath5k_hw *ah)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001129{
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001130
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001131 ath5k_hw_set_rx_filter(ah, 0); /* clear recv filter */
Nick Kossifidis80dac9e2010-11-23 20:45:38 +02001132 ath5k_hw_stop_rx_pcu(ah); /* disable PCU */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001133
Pavel Roskine0d687b2011-07-14 20:21:55 -04001134 ath5k_debug_printrxbuffs(ah);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001135}
1136
1137static unsigned int
Pavel Roskine0d687b2011-07-14 20:21:55 -04001138ath5k_rx_decrypted(struct ath5k_hw *ah, struct sk_buff *skb,
Bruno Randolf8a89f062010-06-16 19:11:51 +09001139 struct ath5k_rx_status *rs)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001140{
Luis R. Rodriguezdc1e0012009-11-04 17:47:31 -08001141 struct ath_common *common = ath5k_hw_common(ah);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001142 struct ieee80211_hdr *hdr = (void *)skb->data;
Harvey Harrison798ee982008-07-15 18:44:02 -07001143 unsigned int keyix, hlen;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001144
Bruno Randolfb47f4072008-03-05 18:35:45 +09001145 if (!(rs->rs_status & AR5K_RXERR_DECRYPT) &&
1146 rs->rs_keyix != AR5K_RXKEYIX_INVALID)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001147 return RX_FLAG_DECRYPTED;
1148
1149 /* Apparently when a default key is used to decrypt the packet
1150 the hw does not set the index used to decrypt. In such cases
1151 get the index from the packet. */
Harvey Harrison798ee982008-07-15 18:44:02 -07001152 hlen = ieee80211_hdrlen(hdr->frame_control);
Harvey Harrison24b56e72008-06-14 23:33:38 -07001153 if (ieee80211_has_protected(hdr->frame_control) &&
1154 !(rs->rs_status & AR5K_RXERR_DECRYPT) &&
1155 skb->len >= hlen + 4) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001156 keyix = skb->data[hlen + 3] >> 6;
1157
Luis R. Rodriguezdc1e0012009-11-04 17:47:31 -08001158 if (test_bit(keyix, common->keymap))
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001159 return RX_FLAG_DECRYPTED;
1160 }
1161
1162 return 0;
1163}
1164
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001165
1166static void
Pavel Roskine0d687b2011-07-14 20:21:55 -04001167ath5k_check_ibss_tsf(struct ath5k_hw *ah, struct sk_buff *skb,
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001168 struct ieee80211_rx_status *rxs)
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001169{
Pavel Roskine0d687b2011-07-14 20:21:55 -04001170 struct ath_common *common = ath5k_hw_common(ah);
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001171 u64 tsf, bc_tstamp;
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001172 u32 hw_tu;
1173 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)skb->data;
1174
Harvey Harrison24b56e72008-06-14 23:33:38 -07001175 if (ieee80211_is_beacon(mgmt->frame_control) &&
Pavel Roskin38c07b42008-02-26 17:59:14 -05001176 le16_to_cpu(mgmt->u.beacon.capab_info) & WLAN_CAPABILITY_IBSS &&
Joe Perches2e42e472012-05-09 17:17:46 +00001177 ether_addr_equal(mgmt->bssid, common->curbssid)) {
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001178 /*
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001179 * Received an IBSS beacon with the same BSSID. Hardware *must*
1180 * have updated the local TSF. We have to work around various
1181 * hardware bugs, though...
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001182 */
Pavel Roskine0d687b2011-07-14 20:21:55 -04001183 tsf = ath5k_hw_get_tsf64(ah);
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001184 bc_tstamp = le64_to_cpu(mgmt->u.beacon.timestamp);
1185 hw_tu = TSF_TO_TU(tsf);
1186
Pavel Roskine0d687b2011-07-14 20:21:55 -04001187 ATH5K_DBG_UNLIMIT(ah, ATH5K_DEBUG_BEACON,
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001188 "beacon %llx mactime %llx (diff %lld) tsf now %llx\n",
John W. Linville06501d22008-04-01 17:38:47 -04001189 (unsigned long long)bc_tstamp,
1190 (unsigned long long)rxs->mactime,
1191 (unsigned long long)(rxs->mactime - bc_tstamp),
1192 (unsigned long long)tsf);
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001193
1194 /*
1195 * Sometimes the HW will give us a wrong tstamp in the rx
1196 * status, causing the timestamp extension to go wrong.
1197 * (This seems to happen especially with beacon frames bigger
1198 * than 78 byte (incl. FCS))
1199 * But we know that the receive timestamp must be later than the
1200 * timestamp of the beacon since HW must have synced to that.
1201 *
1202 * NOTE: here we assume mactime to be after the frame was
1203 * received, not like mac80211 which defines it at the start.
1204 */
1205 if (bc_tstamp > rxs->mactime) {
Pavel Roskine0d687b2011-07-14 20:21:55 -04001206 ATH5K_DBG_UNLIMIT(ah, ATH5K_DEBUG_BEACON,
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001207 "fixing mactime from %llx to %llx\n",
John W. Linville06501d22008-04-01 17:38:47 -04001208 (unsigned long long)rxs->mactime,
1209 (unsigned long long)tsf);
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001210 rxs->mactime = tsf;
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001211 }
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001212
1213 /*
1214 * Local TSF might have moved higher than our beacon timers,
1215 * in that case we have to update them to continue sending
1216 * beacons. This also takes care of synchronizing beacon sending
1217 * times with other stations.
1218 */
Pavel Roskine0d687b2011-07-14 20:21:55 -04001219 if (hw_tu >= ah->nexttbtt)
1220 ath5k_beacon_update_timers(ah, bc_tstamp);
Bruno Randolf7f896122010-09-27 12:22:21 +09001221
1222 /* Check if the beacon timers are still correct, because a TSF
1223 * update might have created a window between them - for a
1224 * longer description see the comment of this function: */
Pavel Roskine0d687b2011-07-14 20:21:55 -04001225 if (!ath5k_hw_check_beacon_timers(ah, ah->bintval)) {
1226 ath5k_beacon_update_timers(ah, bc_tstamp);
1227 ATH5K_DBG_UNLIMIT(ah, ATH5K_DEBUG_BEACON,
Bruno Randolf7f896122010-09-27 12:22:21 +09001228 "fixed beacon timers after beacon receive\n");
1229 }
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001230 }
1231}
1232
Bruno Randolfb4ea4492010-03-25 14:49:25 +09001233static void
Pavel Roskine0d687b2011-07-14 20:21:55 -04001234ath5k_update_beacon_rssi(struct ath5k_hw *ah, struct sk_buff *skb, int rssi)
Bruno Randolfb4ea4492010-03-25 14:49:25 +09001235{
1236 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)skb->data;
Bruno Randolfb4ea4492010-03-25 14:49:25 +09001237 struct ath_common *common = ath5k_hw_common(ah);
1238
1239 /* only beacons from our BSSID */
1240 if (!ieee80211_is_beacon(mgmt->frame_control) ||
Joe Perches2e42e472012-05-09 17:17:46 +00001241 !ether_addr_equal(mgmt->bssid, common->curbssid))
Bruno Randolfb4ea4492010-03-25 14:49:25 +09001242 return;
1243
Bruno Randolfeef39be2010-11-16 10:58:43 +09001244 ewma_add(&ah->ah_beacon_rssi_avg, rssi);
Bruno Randolfb4ea4492010-03-25 14:49:25 +09001245
1246 /* in IBSS mode we should keep RSSI statistics per neighbour */
1247 /* le16_to_cpu(mgmt->u.beacon.capab_info) & WLAN_CAPABILITY_IBSS */
1248}
1249
Benoit Papillault8127fbd2010-02-27 23:05:26 +01001250/*
Bob Copelanda180a132010-08-15 13:03:12 -04001251 * Compute padding position. skb must contain an IEEE 802.11 frame
Benoit Papillault8127fbd2010-02-27 23:05:26 +01001252 */
1253static int ath5k_common_padpos(struct sk_buff *skb)
1254{
Pavel Roskine4bbf2f2011-07-07 18:14:13 -04001255 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Benoit Papillault8127fbd2010-02-27 23:05:26 +01001256 __le16 frame_control = hdr->frame_control;
1257 int padpos = 24;
1258
Pavel Roskind2c7f772011-07-07 18:14:07 -04001259 if (ieee80211_has_a4(frame_control))
Benoit Papillault8127fbd2010-02-27 23:05:26 +01001260 padpos += ETH_ALEN;
Pavel Roskind2c7f772011-07-07 18:14:07 -04001261
1262 if (ieee80211_is_data_qos(frame_control))
Benoit Papillault8127fbd2010-02-27 23:05:26 +01001263 padpos += IEEE80211_QOS_CTL_LEN;
Benoit Papillault8127fbd2010-02-27 23:05:26 +01001264
1265 return padpos;
1266}
1267
1268/*
Bob Copelanda180a132010-08-15 13:03:12 -04001269 * This function expects an 802.11 frame and returns the number of
1270 * bytes added, or -1 if we don't have enough header room.
Benoit Papillault8127fbd2010-02-27 23:05:26 +01001271 */
Benoit Papillault8127fbd2010-02-27 23:05:26 +01001272static int ath5k_add_padding(struct sk_buff *skb)
1273{
1274 int padpos = ath5k_common_padpos(skb);
1275 int padsize = padpos & 3;
1276
Pavel Roskine4bbf2f2011-07-07 18:14:13 -04001277 if (padsize && skb->len > padpos) {
Benoit Papillault8127fbd2010-02-27 23:05:26 +01001278
1279 if (skb_headroom(skb) < padsize)
1280 return -1;
1281
1282 skb_push(skb, padsize);
Pavel Roskine4bbf2f2011-07-07 18:14:13 -04001283 memmove(skb->data, skb->data + padsize, padpos);
Benoit Papillault8127fbd2010-02-27 23:05:26 +01001284 return padsize;
1285 }
1286
1287 return 0;
1288}
1289
1290/*
Bob Copelanda180a132010-08-15 13:03:12 -04001291 * The MAC header is padded to have 32-bit boundary if the
1292 * packet payload is non-zero. The general calculation for
1293 * padsize would take into account odd header lengths:
1294 * padsize = 4 - (hdrlen & 3); however, since only
1295 * even-length headers are used, padding can only be 0 or 2
1296 * bytes and we can optimize this a bit. We must not try to
1297 * remove padding from short control frames that do not have a
1298 * payload.
1299 *
1300 * This function expects an 802.11 frame and returns the number of
1301 * bytes removed.
Benoit Papillault8127fbd2010-02-27 23:05:26 +01001302 */
Benoit Papillault8127fbd2010-02-27 23:05:26 +01001303static int ath5k_remove_padding(struct sk_buff *skb)
1304{
1305 int padpos = ath5k_common_padpos(skb);
1306 int padsize = padpos & 3;
1307
Pavel Roskine4bbf2f2011-07-07 18:14:13 -04001308 if (padsize && skb->len >= padpos + padsize) {
Benoit Papillault8127fbd2010-02-27 23:05:26 +01001309 memmove(skb->data + padsize, skb->data, padpos);
1310 skb_pull(skb, padsize);
1311 return padsize;
1312 }
1313
1314 return 0;
1315}
1316
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001317static void
Pavel Roskine0d687b2011-07-14 20:21:55 -04001318ath5k_receive_frame(struct ath5k_hw *ah, struct sk_buff *skb,
Bruno Randolf8a89f062010-06-16 19:11:51 +09001319 struct ath5k_rx_status *rs)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001320{
Bob Copeland1c5256b2009-08-24 23:00:32 -04001321 struct ieee80211_rx_status *rxs;
Bruno Randolf8a89f062010-06-16 19:11:51 +09001322
Bruno Randolf8a89f062010-06-16 19:11:51 +09001323 ath5k_remove_padding(skb);
1324
1325 rxs = IEEE80211_SKB_RXCB(skb);
1326
1327 rxs->flag = 0;
1328 if (unlikely(rs->rs_status & AR5K_RXERR_MIC))
1329 rxs->flag |= RX_FLAG_MMIC_ERROR;
1330
1331 /*
1332 * always extend the mac timestamp, since this information is
1333 * also needed for proper IBSS merging.
1334 *
1335 * XXX: it might be too late to do it here, since rs_tstamp is
1336 * 15bit only. that means TSF extension has to be done within
1337 * 32768usec (about 32ms). it might be necessary to move this to
1338 * the interrupt handler, like it is done in madwifi.
1339 *
1340 * Unfortunately we don't know when the hardware takes the rx
1341 * timestamp (beginning of phy frame, data frame, end of rx?).
1342 * The only thing we know is that it is hardware specific...
1343 * On AR5213 it seems the rx timestamp is at the end of the
Pavel Roskin6a2a0e72011-07-09 00:17:51 -04001344 * frame, but I'm not sure.
Bruno Randolf8a89f062010-06-16 19:11:51 +09001345 *
1346 * NOTE: mac80211 defines mactime at the beginning of the first
1347 * data symbol. Since we don't have any time references it's
1348 * impossible to comply to that. This affects IBSS merge only
1349 * right now, so it's not too bad...
1350 */
Pavel Roskine0d687b2011-07-14 20:21:55 -04001351 rxs->mactime = ath5k_extend_tsf(ah, rs->rs_tstamp);
Johannes Berg6ebacbb2011-02-23 15:06:08 +01001352 rxs->flag |= RX_FLAG_MACTIME_MPDU;
Bruno Randolf8a89f062010-06-16 19:11:51 +09001353
Pavel Roskine0d687b2011-07-14 20:21:55 -04001354 rxs->freq = ah->curchan->center_freq;
1355 rxs->band = ah->curchan->band;
Bruno Randolf8a89f062010-06-16 19:11:51 +09001356
Pavel Roskine0d687b2011-07-14 20:21:55 -04001357 rxs->signal = ah->ah_noise_floor + rs->rs_rssi;
Bruno Randolf8a89f062010-06-16 19:11:51 +09001358
1359 rxs->antenna = rs->rs_antenna;
1360
1361 if (rs->rs_antenna > 0 && rs->rs_antenna < 5)
Pavel Roskine0d687b2011-07-14 20:21:55 -04001362 ah->stats.antenna_rx[rs->rs_antenna]++;
Bruno Randolf8a89f062010-06-16 19:11:51 +09001363 else
Pavel Roskine0d687b2011-07-14 20:21:55 -04001364 ah->stats.antenna_rx[0]++; /* invalid */
Bruno Randolf8a89f062010-06-16 19:11:51 +09001365
Pavel Roskine0d687b2011-07-14 20:21:55 -04001366 rxs->rate_idx = ath5k_hw_to_driver_rix(ah, rs->rs_rate);
1367 rxs->flag |= ath5k_rx_decrypted(ah, skb, rs);
Bruno Randolf8a89f062010-06-16 19:11:51 +09001368
1369 if (rxs->rate_idx >= 0 && rs->rs_rate ==
Pavel Roskine0d687b2011-07-14 20:21:55 -04001370 ah->sbands[ah->curchan->band].bitrates[rxs->rate_idx].hw_value_short)
Bruno Randolf8a89f062010-06-16 19:11:51 +09001371 rxs->flag |= RX_FLAG_SHORTPRE;
1372
Pavel Roskine0d687b2011-07-14 20:21:55 -04001373 trace_ath5k_rx(ah, skb);
Bruno Randolf8a89f062010-06-16 19:11:51 +09001374
Pavel Roskine0d687b2011-07-14 20:21:55 -04001375 ath5k_update_beacon_rssi(ah, skb, rs->rs_rssi);
Bruno Randolf8a89f062010-06-16 19:11:51 +09001376
1377 /* check beacons in IBSS mode */
Pavel Roskine0d687b2011-07-14 20:21:55 -04001378 if (ah->opmode == NL80211_IFTYPE_ADHOC)
1379 ath5k_check_ibss_tsf(ah, skb, rxs);
Bruno Randolf8a89f062010-06-16 19:11:51 +09001380
Pavel Roskine0d687b2011-07-14 20:21:55 -04001381 ieee80211_rx(ah->hw, skb);
Bruno Randolf8a89f062010-06-16 19:11:51 +09001382}
1383
Bruno Randolf02a78b42010-06-16 19:11:56 +09001384/** ath5k_frame_receive_ok() - Do we want to receive this frame or not?
1385 *
1386 * Check if we want to further process this frame or not. Also update
1387 * statistics. Return true if we want this frame, false if not.
1388 */
1389static bool
Pavel Roskine0d687b2011-07-14 20:21:55 -04001390ath5k_receive_frame_ok(struct ath5k_hw *ah, struct ath5k_rx_status *rs)
Bruno Randolf02a78b42010-06-16 19:11:56 +09001391{
Pavel Roskine0d687b2011-07-14 20:21:55 -04001392 ah->stats.rx_all_count++;
1393 ah->stats.rx_bytes_count += rs->rs_datalen;
Bruno Randolf02a78b42010-06-16 19:11:56 +09001394
1395 if (unlikely(rs->rs_status)) {
1396 if (rs->rs_status & AR5K_RXERR_CRC)
Pavel Roskine0d687b2011-07-14 20:21:55 -04001397 ah->stats.rxerr_crc++;
Bruno Randolf02a78b42010-06-16 19:11:56 +09001398 if (rs->rs_status & AR5K_RXERR_FIFO)
Pavel Roskine0d687b2011-07-14 20:21:55 -04001399 ah->stats.rxerr_fifo++;
Bruno Randolf02a78b42010-06-16 19:11:56 +09001400 if (rs->rs_status & AR5K_RXERR_PHY) {
Pavel Roskine0d687b2011-07-14 20:21:55 -04001401 ah->stats.rxerr_phy++;
Bruno Randolf02a78b42010-06-16 19:11:56 +09001402 if (rs->rs_phyerr > 0 && rs->rs_phyerr < 32)
Pavel Roskine0d687b2011-07-14 20:21:55 -04001403 ah->stats.rxerr_phy_code[rs->rs_phyerr]++;
Bruno Randolf02a78b42010-06-16 19:11:56 +09001404 return false;
1405 }
1406 if (rs->rs_status & AR5K_RXERR_DECRYPT) {
1407 /*
1408 * Decrypt error. If the error occurred
1409 * because there was no hardware key, then
1410 * let the frame through so the upper layers
1411 * can process it. This is necessary for 5210
1412 * parts which have no way to setup a ``clear''
1413 * key cache entry.
1414 *
1415 * XXX do key cache faulting
1416 */
Pavel Roskine0d687b2011-07-14 20:21:55 -04001417 ah->stats.rxerr_decrypt++;
Bruno Randolf02a78b42010-06-16 19:11:56 +09001418 if (rs->rs_keyix == AR5K_RXKEYIX_INVALID &&
1419 !(rs->rs_status & AR5K_RXERR_CRC))
1420 return true;
1421 }
1422 if (rs->rs_status & AR5K_RXERR_MIC) {
Pavel Roskine0d687b2011-07-14 20:21:55 -04001423 ah->stats.rxerr_mic++;
Bruno Randolf02a78b42010-06-16 19:11:56 +09001424 return true;
1425 }
1426
Bob Copeland23538c22010-08-15 13:03:13 -04001427 /* reject any frames with non-crypto errors */
1428 if (rs->rs_status & ~(AR5K_RXERR_DECRYPT))
Bruno Randolf02a78b42010-06-16 19:11:56 +09001429 return false;
1430 }
1431
1432 if (unlikely(rs->rs_more)) {
Pavel Roskine0d687b2011-07-14 20:21:55 -04001433 ah->stats.rxerr_jumbo++;
Bruno Randolf02a78b42010-06-16 19:11:56 +09001434 return false;
1435 }
1436 return true;
1437}
1438
Bruno Randolf8a89f062010-06-16 19:11:51 +09001439static void
Pavel Roskine0d687b2011-07-14 20:21:55 -04001440ath5k_set_current_imask(struct ath5k_hw *ah)
Felix Fietkauc266c712011-04-10 18:32:19 +02001441{
Pavel Roskin4fc54012011-07-07 18:14:25 -04001442 enum ath5k_int imask;
Felix Fietkauc266c712011-04-10 18:32:19 +02001443 unsigned long flags;
1444
Pavel Roskine0d687b2011-07-14 20:21:55 -04001445 spin_lock_irqsave(&ah->irqlock, flags);
1446 imask = ah->imask;
1447 if (ah->rx_pending)
Felix Fietkauc266c712011-04-10 18:32:19 +02001448 imask &= ~AR5K_INT_RX_ALL;
Pavel Roskine0d687b2011-07-14 20:21:55 -04001449 if (ah->tx_pending)
Felix Fietkauc266c712011-04-10 18:32:19 +02001450 imask &= ~AR5K_INT_TX_ALL;
Pavel Roskine0d687b2011-07-14 20:21:55 -04001451 ath5k_hw_set_imr(ah, imask);
1452 spin_unlock_irqrestore(&ah->irqlock, flags);
Felix Fietkauc266c712011-04-10 18:32:19 +02001453}
1454
1455static void
Bruno Randolf8a89f062010-06-16 19:11:51 +09001456ath5k_tasklet_rx(unsigned long data)
1457{
Bruno Randolfb47f4072008-03-05 18:35:45 +09001458 struct ath5k_rx_status rs = {};
Bob Copelandb6ea0352009-01-10 14:42:54 -05001459 struct sk_buff *skb, *next_skb;
1460 dma_addr_t next_skb_addr;
Pavel Roskine0d687b2011-07-14 20:21:55 -04001461 struct ath5k_hw *ah = (void *)data;
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -08001462 struct ath_common *common = ath5k_hw_common(ah);
Bob Copelandc57ca812009-04-15 07:57:35 -04001463 struct ath5k_buf *bf;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001464 struct ath5k_desc *ds;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001465 int ret;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001466
Pavel Roskine0d687b2011-07-14 20:21:55 -04001467 spin_lock(&ah->rxbuflock);
1468 if (list_empty(&ah->rxbuf)) {
1469 ATH5K_WARN(ah, "empty rx buf pool\n");
Jiri Slaby3a0f2c82008-07-15 17:44:18 +02001470 goto unlock;
1471 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001472 do {
Pavel Roskine0d687b2011-07-14 20:21:55 -04001473 bf = list_first_entry(&ah->rxbuf, struct ath5k_buf, list);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001474 BUG_ON(bf->skb == NULL);
1475 skb = bf->skb;
1476 ds = bf->desc;
1477
Bob Copelandc57ca812009-04-15 07:57:35 -04001478 /* bail if HW is still using self-linked descriptor */
Pavel Roskine0d687b2011-07-14 20:21:55 -04001479 if (ath5k_hw_get_rxdp(ah) == bf->daddr)
Bob Copelandc57ca812009-04-15 07:57:35 -04001480 break;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001481
Pavel Roskine0d687b2011-07-14 20:21:55 -04001482 ret = ah->ah_proc_rx_desc(ah, ds, &rs);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001483 if (unlikely(ret == -EINPROGRESS))
1484 break;
1485 else if (unlikely(ret)) {
Pavel Roskine0d687b2011-07-14 20:21:55 -04001486 ATH5K_ERR(ah, "error in processing rx descriptor\n");
1487 ah->stats.rxerr_proc++;
Bruno Randolfb16062f2010-06-16 19:11:46 +09001488 break;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001489 }
1490
Pavel Roskine0d687b2011-07-14 20:21:55 -04001491 if (ath5k_receive_frame_ok(ah, &rs)) {
1492 next_skb = ath5k_rx_skb_alloc(ah, &next_skb_addr);
Bruno Randolf76443952010-03-09 16:56:00 +09001493
Bruno Randolf02a78b42010-06-16 19:11:56 +09001494 /*
1495 * If we can't replace bf->skb with a new skb under
1496 * memory pressure, just skip this packet
1497 */
1498 if (!next_skb)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001499 goto next;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001500
Pavel Roskine0d687b2011-07-14 20:21:55 -04001501 dma_unmap_single(ah->dev, bf->skbaddr,
Bruno Randolf02a78b42010-06-16 19:11:56 +09001502 common->rx_bufsize,
Felix Fietkauaeae4ac2010-12-02 10:26:51 +01001503 DMA_FROM_DEVICE);
Bruno Randolf02a78b42010-06-16 19:11:56 +09001504
1505 skb_put(skb, rs.rs_datalen);
1506
Pavel Roskine0d687b2011-07-14 20:21:55 -04001507 ath5k_receive_frame(ah, skb, &rs);
Bruno Randolf02a78b42010-06-16 19:11:56 +09001508
1509 bf->skb = next_skb;
1510 bf->skbaddr = next_skb_addr;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001511 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001512next:
Pavel Roskine0d687b2011-07-14 20:21:55 -04001513 list_move_tail(&bf->list, &ah->rxbuf);
1514 } while (ath5k_rxbuf_setup(ah, bf) == 0);
Jiri Slaby3a0f2c82008-07-15 17:44:18 +02001515unlock:
Pavel Roskine0d687b2011-07-14 20:21:55 -04001516 spin_unlock(&ah->rxbuflock);
1517 ah->rx_pending = false;
1518 ath5k_set_current_imask(ah);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001519}
1520
1521
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001522/*************\
1523* TX Handling *
1524\*************/
1525
Johannes Berg7bb45682011-02-24 14:42:06 +01001526void
Bruno Randolfcd2c5482010-12-22 19:20:32 +09001527ath5k_tx_queue(struct ieee80211_hw *hw, struct sk_buff *skb,
1528 struct ath5k_txq *txq)
Bob Copeland8a63fac2010-09-17 12:45:07 +09001529{
Pavel Roskine0d687b2011-07-14 20:21:55 -04001530 struct ath5k_hw *ah = hw->priv;
Bob Copeland8a63fac2010-09-17 12:45:07 +09001531 struct ath5k_buf *bf;
1532 unsigned long flags;
1533 int padsize;
1534
Pavel Roskine0d687b2011-07-14 20:21:55 -04001535 trace_ath5k_tx(ah, skb, txq);
Bob Copeland8a63fac2010-09-17 12:45:07 +09001536
1537 /*
1538 * The hardware expects the header padded to 4 byte boundaries.
1539 * If this is not the case, we add the padding after the header.
1540 */
1541 padsize = ath5k_add_padding(skb);
1542 if (padsize < 0) {
Pavel Roskine0d687b2011-07-14 20:21:55 -04001543 ATH5K_ERR(ah, "tx hdrlen not %%4: not enough"
Bob Copeland8a63fac2010-09-17 12:45:07 +09001544 " headroom to pad");
1545 goto drop_packet;
1546 }
1547
Felix Fietkau4e868792011-07-12 09:02:05 +08001548 if (txq->txq_len >= txq->txq_max &&
1549 txq->qnum <= AR5K_TX_QUEUE_ID_DATA_MAX)
Bruno Randolf925e0b02010-09-17 11:36:35 +09001550 ieee80211_stop_queue(hw, txq->qnum);
1551
Pavel Roskine0d687b2011-07-14 20:21:55 -04001552 spin_lock_irqsave(&ah->txbuflock, flags);
1553 if (list_empty(&ah->txbuf)) {
1554 ATH5K_ERR(ah, "no further txbuf available, dropping packet\n");
1555 spin_unlock_irqrestore(&ah->txbuflock, flags);
Bruno Randolf651d9372010-09-17 11:36:46 +09001556 ieee80211_stop_queues(hw);
Bob Copeland8a63fac2010-09-17 12:45:07 +09001557 goto drop_packet;
1558 }
Pavel Roskine0d687b2011-07-14 20:21:55 -04001559 bf = list_first_entry(&ah->txbuf, struct ath5k_buf, list);
Bob Copeland8a63fac2010-09-17 12:45:07 +09001560 list_del(&bf->list);
Pavel Roskine0d687b2011-07-14 20:21:55 -04001561 ah->txbuf_len--;
1562 if (list_empty(&ah->txbuf))
Bob Copeland8a63fac2010-09-17 12:45:07 +09001563 ieee80211_stop_queues(hw);
Pavel Roskine0d687b2011-07-14 20:21:55 -04001564 spin_unlock_irqrestore(&ah->txbuflock, flags);
Bob Copeland8a63fac2010-09-17 12:45:07 +09001565
1566 bf->skb = skb;
1567
Pavel Roskine0d687b2011-07-14 20:21:55 -04001568 if (ath5k_txbuf_setup(ah, bf, txq, padsize)) {
Bob Copeland8a63fac2010-09-17 12:45:07 +09001569 bf->skb = NULL;
Pavel Roskine0d687b2011-07-14 20:21:55 -04001570 spin_lock_irqsave(&ah->txbuflock, flags);
1571 list_add_tail(&bf->list, &ah->txbuf);
1572 ah->txbuf_len++;
1573 spin_unlock_irqrestore(&ah->txbuflock, flags);
Bob Copeland8a63fac2010-09-17 12:45:07 +09001574 goto drop_packet;
1575 }
Johannes Berg7bb45682011-02-24 14:42:06 +01001576 return;
Bob Copeland8a63fac2010-09-17 12:45:07 +09001577
1578drop_packet:
1579 dev_kfree_skb_any(skb);
Bob Copeland8a63fac2010-09-17 12:45:07 +09001580}
1581
Bruno Randolf14404012010-09-17 11:36:51 +09001582static void
Pavel Roskine0d687b2011-07-14 20:21:55 -04001583ath5k_tx_frame_completed(struct ath5k_hw *ah, struct sk_buff *skb,
Bob Copeland0e472252011-01-24 23:32:55 -05001584 struct ath5k_txq *txq, struct ath5k_tx_status *ts)
Bruno Randolf14404012010-09-17 11:36:51 +09001585{
1586 struct ieee80211_tx_info *info;
Felix Fietkaued895082011-04-10 18:32:17 +02001587 u8 tries[3];
Bruno Randolf14404012010-09-17 11:36:51 +09001588 int i;
1589
Pavel Roskine0d687b2011-07-14 20:21:55 -04001590 ah->stats.tx_all_count++;
1591 ah->stats.tx_bytes_count += skb->len;
Bruno Randolf14404012010-09-17 11:36:51 +09001592 info = IEEE80211_SKB_CB(skb);
1593
Felix Fietkaued895082011-04-10 18:32:17 +02001594 tries[0] = info->status.rates[0].count;
1595 tries[1] = info->status.rates[1].count;
1596 tries[2] = info->status.rates[2].count;
1597
Bruno Randolf14404012010-09-17 11:36:51 +09001598 ieee80211_tx_info_clear_status(info);
Felix Fietkaued895082011-04-10 18:32:17 +02001599
1600 for (i = 0; i < ts->ts_final_idx; i++) {
Bruno Randolf14404012010-09-17 11:36:51 +09001601 struct ieee80211_tx_rate *r =
1602 &info->status.rates[i];
1603
Felix Fietkaued895082011-04-10 18:32:17 +02001604 r->count = tries[i];
Bruno Randolf14404012010-09-17 11:36:51 +09001605 }
1606
Felix Fietkaued895082011-04-10 18:32:17 +02001607 info->status.rates[ts->ts_final_idx].count = ts->ts_final_retry;
Felix Fietkau6d7b97b2011-04-09 21:37:14 +02001608 info->status.rates[ts->ts_final_idx + 1].idx = -1;
Bruno Randolf14404012010-09-17 11:36:51 +09001609
1610 if (unlikely(ts->ts_status)) {
Pavel Roskine0d687b2011-07-14 20:21:55 -04001611 ah->stats.ack_fail++;
Bruno Randolf14404012010-09-17 11:36:51 +09001612 if (ts->ts_status & AR5K_TXERR_FILT) {
1613 info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
Pavel Roskine0d687b2011-07-14 20:21:55 -04001614 ah->stats.txerr_filt++;
Bruno Randolf14404012010-09-17 11:36:51 +09001615 }
1616 if (ts->ts_status & AR5K_TXERR_XRETRY)
Pavel Roskine0d687b2011-07-14 20:21:55 -04001617 ah->stats.txerr_retry++;
Bruno Randolf14404012010-09-17 11:36:51 +09001618 if (ts->ts_status & AR5K_TXERR_FIFO)
Pavel Roskine0d687b2011-07-14 20:21:55 -04001619 ah->stats.txerr_fifo++;
Bruno Randolf14404012010-09-17 11:36:51 +09001620 } else {
1621 info->flags |= IEEE80211_TX_STAT_ACK;
1622 info->status.ack_signal = ts->ts_rssi;
Felix Fietkau6d7b97b2011-04-09 21:37:14 +02001623
1624 /* count the successful attempt as well */
1625 info->status.rates[ts->ts_final_idx].count++;
Bruno Randolf14404012010-09-17 11:36:51 +09001626 }
1627
1628 /*
1629 * Remove MAC header padding before giving the frame
1630 * back to mac80211.
1631 */
1632 ath5k_remove_padding(skb);
1633
1634 if (ts->ts_antenna > 0 && ts->ts_antenna < 5)
Pavel Roskine0d687b2011-07-14 20:21:55 -04001635 ah->stats.antenna_tx[ts->ts_antenna]++;
Bruno Randolf14404012010-09-17 11:36:51 +09001636 else
Pavel Roskine0d687b2011-07-14 20:21:55 -04001637 ah->stats.antenna_tx[0]++; /* invalid */
Bruno Randolf14404012010-09-17 11:36:51 +09001638
Pavel Roskine0d687b2011-07-14 20:21:55 -04001639 trace_ath5k_tx_complete(ah, skb, txq, ts);
1640 ieee80211_tx_status(ah->hw, skb);
Bruno Randolf14404012010-09-17 11:36:51 +09001641}
Bob Copeland8a63fac2010-09-17 12:45:07 +09001642
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001643static void
Pavel Roskine0d687b2011-07-14 20:21:55 -04001644ath5k_tx_processq(struct ath5k_hw *ah, struct ath5k_txq *txq)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001645{
Bruno Randolfb47f4072008-03-05 18:35:45 +09001646 struct ath5k_tx_status ts = {};
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001647 struct ath5k_buf *bf, *bf0;
1648 struct ath5k_desc *ds;
1649 struct sk_buff *skb;
Bruno Randolf14404012010-09-17 11:36:51 +09001650 int ret;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001651
1652 spin_lock(&txq->lock);
1653 list_for_each_entry_safe(bf, bf0, &txq->q, list) {
Bruno Randolf23413292010-09-17 11:37:07 +09001654
1655 txq->txq_poll_mark = false;
1656
1657 /* skb might already have been processed last time. */
1658 if (bf->skb != NULL) {
1659 ds = bf->desc;
1660
Pavel Roskine0d687b2011-07-14 20:21:55 -04001661 ret = ah->ah_proc_tx_desc(ah, ds, &ts);
Bruno Randolf23413292010-09-17 11:37:07 +09001662 if (unlikely(ret == -EINPROGRESS))
1663 break;
1664 else if (unlikely(ret)) {
Pavel Roskine0d687b2011-07-14 20:21:55 -04001665 ATH5K_ERR(ah,
Bruno Randolf23413292010-09-17 11:37:07 +09001666 "error %d while processing "
1667 "queue %u\n", ret, txq->qnum);
1668 break;
1669 }
1670
1671 skb = bf->skb;
1672 bf->skb = NULL;
Felix Fietkauaeae4ac2010-12-02 10:26:51 +01001673
Pavel Roskine0d687b2011-07-14 20:21:55 -04001674 dma_unmap_single(ah->dev, bf->skbaddr, skb->len,
Felix Fietkauaeae4ac2010-12-02 10:26:51 +01001675 DMA_TO_DEVICE);
Pavel Roskine0d687b2011-07-14 20:21:55 -04001676 ath5k_tx_frame_completed(ah, skb, txq, &ts);
Bruno Randolf23413292010-09-17 11:37:07 +09001677 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001678
Bob Copelanda05988b2010-04-07 23:55:58 -04001679 /*
1680 * It's possible that the hardware can say the buffer is
1681 * completed when it hasn't yet loaded the ds_link from
Bruno Randolf23413292010-09-17 11:37:07 +09001682 * host memory and moved on.
1683 * Always keep the last descriptor to avoid HW races...
Bob Copelanda05988b2010-04-07 23:55:58 -04001684 */
Pavel Roskine0d687b2011-07-14 20:21:55 -04001685 if (ath5k_hw_get_txdp(ah, txq->qnum) != bf->daddr) {
1686 spin_lock(&ah->txbuflock);
1687 list_move_tail(&bf->list, &ah->txbuf);
1688 ah->txbuf_len++;
Bruno Randolf23413292010-09-17 11:37:07 +09001689 txq->txq_len--;
Pavel Roskine0d687b2011-07-14 20:21:55 -04001690 spin_unlock(&ah->txbuflock);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001691 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001692 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001693 spin_unlock(&txq->lock);
Bruno Randolf4198a8d2010-10-05 13:27:17 +09001694 if (txq->txq_len < ATH5K_TXQ_LEN_LOW && txq->qnum < 4)
Pavel Roskine0d687b2011-07-14 20:21:55 -04001695 ieee80211_wake_queue(ah->hw, txq->qnum);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001696}
1697
1698static void
1699ath5k_tasklet_tx(unsigned long data)
1700{
Bob Copeland8784d2e2009-07-29 17:32:28 -04001701 int i;
Pavel Roskine0d687b2011-07-14 20:21:55 -04001702 struct ath5k_hw *ah = (void *)data;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001703
Pavel Roskine4bbf2f2011-07-07 18:14:13 -04001704 for (i = 0; i < AR5K_NUM_TX_QUEUES; i++)
Nick Kossifidis7ff7c822011-11-25 20:40:20 +02001705 if (ah->txqs[i].setup && (ah->ah_txq_isr_txok_all & BIT(i)))
Pavel Roskine0d687b2011-07-14 20:21:55 -04001706 ath5k_tx_processq(ah, &ah->txqs[i]);
Felix Fietkauc266c712011-04-10 18:32:19 +02001707
Pavel Roskine0d687b2011-07-14 20:21:55 -04001708 ah->tx_pending = false;
1709 ath5k_set_current_imask(ah);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001710}
1711
1712
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001713/*****************\
1714* Beacon handling *
1715\*****************/
1716
1717/*
1718 * Setup the beacon frame for transmit.
1719 */
1720static int
Pavel Roskine0d687b2011-07-14 20:21:55 -04001721ath5k_beacon_setup(struct ath5k_hw *ah, struct ath5k_buf *bf)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001722{
1723 struct sk_buff *skb = bf->skb;
Johannes Berga888d522008-05-26 16:43:39 +02001724 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001725 struct ath5k_desc *ds;
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04001726 int ret = 0;
1727 u8 antenna;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001728 u32 flags;
Benoit Papillault8127fbd2010-02-27 23:05:26 +01001729 const int padsize = 0;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001730
Pavel Roskine0d687b2011-07-14 20:21:55 -04001731 bf->skbaddr = dma_map_single(ah->dev, skb->data, skb->len,
Felix Fietkauaeae4ac2010-12-02 10:26:51 +01001732 DMA_TO_DEVICE);
Pavel Roskine0d687b2011-07-14 20:21:55 -04001733 ATH5K_DBG(ah, ATH5K_DEBUG_BEACON, "skb %p [data %p len %u] "
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001734 "skbaddr %llx\n", skb, skb->data, skb->len,
1735 (unsigned long long)bf->skbaddr);
Felix Fietkauaeae4ac2010-12-02 10:26:51 +01001736
Pavel Roskine0d687b2011-07-14 20:21:55 -04001737 if (dma_mapping_error(ah->dev, bf->skbaddr)) {
1738 ATH5K_ERR(ah, "beacon DMA mapping failed\n");
Bob Copelandbdc71bc2011-08-07 19:36:07 -04001739 dev_kfree_skb_any(skb);
1740 bf->skb = NULL;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001741 return -EIO;
1742 }
1743
1744 ds = bf->desc;
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04001745 antenna = ah->ah_tx_ant;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001746
1747 flags = AR5K_TXDESC_NOACK;
Pavel Roskine0d687b2011-07-14 20:21:55 -04001748 if (ah->opmode == NL80211_IFTYPE_ADHOC && ath5k_hw_hasveol(ah)) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001749 ds->ds_link = bf->daddr; /* self-linked */
1750 flags |= AR5K_TXDESC_VEOL;
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04001751 } else
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001752 ds->ds_link = 0;
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04001753
1754 /*
1755 * If we use multiple antennas on AP and use
1756 * the Sectored AP scenario, switch antenna every
1757 * 4 beacons to make sure everybody hears our AP.
1758 * When a client tries to associate, hw will keep
1759 * track of the tx antenna to be used for this client
Pavel Roskin6a2a0e72011-07-09 00:17:51 -04001760 * automatically, based on ACKed packets.
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04001761 *
1762 * Note: AP still listens and transmits RTS on the
1763 * default antenna which is supposed to be an omni.
1764 *
1765 * Note2: On sectored scenarios it's possible to have
Bob Copelanda180a132010-08-15 13:03:12 -04001766 * multiple antennas (1 omni -- the default -- and 14
1767 * sectors), so if we choose to actually support this
1768 * mode, we need to allow the user to set how many antennas
1769 * we have and tweak the code below to send beacons
1770 * on all of them.
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04001771 */
1772 if (ah->ah_ant_mode == AR5K_ANTMODE_SECTOR_AP)
Pavel Roskine0d687b2011-07-14 20:21:55 -04001773 antenna = ah->bsent & 4 ? 2 : 1;
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04001774
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001775
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02001776 /* FIXME: If we are in g mode and rate is a CCK rate
1777 * subtract ah->ah_txpower.txp_cck_ofdm_pwr_delta
1778 * from tx power (value is in dB units already) */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001779 ds->ds_data = bf->skbaddr;
Bruno Randolf281c56d2008-02-05 18:44:55 +09001780 ret = ah->ah_setup_tx_desc(ah, ds, skb->len,
Benoit Papillault8127fbd2010-02-27 23:05:26 +01001781 ieee80211_get_hdrlen_from_skb(skb), padsize,
Nick Kossifidis987af542012-08-05 22:35:36 +03001782 AR5K_PKT_TYPE_BEACON,
1783 (ah->ah_txpower.txp_requested * 2),
Pavel Roskine0d687b2011-07-14 20:21:55 -04001784 ieee80211_get_tx_rate(ah->hw, info)->hw_value,
Johannes Berg2e92e6f2008-05-15 12:55:27 +02001785 1, AR5K_TXKEYIX_INVALID,
Luis R. Rodriguez400ec452008-02-03 21:51:49 -05001786 antenna, flags, 0, 0);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001787 if (ret)
1788 goto err_unmap;
1789
1790 return 0;
1791err_unmap:
Pavel Roskine0d687b2011-07-14 20:21:55 -04001792 dma_unmap_single(ah->dev, bf->skbaddr, skb->len, DMA_TO_DEVICE);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001793 return ret;
1794}
1795
1796/*
Bob Copeland8a63fac2010-09-17 12:45:07 +09001797 * Updates the beacon that is sent by ath5k_beacon_send. For adhoc,
1798 * this is called only once at config_bss time, for AP we do it every
1799 * SWBA interrupt so that the TIM will reflect buffered frames.
1800 *
1801 * Called with the beacon lock.
1802 */
Bruno Randolfcd2c5482010-12-22 19:20:32 +09001803int
Bob Copeland8a63fac2010-09-17 12:45:07 +09001804ath5k_beacon_update(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1805{
1806 int ret;
Pavel Roskine0d687b2011-07-14 20:21:55 -04001807 struct ath5k_hw *ah = hw->priv;
Wei Yongjun9c371f92012-10-08 08:42:58 +08001808 struct ath5k_vif *avf;
Bob Copeland8a63fac2010-09-17 12:45:07 +09001809 struct sk_buff *skb;
1810
1811 if (WARN_ON(!vif)) {
1812 ret = -EINVAL;
1813 goto out;
1814 }
1815
1816 skb = ieee80211_beacon_get(hw, vif);
1817
1818 if (!skb) {
1819 ret = -ENOMEM;
1820 goto out;
1821 }
1822
Wei Yongjun9c371f92012-10-08 08:42:58 +08001823 avf = (void *)vif->drv_priv;
Pavel Roskine0d687b2011-07-14 20:21:55 -04001824 ath5k_txbuf_free_skb(ah, avf->bbuf);
Ben Greearb1ae1ed2010-09-30 12:22:58 -07001825 avf->bbuf->skb = skb;
Pavel Roskine0d687b2011-07-14 20:21:55 -04001826 ret = ath5k_beacon_setup(ah, avf->bbuf);
Bob Copeland8a63fac2010-09-17 12:45:07 +09001827out:
1828 return ret;
1829}
1830
1831/*
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001832 * Transmit a beacon frame at SWBA. Dynamic updates to the
1833 * frame contents are done as needed and the slot time is
1834 * also adjusted based on current state.
1835 *
Bob Copeland5faaff72010-07-13 11:32:40 -04001836 * This is called from software irq context (beacontq tasklets)
1837 * or user context from ath5k_beacon_config.
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001838 */
1839static void
Pavel Roskine0d687b2011-07-14 20:21:55 -04001840ath5k_beacon_send(struct ath5k_hw *ah)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001841{
Ben Greearb1ae1ed2010-09-30 12:22:58 -07001842 struct ieee80211_vif *vif;
1843 struct ath5k_vif *avf;
1844 struct ath5k_buf *bf;
Bob Copelandcec8db22009-07-04 12:59:51 -04001845 struct sk_buff *skb;
Bob Copelandbdc71bc2011-08-07 19:36:07 -04001846 int err;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001847
Pavel Roskine0d687b2011-07-14 20:21:55 -04001848 ATH5K_DBG_UNLIMIT(ah, ATH5K_DEBUG_BEACON, "in beacon_send\n");
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001849
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001850 /*
1851 * Check if the previous beacon has gone out. If
Bob Copelanda180a132010-08-15 13:03:12 -04001852 * not, don't don't try to post another: skip this
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001853 * period and wait for the next. Missed beacons
1854 * indicate a problem and should not occur. If we
1855 * miss too many consecutive beacons reset the device.
1856 */
Pavel Roskine0d687b2011-07-14 20:21:55 -04001857 if (unlikely(ath5k_hw_num_tx_pending(ah, ah->bhalq) != 0)) {
1858 ah->bmisscount++;
1859 ATH5K_DBG(ah, ATH5K_DEBUG_BEACON,
1860 "missed %u consecutive beacons\n", ah->bmisscount);
1861 if (ah->bmisscount > 10) { /* NB: 10 is a guess */
1862 ATH5K_DBG(ah, ATH5K_DEBUG_BEACON,
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001863 "stuck beacon time (%u missed)\n",
Pavel Roskine0d687b2011-07-14 20:21:55 -04001864 ah->bmisscount);
1865 ATH5K_DBG(ah, ATH5K_DEBUG_RESET,
Bruno Randolf8d67a032010-06-16 19:11:12 +09001866 "stuck beacon, resetting\n");
Pavel Roskine0d687b2011-07-14 20:21:55 -04001867 ieee80211_queue_work(ah->hw, &ah->reset_work);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001868 }
1869 return;
1870 }
Pavel Roskine0d687b2011-07-14 20:21:55 -04001871 if (unlikely(ah->bmisscount != 0)) {
1872 ATH5K_DBG(ah, ATH5K_DEBUG_BEACON,
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001873 "resume beacon xmit after %u misses\n",
Pavel Roskine0d687b2011-07-14 20:21:55 -04001874 ah->bmisscount);
1875 ah->bmisscount = 0;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001876 }
1877
Chun-Yeow Yeohda473b62012-03-03 09:48:56 +08001878 if ((ah->opmode == NL80211_IFTYPE_AP && ah->num_ap_vifs +
1879 ah->num_mesh_vifs > 1) ||
Pavel Roskine0d687b2011-07-14 20:21:55 -04001880 ah->opmode == NL80211_IFTYPE_MESH_POINT) {
Ben Greearb1ae1ed2010-09-30 12:22:58 -07001881 u64 tsf = ath5k_hw_get_tsf64(ah);
1882 u32 tsftu = TSF_TO_TU(tsf);
Pavel Roskine0d687b2011-07-14 20:21:55 -04001883 int slot = ((tsftu % ah->bintval) * ATH_BCBUF) / ah->bintval;
1884 vif = ah->bslot[(slot + 1) % ATH_BCBUF];
1885 ATH5K_DBG(ah, ATH5K_DEBUG_BEACON,
Ben Greearb1ae1ed2010-09-30 12:22:58 -07001886 "tsf %llx tsftu %x intval %u slot %u vif %p\n",
Pavel Roskine0d687b2011-07-14 20:21:55 -04001887 (unsigned long long)tsf, tsftu, ah->bintval, slot, vif);
Ben Greearb1ae1ed2010-09-30 12:22:58 -07001888 } else /* only one interface */
Pavel Roskine0d687b2011-07-14 20:21:55 -04001889 vif = ah->bslot[0];
Ben Greearb1ae1ed2010-09-30 12:22:58 -07001890
1891 if (!vif)
1892 return;
1893
1894 avf = (void *)vif->drv_priv;
1895 bf = avf->bbuf;
Ben Greearb1ae1ed2010-09-30 12:22:58 -07001896
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001897 /*
1898 * Stop any current dma and put the new frame on the queue.
1899 * This should never fail since we check above that no frames
1900 * are still pending on the queue.
1901 */
Pavel Roskine0d687b2011-07-14 20:21:55 -04001902 if (unlikely(ath5k_hw_stop_beacon_queue(ah, ah->bhalq))) {
1903 ATH5K_WARN(ah, "beacon queue %u didn't start/stop ?\n", ah->bhalq);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001904 /* NB: hw still stops DMA, so proceed */
1905 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001906
Javier Cardonad82b5772010-12-07 13:35:55 -08001907 /* refresh the beacon for AP or MESH mode */
Pavel Roskine0d687b2011-07-14 20:21:55 -04001908 if (ah->opmode == NL80211_IFTYPE_AP ||
Bob Copelandbdc71bc2011-08-07 19:36:07 -04001909 ah->opmode == NL80211_IFTYPE_MESH_POINT) {
1910 err = ath5k_beacon_update(ah->hw, vif);
1911 if (err)
1912 return;
1913 }
1914
1915 if (unlikely(bf->skb == NULL || ah->opmode == NL80211_IFTYPE_STATION ||
1916 ah->opmode == NL80211_IFTYPE_MONITOR)) {
1917 ATH5K_WARN(ah, "bf=%p bf_skb=%p\n", bf, bf->skb);
1918 return;
1919 }
Bob Copeland1071db82009-05-18 10:59:52 -04001920
Pavel Roskine0d687b2011-07-14 20:21:55 -04001921 trace_ath5k_tx(ah, bf->skb, &ah->txqs[ah->bhalq]);
Bob Copeland0e472252011-01-24 23:32:55 -05001922
Pavel Roskine0d687b2011-07-14 20:21:55 -04001923 ath5k_hw_set_txdp(ah, ah->bhalq, bf->daddr);
1924 ath5k_hw_start_tx_dma(ah, ah->bhalq);
1925 ATH5K_DBG(ah, ATH5K_DEBUG_BEACON, "TXDP[%u] = %llx (%p)\n",
1926 ah->bhalq, (unsigned long long)bf->daddr, bf->desc);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001927
Pavel Roskine0d687b2011-07-14 20:21:55 -04001928 skb = ieee80211_get_buffered_bc(ah->hw, vif);
Bob Copelandcec8db22009-07-04 12:59:51 -04001929 while (skb) {
Pavel Roskine0d687b2011-07-14 20:21:55 -04001930 ath5k_tx_queue(ah->hw, skb, ah->cabq);
Felix Fietkau4e868792011-07-12 09:02:05 +08001931
Pavel Roskine0d687b2011-07-14 20:21:55 -04001932 if (ah->cabq->txq_len >= ah->cabq->txq_max)
Felix Fietkau4e868792011-07-12 09:02:05 +08001933 break;
1934
Pavel Roskine0d687b2011-07-14 20:21:55 -04001935 skb = ieee80211_get_buffered_bc(ah->hw, vif);
Bob Copelandcec8db22009-07-04 12:59:51 -04001936 }
1937
Pavel Roskine0d687b2011-07-14 20:21:55 -04001938 ah->bsent++;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001939}
1940
Bruno Randolf9804b982008-01-19 18:17:59 +09001941/**
1942 * ath5k_beacon_update_timers - update beacon timers
1943 *
Pavel Roskine0d687b2011-07-14 20:21:55 -04001944 * @ah: struct ath5k_hw pointer we are operating on
Bruno Randolf9804b982008-01-19 18:17:59 +09001945 * @bc_tsf: the timestamp of the beacon. 0 to reset the TSF. -1 to perform a
1946 * beacon timer update based on the current HW TSF.
1947 *
1948 * Calculate the next target beacon transmit time (TBTT) based on the timestamp
1949 * of a received beacon or the current local hardware TSF and write it to the
1950 * beacon timer registers.
1951 *
1952 * This is called in a variety of situations, e.g. when a beacon is received,
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001953 * when a TSF update has been detected, but also when an new IBSS is created or
Bruno Randolf9804b982008-01-19 18:17:59 +09001954 * when we otherwise know we have to update the timers, but we keep it in this
1955 * function to have it all together in one place.
1956 */
Bruno Randolfcd2c5482010-12-22 19:20:32 +09001957void
Pavel Roskine0d687b2011-07-14 20:21:55 -04001958ath5k_beacon_update_timers(struct ath5k_hw *ah, u64 bc_tsf)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001959{
Bruno Randolf9804b982008-01-19 18:17:59 +09001960 u32 nexttbtt, intval, hw_tu, bc_tu;
1961 u64 hw_tsf;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001962
Pavel Roskine0d687b2011-07-14 20:21:55 -04001963 intval = ah->bintval & AR5K_BEACON_PERIOD;
Chun-Yeow Yeohda473b62012-03-03 09:48:56 +08001964 if (ah->opmode == NL80211_IFTYPE_AP && ah->num_ap_vifs
1965 + ah->num_mesh_vifs > 1) {
Ben Greearb1ae1ed2010-09-30 12:22:58 -07001966 intval /= ATH_BCBUF; /* staggered multi-bss beacons */
1967 if (intval < 15)
Pavel Roskine0d687b2011-07-14 20:21:55 -04001968 ATH5K_WARN(ah, "intval %u is too low, min 15\n",
Ben Greearb1ae1ed2010-09-30 12:22:58 -07001969 intval);
1970 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001971 if (WARN_ON(!intval))
1972 return;
1973
Bruno Randolf9804b982008-01-19 18:17:59 +09001974 /* beacon TSF converted to TU */
1975 bc_tu = TSF_TO_TU(bc_tsf);
1976
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001977 /* current TSF converted to TU */
Bruno Randolf9804b982008-01-19 18:17:59 +09001978 hw_tsf = ath5k_hw_get_tsf64(ah);
1979 hw_tu = TSF_TO_TU(hw_tsf);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001980
Pavel Roskin633d0062011-07-07 18:14:01 -04001981#define FUDGE (AR5K_TUNE_SW_BEACON_RESP + 3)
Bruno Randolf11f21df2010-09-27 12:22:26 +09001982 /* We use FUDGE to make sure the next TBTT is ahead of the current TU.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001983 * Since we later subtract AR5K_TUNE_SW_BEACON_RESP (10) in the timer
Bruno Randolf11f21df2010-09-27 12:22:26 +09001984 * configuration we need to make sure it is bigger than that. */
1985
Bruno Randolf9804b982008-01-19 18:17:59 +09001986 if (bc_tsf == -1) {
1987 /*
1988 * no beacons received, called internally.
1989 * just need to refresh timers based on HW TSF.
1990 */
1991 nexttbtt = roundup(hw_tu + FUDGE, intval);
1992 } else if (bc_tsf == 0) {
1993 /*
1994 * no beacon received, probably called by ath5k_reset_tsf().
1995 * reset TSF to start with 0.
1996 */
1997 nexttbtt = intval;
1998 intval |= AR5K_BEACON_RESET_TSF;
1999 } else if (bc_tsf > hw_tsf) {
2000 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002001 * beacon received, SW merge happened but HW TSF not yet updated.
Bruno Randolf9804b982008-01-19 18:17:59 +09002002 * not possible to reconfigure timers yet, but next time we
2003 * receive a beacon with the same BSSID, the hardware will
2004 * automatically update the TSF and then we need to reconfigure
2005 * the timers.
2006 */
Pavel Roskine0d687b2011-07-14 20:21:55 -04002007 ATH5K_DBG_UNLIMIT(ah, ATH5K_DEBUG_BEACON,
Bruno Randolf9804b982008-01-19 18:17:59 +09002008 "need to wait for HW TSF sync\n");
2009 return;
2010 } else {
2011 /*
2012 * most important case for beacon synchronization between STA.
2013 *
2014 * beacon received and HW TSF has been already updated by HW.
2015 * update next TBTT based on the TSF of the beacon, but make
2016 * sure it is ahead of our local TSF timer.
2017 */
2018 nexttbtt = bc_tu + roundup(hw_tu + FUDGE - bc_tu, intval);
2019 }
2020#undef FUDGE
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002021
Pavel Roskine0d687b2011-07-14 20:21:55 -04002022 ah->nexttbtt = nexttbtt;
Bruno Randolf036cd1e2008-01-19 18:18:21 +09002023
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002024 intval |= AR5K_BEACON_ENA;
Nick Kossifidisc47faa32011-11-25 20:40:25 +02002025 ath5k_hw_init_beacon_timers(ah, nexttbtt, intval);
Bruno Randolf9804b982008-01-19 18:17:59 +09002026
2027 /*
2028 * debugging output last in order to preserve the time critical aspect
2029 * of this function
2030 */
2031 if (bc_tsf == -1)
Pavel Roskine0d687b2011-07-14 20:21:55 -04002032 ATH5K_DBG_UNLIMIT(ah, ATH5K_DEBUG_BEACON,
Bruno Randolf9804b982008-01-19 18:17:59 +09002033 "reconfigured timers based on HW TSF\n");
2034 else if (bc_tsf == 0)
Pavel Roskine0d687b2011-07-14 20:21:55 -04002035 ATH5K_DBG_UNLIMIT(ah, ATH5K_DEBUG_BEACON,
Bruno Randolf9804b982008-01-19 18:17:59 +09002036 "reset HW TSF and timers\n");
2037 else
Pavel Roskine0d687b2011-07-14 20:21:55 -04002038 ATH5K_DBG_UNLIMIT(ah, ATH5K_DEBUG_BEACON,
Bruno Randolf9804b982008-01-19 18:17:59 +09002039 "updated timers based on beacon TSF\n");
2040
Pavel Roskine0d687b2011-07-14 20:21:55 -04002041 ATH5K_DBG_UNLIMIT(ah, ATH5K_DEBUG_BEACON,
David Miller04f93a82008-02-15 16:08:59 -08002042 "bc_tsf %llx hw_tsf %llx bc_tu %u hw_tu %u nexttbtt %u\n",
2043 (unsigned long long) bc_tsf,
2044 (unsigned long long) hw_tsf, bc_tu, hw_tu, nexttbtt);
Pavel Roskine0d687b2011-07-14 20:21:55 -04002045 ATH5K_DBG_UNLIMIT(ah, ATH5K_DEBUG_BEACON, "intval %u %s %s\n",
Bruno Randolf9804b982008-01-19 18:17:59 +09002046 intval & AR5K_BEACON_PERIOD,
2047 intval & AR5K_BEACON_ENA ? "AR5K_BEACON_ENA" : "",
2048 intval & AR5K_BEACON_RESET_TSF ? "AR5K_BEACON_RESET_TSF" : "");
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002049}
2050
Bruno Randolf036cd1e2008-01-19 18:18:21 +09002051/**
2052 * ath5k_beacon_config - Configure the beacon queues and interrupts
2053 *
Pavel Roskine0d687b2011-07-14 20:21:55 -04002054 * @ah: struct ath5k_hw pointer we are operating on
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002055 *
Bruno Randolf036cd1e2008-01-19 18:18:21 +09002056 * In IBSS mode we use a self-linked tx descriptor if possible. We enable SWBA
Bruno Randolf6ba81c22008-03-05 18:36:26 +09002057 * interrupts to detect TSF updates only.
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002058 */
Bruno Randolfcd2c5482010-12-22 19:20:32 +09002059void
Pavel Roskine0d687b2011-07-14 20:21:55 -04002060ath5k_beacon_config(struct ath5k_hw *ah)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002061{
Bob Copeland7dd67532012-08-12 21:18:33 -04002062 spin_lock_bh(&ah->block);
Pavel Roskine0d687b2011-07-14 20:21:55 -04002063 ah->bmisscount = 0;
2064 ah->imask &= ~(AR5K_INT_BMISS | AR5K_INT_SWBA);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002065
Pavel Roskine0d687b2011-07-14 20:21:55 -04002066 if (ah->enable_beacon) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002067 /*
Bruno Randolf036cd1e2008-01-19 18:18:21 +09002068 * In IBSS mode we use a self-linked tx descriptor and let the
2069 * hardware send the beacons automatically. We have to load it
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002070 * only once here.
Bruno Randolf036cd1e2008-01-19 18:18:21 +09002071 * We use the SWBA interrupt only to keep track of the beacon
Bruno Randolf6ba81c22008-03-05 18:36:26 +09002072 * timers in order to detect automatic TSF updates.
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002073 */
Pavel Roskine0d687b2011-07-14 20:21:55 -04002074 ath5k_beaconq_config(ah);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002075
Pavel Roskine0d687b2011-07-14 20:21:55 -04002076 ah->imask |= AR5K_INT_SWBA;
Bruno Randolf036cd1e2008-01-19 18:18:21 +09002077
Pavel Roskine0d687b2011-07-14 20:21:55 -04002078 if (ah->opmode == NL80211_IFTYPE_ADHOC) {
Bob Copeland21800492009-07-04 12:59:52 -04002079 if (ath5k_hw_hasveol(ah))
Pavel Roskine0d687b2011-07-14 20:21:55 -04002080 ath5k_beacon_send(ah);
Jiri Slabyda966bc2008-10-12 22:54:10 +02002081 } else
Pavel Roskine0d687b2011-07-14 20:21:55 -04002082 ath5k_beacon_update_timers(ah, -1);
Bob Copeland21800492009-07-04 12:59:52 -04002083 } else {
Pavel Roskine0d687b2011-07-14 20:21:55 -04002084 ath5k_hw_stop_beacon_queue(ah, ah->bhalq);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002085 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002086
Pavel Roskine0d687b2011-07-14 20:21:55 -04002087 ath5k_hw_set_imr(ah, ah->imask);
Bob Copeland21800492009-07-04 12:59:52 -04002088 mmiowb();
Bob Copeland7dd67532012-08-12 21:18:33 -04002089 spin_unlock_bh(&ah->block);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002090}
2091
Nick Kossifidis428cbd42009-04-30 15:55:47 -04002092static void ath5k_tasklet_beacon(unsigned long data)
2093{
Pavel Roskine0d687b2011-07-14 20:21:55 -04002094 struct ath5k_hw *ah = (struct ath5k_hw *) data;
Nick Kossifidis428cbd42009-04-30 15:55:47 -04002095
2096 /*
2097 * Software beacon alert--time to send a beacon.
2098 *
2099 * In IBSS mode we use this interrupt just to
2100 * keep track of the next TBTT (target beacon
Pavel Roskin6a2a0e72011-07-09 00:17:51 -04002101 * transmission time) in order to detect whether
Nick Kossifidis428cbd42009-04-30 15:55:47 -04002102 * automatic TSF updates happened.
2103 */
Pavel Roskine0d687b2011-07-14 20:21:55 -04002104 if (ah->opmode == NL80211_IFTYPE_ADHOC) {
Pavel Roskin6a2a0e72011-07-09 00:17:51 -04002105 /* XXX: only if VEOL supported */
Pavel Roskine0d687b2011-07-14 20:21:55 -04002106 u64 tsf = ath5k_hw_get_tsf64(ah);
2107 ah->nexttbtt += ah->bintval;
2108 ATH5K_DBG(ah, ATH5K_DEBUG_BEACON,
Nick Kossifidis428cbd42009-04-30 15:55:47 -04002109 "SWBA nexttbtt: %x hw_tu: %x "
2110 "TSF: %llx\n",
Pavel Roskine0d687b2011-07-14 20:21:55 -04002111 ah->nexttbtt,
Nick Kossifidis428cbd42009-04-30 15:55:47 -04002112 TSF_TO_TU(tsf),
2113 (unsigned long long) tsf);
2114 } else {
Pavel Roskine0d687b2011-07-14 20:21:55 -04002115 spin_lock(&ah->block);
2116 ath5k_beacon_send(ah);
2117 spin_unlock(&ah->block);
Nick Kossifidis428cbd42009-04-30 15:55:47 -04002118 }
2119}
2120
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002121
2122/********************\
2123* Interrupt handling *
2124\********************/
2125
Bruno Randolf6a8a3f62010-03-25 14:49:19 +09002126static void
2127ath5k_intr_calibration_poll(struct ath5k_hw *ah)
2128{
Bruno Randolf2111ac02010-04-02 18:44:08 +09002129 if (time_is_before_eq_jiffies(ah->ah_cal_next_ani) &&
Nick Kossifidisce169ac2011-11-25 20:40:23 +02002130 !(ah->ah_cal_mask & AR5K_CALIBRATION_FULL) &&
2131 !(ah->ah_cal_mask & AR5K_CALIBRATION_SHORT)) {
2132
2133 /* Run ANI only when calibration is not active */
2134
Bruno Randolf2111ac02010-04-02 18:44:08 +09002135 ah->ah_cal_next_ani = jiffies +
2136 msecs_to_jiffies(ATH5K_TUNE_CALIBRATION_INTERVAL_ANI);
Pavel Roskine0d687b2011-07-14 20:21:55 -04002137 tasklet_schedule(&ah->ani_tasklet);
Bruno Randolf2111ac02010-04-02 18:44:08 +09002138
Nick Kossifidisce169ac2011-11-25 20:40:23 +02002139 } else if (time_is_before_eq_jiffies(ah->ah_cal_next_short) &&
2140 !(ah->ah_cal_mask & AR5K_CALIBRATION_FULL) &&
2141 !(ah->ah_cal_mask & AR5K_CALIBRATION_SHORT)) {
2142
2143 /* Run calibration only when another calibration
2144 * is not running.
2145 *
2146 * Note: This is for both full/short calibration,
2147 * if it's time for a full one, ath5k_calibrate_work will deal
2148 * with it. */
2149
2150 ah->ah_cal_next_short = jiffies +
2151 msecs_to_jiffies(ATH5K_TUNE_CALIBRATION_INTERVAL_SHORT);
2152 ieee80211_queue_work(ah->hw, &ah->calib_work);
Bruno Randolf6a8a3f62010-03-25 14:49:19 +09002153 }
2154 /* we could use SWI to generate enough interrupts to meet our
2155 * calibration interval requirements, if necessary:
2156 * AR5K_REG_ENABLE_BITS(ah, AR5K_CR, AR5K_CR_SWI); */
2157}
2158
Felix Fietkauc266c712011-04-10 18:32:19 +02002159static void
Pavel Roskine0d687b2011-07-14 20:21:55 -04002160ath5k_schedule_rx(struct ath5k_hw *ah)
Felix Fietkauc266c712011-04-10 18:32:19 +02002161{
Pavel Roskine0d687b2011-07-14 20:21:55 -04002162 ah->rx_pending = true;
2163 tasklet_schedule(&ah->rxtq);
Felix Fietkauc266c712011-04-10 18:32:19 +02002164}
2165
2166static void
Pavel Roskine0d687b2011-07-14 20:21:55 -04002167ath5k_schedule_tx(struct ath5k_hw *ah)
Felix Fietkauc266c712011-04-10 18:32:19 +02002168{
Pavel Roskine0d687b2011-07-14 20:21:55 -04002169 ah->tx_pending = true;
2170 tasklet_schedule(&ah->txtq);
Felix Fietkauc266c712011-04-10 18:32:19 +02002171}
2172
Pavel Roskinf5cbc8b2011-06-15 18:03:22 -04002173static irqreturn_t
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002174ath5k_intr(int irq, void *dev_id)
2175{
Pavel Roskine0d687b2011-07-14 20:21:55 -04002176 struct ath5k_hw *ah = dev_id;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002177 enum ath5k_int status;
2178 unsigned int counter = 1000;
2179
Nick Kossifidis34ce6442011-11-25 20:40:22 +02002180
2181 /*
2182 * If hw is not ready (or detached) and we get an
2183 * interrupt, or if we have no interrupts pending
2184 * (that means it's not for us) skip it.
2185 *
2186 * NOTE: Group 0/1 PCI interface registers are not
2187 * supported on WiSOCs, so we can't check for pending
2188 * interrupts (ISR belongs to another register group
2189 * so we are ok).
2190 */
Pavel Roskine0d687b2011-07-14 20:21:55 -04002191 if (unlikely(test_bit(ATH_STAT_INVALID, ah->status) ||
Nick Kossifidis34ce6442011-11-25 20:40:22 +02002192 ((ath5k_get_bus_type(ah) != ATH_AHB) &&
2193 !ath5k_hw_is_intr_pending(ah))))
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002194 return IRQ_NONE;
2195
Nick Kossifidis34ce6442011-11-25 20:40:22 +02002196 /** Main loop **/
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002197 do {
Nick Kossifidis34ce6442011-11-25 20:40:22 +02002198 ath5k_hw_get_isr(ah, &status); /* NB: clears IRQ too */
2199
Pavel Roskine0d687b2011-07-14 20:21:55 -04002200 ATH5K_DBG(ah, ATH5K_DEBUG_INTR, "status 0x%x/0x%x\n",
2201 status, ah->imask);
Nick Kossifidis34ce6442011-11-25 20:40:22 +02002202
2203 /*
2204 * Fatal hw error -> Log and reset
2205 *
2206 * Fatal errors are unrecoverable so we have to
2207 * reset the card. These errors include bus and
2208 * dma errors.
2209 */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002210 if (unlikely(status & AR5K_INT_FATAL)) {
Nick Kossifidis34ce6442011-11-25 20:40:22 +02002211
Pavel Roskine0d687b2011-07-14 20:21:55 -04002212 ATH5K_DBG(ah, ATH5K_DEBUG_RESET,
Bruno Randolf8d67a032010-06-16 19:11:12 +09002213 "fatal int, resetting\n");
Pavel Roskine0d687b2011-07-14 20:21:55 -04002214 ieee80211_queue_work(ah->hw, &ah->reset_work);
Nick Kossifidis34ce6442011-11-25 20:40:22 +02002215
2216 /*
2217 * RX Overrun -> Count and reset if needed
2218 *
2219 * Receive buffers are full. Either the bus is busy or
2220 * the CPU is not fast enough to process all received
2221 * frames.
2222 */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002223 } else if (unlikely(status & AR5K_INT_RXORN)) {
Nick Kossifidis34ce6442011-11-25 20:40:22 +02002224
Bruno Randolf87d77c42010-04-12 16:38:52 +09002225 /*
Bruno Randolf87d77c42010-04-12 16:38:52 +09002226 * Older chipsets need a reset to come out of this
2227 * condition, but we treat it as RX for newer chips.
Nick Kossifidis34ce6442011-11-25 20:40:22 +02002228 * We don't know exactly which versions need a reset
Bruno Randolf87d77c42010-04-12 16:38:52 +09002229 * this guess is copied from the HAL.
2230 */
Pavel Roskine0d687b2011-07-14 20:21:55 -04002231 ah->stats.rxorn_intr++;
Nick Kossifidis34ce6442011-11-25 20:40:22 +02002232
Bruno Randolf8d67a032010-06-16 19:11:12 +09002233 if (ah->ah_mac_srev < AR5K_SREV_AR5212) {
Pavel Roskine0d687b2011-07-14 20:21:55 -04002234 ATH5K_DBG(ah, ATH5K_DEBUG_RESET,
Bruno Randolf8d67a032010-06-16 19:11:12 +09002235 "rx overrun, resetting\n");
Pavel Roskine0d687b2011-07-14 20:21:55 -04002236 ieee80211_queue_work(ah->hw, &ah->reset_work);
Pavel Roskind2c7f772011-07-07 18:14:07 -04002237 } else
Pavel Roskine0d687b2011-07-14 20:21:55 -04002238 ath5k_schedule_rx(ah);
Nick Kossifidis34ce6442011-11-25 20:40:22 +02002239
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002240 } else {
Nick Kossifidis34ce6442011-11-25 20:40:22 +02002241
2242 /* Software Beacon Alert -> Schedule beacon tasklet */
Pavel Roskind2c7f772011-07-07 18:14:07 -04002243 if (status & AR5K_INT_SWBA)
Pavel Roskine0d687b2011-07-14 20:21:55 -04002244 tasklet_hi_schedule(&ah->beacontq);
Pavel Roskind2c7f772011-07-07 18:14:07 -04002245
Nick Kossifidis34ce6442011-11-25 20:40:22 +02002246 /*
2247 * No more RX descriptors -> Just count
2248 *
2249 * NB: the hardware should re-read the link when
2250 * RXE bit is written, but it doesn't work at
2251 * least on older hardware revs.
2252 */
2253 if (status & AR5K_INT_RXEOL)
Pavel Roskine0d687b2011-07-14 20:21:55 -04002254 ah->stats.rxeol_intr++;
Nick Kossifidis34ce6442011-11-25 20:40:22 +02002255
2256
2257 /* TX Underrun -> Bump tx trigger level */
2258 if (status & AR5K_INT_TXURN)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002259 ath5k_hw_update_tx_triglevel(ah, true);
Nick Kossifidis34ce6442011-11-25 20:40:22 +02002260
2261 /* RX -> Schedule rx tasklet */
Nick Kossifidis4c674c62008-10-26 20:40:25 +02002262 if (status & (AR5K_INT_RXOK | AR5K_INT_RXERR))
Pavel Roskine0d687b2011-07-14 20:21:55 -04002263 ath5k_schedule_rx(ah);
Nick Kossifidis34ce6442011-11-25 20:40:22 +02002264
2265 /* TX -> Schedule tx tasklet */
2266 if (status & (AR5K_INT_TXOK
2267 | AR5K_INT_TXDESC
2268 | AR5K_INT_TXERR
2269 | AR5K_INT_TXEOL))
Pavel Roskine0d687b2011-07-14 20:21:55 -04002270 ath5k_schedule_tx(ah);
Nick Kossifidis34ce6442011-11-25 20:40:22 +02002271
2272 /* Missed beacon -> TODO
2273 if (status & AR5K_INT_BMISS)
2274 */
2275
2276 /* MIB event -> Update counters and notify ANI */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002277 if (status & AR5K_INT_MIB) {
Pavel Roskine0d687b2011-07-14 20:21:55 -04002278 ah->stats.mib_intr++;
Bruno Randolf495391d2010-03-25 14:49:36 +09002279 ath5k_hw_update_mib_counters(ah);
Bruno Randolf2111ac02010-04-02 18:44:08 +09002280 ath5k_ani_mib_intr(ah);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002281 }
Nick Kossifidis34ce6442011-11-25 20:40:22 +02002282
2283 /* GPIO -> Notify RFKill layer */
Tobias Doerffele6a3b612009-06-09 17:33:27 +02002284 if (status & AR5K_INT_GPIO)
Pavel Roskine0d687b2011-07-14 20:21:55 -04002285 tasklet_schedule(&ah->rf_kill.toggleq);
Bob Copelanda6ae0712009-06-09 23:43:11 -04002286
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002287 }
Felix Fietkau4cebb342010-12-02 10:27:21 +01002288
2289 if (ath5k_get_bus_type(ah) == ATH_AHB)
2290 break;
2291
Bob Copeland2516baa2009-04-27 22:18:10 -04002292 } while (ath5k_hw_is_intr_pending(ah) && --counter > 0);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002293
Nick Kossifidis34ce6442011-11-25 20:40:22 +02002294 /*
2295 * Until we handle rx/tx interrupts mask them on IMR
2296 *
2297 * NOTE: ah->(rx/tx)_pending are set when scheduling the tasklets
2298 * and unset after we 've handled the interrupts.
2299 */
Pavel Roskine0d687b2011-07-14 20:21:55 -04002300 if (ah->rx_pending || ah->tx_pending)
2301 ath5k_set_current_imask(ah);
Felix Fietkauc266c712011-04-10 18:32:19 +02002302
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002303 if (unlikely(!counter))
Pavel Roskine0d687b2011-07-14 20:21:55 -04002304 ATH5K_WARN(ah, "too many interrupts, giving up for now\n");
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002305
Nick Kossifidis34ce6442011-11-25 20:40:22 +02002306 /* Fire up calibration poll */
Bruno Randolf6a8a3f62010-03-25 14:49:19 +09002307 ath5k_intr_calibration_poll(ah);
Nick Kossifidis6e2206622009-08-10 03:31:31 +03002308
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002309 return IRQ_HANDLED;
2310}
2311
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002312/*
2313 * Periodically recalibrate the PHY to account
2314 * for temperature/environment changes.
2315 */
2316static void
Nick Kossifidisce169ac2011-11-25 20:40:23 +02002317ath5k_calibrate_work(struct work_struct *work)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002318{
Nick Kossifidisce169ac2011-11-25 20:40:23 +02002319 struct ath5k_hw *ah = container_of(work, struct ath5k_hw,
2320 calib_work);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002321
Nick Kossifidisce169ac2011-11-25 20:40:23 +02002322 /* Should we run a full calibration ? */
2323 if (time_is_before_eq_jiffies(ah->ah_cal_next_full)) {
2324
2325 ah->ah_cal_next_full = jiffies +
2326 msecs_to_jiffies(ATH5K_TUNE_CALIBRATION_INTERVAL_FULL);
2327 ah->ah_cal_mask |= AR5K_CALIBRATION_FULL;
2328
2329 ATH5K_DBG(ah, ATH5K_DEBUG_CALIBRATE,
2330 "running full calibration\n");
2331
2332 if (ath5k_hw_gainf_calibrate(ah) == AR5K_RFGAIN_NEED_CHANGE) {
2333 /*
2334 * Rfgain is out of bounds, reset the chip
2335 * to load new gain values.
2336 */
2337 ATH5K_DBG(ah, ATH5K_DEBUG_RESET,
2338 "got new rfgain, resetting\n");
2339 ieee80211_queue_work(ah->hw, &ah->reset_work);
2340 }
Nick Kossifidisce169ac2011-11-25 20:40:23 +02002341 } else
2342 ah->ah_cal_mask |= AR5K_CALIBRATION_SHORT;
2343
Nick Kossifidis6e2206622009-08-10 03:31:31 +03002344
Pavel Roskine0d687b2011-07-14 20:21:55 -04002345 ATH5K_DBG(ah, ATH5K_DEBUG_CALIBRATE, "channel %u/%x\n",
2346 ieee80211_frequency_to_channel(ah->curchan->center_freq),
2347 ah->curchan->hw_value);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002348
Pavel Roskine0d687b2011-07-14 20:21:55 -04002349 if (ath5k_hw_phy_calibrate(ah, ah->curchan))
2350 ATH5K_ERR(ah, "calibration of channel %u failed\n",
Luis R. Rodriguez400ec452008-02-03 21:51:49 -05002351 ieee80211_frequency_to_channel(
Pavel Roskine0d687b2011-07-14 20:21:55 -04002352 ah->curchan->center_freq));
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002353
Nick Kossifidisce169ac2011-11-25 20:40:23 +02002354 /* Clear calibration flags */
Felix Fietkau62e2c102012-03-06 11:06:37 +01002355 if (ah->ah_cal_mask & AR5K_CALIBRATION_FULL)
Nick Kossifidisce169ac2011-11-25 20:40:23 +02002356 ah->ah_cal_mask &= ~AR5K_CALIBRATION_FULL;
Felix Fietkau62e2c102012-03-06 11:06:37 +01002357 else if (ah->ah_cal_mask & AR5K_CALIBRATION_SHORT)
Nick Kossifidisce169ac2011-11-25 20:40:23 +02002358 ah->ah_cal_mask &= ~AR5K_CALIBRATION_SHORT;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002359}
2360
2361
Bruno Randolf2111ac02010-04-02 18:44:08 +09002362static void
2363ath5k_tasklet_ani(unsigned long data)
2364{
Pavel Roskine0d687b2011-07-14 20:21:55 -04002365 struct ath5k_hw *ah = (void *)data;
Bruno Randolf2111ac02010-04-02 18:44:08 +09002366
2367 ah->ah_cal_mask |= AR5K_CALIBRATION_ANI;
2368 ath5k_ani_calibration(ah);
2369 ah->ah_cal_mask &= ~AR5K_CALIBRATION_ANI;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002370}
2371
2372
Bruno Randolf4edd7612010-09-17 11:36:56 +09002373static void
2374ath5k_tx_complete_poll_work(struct work_struct *work)
2375{
Pavel Roskine0d687b2011-07-14 20:21:55 -04002376 struct ath5k_hw *ah = container_of(work, struct ath5k_hw,
Bruno Randolf4edd7612010-09-17 11:36:56 +09002377 tx_complete_work.work);
2378 struct ath5k_txq *txq;
2379 int i;
2380 bool needreset = false;
2381
Pavel Roskine0d687b2011-07-14 20:21:55 -04002382 mutex_lock(&ah->lock);
Bob Copeland599b13a2011-01-18 08:06:43 -05002383
Pavel Roskine0d687b2011-07-14 20:21:55 -04002384 for (i = 0; i < ARRAY_SIZE(ah->txqs); i++) {
2385 if (ah->txqs[i].setup) {
2386 txq = &ah->txqs[i];
Bruno Randolf4edd7612010-09-17 11:36:56 +09002387 spin_lock_bh(&txq->lock);
Bruno Randolf23413292010-09-17 11:37:07 +09002388 if (txq->txq_len > 1) {
Bruno Randolf4edd7612010-09-17 11:36:56 +09002389 if (txq->txq_poll_mark) {
Pavel Roskine0d687b2011-07-14 20:21:55 -04002390 ATH5K_DBG(ah, ATH5K_DEBUG_XMIT,
Bruno Randolf4edd7612010-09-17 11:36:56 +09002391 "TX queue stuck %d\n",
2392 txq->qnum);
2393 needreset = true;
Bruno Randolf923e5b32010-09-17 11:37:02 +09002394 txq->txq_stuck++;
Bruno Randolf4edd7612010-09-17 11:36:56 +09002395 spin_unlock_bh(&txq->lock);
2396 break;
2397 } else {
2398 txq->txq_poll_mark = true;
2399 }
2400 }
2401 spin_unlock_bh(&txq->lock);
2402 }
2403 }
2404
2405 if (needreset) {
Pavel Roskine0d687b2011-07-14 20:21:55 -04002406 ATH5K_DBG(ah, ATH5K_DEBUG_RESET,
Bruno Randolf4edd7612010-09-17 11:36:56 +09002407 "TX queues stuck, resetting\n");
Pavel Roskine0d687b2011-07-14 20:21:55 -04002408 ath5k_reset(ah, NULL, true);
Bruno Randolf4edd7612010-09-17 11:36:56 +09002409 }
2410
Pavel Roskine0d687b2011-07-14 20:21:55 -04002411 mutex_unlock(&ah->lock);
Bob Copeland599b13a2011-01-18 08:06:43 -05002412
Pavel Roskine0d687b2011-07-14 20:21:55 -04002413 ieee80211_queue_delayed_work(ah->hw, &ah->tx_complete_work,
Bruno Randolf4edd7612010-09-17 11:36:56 +09002414 msecs_to_jiffies(ATH5K_TX_COMPLETE_POLL_INT));
2415}
2416
2417
Bob Copeland8a63fac2010-09-17 12:45:07 +09002418/*************************\
2419* Initialization routines *
2420\*************************/
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002421
Felix Fietkau9b4760e2012-04-17 00:39:27 -04002422static const struct ieee80211_iface_limit if_limits[] = {
2423 { .max = 2048, .types = BIT(NL80211_IFTYPE_STATION) },
2424 { .max = 4, .types =
2425#ifdef CONFIG_MAC80211_MESH
2426 BIT(NL80211_IFTYPE_MESH_POINT) |
2427#endif
2428 BIT(NL80211_IFTYPE_AP) },
2429};
2430
2431static const struct ieee80211_iface_combination if_comb = {
2432 .limits = if_limits,
2433 .n_limits = ARRAY_SIZE(if_limits),
2434 .max_interfaces = 2048,
2435 .num_different_channels = 1,
2436};
2437
Pavel Roskin25380d82011-07-07 18:13:42 -04002438int __devinit
Pavel Roskinbb1f3ad2011-07-26 22:27:05 -04002439ath5k_init_ah(struct ath5k_hw *ah, const struct ath_bus_ops *bus_ops)
Felix Fietkau132b1c32010-12-02 10:26:56 +01002440{
Pavel Roskine0d687b2011-07-14 20:21:55 -04002441 struct ieee80211_hw *hw = ah->hw;
Felix Fietkau132b1c32010-12-02 10:26:56 +01002442 struct ath_common *common;
2443 int ret;
2444 int csz;
2445
2446 /* Initialize driver private data */
Pavel Roskine0d687b2011-07-14 20:21:55 -04002447 SET_IEEE80211_DEV(hw, ah->dev);
Felix Fietkau132b1c32010-12-02 10:26:56 +01002448 hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
Nick Kossifidisb9e61f12010-12-03 06:12:39 +02002449 IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
2450 IEEE80211_HW_SIGNAL_DBM |
Chun-Yeow Yeoh90e62742012-09-14 18:26:11 +08002451 IEEE80211_HW_MFP_CAPABLE |
Nick Kossifidisb9e61f12010-12-03 06:12:39 +02002452 IEEE80211_HW_REPORTS_TX_ACK_STATUS;
Felix Fietkau132b1c32010-12-02 10:26:56 +01002453
2454 hw->wiphy->interface_modes =
2455 BIT(NL80211_IFTYPE_AP) |
2456 BIT(NL80211_IFTYPE_STATION) |
2457 BIT(NL80211_IFTYPE_ADHOC) |
2458 BIT(NL80211_IFTYPE_MESH_POINT);
2459
Felix Fietkau9b4760e2012-04-17 00:39:27 -04002460 hw->wiphy->iface_combinations = &if_comb;
2461 hw->wiphy->n_iface_combinations = 1;
2462
Antonio Quartullif9972572012-01-14 11:42:43 +01002463 /* SW support for IBSS_RSN is provided by mac80211 */
2464 hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
2465
Bruno Randolf3de135d2010-12-16 11:30:33 +09002466 /* both antennas can be configured as RX or TX */
2467 hw->wiphy->available_antennas_tx = 0x3;
2468 hw->wiphy->available_antennas_rx = 0x3;
2469
Felix Fietkau132b1c32010-12-02 10:26:56 +01002470 hw->extra_tx_headroom = 2;
2471 hw->channel_change_time = 5000;
2472
2473 /*
2474 * Mark the device as detached to avoid processing
2475 * interrupts until setup is complete.
2476 */
Pavel Roskine0d687b2011-07-14 20:21:55 -04002477 __set_bit(ATH_STAT_INVALID, ah->status);
Felix Fietkau132b1c32010-12-02 10:26:56 +01002478
Pavel Roskine0d687b2011-07-14 20:21:55 -04002479 ah->opmode = NL80211_IFTYPE_STATION;
2480 ah->bintval = 1000;
2481 mutex_init(&ah->lock);
2482 spin_lock_init(&ah->rxbuflock);
2483 spin_lock_init(&ah->txbuflock);
2484 spin_lock_init(&ah->block);
2485 spin_lock_init(&ah->irqlock);
Felix Fietkau132b1c32010-12-02 10:26:56 +01002486
2487 /* Setup interrupt handler */
Pavel Roskine0d687b2011-07-14 20:21:55 -04002488 ret = request_irq(ah->irq, ath5k_intr, IRQF_SHARED, "ath", ah);
Felix Fietkau132b1c32010-12-02 10:26:56 +01002489 if (ret) {
Pavel Roskine0d687b2011-07-14 20:21:55 -04002490 ATH5K_ERR(ah, "request_irq failed\n");
Felix Fietkau132b1c32010-12-02 10:26:56 +01002491 goto err;
2492 }
2493
Pavel Roskine0d687b2011-07-14 20:21:55 -04002494 common = ath5k_hw_common(ah);
Felix Fietkau132b1c32010-12-02 10:26:56 +01002495 common->ops = &ath5k_common_ops;
2496 common->bus_ops = bus_ops;
Pavel Roskine0d687b2011-07-14 20:21:55 -04002497 common->ah = ah;
Felix Fietkau132b1c32010-12-02 10:26:56 +01002498 common->hw = hw;
Pavel Roskine0d687b2011-07-14 20:21:55 -04002499 common->priv = ah;
Felix Fietkau26d16d22011-07-12 09:02:01 +08002500 common->clockrate = 40;
Felix Fietkau132b1c32010-12-02 10:26:56 +01002501
2502 /*
2503 * Cache line size is used to size and align various
2504 * structures used to communicate with the hardware.
2505 */
2506 ath5k_read_cachesize(common, &csz);
2507 common->cachelsz = csz << 2; /* convert to bytes */
2508
2509 spin_lock_init(&common->cc_lock);
2510
2511 /* Initialize device */
Pavel Roskine0d687b2011-07-14 20:21:55 -04002512 ret = ath5k_hw_init(ah);
Felix Fietkau132b1c32010-12-02 10:26:56 +01002513 if (ret)
Pavel Roskine0d687b2011-07-14 20:21:55 -04002514 goto err_irq;
Felix Fietkau132b1c32010-12-02 10:26:56 +01002515
Nick Kossifidis86f62d92011-11-25 20:40:28 +02002516 /* Set up multi-rate retry capabilities */
2517 if (ah->ah_capabilities.cap_has_mrr_support) {
Felix Fietkau132b1c32010-12-02 10:26:56 +01002518 hw->max_rates = 4;
Bruno Randolf76a9f6f2011-01-28 16:52:11 +09002519 hw->max_rate_tries = max(AR5K_INIT_RETRY_SHORT,
2520 AR5K_INIT_RETRY_LONG);
Felix Fietkau132b1c32010-12-02 10:26:56 +01002521 }
2522
2523 hw->vif_data_size = sizeof(struct ath5k_vif);
2524
2525 /* Finish private driver data initialization */
2526 ret = ath5k_init(hw);
2527 if (ret)
2528 goto err_ah;
2529
Pavel Roskine0d687b2011-07-14 20:21:55 -04002530 ATH5K_INFO(ah, "Atheros AR%s chip found (MAC: 0x%x, PHY: 0x%x)\n",
2531 ath5k_chip_name(AR5K_VERSION_MAC, ah->ah_mac_srev),
2532 ah->ah_mac_srev,
2533 ah->ah_phy_revision);
Felix Fietkau132b1c32010-12-02 10:26:56 +01002534
Pavel Roskine0d687b2011-07-14 20:21:55 -04002535 if (!ah->ah_single_chip) {
Felix Fietkau132b1c32010-12-02 10:26:56 +01002536 /* Single chip radio (!RF5111) */
Pavel Roskine0d687b2011-07-14 20:21:55 -04002537 if (ah->ah_radio_5ghz_revision &&
2538 !ah->ah_radio_2ghz_revision) {
Felix Fietkau132b1c32010-12-02 10:26:56 +01002539 /* No 5GHz support -> report 2GHz radio */
2540 if (!test_bit(AR5K_MODE_11A,
Pavel Roskine0d687b2011-07-14 20:21:55 -04002541 ah->ah_capabilities.cap_mode)) {
2542 ATH5K_INFO(ah, "RF%s 2GHz radio found (0x%x)\n",
Felix Fietkau132b1c32010-12-02 10:26:56 +01002543 ath5k_chip_name(AR5K_VERSION_RAD,
Pavel Roskine0d687b2011-07-14 20:21:55 -04002544 ah->ah_radio_5ghz_revision),
2545 ah->ah_radio_5ghz_revision);
Felix Fietkau132b1c32010-12-02 10:26:56 +01002546 /* No 2GHz support (5110 and some
Pavel Roskin6a2a0e72011-07-09 00:17:51 -04002547 * 5GHz only cards) -> report 5GHz radio */
Felix Fietkau132b1c32010-12-02 10:26:56 +01002548 } else if (!test_bit(AR5K_MODE_11B,
Pavel Roskine0d687b2011-07-14 20:21:55 -04002549 ah->ah_capabilities.cap_mode)) {
2550 ATH5K_INFO(ah, "RF%s 5GHz radio found (0x%x)\n",
Felix Fietkau132b1c32010-12-02 10:26:56 +01002551 ath5k_chip_name(AR5K_VERSION_RAD,
Pavel Roskine0d687b2011-07-14 20:21:55 -04002552 ah->ah_radio_5ghz_revision),
2553 ah->ah_radio_5ghz_revision);
Felix Fietkau132b1c32010-12-02 10:26:56 +01002554 /* Multiband radio */
2555 } else {
Pavel Roskine0d687b2011-07-14 20:21:55 -04002556 ATH5K_INFO(ah, "RF%s multiband radio found"
Felix Fietkau132b1c32010-12-02 10:26:56 +01002557 " (0x%x)\n",
2558 ath5k_chip_name(AR5K_VERSION_RAD,
Pavel Roskine0d687b2011-07-14 20:21:55 -04002559 ah->ah_radio_5ghz_revision),
2560 ah->ah_radio_5ghz_revision);
Felix Fietkau132b1c32010-12-02 10:26:56 +01002561 }
2562 }
2563 /* Multi chip radio (RF5111 - RF2111) ->
2564 * report both 2GHz/5GHz radios */
Pavel Roskine0d687b2011-07-14 20:21:55 -04002565 else if (ah->ah_radio_5ghz_revision &&
2566 ah->ah_radio_2ghz_revision) {
2567 ATH5K_INFO(ah, "RF%s 5GHz radio found (0x%x)\n",
Felix Fietkau132b1c32010-12-02 10:26:56 +01002568 ath5k_chip_name(AR5K_VERSION_RAD,
Pavel Roskine0d687b2011-07-14 20:21:55 -04002569 ah->ah_radio_5ghz_revision),
2570 ah->ah_radio_5ghz_revision);
2571 ATH5K_INFO(ah, "RF%s 2GHz radio found (0x%x)\n",
Felix Fietkau132b1c32010-12-02 10:26:56 +01002572 ath5k_chip_name(AR5K_VERSION_RAD,
Pavel Roskine0d687b2011-07-14 20:21:55 -04002573 ah->ah_radio_2ghz_revision),
2574 ah->ah_radio_2ghz_revision);
Felix Fietkau132b1c32010-12-02 10:26:56 +01002575 }
2576 }
2577
Pavel Roskine0d687b2011-07-14 20:21:55 -04002578 ath5k_debug_init_device(ah);
Felix Fietkau132b1c32010-12-02 10:26:56 +01002579
2580 /* ready to process interrupts */
Pavel Roskine0d687b2011-07-14 20:21:55 -04002581 __clear_bit(ATH_STAT_INVALID, ah->status);
Felix Fietkau132b1c32010-12-02 10:26:56 +01002582
2583 return 0;
2584err_ah:
Pavel Roskine0d687b2011-07-14 20:21:55 -04002585 ath5k_hw_deinit(ah);
Felix Fietkau132b1c32010-12-02 10:26:56 +01002586err_irq:
Pavel Roskine0d687b2011-07-14 20:21:55 -04002587 free_irq(ah->irq, ah);
Felix Fietkau132b1c32010-12-02 10:26:56 +01002588err:
2589 return ret;
2590}
2591
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002592static int
Pavel Roskine0d687b2011-07-14 20:21:55 -04002593ath5k_stop_locked(struct ath5k_hw *ah)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002594{
Bob Copelandcec8db22009-07-04 12:59:51 -04002595
Pavel Roskine0d687b2011-07-14 20:21:55 -04002596 ATH5K_DBG(ah, ATH5K_DEBUG_RESET, "invalid %u\n",
2597 test_bit(ATH_STAT_INVALID, ah->status));
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002598
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002599 /*
Bob Copeland8a63fac2010-09-17 12:45:07 +09002600 * Shutdown the hardware and driver:
2601 * stop output from above
2602 * disable interrupts
2603 * turn off timers
2604 * turn off the radio
2605 * clear transmit machinery
2606 * clear receive machinery
2607 * drain and release tx queues
2608 * reclaim beacon resources
2609 * power down hardware
2610 *
2611 * Note that some of this work is not possible if the
2612 * hardware is gone (invalid).
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002613 */
Pavel Roskine0d687b2011-07-14 20:21:55 -04002614 ieee80211_stop_queues(ah->hw);
Bob Copeland8a63fac2010-09-17 12:45:07 +09002615
Pavel Roskine0d687b2011-07-14 20:21:55 -04002616 if (!test_bit(ATH_STAT_INVALID, ah->status)) {
2617 ath5k_led_off(ah);
Bob Copeland8a63fac2010-09-17 12:45:07 +09002618 ath5k_hw_set_imr(ah, 0);
Pavel Roskine0d687b2011-07-14 20:21:55 -04002619 synchronize_irq(ah->irq);
2620 ath5k_rx_stop(ah);
Nick Kossifidis80dac9e2010-11-23 20:45:38 +02002621 ath5k_hw_dma_stop(ah);
Pavel Roskine0d687b2011-07-14 20:21:55 -04002622 ath5k_drain_tx_buffs(ah);
Bob Copeland8a63fac2010-09-17 12:45:07 +09002623 ath5k_hw_phy_disable(ah);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002624 }
2625
Bob Copeland8a63fac2010-09-17 12:45:07 +09002626 return 0;
2627}
2628
Pavel Roskinfabba042011-07-21 13:36:28 -04002629int ath5k_start(struct ieee80211_hw *hw)
Bob Copeland8a63fac2010-09-17 12:45:07 +09002630{
Pavel Roskinfabba042011-07-21 13:36:28 -04002631 struct ath5k_hw *ah = hw->priv;
Bob Copeland8a63fac2010-09-17 12:45:07 +09002632 struct ath_common *common = ath5k_hw_common(ah);
2633 int ret, i;
2634
Pavel Roskine0d687b2011-07-14 20:21:55 -04002635 mutex_lock(&ah->lock);
Bob Copeland8a63fac2010-09-17 12:45:07 +09002636
Pavel Roskine0d687b2011-07-14 20:21:55 -04002637 ATH5K_DBG(ah, ATH5K_DEBUG_RESET, "mode %d\n", ah->opmode);
Bob Copeland8a63fac2010-09-17 12:45:07 +09002638
2639 /*
2640 * Stop anything previously setup. This is safe
2641 * no matter this is the first time through or not.
2642 */
Pavel Roskine0d687b2011-07-14 20:21:55 -04002643 ath5k_stop_locked(ah);
Bob Copeland8a63fac2010-09-17 12:45:07 +09002644
2645 /*
2646 * The basic interface to setting the hardware in a good
2647 * state is ``reset''. On return the hardware is known to
2648 * be powered up and with interrupts disabled. This must
2649 * be followed by initialization of the appropriate bits
2650 * and then setup of the interrupt mask.
2651 */
Pavel Roskine0d687b2011-07-14 20:21:55 -04002652 ah->curchan = ah->hw->conf.channel;
Nick Kossifidis34ce6442011-11-25 20:40:22 +02002653 ah->imask = AR5K_INT_RXOK
2654 | AR5K_INT_RXERR
2655 | AR5K_INT_RXEOL
2656 | AR5K_INT_RXORN
2657 | AR5K_INT_TXDESC
2658 | AR5K_INT_TXEOL
2659 | AR5K_INT_FATAL
2660 | AR5K_INT_GLOBAL
2661 | AR5K_INT_MIB;
Bob Copeland8a63fac2010-09-17 12:45:07 +09002662
Pavel Roskine0d687b2011-07-14 20:21:55 -04002663 ret = ath5k_reset(ah, NULL, false);
Bob Copeland8a63fac2010-09-17 12:45:07 +09002664 if (ret)
2665 goto done;
2666
Nick Kossifidis84e1e732011-11-25 20:40:27 +02002667 if (!ath5k_modparam_no_hw_rfkill_switch)
2668 ath5k_rfkill_hw_start(ah);
Bob Copeland8a63fac2010-09-17 12:45:07 +09002669
2670 /*
2671 * Reset the key cache since some parts do not reset the
2672 * contents on initial power up or resume from suspend.
2673 */
2674 for (i = 0; i < common->keymax; i++)
2675 ath_hw_keyreset(common, (u16) i);
2676
Nick Kossifidis61cde032010-11-23 21:12:23 +02002677 /* Use higher rates for acks instead of base
2678 * rate */
2679 ah->ah_ack_bitrate_high = true;
Ben Greearb1ae1ed2010-09-30 12:22:58 -07002680
Pavel Roskine0d687b2011-07-14 20:21:55 -04002681 for (i = 0; i < ARRAY_SIZE(ah->bslot); i++)
2682 ah->bslot[i] = NULL;
Ben Greearb1ae1ed2010-09-30 12:22:58 -07002683
Bob Copeland8a63fac2010-09-17 12:45:07 +09002684 ret = 0;
2685done:
2686 mmiowb();
Pavel Roskine0d687b2011-07-14 20:21:55 -04002687 mutex_unlock(&ah->lock);
Bruno Randolf4edd7612010-09-17 11:36:56 +09002688
Pavel Roskine0d687b2011-07-14 20:21:55 -04002689 ieee80211_queue_delayed_work(ah->hw, &ah->tx_complete_work,
Bruno Randolf4edd7612010-09-17 11:36:56 +09002690 msecs_to_jiffies(ATH5K_TX_COMPLETE_POLL_INT));
2691
Bob Copeland8a63fac2010-09-17 12:45:07 +09002692 return ret;
2693}
2694
Pavel Roskine0d687b2011-07-14 20:21:55 -04002695static void ath5k_stop_tasklets(struct ath5k_hw *ah)
Bob Copeland8a63fac2010-09-17 12:45:07 +09002696{
Pavel Roskine0d687b2011-07-14 20:21:55 -04002697 ah->rx_pending = false;
2698 ah->tx_pending = false;
2699 tasklet_kill(&ah->rxtq);
2700 tasklet_kill(&ah->txtq);
Pavel Roskine0d687b2011-07-14 20:21:55 -04002701 tasklet_kill(&ah->beacontq);
2702 tasklet_kill(&ah->ani_tasklet);
Bob Copeland8a63fac2010-09-17 12:45:07 +09002703}
2704
2705/*
2706 * Stop the device, grabbing the top-level lock to protect
2707 * against concurrent entry through ath5k_init (which can happen
2708 * if another thread does a system call and the thread doing the
2709 * stop is preempted).
2710 */
Pavel Roskinfabba042011-07-21 13:36:28 -04002711void ath5k_stop(struct ieee80211_hw *hw)
Bob Copeland8a63fac2010-09-17 12:45:07 +09002712{
Pavel Roskinfabba042011-07-21 13:36:28 -04002713 struct ath5k_hw *ah = hw->priv;
Bob Copeland8a63fac2010-09-17 12:45:07 +09002714 int ret;
2715
Pavel Roskine0d687b2011-07-14 20:21:55 -04002716 mutex_lock(&ah->lock);
2717 ret = ath5k_stop_locked(ah);
2718 if (ret == 0 && !test_bit(ATH_STAT_INVALID, ah->status)) {
Bob Copeland8a63fac2010-09-17 12:45:07 +09002719 /*
2720 * Don't set the card in full sleep mode!
2721 *
2722 * a) When the device is in this state it must be carefully
2723 * woken up or references to registers in the PCI clock
2724 * domain may freeze the bus (and system). This varies
2725 * by chip and is mostly an issue with newer parts
2726 * (madwifi sources mentioned srev >= 0x78) that go to
2727 * sleep more quickly.
2728 *
2729 * b) On older chips full sleep results a weird behaviour
2730 * during wakeup. I tested various cards with srev < 0x78
2731 * and they don't wake up after module reload, a second
2732 * module reload is needed to bring the card up again.
2733 *
2734 * Until we figure out what's going on don't enable
2735 * full chip reset on any chip (this is what Legacy HAL
2736 * and Sam's HAL do anyway). Instead Perform a full reset
2737 * on the device (same as initial state after attach) and
2738 * leave it idle (keep MAC/BB on warm reset) */
Pavel Roskine0d687b2011-07-14 20:21:55 -04002739 ret = ath5k_hw_on_hold(ah);
Bob Copeland8a63fac2010-09-17 12:45:07 +09002740
Pavel Roskine0d687b2011-07-14 20:21:55 -04002741 ATH5K_DBG(ah, ATH5K_DEBUG_RESET,
Bob Copeland8a63fac2010-09-17 12:45:07 +09002742 "putting device to sleep\n");
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002743 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002744
Bob Copeland8a63fac2010-09-17 12:45:07 +09002745 mmiowb();
Pavel Roskine0d687b2011-07-14 20:21:55 -04002746 mutex_unlock(&ah->lock);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002747
Pavel Roskine0d687b2011-07-14 20:21:55 -04002748 ath5k_stop_tasklets(ah);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002749
Pavel Roskine0d687b2011-07-14 20:21:55 -04002750 cancel_delayed_work_sync(&ah->tx_complete_work);
Bruno Randolf4edd7612010-09-17 11:36:56 +09002751
Nick Kossifidis84e1e732011-11-25 20:40:27 +02002752 if (!ath5k_modparam_no_hw_rfkill_switch)
2753 ath5k_rfkill_hw_stop(ah);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002754}
2755
Bob Copeland209d889b2009-05-07 08:09:08 -04002756/*
2757 * Reset the hardware. If chan is not NULL, then also pause rx/tx
2758 * and change to the given channel.
Bob Copeland5faaff72010-07-13 11:32:40 -04002759 *
Pavel Roskine0d687b2011-07-14 20:21:55 -04002760 * This should be called with ah->lock.
Bob Copeland209d889b2009-05-07 08:09:08 -04002761 */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002762static int
Pavel Roskine0d687b2011-07-14 20:21:55 -04002763ath5k_reset(struct ath5k_hw *ah, struct ieee80211_channel *chan,
Nick Kossifidis8aec7af2010-11-23 21:39:28 +02002764 bool skip_pcu)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002765{
Bruno Randolff15a4bb2010-12-16 16:22:20 +09002766 struct ath_common *common = ath5k_hw_common(ah);
Nick Kossifidis344b54b2010-12-03 06:07:13 +02002767 int ret, ani_mode;
Nick Kossifidisa99168e2011-06-02 03:09:48 +03002768 bool fast;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002769
Pavel Roskine0d687b2011-07-14 20:21:55 -04002770 ATH5K_DBG(ah, ATH5K_DEBUG_RESET, "resetting\n");
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002771
Bob Copeland450464d2010-07-13 11:32:41 -04002772 ath5k_hw_set_imr(ah, 0);
Pavel Roskine0d687b2011-07-14 20:21:55 -04002773 synchronize_irq(ah->irq);
2774 ath5k_stop_tasklets(ah);
Bob Copeland450464d2010-07-13 11:32:41 -04002775
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002776 /* Save ani mode and disable ANI during
Nick Kossifidis344b54b2010-12-03 06:07:13 +02002777 * reset. If we don't we might get false
2778 * PHY error interrupts. */
Pavel Roskine0d687b2011-07-14 20:21:55 -04002779 ani_mode = ah->ani_state.ani_mode;
Nick Kossifidis344b54b2010-12-03 06:07:13 +02002780 ath5k_ani_init(ah, ATH5K_ANI_MODE_OFF);
2781
Nick Kossifidis19252ec2010-12-03 06:05:19 +02002782 /* We are going to empty hw queues
2783 * so we should also free any remaining
2784 * tx buffers */
Pavel Roskine0d687b2011-07-14 20:21:55 -04002785 ath5k_drain_tx_buffs(ah);
Bruno Randolf930a7622011-01-19 18:21:13 +09002786 if (chan)
Pavel Roskine0d687b2011-07-14 20:21:55 -04002787 ah->curchan = chan;
Nick Kossifidisa99168e2011-06-02 03:09:48 +03002788
2789 fast = ((chan != NULL) && modparam_fastchanswitch) ? 1 : 0;
2790
Pavel Roskine0d687b2011-07-14 20:21:55 -04002791 ret = ath5k_hw_reset(ah, ah->opmode, ah->curchan, fast, skip_pcu);
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002792 if (ret) {
Pavel Roskine0d687b2011-07-14 20:21:55 -04002793 ATH5K_ERR(ah, "can't reset hardware (%d)\n", ret);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002794 goto err;
2795 }
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002796
Pavel Roskine0d687b2011-07-14 20:21:55 -04002797 ret = ath5k_rx_start(ah);
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002798 if (ret) {
Pavel Roskine0d687b2011-07-14 20:21:55 -04002799 ATH5K_ERR(ah, "can't start recv logic\n");
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002800 goto err;
2801 }
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002802
Nick Kossifidis344b54b2010-12-03 06:07:13 +02002803 ath5k_ani_init(ah, ani_mode);
Bruno Randolf2111ac02010-04-02 18:44:08 +09002804
Nick Kossifidisce169ac2011-11-25 20:40:23 +02002805 /*
2806 * Set calibration intervals
2807 *
2808 * Note: We don't need to run calibration imediately
2809 * since some initial calibration is done on reset
2810 * even for fast channel switching. Also on scanning
2811 * this will get set again and again and it won't get
2812 * executed unless we connect somewhere and spend some
2813 * time on the channel (that's what calibration needs
2814 * anyway to be accurate).
2815 */
2816 ah->ah_cal_next_full = jiffies +
2817 msecs_to_jiffies(ATH5K_TUNE_CALIBRATION_INTERVAL_FULL);
2818 ah->ah_cal_next_ani = jiffies +
2819 msecs_to_jiffies(ATH5K_TUNE_CALIBRATION_INTERVAL_ANI);
2820 ah->ah_cal_next_short = jiffies +
2821 msecs_to_jiffies(ATH5K_TUNE_CALIBRATION_INTERVAL_SHORT);
2822
Bruno Randolf5dcc03f2010-12-02 19:12:31 +09002823 ewma_init(&ah->ah_beacon_rssi_avg, 1024, 8);
Bruno Randolfafe86282010-05-19 10:31:10 +09002824
Bruno Randolff15a4bb2010-12-16 16:22:20 +09002825 /* clear survey data and cycle counters */
Pavel Roskine0d687b2011-07-14 20:21:55 -04002826 memset(&ah->survey, 0, sizeof(ah->survey));
Bob Copelandbb007552010-12-26 12:10:05 -05002827 spin_lock_bh(&common->cc_lock);
Bruno Randolff15a4bb2010-12-16 16:22:20 +09002828 ath_hw_cycle_counters_update(common);
2829 memset(&common->cc_survey, 0, sizeof(common->cc_survey));
2830 memset(&common->cc_ani, 0, sizeof(common->cc_ani));
Bob Copelandbb007552010-12-26 12:10:05 -05002831 spin_unlock_bh(&common->cc_lock);
Bruno Randolff15a4bb2010-12-16 16:22:20 +09002832
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002833 /*
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002834 * Change channels and update the h/w rate map if we're switching;
2835 * e.g. 11a to 11b/g.
2836 *
2837 * We may be doing a reset in response to an ioctl that changes the
2838 * channel so update any state that might change as a result.
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002839 *
2840 * XXX needed?
2841 */
Pavel Roskine0d687b2011-07-14 20:21:55 -04002842/* ath5k_chan_change(ah, c); */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002843
Pavel Roskine0d687b2011-07-14 20:21:55 -04002844 ath5k_beacon_config(ah);
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002845 /* intrs are enabled by ath5k_beacon_config */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002846
Pavel Roskine0d687b2011-07-14 20:21:55 -04002847 ieee80211_wake_queues(ah->hw);
Bruno Randolf397f3852010-05-19 10:30:49 +09002848
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002849 return 0;
2850err:
2851 return ret;
2852}
2853
Bob Copeland5faaff72010-07-13 11:32:40 -04002854static void ath5k_reset_work(struct work_struct *work)
2855{
Pavel Roskine0d687b2011-07-14 20:21:55 -04002856 struct ath5k_hw *ah = container_of(work, struct ath5k_hw,
Bob Copeland5faaff72010-07-13 11:32:40 -04002857 reset_work);
2858
Pavel Roskine0d687b2011-07-14 20:21:55 -04002859 mutex_lock(&ah->lock);
2860 ath5k_reset(ah, NULL, true);
2861 mutex_unlock(&ah->lock);
Bob Copeland5faaff72010-07-13 11:32:40 -04002862}
2863
Pavel Roskin25380d82011-07-07 18:13:42 -04002864static int __devinit
Felix Fietkau132b1c32010-12-02 10:26:56 +01002865ath5k_init(struct ieee80211_hw *hw)
Bob Copeland8a63fac2010-09-17 12:45:07 +09002866{
Felix Fietkau132b1c32010-12-02 10:26:56 +01002867
Pavel Roskine0d687b2011-07-14 20:21:55 -04002868 struct ath5k_hw *ah = hw->priv;
Bob Copeland8a63fac2010-09-17 12:45:07 +09002869 struct ath_regulatory *regulatory = ath5k_hw_regulatory(ah);
Bruno Randolf925e0b02010-09-17 11:36:35 +09002870 struct ath5k_txq *txq;
Bob Copeland8a63fac2010-09-17 12:45:07 +09002871 u8 mac[ETH_ALEN] = {};
2872 int ret;
2873
Bob Copeland8a63fac2010-09-17 12:45:07 +09002874
2875 /*
Bob Copeland8a63fac2010-09-17 12:45:07 +09002876 * Collect the channel list. The 802.11 layer
Pavel Roskin6a2a0e72011-07-09 00:17:51 -04002877 * is responsible for filtering this list based
Bob Copeland8a63fac2010-09-17 12:45:07 +09002878 * on settings like the phy mode and regulatory
2879 * domain restrictions.
2880 */
2881 ret = ath5k_setup_bands(hw);
2882 if (ret) {
Pavel Roskine0d687b2011-07-14 20:21:55 -04002883 ATH5K_ERR(ah, "can't get channels\n");
Bob Copeland8a63fac2010-09-17 12:45:07 +09002884 goto err;
2885 }
2886
Bob Copeland8a63fac2010-09-17 12:45:07 +09002887 /*
2888 * Allocate tx+rx descriptors and populate the lists.
2889 */
Pavel Roskine0d687b2011-07-14 20:21:55 -04002890 ret = ath5k_desc_alloc(ah);
Bob Copeland8a63fac2010-09-17 12:45:07 +09002891 if (ret) {
Pavel Roskine0d687b2011-07-14 20:21:55 -04002892 ATH5K_ERR(ah, "can't allocate descriptors\n");
Bob Copeland8a63fac2010-09-17 12:45:07 +09002893 goto err;
2894 }
2895
2896 /*
2897 * Allocate hardware transmit queues: one queue for
2898 * beacon frames and one data queue for each QoS
2899 * priority. Note that hw functions handle resetting
2900 * these queues at the needed time.
2901 */
2902 ret = ath5k_beaconq_setup(ah);
2903 if (ret < 0) {
Pavel Roskine0d687b2011-07-14 20:21:55 -04002904 ATH5K_ERR(ah, "can't setup a beacon xmit queue\n");
Bob Copeland8a63fac2010-09-17 12:45:07 +09002905 goto err_desc;
2906 }
Pavel Roskine0d687b2011-07-14 20:21:55 -04002907 ah->bhalq = ret;
2908 ah->cabq = ath5k_txq_setup(ah, AR5K_TX_QUEUE_CAB, 0);
2909 if (IS_ERR(ah->cabq)) {
2910 ATH5K_ERR(ah, "can't setup cab queue\n");
2911 ret = PTR_ERR(ah->cabq);
Bob Copeland8a63fac2010-09-17 12:45:07 +09002912 goto err_bhal;
2913 }
2914
Bruno Randolf22d8d9f2010-12-07 11:08:12 +09002915 /* 5211 and 5212 usually support 10 queues but we better rely on the
2916 * capability information */
2917 if (ah->ah_capabilities.cap_queues.q_tx_num >= 6) {
2918 /* This order matches mac80211's queue priority, so we can
2919 * directly use the mac80211 queue number without any mapping */
Pavel Roskine0d687b2011-07-14 20:21:55 -04002920 txq = ath5k_txq_setup(ah, AR5K_TX_QUEUE_DATA, AR5K_WME_AC_VO);
Bruno Randolf22d8d9f2010-12-07 11:08:12 +09002921 if (IS_ERR(txq)) {
Pavel Roskine0d687b2011-07-14 20:21:55 -04002922 ATH5K_ERR(ah, "can't setup xmit queue\n");
Bruno Randolf22d8d9f2010-12-07 11:08:12 +09002923 ret = PTR_ERR(txq);
2924 goto err_queues;
2925 }
Pavel Roskine0d687b2011-07-14 20:21:55 -04002926 txq = ath5k_txq_setup(ah, AR5K_TX_QUEUE_DATA, AR5K_WME_AC_VI);
Bruno Randolf22d8d9f2010-12-07 11:08:12 +09002927 if (IS_ERR(txq)) {
Pavel Roskine0d687b2011-07-14 20:21:55 -04002928 ATH5K_ERR(ah, "can't setup xmit queue\n");
Bruno Randolf22d8d9f2010-12-07 11:08:12 +09002929 ret = PTR_ERR(txq);
2930 goto err_queues;
2931 }
Pavel Roskine0d687b2011-07-14 20:21:55 -04002932 txq = ath5k_txq_setup(ah, AR5K_TX_QUEUE_DATA, AR5K_WME_AC_BE);
Bruno Randolf22d8d9f2010-12-07 11:08:12 +09002933 if (IS_ERR(txq)) {
Pavel Roskine0d687b2011-07-14 20:21:55 -04002934 ATH5K_ERR(ah, "can't setup xmit queue\n");
Bruno Randolf22d8d9f2010-12-07 11:08:12 +09002935 ret = PTR_ERR(txq);
2936 goto err_queues;
2937 }
Pavel Roskine0d687b2011-07-14 20:21:55 -04002938 txq = ath5k_txq_setup(ah, AR5K_TX_QUEUE_DATA, AR5K_WME_AC_BK);
Bruno Randolf22d8d9f2010-12-07 11:08:12 +09002939 if (IS_ERR(txq)) {
Pavel Roskine0d687b2011-07-14 20:21:55 -04002940 ATH5K_ERR(ah, "can't setup xmit queue\n");
Bruno Randolf22d8d9f2010-12-07 11:08:12 +09002941 ret = PTR_ERR(txq);
2942 goto err_queues;
2943 }
2944 hw->queues = 4;
2945 } else {
2946 /* older hardware (5210) can only support one data queue */
Pavel Roskine0d687b2011-07-14 20:21:55 -04002947 txq = ath5k_txq_setup(ah, AR5K_TX_QUEUE_DATA, AR5K_WME_AC_BE);
Bruno Randolf22d8d9f2010-12-07 11:08:12 +09002948 if (IS_ERR(txq)) {
Pavel Roskine0d687b2011-07-14 20:21:55 -04002949 ATH5K_ERR(ah, "can't setup xmit queue\n");
Bruno Randolf22d8d9f2010-12-07 11:08:12 +09002950 ret = PTR_ERR(txq);
2951 goto err_queues;
2952 }
2953 hw->queues = 1;
Bob Copeland8a63fac2010-09-17 12:45:07 +09002954 }
2955
Pavel Roskine0d687b2011-07-14 20:21:55 -04002956 tasklet_init(&ah->rxtq, ath5k_tasklet_rx, (unsigned long)ah);
2957 tasklet_init(&ah->txtq, ath5k_tasklet_tx, (unsigned long)ah);
Pavel Roskine0d687b2011-07-14 20:21:55 -04002958 tasklet_init(&ah->beacontq, ath5k_tasklet_beacon, (unsigned long)ah);
2959 tasklet_init(&ah->ani_tasklet, ath5k_tasklet_ani, (unsigned long)ah);
Bob Copeland8a63fac2010-09-17 12:45:07 +09002960
Pavel Roskine0d687b2011-07-14 20:21:55 -04002961 INIT_WORK(&ah->reset_work, ath5k_reset_work);
Nick Kossifidisce169ac2011-11-25 20:40:23 +02002962 INIT_WORK(&ah->calib_work, ath5k_calibrate_work);
Pavel Roskine0d687b2011-07-14 20:21:55 -04002963 INIT_DELAYED_WORK(&ah->tx_complete_work, ath5k_tx_complete_poll_work);
Bob Copeland8a63fac2010-09-17 12:45:07 +09002964
Felix Fietkaufa9bfd62011-04-13 21:56:44 +02002965 ret = ath5k_hw_common(ah)->bus_ops->eeprom_read_mac(ah, mac);
Bob Copeland8a63fac2010-09-17 12:45:07 +09002966 if (ret) {
Pavel Roskine0d687b2011-07-14 20:21:55 -04002967 ATH5K_ERR(ah, "unable to read address from EEPROM\n");
Bob Copeland8a63fac2010-09-17 12:45:07 +09002968 goto err_queues;
2969 }
2970
2971 SET_IEEE80211_PERM_ADDR(hw, mac);
2972 /* All MAC address bits matter for ACKs */
Pavel Roskine0d687b2011-07-14 20:21:55 -04002973 ath5k_update_bssid_mask_and_opmode(ah, NULL);
Bob Copeland8a63fac2010-09-17 12:45:07 +09002974
2975 regulatory->current_rd = ah->ah_capabilities.cap_eeprom.ee_regdomain;
2976 ret = ath_regd_init(regulatory, hw->wiphy, ath5k_reg_notifier);
2977 if (ret) {
Pavel Roskine0d687b2011-07-14 20:21:55 -04002978 ATH5K_ERR(ah, "can't initialize regulatory system\n");
Bob Copeland8a63fac2010-09-17 12:45:07 +09002979 goto err_queues;
2980 }
2981
2982 ret = ieee80211_register_hw(hw);
2983 if (ret) {
Pavel Roskine0d687b2011-07-14 20:21:55 -04002984 ATH5K_ERR(ah, "can't register ieee80211 hw\n");
Bob Copeland8a63fac2010-09-17 12:45:07 +09002985 goto err_queues;
2986 }
2987
2988 if (!ath_is_world_regd(regulatory))
2989 regulatory_hint(hw->wiphy, regulatory->alpha2);
2990
Pavel Roskine0d687b2011-07-14 20:21:55 -04002991 ath5k_init_leds(ah);
Bob Copeland8a63fac2010-09-17 12:45:07 +09002992
Pavel Roskine0d687b2011-07-14 20:21:55 -04002993 ath5k_sysfs_register(ah);
Bob Copeland8a63fac2010-09-17 12:45:07 +09002994
2995 return 0;
2996err_queues:
Pavel Roskine0d687b2011-07-14 20:21:55 -04002997 ath5k_txq_release(ah);
Bob Copeland8a63fac2010-09-17 12:45:07 +09002998err_bhal:
Pavel Roskine0d687b2011-07-14 20:21:55 -04002999 ath5k_hw_release_tx_queue(ah, ah->bhalq);
Bob Copeland8a63fac2010-09-17 12:45:07 +09003000err_desc:
Pavel Roskine0d687b2011-07-14 20:21:55 -04003001 ath5k_desc_free(ah);
Bob Copeland8a63fac2010-09-17 12:45:07 +09003002err:
3003 return ret;
3004}
3005
Felix Fietkau132b1c32010-12-02 10:26:56 +01003006void
Pavel Roskinbb1f3ad2011-07-26 22:27:05 -04003007ath5k_deinit_ah(struct ath5k_hw *ah)
Bob Copeland8a63fac2010-09-17 12:45:07 +09003008{
Pavel Roskine0d687b2011-07-14 20:21:55 -04003009 struct ieee80211_hw *hw = ah->hw;
Bob Copeland8a63fac2010-09-17 12:45:07 +09003010
3011 /*
3012 * NB: the order of these is important:
3013 * o call the 802.11 layer before detaching ath5k_hw to
3014 * ensure callbacks into the driver to delete global
3015 * key cache entries can be handled
3016 * o reclaim the tx queue data structures after calling
3017 * the 802.11 layer as we'll get called back to reclaim
3018 * node state and potentially want to use them
3019 * o to cleanup the tx queues the hal is called, so detach
3020 * it last
3021 * XXX: ??? detach ath5k_hw ???
3022 * Other than that, it's straightforward...
3023 */
3024 ieee80211_unregister_hw(hw);
Pavel Roskine0d687b2011-07-14 20:21:55 -04003025 ath5k_desc_free(ah);
3026 ath5k_txq_release(ah);
3027 ath5k_hw_release_tx_queue(ah, ah->bhalq);
3028 ath5k_unregister_leds(ah);
Bob Copeland8a63fac2010-09-17 12:45:07 +09003029
Pavel Roskine0d687b2011-07-14 20:21:55 -04003030 ath5k_sysfs_unregister(ah);
Bob Copeland8a63fac2010-09-17 12:45:07 +09003031 /*
3032 * NB: can't reclaim these until after ieee80211_ifdetach
3033 * returns because we'll get called back to reclaim node
3034 * state and potentially want to use them.
3035 */
Pavel Roskine0d687b2011-07-14 20:21:55 -04003036 ath5k_hw_deinit(ah);
3037 free_irq(ah->irq, ah);
Bob Copeland8a63fac2010-09-17 12:45:07 +09003038}
3039
Bruno Randolfcd2c5482010-12-22 19:20:32 +09003040bool
Pavel Roskine0d687b2011-07-14 20:21:55 -04003041ath5k_any_vif_assoc(struct ath5k_hw *ah)
Ben Greearb1ae1ed2010-09-30 12:22:58 -07003042{
Ben Greeare4b0b322011-03-03 14:39:05 -08003043 struct ath5k_vif_iter_data iter_data;
Ben Greearb1ae1ed2010-09-30 12:22:58 -07003044 iter_data.hw_macaddr = NULL;
3045 iter_data.any_assoc = false;
3046 iter_data.need_set_hw_addr = false;
3047 iter_data.found_active = true;
3048
Johannes Berg8b2c9822012-11-06 20:23:30 +01003049 ieee80211_iterate_active_interfaces_atomic(
3050 ah->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
3051 ath5k_vif_iter, &iter_data);
Ben Greearb1ae1ed2010-09-30 12:22:58 -07003052 return iter_data.any_assoc;
3053}
3054
Bruno Randolfcd2c5482010-12-22 19:20:32 +09003055void
Pavel Roskinf5cbc8b2011-06-15 18:03:22 -04003056ath5k_set_beacon_filter(struct ieee80211_hw *hw, bool enable)
Martin Xu02969b32008-11-24 10:49:27 +08003057{
Pavel Roskine0d687b2011-07-14 20:21:55 -04003058 struct ath5k_hw *ah = hw->priv;
Martin Xu02969b32008-11-24 10:49:27 +08003059 u32 rfilt;
3060 rfilt = ath5k_hw_get_rx_filter(ah);
3061 if (enable)
3062 rfilt |= AR5K_RX_FILTER_BEACON;
3063 else
3064 rfilt &= ~AR5K_RX_FILTER_BEACON;
3065 ath5k_hw_set_rx_filter(ah, rfilt);
Pavel Roskine0d687b2011-07-14 20:21:55 -04003066 ah->filter_flags = rfilt;
Martin Xu02969b32008-11-24 10:49:27 +08003067}
Joe Perches227842d2012-03-18 17:30:53 -07003068
3069void _ath5k_printk(const struct ath5k_hw *ah, const char *level,
3070 const char *fmt, ...)
3071{
3072 struct va_format vaf;
3073 va_list args;
3074
3075 va_start(args, fmt);
3076
3077 vaf.fmt = fmt;
3078 vaf.va = &args;
3079
3080 if (ah && ah->hw)
3081 printk("%s" pr_fmt("%s: %pV"),
3082 level, wiphy_name(ah->hw->wiphy), &vaf);
3083 else
3084 printk("%s" pr_fmt("%pV"), level, &vaf);
3085
3086 va_end(args);
3087}