main.c 36 KB

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