sync ath9k with latest git code
[openwrt.git] / package / ath9k / src / drivers / net / wireless / ath9k / main.c
1 /*
2  * Copyright (c) 2008 Atheros Communications Inc.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 /* mac80211 and PCI callbacks */
18
19 #include <linux/nl80211.h>
20 #include "core.h"
21
22 #define ATH_PCI_VERSION "0.1"
23
24 #define IEEE80211_HTCAP_MAXRXAMPDU_FACTOR       13
25 #define IEEE80211_ACTION_CAT_HT                 7
26 #define IEEE80211_ACTION_HT_TXCHWIDTH           0
27
28 static char *dev_info = "ath9k";
29
30 MODULE_AUTHOR("Atheros Communications");
31 MODULE_DESCRIPTION("Support for Atheros 802.11n wireless LAN cards.");
32 MODULE_SUPPORTED_DEVICE("Atheros 802.11n WLAN cards");
33 MODULE_LICENSE("Dual BSD/GPL");
34
35 static struct pci_device_id ath_pci_id_table[] __devinitdata = {
36         { PCI_VDEVICE(ATHEROS, 0x0023) }, /* PCI   */
37         { PCI_VDEVICE(ATHEROS, 0x0024) }, /* PCI-E */
38         { PCI_VDEVICE(ATHEROS, 0x0027) }, /* PCI   */
39         { PCI_VDEVICE(ATHEROS, 0x0029) }, /* PCI   */
40         { PCI_VDEVICE(ATHEROS, 0x002A) }, /* PCI-E */
41         { 0 }
42 };
43
44 static int test_update_chan(enum ieee80211_band band,
45                             const struct hal_channel *chan,
46                             struct ath_softc *sc)
47 {
48         int i;
49
50         for (i = 0; i < sc->sbands[band].n_channels; i++) {
51                 if (sc->channels[band][i].center_freq == chan->channel)
52                         return 1;
53         }
54
55         return 0;
56 }
57
58 static int ath_check_chanflags(struct ieee80211_channel *chan,
59                                u_int32_t mode,
60                                struct ath_softc *sc)
61 {
62         struct ieee80211_hw *hw = sc->hw;
63         struct ieee80211_supported_band *band;
64         struct ieee80211_channel *band_channel;
65         int i;
66
67         band = hw->wiphy->bands[chan->band];
68
69         for (i = 0; i < band->n_channels; i++) {
70                 band_channel = &band->channels[i];
71
72                 if ((band_channel->center_freq == chan->center_freq) &&
73                     ((band_channel->hw_value & mode) == mode))
74                         return 1;
75         }
76         return 0;
77 }
78
79 static int ath_setkey_tkip(struct ath_softc *sc,
80                            struct ieee80211_key_conf *key,
81                            struct hal_keyval *hk,
82                            const u8 *addr)
83 {
84         u8 *key_rxmic = NULL;
85         u8 *key_txmic = NULL;
86
87         key_txmic = key->key + NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY;
88         key_rxmic = key->key + NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY;
89
90         if (addr == NULL) {
91                 /* Group key installation */
92                 memcpy(hk->kv_mic,  key_rxmic, sizeof(hk->kv_mic));
93                 return ath_keyset(sc, key->keyidx, hk, addr);
94         }
95         if (!sc->sc_splitmic) {
96                 /*
97                  * data key goes at first index,
98                  * the hal handles the MIC keys at index+64.
99                  */
100                 memcpy(hk->kv_mic, key_rxmic, sizeof(hk->kv_mic));
101                 memcpy(hk->kv_txmic, key_txmic, sizeof(hk->kv_txmic));
102                 return ath_keyset(sc, key->keyidx, hk, addr);
103         }
104         /*
105          * TX key goes at first index, RX key at +32.
106          * The hal handles the MIC keys at index+64.
107          */
108         memcpy(hk->kv_mic, key_txmic, sizeof(hk->kv_mic));
109         if (!ath_keyset(sc, key->keyidx, hk, NULL)) {
110                 /* Txmic entry failed. No need to proceed further */
111                 DPRINTF(sc, ATH_DBG_KEYCACHE,
112                         "%s Setting TX MIC Key Failed\n", __func__);
113                 return 0;
114         }
115
116         memcpy(hk->kv_mic, key_rxmic, sizeof(hk->kv_mic));
117         /* XXX delete tx key on failure? */
118         return ath_keyset(sc, key->keyidx+32, hk, addr);
119 }
120
121 static int ath_key_config(struct ath_softc *sc,
122                           const u8 *addr,
123                           struct ieee80211_key_conf *key)
124 {
125         struct ieee80211_vif *vif;
126         struct hal_keyval hk;
127         const u8 *mac = NULL;
128         int ret = 0;
129         enum ieee80211_if_types opmode;
130
131         memset(&hk, 0, sizeof(hk));
132
133         switch (key->alg) {
134         case ALG_WEP:
135                 hk.kv_type = HAL_CIPHER_WEP;
136                 break;
137         case ALG_TKIP:
138                 hk.kv_type = HAL_CIPHER_TKIP;
139                 break;
140         case ALG_CCMP:
141                 hk.kv_type = HAL_CIPHER_AES_CCM;
142                 break;
143         default:
144                 return -EINVAL;
145         }
146
147         hk.kv_len  = key->keylen;
148         memcpy(hk.kv_val, key->key, key->keylen);
149
150         if (!sc->sc_vaps[0])
151                 return -EIO;
152
153         vif = sc->sc_vaps[0]->av_if_data;
154         opmode = vif->type;
155
156         /*
157          *  Strategy:
158          *   For _M_STA mc tx, we will not setup a key at all since we never
159          *   tx mc.
160          *   _M_STA mc rx, we will use the keyID.
161          *   for _M_IBSS mc tx, we will use the keyID, and no macaddr.
162          *   for _M_IBSS mc rx, we will alloc a slot and plumb the mac of the
163          *   peer node. BUT we will plumb a cleartext key so that we can do
164          *   perSta default key table lookup in software.
165          */
166         if (is_broadcast_ether_addr(addr)) {
167                 switch (opmode) {
168                 case IEEE80211_IF_TYPE_STA:
169                         /* default key:  could be group WPA key
170                          * or could be static WEP key */
171                         mac = NULL;
172                         break;
173                 case IEEE80211_IF_TYPE_IBSS:
174                         break;
175                 case IEEE80211_IF_TYPE_AP:
176                         break;
177                 default:
178                         ASSERT(0);
179                         break;
180                 }
181         } else {
182                 mac = addr;
183         }
184
185         if (key->alg == ALG_TKIP)
186                 ret = ath_setkey_tkip(sc, key, &hk, mac);
187         else
188                 ret = ath_keyset(sc, key->keyidx, &hk, mac);
189
190         if (!ret)
191                 return -EIO;
192
193         sc->sc_keytype = hk.kv_type;
194         return 0;
195 }
196
197 static void ath_key_delete(struct ath_softc *sc, struct ieee80211_key_conf *key)
198 {
199 #define ATH_MAX_NUM_KEYS 4
200         int freeslot;
201
202         freeslot = (key->keyidx >= ATH_MAX_NUM_KEYS) ? 1 : 0;
203         ath_key_reset(sc, key->keyidx, freeslot);
204 #undef ATH_MAX_NUM_KEYS
205 }
206
207 static void setup_ht_cap(struct ieee80211_ht_info *ht_info)
208 {
209 /* Until mac80211 includes these fields */
210
211 #define IEEE80211_HT_CAP_DSSSCCK40 0x1000
212 #define IEEE80211_HT_CAP_MAXRXAMPDU_65536 0x3   /* 2 ^ 16 */
213 #define IEEE80211_HT_CAP_MPDUDENSITY_8 0x6      /* 8 usec */
214
215         ht_info->ht_supported = 1;
216         ht_info->cap = (u16)IEEE80211_HT_CAP_SUP_WIDTH
217                         |(u16)IEEE80211_HT_CAP_MIMO_PS
218                         |(u16)IEEE80211_HT_CAP_SGI_40
219                         |(u16)IEEE80211_HT_CAP_DSSSCCK40;
220
221         ht_info->ampdu_factor = IEEE80211_HT_CAP_MAXRXAMPDU_65536;
222         ht_info->ampdu_density = IEEE80211_HT_CAP_MPDUDENSITY_8;
223         /* setup supported mcs set */
224         memset(ht_info->supp_mcs_set, 0, 16);
225         ht_info->supp_mcs_set[0] = 0xff;
226         ht_info->supp_mcs_set[1] = 0xff;
227         ht_info->supp_mcs_set[12] = IEEE80211_HT_CAP_MCS_TX_DEFINED;
228 }
229
230 static int ath_rate2idx(struct ath_softc *sc, int rate)
231 {
232         int i = 0, cur_band, n_rates;
233         struct ieee80211_hw *hw = sc->hw;
234
235         cur_band = hw->conf.channel->band;
236         n_rates = sc->sbands[cur_band].n_bitrates;
237
238         for (i = 0; i < n_rates; i++) {
239                 if (sc->sbands[cur_band].bitrates[i].bitrate == rate)
240                         break;
241         }
242
243         /*
244          * NB:mac80211 validates rx rate index against the supported legacy rate
245          * index only (should be done against ht rates also), return the highest
246          * legacy rate index for rx rate which does not match any one of the
247          * supported basic and extended rates to make mac80211 happy.
248          * The following hack will be cleaned up once the issue with
249          * the rx rate index validation in mac80211 is fixed.
250          */
251         if (i == n_rates)
252                 return n_rates - 1;
253         return i;
254 }
255
256 static void ath9k_rx_prepare(struct ath_softc *sc,
257                              struct sk_buff *skb,
258                              struct ath_recv_status *status,
259                              struct ieee80211_rx_status *rx_status)
260 {
261         struct ieee80211_hw *hw = sc->hw;
262         struct ieee80211_channel *curchan = hw->conf.channel;
263
264         memset(rx_status, 0, sizeof(struct ieee80211_rx_status));
265
266         rx_status->mactime = status->tsf;
267         rx_status->band = curchan->band;
268         rx_status->freq =  curchan->center_freq;
269         rx_status->signal = (status->rssi * 64) / 100;
270         rx_status->noise = ATH_DEFAULT_NOISE_FLOOR;
271         rx_status->rate_idx = ath_rate2idx(sc, (status->rateKbps / 100));
272         rx_status->antenna = status->antenna;
273
274         if (status->flags & ATH_RX_MIC_ERROR)
275                 rx_status->flag |= RX_FLAG_MMIC_ERROR;
276         if (status->flags & ATH_RX_FCS_ERROR)
277                 rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
278
279         rx_status->flag |= RX_FLAG_TSFT;
280 }
281
282 static u_int8_t parse_mpdudensity(u_int8_t mpdudensity)
283 {
284         /*
285          * 802.11n D2.0 defined values for "Minimum MPDU Start Spacing":
286          *   0 for no restriction
287          *   1 for 1/4 us
288          *   2 for 1/2 us
289          *   3 for 1 us
290          *   4 for 2 us
291          *   5 for 4 us
292          *   6 for 8 us
293          *   7 for 16 us
294          */
295         switch (mpdudensity) {
296         case 0:
297                 return 0;
298         case 1:
299         case 2:
300         case 3:
301                 /* Our lower layer calculations limit our precision to
302                    1 microsecond */
303                 return 1;
304         case 4:
305                 return 2;
306         case 5:
307                 return 4;
308         case 6:
309                 return 8;
310         case 7:
311                 return 16;
312         default:
313                 return 0;
314         }
315 }
316
317 static int ath9k_start(struct ieee80211_hw *hw)
318 {
319         struct ath_softc *sc = hw->priv;
320         struct ieee80211_channel *curchan = hw->conf.channel;
321         struct hal_channel hchan;
322         int error = 0;
323
324         DPRINTF(sc, ATH_DBG_CONFIG, "%s: Starting driver with "
325                 "initial channel: %d MHz\n", __func__, curchan->center_freq);
326
327         /* setup initial channel */
328
329         hchan.channel = curchan->center_freq;
330         hchan.channelFlags = ath_chan2flags(curchan, sc);
331
332         /* open ath_dev */
333         error = ath_open(sc, &hchan);
334         if (error) {
335                 DPRINTF(sc, ATH_DBG_FATAL,
336                         "%s: Unable to complete ath_open\n", __func__);
337                 return error;
338         }
339
340         ieee80211_wake_queues(hw);
341         return 0;
342 }
343
344 static int ath9k_tx(struct ieee80211_hw *hw,
345                     struct sk_buff *skb)
346 {
347         struct ath_softc *sc = hw->priv;
348         int hdrlen, padsize;
349
350         /* Add the padding after the header if this is not already done */
351         hdrlen = ieee80211_get_hdrlen_from_skb(skb);
352         if (hdrlen & 3) {
353                 padsize = hdrlen % 4;
354                 if (skb_headroom(skb) < padsize)
355                         return -1;
356                 skb_push(skb, padsize);
357                 memmove(skb->data, skb->data + padsize, hdrlen);
358         }
359
360         DPRINTF(sc, ATH_DBG_XMIT, "%s: transmitting packet, skb: %p\n",
361                 __func__,
362                 skb);
363
364         if (ath_tx_start(sc, skb) != 0) {
365                 DPRINTF(sc, ATH_DBG_XMIT, "%s: TX failed\n", __func__);
366                 dev_kfree_skb_any(skb);
367                 /* FIXME: Check for proper return value from ATH_DEV */
368                 return 0;
369         }
370
371         return 0;
372 }
373
374 static void ath9k_stop(struct ieee80211_hw *hw)
375 {
376         struct ath_softc *sc = hw->priv;
377         int error;
378
379         DPRINTF(sc, ATH_DBG_CONFIG, "%s: Driver halt\n", __func__);
380
381         error = ath_suspend(sc);
382         if (error)
383                 DPRINTF(sc, ATH_DBG_CONFIG,
384                         "%s: Device is no longer present\n", __func__);
385
386         ieee80211_stop_queues(hw);
387 }
388
389 static int ath9k_add_interface(struct ieee80211_hw *hw,
390                                struct ieee80211_if_init_conf *conf)
391 {
392         struct ath_softc *sc = hw->priv;
393         int error, ic_opmode = 0;
394
395         /* Support only vap for now */
396
397         if (sc->sc_nvaps)
398                 return -ENOBUFS;
399
400         switch (conf->type) {
401         case IEEE80211_IF_TYPE_STA:
402                 ic_opmode = HAL_M_STA;
403                 break;
404         case IEEE80211_IF_TYPE_IBSS:
405                 ic_opmode = HAL_M_IBSS;
406                 break;
407         default:
408                 DPRINTF(sc, ATH_DBG_FATAL,
409                         "%s: Only STA and IBSS are supported currently\n",
410                         __func__);
411                 return -EOPNOTSUPP;
412         }
413
414         DPRINTF(sc, ATH_DBG_CONFIG, "%s: Attach a VAP of type: %d\n",
415                 __func__,
416                 ic_opmode);
417
418         error = ath_vap_attach(sc, 0, conf->vif, ic_opmode);
419         if (error) {
420                 DPRINTF(sc, ATH_DBG_FATAL,
421                         "%s: Unable to attach vap, error: %d\n",
422                         __func__, error);
423                 return error;
424         }
425
426         return 0;
427 }
428
429 static void ath9k_remove_interface(struct ieee80211_hw *hw,
430                                    struct ieee80211_if_init_conf *conf)
431 {
432         struct ath_softc *sc = hw->priv;
433         struct ath_vap *avp;
434         int error;
435
436         DPRINTF(sc, ATH_DBG_CONFIG, "%s: Detach VAP\n", __func__);
437
438         avp = sc->sc_vaps[0];
439         if (avp == NULL) {
440                 DPRINTF(sc, ATH_DBG_FATAL, "%s: Invalid interface\n",
441                         __func__);
442                 return;
443         }
444
445 #ifdef CONFIG_SLOW_ANT_DIV
446         ath_slow_ant_div_stop(&sc->sc_antdiv);
447 #endif
448
449         /* Update ratectrl */
450         ath_rate_newstate(sc, avp, 0);
451
452         /* Reclaim beacon resources */
453         if (sc->sc_opmode == HAL_M_HOSTAP || sc->sc_opmode == HAL_M_IBSS) {
454                 ath9k_hw_stoptxdma(sc->sc_ah, sc->sc_bhalq);
455                 ath_beacon_return(sc, avp);
456         }
457
458         /* Set interrupt mask */
459         sc->sc_imask &= ~(HAL_INT_SWBA | HAL_INT_BMISS);
460         ath9k_hw_set_interrupts(sc->sc_ah, sc->sc_imask & ~HAL_INT_GLOBAL);
461         sc->sc_beacons = 0;
462
463         error = ath_vap_detach(sc, 0);
464         if (error)
465                 DPRINTF(sc, ATH_DBG_FATAL,
466                         "%s: Unable to detach vap, error: %d\n",
467                         __func__, error);
468 }
469
470 static int ath9k_config(struct ieee80211_hw *hw,
471                         struct ieee80211_conf *conf)
472 {
473         struct ath_softc *sc = hw->priv;
474         struct ieee80211_channel *curchan = hw->conf.channel;
475         struct hal_channel hchan;
476
477         DPRINTF(sc, ATH_DBG_CONFIG, "%s: Set channel: %d MHz\n",
478                 __func__,
479                 curchan->center_freq);
480
481         hchan.channel = curchan->center_freq;
482         hchan.channelFlags = ath_chan2flags(curchan, sc);
483         sc->sc_config.txpowlimit = 2 * conf->power_level;
484
485         /* set h/w channel */
486         if (ath_set_channel(sc, &hchan) < 0)
487                 DPRINTF(sc, ATH_DBG_FATAL, "%s: Unable to set channel\n",
488                         __func__);
489
490         return 0;
491 }
492
493 static int ath9k_config_interface(struct ieee80211_hw *hw,
494                                   struct ieee80211_vif *vif,
495                                   struct ieee80211_if_conf *conf)
496 {
497         struct ath_softc *sc = hw->priv;
498         struct ath_vap *avp;
499         u_int32_t rfilt = 0;
500         int error, i;
501         DECLARE_MAC_BUF(mac);
502
503         avp = sc->sc_vaps[0];
504         if (avp == NULL) {
505                 DPRINTF(sc, ATH_DBG_FATAL, "%s: Invalid interface\n",
506                         __func__);
507                 return -EINVAL;
508         }
509
510         if ((conf->changed & IEEE80211_IFCC_BSSID) &&
511             !is_zero_ether_addr(conf->bssid)) {
512                 switch (vif->type) {
513                 case IEEE80211_IF_TYPE_STA:
514                 case IEEE80211_IF_TYPE_IBSS:
515                         /* Update ratectrl about the new state */
516                         ath_rate_newstate(sc, avp, 0);
517
518                         /* Set rx filter */
519                         rfilt = ath_calcrxfilter(sc);
520                         ath9k_hw_setrxfilter(sc->sc_ah, rfilt);
521
522                         /* Set BSSID */
523                         memcpy(sc->sc_curbssid, conf->bssid, ETH_ALEN);
524                         sc->sc_curaid = 0;
525                         ath9k_hw_write_associd(sc->sc_ah, sc->sc_curbssid,
526                                                sc->sc_curaid);
527
528                         /* Set aggregation protection mode parameters */
529                         sc->sc_config.ath_aggr_prot = 0;
530
531                         /*
532                          * Reset our TSF so that its value is lower than the
533                          * beacon that we are trying to catch.
534                          * Only then hw will update its TSF register with the
535                          * new beacon. Reset the TSF before setting the BSSID
536                          * to avoid allowing in any frames that would update
537                          * our TSF only to have us clear it
538                          * immediately thereafter.
539                          */
540                         ath9k_hw_reset_tsf(sc->sc_ah);
541
542                         /* Disable BMISS interrupt when we're not associated */
543                         ath9k_hw_set_interrupts(sc->sc_ah,
544                                                 sc->sc_imask &
545                                                 ~(HAL_INT_SWBA | HAL_INT_BMISS));
546                         sc->sc_imask &= ~(HAL_INT_SWBA | HAL_INT_BMISS);
547
548                         DPRINTF(sc, ATH_DBG_CONFIG,
549                                 "%s: RX filter 0x%x bssid %s aid 0x%x\n",
550                                 __func__, rfilt,
551                                 print_mac(mac, sc->sc_curbssid), sc->sc_curaid);
552
553                         /* need to reconfigure the beacon */
554                         sc->sc_beacons = 0;
555
556                         break;
557                 default:
558                         break;
559                 }
560         }
561
562         if ((conf->changed & IEEE80211_IFCC_BEACON) &&
563             (vif->type == IEEE80211_IF_TYPE_IBSS)) {
564                 /*
565                  * Allocate and setup the beacon frame.
566                  *
567                  * Stop any previous beacon DMA.  This may be
568                  * necessary, for example, when an ibss merge
569                  * causes reconfiguration; we may be called
570                  * with beacon transmission active.
571                  */
572                 ath9k_hw_stoptxdma(sc->sc_ah, sc->sc_bhalq);
573
574                 error = ath_beacon_alloc(sc, 0);
575                 if (error != 0)
576                         return error;
577
578                 ath_beacon_sync(sc, 0);
579         }
580
581         /* Check for WLAN_CAPABILITY_PRIVACY ? */
582         if ((avp->av_opmode != IEEE80211_IF_TYPE_STA)) {
583                 for (i = 0; i < IEEE80211_WEP_NKID; i++)
584                         if (ath9k_hw_keyisvalid(sc->sc_ah, (u_int16_t)i))
585                                 ath9k_hw_keysetmac(sc->sc_ah,
586                                                    (u_int16_t)i,
587                                                    sc->sc_curbssid);
588         }
589
590         /* Only legacy IBSS for now */
591         if (vif->type == IEEE80211_IF_TYPE_IBSS)
592                 ath_update_chainmask(sc, 0);
593
594         return 0;
595 }
596
597 #define SUPPORTED_FILTERS                       \
598         (FIF_PROMISC_IN_BSS |                   \
599         FIF_ALLMULTI |                          \
600         FIF_CONTROL |                           \
601         FIF_OTHER_BSS |                         \
602         FIF_BCN_PRBRESP_PROMISC |               \
603         FIF_FCSFAIL)
604
605 /* Accept unicast, bcast and mcast frames */
606
607 static void ath9k_configure_filter(struct ieee80211_hw *hw,
608                                    unsigned int changed_flags,
609                                    unsigned int *total_flags,
610                                    int mc_count,
611                                    struct dev_mc_list *mclist)
612 {
613         struct ath_softc *sc = hw->priv;
614
615         changed_flags &= SUPPORTED_FILTERS;
616         *total_flags &= SUPPORTED_FILTERS;
617
618         if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
619                 if (*total_flags & FIF_BCN_PRBRESP_PROMISC)
620                         ath_scan_start(sc);
621                 else
622                         ath_scan_end(sc);
623         }
624 }
625
626 static void ath9k_sta_notify(struct ieee80211_hw *hw,
627                              struct ieee80211_vif *vif,
628                              enum sta_notify_cmd cmd,
629                              const u8 *addr)
630 {
631         struct ath_softc *sc = hw->priv;
632         struct ath_node *an;
633         unsigned long flags;
634         DECLARE_MAC_BUF(mac);
635
636         spin_lock_irqsave(&sc->node_lock, flags);
637         an = ath_node_find(sc, (u8 *) addr);
638         spin_unlock_irqrestore(&sc->node_lock, flags);
639
640         switch (cmd) {
641         case STA_NOTIFY_ADD:
642                 spin_lock_irqsave(&sc->node_lock, flags);
643                 if (!an) {
644                         ath_node_attach(sc, (u8 *)addr, 0);
645                         DPRINTF(sc, ATH_DBG_CONFIG, "%s: Attach a node: %s\n",
646                                 __func__,
647                                 print_mac(mac, addr));
648                 } else {
649                         ath_node_get(sc, (u8 *)addr);
650                 }
651                 spin_unlock_irqrestore(&sc->node_lock, flags);
652                 break;
653         case STA_NOTIFY_REMOVE:
654                 if (!an)
655                         DPRINTF(sc, ATH_DBG_FATAL,
656                                 "%s: Removal of a non-existent node\n",
657                                 __func__);
658                 else {
659                         ath_node_put(sc, an, ATH9K_BH_STATUS_INTACT);
660                         DPRINTF(sc, ATH_DBG_CONFIG, "%s: Put a node: %s\n",
661                                 __func__,
662                                 print_mac(mac, addr));
663                 }
664                 break;
665         default:
666                 break;
667         }
668 }
669
670 static int ath9k_conf_tx(struct ieee80211_hw *hw,
671                          u16 queue,
672                          const struct ieee80211_tx_queue_params *params)
673 {
674         struct ath_softc *sc = hw->priv;
675         struct hal_txq_info qi;
676         int ret = 0, qnum;
677
678         if (queue >= WME_NUM_AC)
679                 return 0;
680
681         qi.tqi_aifs = params->aifs;
682         qi.tqi_cwmin = params->cw_min;
683         qi.tqi_cwmax = params->cw_max;
684         qi.tqi_burstTime = params->txop;
685         qnum = ath_get_hal_qnum(queue, sc);
686
687         DPRINTF(sc, ATH_DBG_CONFIG,
688                 "%s: Configure tx [queue/halq] [%d/%d],  "
689                 "aifs: %d, cw_min: %d, cw_max: %d, txop: %d\n",
690                 __func__,
691                 queue,
692                 qnum,
693                 params->aifs,
694                 params->cw_min,
695                 params->cw_max,
696                 params->txop);
697
698         ret = ath_txq_update(sc, qnum, &qi);
699         if (ret)
700                 DPRINTF(sc, ATH_DBG_FATAL,
701                         "%s: TXQ Update failed\n", __func__);
702
703         return ret;
704 }
705
706 static int ath9k_set_key(struct ieee80211_hw *hw,
707                          enum set_key_cmd cmd,
708                          const u8 *local_addr,
709                          const u8 *addr,
710                          struct ieee80211_key_conf *key)
711 {
712         struct ath_softc *sc = hw->priv;
713         int ret = 0;
714
715         DPRINTF(sc, ATH_DBG_KEYCACHE, " %s: Set HW Key\n", __func__);
716
717         switch (cmd) {
718         case SET_KEY:
719                 ret = ath_key_config(sc, addr, key);
720                 if (!ret) {
721                         set_bit(key->keyidx, sc->sc_keymap);
722                         key->hw_key_idx = key->keyidx;
723                         /* push IV and Michael MIC generation to stack */
724                         key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
725                         key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
726                 }
727                 break;
728         case DISABLE_KEY:
729                 ath_key_delete(sc, key);
730                 clear_bit(key->keyidx, sc->sc_keymap);
731                 sc->sc_keytype = HAL_CIPHER_CLR;
732                 break;
733         default:
734                 ret = -EINVAL;
735         }
736
737         return ret;
738 }
739
740 static void ath9k_ht_conf(struct ath_softc *sc,
741                           struct ieee80211_bss_conf *bss_conf)
742 {
743 #define IEEE80211_HT_CAP_40MHZ_INTOLERANT BIT(14)
744         struct ath_ht_info *ht_info = &sc->sc_ht_info;
745
746         if (bss_conf->assoc_ht) {
747                 ht_info->ext_chan_offset =
748                         bss_conf->ht_bss_conf->bss_cap &
749                                 IEEE80211_HT_IE_CHA_SEC_OFFSET;
750
751                 if (!(bss_conf->ht_conf->cap &
752                         IEEE80211_HT_CAP_40MHZ_INTOLERANT) &&
753                             (bss_conf->ht_bss_conf->bss_cap &
754                                 IEEE80211_HT_IE_CHA_WIDTH))
755                         ht_info->tx_chan_width = HAL_HT_MACMODE_2040;
756                 else
757                         ht_info->tx_chan_width = HAL_HT_MACMODE_20;
758
759                 ath9k_hw_set11nmac2040(sc->sc_ah, ht_info->tx_chan_width);
760                 ht_info->maxampdu = 1 << (IEEE80211_HTCAP_MAXRXAMPDU_FACTOR +
761                                         bss_conf->ht_conf->ampdu_factor);
762                 ht_info->mpdudensity =
763                         parse_mpdudensity(bss_conf->ht_conf->ampdu_density);
764
765         }
766
767 #undef IEEE80211_HT_CAP_40MHZ_INTOLERANT
768 }
769
770 static void ath9k_bss_assoc_info(struct ath_softc *sc,
771                                  struct ieee80211_bss_conf *bss_conf)
772 {
773         struct ieee80211_hw *hw = sc->hw;
774         struct ieee80211_channel *curchan = hw->conf.channel;
775         struct hal_channel hchan;
776         struct ath_vap *avp;
777         DECLARE_MAC_BUF(mac);
778
779         if (bss_conf->assoc) {
780                 DPRINTF(sc, ATH_DBG_CONFIG, "%s: Bss Info ASSOC %d\n",
781                         __func__,
782                         bss_conf->aid);
783
784                 avp = sc->sc_vaps[0];
785                 if (avp == NULL) {
786                         DPRINTF(sc, ATH_DBG_FATAL, "%s: Invalid interface\n",
787                                 __func__);
788                         return;
789                 }
790
791                 /* Update ratectrl about the new state */
792                 ath_rate_newstate(sc, avp, 1);
793
794                 /* New association, store aid */
795                 if (avp->av_opmode == HAL_M_STA) {
796                         sc->sc_curaid = bss_conf->aid;
797                         ath9k_hw_write_associd(sc->sc_ah, sc->sc_curbssid,
798                                                sc->sc_curaid);
799                 }
800
801                 /* Configure the beacon */
802                 ath_beacon_config(sc, 0);
803                 sc->sc_beacons = 1;
804
805                 /* Reset rssi stats */
806                 sc->sc_halstats.ns_avgbrssi = ATH_RSSI_DUMMY_MARKER;
807                 sc->sc_halstats.ns_avgrssi = ATH_RSSI_DUMMY_MARKER;
808                 sc->sc_halstats.ns_avgtxrssi = ATH_RSSI_DUMMY_MARKER;
809                 sc->sc_halstats.ns_avgtxrate = ATH_RATE_DUMMY_MARKER;
810
811                 /* Update chainmask */
812                 ath_update_chainmask(sc, bss_conf->assoc_ht);
813
814                 DPRINTF(sc, ATH_DBG_CONFIG,
815                         "%s: bssid %s aid 0x%x\n",
816                         __func__,
817                         print_mac(mac, sc->sc_curbssid), sc->sc_curaid);
818
819                 DPRINTF(sc, ATH_DBG_CONFIG, "%s: Set channel: %d MHz\n",
820                         __func__,
821                         curchan->center_freq);
822
823                 hchan.channel = curchan->center_freq;
824                 hchan.channelFlags = ath_chan2flags(curchan, sc);
825
826                 /* set h/w channel */
827                 if (ath_set_channel(sc, &hchan) < 0)
828                         DPRINTF(sc, ATH_DBG_FATAL,
829                                 "%s: Unable to set channel\n",
830                                 __func__);
831         } else {
832                 DPRINTF(sc, ATH_DBG_CONFIG,
833                 "%s: Bss Info DISSOC\n", __func__);
834                 sc->sc_curaid = 0;
835         }
836 }
837
838 static void ath9k_bss_info_changed(struct ieee80211_hw *hw,
839                                    struct ieee80211_vif *vif,
840                                    struct ieee80211_bss_conf *bss_conf,
841                                    u32 changed)
842 {
843         struct ath_softc *sc = hw->priv;
844
845         if (changed & BSS_CHANGED_ERP_PREAMBLE) {
846                 DPRINTF(sc, ATH_DBG_CONFIG, "%s: BSS Changed PREAMBLE %d\n",
847                         __func__,
848                         bss_conf->use_short_preamble);
849                 if (bss_conf->use_short_preamble)
850                         sc->sc_flags |= ATH_PREAMBLE_SHORT;
851                 else
852                         sc->sc_flags &= ~ATH_PREAMBLE_SHORT;
853         }
854
855         if (changed & BSS_CHANGED_ERP_CTS_PROT) {
856                 DPRINTF(sc, ATH_DBG_CONFIG, "%s: BSS Changed CTS PROT %d\n",
857                         __func__,
858                         bss_conf->use_cts_prot);
859                 if (bss_conf->use_cts_prot &&
860                     hw->conf.channel->band != IEEE80211_BAND_5GHZ)
861                         sc->sc_flags |= ATH_PROTECT_ENABLE;
862                 else
863                         sc->sc_flags &= ~ATH_PROTECT_ENABLE;
864         }
865
866         if (changed & BSS_CHANGED_HT) {
867                 DPRINTF(sc, ATH_DBG_CONFIG, "%s: BSS Changed HT %d\n",
868                         __func__,
869                         bss_conf->assoc_ht);
870                 ath9k_ht_conf(sc, bss_conf);
871         }
872
873         if (changed & BSS_CHANGED_ASSOC) {
874                 DPRINTF(sc, ATH_DBG_CONFIG, "%s: BSS Changed ASSOC %d\n",
875                         __func__,
876                         bss_conf->assoc);
877                 ath9k_bss_assoc_info(sc, bss_conf);
878         }
879 }
880
881 static u64 ath9k_get_tsf(struct ieee80211_hw *hw)
882 {
883         u_int64_t tsf;
884         struct ath_softc *sc = hw->priv;
885         struct ath_hal *ah = sc->sc_ah;
886
887         tsf = ath9k_hw_gettsf64(ah);
888
889         return tsf;
890 }
891
892 static void ath9k_reset_tsf(struct ieee80211_hw *hw)
893 {
894         struct ath_softc *sc = hw->priv;
895         struct ath_hal *ah = sc->sc_ah;
896
897         ath9k_hw_reset_tsf(ah);
898 }
899
900 static int ath9k_ampdu_action(struct ieee80211_hw *hw,
901                        enum ieee80211_ampdu_mlme_action action,
902                        const u8 *addr,
903                        u16 tid,
904                        u16 *ssn)
905 {
906         struct ath_softc *sc = hw->priv;
907         int ret = 0;
908
909         switch (action) {
910         case IEEE80211_AMPDU_RX_START:
911                 ret = ath_rx_aggr_start(sc, addr, tid, ssn);
912                 if (ret < 0)
913                         DPRINTF(sc, ATH_DBG_FATAL,
914                                 "%s: Unable to start RX aggregation\n",
915                                 __func__);
916                 break;
917         case IEEE80211_AMPDU_RX_STOP:
918                 ret = ath_rx_aggr_stop(sc, addr, tid);
919                 if (ret < 0)
920                         DPRINTF(sc, ATH_DBG_FATAL,
921                                 "%s: Unable to stop RX aggregation\n",
922                                 __func__);
923                 break;
924         case IEEE80211_AMPDU_TX_START:
925                 ret = ath_tx_aggr_start(sc, addr, tid, ssn);
926                 if (ret < 0)
927                         DPRINTF(sc, ATH_DBG_FATAL,
928                                 "%s: Unable to start TX aggregation\n",
929                                 __func__);
930                 else
931                         ieee80211_start_tx_ba_cb_irqsafe(hw, (u8 *)addr, tid);
932                 break;
933         case IEEE80211_AMPDU_TX_STOP:
934                 ret = ath_tx_aggr_stop(sc, addr, tid);
935                 if (ret < 0)
936                         DPRINTF(sc, ATH_DBG_FATAL,
937                                 "%s: Unable to stop TX aggregation\n",
938                                 __func__);
939
940                 ieee80211_stop_tx_ba_cb_irqsafe(hw, (u8 *)addr, tid);
941                 break;
942         default:
943                 DPRINTF(sc, ATH_DBG_FATAL,
944                         "%s: Unknown AMPDU action\n", __func__);
945         }
946
947         return ret;
948 }
949
950 static struct ieee80211_ops ath9k_ops = {
951         .tx                 = ath9k_tx,
952         .start              = ath9k_start,
953         .stop               = ath9k_stop,
954         .add_interface      = ath9k_add_interface,
955         .remove_interface   = ath9k_remove_interface,
956         .config             = ath9k_config,
957         .config_interface   = ath9k_config_interface,
958         .configure_filter   = ath9k_configure_filter,
959         .get_stats          = NULL,
960         .sta_notify         = ath9k_sta_notify,
961         .conf_tx            = ath9k_conf_tx,
962         .get_tx_stats       = NULL,
963         .bss_info_changed   = ath9k_bss_info_changed,
964         .set_tim            = NULL,
965         .set_key            = ath9k_set_key,
966         .hw_scan            = NULL,
967         .get_tkip_seq       = NULL,
968         .set_rts_threshold  = NULL,
969         .set_frag_threshold = NULL,
970         .set_retry_limit    = NULL,
971         .get_tsf            = ath9k_get_tsf,
972         .reset_tsf          = ath9k_reset_tsf,
973         .tx_last_beacon     = NULL,
974         .ampdu_action       = ath9k_ampdu_action
975 };
976
977 u_int32_t ath_chan2flags(struct ieee80211_channel *chan,
978                                 struct ath_softc *sc)
979 {
980         struct ieee80211_hw *hw = sc->hw;
981         struct ath_ht_info *ht_info = &sc->sc_ht_info;
982
983         if (sc->sc_scanning) {
984                 if (chan->band == IEEE80211_BAND_5GHZ) {
985                         if (ath_check_chanflags(chan, CHANNEL_A_HT20, sc))
986                                 return CHANNEL_A_HT20;
987                         else
988                                 return CHANNEL_A;
989                 } else {
990                         if (ath_check_chanflags(chan, CHANNEL_G_HT20, sc))
991                                 return CHANNEL_G_HT20;
992                         else if (ath_check_chanflags(chan, CHANNEL_G, sc))
993                                 return CHANNEL_G;
994                         else
995                                 return CHANNEL_B;
996                 }
997         } else {
998                 if (chan->band == IEEE80211_BAND_2GHZ) {
999                         if (!hw->conf.ht_conf.ht_supported) {
1000                                 if (ath_check_chanflags(chan, CHANNEL_G, sc))
1001                                         return CHANNEL_G;
1002                                 else
1003                                         return CHANNEL_B;
1004                         }
1005                         if ((ht_info->ext_chan_offset ==
1006                              IEEE80211_HT_IE_CHA_SEC_NONE) &&
1007                             (ht_info->tx_chan_width == HAL_HT_MACMODE_20))
1008                                 return CHANNEL_G_HT20;
1009                         if ((ht_info->ext_chan_offset ==
1010                              IEEE80211_HT_IE_CHA_SEC_ABOVE) &&
1011                             (ht_info->tx_chan_width == HAL_HT_MACMODE_2040))
1012                                 return CHANNEL_G_HT40PLUS;
1013                         if ((ht_info->ext_chan_offset ==
1014                              IEEE80211_HT_IE_CHA_SEC_BELOW) &&
1015                             (ht_info->tx_chan_width == HAL_HT_MACMODE_2040))
1016                                 return CHANNEL_G_HT40MINUS;
1017                         return CHANNEL_B;
1018                 } else {
1019                         if (!hw->conf.ht_conf.ht_supported)
1020                                 return CHANNEL_A;
1021                         if ((ht_info->ext_chan_offset ==
1022                              IEEE80211_HT_IE_CHA_SEC_NONE) &&
1023                             (ht_info->tx_chan_width == HAL_HT_MACMODE_20))
1024                                 return CHANNEL_A_HT20;
1025                         if ((ht_info->ext_chan_offset ==
1026                              IEEE80211_HT_IE_CHA_SEC_ABOVE) &&
1027                             (ht_info->tx_chan_width == HAL_HT_MACMODE_2040))
1028                                 return CHANNEL_A_HT40PLUS;
1029                         if ((ht_info->ext_chan_offset ==
1030                              IEEE80211_HT_IE_CHA_SEC_BELOW) &&
1031                             (ht_info->tx_chan_width == HAL_HT_MACMODE_2040))
1032                                 return CHANNEL_A_HT40MINUS;
1033                         return CHANNEL_A;
1034                 }
1035         }
1036 }
1037
1038 void ath_setup_channel_list(struct ath_softc *sc,
1039                             enum ieee80211_clist_cmd cmd,
1040                             const struct hal_channel *chans,
1041                             int nchan,
1042                             const u_int8_t *regclassids,
1043                             u_int nregclass,
1044                             int countrycode)
1045 {
1046         const struct hal_channel *c;
1047         int i, a = 0, b = 0, flags;
1048
1049         if (countrycode == CTRY_DEFAULT) {
1050                 for (i = 0; i < nchan; i++) {
1051                         c = &chans[i];
1052                         flags = 0;
1053                         /* XXX: Ah! make more readable, and
1054                          * idententation friendly */
1055                         if (IS_CHAN_2GHZ(c) &&
1056                             !test_update_chan(IEEE80211_BAND_2GHZ, c, sc)) {
1057                                 sc->channels[IEEE80211_BAND_2GHZ][a].band =
1058                                         IEEE80211_BAND_2GHZ;
1059                                 sc->channels[IEEE80211_BAND_2GHZ][a].
1060                                         center_freq =
1061                                         c->channel;
1062                                 sc->channels[IEEE80211_BAND_2GHZ][a].max_power =
1063                                         c->maxTxPower;
1064                                 sc->channels[IEEE80211_BAND_2GHZ][a].hw_value =
1065                                         c->channelFlags;
1066
1067                                 if (c->privFlags & CHANNEL_DISALLOW_ADHOC)
1068                                         flags |= IEEE80211_CHAN_NO_IBSS;
1069                                 if (IS_CHAN_PASSIVE(c))
1070                                         flags |= IEEE80211_CHAN_PASSIVE_SCAN;
1071
1072                                 sc->channels[IEEE80211_BAND_2GHZ][a].flags =
1073                                         flags;
1074                                 sc->sbands[IEEE80211_BAND_2GHZ].n_channels++;
1075                                 a++;
1076                                 DPRINTF(sc, ATH_DBG_CONFIG,
1077                                         "%s: 2MHz channel: %d, "
1078                                         "channelFlags: 0x%x\n",
1079                                         __func__,
1080                                         c->channel,
1081                                         c->channelFlags);
1082                         } else if (IS_CHAN_5GHZ(c) &&
1083                          !test_update_chan(IEEE80211_BAND_5GHZ, c, sc)) {
1084                                 sc->channels[IEEE80211_BAND_5GHZ][b].band =
1085                                         IEEE80211_BAND_5GHZ;
1086                                 sc->channels[IEEE80211_BAND_5GHZ][b].
1087                                         center_freq =
1088                                         c->channel;
1089                                 sc->channels[IEEE80211_BAND_5GHZ][b].max_power =
1090                                         c->maxTxPower;
1091                                 sc->channels[IEEE80211_BAND_5GHZ][b].hw_value =
1092                                         c->channelFlags;
1093
1094                                 if (c->privFlags & CHANNEL_DISALLOW_ADHOC)
1095                                         flags |= IEEE80211_CHAN_NO_IBSS;
1096                                 if (IS_CHAN_PASSIVE(c))
1097                                         flags |= IEEE80211_CHAN_PASSIVE_SCAN;
1098
1099                                 sc->channels[IEEE80211_BAND_5GHZ][b].
1100                                         flags = flags;
1101                                 sc->sbands[IEEE80211_BAND_5GHZ].n_channels++;
1102                                 b++;
1103                                 DPRINTF(sc, ATH_DBG_CONFIG,
1104                                         "%s: 5MHz channel: %d, "
1105                                         "channelFlags: 0x%x\n",
1106                                         __func__,
1107                                         c->channel,
1108                                         c->channelFlags);
1109                         }
1110                 }
1111         }
1112 }
1113
1114 void ath_get_beaconconfig(struct ath_softc *sc,
1115                           int if_id,
1116                           struct ath_beacon_config *conf)
1117 {
1118         struct ieee80211_hw *hw = sc->hw;
1119
1120         /* fill in beacon config data */
1121
1122         conf->beacon_interval = hw->conf.beacon_int;
1123         conf->listen_interval = 100;
1124         conf->dtim_count = 1;
1125         conf->bmiss_timeout = ATH_DEFAULT_BMISS_LIMIT * conf->listen_interval;
1126 }
1127
1128 int ath_update_beacon(struct ath_softc *sc,
1129                       int if_id,
1130                       struct ath_beacon_offset *bo,
1131                       struct sk_buff *skb,
1132                       int mcast)
1133 {
1134         return 0;
1135 }
1136
1137 void ath_tx_complete(struct ath_softc *sc, struct sk_buff *skb,
1138                      struct ath_xmit_status *tx_status, struct ath_node *an)
1139 {
1140         struct ieee80211_hw *hw = sc->hw;
1141         struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
1142
1143         DPRINTF(sc, ATH_DBG_XMIT,
1144                 "%s: TX complete: skb: %p\n", __func__, skb);
1145
1146         if (tx_info->flags & IEEE80211_TX_CTL_NO_ACK ||
1147                 tx_info->flags & IEEE80211_TX_STAT_TX_FILTERED) {
1148                 /* free driver's private data area of tx_info */
1149                 if (tx_info->driver_data[0] != NULL)
1150                         kfree(tx_info->driver_data[0]);
1151                         tx_info->driver_data[0] = NULL;
1152         }
1153
1154         if (tx_status->flags & ATH_TX_BAR) {
1155                 tx_info->flags |= IEEE80211_TX_STAT_AMPDU_NO_BACK;
1156                 tx_status->flags &= ~ATH_TX_BAR;
1157         }
1158         if (tx_status->flags)
1159                 tx_info->status.excessive_retries = 1;
1160
1161         tx_info->status.retry_count = tx_status->retries;
1162
1163         ieee80211_tx_status(hw, skb);
1164         if (an)
1165                 ath_node_put(sc, an, ATH9K_BH_STATUS_CHANGE);
1166 }
1167
1168 int ath__rx_indicate(struct ath_softc *sc,
1169                      struct sk_buff *skb,
1170                      struct ath_recv_status *status,
1171                      u_int16_t keyix)
1172 {
1173         struct ieee80211_hw *hw = sc->hw;
1174         struct ath_node *an = NULL;
1175         struct ieee80211_rx_status rx_status;
1176         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1177         int hdrlen = ieee80211_get_hdrlen_from_skb(skb);
1178         int padsize;
1179         enum ATH_RX_TYPE st;
1180
1181         /* see if any padding is done by the hw and remove it */
1182         if (hdrlen & 3) {
1183                 padsize = hdrlen % 4;
1184                 memmove(skb->data + padsize, skb->data, hdrlen);
1185                 skb_pull(skb, padsize);
1186         }
1187
1188         /* remove FCS before passing up to protocol stack */
1189         skb_trim(skb, (skb->len - FCS_LEN));
1190
1191         /* Prepare rx status */
1192         ath9k_rx_prepare(sc, skb, status, &rx_status);
1193
1194         if (!(keyix == HAL_RXKEYIX_INVALID) &&
1195             !(status->flags & ATH_RX_DECRYPT_ERROR)) {
1196                 rx_status.flag |= RX_FLAG_DECRYPTED;
1197         } else if ((le16_to_cpu(hdr->frame_control) & IEEE80211_FCTL_PROTECTED)
1198                    && !(status->flags & ATH_RX_DECRYPT_ERROR)
1199                    && skb->len >= hdrlen + 4) {
1200                 keyix = skb->data[hdrlen + 3] >> 6;
1201
1202                 if (test_bit(keyix, sc->sc_keymap))
1203                         rx_status.flag |= RX_FLAG_DECRYPTED;
1204         }
1205
1206         spin_lock_bh(&sc->node_lock);
1207         an = ath_node_find(sc, hdr->addr2);
1208         spin_unlock_bh(&sc->node_lock);
1209
1210         if (an) {
1211                 ath_rx_input(sc, an,
1212                              hw->conf.ht_conf.ht_supported,
1213                              skb, status, &st);
1214         }
1215         if (!an || (st != ATH_RX_CONSUMED))
1216                 __ieee80211_rx(hw, skb, &rx_status);
1217
1218         return 0;
1219 }
1220
1221 int ath_rx_subframe(struct ath_node *an,
1222                     struct sk_buff *skb,
1223                     struct ath_recv_status *status)
1224 {
1225         struct ath_softc *sc = an->an_sc;
1226         struct ieee80211_hw *hw = sc->hw;
1227         struct ieee80211_rx_status rx_status;
1228
1229         /* Prepare rx status */
1230         ath9k_rx_prepare(sc, skb, status, &rx_status);
1231         if (!(status->flags & ATH_RX_DECRYPT_ERROR))
1232                 rx_status.flag |= RX_FLAG_DECRYPTED;
1233
1234         __ieee80211_rx(hw, skb, &rx_status);
1235
1236         return 0;
1237 }
1238
1239 enum hal_ht_macmode ath_cwm_macmode(struct ath_softc *sc)
1240 {
1241         return sc->sc_ht_info.tx_chan_width;
1242 }
1243
1244 void ath_setup_rate(struct ath_softc *sc,
1245                     enum wireless_mode wMode,
1246                     enum RATE_TYPE type,
1247                     const struct hal_rate_table *rt)
1248 {
1249         int i, maxrates, a = 0, b = 0;
1250         struct ieee80211_supported_band *band_2ghz;
1251         struct ieee80211_supported_band *band_5ghz;
1252         struct ieee80211_rate *rates_2ghz;
1253         struct ieee80211_rate *rates_5ghz;
1254
1255         if ((wMode >= WIRELESS_MODE_MAX) || (type != NORMAL_RATE))
1256                 return;
1257
1258         band_2ghz = &sc->sbands[IEEE80211_BAND_2GHZ];
1259         band_5ghz = &sc->sbands[IEEE80211_BAND_5GHZ];
1260         rates_2ghz = sc->rates[IEEE80211_BAND_2GHZ];
1261         rates_5ghz = sc->rates[IEEE80211_BAND_5GHZ];
1262
1263         if (rt->rateCount > ATH_RATE_MAX)
1264                 maxrates = ATH_RATE_MAX;
1265         else
1266                 maxrates = rt->rateCount;
1267
1268         if ((band_2ghz->n_bitrates != 0) && (band_5ghz->n_bitrates != 0)) {
1269                 DPRINTF(sc, ATH_DBG_CONFIG,
1270                         "%s: Rates already setup\n", __func__);
1271                 return;
1272         }
1273
1274         for (i = 0; i < maxrates; i++) {
1275                 switch (wMode) {
1276                 case WIRELESS_MODE_11b:
1277                 case WIRELESS_MODE_11g:
1278                         rates_2ghz[a].bitrate = rt->info[i].rateKbps / 100;
1279                         rates_2ghz[a].hw_value = rt->info[i].rateCode;
1280                         a++;
1281                         band_2ghz->n_bitrates = a;
1282                         break;
1283                 case WIRELESS_MODE_11a:
1284                         rates_5ghz[b].bitrate = rt->info[i].rateKbps / 100;
1285                         rates_5ghz[b].hw_value = rt->info[i].rateCode;
1286                         b++;
1287                         band_5ghz->n_bitrates = b;
1288                         break;
1289                 default:
1290                         break;
1291                 }
1292         }
1293
1294         if (band_2ghz->n_bitrates) {
1295                 for (i = 0; i < band_2ghz->n_bitrates; i++) {
1296                         DPRINTF(sc, ATH_DBG_CONFIG,
1297                                 "%s: 2GHz Rate: %2dMbps, ratecode: %2d\n",
1298                                 __func__,
1299                                 rates_2ghz[i].bitrate / 10,
1300                                 rates_2ghz[i].hw_value);
1301                 }
1302         } else if (band_5ghz->n_bitrates) {
1303                 for (i = 0; i < band_5ghz->n_bitrates; i++) {
1304                         DPRINTF(sc, ATH_DBG_CONFIG,
1305                                 "%s: 5Ghz Rate: %2dMbps, ratecode: %2d\n",
1306                                 __func__,
1307                                 rates_5ghz[i].bitrate / 10,
1308                                 rates_5ghz[i].hw_value);
1309                 }
1310         }
1311 }
1312
1313 static int ath_detach(struct ath_softc *sc)
1314 {
1315         struct ieee80211_hw *hw = sc->hw;
1316
1317         DPRINTF(sc, ATH_DBG_CONFIG, "%s: Detach ATH hw\n", __func__);
1318
1319         /* Unregister hw */
1320
1321         ieee80211_unregister_hw(hw);
1322
1323         /* unregister Rate control */
1324         ath_rate_control_unregister();
1325
1326         /* tx/rx cleanup */
1327
1328         ath_rx_cleanup(sc);
1329         ath_tx_cleanup(sc);
1330
1331         /* Deinit */
1332
1333         ath_deinit(sc);
1334
1335         return 0;
1336 }
1337
1338 static int ath_attach(u_int16_t devid,
1339                       struct ath_softc *sc)
1340 {
1341         struct ieee80211_hw *hw = sc->hw;
1342         int error = 0;
1343
1344         DPRINTF(sc, ATH_DBG_CONFIG, "%s: Attach ATH hw\n", __func__);
1345
1346         error = ath_init(devid, sc);
1347         if (error != 0)
1348                 return error;
1349
1350         /* Init nodes */
1351
1352         INIT_LIST_HEAD(&sc->node_list);
1353         spin_lock_init(&sc->node_lock);
1354
1355         /* get mac address from hardware and set in mac80211 */
1356
1357         SET_IEEE80211_PERM_ADDR(hw, sc->sc_myaddr);
1358
1359         /* setup channels and rates */
1360
1361         sc->sbands[IEEE80211_BAND_2GHZ].channels =
1362                 sc->channels[IEEE80211_BAND_2GHZ];
1363         sc->sbands[IEEE80211_BAND_2GHZ].bitrates =
1364                 sc->rates[IEEE80211_BAND_2GHZ];
1365         sc->sbands[IEEE80211_BAND_2GHZ].band = IEEE80211_BAND_2GHZ;
1366
1367         if (sc->sc_ah->ah_caps.halHTSupport)
1368                 /* Setup HT capabilities for 2.4Ghz*/
1369                 setup_ht_cap(&sc->sbands[IEEE80211_BAND_2GHZ].ht_info);
1370
1371         hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
1372                 &sc->sbands[IEEE80211_BAND_2GHZ];
1373
1374         if (sc->sc_ah->ah_caps.halWirelessModes & ATH9K_MODE_SEL_11A) {
1375                 sc->sbands[IEEE80211_BAND_5GHZ].channels =
1376                         sc->channels[IEEE80211_BAND_5GHZ];
1377                 sc->sbands[IEEE80211_BAND_5GHZ].bitrates =
1378                         sc->rates[IEEE80211_BAND_5GHZ];
1379                 sc->sbands[IEEE80211_BAND_5GHZ].band =
1380                         IEEE80211_BAND_5GHZ;
1381
1382                 if (sc->sc_ah->ah_caps.halHTSupport)
1383                         /* Setup HT capabilities for 5Ghz*/
1384                         setup_ht_cap(&sc->sbands[IEEE80211_BAND_5GHZ].ht_info);
1385
1386                 hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
1387                         &sc->sbands[IEEE80211_BAND_5GHZ];
1388         }
1389
1390         /* FIXME: Have to figure out proper hw init values later */
1391
1392         hw->queues = 4;
1393         hw->ampdu_queues = 1;
1394
1395         /* Register rate control */
1396         hw->rate_control_algorithm = "ath9k_rate_control";
1397         error = ath_rate_control_register();
1398         if (error != 0) {
1399                 DPRINTF(sc, ATH_DBG_FATAL,
1400                         "%s: Unable to register rate control "
1401                         "algorithm:%d\n", __func__, error);
1402                 ath_rate_control_unregister();
1403                 goto bad;
1404         }
1405
1406         error = ieee80211_register_hw(hw);
1407         if (error != 0) {
1408                 ath_rate_control_unregister();
1409                 goto bad;
1410         }
1411
1412         /* initialize tx/rx engine */
1413
1414         error = ath_tx_init(sc, ATH_TXBUF);
1415         if (error != 0)
1416                 goto bad1;
1417
1418         error = ath_rx_init(sc, ATH_RXBUF);
1419         if (error != 0)
1420                 goto bad1;
1421
1422         return 0;
1423 bad1:
1424         ath_detach(sc);
1425 bad:
1426         return error;
1427 }
1428
1429 static int ath_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1430 {
1431         void __iomem *mem;
1432         struct ath_softc *sc;
1433         struct ieee80211_hw *hw;
1434         const char *athname;
1435         u_int8_t csz;
1436         u32 val;
1437         int ret = 0;
1438
1439         if (pci_enable_device(pdev))
1440                 return -EIO;
1441
1442         /* XXX 32-bit addressing only */
1443         if (pci_set_dma_mask(pdev, 0xffffffff)) {
1444                 printk(KERN_ERR "ath_pci: 32-bit DMA not available\n");
1445                 ret = -ENODEV;
1446                 goto bad;
1447         }
1448
1449         /*
1450          * Cache line size is used to size and align various
1451          * structures used to communicate with the hardware.
1452          */
1453         pci_read_config_byte(pdev, PCI_CACHE_LINE_SIZE, &csz);
1454         if (csz == 0) {
1455                 /*
1456                  * Linux 2.4.18 (at least) writes the cache line size
1457                  * register as a 16-bit wide register which is wrong.
1458                  * We must have this setup properly for rx buffer
1459                  * DMA to work so force a reasonable value here if it
1460                  * comes up zero.
1461                  */
1462                 csz = L1_CACHE_BYTES / sizeof(u_int32_t);
1463                 pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, csz);
1464         }
1465         /*
1466          * The default setting of latency timer yields poor results,
1467          * set it to the value used by other systems. It may be worth
1468          * tweaking this setting more.
1469          */
1470         pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0xa8);
1471
1472         pci_set_master(pdev);
1473
1474         /*
1475          * Disable the RETRY_TIMEOUT register (0x41) to keep
1476          * PCI Tx retries from interfering with C3 CPU state.
1477          */
1478         pci_read_config_dword(pdev, 0x40, &val);
1479         if ((val & 0x0000ff00) != 0)
1480                 pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
1481
1482         ret = pci_request_region(pdev, 0, "ath9k");
1483         if (ret) {
1484                 dev_err(&pdev->dev, "PCI memory region reserve error\n");
1485                 ret = -ENODEV;
1486                 goto bad;
1487         }
1488
1489         mem = pci_iomap(pdev, 0, 0);
1490         if (!mem) {
1491                 printk(KERN_ERR "PCI memory map error\n") ;
1492                 ret = -EIO;
1493                 goto bad1;
1494         }
1495
1496         hw = ieee80211_alloc_hw(sizeof(struct ath_softc), &ath9k_ops);
1497         if (hw == NULL) {
1498                 printk(KERN_ERR "ath_pci: no memory for ieee80211_hw\n");
1499                 goto bad2;
1500         }
1501
1502         SET_IEEE80211_DEV(hw, &pdev->dev);
1503         pci_set_drvdata(pdev, hw);
1504
1505         sc = hw->priv;
1506         sc->hw = hw;
1507         sc->pdev = pdev;
1508         sc->mem = mem;
1509
1510         if (ath_attach(id->device, sc) != 0) {
1511                 ret = -ENODEV;
1512                 goto bad3;
1513         }
1514
1515         /* setup interrupt service routine */
1516
1517         if (request_irq(pdev->irq, ath_isr, IRQF_SHARED, "ath", sc)) {
1518                 printk(KERN_ERR "%s: request_irq failed\n",
1519                         wiphy_name(hw->wiphy));
1520                 ret = -EIO;
1521                 goto bad4;
1522         }
1523
1524         athname = ath9k_hw_probe(id->vendor, id->device);
1525
1526         printk(KERN_INFO "%s: %s: mem=0x%lx, irq=%d\n",
1527                wiphy_name(hw->wiphy),
1528                athname ? athname : "Atheros ???",
1529                (unsigned long)mem, pdev->irq);
1530
1531         return 0;
1532 bad4:
1533         ath_detach(sc);
1534 bad3:
1535         ieee80211_free_hw(hw);
1536 bad2:
1537         pci_iounmap(pdev, mem);
1538 bad1:
1539         pci_release_region(pdev, 0);
1540 bad:
1541         pci_disable_device(pdev);
1542         return ret;
1543 }
1544
1545 static void ath_pci_remove(struct pci_dev *pdev)
1546 {
1547         struct ieee80211_hw *hw = pci_get_drvdata(pdev);
1548         struct ath_softc *sc = hw->priv;
1549
1550         if (pdev->irq)
1551                 free_irq(pdev->irq, sc);
1552         ath_detach(sc);
1553         pci_iounmap(pdev, sc->mem);
1554         pci_release_region(pdev, 0);
1555         pci_disable_device(pdev);
1556         ieee80211_free_hw(hw);
1557 }
1558
1559 #ifdef CONFIG_PM
1560
1561 static int ath_pci_suspend(struct pci_dev *pdev, pm_message_t state)
1562 {
1563         pci_save_state(pdev);
1564         pci_disable_device(pdev);
1565         pci_set_power_state(pdev, 3);
1566
1567         return 0;
1568 }
1569
1570 static int ath_pci_resume(struct pci_dev *pdev)
1571 {
1572         u32 val;
1573         int err;
1574
1575         err = pci_enable_device(pdev);
1576         if (err)
1577                 return err;
1578         pci_restore_state(pdev);
1579         /*
1580          * Suspend/Resume resets the PCI configuration space, so we have to
1581          * re-disable the RETRY_TIMEOUT register (0x41) to keep
1582          * PCI Tx retries from interfering with C3 CPU state
1583          */
1584         pci_read_config_dword(pdev, 0x40, &val);
1585         if ((val & 0x0000ff00) != 0)
1586                 pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
1587
1588         return 0;
1589 }
1590
1591 #endif /* CONFIG_PM */
1592
1593 MODULE_DEVICE_TABLE(pci, ath_pci_id_table);
1594
1595 static struct pci_driver ath_pci_driver = {
1596         .name       = "ath9k",
1597         .id_table   = ath_pci_id_table,
1598         .probe      = ath_pci_probe,
1599         .remove     = ath_pci_remove,
1600 #ifdef CONFIG_PM
1601         .suspend    = ath_pci_suspend,
1602         .resume     = ath_pci_resume,
1603 #endif /* CONFIG_PM */
1604 };
1605
1606 static int __init init_ath_pci(void)
1607 {
1608         printk(KERN_INFO "%s: %s\n", dev_info, ATH_PCI_VERSION);
1609
1610         if (pci_register_driver(&ath_pci_driver) < 0) {
1611                 printk(KERN_ERR
1612                         "ath_pci: No devices found, driver not installed.\n");
1613                 pci_unregister_driver(&ath_pci_driver);
1614                 return -ENODEV;
1615         }
1616
1617         return 0;
1618 }
1619 module_init(init_ath_pci);
1620
1621 static void __exit exit_ath_pci(void)
1622 {
1623         pci_unregister_driver(&ath_pci_driver);
1624         printk(KERN_INFO "%s: driver unloaded\n", dev_info);
1625 }
1626 module_exit(exit_ath_pci);