main.c 69 KB

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