ath5k: Update PCU code
[safe/jmp/linux-2.6] / drivers / net / wireless / 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 "ath5k.h"
28 #include "reg.h"
29 #include "debug.h"
30 #include "base.h"
31
32 /*******************\
33 * Generic functions *
34 \*******************/
35
36 /**
37  * ath5k_hw_set_opmode - Set PCU operating mode
38  *
39  * @ah: The &struct ath5k_hw
40  *
41  * Initialize PCU for the various operating modes (AP/STA etc)
42  *
43  * NOTE: ah->ah_op_mode must be set before calling this.
44  */
45 int ath5k_hw_set_opmode(struct ath5k_hw *ah)
46 {
47         u32 pcu_reg, beacon_reg, low_id, high_id;
48
49
50         /* Preserve rest settings */
51         pcu_reg = ath5k_hw_reg_read(ah, AR5K_STA_ID1) & 0xffff0000;
52         pcu_reg &= ~(AR5K_STA_ID1_ADHOC | AR5K_STA_ID1_AP
53                         | AR5K_STA_ID1_KEYSRCH_MODE
54                         | (ah->ah_version == AR5K_AR5210 ?
55                         (AR5K_STA_ID1_PWR_SV | AR5K_STA_ID1_NO_PSPOLL) : 0));
56
57         beacon_reg = 0;
58
59         ATH5K_TRACE(ah->ah_sc);
60
61         switch (ah->ah_op_mode) {
62         case NL80211_IFTYPE_ADHOC:
63                 pcu_reg |= AR5K_STA_ID1_ADHOC | AR5K_STA_ID1_KEYSRCH_MODE;
64                 beacon_reg |= AR5K_BCR_ADHOC;
65                 if (ah->ah_version == AR5K_AR5210)
66                         pcu_reg |= AR5K_STA_ID1_NO_PSPOLL;
67                 else
68                         AR5K_REG_DISABLE_BITS(ah, AR5K_CFG, AR5K_CFG_ADHOC);
69                 break;
70
71         case NL80211_IFTYPE_AP:
72         case NL80211_IFTYPE_MESH_POINT:
73                 pcu_reg |= AR5K_STA_ID1_AP | AR5K_STA_ID1_KEYSRCH_MODE;
74                 beacon_reg |= AR5K_BCR_AP;
75                 if (ah->ah_version == AR5K_AR5210)
76                         pcu_reg |= AR5K_STA_ID1_NO_PSPOLL;
77                 else
78                         AR5K_REG_ENABLE_BITS(ah, AR5K_CFG, AR5K_CFG_ADHOC);
79                 break;
80
81         case NL80211_IFTYPE_STATION:
82                 pcu_reg |= AR5K_STA_ID1_KEYSRCH_MODE
83                         | (ah->ah_version == AR5K_AR5210 ?
84                                 AR5K_STA_ID1_PWR_SV : 0);
85         case NL80211_IFTYPE_MONITOR:
86                 pcu_reg |= AR5K_STA_ID1_KEYSRCH_MODE
87                         | (ah->ah_version == AR5K_AR5210 ?
88                                 AR5K_STA_ID1_NO_PSPOLL : 0);
89                 break;
90
91         default:
92                 return -EINVAL;
93         }
94
95         /*
96          * Set PCU registers
97          */
98         low_id = AR5K_LOW_ID(ah->ah_sta_id);
99         high_id = AR5K_HIGH_ID(ah->ah_sta_id);
100         ath5k_hw_reg_write(ah, low_id, AR5K_STA_ID0);
101         ath5k_hw_reg_write(ah, pcu_reg | high_id, AR5K_STA_ID1);
102
103         /*
104          * Set Beacon Control Register on 5210
105          */
106         if (ah->ah_version == AR5K_AR5210)
107                 ath5k_hw_reg_write(ah, beacon_reg, AR5K_BCR);
108
109         return 0;
110 }
111
112 /**
113  * ath5k_hw_update - Update mib counters (mac layer statistics)
114  *
115  * @ah: The &struct ath5k_hw
116  * @stats: The &struct ieee80211_low_level_stats we use to track
117  * statistics on the driver
118  *
119  * Reads MIB counters from PCU and updates sw statistics. Must be
120  * called after a MIB interrupt.
121  */
122 void ath5k_hw_update_mib_counters(struct ath5k_hw *ah,
123                 struct ieee80211_low_level_stats  *stats)
124 {
125         ATH5K_TRACE(ah->ah_sc);
126
127         /* Read-And-Clear */
128         stats->dot11ACKFailureCount += ath5k_hw_reg_read(ah, AR5K_ACK_FAIL);
129         stats->dot11RTSFailureCount += ath5k_hw_reg_read(ah, AR5K_RTS_FAIL);
130         stats->dot11RTSSuccessCount += ath5k_hw_reg_read(ah, AR5K_RTS_OK);
131         stats->dot11FCSErrorCount += ath5k_hw_reg_read(ah, AR5K_FCS_FAIL);
132
133         /* XXX: Should we use this to track beacon count ?
134          * -we read it anyway to clear the register */
135         ath5k_hw_reg_read(ah, AR5K_BEACON_CNT);
136
137         /* Reset profile count registers on 5212*/
138         if (ah->ah_version == AR5K_AR5212) {
139                 ath5k_hw_reg_write(ah, 0, AR5K_PROFCNT_TX);
140                 ath5k_hw_reg_write(ah, 0, AR5K_PROFCNT_RX);
141                 ath5k_hw_reg_write(ah, 0, AR5K_PROFCNT_RXCLR);
142                 ath5k_hw_reg_write(ah, 0, AR5K_PROFCNT_CYCLE);
143         }
144
145         /* TODO: Handle ANI stats */
146 }
147
148 /**
149  * ath5k_hw_set_ack_bitrate - set bitrate for ACKs
150  *
151  * @ah: The &struct ath5k_hw
152  * @high: Flag to determine if we want to use high transmition rate
153  * for ACKs or not
154  *
155  * If high flag is set, we tell hw to use a set of control rates based on
156  * the current transmition rate (check out control_rates array inside reset.c).
157  * If not hw just uses the lowest rate available for the current modulation
158  * scheme being used (1Mbit for CCK and 6Mbits for OFDM).
159  */
160 void ath5k_hw_set_ack_bitrate_high(struct ath5k_hw *ah, bool high)
161 {
162         if (ah->ah_version != AR5K_AR5212)
163                 return;
164         else {
165                 u32 val = AR5K_STA_ID1_BASE_RATE_11B | AR5K_STA_ID1_ACKCTS_6MB;
166                 if (high)
167                         AR5K_REG_ENABLE_BITS(ah, AR5K_STA_ID1, val);
168                 else
169                         AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1, val);
170         }
171 }
172
173
174 /******************\
175 * ACK/CTS Timeouts *
176 \******************/
177
178 /**
179  * ath5k_hw_het_ack_timeout - Get ACK timeout from PCU in usec
180  *
181  * @ah: The &struct ath5k_hw
182  */
183 unsigned int ath5k_hw_get_ack_timeout(struct ath5k_hw *ah)
184 {
185         ATH5K_TRACE(ah->ah_sc);
186
187         return ath5k_hw_clocktoh(AR5K_REG_MS(ath5k_hw_reg_read(ah,
188                         AR5K_TIME_OUT), AR5K_TIME_OUT_ACK), ah->ah_turbo);
189 }
190
191 /**
192  * ath5k_hw_set_ack_timeout - Set ACK timeout on PCU
193  *
194  * @ah: The &struct ath5k_hw
195  * @timeout: Timeout in usec
196  */
197 int ath5k_hw_set_ack_timeout(struct ath5k_hw *ah, unsigned int timeout)
198 {
199         ATH5K_TRACE(ah->ah_sc);
200         if (ath5k_hw_clocktoh(AR5K_REG_MS(0xffffffff, AR5K_TIME_OUT_ACK),
201                         ah->ah_turbo) <= timeout)
202                 return -EINVAL;
203
204         AR5K_REG_WRITE_BITS(ah, AR5K_TIME_OUT, AR5K_TIME_OUT_ACK,
205                 ath5k_hw_htoclock(timeout, ah->ah_turbo));
206
207         return 0;
208 }
209
210 /**
211  * ath5k_hw_get_cts_timeout - Get CTS timeout from PCU in usec
212  *
213  * @ah: The &struct ath5k_hw
214  */
215 unsigned int ath5k_hw_get_cts_timeout(struct ath5k_hw *ah)
216 {
217         ATH5K_TRACE(ah->ah_sc);
218         return ath5k_hw_clocktoh(AR5K_REG_MS(ath5k_hw_reg_read(ah,
219                         AR5K_TIME_OUT), AR5K_TIME_OUT_CTS), ah->ah_turbo);
220 }
221
222 /**
223  * ath5k_hw_set_cts_timeout - Set CTS timeout on PCU
224  *
225  * @ah: The &struct ath5k_hw
226  * @timeout: Timeout in usec
227  */
228 int ath5k_hw_set_cts_timeout(struct ath5k_hw *ah, unsigned int timeout)
229 {
230         ATH5K_TRACE(ah->ah_sc);
231         if (ath5k_hw_clocktoh(AR5K_REG_MS(0xffffffff, AR5K_TIME_OUT_CTS),
232                         ah->ah_turbo) <= timeout)
233                 return -EINVAL;
234
235         AR5K_REG_WRITE_BITS(ah, AR5K_TIME_OUT, AR5K_TIME_OUT_CTS,
236                         ath5k_hw_htoclock(timeout, ah->ah_turbo));
237
238         return 0;
239 }
240
241
242 /****************\
243 * BSSID handling *
244 \****************/
245
246 /**
247  * ath5k_hw_get_lladdr - Get station id
248  *
249  * @ah: The &struct ath5k_hw
250  * @mac: The card's mac address
251  *
252  * Initialize ah->ah_sta_id using the mac address provided
253  * (just a memcpy).
254  *
255  * TODO: Remove it once we merge ath5k_softc and ath5k_hw
256  */
257 void ath5k_hw_get_lladdr(struct ath5k_hw *ah, u8 *mac)
258 {
259         ATH5K_TRACE(ah->ah_sc);
260         memcpy(mac, ah->ah_sta_id, ETH_ALEN);
261 }
262
263 /**
264  * ath5k_hw_set_lladdr - Set station id
265  *
266  * @ah: The &struct ath5k_hw
267  * @mac: The card's mac address
268  *
269  * Set station id on hw using the provided mac address
270  *
271  * NOTE: This is only called during attach, don't call it
272  * on reset because it overwrites all AR5K_STA_ID1 settings.
273  * We have set_opmode (above) for reset.
274  */
275 int ath5k_hw_set_lladdr(struct ath5k_hw *ah, const u8 *mac)
276 {
277         u32 low_id, high_id;
278
279         ATH5K_TRACE(ah->ah_sc);
280         /* Set new station ID */
281         memcpy(ah->ah_sta_id, mac, ETH_ALEN);
282
283         low_id = AR5K_LOW_ID(mac);
284         high_id = AR5K_HIGH_ID(mac);
285
286         ath5k_hw_reg_write(ah, low_id, AR5K_STA_ID0);
287         ath5k_hw_reg_write(ah, high_id, AR5K_STA_ID1);
288
289         return 0;
290 }
291
292 /**
293  * ath5k_hw_set_associd - Set BSSID for association
294  *
295  * @ah: The &struct ath5k_hw
296  * @bssid: BSSID
297  * @assoc_id: Assoc id
298  *
299  * Sets the BSSID which trigers the "SME Join" operation
300  */
301 void ath5k_hw_set_associd(struct ath5k_hw *ah, const u8 *bssid, u16 assoc_id)
302 {
303         u32 low_id, high_id;
304         u16 tim_offset = 0;
305
306         /*
307          * Set simple BSSID mask on 5212
308          */
309         if (ah->ah_version == AR5K_AR5212) {
310                 ath5k_hw_reg_write(ah, AR5K_LOW_ID(ah->ah_bssid_mask),
311                                                         AR5K_BSS_IDM0);
312                 ath5k_hw_reg_write(ah, AR5K_HIGH_ID(ah->ah_bssid_mask),
313                                                         AR5K_BSS_IDM1);
314         }
315
316         /*
317          * Set BSSID which triggers the "SME Join" operation
318          */
319         low_id = AR5K_LOW_ID(bssid);
320         high_id = AR5K_HIGH_ID(bssid);
321         ath5k_hw_reg_write(ah, low_id, AR5K_BSS_ID0);
322         ath5k_hw_reg_write(ah, high_id | ((assoc_id & 0x3fff) <<
323                                 AR5K_BSS_ID1_AID_S), AR5K_BSS_ID1);
324
325         if (assoc_id == 0) {
326                 ath5k_hw_disable_pspoll(ah);
327                 return;
328         }
329
330         AR5K_REG_WRITE_BITS(ah, AR5K_BEACON, AR5K_BEACON_TIM,
331                         tim_offset ? tim_offset + 4 : 0);
332
333         ath5k_hw_enable_pspoll(ah, NULL, 0);
334 }
335
336 /**
337  * ath5k_hw_set_bssid_mask - filter out bssids we listen
338  *
339  * @ah: the &struct ath5k_hw
340  * @mask: the bssid_mask, a u8 array of size ETH_ALEN
341  *
342  * BSSID masking is a method used by AR5212 and newer hardware to inform PCU
343  * which bits of the interface's MAC address should be looked at when trying
344  * to decide which packets to ACK. In station mode and AP mode with a single
345  * BSS every bit matters since we lock to only one BSS. In AP mode with
346  * multiple BSSes (virtual interfaces) not every bit matters because hw must
347  * accept frames for all BSSes and so we tweak some bits of our mac address
348  * in order to have multiple BSSes.
349  *
350  * NOTE: This is a simple filter and does *not* filter out all
351  * relevant frames. Some frames that are not for us might get ACKed from us
352  * by PCU because they just match the mask.
353  *
354  * When handling multiple BSSes you can get the BSSID mask by computing the
355  * set of  ~ ( MAC XOR BSSID ) for all bssids we handle.
356  *
357  * When you do this you are essentially computing the common bits of all your
358  * BSSes. Later it is assumed the harware will "and" (&) the BSSID mask with
359  * the MAC address to obtain the relevant bits and compare the result with
360  * (frame's BSSID & mask) to see if they match.
361  */
362 /*
363  * Simple example: on your card you have have two BSSes you have created with
364  * BSSID-01 and BSSID-02. Lets assume BSSID-01 will not use the MAC address.
365  * There is another BSSID-03 but you are not part of it. For simplicity's sake,
366  * assuming only 4 bits for a mac address and for BSSIDs you can then have:
367  *
368  *                  \
369  * MAC:                0001 |
370  * BSSID-01:   0100 | --> Belongs to us
371  * BSSID-02:   1001 |
372  *                  /
373  * -------------------
374  * BSSID-03:   0110  | --> External
375  * -------------------
376  *
377  * Our bssid_mask would then be:
378  *
379  *             On loop iteration for BSSID-01:
380  *             ~(0001 ^ 0100)  -> ~(0101)
381  *                             ->   1010
382  *             bssid_mask      =    1010
383  *
384  *             On loop iteration for BSSID-02:
385  *             bssid_mask &= ~(0001   ^   1001)
386  *             bssid_mask =   (1010)  & ~(0001 ^ 1001)
387  *             bssid_mask =   (1010)  & ~(1001)
388  *             bssid_mask =   (1010)  &  (0110)
389  *             bssid_mask =   0010
390  *
391  * A bssid_mask of 0010 means "only pay attention to the second least
392  * significant bit". This is because its the only bit common
393  * amongst the MAC and all BSSIDs we support. To findout what the real
394  * common bit is we can simply "&" the bssid_mask now with any BSSID we have
395  * or our MAC address (we assume the hardware uses the MAC address).
396  *
397  * Now, suppose there's an incoming frame for BSSID-03:
398  *
399  * IFRAME-01:  0110
400  *
401  * An easy eye-inspeciton of this already should tell you that this frame
402  * will not pass our check. This is beacuse the bssid_mask tells the
403  * hardware to only look at the second least significant bit and the
404  * common bit amongst the MAC and BSSIDs is 0, this frame has the 2nd LSB
405  * as 1, which does not match 0.
406  *
407  * So with IFRAME-01 we *assume* the hardware will do:
408  *
409  *     allow = (IFRAME-01 & bssid_mask) == (bssid_mask & MAC) ? 1 : 0;
410  *  --> allow = (0110 & 0010) == (0010 & 0001) ? 1 : 0;
411  *  --> allow = (0010) == 0000 ? 1 : 0;
412  *  --> allow = 0
413  *
414  *  Lets now test a frame that should work:
415  *
416  * IFRAME-02:  0001 (we should allow)
417  *
418  *     allow = (0001 & 1010) == 1010
419  *
420  *     allow = (IFRAME-02 & bssid_mask) == (bssid_mask & MAC) ? 1 : 0;
421  *  --> allow = (0001 & 0010) ==  (0010 & 0001) ? 1 :0;
422  *  --> allow = (0010) == (0010)
423  *  --> allow = 1
424  *
425  * Other examples:
426  *
427  * IFRAME-03:  0100 --> allowed
428  * IFRAME-04:  1001 --> allowed
429  * IFRAME-05:  1101 --> allowed but its not for us!!!
430  *
431  */
432 int ath5k_hw_set_bssid_mask(struct ath5k_hw *ah, const u8 *mask)
433 {
434         u32 low_id, high_id;
435         ATH5K_TRACE(ah->ah_sc);
436
437         /* Cache bssid mask so that we can restore it
438          * on reset */
439         memcpy(ah->ah_bssid_mask, mask, ETH_ALEN);
440         if (ah->ah_version == AR5K_AR5212) {
441                 low_id = AR5K_LOW_ID(mask);
442                 high_id = AR5K_HIGH_ID(mask);
443
444                 ath5k_hw_reg_write(ah, low_id, AR5K_BSS_IDM0);
445                 ath5k_hw_reg_write(ah, high_id, AR5K_BSS_IDM1);
446
447                 return 0;
448         }
449
450         return -EIO;
451 }
452
453
454 /************\
455 * RX Control *
456 \************/
457
458 /**
459  * ath5k_hw_start_rx_pcu - Start RX engine
460  *
461  * @ah: The &struct ath5k_hw
462  *
463  * Starts RX engine on PCU so that hw can process RXed frames
464  * (ACK etc).
465  *
466  * NOTE: RX DMA should be already enabled using ath5k_hw_start_rx_dma
467  * TODO: Init ANI here
468  */
469 void ath5k_hw_start_rx_pcu(struct ath5k_hw *ah)
470 {
471         ATH5K_TRACE(ah->ah_sc);
472         AR5K_REG_DISABLE_BITS(ah, AR5K_DIAG_SW, AR5K_DIAG_SW_DIS_RX);
473 }
474
475 /**
476  * at5k_hw_stop_rx_pcu - Stop RX engine
477  *
478  * @ah: The &struct ath5k_hw
479  *
480  * Stops RX engine on PCU
481  *
482  * TODO: Detach ANI here
483  */
484 void ath5k_hw_stop_rx_pcu(struct ath5k_hw *ah)
485 {
486         ATH5K_TRACE(ah->ah_sc);
487         AR5K_REG_ENABLE_BITS(ah, AR5K_DIAG_SW, AR5K_DIAG_SW_DIS_RX);
488 }
489
490 /*
491  * Set multicast filter
492  */
493 void ath5k_hw_set_mcast_filter(struct ath5k_hw *ah, u32 filter0, u32 filter1)
494 {
495         ATH5K_TRACE(ah->ah_sc);
496         /* Set the multicat filter */
497         ath5k_hw_reg_write(ah, filter0, AR5K_MCAST_FILTER0);
498         ath5k_hw_reg_write(ah, filter1, AR5K_MCAST_FILTER1);
499 }
500
501 /*
502  * Set multicast filter by index
503  */
504 int ath5k_hw_set_mcast_filter_idx(struct ath5k_hw *ah, u32 index)
505 {
506
507         ATH5K_TRACE(ah->ah_sc);
508         if (index >= 64)
509                 return -EINVAL;
510         else if (index >= 32)
511                 AR5K_REG_ENABLE_BITS(ah, AR5K_MCAST_FILTER1,
512                                 (1 << (index - 32)));
513         else
514                 AR5K_REG_ENABLE_BITS(ah, AR5K_MCAST_FILTER0, (1 << index));
515
516         return 0;
517 }
518
519 /*
520  * Clear Multicast filter by index
521  */
522 int ath5k_hw_clear_mcast_filter_idx(struct ath5k_hw *ah, u32 index)
523 {
524
525         ATH5K_TRACE(ah->ah_sc);
526         if (index >= 64)
527                 return -EINVAL;
528         else if (index >= 32)
529                 AR5K_REG_DISABLE_BITS(ah, AR5K_MCAST_FILTER1,
530                                 (1 << (index - 32)));
531         else
532                 AR5K_REG_DISABLE_BITS(ah, AR5K_MCAST_FILTER0, (1 << index));
533
534         return 0;
535 }
536
537 /**
538  * ath5k_hw_get_rx_filter - Get current rx filter
539  *
540  * @ah: The &struct ath5k_hw
541  *
542  * Returns the RX filter by reading rx filter and
543  * phy error filter registers. RX filter is used
544  * to set the allowed frame types that PCU will accept
545  * and pass to the driver. For a list of frame types
546  * check out reg.h.
547  */
548 u32 ath5k_hw_get_rx_filter(struct ath5k_hw *ah)
549 {
550         u32 data, filter = 0;
551
552         ATH5K_TRACE(ah->ah_sc);
553         filter = ath5k_hw_reg_read(ah, AR5K_RX_FILTER);
554
555         /*Radar detection for 5212*/
556         if (ah->ah_version == AR5K_AR5212) {
557                 data = ath5k_hw_reg_read(ah, AR5K_PHY_ERR_FIL);
558
559                 if (data & AR5K_PHY_ERR_FIL_RADAR)
560                         filter |= AR5K_RX_FILTER_RADARERR;
561                 if (data & (AR5K_PHY_ERR_FIL_OFDM | AR5K_PHY_ERR_FIL_CCK))
562                         filter |= AR5K_RX_FILTER_PHYERR;
563         }
564
565         return filter;
566 }
567
568 /**
569  * ath5k_hw_set_rx_filter - Set rx filter
570  *
571  * @ah: The &struct ath5k_hw
572  * @filter: RX filter mask (see reg.h)
573  *
574  * Sets RX filter register and also handles PHY error filter
575  * register on 5212 and newer chips so that we have proper PHY
576  * error reporting.
577  */
578 void ath5k_hw_set_rx_filter(struct ath5k_hw *ah, u32 filter)
579 {
580         u32 data = 0;
581
582         ATH5K_TRACE(ah->ah_sc);
583
584         /* Set PHY error filter register on 5212*/
585         if (ah->ah_version == AR5K_AR5212) {
586                 if (filter & AR5K_RX_FILTER_RADARERR)
587                         data |= AR5K_PHY_ERR_FIL_RADAR;
588                 if (filter & AR5K_RX_FILTER_PHYERR)
589                         data |= AR5K_PHY_ERR_FIL_OFDM | AR5K_PHY_ERR_FIL_CCK;
590         }
591
592         /*
593          * The AR5210 uses promiscous mode to detect radar activity
594          */
595         if (ah->ah_version == AR5K_AR5210 &&
596                         (filter & AR5K_RX_FILTER_RADARERR)) {
597                 filter &= ~AR5K_RX_FILTER_RADARERR;
598                 filter |= AR5K_RX_FILTER_PROM;
599         }
600
601         /*Zero length DMA (phy error reporting) */
602         if (data)
603                 AR5K_REG_ENABLE_BITS(ah, AR5K_RXCFG, AR5K_RXCFG_ZLFDMA);
604         else
605                 AR5K_REG_DISABLE_BITS(ah, AR5K_RXCFG, AR5K_RXCFG_ZLFDMA);
606
607         /*Write RX Filter register*/
608         ath5k_hw_reg_write(ah, filter & 0xff, AR5K_RX_FILTER);
609
610         /*Write PHY error filter register on 5212*/
611         if (ah->ah_version == AR5K_AR5212)
612                 ath5k_hw_reg_write(ah, data, AR5K_PHY_ERR_FIL);
613
614 }
615
616
617 /****************\
618 * Beacon control *
619 \****************/
620
621 /**
622  * ath5k_hw_get_tsf32 - Get a 32bit TSF
623  *
624  * @ah: The &struct ath5k_hw
625  *
626  * Returns lower 32 bits of current TSF
627  */
628 u32 ath5k_hw_get_tsf32(struct ath5k_hw *ah)
629 {
630         ATH5K_TRACE(ah->ah_sc);
631         return ath5k_hw_reg_read(ah, AR5K_TSF_L32);
632 }
633
634 /**
635  * ath5k_hw_get_tsf64 - Get the full 64bit TSF
636  *
637  * @ah: The &struct ath5k_hw
638  *
639  * Returns the current TSF
640  */
641 u64 ath5k_hw_get_tsf64(struct ath5k_hw *ah)
642 {
643         u64 tsf = ath5k_hw_reg_read(ah, AR5K_TSF_U32);
644         ATH5K_TRACE(ah->ah_sc);
645
646         return ath5k_hw_reg_read(ah, AR5K_TSF_L32) | (tsf << 32);
647 }
648
649 /**
650  * ath5k_hw_reset_tsf - Force a TSF reset
651  *
652  * @ah: The &struct ath5k_hw
653  *
654  * Forces a TSF reset on PCU
655  */
656 void ath5k_hw_reset_tsf(struct ath5k_hw *ah)
657 {
658         u32 val;
659
660         ATH5K_TRACE(ah->ah_sc);
661
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);
672 }
673
674 /*
675  * Initialize beacon timers
676  */
677 void 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) {
686         case NL80211_IFTYPE_MONITOR:
687         case NL80211_IFTYPE_STATION:
688                 /* 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 */
692                 if (ah->ah_version == AR5K_AR5210) {
693                         timer1 = 0xffffffff;
694                         timer2 = 0xffffffff;
695                 } else {
696                         timer1 = 0x0000ffff;
697                         timer2 = 0x0007ffff;
698                 }
699                 /* Mark associated AP as PCF incapable for now */
700                 AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1, AR5K_STA_ID1_PCF);
701                 break;
702         case NL80211_IFTYPE_ADHOC:
703                 AR5K_REG_ENABLE_BITS(ah, AR5K_TXCFG, AR5K_TXCFG_ADHOC_BCN_ATIM);
704         default:
705                 /* 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. */
708                 timer1 = (next_beacon - AR5K_TUNE_DMA_BEACON_RESP) << 3;
709                 timer2 = (next_beacon - AR5K_TUNE_SW_BEACON_RESP) << 3;
710                 break;
711         }
712
713         /* Timer3 marks the end of our ATIM window
714          * a zero length window is not allowed because
715          * we 'll get no beacons */
716         timer3 = next_beacon + (ah->ah_atim_window ? ah->ah_atim_window : 1);
717
718         /*
719          * Set the beacon register and enable all timers.
720          */
721         /* When in AP mode zero timer0 to start TSF */
722         if (ah->ah_op_mode == NL80211_IFTYPE_AP)
723                 ath5k_hw_reg_write(ah, 0, AR5K_TIMER0);
724         else
725                 ath5k_hw_reg_write(ah, next_beacon, AR5K_TIMER0);
726         ath5k_hw_reg_write(ah, timer1, AR5K_TIMER1);
727         ath5k_hw_reg_write(ah, timer2, AR5K_TIMER2);
728         ath5k_hw_reg_write(ah, timer3, AR5K_TIMER3);
729
730         /* Force a TSF reset if requested and enable beacons */
731         if (interval & AR5K_BEACON_RESET_TSF)
732                 ath5k_hw_reset_tsf(ah);
733
734         ath5k_hw_reg_write(ah, interval & (AR5K_BEACON_PERIOD |
735                                         AR5K_BEACON_ENABLE),
736                                                 AR5K_BEACON);
737
738         /* Flush any pending BMISS interrupts on ISR by
739          * performing a clear-on-write operation on PISR
740          * register for the BMISS bit (writing a bit on
741          * ISR togles a reset for that bit and leaves
742          * the rest bits intact) */
743         if (ah->ah_version == AR5K_AR5210)
744                 ath5k_hw_reg_write(ah, AR5K_ISR_BMISS, AR5K_ISR);
745         else
746                 ath5k_hw_reg_write(ah, AR5K_ISR_BMISS, AR5K_PISR);
747
748         /* TODO: Set enchanced sleep registers on AR5212
749          * based on vif->bss_conf params, until then
750          * disable power save reporting.*/
751         AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1, AR5K_STA_ID1_PWR_SV);
752
753 }
754
755 #if 0
756 /*
757  * Set beacon timers
758  */
759 int ath5k_hw_set_beacon_timers(struct ath5k_hw *ah,
760                 const struct ath5k_beacon_state *state)
761 {
762         u32 cfp_period, next_cfp, dtim, interval, next_beacon;
763
764         /*
765          * TODO: should be changed through *state
766          * review struct ath5k_beacon_state struct
767          *
768          * XXX: These are used for cfp period bellow, are they
769          * ok ? Is it O.K. for tsf here to be 0 or should we use
770          * get_tsf ?
771          */
772         u32 dtim_count = 0; /* XXX */
773         u32 cfp_count = 0; /* XXX */
774         u32 tsf = 0; /* XXX */
775
776         ATH5K_TRACE(ah->ah_sc);
777         /* Return on an invalid beacon state */
778         if (state->bs_interval < 1)
779                 return -EINVAL;
780
781         interval = state->bs_interval;
782         dtim = state->bs_dtim_period;
783
784         /*
785          * PCF support?
786          */
787         if (state->bs_cfp_period > 0) {
788                 /*
789                  * Enable PCF mode and set the CFP
790                  * (Contention Free Period) and timer registers
791                  */
792                 cfp_period = state->bs_cfp_period * state->bs_dtim_period *
793                         state->bs_interval;
794                 next_cfp = (cfp_count * state->bs_dtim_period + dtim_count) *
795                         state->bs_interval;
796
797                 AR5K_REG_ENABLE_BITS(ah, AR5K_STA_ID1,
798                                 AR5K_STA_ID1_DEFAULT_ANTENNA |
799                                 AR5K_STA_ID1_PCF);
800                 ath5k_hw_reg_write(ah, cfp_period, AR5K_CFP_PERIOD);
801                 ath5k_hw_reg_write(ah, state->bs_cfp_max_duration,
802                                 AR5K_CFP_DUR);
803                 ath5k_hw_reg_write(ah, (tsf + (next_cfp == 0 ? cfp_period :
804                                                 next_cfp)) << 3, AR5K_TIMER2);
805         } else {
806                 /* Disable PCF mode */
807                 AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1,
808                                 AR5K_STA_ID1_DEFAULT_ANTENNA |
809                                 AR5K_STA_ID1_PCF);
810         }
811
812         /*
813          * Enable the beacon timer register
814          */
815         ath5k_hw_reg_write(ah, state->bs_next_beacon, AR5K_TIMER0);
816
817         /*
818          * Start the beacon timers
819          */
820         ath5k_hw_reg_write(ah, (ath5k_hw_reg_read(ah, AR5K_BEACON) &
821                 ~(AR5K_BEACON_PERIOD | AR5K_BEACON_TIM)) |
822                 AR5K_REG_SM(state->bs_tim_offset ? state->bs_tim_offset + 4 : 0,
823                 AR5K_BEACON_TIM) | AR5K_REG_SM(state->bs_interval,
824                 AR5K_BEACON_PERIOD), AR5K_BEACON);
825
826         /*
827          * Write new beacon miss threshold, if it appears to be valid
828          * XXX: Figure out right values for min <= bs_bmiss_threshold <= max
829          * and return if its not in range. We can test this by reading value and
830          * setting value to a largest value and seeing which values register.
831          */
832
833         AR5K_REG_WRITE_BITS(ah, AR5K_RSSI_THR, AR5K_RSSI_THR_BMISS,
834                         state->bs_bmiss_threshold);
835
836         /*
837          * Set sleep control register
838          * XXX: Didn't find this in 5210 code but since this register
839          * exists also in ar5k's 5210 headers i leave it as common code.
840          */
841         AR5K_REG_WRITE_BITS(ah, AR5K_SLEEP_CTL, AR5K_SLEEP_CTL_SLDUR,
842                         (state->bs_sleep_duration - 3) << 3);
843
844         /*
845          * Set enhanced sleep registers on 5212
846          */
847         if (ah->ah_version == AR5K_AR5212) {
848                 if (state->bs_sleep_duration > state->bs_interval &&
849                                 roundup(state->bs_sleep_duration, interval) ==
850                                 state->bs_sleep_duration)
851                         interval = state->bs_sleep_duration;
852
853                 if (state->bs_sleep_duration > dtim && (dtim == 0 ||
854                                 roundup(state->bs_sleep_duration, dtim) ==
855                                 state->bs_sleep_duration))
856                         dtim = state->bs_sleep_duration;
857
858                 if (interval > dtim)
859                         return -EINVAL;
860
861                 next_beacon = interval == dtim ? state->bs_next_dtim :
862                         state->bs_next_beacon;
863
864                 ath5k_hw_reg_write(ah,
865                         AR5K_REG_SM((state->bs_next_dtim - 3) << 3,
866                         AR5K_SLEEP0_NEXT_DTIM) |
867                         AR5K_REG_SM(10, AR5K_SLEEP0_CABTO) |
868                         AR5K_SLEEP0_ENH_SLEEP_EN |
869                         AR5K_SLEEP0_ASSUME_DTIM, AR5K_SLEEP0);
870
871                 ath5k_hw_reg_write(ah, AR5K_REG_SM((next_beacon - 3) << 3,
872                         AR5K_SLEEP1_NEXT_TIM) |
873                         AR5K_REG_SM(10, AR5K_SLEEP1_BEACON_TO), AR5K_SLEEP1);
874
875                 ath5k_hw_reg_write(ah,
876                         AR5K_REG_SM(interval, AR5K_SLEEP2_TIM_PER) |
877                         AR5K_REG_SM(dtim, AR5K_SLEEP2_DTIM_PER), AR5K_SLEEP2);
878         }
879
880         return 0;
881 }
882
883 /*
884  * Reset beacon timers
885  */
886 void ath5k_hw_reset_beacon(struct ath5k_hw *ah)
887 {
888         ATH5K_TRACE(ah->ah_sc);
889         /*
890          * Disable beacon timer
891          */
892         ath5k_hw_reg_write(ah, 0, AR5K_TIMER0);
893
894         /*
895          * Disable some beacon register values
896          */
897         AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1,
898                         AR5K_STA_ID1_DEFAULT_ANTENNA | AR5K_STA_ID1_PCF);
899         ath5k_hw_reg_write(ah, AR5K_BEACON_PERIOD, AR5K_BEACON);
900 }
901
902 /*
903  * Wait for beacon queue to finish
904  */
905 int ath5k_hw_beaconq_finish(struct ath5k_hw *ah, unsigned long phys_addr)
906 {
907         unsigned int i;
908         int ret;
909
910         ATH5K_TRACE(ah->ah_sc);
911
912         /* 5210 doesn't have QCU*/
913         if (ah->ah_version == AR5K_AR5210) {
914                 /*
915                  * Wait for beaconn queue to finish by checking
916                  * Control Register and Beacon Status Register.
917                  */
918                 for (i = AR5K_TUNE_BEACON_INTERVAL / 2; i > 0; i--) {
919                         if (!(ath5k_hw_reg_read(ah, AR5K_BSR) & AR5K_BSR_TXQ1F)
920                                         ||
921                             !(ath5k_hw_reg_read(ah, AR5K_CR) & AR5K_BSR_TXQ1F))
922                                 break;
923                         udelay(10);
924                 }
925
926                 /* Timeout... */
927                 if (i <= 0) {
928                         /*
929                          * Re-schedule the beacon queue
930                          */
931                         ath5k_hw_reg_write(ah, phys_addr, AR5K_NOQCU_TXDP1);
932                         ath5k_hw_reg_write(ah, AR5K_BCR_TQ1V | AR5K_BCR_BDMAE,
933                                         AR5K_BCR);
934
935                         return -EIO;
936                 }
937                 ret = 0;
938         } else {
939         /*5211/5212*/
940                 ret = ath5k_hw_register_timeout(ah,
941                         AR5K_QUEUE_STATUS(AR5K_TX_QUEUE_ID_BEACON),
942                         AR5K_QCU_STS_FRMPENDCNT, 0, false);
943
944                 if (AR5K_REG_READ_Q(ah, AR5K_QCU_TXE, AR5K_TX_QUEUE_ID_BEACON))
945                         return -EIO;
946         }
947
948         return ret;
949 }
950 #endif
951
952
953 /*********************\
954 * Key table functions *
955 \*********************/
956
957 /*
958  * Reset a key entry on the table
959  */
960 int ath5k_hw_reset_key(struct ath5k_hw *ah, u16 entry)
961 {
962         unsigned int i, type;
963
964         ATH5K_TRACE(ah->ah_sc);
965         AR5K_ASSERT_ENTRY(entry, AR5K_KEYTABLE_SIZE);
966
967         type = ath5k_hw_reg_read(ah, AR5K_KEYTABLE_TYPE(entry));
968
969         for (i = 0; i < AR5K_KEYCACHE_SIZE; i++)
970                 ath5k_hw_reg_write(ah, 0, AR5K_KEYTABLE_OFF(entry, i));
971
972         /* Reset associated MIC entry if TKIP
973          * is enabled located at offset (entry + 64) */
974         if (type == AR5K_KEYTABLE_TYPE_TKIP) {
975                 entry = entry + AR5K_KEYTABLE_MIC_OFFSET;
976                 AR5K_ASSERT_ENTRY(entry, AR5K_KEYTABLE_SIZE);
977                 for (i = 0; i < AR5K_KEYCACHE_SIZE / 2 ; i++)
978                         ath5k_hw_reg_write(ah, 0, AR5K_KEYTABLE_OFF(entry, i));
979         }
980
981         /*
982          * Set NULL encryption on AR5212+
983          *
984          * Note: AR5K_KEYTABLE_TYPE -> AR5K_KEYTABLE_OFF(entry, 5)
985          *       AR5K_KEYTABLE_TYPE_NULL -> 0x00000007
986          *
987          * Note2: Windows driver (ndiswrapper) sets this to
988          *        0x00000714 instead of 0x00000007
989          */
990         if (ah->ah_version > AR5K_AR5211)
991                 ath5k_hw_reg_write(ah, AR5K_KEYTABLE_TYPE_NULL,
992                                 AR5K_KEYTABLE_TYPE(entry));
993
994         return 0;
995 }
996
997 /*
998  * Check if a table entry is valid
999  */
1000 int ath5k_hw_is_key_valid(struct ath5k_hw *ah, u16 entry)
1001 {
1002         ATH5K_TRACE(ah->ah_sc);
1003         AR5K_ASSERT_ENTRY(entry, AR5K_KEYTABLE_SIZE);
1004
1005         /* Check the validation flag at the end of the entry */
1006         return ath5k_hw_reg_read(ah, AR5K_KEYTABLE_MAC1(entry)) &
1007                 AR5K_KEYTABLE_VALID;
1008 }
1009
1010 /*
1011  * Set a key entry on the table
1012  */
1013 int ath5k_hw_set_key(struct ath5k_hw *ah, u16 entry,
1014                 const struct ieee80211_key_conf *key, const u8 *mac)
1015 {
1016         unsigned int i;
1017         __le32 key_v[5] = {};
1018         u32 keytype;
1019
1020         ATH5K_TRACE(ah->ah_sc);
1021
1022         /* key->keylen comes in from mac80211 in bytes */
1023
1024         if (key->keylen > AR5K_KEYTABLE_SIZE / 8)
1025                 return -EOPNOTSUPP;
1026
1027         switch (key->keylen) {
1028         /* WEP 40-bit   = 40-bit  entered key + 24 bit IV = 64-bit */
1029         case 40 / 8:
1030                 memcpy(&key_v[0], key->key, 5);
1031                 keytype = AR5K_KEYTABLE_TYPE_40;
1032                 break;
1033
1034         /* WEP 104-bit  = 104-bit entered key + 24-bit IV = 128-bit */
1035         case 104 / 8:
1036                 memcpy(&key_v[0], &key->key[0], 6);
1037                 memcpy(&key_v[2], &key->key[6], 6);
1038                 memcpy(&key_v[4], &key->key[12], 1);
1039                 keytype = AR5K_KEYTABLE_TYPE_104;
1040                 break;
1041         /* WEP 128-bit  = 128-bit entered key + 24 bit IV = 152-bit */
1042         case 128 / 8:
1043                 memcpy(&key_v[0], &key->key[0], 6);
1044                 memcpy(&key_v[2], &key->key[6], 6);
1045                 memcpy(&key_v[4], &key->key[12], 4);
1046                 keytype = AR5K_KEYTABLE_TYPE_128;
1047                 break;
1048
1049         default:
1050                 return -EINVAL; /* shouldn't happen */
1051         }
1052
1053         for (i = 0; i < ARRAY_SIZE(key_v); i++)
1054                 ath5k_hw_reg_write(ah, le32_to_cpu(key_v[i]),
1055                                 AR5K_KEYTABLE_OFF(entry, i));
1056
1057         ath5k_hw_reg_write(ah, keytype, AR5K_KEYTABLE_TYPE(entry));
1058
1059         return ath5k_hw_set_key_lladdr(ah, entry, mac);
1060 }
1061
1062 int ath5k_hw_set_key_lladdr(struct ath5k_hw *ah, u16 entry, const u8 *mac)
1063 {
1064         u32 low_id, high_id;
1065
1066         ATH5K_TRACE(ah->ah_sc);
1067          /* Invalid entry (key table overflow) */
1068         AR5K_ASSERT_ENTRY(entry, AR5K_KEYTABLE_SIZE);
1069
1070         /* MAC may be NULL if it's a broadcast key. In this case no need to
1071          * to compute AR5K_LOW_ID and AR5K_HIGH_ID as we already know it. */
1072         if (unlikely(mac == NULL)) {
1073                 low_id = 0xffffffff;
1074                 high_id = 0xffff | AR5K_KEYTABLE_VALID;
1075         } else {
1076                 low_id = AR5K_LOW_ID(mac);
1077                 high_id = AR5K_HIGH_ID(mac) | AR5K_KEYTABLE_VALID;
1078         }
1079
1080         ath5k_hw_reg_write(ah, low_id, AR5K_KEYTABLE_MAC0(entry));
1081         ath5k_hw_reg_write(ah, high_id, AR5K_KEYTABLE_MAC1(entry));
1082
1083         return 0;
1084 }
1085