blob: 33cd1bc4a71c48be26f5e09b4d91da6da676d2cf [file] [log] [blame]
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001/*-
2 * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
3 * Copyright (c) 2004-2005 Atheros Communications, Inc.
4 * Copyright (c) 2006 Devicescape Software, Inc.
5 * Copyright (c) 2007 Jiri Slaby <jirislaby@gmail.com>
6 * Copyright (c) 2007 Luis R. Rodriguez <mcgrof@winlab.rutgers.edu>
7 *
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer,
15 * without modification.
16 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
17 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
18 * redistribution must be conditioned upon including a substantially
19 * similar Disclaimer requirement for further binary redistribution.
20 * 3. Neither the names of the above-listed copyright holders nor the names
21 * of any contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
23 *
24 * Alternatively, this software may be distributed under the terms of the
25 * GNU General Public License ("GPL") version 2 as published by the Free
26 * Software Foundation.
27 *
28 * NO WARRANTY
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
32 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
33 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
34 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
37 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
39 * THE POSSIBILITY OF SUCH DAMAGES.
40 *
41 */
42
Jiri Slabyfa1c1142007-08-12 17:33:16 +020043#include <linux/module.h>
44#include <linux/delay.h>
Jiri Slaby274c7c32008-07-15 17:44:20 +020045#include <linux/hardirq.h>
Jiri Slabyfa1c1142007-08-12 17:33:16 +020046#include <linux/if.h>
Jiri Slaby274c7c32008-07-15 17:44:20 +020047#include <linux/io.h>
Jiri Slabyfa1c1142007-08-12 17:33:16 +020048#include <linux/netdevice.h>
49#include <linux/cache.h>
50#include <linux/pci.h>
Maxim Levitsky6ccf15a2010-08-13 11:27:28 -040051#include <linux/pci-aspm.h>
Jiri Slabyfa1c1142007-08-12 17:33:16 +020052#include <linux/ethtool.h>
53#include <linux/uaccess.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090054#include <linux/slab.h>
Ben Greearb1ae1ed2010-09-30 12:22:58 -070055#include <linux/etherdevice.h>
Jiri Slabyfa1c1142007-08-12 17:33:16 +020056
57#include <net/ieee80211_radiotap.h>
58
59#include <asm/unaligned.h>
60
61#include "base.h"
62#include "reg.h"
63#include "debug.h"
Bruno Randolf2111ac02010-04-02 18:44:08 +090064#include "ani.h"
Ben Greear62c58fb2010-10-08 12:01:15 -070065#include "../debug.h"
Jiri Slabyfa1c1142007-08-12 17:33:16 +020066
Bob Copeland9ad9a262008-10-29 08:30:54 -040067static int modparam_nohwcrypt;
Bob Copeland46802a42009-04-15 07:57:34 -040068module_param_named(nohwcrypt, modparam_nohwcrypt, bool, S_IRUGO);
Bob Copeland9ad9a262008-10-29 08:30:54 -040069MODULE_PARM_DESC(nohwcrypt, "Disable hardware encryption.");
Jiri Slabyfa1c1142007-08-12 17:33:16 +020070
Bob Copeland42639fc2009-03-30 08:05:29 -040071static int modparam_all_channels;
Bob Copeland46802a42009-04-15 07:57:34 -040072module_param_named(all_channels, modparam_all_channels, bool, S_IRUGO);
Bob Copeland42639fc2009-03-30 08:05:29 -040073MODULE_PARM_DESC(all_channels, "Expose all channels the device can use.");
74
Jiri Slabyfa1c1142007-08-12 17:33:16 +020075/* Module info */
76MODULE_AUTHOR("Jiri Slaby");
77MODULE_AUTHOR("Nick Kossifidis");
78MODULE_DESCRIPTION("Support for 5xxx series of Atheros 802.11 wireless LAN cards.");
79MODULE_SUPPORTED_DEVICE("Atheros 5xxx WLAN cards");
80MODULE_LICENSE("Dual BSD/GPL");
Nick Kossifidis0d5f0312008-09-29 01:27:27 +030081MODULE_VERSION("0.6.0 (EXPERIMENTAL)");
Jiri Slabyfa1c1142007-08-12 17:33:16 +020082
Nick Kossifidis8aec7af2010-11-23 21:39:28 +020083static int ath5k_reset(struct ath5k_softc *sc, struct ieee80211_channel *chan,
84 bool skip_pcu);
Bob Copeland8a63fac2010-09-17 12:45:07 +090085static int ath5k_beacon_update(struct ieee80211_hw *hw,
86 struct ieee80211_vif *vif);
87static void ath5k_beacon_update_timers(struct ath5k_softc *sc, u64 bc_tsf);
Jiri Slabyfa1c1142007-08-12 17:33:16 +020088
89/* Known PCI ids */
Alexey Dobriyana3aa1882010-01-07 11:58:11 +000090static DEFINE_PCI_DEVICE_TABLE(ath5k_pci_id_table) = {
Pavel Roskin97a81f52009-08-26 22:30:09 -040091 { PCI_VDEVICE(ATHEROS, 0x0207) }, /* 5210 early */
92 { PCI_VDEVICE(ATHEROS, 0x0007) }, /* 5210 */
93 { PCI_VDEVICE(ATHEROS, 0x0011) }, /* 5311 - this is on AHB bus !*/
94 { PCI_VDEVICE(ATHEROS, 0x0012) }, /* 5211 */
95 { PCI_VDEVICE(ATHEROS, 0x0013) }, /* 5212 */
96 { PCI_VDEVICE(3COM_2, 0x0013) }, /* 3com 5212 */
97 { PCI_VDEVICE(3COM, 0x0013) }, /* 3com 3CRDAG675 5212 */
98 { PCI_VDEVICE(ATHEROS, 0x1014) }, /* IBM minipci 5212 */
99 { PCI_VDEVICE(ATHEROS, 0x0014) }, /* 5212 combatible */
100 { PCI_VDEVICE(ATHEROS, 0x0015) }, /* 5212 combatible */
101 { PCI_VDEVICE(ATHEROS, 0x0016) }, /* 5212 combatible */
102 { PCI_VDEVICE(ATHEROS, 0x0017) }, /* 5212 combatible */
103 { PCI_VDEVICE(ATHEROS, 0x0018) }, /* 5212 combatible */
104 { PCI_VDEVICE(ATHEROS, 0x0019) }, /* 5212 combatible */
105 { PCI_VDEVICE(ATHEROS, 0x001a) }, /* 2413 Griffin-lite */
106 { PCI_VDEVICE(ATHEROS, 0x001b) }, /* 5413 Eagle */
107 { PCI_VDEVICE(ATHEROS, 0x001c) }, /* PCI-E cards */
108 { PCI_VDEVICE(ATHEROS, 0x001d) }, /* 2417 Nala */
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200109 { 0 }
110};
111MODULE_DEVICE_TABLE(pci, ath5k_pci_id_table);
112
113/* Known SREVs */
Jiri Slaby2c91108c2009-03-07 10:26:41 +0100114static const struct ath5k_srev_name srev_names[] = {
Nick Kossifidis1bef0162008-09-29 02:09:09 +0300115 { "5210", AR5K_VERSION_MAC, AR5K_SREV_AR5210 },
116 { "5311", AR5K_VERSION_MAC, AR5K_SREV_AR5311 },
117 { "5311A", AR5K_VERSION_MAC, AR5K_SREV_AR5311A },
118 { "5311B", AR5K_VERSION_MAC, AR5K_SREV_AR5311B },
119 { "5211", AR5K_VERSION_MAC, AR5K_SREV_AR5211 },
120 { "5212", AR5K_VERSION_MAC, AR5K_SREV_AR5212 },
121 { "5213", AR5K_VERSION_MAC, AR5K_SREV_AR5213 },
122 { "5213A", AR5K_VERSION_MAC, AR5K_SREV_AR5213A },
123 { "2413", AR5K_VERSION_MAC, AR5K_SREV_AR2413 },
124 { "2414", AR5K_VERSION_MAC, AR5K_SREV_AR2414 },
125 { "5424", AR5K_VERSION_MAC, AR5K_SREV_AR5424 },
126 { "5413", AR5K_VERSION_MAC, AR5K_SREV_AR5413 },
127 { "5414", AR5K_VERSION_MAC, AR5K_SREV_AR5414 },
128 { "2415", AR5K_VERSION_MAC, AR5K_SREV_AR2415 },
129 { "5416", AR5K_VERSION_MAC, AR5K_SREV_AR5416 },
130 { "5418", AR5K_VERSION_MAC, AR5K_SREV_AR5418 },
131 { "2425", AR5K_VERSION_MAC, AR5K_SREV_AR2425 },
132 { "2417", AR5K_VERSION_MAC, AR5K_SREV_AR2417 },
133 { "xxxxx", AR5K_VERSION_MAC, AR5K_SREV_UNKNOWN },
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200134 { "5110", AR5K_VERSION_RAD, AR5K_SREV_RAD_5110 },
135 { "5111", AR5K_VERSION_RAD, AR5K_SREV_RAD_5111 },
Nick Kossifidis1bef0162008-09-29 02:09:09 +0300136 { "5111A", AR5K_VERSION_RAD, AR5K_SREV_RAD_5111A },
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200137 { "2111", AR5K_VERSION_RAD, AR5K_SREV_RAD_2111 },
138 { "5112", AR5K_VERSION_RAD, AR5K_SREV_RAD_5112 },
139 { "5112A", AR5K_VERSION_RAD, AR5K_SREV_RAD_5112A },
Nick Kossifidis1bef0162008-09-29 02:09:09 +0300140 { "5112B", AR5K_VERSION_RAD, AR5K_SREV_RAD_5112B },
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200141 { "2112", AR5K_VERSION_RAD, AR5K_SREV_RAD_2112 },
142 { "2112A", AR5K_VERSION_RAD, AR5K_SREV_RAD_2112A },
Nick Kossifidis1bef0162008-09-29 02:09:09 +0300143 { "2112B", AR5K_VERSION_RAD, AR5K_SREV_RAD_2112B },
144 { "2413", AR5K_VERSION_RAD, AR5K_SREV_RAD_2413 },
145 { "5413", AR5K_VERSION_RAD, AR5K_SREV_RAD_5413 },
146 { "2316", AR5K_VERSION_RAD, AR5K_SREV_RAD_2316 },
147 { "2317", AR5K_VERSION_RAD, AR5K_SREV_RAD_2317 },
148 { "5424", AR5K_VERSION_RAD, AR5K_SREV_RAD_5424 },
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200149 { "5133", AR5K_VERSION_RAD, AR5K_SREV_RAD_5133 },
150 { "xxxxx", AR5K_VERSION_RAD, AR5K_SREV_UNKNOWN },
151};
152
Jiri Slaby2c91108c2009-03-07 10:26:41 +0100153static const struct ieee80211_rate ath5k_rates[] = {
Bruno Randolf63266a62008-07-30 17:12:58 +0200154 { .bitrate = 10,
155 .hw_value = ATH5K_RATE_CODE_1M, },
156 { .bitrate = 20,
157 .hw_value = ATH5K_RATE_CODE_2M,
158 .hw_value_short = ATH5K_RATE_CODE_2M | AR5K_SET_SHORT_PREAMBLE,
159 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
160 { .bitrate = 55,
161 .hw_value = ATH5K_RATE_CODE_5_5M,
162 .hw_value_short = ATH5K_RATE_CODE_5_5M | AR5K_SET_SHORT_PREAMBLE,
163 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
164 { .bitrate = 110,
165 .hw_value = ATH5K_RATE_CODE_11M,
166 .hw_value_short = ATH5K_RATE_CODE_11M | AR5K_SET_SHORT_PREAMBLE,
167 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
168 { .bitrate = 60,
169 .hw_value = ATH5K_RATE_CODE_6M,
170 .flags = 0 },
171 { .bitrate = 90,
172 .hw_value = ATH5K_RATE_CODE_9M,
173 .flags = 0 },
174 { .bitrate = 120,
175 .hw_value = ATH5K_RATE_CODE_12M,
176 .flags = 0 },
177 { .bitrate = 180,
178 .hw_value = ATH5K_RATE_CODE_18M,
179 .flags = 0 },
180 { .bitrate = 240,
181 .hw_value = ATH5K_RATE_CODE_24M,
182 .flags = 0 },
183 { .bitrate = 360,
184 .hw_value = ATH5K_RATE_CODE_36M,
185 .flags = 0 },
186 { .bitrate = 480,
187 .hw_value = ATH5K_RATE_CODE_48M,
188 .flags = 0 },
189 { .bitrate = 540,
190 .hw_value = ATH5K_RATE_CODE_54M,
191 .flags = 0 },
192 /* XR missing */
193};
194
Bruno Randolf9e4e43f2010-06-16 19:11:17 +0900195static inline void ath5k_txbuf_free_skb(struct ath5k_softc *sc,
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200196 struct ath5k_buf *bf)
197{
198 BUG_ON(!bf);
199 if (!bf->skb)
200 return;
201 pci_unmap_single(sc->pdev, bf->skbaddr, bf->skb->len,
202 PCI_DMA_TODEVICE);
Jiri Slaby00482972008-08-18 21:45:27 +0200203 dev_kfree_skb_any(bf->skb);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200204 bf->skb = NULL;
Bruno Randolf39d63f22010-06-16 19:11:41 +0900205 bf->skbaddr = 0;
206 bf->desc->ds_data = 0;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200207}
208
Bruno Randolf9e4e43f2010-06-16 19:11:17 +0900209static inline void ath5k_rxbuf_free_skb(struct ath5k_softc *sc,
Felix Fietkaua6c8d372009-01-30 01:36:48 +0100210 struct ath5k_buf *bf)
211{
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -0800212 struct ath5k_hw *ah = sc->ah;
213 struct ath_common *common = ath5k_hw_common(ah);
214
Felix Fietkaua6c8d372009-01-30 01:36:48 +0100215 BUG_ON(!bf);
216 if (!bf->skb)
217 return;
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -0800218 pci_unmap_single(sc->pdev, bf->skbaddr, common->rx_bufsize,
Felix Fietkaua6c8d372009-01-30 01:36:48 +0100219 PCI_DMA_FROMDEVICE);
220 dev_kfree_skb_any(bf->skb);
221 bf->skb = NULL;
Bruno Randolf39d63f22010-06-16 19:11:41 +0900222 bf->skbaddr = 0;
223 bf->desc->ds_data = 0;
Felix Fietkaua6c8d372009-01-30 01:36:48 +0100224}
225
226
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200227static inline u64 ath5k_extend_tsf(struct ath5k_hw *ah, u32 rstamp)
228{
229 u64 tsf = ath5k_hw_get_tsf64(ah);
230
231 if ((tsf & 0x7fff) < rstamp)
232 tsf -= 0x8000;
233
234 return (tsf & ~0x7fff) | rstamp;
235}
236
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200237static const char *
238ath5k_chip_name(enum ath5k_srev_type type, u_int16_t val)
239{
240 const char *name = "xxxxx";
241 unsigned int i;
242
243 for (i = 0; i < ARRAY_SIZE(srev_names); i++) {
244 if (srev_names[i].sr_type != type)
245 continue;
Nick Kossifidis75d0edb2008-09-29 01:24:44 +0300246
247 if ((val & 0xf0) == srev_names[i].sr_val)
248 name = srev_names[i].sr_name;
249
250 if ((val & 0xff) == srev_names[i].sr_val) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200251 name = srev_names[i].sr_name;
252 break;
253 }
254 }
255
256 return name;
257}
Luis R. Rodrigueze5aa8472009-09-10 16:55:11 -0700258static unsigned int ath5k_ioread32(void *hw_priv, u32 reg_offset)
259{
260 struct ath5k_hw *ah = (struct ath5k_hw *) hw_priv;
261 return ath5k_hw_reg_read(ah, reg_offset);
262}
263
264static void ath5k_iowrite32(void *hw_priv, u32 val, u32 reg_offset)
265{
266 struct ath5k_hw *ah = (struct ath5k_hw *) hw_priv;
267 ath5k_hw_reg_write(ah, val, reg_offset);
268}
269
270static const struct ath_ops ath5k_common_ops = {
271 .read = ath5k_ioread32,
272 .write = ath5k_iowrite32,
273};
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200274
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200275/***********************\
276* Driver Initialization *
277\***********************/
278
Bob Copelandf769c362009-03-30 22:30:31 -0400279static int ath5k_reg_notifier(struct wiphy *wiphy, struct regulatory_request *request)
280{
281 struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
282 struct ath5k_softc *sc = hw->priv;
Luis R. Rodriguezdb719712009-09-10 11:20:57 -0700283 struct ath_regulatory *regulatory = ath5k_hw_regulatory(sc->ah);
Bob Copelandf769c362009-03-30 22:30:31 -0400284
Luis R. Rodriguez608b88c2009-08-17 18:07:23 -0700285 return ath_reg_notifier_apply(wiphy, request, regulatory);
Bob Copelandf769c362009-03-30 22:30:31 -0400286}
287
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200288/********************\
289* Channel/mode setup *
290\********************/
291
292/*
293 * Convert IEEE channel number to MHz frequency.
294 */
295static inline short
296ath5k_ieee2mhz(short chan)
297{
298 if (chan <= 14 || chan >= 27)
299 return ieee80211chan2mhz(chan);
300 else
301 return 2212 + chan * 20;
302}
303
Bob Copeland42639fc2009-03-30 08:05:29 -0400304/*
305 * Returns true for the channel numbers used without all_channels modparam.
306 */
307static bool ath5k_is_standard_channel(short chan)
308{
309 return ((chan <= 14) ||
310 /* UNII 1,2 */
311 ((chan & 3) == 0 && chan >= 36 && chan <= 64) ||
312 /* midband */
313 ((chan & 3) == 0 && chan >= 100 && chan <= 140) ||
314 /* UNII-3 */
315 ((chan & 3) == 1 && chan >= 149 && chan <= 165));
316}
317
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200318static unsigned int
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200319ath5k_copy_channels(struct ath5k_hw *ah,
320 struct ieee80211_channel *channels,
321 unsigned int mode,
322 unsigned int max)
323{
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500324 unsigned int i, count, size, chfreq, freq, ch;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200325
326 if (!test_bit(mode, ah->ah_modes))
327 return 0;
328
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200329 switch (mode) {
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500330 case AR5K_MODE_11A:
331 case AR5K_MODE_11A_TURBO:
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200332 /* 1..220, but 2GHz frequencies are filtered by check_channel */
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500333 size = 220 ;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200334 chfreq = CHANNEL_5GHZ;
335 break;
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500336 case AR5K_MODE_11B:
337 case AR5K_MODE_11G:
338 case AR5K_MODE_11G_TURBO:
339 size = 26;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200340 chfreq = CHANNEL_2GHZ;
341 break;
342 default:
343 ATH5K_WARN(ah->ah_sc, "bad mode, not copying channels\n");
344 return 0;
345 }
346
347 for (i = 0, count = 0; i < size && max > 0; i++) {
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500348 ch = i + 1 ;
349 freq = ath5k_ieee2mhz(ch);
350
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200351 /* Check if channel is supported by the chipset */
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500352 if (!ath5k_channel_ok(ah, freq, chfreq))
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200353 continue;
354
Bob Copeland42639fc2009-03-30 08:05:29 -0400355 if (!modparam_all_channels && !ath5k_is_standard_channel(ch))
356 continue;
357
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500358 /* Write channel info and increment counter */
359 channels[count].center_freq = freq;
Luis R. Rodrigueza3f4b912008-02-03 21:52:10 -0500360 channels[count].band = (chfreq == CHANNEL_2GHZ) ?
361 IEEE80211_BAND_2GHZ : IEEE80211_BAND_5GHZ;
Luis R. Rodriguez400ec452008-02-03 21:51:49 -0500362 switch (mode) {
363 case AR5K_MODE_11A:
364 case AR5K_MODE_11G:
365 channels[count].hw_value = chfreq | CHANNEL_OFDM;
366 break;
367 case AR5K_MODE_11A_TURBO:
368 case AR5K_MODE_11G_TURBO:
369 channels[count].hw_value = chfreq |
370 CHANNEL_OFDM | CHANNEL_TURBO;
371 break;
372 case AR5K_MODE_11B:
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500373 channels[count].hw_value = CHANNEL_B;
374 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200375
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200376 count++;
377 max--;
378 }
379
380 return count;
381}
382
Bruno Randolf63266a62008-07-30 17:12:58 +0200383static void
384ath5k_setup_rate_idx(struct ath5k_softc *sc, struct ieee80211_supported_band *b)
385{
386 u8 i;
387
388 for (i = 0; i < AR5K_MAX_RATES; i++)
389 sc->rate_idx[b->band][i] = -1;
390
391 for (i = 0; i < b->n_bitrates; i++) {
392 sc->rate_idx[b->band][b->bitrates[i].hw_value] = i;
393 if (b->bitrates[i].hw_value_short)
394 sc->rate_idx[b->band][b->bitrates[i].hw_value_short] = i;
395 }
396}
397
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200398static int
Bruno Randolf63266a62008-07-30 17:12:58 +0200399ath5k_setup_bands(struct ieee80211_hw *hw)
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200400{
401 struct ath5k_softc *sc = hw->priv;
402 struct ath5k_hw *ah = sc->ah;
Bruno Randolf63266a62008-07-30 17:12:58 +0200403 struct ieee80211_supported_band *sband;
404 int max_c, count_c = 0;
405 int i;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200406
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500407 BUILD_BUG_ON(ARRAY_SIZE(sc->sbands) < IEEE80211_NUM_BANDS);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200408 max_c = ARRAY_SIZE(sc->channels);
409
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500410 /* 2GHz band */
Bruno Randolf63266a62008-07-30 17:12:58 +0200411 sband = &sc->sbands[IEEE80211_BAND_2GHZ];
412 sband->band = IEEE80211_BAND_2GHZ;
413 sband->bitrates = &sc->rates[IEEE80211_BAND_2GHZ][0];
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200414
Bruno Randolf63266a62008-07-30 17:12:58 +0200415 if (test_bit(AR5K_MODE_11G, sc->ah->ah_capabilities.cap_mode)) {
416 /* G mode */
417 memcpy(sband->bitrates, &ath5k_rates[0],
418 sizeof(struct ieee80211_rate) * 12);
419 sband->n_bitrates = 12;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200420
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500421 sband->channels = sc->channels;
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500422 sband->n_channels = ath5k_copy_channels(ah, sband->channels,
Bruno Randolf63266a62008-07-30 17:12:58 +0200423 AR5K_MODE_11G, max_c);
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500424
425 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = sband;
Bruno Randolf63266a62008-07-30 17:12:58 +0200426 count_c = sband->n_channels;
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500427 max_c -= count_c;
Bruno Randolf63266a62008-07-30 17:12:58 +0200428 } else if (test_bit(AR5K_MODE_11B, sc->ah->ah_capabilities.cap_mode)) {
429 /* B mode */
430 memcpy(sband->bitrates, &ath5k_rates[0],
431 sizeof(struct ieee80211_rate) * 4);
432 sband->n_bitrates = 4;
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500433
Bruno Randolf63266a62008-07-30 17:12:58 +0200434 /* 5211 only supports B rates and uses 4bit rate codes
435 * (e.g normally we have 0x1B for 1M, but on 5211 we have 0x0B)
436 * fix them up here:
437 */
438 if (ah->ah_version == AR5K_AR5211) {
439 for (i = 0; i < 4; i++) {
440 sband->bitrates[i].hw_value =
441 sband->bitrates[i].hw_value & 0xF;
442 sband->bitrates[i].hw_value_short =
443 sband->bitrates[i].hw_value_short & 0xF;
444 }
445 }
446
447 sband->channels = sc->channels;
448 sband->n_channels = ath5k_copy_channels(ah, sband->channels,
449 AR5K_MODE_11B, max_c);
450
451 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = sband;
452 count_c = sband->n_channels;
453 max_c -= count_c;
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500454 }
Bruno Randolf63266a62008-07-30 17:12:58 +0200455 ath5k_setup_rate_idx(sc, sband);
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500456
Bruno Randolf63266a62008-07-30 17:12:58 +0200457 /* 5GHz band, A mode */
Luis R. Rodriguez400ec452008-02-03 21:51:49 -0500458 if (test_bit(AR5K_MODE_11A, sc->ah->ah_capabilities.cap_mode)) {
Bruno Randolf63266a62008-07-30 17:12:58 +0200459 sband = &sc->sbands[IEEE80211_BAND_5GHZ];
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500460 sband->band = IEEE80211_BAND_5GHZ;
Bruno Randolf63266a62008-07-30 17:12:58 +0200461 sband->bitrates = &sc->rates[IEEE80211_BAND_5GHZ][0];
462
463 memcpy(sband->bitrates, &ath5k_rates[4],
464 sizeof(struct ieee80211_rate) * 8);
465 sband->n_bitrates = 8;
466
467 sband->channels = &sc->channels[count_c];
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500468 sband->n_channels = ath5k_copy_channels(ah, sband->channels,
469 AR5K_MODE_11A, max_c);
470
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500471 hw->wiphy->bands[IEEE80211_BAND_5GHZ] = sband;
472 }
Bruno Randolf63266a62008-07-30 17:12:58 +0200473 ath5k_setup_rate_idx(sc, sband);
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500474
Luis R. Rodriguezb4461972008-02-04 10:03:54 -0500475 ath5k_debug_dump_bands(sc);
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500476
477 return 0;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200478}
479
480/*
Joerg Alberte30eb4a2009-08-05 01:52:07 +0200481 * Set/change channels. We always reset the chip.
482 * To accomplish this we must first cleanup any pending DMA,
483 * then restart stuff after a la ath5k_init.
Bob Copelandbe009372009-01-22 08:44:16 -0500484 *
485 * Called with sc->lock.
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200486 */
487static int
488ath5k_chan_set(struct ath5k_softc *sc, struct ieee80211_channel *chan)
489{
Bruno Randolf8d67a032010-06-16 19:11:12 +0900490 ATH5K_DBG(sc, ATH5K_DEBUG_RESET,
491 "channel set, resetting (%u -> %u MHz)\n",
492 sc->curchan->center_freq, chan->center_freq);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200493
Joerg Alberte30eb4a2009-08-05 01:52:07 +0200494 /*
495 * To switch channels clear any pending DMA operations;
496 * wait long enough for the RX fifo to drain, reset the
497 * hardware at the new frequency, and then re-enable
498 * the relevant bits of the h/w.
499 */
Nick Kossifidis8aec7af2010-11-23 21:39:28 +0200500 return ath5k_reset(sc, chan, true);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200501}
502
503static void
504ath5k_setcurmode(struct ath5k_softc *sc, unsigned int mode)
505{
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200506 sc->curmode = mode;
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500507
Luis R. Rodriguez400ec452008-02-03 21:51:49 -0500508 if (mode == AR5K_MODE_11A) {
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500509 sc->curband = &sc->sbands[IEEE80211_BAND_5GHZ];
510 } else {
511 sc->curband = &sc->sbands[IEEE80211_BAND_2GHZ];
512 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200513}
514
Ben Greearb1ae1ed2010-09-30 12:22:58 -0700515struct ath_vif_iter_data {
516 const u8 *hw_macaddr;
517 u8 mask[ETH_ALEN];
518 u8 active_mac[ETH_ALEN]; /* first active MAC */
519 bool need_set_hw_addr;
520 bool found_active;
521 bool any_assoc;
Ben Greear62c58fb2010-10-08 12:01:15 -0700522 enum nl80211_iftype opmode;
Ben Greearb1ae1ed2010-09-30 12:22:58 -0700523};
524
525static void ath_vif_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
526{
527 struct ath_vif_iter_data *iter_data = data;
528 int i;
Ben Greear62c58fb2010-10-08 12:01:15 -0700529 struct ath5k_vif *avf = (void *)vif->drv_priv;
Ben Greearb1ae1ed2010-09-30 12:22:58 -0700530
531 if (iter_data->hw_macaddr)
532 for (i = 0; i < ETH_ALEN; i++)
533 iter_data->mask[i] &=
534 ~(iter_data->hw_macaddr[i] ^ mac[i]);
535
536 if (!iter_data->found_active) {
537 iter_data->found_active = true;
538 memcpy(iter_data->active_mac, mac, ETH_ALEN);
539 }
540
541 if (iter_data->need_set_hw_addr && iter_data->hw_macaddr)
542 if (compare_ether_addr(iter_data->hw_macaddr, mac) == 0)
543 iter_data->need_set_hw_addr = false;
544
545 if (!iter_data->any_assoc) {
Ben Greearb1ae1ed2010-09-30 12:22:58 -0700546 if (avf->assoc)
547 iter_data->any_assoc = true;
548 }
Ben Greear62c58fb2010-10-08 12:01:15 -0700549
550 /* Calculate combined mode - when APs are active, operate in AP mode.
551 * Otherwise use the mode of the new interface. This can currently
552 * only deal with combinations of APs and STAs. Only one ad-hoc
Ben Greear7afbb2f2010-11-10 11:43:51 -0800553 * interfaces is allowed.
Ben Greear62c58fb2010-10-08 12:01:15 -0700554 */
555 if (avf->opmode == NL80211_IFTYPE_AP)
556 iter_data->opmode = NL80211_IFTYPE_AP;
557 else
558 if (iter_data->opmode == NL80211_IFTYPE_UNSPECIFIED)
559 iter_data->opmode = avf->opmode;
Ben Greearb1ae1ed2010-09-30 12:22:58 -0700560}
561
Luis R. Rodriguez14fb7c12010-10-20 06:59:38 -0700562static void ath5k_update_bssid_mask_and_opmode(struct ath5k_softc *sc,
563 struct ieee80211_vif *vif)
Ben Greearb1ae1ed2010-09-30 12:22:58 -0700564{
565 struct ath_common *common = ath5k_hw_common(sc->ah);
566 struct ath_vif_iter_data iter_data;
567
568 /*
569 * Use the hardware MAC address as reference, the hardware uses it
570 * together with the BSSID mask when matching addresses.
571 */
572 iter_data.hw_macaddr = common->macaddr;
573 memset(&iter_data.mask, 0xff, ETH_ALEN);
574 iter_data.found_active = false;
575 iter_data.need_set_hw_addr = true;
Ben Greear62c58fb2010-10-08 12:01:15 -0700576 iter_data.opmode = NL80211_IFTYPE_UNSPECIFIED;
Ben Greearb1ae1ed2010-09-30 12:22:58 -0700577
578 if (vif)
579 ath_vif_iter(&iter_data, vif->addr, vif);
580
581 /* Get list of all active MAC addresses */
582 ieee80211_iterate_active_interfaces_atomic(sc->hw, ath_vif_iter,
583 &iter_data);
584 memcpy(sc->bssidmask, iter_data.mask, ETH_ALEN);
585
Ben Greear62c58fb2010-10-08 12:01:15 -0700586 sc->opmode = iter_data.opmode;
587 if (sc->opmode == NL80211_IFTYPE_UNSPECIFIED)
588 /* Nothing active, default to station mode */
589 sc->opmode = NL80211_IFTYPE_STATION;
590
Ben Greear7afbb2f2010-11-10 11:43:51 -0800591 ath5k_hw_set_opmode(sc->ah, sc->opmode);
592 ATH5K_DBG(sc, ATH5K_DEBUG_MODE, "mode setup opmode %d (%s)\n",
593 sc->opmode, ath_opmode_to_string(sc->opmode));
Ben Greear62c58fb2010-10-08 12:01:15 -0700594
Ben Greearb1ae1ed2010-09-30 12:22:58 -0700595 if (iter_data.need_set_hw_addr && iter_data.found_active)
596 ath5k_hw_set_lladdr(sc->ah, iter_data.active_mac);
597
Ben Greear62c58fb2010-10-08 12:01:15 -0700598 if (ath5k_hw_hasbssidmask(sc->ah))
599 ath5k_hw_set_bssid_mask(sc->ah, sc->bssidmask);
Ben Greearb1ae1ed2010-09-30 12:22:58 -0700600}
601
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200602static void
Ben Greearb1ae1ed2010-09-30 12:22:58 -0700603ath5k_mode_setup(struct ath5k_softc *sc, struct ieee80211_vif *vif)
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200604{
605 struct ath5k_hw *ah = sc->ah;
606 u32 rfilt;
607
608 /* configure rx filter */
609 rfilt = sc->filter_flags;
610 ath5k_hw_set_rx_filter(ah, rfilt);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200611 ATH5K_DBG(sc, ATH5K_DEBUG_MODE, "RX filter 0x%x\n", rfilt);
Ben Greear62c58fb2010-10-08 12:01:15 -0700612
613 ath5k_update_bssid_mask_and_opmode(sc, vif);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200614}
615
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500616static inline int
Bruno Randolf63266a62008-07-30 17:12:58 +0200617ath5k_hw_to_driver_rix(struct ath5k_softc *sc, int hw_rix)
618{
Bob Copelandb7266042009-03-02 21:55:18 -0500619 int rix;
620
621 /* return base rate on errors */
622 if (WARN(hw_rix < 0 || hw_rix >= AR5K_MAX_RATES,
623 "hw_rix out of bounds: %x\n", hw_rix))
624 return 0;
625
626 rix = sc->rate_idx[sc->curband->band][hw_rix];
627 if (WARN(rix < 0, "invalid hw_rix: %x\n", hw_rix))
628 rix = 0;
629
630 return rix;
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500631}
632
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200633/***************\
634* Buffers setup *
635\***************/
636
Bob Copelandb6ea0352009-01-10 14:42:54 -0500637static
638struct sk_buff *ath5k_rx_skb_alloc(struct ath5k_softc *sc, dma_addr_t *skb_addr)
639{
Luis R. Rodriguezdb719712009-09-10 11:20:57 -0700640 struct ath_common *common = ath5k_hw_common(sc->ah);
Bob Copelandb6ea0352009-01-10 14:42:54 -0500641 struct sk_buff *skb;
Bob Copelandb6ea0352009-01-10 14:42:54 -0500642
643 /*
644 * Allocate buffer with headroom_needed space for the
645 * fake physical layer header at the start.
646 */
Luis R. Rodriguezdb719712009-09-10 11:20:57 -0700647 skb = ath_rxbuf_alloc(common,
Luis R. Rodriguezdd849782009-11-04 09:44:50 -0800648 common->rx_bufsize,
Luis R. Rodriguezaeb63cf2009-08-12 09:57:00 -0700649 GFP_ATOMIC);
Bob Copelandb6ea0352009-01-10 14:42:54 -0500650
651 if (!skb) {
652 ATH5K_ERR(sc, "can't alloc skbuff of size %u\n",
Luis R. Rodriguezdd849782009-11-04 09:44:50 -0800653 common->rx_bufsize);
Bob Copelandb6ea0352009-01-10 14:42:54 -0500654 return NULL;
655 }
Bob Copelandb6ea0352009-01-10 14:42:54 -0500656
657 *skb_addr = pci_map_single(sc->pdev,
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -0800658 skb->data, common->rx_bufsize,
659 PCI_DMA_FROMDEVICE);
Bob Copelandb6ea0352009-01-10 14:42:54 -0500660 if (unlikely(pci_dma_mapping_error(sc->pdev, *skb_addr))) {
661 ATH5K_ERR(sc, "%s: DMA mapping failed\n", __func__);
662 dev_kfree_skb(skb);
663 return NULL;
664 }
665 return skb;
666}
667
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200668static int
669ath5k_rxbuf_setup(struct ath5k_softc *sc, struct ath5k_buf *bf)
670{
671 struct ath5k_hw *ah = sc->ah;
672 struct sk_buff *skb = bf->skb;
673 struct ath5k_desc *ds;
Bruno Randolfb5eae9f2010-05-19 10:18:16 +0900674 int ret;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200675
Bob Copelandb6ea0352009-01-10 14:42:54 -0500676 if (!skb) {
677 skb = ath5k_rx_skb_alloc(sc, &bf->skbaddr);
678 if (!skb)
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200679 return -ENOMEM;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200680 bf->skb = skb;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200681 }
682
683 /*
684 * Setup descriptors. For receive we always terminate
685 * the descriptor list with a self-linked entry so we'll
686 * not get overrun under high load (as can happen with a
687 * 5212 when ANI processing enables PHY error frames).
688 *
Bruno Randolfbeade632010-06-16 19:11:25 +0900689 * To ensure the last descriptor is self-linked we create
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200690 * each descriptor as self-linked and add it to the end. As
691 * each additional descriptor is added the previous self-linked
Bruno Randolfbeade632010-06-16 19:11:25 +0900692 * entry is "fixed" naturally. This should be safe even
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200693 * if DMA is happening. When processing RX interrupts we
694 * never remove/process the last, self-linked, entry on the
Bruno Randolfbeade632010-06-16 19:11:25 +0900695 * descriptor list. This ensures the hardware always has
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200696 * someplace to write a new frame.
697 */
698 ds = bf->desc;
699 ds->ds_link = bf->daddr; /* link to self */
700 ds->ds_data = bf->skbaddr;
Bruno Randolfa6668192010-06-16 19:12:01 +0900701 ret = ath5k_hw_setup_rx_desc(ah, ds, ah->common.rx_bufsize, 0);
Bruno Randolf0452d4a2010-06-16 19:11:35 +0900702 if (ret) {
703 ATH5K_ERR(sc, "%s: could not setup RX desc\n", __func__);
Bruno Randolfb5eae9f2010-05-19 10:18:16 +0900704 return ret;
Bruno Randolf0452d4a2010-06-16 19:11:35 +0900705 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200706
707 if (sc->rxlink != NULL)
708 *sc->rxlink = bf->daddr;
709 sc->rxlink = &ds->ds_link;
710 return 0;
711}
712
Bob Copeland2ac29272010-02-09 13:06:54 -0500713static enum ath5k_pkt_type get_hw_packet_type(struct sk_buff *skb)
714{
715 struct ieee80211_hdr *hdr;
716 enum ath5k_pkt_type htype;
717 __le16 fc;
718
719 hdr = (struct ieee80211_hdr *)skb->data;
720 fc = hdr->frame_control;
721
722 if (ieee80211_is_beacon(fc))
723 htype = AR5K_PKT_TYPE_BEACON;
724 else if (ieee80211_is_probe_resp(fc))
725 htype = AR5K_PKT_TYPE_PROBE_RESP;
726 else if (ieee80211_is_atim(fc))
727 htype = AR5K_PKT_TYPE_ATIM;
728 else if (ieee80211_is_pspoll(fc))
729 htype = AR5K_PKT_TYPE_PSPOLL;
730 else
731 htype = AR5K_PKT_TYPE_NORMAL;
732
733 return htype;
734}
735
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200736static int
Bob Copelandcec8db22009-07-04 12:59:51 -0400737ath5k_txbuf_setup(struct ath5k_softc *sc, struct ath5k_buf *bf,
Benoit Papillault8127fbd2010-02-27 23:05:26 +0100738 struct ath5k_txq *txq, int padsize)
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200739{
740 struct ath5k_hw *ah = sc->ah;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200741 struct ath5k_desc *ds = bf->desc;
742 struct sk_buff *skb = bf->skb;
Johannes Berga888d522008-05-26 16:43:39 +0200743 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200744 unsigned int pktlen, flags, keyidx = AR5K_TXKEYIX_INVALID;
Felix Fietkau2f7fe872008-10-05 18:05:48 +0200745 struct ieee80211_rate *rate;
746 unsigned int mrr_rate[3], mrr_tries[3];
747 int i, ret;
Bob Copeland8902ff42009-01-22 08:44:20 -0500748 u16 hw_rate;
Bob Copeland07c1e852009-01-22 08:44:21 -0500749 u16 cts_rate = 0;
750 u16 duration = 0;
Bob Copeland8902ff42009-01-22 08:44:20 -0500751 u8 rc_flags;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200752
753 flags = AR5K_TXDESC_INTREQ | AR5K_TXDESC_CLRDMASK;
Johannes Berge039fa42008-05-15 12:55:29 +0200754
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200755 /* XXX endianness */
756 bf->skbaddr = pci_map_single(sc->pdev, skb->data, skb->len,
757 PCI_DMA_TODEVICE);
758
Bob Copeland8902ff42009-01-22 08:44:20 -0500759 rate = ieee80211_get_tx_rate(sc->hw, info);
John W. Linvilled8e1ba72010-08-24 15:27:34 -0400760 if (!rate) {
761 ret = -EINVAL;
762 goto err_unmap;
763 }
Bob Copeland8902ff42009-01-22 08:44:20 -0500764
Johannes Berge039fa42008-05-15 12:55:29 +0200765 if (info->flags & IEEE80211_TX_CTL_NO_ACK)
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200766 flags |= AR5K_TXDESC_NOACK;
767
Bob Copeland8902ff42009-01-22 08:44:20 -0500768 rc_flags = info->control.rates[0].flags;
769 hw_rate = (rc_flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE) ?
770 rate->hw_value_short : rate->hw_value;
771
Bruno Randolf281c56d2008-02-05 18:44:55 +0900772 pktlen = skb->len;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200773
Nick Kossifidis8f655dd2009-03-15 22:20:35 +0200774 /* FIXME: If we are in g mode and rate is a CCK rate
775 * subtract ah->ah_txpower.txp_cck_ofdm_pwr_delta
776 * from tx power (value is in dB units already) */
Bob Copeland362695e2009-02-15 12:06:12 -0500777 if (info->control.hw_key) {
778 keyidx = info->control.hw_key->hw_key_idx;
779 pktlen += info->control.hw_key->icv_len;
780 }
Bob Copeland07c1e852009-01-22 08:44:21 -0500781 if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
782 flags |= AR5K_TXDESC_RTSENA;
783 cts_rate = ieee80211_get_rts_cts_rate(sc->hw, info)->hw_value;
784 duration = le16_to_cpu(ieee80211_rts_duration(sc->hw,
Ben Greearb1ae1ed2010-09-30 12:22:58 -0700785 info->control.vif, pktlen, info));
Bob Copeland07c1e852009-01-22 08:44:21 -0500786 }
787 if (rc_flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
788 flags |= AR5K_TXDESC_CTSENA;
789 cts_rate = ieee80211_get_rts_cts_rate(sc->hw, info)->hw_value;
790 duration = le16_to_cpu(ieee80211_ctstoself_duration(sc->hw,
Ben Greearb1ae1ed2010-09-30 12:22:58 -0700791 info->control.vif, pktlen, info));
Bob Copeland07c1e852009-01-22 08:44:21 -0500792 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200793 ret = ah->ah_setup_tx_desc(ah, ds, pktlen,
Benoit Papillault8127fbd2010-02-27 23:05:26 +0100794 ieee80211_get_hdrlen_from_skb(skb), padsize,
Bob Copeland2ac29272010-02-09 13:06:54 -0500795 get_hw_packet_type(skb),
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200796 (sc->power_level * 2),
Bob Copeland8902ff42009-01-22 08:44:20 -0500797 hw_rate,
Nick Kossifidis2bed03e2009-04-30 15:55:49 -0400798 info->control.rates[0].count, keyidx, ah->ah_tx_ant, flags,
Bob Copeland07c1e852009-01-22 08:44:21 -0500799 cts_rate, duration);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200800 if (ret)
801 goto err_unmap;
802
Felix Fietkau2f7fe872008-10-05 18:05:48 +0200803 memset(mrr_rate, 0, sizeof(mrr_rate));
804 memset(mrr_tries, 0, sizeof(mrr_tries));
805 for (i = 0; i < 3; i++) {
806 rate = ieee80211_get_alt_retry_rate(sc->hw, info, i);
807 if (!rate)
808 break;
809
810 mrr_rate[i] = rate->hw_value;
Johannes Berge6a98542008-10-21 12:40:02 +0200811 mrr_tries[i] = info->control.rates[i + 1].count;
Felix Fietkau2f7fe872008-10-05 18:05:48 +0200812 }
813
Bruno Randolfa6668192010-06-16 19:12:01 +0900814 ath5k_hw_setup_mrr_tx_desc(ah, ds,
Felix Fietkau2f7fe872008-10-05 18:05:48 +0200815 mrr_rate[0], mrr_tries[0],
816 mrr_rate[1], mrr_tries[1],
817 mrr_rate[2], mrr_tries[2]);
818
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200819 ds->ds_link = 0;
820 ds->ds_data = bf->skbaddr;
821
822 spin_lock_bh(&txq->lock);
823 list_add_tail(&bf->list, &txq->q);
Bruno Randolf925e0b02010-09-17 11:36:35 +0900824 txq->txq_len++;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200825 if (txq->link == NULL) /* is this first packet? */
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300826 ath5k_hw_set_txdp(ah, txq->qnum, bf->daddr);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200827 else /* no, so only link it */
828 *txq->link = bf->daddr;
829
830 txq->link = &ds->ds_link;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300831 ath5k_hw_start_tx_dma(ah, txq->qnum);
Jiri Slaby274c7c32008-07-15 17:44:20 +0200832 mmiowb();
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200833 spin_unlock_bh(&txq->lock);
834
835 return 0;
836err_unmap:
837 pci_unmap_single(sc->pdev, bf->skbaddr, skb->len, PCI_DMA_TODEVICE);
838 return ret;
839}
840
841/*******************\
842* Descriptors setup *
843\*******************/
844
845static int
846ath5k_desc_alloc(struct ath5k_softc *sc, struct pci_dev *pdev)
847{
848 struct ath5k_desc *ds;
849 struct ath5k_buf *bf;
850 dma_addr_t da;
851 unsigned int i;
852 int ret;
853
854 /* allocate descriptors */
855 sc->desc_len = sizeof(struct ath5k_desc) *
856 (ATH_TXBUF + ATH_RXBUF + ATH_BCBUF + 1);
857 sc->desc = pci_alloc_consistent(pdev, sc->desc_len, &sc->desc_daddr);
858 if (sc->desc == NULL) {
859 ATH5K_ERR(sc, "can't allocate descriptors\n");
860 ret = -ENOMEM;
861 goto err;
862 }
863 ds = sc->desc;
864 da = sc->desc_daddr;
865 ATH5K_DBG(sc, ATH5K_DEBUG_ANY, "DMA map: %p (%zu) -> %llx\n",
866 ds, sc->desc_len, (unsigned long long)sc->desc_daddr);
867
868 bf = kcalloc(1 + ATH_TXBUF + ATH_RXBUF + ATH_BCBUF,
869 sizeof(struct ath5k_buf), GFP_KERNEL);
870 if (bf == NULL) {
871 ATH5K_ERR(sc, "can't allocate bufptr\n");
872 ret = -ENOMEM;
873 goto err_free;
874 }
875 sc->bufptr = bf;
876
877 INIT_LIST_HEAD(&sc->rxbuf);
878 for (i = 0; i < ATH_RXBUF; i++, bf++, ds++, da += sizeof(*ds)) {
879 bf->desc = ds;
880 bf->daddr = da;
881 list_add_tail(&bf->list, &sc->rxbuf);
882 }
883
884 INIT_LIST_HEAD(&sc->txbuf);
885 sc->txbuf_len = ATH_TXBUF;
886 for (i = 0; i < ATH_TXBUF; i++, bf++, ds++,
887 da += sizeof(*ds)) {
888 bf->desc = ds;
889 bf->daddr = da;
890 list_add_tail(&bf->list, &sc->txbuf);
891 }
892
Ben Greearb1ae1ed2010-09-30 12:22:58 -0700893 /* beacon buffers */
894 INIT_LIST_HEAD(&sc->bcbuf);
895 for (i = 0; i < ATH_BCBUF; i++, bf++, ds++, da += sizeof(*ds)) {
896 bf->desc = ds;
897 bf->daddr = da;
898 list_add_tail(&bf->list, &sc->bcbuf);
899 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200900
901 return 0;
902err_free:
903 pci_free_consistent(pdev, sc->desc_len, sc->desc, sc->desc_daddr);
904err:
905 sc->desc = NULL;
906 return ret;
907}
908
909static void
910ath5k_desc_free(struct ath5k_softc *sc, struct pci_dev *pdev)
911{
912 struct ath5k_buf *bf;
913
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200914 list_for_each_entry(bf, &sc->txbuf, list)
Bruno Randolf9e4e43f2010-06-16 19:11:17 +0900915 ath5k_txbuf_free_skb(sc, bf);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200916 list_for_each_entry(bf, &sc->rxbuf, list)
Bruno Randolf9e4e43f2010-06-16 19:11:17 +0900917 ath5k_rxbuf_free_skb(sc, bf);
Ben Greearb1ae1ed2010-09-30 12:22:58 -0700918 list_for_each_entry(bf, &sc->bcbuf, list)
919 ath5k_txbuf_free_skb(sc, bf);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200920
921 /* Free memory associated with all descriptors */
922 pci_free_consistent(pdev, sc->desc_len, sc->desc, sc->desc_daddr);
Bruno Randolf39d63f22010-06-16 19:11:41 +0900923 sc->desc = NULL;
924 sc->desc_daddr = 0;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200925
926 kfree(sc->bufptr);
927 sc->bufptr = NULL;
928}
929
930
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200931/**************\
932* Queues setup *
933\**************/
934
935static struct ath5k_txq *
936ath5k_txq_setup(struct ath5k_softc *sc,
937 int qtype, int subtype)
938{
939 struct ath5k_hw *ah = sc->ah;
940 struct ath5k_txq *txq;
941 struct ath5k_txq_info qi = {
942 .tqi_subtype = subtype,
Bruno Randolfde8af452010-09-17 11:37:12 +0900943 /* XXX: default values not correct for B and XR channels,
944 * but who cares? */
945 .tqi_aifs = AR5K_TUNE_AIFS,
946 .tqi_cw_min = AR5K_TUNE_CWMIN,
947 .tqi_cw_max = AR5K_TUNE_CWMAX
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200948 };
949 int qnum;
950
951 /*
952 * Enable interrupts only for EOL and DESC conditions.
953 * We mark tx descriptors to receive a DESC interrupt
Bob Copelanda180a132010-08-15 13:03:12 -0400954 * when a tx queue gets deep; otherwise we wait for the
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200955 * EOL to reap descriptors. Note that this is done to
956 * reduce interrupt load and this only defers reaping
957 * descriptors, never transmitting frames. Aside from
958 * reducing interrupts this also permits more concurrency.
959 * The only potential downside is if the tx queue backs
960 * up in which case the top half of the kernel may backup
961 * due to a lack of tx descriptors.
962 */
963 qi.tqi_flags = AR5K_TXQ_FLAG_TXEOLINT_ENABLE |
964 AR5K_TXQ_FLAG_TXDESCINT_ENABLE;
965 qnum = ath5k_hw_setup_tx_queue(ah, qtype, &qi);
966 if (qnum < 0) {
967 /*
968 * NB: don't print a message, this happens
969 * normally on parts with too few tx queues
970 */
971 return ERR_PTR(qnum);
972 }
973 if (qnum >= ARRAY_SIZE(sc->txqs)) {
974 ATH5K_ERR(sc, "hw qnum %u out of range, max %tu!\n",
975 qnum, ARRAY_SIZE(sc->txqs));
976 ath5k_hw_release_tx_queue(ah, qnum);
977 return ERR_PTR(-EINVAL);
978 }
979 txq = &sc->txqs[qnum];
980 if (!txq->setup) {
981 txq->qnum = qnum;
982 txq->link = NULL;
983 INIT_LIST_HEAD(&txq->q);
984 spin_lock_init(&txq->lock);
985 txq->setup = true;
Bruno Randolf925e0b02010-09-17 11:36:35 +0900986 txq->txq_len = 0;
Bruno Randolf4edd7612010-09-17 11:36:56 +0900987 txq->txq_poll_mark = false;
Bruno Randolf923e5b32010-09-17 11:37:02 +0900988 txq->txq_stuck = 0;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200989 }
990 return &sc->txqs[qnum];
991}
992
993static int
994ath5k_beaconq_setup(struct ath5k_hw *ah)
995{
996 struct ath5k_txq_info qi = {
Bruno Randolfde8af452010-09-17 11:37:12 +0900997 /* XXX: default values not correct for B and XR channels,
998 * but who cares? */
999 .tqi_aifs = AR5K_TUNE_AIFS,
1000 .tqi_cw_min = AR5K_TUNE_CWMIN,
1001 .tqi_cw_max = AR5K_TUNE_CWMAX,
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001002 /* NB: for dynamic turbo, don't enable any other interrupts */
1003 .tqi_flags = AR5K_TXQ_FLAG_TXDESCINT_ENABLE
1004 };
1005
1006 return ath5k_hw_setup_tx_queue(ah, AR5K_TX_QUEUE_BEACON, &qi);
1007}
1008
1009static int
1010ath5k_beaconq_config(struct ath5k_softc *sc)
1011{
1012 struct ath5k_hw *ah = sc->ah;
1013 struct ath5k_txq_info qi;
1014 int ret;
1015
1016 ret = ath5k_hw_get_tx_queueprops(ah, sc->bhalq, &qi);
1017 if (ret)
Bob Copelanda951ae22010-01-20 23:51:04 -05001018 goto err;
1019
Johannes Berg05c914f2008-09-11 00:01:58 +02001020 if (sc->opmode == NL80211_IFTYPE_AP ||
1021 sc->opmode == NL80211_IFTYPE_MESH_POINT) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001022 /*
1023 * Always burst out beacon and CAB traffic
1024 * (aifs = cwmin = cwmax = 0)
1025 */
1026 qi.tqi_aifs = 0;
1027 qi.tqi_cw_min = 0;
1028 qi.tqi_cw_max = 0;
Johannes Berg05c914f2008-09-11 00:01:58 +02001029 } else if (sc->opmode == NL80211_IFTYPE_ADHOC) {
Bruno Randolf6d91e1d2008-01-19 18:18:41 +09001030 /*
1031 * Adhoc mode; backoff between 0 and (2 * cw_min).
1032 */
1033 qi.tqi_aifs = 0;
1034 qi.tqi_cw_min = 0;
Bruno Randolfde8af452010-09-17 11:37:12 +09001035 qi.tqi_cw_max = 2 * AR5K_TUNE_CWMIN;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001036 }
1037
Bruno Randolf6d91e1d2008-01-19 18:18:41 +09001038 ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
1039 "beacon queueprops tqi_aifs:%d tqi_cw_min:%d tqi_cw_max:%d\n",
1040 qi.tqi_aifs, qi.tqi_cw_min, qi.tqi_cw_max);
1041
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001042 ret = ath5k_hw_set_tx_queueprops(ah, sc->bhalq, &qi);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001043 if (ret) {
1044 ATH5K_ERR(sc, "%s: unable to update parameters for beacon "
1045 "hardware queue!\n", __func__);
Bob Copelanda951ae22010-01-20 23:51:04 -05001046 goto err;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001047 }
Bob Copelanda951ae22010-01-20 23:51:04 -05001048 ret = ath5k_hw_reset_tx_queue(ah, sc->bhalq); /* push to h/w */
1049 if (ret)
1050 goto err;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001051
Bob Copelanda951ae22010-01-20 23:51:04 -05001052 /* reconfigure cabq with ready time to 80% of beacon_interval */
1053 ret = ath5k_hw_get_tx_queueprops(ah, AR5K_TX_QUEUE_ID_CAB, &qi);
1054 if (ret)
1055 goto err;
1056
1057 qi.tqi_ready_time = (sc->bintval * 80) / 100;
1058 ret = ath5k_hw_set_tx_queueprops(ah, AR5K_TX_QUEUE_ID_CAB, &qi);
1059 if (ret)
1060 goto err;
1061
1062 ret = ath5k_hw_reset_tx_queue(ah, AR5K_TX_QUEUE_ID_CAB);
1063err:
1064 return ret;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001065}
1066
Nick Kossifidis80dac9ee2010-11-23 20:45:38 +02001067/**
1068 * ath5k_drain_tx_buffs - Empty tx buffers
1069 *
1070 * @sc The &struct ath5k_softc
1071 *
1072 * Empty tx buffers from all queues in preparation
1073 * of a reset or during shutdown.
1074 *
1075 * NB: this assumes output has been stopped and
1076 * we do not need to block ath5k_tx_tasklet
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001077 */
1078static void
Nick Kossifidis80dac9ee2010-11-23 20:45:38 +02001079ath5k_drain_tx_buffs(struct ath5k_softc *sc)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001080{
Nick Kossifidis80dac9ee2010-11-23 20:45:38 +02001081 struct ath5k_txq *txq;
1082 struct ath5k_buf *bf, *bf0;
1083 int i;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001084
Nick Kossifidis80dac9ee2010-11-23 20:45:38 +02001085 for (i = 0; i < ARRAY_SIZE(sc->txqs); i++) {
1086 if (sc->txqs[i].setup) {
1087 txq = &sc->txqs[i];
1088 spin_lock_bh(&txq->lock);
1089 list_for_each_entry_safe(bf, bf0, &txq->q, list) {
1090 ath5k_debug_printtxbuf(sc, bf);
1091
1092 ath5k_txbuf_free_skb(sc, bf);
1093
1094 spin_lock_bh(&sc->txbuflock);
1095 list_move_tail(&bf->list, &sc->txbuf);
1096 sc->txbuf_len++;
1097 txq->txq_len--;
1098 spin_unlock_bh(&sc->txbuflock);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001099 }
Nick Kossifidis80dac9ee2010-11-23 20:45:38 +02001100 txq->link = NULL;
1101 txq->txq_poll_mark = false;
1102 spin_unlock_bh(&txq->lock);
1103 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001104 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001105}
1106
1107static void
1108ath5k_txq_release(struct ath5k_softc *sc)
1109{
1110 struct ath5k_txq *txq = sc->txqs;
1111 unsigned int i;
1112
1113 for (i = 0; i < ARRAY_SIZE(sc->txqs); i++, txq++)
1114 if (txq->setup) {
1115 ath5k_hw_release_tx_queue(sc->ah, txq->qnum);
1116 txq->setup = false;
1117 }
1118}
1119
1120
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001121/*************\
1122* RX Handling *
1123\*************/
1124
1125/*
1126 * Enable the receive h/w following a reset.
1127 */
1128static int
1129ath5k_rx_start(struct ath5k_softc *sc)
1130{
1131 struct ath5k_hw *ah = sc->ah;
Luis R. Rodriguezdb719712009-09-10 11:20:57 -07001132 struct ath_common *common = ath5k_hw_common(ah);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001133 struct ath5k_buf *bf;
1134 int ret;
1135
Nick Kossifidisb6127982010-08-15 13:03:11 -04001136 common->rx_bufsize = roundup(IEEE80211_MAX_FRAME_LEN, common->cachelsz);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001137
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -08001138 ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "cachelsz %u rx_bufsize %u\n",
1139 common->cachelsz, common->rx_bufsize);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001140
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001141 spin_lock_bh(&sc->rxbuflock);
Bob Copeland26925042009-04-15 07:57:36 -04001142 sc->rxlink = NULL;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001143 list_for_each_entry(bf, &sc->rxbuf, list) {
1144 ret = ath5k_rxbuf_setup(sc, bf);
1145 if (ret != 0) {
1146 spin_unlock_bh(&sc->rxbuflock);
1147 goto err;
1148 }
1149 }
1150 bf = list_first_entry(&sc->rxbuf, struct ath5k_buf, list);
Bob Copeland26925042009-04-15 07:57:36 -04001151 ath5k_hw_set_rxdp(ah, bf->daddr);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001152 spin_unlock_bh(&sc->rxbuflock);
1153
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001154 ath5k_hw_start_rx_dma(ah); /* enable recv descriptors */
Ben Greearb1ae1ed2010-09-30 12:22:58 -07001155 ath5k_mode_setup(sc, NULL); /* set filters, etc. */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001156 ath5k_hw_start_rx_pcu(ah); /* re-enable PCU/DMA engine */
1157
1158 return 0;
1159err:
1160 return ret;
1161}
1162
1163/*
Nick Kossifidis80dac9ee2010-11-23 20:45:38 +02001164 * Disable the receive logic on PCU (DRU)
1165 * In preparation for a shutdown.
1166 *
1167 * Note: Doesn't stop rx DMA, ath5k_hw_dma_stop
1168 * does.
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001169 */
1170static void
1171ath5k_rx_stop(struct ath5k_softc *sc)
1172{
1173 struct ath5k_hw *ah = sc->ah;
1174
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001175 ath5k_hw_set_rx_filter(ah, 0); /* clear recv filter */
Nick Kossifidis80dac9ee2010-11-23 20:45:38 +02001176 ath5k_hw_stop_rx_pcu(ah); /* disable PCU */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001177
1178 ath5k_debug_printrxbuffs(sc, ah);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001179}
1180
1181static unsigned int
Bruno Randolf8a89f062010-06-16 19:11:51 +09001182ath5k_rx_decrypted(struct ath5k_softc *sc, struct sk_buff *skb,
1183 struct ath5k_rx_status *rs)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001184{
Luis R. Rodriguezdc1e0012009-11-04 17:47:31 -08001185 struct ath5k_hw *ah = sc->ah;
1186 struct ath_common *common = ath5k_hw_common(ah);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001187 struct ieee80211_hdr *hdr = (void *)skb->data;
Harvey Harrison798ee982008-07-15 18:44:02 -07001188 unsigned int keyix, hlen;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001189
Bruno Randolfb47f4072008-03-05 18:35:45 +09001190 if (!(rs->rs_status & AR5K_RXERR_DECRYPT) &&
1191 rs->rs_keyix != AR5K_RXKEYIX_INVALID)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001192 return RX_FLAG_DECRYPTED;
1193
1194 /* Apparently when a default key is used to decrypt the packet
1195 the hw does not set the index used to decrypt. In such cases
1196 get the index from the packet. */
Harvey Harrison798ee982008-07-15 18:44:02 -07001197 hlen = ieee80211_hdrlen(hdr->frame_control);
Harvey Harrison24b56e72008-06-14 23:33:38 -07001198 if (ieee80211_has_protected(hdr->frame_control) &&
1199 !(rs->rs_status & AR5K_RXERR_DECRYPT) &&
1200 skb->len >= hlen + 4) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001201 keyix = skb->data[hlen + 3] >> 6;
1202
Luis R. Rodriguezdc1e0012009-11-04 17:47:31 -08001203 if (test_bit(keyix, common->keymap))
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001204 return RX_FLAG_DECRYPTED;
1205 }
1206
1207 return 0;
1208}
1209
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001210
1211static void
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001212ath5k_check_ibss_tsf(struct ath5k_softc *sc, struct sk_buff *skb,
1213 struct ieee80211_rx_status *rxs)
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001214{
Luis R. Rodriguez954fece2009-09-10 10:51:33 -07001215 struct ath_common *common = ath5k_hw_common(sc->ah);
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001216 u64 tsf, bc_tstamp;
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001217 u32 hw_tu;
1218 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)skb->data;
1219
Harvey Harrison24b56e72008-06-14 23:33:38 -07001220 if (ieee80211_is_beacon(mgmt->frame_control) &&
Pavel Roskin38c07b42008-02-26 17:59:14 -05001221 le16_to_cpu(mgmt->u.beacon.capab_info) & WLAN_CAPABILITY_IBSS &&
Luis R. Rodriguez954fece2009-09-10 10:51:33 -07001222 memcmp(mgmt->bssid, common->curbssid, ETH_ALEN) == 0) {
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001223 /*
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001224 * Received an IBSS beacon with the same BSSID. Hardware *must*
1225 * have updated the local TSF. We have to work around various
1226 * hardware bugs, though...
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001227 */
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001228 tsf = ath5k_hw_get_tsf64(sc->ah);
1229 bc_tstamp = le64_to_cpu(mgmt->u.beacon.timestamp);
1230 hw_tu = TSF_TO_TU(tsf);
1231
1232 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
1233 "beacon %llx mactime %llx (diff %lld) tsf now %llx\n",
John W. Linville06501d22008-04-01 17:38:47 -04001234 (unsigned long long)bc_tstamp,
1235 (unsigned long long)rxs->mactime,
1236 (unsigned long long)(rxs->mactime - bc_tstamp),
1237 (unsigned long long)tsf);
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001238
1239 /*
1240 * Sometimes the HW will give us a wrong tstamp in the rx
1241 * status, causing the timestamp extension to go wrong.
1242 * (This seems to happen especially with beacon frames bigger
1243 * than 78 byte (incl. FCS))
1244 * But we know that the receive timestamp must be later than the
1245 * timestamp of the beacon since HW must have synced to that.
1246 *
1247 * NOTE: here we assume mactime to be after the frame was
1248 * received, not like mac80211 which defines it at the start.
1249 */
1250 if (bc_tstamp > rxs->mactime) {
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001251 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001252 "fixing mactime from %llx to %llx\n",
John W. Linville06501d22008-04-01 17:38:47 -04001253 (unsigned long long)rxs->mactime,
1254 (unsigned long long)tsf);
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001255 rxs->mactime = tsf;
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001256 }
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001257
1258 /*
1259 * Local TSF might have moved higher than our beacon timers,
1260 * in that case we have to update them to continue sending
1261 * beacons. This also takes care of synchronizing beacon sending
1262 * times with other stations.
1263 */
1264 if (hw_tu >= sc->nexttbtt)
1265 ath5k_beacon_update_timers(sc, bc_tstamp);
Bruno Randolf7f896122010-09-27 12:22:21 +09001266
1267 /* Check if the beacon timers are still correct, because a TSF
1268 * update might have created a window between them - for a
1269 * longer description see the comment of this function: */
1270 if (!ath5k_hw_check_beacon_timers(sc->ah, sc->bintval)) {
1271 ath5k_beacon_update_timers(sc, bc_tstamp);
1272 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
1273 "fixed beacon timers after beacon receive\n");
1274 }
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001275 }
1276}
1277
Bruno Randolfb4ea4492010-03-25 14:49:25 +09001278static void
1279ath5k_update_beacon_rssi(struct ath5k_softc *sc, struct sk_buff *skb, int rssi)
1280{
1281 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)skb->data;
1282 struct ath5k_hw *ah = sc->ah;
1283 struct ath_common *common = ath5k_hw_common(ah);
1284
1285 /* only beacons from our BSSID */
1286 if (!ieee80211_is_beacon(mgmt->frame_control) ||
1287 memcmp(mgmt->bssid, common->curbssid, ETH_ALEN) != 0)
1288 return;
1289
Bruno Randolfeef39be2010-11-16 10:58:43 +09001290 ewma_add(&ah->ah_beacon_rssi_avg, rssi);
Bruno Randolfb4ea4492010-03-25 14:49:25 +09001291
1292 /* in IBSS mode we should keep RSSI statistics per neighbour */
1293 /* le16_to_cpu(mgmt->u.beacon.capab_info) & WLAN_CAPABILITY_IBSS */
1294}
1295
Benoit Papillault8127fbd2010-02-27 23:05:26 +01001296/*
Bob Copelanda180a132010-08-15 13:03:12 -04001297 * Compute padding position. skb must contain an IEEE 802.11 frame
Benoit Papillault8127fbd2010-02-27 23:05:26 +01001298 */
1299static int ath5k_common_padpos(struct sk_buff *skb)
1300{
1301 struct ieee80211_hdr * hdr = (struct ieee80211_hdr *)skb->data;
1302 __le16 frame_control = hdr->frame_control;
1303 int padpos = 24;
1304
1305 if (ieee80211_has_a4(frame_control)) {
1306 padpos += ETH_ALEN;
1307 }
1308 if (ieee80211_is_data_qos(frame_control)) {
1309 padpos += IEEE80211_QOS_CTL_LEN;
1310 }
1311
1312 return padpos;
1313}
1314
1315/*
Bob Copelanda180a132010-08-15 13:03:12 -04001316 * This function expects an 802.11 frame and returns the number of
1317 * bytes added, or -1 if we don't have enough header room.
Benoit Papillault8127fbd2010-02-27 23:05:26 +01001318 */
Benoit Papillault8127fbd2010-02-27 23:05:26 +01001319static int ath5k_add_padding(struct sk_buff *skb)
1320{
1321 int padpos = ath5k_common_padpos(skb);
1322 int padsize = padpos & 3;
1323
1324 if (padsize && skb->len>padpos) {
1325
1326 if (skb_headroom(skb) < padsize)
1327 return -1;
1328
1329 skb_push(skb, padsize);
1330 memmove(skb->data, skb->data+padsize, padpos);
1331 return padsize;
1332 }
1333
1334 return 0;
1335}
1336
1337/*
Bob Copelanda180a132010-08-15 13:03:12 -04001338 * The MAC header is padded to have 32-bit boundary if the
1339 * packet payload is non-zero. The general calculation for
1340 * padsize would take into account odd header lengths:
1341 * padsize = 4 - (hdrlen & 3); however, since only
1342 * even-length headers are used, padding can only be 0 or 2
1343 * bytes and we can optimize this a bit. We must not try to
1344 * remove padding from short control frames that do not have a
1345 * payload.
1346 *
1347 * This function expects an 802.11 frame and returns the number of
1348 * bytes removed.
Benoit Papillault8127fbd2010-02-27 23:05:26 +01001349 */
Benoit Papillault8127fbd2010-02-27 23:05:26 +01001350static int ath5k_remove_padding(struct sk_buff *skb)
1351{
1352 int padpos = ath5k_common_padpos(skb);
1353 int padsize = padpos & 3;
1354
1355 if (padsize && skb->len>=padpos+padsize) {
1356 memmove(skb->data + padsize, skb->data, padpos);
1357 skb_pull(skb, padsize);
1358 return padsize;
1359 }
1360
1361 return 0;
1362}
1363
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001364static void
Bruno Randolf8a89f062010-06-16 19:11:51 +09001365ath5k_receive_frame(struct ath5k_softc *sc, struct sk_buff *skb,
1366 struct ath5k_rx_status *rs)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001367{
Bob Copeland1c5256b2009-08-24 23:00:32 -04001368 struct ieee80211_rx_status *rxs;
Bruno Randolf8a89f062010-06-16 19:11:51 +09001369
Bruno Randolf8a89f062010-06-16 19:11:51 +09001370 ath5k_remove_padding(skb);
1371
1372 rxs = IEEE80211_SKB_RXCB(skb);
1373
1374 rxs->flag = 0;
1375 if (unlikely(rs->rs_status & AR5K_RXERR_MIC))
1376 rxs->flag |= RX_FLAG_MMIC_ERROR;
1377
1378 /*
1379 * always extend the mac timestamp, since this information is
1380 * also needed for proper IBSS merging.
1381 *
1382 * XXX: it might be too late to do it here, since rs_tstamp is
1383 * 15bit only. that means TSF extension has to be done within
1384 * 32768usec (about 32ms). it might be necessary to move this to
1385 * the interrupt handler, like it is done in madwifi.
1386 *
1387 * Unfortunately we don't know when the hardware takes the rx
1388 * timestamp (beginning of phy frame, data frame, end of rx?).
1389 * The only thing we know is that it is hardware specific...
1390 * On AR5213 it seems the rx timestamp is at the end of the
1391 * frame, but i'm not sure.
1392 *
1393 * NOTE: mac80211 defines mactime at the beginning of the first
1394 * data symbol. Since we don't have any time references it's
1395 * impossible to comply to that. This affects IBSS merge only
1396 * right now, so it's not too bad...
1397 */
1398 rxs->mactime = ath5k_extend_tsf(sc->ah, rs->rs_tstamp);
1399 rxs->flag |= RX_FLAG_TSFT;
1400
1401 rxs->freq = sc->curchan->center_freq;
1402 rxs->band = sc->curband->band;
1403
1404 rxs->signal = sc->ah->ah_noise_floor + rs->rs_rssi;
1405
1406 rxs->antenna = rs->rs_antenna;
1407
1408 if (rs->rs_antenna > 0 && rs->rs_antenna < 5)
1409 sc->stats.antenna_rx[rs->rs_antenna]++;
1410 else
1411 sc->stats.antenna_rx[0]++; /* invalid */
1412
1413 rxs->rate_idx = ath5k_hw_to_driver_rix(sc, rs->rs_rate);
1414 rxs->flag |= ath5k_rx_decrypted(sc, skb, rs);
1415
1416 if (rxs->rate_idx >= 0 && rs->rs_rate ==
1417 sc->curband->bitrates[rxs->rate_idx].hw_value_short)
1418 rxs->flag |= RX_FLAG_SHORTPRE;
1419
1420 ath5k_debug_dump_skb(sc, skb, "RX ", 0);
1421
1422 ath5k_update_beacon_rssi(sc, skb, rs->rs_rssi);
1423
1424 /* check beacons in IBSS mode */
1425 if (sc->opmode == NL80211_IFTYPE_ADHOC)
1426 ath5k_check_ibss_tsf(sc, skb, rxs);
1427
1428 ieee80211_rx(sc->hw, skb);
1429}
1430
Bruno Randolf02a78b42010-06-16 19:11:56 +09001431/** ath5k_frame_receive_ok() - Do we want to receive this frame or not?
1432 *
1433 * Check if we want to further process this frame or not. Also update
1434 * statistics. Return true if we want this frame, false if not.
1435 */
1436static bool
1437ath5k_receive_frame_ok(struct ath5k_softc *sc, struct ath5k_rx_status *rs)
1438{
1439 sc->stats.rx_all_count++;
Ben Greearb72acdd2010-10-01 10:54:04 -07001440 sc->stats.rx_bytes_count += rs->rs_datalen;
Bruno Randolf02a78b42010-06-16 19:11:56 +09001441
1442 if (unlikely(rs->rs_status)) {
1443 if (rs->rs_status & AR5K_RXERR_CRC)
1444 sc->stats.rxerr_crc++;
1445 if (rs->rs_status & AR5K_RXERR_FIFO)
1446 sc->stats.rxerr_fifo++;
1447 if (rs->rs_status & AR5K_RXERR_PHY) {
1448 sc->stats.rxerr_phy++;
1449 if (rs->rs_phyerr > 0 && rs->rs_phyerr < 32)
1450 sc->stats.rxerr_phy_code[rs->rs_phyerr]++;
1451 return false;
1452 }
1453 if (rs->rs_status & AR5K_RXERR_DECRYPT) {
1454 /*
1455 * Decrypt error. If the error occurred
1456 * because there was no hardware key, then
1457 * let the frame through so the upper layers
1458 * can process it. This is necessary for 5210
1459 * parts which have no way to setup a ``clear''
1460 * key cache entry.
1461 *
1462 * XXX do key cache faulting
1463 */
1464 sc->stats.rxerr_decrypt++;
1465 if (rs->rs_keyix == AR5K_RXKEYIX_INVALID &&
1466 !(rs->rs_status & AR5K_RXERR_CRC))
1467 return true;
1468 }
1469 if (rs->rs_status & AR5K_RXERR_MIC) {
1470 sc->stats.rxerr_mic++;
1471 return true;
1472 }
1473
Bob Copeland23538c22010-08-15 13:03:13 -04001474 /* reject any frames with non-crypto errors */
1475 if (rs->rs_status & ~(AR5K_RXERR_DECRYPT))
Bruno Randolf02a78b42010-06-16 19:11:56 +09001476 return false;
1477 }
1478
1479 if (unlikely(rs->rs_more)) {
1480 sc->stats.rxerr_jumbo++;
1481 return false;
1482 }
1483 return true;
1484}
1485
Bruno Randolf8a89f062010-06-16 19:11:51 +09001486static void
1487ath5k_tasklet_rx(unsigned long data)
1488{
Bruno Randolfb47f4072008-03-05 18:35:45 +09001489 struct ath5k_rx_status rs = {};
Bob Copelandb6ea0352009-01-10 14:42:54 -05001490 struct sk_buff *skb, *next_skb;
1491 dma_addr_t next_skb_addr;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001492 struct ath5k_softc *sc = (void *)data;
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -08001493 struct ath5k_hw *ah = sc->ah;
1494 struct ath_common *common = ath5k_hw_common(ah);
Bob Copelandc57ca812009-04-15 07:57:35 -04001495 struct ath5k_buf *bf;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001496 struct ath5k_desc *ds;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001497 int ret;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001498
1499 spin_lock(&sc->rxbuflock);
Jiri Slaby3a0f2c82008-07-15 17:44:18 +02001500 if (list_empty(&sc->rxbuf)) {
1501 ATH5K_WARN(sc, "empty rx buf pool\n");
1502 goto unlock;
1503 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001504 do {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001505 bf = list_first_entry(&sc->rxbuf, struct ath5k_buf, list);
1506 BUG_ON(bf->skb == NULL);
1507 skb = bf->skb;
1508 ds = bf->desc;
1509
Bob Copelandc57ca812009-04-15 07:57:35 -04001510 /* bail if HW is still using self-linked descriptor */
1511 if (ath5k_hw_get_rxdp(sc->ah) == bf->daddr)
1512 break;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001513
Bruno Randolfb47f4072008-03-05 18:35:45 +09001514 ret = sc->ah->ah_proc_rx_desc(sc->ah, ds, &rs);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001515 if (unlikely(ret == -EINPROGRESS))
1516 break;
1517 else if (unlikely(ret)) {
1518 ATH5K_ERR(sc, "error in processing rx descriptor\n");
Bruno Randolf76443952010-03-09 16:56:00 +09001519 sc->stats.rxerr_proc++;
Bruno Randolfb16062f2010-06-16 19:11:46 +09001520 break;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001521 }
1522
Bruno Randolf02a78b42010-06-16 19:11:56 +09001523 if (ath5k_receive_frame_ok(sc, &rs)) {
1524 next_skb = ath5k_rx_skb_alloc(sc, &next_skb_addr);
Bruno Randolf76443952010-03-09 16:56:00 +09001525
Bruno Randolf02a78b42010-06-16 19:11:56 +09001526 /*
1527 * If we can't replace bf->skb with a new skb under
1528 * memory pressure, just skip this packet
1529 */
1530 if (!next_skb)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001531 goto next;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001532
Bruno Randolf02a78b42010-06-16 19:11:56 +09001533 pci_unmap_single(sc->pdev, bf->skbaddr,
1534 common->rx_bufsize,
1535 PCI_DMA_FROMDEVICE);
1536
1537 skb_put(skb, rs.rs_datalen);
1538
1539 ath5k_receive_frame(sc, skb, &rs);
1540
1541 bf->skb = next_skb;
1542 bf->skbaddr = next_skb_addr;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001543 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001544next:
1545 list_move_tail(&bf->list, &sc->rxbuf);
1546 } while (ath5k_rxbuf_setup(sc, bf) == 0);
Jiri Slaby3a0f2c82008-07-15 17:44:18 +02001547unlock:
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001548 spin_unlock(&sc->rxbuflock);
1549}
1550
1551
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001552/*************\
1553* TX Handling *
1554\*************/
1555
Bob Copeland8a63fac2010-09-17 12:45:07 +09001556static int ath5k_tx_queue(struct ieee80211_hw *hw, struct sk_buff *skb,
1557 struct ath5k_txq *txq)
1558{
1559 struct ath5k_softc *sc = hw->priv;
1560 struct ath5k_buf *bf;
1561 unsigned long flags;
1562 int padsize;
1563
1564 ath5k_debug_dump_skb(sc, skb, "TX ", 1);
1565
1566 /*
1567 * The hardware expects the header padded to 4 byte boundaries.
1568 * If this is not the case, we add the padding after the header.
1569 */
1570 padsize = ath5k_add_padding(skb);
1571 if (padsize < 0) {
1572 ATH5K_ERR(sc, "tx hdrlen not %%4: not enough"
1573 " headroom to pad");
1574 goto drop_packet;
1575 }
1576
Bruno Randolf925e0b02010-09-17 11:36:35 +09001577 if (txq->txq_len >= ATH5K_TXQ_LEN_MAX)
1578 ieee80211_stop_queue(hw, txq->qnum);
1579
Bob Copeland8a63fac2010-09-17 12:45:07 +09001580 spin_lock_irqsave(&sc->txbuflock, flags);
1581 if (list_empty(&sc->txbuf)) {
1582 ATH5K_ERR(sc, "no further txbuf available, dropping packet\n");
1583 spin_unlock_irqrestore(&sc->txbuflock, flags);
Bruno Randolf651d9372010-09-17 11:36:46 +09001584 ieee80211_stop_queues(hw);
Bob Copeland8a63fac2010-09-17 12:45:07 +09001585 goto drop_packet;
1586 }
1587 bf = list_first_entry(&sc->txbuf, struct ath5k_buf, list);
1588 list_del(&bf->list);
1589 sc->txbuf_len--;
1590 if (list_empty(&sc->txbuf))
1591 ieee80211_stop_queues(hw);
1592 spin_unlock_irqrestore(&sc->txbuflock, flags);
1593
1594 bf->skb = skb;
1595
1596 if (ath5k_txbuf_setup(sc, bf, txq, padsize)) {
1597 bf->skb = NULL;
1598 spin_lock_irqsave(&sc->txbuflock, flags);
1599 list_add_tail(&bf->list, &sc->txbuf);
1600 sc->txbuf_len++;
1601 spin_unlock_irqrestore(&sc->txbuflock, flags);
1602 goto drop_packet;
1603 }
1604 return NETDEV_TX_OK;
1605
1606drop_packet:
1607 dev_kfree_skb_any(skb);
1608 return NETDEV_TX_OK;
1609}
1610
Bruno Randolf14404012010-09-17 11:36:51 +09001611static void
1612ath5k_tx_frame_completed(struct ath5k_softc *sc, struct sk_buff *skb,
1613 struct ath5k_tx_status *ts)
1614{
1615 struct ieee80211_tx_info *info;
1616 int i;
1617
1618 sc->stats.tx_all_count++;
Ben Greearb72acdd2010-10-01 10:54:04 -07001619 sc->stats.tx_bytes_count += skb->len;
Bruno Randolf14404012010-09-17 11:36:51 +09001620 info = IEEE80211_SKB_CB(skb);
1621
1622 ieee80211_tx_info_clear_status(info);
1623 for (i = 0; i < 4; i++) {
1624 struct ieee80211_tx_rate *r =
1625 &info->status.rates[i];
1626
1627 if (ts->ts_rate[i]) {
1628 r->idx = ath5k_hw_to_driver_rix(sc, ts->ts_rate[i]);
1629 r->count = ts->ts_retry[i];
1630 } else {
1631 r->idx = -1;
1632 r->count = 0;
1633 }
1634 }
1635
1636 /* count the successful attempt as well */
1637 info->status.rates[ts->ts_final_idx].count++;
1638
1639 if (unlikely(ts->ts_status)) {
1640 sc->stats.ack_fail++;
1641 if (ts->ts_status & AR5K_TXERR_FILT) {
1642 info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
1643 sc->stats.txerr_filt++;
1644 }
1645 if (ts->ts_status & AR5K_TXERR_XRETRY)
1646 sc->stats.txerr_retry++;
1647 if (ts->ts_status & AR5K_TXERR_FIFO)
1648 sc->stats.txerr_fifo++;
1649 } else {
1650 info->flags |= IEEE80211_TX_STAT_ACK;
1651 info->status.ack_signal = ts->ts_rssi;
1652 }
1653
1654 /*
1655 * Remove MAC header padding before giving the frame
1656 * back to mac80211.
1657 */
1658 ath5k_remove_padding(skb);
1659
1660 if (ts->ts_antenna > 0 && ts->ts_antenna < 5)
1661 sc->stats.antenna_tx[ts->ts_antenna]++;
1662 else
1663 sc->stats.antenna_tx[0]++; /* invalid */
1664
1665 ieee80211_tx_status(sc->hw, skb);
1666}
Bob Copeland8a63fac2010-09-17 12:45:07 +09001667
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001668static void
1669ath5k_tx_processq(struct ath5k_softc *sc, struct ath5k_txq *txq)
1670{
Bruno Randolfb47f4072008-03-05 18:35:45 +09001671 struct ath5k_tx_status ts = {};
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001672 struct ath5k_buf *bf, *bf0;
1673 struct ath5k_desc *ds;
1674 struct sk_buff *skb;
Bruno Randolf14404012010-09-17 11:36:51 +09001675 int ret;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001676
1677 spin_lock(&txq->lock);
1678 list_for_each_entry_safe(bf, bf0, &txq->q, list) {
Bruno Randolf23413292010-09-17 11:37:07 +09001679
1680 txq->txq_poll_mark = false;
1681
1682 /* skb might already have been processed last time. */
1683 if (bf->skb != NULL) {
1684 ds = bf->desc;
1685
1686 ret = sc->ah->ah_proc_tx_desc(sc->ah, ds, &ts);
1687 if (unlikely(ret == -EINPROGRESS))
1688 break;
1689 else if (unlikely(ret)) {
1690 ATH5K_ERR(sc,
1691 "error %d while processing "
1692 "queue %u\n", ret, txq->qnum);
1693 break;
1694 }
1695
1696 skb = bf->skb;
1697 bf->skb = NULL;
1698 pci_unmap_single(sc->pdev, bf->skbaddr, skb->len,
1699 PCI_DMA_TODEVICE);
1700 ath5k_tx_frame_completed(sc, skb, &ts);
1701 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001702
Bob Copelanda05988b2010-04-07 23:55:58 -04001703 /*
1704 * It's possible that the hardware can say the buffer is
1705 * completed when it hasn't yet loaded the ds_link from
Bruno Randolf23413292010-09-17 11:37:07 +09001706 * host memory and moved on.
1707 * Always keep the last descriptor to avoid HW races...
Bob Copelanda05988b2010-04-07 23:55:58 -04001708 */
Bruno Randolf23413292010-09-17 11:37:07 +09001709 if (ath5k_hw_get_txdp(sc->ah, txq->qnum) != bf->daddr) {
1710 spin_lock(&sc->txbuflock);
1711 list_move_tail(&bf->list, &sc->txbuf);
1712 sc->txbuf_len++;
1713 txq->txq_len--;
1714 spin_unlock(&sc->txbuflock);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001715 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001716 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001717 spin_unlock(&txq->lock);
Bruno Randolf4198a8d2010-10-05 13:27:17 +09001718 if (txq->txq_len < ATH5K_TXQ_LEN_LOW && txq->qnum < 4)
Bruno Randolf925e0b02010-09-17 11:36:35 +09001719 ieee80211_wake_queue(sc->hw, txq->qnum);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001720}
1721
1722static void
1723ath5k_tasklet_tx(unsigned long data)
1724{
Bob Copeland8784d2e2009-07-29 17:32:28 -04001725 int i;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001726 struct ath5k_softc *sc = (void *)data;
1727
Bob Copeland8784d2e2009-07-29 17:32:28 -04001728 for (i=0; i < AR5K_NUM_TX_QUEUES; i++)
1729 if (sc->txqs[i].setup && (sc->ah->ah_txq_isr & BIT(i)))
1730 ath5k_tx_processq(sc, &sc->txqs[i]);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001731}
1732
1733
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001734/*****************\
1735* Beacon handling *
1736\*****************/
1737
1738/*
1739 * Setup the beacon frame for transmit.
1740 */
1741static int
Johannes Berge039fa42008-05-15 12:55:29 +02001742ath5k_beacon_setup(struct ath5k_softc *sc, struct ath5k_buf *bf)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001743{
1744 struct sk_buff *skb = bf->skb;
Johannes Berga888d522008-05-26 16:43:39 +02001745 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001746 struct ath5k_hw *ah = sc->ah;
1747 struct ath5k_desc *ds;
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04001748 int ret = 0;
1749 u8 antenna;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001750 u32 flags;
Benoit Papillault8127fbd2010-02-27 23:05:26 +01001751 const int padsize = 0;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001752
1753 bf->skbaddr = pci_map_single(sc->pdev, skb->data, skb->len,
1754 PCI_DMA_TODEVICE);
1755 ATH5K_DBG(sc, ATH5K_DEBUG_BEACON, "skb %p [data %p len %u] "
1756 "skbaddr %llx\n", skb, skb->data, skb->len,
1757 (unsigned long long)bf->skbaddr);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -07001758 if (pci_dma_mapping_error(sc->pdev, bf->skbaddr)) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001759 ATH5K_ERR(sc, "beacon DMA mapping failed\n");
1760 return -EIO;
1761 }
1762
1763 ds = bf->desc;
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04001764 antenna = ah->ah_tx_ant;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001765
1766 flags = AR5K_TXDESC_NOACK;
Johannes Berg05c914f2008-09-11 00:01:58 +02001767 if (sc->opmode == NL80211_IFTYPE_ADHOC && ath5k_hw_hasveol(ah)) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001768 ds->ds_link = bf->daddr; /* self-linked */
1769 flags |= AR5K_TXDESC_VEOL;
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04001770 } else
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001771 ds->ds_link = 0;
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04001772
1773 /*
1774 * If we use multiple antennas on AP and use
1775 * the Sectored AP scenario, switch antenna every
1776 * 4 beacons to make sure everybody hears our AP.
1777 * When a client tries to associate, hw will keep
1778 * track of the tx antenna to be used for this client
1779 * automaticaly, based on ACKed packets.
1780 *
1781 * Note: AP still listens and transmits RTS on the
1782 * default antenna which is supposed to be an omni.
1783 *
1784 * Note2: On sectored scenarios it's possible to have
Bob Copelanda180a132010-08-15 13:03:12 -04001785 * multiple antennas (1 omni -- the default -- and 14
1786 * sectors), so if we choose to actually support this
1787 * mode, we need to allow the user to set how many antennas
1788 * we have and tweak the code below to send beacons
1789 * on all of them.
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04001790 */
1791 if (ah->ah_ant_mode == AR5K_ANTMODE_SECTOR_AP)
1792 antenna = sc->bsent & 4 ? 2 : 1;
1793
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001794
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02001795 /* FIXME: If we are in g mode and rate is a CCK rate
1796 * subtract ah->ah_txpower.txp_cck_ofdm_pwr_delta
1797 * from tx power (value is in dB units already) */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001798 ds->ds_data = bf->skbaddr;
Bruno Randolf281c56d2008-02-05 18:44:55 +09001799 ret = ah->ah_setup_tx_desc(ah, ds, skb->len,
Benoit Papillault8127fbd2010-02-27 23:05:26 +01001800 ieee80211_get_hdrlen_from_skb(skb), padsize,
Luis R. Rodriguez400ec452008-02-03 21:51:49 -05001801 AR5K_PKT_TYPE_BEACON, (sc->power_level * 2),
Johannes Berge039fa42008-05-15 12:55:29 +02001802 ieee80211_get_tx_rate(sc->hw, info)->hw_value,
Johannes Berg2e92e6f2008-05-15 12:55:27 +02001803 1, AR5K_TXKEYIX_INVALID,
Luis R. Rodriguez400ec452008-02-03 21:51:49 -05001804 antenna, flags, 0, 0);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001805 if (ret)
1806 goto err_unmap;
1807
1808 return 0;
1809err_unmap:
1810 pci_unmap_single(sc->pdev, bf->skbaddr, skb->len, PCI_DMA_TODEVICE);
1811 return ret;
1812}
1813
1814/*
Bob Copeland8a63fac2010-09-17 12:45:07 +09001815 * Updates the beacon that is sent by ath5k_beacon_send. For adhoc,
1816 * this is called only once at config_bss time, for AP we do it every
1817 * SWBA interrupt so that the TIM will reflect buffered frames.
1818 *
1819 * Called with the beacon lock.
1820 */
1821static int
1822ath5k_beacon_update(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1823{
1824 int ret;
1825 struct ath5k_softc *sc = hw->priv;
Ben Greearb1ae1ed2010-09-30 12:22:58 -07001826 struct ath5k_vif *avf = (void *)vif->drv_priv;
Bob Copeland8a63fac2010-09-17 12:45:07 +09001827 struct sk_buff *skb;
1828
1829 if (WARN_ON(!vif)) {
1830 ret = -EINVAL;
1831 goto out;
1832 }
1833
1834 skb = ieee80211_beacon_get(hw, vif);
1835
1836 if (!skb) {
1837 ret = -ENOMEM;
1838 goto out;
1839 }
1840
1841 ath5k_debug_dump_skb(sc, skb, "BC ", 1);
1842
Ben Greearb1ae1ed2010-09-30 12:22:58 -07001843 ath5k_txbuf_free_skb(sc, avf->bbuf);
1844 avf->bbuf->skb = skb;
1845 ret = ath5k_beacon_setup(sc, avf->bbuf);
Bob Copeland8a63fac2010-09-17 12:45:07 +09001846 if (ret)
Ben Greearb1ae1ed2010-09-30 12:22:58 -07001847 avf->bbuf->skb = NULL;
Bob Copeland8a63fac2010-09-17 12:45:07 +09001848out:
1849 return ret;
1850}
1851
1852/*
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001853 * Transmit a beacon frame at SWBA. Dynamic updates to the
1854 * frame contents are done as needed and the slot time is
1855 * also adjusted based on current state.
1856 *
Bob Copeland5faaff72010-07-13 11:32:40 -04001857 * This is called from software irq context (beacontq tasklets)
1858 * or user context from ath5k_beacon_config.
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001859 */
1860static void
1861ath5k_beacon_send(struct ath5k_softc *sc)
1862{
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001863 struct ath5k_hw *ah = sc->ah;
Ben Greearb1ae1ed2010-09-30 12:22:58 -07001864 struct ieee80211_vif *vif;
1865 struct ath5k_vif *avf;
1866 struct ath5k_buf *bf;
Bob Copelandcec8db22009-07-04 12:59:51 -04001867 struct sk_buff *skb;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001868
Bruno Randolfbe9b7252008-01-23 10:27:51 +09001869 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON, "in beacon_send\n");
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001870
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001871 /*
1872 * Check if the previous beacon has gone out. If
Bob Copelanda180a132010-08-15 13:03:12 -04001873 * not, don't don't try to post another: skip this
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001874 * period and wait for the next. Missed beacons
1875 * indicate a problem and should not occur. If we
1876 * miss too many consecutive beacons reset the device.
1877 */
1878 if (unlikely(ath5k_hw_num_tx_pending(ah, sc->bhalq) != 0)) {
1879 sc->bmisscount++;
Bruno Randolfbe9b7252008-01-23 10:27:51 +09001880 ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001881 "missed %u consecutive beacons\n", sc->bmisscount);
Nick Kossifidis428cbd42009-04-30 15:55:47 -04001882 if (sc->bmisscount > 10) { /* NB: 10 is a guess */
Bruno Randolfbe9b7252008-01-23 10:27:51 +09001883 ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001884 "stuck beacon time (%u missed)\n",
1885 sc->bmisscount);
Bruno Randolf8d67a032010-06-16 19:11:12 +09001886 ATH5K_DBG(sc, ATH5K_DEBUG_RESET,
1887 "stuck beacon, resetting\n");
Bob Copeland5faaff72010-07-13 11:32:40 -04001888 ieee80211_queue_work(sc->hw, &sc->reset_work);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001889 }
1890 return;
1891 }
1892 if (unlikely(sc->bmisscount != 0)) {
Bruno Randolfbe9b7252008-01-23 10:27:51 +09001893 ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001894 "resume beacon xmit after %u misses\n",
1895 sc->bmisscount);
1896 sc->bmisscount = 0;
1897 }
1898
Ben Greearb1ae1ed2010-09-30 12:22:58 -07001899 if (sc->opmode == NL80211_IFTYPE_AP && sc->num_ap_vifs > 1) {
1900 u64 tsf = ath5k_hw_get_tsf64(ah);
1901 u32 tsftu = TSF_TO_TU(tsf);
1902 int slot = ((tsftu % sc->bintval) * ATH_BCBUF) / sc->bintval;
1903 vif = sc->bslot[(slot + 1) % ATH_BCBUF];
1904 ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
1905 "tsf %llx tsftu %x intval %u slot %u vif %p\n",
1906 (unsigned long long)tsf, tsftu, sc->bintval, slot, vif);
1907 } else /* only one interface */
1908 vif = sc->bslot[0];
1909
1910 if (!vif)
1911 return;
1912
1913 avf = (void *)vif->drv_priv;
1914 bf = avf->bbuf;
1915 if (unlikely(bf->skb == NULL || sc->opmode == NL80211_IFTYPE_STATION ||
1916 sc->opmode == NL80211_IFTYPE_MONITOR)) {
1917 ATH5K_WARN(sc, "bf=%p bf_skb=%p\n", bf, bf ? bf->skb : NULL);
1918 return;
1919 }
1920
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001921 /*
1922 * Stop any current dma and put the new frame on the queue.
1923 * This should never fail since we check above that no frames
1924 * are still pending on the queue.
1925 */
Nick Kossifidis14fae2d2010-11-23 20:55:17 +02001926 if (unlikely(ath5k_hw_stop_beacon_queue(ah, sc->bhalq))) {
Nick Kossifidis428cbd42009-04-30 15:55:47 -04001927 ATH5K_WARN(sc, "beacon queue %u didn't start/stop ?\n", sc->bhalq);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001928 /* NB: hw still stops DMA, so proceed */
1929 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001930
Bob Copeland1071db82009-05-18 10:59:52 -04001931 /* refresh the beacon for AP mode */
1932 if (sc->opmode == NL80211_IFTYPE_AP)
Ben Greearb1ae1ed2010-09-30 12:22:58 -07001933 ath5k_beacon_update(sc->hw, vif);
Bob Copeland1071db82009-05-18 10:59:52 -04001934
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001935 ath5k_hw_set_txdp(ah, sc->bhalq, bf->daddr);
1936 ath5k_hw_start_tx_dma(ah, sc->bhalq);
Bruno Randolfbe9b7252008-01-23 10:27:51 +09001937 ATH5K_DBG(sc, ATH5K_DEBUG_BEACON, "TXDP[%u] = %llx (%p)\n",
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001938 sc->bhalq, (unsigned long long)bf->daddr, bf->desc);
1939
Ben Greearb1ae1ed2010-09-30 12:22:58 -07001940 skb = ieee80211_get_buffered_bc(sc->hw, vif);
Bob Copelandcec8db22009-07-04 12:59:51 -04001941 while (skb) {
1942 ath5k_tx_queue(sc->hw, skb, sc->cabq);
Ben Greearb1ae1ed2010-09-30 12:22:58 -07001943 skb = ieee80211_get_buffered_bc(sc->hw, vif);
Bob Copelandcec8db22009-07-04 12:59:51 -04001944 }
1945
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001946 sc->bsent++;
1947}
1948
Bruno Randolf9804b982008-01-19 18:17:59 +09001949/**
1950 * ath5k_beacon_update_timers - update beacon timers
1951 *
1952 * @sc: struct ath5k_softc pointer we are operating on
1953 * @bc_tsf: the timestamp of the beacon. 0 to reset the TSF. -1 to perform a
1954 * beacon timer update based on the current HW TSF.
1955 *
1956 * Calculate the next target beacon transmit time (TBTT) based on the timestamp
1957 * of a received beacon or the current local hardware TSF and write it to the
1958 * beacon timer registers.
1959 *
1960 * This is called in a variety of situations, e.g. when a beacon is received,
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001961 * when a TSF update has been detected, but also when an new IBSS is created or
Bruno Randolf9804b982008-01-19 18:17:59 +09001962 * when we otherwise know we have to update the timers, but we keep it in this
1963 * function to have it all together in one place.
1964 */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001965static void
Bruno Randolf9804b982008-01-19 18:17:59 +09001966ath5k_beacon_update_timers(struct ath5k_softc *sc, u64 bc_tsf)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001967{
1968 struct ath5k_hw *ah = sc->ah;
Bruno Randolf9804b982008-01-19 18:17:59 +09001969 u32 nexttbtt, intval, hw_tu, bc_tu;
1970 u64 hw_tsf;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001971
1972 intval = sc->bintval & AR5K_BEACON_PERIOD;
Ben Greearb1ae1ed2010-09-30 12:22:58 -07001973 if (sc->opmode == NL80211_IFTYPE_AP && sc->num_ap_vifs > 1) {
1974 intval /= ATH_BCBUF; /* staggered multi-bss beacons */
1975 if (intval < 15)
1976 ATH5K_WARN(sc, "intval %u is too low, min 15\n",
1977 intval);
1978 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001979 if (WARN_ON(!intval))
1980 return;
1981
Bruno Randolf9804b982008-01-19 18:17:59 +09001982 /* beacon TSF converted to TU */
1983 bc_tu = TSF_TO_TU(bc_tsf);
1984
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001985 /* current TSF converted to TU */
Bruno Randolf9804b982008-01-19 18:17:59 +09001986 hw_tsf = ath5k_hw_get_tsf64(ah);
1987 hw_tu = TSF_TO_TU(hw_tsf);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001988
Bruno Randolf11f21df2010-09-27 12:22:26 +09001989#define FUDGE AR5K_TUNE_SW_BEACON_RESP + 3
1990 /* We use FUDGE to make sure the next TBTT is ahead of the current TU.
1991 * Since we later substract AR5K_TUNE_SW_BEACON_RESP (10) in the timer
1992 * configuration we need to make sure it is bigger than that. */
1993
Bruno Randolf9804b982008-01-19 18:17:59 +09001994 if (bc_tsf == -1) {
1995 /*
1996 * no beacons received, called internally.
1997 * just need to refresh timers based on HW TSF.
1998 */
1999 nexttbtt = roundup(hw_tu + FUDGE, intval);
2000 } else if (bc_tsf == 0) {
2001 /*
2002 * no beacon received, probably called by ath5k_reset_tsf().
2003 * reset TSF to start with 0.
2004 */
2005 nexttbtt = intval;
2006 intval |= AR5K_BEACON_RESET_TSF;
2007 } else if (bc_tsf > hw_tsf) {
2008 /*
2009 * beacon received, SW merge happend but HW TSF not yet updated.
2010 * not possible to reconfigure timers yet, but next time we
2011 * receive a beacon with the same BSSID, the hardware will
2012 * automatically update the TSF and then we need to reconfigure
2013 * the timers.
2014 */
2015 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
2016 "need to wait for HW TSF sync\n");
2017 return;
2018 } else {
2019 /*
2020 * most important case for beacon synchronization between STA.
2021 *
2022 * beacon received and HW TSF has been already updated by HW.
2023 * update next TBTT based on the TSF of the beacon, but make
2024 * sure it is ahead of our local TSF timer.
2025 */
2026 nexttbtt = bc_tu + roundup(hw_tu + FUDGE - bc_tu, intval);
2027 }
2028#undef FUDGE
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002029
Bruno Randolf036cd1e2008-01-19 18:18:21 +09002030 sc->nexttbtt = nexttbtt;
2031
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002032 intval |= AR5K_BEACON_ENA;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002033 ath5k_hw_init_beacon(ah, nexttbtt, intval);
Bruno Randolf9804b982008-01-19 18:17:59 +09002034
2035 /*
2036 * debugging output last in order to preserve the time critical aspect
2037 * of this function
2038 */
2039 if (bc_tsf == -1)
2040 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
2041 "reconfigured timers based on HW TSF\n");
2042 else if (bc_tsf == 0)
2043 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
2044 "reset HW TSF and timers\n");
2045 else
2046 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
2047 "updated timers based on beacon TSF\n");
2048
2049 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
David Miller04f93a82008-02-15 16:08:59 -08002050 "bc_tsf %llx hw_tsf %llx bc_tu %u hw_tu %u nexttbtt %u\n",
2051 (unsigned long long) bc_tsf,
2052 (unsigned long long) hw_tsf, bc_tu, hw_tu, nexttbtt);
Bruno Randolf9804b982008-01-19 18:17:59 +09002053 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON, "intval %u %s %s\n",
2054 intval & AR5K_BEACON_PERIOD,
2055 intval & AR5K_BEACON_ENA ? "AR5K_BEACON_ENA" : "",
2056 intval & AR5K_BEACON_RESET_TSF ? "AR5K_BEACON_RESET_TSF" : "");
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002057}
2058
Bruno Randolf036cd1e2008-01-19 18:18:21 +09002059/**
2060 * ath5k_beacon_config - Configure the beacon queues and interrupts
2061 *
2062 * @sc: struct ath5k_softc pointer we are operating on
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002063 *
Bruno Randolf036cd1e2008-01-19 18:18:21 +09002064 * In IBSS mode we use a self-linked tx descriptor if possible. We enable SWBA
Bruno Randolf6ba81c22008-03-05 18:36:26 +09002065 * interrupts to detect TSF updates only.
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002066 */
2067static void
2068ath5k_beacon_config(struct ath5k_softc *sc)
2069{
2070 struct ath5k_hw *ah = sc->ah;
Bob Copelandb5f03952009-02-15 12:06:10 -05002071 unsigned long flags;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002072
Bob Copeland21800492009-07-04 12:59:52 -04002073 spin_lock_irqsave(&sc->block, flags);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002074 sc->bmisscount = 0;
Jiri Slabydc1968e2008-07-23 13:17:34 +02002075 sc->imask &= ~(AR5K_INT_BMISS | AR5K_INT_SWBA);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002076
Bob Copeland21800492009-07-04 12:59:52 -04002077 if (sc->enable_beacon) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002078 /*
Bruno Randolf036cd1e2008-01-19 18:18:21 +09002079 * In IBSS mode we use a self-linked tx descriptor and let the
2080 * hardware send the beacons automatically. We have to load it
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002081 * only once here.
Bruno Randolf036cd1e2008-01-19 18:18:21 +09002082 * We use the SWBA interrupt only to keep track of the beacon
Bruno Randolf6ba81c22008-03-05 18:36:26 +09002083 * timers in order to detect automatic TSF updates.
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002084 */
2085 ath5k_beaconq_config(sc);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002086
Bruno Randolf036cd1e2008-01-19 18:18:21 +09002087 sc->imask |= AR5K_INT_SWBA;
2088
Jiri Slabyda966bc2008-10-12 22:54:10 +02002089 if (sc->opmode == NL80211_IFTYPE_ADHOC) {
Bob Copeland21800492009-07-04 12:59:52 -04002090 if (ath5k_hw_hasveol(ah))
Jiri Slabyda966bc2008-10-12 22:54:10 +02002091 ath5k_beacon_send(sc);
Jiri Slabyda966bc2008-10-12 22:54:10 +02002092 } else
2093 ath5k_beacon_update_timers(sc, -1);
Bob Copeland21800492009-07-04 12:59:52 -04002094 } else {
Nick Kossifidis14fae2d2010-11-23 20:55:17 +02002095 ath5k_hw_stop_beacon_queue(sc->ah, sc->bhalq);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002096 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002097
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03002098 ath5k_hw_set_imr(ah, sc->imask);
Bob Copeland21800492009-07-04 12:59:52 -04002099 mmiowb();
2100 spin_unlock_irqrestore(&sc->block, flags);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002101}
2102
Nick Kossifidis428cbd42009-04-30 15:55:47 -04002103static void ath5k_tasklet_beacon(unsigned long data)
2104{
2105 struct ath5k_softc *sc = (struct ath5k_softc *) data;
2106
2107 /*
2108 * Software beacon alert--time to send a beacon.
2109 *
2110 * In IBSS mode we use this interrupt just to
2111 * keep track of the next TBTT (target beacon
2112 * transmission time) in order to detect wether
2113 * automatic TSF updates happened.
2114 */
2115 if (sc->opmode == NL80211_IFTYPE_ADHOC) {
2116 /* XXX: only if VEOL suppported */
2117 u64 tsf = ath5k_hw_get_tsf64(sc->ah);
2118 sc->nexttbtt += sc->bintval;
2119 ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
2120 "SWBA nexttbtt: %x hw_tu: %x "
2121 "TSF: %llx\n",
2122 sc->nexttbtt,
2123 TSF_TO_TU(tsf),
2124 (unsigned long long) tsf);
2125 } else {
2126 spin_lock(&sc->block);
2127 ath5k_beacon_send(sc);
2128 spin_unlock(&sc->block);
2129 }
2130}
2131
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002132
2133/********************\
2134* Interrupt handling *
2135\********************/
2136
Bruno Randolf6a8a3f62010-03-25 14:49:19 +09002137static void
2138ath5k_intr_calibration_poll(struct ath5k_hw *ah)
2139{
Bruno Randolf2111ac02010-04-02 18:44:08 +09002140 if (time_is_before_eq_jiffies(ah->ah_cal_next_ani) &&
2141 !(ah->ah_cal_mask & AR5K_CALIBRATION_FULL)) {
2142 /* run ANI only when full calibration is not active */
2143 ah->ah_cal_next_ani = jiffies +
2144 msecs_to_jiffies(ATH5K_TUNE_CALIBRATION_INTERVAL_ANI);
2145 tasklet_schedule(&ah->ah_sc->ani_tasklet);
2146
2147 } else if (time_is_before_eq_jiffies(ah->ah_cal_next_full)) {
Bruno Randolf6a8a3f62010-03-25 14:49:19 +09002148 ah->ah_cal_next_full = jiffies +
2149 msecs_to_jiffies(ATH5K_TUNE_CALIBRATION_INTERVAL_FULL);
2150 tasklet_schedule(&ah->ah_sc->calib);
2151 }
2152 /* we could use SWI to generate enough interrupts to meet our
2153 * calibration interval requirements, if necessary:
2154 * AR5K_REG_ENABLE_BITS(ah, AR5K_CR, AR5K_CR_SWI); */
2155}
2156
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002157static irqreturn_t
2158ath5k_intr(int irq, void *dev_id)
2159{
2160 struct ath5k_softc *sc = dev_id;
2161 struct ath5k_hw *ah = sc->ah;
2162 enum ath5k_int status;
2163 unsigned int counter = 1000;
2164
2165 if (unlikely(test_bit(ATH_STAT_INVALID, sc->status) ||
2166 !ath5k_hw_is_intr_pending(ah)))
2167 return IRQ_NONE;
2168
2169 do {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002170 ath5k_hw_get_isr(ah, &status); /* NB: clears IRQ too */
2171 ATH5K_DBG(sc, ATH5K_DEBUG_INTR, "status 0x%x/0x%x\n",
2172 status, sc->imask);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002173 if (unlikely(status & AR5K_INT_FATAL)) {
2174 /*
2175 * Fatal errors are unrecoverable.
2176 * Typically these are caused by DMA errors.
2177 */
Bruno Randolf8d67a032010-06-16 19:11:12 +09002178 ATH5K_DBG(sc, ATH5K_DEBUG_RESET,
2179 "fatal int, resetting\n");
Bob Copeland5faaff72010-07-13 11:32:40 -04002180 ieee80211_queue_work(sc->hw, &sc->reset_work);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002181 } else if (unlikely(status & AR5K_INT_RXORN)) {
Bruno Randolf87d77c42010-04-12 16:38:52 +09002182 /*
2183 * Receive buffers are full. Either the bus is busy or
2184 * the CPU is not fast enough to process all received
2185 * frames.
2186 * Older chipsets need a reset to come out of this
2187 * condition, but we treat it as RX for newer chips.
2188 * We don't know exactly which versions need a reset -
2189 * this guess is copied from the HAL.
2190 */
2191 sc->stats.rxorn_intr++;
Bruno Randolf8d67a032010-06-16 19:11:12 +09002192 if (ah->ah_mac_srev < AR5K_SREV_AR5212) {
2193 ATH5K_DBG(sc, ATH5K_DEBUG_RESET,
2194 "rx overrun, resetting\n");
Bob Copeland5faaff72010-07-13 11:32:40 -04002195 ieee80211_queue_work(sc->hw, &sc->reset_work);
Bruno Randolf8d67a032010-06-16 19:11:12 +09002196 }
Bruno Randolf87d77c42010-04-12 16:38:52 +09002197 else
2198 tasklet_schedule(&sc->rxtq);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002199 } else {
2200 if (status & AR5K_INT_SWBA) {
Bob Copeland56d2ac72009-04-15 07:57:33 -04002201 tasklet_hi_schedule(&sc->beacontq);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002202 }
2203 if (status & AR5K_INT_RXEOL) {
2204 /*
2205 * NB: the hardware should re-read the link when
2206 * RXE bit is written, but it doesn't work at
2207 * least on older hardware revs.
2208 */
Bruno Randolfb3f194e2010-07-14 10:53:29 +09002209 sc->stats.rxeol_intr++;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002210 }
2211 if (status & AR5K_INT_TXURN) {
2212 /* bump tx trigger level */
2213 ath5k_hw_update_tx_triglevel(ah, true);
2214 }
Nick Kossifidis4c674c62008-10-26 20:40:25 +02002215 if (status & (AR5K_INT_RXOK | AR5K_INT_RXERR))
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002216 tasklet_schedule(&sc->rxtq);
Nick Kossifidis4c674c62008-10-26 20:40:25 +02002217 if (status & (AR5K_INT_TXOK | AR5K_INT_TXDESC
2218 | AR5K_INT_TXERR | AR5K_INT_TXEOL))
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002219 tasklet_schedule(&sc->txtq);
2220 if (status & AR5K_INT_BMISS) {
Nick Kossifidis1e3e6e82009-02-09 06:15:42 +02002221 /* TODO */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002222 }
2223 if (status & AR5K_INT_MIB) {
Bruno Randolf2111ac02010-04-02 18:44:08 +09002224 sc->stats.mib_intr++;
Bruno Randolf495391d2010-03-25 14:49:36 +09002225 ath5k_hw_update_mib_counters(ah);
Bruno Randolf2111ac02010-04-02 18:44:08 +09002226 ath5k_ani_mib_intr(ah);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002227 }
Tobias Doerffele6a3b612009-06-09 17:33:27 +02002228 if (status & AR5K_INT_GPIO)
Tobias Doerffele6a3b612009-06-09 17:33:27 +02002229 tasklet_schedule(&sc->rf_kill.toggleq);
Bob Copelanda6ae0712009-06-09 23:43:11 -04002230
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002231 }
Bob Copeland2516baa2009-04-27 22:18:10 -04002232 } while (ath5k_hw_is_intr_pending(ah) && --counter > 0);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002233
2234 if (unlikely(!counter))
2235 ATH5K_WARN(sc, "too many interrupts, giving up for now\n");
2236
Bruno Randolf6a8a3f62010-03-25 14:49:19 +09002237 ath5k_intr_calibration_poll(ah);
Nick Kossifidis6e220662009-08-10 03:31:31 +03002238
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002239 return IRQ_HANDLED;
2240}
2241
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002242/*
2243 * Periodically recalibrate the PHY to account
2244 * for temperature/environment changes.
2245 */
2246static void
Nick Kossifidis6e220662009-08-10 03:31:31 +03002247ath5k_tasklet_calibrate(unsigned long data)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002248{
2249 struct ath5k_softc *sc = (void *)data;
2250 struct ath5k_hw *ah = sc->ah;
2251
Nick Kossifidis6e220662009-08-10 03:31:31 +03002252 /* Only full calibration for now */
Bruno Randolfe65e1d72010-03-25 14:49:09 +09002253 ah->ah_cal_mask |= AR5K_CALIBRATION_FULL;
Nick Kossifidis6e220662009-08-10 03:31:31 +03002254
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002255 ATH5K_DBG(sc, ATH5K_DEBUG_CALIBRATE, "channel %u/%x\n",
Luis R. Rodriguez400ec452008-02-03 21:51:49 -05002256 ieee80211_frequency_to_channel(sc->curchan->center_freq),
2257 sc->curchan->hw_value);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002258
Nick Kossifidis6f3b4142009-02-09 06:03:41 +02002259 if (ath5k_hw_gainf_calibrate(ah) == AR5K_RFGAIN_NEED_CHANGE) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002260 /*
2261 * Rfgain is out of bounds, reset the chip
2262 * to load new gain values.
2263 */
2264 ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "calibration, resetting\n");
Bob Copeland5faaff72010-07-13 11:32:40 -04002265 ieee80211_queue_work(sc->hw, &sc->reset_work);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002266 }
2267 if (ath5k_hw_phy_calibrate(ah, sc->curchan))
2268 ATH5K_ERR(sc, "calibration of channel %u failed\n",
Luis R. Rodriguez400ec452008-02-03 21:51:49 -05002269 ieee80211_frequency_to_channel(
2270 sc->curchan->center_freq));
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002271
Bruno Randolf0e8e02d2010-05-19 10:31:05 +09002272 /* Noise floor calibration interrupts rx/tx path while I/Q calibration
Bruno Randolf651d9372010-09-17 11:36:46 +09002273 * doesn't.
2274 * TODO: We should stop TX here, so that it doesn't interfere.
2275 * Note that stopping the queues is not enough to stop TX! */
Bruno Randolfafe86282010-05-19 10:31:10 +09002276 if (time_is_before_eq_jiffies(ah->ah_cal_next_nf)) {
2277 ah->ah_cal_next_nf = jiffies +
2278 msecs_to_jiffies(ATH5K_TUNE_CALIBRATION_INTERVAL_NF);
Bruno Randolfafe86282010-05-19 10:31:10 +09002279 ath5k_hw_update_noise_floor(ah);
Bruno Randolfafe86282010-05-19 10:31:10 +09002280 }
Nick Kossifidis6e220662009-08-10 03:31:31 +03002281
Bruno Randolfe65e1d72010-03-25 14:49:09 +09002282 ah->ah_cal_mask &= ~AR5K_CALIBRATION_FULL;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002283}
2284
2285
Bruno Randolf2111ac02010-04-02 18:44:08 +09002286static void
2287ath5k_tasklet_ani(unsigned long data)
2288{
2289 struct ath5k_softc *sc = (void *)data;
2290 struct ath5k_hw *ah = sc->ah;
2291
2292 ah->ah_cal_mask |= AR5K_CALIBRATION_ANI;
2293 ath5k_ani_calibration(ah);
2294 ah->ah_cal_mask &= ~AR5K_CALIBRATION_ANI;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002295}
2296
2297
Bruno Randolf4edd7612010-09-17 11:36:56 +09002298static void
2299ath5k_tx_complete_poll_work(struct work_struct *work)
2300{
2301 struct ath5k_softc *sc = container_of(work, struct ath5k_softc,
2302 tx_complete_work.work);
2303 struct ath5k_txq *txq;
2304 int i;
2305 bool needreset = false;
2306
2307 for (i = 0; i < ARRAY_SIZE(sc->txqs); i++) {
2308 if (sc->txqs[i].setup) {
2309 txq = &sc->txqs[i];
2310 spin_lock_bh(&txq->lock);
Bruno Randolf23413292010-09-17 11:37:07 +09002311 if (txq->txq_len > 1) {
Bruno Randolf4edd7612010-09-17 11:36:56 +09002312 if (txq->txq_poll_mark) {
2313 ATH5K_DBG(sc, ATH5K_DEBUG_XMIT,
2314 "TX queue stuck %d\n",
2315 txq->qnum);
2316 needreset = true;
Bruno Randolf923e5b32010-09-17 11:37:02 +09002317 txq->txq_stuck++;
Bruno Randolf4edd7612010-09-17 11:36:56 +09002318 spin_unlock_bh(&txq->lock);
2319 break;
2320 } else {
2321 txq->txq_poll_mark = true;
2322 }
2323 }
2324 spin_unlock_bh(&txq->lock);
2325 }
2326 }
2327
2328 if (needreset) {
2329 ATH5K_DBG(sc, ATH5K_DEBUG_RESET,
2330 "TX queues stuck, resetting\n");
Nick Kossifidis8aec7af2010-11-23 21:39:28 +02002331 ath5k_reset(sc, NULL, true);
Bruno Randolf4edd7612010-09-17 11:36:56 +09002332 }
2333
2334 ieee80211_queue_delayed_work(sc->hw, &sc->tx_complete_work,
2335 msecs_to_jiffies(ATH5K_TX_COMPLETE_POLL_INT));
2336}
2337
2338
Bob Copeland8a63fac2010-09-17 12:45:07 +09002339/*************************\
2340* Initialization routines *
2341\*************************/
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002342
2343static int
Bob Copeland8a63fac2010-09-17 12:45:07 +09002344ath5k_stop_locked(struct ath5k_softc *sc)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002345{
Bob Copeland8a63fac2010-09-17 12:45:07 +09002346 struct ath5k_hw *ah = sc->ah;
Bob Copelandcec8db22009-07-04 12:59:51 -04002347
Bob Copeland8a63fac2010-09-17 12:45:07 +09002348 ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "invalid %u\n",
2349 test_bit(ATH_STAT_INVALID, sc->status));
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002350
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002351 /*
Bob Copeland8a63fac2010-09-17 12:45:07 +09002352 * Shutdown the hardware and driver:
2353 * stop output from above
2354 * disable interrupts
2355 * turn off timers
2356 * turn off the radio
2357 * clear transmit machinery
2358 * clear receive machinery
2359 * drain and release tx queues
2360 * reclaim beacon resources
2361 * power down hardware
2362 *
2363 * Note that some of this work is not possible if the
2364 * hardware is gone (invalid).
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002365 */
Bob Copeland8a63fac2010-09-17 12:45:07 +09002366 ieee80211_stop_queues(sc->hw);
2367
2368 if (!test_bit(ATH_STAT_INVALID, sc->status)) {
2369 ath5k_led_off(sc);
2370 ath5k_hw_set_imr(ah, 0);
2371 synchronize_irq(sc->pdev->irq);
Bob Copeland8a63fac2010-09-17 12:45:07 +09002372 ath5k_rx_stop(sc);
Nick Kossifidis80dac9ee2010-11-23 20:45:38 +02002373 ath5k_hw_dma_stop(ah);
2374 ath5k_drain_tx_buffs(sc);
Bob Copeland8a63fac2010-09-17 12:45:07 +09002375 ath5k_hw_phy_disable(ah);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002376 }
2377
Bob Copeland8a63fac2010-09-17 12:45:07 +09002378 return 0;
2379}
2380
2381static int
2382ath5k_init(struct ath5k_softc *sc)
2383{
2384 struct ath5k_hw *ah = sc->ah;
2385 struct ath_common *common = ath5k_hw_common(ah);
2386 int ret, i;
2387
2388 mutex_lock(&sc->lock);
2389
2390 ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "mode %d\n", sc->opmode);
2391
2392 /*
2393 * Stop anything previously setup. This is safe
2394 * no matter this is the first time through or not.
2395 */
2396 ath5k_stop_locked(sc);
2397
2398 /*
2399 * The basic interface to setting the hardware in a good
2400 * state is ``reset''. On return the hardware is known to
2401 * be powered up and with interrupts disabled. This must
2402 * be followed by initialization of the appropriate bits
2403 * and then setup of the interrupt mask.
2404 */
2405 sc->curchan = sc->hw->conf.channel;
2406 sc->curband = &sc->sbands[sc->curchan->band];
2407 sc->imask = AR5K_INT_RXOK | AR5K_INT_RXERR | AR5K_INT_RXEOL |
2408 AR5K_INT_RXORN | AR5K_INT_TXDESC | AR5K_INT_TXEOL |
2409 AR5K_INT_FATAL | AR5K_INT_GLOBAL | AR5K_INT_MIB;
2410
Nick Kossifidis8aec7af2010-11-23 21:39:28 +02002411 ret = ath5k_reset(sc, NULL, false);
Bob Copeland8a63fac2010-09-17 12:45:07 +09002412 if (ret)
2413 goto done;
2414
2415 ath5k_rfkill_hw_start(ah);
2416
2417 /*
2418 * Reset the key cache since some parts do not reset the
2419 * contents on initial power up or resume from suspend.
2420 */
2421 for (i = 0; i < common->keymax; i++)
2422 ath_hw_keyreset(common, (u16) i);
2423
Nick Kossifidis61cde032010-11-23 21:12:23 +02002424 /* Use higher rates for acks instead of base
2425 * rate */
2426 ah->ah_ack_bitrate_high = true;
Ben Greearb1ae1ed2010-09-30 12:22:58 -07002427
2428 for (i = 0; i < ARRAY_SIZE(sc->bslot); i++)
2429 sc->bslot[i] = NULL;
2430
Bob Copeland8a63fac2010-09-17 12:45:07 +09002431 ret = 0;
2432done:
2433 mmiowb();
2434 mutex_unlock(&sc->lock);
Bruno Randolf4edd7612010-09-17 11:36:56 +09002435
2436 ieee80211_queue_delayed_work(sc->hw, &sc->tx_complete_work,
2437 msecs_to_jiffies(ATH5K_TX_COMPLETE_POLL_INT));
2438
Bob Copeland8a63fac2010-09-17 12:45:07 +09002439 return ret;
2440}
2441
2442static void stop_tasklets(struct ath5k_softc *sc)
2443{
2444 tasklet_kill(&sc->rxtq);
2445 tasklet_kill(&sc->txtq);
2446 tasklet_kill(&sc->calib);
2447 tasklet_kill(&sc->beacontq);
2448 tasklet_kill(&sc->ani_tasklet);
2449}
2450
2451/*
2452 * Stop the device, grabbing the top-level lock to protect
2453 * against concurrent entry through ath5k_init (which can happen
2454 * if another thread does a system call and the thread doing the
2455 * stop is preempted).
2456 */
2457static int
2458ath5k_stop_hw(struct ath5k_softc *sc)
2459{
2460 int ret;
2461
2462 mutex_lock(&sc->lock);
2463 ret = ath5k_stop_locked(sc);
2464 if (ret == 0 && !test_bit(ATH_STAT_INVALID, sc->status)) {
2465 /*
2466 * Don't set the card in full sleep mode!
2467 *
2468 * a) When the device is in this state it must be carefully
2469 * woken up or references to registers in the PCI clock
2470 * domain may freeze the bus (and system). This varies
2471 * by chip and is mostly an issue with newer parts
2472 * (madwifi sources mentioned srev >= 0x78) that go to
2473 * sleep more quickly.
2474 *
2475 * b) On older chips full sleep results a weird behaviour
2476 * during wakeup. I tested various cards with srev < 0x78
2477 * and they don't wake up after module reload, a second
2478 * module reload is needed to bring the card up again.
2479 *
2480 * Until we figure out what's going on don't enable
2481 * full chip reset on any chip (this is what Legacy HAL
2482 * and Sam's HAL do anyway). Instead Perform a full reset
2483 * on the device (same as initial state after attach) and
2484 * leave it idle (keep MAC/BB on warm reset) */
2485 ret = ath5k_hw_on_hold(sc->ah);
2486
2487 ATH5K_DBG(sc, ATH5K_DEBUG_RESET,
2488 "putting device to sleep\n");
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002489 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002490
Bob Copeland8a63fac2010-09-17 12:45:07 +09002491 mmiowb();
2492 mutex_unlock(&sc->lock);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002493
Bob Copeland8a63fac2010-09-17 12:45:07 +09002494 stop_tasklets(sc);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002495
Bruno Randolf4edd7612010-09-17 11:36:56 +09002496 cancel_delayed_work_sync(&sc->tx_complete_work);
2497
Bob Copeland8a63fac2010-09-17 12:45:07 +09002498 ath5k_rfkill_hw_stop(sc->ah);
2499
2500 return ret;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002501}
2502
Bob Copeland209d8892009-05-07 08:09:08 -04002503/*
2504 * Reset the hardware. If chan is not NULL, then also pause rx/tx
2505 * and change to the given channel.
Bob Copeland5faaff72010-07-13 11:32:40 -04002506 *
2507 * This should be called with sc->lock.
Bob Copeland209d8892009-05-07 08:09:08 -04002508 */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002509static int
Nick Kossifidis8aec7af2010-11-23 21:39:28 +02002510ath5k_reset(struct ath5k_softc *sc, struct ieee80211_channel *chan,
2511 bool skip_pcu)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002512{
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002513 struct ath5k_hw *ah = sc->ah;
2514 int ret;
2515
2516 ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "resetting\n");
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002517
Bob Copeland450464d2010-07-13 11:32:41 -04002518 ath5k_hw_set_imr(ah, 0);
2519 synchronize_irq(sc->pdev->irq);
2520 stop_tasklets(sc);
2521
Bob Copeland209d8892009-05-07 08:09:08 -04002522 if (chan) {
Nick Kossifidis80dac9ee2010-11-23 20:45:38 +02002523 ath5k_drain_tx_buffs(sc);
Bob Copeland209d8892009-05-07 08:09:08 -04002524
2525 sc->curchan = chan;
2526 sc->curband = &sc->sbands[chan->band];
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002527 }
Nick Kossifidis8aec7af2010-11-23 21:39:28 +02002528 ret = ath5k_hw_reset(ah, sc->opmode, sc->curchan, chan != NULL,
2529 skip_pcu);
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002530 if (ret) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002531 ATH5K_ERR(sc, "can't reset hardware (%d)\n", ret);
2532 goto err;
2533 }
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002534
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002535 ret = ath5k_rx_start(sc);
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002536 if (ret) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002537 ATH5K_ERR(sc, "can't start recv logic\n");
2538 goto err;
2539 }
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002540
Bruno Randolf2111ac02010-04-02 18:44:08 +09002541 ath5k_ani_init(ah, ah->ah_sc->ani_state.ani_mode);
2542
Bruno Randolfac559522010-05-19 10:30:55 +09002543 ah->ah_cal_next_full = jiffies;
2544 ah->ah_cal_next_ani = jiffies;
Bruno Randolfafe86282010-05-19 10:31:10 +09002545 ah->ah_cal_next_nf = jiffies;
Bruno Randolfeef39be2010-11-16 10:58:43 +09002546 ewma_init(&ah->ah_beacon_rssi_avg, 1000, 8);
Bruno Randolfafe86282010-05-19 10:31:10 +09002547
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002548 /*
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002549 * Change channels and update the h/w rate map if we're switching;
2550 * e.g. 11a to 11b/g.
2551 *
2552 * We may be doing a reset in response to an ioctl that changes the
2553 * channel so update any state that might change as a result.
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002554 *
2555 * XXX needed?
2556 */
2557/* ath5k_chan_change(sc, c); */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002558
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002559 ath5k_beacon_config(sc);
2560 /* intrs are enabled by ath5k_beacon_config */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002561
Bruno Randolf397f3852010-05-19 10:30:49 +09002562 ieee80211_wake_queues(sc->hw);
2563
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002564 return 0;
2565err:
2566 return ret;
2567}
2568
Bob Copeland5faaff72010-07-13 11:32:40 -04002569static void ath5k_reset_work(struct work_struct *work)
2570{
2571 struct ath5k_softc *sc = container_of(work, struct ath5k_softc,
2572 reset_work);
2573
2574 mutex_lock(&sc->lock);
Nick Kossifidis8aec7af2010-11-23 21:39:28 +02002575 ath5k_reset(sc, NULL, true);
Bob Copeland5faaff72010-07-13 11:32:40 -04002576 mutex_unlock(&sc->lock);
2577}
2578
Bob Copeland8a63fac2010-09-17 12:45:07 +09002579static int
2580ath5k_attach(struct pci_dev *pdev, struct ieee80211_hw *hw)
2581{
2582 struct ath5k_softc *sc = hw->priv;
2583 struct ath5k_hw *ah = sc->ah;
2584 struct ath_regulatory *regulatory = ath5k_hw_regulatory(ah);
Bruno Randolf925e0b02010-09-17 11:36:35 +09002585 struct ath5k_txq *txq;
Bob Copeland8a63fac2010-09-17 12:45:07 +09002586 u8 mac[ETH_ALEN] = {};
2587 int ret;
2588
2589 ATH5K_DBG(sc, ATH5K_DEBUG_ANY, "devid 0x%x\n", pdev->device);
2590
2591 /*
2592 * Check if the MAC has multi-rate retry support.
2593 * We do this by trying to setup a fake extended
2594 * descriptor. MACs that don't have support will
2595 * return false w/o doing anything. MACs that do
2596 * support it will return true w/o doing anything.
2597 */
2598 ret = ath5k_hw_setup_mrr_tx_desc(ah, NULL, 0, 0, 0, 0, 0, 0);
2599
2600 if (ret < 0)
2601 goto err;
2602 if (ret > 0)
2603 __set_bit(ATH_STAT_MRRETRY, sc->status);
2604
2605 /*
2606 * Collect the channel list. The 802.11 layer
2607 * is resposible for filtering this list based
2608 * on settings like the phy mode and regulatory
2609 * domain restrictions.
2610 */
2611 ret = ath5k_setup_bands(hw);
2612 if (ret) {
2613 ATH5K_ERR(sc, "can't get channels\n");
2614 goto err;
2615 }
2616
2617 /* NB: setup here so ath5k_rate_update is happy */
2618 if (test_bit(AR5K_MODE_11A, ah->ah_modes))
2619 ath5k_setcurmode(sc, AR5K_MODE_11A);
2620 else
2621 ath5k_setcurmode(sc, AR5K_MODE_11B);
2622
2623 /*
2624 * Allocate tx+rx descriptors and populate the lists.
2625 */
2626 ret = ath5k_desc_alloc(sc, pdev);
2627 if (ret) {
2628 ATH5K_ERR(sc, "can't allocate descriptors\n");
2629 goto err;
2630 }
2631
2632 /*
2633 * Allocate hardware transmit queues: one queue for
2634 * beacon frames and one data queue for each QoS
2635 * priority. Note that hw functions handle resetting
2636 * these queues at the needed time.
2637 */
2638 ret = ath5k_beaconq_setup(ah);
2639 if (ret < 0) {
2640 ATH5K_ERR(sc, "can't setup a beacon xmit queue\n");
2641 goto err_desc;
2642 }
2643 sc->bhalq = ret;
2644 sc->cabq = ath5k_txq_setup(sc, AR5K_TX_QUEUE_CAB, 0);
2645 if (IS_ERR(sc->cabq)) {
2646 ATH5K_ERR(sc, "can't setup cab queue\n");
2647 ret = PTR_ERR(sc->cabq);
2648 goto err_bhal;
2649 }
2650
Bruno Randolf925e0b02010-09-17 11:36:35 +09002651 /* This order matches mac80211's queue priority, so we can
2652 * directly use the mac80211 queue number without any mapping */
2653 txq = ath5k_txq_setup(sc, AR5K_TX_QUEUE_DATA, AR5K_WME_AC_VO);
2654 if (IS_ERR(txq)) {
Bob Copeland8a63fac2010-09-17 12:45:07 +09002655 ATH5K_ERR(sc, "can't setup xmit queue\n");
Bruno Randolf925e0b02010-09-17 11:36:35 +09002656 ret = PTR_ERR(txq);
Bob Copeland8a63fac2010-09-17 12:45:07 +09002657 goto err_queues;
2658 }
Bruno Randolf925e0b02010-09-17 11:36:35 +09002659 txq = ath5k_txq_setup(sc, AR5K_TX_QUEUE_DATA, AR5K_WME_AC_VI);
2660 if (IS_ERR(txq)) {
2661 ATH5K_ERR(sc, "can't setup xmit queue\n");
2662 ret = PTR_ERR(txq);
2663 goto err_queues;
2664 }
2665 txq = ath5k_txq_setup(sc, AR5K_TX_QUEUE_DATA, AR5K_WME_AC_BE);
2666 if (IS_ERR(txq)) {
2667 ATH5K_ERR(sc, "can't setup xmit queue\n");
2668 ret = PTR_ERR(txq);
2669 goto err_queues;
2670 }
2671 txq = ath5k_txq_setup(sc, AR5K_TX_QUEUE_DATA, AR5K_WME_AC_BK);
2672 if (IS_ERR(txq)) {
2673 ATH5K_ERR(sc, "can't setup xmit queue\n");
2674 ret = PTR_ERR(txq);
2675 goto err_queues;
2676 }
2677 hw->queues = 4;
Bob Copeland8a63fac2010-09-17 12:45:07 +09002678
2679 tasklet_init(&sc->rxtq, ath5k_tasklet_rx, (unsigned long)sc);
2680 tasklet_init(&sc->txtq, ath5k_tasklet_tx, (unsigned long)sc);
2681 tasklet_init(&sc->calib, ath5k_tasklet_calibrate, (unsigned long)sc);
2682 tasklet_init(&sc->beacontq, ath5k_tasklet_beacon, (unsigned long)sc);
2683 tasklet_init(&sc->ani_tasklet, ath5k_tasklet_ani, (unsigned long)sc);
2684
2685 INIT_WORK(&sc->reset_work, ath5k_reset_work);
Bruno Randolf4edd7612010-09-17 11:36:56 +09002686 INIT_DELAYED_WORK(&sc->tx_complete_work, ath5k_tx_complete_poll_work);
Bob Copeland8a63fac2010-09-17 12:45:07 +09002687
2688 ret = ath5k_eeprom_read_mac(ah, mac);
2689 if (ret) {
2690 ATH5K_ERR(sc, "unable to read address from EEPROM: 0x%04x\n",
2691 sc->pdev->device);
2692 goto err_queues;
2693 }
2694
2695 SET_IEEE80211_PERM_ADDR(hw, mac);
Ben Greearb1ae1ed2010-09-30 12:22:58 -07002696 memcpy(&sc->lladdr, mac, ETH_ALEN);
Bob Copeland8a63fac2010-09-17 12:45:07 +09002697 /* All MAC address bits matter for ACKs */
Ben Greear62c58fb2010-10-08 12:01:15 -07002698 ath5k_update_bssid_mask_and_opmode(sc, NULL);
Bob Copeland8a63fac2010-09-17 12:45:07 +09002699
2700 regulatory->current_rd = ah->ah_capabilities.cap_eeprom.ee_regdomain;
2701 ret = ath_regd_init(regulatory, hw->wiphy, ath5k_reg_notifier);
2702 if (ret) {
2703 ATH5K_ERR(sc, "can't initialize regulatory system\n");
2704 goto err_queues;
2705 }
2706
2707 ret = ieee80211_register_hw(hw);
2708 if (ret) {
2709 ATH5K_ERR(sc, "can't register ieee80211 hw\n");
2710 goto err_queues;
2711 }
2712
2713 if (!ath_is_world_regd(regulatory))
2714 regulatory_hint(hw->wiphy, regulatory->alpha2);
2715
2716 ath5k_init_leds(sc);
2717
2718 ath5k_sysfs_register(sc);
2719
2720 return 0;
2721err_queues:
2722 ath5k_txq_release(sc);
2723err_bhal:
2724 ath5k_hw_release_tx_queue(ah, sc->bhalq);
2725err_desc:
2726 ath5k_desc_free(sc, pdev);
2727err:
2728 return ret;
2729}
2730
2731static void
2732ath5k_detach(struct pci_dev *pdev, struct ieee80211_hw *hw)
2733{
2734 struct ath5k_softc *sc = hw->priv;
2735
2736 /*
2737 * NB: the order of these is important:
2738 * o call the 802.11 layer before detaching ath5k_hw to
2739 * ensure callbacks into the driver to delete global
2740 * key cache entries can be handled
2741 * o reclaim the tx queue data structures after calling
2742 * the 802.11 layer as we'll get called back to reclaim
2743 * node state and potentially want to use them
2744 * o to cleanup the tx queues the hal is called, so detach
2745 * it last
2746 * XXX: ??? detach ath5k_hw ???
2747 * Other than that, it's straightforward...
2748 */
2749 ieee80211_unregister_hw(hw);
2750 ath5k_desc_free(sc, pdev);
2751 ath5k_txq_release(sc);
2752 ath5k_hw_release_tx_queue(sc->ah, sc->bhalq);
2753 ath5k_unregister_leds(sc);
2754
2755 ath5k_sysfs_unregister(sc);
2756 /*
2757 * NB: can't reclaim these until after ieee80211_ifdetach
2758 * returns because we'll get called back to reclaim node
2759 * state and potentially want to use them.
2760 */
2761}
2762
2763/********************\
2764* Mac80211 functions *
2765\********************/
2766
2767static int
2768ath5k_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
2769{
2770 struct ath5k_softc *sc = hw->priv;
Bruno Randolf925e0b02010-09-17 11:36:35 +09002771 u16 qnum = skb_get_queue_mapping(skb);
Bob Copeland8a63fac2010-09-17 12:45:07 +09002772
Bruno Randolf925e0b02010-09-17 11:36:35 +09002773 if (WARN_ON(qnum >= sc->ah->ah_capabilities.cap_queues.q_tx_num)) {
2774 dev_kfree_skb_any(skb);
2775 return 0;
2776 }
2777
2778 return ath5k_tx_queue(hw, skb, &sc->txqs[qnum]);
Bob Copeland8a63fac2010-09-17 12:45:07 +09002779}
2780
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002781static int ath5k_start(struct ieee80211_hw *hw)
2782{
Bob Copelandbb2beca2009-01-19 11:20:54 -05002783 return ath5k_init(hw->priv);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002784}
2785
2786static void ath5k_stop(struct ieee80211_hw *hw)
2787{
Bob Copelandbb2beca2009-01-19 11:20:54 -05002788 ath5k_stop_hw(hw->priv);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002789}
2790
2791static int ath5k_add_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +01002792 struct ieee80211_vif *vif)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002793{
2794 struct ath5k_softc *sc = hw->priv;
2795 int ret;
Ben Greearb1ae1ed2010-09-30 12:22:58 -07002796 struct ath5k_vif *avf = (void *)vif->drv_priv;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002797
2798 mutex_lock(&sc->lock);
Ben Greearb1ae1ed2010-09-30 12:22:58 -07002799
2800 if ((vif->type == NL80211_IFTYPE_AP ||
2801 vif->type == NL80211_IFTYPE_ADHOC)
2802 && (sc->num_ap_vifs + sc->num_adhoc_vifs) >= ATH_BCBUF) {
2803 ret = -ELNRNG;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002804 goto end;
2805 }
2806
Ben Greearb1ae1ed2010-09-30 12:22:58 -07002807 /* Don't allow other interfaces if one ad-hoc is configured.
2808 * TODO: Fix the problems with ad-hoc and multiple other interfaces.
2809 * We would need to operate the HW in ad-hoc mode to allow TSF updates
2810 * for the IBSS, but this breaks with additional AP or STA interfaces
2811 * at the moment. */
2812 if (sc->num_adhoc_vifs ||
2813 (sc->nvifs && vif->type == NL80211_IFTYPE_ADHOC)) {
2814 ATH5K_ERR(sc, "Only one single ad-hoc interface is allowed.\n");
2815 ret = -ELNRNG;
2816 goto end;
2817 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002818
Johannes Berg1ed32e42009-12-23 13:15:45 +01002819 switch (vif->type) {
Jiri Slabyda966bc2008-10-12 22:54:10 +02002820 case NL80211_IFTYPE_AP:
Johannes Berg05c914f2008-09-11 00:01:58 +02002821 case NL80211_IFTYPE_STATION:
2822 case NL80211_IFTYPE_ADHOC:
Andrey Yurovskyb706e652008-10-13 18:23:07 -07002823 case NL80211_IFTYPE_MESH_POINT:
Ben Greearb1ae1ed2010-09-30 12:22:58 -07002824 avf->opmode = vif->type;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002825 break;
2826 default:
2827 ret = -EOPNOTSUPP;
2828 goto end;
2829 }
Jiri Slaby67d2e2d2008-08-18 21:45:28 +02002830
Ben Greearb1ae1ed2010-09-30 12:22:58 -07002831 sc->nvifs++;
2832 ATH5K_DBG(sc, ATH5K_DEBUG_MODE, "add interface mode %d\n", avf->opmode);
Bruno Randolfccfe5552010-03-09 16:55:38 +09002833
Ben Greearb1ae1ed2010-09-30 12:22:58 -07002834 /* Assign the vap/adhoc to a beacon xmit slot. */
2835 if ((avf->opmode == NL80211_IFTYPE_AP) ||
2836 (avf->opmode == NL80211_IFTYPE_ADHOC)) {
2837 int slot;
2838
2839 WARN_ON(list_empty(&sc->bcbuf));
2840 avf->bbuf = list_first_entry(&sc->bcbuf, struct ath5k_buf,
2841 list);
2842 list_del(&avf->bbuf->list);
2843
2844 avf->bslot = 0;
2845 for (slot = 0; slot < ATH_BCBUF; slot++) {
2846 if (!sc->bslot[slot]) {
2847 avf->bslot = slot;
2848 break;
2849 }
2850 }
2851 BUG_ON(sc->bslot[avf->bslot] != NULL);
2852 sc->bslot[avf->bslot] = vif;
2853 if (avf->opmode == NL80211_IFTYPE_AP)
2854 sc->num_ap_vifs++;
2855 else
2856 sc->num_adhoc_vifs++;
2857 }
2858
Ben Greearb1ae1ed2010-09-30 12:22:58 -07002859 /* Any MAC address is fine, all others are included through the
2860 * filter.
2861 */
2862 memcpy(&sc->lladdr, vif->addr, ETH_ALEN);
Johannes Berg1ed32e42009-12-23 13:15:45 +01002863 ath5k_hw_set_lladdr(sc->ah, vif->addr);
Ben Greearb1ae1ed2010-09-30 12:22:58 -07002864
2865 memcpy(&avf->lladdr, vif->addr, ETH_ALEN);
2866
2867 ath5k_mode_setup(sc, vif);
Jiri Slaby67d2e2d2008-08-18 21:45:28 +02002868
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002869 ret = 0;
2870end:
2871 mutex_unlock(&sc->lock);
2872 return ret;
2873}
2874
2875static void
2876ath5k_remove_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +01002877 struct ieee80211_vif *vif)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002878{
2879 struct ath5k_softc *sc = hw->priv;
Ben Greearb1ae1ed2010-09-30 12:22:58 -07002880 struct ath5k_vif *avf = (void *)vif->drv_priv;
2881 unsigned int i;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002882
2883 mutex_lock(&sc->lock);
Ben Greearb1ae1ed2010-09-30 12:22:58 -07002884 sc->nvifs--;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002885
Ben Greearb1ae1ed2010-09-30 12:22:58 -07002886 if (avf->bbuf) {
2887 ath5k_txbuf_free_skb(sc, avf->bbuf);
2888 list_add_tail(&avf->bbuf->list, &sc->bcbuf);
2889 for (i = 0; i < ATH_BCBUF; i++) {
2890 if (sc->bslot[i] == vif) {
2891 sc->bslot[i] = NULL;
2892 break;
2893 }
2894 }
2895 avf->bbuf = NULL;
2896 }
2897 if (avf->opmode == NL80211_IFTYPE_AP)
2898 sc->num_ap_vifs--;
2899 else if (avf->opmode == NL80211_IFTYPE_ADHOC)
2900 sc->num_adhoc_vifs--;
2901
Ben Greear62c58fb2010-10-08 12:01:15 -07002902 ath5k_update_bssid_mask_and_opmode(sc, NULL);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002903 mutex_unlock(&sc->lock);
2904}
2905
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -05002906/*
2907 * TODO: Phy disable/diversity etc
2908 */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002909static int
Johannes Berge8975582008-10-09 12:18:51 +02002910ath5k_config(struct ieee80211_hw *hw, u32 changed)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002911{
2912 struct ath5k_softc *sc = hw->priv;
Nick Kossifidisa0823812009-04-30 15:55:44 -04002913 struct ath5k_hw *ah = sc->ah;
Johannes Berge8975582008-10-09 12:18:51 +02002914 struct ieee80211_conf *conf = &hw->conf;
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04002915 int ret = 0;
Bob Copelandbe009372009-01-22 08:44:16 -05002916
2917 mutex_lock(&sc->lock);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002918
Joerg Alberte30eb4a2009-08-05 01:52:07 +02002919 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
2920 ret = ath5k_chan_set(sc, conf->channel);
2921 if (ret < 0)
2922 goto unlock;
2923 }
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04002924
Nick Kossifidisa0823812009-04-30 15:55:44 -04002925 if ((changed & IEEE80211_CONF_CHANGE_POWER) &&
2926 (sc->power_level != conf->power_level)) {
2927 sc->power_level = conf->power_level;
2928
2929 /* Half dB steps */
2930 ath5k_hw_set_txpower_limit(ah, (conf->power_level * 2));
2931 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002932
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04002933 /* TODO:
2934 * 1) Move this on config_interface and handle each case
2935 * separately eg. when we have only one STA vif, use
2936 * AR5K_ANTMODE_SINGLE_AP
2937 *
2938 * 2) Allow the user to change antenna mode eg. when only
2939 * one antenna is present
2940 *
2941 * 3) Allow the user to set default/tx antenna when possible
2942 *
2943 * 4) Default mode should handle 90% of the cases, together
2944 * with fixed a/b and single AP modes we should be able to
2945 * handle 99%. Sectored modes are extreme cases and i still
2946 * haven't found a usage for them. If we decide to support them,
2947 * then we must allow the user to set how many tx antennas we
2948 * have available
2949 */
Bruno Randolfcaec9112010-03-09 16:55:28 +09002950 ath5k_hw_set_antenna_mode(ah, ah->ah_ant_mode);
Bob Copelandbe009372009-01-22 08:44:16 -05002951
John W. Linville55aa4e02009-05-25 21:28:47 +02002952unlock:
Bob Copelandbe009372009-01-22 08:44:16 -05002953 mutex_unlock(&sc->lock);
John W. Linville55aa4e02009-05-25 21:28:47 +02002954 return ret;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002955}
2956
Johannes Berg3ac64be2009-08-17 16:16:53 +02002957static u64 ath5k_prepare_multicast(struct ieee80211_hw *hw,
Jiri Pirko22bedad2010-04-01 21:22:57 +00002958 struct netdev_hw_addr_list *mc_list)
Johannes Berg3ac64be2009-08-17 16:16:53 +02002959{
2960 u32 mfilt[2], val;
Johannes Berg3ac64be2009-08-17 16:16:53 +02002961 u8 pos;
Jiri Pirko22bedad2010-04-01 21:22:57 +00002962 struct netdev_hw_addr *ha;
Johannes Berg3ac64be2009-08-17 16:16:53 +02002963
2964 mfilt[0] = 0;
2965 mfilt[1] = 1;
2966
Jiri Pirko22bedad2010-04-01 21:22:57 +00002967 netdev_hw_addr_list_for_each(ha, mc_list) {
Johannes Berg3ac64be2009-08-17 16:16:53 +02002968 /* calculate XOR of eight 6-bit values */
Jiri Pirko22bedad2010-04-01 21:22:57 +00002969 val = get_unaligned_le32(ha->addr + 0);
Johannes Berg3ac64be2009-08-17 16:16:53 +02002970 pos = (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
Jiri Pirko22bedad2010-04-01 21:22:57 +00002971 val = get_unaligned_le32(ha->addr + 3);
Johannes Berg3ac64be2009-08-17 16:16:53 +02002972 pos ^= (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
2973 pos &= 0x3f;
2974 mfilt[pos / 32] |= (1 << (pos % 32));
2975 /* XXX: we might be able to just do this instead,
2976 * but not sure, needs testing, if we do use this we'd
2977 * neet to inform below to not reset the mcast */
2978 /* ath5k_hw_set_mcast_filterindex(ah,
Jiri Pirko22bedad2010-04-01 21:22:57 +00002979 * ha->addr[5]); */
Johannes Berg3ac64be2009-08-17 16:16:53 +02002980 }
2981
2982 return ((u64)(mfilt[1]) << 32) | mfilt[0];
2983}
2984
Ben Greearb1ae1ed2010-09-30 12:22:58 -07002985static bool ath_any_vif_assoc(struct ath5k_softc *sc)
2986{
2987 struct ath_vif_iter_data iter_data;
2988 iter_data.hw_macaddr = NULL;
2989 iter_data.any_assoc = false;
2990 iter_data.need_set_hw_addr = false;
2991 iter_data.found_active = true;
2992
2993 ieee80211_iterate_active_interfaces_atomic(sc->hw, ath_vif_iter,
2994 &iter_data);
2995 return iter_data.any_assoc;
2996}
2997
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002998#define SUPPORTED_FIF_FLAGS \
2999 FIF_PROMISC_IN_BSS | FIF_ALLMULTI | FIF_FCSFAIL | \
3000 FIF_PLCPFAIL | FIF_CONTROL | FIF_OTHER_BSS | \
3001 FIF_BCN_PRBRESP_PROMISC
3002/*
3003 * o always accept unicast, broadcast, and multicast traffic
3004 * o multicast traffic for all BSSIDs will be enabled if mac80211
3005 * says it should be
3006 * o maintain current state of phy ofdm or phy cck error reception.
3007 * If the hardware detects any of these type of errors then
3008 * ath5k_hw_get_rx_filter() will pass to us the respective
3009 * hardware filters to be able to receive these type of frames.
3010 * o probe request frames are accepted only when operating in
3011 * hostap, adhoc, or monitor modes
3012 * o enable promiscuous mode according to the interface state
3013 * o accept beacons:
3014 * - when operating in adhoc mode so the 802.11 layer creates
3015 * node table entries for peers,
3016 * - when operating in station mode for collecting rssi data when
3017 * the station is otherwise quiet, or
3018 * - when scanning
3019 */
3020static void ath5k_configure_filter(struct ieee80211_hw *hw,
3021 unsigned int changed_flags,
3022 unsigned int *new_flags,
Johannes Berg3ac64be2009-08-17 16:16:53 +02003023 u64 multicast)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003024{
3025 struct ath5k_softc *sc = hw->priv;
3026 struct ath5k_hw *ah = sc->ah;
Johannes Berg3ac64be2009-08-17 16:16:53 +02003027 u32 mfilt[2], rfilt;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003028
Bob Copeland56d1de02009-08-24 23:00:30 -04003029 mutex_lock(&sc->lock);
3030
Johannes Berg3ac64be2009-08-17 16:16:53 +02003031 mfilt[0] = multicast;
3032 mfilt[1] = multicast >> 32;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003033
3034 /* Only deal with supported flags */
3035 changed_flags &= SUPPORTED_FIF_FLAGS;
3036 *new_flags &= SUPPORTED_FIF_FLAGS;
3037
3038 /* If HW detects any phy or radar errors, leave those filters on.
3039 * Also, always enable Unicast, Broadcasts and Multicast
3040 * XXX: move unicast, bssid broadcasts and multicast to mac80211 */
3041 rfilt = (ath5k_hw_get_rx_filter(ah) & (AR5K_RX_FILTER_PHYERR)) |
3042 (AR5K_RX_FILTER_UCAST | AR5K_RX_FILTER_BCAST |
3043 AR5K_RX_FILTER_MCAST);
3044
3045 if (changed_flags & (FIF_PROMISC_IN_BSS | FIF_OTHER_BSS)) {
3046 if (*new_flags & FIF_PROMISC_IN_BSS) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003047 __set_bit(ATH_STAT_PROMISC, sc->status);
John Daiker0bbac082008-10-17 12:16:00 -07003048 } else {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003049 __clear_bit(ATH_STAT_PROMISC, sc->status);
John Daiker0bbac082008-10-17 12:16:00 -07003050 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003051 }
3052
Bob Copeland6b5dccc2010-06-04 08:14:14 -04003053 if (test_bit(ATH_STAT_PROMISC, sc->status))
3054 rfilt |= AR5K_RX_FILTER_PROM;
3055
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003056 /* Note, AR5K_RX_FILTER_MCAST is already enabled */
3057 if (*new_flags & FIF_ALLMULTI) {
3058 mfilt[0] = ~0;
3059 mfilt[1] = ~0;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003060 }
3061
3062 /* This is the best we can do */
3063 if (*new_flags & (FIF_FCSFAIL | FIF_PLCPFAIL))
3064 rfilt |= AR5K_RX_FILTER_PHYERR;
3065
3066 /* FIF_BCN_PRBRESP_PROMISC really means to enable beacons
Bob Copeland30bf4162010-08-15 13:03:15 -04003067 * and probes for any BSSID */
Ben Greearb1ae1ed2010-09-30 12:22:58 -07003068 if ((*new_flags & FIF_BCN_PRBRESP_PROMISC) || (sc->nvifs > 1))
Bob Copeland30bf4162010-08-15 13:03:15 -04003069 rfilt |= AR5K_RX_FILTER_BEACON;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003070
3071 /* FIF_CONTROL doc says that if FIF_PROMISC_IN_BSS is not
3072 * set we should only pass on control frames for this
3073 * station. This needs testing. I believe right now this
3074 * enables *all* control frames, which is OK.. but
3075 * but we should see if we can improve on granularity */
3076 if (*new_flags & FIF_CONTROL)
3077 rfilt |= AR5K_RX_FILTER_CONTROL;
3078
3079 /* Additional settings per mode -- this is per ath5k */
3080
3081 /* XXX move these to mac80211, and add a beacon IFF flag to mac80211 */
3082
Bob Copeland56d1de02009-08-24 23:00:30 -04003083 switch (sc->opmode) {
3084 case NL80211_IFTYPE_MESH_POINT:
Bob Copeland56d1de02009-08-24 23:00:30 -04003085 rfilt |= AR5K_RX_FILTER_CONTROL |
3086 AR5K_RX_FILTER_BEACON |
3087 AR5K_RX_FILTER_PROBEREQ |
3088 AR5K_RX_FILTER_PROM;
3089 break;
3090 case NL80211_IFTYPE_AP:
3091 case NL80211_IFTYPE_ADHOC:
3092 rfilt |= AR5K_RX_FILTER_PROBEREQ |
3093 AR5K_RX_FILTER_BEACON;
3094 break;
3095 case NL80211_IFTYPE_STATION:
3096 if (sc->assoc)
3097 rfilt |= AR5K_RX_FILTER_BEACON;
3098 default:
3099 break;
3100 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003101
3102 /* Set filters */
John Daiker0bbac082008-10-17 12:16:00 -07003103 ath5k_hw_set_rx_filter(ah, rfilt);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003104
3105 /* Set multicast bits */
3106 ath5k_hw_set_mcast_filter(ah, mfilt[0], mfilt[1]);
Bob Copelanda180a132010-08-15 13:03:12 -04003107 /* Set the cached hw filter flags, this will later actually
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003108 * be set in HW */
3109 sc->filter_flags = rfilt;
Bob Copeland56d1de02009-08-24 23:00:30 -04003110
3111 mutex_unlock(&sc->lock);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003112}
3113
3114static int
3115ath5k_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Johannes Bergdc822b52008-12-29 12:55:09 +01003116 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
3117 struct ieee80211_key_conf *key)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003118{
3119 struct ath5k_softc *sc = hw->priv;
Luis R. Rodriguezdc1e0012009-11-04 17:47:31 -08003120 struct ath5k_hw *ah = sc->ah;
3121 struct ath_common *common = ath5k_hw_common(ah);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003122 int ret = 0;
3123
Bob Copeland9ad9a262008-10-29 08:30:54 -04003124 if (modparam_nohwcrypt)
3125 return -EOPNOTSUPP;
3126
Johannes Berg97359d12010-08-10 09:46:38 +02003127 switch (key->cipher) {
3128 case WLAN_CIPHER_SUITE_WEP40:
3129 case WLAN_CIPHER_SUITE_WEP104:
3130 case WLAN_CIPHER_SUITE_TKIP:
Bob Copeland3f64b432008-10-29 23:19:14 -04003131 break;
Johannes Berg97359d12010-08-10 09:46:38 +02003132 case WLAN_CIPHER_SUITE_CCMP:
Bruno Randolf781f3132010-09-08 16:04:59 +09003133 if (common->crypt_caps & ATH_CRYPT_CAP_CIPHER_AESCCM)
Bob Copeland1c818742009-08-24 23:00:33 -04003134 break;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003135 return -EOPNOTSUPP;
3136 default:
3137 WARN_ON(1);
3138 return -EINVAL;
3139 }
3140
3141 mutex_lock(&sc->lock);
3142
3143 switch (cmd) {
3144 case SET_KEY:
Bruno Randolfe0f8c2a2010-09-08 16:04:43 +09003145 ret = ath_key_config(common, vif, sta, key);
3146 if (ret >= 0) {
3147 key->hw_key_idx = ret;
3148 /* push IV and Michael MIC generation to stack */
3149 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
3150 if (key->cipher == WLAN_CIPHER_SUITE_TKIP)
3151 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
3152 if (key->cipher == WLAN_CIPHER_SUITE_CCMP)
3153 key->flags |= IEEE80211_KEY_FLAG_SW_MGMT;
3154 ret = 0;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003155 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003156 break;
3157 case DISABLE_KEY:
Bruno Randolfe0f8c2a2010-09-08 16:04:43 +09003158 ath_key_delete(common, key);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003159 break;
3160 default:
3161 ret = -EINVAL;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003162 }
3163
Jiri Slaby274c7c32008-07-15 17:44:20 +02003164 mmiowb();
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003165 mutex_unlock(&sc->lock);
3166 return ret;
3167}
3168
3169static int
3170ath5k_get_stats(struct ieee80211_hw *hw,
3171 struct ieee80211_low_level_stats *stats)
3172{
3173 struct ath5k_softc *sc = hw->priv;
Nick Kossifidis194828a2008-04-16 18:49:02 +03003174
3175 /* Force update */
Bruno Randolf495391d2010-03-25 14:49:36 +09003176 ath5k_hw_update_mib_counters(sc->ah);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003177
Bruno Randolf495391d2010-03-25 14:49:36 +09003178 stats->dot11ACKFailureCount = sc->stats.ack_fail;
3179 stats->dot11RTSFailureCount = sc->stats.rts_fail;
3180 stats->dot11RTSSuccessCount = sc->stats.rts_ok;
3181 stats->dot11FCSErrorCount = sc->stats.fcs_error;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003182
3183 return 0;
3184}
3185
Holger Schurig55ee82b2010-04-19 10:24:22 +02003186static int ath5k_get_survey(struct ieee80211_hw *hw, int idx,
3187 struct survey_info *survey)
3188{
3189 struct ath5k_softc *sc = hw->priv;
3190 struct ieee80211_conf *conf = &hw->conf;
Bruno Randolfedb40a22010-10-19 16:56:54 +09003191 struct ath_common *common = ath5k_hw_common(sc->ah);
3192 struct ath_cycle_counters *cc = &common->cc_survey;
3193 unsigned int div = common->clockrate * 1000;
Holger Schurig55ee82b2010-04-19 10:24:22 +02003194
Bruno Randolfedb40a22010-10-19 16:56:54 +09003195 if (idx != 0)
Holger Schurig55ee82b2010-04-19 10:24:22 +02003196 return -ENOENT;
3197
3198 survey->channel = conf->channel;
3199 survey->filled = SURVEY_INFO_NOISE_DBM;
3200 survey->noise = sc->ah->ah_noise_floor;
3201
Bruno Randolfedb40a22010-10-19 16:56:54 +09003202 spin_lock_bh(&common->cc_lock);
3203 ath_hw_cycle_counters_update(common);
3204 if (cc->cycles > 0) {
3205 survey->filled |= SURVEY_INFO_CHANNEL_TIME |
3206 SURVEY_INFO_CHANNEL_TIME_BUSY |
3207 SURVEY_INFO_CHANNEL_TIME_RX |
3208 SURVEY_INFO_CHANNEL_TIME_TX;
3209 survey->channel_time += cc->cycles / div;
3210 survey->channel_time_busy += cc->rx_busy / div;
3211 survey->channel_time_rx += cc->rx_frame / div;
3212 survey->channel_time_tx += cc->tx_frame / div;
3213 }
3214 memset(cc, 0, sizeof(*cc));
3215 spin_unlock_bh(&common->cc_lock);
3216
Holger Schurig55ee82b2010-04-19 10:24:22 +02003217 return 0;
3218}
3219
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003220static u64
3221ath5k_get_tsf(struct ieee80211_hw *hw)
3222{
3223 struct ath5k_softc *sc = hw->priv;
3224
3225 return ath5k_hw_get_tsf64(sc->ah);
3226}
3227
3228static void
Alina Friedrichsen3b5d6652009-01-24 07:09:59 +01003229ath5k_set_tsf(struct ieee80211_hw *hw, u64 tsf)
3230{
3231 struct ath5k_softc *sc = hw->priv;
3232
3233 ath5k_hw_set_tsf64(sc->ah, tsf);
3234}
3235
3236static void
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003237ath5k_reset_tsf(struct ieee80211_hw *hw)
3238{
3239 struct ath5k_softc *sc = hw->priv;
3240
Bruno Randolf9804b982008-01-19 18:17:59 +09003241 /*
3242 * in IBSS mode we need to update the beacon timers too.
3243 * this will also reset the TSF if we call it with 0
3244 */
Johannes Berg05c914f2008-09-11 00:01:58 +02003245 if (sc->opmode == NL80211_IFTYPE_ADHOC)
Bruno Randolf9804b982008-01-19 18:17:59 +09003246 ath5k_beacon_update_timers(sc, 0);
3247 else
3248 ath5k_hw_reset_tsf(sc->ah);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003249}
3250
Martin Xu02969b32008-11-24 10:49:27 +08003251static void
3252set_beacon_filter(struct ieee80211_hw *hw, bool enable)
3253{
3254 struct ath5k_softc *sc = hw->priv;
3255 struct ath5k_hw *ah = sc->ah;
3256 u32 rfilt;
3257 rfilt = ath5k_hw_get_rx_filter(ah);
3258 if (enable)
3259 rfilt |= AR5K_RX_FILTER_BEACON;
3260 else
3261 rfilt &= ~AR5K_RX_FILTER_BEACON;
3262 ath5k_hw_set_rx_filter(ah, rfilt);
3263 sc->filter_flags = rfilt;
3264}
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003265
Martin Xu02969b32008-11-24 10:49:27 +08003266static void ath5k_bss_info_changed(struct ieee80211_hw *hw,
3267 struct ieee80211_vif *vif,
3268 struct ieee80211_bss_conf *bss_conf,
3269 u32 changes)
3270{
Ben Greearb1ae1ed2010-09-30 12:22:58 -07003271 struct ath5k_vif *avf = (void *)vif->drv_priv;
Martin Xu02969b32008-11-24 10:49:27 +08003272 struct ath5k_softc *sc = hw->priv;
Johannes Berg2d0ddec2009-04-23 16:13:26 +02003273 struct ath5k_hw *ah = sc->ah;
Luis R. Rodriguez954fece2009-09-10 10:51:33 -07003274 struct ath_common *common = ath5k_hw_common(ah);
Bob Copeland21800492009-07-04 12:59:52 -04003275 unsigned long flags;
Johannes Berg2d0ddec2009-04-23 16:13:26 +02003276
3277 mutex_lock(&sc->lock);
Johannes Berg2d0ddec2009-04-23 16:13:26 +02003278
3279 if (changes & BSS_CHANGED_BSSID) {
3280 /* Cache for later use during resets */
Luis R. Rodriguez954fece2009-09-10 10:51:33 -07003281 memcpy(common->curbssid, bss_conf->bssid, ETH_ALEN);
Luis R. Rodriguez8ce54c52009-10-06 20:44:34 -04003282 common->curaid = 0;
Nick Kossifidis418de6d2010-08-15 13:03:10 -04003283 ath5k_hw_set_bssid(ah);
Johannes Berg2d0ddec2009-04-23 16:13:26 +02003284 mmiowb();
3285 }
Johannes Berg57c4d7b2009-04-23 16:10:04 +02003286
3287 if (changes & BSS_CHANGED_BEACON_INT)
3288 sc->bintval = bss_conf->beacon_int;
3289
Martin Xu02969b32008-11-24 10:49:27 +08003290 if (changes & BSS_CHANGED_ASSOC) {
Ben Greearb1ae1ed2010-09-30 12:22:58 -07003291 avf->assoc = bss_conf->assoc;
3292 if (bss_conf->assoc)
3293 sc->assoc = bss_conf->assoc;
3294 else
3295 sc->assoc = ath_any_vif_assoc(sc);
3296
Martin Xu02969b32008-11-24 10:49:27 +08003297 if (sc->opmode == NL80211_IFTYPE_STATION)
3298 set_beacon_filter(hw, sc->assoc);
Bob Copelandf0f3d382009-06-10 22:22:21 -04003299 ath5k_hw_set_ledstate(sc->ah, sc->assoc ?
3300 AR5K_LED_ASSOC : AR5K_LED_INIT);
Luis R. Rodriguez8ce54c52009-10-06 20:44:34 -04003301 if (bss_conf->assoc) {
3302 ATH5K_DBG(sc, ATH5K_DEBUG_ANY,
3303 "Bss Info ASSOC %d, bssid: %pM\n",
3304 bss_conf->aid, common->curbssid);
3305 common->curaid = bss_conf->aid;
Nick Kossifidis418de6d2010-08-15 13:03:10 -04003306 ath5k_hw_set_bssid(ah);
Luis R. Rodriguez8ce54c52009-10-06 20:44:34 -04003307 /* Once ANI is available you would start it here */
3308 }
Martin Xu02969b32008-11-24 10:49:27 +08003309 }
Johannes Berg2d0ddec2009-04-23 16:13:26 +02003310
Bob Copeland21800492009-07-04 12:59:52 -04003311 if (changes & BSS_CHANGED_BEACON) {
3312 spin_lock_irqsave(&sc->block, flags);
3313 ath5k_beacon_update(hw, vif);
3314 spin_unlock_irqrestore(&sc->block, flags);
Johannes Berg2d0ddec2009-04-23 16:13:26 +02003315 }
3316
Bob Copeland21800492009-07-04 12:59:52 -04003317 if (changes & BSS_CHANGED_BEACON_ENABLED)
3318 sc->enable_beacon = bss_conf->enable_beacon;
3319
3320 if (changes & (BSS_CHANGED_BEACON | BSS_CHANGED_BEACON_ENABLED |
3321 BSS_CHANGED_BEACON_INT))
3322 ath5k_beacon_config(sc);
3323
Johannes Berg2d0ddec2009-04-23 16:13:26 +02003324 mutex_unlock(&sc->lock);
Martin Xu02969b32008-11-24 10:49:27 +08003325}
Bob Copelandf0f3d382009-06-10 22:22:21 -04003326
3327static void ath5k_sw_scan_start(struct ieee80211_hw *hw)
3328{
3329 struct ath5k_softc *sc = hw->priv;
3330 if (!sc->assoc)
3331 ath5k_hw_set_ledstate(sc->ah, AR5K_LED_SCAN);
3332}
3333
3334static void ath5k_sw_scan_complete(struct ieee80211_hw *hw)
3335{
3336 struct ath5k_softc *sc = hw->priv;
3337 ath5k_hw_set_ledstate(sc->ah, sc->assoc ?
3338 AR5K_LED_ASSOC : AR5K_LED_INIT);
3339}
Lukáš Turek6e08d222009-12-21 22:50:51 +01003340
3341/**
3342 * ath5k_set_coverage_class - Set IEEE 802.11 coverage class
3343 *
3344 * @hw: struct ieee80211_hw pointer
3345 * @coverage_class: IEEE 802.11 coverage class number
3346 *
3347 * Mac80211 callback. Sets slot time, ACK timeout and CTS timeout for given
3348 * coverage class. The values are persistent, they are restored after device
3349 * reset.
3350 */
3351static void ath5k_set_coverage_class(struct ieee80211_hw *hw, u8 coverage_class)
3352{
3353 struct ath5k_softc *sc = hw->priv;
3354
3355 mutex_lock(&sc->lock);
3356 ath5k_hw_set_coverage_class(sc->ah, coverage_class);
3357 mutex_unlock(&sc->lock);
3358}
Bob Copeland8a63fac2010-09-17 12:45:07 +09003359
Bruno Randolfe0b1cc52010-09-17 11:37:18 +09003360static int ath5k_conf_tx(struct ieee80211_hw *hw, u16 queue,
3361 const struct ieee80211_tx_queue_params *params)
3362{
3363 struct ath5k_softc *sc = hw->priv;
3364 struct ath5k_hw *ah = sc->ah;
3365 struct ath5k_txq_info qi;
3366 int ret = 0;
3367
3368 if (queue >= ah->ah_capabilities.cap_queues.q_tx_num)
3369 return 0;
3370
3371 mutex_lock(&sc->lock);
3372
3373 ath5k_hw_get_tx_queueprops(ah, queue, &qi);
3374
3375 qi.tqi_aifs = params->aifs;
3376 qi.tqi_cw_min = params->cw_min;
3377 qi.tqi_cw_max = params->cw_max;
3378 qi.tqi_burst_time = params->txop;
3379
3380 ATH5K_DBG(sc, ATH5K_DEBUG_ANY,
3381 "Configure tx [queue %d], "
3382 "aifs: %d, cw_min: %d, cw_max: %d, txop: %d\n",
3383 queue, params->aifs, params->cw_min,
3384 params->cw_max, params->txop);
3385
3386 if (ath5k_hw_set_tx_queueprops(ah, queue, &qi)) {
3387 ATH5K_ERR(sc,
3388 "Unable to update hardware queue %u!\n", queue);
3389 ret = -EIO;
3390 } else
3391 ath5k_hw_reset_tx_queue(ah, queue);
3392
3393 mutex_unlock(&sc->lock);
3394
3395 return ret;
3396}
3397
Bruno Randolf72a80112010-11-10 12:51:01 +09003398static int ath5k_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
3399{
3400 struct ath5k_softc *sc = hw->priv;
3401
3402 if (tx_ant == 1 && rx_ant == 1)
3403 ath5k_hw_set_antenna_mode(sc->ah, AR5K_ANTMODE_FIXED_A);
3404 else if (tx_ant == 2 && rx_ant == 2)
3405 ath5k_hw_set_antenna_mode(sc->ah, AR5K_ANTMODE_FIXED_B);
3406 else if ((tx_ant & 3) == 3 && (rx_ant & 3) == 3)
3407 ath5k_hw_set_antenna_mode(sc->ah, AR5K_ANTMODE_DEFAULT);
3408 else
3409 return -EINVAL;
3410 return 0;
3411}
3412
3413static int ath5k_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
3414{
3415 struct ath5k_softc *sc = hw->priv;
3416
3417 switch (sc->ah->ah_ant_mode) {
3418 case AR5K_ANTMODE_FIXED_A:
3419 *tx_ant = 1; *rx_ant = 1; break;
3420 case AR5K_ANTMODE_FIXED_B:
3421 *tx_ant = 2; *rx_ant = 2; break;
3422 case AR5K_ANTMODE_DEFAULT:
3423 *tx_ant = 3; *rx_ant = 3; break;
3424 }
3425 return 0;
3426}
3427
Bob Copeland8a63fac2010-09-17 12:45:07 +09003428static const struct ieee80211_ops ath5k_hw_ops = {
3429 .tx = ath5k_tx,
3430 .start = ath5k_start,
3431 .stop = ath5k_stop,
3432 .add_interface = ath5k_add_interface,
3433 .remove_interface = ath5k_remove_interface,
3434 .config = ath5k_config,
3435 .prepare_multicast = ath5k_prepare_multicast,
3436 .configure_filter = ath5k_configure_filter,
3437 .set_key = ath5k_set_key,
3438 .get_stats = ath5k_get_stats,
3439 .get_survey = ath5k_get_survey,
Bruno Randolfe0b1cc52010-09-17 11:37:18 +09003440 .conf_tx = ath5k_conf_tx,
Bob Copeland8a63fac2010-09-17 12:45:07 +09003441 .get_tsf = ath5k_get_tsf,
3442 .set_tsf = ath5k_set_tsf,
3443 .reset_tsf = ath5k_reset_tsf,
3444 .bss_info_changed = ath5k_bss_info_changed,
3445 .sw_scan_start = ath5k_sw_scan_start,
3446 .sw_scan_complete = ath5k_sw_scan_complete,
3447 .set_coverage_class = ath5k_set_coverage_class,
Bruno Randolf72a80112010-11-10 12:51:01 +09003448 .set_antenna = ath5k_set_antenna,
3449 .get_antenna = ath5k_get_antenna,
Bob Copeland8a63fac2010-09-17 12:45:07 +09003450};
3451
3452/********************\
3453* PCI Initialization *
3454\********************/
3455
3456static int __devinit
3457ath5k_pci_probe(struct pci_dev *pdev,
3458 const struct pci_device_id *id)
3459{
3460 void __iomem *mem;
3461 struct ath5k_softc *sc;
3462 struct ath_common *common;
3463 struct ieee80211_hw *hw;
3464 int ret;
3465 u8 csz;
3466
3467 /*
3468 * L0s needs to be disabled on all ath5k cards.
3469 *
3470 * For distributions shipping with CONFIG_PCIEASPM (this will be enabled
3471 * by default in the future in 2.6.36) this will also mean both L1 and
3472 * L0s will be disabled when a pre 1.1 PCIe device is detected. We do
3473 * know L1 works correctly even for all ath5k pre 1.1 PCIe devices
3474 * though but cannot currently undue the effect of a blacklist, for
3475 * details you can read pcie_aspm_sanity_check() and see how it adjusts
3476 * the device link capability.
3477 *
3478 * It may be possible in the future to implement some PCI API to allow
3479 * drivers to override blacklists for pre 1.1 PCIe but for now it is
3480 * best to accept that both L0s and L1 will be disabled completely for
3481 * distributions shipping with CONFIG_PCIEASPM rather than having this
3482 * issue present. Motivation for adding this new API will be to help
3483 * with power consumption for some of these devices.
3484 */
3485 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S);
3486
3487 ret = pci_enable_device(pdev);
3488 if (ret) {
3489 dev_err(&pdev->dev, "can't enable device\n");
3490 goto err;
3491 }
3492
3493 /* XXX 32-bit addressing only */
3494 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
3495 if (ret) {
3496 dev_err(&pdev->dev, "32-bit DMA not available\n");
3497 goto err_dis;
3498 }
3499
3500 /*
3501 * Cache line size is used to size and align various
3502 * structures used to communicate with the hardware.
3503 */
3504 pci_read_config_byte(pdev, PCI_CACHE_LINE_SIZE, &csz);
3505 if (csz == 0) {
3506 /*
3507 * Linux 2.4.18 (at least) writes the cache line size
3508 * register as a 16-bit wide register which is wrong.
3509 * We must have this setup properly for rx buffer
3510 * DMA to work so force a reasonable value here if it
3511 * comes up zero.
3512 */
3513 csz = L1_CACHE_BYTES >> 2;
3514 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, csz);
3515 }
3516 /*
3517 * The default setting of latency timer yields poor results,
3518 * set it to the value used by other systems. It may be worth
3519 * tweaking this setting more.
3520 */
3521 pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0xa8);
3522
3523 /* Enable bus mastering */
3524 pci_set_master(pdev);
3525
3526 /*
3527 * Disable the RETRY_TIMEOUT register (0x41) to keep
3528 * PCI Tx retries from interfering with C3 CPU state.
3529 */
3530 pci_write_config_byte(pdev, 0x41, 0);
3531
3532 ret = pci_request_region(pdev, 0, "ath5k");
3533 if (ret) {
3534 dev_err(&pdev->dev, "cannot reserve PCI memory region\n");
3535 goto err_dis;
3536 }
3537
3538 mem = pci_iomap(pdev, 0, 0);
3539 if (!mem) {
3540 dev_err(&pdev->dev, "cannot remap PCI memory region\n") ;
3541 ret = -EIO;
3542 goto err_reg;
3543 }
3544
3545 /*
3546 * Allocate hw (mac80211 main struct)
3547 * and hw->priv (driver private data)
3548 */
3549 hw = ieee80211_alloc_hw(sizeof(*sc), &ath5k_hw_ops);
3550 if (hw == NULL) {
3551 dev_err(&pdev->dev, "cannot allocate ieee80211_hw\n");
3552 ret = -ENOMEM;
3553 goto err_map;
3554 }
3555
3556 dev_info(&pdev->dev, "registered as '%s'\n", wiphy_name(hw->wiphy));
3557
3558 /* Initialize driver private data */
3559 SET_IEEE80211_DEV(hw, &pdev->dev);
3560 hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
3561 IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
3562 IEEE80211_HW_SIGNAL_DBM;
3563
3564 hw->wiphy->interface_modes =
3565 BIT(NL80211_IFTYPE_AP) |
3566 BIT(NL80211_IFTYPE_STATION) |
3567 BIT(NL80211_IFTYPE_ADHOC) |
3568 BIT(NL80211_IFTYPE_MESH_POINT);
3569
3570 hw->extra_tx_headroom = 2;
3571 hw->channel_change_time = 5000;
3572 sc = hw->priv;
3573 sc->hw = hw;
3574 sc->pdev = pdev;
3575
Bob Copeland8a63fac2010-09-17 12:45:07 +09003576 /*
3577 * Mark the device as detached to avoid processing
3578 * interrupts until setup is complete.
3579 */
3580 __set_bit(ATH_STAT_INVALID, sc->status);
3581
3582 sc->iobase = mem; /* So we can unmap it on detach */
3583 sc->opmode = NL80211_IFTYPE_STATION;
3584 sc->bintval = 1000;
3585 mutex_init(&sc->lock);
3586 spin_lock_init(&sc->rxbuflock);
3587 spin_lock_init(&sc->txbuflock);
3588 spin_lock_init(&sc->block);
3589
3590 /* Set private data */
3591 pci_set_drvdata(pdev, sc);
3592
3593 /* Setup interrupt handler */
3594 ret = request_irq(pdev->irq, ath5k_intr, IRQF_SHARED, "ath", sc);
3595 if (ret) {
3596 ATH5K_ERR(sc, "request_irq failed\n");
3597 goto err_free;
3598 }
3599
3600 /* If we passed the test, malloc an ath5k_hw struct */
3601 sc->ah = kzalloc(sizeof(struct ath5k_hw), GFP_KERNEL);
3602 if (!sc->ah) {
3603 ret = -ENOMEM;
3604 ATH5K_ERR(sc, "out of memory\n");
3605 goto err_irq;
3606 }
3607
3608 sc->ah->ah_sc = sc;
3609 sc->ah->ah_iobase = sc->iobase;
3610 common = ath5k_hw_common(sc->ah);
3611 common->ops = &ath5k_common_ops;
3612 common->ah = sc->ah;
3613 common->hw = hw;
3614 common->cachelsz = csz << 2; /* convert to bytes */
Ben Greear9192f712010-10-15 15:51:32 -07003615 spin_lock_init(&common->cc_lock);
Bob Copeland8a63fac2010-09-17 12:45:07 +09003616
3617 /* Initialize device */
3618 ret = ath5k_hw_attach(sc);
3619 if (ret) {
3620 goto err_free_ah;
3621 }
3622
3623 /* set up multi-rate retry capabilities */
3624 if (sc->ah->ah_version == AR5K_AR5212) {
3625 hw->max_rates = 4;
3626 hw->max_rate_tries = 11;
3627 }
3628
Ben Greearb1ae1ed2010-09-30 12:22:58 -07003629 hw->vif_data_size = sizeof(struct ath5k_vif);
3630
Bob Copeland8a63fac2010-09-17 12:45:07 +09003631 /* Finish private driver data initialization */
3632 ret = ath5k_attach(pdev, hw);
3633 if (ret)
3634 goto err_ah;
3635
3636 ATH5K_INFO(sc, "Atheros AR%s chip found (MAC: 0x%x, PHY: 0x%x)\n",
3637 ath5k_chip_name(AR5K_VERSION_MAC, sc->ah->ah_mac_srev),
3638 sc->ah->ah_mac_srev,
3639 sc->ah->ah_phy_revision);
3640
3641 if (!sc->ah->ah_single_chip) {
3642 /* Single chip radio (!RF5111) */
3643 if (sc->ah->ah_radio_5ghz_revision &&
3644 !sc->ah->ah_radio_2ghz_revision) {
3645 /* No 5GHz support -> report 2GHz radio */
3646 if (!test_bit(AR5K_MODE_11A,
3647 sc->ah->ah_capabilities.cap_mode)) {
3648 ATH5K_INFO(sc, "RF%s 2GHz radio found (0x%x)\n",
3649 ath5k_chip_name(AR5K_VERSION_RAD,
3650 sc->ah->ah_radio_5ghz_revision),
3651 sc->ah->ah_radio_5ghz_revision);
3652 /* No 2GHz support (5110 and some
3653 * 5Ghz only cards) -> report 5Ghz radio */
3654 } else if (!test_bit(AR5K_MODE_11B,
3655 sc->ah->ah_capabilities.cap_mode)) {
3656 ATH5K_INFO(sc, "RF%s 5GHz radio found (0x%x)\n",
3657 ath5k_chip_name(AR5K_VERSION_RAD,
3658 sc->ah->ah_radio_5ghz_revision),
3659 sc->ah->ah_radio_5ghz_revision);
3660 /* Multiband radio */
3661 } else {
3662 ATH5K_INFO(sc, "RF%s multiband radio found"
3663 " (0x%x)\n",
3664 ath5k_chip_name(AR5K_VERSION_RAD,
3665 sc->ah->ah_radio_5ghz_revision),
3666 sc->ah->ah_radio_5ghz_revision);
3667 }
3668 }
3669 /* Multi chip radio (RF5111 - RF2111) ->
3670 * report both 2GHz/5GHz radios */
3671 else if (sc->ah->ah_radio_5ghz_revision &&
3672 sc->ah->ah_radio_2ghz_revision){
3673 ATH5K_INFO(sc, "RF%s 5GHz radio found (0x%x)\n",
3674 ath5k_chip_name(AR5K_VERSION_RAD,
3675 sc->ah->ah_radio_5ghz_revision),
3676 sc->ah->ah_radio_5ghz_revision);
3677 ATH5K_INFO(sc, "RF%s 2GHz radio found (0x%x)\n",
3678 ath5k_chip_name(AR5K_VERSION_RAD,
3679 sc->ah->ah_radio_2ghz_revision),
3680 sc->ah->ah_radio_2ghz_revision);
3681 }
3682 }
3683
Ben Greeard84a35d2010-10-12 10:55:38 -07003684 ath5k_debug_init_device(sc);
Bob Copeland8a63fac2010-09-17 12:45:07 +09003685
3686 /* ready to process interrupts */
3687 __clear_bit(ATH_STAT_INVALID, sc->status);
3688
3689 return 0;
3690err_ah:
3691 ath5k_hw_detach(sc->ah);
3692err_free_ah:
3693 kfree(sc->ah);
3694err_irq:
3695 free_irq(pdev->irq, sc);
3696err_free:
3697 ieee80211_free_hw(hw);
3698err_map:
3699 pci_iounmap(pdev, mem);
3700err_reg:
3701 pci_release_region(pdev, 0);
3702err_dis:
3703 pci_disable_device(pdev);
3704err:
3705 return ret;
3706}
3707
3708static void __devexit
3709ath5k_pci_remove(struct pci_dev *pdev)
3710{
3711 struct ath5k_softc *sc = pci_get_drvdata(pdev);
3712
3713 ath5k_debug_finish_device(sc);
3714 ath5k_detach(pdev, sc->hw);
3715 ath5k_hw_detach(sc->ah);
3716 kfree(sc->ah);
3717 free_irq(pdev->irq, sc);
3718 pci_iounmap(pdev, sc->iobase);
3719 pci_release_region(pdev, 0);
3720 pci_disable_device(pdev);
3721 ieee80211_free_hw(sc->hw);
3722}
3723
3724#ifdef CONFIG_PM_SLEEP
3725static int ath5k_pci_suspend(struct device *dev)
3726{
3727 struct ath5k_softc *sc = pci_get_drvdata(to_pci_dev(dev));
3728
3729 ath5k_led_off(sc);
3730 return 0;
3731}
3732
3733static int ath5k_pci_resume(struct device *dev)
3734{
3735 struct pci_dev *pdev = to_pci_dev(dev);
3736 struct ath5k_softc *sc = pci_get_drvdata(pdev);
3737
3738 /*
3739 * Suspend/Resume resets the PCI configuration space, so we have to
3740 * re-disable the RETRY_TIMEOUT register (0x41) to keep
3741 * PCI Tx retries from interfering with C3 CPU state
3742 */
3743 pci_write_config_byte(pdev, 0x41, 0);
3744
3745 ath5k_led_enable(sc);
3746 return 0;
3747}
3748
3749static SIMPLE_DEV_PM_OPS(ath5k_pm_ops, ath5k_pci_suspend, ath5k_pci_resume);
3750#define ATH5K_PM_OPS (&ath5k_pm_ops)
3751#else
3752#define ATH5K_PM_OPS NULL
3753#endif /* CONFIG_PM_SLEEP */
3754
3755static struct pci_driver ath5k_pci_driver = {
3756 .name = KBUILD_MODNAME,
3757 .id_table = ath5k_pci_id_table,
3758 .probe = ath5k_pci_probe,
3759 .remove = __devexit_p(ath5k_pci_remove),
3760 .driver.pm = ATH5K_PM_OPS,
3761};
3762
3763/*
3764 * Module init/exit functions
3765 */
3766static int __init
3767init_ath5k_pci(void)
3768{
3769 int ret;
3770
Bob Copeland8a63fac2010-09-17 12:45:07 +09003771 ret = pci_register_driver(&ath5k_pci_driver);
3772 if (ret) {
3773 printk(KERN_ERR "ath5k_pci: can't register pci driver\n");
3774 return ret;
3775 }
3776
3777 return 0;
3778}
3779
3780static void __exit
3781exit_ath5k_pci(void)
3782{
3783 pci_unregister_driver(&ath5k_pci_driver);
Bob Copeland8a63fac2010-09-17 12:45:07 +09003784}
3785
3786module_init(init_ath5k_pci);
3787module_exit(exit_ath5k_pci);