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