blob: 92d139bbcc31b3141f49f8e25a2cfdbc88f3829e [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>
Jiri Slabyfa1c1142007-08-12 17:33:16 +020055
56#include <net/ieee80211_radiotap.h>
57
58#include <asm/unaligned.h>
59
60#include "base.h"
61#include "reg.h"
62#include "debug.h"
Bruno Randolf2111ac02010-04-02 18:44:08 +090063#include "ani.h"
Jiri Slabyfa1c1142007-08-12 17:33:16 +020064
Bob Copeland9ad9a262008-10-29 08:30:54 -040065static int modparam_nohwcrypt;
Bob Copeland46802a42009-04-15 07:57:34 -040066module_param_named(nohwcrypt, modparam_nohwcrypt, bool, S_IRUGO);
Bob Copeland9ad9a262008-10-29 08:30:54 -040067MODULE_PARM_DESC(nohwcrypt, "Disable hardware encryption.");
Jiri Slabyfa1c1142007-08-12 17:33:16 +020068
Bob Copeland42639fc2009-03-30 08:05:29 -040069static int modparam_all_channels;
Bob Copeland46802a42009-04-15 07:57:34 -040070module_param_named(all_channels, modparam_all_channels, bool, S_IRUGO);
Bob Copeland42639fc2009-03-30 08:05:29 -040071MODULE_PARM_DESC(all_channels, "Expose all channels the device can use.");
72
Jiri Slabyfa1c1142007-08-12 17:33:16 +020073/* Module info */
74MODULE_AUTHOR("Jiri Slaby");
75MODULE_AUTHOR("Nick Kossifidis");
76MODULE_DESCRIPTION("Support for 5xxx series of Atheros 802.11 wireless LAN cards.");
77MODULE_SUPPORTED_DEVICE("Atheros 5xxx WLAN cards");
78MODULE_LICENSE("Dual BSD/GPL");
Nick Kossifidis0d5f0312008-09-29 01:27:27 +030079MODULE_VERSION("0.6.0 (EXPERIMENTAL)");
Jiri Slabyfa1c1142007-08-12 17:33:16 +020080
Bob Copeland8a63fac2010-09-17 12:45:07 +090081static int ath5k_reset(struct ath5k_softc *sc, struct ieee80211_channel *chan);
82static int ath5k_beacon_update(struct ieee80211_hw *hw,
83 struct ieee80211_vif *vif);
84static void ath5k_beacon_update_timers(struct ath5k_softc *sc, u64 bc_tsf);
Jiri Slabyfa1c1142007-08-12 17:33:16 +020085
86/* Known PCI ids */
Alexey Dobriyana3aa1882010-01-07 11:58:11 +000087static DEFINE_PCI_DEVICE_TABLE(ath5k_pci_id_table) = {
Pavel Roskin97a81f52009-08-26 22:30:09 -040088 { PCI_VDEVICE(ATHEROS, 0x0207) }, /* 5210 early */
89 { PCI_VDEVICE(ATHEROS, 0x0007) }, /* 5210 */
90 { PCI_VDEVICE(ATHEROS, 0x0011) }, /* 5311 - this is on AHB bus !*/
91 { PCI_VDEVICE(ATHEROS, 0x0012) }, /* 5211 */
92 { PCI_VDEVICE(ATHEROS, 0x0013) }, /* 5212 */
93 { PCI_VDEVICE(3COM_2, 0x0013) }, /* 3com 5212 */
94 { PCI_VDEVICE(3COM, 0x0013) }, /* 3com 3CRDAG675 5212 */
95 { PCI_VDEVICE(ATHEROS, 0x1014) }, /* IBM minipci 5212 */
96 { PCI_VDEVICE(ATHEROS, 0x0014) }, /* 5212 combatible */
97 { PCI_VDEVICE(ATHEROS, 0x0015) }, /* 5212 combatible */
98 { PCI_VDEVICE(ATHEROS, 0x0016) }, /* 5212 combatible */
99 { PCI_VDEVICE(ATHEROS, 0x0017) }, /* 5212 combatible */
100 { PCI_VDEVICE(ATHEROS, 0x0018) }, /* 5212 combatible */
101 { PCI_VDEVICE(ATHEROS, 0x0019) }, /* 5212 combatible */
102 { PCI_VDEVICE(ATHEROS, 0x001a) }, /* 2413 Griffin-lite */
103 { PCI_VDEVICE(ATHEROS, 0x001b) }, /* 5413 Eagle */
104 { PCI_VDEVICE(ATHEROS, 0x001c) }, /* PCI-E cards */
105 { PCI_VDEVICE(ATHEROS, 0x001d) }, /* 2417 Nala */
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200106 { 0 }
107};
108MODULE_DEVICE_TABLE(pci, ath5k_pci_id_table);
109
110/* Known SREVs */
Jiri Slaby2c91108c2009-03-07 10:26:41 +0100111static const struct ath5k_srev_name srev_names[] = {
Nick Kossifidis1bef0162008-09-29 02:09:09 +0300112 { "5210", AR5K_VERSION_MAC, AR5K_SREV_AR5210 },
113 { "5311", AR5K_VERSION_MAC, AR5K_SREV_AR5311 },
114 { "5311A", AR5K_VERSION_MAC, AR5K_SREV_AR5311A },
115 { "5311B", AR5K_VERSION_MAC, AR5K_SREV_AR5311B },
116 { "5211", AR5K_VERSION_MAC, AR5K_SREV_AR5211 },
117 { "5212", AR5K_VERSION_MAC, AR5K_SREV_AR5212 },
118 { "5213", AR5K_VERSION_MAC, AR5K_SREV_AR5213 },
119 { "5213A", AR5K_VERSION_MAC, AR5K_SREV_AR5213A },
120 { "2413", AR5K_VERSION_MAC, AR5K_SREV_AR2413 },
121 { "2414", AR5K_VERSION_MAC, AR5K_SREV_AR2414 },
122 { "5424", AR5K_VERSION_MAC, AR5K_SREV_AR5424 },
123 { "5413", AR5K_VERSION_MAC, AR5K_SREV_AR5413 },
124 { "5414", AR5K_VERSION_MAC, AR5K_SREV_AR5414 },
125 { "2415", AR5K_VERSION_MAC, AR5K_SREV_AR2415 },
126 { "5416", AR5K_VERSION_MAC, AR5K_SREV_AR5416 },
127 { "5418", AR5K_VERSION_MAC, AR5K_SREV_AR5418 },
128 { "2425", AR5K_VERSION_MAC, AR5K_SREV_AR2425 },
129 { "2417", AR5K_VERSION_MAC, AR5K_SREV_AR2417 },
130 { "xxxxx", AR5K_VERSION_MAC, AR5K_SREV_UNKNOWN },
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200131 { "5110", AR5K_VERSION_RAD, AR5K_SREV_RAD_5110 },
132 { "5111", AR5K_VERSION_RAD, AR5K_SREV_RAD_5111 },
Nick Kossifidis1bef0162008-09-29 02:09:09 +0300133 { "5111A", AR5K_VERSION_RAD, AR5K_SREV_RAD_5111A },
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200134 { "2111", AR5K_VERSION_RAD, AR5K_SREV_RAD_2111 },
135 { "5112", AR5K_VERSION_RAD, AR5K_SREV_RAD_5112 },
136 { "5112A", AR5K_VERSION_RAD, AR5K_SREV_RAD_5112A },
Nick Kossifidis1bef0162008-09-29 02:09:09 +0300137 { "5112B", AR5K_VERSION_RAD, AR5K_SREV_RAD_5112B },
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200138 { "2112", AR5K_VERSION_RAD, AR5K_SREV_RAD_2112 },
139 { "2112A", AR5K_VERSION_RAD, AR5K_SREV_RAD_2112A },
Nick Kossifidis1bef0162008-09-29 02:09:09 +0300140 { "2112B", AR5K_VERSION_RAD, AR5K_SREV_RAD_2112B },
141 { "2413", AR5K_VERSION_RAD, AR5K_SREV_RAD_2413 },
142 { "5413", AR5K_VERSION_RAD, AR5K_SREV_RAD_5413 },
143 { "2316", AR5K_VERSION_RAD, AR5K_SREV_RAD_2316 },
144 { "2317", AR5K_VERSION_RAD, AR5K_SREV_RAD_2317 },
145 { "5424", AR5K_VERSION_RAD, AR5K_SREV_RAD_5424 },
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200146 { "5133", AR5K_VERSION_RAD, AR5K_SREV_RAD_5133 },
147 { "xxxxx", AR5K_VERSION_RAD, AR5K_SREV_UNKNOWN },
148};
149
Jiri Slaby2c91108c2009-03-07 10:26:41 +0100150static const struct ieee80211_rate ath5k_rates[] = {
Bruno Randolf63266a62008-07-30 17:12:58 +0200151 { .bitrate = 10,
152 .hw_value = ATH5K_RATE_CODE_1M, },
153 { .bitrate = 20,
154 .hw_value = ATH5K_RATE_CODE_2M,
155 .hw_value_short = ATH5K_RATE_CODE_2M | AR5K_SET_SHORT_PREAMBLE,
156 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
157 { .bitrate = 55,
158 .hw_value = ATH5K_RATE_CODE_5_5M,
159 .hw_value_short = ATH5K_RATE_CODE_5_5M | AR5K_SET_SHORT_PREAMBLE,
160 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
161 { .bitrate = 110,
162 .hw_value = ATH5K_RATE_CODE_11M,
163 .hw_value_short = ATH5K_RATE_CODE_11M | AR5K_SET_SHORT_PREAMBLE,
164 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
165 { .bitrate = 60,
166 .hw_value = ATH5K_RATE_CODE_6M,
167 .flags = 0 },
168 { .bitrate = 90,
169 .hw_value = ATH5K_RATE_CODE_9M,
170 .flags = 0 },
171 { .bitrate = 120,
172 .hw_value = ATH5K_RATE_CODE_12M,
173 .flags = 0 },
174 { .bitrate = 180,
175 .hw_value = ATH5K_RATE_CODE_18M,
176 .flags = 0 },
177 { .bitrate = 240,
178 .hw_value = ATH5K_RATE_CODE_24M,
179 .flags = 0 },
180 { .bitrate = 360,
181 .hw_value = ATH5K_RATE_CODE_36M,
182 .flags = 0 },
183 { .bitrate = 480,
184 .hw_value = ATH5K_RATE_CODE_48M,
185 .flags = 0 },
186 { .bitrate = 540,
187 .hw_value = ATH5K_RATE_CODE_54M,
188 .flags = 0 },
189 /* XR missing */
190};
191
Bruno Randolf9e4e43f2010-06-16 19:11:17 +0900192static inline void ath5k_txbuf_free_skb(struct ath5k_softc *sc,
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200193 struct ath5k_buf *bf)
194{
195 BUG_ON(!bf);
196 if (!bf->skb)
197 return;
198 pci_unmap_single(sc->pdev, bf->skbaddr, bf->skb->len,
199 PCI_DMA_TODEVICE);
Jiri Slaby00482972008-08-18 21:45:27 +0200200 dev_kfree_skb_any(bf->skb);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200201 bf->skb = NULL;
Bruno Randolf39d63f22010-06-16 19:11:41 +0900202 bf->skbaddr = 0;
203 bf->desc->ds_data = 0;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200204}
205
Bruno Randolf9e4e43f2010-06-16 19:11:17 +0900206static inline void ath5k_rxbuf_free_skb(struct ath5k_softc *sc,
Felix Fietkaua6c8d3752009-01-30 01:36:48 +0100207 struct ath5k_buf *bf)
208{
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -0800209 struct ath5k_hw *ah = sc->ah;
210 struct ath_common *common = ath5k_hw_common(ah);
211
Felix Fietkaua6c8d3752009-01-30 01:36:48 +0100212 BUG_ON(!bf);
213 if (!bf->skb)
214 return;
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -0800215 pci_unmap_single(sc->pdev, bf->skbaddr, common->rx_bufsize,
Felix Fietkaua6c8d3752009-01-30 01:36:48 +0100216 PCI_DMA_FROMDEVICE);
217 dev_kfree_skb_any(bf->skb);
218 bf->skb = NULL;
Bruno Randolf39d63f22010-06-16 19:11:41 +0900219 bf->skbaddr = 0;
220 bf->desc->ds_data = 0;
Felix Fietkaua6c8d3752009-01-30 01:36:48 +0100221}
222
223
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200224static inline u64 ath5k_extend_tsf(struct ath5k_hw *ah, u32 rstamp)
225{
226 u64 tsf = ath5k_hw_get_tsf64(ah);
227
228 if ((tsf & 0x7fff) < rstamp)
229 tsf -= 0x8000;
230
231 return (tsf & ~0x7fff) | rstamp;
232}
233
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200234static const char *
235ath5k_chip_name(enum ath5k_srev_type type, u_int16_t val)
236{
237 const char *name = "xxxxx";
238 unsigned int i;
239
240 for (i = 0; i < ARRAY_SIZE(srev_names); i++) {
241 if (srev_names[i].sr_type != type)
242 continue;
Nick Kossifidis75d0edb2008-09-29 01:24:44 +0300243
244 if ((val & 0xf0) == srev_names[i].sr_val)
245 name = srev_names[i].sr_name;
246
247 if ((val & 0xff) == srev_names[i].sr_val) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200248 name = srev_names[i].sr_name;
249 break;
250 }
251 }
252
253 return name;
254}
Luis R. Rodrigueze5aa8472009-09-10 16:55:11 -0700255static unsigned int ath5k_ioread32(void *hw_priv, u32 reg_offset)
256{
257 struct ath5k_hw *ah = (struct ath5k_hw *) hw_priv;
258 return ath5k_hw_reg_read(ah, reg_offset);
259}
260
261static void ath5k_iowrite32(void *hw_priv, u32 val, u32 reg_offset)
262{
263 struct ath5k_hw *ah = (struct ath5k_hw *) hw_priv;
264 ath5k_hw_reg_write(ah, val, reg_offset);
265}
266
267static const struct ath_ops ath5k_common_ops = {
268 .read = ath5k_ioread32,
269 .write = ath5k_iowrite32,
270};
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200271
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200272/***********************\
273* Driver Initialization *
274\***********************/
275
Bob Copelandf769c362009-03-30 22:30:31 -0400276static int ath5k_reg_notifier(struct wiphy *wiphy, struct regulatory_request *request)
277{
278 struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
279 struct ath5k_softc *sc = hw->priv;
Luis R. Rodriguezdb719712009-09-10 11:20:57 -0700280 struct ath_regulatory *regulatory = ath5k_hw_regulatory(sc->ah);
Bob Copelandf769c362009-03-30 22:30:31 -0400281
Luis R. Rodriguez608b88c2009-08-17 18:07:23 -0700282 return ath_reg_notifier_apply(wiphy, request, regulatory);
Bob Copelandf769c362009-03-30 22:30:31 -0400283}
284
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200285/********************\
286* Channel/mode setup *
287\********************/
288
289/*
290 * Convert IEEE channel number to MHz frequency.
291 */
292static inline short
293ath5k_ieee2mhz(short chan)
294{
295 if (chan <= 14 || chan >= 27)
296 return ieee80211chan2mhz(chan);
297 else
298 return 2212 + chan * 20;
299}
300
Bob Copeland42639fc2009-03-30 08:05:29 -0400301/*
302 * Returns true for the channel numbers used without all_channels modparam.
303 */
304static bool ath5k_is_standard_channel(short chan)
305{
306 return ((chan <= 14) ||
307 /* UNII 1,2 */
308 ((chan & 3) == 0 && chan >= 36 && chan <= 64) ||
309 /* midband */
310 ((chan & 3) == 0 && chan >= 100 && chan <= 140) ||
311 /* UNII-3 */
312 ((chan & 3) == 1 && chan >= 149 && chan <= 165));
313}
314
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200315static unsigned int
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200316ath5k_copy_channels(struct ath5k_hw *ah,
317 struct ieee80211_channel *channels,
318 unsigned int mode,
319 unsigned int max)
320{
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500321 unsigned int i, count, size, chfreq, freq, ch;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200322
323 if (!test_bit(mode, ah->ah_modes))
324 return 0;
325
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200326 switch (mode) {
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500327 case AR5K_MODE_11A:
328 case AR5K_MODE_11A_TURBO:
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200329 /* 1..220, but 2GHz frequencies are filtered by check_channel */
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500330 size = 220 ;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200331 chfreq = CHANNEL_5GHZ;
332 break;
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500333 case AR5K_MODE_11B:
334 case AR5K_MODE_11G:
335 case AR5K_MODE_11G_TURBO:
336 size = 26;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200337 chfreq = CHANNEL_2GHZ;
338 break;
339 default:
340 ATH5K_WARN(ah->ah_sc, "bad mode, not copying channels\n");
341 return 0;
342 }
343
344 for (i = 0, count = 0; i < size && max > 0; i++) {
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500345 ch = i + 1 ;
346 freq = ath5k_ieee2mhz(ch);
347
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200348 /* Check if channel is supported by the chipset */
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500349 if (!ath5k_channel_ok(ah, freq, chfreq))
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200350 continue;
351
Bob Copeland42639fc2009-03-30 08:05:29 -0400352 if (!modparam_all_channels && !ath5k_is_standard_channel(ch))
353 continue;
354
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500355 /* Write channel info and increment counter */
356 channels[count].center_freq = freq;
Luis R. Rodrigueza3f4b912008-02-03 21:52:10 -0500357 channels[count].band = (chfreq == CHANNEL_2GHZ) ?
358 IEEE80211_BAND_2GHZ : IEEE80211_BAND_5GHZ;
Luis R. Rodriguez400ec452008-02-03 21:51:49 -0500359 switch (mode) {
360 case AR5K_MODE_11A:
361 case AR5K_MODE_11G:
362 channels[count].hw_value = chfreq | CHANNEL_OFDM;
363 break;
364 case AR5K_MODE_11A_TURBO:
365 case AR5K_MODE_11G_TURBO:
366 channels[count].hw_value = chfreq |
367 CHANNEL_OFDM | CHANNEL_TURBO;
368 break;
369 case AR5K_MODE_11B:
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500370 channels[count].hw_value = CHANNEL_B;
371 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200372
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200373 count++;
374 max--;
375 }
376
377 return count;
378}
379
Bruno Randolf63266a62008-07-30 17:12:58 +0200380static void
381ath5k_setup_rate_idx(struct ath5k_softc *sc, struct ieee80211_supported_band *b)
382{
383 u8 i;
384
385 for (i = 0; i < AR5K_MAX_RATES; i++)
386 sc->rate_idx[b->band][i] = -1;
387
388 for (i = 0; i < b->n_bitrates; i++) {
389 sc->rate_idx[b->band][b->bitrates[i].hw_value] = i;
390 if (b->bitrates[i].hw_value_short)
391 sc->rate_idx[b->band][b->bitrates[i].hw_value_short] = i;
392 }
393}
394
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200395static int
Bruno Randolf63266a62008-07-30 17:12:58 +0200396ath5k_setup_bands(struct ieee80211_hw *hw)
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200397{
398 struct ath5k_softc *sc = hw->priv;
399 struct ath5k_hw *ah = sc->ah;
Bruno Randolf63266a62008-07-30 17:12:58 +0200400 struct ieee80211_supported_band *sband;
401 int max_c, count_c = 0;
402 int i;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200403
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500404 BUILD_BUG_ON(ARRAY_SIZE(sc->sbands) < IEEE80211_NUM_BANDS);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200405 max_c = ARRAY_SIZE(sc->channels);
406
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500407 /* 2GHz band */
Bruno Randolf63266a62008-07-30 17:12:58 +0200408 sband = &sc->sbands[IEEE80211_BAND_2GHZ];
409 sband->band = IEEE80211_BAND_2GHZ;
410 sband->bitrates = &sc->rates[IEEE80211_BAND_2GHZ][0];
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200411
Bruno Randolf63266a62008-07-30 17:12:58 +0200412 if (test_bit(AR5K_MODE_11G, sc->ah->ah_capabilities.cap_mode)) {
413 /* G mode */
414 memcpy(sband->bitrates, &ath5k_rates[0],
415 sizeof(struct ieee80211_rate) * 12);
416 sband->n_bitrates = 12;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200417
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500418 sband->channels = sc->channels;
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500419 sband->n_channels = ath5k_copy_channels(ah, sband->channels,
Bruno Randolf63266a62008-07-30 17:12:58 +0200420 AR5K_MODE_11G, max_c);
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500421
422 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = sband;
Bruno Randolf63266a62008-07-30 17:12:58 +0200423 count_c = sband->n_channels;
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500424 max_c -= count_c;
Bruno Randolf63266a62008-07-30 17:12:58 +0200425 } else if (test_bit(AR5K_MODE_11B, sc->ah->ah_capabilities.cap_mode)) {
426 /* B mode */
427 memcpy(sband->bitrates, &ath5k_rates[0],
428 sizeof(struct ieee80211_rate) * 4);
429 sband->n_bitrates = 4;
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500430
Bruno Randolf63266a62008-07-30 17:12:58 +0200431 /* 5211 only supports B rates and uses 4bit rate codes
432 * (e.g normally we have 0x1B for 1M, but on 5211 we have 0x0B)
433 * fix them up here:
434 */
435 if (ah->ah_version == AR5K_AR5211) {
436 for (i = 0; i < 4; i++) {
437 sband->bitrates[i].hw_value =
438 sband->bitrates[i].hw_value & 0xF;
439 sband->bitrates[i].hw_value_short =
440 sband->bitrates[i].hw_value_short & 0xF;
441 }
442 }
443
444 sband->channels = sc->channels;
445 sband->n_channels = ath5k_copy_channels(ah, sband->channels,
446 AR5K_MODE_11B, max_c);
447
448 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = sband;
449 count_c = sband->n_channels;
450 max_c -= count_c;
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500451 }
Bruno Randolf63266a62008-07-30 17:12:58 +0200452 ath5k_setup_rate_idx(sc, sband);
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500453
Bruno Randolf63266a62008-07-30 17:12:58 +0200454 /* 5GHz band, A mode */
Luis R. Rodriguez400ec452008-02-03 21:51:49 -0500455 if (test_bit(AR5K_MODE_11A, sc->ah->ah_capabilities.cap_mode)) {
Bruno Randolf63266a62008-07-30 17:12:58 +0200456 sband = &sc->sbands[IEEE80211_BAND_5GHZ];
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500457 sband->band = IEEE80211_BAND_5GHZ;
Bruno Randolf63266a62008-07-30 17:12:58 +0200458 sband->bitrates = &sc->rates[IEEE80211_BAND_5GHZ][0];
459
460 memcpy(sband->bitrates, &ath5k_rates[4],
461 sizeof(struct ieee80211_rate) * 8);
462 sband->n_bitrates = 8;
463
464 sband->channels = &sc->channels[count_c];
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500465 sband->n_channels = ath5k_copy_channels(ah, sband->channels,
466 AR5K_MODE_11A, max_c);
467
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500468 hw->wiphy->bands[IEEE80211_BAND_5GHZ] = sband;
469 }
Bruno Randolf63266a62008-07-30 17:12:58 +0200470 ath5k_setup_rate_idx(sc, sband);
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500471
Luis R. Rodriguezb4461972008-02-04 10:03:54 -0500472 ath5k_debug_dump_bands(sc);
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500473
474 return 0;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200475}
476
477/*
Joerg Alberte30eb4a2009-08-05 01:52:07 +0200478 * Set/change channels. We always reset the chip.
479 * To accomplish this we must first cleanup any pending DMA,
480 * then restart stuff after a la ath5k_init.
Bob Copelandbe009372009-01-22 08:44:16 -0500481 *
482 * Called with sc->lock.
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200483 */
484static int
485ath5k_chan_set(struct ath5k_softc *sc, struct ieee80211_channel *chan)
486{
Bruno Randolf8d67a032010-06-16 19:11:12 +0900487 ATH5K_DBG(sc, ATH5K_DEBUG_RESET,
488 "channel set, resetting (%u -> %u MHz)\n",
489 sc->curchan->center_freq, chan->center_freq);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200490
Joerg Alberte30eb4a2009-08-05 01:52:07 +0200491 /*
492 * To switch channels clear any pending DMA operations;
493 * wait long enough for the RX fifo to drain, reset the
494 * hardware at the new frequency, and then re-enable
495 * the relevant bits of the h/w.
496 */
497 return ath5k_reset(sc, chan);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200498}
499
500static void
501ath5k_setcurmode(struct ath5k_softc *sc, unsigned int mode)
502{
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200503 sc->curmode = mode;
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500504
Luis R. Rodriguez400ec452008-02-03 21:51:49 -0500505 if (mode == AR5K_MODE_11A) {
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500506 sc->curband = &sc->sbands[IEEE80211_BAND_5GHZ];
507 } else {
508 sc->curband = &sc->sbands[IEEE80211_BAND_2GHZ];
509 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200510}
511
512static void
513ath5k_mode_setup(struct ath5k_softc *sc)
514{
515 struct ath5k_hw *ah = sc->ah;
516 u32 rfilt;
517
518 /* configure rx filter */
519 rfilt = sc->filter_flags;
520 ath5k_hw_set_rx_filter(ah, rfilt);
521
522 if (ath5k_hw_hasbssidmask(ah))
523 ath5k_hw_set_bssid_mask(ah, sc->bssidmask);
524
525 /* configure operational mode */
Bruno Randolfccfe5552010-03-09 16:55:38 +0900526 ath5k_hw_set_opmode(ah, sc->opmode);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200527
Bruno Randolfccfe5552010-03-09 16:55:38 +0900528 ATH5K_DBG(sc, ATH5K_DEBUG_MODE, "mode setup opmode %d\n", sc->opmode);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200529 ATH5K_DBG(sc, ATH5K_DEBUG_MODE, "RX filter 0x%x\n", rfilt);
530}
531
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500532static inline int
Bruno Randolf63266a62008-07-30 17:12:58 +0200533ath5k_hw_to_driver_rix(struct ath5k_softc *sc, int hw_rix)
534{
Bob Copelandb7266042009-03-02 21:55:18 -0500535 int rix;
536
537 /* return base rate on errors */
538 if (WARN(hw_rix < 0 || hw_rix >= AR5K_MAX_RATES,
539 "hw_rix out of bounds: %x\n", hw_rix))
540 return 0;
541
542 rix = sc->rate_idx[sc->curband->band][hw_rix];
543 if (WARN(rix < 0, "invalid hw_rix: %x\n", hw_rix))
544 rix = 0;
545
546 return rix;
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -0500547}
548
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200549/***************\
550* Buffers setup *
551\***************/
552
Bob Copelandb6ea0352009-01-10 14:42:54 -0500553static
554struct sk_buff *ath5k_rx_skb_alloc(struct ath5k_softc *sc, dma_addr_t *skb_addr)
555{
Luis R. Rodriguezdb719712009-09-10 11:20:57 -0700556 struct ath_common *common = ath5k_hw_common(sc->ah);
Bob Copelandb6ea0352009-01-10 14:42:54 -0500557 struct sk_buff *skb;
Bob Copelandb6ea0352009-01-10 14:42:54 -0500558
559 /*
560 * Allocate buffer with headroom_needed space for the
561 * fake physical layer header at the start.
562 */
Luis R. Rodriguezdb719712009-09-10 11:20:57 -0700563 skb = ath_rxbuf_alloc(common,
Luis R. Rodriguezdd849782009-11-04 09:44:50 -0800564 common->rx_bufsize,
Luis R. Rodriguezaeb63cf2009-08-12 09:57:00 -0700565 GFP_ATOMIC);
Bob Copelandb6ea0352009-01-10 14:42:54 -0500566
567 if (!skb) {
568 ATH5K_ERR(sc, "can't alloc skbuff of size %u\n",
Luis R. Rodriguezdd849782009-11-04 09:44:50 -0800569 common->rx_bufsize);
Bob Copelandb6ea0352009-01-10 14:42:54 -0500570 return NULL;
571 }
Bob Copelandb6ea0352009-01-10 14:42:54 -0500572
573 *skb_addr = pci_map_single(sc->pdev,
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -0800574 skb->data, common->rx_bufsize,
575 PCI_DMA_FROMDEVICE);
Bob Copelandb6ea0352009-01-10 14:42:54 -0500576 if (unlikely(pci_dma_mapping_error(sc->pdev, *skb_addr))) {
577 ATH5K_ERR(sc, "%s: DMA mapping failed\n", __func__);
578 dev_kfree_skb(skb);
579 return NULL;
580 }
581 return skb;
582}
583
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200584static int
585ath5k_rxbuf_setup(struct ath5k_softc *sc, struct ath5k_buf *bf)
586{
587 struct ath5k_hw *ah = sc->ah;
588 struct sk_buff *skb = bf->skb;
589 struct ath5k_desc *ds;
Bruno Randolfb5eae9f2010-05-19 10:18:16 +0900590 int ret;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200591
Bob Copelandb6ea0352009-01-10 14:42:54 -0500592 if (!skb) {
593 skb = ath5k_rx_skb_alloc(sc, &bf->skbaddr);
594 if (!skb)
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200595 return -ENOMEM;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200596 bf->skb = skb;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200597 }
598
599 /*
600 * Setup descriptors. For receive we always terminate
601 * the descriptor list with a self-linked entry so we'll
602 * not get overrun under high load (as can happen with a
603 * 5212 when ANI processing enables PHY error frames).
604 *
Bruno Randolfbeade632010-06-16 19:11:25 +0900605 * To ensure the last descriptor is self-linked we create
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200606 * each descriptor as self-linked and add it to the end. As
607 * each additional descriptor is added the previous self-linked
Bruno Randolfbeade632010-06-16 19:11:25 +0900608 * entry is "fixed" naturally. This should be safe even
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200609 * if DMA is happening. When processing RX interrupts we
610 * never remove/process the last, self-linked, entry on the
Bruno Randolfbeade632010-06-16 19:11:25 +0900611 * descriptor list. This ensures the hardware always has
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200612 * someplace to write a new frame.
613 */
614 ds = bf->desc;
615 ds->ds_link = bf->daddr; /* link to self */
616 ds->ds_data = bf->skbaddr;
Bruno Randolfa6668192010-06-16 19:12:01 +0900617 ret = ath5k_hw_setup_rx_desc(ah, ds, ah->common.rx_bufsize, 0);
Bruno Randolf0452d4a2010-06-16 19:11:35 +0900618 if (ret) {
619 ATH5K_ERR(sc, "%s: could not setup RX desc\n", __func__);
Bruno Randolfb5eae9f2010-05-19 10:18:16 +0900620 return ret;
Bruno Randolf0452d4a2010-06-16 19:11:35 +0900621 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200622
623 if (sc->rxlink != NULL)
624 *sc->rxlink = bf->daddr;
625 sc->rxlink = &ds->ds_link;
626 return 0;
627}
628
Bob Copeland2ac29272010-02-09 13:06:54 -0500629static enum ath5k_pkt_type get_hw_packet_type(struct sk_buff *skb)
630{
631 struct ieee80211_hdr *hdr;
632 enum ath5k_pkt_type htype;
633 __le16 fc;
634
635 hdr = (struct ieee80211_hdr *)skb->data;
636 fc = hdr->frame_control;
637
638 if (ieee80211_is_beacon(fc))
639 htype = AR5K_PKT_TYPE_BEACON;
640 else if (ieee80211_is_probe_resp(fc))
641 htype = AR5K_PKT_TYPE_PROBE_RESP;
642 else if (ieee80211_is_atim(fc))
643 htype = AR5K_PKT_TYPE_ATIM;
644 else if (ieee80211_is_pspoll(fc))
645 htype = AR5K_PKT_TYPE_PSPOLL;
646 else
647 htype = AR5K_PKT_TYPE_NORMAL;
648
649 return htype;
650}
651
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200652static int
Bob Copelandcec8db22009-07-04 12:59:51 -0400653ath5k_txbuf_setup(struct ath5k_softc *sc, struct ath5k_buf *bf,
Benoit Papillault8127fbd2010-02-27 23:05:26 +0100654 struct ath5k_txq *txq, int padsize)
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200655{
656 struct ath5k_hw *ah = sc->ah;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200657 struct ath5k_desc *ds = bf->desc;
658 struct sk_buff *skb = bf->skb;
Johannes Berga888d522008-05-26 16:43:39 +0200659 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200660 unsigned int pktlen, flags, keyidx = AR5K_TXKEYIX_INVALID;
Felix Fietkau2f7fe8702008-10-05 18:05:48 +0200661 struct ieee80211_rate *rate;
662 unsigned int mrr_rate[3], mrr_tries[3];
663 int i, ret;
Bob Copeland8902ff42009-01-22 08:44:20 -0500664 u16 hw_rate;
Bob Copeland07c1e852009-01-22 08:44:21 -0500665 u16 cts_rate = 0;
666 u16 duration = 0;
Bob Copeland8902ff42009-01-22 08:44:20 -0500667 u8 rc_flags;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200668
669 flags = AR5K_TXDESC_INTREQ | AR5K_TXDESC_CLRDMASK;
Johannes Berge039fa42008-05-15 12:55:29 +0200670
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200671 /* XXX endianness */
672 bf->skbaddr = pci_map_single(sc->pdev, skb->data, skb->len,
673 PCI_DMA_TODEVICE);
674
Bob Copeland8902ff42009-01-22 08:44:20 -0500675 rate = ieee80211_get_tx_rate(sc->hw, info);
676
Johannes Berge039fa42008-05-15 12:55:29 +0200677 if (info->flags & IEEE80211_TX_CTL_NO_ACK)
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200678 flags |= AR5K_TXDESC_NOACK;
679
Bob Copeland8902ff42009-01-22 08:44:20 -0500680 rc_flags = info->control.rates[0].flags;
681 hw_rate = (rc_flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE) ?
682 rate->hw_value_short : rate->hw_value;
683
Bruno Randolf281c56d2008-02-05 18:44:55 +0900684 pktlen = skb->len;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200685
Nick Kossifidis8f655dd2009-03-15 22:20:35 +0200686 /* FIXME: If we are in g mode and rate is a CCK rate
687 * subtract ah->ah_txpower.txp_cck_ofdm_pwr_delta
688 * from tx power (value is in dB units already) */
Bob Copeland362695e2009-02-15 12:06:12 -0500689 if (info->control.hw_key) {
690 keyidx = info->control.hw_key->hw_key_idx;
691 pktlen += info->control.hw_key->icv_len;
692 }
Bob Copeland07c1e852009-01-22 08:44:21 -0500693 if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
694 flags |= AR5K_TXDESC_RTSENA;
695 cts_rate = ieee80211_get_rts_cts_rate(sc->hw, info)->hw_value;
696 duration = le16_to_cpu(ieee80211_rts_duration(sc->hw,
697 sc->vif, pktlen, info));
698 }
699 if (rc_flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
700 flags |= AR5K_TXDESC_CTSENA;
701 cts_rate = ieee80211_get_rts_cts_rate(sc->hw, info)->hw_value;
702 duration = le16_to_cpu(ieee80211_ctstoself_duration(sc->hw,
703 sc->vif, pktlen, info));
704 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200705 ret = ah->ah_setup_tx_desc(ah, ds, pktlen,
Benoit Papillault8127fbd2010-02-27 23:05:26 +0100706 ieee80211_get_hdrlen_from_skb(skb), padsize,
Bob Copeland2ac29272010-02-09 13:06:54 -0500707 get_hw_packet_type(skb),
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200708 (sc->power_level * 2),
Bob Copeland8902ff42009-01-22 08:44:20 -0500709 hw_rate,
Nick Kossifidis2bed03e2009-04-30 15:55:49 -0400710 info->control.rates[0].count, keyidx, ah->ah_tx_ant, flags,
Bob Copeland07c1e852009-01-22 08:44:21 -0500711 cts_rate, duration);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200712 if (ret)
713 goto err_unmap;
714
Felix Fietkau2f7fe8702008-10-05 18:05:48 +0200715 memset(mrr_rate, 0, sizeof(mrr_rate));
716 memset(mrr_tries, 0, sizeof(mrr_tries));
717 for (i = 0; i < 3; i++) {
718 rate = ieee80211_get_alt_retry_rate(sc->hw, info, i);
719 if (!rate)
720 break;
721
722 mrr_rate[i] = rate->hw_value;
Johannes Berge6a98542008-10-21 12:40:02 +0200723 mrr_tries[i] = info->control.rates[i + 1].count;
Felix Fietkau2f7fe8702008-10-05 18:05:48 +0200724 }
725
Bruno Randolfa6668192010-06-16 19:12:01 +0900726 ath5k_hw_setup_mrr_tx_desc(ah, ds,
Felix Fietkau2f7fe8702008-10-05 18:05:48 +0200727 mrr_rate[0], mrr_tries[0],
728 mrr_rate[1], mrr_tries[1],
729 mrr_rate[2], mrr_tries[2]);
730
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200731 ds->ds_link = 0;
732 ds->ds_data = bf->skbaddr;
733
734 spin_lock_bh(&txq->lock);
735 list_add_tail(&bf->list, &txq->q);
Bruno Randolf925e0b02010-09-17 11:36:35 +0900736 txq->txq_len++;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200737 if (txq->link == NULL) /* is this first packet? */
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300738 ath5k_hw_set_txdp(ah, txq->qnum, bf->daddr);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200739 else /* no, so only link it */
740 *txq->link = bf->daddr;
741
742 txq->link = &ds->ds_link;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300743 ath5k_hw_start_tx_dma(ah, txq->qnum);
Jiri Slaby274c7c32008-07-15 17:44:20 +0200744 mmiowb();
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200745 spin_unlock_bh(&txq->lock);
746
747 return 0;
748err_unmap:
749 pci_unmap_single(sc->pdev, bf->skbaddr, skb->len, PCI_DMA_TODEVICE);
750 return ret;
751}
752
753/*******************\
754* Descriptors setup *
755\*******************/
756
757static int
758ath5k_desc_alloc(struct ath5k_softc *sc, struct pci_dev *pdev)
759{
760 struct ath5k_desc *ds;
761 struct ath5k_buf *bf;
762 dma_addr_t da;
763 unsigned int i;
764 int ret;
765
766 /* allocate descriptors */
767 sc->desc_len = sizeof(struct ath5k_desc) *
768 (ATH_TXBUF + ATH_RXBUF + ATH_BCBUF + 1);
769 sc->desc = pci_alloc_consistent(pdev, sc->desc_len, &sc->desc_daddr);
770 if (sc->desc == NULL) {
771 ATH5K_ERR(sc, "can't allocate descriptors\n");
772 ret = -ENOMEM;
773 goto err;
774 }
775 ds = sc->desc;
776 da = sc->desc_daddr;
777 ATH5K_DBG(sc, ATH5K_DEBUG_ANY, "DMA map: %p (%zu) -> %llx\n",
778 ds, sc->desc_len, (unsigned long long)sc->desc_daddr);
779
780 bf = kcalloc(1 + ATH_TXBUF + ATH_RXBUF + ATH_BCBUF,
781 sizeof(struct ath5k_buf), GFP_KERNEL);
782 if (bf == NULL) {
783 ATH5K_ERR(sc, "can't allocate bufptr\n");
784 ret = -ENOMEM;
785 goto err_free;
786 }
787 sc->bufptr = bf;
788
789 INIT_LIST_HEAD(&sc->rxbuf);
790 for (i = 0; i < ATH_RXBUF; i++, bf++, ds++, da += sizeof(*ds)) {
791 bf->desc = ds;
792 bf->daddr = da;
793 list_add_tail(&bf->list, &sc->rxbuf);
794 }
795
796 INIT_LIST_HEAD(&sc->txbuf);
797 sc->txbuf_len = ATH_TXBUF;
798 for (i = 0; i < ATH_TXBUF; i++, bf++, ds++,
799 da += sizeof(*ds)) {
800 bf->desc = ds;
801 bf->daddr = da;
802 list_add_tail(&bf->list, &sc->txbuf);
803 }
804
805 /* beacon buffer */
806 bf->desc = ds;
807 bf->daddr = da;
808 sc->bbuf = bf;
809
810 return 0;
811err_free:
812 pci_free_consistent(pdev, sc->desc_len, sc->desc, sc->desc_daddr);
813err:
814 sc->desc = NULL;
815 return ret;
816}
817
818static void
819ath5k_desc_free(struct ath5k_softc *sc, struct pci_dev *pdev)
820{
821 struct ath5k_buf *bf;
822
Bruno Randolf9e4e43f2010-06-16 19:11:17 +0900823 ath5k_txbuf_free_skb(sc, sc->bbuf);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200824 list_for_each_entry(bf, &sc->txbuf, list)
Bruno Randolf9e4e43f2010-06-16 19:11:17 +0900825 ath5k_txbuf_free_skb(sc, bf);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200826 list_for_each_entry(bf, &sc->rxbuf, list)
Bruno Randolf9e4e43f2010-06-16 19:11:17 +0900827 ath5k_rxbuf_free_skb(sc, bf);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200828
829 /* Free memory associated with all descriptors */
830 pci_free_consistent(pdev, sc->desc_len, sc->desc, sc->desc_daddr);
Bruno Randolf39d63f22010-06-16 19:11:41 +0900831 sc->desc = NULL;
832 sc->desc_daddr = 0;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200833
834 kfree(sc->bufptr);
835 sc->bufptr = NULL;
Bruno Randolf39d63f22010-06-16 19:11:41 +0900836 sc->bbuf = NULL;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200837}
838
839
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200840/**************\
841* Queues setup *
842\**************/
843
844static struct ath5k_txq *
845ath5k_txq_setup(struct ath5k_softc *sc,
846 int qtype, int subtype)
847{
848 struct ath5k_hw *ah = sc->ah;
849 struct ath5k_txq *txq;
850 struct ath5k_txq_info qi = {
851 .tqi_subtype = subtype,
852 .tqi_aifs = AR5K_TXQ_USEDEFAULT,
853 .tqi_cw_min = AR5K_TXQ_USEDEFAULT,
854 .tqi_cw_max = AR5K_TXQ_USEDEFAULT
855 };
856 int qnum;
857
858 /*
859 * Enable interrupts only for EOL and DESC conditions.
860 * We mark tx descriptors to receive a DESC interrupt
Bob Copelanda180a132010-08-15 13:03:12 -0400861 * when a tx queue gets deep; otherwise we wait for the
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200862 * EOL to reap descriptors. Note that this is done to
863 * reduce interrupt load and this only defers reaping
864 * descriptors, never transmitting frames. Aside from
865 * reducing interrupts this also permits more concurrency.
866 * The only potential downside is if the tx queue backs
867 * up in which case the top half of the kernel may backup
868 * due to a lack of tx descriptors.
869 */
870 qi.tqi_flags = AR5K_TXQ_FLAG_TXEOLINT_ENABLE |
871 AR5K_TXQ_FLAG_TXDESCINT_ENABLE;
872 qnum = ath5k_hw_setup_tx_queue(ah, qtype, &qi);
873 if (qnum < 0) {
874 /*
875 * NB: don't print a message, this happens
876 * normally on parts with too few tx queues
877 */
878 return ERR_PTR(qnum);
879 }
880 if (qnum >= ARRAY_SIZE(sc->txqs)) {
881 ATH5K_ERR(sc, "hw qnum %u out of range, max %tu!\n",
882 qnum, ARRAY_SIZE(sc->txqs));
883 ath5k_hw_release_tx_queue(ah, qnum);
884 return ERR_PTR(-EINVAL);
885 }
886 txq = &sc->txqs[qnum];
887 if (!txq->setup) {
888 txq->qnum = qnum;
889 txq->link = NULL;
890 INIT_LIST_HEAD(&txq->q);
891 spin_lock_init(&txq->lock);
892 txq->setup = true;
Bruno Randolf925e0b02010-09-17 11:36:35 +0900893 txq->txq_len = 0;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200894 }
895 return &sc->txqs[qnum];
896}
897
898static int
899ath5k_beaconq_setup(struct ath5k_hw *ah)
900{
901 struct ath5k_txq_info qi = {
902 .tqi_aifs = AR5K_TXQ_USEDEFAULT,
903 .tqi_cw_min = AR5K_TXQ_USEDEFAULT,
904 .tqi_cw_max = AR5K_TXQ_USEDEFAULT,
905 /* NB: for dynamic turbo, don't enable any other interrupts */
906 .tqi_flags = AR5K_TXQ_FLAG_TXDESCINT_ENABLE
907 };
908
909 return ath5k_hw_setup_tx_queue(ah, AR5K_TX_QUEUE_BEACON, &qi);
910}
911
912static int
913ath5k_beaconq_config(struct ath5k_softc *sc)
914{
915 struct ath5k_hw *ah = sc->ah;
916 struct ath5k_txq_info qi;
917 int ret;
918
919 ret = ath5k_hw_get_tx_queueprops(ah, sc->bhalq, &qi);
920 if (ret)
Bob Copelanda951ae22010-01-20 23:51:04 -0500921 goto err;
922
Johannes Berg05c914f2008-09-11 00:01:58 +0200923 if (sc->opmode == NL80211_IFTYPE_AP ||
924 sc->opmode == NL80211_IFTYPE_MESH_POINT) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200925 /*
926 * Always burst out beacon and CAB traffic
927 * (aifs = cwmin = cwmax = 0)
928 */
929 qi.tqi_aifs = 0;
930 qi.tqi_cw_min = 0;
931 qi.tqi_cw_max = 0;
Johannes Berg05c914f2008-09-11 00:01:58 +0200932 } else if (sc->opmode == NL80211_IFTYPE_ADHOC) {
Bruno Randolf6d91e1d2008-01-19 18:18:41 +0900933 /*
934 * Adhoc mode; backoff between 0 and (2 * cw_min).
935 */
936 qi.tqi_aifs = 0;
937 qi.tqi_cw_min = 0;
938 qi.tqi_cw_max = 2 * ah->ah_cw_min;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200939 }
940
Bruno Randolf6d91e1d2008-01-19 18:18:41 +0900941 ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
942 "beacon queueprops tqi_aifs:%d tqi_cw_min:%d tqi_cw_max:%d\n",
943 qi.tqi_aifs, qi.tqi_cw_min, qi.tqi_cw_max);
944
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300945 ret = ath5k_hw_set_tx_queueprops(ah, sc->bhalq, &qi);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200946 if (ret) {
947 ATH5K_ERR(sc, "%s: unable to update parameters for beacon "
948 "hardware queue!\n", __func__);
Bob Copelanda951ae22010-01-20 23:51:04 -0500949 goto err;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200950 }
Bob Copelanda951ae22010-01-20 23:51:04 -0500951 ret = ath5k_hw_reset_tx_queue(ah, sc->bhalq); /* push to h/w */
952 if (ret)
953 goto err;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200954
Bob Copelanda951ae22010-01-20 23:51:04 -0500955 /* reconfigure cabq with ready time to 80% of beacon_interval */
956 ret = ath5k_hw_get_tx_queueprops(ah, AR5K_TX_QUEUE_ID_CAB, &qi);
957 if (ret)
958 goto err;
959
960 qi.tqi_ready_time = (sc->bintval * 80) / 100;
961 ret = ath5k_hw_set_tx_queueprops(ah, AR5K_TX_QUEUE_ID_CAB, &qi);
962 if (ret)
963 goto err;
964
965 ret = ath5k_hw_reset_tx_queue(ah, AR5K_TX_QUEUE_ID_CAB);
966err:
967 return ret;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200968}
969
970static void
971ath5k_txq_drainq(struct ath5k_softc *sc, struct ath5k_txq *txq)
972{
973 struct ath5k_buf *bf, *bf0;
974
975 /*
976 * NB: this assumes output has been stopped and
977 * we do not need to block ath5k_tx_tasklet
978 */
979 spin_lock_bh(&txq->lock);
980 list_for_each_entry_safe(bf, bf0, &txq->q, list) {
Bruno Randolfb47f4072008-03-05 18:35:45 +0900981 ath5k_debug_printtxbuf(sc, bf);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200982
Bruno Randolf9e4e43f2010-06-16 19:11:17 +0900983 ath5k_txbuf_free_skb(sc, bf);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200984
985 spin_lock_bh(&sc->txbuflock);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200986 list_move_tail(&bf->list, &sc->txbuf);
987 sc->txbuf_len++;
Bruno Randolf925e0b02010-09-17 11:36:35 +0900988 txq->txq_len--;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200989 spin_unlock_bh(&sc->txbuflock);
990 }
991 txq->link = NULL;
992 spin_unlock_bh(&txq->lock);
993}
994
995/*
996 * Drain the transmit queues and reclaim resources.
997 */
998static void
999ath5k_txq_cleanup(struct ath5k_softc *sc)
1000{
1001 struct ath5k_hw *ah = sc->ah;
1002 unsigned int i;
1003
1004 /* XXX return value */
1005 if (likely(!test_bit(ATH_STAT_INVALID, sc->status))) {
1006 /* don't touch the hardware if marked invalid */
1007 ath5k_hw_stop_tx_dma(ah, sc->bhalq);
1008 ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "beacon queue %x\n",
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001009 ath5k_hw_get_txdp(ah, sc->bhalq));
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001010 for (i = 0; i < ARRAY_SIZE(sc->txqs); i++)
1011 if (sc->txqs[i].setup) {
1012 ath5k_hw_stop_tx_dma(ah, sc->txqs[i].qnum);
1013 ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "txq [%u] %x, "
1014 "link %p\n",
1015 sc->txqs[i].qnum,
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001016 ath5k_hw_get_txdp(ah,
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001017 sc->txqs[i].qnum),
1018 sc->txqs[i].link);
1019 }
1020 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001021
1022 for (i = 0; i < ARRAY_SIZE(sc->txqs); i++)
1023 if (sc->txqs[i].setup)
1024 ath5k_txq_drainq(sc, &sc->txqs[i]);
1025}
1026
1027static void
1028ath5k_txq_release(struct ath5k_softc *sc)
1029{
1030 struct ath5k_txq *txq = sc->txqs;
1031 unsigned int i;
1032
1033 for (i = 0; i < ARRAY_SIZE(sc->txqs); i++, txq++)
1034 if (txq->setup) {
1035 ath5k_hw_release_tx_queue(sc->ah, txq->qnum);
1036 txq->setup = false;
1037 }
1038}
1039
1040
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001041/*************\
1042* RX Handling *
1043\*************/
1044
1045/*
1046 * Enable the receive h/w following a reset.
1047 */
1048static int
1049ath5k_rx_start(struct ath5k_softc *sc)
1050{
1051 struct ath5k_hw *ah = sc->ah;
Luis R. Rodriguezdb719712009-09-10 11:20:57 -07001052 struct ath_common *common = ath5k_hw_common(ah);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001053 struct ath5k_buf *bf;
1054 int ret;
1055
Nick Kossifidisb6127982010-08-15 13:03:11 -04001056 common->rx_bufsize = roundup(IEEE80211_MAX_FRAME_LEN, common->cachelsz);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001057
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -08001058 ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "cachelsz %u rx_bufsize %u\n",
1059 common->cachelsz, common->rx_bufsize);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001060
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001061 spin_lock_bh(&sc->rxbuflock);
Bob Copeland26925042009-04-15 07:57:36 -04001062 sc->rxlink = NULL;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001063 list_for_each_entry(bf, &sc->rxbuf, list) {
1064 ret = ath5k_rxbuf_setup(sc, bf);
1065 if (ret != 0) {
1066 spin_unlock_bh(&sc->rxbuflock);
1067 goto err;
1068 }
1069 }
1070 bf = list_first_entry(&sc->rxbuf, struct ath5k_buf, list);
Bob Copeland26925042009-04-15 07:57:36 -04001071 ath5k_hw_set_rxdp(ah, bf->daddr);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001072 spin_unlock_bh(&sc->rxbuflock);
1073
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001074 ath5k_hw_start_rx_dma(ah); /* enable recv descriptors */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001075 ath5k_mode_setup(sc); /* set filters, etc. */
1076 ath5k_hw_start_rx_pcu(ah); /* re-enable PCU/DMA engine */
1077
1078 return 0;
1079err:
1080 return ret;
1081}
1082
1083/*
1084 * Disable the receive h/w in preparation for a reset.
1085 */
1086static void
1087ath5k_rx_stop(struct ath5k_softc *sc)
1088{
1089 struct ath5k_hw *ah = sc->ah;
1090
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001091 ath5k_hw_stop_rx_pcu(ah); /* disable PCU */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001092 ath5k_hw_set_rx_filter(ah, 0); /* clear recv filter */
1093 ath5k_hw_stop_rx_dma(ah); /* disable DMA engine */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001094
1095 ath5k_debug_printrxbuffs(sc, ah);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001096}
1097
1098static unsigned int
Bruno Randolf8a89f062010-06-16 19:11:51 +09001099ath5k_rx_decrypted(struct ath5k_softc *sc, struct sk_buff *skb,
1100 struct ath5k_rx_status *rs)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001101{
Luis R. Rodriguezdc1e0012009-11-04 17:47:31 -08001102 struct ath5k_hw *ah = sc->ah;
1103 struct ath_common *common = ath5k_hw_common(ah);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001104 struct ieee80211_hdr *hdr = (void *)skb->data;
Harvey Harrison798ee982008-07-15 18:44:02 -07001105 unsigned int keyix, hlen;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001106
Bruno Randolfb47f4072008-03-05 18:35:45 +09001107 if (!(rs->rs_status & AR5K_RXERR_DECRYPT) &&
1108 rs->rs_keyix != AR5K_RXKEYIX_INVALID)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001109 return RX_FLAG_DECRYPTED;
1110
1111 /* Apparently when a default key is used to decrypt the packet
1112 the hw does not set the index used to decrypt. In such cases
1113 get the index from the packet. */
Harvey Harrison798ee982008-07-15 18:44:02 -07001114 hlen = ieee80211_hdrlen(hdr->frame_control);
Harvey Harrison24b56e72008-06-14 23:33:38 -07001115 if (ieee80211_has_protected(hdr->frame_control) &&
1116 !(rs->rs_status & AR5K_RXERR_DECRYPT) &&
1117 skb->len >= hlen + 4) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001118 keyix = skb->data[hlen + 3] >> 6;
1119
Luis R. Rodriguezdc1e0012009-11-04 17:47:31 -08001120 if (test_bit(keyix, common->keymap))
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001121 return RX_FLAG_DECRYPTED;
1122 }
1123
1124 return 0;
1125}
1126
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001127
1128static void
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001129ath5k_check_ibss_tsf(struct ath5k_softc *sc, struct sk_buff *skb,
1130 struct ieee80211_rx_status *rxs)
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001131{
Luis R. Rodriguez954fece2009-09-10 10:51:33 -07001132 struct ath_common *common = ath5k_hw_common(sc->ah);
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001133 u64 tsf, bc_tstamp;
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001134 u32 hw_tu;
1135 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)skb->data;
1136
Harvey Harrison24b56e72008-06-14 23:33:38 -07001137 if (ieee80211_is_beacon(mgmt->frame_control) &&
Pavel Roskin38c07b42008-02-26 17:59:14 -05001138 le16_to_cpu(mgmt->u.beacon.capab_info) & WLAN_CAPABILITY_IBSS &&
Luis R. Rodriguez954fece2009-09-10 10:51:33 -07001139 memcmp(mgmt->bssid, common->curbssid, ETH_ALEN) == 0) {
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001140 /*
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001141 * Received an IBSS beacon with the same BSSID. Hardware *must*
1142 * have updated the local TSF. We have to work around various
1143 * hardware bugs, though...
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001144 */
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001145 tsf = ath5k_hw_get_tsf64(sc->ah);
1146 bc_tstamp = le64_to_cpu(mgmt->u.beacon.timestamp);
1147 hw_tu = TSF_TO_TU(tsf);
1148
1149 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
1150 "beacon %llx mactime %llx (diff %lld) tsf now %llx\n",
John W. Linville06501d22008-04-01 17:38:47 -04001151 (unsigned long long)bc_tstamp,
1152 (unsigned long long)rxs->mactime,
1153 (unsigned long long)(rxs->mactime - bc_tstamp),
1154 (unsigned long long)tsf);
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001155
1156 /*
1157 * Sometimes the HW will give us a wrong tstamp in the rx
1158 * status, causing the timestamp extension to go wrong.
1159 * (This seems to happen especially with beacon frames bigger
1160 * than 78 byte (incl. FCS))
1161 * But we know that the receive timestamp must be later than the
1162 * timestamp of the beacon since HW must have synced to that.
1163 *
1164 * NOTE: here we assume mactime to be after the frame was
1165 * received, not like mac80211 which defines it at the start.
1166 */
1167 if (bc_tstamp > rxs->mactime) {
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001168 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001169 "fixing mactime from %llx to %llx\n",
John W. Linville06501d22008-04-01 17:38:47 -04001170 (unsigned long long)rxs->mactime,
1171 (unsigned long long)tsf);
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001172 rxs->mactime = tsf;
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001173 }
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001174
1175 /*
1176 * Local TSF might have moved higher than our beacon timers,
1177 * in that case we have to update them to continue sending
1178 * beacons. This also takes care of synchronizing beacon sending
1179 * times with other stations.
1180 */
1181 if (hw_tu >= sc->nexttbtt)
1182 ath5k_beacon_update_timers(sc, bc_tstamp);
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001183 }
1184}
1185
Bruno Randolfb4ea4492010-03-25 14:49:25 +09001186static void
1187ath5k_update_beacon_rssi(struct ath5k_softc *sc, struct sk_buff *skb, int rssi)
1188{
1189 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)skb->data;
1190 struct ath5k_hw *ah = sc->ah;
1191 struct ath_common *common = ath5k_hw_common(ah);
1192
1193 /* only beacons from our BSSID */
1194 if (!ieee80211_is_beacon(mgmt->frame_control) ||
1195 memcmp(mgmt->bssid, common->curbssid, ETH_ALEN) != 0)
1196 return;
1197
1198 ah->ah_beacon_rssi_avg = ath5k_moving_average(ah->ah_beacon_rssi_avg,
1199 rssi);
1200
1201 /* in IBSS mode we should keep RSSI statistics per neighbour */
1202 /* le16_to_cpu(mgmt->u.beacon.capab_info) & WLAN_CAPABILITY_IBSS */
1203}
1204
Benoit Papillault8127fbd2010-02-27 23:05:26 +01001205/*
Bob Copelanda180a132010-08-15 13:03:12 -04001206 * Compute padding position. skb must contain an IEEE 802.11 frame
Benoit Papillault8127fbd2010-02-27 23:05:26 +01001207 */
1208static int ath5k_common_padpos(struct sk_buff *skb)
1209{
1210 struct ieee80211_hdr * hdr = (struct ieee80211_hdr *)skb->data;
1211 __le16 frame_control = hdr->frame_control;
1212 int padpos = 24;
1213
1214 if (ieee80211_has_a4(frame_control)) {
1215 padpos += ETH_ALEN;
1216 }
1217 if (ieee80211_is_data_qos(frame_control)) {
1218 padpos += IEEE80211_QOS_CTL_LEN;
1219 }
1220
1221 return padpos;
1222}
1223
1224/*
Bob Copelanda180a132010-08-15 13:03:12 -04001225 * This function expects an 802.11 frame and returns the number of
1226 * bytes added, or -1 if we don't have enough header room.
Benoit Papillault8127fbd2010-02-27 23:05:26 +01001227 */
Benoit Papillault8127fbd2010-02-27 23:05:26 +01001228static int ath5k_add_padding(struct sk_buff *skb)
1229{
1230 int padpos = ath5k_common_padpos(skb);
1231 int padsize = padpos & 3;
1232
1233 if (padsize && skb->len>padpos) {
1234
1235 if (skb_headroom(skb) < padsize)
1236 return -1;
1237
1238 skb_push(skb, padsize);
1239 memmove(skb->data, skb->data+padsize, padpos);
1240 return padsize;
1241 }
1242
1243 return 0;
1244}
1245
1246/*
Bob Copelanda180a132010-08-15 13:03:12 -04001247 * The MAC header is padded to have 32-bit boundary if the
1248 * packet payload is non-zero. The general calculation for
1249 * padsize would take into account odd header lengths:
1250 * padsize = 4 - (hdrlen & 3); however, since only
1251 * even-length headers are used, padding can only be 0 or 2
1252 * bytes and we can optimize this a bit. We must not try to
1253 * remove padding from short control frames that do not have a
1254 * payload.
1255 *
1256 * This function expects an 802.11 frame and returns the number of
1257 * bytes removed.
Benoit Papillault8127fbd2010-02-27 23:05:26 +01001258 */
Benoit Papillault8127fbd2010-02-27 23:05:26 +01001259static int ath5k_remove_padding(struct sk_buff *skb)
1260{
1261 int padpos = ath5k_common_padpos(skb);
1262 int padsize = padpos & 3;
1263
1264 if (padsize && skb->len>=padpos+padsize) {
1265 memmove(skb->data + padsize, skb->data, padpos);
1266 skb_pull(skb, padsize);
1267 return padsize;
1268 }
1269
1270 return 0;
1271}
1272
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001273static void
Bruno Randolf8a89f062010-06-16 19:11:51 +09001274ath5k_receive_frame(struct ath5k_softc *sc, struct sk_buff *skb,
1275 struct ath5k_rx_status *rs)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001276{
Bob Copeland1c5256b2009-08-24 23:00:32 -04001277 struct ieee80211_rx_status *rxs;
Bruno Randolf8a89f062010-06-16 19:11:51 +09001278
Bruno Randolf8a89f062010-06-16 19:11:51 +09001279 ath5k_remove_padding(skb);
1280
1281 rxs = IEEE80211_SKB_RXCB(skb);
1282
1283 rxs->flag = 0;
1284 if (unlikely(rs->rs_status & AR5K_RXERR_MIC))
1285 rxs->flag |= RX_FLAG_MMIC_ERROR;
1286
1287 /*
1288 * always extend the mac timestamp, since this information is
1289 * also needed for proper IBSS merging.
1290 *
1291 * XXX: it might be too late to do it here, since rs_tstamp is
1292 * 15bit only. that means TSF extension has to be done within
1293 * 32768usec (about 32ms). it might be necessary to move this to
1294 * the interrupt handler, like it is done in madwifi.
1295 *
1296 * Unfortunately we don't know when the hardware takes the rx
1297 * timestamp (beginning of phy frame, data frame, end of rx?).
1298 * The only thing we know is that it is hardware specific...
1299 * On AR5213 it seems the rx timestamp is at the end of the
1300 * frame, but i'm not sure.
1301 *
1302 * NOTE: mac80211 defines mactime at the beginning of the first
1303 * data symbol. Since we don't have any time references it's
1304 * impossible to comply to that. This affects IBSS merge only
1305 * right now, so it's not too bad...
1306 */
1307 rxs->mactime = ath5k_extend_tsf(sc->ah, rs->rs_tstamp);
1308 rxs->flag |= RX_FLAG_TSFT;
1309
1310 rxs->freq = sc->curchan->center_freq;
1311 rxs->band = sc->curband->band;
1312
1313 rxs->signal = sc->ah->ah_noise_floor + rs->rs_rssi;
1314
1315 rxs->antenna = rs->rs_antenna;
1316
1317 if (rs->rs_antenna > 0 && rs->rs_antenna < 5)
1318 sc->stats.antenna_rx[rs->rs_antenna]++;
1319 else
1320 sc->stats.antenna_rx[0]++; /* invalid */
1321
1322 rxs->rate_idx = ath5k_hw_to_driver_rix(sc, rs->rs_rate);
1323 rxs->flag |= ath5k_rx_decrypted(sc, skb, rs);
1324
1325 if (rxs->rate_idx >= 0 && rs->rs_rate ==
1326 sc->curband->bitrates[rxs->rate_idx].hw_value_short)
1327 rxs->flag |= RX_FLAG_SHORTPRE;
1328
1329 ath5k_debug_dump_skb(sc, skb, "RX ", 0);
1330
1331 ath5k_update_beacon_rssi(sc, skb, rs->rs_rssi);
1332
1333 /* check beacons in IBSS mode */
1334 if (sc->opmode == NL80211_IFTYPE_ADHOC)
1335 ath5k_check_ibss_tsf(sc, skb, rxs);
1336
1337 ieee80211_rx(sc->hw, skb);
1338}
1339
Bruno Randolf02a78b42010-06-16 19:11:56 +09001340/** ath5k_frame_receive_ok() - Do we want to receive this frame or not?
1341 *
1342 * Check if we want to further process this frame or not. Also update
1343 * statistics. Return true if we want this frame, false if not.
1344 */
1345static bool
1346ath5k_receive_frame_ok(struct ath5k_softc *sc, struct ath5k_rx_status *rs)
1347{
1348 sc->stats.rx_all_count++;
1349
1350 if (unlikely(rs->rs_status)) {
1351 if (rs->rs_status & AR5K_RXERR_CRC)
1352 sc->stats.rxerr_crc++;
1353 if (rs->rs_status & AR5K_RXERR_FIFO)
1354 sc->stats.rxerr_fifo++;
1355 if (rs->rs_status & AR5K_RXERR_PHY) {
1356 sc->stats.rxerr_phy++;
1357 if (rs->rs_phyerr > 0 && rs->rs_phyerr < 32)
1358 sc->stats.rxerr_phy_code[rs->rs_phyerr]++;
1359 return false;
1360 }
1361 if (rs->rs_status & AR5K_RXERR_DECRYPT) {
1362 /*
1363 * Decrypt error. If the error occurred
1364 * because there was no hardware key, then
1365 * let the frame through so the upper layers
1366 * can process it. This is necessary for 5210
1367 * parts which have no way to setup a ``clear''
1368 * key cache entry.
1369 *
1370 * XXX do key cache faulting
1371 */
1372 sc->stats.rxerr_decrypt++;
1373 if (rs->rs_keyix == AR5K_RXKEYIX_INVALID &&
1374 !(rs->rs_status & AR5K_RXERR_CRC))
1375 return true;
1376 }
1377 if (rs->rs_status & AR5K_RXERR_MIC) {
1378 sc->stats.rxerr_mic++;
1379 return true;
1380 }
1381
Bob Copeland23538c22010-08-15 13:03:13 -04001382 /* reject any frames with non-crypto errors */
1383 if (rs->rs_status & ~(AR5K_RXERR_DECRYPT))
Bruno Randolf02a78b42010-06-16 19:11:56 +09001384 return false;
1385 }
1386
1387 if (unlikely(rs->rs_more)) {
1388 sc->stats.rxerr_jumbo++;
1389 return false;
1390 }
1391 return true;
1392}
1393
Bruno Randolf8a89f062010-06-16 19:11:51 +09001394static void
1395ath5k_tasklet_rx(unsigned long data)
1396{
Bruno Randolfb47f4072008-03-05 18:35:45 +09001397 struct ath5k_rx_status rs = {};
Bob Copelandb6ea0352009-01-10 14:42:54 -05001398 struct sk_buff *skb, *next_skb;
1399 dma_addr_t next_skb_addr;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001400 struct ath5k_softc *sc = (void *)data;
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -08001401 struct ath5k_hw *ah = sc->ah;
1402 struct ath_common *common = ath5k_hw_common(ah);
Bob Copelandc57ca812009-04-15 07:57:35 -04001403 struct ath5k_buf *bf;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001404 struct ath5k_desc *ds;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001405 int ret;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001406
1407 spin_lock(&sc->rxbuflock);
Jiri Slaby3a0f2c82008-07-15 17:44:18 +02001408 if (list_empty(&sc->rxbuf)) {
1409 ATH5K_WARN(sc, "empty rx buf pool\n");
1410 goto unlock;
1411 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001412 do {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001413 bf = list_first_entry(&sc->rxbuf, struct ath5k_buf, list);
1414 BUG_ON(bf->skb == NULL);
1415 skb = bf->skb;
1416 ds = bf->desc;
1417
Bob Copelandc57ca812009-04-15 07:57:35 -04001418 /* bail if HW is still using self-linked descriptor */
1419 if (ath5k_hw_get_rxdp(sc->ah) == bf->daddr)
1420 break;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001421
Bruno Randolfb47f4072008-03-05 18:35:45 +09001422 ret = sc->ah->ah_proc_rx_desc(sc->ah, ds, &rs);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001423 if (unlikely(ret == -EINPROGRESS))
1424 break;
1425 else if (unlikely(ret)) {
1426 ATH5K_ERR(sc, "error in processing rx descriptor\n");
Bruno Randolf76443952010-03-09 16:56:00 +09001427 sc->stats.rxerr_proc++;
Bruno Randolfb16062f2010-06-16 19:11:46 +09001428 break;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001429 }
1430
Bruno Randolf02a78b42010-06-16 19:11:56 +09001431 if (ath5k_receive_frame_ok(sc, &rs)) {
1432 next_skb = ath5k_rx_skb_alloc(sc, &next_skb_addr);
Bruno Randolf76443952010-03-09 16:56:00 +09001433
Bruno Randolf02a78b42010-06-16 19:11:56 +09001434 /*
1435 * If we can't replace bf->skb with a new skb under
1436 * memory pressure, just skip this packet
1437 */
1438 if (!next_skb)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001439 goto next;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001440
Bruno Randolf02a78b42010-06-16 19:11:56 +09001441 pci_unmap_single(sc->pdev, bf->skbaddr,
1442 common->rx_bufsize,
1443 PCI_DMA_FROMDEVICE);
1444
1445 skb_put(skb, rs.rs_datalen);
1446
1447 ath5k_receive_frame(sc, skb, &rs);
1448
1449 bf->skb = next_skb;
1450 bf->skbaddr = next_skb_addr;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001451 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001452next:
1453 list_move_tail(&bf->list, &sc->rxbuf);
1454 } while (ath5k_rxbuf_setup(sc, bf) == 0);
Jiri Slaby3a0f2c82008-07-15 17:44:18 +02001455unlock:
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001456 spin_unlock(&sc->rxbuflock);
1457}
1458
1459
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001460/*************\
1461* TX Handling *
1462\*************/
1463
Bob Copeland8a63fac2010-09-17 12:45:07 +09001464static int ath5k_tx_queue(struct ieee80211_hw *hw, struct sk_buff *skb,
1465 struct ath5k_txq *txq)
1466{
1467 struct ath5k_softc *sc = hw->priv;
1468 struct ath5k_buf *bf;
1469 unsigned long flags;
1470 int padsize;
1471
1472 ath5k_debug_dump_skb(sc, skb, "TX ", 1);
1473
1474 /*
1475 * The hardware expects the header padded to 4 byte boundaries.
1476 * If this is not the case, we add the padding after the header.
1477 */
1478 padsize = ath5k_add_padding(skb);
1479 if (padsize < 0) {
1480 ATH5K_ERR(sc, "tx hdrlen not %%4: not enough"
1481 " headroom to pad");
1482 goto drop_packet;
1483 }
1484
Bruno Randolf925e0b02010-09-17 11:36:35 +09001485 if (txq->txq_len >= ATH5K_TXQ_LEN_MAX)
1486 ieee80211_stop_queue(hw, txq->qnum);
1487
Bob Copeland8a63fac2010-09-17 12:45:07 +09001488 spin_lock_irqsave(&sc->txbuflock, flags);
1489 if (list_empty(&sc->txbuf)) {
1490 ATH5K_ERR(sc, "no further txbuf available, dropping packet\n");
1491 spin_unlock_irqrestore(&sc->txbuflock, flags);
Bruno Randolf651d9372010-09-17 11:36:46 +09001492 ieee80211_stop_queues(hw);
Bob Copeland8a63fac2010-09-17 12:45:07 +09001493 goto drop_packet;
1494 }
1495 bf = list_first_entry(&sc->txbuf, struct ath5k_buf, list);
1496 list_del(&bf->list);
1497 sc->txbuf_len--;
1498 if (list_empty(&sc->txbuf))
1499 ieee80211_stop_queues(hw);
1500 spin_unlock_irqrestore(&sc->txbuflock, flags);
1501
1502 bf->skb = skb;
1503
1504 if (ath5k_txbuf_setup(sc, bf, txq, padsize)) {
1505 bf->skb = NULL;
1506 spin_lock_irqsave(&sc->txbuflock, flags);
1507 list_add_tail(&bf->list, &sc->txbuf);
1508 sc->txbuf_len++;
1509 spin_unlock_irqrestore(&sc->txbuflock, flags);
1510 goto drop_packet;
1511 }
1512 return NETDEV_TX_OK;
1513
1514drop_packet:
1515 dev_kfree_skb_any(skb);
1516 return NETDEV_TX_OK;
1517}
1518
1519
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001520static void
1521ath5k_tx_processq(struct ath5k_softc *sc, struct ath5k_txq *txq)
1522{
Bruno Randolfb47f4072008-03-05 18:35:45 +09001523 struct ath5k_tx_status ts = {};
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001524 struct ath5k_buf *bf, *bf0;
1525 struct ath5k_desc *ds;
1526 struct sk_buff *skb;
Johannes Berge039fa42008-05-15 12:55:29 +02001527 struct ieee80211_tx_info *info;
Felix Fietkau2f7fe8702008-10-05 18:05:48 +02001528 int i, ret;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001529
1530 spin_lock(&txq->lock);
1531 list_for_each_entry_safe(bf, bf0, &txq->q, list) {
1532 ds = bf->desc;
1533
Bob Copelanda05988b2010-04-07 23:55:58 -04001534 /*
1535 * It's possible that the hardware can say the buffer is
1536 * completed when it hasn't yet loaded the ds_link from
1537 * host memory and moved on. If there are more TX
1538 * descriptors in the queue, wait for TXDP to change
1539 * before processing this one.
1540 */
1541 if (ath5k_hw_get_txdp(sc->ah, txq->qnum) == bf->daddr &&
1542 !list_is_last(&bf->list, &txq->q))
1543 break;
1544
Bruno Randolfb47f4072008-03-05 18:35:45 +09001545 ret = sc->ah->ah_proc_tx_desc(sc->ah, ds, &ts);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001546 if (unlikely(ret == -EINPROGRESS))
1547 break;
1548 else if (unlikely(ret)) {
1549 ATH5K_ERR(sc, "error %d while processing queue %u\n",
1550 ret, txq->qnum);
1551 break;
1552 }
1553
Bruno Randolf76443952010-03-09 16:56:00 +09001554 sc->stats.tx_all_count++;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001555 skb = bf->skb;
Johannes Berga888d522008-05-26 16:43:39 +02001556 info = IEEE80211_SKB_CB(skb);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001557 bf->skb = NULL;
Johannes Berge039fa42008-05-15 12:55:29 +02001558
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001559 pci_unmap_single(sc->pdev, bf->skbaddr, skb->len,
1560 PCI_DMA_TODEVICE);
1561
Johannes Berge6a98542008-10-21 12:40:02 +02001562 ieee80211_tx_info_clear_status(info);
Felix Fietkau2f7fe8702008-10-05 18:05:48 +02001563 for (i = 0; i < 4; i++) {
Johannes Berge6a98542008-10-21 12:40:02 +02001564 struct ieee80211_tx_rate *r =
1565 &info->status.rates[i];
Felix Fietkau2f7fe8702008-10-05 18:05:48 +02001566
1567 if (ts.ts_rate[i]) {
Johannes Berge6a98542008-10-21 12:40:02 +02001568 r->idx = ath5k_hw_to_driver_rix(sc, ts.ts_rate[i]);
1569 r->count = ts.ts_retry[i];
Felix Fietkau2f7fe8702008-10-05 18:05:48 +02001570 } else {
Johannes Berge6a98542008-10-21 12:40:02 +02001571 r->idx = -1;
1572 r->count = 0;
Felix Fietkau2f7fe8702008-10-05 18:05:48 +02001573 }
1574 }
1575
Johannes Berge6a98542008-10-21 12:40:02 +02001576 /* count the successful attempt as well */
1577 info->status.rates[ts.ts_final_idx].count++;
1578
Bruno Randolfb47f4072008-03-05 18:35:45 +09001579 if (unlikely(ts.ts_status)) {
Bruno Randolf495391d2010-03-25 14:49:36 +09001580 sc->stats.ack_fail++;
Bruno Randolf76443952010-03-09 16:56:00 +09001581 if (ts.ts_status & AR5K_TXERR_FILT) {
Johannes Berge039fa42008-05-15 12:55:29 +02001582 info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
Bruno Randolf76443952010-03-09 16:56:00 +09001583 sc->stats.txerr_filt++;
1584 }
1585 if (ts.ts_status & AR5K_TXERR_XRETRY)
1586 sc->stats.txerr_retry++;
1587 if (ts.ts_status & AR5K_TXERR_FIFO)
1588 sc->stats.txerr_fifo++;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001589 } else {
Johannes Berge039fa42008-05-15 12:55:29 +02001590 info->flags |= IEEE80211_TX_STAT_ACK;
1591 info->status.ack_signal = ts.ts_rssi;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001592 }
1593
Benoit Papillault8127fbd2010-02-27 23:05:26 +01001594 /*
1595 * Remove MAC header padding before giving the frame
1596 * back to mac80211.
1597 */
1598 ath5k_remove_padding(skb);
1599
Bruno Randolf604eead2010-03-09 16:55:17 +09001600 if (ts.ts_antenna > 0 && ts.ts_antenna < 5)
1601 sc->stats.antenna_tx[ts.ts_antenna]++;
1602 else
1603 sc->stats.antenna_tx[0]++; /* invalid */
1604
Johannes Berge039fa42008-05-15 12:55:29 +02001605 ieee80211_tx_status(sc->hw, skb);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001606
1607 spin_lock(&sc->txbuflock);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001608 list_move_tail(&bf->list, &sc->txbuf);
1609 sc->txbuf_len++;
Bruno Randolf925e0b02010-09-17 11:36:35 +09001610 txq->txq_len--;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001611 spin_unlock(&sc->txbuflock);
1612 }
1613 if (likely(list_empty(&txq->q)))
1614 txq->link = NULL;
1615 spin_unlock(&txq->lock);
Bruno Randolf925e0b02010-09-17 11:36:35 +09001616 if (txq->txq_len < ATH5K_TXQ_LEN_LOW)
1617 ieee80211_wake_queue(sc->hw, txq->qnum);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001618}
1619
1620static void
1621ath5k_tasklet_tx(unsigned long data)
1622{
Bob Copeland8784d2e2009-07-29 17:32:28 -04001623 int i;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001624 struct ath5k_softc *sc = (void *)data;
1625
Bob Copeland8784d2e2009-07-29 17:32:28 -04001626 for (i=0; i < AR5K_NUM_TX_QUEUES; i++)
1627 if (sc->txqs[i].setup && (sc->ah->ah_txq_isr & BIT(i)))
1628 ath5k_tx_processq(sc, &sc->txqs[i]);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001629}
1630
1631
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001632/*****************\
1633* Beacon handling *
1634\*****************/
1635
1636/*
1637 * Setup the beacon frame for transmit.
1638 */
1639static int
Johannes Berge039fa42008-05-15 12:55:29 +02001640ath5k_beacon_setup(struct ath5k_softc *sc, struct ath5k_buf *bf)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001641{
1642 struct sk_buff *skb = bf->skb;
Johannes Berga888d522008-05-26 16:43:39 +02001643 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001644 struct ath5k_hw *ah = sc->ah;
1645 struct ath5k_desc *ds;
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04001646 int ret = 0;
1647 u8 antenna;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001648 u32 flags;
Benoit Papillault8127fbd2010-02-27 23:05:26 +01001649 const int padsize = 0;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001650
1651 bf->skbaddr = pci_map_single(sc->pdev, skb->data, skb->len,
1652 PCI_DMA_TODEVICE);
1653 ATH5K_DBG(sc, ATH5K_DEBUG_BEACON, "skb %p [data %p len %u] "
1654 "skbaddr %llx\n", skb, skb->data, skb->len,
1655 (unsigned long long)bf->skbaddr);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -07001656 if (pci_dma_mapping_error(sc->pdev, bf->skbaddr)) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001657 ATH5K_ERR(sc, "beacon DMA mapping failed\n");
1658 return -EIO;
1659 }
1660
1661 ds = bf->desc;
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04001662 antenna = ah->ah_tx_ant;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001663
1664 flags = AR5K_TXDESC_NOACK;
Johannes Berg05c914f2008-09-11 00:01:58 +02001665 if (sc->opmode == NL80211_IFTYPE_ADHOC && ath5k_hw_hasveol(ah)) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001666 ds->ds_link = bf->daddr; /* self-linked */
1667 flags |= AR5K_TXDESC_VEOL;
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04001668 } else
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001669 ds->ds_link = 0;
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04001670
1671 /*
1672 * If we use multiple antennas on AP and use
1673 * the Sectored AP scenario, switch antenna every
1674 * 4 beacons to make sure everybody hears our AP.
1675 * When a client tries to associate, hw will keep
1676 * track of the tx antenna to be used for this client
1677 * automaticaly, based on ACKed packets.
1678 *
1679 * Note: AP still listens and transmits RTS on the
1680 * default antenna which is supposed to be an omni.
1681 *
1682 * Note2: On sectored scenarios it's possible to have
Bob Copelanda180a132010-08-15 13:03:12 -04001683 * multiple antennas (1 omni -- the default -- and 14
1684 * sectors), so if we choose to actually support this
1685 * mode, we need to allow the user to set how many antennas
1686 * we have and tweak the code below to send beacons
1687 * on all of them.
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04001688 */
1689 if (ah->ah_ant_mode == AR5K_ANTMODE_SECTOR_AP)
1690 antenna = sc->bsent & 4 ? 2 : 1;
1691
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001692
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02001693 /* FIXME: If we are in g mode and rate is a CCK rate
1694 * subtract ah->ah_txpower.txp_cck_ofdm_pwr_delta
1695 * from tx power (value is in dB units already) */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001696 ds->ds_data = bf->skbaddr;
Bruno Randolf281c56d2008-02-05 18:44:55 +09001697 ret = ah->ah_setup_tx_desc(ah, ds, skb->len,
Benoit Papillault8127fbd2010-02-27 23:05:26 +01001698 ieee80211_get_hdrlen_from_skb(skb), padsize,
Luis R. Rodriguez400ec452008-02-03 21:51:49 -05001699 AR5K_PKT_TYPE_BEACON, (sc->power_level * 2),
Johannes Berge039fa42008-05-15 12:55:29 +02001700 ieee80211_get_tx_rate(sc->hw, info)->hw_value,
Johannes Berg2e92e6f2008-05-15 12:55:27 +02001701 1, AR5K_TXKEYIX_INVALID,
Luis R. Rodriguez400ec452008-02-03 21:51:49 -05001702 antenna, flags, 0, 0);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001703 if (ret)
1704 goto err_unmap;
1705
1706 return 0;
1707err_unmap:
1708 pci_unmap_single(sc->pdev, bf->skbaddr, skb->len, PCI_DMA_TODEVICE);
1709 return ret;
1710}
1711
1712/*
Bob Copeland8a63fac2010-09-17 12:45:07 +09001713 * Updates the beacon that is sent by ath5k_beacon_send. For adhoc,
1714 * this is called only once at config_bss time, for AP we do it every
1715 * SWBA interrupt so that the TIM will reflect buffered frames.
1716 *
1717 * Called with the beacon lock.
1718 */
1719static int
1720ath5k_beacon_update(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1721{
1722 int ret;
1723 struct ath5k_softc *sc = hw->priv;
1724 struct sk_buff *skb;
1725
1726 if (WARN_ON(!vif)) {
1727 ret = -EINVAL;
1728 goto out;
1729 }
1730
1731 skb = ieee80211_beacon_get(hw, vif);
1732
1733 if (!skb) {
1734 ret = -ENOMEM;
1735 goto out;
1736 }
1737
1738 ath5k_debug_dump_skb(sc, skb, "BC ", 1);
1739
1740 ath5k_txbuf_free_skb(sc, sc->bbuf);
1741 sc->bbuf->skb = skb;
1742 ret = ath5k_beacon_setup(sc, sc->bbuf);
1743 if (ret)
1744 sc->bbuf->skb = NULL;
1745out:
1746 return ret;
1747}
1748
1749/*
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001750 * Transmit a beacon frame at SWBA. Dynamic updates to the
1751 * frame contents are done as needed and the slot time is
1752 * also adjusted based on current state.
1753 *
Bob Copeland5faaff72010-07-13 11:32:40 -04001754 * This is called from software irq context (beacontq tasklets)
1755 * or user context from ath5k_beacon_config.
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001756 */
1757static void
1758ath5k_beacon_send(struct ath5k_softc *sc)
1759{
1760 struct ath5k_buf *bf = sc->bbuf;
1761 struct ath5k_hw *ah = sc->ah;
Bob Copelandcec8db22009-07-04 12:59:51 -04001762 struct sk_buff *skb;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001763
Bruno Randolfbe9b7252008-01-23 10:27:51 +09001764 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON, "in beacon_send\n");
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001765
Bob Copeland4afd89d2010-08-15 13:03:14 -04001766 if (unlikely(bf->skb == NULL || sc->opmode == NL80211_IFTYPE_STATION)) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001767 ATH5K_WARN(sc, "bf=%p bf_skb=%p\n", bf, bf ? bf->skb : NULL);
1768 return;
1769 }
1770 /*
1771 * Check if the previous beacon has gone out. If
Bob Copelanda180a132010-08-15 13:03:12 -04001772 * not, don't don't try to post another: skip this
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001773 * period and wait for the next. Missed beacons
1774 * indicate a problem and should not occur. If we
1775 * miss too many consecutive beacons reset the device.
1776 */
1777 if (unlikely(ath5k_hw_num_tx_pending(ah, sc->bhalq) != 0)) {
1778 sc->bmisscount++;
Bruno Randolfbe9b7252008-01-23 10:27:51 +09001779 ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001780 "missed %u consecutive beacons\n", sc->bmisscount);
Nick Kossifidis428cbd42009-04-30 15:55:47 -04001781 if (sc->bmisscount > 10) { /* NB: 10 is a guess */
Bruno Randolfbe9b7252008-01-23 10:27:51 +09001782 ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001783 "stuck beacon time (%u missed)\n",
1784 sc->bmisscount);
Bruno Randolf8d67a032010-06-16 19:11:12 +09001785 ATH5K_DBG(sc, ATH5K_DEBUG_RESET,
1786 "stuck beacon, resetting\n");
Bob Copeland5faaff72010-07-13 11:32:40 -04001787 ieee80211_queue_work(sc->hw, &sc->reset_work);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001788 }
1789 return;
1790 }
1791 if (unlikely(sc->bmisscount != 0)) {
Bruno Randolfbe9b7252008-01-23 10:27:51 +09001792 ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001793 "resume beacon xmit after %u misses\n",
1794 sc->bmisscount);
1795 sc->bmisscount = 0;
1796 }
1797
1798 /*
1799 * Stop any current dma and put the new frame on the queue.
1800 * This should never fail since we check above that no frames
1801 * are still pending on the queue.
1802 */
1803 if (unlikely(ath5k_hw_stop_tx_dma(ah, sc->bhalq))) {
Nick Kossifidis428cbd42009-04-30 15:55:47 -04001804 ATH5K_WARN(sc, "beacon queue %u didn't start/stop ?\n", sc->bhalq);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001805 /* NB: hw still stops DMA, so proceed */
1806 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001807
Bob Copeland1071db82009-05-18 10:59:52 -04001808 /* refresh the beacon for AP mode */
1809 if (sc->opmode == NL80211_IFTYPE_AP)
1810 ath5k_beacon_update(sc->hw, sc->vif);
1811
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001812 ath5k_hw_set_txdp(ah, sc->bhalq, bf->daddr);
1813 ath5k_hw_start_tx_dma(ah, sc->bhalq);
Bruno Randolfbe9b7252008-01-23 10:27:51 +09001814 ATH5K_DBG(sc, ATH5K_DEBUG_BEACON, "TXDP[%u] = %llx (%p)\n",
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001815 sc->bhalq, (unsigned long long)bf->daddr, bf->desc);
1816
Bob Copelandcec8db22009-07-04 12:59:51 -04001817 skb = ieee80211_get_buffered_bc(sc->hw, sc->vif);
1818 while (skb) {
1819 ath5k_tx_queue(sc->hw, skb, sc->cabq);
1820 skb = ieee80211_get_buffered_bc(sc->hw, sc->vif);
1821 }
1822
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001823 sc->bsent++;
1824}
1825
Bruno Randolf9804b982008-01-19 18:17:59 +09001826/**
1827 * ath5k_beacon_update_timers - update beacon timers
1828 *
1829 * @sc: struct ath5k_softc pointer we are operating on
1830 * @bc_tsf: the timestamp of the beacon. 0 to reset the TSF. -1 to perform a
1831 * beacon timer update based on the current HW TSF.
1832 *
1833 * Calculate the next target beacon transmit time (TBTT) based on the timestamp
1834 * of a received beacon or the current local hardware TSF and write it to the
1835 * beacon timer registers.
1836 *
1837 * This is called in a variety of situations, e.g. when a beacon is received,
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001838 * when a TSF update has been detected, but also when an new IBSS is created or
Bruno Randolf9804b982008-01-19 18:17:59 +09001839 * when we otherwise know we have to update the timers, but we keep it in this
1840 * function to have it all together in one place.
1841 */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001842static void
Bruno Randolf9804b982008-01-19 18:17:59 +09001843ath5k_beacon_update_timers(struct ath5k_softc *sc, u64 bc_tsf)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001844{
1845 struct ath5k_hw *ah = sc->ah;
Bruno Randolf9804b982008-01-19 18:17:59 +09001846 u32 nexttbtt, intval, hw_tu, bc_tu;
1847 u64 hw_tsf;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001848
1849 intval = sc->bintval & AR5K_BEACON_PERIOD;
1850 if (WARN_ON(!intval))
1851 return;
1852
Bruno Randolf9804b982008-01-19 18:17:59 +09001853 /* beacon TSF converted to TU */
1854 bc_tu = TSF_TO_TU(bc_tsf);
1855
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001856 /* current TSF converted to TU */
Bruno Randolf9804b982008-01-19 18:17:59 +09001857 hw_tsf = ath5k_hw_get_tsf64(ah);
1858 hw_tu = TSF_TO_TU(hw_tsf);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001859
Bruno Randolf9804b982008-01-19 18:17:59 +09001860#define FUDGE 3
1861 /* we use FUDGE to make sure the next TBTT is ahead of the current TU */
1862 if (bc_tsf == -1) {
1863 /*
1864 * no beacons received, called internally.
1865 * just need to refresh timers based on HW TSF.
1866 */
1867 nexttbtt = roundup(hw_tu + FUDGE, intval);
1868 } else if (bc_tsf == 0) {
1869 /*
1870 * no beacon received, probably called by ath5k_reset_tsf().
1871 * reset TSF to start with 0.
1872 */
1873 nexttbtt = intval;
1874 intval |= AR5K_BEACON_RESET_TSF;
1875 } else if (bc_tsf > hw_tsf) {
1876 /*
1877 * beacon received, SW merge happend but HW TSF not yet updated.
1878 * not possible to reconfigure timers yet, but next time we
1879 * receive a beacon with the same BSSID, the hardware will
1880 * automatically update the TSF and then we need to reconfigure
1881 * the timers.
1882 */
1883 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
1884 "need to wait for HW TSF sync\n");
1885 return;
1886 } else {
1887 /*
1888 * most important case for beacon synchronization between STA.
1889 *
1890 * beacon received and HW TSF has been already updated by HW.
1891 * update next TBTT based on the TSF of the beacon, but make
1892 * sure it is ahead of our local TSF timer.
1893 */
1894 nexttbtt = bc_tu + roundup(hw_tu + FUDGE - bc_tu, intval);
1895 }
1896#undef FUDGE
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001897
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001898 sc->nexttbtt = nexttbtt;
1899
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001900 intval |= AR5K_BEACON_ENA;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001901 ath5k_hw_init_beacon(ah, nexttbtt, intval);
Bruno Randolf9804b982008-01-19 18:17:59 +09001902
1903 /*
1904 * debugging output last in order to preserve the time critical aspect
1905 * of this function
1906 */
1907 if (bc_tsf == -1)
1908 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
1909 "reconfigured timers based on HW TSF\n");
1910 else if (bc_tsf == 0)
1911 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
1912 "reset HW TSF and timers\n");
1913 else
1914 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
1915 "updated timers based on beacon TSF\n");
1916
1917 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
David Miller04f93a82008-02-15 16:08:59 -08001918 "bc_tsf %llx hw_tsf %llx bc_tu %u hw_tu %u nexttbtt %u\n",
1919 (unsigned long long) bc_tsf,
1920 (unsigned long long) hw_tsf, bc_tu, hw_tu, nexttbtt);
Bruno Randolf9804b982008-01-19 18:17:59 +09001921 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON, "intval %u %s %s\n",
1922 intval & AR5K_BEACON_PERIOD,
1923 intval & AR5K_BEACON_ENA ? "AR5K_BEACON_ENA" : "",
1924 intval & AR5K_BEACON_RESET_TSF ? "AR5K_BEACON_RESET_TSF" : "");
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001925}
1926
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001927/**
1928 * ath5k_beacon_config - Configure the beacon queues and interrupts
1929 *
1930 * @sc: struct ath5k_softc pointer we are operating on
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001931 *
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001932 * In IBSS mode we use a self-linked tx descriptor if possible. We enable SWBA
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001933 * interrupts to detect TSF updates only.
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001934 */
1935static void
1936ath5k_beacon_config(struct ath5k_softc *sc)
1937{
1938 struct ath5k_hw *ah = sc->ah;
Bob Copelandb5f03952009-02-15 12:06:10 -05001939 unsigned long flags;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001940
Bob Copeland21800492009-07-04 12:59:52 -04001941 spin_lock_irqsave(&sc->block, flags);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001942 sc->bmisscount = 0;
Jiri Slabydc1968e2008-07-23 13:17:34 +02001943 sc->imask &= ~(AR5K_INT_BMISS | AR5K_INT_SWBA);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001944
Bob Copeland21800492009-07-04 12:59:52 -04001945 if (sc->enable_beacon) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001946 /*
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001947 * In IBSS mode we use a self-linked tx descriptor and let the
1948 * hardware send the beacons automatically. We have to load it
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001949 * only once here.
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001950 * We use the SWBA interrupt only to keep track of the beacon
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001951 * timers in order to detect automatic TSF updates.
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001952 */
1953 ath5k_beaconq_config(sc);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001954
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001955 sc->imask |= AR5K_INT_SWBA;
1956
Jiri Slabyda966bc2008-10-12 22:54:10 +02001957 if (sc->opmode == NL80211_IFTYPE_ADHOC) {
Bob Copeland21800492009-07-04 12:59:52 -04001958 if (ath5k_hw_hasveol(ah))
Jiri Slabyda966bc2008-10-12 22:54:10 +02001959 ath5k_beacon_send(sc);
Jiri Slabyda966bc2008-10-12 22:54:10 +02001960 } else
1961 ath5k_beacon_update_timers(sc, -1);
Bob Copeland21800492009-07-04 12:59:52 -04001962 } else {
1963 ath5k_hw_stop_tx_dma(sc->ah, sc->bhalq);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001964 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001965
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001966 ath5k_hw_set_imr(ah, sc->imask);
Bob Copeland21800492009-07-04 12:59:52 -04001967 mmiowb();
1968 spin_unlock_irqrestore(&sc->block, flags);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001969}
1970
Nick Kossifidis428cbd42009-04-30 15:55:47 -04001971static void ath5k_tasklet_beacon(unsigned long data)
1972{
1973 struct ath5k_softc *sc = (struct ath5k_softc *) data;
1974
1975 /*
1976 * Software beacon alert--time to send a beacon.
1977 *
1978 * In IBSS mode we use this interrupt just to
1979 * keep track of the next TBTT (target beacon
1980 * transmission time) in order to detect wether
1981 * automatic TSF updates happened.
1982 */
1983 if (sc->opmode == NL80211_IFTYPE_ADHOC) {
1984 /* XXX: only if VEOL suppported */
1985 u64 tsf = ath5k_hw_get_tsf64(sc->ah);
1986 sc->nexttbtt += sc->bintval;
1987 ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
1988 "SWBA nexttbtt: %x hw_tu: %x "
1989 "TSF: %llx\n",
1990 sc->nexttbtt,
1991 TSF_TO_TU(tsf),
1992 (unsigned long long) tsf);
1993 } else {
1994 spin_lock(&sc->block);
1995 ath5k_beacon_send(sc);
1996 spin_unlock(&sc->block);
1997 }
1998}
1999
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002000
2001/********************\
2002* Interrupt handling *
2003\********************/
2004
Bruno Randolf6a8a3f62010-03-25 14:49:19 +09002005static void
2006ath5k_intr_calibration_poll(struct ath5k_hw *ah)
2007{
Bruno Randolf2111ac02010-04-02 18:44:08 +09002008 if (time_is_before_eq_jiffies(ah->ah_cal_next_ani) &&
2009 !(ah->ah_cal_mask & AR5K_CALIBRATION_FULL)) {
2010 /* run ANI only when full calibration is not active */
2011 ah->ah_cal_next_ani = jiffies +
2012 msecs_to_jiffies(ATH5K_TUNE_CALIBRATION_INTERVAL_ANI);
2013 tasklet_schedule(&ah->ah_sc->ani_tasklet);
2014
2015 } else if (time_is_before_eq_jiffies(ah->ah_cal_next_full)) {
Bruno Randolf6a8a3f62010-03-25 14:49:19 +09002016 ah->ah_cal_next_full = jiffies +
2017 msecs_to_jiffies(ATH5K_TUNE_CALIBRATION_INTERVAL_FULL);
2018 tasklet_schedule(&ah->ah_sc->calib);
2019 }
2020 /* we could use SWI to generate enough interrupts to meet our
2021 * calibration interval requirements, if necessary:
2022 * AR5K_REG_ENABLE_BITS(ah, AR5K_CR, AR5K_CR_SWI); */
2023}
2024
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002025static irqreturn_t
2026ath5k_intr(int irq, void *dev_id)
2027{
2028 struct ath5k_softc *sc = dev_id;
2029 struct ath5k_hw *ah = sc->ah;
2030 enum ath5k_int status;
2031 unsigned int counter = 1000;
2032
2033 if (unlikely(test_bit(ATH_STAT_INVALID, sc->status) ||
2034 !ath5k_hw_is_intr_pending(ah)))
2035 return IRQ_NONE;
2036
2037 do {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002038 ath5k_hw_get_isr(ah, &status); /* NB: clears IRQ too */
2039 ATH5K_DBG(sc, ATH5K_DEBUG_INTR, "status 0x%x/0x%x\n",
2040 status, sc->imask);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002041 if (unlikely(status & AR5K_INT_FATAL)) {
2042 /*
2043 * Fatal errors are unrecoverable.
2044 * Typically these are caused by DMA errors.
2045 */
Bruno Randolf8d67a032010-06-16 19:11:12 +09002046 ATH5K_DBG(sc, ATH5K_DEBUG_RESET,
2047 "fatal int, resetting\n");
Bob Copeland5faaff72010-07-13 11:32:40 -04002048 ieee80211_queue_work(sc->hw, &sc->reset_work);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002049 } else if (unlikely(status & AR5K_INT_RXORN)) {
Bruno Randolf87d77c42010-04-12 16:38:52 +09002050 /*
2051 * Receive buffers are full. Either the bus is busy or
2052 * the CPU is not fast enough to process all received
2053 * frames.
2054 * Older chipsets need a reset to come out of this
2055 * condition, but we treat it as RX for newer chips.
2056 * We don't know exactly which versions need a reset -
2057 * this guess is copied from the HAL.
2058 */
2059 sc->stats.rxorn_intr++;
Bruno Randolf8d67a032010-06-16 19:11:12 +09002060 if (ah->ah_mac_srev < AR5K_SREV_AR5212) {
2061 ATH5K_DBG(sc, ATH5K_DEBUG_RESET,
2062 "rx overrun, resetting\n");
Bob Copeland5faaff72010-07-13 11:32:40 -04002063 ieee80211_queue_work(sc->hw, &sc->reset_work);
Bruno Randolf8d67a032010-06-16 19:11:12 +09002064 }
Bruno Randolf87d77c42010-04-12 16:38:52 +09002065 else
2066 tasklet_schedule(&sc->rxtq);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002067 } else {
2068 if (status & AR5K_INT_SWBA) {
Bob Copeland56d2ac72009-04-15 07:57:33 -04002069 tasklet_hi_schedule(&sc->beacontq);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002070 }
2071 if (status & AR5K_INT_RXEOL) {
2072 /*
2073 * NB: the hardware should re-read the link when
2074 * RXE bit is written, but it doesn't work at
2075 * least on older hardware revs.
2076 */
Bruno Randolfb3f194e2010-07-14 10:53:29 +09002077 sc->stats.rxeol_intr++;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002078 }
2079 if (status & AR5K_INT_TXURN) {
2080 /* bump tx trigger level */
2081 ath5k_hw_update_tx_triglevel(ah, true);
2082 }
Nick Kossifidis4c674c62008-10-26 20:40:25 +02002083 if (status & (AR5K_INT_RXOK | AR5K_INT_RXERR))
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002084 tasklet_schedule(&sc->rxtq);
Nick Kossifidis4c674c62008-10-26 20:40:25 +02002085 if (status & (AR5K_INT_TXOK | AR5K_INT_TXDESC
2086 | AR5K_INT_TXERR | AR5K_INT_TXEOL))
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002087 tasklet_schedule(&sc->txtq);
2088 if (status & AR5K_INT_BMISS) {
Nick Kossifidis1e3e6e82009-02-09 06:15:42 +02002089 /* TODO */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002090 }
2091 if (status & AR5K_INT_MIB) {
Bruno Randolf2111ac02010-04-02 18:44:08 +09002092 sc->stats.mib_intr++;
Bruno Randolf495391d2010-03-25 14:49:36 +09002093 ath5k_hw_update_mib_counters(ah);
Bruno Randolf2111ac02010-04-02 18:44:08 +09002094 ath5k_ani_mib_intr(ah);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002095 }
Tobias Doerffele6a3b612009-06-09 17:33:27 +02002096 if (status & AR5K_INT_GPIO)
Tobias Doerffele6a3b612009-06-09 17:33:27 +02002097 tasklet_schedule(&sc->rf_kill.toggleq);
Bob Copelanda6ae0712009-06-09 23:43:11 -04002098
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002099 }
Bob Copeland2516baa2009-04-27 22:18:10 -04002100 } while (ath5k_hw_is_intr_pending(ah) && --counter > 0);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002101
2102 if (unlikely(!counter))
2103 ATH5K_WARN(sc, "too many interrupts, giving up for now\n");
2104
Bruno Randolf6a8a3f62010-03-25 14:49:19 +09002105 ath5k_intr_calibration_poll(ah);
Nick Kossifidis6e2206622009-08-10 03:31:31 +03002106
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002107 return IRQ_HANDLED;
2108}
2109
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002110/*
2111 * Periodically recalibrate the PHY to account
2112 * for temperature/environment changes.
2113 */
2114static void
Nick Kossifidis6e2206622009-08-10 03:31:31 +03002115ath5k_tasklet_calibrate(unsigned long data)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002116{
2117 struct ath5k_softc *sc = (void *)data;
2118 struct ath5k_hw *ah = sc->ah;
2119
Nick Kossifidis6e2206622009-08-10 03:31:31 +03002120 /* Only full calibration for now */
Bruno Randolfe65e1d72010-03-25 14:49:09 +09002121 ah->ah_cal_mask |= AR5K_CALIBRATION_FULL;
Nick Kossifidis6e2206622009-08-10 03:31:31 +03002122
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002123 ATH5K_DBG(sc, ATH5K_DEBUG_CALIBRATE, "channel %u/%x\n",
Luis R. Rodriguez400ec452008-02-03 21:51:49 -05002124 ieee80211_frequency_to_channel(sc->curchan->center_freq),
2125 sc->curchan->hw_value);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002126
Nick Kossifidis6f3b4142009-02-09 06:03:41 +02002127 if (ath5k_hw_gainf_calibrate(ah) == AR5K_RFGAIN_NEED_CHANGE) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002128 /*
2129 * Rfgain is out of bounds, reset the chip
2130 * to load new gain values.
2131 */
2132 ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "calibration, resetting\n");
Bob Copeland5faaff72010-07-13 11:32:40 -04002133 ieee80211_queue_work(sc->hw, &sc->reset_work);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002134 }
2135 if (ath5k_hw_phy_calibrate(ah, sc->curchan))
2136 ATH5K_ERR(sc, "calibration of channel %u failed\n",
Luis R. Rodriguez400ec452008-02-03 21:51:49 -05002137 ieee80211_frequency_to_channel(
2138 sc->curchan->center_freq));
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002139
Bruno Randolf0e8e02d2010-05-19 10:31:05 +09002140 /* Noise floor calibration interrupts rx/tx path while I/Q calibration
Bruno Randolf651d9372010-09-17 11:36:46 +09002141 * doesn't.
2142 * TODO: We should stop TX here, so that it doesn't interfere.
2143 * Note that stopping the queues is not enough to stop TX! */
Bruno Randolfafe86282010-05-19 10:31:10 +09002144 if (time_is_before_eq_jiffies(ah->ah_cal_next_nf)) {
2145 ah->ah_cal_next_nf = jiffies +
2146 msecs_to_jiffies(ATH5K_TUNE_CALIBRATION_INTERVAL_NF);
Bruno Randolfafe86282010-05-19 10:31:10 +09002147 ath5k_hw_update_noise_floor(ah);
Bruno Randolfafe86282010-05-19 10:31:10 +09002148 }
Nick Kossifidis6e2206622009-08-10 03:31:31 +03002149
Bruno Randolfe65e1d72010-03-25 14:49:09 +09002150 ah->ah_cal_mask &= ~AR5K_CALIBRATION_FULL;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002151}
2152
2153
Bruno Randolf2111ac02010-04-02 18:44:08 +09002154static void
2155ath5k_tasklet_ani(unsigned long data)
2156{
2157 struct ath5k_softc *sc = (void *)data;
2158 struct ath5k_hw *ah = sc->ah;
2159
2160 ah->ah_cal_mask |= AR5K_CALIBRATION_ANI;
2161 ath5k_ani_calibration(ah);
2162 ah->ah_cal_mask &= ~AR5K_CALIBRATION_ANI;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002163}
2164
2165
Bob Copeland8a63fac2010-09-17 12:45:07 +09002166/*************************\
2167* Initialization routines *
2168\*************************/
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002169
2170static int
Bob Copeland8a63fac2010-09-17 12:45:07 +09002171ath5k_stop_locked(struct ath5k_softc *sc)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002172{
Bob Copeland8a63fac2010-09-17 12:45:07 +09002173 struct ath5k_hw *ah = sc->ah;
Bob Copelandcec8db22009-07-04 12:59:51 -04002174
Bob Copeland8a63fac2010-09-17 12:45:07 +09002175 ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "invalid %u\n",
2176 test_bit(ATH_STAT_INVALID, sc->status));
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002177
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002178 /*
Bob Copeland8a63fac2010-09-17 12:45:07 +09002179 * Shutdown the hardware and driver:
2180 * stop output from above
2181 * disable interrupts
2182 * turn off timers
2183 * turn off the radio
2184 * clear transmit machinery
2185 * clear receive machinery
2186 * drain and release tx queues
2187 * reclaim beacon resources
2188 * power down hardware
2189 *
2190 * Note that some of this work is not possible if the
2191 * hardware is gone (invalid).
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002192 */
Bob Copeland8a63fac2010-09-17 12:45:07 +09002193 ieee80211_stop_queues(sc->hw);
2194
2195 if (!test_bit(ATH_STAT_INVALID, sc->status)) {
2196 ath5k_led_off(sc);
2197 ath5k_hw_set_imr(ah, 0);
2198 synchronize_irq(sc->pdev->irq);
2199 }
2200 ath5k_txq_cleanup(sc);
2201 if (!test_bit(ATH_STAT_INVALID, sc->status)) {
2202 ath5k_rx_stop(sc);
2203 ath5k_hw_phy_disable(ah);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002204 }
2205
Bob Copeland8a63fac2010-09-17 12:45:07 +09002206 return 0;
2207}
2208
2209static int
2210ath5k_init(struct ath5k_softc *sc)
2211{
2212 struct ath5k_hw *ah = sc->ah;
2213 struct ath_common *common = ath5k_hw_common(ah);
2214 int ret, i;
2215
2216 mutex_lock(&sc->lock);
2217
2218 ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "mode %d\n", sc->opmode);
2219
2220 /*
2221 * Stop anything previously setup. This is safe
2222 * no matter this is the first time through or not.
2223 */
2224 ath5k_stop_locked(sc);
2225
2226 /*
2227 * The basic interface to setting the hardware in a good
2228 * state is ``reset''. On return the hardware is known to
2229 * be powered up and with interrupts disabled. This must
2230 * be followed by initialization of the appropriate bits
2231 * and then setup of the interrupt mask.
2232 */
2233 sc->curchan = sc->hw->conf.channel;
2234 sc->curband = &sc->sbands[sc->curchan->band];
2235 sc->imask = AR5K_INT_RXOK | AR5K_INT_RXERR | AR5K_INT_RXEOL |
2236 AR5K_INT_RXORN | AR5K_INT_TXDESC | AR5K_INT_TXEOL |
2237 AR5K_INT_FATAL | AR5K_INT_GLOBAL | AR5K_INT_MIB;
2238
2239 ret = ath5k_reset(sc, NULL);
2240 if (ret)
2241 goto done;
2242
2243 ath5k_rfkill_hw_start(ah);
2244
2245 /*
2246 * Reset the key cache since some parts do not reset the
2247 * contents on initial power up or resume from suspend.
2248 */
2249 for (i = 0; i < common->keymax; i++)
2250 ath_hw_keyreset(common, (u16) i);
2251
2252 ath5k_hw_set_ack_bitrate_high(ah, true);
2253 ret = 0;
2254done:
2255 mmiowb();
2256 mutex_unlock(&sc->lock);
2257 return ret;
2258}
2259
2260static void stop_tasklets(struct ath5k_softc *sc)
2261{
2262 tasklet_kill(&sc->rxtq);
2263 tasklet_kill(&sc->txtq);
2264 tasklet_kill(&sc->calib);
2265 tasklet_kill(&sc->beacontq);
2266 tasklet_kill(&sc->ani_tasklet);
2267}
2268
2269/*
2270 * Stop the device, grabbing the top-level lock to protect
2271 * against concurrent entry through ath5k_init (which can happen
2272 * if another thread does a system call and the thread doing the
2273 * stop is preempted).
2274 */
2275static int
2276ath5k_stop_hw(struct ath5k_softc *sc)
2277{
2278 int ret;
2279
2280 mutex_lock(&sc->lock);
2281 ret = ath5k_stop_locked(sc);
2282 if (ret == 0 && !test_bit(ATH_STAT_INVALID, sc->status)) {
2283 /*
2284 * Don't set the card in full sleep mode!
2285 *
2286 * a) When the device is in this state it must be carefully
2287 * woken up or references to registers in the PCI clock
2288 * domain may freeze the bus (and system). This varies
2289 * by chip and is mostly an issue with newer parts
2290 * (madwifi sources mentioned srev >= 0x78) that go to
2291 * sleep more quickly.
2292 *
2293 * b) On older chips full sleep results a weird behaviour
2294 * during wakeup. I tested various cards with srev < 0x78
2295 * and they don't wake up after module reload, a second
2296 * module reload is needed to bring the card up again.
2297 *
2298 * Until we figure out what's going on don't enable
2299 * full chip reset on any chip (this is what Legacy HAL
2300 * and Sam's HAL do anyway). Instead Perform a full reset
2301 * on the device (same as initial state after attach) and
2302 * leave it idle (keep MAC/BB on warm reset) */
2303 ret = ath5k_hw_on_hold(sc->ah);
2304
2305 ATH5K_DBG(sc, ATH5K_DEBUG_RESET,
2306 "putting device to sleep\n");
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002307 }
Bob Copeland8a63fac2010-09-17 12:45:07 +09002308 ath5k_txbuf_free_skb(sc, sc->bbuf);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002309
Bob Copeland8a63fac2010-09-17 12:45:07 +09002310 mmiowb();
2311 mutex_unlock(&sc->lock);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002312
Bob Copeland8a63fac2010-09-17 12:45:07 +09002313 stop_tasklets(sc);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002314
Bob Copeland8a63fac2010-09-17 12:45:07 +09002315 ath5k_rfkill_hw_stop(sc->ah);
2316
2317 return ret;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002318}
2319
Bob Copeland209d8892009-05-07 08:09:08 -04002320/*
2321 * Reset the hardware. If chan is not NULL, then also pause rx/tx
2322 * and change to the given channel.
Bob Copeland5faaff72010-07-13 11:32:40 -04002323 *
2324 * This should be called with sc->lock.
Bob Copeland209d8892009-05-07 08:09:08 -04002325 */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002326static int
Bob Copeland209d8892009-05-07 08:09:08 -04002327ath5k_reset(struct ath5k_softc *sc, struct ieee80211_channel *chan)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002328{
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002329 struct ath5k_hw *ah = sc->ah;
2330 int ret;
2331
2332 ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "resetting\n");
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002333
Bob Copeland450464d2010-07-13 11:32:41 -04002334 ath5k_hw_set_imr(ah, 0);
2335 synchronize_irq(sc->pdev->irq);
2336 stop_tasklets(sc);
2337
Bob Copeland209d8892009-05-07 08:09:08 -04002338 if (chan) {
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002339 ath5k_txq_cleanup(sc);
2340 ath5k_rx_stop(sc);
Bob Copeland209d8892009-05-07 08:09:08 -04002341
2342 sc->curchan = chan;
2343 sc->curband = &sc->sbands[chan->band];
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002344 }
Bob Copeland33554432009-07-04 21:03:13 -04002345 ret = ath5k_hw_reset(ah, sc->opmode, sc->curchan, chan != NULL);
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002346 if (ret) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002347 ATH5K_ERR(sc, "can't reset hardware (%d)\n", ret);
2348 goto err;
2349 }
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002350
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002351 ret = ath5k_rx_start(sc);
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002352 if (ret) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002353 ATH5K_ERR(sc, "can't start recv logic\n");
2354 goto err;
2355 }
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002356
Bruno Randolf2111ac02010-04-02 18:44:08 +09002357 ath5k_ani_init(ah, ah->ah_sc->ani_state.ani_mode);
2358
Bruno Randolfac559522010-05-19 10:30:55 +09002359 ah->ah_cal_next_full = jiffies;
2360 ah->ah_cal_next_ani = jiffies;
Bruno Randolfafe86282010-05-19 10:31:10 +09002361 ah->ah_cal_next_nf = jiffies;
2362
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002363 /*
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002364 * Change channels and update the h/w rate map if we're switching;
2365 * e.g. 11a to 11b/g.
2366 *
2367 * We may be doing a reset in response to an ioctl that changes the
2368 * channel so update any state that might change as a result.
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002369 *
2370 * XXX needed?
2371 */
2372/* ath5k_chan_change(sc, c); */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002373
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002374 ath5k_beacon_config(sc);
2375 /* intrs are enabled by ath5k_beacon_config */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002376
Bruno Randolf397f3852010-05-19 10:30:49 +09002377 ieee80211_wake_queues(sc->hw);
2378
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002379 return 0;
2380err:
2381 return ret;
2382}
2383
Bob Copeland5faaff72010-07-13 11:32:40 -04002384static void ath5k_reset_work(struct work_struct *work)
2385{
2386 struct ath5k_softc *sc = container_of(work, struct ath5k_softc,
2387 reset_work);
2388
2389 mutex_lock(&sc->lock);
2390 ath5k_reset(sc, sc->curchan);
2391 mutex_unlock(&sc->lock);
2392}
2393
Bob Copeland8a63fac2010-09-17 12:45:07 +09002394static int
2395ath5k_attach(struct pci_dev *pdev, struct ieee80211_hw *hw)
2396{
2397 struct ath5k_softc *sc = hw->priv;
2398 struct ath5k_hw *ah = sc->ah;
2399 struct ath_regulatory *regulatory = ath5k_hw_regulatory(ah);
Bruno Randolf925e0b02010-09-17 11:36:35 +09002400 struct ath5k_txq *txq;
Bob Copeland8a63fac2010-09-17 12:45:07 +09002401 u8 mac[ETH_ALEN] = {};
2402 int ret;
2403
2404 ATH5K_DBG(sc, ATH5K_DEBUG_ANY, "devid 0x%x\n", pdev->device);
2405
2406 /*
2407 * Check if the MAC has multi-rate retry support.
2408 * We do this by trying to setup a fake extended
2409 * descriptor. MACs that don't have support will
2410 * return false w/o doing anything. MACs that do
2411 * support it will return true w/o doing anything.
2412 */
2413 ret = ath5k_hw_setup_mrr_tx_desc(ah, NULL, 0, 0, 0, 0, 0, 0);
2414
2415 if (ret < 0)
2416 goto err;
2417 if (ret > 0)
2418 __set_bit(ATH_STAT_MRRETRY, sc->status);
2419
2420 /*
2421 * Collect the channel list. The 802.11 layer
2422 * is resposible for filtering this list based
2423 * on settings like the phy mode and regulatory
2424 * domain restrictions.
2425 */
2426 ret = ath5k_setup_bands(hw);
2427 if (ret) {
2428 ATH5K_ERR(sc, "can't get channels\n");
2429 goto err;
2430 }
2431
2432 /* NB: setup here so ath5k_rate_update is happy */
2433 if (test_bit(AR5K_MODE_11A, ah->ah_modes))
2434 ath5k_setcurmode(sc, AR5K_MODE_11A);
2435 else
2436 ath5k_setcurmode(sc, AR5K_MODE_11B);
2437
2438 /*
2439 * Allocate tx+rx descriptors and populate the lists.
2440 */
2441 ret = ath5k_desc_alloc(sc, pdev);
2442 if (ret) {
2443 ATH5K_ERR(sc, "can't allocate descriptors\n");
2444 goto err;
2445 }
2446
2447 /*
2448 * Allocate hardware transmit queues: one queue for
2449 * beacon frames and one data queue for each QoS
2450 * priority. Note that hw functions handle resetting
2451 * these queues at the needed time.
2452 */
2453 ret = ath5k_beaconq_setup(ah);
2454 if (ret < 0) {
2455 ATH5K_ERR(sc, "can't setup a beacon xmit queue\n");
2456 goto err_desc;
2457 }
2458 sc->bhalq = ret;
2459 sc->cabq = ath5k_txq_setup(sc, AR5K_TX_QUEUE_CAB, 0);
2460 if (IS_ERR(sc->cabq)) {
2461 ATH5K_ERR(sc, "can't setup cab queue\n");
2462 ret = PTR_ERR(sc->cabq);
2463 goto err_bhal;
2464 }
2465
Bruno Randolf925e0b02010-09-17 11:36:35 +09002466 /* This order matches mac80211's queue priority, so we can
2467 * directly use the mac80211 queue number without any mapping */
2468 txq = ath5k_txq_setup(sc, AR5K_TX_QUEUE_DATA, AR5K_WME_AC_VO);
2469 if (IS_ERR(txq)) {
Bob Copeland8a63fac2010-09-17 12:45:07 +09002470 ATH5K_ERR(sc, "can't setup xmit queue\n");
Bruno Randolf925e0b02010-09-17 11:36:35 +09002471 ret = PTR_ERR(txq);
Bob Copeland8a63fac2010-09-17 12:45:07 +09002472 goto err_queues;
2473 }
Bruno Randolf925e0b02010-09-17 11:36:35 +09002474 txq = ath5k_txq_setup(sc, AR5K_TX_QUEUE_DATA, AR5K_WME_AC_VI);
2475 if (IS_ERR(txq)) {
2476 ATH5K_ERR(sc, "can't setup xmit queue\n");
2477 ret = PTR_ERR(txq);
2478 goto err_queues;
2479 }
2480 txq = ath5k_txq_setup(sc, AR5K_TX_QUEUE_DATA, AR5K_WME_AC_BE);
2481 if (IS_ERR(txq)) {
2482 ATH5K_ERR(sc, "can't setup xmit queue\n");
2483 ret = PTR_ERR(txq);
2484 goto err_queues;
2485 }
2486 txq = ath5k_txq_setup(sc, AR5K_TX_QUEUE_DATA, AR5K_WME_AC_BK);
2487 if (IS_ERR(txq)) {
2488 ATH5K_ERR(sc, "can't setup xmit queue\n");
2489 ret = PTR_ERR(txq);
2490 goto err_queues;
2491 }
2492 hw->queues = 4;
Bob Copeland8a63fac2010-09-17 12:45:07 +09002493
2494 tasklet_init(&sc->rxtq, ath5k_tasklet_rx, (unsigned long)sc);
2495 tasklet_init(&sc->txtq, ath5k_tasklet_tx, (unsigned long)sc);
2496 tasklet_init(&sc->calib, ath5k_tasklet_calibrate, (unsigned long)sc);
2497 tasklet_init(&sc->beacontq, ath5k_tasklet_beacon, (unsigned long)sc);
2498 tasklet_init(&sc->ani_tasklet, ath5k_tasklet_ani, (unsigned long)sc);
2499
2500 INIT_WORK(&sc->reset_work, ath5k_reset_work);
2501
2502 ret = ath5k_eeprom_read_mac(ah, mac);
2503 if (ret) {
2504 ATH5K_ERR(sc, "unable to read address from EEPROM: 0x%04x\n",
2505 sc->pdev->device);
2506 goto err_queues;
2507 }
2508
2509 SET_IEEE80211_PERM_ADDR(hw, mac);
2510 /* All MAC address bits matter for ACKs */
2511 memcpy(sc->bssidmask, ath_bcast_mac, ETH_ALEN);
2512 ath5k_hw_set_bssid_mask(sc->ah, sc->bssidmask);
2513
2514 regulatory->current_rd = ah->ah_capabilities.cap_eeprom.ee_regdomain;
2515 ret = ath_regd_init(regulatory, hw->wiphy, ath5k_reg_notifier);
2516 if (ret) {
2517 ATH5K_ERR(sc, "can't initialize regulatory system\n");
2518 goto err_queues;
2519 }
2520
2521 ret = ieee80211_register_hw(hw);
2522 if (ret) {
2523 ATH5K_ERR(sc, "can't register ieee80211 hw\n");
2524 goto err_queues;
2525 }
2526
2527 if (!ath_is_world_regd(regulatory))
2528 regulatory_hint(hw->wiphy, regulatory->alpha2);
2529
2530 ath5k_init_leds(sc);
2531
2532 ath5k_sysfs_register(sc);
2533
2534 return 0;
2535err_queues:
2536 ath5k_txq_release(sc);
2537err_bhal:
2538 ath5k_hw_release_tx_queue(ah, sc->bhalq);
2539err_desc:
2540 ath5k_desc_free(sc, pdev);
2541err:
2542 return ret;
2543}
2544
2545static void
2546ath5k_detach(struct pci_dev *pdev, struct ieee80211_hw *hw)
2547{
2548 struct ath5k_softc *sc = hw->priv;
2549
2550 /*
2551 * NB: the order of these is important:
2552 * o call the 802.11 layer before detaching ath5k_hw to
2553 * ensure callbacks into the driver to delete global
2554 * key cache entries can be handled
2555 * o reclaim the tx queue data structures after calling
2556 * the 802.11 layer as we'll get called back to reclaim
2557 * node state and potentially want to use them
2558 * o to cleanup the tx queues the hal is called, so detach
2559 * it last
2560 * XXX: ??? detach ath5k_hw ???
2561 * Other than that, it's straightforward...
2562 */
2563 ieee80211_unregister_hw(hw);
2564 ath5k_desc_free(sc, pdev);
2565 ath5k_txq_release(sc);
2566 ath5k_hw_release_tx_queue(sc->ah, sc->bhalq);
2567 ath5k_unregister_leds(sc);
2568
2569 ath5k_sysfs_unregister(sc);
2570 /*
2571 * NB: can't reclaim these until after ieee80211_ifdetach
2572 * returns because we'll get called back to reclaim node
2573 * state and potentially want to use them.
2574 */
2575}
2576
2577/********************\
2578* Mac80211 functions *
2579\********************/
2580
2581static int
2582ath5k_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
2583{
2584 struct ath5k_softc *sc = hw->priv;
Bruno Randolf925e0b02010-09-17 11:36:35 +09002585 u16 qnum = skb_get_queue_mapping(skb);
Bob Copeland8a63fac2010-09-17 12:45:07 +09002586
Bruno Randolf925e0b02010-09-17 11:36:35 +09002587 if (WARN_ON(qnum >= sc->ah->ah_capabilities.cap_queues.q_tx_num)) {
2588 dev_kfree_skb_any(skb);
2589 return 0;
2590 }
2591
2592 return ath5k_tx_queue(hw, skb, &sc->txqs[qnum]);
Bob Copeland8a63fac2010-09-17 12:45:07 +09002593}
2594
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002595static int ath5k_start(struct ieee80211_hw *hw)
2596{
Bob Copelandbb2beca2009-01-19 11:20:54 -05002597 return ath5k_init(hw->priv);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002598}
2599
2600static void ath5k_stop(struct ieee80211_hw *hw)
2601{
Bob Copelandbb2beca2009-01-19 11:20:54 -05002602 ath5k_stop_hw(hw->priv);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002603}
2604
2605static int ath5k_add_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +01002606 struct ieee80211_vif *vif)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002607{
2608 struct ath5k_softc *sc = hw->priv;
2609 int ret;
2610
2611 mutex_lock(&sc->lock);
Johannes Berg32bfd352007-12-19 01:31:26 +01002612 if (sc->vif) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002613 ret = 0;
2614 goto end;
2615 }
2616
Johannes Berg1ed32e42009-12-23 13:15:45 +01002617 sc->vif = vif;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002618
Johannes Berg1ed32e42009-12-23 13:15:45 +01002619 switch (vif->type) {
Jiri Slabyda966bc2008-10-12 22:54:10 +02002620 case NL80211_IFTYPE_AP:
Johannes Berg05c914f2008-09-11 00:01:58 +02002621 case NL80211_IFTYPE_STATION:
2622 case NL80211_IFTYPE_ADHOC:
Andrey Yurovskyb706e652008-10-13 18:23:07 -07002623 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg1ed32e42009-12-23 13:15:45 +01002624 sc->opmode = vif->type;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002625 break;
2626 default:
2627 ret = -EOPNOTSUPP;
2628 goto end;
2629 }
Jiri Slaby67d2e2d2008-08-18 21:45:28 +02002630
Bruno Randolfccfe5552010-03-09 16:55:38 +09002631 ATH5K_DBG(sc, ATH5K_DEBUG_MODE, "add interface mode %d\n", sc->opmode);
2632
Johannes Berg1ed32e42009-12-23 13:15:45 +01002633 ath5k_hw_set_lladdr(sc->ah, vif->addr);
Bob Copelandae6f53f2009-07-29 10:29:03 -04002634 ath5k_mode_setup(sc);
Jiri Slaby67d2e2d2008-08-18 21:45:28 +02002635
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002636 ret = 0;
2637end:
2638 mutex_unlock(&sc->lock);
2639 return ret;
2640}
2641
2642static void
2643ath5k_remove_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +01002644 struct ieee80211_vif *vif)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002645{
2646 struct ath5k_softc *sc = hw->priv;
Bob Copeland0e149cf2008-11-17 23:40:38 -05002647 u8 mac[ETH_ALEN] = {};
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002648
2649 mutex_lock(&sc->lock);
Johannes Berg1ed32e42009-12-23 13:15:45 +01002650 if (sc->vif != vif)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002651 goto end;
2652
Bob Copeland0e149cf2008-11-17 23:40:38 -05002653 ath5k_hw_set_lladdr(sc->ah, mac);
Johannes Berg32bfd352007-12-19 01:31:26 +01002654 sc->vif = NULL;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002655end:
2656 mutex_unlock(&sc->lock);
2657}
2658
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -05002659/*
2660 * TODO: Phy disable/diversity etc
2661 */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002662static int
Johannes Berge8975582008-10-09 12:18:51 +02002663ath5k_config(struct ieee80211_hw *hw, u32 changed)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002664{
2665 struct ath5k_softc *sc = hw->priv;
Nick Kossifidisa0823812009-04-30 15:55:44 -04002666 struct ath5k_hw *ah = sc->ah;
Johannes Berge8975582008-10-09 12:18:51 +02002667 struct ieee80211_conf *conf = &hw->conf;
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04002668 int ret = 0;
Bob Copelandbe009372009-01-22 08:44:16 -05002669
2670 mutex_lock(&sc->lock);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002671
Joerg Alberte30eb4a2009-08-05 01:52:07 +02002672 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
2673 ret = ath5k_chan_set(sc, conf->channel);
2674 if (ret < 0)
2675 goto unlock;
2676 }
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04002677
Nick Kossifidisa0823812009-04-30 15:55:44 -04002678 if ((changed & IEEE80211_CONF_CHANGE_POWER) &&
2679 (sc->power_level != conf->power_level)) {
2680 sc->power_level = conf->power_level;
2681
2682 /* Half dB steps */
2683 ath5k_hw_set_txpower_limit(ah, (conf->power_level * 2));
2684 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002685
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04002686 /* TODO:
2687 * 1) Move this on config_interface and handle each case
2688 * separately eg. when we have only one STA vif, use
2689 * AR5K_ANTMODE_SINGLE_AP
2690 *
2691 * 2) Allow the user to change antenna mode eg. when only
2692 * one antenna is present
2693 *
2694 * 3) Allow the user to set default/tx antenna when possible
2695 *
2696 * 4) Default mode should handle 90% of the cases, together
2697 * with fixed a/b and single AP modes we should be able to
2698 * handle 99%. Sectored modes are extreme cases and i still
2699 * haven't found a usage for them. If we decide to support them,
2700 * then we must allow the user to set how many tx antennas we
2701 * have available
2702 */
Bruno Randolfcaec9112010-03-09 16:55:28 +09002703 ath5k_hw_set_antenna_mode(ah, ah->ah_ant_mode);
Bob Copelandbe009372009-01-22 08:44:16 -05002704
John W. Linville55aa4e02009-05-25 21:28:47 +02002705unlock:
Bob Copelandbe009372009-01-22 08:44:16 -05002706 mutex_unlock(&sc->lock);
John W. Linville55aa4e02009-05-25 21:28:47 +02002707 return ret;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002708}
2709
Johannes Berg3ac64be2009-08-17 16:16:53 +02002710static u64 ath5k_prepare_multicast(struct ieee80211_hw *hw,
Jiri Pirko22bedad32010-04-01 21:22:57 +00002711 struct netdev_hw_addr_list *mc_list)
Johannes Berg3ac64be2009-08-17 16:16:53 +02002712{
2713 u32 mfilt[2], val;
Johannes Berg3ac64be2009-08-17 16:16:53 +02002714 u8 pos;
Jiri Pirko22bedad32010-04-01 21:22:57 +00002715 struct netdev_hw_addr *ha;
Johannes Berg3ac64be2009-08-17 16:16:53 +02002716
2717 mfilt[0] = 0;
2718 mfilt[1] = 1;
2719
Jiri Pirko22bedad32010-04-01 21:22:57 +00002720 netdev_hw_addr_list_for_each(ha, mc_list) {
Johannes Berg3ac64be2009-08-17 16:16:53 +02002721 /* calculate XOR of eight 6-bit values */
Jiri Pirko22bedad32010-04-01 21:22:57 +00002722 val = get_unaligned_le32(ha->addr + 0);
Johannes Berg3ac64be2009-08-17 16:16:53 +02002723 pos = (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
Jiri Pirko22bedad32010-04-01 21:22:57 +00002724 val = get_unaligned_le32(ha->addr + 3);
Johannes Berg3ac64be2009-08-17 16:16:53 +02002725 pos ^= (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
2726 pos &= 0x3f;
2727 mfilt[pos / 32] |= (1 << (pos % 32));
2728 /* XXX: we might be able to just do this instead,
2729 * but not sure, needs testing, if we do use this we'd
2730 * neet to inform below to not reset the mcast */
2731 /* ath5k_hw_set_mcast_filterindex(ah,
Jiri Pirko22bedad32010-04-01 21:22:57 +00002732 * ha->addr[5]); */
Johannes Berg3ac64be2009-08-17 16:16:53 +02002733 }
2734
2735 return ((u64)(mfilt[1]) << 32) | mfilt[0];
2736}
2737
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002738#define SUPPORTED_FIF_FLAGS \
2739 FIF_PROMISC_IN_BSS | FIF_ALLMULTI | FIF_FCSFAIL | \
2740 FIF_PLCPFAIL | FIF_CONTROL | FIF_OTHER_BSS | \
2741 FIF_BCN_PRBRESP_PROMISC
2742/*
2743 * o always accept unicast, broadcast, and multicast traffic
2744 * o multicast traffic for all BSSIDs will be enabled if mac80211
2745 * says it should be
2746 * o maintain current state of phy ofdm or phy cck error reception.
2747 * If the hardware detects any of these type of errors then
2748 * ath5k_hw_get_rx_filter() will pass to us the respective
2749 * hardware filters to be able to receive these type of frames.
2750 * o probe request frames are accepted only when operating in
2751 * hostap, adhoc, or monitor modes
2752 * o enable promiscuous mode according to the interface state
2753 * o accept beacons:
2754 * - when operating in adhoc mode so the 802.11 layer creates
2755 * node table entries for peers,
2756 * - when operating in station mode for collecting rssi data when
2757 * the station is otherwise quiet, or
2758 * - when scanning
2759 */
2760static void ath5k_configure_filter(struct ieee80211_hw *hw,
2761 unsigned int changed_flags,
2762 unsigned int *new_flags,
Johannes Berg3ac64be2009-08-17 16:16:53 +02002763 u64 multicast)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002764{
2765 struct ath5k_softc *sc = hw->priv;
2766 struct ath5k_hw *ah = sc->ah;
Johannes Berg3ac64be2009-08-17 16:16:53 +02002767 u32 mfilt[2], rfilt;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002768
Bob Copeland56d1de02009-08-24 23:00:30 -04002769 mutex_lock(&sc->lock);
2770
Johannes Berg3ac64be2009-08-17 16:16:53 +02002771 mfilt[0] = multicast;
2772 mfilt[1] = multicast >> 32;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002773
2774 /* Only deal with supported flags */
2775 changed_flags &= SUPPORTED_FIF_FLAGS;
2776 *new_flags &= SUPPORTED_FIF_FLAGS;
2777
2778 /* If HW detects any phy or radar errors, leave those filters on.
2779 * Also, always enable Unicast, Broadcasts and Multicast
2780 * XXX: move unicast, bssid broadcasts and multicast to mac80211 */
2781 rfilt = (ath5k_hw_get_rx_filter(ah) & (AR5K_RX_FILTER_PHYERR)) |
2782 (AR5K_RX_FILTER_UCAST | AR5K_RX_FILTER_BCAST |
2783 AR5K_RX_FILTER_MCAST);
2784
2785 if (changed_flags & (FIF_PROMISC_IN_BSS | FIF_OTHER_BSS)) {
2786 if (*new_flags & FIF_PROMISC_IN_BSS) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002787 __set_bit(ATH_STAT_PROMISC, sc->status);
John Daiker0bbac082008-10-17 12:16:00 -07002788 } else {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002789 __clear_bit(ATH_STAT_PROMISC, sc->status);
John Daiker0bbac082008-10-17 12:16:00 -07002790 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002791 }
2792
Bob Copeland6b5dcccb2010-06-04 08:14:14 -04002793 if (test_bit(ATH_STAT_PROMISC, sc->status))
2794 rfilt |= AR5K_RX_FILTER_PROM;
2795
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002796 /* Note, AR5K_RX_FILTER_MCAST is already enabled */
2797 if (*new_flags & FIF_ALLMULTI) {
2798 mfilt[0] = ~0;
2799 mfilt[1] = ~0;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002800 }
2801
2802 /* This is the best we can do */
2803 if (*new_flags & (FIF_FCSFAIL | FIF_PLCPFAIL))
2804 rfilt |= AR5K_RX_FILTER_PHYERR;
2805
2806 /* FIF_BCN_PRBRESP_PROMISC really means to enable beacons
Bob Copeland30bf4162010-08-15 13:03:15 -04002807 * and probes for any BSSID */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002808 if (*new_flags & FIF_BCN_PRBRESP_PROMISC)
Bob Copeland30bf4162010-08-15 13:03:15 -04002809 rfilt |= AR5K_RX_FILTER_BEACON;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002810
2811 /* FIF_CONTROL doc says that if FIF_PROMISC_IN_BSS is not
2812 * set we should only pass on control frames for this
2813 * station. This needs testing. I believe right now this
2814 * enables *all* control frames, which is OK.. but
2815 * but we should see if we can improve on granularity */
2816 if (*new_flags & FIF_CONTROL)
2817 rfilt |= AR5K_RX_FILTER_CONTROL;
2818
2819 /* Additional settings per mode -- this is per ath5k */
2820
2821 /* XXX move these to mac80211, and add a beacon IFF flag to mac80211 */
2822
Bob Copeland56d1de02009-08-24 23:00:30 -04002823 switch (sc->opmode) {
2824 case NL80211_IFTYPE_MESH_POINT:
Bob Copeland56d1de02009-08-24 23:00:30 -04002825 rfilt |= AR5K_RX_FILTER_CONTROL |
2826 AR5K_RX_FILTER_BEACON |
2827 AR5K_RX_FILTER_PROBEREQ |
2828 AR5K_RX_FILTER_PROM;
2829 break;
2830 case NL80211_IFTYPE_AP:
2831 case NL80211_IFTYPE_ADHOC:
2832 rfilt |= AR5K_RX_FILTER_PROBEREQ |
2833 AR5K_RX_FILTER_BEACON;
2834 break;
2835 case NL80211_IFTYPE_STATION:
2836 if (sc->assoc)
2837 rfilt |= AR5K_RX_FILTER_BEACON;
2838 default:
2839 break;
2840 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002841
2842 /* Set filters */
John Daiker0bbac082008-10-17 12:16:00 -07002843 ath5k_hw_set_rx_filter(ah, rfilt);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002844
2845 /* Set multicast bits */
2846 ath5k_hw_set_mcast_filter(ah, mfilt[0], mfilt[1]);
Bob Copelanda180a132010-08-15 13:03:12 -04002847 /* Set the cached hw filter flags, this will later actually
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002848 * be set in HW */
2849 sc->filter_flags = rfilt;
Bob Copeland56d1de02009-08-24 23:00:30 -04002850
2851 mutex_unlock(&sc->lock);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002852}
2853
2854static int
2855ath5k_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Johannes Bergdc822b52008-12-29 12:55:09 +01002856 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
2857 struct ieee80211_key_conf *key)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002858{
2859 struct ath5k_softc *sc = hw->priv;
Luis R. Rodriguezdc1e0012009-11-04 17:47:31 -08002860 struct ath5k_hw *ah = sc->ah;
2861 struct ath_common *common = ath5k_hw_common(ah);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002862 int ret = 0;
2863
Bob Copeland9ad9a262008-10-29 08:30:54 -04002864 if (modparam_nohwcrypt)
2865 return -EOPNOTSUPP;
2866
Johannes Berg97359d12010-08-10 09:46:38 +02002867 switch (key->cipher) {
2868 case WLAN_CIPHER_SUITE_WEP40:
2869 case WLAN_CIPHER_SUITE_WEP104:
2870 case WLAN_CIPHER_SUITE_TKIP:
Bob Copeland3f64b432008-10-29 23:19:14 -04002871 break;
Johannes Berg97359d12010-08-10 09:46:38 +02002872 case WLAN_CIPHER_SUITE_CCMP:
Bruno Randolf781f3132010-09-08 16:04:59 +09002873 if (common->crypt_caps & ATH_CRYPT_CAP_CIPHER_AESCCM)
Bob Copeland1c818742009-08-24 23:00:33 -04002874 break;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002875 return -EOPNOTSUPP;
2876 default:
2877 WARN_ON(1);
2878 return -EINVAL;
2879 }
2880
2881 mutex_lock(&sc->lock);
2882
2883 switch (cmd) {
2884 case SET_KEY:
Bruno Randolfe0f8c2a2010-09-08 16:04:43 +09002885 ret = ath_key_config(common, vif, sta, key);
2886 if (ret >= 0) {
2887 key->hw_key_idx = ret;
2888 /* push IV and Michael MIC generation to stack */
2889 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
2890 if (key->cipher == WLAN_CIPHER_SUITE_TKIP)
2891 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
2892 if (key->cipher == WLAN_CIPHER_SUITE_CCMP)
2893 key->flags |= IEEE80211_KEY_FLAG_SW_MGMT;
2894 ret = 0;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002895 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002896 break;
2897 case DISABLE_KEY:
Bruno Randolfe0f8c2a2010-09-08 16:04:43 +09002898 ath_key_delete(common, key);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002899 break;
2900 default:
2901 ret = -EINVAL;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002902 }
2903
Jiri Slaby274c7c32008-07-15 17:44:20 +02002904 mmiowb();
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002905 mutex_unlock(&sc->lock);
2906 return ret;
2907}
2908
2909static int
2910ath5k_get_stats(struct ieee80211_hw *hw,
2911 struct ieee80211_low_level_stats *stats)
2912{
2913 struct ath5k_softc *sc = hw->priv;
Nick Kossifidis194828a2008-04-16 18:49:02 +03002914
2915 /* Force update */
Bruno Randolf495391d2010-03-25 14:49:36 +09002916 ath5k_hw_update_mib_counters(sc->ah);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002917
Bruno Randolf495391d2010-03-25 14:49:36 +09002918 stats->dot11ACKFailureCount = sc->stats.ack_fail;
2919 stats->dot11RTSFailureCount = sc->stats.rts_fail;
2920 stats->dot11RTSSuccessCount = sc->stats.rts_ok;
2921 stats->dot11FCSErrorCount = sc->stats.fcs_error;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002922
2923 return 0;
2924}
2925
Holger Schurig55ee82b2010-04-19 10:24:22 +02002926static int ath5k_get_survey(struct ieee80211_hw *hw, int idx,
2927 struct survey_info *survey)
2928{
2929 struct ath5k_softc *sc = hw->priv;
2930 struct ieee80211_conf *conf = &hw->conf;
2931
2932 if (idx != 0)
2933 return -ENOENT;
2934
2935 survey->channel = conf->channel;
2936 survey->filled = SURVEY_INFO_NOISE_DBM;
2937 survey->noise = sc->ah->ah_noise_floor;
2938
2939 return 0;
2940}
2941
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002942static u64
2943ath5k_get_tsf(struct ieee80211_hw *hw)
2944{
2945 struct ath5k_softc *sc = hw->priv;
2946
2947 return ath5k_hw_get_tsf64(sc->ah);
2948}
2949
2950static void
Alina Friedrichsen3b5d6652009-01-24 07:09:59 +01002951ath5k_set_tsf(struct ieee80211_hw *hw, u64 tsf)
2952{
2953 struct ath5k_softc *sc = hw->priv;
2954
2955 ath5k_hw_set_tsf64(sc->ah, tsf);
2956}
2957
2958static void
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002959ath5k_reset_tsf(struct ieee80211_hw *hw)
2960{
2961 struct ath5k_softc *sc = hw->priv;
2962
Bruno Randolf9804b982008-01-19 18:17:59 +09002963 /*
2964 * in IBSS mode we need to update the beacon timers too.
2965 * this will also reset the TSF if we call it with 0
2966 */
Johannes Berg05c914f2008-09-11 00:01:58 +02002967 if (sc->opmode == NL80211_IFTYPE_ADHOC)
Bruno Randolf9804b982008-01-19 18:17:59 +09002968 ath5k_beacon_update_timers(sc, 0);
2969 else
2970 ath5k_hw_reset_tsf(sc->ah);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002971}
2972
Martin Xu02969b32008-11-24 10:49:27 +08002973static void
2974set_beacon_filter(struct ieee80211_hw *hw, bool enable)
2975{
2976 struct ath5k_softc *sc = hw->priv;
2977 struct ath5k_hw *ah = sc->ah;
2978 u32 rfilt;
2979 rfilt = ath5k_hw_get_rx_filter(ah);
2980 if (enable)
2981 rfilt |= AR5K_RX_FILTER_BEACON;
2982 else
2983 rfilt &= ~AR5K_RX_FILTER_BEACON;
2984 ath5k_hw_set_rx_filter(ah, rfilt);
2985 sc->filter_flags = rfilt;
2986}
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002987
Martin Xu02969b32008-11-24 10:49:27 +08002988static void ath5k_bss_info_changed(struct ieee80211_hw *hw,
2989 struct ieee80211_vif *vif,
2990 struct ieee80211_bss_conf *bss_conf,
2991 u32 changes)
2992{
2993 struct ath5k_softc *sc = hw->priv;
Johannes Berg2d0ddec2009-04-23 16:13:26 +02002994 struct ath5k_hw *ah = sc->ah;
Luis R. Rodriguez954fece2009-09-10 10:51:33 -07002995 struct ath_common *common = ath5k_hw_common(ah);
Bob Copeland21800492009-07-04 12:59:52 -04002996 unsigned long flags;
Johannes Berg2d0ddec2009-04-23 16:13:26 +02002997
2998 mutex_lock(&sc->lock);
2999 if (WARN_ON(sc->vif != vif))
3000 goto unlock;
3001
3002 if (changes & BSS_CHANGED_BSSID) {
3003 /* Cache for later use during resets */
Luis R. Rodriguez954fece2009-09-10 10:51:33 -07003004 memcpy(common->curbssid, bss_conf->bssid, ETH_ALEN);
Luis R. Rodriguez8ce54c52009-10-06 20:44:34 -04003005 common->curaid = 0;
Nick Kossifidis418de6d2010-08-15 13:03:10 -04003006 ath5k_hw_set_bssid(ah);
Johannes Berg2d0ddec2009-04-23 16:13:26 +02003007 mmiowb();
3008 }
Johannes Berg57c4d7b2009-04-23 16:10:04 +02003009
3010 if (changes & BSS_CHANGED_BEACON_INT)
3011 sc->bintval = bss_conf->beacon_int;
3012
Martin Xu02969b32008-11-24 10:49:27 +08003013 if (changes & BSS_CHANGED_ASSOC) {
Martin Xu02969b32008-11-24 10:49:27 +08003014 sc->assoc = bss_conf->assoc;
3015 if (sc->opmode == NL80211_IFTYPE_STATION)
3016 set_beacon_filter(hw, sc->assoc);
Bob Copelandf0f3d382009-06-10 22:22:21 -04003017 ath5k_hw_set_ledstate(sc->ah, sc->assoc ?
3018 AR5K_LED_ASSOC : AR5K_LED_INIT);
Luis R. Rodriguez8ce54c52009-10-06 20:44:34 -04003019 if (bss_conf->assoc) {
3020 ATH5K_DBG(sc, ATH5K_DEBUG_ANY,
3021 "Bss Info ASSOC %d, bssid: %pM\n",
3022 bss_conf->aid, common->curbssid);
3023 common->curaid = bss_conf->aid;
Nick Kossifidis418de6d2010-08-15 13:03:10 -04003024 ath5k_hw_set_bssid(ah);
Luis R. Rodriguez8ce54c52009-10-06 20:44:34 -04003025 /* Once ANI is available you would start it here */
3026 }
Martin Xu02969b32008-11-24 10:49:27 +08003027 }
Johannes Berg2d0ddec2009-04-23 16:13:26 +02003028
Bob Copeland21800492009-07-04 12:59:52 -04003029 if (changes & BSS_CHANGED_BEACON) {
3030 spin_lock_irqsave(&sc->block, flags);
3031 ath5k_beacon_update(hw, vif);
3032 spin_unlock_irqrestore(&sc->block, flags);
Johannes Berg2d0ddec2009-04-23 16:13:26 +02003033 }
3034
Bob Copeland21800492009-07-04 12:59:52 -04003035 if (changes & BSS_CHANGED_BEACON_ENABLED)
3036 sc->enable_beacon = bss_conf->enable_beacon;
3037
3038 if (changes & (BSS_CHANGED_BEACON | BSS_CHANGED_BEACON_ENABLED |
3039 BSS_CHANGED_BEACON_INT))
3040 ath5k_beacon_config(sc);
3041
Johannes Berg2d0ddec2009-04-23 16:13:26 +02003042 unlock:
3043 mutex_unlock(&sc->lock);
Martin Xu02969b32008-11-24 10:49:27 +08003044}
Bob Copelandf0f3d382009-06-10 22:22:21 -04003045
3046static void ath5k_sw_scan_start(struct ieee80211_hw *hw)
3047{
3048 struct ath5k_softc *sc = hw->priv;
3049 if (!sc->assoc)
3050 ath5k_hw_set_ledstate(sc->ah, AR5K_LED_SCAN);
3051}
3052
3053static void ath5k_sw_scan_complete(struct ieee80211_hw *hw)
3054{
3055 struct ath5k_softc *sc = hw->priv;
3056 ath5k_hw_set_ledstate(sc->ah, sc->assoc ?
3057 AR5K_LED_ASSOC : AR5K_LED_INIT);
3058}
Lukáš Turek6e08d222009-12-21 22:50:51 +01003059
3060/**
3061 * ath5k_set_coverage_class - Set IEEE 802.11 coverage class
3062 *
3063 * @hw: struct ieee80211_hw pointer
3064 * @coverage_class: IEEE 802.11 coverage class number
3065 *
3066 * Mac80211 callback. Sets slot time, ACK timeout and CTS timeout for given
3067 * coverage class. The values are persistent, they are restored after device
3068 * reset.
3069 */
3070static void ath5k_set_coverage_class(struct ieee80211_hw *hw, u8 coverage_class)
3071{
3072 struct ath5k_softc *sc = hw->priv;
3073
3074 mutex_lock(&sc->lock);
3075 ath5k_hw_set_coverage_class(sc->ah, coverage_class);
3076 mutex_unlock(&sc->lock);
3077}
Bob Copeland8a63fac2010-09-17 12:45:07 +09003078
3079static const struct ieee80211_ops ath5k_hw_ops = {
3080 .tx = ath5k_tx,
3081 .start = ath5k_start,
3082 .stop = ath5k_stop,
3083 .add_interface = ath5k_add_interface,
3084 .remove_interface = ath5k_remove_interface,
3085 .config = ath5k_config,
3086 .prepare_multicast = ath5k_prepare_multicast,
3087 .configure_filter = ath5k_configure_filter,
3088 .set_key = ath5k_set_key,
3089 .get_stats = ath5k_get_stats,
3090 .get_survey = ath5k_get_survey,
3091 .conf_tx = NULL,
3092 .get_tsf = ath5k_get_tsf,
3093 .set_tsf = ath5k_set_tsf,
3094 .reset_tsf = ath5k_reset_tsf,
3095 .bss_info_changed = ath5k_bss_info_changed,
3096 .sw_scan_start = ath5k_sw_scan_start,
3097 .sw_scan_complete = ath5k_sw_scan_complete,
3098 .set_coverage_class = ath5k_set_coverage_class,
3099};
3100
3101/********************\
3102* PCI Initialization *
3103\********************/
3104
3105static int __devinit
3106ath5k_pci_probe(struct pci_dev *pdev,
3107 const struct pci_device_id *id)
3108{
3109 void __iomem *mem;
3110 struct ath5k_softc *sc;
3111 struct ath_common *common;
3112 struct ieee80211_hw *hw;
3113 int ret;
3114 u8 csz;
3115
3116 /*
3117 * L0s needs to be disabled on all ath5k cards.
3118 *
3119 * For distributions shipping with CONFIG_PCIEASPM (this will be enabled
3120 * by default in the future in 2.6.36) this will also mean both L1 and
3121 * L0s will be disabled when a pre 1.1 PCIe device is detected. We do
3122 * know L1 works correctly even for all ath5k pre 1.1 PCIe devices
3123 * though but cannot currently undue the effect of a blacklist, for
3124 * details you can read pcie_aspm_sanity_check() and see how it adjusts
3125 * the device link capability.
3126 *
3127 * It may be possible in the future to implement some PCI API to allow
3128 * drivers to override blacklists for pre 1.1 PCIe but for now it is
3129 * best to accept that both L0s and L1 will be disabled completely for
3130 * distributions shipping with CONFIG_PCIEASPM rather than having this
3131 * issue present. Motivation for adding this new API will be to help
3132 * with power consumption for some of these devices.
3133 */
3134 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S);
3135
3136 ret = pci_enable_device(pdev);
3137 if (ret) {
3138 dev_err(&pdev->dev, "can't enable device\n");
3139 goto err;
3140 }
3141
3142 /* XXX 32-bit addressing only */
3143 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
3144 if (ret) {
3145 dev_err(&pdev->dev, "32-bit DMA not available\n");
3146 goto err_dis;
3147 }
3148
3149 /*
3150 * Cache line size is used to size and align various
3151 * structures used to communicate with the hardware.
3152 */
3153 pci_read_config_byte(pdev, PCI_CACHE_LINE_SIZE, &csz);
3154 if (csz == 0) {
3155 /*
3156 * Linux 2.4.18 (at least) writes the cache line size
3157 * register as a 16-bit wide register which is wrong.
3158 * We must have this setup properly for rx buffer
3159 * DMA to work so force a reasonable value here if it
3160 * comes up zero.
3161 */
3162 csz = L1_CACHE_BYTES >> 2;
3163 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, csz);
3164 }
3165 /*
3166 * The default setting of latency timer yields poor results,
3167 * set it to the value used by other systems. It may be worth
3168 * tweaking this setting more.
3169 */
3170 pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0xa8);
3171
3172 /* Enable bus mastering */
3173 pci_set_master(pdev);
3174
3175 /*
3176 * Disable the RETRY_TIMEOUT register (0x41) to keep
3177 * PCI Tx retries from interfering with C3 CPU state.
3178 */
3179 pci_write_config_byte(pdev, 0x41, 0);
3180
3181 ret = pci_request_region(pdev, 0, "ath5k");
3182 if (ret) {
3183 dev_err(&pdev->dev, "cannot reserve PCI memory region\n");
3184 goto err_dis;
3185 }
3186
3187 mem = pci_iomap(pdev, 0, 0);
3188 if (!mem) {
3189 dev_err(&pdev->dev, "cannot remap PCI memory region\n") ;
3190 ret = -EIO;
3191 goto err_reg;
3192 }
3193
3194 /*
3195 * Allocate hw (mac80211 main struct)
3196 * and hw->priv (driver private data)
3197 */
3198 hw = ieee80211_alloc_hw(sizeof(*sc), &ath5k_hw_ops);
3199 if (hw == NULL) {
3200 dev_err(&pdev->dev, "cannot allocate ieee80211_hw\n");
3201 ret = -ENOMEM;
3202 goto err_map;
3203 }
3204
3205 dev_info(&pdev->dev, "registered as '%s'\n", wiphy_name(hw->wiphy));
3206
3207 /* Initialize driver private data */
3208 SET_IEEE80211_DEV(hw, &pdev->dev);
3209 hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
3210 IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
3211 IEEE80211_HW_SIGNAL_DBM;
3212
3213 hw->wiphy->interface_modes =
3214 BIT(NL80211_IFTYPE_AP) |
3215 BIT(NL80211_IFTYPE_STATION) |
3216 BIT(NL80211_IFTYPE_ADHOC) |
3217 BIT(NL80211_IFTYPE_MESH_POINT);
3218
3219 hw->extra_tx_headroom = 2;
3220 hw->channel_change_time = 5000;
3221 sc = hw->priv;
3222 sc->hw = hw;
3223 sc->pdev = pdev;
3224
3225 ath5k_debug_init_device(sc);
3226
3227 /*
3228 * Mark the device as detached to avoid processing
3229 * interrupts until setup is complete.
3230 */
3231 __set_bit(ATH_STAT_INVALID, sc->status);
3232
3233 sc->iobase = mem; /* So we can unmap it on detach */
3234 sc->opmode = NL80211_IFTYPE_STATION;
3235 sc->bintval = 1000;
3236 mutex_init(&sc->lock);
3237 spin_lock_init(&sc->rxbuflock);
3238 spin_lock_init(&sc->txbuflock);
3239 spin_lock_init(&sc->block);
3240
3241 /* Set private data */
3242 pci_set_drvdata(pdev, sc);
3243
3244 /* Setup interrupt handler */
3245 ret = request_irq(pdev->irq, ath5k_intr, IRQF_SHARED, "ath", sc);
3246 if (ret) {
3247 ATH5K_ERR(sc, "request_irq failed\n");
3248 goto err_free;
3249 }
3250
3251 /* If we passed the test, malloc an ath5k_hw struct */
3252 sc->ah = kzalloc(sizeof(struct ath5k_hw), GFP_KERNEL);
3253 if (!sc->ah) {
3254 ret = -ENOMEM;
3255 ATH5K_ERR(sc, "out of memory\n");
3256 goto err_irq;
3257 }
3258
3259 sc->ah->ah_sc = sc;
3260 sc->ah->ah_iobase = sc->iobase;
3261 common = ath5k_hw_common(sc->ah);
3262 common->ops = &ath5k_common_ops;
3263 common->ah = sc->ah;
3264 common->hw = hw;
3265 common->cachelsz = csz << 2; /* convert to bytes */
3266
3267 /* Initialize device */
3268 ret = ath5k_hw_attach(sc);
3269 if (ret) {
3270 goto err_free_ah;
3271 }
3272
3273 /* set up multi-rate retry capabilities */
3274 if (sc->ah->ah_version == AR5K_AR5212) {
3275 hw->max_rates = 4;
3276 hw->max_rate_tries = 11;
3277 }
3278
3279 /* Finish private driver data initialization */
3280 ret = ath5k_attach(pdev, hw);
3281 if (ret)
3282 goto err_ah;
3283
3284 ATH5K_INFO(sc, "Atheros AR%s chip found (MAC: 0x%x, PHY: 0x%x)\n",
3285 ath5k_chip_name(AR5K_VERSION_MAC, sc->ah->ah_mac_srev),
3286 sc->ah->ah_mac_srev,
3287 sc->ah->ah_phy_revision);
3288
3289 if (!sc->ah->ah_single_chip) {
3290 /* Single chip radio (!RF5111) */
3291 if (sc->ah->ah_radio_5ghz_revision &&
3292 !sc->ah->ah_radio_2ghz_revision) {
3293 /* No 5GHz support -> report 2GHz radio */
3294 if (!test_bit(AR5K_MODE_11A,
3295 sc->ah->ah_capabilities.cap_mode)) {
3296 ATH5K_INFO(sc, "RF%s 2GHz radio found (0x%x)\n",
3297 ath5k_chip_name(AR5K_VERSION_RAD,
3298 sc->ah->ah_radio_5ghz_revision),
3299 sc->ah->ah_radio_5ghz_revision);
3300 /* No 2GHz support (5110 and some
3301 * 5Ghz only cards) -> report 5Ghz radio */
3302 } else if (!test_bit(AR5K_MODE_11B,
3303 sc->ah->ah_capabilities.cap_mode)) {
3304 ATH5K_INFO(sc, "RF%s 5GHz radio found (0x%x)\n",
3305 ath5k_chip_name(AR5K_VERSION_RAD,
3306 sc->ah->ah_radio_5ghz_revision),
3307 sc->ah->ah_radio_5ghz_revision);
3308 /* Multiband radio */
3309 } else {
3310 ATH5K_INFO(sc, "RF%s multiband radio found"
3311 " (0x%x)\n",
3312 ath5k_chip_name(AR5K_VERSION_RAD,
3313 sc->ah->ah_radio_5ghz_revision),
3314 sc->ah->ah_radio_5ghz_revision);
3315 }
3316 }
3317 /* Multi chip radio (RF5111 - RF2111) ->
3318 * report both 2GHz/5GHz radios */
3319 else if (sc->ah->ah_radio_5ghz_revision &&
3320 sc->ah->ah_radio_2ghz_revision){
3321 ATH5K_INFO(sc, "RF%s 5GHz radio found (0x%x)\n",
3322 ath5k_chip_name(AR5K_VERSION_RAD,
3323 sc->ah->ah_radio_5ghz_revision),
3324 sc->ah->ah_radio_5ghz_revision);
3325 ATH5K_INFO(sc, "RF%s 2GHz radio found (0x%x)\n",
3326 ath5k_chip_name(AR5K_VERSION_RAD,
3327 sc->ah->ah_radio_2ghz_revision),
3328 sc->ah->ah_radio_2ghz_revision);
3329 }
3330 }
3331
3332
3333 /* ready to process interrupts */
3334 __clear_bit(ATH_STAT_INVALID, sc->status);
3335
3336 return 0;
3337err_ah:
3338 ath5k_hw_detach(sc->ah);
3339err_free_ah:
3340 kfree(sc->ah);
3341err_irq:
3342 free_irq(pdev->irq, sc);
3343err_free:
3344 ieee80211_free_hw(hw);
3345err_map:
3346 pci_iounmap(pdev, mem);
3347err_reg:
3348 pci_release_region(pdev, 0);
3349err_dis:
3350 pci_disable_device(pdev);
3351err:
3352 return ret;
3353}
3354
3355static void __devexit
3356ath5k_pci_remove(struct pci_dev *pdev)
3357{
3358 struct ath5k_softc *sc = pci_get_drvdata(pdev);
3359
3360 ath5k_debug_finish_device(sc);
3361 ath5k_detach(pdev, sc->hw);
3362 ath5k_hw_detach(sc->ah);
3363 kfree(sc->ah);
3364 free_irq(pdev->irq, sc);
3365 pci_iounmap(pdev, sc->iobase);
3366 pci_release_region(pdev, 0);
3367 pci_disable_device(pdev);
3368 ieee80211_free_hw(sc->hw);
3369}
3370
3371#ifdef CONFIG_PM_SLEEP
3372static int ath5k_pci_suspend(struct device *dev)
3373{
3374 struct ath5k_softc *sc = pci_get_drvdata(to_pci_dev(dev));
3375
3376 ath5k_led_off(sc);
3377 return 0;
3378}
3379
3380static int ath5k_pci_resume(struct device *dev)
3381{
3382 struct pci_dev *pdev = to_pci_dev(dev);
3383 struct ath5k_softc *sc = pci_get_drvdata(pdev);
3384
3385 /*
3386 * Suspend/Resume resets the PCI configuration space, so we have to
3387 * re-disable the RETRY_TIMEOUT register (0x41) to keep
3388 * PCI Tx retries from interfering with C3 CPU state
3389 */
3390 pci_write_config_byte(pdev, 0x41, 0);
3391
3392 ath5k_led_enable(sc);
3393 return 0;
3394}
3395
3396static SIMPLE_DEV_PM_OPS(ath5k_pm_ops, ath5k_pci_suspend, ath5k_pci_resume);
3397#define ATH5K_PM_OPS (&ath5k_pm_ops)
3398#else
3399#define ATH5K_PM_OPS NULL
3400#endif /* CONFIG_PM_SLEEP */
3401
3402static struct pci_driver ath5k_pci_driver = {
3403 .name = KBUILD_MODNAME,
3404 .id_table = ath5k_pci_id_table,
3405 .probe = ath5k_pci_probe,
3406 .remove = __devexit_p(ath5k_pci_remove),
3407 .driver.pm = ATH5K_PM_OPS,
3408};
3409
3410/*
3411 * Module init/exit functions
3412 */
3413static int __init
3414init_ath5k_pci(void)
3415{
3416 int ret;
3417
3418 ath5k_debug_init();
3419
3420 ret = pci_register_driver(&ath5k_pci_driver);
3421 if (ret) {
3422 printk(KERN_ERR "ath5k_pci: can't register pci driver\n");
3423 return ret;
3424 }
3425
3426 return 0;
3427}
3428
3429static void __exit
3430exit_ath5k_pci(void)
3431{
3432 pci_unregister_driver(&ath5k_pci_driver);
3433
3434 ath5k_debug_finish();
3435}
3436
3437module_init(init_ath5k_pci);
3438module_exit(exit_ath5k_pci);