]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/net/wireless/ath/ath5k/base.c
ath5k: Add watchdog for stuck TX queues
[net-next-2.6.git] / drivers / net / wireless / ath / ath5k / base.c
CommitLineData
fa1c114f
JS
1/*-
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
fa1c114f
JS
43#include <linux/module.h>
44#include <linux/delay.h>
274c7c36 45#include <linux/hardirq.h>
fa1c114f 46#include <linux/if.h>
274c7c36 47#include <linux/io.h>
fa1c114f
JS
48#include <linux/netdevice.h>
49#include <linux/cache.h>
50#include <linux/pci.h>
6ccf15a1 51#include <linux/pci-aspm.h>
fa1c114f
JS
52#include <linux/ethtool.h>
53#include <linux/uaccess.h>
5a0e3ad6 54#include <linux/slab.h>
fa1c114f
JS
55
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"
2111ac0d 63#include "ani.h"
fa1c114f 64
9ad9a26e 65static int modparam_nohwcrypt;
46802a4f 66module_param_named(nohwcrypt, modparam_nohwcrypt, bool, S_IRUGO);
9ad9a26e 67MODULE_PARM_DESC(nohwcrypt, "Disable hardware encryption.");
fa1c114f 68
42639fcd 69static int modparam_all_channels;
46802a4f 70module_param_named(all_channels, modparam_all_channels, bool, S_IRUGO);
42639fcd
BC
71MODULE_PARM_DESC(all_channels, "Expose all channels the device can use.");
72
fa1c114f
JS
73/* 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");
0d5f0316 79MODULE_VERSION("0.6.0 (EXPERIMENTAL)");
fa1c114f 80
8a63facc
BC
81static 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);
fa1c114f
JS
85
86/* Known PCI ids */
a3aa1884 87static DEFINE_PCI_DEVICE_TABLE(ath5k_pci_id_table) = {
97a81f5c
PR
88 { 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 */
fa1c114f
JS
106 { 0 }
107};
108MODULE_DEVICE_TABLE(pci, ath5k_pci_id_table);
109
110/* Known SREVs */
2c91108c 111static const struct ath5k_srev_name srev_names[] = {
1bef016a
NK
112 { "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 },
fa1c114f
JS
131 { "5110", AR5K_VERSION_RAD, AR5K_SREV_RAD_5110 },
132 { "5111", AR5K_VERSION_RAD, AR5K_SREV_RAD_5111 },
1bef016a 133 { "5111A", AR5K_VERSION_RAD, AR5K_SREV_RAD_5111A },
fa1c114f
JS
134 { "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 },
1bef016a 137 { "5112B", AR5K_VERSION_RAD, AR5K_SREV_RAD_5112B },
fa1c114f
JS
138 { "2112", AR5K_VERSION_RAD, AR5K_SREV_RAD_2112 },
139 { "2112A", AR5K_VERSION_RAD, AR5K_SREV_RAD_2112A },
1bef016a
NK
140 { "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 },
fa1c114f
JS
146 { "5133", AR5K_VERSION_RAD, AR5K_SREV_RAD_5133 },
147 { "xxxxx", AR5K_VERSION_RAD, AR5K_SREV_UNKNOWN },
148};
149
2c91108c 150static const struct ieee80211_rate ath5k_rates[] = {
63266a65
BR
151 { .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
9e4e43f2 192static inline void ath5k_txbuf_free_skb(struct ath5k_softc *sc,
fa1c114f
JS
193 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);
00482973 200 dev_kfree_skb_any(bf->skb);
fa1c114f 201 bf->skb = NULL;
39d63f2a
BR
202 bf->skbaddr = 0;
203 bf->desc->ds_data = 0;
fa1c114f
JS
204}
205
9e4e43f2 206static inline void ath5k_rxbuf_free_skb(struct ath5k_softc *sc,
a6c8d375
FF
207 struct ath5k_buf *bf)
208{
cc861f74
LR
209 struct ath5k_hw *ah = sc->ah;
210 struct ath_common *common = ath5k_hw_common(ah);
211
a6c8d375
FF
212 BUG_ON(!bf);
213 if (!bf->skb)
214 return;
cc861f74 215 pci_unmap_single(sc->pdev, bf->skbaddr, common->rx_bufsize,
a6c8d375
FF
216 PCI_DMA_FROMDEVICE);
217 dev_kfree_skb_any(bf->skb);
218 bf->skb = NULL;
39d63f2a
BR
219 bf->skbaddr = 0;
220 bf->desc->ds_data = 0;
a6c8d375
FF
221}
222
223
fa1c114f
JS
224static 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
fa1c114f
JS
234static 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;
75d0edb8
NK
243
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) {
fa1c114f
JS
248 name = srev_names[i].sr_name;
249 break;
250 }
251 }
252
253 return name;
254}
e5aa8474
LR
255static 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};
fa1c114f 271
8a63facc
BC
272/***********************\
273* Driver Initialization *
274\***********************/
275
276static int ath5k_reg_notifier(struct wiphy *wiphy, struct regulatory_request *request)
fa1c114f 277{
8a63facc
BC
278 struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
279 struct ath5k_softc *sc = hw->priv;
280 struct ath_regulatory *regulatory = ath5k_hw_regulatory(sc->ah);
fa1c114f 281
8a63facc
BC
282 return ath_reg_notifier_apply(wiphy, request, regulatory);
283}
6ccf15a1 284
8a63facc
BC
285/********************\
286* Channel/mode setup *
287\********************/
fa1c114f 288
8a63facc
BC
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}
fa1c114f 300
8a63facc
BC
301/*
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}
fa1c114f 314
8a63facc
BC
315static unsigned int
316ath5k_copy_channels(struct ath5k_hw *ah,
317 struct ieee80211_channel *channels,
318 unsigned int mode,
319 unsigned int max)
320{
321 unsigned int i, count, size, chfreq, freq, ch;
fa1c114f 322
8a63facc
BC
323 if (!test_bit(mode, ah->ah_modes))
324 return 0;
fa1c114f 325
8a63facc
BC
326 switch (mode) {
327 case AR5K_MODE_11A:
328 case AR5K_MODE_11A_TURBO:
329 /* 1..220, but 2GHz frequencies are filtered by check_channel */
330 size = 220 ;
331 chfreq = CHANNEL_5GHZ;
332 break;
333 case AR5K_MODE_11B:
334 case AR5K_MODE_11G:
335 case AR5K_MODE_11G_TURBO:
336 size = 26;
337 chfreq = CHANNEL_2GHZ;
338 break;
339 default:
340 ATH5K_WARN(ah->ah_sc, "bad mode, not copying channels\n");
341 return 0;
fa1c114f
JS
342 }
343
8a63facc
BC
344 for (i = 0, count = 0; i < size && max > 0; i++) {
345 ch = i + 1 ;
346 freq = ath5k_ieee2mhz(ch);
fa1c114f 347
8a63facc
BC
348 /* Check if channel is supported by the chipset */
349 if (!ath5k_channel_ok(ah, freq, chfreq))
350 continue;
f59ac048 351
8a63facc
BC
352 if (!modparam_all_channels && !ath5k_is_standard_channel(ch))
353 continue;
f59ac048 354
8a63facc
BC
355 /* Write channel info and increment counter */
356 channels[count].center_freq = freq;
357 channels[count].band = (chfreq == CHANNEL_2GHZ) ?
358 IEEE80211_BAND_2GHZ : IEEE80211_BAND_5GHZ;
359 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:
370 channels[count].hw_value = CHANNEL_B;
371 }
fa1c114f 372
8a63facc
BC
373 count++;
374 max--;
375 }
fa1c114f 376
8a63facc
BC
377 return count;
378}
fa1c114f 379
8a63facc
BC
380static void
381ath5k_setup_rate_idx(struct ath5k_softc *sc, struct ieee80211_supported_band *b)
382{
383 u8 i;
fa1c114f 384
8a63facc
BC
385 for (i = 0; i < AR5K_MAX_RATES; i++)
386 sc->rate_idx[b->band][i] = -1;
fa1c114f 387
8a63facc
BC
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;
fa1c114f 392 }
8a63facc 393}
fa1c114f 394
8a63facc
BC
395static int
396ath5k_setup_bands(struct ieee80211_hw *hw)
397{
398 struct ath5k_softc *sc = hw->priv;
399 struct ath5k_hw *ah = sc->ah;
400 struct ieee80211_supported_band *sband;
401 int max_c, count_c = 0;
402 int i;
fa1c114f 403
8a63facc
BC
404 BUILD_BUG_ON(ARRAY_SIZE(sc->sbands) < IEEE80211_NUM_BANDS);
405 max_c = ARRAY_SIZE(sc->channels);
db719718 406
8a63facc
BC
407 /* 2GHz band */
408 sband = &sc->sbands[IEEE80211_BAND_2GHZ];
409 sband->band = IEEE80211_BAND_2GHZ;
410 sband->bitrates = &sc->rates[IEEE80211_BAND_2GHZ][0];
9adca126 411
8a63facc
BC
412 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;
2f7fe870 417
8a63facc
BC
418 sband->channels = sc->channels;
419 sband->n_channels = ath5k_copy_channels(ah, sband->channels,
420 AR5K_MODE_11G, max_c);
fa1c114f 421
8a63facc
BC
422 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = sband;
423 count_c = sband->n_channels;
424 max_c -= count_c;
425 } 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;
fa1c114f 430
8a63facc
BC
431 /* 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;
fa1c114f
JS
441 }
442 }
fa1c114f 443
8a63facc
BC
444 sband->channels = sc->channels;
445 sband->n_channels = ath5k_copy_channels(ah, sband->channels,
446 AR5K_MODE_11B, max_c);
fa1c114f 447
8a63facc
BC
448 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = sband;
449 count_c = sband->n_channels;
450 max_c -= count_c;
451 }
452 ath5k_setup_rate_idx(sc, sband);
fa1c114f 453
8a63facc
BC
454 /* 5GHz band, A mode */
455 if (test_bit(AR5K_MODE_11A, sc->ah->ah_capabilities.cap_mode)) {
456 sband = &sc->sbands[IEEE80211_BAND_5GHZ];
457 sband->band = IEEE80211_BAND_5GHZ;
458 sband->bitrates = &sc->rates[IEEE80211_BAND_5GHZ][0];
fa1c114f 459
8a63facc
BC
460 memcpy(sband->bitrates, &ath5k_rates[4],
461 sizeof(struct ieee80211_rate) * 8);
462 sband->n_bitrates = 8;
fa1c114f 463
8a63facc
BC
464 sband->channels = &sc->channels[count_c];
465 sband->n_channels = ath5k_copy_channels(ah, sband->channels,
466 AR5K_MODE_11A, max_c);
fa1c114f 467
8a63facc
BC
468 hw->wiphy->bands[IEEE80211_BAND_5GHZ] = sband;
469 }
470 ath5k_setup_rate_idx(sc, sband);
471
472 ath5k_debug_dump_bands(sc);
fa1c114f 473
fa1c114f
JS
474 return 0;
475}
476
8a63facc
BC
477/*
478 * 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.
481 *
482 * Called with sc->lock.
483 */
484static int
485ath5k_chan_set(struct ath5k_softc *sc, struct ieee80211_channel *chan)
486{
487 ATH5K_DBG(sc, ATH5K_DEBUG_RESET,
488 "channel set, resetting (%u -> %u MHz)\n",
489 sc->curchan->center_freq, chan->center_freq);
490
8451d22d 491 /*
8a63facc
BC
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.
8451d22d 496 */
8a63facc 497 return ath5k_reset(sc, chan);
fa1c114f 498}
fa1c114f 499
8a63facc
BC
500static void
501ath5k_setcurmode(struct ath5k_softc *sc, unsigned int mode)
f769c36b 502{
8a63facc 503 sc->curmode = mode;
f769c36b 504
8a63facc
BC
505 if (mode == AR5K_MODE_11A) {
506 sc->curband = &sc->sbands[IEEE80211_BAND_5GHZ];
507 } else {
508 sc->curband = &sc->sbands[IEEE80211_BAND_2GHZ];
509 }
f769c36b
BC
510}
511
8a63facc
BC
512static void
513ath5k_mode_setup(struct ath5k_softc *sc)
fa1c114f 514{
fa1c114f 515 struct ath5k_hw *ah = sc->ah;
8a63facc 516 u32 rfilt;
fa1c114f 517
8a63facc
BC
518 /* configure rx filter */
519 rfilt = sc->filter_flags;
520 ath5k_hw_set_rx_filter(ah, rfilt);
fa1c114f 521
8a63facc
BC
522 if (ath5k_hw_hasbssidmask(ah))
523 ath5k_hw_set_bssid_mask(ah, sc->bssidmask);
a6668193 524
8a63facc
BC
525 /* configure operational mode */
526 ath5k_hw_set_opmode(ah, sc->opmode);
fa1c114f 527
8a63facc
BC
528 ATH5K_DBG(sc, ATH5K_DEBUG_MODE, "mode setup opmode %d\n", sc->opmode);
529 ATH5K_DBG(sc, ATH5K_DEBUG_MODE, "RX filter 0x%x\n", rfilt);
530}
fa1c114f 531
8a63facc
BC
532static inline int
533ath5k_hw_to_driver_rix(struct ath5k_softc *sc, int hw_rix)
534{
535 int rix;
fa1c114f 536
8a63facc
BC
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;
547}
548
549/***************\
550* Buffers setup *
551\***************/
552
553static
554struct sk_buff *ath5k_rx_skb_alloc(struct ath5k_softc *sc, dma_addr_t *skb_addr)
555{
556 struct ath_common *common = ath5k_hw_common(sc->ah);
557 struct sk_buff *skb;
fa1c114f
JS
558
559 /*
8a63facc
BC
560 * Allocate buffer with headroom_needed space for the
561 * fake physical layer header at the start.
fa1c114f 562 */
8a63facc
BC
563 skb = ath_rxbuf_alloc(common,
564 common->rx_bufsize,
565 GFP_ATOMIC);
fa1c114f 566
8a63facc
BC
567 if (!skb) {
568 ATH5K_ERR(sc, "can't alloc skbuff of size %u\n",
569 common->rx_bufsize);
570 return NULL;
fa1c114f
JS
571 }
572
8a63facc
BC
573 *skb_addr = pci_map_single(sc->pdev,
574 skb->data, common->rx_bufsize,
575 PCI_DMA_FROMDEVICE);
576 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;
0e149cf5 580 }
8a63facc
BC
581 return skb;
582}
0e149cf5 583
8a63facc
BC
584static 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;
590 int ret;
fa1c114f 591
8a63facc
BC
592 if (!skb) {
593 skb = ath5k_rx_skb_alloc(sc, &bf->skbaddr);
594 if (!skb)
595 return -ENOMEM;
596 bf->skb = skb;
f769c36b
BC
597 }
598
8a63facc
BC
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 *
605 * To ensure the last descriptor is self-linked we create
606 * each descriptor as self-linked and add it to the end. As
607 * each additional descriptor is added the previous self-linked
608 * entry is "fixed" naturally. This should be safe even
609 * if DMA is happening. When processing RX interrupts we
610 * never remove/process the last, self-linked, entry on the
611 * descriptor list. This ensures the hardware always has
612 * 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;
617 ret = ath5k_hw_setup_rx_desc(ah, ds, ah->common.rx_bufsize, 0);
fa1c114f 618 if (ret) {
8a63facc
BC
619 ATH5K_ERR(sc, "%s: could not setup RX desc\n", __func__);
620 return ret;
fa1c114f
JS
621 }
622
8a63facc
BC
623 if (sc->rxlink != NULL)
624 *sc->rxlink = bf->daddr;
625 sc->rxlink = &ds->ds_link;
fa1c114f 626 return 0;
fa1c114f
JS
627}
628
8a63facc 629static enum ath5k_pkt_type get_hw_packet_type(struct sk_buff *skb)
fa1c114f 630{
8a63facc
BC
631 struct ieee80211_hdr *hdr;
632 enum ath5k_pkt_type htype;
633 __le16 fc;
fa1c114f 634
8a63facc
BC
635 hdr = (struct ieee80211_hdr *)skb->data;
636 fc = hdr->frame_control;
fa1c114f 637
8a63facc
BC
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;
fa1c114f 646 else
8a63facc 647 htype = AR5K_PKT_TYPE_NORMAL;
fa1c114f 648
8a63facc 649 return htype;
42639fcd
BC
650}
651
8a63facc
BC
652static int
653ath5k_txbuf_setup(struct ath5k_softc *sc, struct ath5k_buf *bf,
654 struct ath5k_txq *txq, int padsize)
fa1c114f 655{
8a63facc
BC
656 struct ath5k_hw *ah = sc->ah;
657 struct ath5k_desc *ds = bf->desc;
658 struct sk_buff *skb = bf->skb;
659 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
660 unsigned int pktlen, flags, keyidx = AR5K_TXKEYIX_INVALID;
661 struct ieee80211_rate *rate;
662 unsigned int mrr_rate[3], mrr_tries[3];
663 int i, ret;
664 u16 hw_rate;
665 u16 cts_rate = 0;
666 u16 duration = 0;
667 u8 rc_flags;
fa1c114f 668
8a63facc 669 flags = AR5K_TXDESC_INTREQ | AR5K_TXDESC_CLRDMASK;
fa1c114f 670
8a63facc
BC
671 /* XXX endianness */
672 bf->skbaddr = pci_map_single(sc->pdev, skb->data, skb->len,
673 PCI_DMA_TODEVICE);
fa1c114f 674
8a63facc 675 rate = ieee80211_get_tx_rate(sc->hw, info);
fa1c114f 676
8a63facc
BC
677 if (info->flags & IEEE80211_TX_CTL_NO_ACK)
678 flags |= AR5K_TXDESC_NOACK;
fa1c114f 679
8a63facc
BC
680 rc_flags = info->control.rates[0].flags;
681 hw_rate = (rc_flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE) ?
682 rate->hw_value_short : rate->hw_value;
42639fcd 683
8a63facc
BC
684 pktlen = skb->len;
685
686 /* FIXME: If we are in g mode and rate is a CCK rate
687 * subtract ah->ah_txpower.txp_cck_ofdm_pwr_delta
688 * from tx power (value is in dB units already) */
689 if (info->control.hw_key) {
690 keyidx = info->control.hw_key->hw_key_idx;
691 pktlen += info->control.hw_key->icv_len;
692 }
693 if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
694 flags |= AR5K_TXDESC_RTSENA;
695 cts_rate = ieee80211_get_rts_cts_rate(sc->hw, info)->hw_value;
696 duration = le16_to_cpu(ieee80211_rts_duration(sc->hw,
697 sc->vif, pktlen, info));
698 }
699 if (rc_flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
700 flags |= AR5K_TXDESC_CTSENA;
701 cts_rate = ieee80211_get_rts_cts_rate(sc->hw, info)->hw_value;
702 duration = le16_to_cpu(ieee80211_ctstoself_duration(sc->hw,
703 sc->vif, pktlen, info));
704 }
705 ret = ah->ah_setup_tx_desc(ah, ds, pktlen,
706 ieee80211_get_hdrlen_from_skb(skb), padsize,
707 get_hw_packet_type(skb),
708 (sc->power_level * 2),
709 hw_rate,
710 info->control.rates[0].count, keyidx, ah->ah_tx_ant, flags,
711 cts_rate, duration);
712 if (ret)
713 goto err_unmap;
714
715 memset(mrr_rate, 0, sizeof(mrr_rate));
716 memset(mrr_tries, 0, sizeof(mrr_tries));
717 for (i = 0; i < 3; i++) {
718 rate = ieee80211_get_alt_retry_rate(sc->hw, info, i);
719 if (!rate)
400ec45a 720 break;
fa1c114f 721
8a63facc
BC
722 mrr_rate[i] = rate->hw_value;
723 mrr_tries[i] = info->control.rates[i + 1].count;
fa1c114f
JS
724 }
725
8a63facc
BC
726 ath5k_hw_setup_mrr_tx_desc(ah, ds,
727 mrr_rate[0], mrr_tries[0],
728 mrr_rate[1], mrr_tries[1],
729 mrr_rate[2], mrr_tries[2]);
fa1c114f 730
8a63facc
BC
731 ds->ds_link = 0;
732 ds->ds_data = bf->skbaddr;
63266a65 733
8a63facc
BC
734 spin_lock_bh(&txq->lock);
735 list_add_tail(&bf->list, &txq->q);
925e0b06 736 txq->txq_len++;
8a63facc
BC
737 if (txq->link == NULL) /* is this first packet? */
738 ath5k_hw_set_txdp(ah, txq->qnum, bf->daddr);
739 else /* no, so only link it */
740 *txq->link = bf->daddr;
63266a65 741
8a63facc
BC
742 txq->link = &ds->ds_link;
743 ath5k_hw_start_tx_dma(ah, txq->qnum);
744 mmiowb();
745 spin_unlock_bh(&txq->lock);
746
747 return 0;
748err_unmap:
749 pci_unmap_single(sc->pdev, bf->skbaddr, skb->len, PCI_DMA_TODEVICE);
750 return ret;
63266a65
BR
751}
752
8a63facc
BC
753/*******************\
754* Descriptors setup *
755\*******************/
756
d8ee398d 757static int
8a63facc 758ath5k_desc_alloc(struct ath5k_softc *sc, struct pci_dev *pdev)
fa1c114f 759{
8a63facc
BC
760 struct ath5k_desc *ds;
761 struct ath5k_buf *bf;
762 dma_addr_t da;
763 unsigned int i;
764 int ret;
d8ee398d 765
8a63facc
BC
766 /* allocate descriptors */
767 sc->desc_len = sizeof(struct ath5k_desc) *
768 (ATH_TXBUF + ATH_RXBUF + ATH_BCBUF + 1);
769 sc->desc = pci_alloc_consistent(pdev, sc->desc_len, &sc->desc_daddr);
770 if (sc->desc == NULL) {
771 ATH5K_ERR(sc, "can't allocate descriptors\n");
772 ret = -ENOMEM;
773 goto err;
774 }
775 ds = sc->desc;
776 da = sc->desc_daddr;
777 ATH5K_DBG(sc, ATH5K_DEBUG_ANY, "DMA map: %p (%zu) -> %llx\n",
778 ds, sc->desc_len, (unsigned long long)sc->desc_daddr);
fa1c114f 779
8a63facc
BC
780 bf = kcalloc(1 + ATH_TXBUF + ATH_RXBUF + ATH_BCBUF,
781 sizeof(struct ath5k_buf), GFP_KERNEL);
782 if (bf == NULL) {
783 ATH5K_ERR(sc, "can't allocate bufptr\n");
784 ret = -ENOMEM;
785 goto err_free;
786 }
787 sc->bufptr = bf;
fa1c114f 788
8a63facc
BC
789 INIT_LIST_HEAD(&sc->rxbuf);
790 for (i = 0; i < ATH_RXBUF; i++, bf++, ds++, da += sizeof(*ds)) {
791 bf->desc = ds;
792 bf->daddr = da;
793 list_add_tail(&bf->list, &sc->rxbuf);
794 }
d8ee398d 795
8a63facc
BC
796 INIT_LIST_HEAD(&sc->txbuf);
797 sc->txbuf_len = ATH_TXBUF;
798 for (i = 0; i < ATH_TXBUF; i++, bf++, ds++,
799 da += sizeof(*ds)) {
800 bf->desc = ds;
801 bf->daddr = da;
802 list_add_tail(&bf->list, &sc->txbuf);
fa1c114f
JS
803 }
804
8a63facc
BC
805 /* beacon buffer */
806 bf->desc = ds;
807 bf->daddr = da;
808 sc->bbuf = bf;
fa1c114f 809
8a63facc
BC
810 return 0;
811err_free:
812 pci_free_consistent(pdev, sc->desc_len, sc->desc, sc->desc_daddr);
813err:
814 sc->desc = NULL;
815 return ret;
816}
fa1c114f 817
8a63facc
BC
818static void
819ath5k_desc_free(struct ath5k_softc *sc, struct pci_dev *pdev)
820{
821 struct ath5k_buf *bf;
d8ee398d 822
8a63facc
BC
823 ath5k_txbuf_free_skb(sc, sc->bbuf);
824 list_for_each_entry(bf, &sc->txbuf, list)
825 ath5k_txbuf_free_skb(sc, bf);
826 list_for_each_entry(bf, &sc->rxbuf, list)
827 ath5k_rxbuf_free_skb(sc, bf);
d8ee398d 828
8a63facc
BC
829 /* Free memory associated with all descriptors */
830 pci_free_consistent(pdev, sc->desc_len, sc->desc, sc->desc_daddr);
831 sc->desc = NULL;
832 sc->desc_daddr = 0;
d8ee398d 833
8a63facc
BC
834 kfree(sc->bufptr);
835 sc->bufptr = NULL;
836 sc->bbuf = NULL;
fa1c114f
JS
837}
838
8a63facc
BC
839
840/**************\
841* Queues setup *
842\**************/
843
844static struct ath5k_txq *
845ath5k_txq_setup(struct ath5k_softc *sc,
846 int qtype, int subtype)
fa1c114f 847{
8a63facc
BC
848 struct ath5k_hw *ah = sc->ah;
849 struct ath5k_txq *txq;
850 struct ath5k_txq_info qi = {
851 .tqi_subtype = subtype,
852 .tqi_aifs = AR5K_TXQ_USEDEFAULT,
853 .tqi_cw_min = AR5K_TXQ_USEDEFAULT,
854 .tqi_cw_max = AR5K_TXQ_USEDEFAULT
855 };
856 int qnum;
d8ee398d 857
e30eb4ab 858 /*
8a63facc
BC
859 * Enable interrupts only for EOL and DESC conditions.
860 * We mark tx descriptors to receive a DESC interrupt
861 * when a tx queue gets deep; otherwise we wait for the
862 * EOL to reap descriptors. Note that this is done to
863 * reduce interrupt load and this only defers reaping
864 * descriptors, never transmitting frames. Aside from
865 * reducing interrupts this also permits more concurrency.
866 * The only potential downside is if the tx queue backs
867 * up in which case the top half of the kernel may backup
868 * due to a lack of tx descriptors.
e30eb4ab 869 */
8a63facc
BC
870 qi.tqi_flags = AR5K_TXQ_FLAG_TXEOLINT_ENABLE |
871 AR5K_TXQ_FLAG_TXDESCINT_ENABLE;
872 qnum = ath5k_hw_setup_tx_queue(ah, qtype, &qi);
873 if (qnum < 0) {
874 /*
875 * NB: don't print a message, this happens
876 * normally on parts with too few tx queues
877 */
878 return ERR_PTR(qnum);
879 }
880 if (qnum >= ARRAY_SIZE(sc->txqs)) {
881 ATH5K_ERR(sc, "hw qnum %u out of range, max %tu!\n",
882 qnum, ARRAY_SIZE(sc->txqs));
883 ath5k_hw_release_tx_queue(ah, qnum);
884 return ERR_PTR(-EINVAL);
885 }
886 txq = &sc->txqs[qnum];
887 if (!txq->setup) {
888 txq->qnum = qnum;
889 txq->link = NULL;
890 INIT_LIST_HEAD(&txq->q);
891 spin_lock_init(&txq->lock);
892 txq->setup = true;
925e0b06 893 txq->txq_len = 0;
4edd761f 894 txq->txq_poll_mark = false;
8a63facc
BC
895 }
896 return &sc->txqs[qnum];
fa1c114f
JS
897}
898
8a63facc
BC
899static int
900ath5k_beaconq_setup(struct ath5k_hw *ah)
fa1c114f 901{
8a63facc
BC
902 struct ath5k_txq_info qi = {
903 .tqi_aifs = AR5K_TXQ_USEDEFAULT,
904 .tqi_cw_min = AR5K_TXQ_USEDEFAULT,
905 .tqi_cw_max = AR5K_TXQ_USEDEFAULT,
906 /* NB: for dynamic turbo, don't enable any other interrupts */
907 .tqi_flags = AR5K_TXQ_FLAG_TXDESCINT_ENABLE
908 };
d8ee398d 909
8a63facc 910 return ath5k_hw_setup_tx_queue(ah, AR5K_TX_QUEUE_BEACON, &qi);
fa1c114f
JS
911}
912
8a63facc
BC
913static int
914ath5k_beaconq_config(struct ath5k_softc *sc)
fa1c114f
JS
915{
916 struct ath5k_hw *ah = sc->ah;
8a63facc
BC
917 struct ath5k_txq_info qi;
918 int ret;
fa1c114f 919
8a63facc
BC
920 ret = ath5k_hw_get_tx_queueprops(ah, sc->bhalq, &qi);
921 if (ret)
922 goto err;
fa1c114f 923
8a63facc
BC
924 if (sc->opmode == NL80211_IFTYPE_AP ||
925 sc->opmode == NL80211_IFTYPE_MESH_POINT) {
926 /*
927 * Always burst out beacon and CAB traffic
928 * (aifs = cwmin = cwmax = 0)
929 */
930 qi.tqi_aifs = 0;
931 qi.tqi_cw_min = 0;
932 qi.tqi_cw_max = 0;
933 } else if (sc->opmode == NL80211_IFTYPE_ADHOC) {
934 /*
935 * Adhoc mode; backoff between 0 and (2 * cw_min).
936 */
937 qi.tqi_aifs = 0;
938 qi.tqi_cw_min = 0;
939 qi.tqi_cw_max = 2 * ah->ah_cw_min;
940 }
fa1c114f 941
8a63facc
BC
942 ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
943 "beacon queueprops tqi_aifs:%d tqi_cw_min:%d tqi_cw_max:%d\n",
944 qi.tqi_aifs, qi.tqi_cw_min, qi.tqi_cw_max);
fa1c114f 945
8a63facc
BC
946 ret = ath5k_hw_set_tx_queueprops(ah, sc->bhalq, &qi);
947 if (ret) {
948 ATH5K_ERR(sc, "%s: unable to update parameters for beacon "
949 "hardware queue!\n", __func__);
950 goto err;
951 }
952 ret = ath5k_hw_reset_tx_queue(ah, sc->bhalq); /* push to h/w */
953 if (ret)
954 goto err;
b7266047 955
8a63facc
BC
956 /* reconfigure cabq with ready time to 80% of beacon_interval */
957 ret = ath5k_hw_get_tx_queueprops(ah, AR5K_TX_QUEUE_ID_CAB, &qi);
958 if (ret)
959 goto err;
b7266047 960
8a63facc
BC
961 qi.tqi_ready_time = (sc->bintval * 80) / 100;
962 ret = ath5k_hw_set_tx_queueprops(ah, AR5K_TX_QUEUE_ID_CAB, &qi);
963 if (ret)
964 goto err;
b7266047 965
8a63facc
BC
966 ret = ath5k_hw_reset_tx_queue(ah, AR5K_TX_QUEUE_ID_CAB);
967err:
968 return ret;
d8ee398d
LR
969}
970
8a63facc
BC
971static void
972ath5k_txq_drainq(struct ath5k_softc *sc, struct ath5k_txq *txq)
973{
974 struct ath5k_buf *bf, *bf0;
b6ea0356
BC
975
976 /*
8a63facc
BC
977 * NB: this assumes output has been stopped and
978 * we do not need to block ath5k_tx_tasklet
b6ea0356 979 */
8a63facc
BC
980 spin_lock_bh(&txq->lock);
981 list_for_each_entry_safe(bf, bf0, &txq->q, list) {
982 ath5k_debug_printtxbuf(sc, bf);
b6ea0356 983
8a63facc 984 ath5k_txbuf_free_skb(sc, bf);
b6ea0356 985
8a63facc
BC
986 spin_lock_bh(&sc->txbuflock);
987 list_move_tail(&bf->list, &sc->txbuf);
988 sc->txbuf_len++;
925e0b06 989 txq->txq_len--;
8a63facc 990 spin_unlock_bh(&sc->txbuflock);
b6ea0356 991 }
8a63facc 992 txq->link = NULL;
4edd761f 993 txq->txq_poll_mark = false;
8a63facc 994 spin_unlock_bh(&txq->lock);
b6ea0356
BC
995}
996
8a63facc
BC
997/*
998 * Drain the transmit queues and reclaim resources.
999 */
1000static void
1001ath5k_txq_cleanup(struct ath5k_softc *sc)
fa1c114f
JS
1002{
1003 struct ath5k_hw *ah = sc->ah;
8a63facc 1004 unsigned int i;
fa1c114f 1005
8a63facc
BC
1006 /* XXX return value */
1007 if (likely(!test_bit(ATH_STAT_INVALID, sc->status))) {
1008 /* don't touch the hardware if marked invalid */
1009 ath5k_hw_stop_tx_dma(ah, sc->bhalq);
1010 ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "beacon queue %x\n",
1011 ath5k_hw_get_txdp(ah, sc->bhalq));
1012 for (i = 0; i < ARRAY_SIZE(sc->txqs); i++)
1013 if (sc->txqs[i].setup) {
1014 ath5k_hw_stop_tx_dma(ah, sc->txqs[i].qnum);
1015 ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "txq [%u] %x, "
1016 "link %p\n",
1017 sc->txqs[i].qnum,
1018 ath5k_hw_get_txdp(ah,
1019 sc->txqs[i].qnum),
1020 sc->txqs[i].link);
1021 }
0452d4a5 1022 }
fa1c114f 1023
8a63facc
BC
1024 for (i = 0; i < ARRAY_SIZE(sc->txqs); i++)
1025 if (sc->txqs[i].setup)
1026 ath5k_txq_drainq(sc, &sc->txqs[i]);
fa1c114f
JS
1027}
1028
8a63facc
BC
1029static void
1030ath5k_txq_release(struct ath5k_softc *sc)
2ac2927a 1031{
8a63facc
BC
1032 struct ath5k_txq *txq = sc->txqs;
1033 unsigned int i;
2ac2927a 1034
8a63facc
BC
1035 for (i = 0; i < ARRAY_SIZE(sc->txqs); i++, txq++)
1036 if (txq->setup) {
1037 ath5k_hw_release_tx_queue(sc->ah, txq->qnum);
1038 txq->setup = false;
1039 }
1040}
2ac2927a 1041
2ac2927a 1042
8a63facc
BC
1043/*************\
1044* RX Handling *
1045\*************/
2ac2927a 1046
8a63facc
BC
1047/*
1048 * Enable the receive h/w following a reset.
1049 */
fa1c114f 1050static int
8a63facc 1051ath5k_rx_start(struct ath5k_softc *sc)
fa1c114f
JS
1052{
1053 struct ath5k_hw *ah = sc->ah;
8a63facc
BC
1054 struct ath_common *common = ath5k_hw_common(ah);
1055 struct ath5k_buf *bf;
1056 int ret;
fa1c114f 1057
8a63facc 1058 common->rx_bufsize = roundup(IEEE80211_MAX_FRAME_LEN, common->cachelsz);
fa1c114f 1059
8a63facc
BC
1060 ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "cachelsz %u rx_bufsize %u\n",
1061 common->cachelsz, common->rx_bufsize);
2f7fe870 1062
8a63facc
BC
1063 spin_lock_bh(&sc->rxbuflock);
1064 sc->rxlink = NULL;
1065 list_for_each_entry(bf, &sc->rxbuf, list) {
1066 ret = ath5k_rxbuf_setup(sc, bf);
1067 if (ret != 0) {
1068 spin_unlock_bh(&sc->rxbuflock);
1069 goto err;
1070 }
2f7fe870 1071 }
8a63facc
BC
1072 bf = list_first_entry(&sc->rxbuf, struct ath5k_buf, list);
1073 ath5k_hw_set_rxdp(ah, bf->daddr);
1074 spin_unlock_bh(&sc->rxbuflock);
2f7fe870 1075
8a63facc
BC
1076 ath5k_hw_start_rx_dma(ah); /* enable recv descriptors */
1077 ath5k_mode_setup(sc); /* set filters, etc. */
1078 ath5k_hw_start_rx_pcu(ah); /* re-enable PCU/DMA engine */
fa1c114f
JS
1079
1080 return 0;
8a63facc 1081err:
fa1c114f
JS
1082 return ret;
1083}
1084
8a63facc
BC
1085/*
1086 * Disable the receive h/w in preparation for a reset.
1087 */
1088static void
1089ath5k_rx_stop(struct ath5k_softc *sc)
fa1c114f 1090{
8a63facc 1091 struct ath5k_hw *ah = sc->ah;
fa1c114f 1092
8a63facc
BC
1093 ath5k_hw_stop_rx_pcu(ah); /* disable PCU */
1094 ath5k_hw_set_rx_filter(ah, 0); /* clear recv filter */
1095 ath5k_hw_stop_rx_dma(ah); /* disable DMA engine */
fa1c114f 1096
8a63facc
BC
1097 ath5k_debug_printrxbuffs(sc, ah);
1098}
fa1c114f 1099
8a63facc
BC
1100static unsigned int
1101ath5k_rx_decrypted(struct ath5k_softc *sc, struct sk_buff *skb,
1102 struct ath5k_rx_status *rs)
1103{
1104 struct ath5k_hw *ah = sc->ah;
1105 struct ath_common *common = ath5k_hw_common(ah);
1106 struct ieee80211_hdr *hdr = (void *)skb->data;
1107 unsigned int keyix, hlen;
fa1c114f 1108
8a63facc
BC
1109 if (!(rs->rs_status & AR5K_RXERR_DECRYPT) &&
1110 rs->rs_keyix != AR5K_RXKEYIX_INVALID)
1111 return RX_FLAG_DECRYPTED;
fa1c114f 1112
8a63facc
BC
1113 /* Apparently when a default key is used to decrypt the packet
1114 the hw does not set the index used to decrypt. In such cases
1115 get the index from the packet. */
1116 hlen = ieee80211_hdrlen(hdr->frame_control);
1117 if (ieee80211_has_protected(hdr->frame_control) &&
1118 !(rs->rs_status & AR5K_RXERR_DECRYPT) &&
1119 skb->len >= hlen + 4) {
1120 keyix = skb->data[hlen + 3] >> 6;
1121
1122 if (test_bit(keyix, common->keymap))
1123 return RX_FLAG_DECRYPTED;
1124 }
fa1c114f
JS
1125
1126 return 0;
fa1c114f
JS
1127}
1128
8a63facc 1129
fa1c114f 1130static void
8a63facc
BC
1131ath5k_check_ibss_tsf(struct ath5k_softc *sc, struct sk_buff *skb,
1132 struct ieee80211_rx_status *rxs)
fa1c114f 1133{
8a63facc
BC
1134 struct ath_common *common = ath5k_hw_common(sc->ah);
1135 u64 tsf, bc_tstamp;
1136 u32 hw_tu;
1137 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)skb->data;
fa1c114f 1138
8a63facc
BC
1139 if (ieee80211_is_beacon(mgmt->frame_control) &&
1140 le16_to_cpu(mgmt->u.beacon.capab_info) & WLAN_CAPABILITY_IBSS &&
1141 memcmp(mgmt->bssid, common->curbssid, ETH_ALEN) == 0) {
1142 /*
1143 * Received an IBSS beacon with the same BSSID. Hardware *must*
1144 * have updated the local TSF. We have to work around various
1145 * hardware bugs, though...
1146 */
1147 tsf = ath5k_hw_get_tsf64(sc->ah);
1148 bc_tstamp = le64_to_cpu(mgmt->u.beacon.timestamp);
1149 hw_tu = TSF_TO_TU(tsf);
fa1c114f 1150
8a63facc
BC
1151 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
1152 "beacon %llx mactime %llx (diff %lld) tsf now %llx\n",
1153 (unsigned long long)bc_tstamp,
1154 (unsigned long long)rxs->mactime,
1155 (unsigned long long)(rxs->mactime - bc_tstamp),
1156 (unsigned long long)tsf);
fa1c114f 1157
8a63facc
BC
1158 /*
1159 * Sometimes the HW will give us a wrong tstamp in the rx
1160 * status, causing the timestamp extension to go wrong.
1161 * (This seems to happen especially with beacon frames bigger
1162 * than 78 byte (incl. FCS))
1163 * But we know that the receive timestamp must be later than the
1164 * timestamp of the beacon since HW must have synced to that.
1165 *
1166 * NOTE: here we assume mactime to be after the frame was
1167 * received, not like mac80211 which defines it at the start.
1168 */
1169 if (bc_tstamp > rxs->mactime) {
1170 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
1171 "fixing mactime from %llx to %llx\n",
1172 (unsigned long long)rxs->mactime,
1173 (unsigned long long)tsf);
1174 rxs->mactime = tsf;
1175 }
fa1c114f 1176
8a63facc
BC
1177 /*
1178 * Local TSF might have moved higher than our beacon timers,
1179 * in that case we have to update them to continue sending
1180 * beacons. This also takes care of synchronizing beacon sending
1181 * times with other stations.
1182 */
1183 if (hw_tu >= sc->nexttbtt)
1184 ath5k_beacon_update_timers(sc, bc_tstamp);
1185 }
1186}
fa1c114f 1187
8a63facc
BC
1188static void
1189ath5k_update_beacon_rssi(struct ath5k_softc *sc, struct sk_buff *skb, int rssi)
1190{
1191 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)skb->data;
1192 struct ath5k_hw *ah = sc->ah;
1193 struct ath_common *common = ath5k_hw_common(ah);
fa1c114f 1194
8a63facc
BC
1195 /* only beacons from our BSSID */
1196 if (!ieee80211_is_beacon(mgmt->frame_control) ||
1197 memcmp(mgmt->bssid, common->curbssid, ETH_ALEN) != 0)
1198 return;
fa1c114f 1199
8a63facc
BC
1200 ah->ah_beacon_rssi_avg = ath5k_moving_average(ah->ah_beacon_rssi_avg,
1201 rssi);
fa1c114f 1202
8a63facc
BC
1203 /* in IBSS mode we should keep RSSI statistics per neighbour */
1204 /* le16_to_cpu(mgmt->u.beacon.capab_info) & WLAN_CAPABILITY_IBSS */
1205}
fa1c114f 1206
8a63facc
BC
1207/*
1208 * Compute padding position. skb must contain an IEEE 802.11 frame
1209 */
1210static int ath5k_common_padpos(struct sk_buff *skb)
fa1c114f 1211{
8a63facc
BC
1212 struct ieee80211_hdr * hdr = (struct ieee80211_hdr *)skb->data;
1213 __le16 frame_control = hdr->frame_control;
1214 int padpos = 24;
fa1c114f 1215
8a63facc
BC
1216 if (ieee80211_has_a4(frame_control)) {
1217 padpos += ETH_ALEN;
fa1c114f 1218 }
8a63facc
BC
1219 if (ieee80211_is_data_qos(frame_control)) {
1220 padpos += IEEE80211_QOS_CTL_LEN;
fa1c114f 1221 }
8a63facc
BC
1222
1223 return padpos;
fa1c114f
JS
1224}
1225
8a63facc
BC
1226/*
1227 * This function expects an 802.11 frame and returns the number of
1228 * bytes added, or -1 if we don't have enough header room.
1229 */
1230static int ath5k_add_padding(struct sk_buff *skb)
fa1c114f 1231{
8a63facc
BC
1232 int padpos = ath5k_common_padpos(skb);
1233 int padsize = padpos & 3;
fa1c114f 1234
8a63facc 1235 if (padsize && skb->len>padpos) {
fa1c114f 1236
8a63facc
BC
1237 if (skb_headroom(skb) < padsize)
1238 return -1;
fa1c114f 1239
8a63facc
BC
1240 skb_push(skb, padsize);
1241 memmove(skb->data, skb->data+padsize, padpos);
1242 return padsize;
1243 }
a951ae21 1244
8a63facc
BC
1245 return 0;
1246}
fa1c114f 1247
8a63facc
BC
1248/*
1249 * The MAC header is padded to have 32-bit boundary if the
1250 * packet payload is non-zero. The general calculation for
1251 * padsize would take into account odd header lengths:
1252 * padsize = 4 - (hdrlen & 3); however, since only
1253 * even-length headers are used, padding can only be 0 or 2
1254 * bytes and we can optimize this a bit. We must not try to
1255 * remove padding from short control frames that do not have a
1256 * payload.
1257 *
1258 * This function expects an 802.11 frame and returns the number of
1259 * bytes removed.
1260 */
1261static int ath5k_remove_padding(struct sk_buff *skb)
1262{
1263 int padpos = ath5k_common_padpos(skb);
1264 int padsize = padpos & 3;
6d91e1d8 1265
8a63facc
BC
1266 if (padsize && skb->len>=padpos+padsize) {
1267 memmove(skb->data + padsize, skb->data, padpos);
1268 skb_pull(skb, padsize);
1269 return padsize;
fa1c114f 1270 }
a951ae21 1271
8a63facc 1272 return 0;
fa1c114f
JS
1273}
1274
1275static void
8a63facc
BC
1276ath5k_receive_frame(struct ath5k_softc *sc, struct sk_buff *skb,
1277 struct ath5k_rx_status *rs)
fa1c114f 1278{
8a63facc
BC
1279 struct ieee80211_rx_status *rxs;
1280
1281 ath5k_remove_padding(skb);
1282
1283 rxs = IEEE80211_SKB_RXCB(skb);
1284
1285 rxs->flag = 0;
1286 if (unlikely(rs->rs_status & AR5K_RXERR_MIC))
1287 rxs->flag |= RX_FLAG_MMIC_ERROR;
fa1c114f
JS
1288
1289 /*
8a63facc
BC
1290 * always extend the mac timestamp, since this information is
1291 * also needed for proper IBSS merging.
1292 *
1293 * XXX: it might be too late to do it here, since rs_tstamp is
1294 * 15bit only. that means TSF extension has to be done within
1295 * 32768usec (about 32ms). it might be necessary to move this to
1296 * the interrupt handler, like it is done in madwifi.
1297 *
1298 * Unfortunately we don't know when the hardware takes the rx
1299 * timestamp (beginning of phy frame, data frame, end of rx?).
1300 * The only thing we know is that it is hardware specific...
1301 * On AR5213 it seems the rx timestamp is at the end of the
1302 * frame, but i'm not sure.
1303 *
1304 * NOTE: mac80211 defines mactime at the beginning of the first
1305 * data symbol. Since we don't have any time references it's
1306 * impossible to comply to that. This affects IBSS merge only
1307 * right now, so it's not too bad...
fa1c114f 1308 */
8a63facc
BC
1309 rxs->mactime = ath5k_extend_tsf(sc->ah, rs->rs_tstamp);
1310 rxs->flag |= RX_FLAG_TSFT;
fa1c114f 1311
8a63facc
BC
1312 rxs->freq = sc->curchan->center_freq;
1313 rxs->band = sc->curband->band;
fa1c114f 1314
8a63facc 1315 rxs->signal = sc->ah->ah_noise_floor + rs->rs_rssi;
fa1c114f 1316
8a63facc 1317 rxs->antenna = rs->rs_antenna;
fa1c114f 1318
8a63facc
BC
1319 if (rs->rs_antenna > 0 && rs->rs_antenna < 5)
1320 sc->stats.antenna_rx[rs->rs_antenna]++;
1321 else
1322 sc->stats.antenna_rx[0]++; /* invalid */
fa1c114f 1323
8a63facc
BC
1324 rxs->rate_idx = ath5k_hw_to_driver_rix(sc, rs->rs_rate);
1325 rxs->flag |= ath5k_rx_decrypted(sc, skb, rs);
fa1c114f 1326
8a63facc
BC
1327 if (rxs->rate_idx >= 0 && rs->rs_rate ==
1328 sc->curband->bitrates[rxs->rate_idx].hw_value_short)
1329 rxs->flag |= RX_FLAG_SHORTPRE;
fa1c114f 1330
8a63facc 1331 ath5k_debug_dump_skb(sc, skb, "RX ", 0);
fa1c114f 1332
8a63facc 1333 ath5k_update_beacon_rssi(sc, skb, rs->rs_rssi);
fa1c114f 1334
8a63facc
BC
1335 /* check beacons in IBSS mode */
1336 if (sc->opmode == NL80211_IFTYPE_ADHOC)
1337 ath5k_check_ibss_tsf(sc, skb, rxs);
fa1c114f 1338
8a63facc
BC
1339 ieee80211_rx(sc->hw, skb);
1340}
fa1c114f 1341
8a63facc
BC
1342/** ath5k_frame_receive_ok() - Do we want to receive this frame or not?
1343 *
1344 * Check if we want to further process this frame or not. Also update
1345 * statistics. Return true if we want this frame, false if not.
fa1c114f 1346 */
8a63facc
BC
1347static bool
1348ath5k_receive_frame_ok(struct ath5k_softc *sc, struct ath5k_rx_status *rs)
fa1c114f 1349{
8a63facc 1350 sc->stats.rx_all_count++;
fa1c114f 1351
8a63facc
BC
1352 if (unlikely(rs->rs_status)) {
1353 if (rs->rs_status & AR5K_RXERR_CRC)
1354 sc->stats.rxerr_crc++;
1355 if (rs->rs_status & AR5K_RXERR_FIFO)
1356 sc->stats.rxerr_fifo++;
1357 if (rs->rs_status & AR5K_RXERR_PHY) {
1358 sc->stats.rxerr_phy++;
1359 if (rs->rs_phyerr > 0 && rs->rs_phyerr < 32)
1360 sc->stats.rxerr_phy_code[rs->rs_phyerr]++;
1361 return false;
1362 }
1363 if (rs->rs_status & AR5K_RXERR_DECRYPT) {
1364 /*
1365 * Decrypt error. If the error occurred
1366 * because there was no hardware key, then
1367 * let the frame through so the upper layers
1368 * can process it. This is necessary for 5210
1369 * parts which have no way to setup a ``clear''
1370 * key cache entry.
1371 *
1372 * XXX do key cache faulting
1373 */
1374 sc->stats.rxerr_decrypt++;
1375 if (rs->rs_keyix == AR5K_RXKEYIX_INVALID &&
1376 !(rs->rs_status & AR5K_RXERR_CRC))
1377 return true;
1378 }
1379 if (rs->rs_status & AR5K_RXERR_MIC) {
1380 sc->stats.rxerr_mic++;
1381 return true;
fa1c114f 1382 }
fa1c114f 1383
8a63facc
BC
1384 /* reject any frames with non-crypto errors */
1385 if (rs->rs_status & ~(AR5K_RXERR_DECRYPT))
1386 return false;
1387 }
fa1c114f 1388
8a63facc
BC
1389 if (unlikely(rs->rs_more)) {
1390 sc->stats.rxerr_jumbo++;
1391 return false;
1392 }
1393 return true;
fa1c114f
JS
1394}
1395
fa1c114f 1396static void
8a63facc 1397ath5k_tasklet_rx(unsigned long data)
fa1c114f 1398{
8a63facc
BC
1399 struct ath5k_rx_status rs = {};
1400 struct sk_buff *skb, *next_skb;
1401 dma_addr_t next_skb_addr;
1402 struct ath5k_softc *sc = (void *)data;
dc1e001b
LR
1403 struct ath5k_hw *ah = sc->ah;
1404 struct ath_common *common = ath5k_hw_common(ah);
8a63facc
BC
1405 struct ath5k_buf *bf;
1406 struct ath5k_desc *ds;
1407 int ret;
fa1c114f 1408
8a63facc
BC
1409 spin_lock(&sc->rxbuflock);
1410 if (list_empty(&sc->rxbuf)) {
1411 ATH5K_WARN(sc, "empty rx buf pool\n");
1412 goto unlock;
1413 }
1414 do {
1415 bf = list_first_entry(&sc->rxbuf, struct ath5k_buf, list);
1416 BUG_ON(bf->skb == NULL);
1417 skb = bf->skb;
1418 ds = bf->desc;
fa1c114f 1419
8a63facc
BC
1420 /* bail if HW is still using self-linked descriptor */
1421 if (ath5k_hw_get_rxdp(sc->ah) == bf->daddr)
1422 break;
fa1c114f 1423
8a63facc
BC
1424 ret = sc->ah->ah_proc_rx_desc(sc->ah, ds, &rs);
1425 if (unlikely(ret == -EINPROGRESS))
1426 break;
1427 else if (unlikely(ret)) {
1428 ATH5K_ERR(sc, "error in processing rx descriptor\n");
1429 sc->stats.rxerr_proc++;
1430 break;
1431 }
fa1c114f 1432
8a63facc
BC
1433 if (ath5k_receive_frame_ok(sc, &rs)) {
1434 next_skb = ath5k_rx_skb_alloc(sc, &next_skb_addr);
fa1c114f 1435
8a63facc
BC
1436 /*
1437 * If we can't replace bf->skb with a new skb under
1438 * memory pressure, just skip this packet
1439 */
1440 if (!next_skb)
1441 goto next;
036cd1ec 1442
8a63facc
BC
1443 pci_unmap_single(sc->pdev, bf->skbaddr,
1444 common->rx_bufsize,
1445 PCI_DMA_FROMDEVICE);
036cd1ec 1446
8a63facc 1447 skb_put(skb, rs.rs_datalen);
6ba81c2c 1448
8a63facc 1449 ath5k_receive_frame(sc, skb, &rs);
6ba81c2c 1450
8a63facc
BC
1451 bf->skb = next_skb;
1452 bf->skbaddr = next_skb_addr;
036cd1ec 1453 }
8a63facc
BC
1454next:
1455 list_move_tail(&bf->list, &sc->rxbuf);
1456 } while (ath5k_rxbuf_setup(sc, bf) == 0);
1457unlock:
1458 spin_unlock(&sc->rxbuflock);
036cd1ec
BR
1459}
1460
b4ea449d 1461
8a63facc
BC
1462/*************\
1463* TX Handling *
1464\*************/
b4ea449d 1465
8a63facc
BC
1466static int ath5k_tx_queue(struct ieee80211_hw *hw, struct sk_buff *skb,
1467 struct ath5k_txq *txq)
1468{
1469 struct ath5k_softc *sc = hw->priv;
1470 struct ath5k_buf *bf;
1471 unsigned long flags;
1472 int padsize;
b4ea449d 1473
8a63facc 1474 ath5k_debug_dump_skb(sc, skb, "TX ", 1);
b4ea449d 1475
8a63facc
BC
1476 /*
1477 * The hardware expects the header padded to 4 byte boundaries.
1478 * If this is not the case, we add the padding after the header.
1479 */
1480 padsize = ath5k_add_padding(skb);
1481 if (padsize < 0) {
1482 ATH5K_ERR(sc, "tx hdrlen not %%4: not enough"
1483 " headroom to pad");
1484 goto drop_packet;
1485 }
8127fbdc 1486
925e0b06
BR
1487 if (txq->txq_len >= ATH5K_TXQ_LEN_MAX)
1488 ieee80211_stop_queue(hw, txq->qnum);
1489
8a63facc
BC
1490 spin_lock_irqsave(&sc->txbuflock, flags);
1491 if (list_empty(&sc->txbuf)) {
1492 ATH5K_ERR(sc, "no further txbuf available, dropping packet\n");
1493 spin_unlock_irqrestore(&sc->txbuflock, flags);
651d9375 1494 ieee80211_stop_queues(hw);
8a63facc 1495 goto drop_packet;
8127fbdc 1496 }
8a63facc
BC
1497 bf = list_first_entry(&sc->txbuf, struct ath5k_buf, list);
1498 list_del(&bf->list);
1499 sc->txbuf_len--;
1500 if (list_empty(&sc->txbuf))
1501 ieee80211_stop_queues(hw);
1502 spin_unlock_irqrestore(&sc->txbuflock, flags);
1503
1504 bf->skb = skb;
1505
1506 if (ath5k_txbuf_setup(sc, bf, txq, padsize)) {
1507 bf->skb = NULL;
1508 spin_lock_irqsave(&sc->txbuflock, flags);
1509 list_add_tail(&bf->list, &sc->txbuf);
1510 sc->txbuf_len++;
1511 spin_unlock_irqrestore(&sc->txbuflock, flags);
1512 goto drop_packet;
8127fbdc 1513 }
8a63facc 1514 return NETDEV_TX_OK;
8127fbdc 1515
8a63facc
BC
1516drop_packet:
1517 dev_kfree_skb_any(skb);
1518 return NETDEV_TX_OK;
8127fbdc
BP
1519}
1520
1440401e
BR
1521static void
1522ath5k_tx_frame_completed(struct ath5k_softc *sc, struct sk_buff *skb,
1523 struct ath5k_tx_status *ts)
1524{
1525 struct ieee80211_tx_info *info;
1526 int i;
1527
1528 sc->stats.tx_all_count++;
1529 info = IEEE80211_SKB_CB(skb);
1530
1531 ieee80211_tx_info_clear_status(info);
1532 for (i = 0; i < 4; i++) {
1533 struct ieee80211_tx_rate *r =
1534 &info->status.rates[i];
1535
1536 if (ts->ts_rate[i]) {
1537 r->idx = ath5k_hw_to_driver_rix(sc, ts->ts_rate[i]);
1538 r->count = ts->ts_retry[i];
1539 } else {
1540 r->idx = -1;
1541 r->count = 0;
1542 }
1543 }
1544
1545 /* count the successful attempt as well */
1546 info->status.rates[ts->ts_final_idx].count++;
1547
1548 if (unlikely(ts->ts_status)) {
1549 sc->stats.ack_fail++;
1550 if (ts->ts_status & AR5K_TXERR_FILT) {
1551 info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
1552 sc->stats.txerr_filt++;
1553 }
1554 if (ts->ts_status & AR5K_TXERR_XRETRY)
1555 sc->stats.txerr_retry++;
1556 if (ts->ts_status & AR5K_TXERR_FIFO)
1557 sc->stats.txerr_fifo++;
1558 } else {
1559 info->flags |= IEEE80211_TX_STAT_ACK;
1560 info->status.ack_signal = ts->ts_rssi;
1561 }
1562
1563 /*
1564 * Remove MAC header padding before giving the frame
1565 * back to mac80211.
1566 */
1567 ath5k_remove_padding(skb);
1568
1569 if (ts->ts_antenna > 0 && ts->ts_antenna < 5)
1570 sc->stats.antenna_tx[ts->ts_antenna]++;
1571 else
1572 sc->stats.antenna_tx[0]++; /* invalid */
1573
1574 ieee80211_tx_status(sc->hw, skb);
1575}
8a63facc
BC
1576
1577static void
1578ath5k_tx_processq(struct ath5k_softc *sc, struct ath5k_txq *txq)
8127fbdc 1579{
8a63facc
BC
1580 struct ath5k_tx_status ts = {};
1581 struct ath5k_buf *bf, *bf0;
1582 struct ath5k_desc *ds;
1583 struct sk_buff *skb;
1440401e 1584 int ret;
8127fbdc 1585
8a63facc
BC
1586 spin_lock(&txq->lock);
1587 list_for_each_entry_safe(bf, bf0, &txq->q, list) {
1588 ds = bf->desc;
8127fbdc 1589
8a63facc
BC
1590 /*
1591 * It's possible that the hardware can say the buffer is
1592 * completed when it hasn't yet loaded the ds_link from
1593 * host memory and moved on. If there are more TX
1594 * descriptors in the queue, wait for TXDP to change
1595 * before processing this one.
1596 */
1597 if (ath5k_hw_get_txdp(sc->ah, txq->qnum) == bf->daddr &&
1598 !list_is_last(&bf->list, &txq->q))
1599 break;
8a63facc
BC
1600 ret = sc->ah->ah_proc_tx_desc(sc->ah, ds, &ts);
1601 if (unlikely(ret == -EINPROGRESS))
1602 break;
1603 else if (unlikely(ret)) {
1604 ATH5K_ERR(sc, "error %d while processing queue %u\n",
1605 ret, txq->qnum);
1606 break;
1607 }
8127fbdc 1608
8a63facc 1609 skb = bf->skb;
8a63facc 1610 bf->skb = NULL;
fa1c114f
JS
1611 pci_unmap_single(sc->pdev, bf->skbaddr, skb->len,
1612 PCI_DMA_TODEVICE);
1613
1440401e 1614 ath5k_tx_frame_completed(sc, skb, &ts);
fa1c114f
JS
1615
1616 spin_lock(&sc->txbuflock);
fa1c114f
JS
1617 list_move_tail(&bf->list, &sc->txbuf);
1618 sc->txbuf_len++;
925e0b06 1619 txq->txq_len--;
fa1c114f 1620 spin_unlock(&sc->txbuflock);
4edd761f
BR
1621
1622 txq->txq_poll_mark = false;
fa1c114f
JS
1623 }
1624 if (likely(list_empty(&txq->q)))
1625 txq->link = NULL;
1626 spin_unlock(&txq->lock);
925e0b06
BR
1627 if (txq->txq_len < ATH5K_TXQ_LEN_LOW)
1628 ieee80211_wake_queue(sc->hw, txq->qnum);
fa1c114f
JS
1629}
1630
1631static void
1632ath5k_tasklet_tx(unsigned long data)
1633{
8784d2ee 1634 int i;
fa1c114f
JS
1635 struct ath5k_softc *sc = (void *)data;
1636
8784d2ee
BC
1637 for (i=0; i < AR5K_NUM_TX_QUEUES; i++)
1638 if (sc->txqs[i].setup && (sc->ah->ah_txq_isr & BIT(i)))
1639 ath5k_tx_processq(sc, &sc->txqs[i]);
fa1c114f
JS
1640}
1641
1642
fa1c114f
JS
1643/*****************\
1644* Beacon handling *
1645\*****************/
1646
1647/*
1648 * Setup the beacon frame for transmit.
1649 */
1650static int
e039fa4a 1651ath5k_beacon_setup(struct ath5k_softc *sc, struct ath5k_buf *bf)
fa1c114f
JS
1652{
1653 struct sk_buff *skb = bf->skb;
a888d52d 1654 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
fa1c114f
JS
1655 struct ath5k_hw *ah = sc->ah;
1656 struct ath5k_desc *ds;
2bed03eb
NK
1657 int ret = 0;
1658 u8 antenna;
fa1c114f 1659 u32 flags;
8127fbdc 1660 const int padsize = 0;
fa1c114f
JS
1661
1662 bf->skbaddr = pci_map_single(sc->pdev, skb->data, skb->len,
1663 PCI_DMA_TODEVICE);
1664 ATH5K_DBG(sc, ATH5K_DEBUG_BEACON, "skb %p [data %p len %u] "
1665 "skbaddr %llx\n", skb, skb->data, skb->len,
1666 (unsigned long long)bf->skbaddr);
8d8bb39b 1667 if (pci_dma_mapping_error(sc->pdev, bf->skbaddr)) {
fa1c114f
JS
1668 ATH5K_ERR(sc, "beacon DMA mapping failed\n");
1669 return -EIO;
1670 }
1671
1672 ds = bf->desc;
2bed03eb 1673 antenna = ah->ah_tx_ant;
fa1c114f
JS
1674
1675 flags = AR5K_TXDESC_NOACK;
05c914fe 1676 if (sc->opmode == NL80211_IFTYPE_ADHOC && ath5k_hw_hasveol(ah)) {
fa1c114f
JS
1677 ds->ds_link = bf->daddr; /* self-linked */
1678 flags |= AR5K_TXDESC_VEOL;
2bed03eb 1679 } else
fa1c114f 1680 ds->ds_link = 0;
2bed03eb
NK
1681
1682 /*
1683 * If we use multiple antennas on AP and use
1684 * the Sectored AP scenario, switch antenna every
1685 * 4 beacons to make sure everybody hears our AP.
1686 * When a client tries to associate, hw will keep
1687 * track of the tx antenna to be used for this client
1688 * automaticaly, based on ACKed packets.
1689 *
1690 * Note: AP still listens and transmits RTS on the
1691 * default antenna which is supposed to be an omni.
1692 *
1693 * Note2: On sectored scenarios it's possible to have
a180a130
BC
1694 * multiple antennas (1 omni -- the default -- and 14
1695 * sectors), so if we choose to actually support this
1696 * mode, we need to allow the user to set how many antennas
1697 * we have and tweak the code below to send beacons
1698 * on all of them.
2bed03eb
NK
1699 */
1700 if (ah->ah_ant_mode == AR5K_ANTMODE_SECTOR_AP)
1701 antenna = sc->bsent & 4 ? 2 : 1;
1702
fa1c114f 1703
8f655dde
NK
1704 /* FIXME: If we are in g mode and rate is a CCK rate
1705 * subtract ah->ah_txpower.txp_cck_ofdm_pwr_delta
1706 * from tx power (value is in dB units already) */
fa1c114f 1707 ds->ds_data = bf->skbaddr;
281c56dd 1708 ret = ah->ah_setup_tx_desc(ah, ds, skb->len,
8127fbdc 1709 ieee80211_get_hdrlen_from_skb(skb), padsize,
400ec45a 1710 AR5K_PKT_TYPE_BEACON, (sc->power_level * 2),
e039fa4a 1711 ieee80211_get_tx_rate(sc->hw, info)->hw_value,
2e92e6f2 1712 1, AR5K_TXKEYIX_INVALID,
400ec45a 1713 antenna, flags, 0, 0);
fa1c114f
JS
1714 if (ret)
1715 goto err_unmap;
1716
1717 return 0;
1718err_unmap:
1719 pci_unmap_single(sc->pdev, bf->skbaddr, skb->len, PCI_DMA_TODEVICE);
1720 return ret;
1721}
1722
8a63facc
BC
1723/*
1724 * Updates the beacon that is sent by ath5k_beacon_send. For adhoc,
1725 * this is called only once at config_bss time, for AP we do it every
1726 * SWBA interrupt so that the TIM will reflect buffered frames.
1727 *
1728 * Called with the beacon lock.
1729 */
1730static int
1731ath5k_beacon_update(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1732{
1733 int ret;
1734 struct ath5k_softc *sc = hw->priv;
1735 struct sk_buff *skb;
1736
1737 if (WARN_ON(!vif)) {
1738 ret = -EINVAL;
1739 goto out;
1740 }
1741
1742 skb = ieee80211_beacon_get(hw, vif);
1743
1744 if (!skb) {
1745 ret = -ENOMEM;
1746 goto out;
1747 }
1748
1749 ath5k_debug_dump_skb(sc, skb, "BC ", 1);
1750
1751 ath5k_txbuf_free_skb(sc, sc->bbuf);
1752 sc->bbuf->skb = skb;
1753 ret = ath5k_beacon_setup(sc, sc->bbuf);
1754 if (ret)
1755 sc->bbuf->skb = NULL;
1756out:
1757 return ret;
1758}
1759
fa1c114f
JS
1760/*
1761 * Transmit a beacon frame at SWBA. Dynamic updates to the
1762 * frame contents are done as needed and the slot time is
1763 * also adjusted based on current state.
1764 *
5faaff74
BC
1765 * This is called from software irq context (beacontq tasklets)
1766 * or user context from ath5k_beacon_config.
fa1c114f
JS
1767 */
1768static void
1769ath5k_beacon_send(struct ath5k_softc *sc)
1770{
1771 struct ath5k_buf *bf = sc->bbuf;
1772 struct ath5k_hw *ah = sc->ah;
cec8db23 1773 struct sk_buff *skb;
fa1c114f 1774
be9b7259 1775 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON, "in beacon_send\n");
fa1c114f 1776
4afd89d9 1777 if (unlikely(bf->skb == NULL || sc->opmode == NL80211_IFTYPE_STATION)) {
fa1c114f
JS
1778 ATH5K_WARN(sc, "bf=%p bf_skb=%p\n", bf, bf ? bf->skb : NULL);
1779 return;
1780 }
1781 /*
1782 * Check if the previous beacon has gone out. If
a180a130 1783 * not, don't don't try to post another: skip this
fa1c114f
JS
1784 * period and wait for the next. Missed beacons
1785 * indicate a problem and should not occur. If we
1786 * miss too many consecutive beacons reset the device.
1787 */
1788 if (unlikely(ath5k_hw_num_tx_pending(ah, sc->bhalq) != 0)) {
1789 sc->bmisscount++;
be9b7259 1790 ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
fa1c114f 1791 "missed %u consecutive beacons\n", sc->bmisscount);
428cbd4f 1792 if (sc->bmisscount > 10) { /* NB: 10 is a guess */
be9b7259 1793 ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
fa1c114f
JS
1794 "stuck beacon time (%u missed)\n",
1795 sc->bmisscount);
8d67a031
BR
1796 ATH5K_DBG(sc, ATH5K_DEBUG_RESET,
1797 "stuck beacon, resetting\n");
5faaff74 1798 ieee80211_queue_work(sc->hw, &sc->reset_work);
fa1c114f
JS
1799 }
1800 return;
1801 }
1802 if (unlikely(sc->bmisscount != 0)) {
be9b7259 1803 ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
fa1c114f
JS
1804 "resume beacon xmit after %u misses\n",
1805 sc->bmisscount);
1806 sc->bmisscount = 0;
1807 }
1808
1809 /*
1810 * Stop any current dma and put the new frame on the queue.
1811 * This should never fail since we check above that no frames
1812 * are still pending on the queue.
1813 */
1814 if (unlikely(ath5k_hw_stop_tx_dma(ah, sc->bhalq))) {
428cbd4f 1815 ATH5K_WARN(sc, "beacon queue %u didn't start/stop ?\n", sc->bhalq);
fa1c114f
JS
1816 /* NB: hw still stops DMA, so proceed */
1817 }
fa1c114f 1818
1071db86
BC
1819 /* refresh the beacon for AP mode */
1820 if (sc->opmode == NL80211_IFTYPE_AP)
1821 ath5k_beacon_update(sc->hw, sc->vif);
1822
c6e387a2
NK
1823 ath5k_hw_set_txdp(ah, sc->bhalq, bf->daddr);
1824 ath5k_hw_start_tx_dma(ah, sc->bhalq);
be9b7259 1825 ATH5K_DBG(sc, ATH5K_DEBUG_BEACON, "TXDP[%u] = %llx (%p)\n",
fa1c114f
JS
1826 sc->bhalq, (unsigned long long)bf->daddr, bf->desc);
1827
cec8db23
BC
1828 skb = ieee80211_get_buffered_bc(sc->hw, sc->vif);
1829 while (skb) {
1830 ath5k_tx_queue(sc->hw, skb, sc->cabq);
1831 skb = ieee80211_get_buffered_bc(sc->hw, sc->vif);
1832 }
1833
fa1c114f
JS
1834 sc->bsent++;
1835}
1836
9804b98d
BR
1837/**
1838 * ath5k_beacon_update_timers - update beacon timers
1839 *
1840 * @sc: struct ath5k_softc pointer we are operating on
1841 * @bc_tsf: the timestamp of the beacon. 0 to reset the TSF. -1 to perform a
1842 * beacon timer update based on the current HW TSF.
1843 *
1844 * Calculate the next target beacon transmit time (TBTT) based on the timestamp
1845 * of a received beacon or the current local hardware TSF and write it to the
1846 * beacon timer registers.
1847 *
1848 * This is called in a variety of situations, e.g. when a beacon is received,
6ba81c2c 1849 * when a TSF update has been detected, but also when an new IBSS is created or
9804b98d
BR
1850 * when we otherwise know we have to update the timers, but we keep it in this
1851 * function to have it all together in one place.
1852 */
fa1c114f 1853static void
9804b98d 1854ath5k_beacon_update_timers(struct ath5k_softc *sc, u64 bc_tsf)
fa1c114f
JS
1855{
1856 struct ath5k_hw *ah = sc->ah;
9804b98d
BR
1857 u32 nexttbtt, intval, hw_tu, bc_tu;
1858 u64 hw_tsf;
fa1c114f
JS
1859
1860 intval = sc->bintval & AR5K_BEACON_PERIOD;
1861 if (WARN_ON(!intval))
1862 return;
1863
9804b98d
BR
1864 /* beacon TSF converted to TU */
1865 bc_tu = TSF_TO_TU(bc_tsf);
fa1c114f 1866
9804b98d
BR
1867 /* current TSF converted to TU */
1868 hw_tsf = ath5k_hw_get_tsf64(ah);
1869 hw_tu = TSF_TO_TU(hw_tsf);
fa1c114f 1870
9804b98d
BR
1871#define FUDGE 3
1872 /* we use FUDGE to make sure the next TBTT is ahead of the current TU */
1873 if (bc_tsf == -1) {
1874 /*
1875 * no beacons received, called internally.
1876 * just need to refresh timers based on HW TSF.
1877 */
1878 nexttbtt = roundup(hw_tu + FUDGE, intval);
1879 } else if (bc_tsf == 0) {
1880 /*
1881 * no beacon received, probably called by ath5k_reset_tsf().
1882 * reset TSF to start with 0.
1883 */
1884 nexttbtt = intval;
1885 intval |= AR5K_BEACON_RESET_TSF;
1886 } else if (bc_tsf > hw_tsf) {
1887 /*
1888 * beacon received, SW merge happend but HW TSF not yet updated.
1889 * not possible to reconfigure timers yet, but next time we
1890 * receive a beacon with the same BSSID, the hardware will
1891 * automatically update the TSF and then we need to reconfigure
1892 * the timers.
1893 */
1894 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
1895 "need to wait for HW TSF sync\n");
1896 return;
1897 } else {
1898 /*
1899 * most important case for beacon synchronization between STA.
1900 *
1901 * beacon received and HW TSF has been already updated by HW.
1902 * update next TBTT based on the TSF of the beacon, but make
1903 * sure it is ahead of our local TSF timer.
1904 */
1905 nexttbtt = bc_tu + roundup(hw_tu + FUDGE - bc_tu, intval);
1906 }
1907#undef FUDGE
fa1c114f 1908
036cd1ec
BR
1909 sc->nexttbtt = nexttbtt;
1910
fa1c114f 1911 intval |= AR5K_BEACON_ENA;
fa1c114f 1912 ath5k_hw_init_beacon(ah, nexttbtt, intval);
9804b98d
BR
1913
1914 /*
1915 * debugging output last in order to preserve the time critical aspect
1916 * of this function
1917 */
1918 if (bc_tsf == -1)
1919 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
1920 "reconfigured timers based on HW TSF\n");
1921 else if (bc_tsf == 0)
1922 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
1923 "reset HW TSF and timers\n");
1924 else
1925 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
1926 "updated timers based on beacon TSF\n");
1927
1928 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
04f93a87
DM
1929 "bc_tsf %llx hw_tsf %llx bc_tu %u hw_tu %u nexttbtt %u\n",
1930 (unsigned long long) bc_tsf,
1931 (unsigned long long) hw_tsf, bc_tu, hw_tu, nexttbtt);
9804b98d
BR
1932 ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON, "intval %u %s %s\n",
1933 intval & AR5K_BEACON_PERIOD,
1934 intval & AR5K_BEACON_ENA ? "AR5K_BEACON_ENA" : "",
1935 intval & AR5K_BEACON_RESET_TSF ? "AR5K_BEACON_RESET_TSF" : "");
fa1c114f
JS
1936}
1937
036cd1ec
BR
1938/**
1939 * ath5k_beacon_config - Configure the beacon queues and interrupts
1940 *
1941 * @sc: struct ath5k_softc pointer we are operating on
fa1c114f 1942 *
036cd1ec 1943 * In IBSS mode we use a self-linked tx descriptor if possible. We enable SWBA
6ba81c2c 1944 * interrupts to detect TSF updates only.
fa1c114f
JS
1945 */
1946static void
1947ath5k_beacon_config(struct ath5k_softc *sc)
1948{
1949 struct ath5k_hw *ah = sc->ah;
b5f03956 1950 unsigned long flags;
fa1c114f 1951
21800491 1952 spin_lock_irqsave(&sc->block, flags);
fa1c114f 1953 sc->bmisscount = 0;
dc1968e7 1954 sc->imask &= ~(AR5K_INT_BMISS | AR5K_INT_SWBA);
fa1c114f 1955
21800491 1956 if (sc->enable_beacon) {
fa1c114f 1957 /*
036cd1ec
BR
1958 * In IBSS mode we use a self-linked tx descriptor and let the
1959 * hardware send the beacons automatically. We have to load it
fa1c114f 1960 * only once here.
036cd1ec 1961 * We use the SWBA interrupt only to keep track of the beacon
6ba81c2c 1962 * timers in order to detect automatic TSF updates.
fa1c114f
JS
1963 */
1964 ath5k_beaconq_config(sc);
fa1c114f 1965
036cd1ec
BR
1966 sc->imask |= AR5K_INT_SWBA;
1967
da966bca 1968 if (sc->opmode == NL80211_IFTYPE_ADHOC) {
21800491 1969 if (ath5k_hw_hasveol(ah))
da966bca 1970 ath5k_beacon_send(sc);
da966bca
JS
1971 } else
1972 ath5k_beacon_update_timers(sc, -1);
21800491
BC
1973 } else {
1974 ath5k_hw_stop_tx_dma(sc->ah, sc->bhalq);
fa1c114f 1975 }
fa1c114f 1976
c6e387a2 1977 ath5k_hw_set_imr(ah, sc->imask);
21800491
BC
1978 mmiowb();
1979 spin_unlock_irqrestore(&sc->block, flags);
fa1c114f
JS
1980}
1981
428cbd4f
NK
1982static void ath5k_tasklet_beacon(unsigned long data)
1983{
1984 struct ath5k_softc *sc = (struct ath5k_softc *) data;
1985
1986 /*
1987 * Software beacon alert--time to send a beacon.
1988 *
1989 * In IBSS mode we use this interrupt just to
1990 * keep track of the next TBTT (target beacon
1991 * transmission time) in order to detect wether
1992 * automatic TSF updates happened.
1993 */
1994 if (sc->opmode == NL80211_IFTYPE_ADHOC) {
1995 /* XXX: only if VEOL suppported */
1996 u64 tsf = ath5k_hw_get_tsf64(sc->ah);
1997 sc->nexttbtt += sc->bintval;
1998 ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
1999 "SWBA nexttbtt: %x hw_tu: %x "
2000 "TSF: %llx\n",
2001 sc->nexttbtt,
2002 TSF_TO_TU(tsf),
2003 (unsigned long long) tsf);
2004 } else {
2005 spin_lock(&sc->block);
2006 ath5k_beacon_send(sc);
2007 spin_unlock(&sc->block);
2008 }
2009}
2010
fa1c114f
JS
2011
2012/********************\
2013* Interrupt handling *
2014\********************/
2015
6a8a3f6b
BR
2016static void
2017ath5k_intr_calibration_poll(struct ath5k_hw *ah)
2018{
2111ac0d
BR
2019 if (time_is_before_eq_jiffies(ah->ah_cal_next_ani) &&
2020 !(ah->ah_cal_mask & AR5K_CALIBRATION_FULL)) {
2021 /* run ANI only when full calibration is not active */
2022 ah->ah_cal_next_ani = jiffies +
2023 msecs_to_jiffies(ATH5K_TUNE_CALIBRATION_INTERVAL_ANI);
2024 tasklet_schedule(&ah->ah_sc->ani_tasklet);
2025
2026 } else if (time_is_before_eq_jiffies(ah->ah_cal_next_full)) {
6a8a3f6b
BR
2027 ah->ah_cal_next_full = jiffies +
2028 msecs_to_jiffies(ATH5K_TUNE_CALIBRATION_INTERVAL_FULL);
2029 tasklet_schedule(&ah->ah_sc->calib);
2030 }
2031 /* we could use SWI to generate enough interrupts to meet our
2032 * calibration interval requirements, if necessary:
2033 * AR5K_REG_ENABLE_BITS(ah, AR5K_CR, AR5K_CR_SWI); */
2034}
2035
fa1c114f
JS
2036static irqreturn_t
2037ath5k_intr(int irq, void *dev_id)
2038{
2039 struct ath5k_softc *sc = dev_id;
2040 struct ath5k_hw *ah = sc->ah;
2041 enum ath5k_int status;
2042 unsigned int counter = 1000;
2043
2044 if (unlikely(test_bit(ATH_STAT_INVALID, sc->status) ||
2045 !ath5k_hw_is_intr_pending(ah)))
2046 return IRQ_NONE;
2047
2048 do {
fa1c114f
JS
2049 ath5k_hw_get_isr(ah, &status); /* NB: clears IRQ too */
2050 ATH5K_DBG(sc, ATH5K_DEBUG_INTR, "status 0x%x/0x%x\n",
2051 status, sc->imask);
fa1c114f
JS
2052 if (unlikely(status & AR5K_INT_FATAL)) {
2053 /*
2054 * Fatal errors are unrecoverable.
2055 * Typically these are caused by DMA errors.
2056 */
8d67a031
BR
2057 ATH5K_DBG(sc, ATH5K_DEBUG_RESET,
2058 "fatal int, resetting\n");
5faaff74 2059 ieee80211_queue_work(sc->hw, &sc->reset_work);
fa1c114f 2060 } else if (unlikely(status & AR5K_INT_RXORN)) {
87d77c4e
BR
2061 /*
2062 * Receive buffers are full. Either the bus is busy or
2063 * the CPU is not fast enough to process all received
2064 * frames.
2065 * Older chipsets need a reset to come out of this
2066 * condition, but we treat it as RX for newer chips.
2067 * We don't know exactly which versions need a reset -
2068 * this guess is copied from the HAL.
2069 */
2070 sc->stats.rxorn_intr++;
8d67a031
BR
2071 if (ah->ah_mac_srev < AR5K_SREV_AR5212) {
2072 ATH5K_DBG(sc, ATH5K_DEBUG_RESET,
2073 "rx overrun, resetting\n");
5faaff74 2074 ieee80211_queue_work(sc->hw, &sc->reset_work);
8d67a031 2075 }
87d77c4e
BR
2076 else
2077 tasklet_schedule(&sc->rxtq);
fa1c114f
JS
2078 } else {
2079 if (status & AR5K_INT_SWBA) {
56d2ac76 2080 tasklet_hi_schedule(&sc->beacontq);
fa1c114f
JS
2081 }
2082 if (status & AR5K_INT_RXEOL) {
2083 /*
2084 * NB: the hardware should re-read the link when
2085 * RXE bit is written, but it doesn't work at
2086 * least on older hardware revs.
2087 */
b3f194e5 2088 sc->stats.rxeol_intr++;
fa1c114f
JS
2089 }
2090 if (status & AR5K_INT_TXURN) {
2091 /* bump tx trigger level */
2092 ath5k_hw_update_tx_triglevel(ah, true);
2093 }
4c674c60 2094 if (status & (AR5K_INT_RXOK | AR5K_INT_RXERR))
fa1c114f 2095 tasklet_schedule(&sc->rxtq);
4c674c60
NK
2096 if (status & (AR5K_INT_TXOK | AR5K_INT_TXDESC
2097 | AR5K_INT_TXERR | AR5K_INT_TXEOL))
fa1c114f
JS
2098 tasklet_schedule(&sc->txtq);
2099 if (status & AR5K_INT_BMISS) {
1e3e6e8f 2100 /* TODO */
fa1c114f
JS
2101 }
2102 if (status & AR5K_INT_MIB) {
2111ac0d 2103 sc->stats.mib_intr++;
495391d7 2104 ath5k_hw_update_mib_counters(ah);
2111ac0d 2105 ath5k_ani_mib_intr(ah);
fa1c114f 2106 }
e6a3b616 2107 if (status & AR5K_INT_GPIO)
e6a3b616 2108 tasklet_schedule(&sc->rf_kill.toggleq);
a6ae0716 2109
fa1c114f 2110 }
2516baa6 2111 } while (ath5k_hw_is_intr_pending(ah) && --counter > 0);
fa1c114f
JS
2112
2113 if (unlikely(!counter))
2114 ATH5K_WARN(sc, "too many interrupts, giving up for now\n");
2115
6a8a3f6b 2116 ath5k_intr_calibration_poll(ah);
6e220662 2117
fa1c114f
JS
2118 return IRQ_HANDLED;
2119}
2120
fa1c114f
JS
2121/*
2122 * Periodically recalibrate the PHY to account
2123 * for temperature/environment changes.
2124 */
2125static void
6e220662 2126ath5k_tasklet_calibrate(unsigned long data)
fa1c114f
JS
2127{
2128 struct ath5k_softc *sc = (void *)data;
2129 struct ath5k_hw *ah = sc->ah;
2130
6e220662 2131 /* Only full calibration for now */
e65e1d77 2132 ah->ah_cal_mask |= AR5K_CALIBRATION_FULL;
6e220662 2133
fa1c114f 2134 ATH5K_DBG(sc, ATH5K_DEBUG_CALIBRATE, "channel %u/%x\n",
400ec45a
LR
2135 ieee80211_frequency_to_channel(sc->curchan->center_freq),
2136 sc->curchan->hw_value);
fa1c114f 2137
6f3b414a 2138 if (ath5k_hw_gainf_calibrate(ah) == AR5K_RFGAIN_NEED_CHANGE) {
fa1c114f
JS
2139 /*
2140 * Rfgain is out of bounds, reset the chip
2141 * to load new gain values.
2142 */
2143 ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "calibration, resetting\n");
5faaff74 2144 ieee80211_queue_work(sc->hw, &sc->reset_work);
fa1c114f
JS
2145 }
2146 if (ath5k_hw_phy_calibrate(ah, sc->curchan))
2147 ATH5K_ERR(sc, "calibration of channel %u failed\n",
400ec45a
LR
2148 ieee80211_frequency_to_channel(
2149 sc->curchan->center_freq));
fa1c114f 2150
0e8e02dd 2151 /* Noise floor calibration interrupts rx/tx path while I/Q calibration
651d9375
BR
2152 * doesn't.
2153 * TODO: We should stop TX here, so that it doesn't interfere.
2154 * Note that stopping the queues is not enough to stop TX! */
afe86286
BR
2155 if (time_is_before_eq_jiffies(ah->ah_cal_next_nf)) {
2156 ah->ah_cal_next_nf = jiffies +
2157 msecs_to_jiffies(ATH5K_TUNE_CALIBRATION_INTERVAL_NF);
afe86286 2158 ath5k_hw_update_noise_floor(ah);
afe86286 2159 }
6e220662 2160
e65e1d77 2161 ah->ah_cal_mask &= ~AR5K_CALIBRATION_FULL;
fa1c114f
JS
2162}
2163
2164
2111ac0d
BR
2165static void
2166ath5k_tasklet_ani(unsigned long data)
2167{
2168 struct ath5k_softc *sc = (void *)data;
2169 struct ath5k_hw *ah = sc->ah;
2170
2171 ah->ah_cal_mask |= AR5K_CALIBRATION_ANI;
2172 ath5k_ani_calibration(ah);
2173 ah->ah_cal_mask &= ~AR5K_CALIBRATION_ANI;
fa1c114f
JS
2174}
2175
2176
4edd761f
BR
2177static void
2178ath5k_tx_complete_poll_work(struct work_struct *work)
2179{
2180 struct ath5k_softc *sc = container_of(work, struct ath5k_softc,
2181 tx_complete_work.work);
2182 struct ath5k_txq *txq;
2183 int i;
2184 bool needreset = false;
2185
2186 for (i = 0; i < ARRAY_SIZE(sc->txqs); i++) {
2187 if (sc->txqs[i].setup) {
2188 txq = &sc->txqs[i];
2189 spin_lock_bh(&txq->lock);
2190 if (txq->txq_len > 0) {
2191 if (txq->txq_poll_mark) {
2192 ATH5K_DBG(sc, ATH5K_DEBUG_XMIT,
2193 "TX queue stuck %d\n",
2194 txq->qnum);
2195 needreset = true;
2196 spin_unlock_bh(&txq->lock);
2197 break;
2198 } else {
2199 txq->txq_poll_mark = true;
2200 }
2201 }
2202 spin_unlock_bh(&txq->lock);
2203 }
2204 }
2205
2206 if (needreset) {
2207 ATH5K_DBG(sc, ATH5K_DEBUG_RESET,
2208 "TX queues stuck, resetting\n");
2209 ath5k_reset(sc, sc->curchan);
2210 }
2211
2212 ieee80211_queue_delayed_work(sc->hw, &sc->tx_complete_work,
2213 msecs_to_jiffies(ATH5K_TX_COMPLETE_POLL_INT));
2214}
2215
2216
8a63facc
BC
2217/*************************\
2218* Initialization routines *
2219\*************************/
fa1c114f
JS
2220
2221static int
8a63facc 2222ath5k_stop_locked(struct ath5k_softc *sc)
cec8db23 2223{
8a63facc 2224 struct ath5k_hw *ah = sc->ah;
cec8db23 2225
8a63facc
BC
2226 ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "invalid %u\n",
2227 test_bit(ATH_STAT_INVALID, sc->status));
2228
2229 /*
2230 * Shutdown the hardware and driver:
2231 * stop output from above
2232 * disable interrupts
2233 * turn off timers
2234 * turn off the radio
2235 * clear transmit machinery
2236 * clear receive machinery
2237 * drain and release tx queues
2238 * reclaim beacon resources
2239 * power down hardware
2240 *
2241 * Note that some of this work is not possible if the
2242 * hardware is gone (invalid).
2243 */
2244 ieee80211_stop_queues(sc->hw);
2245
2246 if (!test_bit(ATH_STAT_INVALID, sc->status)) {
2247 ath5k_led_off(sc);
2248 ath5k_hw_set_imr(ah, 0);
2249 synchronize_irq(sc->pdev->irq);
2250 }
2251 ath5k_txq_cleanup(sc);
2252 if (!test_bit(ATH_STAT_INVALID, sc->status)) {
2253 ath5k_rx_stop(sc);
2254 ath5k_hw_phy_disable(ah);
2255 }
2256
2257 return 0;
cec8db23
BC
2258}
2259
8a63facc
BC
2260static int
2261ath5k_init(struct ath5k_softc *sc)
fa1c114f 2262{
8a63facc
BC
2263 struct ath5k_hw *ah = sc->ah;
2264 struct ath_common *common = ath5k_hw_common(ah);
2265 int ret, i;
fa1c114f 2266
8a63facc
BC
2267 mutex_lock(&sc->lock);
2268
2269 ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "mode %d\n", sc->opmode);
fa1c114f 2270
fa1c114f 2271 /*
8a63facc
BC
2272 * Stop anything previously setup. This is safe
2273 * no matter this is the first time through or not.
fa1c114f 2274 */
8a63facc 2275 ath5k_stop_locked(sc);
fa1c114f 2276
8a63facc
BC
2277 /*
2278 * The basic interface to setting the hardware in a good
2279 * state is ``reset''. On return the hardware is known to
2280 * be powered up and with interrupts disabled. This must
2281 * be followed by initialization of the appropriate bits
2282 * and then setup of the interrupt mask.
2283 */
2284 sc->curchan = sc->hw->conf.channel;
2285 sc->curband = &sc->sbands[sc->curchan->band];
2286 sc->imask = AR5K_INT_RXOK | AR5K_INT_RXERR | AR5K_INT_RXEOL |
2287 AR5K_INT_RXORN | AR5K_INT_TXDESC | AR5K_INT_TXEOL |
2288 AR5K_INT_FATAL | AR5K_INT_GLOBAL | AR5K_INT_MIB;
fa1c114f 2289
8a63facc
BC
2290 ret = ath5k_reset(sc, NULL);
2291 if (ret)
2292 goto done;
fa1c114f 2293
8a63facc
BC
2294 ath5k_rfkill_hw_start(ah);
2295
2296 /*
2297 * Reset the key cache since some parts do not reset the
2298 * contents on initial power up or resume from suspend.
2299 */
2300 for (i = 0; i < common->keymax; i++)
2301 ath_hw_keyreset(common, (u16) i);
2302
2303 ath5k_hw_set_ack_bitrate_high(ah, true);
2304 ret = 0;
2305done:
2306 mmiowb();
2307 mutex_unlock(&sc->lock);
4edd761f
BR
2308
2309 ieee80211_queue_delayed_work(sc->hw, &sc->tx_complete_work,
2310 msecs_to_jiffies(ATH5K_TX_COMPLETE_POLL_INT));
2311
8a63facc
BC
2312 return ret;
2313}
2314
2315static void stop_tasklets(struct ath5k_softc *sc)
2316{
2317 tasklet_kill(&sc->rxtq);
2318 tasklet_kill(&sc->txtq);
2319 tasklet_kill(&sc->calib);
2320 tasklet_kill(&sc->beacontq);
2321 tasklet_kill(&sc->ani_tasklet);
2322}
2323
2324/*
2325 * Stop the device, grabbing the top-level lock to protect
2326 * against concurrent entry through ath5k_init (which can happen
2327 * if another thread does a system call and the thread doing the
2328 * stop is preempted).
2329 */
2330static int
2331ath5k_stop_hw(struct ath5k_softc *sc)
2332{
2333 int ret;
2334
2335 mutex_lock(&sc->lock);
2336 ret = ath5k_stop_locked(sc);
2337 if (ret == 0 && !test_bit(ATH_STAT_INVALID, sc->status)) {
2338 /*
2339 * Don't set the card in full sleep mode!
2340 *
2341 * a) When the device is in this state it must be carefully
2342 * woken up or references to registers in the PCI clock
2343 * domain may freeze the bus (and system). This varies
2344 * by chip and is mostly an issue with newer parts
2345 * (madwifi sources mentioned srev >= 0x78) that go to
2346 * sleep more quickly.
2347 *
2348 * b) On older chips full sleep results a weird behaviour
2349 * during wakeup. I tested various cards with srev < 0x78
2350 * and they don't wake up after module reload, a second
2351 * module reload is needed to bring the card up again.
2352 *
2353 * Until we figure out what's going on don't enable
2354 * full chip reset on any chip (this is what Legacy HAL
2355 * and Sam's HAL do anyway). Instead Perform a full reset
2356 * on the device (same as initial state after attach) and
2357 * leave it idle (keep MAC/BB on warm reset) */
2358 ret = ath5k_hw_on_hold(sc->ah);
2359
2360 ATH5K_DBG(sc, ATH5K_DEBUG_RESET,
2361 "putting device to sleep\n");
fa1c114f 2362 }
8a63facc 2363 ath5k_txbuf_free_skb(sc, sc->bbuf);
fa1c114f 2364
8a63facc
BC
2365 mmiowb();
2366 mutex_unlock(&sc->lock);
2367
2368 stop_tasklets(sc);
2369
4edd761f
BR
2370 cancel_delayed_work_sync(&sc->tx_complete_work);
2371
8a63facc
BC
2372 ath5k_rfkill_hw_stop(sc->ah);
2373
2374 return ret;
fa1c114f
JS
2375}
2376
209d889b
BC
2377/*
2378 * Reset the hardware. If chan is not NULL, then also pause rx/tx
2379 * and change to the given channel.
5faaff74
BC
2380 *
2381 * This should be called with sc->lock.
209d889b 2382 */
fa1c114f 2383static int
209d889b 2384ath5k_reset(struct ath5k_softc *sc, struct ieee80211_channel *chan)
fa1c114f 2385{
fa1c114f
JS
2386 struct ath5k_hw *ah = sc->ah;
2387 int ret;
2388
2389 ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "resetting\n");
fa1c114f 2390
450464de
BC
2391 ath5k_hw_set_imr(ah, 0);
2392 synchronize_irq(sc->pdev->irq);
2393 stop_tasklets(sc);
2394
209d889b 2395 if (chan) {
d7dc1003
JS
2396 ath5k_txq_cleanup(sc);
2397 ath5k_rx_stop(sc);
209d889b
BC
2398
2399 sc->curchan = chan;
2400 sc->curband = &sc->sbands[chan->band];
d7dc1003 2401 }
3355443a 2402 ret = ath5k_hw_reset(ah, sc->opmode, sc->curchan, chan != NULL);
d7dc1003 2403 if (ret) {
fa1c114f
JS
2404 ATH5K_ERR(sc, "can't reset hardware (%d)\n", ret);
2405 goto err;
2406 }
d7dc1003 2407
fa1c114f 2408 ret = ath5k_rx_start(sc);
d7dc1003 2409 if (ret) {
fa1c114f
JS
2410 ATH5K_ERR(sc, "can't start recv logic\n");
2411 goto err;
2412 }
d7dc1003 2413
2111ac0d
BR
2414 ath5k_ani_init(ah, ah->ah_sc->ani_state.ani_mode);
2415
ac559526
BR
2416 ah->ah_cal_next_full = jiffies;
2417 ah->ah_cal_next_ani = jiffies;
afe86286
BR
2418 ah->ah_cal_next_nf = jiffies;
2419
fa1c114f 2420 /*
d7dc1003
JS
2421 * Change channels and update the h/w rate map if we're switching;
2422 * e.g. 11a to 11b/g.
2423 *
2424 * We may be doing a reset in response to an ioctl that changes the
2425 * channel so update any state that might change as a result.
fa1c114f
JS
2426 *
2427 * XXX needed?
2428 */
2429/* ath5k_chan_change(sc, c); */
fa1c114f 2430
d7dc1003
JS
2431 ath5k_beacon_config(sc);
2432 /* intrs are enabled by ath5k_beacon_config */
fa1c114f 2433
397f385b
BR
2434 ieee80211_wake_queues(sc->hw);
2435
fa1c114f
JS
2436 return 0;
2437err:
2438 return ret;
2439}
2440
5faaff74
BC
2441static void ath5k_reset_work(struct work_struct *work)
2442{
2443 struct ath5k_softc *sc = container_of(work, struct ath5k_softc,
2444 reset_work);
2445
2446 mutex_lock(&sc->lock);
2447 ath5k_reset(sc, sc->curchan);
2448 mutex_unlock(&sc->lock);
2449}
2450
8a63facc
BC
2451static int
2452ath5k_attach(struct pci_dev *pdev, struct ieee80211_hw *hw)
fa1c114f
JS
2453{
2454 struct ath5k_softc *sc = hw->priv;
8a63facc
BC
2455 struct ath5k_hw *ah = sc->ah;
2456 struct ath_regulatory *regulatory = ath5k_hw_regulatory(ah);
925e0b06 2457 struct ath5k_txq *txq;
8a63facc 2458 u8 mac[ETH_ALEN] = {};
fa1c114f
JS
2459 int ret;
2460
8a63facc 2461 ATH5K_DBG(sc, ATH5K_DEBUG_ANY, "devid 0x%x\n", pdev->device);
fa1c114f 2462
8a63facc
BC
2463 /*
2464 * Check if the MAC has multi-rate retry support.
2465 * We do this by trying to setup a fake extended
2466 * descriptor. MACs that don't have support will
2467 * return false w/o doing anything. MACs that do
2468 * support it will return true w/o doing anything.
2469 */
2470 ret = ath5k_hw_setup_mrr_tx_desc(ah, NULL, 0, 0, 0, 0, 0, 0);
67d2e2df 2471
8a63facc
BC
2472 if (ret < 0)
2473 goto err;
2474 if (ret > 0)
2475 __set_bit(ATH_STAT_MRRETRY, sc->status);
ccfe5552 2476
8a63facc
BC
2477 /*
2478 * Collect the channel list. The 802.11 layer
2479 * is resposible for filtering this list based
2480 * on settings like the phy mode and regulatory
2481 * domain restrictions.
2482 */
2483 ret = ath5k_setup_bands(hw);
2484 if (ret) {
2485 ATH5K_ERR(sc, "can't get channels\n");
2486 goto err;
2487 }
67d2e2df 2488
8a63facc
BC
2489 /* NB: setup here so ath5k_rate_update is happy */
2490 if (test_bit(AR5K_MODE_11A, ah->ah_modes))
2491 ath5k_setcurmode(sc, AR5K_MODE_11A);
2492 else
2493 ath5k_setcurmode(sc, AR5K_MODE_11B);
fa1c114f 2494
8a63facc
BC
2495 /*
2496 * Allocate tx+rx descriptors and populate the lists.
2497 */
2498 ret = ath5k_desc_alloc(sc, pdev);
2499 if (ret) {
2500 ATH5K_ERR(sc, "can't allocate descriptors\n");
2501 goto err;
2502 }
fa1c114f 2503
8a63facc
BC
2504 /*
2505 * Allocate hardware transmit queues: one queue for
2506 * beacon frames and one data queue for each QoS
2507 * priority. Note that hw functions handle resetting
2508 * these queues at the needed time.
2509 */
2510 ret = ath5k_beaconq_setup(ah);
2511 if (ret < 0) {
2512 ATH5K_ERR(sc, "can't setup a beacon xmit queue\n");
2513 goto err_desc;
2514 }
2515 sc->bhalq = ret;
2516 sc->cabq = ath5k_txq_setup(sc, AR5K_TX_QUEUE_CAB, 0);
2517 if (IS_ERR(sc->cabq)) {
2518 ATH5K_ERR(sc, "can't setup cab queue\n");
2519 ret = PTR_ERR(sc->cabq);
2520 goto err_bhal;
2521 }
fa1c114f 2522
925e0b06
BR
2523 /* This order matches mac80211's queue priority, so we can
2524 * directly use the mac80211 queue number without any mapping */
2525 txq = ath5k_txq_setup(sc, AR5K_TX_QUEUE_DATA, AR5K_WME_AC_VO);
2526 if (IS_ERR(txq)) {
2527 ATH5K_ERR(sc, "can't setup xmit queue\n");
2528 ret = PTR_ERR(txq);
2529 goto err_queues;
2530 }
2531 txq = ath5k_txq_setup(sc, AR5K_TX_QUEUE_DATA, AR5K_WME_AC_VI);
2532 if (IS_ERR(txq)) {
8a63facc 2533 ATH5K_ERR(sc, "can't setup xmit queue\n");
925e0b06 2534 ret = PTR_ERR(txq);
8a63facc
BC
2535 goto err_queues;
2536 }
925e0b06
BR
2537 txq = ath5k_txq_setup(sc, AR5K_TX_QUEUE_DATA, AR5K_WME_AC_BE);
2538 if (IS_ERR(txq)) {
2539 ATH5K_ERR(sc, "can't setup xmit queue\n");
2540 ret = PTR_ERR(txq);
2541 goto err_queues;
2542 }
2543 txq = ath5k_txq_setup(sc, AR5K_TX_QUEUE_DATA, AR5K_WME_AC_BK);
2544 if (IS_ERR(txq)) {
2545 ATH5K_ERR(sc, "can't setup xmit queue\n");
2546 ret = PTR_ERR(txq);
2547 goto err_queues;
2548 }
2549 hw->queues = 4;
fa1c114f 2550
8a63facc
BC
2551 tasklet_init(&sc->rxtq, ath5k_tasklet_rx, (unsigned long)sc);
2552 tasklet_init(&sc->txtq, ath5k_tasklet_tx, (unsigned long)sc);
2553 tasklet_init(&sc->calib, ath5k_tasklet_calibrate, (unsigned long)sc);
2554 tasklet_init(&sc->beacontq, ath5k_tasklet_beacon, (unsigned long)sc);
2555 tasklet_init(&sc->ani_tasklet, ath5k_tasklet_ani, (unsigned long)sc);
be009370 2556
8a63facc 2557 INIT_WORK(&sc->reset_work, ath5k_reset_work);
4edd761f 2558 INIT_DELAYED_WORK(&sc->tx_complete_work, ath5k_tx_complete_poll_work);
fa1c114f 2559
8a63facc
BC
2560 ret = ath5k_eeprom_read_mac(ah, mac);
2561 if (ret) {
2562 ATH5K_ERR(sc, "unable to read address from EEPROM: 0x%04x\n",
2563 sc->pdev->device);
2564 goto err_queues;
e30eb4ab 2565 }
2bed03eb 2566
8a63facc
BC
2567 SET_IEEE80211_PERM_ADDR(hw, mac);
2568 /* All MAC address bits matter for ACKs */
2569 memcpy(sc->bssidmask, ath_bcast_mac, ETH_ALEN);
2570 ath5k_hw_set_bssid_mask(sc->ah, sc->bssidmask);
2571
2572 regulatory->current_rd = ah->ah_capabilities.cap_eeprom.ee_regdomain;
2573 ret = ath_regd_init(regulatory, hw->wiphy, ath5k_reg_notifier);
2574 if (ret) {
2575 ATH5K_ERR(sc, "can't initialize regulatory system\n");
2576 goto err_queues;
2577 }
2578
2579 ret = ieee80211_register_hw(hw);
2580 if (ret) {
2581 ATH5K_ERR(sc, "can't register ieee80211 hw\n");
2582 goto err_queues;
2583 }
2584
2585 if (!ath_is_world_regd(regulatory))
2586 regulatory_hint(hw->wiphy, regulatory->alpha2);
2587
2588 ath5k_init_leds(sc);
2589
2590 ath5k_sysfs_register(sc);
2591
2592 return 0;
2593err_queues:
2594 ath5k_txq_release(sc);
2595err_bhal:
2596 ath5k_hw_release_tx_queue(ah, sc->bhalq);
2597err_desc:
2598 ath5k_desc_free(sc, pdev);
2599err:
2600 return ret;
2601}
2602
2603static void
2604ath5k_detach(struct pci_dev *pdev, struct ieee80211_hw *hw)
2605{
2606 struct ath5k_softc *sc = hw->priv;
2607
2608 /*
2609 * NB: the order of these is important:
2610 * o call the 802.11 layer before detaching ath5k_hw to
2611 * ensure callbacks into the driver to delete global
2612 * key cache entries can be handled
2613 * o reclaim the tx queue data structures after calling
2614 * the 802.11 layer as we'll get called back to reclaim
2615 * node state and potentially want to use them
2616 * o to cleanup the tx queues the hal is called, so detach
2617 * it last
2618 * XXX: ??? detach ath5k_hw ???
2619 * Other than that, it's straightforward...
2620 */
2621 ieee80211_unregister_hw(hw);
2622 ath5k_desc_free(sc, pdev);
2623 ath5k_txq_release(sc);
2624 ath5k_hw_release_tx_queue(sc->ah, sc->bhalq);
2625 ath5k_unregister_leds(sc);
2626
2627 ath5k_sysfs_unregister(sc);
2628 /*
2629 * NB: can't reclaim these until after ieee80211_ifdetach
2630 * returns because we'll get called back to reclaim node
2631 * state and potentially want to use them.
2632 */
2633}
2634
2635/********************\
2636* Mac80211 functions *
2637\********************/
2638
2639static int
2640ath5k_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
2641{
2642 struct ath5k_softc *sc = hw->priv;
925e0b06
BR
2643 u16 qnum = skb_get_queue_mapping(skb);
2644
2645 if (WARN_ON(qnum >= sc->ah->ah_capabilities.cap_queues.q_tx_num)) {
2646 dev_kfree_skb_any(skb);
2647 return 0;
2648 }
8a63facc 2649
925e0b06 2650 return ath5k_tx_queue(hw, skb, &sc->txqs[qnum]);
8a63facc
BC
2651}
2652
2653static int ath5k_start(struct ieee80211_hw *hw)
2654{
2655 return ath5k_init(hw->priv);
2656}
2657
2658static void ath5k_stop(struct ieee80211_hw *hw)
2659{
2660 ath5k_stop_hw(hw->priv);
2661}
2662
2663static int ath5k_add_interface(struct ieee80211_hw *hw,
2664 struct ieee80211_vif *vif)
2665{
2666 struct ath5k_softc *sc = hw->priv;
2667 int ret;
2668
2669 mutex_lock(&sc->lock);
2670 if (sc->vif) {
2671 ret = 0;
2672 goto end;
2673 }
2674
2675 sc->vif = vif;
2676
2677 switch (vif->type) {
2678 case NL80211_IFTYPE_AP:
2679 case NL80211_IFTYPE_STATION:
2680 case NL80211_IFTYPE_ADHOC:
2681 case NL80211_IFTYPE_MESH_POINT:
2682 sc->opmode = vif->type;
2683 break;
2684 default:
2685 ret = -EOPNOTSUPP;
2686 goto end;
2687 }
2688
2689 ATH5K_DBG(sc, ATH5K_DEBUG_MODE, "add interface mode %d\n", sc->opmode);
2690
2691 ath5k_hw_set_lladdr(sc->ah, vif->addr);
2692 ath5k_mode_setup(sc);
2693
2694 ret = 0;
2695end:
2696 mutex_unlock(&sc->lock);
2697 return ret;
2698}
2699
2700static void
2701ath5k_remove_interface(struct ieee80211_hw *hw,
2702 struct ieee80211_vif *vif)
2703{
2704 struct ath5k_softc *sc = hw->priv;
2705 u8 mac[ETH_ALEN] = {};
2706
2707 mutex_lock(&sc->lock);
2708 if (sc->vif != vif)
2709 goto end;
2710
2711 ath5k_hw_set_lladdr(sc->ah, mac);
2712 sc->vif = NULL;
2713end:
2714 mutex_unlock(&sc->lock);
2715}
2716
2717/*
2718 * TODO: Phy disable/diversity etc
2719 */
2720static int
2721ath5k_config(struct ieee80211_hw *hw, u32 changed)
2722{
2723 struct ath5k_softc *sc = hw->priv;
2724 struct ath5k_hw *ah = sc->ah;
2725 struct ieee80211_conf *conf = &hw->conf;
2726 int ret = 0;
2727
2728 mutex_lock(&sc->lock);
2729
2730 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
2731 ret = ath5k_chan_set(sc, conf->channel);
2732 if (ret < 0)
2733 goto unlock;
2734 }
2735
2736 if ((changed & IEEE80211_CONF_CHANGE_POWER) &&
2737 (sc->power_level != conf->power_level)) {
a0823810
NK
2738 sc->power_level = conf->power_level;
2739
2740 /* Half dB steps */
2741 ath5k_hw_set_txpower_limit(ah, (conf->power_level * 2));
2742 }
fa1c114f 2743
2bed03eb
NK
2744 /* TODO:
2745 * 1) Move this on config_interface and handle each case
2746 * separately eg. when we have only one STA vif, use
2747 * AR5K_ANTMODE_SINGLE_AP
2748 *
2749 * 2) Allow the user to change antenna mode eg. when only
2750 * one antenna is present
2751 *
2752 * 3) Allow the user to set default/tx antenna when possible
2753 *
2754 * 4) Default mode should handle 90% of the cases, together
2755 * with fixed a/b and single AP modes we should be able to
2756 * handle 99%. Sectored modes are extreme cases and i still
2757 * haven't found a usage for them. If we decide to support them,
2758 * then we must allow the user to set how many tx antennas we
2759 * have available
2760 */
caec9112 2761 ath5k_hw_set_antenna_mode(ah, ah->ah_ant_mode);
be009370 2762
55aa4e0f 2763unlock:
be009370 2764 mutex_unlock(&sc->lock);
55aa4e0f 2765 return ret;
fa1c114f
JS
2766}
2767
3ac64bee 2768static u64 ath5k_prepare_multicast(struct ieee80211_hw *hw,
22bedad3 2769 struct netdev_hw_addr_list *mc_list)
3ac64bee
JB
2770{
2771 u32 mfilt[2], val;
3ac64bee 2772 u8 pos;
22bedad3 2773 struct netdev_hw_addr *ha;
3ac64bee
JB
2774
2775 mfilt[0] = 0;
2776 mfilt[1] = 1;
2777
22bedad3 2778 netdev_hw_addr_list_for_each(ha, mc_list) {
3ac64bee 2779 /* calculate XOR of eight 6-bit values */
22bedad3 2780 val = get_unaligned_le32(ha->addr + 0);
3ac64bee 2781 pos = (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
22bedad3 2782 val = get_unaligned_le32(ha->addr + 3);
3ac64bee
JB
2783 pos ^= (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
2784 pos &= 0x3f;
2785 mfilt[pos / 32] |= (1 << (pos % 32));
2786 /* XXX: we might be able to just do this instead,
2787 * but not sure, needs testing, if we do use this we'd
2788 * neet to inform below to not reset the mcast */
2789 /* ath5k_hw_set_mcast_filterindex(ah,
22bedad3 2790 * ha->addr[5]); */
3ac64bee
JB
2791 }
2792
2793 return ((u64)(mfilt[1]) << 32) | mfilt[0];
2794}
2795
fa1c114f
JS
2796#define SUPPORTED_FIF_FLAGS \
2797 FIF_PROMISC_IN_BSS | FIF_ALLMULTI | FIF_FCSFAIL | \
2798 FIF_PLCPFAIL | FIF_CONTROL | FIF_OTHER_BSS | \
2799 FIF_BCN_PRBRESP_PROMISC
2800/*
2801 * o always accept unicast, broadcast, and multicast traffic
2802 * o multicast traffic for all BSSIDs will be enabled if mac80211
2803 * says it should be
2804 * o maintain current state of phy ofdm or phy cck error reception.
2805 * If the hardware detects any of these type of errors then
2806 * ath5k_hw_get_rx_filter() will pass to us the respective
2807 * hardware filters to be able to receive these type of frames.
2808 * o probe request frames are accepted only when operating in
2809 * hostap, adhoc, or monitor modes
2810 * o enable promiscuous mode according to the interface state
2811 * o accept beacons:
2812 * - when operating in adhoc mode so the 802.11 layer creates
2813 * node table entries for peers,
2814 * - when operating in station mode for collecting rssi data when
2815 * the station is otherwise quiet, or
2816 * - when scanning
2817 */
2818static void ath5k_configure_filter(struct ieee80211_hw *hw,
2819 unsigned int changed_flags,
2820 unsigned int *new_flags,
3ac64bee 2821 u64 multicast)
fa1c114f
JS
2822{
2823 struct ath5k_softc *sc = hw->priv;
2824 struct ath5k_hw *ah = sc->ah;
3ac64bee 2825 u32 mfilt[2], rfilt;
fa1c114f 2826
56d1de0a
BC
2827 mutex_lock(&sc->lock);
2828
3ac64bee
JB
2829 mfilt[0] = multicast;
2830 mfilt[1] = multicast >> 32;
fa1c114f
JS
2831
2832 /* Only deal with supported flags */
2833 changed_flags &= SUPPORTED_FIF_FLAGS;
2834 *new_flags &= SUPPORTED_FIF_FLAGS;
2835
2836 /* If HW detects any phy or radar errors, leave those filters on.
2837 * Also, always enable Unicast, Broadcasts and Multicast
2838 * XXX: move unicast, bssid broadcasts and multicast to mac80211 */
2839 rfilt = (ath5k_hw_get_rx_filter(ah) & (AR5K_RX_FILTER_PHYERR)) |
2840 (AR5K_RX_FILTER_UCAST | AR5K_RX_FILTER_BCAST |
2841 AR5K_RX_FILTER_MCAST);
2842
2843 if (changed_flags & (FIF_PROMISC_IN_BSS | FIF_OTHER_BSS)) {
2844 if (*new_flags & FIF_PROMISC_IN_BSS) {
fa1c114f 2845 __set_bit(ATH_STAT_PROMISC, sc->status);
0bbac08f 2846 } else {
fa1c114f 2847 __clear_bit(ATH_STAT_PROMISC, sc->status);
0bbac08f 2848 }
fa1c114f
JS
2849 }
2850
6b5dcccb
BC
2851 if (test_bit(ATH_STAT_PROMISC, sc->status))
2852 rfilt |= AR5K_RX_FILTER_PROM;
2853
fa1c114f
JS
2854 /* Note, AR5K_RX_FILTER_MCAST is already enabled */
2855 if (*new_flags & FIF_ALLMULTI) {
2856 mfilt[0] = ~0;
2857 mfilt[1] = ~0;
fa1c114f
JS
2858 }
2859
2860 /* This is the best we can do */
2861 if (*new_flags & (FIF_FCSFAIL | FIF_PLCPFAIL))
2862 rfilt |= AR5K_RX_FILTER_PHYERR;
2863
2864 /* FIF_BCN_PRBRESP_PROMISC really means to enable beacons
30bf4169 2865 * and probes for any BSSID */
fa1c114f 2866 if (*new_flags & FIF_BCN_PRBRESP_PROMISC)
30bf4169 2867 rfilt |= AR5K_RX_FILTER_BEACON;
fa1c114f
JS
2868
2869 /* FIF_CONTROL doc says that if FIF_PROMISC_IN_BSS is not
2870 * set we should only pass on control frames for this
2871 * station. This needs testing. I believe right now this
2872 * enables *all* control frames, which is OK.. but
2873 * but we should see if we can improve on granularity */
2874 if (*new_flags & FIF_CONTROL)
2875 rfilt |= AR5K_RX_FILTER_CONTROL;
2876
2877 /* Additional settings per mode -- this is per ath5k */
2878
2879 /* XXX move these to mac80211, and add a beacon IFF flag to mac80211 */
2880
56d1de0a
BC
2881 switch (sc->opmode) {
2882 case NL80211_IFTYPE_MESH_POINT:
56d1de0a
BC
2883 rfilt |= AR5K_RX_FILTER_CONTROL |
2884 AR5K_RX_FILTER_BEACON |
2885 AR5K_RX_FILTER_PROBEREQ |
2886 AR5K_RX_FILTER_PROM;
2887 break;
2888 case NL80211_IFTYPE_AP:
2889 case NL80211_IFTYPE_ADHOC:
2890 rfilt |= AR5K_RX_FILTER_PROBEREQ |
2891 AR5K_RX_FILTER_BEACON;
2892 break;
2893 case NL80211_IFTYPE_STATION:
2894 if (sc->assoc)
2895 rfilt |= AR5K_RX_FILTER_BEACON;
2896 default:
2897 break;
2898 }
fa1c114f
JS
2899
2900 /* Set filters */
0bbac08f 2901 ath5k_hw_set_rx_filter(ah, rfilt);
fa1c114f
JS
2902
2903 /* Set multicast bits */
2904 ath5k_hw_set_mcast_filter(ah, mfilt[0], mfilt[1]);
a180a130 2905 /* Set the cached hw filter flags, this will later actually
fa1c114f
JS
2906 * be set in HW */
2907 sc->filter_flags = rfilt;
56d1de0a
BC
2908
2909 mutex_unlock(&sc->lock);
fa1c114f
JS
2910}
2911
2912static int
2913ath5k_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
dc822b5d
JB
2914 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
2915 struct ieee80211_key_conf *key)
fa1c114f
JS
2916{
2917 struct ath5k_softc *sc = hw->priv;
dc1e001b
LR
2918 struct ath5k_hw *ah = sc->ah;
2919 struct ath_common *common = ath5k_hw_common(ah);
fa1c114f
JS
2920 int ret = 0;
2921
9ad9a26e
BC
2922 if (modparam_nohwcrypt)
2923 return -EOPNOTSUPP;
2924
97359d12
JB
2925 switch (key->cipher) {
2926 case WLAN_CIPHER_SUITE_WEP40:
2927 case WLAN_CIPHER_SUITE_WEP104:
2928 case WLAN_CIPHER_SUITE_TKIP:
3f64b435 2929 break;
97359d12 2930 case WLAN_CIPHER_SUITE_CCMP:
781f3136 2931 if (common->crypt_caps & ATH_CRYPT_CAP_CIPHER_AESCCM)
1c818740 2932 break;
fa1c114f
JS
2933 return -EOPNOTSUPP;
2934 default:
2935 WARN_ON(1);
2936 return -EINVAL;
2937 }
2938
2939 mutex_lock(&sc->lock);
2940
2941 switch (cmd) {
2942 case SET_KEY:
e0f8c2a9
BR
2943 ret = ath_key_config(common, vif, sta, key);
2944 if (ret >= 0) {
2945 key->hw_key_idx = ret;
2946 /* push IV and Michael MIC generation to stack */
2947 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
2948 if (key->cipher == WLAN_CIPHER_SUITE_TKIP)
2949 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
2950 if (key->cipher == WLAN_CIPHER_SUITE_CCMP)
2951 key->flags |= IEEE80211_KEY_FLAG_SW_MGMT;
2952 ret = 0;
fa1c114f 2953 }
fa1c114f
JS
2954 break;
2955 case DISABLE_KEY:
e0f8c2a9 2956 ath_key_delete(common, key);
fa1c114f
JS
2957 break;
2958 default:
2959 ret = -EINVAL;
fa1c114f
JS
2960 }
2961
8a63facc
BC
2962 mmiowb();
2963 mutex_unlock(&sc->lock);
2964 return ret;
2965}
2966
2967static int
2968ath5k_get_stats(struct ieee80211_hw *hw,
2969 struct ieee80211_low_level_stats *stats)
2970{
2971 struct ath5k_softc *sc = hw->priv;
2972
2973 /* Force update */
2974 ath5k_hw_update_mib_counters(sc->ah);
2975
2976 stats->dot11ACKFailureCount = sc->stats.ack_fail;
2977 stats->dot11RTSFailureCount = sc->stats.rts_fail;
2978 stats->dot11RTSSuccessCount = sc->stats.rts_ok;
2979 stats->dot11FCSErrorCount = sc->stats.fcs_error;
2980
2981 return 0;
2982}
2983
2984static int ath5k_get_survey(struct ieee80211_hw *hw, int idx,
2985 struct survey_info *survey)
2986{
2987 struct ath5k_softc *sc = hw->priv;
2988 struct ieee80211_conf *conf = &hw->conf;
2989
2990 if (idx != 0)
2991 return -ENOENT;
2992
2993 survey->channel = conf->channel;
2994 survey->filled = SURVEY_INFO_NOISE_DBM;
2995 survey->noise = sc->ah->ah_noise_floor;
2996
2997 return 0;
2998}
2999
3000static u64
3001ath5k_get_tsf(struct ieee80211_hw *hw)
3002{
3003 struct ath5k_softc *sc = hw->priv;
3004
3005 return ath5k_hw_get_tsf64(sc->ah);
3006}
3007
3008static void
3009ath5k_set_tsf(struct ieee80211_hw *hw, u64 tsf)
3010{
3011 struct ath5k_softc *sc = hw->priv;
3012
3013 ath5k_hw_set_tsf64(sc->ah, tsf);
3014}
3015
3016static void
3017ath5k_reset_tsf(struct ieee80211_hw *hw)
3018{
3019 struct ath5k_softc *sc = hw->priv;
3020
3021 /*
3022 * in IBSS mode we need to update the beacon timers too.
3023 * this will also reset the TSF if we call it with 0
3024 */
3025 if (sc->opmode == NL80211_IFTYPE_ADHOC)
3026 ath5k_beacon_update_timers(sc, 0);
3027 else
3028 ath5k_hw_reset_tsf(sc->ah);
3029}
3030
3031static void
3032set_beacon_filter(struct ieee80211_hw *hw, bool enable)
3033{
3034 struct ath5k_softc *sc = hw->priv;
3035 struct ath5k_hw *ah = sc->ah;
3036 u32 rfilt;
3037 rfilt = ath5k_hw_get_rx_filter(ah);
3038 if (enable)
3039 rfilt |= AR5K_RX_FILTER_BEACON;
3040 else
3041 rfilt &= ~AR5K_RX_FILTER_BEACON;
3042 ath5k_hw_set_rx_filter(ah, rfilt);
3043 sc->filter_flags = rfilt;
3044}
3045
3046static void ath5k_bss_info_changed(struct ieee80211_hw *hw,
3047 struct ieee80211_vif *vif,
3048 struct ieee80211_bss_conf *bss_conf,
3049 u32 changes)
3050{
3051 struct ath5k_softc *sc = hw->priv;
3052 struct ath5k_hw *ah = sc->ah;
3053 struct ath_common *common = ath5k_hw_common(ah);
3054 unsigned long flags;
3055
3056 mutex_lock(&sc->lock);
3057 if (WARN_ON(sc->vif != vif))
3058 goto unlock;
3059
3060 if (changes & BSS_CHANGED_BSSID) {
3061 /* Cache for later use during resets */
3062 memcpy(common->curbssid, bss_conf->bssid, ETH_ALEN);
3063 common->curaid = 0;
3064 ath5k_hw_set_bssid(ah);
3065 mmiowb();
3066 }
3067
3068 if (changes & BSS_CHANGED_BEACON_INT)
3069 sc->bintval = bss_conf->beacon_int;
3070
3071 if (changes & BSS_CHANGED_ASSOC) {
3072 sc->assoc = bss_conf->assoc;
3073 if (sc->opmode == NL80211_IFTYPE_STATION)
3074 set_beacon_filter(hw, sc->assoc);
3075 ath5k_hw_set_ledstate(sc->ah, sc->assoc ?
3076 AR5K_LED_ASSOC : AR5K_LED_INIT);
3077 if (bss_conf->assoc) {
3078 ATH5K_DBG(sc, ATH5K_DEBUG_ANY,
3079 "Bss Info ASSOC %d, bssid: %pM\n",
3080 bss_conf->aid, common->curbssid);
3081 common->curaid = bss_conf->aid;
3082 ath5k_hw_set_bssid(ah);
3083 /* Once ANI is available you would start it here */
3084 }
3085 }
3086
3087 if (changes & BSS_CHANGED_BEACON) {
3088 spin_lock_irqsave(&sc->block, flags);
3089 ath5k_beacon_update(hw, vif);
3090 spin_unlock_irqrestore(&sc->block, flags);
3091 }
3092
3093 if (changes & BSS_CHANGED_BEACON_ENABLED)
3094 sc->enable_beacon = bss_conf->enable_beacon;
3095
3096 if (changes & (BSS_CHANGED_BEACON | BSS_CHANGED_BEACON_ENABLED |
3097 BSS_CHANGED_BEACON_INT))
3098 ath5k_beacon_config(sc);
3099
3100 unlock:
3101 mutex_unlock(&sc->lock);
3102}
3103
3104static void ath5k_sw_scan_start(struct ieee80211_hw *hw)
3105{
3106 struct ath5k_softc *sc = hw->priv;
3107 if (!sc->assoc)
3108 ath5k_hw_set_ledstate(sc->ah, AR5K_LED_SCAN);
3109}
3110
3111static void ath5k_sw_scan_complete(struct ieee80211_hw *hw)
3112{
3113 struct ath5k_softc *sc = hw->priv;
3114 ath5k_hw_set_ledstate(sc->ah, sc->assoc ?
3115 AR5K_LED_ASSOC : AR5K_LED_INIT);
3116}
3117
3118/**
3119 * ath5k_set_coverage_class - Set IEEE 802.11 coverage class
3120 *
3121 * @hw: struct ieee80211_hw pointer
3122 * @coverage_class: IEEE 802.11 coverage class number
3123 *
3124 * Mac80211 callback. Sets slot time, ACK timeout and CTS timeout for given
3125 * coverage class. The values are persistent, they are restored after device
3126 * reset.
3127 */
3128static void ath5k_set_coverage_class(struct ieee80211_hw *hw, u8 coverage_class)
3129{
3130 struct ath5k_softc *sc = hw->priv;
3131
3132 mutex_lock(&sc->lock);
3133 ath5k_hw_set_coverage_class(sc->ah, coverage_class);
3134 mutex_unlock(&sc->lock);
3135}
3136
3137static const struct ieee80211_ops ath5k_hw_ops = {
3138 .tx = ath5k_tx,
3139 .start = ath5k_start,
3140 .stop = ath5k_stop,
3141 .add_interface = ath5k_add_interface,
3142 .remove_interface = ath5k_remove_interface,
3143 .config = ath5k_config,
3144 .prepare_multicast = ath5k_prepare_multicast,
3145 .configure_filter = ath5k_configure_filter,
3146 .set_key = ath5k_set_key,
3147 .get_stats = ath5k_get_stats,
3148 .get_survey = ath5k_get_survey,
3149 .conf_tx = NULL,
3150 .get_tsf = ath5k_get_tsf,
3151 .set_tsf = ath5k_set_tsf,
3152 .reset_tsf = ath5k_reset_tsf,
3153 .bss_info_changed = ath5k_bss_info_changed,
3154 .sw_scan_start = ath5k_sw_scan_start,
3155 .sw_scan_complete = ath5k_sw_scan_complete,
3156 .set_coverage_class = ath5k_set_coverage_class,
3157};
3158
3159/********************\
3160* PCI Initialization *
3161\********************/
3162
3163static int __devinit
3164ath5k_pci_probe(struct pci_dev *pdev,
3165 const struct pci_device_id *id)
3166{
3167 void __iomem *mem;
3168 struct ath5k_softc *sc;
3169 struct ath_common *common;
3170 struct ieee80211_hw *hw;
3171 int ret;
3172 u8 csz;
3173
3174 /*
3175 * L0s needs to be disabled on all ath5k cards.
3176 *
3177 * For distributions shipping with CONFIG_PCIEASPM (this will be enabled
3178 * by default in the future in 2.6.36) this will also mean both L1 and
3179 * L0s will be disabled when a pre 1.1 PCIe device is detected. We do
3180 * know L1 works correctly even for all ath5k pre 1.1 PCIe devices
3181 * though but cannot currently undue the effect of a blacklist, for
3182 * details you can read pcie_aspm_sanity_check() and see how it adjusts
3183 * the device link capability.
3184 *
3185 * It may be possible in the future to implement some PCI API to allow
3186 * drivers to override blacklists for pre 1.1 PCIe but for now it is
3187 * best to accept that both L0s and L1 will be disabled completely for
3188 * distributions shipping with CONFIG_PCIEASPM rather than having this
3189 * issue present. Motivation for adding this new API will be to help
3190 * with power consumption for some of these devices.
3191 */
3192 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S);
3193
3194 ret = pci_enable_device(pdev);
3195 if (ret) {
3196 dev_err(&pdev->dev, "can't enable device\n");
3197 goto err;
3198 }
3199
3200 /* XXX 32-bit addressing only */
3201 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
3202 if (ret) {
3203 dev_err(&pdev->dev, "32-bit DMA not available\n");
3204 goto err_dis;
3205 }
3206
3207 /*
3208 * Cache line size is used to size and align various
3209 * structures used to communicate with the hardware.
3210 */
3211 pci_read_config_byte(pdev, PCI_CACHE_LINE_SIZE, &csz);
3212 if (csz == 0) {
3213 /*
3214 * Linux 2.4.18 (at least) writes the cache line size
3215 * register as a 16-bit wide register which is wrong.
3216 * We must have this setup properly for rx buffer
3217 * DMA to work so force a reasonable value here if it
3218 * comes up zero.
3219 */
3220 csz = L1_CACHE_BYTES >> 2;
3221 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, csz);
3222 }
3223 /*
3224 * The default setting of latency timer yields poor results,
3225 * set it to the value used by other systems. It may be worth
3226 * tweaking this setting more.
3227 */
3228 pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0xa8);
3229
3230 /* Enable bus mastering */
3231 pci_set_master(pdev);
3232
3233 /*
3234 * Disable the RETRY_TIMEOUT register (0x41) to keep
3235 * PCI Tx retries from interfering with C3 CPU state.
3236 */
3237 pci_write_config_byte(pdev, 0x41, 0);
3238
3239 ret = pci_request_region(pdev, 0, "ath5k");
3240 if (ret) {
3241 dev_err(&pdev->dev, "cannot reserve PCI memory region\n");
3242 goto err_dis;
3243 }
3244
3245 mem = pci_iomap(pdev, 0, 0);
3246 if (!mem) {
3247 dev_err(&pdev->dev, "cannot remap PCI memory region\n") ;
3248 ret = -EIO;
3249 goto err_reg;
3250 }
3251
3252 /*
3253 * Allocate hw (mac80211 main struct)
3254 * and hw->priv (driver private data)
3255 */
3256 hw = ieee80211_alloc_hw(sizeof(*sc), &ath5k_hw_ops);
3257 if (hw == NULL) {
3258 dev_err(&pdev->dev, "cannot allocate ieee80211_hw\n");
3259 ret = -ENOMEM;
3260 goto err_map;
3261 }
3262
3263 dev_info(&pdev->dev, "registered as '%s'\n", wiphy_name(hw->wiphy));
3264
3265 /* Initialize driver private data */
3266 SET_IEEE80211_DEV(hw, &pdev->dev);
3267 hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
3268 IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
3269 IEEE80211_HW_SIGNAL_DBM;
3270
3271 hw->wiphy->interface_modes =
3272 BIT(NL80211_IFTYPE_AP) |
3273 BIT(NL80211_IFTYPE_STATION) |
3274 BIT(NL80211_IFTYPE_ADHOC) |
3275 BIT(NL80211_IFTYPE_MESH_POINT);
3276
3277 hw->extra_tx_headroom = 2;
3278 hw->channel_change_time = 5000;
3279 sc = hw->priv;
3280 sc->hw = hw;
3281 sc->pdev = pdev;
3282
3283 ath5k_debug_init_device(sc);
3284
3285 /*
3286 * Mark the device as detached to avoid processing
3287 * interrupts until setup is complete.
3288 */
3289 __set_bit(ATH_STAT_INVALID, sc->status);
3290
3291 sc->iobase = mem; /* So we can unmap it on detach */
3292 sc->opmode = NL80211_IFTYPE_STATION;
3293 sc->bintval = 1000;
3294 mutex_init(&sc->lock);
3295 spin_lock_init(&sc->rxbuflock);
3296 spin_lock_init(&sc->txbuflock);
3297 spin_lock_init(&sc->block);
3298
3299 /* Set private data */
3300 pci_set_drvdata(pdev, sc);
3301
3302 /* Setup interrupt handler */
3303 ret = request_irq(pdev->irq, ath5k_intr, IRQF_SHARED, "ath", sc);
3304 if (ret) {
3305 ATH5K_ERR(sc, "request_irq failed\n");
3306 goto err_free;
3307 }
3308
3309 /* If we passed the test, malloc an ath5k_hw struct */
3310 sc->ah = kzalloc(sizeof(struct ath5k_hw), GFP_KERNEL);
3311 if (!sc->ah) {
3312 ret = -ENOMEM;
3313 ATH5K_ERR(sc, "out of memory\n");
3314 goto err_irq;
3315 }
3316
3317 sc->ah->ah_sc = sc;
3318 sc->ah->ah_iobase = sc->iobase;
3319 common = ath5k_hw_common(sc->ah);
3320 common->ops = &ath5k_common_ops;
3321 common->ah = sc->ah;
3322 common->hw = hw;
3323 common->cachelsz = csz << 2; /* convert to bytes */
3324
3325 /* Initialize device */
3326 ret = ath5k_hw_attach(sc);
3327 if (ret) {
3328 goto err_free_ah;
3329 }
3330
3331 /* set up multi-rate retry capabilities */
3332 if (sc->ah->ah_version == AR5K_AR5212) {
3333 hw->max_rates = 4;
3334 hw->max_rate_tries = 11;
3335 }
3336
3337 /* Finish private driver data initialization */
3338 ret = ath5k_attach(pdev, hw);
3339 if (ret)
3340 goto err_ah;
3341
3342 ATH5K_INFO(sc, "Atheros AR%s chip found (MAC: 0x%x, PHY: 0x%x)\n",
3343 ath5k_chip_name(AR5K_VERSION_MAC, sc->ah->ah_mac_srev),
3344 sc->ah->ah_mac_srev,
3345 sc->ah->ah_phy_revision);
3346
3347 if (!sc->ah->ah_single_chip) {
3348 /* Single chip radio (!RF5111) */
3349 if (sc->ah->ah_radio_5ghz_revision &&
3350 !sc->ah->ah_radio_2ghz_revision) {
3351 /* No 5GHz support -> report 2GHz radio */
3352 if (!test_bit(AR5K_MODE_11A,
3353 sc->ah->ah_capabilities.cap_mode)) {
3354 ATH5K_INFO(sc, "RF%s 2GHz radio found (0x%x)\n",
3355 ath5k_chip_name(AR5K_VERSION_RAD,
3356 sc->ah->ah_radio_5ghz_revision),
3357 sc->ah->ah_radio_5ghz_revision);
3358 /* No 2GHz support (5110 and some
3359 * 5Ghz only cards) -> report 5Ghz radio */
3360 } else if (!test_bit(AR5K_MODE_11B,
3361 sc->ah->ah_capabilities.cap_mode)) {
3362 ATH5K_INFO(sc, "RF%s 5GHz radio found (0x%x)\n",
3363 ath5k_chip_name(AR5K_VERSION_RAD,
3364 sc->ah->ah_radio_5ghz_revision),
3365 sc->ah->ah_radio_5ghz_revision);
3366 /* Multiband radio */
3367 } else {
3368 ATH5K_INFO(sc, "RF%s multiband radio found"
3369 " (0x%x)\n",
3370 ath5k_chip_name(AR5K_VERSION_RAD,
3371 sc->ah->ah_radio_5ghz_revision),
3372 sc->ah->ah_radio_5ghz_revision);
3373 }
3374 }
3375 /* Multi chip radio (RF5111 - RF2111) ->
3376 * report both 2GHz/5GHz radios */
3377 else if (sc->ah->ah_radio_5ghz_revision &&
3378 sc->ah->ah_radio_2ghz_revision){
3379 ATH5K_INFO(sc, "RF%s 5GHz radio found (0x%x)\n",
3380 ath5k_chip_name(AR5K_VERSION_RAD,
3381 sc->ah->ah_radio_5ghz_revision),
3382 sc->ah->ah_radio_5ghz_revision);
3383 ATH5K_INFO(sc, "RF%s 2GHz radio found (0x%x)\n",
3384 ath5k_chip_name(AR5K_VERSION_RAD,
3385 sc->ah->ah_radio_2ghz_revision),
3386 sc->ah->ah_radio_2ghz_revision);
3387 }
3388 }
3389
55ee82b5 3390
8a63facc
BC
3391 /* ready to process interrupts */
3392 __clear_bit(ATH_STAT_INVALID, sc->status);
55ee82b5
HS
3393
3394 return 0;
8a63facc
BC
3395err_ah:
3396 ath5k_hw_detach(sc->ah);
3397err_free_ah:
3398 kfree(sc->ah);
3399err_irq:
3400 free_irq(pdev->irq, sc);
3401err_free:
3402 ieee80211_free_hw(hw);
3403err_map:
3404 pci_iounmap(pdev, mem);
3405err_reg:
3406 pci_release_region(pdev, 0);
3407err_dis:
3408 pci_disable_device(pdev);
3409err:
3410 return ret;
55ee82b5
HS
3411}
3412
8a63facc
BC
3413static void __devexit
3414ath5k_pci_remove(struct pci_dev *pdev)
fa1c114f 3415{
8a63facc 3416 struct ath5k_softc *sc = pci_get_drvdata(pdev);
fa1c114f 3417
8a63facc
BC
3418 ath5k_debug_finish_device(sc);
3419 ath5k_detach(pdev, sc->hw);
3420 ath5k_hw_detach(sc->ah);
3421 kfree(sc->ah);
3422 free_irq(pdev->irq, sc);
3423 pci_iounmap(pdev, sc->iobase);
3424 pci_release_region(pdev, 0);
3425 pci_disable_device(pdev);
3426 ieee80211_free_hw(sc->hw);
fa1c114f
JS
3427}
3428
8a63facc
BC
3429#ifdef CONFIG_PM_SLEEP
3430static int ath5k_pci_suspend(struct device *dev)
3b5d665b 3431{
8a63facc 3432 struct ath5k_softc *sc = pci_get_drvdata(to_pci_dev(dev));
3b5d665b 3433
8a63facc
BC
3434 ath5k_led_off(sc);
3435 return 0;
3b5d665b
AF
3436}
3437
8a63facc 3438static int ath5k_pci_resume(struct device *dev)
fa1c114f 3439{
8a63facc
BC
3440 struct pci_dev *pdev = to_pci_dev(dev);
3441 struct ath5k_softc *sc = pci_get_drvdata(pdev);
fa1c114f 3442
9804b98d 3443 /*
8a63facc
BC
3444 * Suspend/Resume resets the PCI configuration space, so we have to
3445 * re-disable the RETRY_TIMEOUT register (0x41) to keep
3446 * PCI Tx retries from interfering with C3 CPU state
9804b98d 3447 */
8a63facc
BC
3448 pci_write_config_byte(pdev, 0x41, 0);
3449
3450 ath5k_led_enable(sc);
3451 return 0;
fa1c114f
JS
3452}
3453
8a63facc
BC
3454static SIMPLE_DEV_PM_OPS(ath5k_pm_ops, ath5k_pci_suspend, ath5k_pci_resume);
3455#define ATH5K_PM_OPS (&ath5k_pm_ops)
3456#else
3457#define ATH5K_PM_OPS NULL
3458#endif /* CONFIG_PM_SLEEP */
3459
3460static struct pci_driver ath5k_pci_driver = {
3461 .name = KBUILD_MODNAME,
3462 .id_table = ath5k_pci_id_table,
3463 .probe = ath5k_pci_probe,
3464 .remove = __devexit_p(ath5k_pci_remove),
3465 .driver.pm = ATH5K_PM_OPS,
3466};
3467
1071db86 3468/*
8a63facc 3469 * Module init/exit functions
1071db86 3470 */
8a63facc
BC
3471static int __init
3472init_ath5k_pci(void)
fa1c114f 3473{
fa1c114f 3474 int ret;
57c4d7b4 3475
8a63facc 3476 ath5k_debug_init();
2d0ddec5 3477
8a63facc
BC
3478 ret = pci_register_driver(&ath5k_pci_driver);
3479 if (ret) {
3480 printk(KERN_ERR "ath5k_pci: can't register pci driver\n");
3481 return ret;
2d0ddec5
JB
3482 }
3483
8a63facc 3484 return 0;
02969b38 3485}
f0f3d388 3486
8a63facc
BC
3487static void __exit
3488exit_ath5k_pci(void)
f0f3d388 3489{
8a63facc 3490 pci_unregister_driver(&ath5k_pci_driver);
f0f3d388 3491
8a63facc 3492 ath5k_debug_finish();
f0f3d388 3493}
6e08d228 3494
8a63facc
BC
3495module_init(init_ath5k_pci);
3496module_exit(exit_ath5k_pci);