main.c 84 KB

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