ath5k: Reset cleanup and generic cleanup
[pandora-kernel.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 * Helper functions *
36 \*******************/
37
38 /**
39  * ath5k_hw_get_default_slottime - Get the default slot time for current mode
40  *
41  * @ah: The &struct ath5k_hw
42  */
43 static unsigned int ath5k_hw_get_default_slottime(struct ath5k_hw *ah)
44 {
45         struct ieee80211_channel *channel = ah->ah_current_channel;
46
47         if (channel->hw_value & CHANNEL_TURBO)
48                 return 6; /* both turbo modes */
49
50         if (channel->hw_value & CHANNEL_CCK)
51                 return 20; /* 802.11b */
52
53         return 9; /* 802.11 a/g */
54 }
55
56 /**
57  * ath5k_hw_get_default_sifs - Get the default SIFS for current mode
58  *
59  * @ah: The &struct ath5k_hw
60  */
61 static unsigned int ath5k_hw_get_default_sifs(struct ath5k_hw *ah)
62 {
63         struct ieee80211_channel *channel = ah->ah_current_channel;
64
65         if (channel->hw_value & CHANNEL_TURBO)
66                 return 8; /* both turbo modes */
67
68         if (channel->hw_value & CHANNEL_5GHZ)
69                 return 16; /* 802.11a */
70
71         return 10; /* 802.11 b/g */
72 }
73
74 /**
75  * ath5k_hw_update_mib_counters - Update MIB counters (mac layer statistics)
76  *
77  * @ah: The &struct ath5k_hw
78  *
79  * Reads MIB counters from PCU and updates sw statistics. Is called after a
80  * MIB interrupt, because one of these counters might have reached their maximum
81  * and triggered the MIB interrupt, to let us read and clear the counter.
82  *
83  * Is called in interrupt context!
84  */
85 void ath5k_hw_update_mib_counters(struct ath5k_hw *ah)
86 {
87         struct ath5k_statistics *stats = &ah->ah_sc->stats;
88
89         /* Read-And-Clear */
90         stats->ack_fail += ath5k_hw_reg_read(ah, AR5K_ACK_FAIL);
91         stats->rts_fail += ath5k_hw_reg_read(ah, AR5K_RTS_FAIL);
92         stats->rts_ok += ath5k_hw_reg_read(ah, AR5K_RTS_OK);
93         stats->fcs_error += ath5k_hw_reg_read(ah, AR5K_FCS_FAIL);
94         stats->beacons += ath5k_hw_reg_read(ah, AR5K_BEACON_CNT);
95 }
96
97 /**
98  * ath5k_hw_set_ack_bitrate - set bitrate for ACKs
99  *
100  * @ah: The &struct ath5k_hw
101  * @high: Flag to determine if we want to use high transmission rate
102  * for ACKs or not
103  *
104  * If high flag is set, we tell hw to use a set of control rates based on
105  * the current transmission rate (check out control_rates array inside reset.c).
106  * If not hw just uses the lowest rate available for the current modulation
107  * scheme being used (1Mbit for CCK and 6Mbits for OFDM).
108  */
109 void ath5k_hw_set_ack_bitrate_high(struct ath5k_hw *ah, bool high)
110 {
111         if (ah->ah_version != AR5K_AR5212)
112                 return;
113         else {
114                 u32 val = AR5K_STA_ID1_BASE_RATE_11B | AR5K_STA_ID1_ACKCTS_6MB;
115                 if (high)
116                         AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1, val);
117                 else
118                         AR5K_REG_ENABLE_BITS(ah, AR5K_STA_ID1, val);
119         }
120 }
121
122
123 /******************\
124 * ACK/CTS Timeouts *
125 \******************/
126
127 /*
128  * index into rates for control rates, we can set it up like this because
129  * this is only used for AR5212 and we know it supports G mode
130  */
131 static const unsigned int control_rates[] =
132         { 0, 1, 1, 1, 4, 4, 6, 6, 8, 8, 8, 8 };
133
134 /**
135  * ath5k_hw_write_rate_duration - fill rate code to duration table
136  *
137  * @ah: the &struct ath5k_hw
138  * @mode: one of enum ath5k_driver_mode
139  *
140  * Write the rate code to duration table upon hw reset. This is a helper for
141  * ath5k_hw_reset(). It seems all this is doing is setting an ACK timeout on
142  * the hardware, based on current mode, for each rate. The rates which are
143  * capable of short preamble (802.11b rates 2Mbps, 5.5Mbps, and 11Mbps) have
144  * different rate code so we write their value twice (one for long preamble
145  * and one for short).
146  *
147  * Note: Band doesn't matter here, if we set the values for OFDM it works
148  * on both a and g modes. So all we have to do is set values for all g rates
149  * that include all OFDM and CCK rates. If we operate in turbo or xr/half/
150  * quarter rate mode, we need to use another set of bitrates (that's why we
151  * need the mode parameter) but we don't handle these proprietary modes yet.
152  */
153 static inline void ath5k_hw_write_rate_duration(struct ath5k_hw *ah,
154                                                 unsigned int mode)
155 {
156         struct ath5k_softc *sc = ah->ah_sc;
157         struct ieee80211_rate *rate;
158         unsigned int i;
159
160         /* Write rate duration table */
161         for (i = 0; i < sc->sbands[IEEE80211_BAND_2GHZ].n_bitrates; i++) {
162                 u32 reg;
163                 u16 tx_time;
164
165                 rate = &sc->sbands[IEEE80211_BAND_2GHZ].bitrates[control_rates[i]];
166
167                 /* Set ACK timeout */
168                 reg = AR5K_RATE_DUR(rate->hw_value);
169
170                 /* An ACK frame consists of 10 bytes. If you add the FCS,
171                  * which ieee80211_generic_frame_duration() adds,
172                  * its 14 bytes. Note we use the control rate and not the
173                  * actual rate for this rate. See mac80211 tx.c
174                  * ieee80211_duration() for a brief description of
175                  * what rate we should choose to TX ACKs. */
176                 tx_time = le16_to_cpu(ieee80211_generic_frame_duration(sc->hw,
177                                                         NULL, 10, rate));
178
179                 ath5k_hw_reg_write(ah, tx_time, reg);
180
181                 if (!(rate->flags & IEEE80211_RATE_SHORT_PREAMBLE))
182                         continue;
183
184                 /*
185                  * We're not distinguishing short preamble here,
186                  * This is true, all we'll get is a longer value here
187                  * which is not necessarilly bad. We could use
188                  * export ieee80211_frame_duration() but that needs to be
189                  * fixed first to be properly used by mac802111 drivers:
190                  *
191                  *  - remove erp stuff and let the routine figure ofdm
192                  *    erp rates
193                  *  - remove passing argument ieee80211_local as
194                  *    drivers don't have access to it
195                  *  - move drivers using ieee80211_generic_frame_duration()
196                  *    to this
197                  */
198                 ath5k_hw_reg_write(ah, tx_time,
199                         reg + (AR5K_SET_SHORT_PREAMBLE << 2));
200         }
201 }
202
203 /**
204  * ath5k_hw_set_ack_timeout - Set ACK timeout on PCU
205  *
206  * @ah: The &struct ath5k_hw
207  * @timeout: Timeout in usec
208  */
209 static int ath5k_hw_set_ack_timeout(struct ath5k_hw *ah, unsigned int timeout)
210 {
211         if (ath5k_hw_clocktoh(ah, AR5K_REG_MS(0xffffffff, AR5K_TIME_OUT_ACK))
212                         <= timeout)
213                 return -EINVAL;
214
215         AR5K_REG_WRITE_BITS(ah, AR5K_TIME_OUT, AR5K_TIME_OUT_ACK,
216                 ath5k_hw_htoclock(ah, timeout));
217
218         return 0;
219 }
220
221 /**
222  * ath5k_hw_set_cts_timeout - Set CTS timeout on PCU
223  *
224  * @ah: The &struct ath5k_hw
225  * @timeout: Timeout in usec
226  */
227 static int ath5k_hw_set_cts_timeout(struct ath5k_hw *ah, unsigned int timeout)
228 {
229         if (ath5k_hw_clocktoh(ah, AR5K_REG_MS(0xffffffff, AR5K_TIME_OUT_CTS))
230                         <= timeout)
231                 return -EINVAL;
232
233         AR5K_REG_WRITE_BITS(ah, AR5K_TIME_OUT, AR5K_TIME_OUT_CTS,
234                         ath5k_hw_htoclock(ah, timeout));
235
236         return 0;
237 }
238
239
240 /*******************\
241 * RX filter Control *
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         /* Set new station ID */
259         memcpy(common->macaddr, mac, ETH_ALEN);
260
261         pcu_reg = ath5k_hw_reg_read(ah, AR5K_STA_ID1) & 0xffff0000;
262
263         low_id = get_unaligned_le32(mac);
264         high_id = get_unaligned_le16(mac + 4);
265
266         ath5k_hw_reg_write(ah, low_id, AR5K_STA_ID0);
267         ath5k_hw_reg_write(ah, pcu_reg | high_id, AR5K_STA_ID1);
268
269         return 0;
270 }
271
272 /**
273  * ath5k_hw_set_bssid - Set current BSSID on hw
274  *
275  * @ah: The &struct ath5k_hw
276  *
277  * Sets the current BSSID and BSSID mask we have from the
278  * common struct into the hardware
279  */
280 void ath5k_hw_set_bssid(struct ath5k_hw *ah)
281 {
282         struct ath_common *common = ath5k_hw_common(ah);
283         u16 tim_offset = 0;
284
285         /*
286          * Set BSSID mask on 5212
287          */
288         if (ah->ah_version == AR5K_AR5212)
289                 ath_hw_setbssidmask(common);
290
291         /*
292          * Set BSSID
293          */
294         ath5k_hw_reg_write(ah,
295                            get_unaligned_le32(common->curbssid),
296                            AR5K_BSS_ID0);
297         ath5k_hw_reg_write(ah,
298                            get_unaligned_le16(common->curbssid + 4) |
299                            ((common->curaid & 0x3fff) << AR5K_BSS_ID1_AID_S),
300                            AR5K_BSS_ID1);
301
302         if (common->curaid == 0) {
303                 ath5k_hw_disable_pspoll(ah);
304                 return;
305         }
306
307         AR5K_REG_WRITE_BITS(ah, AR5K_BEACON, AR5K_BEACON_TIM,
308                             tim_offset ? tim_offset + 4 : 0);
309
310         ath5k_hw_enable_pspoll(ah, NULL, 0);
311 }
312
313 void ath5k_hw_set_bssid_mask(struct ath5k_hw *ah, const u8 *mask)
314 {
315         struct ath_common *common = ath5k_hw_common(ah);
316
317         /* Cache bssid mask so that we can restore it
318          * on reset */
319         memcpy(common->bssidmask, mask, ETH_ALEN);
320         if (ah->ah_version == AR5K_AR5212)
321                 ath_hw_setbssidmask(common);
322 }
323
324 /*
325  * Set multicast filter
326  */
327 void ath5k_hw_set_mcast_filter(struct ath5k_hw *ah, u32 filter0, u32 filter1)
328 {
329         ath5k_hw_reg_write(ah, filter0, AR5K_MCAST_FILTER0);
330         ath5k_hw_reg_write(ah, filter1, AR5K_MCAST_FILTER1);
331 }
332
333 /**
334  * ath5k_hw_get_rx_filter - Get current rx filter
335  *
336  * @ah: The &struct ath5k_hw
337  *
338  * Returns the RX filter by reading rx filter and
339  * phy error filter registers. RX filter is used
340  * to set the allowed frame types that PCU will accept
341  * and pass to the driver. For a list of frame types
342  * check out reg.h.
343  */
344 u32 ath5k_hw_get_rx_filter(struct ath5k_hw *ah)
345 {
346         u32 data, filter = 0;
347
348         filter = ath5k_hw_reg_read(ah, AR5K_RX_FILTER);
349
350         /*Radar detection for 5212*/
351         if (ah->ah_version == AR5K_AR5212) {
352                 data = ath5k_hw_reg_read(ah, AR5K_PHY_ERR_FIL);
353
354                 if (data & AR5K_PHY_ERR_FIL_RADAR)
355                         filter |= AR5K_RX_FILTER_RADARERR;
356                 if (data & (AR5K_PHY_ERR_FIL_OFDM | AR5K_PHY_ERR_FIL_CCK))
357                         filter |= AR5K_RX_FILTER_PHYERR;
358         }
359
360         return filter;
361 }
362
363 /**
364  * ath5k_hw_set_rx_filter - Set rx filter
365  *
366  * @ah: The &struct ath5k_hw
367  * @filter: RX filter mask (see reg.h)
368  *
369  * Sets RX filter register and also handles PHY error filter
370  * register on 5212 and newer chips so that we have proper PHY
371  * error reporting.
372  */
373 void ath5k_hw_set_rx_filter(struct ath5k_hw *ah, u32 filter)
374 {
375         u32 data = 0;
376
377         /* Set PHY error filter register on 5212*/
378         if (ah->ah_version == AR5K_AR5212) {
379                 if (filter & AR5K_RX_FILTER_RADARERR)
380                         data |= AR5K_PHY_ERR_FIL_RADAR;
381                 if (filter & AR5K_RX_FILTER_PHYERR)
382                         data |= AR5K_PHY_ERR_FIL_OFDM | AR5K_PHY_ERR_FIL_CCK;
383         }
384
385         /*
386          * The AR5210 uses promiscous mode to detect radar activity
387          */
388         if (ah->ah_version == AR5K_AR5210 &&
389                         (filter & AR5K_RX_FILTER_RADARERR)) {
390                 filter &= ~AR5K_RX_FILTER_RADARERR;
391                 filter |= AR5K_RX_FILTER_PROM;
392         }
393
394         /*Zero length DMA (phy error reporting) */
395         if (data)
396                 AR5K_REG_ENABLE_BITS(ah, AR5K_RXCFG, AR5K_RXCFG_ZLFDMA);
397         else
398                 AR5K_REG_DISABLE_BITS(ah, AR5K_RXCFG, AR5K_RXCFG_ZLFDMA);
399
400         /*Write RX Filter register*/
401         ath5k_hw_reg_write(ah, filter & 0xff, AR5K_RX_FILTER);
402
403         /*Write PHY error filter register on 5212*/
404         if (ah->ah_version == AR5K_AR5212)
405                 ath5k_hw_reg_write(ah, data, AR5K_PHY_ERR_FIL);
406
407 }
408
409
410 /****************\
411 * Beacon control *
412 \****************/
413
414 #define ATH5K_MAX_TSF_READ 10
415
416 /**
417  * ath5k_hw_get_tsf64 - Get the full 64bit TSF
418  *
419  * @ah: The &struct ath5k_hw
420  *
421  * Returns the current TSF
422  */
423 u64 ath5k_hw_get_tsf64(struct ath5k_hw *ah)
424 {
425         u32 tsf_lower, tsf_upper1, tsf_upper2;
426         int i;
427         unsigned long flags;
428
429         /* This code is time critical - we don't want to be interrupted here */
430         local_irq_save(flags);
431
432         /*
433          * While reading TSF upper and then lower part, the clock is still
434          * counting (or jumping in case of IBSS merge) so we might get
435          * inconsistent values. To avoid this, we read the upper part again
436          * and check it has not been changed. We make the hypothesis that a
437          * maximum of 3 changes can happens in a row (we use 10 as a safe
438          * value).
439          *
440          * Impact on performance is pretty small, since in most cases, only
441          * 3 register reads are needed.
442          */
443
444         tsf_upper1 = ath5k_hw_reg_read(ah, AR5K_TSF_U32);
445         for (i = 0; i < ATH5K_MAX_TSF_READ; i++) {
446                 tsf_lower = ath5k_hw_reg_read(ah, AR5K_TSF_L32);
447                 tsf_upper2 = ath5k_hw_reg_read(ah, AR5K_TSF_U32);
448                 if (tsf_upper2 == tsf_upper1)
449                         break;
450                 tsf_upper1 = tsf_upper2;
451         }
452
453         local_irq_restore(flags);
454
455         WARN_ON( i == ATH5K_MAX_TSF_READ );
456
457         return (((u64)tsf_upper1 << 32) | tsf_lower);
458 }
459
460 /**
461  * ath5k_hw_set_tsf64 - Set a new 64bit TSF
462  *
463  * @ah: The &struct ath5k_hw
464  * @tsf64: The new 64bit TSF
465  *
466  * Sets the new TSF
467  */
468 void ath5k_hw_set_tsf64(struct ath5k_hw *ah, u64 tsf64)
469 {
470         ath5k_hw_reg_write(ah, tsf64 & 0xffffffff, AR5K_TSF_L32);
471         ath5k_hw_reg_write(ah, (tsf64 >> 32) & 0xffffffff, AR5K_TSF_U32);
472 }
473
474 /**
475  * ath5k_hw_reset_tsf - Force a TSF reset
476  *
477  * @ah: The &struct ath5k_hw
478  *
479  * Forces a TSF reset on PCU
480  */
481 void ath5k_hw_reset_tsf(struct ath5k_hw *ah)
482 {
483         u32 val;
484
485         val = ath5k_hw_reg_read(ah, AR5K_BEACON) | AR5K_BEACON_RESET_TSF;
486
487         /*
488          * Each write to the RESET_TSF bit toggles a hardware internal
489          * signal to reset TSF, but if left high it will cause a TSF reset
490          * on the next chip reset as well.  Thus we always write the value
491          * twice to clear the signal.
492          */
493         ath5k_hw_reg_write(ah, val, AR5K_BEACON);
494         ath5k_hw_reg_write(ah, val, AR5K_BEACON);
495 }
496
497 /*
498  * Initialize beacon timers
499  */
500 void ath5k_hw_init_beacon(struct ath5k_hw *ah, u32 next_beacon, u32 interval)
501 {
502         u32 timer1, timer2, timer3;
503
504         /*
505          * Set the additional timers by mode
506          */
507         switch (ah->ah_sc->opmode) {
508         case NL80211_IFTYPE_MONITOR:
509         case NL80211_IFTYPE_STATION:
510                 /* In STA mode timer1 is used as next wakeup
511                  * timer and timer2 as next CFP duration start
512                  * timer. Both in 1/8TUs. */
513                 /* TODO: PCF handling */
514                 if (ah->ah_version == AR5K_AR5210) {
515                         timer1 = 0xffffffff;
516                         timer2 = 0xffffffff;
517                 } else {
518                         timer1 = 0x0000ffff;
519                         timer2 = 0x0007ffff;
520                 }
521                 /* Mark associated AP as PCF incapable for now */
522                 AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1, AR5K_STA_ID1_PCF);
523                 break;
524         case NL80211_IFTYPE_ADHOC:
525                 AR5K_REG_ENABLE_BITS(ah, AR5K_TXCFG, AR5K_TXCFG_ADHOC_BCN_ATIM);
526         default:
527                 /* On non-STA modes timer1 is used as next DMA
528                  * beacon alert (DBA) timer and timer2 as next
529                  * software beacon alert. Both in 1/8TUs. */
530                 timer1 = (next_beacon - AR5K_TUNE_DMA_BEACON_RESP) << 3;
531                 timer2 = (next_beacon - AR5K_TUNE_SW_BEACON_RESP) << 3;
532                 break;
533         }
534
535         /* Timer3 marks the end of our ATIM window
536          * a zero length window is not allowed because
537          * we 'll get no beacons */
538         timer3 = next_beacon + 1;
539
540         /*
541          * Set the beacon register and enable all timers.
542          */
543         /* When in AP or Mesh Point mode zero timer0 to start TSF */
544         if (ah->ah_sc->opmode == NL80211_IFTYPE_AP ||
545             ah->ah_sc->opmode == NL80211_IFTYPE_MESH_POINT)
546                 ath5k_hw_reg_write(ah, 0, AR5K_TIMER0);
547
548         ath5k_hw_reg_write(ah, next_beacon, AR5K_TIMER0);
549         ath5k_hw_reg_write(ah, timer1, AR5K_TIMER1);
550         ath5k_hw_reg_write(ah, timer2, AR5K_TIMER2);
551         ath5k_hw_reg_write(ah, timer3, AR5K_TIMER3);
552
553         /* Force a TSF reset if requested and enable beacons */
554         if (interval & AR5K_BEACON_RESET_TSF)
555                 ath5k_hw_reset_tsf(ah);
556
557         ath5k_hw_reg_write(ah, interval & (AR5K_BEACON_PERIOD |
558                                         AR5K_BEACON_ENABLE),
559                                                 AR5K_BEACON);
560
561         /* Flush any pending BMISS interrupts on ISR by
562          * performing a clear-on-write operation on PISR
563          * register for the BMISS bit (writing a bit on
564          * ISR togles a reset for that bit and leaves
565          * the rest bits intact) */
566         if (ah->ah_version == AR5K_AR5210)
567                 ath5k_hw_reg_write(ah, AR5K_ISR_BMISS, AR5K_ISR);
568         else
569                 ath5k_hw_reg_write(ah, AR5K_ISR_BMISS, AR5K_PISR);
570
571         /* TODO: Set enchanced sleep registers on AR5212
572          * based on vif->bss_conf params, until then
573          * disable power save reporting.*/
574         AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1, AR5K_STA_ID1_PWR_SV);
575
576 }
577
578 /**
579  * ath5k_check_timer_win - Check if timer B is timer A + window
580  *
581  * @a: timer a (before b)
582  * @b: timer b (after a)
583  * @window: difference between a and b
584  * @intval: timers are increased by this interval
585  *
586  * This helper function checks if timer B is timer A + window and covers
587  * cases where timer A or B might have already been updated or wrapped
588  * around (Timers are 16 bit).
589  *
590  * Returns true if O.K.
591  */
592 static inline bool
593 ath5k_check_timer_win(int a, int b, int window, int intval)
594 {
595         /*
596          * 1.) usually B should be A + window
597          * 2.) A already updated, B not updated yet
598          * 3.) A already updated and has wrapped around
599          * 4.) B has wrapped around
600          */
601         if ((b - a == window) ||                                /* 1.) */
602             (a - b == intval - window) ||                       /* 2.) */
603             ((a | 0x10000) - b == intval - window) ||           /* 3.) */
604             ((b | 0x10000) - a == window))                      /* 4.) */
605                 return true; /* O.K. */
606         return false;
607 }
608
609 /**
610  * ath5k_hw_check_beacon_timers - Check if the beacon timers are correct
611  *
612  * @ah: The &struct ath5k_hw
613  * @intval: beacon interval
614  *
615  * This is a workaround for IBSS mode:
616  *
617  * The need for this function arises from the fact that we have 4 separate
618  * HW timer registers (TIMER0 - TIMER3), which are closely related to the
619  * next beacon target time (NBTT), and that the HW updates these timers
620  * seperately based on the current TSF value. The hardware increments each
621  * timer by the beacon interval, when the local TSF coverted to TU is equal
622  * to the value stored in the timer.
623  *
624  * The reception of a beacon with the same BSSID can update the local HW TSF
625  * at any time - this is something we can't avoid. If the TSF jumps to a
626  * time which is later than the time stored in a timer, this timer will not
627  * be updated until the TSF in TU wraps around at 16 bit (the size of the
628  * timers) and reaches the time which is stored in the timer.
629  *
630  * The problem is that these timers are closely related to TIMER0 (NBTT) and
631  * that they define a time "window". When the TSF jumps between two timers
632  * (e.g. ATIM and NBTT), the one in the past will be left behind (not
633  * updated), while the one in the future will be updated every beacon
634  * interval. This causes the window to get larger, until the TSF wraps
635  * around as described above and the timer which was left behind gets
636  * updated again. But - because the beacon interval is usually not an exact
637  * divisor of the size of the timers (16 bit), an unwanted "window" between
638  * these timers has developed!
639  *
640  * This is especially important with the ATIM window, because during
641  * the ATIM window only ATIM frames and no data frames are allowed to be
642  * sent, which creates transmission pauses after each beacon. This symptom
643  * has been described as "ramping ping" because ping times increase linearly
644  * for some time and then drop down again. A wrong window on the DMA beacon
645  * timer has the same effect, so we check for these two conditions.
646  *
647  * Returns true if O.K.
648  */
649 bool
650 ath5k_hw_check_beacon_timers(struct ath5k_hw *ah, int intval)
651 {
652         unsigned int nbtt, atim, dma;
653
654         nbtt = ath5k_hw_reg_read(ah, AR5K_TIMER0);
655         atim = ath5k_hw_reg_read(ah, AR5K_TIMER3);
656         dma = ath5k_hw_reg_read(ah, AR5K_TIMER1) >> 3;
657
658         /* NOTE: SWBA is different. Having a wrong window there does not
659          * stop us from sending data and this condition is catched thru
660          * other means (SWBA interrupt) */
661
662         if (ath5k_check_timer_win(nbtt, atim, 1, intval) &&
663             ath5k_check_timer_win(dma, nbtt, AR5K_TUNE_DMA_BEACON_RESP,
664                                   intval))
665                 return true; /* O.K. */
666         return false;
667 }
668
669 /**
670  * ath5k_hw_set_coverage_class - Set IEEE 802.11 coverage class
671  *
672  * @ah: The &struct ath5k_hw
673  * @coverage_class: IEEE 802.11 coverage class number
674  *
675  * Sets slot time, ACK timeout and CTS timeout for given coverage class.
676  */
677 void ath5k_hw_set_coverage_class(struct ath5k_hw *ah, u8 coverage_class)
678 {
679         /* As defined by IEEE 802.11-2007 17.3.8.6 */
680         int slot_time = ath5k_hw_get_default_slottime(ah) + 3 * coverage_class;
681         int ack_timeout = ath5k_hw_get_default_sifs(ah) + slot_time;
682         int cts_timeout = ack_timeout;
683
684         ath5k_hw_set_slot_time(ah, slot_time);
685         ath5k_hw_set_ack_timeout(ah, ack_timeout);
686         ath5k_hw_set_cts_timeout(ah, cts_timeout);
687
688         ah->ah_coverage_class = coverage_class;
689 }
690
691 /***************************\
692 * Init/Start/Stop functions *
693 \***************************/
694
695 /**
696  * ath5k_hw_start_rx_pcu - Start RX engine
697  *
698  * @ah: The &struct ath5k_hw
699  *
700  * Starts RX engine on PCU so that hw can process RXed frames
701  * (ACK etc).
702  *
703  * NOTE: RX DMA should be already enabled using ath5k_hw_start_rx_dma
704  */
705 void ath5k_hw_start_rx_pcu(struct ath5k_hw *ah)
706 {
707         AR5K_REG_DISABLE_BITS(ah, AR5K_DIAG_SW, AR5K_DIAG_SW_DIS_RX);
708 }
709
710 /**
711  * at5k_hw_stop_rx_pcu - Stop RX engine
712  *
713  * @ah: The &struct ath5k_hw
714  *
715  * Stops RX engine on PCU
716  */
717 void ath5k_hw_stop_rx_pcu(struct ath5k_hw *ah)
718 {
719         AR5K_REG_ENABLE_BITS(ah, AR5K_DIAG_SW, AR5K_DIAG_SW_DIS_RX);
720 }
721
722 /**
723  * ath5k_hw_set_opmode - Set PCU operating mode
724  *
725  * @ah: The &struct ath5k_hw
726  * @op_mode: &enum nl80211_iftype operating mode
727  *
728  * Configure PCU for the various operating modes (AP/STA etc)
729  */
730 int ath5k_hw_set_opmode(struct ath5k_hw *ah, enum nl80211_iftype op_mode)
731 {
732         struct ath_common *common = ath5k_hw_common(ah);
733         u32 pcu_reg, beacon_reg, low_id, high_id;
734
735         ATH5K_DBG(ah->ah_sc, ATH5K_DEBUG_MODE, "mode %d\n", op_mode);
736
737         /* Preserve rest settings */
738         pcu_reg = ath5k_hw_reg_read(ah, AR5K_STA_ID1) & 0xffff0000;
739         pcu_reg &= ~(AR5K_STA_ID1_ADHOC | AR5K_STA_ID1_AP
740                         | AR5K_STA_ID1_KEYSRCH_MODE
741                         | (ah->ah_version == AR5K_AR5210 ?
742                         (AR5K_STA_ID1_PWR_SV | AR5K_STA_ID1_NO_PSPOLL) : 0));
743
744         beacon_reg = 0;
745
746         switch (op_mode) {
747         case NL80211_IFTYPE_ADHOC:
748                 pcu_reg |= AR5K_STA_ID1_ADHOC | AR5K_STA_ID1_KEYSRCH_MODE;
749                 beacon_reg |= AR5K_BCR_ADHOC;
750                 if (ah->ah_version == AR5K_AR5210)
751                         pcu_reg |= AR5K_STA_ID1_NO_PSPOLL;
752                 else
753                         AR5K_REG_ENABLE_BITS(ah, AR5K_CFG, AR5K_CFG_IBSS);
754                 break;
755
756         case NL80211_IFTYPE_AP:
757         case NL80211_IFTYPE_MESH_POINT:
758                 pcu_reg |= AR5K_STA_ID1_AP | AR5K_STA_ID1_KEYSRCH_MODE;
759                 beacon_reg |= AR5K_BCR_AP;
760                 if (ah->ah_version == AR5K_AR5210)
761                         pcu_reg |= AR5K_STA_ID1_NO_PSPOLL;
762                 else
763                         AR5K_REG_DISABLE_BITS(ah, AR5K_CFG, AR5K_CFG_IBSS);
764                 break;
765
766         case NL80211_IFTYPE_STATION:
767                 pcu_reg |= AR5K_STA_ID1_KEYSRCH_MODE
768                         | (ah->ah_version == AR5K_AR5210 ?
769                                 AR5K_STA_ID1_PWR_SV : 0);
770         case NL80211_IFTYPE_MONITOR:
771                 pcu_reg |= AR5K_STA_ID1_KEYSRCH_MODE
772                         | (ah->ah_version == AR5K_AR5210 ?
773                                 AR5K_STA_ID1_NO_PSPOLL : 0);
774                 break;
775
776         default:
777                 return -EINVAL;
778         }
779
780         /*
781          * Set PCU registers
782          */
783         low_id = get_unaligned_le32(common->macaddr);
784         high_id = get_unaligned_le16(common->macaddr + 4);
785         ath5k_hw_reg_write(ah, low_id, AR5K_STA_ID0);
786         ath5k_hw_reg_write(ah, pcu_reg | high_id, AR5K_STA_ID1);
787
788         /*
789          * Set Beacon Control Register on 5210
790          */
791         if (ah->ah_version == AR5K_AR5210)
792                 ath5k_hw_reg_write(ah, beacon_reg, AR5K_BCR);
793
794         return 0;
795 }
796
797 void ath5k_hw_pcu_init(struct ath5k_hw *ah, enum nl80211_iftype op_mode,
798                                                                 u8 mode)
799 {
800         /* Set bssid and bssid mask */
801         ath5k_hw_set_bssid(ah);
802
803         /* Set PCU config */
804         ath5k_hw_set_opmode(ah, op_mode);
805
806         /* Write rate duration table only on AR5212 and if
807          * virtual interface has already been brought up
808          * XXX: rethink this after new mode changes to
809          * mac80211 are integrated */
810         if (ah->ah_version == AR5K_AR5212 &&
811                 ah->ah_sc->nvifs)
812                 ath5k_hw_write_rate_duration(ah, mode);
813
814         /* Set RSSI/BRSSI thresholds
815          *
816          * Note: If we decide to set this value
817          * dynamicaly, have in mind that when AR5K_RSSI_THR
818          * register is read it might return 0x40 if we haven't
819          * wrote anything to it plus BMISS RSSI threshold is zeroed.
820          * So doing a save/restore procedure here isn't the right
821          * choice. Instead store it on ath5k_hw */
822         ath5k_hw_reg_write(ah, (AR5K_TUNE_RSSI_THRES |
823                                 AR5K_TUNE_BMISS_THRES <<
824                                 AR5K_RSSI_THR_BMISS_S),
825                                 AR5K_RSSI_THR);
826
827         /* MIC QoS support */
828         if (ah->ah_mac_srev >= AR5K_SREV_AR2413) {
829                 ath5k_hw_reg_write(ah, 0x000100aa, AR5K_MIC_QOS_CTL);
830                 ath5k_hw_reg_write(ah, 0x00003210, AR5K_MIC_QOS_SEL);
831         }
832
833         /* QoS NOACK Policy */
834         if (ah->ah_version == AR5K_AR5212) {
835                 ath5k_hw_reg_write(ah,
836                         AR5K_REG_SM(2, AR5K_QOS_NOACK_2BIT_VALUES) |
837                         AR5K_REG_SM(5, AR5K_QOS_NOACK_BIT_OFFSET)  |
838                         AR5K_REG_SM(0, AR5K_QOS_NOACK_BYTE_OFFSET),
839                         AR5K_QOS_NOACK);
840         }
841
842         /* Restore slot time and ACK timeouts */
843         if (ah->ah_coverage_class > 0)
844                 ath5k_hw_set_coverage_class(ah, ah->ah_coverage_class);
845
846         return;
847 }