blob: f03c06d583e6232355abdbee79285e121e8b99d9 [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
190 return ath5k_hw_clocktoh(AR5K_REG_MS(ath5k_hw_reg_read(ah,
191 AR5K_TIME_OUT), AR5K_TIME_OUT_ACK), ah->ah_turbo);
192}
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);
203 if (ath5k_hw_clocktoh(AR5K_REG_MS(0xffffffff, AR5K_TIME_OUT_ACK),
204 ah->ah_turbo) <= timeout)
205 return -EINVAL;
206
207 AR5K_REG_WRITE_BITS(ah, AR5K_TIME_OUT, AR5K_TIME_OUT_ACK,
208 ath5k_hw_htoclock(timeout, ah->ah_turbo));
209
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);
221 return ath5k_hw_clocktoh(AR5K_REG_MS(ath5k_hw_reg_read(ah,
222 AR5K_TIME_OUT), AR5K_TIME_OUT_CTS), ah->ah_turbo);
223}
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);
234 if (ath5k_hw_clocktoh(AR5K_REG_MS(0xffffffff, AR5K_TIME_OUT_CTS),
235 ah->ah_turbo) <= timeout)
236 return -EINVAL;
237
238 AR5K_REG_WRITE_BITS(ah, AR5K_TIME_OUT, AR5K_TIME_OUT_CTS,
239 ath5k_hw_htoclock(timeout, ah->ah_turbo));
240
241 return 0;
242}
243
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300244/**
245 * ath5k_hw_set_lladdr - Set station id
246 *
247 * @ah: The &struct ath5k_hw
248 * @mac: The card's mac address
249 *
250 * Set station id on hw using the provided mac address
251 */
252int ath5k_hw_set_lladdr(struct ath5k_hw *ah, const u8 *mac)
253{
Luis R. Rodriguez954fece2009-09-10 10:51:33 -0700254 struct ath_common *common = ath5k_hw_common(ah);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300255 u32 low_id, high_id;
Bob Copelandf6bac3e2008-11-26 16:17:11 -0500256 u32 pcu_reg;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300257
258 ATH5K_TRACE(ah->ah_sc);
259 /* Set new station ID */
Luis R. Rodriguez954fece2009-09-10 10:51:33 -0700260 memcpy(common->macaddr, mac, ETH_ALEN);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300261
Bob Copelandf6bac3e2008-11-26 16:17:11 -0500262 pcu_reg = ath5k_hw_reg_read(ah, AR5K_STA_ID1) & 0xffff0000;
263
Luis R. Rodriguezbcd8f542009-09-09 22:43:17 -0700264 low_id = get_unaligned_le32(mac);
265 high_id = get_unaligned_le16(mac + 4);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300266
267 ath5k_hw_reg_write(ah, low_id, AR5K_STA_ID0);
Bob Copelandf6bac3e2008-11-26 16:17:11 -0500268 ath5k_hw_reg_write(ah, pcu_reg | high_id, AR5K_STA_ID1);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300269
270 return 0;
271}
272
273/**
274 * ath5k_hw_set_associd - Set BSSID for association
275 *
276 * @ah: The &struct ath5k_hw
277 * @bssid: BSSID
278 * @assoc_id: Assoc id
279 *
280 * Sets the BSSID which trigers the "SME Join" operation
281 */
282void ath5k_hw_set_associd(struct ath5k_hw *ah, const u8 *bssid, u16 assoc_id)
283{
Luis R. Rodriguez954fece2009-09-10 10:51:33 -0700284 struct ath_common *common = ath5k_hw_common(ah);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300285 u32 low_id, high_id;
286 u16 tim_offset = 0;
287
288 /*
289 * Set simple BSSID mask on 5212
290 */
291 if (ah->ah_version == AR5K_AR5212) {
Luis R. Rodriguez954fece2009-09-10 10:51:33 -0700292 ath5k_hw_reg_write(ah, get_unaligned_le32(common->bssidmask),
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200293 AR5K_BSS_IDM0);
Luis R. Rodriguezbcd8f542009-09-09 22:43:17 -0700294 ath5k_hw_reg_write(ah,
Luis R. Rodriguez954fece2009-09-10 10:51:33 -0700295 get_unaligned_le16(common->curbssid + 4),
Luis R. Rodriguezbcd8f542009-09-09 22:43:17 -0700296 AR5K_BSS_IDM1);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300297 }
298
299 /*
300 * Set BSSID which triggers the "SME Join" operation
301 */
Luis R. Rodriguezbcd8f542009-09-09 22:43:17 -0700302 low_id = get_unaligned_le32(bssid);
303 high_id = get_unaligned_le16(bssid);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300304 ath5k_hw_reg_write(ah, low_id, AR5K_BSS_ID0);
305 ath5k_hw_reg_write(ah, high_id | ((assoc_id & 0x3fff) <<
306 AR5K_BSS_ID1_AID_S), AR5K_BSS_ID1);
307
308 if (assoc_id == 0) {
309 ath5k_hw_disable_pspoll(ah);
310 return;
311 }
312
313 AR5K_REG_WRITE_BITS(ah, AR5K_BEACON, AR5K_BEACON_TIM,
314 tim_offset ? tim_offset + 4 : 0);
315
316 ath5k_hw_enable_pspoll(ah, NULL, 0);
317}
318
319/**
320 * ath5k_hw_set_bssid_mask - filter out bssids we listen
321 *
322 * @ah: the &struct ath5k_hw
323 * @mask: the bssid_mask, a u8 array of size ETH_ALEN
324 *
325 * BSSID masking is a method used by AR5212 and newer hardware to inform PCU
326 * which bits of the interface's MAC address should be looked at when trying
327 * to decide which packets to ACK. In station mode and AP mode with a single
328 * BSS every bit matters since we lock to only one BSS. In AP mode with
329 * multiple BSSes (virtual interfaces) not every bit matters because hw must
330 * accept frames for all BSSes and so we tweak some bits of our mac address
331 * in order to have multiple BSSes.
332 *
333 * NOTE: This is a simple filter and does *not* filter out all
334 * relevant frames. Some frames that are not for us might get ACKed from us
335 * by PCU because they just match the mask.
336 *
337 * When handling multiple BSSes you can get the BSSID mask by computing the
338 * set of ~ ( MAC XOR BSSID ) for all bssids we handle.
339 *
340 * When you do this you are essentially computing the common bits of all your
341 * BSSes. Later it is assumed the harware will "and" (&) the BSSID mask with
342 * the MAC address to obtain the relevant bits and compare the result with
343 * (frame's BSSID & mask) to see if they match.
344 */
345/*
346 * Simple example: on your card you have have two BSSes you have created with
347 * BSSID-01 and BSSID-02. Lets assume BSSID-01 will not use the MAC address.
348 * There is another BSSID-03 but you are not part of it. For simplicity's sake,
349 * assuming only 4 bits for a mac address and for BSSIDs you can then have:
350 *
351 * \
Luis R. Rodriguez17753742009-09-09 22:19:26 -0700352 * MAC: 0001 |
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300353 * BSSID-01: 0100 | --> Belongs to us
354 * BSSID-02: 1001 |
355 * /
356 * -------------------
357 * BSSID-03: 0110 | --> External
358 * -------------------
359 *
360 * Our bssid_mask would then be:
361 *
362 * On loop iteration for BSSID-01:
363 * ~(0001 ^ 0100) -> ~(0101)
364 * -> 1010
365 * bssid_mask = 1010
366 *
367 * On loop iteration for BSSID-02:
368 * bssid_mask &= ~(0001 ^ 1001)
369 * bssid_mask = (1010) & ~(0001 ^ 1001)
370 * bssid_mask = (1010) & ~(1001)
371 * bssid_mask = (1010) & (0110)
372 * bssid_mask = 0010
373 *
374 * A bssid_mask of 0010 means "only pay attention to the second least
375 * significant bit". This is because its the only bit common
376 * amongst the MAC and all BSSIDs we support. To findout what the real
377 * common bit is we can simply "&" the bssid_mask now with any BSSID we have
378 * or our MAC address (we assume the hardware uses the MAC address).
379 *
380 * Now, suppose there's an incoming frame for BSSID-03:
381 *
382 * IFRAME-01: 0110
383 *
384 * An easy eye-inspeciton of this already should tell you that this frame
385 * will not pass our check. This is beacuse the bssid_mask tells the
386 * hardware to only look at the second least significant bit and the
387 * common bit amongst the MAC and BSSIDs is 0, this frame has the 2nd LSB
388 * as 1, which does not match 0.
389 *
390 * So with IFRAME-01 we *assume* the hardware will do:
391 *
392 * allow = (IFRAME-01 & bssid_mask) == (bssid_mask & MAC) ? 1 : 0;
393 * --> allow = (0110 & 0010) == (0010 & 0001) ? 1 : 0;
394 * --> allow = (0010) == 0000 ? 1 : 0;
395 * --> allow = 0
396 *
397 * Lets now test a frame that should work:
398 *
399 * IFRAME-02: 0001 (we should allow)
400 *
401 * allow = (0001 & 1010) == 1010
402 *
403 * allow = (IFRAME-02 & bssid_mask) == (bssid_mask & MAC) ? 1 : 0;
404 * --> allow = (0001 & 0010) == (0010 & 0001) ? 1 :0;
405 * --> allow = (0010) == (0010)
406 * --> allow = 1
407 *
408 * Other examples:
409 *
410 * IFRAME-03: 0100 --> allowed
411 * IFRAME-04: 1001 --> allowed
412 * IFRAME-05: 1101 --> allowed but its not for us!!!
413 *
414 */
415int ath5k_hw_set_bssid_mask(struct ath5k_hw *ah, const u8 *mask)
416{
Luis R. Rodriguez954fece2009-09-10 10:51:33 -0700417 struct ath_common *common = ath5k_hw_common(ah);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300418 u32 low_id, high_id;
419 ATH5K_TRACE(ah->ah_sc);
420
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200421 /* Cache bssid mask so that we can restore it
422 * on reset */
Luis R. Rodriguez954fece2009-09-10 10:51:33 -0700423 memcpy(common->bssidmask, mask, ETH_ALEN);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300424 if (ah->ah_version == AR5K_AR5212) {
Luis R. Rodriguezbcd8f542009-09-09 22:43:17 -0700425 low_id = get_unaligned_le32(mask);
426 high_id = get_unaligned_le16(mask + 4);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300427
428 ath5k_hw_reg_write(ah, low_id, AR5K_BSS_IDM0);
429 ath5k_hw_reg_write(ah, high_id, AR5K_BSS_IDM1);
430
431 return 0;
432 }
433
434 return -EIO;
435}
436
437
438/************\
439* RX Control *
440\************/
441
442/**
443 * ath5k_hw_start_rx_pcu - Start RX engine
444 *
445 * @ah: The &struct ath5k_hw
446 *
447 * Starts RX engine on PCU so that hw can process RXed frames
448 * (ACK etc).
449 *
450 * NOTE: RX DMA should be already enabled using ath5k_hw_start_rx_dma
451 * TODO: Init ANI here
452 */
453void ath5k_hw_start_rx_pcu(struct ath5k_hw *ah)
454{
455 ATH5K_TRACE(ah->ah_sc);
456 AR5K_REG_DISABLE_BITS(ah, AR5K_DIAG_SW, AR5K_DIAG_SW_DIS_RX);
457}
458
459/**
460 * at5k_hw_stop_rx_pcu - Stop RX engine
461 *
462 * @ah: The &struct ath5k_hw
463 *
464 * Stops RX engine on PCU
465 *
466 * TODO: Detach ANI here
467 */
468void ath5k_hw_stop_rx_pcu(struct ath5k_hw *ah)
469{
470 ATH5K_TRACE(ah->ah_sc);
471 AR5K_REG_ENABLE_BITS(ah, AR5K_DIAG_SW, AR5K_DIAG_SW_DIS_RX);
472}
473
474/*
475 * Set multicast filter
476 */
477void ath5k_hw_set_mcast_filter(struct ath5k_hw *ah, u32 filter0, u32 filter1)
478{
479 ATH5K_TRACE(ah->ah_sc);
480 /* Set the multicat filter */
481 ath5k_hw_reg_write(ah, filter0, AR5K_MCAST_FILTER0);
482 ath5k_hw_reg_write(ah, filter1, AR5K_MCAST_FILTER1);
483}
484
485/*
486 * Set multicast filter by index
487 */
488int ath5k_hw_set_mcast_filter_idx(struct ath5k_hw *ah, u32 index)
489{
490
491 ATH5K_TRACE(ah->ah_sc);
492 if (index >= 64)
493 return -EINVAL;
494 else if (index >= 32)
495 AR5K_REG_ENABLE_BITS(ah, AR5K_MCAST_FILTER1,
496 (1 << (index - 32)));
497 else
498 AR5K_REG_ENABLE_BITS(ah, AR5K_MCAST_FILTER0, (1 << index));
499
500 return 0;
501}
502
503/*
504 * Clear Multicast filter by index
505 */
506int ath5k_hw_clear_mcast_filter_idx(struct ath5k_hw *ah, u32 index)
507{
508
509 ATH5K_TRACE(ah->ah_sc);
510 if (index >= 64)
511 return -EINVAL;
512 else if (index >= 32)
513 AR5K_REG_DISABLE_BITS(ah, AR5K_MCAST_FILTER1,
514 (1 << (index - 32)));
515 else
516 AR5K_REG_DISABLE_BITS(ah, AR5K_MCAST_FILTER0, (1 << index));
517
518 return 0;
519}
520
521/**
522 * ath5k_hw_get_rx_filter - Get current rx filter
523 *
524 * @ah: The &struct ath5k_hw
525 *
526 * Returns the RX filter by reading rx filter and
527 * phy error filter registers. RX filter is used
528 * to set the allowed frame types that PCU will accept
529 * and pass to the driver. For a list of frame types
530 * check out reg.h.
531 */
532u32 ath5k_hw_get_rx_filter(struct ath5k_hw *ah)
533{
534 u32 data, filter = 0;
535
536 ATH5K_TRACE(ah->ah_sc);
537 filter = ath5k_hw_reg_read(ah, AR5K_RX_FILTER);
538
539 /*Radar detection for 5212*/
540 if (ah->ah_version == AR5K_AR5212) {
541 data = ath5k_hw_reg_read(ah, AR5K_PHY_ERR_FIL);
542
543 if (data & AR5K_PHY_ERR_FIL_RADAR)
544 filter |= AR5K_RX_FILTER_RADARERR;
545 if (data & (AR5K_PHY_ERR_FIL_OFDM | AR5K_PHY_ERR_FIL_CCK))
546 filter |= AR5K_RX_FILTER_PHYERR;
547 }
548
549 return filter;
550}
551
552/**
553 * ath5k_hw_set_rx_filter - Set rx filter
554 *
555 * @ah: The &struct ath5k_hw
556 * @filter: RX filter mask (see reg.h)
557 *
558 * Sets RX filter register and also handles PHY error filter
559 * register on 5212 and newer chips so that we have proper PHY
560 * error reporting.
561 */
562void ath5k_hw_set_rx_filter(struct ath5k_hw *ah, u32 filter)
563{
564 u32 data = 0;
565
566 ATH5K_TRACE(ah->ah_sc);
567
568 /* Set PHY error filter register on 5212*/
569 if (ah->ah_version == AR5K_AR5212) {
570 if (filter & AR5K_RX_FILTER_RADARERR)
571 data |= AR5K_PHY_ERR_FIL_RADAR;
572 if (filter & AR5K_RX_FILTER_PHYERR)
573 data |= AR5K_PHY_ERR_FIL_OFDM | AR5K_PHY_ERR_FIL_CCK;
574 }
575
576 /*
577 * The AR5210 uses promiscous mode to detect radar activity
578 */
579 if (ah->ah_version == AR5K_AR5210 &&
580 (filter & AR5K_RX_FILTER_RADARERR)) {
581 filter &= ~AR5K_RX_FILTER_RADARERR;
582 filter |= AR5K_RX_FILTER_PROM;
583 }
584
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200585 /*Zero length DMA (phy error reporting) */
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300586 if (data)
587 AR5K_REG_ENABLE_BITS(ah, AR5K_RXCFG, AR5K_RXCFG_ZLFDMA);
588 else
589 AR5K_REG_DISABLE_BITS(ah, AR5K_RXCFG, AR5K_RXCFG_ZLFDMA);
590
591 /*Write RX Filter register*/
592 ath5k_hw_reg_write(ah, filter & 0xff, AR5K_RX_FILTER);
593
594 /*Write PHY error filter register on 5212*/
595 if (ah->ah_version == AR5K_AR5212)
596 ath5k_hw_reg_write(ah, data, AR5K_PHY_ERR_FIL);
597
598}
599
600
601/****************\
602* Beacon control *
603\****************/
604
605/**
606 * ath5k_hw_get_tsf32 - Get a 32bit TSF
607 *
608 * @ah: The &struct ath5k_hw
609 *
610 * Returns lower 32 bits of current TSF
611 */
612u32 ath5k_hw_get_tsf32(struct ath5k_hw *ah)
613{
614 ATH5K_TRACE(ah->ah_sc);
615 return ath5k_hw_reg_read(ah, AR5K_TSF_L32);
616}
617
618/**
619 * ath5k_hw_get_tsf64 - Get the full 64bit TSF
620 *
621 * @ah: The &struct ath5k_hw
622 *
623 * Returns the current TSF
624 */
625u64 ath5k_hw_get_tsf64(struct ath5k_hw *ah)
626{
627 u64 tsf = ath5k_hw_reg_read(ah, AR5K_TSF_U32);
628 ATH5K_TRACE(ah->ah_sc);
629
630 return ath5k_hw_reg_read(ah, AR5K_TSF_L32) | (tsf << 32);
631}
632
633/**
Alina Friedrichsen8cab7582009-01-23 05:39:13 +0100634 * ath5k_hw_set_tsf64 - Set a new 64bit TSF
635 *
636 * @ah: The &struct ath5k_hw
637 * @tsf64: The new 64bit TSF
638 *
639 * Sets the new TSF
640 */
641void ath5k_hw_set_tsf64(struct ath5k_hw *ah, u64 tsf64)
642{
643 ATH5K_TRACE(ah->ah_sc);
644
Alina Friedrichsen8cab7582009-01-23 05:39:13 +0100645 ath5k_hw_reg_write(ah, tsf64 & 0xffffffff, AR5K_TSF_L32);
Alina Friedrichsen0ad65bd2009-03-02 23:29:48 +0100646 ath5k_hw_reg_write(ah, (tsf64 >> 32) & 0xffffffff, AR5K_TSF_U32);
Alina Friedrichsen8cab7582009-01-23 05:39:13 +0100647}
648
649/**
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300650 * ath5k_hw_reset_tsf - Force a TSF reset
651 *
652 * @ah: The &struct ath5k_hw
653 *
654 * Forces a TSF reset on PCU
655 */
656void ath5k_hw_reset_tsf(struct ath5k_hw *ah)
657{
Bob Copeland14be9942008-09-28 12:09:43 -0400658 u32 val;
659
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300660 ATH5K_TRACE(ah->ah_sc);
Bob Copeland14be9942008-09-28 12:09:43 -0400661
662 val = ath5k_hw_reg_read(ah, AR5K_BEACON) | AR5K_BEACON_RESET_TSF;
663
664 /*
665 * Each write to the RESET_TSF bit toggles a hardware internal
666 * signal to reset TSF, but if left high it will cause a TSF reset
667 * on the next chip reset as well. Thus we always write the value
668 * twice to clear the signal.
669 */
670 ath5k_hw_reg_write(ah, val, AR5K_BEACON);
671 ath5k_hw_reg_write(ah, val, AR5K_BEACON);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300672}
673
674/*
675 * Initialize beacon timers
676 */
677void ath5k_hw_init_beacon(struct ath5k_hw *ah, u32 next_beacon, u32 interval)
678{
679 u32 timer1, timer2, timer3;
680
681 ATH5K_TRACE(ah->ah_sc);
682 /*
683 * Set the additional timers by mode
684 */
685 switch (ah->ah_op_mode) {
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200686 case NL80211_IFTYPE_MONITOR:
Johannes Berg05c914f2008-09-11 00:01:58 +0200687 case NL80211_IFTYPE_STATION:
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200688 /* In STA mode timer1 is used as next wakeup
689 * timer and timer2 as next CFP duration start
690 * timer. Both in 1/8TUs. */
691 /* TODO: PCF handling */
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300692 if (ah->ah_version == AR5K_AR5210) {
693 timer1 = 0xffffffff;
694 timer2 = 0xffffffff;
695 } else {
696 timer1 = 0x0000ffff;
697 timer2 = 0x0007ffff;
698 }
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200699 /* Mark associated AP as PCF incapable for now */
700 AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1, AR5K_STA_ID1_PCF);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300701 break;
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200702 case NL80211_IFTYPE_ADHOC:
703 AR5K_REG_ENABLE_BITS(ah, AR5K_TXCFG, AR5K_TXCFG_ADHOC_BCN_ATIM);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300704 default:
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200705 /* On non-STA modes timer1 is used as next DMA
706 * beacon alert (DBA) timer and timer2 as next
707 * software beacon alert. Both in 1/8TUs. */
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300708 timer1 = (next_beacon - AR5K_TUNE_DMA_BEACON_RESP) << 3;
709 timer2 = (next_beacon - AR5K_TUNE_SW_BEACON_RESP) << 3;
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200710 break;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300711 }
712
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200713 /* Timer3 marks the end of our ATIM window
714 * a zero length window is not allowed because
715 * we 'll get no beacons */
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300716 timer3 = next_beacon + (ah->ah_atim_window ? ah->ah_atim_window : 1);
717
718 /*
719 * Set the beacon register and enable all timers.
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300720 */
Nick Kossifidis35edf8a2009-06-12 16:09:53 -0700721 /* When in AP or Mesh Point mode zero timer0 to start TSF */
722 if (ah->ah_op_mode == NL80211_IFTYPE_AP ||
723 ah->ah_op_mode == NL80211_IFTYPE_MESH_POINT)
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200724 ath5k_hw_reg_write(ah, 0, AR5K_TIMER0);
Nick Kossifidis428cbd42009-04-30 15:55:47 -0400725
726 ath5k_hw_reg_write(ah, next_beacon, AR5K_TIMER0);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300727 ath5k_hw_reg_write(ah, timer1, AR5K_TIMER1);
728 ath5k_hw_reg_write(ah, timer2, AR5K_TIMER2);
729 ath5k_hw_reg_write(ah, timer3, AR5K_TIMER3);
730
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200731 /* Force a TSF reset if requested and enable beacons */
732 if (interval & AR5K_BEACON_RESET_TSF)
733 ath5k_hw_reset_tsf(ah);
734
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300735 ath5k_hw_reg_write(ah, interval & (AR5K_BEACON_PERIOD |
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200736 AR5K_BEACON_ENABLE),
737 AR5K_BEACON);
738
739 /* Flush any pending BMISS interrupts on ISR by
740 * performing a clear-on-write operation on PISR
741 * register for the BMISS bit (writing a bit on
742 * ISR togles a reset for that bit and leaves
743 * the rest bits intact) */
744 if (ah->ah_version == AR5K_AR5210)
745 ath5k_hw_reg_write(ah, AR5K_ISR_BMISS, AR5K_ISR);
746 else
747 ath5k_hw_reg_write(ah, AR5K_ISR_BMISS, AR5K_PISR);
748
749 /* TODO: Set enchanced sleep registers on AR5212
750 * based on vif->bss_conf params, until then
751 * disable power save reporting.*/
752 AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1, AR5K_STA_ID1_PWR_SV);
753
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300754}
755
756#if 0
757/*
758 * Set beacon timers
759 */
760int ath5k_hw_set_beacon_timers(struct ath5k_hw *ah,
761 const struct ath5k_beacon_state *state)
762{
763 u32 cfp_period, next_cfp, dtim, interval, next_beacon;
764
765 /*
766 * TODO: should be changed through *state
767 * review struct ath5k_beacon_state struct
768 *
769 * XXX: These are used for cfp period bellow, are they
770 * ok ? Is it O.K. for tsf here to be 0 or should we use
771 * get_tsf ?
772 */
773 u32 dtim_count = 0; /* XXX */
774 u32 cfp_count = 0; /* XXX */
775 u32 tsf = 0; /* XXX */
776
777 ATH5K_TRACE(ah->ah_sc);
778 /* Return on an invalid beacon state */
779 if (state->bs_interval < 1)
780 return -EINVAL;
781
782 interval = state->bs_interval;
783 dtim = state->bs_dtim_period;
784
785 /*
786 * PCF support?
787 */
788 if (state->bs_cfp_period > 0) {
789 /*
790 * Enable PCF mode and set the CFP
791 * (Contention Free Period) and timer registers
792 */
793 cfp_period = state->bs_cfp_period * state->bs_dtim_period *
794 state->bs_interval;
795 next_cfp = (cfp_count * state->bs_dtim_period + dtim_count) *
796 state->bs_interval;
797
798 AR5K_REG_ENABLE_BITS(ah, AR5K_STA_ID1,
799 AR5K_STA_ID1_DEFAULT_ANTENNA |
800 AR5K_STA_ID1_PCF);
801 ath5k_hw_reg_write(ah, cfp_period, AR5K_CFP_PERIOD);
802 ath5k_hw_reg_write(ah, state->bs_cfp_max_duration,
803 AR5K_CFP_DUR);
804 ath5k_hw_reg_write(ah, (tsf + (next_cfp == 0 ? cfp_period :
805 next_cfp)) << 3, AR5K_TIMER2);
806 } else {
807 /* Disable PCF mode */
808 AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1,
809 AR5K_STA_ID1_DEFAULT_ANTENNA |
810 AR5K_STA_ID1_PCF);
811 }
812
813 /*
814 * Enable the beacon timer register
815 */
816 ath5k_hw_reg_write(ah, state->bs_next_beacon, AR5K_TIMER0);
817
818 /*
819 * Start the beacon timers
820 */
821 ath5k_hw_reg_write(ah, (ath5k_hw_reg_read(ah, AR5K_BEACON) &
822 ~(AR5K_BEACON_PERIOD | AR5K_BEACON_TIM)) |
823 AR5K_REG_SM(state->bs_tim_offset ? state->bs_tim_offset + 4 : 0,
824 AR5K_BEACON_TIM) | AR5K_REG_SM(state->bs_interval,
825 AR5K_BEACON_PERIOD), AR5K_BEACON);
826
827 /*
828 * Write new beacon miss threshold, if it appears to be valid
829 * XXX: Figure out right values for min <= bs_bmiss_threshold <= max
830 * and return if its not in range. We can test this by reading value and
831 * setting value to a largest value and seeing which values register.
832 */
833
834 AR5K_REG_WRITE_BITS(ah, AR5K_RSSI_THR, AR5K_RSSI_THR_BMISS,
835 state->bs_bmiss_threshold);
836
837 /*
838 * Set sleep control register
839 * XXX: Didn't find this in 5210 code but since this register
840 * exists also in ar5k's 5210 headers i leave it as common code.
841 */
842 AR5K_REG_WRITE_BITS(ah, AR5K_SLEEP_CTL, AR5K_SLEEP_CTL_SLDUR,
843 (state->bs_sleep_duration - 3) << 3);
844
845 /*
846 * Set enhanced sleep registers on 5212
847 */
848 if (ah->ah_version == AR5K_AR5212) {
849 if (state->bs_sleep_duration > state->bs_interval &&
850 roundup(state->bs_sleep_duration, interval) ==
851 state->bs_sleep_duration)
852 interval = state->bs_sleep_duration;
853
854 if (state->bs_sleep_duration > dtim && (dtim == 0 ||
855 roundup(state->bs_sleep_duration, dtim) ==
856 state->bs_sleep_duration))
857 dtim = state->bs_sleep_duration;
858
859 if (interval > dtim)
860 return -EINVAL;
861
862 next_beacon = interval == dtim ? state->bs_next_dtim :
863 state->bs_next_beacon;
864
865 ath5k_hw_reg_write(ah,
866 AR5K_REG_SM((state->bs_next_dtim - 3) << 3,
867 AR5K_SLEEP0_NEXT_DTIM) |
868 AR5K_REG_SM(10, AR5K_SLEEP0_CABTO) |
869 AR5K_SLEEP0_ENH_SLEEP_EN |
870 AR5K_SLEEP0_ASSUME_DTIM, AR5K_SLEEP0);
871
872 ath5k_hw_reg_write(ah, AR5K_REG_SM((next_beacon - 3) << 3,
873 AR5K_SLEEP1_NEXT_TIM) |
874 AR5K_REG_SM(10, AR5K_SLEEP1_BEACON_TO), AR5K_SLEEP1);
875
876 ath5k_hw_reg_write(ah,
877 AR5K_REG_SM(interval, AR5K_SLEEP2_TIM_PER) |
878 AR5K_REG_SM(dtim, AR5K_SLEEP2_DTIM_PER), AR5K_SLEEP2);
879 }
880
881 return 0;
882}
883
884/*
885 * Reset beacon timers
886 */
887void ath5k_hw_reset_beacon(struct ath5k_hw *ah)
888{
889 ATH5K_TRACE(ah->ah_sc);
890 /*
891 * Disable beacon timer
892 */
893 ath5k_hw_reg_write(ah, 0, AR5K_TIMER0);
894
895 /*
896 * Disable some beacon register values
897 */
898 AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1,
899 AR5K_STA_ID1_DEFAULT_ANTENNA | AR5K_STA_ID1_PCF);
900 ath5k_hw_reg_write(ah, AR5K_BEACON_PERIOD, AR5K_BEACON);
901}
902
903/*
904 * Wait for beacon queue to finish
905 */
906int ath5k_hw_beaconq_finish(struct ath5k_hw *ah, unsigned long phys_addr)
907{
908 unsigned int i;
909 int ret;
910
911 ATH5K_TRACE(ah->ah_sc);
912
913 /* 5210 doesn't have QCU*/
914 if (ah->ah_version == AR5K_AR5210) {
915 /*
916 * Wait for beaconn queue to finish by checking
917 * Control Register and Beacon Status Register.
918 */
919 for (i = AR5K_TUNE_BEACON_INTERVAL / 2; i > 0; i--) {
920 if (!(ath5k_hw_reg_read(ah, AR5K_BSR) & AR5K_BSR_TXQ1F)
921 ||
922 !(ath5k_hw_reg_read(ah, AR5K_CR) & AR5K_BSR_TXQ1F))
923 break;
924 udelay(10);
925 }
926
927 /* Timeout... */
928 if (i <= 0) {
929 /*
930 * Re-schedule the beacon queue
931 */
932 ath5k_hw_reg_write(ah, phys_addr, AR5K_NOQCU_TXDP1);
933 ath5k_hw_reg_write(ah, AR5K_BCR_TQ1V | AR5K_BCR_BDMAE,
934 AR5K_BCR);
935
936 return -EIO;
937 }
938 ret = 0;
939 } else {
940 /*5211/5212*/
941 ret = ath5k_hw_register_timeout(ah,
942 AR5K_QUEUE_STATUS(AR5K_TX_QUEUE_ID_BEACON),
943 AR5K_QCU_STS_FRMPENDCNT, 0, false);
944
945 if (AR5K_REG_READ_Q(ah, AR5K_QCU_TXE, AR5K_TX_QUEUE_ID_BEACON))
946 return -EIO;
947 }
948
949 return ret;
950}
951#endif
952
953
954/*********************\
955* Key table functions *
956\*********************/
957
958/*
959 * Reset a key entry on the table
960 */
961int ath5k_hw_reset_key(struct ath5k_hw *ah, u16 entry)
962{
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200963 unsigned int i, type;
Bob Copeland17683c62008-10-29 23:24:26 -0400964 u16 micentry = entry + AR5K_KEYTABLE_MIC_OFFSET;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300965
966 ATH5K_TRACE(ah->ah_sc);
967 AR5K_ASSERT_ENTRY(entry, AR5K_KEYTABLE_SIZE);
968
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200969 type = ath5k_hw_reg_read(ah, AR5K_KEYTABLE_TYPE(entry));
970
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300971 for (i = 0; i < AR5K_KEYCACHE_SIZE; i++)
972 ath5k_hw_reg_write(ah, 0, AR5K_KEYTABLE_OFF(entry, i));
973
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200974 /* Reset associated MIC entry if TKIP
975 * is enabled located at offset (entry + 64) */
976 if (type == AR5K_KEYTABLE_TYPE_TKIP) {
Bob Copeland17683c62008-10-29 23:24:26 -0400977 AR5K_ASSERT_ENTRY(micentry, AR5K_KEYTABLE_SIZE);
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200978 for (i = 0; i < AR5K_KEYCACHE_SIZE / 2 ; i++)
Bob Copeland17683c62008-10-29 23:24:26 -0400979 ath5k_hw_reg_write(ah, 0,
980 AR5K_KEYTABLE_OFF(micentry, i));
Nick Kossifidisf07a6c42008-10-29 04:28:28 +0200981 }
982
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300983 /*
984 * Set NULL encryption on AR5212+
985 *
986 * Note: AR5K_KEYTABLE_TYPE -> AR5K_KEYTABLE_OFF(entry, 5)
987 * AR5K_KEYTABLE_TYPE_NULL -> 0x00000007
988 *
989 * Note2: Windows driver (ndiswrapper) sets this to
990 * 0x00000714 instead of 0x00000007
991 */
Jiri Slabyded7a7e2009-04-25 14:09:23 +0200992 if (ah->ah_version >= AR5K_AR5211) {
Nick Kossifidisc6e387a2008-08-29 22:45:39 +0300993 ath5k_hw_reg_write(ah, AR5K_KEYTABLE_TYPE_NULL,
994 AR5K_KEYTABLE_TYPE(entry));
995
Bob Copeland17683c62008-10-29 23:24:26 -0400996 if (type == AR5K_KEYTABLE_TYPE_TKIP) {
997 ath5k_hw_reg_write(ah, AR5K_KEYTABLE_TYPE_NULL,
998 AR5K_KEYTABLE_TYPE(micentry));
999 }
1000 }
1001
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001002 return 0;
1003}
1004
1005/*
1006 * Check if a table entry is valid
1007 */
1008int ath5k_hw_is_key_valid(struct ath5k_hw *ah, u16 entry)
1009{
1010 ATH5K_TRACE(ah->ah_sc);
1011 AR5K_ASSERT_ENTRY(entry, AR5K_KEYTABLE_SIZE);
1012
1013 /* Check the validation flag at the end of the entry */
1014 return ath5k_hw_reg_read(ah, AR5K_KEYTABLE_MAC1(entry)) &
1015 AR5K_KEYTABLE_VALID;
1016}
1017
Bob Copeland67143492008-11-25 20:55:21 -05001018static
1019int ath5k_keycache_type(const struct ieee80211_key_conf *key)
1020{
1021 switch (key->alg) {
1022 case ALG_TKIP:
1023 return AR5K_KEYTABLE_TYPE_TKIP;
1024 case ALG_CCMP:
1025 return AR5K_KEYTABLE_TYPE_CCM;
1026 case ALG_WEP:
Zhu Yie31a16d2009-05-21 21:47:03 +08001027 if (key->keylen == WLAN_KEY_LEN_WEP40)
Bob Copeland67143492008-11-25 20:55:21 -05001028 return AR5K_KEYTABLE_TYPE_40;
Zhu Yie31a16d2009-05-21 21:47:03 +08001029 else if (key->keylen == WLAN_KEY_LEN_WEP104)
Bob Copeland67143492008-11-25 20:55:21 -05001030 return AR5K_KEYTABLE_TYPE_104;
Jouni Malinen3cfcf6ac2009-01-08 13:32:02 +02001031 return -EINVAL;
1032 default:
1033 return -EINVAL;
Bob Copeland67143492008-11-25 20:55:21 -05001034 }
1035 return -EINVAL;
1036}
1037
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001038/*
1039 * Set a key entry on the table
1040 */
1041int ath5k_hw_set_key(struct ath5k_hw *ah, u16 entry,
1042 const struct ieee80211_key_conf *key, const u8 *mac)
1043{
1044 unsigned int i;
Bob Copeland3f64b432008-10-29 23:19:14 -04001045 int keylen;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001046 __le32 key_v[5] = {};
Bob Copeland3f64b432008-10-29 23:19:14 -04001047 __le32 key0 = 0, key1 = 0;
1048 __le32 *rxmic, *txmic;
Roel Kluin672cf3c2009-01-18 23:50:27 +01001049 int keytype;
Bob Copeland3f64b432008-10-29 23:19:14 -04001050 u16 micentry = entry + AR5K_KEYTABLE_MIC_OFFSET;
1051 bool is_tkip;
Bob Copeland67143492008-11-25 20:55:21 -05001052 const u8 *key_ptr;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001053
1054 ATH5K_TRACE(ah->ah_sc);
1055
Bob Copeland3f64b432008-10-29 23:19:14 -04001056 is_tkip = (key->alg == ALG_TKIP);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001057
Bob Copeland3f64b432008-10-29 23:19:14 -04001058 /*
1059 * key->keylen comes in from mac80211 in bytes.
1060 * TKIP is 128 bit + 128 bit mic
1061 */
1062 keylen = (is_tkip) ? (128 / 8) : key->keylen;
1063
1064 if (entry > AR5K_KEYTABLE_SIZE ||
1065 (is_tkip && micentry > AR5K_KEYTABLE_SIZE))
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001066 return -EOPNOTSUPP;
1067
Bob Copeland67143492008-11-25 20:55:21 -05001068 if (unlikely(keylen > 16))
1069 return -EOPNOTSUPP;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001070
Bob Copeland67143492008-11-25 20:55:21 -05001071 keytype = ath5k_keycache_type(key);
1072 if (keytype < 0)
1073 return keytype;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001074
Bob Copeland67143492008-11-25 20:55:21 -05001075 /*
1076 * each key block is 6 bytes wide, written as pairs of
1077 * alternating 32 and 16 bit le values.
1078 */
1079 key_ptr = key->key;
1080 for (i = 0; keylen >= 6; keylen -= 6) {
1081 memcpy(&key_v[i], key_ptr, 6);
1082 i += 2;
1083 key_ptr += 6;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001084 }
Bob Copeland67143492008-11-25 20:55:21 -05001085 if (keylen)
1086 memcpy(&key_v[i], key_ptr, keylen);
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001087
Bob Copeland3f64b432008-10-29 23:19:14 -04001088 /* intentionally corrupt key until mic is installed */
1089 if (is_tkip) {
1090 key0 = key_v[0] = ~key_v[0];
1091 key1 = key_v[1] = ~key_v[1];
1092 }
1093
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001094 for (i = 0; i < ARRAY_SIZE(key_v); i++)
1095 ath5k_hw_reg_write(ah, le32_to_cpu(key_v[i]),
1096 AR5K_KEYTABLE_OFF(entry, i));
1097
1098 ath5k_hw_reg_write(ah, keytype, AR5K_KEYTABLE_TYPE(entry));
1099
Bob Copeland3f64b432008-10-29 23:19:14 -04001100 if (is_tkip) {
1101 /* Install rx/tx MIC */
1102 rxmic = (__le32 *) &key->key[16];
1103 txmic = (__le32 *) &key->key[24];
Bob Copelandf6504702008-11-26 16:17:25 -05001104
1105 if (ah->ah_combined_mic) {
1106 key_v[0] = rxmic[0];
Bob Copeland388cdf32008-12-09 23:05:38 -05001107 key_v[1] = cpu_to_le32(le32_to_cpu(txmic[0]) >> 16);
Bob Copelandf6504702008-11-26 16:17:25 -05001108 key_v[2] = rxmic[1];
Bob Copeland388cdf32008-12-09 23:05:38 -05001109 key_v[3] = cpu_to_le32(le32_to_cpu(txmic[0]) & 0xffff);
Bob Copelandf6504702008-11-26 16:17:25 -05001110 key_v[4] = txmic[1];
1111 } else {
1112 key_v[0] = rxmic[0];
1113 key_v[1] = 0;
1114 key_v[2] = rxmic[1];
1115 key_v[3] = 0;
1116 key_v[4] = 0;
1117 }
Bob Copeland3f64b432008-10-29 23:19:14 -04001118 for (i = 0; i < ARRAY_SIZE(key_v); i++)
1119 ath5k_hw_reg_write(ah, le32_to_cpu(key_v[i]),
1120 AR5K_KEYTABLE_OFF(micentry, i));
1121
1122 ath5k_hw_reg_write(ah, AR5K_KEYTABLE_TYPE_NULL,
1123 AR5K_KEYTABLE_TYPE(micentry));
1124 ath5k_hw_reg_write(ah, 0, AR5K_KEYTABLE_MAC0(micentry));
1125 ath5k_hw_reg_write(ah, 0, AR5K_KEYTABLE_MAC1(micentry));
1126
1127 /* restore first 2 words of key */
1128 ath5k_hw_reg_write(ah, le32_to_cpu(~key0),
1129 AR5K_KEYTABLE_OFF(entry, 0));
1130 ath5k_hw_reg_write(ah, le32_to_cpu(~key1),
1131 AR5K_KEYTABLE_OFF(entry, 1));
1132 }
1133
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001134 return ath5k_hw_set_key_lladdr(ah, entry, mac);
1135}
1136
1137int ath5k_hw_set_key_lladdr(struct ath5k_hw *ah, u16 entry, const u8 *mac)
1138{
1139 u32 low_id, high_id;
1140
1141 ATH5K_TRACE(ah->ah_sc);
1142 /* Invalid entry (key table overflow) */
1143 AR5K_ASSERT_ENTRY(entry, AR5K_KEYTABLE_SIZE);
1144
Luis R. Rodriguezbcd8f542009-09-09 22:43:17 -07001145 /*
1146 * MAC may be NULL if it's a broadcast key. In this case no need to
1147 * to compute get_unaligned_le32 and get_unaligned_le16 as we
1148 * already know it.
1149 */
Johannes Bergdc822b52008-12-29 12:55:09 +01001150 if (!mac) {
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001151 low_id = 0xffffffff;
1152 high_id = 0xffff | AR5K_KEYTABLE_VALID;
1153 } else {
Luis R. Rodriguezbcd8f542009-09-09 22:43:17 -07001154 low_id = get_unaligned_le32(mac);
1155 high_id = get_unaligned_le16(mac + 4) | AR5K_KEYTABLE_VALID;
Nick Kossifidisc6e387a2008-08-29 22:45:39 +03001156 }
1157
1158 ath5k_hw_reg_write(ah, low_id, AR5K_KEYTABLE_MAC0(entry));
1159 ath5k_hw_reg_write(ah, high_id, AR5K_KEYTABLE_MAC1(entry));
1160
1161 return 0;
1162}
1163