]> nv-tegra.nvidia Code Review - linux-2.6.git/blob - drivers/net/wireless/ath/ath5k/pcu.c
0385e80225294c670f68e9fc89adcca461893913
[linux-2.6.git] / drivers / net / wireless / ath / ath5k / pcu.c
1 /*
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
27 #include <asm/unaligned.h>
28
29 #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  */
47 int ath5k_hw_set_opmode(struct ath5k_hw *ah)
48 {
49         struct ath_common *common = ath5k_hw_common(ah);
50         u32 pcu_reg, beacon_reg, low_id, high_id;
51
52
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
60         beacon_reg = 0;
61
62         ATH5K_TRACE(ah->ah_sc);
63
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;
70                 else
71                         AR5K_REG_ENABLE_BITS(ah, AR5K_CFG, AR5K_CFG_IBSS);
72                 break;
73
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;
80                 else
81                         AR5K_REG_DISABLE_BITS(ah, AR5K_CFG, AR5K_CFG_IBSS);
82                 break;
83
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);
92                 break;
93
94         default:
95                 return -EINVAL;
96         }
97
98         /*
99          * Set PCU registers
100          */
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);
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  */
125 void 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         }
147
148         /* TODO: Handle ANI stats */
149 }
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  */
163 void 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  */
186 unsigned 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  */
200 int 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  */
218 unsigned 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  */
231 int 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
244 /**
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  */
252 int ath5k_hw_set_lladdr(struct ath5k_hw *ah, const u8 *mac)
253 {
254         struct ath_common *common = ath5k_hw_common(ah);
255         u32 low_id, high_id;
256         u32 pcu_reg;
257
258         ATH5K_TRACE(ah->ah_sc);
259         /* Set new station ID */
260         memcpy(common->macaddr, mac, ETH_ALEN);
261
262         pcu_reg = ath5k_hw_reg_read(ah, AR5K_STA_ID1) & 0xffff0000;
263
264         low_id = get_unaligned_le32(mac);
265         high_id = get_unaligned_le16(mac + 4);
266
267         ath5k_hw_reg_write(ah, low_id, AR5K_STA_ID0);
268         ath5k_hw_reg_write(ah, pcu_reg | high_id, AR5K_STA_ID1);
269
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  */
282 void ath5k_hw_set_associd(struct ath5k_hw *ah, const u8 *bssid, u16 assoc_id)
283 {
284         struct ath_common *common = ath5k_hw_common(ah);
285         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) {
292                 ath5k_hw_reg_write(ah, get_unaligned_le32(common->bssidmask),
293                                    AR_BSSMSKL);
294                 ath5k_hw_reg_write(ah,
295                                    get_unaligned_le16(common->bssidmask + 4),
296                                    AR_BSSMSKU);
297         }
298
299         /*
300          * Set BSSID which triggers the "SME Join" operation
301          */
302         low_id = get_unaligned_le32(bssid);
303         high_id = get_unaligned_le16(bssid);
304         ath5k_hw_reg_write(ah, low_id, AR_BSSMSKL);
305         ath5k_hw_reg_write(ah, high_id | ((assoc_id & 0x3fff) <<
306                                 AR5K_BSS_ID1_AID_S), AR_BSSMSKU);
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 void ath5k_hw_set_bssid_mask(struct ath5k_hw *ah, const u8 *mask)
320 {
321         struct ath_common *common = ath5k_hw_common(ah);
322         ATH5K_TRACE(ah->ah_sc);
323
324         /* Cache bssid mask so that we can restore it
325          * on reset */
326         memcpy(common->bssidmask, mask, ETH_ALEN);
327         if (ah->ah_version == AR5K_AR5212)
328                 ath_hw_setbssidmask(common);
329 }
330
331 /************\
332 * RX Control *
333 \************/
334
335 /**
336  * ath5k_hw_start_rx_pcu - Start RX engine
337  *
338  * @ah: The &struct ath5k_hw
339  *
340  * Starts RX engine on PCU so that hw can process RXed frames
341  * (ACK etc).
342  *
343  * NOTE: RX DMA should be already enabled using ath5k_hw_start_rx_dma
344  * TODO: Init ANI here
345  */
346 void ath5k_hw_start_rx_pcu(struct ath5k_hw *ah)
347 {
348         ATH5K_TRACE(ah->ah_sc);
349         AR5K_REG_DISABLE_BITS(ah, AR5K_DIAG_SW, AR5K_DIAG_SW_DIS_RX);
350 }
351
352 /**
353  * at5k_hw_stop_rx_pcu - Stop RX engine
354  *
355  * @ah: The &struct ath5k_hw
356  *
357  * Stops RX engine on PCU
358  *
359  * TODO: Detach ANI here
360  */
361 void ath5k_hw_stop_rx_pcu(struct ath5k_hw *ah)
362 {
363         ATH5K_TRACE(ah->ah_sc);
364         AR5K_REG_ENABLE_BITS(ah, AR5K_DIAG_SW, AR5K_DIAG_SW_DIS_RX);
365 }
366
367 /*
368  * Set multicast filter
369  */
370 void ath5k_hw_set_mcast_filter(struct ath5k_hw *ah, u32 filter0, u32 filter1)
371 {
372         ATH5K_TRACE(ah->ah_sc);
373         /* Set the multicat filter */
374         ath5k_hw_reg_write(ah, filter0, AR5K_MCAST_FILTER0);
375         ath5k_hw_reg_write(ah, filter1, AR5K_MCAST_FILTER1);
376 }
377
378 /*
379  * Set multicast filter by index
380  */
381 int ath5k_hw_set_mcast_filter_idx(struct ath5k_hw *ah, u32 index)
382 {
383
384         ATH5K_TRACE(ah->ah_sc);
385         if (index >= 64)
386                 return -EINVAL;
387         else if (index >= 32)
388                 AR5K_REG_ENABLE_BITS(ah, AR5K_MCAST_FILTER1,
389                                 (1 << (index - 32)));
390         else
391                 AR5K_REG_ENABLE_BITS(ah, AR5K_MCAST_FILTER0, (1 << index));
392
393         return 0;
394 }
395
396 /*
397  * Clear Multicast filter by index
398  */
399 int ath5k_hw_clear_mcast_filter_idx(struct ath5k_hw *ah, u32 index)
400 {
401
402         ATH5K_TRACE(ah->ah_sc);
403         if (index >= 64)
404                 return -EINVAL;
405         else if (index >= 32)
406                 AR5K_REG_DISABLE_BITS(ah, AR5K_MCAST_FILTER1,
407                                 (1 << (index - 32)));
408         else
409                 AR5K_REG_DISABLE_BITS(ah, AR5K_MCAST_FILTER0, (1 << index));
410
411         return 0;
412 }
413
414 /**
415  * ath5k_hw_get_rx_filter - Get current rx filter
416  *
417  * @ah: The &struct ath5k_hw
418  *
419  * Returns the RX filter by reading rx filter and
420  * phy error filter registers. RX filter is used
421  * to set the allowed frame types that PCU will accept
422  * and pass to the driver. For a list of frame types
423  * check out reg.h.
424  */
425 u32 ath5k_hw_get_rx_filter(struct ath5k_hw *ah)
426 {
427         u32 data, filter = 0;
428
429         ATH5K_TRACE(ah->ah_sc);
430         filter = ath5k_hw_reg_read(ah, AR5K_RX_FILTER);
431
432         /*Radar detection for 5212*/
433         if (ah->ah_version == AR5K_AR5212) {
434                 data = ath5k_hw_reg_read(ah, AR5K_PHY_ERR_FIL);
435
436                 if (data & AR5K_PHY_ERR_FIL_RADAR)
437                         filter |= AR5K_RX_FILTER_RADARERR;
438                 if (data & (AR5K_PHY_ERR_FIL_OFDM | AR5K_PHY_ERR_FIL_CCK))
439                         filter |= AR5K_RX_FILTER_PHYERR;
440         }
441
442         return filter;
443 }
444
445 /**
446  * ath5k_hw_set_rx_filter - Set rx filter
447  *
448  * @ah: The &struct ath5k_hw
449  * @filter: RX filter mask (see reg.h)
450  *
451  * Sets RX filter register and also handles PHY error filter
452  * register on 5212 and newer chips so that we have proper PHY
453  * error reporting.
454  */
455 void ath5k_hw_set_rx_filter(struct ath5k_hw *ah, u32 filter)
456 {
457         u32 data = 0;
458
459         ATH5K_TRACE(ah->ah_sc);
460
461         /* Set PHY error filter register on 5212*/
462         if (ah->ah_version == AR5K_AR5212) {
463                 if (filter & AR5K_RX_FILTER_RADARERR)
464                         data |= AR5K_PHY_ERR_FIL_RADAR;
465                 if (filter & AR5K_RX_FILTER_PHYERR)
466                         data |= AR5K_PHY_ERR_FIL_OFDM | AR5K_PHY_ERR_FIL_CCK;
467         }
468
469         /*
470          * The AR5210 uses promiscous mode to detect radar activity
471          */
472         if (ah->ah_version == AR5K_AR5210 &&
473                         (filter & AR5K_RX_FILTER_RADARERR)) {
474                 filter &= ~AR5K_RX_FILTER_RADARERR;
475                 filter |= AR5K_RX_FILTER_PROM;
476         }
477
478         /*Zero length DMA (phy error reporting) */
479         if (data)
480                 AR5K_REG_ENABLE_BITS(ah, AR5K_RXCFG, AR5K_RXCFG_ZLFDMA);
481         else
482                 AR5K_REG_DISABLE_BITS(ah, AR5K_RXCFG, AR5K_RXCFG_ZLFDMA);
483
484         /*Write RX Filter register*/
485         ath5k_hw_reg_write(ah, filter & 0xff, AR5K_RX_FILTER);
486
487         /*Write PHY error filter register on 5212*/
488         if (ah->ah_version == AR5K_AR5212)
489                 ath5k_hw_reg_write(ah, data, AR5K_PHY_ERR_FIL);
490
491 }
492
493
494 /****************\
495 * Beacon control *
496 \****************/
497
498 /**
499  * ath5k_hw_get_tsf32 - Get a 32bit TSF
500  *
501  * @ah: The &struct ath5k_hw
502  *
503  * Returns lower 32 bits of current TSF
504  */
505 u32 ath5k_hw_get_tsf32(struct ath5k_hw *ah)
506 {
507         ATH5K_TRACE(ah->ah_sc);
508         return ath5k_hw_reg_read(ah, AR5K_TSF_L32);
509 }
510
511 /**
512  * ath5k_hw_get_tsf64 - Get the full 64bit TSF
513  *
514  * @ah: The &struct ath5k_hw
515  *
516  * Returns the current TSF
517  */
518 u64 ath5k_hw_get_tsf64(struct ath5k_hw *ah)
519 {
520         u64 tsf = ath5k_hw_reg_read(ah, AR5K_TSF_U32);
521         ATH5K_TRACE(ah->ah_sc);
522
523         return ath5k_hw_reg_read(ah, AR5K_TSF_L32) | (tsf << 32);
524 }
525
526 /**
527  * ath5k_hw_set_tsf64 - Set a new 64bit TSF
528  *
529  * @ah: The &struct ath5k_hw
530  * @tsf64: The new 64bit TSF
531  *
532  * Sets the new TSF
533  */
534 void ath5k_hw_set_tsf64(struct ath5k_hw *ah, u64 tsf64)
535 {
536         ATH5K_TRACE(ah->ah_sc);
537
538         ath5k_hw_reg_write(ah, tsf64 & 0xffffffff, AR5K_TSF_L32);
539         ath5k_hw_reg_write(ah, (tsf64 >> 32) & 0xffffffff, AR5K_TSF_U32);
540 }
541
542 /**
543  * ath5k_hw_reset_tsf - Force a TSF reset
544  *
545  * @ah: The &struct ath5k_hw
546  *
547  * Forces a TSF reset on PCU
548  */
549 void ath5k_hw_reset_tsf(struct ath5k_hw *ah)
550 {
551         u32 val;
552
553         ATH5K_TRACE(ah->ah_sc);
554
555         val = ath5k_hw_reg_read(ah, AR5K_BEACON) | AR5K_BEACON_RESET_TSF;
556
557         /*
558          * Each write to the RESET_TSF bit toggles a hardware internal
559          * signal to reset TSF, but if left high it will cause a TSF reset
560          * on the next chip reset as well.  Thus we always write the value
561          * twice to clear the signal.
562          */
563         ath5k_hw_reg_write(ah, val, AR5K_BEACON);
564         ath5k_hw_reg_write(ah, val, AR5K_BEACON);
565 }
566
567 /*
568  * Initialize beacon timers
569  */
570 void ath5k_hw_init_beacon(struct ath5k_hw *ah, u32 next_beacon, u32 interval)
571 {
572         u32 timer1, timer2, timer3;
573
574         ATH5K_TRACE(ah->ah_sc);
575         /*
576          * Set the additional timers by mode
577          */
578         switch (ah->ah_op_mode) {
579         case NL80211_IFTYPE_MONITOR:
580         case NL80211_IFTYPE_STATION:
581                 /* In STA mode timer1 is used as next wakeup
582                  * timer and timer2 as next CFP duration start
583                  * timer. Both in 1/8TUs. */
584                 /* TODO: PCF handling */
585                 if (ah->ah_version == AR5K_AR5210) {
586                         timer1 = 0xffffffff;
587                         timer2 = 0xffffffff;
588                 } else {
589                         timer1 = 0x0000ffff;
590                         timer2 = 0x0007ffff;
591                 }
592                 /* Mark associated AP as PCF incapable for now */
593                 AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1, AR5K_STA_ID1_PCF);
594                 break;
595         case NL80211_IFTYPE_ADHOC:
596                 AR5K_REG_ENABLE_BITS(ah, AR5K_TXCFG, AR5K_TXCFG_ADHOC_BCN_ATIM);
597         default:
598                 /* On non-STA modes timer1 is used as next DMA
599                  * beacon alert (DBA) timer and timer2 as next
600                  * software beacon alert. Both in 1/8TUs. */
601                 timer1 = (next_beacon - AR5K_TUNE_DMA_BEACON_RESP) << 3;
602                 timer2 = (next_beacon - AR5K_TUNE_SW_BEACON_RESP) << 3;
603                 break;
604         }
605
606         /* Timer3 marks the end of our ATIM window
607          * a zero length window is not allowed because
608          * we 'll get no beacons */
609         timer3 = next_beacon + (ah->ah_atim_window ? ah->ah_atim_window : 1);
610
611         /*
612          * Set the beacon register and enable all timers.
613          */
614         /* When in AP or Mesh Point mode zero timer0 to start TSF */
615         if (ah->ah_op_mode == NL80211_IFTYPE_AP ||
616             ah->ah_op_mode == NL80211_IFTYPE_MESH_POINT)
617                 ath5k_hw_reg_write(ah, 0, AR5K_TIMER0);
618
619         ath5k_hw_reg_write(ah, next_beacon, AR5K_TIMER0);
620         ath5k_hw_reg_write(ah, timer1, AR5K_TIMER1);
621         ath5k_hw_reg_write(ah, timer2, AR5K_TIMER2);
622         ath5k_hw_reg_write(ah, timer3, AR5K_TIMER3);
623
624         /* Force a TSF reset if requested and enable beacons */
625         if (interval & AR5K_BEACON_RESET_TSF)
626                 ath5k_hw_reset_tsf(ah);
627
628         ath5k_hw_reg_write(ah, interval & (AR5K_BEACON_PERIOD |
629                                         AR5K_BEACON_ENABLE),
630                                                 AR5K_BEACON);
631
632         /* Flush any pending BMISS interrupts on ISR by
633          * performing a clear-on-write operation on PISR
634          * register for the BMISS bit (writing a bit on
635          * ISR togles a reset for that bit and leaves
636          * the rest bits intact) */
637         if (ah->ah_version == AR5K_AR5210)
638                 ath5k_hw_reg_write(ah, AR5K_ISR_BMISS, AR5K_ISR);
639         else
640                 ath5k_hw_reg_write(ah, AR5K_ISR_BMISS, AR5K_PISR);
641
642         /* TODO: Set enchanced sleep registers on AR5212
643          * based on vif->bss_conf params, until then
644          * disable power save reporting.*/
645         AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1, AR5K_STA_ID1_PWR_SV);
646
647 }
648
649 #if 0
650 /*
651  * Set beacon timers
652  */
653 int ath5k_hw_set_beacon_timers(struct ath5k_hw *ah,
654                 const struct ath5k_beacon_state *state)
655 {
656         u32 cfp_period, next_cfp, dtim, interval, next_beacon;
657
658         /*
659          * TODO: should be changed through *state
660          * review struct ath5k_beacon_state struct
661          *
662          * XXX: These are used for cfp period bellow, are they
663          * ok ? Is it O.K. for tsf here to be 0 or should we use
664          * get_tsf ?
665          */
666         u32 dtim_count = 0; /* XXX */
667         u32 cfp_count = 0; /* XXX */
668         u32 tsf = 0; /* XXX */
669
670         ATH5K_TRACE(ah->ah_sc);
671         /* Return on an invalid beacon state */
672         if (state->bs_interval < 1)
673                 return -EINVAL;
674
675         interval = state->bs_interval;
676         dtim = state->bs_dtim_period;
677
678         /*
679          * PCF support?
680          */
681         if (state->bs_cfp_period > 0) {
682                 /*
683                  * Enable PCF mode and set the CFP
684                  * (Contention Free Period) and timer registers
685                  */
686                 cfp_period = state->bs_cfp_period * state->bs_dtim_period *
687                         state->bs_interval;
688                 next_cfp = (cfp_count * state->bs_dtim_period + dtim_count) *
689                         state->bs_interval;
690
691                 AR5K_REG_ENABLE_BITS(ah, AR5K_STA_ID1,
692                                 AR5K_STA_ID1_DEFAULT_ANTENNA |
693                                 AR5K_STA_ID1_PCF);
694                 ath5k_hw_reg_write(ah, cfp_period, AR5K_CFP_PERIOD);
695                 ath5k_hw_reg_write(ah, state->bs_cfp_max_duration,
696                                 AR5K_CFP_DUR);
697                 ath5k_hw_reg_write(ah, (tsf + (next_cfp == 0 ? cfp_period :
698                                                 next_cfp)) << 3, AR5K_TIMER2);
699         } else {
700                 /* Disable PCF mode */
701                 AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1,
702                                 AR5K_STA_ID1_DEFAULT_ANTENNA |
703                                 AR5K_STA_ID1_PCF);
704         }
705
706         /*
707          * Enable the beacon timer register
708          */
709         ath5k_hw_reg_write(ah, state->bs_next_beacon, AR5K_TIMER0);
710
711         /*
712          * Start the beacon timers
713          */
714         ath5k_hw_reg_write(ah, (ath5k_hw_reg_read(ah, AR5K_BEACON) &
715                 ~(AR5K_BEACON_PERIOD | AR5K_BEACON_TIM)) |
716                 AR5K_REG_SM(state->bs_tim_offset ? state->bs_tim_offset + 4 : 0,
717                 AR5K_BEACON_TIM) | AR5K_REG_SM(state->bs_interval,
718                 AR5K_BEACON_PERIOD), AR5K_BEACON);
719
720         /*
721          * Write new beacon miss threshold, if it appears to be valid
722          * XXX: Figure out right values for min <= bs_bmiss_threshold <= max
723          * and return if its not in range. We can test this by reading value and
724          * setting value to a largest value and seeing which values register.
725          */
726
727         AR5K_REG_WRITE_BITS(ah, AR5K_RSSI_THR, AR5K_RSSI_THR_BMISS,
728                         state->bs_bmiss_threshold);
729
730         /*
731          * Set sleep control register
732          * XXX: Didn't find this in 5210 code but since this register
733          * exists also in ar5k's 5210 headers i leave it as common code.
734          */
735         AR5K_REG_WRITE_BITS(ah, AR5K_SLEEP_CTL, AR5K_SLEEP_CTL_SLDUR,
736                         (state->bs_sleep_duration - 3) << 3);
737
738         /*
739          * Set enhanced sleep registers on 5212
740          */
741         if (ah->ah_version == AR5K_AR5212) {
742                 if (state->bs_sleep_duration > state->bs_interval &&
743                                 roundup(state->bs_sleep_duration, interval) ==
744                                 state->bs_sleep_duration)
745                         interval = state->bs_sleep_duration;
746
747                 if (state->bs_sleep_duration > dtim && (dtim == 0 ||
748                                 roundup(state->bs_sleep_duration, dtim) ==
749                                 state->bs_sleep_duration))
750                         dtim = state->bs_sleep_duration;
751
752                 if (interval > dtim)
753                         return -EINVAL;
754
755                 next_beacon = interval == dtim ? state->bs_next_dtim :
756                         state->bs_next_beacon;
757
758                 ath5k_hw_reg_write(ah,
759                         AR5K_REG_SM((state->bs_next_dtim - 3) << 3,
760                         AR5K_SLEEP0_NEXT_DTIM) |
761                         AR5K_REG_SM(10, AR5K_SLEEP0_CABTO) |
762                         AR5K_SLEEP0_ENH_SLEEP_EN |
763                         AR5K_SLEEP0_ASSUME_DTIM, AR5K_SLEEP0);
764
765                 ath5k_hw_reg_write(ah, AR5K_REG_SM((next_beacon - 3) << 3,
766                         AR5K_SLEEP1_NEXT_TIM) |
767                         AR5K_REG_SM(10, AR5K_SLEEP1_BEACON_TO), AR5K_SLEEP1);
768
769                 ath5k_hw_reg_write(ah,
770                         AR5K_REG_SM(interval, AR5K_SLEEP2_TIM_PER) |
771                         AR5K_REG_SM(dtim, AR5K_SLEEP2_DTIM_PER), AR5K_SLEEP2);
772         }
773
774         return 0;
775 }
776
777 /*
778  * Reset beacon timers
779  */
780 void ath5k_hw_reset_beacon(struct ath5k_hw *ah)
781 {
782         ATH5K_TRACE(ah->ah_sc);
783         /*
784          * Disable beacon timer
785          */
786         ath5k_hw_reg_write(ah, 0, AR5K_TIMER0);
787
788         /*
789          * Disable some beacon register values
790          */
791         AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1,
792                         AR5K_STA_ID1_DEFAULT_ANTENNA | AR5K_STA_ID1_PCF);
793         ath5k_hw_reg_write(ah, AR5K_BEACON_PERIOD, AR5K_BEACON);
794 }
795
796 /*
797  * Wait for beacon queue to finish
798  */
799 int ath5k_hw_beaconq_finish(struct ath5k_hw *ah, unsigned long phys_addr)
800 {
801         unsigned int i;
802         int ret;
803
804         ATH5K_TRACE(ah->ah_sc);
805
806         /* 5210 doesn't have QCU*/
807         if (ah->ah_version == AR5K_AR5210) {
808                 /*
809                  * Wait for beaconn queue to finish by checking
810                  * Control Register and Beacon Status Register.
811                  */
812                 for (i = AR5K_TUNE_BEACON_INTERVAL / 2; i > 0; i--) {
813                         if (!(ath5k_hw_reg_read(ah, AR5K_BSR) & AR5K_BSR_TXQ1F)
814                                         ||
815                             !(ath5k_hw_reg_read(ah, AR5K_CR) & AR5K_BSR_TXQ1F))
816                                 break;
817                         udelay(10);
818                 }
819
820                 /* Timeout... */
821                 if (i <= 0) {
822                         /*
823                          * Re-schedule the beacon queue
824                          */
825                         ath5k_hw_reg_write(ah, phys_addr, AR5K_NOQCU_TXDP1);
826                         ath5k_hw_reg_write(ah, AR5K_BCR_TQ1V | AR5K_BCR_BDMAE,
827                                         AR5K_BCR);
828
829                         return -EIO;
830                 }
831                 ret = 0;
832         } else {
833         /*5211/5212*/
834                 ret = ath5k_hw_register_timeout(ah,
835                         AR5K_QUEUE_STATUS(AR5K_TX_QUEUE_ID_BEACON),
836                         AR5K_QCU_STS_FRMPENDCNT, 0, false);
837
838                 if (AR5K_REG_READ_Q(ah, AR5K_QCU_TXE, AR5K_TX_QUEUE_ID_BEACON))
839                         return -EIO;
840         }
841
842         return ret;
843 }
844 #endif
845
846
847 /*********************\
848 * Key table functions *
849 \*********************/
850
851 /*
852  * Reset a key entry on the table
853  */
854 int ath5k_hw_reset_key(struct ath5k_hw *ah, u16 entry)
855 {
856         unsigned int i, type;
857         u16 micentry = entry + AR5K_KEYTABLE_MIC_OFFSET;
858
859         ATH5K_TRACE(ah->ah_sc);
860         AR5K_ASSERT_ENTRY(entry, AR5K_KEYTABLE_SIZE);
861
862         type = ath5k_hw_reg_read(ah, AR5K_KEYTABLE_TYPE(entry));
863
864         for (i = 0; i < AR5K_KEYCACHE_SIZE; i++)
865                 ath5k_hw_reg_write(ah, 0, AR5K_KEYTABLE_OFF(entry, i));
866
867         /* Reset associated MIC entry if TKIP
868          * is enabled located at offset (entry + 64) */
869         if (type == AR5K_KEYTABLE_TYPE_TKIP) {
870                 AR5K_ASSERT_ENTRY(micentry, AR5K_KEYTABLE_SIZE);
871                 for (i = 0; i < AR5K_KEYCACHE_SIZE / 2 ; i++)
872                         ath5k_hw_reg_write(ah, 0,
873                                 AR5K_KEYTABLE_OFF(micentry, i));
874         }
875
876         /*
877          * Set NULL encryption on AR5212+
878          *
879          * Note: AR5K_KEYTABLE_TYPE -> AR5K_KEYTABLE_OFF(entry, 5)
880          *       AR5K_KEYTABLE_TYPE_NULL -> 0x00000007
881          *
882          * Note2: Windows driver (ndiswrapper) sets this to
883          *        0x00000714 instead of 0x00000007
884          */
885         if (ah->ah_version >= AR5K_AR5211) {
886                 ath5k_hw_reg_write(ah, AR5K_KEYTABLE_TYPE_NULL,
887                                 AR5K_KEYTABLE_TYPE(entry));
888
889                 if (type == AR5K_KEYTABLE_TYPE_TKIP) {
890                         ath5k_hw_reg_write(ah, AR5K_KEYTABLE_TYPE_NULL,
891                                 AR5K_KEYTABLE_TYPE(micentry));
892                 }
893         }
894
895         return 0;
896 }
897
898 /*
899  * Check if a table entry is valid
900  */
901 int ath5k_hw_is_key_valid(struct ath5k_hw *ah, u16 entry)
902 {
903         ATH5K_TRACE(ah->ah_sc);
904         AR5K_ASSERT_ENTRY(entry, AR5K_KEYTABLE_SIZE);
905
906         /* Check the validation flag at the end of the entry */
907         return ath5k_hw_reg_read(ah, AR5K_KEYTABLE_MAC1(entry)) &
908                 AR5K_KEYTABLE_VALID;
909 }
910
911 static
912 int ath5k_keycache_type(const struct ieee80211_key_conf *key)
913 {
914         switch (key->alg) {
915         case ALG_TKIP:
916                 return AR5K_KEYTABLE_TYPE_TKIP;
917         case ALG_CCMP:
918                 return AR5K_KEYTABLE_TYPE_CCM;
919         case ALG_WEP:
920                 if (key->keylen == WLAN_KEY_LEN_WEP40)
921                         return AR5K_KEYTABLE_TYPE_40;
922                 else if (key->keylen == WLAN_KEY_LEN_WEP104)
923                         return AR5K_KEYTABLE_TYPE_104;
924                 return -EINVAL;
925         default:
926                 return -EINVAL;
927         }
928         return -EINVAL;
929 }
930
931 /*
932  * Set a key entry on the table
933  */
934 int ath5k_hw_set_key(struct ath5k_hw *ah, u16 entry,
935                 const struct ieee80211_key_conf *key, const u8 *mac)
936 {
937         unsigned int i;
938         int keylen;
939         __le32 key_v[5] = {};
940         __le32 key0 = 0, key1 = 0;
941         __le32 *rxmic, *txmic;
942         int keytype;
943         u16 micentry = entry + AR5K_KEYTABLE_MIC_OFFSET;
944         bool is_tkip;
945         const u8 *key_ptr;
946
947         ATH5K_TRACE(ah->ah_sc);
948
949         is_tkip = (key->alg == ALG_TKIP);
950
951         /*
952          * key->keylen comes in from mac80211 in bytes.
953          * TKIP is 128 bit + 128 bit mic
954          */
955         keylen = (is_tkip) ? (128 / 8) : key->keylen;
956
957         if (entry > AR5K_KEYTABLE_SIZE ||
958                 (is_tkip && micentry > AR5K_KEYTABLE_SIZE))
959                 return -EOPNOTSUPP;
960
961         if (unlikely(keylen > 16))
962                 return -EOPNOTSUPP;
963
964         keytype = ath5k_keycache_type(key);
965         if (keytype < 0)
966                 return keytype;
967
968         /*
969          * each key block is 6 bytes wide, written as pairs of
970          * alternating 32 and 16 bit le values.
971          */
972         key_ptr = key->key;
973         for (i = 0; keylen >= 6; keylen -= 6) {
974                 memcpy(&key_v[i], key_ptr, 6);
975                 i += 2;
976                 key_ptr += 6;
977         }
978         if (keylen)
979                 memcpy(&key_v[i], key_ptr, keylen);
980
981         /* intentionally corrupt key until mic is installed */
982         if (is_tkip) {
983                 key0 = key_v[0] = ~key_v[0];
984                 key1 = key_v[1] = ~key_v[1];
985         }
986
987         for (i = 0; i < ARRAY_SIZE(key_v); i++)
988                 ath5k_hw_reg_write(ah, le32_to_cpu(key_v[i]),
989                                 AR5K_KEYTABLE_OFF(entry, i));
990
991         ath5k_hw_reg_write(ah, keytype, AR5K_KEYTABLE_TYPE(entry));
992
993         if (is_tkip) {
994                 /* Install rx/tx MIC */
995                 rxmic = (__le32 *) &key->key[16];
996                 txmic = (__le32 *) &key->key[24];
997
998                 if (ah->ah_combined_mic) {
999                         key_v[0] = rxmic[0];
1000                         key_v[1] = cpu_to_le32(le32_to_cpu(txmic[0]) >> 16);
1001                         key_v[2] = rxmic[1];
1002                         key_v[3] = cpu_to_le32(le32_to_cpu(txmic[0]) & 0xffff);
1003                         key_v[4] = txmic[1];
1004                 } else {
1005                         key_v[0] = rxmic[0];
1006                         key_v[1] = 0;
1007                         key_v[2] = rxmic[1];
1008                         key_v[3] = 0;
1009                         key_v[4] = 0;
1010                 }
1011                 for (i = 0; i < ARRAY_SIZE(key_v); i++)
1012                         ath5k_hw_reg_write(ah, le32_to_cpu(key_v[i]),
1013                                 AR5K_KEYTABLE_OFF(micentry, i));
1014
1015                 ath5k_hw_reg_write(ah, AR5K_KEYTABLE_TYPE_NULL,
1016                         AR5K_KEYTABLE_TYPE(micentry));
1017                 ath5k_hw_reg_write(ah, 0, AR5K_KEYTABLE_MAC0(micentry));
1018                 ath5k_hw_reg_write(ah, 0, AR5K_KEYTABLE_MAC1(micentry));
1019
1020                 /* restore first 2 words of key */
1021                 ath5k_hw_reg_write(ah, le32_to_cpu(~key0),
1022                         AR5K_KEYTABLE_OFF(entry, 0));
1023                 ath5k_hw_reg_write(ah, le32_to_cpu(~key1),
1024                         AR5K_KEYTABLE_OFF(entry, 1));
1025         }
1026
1027         return ath5k_hw_set_key_lladdr(ah, entry, mac);
1028 }
1029
1030 int ath5k_hw_set_key_lladdr(struct ath5k_hw *ah, u16 entry, const u8 *mac)
1031 {
1032         u32 low_id, high_id;
1033
1034         ATH5K_TRACE(ah->ah_sc);
1035          /* Invalid entry (key table overflow) */
1036         AR5K_ASSERT_ENTRY(entry, AR5K_KEYTABLE_SIZE);
1037
1038         /*
1039          * MAC may be NULL if it's a broadcast key. In this case no need to
1040          * to compute get_unaligned_le32 and get_unaligned_le16 as we
1041          * already know it.
1042          */
1043         if (!mac) {
1044                 low_id = 0xffffffff;
1045                 high_id = 0xffff | AR5K_KEYTABLE_VALID;
1046         } else {
1047                 low_id = get_unaligned_le32(mac);
1048                 high_id = get_unaligned_le16(mac + 4) | AR5K_KEYTABLE_VALID;
1049         }
1050
1051         ath5k_hw_reg_write(ah, low_id, AR5K_KEYTABLE_MAC0(entry));
1052         ath5k_hw_reg_write(ah, high_id, AR5K_KEYTABLE_MAC1(entry));
1053
1054         return 0;
1055 }
1056