main.c 68 KB

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