]> err.no Git - linux-2.6/blob - drivers/net/wireless/ath5k/hw.c
a4e312d4226e6f724fd30ad717bc2213a012f46b
[linux-2.6] / drivers / net / wireless / ath5k / hw.c
1  /*
2  * Copyright (c) 2004-2007 Reyk Floeter <reyk@openbsd.org>
3  * Copyright (c) 2006-2007 Nick Kossifidis <mickflemm@gmail.com>
4  * Copyright (c) 2007 Matthew W. S. Bell  <mentor@madwifi.org>
5  * Copyright (c) 2007 Luis Rodriguez <mcgrof@winlab.rutgers.edu>
6  * Copyright (c) 2007 Pavel Roskin <proski@gnu.org>
7  * Copyright (c) 2007 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  * HW related functions for Atheros Wireless LAN devices.
25  */
26
27 #include <linux/pci.h>
28 #include <linux/delay.h>
29
30 #include "reg.h"
31 #include "base.h"
32 #include "debug.h"
33
34 /*Rate tables*/
35 static const struct ath5k_rate_table ath5k_rt_11a = AR5K_RATES_11A;
36 static const struct ath5k_rate_table ath5k_rt_11b = AR5K_RATES_11B;
37 static const struct ath5k_rate_table ath5k_rt_11g = AR5K_RATES_11G;
38 static const struct ath5k_rate_table ath5k_rt_turbo = AR5K_RATES_TURBO;
39 static const struct ath5k_rate_table ath5k_rt_xr = AR5K_RATES_XR;
40
41 /*Prototypes*/
42 static int ath5k_hw_nic_reset(struct ath5k_hw *, u32);
43 static int ath5k_hw_nic_wakeup(struct ath5k_hw *, int, bool);
44 static int ath5k_hw_setup_4word_tx_desc(struct ath5k_hw *, struct ath5k_desc *,
45         unsigned int, unsigned int, enum ath5k_pkt_type, unsigned int,
46         unsigned int, unsigned int, unsigned int, unsigned int, unsigned int,
47         unsigned int, unsigned int);
48 static int ath5k_hw_setup_xr_tx_desc(struct ath5k_hw *, struct ath5k_desc *,
49         unsigned int, unsigned int, unsigned int, unsigned int, unsigned int,
50         unsigned int);
51 static int ath5k_hw_proc_4word_tx_status(struct ath5k_hw *, struct ath5k_desc *,
52                                          struct ath5k_tx_status *);
53 static int ath5k_hw_setup_2word_tx_desc(struct ath5k_hw *, struct ath5k_desc *,
54         unsigned int, unsigned int, enum ath5k_pkt_type, unsigned int,
55         unsigned int, unsigned int, unsigned int, unsigned int, unsigned int,
56         unsigned int, unsigned int);
57 static int ath5k_hw_proc_2word_tx_status(struct ath5k_hw *, struct ath5k_desc *,
58                                          struct ath5k_tx_status *);
59 static int ath5k_hw_proc_5212_rx_status(struct ath5k_hw *, struct ath5k_desc *,
60                                         struct ath5k_rx_status *);
61 static int ath5k_hw_proc_5210_rx_status(struct ath5k_hw *, struct ath5k_desc *,
62                                         struct ath5k_rx_status *);
63 static int ath5k_hw_get_capabilities(struct ath5k_hw *);
64
65 static int ath5k_eeprom_init(struct ath5k_hw *);
66 static int ath5k_eeprom_read_mac(struct ath5k_hw *, u8 *);
67
68 static int ath5k_hw_enable_pspoll(struct ath5k_hw *, u8 *, u16);
69 static int ath5k_hw_disable_pspoll(struct ath5k_hw *);
70
71 /*
72  * Enable to overwrite the country code (use "00" for debug)
73  */
74 #if 0
75 #define COUNTRYCODE "00"
76 #endif
77
78 /*******************\
79   General Functions
80 \*******************/
81
82 /*
83  * Functions used internaly
84  */
85
86 static inline unsigned int ath5k_hw_htoclock(unsigned int usec, bool turbo)
87 {
88         return turbo == true ? (usec * 80) : (usec * 40);
89 }
90
91 static inline unsigned int ath5k_hw_clocktoh(unsigned int clock, bool turbo)
92 {
93         return turbo == true ? (clock / 80) : (clock / 40);
94 }
95
96 /*
97  * Check if a register write has been completed
98  */
99 int ath5k_hw_register_timeout(struct ath5k_hw *ah, u32 reg, u32 flag, u32 val,
100                 bool is_set)
101 {
102         int i;
103         u32 data;
104
105         for (i = AR5K_TUNE_REGISTER_TIMEOUT; i > 0; i--) {
106                 data = ath5k_hw_reg_read(ah, reg);
107                 if ((is_set == true) && (data & flag))
108                         break;
109                 else if ((data & flag) == val)
110                         break;
111                 udelay(15);
112         }
113
114         return (i <= 0) ? -EAGAIN : 0;
115 }
116
117
118 /***************************************\
119         Attach/Detach Functions
120 \***************************************/
121
122 /*
123  * Check if the device is supported and initialize the needed structs
124  */
125 struct ath5k_hw *ath5k_hw_attach(struct ath5k_softc *sc, u8 mac_version)
126 {
127         struct ath5k_hw *ah;
128         u8 mac[ETH_ALEN];
129         int ret;
130         u32 srev;
131
132         /*If we passed the test malloc a ath5k_hw struct*/
133         ah = kzalloc(sizeof(struct ath5k_hw), GFP_KERNEL);
134         if (ah == NULL) {
135                 ret = -ENOMEM;
136                 ATH5K_ERR(sc, "out of memory\n");
137                 goto err;
138         }
139
140         ah->ah_sc = sc;
141         ah->ah_iobase = sc->iobase;
142
143         /*
144          * HW information
145          */
146
147         ah->ah_op_mode = IEEE80211_IF_TYPE_STA;
148         ah->ah_radar.r_enabled = AR5K_TUNE_RADAR_ALERT;
149         ah->ah_turbo = false;
150         ah->ah_txpower.txp_tpc = AR5K_TUNE_TPC_TXPOWER;
151         ah->ah_imr = 0;
152         ah->ah_atim_window = 0;
153         ah->ah_aifs = AR5K_TUNE_AIFS;
154         ah->ah_cw_min = AR5K_TUNE_CWMIN;
155         ah->ah_limit_tx_retries = AR5K_INIT_TX_RETRY;
156         ah->ah_software_retry = false;
157         ah->ah_ant_diversity = AR5K_TUNE_ANT_DIVERSITY;
158
159         /*
160          * Set the mac revision based on the pci id
161          */
162         ah->ah_version = mac_version;
163
164         /*Fill the ath5k_hw struct with the needed functions*/
165         if (ah->ah_version == AR5K_AR5212)
166                 ah->ah_magic = AR5K_EEPROM_MAGIC_5212;
167         else if (ah->ah_version == AR5K_AR5211)
168                 ah->ah_magic = AR5K_EEPROM_MAGIC_5211;
169
170         if (ah->ah_version == AR5K_AR5212) {
171                 ah->ah_setup_tx_desc = ath5k_hw_setup_4word_tx_desc;
172                 ah->ah_setup_xtx_desc = ath5k_hw_setup_xr_tx_desc;
173                 ah->ah_proc_tx_desc = ath5k_hw_proc_4word_tx_status;
174         } else {
175                 ah->ah_setup_tx_desc = ath5k_hw_setup_2word_tx_desc;
176                 ah->ah_setup_xtx_desc = ath5k_hw_setup_xr_tx_desc;
177                 ah->ah_proc_tx_desc = ath5k_hw_proc_2word_tx_status;
178         }
179
180         if (ah->ah_version == AR5K_AR5212)
181                 ah->ah_proc_rx_desc = ath5k_hw_proc_5212_rx_status;
182         else if (ah->ah_version <= AR5K_AR5211)
183                 ah->ah_proc_rx_desc = ath5k_hw_proc_5210_rx_status;
184
185         /* Bring device out of sleep and reset it's units */
186         ret = ath5k_hw_nic_wakeup(ah, AR5K_INIT_MODE, true);
187         if (ret)
188                 goto err_free;
189
190         /* Get MAC, PHY and RADIO revisions */
191         srev = ath5k_hw_reg_read(ah, AR5K_SREV);
192         ah->ah_mac_srev = srev;
193         ah->ah_mac_version = AR5K_REG_MS(srev, AR5K_SREV_VER);
194         ah->ah_mac_revision = AR5K_REG_MS(srev, AR5K_SREV_REV);
195         ah->ah_phy_revision = ath5k_hw_reg_read(ah, AR5K_PHY_CHIP_ID) &
196                         0xffffffff;
197         ah->ah_radio_5ghz_revision = ath5k_hw_radio_revision(ah,
198                         CHANNEL_5GHZ);
199
200         if (ah->ah_version == AR5K_AR5210)
201                 ah->ah_radio_2ghz_revision = 0;
202         else
203                 ah->ah_radio_2ghz_revision = ath5k_hw_radio_revision(ah,
204                                 CHANNEL_2GHZ);
205
206         /* Return on unsuported chips (unsupported eeprom etc) */
207         if(srev >= AR5K_SREV_VER_AR5416){
208                 ATH5K_ERR(sc, "Device not yet supported.\n");
209                 ret = -ENODEV;
210                 goto err_free;
211         }
212
213         /* Identify single chip solutions */
214         if((srev <= AR5K_SREV_VER_AR5414) &&
215         (srev >= AR5K_SREV_VER_AR2413)) {
216                 ah->ah_single_chip = true;
217         } else {
218                 ah->ah_single_chip = false;
219         }
220
221         /* Single chip radio */
222         if (ah->ah_radio_2ghz_revision == ah->ah_radio_5ghz_revision)
223                 ah->ah_radio_2ghz_revision = 0;
224
225         /* Identify the radio chip*/
226         if (ah->ah_version == AR5K_AR5210) {
227                 ah->ah_radio = AR5K_RF5110;
228         } else if (ah->ah_radio_5ghz_revision < AR5K_SREV_RAD_5112) {
229                 ah->ah_radio = AR5K_RF5111;
230                 ah->ah_phy_spending = AR5K_PHY_SPENDING_RF5111;
231         } else if (ah->ah_radio_5ghz_revision < AR5K_SREV_RAD_SC0) {
232
233                 ah->ah_radio = AR5K_RF5112;
234
235                 if (ah->ah_radio_5ghz_revision < AR5K_SREV_RAD_5112A) {
236                         ah->ah_phy_spending = AR5K_PHY_SPENDING_RF5112;
237                 } else {
238                         ah->ah_phy_spending = AR5K_PHY_SPENDING_RF5112A;
239                 }
240
241         } else if (ah->ah_radio_5ghz_revision < AR5K_SREV_RAD_SC1) {
242                 ah->ah_radio = AR5K_RF2413;
243                 ah->ah_phy_spending = AR5K_PHY_SPENDING_RF5112A;
244         } else {
245
246                 ah->ah_radio = AR5K_RF5413;
247
248                 if (ah->ah_mac_srev <= AR5K_SREV_VER_AR5424 &&
249                         ah->ah_mac_srev >= AR5K_SREV_VER_AR2424)
250                         ah->ah_phy_spending = AR5K_PHY_SPENDING_RF5424;
251                 else if (ah->ah_mac_srev >= AR5K_SREV_VER_AR2425)
252                         ah->ah_phy_spending = AR5K_PHY_SPENDING_RF5112;
253                 else
254                         ah->ah_phy_spending = AR5K_PHY_SPENDING_RF5112A;
255
256
257         }
258
259         ah->ah_phy = AR5K_PHY(0);
260
261         /*
262          * Get card capabilities, values, ...
263          */
264
265         ret = ath5k_eeprom_init(ah);
266         if (ret) {
267                 ATH5K_ERR(sc, "unable to init EEPROM\n");
268                 goto err_free;
269         }
270
271         /* Get misc capabilities */
272         ret = ath5k_hw_get_capabilities(ah);
273         if (ret) {
274                 ATH5K_ERR(sc, "unable to get device capabilities: 0x%04x\n",
275                         sc->pdev->device);
276                 goto err_free;
277         }
278
279         /* Get MAC address */
280         ret = ath5k_eeprom_read_mac(ah, mac);
281         if (ret) {
282                 ATH5K_ERR(sc, "unable to read address from EEPROM: 0x%04x\n",
283                         sc->pdev->device);
284                 goto err_free;
285         }
286
287         ath5k_hw_set_lladdr(ah, mac);
288         /* Set BSSID to bcast address: ff:ff:ff:ff:ff:ff for now */
289         memset(ah->ah_bssid, 0xff, ETH_ALEN);
290         ath5k_hw_set_associd(ah, ah->ah_bssid, 0);
291         ath5k_hw_set_opmode(ah);
292
293         ath5k_hw_set_rfgain_opt(ah);
294
295         return ah;
296 err_free:
297         kfree(ah);
298 err:
299         return ERR_PTR(ret);
300 }
301
302 /*
303  * Bring up MAC + PHY Chips
304  */
305 static int ath5k_hw_nic_wakeup(struct ath5k_hw *ah, int flags, bool initial)
306 {
307         struct pci_dev *pdev = ah->ah_sc->pdev;
308         u32 turbo, mode, clock, bus_flags;
309         int ret;
310
311         turbo = 0;
312         mode = 0;
313         clock = 0;
314
315         ATH5K_TRACE(ah->ah_sc);
316
317         /* Wakeup the device */
318         ret = ath5k_hw_set_power(ah, AR5K_PM_AWAKE, true, 0);
319         if (ret) {
320                 ATH5K_ERR(ah->ah_sc, "failed to wakeup the MAC Chip\n");
321                 return ret;
322         }
323
324         if (ah->ah_version != AR5K_AR5210) {
325                 /*
326                  * Get channel mode flags
327                  */
328
329                 if (ah->ah_radio >= AR5K_RF5112) {
330                         mode = AR5K_PHY_MODE_RAD_RF5112;
331                         clock = AR5K_PHY_PLL_RF5112;
332                 } else {
333                         mode = AR5K_PHY_MODE_RAD_RF5111;        /*Zero*/
334                         clock = AR5K_PHY_PLL_RF5111;            /*Zero*/
335                 }
336
337                 if (flags & CHANNEL_2GHZ) {
338                         mode |= AR5K_PHY_MODE_FREQ_2GHZ;
339                         clock |= AR5K_PHY_PLL_44MHZ;
340
341                         if (flags & CHANNEL_CCK) {
342                                 mode |= AR5K_PHY_MODE_MOD_CCK;
343                         } else if (flags & CHANNEL_OFDM) {
344                                 /* XXX Dynamic OFDM/CCK is not supported by the
345                                  * AR5211 so we set MOD_OFDM for plain g (no
346                                  * CCK headers) operation. We need to test
347                                  * this, 5211 might support ofdm-only g after
348                                  * all, there are also initial register values
349                                  * in the code for g mode (see initvals.c). */
350                                 if (ah->ah_version == AR5K_AR5211)
351                                         mode |= AR5K_PHY_MODE_MOD_OFDM;
352                                 else
353                                         mode |= AR5K_PHY_MODE_MOD_DYN;
354                         } else {
355                                 ATH5K_ERR(ah->ah_sc,
356                                         "invalid radio modulation mode\n");
357                                 return -EINVAL;
358                         }
359                 } else if (flags & CHANNEL_5GHZ) {
360                         mode |= AR5K_PHY_MODE_FREQ_5GHZ;
361                         clock |= AR5K_PHY_PLL_40MHZ;
362
363                         if (flags & CHANNEL_OFDM)
364                                 mode |= AR5K_PHY_MODE_MOD_OFDM;
365                         else {
366                                 ATH5K_ERR(ah->ah_sc,
367                                         "invalid radio modulation mode\n");
368                                 return -EINVAL;
369                         }
370                 } else {
371                         ATH5K_ERR(ah->ah_sc, "invalid radio frequency mode\n");
372                         return -EINVAL;
373                 }
374
375                 if (flags & CHANNEL_TURBO)
376                         turbo = AR5K_PHY_TURBO_MODE | AR5K_PHY_TURBO_SHORT;
377         } else { /* Reset the device */
378
379                 /* ...enable Atheros turbo mode if requested */
380                 if (flags & CHANNEL_TURBO)
381                         ath5k_hw_reg_write(ah, AR5K_PHY_TURBO_MODE,
382                                         AR5K_PHY_TURBO);
383         }
384
385         /* reseting PCI on PCI-E cards results card to hang
386          * and always return 0xffff... so we ingore that flag
387          * for PCI-E cards */
388         bus_flags = (pdev->is_pcie) ? 0 : AR5K_RESET_CTL_PCI;
389
390         /* Reset chipset */
391         ret = ath5k_hw_nic_reset(ah, AR5K_RESET_CTL_PCU |
392                 AR5K_RESET_CTL_BASEBAND | bus_flags);
393         if (ret) {
394                 ATH5K_ERR(ah->ah_sc, "failed to reset the MAC Chip + PCI\n");
395                 return -EIO;
396         }
397
398         if (ah->ah_version == AR5K_AR5210)
399                 udelay(2300);
400
401         /* ...wakeup again!*/
402         ret = ath5k_hw_set_power(ah, AR5K_PM_AWAKE, true, 0);
403         if (ret) {
404                 ATH5K_ERR(ah->ah_sc, "failed to resume the MAC Chip\n");
405                 return ret;
406         }
407
408         /* ...final warm reset */
409         if (ath5k_hw_nic_reset(ah, 0)) {
410                 ATH5K_ERR(ah->ah_sc, "failed to warm reset the MAC Chip\n");
411                 return -EIO;
412         }
413
414         if (ah->ah_version != AR5K_AR5210) {
415                 /* ...set the PHY operating mode */
416                 ath5k_hw_reg_write(ah, clock, AR5K_PHY_PLL);
417                 udelay(300);
418
419                 ath5k_hw_reg_write(ah, mode, AR5K_PHY_MODE);
420                 ath5k_hw_reg_write(ah, turbo, AR5K_PHY_TURBO);
421         }
422
423         return 0;
424 }
425
426 /*
427  * Get the rate table for a specific operation mode
428  */
429 const struct ath5k_rate_table *ath5k_hw_get_rate_table(struct ath5k_hw *ah,
430                 unsigned int mode)
431 {
432         ATH5K_TRACE(ah->ah_sc);
433
434         if (!test_bit(mode, ah->ah_capabilities.cap_mode))
435                 return NULL;
436
437         /* Get rate tables */
438         switch (mode) {
439         case AR5K_MODE_11A:
440                 return &ath5k_rt_11a;
441         case AR5K_MODE_11A_TURBO:
442                 return &ath5k_rt_turbo;
443         case AR5K_MODE_11B:
444                 return &ath5k_rt_11b;
445         case AR5K_MODE_11G:
446                 return &ath5k_rt_11g;
447         case AR5K_MODE_11G_TURBO:
448                 return &ath5k_rt_xr;
449         }
450
451         return NULL;
452 }
453
454 /*
455  * Free the ath5k_hw struct
456  */
457 void ath5k_hw_detach(struct ath5k_hw *ah)
458 {
459         ATH5K_TRACE(ah->ah_sc);
460
461         if (ah->ah_rf_banks != NULL)
462                 kfree(ah->ah_rf_banks);
463
464         /* assume interrupts are down */
465         kfree(ah);
466 }
467
468 /****************************\
469   Reset function and helpers
470 \****************************/
471
472 /**
473  * ath5k_hw_write_ofdm_timings - set OFDM timings on AR5212
474  *
475  * @ah: the &struct ath5k_hw
476  * @channel: the currently set channel upon reset
477  *
478  * Write the OFDM timings for the AR5212 upon reset. This is a helper for
479  * ath5k_hw_reset(). This seems to tune the PLL a specified frequency
480  * depending on the bandwidth of the channel.
481  *
482  */
483 static inline int ath5k_hw_write_ofdm_timings(struct ath5k_hw *ah,
484         struct ieee80211_channel *channel)
485 {
486         /* Get exponent and mantissa and set it */
487         u32 coef_scaled, coef_exp, coef_man,
488                 ds_coef_exp, ds_coef_man, clock;
489
490         if (!(ah->ah_version == AR5K_AR5212) ||
491                 !(channel->hw_value & CHANNEL_OFDM))
492                 BUG();
493
494         /* Seems there are two PLLs, one for baseband sampling and one
495          * for tuning. Tuning basebands are 40 MHz or 80MHz when in
496          * turbo. */
497         clock = channel->hw_value & CHANNEL_TURBO ? 80 : 40;
498         coef_scaled = ((5 * (clock << 24)) / 2) /
499         channel->center_freq;
500
501         for (coef_exp = 31; coef_exp > 0; coef_exp--)
502                 if ((coef_scaled >> coef_exp) & 0x1)
503                         break;
504
505         if (!coef_exp)
506                 return -EINVAL;
507
508         coef_exp = 14 - (coef_exp - 24);
509         coef_man = coef_scaled +
510                 (1 << (24 - coef_exp - 1));
511         ds_coef_man = coef_man >> (24 - coef_exp);
512         ds_coef_exp = coef_exp - 16;
513
514         AR5K_REG_WRITE_BITS(ah, AR5K_PHY_TIMING_3,
515                 AR5K_PHY_TIMING_3_DSC_MAN, ds_coef_man);
516         AR5K_REG_WRITE_BITS(ah, AR5K_PHY_TIMING_3,
517                 AR5K_PHY_TIMING_3_DSC_EXP, ds_coef_exp);
518
519         return 0;
520 }
521
522 /**
523  * ath5k_hw_write_rate_duration - set rate duration during hw resets
524  *
525  * @ah: the &struct ath5k_hw
526  * @mode: one of enum ath5k_driver_mode
527  *
528  * Write the rate duration table for the current mode upon hw reset. This
529  * is a helper for ath5k_hw_reset(). It seems all this is doing is setting
530  * an ACK timeout for the hardware for the current mode for each rate. The
531  * rates which are capable of short preamble (802.11b rates 2Mbps, 5.5Mbps,
532  * and 11Mbps) have another register for the short preamble ACK timeout
533  * calculation.
534  *
535  */
536 static inline void ath5k_hw_write_rate_duration(struct ath5k_hw *ah,
537        unsigned int mode)
538 {
539         struct ath5k_softc *sc = ah->ah_sc;
540         const struct ath5k_rate_table *rt;
541         struct ieee80211_rate srate = {};
542         unsigned int i;
543
544         /* Get rate table for the current operating mode */
545         rt = ath5k_hw_get_rate_table(ah, mode);
546
547         /* Write rate duration table */
548         for (i = 0; i < rt->rate_count; i++) {
549                 const struct ath5k_rate *rate, *control_rate;
550
551                 u32 reg;
552                 u16 tx_time;
553
554                 rate = &rt->rates[i];
555                 control_rate = &rt->rates[rate->control_rate];
556
557                 /* Set ACK timeout */
558                 reg = AR5K_RATE_DUR(rate->rate_code);
559
560                 srate.bitrate = control_rate->rate_kbps/100;
561
562                 /* An ACK frame consists of 10 bytes. If you add the FCS,
563                  * which ieee80211_generic_frame_duration() adds,
564                  * its 14 bytes. Note we use the control rate and not the
565                  * actual rate for this rate. See mac80211 tx.c
566                  * ieee80211_duration() for a brief description of
567                  * what rate we should choose to TX ACKs. */
568                 tx_time = le16_to_cpu(ieee80211_generic_frame_duration(sc->hw,
569                                                         sc->vif, 10, &srate));
570
571                 ath5k_hw_reg_write(ah, tx_time, reg);
572
573                 if (!HAS_SHPREAMBLE(i))
574                         continue;
575
576                 /*
577                  * We're not distinguishing short preamble here,
578                  * This is true, all we'll get is a longer value here
579                  * which is not necessarilly bad. We could use
580                  * export ieee80211_frame_duration() but that needs to be
581                  * fixed first to be properly used by mac802111 drivers:
582                  *
583                  *  - remove erp stuff and let the routine figure ofdm
584                  *    erp rates
585                  *  - remove passing argument ieee80211_local as
586                  *    drivers don't have access to it
587                  *  - move drivers using ieee80211_generic_frame_duration()
588                  *    to this
589                  */
590                 ath5k_hw_reg_write(ah, tx_time,
591                         reg + (AR5K_SET_SHORT_PREAMBLE << 2));
592         }
593 }
594
595 /*
596  * Main reset function
597  */
598 int ath5k_hw_reset(struct ath5k_hw *ah, enum ieee80211_if_types op_mode,
599         struct ieee80211_channel *channel, bool change_channel)
600 {
601         struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
602         struct pci_dev *pdev = ah->ah_sc->pdev;
603         u32 data, s_seq, s_ant, s_led[3], dma_size;
604         unsigned int i, mode, freq, ee_mode, ant[2];
605         int ret;
606
607         ATH5K_TRACE(ah->ah_sc);
608
609         s_seq = 0;
610         s_ant = 0;
611         ee_mode = 0;
612         freq = 0;
613         mode = 0;
614
615         /*
616          * Save some registers before a reset
617          */
618         /*DCU/Antenna selection not available on 5210*/
619         if (ah->ah_version != AR5K_AR5210) {
620                 if (change_channel == true) {
621                         /* Seq number for queue 0 -do this for all queues ? */
622                         s_seq = ath5k_hw_reg_read(ah,
623                                         AR5K_QUEUE_DFS_SEQNUM(0));
624                         /*Default antenna*/
625                         s_ant = ath5k_hw_reg_read(ah, AR5K_DEFAULT_ANTENNA);
626                 }
627         }
628
629         /*GPIOs*/
630         s_led[0] = ath5k_hw_reg_read(ah, AR5K_PCICFG) & AR5K_PCICFG_LEDSTATE;
631         s_led[1] = ath5k_hw_reg_read(ah, AR5K_GPIOCR);
632         s_led[2] = ath5k_hw_reg_read(ah, AR5K_GPIODO);
633
634         if (change_channel == true && ah->ah_rf_banks != NULL)
635                 ath5k_hw_get_rf_gain(ah);
636
637
638         /*Wakeup the device*/
639         ret = ath5k_hw_nic_wakeup(ah, channel->hw_value, false);
640         if (ret)
641                 return ret;
642
643         /*
644          * Initialize operating mode
645          */
646         ah->ah_op_mode = op_mode;
647
648         /*
649          * 5111/5112 Settings
650          * 5210 only comes with RF5110
651          */
652         if (ah->ah_version != AR5K_AR5210) {
653                 if (ah->ah_radio != AR5K_RF5111 &&
654                         ah->ah_radio != AR5K_RF5112 &&
655                         ah->ah_radio != AR5K_RF5413 &&
656                         ah->ah_radio != AR5K_RF2413) {
657                         ATH5K_ERR(ah->ah_sc,
658                                 "invalid phy radio: %u\n", ah->ah_radio);
659                         return -EINVAL;
660                 }
661
662                 switch (channel->hw_value & CHANNEL_MODES) {
663                 case CHANNEL_A:
664                         mode = AR5K_MODE_11A;
665                         freq = AR5K_INI_RFGAIN_5GHZ;
666                         ee_mode = AR5K_EEPROM_MODE_11A;
667                         break;
668                 case CHANNEL_G:
669                         mode = AR5K_MODE_11G;
670                         freq = AR5K_INI_RFGAIN_2GHZ;
671                         ee_mode = AR5K_EEPROM_MODE_11G;
672                         break;
673                 case CHANNEL_B:
674                         mode = AR5K_MODE_11B;
675                         freq = AR5K_INI_RFGAIN_2GHZ;
676                         ee_mode = AR5K_EEPROM_MODE_11B;
677                         break;
678                 case CHANNEL_T:
679                         mode = AR5K_MODE_11A_TURBO;
680                         freq = AR5K_INI_RFGAIN_5GHZ;
681                         ee_mode = AR5K_EEPROM_MODE_11A;
682                         break;
683                 /*Is this ok on 5211 too ?*/
684                 case CHANNEL_TG:
685                         mode = AR5K_MODE_11G_TURBO;
686                         freq = AR5K_INI_RFGAIN_2GHZ;
687                         ee_mode = AR5K_EEPROM_MODE_11G;
688                         break;
689                 case CHANNEL_XR:
690                         if (ah->ah_version == AR5K_AR5211) {
691                                 ATH5K_ERR(ah->ah_sc,
692                                         "XR mode not available on 5211");
693                                 return -EINVAL;
694                         }
695                         mode = AR5K_MODE_XR;
696                         freq = AR5K_INI_RFGAIN_5GHZ;
697                         ee_mode = AR5K_EEPROM_MODE_11A;
698                         break;
699                 default:
700                         ATH5K_ERR(ah->ah_sc,
701                                 "invalid channel: %d\n", channel->center_freq);
702                         return -EINVAL;
703                 }
704
705                 /* PHY access enable */
706                 ath5k_hw_reg_write(ah, AR5K_PHY_SHIFT_5GHZ, AR5K_PHY(0));
707
708         }
709
710         ret = ath5k_hw_write_initvals(ah, mode, change_channel);
711         if (ret)
712                 return ret;
713
714         /*
715          * 5211/5212 Specific
716          */
717         if (ah->ah_version != AR5K_AR5210) {
718                 /*
719                  * Write initial RF gain settings
720                  * This should work for both 5111/5112
721                  */
722                 ret = ath5k_hw_rfgain(ah, freq);
723                 if (ret)
724                         return ret;
725
726                 mdelay(1);
727
728                 /*
729                  * Write some more initial register settings
730                  */
731                 if (ah->ah_version == AR5K_AR5212) {
732                         ath5k_hw_reg_write(ah, 0x0002a002, AR5K_PHY(11));
733
734                         if (channel->hw_value == CHANNEL_G)
735                                 if (ah->ah_mac_srev < AR5K_SREV_VER_AR2413)
736                                         ath5k_hw_reg_write(ah, 0x00f80d80,
737                                                 AR5K_PHY(83));
738                                 else if (ah->ah_mac_srev < AR5K_SREV_VER_AR2424)
739                                         ath5k_hw_reg_write(ah, 0x00380140,
740                                                 AR5K_PHY(83));
741                                 else if (ah->ah_mac_srev < AR5K_SREV_VER_AR2425)
742                                         ath5k_hw_reg_write(ah, 0x00fc0ec0,
743                                                 AR5K_PHY(83));
744                                 else /* 2425 */
745                                         ath5k_hw_reg_write(ah, 0x00fc0fc0,
746                                                 AR5K_PHY(83));
747                         else
748                                 ath5k_hw_reg_write(ah, 0x00000000,
749                                         AR5K_PHY(83));
750
751                         ath5k_hw_reg_write(ah, 0x000009b5, 0xa228);
752                         ath5k_hw_reg_write(ah, 0x0000000f, 0x8060);
753                         ath5k_hw_reg_write(ah, 0x00000000, 0xa254);
754                         ath5k_hw_reg_write(ah, 0x0000000e, AR5K_PHY_SCAL);
755                 }
756
757                 /* Fix for first revision of the RF5112 RF chipset */
758                 if (ah->ah_radio >= AR5K_RF5112 &&
759                                 ah->ah_radio_5ghz_revision <
760                                 AR5K_SREV_RAD_5112A) {
761                         ath5k_hw_reg_write(ah, AR5K_PHY_CCKTXCTL_WORLD,
762                                         AR5K_PHY_CCKTXCTL);
763                         if (channel->hw_value & CHANNEL_5GHZ)
764                                 data = 0xffb81020;
765                         else
766                                 data = 0xffb80d20;
767                         ath5k_hw_reg_write(ah, data, AR5K_PHY_FRAME_CTL);
768                 }
769
770                 /*
771                  * Set TX power (FIXME)
772                  */
773                 ret = ath5k_hw_txpower(ah, channel, AR5K_TUNE_DEFAULT_TXPOWER);
774                 if (ret)
775                         return ret;
776
777                 /* Write rate duration table only on AR5212 and if
778                  * virtual interface has already been brought up
779                  * XXX: rethink this after new mode changes to
780                  * mac80211 are integrated */
781                 if (ah->ah_version == AR5K_AR5212 &&
782                         ah->ah_sc->vif != NULL)
783                         ath5k_hw_write_rate_duration(ah, mode);
784
785                 /*
786                  * Write RF registers
787                  * TODO:Does this work on 5211 (5111) ?
788                  */
789                 ret = ath5k_hw_rfregs(ah, channel, mode);
790                 if (ret)
791                         return ret;
792
793                 /*
794                  * Configure additional registers
795                  */
796
797                 /* Write OFDM timings on 5212*/
798                 if (ah->ah_version == AR5K_AR5212 &&
799                         channel->hw_value & CHANNEL_OFDM) {
800                         ret = ath5k_hw_write_ofdm_timings(ah, channel);
801                         if (ret)
802                                 return ret;
803                 }
804
805                 /*Enable/disable 802.11b mode on 5111
806                 (enable 2111 frequency converter + CCK)*/
807                 if (ah->ah_radio == AR5K_RF5111) {
808                         if (mode == AR5K_MODE_11B)
809                                 AR5K_REG_ENABLE_BITS(ah, AR5K_TXCFG,
810                                     AR5K_TXCFG_B_MODE);
811                         else
812                                 AR5K_REG_DISABLE_BITS(ah, AR5K_TXCFG,
813                                     AR5K_TXCFG_B_MODE);
814                 }
815
816                 /*
817                  * Set channel and calibrate the PHY
818                  */
819                 ret = ath5k_hw_channel(ah, channel);
820                 if (ret)
821                         return ret;
822
823                 /* Set antenna mode */
824                 AR5K_REG_MASKED_BITS(ah, AR5K_PHY(0x44),
825                         ah->ah_antenna[ee_mode][0], 0xfffffc06);
826
827                 /*
828                  * In case a fixed antenna was set as default
829                  * write the same settings on both AR5K_PHY_ANT_SWITCH_TABLE
830                  * registers.
831                  */
832                 if (s_ant != 0){
833                         if (s_ant == AR5K_ANT_FIXED_A) /* 1 - Main */
834                                 ant[0] = ant[1] = AR5K_ANT_FIXED_A;
835                         else    /* 2 - Aux */
836                                 ant[0] = ant[1] = AR5K_ANT_FIXED_B;
837                 } else {
838                         ant[0] = AR5K_ANT_FIXED_A;
839                         ant[1] = AR5K_ANT_FIXED_B;
840                 }
841
842                 ath5k_hw_reg_write(ah, ah->ah_antenna[ee_mode][ant[0]],
843                         AR5K_PHY_ANT_SWITCH_TABLE_0);
844                 ath5k_hw_reg_write(ah, ah->ah_antenna[ee_mode][ant[1]],
845                         AR5K_PHY_ANT_SWITCH_TABLE_1);
846
847                 /* Commit values from EEPROM */
848                 if (ah->ah_radio == AR5K_RF5111)
849                         AR5K_REG_WRITE_BITS(ah, AR5K_PHY_FRAME_CTL,
850                             AR5K_PHY_FRAME_CTL_TX_CLIP, ee->ee_tx_clip);
851
852                 ath5k_hw_reg_write(ah,
853                         AR5K_PHY_NF_SVAL(ee->ee_noise_floor_thr[ee_mode]),
854                         AR5K_PHY(0x5a));
855
856                 AR5K_REG_MASKED_BITS(ah, AR5K_PHY(0x11),
857                         (ee->ee_switch_settling[ee_mode] << 7) & 0x3f80,
858                         0xffffc07f);
859                 AR5K_REG_MASKED_BITS(ah, AR5K_PHY(0x12),
860                         (ee->ee_ant_tx_rx[ee_mode] << 12) & 0x3f000,
861                         0xfffc0fff);
862                 AR5K_REG_MASKED_BITS(ah, AR5K_PHY(0x14),
863                         (ee->ee_adc_desired_size[ee_mode] & 0x00ff) |
864                         ((ee->ee_pga_desired_size[ee_mode] << 8) & 0xff00),
865                         0xffff0000);
866
867                 ath5k_hw_reg_write(ah,
868                         (ee->ee_tx_end2xpa_disable[ee_mode] << 24) |
869                         (ee->ee_tx_end2xpa_disable[ee_mode] << 16) |
870                         (ee->ee_tx_frm2xpa_enable[ee_mode] << 8) |
871                         (ee->ee_tx_frm2xpa_enable[ee_mode]), AR5K_PHY(0x0d));
872
873                 AR5K_REG_MASKED_BITS(ah, AR5K_PHY(0x0a),
874                         ee->ee_tx_end2xlna_enable[ee_mode] << 8, 0xffff00ff);
875                 AR5K_REG_MASKED_BITS(ah, AR5K_PHY(0x19),
876                         (ee->ee_thr_62[ee_mode] << 12) & 0x7f000, 0xfff80fff);
877                 AR5K_REG_MASKED_BITS(ah, AR5K_PHY(0x49), 4, 0xffffff01);
878
879                 AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_IQ,
880                     AR5K_PHY_IQ_CORR_ENABLE |
881                     (ee->ee_i_cal[ee_mode] << AR5K_PHY_IQ_CORR_Q_I_COFF_S) |
882                     ee->ee_q_cal[ee_mode]);
883
884                 if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_4_1)
885                         AR5K_REG_WRITE_BITS(ah, AR5K_PHY_GAIN_2GHZ,
886                                 AR5K_PHY_GAIN_2GHZ_MARGIN_TXRX,
887                                 ee->ee_margin_tx_rx[ee_mode]);
888
889         } else {
890                 mdelay(1);
891                 /* Disable phy and wait */
892                 ath5k_hw_reg_write(ah, AR5K_PHY_ACT_DISABLE, AR5K_PHY_ACT);
893                 mdelay(1);
894         }
895
896         /*
897          * Restore saved values
898          */
899         /*DCU/Antenna selection not available on 5210*/
900         if (ah->ah_version != AR5K_AR5210) {
901                 ath5k_hw_reg_write(ah, s_seq, AR5K_QUEUE_DFS_SEQNUM(0));
902                 ath5k_hw_reg_write(ah, s_ant, AR5K_DEFAULT_ANTENNA);
903         }
904         AR5K_REG_ENABLE_BITS(ah, AR5K_PCICFG, s_led[0]);
905         ath5k_hw_reg_write(ah, s_led[1], AR5K_GPIOCR);
906         ath5k_hw_reg_write(ah, s_led[2], AR5K_GPIODO);
907
908         /*
909          * Misc
910          */
911         /* XXX: add ah->aid once mac80211 gives this to us */
912         ath5k_hw_set_associd(ah, ah->ah_bssid, 0);
913
914         ath5k_hw_set_opmode(ah);
915         /*PISR/SISR Not available on 5210*/
916         if (ah->ah_version != AR5K_AR5210) {
917                 ath5k_hw_reg_write(ah, 0xffffffff, AR5K_PISR);
918                 /* If we later allow tuning for this, store into sc structure */
919                 data = AR5K_TUNE_RSSI_THRES |
920                         AR5K_TUNE_BMISS_THRES << AR5K_RSSI_THR_BMISS_S;
921                 ath5k_hw_reg_write(ah, data, AR5K_RSSI_THR);
922         }
923
924         /*
925          * Set Rx/Tx DMA Configuration
926          *
927          * Set maximum DMA size (512) except for PCI-E cards since
928          * it causes rx overruns and tx errors (tested on 5424 but since
929          * rx overruns also occur on 5416/5418 with madwifi we set 128
930          * for all PCI-E cards to be safe).
931          *
932          * In dumps this is 128 for allchips.
933          *
934          * XXX: need to check 5210 for this
935          * TODO: Check out tx triger level, it's always 64 on dumps but I
936          * guess we can tweak it and see how it goes ;-)
937          */
938         dma_size = (pdev->is_pcie) ? AR5K_DMASIZE_128B : AR5K_DMASIZE_512B;
939         if (ah->ah_version != AR5K_AR5210) {
940                 AR5K_REG_WRITE_BITS(ah, AR5K_TXCFG,
941                         AR5K_TXCFG_SDMAMR, dma_size);
942                 AR5K_REG_WRITE_BITS(ah, AR5K_RXCFG,
943                         AR5K_RXCFG_SDMAMW, dma_size);
944         }
945
946         /*
947          * Enable the PHY and wait until completion
948          */
949         ath5k_hw_reg_write(ah, AR5K_PHY_ACT_ENABLE, AR5K_PHY_ACT);
950
951         /*
952          * 5111/5112 Specific
953          */
954         if (ah->ah_version != AR5K_AR5210) {
955                 data = ath5k_hw_reg_read(ah, AR5K_PHY_RX_DELAY) &
956                         AR5K_PHY_RX_DELAY_M;
957                 data = (channel->hw_value & CHANNEL_CCK) ?
958                         ((data << 2) / 22) : (data / 10);
959
960                 udelay(100 + data);
961         } else {
962                 mdelay(1);
963         }
964
965         /*
966          * Enable calibration and wait until completion
967          */
968         AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_AGCCTL,
969                                 AR5K_PHY_AGCCTL_CAL);
970
971         if (ath5k_hw_register_timeout(ah, AR5K_PHY_AGCCTL,
972                         AR5K_PHY_AGCCTL_CAL, 0, false)) {
973                 ATH5K_ERR(ah->ah_sc, "calibration timeout (%uMHz)\n",
974                         channel->center_freq);
975                 return -EAGAIN;
976         }
977
978         ret = ath5k_hw_noise_floor_calibration(ah, channel->center_freq);
979         if (ret)
980                 return ret;
981
982         ah->ah_calibration = false;
983
984         /* A and G modes can use QAM modulation which requires enabling
985          * I and Q calibration. Don't bother in B mode. */
986         if (!(mode == AR5K_MODE_11B)) {
987                 ah->ah_calibration = true;
988                 AR5K_REG_WRITE_BITS(ah, AR5K_PHY_IQ,
989                                 AR5K_PHY_IQ_CAL_NUM_LOG_MAX, 15);
990                 AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_IQ,
991                                 AR5K_PHY_IQ_RUN);
992         }
993
994         /*
995          * Reset queues and start beacon timers at the end of the reset routine
996          */
997         for (i = 0; i < ah->ah_capabilities.cap_queues.q_tx_num; i++) {
998                 /*No QCU on 5210*/
999                 if (ah->ah_version != AR5K_AR5210)
1000                         AR5K_REG_WRITE_Q(ah, AR5K_QUEUE_QCUMASK(i), i);
1001
1002                 ret = ath5k_hw_reset_tx_queue(ah, i);
1003                 if (ret) {
1004                         ATH5K_ERR(ah->ah_sc,
1005                                 "failed to reset TX queue #%d\n", i);
1006                         return ret;
1007                 }
1008         }
1009
1010         /* Pre-enable interrupts on 5211/5212*/
1011         if (ah->ah_version != AR5K_AR5210)
1012                 ath5k_hw_set_intr(ah, AR5K_INT_RX | AR5K_INT_TX |
1013                                 AR5K_INT_FATAL);
1014
1015         /*
1016          * Set RF kill flags if supported by the device (read from the EEPROM)
1017          * Disable gpio_intr for now since it results system hang.
1018          * TODO: Handle this in ath5k_intr
1019          */
1020 #if 0
1021         if (AR5K_EEPROM_HDR_RFKILL(ah->ah_capabilities.cap_eeprom.ee_header)) {
1022                 ath5k_hw_set_gpio_input(ah, 0);
1023                 ah->ah_gpio[0] = ath5k_hw_get_gpio(ah, 0);
1024                 if (ah->ah_gpio[0] == 0)
1025                         ath5k_hw_set_gpio_intr(ah, 0, 1);
1026                 else
1027                         ath5k_hw_set_gpio_intr(ah, 0, 0);
1028         }
1029 #endif
1030
1031         /*
1032          * Set the 32MHz reference clock on 5212 phy clock sleep register
1033          *
1034          * TODO: Find out how to switch to external 32Khz clock to save power
1035          */
1036         if (ah->ah_version == AR5K_AR5212) {
1037                 ath5k_hw_reg_write(ah, AR5K_PHY_SCR_32MHZ, AR5K_PHY_SCR);
1038                 ath5k_hw_reg_write(ah, AR5K_PHY_SLMT_32MHZ, AR5K_PHY_SLMT);
1039                 ath5k_hw_reg_write(ah, AR5K_PHY_SCAL_32MHZ, AR5K_PHY_SCAL);
1040                 ath5k_hw_reg_write(ah, AR5K_PHY_SCLOCK_32MHZ, AR5K_PHY_SCLOCK);
1041                 ath5k_hw_reg_write(ah, AR5K_PHY_SDELAY_32MHZ, AR5K_PHY_SDELAY);
1042                 ath5k_hw_reg_write(ah, ah->ah_phy_spending, AR5K_PHY_SPENDING);
1043         }
1044
1045         if (ah->ah_version == AR5K_AR5212) {
1046                 ath5k_hw_reg_write(ah, 0x000100aa, 0x8118);
1047                 ath5k_hw_reg_write(ah, 0x00003210, 0x811c);
1048                 ath5k_hw_reg_write(ah, 0x00000052, 0x8108);
1049                 if (ah->ah_mac_srev >= AR5K_SREV_VER_AR2413)
1050                         ath5k_hw_reg_write(ah, 0x00000004, 0x8120);
1051         }
1052
1053         /*
1054          * Disable beacons and reset the register
1055          */
1056         AR5K_REG_DISABLE_BITS(ah, AR5K_BEACON, AR5K_BEACON_ENABLE |
1057                         AR5K_BEACON_RESET_TSF);
1058
1059         return 0;
1060 }
1061
1062 /*
1063  * Reset chipset
1064  */
1065 static int ath5k_hw_nic_reset(struct ath5k_hw *ah, u32 val)
1066 {
1067         int ret;
1068         u32 mask = val ? val : ~0U;
1069
1070         ATH5K_TRACE(ah->ah_sc);
1071
1072         /* Read-and-clear RX Descriptor Pointer*/
1073         ath5k_hw_reg_read(ah, AR5K_RXDP);
1074
1075         /*
1076          * Reset the device and wait until success
1077          */
1078         ath5k_hw_reg_write(ah, val, AR5K_RESET_CTL);
1079
1080         /* Wait at least 128 PCI clocks */
1081         udelay(15);
1082
1083         if (ah->ah_version == AR5K_AR5210) {
1084                 val &= AR5K_RESET_CTL_CHIP;
1085                 mask &= AR5K_RESET_CTL_CHIP;
1086         } else {
1087                 val &= AR5K_RESET_CTL_PCU | AR5K_RESET_CTL_BASEBAND;
1088                 mask &= AR5K_RESET_CTL_PCU | AR5K_RESET_CTL_BASEBAND;
1089         }
1090
1091         ret = ath5k_hw_register_timeout(ah, AR5K_RESET_CTL, mask, val, false);
1092
1093         /*
1094          * Reset configuration register (for hw byte-swap). Note that this
1095          * is only set for big endian. We do the necessary magic in
1096          * AR5K_INIT_CFG.
1097          */
1098         if ((val & AR5K_RESET_CTL_PCU) == 0)
1099                 ath5k_hw_reg_write(ah, AR5K_INIT_CFG, AR5K_CFG);
1100
1101         return ret;
1102 }
1103
1104 /*
1105  * Power management functions
1106  */
1107
1108 /*
1109  * Sleep control
1110  */
1111 int ath5k_hw_set_power(struct ath5k_hw *ah, enum ath5k_power_mode mode,
1112                 bool set_chip, u16 sleep_duration)
1113 {
1114         unsigned int i;
1115         u32 staid;
1116
1117         ATH5K_TRACE(ah->ah_sc);
1118         staid = ath5k_hw_reg_read(ah, AR5K_STA_ID1);
1119
1120         switch (mode) {
1121         case AR5K_PM_AUTO:
1122                 staid &= ~AR5K_STA_ID1_DEFAULT_ANTENNA;
1123                 /* fallthrough */
1124         case AR5K_PM_NETWORK_SLEEP:
1125                 if (set_chip == true)
1126                         ath5k_hw_reg_write(ah,
1127                                 AR5K_SLEEP_CTL_SLE | sleep_duration,
1128                                 AR5K_SLEEP_CTL);
1129
1130                 staid |= AR5K_STA_ID1_PWR_SV;
1131                 break;
1132
1133         case AR5K_PM_FULL_SLEEP:
1134                 if (set_chip == true)
1135                         ath5k_hw_reg_write(ah, AR5K_SLEEP_CTL_SLE_SLP,
1136                                 AR5K_SLEEP_CTL);
1137
1138                 staid |= AR5K_STA_ID1_PWR_SV;
1139                 break;
1140
1141         case AR5K_PM_AWAKE:
1142                 if (set_chip == false)
1143                         goto commit;
1144
1145                 ath5k_hw_reg_write(ah, AR5K_SLEEP_CTL_SLE_WAKE,
1146                                 AR5K_SLEEP_CTL);
1147
1148                 for (i = 5000; i > 0; i--) {
1149                         /* Check if the chip did wake up */
1150                         if ((ath5k_hw_reg_read(ah, AR5K_PCICFG) &
1151                                         AR5K_PCICFG_SPWR_DN) == 0)
1152                                 break;
1153
1154                         /* Wait a bit and retry */
1155                         udelay(200);
1156                         ath5k_hw_reg_write(ah, AR5K_SLEEP_CTL_SLE_WAKE,
1157                                 AR5K_SLEEP_CTL);
1158                 }
1159
1160                 /* Fail if the chip didn't wake up */
1161                 if (i <= 0)
1162                         return -EIO;
1163
1164                 staid &= ~AR5K_STA_ID1_PWR_SV;
1165                 break;
1166
1167         default:
1168                 return -EINVAL;
1169         }
1170
1171 commit:
1172         ah->ah_power_mode = mode;
1173         ath5k_hw_reg_write(ah, staid, AR5K_STA_ID1);
1174
1175         return 0;
1176 }
1177
1178 /***********************\
1179   DMA Related Functions
1180 \***********************/
1181
1182 /*
1183  * Receive functions
1184  */
1185
1186 /*
1187  * Start DMA receive
1188  */
1189 void ath5k_hw_start_rx(struct ath5k_hw *ah)
1190 {
1191         ATH5K_TRACE(ah->ah_sc);
1192         ath5k_hw_reg_write(ah, AR5K_CR_RXE, AR5K_CR);
1193 }
1194
1195 /*
1196  * Stop DMA receive
1197  */
1198 int ath5k_hw_stop_rx_dma(struct ath5k_hw *ah)
1199 {
1200         unsigned int i;
1201
1202         ATH5K_TRACE(ah->ah_sc);
1203         ath5k_hw_reg_write(ah, AR5K_CR_RXD, AR5K_CR);
1204
1205         /*
1206          * It may take some time to disable the DMA receive unit
1207          */
1208         for (i = 2000; i > 0 &&
1209                         (ath5k_hw_reg_read(ah, AR5K_CR) & AR5K_CR_RXE) != 0;
1210                         i--)
1211                 udelay(10);
1212
1213         return i ? 0 : -EBUSY;
1214 }
1215
1216 /*
1217  * Get the address of the RX Descriptor
1218  */
1219 u32 ath5k_hw_get_rx_buf(struct ath5k_hw *ah)
1220 {
1221         return ath5k_hw_reg_read(ah, AR5K_RXDP);
1222 }
1223
1224 /*
1225  * Set the address of the RX Descriptor
1226  */
1227 void ath5k_hw_put_rx_buf(struct ath5k_hw *ah, u32 phys_addr)
1228 {
1229         ATH5K_TRACE(ah->ah_sc);
1230
1231         /*TODO:Shouldn't we check if RX is enabled first ?*/
1232         ath5k_hw_reg_write(ah, phys_addr, AR5K_RXDP);
1233 }
1234
1235 /*
1236  * Transmit functions
1237  */
1238
1239 /*
1240  * Start DMA transmit for a specific queue
1241  * (see also QCU/DCU functions)
1242  */
1243 int ath5k_hw_tx_start(struct ath5k_hw *ah, unsigned int queue)
1244 {
1245         u32 tx_queue;
1246
1247         ATH5K_TRACE(ah->ah_sc);
1248         AR5K_ASSERT_ENTRY(queue, ah->ah_capabilities.cap_queues.q_tx_num);
1249
1250         /* Return if queue is declared inactive */
1251         if (ah->ah_txq[queue].tqi_type == AR5K_TX_QUEUE_INACTIVE)
1252                 return -EIO;
1253
1254         if (ah->ah_version == AR5K_AR5210) {
1255                 tx_queue = ath5k_hw_reg_read(ah, AR5K_CR);
1256
1257                 /*
1258                  * Set the queue by type on 5210
1259                  */
1260                 switch (ah->ah_txq[queue].tqi_type) {
1261                 case AR5K_TX_QUEUE_DATA:
1262                         tx_queue |= AR5K_CR_TXE0 & ~AR5K_CR_TXD0;
1263                         break;
1264                 case AR5K_TX_QUEUE_BEACON:
1265                         tx_queue |= AR5K_CR_TXE1 & ~AR5K_CR_TXD1;
1266                         ath5k_hw_reg_write(ah, AR5K_BCR_TQ1V | AR5K_BCR_BDMAE,
1267                                         AR5K_BSR);
1268                         break;
1269                 case AR5K_TX_QUEUE_CAB:
1270                         tx_queue |= AR5K_CR_TXE1 & ~AR5K_CR_TXD1;
1271                         ath5k_hw_reg_write(ah, AR5K_BCR_TQ1FV | AR5K_BCR_TQ1V |
1272                                 AR5K_BCR_BDMAE, AR5K_BSR);
1273                         break;
1274                 default:
1275                         return -EINVAL;
1276                 }
1277                 /* Start queue */
1278                 ath5k_hw_reg_write(ah, tx_queue, AR5K_CR);
1279         } else {
1280                 /* Return if queue is disabled */
1281                 if (AR5K_REG_READ_Q(ah, AR5K_QCU_TXD, queue))
1282                         return -EIO;
1283
1284                 /* Start queue */
1285                 AR5K_REG_WRITE_Q(ah, AR5K_QCU_TXE, queue);
1286         }
1287
1288         return 0;
1289 }
1290
1291 /*
1292  * Stop DMA transmit for a specific queue
1293  * (see also QCU/DCU functions)
1294  */
1295 int ath5k_hw_stop_tx_dma(struct ath5k_hw *ah, unsigned int queue)
1296 {
1297         unsigned int i = 100;
1298         u32 tx_queue, pending;
1299
1300         ATH5K_TRACE(ah->ah_sc);
1301         AR5K_ASSERT_ENTRY(queue, ah->ah_capabilities.cap_queues.q_tx_num);
1302
1303         /* Return if queue is declared inactive */
1304         if (ah->ah_txq[queue].tqi_type == AR5K_TX_QUEUE_INACTIVE)
1305                 return -EIO;
1306
1307         if (ah->ah_version == AR5K_AR5210) {
1308                 tx_queue = ath5k_hw_reg_read(ah, AR5K_CR);
1309
1310                 /*
1311                  * Set by queue type
1312                  */
1313                 switch (ah->ah_txq[queue].tqi_type) {
1314                 case AR5K_TX_QUEUE_DATA:
1315                         tx_queue |= AR5K_CR_TXD0 & ~AR5K_CR_TXE0;
1316                         break;
1317                 case AR5K_TX_QUEUE_BEACON:
1318                 case AR5K_TX_QUEUE_CAB:
1319                         /* XXX Fix me... */
1320                         tx_queue |= AR5K_CR_TXD1 & ~AR5K_CR_TXD1;
1321                         ath5k_hw_reg_write(ah, 0, AR5K_BSR);
1322                         break;
1323                 default:
1324                         return -EINVAL;
1325                 }
1326
1327                 /* Stop queue */
1328                 ath5k_hw_reg_write(ah, tx_queue, AR5K_CR);
1329         } else {
1330                 /*
1331                  * Schedule TX disable and wait until queue is empty
1332                  */
1333                 AR5K_REG_WRITE_Q(ah, AR5K_QCU_TXD, queue);
1334
1335                 /*Check for pending frames*/
1336                 do {
1337                         pending = ath5k_hw_reg_read(ah,
1338                                 AR5K_QUEUE_STATUS(queue)) &
1339                                 AR5K_QCU_STS_FRMPENDCNT;
1340                         udelay(100);
1341                 } while (--i && pending);
1342
1343                 /* Clear register */
1344                 ath5k_hw_reg_write(ah, 0, AR5K_QCU_TXD);
1345         }
1346
1347         /* TODO: Check for success else return error */
1348         return 0;
1349 }
1350
1351 /*
1352  * Get the address of the TX Descriptor for a specific queue
1353  * (see also QCU/DCU functions)
1354  */
1355 u32 ath5k_hw_get_tx_buf(struct ath5k_hw *ah, unsigned int queue)
1356 {
1357         u16 tx_reg;
1358
1359         ATH5K_TRACE(ah->ah_sc);
1360         AR5K_ASSERT_ENTRY(queue, ah->ah_capabilities.cap_queues.q_tx_num);
1361
1362         /*
1363          * Get the transmit queue descriptor pointer from the selected queue
1364          */
1365         /*5210 doesn't have QCU*/
1366         if (ah->ah_version == AR5K_AR5210) {
1367                 switch (ah->ah_txq[queue].tqi_type) {
1368                 case AR5K_TX_QUEUE_DATA:
1369                         tx_reg = AR5K_NOQCU_TXDP0;
1370                         break;
1371                 case AR5K_TX_QUEUE_BEACON:
1372                 case AR5K_TX_QUEUE_CAB:
1373                         tx_reg = AR5K_NOQCU_TXDP1;
1374                         break;
1375                 default:
1376                         return 0xffffffff;
1377                 }
1378         } else {
1379                 tx_reg = AR5K_QUEUE_TXDP(queue);
1380         }
1381
1382         return ath5k_hw_reg_read(ah, tx_reg);
1383 }
1384
1385 /*
1386  * Set the address of the TX Descriptor for a specific queue
1387  * (see also QCU/DCU functions)
1388  */
1389 int ath5k_hw_put_tx_buf(struct ath5k_hw *ah, unsigned int queue, u32 phys_addr)
1390 {
1391         u16 tx_reg;
1392
1393         ATH5K_TRACE(ah->ah_sc);
1394         AR5K_ASSERT_ENTRY(queue, ah->ah_capabilities.cap_queues.q_tx_num);
1395
1396         /*
1397          * Set the transmit queue descriptor pointer register by type
1398          * on 5210
1399          */
1400         if (ah->ah_version == AR5K_AR5210) {
1401                 switch (ah->ah_txq[queue].tqi_type) {
1402                 case AR5K_TX_QUEUE_DATA:
1403                         tx_reg = AR5K_NOQCU_TXDP0;
1404                         break;
1405                 case AR5K_TX_QUEUE_BEACON:
1406                 case AR5K_TX_QUEUE_CAB:
1407                         tx_reg = AR5K_NOQCU_TXDP1;
1408                         break;
1409                 default:
1410                         return -EINVAL;
1411                 }
1412         } else {
1413                 /*
1414                  * Set the transmit queue descriptor pointer for
1415                  * the selected queue on QCU for 5211+
1416                  * (this won't work if the queue is still active)
1417                  */
1418                 if (AR5K_REG_READ_Q(ah, AR5K_QCU_TXE, queue))
1419                         return -EIO;
1420
1421                 tx_reg = AR5K_QUEUE_TXDP(queue);
1422         }
1423
1424         /* Set descriptor pointer */
1425         ath5k_hw_reg_write(ah, phys_addr, tx_reg);
1426
1427         return 0;
1428 }
1429
1430 /*
1431  * Update tx trigger level
1432  */
1433 int ath5k_hw_update_tx_triglevel(struct ath5k_hw *ah, bool increase)
1434 {
1435         u32 trigger_level, imr;
1436         int ret = -EIO;
1437
1438         ATH5K_TRACE(ah->ah_sc);
1439
1440         /*
1441          * Disable interrupts by setting the mask
1442          */
1443         imr = ath5k_hw_set_intr(ah, ah->ah_imr & ~AR5K_INT_GLOBAL);
1444
1445         /*TODO: Boundary check on trigger_level*/
1446         trigger_level = AR5K_REG_MS(ath5k_hw_reg_read(ah, AR5K_TXCFG),
1447                         AR5K_TXCFG_TXFULL);
1448
1449         if (increase == false) {
1450                 if (--trigger_level < AR5K_TUNE_MIN_TX_FIFO_THRES)
1451                         goto done;
1452         } else
1453                 trigger_level +=
1454                         ((AR5K_TUNE_MAX_TX_FIFO_THRES - trigger_level) / 2);
1455
1456         /*
1457          * Update trigger level on success
1458          */
1459         if (ah->ah_version == AR5K_AR5210)
1460                 ath5k_hw_reg_write(ah, trigger_level, AR5K_TRIG_LVL);
1461         else
1462                 AR5K_REG_WRITE_BITS(ah, AR5K_TXCFG,
1463                                 AR5K_TXCFG_TXFULL, trigger_level);
1464
1465         ret = 0;
1466
1467 done:
1468         /*
1469          * Restore interrupt mask
1470          */
1471         ath5k_hw_set_intr(ah, imr);
1472
1473         return ret;
1474 }
1475
1476 /*
1477  * Interrupt handling
1478  */
1479
1480 /*
1481  * Check if we have pending interrupts
1482  */
1483 bool ath5k_hw_is_intr_pending(struct ath5k_hw *ah)
1484 {
1485         ATH5K_TRACE(ah->ah_sc);
1486         return ath5k_hw_reg_read(ah, AR5K_INTPEND);
1487 }
1488
1489 /*
1490  * Get interrupt mask (ISR)
1491  */
1492 int ath5k_hw_get_isr(struct ath5k_hw *ah, enum ath5k_int *interrupt_mask)
1493 {
1494         u32 data;
1495
1496         ATH5K_TRACE(ah->ah_sc);
1497
1498         /*
1499          * Read interrupt status from the Interrupt Status register
1500          * on 5210
1501          */
1502         if (ah->ah_version == AR5K_AR5210) {
1503                 data = ath5k_hw_reg_read(ah, AR5K_ISR);
1504                 if (unlikely(data == AR5K_INT_NOCARD)) {
1505                         *interrupt_mask = data;
1506                         return -ENODEV;
1507                 }
1508         } else {
1509                 /*
1510                  * Read interrupt status from the Read-And-Clear shadow register
1511                  * Note: PISR/SISR Not available on 5210
1512                  */
1513                 data = ath5k_hw_reg_read(ah, AR5K_RAC_PISR);
1514         }
1515
1516         /*
1517          * Get abstract interrupt mask (driver-compatible)
1518          */
1519         *interrupt_mask = (data & AR5K_INT_COMMON) & ah->ah_imr;
1520
1521         if (unlikely(data == AR5K_INT_NOCARD))
1522                 return -ENODEV;
1523
1524         if (data & (AR5K_ISR_RXOK | AR5K_ISR_RXERR))
1525                 *interrupt_mask |= AR5K_INT_RX;
1526
1527         if (data & (AR5K_ISR_TXOK | AR5K_ISR_TXERR
1528                 | AR5K_ISR_TXDESC | AR5K_ISR_TXEOL))
1529                 *interrupt_mask |= AR5K_INT_TX;
1530
1531         if (ah->ah_version != AR5K_AR5210) {
1532                 /*HIU = Host Interface Unit (PCI etc)*/
1533                 if (unlikely(data & (AR5K_ISR_HIUERR)))
1534                         *interrupt_mask |= AR5K_INT_FATAL;
1535
1536                 /*Beacon Not Ready*/
1537                 if (unlikely(data & (AR5K_ISR_BNR)))
1538                         *interrupt_mask |= AR5K_INT_BNR;
1539         }
1540
1541         /*
1542          * XXX: BMISS interrupts may occur after association.
1543          * I found this on 5210 code but it needs testing. If this is
1544          * true we should disable them before assoc and re-enable them
1545          * after a successfull assoc + some jiffies.
1546          */
1547 #if 0
1548         interrupt_mask &= ~AR5K_INT_BMISS;
1549 #endif
1550
1551         /*
1552          * In case we didn't handle anything,
1553          * print the register value.
1554          */
1555         if (unlikely(*interrupt_mask == 0 && net_ratelimit()))
1556                 ATH5K_PRINTF("0x%08x\n", data);
1557
1558         return 0;
1559 }
1560
1561 /*
1562  * Set interrupt mask
1563  */
1564 enum ath5k_int ath5k_hw_set_intr(struct ath5k_hw *ah, enum ath5k_int new_mask)
1565 {
1566         enum ath5k_int old_mask, int_mask;
1567
1568         /*
1569          * Disable card interrupts to prevent any race conditions
1570          * (they will be re-enabled afterwards).
1571          */
1572         ath5k_hw_reg_write(ah, AR5K_IER_DISABLE, AR5K_IER);
1573
1574         old_mask = ah->ah_imr;
1575
1576         /*
1577          * Add additional, chipset-dependent interrupt mask flags
1578          * and write them to the IMR (interrupt mask register).
1579          */
1580         int_mask = new_mask & AR5K_INT_COMMON;
1581
1582         if (new_mask & AR5K_INT_RX)
1583                 int_mask |= AR5K_IMR_RXOK | AR5K_IMR_RXERR | AR5K_IMR_RXORN |
1584                         AR5K_IMR_RXDESC;
1585
1586         if (new_mask & AR5K_INT_TX)
1587                 int_mask |= AR5K_IMR_TXOK | AR5K_IMR_TXERR | AR5K_IMR_TXDESC |
1588                         AR5K_IMR_TXURN;
1589
1590         if (ah->ah_version != AR5K_AR5210) {
1591                 if (new_mask & AR5K_INT_FATAL) {
1592                         int_mask |= AR5K_IMR_HIUERR;
1593                         AR5K_REG_ENABLE_BITS(ah, AR5K_SIMR2, AR5K_SIMR2_MCABT |
1594                                         AR5K_SIMR2_SSERR | AR5K_SIMR2_DPERR);
1595                 }
1596         }
1597
1598         ath5k_hw_reg_write(ah, int_mask, AR5K_PIMR);
1599
1600         /* Store new interrupt mask */
1601         ah->ah_imr = new_mask;
1602
1603         /* ..re-enable interrupts */
1604         ath5k_hw_reg_write(ah, AR5K_IER_ENABLE, AR5K_IER);
1605
1606         return old_mask;
1607 }
1608
1609
1610 /*************************\
1611   EEPROM access functions
1612 \*************************/
1613
1614 /*
1615  * Read from eeprom
1616  */
1617 static int ath5k_hw_eeprom_read(struct ath5k_hw *ah, u32 offset, u16 *data)
1618 {
1619         u32 status, timeout;
1620
1621         ATH5K_TRACE(ah->ah_sc);
1622         /*
1623          * Initialize EEPROM access
1624          */
1625         if (ah->ah_version == AR5K_AR5210) {
1626                 AR5K_REG_ENABLE_BITS(ah, AR5K_PCICFG, AR5K_PCICFG_EEAE);
1627                 (void)ath5k_hw_reg_read(ah, AR5K_EEPROM_BASE + (4 * offset));
1628         } else {
1629                 ath5k_hw_reg_write(ah, offset, AR5K_EEPROM_BASE);
1630                 AR5K_REG_ENABLE_BITS(ah, AR5K_EEPROM_CMD,
1631                                 AR5K_EEPROM_CMD_READ);
1632         }
1633
1634         for (timeout = AR5K_TUNE_REGISTER_TIMEOUT; timeout > 0; timeout--) {
1635                 status = ath5k_hw_reg_read(ah, AR5K_EEPROM_STATUS);
1636                 if (status & AR5K_EEPROM_STAT_RDDONE) {
1637                         if (status & AR5K_EEPROM_STAT_RDERR)
1638                                 return -EIO;
1639                         *data = (u16)(ath5k_hw_reg_read(ah, AR5K_EEPROM_DATA) &
1640                                         0xffff);
1641                         return 0;
1642                 }
1643                 udelay(15);
1644         }
1645
1646         return -ETIMEDOUT;
1647 }
1648
1649 /*
1650  * Write to eeprom - currently disabled, use at your own risk
1651  */
1652 #if 0
1653 static int ath5k_hw_eeprom_write(struct ath5k_hw *ah, u32 offset, u16 data)
1654 {
1655
1656         u32 status, timeout;
1657
1658         ATH5K_TRACE(ah->ah_sc);
1659
1660         /*
1661          * Initialize eeprom access
1662          */
1663
1664         if (ah->ah_version == AR5K_AR5210) {
1665                 AR5K_REG_ENABLE_BITS(ah, AR5K_PCICFG, AR5K_PCICFG_EEAE);
1666         } else {
1667                 AR5K_REG_ENABLE_BITS(ah, AR5K_EEPROM_CMD,
1668                                 AR5K_EEPROM_CMD_RESET);
1669         }
1670
1671         /*
1672          * Write data to data register
1673          */
1674
1675         if (ah->ah_version == AR5K_AR5210) {
1676                 ath5k_hw_reg_write(ah, data, AR5K_EEPROM_BASE + (4 * offset));
1677         } else {
1678                 ath5k_hw_reg_write(ah, offset, AR5K_EEPROM_BASE);
1679                 ath5k_hw_reg_write(ah, data, AR5K_EEPROM_DATA);
1680                 AR5K_REG_ENABLE_BITS(ah, AR5K_EEPROM_CMD,
1681                                 AR5K_EEPROM_CMD_WRITE);
1682         }
1683
1684         /*
1685          * Check status
1686          */
1687
1688         for (timeout = AR5K_TUNE_REGISTER_TIMEOUT; timeout > 0; timeout--) {
1689                 status = ath5k_hw_reg_read(ah, AR5K_EEPROM_STATUS);
1690                 if (status & AR5K_EEPROM_STAT_WRDONE) {
1691                         if (status & AR5K_EEPROM_STAT_WRERR)
1692                                 return EIO;
1693                         return 0;
1694                 }
1695                 udelay(15);
1696         }
1697
1698         ATH5K_ERR(ah->ah_sc, "EEPROM Write is disabled!");
1699         return -EIO;
1700 }
1701 #endif
1702
1703 /*
1704  * Translate binary channel representation in EEPROM to frequency
1705  */
1706 static u16 ath5k_eeprom_bin2freq(struct ath5k_hw *ah, u16 bin, unsigned int mode)
1707 {
1708         u16 val;
1709
1710         if (bin == AR5K_EEPROM_CHANNEL_DIS)
1711                 return bin;
1712
1713         if (mode == AR5K_EEPROM_MODE_11A) {
1714                 if (ah->ah_ee_version > AR5K_EEPROM_VERSION_3_2)
1715                         val = (5 * bin) + 4800;
1716                 else
1717                         val = bin > 62 ? (10 * 62) + (5 * (bin - 62)) + 5100 :
1718                                 (bin * 10) + 5100;
1719         } else {
1720                 if (ah->ah_ee_version > AR5K_EEPROM_VERSION_3_2)
1721                         val = bin + 2300;
1722                 else
1723                         val = bin + 2400;
1724         }
1725
1726         return val;
1727 }
1728
1729 /*
1730  * Read antenna infos from eeprom
1731  */
1732 static int ath5k_eeprom_read_ants(struct ath5k_hw *ah, u32 *offset,
1733                 unsigned int mode)
1734 {
1735         struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
1736         u32 o = *offset;
1737         u16 val;
1738         int ret, i = 0;
1739
1740         AR5K_EEPROM_READ(o++, val);
1741         ee->ee_switch_settling[mode]    = (val >> 8) & 0x7f;
1742         ee->ee_ant_tx_rx[mode]          = (val >> 2) & 0x3f;
1743         ee->ee_ant_control[mode][i]     = (val << 4) & 0x3f;
1744
1745         AR5K_EEPROM_READ(o++, val);
1746         ee->ee_ant_control[mode][i++]   |= (val >> 12) & 0xf;
1747         ee->ee_ant_control[mode][i++]   = (val >> 6) & 0x3f;
1748         ee->ee_ant_control[mode][i++]   = val & 0x3f;
1749
1750         AR5K_EEPROM_READ(o++, val);
1751         ee->ee_ant_control[mode][i++]   = (val >> 10) & 0x3f;
1752         ee->ee_ant_control[mode][i++]   = (val >> 4) & 0x3f;
1753         ee->ee_ant_control[mode][i]     = (val << 2) & 0x3f;
1754
1755         AR5K_EEPROM_READ(o++, val);
1756         ee->ee_ant_control[mode][i++]   |= (val >> 14) & 0x3;
1757         ee->ee_ant_control[mode][i++]   = (val >> 8) & 0x3f;
1758         ee->ee_ant_control[mode][i++]   = (val >> 2) & 0x3f;
1759         ee->ee_ant_control[mode][i]     = (val << 4) & 0x3f;
1760
1761         AR5K_EEPROM_READ(o++, val);
1762         ee->ee_ant_control[mode][i++]   |= (val >> 12) & 0xf;
1763         ee->ee_ant_control[mode][i++]   = (val >> 6) & 0x3f;
1764         ee->ee_ant_control[mode][i++]   = val & 0x3f;
1765
1766         /* Get antenna modes */
1767         ah->ah_antenna[mode][0] =
1768             (ee->ee_ant_control[mode][0] << 4) | 0x1;
1769         ah->ah_antenna[mode][AR5K_ANT_FIXED_A] =
1770              ee->ee_ant_control[mode][1]        |
1771             (ee->ee_ant_control[mode][2] << 6)  |
1772             (ee->ee_ant_control[mode][3] << 12) |
1773             (ee->ee_ant_control[mode][4] << 18) |
1774             (ee->ee_ant_control[mode][5] << 24);
1775         ah->ah_antenna[mode][AR5K_ANT_FIXED_B] =
1776              ee->ee_ant_control[mode][6]        |
1777             (ee->ee_ant_control[mode][7] << 6)  |
1778             (ee->ee_ant_control[mode][8] << 12) |
1779             (ee->ee_ant_control[mode][9] << 18) |
1780             (ee->ee_ant_control[mode][10] << 24);
1781
1782         /* return new offset */
1783         *offset = o;
1784
1785         return 0;
1786 }
1787
1788 /*
1789  * Read supported modes from eeprom
1790  */
1791 static int ath5k_eeprom_read_modes(struct ath5k_hw *ah, u32 *offset,
1792                 unsigned int mode)
1793 {
1794         struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
1795         u32 o = *offset;
1796         u16 val;
1797         int ret;
1798
1799         AR5K_EEPROM_READ(o++, val);
1800         ee->ee_tx_end2xlna_enable[mode] = (val >> 8) & 0xff;
1801         ee->ee_thr_62[mode]             = val & 0xff;
1802
1803         if (ah->ah_ee_version <= AR5K_EEPROM_VERSION_3_2)
1804                 ee->ee_thr_62[mode] = mode == AR5K_EEPROM_MODE_11A ? 15 : 28;
1805
1806         AR5K_EEPROM_READ(o++, val);
1807         ee->ee_tx_end2xpa_disable[mode] = (val >> 8) & 0xff;
1808         ee->ee_tx_frm2xpa_enable[mode]  = val & 0xff;
1809
1810         AR5K_EEPROM_READ(o++, val);
1811         ee->ee_pga_desired_size[mode]   = (val >> 8) & 0xff;
1812
1813         if ((val & 0xff) & 0x80)
1814                 ee->ee_noise_floor_thr[mode] = -((((val & 0xff) ^ 0xff)) + 1);
1815         else
1816                 ee->ee_noise_floor_thr[mode] = val & 0xff;
1817
1818         if (ah->ah_ee_version <= AR5K_EEPROM_VERSION_3_2)
1819                 ee->ee_noise_floor_thr[mode] =
1820                     mode == AR5K_EEPROM_MODE_11A ? -54 : -1;
1821
1822         AR5K_EEPROM_READ(o++, val);
1823         ee->ee_xlna_gain[mode]          = (val >> 5) & 0xff;
1824         ee->ee_x_gain[mode]             = (val >> 1) & 0xf;
1825         ee->ee_xpd[mode]                = val & 0x1;
1826
1827         if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_4_0)
1828                 ee->ee_fixed_bias[mode] = (val >> 13) & 0x1;
1829
1830         if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_3_3) {
1831                 AR5K_EEPROM_READ(o++, val);
1832                 ee->ee_false_detect[mode] = (val >> 6) & 0x7f;
1833
1834                 if (mode == AR5K_EEPROM_MODE_11A)
1835                         ee->ee_xr_power[mode] = val & 0x3f;
1836                 else {
1837                         ee->ee_ob[mode][0] = val & 0x7;
1838                         ee->ee_db[mode][0] = (val >> 3) & 0x7;
1839                 }
1840         }
1841
1842         if (ah->ah_ee_version < AR5K_EEPROM_VERSION_3_4) {
1843                 ee->ee_i_gain[mode] = AR5K_EEPROM_I_GAIN;
1844                 ee->ee_cck_ofdm_power_delta = AR5K_EEPROM_CCK_OFDM_DELTA;
1845         } else {
1846                 ee->ee_i_gain[mode] = (val >> 13) & 0x7;
1847
1848                 AR5K_EEPROM_READ(o++, val);
1849                 ee->ee_i_gain[mode] |= (val << 3) & 0x38;
1850
1851                 if (mode == AR5K_EEPROM_MODE_11G)
1852                         ee->ee_cck_ofdm_power_delta = (val >> 3) & 0xff;
1853         }
1854
1855         if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_4_0 &&
1856                         mode == AR5K_EEPROM_MODE_11A) {
1857                 ee->ee_i_cal[mode] = (val >> 8) & 0x3f;
1858                 ee->ee_q_cal[mode] = (val >> 3) & 0x1f;
1859         }
1860
1861         if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_4_6 &&
1862             mode == AR5K_EEPROM_MODE_11G)
1863                 ee->ee_scaled_cck_delta = (val >> 11) & 0x1f;
1864
1865         /* return new offset */
1866         *offset = o;
1867
1868         return 0;
1869 }
1870
1871 /*
1872  * Initialize eeprom & capabilities structs
1873  */
1874 static int ath5k_eeprom_init(struct ath5k_hw *ah)
1875 {
1876         struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
1877         unsigned int mode, i;
1878         int ret;
1879         u32 offset;
1880         u16 val;
1881
1882         /* Initial TX thermal adjustment values */
1883         ee->ee_tx_clip = 4;
1884         ee->ee_pwd_84 = ee->ee_pwd_90 = 1;
1885         ee->ee_gain_select = 1;
1886
1887         /*
1888          * Read values from EEPROM and store them in the capability structure
1889          */
1890         AR5K_EEPROM_READ_HDR(AR5K_EEPROM_MAGIC, ee_magic);
1891         AR5K_EEPROM_READ_HDR(AR5K_EEPROM_PROTECT, ee_protect);
1892         AR5K_EEPROM_READ_HDR(AR5K_EEPROM_REG_DOMAIN, ee_regdomain);
1893         AR5K_EEPROM_READ_HDR(AR5K_EEPROM_VERSION, ee_version);
1894         AR5K_EEPROM_READ_HDR(AR5K_EEPROM_HDR, ee_header);
1895
1896         /* Return if we have an old EEPROM */
1897         if (ah->ah_ee_version < AR5K_EEPROM_VERSION_3_0)
1898                 return 0;
1899
1900 #ifdef notyet
1901         /*
1902          * Validate the checksum of the EEPROM date. There are some
1903          * devices with invalid EEPROMs.
1904          */
1905         for (cksum = 0, offset = 0; offset < AR5K_EEPROM_INFO_MAX; offset++) {
1906                 AR5K_EEPROM_READ(AR5K_EEPROM_INFO(offset), val);
1907                 cksum ^= val;
1908         }
1909         if (cksum != AR5K_EEPROM_INFO_CKSUM) {
1910                 ATH5K_ERR(ah->ah_sc, "Invalid EEPROM checksum 0x%04x\n", cksum);
1911                 return -EIO;
1912         }
1913 #endif
1914
1915         AR5K_EEPROM_READ_HDR(AR5K_EEPROM_ANT_GAIN(ah->ah_ee_version),
1916             ee_ant_gain);
1917
1918         if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_4_0) {
1919                 AR5K_EEPROM_READ_HDR(AR5K_EEPROM_MISC0, ee_misc0);
1920                 AR5K_EEPROM_READ_HDR(AR5K_EEPROM_MISC1, ee_misc1);
1921         }
1922
1923         if (ah->ah_ee_version < AR5K_EEPROM_VERSION_3_3) {
1924                 AR5K_EEPROM_READ(AR5K_EEPROM_OBDB0_2GHZ, val);
1925                 ee->ee_ob[AR5K_EEPROM_MODE_11B][0] = val & 0x7;
1926                 ee->ee_db[AR5K_EEPROM_MODE_11B][0] = (val >> 3) & 0x7;
1927
1928                 AR5K_EEPROM_READ(AR5K_EEPROM_OBDB1_2GHZ, val);
1929                 ee->ee_ob[AR5K_EEPROM_MODE_11G][0] = val & 0x7;
1930                 ee->ee_db[AR5K_EEPROM_MODE_11G][0] = (val >> 3) & 0x7;
1931         }
1932
1933         /*
1934          * Get conformance test limit values
1935          */
1936         offset = AR5K_EEPROM_CTL(ah->ah_ee_version);
1937         ee->ee_ctls = AR5K_EEPROM_N_CTLS(ah->ah_ee_version);
1938
1939         for (i = 0; i < ee->ee_ctls; i++) {
1940                 AR5K_EEPROM_READ(offset++, val);
1941                 ee->ee_ctl[i] = (val >> 8) & 0xff;
1942                 ee->ee_ctl[i + 1] = val & 0xff;
1943         }
1944
1945         /*
1946          * Get values for 802.11a (5GHz)
1947          */
1948         mode = AR5K_EEPROM_MODE_11A;
1949
1950         ee->ee_turbo_max_power[mode] =
1951                         AR5K_EEPROM_HDR_T_5GHZ_DBM(ee->ee_header);
1952
1953         offset = AR5K_EEPROM_MODES_11A(ah->ah_ee_version);
1954
1955         ret = ath5k_eeprom_read_ants(ah, &offset, mode);
1956         if (ret)
1957                 return ret;
1958
1959         AR5K_EEPROM_READ(offset++, val);
1960         ee->ee_adc_desired_size[mode]   = (s8)((val >> 8) & 0xff);
1961         ee->ee_ob[mode][3]              = (val >> 5) & 0x7;
1962         ee->ee_db[mode][3]              = (val >> 2) & 0x7;
1963         ee->ee_ob[mode][2]              = (val << 1) & 0x7;
1964
1965         AR5K_EEPROM_READ(offset++, val);
1966         ee->ee_ob[mode][2]              |= (val >> 15) & 0x1;
1967         ee->ee_db[mode][2]              = (val >> 12) & 0x7;
1968         ee->ee_ob[mode][1]              = (val >> 9) & 0x7;
1969         ee->ee_db[mode][1]              = (val >> 6) & 0x7;
1970         ee->ee_ob[mode][0]              = (val >> 3) & 0x7;
1971         ee->ee_db[mode][0]              = val & 0x7;
1972
1973         ret = ath5k_eeprom_read_modes(ah, &offset, mode);
1974         if (ret)
1975                 return ret;
1976
1977         if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_4_1) {
1978                 AR5K_EEPROM_READ(offset++, val);
1979                 ee->ee_margin_tx_rx[mode] = val & 0x3f;
1980         }
1981
1982         /*
1983          * Get values for 802.11b (2.4GHz)
1984          */
1985         mode = AR5K_EEPROM_MODE_11B;
1986         offset = AR5K_EEPROM_MODES_11B(ah->ah_ee_version);
1987
1988         ret = ath5k_eeprom_read_ants(ah, &offset, mode);
1989         if (ret)
1990                 return ret;
1991
1992         AR5K_EEPROM_READ(offset++, val);
1993         ee->ee_adc_desired_size[mode]   = (s8)((val >> 8) & 0xff);
1994         ee->ee_ob[mode][1]              = (val >> 4) & 0x7;
1995         ee->ee_db[mode][1]              = val & 0x7;
1996
1997         ret = ath5k_eeprom_read_modes(ah, &offset, mode);
1998         if (ret)
1999                 return ret;
2000
2001         if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_4_0) {
2002                 AR5K_EEPROM_READ(offset++, val);
2003                 ee->ee_cal_pier[mode][0] =
2004                         ath5k_eeprom_bin2freq(ah, val & 0xff, mode);
2005                 ee->ee_cal_pier[mode][1] =
2006                         ath5k_eeprom_bin2freq(ah, (val >> 8) & 0xff, mode);
2007
2008                 AR5K_EEPROM_READ(offset++, val);
2009                 ee->ee_cal_pier[mode][2] =
2010                         ath5k_eeprom_bin2freq(ah, val & 0xff, mode);
2011         }
2012
2013         if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_4_1)
2014                 ee->ee_margin_tx_rx[mode] = (val >> 8) & 0x3f;
2015
2016         /*
2017          * Get values for 802.11g (2.4GHz)
2018          */
2019         mode = AR5K_EEPROM_MODE_11G;
2020         offset = AR5K_EEPROM_MODES_11G(ah->ah_ee_version);
2021
2022         ret = ath5k_eeprom_read_ants(ah, &offset, mode);
2023         if (ret)
2024                 return ret;
2025
2026         AR5K_EEPROM_READ(offset++, val);
2027         ee->ee_adc_desired_size[mode]   = (s8)((val >> 8) & 0xff);
2028         ee->ee_ob[mode][1]              = (val >> 4) & 0x7;
2029         ee->ee_db[mode][1]              = val & 0x7;
2030
2031         ret = ath5k_eeprom_read_modes(ah, &offset, mode);
2032         if (ret)
2033                 return ret;
2034
2035         if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_4_0) {
2036                 AR5K_EEPROM_READ(offset++, val);
2037                 ee->ee_cal_pier[mode][0] =
2038                         ath5k_eeprom_bin2freq(ah, val & 0xff, mode);
2039                 ee->ee_cal_pier[mode][1] =
2040                         ath5k_eeprom_bin2freq(ah, (val >> 8) & 0xff, mode);
2041
2042                 AR5K_EEPROM_READ(offset++, val);
2043                 ee->ee_turbo_max_power[mode] = val & 0x7f;
2044                 ee->ee_xr_power[mode] = (val >> 7) & 0x3f;
2045
2046                 AR5K_EEPROM_READ(offset++, val);
2047                 ee->ee_cal_pier[mode][2] =
2048                         ath5k_eeprom_bin2freq(ah, val & 0xff, mode);
2049
2050                 if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_4_1)
2051                         ee->ee_margin_tx_rx[mode] = (val >> 8) & 0x3f;
2052
2053                 AR5K_EEPROM_READ(offset++, val);
2054                 ee->ee_i_cal[mode] = (val >> 8) & 0x3f;
2055                 ee->ee_q_cal[mode] = (val >> 3) & 0x1f;
2056
2057                 if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_4_2) {
2058                         AR5K_EEPROM_READ(offset++, val);
2059                         ee->ee_cck_ofdm_gain_delta = val & 0xff;
2060                 }
2061         }
2062
2063         /*
2064          * Read 5GHz EEPROM channels
2065          */
2066
2067         return 0;
2068 }
2069
2070 /*
2071  * Read the MAC address from eeprom
2072  */
2073 static int ath5k_eeprom_read_mac(struct ath5k_hw *ah, u8 *mac)
2074 {
2075         u8 mac_d[ETH_ALEN];
2076         u32 total, offset;
2077         u16 data;
2078         int octet, ret;
2079
2080         memset(mac, 0, ETH_ALEN);
2081         memset(mac_d, 0, ETH_ALEN);
2082
2083         ret = ath5k_hw_eeprom_read(ah, 0x20, &data);
2084         if (ret)
2085                 return ret;
2086
2087         for (offset = 0x1f, octet = 0, total = 0; offset >= 0x1d; offset--) {
2088                 ret = ath5k_hw_eeprom_read(ah, offset, &data);
2089                 if (ret)
2090                         return ret;
2091
2092                 total += data;
2093                 mac_d[octet + 1] = data & 0xff;
2094                 mac_d[octet] = data >> 8;
2095                 octet += 2;
2096         }
2097
2098         memcpy(mac, mac_d, ETH_ALEN);
2099
2100         if (!total || total == 3 * 0xffff)
2101                 return -EINVAL;
2102
2103         return 0;
2104 }
2105
2106 /*
2107  * Fill the capabilities struct
2108  */
2109 static int ath5k_hw_get_capabilities(struct ath5k_hw *ah)
2110 {
2111         u16 ee_header;
2112
2113         ATH5K_TRACE(ah->ah_sc);
2114         /* Capabilities stored in the EEPROM */
2115         ee_header = ah->ah_capabilities.cap_eeprom.ee_header;
2116
2117         if (ah->ah_version == AR5K_AR5210) {
2118                 /*
2119                  * Set radio capabilities
2120                  * (The AR5110 only supports the middle 5GHz band)
2121                  */
2122                 ah->ah_capabilities.cap_range.range_5ghz_min = 5120;
2123                 ah->ah_capabilities.cap_range.range_5ghz_max = 5430;
2124                 ah->ah_capabilities.cap_range.range_2ghz_min = 0;
2125                 ah->ah_capabilities.cap_range.range_2ghz_max = 0;
2126
2127                 /* Set supported modes */
2128                 __set_bit(AR5K_MODE_11A, ah->ah_capabilities.cap_mode);
2129                 __set_bit(AR5K_MODE_11A_TURBO, ah->ah_capabilities.cap_mode);
2130         } else {
2131                 /*
2132                  * XXX The tranceiver supports frequencies from 4920 to 6100GHz
2133                  * XXX and from 2312 to 2732GHz. There are problems with the
2134                  * XXX current ieee80211 implementation because the IEEE
2135                  * XXX channel mapping does not support negative channel
2136                  * XXX numbers (2312MHz is channel -19). Of course, this
2137                  * XXX doesn't matter because these channels are out of range
2138                  * XXX but some regulation domains like MKK (Japan) will
2139                  * XXX support frequencies somewhere around 4.8GHz.
2140                  */
2141
2142                 /*
2143                  * Set radio capabilities
2144                  */
2145
2146                 if (AR5K_EEPROM_HDR_11A(ee_header)) {
2147                         ah->ah_capabilities.cap_range.range_5ghz_min = 5005; /* 4920 */
2148                         ah->ah_capabilities.cap_range.range_5ghz_max = 6100;
2149
2150                         /* Set supported modes */
2151                         __set_bit(AR5K_MODE_11A,
2152                                         ah->ah_capabilities.cap_mode);
2153                         __set_bit(AR5K_MODE_11A_TURBO,
2154                                         ah->ah_capabilities.cap_mode);
2155                         if (ah->ah_version == AR5K_AR5212)
2156                                 __set_bit(AR5K_MODE_11G_TURBO,
2157                                                 ah->ah_capabilities.cap_mode);
2158                 }
2159
2160                 /* Enable  802.11b if a 2GHz capable radio (2111/5112) is
2161                  * connected */
2162                 if (AR5K_EEPROM_HDR_11B(ee_header) ||
2163                                 AR5K_EEPROM_HDR_11G(ee_header)) {
2164                         ah->ah_capabilities.cap_range.range_2ghz_min = 2412; /* 2312 */
2165                         ah->ah_capabilities.cap_range.range_2ghz_max = 2732;
2166
2167                         if (AR5K_EEPROM_HDR_11B(ee_header))
2168                                 __set_bit(AR5K_MODE_11B,
2169                                                 ah->ah_capabilities.cap_mode);
2170
2171                         if (AR5K_EEPROM_HDR_11G(ee_header))
2172                                 __set_bit(AR5K_MODE_11G,
2173                                                 ah->ah_capabilities.cap_mode);
2174                 }
2175         }
2176
2177         /* GPIO */
2178         ah->ah_gpio_npins = AR5K_NUM_GPIO;
2179
2180         /* Set number of supported TX queues */
2181         if (ah->ah_version == AR5K_AR5210)
2182                 ah->ah_capabilities.cap_queues.q_tx_num =
2183                         AR5K_NUM_TX_QUEUES_NOQCU;
2184         else
2185                 ah->ah_capabilities.cap_queues.q_tx_num = AR5K_NUM_TX_QUEUES;
2186
2187         return 0;
2188 }
2189
2190 /*********************************\
2191   Protocol Control Unit Functions
2192 \*********************************/
2193
2194 /*
2195  * Set Operation mode
2196  */
2197 int ath5k_hw_set_opmode(struct ath5k_hw *ah)
2198 {
2199         u32 pcu_reg, beacon_reg, low_id, high_id;
2200
2201         pcu_reg = 0;
2202         beacon_reg = 0;
2203
2204         ATH5K_TRACE(ah->ah_sc);
2205
2206         switch (ah->ah_op_mode) {
2207         case IEEE80211_IF_TYPE_IBSS:
2208                 pcu_reg |= AR5K_STA_ID1_ADHOC | AR5K_STA_ID1_DESC_ANTENNA |
2209                         (ah->ah_version == AR5K_AR5210 ?
2210                                 AR5K_STA_ID1_NO_PSPOLL : 0);
2211                 beacon_reg |= AR5K_BCR_ADHOC;
2212                 break;
2213
2214         case IEEE80211_IF_TYPE_AP:
2215                 pcu_reg |= AR5K_STA_ID1_AP | AR5K_STA_ID1_RTS_DEF_ANTENNA |
2216                         (ah->ah_version == AR5K_AR5210 ?
2217                                 AR5K_STA_ID1_NO_PSPOLL : 0);
2218                 beacon_reg |= AR5K_BCR_AP;
2219                 break;
2220
2221         case IEEE80211_IF_TYPE_STA:
2222                 pcu_reg |= AR5K_STA_ID1_DEFAULT_ANTENNA |
2223                         (ah->ah_version == AR5K_AR5210 ?
2224                                 AR5K_STA_ID1_PWR_SV : 0);
2225         case IEEE80211_IF_TYPE_MNTR:
2226                 pcu_reg |= AR5K_STA_ID1_DEFAULT_ANTENNA |
2227                         (ah->ah_version == AR5K_AR5210 ?
2228                                 AR5K_STA_ID1_NO_PSPOLL : 0);
2229                 break;
2230
2231         default:
2232                 return -EINVAL;
2233         }
2234
2235         /*
2236          * Set PCU registers
2237          */
2238         low_id = AR5K_LOW_ID(ah->ah_sta_id);
2239         high_id = AR5K_HIGH_ID(ah->ah_sta_id);
2240         ath5k_hw_reg_write(ah, low_id, AR5K_STA_ID0);
2241         ath5k_hw_reg_write(ah, pcu_reg | high_id, AR5K_STA_ID1);
2242
2243         /*
2244          * Set Beacon Control Register on 5210
2245          */
2246         if (ah->ah_version == AR5K_AR5210)
2247                 ath5k_hw_reg_write(ah, beacon_reg, AR5K_BCR);
2248
2249         return 0;
2250 }
2251
2252 /*
2253  * BSSID Functions
2254  */
2255
2256 /*
2257  * Get station id
2258  */
2259 void ath5k_hw_get_lladdr(struct ath5k_hw *ah, u8 *mac)
2260 {
2261         ATH5K_TRACE(ah->ah_sc);
2262         memcpy(mac, ah->ah_sta_id, ETH_ALEN);
2263 }
2264
2265 /*
2266  * Set station id
2267  */
2268 int ath5k_hw_set_lladdr(struct ath5k_hw *ah, const u8 *mac)
2269 {
2270         u32 low_id, high_id;
2271
2272         ATH5K_TRACE(ah->ah_sc);
2273         /* Set new station ID */
2274         memcpy(ah->ah_sta_id, mac, ETH_ALEN);
2275
2276         low_id = AR5K_LOW_ID(mac);
2277         high_id = AR5K_HIGH_ID(mac);
2278
2279         ath5k_hw_reg_write(ah, low_id, AR5K_STA_ID0);
2280         ath5k_hw_reg_write(ah, high_id, AR5K_STA_ID1);
2281
2282         return 0;
2283 }
2284
2285 /*
2286  * Set BSSID
2287  */
2288 void ath5k_hw_set_associd(struct ath5k_hw *ah, const u8 *bssid, u16 assoc_id)
2289 {
2290         u32 low_id, high_id;
2291         u16 tim_offset = 0;
2292
2293         /*
2294          * Set simple BSSID mask on 5212
2295          */
2296         if (ah->ah_version == AR5K_AR5212) {
2297                 ath5k_hw_reg_write(ah, 0xffffffff, AR5K_BSS_IDM0);
2298                 ath5k_hw_reg_write(ah, 0xffffffff, AR5K_BSS_IDM1);
2299         }
2300
2301         /*
2302          * Set BSSID which triggers the "SME Join" operation
2303          */
2304         low_id = AR5K_LOW_ID(bssid);
2305         high_id = AR5K_HIGH_ID(bssid);
2306         ath5k_hw_reg_write(ah, low_id, AR5K_BSS_ID0);
2307         ath5k_hw_reg_write(ah, high_id | ((assoc_id & 0x3fff) <<
2308                                 AR5K_BSS_ID1_AID_S), AR5K_BSS_ID1);
2309
2310         if (assoc_id == 0) {
2311                 ath5k_hw_disable_pspoll(ah);
2312                 return;
2313         }
2314
2315         AR5K_REG_WRITE_BITS(ah, AR5K_BEACON, AR5K_BEACON_TIM,
2316                         tim_offset ? tim_offset + 4 : 0);
2317
2318         ath5k_hw_enable_pspoll(ah, NULL, 0);
2319 }
2320 /**
2321  * ath5k_hw_set_bssid_mask - set common bits we should listen to
2322  *
2323  * The bssid_mask is a utility used by AR5212 hardware to inform the hardware
2324  * which bits of the interface's MAC address should be looked at when trying
2325  * to decide which packets to ACK. In station mode every bit matters. In AP
2326  * mode with a single BSS every bit matters as well. In AP mode with
2327  * multiple BSSes not every bit matters.
2328  *
2329  * @ah: the &struct ath5k_hw
2330  * @mask: the bssid_mask, a u8 array of size ETH_ALEN
2331  *
2332  * Note that this is a simple filter and *does* not filter out all
2333  * relevant frames. Some non-relevant frames will get through, probability
2334  * jocks are welcomed to compute.
2335  *
2336  * When handling multiple BSSes (or VAPs) you can get the BSSID mask by
2337  * computing the set of:
2338  *
2339  *     ~ ( MAC XOR BSSID )
2340  *
2341  * When you do this you are essentially computing the common bits. Later it
2342  * is assumed the harware will "and" (&) the BSSID mask with the MAC address
2343  * to obtain the relevant bits which should match on the destination frame.
2344  *
2345  * Simple example: on your card you have have two BSSes you have created with
2346  * BSSID-01 and BSSID-02. Lets assume BSSID-01 will not use the MAC address.
2347  * There is another BSSID-03 but you are not part of it. For simplicity's sake,
2348  * assuming only 4 bits for a mac address and for BSSIDs you can then have:
2349  *
2350  *                  \
2351  * MAC:                0001 |
2352  * BSSID-01:   0100 | --> Belongs to us
2353  * BSSID-02:   1001 |
2354  *                  /
2355  * -------------------
2356  * BSSID-03:   0110  | --> External
2357  * -------------------
2358  *
2359  * Our bssid_mask would then be:
2360  *
2361  *             On loop iteration for BSSID-01:
2362  *             ~(0001 ^ 0100)  -> ~(0101)
2363  *                             ->   1010
2364  *             bssid_mask      =    1010
2365  *
2366  *             On loop iteration for BSSID-02:
2367  *             bssid_mask &= ~(0001   ^   1001)
2368  *             bssid_mask =   (1010)  & ~(0001 ^ 1001)
2369  *             bssid_mask =   (1010)  & ~(1001)
2370  *             bssid_mask =   (1010)  &  (0110)
2371  *             bssid_mask =   0010
2372  *
2373  * A bssid_mask of 0010 means "only pay attention to the second least
2374  * significant bit". This is because its the only bit common
2375  * amongst the MAC and all BSSIDs we support. To findout what the real
2376  * common bit is we can simply "&" the bssid_mask now with any BSSID we have
2377  * or our MAC address (we assume the hardware uses the MAC address).
2378  *
2379  * Now, suppose there's an incoming frame for BSSID-03:
2380  *
2381  * IFRAME-01:  0110
2382  *
2383  * An easy eye-inspeciton of this already should tell you that this frame
2384  * will not pass our check. This is beacuse the bssid_mask tells the
2385  * hardware to only look at the second least significant bit and the
2386  * common bit amongst the MAC and BSSIDs is 0, this frame has the 2nd LSB
2387  * as 1, which does not match 0.
2388  *
2389  * So with IFRAME-01 we *assume* the hardware will do:
2390  *
2391  *     allow = (IFRAME-01 & bssid_mask) == (bssid_mask & MAC) ? 1 : 0;
2392  *  --> allow = (0110 & 0010) == (0010 & 0001) ? 1 : 0;
2393  *  --> allow = (0010) == 0000 ? 1 : 0;
2394  *  --> allow = 0
2395  *
2396  *  Lets now test a frame that should work:
2397  *
2398  * IFRAME-02:  0001 (we should allow)
2399  *
2400  *     allow = (0001 & 1010) == 1010
2401  *
2402  *     allow = (IFRAME-02 & bssid_mask) == (bssid_mask & MAC) ? 1 : 0;
2403  *  --> allow = (0001 & 0010) ==  (0010 & 0001) ? 1 :0;
2404  *  --> allow = (0010) == (0010)
2405  *  --> allow = 1
2406  *
2407  * Other examples:
2408  *
2409  * IFRAME-03:  0100 --> allowed
2410  * IFRAME-04:  1001 --> allowed
2411  * IFRAME-05:  1101 --> allowed but its not for us!!!
2412  *
2413  */
2414 int ath5k_hw_set_bssid_mask(struct ath5k_hw *ah, const u8 *mask)
2415 {
2416         u32 low_id, high_id;
2417         ATH5K_TRACE(ah->ah_sc);
2418
2419         if (ah->ah_version == AR5K_AR5212) {
2420                 low_id = AR5K_LOW_ID(mask);
2421                 high_id = AR5K_HIGH_ID(mask);
2422
2423                 ath5k_hw_reg_write(ah, low_id, AR5K_BSS_IDM0);
2424                 ath5k_hw_reg_write(ah, high_id, AR5K_BSS_IDM1);
2425
2426                 return 0;
2427         }
2428
2429         return -EIO;
2430 }
2431
2432 /*
2433  * Receive start/stop functions
2434  */
2435
2436 /*
2437  * Start receive on PCU
2438  */
2439 void ath5k_hw_start_rx_pcu(struct ath5k_hw *ah)
2440 {
2441         ATH5K_TRACE(ah->ah_sc);
2442         AR5K_REG_DISABLE_BITS(ah, AR5K_DIAG_SW, AR5K_DIAG_SW_DIS_RX);
2443
2444         /* TODO: ANI Support */
2445 }
2446
2447 /*
2448  * Stop receive on PCU
2449  */
2450 void ath5k_hw_stop_pcu_recv(struct ath5k_hw *ah)
2451 {
2452         ATH5K_TRACE(ah->ah_sc);
2453         AR5K_REG_ENABLE_BITS(ah, AR5K_DIAG_SW, AR5K_DIAG_SW_DIS_RX);
2454
2455         /* TODO: ANI Support */
2456 }
2457
2458 /*
2459  * RX Filter functions
2460  */
2461
2462 /*
2463  * Set multicast filter
2464  */
2465 void ath5k_hw_set_mcast_filter(struct ath5k_hw *ah, u32 filter0, u32 filter1)
2466 {
2467         ATH5K_TRACE(ah->ah_sc);
2468         /* Set the multicat filter */
2469         ath5k_hw_reg_write(ah, filter0, AR5K_MCAST_FILTER0);
2470         ath5k_hw_reg_write(ah, filter1, AR5K_MCAST_FILTER1);
2471 }
2472
2473 /*
2474  * Set multicast filter by index
2475  */
2476 int ath5k_hw_set_mcast_filterindex(struct ath5k_hw *ah, u32 index)
2477 {
2478
2479         ATH5K_TRACE(ah->ah_sc);
2480         if (index >= 64)
2481                 return -EINVAL;
2482         else if (index >= 32)
2483                 AR5K_REG_ENABLE_BITS(ah, AR5K_MCAST_FILTER1,
2484                                 (1 << (index - 32)));
2485         else
2486                 AR5K_REG_ENABLE_BITS(ah, AR5K_MCAST_FILTER0, (1 << index));
2487
2488         return 0;
2489 }
2490
2491 /*
2492  * Clear Multicast filter by index
2493  */
2494 int ath5k_hw_clear_mcast_filter_idx(struct ath5k_hw *ah, u32 index)
2495 {
2496
2497         ATH5K_TRACE(ah->ah_sc);
2498         if (index >= 64)
2499                 return -EINVAL;
2500         else if (index >= 32)
2501                 AR5K_REG_DISABLE_BITS(ah, AR5K_MCAST_FILTER1,
2502                                 (1 << (index - 32)));
2503         else
2504                 AR5K_REG_DISABLE_BITS(ah, AR5K_MCAST_FILTER0, (1 << index));
2505
2506         return 0;
2507 }
2508
2509 /*
2510  * Get current rx filter
2511  */
2512 u32 ath5k_hw_get_rx_filter(struct ath5k_hw *ah)
2513 {
2514         u32 data, filter = 0;
2515
2516         ATH5K_TRACE(ah->ah_sc);
2517         filter = ath5k_hw_reg_read(ah, AR5K_RX_FILTER);
2518
2519         /*Radar detection for 5212*/
2520         if (ah->ah_version == AR5K_AR5212) {
2521                 data = ath5k_hw_reg_read(ah, AR5K_PHY_ERR_FIL);
2522
2523                 if (data & AR5K_PHY_ERR_FIL_RADAR)
2524                         filter |= AR5K_RX_FILTER_RADARERR;
2525                 if (data & (AR5K_PHY_ERR_FIL_OFDM | AR5K_PHY_ERR_FIL_CCK))
2526                         filter |= AR5K_RX_FILTER_PHYERR;
2527         }
2528
2529         return filter;
2530 }
2531
2532 /*
2533  * Set rx filter
2534  */
2535 void ath5k_hw_set_rx_filter(struct ath5k_hw *ah, u32 filter)
2536 {
2537         u32 data = 0;
2538
2539         ATH5K_TRACE(ah->ah_sc);
2540
2541         /* Set PHY error filter register on 5212*/
2542         if (ah->ah_version == AR5K_AR5212) {
2543                 if (filter & AR5K_RX_FILTER_RADARERR)
2544                         data |= AR5K_PHY_ERR_FIL_RADAR;
2545                 if (filter & AR5K_RX_FILTER_PHYERR)
2546                         data |= AR5K_PHY_ERR_FIL_OFDM | AR5K_PHY_ERR_FIL_CCK;
2547         }
2548
2549         /*
2550          * The AR5210 uses promiscous mode to detect radar activity
2551          */
2552         if (ah->ah_version == AR5K_AR5210 &&
2553                         (filter & AR5K_RX_FILTER_RADARERR)) {
2554                 filter &= ~AR5K_RX_FILTER_RADARERR;
2555                 filter |= AR5K_RX_FILTER_PROM;
2556         }
2557
2558         /*Zero length DMA*/
2559         if (data)
2560                 AR5K_REG_ENABLE_BITS(ah, AR5K_RXCFG, AR5K_RXCFG_ZLFDMA);
2561         else
2562                 AR5K_REG_DISABLE_BITS(ah, AR5K_RXCFG, AR5K_RXCFG_ZLFDMA);
2563
2564         /*Write RX Filter register*/
2565         ath5k_hw_reg_write(ah, filter & 0xff, AR5K_RX_FILTER);
2566
2567         /*Write PHY error filter register on 5212*/
2568         if (ah->ah_version == AR5K_AR5212)
2569                 ath5k_hw_reg_write(ah, data, AR5K_PHY_ERR_FIL);
2570
2571 }
2572
2573 /*
2574  * Beacon related functions
2575  */
2576
2577 /*
2578  * Get a 32bit TSF
2579  */
2580 u32 ath5k_hw_get_tsf32(struct ath5k_hw *ah)
2581 {
2582         ATH5K_TRACE(ah->ah_sc);
2583         return ath5k_hw_reg_read(ah, AR5K_TSF_L32);
2584 }
2585
2586 /*
2587  * Get the full 64bit TSF
2588  */
2589 u64 ath5k_hw_get_tsf64(struct ath5k_hw *ah)
2590 {
2591         u64 tsf = ath5k_hw_reg_read(ah, AR5K_TSF_U32);
2592         ATH5K_TRACE(ah->ah_sc);
2593
2594         return ath5k_hw_reg_read(ah, AR5K_TSF_L32) | (tsf << 32);
2595 }
2596
2597 /*
2598  * Force a TSF reset
2599  */
2600 void ath5k_hw_reset_tsf(struct ath5k_hw *ah)
2601 {
2602         ATH5K_TRACE(ah->ah_sc);
2603         AR5K_REG_ENABLE_BITS(ah, AR5K_BEACON, AR5K_BEACON_RESET_TSF);
2604 }
2605
2606 /*
2607  * Initialize beacon timers
2608  */
2609 void ath5k_hw_init_beacon(struct ath5k_hw *ah, u32 next_beacon, u32 interval)
2610 {
2611         u32 timer1, timer2, timer3;
2612
2613         ATH5K_TRACE(ah->ah_sc);
2614         /*
2615          * Set the additional timers by mode
2616          */
2617         switch (ah->ah_op_mode) {
2618         case IEEE80211_IF_TYPE_STA:
2619                 if (ah->ah_version == AR5K_AR5210) {
2620                         timer1 = 0xffffffff;
2621                         timer2 = 0xffffffff;
2622                 } else {
2623                         timer1 = 0x0000ffff;
2624                         timer2 = 0x0007ffff;
2625                 }
2626                 break;
2627
2628         default:
2629                 timer1 = (next_beacon - AR5K_TUNE_DMA_BEACON_RESP) << 3;
2630                 timer2 = (next_beacon - AR5K_TUNE_SW_BEACON_RESP) << 3;
2631         }
2632
2633         timer3 = next_beacon + (ah->ah_atim_window ? ah->ah_atim_window : 1);
2634
2635         /*
2636          * Set the beacon register and enable all timers.
2637          * (next beacon, DMA beacon, software beacon, ATIM window time)
2638          */
2639         ath5k_hw_reg_write(ah, next_beacon, AR5K_TIMER0);
2640         ath5k_hw_reg_write(ah, timer1, AR5K_TIMER1);
2641         ath5k_hw_reg_write(ah, timer2, AR5K_TIMER2);
2642         ath5k_hw_reg_write(ah, timer3, AR5K_TIMER3);
2643
2644         ath5k_hw_reg_write(ah, interval & (AR5K_BEACON_PERIOD |
2645                         AR5K_BEACON_RESET_TSF | AR5K_BEACON_ENABLE),
2646                 AR5K_BEACON);
2647 }
2648
2649 #if 0
2650 /*
2651  * Set beacon timers
2652  */
2653 int ath5k_hw_set_beacon_timers(struct ath5k_hw *ah,
2654                 const struct ath5k_beacon_state *state)
2655 {
2656         u32 cfp_period, next_cfp, dtim, interval, next_beacon;
2657
2658         /*
2659          * TODO: should be changed through *state
2660          * review struct ath5k_beacon_state struct
2661          *
2662          * XXX: These are used for cfp period bellow, are they
2663          * ok ? Is it O.K. for tsf here to be 0 or should we use
2664          * get_tsf ?
2665          */
2666         u32 dtim_count = 0; /* XXX */
2667         u32 cfp_count = 0; /* XXX */
2668         u32 tsf = 0; /* XXX */
2669
2670         ATH5K_TRACE(ah->ah_sc);
2671         /* Return on an invalid beacon state */
2672         if (state->bs_interval < 1)
2673                 return -EINVAL;
2674
2675         interval = state->bs_interval;
2676         dtim = state->bs_dtim_period;
2677
2678         /*
2679          * PCF support?
2680          */
2681         if (state->bs_cfp_period > 0) {
2682                 /*
2683                  * Enable PCF mode and set the CFP
2684                  * (Contention Free Period) and timer registers
2685                  */
2686                 cfp_period = state->bs_cfp_period * state->bs_dtim_period *
2687                         state->bs_interval;
2688                 next_cfp = (cfp_count * state->bs_dtim_period + dtim_count) *
2689                         state->bs_interval;
2690
2691                 AR5K_REG_ENABLE_BITS(ah, AR5K_STA_ID1,
2692                                 AR5K_STA_ID1_DEFAULT_ANTENNA |
2693                                 AR5K_STA_ID1_PCF);
2694                 ath5k_hw_reg_write(ah, cfp_period, AR5K_CFP_PERIOD);
2695                 ath5k_hw_reg_write(ah, state->bs_cfp_max_duration,
2696                                 AR5K_CFP_DUR);
2697                 ath5k_hw_reg_write(ah, (tsf + (next_cfp == 0 ? cfp_period :
2698                                                 next_cfp)) << 3, AR5K_TIMER2);
2699         } else {
2700                 /* Disable PCF mode */
2701                 AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1,
2702                                 AR5K_STA_ID1_DEFAULT_ANTENNA |
2703                                 AR5K_STA_ID1_PCF);
2704         }
2705
2706         /*
2707          * Enable the beacon timer register
2708          */
2709         ath5k_hw_reg_write(ah, state->bs_next_beacon, AR5K_TIMER0);
2710
2711         /*
2712          * Start the beacon timers
2713          */
2714         ath5k_hw_reg_write(ah, (ath5k_hw_reg_read(ah, AR5K_BEACON) &~
2715                 (AR5K_BEACON_PERIOD | AR5K_BEACON_TIM)) |
2716                 AR5K_REG_SM(state->bs_tim_offset ? state->bs_tim_offset + 4 : 0,
2717                 AR5K_BEACON_TIM) | AR5K_REG_SM(state->bs_interval,
2718                 AR5K_BEACON_PERIOD), AR5K_BEACON);
2719
2720         /*
2721          * Write new beacon miss threshold, if it appears to be valid
2722          * XXX: Figure out right values for min <= bs_bmiss_threshold <= max
2723          * and return if its not in range. We can test this by reading value and
2724          * setting value to a largest value and seeing which values register.
2725          */
2726
2727         AR5K_REG_WRITE_BITS(ah, AR5K_RSSI_THR, AR5K_RSSI_THR_BMISS,
2728                         state->bs_bmiss_threshold);
2729
2730         /*
2731          * Set sleep control register
2732          * XXX: Didn't find this in 5210 code but since this register
2733          * exists also in ar5k's 5210 headers i leave it as common code.
2734          */
2735         AR5K_REG_WRITE_BITS(ah, AR5K_SLEEP_CTL, AR5K_SLEEP_CTL_SLDUR,
2736                         (state->bs_sleep_duration - 3) << 3);
2737
2738         /*
2739          * Set enhanced sleep registers on 5212
2740          */
2741         if (ah->ah_version == AR5K_AR5212) {
2742                 if (state->bs_sleep_duration > state->bs_interval &&
2743                                 roundup(state->bs_sleep_duration, interval) ==
2744                                 state->bs_sleep_duration)
2745                         interval = state->bs_sleep_duration;
2746
2747                 if (state->bs_sleep_duration > dtim && (dtim == 0 ||
2748                                 roundup(state->bs_sleep_duration, dtim) ==
2749                                 state->bs_sleep_duration))
2750                         dtim = state->bs_sleep_duration;
2751
2752                 if (interval > dtim)
2753                         return -EINVAL;
2754
2755                 next_beacon = interval == dtim ? state->bs_next_dtim :
2756                         state->bs_next_beacon;
2757
2758                 ath5k_hw_reg_write(ah,
2759                         AR5K_REG_SM((state->bs_next_dtim - 3) << 3,
2760                         AR5K_SLEEP0_NEXT_DTIM) |
2761                         AR5K_REG_SM(10, AR5K_SLEEP0_CABTO) |
2762                         AR5K_SLEEP0_ENH_SLEEP_EN |
2763                         AR5K_SLEEP0_ASSUME_DTIM, AR5K_SLEEP0);
2764
2765                 ath5k_hw_reg_write(ah, AR5K_REG_SM((next_beacon - 3) << 3,
2766                         AR5K_SLEEP1_NEXT_TIM) |
2767                         AR5K_REG_SM(10, AR5K_SLEEP1_BEACON_TO), AR5K_SLEEP1);
2768
2769                 ath5k_hw_reg_write(ah,
2770                         AR5K_REG_SM(interval, AR5K_SLEEP2_TIM_PER) |
2771                         AR5K_REG_SM(dtim, AR5K_SLEEP2_DTIM_PER), AR5K_SLEEP2);
2772         }
2773
2774         return 0;
2775 }
2776
2777 /*
2778  * Reset beacon timers
2779  */
2780 void ath5k_hw_reset_beacon(struct ath5k_hw *ah)
2781 {
2782         ATH5K_TRACE(ah->ah_sc);
2783         /*
2784          * Disable beacon timer
2785          */
2786         ath5k_hw_reg_write(ah, 0, AR5K_TIMER0);
2787
2788         /*
2789          * Disable some beacon register values
2790          */
2791         AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1,
2792                         AR5K_STA_ID1_DEFAULT_ANTENNA | AR5K_STA_ID1_PCF);
2793         ath5k_hw_reg_write(ah, AR5K_BEACON_PERIOD, AR5K_BEACON);
2794 }
2795
2796 /*
2797  * Wait for beacon queue to finish
2798  */
2799 int ath5k_hw_beaconq_finish(struct ath5k_hw *ah, unsigned long phys_addr)
2800 {
2801         unsigned int i;
2802         int ret;
2803
2804         ATH5K_TRACE(ah->ah_sc);
2805
2806         /* 5210 doesn't have QCU*/
2807         if (ah->ah_version == AR5K_AR5210) {
2808                 /*
2809                  * Wait for beaconn queue to finish by checking
2810                  * Control Register and Beacon Status Register.
2811                  */
2812                 for (i = AR5K_TUNE_BEACON_INTERVAL / 2; i > 0; i--) {
2813                         if (!(ath5k_hw_reg_read(ah, AR5K_BSR) & AR5K_BSR_TXQ1F)
2814                                         ||
2815                             !(ath5k_hw_reg_read(ah, AR5K_CR) & AR5K_BSR_TXQ1F))
2816                                 break;
2817                         udelay(10);
2818                 }
2819
2820                 /* Timeout... */
2821                 if (i <= 0) {
2822                         /*
2823                          * Re-schedule the beacon queue
2824                          */
2825                         ath5k_hw_reg_write(ah, phys_addr, AR5K_NOQCU_TXDP1);
2826                         ath5k_hw_reg_write(ah, AR5K_BCR_TQ1V | AR5K_BCR_BDMAE,
2827                                         AR5K_BCR);
2828
2829                         return -EIO;
2830                 }
2831                 ret = 0;
2832         } else {
2833         /*5211/5212*/
2834                 ret = ath5k_hw_register_timeout(ah,
2835                         AR5K_QUEUE_STATUS(AR5K_TX_QUEUE_ID_BEACON),
2836                         AR5K_QCU_STS_FRMPENDCNT, 0, false);
2837
2838                 if (AR5K_REG_READ_Q(ah, AR5K_QCU_TXE, AR5K_TX_QUEUE_ID_BEACON))
2839                         return -EIO;
2840         }
2841
2842         return ret;
2843 }
2844 #endif
2845
2846 /*
2847  * Update mib counters (statistics)
2848  */
2849 void ath5k_hw_update_mib_counters(struct ath5k_hw *ah,
2850                 struct ath5k_mib_stats *statistics)
2851 {
2852         ATH5K_TRACE(ah->ah_sc);
2853         /* Read-And-Clear */
2854         statistics->ackrcv_bad += ath5k_hw_reg_read(ah, AR5K_ACK_FAIL);
2855         statistics->rts_bad += ath5k_hw_reg_read(ah, AR5K_RTS_FAIL);
2856         statistics->rts_good += ath5k_hw_reg_read(ah, AR5K_RTS_OK);
2857         statistics->fcs_bad += ath5k_hw_reg_read(ah, AR5K_FCS_FAIL);
2858         statistics->beacons += ath5k_hw_reg_read(ah, AR5K_BEACON_CNT);
2859
2860         /* Reset profile count registers on 5212*/
2861         if (ah->ah_version == AR5K_AR5212) {
2862                 ath5k_hw_reg_write(ah, 0, AR5K_PROFCNT_TX);
2863                 ath5k_hw_reg_write(ah, 0, AR5K_PROFCNT_RX);
2864                 ath5k_hw_reg_write(ah, 0, AR5K_PROFCNT_RXCLR);
2865                 ath5k_hw_reg_write(ah, 0, AR5K_PROFCNT_CYCLE);
2866         }
2867 }
2868
2869 /** ath5k_hw_set_ack_bitrate - set bitrate for ACKs
2870  *
2871  * @ah: the &struct ath5k_hw
2872  * @high: determines if to use low bit rate or now
2873  */
2874 void ath5k_hw_set_ack_bitrate_high(struct ath5k_hw *ah, bool high)
2875 {
2876         if (ah->ah_version != AR5K_AR5212)
2877                 return;
2878         else {
2879                 u32 val = AR5K_STA_ID1_BASE_RATE_11B | AR5K_STA_ID1_ACKCTS_6MB;
2880                 if (high)
2881                         AR5K_REG_ENABLE_BITS(ah, AR5K_STA_ID1, val);
2882                 else
2883                         AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1, val);
2884         }
2885 }
2886
2887
2888 /*
2889  * ACK/CTS Timeouts
2890  */
2891
2892 /*
2893  * Set ACK timeout on PCU
2894  */
2895 int ath5k_hw_set_ack_timeout(struct ath5k_hw *ah, unsigned int timeout)
2896 {
2897         ATH5K_TRACE(ah->ah_sc);
2898         if (ath5k_hw_clocktoh(AR5K_REG_MS(0xffffffff, AR5K_TIME_OUT_ACK),
2899                         ah->ah_turbo) <= timeout)
2900                 return -EINVAL;
2901
2902         AR5K_REG_WRITE_BITS(ah, AR5K_TIME_OUT, AR5K_TIME_OUT_ACK,
2903                 ath5k_hw_htoclock(timeout, ah->ah_turbo));
2904
2905         return 0;
2906 }
2907
2908 /*
2909  * Read the ACK timeout from PCU
2910  */
2911 unsigned int ath5k_hw_get_ack_timeout(struct ath5k_hw *ah)
2912 {
2913         ATH5K_TRACE(ah->ah_sc);
2914
2915         return ath5k_hw_clocktoh(AR5K_REG_MS(ath5k_hw_reg_read(ah,
2916                         AR5K_TIME_OUT), AR5K_TIME_OUT_ACK), ah->ah_turbo);
2917 }
2918
2919 /*
2920  * Set CTS timeout on PCU
2921  */
2922 int ath5k_hw_set_cts_timeout(struct ath5k_hw *ah, unsigned int timeout)
2923 {
2924         ATH5K_TRACE(ah->ah_sc);
2925         if (ath5k_hw_clocktoh(AR5K_REG_MS(0xffffffff, AR5K_TIME_OUT_CTS),
2926                         ah->ah_turbo) <= timeout)
2927                 return -EINVAL;
2928
2929         AR5K_REG_WRITE_BITS(ah, AR5K_TIME_OUT, AR5K_TIME_OUT_CTS,
2930                         ath5k_hw_htoclock(timeout, ah->ah_turbo));
2931
2932         return 0;
2933 }
2934
2935 /*
2936  * Read CTS timeout from PCU
2937  */
2938 unsigned int ath5k_hw_get_cts_timeout(struct ath5k_hw *ah)
2939 {
2940         ATH5K_TRACE(ah->ah_sc);
2941         return ath5k_hw_clocktoh(AR5K_REG_MS(ath5k_hw_reg_read(ah,
2942                         AR5K_TIME_OUT), AR5K_TIME_OUT_CTS), ah->ah_turbo);
2943 }
2944
2945 /*
2946  * Key table (WEP) functions
2947  */
2948
2949 int ath5k_hw_reset_key(struct ath5k_hw *ah, u16 entry)
2950 {
2951         unsigned int i;
2952
2953         ATH5K_TRACE(ah->ah_sc);
2954         AR5K_ASSERT_ENTRY(entry, AR5K_KEYTABLE_SIZE);
2955
2956         for (i = 0; i < AR5K_KEYCACHE_SIZE; i++)
2957                 ath5k_hw_reg_write(ah, 0, AR5K_KEYTABLE_OFF(entry, i));
2958
2959         /* Set NULL encryption on non-5210*/
2960         if (ah->ah_version != AR5K_AR5210)
2961                 ath5k_hw_reg_write(ah, AR5K_KEYTABLE_TYPE_NULL,
2962                                 AR5K_KEYTABLE_TYPE(entry));
2963
2964         return 0;
2965 }
2966
2967 int ath5k_hw_is_key_valid(struct ath5k_hw *ah, u16 entry)
2968 {
2969         ATH5K_TRACE(ah->ah_sc);
2970         AR5K_ASSERT_ENTRY(entry, AR5K_KEYTABLE_SIZE);
2971
2972         /* Check the validation flag at the end of the entry */
2973         return ath5k_hw_reg_read(ah, AR5K_KEYTABLE_MAC1(entry)) &
2974                 AR5K_KEYTABLE_VALID;
2975 }
2976
2977 int ath5k_hw_set_key(struct ath5k_hw *ah, u16 entry,
2978                 const struct ieee80211_key_conf *key, const u8 *mac)
2979 {
2980         unsigned int i;
2981         __le32 key_v[5] = {};
2982         u32 keytype;
2983
2984         ATH5K_TRACE(ah->ah_sc);
2985
2986         /* key->keylen comes in from mac80211 in bytes */
2987
2988         if (key->keylen > AR5K_KEYTABLE_SIZE / 8)
2989                 return -EOPNOTSUPP;
2990
2991         switch (key->keylen) {
2992         /* WEP 40-bit   = 40-bit  entered key + 24 bit IV = 64-bit */
2993         case 40 / 8:
2994                 memcpy(&key_v[0], key->key, 5);
2995                 keytype = AR5K_KEYTABLE_TYPE_40;
2996                 break;
2997
2998         /* WEP 104-bit  = 104-bit entered key + 24-bit IV = 128-bit */
2999         case 104 / 8:
3000                 memcpy(&key_v[0], &key->key[0], 6);
3001                 memcpy(&key_v[2], &key->key[6], 6);
3002                 memcpy(&key_v[4], &key->key[12], 1);
3003                 keytype = AR5K_KEYTABLE_TYPE_104;
3004                 break;
3005         /* WEP 128-bit  = 128-bit entered key + 24 bit IV = 152-bit */
3006         case 128 / 8:
3007                 memcpy(&key_v[0], &key->key[0], 6);
3008                 memcpy(&key_v[2], &key->key[6], 6);
3009                 memcpy(&key_v[4], &key->key[12], 4);
3010                 keytype = AR5K_KEYTABLE_TYPE_128;
3011                 break;
3012
3013         default:
3014                 return -EINVAL; /* shouldn't happen */
3015         }
3016
3017         for (i = 0; i < ARRAY_SIZE(key_v); i++)
3018                 ath5k_hw_reg_write(ah, le32_to_cpu(key_v[i]),
3019                                 AR5K_KEYTABLE_OFF(entry, i));
3020
3021         ath5k_hw_reg_write(ah, keytype, AR5K_KEYTABLE_TYPE(entry));
3022
3023         return ath5k_hw_set_key_lladdr(ah, entry, mac);
3024 }
3025
3026 int ath5k_hw_set_key_lladdr(struct ath5k_hw *ah, u16 entry, const u8 *mac)
3027 {
3028         u32 low_id, high_id;
3029
3030         ATH5K_TRACE(ah->ah_sc);
3031          /* Invalid entry (key table overflow) */
3032         AR5K_ASSERT_ENTRY(entry, AR5K_KEYTABLE_SIZE);
3033
3034         /* MAC may be NULL if it's a broadcast key. In this case no need to
3035          * to compute AR5K_LOW_ID and AR5K_HIGH_ID as we already know it. */
3036         if (unlikely(mac == NULL)) {
3037                 low_id = 0xffffffff;
3038                 high_id = 0xffff | AR5K_KEYTABLE_VALID;
3039         } else {
3040                 low_id = AR5K_LOW_ID(mac);
3041                 high_id = AR5K_HIGH_ID(mac) | AR5K_KEYTABLE_VALID;
3042         }
3043
3044         ath5k_hw_reg_write(ah, low_id, AR5K_KEYTABLE_MAC0(entry));
3045         ath5k_hw_reg_write(ah, high_id, AR5K_KEYTABLE_MAC1(entry));
3046
3047         return 0;
3048 }
3049
3050
3051 /********************************************\
3052 Queue Control Unit, DFS Control Unit Functions
3053 \********************************************/
3054
3055 /*
3056  * Initialize a transmit queue
3057  */
3058 int ath5k_hw_setup_tx_queue(struct ath5k_hw *ah, enum ath5k_tx_queue queue_type,
3059                 struct ath5k_txq_info *queue_info)
3060 {
3061         unsigned int queue;
3062         int ret;
3063
3064         ATH5K_TRACE(ah->ah_sc);
3065
3066         /*
3067          * Get queue by type
3068          */
3069         /*5210 only has 2 queues*/
3070         if (ah->ah_version == AR5K_AR5210) {
3071                 switch (queue_type) {
3072                 case AR5K_TX_QUEUE_DATA:
3073                         queue = AR5K_TX_QUEUE_ID_NOQCU_DATA;
3074                         break;
3075                 case AR5K_TX_QUEUE_BEACON:
3076                 case AR5K_TX_QUEUE_CAB:
3077                         queue = AR5K_TX_QUEUE_ID_NOQCU_BEACON;
3078                         break;
3079                 default:
3080                         return -EINVAL;
3081                 }
3082         } else {
3083                 switch (queue_type) {
3084                 case AR5K_TX_QUEUE_DATA:
3085                         for (queue = AR5K_TX_QUEUE_ID_DATA_MIN;
3086                                 ah->ah_txq[queue].tqi_type !=
3087                                 AR5K_TX_QUEUE_INACTIVE; queue++) {
3088
3089                                 if (queue > AR5K_TX_QUEUE_ID_DATA_MAX)
3090                                         return -EINVAL;
3091                         }
3092                         break;
3093                 case AR5K_TX_QUEUE_UAPSD:
3094                         queue = AR5K_TX_QUEUE_ID_UAPSD;
3095                         break;
3096                 case AR5K_TX_QUEUE_BEACON:
3097                         queue = AR5K_TX_QUEUE_ID_BEACON;
3098                         break;
3099                 case AR5K_TX_QUEUE_CAB:
3100                         queue = AR5K_TX_QUEUE_ID_CAB;
3101                         break;
3102                 case AR5K_TX_QUEUE_XR_DATA:
3103                         if (ah->ah_version != AR5K_AR5212)
3104                                 ATH5K_ERR(ah->ah_sc,
3105                                         "XR data queues only supported in"
3106                                         " 5212!\n");
3107                         queue = AR5K_TX_QUEUE_ID_XR_DATA;
3108                         break;
3109                 default:
3110                         return -EINVAL;
3111                 }
3112         }
3113
3114         /*
3115          * Setup internal queue structure
3116          */
3117         memset(&ah->ah_txq[queue], 0, sizeof(struct ath5k_txq_info));
3118         ah->ah_txq[queue].tqi_type = queue_type;
3119
3120         if (queue_info != NULL) {
3121                 queue_info->tqi_type = queue_type;
3122                 ret = ath5k_hw_setup_tx_queueprops(ah, queue, queue_info);
3123                 if (ret)
3124                         return ret;
3125         }
3126         /*
3127          * We use ah_txq_status to hold a temp value for
3128          * the Secondary interrupt mask registers on 5211+
3129          * check out ath5k_hw_reset_tx_queue
3130          */
3131         AR5K_Q_ENABLE_BITS(ah->ah_txq_status, queue);
3132
3133         return queue;
3134 }
3135
3136 /*
3137  * Setup a transmit queue
3138  */
3139 int ath5k_hw_setup_tx_queueprops(struct ath5k_hw *ah, int queue,
3140                                 const struct ath5k_txq_info *queue_info)
3141 {
3142         ATH5K_TRACE(ah->ah_sc);
3143         AR5K_ASSERT_ENTRY(queue, ah->ah_capabilities.cap_queues.q_tx_num);
3144
3145         if (ah->ah_txq[queue].tqi_type == AR5K_TX_QUEUE_INACTIVE)
3146                 return -EIO;
3147
3148         memcpy(&ah->ah_txq[queue], queue_info, sizeof(struct ath5k_txq_info));
3149
3150         /*XXX: Is this supported on 5210 ?*/
3151         if ((queue_info->tqi_type == AR5K_TX_QUEUE_DATA &&
3152                         ((queue_info->tqi_subtype == AR5K_WME_AC_VI) ||
3153                         (queue_info->tqi_subtype == AR5K_WME_AC_VO))) ||
3154                         queue_info->tqi_type == AR5K_TX_QUEUE_UAPSD)
3155                 ah->ah_txq[queue].tqi_flags |= AR5K_TXQ_FLAG_POST_FR_BKOFF_DIS;
3156
3157         return 0;
3158 }
3159
3160 /*
3161  * Get properties for a specific transmit queue
3162  */
3163 int ath5k_hw_get_tx_queueprops(struct ath5k_hw *ah, int queue,
3164                 struct ath5k_txq_info *queue_info)
3165 {
3166         ATH5K_TRACE(ah->ah_sc);
3167         memcpy(queue_info, &ah->ah_txq[queue], sizeof(struct ath5k_txq_info));
3168         return 0;
3169 }
3170
3171 /*
3172  * Set a transmit queue inactive
3173  */
3174 void ath5k_hw_release_tx_queue(struct ath5k_hw *ah, unsigned int queue)
3175 {
3176         ATH5K_TRACE(ah->ah_sc);
3177         if (WARN_ON(queue >= ah->ah_capabilities.cap_queues.q_tx_num))
3178                 return;
3179
3180         /* This queue will be skipped in further operations */
3181         ah->ah_txq[queue].tqi_type = AR5K_TX_QUEUE_INACTIVE;
3182         /*For SIMR setup*/
3183         AR5K_Q_DISABLE_BITS(ah->ah_txq_status, queue);
3184 }
3185
3186 /*
3187  * Set DFS params for a transmit queue
3188  */
3189 int ath5k_hw_reset_tx_queue(struct ath5k_hw *ah, unsigned int queue)
3190 {
3191         u32 cw_min, cw_max, retry_lg, retry_sh;
3192         struct ath5k_txq_info *tq = &ah->ah_txq[queue];
3193
3194         ATH5K_TRACE(ah->ah_sc);
3195         AR5K_ASSERT_ENTRY(queue, ah->ah_capabilities.cap_queues.q_tx_num);
3196
3197         tq = &ah->ah_txq[queue];
3198
3199         if (tq->tqi_type == AR5K_TX_QUEUE_INACTIVE)
3200                 return 0;
3201
3202         if (ah->ah_version == AR5K_AR5210) {
3203                 /* Only handle data queues, others will be ignored */
3204                 if (tq->tqi_type != AR5K_TX_QUEUE_DATA)
3205                         return 0;
3206
3207                 /* Set Slot time */
3208                 ath5k_hw_reg_write(ah, ah->ah_turbo == true ?
3209                         AR5K_INIT_SLOT_TIME_TURBO : AR5K_INIT_SLOT_TIME,
3210                         AR5K_SLOT_TIME);
3211                 /* Set ACK_CTS timeout */
3212                 ath5k_hw_reg_write(ah, ah->ah_turbo == true ?
3213                         AR5K_INIT_ACK_CTS_TIMEOUT_TURBO :
3214                         AR5K_INIT_ACK_CTS_TIMEOUT, AR5K_SLOT_TIME);
3215                 /* Set Transmit Latency */
3216                 ath5k_hw_reg_write(ah, ah->ah_turbo == true ?
3217                         AR5K_INIT_TRANSMIT_LATENCY_TURBO :
3218                         AR5K_INIT_TRANSMIT_LATENCY, AR5K_USEC_5210);
3219                 /* Set IFS0 */
3220                 if (ah->ah_turbo == true)
3221                          ath5k_hw_reg_write(ah, ((AR5K_INIT_SIFS_TURBO +
3222                                 (ah->ah_aifs + tq->tqi_aifs) *
3223                                 AR5K_INIT_SLOT_TIME_TURBO) <<
3224                                 AR5K_IFS0_DIFS_S) | AR5K_INIT_SIFS_TURBO,
3225                                 AR5K_IFS0);
3226                 else
3227                         ath5k_hw_reg_write(ah, ((AR5K_INIT_SIFS +
3228                                 (ah->ah_aifs + tq->tqi_aifs) *
3229                                 AR5K_INIT_SLOT_TIME) << AR5K_IFS0_DIFS_S) |
3230                                 AR5K_INIT_SIFS, AR5K_IFS0);
3231
3232                 /* Set IFS1 */
3233                 ath5k_hw_reg_write(ah, ah->ah_turbo == true ?
3234                         AR5K_INIT_PROTO_TIME_CNTRL_TURBO :
3235                         AR5K_INIT_PROTO_TIME_CNTRL, AR5K_IFS1);
3236                 /* Set PHY register 0x9844 (??) */
3237                 ath5k_hw_reg_write(ah, ah->ah_turbo == true ?
3238                         (ath5k_hw_reg_read(ah, AR5K_PHY(17)) & ~0x7F) | 0x38 :
3239                         (ath5k_hw_reg_read(ah, AR5K_PHY(17)) & ~0x7F) | 0x1C,
3240                         AR5K_PHY(17));
3241                 /* Set Frame Control Register */
3242                 ath5k_hw_reg_write(ah, ah->ah_turbo == true ?
3243                         (AR5K_PHY_FRAME_CTL_INI | AR5K_PHY_TURBO_MODE |
3244                         AR5K_PHY_TURBO_SHORT | 0x2020) :
3245                         (AR5K_PHY_FRAME_CTL_INI | 0x1020),
3246                         AR5K_PHY_FRAME_CTL_5210);
3247         }
3248
3249         /*
3250          * Calculate cwmin/max by channel mode
3251          */
3252         cw_min = ah->ah_cw_min = AR5K_TUNE_CWMIN;
3253         cw_max = ah->ah_cw_max = AR5K_TUNE_CWMAX;
3254         ah->ah_aifs = AR5K_TUNE_AIFS;
3255         /*XR is only supported on 5212*/
3256         if (IS_CHAN_XR(ah->ah_current_channel) &&
3257                         ah->ah_version == AR5K_AR5212) {
3258                 cw_min = ah->ah_cw_min = AR5K_TUNE_CWMIN_XR;
3259                 cw_max = ah->ah_cw_max = AR5K_TUNE_CWMAX_XR;
3260                 ah->ah_aifs = AR5K_TUNE_AIFS_XR;
3261         /*B mode is not supported on 5210*/
3262         } else if (IS_CHAN_B(ah->ah_current_channel) &&
3263                         ah->ah_version != AR5K_AR5210) {
3264                 cw_min = ah->ah_cw_min = AR5K_TUNE_CWMIN_11B;
3265                 cw_max = ah->ah_cw_max = AR5K_TUNE_CWMAX_11B;
3266                 ah->ah_aifs = AR5K_TUNE_AIFS_11B;
3267         }
3268
3269         cw_min = 1;
3270         while (cw_min < ah->ah_cw_min)
3271                 cw_min = (cw_min << 1) | 1;
3272
3273         cw_min = tq->tqi_cw_min < 0 ? (cw_min >> (-tq->tqi_cw_min)) :
3274                 ((cw_min << tq->tqi_cw_min) + (1 << tq->tqi_cw_min) - 1);
3275         cw_max = tq->tqi_cw_max < 0 ? (cw_max >> (-tq->tqi_cw_max)) :
3276                 ((cw_max << tq->tqi_cw_max) + (1 << tq->tqi_cw_max) - 1);
3277
3278         /*
3279          * Calculate and set retry limits
3280          */
3281         if (ah->ah_software_retry == true) {
3282                 /* XXX Need to test this */
3283                 retry_lg = ah->ah_limit_tx_retries;
3284                 retry_sh = retry_lg = retry_lg > AR5K_DCU_RETRY_LMT_SH_RETRY ?
3285                         AR5K_DCU_RETRY_LMT_SH_RETRY : retry_lg;
3286         } else {
3287                 retry_lg = AR5K_INIT_LG_RETRY;
3288                 retry_sh = AR5K_INIT_SH_RETRY;
3289         }
3290
3291         /*No QCU/DCU [5210]*/
3292         if (ah->ah_version == AR5K_AR5210) {
3293                 ath5k_hw_reg_write(ah,
3294                         (cw_min << AR5K_NODCU_RETRY_LMT_CW_MIN_S)
3295                         | AR5K_REG_SM(AR5K_INIT_SLG_RETRY,
3296                                 AR5K_NODCU_RETRY_LMT_SLG_RETRY)
3297                         | AR5K_REG_SM(AR5K_INIT_SSH_RETRY,
3298                                 AR5K_NODCU_RETRY_LMT_SSH_RETRY)
3299                         | AR5K_REG_SM(retry_lg, AR5K_NODCU_RETRY_LMT_LG_RETRY)
3300                         | AR5K_REG_SM(retry_sh, AR5K_NODCU_RETRY_LMT_SH_RETRY),
3301                         AR5K_NODCU_RETRY_LMT);
3302         } else {
3303                 /*QCU/DCU [5211+]*/
3304                 ath5k_hw_reg_write(ah,
3305                         AR5K_REG_SM(AR5K_INIT_SLG_RETRY,
3306                                 AR5K_DCU_RETRY_LMT_SLG_RETRY) |
3307                         AR5K_REG_SM(AR5K_INIT_SSH_RETRY,
3308                                 AR5K_DCU_RETRY_LMT_SSH_RETRY) |
3309                         AR5K_REG_SM(retry_lg, AR5K_DCU_RETRY_LMT_LG_RETRY) |
3310                         AR5K_REG_SM(retry_sh, AR5K_DCU_RETRY_LMT_SH_RETRY),
3311                         AR5K_QUEUE_DFS_RETRY_LIMIT(queue));
3312
3313         /*===Rest is also for QCU/DCU only [5211+]===*/
3314
3315                 /*
3316                  * Set initial content window (cw_min/cw_max)
3317                  * and arbitrated interframe space (aifs)...
3318                  */
3319                 ath5k_hw_reg_write(ah,
3320                         AR5K_REG_SM(cw_min, AR5K_DCU_LCL_IFS_CW_MIN) |
3321                         AR5K_REG_SM(cw_max, AR5K_DCU_LCL_IFS_CW_MAX) |
3322                         AR5K_REG_SM(ah->ah_aifs + tq->tqi_aifs,
3323                                 AR5K_DCU_LCL_IFS_AIFS),
3324                         AR5K_QUEUE_DFS_LOCAL_IFS(queue));
3325
3326                 /*
3327                  * Set misc registers
3328                  */
3329                 ath5k_hw_reg_write(ah, AR5K_QCU_MISC_DCU_EARLY,
3330                         AR5K_QUEUE_MISC(queue));
3331
3332                 if (tq->tqi_cbr_period) {
3333                         ath5k_hw_reg_write(ah, AR5K_REG_SM(tq->tqi_cbr_period,
3334                                 AR5K_QCU_CBRCFG_INTVAL) |
3335                                 AR5K_REG_SM(tq->tqi_cbr_overflow_limit,
3336                                 AR5K_QCU_CBRCFG_ORN_THRES),
3337                                 AR5K_QUEUE_CBRCFG(queue));
3338                         AR5K_REG_ENABLE_BITS(ah, AR5K_QUEUE_MISC(queue),
3339                                 AR5K_QCU_MISC_FRSHED_CBR);
3340                         if (tq->tqi_cbr_overflow_limit)
3341                                 AR5K_REG_ENABLE_BITS(ah,
3342                                         AR5K_QUEUE_MISC(queue),
3343                                         AR5K_QCU_MISC_CBR_THRES_ENABLE);
3344                 }
3345
3346                 if (tq->tqi_ready_time)
3347                         ath5k_hw_reg_write(ah, AR5K_REG_SM(tq->tqi_ready_time,
3348                                 AR5K_QCU_RDYTIMECFG_INTVAL) |
3349                                 AR5K_QCU_RDYTIMECFG_ENABLE,
3350                                 AR5K_QUEUE_RDYTIMECFG(queue));
3351
3352                 if (tq->tqi_burst_time) {
3353                         ath5k_hw_reg_write(ah, AR5K_REG_SM(tq->tqi_burst_time,
3354                                 AR5K_DCU_CHAN_TIME_DUR) |
3355                                 AR5K_DCU_CHAN_TIME_ENABLE,
3356                                 AR5K_QUEUE_DFS_CHANNEL_TIME(queue));
3357
3358                         if (tq->tqi_flags & AR5K_TXQ_FLAG_RDYTIME_EXP_POLICY_ENABLE)
3359                                 AR5K_REG_ENABLE_BITS(ah,
3360                                         AR5K_QUEUE_MISC(queue),
3361                                         AR5K_QCU_MISC_TXE);
3362                 }
3363
3364                 if (tq->tqi_flags & AR5K_TXQ_FLAG_BACKOFF_DISABLE)
3365                         ath5k_hw_reg_write(ah, AR5K_DCU_MISC_POST_FR_BKOFF_DIS,
3366                                 AR5K_QUEUE_DFS_MISC(queue));
3367
3368                 if (tq->tqi_flags & AR5K_TXQ_FLAG_FRAG_BURST_BACKOFF_ENABLE)
3369                         ath5k_hw_reg_write(ah, AR5K_DCU_MISC_BACKOFF_FRAG,
3370                                 AR5K_QUEUE_DFS_MISC(queue));
3371
3372                 /*
3373                  * Set registers by queue type
3374                  */
3375                 switch (tq->tqi_type) {
3376                 case AR5K_TX_QUEUE_BEACON:
3377                         AR5K_REG_ENABLE_BITS(ah, AR5K_QUEUE_MISC(queue),
3378                                 AR5K_QCU_MISC_FRSHED_DBA_GT |
3379                                 AR5K_QCU_MISC_CBREXP_BCN |
3380                                 AR5K_QCU_MISC_BCN_ENABLE);
3381
3382                         AR5K_REG_ENABLE_BITS(ah, AR5K_QUEUE_DFS_MISC(queue),
3383                                 (AR5K_DCU_MISC_ARBLOCK_CTL_GLOBAL <<
3384                                 AR5K_DCU_MISC_ARBLOCK_CTL_S) |
3385                                 AR5K_DCU_MISC_POST_FR_BKOFF_DIS |
3386                                 AR5K_DCU_MISC_BCN_ENABLE);
3387
3388                         ath5k_hw_reg_write(ah, ((AR5K_TUNE_BEACON_INTERVAL -
3389                                 (AR5K_TUNE_SW_BEACON_RESP -
3390                                 AR5K_TUNE_DMA_BEACON_RESP) -
3391                                 AR5K_TUNE_ADDITIONAL_SWBA_BACKOFF) * 1024) |
3392                                 AR5K_QCU_RDYTIMECFG_ENABLE,
3393                                 AR5K_QUEUE_RDYTIMECFG(queue));
3394                         break;
3395
3396                 case AR5K_TX_QUEUE_CAB:
3397                         AR5K_REG_ENABLE_BITS(ah, AR5K_QUEUE_MISC(queue),
3398                                 AR5K_QCU_MISC_FRSHED_DBA_GT |
3399                                 AR5K_QCU_MISC_CBREXP |
3400                                 AR5K_QCU_MISC_CBREXP_BCN);
3401
3402                         AR5K_REG_ENABLE_BITS(ah, AR5K_QUEUE_DFS_MISC(queue),
3403                                 (AR5K_DCU_MISC_ARBLOCK_CTL_GLOBAL <<
3404                                 AR5K_DCU_MISC_ARBLOCK_CTL_S));
3405                         break;
3406
3407                 case AR5K_TX_QUEUE_UAPSD:
3408                         AR5K_REG_ENABLE_BITS(ah, AR5K_QUEUE_MISC(queue),
3409                                 AR5K_QCU_MISC_CBREXP);
3410                         break;
3411
3412                 case AR5K_TX_QUEUE_DATA:
3413                 default:
3414                         break;
3415                 }
3416
3417                 /*
3418                  * Enable interrupts for this tx queue
3419                  * in the secondary interrupt mask registers
3420                  */
3421                 if (tq->tqi_flags & AR5K_TXQ_FLAG_TXOKINT_ENABLE)
3422                         AR5K_Q_ENABLE_BITS(ah->ah_txq_imr_txok, queue);
3423
3424                 if (tq->tqi_flags & AR5K_TXQ_FLAG_TXERRINT_ENABLE)
3425                         AR5K_Q_ENABLE_BITS(ah->ah_txq_imr_txerr, queue);
3426
3427                 if (tq->tqi_flags & AR5K_TXQ_FLAG_TXURNINT_ENABLE)
3428                         AR5K_Q_ENABLE_BITS(ah->ah_txq_imr_txurn, queue);
3429
3430                 if (tq->tqi_flags & AR5K_TXQ_FLAG_TXDESCINT_ENABLE)
3431                         AR5K_Q_ENABLE_BITS(ah->ah_txq_imr_txdesc, queue);
3432
3433                 if (tq->tqi_flags & AR5K_TXQ_FLAG_TXEOLINT_ENABLE)
3434                         AR5K_Q_ENABLE_BITS(ah->ah_txq_imr_txeol, queue);
3435
3436
3437                 /* Update secondary interrupt mask registers */
3438                 ah->ah_txq_imr_txok &= ah->ah_txq_status;
3439                 ah->ah_txq_imr_txerr &= ah->ah_txq_status;
3440                 ah->ah_txq_imr_txurn &= ah->ah_txq_status;
3441                 ah->ah_txq_imr_txdesc &= ah->ah_txq_status;
3442                 ah->ah_txq_imr_txeol &= ah->ah_txq_status;
3443
3444                 ath5k_hw_reg_write(ah, AR5K_REG_SM(ah->ah_txq_imr_txok,
3445                         AR5K_SIMR0_QCU_TXOK) |
3446                         AR5K_REG_SM(ah->ah_txq_imr_txdesc,
3447                         AR5K_SIMR0_QCU_TXDESC), AR5K_SIMR0);
3448                 ath5k_hw_reg_write(ah, AR5K_REG_SM(ah->ah_txq_imr_txerr,
3449                         AR5K_SIMR1_QCU_TXERR) |
3450                         AR5K_REG_SM(ah->ah_txq_imr_txeol,
3451                         AR5K_SIMR1_QCU_TXEOL), AR5K_SIMR1);
3452                 ath5k_hw_reg_write(ah, AR5K_REG_SM(ah->ah_txq_imr_txurn,
3453                         AR5K_SIMR2_QCU_TXURN), AR5K_SIMR2);
3454         }
3455
3456         return 0;
3457 }
3458
3459 /*
3460  * Get number of pending frames
3461  * for a specific queue [5211+]
3462  */
3463 u32 ath5k_hw_num_tx_pending(struct ath5k_hw *ah, unsigned int queue) {
3464         ATH5K_TRACE(ah->ah_sc);
3465         AR5K_ASSERT_ENTRY(queue, ah->ah_capabilities.cap_queues.q_tx_num);
3466
3467         /* Return if queue is declared inactive */
3468         if (ah->ah_txq[queue].tqi_type == AR5K_TX_QUEUE_INACTIVE)
3469                 return false;
3470
3471         /* XXX: How about AR5K_CFG_TXCNT ? */
3472         if (ah->ah_version == AR5K_AR5210)
3473                 return false;
3474
3475         return AR5K_QUEUE_STATUS(queue) & AR5K_QCU_STS_FRMPENDCNT;
3476 }
3477
3478 /*
3479  * Set slot time
3480  */
3481 int ath5k_hw_set_slot_time(struct ath5k_hw *ah, unsigned int slot_time)
3482 {
3483         ATH5K_TRACE(ah->ah_sc);
3484         if (slot_time < AR5K_SLOT_TIME_9 || slot_time > AR5K_SLOT_TIME_MAX)
3485                 return -EINVAL;
3486
3487         if (ah->ah_version == AR5K_AR5210)
3488                 ath5k_hw_reg_write(ah, ath5k_hw_htoclock(slot_time,
3489                                 ah->ah_turbo), AR5K_SLOT_TIME);
3490         else
3491                 ath5k_hw_reg_write(ah, slot_time, AR5K_DCU_GBL_IFS_SLOT);
3492
3493         return 0;
3494 }
3495
3496 /*
3497  * Get slot time
3498  */
3499 unsigned int ath5k_hw_get_slot_time(struct ath5k_hw *ah)
3500 {
3501         ATH5K_TRACE(ah->ah_sc);
3502         if (ah->ah_version == AR5K_AR5210)
3503                 return ath5k_hw_clocktoh(ath5k_hw_reg_read(ah,
3504                                 AR5K_SLOT_TIME) & 0xffff, ah->ah_turbo);
3505         else
3506                 return ath5k_hw_reg_read(ah, AR5K_DCU_GBL_IFS_SLOT) & 0xffff;
3507 }
3508
3509
3510 /******************************\
3511  Hardware Descriptor Functions
3512 \******************************/
3513
3514 /*
3515  * TX Descriptor
3516  */
3517
3518 /*
3519  * Initialize the 2-word tx descriptor on 5210/5211
3520  */
3521 static int
3522 ath5k_hw_setup_2word_tx_desc(struct ath5k_hw *ah, struct ath5k_desc *desc,
3523         unsigned int pkt_len, unsigned int hdr_len, enum ath5k_pkt_type type,
3524         unsigned int tx_power, unsigned int tx_rate0, unsigned int tx_tries0,
3525         unsigned int key_index, unsigned int antenna_mode, unsigned int flags,
3526         unsigned int rtscts_rate, unsigned int rtscts_duration)
3527 {
3528         u32 frame_type;
3529         struct ath5k_hw_2w_tx_ctl *tx_ctl;
3530         unsigned int frame_len;
3531
3532         tx_ctl = &desc->ud.ds_tx5210.tx_ctl;
3533
3534         /*
3535          * Validate input
3536          * - Zero retries don't make sense.
3537          * - A zero rate will put the HW into a mode where it continously sends
3538          *   noise on the channel, so it is important to avoid this.
3539          */
3540         if (unlikely(tx_tries0 == 0)) {
3541                 ATH5K_ERR(ah->ah_sc, "zero retries\n");
3542                 WARN_ON(1);
3543                 return -EINVAL;
3544         }
3545         if (unlikely(tx_rate0 == 0)) {
3546                 ATH5K_ERR(ah->ah_sc, "zero rate\n");
3547                 WARN_ON(1);
3548                 return -EINVAL;
3549         }
3550
3551         /* Clear descriptor */
3552         memset(&desc->ud.ds_tx5210, 0, sizeof(struct ath5k_hw_5210_tx_desc));
3553
3554         /* Setup control descriptor */
3555
3556         /* Verify and set frame length */
3557
3558         /* remove padding we might have added before */
3559         frame_len = pkt_len - (hdr_len & 3) + FCS_LEN;
3560
3561         if (frame_len & ~AR5K_2W_TX_DESC_CTL0_FRAME_LEN)
3562                 return -EINVAL;
3563
3564         tx_ctl->tx_control_0 = frame_len & AR5K_2W_TX_DESC_CTL0_FRAME_LEN;
3565
3566         /* Verify and set buffer length */
3567
3568         /* NB: beacon's BufLen must be a multiple of 4 bytes */
3569         if(type == AR5K_PKT_TYPE_BEACON)
3570                 pkt_len = roundup(pkt_len, 4);
3571
3572         if (pkt_len & ~AR5K_2W_TX_DESC_CTL1_BUF_LEN)
3573                 return -EINVAL;
3574
3575         tx_ctl->tx_control_1 = pkt_len & AR5K_2W_TX_DESC_CTL1_BUF_LEN;
3576
3577         /*
3578          * Verify and set header length
3579          * XXX: I only found that on 5210 code, does it work on 5211 ?
3580          */
3581         if (ah->ah_version == AR5K_AR5210) {
3582                 if (hdr_len & ~AR5K_2W_TX_DESC_CTL0_HEADER_LEN)
3583                         return -EINVAL;
3584                 tx_ctl->tx_control_0 |=
3585                         AR5K_REG_SM(hdr_len, AR5K_2W_TX_DESC_CTL0_HEADER_LEN);
3586         }
3587
3588         /*Diferences between 5210-5211*/
3589         if (ah->ah_version == AR5K_AR5210) {
3590                 switch (type) {
3591                 case AR5K_PKT_TYPE_BEACON:
3592                 case AR5K_PKT_TYPE_PROBE_RESP:
3593                         frame_type = AR5K_AR5210_TX_DESC_FRAME_TYPE_NO_DELAY;
3594                 case AR5K_PKT_TYPE_PIFS:
3595                         frame_type = AR5K_AR5210_TX_DESC_FRAME_TYPE_PIFS;
3596                 default:
3597                         frame_type = type /*<< 2 ?*/;
3598                 }
3599
3600                 tx_ctl->tx_control_0 |=
3601                         AR5K_REG_SM(frame_type, AR5K_2W_TX_DESC_CTL0_FRAME_TYPE) |
3602                         AR5K_REG_SM(tx_rate0, AR5K_2W_TX_DESC_CTL0_XMIT_RATE);
3603         } else {
3604                 tx_ctl->tx_control_0 |=
3605                         AR5K_REG_SM(tx_rate0, AR5K_2W_TX_DESC_CTL0_XMIT_RATE) |
3606                         AR5K_REG_SM(antenna_mode, AR5K_2W_TX_DESC_CTL0_ANT_MODE_XMIT);
3607                 tx_ctl->tx_control_1 |=
3608                         AR5K_REG_SM(type, AR5K_2W_TX_DESC_CTL1_FRAME_TYPE);
3609         }
3610 #define _TX_FLAGS(_c, _flag)                                            \
3611         if (flags & AR5K_TXDESC_##_flag)                                \
3612                 tx_ctl->tx_control_##_c |=                              \
3613                         AR5K_2W_TX_DESC_CTL##_c##_##_flag
3614
3615         _TX_FLAGS(0, CLRDMASK);
3616         _TX_FLAGS(0, VEOL);
3617         _TX_FLAGS(0, INTREQ);
3618         _TX_FLAGS(0, RTSENA);
3619         _TX_FLAGS(1, NOACK);
3620
3621 #undef _TX_FLAGS
3622
3623         /*
3624          * WEP crap
3625          */
3626         if (key_index != AR5K_TXKEYIX_INVALID) {
3627                 tx_ctl->tx_control_0 |=
3628                         AR5K_2W_TX_DESC_CTL0_ENCRYPT_KEY_VALID;
3629                 tx_ctl->tx_control_1 |=
3630                         AR5K_REG_SM(key_index,
3631                         AR5K_2W_TX_DESC_CTL1_ENCRYPT_KEY_INDEX);
3632         }
3633
3634         /*
3635          * RTS/CTS Duration [5210 ?]
3636          */
3637         if ((ah->ah_version == AR5K_AR5210) &&
3638                         (flags & (AR5K_TXDESC_RTSENA | AR5K_TXDESC_CTSENA)))
3639                 tx_ctl->tx_control_1 |= rtscts_duration &
3640                                 AR5K_2W_TX_DESC_CTL1_RTS_DURATION;
3641
3642         return 0;
3643 }
3644
3645 /*
3646  * Initialize the 4-word tx descriptor on 5212
3647  */
3648 static int ath5k_hw_setup_4word_tx_desc(struct ath5k_hw *ah,
3649         struct ath5k_desc *desc, unsigned int pkt_len, unsigned int hdr_len,
3650         enum ath5k_pkt_type type, unsigned int tx_power, unsigned int tx_rate0,
3651         unsigned int tx_tries0, unsigned int key_index,
3652         unsigned int antenna_mode, unsigned int flags, unsigned int rtscts_rate,
3653         unsigned int rtscts_duration)
3654 {
3655         struct ath5k_hw_4w_tx_ctl *tx_ctl;
3656         unsigned int frame_len;
3657
3658         ATH5K_TRACE(ah->ah_sc);
3659         tx_ctl = &desc->ud.ds_tx5212.tx_ctl;
3660
3661         /*
3662          * Validate input
3663          * - Zero retries don't make sense.
3664          * - A zero rate will put the HW into a mode where it continously sends
3665          *   noise on the channel, so it is important to avoid this.
3666          */
3667         if (unlikely(tx_tries0 == 0)) {
3668                 ATH5K_ERR(ah->ah_sc, "zero retries\n");
3669                 WARN_ON(1);
3670                 return -EINVAL;
3671         }
3672         if (unlikely(tx_rate0 == 0)) {
3673                 ATH5K_ERR(ah->ah_sc, "zero rate\n");
3674                 WARN_ON(1);
3675                 return -EINVAL;
3676         }
3677
3678         /* Clear descriptor */
3679         memset(&desc->ud.ds_tx5212, 0, sizeof(struct ath5k_hw_5212_tx_desc));
3680
3681         /* Setup control descriptor */
3682
3683         /* Verify and set frame length */
3684
3685         /* remove padding we might have added before */
3686         frame_len = pkt_len - (hdr_len & 3) + FCS_LEN;
3687
3688         if (frame_len & ~AR5K_4W_TX_DESC_CTL0_FRAME_LEN)
3689                 return -EINVAL;
3690
3691         tx_ctl->tx_control_0 = frame_len & AR5K_4W_TX_DESC_CTL0_FRAME_LEN;
3692
3693         /* Verify and set buffer length */
3694
3695         /* NB: beacon's BufLen must be a multiple of 4 bytes */
3696         if(type == AR5K_PKT_TYPE_BEACON)
3697                 pkt_len = roundup(pkt_len, 4);
3698
3699         if (pkt_len & ~AR5K_4W_TX_DESC_CTL1_BUF_LEN)
3700                 return -EINVAL;
3701
3702         tx_ctl->tx_control_1 = pkt_len & AR5K_4W_TX_DESC_CTL1_BUF_LEN;
3703
3704         tx_ctl->tx_control_0 |=
3705                 AR5K_REG_SM(tx_power, AR5K_4W_TX_DESC_CTL0_XMIT_POWER) |
3706                 AR5K_REG_SM(antenna_mode, AR5K_4W_TX_DESC_CTL0_ANT_MODE_XMIT);
3707         tx_ctl->tx_control_1 |= AR5K_REG_SM(type,
3708                                         AR5K_4W_TX_DESC_CTL1_FRAME_TYPE);
3709         tx_ctl->tx_control_2 = AR5K_REG_SM(tx_tries0 + AR5K_TUNE_HWTXTRIES,
3710                                         AR5K_4W_TX_DESC_CTL2_XMIT_TRIES0);
3711         tx_ctl->tx_control_3 = tx_rate0 & AR5K_4W_TX_DESC_CTL3_XMIT_RATE0;
3712
3713 #define _TX_FLAGS(_c, _flag)                    \
3714         if (flags & AR5K_TXDESC_##_flag)        \
3715                 tx_ctl->tx_control_##_c |=      \
3716                         AR5K_4W_TX_DESC_CTL##_c##_##_flag
3717
3718         _TX_FLAGS(0, CLRDMASK);
3719         _TX_FLAGS(0, VEOL);
3720         _TX_FLAGS(0, INTREQ);
3721         _TX_FLAGS(0, RTSENA);
3722         _TX_FLAGS(0, CTSENA);
3723         _TX_FLAGS(1, NOACK);
3724
3725 #undef _TX_FLAGS
3726
3727         /*
3728          * WEP crap
3729          */
3730         if (key_index != AR5K_TXKEYIX_INVALID) {
3731                 tx_ctl->tx_control_0 |= AR5K_4W_TX_DESC_CTL0_ENCRYPT_KEY_VALID;
3732                 tx_ctl->tx_control_1 |= AR5K_REG_SM(key_index,
3733                                 AR5K_4W_TX_DESC_CTL1_ENCRYPT_KEY_INDEX);
3734         }
3735
3736         /*
3737          * RTS/CTS
3738          */
3739         if (flags & (AR5K_TXDESC_RTSENA | AR5K_TXDESC_CTSENA)) {
3740                 if ((flags & AR5K_TXDESC_RTSENA) &&
3741                                 (flags & AR5K_TXDESC_CTSENA))
3742                         return -EINVAL;
3743                 tx_ctl->tx_control_2 |= rtscts_duration &
3744                                 AR5K_4W_TX_DESC_CTL2_RTS_DURATION;
3745                 tx_ctl->tx_control_3 |= AR5K_REG_SM(rtscts_rate,
3746                                 AR5K_4W_TX_DESC_CTL3_RTS_CTS_RATE);
3747         }
3748
3749         return 0;
3750 }
3751
3752 /*
3753  * Initialize a 4-word multirate tx descriptor on 5212
3754  */
3755 static int
3756 ath5k_hw_setup_xr_tx_desc(struct ath5k_hw *ah, struct ath5k_desc *desc,
3757         unsigned int tx_rate1, u_int tx_tries1, u_int tx_rate2, u_int tx_tries2,
3758         unsigned int tx_rate3, u_int tx_tries3)
3759 {
3760         struct ath5k_hw_4w_tx_ctl *tx_ctl;
3761
3762         /*
3763          * Rates can be 0 as long as the retry count is 0 too.
3764          * A zero rate and nonzero retry count will put the HW into a mode where
3765          * it continously sends noise on the channel, so it is important to
3766          * avoid this.
3767          */
3768         if (unlikely((tx_rate1 == 0 && tx_tries1 != 0) ||
3769                      (tx_rate2 == 0 && tx_tries2 != 0) ||
3770                      (tx_rate3 == 0 && tx_tries3 != 0))) {
3771                 ATH5K_ERR(ah->ah_sc, "zero rate\n");
3772                 WARN_ON(1);
3773                 return -EINVAL;
3774         }
3775
3776         if (ah->ah_version == AR5K_AR5212) {
3777                 tx_ctl = &desc->ud.ds_tx5212.tx_ctl;
3778
3779 #define _XTX_TRIES(_n)                                                  \
3780         if (tx_tries##_n) {                                             \
3781                 tx_ctl->tx_control_2 |=                         \
3782                     AR5K_REG_SM(tx_tries##_n,                           \
3783                     AR5K_4W_TX_DESC_CTL2_XMIT_TRIES##_n);               \
3784                 tx_ctl->tx_control_3 |=                         \
3785                     AR5K_REG_SM(tx_rate##_n,                            \
3786                     AR5K_4W_TX_DESC_CTL3_XMIT_RATE##_n);                \
3787         }
3788
3789                 _XTX_TRIES(1);
3790                 _XTX_TRIES(2);
3791                 _XTX_TRIES(3);
3792
3793 #undef _XTX_TRIES
3794
3795                 return 1;
3796         }
3797
3798         return 0;
3799 }
3800
3801 /*
3802  * Proccess the tx status descriptor on 5210/5211
3803  */
3804 static int ath5k_hw_proc_2word_tx_status(struct ath5k_hw *ah,
3805                 struct ath5k_desc *desc, struct ath5k_tx_status *ts)
3806 {
3807         struct ath5k_hw_2w_tx_ctl *tx_ctl;
3808         struct ath5k_hw_tx_status *tx_status;
3809
3810         ATH5K_TRACE(ah->ah_sc);
3811
3812         tx_ctl = &desc->ud.ds_tx5210.tx_ctl;
3813         tx_status = &desc->ud.ds_tx5210.tx_stat;
3814
3815         /* No frame has been send or error */
3816         if (unlikely((tx_status->tx_status_1 & AR5K_DESC_TX_STATUS1_DONE) == 0))
3817                 return -EINPROGRESS;
3818
3819         /*
3820          * Get descriptor status
3821          */
3822         ts->ts_tstamp = AR5K_REG_MS(tx_status->tx_status_0,
3823                 AR5K_DESC_TX_STATUS0_SEND_TIMESTAMP);
3824         ts->ts_shortretry = AR5K_REG_MS(tx_status->tx_status_0,
3825                 AR5K_DESC_TX_STATUS0_SHORT_RETRY_COUNT);
3826         ts->ts_longretry = AR5K_REG_MS(tx_status->tx_status_0,
3827                 AR5K_DESC_TX_STATUS0_LONG_RETRY_COUNT);
3828         /*TODO: ts->ts_virtcol + test*/
3829         ts->ts_seqnum = AR5K_REG_MS(tx_status->tx_status_1,
3830                 AR5K_DESC_TX_STATUS1_SEQ_NUM);
3831         ts->ts_rssi = AR5K_REG_MS(tx_status->tx_status_1,
3832                 AR5K_DESC_TX_STATUS1_ACK_SIG_STRENGTH);
3833         ts->ts_antenna = 1;
3834         ts->ts_status = 0;
3835         ts->ts_rate = AR5K_REG_MS(tx_ctl->tx_control_0,
3836                 AR5K_2W_TX_DESC_CTL0_XMIT_RATE);
3837
3838         if ((tx_status->tx_status_0 & AR5K_DESC_TX_STATUS0_FRAME_XMIT_OK) == 0){
3839                 if (tx_status->tx_status_0 &
3840                                 AR5K_DESC_TX_STATUS0_EXCESSIVE_RETRIES)
3841                         ts->ts_status |= AR5K_TXERR_XRETRY;
3842
3843                 if (tx_status->tx_status_0 & AR5K_DESC_TX_STATUS0_FIFO_UNDERRUN)
3844                         ts->ts_status |= AR5K_TXERR_FIFO;
3845
3846                 if (tx_status->tx_status_0 & AR5K_DESC_TX_STATUS0_FILTERED)
3847                         ts->ts_status |= AR5K_TXERR_FILT;
3848         }
3849
3850         return 0;
3851 }
3852
3853 /*
3854  * Proccess a tx descriptor on 5212
3855  */
3856 static int ath5k_hw_proc_4word_tx_status(struct ath5k_hw *ah,
3857                 struct ath5k_desc *desc, struct ath5k_tx_status *ts)
3858 {
3859         struct ath5k_hw_4w_tx_ctl *tx_ctl;
3860         struct ath5k_hw_tx_status *tx_status;
3861
3862         ATH5K_TRACE(ah->ah_sc);
3863
3864         tx_ctl = &desc->ud.ds_tx5212.tx_ctl;
3865         tx_status = &desc->ud.ds_tx5212.tx_stat;
3866
3867         /* No frame has been send or error */
3868         if (unlikely((tx_status->tx_status_1 & AR5K_DESC_TX_STATUS1_DONE) == 0))
3869                 return -EINPROGRESS;
3870
3871         /*
3872          * Get descriptor status
3873          */
3874         ts->ts_tstamp = AR5K_REG_MS(tx_status->tx_status_0,
3875                 AR5K_DESC_TX_STATUS0_SEND_TIMESTAMP);
3876         ts->ts_shortretry = AR5K_REG_MS(tx_status->tx_status_0,
3877                 AR5K_DESC_TX_STATUS0_SHORT_RETRY_COUNT);
3878         ts->ts_longretry = AR5K_REG_MS(tx_status->tx_status_0,
3879                 AR5K_DESC_TX_STATUS0_LONG_RETRY_COUNT);
3880         ts->ts_seqnum = AR5K_REG_MS(tx_status->tx_status_1,
3881                 AR5K_DESC_TX_STATUS1_SEQ_NUM);
3882         ts->ts_rssi = AR5K_REG_MS(tx_status->tx_status_1,
3883                 AR5K_DESC_TX_STATUS1_ACK_SIG_STRENGTH);
3884         ts->ts_antenna = (tx_status->tx_status_1 &
3885                 AR5K_DESC_TX_STATUS1_XMIT_ANTENNA) ? 2 : 1;
3886         ts->ts_status = 0;
3887
3888         switch (AR5K_REG_MS(tx_status->tx_status_1,
3889                         AR5K_DESC_TX_STATUS1_FINAL_TS_INDEX)) {
3890         case 0:
3891                 ts->ts_rate = tx_ctl->tx_control_3 &
3892                         AR5K_4W_TX_DESC_CTL3_XMIT_RATE0;
3893                 break;
3894         case 1:
3895                 ts->ts_rate = AR5K_REG_MS(tx_ctl->tx_control_3,
3896                         AR5K_4W_TX_DESC_CTL3_XMIT_RATE1);
3897                 ts->ts_longretry += AR5K_REG_MS(tx_ctl->tx_control_2,
3898                         AR5K_4W_TX_DESC_CTL2_XMIT_TRIES1);
3899                 break;
3900         case 2:
3901                 ts->ts_rate = AR5K_REG_MS(tx_ctl->tx_control_3,
3902                         AR5K_4W_TX_DESC_CTL3_XMIT_RATE2);
3903                 ts->ts_longretry += AR5K_REG_MS(tx_ctl->tx_control_2,
3904                         AR5K_4W_TX_DESC_CTL2_XMIT_TRIES2);
3905                 break;
3906         case 3:
3907                 ts->ts_rate = AR5K_REG_MS(tx_ctl->tx_control_3,
3908                         AR5K_4W_TX_DESC_CTL3_XMIT_RATE3);
3909                 ts->ts_longretry += AR5K_REG_MS(tx_ctl->tx_control_2,
3910                         AR5K_4W_TX_DESC_CTL2_XMIT_TRIES3);
3911                 break;
3912         }
3913
3914         if ((tx_status->tx_status_0 & AR5K_DESC_TX_STATUS0_FRAME_XMIT_OK) == 0){
3915                 if (tx_status->tx_status_0 &
3916                                 AR5K_DESC_TX_STATUS0_EXCESSIVE_RETRIES)
3917                         ts->ts_status |= AR5K_TXERR_XRETRY;
3918
3919                 if (tx_status->tx_status_0 & AR5K_DESC_TX_STATUS0_FIFO_UNDERRUN)
3920                         ts->ts_status |= AR5K_TXERR_FIFO;
3921
3922                 if (tx_status->tx_status_0 & AR5K_DESC_TX_STATUS0_FILTERED)
3923                         ts->ts_status |= AR5K_TXERR_FILT;
3924         }
3925
3926         return 0;
3927 }
3928
3929 /*
3930  * RX Descriptor
3931  */
3932
3933 /*
3934  * Initialize an rx descriptor
3935  */
3936 int ath5k_hw_setup_rx_desc(struct ath5k_hw *ah, struct ath5k_desc *desc,
3937                         u32 size, unsigned int flags)
3938 {
3939         struct ath5k_hw_rx_ctl *rx_ctl;
3940
3941         ATH5K_TRACE(ah->ah_sc);
3942         rx_ctl = &desc->ud.ds_rx.rx_ctl;
3943
3944         /*
3945          * Clear the descriptor
3946          * If we don't clean the status descriptor,
3947          * while scanning we get too many results,
3948          * most of them virtual, after some secs
3949          * of scanning system hangs. M.F.
3950         */
3951         memset(&desc->ud.ds_rx, 0, sizeof(struct ath5k_hw_all_rx_desc));
3952
3953         /* Setup descriptor */
3954         rx_ctl->rx_control_1 = size & AR5K_DESC_RX_CTL1_BUF_LEN;
3955         if (unlikely(rx_ctl->rx_control_1 != size))
3956                 return -EINVAL;
3957
3958         if (flags & AR5K_RXDESC_INTREQ)
3959                 rx_ctl->rx_control_1 |= AR5K_DESC_RX_CTL1_INTREQ;
3960
3961         return 0;
3962 }
3963
3964 /*
3965  * Proccess the rx status descriptor on 5210/5211
3966  */
3967 static int ath5k_hw_proc_5210_rx_status(struct ath5k_hw *ah,
3968                 struct ath5k_desc *desc, struct ath5k_rx_status *rs)
3969 {
3970         struct ath5k_hw_rx_status *rx_status;
3971
3972         rx_status = &desc->ud.ds_rx.u.rx_stat;
3973
3974         /* No frame received / not ready */
3975         if (unlikely((rx_status->rx_status_1 & AR5K_5210_RX_DESC_STATUS1_DONE)
3976                                 == 0))
3977                 return -EINPROGRESS;
3978
3979         /*
3980          * Frame receive status
3981          */
3982         rs->rs_datalen = rx_status->rx_status_0 &
3983                 AR5K_5210_RX_DESC_STATUS0_DATA_LEN;
3984         rs->rs_rssi = AR5K_REG_MS(rx_status->rx_status_0,
3985                 AR5K_5210_RX_DESC_STATUS0_RECEIVE_SIGNAL);
3986         rs->rs_rate = AR5K_REG_MS(rx_status->rx_status_0,
3987                 AR5K_5210_RX_DESC_STATUS0_RECEIVE_RATE);
3988         rs->rs_antenna = rx_status->rx_status_0 &
3989                 AR5K_5210_RX_DESC_STATUS0_RECEIVE_ANTENNA;
3990         rs->rs_more = rx_status->rx_status_0 &
3991                 AR5K_5210_RX_DESC_STATUS0_MORE;
3992         /* TODO: this timestamp is 13 bit, later on we assume 15 bit */
3993         rs->rs_tstamp = AR5K_REG_MS(rx_status->rx_status_1,
3994                 AR5K_5210_RX_DESC_STATUS1_RECEIVE_TIMESTAMP);
3995         rs->rs_status = 0;
3996
3997         /*
3998          * Key table status
3999          */
4000         if (rx_status->rx_status_1 & AR5K_5210_RX_DESC_STATUS1_KEY_INDEX_VALID)
4001                 rs->rs_keyix = AR5K_REG_MS(rx_status->rx_status_1,
4002                         AR5K_5210_RX_DESC_STATUS1_KEY_INDEX);
4003         else
4004                 rs->rs_keyix = AR5K_RXKEYIX_INVALID;
4005
4006         /*
4007          * Receive/descriptor errors
4008          */
4009         if ((rx_status->rx_status_1 &
4010                         AR5K_5210_RX_DESC_STATUS1_FRAME_RECEIVE_OK) == 0) {
4011                 if (rx_status->rx_status_1 &
4012                                 AR5K_5210_RX_DESC_STATUS1_CRC_ERROR)
4013                         rs->rs_status |= AR5K_RXERR_CRC;
4014
4015                 if (rx_status->rx_status_1 &
4016                                 AR5K_5210_RX_DESC_STATUS1_FIFO_OVERRUN)
4017                         rs->rs_status |= AR5K_RXERR_FIFO;
4018
4019                 if (rx_status->rx_status_1 &
4020                                 AR5K_5210_RX_DESC_STATUS1_PHY_ERROR) {
4021                         rs->rs_status |= AR5K_RXERR_PHY;
4022                         rs->rs_phyerr = AR5K_REG_MS(rx_status->rx_status_1,
4023                                            AR5K_5210_RX_DESC_STATUS1_PHY_ERROR);
4024                 }
4025
4026                 if (rx_status->rx_status_1 &
4027                                 AR5K_5210_RX_DESC_STATUS1_DECRYPT_CRC_ERROR)
4028                         rs->rs_status |= AR5K_RXERR_DECRYPT;
4029         }
4030
4031         return 0;
4032 }
4033
4034 /*
4035  * Proccess the rx status descriptor on 5212
4036  */
4037 static int ath5k_hw_proc_5212_rx_status(struct ath5k_hw *ah,
4038                 struct ath5k_desc *desc, struct ath5k_rx_status *rs)
4039 {
4040         struct ath5k_hw_rx_status *rx_status;
4041         struct ath5k_hw_rx_error *rx_err;
4042
4043         ATH5K_TRACE(ah->ah_sc);
4044         rx_status = &desc->ud.ds_rx.u.rx_stat;
4045
4046         /* Overlay on error */
4047         rx_err = &desc->ud.ds_rx.u.rx_err;
4048
4049         /* No frame received / not ready */
4050         if (unlikely((rx_status->rx_status_1 & AR5K_5212_RX_DESC_STATUS1_DONE)
4051                                 == 0))
4052                 return -EINPROGRESS;
4053
4054         /*
4055          * Frame receive status
4056          */
4057         rs->rs_datalen = rx_status->rx_status_0 &
4058                 AR5K_5212_RX_DESC_STATUS0_DATA_LEN;
4059         rs->rs_rssi = AR5K_REG_MS(rx_status->rx_status_0,
4060                 AR5K_5212_RX_DESC_STATUS0_RECEIVE_SIGNAL);
4061         rs->rs_rate = AR5K_REG_MS(rx_status->rx_status_0,
4062                 AR5K_5212_RX_DESC_STATUS0_RECEIVE_RATE);
4063         rs->rs_antenna = rx_status->rx_status_0 &
4064                 AR5K_5212_RX_DESC_STATUS0_RECEIVE_ANTENNA;
4065         rs->rs_more = rx_status->rx_status_0 &
4066                 AR5K_5212_RX_DESC_STATUS0_MORE;
4067         rs->rs_tstamp = AR5K_REG_MS(rx_status->rx_status_1,
4068                 AR5K_5212_RX_DESC_STATUS1_RECEIVE_TIMESTAMP);
4069         rs->rs_status = 0;
4070
4071         /*
4072          * Key table status
4073          */
4074         if (rx_status->rx_status_1 & AR5K_5212_RX_DESC_STATUS1_KEY_INDEX_VALID)
4075                 rs->rs_keyix = AR5K_REG_MS(rx_status->rx_status_1,
4076                                 AR5K_5212_RX_DESC_STATUS1_KEY_INDEX);
4077         else
4078                 rs->rs_keyix = AR5K_RXKEYIX_INVALID;
4079
4080         /*
4081          * Receive/descriptor errors
4082          */
4083         if ((rx_status->rx_status_1 &
4084                         AR5K_5212_RX_DESC_STATUS1_FRAME_RECEIVE_OK) == 0) {
4085                 if (rx_status->rx_status_1 &
4086                                 AR5K_5212_RX_DESC_STATUS1_CRC_ERROR)
4087                         rs->rs_status |= AR5K_RXERR_CRC;
4088
4089                 if (rx_status->rx_status_1 &
4090                                 AR5K_5212_RX_DESC_STATUS1_PHY_ERROR) {
4091                         rs->rs_status |= AR5K_RXERR_PHY;
4092                         rs->rs_phyerr = AR5K_REG_MS(rx_err->rx_error_1,
4093                                            AR5K_RX_DESC_ERROR1_PHY_ERROR_CODE);
4094                 }
4095
4096                 if (rx_status->rx_status_1 &
4097                                 AR5K_5212_RX_DESC_STATUS1_DECRYPT_CRC_ERROR)
4098                         rs->rs_status |= AR5K_RXERR_DECRYPT;
4099
4100                 if (rx_status->rx_status_1 &
4101                                 AR5K_5212_RX_DESC_STATUS1_MIC_ERROR)
4102                         rs->rs_status |= AR5K_RXERR_MIC;
4103         }
4104
4105         return 0;
4106 }
4107
4108
4109 /****************\
4110   GPIO Functions
4111 \****************/
4112
4113 /*
4114  * Set led state
4115  */
4116 void ath5k_hw_set_ledstate(struct ath5k_hw *ah, unsigned int state)
4117 {
4118         u32 led;
4119         /*5210 has different led mode handling*/
4120         u32 led_5210;
4121
4122         ATH5K_TRACE(ah->ah_sc);
4123
4124         /*Reset led status*/
4125         if (ah->ah_version != AR5K_AR5210)
4126                 AR5K_REG_DISABLE_BITS(ah, AR5K_PCICFG,
4127                         AR5K_PCICFG_LEDMODE |  AR5K_PCICFG_LED);
4128         else
4129                 AR5K_REG_DISABLE_BITS(ah, AR5K_PCICFG, AR5K_PCICFG_LED);
4130
4131         /*
4132          * Some blinking values, define at your wish
4133          */
4134         switch (state) {
4135         case AR5K_LED_SCAN:
4136         case AR5K_LED_AUTH:
4137                 led = AR5K_PCICFG_LEDMODE_PROP | AR5K_PCICFG_LED_PEND;
4138                 led_5210 = AR5K_PCICFG_LED_PEND | AR5K_PCICFG_LED_BCTL;
4139                 break;
4140
4141         case AR5K_LED_INIT:
4142                 led = AR5K_PCICFG_LEDMODE_PROP | AR5K_PCICFG_LED_NONE;
4143                 led_5210 = AR5K_PCICFG_LED_PEND;
4144                 break;
4145
4146         case AR5K_LED_ASSOC:
4147         case AR5K_LED_RUN:
4148                 led = AR5K_PCICFG_LEDMODE_PROP | AR5K_PCICFG_LED_ASSOC;
4149                 led_5210 = AR5K_PCICFG_LED_ASSOC;
4150                 break;
4151
4152         default:
4153                 led = AR5K_PCICFG_LEDMODE_PROM | AR5K_PCICFG_LED_NONE;
4154                 led_5210 = AR5K_PCICFG_LED_PEND;
4155                 break;
4156         }
4157
4158         /*Write new status to the register*/
4159         if (ah->ah_version != AR5K_AR5210)
4160                 AR5K_REG_ENABLE_BITS(ah, AR5K_PCICFG, led);
4161         else
4162                 AR5K_REG_ENABLE_BITS(ah, AR5K_PCICFG, led_5210);
4163 }
4164
4165 /*
4166  * Set GPIO outputs
4167  */
4168 int ath5k_hw_set_gpio_output(struct ath5k_hw *ah, u32 gpio)
4169 {
4170         ATH5K_TRACE(ah->ah_sc);
4171         if (gpio > AR5K_NUM_GPIO)
4172                 return -EINVAL;
4173
4174         ath5k_hw_reg_write(ah, (ath5k_hw_reg_read(ah, AR5K_GPIOCR) &~
4175                 AR5K_GPIOCR_OUT(gpio)) | AR5K_GPIOCR_OUT(gpio), AR5K_GPIOCR);
4176
4177         return 0;
4178 }
4179
4180 /*
4181  * Set GPIO inputs
4182  */
4183 int ath5k_hw_set_gpio_input(struct ath5k_hw *ah, u32 gpio)
4184 {
4185         ATH5K_TRACE(ah->ah_sc);
4186         if (gpio > AR5K_NUM_GPIO)
4187                 return -EINVAL;
4188
4189         ath5k_hw_reg_write(ah, (ath5k_hw_reg_read(ah, AR5K_GPIOCR) &~
4190                 AR5K_GPIOCR_OUT(gpio)) | AR5K_GPIOCR_IN(gpio), AR5K_GPIOCR);
4191
4192         return 0;
4193 }
4194
4195 /*
4196  * Get GPIO state
4197  */
4198 u32 ath5k_hw_get_gpio(struct ath5k_hw *ah, u32 gpio)
4199 {
4200         ATH5K_TRACE(ah->ah_sc);
4201         if (gpio > AR5K_NUM_GPIO)
4202                 return 0xffffffff;
4203
4204         /* GPIO input magic */
4205         return ((ath5k_hw_reg_read(ah, AR5K_GPIODI) & AR5K_GPIODI_M) >> gpio) &
4206                 0x1;
4207 }
4208
4209 /*
4210  * Set GPIO state
4211  */
4212 int ath5k_hw_set_gpio(struct ath5k_hw *ah, u32 gpio, u32 val)
4213 {
4214         u32 data;
4215         ATH5K_TRACE(ah->ah_sc);
4216
4217         if (gpio > AR5K_NUM_GPIO)
4218                 return -EINVAL;
4219
4220         /* GPIO output magic */
4221         data = ath5k_hw_reg_read(ah, AR5K_GPIODO);
4222
4223         data &= ~(1 << gpio);
4224         data |= (val & 1) << gpio;
4225
4226         ath5k_hw_reg_write(ah, data, AR5K_GPIODO);
4227
4228         return 0;
4229 }
4230
4231 /*
4232  * Initialize the GPIO interrupt (RFKill switch)
4233  */
4234 void ath5k_hw_set_gpio_intr(struct ath5k_hw *ah, unsigned int gpio,
4235                 u32 interrupt_level)
4236 {
4237         u32 data;
4238
4239         ATH5K_TRACE(ah->ah_sc);
4240         if (gpio > AR5K_NUM_GPIO)
4241                 return;
4242
4243         /*
4244          * Set the GPIO interrupt
4245          */
4246         data = (ath5k_hw_reg_read(ah, AR5K_GPIOCR) &
4247                 ~(AR5K_GPIOCR_INT_SEL(gpio) | AR5K_GPIOCR_INT_SELH |
4248                 AR5K_GPIOCR_INT_ENA | AR5K_GPIOCR_OUT(gpio))) |
4249                 (AR5K_GPIOCR_INT_SEL(gpio) | AR5K_GPIOCR_INT_ENA);
4250
4251         ath5k_hw_reg_write(ah, interrupt_level ? data :
4252                 (data | AR5K_GPIOCR_INT_SELH), AR5K_GPIOCR);
4253
4254         ah->ah_imr |= AR5K_IMR_GPIO;
4255
4256         /* Enable GPIO interrupts */
4257         AR5K_REG_ENABLE_BITS(ah, AR5K_PIMR, AR5K_IMR_GPIO);
4258 }
4259
4260
4261
4262
4263 /****************\
4264   Misc functions
4265 \****************/
4266
4267 int ath5k_hw_get_capability(struct ath5k_hw *ah,
4268                 enum ath5k_capability_type cap_type,
4269                 u32 capability, u32 *result)
4270 {
4271         ATH5K_TRACE(ah->ah_sc);
4272
4273         switch (cap_type) {
4274         case AR5K_CAP_NUM_TXQUEUES:
4275                 if (result) {
4276                         if (ah->ah_version == AR5K_AR5210)
4277                                 *result = AR5K_NUM_TX_QUEUES_NOQCU;
4278                         else
4279                                 *result = AR5K_NUM_TX_QUEUES;
4280                         goto yes;
4281                 }
4282         case AR5K_CAP_VEOL:
4283                 goto yes;
4284         case AR5K_CAP_COMPRESSION:
4285                 if (ah->ah_version == AR5K_AR5212)
4286                         goto yes;
4287                 else
4288                         goto no;
4289         case AR5K_CAP_BURST:
4290                 goto yes;
4291         case AR5K_CAP_TPC:
4292                 goto yes;
4293         case AR5K_CAP_BSSIDMASK:
4294                 if (ah->ah_version == AR5K_AR5212)
4295                         goto yes;
4296                 else
4297                         goto no;
4298         case AR5K_CAP_XR:
4299                 if (ah->ah_version == AR5K_AR5212)
4300                         goto yes;
4301                 else
4302                         goto no;
4303         default:
4304                 goto no;
4305         }
4306
4307 no:
4308         return -EINVAL;
4309 yes:
4310         return 0;
4311 }
4312
4313 static int ath5k_hw_enable_pspoll(struct ath5k_hw *ah, u8 *bssid,
4314                 u16 assoc_id)
4315 {
4316         ATH5K_TRACE(ah->ah_sc);
4317
4318         if (ah->ah_version == AR5K_AR5210) {
4319                 AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1,
4320                         AR5K_STA_ID1_NO_PSPOLL | AR5K_STA_ID1_DEFAULT_ANTENNA);
4321                 return 0;
4322         }
4323
4324         return -EIO;
4325 }
4326
4327 static int ath5k_hw_disable_pspoll(struct ath5k_hw *ah)
4328 {
4329         ATH5K_TRACE(ah->ah_sc);
4330
4331         if (ah->ah_version == AR5K_AR5210) {
4332                 AR5K_REG_ENABLE_BITS(ah, AR5K_STA_ID1,
4333                         AR5K_STA_ID1_NO_PSPOLL | AR5K_STA_ID1_DEFAULT_ANTENNA);
4334                 return 0;
4335         }
4336
4337         return -EIO;
4338 }