main.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533
  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) ? CHANNEL_G : CHANNEL_A;
  432. sc->sc_config.txpowlimit = 2 * conf->power_level;
  433. /* set h/w channel */
  434. if (ath_set_channel(sc, &sc->sc_ah->ah_channels[pos]) < 0)
  435. DPRINTF(sc, ATH_DBG_FATAL, "%s: Unable to set channel\n",
  436. __func__);
  437. return 0;
  438. }
  439. static int ath9k_config_interface(struct ieee80211_hw *hw,
  440. struct ieee80211_vif *vif,
  441. struct ieee80211_if_conf *conf)
  442. {
  443. struct ath_softc *sc = hw->priv;
  444. struct ath_vap *avp;
  445. u32 rfilt = 0;
  446. int error, i;
  447. DECLARE_MAC_BUF(mac);
  448. avp = sc->sc_vaps[0];
  449. if (avp == NULL) {
  450. DPRINTF(sc, ATH_DBG_FATAL, "%s: Invalid interface\n",
  451. __func__);
  452. return -EINVAL;
  453. }
  454. if ((conf->changed & IEEE80211_IFCC_BSSID) &&
  455. !is_zero_ether_addr(conf->bssid)) {
  456. switch (vif->type) {
  457. case IEEE80211_IF_TYPE_STA:
  458. case IEEE80211_IF_TYPE_IBSS:
  459. /* Update ratectrl about the new state */
  460. ath_rate_newstate(sc, avp);
  461. /* Set rx filter */
  462. rfilt = ath_calcrxfilter(sc);
  463. ath9k_hw_setrxfilter(sc->sc_ah, rfilt);
  464. /* Set BSSID */
  465. memcpy(sc->sc_curbssid, conf->bssid, ETH_ALEN);
  466. sc->sc_curaid = 0;
  467. ath9k_hw_write_associd(sc->sc_ah, sc->sc_curbssid,
  468. sc->sc_curaid);
  469. /* Set aggregation protection mode parameters */
  470. sc->sc_config.ath_aggr_prot = 0;
  471. /*
  472. * Reset our TSF so that its value is lower than the
  473. * beacon that we are trying to catch.
  474. * Only then hw will update its TSF register with the
  475. * new beacon. Reset the TSF before setting the BSSID
  476. * to avoid allowing in any frames that would update
  477. * our TSF only to have us clear it
  478. * immediately thereafter.
  479. */
  480. ath9k_hw_reset_tsf(sc->sc_ah);
  481. /* Disable BMISS interrupt when we're not associated */
  482. ath9k_hw_set_interrupts(sc->sc_ah,
  483. sc->sc_imask &
  484. ~(ATH9K_INT_SWBA | ATH9K_INT_BMISS));
  485. sc->sc_imask &= ~(ATH9K_INT_SWBA | ATH9K_INT_BMISS);
  486. DPRINTF(sc, ATH_DBG_CONFIG,
  487. "%s: RX filter 0x%x bssid %s aid 0x%x\n",
  488. __func__, rfilt,
  489. print_mac(mac, sc->sc_curbssid), sc->sc_curaid);
  490. /* need to reconfigure the beacon */
  491. sc->sc_beacons = 0;
  492. break;
  493. default:
  494. break;
  495. }
  496. }
  497. if ((conf->changed & IEEE80211_IFCC_BEACON) &&
  498. (vif->type == IEEE80211_IF_TYPE_IBSS)) {
  499. /*
  500. * Allocate and setup the beacon frame.
  501. *
  502. * Stop any previous beacon DMA. This may be
  503. * necessary, for example, when an ibss merge
  504. * causes reconfiguration; we may be called
  505. * with beacon transmission active.
  506. */
  507. ath9k_hw_stoptxdma(sc->sc_ah, sc->sc_bhalq);
  508. error = ath_beacon_alloc(sc, 0);
  509. if (error != 0)
  510. return error;
  511. ath_beacon_sync(sc, 0);
  512. }
  513. /* Check for WLAN_CAPABILITY_PRIVACY ? */
  514. if ((avp->av_opmode != IEEE80211_IF_TYPE_STA)) {
  515. for (i = 0; i < IEEE80211_WEP_NKID; i++)
  516. if (ath9k_hw_keyisvalid(sc->sc_ah, (u16)i))
  517. ath9k_hw_keysetmac(sc->sc_ah,
  518. (u16)i,
  519. sc->sc_curbssid);
  520. }
  521. /* Only legacy IBSS for now */
  522. if (vif->type == IEEE80211_IF_TYPE_IBSS)
  523. ath_update_chainmask(sc, 0);
  524. return 0;
  525. }
  526. #define SUPPORTED_FILTERS \
  527. (FIF_PROMISC_IN_BSS | \
  528. FIF_ALLMULTI | \
  529. FIF_CONTROL | \
  530. FIF_OTHER_BSS | \
  531. FIF_BCN_PRBRESP_PROMISC | \
  532. FIF_FCSFAIL)
  533. /* Accept unicast, bcast and mcast frames */
  534. static void ath9k_configure_filter(struct ieee80211_hw *hw,
  535. unsigned int changed_flags,
  536. unsigned int *total_flags,
  537. int mc_count,
  538. struct dev_mc_list *mclist)
  539. {
  540. struct ath_softc *sc = hw->priv;
  541. changed_flags &= SUPPORTED_FILTERS;
  542. *total_flags &= SUPPORTED_FILTERS;
  543. if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
  544. if (*total_flags & FIF_BCN_PRBRESP_PROMISC)
  545. ath_scan_start(sc);
  546. else
  547. ath_scan_end(sc);
  548. }
  549. }
  550. static void ath9k_sta_notify(struct ieee80211_hw *hw,
  551. struct ieee80211_vif *vif,
  552. enum sta_notify_cmd cmd,
  553. const u8 *addr)
  554. {
  555. struct ath_softc *sc = hw->priv;
  556. struct ath_node *an;
  557. unsigned long flags;
  558. DECLARE_MAC_BUF(mac);
  559. spin_lock_irqsave(&sc->node_lock, flags);
  560. an = ath_node_find(sc, (u8 *) addr);
  561. spin_unlock_irqrestore(&sc->node_lock, flags);
  562. switch (cmd) {
  563. case STA_NOTIFY_ADD:
  564. spin_lock_irqsave(&sc->node_lock, flags);
  565. if (!an) {
  566. ath_node_attach(sc, (u8 *)addr, 0);
  567. DPRINTF(sc, ATH_DBG_CONFIG, "%s: Attach a node: %s\n",
  568. __func__,
  569. print_mac(mac, addr));
  570. } else {
  571. ath_node_get(sc, (u8 *)addr);
  572. }
  573. spin_unlock_irqrestore(&sc->node_lock, flags);
  574. break;
  575. case STA_NOTIFY_REMOVE:
  576. if (!an)
  577. DPRINTF(sc, ATH_DBG_FATAL,
  578. "%s: Removal of a non-existent node\n",
  579. __func__);
  580. else {
  581. ath_node_put(sc, an, ATH9K_BH_STATUS_INTACT);
  582. DPRINTF(sc, ATH_DBG_CONFIG, "%s: Put a node: %s\n",
  583. __func__,
  584. print_mac(mac, addr));
  585. }
  586. break;
  587. default:
  588. break;
  589. }
  590. }
  591. static int ath9k_conf_tx(struct ieee80211_hw *hw,
  592. u16 queue,
  593. const struct ieee80211_tx_queue_params *params)
  594. {
  595. struct ath_softc *sc = hw->priv;
  596. struct ath9k_txq_info qi;
  597. int ret = 0, qnum;
  598. if (queue >= WME_NUM_AC)
  599. return 0;
  600. qi.tqi_aifs = params->aifs;
  601. qi.tqi_cwmin = params->cw_min;
  602. qi.tqi_cwmax = params->cw_max;
  603. qi.tqi_burstTime = params->txop;
  604. qnum = ath_get_hal_qnum(queue, sc);
  605. DPRINTF(sc, ATH_DBG_CONFIG,
  606. "%s: Configure tx [queue/halq] [%d/%d], "
  607. "aifs: %d, cw_min: %d, cw_max: %d, txop: %d\n",
  608. __func__,
  609. queue,
  610. qnum,
  611. params->aifs,
  612. params->cw_min,
  613. params->cw_max,
  614. params->txop);
  615. ret = ath_txq_update(sc, qnum, &qi);
  616. if (ret)
  617. DPRINTF(sc, ATH_DBG_FATAL,
  618. "%s: TXQ Update failed\n", __func__);
  619. return ret;
  620. }
  621. static int ath9k_set_key(struct ieee80211_hw *hw,
  622. enum set_key_cmd cmd,
  623. const u8 *local_addr,
  624. const u8 *addr,
  625. struct ieee80211_key_conf *key)
  626. {
  627. struct ath_softc *sc = hw->priv;
  628. int ret = 0;
  629. DPRINTF(sc, ATH_DBG_KEYCACHE, " %s: Set HW Key\n", __func__);
  630. switch (cmd) {
  631. case SET_KEY:
  632. ret = ath_key_config(sc, addr, key);
  633. if (!ret) {
  634. set_bit(key->keyidx, sc->sc_keymap);
  635. key->hw_key_idx = key->keyidx;
  636. /* push IV and Michael MIC generation to stack */
  637. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  638. key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
  639. }
  640. break;
  641. case DISABLE_KEY:
  642. ath_key_delete(sc, key);
  643. clear_bit(key->keyidx, sc->sc_keymap);
  644. sc->sc_keytype = ATH9K_CIPHER_CLR;
  645. break;
  646. default:
  647. ret = -EINVAL;
  648. }
  649. return ret;
  650. }
  651. static void ath9k_ht_conf(struct ath_softc *sc,
  652. struct ieee80211_bss_conf *bss_conf)
  653. {
  654. #define IEEE80211_HT_CAP_40MHZ_INTOLERANT BIT(14)
  655. struct ath_ht_info *ht_info = &sc->sc_ht_info;
  656. if (bss_conf->assoc_ht) {
  657. ht_info->ext_chan_offset =
  658. bss_conf->ht_bss_conf->bss_cap &
  659. IEEE80211_HT_IE_CHA_SEC_OFFSET;
  660. if (!(bss_conf->ht_conf->cap &
  661. IEEE80211_HT_CAP_40MHZ_INTOLERANT) &&
  662. (bss_conf->ht_bss_conf->bss_cap &
  663. IEEE80211_HT_IE_CHA_WIDTH))
  664. ht_info->tx_chan_width = ATH9K_HT_MACMODE_2040;
  665. else
  666. ht_info->tx_chan_width = ATH9K_HT_MACMODE_20;
  667. ath9k_hw_set11nmac2040(sc->sc_ah, ht_info->tx_chan_width);
  668. ht_info->maxampdu = 1 << (IEEE80211_HTCAP_MAXRXAMPDU_FACTOR +
  669. bss_conf->ht_conf->ampdu_factor);
  670. ht_info->mpdudensity =
  671. parse_mpdudensity(bss_conf->ht_conf->ampdu_density);
  672. }
  673. #undef IEEE80211_HT_CAP_40MHZ_INTOLERANT
  674. }
  675. static void ath9k_bss_assoc_info(struct ath_softc *sc,
  676. struct ieee80211_bss_conf *bss_conf)
  677. {
  678. struct ieee80211_hw *hw = sc->hw;
  679. struct ieee80211_channel *curchan = hw->conf.channel;
  680. struct ath_vap *avp;
  681. int pos;
  682. DECLARE_MAC_BUF(mac);
  683. if (bss_conf->assoc) {
  684. DPRINTF(sc, ATH_DBG_CONFIG, "%s: Bss Info ASSOC %d\n",
  685. __func__,
  686. bss_conf->aid);
  687. avp = sc->sc_vaps[0];
  688. if (avp == NULL) {
  689. DPRINTF(sc, ATH_DBG_FATAL, "%s: Invalid interface\n",
  690. __func__);
  691. return;
  692. }
  693. /* New association, store aid */
  694. if (avp->av_opmode == ATH9K_M_STA) {
  695. sc->sc_curaid = bss_conf->aid;
  696. ath9k_hw_write_associd(sc->sc_ah, sc->sc_curbssid,
  697. sc->sc_curaid);
  698. }
  699. /* Configure the beacon */
  700. ath_beacon_config(sc, 0);
  701. sc->sc_beacons = 1;
  702. /* Reset rssi stats */
  703. sc->sc_halstats.ns_avgbrssi = ATH_RSSI_DUMMY_MARKER;
  704. sc->sc_halstats.ns_avgrssi = ATH_RSSI_DUMMY_MARKER;
  705. sc->sc_halstats.ns_avgtxrssi = ATH_RSSI_DUMMY_MARKER;
  706. sc->sc_halstats.ns_avgtxrate = ATH_RATE_DUMMY_MARKER;
  707. /* Update chainmask */
  708. ath_update_chainmask(sc, bss_conf->assoc_ht);
  709. DPRINTF(sc, ATH_DBG_CONFIG,
  710. "%s: bssid %s aid 0x%x\n",
  711. __func__,
  712. print_mac(mac, sc->sc_curbssid), sc->sc_curaid);
  713. DPRINTF(sc, ATH_DBG_CONFIG, "%s: Set channel: %d MHz\n",
  714. __func__,
  715. curchan->center_freq);
  716. pos = ath_get_channel(sc, curchan);
  717. if (pos == -1) {
  718. DPRINTF(sc, ATH_DBG_FATAL,
  719. "%s: Invalid channel\n", __func__);
  720. return;
  721. }
  722. if (hw->conf.ht_conf.ht_supported)
  723. sc->sc_ah->ah_channels[pos].chanmode =
  724. ath_get_extchanmode(sc, curchan);
  725. else
  726. sc->sc_ah->ah_channels[pos].chanmode =
  727. (curchan->band == IEEE80211_BAND_2GHZ) ?
  728. CHANNEL_G : CHANNEL_A;
  729. /* set h/w channel */
  730. if (ath_set_channel(sc, &sc->sc_ah->ah_channels[pos]) < 0)
  731. DPRINTF(sc, ATH_DBG_FATAL,
  732. "%s: Unable to set channel\n",
  733. __func__);
  734. ath_rate_newstate(sc, avp);
  735. /* Update ratectrl about the new state */
  736. ath_rc_node_update(hw, avp->rc_node);
  737. } else {
  738. DPRINTF(sc, ATH_DBG_CONFIG,
  739. "%s: Bss Info DISSOC\n", __func__);
  740. sc->sc_curaid = 0;
  741. }
  742. }
  743. static void ath9k_bss_info_changed(struct ieee80211_hw *hw,
  744. struct ieee80211_vif *vif,
  745. struct ieee80211_bss_conf *bss_conf,
  746. u32 changed)
  747. {
  748. struct ath_softc *sc = hw->priv;
  749. if (changed & BSS_CHANGED_ERP_PREAMBLE) {
  750. DPRINTF(sc, ATH_DBG_CONFIG, "%s: BSS Changed PREAMBLE %d\n",
  751. __func__,
  752. bss_conf->use_short_preamble);
  753. if (bss_conf->use_short_preamble)
  754. sc->sc_flags |= ATH_PREAMBLE_SHORT;
  755. else
  756. sc->sc_flags &= ~ATH_PREAMBLE_SHORT;
  757. }
  758. if (changed & BSS_CHANGED_ERP_CTS_PROT) {
  759. DPRINTF(sc, ATH_DBG_CONFIG, "%s: BSS Changed CTS PROT %d\n",
  760. __func__,
  761. bss_conf->use_cts_prot);
  762. if (bss_conf->use_cts_prot &&
  763. hw->conf.channel->band != IEEE80211_BAND_5GHZ)
  764. sc->sc_flags |= ATH_PROTECT_ENABLE;
  765. else
  766. sc->sc_flags &= ~ATH_PROTECT_ENABLE;
  767. }
  768. if (changed & BSS_CHANGED_HT) {
  769. DPRINTF(sc, ATH_DBG_CONFIG, "%s: BSS Changed HT %d\n",
  770. __func__,
  771. bss_conf->assoc_ht);
  772. ath9k_ht_conf(sc, bss_conf);
  773. }
  774. if (changed & BSS_CHANGED_ASSOC) {
  775. DPRINTF(sc, ATH_DBG_CONFIG, "%s: BSS Changed ASSOC %d\n",
  776. __func__,
  777. bss_conf->assoc);
  778. ath9k_bss_assoc_info(sc, bss_conf);
  779. }
  780. }
  781. static u64 ath9k_get_tsf(struct ieee80211_hw *hw)
  782. {
  783. u64 tsf;
  784. struct ath_softc *sc = hw->priv;
  785. struct ath_hal *ah = sc->sc_ah;
  786. tsf = ath9k_hw_gettsf64(ah);
  787. return tsf;
  788. }
  789. static void ath9k_reset_tsf(struct ieee80211_hw *hw)
  790. {
  791. struct ath_softc *sc = hw->priv;
  792. struct ath_hal *ah = sc->sc_ah;
  793. ath9k_hw_reset_tsf(ah);
  794. }
  795. static int ath9k_ampdu_action(struct ieee80211_hw *hw,
  796. enum ieee80211_ampdu_mlme_action action,
  797. const u8 *addr,
  798. u16 tid,
  799. u16 *ssn)
  800. {
  801. struct ath_softc *sc = hw->priv;
  802. int ret = 0;
  803. switch (action) {
  804. case IEEE80211_AMPDU_RX_START:
  805. ret = ath_rx_aggr_start(sc, addr, tid, ssn);
  806. if (ret < 0)
  807. DPRINTF(sc, ATH_DBG_FATAL,
  808. "%s: Unable to start RX aggregation\n",
  809. __func__);
  810. break;
  811. case IEEE80211_AMPDU_RX_STOP:
  812. ret = ath_rx_aggr_stop(sc, addr, tid);
  813. if (ret < 0)
  814. DPRINTF(sc, ATH_DBG_FATAL,
  815. "%s: Unable to stop RX aggregation\n",
  816. __func__);
  817. break;
  818. case IEEE80211_AMPDU_TX_START:
  819. ret = ath_tx_aggr_start(sc, addr, tid, ssn);
  820. if (ret < 0)
  821. DPRINTF(sc, ATH_DBG_FATAL,
  822. "%s: Unable to start TX aggregation\n",
  823. __func__);
  824. else
  825. ieee80211_start_tx_ba_cb_irqsafe(hw, (u8 *)addr, tid);
  826. break;
  827. case IEEE80211_AMPDU_TX_STOP:
  828. ret = ath_tx_aggr_stop(sc, addr, tid);
  829. if (ret < 0)
  830. DPRINTF(sc, ATH_DBG_FATAL,
  831. "%s: Unable to stop TX aggregation\n",
  832. __func__);
  833. ieee80211_stop_tx_ba_cb_irqsafe(hw, (u8 *)addr, tid);
  834. break;
  835. default:
  836. DPRINTF(sc, ATH_DBG_FATAL,
  837. "%s: Unknown AMPDU action\n", __func__);
  838. }
  839. return ret;
  840. }
  841. static struct ieee80211_ops ath9k_ops = {
  842. .tx = ath9k_tx,
  843. .start = ath9k_start,
  844. .stop = ath9k_stop,
  845. .add_interface = ath9k_add_interface,
  846. .remove_interface = ath9k_remove_interface,
  847. .config = ath9k_config,
  848. .config_interface = ath9k_config_interface,
  849. .configure_filter = ath9k_configure_filter,
  850. .get_stats = NULL,
  851. .sta_notify = ath9k_sta_notify,
  852. .conf_tx = ath9k_conf_tx,
  853. .get_tx_stats = NULL,
  854. .bss_info_changed = ath9k_bss_info_changed,
  855. .set_tim = NULL,
  856. .set_key = ath9k_set_key,
  857. .hw_scan = NULL,
  858. .get_tkip_seq = NULL,
  859. .set_rts_threshold = NULL,
  860. .set_frag_threshold = NULL,
  861. .set_retry_limit = NULL,
  862. .get_tsf = ath9k_get_tsf,
  863. .reset_tsf = ath9k_reset_tsf,
  864. .tx_last_beacon = NULL,
  865. .ampdu_action = ath9k_ampdu_action
  866. };
  867. void ath_get_beaconconfig(struct ath_softc *sc,
  868. int if_id,
  869. struct ath_beacon_config *conf)
  870. {
  871. struct ieee80211_hw *hw = sc->hw;
  872. /* fill in beacon config data */
  873. conf->beacon_interval = hw->conf.beacon_int;
  874. conf->listen_interval = 100;
  875. conf->dtim_count = 1;
  876. conf->bmiss_timeout = ATH_DEFAULT_BMISS_LIMIT * conf->listen_interval;
  877. }
  878. int ath_update_beacon(struct ath_softc *sc,
  879. int if_id,
  880. struct ath_beacon_offset *bo,
  881. struct sk_buff *skb,
  882. int mcast)
  883. {
  884. return 0;
  885. }
  886. void ath_tx_complete(struct ath_softc *sc, struct sk_buff *skb,
  887. struct ath_xmit_status *tx_status, struct ath_node *an)
  888. {
  889. struct ieee80211_hw *hw = sc->hw;
  890. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
  891. DPRINTF(sc, ATH_DBG_XMIT,
  892. "%s: TX complete: skb: %p\n", __func__, skb);
  893. if (tx_info->flags & IEEE80211_TX_CTL_NO_ACK ||
  894. tx_info->flags & IEEE80211_TX_STAT_TX_FILTERED) {
  895. /* free driver's private data area of tx_info */
  896. if (tx_info->driver_data[0] != NULL)
  897. kfree(tx_info->driver_data[0]);
  898. tx_info->driver_data[0] = NULL;
  899. }
  900. if (tx_status->flags & ATH_TX_BAR) {
  901. tx_info->flags |= IEEE80211_TX_STAT_AMPDU_NO_BACK;
  902. tx_status->flags &= ~ATH_TX_BAR;
  903. }
  904. if (tx_status->flags)
  905. tx_info->status.excessive_retries = 1;
  906. tx_info->status.retry_count = tx_status->retries;
  907. ieee80211_tx_status(hw, skb);
  908. if (an)
  909. ath_node_put(sc, an, ATH9K_BH_STATUS_CHANGE);
  910. }
  911. int ath__rx_indicate(struct ath_softc *sc,
  912. struct sk_buff *skb,
  913. struct ath_recv_status *status,
  914. u16 keyix)
  915. {
  916. struct ieee80211_hw *hw = sc->hw;
  917. struct ath_node *an = NULL;
  918. struct ieee80211_rx_status rx_status;
  919. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  920. int hdrlen = ieee80211_get_hdrlen_from_skb(skb);
  921. int padsize;
  922. enum ATH_RX_TYPE st;
  923. /* see if any padding is done by the hw and remove it */
  924. if (hdrlen & 3) {
  925. padsize = hdrlen % 4;
  926. memmove(skb->data + padsize, skb->data, hdrlen);
  927. skb_pull(skb, padsize);
  928. }
  929. /* remove FCS before passing up to protocol stack */
  930. skb_trim(skb, (skb->len - FCS_LEN));
  931. /* Prepare rx status */
  932. ath9k_rx_prepare(sc, skb, status, &rx_status);
  933. if (!(keyix == ATH9K_RXKEYIX_INVALID) &&
  934. !(status->flags & ATH_RX_DECRYPT_ERROR)) {
  935. rx_status.flag |= RX_FLAG_DECRYPTED;
  936. } else if ((le16_to_cpu(hdr->frame_control) & IEEE80211_FCTL_PROTECTED)
  937. && !(status->flags & ATH_RX_DECRYPT_ERROR)
  938. && skb->len >= hdrlen + 4) {
  939. keyix = skb->data[hdrlen + 3] >> 6;
  940. if (test_bit(keyix, sc->sc_keymap))
  941. rx_status.flag |= RX_FLAG_DECRYPTED;
  942. }
  943. spin_lock_bh(&sc->node_lock);
  944. an = ath_node_find(sc, hdr->addr2);
  945. spin_unlock_bh(&sc->node_lock);
  946. if (an) {
  947. ath_rx_input(sc, an,
  948. hw->conf.ht_conf.ht_supported,
  949. skb, status, &st);
  950. }
  951. if (!an || (st != ATH_RX_CONSUMED))
  952. __ieee80211_rx(hw, skb, &rx_status);
  953. return 0;
  954. }
  955. int ath_rx_subframe(struct ath_node *an,
  956. struct sk_buff *skb,
  957. struct ath_recv_status *status)
  958. {
  959. struct ath_softc *sc = an->an_sc;
  960. struct ieee80211_hw *hw = sc->hw;
  961. struct ieee80211_rx_status rx_status;
  962. /* Prepare rx status */
  963. ath9k_rx_prepare(sc, skb, status, &rx_status);
  964. if (!(status->flags & ATH_RX_DECRYPT_ERROR))
  965. rx_status.flag |= RX_FLAG_DECRYPTED;
  966. __ieee80211_rx(hw, skb, &rx_status);
  967. return 0;
  968. }
  969. enum ath9k_ht_macmode ath_cwm_macmode(struct ath_softc *sc)
  970. {
  971. return sc->sc_ht_info.tx_chan_width;
  972. }
  973. void ath_setup_rate(struct ath_softc *sc,
  974. enum wireless_mode wMode,
  975. enum RATE_TYPE type,
  976. const struct ath9k_rate_table *rt)
  977. {
  978. int i, maxrates, a = 0, b = 0;
  979. struct ieee80211_supported_band *band_2ghz;
  980. struct ieee80211_supported_band *band_5ghz;
  981. struct ieee80211_rate *rates_2ghz;
  982. struct ieee80211_rate *rates_5ghz;
  983. if ((wMode >= WIRELESS_MODE_MAX) || (type != NORMAL_RATE))
  984. return;
  985. band_2ghz = &sc->sbands[IEEE80211_BAND_2GHZ];
  986. band_5ghz = &sc->sbands[IEEE80211_BAND_5GHZ];
  987. rates_2ghz = sc->rates[IEEE80211_BAND_2GHZ];
  988. rates_5ghz = sc->rates[IEEE80211_BAND_5GHZ];
  989. if (rt->rateCount > ATH_RATE_MAX)
  990. maxrates = ATH_RATE_MAX;
  991. else
  992. maxrates = rt->rateCount;
  993. if ((band_2ghz->n_bitrates != 0) && (band_5ghz->n_bitrates != 0)) {
  994. DPRINTF(sc, ATH_DBG_CONFIG,
  995. "%s: Rates already setup\n", __func__);
  996. return;
  997. }
  998. for (i = 0; i < maxrates; i++) {
  999. switch (wMode) {
  1000. case WIRELESS_MODE_11b:
  1001. case WIRELESS_MODE_11g:
  1002. rates_2ghz[a].bitrate = rt->info[i].rateKbps / 100;
  1003. rates_2ghz[a].hw_value = rt->info[i].rateCode;
  1004. a++;
  1005. band_2ghz->n_bitrates = a;
  1006. break;
  1007. case WIRELESS_MODE_11a:
  1008. rates_5ghz[b].bitrate = rt->info[i].rateKbps / 100;
  1009. rates_5ghz[b].hw_value = rt->info[i].rateCode;
  1010. b++;
  1011. band_5ghz->n_bitrates = b;
  1012. break;
  1013. default:
  1014. break;
  1015. }
  1016. }
  1017. if (band_2ghz->n_bitrates) {
  1018. for (i = 0; i < band_2ghz->n_bitrates; i++) {
  1019. DPRINTF(sc, ATH_DBG_CONFIG,
  1020. "%s: 2GHz Rate: %2dMbps, ratecode: %2d\n",
  1021. __func__,
  1022. rates_2ghz[i].bitrate / 10,
  1023. rates_2ghz[i].hw_value);
  1024. }
  1025. } else if (band_5ghz->n_bitrates) {
  1026. for (i = 0; i < band_5ghz->n_bitrates; i++) {
  1027. DPRINTF(sc, ATH_DBG_CONFIG,
  1028. "%s: 5Ghz Rate: %2dMbps, ratecode: %2d\n",
  1029. __func__,
  1030. rates_5ghz[i].bitrate / 10,
  1031. rates_5ghz[i].hw_value);
  1032. }
  1033. }
  1034. }
  1035. static int ath_detach(struct ath_softc *sc)
  1036. {
  1037. struct ieee80211_hw *hw = sc->hw;
  1038. DPRINTF(sc, ATH_DBG_CONFIG, "%s: Detach ATH hw\n", __func__);
  1039. /* Unregister hw */
  1040. ieee80211_unregister_hw(hw);
  1041. /* unregister Rate control */
  1042. ath_rate_control_unregister();
  1043. /* tx/rx cleanup */
  1044. ath_rx_cleanup(sc);
  1045. ath_tx_cleanup(sc);
  1046. /* Deinit */
  1047. ath_deinit(sc);
  1048. return 0;
  1049. }
  1050. static int ath_attach(u16 devid,
  1051. struct ath_softc *sc)
  1052. {
  1053. struct ieee80211_hw *hw = sc->hw;
  1054. int error = 0;
  1055. DPRINTF(sc, ATH_DBG_CONFIG, "%s: Attach ATH hw\n", __func__);
  1056. error = ath_init(devid, sc);
  1057. if (error != 0)
  1058. return error;
  1059. /* Init nodes */
  1060. INIT_LIST_HEAD(&sc->node_list);
  1061. spin_lock_init(&sc->node_lock);
  1062. /* get mac address from hardware and set in mac80211 */
  1063. SET_IEEE80211_PERM_ADDR(hw, sc->sc_myaddr);
  1064. /* setup channels and rates */
  1065. sc->sbands[IEEE80211_BAND_2GHZ].channels =
  1066. sc->channels[IEEE80211_BAND_2GHZ];
  1067. sc->sbands[IEEE80211_BAND_2GHZ].bitrates =
  1068. sc->rates[IEEE80211_BAND_2GHZ];
  1069. sc->sbands[IEEE80211_BAND_2GHZ].band = IEEE80211_BAND_2GHZ;
  1070. if (sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_HT)
  1071. /* Setup HT capabilities for 2.4Ghz*/
  1072. setup_ht_cap(&sc->sbands[IEEE80211_BAND_2GHZ].ht_info);
  1073. hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
  1074. &sc->sbands[IEEE80211_BAND_2GHZ];
  1075. if (sc->sc_ah->ah_caps.wireless_modes & ATH9K_MODE_SEL_11A) {
  1076. sc->sbands[IEEE80211_BAND_5GHZ].channels =
  1077. sc->channels[IEEE80211_BAND_5GHZ];
  1078. sc->sbands[IEEE80211_BAND_5GHZ].bitrates =
  1079. sc->rates[IEEE80211_BAND_5GHZ];
  1080. sc->sbands[IEEE80211_BAND_5GHZ].band =
  1081. IEEE80211_BAND_5GHZ;
  1082. if (sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_HT)
  1083. /* Setup HT capabilities for 5Ghz*/
  1084. setup_ht_cap(&sc->sbands[IEEE80211_BAND_5GHZ].ht_info);
  1085. hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
  1086. &sc->sbands[IEEE80211_BAND_5GHZ];
  1087. }
  1088. /* FIXME: Have to figure out proper hw init values later */
  1089. hw->queues = 4;
  1090. hw->ampdu_queues = 1;
  1091. /* Register rate control */
  1092. hw->rate_control_algorithm = "ath9k_rate_control";
  1093. error = ath_rate_control_register();
  1094. if (error != 0) {
  1095. DPRINTF(sc, ATH_DBG_FATAL,
  1096. "%s: Unable to register rate control "
  1097. "algorithm:%d\n", __func__, error);
  1098. ath_rate_control_unregister();
  1099. goto bad;
  1100. }
  1101. error = ieee80211_register_hw(hw);
  1102. if (error != 0) {
  1103. ath_rate_control_unregister();
  1104. goto bad;
  1105. }
  1106. /* initialize tx/rx engine */
  1107. error = ath_tx_init(sc, ATH_TXBUF);
  1108. if (error != 0)
  1109. goto bad1;
  1110. error = ath_rx_init(sc, ATH_RXBUF);
  1111. if (error != 0)
  1112. goto bad1;
  1113. return 0;
  1114. bad1:
  1115. ath_detach(sc);
  1116. bad:
  1117. return error;
  1118. }
  1119. static int ath_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  1120. {
  1121. void __iomem *mem;
  1122. struct ath_softc *sc;
  1123. struct ieee80211_hw *hw;
  1124. const char *athname;
  1125. u8 csz;
  1126. u32 val;
  1127. int ret = 0;
  1128. if (pci_enable_device(pdev))
  1129. return -EIO;
  1130. /* XXX 32-bit addressing only */
  1131. if (pci_set_dma_mask(pdev, 0xffffffff)) {
  1132. printk(KERN_ERR "ath_pci: 32-bit DMA not available\n");
  1133. ret = -ENODEV;
  1134. goto bad;
  1135. }
  1136. /*
  1137. * Cache line size is used to size and align various
  1138. * structures used to communicate with the hardware.
  1139. */
  1140. pci_read_config_byte(pdev, PCI_CACHE_LINE_SIZE, &csz);
  1141. if (csz == 0) {
  1142. /*
  1143. * Linux 2.4.18 (at least) writes the cache line size
  1144. * register as a 16-bit wide register which is wrong.
  1145. * We must have this setup properly for rx buffer
  1146. * DMA to work so force a reasonable value here if it
  1147. * comes up zero.
  1148. */
  1149. csz = L1_CACHE_BYTES / sizeof(u32);
  1150. pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, csz);
  1151. }
  1152. /*
  1153. * The default setting of latency timer yields poor results,
  1154. * set it to the value used by other systems. It may be worth
  1155. * tweaking this setting more.
  1156. */
  1157. pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0xa8);
  1158. pci_set_master(pdev);
  1159. /*
  1160. * Disable the RETRY_TIMEOUT register (0x41) to keep
  1161. * PCI Tx retries from interfering with C3 CPU state.
  1162. */
  1163. pci_read_config_dword(pdev, 0x40, &val);
  1164. if ((val & 0x0000ff00) != 0)
  1165. pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
  1166. ret = pci_request_region(pdev, 0, "ath9k");
  1167. if (ret) {
  1168. dev_err(&pdev->dev, "PCI memory region reserve error\n");
  1169. ret = -ENODEV;
  1170. goto bad;
  1171. }
  1172. mem = pci_iomap(pdev, 0, 0);
  1173. if (!mem) {
  1174. printk(KERN_ERR "PCI memory map error\n") ;
  1175. ret = -EIO;
  1176. goto bad1;
  1177. }
  1178. hw = ieee80211_alloc_hw(sizeof(struct ath_softc), &ath9k_ops);
  1179. if (hw == NULL) {
  1180. printk(KERN_ERR "ath_pci: no memory for ieee80211_hw\n");
  1181. goto bad2;
  1182. }
  1183. hw->flags = IEEE80211_HW_SIGNAL_DBM |
  1184. IEEE80211_HW_NOISE_DBM;
  1185. SET_IEEE80211_DEV(hw, &pdev->dev);
  1186. pci_set_drvdata(pdev, hw);
  1187. sc = hw->priv;
  1188. sc->hw = hw;
  1189. sc->pdev = pdev;
  1190. sc->mem = mem;
  1191. if (ath_attach(id->device, sc) != 0) {
  1192. ret = -ENODEV;
  1193. goto bad3;
  1194. }
  1195. /* setup interrupt service routine */
  1196. if (request_irq(pdev->irq, ath_isr, IRQF_SHARED, "ath", sc)) {
  1197. printk(KERN_ERR "%s: request_irq failed\n",
  1198. wiphy_name(hw->wiphy));
  1199. ret = -EIO;
  1200. goto bad4;
  1201. }
  1202. athname = ath9k_hw_probe(id->vendor, id->device);
  1203. printk(KERN_INFO "%s: %s: mem=0x%lx, irq=%d\n",
  1204. wiphy_name(hw->wiphy),
  1205. athname ? athname : "Atheros ???",
  1206. (unsigned long)mem, pdev->irq);
  1207. return 0;
  1208. bad4:
  1209. ath_detach(sc);
  1210. bad3:
  1211. ieee80211_free_hw(hw);
  1212. bad2:
  1213. pci_iounmap(pdev, mem);
  1214. bad1:
  1215. pci_release_region(pdev, 0);
  1216. bad:
  1217. pci_disable_device(pdev);
  1218. return ret;
  1219. }
  1220. static void ath_pci_remove(struct pci_dev *pdev)
  1221. {
  1222. struct ieee80211_hw *hw = pci_get_drvdata(pdev);
  1223. struct ath_softc *sc = hw->priv;
  1224. if (pdev->irq)
  1225. free_irq(pdev->irq, sc);
  1226. ath_detach(sc);
  1227. pci_iounmap(pdev, sc->mem);
  1228. pci_release_region(pdev, 0);
  1229. pci_disable_device(pdev);
  1230. ieee80211_free_hw(hw);
  1231. }
  1232. #ifdef CONFIG_PM
  1233. static int ath_pci_suspend(struct pci_dev *pdev, pm_message_t state)
  1234. {
  1235. pci_save_state(pdev);
  1236. pci_disable_device(pdev);
  1237. pci_set_power_state(pdev, 3);
  1238. return 0;
  1239. }
  1240. static int ath_pci_resume(struct pci_dev *pdev)
  1241. {
  1242. u32 val;
  1243. int err;
  1244. err = pci_enable_device(pdev);
  1245. if (err)
  1246. return err;
  1247. pci_restore_state(pdev);
  1248. /*
  1249. * Suspend/Resume resets the PCI configuration space, so we have to
  1250. * re-disable the RETRY_TIMEOUT register (0x41) to keep
  1251. * PCI Tx retries from interfering with C3 CPU state
  1252. */
  1253. pci_read_config_dword(pdev, 0x40, &val);
  1254. if ((val & 0x0000ff00) != 0)
  1255. pci_write_config_dword(pdev, 0x40, val & 0xffff00ff);
  1256. return 0;
  1257. }
  1258. #endif /* CONFIG_PM */
  1259. MODULE_DEVICE_TABLE(pci, ath_pci_id_table);
  1260. static struct pci_driver ath_pci_driver = {
  1261. .name = "ath9k",
  1262. .id_table = ath_pci_id_table,
  1263. .probe = ath_pci_probe,
  1264. .remove = ath_pci_remove,
  1265. #ifdef CONFIG_PM
  1266. .suspend = ath_pci_suspend,
  1267. .resume = ath_pci_resume,
  1268. #endif /* CONFIG_PM */
  1269. };
  1270. static int __init init_ath_pci(void)
  1271. {
  1272. printk(KERN_INFO "%s: %s\n", dev_info, ATH_PCI_VERSION);
  1273. if (pci_register_driver(&ath_pci_driver) < 0) {
  1274. printk(KERN_ERR
  1275. "ath_pci: No devices found, driver not installed.\n");
  1276. pci_unregister_driver(&ath_pci_driver);
  1277. return -ENODEV;
  1278. }
  1279. return 0;
  1280. }
  1281. module_init(init_ath_pci);
  1282. static void __exit exit_ath_pci(void)
  1283. {
  1284. pci_unregister_driver(&ath_pci_driver);
  1285. printk(KERN_INFO "%s: driver unloaded\n", dev_info);
  1286. }
  1287. module_exit(exit_ath_pci);