blob: 095d30b50ec7edbb5d82b77e188605d3a458d291 [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/*******************\
35* Generic functions *
36\*******************/
37
38/**
39 * ath5k_hw_set_opmode - Set PCU operating mode
40 *
41 * @ah: The &struct ath5k_hw
Bruno Randolfccfe5552010-03-09 16:55:38 +090042 * @op_mode: &enum nl80211_iftype operating mode
Nick Kossifidisc6e387a2008-08-29 22:45:39 +030043 *
44 * Initialize PCU for the various operating modes (AP/STA etc)
Nick Kossifidisc6e387a2008-08-29 22:45:39 +030045 */
Bruno Randolfccfe5552010-03-09 16:55:38 +090046int ath5k_hw_set_opmode(struct ath5k_hw *ah, enum nl80211_iftype op_mode)
Nick Kossifidisc6e387a2008-08-29 22:45:39 +030047{
Luis R. Rodriguez954fece2009-09-10 10:51:33 -070048 struct ath_common *common = ath5k_hw_common(ah);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +030049 u32 pcu_reg, beacon_reg, low_id, high_id;
50
Bruno Randolfccfe5552010-03-09 16:55:38 +090051 ATH5K_DBG(ah->ah_sc, ATH5K_DEBUG_MODE, "mode %d\n", op_mode);
Nick Kossifidisf07a6c42008-10-29 04:28:28 +020052
53 /* Preserve rest settings */
54 pcu_reg = ath5k_hw_reg_read(ah, AR5K_STA_ID1) & 0xffff0000;
55 pcu_reg &= ~(AR5K_STA_ID1_ADHOC | AR5K_STA_ID1_AP
56 | AR5K_STA_ID1_KEYSRCH_MODE
57 | (ah->ah_version == AR5K_AR5210 ?
58 (AR5K_STA_ID1_PWR_SV | AR5K_STA_ID1_NO_PSPOLL) : 0));
59
Nick Kossifidisc6e387a2008-08-29 22:45:39 +030060 beacon_reg = 0;
61
Bruno Randolfccfe5552010-03-09 16:55:38 +090062 switch (op_mode) {
Johannes Berg05c914f2008-09-11 00:01:58 +020063 case NL80211_IFTYPE_ADHOC:
Nick Kossifidisf07a6c42008-10-29 04:28:28 +020064 pcu_reg |= AR5K_STA_ID1_ADHOC | AR5K_STA_ID1_KEYSRCH_MODE;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +030065 beacon_reg |= AR5K_BCR_ADHOC;
Nick Kossifidisf07a6c42008-10-29 04:28:28 +020066 if (ah->ah_version == AR5K_AR5210)
67 pcu_reg |= AR5K_STA_ID1_NO_PSPOLL;
68 else
Steve Brown4fb74042008-12-23 07:57:05 -050069 AR5K_REG_ENABLE_BITS(ah, AR5K_CFG, AR5K_CFG_IBSS);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +030070 break;
71
Johannes Berg05c914f2008-09-11 00:01:58 +020072 case NL80211_IFTYPE_AP:
73 case NL80211_IFTYPE_MESH_POINT:
Nick Kossifidisf07a6c42008-10-29 04:28:28 +020074 pcu_reg |= AR5K_STA_ID1_AP | AR5K_STA_ID1_KEYSRCH_MODE;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +030075 beacon_reg |= AR5K_BCR_AP;
Nick Kossifidisf07a6c42008-10-29 04:28:28 +020076 if (ah->ah_version == AR5K_AR5210)
77 pcu_reg |= AR5K_STA_ID1_NO_PSPOLL;
78 else
Steve Brown4fb74042008-12-23 07:57:05 -050079 AR5K_REG_DISABLE_BITS(ah, AR5K_CFG, AR5K_CFG_IBSS);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +030080 break;
81
Johannes Berg05c914f2008-09-11 00:01:58 +020082 case NL80211_IFTYPE_STATION:
Nick Kossifidisf07a6c42008-10-29 04:28:28 +020083 pcu_reg |= AR5K_STA_ID1_KEYSRCH_MODE
84 | (ah->ah_version == AR5K_AR5210 ?
Nick Kossifidisc6e387a2008-08-29 22:45:39 +030085 AR5K_STA_ID1_PWR_SV : 0);
Johannes Berg05c914f2008-09-11 00:01:58 +020086 case NL80211_IFTYPE_MONITOR:
Nick Kossifidisf07a6c42008-10-29 04:28:28 +020087 pcu_reg |= AR5K_STA_ID1_KEYSRCH_MODE
88 | (ah->ah_version == AR5K_AR5210 ?
Nick Kossifidisc6e387a2008-08-29 22:45:39 +030089 AR5K_STA_ID1_NO_PSPOLL : 0);
90 break;
91
92 default:
93 return -EINVAL;
94 }
95
96 /*
97 * Set PCU registers
98 */
Luis R. Rodriguez954fece2009-09-10 10:51:33 -070099 low_id = get_unaligned_le32(common->macaddr);
100 high_id = get_unaligned_le16(common->macaddr + 4);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300101 ath5k_hw_reg_write(ah, low_id, AR5K_STA_ID0);
102 ath5k_hw_reg_write(ah, pcu_reg | high_id, AR5K_STA_ID1);
103
104 /*
105 * Set Beacon Control Register on 5210
106 */
107 if (ah->ah_version == AR5K_AR5210)
108 ath5k_hw_reg_write(ah, beacon_reg, AR5K_BCR);
109
110 return 0;
111}
112
113/**
Bruno Randolf495391d2010-03-25 14:49:36 +0900114 * ath5k_hw_update - Update MIB counters (mac layer statistics)
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300115 *
116 * @ah: The &struct ath5k_hw
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300117 *
Bruno Randolf495391d2010-03-25 14:49:36 +0900118 * Reads MIB counters from PCU and updates sw statistics. Is called after a
119 * MIB interrupt, because one of these counters might have reached their maximum
120 * and triggered the MIB interrupt, to let us read and clear the counter.
121 *
122 * Is called in interrupt context!
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300123 */
Bruno Randolf495391d2010-03-25 14:49:36 +0900124void ath5k_hw_update_mib_counters(struct ath5k_hw *ah)
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300125{
Bruno Randolf495391d2010-03-25 14:49:36 +0900126 struct ath5k_statistics *stats = &ah->ah_sc->stats;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300127
128 /* Read-And-Clear */
Bruno Randolf495391d2010-03-25 14:49:36 +0900129 stats->ack_fail += ath5k_hw_reg_read(ah, AR5K_ACK_FAIL);
130 stats->rts_fail += ath5k_hw_reg_read(ah, AR5K_RTS_FAIL);
131 stats->rts_ok += ath5k_hw_reg_read(ah, AR5K_RTS_OK);
132 stats->fcs_error += ath5k_hw_reg_read(ah, AR5K_FCS_FAIL);
133 stats->beacons += ath5k_hw_reg_read(ah, AR5K_BEACON_CNT);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300134}
135
136/**
137 * ath5k_hw_set_ack_bitrate - set bitrate for ACKs
138 *
139 * @ah: The &struct ath5k_hw
Bob Copeland8801df82010-08-21 16:39:02 -0400140 * @high: Flag to determine if we want to use high transmission rate
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300141 * for ACKs or not
142 *
143 * If high flag is set, we tell hw to use a set of control rates based on
Bob Copeland8801df82010-08-21 16:39:02 -0400144 * the current transmission rate (check out control_rates array inside reset.c).
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300145 * If not hw just uses the lowest rate available for the current modulation
146 * scheme being used (1Mbit for CCK and 6Mbits for OFDM).
147 */
148void ath5k_hw_set_ack_bitrate_high(struct ath5k_hw *ah, bool high)
149{
150 if (ah->ah_version != AR5K_AR5212)
151 return;
152 else {
153 u32 val = AR5K_STA_ID1_BASE_RATE_11B | AR5K_STA_ID1_ACKCTS_6MB;
154 if (high)
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300155 AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1, val);
Bruno Randolf0edc9a62010-04-12 16:38:47 +0900156 else
157 AR5K_REG_ENABLE_BITS(ah, AR5K_STA_ID1, val);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300158 }
159}
160
161
162/******************\
163* ACK/CTS Timeouts *
164\******************/
165
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300166/**
167 * ath5k_hw_set_ack_timeout - Set ACK timeout on PCU
168 *
169 * @ah: The &struct ath5k_hw
170 * @timeout: Timeout in usec
171 */
Pavel Roskin626ede62010-02-18 20:28:02 -0500172static int ath5k_hw_set_ack_timeout(struct ath5k_hw *ah, unsigned int timeout)
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300173{
Lukáš Turek3578e6e2009-12-21 22:50:50 +0100174 if (ath5k_hw_clocktoh(ah, AR5K_REG_MS(0xffffffff, AR5K_TIME_OUT_ACK))
175 <= timeout)
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300176 return -EINVAL;
177
178 AR5K_REG_WRITE_BITS(ah, AR5K_TIME_OUT, AR5K_TIME_OUT_ACK,
Lukáš Turek3578e6e2009-12-21 22:50:50 +0100179 ath5k_hw_htoclock(ah, timeout));
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300180
181 return 0;
182}
183
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300184/**
185 * ath5k_hw_set_cts_timeout - Set CTS timeout on PCU
186 *
187 * @ah: The &struct ath5k_hw
188 * @timeout: Timeout in usec
189 */
Pavel Roskin626ede62010-02-18 20:28:02 -0500190static int ath5k_hw_set_cts_timeout(struct ath5k_hw *ah, unsigned int timeout)
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300191{
Lukáš Turek3578e6e2009-12-21 22:50:50 +0100192 if (ath5k_hw_clocktoh(ah, AR5K_REG_MS(0xffffffff, AR5K_TIME_OUT_CTS))
193 <= timeout)
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300194 return -EINVAL;
195
196 AR5K_REG_WRITE_BITS(ah, AR5K_TIME_OUT, AR5K_TIME_OUT_CTS,
Lukáš Turek3578e6e2009-12-21 22:50:50 +0100197 ath5k_hw_htoclock(ah, timeout));
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300198
199 return 0;
200}
201
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300202/**
Lukáš Turek3578e6e2009-12-21 22:50:50 +0100203 * ath5k_hw_htoclock - Translate usec to hw clock units
204 *
205 * @ah: The &struct ath5k_hw
206 * @usec: value in microseconds
207 */
208unsigned int ath5k_hw_htoclock(struct ath5k_hw *ah, unsigned int usec)
209{
210 return usec * ath5k_hw_get_clockrate(ah);
211}
212
213/**
214 * ath5k_hw_clocktoh - Translate hw clock units to usec
215 * @clock: value in hw clock units
216 */
217unsigned int ath5k_hw_clocktoh(struct ath5k_hw *ah, unsigned int clock)
218{
219 return clock / ath5k_hw_get_clockrate(ah);
220}
221
222/**
223 * ath5k_hw_get_clockrate - Get the clock rate for current mode
224 *
225 * @ah: The &struct ath5k_hw
226 */
227unsigned int ath5k_hw_get_clockrate(struct ath5k_hw *ah)
228{
229 struct ieee80211_channel *channel = ah->ah_current_channel;
230 int clock;
231
232 if (channel->hw_value & CHANNEL_5GHZ)
233 clock = 40; /* 802.11a */
234 else if (channel->hw_value & CHANNEL_CCK)
235 clock = 22; /* 802.11b */
236 else
237 clock = 44; /* 802.11g */
238
239 /* Clock rate in turbo modes is twice the normal rate */
240 if (channel->hw_value & CHANNEL_TURBO)
241 clock *= 2;
242
243 return clock;
244}
245
246/**
Lukáš Turek6e08d222009-12-21 22:50:51 +0100247 * ath5k_hw_get_default_slottime - Get the default slot time for current mode
248 *
249 * @ah: The &struct ath5k_hw
250 */
Pavel Roskin626ede62010-02-18 20:28:02 -0500251static unsigned int ath5k_hw_get_default_slottime(struct ath5k_hw *ah)
Lukáš Turek6e08d222009-12-21 22:50:51 +0100252{
253 struct ieee80211_channel *channel = ah->ah_current_channel;
254
255 if (channel->hw_value & CHANNEL_TURBO)
256 return 6; /* both turbo modes */
257
258 if (channel->hw_value & CHANNEL_CCK)
259 return 20; /* 802.11b */
260
261 return 9; /* 802.11 a/g */
262}
263
264/**
265 * ath5k_hw_get_default_sifs - Get the default SIFS for current mode
266 *
267 * @ah: The &struct ath5k_hw
268 */
Pavel Roskin626ede62010-02-18 20:28:02 -0500269static unsigned int ath5k_hw_get_default_sifs(struct ath5k_hw *ah)
Lukáš Turek6e08d222009-12-21 22:50:51 +0100270{
271 struct ieee80211_channel *channel = ah->ah_current_channel;
272
273 if (channel->hw_value & CHANNEL_TURBO)
274 return 8; /* both turbo modes */
275
276 if (channel->hw_value & CHANNEL_5GHZ)
277 return 16; /* 802.11a */
278
279 return 10; /* 802.11 b/g */
280}
281
282/**
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300283 * ath5k_hw_set_lladdr - Set station id
284 *
285 * @ah: The &struct ath5k_hw
286 * @mac: The card's mac address
287 *
288 * Set station id on hw using the provided mac address
289 */
290int ath5k_hw_set_lladdr(struct ath5k_hw *ah, const u8 *mac)
291{
Luis R. Rodriguez954fece2009-09-10 10:51:33 -0700292 struct ath_common *common = ath5k_hw_common(ah);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300293 u32 low_id, high_id;
Bob Copelandf6bac3e2008-11-26 16:17:11 -0500294 u32 pcu_reg;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300295
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300296 /* Set new station ID */
Luis R. Rodriguez954fece2009-09-10 10:51:33 -0700297 memcpy(common->macaddr, mac, ETH_ALEN);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300298
Bob Copelandf6bac3e2008-11-26 16:17:11 -0500299 pcu_reg = ath5k_hw_reg_read(ah, AR5K_STA_ID1) & 0xffff0000;
300
Luis R. Rodriguezbcd8f542009-09-09 22:43:17 -0700301 low_id = get_unaligned_le32(mac);
302 high_id = get_unaligned_le16(mac + 4);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300303
304 ath5k_hw_reg_write(ah, low_id, AR5K_STA_ID0);
Bob Copelandf6bac3e2008-11-26 16:17:11 -0500305 ath5k_hw_reg_write(ah, pcu_reg | high_id, AR5K_STA_ID1);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300306
307 return 0;
308}
309
310/**
Nick Kossifidis418de6d2010-08-15 13:03:10 -0400311 * ath5k_hw_set_bssid - Set current BSSID on hw
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300312 *
313 * @ah: The &struct ath5k_hw
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300314 *
Nick Kossifidis418de6d2010-08-15 13:03:10 -0400315 * Sets the current BSSID and BSSID mask we have from the
316 * common struct into the hardware
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300317 */
Nick Kossifidis418de6d2010-08-15 13:03:10 -0400318void ath5k_hw_set_bssid(struct ath5k_hw *ah)
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300319{
Luis R. Rodriguez954fece2009-09-10 10:51:33 -0700320 struct ath_common *common = ath5k_hw_common(ah);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300321 u16 tim_offset = 0;
322
323 /*
Nick Kossifidis418de6d2010-08-15 13:03:10 -0400324 * Set BSSID mask on 5212
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300325 */
Luis R. Rodrigueza72d57a2009-10-06 20:44:29 -0400326 if (ah->ah_version == AR5K_AR5212)
327 ath_hw_setbssidmask(common);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300328
329 /*
Nick Kossifidis418de6d2010-08-15 13:03:10 -0400330 * Set BSSID
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300331 */
Luis R. Rodriguezabba0682009-10-06 20:44:32 -0400332 ath5k_hw_reg_write(ah,
333 get_unaligned_le32(common->curbssid),
Luis R. Rodrigueza3f86bf2009-10-06 20:44:33 -0400334 AR5K_BSS_ID0);
Luis R. Rodriguezabba0682009-10-06 20:44:32 -0400335 ath5k_hw_reg_write(ah,
336 get_unaligned_le16(common->curbssid + 4) |
337 ((common->curaid & 0x3fff) << AR5K_BSS_ID1_AID_S),
Luis R. Rodrigueza3f86bf2009-10-06 20:44:33 -0400338 AR5K_BSS_ID1);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300339
Luis R. Rodriguezbe5d6b72009-10-06 20:44:31 -0400340 if (common->curaid == 0) {
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300341 ath5k_hw_disable_pspoll(ah);
342 return;
343 }
344
345 AR5K_REG_WRITE_BITS(ah, AR5K_BEACON, AR5K_BEACON_TIM,
Luis R. Rodriguezabba0682009-10-06 20:44:32 -0400346 tim_offset ? tim_offset + 4 : 0);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300347
348 ath5k_hw_enable_pspoll(ah, NULL, 0);
349}
350
Luis R. Rodriguez13b81552009-09-10 17:52:45 -0700351void ath5k_hw_set_bssid_mask(struct ath5k_hw *ah, const u8 *mask)
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300352{
Luis R. Rodriguez954fece2009-09-10 10:51:33 -0700353 struct ath_common *common = ath5k_hw_common(ah);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300354
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200355 /* Cache bssid mask so that we can restore it
356 * on reset */
Luis R. Rodriguez954fece2009-09-10 10:51:33 -0700357 memcpy(common->bssidmask, mask, ETH_ALEN);
Luis R. Rodriguez13b81552009-09-10 17:52:45 -0700358 if (ah->ah_version == AR5K_AR5212)
359 ath_hw_setbssidmask(common);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300360}
361
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300362/************\
363* RX Control *
364\************/
365
366/**
367 * ath5k_hw_start_rx_pcu - Start RX engine
368 *
369 * @ah: The &struct ath5k_hw
370 *
371 * Starts RX engine on PCU so that hw can process RXed frames
372 * (ACK etc).
373 *
374 * NOTE: RX DMA should be already enabled using ath5k_hw_start_rx_dma
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300375 */
376void ath5k_hw_start_rx_pcu(struct ath5k_hw *ah)
377{
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300378 AR5K_REG_DISABLE_BITS(ah, AR5K_DIAG_SW, AR5K_DIAG_SW_DIS_RX);
379}
380
381/**
382 * at5k_hw_stop_rx_pcu - Stop RX engine
383 *
384 * @ah: The &struct ath5k_hw
385 *
386 * Stops RX engine on PCU
387 *
388 * TODO: Detach ANI here
389 */
390void ath5k_hw_stop_rx_pcu(struct ath5k_hw *ah)
391{
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300392 AR5K_REG_ENABLE_BITS(ah, AR5K_DIAG_SW, AR5K_DIAG_SW_DIS_RX);
393}
394
395/*
396 * Set multicast filter
397 */
398void ath5k_hw_set_mcast_filter(struct ath5k_hw *ah, u32 filter0, u32 filter1)
399{
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300400 ath5k_hw_reg_write(ah, filter0, AR5K_MCAST_FILTER0);
401 ath5k_hw_reg_write(ah, filter1, AR5K_MCAST_FILTER1);
402}
403
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300404/**
405 * ath5k_hw_get_rx_filter - Get current rx filter
406 *
407 * @ah: The &struct ath5k_hw
408 *
409 * Returns the RX filter by reading rx filter and
410 * phy error filter registers. RX filter is used
411 * to set the allowed frame types that PCU will accept
412 * and pass to the driver. For a list of frame types
413 * check out reg.h.
414 */
415u32 ath5k_hw_get_rx_filter(struct ath5k_hw *ah)
416{
417 u32 data, filter = 0;
418
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300419 filter = ath5k_hw_reg_read(ah, AR5K_RX_FILTER);
420
421 /*Radar detection for 5212*/
422 if (ah->ah_version == AR5K_AR5212) {
423 data = ath5k_hw_reg_read(ah, AR5K_PHY_ERR_FIL);
424
425 if (data & AR5K_PHY_ERR_FIL_RADAR)
426 filter |= AR5K_RX_FILTER_RADARERR;
427 if (data & (AR5K_PHY_ERR_FIL_OFDM | AR5K_PHY_ERR_FIL_CCK))
428 filter |= AR5K_RX_FILTER_PHYERR;
429 }
430
431 return filter;
432}
433
434/**
435 * ath5k_hw_set_rx_filter - Set rx filter
436 *
437 * @ah: The &struct ath5k_hw
438 * @filter: RX filter mask (see reg.h)
439 *
440 * Sets RX filter register and also handles PHY error filter
441 * register on 5212 and newer chips so that we have proper PHY
442 * error reporting.
443 */
444void ath5k_hw_set_rx_filter(struct ath5k_hw *ah, u32 filter)
445{
446 u32 data = 0;
447
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300448 /* Set PHY error filter register on 5212*/
449 if (ah->ah_version == AR5K_AR5212) {
450 if (filter & AR5K_RX_FILTER_RADARERR)
451 data |= AR5K_PHY_ERR_FIL_RADAR;
452 if (filter & AR5K_RX_FILTER_PHYERR)
453 data |= AR5K_PHY_ERR_FIL_OFDM | AR5K_PHY_ERR_FIL_CCK;
454 }
455
456 /*
457 * The AR5210 uses promiscous mode to detect radar activity
458 */
459 if (ah->ah_version == AR5K_AR5210 &&
460 (filter & AR5K_RX_FILTER_RADARERR)) {
461 filter &= ~AR5K_RX_FILTER_RADARERR;
462 filter |= AR5K_RX_FILTER_PROM;
463 }
464
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200465 /*Zero length DMA (phy error reporting) */
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300466 if (data)
467 AR5K_REG_ENABLE_BITS(ah, AR5K_RXCFG, AR5K_RXCFG_ZLFDMA);
468 else
469 AR5K_REG_DISABLE_BITS(ah, AR5K_RXCFG, AR5K_RXCFG_ZLFDMA);
470
471 /*Write RX Filter register*/
472 ath5k_hw_reg_write(ah, filter & 0xff, AR5K_RX_FILTER);
473
474 /*Write PHY error filter register on 5212*/
475 if (ah->ah_version == AR5K_AR5212)
476 ath5k_hw_reg_write(ah, data, AR5K_PHY_ERR_FIL);
477
478}
479
480
481/****************\
482* Beacon control *
483\****************/
484
Benoit Papillault1c0fc652010-04-16 00:07:26 +0200485#define ATH5K_MAX_TSF_READ 10
486
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300487/**
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300488 * ath5k_hw_get_tsf64 - Get the full 64bit TSF
489 *
490 * @ah: The &struct ath5k_hw
491 *
492 * Returns the current TSF
493 */
494u64 ath5k_hw_get_tsf64(struct ath5k_hw *ah)
495{
Benoit Papillault1c0fc652010-04-16 00:07:26 +0200496 u32 tsf_lower, tsf_upper1, tsf_upper2;
497 int i;
Bruno Randolf28df8972010-09-27 12:22:32 +0900498 unsigned long flags;
499
500 /* This code is time critical - we don't want to be interrupted here */
501 local_irq_save(flags);
Benoit Papillault1c0fc652010-04-16 00:07:26 +0200502
503 /*
504 * While reading TSF upper and then lower part, the clock is still
505 * counting (or jumping in case of IBSS merge) so we might get
506 * inconsistent values. To avoid this, we read the upper part again
507 * and check it has not been changed. We make the hypothesis that a
508 * maximum of 3 changes can happens in a row (we use 10 as a safe
509 * value).
510 *
511 * Impact on performance is pretty small, since in most cases, only
512 * 3 register reads are needed.
513 */
514
515 tsf_upper1 = ath5k_hw_reg_read(ah, AR5K_TSF_U32);
516 for (i = 0; i < ATH5K_MAX_TSF_READ; i++) {
517 tsf_lower = ath5k_hw_reg_read(ah, AR5K_TSF_L32);
518 tsf_upper2 = ath5k_hw_reg_read(ah, AR5K_TSF_U32);
519 if (tsf_upper2 == tsf_upper1)
520 break;
521 tsf_upper1 = tsf_upper2;
522 }
523
Bruno Randolf28df8972010-09-27 12:22:32 +0900524 local_irq_restore(flags);
525
Benoit Papillault1c0fc652010-04-16 00:07:26 +0200526 WARN_ON( i == ATH5K_MAX_TSF_READ );
527
Benoit Papillault1c0fc652010-04-16 00:07:26 +0200528 return (((u64)tsf_upper1 << 32) | tsf_lower);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300529}
530
531/**
Alina Friedrichsen8cab7582009-01-23 05:39:13 +0100532 * ath5k_hw_set_tsf64 - Set a new 64bit TSF
533 *
534 * @ah: The &struct ath5k_hw
535 * @tsf64: The new 64bit TSF
536 *
537 * Sets the new TSF
538 */
539void ath5k_hw_set_tsf64(struct ath5k_hw *ah, u64 tsf64)
540{
Alina Friedrichsen8cab7582009-01-23 05:39:13 +0100541 ath5k_hw_reg_write(ah, tsf64 & 0xffffffff, AR5K_TSF_L32);
Alina Friedrichsen0ad65bd2009-03-02 23:29:48 +0100542 ath5k_hw_reg_write(ah, (tsf64 >> 32) & 0xffffffff, AR5K_TSF_U32);
Alina Friedrichsen8cab7582009-01-23 05:39:13 +0100543}
544
545/**
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300546 * ath5k_hw_reset_tsf - Force a TSF reset
547 *
548 * @ah: The &struct ath5k_hw
549 *
550 * Forces a TSF reset on PCU
551 */
552void ath5k_hw_reset_tsf(struct ath5k_hw *ah)
553{
Bob Copeland14be9942008-09-28 12:09:43 -0400554 u32 val;
555
Bob Copeland14be9942008-09-28 12:09:43 -0400556 val = ath5k_hw_reg_read(ah, AR5K_BEACON) | AR5K_BEACON_RESET_TSF;
557
558 /*
559 * Each write to the RESET_TSF bit toggles a hardware internal
560 * signal to reset TSF, but if left high it will cause a TSF reset
561 * on the next chip reset as well. Thus we always write the value
562 * twice to clear the signal.
563 */
564 ath5k_hw_reg_write(ah, val, AR5K_BEACON);
565 ath5k_hw_reg_write(ah, val, AR5K_BEACON);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300566}
567
568/*
569 * Initialize beacon timers
570 */
571void ath5k_hw_init_beacon(struct ath5k_hw *ah, u32 next_beacon, u32 interval)
572{
573 u32 timer1, timer2, timer3;
574
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300575 /*
576 * Set the additional timers by mode
577 */
Bruno Randolfccfe5552010-03-09 16:55:38 +0900578 switch (ah->ah_sc->opmode) {
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200579 case NL80211_IFTYPE_MONITOR:
Johannes Berg05c914f2008-09-11 00:01:58 +0200580 case NL80211_IFTYPE_STATION:
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200581 /* In STA mode timer1 is used as next wakeup
582 * timer and timer2 as next CFP duration start
583 * timer. Both in 1/8TUs. */
584 /* TODO: PCF handling */
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300585 if (ah->ah_version == AR5K_AR5210) {
586 timer1 = 0xffffffff;
587 timer2 = 0xffffffff;
588 } else {
589 timer1 = 0x0000ffff;
590 timer2 = 0x0007ffff;
591 }
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200592 /* Mark associated AP as PCF incapable for now */
593 AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1, AR5K_STA_ID1_PCF);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300594 break;
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200595 case NL80211_IFTYPE_ADHOC:
596 AR5K_REG_ENABLE_BITS(ah, AR5K_TXCFG, AR5K_TXCFG_ADHOC_BCN_ATIM);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300597 default:
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200598 /* On non-STA modes timer1 is used as next DMA
599 * beacon alert (DBA) timer and timer2 as next
600 * software beacon alert. Both in 1/8TUs. */
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300601 timer1 = (next_beacon - AR5K_TUNE_DMA_BEACON_RESP) << 3;
602 timer2 = (next_beacon - AR5K_TUNE_SW_BEACON_RESP) << 3;
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200603 break;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300604 }
605
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200606 /* Timer3 marks the end of our ATIM window
607 * a zero length window is not allowed because
608 * we 'll get no beacons */
Bruno Randolf4a79f2c2010-09-27 12:22:16 +0900609 timer3 = next_beacon + 1;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300610
611 /*
612 * Set the beacon register and enable all timers.
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300613 */
Nick Kossifidis35edf8a2009-06-12 16:09:53 -0700614 /* When in AP or Mesh Point mode zero timer0 to start TSF */
Bruno Randolfccfe5552010-03-09 16:55:38 +0900615 if (ah->ah_sc->opmode == NL80211_IFTYPE_AP ||
616 ah->ah_sc->opmode == NL80211_IFTYPE_MESH_POINT)
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200617 ath5k_hw_reg_write(ah, 0, AR5K_TIMER0);
Nick Kossifidis428cbd42009-04-30 15:55:47 -0400618
619 ath5k_hw_reg_write(ah, next_beacon, AR5K_TIMER0);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300620 ath5k_hw_reg_write(ah, timer1, AR5K_TIMER1);
621 ath5k_hw_reg_write(ah, timer2, AR5K_TIMER2);
622 ath5k_hw_reg_write(ah, timer3, AR5K_TIMER3);
623
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200624 /* Force a TSF reset if requested and enable beacons */
625 if (interval & AR5K_BEACON_RESET_TSF)
626 ath5k_hw_reset_tsf(ah);
627
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300628 ath5k_hw_reg_write(ah, interval & (AR5K_BEACON_PERIOD |
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200629 AR5K_BEACON_ENABLE),
630 AR5K_BEACON);
631
632 /* Flush any pending BMISS interrupts on ISR by
633 * performing a clear-on-write operation on PISR
634 * register for the BMISS bit (writing a bit on
635 * ISR togles a reset for that bit and leaves
636 * the rest bits intact) */
637 if (ah->ah_version == AR5K_AR5210)
638 ath5k_hw_reg_write(ah, AR5K_ISR_BMISS, AR5K_ISR);
639 else
640 ath5k_hw_reg_write(ah, AR5K_ISR_BMISS, AR5K_PISR);
641
642 /* TODO: Set enchanced sleep registers on AR5212
643 * based on vif->bss_conf params, until then
644 * disable power save reporting.*/
645 AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1, AR5K_STA_ID1_PWR_SV);
646
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300647}
648
Lukáš Turek6e08d222009-12-21 22:50:51 +0100649/**
Bruno Randolf7f896122010-09-27 12:22:21 +0900650 * ath5k_check_timer_win - Check if timer B is timer A + window
651 *
652 * @a: timer a (before b)
653 * @b: timer b (after a)
654 * @window: difference between a and b
655 * @intval: timers are increased by this interval
656 *
657 * This helper function checks if timer B is timer A + window and covers
658 * cases where timer A or B might have already been updated or wrapped
659 * around (Timers are 16 bit).
660 *
661 * Returns true if O.K.
662 */
663static inline bool
664ath5k_check_timer_win(int a, int b, int window, int intval)
665{
666 /*
667 * 1.) usually B should be A + window
668 * 2.) A already updated, B not updated yet
669 * 3.) A already updated and has wrapped around
670 * 4.) B has wrapped around
671 */
672 if ((b - a == window) || /* 1.) */
673 (a - b == intval - window) || /* 2.) */
674 ((a | 0x10000) - b == intval - window) || /* 3.) */
675 ((b | 0x10000) - a == window)) /* 4.) */
676 return true; /* O.K. */
677 return false;
678}
679
680/**
681 * ath5k_hw_check_beacon_timers - Check if the beacon timers are correct
682 *
683 * @ah: The &struct ath5k_hw
684 * @intval: beacon interval
685 *
686 * This is a workaround for IBSS mode:
687 *
688 * The need for this function arises from the fact that we have 4 separate
689 * HW timer registers (TIMER0 - TIMER3), which are closely related to the
690 * next beacon target time (NBTT), and that the HW updates these timers
691 * seperately based on the current TSF value. The hardware increments each
692 * timer by the beacon interval, when the local TSF coverted to TU is equal
693 * to the value stored in the timer.
694 *
695 * The reception of a beacon with the same BSSID can update the local HW TSF
696 * at any time - this is something we can't avoid. If the TSF jumps to a
697 * time which is later than the time stored in a timer, this timer will not
698 * be updated until the TSF in TU wraps around at 16 bit (the size of the
699 * timers) and reaches the time which is stored in the timer.
700 *
701 * The problem is that these timers are closely related to TIMER0 (NBTT) and
702 * that they define a time "window". When the TSF jumps between two timers
703 * (e.g. ATIM and NBTT), the one in the past will be left behind (not
704 * updated), while the one in the future will be updated every beacon
705 * interval. This causes the window to get larger, until the TSF wraps
706 * around as described above and the timer which was left behind gets
707 * updated again. But - because the beacon interval is usually not an exact
708 * divisor of the size of the timers (16 bit), an unwanted "window" between
709 * these timers has developed!
710 *
711 * This is especially important with the ATIM window, because during
712 * the ATIM window only ATIM frames and no data frames are allowed to be
713 * sent, which creates transmission pauses after each beacon. This symptom
714 * has been described as "ramping ping" because ping times increase linearly
715 * for some time and then drop down again. A wrong window on the DMA beacon
716 * timer has the same effect, so we check for these two conditions.
717 *
718 * Returns true if O.K.
719 */
720bool
721ath5k_hw_check_beacon_timers(struct ath5k_hw *ah, int intval)
722{
723 unsigned int nbtt, atim, dma;
724
725 nbtt = ath5k_hw_reg_read(ah, AR5K_TIMER0);
726 atim = ath5k_hw_reg_read(ah, AR5K_TIMER3);
727 dma = ath5k_hw_reg_read(ah, AR5K_TIMER1) >> 3;
728
729 /* NOTE: SWBA is different. Having a wrong window there does not
730 * stop us from sending data and this condition is catched thru
731 * other means (SWBA interrupt) */
732
733 if (ath5k_check_timer_win(nbtt, atim, 1, intval) &&
734 ath5k_check_timer_win(dma, nbtt, AR5K_TUNE_DMA_BEACON_RESP,
735 intval))
736 return true; /* O.K. */
737 return false;
738}
739
740/**
Lukáš Turek6e08d222009-12-21 22:50:51 +0100741 * ath5k_hw_set_coverage_class - Set IEEE 802.11 coverage class
742 *
743 * @ah: The &struct ath5k_hw
744 * @coverage_class: IEEE 802.11 coverage class number
745 *
746 * Sets slot time, ACK timeout and CTS timeout for given coverage class.
747 */
748void ath5k_hw_set_coverage_class(struct ath5k_hw *ah, u8 coverage_class)
749{
750 /* As defined by IEEE 802.11-2007 17.3.8.6 */
751 int slot_time = ath5k_hw_get_default_slottime(ah) + 3 * coverage_class;
752 int ack_timeout = ath5k_hw_get_default_sifs(ah) + slot_time;
753 int cts_timeout = ack_timeout;
754
755 ath5k_hw_set_slot_time(ah, slot_time);
756 ath5k_hw_set_ack_timeout(ah, ack_timeout);
757 ath5k_hw_set_cts_timeout(ah, cts_timeout);
758
759 ah->ah_coverage_class = coverage_class;
760}