main.c 67 KB

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