blob: 2c2ea1539849a2ddde9ebdd37e7ed416a429731d [file] [log] [blame]
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001/*
2 * Copyright (c) 2004-2008 Reyk Floeter <reyk@openbsd.org>
3 * Copyright (c) 2006-2008 Nick Kossifidis <mickflemm@gmail.com>
4 * Copyright (c) 2007-2008 Matthew W. S. Bell <mentor@madwifi.org>
5 * Copyright (c) 2007-2008 Luis Rodriguez <mcgrof@winlab.rutgers.edu>
6 * Copyright (c) 2007-2008 Pavel Roskin <proski@gnu.org>
7 * Copyright (c) 2007-2008 Jiri Slaby <jirislaby@gmail.com>
8 *
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 *
21 */
22
23/*********************************\
24* Protocol Control Unit Functions *
25\*********************************/
26
Luis R. Rodriguezbcd8f542009-09-09 22:43:17 -070027#include <asm/unaligned.h>
28
Nick Kossifidisc6e387a2008-08-29 22:45:39 +030029#include "ath5k.h"
30#include "reg.h"
31#include "debug.h"
32#include "base.h"
33
34/*******************\
Nick Kossifidis9320b5c2010-11-23 20:36:45 +020035* Helper functions *
Nick Kossifidisc6e387a2008-08-29 22:45:39 +030036\*******************/
37
38/**
Nick Kossifidis9320b5c2010-11-23 20:36:45 +020039 * ath5k_hw_get_default_slottime - Get the default slot time for current mode
Nick Kossifidisc6e387a2008-08-29 22:45:39 +030040 *
41 * @ah: The &struct ath5k_hw
Nick Kossifidisc6e387a2008-08-29 22:45:39 +030042 */
Nick Kossifidis9320b5c2010-11-23 20:36:45 +020043static unsigned int ath5k_hw_get_default_slottime(struct ath5k_hw *ah)
Nick Kossifidisc6e387a2008-08-29 22:45:39 +030044{
Nick Kossifidis9320b5c2010-11-23 20:36:45 +020045 struct ieee80211_channel *channel = ah->ah_current_channel;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +030046
Nick Kossifidis9320b5c2010-11-23 20:36:45 +020047 if (channel->hw_value & CHANNEL_TURBO)
48 return 6; /* both turbo modes */
Nick Kossifidisf07a6c42008-10-29 04:28:28 +020049
Nick Kossifidis9320b5c2010-11-23 20:36:45 +020050 if (channel->hw_value & CHANNEL_CCK)
51 return 20; /* 802.11b */
Nick Kossifidisf07a6c42008-10-29 04:28:28 +020052
Nick Kossifidis9320b5c2010-11-23 20:36:45 +020053 return 9; /* 802.11 a/g */
Nick Kossifidisc6e387a2008-08-29 22:45:39 +030054}
55
56/**
Nick Kossifidis9320b5c2010-11-23 20:36:45 +020057 * ath5k_hw_get_default_sifs - Get the default SIFS for current mode
58 *
59 * @ah: The &struct ath5k_hw
60 */
61static unsigned int ath5k_hw_get_default_sifs(struct ath5k_hw *ah)
62{
63 struct ieee80211_channel *channel = ah->ah_current_channel;
64
65 if (channel->hw_value & CHANNEL_TURBO)
66 return 8; /* both turbo modes */
67
68 if (channel->hw_value & CHANNEL_5GHZ)
69 return 16; /* 802.11a */
70
71 return 10; /* 802.11 b/g */
72}
73
74/**
75 * ath5k_hw_update_mib_counters - Update MIB counters (mac layer statistics)
Nick Kossifidisc6e387a2008-08-29 22:45:39 +030076 *
77 * @ah: The &struct ath5k_hw
Nick Kossifidisc6e387a2008-08-29 22:45:39 +030078 *
Bruno Randolf495391d2010-03-25 14:49:36 +090079 * Reads MIB counters from PCU and updates sw statistics. Is called after a
80 * MIB interrupt, because one of these counters might have reached their maximum
81 * and triggered the MIB interrupt, to let us read and clear the counter.
82 *
83 * Is called in interrupt context!
Nick Kossifidisc6e387a2008-08-29 22:45:39 +030084 */
Bruno Randolf495391d2010-03-25 14:49:36 +090085void ath5k_hw_update_mib_counters(struct ath5k_hw *ah)
Nick Kossifidisc6e387a2008-08-29 22:45:39 +030086{
Bruno Randolf495391d2010-03-25 14:49:36 +090087 struct ath5k_statistics *stats = &ah->ah_sc->stats;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +030088
89 /* Read-And-Clear */
Bruno Randolf495391d2010-03-25 14:49:36 +090090 stats->ack_fail += ath5k_hw_reg_read(ah, AR5K_ACK_FAIL);
91 stats->rts_fail += ath5k_hw_reg_read(ah, AR5K_RTS_FAIL);
92 stats->rts_ok += ath5k_hw_reg_read(ah, AR5K_RTS_OK);
93 stats->fcs_error += ath5k_hw_reg_read(ah, AR5K_FCS_FAIL);
94 stats->beacons += ath5k_hw_reg_read(ah, AR5K_BEACON_CNT);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +030095}
96
97/**
98 * ath5k_hw_set_ack_bitrate - set bitrate for ACKs
99 *
100 * @ah: The &struct ath5k_hw
Bob Copeland8801df82010-08-21 16:39:02 -0400101 * @high: Flag to determine if we want to use high transmission rate
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300102 * for ACKs or not
103 *
104 * If high flag is set, we tell hw to use a set of control rates based on
Bob Copeland8801df82010-08-21 16:39:02 -0400105 * the current transmission rate (check out control_rates array inside reset.c).
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300106 * If not hw just uses the lowest rate available for the current modulation
107 * scheme being used (1Mbit for CCK and 6Mbits for OFDM).
108 */
109void ath5k_hw_set_ack_bitrate_high(struct ath5k_hw *ah, bool high)
110{
111 if (ah->ah_version != AR5K_AR5212)
112 return;
113 else {
114 u32 val = AR5K_STA_ID1_BASE_RATE_11B | AR5K_STA_ID1_ACKCTS_6MB;
115 if (high)
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300116 AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1, val);
Bruno Randolf0edc9a62010-04-12 16:38:47 +0900117 else
118 AR5K_REG_ENABLE_BITS(ah, AR5K_STA_ID1, val);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300119 }
120}
121
122
123/******************\
124* ACK/CTS Timeouts *
125\******************/
126
Nick Kossifidis9320b5c2010-11-23 20:36:45 +0200127/*
128 * index into rates for control rates, we can set it up like this because
129 * this is only used for AR5212 and we know it supports G mode
130 */
131static const unsigned int control_rates[] =
132 { 0, 1, 1, 1, 4, 4, 6, 6, 8, 8, 8, 8 };
133
134/**
135 * ath5k_hw_write_rate_duration - fill rate code to duration table
136 *
137 * @ah: the &struct ath5k_hw
138 * @mode: one of enum ath5k_driver_mode
139 *
140 * Write the rate code to duration table upon hw reset. This is a helper for
141 * ath5k_hw_reset(). It seems all this is doing is setting an ACK timeout on
142 * the hardware, based on current mode, for each rate. The rates which are
143 * capable of short preamble (802.11b rates 2Mbps, 5.5Mbps, and 11Mbps) have
144 * different rate code so we write their value twice (one for long preamble
145 * and one for short).
146 *
147 * Note: Band doesn't matter here, if we set the values for OFDM it works
148 * on both a and g modes. So all we have to do is set values for all g rates
149 * that include all OFDM and CCK rates. If we operate in turbo or xr/half/
150 * quarter rate mode, we need to use another set of bitrates (that's why we
151 * need the mode parameter) but we don't handle these proprietary modes yet.
152 */
153static inline void ath5k_hw_write_rate_duration(struct ath5k_hw *ah,
154 unsigned int mode)
155{
156 struct ath5k_softc *sc = ah->ah_sc;
157 struct ieee80211_rate *rate;
158 unsigned int i;
159
160 /* Write rate duration table */
161 for (i = 0; i < sc->sbands[IEEE80211_BAND_2GHZ].n_bitrates; i++) {
162 u32 reg;
163 u16 tx_time;
164
165 rate = &sc->sbands[IEEE80211_BAND_2GHZ].bitrates[control_rates[i]];
166
167 /* Set ACK timeout */
168 reg = AR5K_RATE_DUR(rate->hw_value);
169
170 /* An ACK frame consists of 10 bytes. If you add the FCS,
171 * which ieee80211_generic_frame_duration() adds,
172 * its 14 bytes. Note we use the control rate and not the
173 * actual rate for this rate. See mac80211 tx.c
174 * ieee80211_duration() for a brief description of
175 * what rate we should choose to TX ACKs. */
176 tx_time = le16_to_cpu(ieee80211_generic_frame_duration(sc->hw,
177 NULL, 10, rate));
178
179 ath5k_hw_reg_write(ah, tx_time, reg);
180
181 if (!(rate->flags & IEEE80211_RATE_SHORT_PREAMBLE))
182 continue;
183
184 /*
185 * We're not distinguishing short preamble here,
186 * This is true, all we'll get is a longer value here
187 * which is not necessarilly bad. We could use
188 * export ieee80211_frame_duration() but that needs to be
189 * fixed first to be properly used by mac802111 drivers:
190 *
191 * - remove erp stuff and let the routine figure ofdm
192 * erp rates
193 * - remove passing argument ieee80211_local as
194 * drivers don't have access to it
195 * - move drivers using ieee80211_generic_frame_duration()
196 * to this
197 */
198 ath5k_hw_reg_write(ah, tx_time,
199 reg + (AR5K_SET_SHORT_PREAMBLE << 2));
200 }
201}
202
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300203/**
204 * ath5k_hw_set_ack_timeout - Set ACK timeout on PCU
205 *
206 * @ah: The &struct ath5k_hw
207 * @timeout: Timeout in usec
208 */
Pavel Roskin626ede62010-02-18 20:28:02 -0500209static int ath5k_hw_set_ack_timeout(struct ath5k_hw *ah, unsigned int timeout)
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300210{
Lukáš Turek3578e6e2009-12-21 22:50:50 +0100211 if (ath5k_hw_clocktoh(ah, AR5K_REG_MS(0xffffffff, AR5K_TIME_OUT_ACK))
212 <= timeout)
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300213 return -EINVAL;
214
215 AR5K_REG_WRITE_BITS(ah, AR5K_TIME_OUT, AR5K_TIME_OUT_ACK,
Lukáš Turek3578e6e2009-12-21 22:50:50 +0100216 ath5k_hw_htoclock(ah, timeout));
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300217
218 return 0;
219}
220
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300221/**
222 * ath5k_hw_set_cts_timeout - Set CTS timeout on PCU
223 *
224 * @ah: The &struct ath5k_hw
225 * @timeout: Timeout in usec
226 */
Pavel Roskin626ede62010-02-18 20:28:02 -0500227static int ath5k_hw_set_cts_timeout(struct ath5k_hw *ah, unsigned int timeout)
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300228{
Lukáš Turek3578e6e2009-12-21 22:50:50 +0100229 if (ath5k_hw_clocktoh(ah, AR5K_REG_MS(0xffffffff, AR5K_TIME_OUT_CTS))
230 <= timeout)
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300231 return -EINVAL;
232
233 AR5K_REG_WRITE_BITS(ah, AR5K_TIME_OUT, AR5K_TIME_OUT_CTS,
Lukáš Turek3578e6e2009-12-21 22:50:50 +0100234 ath5k_hw_htoclock(ah, timeout));
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300235
236 return 0;
237}
238
Lukáš Turek3578e6e2009-12-21 22:50:50 +0100239
Nick Kossifidis9320b5c2010-11-23 20:36:45 +0200240/*******************\
241* RX filter Control *
242\*******************/
Lukáš Turek6e08d222009-12-21 22:50:51 +0100243
244/**
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300245 * ath5k_hw_set_lladdr - Set station id
246 *
247 * @ah: The &struct ath5k_hw
248 * @mac: The card's mac address
249 *
250 * Set station id on hw using the provided mac address
251 */
252int ath5k_hw_set_lladdr(struct ath5k_hw *ah, const u8 *mac)
253{
Luis R. Rodriguez954fece2009-09-10 10:51:33 -0700254 struct ath_common *common = ath5k_hw_common(ah);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300255 u32 low_id, high_id;
Bob Copelandf6bac3e2008-11-26 16:17:11 -0500256 u32 pcu_reg;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300257
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300258 /* Set new station ID */
Luis R. Rodriguez954fece2009-09-10 10:51:33 -0700259 memcpy(common->macaddr, mac, ETH_ALEN);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300260
Bob Copelandf6bac3e2008-11-26 16:17:11 -0500261 pcu_reg = ath5k_hw_reg_read(ah, AR5K_STA_ID1) & 0xffff0000;
262
Luis R. Rodriguezbcd8f542009-09-09 22:43:17 -0700263 low_id = get_unaligned_le32(mac);
264 high_id = get_unaligned_le16(mac + 4);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300265
266 ath5k_hw_reg_write(ah, low_id, AR5K_STA_ID0);
Bob Copelandf6bac3e2008-11-26 16:17:11 -0500267 ath5k_hw_reg_write(ah, pcu_reg | high_id, AR5K_STA_ID1);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300268
269 return 0;
270}
271
272/**
Nick Kossifidis418de6d2010-08-15 13:03:10 -0400273 * ath5k_hw_set_bssid - Set current BSSID on hw
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300274 *
275 * @ah: The &struct ath5k_hw
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300276 *
Nick Kossifidis418de6d2010-08-15 13:03:10 -0400277 * Sets the current BSSID and BSSID mask we have from the
278 * common struct into the hardware
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300279 */
Nick Kossifidis418de6d2010-08-15 13:03:10 -0400280void ath5k_hw_set_bssid(struct ath5k_hw *ah)
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300281{
Luis R. Rodriguez954fece2009-09-10 10:51:33 -0700282 struct ath_common *common = ath5k_hw_common(ah);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300283 u16 tim_offset = 0;
284
285 /*
Nick Kossifidis418de6d2010-08-15 13:03:10 -0400286 * Set BSSID mask on 5212
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300287 */
Luis R. Rodrigueza72d57a2009-10-06 20:44:29 -0400288 if (ah->ah_version == AR5K_AR5212)
289 ath_hw_setbssidmask(common);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300290
291 /*
Nick Kossifidis418de6d2010-08-15 13:03:10 -0400292 * Set BSSID
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300293 */
Luis R. Rodriguezabba0682009-10-06 20:44:32 -0400294 ath5k_hw_reg_write(ah,
295 get_unaligned_le32(common->curbssid),
Luis R. Rodrigueza3f86bf2009-10-06 20:44:33 -0400296 AR5K_BSS_ID0);
Luis R. Rodriguezabba0682009-10-06 20:44:32 -0400297 ath5k_hw_reg_write(ah,
298 get_unaligned_le16(common->curbssid + 4) |
299 ((common->curaid & 0x3fff) << AR5K_BSS_ID1_AID_S),
Luis R. Rodrigueza3f86bf2009-10-06 20:44:33 -0400300 AR5K_BSS_ID1);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300301
Luis R. Rodriguezbe5d6b72009-10-06 20:44:31 -0400302 if (common->curaid == 0) {
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300303 ath5k_hw_disable_pspoll(ah);
304 return;
305 }
306
307 AR5K_REG_WRITE_BITS(ah, AR5K_BEACON, AR5K_BEACON_TIM,
Luis R. Rodriguezabba0682009-10-06 20:44:32 -0400308 tim_offset ? tim_offset + 4 : 0);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300309
310 ath5k_hw_enable_pspoll(ah, NULL, 0);
311}
312
Luis R. Rodriguez13b81552009-09-10 17:52:45 -0700313void ath5k_hw_set_bssid_mask(struct ath5k_hw *ah, const u8 *mask)
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300314{
Luis R. Rodriguez954fece2009-09-10 10:51:33 -0700315 struct ath_common *common = ath5k_hw_common(ah);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300316
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200317 /* Cache bssid mask so that we can restore it
318 * on reset */
Luis R. Rodriguez954fece2009-09-10 10:51:33 -0700319 memcpy(common->bssidmask, mask, ETH_ALEN);
Luis R. Rodriguez13b81552009-09-10 17:52:45 -0700320 if (ah->ah_version == AR5K_AR5212)
321 ath_hw_setbssidmask(common);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300322}
323
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300324/*
325 * Set multicast filter
326 */
327void ath5k_hw_set_mcast_filter(struct ath5k_hw *ah, u32 filter0, u32 filter1)
328{
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300329 ath5k_hw_reg_write(ah, filter0, AR5K_MCAST_FILTER0);
330 ath5k_hw_reg_write(ah, filter1, AR5K_MCAST_FILTER1);
331}
332
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300333/**
334 * ath5k_hw_get_rx_filter - Get current rx filter
335 *
336 * @ah: The &struct ath5k_hw
337 *
338 * Returns the RX filter by reading rx filter and
339 * phy error filter registers. RX filter is used
340 * to set the allowed frame types that PCU will accept
341 * and pass to the driver. For a list of frame types
342 * check out reg.h.
343 */
344u32 ath5k_hw_get_rx_filter(struct ath5k_hw *ah)
345{
346 u32 data, filter = 0;
347
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300348 filter = ath5k_hw_reg_read(ah, AR5K_RX_FILTER);
349
350 /*Radar detection for 5212*/
351 if (ah->ah_version == AR5K_AR5212) {
352 data = ath5k_hw_reg_read(ah, AR5K_PHY_ERR_FIL);
353
354 if (data & AR5K_PHY_ERR_FIL_RADAR)
355 filter |= AR5K_RX_FILTER_RADARERR;
356 if (data & (AR5K_PHY_ERR_FIL_OFDM | AR5K_PHY_ERR_FIL_CCK))
357 filter |= AR5K_RX_FILTER_PHYERR;
358 }
359
360 return filter;
361}
362
363/**
364 * ath5k_hw_set_rx_filter - Set rx filter
365 *
366 * @ah: The &struct ath5k_hw
367 * @filter: RX filter mask (see reg.h)
368 *
369 * Sets RX filter register and also handles PHY error filter
370 * register on 5212 and newer chips so that we have proper PHY
371 * error reporting.
372 */
373void ath5k_hw_set_rx_filter(struct ath5k_hw *ah, u32 filter)
374{
375 u32 data = 0;
376
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300377 /* Set PHY error filter register on 5212*/
378 if (ah->ah_version == AR5K_AR5212) {
379 if (filter & AR5K_RX_FILTER_RADARERR)
380 data |= AR5K_PHY_ERR_FIL_RADAR;
381 if (filter & AR5K_RX_FILTER_PHYERR)
382 data |= AR5K_PHY_ERR_FIL_OFDM | AR5K_PHY_ERR_FIL_CCK;
383 }
384
385 /*
386 * The AR5210 uses promiscous mode to detect radar activity
387 */
388 if (ah->ah_version == AR5K_AR5210 &&
389 (filter & AR5K_RX_FILTER_RADARERR)) {
390 filter &= ~AR5K_RX_FILTER_RADARERR;
391 filter |= AR5K_RX_FILTER_PROM;
392 }
393
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200394 /*Zero length DMA (phy error reporting) */
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300395 if (data)
396 AR5K_REG_ENABLE_BITS(ah, AR5K_RXCFG, AR5K_RXCFG_ZLFDMA);
397 else
398 AR5K_REG_DISABLE_BITS(ah, AR5K_RXCFG, AR5K_RXCFG_ZLFDMA);
399
400 /*Write RX Filter register*/
401 ath5k_hw_reg_write(ah, filter & 0xff, AR5K_RX_FILTER);
402
403 /*Write PHY error filter register on 5212*/
404 if (ah->ah_version == AR5K_AR5212)
405 ath5k_hw_reg_write(ah, data, AR5K_PHY_ERR_FIL);
406
407}
408
409
410/****************\
411* Beacon control *
412\****************/
413
Benoit Papillault1c0fc652010-04-16 00:07:26 +0200414#define ATH5K_MAX_TSF_READ 10
415
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300416/**
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300417 * ath5k_hw_get_tsf64 - Get the full 64bit TSF
418 *
419 * @ah: The &struct ath5k_hw
420 *
421 * Returns the current TSF
422 */
423u64 ath5k_hw_get_tsf64(struct ath5k_hw *ah)
424{
Benoit Papillault1c0fc652010-04-16 00:07:26 +0200425 u32 tsf_lower, tsf_upper1, tsf_upper2;
426 int i;
Bruno Randolf28df8972010-09-27 12:22:32 +0900427 unsigned long flags;
428
429 /* This code is time critical - we don't want to be interrupted here */
430 local_irq_save(flags);
Benoit Papillault1c0fc652010-04-16 00:07:26 +0200431
432 /*
433 * While reading TSF upper and then lower part, the clock is still
434 * counting (or jumping in case of IBSS merge) so we might get
435 * inconsistent values. To avoid this, we read the upper part again
436 * and check it has not been changed. We make the hypothesis that a
437 * maximum of 3 changes can happens in a row (we use 10 as a safe
438 * value).
439 *
440 * Impact on performance is pretty small, since in most cases, only
441 * 3 register reads are needed.
442 */
443
444 tsf_upper1 = ath5k_hw_reg_read(ah, AR5K_TSF_U32);
445 for (i = 0; i < ATH5K_MAX_TSF_READ; i++) {
446 tsf_lower = ath5k_hw_reg_read(ah, AR5K_TSF_L32);
447 tsf_upper2 = ath5k_hw_reg_read(ah, AR5K_TSF_U32);
448 if (tsf_upper2 == tsf_upper1)
449 break;
450 tsf_upper1 = tsf_upper2;
451 }
452
Bruno Randolf28df8972010-09-27 12:22:32 +0900453 local_irq_restore(flags);
454
Benoit Papillault1c0fc652010-04-16 00:07:26 +0200455 WARN_ON( i == ATH5K_MAX_TSF_READ );
456
Benoit Papillault1c0fc652010-04-16 00:07:26 +0200457 return (((u64)tsf_upper1 << 32) | tsf_lower);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300458}
459
460/**
Alina Friedrichsen8cab7582009-01-23 05:39:13 +0100461 * ath5k_hw_set_tsf64 - Set a new 64bit TSF
462 *
463 * @ah: The &struct ath5k_hw
464 * @tsf64: The new 64bit TSF
465 *
466 * Sets the new TSF
467 */
468void ath5k_hw_set_tsf64(struct ath5k_hw *ah, u64 tsf64)
469{
Alina Friedrichsen8cab7582009-01-23 05:39:13 +0100470 ath5k_hw_reg_write(ah, tsf64 & 0xffffffff, AR5K_TSF_L32);
Alina Friedrichsen0ad65bd2009-03-02 23:29:48 +0100471 ath5k_hw_reg_write(ah, (tsf64 >> 32) & 0xffffffff, AR5K_TSF_U32);
Alina Friedrichsen8cab7582009-01-23 05:39:13 +0100472}
473
474/**
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300475 * ath5k_hw_reset_tsf - Force a TSF reset
476 *
477 * @ah: The &struct ath5k_hw
478 *
479 * Forces a TSF reset on PCU
480 */
481void ath5k_hw_reset_tsf(struct ath5k_hw *ah)
482{
Bob Copeland14be9942008-09-28 12:09:43 -0400483 u32 val;
484
Bob Copeland14be9942008-09-28 12:09:43 -0400485 val = ath5k_hw_reg_read(ah, AR5K_BEACON) | AR5K_BEACON_RESET_TSF;
486
487 /*
488 * Each write to the RESET_TSF bit toggles a hardware internal
489 * signal to reset TSF, but if left high it will cause a TSF reset
490 * on the next chip reset as well. Thus we always write the value
491 * twice to clear the signal.
492 */
493 ath5k_hw_reg_write(ah, val, AR5K_BEACON);
494 ath5k_hw_reg_write(ah, val, AR5K_BEACON);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300495}
496
497/*
498 * Initialize beacon timers
499 */
500void ath5k_hw_init_beacon(struct ath5k_hw *ah, u32 next_beacon, u32 interval)
501{
502 u32 timer1, timer2, timer3;
503
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300504 /*
505 * Set the additional timers by mode
506 */
Bruno Randolfccfe5552010-03-09 16:55:38 +0900507 switch (ah->ah_sc->opmode) {
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200508 case NL80211_IFTYPE_MONITOR:
Johannes Berg05c914f2008-09-11 00:01:58 +0200509 case NL80211_IFTYPE_STATION:
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200510 /* In STA mode timer1 is used as next wakeup
511 * timer and timer2 as next CFP duration start
512 * timer. Both in 1/8TUs. */
513 /* TODO: PCF handling */
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300514 if (ah->ah_version == AR5K_AR5210) {
515 timer1 = 0xffffffff;
516 timer2 = 0xffffffff;
517 } else {
518 timer1 = 0x0000ffff;
519 timer2 = 0x0007ffff;
520 }
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200521 /* Mark associated AP as PCF incapable for now */
522 AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1, AR5K_STA_ID1_PCF);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300523 break;
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200524 case NL80211_IFTYPE_ADHOC:
525 AR5K_REG_ENABLE_BITS(ah, AR5K_TXCFG, AR5K_TXCFG_ADHOC_BCN_ATIM);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300526 default:
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200527 /* On non-STA modes timer1 is used as next DMA
528 * beacon alert (DBA) timer and timer2 as next
529 * software beacon alert. Both in 1/8TUs. */
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300530 timer1 = (next_beacon - AR5K_TUNE_DMA_BEACON_RESP) << 3;
531 timer2 = (next_beacon - AR5K_TUNE_SW_BEACON_RESP) << 3;
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200532 break;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300533 }
534
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200535 /* Timer3 marks the end of our ATIM window
536 * a zero length window is not allowed because
537 * we 'll get no beacons */
Bruno Randolf4a79f2c2010-09-27 12:22:16 +0900538 timer3 = next_beacon + 1;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300539
540 /*
541 * Set the beacon register and enable all timers.
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300542 */
Nick Kossifidis35edf8a2009-06-12 16:09:53 -0700543 /* When in AP or Mesh Point mode zero timer0 to start TSF */
Bruno Randolfccfe5552010-03-09 16:55:38 +0900544 if (ah->ah_sc->opmode == NL80211_IFTYPE_AP ||
545 ah->ah_sc->opmode == NL80211_IFTYPE_MESH_POINT)
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200546 ath5k_hw_reg_write(ah, 0, AR5K_TIMER0);
Nick Kossifidis428cbd42009-04-30 15:55:47 -0400547
548 ath5k_hw_reg_write(ah, next_beacon, AR5K_TIMER0);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300549 ath5k_hw_reg_write(ah, timer1, AR5K_TIMER1);
550 ath5k_hw_reg_write(ah, timer2, AR5K_TIMER2);
551 ath5k_hw_reg_write(ah, timer3, AR5K_TIMER3);
552
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200553 /* Force a TSF reset if requested and enable beacons */
554 if (interval & AR5K_BEACON_RESET_TSF)
555 ath5k_hw_reset_tsf(ah);
556
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300557 ath5k_hw_reg_write(ah, interval & (AR5K_BEACON_PERIOD |
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200558 AR5K_BEACON_ENABLE),
559 AR5K_BEACON);
560
561 /* Flush any pending BMISS interrupts on ISR by
562 * performing a clear-on-write operation on PISR
563 * register for the BMISS bit (writing a bit on
564 * ISR togles a reset for that bit and leaves
565 * the rest bits intact) */
566 if (ah->ah_version == AR5K_AR5210)
567 ath5k_hw_reg_write(ah, AR5K_ISR_BMISS, AR5K_ISR);
568 else
569 ath5k_hw_reg_write(ah, AR5K_ISR_BMISS, AR5K_PISR);
570
571 /* TODO: Set enchanced sleep registers on AR5212
572 * based on vif->bss_conf params, until then
573 * disable power save reporting.*/
574 AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1, AR5K_STA_ID1_PWR_SV);
575
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300576}
577
Lukáš Turek6e08d222009-12-21 22:50:51 +0100578/**
Bruno Randolf7f896122010-09-27 12:22:21 +0900579 * ath5k_check_timer_win - Check if timer B is timer A + window
580 *
581 * @a: timer a (before b)
582 * @b: timer b (after a)
583 * @window: difference between a and b
584 * @intval: timers are increased by this interval
585 *
586 * This helper function checks if timer B is timer A + window and covers
587 * cases where timer A or B might have already been updated or wrapped
588 * around (Timers are 16 bit).
589 *
590 * Returns true if O.K.
591 */
592static inline bool
593ath5k_check_timer_win(int a, int b, int window, int intval)
594{
595 /*
596 * 1.) usually B should be A + window
597 * 2.) A already updated, B not updated yet
598 * 3.) A already updated and has wrapped around
599 * 4.) B has wrapped around
600 */
601 if ((b - a == window) || /* 1.) */
602 (a - b == intval - window) || /* 2.) */
603 ((a | 0x10000) - b == intval - window) || /* 3.) */
604 ((b | 0x10000) - a == window)) /* 4.) */
605 return true; /* O.K. */
606 return false;
607}
608
609/**
610 * ath5k_hw_check_beacon_timers - Check if the beacon timers are correct
611 *
612 * @ah: The &struct ath5k_hw
613 * @intval: beacon interval
614 *
615 * This is a workaround for IBSS mode:
616 *
617 * The need for this function arises from the fact that we have 4 separate
618 * HW timer registers (TIMER0 - TIMER3), which are closely related to the
619 * next beacon target time (NBTT), and that the HW updates these timers
620 * seperately based on the current TSF value. The hardware increments each
621 * timer by the beacon interval, when the local TSF coverted to TU is equal
622 * to the value stored in the timer.
623 *
624 * The reception of a beacon with the same BSSID can update the local HW TSF
625 * at any time - this is something we can't avoid. If the TSF jumps to a
626 * time which is later than the time stored in a timer, this timer will not
627 * be updated until the TSF in TU wraps around at 16 bit (the size of the
628 * timers) and reaches the time which is stored in the timer.
629 *
630 * The problem is that these timers are closely related to TIMER0 (NBTT) and
631 * that they define a time "window". When the TSF jumps between two timers
632 * (e.g. ATIM and NBTT), the one in the past will be left behind (not
633 * updated), while the one in the future will be updated every beacon
634 * interval. This causes the window to get larger, until the TSF wraps
635 * around as described above and the timer which was left behind gets
636 * updated again. But - because the beacon interval is usually not an exact
637 * divisor of the size of the timers (16 bit), an unwanted "window" between
638 * these timers has developed!
639 *
640 * This is especially important with the ATIM window, because during
641 * the ATIM window only ATIM frames and no data frames are allowed to be
642 * sent, which creates transmission pauses after each beacon. This symptom
643 * has been described as "ramping ping" because ping times increase linearly
644 * for some time and then drop down again. A wrong window on the DMA beacon
645 * timer has the same effect, so we check for these two conditions.
646 *
647 * Returns true if O.K.
648 */
649bool
650ath5k_hw_check_beacon_timers(struct ath5k_hw *ah, int intval)
651{
652 unsigned int nbtt, atim, dma;
653
654 nbtt = ath5k_hw_reg_read(ah, AR5K_TIMER0);
655 atim = ath5k_hw_reg_read(ah, AR5K_TIMER3);
656 dma = ath5k_hw_reg_read(ah, AR5K_TIMER1) >> 3;
657
658 /* NOTE: SWBA is different. Having a wrong window there does not
659 * stop us from sending data and this condition is catched thru
660 * other means (SWBA interrupt) */
661
662 if (ath5k_check_timer_win(nbtt, atim, 1, intval) &&
663 ath5k_check_timer_win(dma, nbtt, AR5K_TUNE_DMA_BEACON_RESP,
664 intval))
665 return true; /* O.K. */
666 return false;
667}
668
669/**
Lukáš Turek6e08d222009-12-21 22:50:51 +0100670 * ath5k_hw_set_coverage_class - Set IEEE 802.11 coverage class
671 *
672 * @ah: The &struct ath5k_hw
673 * @coverage_class: IEEE 802.11 coverage class number
674 *
675 * Sets slot time, ACK timeout and CTS timeout for given coverage class.
676 */
677void ath5k_hw_set_coverage_class(struct ath5k_hw *ah, u8 coverage_class)
678{
679 /* As defined by IEEE 802.11-2007 17.3.8.6 */
680 int slot_time = ath5k_hw_get_default_slottime(ah) + 3 * coverage_class;
681 int ack_timeout = ath5k_hw_get_default_sifs(ah) + slot_time;
682 int cts_timeout = ack_timeout;
683
684 ath5k_hw_set_slot_time(ah, slot_time);
685 ath5k_hw_set_ack_timeout(ah, ack_timeout);
686 ath5k_hw_set_cts_timeout(ah, cts_timeout);
687
688 ah->ah_coverage_class = coverage_class;
689}
Nick Kossifidis9320b5c2010-11-23 20:36:45 +0200690
691/***************************\
692* Init/Start/Stop functions *
693\***************************/
694
695/**
696 * ath5k_hw_start_rx_pcu - Start RX engine
697 *
698 * @ah: The &struct ath5k_hw
699 *
700 * Starts RX engine on PCU so that hw can process RXed frames
701 * (ACK etc).
702 *
703 * NOTE: RX DMA should be already enabled using ath5k_hw_start_rx_dma
704 */
705void ath5k_hw_start_rx_pcu(struct ath5k_hw *ah)
706{
707 AR5K_REG_DISABLE_BITS(ah, AR5K_DIAG_SW, AR5K_DIAG_SW_DIS_RX);
708}
709
710/**
711 * at5k_hw_stop_rx_pcu - Stop RX engine
712 *
713 * @ah: The &struct ath5k_hw
714 *
715 * Stops RX engine on PCU
716 */
717void ath5k_hw_stop_rx_pcu(struct ath5k_hw *ah)
718{
719 AR5K_REG_ENABLE_BITS(ah, AR5K_DIAG_SW, AR5K_DIAG_SW_DIS_RX);
720}
721
722/**
723 * ath5k_hw_set_opmode - Set PCU operating mode
724 *
725 * @ah: The &struct ath5k_hw
726 * @op_mode: &enum nl80211_iftype operating mode
727 *
728 * Configure PCU for the various operating modes (AP/STA etc)
729 */
730int ath5k_hw_set_opmode(struct ath5k_hw *ah, enum nl80211_iftype op_mode)
731{
732 struct ath_common *common = ath5k_hw_common(ah);
733 u32 pcu_reg, beacon_reg, low_id, high_id;
734
735 ATH5K_DBG(ah->ah_sc, ATH5K_DEBUG_MODE, "mode %d\n", op_mode);
736
737 /* Preserve rest settings */
738 pcu_reg = ath5k_hw_reg_read(ah, AR5K_STA_ID1) & 0xffff0000;
739 pcu_reg &= ~(AR5K_STA_ID1_ADHOC | AR5K_STA_ID1_AP
740 | AR5K_STA_ID1_KEYSRCH_MODE
741 | (ah->ah_version == AR5K_AR5210 ?
742 (AR5K_STA_ID1_PWR_SV | AR5K_STA_ID1_NO_PSPOLL) : 0));
743
744 beacon_reg = 0;
745
746 switch (op_mode) {
747 case NL80211_IFTYPE_ADHOC:
748 pcu_reg |= AR5K_STA_ID1_ADHOC | AR5K_STA_ID1_KEYSRCH_MODE;
749 beacon_reg |= AR5K_BCR_ADHOC;
750 if (ah->ah_version == AR5K_AR5210)
751 pcu_reg |= AR5K_STA_ID1_NO_PSPOLL;
752 else
753 AR5K_REG_ENABLE_BITS(ah, AR5K_CFG, AR5K_CFG_IBSS);
754 break;
755
756 case NL80211_IFTYPE_AP:
757 case NL80211_IFTYPE_MESH_POINT:
758 pcu_reg |= AR5K_STA_ID1_AP | AR5K_STA_ID1_KEYSRCH_MODE;
759 beacon_reg |= AR5K_BCR_AP;
760 if (ah->ah_version == AR5K_AR5210)
761 pcu_reg |= AR5K_STA_ID1_NO_PSPOLL;
762 else
763 AR5K_REG_DISABLE_BITS(ah, AR5K_CFG, AR5K_CFG_IBSS);
764 break;
765
766 case NL80211_IFTYPE_STATION:
767 pcu_reg |= AR5K_STA_ID1_KEYSRCH_MODE
768 | (ah->ah_version == AR5K_AR5210 ?
769 AR5K_STA_ID1_PWR_SV : 0);
770 case NL80211_IFTYPE_MONITOR:
771 pcu_reg |= AR5K_STA_ID1_KEYSRCH_MODE
772 | (ah->ah_version == AR5K_AR5210 ?
773 AR5K_STA_ID1_NO_PSPOLL : 0);
774 break;
775
776 default:
777 return -EINVAL;
778 }
779
780 /*
781 * Set PCU registers
782 */
783 low_id = get_unaligned_le32(common->macaddr);
784 high_id = get_unaligned_le16(common->macaddr + 4);
785 ath5k_hw_reg_write(ah, low_id, AR5K_STA_ID0);
786 ath5k_hw_reg_write(ah, pcu_reg | high_id, AR5K_STA_ID1);
787
788 /*
789 * Set Beacon Control Register on 5210
790 */
791 if (ah->ah_version == AR5K_AR5210)
792 ath5k_hw_reg_write(ah, beacon_reg, AR5K_BCR);
793
794 return 0;
795}
796
797void ath5k_hw_pcu_init(struct ath5k_hw *ah, enum nl80211_iftype op_mode,
798 u8 mode)
799{
800 /* Set bssid and bssid mask */
801 ath5k_hw_set_bssid(ah);
802
803 /* Set PCU config */
804 ath5k_hw_set_opmode(ah, op_mode);
805
806 /* Write rate duration table only on AR5212 and if
807 * virtual interface has already been brought up
808 * XXX: rethink this after new mode changes to
809 * mac80211 are integrated */
810 if (ah->ah_version == AR5K_AR5212 &&
811 ah->ah_sc->nvifs)
812 ath5k_hw_write_rate_duration(ah, mode);
813
814 /* Set RSSI/BRSSI thresholds
815 *
816 * Note: If we decide to set this value
817 * dynamicaly, have in mind that when AR5K_RSSI_THR
818 * register is read it might return 0x40 if we haven't
819 * wrote anything to it plus BMISS RSSI threshold is zeroed.
820 * So doing a save/restore procedure here isn't the right
821 * choice. Instead store it on ath5k_hw */
822 ath5k_hw_reg_write(ah, (AR5K_TUNE_RSSI_THRES |
823 AR5K_TUNE_BMISS_THRES <<
824 AR5K_RSSI_THR_BMISS_S),
825 AR5K_RSSI_THR);
826
827 /* MIC QoS support */
828 if (ah->ah_mac_srev >= AR5K_SREV_AR2413) {
829 ath5k_hw_reg_write(ah, 0x000100aa, AR5K_MIC_QOS_CTL);
830 ath5k_hw_reg_write(ah, 0x00003210, AR5K_MIC_QOS_SEL);
831 }
832
833 /* QoS NOACK Policy */
834 if (ah->ah_version == AR5K_AR5212) {
835 ath5k_hw_reg_write(ah,
836 AR5K_REG_SM(2, AR5K_QOS_NOACK_2BIT_VALUES) |
837 AR5K_REG_SM(5, AR5K_QOS_NOACK_BIT_OFFSET) |
838 AR5K_REG_SM(0, AR5K_QOS_NOACK_BYTE_OFFSET),
839 AR5K_QOS_NOACK);
840 }
841
842 /* Restore slot time and ACK timeouts */
843 if (ah->ah_coverage_class > 0)
844 ath5k_hw_set_coverage_class(ah, ah->ah_coverage_class);
845
846 return;
847}