blob: 4103765000ea69cbe071216193a089d0f575384b [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 Fietkaua6c8d372009-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 Fietkaua6c8d372009-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 Fietkaua6c8d372009-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 Fietkaua6c8d372009-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 Fietkau2f7fe872008-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);
John W. Linvilled8e1ba72010-08-24 15:27:34 -0400676 if (!rate) {
677 ret = -EINVAL;
678 goto err_unmap;
679 }
Bob Copeland8902ff42009-01-22 08:44:20 -0500680
Johannes Berge039fa42008-05-15 12:55:29 +0200681 if (info->flags & IEEE80211_TX_CTL_NO_ACK)
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200682 flags |= AR5K_TXDESC_NOACK;
683
Bob Copeland8902ff42009-01-22 08:44:20 -0500684 rc_flags = info->control.rates[0].flags;
685 hw_rate = (rc_flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE) ?
686 rate->hw_value_short : rate->hw_value;
687
Bruno Randolf281c56d2008-02-05 18:44:55 +0900688 pktlen = skb->len;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200689
Nick Kossifidis8f655dd2009-03-15 22:20:35 +0200690 /* FIXME: If we are in g mode and rate is a CCK rate
691 * subtract ah->ah_txpower.txp_cck_ofdm_pwr_delta
692 * from tx power (value is in dB units already) */
Bob Copeland362695e2009-02-15 12:06:12 -0500693 if (info->control.hw_key) {
694 keyidx = info->control.hw_key->hw_key_idx;
695 pktlen += info->control.hw_key->icv_len;
696 }
Bob Copeland07c1e852009-01-22 08:44:21 -0500697 if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
698 flags |= AR5K_TXDESC_RTSENA;
699 cts_rate = ieee80211_get_rts_cts_rate(sc->hw, info)->hw_value;
700 duration = le16_to_cpu(ieee80211_rts_duration(sc->hw,
701 sc->vif, pktlen, info));
702 }
703 if (rc_flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
704 flags |= AR5K_TXDESC_CTSENA;
705 cts_rate = ieee80211_get_rts_cts_rate(sc->hw, info)->hw_value;
706 duration = le16_to_cpu(ieee80211_ctstoself_duration(sc->hw,
707 sc->vif, pktlen, info));
708 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200709 ret = ah->ah_setup_tx_desc(ah, ds, pktlen,
Benoit Papillault8127fbd2010-02-27 23:05:26 +0100710 ieee80211_get_hdrlen_from_skb(skb), padsize,
Bob Copeland2ac29272010-02-09 13:06:54 -0500711 get_hw_packet_type(skb),
Johannes Berg2e92e6f2008-05-15 12:55:27 +0200712 (sc->power_level * 2),
Bob Copeland8902ff42009-01-22 08:44:20 -0500713 hw_rate,
Nick Kossifidis2bed03e2009-04-30 15:55:49 -0400714 info->control.rates[0].count, keyidx, ah->ah_tx_ant, flags,
Bob Copeland07c1e852009-01-22 08:44:21 -0500715 cts_rate, duration);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200716 if (ret)
717 goto err_unmap;
718
Felix Fietkau2f7fe872008-10-05 18:05:48 +0200719 memset(mrr_rate, 0, sizeof(mrr_rate));
720 memset(mrr_tries, 0, sizeof(mrr_tries));
721 for (i = 0; i < 3; i++) {
722 rate = ieee80211_get_alt_retry_rate(sc->hw, info, i);
723 if (!rate)
724 break;
725
726 mrr_rate[i] = rate->hw_value;
Johannes Berge6a98542008-10-21 12:40:02 +0200727 mrr_tries[i] = info->control.rates[i + 1].count;
Felix Fietkau2f7fe872008-10-05 18:05:48 +0200728 }
729
Bruno Randolfa6668192010-06-16 19:12:01 +0900730 ath5k_hw_setup_mrr_tx_desc(ah, ds,
Felix Fietkau2f7fe872008-10-05 18:05:48 +0200731 mrr_rate[0], mrr_tries[0],
732 mrr_rate[1], mrr_tries[1],
733 mrr_rate[2], mrr_tries[2]);
734
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200735 ds->ds_link = 0;
736 ds->ds_data = bf->skbaddr;
737
738 spin_lock_bh(&txq->lock);
739 list_add_tail(&bf->list, &txq->q);
Bruno Randolf925e0b02010-09-17 11:36:35 +0900740 txq->txq_len++;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200741 if (txq->link == NULL) /* is this first packet? */
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300742 ath5k_hw_set_txdp(ah, txq->qnum, bf->daddr);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200743 else /* no, so only link it */
744 *txq->link = bf->daddr;
745
746 txq->link = &ds->ds_link;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300747 ath5k_hw_start_tx_dma(ah, txq->qnum);
Jiri Slaby274c7c32008-07-15 17:44:20 +0200748 mmiowb();
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200749 spin_unlock_bh(&txq->lock);
750
751 return 0;
752err_unmap:
753 pci_unmap_single(sc->pdev, bf->skbaddr, skb->len, PCI_DMA_TODEVICE);
754 return ret;
755}
756
757/*******************\
758* Descriptors setup *
759\*******************/
760
761static int
762ath5k_desc_alloc(struct ath5k_softc *sc, struct pci_dev *pdev)
763{
764 struct ath5k_desc *ds;
765 struct ath5k_buf *bf;
766 dma_addr_t da;
767 unsigned int i;
768 int ret;
769
770 /* allocate descriptors */
771 sc->desc_len = sizeof(struct ath5k_desc) *
772 (ATH_TXBUF + ATH_RXBUF + ATH_BCBUF + 1);
773 sc->desc = pci_alloc_consistent(pdev, sc->desc_len, &sc->desc_daddr);
774 if (sc->desc == NULL) {
775 ATH5K_ERR(sc, "can't allocate descriptors\n");
776 ret = -ENOMEM;
777 goto err;
778 }
779 ds = sc->desc;
780 da = sc->desc_daddr;
781 ATH5K_DBG(sc, ATH5K_DEBUG_ANY, "DMA map: %p (%zu) -> %llx\n",
782 ds, sc->desc_len, (unsigned long long)sc->desc_daddr);
783
784 bf = kcalloc(1 + ATH_TXBUF + ATH_RXBUF + ATH_BCBUF,
785 sizeof(struct ath5k_buf), GFP_KERNEL);
786 if (bf == NULL) {
787 ATH5K_ERR(sc, "can't allocate bufptr\n");
788 ret = -ENOMEM;
789 goto err_free;
790 }
791 sc->bufptr = bf;
792
793 INIT_LIST_HEAD(&sc->rxbuf);
794 for (i = 0; i < ATH_RXBUF; i++, bf++, ds++, da += sizeof(*ds)) {
795 bf->desc = ds;
796 bf->daddr = da;
797 list_add_tail(&bf->list, &sc->rxbuf);
798 }
799
800 INIT_LIST_HEAD(&sc->txbuf);
801 sc->txbuf_len = ATH_TXBUF;
802 for (i = 0; i < ATH_TXBUF; i++, bf++, ds++,
803 da += sizeof(*ds)) {
804 bf->desc = ds;
805 bf->daddr = da;
806 list_add_tail(&bf->list, &sc->txbuf);
807 }
808
809 /* beacon buffer */
810 bf->desc = ds;
811 bf->daddr = da;
812 sc->bbuf = bf;
813
814 return 0;
815err_free:
816 pci_free_consistent(pdev, sc->desc_len, sc->desc, sc->desc_daddr);
817err:
818 sc->desc = NULL;
819 return ret;
820}
821
822static void
823ath5k_desc_free(struct ath5k_softc *sc, struct pci_dev *pdev)
824{
825 struct ath5k_buf *bf;
826
Bruno Randolf9e4e43f2010-06-16 19:11:17 +0900827 ath5k_txbuf_free_skb(sc, sc->bbuf);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200828 list_for_each_entry(bf, &sc->txbuf, list)
Bruno Randolf9e4e43f2010-06-16 19:11:17 +0900829 ath5k_txbuf_free_skb(sc, bf);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200830 list_for_each_entry(bf, &sc->rxbuf, list)
Bruno Randolf9e4e43f2010-06-16 19:11:17 +0900831 ath5k_rxbuf_free_skb(sc, bf);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200832
833 /* Free memory associated with all descriptors */
834 pci_free_consistent(pdev, sc->desc_len, sc->desc, sc->desc_daddr);
Bruno Randolf39d63f22010-06-16 19:11:41 +0900835 sc->desc = NULL;
836 sc->desc_daddr = 0;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200837
838 kfree(sc->bufptr);
839 sc->bufptr = NULL;
Bruno Randolf39d63f22010-06-16 19:11:41 +0900840 sc->bbuf = NULL;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200841}
842
843
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200844/**************\
845* Queues setup *
846\**************/
847
848static struct ath5k_txq *
849ath5k_txq_setup(struct ath5k_softc *sc,
850 int qtype, int subtype)
851{
852 struct ath5k_hw *ah = sc->ah;
853 struct ath5k_txq *txq;
854 struct ath5k_txq_info qi = {
855 .tqi_subtype = subtype,
Bruno Randolfde8af452010-09-17 11:37:12 +0900856 /* XXX: default values not correct for B and XR channels,
857 * but who cares? */
858 .tqi_aifs = AR5K_TUNE_AIFS,
859 .tqi_cw_min = AR5K_TUNE_CWMIN,
860 .tqi_cw_max = AR5K_TUNE_CWMAX
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200861 };
862 int qnum;
863
864 /*
865 * Enable interrupts only for EOL and DESC conditions.
866 * We mark tx descriptors to receive a DESC interrupt
Bob Copelanda180a132010-08-15 13:03:12 -0400867 * when a tx queue gets deep; otherwise we wait for the
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200868 * EOL to reap descriptors. Note that this is done to
869 * reduce interrupt load and this only defers reaping
870 * descriptors, never transmitting frames. Aside from
871 * reducing interrupts this also permits more concurrency.
872 * The only potential downside is if the tx queue backs
873 * up in which case the top half of the kernel may backup
874 * due to a lack of tx descriptors.
875 */
876 qi.tqi_flags = AR5K_TXQ_FLAG_TXEOLINT_ENABLE |
877 AR5K_TXQ_FLAG_TXDESCINT_ENABLE;
878 qnum = ath5k_hw_setup_tx_queue(ah, qtype, &qi);
879 if (qnum < 0) {
880 /*
881 * NB: don't print a message, this happens
882 * normally on parts with too few tx queues
883 */
884 return ERR_PTR(qnum);
885 }
886 if (qnum >= ARRAY_SIZE(sc->txqs)) {
887 ATH5K_ERR(sc, "hw qnum %u out of range, max %tu!\n",
888 qnum, ARRAY_SIZE(sc->txqs));
889 ath5k_hw_release_tx_queue(ah, qnum);
890 return ERR_PTR(-EINVAL);
891 }
892 txq = &sc->txqs[qnum];
893 if (!txq->setup) {
894 txq->qnum = qnum;
895 txq->link = NULL;
896 INIT_LIST_HEAD(&txq->q);
897 spin_lock_init(&txq->lock);
898 txq->setup = true;
Bruno Randolf925e0b02010-09-17 11:36:35 +0900899 txq->txq_len = 0;
Bruno Randolf4edd7612010-09-17 11:36:56 +0900900 txq->txq_poll_mark = false;
Bruno Randolf923e5b32010-09-17 11:37:02 +0900901 txq->txq_stuck = 0;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200902 }
903 return &sc->txqs[qnum];
904}
905
906static int
907ath5k_beaconq_setup(struct ath5k_hw *ah)
908{
909 struct ath5k_txq_info qi = {
Bruno Randolfde8af452010-09-17 11:37:12 +0900910 /* XXX: default values not correct for B and XR channels,
911 * but who cares? */
912 .tqi_aifs = AR5K_TUNE_AIFS,
913 .tqi_cw_min = AR5K_TUNE_CWMIN,
914 .tqi_cw_max = AR5K_TUNE_CWMAX,
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200915 /* NB: for dynamic turbo, don't enable any other interrupts */
916 .tqi_flags = AR5K_TXQ_FLAG_TXDESCINT_ENABLE
917 };
918
919 return ath5k_hw_setup_tx_queue(ah, AR5K_TX_QUEUE_BEACON, &qi);
920}
921
922static int
923ath5k_beaconq_config(struct ath5k_softc *sc)
924{
925 struct ath5k_hw *ah = sc->ah;
926 struct ath5k_txq_info qi;
927 int ret;
928
929 ret = ath5k_hw_get_tx_queueprops(ah, sc->bhalq, &qi);
930 if (ret)
Bob Copelanda951ae22010-01-20 23:51:04 -0500931 goto err;
932
Johannes Berg05c914f2008-09-11 00:01:58 +0200933 if (sc->opmode == NL80211_IFTYPE_AP ||
934 sc->opmode == NL80211_IFTYPE_MESH_POINT) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200935 /*
936 * Always burst out beacon and CAB traffic
937 * (aifs = cwmin = cwmax = 0)
938 */
939 qi.tqi_aifs = 0;
940 qi.tqi_cw_min = 0;
941 qi.tqi_cw_max = 0;
Johannes Berg05c914f2008-09-11 00:01:58 +0200942 } else if (sc->opmode == NL80211_IFTYPE_ADHOC) {
Bruno Randolf6d91e1d2008-01-19 18:18:41 +0900943 /*
944 * Adhoc mode; backoff between 0 and (2 * cw_min).
945 */
946 qi.tqi_aifs = 0;
947 qi.tqi_cw_min = 0;
Bruno Randolfde8af452010-09-17 11:37:12 +0900948 qi.tqi_cw_max = 2 * AR5K_TUNE_CWMIN;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200949 }
950
Bruno Randolf6d91e1d2008-01-19 18:18:41 +0900951 ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
952 "beacon queueprops tqi_aifs:%d tqi_cw_min:%d tqi_cw_max:%d\n",
953 qi.tqi_aifs, qi.tqi_cw_min, qi.tqi_cw_max);
954
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300955 ret = ath5k_hw_set_tx_queueprops(ah, sc->bhalq, &qi);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200956 if (ret) {
957 ATH5K_ERR(sc, "%s: unable to update parameters for beacon "
958 "hardware queue!\n", __func__);
Bob Copelanda951ae22010-01-20 23:51:04 -0500959 goto err;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200960 }
Bob Copelanda951ae22010-01-20 23:51:04 -0500961 ret = ath5k_hw_reset_tx_queue(ah, sc->bhalq); /* push to h/w */
962 if (ret)
963 goto err;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200964
Bob Copelanda951ae22010-01-20 23:51:04 -0500965 /* reconfigure cabq with ready time to 80% of beacon_interval */
966 ret = ath5k_hw_get_tx_queueprops(ah, AR5K_TX_QUEUE_ID_CAB, &qi);
967 if (ret)
968 goto err;
969
970 qi.tqi_ready_time = (sc->bintval * 80) / 100;
971 ret = ath5k_hw_set_tx_queueprops(ah, AR5K_TX_QUEUE_ID_CAB, &qi);
972 if (ret)
973 goto err;
974
975 ret = ath5k_hw_reset_tx_queue(ah, AR5K_TX_QUEUE_ID_CAB);
976err:
977 return ret;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200978}
979
980static void
981ath5k_txq_drainq(struct ath5k_softc *sc, struct ath5k_txq *txq)
982{
983 struct ath5k_buf *bf, *bf0;
984
985 /*
986 * NB: this assumes output has been stopped and
987 * we do not need to block ath5k_tx_tasklet
988 */
989 spin_lock_bh(&txq->lock);
990 list_for_each_entry_safe(bf, bf0, &txq->q, list) {
Bruno Randolfb47f4072008-03-05 18:35:45 +0900991 ath5k_debug_printtxbuf(sc, bf);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200992
Bruno Randolf9e4e43f2010-06-16 19:11:17 +0900993 ath5k_txbuf_free_skb(sc, bf);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200994
995 spin_lock_bh(&sc->txbuflock);
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200996 list_move_tail(&bf->list, &sc->txbuf);
997 sc->txbuf_len++;
Bruno Randolf925e0b02010-09-17 11:36:35 +0900998 txq->txq_len--;
Jiri Slabyfa1c1142007-08-12 17:33:16 +0200999 spin_unlock_bh(&sc->txbuflock);
1000 }
1001 txq->link = NULL;
Bruno Randolf4edd7612010-09-17 11:36:56 +09001002 txq->txq_poll_mark = false;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001003 spin_unlock_bh(&txq->lock);
1004}
1005
1006/*
1007 * Drain the transmit queues and reclaim resources.
1008 */
1009static void
1010ath5k_txq_cleanup(struct ath5k_softc *sc)
1011{
1012 struct ath5k_hw *ah = sc->ah;
1013 unsigned int i;
1014
1015 /* XXX return value */
1016 if (likely(!test_bit(ATH_STAT_INVALID, sc->status))) {
1017 /* don't touch the hardware if marked invalid */
1018 ath5k_hw_stop_tx_dma(ah, sc->bhalq);
1019 ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "beacon queue %x\n",
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001020 ath5k_hw_get_txdp(ah, sc->bhalq));
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001021 for (i = 0; i < ARRAY_SIZE(sc->txqs); i++)
1022 if (sc->txqs[i].setup) {
1023 ath5k_hw_stop_tx_dma(ah, sc->txqs[i].qnum);
1024 ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "txq [%u] %x, "
1025 "link %p\n",
1026 sc->txqs[i].qnum,
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001027 ath5k_hw_get_txdp(ah,
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001028 sc->txqs[i].qnum),
1029 sc->txqs[i].link);
1030 }
1031 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001032
1033 for (i = 0; i < ARRAY_SIZE(sc->txqs); i++)
1034 if (sc->txqs[i].setup)
1035 ath5k_txq_drainq(sc, &sc->txqs[i]);
1036}
1037
1038static void
1039ath5k_txq_release(struct ath5k_softc *sc)
1040{
1041 struct ath5k_txq *txq = sc->txqs;
1042 unsigned int i;
1043
1044 for (i = 0; i < ARRAY_SIZE(sc->txqs); i++, txq++)
1045 if (txq->setup) {
1046 ath5k_hw_release_tx_queue(sc->ah, txq->qnum);
1047 txq->setup = false;
1048 }
1049}
1050
1051
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001052/*************\
1053* RX Handling *
1054\*************/
1055
1056/*
1057 * Enable the receive h/w following a reset.
1058 */
1059static int
1060ath5k_rx_start(struct ath5k_softc *sc)
1061{
1062 struct ath5k_hw *ah = sc->ah;
Luis R. Rodriguezdb719712009-09-10 11:20:57 -07001063 struct ath_common *common = ath5k_hw_common(ah);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001064 struct ath5k_buf *bf;
1065 int ret;
1066
Nick Kossifidisb6127982010-08-15 13:03:11 -04001067 common->rx_bufsize = roundup(IEEE80211_MAX_FRAME_LEN, common->cachelsz);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001068
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -08001069 ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "cachelsz %u rx_bufsize %u\n",
1070 common->cachelsz, common->rx_bufsize);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001071
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001072 spin_lock_bh(&sc->rxbuflock);
Bob Copeland26925042009-04-15 07:57:36 -04001073 sc->rxlink = NULL;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001074 list_for_each_entry(bf, &sc->rxbuf, list) {
1075 ret = ath5k_rxbuf_setup(sc, bf);
1076 if (ret != 0) {
1077 spin_unlock_bh(&sc->rxbuflock);
1078 goto err;
1079 }
1080 }
1081 bf = list_first_entry(&sc->rxbuf, struct ath5k_buf, list);
Bob Copeland26925042009-04-15 07:57:36 -04001082 ath5k_hw_set_rxdp(ah, bf->daddr);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001083 spin_unlock_bh(&sc->rxbuflock);
1084
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001085 ath5k_hw_start_rx_dma(ah); /* enable recv descriptors */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001086 ath5k_mode_setup(sc); /* set filters, etc. */
1087 ath5k_hw_start_rx_pcu(ah); /* re-enable PCU/DMA engine */
1088
1089 return 0;
1090err:
1091 return ret;
1092}
1093
1094/*
1095 * Disable the receive h/w in preparation for a reset.
1096 */
1097static void
1098ath5k_rx_stop(struct ath5k_softc *sc)
1099{
1100 struct ath5k_hw *ah = sc->ah;
1101
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001102 ath5k_hw_stop_rx_pcu(ah); /* disable PCU */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001103 ath5k_hw_set_rx_filter(ah, 0); /* clear recv filter */
1104 ath5k_hw_stop_rx_dma(ah); /* disable DMA engine */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001105
1106 ath5k_debug_printrxbuffs(sc, ah);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001107}
1108
1109static unsigned int
Bruno Randolf8a89f062010-06-16 19:11:51 +09001110ath5k_rx_decrypted(struct ath5k_softc *sc, struct sk_buff *skb,
1111 struct ath5k_rx_status *rs)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001112{
Luis R. Rodriguezdc1e0012009-11-04 17:47:31 -08001113 struct ath5k_hw *ah = sc->ah;
1114 struct ath_common *common = ath5k_hw_common(ah);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001115 struct ieee80211_hdr *hdr = (void *)skb->data;
Harvey Harrison798ee982008-07-15 18:44:02 -07001116 unsigned int keyix, hlen;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001117
Bruno Randolfb47f4072008-03-05 18:35:45 +09001118 if (!(rs->rs_status & AR5K_RXERR_DECRYPT) &&
1119 rs->rs_keyix != AR5K_RXKEYIX_INVALID)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001120 return RX_FLAG_DECRYPTED;
1121
1122 /* Apparently when a default key is used to decrypt the packet
1123 the hw does not set the index used to decrypt. In such cases
1124 get the index from the packet. */
Harvey Harrison798ee982008-07-15 18:44:02 -07001125 hlen = ieee80211_hdrlen(hdr->frame_control);
Harvey Harrison24b56e72008-06-14 23:33:38 -07001126 if (ieee80211_has_protected(hdr->frame_control) &&
1127 !(rs->rs_status & AR5K_RXERR_DECRYPT) &&
1128 skb->len >= hlen + 4) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001129 keyix = skb->data[hlen + 3] >> 6;
1130
Luis R. Rodriguezdc1e0012009-11-04 17:47:31 -08001131 if (test_bit(keyix, common->keymap))
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001132 return RX_FLAG_DECRYPTED;
1133 }
1134
1135 return 0;
1136}
1137
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001138
1139static void
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001140ath5k_check_ibss_tsf(struct ath5k_softc *sc, struct sk_buff *skb,
1141 struct ieee80211_rx_status *rxs)
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001142{
Luis R. Rodriguez954fece2009-09-10 10:51:33 -07001143 struct ath_common *common = ath5k_hw_common(sc->ah);
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001144 u64 tsf, bc_tstamp;
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001145 u32 hw_tu;
1146 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)skb->data;
1147
Harvey Harrison24b56e72008-06-14 23:33:38 -07001148 if (ieee80211_is_beacon(mgmt->frame_control) &&
Pavel Roskin38c07b42008-02-26 17:59:14 -05001149 le16_to_cpu(mgmt->u.beacon.capab_info) & WLAN_CAPABILITY_IBSS &&
Luis R. Rodriguez954fece2009-09-10 10:51:33 -07001150 memcmp(mgmt->bssid, common->curbssid, ETH_ALEN) == 0) {
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001151 /*
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001152 * Received an IBSS beacon with the same BSSID. Hardware *must*
1153 * have updated the local TSF. We have to work around various
1154 * hardware bugs, though...
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001155 */
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001156 tsf = ath5k_hw_get_tsf64(sc->ah);
1157 bc_tstamp = le64_to_cpu(mgmt->u.beacon.timestamp);
1158 hw_tu = TSF_TO_TU(tsf);
1159
1160 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
1161 "beacon %llx mactime %llx (diff %lld) tsf now %llx\n",
John W. Linville06501d22008-04-01 17:38:47 -04001162 (unsigned long long)bc_tstamp,
1163 (unsigned long long)rxs->mactime,
1164 (unsigned long long)(rxs->mactime - bc_tstamp),
1165 (unsigned long long)tsf);
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001166
1167 /*
1168 * Sometimes the HW will give us a wrong tstamp in the rx
1169 * status, causing the timestamp extension to go wrong.
1170 * (This seems to happen especially with beacon frames bigger
1171 * than 78 byte (incl. FCS))
1172 * But we know that the receive timestamp must be later than the
1173 * timestamp of the beacon since HW must have synced to that.
1174 *
1175 * NOTE: here we assume mactime to be after the frame was
1176 * received, not like mac80211 which defines it at the start.
1177 */
1178 if (bc_tstamp > rxs->mactime) {
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001179 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001180 "fixing mactime from %llx to %llx\n",
John W. Linville06501d22008-04-01 17:38:47 -04001181 (unsigned long long)rxs->mactime,
1182 (unsigned long long)tsf);
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001183 rxs->mactime = tsf;
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001184 }
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001185
1186 /*
1187 * Local TSF might have moved higher than our beacon timers,
1188 * in that case we have to update them to continue sending
1189 * beacons. This also takes care of synchronizing beacon sending
1190 * times with other stations.
1191 */
1192 if (hw_tu >= sc->nexttbtt)
1193 ath5k_beacon_update_timers(sc, bc_tstamp);
Bruno Randolf7f896122010-09-27 12:22:21 +09001194
1195 /* Check if the beacon timers are still correct, because a TSF
1196 * update might have created a window between them - for a
1197 * longer description see the comment of this function: */
1198 if (!ath5k_hw_check_beacon_timers(sc->ah, sc->bintval)) {
1199 ath5k_beacon_update_timers(sc, bc_tstamp);
1200 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
1201 "fixed beacon timers after beacon receive\n");
1202 }
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001203 }
1204}
1205
Bruno Randolfb4ea4492010-03-25 14:49:25 +09001206static void
1207ath5k_update_beacon_rssi(struct ath5k_softc *sc, struct sk_buff *skb, int rssi)
1208{
1209 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)skb->data;
1210 struct ath5k_hw *ah = sc->ah;
1211 struct ath_common *common = ath5k_hw_common(ah);
1212
1213 /* only beacons from our BSSID */
1214 if (!ieee80211_is_beacon(mgmt->frame_control) ||
1215 memcmp(mgmt->bssid, common->curbssid, ETH_ALEN) != 0)
1216 return;
1217
1218 ah->ah_beacon_rssi_avg = ath5k_moving_average(ah->ah_beacon_rssi_avg,
1219 rssi);
1220
1221 /* in IBSS mode we should keep RSSI statistics per neighbour */
1222 /* le16_to_cpu(mgmt->u.beacon.capab_info) & WLAN_CAPABILITY_IBSS */
1223}
1224
Benoit Papillault8127fbd2010-02-27 23:05:26 +01001225/*
Bob Copelanda180a132010-08-15 13:03:12 -04001226 * Compute padding position. skb must contain an IEEE 802.11 frame
Benoit Papillault8127fbd2010-02-27 23:05:26 +01001227 */
1228static int ath5k_common_padpos(struct sk_buff *skb)
1229{
1230 struct ieee80211_hdr * hdr = (struct ieee80211_hdr *)skb->data;
1231 __le16 frame_control = hdr->frame_control;
1232 int padpos = 24;
1233
1234 if (ieee80211_has_a4(frame_control)) {
1235 padpos += ETH_ALEN;
1236 }
1237 if (ieee80211_is_data_qos(frame_control)) {
1238 padpos += IEEE80211_QOS_CTL_LEN;
1239 }
1240
1241 return padpos;
1242}
1243
1244/*
Bob Copelanda180a132010-08-15 13:03:12 -04001245 * This function expects an 802.11 frame and returns the number of
1246 * bytes added, or -1 if we don't have enough header room.
Benoit Papillault8127fbd2010-02-27 23:05:26 +01001247 */
Benoit Papillault8127fbd2010-02-27 23:05:26 +01001248static int ath5k_add_padding(struct sk_buff *skb)
1249{
1250 int padpos = ath5k_common_padpos(skb);
1251 int padsize = padpos & 3;
1252
1253 if (padsize && skb->len>padpos) {
1254
1255 if (skb_headroom(skb) < padsize)
1256 return -1;
1257
1258 skb_push(skb, padsize);
1259 memmove(skb->data, skb->data+padsize, padpos);
1260 return padsize;
1261 }
1262
1263 return 0;
1264}
1265
1266/*
Bob Copelanda180a132010-08-15 13:03:12 -04001267 * The MAC header is padded to have 32-bit boundary if the
1268 * packet payload is non-zero. The general calculation for
1269 * padsize would take into account odd header lengths:
1270 * padsize = 4 - (hdrlen & 3); however, since only
1271 * even-length headers are used, padding can only be 0 or 2
1272 * bytes and we can optimize this a bit. We must not try to
1273 * remove padding from short control frames that do not have a
1274 * payload.
1275 *
1276 * This function expects an 802.11 frame and returns the number of
1277 * bytes removed.
Benoit Papillault8127fbd2010-02-27 23:05:26 +01001278 */
Benoit Papillault8127fbd2010-02-27 23:05:26 +01001279static int ath5k_remove_padding(struct sk_buff *skb)
1280{
1281 int padpos = ath5k_common_padpos(skb);
1282 int padsize = padpos & 3;
1283
1284 if (padsize && skb->len>=padpos+padsize) {
1285 memmove(skb->data + padsize, skb->data, padpos);
1286 skb_pull(skb, padsize);
1287 return padsize;
1288 }
1289
1290 return 0;
1291}
1292
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001293static void
Bruno Randolf8a89f062010-06-16 19:11:51 +09001294ath5k_receive_frame(struct ath5k_softc *sc, struct sk_buff *skb,
1295 struct ath5k_rx_status *rs)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001296{
Bob Copeland1c5256b2009-08-24 23:00:32 -04001297 struct ieee80211_rx_status *rxs;
Bruno Randolf8a89f062010-06-16 19:11:51 +09001298
Bruno Randolf8a89f062010-06-16 19:11:51 +09001299 ath5k_remove_padding(skb);
1300
1301 rxs = IEEE80211_SKB_RXCB(skb);
1302
1303 rxs->flag = 0;
1304 if (unlikely(rs->rs_status & AR5K_RXERR_MIC))
1305 rxs->flag |= RX_FLAG_MMIC_ERROR;
1306
1307 /*
1308 * always extend the mac timestamp, since this information is
1309 * also needed for proper IBSS merging.
1310 *
1311 * XXX: it might be too late to do it here, since rs_tstamp is
1312 * 15bit only. that means TSF extension has to be done within
1313 * 32768usec (about 32ms). it might be necessary to move this to
1314 * the interrupt handler, like it is done in madwifi.
1315 *
1316 * Unfortunately we don't know when the hardware takes the rx
1317 * timestamp (beginning of phy frame, data frame, end of rx?).
1318 * The only thing we know is that it is hardware specific...
1319 * On AR5213 it seems the rx timestamp is at the end of the
1320 * frame, but i'm not sure.
1321 *
1322 * NOTE: mac80211 defines mactime at the beginning of the first
1323 * data symbol. Since we don't have any time references it's
1324 * impossible to comply to that. This affects IBSS merge only
1325 * right now, so it's not too bad...
1326 */
1327 rxs->mactime = ath5k_extend_tsf(sc->ah, rs->rs_tstamp);
1328 rxs->flag |= RX_FLAG_TSFT;
1329
1330 rxs->freq = sc->curchan->center_freq;
1331 rxs->band = sc->curband->band;
1332
1333 rxs->signal = sc->ah->ah_noise_floor + rs->rs_rssi;
1334
1335 rxs->antenna = rs->rs_antenna;
1336
1337 if (rs->rs_antenna > 0 && rs->rs_antenna < 5)
1338 sc->stats.antenna_rx[rs->rs_antenna]++;
1339 else
1340 sc->stats.antenna_rx[0]++; /* invalid */
1341
1342 rxs->rate_idx = ath5k_hw_to_driver_rix(sc, rs->rs_rate);
1343 rxs->flag |= ath5k_rx_decrypted(sc, skb, rs);
1344
1345 if (rxs->rate_idx >= 0 && rs->rs_rate ==
1346 sc->curband->bitrates[rxs->rate_idx].hw_value_short)
1347 rxs->flag |= RX_FLAG_SHORTPRE;
1348
1349 ath5k_debug_dump_skb(sc, skb, "RX ", 0);
1350
1351 ath5k_update_beacon_rssi(sc, skb, rs->rs_rssi);
1352
1353 /* check beacons in IBSS mode */
1354 if (sc->opmode == NL80211_IFTYPE_ADHOC)
1355 ath5k_check_ibss_tsf(sc, skb, rxs);
1356
1357 ieee80211_rx(sc->hw, skb);
1358}
1359
Bruno Randolf02a78b42010-06-16 19:11:56 +09001360/** ath5k_frame_receive_ok() - Do we want to receive this frame or not?
1361 *
1362 * Check if we want to further process this frame or not. Also update
1363 * statistics. Return true if we want this frame, false if not.
1364 */
1365static bool
1366ath5k_receive_frame_ok(struct ath5k_softc *sc, struct ath5k_rx_status *rs)
1367{
1368 sc->stats.rx_all_count++;
1369
1370 if (unlikely(rs->rs_status)) {
1371 if (rs->rs_status & AR5K_RXERR_CRC)
1372 sc->stats.rxerr_crc++;
1373 if (rs->rs_status & AR5K_RXERR_FIFO)
1374 sc->stats.rxerr_fifo++;
1375 if (rs->rs_status & AR5K_RXERR_PHY) {
1376 sc->stats.rxerr_phy++;
1377 if (rs->rs_phyerr > 0 && rs->rs_phyerr < 32)
1378 sc->stats.rxerr_phy_code[rs->rs_phyerr]++;
1379 return false;
1380 }
1381 if (rs->rs_status & AR5K_RXERR_DECRYPT) {
1382 /*
1383 * Decrypt error. If the error occurred
1384 * because there was no hardware key, then
1385 * let the frame through so the upper layers
1386 * can process it. This is necessary for 5210
1387 * parts which have no way to setup a ``clear''
1388 * key cache entry.
1389 *
1390 * XXX do key cache faulting
1391 */
1392 sc->stats.rxerr_decrypt++;
1393 if (rs->rs_keyix == AR5K_RXKEYIX_INVALID &&
1394 !(rs->rs_status & AR5K_RXERR_CRC))
1395 return true;
1396 }
1397 if (rs->rs_status & AR5K_RXERR_MIC) {
1398 sc->stats.rxerr_mic++;
1399 return true;
1400 }
1401
Bob Copeland23538c22010-08-15 13:03:13 -04001402 /* reject any frames with non-crypto errors */
1403 if (rs->rs_status & ~(AR5K_RXERR_DECRYPT))
Bruno Randolf02a78b42010-06-16 19:11:56 +09001404 return false;
1405 }
1406
1407 if (unlikely(rs->rs_more)) {
1408 sc->stats.rxerr_jumbo++;
1409 return false;
1410 }
1411 return true;
1412}
1413
Bruno Randolf8a89f062010-06-16 19:11:51 +09001414static void
1415ath5k_tasklet_rx(unsigned long data)
1416{
Bruno Randolfb47f4072008-03-05 18:35:45 +09001417 struct ath5k_rx_status rs = {};
Bob Copelandb6ea0352009-01-10 14:42:54 -05001418 struct sk_buff *skb, *next_skb;
1419 dma_addr_t next_skb_addr;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001420 struct ath5k_softc *sc = (void *)data;
Luis R. Rodriguezcc861f72009-11-04 09:11:34 -08001421 struct ath5k_hw *ah = sc->ah;
1422 struct ath_common *common = ath5k_hw_common(ah);
Bob Copelandc57ca812009-04-15 07:57:35 -04001423 struct ath5k_buf *bf;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001424 struct ath5k_desc *ds;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001425 int ret;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001426
1427 spin_lock(&sc->rxbuflock);
Jiri Slaby3a0f2c82008-07-15 17:44:18 +02001428 if (list_empty(&sc->rxbuf)) {
1429 ATH5K_WARN(sc, "empty rx buf pool\n");
1430 goto unlock;
1431 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001432 do {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001433 bf = list_first_entry(&sc->rxbuf, struct ath5k_buf, list);
1434 BUG_ON(bf->skb == NULL);
1435 skb = bf->skb;
1436 ds = bf->desc;
1437
Bob Copelandc57ca812009-04-15 07:57:35 -04001438 /* bail if HW is still using self-linked descriptor */
1439 if (ath5k_hw_get_rxdp(sc->ah) == bf->daddr)
1440 break;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001441
Bruno Randolfb47f4072008-03-05 18:35:45 +09001442 ret = sc->ah->ah_proc_rx_desc(sc->ah, ds, &rs);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001443 if (unlikely(ret == -EINPROGRESS))
1444 break;
1445 else if (unlikely(ret)) {
1446 ATH5K_ERR(sc, "error in processing rx descriptor\n");
Bruno Randolf76443952010-03-09 16:56:00 +09001447 sc->stats.rxerr_proc++;
Bruno Randolfb16062f2010-06-16 19:11:46 +09001448 break;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001449 }
1450
Bruno Randolf02a78b42010-06-16 19:11:56 +09001451 if (ath5k_receive_frame_ok(sc, &rs)) {
1452 next_skb = ath5k_rx_skb_alloc(sc, &next_skb_addr);
Bruno Randolf76443952010-03-09 16:56:00 +09001453
Bruno Randolf02a78b42010-06-16 19:11:56 +09001454 /*
1455 * If we can't replace bf->skb with a new skb under
1456 * memory pressure, just skip this packet
1457 */
1458 if (!next_skb)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001459 goto next;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001460
Bruno Randolf02a78b42010-06-16 19:11:56 +09001461 pci_unmap_single(sc->pdev, bf->skbaddr,
1462 common->rx_bufsize,
1463 PCI_DMA_FROMDEVICE);
1464
1465 skb_put(skb, rs.rs_datalen);
1466
1467 ath5k_receive_frame(sc, skb, &rs);
1468
1469 bf->skb = next_skb;
1470 bf->skbaddr = next_skb_addr;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001471 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001472next:
1473 list_move_tail(&bf->list, &sc->rxbuf);
1474 } while (ath5k_rxbuf_setup(sc, bf) == 0);
Jiri Slaby3a0f2c82008-07-15 17:44:18 +02001475unlock:
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001476 spin_unlock(&sc->rxbuflock);
1477}
1478
1479
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001480/*************\
1481* TX Handling *
1482\*************/
1483
Bob Copeland8a63fac2010-09-17 12:45:07 +09001484static int ath5k_tx_queue(struct ieee80211_hw *hw, struct sk_buff *skb,
1485 struct ath5k_txq *txq)
1486{
1487 struct ath5k_softc *sc = hw->priv;
1488 struct ath5k_buf *bf;
1489 unsigned long flags;
1490 int padsize;
1491
1492 ath5k_debug_dump_skb(sc, skb, "TX ", 1);
1493
1494 /*
1495 * The hardware expects the header padded to 4 byte boundaries.
1496 * If this is not the case, we add the padding after the header.
1497 */
1498 padsize = ath5k_add_padding(skb);
1499 if (padsize < 0) {
1500 ATH5K_ERR(sc, "tx hdrlen not %%4: not enough"
1501 " headroom to pad");
1502 goto drop_packet;
1503 }
1504
Bruno Randolf925e0b02010-09-17 11:36:35 +09001505 if (txq->txq_len >= ATH5K_TXQ_LEN_MAX)
1506 ieee80211_stop_queue(hw, txq->qnum);
1507
Bob Copeland8a63fac2010-09-17 12:45:07 +09001508 spin_lock_irqsave(&sc->txbuflock, flags);
1509 if (list_empty(&sc->txbuf)) {
1510 ATH5K_ERR(sc, "no further txbuf available, dropping packet\n");
1511 spin_unlock_irqrestore(&sc->txbuflock, flags);
Bruno Randolf651d9372010-09-17 11:36:46 +09001512 ieee80211_stop_queues(hw);
Bob Copeland8a63fac2010-09-17 12:45:07 +09001513 goto drop_packet;
1514 }
1515 bf = list_first_entry(&sc->txbuf, struct ath5k_buf, list);
1516 list_del(&bf->list);
1517 sc->txbuf_len--;
1518 if (list_empty(&sc->txbuf))
1519 ieee80211_stop_queues(hw);
1520 spin_unlock_irqrestore(&sc->txbuflock, flags);
1521
1522 bf->skb = skb;
1523
1524 if (ath5k_txbuf_setup(sc, bf, txq, padsize)) {
1525 bf->skb = NULL;
1526 spin_lock_irqsave(&sc->txbuflock, flags);
1527 list_add_tail(&bf->list, &sc->txbuf);
1528 sc->txbuf_len++;
1529 spin_unlock_irqrestore(&sc->txbuflock, flags);
1530 goto drop_packet;
1531 }
1532 return NETDEV_TX_OK;
1533
1534drop_packet:
1535 dev_kfree_skb_any(skb);
1536 return NETDEV_TX_OK;
1537}
1538
Bruno Randolf14404012010-09-17 11:36:51 +09001539static void
1540ath5k_tx_frame_completed(struct ath5k_softc *sc, struct sk_buff *skb,
1541 struct ath5k_tx_status *ts)
1542{
1543 struct ieee80211_tx_info *info;
1544 int i;
1545
1546 sc->stats.tx_all_count++;
1547 info = IEEE80211_SKB_CB(skb);
1548
1549 ieee80211_tx_info_clear_status(info);
1550 for (i = 0; i < 4; i++) {
1551 struct ieee80211_tx_rate *r =
1552 &info->status.rates[i];
1553
1554 if (ts->ts_rate[i]) {
1555 r->idx = ath5k_hw_to_driver_rix(sc, ts->ts_rate[i]);
1556 r->count = ts->ts_retry[i];
1557 } else {
1558 r->idx = -1;
1559 r->count = 0;
1560 }
1561 }
1562
1563 /* count the successful attempt as well */
1564 info->status.rates[ts->ts_final_idx].count++;
1565
1566 if (unlikely(ts->ts_status)) {
1567 sc->stats.ack_fail++;
1568 if (ts->ts_status & AR5K_TXERR_FILT) {
1569 info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
1570 sc->stats.txerr_filt++;
1571 }
1572 if (ts->ts_status & AR5K_TXERR_XRETRY)
1573 sc->stats.txerr_retry++;
1574 if (ts->ts_status & AR5K_TXERR_FIFO)
1575 sc->stats.txerr_fifo++;
1576 } else {
1577 info->flags |= IEEE80211_TX_STAT_ACK;
1578 info->status.ack_signal = ts->ts_rssi;
1579 }
1580
1581 /*
1582 * Remove MAC header padding before giving the frame
1583 * back to mac80211.
1584 */
1585 ath5k_remove_padding(skb);
1586
1587 if (ts->ts_antenna > 0 && ts->ts_antenna < 5)
1588 sc->stats.antenna_tx[ts->ts_antenna]++;
1589 else
1590 sc->stats.antenna_tx[0]++; /* invalid */
1591
1592 ieee80211_tx_status(sc->hw, skb);
1593}
Bob Copeland8a63fac2010-09-17 12:45:07 +09001594
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001595static void
1596ath5k_tx_processq(struct ath5k_softc *sc, struct ath5k_txq *txq)
1597{
Bruno Randolfb47f4072008-03-05 18:35:45 +09001598 struct ath5k_tx_status ts = {};
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001599 struct ath5k_buf *bf, *bf0;
1600 struct ath5k_desc *ds;
1601 struct sk_buff *skb;
Bruno Randolf14404012010-09-17 11:36:51 +09001602 int ret;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001603
1604 spin_lock(&txq->lock);
1605 list_for_each_entry_safe(bf, bf0, &txq->q, list) {
Bruno Randolf23413292010-09-17 11:37:07 +09001606
1607 txq->txq_poll_mark = false;
1608
1609 /* skb might already have been processed last time. */
1610 if (bf->skb != NULL) {
1611 ds = bf->desc;
1612
1613 ret = sc->ah->ah_proc_tx_desc(sc->ah, ds, &ts);
1614 if (unlikely(ret == -EINPROGRESS))
1615 break;
1616 else if (unlikely(ret)) {
1617 ATH5K_ERR(sc,
1618 "error %d while processing "
1619 "queue %u\n", ret, txq->qnum);
1620 break;
1621 }
1622
1623 skb = bf->skb;
1624 bf->skb = NULL;
1625 pci_unmap_single(sc->pdev, bf->skbaddr, skb->len,
1626 PCI_DMA_TODEVICE);
1627 ath5k_tx_frame_completed(sc, skb, &ts);
1628 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001629
Bob Copelanda05988b2010-04-07 23:55:58 -04001630 /*
1631 * It's possible that the hardware can say the buffer is
1632 * completed when it hasn't yet loaded the ds_link from
Bruno Randolf23413292010-09-17 11:37:07 +09001633 * host memory and moved on.
1634 * Always keep the last descriptor to avoid HW races...
Bob Copelanda05988b2010-04-07 23:55:58 -04001635 */
Bruno Randolf23413292010-09-17 11:37:07 +09001636 if (ath5k_hw_get_txdp(sc->ah, txq->qnum) != bf->daddr) {
1637 spin_lock(&sc->txbuflock);
1638 list_move_tail(&bf->list, &sc->txbuf);
1639 sc->txbuf_len++;
1640 txq->txq_len--;
1641 spin_unlock(&sc->txbuflock);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001642 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001643 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001644 spin_unlock(&txq->lock);
Bruno Randolf925e0b02010-09-17 11:36:35 +09001645 if (txq->txq_len < ATH5K_TXQ_LEN_LOW)
1646 ieee80211_wake_queue(sc->hw, txq->qnum);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001647}
1648
1649static void
1650ath5k_tasklet_tx(unsigned long data)
1651{
Bob Copeland8784d2e2009-07-29 17:32:28 -04001652 int i;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001653 struct ath5k_softc *sc = (void *)data;
1654
Bob Copeland8784d2e2009-07-29 17:32:28 -04001655 for (i=0; i < AR5K_NUM_TX_QUEUES; i++)
1656 if (sc->txqs[i].setup && (sc->ah->ah_txq_isr & BIT(i)))
1657 ath5k_tx_processq(sc, &sc->txqs[i]);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001658}
1659
1660
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001661/*****************\
1662* Beacon handling *
1663\*****************/
1664
1665/*
1666 * Setup the beacon frame for transmit.
1667 */
1668static int
Johannes Berge039fa42008-05-15 12:55:29 +02001669ath5k_beacon_setup(struct ath5k_softc *sc, struct ath5k_buf *bf)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001670{
1671 struct sk_buff *skb = bf->skb;
Johannes Berga888d522008-05-26 16:43:39 +02001672 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001673 struct ath5k_hw *ah = sc->ah;
1674 struct ath5k_desc *ds;
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04001675 int ret = 0;
1676 u8 antenna;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001677 u32 flags;
Benoit Papillault8127fbd2010-02-27 23:05:26 +01001678 const int padsize = 0;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001679
1680 bf->skbaddr = pci_map_single(sc->pdev, skb->data, skb->len,
1681 PCI_DMA_TODEVICE);
1682 ATH5K_DBG(sc, ATH5K_DEBUG_BEACON, "skb %p [data %p len %u] "
1683 "skbaddr %llx\n", skb, skb->data, skb->len,
1684 (unsigned long long)bf->skbaddr);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -07001685 if (pci_dma_mapping_error(sc->pdev, bf->skbaddr)) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001686 ATH5K_ERR(sc, "beacon DMA mapping failed\n");
1687 return -EIO;
1688 }
1689
1690 ds = bf->desc;
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04001691 antenna = ah->ah_tx_ant;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001692
1693 flags = AR5K_TXDESC_NOACK;
Johannes Berg05c914f2008-09-11 00:01:58 +02001694 if (sc->opmode == NL80211_IFTYPE_ADHOC && ath5k_hw_hasveol(ah)) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001695 ds->ds_link = bf->daddr; /* self-linked */
1696 flags |= AR5K_TXDESC_VEOL;
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04001697 } else
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001698 ds->ds_link = 0;
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04001699
1700 /*
1701 * If we use multiple antennas on AP and use
1702 * the Sectored AP scenario, switch antenna every
1703 * 4 beacons to make sure everybody hears our AP.
1704 * When a client tries to associate, hw will keep
1705 * track of the tx antenna to be used for this client
1706 * automaticaly, based on ACKed packets.
1707 *
1708 * Note: AP still listens and transmits RTS on the
1709 * default antenna which is supposed to be an omni.
1710 *
1711 * Note2: On sectored scenarios it's possible to have
Bob Copelanda180a132010-08-15 13:03:12 -04001712 * multiple antennas (1 omni -- the default -- and 14
1713 * sectors), so if we choose to actually support this
1714 * mode, we need to allow the user to set how many antennas
1715 * we have and tweak the code below to send beacons
1716 * on all of them.
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04001717 */
1718 if (ah->ah_ant_mode == AR5K_ANTMODE_SECTOR_AP)
1719 antenna = sc->bsent & 4 ? 2 : 1;
1720
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001721
Nick Kossifidis8f655dd2009-03-15 22:20:35 +02001722 /* FIXME: If we are in g mode and rate is a CCK rate
1723 * subtract ah->ah_txpower.txp_cck_ofdm_pwr_delta
1724 * from tx power (value is in dB units already) */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001725 ds->ds_data = bf->skbaddr;
Bruno Randolf281c56d2008-02-05 18:44:55 +09001726 ret = ah->ah_setup_tx_desc(ah, ds, skb->len,
Benoit Papillault8127fbd2010-02-27 23:05:26 +01001727 ieee80211_get_hdrlen_from_skb(skb), padsize,
Luis R. Rodriguez400ec452008-02-03 21:51:49 -05001728 AR5K_PKT_TYPE_BEACON, (sc->power_level * 2),
Johannes Berge039fa42008-05-15 12:55:29 +02001729 ieee80211_get_tx_rate(sc->hw, info)->hw_value,
Johannes Berg2e92e6f2008-05-15 12:55:27 +02001730 1, AR5K_TXKEYIX_INVALID,
Luis R. Rodriguez400ec452008-02-03 21:51:49 -05001731 antenna, flags, 0, 0);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001732 if (ret)
1733 goto err_unmap;
1734
1735 return 0;
1736err_unmap:
1737 pci_unmap_single(sc->pdev, bf->skbaddr, skb->len, PCI_DMA_TODEVICE);
1738 return ret;
1739}
1740
1741/*
Bob Copeland8a63fac2010-09-17 12:45:07 +09001742 * Updates the beacon that is sent by ath5k_beacon_send. For adhoc,
1743 * this is called only once at config_bss time, for AP we do it every
1744 * SWBA interrupt so that the TIM will reflect buffered frames.
1745 *
1746 * Called with the beacon lock.
1747 */
1748static int
1749ath5k_beacon_update(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1750{
1751 int ret;
1752 struct ath5k_softc *sc = hw->priv;
1753 struct sk_buff *skb;
1754
1755 if (WARN_ON(!vif)) {
1756 ret = -EINVAL;
1757 goto out;
1758 }
1759
1760 skb = ieee80211_beacon_get(hw, vif);
1761
1762 if (!skb) {
1763 ret = -ENOMEM;
1764 goto out;
1765 }
1766
1767 ath5k_debug_dump_skb(sc, skb, "BC ", 1);
1768
1769 ath5k_txbuf_free_skb(sc, sc->bbuf);
1770 sc->bbuf->skb = skb;
1771 ret = ath5k_beacon_setup(sc, sc->bbuf);
1772 if (ret)
1773 sc->bbuf->skb = NULL;
1774out:
1775 return ret;
1776}
1777
1778/*
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001779 * Transmit a beacon frame at SWBA. Dynamic updates to the
1780 * frame contents are done as needed and the slot time is
1781 * also adjusted based on current state.
1782 *
Bob Copeland5faaff72010-07-13 11:32:40 -04001783 * This is called from software irq context (beacontq tasklets)
1784 * or user context from ath5k_beacon_config.
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001785 */
1786static void
1787ath5k_beacon_send(struct ath5k_softc *sc)
1788{
1789 struct ath5k_buf *bf = sc->bbuf;
1790 struct ath5k_hw *ah = sc->ah;
Bob Copelandcec8db22009-07-04 12:59:51 -04001791 struct sk_buff *skb;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001792
Bruno Randolfbe9b7252008-01-23 10:27:51 +09001793 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON, "in beacon_send\n");
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001794
Bob Copeland4afd89d2010-08-15 13:03:14 -04001795 if (unlikely(bf->skb == NULL || sc->opmode == NL80211_IFTYPE_STATION)) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001796 ATH5K_WARN(sc, "bf=%p bf_skb=%p\n", bf, bf ? bf->skb : NULL);
1797 return;
1798 }
1799 /*
1800 * Check if the previous beacon has gone out. If
Bob Copelanda180a132010-08-15 13:03:12 -04001801 * not, don't don't try to post another: skip this
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001802 * period and wait for the next. Missed beacons
1803 * indicate a problem and should not occur. If we
1804 * miss too many consecutive beacons reset the device.
1805 */
1806 if (unlikely(ath5k_hw_num_tx_pending(ah, sc->bhalq) != 0)) {
1807 sc->bmisscount++;
Bruno Randolfbe9b7252008-01-23 10:27:51 +09001808 ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001809 "missed %u consecutive beacons\n", sc->bmisscount);
Nick Kossifidis428cbd42009-04-30 15:55:47 -04001810 if (sc->bmisscount > 10) { /* NB: 10 is a guess */
Bruno Randolfbe9b7252008-01-23 10:27:51 +09001811 ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001812 "stuck beacon time (%u missed)\n",
1813 sc->bmisscount);
Bruno Randolf8d67a032010-06-16 19:11:12 +09001814 ATH5K_DBG(sc, ATH5K_DEBUG_RESET,
1815 "stuck beacon, resetting\n");
Bob Copeland5faaff72010-07-13 11:32:40 -04001816 ieee80211_queue_work(sc->hw, &sc->reset_work);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001817 }
1818 return;
1819 }
1820 if (unlikely(sc->bmisscount != 0)) {
Bruno Randolfbe9b7252008-01-23 10:27:51 +09001821 ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001822 "resume beacon xmit after %u misses\n",
1823 sc->bmisscount);
1824 sc->bmisscount = 0;
1825 }
1826
1827 /*
1828 * Stop any current dma and put the new frame on the queue.
1829 * This should never fail since we check above that no frames
1830 * are still pending on the queue.
1831 */
1832 if (unlikely(ath5k_hw_stop_tx_dma(ah, sc->bhalq))) {
Nick Kossifidis428cbd42009-04-30 15:55:47 -04001833 ATH5K_WARN(sc, "beacon queue %u didn't start/stop ?\n", sc->bhalq);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001834 /* NB: hw still stops DMA, so proceed */
1835 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001836
Bob Copeland1071db82009-05-18 10:59:52 -04001837 /* refresh the beacon for AP mode */
1838 if (sc->opmode == NL80211_IFTYPE_AP)
1839 ath5k_beacon_update(sc->hw, sc->vif);
1840
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001841 ath5k_hw_set_txdp(ah, sc->bhalq, bf->daddr);
1842 ath5k_hw_start_tx_dma(ah, sc->bhalq);
Bruno Randolfbe9b7252008-01-23 10:27:51 +09001843 ATH5K_DBG(sc, ATH5K_DEBUG_BEACON, "TXDP[%u] = %llx (%p)\n",
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001844 sc->bhalq, (unsigned long long)bf->daddr, bf->desc);
1845
Bob Copelandcec8db22009-07-04 12:59:51 -04001846 skb = ieee80211_get_buffered_bc(sc->hw, sc->vif);
1847 while (skb) {
1848 ath5k_tx_queue(sc->hw, skb, sc->cabq);
1849 skb = ieee80211_get_buffered_bc(sc->hw, sc->vif);
1850 }
1851
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001852 sc->bsent++;
1853}
1854
Bruno Randolf9804b982008-01-19 18:17:59 +09001855/**
1856 * ath5k_beacon_update_timers - update beacon timers
1857 *
1858 * @sc: struct ath5k_softc pointer we are operating on
1859 * @bc_tsf: the timestamp of the beacon. 0 to reset the TSF. -1 to perform a
1860 * beacon timer update based on the current HW TSF.
1861 *
1862 * Calculate the next target beacon transmit time (TBTT) based on the timestamp
1863 * of a received beacon or the current local hardware TSF and write it to the
1864 * beacon timer registers.
1865 *
1866 * This is called in a variety of situations, e.g. when a beacon is received,
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001867 * when a TSF update has been detected, but also when an new IBSS is created or
Bruno Randolf9804b982008-01-19 18:17:59 +09001868 * when we otherwise know we have to update the timers, but we keep it in this
1869 * function to have it all together in one place.
1870 */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001871static void
Bruno Randolf9804b982008-01-19 18:17:59 +09001872ath5k_beacon_update_timers(struct ath5k_softc *sc, u64 bc_tsf)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001873{
1874 struct ath5k_hw *ah = sc->ah;
Bruno Randolf9804b982008-01-19 18:17:59 +09001875 u32 nexttbtt, intval, hw_tu, bc_tu;
1876 u64 hw_tsf;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001877
1878 intval = sc->bintval & AR5K_BEACON_PERIOD;
1879 if (WARN_ON(!intval))
1880 return;
1881
Bruno Randolf9804b982008-01-19 18:17:59 +09001882 /* beacon TSF converted to TU */
1883 bc_tu = TSF_TO_TU(bc_tsf);
1884
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001885 /* current TSF converted to TU */
Bruno Randolf9804b982008-01-19 18:17:59 +09001886 hw_tsf = ath5k_hw_get_tsf64(ah);
1887 hw_tu = TSF_TO_TU(hw_tsf);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001888
Bruno Randolf9804b982008-01-19 18:17:59 +09001889#define FUDGE 3
1890 /* we use FUDGE to make sure the next TBTT is ahead of the current TU */
1891 if (bc_tsf == -1) {
1892 /*
1893 * no beacons received, called internally.
1894 * just need to refresh timers based on HW TSF.
1895 */
1896 nexttbtt = roundup(hw_tu + FUDGE, intval);
1897 } else if (bc_tsf == 0) {
1898 /*
1899 * no beacon received, probably called by ath5k_reset_tsf().
1900 * reset TSF to start with 0.
1901 */
1902 nexttbtt = intval;
1903 intval |= AR5K_BEACON_RESET_TSF;
1904 } else if (bc_tsf > hw_tsf) {
1905 /*
1906 * beacon received, SW merge happend but HW TSF not yet updated.
1907 * not possible to reconfigure timers yet, but next time we
1908 * receive a beacon with the same BSSID, the hardware will
1909 * automatically update the TSF and then we need to reconfigure
1910 * the timers.
1911 */
1912 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
1913 "need to wait for HW TSF sync\n");
1914 return;
1915 } else {
1916 /*
1917 * most important case for beacon synchronization between STA.
1918 *
1919 * beacon received and HW TSF has been already updated by HW.
1920 * update next TBTT based on the TSF of the beacon, but make
1921 * sure it is ahead of our local TSF timer.
1922 */
1923 nexttbtt = bc_tu + roundup(hw_tu + FUDGE - bc_tu, intval);
1924 }
1925#undef FUDGE
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001926
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001927 sc->nexttbtt = nexttbtt;
1928
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001929 intval |= AR5K_BEACON_ENA;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001930 ath5k_hw_init_beacon(ah, nexttbtt, intval);
Bruno Randolf9804b982008-01-19 18:17:59 +09001931
1932 /*
1933 * debugging output last in order to preserve the time critical aspect
1934 * of this function
1935 */
1936 if (bc_tsf == -1)
1937 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
1938 "reconfigured timers based on HW TSF\n");
1939 else if (bc_tsf == 0)
1940 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
1941 "reset HW TSF and timers\n");
1942 else
1943 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
1944 "updated timers based on beacon TSF\n");
1945
1946 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
David Miller04f93a82008-02-15 16:08:59 -08001947 "bc_tsf %llx hw_tsf %llx bc_tu %u hw_tu %u nexttbtt %u\n",
1948 (unsigned long long) bc_tsf,
1949 (unsigned long long) hw_tsf, bc_tu, hw_tu, nexttbtt);
Bruno Randolf9804b982008-01-19 18:17:59 +09001950 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON, "intval %u %s %s\n",
1951 intval & AR5K_BEACON_PERIOD,
1952 intval & AR5K_BEACON_ENA ? "AR5K_BEACON_ENA" : "",
1953 intval & AR5K_BEACON_RESET_TSF ? "AR5K_BEACON_RESET_TSF" : "");
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001954}
1955
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001956/**
1957 * ath5k_beacon_config - Configure the beacon queues and interrupts
1958 *
1959 * @sc: struct ath5k_softc pointer we are operating on
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001960 *
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001961 * In IBSS mode we use a self-linked tx descriptor if possible. We enable SWBA
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001962 * interrupts to detect TSF updates only.
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001963 */
1964static void
1965ath5k_beacon_config(struct ath5k_softc *sc)
1966{
1967 struct ath5k_hw *ah = sc->ah;
Bob Copelandb5f03952009-02-15 12:06:10 -05001968 unsigned long flags;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001969
Bob Copeland21800492009-07-04 12:59:52 -04001970 spin_lock_irqsave(&sc->block, flags);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001971 sc->bmisscount = 0;
Jiri Slabydc1968e2008-07-23 13:17:34 +02001972 sc->imask &= ~(AR5K_INT_BMISS | AR5K_INT_SWBA);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001973
Bob Copeland21800492009-07-04 12:59:52 -04001974 if (sc->enable_beacon) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001975 /*
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001976 * In IBSS mode we use a self-linked tx descriptor and let the
1977 * hardware send the beacons automatically. We have to load it
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001978 * only once here.
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001979 * We use the SWBA interrupt only to keep track of the beacon
Bruno Randolf6ba81c22008-03-05 18:36:26 +09001980 * timers in order to detect automatic TSF updates.
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001981 */
1982 ath5k_beaconq_config(sc);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001983
Bruno Randolf036cd1e2008-01-19 18:18:21 +09001984 sc->imask |= AR5K_INT_SWBA;
1985
Jiri Slabyda966bc2008-10-12 22:54:10 +02001986 if (sc->opmode == NL80211_IFTYPE_ADHOC) {
Bob Copeland21800492009-07-04 12:59:52 -04001987 if (ath5k_hw_hasveol(ah))
Jiri Slabyda966bc2008-10-12 22:54:10 +02001988 ath5k_beacon_send(sc);
Jiri Slabyda966bc2008-10-12 22:54:10 +02001989 } else
1990 ath5k_beacon_update_timers(sc, -1);
Bob Copeland21800492009-07-04 12:59:52 -04001991 } else {
1992 ath5k_hw_stop_tx_dma(sc->ah, sc->bhalq);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001993 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001994
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001995 ath5k_hw_set_imr(ah, sc->imask);
Bob Copeland21800492009-07-04 12:59:52 -04001996 mmiowb();
1997 spin_unlock_irqrestore(&sc->block, flags);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02001998}
1999
Nick Kossifidis428cbd42009-04-30 15:55:47 -04002000static void ath5k_tasklet_beacon(unsigned long data)
2001{
2002 struct ath5k_softc *sc = (struct ath5k_softc *) data;
2003
2004 /*
2005 * Software beacon alert--time to send a beacon.
2006 *
2007 * In IBSS mode we use this interrupt just to
2008 * keep track of the next TBTT (target beacon
2009 * transmission time) in order to detect wether
2010 * automatic TSF updates happened.
2011 */
2012 if (sc->opmode == NL80211_IFTYPE_ADHOC) {
2013 /* XXX: only if VEOL suppported */
2014 u64 tsf = ath5k_hw_get_tsf64(sc->ah);
2015 sc->nexttbtt += sc->bintval;
2016 ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
2017 "SWBA nexttbtt: %x hw_tu: %x "
2018 "TSF: %llx\n",
2019 sc->nexttbtt,
2020 TSF_TO_TU(tsf),
2021 (unsigned long long) tsf);
2022 } else {
2023 spin_lock(&sc->block);
2024 ath5k_beacon_send(sc);
2025 spin_unlock(&sc->block);
2026 }
2027}
2028
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002029
2030/********************\
2031* Interrupt handling *
2032\********************/
2033
Bruno Randolf6a8a3f62010-03-25 14:49:19 +09002034static void
2035ath5k_intr_calibration_poll(struct ath5k_hw *ah)
2036{
Bruno Randolf2111ac02010-04-02 18:44:08 +09002037 if (time_is_before_eq_jiffies(ah->ah_cal_next_ani) &&
2038 !(ah->ah_cal_mask & AR5K_CALIBRATION_FULL)) {
2039 /* run ANI only when full calibration is not active */
2040 ah->ah_cal_next_ani = jiffies +
2041 msecs_to_jiffies(ATH5K_TUNE_CALIBRATION_INTERVAL_ANI);
2042 tasklet_schedule(&ah->ah_sc->ani_tasklet);
2043
2044 } else if (time_is_before_eq_jiffies(ah->ah_cal_next_full)) {
Bruno Randolf6a8a3f62010-03-25 14:49:19 +09002045 ah->ah_cal_next_full = jiffies +
2046 msecs_to_jiffies(ATH5K_TUNE_CALIBRATION_INTERVAL_FULL);
2047 tasklet_schedule(&ah->ah_sc->calib);
2048 }
2049 /* we could use SWI to generate enough interrupts to meet our
2050 * calibration interval requirements, if necessary:
2051 * AR5K_REG_ENABLE_BITS(ah, AR5K_CR, AR5K_CR_SWI); */
2052}
2053
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002054static irqreturn_t
2055ath5k_intr(int irq, void *dev_id)
2056{
2057 struct ath5k_softc *sc = dev_id;
2058 struct ath5k_hw *ah = sc->ah;
2059 enum ath5k_int status;
2060 unsigned int counter = 1000;
2061
2062 if (unlikely(test_bit(ATH_STAT_INVALID, sc->status) ||
2063 !ath5k_hw_is_intr_pending(ah)))
2064 return IRQ_NONE;
2065
2066 do {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002067 ath5k_hw_get_isr(ah, &status); /* NB: clears IRQ too */
2068 ATH5K_DBG(sc, ATH5K_DEBUG_INTR, "status 0x%x/0x%x\n",
2069 status, sc->imask);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002070 if (unlikely(status & AR5K_INT_FATAL)) {
2071 /*
2072 * Fatal errors are unrecoverable.
2073 * Typically these are caused by DMA errors.
2074 */
Bruno Randolf8d67a032010-06-16 19:11:12 +09002075 ATH5K_DBG(sc, ATH5K_DEBUG_RESET,
2076 "fatal int, resetting\n");
Bob Copeland5faaff72010-07-13 11:32:40 -04002077 ieee80211_queue_work(sc->hw, &sc->reset_work);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002078 } else if (unlikely(status & AR5K_INT_RXORN)) {
Bruno Randolf87d77c42010-04-12 16:38:52 +09002079 /*
2080 * Receive buffers are full. Either the bus is busy or
2081 * the CPU is not fast enough to process all received
2082 * frames.
2083 * Older chipsets need a reset to come out of this
2084 * condition, but we treat it as RX for newer chips.
2085 * We don't know exactly which versions need a reset -
2086 * this guess is copied from the HAL.
2087 */
2088 sc->stats.rxorn_intr++;
Bruno Randolf8d67a032010-06-16 19:11:12 +09002089 if (ah->ah_mac_srev < AR5K_SREV_AR5212) {
2090 ATH5K_DBG(sc, ATH5K_DEBUG_RESET,
2091 "rx overrun, resetting\n");
Bob Copeland5faaff72010-07-13 11:32:40 -04002092 ieee80211_queue_work(sc->hw, &sc->reset_work);
Bruno Randolf8d67a032010-06-16 19:11:12 +09002093 }
Bruno Randolf87d77c42010-04-12 16:38:52 +09002094 else
2095 tasklet_schedule(&sc->rxtq);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002096 } else {
2097 if (status & AR5K_INT_SWBA) {
Bob Copeland56d2ac72009-04-15 07:57:33 -04002098 tasklet_hi_schedule(&sc->beacontq);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002099 }
2100 if (status & AR5K_INT_RXEOL) {
2101 /*
2102 * NB: the hardware should re-read the link when
2103 * RXE bit is written, but it doesn't work at
2104 * least on older hardware revs.
2105 */
Bruno Randolfb3f194e2010-07-14 10:53:29 +09002106 sc->stats.rxeol_intr++;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002107 }
2108 if (status & AR5K_INT_TXURN) {
2109 /* bump tx trigger level */
2110 ath5k_hw_update_tx_triglevel(ah, true);
2111 }
Nick Kossifidis4c674c62008-10-26 20:40:25 +02002112 if (status & (AR5K_INT_RXOK | AR5K_INT_RXERR))
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002113 tasklet_schedule(&sc->rxtq);
Nick Kossifidis4c674c62008-10-26 20:40:25 +02002114 if (status & (AR5K_INT_TXOK | AR5K_INT_TXDESC
2115 | AR5K_INT_TXERR | AR5K_INT_TXEOL))
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002116 tasklet_schedule(&sc->txtq);
2117 if (status & AR5K_INT_BMISS) {
Nick Kossifidis1e3e6e82009-02-09 06:15:42 +02002118 /* TODO */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002119 }
2120 if (status & AR5K_INT_MIB) {
Bruno Randolf2111ac02010-04-02 18:44:08 +09002121 sc->stats.mib_intr++;
Bruno Randolf495391d2010-03-25 14:49:36 +09002122 ath5k_hw_update_mib_counters(ah);
Bruno Randolf2111ac02010-04-02 18:44:08 +09002123 ath5k_ani_mib_intr(ah);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002124 }
Tobias Doerffele6a3b612009-06-09 17:33:27 +02002125 if (status & AR5K_INT_GPIO)
Tobias Doerffele6a3b612009-06-09 17:33:27 +02002126 tasklet_schedule(&sc->rf_kill.toggleq);
Bob Copelanda6ae0712009-06-09 23:43:11 -04002127
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002128 }
Bob Copeland2516baa2009-04-27 22:18:10 -04002129 } while (ath5k_hw_is_intr_pending(ah) && --counter > 0);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002130
2131 if (unlikely(!counter))
2132 ATH5K_WARN(sc, "too many interrupts, giving up for now\n");
2133
Bruno Randolf6a8a3f62010-03-25 14:49:19 +09002134 ath5k_intr_calibration_poll(ah);
Nick Kossifidis6e220662009-08-10 03:31:31 +03002135
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002136 return IRQ_HANDLED;
2137}
2138
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002139/*
2140 * Periodically recalibrate the PHY to account
2141 * for temperature/environment changes.
2142 */
2143static void
Nick Kossifidis6e220662009-08-10 03:31:31 +03002144ath5k_tasklet_calibrate(unsigned long data)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002145{
2146 struct ath5k_softc *sc = (void *)data;
2147 struct ath5k_hw *ah = sc->ah;
2148
Nick Kossifidis6e220662009-08-10 03:31:31 +03002149 /* Only full calibration for now */
Bruno Randolfe65e1d72010-03-25 14:49:09 +09002150 ah->ah_cal_mask |= AR5K_CALIBRATION_FULL;
Nick Kossifidis6e220662009-08-10 03:31:31 +03002151
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002152 ATH5K_DBG(sc, ATH5K_DEBUG_CALIBRATE, "channel %u/%x\n",
Luis R. Rodriguez400ec452008-02-03 21:51:49 -05002153 ieee80211_frequency_to_channel(sc->curchan->center_freq),
2154 sc->curchan->hw_value);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002155
Nick Kossifidis6f3b4142009-02-09 06:03:41 +02002156 if (ath5k_hw_gainf_calibrate(ah) == AR5K_RFGAIN_NEED_CHANGE) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002157 /*
2158 * Rfgain is out of bounds, reset the chip
2159 * to load new gain values.
2160 */
2161 ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "calibration, resetting\n");
Bob Copeland5faaff72010-07-13 11:32:40 -04002162 ieee80211_queue_work(sc->hw, &sc->reset_work);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002163 }
2164 if (ath5k_hw_phy_calibrate(ah, sc->curchan))
2165 ATH5K_ERR(sc, "calibration of channel %u failed\n",
Luis R. Rodriguez400ec452008-02-03 21:51:49 -05002166 ieee80211_frequency_to_channel(
2167 sc->curchan->center_freq));
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002168
Bruno Randolf0e8e02d2010-05-19 10:31:05 +09002169 /* Noise floor calibration interrupts rx/tx path while I/Q calibration
Bruno Randolf651d9372010-09-17 11:36:46 +09002170 * doesn't.
2171 * TODO: We should stop TX here, so that it doesn't interfere.
2172 * Note that stopping the queues is not enough to stop TX! */
Bruno Randolfafe86282010-05-19 10:31:10 +09002173 if (time_is_before_eq_jiffies(ah->ah_cal_next_nf)) {
2174 ah->ah_cal_next_nf = jiffies +
2175 msecs_to_jiffies(ATH5K_TUNE_CALIBRATION_INTERVAL_NF);
Bruno Randolfafe86282010-05-19 10:31:10 +09002176 ath5k_hw_update_noise_floor(ah);
Bruno Randolfafe86282010-05-19 10:31:10 +09002177 }
Nick Kossifidis6e220662009-08-10 03:31:31 +03002178
Bruno Randolfe65e1d72010-03-25 14:49:09 +09002179 ah->ah_cal_mask &= ~AR5K_CALIBRATION_FULL;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002180}
2181
2182
Bruno Randolf2111ac02010-04-02 18:44:08 +09002183static void
2184ath5k_tasklet_ani(unsigned long data)
2185{
2186 struct ath5k_softc *sc = (void *)data;
2187 struct ath5k_hw *ah = sc->ah;
2188
2189 ah->ah_cal_mask |= AR5K_CALIBRATION_ANI;
2190 ath5k_ani_calibration(ah);
2191 ah->ah_cal_mask &= ~AR5K_CALIBRATION_ANI;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002192}
2193
2194
Bruno Randolf4edd7612010-09-17 11:36:56 +09002195static void
2196ath5k_tx_complete_poll_work(struct work_struct *work)
2197{
2198 struct ath5k_softc *sc = container_of(work, struct ath5k_softc,
2199 tx_complete_work.work);
2200 struct ath5k_txq *txq;
2201 int i;
2202 bool needreset = false;
2203
2204 for (i = 0; i < ARRAY_SIZE(sc->txqs); i++) {
2205 if (sc->txqs[i].setup) {
2206 txq = &sc->txqs[i];
2207 spin_lock_bh(&txq->lock);
Bruno Randolf23413292010-09-17 11:37:07 +09002208 if (txq->txq_len > 1) {
Bruno Randolf4edd7612010-09-17 11:36:56 +09002209 if (txq->txq_poll_mark) {
2210 ATH5K_DBG(sc, ATH5K_DEBUG_XMIT,
2211 "TX queue stuck %d\n",
2212 txq->qnum);
2213 needreset = true;
Bruno Randolf923e5b32010-09-17 11:37:02 +09002214 txq->txq_stuck++;
Bruno Randolf4edd7612010-09-17 11:36:56 +09002215 spin_unlock_bh(&txq->lock);
2216 break;
2217 } else {
2218 txq->txq_poll_mark = true;
2219 }
2220 }
2221 spin_unlock_bh(&txq->lock);
2222 }
2223 }
2224
2225 if (needreset) {
2226 ATH5K_DBG(sc, ATH5K_DEBUG_RESET,
2227 "TX queues stuck, resetting\n");
2228 ath5k_reset(sc, sc->curchan);
2229 }
2230
2231 ieee80211_queue_delayed_work(sc->hw, &sc->tx_complete_work,
2232 msecs_to_jiffies(ATH5K_TX_COMPLETE_POLL_INT));
2233}
2234
2235
Bob Copeland8a63fac2010-09-17 12:45:07 +09002236/*************************\
2237* Initialization routines *
2238\*************************/
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002239
2240static int
Bob Copeland8a63fac2010-09-17 12:45:07 +09002241ath5k_stop_locked(struct ath5k_softc *sc)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002242{
Bob Copeland8a63fac2010-09-17 12:45:07 +09002243 struct ath5k_hw *ah = sc->ah;
Bob Copelandcec8db22009-07-04 12:59:51 -04002244
Bob Copeland8a63fac2010-09-17 12:45:07 +09002245 ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "invalid %u\n",
2246 test_bit(ATH_STAT_INVALID, sc->status));
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002247
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002248 /*
Bob Copeland8a63fac2010-09-17 12:45:07 +09002249 * Shutdown the hardware and driver:
2250 * stop output from above
2251 * disable interrupts
2252 * turn off timers
2253 * turn off the radio
2254 * clear transmit machinery
2255 * clear receive machinery
2256 * drain and release tx queues
2257 * reclaim beacon resources
2258 * power down hardware
2259 *
2260 * Note that some of this work is not possible if the
2261 * hardware is gone (invalid).
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002262 */
Bob Copeland8a63fac2010-09-17 12:45:07 +09002263 ieee80211_stop_queues(sc->hw);
2264
2265 if (!test_bit(ATH_STAT_INVALID, sc->status)) {
2266 ath5k_led_off(sc);
2267 ath5k_hw_set_imr(ah, 0);
2268 synchronize_irq(sc->pdev->irq);
2269 }
2270 ath5k_txq_cleanup(sc);
2271 if (!test_bit(ATH_STAT_INVALID, sc->status)) {
2272 ath5k_rx_stop(sc);
2273 ath5k_hw_phy_disable(ah);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002274 }
2275
Bob Copeland8a63fac2010-09-17 12:45:07 +09002276 return 0;
2277}
2278
2279static int
2280ath5k_init(struct ath5k_softc *sc)
2281{
2282 struct ath5k_hw *ah = sc->ah;
2283 struct ath_common *common = ath5k_hw_common(ah);
2284 int ret, i;
2285
2286 mutex_lock(&sc->lock);
2287
2288 ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "mode %d\n", sc->opmode);
2289
2290 /*
2291 * Stop anything previously setup. This is safe
2292 * no matter this is the first time through or not.
2293 */
2294 ath5k_stop_locked(sc);
2295
2296 /*
2297 * The basic interface to setting the hardware in a good
2298 * state is ``reset''. On return the hardware is known to
2299 * be powered up and with interrupts disabled. This must
2300 * be followed by initialization of the appropriate bits
2301 * and then setup of the interrupt mask.
2302 */
2303 sc->curchan = sc->hw->conf.channel;
2304 sc->curband = &sc->sbands[sc->curchan->band];
2305 sc->imask = AR5K_INT_RXOK | AR5K_INT_RXERR | AR5K_INT_RXEOL |
2306 AR5K_INT_RXORN | AR5K_INT_TXDESC | AR5K_INT_TXEOL |
2307 AR5K_INT_FATAL | AR5K_INT_GLOBAL | AR5K_INT_MIB;
2308
2309 ret = ath5k_reset(sc, NULL);
2310 if (ret)
2311 goto done;
2312
2313 ath5k_rfkill_hw_start(ah);
2314
2315 /*
2316 * Reset the key cache since some parts do not reset the
2317 * contents on initial power up or resume from suspend.
2318 */
2319 for (i = 0; i < common->keymax; i++)
2320 ath_hw_keyreset(common, (u16) i);
2321
2322 ath5k_hw_set_ack_bitrate_high(ah, true);
2323 ret = 0;
2324done:
2325 mmiowb();
2326 mutex_unlock(&sc->lock);
Bruno Randolf4edd7612010-09-17 11:36:56 +09002327
2328 ieee80211_queue_delayed_work(sc->hw, &sc->tx_complete_work,
2329 msecs_to_jiffies(ATH5K_TX_COMPLETE_POLL_INT));
2330
Bob Copeland8a63fac2010-09-17 12:45:07 +09002331 return ret;
2332}
2333
2334static void stop_tasklets(struct ath5k_softc *sc)
2335{
2336 tasklet_kill(&sc->rxtq);
2337 tasklet_kill(&sc->txtq);
2338 tasklet_kill(&sc->calib);
2339 tasklet_kill(&sc->beacontq);
2340 tasklet_kill(&sc->ani_tasklet);
2341}
2342
2343/*
2344 * Stop the device, grabbing the top-level lock to protect
2345 * against concurrent entry through ath5k_init (which can happen
2346 * if another thread does a system call and the thread doing the
2347 * stop is preempted).
2348 */
2349static int
2350ath5k_stop_hw(struct ath5k_softc *sc)
2351{
2352 int ret;
2353
2354 mutex_lock(&sc->lock);
2355 ret = ath5k_stop_locked(sc);
2356 if (ret == 0 && !test_bit(ATH_STAT_INVALID, sc->status)) {
2357 /*
2358 * Don't set the card in full sleep mode!
2359 *
2360 * a) When the device is in this state it must be carefully
2361 * woken up or references to registers in the PCI clock
2362 * domain may freeze the bus (and system). This varies
2363 * by chip and is mostly an issue with newer parts
2364 * (madwifi sources mentioned srev >= 0x78) that go to
2365 * sleep more quickly.
2366 *
2367 * b) On older chips full sleep results a weird behaviour
2368 * during wakeup. I tested various cards with srev < 0x78
2369 * and they don't wake up after module reload, a second
2370 * module reload is needed to bring the card up again.
2371 *
2372 * Until we figure out what's going on don't enable
2373 * full chip reset on any chip (this is what Legacy HAL
2374 * and Sam's HAL do anyway). Instead Perform a full reset
2375 * on the device (same as initial state after attach) and
2376 * leave it idle (keep MAC/BB on warm reset) */
2377 ret = ath5k_hw_on_hold(sc->ah);
2378
2379 ATH5K_DBG(sc, ATH5K_DEBUG_RESET,
2380 "putting device to sleep\n");
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002381 }
Bob Copeland8a63fac2010-09-17 12:45:07 +09002382 ath5k_txbuf_free_skb(sc, sc->bbuf);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002383
Bob Copeland8a63fac2010-09-17 12:45:07 +09002384 mmiowb();
2385 mutex_unlock(&sc->lock);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002386
Bob Copeland8a63fac2010-09-17 12:45:07 +09002387 stop_tasklets(sc);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002388
Bruno Randolf4edd7612010-09-17 11:36:56 +09002389 cancel_delayed_work_sync(&sc->tx_complete_work);
2390
Bob Copeland8a63fac2010-09-17 12:45:07 +09002391 ath5k_rfkill_hw_stop(sc->ah);
2392
2393 return ret;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002394}
2395
Bob Copeland209d8892009-05-07 08:09:08 -04002396/*
2397 * Reset the hardware. If chan is not NULL, then also pause rx/tx
2398 * and change to the given channel.
Bob Copeland5faaff72010-07-13 11:32:40 -04002399 *
2400 * This should be called with sc->lock.
Bob Copeland209d8892009-05-07 08:09:08 -04002401 */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002402static int
Bob Copeland209d8892009-05-07 08:09:08 -04002403ath5k_reset(struct ath5k_softc *sc, struct ieee80211_channel *chan)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002404{
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002405 struct ath5k_hw *ah = sc->ah;
2406 int ret;
2407
2408 ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "resetting\n");
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002409
Bob Copeland450464d2010-07-13 11:32:41 -04002410 ath5k_hw_set_imr(ah, 0);
2411 synchronize_irq(sc->pdev->irq);
2412 stop_tasklets(sc);
2413
Bob Copeland209d8892009-05-07 08:09:08 -04002414 if (chan) {
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002415 ath5k_txq_cleanup(sc);
2416 ath5k_rx_stop(sc);
Bob Copeland209d8892009-05-07 08:09:08 -04002417
2418 sc->curchan = chan;
2419 sc->curband = &sc->sbands[chan->band];
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002420 }
Bob Copeland33554432009-07-04 21:03:13 -04002421 ret = ath5k_hw_reset(ah, sc->opmode, sc->curchan, chan != NULL);
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002422 if (ret) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002423 ATH5K_ERR(sc, "can't reset hardware (%d)\n", ret);
2424 goto err;
2425 }
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002426
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002427 ret = ath5k_rx_start(sc);
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002428 if (ret) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002429 ATH5K_ERR(sc, "can't start recv logic\n");
2430 goto err;
2431 }
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002432
Bruno Randolf2111ac02010-04-02 18:44:08 +09002433 ath5k_ani_init(ah, ah->ah_sc->ani_state.ani_mode);
2434
Bruno Randolfac559522010-05-19 10:30:55 +09002435 ah->ah_cal_next_full = jiffies;
2436 ah->ah_cal_next_ani = jiffies;
Bruno Randolfafe86282010-05-19 10:31:10 +09002437 ah->ah_cal_next_nf = jiffies;
2438
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002439 /*
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002440 * Change channels and update the h/w rate map if we're switching;
2441 * e.g. 11a to 11b/g.
2442 *
2443 * We may be doing a reset in response to an ioctl that changes the
2444 * channel so update any state that might change as a result.
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002445 *
2446 * XXX needed?
2447 */
2448/* ath5k_chan_change(sc, c); */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002449
Jiri Slabyd7dc1002008-07-23 13:17:35 +02002450 ath5k_beacon_config(sc);
2451 /* intrs are enabled by ath5k_beacon_config */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002452
Bruno Randolf397f3852010-05-19 10:30:49 +09002453 ieee80211_wake_queues(sc->hw);
2454
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002455 return 0;
2456err:
2457 return ret;
2458}
2459
Bob Copeland5faaff72010-07-13 11:32:40 -04002460static void ath5k_reset_work(struct work_struct *work)
2461{
2462 struct ath5k_softc *sc = container_of(work, struct ath5k_softc,
2463 reset_work);
2464
2465 mutex_lock(&sc->lock);
2466 ath5k_reset(sc, sc->curchan);
2467 mutex_unlock(&sc->lock);
2468}
2469
Bob Copeland8a63fac2010-09-17 12:45:07 +09002470static int
2471ath5k_attach(struct pci_dev *pdev, struct ieee80211_hw *hw)
2472{
2473 struct ath5k_softc *sc = hw->priv;
2474 struct ath5k_hw *ah = sc->ah;
2475 struct ath_regulatory *regulatory = ath5k_hw_regulatory(ah);
Bruno Randolf925e0b02010-09-17 11:36:35 +09002476 struct ath5k_txq *txq;
Bob Copeland8a63fac2010-09-17 12:45:07 +09002477 u8 mac[ETH_ALEN] = {};
2478 int ret;
2479
2480 ATH5K_DBG(sc, ATH5K_DEBUG_ANY, "devid 0x%x\n", pdev->device);
2481
2482 /*
2483 * Check if the MAC has multi-rate retry support.
2484 * We do this by trying to setup a fake extended
2485 * descriptor. MACs that don't have support will
2486 * return false w/o doing anything. MACs that do
2487 * support it will return true w/o doing anything.
2488 */
2489 ret = ath5k_hw_setup_mrr_tx_desc(ah, NULL, 0, 0, 0, 0, 0, 0);
2490
2491 if (ret < 0)
2492 goto err;
2493 if (ret > 0)
2494 __set_bit(ATH_STAT_MRRETRY, sc->status);
2495
2496 /*
2497 * Collect the channel list. The 802.11 layer
2498 * is resposible for filtering this list based
2499 * on settings like the phy mode and regulatory
2500 * domain restrictions.
2501 */
2502 ret = ath5k_setup_bands(hw);
2503 if (ret) {
2504 ATH5K_ERR(sc, "can't get channels\n");
2505 goto err;
2506 }
2507
2508 /* NB: setup here so ath5k_rate_update is happy */
2509 if (test_bit(AR5K_MODE_11A, ah->ah_modes))
2510 ath5k_setcurmode(sc, AR5K_MODE_11A);
2511 else
2512 ath5k_setcurmode(sc, AR5K_MODE_11B);
2513
2514 /*
2515 * Allocate tx+rx descriptors and populate the lists.
2516 */
2517 ret = ath5k_desc_alloc(sc, pdev);
2518 if (ret) {
2519 ATH5K_ERR(sc, "can't allocate descriptors\n");
2520 goto err;
2521 }
2522
2523 /*
2524 * Allocate hardware transmit queues: one queue for
2525 * beacon frames and one data queue for each QoS
2526 * priority. Note that hw functions handle resetting
2527 * these queues at the needed time.
2528 */
2529 ret = ath5k_beaconq_setup(ah);
2530 if (ret < 0) {
2531 ATH5K_ERR(sc, "can't setup a beacon xmit queue\n");
2532 goto err_desc;
2533 }
2534 sc->bhalq = ret;
2535 sc->cabq = ath5k_txq_setup(sc, AR5K_TX_QUEUE_CAB, 0);
2536 if (IS_ERR(sc->cabq)) {
2537 ATH5K_ERR(sc, "can't setup cab queue\n");
2538 ret = PTR_ERR(sc->cabq);
2539 goto err_bhal;
2540 }
2541
Bruno Randolf925e0b02010-09-17 11:36:35 +09002542 /* This order matches mac80211's queue priority, so we can
2543 * directly use the mac80211 queue number without any mapping */
2544 txq = ath5k_txq_setup(sc, AR5K_TX_QUEUE_DATA, AR5K_WME_AC_VO);
2545 if (IS_ERR(txq)) {
Bob Copeland8a63fac2010-09-17 12:45:07 +09002546 ATH5K_ERR(sc, "can't setup xmit queue\n");
Bruno Randolf925e0b02010-09-17 11:36:35 +09002547 ret = PTR_ERR(txq);
Bob Copeland8a63fac2010-09-17 12:45:07 +09002548 goto err_queues;
2549 }
Bruno Randolf925e0b02010-09-17 11:36:35 +09002550 txq = ath5k_txq_setup(sc, AR5K_TX_QUEUE_DATA, AR5K_WME_AC_VI);
2551 if (IS_ERR(txq)) {
2552 ATH5K_ERR(sc, "can't setup xmit queue\n");
2553 ret = PTR_ERR(txq);
2554 goto err_queues;
2555 }
2556 txq = ath5k_txq_setup(sc, AR5K_TX_QUEUE_DATA, AR5K_WME_AC_BE);
2557 if (IS_ERR(txq)) {
2558 ATH5K_ERR(sc, "can't setup xmit queue\n");
2559 ret = PTR_ERR(txq);
2560 goto err_queues;
2561 }
2562 txq = ath5k_txq_setup(sc, AR5K_TX_QUEUE_DATA, AR5K_WME_AC_BK);
2563 if (IS_ERR(txq)) {
2564 ATH5K_ERR(sc, "can't setup xmit queue\n");
2565 ret = PTR_ERR(txq);
2566 goto err_queues;
2567 }
2568 hw->queues = 4;
Bob Copeland8a63fac2010-09-17 12:45:07 +09002569
2570 tasklet_init(&sc->rxtq, ath5k_tasklet_rx, (unsigned long)sc);
2571 tasklet_init(&sc->txtq, ath5k_tasklet_tx, (unsigned long)sc);
2572 tasklet_init(&sc->calib, ath5k_tasklet_calibrate, (unsigned long)sc);
2573 tasklet_init(&sc->beacontq, ath5k_tasklet_beacon, (unsigned long)sc);
2574 tasklet_init(&sc->ani_tasklet, ath5k_tasklet_ani, (unsigned long)sc);
2575
2576 INIT_WORK(&sc->reset_work, ath5k_reset_work);
Bruno Randolf4edd7612010-09-17 11:36:56 +09002577 INIT_DELAYED_WORK(&sc->tx_complete_work, ath5k_tx_complete_poll_work);
Bob Copeland8a63fac2010-09-17 12:45:07 +09002578
2579 ret = ath5k_eeprom_read_mac(ah, mac);
2580 if (ret) {
2581 ATH5K_ERR(sc, "unable to read address from EEPROM: 0x%04x\n",
2582 sc->pdev->device);
2583 goto err_queues;
2584 }
2585
2586 SET_IEEE80211_PERM_ADDR(hw, mac);
2587 /* All MAC address bits matter for ACKs */
2588 memcpy(sc->bssidmask, ath_bcast_mac, ETH_ALEN);
2589 ath5k_hw_set_bssid_mask(sc->ah, sc->bssidmask);
2590
2591 regulatory->current_rd = ah->ah_capabilities.cap_eeprom.ee_regdomain;
2592 ret = ath_regd_init(regulatory, hw->wiphy, ath5k_reg_notifier);
2593 if (ret) {
2594 ATH5K_ERR(sc, "can't initialize regulatory system\n");
2595 goto err_queues;
2596 }
2597
2598 ret = ieee80211_register_hw(hw);
2599 if (ret) {
2600 ATH5K_ERR(sc, "can't register ieee80211 hw\n");
2601 goto err_queues;
2602 }
2603
2604 if (!ath_is_world_regd(regulatory))
2605 regulatory_hint(hw->wiphy, regulatory->alpha2);
2606
2607 ath5k_init_leds(sc);
2608
2609 ath5k_sysfs_register(sc);
2610
2611 return 0;
2612err_queues:
2613 ath5k_txq_release(sc);
2614err_bhal:
2615 ath5k_hw_release_tx_queue(ah, sc->bhalq);
2616err_desc:
2617 ath5k_desc_free(sc, pdev);
2618err:
2619 return ret;
2620}
2621
2622static void
2623ath5k_detach(struct pci_dev *pdev, struct ieee80211_hw *hw)
2624{
2625 struct ath5k_softc *sc = hw->priv;
2626
2627 /*
2628 * NB: the order of these is important:
2629 * o call the 802.11 layer before detaching ath5k_hw to
2630 * ensure callbacks into the driver to delete global
2631 * key cache entries can be handled
2632 * o reclaim the tx queue data structures after calling
2633 * the 802.11 layer as we'll get called back to reclaim
2634 * node state and potentially want to use them
2635 * o to cleanup the tx queues the hal is called, so detach
2636 * it last
2637 * XXX: ??? detach ath5k_hw ???
2638 * Other than that, it's straightforward...
2639 */
2640 ieee80211_unregister_hw(hw);
2641 ath5k_desc_free(sc, pdev);
2642 ath5k_txq_release(sc);
2643 ath5k_hw_release_tx_queue(sc->ah, sc->bhalq);
2644 ath5k_unregister_leds(sc);
2645
2646 ath5k_sysfs_unregister(sc);
2647 /*
2648 * NB: can't reclaim these until after ieee80211_ifdetach
2649 * returns because we'll get called back to reclaim node
2650 * state and potentially want to use them.
2651 */
2652}
2653
2654/********************\
2655* Mac80211 functions *
2656\********************/
2657
2658static int
2659ath5k_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
2660{
2661 struct ath5k_softc *sc = hw->priv;
Bruno Randolf925e0b02010-09-17 11:36:35 +09002662 u16 qnum = skb_get_queue_mapping(skb);
Bob Copeland8a63fac2010-09-17 12:45:07 +09002663
Bruno Randolf925e0b02010-09-17 11:36:35 +09002664 if (WARN_ON(qnum >= sc->ah->ah_capabilities.cap_queues.q_tx_num)) {
2665 dev_kfree_skb_any(skb);
2666 return 0;
2667 }
2668
2669 return ath5k_tx_queue(hw, skb, &sc->txqs[qnum]);
Bob Copeland8a63fac2010-09-17 12:45:07 +09002670}
2671
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002672static int ath5k_start(struct ieee80211_hw *hw)
2673{
Bob Copelandbb2beca2009-01-19 11:20:54 -05002674 return ath5k_init(hw->priv);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002675}
2676
2677static void ath5k_stop(struct ieee80211_hw *hw)
2678{
Bob Copelandbb2beca2009-01-19 11:20:54 -05002679 ath5k_stop_hw(hw->priv);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002680}
2681
2682static int ath5k_add_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +01002683 struct ieee80211_vif *vif)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002684{
2685 struct ath5k_softc *sc = hw->priv;
2686 int ret;
2687
2688 mutex_lock(&sc->lock);
Johannes Berg32bfd352007-12-19 01:31:26 +01002689 if (sc->vif) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002690 ret = 0;
2691 goto end;
2692 }
2693
Johannes Berg1ed32e42009-12-23 13:15:45 +01002694 sc->vif = vif;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002695
Johannes Berg1ed32e42009-12-23 13:15:45 +01002696 switch (vif->type) {
Jiri Slabyda966bc2008-10-12 22:54:10 +02002697 case NL80211_IFTYPE_AP:
Johannes Berg05c914f2008-09-11 00:01:58 +02002698 case NL80211_IFTYPE_STATION:
2699 case NL80211_IFTYPE_ADHOC:
Andrey Yurovskyb706e652008-10-13 18:23:07 -07002700 case NL80211_IFTYPE_MESH_POINT:
Johannes Berg1ed32e42009-12-23 13:15:45 +01002701 sc->opmode = vif->type;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002702 break;
2703 default:
2704 ret = -EOPNOTSUPP;
2705 goto end;
2706 }
Jiri Slaby67d2e2d2008-08-18 21:45:28 +02002707
Bruno Randolfccfe5552010-03-09 16:55:38 +09002708 ATH5K_DBG(sc, ATH5K_DEBUG_MODE, "add interface mode %d\n", sc->opmode);
2709
Johannes Berg1ed32e42009-12-23 13:15:45 +01002710 ath5k_hw_set_lladdr(sc->ah, vif->addr);
Bob Copelandae6f53f2009-07-29 10:29:03 -04002711 ath5k_mode_setup(sc);
Jiri Slaby67d2e2d2008-08-18 21:45:28 +02002712
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002713 ret = 0;
2714end:
2715 mutex_unlock(&sc->lock);
2716 return ret;
2717}
2718
2719static void
2720ath5k_remove_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +01002721 struct ieee80211_vif *vif)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002722{
2723 struct ath5k_softc *sc = hw->priv;
Bob Copeland0e149cf2008-11-17 23:40:38 -05002724 u8 mac[ETH_ALEN] = {};
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002725
2726 mutex_lock(&sc->lock);
Johannes Berg1ed32e42009-12-23 13:15:45 +01002727 if (sc->vif != vif)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002728 goto end;
2729
Bob Copeland0e149cf2008-11-17 23:40:38 -05002730 ath5k_hw_set_lladdr(sc->ah, mac);
Johannes Berg32bfd352007-12-19 01:31:26 +01002731 sc->vif = NULL;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002732end:
2733 mutex_unlock(&sc->lock);
2734}
2735
Luis R. Rodriguezd8ee3982008-02-03 21:51:04 -05002736/*
2737 * TODO: Phy disable/diversity etc
2738 */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002739static int
Johannes Berge8975582008-10-09 12:18:51 +02002740ath5k_config(struct ieee80211_hw *hw, u32 changed)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002741{
2742 struct ath5k_softc *sc = hw->priv;
Nick Kossifidisa0823812009-04-30 15:55:44 -04002743 struct ath5k_hw *ah = sc->ah;
Johannes Berge8975582008-10-09 12:18:51 +02002744 struct ieee80211_conf *conf = &hw->conf;
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04002745 int ret = 0;
Bob Copelandbe009372009-01-22 08:44:16 -05002746
2747 mutex_lock(&sc->lock);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002748
Joerg Alberte30eb4a2009-08-05 01:52:07 +02002749 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
2750 ret = ath5k_chan_set(sc, conf->channel);
2751 if (ret < 0)
2752 goto unlock;
2753 }
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04002754
Nick Kossifidisa0823812009-04-30 15:55:44 -04002755 if ((changed & IEEE80211_CONF_CHANGE_POWER) &&
2756 (sc->power_level != conf->power_level)) {
2757 sc->power_level = conf->power_level;
2758
2759 /* Half dB steps */
2760 ath5k_hw_set_txpower_limit(ah, (conf->power_level * 2));
2761 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002762
Nick Kossifidis2bed03e2009-04-30 15:55:49 -04002763 /* TODO:
2764 * 1) Move this on config_interface and handle each case
2765 * separately eg. when we have only one STA vif, use
2766 * AR5K_ANTMODE_SINGLE_AP
2767 *
2768 * 2) Allow the user to change antenna mode eg. when only
2769 * one antenna is present
2770 *
2771 * 3) Allow the user to set default/tx antenna when possible
2772 *
2773 * 4) Default mode should handle 90% of the cases, together
2774 * with fixed a/b and single AP modes we should be able to
2775 * handle 99%. Sectored modes are extreme cases and i still
2776 * haven't found a usage for them. If we decide to support them,
2777 * then we must allow the user to set how many tx antennas we
2778 * have available
2779 */
Bruno Randolfcaec9112010-03-09 16:55:28 +09002780 ath5k_hw_set_antenna_mode(ah, ah->ah_ant_mode);
Bob Copelandbe009372009-01-22 08:44:16 -05002781
John W. Linville55aa4e02009-05-25 21:28:47 +02002782unlock:
Bob Copelandbe009372009-01-22 08:44:16 -05002783 mutex_unlock(&sc->lock);
John W. Linville55aa4e02009-05-25 21:28:47 +02002784 return ret;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002785}
2786
Johannes Berg3ac64be2009-08-17 16:16:53 +02002787static u64 ath5k_prepare_multicast(struct ieee80211_hw *hw,
Jiri Pirko22bedad2010-04-01 21:22:57 +00002788 struct netdev_hw_addr_list *mc_list)
Johannes Berg3ac64be2009-08-17 16:16:53 +02002789{
2790 u32 mfilt[2], val;
Johannes Berg3ac64be2009-08-17 16:16:53 +02002791 u8 pos;
Jiri Pirko22bedad2010-04-01 21:22:57 +00002792 struct netdev_hw_addr *ha;
Johannes Berg3ac64be2009-08-17 16:16:53 +02002793
2794 mfilt[0] = 0;
2795 mfilt[1] = 1;
2796
Jiri Pirko22bedad2010-04-01 21:22:57 +00002797 netdev_hw_addr_list_for_each(ha, mc_list) {
Johannes Berg3ac64be2009-08-17 16:16:53 +02002798 /* calculate XOR of eight 6-bit values */
Jiri Pirko22bedad2010-04-01 21:22:57 +00002799 val = get_unaligned_le32(ha->addr + 0);
Johannes Berg3ac64be2009-08-17 16:16:53 +02002800 pos = (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
Jiri Pirko22bedad2010-04-01 21:22:57 +00002801 val = get_unaligned_le32(ha->addr + 3);
Johannes Berg3ac64be2009-08-17 16:16:53 +02002802 pos ^= (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
2803 pos &= 0x3f;
2804 mfilt[pos / 32] |= (1 << (pos % 32));
2805 /* XXX: we might be able to just do this instead,
2806 * but not sure, needs testing, if we do use this we'd
2807 * neet to inform below to not reset the mcast */
2808 /* ath5k_hw_set_mcast_filterindex(ah,
Jiri Pirko22bedad2010-04-01 21:22:57 +00002809 * ha->addr[5]); */
Johannes Berg3ac64be2009-08-17 16:16:53 +02002810 }
2811
2812 return ((u64)(mfilt[1]) << 32) | mfilt[0];
2813}
2814
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002815#define SUPPORTED_FIF_FLAGS \
2816 FIF_PROMISC_IN_BSS | FIF_ALLMULTI | FIF_FCSFAIL | \
2817 FIF_PLCPFAIL | FIF_CONTROL | FIF_OTHER_BSS | \
2818 FIF_BCN_PRBRESP_PROMISC
2819/*
2820 * o always accept unicast, broadcast, and multicast traffic
2821 * o multicast traffic for all BSSIDs will be enabled if mac80211
2822 * says it should be
2823 * o maintain current state of phy ofdm or phy cck error reception.
2824 * If the hardware detects any of these type of errors then
2825 * ath5k_hw_get_rx_filter() will pass to us the respective
2826 * hardware filters to be able to receive these type of frames.
2827 * o probe request frames are accepted only when operating in
2828 * hostap, adhoc, or monitor modes
2829 * o enable promiscuous mode according to the interface state
2830 * o accept beacons:
2831 * - when operating in adhoc mode so the 802.11 layer creates
2832 * node table entries for peers,
2833 * - when operating in station mode for collecting rssi data when
2834 * the station is otherwise quiet, or
2835 * - when scanning
2836 */
2837static void ath5k_configure_filter(struct ieee80211_hw *hw,
2838 unsigned int changed_flags,
2839 unsigned int *new_flags,
Johannes Berg3ac64be2009-08-17 16:16:53 +02002840 u64 multicast)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002841{
2842 struct ath5k_softc *sc = hw->priv;
2843 struct ath5k_hw *ah = sc->ah;
Johannes Berg3ac64be2009-08-17 16:16:53 +02002844 u32 mfilt[2], rfilt;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002845
Bob Copeland56d1de02009-08-24 23:00:30 -04002846 mutex_lock(&sc->lock);
2847
Johannes Berg3ac64be2009-08-17 16:16:53 +02002848 mfilt[0] = multicast;
2849 mfilt[1] = multicast >> 32;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002850
2851 /* Only deal with supported flags */
2852 changed_flags &= SUPPORTED_FIF_FLAGS;
2853 *new_flags &= SUPPORTED_FIF_FLAGS;
2854
2855 /* If HW detects any phy or radar errors, leave those filters on.
2856 * Also, always enable Unicast, Broadcasts and Multicast
2857 * XXX: move unicast, bssid broadcasts and multicast to mac80211 */
2858 rfilt = (ath5k_hw_get_rx_filter(ah) & (AR5K_RX_FILTER_PHYERR)) |
2859 (AR5K_RX_FILTER_UCAST | AR5K_RX_FILTER_BCAST |
2860 AR5K_RX_FILTER_MCAST);
2861
2862 if (changed_flags & (FIF_PROMISC_IN_BSS | FIF_OTHER_BSS)) {
2863 if (*new_flags & FIF_PROMISC_IN_BSS) {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002864 __set_bit(ATH_STAT_PROMISC, sc->status);
John Daiker0bbac082008-10-17 12:16:00 -07002865 } else {
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002866 __clear_bit(ATH_STAT_PROMISC, sc->status);
John Daiker0bbac082008-10-17 12:16:00 -07002867 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002868 }
2869
Bob Copeland6b5dccc2010-06-04 08:14:14 -04002870 if (test_bit(ATH_STAT_PROMISC, sc->status))
2871 rfilt |= AR5K_RX_FILTER_PROM;
2872
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002873 /* Note, AR5K_RX_FILTER_MCAST is already enabled */
2874 if (*new_flags & FIF_ALLMULTI) {
2875 mfilt[0] = ~0;
2876 mfilt[1] = ~0;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002877 }
2878
2879 /* This is the best we can do */
2880 if (*new_flags & (FIF_FCSFAIL | FIF_PLCPFAIL))
2881 rfilt |= AR5K_RX_FILTER_PHYERR;
2882
2883 /* FIF_BCN_PRBRESP_PROMISC really means to enable beacons
Bob Copeland30bf4162010-08-15 13:03:15 -04002884 * and probes for any BSSID */
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002885 if (*new_flags & FIF_BCN_PRBRESP_PROMISC)
Bob Copeland30bf4162010-08-15 13:03:15 -04002886 rfilt |= AR5K_RX_FILTER_BEACON;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002887
2888 /* FIF_CONTROL doc says that if FIF_PROMISC_IN_BSS is not
2889 * set we should only pass on control frames for this
2890 * station. This needs testing. I believe right now this
2891 * enables *all* control frames, which is OK.. but
2892 * but we should see if we can improve on granularity */
2893 if (*new_flags & FIF_CONTROL)
2894 rfilt |= AR5K_RX_FILTER_CONTROL;
2895
2896 /* Additional settings per mode -- this is per ath5k */
2897
2898 /* XXX move these to mac80211, and add a beacon IFF flag to mac80211 */
2899
Bob Copeland56d1de02009-08-24 23:00:30 -04002900 switch (sc->opmode) {
2901 case NL80211_IFTYPE_MESH_POINT:
Bob Copeland56d1de02009-08-24 23:00:30 -04002902 rfilt |= AR5K_RX_FILTER_CONTROL |
2903 AR5K_RX_FILTER_BEACON |
2904 AR5K_RX_FILTER_PROBEREQ |
2905 AR5K_RX_FILTER_PROM;
2906 break;
2907 case NL80211_IFTYPE_AP:
2908 case NL80211_IFTYPE_ADHOC:
2909 rfilt |= AR5K_RX_FILTER_PROBEREQ |
2910 AR5K_RX_FILTER_BEACON;
2911 break;
2912 case NL80211_IFTYPE_STATION:
2913 if (sc->assoc)
2914 rfilt |= AR5K_RX_FILTER_BEACON;
2915 default:
2916 break;
2917 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002918
2919 /* Set filters */
John Daiker0bbac082008-10-17 12:16:00 -07002920 ath5k_hw_set_rx_filter(ah, rfilt);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002921
2922 /* Set multicast bits */
2923 ath5k_hw_set_mcast_filter(ah, mfilt[0], mfilt[1]);
Bob Copelanda180a132010-08-15 13:03:12 -04002924 /* Set the cached hw filter flags, this will later actually
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002925 * be set in HW */
2926 sc->filter_flags = rfilt;
Bob Copeland56d1de02009-08-24 23:00:30 -04002927
2928 mutex_unlock(&sc->lock);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002929}
2930
2931static int
2932ath5k_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Johannes Bergdc822b52008-12-29 12:55:09 +01002933 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
2934 struct ieee80211_key_conf *key)
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002935{
2936 struct ath5k_softc *sc = hw->priv;
Luis R. Rodriguezdc1e0012009-11-04 17:47:31 -08002937 struct ath5k_hw *ah = sc->ah;
2938 struct ath_common *common = ath5k_hw_common(ah);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002939 int ret = 0;
2940
Bob Copeland9ad9a262008-10-29 08:30:54 -04002941 if (modparam_nohwcrypt)
2942 return -EOPNOTSUPP;
2943
Johannes Berg97359d12010-08-10 09:46:38 +02002944 switch (key->cipher) {
2945 case WLAN_CIPHER_SUITE_WEP40:
2946 case WLAN_CIPHER_SUITE_WEP104:
2947 case WLAN_CIPHER_SUITE_TKIP:
Bob Copeland3f64b432008-10-29 23:19:14 -04002948 break;
Johannes Berg97359d12010-08-10 09:46:38 +02002949 case WLAN_CIPHER_SUITE_CCMP:
Bruno Randolf781f3132010-09-08 16:04:59 +09002950 if (common->crypt_caps & ATH_CRYPT_CAP_CIPHER_AESCCM)
Bob Copeland1c818742009-08-24 23:00:33 -04002951 break;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002952 return -EOPNOTSUPP;
2953 default:
2954 WARN_ON(1);
2955 return -EINVAL;
2956 }
2957
2958 mutex_lock(&sc->lock);
2959
2960 switch (cmd) {
2961 case SET_KEY:
Bruno Randolfe0f8c2a2010-09-08 16:04:43 +09002962 ret = ath_key_config(common, vif, sta, key);
2963 if (ret >= 0) {
2964 key->hw_key_idx = ret;
2965 /* push IV and Michael MIC generation to stack */
2966 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
2967 if (key->cipher == WLAN_CIPHER_SUITE_TKIP)
2968 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
2969 if (key->cipher == WLAN_CIPHER_SUITE_CCMP)
2970 key->flags |= IEEE80211_KEY_FLAG_SW_MGMT;
2971 ret = 0;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002972 }
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002973 break;
2974 case DISABLE_KEY:
Bruno Randolfe0f8c2a2010-09-08 16:04:43 +09002975 ath_key_delete(common, key);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002976 break;
2977 default:
2978 ret = -EINVAL;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002979 }
2980
Jiri Slaby274c7c32008-07-15 17:44:20 +02002981 mmiowb();
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002982 mutex_unlock(&sc->lock);
2983 return ret;
2984}
2985
2986static int
2987ath5k_get_stats(struct ieee80211_hw *hw,
2988 struct ieee80211_low_level_stats *stats)
2989{
2990 struct ath5k_softc *sc = hw->priv;
Nick Kossifidis194828a2008-04-16 18:49:02 +03002991
2992 /* Force update */
Bruno Randolf495391d2010-03-25 14:49:36 +09002993 ath5k_hw_update_mib_counters(sc->ah);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002994
Bruno Randolf495391d2010-03-25 14:49:36 +09002995 stats->dot11ACKFailureCount = sc->stats.ack_fail;
2996 stats->dot11RTSFailureCount = sc->stats.rts_fail;
2997 stats->dot11RTSSuccessCount = sc->stats.rts_ok;
2998 stats->dot11FCSErrorCount = sc->stats.fcs_error;
Jiri Slabyfa1c1142007-08-12 17:33:16 +02002999
3000 return 0;
3001}
3002
Holger Schurig55ee82b2010-04-19 10:24:22 +02003003static int ath5k_get_survey(struct ieee80211_hw *hw, int idx,
3004 struct survey_info *survey)
3005{
3006 struct ath5k_softc *sc = hw->priv;
3007 struct ieee80211_conf *conf = &hw->conf;
3008
3009 if (idx != 0)
3010 return -ENOENT;
3011
3012 survey->channel = conf->channel;
3013 survey->filled = SURVEY_INFO_NOISE_DBM;
3014 survey->noise = sc->ah->ah_noise_floor;
3015
3016 return 0;
3017}
3018
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003019static u64
3020ath5k_get_tsf(struct ieee80211_hw *hw)
3021{
3022 struct ath5k_softc *sc = hw->priv;
3023
3024 return ath5k_hw_get_tsf64(sc->ah);
3025}
3026
3027static void
Alina Friedrichsen3b5d6652009-01-24 07:09:59 +01003028ath5k_set_tsf(struct ieee80211_hw *hw, u64 tsf)
3029{
3030 struct ath5k_softc *sc = hw->priv;
3031
3032 ath5k_hw_set_tsf64(sc->ah, tsf);
3033}
3034
3035static void
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003036ath5k_reset_tsf(struct ieee80211_hw *hw)
3037{
3038 struct ath5k_softc *sc = hw->priv;
3039
Bruno Randolf9804b982008-01-19 18:17:59 +09003040 /*
3041 * in IBSS mode we need to update the beacon timers too.
3042 * this will also reset the TSF if we call it with 0
3043 */
Johannes Berg05c914f2008-09-11 00:01:58 +02003044 if (sc->opmode == NL80211_IFTYPE_ADHOC)
Bruno Randolf9804b982008-01-19 18:17:59 +09003045 ath5k_beacon_update_timers(sc, 0);
3046 else
3047 ath5k_hw_reset_tsf(sc->ah);
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003048}
3049
Martin Xu02969b32008-11-24 10:49:27 +08003050static void
3051set_beacon_filter(struct ieee80211_hw *hw, bool enable)
3052{
3053 struct ath5k_softc *sc = hw->priv;
3054 struct ath5k_hw *ah = sc->ah;
3055 u32 rfilt;
3056 rfilt = ath5k_hw_get_rx_filter(ah);
3057 if (enable)
3058 rfilt |= AR5K_RX_FILTER_BEACON;
3059 else
3060 rfilt &= ~AR5K_RX_FILTER_BEACON;
3061 ath5k_hw_set_rx_filter(ah, rfilt);
3062 sc->filter_flags = rfilt;
3063}
Jiri Slabyfa1c1142007-08-12 17:33:16 +02003064
Martin Xu02969b32008-11-24 10:49:27 +08003065static void ath5k_bss_info_changed(struct ieee80211_hw *hw,
3066 struct ieee80211_vif *vif,
3067 struct ieee80211_bss_conf *bss_conf,
3068 u32 changes)
3069{
3070 struct ath5k_softc *sc = hw->priv;
Johannes Berg2d0ddec2009-04-23 16:13:26 +02003071 struct ath5k_hw *ah = sc->ah;
Luis R. Rodriguez954fece2009-09-10 10:51:33 -07003072 struct ath_common *common = ath5k_hw_common(ah);
Bob Copeland21800492009-07-04 12:59:52 -04003073 unsigned long flags;
Johannes Berg2d0ddec2009-04-23 16:13:26 +02003074
3075 mutex_lock(&sc->lock);
3076 if (WARN_ON(sc->vif != vif))
3077 goto unlock;
3078
3079 if (changes & BSS_CHANGED_BSSID) {
3080 /* Cache for later use during resets */
Luis R. Rodriguez954fece2009-09-10 10:51:33 -07003081 memcpy(common->curbssid, bss_conf->bssid, ETH_ALEN);
Luis R. Rodriguez8ce54c52009-10-06 20:44:34 -04003082 common->curaid = 0;
Nick Kossifidis418de6d2010-08-15 13:03:10 -04003083 ath5k_hw_set_bssid(ah);
Johannes Berg2d0ddec2009-04-23 16:13:26 +02003084 mmiowb();
3085 }
Johannes Berg57c4d7b2009-04-23 16:10:04 +02003086
3087 if (changes & BSS_CHANGED_BEACON_INT)
3088 sc->bintval = bss_conf->beacon_int;
3089
Martin Xu02969b32008-11-24 10:49:27 +08003090 if (changes & BSS_CHANGED_ASSOC) {
Martin Xu02969b32008-11-24 10:49:27 +08003091 sc->assoc = bss_conf->assoc;
3092 if (sc->opmode == NL80211_IFTYPE_STATION)
3093 set_beacon_filter(hw, sc->assoc);
Bob Copelandf0f3d382009-06-10 22:22:21 -04003094 ath5k_hw_set_ledstate(sc->ah, sc->assoc ?
3095 AR5K_LED_ASSOC : AR5K_LED_INIT);
Luis R. Rodriguez8ce54c52009-10-06 20:44:34 -04003096 if (bss_conf->assoc) {
3097 ATH5K_DBG(sc, ATH5K_DEBUG_ANY,
3098 "Bss Info ASSOC %d, bssid: %pM\n",
3099 bss_conf->aid, common->curbssid);
3100 common->curaid = bss_conf->aid;
Nick Kossifidis418de6d2010-08-15 13:03:10 -04003101 ath5k_hw_set_bssid(ah);
Luis R. Rodriguez8ce54c52009-10-06 20:44:34 -04003102 /* Once ANI is available you would start it here */
3103 }
Martin Xu02969b32008-11-24 10:49:27 +08003104 }
Johannes Berg2d0ddec2009-04-23 16:13:26 +02003105
Bob Copeland21800492009-07-04 12:59:52 -04003106 if (changes & BSS_CHANGED_BEACON) {
3107 spin_lock_irqsave(&sc->block, flags);
3108 ath5k_beacon_update(hw, vif);
3109 spin_unlock_irqrestore(&sc->block, flags);
Johannes Berg2d0ddec2009-04-23 16:13:26 +02003110 }
3111
Bob Copeland21800492009-07-04 12:59:52 -04003112 if (changes & BSS_CHANGED_BEACON_ENABLED)
3113 sc->enable_beacon = bss_conf->enable_beacon;
3114
3115 if (changes & (BSS_CHANGED_BEACON | BSS_CHANGED_BEACON_ENABLED |
3116 BSS_CHANGED_BEACON_INT))
3117 ath5k_beacon_config(sc);
3118
Johannes Berg2d0ddec2009-04-23 16:13:26 +02003119 unlock:
3120 mutex_unlock(&sc->lock);
Martin Xu02969b32008-11-24 10:49:27 +08003121}
Bob Copelandf0f3d382009-06-10 22:22:21 -04003122
3123static void ath5k_sw_scan_start(struct ieee80211_hw *hw)
3124{
3125 struct ath5k_softc *sc = hw->priv;
3126 if (!sc->assoc)
3127 ath5k_hw_set_ledstate(sc->ah, AR5K_LED_SCAN);
3128}
3129
3130static void ath5k_sw_scan_complete(struct ieee80211_hw *hw)
3131{
3132 struct ath5k_softc *sc = hw->priv;
3133 ath5k_hw_set_ledstate(sc->ah, sc->assoc ?
3134 AR5K_LED_ASSOC : AR5K_LED_INIT);
3135}
Lukáš Turek6e08d222009-12-21 22:50:51 +01003136
3137/**
3138 * ath5k_set_coverage_class - Set IEEE 802.11 coverage class
3139 *
3140 * @hw: struct ieee80211_hw pointer
3141 * @coverage_class: IEEE 802.11 coverage class number
3142 *
3143 * Mac80211 callback. Sets slot time, ACK timeout and CTS timeout for given
3144 * coverage class. The values are persistent, they are restored after device
3145 * reset.
3146 */
3147static void ath5k_set_coverage_class(struct ieee80211_hw *hw, u8 coverage_class)
3148{
3149 struct ath5k_softc *sc = hw->priv;
3150
3151 mutex_lock(&sc->lock);
3152 ath5k_hw_set_coverage_class(sc->ah, coverage_class);
3153 mutex_unlock(&sc->lock);
3154}
Bob Copeland8a63fac2010-09-17 12:45:07 +09003155
Bruno Randolfe0b1cc52010-09-17 11:37:18 +09003156static int ath5k_conf_tx(struct ieee80211_hw *hw, u16 queue,
3157 const struct ieee80211_tx_queue_params *params)
3158{
3159 struct ath5k_softc *sc = hw->priv;
3160 struct ath5k_hw *ah = sc->ah;
3161 struct ath5k_txq_info qi;
3162 int ret = 0;
3163
3164 if (queue >= ah->ah_capabilities.cap_queues.q_tx_num)
3165 return 0;
3166
3167 mutex_lock(&sc->lock);
3168
3169 ath5k_hw_get_tx_queueprops(ah, queue, &qi);
3170
3171 qi.tqi_aifs = params->aifs;
3172 qi.tqi_cw_min = params->cw_min;
3173 qi.tqi_cw_max = params->cw_max;
3174 qi.tqi_burst_time = params->txop;
3175
3176 ATH5K_DBG(sc, ATH5K_DEBUG_ANY,
3177 "Configure tx [queue %d], "
3178 "aifs: %d, cw_min: %d, cw_max: %d, txop: %d\n",
3179 queue, params->aifs, params->cw_min,
3180 params->cw_max, params->txop);
3181
3182 if (ath5k_hw_set_tx_queueprops(ah, queue, &qi)) {
3183 ATH5K_ERR(sc,
3184 "Unable to update hardware queue %u!\n", queue);
3185 ret = -EIO;
3186 } else
3187 ath5k_hw_reset_tx_queue(ah, queue);
3188
3189 mutex_unlock(&sc->lock);
3190
3191 return ret;
3192}
3193
Bob Copeland8a63fac2010-09-17 12:45:07 +09003194static const struct ieee80211_ops ath5k_hw_ops = {
3195 .tx = ath5k_tx,
3196 .start = ath5k_start,
3197 .stop = ath5k_stop,
3198 .add_interface = ath5k_add_interface,
3199 .remove_interface = ath5k_remove_interface,
3200 .config = ath5k_config,
3201 .prepare_multicast = ath5k_prepare_multicast,
3202 .configure_filter = ath5k_configure_filter,
3203 .set_key = ath5k_set_key,
3204 .get_stats = ath5k_get_stats,
3205 .get_survey = ath5k_get_survey,
Bruno Randolfe0b1cc52010-09-17 11:37:18 +09003206 .conf_tx = ath5k_conf_tx,
Bob Copeland8a63fac2010-09-17 12:45:07 +09003207 .get_tsf = ath5k_get_tsf,
3208 .set_tsf = ath5k_set_tsf,
3209 .reset_tsf = ath5k_reset_tsf,
3210 .bss_info_changed = ath5k_bss_info_changed,
3211 .sw_scan_start = ath5k_sw_scan_start,
3212 .sw_scan_complete = ath5k_sw_scan_complete,
3213 .set_coverage_class = ath5k_set_coverage_class,
3214};
3215
3216/********************\
3217* PCI Initialization *
3218\********************/
3219
3220static int __devinit
3221ath5k_pci_probe(struct pci_dev *pdev,
3222 const struct pci_device_id *id)
3223{
3224 void __iomem *mem;
3225 struct ath5k_softc *sc;
3226 struct ath_common *common;
3227 struct ieee80211_hw *hw;
3228 int ret;
3229 u8 csz;
3230
3231 /*
3232 * L0s needs to be disabled on all ath5k cards.
3233 *
3234 * For distributions shipping with CONFIG_PCIEASPM (this will be enabled
3235 * by default in the future in 2.6.36) this will also mean both L1 and
3236 * L0s will be disabled when a pre 1.1 PCIe device is detected. We do
3237 * know L1 works correctly even for all ath5k pre 1.1 PCIe devices
3238 * though but cannot currently undue the effect of a blacklist, for
3239 * details you can read pcie_aspm_sanity_check() and see how it adjusts
3240 * the device link capability.
3241 *
3242 * It may be possible in the future to implement some PCI API to allow
3243 * drivers to override blacklists for pre 1.1 PCIe but for now it is
3244 * best to accept that both L0s and L1 will be disabled completely for
3245 * distributions shipping with CONFIG_PCIEASPM rather than having this
3246 * issue present. Motivation for adding this new API will be to help
3247 * with power consumption for some of these devices.
3248 */
3249 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S);
3250
3251 ret = pci_enable_device(pdev);
3252 if (ret) {
3253 dev_err(&pdev->dev, "can't enable device\n");
3254 goto err;
3255 }
3256
3257 /* XXX 32-bit addressing only */
3258 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
3259 if (ret) {
3260 dev_err(&pdev->dev, "32-bit DMA not available\n");
3261 goto err_dis;
3262 }
3263
3264 /*
3265 * Cache line size is used to size and align various
3266 * structures used to communicate with the hardware.
3267 */
3268 pci_read_config_byte(pdev, PCI_CACHE_LINE_SIZE, &csz);
3269 if (csz == 0) {
3270 /*
3271 * Linux 2.4.18 (at least) writes the cache line size
3272 * register as a 16-bit wide register which is wrong.
3273 * We must have this setup properly for rx buffer
3274 * DMA to work so force a reasonable value here if it
3275 * comes up zero.
3276 */
3277 csz = L1_CACHE_BYTES >> 2;
3278 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, csz);
3279 }
3280 /*
3281 * The default setting of latency timer yields poor results,
3282 * set it to the value used by other systems. It may be worth
3283 * tweaking this setting more.
3284 */
3285 pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0xa8);
3286
3287 /* Enable bus mastering */
3288 pci_set_master(pdev);
3289
3290 /*
3291 * Disable the RETRY_TIMEOUT register (0x41) to keep
3292 * PCI Tx retries from interfering with C3 CPU state.
3293 */
3294 pci_write_config_byte(pdev, 0x41, 0);
3295
3296 ret = pci_request_region(pdev, 0, "ath5k");
3297 if (ret) {
3298 dev_err(&pdev->dev, "cannot reserve PCI memory region\n");
3299 goto err_dis;
3300 }
3301
3302 mem = pci_iomap(pdev, 0, 0);
3303 if (!mem) {
3304 dev_err(&pdev->dev, "cannot remap PCI memory region\n") ;
3305 ret = -EIO;
3306 goto err_reg;
3307 }
3308
3309 /*
3310 * Allocate hw (mac80211 main struct)
3311 * and hw->priv (driver private data)
3312 */
3313 hw = ieee80211_alloc_hw(sizeof(*sc), &ath5k_hw_ops);
3314 if (hw == NULL) {
3315 dev_err(&pdev->dev, "cannot allocate ieee80211_hw\n");
3316 ret = -ENOMEM;
3317 goto err_map;
3318 }
3319
3320 dev_info(&pdev->dev, "registered as '%s'\n", wiphy_name(hw->wiphy));
3321
3322 /* Initialize driver private data */
3323 SET_IEEE80211_DEV(hw, &pdev->dev);
3324 hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
3325 IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
3326 IEEE80211_HW_SIGNAL_DBM;
3327
3328 hw->wiphy->interface_modes =
3329 BIT(NL80211_IFTYPE_AP) |
3330 BIT(NL80211_IFTYPE_STATION) |
3331 BIT(NL80211_IFTYPE_ADHOC) |
3332 BIT(NL80211_IFTYPE_MESH_POINT);
3333
3334 hw->extra_tx_headroom = 2;
3335 hw->channel_change_time = 5000;
3336 sc = hw->priv;
3337 sc->hw = hw;
3338 sc->pdev = pdev;
3339
3340 ath5k_debug_init_device(sc);
3341
3342 /*
3343 * Mark the device as detached to avoid processing
3344 * interrupts until setup is complete.
3345 */
3346 __set_bit(ATH_STAT_INVALID, sc->status);
3347
3348 sc->iobase = mem; /* So we can unmap it on detach */
3349 sc->opmode = NL80211_IFTYPE_STATION;
3350 sc->bintval = 1000;
3351 mutex_init(&sc->lock);
3352 spin_lock_init(&sc->rxbuflock);
3353 spin_lock_init(&sc->txbuflock);
3354 spin_lock_init(&sc->block);
3355
3356 /* Set private data */
3357 pci_set_drvdata(pdev, sc);
3358
3359 /* Setup interrupt handler */
3360 ret = request_irq(pdev->irq, ath5k_intr, IRQF_SHARED, "ath", sc);
3361 if (ret) {
3362 ATH5K_ERR(sc, "request_irq failed\n");
3363 goto err_free;
3364 }
3365
3366 /* If we passed the test, malloc an ath5k_hw struct */
3367 sc->ah = kzalloc(sizeof(struct ath5k_hw), GFP_KERNEL);
3368 if (!sc->ah) {
3369 ret = -ENOMEM;
3370 ATH5K_ERR(sc, "out of memory\n");
3371 goto err_irq;
3372 }
3373
3374 sc->ah->ah_sc = sc;
3375 sc->ah->ah_iobase = sc->iobase;
3376 common = ath5k_hw_common(sc->ah);
3377 common->ops = &ath5k_common_ops;
3378 common->ah = sc->ah;
3379 common->hw = hw;
3380 common->cachelsz = csz << 2; /* convert to bytes */
3381
3382 /* Initialize device */
3383 ret = ath5k_hw_attach(sc);
3384 if (ret) {
3385 goto err_free_ah;
3386 }
3387
3388 /* set up multi-rate retry capabilities */
3389 if (sc->ah->ah_version == AR5K_AR5212) {
3390 hw->max_rates = 4;
3391 hw->max_rate_tries = 11;
3392 }
3393
3394 /* Finish private driver data initialization */
3395 ret = ath5k_attach(pdev, hw);
3396 if (ret)
3397 goto err_ah;
3398
3399 ATH5K_INFO(sc, "Atheros AR%s chip found (MAC: 0x%x, PHY: 0x%x)\n",
3400 ath5k_chip_name(AR5K_VERSION_MAC, sc->ah->ah_mac_srev),
3401 sc->ah->ah_mac_srev,
3402 sc->ah->ah_phy_revision);
3403
3404 if (!sc->ah->ah_single_chip) {
3405 /* Single chip radio (!RF5111) */
3406 if (sc->ah->ah_radio_5ghz_revision &&
3407 !sc->ah->ah_radio_2ghz_revision) {
3408 /* No 5GHz support -> report 2GHz radio */
3409 if (!test_bit(AR5K_MODE_11A,
3410 sc->ah->ah_capabilities.cap_mode)) {
3411 ATH5K_INFO(sc, "RF%s 2GHz radio found (0x%x)\n",
3412 ath5k_chip_name(AR5K_VERSION_RAD,
3413 sc->ah->ah_radio_5ghz_revision),
3414 sc->ah->ah_radio_5ghz_revision);
3415 /* No 2GHz support (5110 and some
3416 * 5Ghz only cards) -> report 5Ghz radio */
3417 } else if (!test_bit(AR5K_MODE_11B,
3418 sc->ah->ah_capabilities.cap_mode)) {
3419 ATH5K_INFO(sc, "RF%s 5GHz radio found (0x%x)\n",
3420 ath5k_chip_name(AR5K_VERSION_RAD,
3421 sc->ah->ah_radio_5ghz_revision),
3422 sc->ah->ah_radio_5ghz_revision);
3423 /* Multiband radio */
3424 } else {
3425 ATH5K_INFO(sc, "RF%s multiband radio found"
3426 " (0x%x)\n",
3427 ath5k_chip_name(AR5K_VERSION_RAD,
3428 sc->ah->ah_radio_5ghz_revision),
3429 sc->ah->ah_radio_5ghz_revision);
3430 }
3431 }
3432 /* Multi chip radio (RF5111 - RF2111) ->
3433 * report both 2GHz/5GHz radios */
3434 else if (sc->ah->ah_radio_5ghz_revision &&
3435 sc->ah->ah_radio_2ghz_revision){
3436 ATH5K_INFO(sc, "RF%s 5GHz radio found (0x%x)\n",
3437 ath5k_chip_name(AR5K_VERSION_RAD,
3438 sc->ah->ah_radio_5ghz_revision),
3439 sc->ah->ah_radio_5ghz_revision);
3440 ATH5K_INFO(sc, "RF%s 2GHz radio found (0x%x)\n",
3441 ath5k_chip_name(AR5K_VERSION_RAD,
3442 sc->ah->ah_radio_2ghz_revision),
3443 sc->ah->ah_radio_2ghz_revision);
3444 }
3445 }
3446
3447
3448 /* ready to process interrupts */
3449 __clear_bit(ATH_STAT_INVALID, sc->status);
3450
3451 return 0;
3452err_ah:
3453 ath5k_hw_detach(sc->ah);
3454err_free_ah:
3455 kfree(sc->ah);
3456err_irq:
3457 free_irq(pdev->irq, sc);
3458err_free:
3459 ieee80211_free_hw(hw);
3460err_map:
3461 pci_iounmap(pdev, mem);
3462err_reg:
3463 pci_release_region(pdev, 0);
3464err_dis:
3465 pci_disable_device(pdev);
3466err:
3467 return ret;
3468}
3469
3470static void __devexit
3471ath5k_pci_remove(struct pci_dev *pdev)
3472{
3473 struct ath5k_softc *sc = pci_get_drvdata(pdev);
3474
3475 ath5k_debug_finish_device(sc);
3476 ath5k_detach(pdev, sc->hw);
3477 ath5k_hw_detach(sc->ah);
3478 kfree(sc->ah);
3479 free_irq(pdev->irq, sc);
3480 pci_iounmap(pdev, sc->iobase);
3481 pci_release_region(pdev, 0);
3482 pci_disable_device(pdev);
3483 ieee80211_free_hw(sc->hw);
3484}
3485
3486#ifdef CONFIG_PM_SLEEP
3487static int ath5k_pci_suspend(struct device *dev)
3488{
3489 struct ath5k_softc *sc = pci_get_drvdata(to_pci_dev(dev));
3490
3491 ath5k_led_off(sc);
3492 return 0;
3493}
3494
3495static int ath5k_pci_resume(struct device *dev)
3496{
3497 struct pci_dev *pdev = to_pci_dev(dev);
3498 struct ath5k_softc *sc = pci_get_drvdata(pdev);
3499
3500 /*
3501 * Suspend/Resume resets the PCI configuration space, so we have to
3502 * re-disable the RETRY_TIMEOUT register (0x41) to keep
3503 * PCI Tx retries from interfering with C3 CPU state
3504 */
3505 pci_write_config_byte(pdev, 0x41, 0);
3506
3507 ath5k_led_enable(sc);
3508 return 0;
3509}
3510
3511static SIMPLE_DEV_PM_OPS(ath5k_pm_ops, ath5k_pci_suspend, ath5k_pci_resume);
3512#define ATH5K_PM_OPS (&ath5k_pm_ops)
3513#else
3514#define ATH5K_PM_OPS NULL
3515#endif /* CONFIG_PM_SLEEP */
3516
3517static struct pci_driver ath5k_pci_driver = {
3518 .name = KBUILD_MODNAME,
3519 .id_table = ath5k_pci_id_table,
3520 .probe = ath5k_pci_probe,
3521 .remove = __devexit_p(ath5k_pci_remove),
3522 .driver.pm = ATH5K_PM_OPS,
3523};
3524
3525/*
3526 * Module init/exit functions
3527 */
3528static int __init
3529init_ath5k_pci(void)
3530{
3531 int ret;
3532
3533 ath5k_debug_init();
3534
3535 ret = pci_register_driver(&ath5k_pci_driver);
3536 if (ret) {
3537 printk(KERN_ERR "ath5k_pci: can't register pci driver\n");
3538 return ret;
3539 }
3540
3541 return 0;
3542}
3543
3544static void __exit
3545exit_ath5k_pci(void)
3546{
3547 pci_unregister_driver(&ath5k_pci_driver);
3548
3549 ath5k_debug_finish();
3550}
3551
3552module_init(init_ath5k_pci);
3553module_exit(exit_ath5k_pci);