blob: be69ebb2744ea2f8a503cebb0cac2808a3964a9d [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
42 *
43 * Initialize PCU for the various operating modes (AP/STA etc)
44 *
45 * NOTE: ah->ah_op_mode must be set before calling this.
46 */
47int ath5k_hw_set_opmode(struct ath5k_hw *ah)
48{
Luis R. Rodriguez954fece2009-09-10 10:51:33 -070049 struct ath_common *common = ath5k_hw_common(ah);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +030050 u32 pcu_reg, beacon_reg, low_id, high_id;
51
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
62 ATH5K_TRACE(ah->ah_sc);
63
64 switch (ah->ah_op_mode) {
Johannes Berg05c914f2008-09-11 00:01:58 +020065 case NL80211_IFTYPE_ADHOC:
Nick Kossifidisf07a6c42008-10-29 04:28:28 +020066 pcu_reg |= AR5K_STA_ID1_ADHOC | AR5K_STA_ID1_KEYSRCH_MODE;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +030067 beacon_reg |= AR5K_BCR_ADHOC;
Nick Kossifidisf07a6c42008-10-29 04:28:28 +020068 if (ah->ah_version == AR5K_AR5210)
69 pcu_reg |= AR5K_STA_ID1_NO_PSPOLL;
70 else
Steve Brown4fb74042008-12-23 07:57:05 -050071 AR5K_REG_ENABLE_BITS(ah, AR5K_CFG, AR5K_CFG_IBSS);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +030072 break;
73
Johannes Berg05c914f2008-09-11 00:01:58 +020074 case NL80211_IFTYPE_AP:
75 case NL80211_IFTYPE_MESH_POINT:
Nick Kossifidisf07a6c42008-10-29 04:28:28 +020076 pcu_reg |= AR5K_STA_ID1_AP | AR5K_STA_ID1_KEYSRCH_MODE;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +030077 beacon_reg |= AR5K_BCR_AP;
Nick Kossifidisf07a6c42008-10-29 04:28:28 +020078 if (ah->ah_version == AR5K_AR5210)
79 pcu_reg |= AR5K_STA_ID1_NO_PSPOLL;
80 else
Steve Brown4fb74042008-12-23 07:57:05 -050081 AR5K_REG_DISABLE_BITS(ah, AR5K_CFG, AR5K_CFG_IBSS);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +030082 break;
83
Johannes Berg05c914f2008-09-11 00:01:58 +020084 case NL80211_IFTYPE_STATION:
Nick Kossifidisf07a6c42008-10-29 04:28:28 +020085 pcu_reg |= AR5K_STA_ID1_KEYSRCH_MODE
86 | (ah->ah_version == AR5K_AR5210 ?
Nick Kossifidisc6e387a2008-08-29 22:45:39 +030087 AR5K_STA_ID1_PWR_SV : 0);
Johannes Berg05c914f2008-09-11 00:01:58 +020088 case NL80211_IFTYPE_MONITOR:
Nick Kossifidisf07a6c42008-10-29 04:28:28 +020089 pcu_reg |= AR5K_STA_ID1_KEYSRCH_MODE
90 | (ah->ah_version == AR5K_AR5210 ?
Nick Kossifidisc6e387a2008-08-29 22:45:39 +030091 AR5K_STA_ID1_NO_PSPOLL : 0);
92 break;
93
94 default:
95 return -EINVAL;
96 }
97
98 /*
99 * Set PCU registers
100 */
Luis R. Rodriguez954fece2009-09-10 10:51:33 -0700101 low_id = get_unaligned_le32(common->macaddr);
102 high_id = get_unaligned_le16(common->macaddr + 4);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300103 ath5k_hw_reg_write(ah, low_id, AR5K_STA_ID0);
104 ath5k_hw_reg_write(ah, pcu_reg | high_id, AR5K_STA_ID1);
105
106 /*
107 * Set Beacon Control Register on 5210
108 */
109 if (ah->ah_version == AR5K_AR5210)
110 ath5k_hw_reg_write(ah, beacon_reg, AR5K_BCR);
111
112 return 0;
113}
114
115/**
116 * ath5k_hw_update - Update mib counters (mac layer statistics)
117 *
118 * @ah: The &struct ath5k_hw
119 * @stats: The &struct ieee80211_low_level_stats we use to track
120 * statistics on the driver
121 *
122 * Reads MIB counters from PCU and updates sw statistics. Must be
123 * called after a MIB interrupt.
124 */
125void ath5k_hw_update_mib_counters(struct ath5k_hw *ah,
126 struct ieee80211_low_level_stats *stats)
127{
128 ATH5K_TRACE(ah->ah_sc);
129
130 /* Read-And-Clear */
131 stats->dot11ACKFailureCount += ath5k_hw_reg_read(ah, AR5K_ACK_FAIL);
132 stats->dot11RTSFailureCount += ath5k_hw_reg_read(ah, AR5K_RTS_FAIL);
133 stats->dot11RTSSuccessCount += ath5k_hw_reg_read(ah, AR5K_RTS_OK);
134 stats->dot11FCSErrorCount += ath5k_hw_reg_read(ah, AR5K_FCS_FAIL);
135
136 /* XXX: Should we use this to track beacon count ?
137 * -we read it anyway to clear the register */
138 ath5k_hw_reg_read(ah, AR5K_BEACON_CNT);
139
140 /* Reset profile count registers on 5212*/
141 if (ah->ah_version == AR5K_AR5212) {
142 ath5k_hw_reg_write(ah, 0, AR5K_PROFCNT_TX);
143 ath5k_hw_reg_write(ah, 0, AR5K_PROFCNT_RX);
144 ath5k_hw_reg_write(ah, 0, AR5K_PROFCNT_RXCLR);
145 ath5k_hw_reg_write(ah, 0, AR5K_PROFCNT_CYCLE);
146 }
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200147
148 /* TODO: Handle ANI stats */
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300149}
150
151/**
152 * ath5k_hw_set_ack_bitrate - set bitrate for ACKs
153 *
154 * @ah: The &struct ath5k_hw
155 * @high: Flag to determine if we want to use high transmition rate
156 * for ACKs or not
157 *
158 * If high flag is set, we tell hw to use a set of control rates based on
159 * the current transmition rate (check out control_rates array inside reset.c).
160 * If not hw just uses the lowest rate available for the current modulation
161 * scheme being used (1Mbit for CCK and 6Mbits for OFDM).
162 */
163void ath5k_hw_set_ack_bitrate_high(struct ath5k_hw *ah, bool high)
164{
165 if (ah->ah_version != AR5K_AR5212)
166 return;
167 else {
168 u32 val = AR5K_STA_ID1_BASE_RATE_11B | AR5K_STA_ID1_ACKCTS_6MB;
169 if (high)
170 AR5K_REG_ENABLE_BITS(ah, AR5K_STA_ID1, val);
171 else
172 AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1, val);
173 }
174}
175
176
177/******************\
178* ACK/CTS Timeouts *
179\******************/
180
Pavel Roskin626ede62010-02-18 20:28:02 -0500181#if 0
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300182/**
183 * ath5k_hw_het_ack_timeout - Get ACK timeout from PCU in usec
184 *
185 * @ah: The &struct ath5k_hw
186 */
187unsigned int ath5k_hw_get_ack_timeout(struct ath5k_hw *ah)
188{
189 ATH5K_TRACE(ah->ah_sc);
190
Lukáš Turek3578e6e2009-12-21 22:50:50 +0100191 return ath5k_hw_clocktoh(ah, AR5K_REG_MS(ath5k_hw_reg_read(ah,
192 AR5K_TIME_OUT), AR5K_TIME_OUT_ACK));
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300193}
Pavel Roskin626ede62010-02-18 20:28:02 -0500194#endif
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300195
196/**
197 * ath5k_hw_set_ack_timeout - Set ACK timeout on PCU
198 *
199 * @ah: The &struct ath5k_hw
200 * @timeout: Timeout in usec
201 */
Pavel Roskin626ede62010-02-18 20:28:02 -0500202static int ath5k_hw_set_ack_timeout(struct ath5k_hw *ah, unsigned int timeout)
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300203{
204 ATH5K_TRACE(ah->ah_sc);
Lukáš Turek3578e6e2009-12-21 22:50:50 +0100205 if (ath5k_hw_clocktoh(ah, AR5K_REG_MS(0xffffffff, AR5K_TIME_OUT_ACK))
206 <= timeout)
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300207 return -EINVAL;
208
209 AR5K_REG_WRITE_BITS(ah, AR5K_TIME_OUT, AR5K_TIME_OUT_ACK,
Lukáš Turek3578e6e2009-12-21 22:50:50 +0100210 ath5k_hw_htoclock(ah, timeout));
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300211
212 return 0;
213}
214
Pavel Roskin626ede62010-02-18 20:28:02 -0500215#if 0
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300216/**
217 * ath5k_hw_get_cts_timeout - Get CTS timeout from PCU in usec
218 *
219 * @ah: The &struct ath5k_hw
220 */
221unsigned int ath5k_hw_get_cts_timeout(struct ath5k_hw *ah)
222{
223 ATH5K_TRACE(ah->ah_sc);
Lukáš Turek3578e6e2009-12-21 22:50:50 +0100224 return ath5k_hw_clocktoh(ah, AR5K_REG_MS(ath5k_hw_reg_read(ah,
225 AR5K_TIME_OUT), AR5K_TIME_OUT_CTS));
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300226}
Pavel Roskin626ede62010-02-18 20:28:02 -0500227#endif
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300228
229/**
230 * ath5k_hw_set_cts_timeout - Set CTS timeout on PCU
231 *
232 * @ah: The &struct ath5k_hw
233 * @timeout: Timeout in usec
234 */
Pavel Roskin626ede62010-02-18 20:28:02 -0500235static int ath5k_hw_set_cts_timeout(struct ath5k_hw *ah, unsigned int timeout)
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300236{
237 ATH5K_TRACE(ah->ah_sc);
Lukáš Turek3578e6e2009-12-21 22:50:50 +0100238 if (ath5k_hw_clocktoh(ah, AR5K_REG_MS(0xffffffff, AR5K_TIME_OUT_CTS))
239 <= timeout)
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300240 return -EINVAL;
241
242 AR5K_REG_WRITE_BITS(ah, AR5K_TIME_OUT, AR5K_TIME_OUT_CTS,
Lukáš Turek3578e6e2009-12-21 22:50:50 +0100243 ath5k_hw_htoclock(ah, timeout));
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300244
245 return 0;
246}
247
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300248/**
Lukáš Turek3578e6e2009-12-21 22:50:50 +0100249 * ath5k_hw_htoclock - Translate usec to hw clock units
250 *
251 * @ah: The &struct ath5k_hw
252 * @usec: value in microseconds
253 */
254unsigned int ath5k_hw_htoclock(struct ath5k_hw *ah, unsigned int usec)
255{
256 return usec * ath5k_hw_get_clockrate(ah);
257}
258
259/**
260 * ath5k_hw_clocktoh - Translate hw clock units to usec
261 * @clock: value in hw clock units
262 */
263unsigned int ath5k_hw_clocktoh(struct ath5k_hw *ah, unsigned int clock)
264{
265 return clock / ath5k_hw_get_clockrate(ah);
266}
267
268/**
269 * ath5k_hw_get_clockrate - Get the clock rate for current mode
270 *
271 * @ah: The &struct ath5k_hw
272 */
273unsigned int ath5k_hw_get_clockrate(struct ath5k_hw *ah)
274{
275 struct ieee80211_channel *channel = ah->ah_current_channel;
276 int clock;
277
278 if (channel->hw_value & CHANNEL_5GHZ)
279 clock = 40; /* 802.11a */
280 else if (channel->hw_value & CHANNEL_CCK)
281 clock = 22; /* 802.11b */
282 else
283 clock = 44; /* 802.11g */
284
285 /* Clock rate in turbo modes is twice the normal rate */
286 if (channel->hw_value & CHANNEL_TURBO)
287 clock *= 2;
288
289 return clock;
290}
291
292/**
Lukáš Turek6e08d222009-12-21 22:50:51 +0100293 * ath5k_hw_get_default_slottime - Get the default slot time for current mode
294 *
295 * @ah: The &struct ath5k_hw
296 */
Pavel Roskin626ede62010-02-18 20:28:02 -0500297static unsigned int ath5k_hw_get_default_slottime(struct ath5k_hw *ah)
Lukáš Turek6e08d222009-12-21 22:50:51 +0100298{
299 struct ieee80211_channel *channel = ah->ah_current_channel;
300
301 if (channel->hw_value & CHANNEL_TURBO)
302 return 6; /* both turbo modes */
303
304 if (channel->hw_value & CHANNEL_CCK)
305 return 20; /* 802.11b */
306
307 return 9; /* 802.11 a/g */
308}
309
310/**
311 * ath5k_hw_get_default_sifs - Get the default SIFS for current mode
312 *
313 * @ah: The &struct ath5k_hw
314 */
Pavel Roskin626ede62010-02-18 20:28:02 -0500315static unsigned int ath5k_hw_get_default_sifs(struct ath5k_hw *ah)
Lukáš Turek6e08d222009-12-21 22:50:51 +0100316{
317 struct ieee80211_channel *channel = ah->ah_current_channel;
318
319 if (channel->hw_value & CHANNEL_TURBO)
320 return 8; /* both turbo modes */
321
322 if (channel->hw_value & CHANNEL_5GHZ)
323 return 16; /* 802.11a */
324
325 return 10; /* 802.11 b/g */
326}
327
328/**
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300329 * ath5k_hw_set_lladdr - Set station id
330 *
331 * @ah: The &struct ath5k_hw
332 * @mac: The card's mac address
333 *
334 * Set station id on hw using the provided mac address
335 */
336int ath5k_hw_set_lladdr(struct ath5k_hw *ah, const u8 *mac)
337{
Luis R. Rodriguez954fece2009-09-10 10:51:33 -0700338 struct ath_common *common = ath5k_hw_common(ah);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300339 u32 low_id, high_id;
Bob Copelandf6bac3e2008-11-26 16:17:11 -0500340 u32 pcu_reg;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300341
342 ATH5K_TRACE(ah->ah_sc);
343 /* Set new station ID */
Luis R. Rodriguez954fece2009-09-10 10:51:33 -0700344 memcpy(common->macaddr, mac, ETH_ALEN);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300345
Bob Copelandf6bac3e2008-11-26 16:17:11 -0500346 pcu_reg = ath5k_hw_reg_read(ah, AR5K_STA_ID1) & 0xffff0000;
347
Luis R. Rodriguezbcd8f542009-09-09 22:43:17 -0700348 low_id = get_unaligned_le32(mac);
349 high_id = get_unaligned_le16(mac + 4);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300350
351 ath5k_hw_reg_write(ah, low_id, AR5K_STA_ID0);
Bob Copelandf6bac3e2008-11-26 16:17:11 -0500352 ath5k_hw_reg_write(ah, pcu_reg | high_id, AR5K_STA_ID1);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300353
354 return 0;
355}
356
357/**
358 * ath5k_hw_set_associd - Set BSSID for association
359 *
360 * @ah: The &struct ath5k_hw
361 * @bssid: BSSID
362 * @assoc_id: Assoc id
363 *
364 * Sets the BSSID which trigers the "SME Join" operation
365 */
Luis R. Rodriguezbe5d6b72009-10-06 20:44:31 -0400366void ath5k_hw_set_associd(struct ath5k_hw *ah)
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300367{
Luis R. Rodriguez954fece2009-09-10 10:51:33 -0700368 struct ath_common *common = ath5k_hw_common(ah);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300369 u16 tim_offset = 0;
370
371 /*
372 * Set simple BSSID mask on 5212
373 */
Luis R. Rodrigueza72d57a2009-10-06 20:44:29 -0400374 if (ah->ah_version == AR5K_AR5212)
375 ath_hw_setbssidmask(common);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300376
377 /*
378 * Set BSSID which triggers the "SME Join" operation
379 */
Luis R. Rodriguezabba0682009-10-06 20:44:32 -0400380 ath5k_hw_reg_write(ah,
381 get_unaligned_le32(common->curbssid),
Luis R. Rodrigueza3f86bf2009-10-06 20:44:33 -0400382 AR5K_BSS_ID0);
Luis R. Rodriguezabba0682009-10-06 20:44:32 -0400383 ath5k_hw_reg_write(ah,
384 get_unaligned_le16(common->curbssid + 4) |
385 ((common->curaid & 0x3fff) << AR5K_BSS_ID1_AID_S),
Luis R. Rodrigueza3f86bf2009-10-06 20:44:33 -0400386 AR5K_BSS_ID1);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300387
Luis R. Rodriguezbe5d6b72009-10-06 20:44:31 -0400388 if (common->curaid == 0) {
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300389 ath5k_hw_disable_pspoll(ah);
390 return;
391 }
392
393 AR5K_REG_WRITE_BITS(ah, AR5K_BEACON, AR5K_BEACON_TIM,
Luis R. Rodriguezabba0682009-10-06 20:44:32 -0400394 tim_offset ? tim_offset + 4 : 0);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300395
396 ath5k_hw_enable_pspoll(ah, NULL, 0);
397}
398
Luis R. Rodriguez13b81552009-09-10 17:52:45 -0700399void ath5k_hw_set_bssid_mask(struct ath5k_hw *ah, const u8 *mask)
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300400{
Luis R. Rodriguez954fece2009-09-10 10:51:33 -0700401 struct ath_common *common = ath5k_hw_common(ah);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300402 ATH5K_TRACE(ah->ah_sc);
403
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200404 /* Cache bssid mask so that we can restore it
405 * on reset */
Luis R. Rodriguez954fece2009-09-10 10:51:33 -0700406 memcpy(common->bssidmask, mask, ETH_ALEN);
Luis R. Rodriguez13b81552009-09-10 17:52:45 -0700407 if (ah->ah_version == AR5K_AR5212)
408 ath_hw_setbssidmask(common);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300409}
410
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300411/************\
412* RX Control *
413\************/
414
415/**
416 * ath5k_hw_start_rx_pcu - Start RX engine
417 *
418 * @ah: The &struct ath5k_hw
419 *
420 * Starts RX engine on PCU so that hw can process RXed frames
421 * (ACK etc).
422 *
423 * NOTE: RX DMA should be already enabled using ath5k_hw_start_rx_dma
424 * TODO: Init ANI here
425 */
426void ath5k_hw_start_rx_pcu(struct ath5k_hw *ah)
427{
428 ATH5K_TRACE(ah->ah_sc);
429 AR5K_REG_DISABLE_BITS(ah, AR5K_DIAG_SW, AR5K_DIAG_SW_DIS_RX);
430}
431
432/**
433 * at5k_hw_stop_rx_pcu - Stop RX engine
434 *
435 * @ah: The &struct ath5k_hw
436 *
437 * Stops RX engine on PCU
438 *
439 * TODO: Detach ANI here
440 */
441void ath5k_hw_stop_rx_pcu(struct ath5k_hw *ah)
442{
443 ATH5K_TRACE(ah->ah_sc);
444 AR5K_REG_ENABLE_BITS(ah, AR5K_DIAG_SW, AR5K_DIAG_SW_DIS_RX);
445}
446
447/*
448 * Set multicast filter
449 */
450void ath5k_hw_set_mcast_filter(struct ath5k_hw *ah, u32 filter0, u32 filter1)
451{
452 ATH5K_TRACE(ah->ah_sc);
453 /* Set the multicat filter */
454 ath5k_hw_reg_write(ah, filter0, AR5K_MCAST_FILTER0);
455 ath5k_hw_reg_write(ah, filter1, AR5K_MCAST_FILTER1);
456}
457
Pavel Roskin626ede62010-02-18 20:28:02 -0500458#if 0
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300459/*
460 * Set multicast filter by index
461 */
462int ath5k_hw_set_mcast_filter_idx(struct ath5k_hw *ah, u32 index)
463{
464
465 ATH5K_TRACE(ah->ah_sc);
466 if (index >= 64)
467 return -EINVAL;
468 else if (index >= 32)
469 AR5K_REG_ENABLE_BITS(ah, AR5K_MCAST_FILTER1,
470 (1 << (index - 32)));
471 else
472 AR5K_REG_ENABLE_BITS(ah, AR5K_MCAST_FILTER0, (1 << index));
473
474 return 0;
475}
476
477/*
478 * Clear Multicast filter by index
479 */
480int ath5k_hw_clear_mcast_filter_idx(struct ath5k_hw *ah, u32 index)
481{
482
483 ATH5K_TRACE(ah->ah_sc);
484 if (index >= 64)
485 return -EINVAL;
486 else if (index >= 32)
487 AR5K_REG_DISABLE_BITS(ah, AR5K_MCAST_FILTER1,
488 (1 << (index - 32)));
489 else
490 AR5K_REG_DISABLE_BITS(ah, AR5K_MCAST_FILTER0, (1 << index));
491
492 return 0;
493}
Pavel Roskin626ede62010-02-18 20:28:02 -0500494#endif
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300495
496/**
497 * ath5k_hw_get_rx_filter - Get current rx filter
498 *
499 * @ah: The &struct ath5k_hw
500 *
501 * Returns the RX filter by reading rx filter and
502 * phy error filter registers. RX filter is used
503 * to set the allowed frame types that PCU will accept
504 * and pass to the driver. For a list of frame types
505 * check out reg.h.
506 */
507u32 ath5k_hw_get_rx_filter(struct ath5k_hw *ah)
508{
509 u32 data, filter = 0;
510
511 ATH5K_TRACE(ah->ah_sc);
512 filter = ath5k_hw_reg_read(ah, AR5K_RX_FILTER);
513
514 /*Radar detection for 5212*/
515 if (ah->ah_version == AR5K_AR5212) {
516 data = ath5k_hw_reg_read(ah, AR5K_PHY_ERR_FIL);
517
518 if (data & AR5K_PHY_ERR_FIL_RADAR)
519 filter |= AR5K_RX_FILTER_RADARERR;
520 if (data & (AR5K_PHY_ERR_FIL_OFDM | AR5K_PHY_ERR_FIL_CCK))
521 filter |= AR5K_RX_FILTER_PHYERR;
522 }
523
524 return filter;
525}
526
527/**
528 * ath5k_hw_set_rx_filter - Set rx filter
529 *
530 * @ah: The &struct ath5k_hw
531 * @filter: RX filter mask (see reg.h)
532 *
533 * Sets RX filter register and also handles PHY error filter
534 * register on 5212 and newer chips so that we have proper PHY
535 * error reporting.
536 */
537void ath5k_hw_set_rx_filter(struct ath5k_hw *ah, u32 filter)
538{
539 u32 data = 0;
540
541 ATH5K_TRACE(ah->ah_sc);
542
543 /* Set PHY error filter register on 5212*/
544 if (ah->ah_version == AR5K_AR5212) {
545 if (filter & AR5K_RX_FILTER_RADARERR)
546 data |= AR5K_PHY_ERR_FIL_RADAR;
547 if (filter & AR5K_RX_FILTER_PHYERR)
548 data |= AR5K_PHY_ERR_FIL_OFDM | AR5K_PHY_ERR_FIL_CCK;
549 }
550
551 /*
552 * The AR5210 uses promiscous mode to detect radar activity
553 */
554 if (ah->ah_version == AR5K_AR5210 &&
555 (filter & AR5K_RX_FILTER_RADARERR)) {
556 filter &= ~AR5K_RX_FILTER_RADARERR;
557 filter |= AR5K_RX_FILTER_PROM;
558 }
559
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200560 /*Zero length DMA (phy error reporting) */
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300561 if (data)
562 AR5K_REG_ENABLE_BITS(ah, AR5K_RXCFG, AR5K_RXCFG_ZLFDMA);
563 else
564 AR5K_REG_DISABLE_BITS(ah, AR5K_RXCFG, AR5K_RXCFG_ZLFDMA);
565
566 /*Write RX Filter register*/
567 ath5k_hw_reg_write(ah, filter & 0xff, AR5K_RX_FILTER);
568
569 /*Write PHY error filter register on 5212*/
570 if (ah->ah_version == AR5K_AR5212)
571 ath5k_hw_reg_write(ah, data, AR5K_PHY_ERR_FIL);
572
573}
574
575
576/****************\
577* Beacon control *
578\****************/
579
580/**
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300581 * ath5k_hw_get_tsf64 - Get the full 64bit TSF
582 *
583 * @ah: The &struct ath5k_hw
584 *
585 * Returns the current TSF
586 */
587u64 ath5k_hw_get_tsf64(struct ath5k_hw *ah)
588{
589 u64 tsf = ath5k_hw_reg_read(ah, AR5K_TSF_U32);
590 ATH5K_TRACE(ah->ah_sc);
591
592 return ath5k_hw_reg_read(ah, AR5K_TSF_L32) | (tsf << 32);
593}
594
595/**
Alina Friedrichsen8cab7582009-01-23 05:39:13 +0100596 * ath5k_hw_set_tsf64 - Set a new 64bit TSF
597 *
598 * @ah: The &struct ath5k_hw
599 * @tsf64: The new 64bit TSF
600 *
601 * Sets the new TSF
602 */
603void ath5k_hw_set_tsf64(struct ath5k_hw *ah, u64 tsf64)
604{
605 ATH5K_TRACE(ah->ah_sc);
606
Alina Friedrichsen8cab7582009-01-23 05:39:13 +0100607 ath5k_hw_reg_write(ah, tsf64 & 0xffffffff, AR5K_TSF_L32);
Alina Friedrichsen0ad65bd2009-03-02 23:29:48 +0100608 ath5k_hw_reg_write(ah, (tsf64 >> 32) & 0xffffffff, AR5K_TSF_U32);
Alina Friedrichsen8cab7582009-01-23 05:39:13 +0100609}
610
611/**
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300612 * ath5k_hw_reset_tsf - Force a TSF reset
613 *
614 * @ah: The &struct ath5k_hw
615 *
616 * Forces a TSF reset on PCU
617 */
618void ath5k_hw_reset_tsf(struct ath5k_hw *ah)
619{
Bob Copeland14be9942008-09-28 12:09:43 -0400620 u32 val;
621
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300622 ATH5K_TRACE(ah->ah_sc);
Bob Copeland14be9942008-09-28 12:09:43 -0400623
624 val = ath5k_hw_reg_read(ah, AR5K_BEACON) | AR5K_BEACON_RESET_TSF;
625
626 /*
627 * Each write to the RESET_TSF bit toggles a hardware internal
628 * signal to reset TSF, but if left high it will cause a TSF reset
629 * on the next chip reset as well. Thus we always write the value
630 * twice to clear the signal.
631 */
632 ath5k_hw_reg_write(ah, val, AR5K_BEACON);
633 ath5k_hw_reg_write(ah, val, AR5K_BEACON);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300634}
635
636/*
637 * Initialize beacon timers
638 */
639void ath5k_hw_init_beacon(struct ath5k_hw *ah, u32 next_beacon, u32 interval)
640{
641 u32 timer1, timer2, timer3;
642
643 ATH5K_TRACE(ah->ah_sc);
644 /*
645 * Set the additional timers by mode
646 */
647 switch (ah->ah_op_mode) {
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200648 case NL80211_IFTYPE_MONITOR:
Johannes Berg05c914f2008-09-11 00:01:58 +0200649 case NL80211_IFTYPE_STATION:
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200650 /* In STA mode timer1 is used as next wakeup
651 * timer and timer2 as next CFP duration start
652 * timer. Both in 1/8TUs. */
653 /* TODO: PCF handling */
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300654 if (ah->ah_version == AR5K_AR5210) {
655 timer1 = 0xffffffff;
656 timer2 = 0xffffffff;
657 } else {
658 timer1 = 0x0000ffff;
659 timer2 = 0x0007ffff;
660 }
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200661 /* Mark associated AP as PCF incapable for now */
662 AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1, AR5K_STA_ID1_PCF);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300663 break;
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200664 case NL80211_IFTYPE_ADHOC:
665 AR5K_REG_ENABLE_BITS(ah, AR5K_TXCFG, AR5K_TXCFG_ADHOC_BCN_ATIM);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300666 default:
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200667 /* On non-STA modes timer1 is used as next DMA
668 * beacon alert (DBA) timer and timer2 as next
669 * software beacon alert. Both in 1/8TUs. */
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300670 timer1 = (next_beacon - AR5K_TUNE_DMA_BEACON_RESP) << 3;
671 timer2 = (next_beacon - AR5K_TUNE_SW_BEACON_RESP) << 3;
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200672 break;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300673 }
674
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200675 /* Timer3 marks the end of our ATIM window
676 * a zero length window is not allowed because
677 * we 'll get no beacons */
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300678 timer3 = next_beacon + (ah->ah_atim_window ? ah->ah_atim_window : 1);
679
680 /*
681 * Set the beacon register and enable all timers.
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300682 */
Nick Kossifidis35edf8a2009-06-12 16:09:53 -0700683 /* When in AP or Mesh Point mode zero timer0 to start TSF */
684 if (ah->ah_op_mode == NL80211_IFTYPE_AP ||
685 ah->ah_op_mode == NL80211_IFTYPE_MESH_POINT)
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200686 ath5k_hw_reg_write(ah, 0, AR5K_TIMER0);
Nick Kossifidis428cbd42009-04-30 15:55:47 -0400687
688 ath5k_hw_reg_write(ah, next_beacon, AR5K_TIMER0);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300689 ath5k_hw_reg_write(ah, timer1, AR5K_TIMER1);
690 ath5k_hw_reg_write(ah, timer2, AR5K_TIMER2);
691 ath5k_hw_reg_write(ah, timer3, AR5K_TIMER3);
692
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200693 /* Force a TSF reset if requested and enable beacons */
694 if (interval & AR5K_BEACON_RESET_TSF)
695 ath5k_hw_reset_tsf(ah);
696
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300697 ath5k_hw_reg_write(ah, interval & (AR5K_BEACON_PERIOD |
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200698 AR5K_BEACON_ENABLE),
699 AR5K_BEACON);
700
701 /* Flush any pending BMISS interrupts on ISR by
702 * performing a clear-on-write operation on PISR
703 * register for the BMISS bit (writing a bit on
704 * ISR togles a reset for that bit and leaves
705 * the rest bits intact) */
706 if (ah->ah_version == AR5K_AR5210)
707 ath5k_hw_reg_write(ah, AR5K_ISR_BMISS, AR5K_ISR);
708 else
709 ath5k_hw_reg_write(ah, AR5K_ISR_BMISS, AR5K_PISR);
710
711 /* TODO: Set enchanced sleep registers on AR5212
712 * based on vif->bss_conf params, until then
713 * disable power save reporting.*/
714 AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1, AR5K_STA_ID1_PWR_SV);
715
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300716}
717
718#if 0
719/*
720 * Set beacon timers
721 */
722int ath5k_hw_set_beacon_timers(struct ath5k_hw *ah,
723 const struct ath5k_beacon_state *state)
724{
725 u32 cfp_period, next_cfp, dtim, interval, next_beacon;
726
727 /*
728 * TODO: should be changed through *state
729 * review struct ath5k_beacon_state struct
730 *
731 * XXX: These are used for cfp period bellow, are they
732 * ok ? Is it O.K. for tsf here to be 0 or should we use
733 * get_tsf ?
734 */
735 u32 dtim_count = 0; /* XXX */
736 u32 cfp_count = 0; /* XXX */
737 u32 tsf = 0; /* XXX */
738
739 ATH5K_TRACE(ah->ah_sc);
740 /* Return on an invalid beacon state */
741 if (state->bs_interval < 1)
742 return -EINVAL;
743
744 interval = state->bs_interval;
745 dtim = state->bs_dtim_period;
746
747 /*
748 * PCF support?
749 */
750 if (state->bs_cfp_period > 0) {
751 /*
752 * Enable PCF mode and set the CFP
753 * (Contention Free Period) and timer registers
754 */
755 cfp_period = state->bs_cfp_period * state->bs_dtim_period *
756 state->bs_interval;
757 next_cfp = (cfp_count * state->bs_dtim_period + dtim_count) *
758 state->bs_interval;
759
760 AR5K_REG_ENABLE_BITS(ah, AR5K_STA_ID1,
761 AR5K_STA_ID1_DEFAULT_ANTENNA |
762 AR5K_STA_ID1_PCF);
763 ath5k_hw_reg_write(ah, cfp_period, AR5K_CFP_PERIOD);
764 ath5k_hw_reg_write(ah, state->bs_cfp_max_duration,
765 AR5K_CFP_DUR);
766 ath5k_hw_reg_write(ah, (tsf + (next_cfp == 0 ? cfp_period :
767 next_cfp)) << 3, AR5K_TIMER2);
768 } else {
769 /* Disable PCF mode */
770 AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1,
771 AR5K_STA_ID1_DEFAULT_ANTENNA |
772 AR5K_STA_ID1_PCF);
773 }
774
775 /*
776 * Enable the beacon timer register
777 */
778 ath5k_hw_reg_write(ah, state->bs_next_beacon, AR5K_TIMER0);
779
780 /*
781 * Start the beacon timers
782 */
783 ath5k_hw_reg_write(ah, (ath5k_hw_reg_read(ah, AR5K_BEACON) &
784 ~(AR5K_BEACON_PERIOD | AR5K_BEACON_TIM)) |
785 AR5K_REG_SM(state->bs_tim_offset ? state->bs_tim_offset + 4 : 0,
786 AR5K_BEACON_TIM) | AR5K_REG_SM(state->bs_interval,
787 AR5K_BEACON_PERIOD), AR5K_BEACON);
788
789 /*
790 * Write new beacon miss threshold, if it appears to be valid
791 * XXX: Figure out right values for min <= bs_bmiss_threshold <= max
792 * and return if its not in range. We can test this by reading value and
793 * setting value to a largest value and seeing which values register.
794 */
795
796 AR5K_REG_WRITE_BITS(ah, AR5K_RSSI_THR, AR5K_RSSI_THR_BMISS,
797 state->bs_bmiss_threshold);
798
799 /*
800 * Set sleep control register
801 * XXX: Didn't find this in 5210 code but since this register
802 * exists also in ar5k's 5210 headers i leave it as common code.
803 */
804 AR5K_REG_WRITE_BITS(ah, AR5K_SLEEP_CTL, AR5K_SLEEP_CTL_SLDUR,
805 (state->bs_sleep_duration - 3) << 3);
806
807 /*
808 * Set enhanced sleep registers on 5212
809 */
810 if (ah->ah_version == AR5K_AR5212) {
811 if (state->bs_sleep_duration > state->bs_interval &&
812 roundup(state->bs_sleep_duration, interval) ==
813 state->bs_sleep_duration)
814 interval = state->bs_sleep_duration;
815
816 if (state->bs_sleep_duration > dtim && (dtim == 0 ||
817 roundup(state->bs_sleep_duration, dtim) ==
818 state->bs_sleep_duration))
819 dtim = state->bs_sleep_duration;
820
821 if (interval > dtim)
822 return -EINVAL;
823
824 next_beacon = interval == dtim ? state->bs_next_dtim :
825 state->bs_next_beacon;
826
827 ath5k_hw_reg_write(ah,
828 AR5K_REG_SM((state->bs_next_dtim - 3) << 3,
829 AR5K_SLEEP0_NEXT_DTIM) |
830 AR5K_REG_SM(10, AR5K_SLEEP0_CABTO) |
831 AR5K_SLEEP0_ENH_SLEEP_EN |
832 AR5K_SLEEP0_ASSUME_DTIM, AR5K_SLEEP0);
833
834 ath5k_hw_reg_write(ah, AR5K_REG_SM((next_beacon - 3) << 3,
835 AR5K_SLEEP1_NEXT_TIM) |
836 AR5K_REG_SM(10, AR5K_SLEEP1_BEACON_TO), AR5K_SLEEP1);
837
838 ath5k_hw_reg_write(ah,
839 AR5K_REG_SM(interval, AR5K_SLEEP2_TIM_PER) |
840 AR5K_REG_SM(dtim, AR5K_SLEEP2_DTIM_PER), AR5K_SLEEP2);
841 }
842
843 return 0;
844}
845
846/*
847 * Reset beacon timers
848 */
849void ath5k_hw_reset_beacon(struct ath5k_hw *ah)
850{
851 ATH5K_TRACE(ah->ah_sc);
852 /*
853 * Disable beacon timer
854 */
855 ath5k_hw_reg_write(ah, 0, AR5K_TIMER0);
856
857 /*
858 * Disable some beacon register values
859 */
860 AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1,
861 AR5K_STA_ID1_DEFAULT_ANTENNA | AR5K_STA_ID1_PCF);
862 ath5k_hw_reg_write(ah, AR5K_BEACON_PERIOD, AR5K_BEACON);
863}
864
865/*
866 * Wait for beacon queue to finish
867 */
868int ath5k_hw_beaconq_finish(struct ath5k_hw *ah, unsigned long phys_addr)
869{
870 unsigned int i;
871 int ret;
872
873 ATH5K_TRACE(ah->ah_sc);
874
875 /* 5210 doesn't have QCU*/
876 if (ah->ah_version == AR5K_AR5210) {
877 /*
878 * Wait for beaconn queue to finish by checking
879 * Control Register and Beacon Status Register.
880 */
881 for (i = AR5K_TUNE_BEACON_INTERVAL / 2; i > 0; i--) {
882 if (!(ath5k_hw_reg_read(ah, AR5K_BSR) & AR5K_BSR_TXQ1F)
883 ||
884 !(ath5k_hw_reg_read(ah, AR5K_CR) & AR5K_BSR_TXQ1F))
885 break;
886 udelay(10);
887 }
888
889 /* Timeout... */
890 if (i <= 0) {
891 /*
892 * Re-schedule the beacon queue
893 */
894 ath5k_hw_reg_write(ah, phys_addr, AR5K_NOQCU_TXDP1);
895 ath5k_hw_reg_write(ah, AR5K_BCR_TQ1V | AR5K_BCR_BDMAE,
896 AR5K_BCR);
897
898 return -EIO;
899 }
900 ret = 0;
901 } else {
902 /*5211/5212*/
903 ret = ath5k_hw_register_timeout(ah,
904 AR5K_QUEUE_STATUS(AR5K_TX_QUEUE_ID_BEACON),
905 AR5K_QCU_STS_FRMPENDCNT, 0, false);
906
907 if (AR5K_REG_READ_Q(ah, AR5K_QCU_TXE, AR5K_TX_QUEUE_ID_BEACON))
908 return -EIO;
909 }
910
911 return ret;
912}
913#endif
914
915
916/*********************\
917* Key table functions *
918\*********************/
919
920/*
921 * Reset a key entry on the table
922 */
923int ath5k_hw_reset_key(struct ath5k_hw *ah, u16 entry)
924{
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200925 unsigned int i, type;
Bob Copeland17683c62008-10-29 23:24:26 -0400926 u16 micentry = entry + AR5K_KEYTABLE_MIC_OFFSET;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300927
928 ATH5K_TRACE(ah->ah_sc);
929 AR5K_ASSERT_ENTRY(entry, AR5K_KEYTABLE_SIZE);
930
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200931 type = ath5k_hw_reg_read(ah, AR5K_KEYTABLE_TYPE(entry));
932
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300933 for (i = 0; i < AR5K_KEYCACHE_SIZE; i++)
934 ath5k_hw_reg_write(ah, 0, AR5K_KEYTABLE_OFF(entry, i));
935
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200936 /* Reset associated MIC entry if TKIP
937 * is enabled located at offset (entry + 64) */
938 if (type == AR5K_KEYTABLE_TYPE_TKIP) {
Bob Copeland17683c62008-10-29 23:24:26 -0400939 AR5K_ASSERT_ENTRY(micentry, AR5K_KEYTABLE_SIZE);
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200940 for (i = 0; i < AR5K_KEYCACHE_SIZE / 2 ; i++)
Bob Copeland17683c62008-10-29 23:24:26 -0400941 ath5k_hw_reg_write(ah, 0,
942 AR5K_KEYTABLE_OFF(micentry, i));
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200943 }
944
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300945 /*
946 * Set NULL encryption on AR5212+
947 *
948 * Note: AR5K_KEYTABLE_TYPE -> AR5K_KEYTABLE_OFF(entry, 5)
949 * AR5K_KEYTABLE_TYPE_NULL -> 0x00000007
950 *
951 * Note2: Windows driver (ndiswrapper) sets this to
952 * 0x00000714 instead of 0x00000007
953 */
Jiri Slabyded7a7e2009-04-25 14:09:23 +0200954 if (ah->ah_version >= AR5K_AR5211) {
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300955 ath5k_hw_reg_write(ah, AR5K_KEYTABLE_TYPE_NULL,
956 AR5K_KEYTABLE_TYPE(entry));
957
Bob Copeland17683c62008-10-29 23:24:26 -0400958 if (type == AR5K_KEYTABLE_TYPE_TKIP) {
959 ath5k_hw_reg_write(ah, AR5K_KEYTABLE_TYPE_NULL,
960 AR5K_KEYTABLE_TYPE(micentry));
961 }
962 }
963
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300964 return 0;
965}
966
Pavel Roskin626ede62010-02-18 20:28:02 -0500967#if 0
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300968/*
969 * Check if a table entry is valid
970 */
971int ath5k_hw_is_key_valid(struct ath5k_hw *ah, u16 entry)
972{
973 ATH5K_TRACE(ah->ah_sc);
974 AR5K_ASSERT_ENTRY(entry, AR5K_KEYTABLE_SIZE);
975
976 /* Check the validation flag at the end of the entry */
977 return ath5k_hw_reg_read(ah, AR5K_KEYTABLE_MAC1(entry)) &
978 AR5K_KEYTABLE_VALID;
979}
Pavel Roskin626ede62010-02-18 20:28:02 -0500980#endif
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300981
Bob Copeland67143492008-11-25 20:55:21 -0500982static
983int ath5k_keycache_type(const struct ieee80211_key_conf *key)
984{
985 switch (key->alg) {
986 case ALG_TKIP:
987 return AR5K_KEYTABLE_TYPE_TKIP;
988 case ALG_CCMP:
989 return AR5K_KEYTABLE_TYPE_CCM;
990 case ALG_WEP:
Zhu Yie31a16d2009-05-21 21:47:03 +0800991 if (key->keylen == WLAN_KEY_LEN_WEP40)
Bob Copeland67143492008-11-25 20:55:21 -0500992 return AR5K_KEYTABLE_TYPE_40;
Zhu Yie31a16d2009-05-21 21:47:03 +0800993 else if (key->keylen == WLAN_KEY_LEN_WEP104)
Bob Copeland67143492008-11-25 20:55:21 -0500994 return AR5K_KEYTABLE_TYPE_104;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200995 return -EINVAL;
996 default:
997 return -EINVAL;
Bob Copeland67143492008-11-25 20:55:21 -0500998 }
999 return -EINVAL;
1000}
1001
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001002/*
1003 * Set a key entry on the table
1004 */
1005int ath5k_hw_set_key(struct ath5k_hw *ah, u16 entry,
1006 const struct ieee80211_key_conf *key, const u8 *mac)
1007{
1008 unsigned int i;
Bob Copeland3f64b432008-10-29 23:19:14 -04001009 int keylen;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001010 __le32 key_v[5] = {};
Bob Copeland3f64b432008-10-29 23:19:14 -04001011 __le32 key0 = 0, key1 = 0;
1012 __le32 *rxmic, *txmic;
Roel Kluin672cf3c2009-01-18 23:50:27 +01001013 int keytype;
Bob Copeland3f64b432008-10-29 23:19:14 -04001014 u16 micentry = entry + AR5K_KEYTABLE_MIC_OFFSET;
1015 bool is_tkip;
Bob Copeland67143492008-11-25 20:55:21 -05001016 const u8 *key_ptr;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001017
1018 ATH5K_TRACE(ah->ah_sc);
1019
Bob Copeland3f64b432008-10-29 23:19:14 -04001020 is_tkip = (key->alg == ALG_TKIP);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001021
Bob Copeland3f64b432008-10-29 23:19:14 -04001022 /*
1023 * key->keylen comes in from mac80211 in bytes.
1024 * TKIP is 128 bit + 128 bit mic
1025 */
1026 keylen = (is_tkip) ? (128 / 8) : key->keylen;
1027
1028 if (entry > AR5K_KEYTABLE_SIZE ||
1029 (is_tkip && micentry > AR5K_KEYTABLE_SIZE))
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001030 return -EOPNOTSUPP;
1031
Bob Copeland67143492008-11-25 20:55:21 -05001032 if (unlikely(keylen > 16))
1033 return -EOPNOTSUPP;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001034
Bob Copeland67143492008-11-25 20:55:21 -05001035 keytype = ath5k_keycache_type(key);
1036 if (keytype < 0)
1037 return keytype;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001038
Bob Copeland67143492008-11-25 20:55:21 -05001039 /*
1040 * each key block is 6 bytes wide, written as pairs of
1041 * alternating 32 and 16 bit le values.
1042 */
1043 key_ptr = key->key;
1044 for (i = 0; keylen >= 6; keylen -= 6) {
1045 memcpy(&key_v[i], key_ptr, 6);
1046 i += 2;
1047 key_ptr += 6;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001048 }
Bob Copeland67143492008-11-25 20:55:21 -05001049 if (keylen)
1050 memcpy(&key_v[i], key_ptr, keylen);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001051
Bob Copeland3f64b432008-10-29 23:19:14 -04001052 /* intentionally corrupt key until mic is installed */
1053 if (is_tkip) {
1054 key0 = key_v[0] = ~key_v[0];
1055 key1 = key_v[1] = ~key_v[1];
1056 }
1057
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001058 for (i = 0; i < ARRAY_SIZE(key_v); i++)
1059 ath5k_hw_reg_write(ah, le32_to_cpu(key_v[i]),
1060 AR5K_KEYTABLE_OFF(entry, i));
1061
1062 ath5k_hw_reg_write(ah, keytype, AR5K_KEYTABLE_TYPE(entry));
1063
Bob Copeland3f64b432008-10-29 23:19:14 -04001064 if (is_tkip) {
1065 /* Install rx/tx MIC */
1066 rxmic = (__le32 *) &key->key[16];
1067 txmic = (__le32 *) &key->key[24];
Bob Copelandf6504702008-11-26 16:17:25 -05001068
1069 if (ah->ah_combined_mic) {
1070 key_v[0] = rxmic[0];
Bob Copeland388cdf32008-12-09 23:05:38 -05001071 key_v[1] = cpu_to_le32(le32_to_cpu(txmic[0]) >> 16);
Bob Copelandf6504702008-11-26 16:17:25 -05001072 key_v[2] = rxmic[1];
Bob Copeland388cdf32008-12-09 23:05:38 -05001073 key_v[3] = cpu_to_le32(le32_to_cpu(txmic[0]) & 0xffff);
Bob Copelandf6504702008-11-26 16:17:25 -05001074 key_v[4] = txmic[1];
1075 } else {
1076 key_v[0] = rxmic[0];
1077 key_v[1] = 0;
1078 key_v[2] = rxmic[1];
1079 key_v[3] = 0;
1080 key_v[4] = 0;
1081 }
Bob Copeland3f64b432008-10-29 23:19:14 -04001082 for (i = 0; i < ARRAY_SIZE(key_v); i++)
1083 ath5k_hw_reg_write(ah, le32_to_cpu(key_v[i]),
1084 AR5K_KEYTABLE_OFF(micentry, i));
1085
1086 ath5k_hw_reg_write(ah, AR5K_KEYTABLE_TYPE_NULL,
1087 AR5K_KEYTABLE_TYPE(micentry));
1088 ath5k_hw_reg_write(ah, 0, AR5K_KEYTABLE_MAC0(micentry));
1089 ath5k_hw_reg_write(ah, 0, AR5K_KEYTABLE_MAC1(micentry));
1090
1091 /* restore first 2 words of key */
1092 ath5k_hw_reg_write(ah, le32_to_cpu(~key0),
1093 AR5K_KEYTABLE_OFF(entry, 0));
1094 ath5k_hw_reg_write(ah, le32_to_cpu(~key1),
1095 AR5K_KEYTABLE_OFF(entry, 1));
1096 }
1097
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001098 return ath5k_hw_set_key_lladdr(ah, entry, mac);
1099}
1100
1101int ath5k_hw_set_key_lladdr(struct ath5k_hw *ah, u16 entry, const u8 *mac)
1102{
1103 u32 low_id, high_id;
1104
1105 ATH5K_TRACE(ah->ah_sc);
1106 /* Invalid entry (key table overflow) */
1107 AR5K_ASSERT_ENTRY(entry, AR5K_KEYTABLE_SIZE);
1108
Luis R. Rodriguezbcd8f542009-09-09 22:43:17 -07001109 /*
1110 * MAC may be NULL if it's a broadcast key. In this case no need to
1111 * to compute get_unaligned_le32 and get_unaligned_le16 as we
1112 * already know it.
1113 */
Johannes Bergdc822b52008-12-29 12:55:09 +01001114 if (!mac) {
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001115 low_id = 0xffffffff;
1116 high_id = 0xffff | AR5K_KEYTABLE_VALID;
1117 } else {
Luis R. Rodriguezbcd8f542009-09-09 22:43:17 -07001118 low_id = get_unaligned_le32(mac);
1119 high_id = get_unaligned_le16(mac + 4) | AR5K_KEYTABLE_VALID;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001120 }
1121
1122 ath5k_hw_reg_write(ah, low_id, AR5K_KEYTABLE_MAC0(entry));
1123 ath5k_hw_reg_write(ah, high_id, AR5K_KEYTABLE_MAC1(entry));
1124
1125 return 0;
1126}
1127
Lukáš Turek6e08d222009-12-21 22:50:51 +01001128/**
1129 * ath5k_hw_set_coverage_class - Set IEEE 802.11 coverage class
1130 *
1131 * @ah: The &struct ath5k_hw
1132 * @coverage_class: IEEE 802.11 coverage class number
1133 *
1134 * Sets slot time, ACK timeout and CTS timeout for given coverage class.
1135 */
1136void ath5k_hw_set_coverage_class(struct ath5k_hw *ah, u8 coverage_class)
1137{
1138 /* As defined by IEEE 802.11-2007 17.3.8.6 */
1139 int slot_time = ath5k_hw_get_default_slottime(ah) + 3 * coverage_class;
1140 int ack_timeout = ath5k_hw_get_default_sifs(ah) + slot_time;
1141 int cts_timeout = ack_timeout;
1142
1143 ath5k_hw_set_slot_time(ah, slot_time);
1144 ath5k_hw_set_ack_timeout(ah, ack_timeout);
1145 ath5k_hw_set_cts_timeout(ah, cts_timeout);
1146
1147 ah->ah_coverage_class = coverage_class;
1148}