main.c 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634
  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. #include <linux/nl80211.h>
  17. #include "core.h"
  18. #include "reg.h"
  19. #include "hw.h"
  20. #define ATH_PCI_VERSION "0.1"
  21. static char *dev_info = "ath9k";
  22. MODULE_AUTHOR("Atheros Communications");
  23. MODULE_DESCRIPTION("Support for Atheros 802.11n wireless LAN cards.");
  24. MODULE_SUPPORTED_DEVICE("Atheros 802.11n WLAN cards");
  25. MODULE_LICENSE("Dual BSD/GPL");
  26. /* We use the hw_value as an index into our private channel structure */
  27. #define CHAN2G(_freq, _idx) { \
  28. .center_freq = (_freq), \
  29. .hw_value = (_idx), \
  30. .max_power = 30, \
  31. }
  32. #define CHAN5G(_freq, _idx) { \
  33. .band = IEEE80211_BAND_5GHZ, \
  34. .center_freq = (_freq), \
  35. .hw_value = (_idx), \
  36. .max_power = 30, \
  37. }
  38. /* Some 2 GHz radios are actually tunable on 2312-2732
  39. * on 5 MHz steps, we support the channels which we know
  40. * we have calibration data for all cards though to make
  41. * this static */
  42. static struct ieee80211_channel ath9k_2ghz_chantable[] = {
  43. CHAN2G(2412, 0), /* Channel 1 */
  44. CHAN2G(2417, 1), /* Channel 2 */
  45. CHAN2G(2422, 2), /* Channel 3 */
  46. CHAN2G(2427, 3), /* Channel 4 */
  47. CHAN2G(2432, 4), /* Channel 5 */
  48. CHAN2G(2437, 5), /* Channel 6 */
  49. CHAN2G(2442, 6), /* Channel 7 */
  50. CHAN2G(2447, 7), /* Channel 8 */
  51. CHAN2G(2452, 8), /* Channel 9 */
  52. CHAN2G(2457, 9), /* Channel 10 */
  53. CHAN2G(2462, 10), /* Channel 11 */
  54. CHAN2G(2467, 11), /* Channel 12 */
  55. CHAN2G(2472, 12), /* Channel 13 */
  56. CHAN2G(2484, 13), /* Channel 14 */
  57. };
  58. /* Some 5 GHz radios are actually tunable on XXXX-YYYY
  59. * on 5 MHz steps, we support the channels which we know
  60. * we have calibration data for all cards though to make
  61. * this static */
  62. static struct ieee80211_channel ath9k_5ghz_chantable[] = {
  63. /* _We_ call this UNII 1 */
  64. CHAN5G(5180, 14), /* Channel 36 */
  65. CHAN5G(5200, 15), /* Channel 40 */
  66. CHAN5G(5220, 16), /* Channel 44 */
  67. CHAN5G(5240, 17), /* Channel 48 */
  68. /* _We_ call this UNII 2 */
  69. CHAN5G(5260, 18), /* Channel 52 */
  70. CHAN5G(5280, 19), /* Channel 56 */
  71. CHAN5G(5300, 20), /* Channel 60 */
  72. CHAN5G(5320, 21), /* Channel 64 */
  73. /* _We_ call this "Middle band" */
  74. CHAN5G(5500, 22), /* Channel 100 */
  75. CHAN5G(5520, 23), /* Channel 104 */
  76. CHAN5G(5540, 24), /* Channel 108 */
  77. CHAN5G(5560, 25), /* Channel 112 */
  78. CHAN5G(5580, 26), /* Channel 116 */
  79. CHAN5G(5600, 27), /* Channel 120 */
  80. CHAN5G(5620, 28), /* Channel 124 */
  81. CHAN5G(5640, 29), /* Channel 128 */
  82. CHAN5G(5660, 30), /* Channel 132 */
  83. CHAN5G(5680, 31), /* Channel 136 */
  84. CHAN5G(5700, 32), /* Channel 140 */
  85. /* _We_ call this UNII 3 */
  86. CHAN5G(5745, 33), /* Channel 149 */
  87. CHAN5G(5765, 34), /* Channel 153 */
  88. CHAN5G(5785, 35), /* Channel 157 */
  89. CHAN5G(5805, 36), /* Channel 161 */
  90. CHAN5G(5825, 37), /* Channel 165 */
  91. };
  92. static void ath_cache_conf_rate(struct ath_softc *sc,
  93. struct ieee80211_conf *conf)
  94. {
  95. switch (conf->channel->band) {
  96. case IEEE80211_BAND_2GHZ:
  97. if (conf_is_ht20(conf))
  98. sc->cur_rate_table =
  99. sc->hw_rate_table[ATH9K_MODE_11NG_HT20];
  100. else if (conf_is_ht40_minus(conf))
  101. sc->cur_rate_table =
  102. sc->hw_rate_table[ATH9K_MODE_11NG_HT40MINUS];
  103. else if (conf_is_ht40_plus(conf))
  104. sc->cur_rate_table =
  105. sc->hw_rate_table[ATH9K_MODE_11NG_HT40PLUS];
  106. else
  107. sc->cur_rate_table =
  108. sc->hw_rate_table[ATH9K_MODE_11G];
  109. break;
  110. case IEEE80211_BAND_5GHZ:
  111. if (conf_is_ht20(conf))
  112. sc->cur_rate_table =
  113. sc->hw_rate_table[ATH9K_MODE_11NA_HT20];
  114. else if (conf_is_ht40_minus(conf))
  115. sc->cur_rate_table =
  116. sc->hw_rate_table[ATH9K_MODE_11NA_HT40MINUS];
  117. else if (conf_is_ht40_plus(conf))
  118. sc->cur_rate_table =
  119. sc->hw_rate_table[ATH9K_MODE_11NA_HT40PLUS];
  120. else
  121. sc->cur_rate_table =
  122. sc->hw_rate_table[ATH9K_MODE_11A];
  123. break;
  124. default:
  125. BUG_ON(1);
  126. break;
  127. }
  128. }
  129. static void ath_update_txpow(struct ath_softc *sc)
  130. {
  131. struct ath_hal *ah = sc->sc_ah;
  132. u32 txpow;
  133. if (sc->sc_curtxpow != sc->sc_config.txpowlimit) {
  134. ath9k_hw_set_txpowerlimit(ah, sc->sc_config.txpowlimit);
  135. /* read back in case value is clamped */
  136. ath9k_hw_getcapability(ah, ATH9K_CAP_TXPOW, 1, &txpow);
  137. sc->sc_curtxpow = txpow;
  138. }
  139. }
  140. static u8 parse_mpdudensity(u8 mpdudensity)
  141. {
  142. /*
  143. * 802.11n D2.0 defined values for "Minimum MPDU Start Spacing":
  144. * 0 for no restriction
  145. * 1 for 1/4 us
  146. * 2 for 1/2 us
  147. * 3 for 1 us
  148. * 4 for 2 us
  149. * 5 for 4 us
  150. * 6 for 8 us
  151. * 7 for 16 us
  152. */
  153. switch (mpdudensity) {
  154. case 0:
  155. return 0;
  156. case 1:
  157. case 2:
  158. case 3:
  159. /* Our lower layer calculations limit our precision to
  160. 1 microsecond */
  161. return 1;
  162. case 4:
  163. return 2;
  164. case 5:
  165. return 4;
  166. case 6:
  167. return 8;
  168. case 7:
  169. return 16;
  170. default:
  171. return 0;
  172. }
  173. }
  174. static void ath_setup_rates(struct ath_softc *sc, enum ieee80211_band band)
  175. {
  176. struct ath_rate_table *rate_table = NULL;
  177. struct ieee80211_supported_band *sband;
  178. struct ieee80211_rate *rate;
  179. int i, maxrates;
  180. switch (band) {
  181. case IEEE80211_BAND_2GHZ:
  182. rate_table = sc->hw_rate_table[ATH9K_MODE_11G];
  183. break;
  184. case IEEE80211_BAND_5GHZ:
  185. rate_table = sc->hw_rate_table[ATH9K_MODE_11A];
  186. break;
  187. default:
  188. break;
  189. }
  190. if (rate_table == NULL)
  191. return;
  192. sband = &sc->sbands[band];
  193. rate = sc->rates[band];
  194. if (rate_table->rate_cnt > ATH_RATE_MAX)
  195. maxrates = ATH_RATE_MAX;
  196. else
  197. maxrates = rate_table->rate_cnt;
  198. for (i = 0; i < maxrates; i++) {
  199. rate[i].bitrate = rate_table->info[i].ratekbps / 100;
  200. rate[i].hw_value = rate_table->info[i].ratecode;
  201. sband->n_bitrates++;
  202. DPRINTF(sc, ATH_DBG_CONFIG, "Rate: %2dMbps, ratecode: %2d\n",
  203. rate[i].bitrate / 10, rate[i].hw_value);
  204. }
  205. }
  206. /*
  207. * Set/change channels. If the channel is really being changed, it's done
  208. * by reseting the chip. To accomplish this we must first cleanup any pending
  209. * DMA, then restart stuff.
  210. */
  211. static int ath_set_channel(struct ath_softc *sc, struct ath9k_channel *hchan)
  212. {
  213. struct ath_hal *ah = sc->sc_ah;
  214. bool fastcc = true, stopped;
  215. struct ieee80211_hw *hw = sc->hw;
  216. struct ieee80211_channel *channel = hw->conf.channel;
  217. int r;
  218. if (sc->sc_flags & SC_OP_INVALID)
  219. return -EIO;
  220. ath9k_ps_wakeup(sc);
  221. /*
  222. * This is only performed if the channel settings have
  223. * actually changed.
  224. *
  225. * To switch channels clear any pending DMA operations;
  226. * wait long enough for the RX fifo to drain, reset the
  227. * hardware at the new frequency, and then re-enable
  228. * the relevant bits of the h/w.
  229. */
  230. ath9k_hw_set_interrupts(ah, 0);
  231. ath_drain_all_txq(sc, false);
  232. stopped = ath_stoprecv(sc);
  233. /* XXX: do not flush receive queue here. We don't want
  234. * to flush data frames already in queue because of
  235. * changing channel. */
  236. if (!stopped || (sc->sc_flags & SC_OP_FULL_RESET))
  237. fastcc = false;
  238. DPRINTF(sc, ATH_DBG_CONFIG,
  239. "(%u MHz) -> (%u MHz), chanwidth: %d\n",
  240. sc->sc_ah->ah_curchan->channel,
  241. channel->center_freq, sc->tx_chan_width);
  242. spin_lock_bh(&sc->sc_resetlock);
  243. r = ath9k_hw_reset(ah, hchan, fastcc);
  244. if (r) {
  245. DPRINTF(sc, ATH_DBG_FATAL,
  246. "Unable to reset channel (%u Mhz) "
  247. "reset status %u\n",
  248. channel->center_freq, r);
  249. spin_unlock_bh(&sc->sc_resetlock);
  250. return r;
  251. }
  252. spin_unlock_bh(&sc->sc_resetlock);
  253. sc->sc_flags &= ~SC_OP_CHAINMASK_UPDATE;
  254. sc->sc_flags &= ~SC_OP_FULL_RESET;
  255. if (ath_startrecv(sc) != 0) {
  256. DPRINTF(sc, ATH_DBG_FATAL,
  257. "Unable to restart recv logic\n");
  258. return -EIO;
  259. }
  260. ath_cache_conf_rate(sc, &hw->conf);
  261. ath_update_txpow(sc);
  262. ath9k_hw_set_interrupts(ah, sc->sc_imask);
  263. ath9k_ps_restore(sc);
  264. return 0;
  265. }
  266. /*
  267. * This routine performs the periodic noise floor calibration function
  268. * that is used to adjust and optimize the chip performance. This
  269. * takes environmental changes (location, temperature) into account.
  270. * When the task is complete, it reschedules itself depending on the
  271. * appropriate interval that was calculated.
  272. */
  273. static void ath_ani_calibrate(unsigned long data)
  274. {
  275. struct ath_softc *sc;
  276. struct ath_hal *ah;
  277. bool longcal = false;
  278. bool shortcal = false;
  279. bool aniflag = false;
  280. unsigned int timestamp = jiffies_to_msecs(jiffies);
  281. u32 cal_interval;
  282. sc = (struct ath_softc *)data;
  283. ah = sc->sc_ah;
  284. /*
  285. * don't calibrate when we're scanning.
  286. * we are most likely not on our home channel.
  287. */
  288. if (sc->rx.rxfilter & FIF_BCN_PRBRESP_PROMISC)
  289. return;
  290. /* Long calibration runs independently of short calibration. */
  291. if ((timestamp - sc->sc_ani.sc_longcal_timer) >= ATH_LONG_CALINTERVAL) {
  292. longcal = true;
  293. DPRINTF(sc, ATH_DBG_ANI, "longcal @%lu\n", jiffies);
  294. sc->sc_ani.sc_longcal_timer = timestamp;
  295. }
  296. /* Short calibration applies only while sc_caldone is false */
  297. if (!sc->sc_ani.sc_caldone) {
  298. if ((timestamp - sc->sc_ani.sc_shortcal_timer) >=
  299. ATH_SHORT_CALINTERVAL) {
  300. shortcal = true;
  301. DPRINTF(sc, ATH_DBG_ANI, "shortcal @%lu\n", jiffies);
  302. sc->sc_ani.sc_shortcal_timer = timestamp;
  303. sc->sc_ani.sc_resetcal_timer = timestamp;
  304. }
  305. } else {
  306. if ((timestamp - sc->sc_ani.sc_resetcal_timer) >=
  307. ATH_RESTART_CALINTERVAL) {
  308. sc->sc_ani.sc_caldone = ath9k_hw_reset_calvalid(ah);
  309. if (sc->sc_ani.sc_caldone)
  310. sc->sc_ani.sc_resetcal_timer = timestamp;
  311. }
  312. }
  313. /* Verify whether we must check ANI */
  314. if ((timestamp - sc->sc_ani.sc_checkani_timer) >=
  315. ATH_ANI_POLLINTERVAL) {
  316. aniflag = true;
  317. sc->sc_ani.sc_checkani_timer = timestamp;
  318. }
  319. /* Skip all processing if there's nothing to do. */
  320. if (longcal || shortcal || aniflag) {
  321. /* Call ANI routine if necessary */
  322. if (aniflag)
  323. ath9k_hw_ani_monitor(ah, &sc->sc_halstats,
  324. ah->ah_curchan);
  325. /* Perform calibration if necessary */
  326. if (longcal || shortcal) {
  327. bool iscaldone = false;
  328. if (ath9k_hw_calibrate(ah, ah->ah_curchan,
  329. sc->sc_rx_chainmask, longcal,
  330. &iscaldone)) {
  331. if (longcal)
  332. sc->sc_ani.sc_noise_floor =
  333. ath9k_hw_getchan_noise(ah,
  334. ah->ah_curchan);
  335. DPRINTF(sc, ATH_DBG_ANI,
  336. "calibrate chan %u/%x nf: %d\n",
  337. ah->ah_curchan->channel,
  338. ah->ah_curchan->channelFlags,
  339. sc->sc_ani.sc_noise_floor);
  340. } else {
  341. DPRINTF(sc, ATH_DBG_ANY,
  342. "calibrate chan %u/%x failed\n",
  343. ah->ah_curchan->channel,
  344. ah->ah_curchan->channelFlags);
  345. }
  346. sc->sc_ani.sc_caldone = iscaldone;
  347. }
  348. }
  349. /*
  350. * Set timer interval based on previous results.
  351. * The interval must be the shortest necessary to satisfy ANI,
  352. * short calibration and long calibration.
  353. */
  354. cal_interval = ATH_LONG_CALINTERVAL;
  355. if (sc->sc_ah->ah_config.enable_ani)
  356. cal_interval = min(cal_interval, (u32)ATH_ANI_POLLINTERVAL);
  357. if (!sc->sc_ani.sc_caldone)
  358. cal_interval = min(cal_interval, (u32)ATH_SHORT_CALINTERVAL);
  359. mod_timer(&sc->sc_ani.timer, jiffies + msecs_to_jiffies(cal_interval));
  360. }
  361. /*
  362. * Update tx/rx chainmask. For legacy association,
  363. * hard code chainmask to 1x1, for 11n association, use
  364. * the chainmask configuration, for bt coexistence, use
  365. * the chainmask configuration even in legacy mode.
  366. */
  367. static void ath_update_chainmask(struct ath_softc *sc, int is_ht)
  368. {
  369. sc->sc_flags |= SC_OP_CHAINMASK_UPDATE;
  370. if (is_ht ||
  371. (sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_BT_COEX)) {
  372. sc->sc_tx_chainmask = sc->sc_ah->ah_caps.tx_chainmask;
  373. sc->sc_rx_chainmask = sc->sc_ah->ah_caps.rx_chainmask;
  374. } else {
  375. sc->sc_tx_chainmask = 1;
  376. sc->sc_rx_chainmask = 1;
  377. }
  378. DPRINTF(sc, ATH_DBG_CONFIG, "tx chmask: %d, rx chmask: %d\n",
  379. sc->sc_tx_chainmask, sc->sc_rx_chainmask);
  380. }
  381. static void ath_node_attach(struct ath_softc *sc, struct ieee80211_sta *sta)
  382. {
  383. struct ath_node *an;
  384. an = (struct ath_node *)sta->drv_priv;
  385. if (sc->sc_flags & SC_OP_TXAGGR)
  386. ath_tx_node_init(sc, an);
  387. an->maxampdu = 1 << (IEEE80211_HTCAP_MAXRXAMPDU_FACTOR +
  388. sta->ht_cap.ampdu_factor);
  389. an->mpdudensity = parse_mpdudensity(sta->ht_cap.ampdu_density);
  390. }
  391. static void ath_node_detach(struct ath_softc *sc, struct ieee80211_sta *sta)
  392. {
  393. struct ath_node *an = (struct ath_node *)sta->drv_priv;
  394. if (sc->sc_flags & SC_OP_TXAGGR)
  395. ath_tx_node_cleanup(sc, an);
  396. }
  397. static void ath9k_tasklet(unsigned long data)
  398. {
  399. struct ath_softc *sc = (struct ath_softc *)data;
  400. u32 status = sc->sc_intrstatus;
  401. if (status & ATH9K_INT_FATAL) {
  402. /* need a chip reset */
  403. ath_reset(sc, false);
  404. return;
  405. } else {
  406. if (status &
  407. (ATH9K_INT_RX | ATH9K_INT_RXEOL | ATH9K_INT_RXORN)) {
  408. spin_lock_bh(&sc->rx.rxflushlock);
  409. ath_rx_tasklet(sc, 0);
  410. spin_unlock_bh(&sc->rx.rxflushlock);
  411. }
  412. /* XXX: optimize this */
  413. if (status & ATH9K_INT_TX)
  414. ath_tx_tasklet(sc);
  415. }
  416. /* re-enable hardware interrupt */
  417. ath9k_hw_set_interrupts(sc->sc_ah, sc->sc_imask);
  418. }
  419. irqreturn_t ath_isr(int irq, void *dev)
  420. {
  421. struct ath_softc *sc = dev;
  422. struct ath_hal *ah = sc->sc_ah;
  423. enum ath9k_int status;
  424. bool sched = false;
  425. do {
  426. if (sc->sc_flags & SC_OP_INVALID) {
  427. /*
  428. * The hardware is not ready/present, don't
  429. * touch anything. Note this can happen early
  430. * on if the IRQ is shared.
  431. */
  432. return IRQ_NONE;
  433. }
  434. if (!ath9k_hw_intrpend(ah)) { /* shared irq, not for us */
  435. return IRQ_NONE;
  436. }
  437. /*
  438. * Figure out the reason(s) for the interrupt. Note
  439. * that the hal returns a pseudo-ISR that may include
  440. * bits we haven't explicitly enabled so we mask the
  441. * value to insure we only process bits we requested.
  442. */
  443. ath9k_hw_getisr(ah, &status); /* NB: clears ISR too */
  444. status &= sc->sc_imask; /* discard unasked-for bits */
  445. /*
  446. * If there are no status bits set, then this interrupt was not
  447. * for me (should have been caught above).
  448. */
  449. if (!status)
  450. return IRQ_NONE;
  451. sc->sc_intrstatus = status;
  452. if (status & ATH9K_INT_FATAL) {
  453. /* need a chip reset */
  454. sched = true;
  455. } else if (status & ATH9K_INT_RXORN) {
  456. /* need a chip reset */
  457. sched = true;
  458. } else {
  459. if (status & ATH9K_INT_SWBA) {
  460. /* schedule a tasklet for beacon handling */
  461. tasklet_schedule(&sc->bcon_tasklet);
  462. }
  463. if (status & ATH9K_INT_RXEOL) {
  464. /*
  465. * NB: the hardware should re-read the link when
  466. * RXE bit is written, but it doesn't work
  467. * at least on older hardware revs.
  468. */
  469. sched = true;
  470. }
  471. if (status & ATH9K_INT_TXURN)
  472. /* bump tx trigger level */
  473. ath9k_hw_updatetxtriglevel(ah, true);
  474. /* XXX: optimize this */
  475. if (status & ATH9K_INT_RX)
  476. sched = true;
  477. if (status & ATH9K_INT_TX)
  478. sched = true;
  479. if (status & ATH9K_INT_BMISS)
  480. sched = true;
  481. /* carrier sense timeout */
  482. if (status & ATH9K_INT_CST)
  483. sched = true;
  484. if (status & ATH9K_INT_MIB) {
  485. /*
  486. * Disable interrupts until we service the MIB
  487. * interrupt; otherwise it will continue to
  488. * fire.
  489. */
  490. ath9k_hw_set_interrupts(ah, 0);
  491. /*
  492. * Let the hal handle the event. We assume
  493. * it will clear whatever condition caused
  494. * the interrupt.
  495. */
  496. ath9k_hw_procmibevent(ah, &sc->sc_halstats);
  497. ath9k_hw_set_interrupts(ah, sc->sc_imask);
  498. }
  499. if (status & ATH9K_INT_TIM_TIMER) {
  500. if (!(ah->ah_caps.hw_caps &
  501. ATH9K_HW_CAP_AUTOSLEEP)) {
  502. /* Clear RxAbort bit so that we can
  503. * receive frames */
  504. ath9k_hw_setpower(ah, ATH9K_PM_AWAKE);
  505. ath9k_hw_setrxabort(ah, 0);
  506. sched = true;
  507. sc->sc_flags |= SC_OP_WAIT_FOR_BEACON;
  508. }
  509. }
  510. }
  511. } while (0);
  512. ath_debug_stat_interrupt(sc, status);
  513. if (sched) {
  514. /* turn off every interrupt except SWBA */
  515. ath9k_hw_set_interrupts(ah, (sc->sc_imask & ATH9K_INT_SWBA));
  516. tasklet_schedule(&sc->intr_tq);
  517. }
  518. return IRQ_HANDLED;
  519. }
  520. static u32 ath_get_extchanmode(struct ath_softc *sc,
  521. struct ieee80211_channel *chan,
  522. enum nl80211_channel_type channel_type)
  523. {
  524. u32 chanmode = 0;
  525. switch (chan->band) {
  526. case IEEE80211_BAND_2GHZ:
  527. switch(channel_type) {
  528. case NL80211_CHAN_NO_HT:
  529. case NL80211_CHAN_HT20:
  530. chanmode = CHANNEL_G_HT20;
  531. break;
  532. case NL80211_CHAN_HT40PLUS:
  533. chanmode = CHANNEL_G_HT40PLUS;
  534. break;
  535. case NL80211_CHAN_HT40MINUS:
  536. chanmode = CHANNEL_G_HT40MINUS;
  537. break;
  538. }
  539. break;
  540. case IEEE80211_BAND_5GHZ:
  541. switch(channel_type) {
  542. case NL80211_CHAN_NO_HT:
  543. case NL80211_CHAN_HT20:
  544. chanmode = CHANNEL_A_HT20;
  545. break;
  546. case NL80211_CHAN_HT40PLUS:
  547. chanmode = CHANNEL_A_HT40PLUS;
  548. break;
  549. case NL80211_CHAN_HT40MINUS:
  550. chanmode = CHANNEL_A_HT40MINUS;
  551. break;
  552. }
  553. break;
  554. default:
  555. break;
  556. }
  557. return chanmode;
  558. }
  559. static int ath_keyset(struct ath_softc *sc, u16 keyix,
  560. struct ath9k_keyval *hk, const u8 mac[ETH_ALEN])
  561. {
  562. bool status;
  563. status = ath9k_hw_set_keycache_entry(sc->sc_ah,
  564. keyix, hk, mac, false);
  565. return status != false;
  566. }
  567. static int ath_setkey_tkip(struct ath_softc *sc, u16 keyix, const u8 *key,
  568. struct ath9k_keyval *hk,
  569. const u8 *addr)
  570. {
  571. const u8 *key_rxmic;
  572. const u8 *key_txmic;
  573. key_txmic = key + NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY;
  574. key_rxmic = key + NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY;
  575. if (addr == NULL) {
  576. /* Group key installation */
  577. memcpy(hk->kv_mic, key_rxmic, sizeof(hk->kv_mic));
  578. return ath_keyset(sc, keyix, hk, addr);
  579. }
  580. if (!sc->sc_splitmic) {
  581. /*
  582. * data key goes at first index,
  583. * the hal handles the MIC keys at index+64.
  584. */
  585. memcpy(hk->kv_mic, key_rxmic, sizeof(hk->kv_mic));
  586. memcpy(hk->kv_txmic, key_txmic, sizeof(hk->kv_txmic));
  587. return ath_keyset(sc, keyix, hk, addr);
  588. }
  589. /*
  590. * TX key goes at first index, RX key at +32.
  591. * The hal handles the MIC keys at index+64.
  592. */
  593. memcpy(hk->kv_mic, key_txmic, sizeof(hk->kv_mic));
  594. if (!ath_keyset(sc, keyix, hk, NULL)) {
  595. /* Txmic entry failed. No need to proceed further */
  596. DPRINTF(sc, ATH_DBG_KEYCACHE,
  597. "Setting TX MIC Key Failed\n");
  598. return 0;
  599. }
  600. memcpy(hk->kv_mic, key_rxmic, sizeof(hk->kv_mic));
  601. /* XXX delete tx key on failure? */
  602. return ath_keyset(sc, keyix + 32, hk, addr);
  603. }
  604. static int ath_reserve_key_cache_slot_tkip(struct ath_softc *sc)
  605. {
  606. int i;
  607. for (i = IEEE80211_WEP_NKID; i < sc->sc_keymax / 2; i++) {
  608. if (test_bit(i, sc->sc_keymap) ||
  609. test_bit(i + 64, sc->sc_keymap))
  610. continue; /* At least one part of TKIP key allocated */
  611. if (sc->sc_splitmic &&
  612. (test_bit(i + 32, sc->sc_keymap) ||
  613. test_bit(i + 64 + 32, sc->sc_keymap)))
  614. continue; /* At least one part of TKIP key allocated */
  615. /* Found a free slot for a TKIP key */
  616. return i;
  617. }
  618. return -1;
  619. }
  620. static int ath_reserve_key_cache_slot(struct ath_softc *sc)
  621. {
  622. int i;
  623. /* First, try to find slots that would not be available for TKIP. */
  624. if (sc->sc_splitmic) {
  625. for (i = IEEE80211_WEP_NKID; i < sc->sc_keymax / 4; i++) {
  626. if (!test_bit(i, sc->sc_keymap) &&
  627. (test_bit(i + 32, sc->sc_keymap) ||
  628. test_bit(i + 64, sc->sc_keymap) ||
  629. test_bit(i + 64 + 32, sc->sc_keymap)))
  630. return i;
  631. if (!test_bit(i + 32, sc->sc_keymap) &&
  632. (test_bit(i, sc->sc_keymap) ||
  633. test_bit(i + 64, sc->sc_keymap) ||
  634. test_bit(i + 64 + 32, sc->sc_keymap)))
  635. return i + 32;
  636. if (!test_bit(i + 64, sc->sc_keymap) &&
  637. (test_bit(i , sc->sc_keymap) ||
  638. test_bit(i + 32, sc->sc_keymap) ||
  639. test_bit(i + 64 + 32, sc->sc_keymap)))
  640. return i + 64;
  641. if (!test_bit(i + 64 + 32, sc->sc_keymap) &&
  642. (test_bit(i, sc->sc_keymap) ||
  643. test_bit(i + 32, sc->sc_keymap) ||
  644. test_bit(i + 64, sc->sc_keymap)))
  645. return i + 64 + 32;
  646. }
  647. } else {
  648. for (i = IEEE80211_WEP_NKID; i < sc->sc_keymax / 2; i++) {
  649. if (!test_bit(i, sc->sc_keymap) &&
  650. test_bit(i + 64, sc->sc_keymap))
  651. return i;
  652. if (test_bit(i, sc->sc_keymap) &&
  653. !test_bit(i + 64, sc->sc_keymap))
  654. return i + 64;
  655. }
  656. }
  657. /* No partially used TKIP slots, pick any available slot */
  658. for (i = IEEE80211_WEP_NKID; i < sc->sc_keymax; i++) {
  659. /* Do not allow slots that could be needed for TKIP group keys
  660. * to be used. This limitation could be removed if we know that
  661. * TKIP will not be used. */
  662. if (i >= 64 && i < 64 + IEEE80211_WEP_NKID)
  663. continue;
  664. if (sc->sc_splitmic) {
  665. if (i >= 32 && i < 32 + IEEE80211_WEP_NKID)
  666. continue;
  667. if (i >= 64 + 32 && i < 64 + 32 + IEEE80211_WEP_NKID)
  668. continue;
  669. }
  670. if (!test_bit(i, sc->sc_keymap))
  671. return i; /* Found a free slot for a key */
  672. }
  673. /* No free slot found */
  674. return -1;
  675. }
  676. static int ath_key_config(struct ath_softc *sc,
  677. struct ieee80211_sta *sta,
  678. struct ieee80211_key_conf *key)
  679. {
  680. struct ath9k_keyval hk;
  681. const u8 *mac = NULL;
  682. int ret = 0;
  683. int idx;
  684. memset(&hk, 0, sizeof(hk));
  685. switch (key->alg) {
  686. case ALG_WEP:
  687. hk.kv_type = ATH9K_CIPHER_WEP;
  688. break;
  689. case ALG_TKIP:
  690. hk.kv_type = ATH9K_CIPHER_TKIP;
  691. break;
  692. case ALG_CCMP:
  693. hk.kv_type = ATH9K_CIPHER_AES_CCM;
  694. break;
  695. default:
  696. return -EOPNOTSUPP;
  697. }
  698. hk.kv_len = key->keylen;
  699. memcpy(hk.kv_val, key->key, key->keylen);
  700. if (!(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
  701. /* For now, use the default keys for broadcast keys. This may
  702. * need to change with virtual interfaces. */
  703. idx = key->keyidx;
  704. } else if (key->keyidx) {
  705. struct ieee80211_vif *vif;
  706. if (WARN_ON(!sta))
  707. return -EOPNOTSUPP;
  708. mac = sta->addr;
  709. vif = sc->sc_vaps[0];
  710. if (vif->type != NL80211_IFTYPE_AP) {
  711. /* Only keyidx 0 should be used with unicast key, but
  712. * allow this for client mode for now. */
  713. idx = key->keyidx;
  714. } else
  715. return -EIO;
  716. } else {
  717. if (WARN_ON(!sta))
  718. return -EOPNOTSUPP;
  719. mac = sta->addr;
  720. if (key->alg == ALG_TKIP)
  721. idx = ath_reserve_key_cache_slot_tkip(sc);
  722. else
  723. idx = ath_reserve_key_cache_slot(sc);
  724. if (idx < 0)
  725. return -ENOSPC; /* no free key cache entries */
  726. }
  727. if (key->alg == ALG_TKIP)
  728. ret = ath_setkey_tkip(sc, idx, key->key, &hk, mac);
  729. else
  730. ret = ath_keyset(sc, idx, &hk, mac);
  731. if (!ret)
  732. return -EIO;
  733. set_bit(idx, sc->sc_keymap);
  734. if (key->alg == ALG_TKIP) {
  735. set_bit(idx + 64, sc->sc_keymap);
  736. if (sc->sc_splitmic) {
  737. set_bit(idx + 32, sc->sc_keymap);
  738. set_bit(idx + 64 + 32, sc->sc_keymap);
  739. }
  740. }
  741. return idx;
  742. }
  743. static void ath_key_delete(struct ath_softc *sc, struct ieee80211_key_conf *key)
  744. {
  745. ath9k_hw_keyreset(sc->sc_ah, key->hw_key_idx);
  746. if (key->hw_key_idx < IEEE80211_WEP_NKID)
  747. return;
  748. clear_bit(key->hw_key_idx, sc->sc_keymap);
  749. if (key->alg != ALG_TKIP)
  750. return;
  751. clear_bit(key->hw_key_idx + 64, sc->sc_keymap);
  752. if (sc->sc_splitmic) {
  753. clear_bit(key->hw_key_idx + 32, sc->sc_keymap);
  754. clear_bit(key->hw_key_idx + 64 + 32, sc->sc_keymap);
  755. }
  756. }
  757. static void setup_ht_cap(struct ath_softc *sc,
  758. struct ieee80211_sta_ht_cap *ht_info)
  759. {
  760. #define ATH9K_HT_CAP_MAXRXAMPDU_65536 0x3 /* 2 ^ 16 */
  761. #define ATH9K_HT_CAP_MPDUDENSITY_8 0x6 /* 8 usec */
  762. ht_info->ht_supported = true;
  763. ht_info->cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
  764. IEEE80211_HT_CAP_SM_PS |
  765. IEEE80211_HT_CAP_SGI_40 |
  766. IEEE80211_HT_CAP_DSSSCCK40;
  767. ht_info->ampdu_factor = ATH9K_HT_CAP_MAXRXAMPDU_65536;
  768. ht_info->ampdu_density = ATH9K_HT_CAP_MPDUDENSITY_8;
  769. /* set up supported mcs set */
  770. memset(&ht_info->mcs, 0, sizeof(ht_info->mcs));
  771. switch(sc->sc_rx_chainmask) {
  772. case 1:
  773. ht_info->mcs.rx_mask[0] = 0xff;
  774. break;
  775. case 5:
  776. case 7:
  777. default:
  778. ht_info->mcs.rx_mask[0] = 0xff;
  779. ht_info->mcs.rx_mask[1] = 0xff;
  780. break;
  781. }
  782. ht_info->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
  783. }
  784. static void ath9k_bss_assoc_info(struct ath_softc *sc,
  785. struct ieee80211_vif *vif,
  786. struct ieee80211_bss_conf *bss_conf)
  787. {
  788. struct ath_vap *avp = (void *)vif->drv_priv;
  789. if (bss_conf->assoc) {
  790. DPRINTF(sc, ATH_DBG_CONFIG, "Bss Info ASSOC %d, bssid: %pM\n",
  791. bss_conf->aid, sc->sc_curbssid);
  792. /* New association, store aid */
  793. if (avp->av_opmode == NL80211_IFTYPE_STATION) {
  794. sc->sc_curaid = bss_conf->aid;
  795. ath9k_hw_write_associd(sc->sc_ah, sc->sc_curbssid,
  796. sc->sc_curaid);
  797. }
  798. /* Configure the beacon */
  799. ath_beacon_config(sc, 0);
  800. sc->sc_flags |= SC_OP_BEACONS;
  801. /* Reset rssi stats */
  802. sc->sc_halstats.ns_avgbrssi = ATH_RSSI_DUMMY_MARKER;
  803. sc->sc_halstats.ns_avgrssi = ATH_RSSI_DUMMY_MARKER;
  804. sc->sc_halstats.ns_avgtxrssi = ATH_RSSI_DUMMY_MARKER;
  805. sc->sc_halstats.ns_avgtxrate = ATH_RATE_DUMMY_MARKER;
  806. /* Start ANI */
  807. mod_timer(&sc->sc_ani.timer,
  808. jiffies + msecs_to_jiffies(ATH_ANI_POLLINTERVAL));
  809. } else {
  810. DPRINTF(sc, ATH_DBG_CONFIG, "Bss Info DISSOC\n");
  811. sc->sc_curaid = 0;
  812. }
  813. }
  814. /********************************/
  815. /* LED functions */
  816. /********************************/
  817. static void ath_led_brightness(struct led_classdev *led_cdev,
  818. enum led_brightness brightness)
  819. {
  820. struct ath_led *led = container_of(led_cdev, struct ath_led, led_cdev);
  821. struct ath_softc *sc = led->sc;
  822. switch (brightness) {
  823. case LED_OFF:
  824. if (led->led_type == ATH_LED_ASSOC ||
  825. led->led_type == ATH_LED_RADIO)
  826. sc->sc_flags &= ~SC_OP_LED_ASSOCIATED;
  827. ath9k_hw_set_gpio(sc->sc_ah, ATH_LED_PIN,
  828. (led->led_type == ATH_LED_RADIO) ? 1 :
  829. !!(sc->sc_flags & SC_OP_LED_ASSOCIATED));
  830. break;
  831. case LED_FULL:
  832. if (led->led_type == ATH_LED_ASSOC)
  833. sc->sc_flags |= SC_OP_LED_ASSOCIATED;
  834. ath9k_hw_set_gpio(sc->sc_ah, ATH_LED_PIN, 0);
  835. break;
  836. default:
  837. break;
  838. }
  839. }
  840. static int ath_register_led(struct ath_softc *sc, struct ath_led *led,
  841. char *trigger)
  842. {
  843. int ret;
  844. led->sc = sc;
  845. led->led_cdev.name = led->name;
  846. led->led_cdev.default_trigger = trigger;
  847. led->led_cdev.brightness_set = ath_led_brightness;
  848. ret = led_classdev_register(wiphy_dev(sc->hw->wiphy), &led->led_cdev);
  849. if (ret)
  850. DPRINTF(sc, ATH_DBG_FATAL,
  851. "Failed to register led:%s", led->name);
  852. else
  853. led->registered = 1;
  854. return ret;
  855. }
  856. static void ath_unregister_led(struct ath_led *led)
  857. {
  858. if (led->registered) {
  859. led_classdev_unregister(&led->led_cdev);
  860. led->registered = 0;
  861. }
  862. }
  863. static void ath_deinit_leds(struct ath_softc *sc)
  864. {
  865. ath_unregister_led(&sc->assoc_led);
  866. sc->sc_flags &= ~SC_OP_LED_ASSOCIATED;
  867. ath_unregister_led(&sc->tx_led);
  868. ath_unregister_led(&sc->rx_led);
  869. ath_unregister_led(&sc->radio_led);
  870. ath9k_hw_set_gpio(sc->sc_ah, ATH_LED_PIN, 1);
  871. }
  872. static void ath_init_leds(struct ath_softc *sc)
  873. {
  874. char *trigger;
  875. int ret;
  876. /* Configure gpio 1 for output */
  877. ath9k_hw_cfg_output(sc->sc_ah, ATH_LED_PIN,
  878. AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
  879. /* LED off, active low */
  880. ath9k_hw_set_gpio(sc->sc_ah, ATH_LED_PIN, 1);
  881. trigger = ieee80211_get_radio_led_name(sc->hw);
  882. snprintf(sc->radio_led.name, sizeof(sc->radio_led.name),
  883. "ath9k-%s:radio", wiphy_name(sc->hw->wiphy));
  884. ret = ath_register_led(sc, &sc->radio_led, trigger);
  885. sc->radio_led.led_type = ATH_LED_RADIO;
  886. if (ret)
  887. goto fail;
  888. trigger = ieee80211_get_assoc_led_name(sc->hw);
  889. snprintf(sc->assoc_led.name, sizeof(sc->assoc_led.name),
  890. "ath9k-%s:assoc", wiphy_name(sc->hw->wiphy));
  891. ret = ath_register_led(sc, &sc->assoc_led, trigger);
  892. sc->assoc_led.led_type = ATH_LED_ASSOC;
  893. if (ret)
  894. goto fail;
  895. trigger = ieee80211_get_tx_led_name(sc->hw);
  896. snprintf(sc->tx_led.name, sizeof(sc->tx_led.name),
  897. "ath9k-%s:tx", wiphy_name(sc->hw->wiphy));
  898. ret = ath_register_led(sc, &sc->tx_led, trigger);
  899. sc->tx_led.led_type = ATH_LED_TX;
  900. if (ret)
  901. goto fail;
  902. trigger = ieee80211_get_rx_led_name(sc->hw);
  903. snprintf(sc->rx_led.name, sizeof(sc->rx_led.name),
  904. "ath9k-%s:rx", wiphy_name(sc->hw->wiphy));
  905. ret = ath_register_led(sc, &sc->rx_led, trigger);
  906. sc->rx_led.led_type = ATH_LED_RX;
  907. if (ret)
  908. goto fail;
  909. return;
  910. fail:
  911. ath_deinit_leds(sc);
  912. }
  913. #if defined(CONFIG_RFKILL) || defined(CONFIG_RFKILL_MODULE)
  914. /*******************/
  915. /* Rfkill */
  916. /*******************/
  917. static void ath_radio_enable(struct ath_softc *sc)
  918. {
  919. struct ath_hal *ah = sc->sc_ah;
  920. struct ieee80211_channel *channel = sc->hw->conf.channel;
  921. int r;
  922. ath9k_ps_wakeup(sc);
  923. spin_lock_bh(&sc->sc_resetlock);
  924. r = ath9k_hw_reset(ah, ah->ah_curchan, false);
  925. if (r) {
  926. DPRINTF(sc, ATH_DBG_FATAL,
  927. "Unable to reset channel %u (%uMhz) ",
  928. "reset status %u\n",
  929. channel->center_freq, r);
  930. }
  931. spin_unlock_bh(&sc->sc_resetlock);
  932. ath_update_txpow(sc);
  933. if (ath_startrecv(sc) != 0) {
  934. DPRINTF(sc, ATH_DBG_FATAL,
  935. "Unable to restart recv logic\n");
  936. return;
  937. }
  938. if (sc->sc_flags & SC_OP_BEACONS)
  939. ath_beacon_config(sc, ATH_IF_ID_ANY); /* restart beacons */
  940. /* Re-Enable interrupts */
  941. ath9k_hw_set_interrupts(ah, sc->sc_imask);
  942. /* Enable LED */
  943. ath9k_hw_cfg_output(ah, ATH_LED_PIN,
  944. AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
  945. ath9k_hw_set_gpio(ah, ATH_LED_PIN, 0);
  946. ieee80211_wake_queues(sc->hw);
  947. ath9k_ps_restore(sc);
  948. }
  949. static void ath_radio_disable(struct ath_softc *sc)
  950. {
  951. struct ath_hal *ah = sc->sc_ah;
  952. struct ieee80211_channel *channel = sc->hw->conf.channel;
  953. int r;
  954. ath9k_ps_wakeup(sc);
  955. ieee80211_stop_queues(sc->hw);
  956. /* Disable LED */
  957. ath9k_hw_set_gpio(ah, ATH_LED_PIN, 1);
  958. ath9k_hw_cfg_gpio_input(ah, ATH_LED_PIN);
  959. /* Disable interrupts */
  960. ath9k_hw_set_interrupts(ah, 0);
  961. ath_drain_all_txq(sc, false); /* clear pending tx frames */
  962. ath_stoprecv(sc); /* turn off frame recv */
  963. ath_flushrecv(sc); /* flush recv queue */
  964. spin_lock_bh(&sc->sc_resetlock);
  965. r = ath9k_hw_reset(ah, ah->ah_curchan, false);
  966. if (r) {
  967. DPRINTF(sc, ATH_DBG_FATAL,
  968. "Unable to reset channel %u (%uMhz) "
  969. "reset status %u\n",
  970. channel->center_freq, r);
  971. }
  972. spin_unlock_bh(&sc->sc_resetlock);
  973. ath9k_hw_phy_disable(ah);
  974. ath9k_hw_setpower(ah, ATH9K_PM_FULL_SLEEP);
  975. ath9k_ps_restore(sc);
  976. }
  977. static bool ath_is_rfkill_set(struct ath_softc *sc)
  978. {
  979. struct ath_hal *ah = sc->sc_ah;
  980. return ath9k_hw_gpio_get(ah, ah->ah_rfkill_gpio) ==
  981. ah->ah_rfkill_polarity;
  982. }
  983. /* h/w rfkill poll function */
  984. static void ath_rfkill_poll(struct work_struct *work)
  985. {
  986. struct ath_softc *sc = container_of(work, struct ath_softc,
  987. rf_kill.rfkill_poll.work);
  988. bool radio_on;
  989. if (sc->sc_flags & SC_OP_INVALID)
  990. return;
  991. radio_on = !ath_is_rfkill_set(sc);
  992. /*
  993. * enable/disable radio only when there is a
  994. * state change in RF switch
  995. */
  996. if (radio_on == !!(sc->sc_flags & SC_OP_RFKILL_HW_BLOCKED)) {
  997. enum rfkill_state state;
  998. if (sc->sc_flags & SC_OP_RFKILL_SW_BLOCKED) {
  999. state = radio_on ? RFKILL_STATE_SOFT_BLOCKED
  1000. : RFKILL_STATE_HARD_BLOCKED;
  1001. } else if (radio_on) {
  1002. ath_radio_enable(sc);
  1003. state = RFKILL_STATE_UNBLOCKED;
  1004. } else {
  1005. ath_radio_disable(sc);
  1006. state = RFKILL_STATE_HARD_BLOCKED;
  1007. }
  1008. if (state == RFKILL_STATE_HARD_BLOCKED)
  1009. sc->sc_flags |= SC_OP_RFKILL_HW_BLOCKED;
  1010. else
  1011. sc->sc_flags &= ~SC_OP_RFKILL_HW_BLOCKED;
  1012. rfkill_force_state(sc->rf_kill.rfkill, state);
  1013. }
  1014. queue_delayed_work(sc->hw->workqueue, &sc->rf_kill.rfkill_poll,
  1015. msecs_to_jiffies(ATH_RFKILL_POLL_INTERVAL));
  1016. }
  1017. /* s/w rfkill handler */
  1018. static int ath_sw_toggle_radio(void *data, enum rfkill_state state)
  1019. {
  1020. struct ath_softc *sc = data;
  1021. switch (state) {
  1022. case RFKILL_STATE_SOFT_BLOCKED:
  1023. if (!(sc->sc_flags & (SC_OP_RFKILL_HW_BLOCKED |
  1024. SC_OP_RFKILL_SW_BLOCKED)))
  1025. ath_radio_disable(sc);
  1026. sc->sc_flags |= SC_OP_RFKILL_SW_BLOCKED;
  1027. return 0;
  1028. case RFKILL_STATE_UNBLOCKED:
  1029. if ((sc->sc_flags & SC_OP_RFKILL_SW_BLOCKED)) {
  1030. sc->sc_flags &= ~SC_OP_RFKILL_SW_BLOCKED;
  1031. if (sc->sc_flags & SC_OP_RFKILL_HW_BLOCKED) {
  1032. DPRINTF(sc, ATH_DBG_FATAL, "Can't turn on the"
  1033. "radio as it is disabled by h/w\n");
  1034. return -EPERM;
  1035. }
  1036. ath_radio_enable(sc);
  1037. }
  1038. return 0;
  1039. default:
  1040. return -EINVAL;
  1041. }
  1042. }
  1043. /* Init s/w rfkill */
  1044. static int ath_init_sw_rfkill(struct ath_softc *sc)
  1045. {
  1046. sc->rf_kill.rfkill = rfkill_allocate(wiphy_dev(sc->hw->wiphy),
  1047. RFKILL_TYPE_WLAN);
  1048. if (!sc->rf_kill.rfkill) {
  1049. DPRINTF(sc, ATH_DBG_FATAL, "Failed to allocate rfkill\n");
  1050. return -ENOMEM;
  1051. }
  1052. snprintf(sc->rf_kill.rfkill_name, sizeof(sc->rf_kill.rfkill_name),
  1053. "ath9k-%s:rfkill", wiphy_name(sc->hw->wiphy));
  1054. sc->rf_kill.rfkill->name = sc->rf_kill.rfkill_name;
  1055. sc->rf_kill.rfkill->data = sc;
  1056. sc->rf_kill.rfkill->toggle_radio = ath_sw_toggle_radio;
  1057. sc->rf_kill.rfkill->state = RFKILL_STATE_UNBLOCKED;
  1058. sc->rf_kill.rfkill->user_claim_unsupported = 1;
  1059. return 0;
  1060. }
  1061. /* Deinitialize rfkill */
  1062. static void ath_deinit_rfkill(struct ath_softc *sc)
  1063. {
  1064. if (sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
  1065. cancel_delayed_work_sync(&sc->rf_kill.rfkill_poll);
  1066. if (sc->sc_flags & SC_OP_RFKILL_REGISTERED) {
  1067. rfkill_unregister(sc->rf_kill.rfkill);
  1068. sc->sc_flags &= ~SC_OP_RFKILL_REGISTERED;
  1069. sc->rf_kill.rfkill = NULL;
  1070. }
  1071. }
  1072. static int ath_start_rfkill_poll(struct ath_softc *sc)
  1073. {
  1074. if (sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
  1075. queue_delayed_work(sc->hw->workqueue,
  1076. &sc->rf_kill.rfkill_poll, 0);
  1077. if (!(sc->sc_flags & SC_OP_RFKILL_REGISTERED)) {
  1078. if (rfkill_register(sc->rf_kill.rfkill)) {
  1079. DPRINTF(sc, ATH_DBG_FATAL,
  1080. "Unable to register rfkill\n");
  1081. rfkill_free(sc->rf_kill.rfkill);
  1082. /* Deinitialize the device */
  1083. ath_cleanup(sc);
  1084. return -EIO;
  1085. } else {
  1086. sc->sc_flags |= SC_OP_RFKILL_REGISTERED;
  1087. }
  1088. }
  1089. return 0;
  1090. }
  1091. #endif /* CONFIG_RFKILL */
  1092. void ath_cleanup(struct ath_softc *sc)
  1093. {
  1094. ath_detach(sc);
  1095. free_irq(sc->irq, sc);
  1096. ath_bus_cleanup(sc);
  1097. ieee80211_free_hw(sc->hw);
  1098. }
  1099. void ath_detach(struct ath_softc *sc)
  1100. {
  1101. struct ieee80211_hw *hw = sc->hw;
  1102. int i = 0;
  1103. ath9k_ps_wakeup(sc);
  1104. DPRINTF(sc, ATH_DBG_CONFIG, "Detach ATH hw\n");
  1105. #if defined(CONFIG_RFKILL) || defined(CONFIG_RFKILL_MODULE)
  1106. ath_deinit_rfkill(sc);
  1107. #endif
  1108. ath_deinit_leds(sc);
  1109. ieee80211_unregister_hw(hw);
  1110. ath_rx_cleanup(sc);
  1111. ath_tx_cleanup(sc);
  1112. tasklet_kill(&sc->intr_tq);
  1113. tasklet_kill(&sc->bcon_tasklet);
  1114. if (!(sc->sc_flags & SC_OP_INVALID))
  1115. ath9k_hw_setpower(sc->sc_ah, ATH9K_PM_AWAKE);
  1116. /* cleanup tx queues */
  1117. for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++)
  1118. if (ATH_TXQ_SETUP(sc, i))
  1119. ath_tx_cleanupq(sc, &sc->tx.txq[i]);
  1120. ath9k_hw_detach(sc->sc_ah);
  1121. ath9k_exit_debug(sc);
  1122. ath9k_ps_restore(sc);
  1123. }
  1124. static int ath_init(u16 devid, struct ath_softc *sc)
  1125. {
  1126. struct ath_hal *ah = NULL;
  1127. int status;
  1128. int error = 0, i;
  1129. int csz = 0;
  1130. /* XXX: hardware will not be ready until ath_open() being called */
  1131. sc->sc_flags |= SC_OP_INVALID;
  1132. if (ath9k_init_debug(sc) < 0)
  1133. printk(KERN_ERR "Unable to create debugfs files\n");
  1134. spin_lock_init(&sc->sc_resetlock);
  1135. mutex_init(&sc->mutex);
  1136. tasklet_init(&sc->intr_tq, ath9k_tasklet, (unsigned long)sc);
  1137. tasklet_init(&sc->bcon_tasklet, ath9k_beacon_tasklet,
  1138. (unsigned long)sc);
  1139. /*
  1140. * Cache line size is used to size and align various
  1141. * structures used to communicate with the hardware.
  1142. */
  1143. ath_read_cachesize(sc, &csz);
  1144. /* XXX assert csz is non-zero */
  1145. sc->sc_cachelsz = csz << 2; /* convert to bytes */
  1146. ah = ath9k_hw_attach(devid, sc, sc->mem, &status);
  1147. if (ah == NULL) {
  1148. DPRINTF(sc, ATH_DBG_FATAL,
  1149. "Unable to attach hardware; HAL status %d\n", status);
  1150. error = -ENXIO;
  1151. goto bad;
  1152. }
  1153. sc->sc_ah = ah;
  1154. /* Get the hardware key cache size. */
  1155. sc->sc_keymax = ah->ah_caps.keycache_size;
  1156. if (sc->sc_keymax > ATH_KEYMAX) {
  1157. DPRINTF(sc, ATH_DBG_KEYCACHE,
  1158. "Warning, using only %u entries in %u key cache\n",
  1159. ATH_KEYMAX, sc->sc_keymax);
  1160. sc->sc_keymax = ATH_KEYMAX;
  1161. }
  1162. /*
  1163. * Reset the key cache since some parts do not
  1164. * reset the contents on initial power up.
  1165. */
  1166. for (i = 0; i < sc->sc_keymax; i++)
  1167. ath9k_hw_keyreset(ah, (u16) i);
  1168. if (ath9k_regd_init(sc->sc_ah))
  1169. goto bad;
  1170. /* default to MONITOR mode */
  1171. sc->sc_ah->ah_opmode = NL80211_IFTYPE_MONITOR;
  1172. /* Setup rate tables */
  1173. ath_rate_attach(sc);
  1174. ath_setup_rates(sc, IEEE80211_BAND_2GHZ);
  1175. ath_setup_rates(sc, IEEE80211_BAND_5GHZ);
  1176. /*
  1177. * Allocate hardware transmit queues: one queue for
  1178. * beacon frames and one data queue for each QoS
  1179. * priority. Note that the hal handles reseting
  1180. * these queues at the needed time.
  1181. */
  1182. sc->beacon.beaconq = ath_beaconq_setup(ah);
  1183. if (sc->beacon.beaconq == -1) {
  1184. DPRINTF(sc, ATH_DBG_FATAL,
  1185. "Unable to setup a beacon xmit queue\n");
  1186. error = -EIO;
  1187. goto bad2;
  1188. }
  1189. sc->beacon.cabq = ath_txq_setup(sc, ATH9K_TX_QUEUE_CAB, 0);
  1190. if (sc->beacon.cabq == NULL) {
  1191. DPRINTF(sc, ATH_DBG_FATAL,
  1192. "Unable to setup CAB xmit queue\n");
  1193. error = -EIO;
  1194. goto bad2;
  1195. }
  1196. sc->sc_config.cabqReadytime = ATH_CABQ_READY_TIME;
  1197. ath_cabq_update(sc);
  1198. for (i = 0; i < ARRAY_SIZE(sc->tx.hwq_map); i++)
  1199. sc->tx.hwq_map[i] = -1;
  1200. /* Setup data queues */
  1201. /* NB: ensure BK queue is the lowest priority h/w queue */
  1202. if (!ath_tx_setup(sc, ATH9K_WME_AC_BK)) {
  1203. DPRINTF(sc, ATH_DBG_FATAL,
  1204. "Unable to setup xmit queue for BK traffic\n");
  1205. error = -EIO;
  1206. goto bad2;
  1207. }
  1208. if (!ath_tx_setup(sc, ATH9K_WME_AC_BE)) {
  1209. DPRINTF(sc, ATH_DBG_FATAL,
  1210. "Unable to setup xmit queue for BE traffic\n");
  1211. error = -EIO;
  1212. goto bad2;
  1213. }
  1214. if (!ath_tx_setup(sc, ATH9K_WME_AC_VI)) {
  1215. DPRINTF(sc, ATH_DBG_FATAL,
  1216. "Unable to setup xmit queue for VI traffic\n");
  1217. error = -EIO;
  1218. goto bad2;
  1219. }
  1220. if (!ath_tx_setup(sc, ATH9K_WME_AC_VO)) {
  1221. DPRINTF(sc, ATH_DBG_FATAL,
  1222. "Unable to setup xmit queue for VO traffic\n");
  1223. error = -EIO;
  1224. goto bad2;
  1225. }
  1226. /* Initializes the noise floor to a reasonable default value.
  1227. * Later on this will be updated during ANI processing. */
  1228. sc->sc_ani.sc_noise_floor = ATH_DEFAULT_NOISE_FLOOR;
  1229. setup_timer(&sc->sc_ani.timer, ath_ani_calibrate, (unsigned long)sc);
  1230. if (ath9k_hw_getcapability(ah, ATH9K_CAP_CIPHER,
  1231. ATH9K_CIPHER_TKIP, NULL)) {
  1232. /*
  1233. * Whether we should enable h/w TKIP MIC.
  1234. * XXX: if we don't support WME TKIP MIC, then we wouldn't
  1235. * report WMM capable, so it's always safe to turn on
  1236. * TKIP MIC in this case.
  1237. */
  1238. ath9k_hw_setcapability(sc->sc_ah, ATH9K_CAP_TKIP_MIC,
  1239. 0, 1, NULL);
  1240. }
  1241. /*
  1242. * Check whether the separate key cache entries
  1243. * are required to handle both tx+rx MIC keys.
  1244. * With split mic keys the number of stations is limited
  1245. * to 27 otherwise 59.
  1246. */
  1247. if (ath9k_hw_getcapability(ah, ATH9K_CAP_CIPHER,
  1248. ATH9K_CIPHER_TKIP, NULL)
  1249. && ath9k_hw_getcapability(ah, ATH9K_CAP_CIPHER,
  1250. ATH9K_CIPHER_MIC, NULL)
  1251. && ath9k_hw_getcapability(ah, ATH9K_CAP_TKIP_SPLIT,
  1252. 0, NULL))
  1253. sc->sc_splitmic = 1;
  1254. /* turn on mcast key search if possible */
  1255. if (!ath9k_hw_getcapability(ah, ATH9K_CAP_MCAST_KEYSRCH, 0, NULL))
  1256. (void)ath9k_hw_setcapability(ah, ATH9K_CAP_MCAST_KEYSRCH, 1,
  1257. 1, NULL);
  1258. sc->sc_config.txpowlimit = ATH_TXPOWER_MAX;
  1259. sc->sc_config.txpowlimit_override = 0;
  1260. /* 11n Capabilities */
  1261. if (ah->ah_caps.hw_caps & ATH9K_HW_CAP_HT) {
  1262. sc->sc_flags |= SC_OP_TXAGGR;
  1263. sc->sc_flags |= SC_OP_RXAGGR;
  1264. }
  1265. sc->sc_tx_chainmask = ah->ah_caps.tx_chainmask;
  1266. sc->sc_rx_chainmask = ah->ah_caps.rx_chainmask;
  1267. ath9k_hw_setcapability(ah, ATH9K_CAP_DIVERSITY, 1, true, NULL);
  1268. sc->rx.defant = ath9k_hw_getdefantenna(ah);
  1269. ath9k_hw_getmac(ah, sc->sc_myaddr);
  1270. if (ah->ah_caps.hw_caps & ATH9K_HW_CAP_BSSIDMASK) {
  1271. ath9k_hw_getbssidmask(ah, sc->sc_bssidmask);
  1272. ATH_SET_VAP_BSSID_MASK(sc->sc_bssidmask);
  1273. ath9k_hw_setbssidmask(ah, sc->sc_bssidmask);
  1274. }
  1275. sc->beacon.slottime = ATH9K_SLOT_TIME_9; /* default to short slot time */
  1276. /* initialize beacon slots */
  1277. for (i = 0; i < ARRAY_SIZE(sc->beacon.bslot); i++)
  1278. sc->beacon.bslot[i] = ATH_IF_ID_ANY;
  1279. /* save MISC configurations */
  1280. sc->sc_config.swBeaconProcess = 1;
  1281. /* setup channels and rates */
  1282. sc->sbands[IEEE80211_BAND_2GHZ].channels = ath9k_2ghz_chantable;
  1283. sc->sbands[IEEE80211_BAND_2GHZ].bitrates =
  1284. sc->rates[IEEE80211_BAND_2GHZ];
  1285. sc->sbands[IEEE80211_BAND_2GHZ].band = IEEE80211_BAND_2GHZ;
  1286. sc->sbands[IEEE80211_BAND_2GHZ].n_channels =
  1287. ARRAY_SIZE(ath9k_2ghz_chantable);
  1288. if (test_bit(ATH9K_MODE_11A, sc->sc_ah->ah_caps.wireless_modes)) {
  1289. sc->sbands[IEEE80211_BAND_5GHZ].channels = ath9k_5ghz_chantable;
  1290. sc->sbands[IEEE80211_BAND_5GHZ].bitrates =
  1291. sc->rates[IEEE80211_BAND_5GHZ];
  1292. sc->sbands[IEEE80211_BAND_5GHZ].band = IEEE80211_BAND_5GHZ;
  1293. sc->sbands[IEEE80211_BAND_5GHZ].n_channels =
  1294. ARRAY_SIZE(ath9k_5ghz_chantable);
  1295. }
  1296. if (sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_BT_COEX)
  1297. ath9k_hw_btcoex_enable(sc->sc_ah);
  1298. return 0;
  1299. bad2:
  1300. /* cleanup tx queues */
  1301. for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++)
  1302. if (ATH_TXQ_SETUP(sc, i))
  1303. ath_tx_cleanupq(sc, &sc->tx.txq[i]);
  1304. bad:
  1305. if (ah)
  1306. ath9k_hw_detach(ah);
  1307. return error;
  1308. }
  1309. int ath_attach(u16 devid, struct ath_softc *sc)
  1310. {
  1311. struct ieee80211_hw *hw = sc->hw;
  1312. int error = 0;
  1313. DPRINTF(sc, ATH_DBG_CONFIG, "Attach ATH hw\n");
  1314. error = ath_init(devid, sc);
  1315. if (error != 0)
  1316. return error;
  1317. /* get mac address from hardware and set in mac80211 */
  1318. SET_IEEE80211_PERM_ADDR(hw, sc->sc_myaddr);
  1319. hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
  1320. IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
  1321. IEEE80211_HW_SIGNAL_DBM |
  1322. IEEE80211_HW_AMPDU_AGGREGATION |
  1323. IEEE80211_HW_SUPPORTS_PS |
  1324. IEEE80211_HW_PS_NULLFUNC_STACK;
  1325. if (AR_SREV_9160_10_OR_LATER(sc->sc_ah))
  1326. hw->flags |= IEEE80211_HW_MFP_CAPABLE;
  1327. hw->wiphy->interface_modes =
  1328. BIT(NL80211_IFTYPE_AP) |
  1329. BIT(NL80211_IFTYPE_STATION) |
  1330. BIT(NL80211_IFTYPE_ADHOC);
  1331. hw->wiphy->reg_notifier = ath9k_reg_notifier;
  1332. hw->wiphy->strict_regulatory = true;
  1333. hw->queues = 4;
  1334. hw->max_rates = 4;
  1335. hw->max_rate_tries = ATH_11N_TXMAXTRY;
  1336. hw->sta_data_size = sizeof(struct ath_node);
  1337. hw->vif_data_size = sizeof(struct ath_vap);
  1338. hw->rate_control_algorithm = "ath9k_rate_control";
  1339. if (sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_HT) {
  1340. setup_ht_cap(sc, &sc->sbands[IEEE80211_BAND_2GHZ].ht_cap);
  1341. if (test_bit(ATH9K_MODE_11A, sc->sc_ah->ah_caps.wireless_modes))
  1342. setup_ht_cap(sc, &sc->sbands[IEEE80211_BAND_5GHZ].ht_cap);
  1343. }
  1344. hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &sc->sbands[IEEE80211_BAND_2GHZ];
  1345. if (test_bit(ATH9K_MODE_11A, sc->sc_ah->ah_caps.wireless_modes))
  1346. hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
  1347. &sc->sbands[IEEE80211_BAND_5GHZ];
  1348. /* initialize tx/rx engine */
  1349. error = ath_tx_init(sc, ATH_TXBUF);
  1350. if (error != 0)
  1351. goto detach;
  1352. error = ath_rx_init(sc, ATH_RXBUF);
  1353. if (error != 0)
  1354. goto detach;
  1355. #if defined(CONFIG_RFKILL) || defined(CONFIG_RFKILL_MODULE)
  1356. /* Initialze h/w Rfkill */
  1357. if (sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
  1358. INIT_DELAYED_WORK(&sc->rf_kill.rfkill_poll, ath_rfkill_poll);
  1359. /* Initialize s/w rfkill */
  1360. if (ath_init_sw_rfkill(sc))
  1361. goto detach;
  1362. #endif
  1363. if (ath9k_is_world_regd(sc->sc_ah)) {
  1364. /* Anything applied here (prior to wiphy registratoin) gets
  1365. * saved on the wiphy orig_* parameters */
  1366. const struct ieee80211_regdomain *regd =
  1367. ath9k_world_regdomain(sc->sc_ah);
  1368. hw->wiphy->custom_regulatory = true;
  1369. hw->wiphy->strict_regulatory = false;
  1370. wiphy_apply_custom_regulatory(sc->hw->wiphy, regd);
  1371. ath9k_reg_apply_radar_flags(hw->wiphy);
  1372. ath9k_reg_apply_world_flags(hw->wiphy, REGDOM_SET_BY_INIT);
  1373. } else {
  1374. /* This gets applied in the case of the absense of CRDA,
  1375. * its our own custom world regulatory domain, similar to
  1376. * cfg80211's but we enable passive scanning */
  1377. const struct ieee80211_regdomain *regd =
  1378. ath9k_default_world_regdomain();
  1379. wiphy_apply_custom_regulatory(sc->hw->wiphy, regd);
  1380. ath9k_reg_apply_radar_flags(hw->wiphy);
  1381. ath9k_reg_apply_world_flags(hw->wiphy, REGDOM_SET_BY_INIT);
  1382. }
  1383. error = ieee80211_register_hw(hw);
  1384. if (!ath9k_is_world_regd(sc->sc_ah))
  1385. regulatory_hint(hw->wiphy, sc->sc_ah->alpha2);
  1386. /* Initialize LED control */
  1387. ath_init_leds(sc);
  1388. return 0;
  1389. detach:
  1390. ath_detach(sc);
  1391. return error;
  1392. }
  1393. int ath_reset(struct ath_softc *sc, bool retry_tx)
  1394. {
  1395. struct ath_hal *ah = sc->sc_ah;
  1396. struct ieee80211_hw *hw = sc->hw;
  1397. int r;
  1398. ath9k_hw_set_interrupts(ah, 0);
  1399. ath_drain_all_txq(sc, retry_tx);
  1400. ath_stoprecv(sc);
  1401. ath_flushrecv(sc);
  1402. spin_lock_bh(&sc->sc_resetlock);
  1403. r = ath9k_hw_reset(ah, sc->sc_ah->ah_curchan, false);
  1404. if (r)
  1405. DPRINTF(sc, ATH_DBG_FATAL,
  1406. "Unable to reset hardware; reset status %u\n", r);
  1407. spin_unlock_bh(&sc->sc_resetlock);
  1408. if (ath_startrecv(sc) != 0)
  1409. DPRINTF(sc, ATH_DBG_FATAL, "Unable to start recv logic\n");
  1410. /*
  1411. * We may be doing a reset in response to a request
  1412. * that changes the channel so update any state that
  1413. * might change as a result.
  1414. */
  1415. ath_cache_conf_rate(sc, &hw->conf);
  1416. ath_update_txpow(sc);
  1417. if (sc->sc_flags & SC_OP_BEACONS)
  1418. ath_beacon_config(sc, ATH_IF_ID_ANY); /* restart beacons */
  1419. ath9k_hw_set_interrupts(ah, sc->sc_imask);
  1420. if (retry_tx) {
  1421. int i;
  1422. for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
  1423. if (ATH_TXQ_SETUP(sc, i)) {
  1424. spin_lock_bh(&sc->tx.txq[i].axq_lock);
  1425. ath_txq_schedule(sc, &sc->tx.txq[i]);
  1426. spin_unlock_bh(&sc->tx.txq[i].axq_lock);
  1427. }
  1428. }
  1429. }
  1430. return r;
  1431. }
  1432. /*
  1433. * This function will allocate both the DMA descriptor structure, and the
  1434. * buffers it contains. These are used to contain the descriptors used
  1435. * by the system.
  1436. */
  1437. int ath_descdma_setup(struct ath_softc *sc, struct ath_descdma *dd,
  1438. struct list_head *head, const char *name,
  1439. int nbuf, int ndesc)
  1440. {
  1441. #define DS2PHYS(_dd, _ds) \
  1442. ((_dd)->dd_desc_paddr + ((caddr_t)(_ds) - (caddr_t)(_dd)->dd_desc))
  1443. #define ATH_DESC_4KB_BOUND_CHECK(_daddr) ((((_daddr) & 0xFFF) > 0xF7F) ? 1 : 0)
  1444. #define ATH_DESC_4KB_BOUND_NUM_SKIPPED(_len) ((_len) / 4096)
  1445. struct ath_desc *ds;
  1446. struct ath_buf *bf;
  1447. int i, bsize, error;
  1448. DPRINTF(sc, ATH_DBG_CONFIG, "%s DMA: %u buffers %u desc/buf\n",
  1449. name, nbuf, ndesc);
  1450. /* ath_desc must be a multiple of DWORDs */
  1451. if ((sizeof(struct ath_desc) % 4) != 0) {
  1452. DPRINTF(sc, ATH_DBG_FATAL, "ath_desc not DWORD aligned\n");
  1453. ASSERT((sizeof(struct ath_desc) % 4) == 0);
  1454. error = -ENOMEM;
  1455. goto fail;
  1456. }
  1457. dd->dd_name = name;
  1458. dd->dd_desc_len = sizeof(struct ath_desc) * nbuf * ndesc;
  1459. /*
  1460. * Need additional DMA memory because we can't use
  1461. * descriptors that cross the 4K page boundary. Assume
  1462. * one skipped descriptor per 4K page.
  1463. */
  1464. if (!(sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_4KB_SPLITTRANS)) {
  1465. u32 ndesc_skipped =
  1466. ATH_DESC_4KB_BOUND_NUM_SKIPPED(dd->dd_desc_len);
  1467. u32 dma_len;
  1468. while (ndesc_skipped) {
  1469. dma_len = ndesc_skipped * sizeof(struct ath_desc);
  1470. dd->dd_desc_len += dma_len;
  1471. ndesc_skipped = ATH_DESC_4KB_BOUND_NUM_SKIPPED(dma_len);
  1472. };
  1473. }
  1474. /* allocate descriptors */
  1475. dd->dd_desc = dma_alloc_coherent(sc->dev, dd->dd_desc_len,
  1476. &dd->dd_desc_paddr, GFP_ATOMIC);
  1477. if (dd->dd_desc == NULL) {
  1478. error = -ENOMEM;
  1479. goto fail;
  1480. }
  1481. ds = dd->dd_desc;
  1482. DPRINTF(sc, ATH_DBG_CONFIG, "%s DMA map: %p (%u) -> %llx (%u)\n",
  1483. dd->dd_name, ds, (u32) dd->dd_desc_len,
  1484. ito64(dd->dd_desc_paddr), /*XXX*/(u32) dd->dd_desc_len);
  1485. /* allocate buffers */
  1486. bsize = sizeof(struct ath_buf) * nbuf;
  1487. bf = kmalloc(bsize, GFP_KERNEL);
  1488. if (bf == NULL) {
  1489. error = -ENOMEM;
  1490. goto fail2;
  1491. }
  1492. memset(bf, 0, bsize);
  1493. dd->dd_bufptr = bf;
  1494. INIT_LIST_HEAD(head);
  1495. for (i = 0; i < nbuf; i++, bf++, ds += ndesc) {
  1496. bf->bf_desc = ds;
  1497. bf->bf_daddr = DS2PHYS(dd, ds);
  1498. if (!(sc->sc_ah->ah_caps.hw_caps &
  1499. ATH9K_HW_CAP_4KB_SPLITTRANS)) {
  1500. /*
  1501. * Skip descriptor addresses which can cause 4KB
  1502. * boundary crossing (addr + length) with a 32 dword
  1503. * descriptor fetch.
  1504. */
  1505. while (ATH_DESC_4KB_BOUND_CHECK(bf->bf_daddr)) {
  1506. ASSERT((caddr_t) bf->bf_desc <
  1507. ((caddr_t) dd->dd_desc +
  1508. dd->dd_desc_len));
  1509. ds += ndesc;
  1510. bf->bf_desc = ds;
  1511. bf->bf_daddr = DS2PHYS(dd, ds);
  1512. }
  1513. }
  1514. list_add_tail(&bf->list, head);
  1515. }
  1516. return 0;
  1517. fail2:
  1518. dma_free_coherent(sc->dev, dd->dd_desc_len, dd->dd_desc,
  1519. dd->dd_desc_paddr);
  1520. fail:
  1521. memset(dd, 0, sizeof(*dd));
  1522. return error;
  1523. #undef ATH_DESC_4KB_BOUND_CHECK
  1524. #undef ATH_DESC_4KB_BOUND_NUM_SKIPPED
  1525. #undef DS2PHYS
  1526. }
  1527. void ath_descdma_cleanup(struct ath_softc *sc,
  1528. struct ath_descdma *dd,
  1529. struct list_head *head)
  1530. {
  1531. dma_free_coherent(sc->dev, dd->dd_desc_len, dd->dd_desc,
  1532. dd->dd_desc_paddr);
  1533. INIT_LIST_HEAD(head);
  1534. kfree(dd->dd_bufptr);
  1535. memset(dd, 0, sizeof(*dd));
  1536. }
  1537. int ath_get_hal_qnum(u16 queue, struct ath_softc *sc)
  1538. {
  1539. int qnum;
  1540. switch (queue) {
  1541. case 0:
  1542. qnum = sc->tx.hwq_map[ATH9K_WME_AC_VO];
  1543. break;
  1544. case 1:
  1545. qnum = sc->tx.hwq_map[ATH9K_WME_AC_VI];
  1546. break;
  1547. case 2:
  1548. qnum = sc->tx.hwq_map[ATH9K_WME_AC_BE];
  1549. break;
  1550. case 3:
  1551. qnum = sc->tx.hwq_map[ATH9K_WME_AC_BK];
  1552. break;
  1553. default:
  1554. qnum = sc->tx.hwq_map[ATH9K_WME_AC_BE];
  1555. break;
  1556. }
  1557. return qnum;
  1558. }
  1559. int ath_get_mac80211_qnum(u32 queue, struct ath_softc *sc)
  1560. {
  1561. int qnum;
  1562. switch (queue) {
  1563. case ATH9K_WME_AC_VO:
  1564. qnum = 0;
  1565. break;
  1566. case ATH9K_WME_AC_VI:
  1567. qnum = 1;
  1568. break;
  1569. case ATH9K_WME_AC_BE:
  1570. qnum = 2;
  1571. break;
  1572. case ATH9K_WME_AC_BK:
  1573. qnum = 3;
  1574. break;
  1575. default:
  1576. qnum = -1;
  1577. break;
  1578. }
  1579. return qnum;
  1580. }
  1581. /* XXX: Remove me once we don't depend on ath9k_channel for all
  1582. * this redundant data */
  1583. static void ath9k_update_ichannel(struct ath_softc *sc,
  1584. struct ath9k_channel *ichan)
  1585. {
  1586. struct ieee80211_hw *hw = sc->hw;
  1587. struct ieee80211_channel *chan = hw->conf.channel;
  1588. struct ieee80211_conf *conf = &hw->conf;
  1589. ichan->channel = chan->center_freq;
  1590. ichan->chan = chan;
  1591. if (chan->band == IEEE80211_BAND_2GHZ) {
  1592. ichan->chanmode = CHANNEL_G;
  1593. ichan->channelFlags = CHANNEL_2GHZ | CHANNEL_OFDM;
  1594. } else {
  1595. ichan->chanmode = CHANNEL_A;
  1596. ichan->channelFlags = CHANNEL_5GHZ | CHANNEL_OFDM;
  1597. }
  1598. sc->tx_chan_width = ATH9K_HT_MACMODE_20;
  1599. if (conf_is_ht(conf)) {
  1600. if (conf_is_ht40(conf))
  1601. sc->tx_chan_width = ATH9K_HT_MACMODE_2040;
  1602. ichan->chanmode = ath_get_extchanmode(sc, chan,
  1603. conf->channel_type);
  1604. }
  1605. }
  1606. /**********************/
  1607. /* mac80211 callbacks */
  1608. /**********************/
  1609. static int ath9k_start(struct ieee80211_hw *hw)
  1610. {
  1611. struct ath_softc *sc = hw->priv;
  1612. struct ieee80211_channel *curchan = hw->conf.channel;
  1613. struct ath9k_channel *init_channel;
  1614. int r, pos;
  1615. DPRINTF(sc, ATH_DBG_CONFIG, "Starting driver with "
  1616. "initial channel: %d MHz\n", curchan->center_freq);
  1617. /* setup initial channel */
  1618. pos = curchan->hw_value;
  1619. init_channel = &sc->sc_ah->ah_channels[pos];
  1620. ath9k_update_ichannel(sc, init_channel);
  1621. /* Reset SERDES registers */
  1622. ath9k_hw_configpcipowersave(sc->sc_ah, 0);
  1623. /*
  1624. * The basic interface to setting the hardware in a good
  1625. * state is ``reset''. On return the hardware is known to
  1626. * be powered up and with interrupts disabled. This must
  1627. * be followed by initialization of the appropriate bits
  1628. * and then setup of the interrupt mask.
  1629. */
  1630. spin_lock_bh(&sc->sc_resetlock);
  1631. r = ath9k_hw_reset(sc->sc_ah, init_channel, false);
  1632. if (r) {
  1633. DPRINTF(sc, ATH_DBG_FATAL,
  1634. "Unable to reset hardware; reset status %u "
  1635. "(freq %u MHz)\n", r,
  1636. curchan->center_freq);
  1637. spin_unlock_bh(&sc->sc_resetlock);
  1638. return r;
  1639. }
  1640. spin_unlock_bh(&sc->sc_resetlock);
  1641. /*
  1642. * This is needed only to setup initial state
  1643. * but it's best done after a reset.
  1644. */
  1645. ath_update_txpow(sc);
  1646. /*
  1647. * Setup the hardware after reset:
  1648. * The receive engine is set going.
  1649. * Frame transmit is handled entirely
  1650. * in the frame output path; there's nothing to do
  1651. * here except setup the interrupt mask.
  1652. */
  1653. if (ath_startrecv(sc) != 0) {
  1654. DPRINTF(sc, ATH_DBG_FATAL,
  1655. "Unable to start recv logic\n");
  1656. return -EIO;
  1657. }
  1658. /* Setup our intr mask. */
  1659. sc->sc_imask = ATH9K_INT_RX | ATH9K_INT_TX
  1660. | ATH9K_INT_RXEOL | ATH9K_INT_RXORN
  1661. | ATH9K_INT_FATAL | ATH9K_INT_GLOBAL;
  1662. if (sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_GTT)
  1663. sc->sc_imask |= ATH9K_INT_GTT;
  1664. if (sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_HT)
  1665. sc->sc_imask |= ATH9K_INT_CST;
  1666. /*
  1667. * Enable MIB interrupts when there are hardware phy counters.
  1668. * Note we only do this (at the moment) for station mode.
  1669. */
  1670. if (ath9k_hw_phycounters(sc->sc_ah) &&
  1671. ((sc->sc_ah->ah_opmode == NL80211_IFTYPE_STATION) ||
  1672. (sc->sc_ah->ah_opmode == NL80211_IFTYPE_ADHOC)))
  1673. sc->sc_imask |= ATH9K_INT_MIB;
  1674. /*
  1675. * Some hardware processes the TIM IE and fires an
  1676. * interrupt when the TIM bit is set. For hardware
  1677. * that does, if not overridden by configuration,
  1678. * enable the TIM interrupt when operating as station.
  1679. */
  1680. if ((sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_ENHANCEDPM) &&
  1681. (sc->sc_ah->ah_opmode == NL80211_IFTYPE_STATION) &&
  1682. !sc->sc_config.swBeaconProcess)
  1683. sc->sc_imask |= ATH9K_INT_TIM;
  1684. ath_cache_conf_rate(sc, &hw->conf);
  1685. sc->sc_flags &= ~SC_OP_INVALID;
  1686. /* Disable BMISS interrupt when we're not associated */
  1687. sc->sc_imask &= ~(ATH9K_INT_SWBA | ATH9K_INT_BMISS);
  1688. ath9k_hw_set_interrupts(sc->sc_ah, sc->sc_imask);
  1689. ieee80211_wake_queues(sc->hw);
  1690. #if defined(CONFIG_RFKILL) || defined(CONFIG_RFKILL_MODULE)
  1691. r = ath_start_rfkill_poll(sc);
  1692. #endif
  1693. return r;
  1694. }
  1695. static int ath9k_tx(struct ieee80211_hw *hw,
  1696. struct sk_buff *skb)
  1697. {
  1698. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1699. struct ath_softc *sc = hw->priv;
  1700. struct ath_tx_control txctl;
  1701. int hdrlen, padsize;
  1702. memset(&txctl, 0, sizeof(struct ath_tx_control));
  1703. /*
  1704. * As a temporary workaround, assign seq# here; this will likely need
  1705. * to be cleaned up to work better with Beacon transmission and virtual
  1706. * BSSes.
  1707. */
  1708. if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
  1709. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  1710. if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
  1711. sc->tx.seq_no += 0x10;
  1712. hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
  1713. hdr->seq_ctrl |= cpu_to_le16(sc->tx.seq_no);
  1714. }
  1715. /* Add the padding after the header if this is not already done */
  1716. hdrlen = ieee80211_get_hdrlen_from_skb(skb);
  1717. if (hdrlen & 3) {
  1718. padsize = hdrlen % 4;
  1719. if (skb_headroom(skb) < padsize)
  1720. return -1;
  1721. skb_push(skb, padsize);
  1722. memmove(skb->data, skb->data + padsize, hdrlen);
  1723. }
  1724. /* Check if a tx queue is available */
  1725. txctl.txq = ath_test_get_txq(sc, skb);
  1726. if (!txctl.txq)
  1727. goto exit;
  1728. DPRINTF(sc, ATH_DBG_XMIT, "transmitting packet, skb: %p\n", skb);
  1729. if (ath_tx_start(sc, skb, &txctl) != 0) {
  1730. DPRINTF(sc, ATH_DBG_XMIT, "TX failed\n");
  1731. goto exit;
  1732. }
  1733. return 0;
  1734. exit:
  1735. dev_kfree_skb_any(skb);
  1736. return 0;
  1737. }
  1738. static void ath9k_stop(struct ieee80211_hw *hw)
  1739. {
  1740. struct ath_softc *sc = hw->priv;
  1741. if (sc->sc_flags & SC_OP_INVALID) {
  1742. DPRINTF(sc, ATH_DBG_ANY, "Device not present\n");
  1743. return;
  1744. }
  1745. DPRINTF(sc, ATH_DBG_CONFIG, "Cleaning up\n");
  1746. ieee80211_stop_queues(sc->hw);
  1747. /* make sure h/w will not generate any interrupt
  1748. * before setting the invalid flag. */
  1749. ath9k_hw_set_interrupts(sc->sc_ah, 0);
  1750. if (!(sc->sc_flags & SC_OP_INVALID)) {
  1751. ath_drain_all_txq(sc, false);
  1752. ath_stoprecv(sc);
  1753. ath9k_hw_phy_disable(sc->sc_ah);
  1754. } else
  1755. sc->rx.rxlink = NULL;
  1756. #if defined(CONFIG_RFKILL) || defined(CONFIG_RFKILL_MODULE)
  1757. if (sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
  1758. cancel_delayed_work_sync(&sc->rf_kill.rfkill_poll);
  1759. #endif
  1760. /* disable HAL and put h/w to sleep */
  1761. ath9k_hw_disable(sc->sc_ah);
  1762. ath9k_hw_configpcipowersave(sc->sc_ah, 1);
  1763. sc->sc_flags |= SC_OP_INVALID;
  1764. DPRINTF(sc, ATH_DBG_CONFIG, "Driver halt\n");
  1765. }
  1766. static int ath9k_add_interface(struct ieee80211_hw *hw,
  1767. struct ieee80211_if_init_conf *conf)
  1768. {
  1769. struct ath_softc *sc = hw->priv;
  1770. struct ath_vap *avp = (void *)conf->vif->drv_priv;
  1771. enum nl80211_iftype ic_opmode = NL80211_IFTYPE_UNSPECIFIED;
  1772. /* Support only vap for now */
  1773. if (sc->sc_nvaps)
  1774. return -ENOBUFS;
  1775. switch (conf->type) {
  1776. case NL80211_IFTYPE_STATION:
  1777. ic_opmode = NL80211_IFTYPE_STATION;
  1778. break;
  1779. case NL80211_IFTYPE_ADHOC:
  1780. ic_opmode = NL80211_IFTYPE_ADHOC;
  1781. break;
  1782. case NL80211_IFTYPE_AP:
  1783. ic_opmode = NL80211_IFTYPE_AP;
  1784. break;
  1785. default:
  1786. DPRINTF(sc, ATH_DBG_FATAL,
  1787. "Interface type %d not yet supported\n", conf->type);
  1788. return -EOPNOTSUPP;
  1789. }
  1790. DPRINTF(sc, ATH_DBG_CONFIG, "Attach a VAP of type: %d\n", ic_opmode);
  1791. /* Set the VAP opmode */
  1792. avp->av_opmode = ic_opmode;
  1793. avp->av_bslot = -1;
  1794. if (ic_opmode == NL80211_IFTYPE_AP)
  1795. ath9k_hw_set_tsfadjust(sc->sc_ah, 1);
  1796. sc->sc_vaps[0] = conf->vif;
  1797. sc->sc_nvaps++;
  1798. /* Set the device opmode */
  1799. sc->sc_ah->ah_opmode = ic_opmode;
  1800. if (conf->type == NL80211_IFTYPE_AP) {
  1801. /* TODO: is this a suitable place to start ANI for AP mode? */
  1802. /* Start ANI */
  1803. mod_timer(&sc->sc_ani.timer,
  1804. jiffies + msecs_to_jiffies(ATH_ANI_POLLINTERVAL));
  1805. }
  1806. return 0;
  1807. }
  1808. static void ath9k_remove_interface(struct ieee80211_hw *hw,
  1809. struct ieee80211_if_init_conf *conf)
  1810. {
  1811. struct ath_softc *sc = hw->priv;
  1812. struct ath_vap *avp = (void *)conf->vif->drv_priv;
  1813. DPRINTF(sc, ATH_DBG_CONFIG, "Detach Interface\n");
  1814. /* Stop ANI */
  1815. del_timer_sync(&sc->sc_ani.timer);
  1816. /* Reclaim beacon resources */
  1817. if (sc->sc_ah->ah_opmode == NL80211_IFTYPE_AP ||
  1818. sc->sc_ah->ah_opmode == NL80211_IFTYPE_ADHOC) {
  1819. ath9k_hw_stoptxdma(sc->sc_ah, sc->beacon.beaconq);
  1820. ath_beacon_return(sc, avp);
  1821. }
  1822. sc->sc_flags &= ~SC_OP_BEACONS;
  1823. sc->sc_vaps[0] = NULL;
  1824. sc->sc_nvaps--;
  1825. }
  1826. static int ath9k_config(struct ieee80211_hw *hw, u32 changed)
  1827. {
  1828. struct ath_softc *sc = hw->priv;
  1829. struct ieee80211_conf *conf = &hw->conf;
  1830. mutex_lock(&sc->mutex);
  1831. if (changed & IEEE80211_CONF_CHANGE_PS) {
  1832. if (conf->flags & IEEE80211_CONF_PS) {
  1833. if ((sc->sc_imask & ATH9K_INT_TIM_TIMER) == 0) {
  1834. sc->sc_imask |= ATH9K_INT_TIM_TIMER;
  1835. ath9k_hw_set_interrupts(sc->sc_ah,
  1836. sc->sc_imask);
  1837. }
  1838. ath9k_hw_setrxabort(sc->sc_ah, 1);
  1839. ath9k_hw_setpower(sc->sc_ah, ATH9K_PM_NETWORK_SLEEP);
  1840. } else {
  1841. ath9k_hw_setpower(sc->sc_ah, ATH9K_PM_AWAKE);
  1842. ath9k_hw_setrxabort(sc->sc_ah, 0);
  1843. sc->sc_flags &= ~SC_OP_WAIT_FOR_BEACON;
  1844. if (sc->sc_imask & ATH9K_INT_TIM_TIMER) {
  1845. sc->sc_imask &= ~ATH9K_INT_TIM_TIMER;
  1846. ath9k_hw_set_interrupts(sc->sc_ah,
  1847. sc->sc_imask);
  1848. }
  1849. }
  1850. }
  1851. if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
  1852. struct ieee80211_channel *curchan = hw->conf.channel;
  1853. int pos = curchan->hw_value;
  1854. DPRINTF(sc, ATH_DBG_CONFIG, "Set channel: %d MHz\n",
  1855. curchan->center_freq);
  1856. /* XXX: remove me eventualy */
  1857. ath9k_update_ichannel(sc, &sc->sc_ah->ah_channels[pos]);
  1858. ath_update_chainmask(sc, conf_is_ht(conf));
  1859. if (ath_set_channel(sc, &sc->sc_ah->ah_channels[pos]) < 0) {
  1860. DPRINTF(sc, ATH_DBG_FATAL, "Unable to set channel\n");
  1861. mutex_unlock(&sc->mutex);
  1862. return -EINVAL;
  1863. }
  1864. }
  1865. if (changed & IEEE80211_CONF_CHANGE_POWER)
  1866. sc->sc_config.txpowlimit = 2 * conf->power_level;
  1867. mutex_unlock(&sc->mutex);
  1868. return 0;
  1869. }
  1870. static int ath9k_config_interface(struct ieee80211_hw *hw,
  1871. struct ieee80211_vif *vif,
  1872. struct ieee80211_if_conf *conf)
  1873. {
  1874. struct ath_softc *sc = hw->priv;
  1875. struct ath_hal *ah = sc->sc_ah;
  1876. struct ath_vap *avp = (void *)vif->drv_priv;
  1877. u32 rfilt = 0;
  1878. int error, i;
  1879. /* TODO: Need to decide which hw opmode to use for multi-interface
  1880. * cases */
  1881. if (vif->type == NL80211_IFTYPE_AP &&
  1882. ah->ah_opmode != NL80211_IFTYPE_AP) {
  1883. ah->ah_opmode = NL80211_IFTYPE_STATION;
  1884. ath9k_hw_setopmode(ah);
  1885. ath9k_hw_write_associd(ah, sc->sc_myaddr, 0);
  1886. /* Request full reset to get hw opmode changed properly */
  1887. sc->sc_flags |= SC_OP_FULL_RESET;
  1888. }
  1889. if ((conf->changed & IEEE80211_IFCC_BSSID) &&
  1890. !is_zero_ether_addr(conf->bssid)) {
  1891. switch (vif->type) {
  1892. case NL80211_IFTYPE_STATION:
  1893. case NL80211_IFTYPE_ADHOC:
  1894. /* Set BSSID */
  1895. memcpy(sc->sc_curbssid, conf->bssid, ETH_ALEN);
  1896. sc->sc_curaid = 0;
  1897. ath9k_hw_write_associd(sc->sc_ah, sc->sc_curbssid,
  1898. sc->sc_curaid);
  1899. /* Set aggregation protection mode parameters */
  1900. sc->sc_config.ath_aggr_prot = 0;
  1901. DPRINTF(sc, ATH_DBG_CONFIG,
  1902. "RX filter 0x%x bssid %pM aid 0x%x\n",
  1903. rfilt, sc->sc_curbssid, sc->sc_curaid);
  1904. /* need to reconfigure the beacon */
  1905. sc->sc_flags &= ~SC_OP_BEACONS ;
  1906. break;
  1907. default:
  1908. break;
  1909. }
  1910. }
  1911. if ((conf->changed & IEEE80211_IFCC_BEACON) &&
  1912. ((vif->type == NL80211_IFTYPE_ADHOC) ||
  1913. (vif->type == NL80211_IFTYPE_AP))) {
  1914. /*
  1915. * Allocate and setup the beacon frame.
  1916. *
  1917. * Stop any previous beacon DMA. This may be
  1918. * necessary, for example, when an ibss merge
  1919. * causes reconfiguration; we may be called
  1920. * with beacon transmission active.
  1921. */
  1922. ath9k_hw_stoptxdma(sc->sc_ah, sc->beacon.beaconq);
  1923. error = ath_beacon_alloc(sc, 0);
  1924. if (error != 0)
  1925. return error;
  1926. ath_beacon_sync(sc, 0);
  1927. }
  1928. /* Check for WLAN_CAPABILITY_PRIVACY ? */
  1929. if ((avp->av_opmode != NL80211_IFTYPE_STATION)) {
  1930. for (i = 0; i < IEEE80211_WEP_NKID; i++)
  1931. if (ath9k_hw_keyisvalid(sc->sc_ah, (u16)i))
  1932. ath9k_hw_keysetmac(sc->sc_ah,
  1933. (u16)i,
  1934. sc->sc_curbssid);
  1935. }
  1936. /* Only legacy IBSS for now */
  1937. if (vif->type == NL80211_IFTYPE_ADHOC)
  1938. ath_update_chainmask(sc, 0);
  1939. return 0;
  1940. }
  1941. #define SUPPORTED_FILTERS \
  1942. (FIF_PROMISC_IN_BSS | \
  1943. FIF_ALLMULTI | \
  1944. FIF_CONTROL | \
  1945. FIF_OTHER_BSS | \
  1946. FIF_BCN_PRBRESP_PROMISC | \
  1947. FIF_FCSFAIL)
  1948. /* FIXME: sc->sc_full_reset ? */
  1949. static void ath9k_configure_filter(struct ieee80211_hw *hw,
  1950. unsigned int changed_flags,
  1951. unsigned int *total_flags,
  1952. int mc_count,
  1953. struct dev_mc_list *mclist)
  1954. {
  1955. struct ath_softc *sc = hw->priv;
  1956. u32 rfilt;
  1957. changed_flags &= SUPPORTED_FILTERS;
  1958. *total_flags &= SUPPORTED_FILTERS;
  1959. sc->rx.rxfilter = *total_flags;
  1960. rfilt = ath_calcrxfilter(sc);
  1961. ath9k_hw_setrxfilter(sc->sc_ah, rfilt);
  1962. if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
  1963. if (*total_flags & FIF_BCN_PRBRESP_PROMISC)
  1964. ath9k_hw_write_associd(sc->sc_ah, ath_bcast_mac, 0);
  1965. }
  1966. DPRINTF(sc, ATH_DBG_CONFIG, "Set HW RX filter: 0x%x\n", sc->rx.rxfilter);
  1967. }
  1968. static void ath9k_sta_notify(struct ieee80211_hw *hw,
  1969. struct ieee80211_vif *vif,
  1970. enum sta_notify_cmd cmd,
  1971. struct ieee80211_sta *sta)
  1972. {
  1973. struct ath_softc *sc = hw->priv;
  1974. switch (cmd) {
  1975. case STA_NOTIFY_ADD:
  1976. ath_node_attach(sc, sta);
  1977. break;
  1978. case STA_NOTIFY_REMOVE:
  1979. ath_node_detach(sc, sta);
  1980. break;
  1981. default:
  1982. break;
  1983. }
  1984. }
  1985. static int ath9k_conf_tx(struct ieee80211_hw *hw,
  1986. u16 queue,
  1987. const struct ieee80211_tx_queue_params *params)
  1988. {
  1989. struct ath_softc *sc = hw->priv;
  1990. struct ath9k_tx_queue_info qi;
  1991. int ret = 0, qnum;
  1992. if (queue >= WME_NUM_AC)
  1993. return 0;
  1994. qi.tqi_aifs = params->aifs;
  1995. qi.tqi_cwmin = params->cw_min;
  1996. qi.tqi_cwmax = params->cw_max;
  1997. qi.tqi_burstTime = params->txop;
  1998. qnum = ath_get_hal_qnum(queue, sc);
  1999. DPRINTF(sc, ATH_DBG_CONFIG,
  2000. "Configure tx [queue/halq] [%d/%d], "
  2001. "aifs: %d, cw_min: %d, cw_max: %d, txop: %d\n",
  2002. queue, qnum, params->aifs, params->cw_min,
  2003. params->cw_max, params->txop);
  2004. ret = ath_txq_update(sc, qnum, &qi);
  2005. if (ret)
  2006. DPRINTF(sc, ATH_DBG_FATAL, "TXQ Update failed\n");
  2007. return ret;
  2008. }
  2009. static int ath9k_set_key(struct ieee80211_hw *hw,
  2010. enum set_key_cmd cmd,
  2011. struct ieee80211_vif *vif,
  2012. struct ieee80211_sta *sta,
  2013. struct ieee80211_key_conf *key)
  2014. {
  2015. struct ath_softc *sc = hw->priv;
  2016. int ret = 0;
  2017. ath9k_ps_wakeup(sc);
  2018. DPRINTF(sc, ATH_DBG_KEYCACHE, "Set HW Key\n");
  2019. switch (cmd) {
  2020. case SET_KEY:
  2021. ret = ath_key_config(sc, sta, key);
  2022. if (ret >= 0) {
  2023. key->hw_key_idx = ret;
  2024. /* push IV and Michael MIC generation to stack */
  2025. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  2026. if (key->alg == ALG_TKIP)
  2027. key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
  2028. if (sc->sc_ah->sw_mgmt_crypto && key->alg == ALG_CCMP)
  2029. key->flags |= IEEE80211_KEY_FLAG_SW_MGMT;
  2030. ret = 0;
  2031. }
  2032. break;
  2033. case DISABLE_KEY:
  2034. ath_key_delete(sc, key);
  2035. break;
  2036. default:
  2037. ret = -EINVAL;
  2038. }
  2039. ath9k_ps_restore(sc);
  2040. return ret;
  2041. }
  2042. static void ath9k_bss_info_changed(struct ieee80211_hw *hw,
  2043. struct ieee80211_vif *vif,
  2044. struct ieee80211_bss_conf *bss_conf,
  2045. u32 changed)
  2046. {
  2047. struct ath_softc *sc = hw->priv;
  2048. if (changed & BSS_CHANGED_ERP_PREAMBLE) {
  2049. DPRINTF(sc, ATH_DBG_CONFIG, "BSS Changed PREAMBLE %d\n",
  2050. bss_conf->use_short_preamble);
  2051. if (bss_conf->use_short_preamble)
  2052. sc->sc_flags |= SC_OP_PREAMBLE_SHORT;
  2053. else
  2054. sc->sc_flags &= ~SC_OP_PREAMBLE_SHORT;
  2055. }
  2056. if (changed & BSS_CHANGED_ERP_CTS_PROT) {
  2057. DPRINTF(sc, ATH_DBG_CONFIG, "BSS Changed CTS PROT %d\n",
  2058. bss_conf->use_cts_prot);
  2059. if (bss_conf->use_cts_prot &&
  2060. hw->conf.channel->band != IEEE80211_BAND_5GHZ)
  2061. sc->sc_flags |= SC_OP_PROTECT_ENABLE;
  2062. else
  2063. sc->sc_flags &= ~SC_OP_PROTECT_ENABLE;
  2064. }
  2065. if (changed & BSS_CHANGED_ASSOC) {
  2066. DPRINTF(sc, ATH_DBG_CONFIG, "BSS Changed ASSOC %d\n",
  2067. bss_conf->assoc);
  2068. ath9k_bss_assoc_info(sc, vif, bss_conf);
  2069. }
  2070. }
  2071. static u64 ath9k_get_tsf(struct ieee80211_hw *hw)
  2072. {
  2073. u64 tsf;
  2074. struct ath_softc *sc = hw->priv;
  2075. struct ath_hal *ah = sc->sc_ah;
  2076. tsf = ath9k_hw_gettsf64(ah);
  2077. return tsf;
  2078. }
  2079. static void ath9k_set_tsf(struct ieee80211_hw *hw, u64 tsf)
  2080. {
  2081. struct ath_softc *sc = hw->priv;
  2082. struct ath_hal *ah = sc->sc_ah;
  2083. ath9k_hw_settsf64(ah, tsf);
  2084. }
  2085. static void ath9k_reset_tsf(struct ieee80211_hw *hw)
  2086. {
  2087. struct ath_softc *sc = hw->priv;
  2088. struct ath_hal *ah = sc->sc_ah;
  2089. ath9k_hw_reset_tsf(ah);
  2090. }
  2091. static int ath9k_ampdu_action(struct ieee80211_hw *hw,
  2092. enum ieee80211_ampdu_mlme_action action,
  2093. struct ieee80211_sta *sta,
  2094. u16 tid, u16 *ssn)
  2095. {
  2096. struct ath_softc *sc = hw->priv;
  2097. int ret = 0;
  2098. switch (action) {
  2099. case IEEE80211_AMPDU_RX_START:
  2100. if (!(sc->sc_flags & SC_OP_RXAGGR))
  2101. ret = -ENOTSUPP;
  2102. break;
  2103. case IEEE80211_AMPDU_RX_STOP:
  2104. break;
  2105. case IEEE80211_AMPDU_TX_START:
  2106. ret = ath_tx_aggr_start(sc, sta, tid, ssn);
  2107. if (ret < 0)
  2108. DPRINTF(sc, ATH_DBG_FATAL,
  2109. "Unable to start TX aggregation\n");
  2110. else
  2111. ieee80211_start_tx_ba_cb_irqsafe(hw, sta->addr, tid);
  2112. break;
  2113. case IEEE80211_AMPDU_TX_STOP:
  2114. ret = ath_tx_aggr_stop(sc, sta, tid);
  2115. if (ret < 0)
  2116. DPRINTF(sc, ATH_DBG_FATAL,
  2117. "Unable to stop TX aggregation\n");
  2118. ieee80211_stop_tx_ba_cb_irqsafe(hw, sta->addr, tid);
  2119. break;
  2120. case IEEE80211_AMPDU_TX_RESUME:
  2121. ath_tx_aggr_resume(sc, sta, tid);
  2122. break;
  2123. default:
  2124. DPRINTF(sc, ATH_DBG_FATAL, "Unknown AMPDU action\n");
  2125. }
  2126. return ret;
  2127. }
  2128. struct ieee80211_ops ath9k_ops = {
  2129. .tx = ath9k_tx,
  2130. .start = ath9k_start,
  2131. .stop = ath9k_stop,
  2132. .add_interface = ath9k_add_interface,
  2133. .remove_interface = ath9k_remove_interface,
  2134. .config = ath9k_config,
  2135. .config_interface = ath9k_config_interface,
  2136. .configure_filter = ath9k_configure_filter,
  2137. .sta_notify = ath9k_sta_notify,
  2138. .conf_tx = ath9k_conf_tx,
  2139. .bss_info_changed = ath9k_bss_info_changed,
  2140. .set_key = ath9k_set_key,
  2141. .get_tsf = ath9k_get_tsf,
  2142. .set_tsf = ath9k_set_tsf,
  2143. .reset_tsf = ath9k_reset_tsf,
  2144. .ampdu_action = ath9k_ampdu_action,
  2145. };
  2146. static struct {
  2147. u32 version;
  2148. const char * name;
  2149. } ath_mac_bb_names[] = {
  2150. { AR_SREV_VERSION_5416_PCI, "5416" },
  2151. { AR_SREV_VERSION_5416_PCIE, "5418" },
  2152. { AR_SREV_VERSION_9100, "9100" },
  2153. { AR_SREV_VERSION_9160, "9160" },
  2154. { AR_SREV_VERSION_9280, "9280" },
  2155. { AR_SREV_VERSION_9285, "9285" }
  2156. };
  2157. static struct {
  2158. u16 version;
  2159. const char * name;
  2160. } ath_rf_names[] = {
  2161. { 0, "5133" },
  2162. { AR_RAD5133_SREV_MAJOR, "5133" },
  2163. { AR_RAD5122_SREV_MAJOR, "5122" },
  2164. { AR_RAD2133_SREV_MAJOR, "2133" },
  2165. { AR_RAD2122_SREV_MAJOR, "2122" }
  2166. };
  2167. /*
  2168. * Return the MAC/BB name. "????" is returned if the MAC/BB is unknown.
  2169. */
  2170. const char *
  2171. ath_mac_bb_name(u32 mac_bb_version)
  2172. {
  2173. int i;
  2174. for (i=0; i<ARRAY_SIZE(ath_mac_bb_names); i++) {
  2175. if (ath_mac_bb_names[i].version == mac_bb_version) {
  2176. return ath_mac_bb_names[i].name;
  2177. }
  2178. }
  2179. return "????";
  2180. }
  2181. /*
  2182. * Return the RF name. "????" is returned if the RF is unknown.
  2183. */
  2184. const char *
  2185. ath_rf_name(u16 rf_version)
  2186. {
  2187. int i;
  2188. for (i=0; i<ARRAY_SIZE(ath_rf_names); i++) {
  2189. if (ath_rf_names[i].version == rf_version) {
  2190. return ath_rf_names[i].name;
  2191. }
  2192. }
  2193. return "????";
  2194. }
  2195. static int __init ath9k_init(void)
  2196. {
  2197. int error;
  2198. /* Register rate control algorithm */
  2199. error = ath_rate_control_register();
  2200. if (error != 0) {
  2201. printk(KERN_ERR
  2202. "ath9k: Unable to register rate control "
  2203. "algorithm: %d\n",
  2204. error);
  2205. goto err_out;
  2206. }
  2207. error = ath_pci_init();
  2208. if (error < 0) {
  2209. printk(KERN_ERR
  2210. "ath9k: No PCI devices found, driver not installed.\n");
  2211. error = -ENODEV;
  2212. goto err_rate_unregister;
  2213. }
  2214. error = ath_ahb_init();
  2215. if (error < 0) {
  2216. error = -ENODEV;
  2217. goto err_pci_exit;
  2218. }
  2219. return 0;
  2220. err_pci_exit:
  2221. ath_pci_exit();
  2222. err_rate_unregister:
  2223. ath_rate_control_unregister();
  2224. err_out:
  2225. return error;
  2226. }
  2227. module_init(ath9k_init);
  2228. static void __exit ath9k_exit(void)
  2229. {
  2230. ath_ahb_exit();
  2231. ath_pci_exit();
  2232. ath_rate_control_unregister();
  2233. printk(KERN_INFO "%s: Driver unloaded\n", dev_info);
  2234. }
  2235. module_exit(ath9k_exit);