main.c 36 KB

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