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