main.c 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613
  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 ieee80211_sta_ht_cap *ht_info)
  758. {
  759. #define ATH9K_HT_CAP_MAXRXAMPDU_65536 0x3 /* 2 ^ 16 */
  760. #define ATH9K_HT_CAP_MPDUDENSITY_8 0x6 /* 8 usec */
  761. ht_info->ht_supported = true;
  762. ht_info->cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
  763. IEEE80211_HT_CAP_SM_PS |
  764. IEEE80211_HT_CAP_SGI_40 |
  765. IEEE80211_HT_CAP_DSSSCCK40;
  766. ht_info->ampdu_factor = ATH9K_HT_CAP_MAXRXAMPDU_65536;
  767. ht_info->ampdu_density = ATH9K_HT_CAP_MPDUDENSITY_8;
  768. /* set up supported mcs set */
  769. memset(&ht_info->mcs, 0, sizeof(ht_info->mcs));
  770. ht_info->mcs.rx_mask[0] = 0xff;
  771. ht_info->mcs.rx_mask[1] = 0xff;
  772. ht_info->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
  773. }
  774. static void ath9k_bss_assoc_info(struct ath_softc *sc,
  775. struct ieee80211_vif *vif,
  776. struct ieee80211_bss_conf *bss_conf)
  777. {
  778. struct ath_vap *avp = (void *)vif->drv_priv;
  779. if (bss_conf->assoc) {
  780. DPRINTF(sc, ATH_DBG_CONFIG, "Bss Info ASSOC %d, bssid: %pM\n",
  781. bss_conf->aid, sc->sc_curbssid);
  782. /* New association, store aid */
  783. if (avp->av_opmode == NL80211_IFTYPE_STATION) {
  784. sc->sc_curaid = bss_conf->aid;
  785. ath9k_hw_write_associd(sc->sc_ah, sc->sc_curbssid,
  786. sc->sc_curaid);
  787. }
  788. /* Configure the beacon */
  789. ath_beacon_config(sc, 0);
  790. sc->sc_flags |= SC_OP_BEACONS;
  791. /* Reset rssi stats */
  792. sc->sc_halstats.ns_avgbrssi = ATH_RSSI_DUMMY_MARKER;
  793. sc->sc_halstats.ns_avgrssi = ATH_RSSI_DUMMY_MARKER;
  794. sc->sc_halstats.ns_avgtxrssi = ATH_RSSI_DUMMY_MARKER;
  795. sc->sc_halstats.ns_avgtxrate = ATH_RATE_DUMMY_MARKER;
  796. /* Start ANI */
  797. mod_timer(&sc->sc_ani.timer,
  798. jiffies + msecs_to_jiffies(ATH_ANI_POLLINTERVAL));
  799. } else {
  800. DPRINTF(sc, ATH_DBG_CONFIG, "Bss Info DISSOC\n");
  801. sc->sc_curaid = 0;
  802. }
  803. }
  804. /********************************/
  805. /* LED functions */
  806. /********************************/
  807. static void ath_led_brightness(struct led_classdev *led_cdev,
  808. enum led_brightness brightness)
  809. {
  810. struct ath_led *led = container_of(led_cdev, struct ath_led, led_cdev);
  811. struct ath_softc *sc = led->sc;
  812. switch (brightness) {
  813. case LED_OFF:
  814. if (led->led_type == ATH_LED_ASSOC ||
  815. led->led_type == ATH_LED_RADIO)
  816. sc->sc_flags &= ~SC_OP_LED_ASSOCIATED;
  817. ath9k_hw_set_gpio(sc->sc_ah, ATH_LED_PIN,
  818. (led->led_type == ATH_LED_RADIO) ? 1 :
  819. !!(sc->sc_flags & SC_OP_LED_ASSOCIATED));
  820. break;
  821. case LED_FULL:
  822. if (led->led_type == ATH_LED_ASSOC)
  823. sc->sc_flags |= SC_OP_LED_ASSOCIATED;
  824. ath9k_hw_set_gpio(sc->sc_ah, ATH_LED_PIN, 0);
  825. break;
  826. default:
  827. break;
  828. }
  829. }
  830. static int ath_register_led(struct ath_softc *sc, struct ath_led *led,
  831. char *trigger)
  832. {
  833. int ret;
  834. led->sc = sc;
  835. led->led_cdev.name = led->name;
  836. led->led_cdev.default_trigger = trigger;
  837. led->led_cdev.brightness_set = ath_led_brightness;
  838. ret = led_classdev_register(wiphy_dev(sc->hw->wiphy), &led->led_cdev);
  839. if (ret)
  840. DPRINTF(sc, ATH_DBG_FATAL,
  841. "Failed to register led:%s", led->name);
  842. else
  843. led->registered = 1;
  844. return ret;
  845. }
  846. static void ath_unregister_led(struct ath_led *led)
  847. {
  848. if (led->registered) {
  849. led_classdev_unregister(&led->led_cdev);
  850. led->registered = 0;
  851. }
  852. }
  853. static void ath_deinit_leds(struct ath_softc *sc)
  854. {
  855. ath_unregister_led(&sc->assoc_led);
  856. sc->sc_flags &= ~SC_OP_LED_ASSOCIATED;
  857. ath_unregister_led(&sc->tx_led);
  858. ath_unregister_led(&sc->rx_led);
  859. ath_unregister_led(&sc->radio_led);
  860. ath9k_hw_set_gpio(sc->sc_ah, ATH_LED_PIN, 1);
  861. }
  862. static void ath_init_leds(struct ath_softc *sc)
  863. {
  864. char *trigger;
  865. int ret;
  866. /* Configure gpio 1 for output */
  867. ath9k_hw_cfg_output(sc->sc_ah, ATH_LED_PIN,
  868. AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
  869. /* LED off, active low */
  870. ath9k_hw_set_gpio(sc->sc_ah, ATH_LED_PIN, 1);
  871. trigger = ieee80211_get_radio_led_name(sc->hw);
  872. snprintf(sc->radio_led.name, sizeof(sc->radio_led.name),
  873. "ath9k-%s:radio", wiphy_name(sc->hw->wiphy));
  874. ret = ath_register_led(sc, &sc->radio_led, trigger);
  875. sc->radio_led.led_type = ATH_LED_RADIO;
  876. if (ret)
  877. goto fail;
  878. trigger = ieee80211_get_assoc_led_name(sc->hw);
  879. snprintf(sc->assoc_led.name, sizeof(sc->assoc_led.name),
  880. "ath9k-%s:assoc", wiphy_name(sc->hw->wiphy));
  881. ret = ath_register_led(sc, &sc->assoc_led, trigger);
  882. sc->assoc_led.led_type = ATH_LED_ASSOC;
  883. if (ret)
  884. goto fail;
  885. trigger = ieee80211_get_tx_led_name(sc->hw);
  886. snprintf(sc->tx_led.name, sizeof(sc->tx_led.name),
  887. "ath9k-%s:tx", wiphy_name(sc->hw->wiphy));
  888. ret = ath_register_led(sc, &sc->tx_led, trigger);
  889. sc->tx_led.led_type = ATH_LED_TX;
  890. if (ret)
  891. goto fail;
  892. trigger = ieee80211_get_rx_led_name(sc->hw);
  893. snprintf(sc->rx_led.name, sizeof(sc->rx_led.name),
  894. "ath9k-%s:rx", wiphy_name(sc->hw->wiphy));
  895. ret = ath_register_led(sc, &sc->rx_led, trigger);
  896. sc->rx_led.led_type = ATH_LED_RX;
  897. if (ret)
  898. goto fail;
  899. return;
  900. fail:
  901. ath_deinit_leds(sc);
  902. }
  903. #if defined(CONFIG_RFKILL) || defined(CONFIG_RFKILL_MODULE)
  904. /*******************/
  905. /* Rfkill */
  906. /*******************/
  907. static void ath_radio_enable(struct ath_softc *sc)
  908. {
  909. struct ath_hal *ah = sc->sc_ah;
  910. struct ieee80211_channel *channel = sc->hw->conf.channel;
  911. int r;
  912. ath9k_ps_wakeup(sc);
  913. spin_lock_bh(&sc->sc_resetlock);
  914. r = ath9k_hw_reset(ah, ah->ah_curchan, false);
  915. if (r) {
  916. DPRINTF(sc, ATH_DBG_FATAL,
  917. "Unable to reset channel %u (%uMhz) ",
  918. "reset status %u\n",
  919. channel->center_freq, r);
  920. }
  921. spin_unlock_bh(&sc->sc_resetlock);
  922. ath_update_txpow(sc);
  923. if (ath_startrecv(sc) != 0) {
  924. DPRINTF(sc, ATH_DBG_FATAL,
  925. "Unable to restart recv logic\n");
  926. return;
  927. }
  928. if (sc->sc_flags & SC_OP_BEACONS)
  929. ath_beacon_config(sc, ATH_IF_ID_ANY); /* restart beacons */
  930. /* Re-Enable interrupts */
  931. ath9k_hw_set_interrupts(ah, sc->sc_imask);
  932. /* Enable LED */
  933. ath9k_hw_cfg_output(ah, ATH_LED_PIN,
  934. AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
  935. ath9k_hw_set_gpio(ah, ATH_LED_PIN, 0);
  936. ieee80211_wake_queues(sc->hw);
  937. ath9k_ps_restore(sc);
  938. }
  939. static void ath_radio_disable(struct ath_softc *sc)
  940. {
  941. struct ath_hal *ah = sc->sc_ah;
  942. struct ieee80211_channel *channel = sc->hw->conf.channel;
  943. int r;
  944. ath9k_ps_wakeup(sc);
  945. ieee80211_stop_queues(sc->hw);
  946. /* Disable LED */
  947. ath9k_hw_set_gpio(ah, ATH_LED_PIN, 1);
  948. ath9k_hw_cfg_gpio_input(ah, ATH_LED_PIN);
  949. /* Disable interrupts */
  950. ath9k_hw_set_interrupts(ah, 0);
  951. ath_drain_all_txq(sc, false); /* clear pending tx frames */
  952. ath_stoprecv(sc); /* turn off frame recv */
  953. ath_flushrecv(sc); /* flush recv queue */
  954. spin_lock_bh(&sc->sc_resetlock);
  955. r = ath9k_hw_reset(ah, ah->ah_curchan, false);
  956. if (r) {
  957. DPRINTF(sc, ATH_DBG_FATAL,
  958. "Unable to reset channel %u (%uMhz) "
  959. "reset status %u\n",
  960. channel->center_freq, r);
  961. }
  962. spin_unlock_bh(&sc->sc_resetlock);
  963. ath9k_hw_phy_disable(ah);
  964. ath9k_hw_setpower(ah, ATH9K_PM_FULL_SLEEP);
  965. ath9k_ps_restore(sc);
  966. }
  967. static bool ath_is_rfkill_set(struct ath_softc *sc)
  968. {
  969. struct ath_hal *ah = sc->sc_ah;
  970. return ath9k_hw_gpio_get(ah, ah->ah_rfkill_gpio) ==
  971. ah->ah_rfkill_polarity;
  972. }
  973. /* h/w rfkill poll function */
  974. static void ath_rfkill_poll(struct work_struct *work)
  975. {
  976. struct ath_softc *sc = container_of(work, struct ath_softc,
  977. rf_kill.rfkill_poll.work);
  978. bool radio_on;
  979. if (sc->sc_flags & SC_OP_INVALID)
  980. return;
  981. radio_on = !ath_is_rfkill_set(sc);
  982. /*
  983. * enable/disable radio only when there is a
  984. * state change in RF switch
  985. */
  986. if (radio_on == !!(sc->sc_flags & SC_OP_RFKILL_HW_BLOCKED)) {
  987. enum rfkill_state state;
  988. if (sc->sc_flags & SC_OP_RFKILL_SW_BLOCKED) {
  989. state = radio_on ? RFKILL_STATE_SOFT_BLOCKED
  990. : RFKILL_STATE_HARD_BLOCKED;
  991. } else if (radio_on) {
  992. ath_radio_enable(sc);
  993. state = RFKILL_STATE_UNBLOCKED;
  994. } else {
  995. ath_radio_disable(sc);
  996. state = RFKILL_STATE_HARD_BLOCKED;
  997. }
  998. if (state == RFKILL_STATE_HARD_BLOCKED)
  999. sc->sc_flags |= SC_OP_RFKILL_HW_BLOCKED;
  1000. else
  1001. sc->sc_flags &= ~SC_OP_RFKILL_HW_BLOCKED;
  1002. rfkill_force_state(sc->rf_kill.rfkill, state);
  1003. }
  1004. queue_delayed_work(sc->hw->workqueue, &sc->rf_kill.rfkill_poll,
  1005. msecs_to_jiffies(ATH_RFKILL_POLL_INTERVAL));
  1006. }
  1007. /* s/w rfkill handler */
  1008. static int ath_sw_toggle_radio(void *data, enum rfkill_state state)
  1009. {
  1010. struct ath_softc *sc = data;
  1011. switch (state) {
  1012. case RFKILL_STATE_SOFT_BLOCKED:
  1013. if (!(sc->sc_flags & (SC_OP_RFKILL_HW_BLOCKED |
  1014. SC_OP_RFKILL_SW_BLOCKED)))
  1015. ath_radio_disable(sc);
  1016. sc->sc_flags |= SC_OP_RFKILL_SW_BLOCKED;
  1017. return 0;
  1018. case RFKILL_STATE_UNBLOCKED:
  1019. if ((sc->sc_flags & SC_OP_RFKILL_SW_BLOCKED)) {
  1020. sc->sc_flags &= ~SC_OP_RFKILL_SW_BLOCKED;
  1021. if (sc->sc_flags & SC_OP_RFKILL_HW_BLOCKED) {
  1022. DPRINTF(sc, ATH_DBG_FATAL, "Can't turn on the"
  1023. "radio as it is disabled by h/w\n");
  1024. return -EPERM;
  1025. }
  1026. ath_radio_enable(sc);
  1027. }
  1028. return 0;
  1029. default:
  1030. return -EINVAL;
  1031. }
  1032. }
  1033. /* Init s/w rfkill */
  1034. static int ath_init_sw_rfkill(struct ath_softc *sc)
  1035. {
  1036. sc->rf_kill.rfkill = rfkill_allocate(wiphy_dev(sc->hw->wiphy),
  1037. RFKILL_TYPE_WLAN);
  1038. if (!sc->rf_kill.rfkill) {
  1039. DPRINTF(sc, ATH_DBG_FATAL, "Failed to allocate rfkill\n");
  1040. return -ENOMEM;
  1041. }
  1042. snprintf(sc->rf_kill.rfkill_name, sizeof(sc->rf_kill.rfkill_name),
  1043. "ath9k-%s:rfkill", wiphy_name(sc->hw->wiphy));
  1044. sc->rf_kill.rfkill->name = sc->rf_kill.rfkill_name;
  1045. sc->rf_kill.rfkill->data = sc;
  1046. sc->rf_kill.rfkill->toggle_radio = ath_sw_toggle_radio;
  1047. sc->rf_kill.rfkill->state = RFKILL_STATE_UNBLOCKED;
  1048. sc->rf_kill.rfkill->user_claim_unsupported = 1;
  1049. return 0;
  1050. }
  1051. /* Deinitialize rfkill */
  1052. static void ath_deinit_rfkill(struct ath_softc *sc)
  1053. {
  1054. if (sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
  1055. cancel_delayed_work_sync(&sc->rf_kill.rfkill_poll);
  1056. if (sc->sc_flags & SC_OP_RFKILL_REGISTERED) {
  1057. rfkill_unregister(sc->rf_kill.rfkill);
  1058. sc->sc_flags &= ~SC_OP_RFKILL_REGISTERED;
  1059. sc->rf_kill.rfkill = NULL;
  1060. }
  1061. }
  1062. static int ath_start_rfkill_poll(struct ath_softc *sc)
  1063. {
  1064. if (sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
  1065. queue_delayed_work(sc->hw->workqueue,
  1066. &sc->rf_kill.rfkill_poll, 0);
  1067. if (!(sc->sc_flags & SC_OP_RFKILL_REGISTERED)) {
  1068. if (rfkill_register(sc->rf_kill.rfkill)) {
  1069. DPRINTF(sc, ATH_DBG_FATAL,
  1070. "Unable to register rfkill\n");
  1071. rfkill_free(sc->rf_kill.rfkill);
  1072. /* Deinitialize the device */
  1073. ath_cleanup(sc);
  1074. return -EIO;
  1075. } else {
  1076. sc->sc_flags |= SC_OP_RFKILL_REGISTERED;
  1077. }
  1078. }
  1079. return 0;
  1080. }
  1081. #endif /* CONFIG_RFKILL */
  1082. void ath_cleanup(struct ath_softc *sc)
  1083. {
  1084. ath_detach(sc);
  1085. free_irq(sc->irq, sc);
  1086. ath_bus_cleanup(sc);
  1087. ieee80211_free_hw(sc->hw);
  1088. }
  1089. void ath_detach(struct ath_softc *sc)
  1090. {
  1091. struct ieee80211_hw *hw = sc->hw;
  1092. int i = 0;
  1093. ath9k_ps_wakeup(sc);
  1094. DPRINTF(sc, ATH_DBG_CONFIG, "Detach ATH hw\n");
  1095. #if defined(CONFIG_RFKILL) || defined(CONFIG_RFKILL_MODULE)
  1096. ath_deinit_rfkill(sc);
  1097. #endif
  1098. ath_deinit_leds(sc);
  1099. ieee80211_unregister_hw(hw);
  1100. ath_rx_cleanup(sc);
  1101. ath_tx_cleanup(sc);
  1102. tasklet_kill(&sc->intr_tq);
  1103. tasklet_kill(&sc->bcon_tasklet);
  1104. if (!(sc->sc_flags & SC_OP_INVALID))
  1105. ath9k_hw_setpower(sc->sc_ah, ATH9K_PM_AWAKE);
  1106. /* cleanup tx queues */
  1107. for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++)
  1108. if (ATH_TXQ_SETUP(sc, i))
  1109. ath_tx_cleanupq(sc, &sc->tx.txq[i]);
  1110. ath9k_hw_detach(sc->sc_ah);
  1111. ath9k_exit_debug(sc);
  1112. ath9k_ps_restore(sc);
  1113. }
  1114. static int ath_init(u16 devid, struct ath_softc *sc)
  1115. {
  1116. struct ath_hal *ah = NULL;
  1117. int status;
  1118. int error = 0, i;
  1119. int csz = 0;
  1120. /* XXX: hardware will not be ready until ath_open() being called */
  1121. sc->sc_flags |= SC_OP_INVALID;
  1122. if (ath9k_init_debug(sc) < 0)
  1123. printk(KERN_ERR "Unable to create debugfs files\n");
  1124. spin_lock_init(&sc->sc_resetlock);
  1125. mutex_init(&sc->mutex);
  1126. tasklet_init(&sc->intr_tq, ath9k_tasklet, (unsigned long)sc);
  1127. tasklet_init(&sc->bcon_tasklet, ath9k_beacon_tasklet,
  1128. (unsigned long)sc);
  1129. /*
  1130. * Cache line size is used to size and align various
  1131. * structures used to communicate with the hardware.
  1132. */
  1133. ath_read_cachesize(sc, &csz);
  1134. /* XXX assert csz is non-zero */
  1135. sc->sc_cachelsz = csz << 2; /* convert to bytes */
  1136. ah = ath9k_hw_attach(devid, sc, sc->mem, &status);
  1137. if (ah == NULL) {
  1138. DPRINTF(sc, ATH_DBG_FATAL,
  1139. "Unable to attach hardware; HAL status %d\n", status);
  1140. error = -ENXIO;
  1141. goto bad;
  1142. }
  1143. sc->sc_ah = ah;
  1144. /* Get the hardware key cache size. */
  1145. sc->sc_keymax = ah->ah_caps.keycache_size;
  1146. if (sc->sc_keymax > ATH_KEYMAX) {
  1147. DPRINTF(sc, ATH_DBG_KEYCACHE,
  1148. "Warning, using only %u entries in %u key cache\n",
  1149. ATH_KEYMAX, sc->sc_keymax);
  1150. sc->sc_keymax = ATH_KEYMAX;
  1151. }
  1152. /*
  1153. * Reset the key cache since some parts do not
  1154. * reset the contents on initial power up.
  1155. */
  1156. for (i = 0; i < sc->sc_keymax; i++)
  1157. ath9k_hw_keyreset(ah, (u16) i);
  1158. if (ath9k_regd_init(sc->sc_ah))
  1159. goto bad;
  1160. /* default to MONITOR mode */
  1161. sc->sc_ah->ah_opmode = NL80211_IFTYPE_MONITOR;
  1162. /* Setup rate tables */
  1163. ath_rate_attach(sc);
  1164. ath_setup_rates(sc, IEEE80211_BAND_2GHZ);
  1165. ath_setup_rates(sc, IEEE80211_BAND_5GHZ);
  1166. /*
  1167. * Allocate hardware transmit queues: one queue for
  1168. * beacon frames and one data queue for each QoS
  1169. * priority. Note that the hal handles reseting
  1170. * these queues at the needed time.
  1171. */
  1172. sc->beacon.beaconq = ath_beaconq_setup(ah);
  1173. if (sc->beacon.beaconq == -1) {
  1174. DPRINTF(sc, ATH_DBG_FATAL,
  1175. "Unable to setup a beacon xmit queue\n");
  1176. error = -EIO;
  1177. goto bad2;
  1178. }
  1179. sc->beacon.cabq = ath_txq_setup(sc, ATH9K_TX_QUEUE_CAB, 0);
  1180. if (sc->beacon.cabq == NULL) {
  1181. DPRINTF(sc, ATH_DBG_FATAL,
  1182. "Unable to setup CAB xmit queue\n");
  1183. error = -EIO;
  1184. goto bad2;
  1185. }
  1186. sc->sc_config.cabqReadytime = ATH_CABQ_READY_TIME;
  1187. ath_cabq_update(sc);
  1188. for (i = 0; i < ARRAY_SIZE(sc->tx.hwq_map); i++)
  1189. sc->tx.hwq_map[i] = -1;
  1190. /* Setup data queues */
  1191. /* NB: ensure BK queue is the lowest priority h/w queue */
  1192. if (!ath_tx_setup(sc, ATH9K_WME_AC_BK)) {
  1193. DPRINTF(sc, ATH_DBG_FATAL,
  1194. "Unable to setup xmit queue for BK traffic\n");
  1195. error = -EIO;
  1196. goto bad2;
  1197. }
  1198. if (!ath_tx_setup(sc, ATH9K_WME_AC_BE)) {
  1199. DPRINTF(sc, ATH_DBG_FATAL,
  1200. "Unable to setup xmit queue for BE traffic\n");
  1201. error = -EIO;
  1202. goto bad2;
  1203. }
  1204. if (!ath_tx_setup(sc, ATH9K_WME_AC_VI)) {
  1205. DPRINTF(sc, ATH_DBG_FATAL,
  1206. "Unable to setup xmit queue for VI traffic\n");
  1207. error = -EIO;
  1208. goto bad2;
  1209. }
  1210. if (!ath_tx_setup(sc, ATH9K_WME_AC_VO)) {
  1211. DPRINTF(sc, ATH_DBG_FATAL,
  1212. "Unable to setup xmit queue for VO traffic\n");
  1213. error = -EIO;
  1214. goto bad2;
  1215. }
  1216. /* Initializes the noise floor to a reasonable default value.
  1217. * Later on this will be updated during ANI processing. */
  1218. sc->sc_ani.sc_noise_floor = ATH_DEFAULT_NOISE_FLOOR;
  1219. setup_timer(&sc->sc_ani.timer, ath_ani_calibrate, (unsigned long)sc);
  1220. if (ath9k_hw_getcapability(ah, ATH9K_CAP_CIPHER,
  1221. ATH9K_CIPHER_TKIP, NULL)) {
  1222. /*
  1223. * Whether we should enable h/w TKIP MIC.
  1224. * XXX: if we don't support WME TKIP MIC, then we wouldn't
  1225. * report WMM capable, so it's always safe to turn on
  1226. * TKIP MIC in this case.
  1227. */
  1228. ath9k_hw_setcapability(sc->sc_ah, ATH9K_CAP_TKIP_MIC,
  1229. 0, 1, NULL);
  1230. }
  1231. /*
  1232. * Check whether the separate key cache entries
  1233. * are required to handle both tx+rx MIC keys.
  1234. * With split mic keys the number of stations is limited
  1235. * to 27 otherwise 59.
  1236. */
  1237. if (ath9k_hw_getcapability(ah, ATH9K_CAP_CIPHER,
  1238. ATH9K_CIPHER_TKIP, NULL)
  1239. && ath9k_hw_getcapability(ah, ATH9K_CAP_CIPHER,
  1240. ATH9K_CIPHER_MIC, NULL)
  1241. && ath9k_hw_getcapability(ah, ATH9K_CAP_TKIP_SPLIT,
  1242. 0, NULL))
  1243. sc->sc_splitmic = 1;
  1244. /* turn on mcast key search if possible */
  1245. if (!ath9k_hw_getcapability(ah, ATH9K_CAP_MCAST_KEYSRCH, 0, NULL))
  1246. (void)ath9k_hw_setcapability(ah, ATH9K_CAP_MCAST_KEYSRCH, 1,
  1247. 1, NULL);
  1248. sc->sc_config.txpowlimit = ATH_TXPOWER_MAX;
  1249. sc->sc_config.txpowlimit_override = 0;
  1250. /* 11n Capabilities */
  1251. if (ah->ah_caps.hw_caps & ATH9K_HW_CAP_HT) {
  1252. sc->sc_flags |= SC_OP_TXAGGR;
  1253. sc->sc_flags |= SC_OP_RXAGGR;
  1254. }
  1255. sc->sc_tx_chainmask = ah->ah_caps.tx_chainmask;
  1256. sc->sc_rx_chainmask = ah->ah_caps.rx_chainmask;
  1257. ath9k_hw_setcapability(ah, ATH9K_CAP_DIVERSITY, 1, true, NULL);
  1258. sc->rx.defant = ath9k_hw_getdefantenna(ah);
  1259. ath9k_hw_getmac(ah, sc->sc_myaddr);
  1260. if (ah->ah_caps.hw_caps & ATH9K_HW_CAP_BSSIDMASK) {
  1261. ath9k_hw_getbssidmask(ah, sc->sc_bssidmask);
  1262. ATH_SET_VAP_BSSID_MASK(sc->sc_bssidmask);
  1263. ath9k_hw_setbssidmask(ah, sc->sc_bssidmask);
  1264. }
  1265. sc->beacon.slottime = ATH9K_SLOT_TIME_9; /* default to short slot time */
  1266. /* initialize beacon slots */
  1267. for (i = 0; i < ARRAY_SIZE(sc->beacon.bslot); i++)
  1268. sc->beacon.bslot[i] = ATH_IF_ID_ANY;
  1269. /* save MISC configurations */
  1270. sc->sc_config.swBeaconProcess = 1;
  1271. /* setup channels and rates */
  1272. sc->sbands[IEEE80211_BAND_2GHZ].channels = ath9k_2ghz_chantable;
  1273. sc->sbands[IEEE80211_BAND_2GHZ].bitrates =
  1274. sc->rates[IEEE80211_BAND_2GHZ];
  1275. sc->sbands[IEEE80211_BAND_2GHZ].band = IEEE80211_BAND_2GHZ;
  1276. sc->sbands[IEEE80211_BAND_2GHZ].n_channels =
  1277. ARRAY_SIZE(ath9k_2ghz_chantable);
  1278. if (test_bit(ATH9K_MODE_11A, sc->sc_ah->ah_caps.wireless_modes)) {
  1279. sc->sbands[IEEE80211_BAND_5GHZ].channels = ath9k_5ghz_chantable;
  1280. sc->sbands[IEEE80211_BAND_5GHZ].bitrates =
  1281. sc->rates[IEEE80211_BAND_5GHZ];
  1282. sc->sbands[IEEE80211_BAND_5GHZ].band = IEEE80211_BAND_5GHZ;
  1283. sc->sbands[IEEE80211_BAND_5GHZ].n_channels =
  1284. ARRAY_SIZE(ath9k_5ghz_chantable);
  1285. }
  1286. if (sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_BT_COEX)
  1287. ath9k_hw_btcoex_enable(sc->sc_ah);
  1288. return 0;
  1289. bad2:
  1290. /* cleanup tx queues */
  1291. for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++)
  1292. if (ATH_TXQ_SETUP(sc, i))
  1293. ath_tx_cleanupq(sc, &sc->tx.txq[i]);
  1294. bad:
  1295. if (ah)
  1296. ath9k_hw_detach(ah);
  1297. return error;
  1298. }
  1299. int ath_attach(u16 devid, struct ath_softc *sc)
  1300. {
  1301. struct ieee80211_hw *hw = sc->hw;
  1302. int error = 0;
  1303. DPRINTF(sc, ATH_DBG_CONFIG, "Attach ATH hw\n");
  1304. error = ath_init(devid, sc);
  1305. if (error != 0)
  1306. return error;
  1307. /* get mac address from hardware and set in mac80211 */
  1308. SET_IEEE80211_PERM_ADDR(hw, sc->sc_myaddr);
  1309. hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
  1310. IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
  1311. IEEE80211_HW_SIGNAL_DBM |
  1312. IEEE80211_HW_AMPDU_AGGREGATION |
  1313. IEEE80211_HW_SUPPORTS_PS |
  1314. IEEE80211_HW_PS_NULLFUNC_STACK;
  1315. if (AR_SREV_9160_10_OR_LATER(sc->sc_ah))
  1316. hw->flags |= IEEE80211_HW_MFP_CAPABLE;
  1317. hw->wiphy->interface_modes =
  1318. BIT(NL80211_IFTYPE_AP) |
  1319. BIT(NL80211_IFTYPE_STATION) |
  1320. BIT(NL80211_IFTYPE_ADHOC);
  1321. hw->wiphy->reg_notifier = ath9k_reg_notifier;
  1322. hw->wiphy->strict_regulatory = true;
  1323. hw->queues = 4;
  1324. hw->max_rates = 4;
  1325. hw->max_rate_tries = ATH_11N_TXMAXTRY;
  1326. hw->sta_data_size = sizeof(struct ath_node);
  1327. hw->vif_data_size = sizeof(struct ath_vap);
  1328. hw->rate_control_algorithm = "ath9k_rate_control";
  1329. if (sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_HT) {
  1330. setup_ht_cap(&sc->sbands[IEEE80211_BAND_2GHZ].ht_cap);
  1331. if (test_bit(ATH9K_MODE_11A, sc->sc_ah->ah_caps.wireless_modes))
  1332. setup_ht_cap(&sc->sbands[IEEE80211_BAND_5GHZ].ht_cap);
  1333. }
  1334. hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &sc->sbands[IEEE80211_BAND_2GHZ];
  1335. if (test_bit(ATH9K_MODE_11A, sc->sc_ah->ah_caps.wireless_modes))
  1336. hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
  1337. &sc->sbands[IEEE80211_BAND_5GHZ];
  1338. /* initialize tx/rx engine */
  1339. error = ath_tx_init(sc, ATH_TXBUF);
  1340. if (error != 0)
  1341. goto detach;
  1342. error = ath_rx_init(sc, ATH_RXBUF);
  1343. if (error != 0)
  1344. goto detach;
  1345. #if defined(CONFIG_RFKILL) || defined(CONFIG_RFKILL_MODULE)
  1346. /* Initialze h/w Rfkill */
  1347. if (sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
  1348. INIT_DELAYED_WORK(&sc->rf_kill.rfkill_poll, ath_rfkill_poll);
  1349. /* Initialize s/w rfkill */
  1350. if (ath_init_sw_rfkill(sc))
  1351. goto detach;
  1352. #endif
  1353. if (ath9k_is_world_regd(sc->sc_ah)) {
  1354. /* Anything applied here (prior to wiphy registratoin) gets
  1355. * saved on the wiphy orig_* parameters */
  1356. const struct ieee80211_regdomain *regd =
  1357. ath9k_world_regdomain(sc->sc_ah);
  1358. hw->wiphy->custom_regulatory = true;
  1359. hw->wiphy->strict_regulatory = false;
  1360. wiphy_apply_custom_regulatory(sc->hw->wiphy, regd);
  1361. ath9k_reg_apply_radar_flags(hw->wiphy);
  1362. ath9k_reg_apply_world_flags(hw->wiphy, REGDOM_SET_BY_INIT);
  1363. } else {
  1364. /* This gets applied in the case of the absense of CRDA,
  1365. * its our own custom world regulatory domain, similar to
  1366. * cfg80211's but we enable passive scanning */
  1367. const struct ieee80211_regdomain *regd =
  1368. ath9k_default_world_regdomain();
  1369. wiphy_apply_custom_regulatory(sc->hw->wiphy, regd);
  1370. ath9k_reg_apply_radar_flags(hw->wiphy);
  1371. ath9k_reg_apply_world_flags(hw->wiphy, REGDOM_SET_BY_INIT);
  1372. }
  1373. error = ieee80211_register_hw(hw);
  1374. if (!ath9k_is_world_regd(sc->sc_ah))
  1375. regulatory_hint(hw->wiphy, sc->sc_ah->alpha2);
  1376. /* Initialize LED control */
  1377. ath_init_leds(sc);
  1378. return 0;
  1379. detach:
  1380. ath_detach(sc);
  1381. return error;
  1382. }
  1383. int ath_reset(struct ath_softc *sc, bool retry_tx)
  1384. {
  1385. struct ath_hal *ah = sc->sc_ah;
  1386. struct ieee80211_hw *hw = sc->hw;
  1387. int r;
  1388. ath9k_hw_set_interrupts(ah, 0);
  1389. ath_drain_all_txq(sc, retry_tx);
  1390. ath_stoprecv(sc);
  1391. ath_flushrecv(sc);
  1392. spin_lock_bh(&sc->sc_resetlock);
  1393. r = ath9k_hw_reset(ah, sc->sc_ah->ah_curchan, false);
  1394. if (r)
  1395. DPRINTF(sc, ATH_DBG_FATAL,
  1396. "Unable to reset hardware; reset status %u\n", r);
  1397. spin_unlock_bh(&sc->sc_resetlock);
  1398. if (ath_startrecv(sc) != 0)
  1399. DPRINTF(sc, ATH_DBG_FATAL, "Unable to start recv logic\n");
  1400. /*
  1401. * We may be doing a reset in response to a request
  1402. * that changes the channel so update any state that
  1403. * might change as a result.
  1404. */
  1405. ath_cache_conf_rate(sc, &hw->conf);
  1406. ath_update_txpow(sc);
  1407. if (sc->sc_flags & SC_OP_BEACONS)
  1408. ath_beacon_config(sc, ATH_IF_ID_ANY); /* restart beacons */
  1409. ath9k_hw_set_interrupts(ah, sc->sc_imask);
  1410. if (retry_tx) {
  1411. int i;
  1412. for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
  1413. if (ATH_TXQ_SETUP(sc, i)) {
  1414. spin_lock_bh(&sc->tx.txq[i].axq_lock);
  1415. ath_txq_schedule(sc, &sc->tx.txq[i]);
  1416. spin_unlock_bh(&sc->tx.txq[i].axq_lock);
  1417. }
  1418. }
  1419. }
  1420. return r;
  1421. }
  1422. /*
  1423. * This function will allocate both the DMA descriptor structure, and the
  1424. * buffers it contains. These are used to contain the descriptors used
  1425. * by the system.
  1426. */
  1427. int ath_descdma_setup(struct ath_softc *sc, struct ath_descdma *dd,
  1428. struct list_head *head, const char *name,
  1429. int nbuf, int ndesc)
  1430. {
  1431. #define DS2PHYS(_dd, _ds) \
  1432. ((_dd)->dd_desc_paddr + ((caddr_t)(_ds) - (caddr_t)(_dd)->dd_desc))
  1433. #define ATH_DESC_4KB_BOUND_CHECK(_daddr) ((((_daddr) & 0xFFF) > 0xF7F) ? 1 : 0)
  1434. #define ATH_DESC_4KB_BOUND_NUM_SKIPPED(_len) ((_len) / 4096)
  1435. struct ath_desc *ds;
  1436. struct ath_buf *bf;
  1437. int i, bsize, error;
  1438. DPRINTF(sc, ATH_DBG_CONFIG, "%s DMA: %u buffers %u desc/buf\n",
  1439. name, nbuf, ndesc);
  1440. /* ath_desc must be a multiple of DWORDs */
  1441. if ((sizeof(struct ath_desc) % 4) != 0) {
  1442. DPRINTF(sc, ATH_DBG_FATAL, "ath_desc not DWORD aligned\n");
  1443. ASSERT((sizeof(struct ath_desc) % 4) == 0);
  1444. error = -ENOMEM;
  1445. goto fail;
  1446. }
  1447. dd->dd_name = name;
  1448. dd->dd_desc_len = sizeof(struct ath_desc) * nbuf * ndesc;
  1449. /*
  1450. * Need additional DMA memory because we can't use
  1451. * descriptors that cross the 4K page boundary. Assume
  1452. * one skipped descriptor per 4K page.
  1453. */
  1454. if (!(sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_4KB_SPLITTRANS)) {
  1455. u32 ndesc_skipped =
  1456. ATH_DESC_4KB_BOUND_NUM_SKIPPED(dd->dd_desc_len);
  1457. u32 dma_len;
  1458. while (ndesc_skipped) {
  1459. dma_len = ndesc_skipped * sizeof(struct ath_desc);
  1460. dd->dd_desc_len += dma_len;
  1461. ndesc_skipped = ATH_DESC_4KB_BOUND_NUM_SKIPPED(dma_len);
  1462. };
  1463. }
  1464. /* allocate descriptors */
  1465. dd->dd_desc = dma_alloc_coherent(sc->dev, dd->dd_desc_len,
  1466. &dd->dd_desc_paddr, GFP_ATOMIC);
  1467. if (dd->dd_desc == NULL) {
  1468. error = -ENOMEM;
  1469. goto fail;
  1470. }
  1471. ds = dd->dd_desc;
  1472. DPRINTF(sc, ATH_DBG_CONFIG, "%s DMA map: %p (%u) -> %llx (%u)\n",
  1473. dd->dd_name, ds, (u32) dd->dd_desc_len,
  1474. ito64(dd->dd_desc_paddr), /*XXX*/(u32) dd->dd_desc_len);
  1475. /* allocate buffers */
  1476. bsize = sizeof(struct ath_buf) * nbuf;
  1477. bf = kmalloc(bsize, GFP_KERNEL);
  1478. if (bf == NULL) {
  1479. error = -ENOMEM;
  1480. goto fail2;
  1481. }
  1482. memset(bf, 0, bsize);
  1483. dd->dd_bufptr = bf;
  1484. INIT_LIST_HEAD(head);
  1485. for (i = 0; i < nbuf; i++, bf++, ds += ndesc) {
  1486. bf->bf_desc = ds;
  1487. bf->bf_daddr = DS2PHYS(dd, ds);
  1488. if (!(sc->sc_ah->ah_caps.hw_caps &
  1489. ATH9K_HW_CAP_4KB_SPLITTRANS)) {
  1490. /*
  1491. * Skip descriptor addresses which can cause 4KB
  1492. * boundary crossing (addr + length) with a 32 dword
  1493. * descriptor fetch.
  1494. */
  1495. while (ATH_DESC_4KB_BOUND_CHECK(bf->bf_daddr)) {
  1496. ASSERT((caddr_t) bf->bf_desc <
  1497. ((caddr_t) dd->dd_desc +
  1498. dd->dd_desc_len));
  1499. ds += ndesc;
  1500. bf->bf_desc = ds;
  1501. bf->bf_daddr = DS2PHYS(dd, ds);
  1502. }
  1503. }
  1504. list_add_tail(&bf->list, head);
  1505. }
  1506. return 0;
  1507. fail2:
  1508. dma_free_coherent(sc->dev, dd->dd_desc_len, dd->dd_desc,
  1509. dd->dd_desc_paddr);
  1510. fail:
  1511. memset(dd, 0, sizeof(*dd));
  1512. return error;
  1513. #undef ATH_DESC_4KB_BOUND_CHECK
  1514. #undef ATH_DESC_4KB_BOUND_NUM_SKIPPED
  1515. #undef DS2PHYS
  1516. }
  1517. void ath_descdma_cleanup(struct ath_softc *sc,
  1518. struct ath_descdma *dd,
  1519. struct list_head *head)
  1520. {
  1521. dma_free_coherent(sc->dev, dd->dd_desc_len, dd->dd_desc,
  1522. dd->dd_desc_paddr);
  1523. INIT_LIST_HEAD(head);
  1524. kfree(dd->dd_bufptr);
  1525. memset(dd, 0, sizeof(*dd));
  1526. }
  1527. int ath_get_hal_qnum(u16 queue, struct ath_softc *sc)
  1528. {
  1529. int qnum;
  1530. switch (queue) {
  1531. case 0:
  1532. qnum = sc->tx.hwq_map[ATH9K_WME_AC_VO];
  1533. break;
  1534. case 1:
  1535. qnum = sc->tx.hwq_map[ATH9K_WME_AC_VI];
  1536. break;
  1537. case 2:
  1538. qnum = sc->tx.hwq_map[ATH9K_WME_AC_BE];
  1539. break;
  1540. case 3:
  1541. qnum = sc->tx.hwq_map[ATH9K_WME_AC_BK];
  1542. break;
  1543. default:
  1544. qnum = sc->tx.hwq_map[ATH9K_WME_AC_BE];
  1545. break;
  1546. }
  1547. return qnum;
  1548. }
  1549. int ath_get_mac80211_qnum(u32 queue, struct ath_softc *sc)
  1550. {
  1551. int qnum;
  1552. switch (queue) {
  1553. case ATH9K_WME_AC_VO:
  1554. qnum = 0;
  1555. break;
  1556. case ATH9K_WME_AC_VI:
  1557. qnum = 1;
  1558. break;
  1559. case ATH9K_WME_AC_BE:
  1560. qnum = 2;
  1561. break;
  1562. case ATH9K_WME_AC_BK:
  1563. qnum = 3;
  1564. break;
  1565. default:
  1566. qnum = -1;
  1567. break;
  1568. }
  1569. return qnum;
  1570. }
  1571. /* XXX: Remove me once we don't depend on ath9k_channel for all
  1572. * this redundant data */
  1573. static void ath9k_update_ichannel(struct ath_softc *sc,
  1574. struct ath9k_channel *ichan)
  1575. {
  1576. struct ieee80211_hw *hw = sc->hw;
  1577. struct ieee80211_channel *chan = hw->conf.channel;
  1578. struct ieee80211_conf *conf = &hw->conf;
  1579. ichan->channel = chan->center_freq;
  1580. ichan->chan = chan;
  1581. if (chan->band == IEEE80211_BAND_2GHZ) {
  1582. ichan->chanmode = CHANNEL_G;
  1583. ichan->channelFlags = CHANNEL_2GHZ | CHANNEL_OFDM;
  1584. } else {
  1585. ichan->chanmode = CHANNEL_A;
  1586. ichan->channelFlags = CHANNEL_5GHZ | CHANNEL_OFDM;
  1587. }
  1588. sc->tx_chan_width = ATH9K_HT_MACMODE_20;
  1589. if (conf_is_ht(conf)) {
  1590. if (conf_is_ht40(conf))
  1591. sc->tx_chan_width = ATH9K_HT_MACMODE_2040;
  1592. ichan->chanmode = ath_get_extchanmode(sc, chan,
  1593. conf->channel_type);
  1594. }
  1595. }
  1596. /**********************/
  1597. /* mac80211 callbacks */
  1598. /**********************/
  1599. static int ath9k_start(struct ieee80211_hw *hw)
  1600. {
  1601. struct ath_softc *sc = hw->priv;
  1602. struct ieee80211_channel *curchan = hw->conf.channel;
  1603. struct ath9k_channel *init_channel;
  1604. int r, pos;
  1605. DPRINTF(sc, ATH_DBG_CONFIG, "Starting driver with "
  1606. "initial channel: %d MHz\n", curchan->center_freq);
  1607. /* setup initial channel */
  1608. pos = curchan->hw_value;
  1609. init_channel = &sc->sc_ah->ah_channels[pos];
  1610. ath9k_update_ichannel(sc, init_channel);
  1611. /* Reset SERDES registers */
  1612. ath9k_hw_configpcipowersave(sc->sc_ah, 0);
  1613. /*
  1614. * The basic interface to setting the hardware in a good
  1615. * state is ``reset''. On return the hardware is known to
  1616. * be powered up and with interrupts disabled. This must
  1617. * be followed by initialization of the appropriate bits
  1618. * and then setup of the interrupt mask.
  1619. */
  1620. spin_lock_bh(&sc->sc_resetlock);
  1621. r = ath9k_hw_reset(sc->sc_ah, init_channel, false);
  1622. if (r) {
  1623. DPRINTF(sc, ATH_DBG_FATAL,
  1624. "Unable to reset hardware; reset status %u "
  1625. "(freq %u MHz)\n", r,
  1626. curchan->center_freq);
  1627. spin_unlock_bh(&sc->sc_resetlock);
  1628. return r;
  1629. }
  1630. spin_unlock_bh(&sc->sc_resetlock);
  1631. /*
  1632. * This is needed only to setup initial state
  1633. * but it's best done after a reset.
  1634. */
  1635. ath_update_txpow(sc);
  1636. /*
  1637. * Setup the hardware after reset:
  1638. * The receive engine is set going.
  1639. * Frame transmit is handled entirely
  1640. * in the frame output path; there's nothing to do
  1641. * here except setup the interrupt mask.
  1642. */
  1643. if (ath_startrecv(sc) != 0) {
  1644. DPRINTF(sc, ATH_DBG_FATAL,
  1645. "Unable to start recv logic\n");
  1646. return -EIO;
  1647. }
  1648. /* Setup our intr mask. */
  1649. sc->sc_imask = ATH9K_INT_RX | ATH9K_INT_TX
  1650. | ATH9K_INT_RXEOL | ATH9K_INT_RXORN
  1651. | ATH9K_INT_FATAL | ATH9K_INT_GLOBAL;
  1652. if (sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_GTT)
  1653. sc->sc_imask |= ATH9K_INT_GTT;
  1654. if (sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_HT)
  1655. sc->sc_imask |= ATH9K_INT_CST;
  1656. /*
  1657. * Enable MIB interrupts when there are hardware phy counters.
  1658. * Note we only do this (at the moment) for station mode.
  1659. */
  1660. if (ath9k_hw_phycounters(sc->sc_ah) &&
  1661. ((sc->sc_ah->ah_opmode == NL80211_IFTYPE_STATION) ||
  1662. (sc->sc_ah->ah_opmode == NL80211_IFTYPE_ADHOC)))
  1663. sc->sc_imask |= ATH9K_INT_MIB;
  1664. /*
  1665. * Some hardware processes the TIM IE and fires an
  1666. * interrupt when the TIM bit is set. For hardware
  1667. * that does, if not overridden by configuration,
  1668. * enable the TIM interrupt when operating as station.
  1669. */
  1670. if ((sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_ENHANCEDPM) &&
  1671. (sc->sc_ah->ah_opmode == NL80211_IFTYPE_STATION) &&
  1672. !sc->sc_config.swBeaconProcess)
  1673. sc->sc_imask |= ATH9K_INT_TIM;
  1674. ath_cache_conf_rate(sc, &hw->conf);
  1675. sc->sc_flags &= ~SC_OP_INVALID;
  1676. /* Disable BMISS interrupt when we're not associated */
  1677. sc->sc_imask &= ~(ATH9K_INT_SWBA | ATH9K_INT_BMISS);
  1678. ath9k_hw_set_interrupts(sc->sc_ah, sc->sc_imask);
  1679. ieee80211_wake_queues(sc->hw);
  1680. #if defined(CONFIG_RFKILL) || defined(CONFIG_RFKILL_MODULE)
  1681. r = ath_start_rfkill_poll(sc);
  1682. #endif
  1683. return r;
  1684. }
  1685. static int ath9k_tx(struct ieee80211_hw *hw,
  1686. struct sk_buff *skb)
  1687. {
  1688. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1689. struct ath_softc *sc = hw->priv;
  1690. struct ath_tx_control txctl;
  1691. int hdrlen, padsize;
  1692. memset(&txctl, 0, sizeof(struct ath_tx_control));
  1693. /*
  1694. * As a temporary workaround, assign seq# here; this will likely need
  1695. * to be cleaned up to work better with Beacon transmission and virtual
  1696. * BSSes.
  1697. */
  1698. if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
  1699. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  1700. if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
  1701. sc->tx.seq_no += 0x10;
  1702. hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
  1703. hdr->seq_ctrl |= cpu_to_le16(sc->tx.seq_no);
  1704. }
  1705. /* Add the padding after the header if this is not already done */
  1706. hdrlen = ieee80211_get_hdrlen_from_skb(skb);
  1707. if (hdrlen & 3) {
  1708. padsize = hdrlen % 4;
  1709. if (skb_headroom(skb) < padsize)
  1710. return -1;
  1711. skb_push(skb, padsize);
  1712. memmove(skb->data, skb->data + padsize, hdrlen);
  1713. }
  1714. /* Check if a tx queue is available */
  1715. txctl.txq = ath_test_get_txq(sc, skb);
  1716. if (!txctl.txq)
  1717. goto exit;
  1718. DPRINTF(sc, ATH_DBG_XMIT, "transmitting packet, skb: %p\n", skb);
  1719. if (ath_tx_start(sc, skb, &txctl) != 0) {
  1720. DPRINTF(sc, ATH_DBG_XMIT, "TX failed\n");
  1721. goto exit;
  1722. }
  1723. return 0;
  1724. exit:
  1725. dev_kfree_skb_any(skb);
  1726. return 0;
  1727. }
  1728. static void ath9k_stop(struct ieee80211_hw *hw)
  1729. {
  1730. struct ath_softc *sc = hw->priv;
  1731. if (sc->sc_flags & SC_OP_INVALID) {
  1732. DPRINTF(sc, ATH_DBG_ANY, "Device not present\n");
  1733. return;
  1734. }
  1735. DPRINTF(sc, ATH_DBG_CONFIG, "Cleaning up\n");
  1736. ieee80211_stop_queues(sc->hw);
  1737. /* make sure h/w will not generate any interrupt
  1738. * before setting the invalid flag. */
  1739. ath9k_hw_set_interrupts(sc->sc_ah, 0);
  1740. if (!(sc->sc_flags & SC_OP_INVALID)) {
  1741. ath_drain_all_txq(sc, false);
  1742. ath_stoprecv(sc);
  1743. ath9k_hw_phy_disable(sc->sc_ah);
  1744. } else
  1745. sc->rx.rxlink = NULL;
  1746. #if defined(CONFIG_RFKILL) || defined(CONFIG_RFKILL_MODULE)
  1747. if (sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
  1748. cancel_delayed_work_sync(&sc->rf_kill.rfkill_poll);
  1749. #endif
  1750. /* disable HAL and put h/w to sleep */
  1751. ath9k_hw_disable(sc->sc_ah);
  1752. ath9k_hw_configpcipowersave(sc->sc_ah, 1);
  1753. sc->sc_flags |= SC_OP_INVALID;
  1754. DPRINTF(sc, ATH_DBG_CONFIG, "Driver halt\n");
  1755. }
  1756. static int ath9k_add_interface(struct ieee80211_hw *hw,
  1757. struct ieee80211_if_init_conf *conf)
  1758. {
  1759. struct ath_softc *sc = hw->priv;
  1760. struct ath_vap *avp = (void *)conf->vif->drv_priv;
  1761. enum nl80211_iftype ic_opmode = NL80211_IFTYPE_UNSPECIFIED;
  1762. /* Support only vap for now */
  1763. if (sc->sc_nvaps)
  1764. return -ENOBUFS;
  1765. switch (conf->type) {
  1766. case NL80211_IFTYPE_STATION:
  1767. ic_opmode = NL80211_IFTYPE_STATION;
  1768. break;
  1769. case NL80211_IFTYPE_ADHOC:
  1770. ic_opmode = NL80211_IFTYPE_ADHOC;
  1771. break;
  1772. case NL80211_IFTYPE_AP:
  1773. ic_opmode = NL80211_IFTYPE_AP;
  1774. break;
  1775. default:
  1776. DPRINTF(sc, ATH_DBG_FATAL,
  1777. "Interface type %d not yet supported\n", conf->type);
  1778. return -EOPNOTSUPP;
  1779. }
  1780. DPRINTF(sc, ATH_DBG_CONFIG, "Attach a VAP of type: %d\n", ic_opmode);
  1781. /* Set the VAP opmode */
  1782. avp->av_opmode = ic_opmode;
  1783. avp->av_bslot = -1;
  1784. if (ic_opmode == NL80211_IFTYPE_AP)
  1785. ath9k_hw_set_tsfadjust(sc->sc_ah, 1);
  1786. sc->sc_vaps[0] = conf->vif;
  1787. sc->sc_nvaps++;
  1788. /* Set the device opmode */
  1789. sc->sc_ah->ah_opmode = ic_opmode;
  1790. if (conf->type == NL80211_IFTYPE_AP) {
  1791. /* TODO: is this a suitable place to start ANI for AP mode? */
  1792. /* Start ANI */
  1793. mod_timer(&sc->sc_ani.timer,
  1794. jiffies + msecs_to_jiffies(ATH_ANI_POLLINTERVAL));
  1795. }
  1796. return 0;
  1797. }
  1798. static void ath9k_remove_interface(struct ieee80211_hw *hw,
  1799. struct ieee80211_if_init_conf *conf)
  1800. {
  1801. struct ath_softc *sc = hw->priv;
  1802. struct ath_vap *avp = (void *)conf->vif->drv_priv;
  1803. DPRINTF(sc, ATH_DBG_CONFIG, "Detach Interface\n");
  1804. /* Stop ANI */
  1805. del_timer_sync(&sc->sc_ani.timer);
  1806. /* Reclaim beacon resources */
  1807. if (sc->sc_ah->ah_opmode == NL80211_IFTYPE_AP ||
  1808. sc->sc_ah->ah_opmode == NL80211_IFTYPE_ADHOC) {
  1809. ath9k_hw_stoptxdma(sc->sc_ah, sc->beacon.beaconq);
  1810. ath_beacon_return(sc, avp);
  1811. }
  1812. sc->sc_flags &= ~SC_OP_BEACONS;
  1813. sc->sc_vaps[0] = NULL;
  1814. sc->sc_nvaps--;
  1815. }
  1816. static int ath9k_config(struct ieee80211_hw *hw, u32 changed)
  1817. {
  1818. struct ath_softc *sc = hw->priv;
  1819. struct ieee80211_conf *conf = &hw->conf;
  1820. mutex_lock(&sc->mutex);
  1821. if (changed & IEEE80211_CONF_CHANGE_PS) {
  1822. if (conf->flags & IEEE80211_CONF_PS) {
  1823. if ((sc->sc_imask & ATH9K_INT_TIM_TIMER) == 0) {
  1824. sc->sc_imask |= ATH9K_INT_TIM_TIMER;
  1825. ath9k_hw_set_interrupts(sc->sc_ah,
  1826. sc->sc_imask);
  1827. }
  1828. ath9k_hw_setrxabort(sc->sc_ah, 1);
  1829. ath9k_hw_setpower(sc->sc_ah, ATH9K_PM_NETWORK_SLEEP);
  1830. } else {
  1831. ath9k_hw_setpower(sc->sc_ah, ATH9K_PM_AWAKE);
  1832. ath9k_hw_setrxabort(sc->sc_ah, 0);
  1833. sc->sc_flags &= ~SC_OP_WAIT_FOR_BEACON;
  1834. if (sc->sc_imask & ATH9K_INT_TIM_TIMER) {
  1835. sc->sc_imask &= ~ATH9K_INT_TIM_TIMER;
  1836. ath9k_hw_set_interrupts(sc->sc_ah,
  1837. sc->sc_imask);
  1838. }
  1839. }
  1840. }
  1841. if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
  1842. struct ieee80211_channel *curchan = hw->conf.channel;
  1843. int pos = curchan->hw_value;
  1844. DPRINTF(sc, ATH_DBG_CONFIG, "Set channel: %d MHz\n",
  1845. curchan->center_freq);
  1846. /* XXX: remove me eventualy */
  1847. ath9k_update_ichannel(sc, &sc->sc_ah->ah_channels[pos]);
  1848. ath_update_chainmask(sc, conf_is_ht(conf));
  1849. if (ath_set_channel(sc, &sc->sc_ah->ah_channels[pos]) < 0) {
  1850. DPRINTF(sc, ATH_DBG_FATAL, "Unable to set channel\n");
  1851. mutex_unlock(&sc->mutex);
  1852. return -EINVAL;
  1853. }
  1854. }
  1855. if (changed & IEEE80211_CONF_CHANGE_POWER)
  1856. sc->sc_config.txpowlimit = 2 * conf->power_level;
  1857. mutex_unlock(&sc->mutex);
  1858. return 0;
  1859. }
  1860. static int ath9k_config_interface(struct ieee80211_hw *hw,
  1861. struct ieee80211_vif *vif,
  1862. struct ieee80211_if_conf *conf)
  1863. {
  1864. struct ath_softc *sc = hw->priv;
  1865. struct ath_hal *ah = sc->sc_ah;
  1866. struct ath_vap *avp = (void *)vif->drv_priv;
  1867. u32 rfilt = 0;
  1868. int error, i;
  1869. /* TODO: Need to decide which hw opmode to use for multi-interface
  1870. * cases */
  1871. if (vif->type == NL80211_IFTYPE_AP &&
  1872. ah->ah_opmode != NL80211_IFTYPE_AP) {
  1873. ah->ah_opmode = NL80211_IFTYPE_STATION;
  1874. ath9k_hw_setopmode(ah);
  1875. ath9k_hw_write_associd(ah, sc->sc_myaddr, 0);
  1876. /* Request full reset to get hw opmode changed properly */
  1877. sc->sc_flags |= SC_OP_FULL_RESET;
  1878. }
  1879. if ((conf->changed & IEEE80211_IFCC_BSSID) &&
  1880. !is_zero_ether_addr(conf->bssid)) {
  1881. switch (vif->type) {
  1882. case NL80211_IFTYPE_STATION:
  1883. case NL80211_IFTYPE_ADHOC:
  1884. /* Set BSSID */
  1885. memcpy(sc->sc_curbssid, conf->bssid, ETH_ALEN);
  1886. sc->sc_curaid = 0;
  1887. ath9k_hw_write_associd(sc->sc_ah, sc->sc_curbssid,
  1888. sc->sc_curaid);
  1889. /* Set aggregation protection mode parameters */
  1890. sc->sc_config.ath_aggr_prot = 0;
  1891. DPRINTF(sc, ATH_DBG_CONFIG,
  1892. "RX filter 0x%x bssid %pM aid 0x%x\n",
  1893. rfilt, sc->sc_curbssid, sc->sc_curaid);
  1894. /* need to reconfigure the beacon */
  1895. sc->sc_flags &= ~SC_OP_BEACONS ;
  1896. break;
  1897. default:
  1898. break;
  1899. }
  1900. }
  1901. if ((conf->changed & IEEE80211_IFCC_BEACON) &&
  1902. ((vif->type == NL80211_IFTYPE_ADHOC) ||
  1903. (vif->type == NL80211_IFTYPE_AP))) {
  1904. /*
  1905. * Allocate and setup the beacon frame.
  1906. *
  1907. * Stop any previous beacon DMA. This may be
  1908. * necessary, for example, when an ibss merge
  1909. * causes reconfiguration; we may be called
  1910. * with beacon transmission active.
  1911. */
  1912. ath9k_hw_stoptxdma(sc->sc_ah, sc->beacon.beaconq);
  1913. error = ath_beacon_alloc(sc, 0);
  1914. if (error != 0)
  1915. return error;
  1916. ath_beacon_sync(sc, 0);
  1917. }
  1918. /* Check for WLAN_CAPABILITY_PRIVACY ? */
  1919. if ((avp->av_opmode != NL80211_IFTYPE_STATION)) {
  1920. for (i = 0; i < IEEE80211_WEP_NKID; i++)
  1921. if (ath9k_hw_keyisvalid(sc->sc_ah, (u16)i))
  1922. ath9k_hw_keysetmac(sc->sc_ah,
  1923. (u16)i,
  1924. sc->sc_curbssid);
  1925. }
  1926. /* Only legacy IBSS for now */
  1927. if (vif->type == NL80211_IFTYPE_ADHOC)
  1928. ath_update_chainmask(sc, 0);
  1929. return 0;
  1930. }
  1931. #define SUPPORTED_FILTERS \
  1932. (FIF_PROMISC_IN_BSS | \
  1933. FIF_ALLMULTI | \
  1934. FIF_CONTROL | \
  1935. FIF_OTHER_BSS | \
  1936. FIF_BCN_PRBRESP_PROMISC | \
  1937. FIF_FCSFAIL)
  1938. /* FIXME: sc->sc_full_reset ? */
  1939. static void ath9k_configure_filter(struct ieee80211_hw *hw,
  1940. unsigned int changed_flags,
  1941. unsigned int *total_flags,
  1942. int mc_count,
  1943. struct dev_mc_list *mclist)
  1944. {
  1945. struct ath_softc *sc = hw->priv;
  1946. u32 rfilt;
  1947. changed_flags &= SUPPORTED_FILTERS;
  1948. *total_flags &= SUPPORTED_FILTERS;
  1949. sc->rx.rxfilter = *total_flags;
  1950. rfilt = ath_calcrxfilter(sc);
  1951. ath9k_hw_setrxfilter(sc->sc_ah, rfilt);
  1952. if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
  1953. if (*total_flags & FIF_BCN_PRBRESP_PROMISC)
  1954. ath9k_hw_write_associd(sc->sc_ah, ath_bcast_mac, 0);
  1955. }
  1956. DPRINTF(sc, ATH_DBG_CONFIG, "Set HW RX filter: 0x%x\n", sc->rx.rxfilter);
  1957. }
  1958. static void ath9k_sta_notify(struct ieee80211_hw *hw,
  1959. struct ieee80211_vif *vif,
  1960. enum sta_notify_cmd cmd,
  1961. struct ieee80211_sta *sta)
  1962. {
  1963. struct ath_softc *sc = hw->priv;
  1964. switch (cmd) {
  1965. case STA_NOTIFY_ADD:
  1966. ath_node_attach(sc, sta);
  1967. break;
  1968. case STA_NOTIFY_REMOVE:
  1969. ath_node_detach(sc, sta);
  1970. break;
  1971. default:
  1972. break;
  1973. }
  1974. }
  1975. static int ath9k_conf_tx(struct ieee80211_hw *hw,
  1976. u16 queue,
  1977. const struct ieee80211_tx_queue_params *params)
  1978. {
  1979. struct ath_softc *sc = hw->priv;
  1980. struct ath9k_tx_queue_info qi;
  1981. int ret = 0, qnum;
  1982. if (queue >= WME_NUM_AC)
  1983. return 0;
  1984. qi.tqi_aifs = params->aifs;
  1985. qi.tqi_cwmin = params->cw_min;
  1986. qi.tqi_cwmax = params->cw_max;
  1987. qi.tqi_burstTime = params->txop;
  1988. qnum = ath_get_hal_qnum(queue, sc);
  1989. DPRINTF(sc, ATH_DBG_CONFIG,
  1990. "Configure tx [queue/halq] [%d/%d], "
  1991. "aifs: %d, cw_min: %d, cw_max: %d, txop: %d\n",
  1992. queue, qnum, params->aifs, params->cw_min,
  1993. params->cw_max, params->txop);
  1994. ret = ath_txq_update(sc, qnum, &qi);
  1995. if (ret)
  1996. DPRINTF(sc, ATH_DBG_FATAL, "TXQ Update failed\n");
  1997. return ret;
  1998. }
  1999. static int ath9k_set_key(struct ieee80211_hw *hw,
  2000. enum set_key_cmd cmd,
  2001. struct ieee80211_vif *vif,
  2002. struct ieee80211_sta *sta,
  2003. struct ieee80211_key_conf *key)
  2004. {
  2005. struct ath_softc *sc = hw->priv;
  2006. int ret = 0;
  2007. ath9k_ps_wakeup(sc);
  2008. DPRINTF(sc, ATH_DBG_KEYCACHE, "Set HW Key\n");
  2009. switch (cmd) {
  2010. case SET_KEY:
  2011. ret = ath_key_config(sc, sta, key);
  2012. if (ret >= 0) {
  2013. key->hw_key_idx = ret;
  2014. /* push IV and Michael MIC generation to stack */
  2015. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  2016. if (key->alg == ALG_TKIP)
  2017. key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
  2018. if (sc->sc_ah->sw_mgmt_crypto && key->alg == ALG_CCMP)
  2019. key->flags |= IEEE80211_KEY_FLAG_SW_MGMT;
  2020. ret = 0;
  2021. }
  2022. break;
  2023. case DISABLE_KEY:
  2024. ath_key_delete(sc, key);
  2025. break;
  2026. default:
  2027. ret = -EINVAL;
  2028. }
  2029. ath9k_ps_restore(sc);
  2030. return ret;
  2031. }
  2032. static void ath9k_bss_info_changed(struct ieee80211_hw *hw,
  2033. struct ieee80211_vif *vif,
  2034. struct ieee80211_bss_conf *bss_conf,
  2035. u32 changed)
  2036. {
  2037. struct ath_softc *sc = hw->priv;
  2038. if (changed & BSS_CHANGED_ERP_PREAMBLE) {
  2039. DPRINTF(sc, ATH_DBG_CONFIG, "BSS Changed PREAMBLE %d\n",
  2040. bss_conf->use_short_preamble);
  2041. if (bss_conf->use_short_preamble)
  2042. sc->sc_flags |= SC_OP_PREAMBLE_SHORT;
  2043. else
  2044. sc->sc_flags &= ~SC_OP_PREAMBLE_SHORT;
  2045. }
  2046. if (changed & BSS_CHANGED_ERP_CTS_PROT) {
  2047. DPRINTF(sc, ATH_DBG_CONFIG, "BSS Changed CTS PROT %d\n",
  2048. bss_conf->use_cts_prot);
  2049. if (bss_conf->use_cts_prot &&
  2050. hw->conf.channel->band != IEEE80211_BAND_5GHZ)
  2051. sc->sc_flags |= SC_OP_PROTECT_ENABLE;
  2052. else
  2053. sc->sc_flags &= ~SC_OP_PROTECT_ENABLE;
  2054. }
  2055. if (changed & BSS_CHANGED_ASSOC) {
  2056. DPRINTF(sc, ATH_DBG_CONFIG, "BSS Changed ASSOC %d\n",
  2057. bss_conf->assoc);
  2058. ath9k_bss_assoc_info(sc, vif, bss_conf);
  2059. }
  2060. }
  2061. static u64 ath9k_get_tsf(struct ieee80211_hw *hw)
  2062. {
  2063. u64 tsf;
  2064. struct ath_softc *sc = hw->priv;
  2065. struct ath_hal *ah = sc->sc_ah;
  2066. tsf = ath9k_hw_gettsf64(ah);
  2067. return tsf;
  2068. }
  2069. static void ath9k_reset_tsf(struct ieee80211_hw *hw)
  2070. {
  2071. struct ath_softc *sc = hw->priv;
  2072. struct ath_hal *ah = sc->sc_ah;
  2073. ath9k_hw_reset_tsf(ah);
  2074. }
  2075. static int ath9k_ampdu_action(struct ieee80211_hw *hw,
  2076. enum ieee80211_ampdu_mlme_action action,
  2077. struct ieee80211_sta *sta,
  2078. u16 tid, u16 *ssn)
  2079. {
  2080. struct ath_softc *sc = hw->priv;
  2081. int ret = 0;
  2082. switch (action) {
  2083. case IEEE80211_AMPDU_RX_START:
  2084. if (!(sc->sc_flags & SC_OP_RXAGGR))
  2085. ret = -ENOTSUPP;
  2086. break;
  2087. case IEEE80211_AMPDU_RX_STOP:
  2088. break;
  2089. case IEEE80211_AMPDU_TX_START:
  2090. ret = ath_tx_aggr_start(sc, sta, tid, ssn);
  2091. if (ret < 0)
  2092. DPRINTF(sc, ATH_DBG_FATAL,
  2093. "Unable to start TX aggregation\n");
  2094. else
  2095. ieee80211_start_tx_ba_cb_irqsafe(hw, sta->addr, tid);
  2096. break;
  2097. case IEEE80211_AMPDU_TX_STOP:
  2098. ret = ath_tx_aggr_stop(sc, sta, tid);
  2099. if (ret < 0)
  2100. DPRINTF(sc, ATH_DBG_FATAL,
  2101. "Unable to stop TX aggregation\n");
  2102. ieee80211_stop_tx_ba_cb_irqsafe(hw, sta->addr, tid);
  2103. break;
  2104. case IEEE80211_AMPDU_TX_RESUME:
  2105. ath_tx_aggr_resume(sc, sta, tid);
  2106. break;
  2107. default:
  2108. DPRINTF(sc, ATH_DBG_FATAL, "Unknown AMPDU action\n");
  2109. }
  2110. return ret;
  2111. }
  2112. struct ieee80211_ops ath9k_ops = {
  2113. .tx = ath9k_tx,
  2114. .start = ath9k_start,
  2115. .stop = ath9k_stop,
  2116. .add_interface = ath9k_add_interface,
  2117. .remove_interface = ath9k_remove_interface,
  2118. .config = ath9k_config,
  2119. .config_interface = ath9k_config_interface,
  2120. .configure_filter = ath9k_configure_filter,
  2121. .sta_notify = ath9k_sta_notify,
  2122. .conf_tx = ath9k_conf_tx,
  2123. .bss_info_changed = ath9k_bss_info_changed,
  2124. .set_key = ath9k_set_key,
  2125. .get_tsf = ath9k_get_tsf,
  2126. .reset_tsf = ath9k_reset_tsf,
  2127. .ampdu_action = ath9k_ampdu_action,
  2128. };
  2129. static struct {
  2130. u32 version;
  2131. const char * name;
  2132. } ath_mac_bb_names[] = {
  2133. { AR_SREV_VERSION_5416_PCI, "5416" },
  2134. { AR_SREV_VERSION_5416_PCIE, "5418" },
  2135. { AR_SREV_VERSION_9100, "9100" },
  2136. { AR_SREV_VERSION_9160, "9160" },
  2137. { AR_SREV_VERSION_9280, "9280" },
  2138. { AR_SREV_VERSION_9285, "9285" }
  2139. };
  2140. static struct {
  2141. u16 version;
  2142. const char * name;
  2143. } ath_rf_names[] = {
  2144. { 0, "5133" },
  2145. { AR_RAD5133_SREV_MAJOR, "5133" },
  2146. { AR_RAD5122_SREV_MAJOR, "5122" },
  2147. { AR_RAD2133_SREV_MAJOR, "2133" },
  2148. { AR_RAD2122_SREV_MAJOR, "2122" }
  2149. };
  2150. /*
  2151. * Return the MAC/BB name. "????" is returned if the MAC/BB is unknown.
  2152. */
  2153. const char *
  2154. ath_mac_bb_name(u32 mac_bb_version)
  2155. {
  2156. int i;
  2157. for (i=0; i<ARRAY_SIZE(ath_mac_bb_names); i++) {
  2158. if (ath_mac_bb_names[i].version == mac_bb_version) {
  2159. return ath_mac_bb_names[i].name;
  2160. }
  2161. }
  2162. return "????";
  2163. }
  2164. /*
  2165. * Return the RF name. "????" is returned if the RF is unknown.
  2166. */
  2167. const char *
  2168. ath_rf_name(u16 rf_version)
  2169. {
  2170. int i;
  2171. for (i=0; i<ARRAY_SIZE(ath_rf_names); i++) {
  2172. if (ath_rf_names[i].version == rf_version) {
  2173. return ath_rf_names[i].name;
  2174. }
  2175. }
  2176. return "????";
  2177. }
  2178. static int __init ath9k_init(void)
  2179. {
  2180. int error;
  2181. printk(KERN_INFO "%s: %s\n", dev_info, ATH_PCI_VERSION);
  2182. /* Register rate control algorithm */
  2183. error = ath_rate_control_register();
  2184. if (error != 0) {
  2185. printk(KERN_ERR
  2186. "Unable to register rate control algorithm: %d\n",
  2187. error);
  2188. goto err_out;
  2189. }
  2190. error = ath_pci_init();
  2191. if (error < 0) {
  2192. printk(KERN_ERR
  2193. "ath_pci: No devices found, driver not installed.\n");
  2194. error = -ENODEV;
  2195. goto err_rate_unregister;
  2196. }
  2197. error = ath_ahb_init();
  2198. if (error < 0) {
  2199. error = -ENODEV;
  2200. goto err_pci_exit;
  2201. }
  2202. return 0;
  2203. err_pci_exit:
  2204. ath_pci_exit();
  2205. err_rate_unregister:
  2206. ath_rate_control_unregister();
  2207. err_out:
  2208. return error;
  2209. }
  2210. module_init(ath9k_init);
  2211. static void __exit ath9k_exit(void)
  2212. {
  2213. ath_ahb_exit();
  2214. ath_pci_exit();
  2215. ath_rate_control_unregister();
  2216. printk(KERN_INFO "%s: Driver unloaded\n", dev_info);
  2217. }
  2218. module_exit(ath9k_exit);