blob: b601ff9865fde542ecb4412648f542e7aac39711 [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
181/**
182 * ath5k_hw_het_ack_timeout - Get ACK timeout from PCU in usec
183 *
184 * @ah: The &struct ath5k_hw
185 */
186unsigned int ath5k_hw_get_ack_timeout(struct ath5k_hw *ah)
187{
188 ATH5K_TRACE(ah->ah_sc);
189
Lukáš Turek3578e6e2009-12-21 22:50:50 +0100190 return ath5k_hw_clocktoh(ah, AR5K_REG_MS(ath5k_hw_reg_read(ah,
191 AR5K_TIME_OUT), AR5K_TIME_OUT_ACK));
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300192}
193
194/**
195 * ath5k_hw_set_ack_timeout - Set ACK timeout on PCU
196 *
197 * @ah: The &struct ath5k_hw
198 * @timeout: Timeout in usec
199 */
200int ath5k_hw_set_ack_timeout(struct ath5k_hw *ah, unsigned int timeout)
201{
202 ATH5K_TRACE(ah->ah_sc);
Lukáš Turek3578e6e2009-12-21 22:50:50 +0100203 if (ath5k_hw_clocktoh(ah, AR5K_REG_MS(0xffffffff, AR5K_TIME_OUT_ACK))
204 <= timeout)
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300205 return -EINVAL;
206
207 AR5K_REG_WRITE_BITS(ah, AR5K_TIME_OUT, AR5K_TIME_OUT_ACK,
Lukáš Turek3578e6e2009-12-21 22:50:50 +0100208 ath5k_hw_htoclock(ah, timeout));
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300209
210 return 0;
211}
212
213/**
214 * ath5k_hw_get_cts_timeout - Get CTS timeout from PCU in usec
215 *
216 * @ah: The &struct ath5k_hw
217 */
218unsigned int ath5k_hw_get_cts_timeout(struct ath5k_hw *ah)
219{
220 ATH5K_TRACE(ah->ah_sc);
Lukáš Turek3578e6e2009-12-21 22:50:50 +0100221 return ath5k_hw_clocktoh(ah, AR5K_REG_MS(ath5k_hw_reg_read(ah,
222 AR5K_TIME_OUT), AR5K_TIME_OUT_CTS));
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300223}
224
225/**
226 * ath5k_hw_set_cts_timeout - Set CTS timeout on PCU
227 *
228 * @ah: The &struct ath5k_hw
229 * @timeout: Timeout in usec
230 */
231int ath5k_hw_set_cts_timeout(struct ath5k_hw *ah, unsigned int timeout)
232{
233 ATH5K_TRACE(ah->ah_sc);
Lukáš Turek3578e6e2009-12-21 22:50:50 +0100234 if (ath5k_hw_clocktoh(ah, AR5K_REG_MS(0xffffffff, AR5K_TIME_OUT_CTS))
235 <= timeout)
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300236 return -EINVAL;
237
238 AR5K_REG_WRITE_BITS(ah, AR5K_TIME_OUT, AR5K_TIME_OUT_CTS,
Lukáš Turek3578e6e2009-12-21 22:50:50 +0100239 ath5k_hw_htoclock(ah, timeout));
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300240
241 return 0;
242}
243
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300244/**
Lukáš Turek3578e6e2009-12-21 22:50:50 +0100245 * ath5k_hw_htoclock - Translate usec to hw clock units
246 *
247 * @ah: The &struct ath5k_hw
248 * @usec: value in microseconds
249 */
250unsigned int ath5k_hw_htoclock(struct ath5k_hw *ah, unsigned int usec)
251{
252 return usec * ath5k_hw_get_clockrate(ah);
253}
254
255/**
256 * ath5k_hw_clocktoh - Translate hw clock units to usec
257 * @clock: value in hw clock units
258 */
259unsigned int ath5k_hw_clocktoh(struct ath5k_hw *ah, unsigned int clock)
260{
261 return clock / ath5k_hw_get_clockrate(ah);
262}
263
264/**
265 * ath5k_hw_get_clockrate - Get the clock rate for current mode
266 *
267 * @ah: The &struct ath5k_hw
268 */
269unsigned int ath5k_hw_get_clockrate(struct ath5k_hw *ah)
270{
271 struct ieee80211_channel *channel = ah->ah_current_channel;
272 int clock;
273
274 if (channel->hw_value & CHANNEL_5GHZ)
275 clock = 40; /* 802.11a */
276 else if (channel->hw_value & CHANNEL_CCK)
277 clock = 22; /* 802.11b */
278 else
279 clock = 44; /* 802.11g */
280
281 /* Clock rate in turbo modes is twice the normal rate */
282 if (channel->hw_value & CHANNEL_TURBO)
283 clock *= 2;
284
285 return clock;
286}
287
288/**
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300289 * ath5k_hw_set_lladdr - Set station id
290 *
291 * @ah: The &struct ath5k_hw
292 * @mac: The card's mac address
293 *
294 * Set station id on hw using the provided mac address
295 */
296int ath5k_hw_set_lladdr(struct ath5k_hw *ah, const u8 *mac)
297{
Luis R. Rodriguez954fece2009-09-10 10:51:33 -0700298 struct ath_common *common = ath5k_hw_common(ah);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300299 u32 low_id, high_id;
Bob Copelandf6bac3e2008-11-26 16:17:11 -0500300 u32 pcu_reg;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300301
302 ATH5K_TRACE(ah->ah_sc);
303 /* Set new station ID */
Luis R. Rodriguez954fece2009-09-10 10:51:33 -0700304 memcpy(common->macaddr, mac, ETH_ALEN);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300305
Bob Copelandf6bac3e2008-11-26 16:17:11 -0500306 pcu_reg = ath5k_hw_reg_read(ah, AR5K_STA_ID1) & 0xffff0000;
307
Luis R. Rodriguezbcd8f542009-09-09 22:43:17 -0700308 low_id = get_unaligned_le32(mac);
309 high_id = get_unaligned_le16(mac + 4);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300310
311 ath5k_hw_reg_write(ah, low_id, AR5K_STA_ID0);
Bob Copelandf6bac3e2008-11-26 16:17:11 -0500312 ath5k_hw_reg_write(ah, pcu_reg | high_id, AR5K_STA_ID1);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300313
314 return 0;
315}
316
317/**
318 * ath5k_hw_set_associd - Set BSSID for association
319 *
320 * @ah: The &struct ath5k_hw
321 * @bssid: BSSID
322 * @assoc_id: Assoc id
323 *
324 * Sets the BSSID which trigers the "SME Join" operation
325 */
Luis R. Rodriguezbe5d6b72009-10-06 20:44:31 -0400326void ath5k_hw_set_associd(struct ath5k_hw *ah)
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300327{
Luis R. Rodriguez954fece2009-09-10 10:51:33 -0700328 struct ath_common *common = ath5k_hw_common(ah);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300329 u16 tim_offset = 0;
330
331 /*
332 * Set simple BSSID mask on 5212
333 */
Luis R. Rodrigueza72d57a2009-10-06 20:44:29 -0400334 if (ah->ah_version == AR5K_AR5212)
335 ath_hw_setbssidmask(common);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300336
337 /*
338 * Set BSSID which triggers the "SME Join" operation
339 */
Luis R. Rodriguezabba0682009-10-06 20:44:32 -0400340 ath5k_hw_reg_write(ah,
341 get_unaligned_le32(common->curbssid),
Luis R. Rodrigueza3f86bf2009-10-06 20:44:33 -0400342 AR5K_BSS_ID0);
Luis R. Rodriguezabba0682009-10-06 20:44:32 -0400343 ath5k_hw_reg_write(ah,
344 get_unaligned_le16(common->curbssid + 4) |
345 ((common->curaid & 0x3fff) << AR5K_BSS_ID1_AID_S),
Luis R. Rodrigueza3f86bf2009-10-06 20:44:33 -0400346 AR5K_BSS_ID1);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300347
Luis R. Rodriguezbe5d6b72009-10-06 20:44:31 -0400348 if (common->curaid == 0) {
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300349 ath5k_hw_disable_pspoll(ah);
350 return;
351 }
352
353 AR5K_REG_WRITE_BITS(ah, AR5K_BEACON, AR5K_BEACON_TIM,
Luis R. Rodriguezabba0682009-10-06 20:44:32 -0400354 tim_offset ? tim_offset + 4 : 0);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300355
356 ath5k_hw_enable_pspoll(ah, NULL, 0);
357}
358
Luis R. Rodriguez13b81552009-09-10 17:52:45 -0700359void ath5k_hw_set_bssid_mask(struct ath5k_hw *ah, const u8 *mask)
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300360{
Luis R. Rodriguez954fece2009-09-10 10:51:33 -0700361 struct ath_common *common = ath5k_hw_common(ah);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300362 ATH5K_TRACE(ah->ah_sc);
363
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200364 /* Cache bssid mask so that we can restore it
365 * on reset */
Luis R. Rodriguez954fece2009-09-10 10:51:33 -0700366 memcpy(common->bssidmask, mask, ETH_ALEN);
Luis R. Rodriguez13b81552009-09-10 17:52:45 -0700367 if (ah->ah_version == AR5K_AR5212)
368 ath_hw_setbssidmask(common);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300369}
370
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300371/************\
372* RX Control *
373\************/
374
375/**
376 * ath5k_hw_start_rx_pcu - Start RX engine
377 *
378 * @ah: The &struct ath5k_hw
379 *
380 * Starts RX engine on PCU so that hw can process RXed frames
381 * (ACK etc).
382 *
383 * NOTE: RX DMA should be already enabled using ath5k_hw_start_rx_dma
384 * TODO: Init ANI here
385 */
386void ath5k_hw_start_rx_pcu(struct ath5k_hw *ah)
387{
388 ATH5K_TRACE(ah->ah_sc);
389 AR5K_REG_DISABLE_BITS(ah, AR5K_DIAG_SW, AR5K_DIAG_SW_DIS_RX);
390}
391
392/**
393 * at5k_hw_stop_rx_pcu - Stop RX engine
394 *
395 * @ah: The &struct ath5k_hw
396 *
397 * Stops RX engine on PCU
398 *
399 * TODO: Detach ANI here
400 */
401void ath5k_hw_stop_rx_pcu(struct ath5k_hw *ah)
402{
403 ATH5K_TRACE(ah->ah_sc);
404 AR5K_REG_ENABLE_BITS(ah, AR5K_DIAG_SW, AR5K_DIAG_SW_DIS_RX);
405}
406
407/*
408 * Set multicast filter
409 */
410void ath5k_hw_set_mcast_filter(struct ath5k_hw *ah, u32 filter0, u32 filter1)
411{
412 ATH5K_TRACE(ah->ah_sc);
413 /* Set the multicat filter */
414 ath5k_hw_reg_write(ah, filter0, AR5K_MCAST_FILTER0);
415 ath5k_hw_reg_write(ah, filter1, AR5K_MCAST_FILTER1);
416}
417
418/*
419 * Set multicast filter by index
420 */
421int ath5k_hw_set_mcast_filter_idx(struct ath5k_hw *ah, u32 index)
422{
423
424 ATH5K_TRACE(ah->ah_sc);
425 if (index >= 64)
426 return -EINVAL;
427 else if (index >= 32)
428 AR5K_REG_ENABLE_BITS(ah, AR5K_MCAST_FILTER1,
429 (1 << (index - 32)));
430 else
431 AR5K_REG_ENABLE_BITS(ah, AR5K_MCAST_FILTER0, (1 << index));
432
433 return 0;
434}
435
436/*
437 * Clear Multicast filter by index
438 */
439int ath5k_hw_clear_mcast_filter_idx(struct ath5k_hw *ah, u32 index)
440{
441
442 ATH5K_TRACE(ah->ah_sc);
443 if (index >= 64)
444 return -EINVAL;
445 else if (index >= 32)
446 AR5K_REG_DISABLE_BITS(ah, AR5K_MCAST_FILTER1,
447 (1 << (index - 32)));
448 else
449 AR5K_REG_DISABLE_BITS(ah, AR5K_MCAST_FILTER0, (1 << index));
450
451 return 0;
452}
453
454/**
455 * ath5k_hw_get_rx_filter - Get current rx filter
456 *
457 * @ah: The &struct ath5k_hw
458 *
459 * Returns the RX filter by reading rx filter and
460 * phy error filter registers. RX filter is used
461 * to set the allowed frame types that PCU will accept
462 * and pass to the driver. For a list of frame types
463 * check out reg.h.
464 */
465u32 ath5k_hw_get_rx_filter(struct ath5k_hw *ah)
466{
467 u32 data, filter = 0;
468
469 ATH5K_TRACE(ah->ah_sc);
470 filter = ath5k_hw_reg_read(ah, AR5K_RX_FILTER);
471
472 /*Radar detection for 5212*/
473 if (ah->ah_version == AR5K_AR5212) {
474 data = ath5k_hw_reg_read(ah, AR5K_PHY_ERR_FIL);
475
476 if (data & AR5K_PHY_ERR_FIL_RADAR)
477 filter |= AR5K_RX_FILTER_RADARERR;
478 if (data & (AR5K_PHY_ERR_FIL_OFDM | AR5K_PHY_ERR_FIL_CCK))
479 filter |= AR5K_RX_FILTER_PHYERR;
480 }
481
482 return filter;
483}
484
485/**
486 * ath5k_hw_set_rx_filter - Set rx filter
487 *
488 * @ah: The &struct ath5k_hw
489 * @filter: RX filter mask (see reg.h)
490 *
491 * Sets RX filter register and also handles PHY error filter
492 * register on 5212 and newer chips so that we have proper PHY
493 * error reporting.
494 */
495void ath5k_hw_set_rx_filter(struct ath5k_hw *ah, u32 filter)
496{
497 u32 data = 0;
498
499 ATH5K_TRACE(ah->ah_sc);
500
501 /* Set PHY error filter register on 5212*/
502 if (ah->ah_version == AR5K_AR5212) {
503 if (filter & AR5K_RX_FILTER_RADARERR)
504 data |= AR5K_PHY_ERR_FIL_RADAR;
505 if (filter & AR5K_RX_FILTER_PHYERR)
506 data |= AR5K_PHY_ERR_FIL_OFDM | AR5K_PHY_ERR_FIL_CCK;
507 }
508
509 /*
510 * The AR5210 uses promiscous mode to detect radar activity
511 */
512 if (ah->ah_version == AR5K_AR5210 &&
513 (filter & AR5K_RX_FILTER_RADARERR)) {
514 filter &= ~AR5K_RX_FILTER_RADARERR;
515 filter |= AR5K_RX_FILTER_PROM;
516 }
517
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200518 /*Zero length DMA (phy error reporting) */
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300519 if (data)
520 AR5K_REG_ENABLE_BITS(ah, AR5K_RXCFG, AR5K_RXCFG_ZLFDMA);
521 else
522 AR5K_REG_DISABLE_BITS(ah, AR5K_RXCFG, AR5K_RXCFG_ZLFDMA);
523
524 /*Write RX Filter register*/
525 ath5k_hw_reg_write(ah, filter & 0xff, AR5K_RX_FILTER);
526
527 /*Write PHY error filter register on 5212*/
528 if (ah->ah_version == AR5K_AR5212)
529 ath5k_hw_reg_write(ah, data, AR5K_PHY_ERR_FIL);
530
531}
532
533
534/****************\
535* Beacon control *
536\****************/
537
538/**
539 * ath5k_hw_get_tsf32 - Get a 32bit TSF
540 *
541 * @ah: The &struct ath5k_hw
542 *
543 * Returns lower 32 bits of current TSF
544 */
545u32 ath5k_hw_get_tsf32(struct ath5k_hw *ah)
546{
547 ATH5K_TRACE(ah->ah_sc);
548 return ath5k_hw_reg_read(ah, AR5K_TSF_L32);
549}
550
551/**
552 * ath5k_hw_get_tsf64 - Get the full 64bit TSF
553 *
554 * @ah: The &struct ath5k_hw
555 *
556 * Returns the current TSF
557 */
558u64 ath5k_hw_get_tsf64(struct ath5k_hw *ah)
559{
560 u64 tsf = ath5k_hw_reg_read(ah, AR5K_TSF_U32);
561 ATH5K_TRACE(ah->ah_sc);
562
563 return ath5k_hw_reg_read(ah, AR5K_TSF_L32) | (tsf << 32);
564}
565
566/**
Alina Friedrichsen8cab7582009-01-23 05:39:13 +0100567 * ath5k_hw_set_tsf64 - Set a new 64bit TSF
568 *
569 * @ah: The &struct ath5k_hw
570 * @tsf64: The new 64bit TSF
571 *
572 * Sets the new TSF
573 */
574void ath5k_hw_set_tsf64(struct ath5k_hw *ah, u64 tsf64)
575{
576 ATH5K_TRACE(ah->ah_sc);
577
Alina Friedrichsen8cab7582009-01-23 05:39:13 +0100578 ath5k_hw_reg_write(ah, tsf64 & 0xffffffff, AR5K_TSF_L32);
Alina Friedrichsen0ad65bd2009-03-02 23:29:48 +0100579 ath5k_hw_reg_write(ah, (tsf64 >> 32) & 0xffffffff, AR5K_TSF_U32);
Alina Friedrichsen8cab7582009-01-23 05:39:13 +0100580}
581
582/**
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300583 * ath5k_hw_reset_tsf - Force a TSF reset
584 *
585 * @ah: The &struct ath5k_hw
586 *
587 * Forces a TSF reset on PCU
588 */
589void ath5k_hw_reset_tsf(struct ath5k_hw *ah)
590{
Bob Copeland14be9942008-09-28 12:09:43 -0400591 u32 val;
592
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300593 ATH5K_TRACE(ah->ah_sc);
Bob Copeland14be9942008-09-28 12:09:43 -0400594
595 val = ath5k_hw_reg_read(ah, AR5K_BEACON) | AR5K_BEACON_RESET_TSF;
596
597 /*
598 * Each write to the RESET_TSF bit toggles a hardware internal
599 * signal to reset TSF, but if left high it will cause a TSF reset
600 * on the next chip reset as well. Thus we always write the value
601 * twice to clear the signal.
602 */
603 ath5k_hw_reg_write(ah, val, AR5K_BEACON);
604 ath5k_hw_reg_write(ah, val, AR5K_BEACON);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300605}
606
607/*
608 * Initialize beacon timers
609 */
610void ath5k_hw_init_beacon(struct ath5k_hw *ah, u32 next_beacon, u32 interval)
611{
612 u32 timer1, timer2, timer3;
613
614 ATH5K_TRACE(ah->ah_sc);
615 /*
616 * Set the additional timers by mode
617 */
618 switch (ah->ah_op_mode) {
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200619 case NL80211_IFTYPE_MONITOR:
Johannes Berg05c914f2008-09-11 00:01:58 +0200620 case NL80211_IFTYPE_STATION:
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200621 /* In STA mode timer1 is used as next wakeup
622 * timer and timer2 as next CFP duration start
623 * timer. Both in 1/8TUs. */
624 /* TODO: PCF handling */
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300625 if (ah->ah_version == AR5K_AR5210) {
626 timer1 = 0xffffffff;
627 timer2 = 0xffffffff;
628 } else {
629 timer1 = 0x0000ffff;
630 timer2 = 0x0007ffff;
631 }
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200632 /* Mark associated AP as PCF incapable for now */
633 AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1, AR5K_STA_ID1_PCF);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300634 break;
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200635 case NL80211_IFTYPE_ADHOC:
636 AR5K_REG_ENABLE_BITS(ah, AR5K_TXCFG, AR5K_TXCFG_ADHOC_BCN_ATIM);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300637 default:
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200638 /* On non-STA modes timer1 is used as next DMA
639 * beacon alert (DBA) timer and timer2 as next
640 * software beacon alert. Both in 1/8TUs. */
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300641 timer1 = (next_beacon - AR5K_TUNE_DMA_BEACON_RESP) << 3;
642 timer2 = (next_beacon - AR5K_TUNE_SW_BEACON_RESP) << 3;
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200643 break;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300644 }
645
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200646 /* Timer3 marks the end of our ATIM window
647 * a zero length window is not allowed because
648 * we 'll get no beacons */
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300649 timer3 = next_beacon + (ah->ah_atim_window ? ah->ah_atim_window : 1);
650
651 /*
652 * Set the beacon register and enable all timers.
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300653 */
Nick Kossifidis35edf8a2009-06-12 16:09:53 -0700654 /* When in AP or Mesh Point mode zero timer0 to start TSF */
655 if (ah->ah_op_mode == NL80211_IFTYPE_AP ||
656 ah->ah_op_mode == NL80211_IFTYPE_MESH_POINT)
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200657 ath5k_hw_reg_write(ah, 0, AR5K_TIMER0);
Nick Kossifidis428cbd42009-04-30 15:55:47 -0400658
659 ath5k_hw_reg_write(ah, next_beacon, AR5K_TIMER0);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300660 ath5k_hw_reg_write(ah, timer1, AR5K_TIMER1);
661 ath5k_hw_reg_write(ah, timer2, AR5K_TIMER2);
662 ath5k_hw_reg_write(ah, timer3, AR5K_TIMER3);
663
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200664 /* Force a TSF reset if requested and enable beacons */
665 if (interval & AR5K_BEACON_RESET_TSF)
666 ath5k_hw_reset_tsf(ah);
667
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300668 ath5k_hw_reg_write(ah, interval & (AR5K_BEACON_PERIOD |
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200669 AR5K_BEACON_ENABLE),
670 AR5K_BEACON);
671
672 /* Flush any pending BMISS interrupts on ISR by
673 * performing a clear-on-write operation on PISR
674 * register for the BMISS bit (writing a bit on
675 * ISR togles a reset for that bit and leaves
676 * the rest bits intact) */
677 if (ah->ah_version == AR5K_AR5210)
678 ath5k_hw_reg_write(ah, AR5K_ISR_BMISS, AR5K_ISR);
679 else
680 ath5k_hw_reg_write(ah, AR5K_ISR_BMISS, AR5K_PISR);
681
682 /* TODO: Set enchanced sleep registers on AR5212
683 * based on vif->bss_conf params, until then
684 * disable power save reporting.*/
685 AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1, AR5K_STA_ID1_PWR_SV);
686
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300687}
688
689#if 0
690/*
691 * Set beacon timers
692 */
693int ath5k_hw_set_beacon_timers(struct ath5k_hw *ah,
694 const struct ath5k_beacon_state *state)
695{
696 u32 cfp_period, next_cfp, dtim, interval, next_beacon;
697
698 /*
699 * TODO: should be changed through *state
700 * review struct ath5k_beacon_state struct
701 *
702 * XXX: These are used for cfp period bellow, are they
703 * ok ? Is it O.K. for tsf here to be 0 or should we use
704 * get_tsf ?
705 */
706 u32 dtim_count = 0; /* XXX */
707 u32 cfp_count = 0; /* XXX */
708 u32 tsf = 0; /* XXX */
709
710 ATH5K_TRACE(ah->ah_sc);
711 /* Return on an invalid beacon state */
712 if (state->bs_interval < 1)
713 return -EINVAL;
714
715 interval = state->bs_interval;
716 dtim = state->bs_dtim_period;
717
718 /*
719 * PCF support?
720 */
721 if (state->bs_cfp_period > 0) {
722 /*
723 * Enable PCF mode and set the CFP
724 * (Contention Free Period) and timer registers
725 */
726 cfp_period = state->bs_cfp_period * state->bs_dtim_period *
727 state->bs_interval;
728 next_cfp = (cfp_count * state->bs_dtim_period + dtim_count) *
729 state->bs_interval;
730
731 AR5K_REG_ENABLE_BITS(ah, AR5K_STA_ID1,
732 AR5K_STA_ID1_DEFAULT_ANTENNA |
733 AR5K_STA_ID1_PCF);
734 ath5k_hw_reg_write(ah, cfp_period, AR5K_CFP_PERIOD);
735 ath5k_hw_reg_write(ah, state->bs_cfp_max_duration,
736 AR5K_CFP_DUR);
737 ath5k_hw_reg_write(ah, (tsf + (next_cfp == 0 ? cfp_period :
738 next_cfp)) << 3, AR5K_TIMER2);
739 } else {
740 /* Disable PCF mode */
741 AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1,
742 AR5K_STA_ID1_DEFAULT_ANTENNA |
743 AR5K_STA_ID1_PCF);
744 }
745
746 /*
747 * Enable the beacon timer register
748 */
749 ath5k_hw_reg_write(ah, state->bs_next_beacon, AR5K_TIMER0);
750
751 /*
752 * Start the beacon timers
753 */
754 ath5k_hw_reg_write(ah, (ath5k_hw_reg_read(ah, AR5K_BEACON) &
755 ~(AR5K_BEACON_PERIOD | AR5K_BEACON_TIM)) |
756 AR5K_REG_SM(state->bs_tim_offset ? state->bs_tim_offset + 4 : 0,
757 AR5K_BEACON_TIM) | AR5K_REG_SM(state->bs_interval,
758 AR5K_BEACON_PERIOD), AR5K_BEACON);
759
760 /*
761 * Write new beacon miss threshold, if it appears to be valid
762 * XXX: Figure out right values for min <= bs_bmiss_threshold <= max
763 * and return if its not in range. We can test this by reading value and
764 * setting value to a largest value and seeing which values register.
765 */
766
767 AR5K_REG_WRITE_BITS(ah, AR5K_RSSI_THR, AR5K_RSSI_THR_BMISS,
768 state->bs_bmiss_threshold);
769
770 /*
771 * Set sleep control register
772 * XXX: Didn't find this in 5210 code but since this register
773 * exists also in ar5k's 5210 headers i leave it as common code.
774 */
775 AR5K_REG_WRITE_BITS(ah, AR5K_SLEEP_CTL, AR5K_SLEEP_CTL_SLDUR,
776 (state->bs_sleep_duration - 3) << 3);
777
778 /*
779 * Set enhanced sleep registers on 5212
780 */
781 if (ah->ah_version == AR5K_AR5212) {
782 if (state->bs_sleep_duration > state->bs_interval &&
783 roundup(state->bs_sleep_duration, interval) ==
784 state->bs_sleep_duration)
785 interval = state->bs_sleep_duration;
786
787 if (state->bs_sleep_duration > dtim && (dtim == 0 ||
788 roundup(state->bs_sleep_duration, dtim) ==
789 state->bs_sleep_duration))
790 dtim = state->bs_sleep_duration;
791
792 if (interval > dtim)
793 return -EINVAL;
794
795 next_beacon = interval == dtim ? state->bs_next_dtim :
796 state->bs_next_beacon;
797
798 ath5k_hw_reg_write(ah,
799 AR5K_REG_SM((state->bs_next_dtim - 3) << 3,
800 AR5K_SLEEP0_NEXT_DTIM) |
801 AR5K_REG_SM(10, AR5K_SLEEP0_CABTO) |
802 AR5K_SLEEP0_ENH_SLEEP_EN |
803 AR5K_SLEEP0_ASSUME_DTIM, AR5K_SLEEP0);
804
805 ath5k_hw_reg_write(ah, AR5K_REG_SM((next_beacon - 3) << 3,
806 AR5K_SLEEP1_NEXT_TIM) |
807 AR5K_REG_SM(10, AR5K_SLEEP1_BEACON_TO), AR5K_SLEEP1);
808
809 ath5k_hw_reg_write(ah,
810 AR5K_REG_SM(interval, AR5K_SLEEP2_TIM_PER) |
811 AR5K_REG_SM(dtim, AR5K_SLEEP2_DTIM_PER), AR5K_SLEEP2);
812 }
813
814 return 0;
815}
816
817/*
818 * Reset beacon timers
819 */
820void ath5k_hw_reset_beacon(struct ath5k_hw *ah)
821{
822 ATH5K_TRACE(ah->ah_sc);
823 /*
824 * Disable beacon timer
825 */
826 ath5k_hw_reg_write(ah, 0, AR5K_TIMER0);
827
828 /*
829 * Disable some beacon register values
830 */
831 AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1,
832 AR5K_STA_ID1_DEFAULT_ANTENNA | AR5K_STA_ID1_PCF);
833 ath5k_hw_reg_write(ah, AR5K_BEACON_PERIOD, AR5K_BEACON);
834}
835
836/*
837 * Wait for beacon queue to finish
838 */
839int ath5k_hw_beaconq_finish(struct ath5k_hw *ah, unsigned long phys_addr)
840{
841 unsigned int i;
842 int ret;
843
844 ATH5K_TRACE(ah->ah_sc);
845
846 /* 5210 doesn't have QCU*/
847 if (ah->ah_version == AR5K_AR5210) {
848 /*
849 * Wait for beaconn queue to finish by checking
850 * Control Register and Beacon Status Register.
851 */
852 for (i = AR5K_TUNE_BEACON_INTERVAL / 2; i > 0; i--) {
853 if (!(ath5k_hw_reg_read(ah, AR5K_BSR) & AR5K_BSR_TXQ1F)
854 ||
855 !(ath5k_hw_reg_read(ah, AR5K_CR) & AR5K_BSR_TXQ1F))
856 break;
857 udelay(10);
858 }
859
860 /* Timeout... */
861 if (i <= 0) {
862 /*
863 * Re-schedule the beacon queue
864 */
865 ath5k_hw_reg_write(ah, phys_addr, AR5K_NOQCU_TXDP1);
866 ath5k_hw_reg_write(ah, AR5K_BCR_TQ1V | AR5K_BCR_BDMAE,
867 AR5K_BCR);
868
869 return -EIO;
870 }
871 ret = 0;
872 } else {
873 /*5211/5212*/
874 ret = ath5k_hw_register_timeout(ah,
875 AR5K_QUEUE_STATUS(AR5K_TX_QUEUE_ID_BEACON),
876 AR5K_QCU_STS_FRMPENDCNT, 0, false);
877
878 if (AR5K_REG_READ_Q(ah, AR5K_QCU_TXE, AR5K_TX_QUEUE_ID_BEACON))
879 return -EIO;
880 }
881
882 return ret;
883}
884#endif
885
886
887/*********************\
888* Key table functions *
889\*********************/
890
891/*
892 * Reset a key entry on the table
893 */
894int ath5k_hw_reset_key(struct ath5k_hw *ah, u16 entry)
895{
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200896 unsigned int i, type;
Bob Copeland17683c62008-10-29 23:24:26 -0400897 u16 micentry = entry + AR5K_KEYTABLE_MIC_OFFSET;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300898
899 ATH5K_TRACE(ah->ah_sc);
900 AR5K_ASSERT_ENTRY(entry, AR5K_KEYTABLE_SIZE);
901
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200902 type = ath5k_hw_reg_read(ah, AR5K_KEYTABLE_TYPE(entry));
903
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300904 for (i = 0; i < AR5K_KEYCACHE_SIZE; i++)
905 ath5k_hw_reg_write(ah, 0, AR5K_KEYTABLE_OFF(entry, i));
906
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200907 /* Reset associated MIC entry if TKIP
908 * is enabled located at offset (entry + 64) */
909 if (type == AR5K_KEYTABLE_TYPE_TKIP) {
Bob Copeland17683c62008-10-29 23:24:26 -0400910 AR5K_ASSERT_ENTRY(micentry, AR5K_KEYTABLE_SIZE);
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200911 for (i = 0; i < AR5K_KEYCACHE_SIZE / 2 ; i++)
Bob Copeland17683c62008-10-29 23:24:26 -0400912 ath5k_hw_reg_write(ah, 0,
913 AR5K_KEYTABLE_OFF(micentry, i));
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200914 }
915
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300916 /*
917 * Set NULL encryption on AR5212+
918 *
919 * Note: AR5K_KEYTABLE_TYPE -> AR5K_KEYTABLE_OFF(entry, 5)
920 * AR5K_KEYTABLE_TYPE_NULL -> 0x00000007
921 *
922 * Note2: Windows driver (ndiswrapper) sets this to
923 * 0x00000714 instead of 0x00000007
924 */
Jiri Slabyded7a7e2009-04-25 14:09:23 +0200925 if (ah->ah_version >= AR5K_AR5211) {
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300926 ath5k_hw_reg_write(ah, AR5K_KEYTABLE_TYPE_NULL,
927 AR5K_KEYTABLE_TYPE(entry));
928
Bob Copeland17683c62008-10-29 23:24:26 -0400929 if (type == AR5K_KEYTABLE_TYPE_TKIP) {
930 ath5k_hw_reg_write(ah, AR5K_KEYTABLE_TYPE_NULL,
931 AR5K_KEYTABLE_TYPE(micentry));
932 }
933 }
934
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300935 return 0;
936}
937
938/*
939 * Check if a table entry is valid
940 */
941int ath5k_hw_is_key_valid(struct ath5k_hw *ah, u16 entry)
942{
943 ATH5K_TRACE(ah->ah_sc);
944 AR5K_ASSERT_ENTRY(entry, AR5K_KEYTABLE_SIZE);
945
946 /* Check the validation flag at the end of the entry */
947 return ath5k_hw_reg_read(ah, AR5K_KEYTABLE_MAC1(entry)) &
948 AR5K_KEYTABLE_VALID;
949}
950
Bob Copeland67143492008-11-25 20:55:21 -0500951static
952int ath5k_keycache_type(const struct ieee80211_key_conf *key)
953{
954 switch (key->alg) {
955 case ALG_TKIP:
956 return AR5K_KEYTABLE_TYPE_TKIP;
957 case ALG_CCMP:
958 return AR5K_KEYTABLE_TYPE_CCM;
959 case ALG_WEP:
Zhu Yie31a16d2009-05-21 21:47:03 +0800960 if (key->keylen == WLAN_KEY_LEN_WEP40)
Bob Copeland67143492008-11-25 20:55:21 -0500961 return AR5K_KEYTABLE_TYPE_40;
Zhu Yie31a16d2009-05-21 21:47:03 +0800962 else if (key->keylen == WLAN_KEY_LEN_WEP104)
Bob Copeland67143492008-11-25 20:55:21 -0500963 return AR5K_KEYTABLE_TYPE_104;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +0200964 return -EINVAL;
965 default:
966 return -EINVAL;
Bob Copeland67143492008-11-25 20:55:21 -0500967 }
968 return -EINVAL;
969}
970
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300971/*
972 * Set a key entry on the table
973 */
974int ath5k_hw_set_key(struct ath5k_hw *ah, u16 entry,
975 const struct ieee80211_key_conf *key, const u8 *mac)
976{
977 unsigned int i;
Bob Copeland3f64b432008-10-29 23:19:14 -0400978 int keylen;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300979 __le32 key_v[5] = {};
Bob Copeland3f64b432008-10-29 23:19:14 -0400980 __le32 key0 = 0, key1 = 0;
981 __le32 *rxmic, *txmic;
Roel Kluin672cf3c2009-01-18 23:50:27 +0100982 int keytype;
Bob Copeland3f64b432008-10-29 23:19:14 -0400983 u16 micentry = entry + AR5K_KEYTABLE_MIC_OFFSET;
984 bool is_tkip;
Bob Copeland67143492008-11-25 20:55:21 -0500985 const u8 *key_ptr;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300986
987 ATH5K_TRACE(ah->ah_sc);
988
Bob Copeland3f64b432008-10-29 23:19:14 -0400989 is_tkip = (key->alg == ALG_TKIP);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300990
Bob Copeland3f64b432008-10-29 23:19:14 -0400991 /*
992 * key->keylen comes in from mac80211 in bytes.
993 * TKIP is 128 bit + 128 bit mic
994 */
995 keylen = (is_tkip) ? (128 / 8) : key->keylen;
996
997 if (entry > AR5K_KEYTABLE_SIZE ||
998 (is_tkip && micentry > AR5K_KEYTABLE_SIZE))
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300999 return -EOPNOTSUPP;
1000
Bob Copeland67143492008-11-25 20:55:21 -05001001 if (unlikely(keylen > 16))
1002 return -EOPNOTSUPP;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001003
Bob Copeland67143492008-11-25 20:55:21 -05001004 keytype = ath5k_keycache_type(key);
1005 if (keytype < 0)
1006 return keytype;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001007
Bob Copeland67143492008-11-25 20:55:21 -05001008 /*
1009 * each key block is 6 bytes wide, written as pairs of
1010 * alternating 32 and 16 bit le values.
1011 */
1012 key_ptr = key->key;
1013 for (i = 0; keylen >= 6; keylen -= 6) {
1014 memcpy(&key_v[i], key_ptr, 6);
1015 i += 2;
1016 key_ptr += 6;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001017 }
Bob Copeland67143492008-11-25 20:55:21 -05001018 if (keylen)
1019 memcpy(&key_v[i], key_ptr, keylen);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001020
Bob Copeland3f64b432008-10-29 23:19:14 -04001021 /* intentionally corrupt key until mic is installed */
1022 if (is_tkip) {
1023 key0 = key_v[0] = ~key_v[0];
1024 key1 = key_v[1] = ~key_v[1];
1025 }
1026
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001027 for (i = 0; i < ARRAY_SIZE(key_v); i++)
1028 ath5k_hw_reg_write(ah, le32_to_cpu(key_v[i]),
1029 AR5K_KEYTABLE_OFF(entry, i));
1030
1031 ath5k_hw_reg_write(ah, keytype, AR5K_KEYTABLE_TYPE(entry));
1032
Bob Copeland3f64b432008-10-29 23:19:14 -04001033 if (is_tkip) {
1034 /* Install rx/tx MIC */
1035 rxmic = (__le32 *) &key->key[16];
1036 txmic = (__le32 *) &key->key[24];
Bob Copelandf6504702008-11-26 16:17:25 -05001037
1038 if (ah->ah_combined_mic) {
1039 key_v[0] = rxmic[0];
Bob Copeland388cdf32008-12-09 23:05:38 -05001040 key_v[1] = cpu_to_le32(le32_to_cpu(txmic[0]) >> 16);
Bob Copelandf6504702008-11-26 16:17:25 -05001041 key_v[2] = rxmic[1];
Bob Copeland388cdf32008-12-09 23:05:38 -05001042 key_v[3] = cpu_to_le32(le32_to_cpu(txmic[0]) & 0xffff);
Bob Copelandf6504702008-11-26 16:17:25 -05001043 key_v[4] = txmic[1];
1044 } else {
1045 key_v[0] = rxmic[0];
1046 key_v[1] = 0;
1047 key_v[2] = rxmic[1];
1048 key_v[3] = 0;
1049 key_v[4] = 0;
1050 }
Bob Copeland3f64b432008-10-29 23:19:14 -04001051 for (i = 0; i < ARRAY_SIZE(key_v); i++)
1052 ath5k_hw_reg_write(ah, le32_to_cpu(key_v[i]),
1053 AR5K_KEYTABLE_OFF(micentry, i));
1054
1055 ath5k_hw_reg_write(ah, AR5K_KEYTABLE_TYPE_NULL,
1056 AR5K_KEYTABLE_TYPE(micentry));
1057 ath5k_hw_reg_write(ah, 0, AR5K_KEYTABLE_MAC0(micentry));
1058 ath5k_hw_reg_write(ah, 0, AR5K_KEYTABLE_MAC1(micentry));
1059
1060 /* restore first 2 words of key */
1061 ath5k_hw_reg_write(ah, le32_to_cpu(~key0),
1062 AR5K_KEYTABLE_OFF(entry, 0));
1063 ath5k_hw_reg_write(ah, le32_to_cpu(~key1),
1064 AR5K_KEYTABLE_OFF(entry, 1));
1065 }
1066
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001067 return ath5k_hw_set_key_lladdr(ah, entry, mac);
1068}
1069
1070int ath5k_hw_set_key_lladdr(struct ath5k_hw *ah, u16 entry, const u8 *mac)
1071{
1072 u32 low_id, high_id;
1073
1074 ATH5K_TRACE(ah->ah_sc);
1075 /* Invalid entry (key table overflow) */
1076 AR5K_ASSERT_ENTRY(entry, AR5K_KEYTABLE_SIZE);
1077
Luis R. Rodriguezbcd8f542009-09-09 22:43:17 -07001078 /*
1079 * MAC may be NULL if it's a broadcast key. In this case no need to
1080 * to compute get_unaligned_le32 and get_unaligned_le16 as we
1081 * already know it.
1082 */
Johannes Bergdc822b52008-12-29 12:55:09 +01001083 if (!mac) {
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001084 low_id = 0xffffffff;
1085 high_id = 0xffff | AR5K_KEYTABLE_VALID;
1086 } else {
Luis R. Rodriguezbcd8f542009-09-09 22:43:17 -07001087 low_id = get_unaligned_le32(mac);
1088 high_id = get_unaligned_le16(mac + 4) | AR5K_KEYTABLE_VALID;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001089 }
1090
1091 ath5k_hw_reg_write(ah, low_id, AR5K_KEYTABLE_MAC0(entry));
1092 ath5k_hw_reg_write(ah, high_id, AR5K_KEYTABLE_MAC1(entry));
1093
1094 return 0;
1095}
1096