main.c 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126
  1. /*
  2. * Copyright (c) 2008-2009 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. #include "btcoex.h"
  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 = 20, \
  32. }
  33. #define CHAN5G(_freq, _idx) { \
  34. .band = IEEE80211_BAND_5GHZ, \
  35. .center_freq = (_freq), \
  36. .hw_value = (_idx), \
  37. .max_power = 20, \
  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. const 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->sc_ah, ATH_DBG_CONFIG, "Rate: %2dMbps, ratecode: %2d\n",
  209. rate[i].bitrate / 10, rate[i].hw_value);
  210. }
  211. }
  212. static struct ath9k_channel *ath_get_curchannel(struct ath_softc *sc,
  213. struct ieee80211_hw *hw)
  214. {
  215. struct ieee80211_channel *curchan = hw->conf.channel;
  216. struct ath9k_channel *channel;
  217. u8 chan_idx;
  218. chan_idx = curchan->hw_value;
  219. channel = &sc->sc_ah->channels[chan_idx];
  220. ath9k_update_ichannel(sc, hw, channel);
  221. return channel;
  222. }
  223. /*
  224. * Set/change channels. If the channel is really being changed, it's done
  225. * by reseting the chip. To accomplish this we must first cleanup any pending
  226. * DMA, then restart stuff.
  227. */
  228. int ath_set_channel(struct ath_softc *sc, struct ieee80211_hw *hw,
  229. struct ath9k_channel *hchan)
  230. {
  231. struct ath_hw *ah = sc->sc_ah;
  232. bool fastcc = true, stopped;
  233. struct ieee80211_channel *channel = hw->conf.channel;
  234. int r;
  235. if (sc->sc_flags & SC_OP_INVALID)
  236. return -EIO;
  237. ath9k_ps_wakeup(sc);
  238. /*
  239. * This is only performed if the channel settings have
  240. * actually changed.
  241. *
  242. * To switch channels clear any pending DMA operations;
  243. * wait long enough for the RX fifo to drain, reset the
  244. * hardware at the new frequency, and then re-enable
  245. * the relevant bits of the h/w.
  246. */
  247. ath9k_hw_set_interrupts(ah, 0);
  248. ath_drain_all_txq(sc, false);
  249. stopped = ath_stoprecv(sc);
  250. /* XXX: do not flush receive queue here. We don't want
  251. * to flush data frames already in queue because of
  252. * changing channel. */
  253. if (!stopped || (sc->sc_flags & SC_OP_FULL_RESET))
  254. fastcc = false;
  255. DPRINTF(sc->sc_ah, ATH_DBG_CONFIG,
  256. "(%u MHz) -> (%u MHz), chanwidth: %d\n",
  257. sc->sc_ah->curchan->channel,
  258. channel->center_freq, sc->tx_chan_width);
  259. spin_lock_bh(&sc->sc_resetlock);
  260. r = ath9k_hw_reset(ah, hchan, fastcc);
  261. if (r) {
  262. DPRINTF(sc->sc_ah, ATH_DBG_FATAL,
  263. "Unable to reset channel (%u Mhz) "
  264. "reset status %d\n",
  265. channel->center_freq, r);
  266. spin_unlock_bh(&sc->sc_resetlock);
  267. goto ps_restore;
  268. }
  269. spin_unlock_bh(&sc->sc_resetlock);
  270. sc->sc_flags &= ~SC_OP_FULL_RESET;
  271. if (ath_startrecv(sc) != 0) {
  272. DPRINTF(sc->sc_ah, ATH_DBG_FATAL,
  273. "Unable to restart recv logic\n");
  274. r = -EIO;
  275. goto ps_restore;
  276. }
  277. ath_cache_conf_rate(sc, &hw->conf);
  278. ath_update_txpow(sc);
  279. ath9k_hw_set_interrupts(ah, sc->imask);
  280. ps_restore:
  281. ath9k_ps_restore(sc);
  282. return r;
  283. }
  284. /*
  285. * This routine performs the periodic noise floor calibration function
  286. * that is used to adjust and optimize the chip performance. This
  287. * takes environmental changes (location, temperature) into account.
  288. * When the task is complete, it reschedules itself depending on the
  289. * appropriate interval that was calculated.
  290. */
  291. static void ath_ani_calibrate(unsigned long data)
  292. {
  293. struct ath_softc *sc = (struct ath_softc *)data;
  294. struct ath_hw *ah = sc->sc_ah;
  295. bool longcal = false;
  296. bool shortcal = false;
  297. bool aniflag = false;
  298. unsigned int timestamp = jiffies_to_msecs(jiffies);
  299. u32 cal_interval, short_cal_interval;
  300. short_cal_interval = (ah->opmode == NL80211_IFTYPE_AP) ?
  301. ATH_AP_SHORT_CALINTERVAL : ATH_STA_SHORT_CALINTERVAL;
  302. /*
  303. * don't calibrate when we're scanning.
  304. * we are most likely not on our home channel.
  305. */
  306. spin_lock(&sc->ani_lock);
  307. if (sc->sc_flags & SC_OP_SCANNING)
  308. goto set_timer;
  309. /* Only calibrate if awake */
  310. if (sc->sc_ah->power_mode != ATH9K_PM_AWAKE)
  311. goto set_timer;
  312. ath9k_ps_wakeup(sc);
  313. /* Long calibration runs independently of short calibration. */
  314. if ((timestamp - sc->ani.longcal_timer) >= ATH_LONG_CALINTERVAL) {
  315. longcal = true;
  316. DPRINTF(sc->sc_ah, ATH_DBG_ANI, "longcal @%lu\n", jiffies);
  317. sc->ani.longcal_timer = timestamp;
  318. }
  319. /* Short calibration applies only while caldone is false */
  320. if (!sc->ani.caldone) {
  321. if ((timestamp - sc->ani.shortcal_timer) >= short_cal_interval) {
  322. shortcal = true;
  323. DPRINTF(sc->sc_ah, ATH_DBG_ANI, "shortcal @%lu\n", jiffies);
  324. sc->ani.shortcal_timer = timestamp;
  325. sc->ani.resetcal_timer = timestamp;
  326. }
  327. } else {
  328. if ((timestamp - sc->ani.resetcal_timer) >=
  329. ATH_RESTART_CALINTERVAL) {
  330. sc->ani.caldone = ath9k_hw_reset_calvalid(ah);
  331. if (sc->ani.caldone)
  332. sc->ani.resetcal_timer = timestamp;
  333. }
  334. }
  335. /* Verify whether we must check ANI */
  336. if ((timestamp - sc->ani.checkani_timer) >= ATH_ANI_POLLINTERVAL) {
  337. aniflag = true;
  338. sc->ani.checkani_timer = timestamp;
  339. }
  340. /* Skip all processing if there's nothing to do. */
  341. if (longcal || shortcal || aniflag) {
  342. /* Call ANI routine if necessary */
  343. if (aniflag)
  344. ath9k_hw_ani_monitor(ah, ah->curchan);
  345. /* Perform calibration if necessary */
  346. if (longcal || shortcal) {
  347. sc->ani.caldone = ath9k_hw_calibrate(ah, ah->curchan,
  348. sc->rx_chainmask, longcal);
  349. if (longcal)
  350. sc->ani.noise_floor = ath9k_hw_getchan_noise(ah,
  351. ah->curchan);
  352. DPRINTF(sc->sc_ah, ATH_DBG_ANI," calibrate chan %u/%x nf: %d\n",
  353. ah->curchan->channel, ah->curchan->channelFlags,
  354. sc->ani.noise_floor);
  355. }
  356. }
  357. ath9k_ps_restore(sc);
  358. set_timer:
  359. spin_unlock(&sc->ani_lock);
  360. /*
  361. * Set timer interval based on previous results.
  362. * The interval must be the shortest necessary to satisfy ANI,
  363. * short calibration and long calibration.
  364. */
  365. cal_interval = ATH_LONG_CALINTERVAL;
  366. if (sc->sc_ah->config.enable_ani)
  367. cal_interval = min(cal_interval, (u32)ATH_ANI_POLLINTERVAL);
  368. if (!sc->ani.caldone)
  369. cal_interval = min(cal_interval, (u32)short_cal_interval);
  370. mod_timer(&sc->ani.timer, jiffies + msecs_to_jiffies(cal_interval));
  371. }
  372. static void ath_start_ani(struct ath_softc *sc)
  373. {
  374. unsigned long timestamp = jiffies_to_msecs(jiffies);
  375. sc->ani.longcal_timer = timestamp;
  376. sc->ani.shortcal_timer = timestamp;
  377. sc->ani.checkani_timer = timestamp;
  378. mod_timer(&sc->ani.timer,
  379. jiffies + msecs_to_jiffies(ATH_ANI_POLLINTERVAL));
  380. }
  381. /*
  382. * Update tx/rx chainmask. For legacy association,
  383. * hard code chainmask to 1x1, for 11n association, use
  384. * the chainmask configuration, for bt coexistence, use
  385. * the chainmask configuration even in legacy mode.
  386. */
  387. void ath_update_chainmask(struct ath_softc *sc, int is_ht)
  388. {
  389. struct ath_hw *ah = sc->sc_ah;
  390. if ((sc->sc_flags & SC_OP_SCANNING) || is_ht ||
  391. (ah->btcoex_hw.scheme != ATH_BTCOEX_CFG_NONE)) {
  392. sc->tx_chainmask = sc->sc_ah->caps.tx_chainmask;
  393. sc->rx_chainmask = sc->sc_ah->caps.rx_chainmask;
  394. } else {
  395. sc->tx_chainmask = 1;
  396. sc->rx_chainmask = 1;
  397. }
  398. DPRINTF(ah, ATH_DBG_CONFIG, "tx chmask: %d, rx chmask: %d\n",
  399. sc->tx_chainmask, sc->rx_chainmask);
  400. }
  401. static void ath_node_attach(struct ath_softc *sc, struct ieee80211_sta *sta)
  402. {
  403. struct ath_node *an;
  404. an = (struct ath_node *)sta->drv_priv;
  405. if (sc->sc_flags & SC_OP_TXAGGR) {
  406. ath_tx_node_init(sc, an);
  407. an->maxampdu = 1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
  408. sta->ht_cap.ampdu_factor);
  409. an->mpdudensity = parse_mpdudensity(sta->ht_cap.ampdu_density);
  410. an->last_rssi = ATH_RSSI_DUMMY_MARKER;
  411. }
  412. }
  413. static void ath_node_detach(struct ath_softc *sc, struct ieee80211_sta *sta)
  414. {
  415. struct ath_node *an = (struct ath_node *)sta->drv_priv;
  416. if (sc->sc_flags & SC_OP_TXAGGR)
  417. ath_tx_node_cleanup(sc, an);
  418. }
  419. static void ath9k_tasklet(unsigned long data)
  420. {
  421. struct ath_softc *sc = (struct ath_softc *)data;
  422. struct ath_hw *ah = sc->sc_ah;
  423. u32 status = sc->intrstatus;
  424. ath9k_ps_wakeup(sc);
  425. if (status & ATH9K_INT_FATAL) {
  426. ath_reset(sc, false);
  427. ath9k_ps_restore(sc);
  428. return;
  429. }
  430. if (status & (ATH9K_INT_RX | ATH9K_INT_RXEOL | ATH9K_INT_RXORN)) {
  431. spin_lock_bh(&sc->rx.rxflushlock);
  432. ath_rx_tasklet(sc, 0);
  433. spin_unlock_bh(&sc->rx.rxflushlock);
  434. }
  435. if (status & ATH9K_INT_TX)
  436. ath_tx_tasklet(sc);
  437. if ((status & ATH9K_INT_TSFOOR) && sc->ps_enabled) {
  438. /*
  439. * TSF sync does not look correct; remain awake to sync with
  440. * the next Beacon.
  441. */
  442. DPRINTF(ah, ATH_DBG_PS, "TSFOOR - Sync with next Beacon\n");
  443. sc->sc_flags |= SC_OP_WAIT_FOR_BEACON | SC_OP_BEACON_SYNC;
  444. }
  445. if (ah->btcoex_hw.scheme == ATH_BTCOEX_CFG_3WIRE)
  446. if (status & ATH9K_INT_GENTIMER)
  447. ath_gen_timer_isr(sc->sc_ah);
  448. /* re-enable hardware interrupt */
  449. ath9k_hw_set_interrupts(ah, sc->imask);
  450. ath9k_ps_restore(sc);
  451. }
  452. irqreturn_t ath_isr(int irq, void *dev)
  453. {
  454. #define SCHED_INTR ( \
  455. ATH9K_INT_FATAL | \
  456. ATH9K_INT_RXORN | \
  457. ATH9K_INT_RXEOL | \
  458. ATH9K_INT_RX | \
  459. ATH9K_INT_TX | \
  460. ATH9K_INT_BMISS | \
  461. ATH9K_INT_CST | \
  462. ATH9K_INT_TSFOOR | \
  463. ATH9K_INT_GENTIMER)
  464. struct ath_softc *sc = dev;
  465. struct ath_hw *ah = sc->sc_ah;
  466. enum ath9k_int status;
  467. bool sched = false;
  468. /*
  469. * The hardware is not ready/present, don't
  470. * touch anything. Note this can happen early
  471. * on if the IRQ is shared.
  472. */
  473. if (sc->sc_flags & SC_OP_INVALID)
  474. return IRQ_NONE;
  475. /* shared irq, not for us */
  476. if (!ath9k_hw_intrpend(ah))
  477. return IRQ_NONE;
  478. /*
  479. * Figure out the reason(s) for the interrupt. Note
  480. * that the hal returns a pseudo-ISR that may include
  481. * bits we haven't explicitly enabled so we mask the
  482. * value to insure we only process bits we requested.
  483. */
  484. ath9k_hw_getisr(ah, &status); /* NB: clears ISR too */
  485. status &= sc->imask; /* discard unasked-for bits */
  486. /*
  487. * If there are no status bits set, then this interrupt was not
  488. * for me (should have been caught above).
  489. */
  490. if (!status)
  491. return IRQ_NONE;
  492. /* Cache the status */
  493. sc->intrstatus = status;
  494. if (status & SCHED_INTR)
  495. sched = true;
  496. /*
  497. * If a FATAL or RXORN interrupt is received, we have to reset the
  498. * chip immediately.
  499. */
  500. if (status & (ATH9K_INT_FATAL | ATH9K_INT_RXORN))
  501. goto chip_reset;
  502. if (status & ATH9K_INT_SWBA)
  503. tasklet_schedule(&sc->bcon_tasklet);
  504. if (status & ATH9K_INT_TXURN)
  505. ath9k_hw_updatetxtriglevel(ah, true);
  506. if (status & ATH9K_INT_MIB) {
  507. /*
  508. * Disable interrupts until we service the MIB
  509. * interrupt; otherwise it will continue to
  510. * fire.
  511. */
  512. ath9k_hw_set_interrupts(ah, 0);
  513. /*
  514. * Let the hal handle the event. We assume
  515. * it will clear whatever condition caused
  516. * the interrupt.
  517. */
  518. ath9k_hw_procmibevent(ah);
  519. ath9k_hw_set_interrupts(ah, sc->imask);
  520. }
  521. if (!(ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP))
  522. if (status & ATH9K_INT_TIM_TIMER) {
  523. /* Clear RxAbort bit so that we can
  524. * receive frames */
  525. ath9k_hw_setpower(ah, ATH9K_PM_AWAKE);
  526. ath9k_hw_setrxabort(sc->sc_ah, 0);
  527. sc->sc_flags |= SC_OP_WAIT_FOR_BEACON;
  528. }
  529. chip_reset:
  530. ath_debug_stat_interrupt(sc, status);
  531. if (sched) {
  532. /* turn off every interrupt except SWBA */
  533. ath9k_hw_set_interrupts(ah, (sc->imask & ATH9K_INT_SWBA));
  534. tasklet_schedule(&sc->intr_tq);
  535. }
  536. return IRQ_HANDLED;
  537. #undef SCHED_INTR
  538. }
  539. static u32 ath_get_extchanmode(struct ath_softc *sc,
  540. struct ieee80211_channel *chan,
  541. enum nl80211_channel_type channel_type)
  542. {
  543. u32 chanmode = 0;
  544. switch (chan->band) {
  545. case IEEE80211_BAND_2GHZ:
  546. switch(channel_type) {
  547. case NL80211_CHAN_NO_HT:
  548. case NL80211_CHAN_HT20:
  549. chanmode = CHANNEL_G_HT20;
  550. break;
  551. case NL80211_CHAN_HT40PLUS:
  552. chanmode = CHANNEL_G_HT40PLUS;
  553. break;
  554. case NL80211_CHAN_HT40MINUS:
  555. chanmode = CHANNEL_G_HT40MINUS;
  556. break;
  557. }
  558. break;
  559. case IEEE80211_BAND_5GHZ:
  560. switch(channel_type) {
  561. case NL80211_CHAN_NO_HT:
  562. case NL80211_CHAN_HT20:
  563. chanmode = CHANNEL_A_HT20;
  564. break;
  565. case NL80211_CHAN_HT40PLUS:
  566. chanmode = CHANNEL_A_HT40PLUS;
  567. break;
  568. case NL80211_CHAN_HT40MINUS:
  569. chanmode = CHANNEL_A_HT40MINUS;
  570. break;
  571. }
  572. break;
  573. default:
  574. break;
  575. }
  576. return chanmode;
  577. }
  578. static int ath_setkey_tkip(struct ath_softc *sc, u16 keyix, const u8 *key,
  579. struct ath9k_keyval *hk, const u8 *addr,
  580. bool authenticator)
  581. {
  582. const u8 *key_rxmic;
  583. const u8 *key_txmic;
  584. key_txmic = key + NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY;
  585. key_rxmic = key + NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY;
  586. if (addr == NULL) {
  587. /*
  588. * Group key installation - only two key cache entries are used
  589. * regardless of splitmic capability since group key is only
  590. * used either for TX or RX.
  591. */
  592. if (authenticator) {
  593. memcpy(hk->kv_mic, key_txmic, sizeof(hk->kv_mic));
  594. memcpy(hk->kv_txmic, key_txmic, sizeof(hk->kv_mic));
  595. } else {
  596. memcpy(hk->kv_mic, key_rxmic, sizeof(hk->kv_mic));
  597. memcpy(hk->kv_txmic, key_rxmic, sizeof(hk->kv_mic));
  598. }
  599. return ath9k_hw_set_keycache_entry(sc->sc_ah, keyix, hk, addr);
  600. }
  601. if (!sc->splitmic) {
  602. /* TX and RX keys share the same key cache entry. */
  603. memcpy(hk->kv_mic, key_rxmic, sizeof(hk->kv_mic));
  604. memcpy(hk->kv_txmic, key_txmic, sizeof(hk->kv_txmic));
  605. return ath9k_hw_set_keycache_entry(sc->sc_ah, keyix, hk, addr);
  606. }
  607. /* Separate key cache entries for TX and RX */
  608. /* TX key goes at first index, RX key at +32. */
  609. memcpy(hk->kv_mic, key_txmic, sizeof(hk->kv_mic));
  610. if (!ath9k_hw_set_keycache_entry(sc->sc_ah, keyix, hk, NULL)) {
  611. /* TX MIC entry failed. No need to proceed further */
  612. DPRINTF(sc->sc_ah, ATH_DBG_FATAL,
  613. "Setting TX MIC Key Failed\n");
  614. return 0;
  615. }
  616. memcpy(hk->kv_mic, key_rxmic, sizeof(hk->kv_mic));
  617. /* XXX delete tx key on failure? */
  618. return ath9k_hw_set_keycache_entry(sc->sc_ah, keyix + 32, hk, addr);
  619. }
  620. static int ath_reserve_key_cache_slot_tkip(struct ath_softc *sc)
  621. {
  622. int i;
  623. for (i = IEEE80211_WEP_NKID; i < sc->keymax / 2; i++) {
  624. if (test_bit(i, sc->keymap) ||
  625. test_bit(i + 64, sc->keymap))
  626. continue; /* At least one part of TKIP key allocated */
  627. if (sc->splitmic &&
  628. (test_bit(i + 32, sc->keymap) ||
  629. test_bit(i + 64 + 32, sc->keymap)))
  630. continue; /* At least one part of TKIP key allocated */
  631. /* Found a free slot for a TKIP key */
  632. return i;
  633. }
  634. return -1;
  635. }
  636. static int ath_reserve_key_cache_slot(struct ath_softc *sc)
  637. {
  638. int i;
  639. /* First, try to find slots that would not be available for TKIP. */
  640. if (sc->splitmic) {
  641. for (i = IEEE80211_WEP_NKID; i < sc->keymax / 4; i++) {
  642. if (!test_bit(i, sc->keymap) &&
  643. (test_bit(i + 32, sc->keymap) ||
  644. test_bit(i + 64, sc->keymap) ||
  645. test_bit(i + 64 + 32, sc->keymap)))
  646. return i;
  647. if (!test_bit(i + 32, sc->keymap) &&
  648. (test_bit(i, sc->keymap) ||
  649. test_bit(i + 64, sc->keymap) ||
  650. test_bit(i + 64 + 32, sc->keymap)))
  651. return i + 32;
  652. if (!test_bit(i + 64, sc->keymap) &&
  653. (test_bit(i , sc->keymap) ||
  654. test_bit(i + 32, sc->keymap) ||
  655. test_bit(i + 64 + 32, sc->keymap)))
  656. return i + 64;
  657. if (!test_bit(i + 64 + 32, sc->keymap) &&
  658. (test_bit(i, sc->keymap) ||
  659. test_bit(i + 32, sc->keymap) ||
  660. test_bit(i + 64, sc->keymap)))
  661. return i + 64 + 32;
  662. }
  663. } else {
  664. for (i = IEEE80211_WEP_NKID; i < sc->keymax / 2; i++) {
  665. if (!test_bit(i, sc->keymap) &&
  666. test_bit(i + 64, sc->keymap))
  667. return i;
  668. if (test_bit(i, sc->keymap) &&
  669. !test_bit(i + 64, sc->keymap))
  670. return i + 64;
  671. }
  672. }
  673. /* No partially used TKIP slots, pick any available slot */
  674. for (i = IEEE80211_WEP_NKID; i < sc->keymax; i++) {
  675. /* Do not allow slots that could be needed for TKIP group keys
  676. * to be used. This limitation could be removed if we know that
  677. * TKIP will not be used. */
  678. if (i >= 64 && i < 64 + IEEE80211_WEP_NKID)
  679. continue;
  680. if (sc->splitmic) {
  681. if (i >= 32 && i < 32 + IEEE80211_WEP_NKID)
  682. continue;
  683. if (i >= 64 + 32 && i < 64 + 32 + IEEE80211_WEP_NKID)
  684. continue;
  685. }
  686. if (!test_bit(i, sc->keymap))
  687. return i; /* Found a free slot for a key */
  688. }
  689. /* No free slot found */
  690. return -1;
  691. }
  692. static int ath_key_config(struct ath_softc *sc,
  693. struct ieee80211_vif *vif,
  694. struct ieee80211_sta *sta,
  695. struct ieee80211_key_conf *key)
  696. {
  697. struct ath9k_keyval hk;
  698. const u8 *mac = NULL;
  699. int ret = 0;
  700. int idx;
  701. memset(&hk, 0, sizeof(hk));
  702. switch (key->alg) {
  703. case ALG_WEP:
  704. hk.kv_type = ATH9K_CIPHER_WEP;
  705. break;
  706. case ALG_TKIP:
  707. hk.kv_type = ATH9K_CIPHER_TKIP;
  708. break;
  709. case ALG_CCMP:
  710. hk.kv_type = ATH9K_CIPHER_AES_CCM;
  711. break;
  712. default:
  713. return -EOPNOTSUPP;
  714. }
  715. hk.kv_len = key->keylen;
  716. memcpy(hk.kv_val, key->key, key->keylen);
  717. if (!(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
  718. /* For now, use the default keys for broadcast keys. This may
  719. * need to change with virtual interfaces. */
  720. idx = key->keyidx;
  721. } else if (key->keyidx) {
  722. if (WARN_ON(!sta))
  723. return -EOPNOTSUPP;
  724. mac = sta->addr;
  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 = ath9k_hw_set_keycache_entry(sc->sc_ah, 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. u8 tx_streams, rx_streams;
  777. ht_info->ht_supported = true;
  778. ht_info->cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
  779. IEEE80211_HT_CAP_SM_PS |
  780. IEEE80211_HT_CAP_SGI_40 |
  781. IEEE80211_HT_CAP_DSSSCCK40;
  782. ht_info->ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
  783. ht_info->ampdu_density = IEEE80211_HT_MPDU_DENSITY_8;
  784. /* set up supported mcs set */
  785. memset(&ht_info->mcs, 0, sizeof(ht_info->mcs));
  786. tx_streams = !(sc->tx_chainmask & (sc->tx_chainmask - 1)) ? 1 : 2;
  787. rx_streams = !(sc->rx_chainmask & (sc->rx_chainmask - 1)) ? 1 : 2;
  788. if (tx_streams != rx_streams) {
  789. DPRINTF(sc->sc_ah, ATH_DBG_CONFIG, "TX streams %d, RX streams: %d\n",
  790. tx_streams, rx_streams);
  791. ht_info->mcs.tx_params |= IEEE80211_HT_MCS_TX_RX_DIFF;
  792. ht_info->mcs.tx_params |= ((tx_streams - 1) <<
  793. IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT);
  794. }
  795. ht_info->mcs.rx_mask[0] = 0xff;
  796. if (rx_streams >= 2)
  797. ht_info->mcs.rx_mask[1] = 0xff;
  798. ht_info->mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
  799. }
  800. static void ath9k_bss_assoc_info(struct ath_softc *sc,
  801. struct ieee80211_vif *vif,
  802. struct ieee80211_bss_conf *bss_conf)
  803. {
  804. if (bss_conf->assoc) {
  805. DPRINTF(sc->sc_ah, ATH_DBG_CONFIG, "Bss Info ASSOC %d, bssid: %pM\n",
  806. bss_conf->aid, sc->curbssid);
  807. /* New association, store aid */
  808. sc->curaid = bss_conf->aid;
  809. ath9k_hw_write_associd(sc);
  810. /*
  811. * Request a re-configuration of Beacon related timers
  812. * on the receipt of the first Beacon frame (i.e.,
  813. * after time sync with the AP).
  814. */
  815. sc->sc_flags |= SC_OP_BEACON_SYNC;
  816. /* Configure the beacon */
  817. ath_beacon_config(sc, vif);
  818. /* Reset rssi stats */
  819. sc->sc_ah->stats.avgbrssi = ATH_RSSI_DUMMY_MARKER;
  820. ath_start_ani(sc);
  821. } else {
  822. DPRINTF(sc->sc_ah, ATH_DBG_CONFIG, "Bss Info DISASSOC\n");
  823. sc->curaid = 0;
  824. /* Stop ANI */
  825. del_timer_sync(&sc->ani.timer);
  826. }
  827. }
  828. /********************************/
  829. /* LED functions */
  830. /********************************/
  831. static void ath_led_blink_work(struct work_struct *work)
  832. {
  833. struct ath_softc *sc = container_of(work, struct ath_softc,
  834. ath_led_blink_work.work);
  835. if (!(sc->sc_flags & SC_OP_LED_ASSOCIATED))
  836. return;
  837. if ((sc->led_on_duration == ATH_LED_ON_DURATION_IDLE) ||
  838. (sc->led_off_duration == ATH_LED_OFF_DURATION_IDLE))
  839. ath9k_hw_set_gpio(sc->sc_ah, sc->sc_ah->led_pin, 0);
  840. else
  841. ath9k_hw_set_gpio(sc->sc_ah, sc->sc_ah->led_pin,
  842. (sc->sc_flags & SC_OP_LED_ON) ? 1 : 0);
  843. ieee80211_queue_delayed_work(sc->hw,
  844. &sc->ath_led_blink_work,
  845. (sc->sc_flags & SC_OP_LED_ON) ?
  846. msecs_to_jiffies(sc->led_off_duration) :
  847. msecs_to_jiffies(sc->led_on_duration));
  848. sc->led_on_duration = sc->led_on_cnt ?
  849. max((ATH_LED_ON_DURATION_IDLE - sc->led_on_cnt), 25) :
  850. ATH_LED_ON_DURATION_IDLE;
  851. sc->led_off_duration = sc->led_off_cnt ?
  852. max((ATH_LED_OFF_DURATION_IDLE - sc->led_off_cnt), 10) :
  853. ATH_LED_OFF_DURATION_IDLE;
  854. sc->led_on_cnt = sc->led_off_cnt = 0;
  855. if (sc->sc_flags & SC_OP_LED_ON)
  856. sc->sc_flags &= ~SC_OP_LED_ON;
  857. else
  858. sc->sc_flags |= SC_OP_LED_ON;
  859. }
  860. static void ath_led_brightness(struct led_classdev *led_cdev,
  861. enum led_brightness brightness)
  862. {
  863. struct ath_led *led = container_of(led_cdev, struct ath_led, led_cdev);
  864. struct ath_softc *sc = led->sc;
  865. switch (brightness) {
  866. case LED_OFF:
  867. if (led->led_type == ATH_LED_ASSOC ||
  868. led->led_type == ATH_LED_RADIO) {
  869. ath9k_hw_set_gpio(sc->sc_ah, sc->sc_ah->led_pin,
  870. (led->led_type == ATH_LED_RADIO));
  871. sc->sc_flags &= ~SC_OP_LED_ASSOCIATED;
  872. if (led->led_type == ATH_LED_RADIO)
  873. sc->sc_flags &= ~SC_OP_LED_ON;
  874. } else {
  875. sc->led_off_cnt++;
  876. }
  877. break;
  878. case LED_FULL:
  879. if (led->led_type == ATH_LED_ASSOC) {
  880. sc->sc_flags |= SC_OP_LED_ASSOCIATED;
  881. ieee80211_queue_delayed_work(sc->hw,
  882. &sc->ath_led_blink_work, 0);
  883. } else if (led->led_type == ATH_LED_RADIO) {
  884. ath9k_hw_set_gpio(sc->sc_ah, sc->sc_ah->led_pin, 0);
  885. sc->sc_flags |= SC_OP_LED_ON;
  886. } else {
  887. sc->led_on_cnt++;
  888. }
  889. break;
  890. default:
  891. break;
  892. }
  893. }
  894. static int ath_register_led(struct ath_softc *sc, struct ath_led *led,
  895. char *trigger)
  896. {
  897. int ret;
  898. led->sc = sc;
  899. led->led_cdev.name = led->name;
  900. led->led_cdev.default_trigger = trigger;
  901. led->led_cdev.brightness_set = ath_led_brightness;
  902. ret = led_classdev_register(wiphy_dev(sc->hw->wiphy), &led->led_cdev);
  903. if (ret)
  904. DPRINTF(sc->sc_ah, ATH_DBG_FATAL,
  905. "Failed to register led:%s", led->name);
  906. else
  907. led->registered = 1;
  908. return ret;
  909. }
  910. static void ath_unregister_led(struct ath_led *led)
  911. {
  912. if (led->registered) {
  913. led_classdev_unregister(&led->led_cdev);
  914. led->registered = 0;
  915. }
  916. }
  917. static void ath_deinit_leds(struct ath_softc *sc)
  918. {
  919. ath_unregister_led(&sc->assoc_led);
  920. sc->sc_flags &= ~SC_OP_LED_ASSOCIATED;
  921. ath_unregister_led(&sc->tx_led);
  922. ath_unregister_led(&sc->rx_led);
  923. ath_unregister_led(&sc->radio_led);
  924. ath9k_hw_set_gpio(sc->sc_ah, sc->sc_ah->led_pin, 1);
  925. }
  926. static void ath_init_leds(struct ath_softc *sc)
  927. {
  928. char *trigger;
  929. int ret;
  930. if (AR_SREV_9287(sc->sc_ah))
  931. sc->sc_ah->led_pin = ATH_LED_PIN_9287;
  932. else
  933. sc->sc_ah->led_pin = ATH_LED_PIN_DEF;
  934. /* Configure gpio 1 for output */
  935. ath9k_hw_cfg_output(sc->sc_ah, sc->sc_ah->led_pin,
  936. AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
  937. /* LED off, active low */
  938. ath9k_hw_set_gpio(sc->sc_ah, sc->sc_ah->led_pin, 1);
  939. INIT_DELAYED_WORK(&sc->ath_led_blink_work, ath_led_blink_work);
  940. trigger = ieee80211_get_radio_led_name(sc->hw);
  941. snprintf(sc->radio_led.name, sizeof(sc->radio_led.name),
  942. "ath9k-%s::radio", wiphy_name(sc->hw->wiphy));
  943. ret = ath_register_led(sc, &sc->radio_led, trigger);
  944. sc->radio_led.led_type = ATH_LED_RADIO;
  945. if (ret)
  946. goto fail;
  947. trigger = ieee80211_get_assoc_led_name(sc->hw);
  948. snprintf(sc->assoc_led.name, sizeof(sc->assoc_led.name),
  949. "ath9k-%s::assoc", wiphy_name(sc->hw->wiphy));
  950. ret = ath_register_led(sc, &sc->assoc_led, trigger);
  951. sc->assoc_led.led_type = ATH_LED_ASSOC;
  952. if (ret)
  953. goto fail;
  954. trigger = ieee80211_get_tx_led_name(sc->hw);
  955. snprintf(sc->tx_led.name, sizeof(sc->tx_led.name),
  956. "ath9k-%s::tx", wiphy_name(sc->hw->wiphy));
  957. ret = ath_register_led(sc, &sc->tx_led, trigger);
  958. sc->tx_led.led_type = ATH_LED_TX;
  959. if (ret)
  960. goto fail;
  961. trigger = ieee80211_get_rx_led_name(sc->hw);
  962. snprintf(sc->rx_led.name, sizeof(sc->rx_led.name),
  963. "ath9k-%s::rx", wiphy_name(sc->hw->wiphy));
  964. ret = ath_register_led(sc, &sc->rx_led, trigger);
  965. sc->rx_led.led_type = ATH_LED_RX;
  966. if (ret)
  967. goto fail;
  968. return;
  969. fail:
  970. cancel_delayed_work_sync(&sc->ath_led_blink_work);
  971. ath_deinit_leds(sc);
  972. }
  973. void ath_radio_enable(struct ath_softc *sc)
  974. {
  975. struct ath_hw *ah = sc->sc_ah;
  976. struct ieee80211_channel *channel = sc->hw->conf.channel;
  977. int r;
  978. ath9k_ps_wakeup(sc);
  979. ath9k_hw_configpcipowersave(ah, 0, 0);
  980. if (!ah->curchan)
  981. ah->curchan = ath_get_curchannel(sc, sc->hw);
  982. spin_lock_bh(&sc->sc_resetlock);
  983. r = ath9k_hw_reset(ah, ah->curchan, false);
  984. if (r) {
  985. DPRINTF(sc->sc_ah, ATH_DBG_FATAL,
  986. "Unable to reset channel %u (%uMhz) ",
  987. "reset status %d\n",
  988. channel->center_freq, r);
  989. }
  990. spin_unlock_bh(&sc->sc_resetlock);
  991. ath_update_txpow(sc);
  992. if (ath_startrecv(sc) != 0) {
  993. DPRINTF(sc->sc_ah, ATH_DBG_FATAL,
  994. "Unable to restart recv logic\n");
  995. return;
  996. }
  997. if (sc->sc_flags & SC_OP_BEACONS)
  998. ath_beacon_config(sc, NULL); /* restart beacons */
  999. /* Re-Enable interrupts */
  1000. ath9k_hw_set_interrupts(ah, sc->imask);
  1001. /* Enable LED */
  1002. ath9k_hw_cfg_output(ah, ah->led_pin,
  1003. AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
  1004. ath9k_hw_set_gpio(ah, ah->led_pin, 0);
  1005. ieee80211_wake_queues(sc->hw);
  1006. ath9k_ps_restore(sc);
  1007. }
  1008. void ath_radio_disable(struct ath_softc *sc)
  1009. {
  1010. struct ath_hw *ah = sc->sc_ah;
  1011. struct ieee80211_channel *channel = sc->hw->conf.channel;
  1012. int r;
  1013. ath9k_ps_wakeup(sc);
  1014. ieee80211_stop_queues(sc->hw);
  1015. /* Disable LED */
  1016. ath9k_hw_set_gpio(ah, ah->led_pin, 1);
  1017. ath9k_hw_cfg_gpio_input(ah, ah->led_pin);
  1018. /* Disable interrupts */
  1019. ath9k_hw_set_interrupts(ah, 0);
  1020. ath_drain_all_txq(sc, false); /* clear pending tx frames */
  1021. ath_stoprecv(sc); /* turn off frame recv */
  1022. ath_flushrecv(sc); /* flush recv queue */
  1023. if (!ah->curchan)
  1024. ah->curchan = ath_get_curchannel(sc, sc->hw);
  1025. spin_lock_bh(&sc->sc_resetlock);
  1026. r = ath9k_hw_reset(ah, ah->curchan, false);
  1027. if (r) {
  1028. DPRINTF(sc->sc_ah, ATH_DBG_FATAL,
  1029. "Unable to reset channel %u (%uMhz) "
  1030. "reset status %d\n",
  1031. channel->center_freq, r);
  1032. }
  1033. spin_unlock_bh(&sc->sc_resetlock);
  1034. ath9k_hw_phy_disable(ah);
  1035. ath9k_hw_configpcipowersave(ah, 1, 1);
  1036. ath9k_ps_restore(sc);
  1037. ath9k_hw_setpower(ah, ATH9K_PM_FULL_SLEEP);
  1038. }
  1039. /*******************/
  1040. /* Rfkill */
  1041. /*******************/
  1042. static bool ath_is_rfkill_set(struct ath_softc *sc)
  1043. {
  1044. struct ath_hw *ah = sc->sc_ah;
  1045. return ath9k_hw_gpio_get(ah, ah->rfkill_gpio) ==
  1046. ah->rfkill_polarity;
  1047. }
  1048. static void ath9k_rfkill_poll_state(struct ieee80211_hw *hw)
  1049. {
  1050. struct ath_wiphy *aphy = hw->priv;
  1051. struct ath_softc *sc = aphy->sc;
  1052. bool blocked = !!ath_is_rfkill_set(sc);
  1053. wiphy_rfkill_set_hw_state(hw->wiphy, blocked);
  1054. }
  1055. static void ath_start_rfkill_poll(struct ath_softc *sc)
  1056. {
  1057. struct ath_hw *ah = sc->sc_ah;
  1058. if (ah->caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
  1059. wiphy_rfkill_start_polling(sc->hw->wiphy);
  1060. }
  1061. void ath_cleanup(struct ath_softc *sc)
  1062. {
  1063. ath_detach(sc);
  1064. free_irq(sc->irq, sc);
  1065. ath_bus_cleanup(sc);
  1066. kfree(sc->sec_wiphy);
  1067. ieee80211_free_hw(sc->hw);
  1068. }
  1069. void ath_detach(struct ath_softc *sc)
  1070. {
  1071. struct ieee80211_hw *hw = sc->hw;
  1072. struct ath_hw *ah = sc->sc_ah;
  1073. int i = 0;
  1074. ath9k_ps_wakeup(sc);
  1075. dev_dbg(sc->dev, "Detach ATH hw\n");
  1076. ath_deinit_leds(sc);
  1077. wiphy_rfkill_stop_polling(sc->hw->wiphy);
  1078. for (i = 0; i < sc->num_sec_wiphy; i++) {
  1079. struct ath_wiphy *aphy = sc->sec_wiphy[i];
  1080. if (aphy == NULL)
  1081. continue;
  1082. sc->sec_wiphy[i] = NULL;
  1083. ieee80211_unregister_hw(aphy->hw);
  1084. ieee80211_free_hw(aphy->hw);
  1085. }
  1086. ieee80211_unregister_hw(hw);
  1087. ath_rx_cleanup(sc);
  1088. ath_tx_cleanup(sc);
  1089. tasklet_kill(&sc->intr_tq);
  1090. tasklet_kill(&sc->bcon_tasklet);
  1091. if (!(sc->sc_flags & SC_OP_INVALID))
  1092. ath9k_hw_setpower(ah, ATH9K_PM_AWAKE);
  1093. /* cleanup tx queues */
  1094. for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++)
  1095. if (ATH_TXQ_SETUP(sc, i))
  1096. ath_tx_cleanupq(sc, &sc->tx.txq[i]);
  1097. if ((sc->btcoex.no_stomp_timer) &&
  1098. ah->btcoex_hw.scheme == ATH_BTCOEX_CFG_3WIRE)
  1099. ath_gen_timer_free(ah, sc->btcoex.no_stomp_timer);
  1100. ath9k_hw_detach(ah);
  1101. ath9k_exit_debug(ah);
  1102. sc->sc_ah = NULL;
  1103. }
  1104. static int ath9k_reg_notifier(struct wiphy *wiphy,
  1105. struct regulatory_request *request)
  1106. {
  1107. struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
  1108. struct ath_wiphy *aphy = hw->priv;
  1109. struct ath_softc *sc = aphy->sc;
  1110. struct ath_regulatory *reg = &sc->common.regulatory;
  1111. return ath_reg_notifier_apply(wiphy, request, reg);
  1112. }
  1113. /*
  1114. * Detects if there is any priority bt traffic
  1115. */
  1116. static void ath_detect_bt_priority(struct ath_softc *sc)
  1117. {
  1118. struct ath_btcoex *btcoex = &sc->btcoex;
  1119. struct ath_hw *ah = sc->sc_ah;
  1120. if (ath9k_hw_gpio_get(sc->sc_ah, ah->btcoex_hw.btpriority_gpio))
  1121. btcoex->bt_priority_cnt++;
  1122. if (time_after(jiffies, btcoex->bt_priority_time +
  1123. msecs_to_jiffies(ATH_BT_PRIORITY_TIME_THRESHOLD))) {
  1124. if (btcoex->bt_priority_cnt >= ATH_BT_CNT_THRESHOLD) {
  1125. DPRINTF(sc->sc_ah, ATH_DBG_BTCOEX,
  1126. "BT priority traffic detected");
  1127. sc->sc_flags |= SC_OP_BT_PRIORITY_DETECTED;
  1128. } else {
  1129. sc->sc_flags &= ~SC_OP_BT_PRIORITY_DETECTED;
  1130. }
  1131. btcoex->bt_priority_cnt = 0;
  1132. btcoex->bt_priority_time = jiffies;
  1133. }
  1134. }
  1135. static void ath9k_hw_btcoex_set_weight(struct ath_hw *ah,
  1136. u32 bt_weight,
  1137. u32 wlan_weight)
  1138. {
  1139. struct ath_btcoex_hw *btcoex_hw = &ah->btcoex_hw;
  1140. btcoex_hw->bt_coex_weights = SM(bt_weight, AR_BTCOEX_BT_WGHT) |
  1141. SM(wlan_weight, AR_BTCOEX_WL_WGHT);
  1142. }
  1143. static void ath9k_hw_btcoex_init_weight(struct ath_hw *ah)
  1144. {
  1145. ath9k_hw_btcoex_set_weight(ah, AR_BT_COEX_WGHT, AR_STOMP_LOW_WLAN_WGHT);
  1146. }
  1147. /*
  1148. * Configures appropriate weight based on stomp type.
  1149. */
  1150. static void ath9k_btcoex_bt_stomp(struct ath_softc *sc,
  1151. enum ath_stomp_type stomp_type)
  1152. {
  1153. struct ath_hw *ah = sc->sc_ah;
  1154. switch (stomp_type) {
  1155. case ATH_BTCOEX_STOMP_ALL:
  1156. ath9k_hw_btcoex_set_weight(ah, AR_BT_COEX_WGHT,
  1157. AR_STOMP_ALL_WLAN_WGHT);
  1158. break;
  1159. case ATH_BTCOEX_STOMP_LOW:
  1160. ath9k_hw_btcoex_set_weight(ah, AR_BT_COEX_WGHT,
  1161. AR_STOMP_LOW_WLAN_WGHT);
  1162. break;
  1163. case ATH_BTCOEX_STOMP_NONE:
  1164. ath9k_hw_btcoex_set_weight(ah, AR_BT_COEX_WGHT,
  1165. AR_STOMP_NONE_WLAN_WGHT);
  1166. break;
  1167. default:
  1168. DPRINTF(ah, ATH_DBG_BTCOEX, "Invalid Stomptype\n");
  1169. break;
  1170. }
  1171. ath9k_hw_btcoex_enable(ah);
  1172. }
  1173. /*
  1174. * This is the master bt coex timer which runs for every
  1175. * 45ms, bt traffic will be given priority during 55% of this
  1176. * period while wlan gets remaining 45%
  1177. */
  1178. static void ath_btcoex_period_timer(unsigned long data)
  1179. {
  1180. struct ath_softc *sc = (struct ath_softc *) data;
  1181. struct ath_hw *ah = sc->sc_ah;
  1182. struct ath_btcoex *btcoex = &sc->btcoex;
  1183. ath_detect_bt_priority(sc);
  1184. spin_lock_bh(&btcoex->btcoex_lock);
  1185. ath9k_btcoex_bt_stomp(sc, btcoex->bt_stomp_type);
  1186. spin_unlock_bh(&btcoex->btcoex_lock);
  1187. if (btcoex->btcoex_period != btcoex->btcoex_no_stomp) {
  1188. if (btcoex->hw_timer_enabled)
  1189. ath_gen_timer_stop(ah, btcoex->no_stomp_timer);
  1190. ath_gen_timer_start(ah,
  1191. btcoex->no_stomp_timer,
  1192. (ath9k_hw_gettsf32(ah) +
  1193. btcoex->btcoex_no_stomp),
  1194. btcoex->btcoex_no_stomp * 10);
  1195. btcoex->hw_timer_enabled = true;
  1196. }
  1197. mod_timer(&btcoex->period_timer, jiffies +
  1198. msecs_to_jiffies(ATH_BTCOEX_DEF_BT_PERIOD));
  1199. }
  1200. /*
  1201. * Generic tsf based hw timer which configures weight
  1202. * registers to time slice between wlan and bt traffic
  1203. */
  1204. static void ath_btcoex_no_stomp_timer(void *arg)
  1205. {
  1206. struct ath_softc *sc = (struct ath_softc *)arg;
  1207. struct ath_hw *ah = sc->sc_ah;
  1208. struct ath_btcoex *btcoex = &sc->btcoex;
  1209. DPRINTF(ah, ATH_DBG_BTCOEX, "no stomp timer running \n");
  1210. spin_lock_bh(&btcoex->btcoex_lock);
  1211. if (btcoex->bt_stomp_type == ATH_BTCOEX_STOMP_LOW)
  1212. ath9k_btcoex_bt_stomp(sc, ATH_BTCOEX_STOMP_NONE);
  1213. else if (btcoex->bt_stomp_type == ATH_BTCOEX_STOMP_ALL)
  1214. ath9k_btcoex_bt_stomp(sc, ATH_BTCOEX_STOMP_LOW);
  1215. spin_unlock_bh(&btcoex->btcoex_lock);
  1216. }
  1217. static int ath_init_btcoex_timer(struct ath_softc *sc)
  1218. {
  1219. struct ath_btcoex *btcoex = &sc->btcoex;
  1220. btcoex->btcoex_period = ATH_BTCOEX_DEF_BT_PERIOD * 1000;
  1221. btcoex->btcoex_no_stomp = (100 - ATH_BTCOEX_DEF_DUTY_CYCLE) *
  1222. btcoex->btcoex_period / 100;
  1223. setup_timer(&btcoex->period_timer, ath_btcoex_period_timer,
  1224. (unsigned long) sc);
  1225. spin_lock_init(&btcoex->btcoex_lock);
  1226. btcoex->no_stomp_timer = ath_gen_timer_alloc(sc->sc_ah,
  1227. ath_btcoex_no_stomp_timer,
  1228. ath_btcoex_no_stomp_timer,
  1229. (void *) sc, AR_FIRST_NDP_TIMER);
  1230. if (!btcoex->no_stomp_timer)
  1231. return -ENOMEM;
  1232. return 0;
  1233. }
  1234. /*
  1235. * Initialize and fill ath_softc, ath_sofct is the
  1236. * "Software Carrier" struct. Historically it has existed
  1237. * to allow the separation between hardware specific
  1238. * variables (now in ath_hw) and driver specific variables.
  1239. */
  1240. static int ath_init_softc(u16 devid, struct ath_softc *sc, u16 subsysid)
  1241. {
  1242. struct ath_hw *ah = NULL;
  1243. int r = 0, i;
  1244. int csz = 0;
  1245. int qnum;
  1246. /* XXX: hardware will not be ready until ath_open() being called */
  1247. sc->sc_flags |= SC_OP_INVALID;
  1248. spin_lock_init(&sc->wiphy_lock);
  1249. spin_lock_init(&sc->sc_resetlock);
  1250. spin_lock_init(&sc->sc_serial_rw);
  1251. spin_lock_init(&sc->ani_lock);
  1252. spin_lock_init(&sc->sc_pm_lock);
  1253. mutex_init(&sc->mutex);
  1254. tasklet_init(&sc->intr_tq, ath9k_tasklet, (unsigned long)sc);
  1255. tasklet_init(&sc->bcon_tasklet, ath_beacon_tasklet,
  1256. (unsigned long)sc);
  1257. /*
  1258. * Cache line size is used to size and align various
  1259. * structures used to communicate with the hardware.
  1260. */
  1261. ath_read_cachesize(sc, &csz);
  1262. /* XXX assert csz is non-zero */
  1263. sc->common.cachelsz = csz << 2; /* convert to bytes */
  1264. ah = kzalloc(sizeof(struct ath_hw), GFP_KERNEL);
  1265. if (!ah) {
  1266. r = -ENOMEM;
  1267. goto bad_no_ah;
  1268. }
  1269. ah->ah_sc = sc;
  1270. ah->hw_version.devid = devid;
  1271. ah->hw_version.subsysid = subsysid;
  1272. sc->sc_ah = ah;
  1273. if (ath9k_init_debug(ah) < 0)
  1274. dev_err(sc->dev, "Unable to create debugfs files\n");
  1275. r = ath9k_hw_init(ah);
  1276. if (r) {
  1277. DPRINTF(ah, ATH_DBG_FATAL,
  1278. "Unable to initialize hardware; "
  1279. "initialization status: %d\n", r);
  1280. goto bad;
  1281. }
  1282. /* Get the hardware key cache size. */
  1283. sc->keymax = ah->caps.keycache_size;
  1284. if (sc->keymax > ATH_KEYMAX) {
  1285. DPRINTF(ah, ATH_DBG_ANY,
  1286. "Warning, using only %u entries in %u key cache\n",
  1287. ATH_KEYMAX, sc->keymax);
  1288. sc->keymax = ATH_KEYMAX;
  1289. }
  1290. /*
  1291. * Reset the key cache since some parts do not
  1292. * reset the contents on initial power up.
  1293. */
  1294. for (i = 0; i < sc->keymax; i++)
  1295. ath9k_hw_keyreset(ah, (u16) i);
  1296. /* default to MONITOR mode */
  1297. sc->sc_ah->opmode = NL80211_IFTYPE_MONITOR;
  1298. /* Setup rate tables */
  1299. ath_rate_attach(sc);
  1300. ath_setup_rates(sc, IEEE80211_BAND_2GHZ);
  1301. ath_setup_rates(sc, IEEE80211_BAND_5GHZ);
  1302. /*
  1303. * Allocate hardware transmit queues: one queue for
  1304. * beacon frames and one data queue for each QoS
  1305. * priority. Note that the hal handles reseting
  1306. * these queues at the needed time.
  1307. */
  1308. sc->beacon.beaconq = ath_beaconq_setup(ah);
  1309. if (sc->beacon.beaconq == -1) {
  1310. DPRINTF(ah, ATH_DBG_FATAL,
  1311. "Unable to setup a beacon xmit queue\n");
  1312. r = -EIO;
  1313. goto bad2;
  1314. }
  1315. sc->beacon.cabq = ath_txq_setup(sc, ATH9K_TX_QUEUE_CAB, 0);
  1316. if (sc->beacon.cabq == NULL) {
  1317. DPRINTF(ah, ATH_DBG_FATAL,
  1318. "Unable to setup CAB xmit queue\n");
  1319. r = -EIO;
  1320. goto bad2;
  1321. }
  1322. sc->config.cabqReadytime = ATH_CABQ_READY_TIME;
  1323. ath_cabq_update(sc);
  1324. for (i = 0; i < ARRAY_SIZE(sc->tx.hwq_map); i++)
  1325. sc->tx.hwq_map[i] = -1;
  1326. /* Setup data queues */
  1327. /* NB: ensure BK queue is the lowest priority h/w queue */
  1328. if (!ath_tx_setup(sc, ATH9K_WME_AC_BK)) {
  1329. DPRINTF(ah, ATH_DBG_FATAL,
  1330. "Unable to setup xmit queue for BK traffic\n");
  1331. r = -EIO;
  1332. goto bad2;
  1333. }
  1334. if (!ath_tx_setup(sc, ATH9K_WME_AC_BE)) {
  1335. DPRINTF(ah, ATH_DBG_FATAL,
  1336. "Unable to setup xmit queue for BE traffic\n");
  1337. r = -EIO;
  1338. goto bad2;
  1339. }
  1340. if (!ath_tx_setup(sc, ATH9K_WME_AC_VI)) {
  1341. DPRINTF(ah, ATH_DBG_FATAL,
  1342. "Unable to setup xmit queue for VI traffic\n");
  1343. r = -EIO;
  1344. goto bad2;
  1345. }
  1346. if (!ath_tx_setup(sc, ATH9K_WME_AC_VO)) {
  1347. DPRINTF(ah, ATH_DBG_FATAL,
  1348. "Unable to setup xmit queue for VO traffic\n");
  1349. r = -EIO;
  1350. goto bad2;
  1351. }
  1352. /* Initializes the noise floor to a reasonable default value.
  1353. * Later on this will be updated during ANI processing. */
  1354. sc->ani.noise_floor = ATH_DEFAULT_NOISE_FLOOR;
  1355. setup_timer(&sc->ani.timer, ath_ani_calibrate, (unsigned long)sc);
  1356. if (ath9k_hw_getcapability(ah, ATH9K_CAP_CIPHER,
  1357. ATH9K_CIPHER_TKIP, NULL)) {
  1358. /*
  1359. * Whether we should enable h/w TKIP MIC.
  1360. * XXX: if we don't support WME TKIP MIC, then we wouldn't
  1361. * report WMM capable, so it's always safe to turn on
  1362. * TKIP MIC in this case.
  1363. */
  1364. ath9k_hw_setcapability(sc->sc_ah, ATH9K_CAP_TKIP_MIC,
  1365. 0, 1, NULL);
  1366. }
  1367. /*
  1368. * Check whether the separate key cache entries
  1369. * are required to handle both tx+rx MIC keys.
  1370. * With split mic keys the number of stations is limited
  1371. * to 27 otherwise 59.
  1372. */
  1373. if (ath9k_hw_getcapability(ah, ATH9K_CAP_CIPHER,
  1374. ATH9K_CIPHER_TKIP, NULL)
  1375. && ath9k_hw_getcapability(ah, ATH9K_CAP_CIPHER,
  1376. ATH9K_CIPHER_MIC, NULL)
  1377. && ath9k_hw_getcapability(ah, ATH9K_CAP_TKIP_SPLIT,
  1378. 0, NULL))
  1379. sc->splitmic = 1;
  1380. /* turn on mcast key search if possible */
  1381. if (!ath9k_hw_getcapability(ah, ATH9K_CAP_MCAST_KEYSRCH, 0, NULL))
  1382. (void)ath9k_hw_setcapability(ah, ATH9K_CAP_MCAST_KEYSRCH, 1,
  1383. 1, NULL);
  1384. sc->config.txpowlimit = ATH_TXPOWER_MAX;
  1385. /* 11n Capabilities */
  1386. if (ah->caps.hw_caps & ATH9K_HW_CAP_HT) {
  1387. sc->sc_flags |= SC_OP_TXAGGR;
  1388. sc->sc_flags |= SC_OP_RXAGGR;
  1389. }
  1390. sc->tx_chainmask = ah->caps.tx_chainmask;
  1391. sc->rx_chainmask = ah->caps.rx_chainmask;
  1392. ath9k_hw_setcapability(ah, ATH9K_CAP_DIVERSITY, 1, true, NULL);
  1393. sc->rx.defant = ath9k_hw_getdefantenna(ah);
  1394. if (ah->caps.hw_caps & ATH9K_HW_CAP_BSSIDMASK)
  1395. memcpy(sc->bssidmask, ath_bcast_mac, ETH_ALEN);
  1396. sc->beacon.slottime = ATH9K_SLOT_TIME_9; /* default to short slot time */
  1397. /* initialize beacon slots */
  1398. for (i = 0; i < ARRAY_SIZE(sc->beacon.bslot); i++) {
  1399. sc->beacon.bslot[i] = NULL;
  1400. sc->beacon.bslot_aphy[i] = NULL;
  1401. }
  1402. /* setup channels and rates */
  1403. sc->sbands[IEEE80211_BAND_2GHZ].channels = ath9k_2ghz_chantable;
  1404. sc->sbands[IEEE80211_BAND_2GHZ].bitrates =
  1405. sc->rates[IEEE80211_BAND_2GHZ];
  1406. sc->sbands[IEEE80211_BAND_2GHZ].band = IEEE80211_BAND_2GHZ;
  1407. sc->sbands[IEEE80211_BAND_2GHZ].n_channels =
  1408. ARRAY_SIZE(ath9k_2ghz_chantable);
  1409. if (test_bit(ATH9K_MODE_11A, sc->sc_ah->caps.wireless_modes)) {
  1410. sc->sbands[IEEE80211_BAND_5GHZ].channels = ath9k_5ghz_chantable;
  1411. sc->sbands[IEEE80211_BAND_5GHZ].bitrates =
  1412. sc->rates[IEEE80211_BAND_5GHZ];
  1413. sc->sbands[IEEE80211_BAND_5GHZ].band = IEEE80211_BAND_5GHZ;
  1414. sc->sbands[IEEE80211_BAND_5GHZ].n_channels =
  1415. ARRAY_SIZE(ath9k_5ghz_chantable);
  1416. }
  1417. switch (ah->btcoex_hw.scheme) {
  1418. case ATH_BTCOEX_CFG_NONE:
  1419. break;
  1420. case ATH_BTCOEX_CFG_2WIRE:
  1421. ath9k_hw_btcoex_init_2wire(ah);
  1422. break;
  1423. case ATH_BTCOEX_CFG_3WIRE:
  1424. ath9k_hw_btcoex_init_3wire(ah);
  1425. r = ath_init_btcoex_timer(sc);
  1426. if (r)
  1427. goto bad2;
  1428. qnum = ath_tx_get_qnum(sc, ATH9K_TX_QUEUE_DATA, ATH9K_WME_AC_BE);
  1429. ath9k_hw_init_btcoex_hw(ah, qnum);
  1430. sc->btcoex.bt_stomp_type = ATH_BTCOEX_STOMP_LOW;
  1431. break;
  1432. default:
  1433. WARN_ON(1);
  1434. break;
  1435. }
  1436. return 0;
  1437. bad2:
  1438. /* cleanup tx queues */
  1439. for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++)
  1440. if (ATH_TXQ_SETUP(sc, i))
  1441. ath_tx_cleanupq(sc, &sc->tx.txq[i]);
  1442. bad:
  1443. ath9k_hw_detach(ah);
  1444. bad_no_ah:
  1445. ath9k_exit_debug(sc->sc_ah);
  1446. sc->sc_ah = NULL;
  1447. return r;
  1448. }
  1449. void ath_set_hw_capab(struct ath_softc *sc, struct ieee80211_hw *hw)
  1450. {
  1451. hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
  1452. IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
  1453. IEEE80211_HW_SIGNAL_DBM |
  1454. IEEE80211_HW_AMPDU_AGGREGATION |
  1455. IEEE80211_HW_SUPPORTS_PS |
  1456. IEEE80211_HW_PS_NULLFUNC_STACK |
  1457. IEEE80211_HW_SPECTRUM_MGMT;
  1458. if (AR_SREV_9160_10_OR_LATER(sc->sc_ah) || modparam_nohwcrypt)
  1459. hw->flags |= IEEE80211_HW_MFP_CAPABLE;
  1460. hw->wiphy->interface_modes =
  1461. BIT(NL80211_IFTYPE_AP) |
  1462. BIT(NL80211_IFTYPE_STATION) |
  1463. BIT(NL80211_IFTYPE_ADHOC) |
  1464. BIT(NL80211_IFTYPE_MESH_POINT);
  1465. hw->queues = 4;
  1466. hw->max_rates = 4;
  1467. hw->channel_change_time = 5000;
  1468. hw->max_listen_interval = 10;
  1469. /* Hardware supports 10 but we use 4 */
  1470. hw->max_rate_tries = 4;
  1471. hw->sta_data_size = sizeof(struct ath_node);
  1472. hw->vif_data_size = sizeof(struct ath_vif);
  1473. hw->rate_control_algorithm = "ath9k_rate_control";
  1474. hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
  1475. &sc->sbands[IEEE80211_BAND_2GHZ];
  1476. if (test_bit(ATH9K_MODE_11A, sc->sc_ah->caps.wireless_modes))
  1477. hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
  1478. &sc->sbands[IEEE80211_BAND_5GHZ];
  1479. }
  1480. /* Device driver core initialization */
  1481. int ath_init_device(u16 devid, struct ath_softc *sc, u16 subsysid)
  1482. {
  1483. struct ieee80211_hw *hw = sc->hw;
  1484. struct ath_hw *ah;
  1485. int error = 0, i;
  1486. struct ath_regulatory *reg;
  1487. dev_dbg(sc->dev, "Attach ATH hw\n");
  1488. error = ath_init_softc(devid, sc, subsysid);
  1489. if (error != 0)
  1490. return error;
  1491. ah = sc->sc_ah;
  1492. /* get mac address from hardware and set in mac80211 */
  1493. SET_IEEE80211_PERM_ADDR(hw, ah->macaddr);
  1494. ath_set_hw_capab(sc, hw);
  1495. error = ath_regd_init(&sc->common.regulatory, sc->hw->wiphy,
  1496. ath9k_reg_notifier);
  1497. if (error)
  1498. return error;
  1499. reg = &sc->common.regulatory;
  1500. if (ah->caps.hw_caps & ATH9K_HW_CAP_HT) {
  1501. setup_ht_cap(sc, &sc->sbands[IEEE80211_BAND_2GHZ].ht_cap);
  1502. if (test_bit(ATH9K_MODE_11A, ah->caps.wireless_modes))
  1503. setup_ht_cap(sc, &sc->sbands[IEEE80211_BAND_5GHZ].ht_cap);
  1504. }
  1505. /* initialize tx/rx engine */
  1506. error = ath_tx_init(sc, ATH_TXBUF);
  1507. if (error != 0)
  1508. goto error_attach;
  1509. error = ath_rx_init(sc, ATH_RXBUF);
  1510. if (error != 0)
  1511. goto error_attach;
  1512. INIT_WORK(&sc->chan_work, ath9k_wiphy_chan_work);
  1513. INIT_DELAYED_WORK(&sc->wiphy_work, ath9k_wiphy_work);
  1514. sc->wiphy_scheduler_int = msecs_to_jiffies(500);
  1515. error = ieee80211_register_hw(hw);
  1516. if (!ath_is_world_regd(reg)) {
  1517. error = regulatory_hint(hw->wiphy, reg->alpha2);
  1518. if (error)
  1519. goto error_attach;
  1520. }
  1521. /* Initialize LED control */
  1522. ath_init_leds(sc);
  1523. ath_start_rfkill_poll(sc);
  1524. return 0;
  1525. error_attach:
  1526. /* cleanup tx queues */
  1527. for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++)
  1528. if (ATH_TXQ_SETUP(sc, i))
  1529. ath_tx_cleanupq(sc, &sc->tx.txq[i]);
  1530. ath9k_hw_detach(ah);
  1531. ath9k_exit_debug(ah);
  1532. sc->sc_ah = NULL;
  1533. return error;
  1534. }
  1535. int ath_reset(struct ath_softc *sc, bool retry_tx)
  1536. {
  1537. struct ath_hw *ah = sc->sc_ah;
  1538. struct ieee80211_hw *hw = sc->hw;
  1539. int r;
  1540. ath9k_hw_set_interrupts(ah, 0);
  1541. ath_drain_all_txq(sc, retry_tx);
  1542. ath_stoprecv(sc);
  1543. ath_flushrecv(sc);
  1544. spin_lock_bh(&sc->sc_resetlock);
  1545. r = ath9k_hw_reset(ah, sc->sc_ah->curchan, false);
  1546. if (r)
  1547. DPRINTF(ah, ATH_DBG_FATAL,
  1548. "Unable to reset hardware; reset status %d\n", r);
  1549. spin_unlock_bh(&sc->sc_resetlock);
  1550. if (ath_startrecv(sc) != 0)
  1551. DPRINTF(ah, ATH_DBG_FATAL, "Unable to start recv logic\n");
  1552. /*
  1553. * We may be doing a reset in response to a request
  1554. * that changes the channel so update any state that
  1555. * might change as a result.
  1556. */
  1557. ath_cache_conf_rate(sc, &hw->conf);
  1558. ath_update_txpow(sc);
  1559. if (sc->sc_flags & SC_OP_BEACONS)
  1560. ath_beacon_config(sc, NULL); /* restart beacons */
  1561. ath9k_hw_set_interrupts(ah, sc->imask);
  1562. if (retry_tx) {
  1563. int i;
  1564. for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
  1565. if (ATH_TXQ_SETUP(sc, i)) {
  1566. spin_lock_bh(&sc->tx.txq[i].axq_lock);
  1567. ath_txq_schedule(sc, &sc->tx.txq[i]);
  1568. spin_unlock_bh(&sc->tx.txq[i].axq_lock);
  1569. }
  1570. }
  1571. }
  1572. return r;
  1573. }
  1574. /*
  1575. * This function will allocate both the DMA descriptor structure, and the
  1576. * buffers it contains. These are used to contain the descriptors used
  1577. * by the system.
  1578. */
  1579. int ath_descdma_setup(struct ath_softc *sc, struct ath_descdma *dd,
  1580. struct list_head *head, const char *name,
  1581. int nbuf, int ndesc)
  1582. {
  1583. #define DS2PHYS(_dd, _ds) \
  1584. ((_dd)->dd_desc_paddr + ((caddr_t)(_ds) - (caddr_t)(_dd)->dd_desc))
  1585. #define ATH_DESC_4KB_BOUND_CHECK(_daddr) ((((_daddr) & 0xFFF) > 0xF7F) ? 1 : 0)
  1586. #define ATH_DESC_4KB_BOUND_NUM_SKIPPED(_len) ((_len) / 4096)
  1587. struct ath_desc *ds;
  1588. struct ath_buf *bf;
  1589. int i, bsize, error;
  1590. DPRINTF(sc->sc_ah, ATH_DBG_CONFIG, "%s DMA: %u buffers %u desc/buf\n",
  1591. name, nbuf, ndesc);
  1592. INIT_LIST_HEAD(head);
  1593. /* ath_desc must be a multiple of DWORDs */
  1594. if ((sizeof(struct ath_desc) % 4) != 0) {
  1595. DPRINTF(sc->sc_ah, ATH_DBG_FATAL, "ath_desc not DWORD aligned\n");
  1596. ASSERT((sizeof(struct ath_desc) % 4) == 0);
  1597. error = -ENOMEM;
  1598. goto fail;
  1599. }
  1600. dd->dd_desc_len = sizeof(struct ath_desc) * nbuf * ndesc;
  1601. /*
  1602. * Need additional DMA memory because we can't use
  1603. * descriptors that cross the 4K page boundary. Assume
  1604. * one skipped descriptor per 4K page.
  1605. */
  1606. if (!(sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_4KB_SPLITTRANS)) {
  1607. u32 ndesc_skipped =
  1608. ATH_DESC_4KB_BOUND_NUM_SKIPPED(dd->dd_desc_len);
  1609. u32 dma_len;
  1610. while (ndesc_skipped) {
  1611. dma_len = ndesc_skipped * sizeof(struct ath_desc);
  1612. dd->dd_desc_len += dma_len;
  1613. ndesc_skipped = ATH_DESC_4KB_BOUND_NUM_SKIPPED(dma_len);
  1614. };
  1615. }
  1616. /* allocate descriptors */
  1617. dd->dd_desc = dma_alloc_coherent(sc->dev, dd->dd_desc_len,
  1618. &dd->dd_desc_paddr, GFP_KERNEL);
  1619. if (dd->dd_desc == NULL) {
  1620. error = -ENOMEM;
  1621. goto fail;
  1622. }
  1623. ds = dd->dd_desc;
  1624. DPRINTF(sc->sc_ah, ATH_DBG_CONFIG, "%s DMA map: %p (%u) -> %llx (%u)\n",
  1625. name, ds, (u32) dd->dd_desc_len,
  1626. ito64(dd->dd_desc_paddr), /*XXX*/(u32) dd->dd_desc_len);
  1627. /* allocate buffers */
  1628. bsize = sizeof(struct ath_buf) * nbuf;
  1629. bf = kzalloc(bsize, GFP_KERNEL);
  1630. if (bf == NULL) {
  1631. error = -ENOMEM;
  1632. goto fail2;
  1633. }
  1634. dd->dd_bufptr = bf;
  1635. for (i = 0; i < nbuf; i++, bf++, ds += ndesc) {
  1636. bf->bf_desc = ds;
  1637. bf->bf_daddr = DS2PHYS(dd, ds);
  1638. if (!(sc->sc_ah->caps.hw_caps &
  1639. ATH9K_HW_CAP_4KB_SPLITTRANS)) {
  1640. /*
  1641. * Skip descriptor addresses which can cause 4KB
  1642. * boundary crossing (addr + length) with a 32 dword
  1643. * descriptor fetch.
  1644. */
  1645. while (ATH_DESC_4KB_BOUND_CHECK(bf->bf_daddr)) {
  1646. ASSERT((caddr_t) bf->bf_desc <
  1647. ((caddr_t) dd->dd_desc +
  1648. dd->dd_desc_len));
  1649. ds += ndesc;
  1650. bf->bf_desc = ds;
  1651. bf->bf_daddr = DS2PHYS(dd, ds);
  1652. }
  1653. }
  1654. list_add_tail(&bf->list, head);
  1655. }
  1656. return 0;
  1657. fail2:
  1658. dma_free_coherent(sc->dev, dd->dd_desc_len, dd->dd_desc,
  1659. dd->dd_desc_paddr);
  1660. fail:
  1661. memset(dd, 0, sizeof(*dd));
  1662. return error;
  1663. #undef ATH_DESC_4KB_BOUND_CHECK
  1664. #undef ATH_DESC_4KB_BOUND_NUM_SKIPPED
  1665. #undef DS2PHYS
  1666. }
  1667. void ath_descdma_cleanup(struct ath_softc *sc,
  1668. struct ath_descdma *dd,
  1669. struct list_head *head)
  1670. {
  1671. dma_free_coherent(sc->dev, dd->dd_desc_len, dd->dd_desc,
  1672. dd->dd_desc_paddr);
  1673. INIT_LIST_HEAD(head);
  1674. kfree(dd->dd_bufptr);
  1675. memset(dd, 0, sizeof(*dd));
  1676. }
  1677. int ath_get_hal_qnum(u16 queue, struct ath_softc *sc)
  1678. {
  1679. int qnum;
  1680. switch (queue) {
  1681. case 0:
  1682. qnum = sc->tx.hwq_map[ATH9K_WME_AC_VO];
  1683. break;
  1684. case 1:
  1685. qnum = sc->tx.hwq_map[ATH9K_WME_AC_VI];
  1686. break;
  1687. case 2:
  1688. qnum = sc->tx.hwq_map[ATH9K_WME_AC_BE];
  1689. break;
  1690. case 3:
  1691. qnum = sc->tx.hwq_map[ATH9K_WME_AC_BK];
  1692. break;
  1693. default:
  1694. qnum = sc->tx.hwq_map[ATH9K_WME_AC_BE];
  1695. break;
  1696. }
  1697. return qnum;
  1698. }
  1699. int ath_get_mac80211_qnum(u32 queue, struct ath_softc *sc)
  1700. {
  1701. int qnum;
  1702. switch (queue) {
  1703. case ATH9K_WME_AC_VO:
  1704. qnum = 0;
  1705. break;
  1706. case ATH9K_WME_AC_VI:
  1707. qnum = 1;
  1708. break;
  1709. case ATH9K_WME_AC_BE:
  1710. qnum = 2;
  1711. break;
  1712. case ATH9K_WME_AC_BK:
  1713. qnum = 3;
  1714. break;
  1715. default:
  1716. qnum = -1;
  1717. break;
  1718. }
  1719. return qnum;
  1720. }
  1721. /* XXX: Remove me once we don't depend on ath9k_channel for all
  1722. * this redundant data */
  1723. void ath9k_update_ichannel(struct ath_softc *sc, struct ieee80211_hw *hw,
  1724. struct ath9k_channel *ichan)
  1725. {
  1726. struct ieee80211_channel *chan = hw->conf.channel;
  1727. struct ieee80211_conf *conf = &hw->conf;
  1728. ichan->channel = chan->center_freq;
  1729. ichan->chan = chan;
  1730. if (chan->band == IEEE80211_BAND_2GHZ) {
  1731. ichan->chanmode = CHANNEL_G;
  1732. ichan->channelFlags = CHANNEL_2GHZ | CHANNEL_OFDM | CHANNEL_G;
  1733. } else {
  1734. ichan->chanmode = CHANNEL_A;
  1735. ichan->channelFlags = CHANNEL_5GHZ | CHANNEL_OFDM;
  1736. }
  1737. sc->tx_chan_width = ATH9K_HT_MACMODE_20;
  1738. if (conf_is_ht(conf)) {
  1739. if (conf_is_ht40(conf))
  1740. sc->tx_chan_width = ATH9K_HT_MACMODE_2040;
  1741. ichan->chanmode = ath_get_extchanmode(sc, chan,
  1742. conf->channel_type);
  1743. }
  1744. }
  1745. /**********************/
  1746. /* mac80211 callbacks */
  1747. /**********************/
  1748. /*
  1749. * (Re)start btcoex timers
  1750. */
  1751. static void ath9k_btcoex_timer_resume(struct ath_softc *sc)
  1752. {
  1753. struct ath_btcoex *btcoex = &sc->btcoex;
  1754. struct ath_hw *ah = sc->sc_ah;
  1755. DPRINTF(ah, ATH_DBG_BTCOEX, "Starting btcoex timers");
  1756. /* make sure duty cycle timer is also stopped when resuming */
  1757. if (btcoex->hw_timer_enabled)
  1758. ath_gen_timer_stop(sc->sc_ah, btcoex->no_stomp_timer);
  1759. btcoex->bt_priority_cnt = 0;
  1760. btcoex->bt_priority_time = jiffies;
  1761. sc->sc_flags &= ~SC_OP_BT_PRIORITY_DETECTED;
  1762. mod_timer(&btcoex->period_timer, jiffies);
  1763. }
  1764. static int ath9k_start(struct ieee80211_hw *hw)
  1765. {
  1766. struct ath_wiphy *aphy = hw->priv;
  1767. struct ath_softc *sc = aphy->sc;
  1768. struct ath_hw *ah = sc->sc_ah;
  1769. struct ieee80211_channel *curchan = hw->conf.channel;
  1770. struct ath9k_channel *init_channel;
  1771. int r;
  1772. DPRINTF(ah, ATH_DBG_CONFIG, "Starting driver with "
  1773. "initial channel: %d MHz\n", curchan->center_freq);
  1774. mutex_lock(&sc->mutex);
  1775. if (ath9k_wiphy_started(sc)) {
  1776. if (sc->chan_idx == curchan->hw_value) {
  1777. /*
  1778. * Already on the operational channel, the new wiphy
  1779. * can be marked active.
  1780. */
  1781. aphy->state = ATH_WIPHY_ACTIVE;
  1782. ieee80211_wake_queues(hw);
  1783. } else {
  1784. /*
  1785. * Another wiphy is on another channel, start the new
  1786. * wiphy in paused state.
  1787. */
  1788. aphy->state = ATH_WIPHY_PAUSED;
  1789. ieee80211_stop_queues(hw);
  1790. }
  1791. mutex_unlock(&sc->mutex);
  1792. return 0;
  1793. }
  1794. aphy->state = ATH_WIPHY_ACTIVE;
  1795. /* setup initial channel */
  1796. sc->chan_idx = curchan->hw_value;
  1797. init_channel = ath_get_curchannel(sc, hw);
  1798. /* Reset SERDES registers */
  1799. ath9k_hw_configpcipowersave(ah, 0, 0);
  1800. /*
  1801. * The basic interface to setting the hardware in a good
  1802. * state is ``reset''. On return the hardware is known to
  1803. * be powered up and with interrupts disabled. This must
  1804. * be followed by initialization of the appropriate bits
  1805. * and then setup of the interrupt mask.
  1806. */
  1807. spin_lock_bh(&sc->sc_resetlock);
  1808. r = ath9k_hw_reset(ah, init_channel, false);
  1809. if (r) {
  1810. DPRINTF(ah, ATH_DBG_FATAL,
  1811. "Unable to reset hardware; reset status %d "
  1812. "(freq %u MHz)\n", r,
  1813. curchan->center_freq);
  1814. spin_unlock_bh(&sc->sc_resetlock);
  1815. goto mutex_unlock;
  1816. }
  1817. spin_unlock_bh(&sc->sc_resetlock);
  1818. /*
  1819. * This is needed only to setup initial state
  1820. * but it's best done after a reset.
  1821. */
  1822. ath_update_txpow(sc);
  1823. /*
  1824. * Setup the hardware after reset:
  1825. * The receive engine is set going.
  1826. * Frame transmit is handled entirely
  1827. * in the frame output path; there's nothing to do
  1828. * here except setup the interrupt mask.
  1829. */
  1830. if (ath_startrecv(sc) != 0) {
  1831. DPRINTF(ah, ATH_DBG_FATAL, "Unable to start recv logic\n");
  1832. r = -EIO;
  1833. goto mutex_unlock;
  1834. }
  1835. /* Setup our intr mask. */
  1836. sc->imask = ATH9K_INT_RX | ATH9K_INT_TX
  1837. | ATH9K_INT_RXEOL | ATH9K_INT_RXORN
  1838. | ATH9K_INT_FATAL | ATH9K_INT_GLOBAL;
  1839. if (ah->caps.hw_caps & ATH9K_HW_CAP_GTT)
  1840. sc->imask |= ATH9K_INT_GTT;
  1841. if (ah->caps.hw_caps & ATH9K_HW_CAP_HT)
  1842. sc->imask |= ATH9K_INT_CST;
  1843. ath_cache_conf_rate(sc, &hw->conf);
  1844. sc->sc_flags &= ~SC_OP_INVALID;
  1845. /* Disable BMISS interrupt when we're not associated */
  1846. sc->imask &= ~(ATH9K_INT_SWBA | ATH9K_INT_BMISS);
  1847. ath9k_hw_set_interrupts(ah, sc->imask);
  1848. ieee80211_wake_queues(hw);
  1849. ieee80211_queue_delayed_work(sc->hw, &sc->tx_complete_work, 0);
  1850. if ((ah->btcoex_hw.scheme != ATH_BTCOEX_CFG_NONE) &&
  1851. !ah->btcoex_hw.enabled) {
  1852. ath9k_hw_btcoex_init_weight(ah);
  1853. ath9k_hw_btcoex_enable(ah);
  1854. ath_pcie_aspm_disable(sc);
  1855. if (ah->btcoex_hw.scheme == ATH_BTCOEX_CFG_3WIRE)
  1856. ath9k_btcoex_timer_resume(sc);
  1857. }
  1858. mutex_unlock:
  1859. mutex_unlock(&sc->mutex);
  1860. return r;
  1861. }
  1862. static int ath9k_tx(struct ieee80211_hw *hw,
  1863. struct sk_buff *skb)
  1864. {
  1865. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1866. struct ath_wiphy *aphy = hw->priv;
  1867. struct ath_softc *sc = aphy->sc;
  1868. struct ath_tx_control txctl;
  1869. int hdrlen, padsize;
  1870. if (aphy->state != ATH_WIPHY_ACTIVE && aphy->state != ATH_WIPHY_SCAN) {
  1871. printk(KERN_DEBUG "ath9k: %s: TX in unexpected wiphy state "
  1872. "%d\n", wiphy_name(hw->wiphy), aphy->state);
  1873. goto exit;
  1874. }
  1875. if (sc->ps_enabled) {
  1876. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  1877. /*
  1878. * mac80211 does not set PM field for normal data frames, so we
  1879. * need to update that based on the current PS mode.
  1880. */
  1881. if (ieee80211_is_data(hdr->frame_control) &&
  1882. !ieee80211_is_nullfunc(hdr->frame_control) &&
  1883. !ieee80211_has_pm(hdr->frame_control)) {
  1884. DPRINTF(sc->sc_ah, ATH_DBG_PS, "Add PM=1 for a TX frame "
  1885. "while in PS mode\n");
  1886. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
  1887. }
  1888. }
  1889. if (unlikely(sc->sc_ah->power_mode != ATH9K_PM_AWAKE)) {
  1890. /*
  1891. * We are using PS-Poll and mac80211 can request TX while in
  1892. * power save mode. Need to wake up hardware for the TX to be
  1893. * completed and if needed, also for RX of buffered frames.
  1894. */
  1895. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  1896. ath9k_ps_wakeup(sc);
  1897. ath9k_hw_setrxabort(sc->sc_ah, 0);
  1898. if (ieee80211_is_pspoll(hdr->frame_control)) {
  1899. DPRINTF(sc->sc_ah, ATH_DBG_PS, "Sending PS-Poll to pick a "
  1900. "buffered frame\n");
  1901. sc->sc_flags |= SC_OP_WAIT_FOR_PSPOLL_DATA;
  1902. } else {
  1903. DPRINTF(sc->sc_ah, ATH_DBG_PS, "Wake up to complete TX\n");
  1904. sc->sc_flags |= SC_OP_WAIT_FOR_TX_ACK;
  1905. }
  1906. /*
  1907. * The actual restore operation will happen only after
  1908. * the sc_flags bit is cleared. We are just dropping
  1909. * the ps_usecount here.
  1910. */
  1911. ath9k_ps_restore(sc);
  1912. }
  1913. memset(&txctl, 0, sizeof(struct ath_tx_control));
  1914. /*
  1915. * As a temporary workaround, assign seq# here; this will likely need
  1916. * to be cleaned up to work better with Beacon transmission and virtual
  1917. * BSSes.
  1918. */
  1919. if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
  1920. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  1921. if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
  1922. sc->tx.seq_no += 0x10;
  1923. hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
  1924. hdr->seq_ctrl |= cpu_to_le16(sc->tx.seq_no);
  1925. }
  1926. /* Add the padding after the header if this is not already done */
  1927. hdrlen = ieee80211_get_hdrlen_from_skb(skb);
  1928. if (hdrlen & 3) {
  1929. padsize = hdrlen % 4;
  1930. if (skb_headroom(skb) < padsize)
  1931. return -1;
  1932. skb_push(skb, padsize);
  1933. memmove(skb->data, skb->data + padsize, hdrlen);
  1934. }
  1935. /* Check if a tx queue is available */
  1936. txctl.txq = ath_test_get_txq(sc, skb);
  1937. if (!txctl.txq)
  1938. goto exit;
  1939. DPRINTF(sc->sc_ah, ATH_DBG_XMIT, "transmitting packet, skb: %p\n", skb);
  1940. if (ath_tx_start(hw, skb, &txctl) != 0) {
  1941. DPRINTF(sc->sc_ah, ATH_DBG_XMIT, "TX failed\n");
  1942. goto exit;
  1943. }
  1944. return 0;
  1945. exit:
  1946. dev_kfree_skb_any(skb);
  1947. return 0;
  1948. }
  1949. /*
  1950. * Pause btcoex timer and bt duty cycle timer
  1951. */
  1952. static void ath9k_btcoex_timer_pause(struct ath_softc *sc)
  1953. {
  1954. struct ath_btcoex *btcoex = &sc->btcoex;
  1955. struct ath_hw *ah = sc->sc_ah;
  1956. del_timer_sync(&btcoex->period_timer);
  1957. if (btcoex->hw_timer_enabled)
  1958. ath_gen_timer_stop(ah, btcoex->no_stomp_timer);
  1959. btcoex->hw_timer_enabled = false;
  1960. }
  1961. static void ath9k_stop(struct ieee80211_hw *hw)
  1962. {
  1963. struct ath_wiphy *aphy = hw->priv;
  1964. struct ath_softc *sc = aphy->sc;
  1965. struct ath_hw *ah = sc->sc_ah;
  1966. mutex_lock(&sc->mutex);
  1967. aphy->state = ATH_WIPHY_INACTIVE;
  1968. cancel_delayed_work_sync(&sc->ath_led_blink_work);
  1969. cancel_delayed_work_sync(&sc->tx_complete_work);
  1970. if (!sc->num_sec_wiphy) {
  1971. cancel_delayed_work_sync(&sc->wiphy_work);
  1972. cancel_work_sync(&sc->chan_work);
  1973. }
  1974. if (sc->sc_flags & SC_OP_INVALID) {
  1975. DPRINTF(ah, ATH_DBG_ANY, "Device not present\n");
  1976. mutex_unlock(&sc->mutex);
  1977. return;
  1978. }
  1979. if (ath9k_wiphy_started(sc)) {
  1980. mutex_unlock(&sc->mutex);
  1981. return; /* another wiphy still in use */
  1982. }
  1983. if (ah->btcoex_hw.enabled) {
  1984. ath9k_hw_btcoex_disable(ah);
  1985. if (ah->btcoex_hw.scheme == ATH_BTCOEX_CFG_3WIRE)
  1986. ath9k_btcoex_timer_pause(sc);
  1987. }
  1988. /* make sure h/w will not generate any interrupt
  1989. * before setting the invalid flag. */
  1990. ath9k_hw_set_interrupts(ah, 0);
  1991. if (!(sc->sc_flags & SC_OP_INVALID)) {
  1992. ath_drain_all_txq(sc, false);
  1993. ath_stoprecv(sc);
  1994. ath9k_hw_phy_disable(ah);
  1995. } else
  1996. sc->rx.rxlink = NULL;
  1997. /* disable HAL and put h/w to sleep */
  1998. ath9k_hw_disable(ah);
  1999. ath9k_hw_configpcipowersave(ah, 1, 1);
  2000. ath9k_hw_setpower(ah, ATH9K_PM_FULL_SLEEP);
  2001. sc->sc_flags |= SC_OP_INVALID;
  2002. mutex_unlock(&sc->mutex);
  2003. DPRINTF(ah, ATH_DBG_CONFIG, "Driver halt\n");
  2004. }
  2005. static int ath9k_add_interface(struct ieee80211_hw *hw,
  2006. struct ieee80211_if_init_conf *conf)
  2007. {
  2008. struct ath_wiphy *aphy = hw->priv;
  2009. struct ath_softc *sc = aphy->sc;
  2010. struct ath_vif *avp = (void *)conf->vif->drv_priv;
  2011. enum nl80211_iftype ic_opmode = NL80211_IFTYPE_UNSPECIFIED;
  2012. int ret = 0;
  2013. mutex_lock(&sc->mutex);
  2014. if (!(sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_BSSIDMASK) &&
  2015. sc->nvifs > 0) {
  2016. ret = -ENOBUFS;
  2017. goto out;
  2018. }
  2019. switch (conf->type) {
  2020. case NL80211_IFTYPE_STATION:
  2021. ic_opmode = NL80211_IFTYPE_STATION;
  2022. break;
  2023. case NL80211_IFTYPE_ADHOC:
  2024. case NL80211_IFTYPE_AP:
  2025. case NL80211_IFTYPE_MESH_POINT:
  2026. if (sc->nbcnvifs >= ATH_BCBUF) {
  2027. ret = -ENOBUFS;
  2028. goto out;
  2029. }
  2030. ic_opmode = conf->type;
  2031. break;
  2032. default:
  2033. DPRINTF(sc->sc_ah, ATH_DBG_FATAL,
  2034. "Interface type %d not yet supported\n", conf->type);
  2035. ret = -EOPNOTSUPP;
  2036. goto out;
  2037. }
  2038. DPRINTF(sc->sc_ah, ATH_DBG_CONFIG, "Attach a VIF of type: %d\n", ic_opmode);
  2039. /* Set the VIF opmode */
  2040. avp->av_opmode = ic_opmode;
  2041. avp->av_bslot = -1;
  2042. sc->nvifs++;
  2043. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_BSSIDMASK)
  2044. ath9k_set_bssid_mask(hw);
  2045. if (sc->nvifs > 1)
  2046. goto out; /* skip global settings for secondary vif */
  2047. if (ic_opmode == NL80211_IFTYPE_AP) {
  2048. ath9k_hw_set_tsfadjust(sc->sc_ah, 1);
  2049. sc->sc_flags |= SC_OP_TSF_RESET;
  2050. }
  2051. /* Set the device opmode */
  2052. sc->sc_ah->opmode = ic_opmode;
  2053. /*
  2054. * Enable MIB interrupts when there are hardware phy counters.
  2055. * Note we only do this (at the moment) for station mode.
  2056. */
  2057. if ((conf->type == NL80211_IFTYPE_STATION) ||
  2058. (conf->type == NL80211_IFTYPE_ADHOC) ||
  2059. (conf->type == NL80211_IFTYPE_MESH_POINT)) {
  2060. sc->imask |= ATH9K_INT_MIB;
  2061. sc->imask |= ATH9K_INT_TSFOOR;
  2062. }
  2063. ath9k_hw_set_interrupts(sc->sc_ah, sc->imask);
  2064. if (conf->type == NL80211_IFTYPE_AP ||
  2065. conf->type == NL80211_IFTYPE_ADHOC ||
  2066. conf->type == NL80211_IFTYPE_MONITOR)
  2067. ath_start_ani(sc);
  2068. out:
  2069. mutex_unlock(&sc->mutex);
  2070. return ret;
  2071. }
  2072. static void ath9k_remove_interface(struct ieee80211_hw *hw,
  2073. struct ieee80211_if_init_conf *conf)
  2074. {
  2075. struct ath_wiphy *aphy = hw->priv;
  2076. struct ath_softc *sc = aphy->sc;
  2077. struct ath_vif *avp = (void *)conf->vif->drv_priv;
  2078. int i;
  2079. DPRINTF(sc->sc_ah, ATH_DBG_CONFIG, "Detach Interface\n");
  2080. mutex_lock(&sc->mutex);
  2081. /* Stop ANI */
  2082. del_timer_sync(&sc->ani.timer);
  2083. /* Reclaim beacon resources */
  2084. if ((sc->sc_ah->opmode == NL80211_IFTYPE_AP) ||
  2085. (sc->sc_ah->opmode == NL80211_IFTYPE_ADHOC) ||
  2086. (sc->sc_ah->opmode == NL80211_IFTYPE_MESH_POINT)) {
  2087. ath9k_hw_stoptxdma(sc->sc_ah, sc->beacon.beaconq);
  2088. ath_beacon_return(sc, avp);
  2089. }
  2090. sc->sc_flags &= ~SC_OP_BEACONS;
  2091. for (i = 0; i < ARRAY_SIZE(sc->beacon.bslot); i++) {
  2092. if (sc->beacon.bslot[i] == conf->vif) {
  2093. printk(KERN_DEBUG "%s: vif had allocated beacon "
  2094. "slot\n", __func__);
  2095. sc->beacon.bslot[i] = NULL;
  2096. sc->beacon.bslot_aphy[i] = NULL;
  2097. }
  2098. }
  2099. sc->nvifs--;
  2100. mutex_unlock(&sc->mutex);
  2101. }
  2102. static int ath9k_config(struct ieee80211_hw *hw, u32 changed)
  2103. {
  2104. struct ath_wiphy *aphy = hw->priv;
  2105. struct ath_softc *sc = aphy->sc;
  2106. struct ieee80211_conf *conf = &hw->conf;
  2107. struct ath_hw *ah = sc->sc_ah;
  2108. bool all_wiphys_idle = false, disable_radio = false;
  2109. mutex_lock(&sc->mutex);
  2110. /* Leave this as the first check */
  2111. if (changed & IEEE80211_CONF_CHANGE_IDLE) {
  2112. spin_lock_bh(&sc->wiphy_lock);
  2113. all_wiphys_idle = ath9k_all_wiphys_idle(sc);
  2114. spin_unlock_bh(&sc->wiphy_lock);
  2115. if (conf->flags & IEEE80211_CONF_IDLE){
  2116. if (all_wiphys_idle)
  2117. disable_radio = true;
  2118. }
  2119. else if (all_wiphys_idle) {
  2120. ath_radio_enable(sc);
  2121. DPRINTF(sc->sc_ah, ATH_DBG_CONFIG,
  2122. "not-idle: enabling radio\n");
  2123. }
  2124. }
  2125. if (changed & IEEE80211_CONF_CHANGE_PS) {
  2126. if (conf->flags & IEEE80211_CONF_PS) {
  2127. if (!(ah->caps.hw_caps &
  2128. ATH9K_HW_CAP_AUTOSLEEP)) {
  2129. if ((sc->imask & ATH9K_INT_TIM_TIMER) == 0) {
  2130. sc->imask |= ATH9K_INT_TIM_TIMER;
  2131. ath9k_hw_set_interrupts(sc->sc_ah,
  2132. sc->imask);
  2133. }
  2134. ath9k_hw_setrxabort(sc->sc_ah, 1);
  2135. }
  2136. sc->ps_enabled = true;
  2137. } else {
  2138. sc->ps_enabled = false;
  2139. ath9k_hw_setpower(sc->sc_ah, ATH9K_PM_AWAKE);
  2140. if (!(ah->caps.hw_caps &
  2141. ATH9K_HW_CAP_AUTOSLEEP)) {
  2142. ath9k_hw_setrxabort(sc->sc_ah, 0);
  2143. sc->sc_flags &= ~(SC_OP_WAIT_FOR_BEACON |
  2144. SC_OP_WAIT_FOR_CAB |
  2145. SC_OP_WAIT_FOR_PSPOLL_DATA |
  2146. SC_OP_WAIT_FOR_TX_ACK);
  2147. if (sc->imask & ATH9K_INT_TIM_TIMER) {
  2148. sc->imask &= ~ATH9K_INT_TIM_TIMER;
  2149. ath9k_hw_set_interrupts(sc->sc_ah,
  2150. sc->imask);
  2151. }
  2152. }
  2153. }
  2154. }
  2155. if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
  2156. struct ieee80211_channel *curchan = hw->conf.channel;
  2157. int pos = curchan->hw_value;
  2158. aphy->chan_idx = pos;
  2159. aphy->chan_is_ht = conf_is_ht(conf);
  2160. if (aphy->state == ATH_WIPHY_SCAN ||
  2161. aphy->state == ATH_WIPHY_ACTIVE)
  2162. ath9k_wiphy_pause_all_forced(sc, aphy);
  2163. else {
  2164. /*
  2165. * Do not change operational channel based on a paused
  2166. * wiphy changes.
  2167. */
  2168. goto skip_chan_change;
  2169. }
  2170. DPRINTF(sc->sc_ah, ATH_DBG_CONFIG, "Set channel: %d MHz\n",
  2171. curchan->center_freq);
  2172. /* XXX: remove me eventualy */
  2173. ath9k_update_ichannel(sc, hw, &sc->sc_ah->channels[pos]);
  2174. ath_update_chainmask(sc, conf_is_ht(conf));
  2175. if (ath_set_channel(sc, hw, &sc->sc_ah->channels[pos]) < 0) {
  2176. DPRINTF(sc->sc_ah, ATH_DBG_FATAL, "Unable to set channel\n");
  2177. mutex_unlock(&sc->mutex);
  2178. return -EINVAL;
  2179. }
  2180. }
  2181. skip_chan_change:
  2182. if (changed & IEEE80211_CONF_CHANGE_POWER)
  2183. sc->config.txpowlimit = 2 * conf->power_level;
  2184. if (disable_radio) {
  2185. DPRINTF(sc->sc_ah, ATH_DBG_CONFIG, "idle: disabling radio\n");
  2186. ath_radio_disable(sc);
  2187. }
  2188. mutex_unlock(&sc->mutex);
  2189. return 0;
  2190. }
  2191. #define SUPPORTED_FILTERS \
  2192. (FIF_PROMISC_IN_BSS | \
  2193. FIF_ALLMULTI | \
  2194. FIF_CONTROL | \
  2195. FIF_PSPOLL | \
  2196. FIF_OTHER_BSS | \
  2197. FIF_BCN_PRBRESP_PROMISC | \
  2198. FIF_FCSFAIL)
  2199. /* FIXME: sc->sc_full_reset ? */
  2200. static void ath9k_configure_filter(struct ieee80211_hw *hw,
  2201. unsigned int changed_flags,
  2202. unsigned int *total_flags,
  2203. u64 multicast)
  2204. {
  2205. struct ath_wiphy *aphy = hw->priv;
  2206. struct ath_softc *sc = aphy->sc;
  2207. u32 rfilt;
  2208. changed_flags &= SUPPORTED_FILTERS;
  2209. *total_flags &= SUPPORTED_FILTERS;
  2210. sc->rx.rxfilter = *total_flags;
  2211. ath9k_ps_wakeup(sc);
  2212. rfilt = ath_calcrxfilter(sc);
  2213. ath9k_hw_setrxfilter(sc->sc_ah, rfilt);
  2214. ath9k_ps_restore(sc);
  2215. DPRINTF(sc->sc_ah, ATH_DBG_CONFIG, "Set HW RX filter: 0x%x\n", rfilt);
  2216. }
  2217. static void ath9k_sta_notify(struct ieee80211_hw *hw,
  2218. struct ieee80211_vif *vif,
  2219. enum sta_notify_cmd cmd,
  2220. struct ieee80211_sta *sta)
  2221. {
  2222. struct ath_wiphy *aphy = hw->priv;
  2223. struct ath_softc *sc = aphy->sc;
  2224. switch (cmd) {
  2225. case STA_NOTIFY_ADD:
  2226. ath_node_attach(sc, sta);
  2227. break;
  2228. case STA_NOTIFY_REMOVE:
  2229. ath_node_detach(sc, sta);
  2230. break;
  2231. default:
  2232. break;
  2233. }
  2234. }
  2235. static int ath9k_conf_tx(struct ieee80211_hw *hw, u16 queue,
  2236. const struct ieee80211_tx_queue_params *params)
  2237. {
  2238. struct ath_wiphy *aphy = hw->priv;
  2239. struct ath_softc *sc = aphy->sc;
  2240. struct ath9k_tx_queue_info qi;
  2241. int ret = 0, qnum;
  2242. if (queue >= WME_NUM_AC)
  2243. return 0;
  2244. mutex_lock(&sc->mutex);
  2245. memset(&qi, 0, sizeof(struct ath9k_tx_queue_info));
  2246. qi.tqi_aifs = params->aifs;
  2247. qi.tqi_cwmin = params->cw_min;
  2248. qi.tqi_cwmax = params->cw_max;
  2249. qi.tqi_burstTime = params->txop;
  2250. qnum = ath_get_hal_qnum(queue, sc);
  2251. DPRINTF(sc->sc_ah, ATH_DBG_CONFIG,
  2252. "Configure tx [queue/halq] [%d/%d], "
  2253. "aifs: %d, cw_min: %d, cw_max: %d, txop: %d\n",
  2254. queue, qnum, params->aifs, params->cw_min,
  2255. params->cw_max, params->txop);
  2256. ret = ath_txq_update(sc, qnum, &qi);
  2257. if (ret)
  2258. DPRINTF(sc->sc_ah, ATH_DBG_FATAL, "TXQ Update failed\n");
  2259. mutex_unlock(&sc->mutex);
  2260. return ret;
  2261. }
  2262. static int ath9k_set_key(struct ieee80211_hw *hw,
  2263. enum set_key_cmd cmd,
  2264. struct ieee80211_vif *vif,
  2265. struct ieee80211_sta *sta,
  2266. struct ieee80211_key_conf *key)
  2267. {
  2268. struct ath_wiphy *aphy = hw->priv;
  2269. struct ath_softc *sc = aphy->sc;
  2270. int ret = 0;
  2271. if (modparam_nohwcrypt)
  2272. return -ENOSPC;
  2273. mutex_lock(&sc->mutex);
  2274. ath9k_ps_wakeup(sc);
  2275. DPRINTF(sc->sc_ah, ATH_DBG_CONFIG, "Set HW Key\n");
  2276. switch (cmd) {
  2277. case SET_KEY:
  2278. ret = ath_key_config(sc, vif, sta, key);
  2279. if (ret >= 0) {
  2280. key->hw_key_idx = ret;
  2281. /* push IV and Michael MIC generation to stack */
  2282. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  2283. if (key->alg == ALG_TKIP)
  2284. key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
  2285. if (sc->sc_ah->sw_mgmt_crypto && key->alg == ALG_CCMP)
  2286. key->flags |= IEEE80211_KEY_FLAG_SW_MGMT;
  2287. ret = 0;
  2288. }
  2289. break;
  2290. case DISABLE_KEY:
  2291. ath_key_delete(sc, key);
  2292. break;
  2293. default:
  2294. ret = -EINVAL;
  2295. }
  2296. ath9k_ps_restore(sc);
  2297. mutex_unlock(&sc->mutex);
  2298. return ret;
  2299. }
  2300. static void ath9k_bss_info_changed(struct ieee80211_hw *hw,
  2301. struct ieee80211_vif *vif,
  2302. struct ieee80211_bss_conf *bss_conf,
  2303. u32 changed)
  2304. {
  2305. struct ath_wiphy *aphy = hw->priv;
  2306. struct ath_softc *sc = aphy->sc;
  2307. struct ath_hw *ah = sc->sc_ah;
  2308. struct ath_vif *avp = (void *)vif->drv_priv;
  2309. u32 rfilt = 0;
  2310. int error, i;
  2311. mutex_lock(&sc->mutex);
  2312. /*
  2313. * TODO: Need to decide which hw opmode to use for
  2314. * multi-interface cases
  2315. * XXX: This belongs into add_interface!
  2316. */
  2317. if (vif->type == NL80211_IFTYPE_AP &&
  2318. ah->opmode != NL80211_IFTYPE_AP) {
  2319. ah->opmode = NL80211_IFTYPE_STATION;
  2320. ath9k_hw_setopmode(ah);
  2321. memcpy(sc->curbssid, sc->sc_ah->macaddr, ETH_ALEN);
  2322. sc->curaid = 0;
  2323. ath9k_hw_write_associd(sc);
  2324. /* Request full reset to get hw opmode changed properly */
  2325. sc->sc_flags |= SC_OP_FULL_RESET;
  2326. }
  2327. if ((changed & BSS_CHANGED_BSSID) &&
  2328. !is_zero_ether_addr(bss_conf->bssid)) {
  2329. switch (vif->type) {
  2330. case NL80211_IFTYPE_STATION:
  2331. case NL80211_IFTYPE_ADHOC:
  2332. case NL80211_IFTYPE_MESH_POINT:
  2333. /* Set BSSID */
  2334. memcpy(sc->curbssid, bss_conf->bssid, ETH_ALEN);
  2335. memcpy(avp->bssid, bss_conf->bssid, ETH_ALEN);
  2336. sc->curaid = 0;
  2337. ath9k_hw_write_associd(sc);
  2338. /* Set aggregation protection mode parameters */
  2339. sc->config.ath_aggr_prot = 0;
  2340. DPRINTF(sc->sc_ah, ATH_DBG_CONFIG,
  2341. "RX filter 0x%x bssid %pM aid 0x%x\n",
  2342. rfilt, sc->curbssid, sc->curaid);
  2343. /* need to reconfigure the beacon */
  2344. sc->sc_flags &= ~SC_OP_BEACONS ;
  2345. break;
  2346. default:
  2347. break;
  2348. }
  2349. }
  2350. if ((vif->type == NL80211_IFTYPE_ADHOC) ||
  2351. (vif->type == NL80211_IFTYPE_AP) ||
  2352. (vif->type == NL80211_IFTYPE_MESH_POINT)) {
  2353. if ((changed & BSS_CHANGED_BEACON) ||
  2354. (changed & BSS_CHANGED_BEACON_ENABLED &&
  2355. bss_conf->enable_beacon)) {
  2356. /*
  2357. * Allocate and setup the beacon frame.
  2358. *
  2359. * Stop any previous beacon DMA. This may be
  2360. * necessary, for example, when an ibss merge
  2361. * causes reconfiguration; we may be called
  2362. * with beacon transmission active.
  2363. */
  2364. ath9k_hw_stoptxdma(sc->sc_ah, sc->beacon.beaconq);
  2365. error = ath_beacon_alloc(aphy, vif);
  2366. if (!error)
  2367. ath_beacon_config(sc, vif);
  2368. }
  2369. }
  2370. /* Check for WLAN_CAPABILITY_PRIVACY ? */
  2371. if ((avp->av_opmode != NL80211_IFTYPE_STATION)) {
  2372. for (i = 0; i < IEEE80211_WEP_NKID; i++)
  2373. if (ath9k_hw_keyisvalid(sc->sc_ah, (u16)i))
  2374. ath9k_hw_keysetmac(sc->sc_ah,
  2375. (u16)i,
  2376. sc->curbssid);
  2377. }
  2378. /* Only legacy IBSS for now */
  2379. if (vif->type == NL80211_IFTYPE_ADHOC)
  2380. ath_update_chainmask(sc, 0);
  2381. if (changed & BSS_CHANGED_ERP_PREAMBLE) {
  2382. DPRINTF(sc->sc_ah, ATH_DBG_CONFIG, "BSS Changed PREAMBLE %d\n",
  2383. bss_conf->use_short_preamble);
  2384. if (bss_conf->use_short_preamble)
  2385. sc->sc_flags |= SC_OP_PREAMBLE_SHORT;
  2386. else
  2387. sc->sc_flags &= ~SC_OP_PREAMBLE_SHORT;
  2388. }
  2389. if (changed & BSS_CHANGED_ERP_CTS_PROT) {
  2390. DPRINTF(sc->sc_ah, ATH_DBG_CONFIG, "BSS Changed CTS PROT %d\n",
  2391. bss_conf->use_cts_prot);
  2392. if (bss_conf->use_cts_prot &&
  2393. hw->conf.channel->band != IEEE80211_BAND_5GHZ)
  2394. sc->sc_flags |= SC_OP_PROTECT_ENABLE;
  2395. else
  2396. sc->sc_flags &= ~SC_OP_PROTECT_ENABLE;
  2397. }
  2398. if (changed & BSS_CHANGED_ASSOC) {
  2399. DPRINTF(sc->sc_ah, ATH_DBG_CONFIG, "BSS Changed ASSOC %d\n",
  2400. bss_conf->assoc);
  2401. ath9k_bss_assoc_info(sc, vif, bss_conf);
  2402. }
  2403. /*
  2404. * The HW TSF has to be reset when the beacon interval changes.
  2405. * We set the flag here, and ath_beacon_config_ap() would take this
  2406. * into account when it gets called through the subsequent
  2407. * config_interface() call - with IFCC_BEACON in the changed field.
  2408. */
  2409. if (changed & BSS_CHANGED_BEACON_INT) {
  2410. sc->sc_flags |= SC_OP_TSF_RESET;
  2411. sc->beacon_interval = bss_conf->beacon_int;
  2412. }
  2413. mutex_unlock(&sc->mutex);
  2414. }
  2415. static u64 ath9k_get_tsf(struct ieee80211_hw *hw)
  2416. {
  2417. u64 tsf;
  2418. struct ath_wiphy *aphy = hw->priv;
  2419. struct ath_softc *sc = aphy->sc;
  2420. mutex_lock(&sc->mutex);
  2421. tsf = ath9k_hw_gettsf64(sc->sc_ah);
  2422. mutex_unlock(&sc->mutex);
  2423. return tsf;
  2424. }
  2425. static void ath9k_set_tsf(struct ieee80211_hw *hw, u64 tsf)
  2426. {
  2427. struct ath_wiphy *aphy = hw->priv;
  2428. struct ath_softc *sc = aphy->sc;
  2429. mutex_lock(&sc->mutex);
  2430. ath9k_hw_settsf64(sc->sc_ah, tsf);
  2431. mutex_unlock(&sc->mutex);
  2432. }
  2433. static void ath9k_reset_tsf(struct ieee80211_hw *hw)
  2434. {
  2435. struct ath_wiphy *aphy = hw->priv;
  2436. struct ath_softc *sc = aphy->sc;
  2437. mutex_lock(&sc->mutex);
  2438. ath9k_hw_reset_tsf(sc->sc_ah);
  2439. mutex_unlock(&sc->mutex);
  2440. }
  2441. static int ath9k_ampdu_action(struct ieee80211_hw *hw,
  2442. enum ieee80211_ampdu_mlme_action action,
  2443. struct ieee80211_sta *sta,
  2444. u16 tid, u16 *ssn)
  2445. {
  2446. struct ath_wiphy *aphy = hw->priv;
  2447. struct ath_softc *sc = aphy->sc;
  2448. int ret = 0;
  2449. switch (action) {
  2450. case IEEE80211_AMPDU_RX_START:
  2451. if (!(sc->sc_flags & SC_OP_RXAGGR))
  2452. ret = -ENOTSUPP;
  2453. break;
  2454. case IEEE80211_AMPDU_RX_STOP:
  2455. break;
  2456. case IEEE80211_AMPDU_TX_START:
  2457. ath_tx_aggr_start(sc, sta, tid, ssn);
  2458. ieee80211_start_tx_ba_cb_irqsafe(hw, sta->addr, tid);
  2459. break;
  2460. case IEEE80211_AMPDU_TX_STOP:
  2461. ath_tx_aggr_stop(sc, sta, tid);
  2462. ieee80211_stop_tx_ba_cb_irqsafe(hw, sta->addr, tid);
  2463. break;
  2464. case IEEE80211_AMPDU_TX_OPERATIONAL:
  2465. ath_tx_aggr_resume(sc, sta, tid);
  2466. break;
  2467. default:
  2468. DPRINTF(sc->sc_ah, ATH_DBG_FATAL, "Unknown AMPDU action\n");
  2469. }
  2470. return ret;
  2471. }
  2472. static void ath9k_sw_scan_start(struct ieee80211_hw *hw)
  2473. {
  2474. struct ath_wiphy *aphy = hw->priv;
  2475. struct ath_softc *sc = aphy->sc;
  2476. mutex_lock(&sc->mutex);
  2477. if (ath9k_wiphy_scanning(sc)) {
  2478. printk(KERN_DEBUG "ath9k: Two wiphys trying to scan at the "
  2479. "same time\n");
  2480. /*
  2481. * Do not allow the concurrent scanning state for now. This
  2482. * could be improved with scanning control moved into ath9k.
  2483. */
  2484. mutex_unlock(&sc->mutex);
  2485. return;
  2486. }
  2487. aphy->state = ATH_WIPHY_SCAN;
  2488. ath9k_wiphy_pause_all_forced(sc, aphy);
  2489. spin_lock_bh(&sc->ani_lock);
  2490. sc->sc_flags |= SC_OP_SCANNING;
  2491. spin_unlock_bh(&sc->ani_lock);
  2492. mutex_unlock(&sc->mutex);
  2493. }
  2494. static void ath9k_sw_scan_complete(struct ieee80211_hw *hw)
  2495. {
  2496. struct ath_wiphy *aphy = hw->priv;
  2497. struct ath_softc *sc = aphy->sc;
  2498. mutex_lock(&sc->mutex);
  2499. spin_lock_bh(&sc->ani_lock);
  2500. aphy->state = ATH_WIPHY_ACTIVE;
  2501. sc->sc_flags &= ~SC_OP_SCANNING;
  2502. sc->sc_flags |= SC_OP_FULL_RESET;
  2503. spin_unlock_bh(&sc->ani_lock);
  2504. ath_beacon_config(sc, NULL);
  2505. mutex_unlock(&sc->mutex);
  2506. }
  2507. struct ieee80211_ops ath9k_ops = {
  2508. .tx = ath9k_tx,
  2509. .start = ath9k_start,
  2510. .stop = ath9k_stop,
  2511. .add_interface = ath9k_add_interface,
  2512. .remove_interface = ath9k_remove_interface,
  2513. .config = ath9k_config,
  2514. .configure_filter = ath9k_configure_filter,
  2515. .sta_notify = ath9k_sta_notify,
  2516. .conf_tx = ath9k_conf_tx,
  2517. .bss_info_changed = ath9k_bss_info_changed,
  2518. .set_key = ath9k_set_key,
  2519. .get_tsf = ath9k_get_tsf,
  2520. .set_tsf = ath9k_set_tsf,
  2521. .reset_tsf = ath9k_reset_tsf,
  2522. .ampdu_action = ath9k_ampdu_action,
  2523. .sw_scan_start = ath9k_sw_scan_start,
  2524. .sw_scan_complete = ath9k_sw_scan_complete,
  2525. .rfkill_poll = ath9k_rfkill_poll_state,
  2526. };
  2527. static struct {
  2528. u32 version;
  2529. const char * name;
  2530. } ath_mac_bb_names[] = {
  2531. { AR_SREV_VERSION_5416_PCI, "5416" },
  2532. { AR_SREV_VERSION_5416_PCIE, "5418" },
  2533. { AR_SREV_VERSION_9100, "9100" },
  2534. { AR_SREV_VERSION_9160, "9160" },
  2535. { AR_SREV_VERSION_9280, "9280" },
  2536. { AR_SREV_VERSION_9285, "9285" },
  2537. { AR_SREV_VERSION_9287, "9287" }
  2538. };
  2539. static struct {
  2540. u16 version;
  2541. const char * name;
  2542. } ath_rf_names[] = {
  2543. { 0, "5133" },
  2544. { AR_RAD5133_SREV_MAJOR, "5133" },
  2545. { AR_RAD5122_SREV_MAJOR, "5122" },
  2546. { AR_RAD2133_SREV_MAJOR, "2133" },
  2547. { AR_RAD2122_SREV_MAJOR, "2122" }
  2548. };
  2549. /*
  2550. * Return the MAC/BB name. "????" is returned if the MAC/BB is unknown.
  2551. */
  2552. const char *
  2553. ath_mac_bb_name(u32 mac_bb_version)
  2554. {
  2555. int i;
  2556. for (i=0; i<ARRAY_SIZE(ath_mac_bb_names); i++) {
  2557. if (ath_mac_bb_names[i].version == mac_bb_version) {
  2558. return ath_mac_bb_names[i].name;
  2559. }
  2560. }
  2561. return "????";
  2562. }
  2563. /*
  2564. * Return the RF name. "????" is returned if the RF is unknown.
  2565. */
  2566. const char *
  2567. ath_rf_name(u16 rf_version)
  2568. {
  2569. int i;
  2570. for (i=0; i<ARRAY_SIZE(ath_rf_names); i++) {
  2571. if (ath_rf_names[i].version == rf_version) {
  2572. return ath_rf_names[i].name;
  2573. }
  2574. }
  2575. return "????";
  2576. }
  2577. static int __init ath9k_init(void)
  2578. {
  2579. int error;
  2580. /* Register rate control algorithm */
  2581. error = ath_rate_control_register();
  2582. if (error != 0) {
  2583. printk(KERN_ERR
  2584. "ath9k: Unable to register rate control "
  2585. "algorithm: %d\n",
  2586. error);
  2587. goto err_out;
  2588. }
  2589. error = ath9k_debug_create_root();
  2590. if (error) {
  2591. printk(KERN_ERR
  2592. "ath9k: Unable to create debugfs root: %d\n",
  2593. error);
  2594. goto err_rate_unregister;
  2595. }
  2596. error = ath_pci_init();
  2597. if (error < 0) {
  2598. printk(KERN_ERR
  2599. "ath9k: No PCI devices found, driver not installed.\n");
  2600. error = -ENODEV;
  2601. goto err_remove_root;
  2602. }
  2603. error = ath_ahb_init();
  2604. if (error < 0) {
  2605. error = -ENODEV;
  2606. goto err_pci_exit;
  2607. }
  2608. return 0;
  2609. err_pci_exit:
  2610. ath_pci_exit();
  2611. err_remove_root:
  2612. ath9k_debug_remove_root();
  2613. err_rate_unregister:
  2614. ath_rate_control_unregister();
  2615. err_out:
  2616. return error;
  2617. }
  2618. module_init(ath9k_init);
  2619. static void __exit ath9k_exit(void)
  2620. {
  2621. ath_ahb_exit();
  2622. ath_pci_exit();
  2623. ath9k_debug_remove_root();
  2624. ath_rate_control_unregister();
  2625. printk(KERN_INFO "%s: Driver unloaded\n", dev_info);
  2626. }
  2627. module_exit(ath9k_exit);