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>
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.
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.
23 /*********************************\
24 * Protocol Control Unit Functions *
25 \*********************************/
27 #include <asm/unaligned.h>
39 * ath5k_hw_set_opmode - Set PCU operating mode
41 * @ah: The &struct ath5k_hw
43 * Initialize PCU for the various operating modes (AP/STA etc)
45 * NOTE: ah->ah_op_mode must be set before calling this.
47 int ath5k_hw_set_opmode(struct ath5k_hw *ah)
49 struct ath_common *common = ath5k_hw_common(ah);
50 u32 pcu_reg, beacon_reg, low_id, high_id;
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));
62 ATH5K_TRACE(ah->ah_sc);
64 switch (ah->ah_op_mode) {
65 case NL80211_IFTYPE_ADHOC:
66 pcu_reg |= AR5K_STA_ID1_ADHOC | AR5K_STA_ID1_KEYSRCH_MODE;
67 beacon_reg |= AR5K_BCR_ADHOC;
68 if (ah->ah_version == AR5K_AR5210)
69 pcu_reg |= AR5K_STA_ID1_NO_PSPOLL;
71 AR5K_REG_ENABLE_BITS(ah, AR5K_CFG, AR5K_CFG_IBSS);
74 case NL80211_IFTYPE_AP:
75 case NL80211_IFTYPE_MESH_POINT:
76 pcu_reg |= AR5K_STA_ID1_AP | AR5K_STA_ID1_KEYSRCH_MODE;
77 beacon_reg |= AR5K_BCR_AP;
78 if (ah->ah_version == AR5K_AR5210)
79 pcu_reg |= AR5K_STA_ID1_NO_PSPOLL;
81 AR5K_REG_DISABLE_BITS(ah, AR5K_CFG, AR5K_CFG_IBSS);
84 case NL80211_IFTYPE_STATION:
85 pcu_reg |= AR5K_STA_ID1_KEYSRCH_MODE
86 | (ah->ah_version == AR5K_AR5210 ?
87 AR5K_STA_ID1_PWR_SV : 0);
88 case NL80211_IFTYPE_MONITOR:
89 pcu_reg |= AR5K_STA_ID1_KEYSRCH_MODE
90 | (ah->ah_version == AR5K_AR5210 ?
91 AR5K_STA_ID1_NO_PSPOLL : 0);
101 low_id = get_unaligned_le32(common->macaddr);
102 high_id = get_unaligned_le16(common->macaddr + 4);
103 ath5k_hw_reg_write(ah, low_id, AR5K_STA_ID0);
104 ath5k_hw_reg_write(ah, pcu_reg | high_id, AR5K_STA_ID1);
107 * Set Beacon Control Register on 5210
109 if (ah->ah_version == AR5K_AR5210)
110 ath5k_hw_reg_write(ah, beacon_reg, AR5K_BCR);
116 * ath5k_hw_update - Update mib counters (mac layer statistics)
118 * @ah: The &struct ath5k_hw
119 * @stats: The &struct ieee80211_low_level_stats we use to track
120 * statistics on the driver
122 * Reads MIB counters from PCU and updates sw statistics. Must be
123 * called after a MIB interrupt.
125 void ath5k_hw_update_mib_counters(struct ath5k_hw *ah,
126 struct ieee80211_low_level_stats *stats)
128 ATH5K_TRACE(ah->ah_sc);
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);
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);
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);
148 /* TODO: Handle ANI stats */
152 * ath5k_hw_set_ack_bitrate - set bitrate for ACKs
154 * @ah: The &struct ath5k_hw
155 * @high: Flag to determine if we want to use high transmition rate
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).
163 void ath5k_hw_set_ack_bitrate_high(struct ath5k_hw *ah, bool high)
165 if (ah->ah_version != AR5K_AR5212)
168 u32 val = AR5K_STA_ID1_BASE_RATE_11B | AR5K_STA_ID1_ACKCTS_6MB;
170 AR5K_REG_ENABLE_BITS(ah, AR5K_STA_ID1, val);
172 AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1, val);
182 * ath5k_hw_het_ack_timeout - Get ACK timeout from PCU in usec
184 * @ah: The &struct ath5k_hw
186 unsigned int ath5k_hw_get_ack_timeout(struct ath5k_hw *ah)
188 ATH5K_TRACE(ah->ah_sc);
190 return ath5k_hw_clocktoh(AR5K_REG_MS(ath5k_hw_reg_read(ah,
191 AR5K_TIME_OUT), AR5K_TIME_OUT_ACK), ah->ah_turbo);
195 * ath5k_hw_set_ack_timeout - Set ACK timeout on PCU
197 * @ah: The &struct ath5k_hw
198 * @timeout: Timeout in usec
200 int ath5k_hw_set_ack_timeout(struct ath5k_hw *ah, unsigned int timeout)
202 ATH5K_TRACE(ah->ah_sc);
203 if (ath5k_hw_clocktoh(AR5K_REG_MS(0xffffffff, AR5K_TIME_OUT_ACK),
204 ah->ah_turbo) <= timeout)
207 AR5K_REG_WRITE_BITS(ah, AR5K_TIME_OUT, AR5K_TIME_OUT_ACK,
208 ath5k_hw_htoclock(timeout, ah->ah_turbo));
214 * ath5k_hw_get_cts_timeout - Get CTS timeout from PCU in usec
216 * @ah: The &struct ath5k_hw
218 unsigned int ath5k_hw_get_cts_timeout(struct ath5k_hw *ah)
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);
226 * ath5k_hw_set_cts_timeout - Set CTS timeout on PCU
228 * @ah: The &struct ath5k_hw
229 * @timeout: Timeout in usec
231 int ath5k_hw_set_cts_timeout(struct ath5k_hw *ah, unsigned int timeout)
233 ATH5K_TRACE(ah->ah_sc);
234 if (ath5k_hw_clocktoh(AR5K_REG_MS(0xffffffff, AR5K_TIME_OUT_CTS),
235 ah->ah_turbo) <= timeout)
238 AR5K_REG_WRITE_BITS(ah, AR5K_TIME_OUT, AR5K_TIME_OUT_CTS,
239 ath5k_hw_htoclock(timeout, ah->ah_turbo));
245 * ath5k_hw_set_lladdr - Set station id
247 * @ah: The &struct ath5k_hw
248 * @mac: The card's mac address
250 * Set station id on hw using the provided mac address
252 int ath5k_hw_set_lladdr(struct ath5k_hw *ah, const u8 *mac)
254 struct ath_common *common = ath5k_hw_common(ah);
258 ATH5K_TRACE(ah->ah_sc);
259 /* Set new station ID */
260 memcpy(common->macaddr, mac, ETH_ALEN);
262 pcu_reg = ath5k_hw_reg_read(ah, AR5K_STA_ID1) & 0xffff0000;
264 low_id = get_unaligned_le32(mac);
265 high_id = get_unaligned_le16(mac + 4);
267 ath5k_hw_reg_write(ah, low_id, AR5K_STA_ID0);
268 ath5k_hw_reg_write(ah, pcu_reg | high_id, AR5K_STA_ID1);
274 * ath5k_hw_set_associd - Set BSSID for association
276 * @ah: The &struct ath5k_hw
278 * @assoc_id: Assoc id
280 * Sets the BSSID which trigers the "SME Join" operation
282 void ath5k_hw_set_associd(struct ath5k_hw *ah, const u8 *bssid, u16 assoc_id)
284 struct ath_common *common = ath5k_hw_common(ah);
289 * Set simple BSSID mask on 5212
291 if (ah->ah_version == AR5K_AR5212)
292 ath_hw_setbssidmask(common);
295 * Set BSSID which triggers the "SME Join" operation
297 low_id = get_unaligned_le32(bssid);
298 high_id = get_unaligned_le16(bssid);
299 ath5k_hw_reg_write(ah, low_id, AR_BSSMSKL);
300 ath5k_hw_reg_write(ah, high_id | ((assoc_id & 0x3fff) <<
301 AR5K_BSS_ID1_AID_S), AR_BSSMSKU);
304 ath5k_hw_disable_pspoll(ah);
308 AR5K_REG_WRITE_BITS(ah, AR5K_BEACON, AR5K_BEACON_TIM,
309 tim_offset ? tim_offset + 4 : 0);
311 ath5k_hw_enable_pspoll(ah, NULL, 0);
314 void ath5k_hw_set_bssid_mask(struct ath5k_hw *ah, const u8 *mask)
316 struct ath_common *common = ath5k_hw_common(ah);
317 ATH5K_TRACE(ah->ah_sc);
319 /* Cache bssid mask so that we can restore it
321 memcpy(common->bssidmask, mask, ETH_ALEN);
322 if (ah->ah_version == AR5K_AR5212)
323 ath_hw_setbssidmask(common);
331 * ath5k_hw_start_rx_pcu - Start RX engine
333 * @ah: The &struct ath5k_hw
335 * Starts RX engine on PCU so that hw can process RXed frames
338 * NOTE: RX DMA should be already enabled using ath5k_hw_start_rx_dma
339 * TODO: Init ANI here
341 void ath5k_hw_start_rx_pcu(struct ath5k_hw *ah)
343 ATH5K_TRACE(ah->ah_sc);
344 AR5K_REG_DISABLE_BITS(ah, AR5K_DIAG_SW, AR5K_DIAG_SW_DIS_RX);
348 * at5k_hw_stop_rx_pcu - Stop RX engine
350 * @ah: The &struct ath5k_hw
352 * Stops RX engine on PCU
354 * TODO: Detach ANI here
356 void ath5k_hw_stop_rx_pcu(struct ath5k_hw *ah)
358 ATH5K_TRACE(ah->ah_sc);
359 AR5K_REG_ENABLE_BITS(ah, AR5K_DIAG_SW, AR5K_DIAG_SW_DIS_RX);
363 * Set multicast filter
365 void ath5k_hw_set_mcast_filter(struct ath5k_hw *ah, u32 filter0, u32 filter1)
367 ATH5K_TRACE(ah->ah_sc);
368 /* Set the multicat filter */
369 ath5k_hw_reg_write(ah, filter0, AR5K_MCAST_FILTER0);
370 ath5k_hw_reg_write(ah, filter1, AR5K_MCAST_FILTER1);
374 * Set multicast filter by index
376 int ath5k_hw_set_mcast_filter_idx(struct ath5k_hw *ah, u32 index)
379 ATH5K_TRACE(ah->ah_sc);
382 else if (index >= 32)
383 AR5K_REG_ENABLE_BITS(ah, AR5K_MCAST_FILTER1,
384 (1 << (index - 32)));
386 AR5K_REG_ENABLE_BITS(ah, AR5K_MCAST_FILTER0, (1 << index));
392 * Clear Multicast filter by index
394 int ath5k_hw_clear_mcast_filter_idx(struct ath5k_hw *ah, u32 index)
397 ATH5K_TRACE(ah->ah_sc);
400 else if (index >= 32)
401 AR5K_REG_DISABLE_BITS(ah, AR5K_MCAST_FILTER1,
402 (1 << (index - 32)));
404 AR5K_REG_DISABLE_BITS(ah, AR5K_MCAST_FILTER0, (1 << index));
410 * ath5k_hw_get_rx_filter - Get current rx filter
412 * @ah: The &struct ath5k_hw
414 * Returns the RX filter by reading rx filter and
415 * phy error filter registers. RX filter is used
416 * to set the allowed frame types that PCU will accept
417 * and pass to the driver. For a list of frame types
420 u32 ath5k_hw_get_rx_filter(struct ath5k_hw *ah)
422 u32 data, filter = 0;
424 ATH5K_TRACE(ah->ah_sc);
425 filter = ath5k_hw_reg_read(ah, AR5K_RX_FILTER);
427 /*Radar detection for 5212*/
428 if (ah->ah_version == AR5K_AR5212) {
429 data = ath5k_hw_reg_read(ah, AR5K_PHY_ERR_FIL);
431 if (data & AR5K_PHY_ERR_FIL_RADAR)
432 filter |= AR5K_RX_FILTER_RADARERR;
433 if (data & (AR5K_PHY_ERR_FIL_OFDM | AR5K_PHY_ERR_FIL_CCK))
434 filter |= AR5K_RX_FILTER_PHYERR;
441 * ath5k_hw_set_rx_filter - Set rx filter
443 * @ah: The &struct ath5k_hw
444 * @filter: RX filter mask (see reg.h)
446 * Sets RX filter register and also handles PHY error filter
447 * register on 5212 and newer chips so that we have proper PHY
450 void ath5k_hw_set_rx_filter(struct ath5k_hw *ah, u32 filter)
454 ATH5K_TRACE(ah->ah_sc);
456 /* Set PHY error filter register on 5212*/
457 if (ah->ah_version == AR5K_AR5212) {
458 if (filter & AR5K_RX_FILTER_RADARERR)
459 data |= AR5K_PHY_ERR_FIL_RADAR;
460 if (filter & AR5K_RX_FILTER_PHYERR)
461 data |= AR5K_PHY_ERR_FIL_OFDM | AR5K_PHY_ERR_FIL_CCK;
465 * The AR5210 uses promiscous mode to detect radar activity
467 if (ah->ah_version == AR5K_AR5210 &&
468 (filter & AR5K_RX_FILTER_RADARERR)) {
469 filter &= ~AR5K_RX_FILTER_RADARERR;
470 filter |= AR5K_RX_FILTER_PROM;
473 /*Zero length DMA (phy error reporting) */
475 AR5K_REG_ENABLE_BITS(ah, AR5K_RXCFG, AR5K_RXCFG_ZLFDMA);
477 AR5K_REG_DISABLE_BITS(ah, AR5K_RXCFG, AR5K_RXCFG_ZLFDMA);
479 /*Write RX Filter register*/
480 ath5k_hw_reg_write(ah, filter & 0xff, AR5K_RX_FILTER);
482 /*Write PHY error filter register on 5212*/
483 if (ah->ah_version == AR5K_AR5212)
484 ath5k_hw_reg_write(ah, data, AR5K_PHY_ERR_FIL);
494 * ath5k_hw_get_tsf32 - Get a 32bit TSF
496 * @ah: The &struct ath5k_hw
498 * Returns lower 32 bits of current TSF
500 u32 ath5k_hw_get_tsf32(struct ath5k_hw *ah)
502 ATH5K_TRACE(ah->ah_sc);
503 return ath5k_hw_reg_read(ah, AR5K_TSF_L32);
507 * ath5k_hw_get_tsf64 - Get the full 64bit TSF
509 * @ah: The &struct ath5k_hw
511 * Returns the current TSF
513 u64 ath5k_hw_get_tsf64(struct ath5k_hw *ah)
515 u64 tsf = ath5k_hw_reg_read(ah, AR5K_TSF_U32);
516 ATH5K_TRACE(ah->ah_sc);
518 return ath5k_hw_reg_read(ah, AR5K_TSF_L32) | (tsf << 32);
522 * ath5k_hw_set_tsf64 - Set a new 64bit TSF
524 * @ah: The &struct ath5k_hw
525 * @tsf64: The new 64bit TSF
529 void ath5k_hw_set_tsf64(struct ath5k_hw *ah, u64 tsf64)
531 ATH5K_TRACE(ah->ah_sc);
533 ath5k_hw_reg_write(ah, tsf64 & 0xffffffff, AR5K_TSF_L32);
534 ath5k_hw_reg_write(ah, (tsf64 >> 32) & 0xffffffff, AR5K_TSF_U32);
538 * ath5k_hw_reset_tsf - Force a TSF reset
540 * @ah: The &struct ath5k_hw
542 * Forces a TSF reset on PCU
544 void ath5k_hw_reset_tsf(struct ath5k_hw *ah)
548 ATH5K_TRACE(ah->ah_sc);
550 val = ath5k_hw_reg_read(ah, AR5K_BEACON) | AR5K_BEACON_RESET_TSF;
553 * Each write to the RESET_TSF bit toggles a hardware internal
554 * signal to reset TSF, but if left high it will cause a TSF reset
555 * on the next chip reset as well. Thus we always write the value
556 * twice to clear the signal.
558 ath5k_hw_reg_write(ah, val, AR5K_BEACON);
559 ath5k_hw_reg_write(ah, val, AR5K_BEACON);
563 * Initialize beacon timers
565 void ath5k_hw_init_beacon(struct ath5k_hw *ah, u32 next_beacon, u32 interval)
567 u32 timer1, timer2, timer3;
569 ATH5K_TRACE(ah->ah_sc);
571 * Set the additional timers by mode
573 switch (ah->ah_op_mode) {
574 case NL80211_IFTYPE_MONITOR:
575 case NL80211_IFTYPE_STATION:
576 /* In STA mode timer1 is used as next wakeup
577 * timer and timer2 as next CFP duration start
578 * timer. Both in 1/8TUs. */
579 /* TODO: PCF handling */
580 if (ah->ah_version == AR5K_AR5210) {
587 /* Mark associated AP as PCF incapable for now */
588 AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1, AR5K_STA_ID1_PCF);
590 case NL80211_IFTYPE_ADHOC:
591 AR5K_REG_ENABLE_BITS(ah, AR5K_TXCFG, AR5K_TXCFG_ADHOC_BCN_ATIM);
593 /* On non-STA modes timer1 is used as next DMA
594 * beacon alert (DBA) timer and timer2 as next
595 * software beacon alert. Both in 1/8TUs. */
596 timer1 = (next_beacon - AR5K_TUNE_DMA_BEACON_RESP) << 3;
597 timer2 = (next_beacon - AR5K_TUNE_SW_BEACON_RESP) << 3;
601 /* Timer3 marks the end of our ATIM window
602 * a zero length window is not allowed because
603 * we 'll get no beacons */
604 timer3 = next_beacon + (ah->ah_atim_window ? ah->ah_atim_window : 1);
607 * Set the beacon register and enable all timers.
609 /* When in AP or Mesh Point mode zero timer0 to start TSF */
610 if (ah->ah_op_mode == NL80211_IFTYPE_AP ||
611 ah->ah_op_mode == NL80211_IFTYPE_MESH_POINT)
612 ath5k_hw_reg_write(ah, 0, AR5K_TIMER0);
614 ath5k_hw_reg_write(ah, next_beacon, AR5K_TIMER0);
615 ath5k_hw_reg_write(ah, timer1, AR5K_TIMER1);
616 ath5k_hw_reg_write(ah, timer2, AR5K_TIMER2);
617 ath5k_hw_reg_write(ah, timer3, AR5K_TIMER3);
619 /* Force a TSF reset if requested and enable beacons */
620 if (interval & AR5K_BEACON_RESET_TSF)
621 ath5k_hw_reset_tsf(ah);
623 ath5k_hw_reg_write(ah, interval & (AR5K_BEACON_PERIOD |
627 /* Flush any pending BMISS interrupts on ISR by
628 * performing a clear-on-write operation on PISR
629 * register for the BMISS bit (writing a bit on
630 * ISR togles a reset for that bit and leaves
631 * the rest bits intact) */
632 if (ah->ah_version == AR5K_AR5210)
633 ath5k_hw_reg_write(ah, AR5K_ISR_BMISS, AR5K_ISR);
635 ath5k_hw_reg_write(ah, AR5K_ISR_BMISS, AR5K_PISR);
637 /* TODO: Set enchanced sleep registers on AR5212
638 * based on vif->bss_conf params, until then
639 * disable power save reporting.*/
640 AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1, AR5K_STA_ID1_PWR_SV);
648 int ath5k_hw_set_beacon_timers(struct ath5k_hw *ah,
649 const struct ath5k_beacon_state *state)
651 u32 cfp_period, next_cfp, dtim, interval, next_beacon;
654 * TODO: should be changed through *state
655 * review struct ath5k_beacon_state struct
657 * XXX: These are used for cfp period bellow, are they
658 * ok ? Is it O.K. for tsf here to be 0 or should we use
661 u32 dtim_count = 0; /* XXX */
662 u32 cfp_count = 0; /* XXX */
663 u32 tsf = 0; /* XXX */
665 ATH5K_TRACE(ah->ah_sc);
666 /* Return on an invalid beacon state */
667 if (state->bs_interval < 1)
670 interval = state->bs_interval;
671 dtim = state->bs_dtim_period;
676 if (state->bs_cfp_period > 0) {
678 * Enable PCF mode and set the CFP
679 * (Contention Free Period) and timer registers
681 cfp_period = state->bs_cfp_period * state->bs_dtim_period *
683 next_cfp = (cfp_count * state->bs_dtim_period + dtim_count) *
686 AR5K_REG_ENABLE_BITS(ah, AR5K_STA_ID1,
687 AR5K_STA_ID1_DEFAULT_ANTENNA |
689 ath5k_hw_reg_write(ah, cfp_period, AR5K_CFP_PERIOD);
690 ath5k_hw_reg_write(ah, state->bs_cfp_max_duration,
692 ath5k_hw_reg_write(ah, (tsf + (next_cfp == 0 ? cfp_period :
693 next_cfp)) << 3, AR5K_TIMER2);
695 /* Disable PCF mode */
696 AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1,
697 AR5K_STA_ID1_DEFAULT_ANTENNA |
702 * Enable the beacon timer register
704 ath5k_hw_reg_write(ah, state->bs_next_beacon, AR5K_TIMER0);
707 * Start the beacon timers
709 ath5k_hw_reg_write(ah, (ath5k_hw_reg_read(ah, AR5K_BEACON) &
710 ~(AR5K_BEACON_PERIOD | AR5K_BEACON_TIM)) |
711 AR5K_REG_SM(state->bs_tim_offset ? state->bs_tim_offset + 4 : 0,
712 AR5K_BEACON_TIM) | AR5K_REG_SM(state->bs_interval,
713 AR5K_BEACON_PERIOD), AR5K_BEACON);
716 * Write new beacon miss threshold, if it appears to be valid
717 * XXX: Figure out right values for min <= bs_bmiss_threshold <= max
718 * and return if its not in range. We can test this by reading value and
719 * setting value to a largest value and seeing which values register.
722 AR5K_REG_WRITE_BITS(ah, AR5K_RSSI_THR, AR5K_RSSI_THR_BMISS,
723 state->bs_bmiss_threshold);
726 * Set sleep control register
727 * XXX: Didn't find this in 5210 code but since this register
728 * exists also in ar5k's 5210 headers i leave it as common code.
730 AR5K_REG_WRITE_BITS(ah, AR5K_SLEEP_CTL, AR5K_SLEEP_CTL_SLDUR,
731 (state->bs_sleep_duration - 3) << 3);
734 * Set enhanced sleep registers on 5212
736 if (ah->ah_version == AR5K_AR5212) {
737 if (state->bs_sleep_duration > state->bs_interval &&
738 roundup(state->bs_sleep_duration, interval) ==
739 state->bs_sleep_duration)
740 interval = state->bs_sleep_duration;
742 if (state->bs_sleep_duration > dtim && (dtim == 0 ||
743 roundup(state->bs_sleep_duration, dtim) ==
744 state->bs_sleep_duration))
745 dtim = state->bs_sleep_duration;
750 next_beacon = interval == dtim ? state->bs_next_dtim :
751 state->bs_next_beacon;
753 ath5k_hw_reg_write(ah,
754 AR5K_REG_SM((state->bs_next_dtim - 3) << 3,
755 AR5K_SLEEP0_NEXT_DTIM) |
756 AR5K_REG_SM(10, AR5K_SLEEP0_CABTO) |
757 AR5K_SLEEP0_ENH_SLEEP_EN |
758 AR5K_SLEEP0_ASSUME_DTIM, AR5K_SLEEP0);
760 ath5k_hw_reg_write(ah, AR5K_REG_SM((next_beacon - 3) << 3,
761 AR5K_SLEEP1_NEXT_TIM) |
762 AR5K_REG_SM(10, AR5K_SLEEP1_BEACON_TO), AR5K_SLEEP1);
764 ath5k_hw_reg_write(ah,
765 AR5K_REG_SM(interval, AR5K_SLEEP2_TIM_PER) |
766 AR5K_REG_SM(dtim, AR5K_SLEEP2_DTIM_PER), AR5K_SLEEP2);
773 * Reset beacon timers
775 void ath5k_hw_reset_beacon(struct ath5k_hw *ah)
777 ATH5K_TRACE(ah->ah_sc);
779 * Disable beacon timer
781 ath5k_hw_reg_write(ah, 0, AR5K_TIMER0);
784 * Disable some beacon register values
786 AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1,
787 AR5K_STA_ID1_DEFAULT_ANTENNA | AR5K_STA_ID1_PCF);
788 ath5k_hw_reg_write(ah, AR5K_BEACON_PERIOD, AR5K_BEACON);
792 * Wait for beacon queue to finish
794 int ath5k_hw_beaconq_finish(struct ath5k_hw *ah, unsigned long phys_addr)
799 ATH5K_TRACE(ah->ah_sc);
801 /* 5210 doesn't have QCU*/
802 if (ah->ah_version == AR5K_AR5210) {
804 * Wait for beaconn queue to finish by checking
805 * Control Register and Beacon Status Register.
807 for (i = AR5K_TUNE_BEACON_INTERVAL / 2; i > 0; i--) {
808 if (!(ath5k_hw_reg_read(ah, AR5K_BSR) & AR5K_BSR_TXQ1F)
810 !(ath5k_hw_reg_read(ah, AR5K_CR) & AR5K_BSR_TXQ1F))
818 * Re-schedule the beacon queue
820 ath5k_hw_reg_write(ah, phys_addr, AR5K_NOQCU_TXDP1);
821 ath5k_hw_reg_write(ah, AR5K_BCR_TQ1V | AR5K_BCR_BDMAE,
829 ret = ath5k_hw_register_timeout(ah,
830 AR5K_QUEUE_STATUS(AR5K_TX_QUEUE_ID_BEACON),
831 AR5K_QCU_STS_FRMPENDCNT, 0, false);
833 if (AR5K_REG_READ_Q(ah, AR5K_QCU_TXE, AR5K_TX_QUEUE_ID_BEACON))
842 /*********************\
843 * Key table functions *
844 \*********************/
847 * Reset a key entry on the table
849 int ath5k_hw_reset_key(struct ath5k_hw *ah, u16 entry)
851 unsigned int i, type;
852 u16 micentry = entry + AR5K_KEYTABLE_MIC_OFFSET;
854 ATH5K_TRACE(ah->ah_sc);
855 AR5K_ASSERT_ENTRY(entry, AR5K_KEYTABLE_SIZE);
857 type = ath5k_hw_reg_read(ah, AR5K_KEYTABLE_TYPE(entry));
859 for (i = 0; i < AR5K_KEYCACHE_SIZE; i++)
860 ath5k_hw_reg_write(ah, 0, AR5K_KEYTABLE_OFF(entry, i));
862 /* Reset associated MIC entry if TKIP
863 * is enabled located at offset (entry + 64) */
864 if (type == AR5K_KEYTABLE_TYPE_TKIP) {
865 AR5K_ASSERT_ENTRY(micentry, AR5K_KEYTABLE_SIZE);
866 for (i = 0; i < AR5K_KEYCACHE_SIZE / 2 ; i++)
867 ath5k_hw_reg_write(ah, 0,
868 AR5K_KEYTABLE_OFF(micentry, i));
872 * Set NULL encryption on AR5212+
874 * Note: AR5K_KEYTABLE_TYPE -> AR5K_KEYTABLE_OFF(entry, 5)
875 * AR5K_KEYTABLE_TYPE_NULL -> 0x00000007
877 * Note2: Windows driver (ndiswrapper) sets this to
878 * 0x00000714 instead of 0x00000007
880 if (ah->ah_version >= AR5K_AR5211) {
881 ath5k_hw_reg_write(ah, AR5K_KEYTABLE_TYPE_NULL,
882 AR5K_KEYTABLE_TYPE(entry));
884 if (type == AR5K_KEYTABLE_TYPE_TKIP) {
885 ath5k_hw_reg_write(ah, AR5K_KEYTABLE_TYPE_NULL,
886 AR5K_KEYTABLE_TYPE(micentry));
894 * Check if a table entry is valid
896 int ath5k_hw_is_key_valid(struct ath5k_hw *ah, u16 entry)
898 ATH5K_TRACE(ah->ah_sc);
899 AR5K_ASSERT_ENTRY(entry, AR5K_KEYTABLE_SIZE);
901 /* Check the validation flag at the end of the entry */
902 return ath5k_hw_reg_read(ah, AR5K_KEYTABLE_MAC1(entry)) &
907 int ath5k_keycache_type(const struct ieee80211_key_conf *key)
911 return AR5K_KEYTABLE_TYPE_TKIP;
913 return AR5K_KEYTABLE_TYPE_CCM;
915 if (key->keylen == WLAN_KEY_LEN_WEP40)
916 return AR5K_KEYTABLE_TYPE_40;
917 else if (key->keylen == WLAN_KEY_LEN_WEP104)
918 return AR5K_KEYTABLE_TYPE_104;
927 * Set a key entry on the table
929 int ath5k_hw_set_key(struct ath5k_hw *ah, u16 entry,
930 const struct ieee80211_key_conf *key, const u8 *mac)
934 __le32 key_v[5] = {};
935 __le32 key0 = 0, key1 = 0;
936 __le32 *rxmic, *txmic;
938 u16 micentry = entry + AR5K_KEYTABLE_MIC_OFFSET;
942 ATH5K_TRACE(ah->ah_sc);
944 is_tkip = (key->alg == ALG_TKIP);
947 * key->keylen comes in from mac80211 in bytes.
948 * TKIP is 128 bit + 128 bit mic
950 keylen = (is_tkip) ? (128 / 8) : key->keylen;
952 if (entry > AR5K_KEYTABLE_SIZE ||
953 (is_tkip && micentry > AR5K_KEYTABLE_SIZE))
956 if (unlikely(keylen > 16))
959 keytype = ath5k_keycache_type(key);
964 * each key block is 6 bytes wide, written as pairs of
965 * alternating 32 and 16 bit le values.
968 for (i = 0; keylen >= 6; keylen -= 6) {
969 memcpy(&key_v[i], key_ptr, 6);
974 memcpy(&key_v[i], key_ptr, keylen);
976 /* intentionally corrupt key until mic is installed */
978 key0 = key_v[0] = ~key_v[0];
979 key1 = key_v[1] = ~key_v[1];
982 for (i = 0; i < ARRAY_SIZE(key_v); i++)
983 ath5k_hw_reg_write(ah, le32_to_cpu(key_v[i]),
984 AR5K_KEYTABLE_OFF(entry, i));
986 ath5k_hw_reg_write(ah, keytype, AR5K_KEYTABLE_TYPE(entry));
989 /* Install rx/tx MIC */
990 rxmic = (__le32 *) &key->key[16];
991 txmic = (__le32 *) &key->key[24];
993 if (ah->ah_combined_mic) {
995 key_v[1] = cpu_to_le32(le32_to_cpu(txmic[0]) >> 16);
997 key_v[3] = cpu_to_le32(le32_to_cpu(txmic[0]) & 0xffff);
1000 key_v[0] = rxmic[0];
1002 key_v[2] = rxmic[1];
1006 for (i = 0; i < ARRAY_SIZE(key_v); i++)
1007 ath5k_hw_reg_write(ah, le32_to_cpu(key_v[i]),
1008 AR5K_KEYTABLE_OFF(micentry, i));
1010 ath5k_hw_reg_write(ah, AR5K_KEYTABLE_TYPE_NULL,
1011 AR5K_KEYTABLE_TYPE(micentry));
1012 ath5k_hw_reg_write(ah, 0, AR5K_KEYTABLE_MAC0(micentry));
1013 ath5k_hw_reg_write(ah, 0, AR5K_KEYTABLE_MAC1(micentry));
1015 /* restore first 2 words of key */
1016 ath5k_hw_reg_write(ah, le32_to_cpu(~key0),
1017 AR5K_KEYTABLE_OFF(entry, 0));
1018 ath5k_hw_reg_write(ah, le32_to_cpu(~key1),
1019 AR5K_KEYTABLE_OFF(entry, 1));
1022 return ath5k_hw_set_key_lladdr(ah, entry, mac);
1025 int ath5k_hw_set_key_lladdr(struct ath5k_hw *ah, u16 entry, const u8 *mac)
1027 u32 low_id, high_id;
1029 ATH5K_TRACE(ah->ah_sc);
1030 /* Invalid entry (key table overflow) */
1031 AR5K_ASSERT_ENTRY(entry, AR5K_KEYTABLE_SIZE);
1034 * MAC may be NULL if it's a broadcast key. In this case no need to
1035 * to compute get_unaligned_le32 and get_unaligned_le16 as we
1039 low_id = 0xffffffff;
1040 high_id = 0xffff | AR5K_KEYTABLE_VALID;
1042 low_id = get_unaligned_le32(mac);
1043 high_id = get_unaligned_le16(mac + 4) | AR5K_KEYTABLE_VALID;
1046 ath5k_hw_reg_write(ah, low_id, AR5K_KEYTABLE_MAC0(entry));
1047 ath5k_hw_reg_write(ah, high_id, AR5K_KEYTABLE_MAC1(entry));