main.c 83 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279
  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. ath_detach(sc);
  1121. free_irq(sc->irq, sc);
  1122. ath_bus_cleanup(sc);
  1123. kfree(sc->sec_wiphy);
  1124. ieee80211_free_hw(sc->hw);
  1125. }
  1126. void ath_detach(struct ath_softc *sc)
  1127. {
  1128. struct ieee80211_hw *hw = sc->hw;
  1129. struct ath_hw *ah = sc->sc_ah;
  1130. int i = 0;
  1131. ath9k_ps_wakeup(sc);
  1132. dev_dbg(sc->dev, "Detach ATH hw\n");
  1133. ath_deinit_leds(sc);
  1134. wiphy_rfkill_stop_polling(sc->hw->wiphy);
  1135. for (i = 0; i < sc->num_sec_wiphy; i++) {
  1136. struct ath_wiphy *aphy = sc->sec_wiphy[i];
  1137. if (aphy == NULL)
  1138. continue;
  1139. sc->sec_wiphy[i] = NULL;
  1140. ieee80211_unregister_hw(aphy->hw);
  1141. ieee80211_free_hw(aphy->hw);
  1142. }
  1143. ieee80211_unregister_hw(hw);
  1144. ath_rx_cleanup(sc);
  1145. ath_tx_cleanup(sc);
  1146. tasklet_kill(&sc->intr_tq);
  1147. tasklet_kill(&sc->bcon_tasklet);
  1148. if (!(sc->sc_flags & SC_OP_INVALID))
  1149. ath9k_setpower(sc, ATH9K_PM_AWAKE);
  1150. /* cleanup tx queues */
  1151. for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++)
  1152. if (ATH_TXQ_SETUP(sc, i))
  1153. ath_tx_cleanupq(sc, &sc->tx.txq[i]);
  1154. if ((sc->btcoex.no_stomp_timer) &&
  1155. ah->btcoex_hw.scheme == ATH_BTCOEX_CFG_3WIRE)
  1156. ath_gen_timer_free(ah, sc->btcoex.no_stomp_timer);
  1157. ath9k_hw_detach(ah);
  1158. ath9k_exit_debug(ah);
  1159. sc->sc_ah = NULL;
  1160. }
  1161. static int ath9k_reg_notifier(struct wiphy *wiphy,
  1162. struct regulatory_request *request)
  1163. {
  1164. struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
  1165. struct ath_wiphy *aphy = hw->priv;
  1166. struct ath_softc *sc = aphy->sc;
  1167. struct ath_regulatory *reg = ath9k_hw_regulatory(sc->sc_ah);
  1168. return ath_reg_notifier_apply(wiphy, request, reg);
  1169. }
  1170. /*
  1171. * Detects if there is any priority bt traffic
  1172. */
  1173. static void ath_detect_bt_priority(struct ath_softc *sc)
  1174. {
  1175. struct ath_btcoex *btcoex = &sc->btcoex;
  1176. struct ath_hw *ah = sc->sc_ah;
  1177. if (ath9k_hw_gpio_get(sc->sc_ah, ah->btcoex_hw.btpriority_gpio))
  1178. btcoex->bt_priority_cnt++;
  1179. if (time_after(jiffies, btcoex->bt_priority_time +
  1180. msecs_to_jiffies(ATH_BT_PRIORITY_TIME_THRESHOLD))) {
  1181. if (btcoex->bt_priority_cnt >= ATH_BT_CNT_THRESHOLD) {
  1182. ath_print(ath9k_hw_common(sc->sc_ah), ATH_DBG_BTCOEX,
  1183. "BT priority traffic detected");
  1184. sc->sc_flags |= SC_OP_BT_PRIORITY_DETECTED;
  1185. } else {
  1186. sc->sc_flags &= ~SC_OP_BT_PRIORITY_DETECTED;
  1187. }
  1188. btcoex->bt_priority_cnt = 0;
  1189. btcoex->bt_priority_time = jiffies;
  1190. }
  1191. }
  1192. /*
  1193. * Configures appropriate weight based on stomp type.
  1194. */
  1195. static void ath9k_btcoex_bt_stomp(struct ath_softc *sc,
  1196. enum ath_stomp_type stomp_type)
  1197. {
  1198. struct ath_hw *ah = sc->sc_ah;
  1199. switch (stomp_type) {
  1200. case ATH_BTCOEX_STOMP_ALL:
  1201. ath9k_hw_btcoex_set_weight(ah, AR_BT_COEX_WGHT,
  1202. AR_STOMP_ALL_WLAN_WGHT);
  1203. break;
  1204. case ATH_BTCOEX_STOMP_LOW:
  1205. ath9k_hw_btcoex_set_weight(ah, AR_BT_COEX_WGHT,
  1206. AR_STOMP_LOW_WLAN_WGHT);
  1207. break;
  1208. case ATH_BTCOEX_STOMP_NONE:
  1209. ath9k_hw_btcoex_set_weight(ah, AR_BT_COEX_WGHT,
  1210. AR_STOMP_NONE_WLAN_WGHT);
  1211. break;
  1212. default:
  1213. ath_print(ath9k_hw_common(ah), ATH_DBG_BTCOEX,
  1214. "Invalid Stomptype\n");
  1215. break;
  1216. }
  1217. ath9k_hw_btcoex_enable(ah);
  1218. }
  1219. static void ath9k_gen_timer_start(struct ath_hw *ah,
  1220. struct ath_gen_timer *timer,
  1221. u32 timer_next,
  1222. u32 timer_period)
  1223. {
  1224. ath9k_hw_gen_timer_start(ah, timer, timer_next, timer_period);
  1225. if ((ah->ah_sc->imask & ATH9K_INT_GENTIMER) == 0) {
  1226. ath9k_hw_set_interrupts(ah, 0);
  1227. ah->ah_sc->imask |= ATH9K_INT_GENTIMER;
  1228. ath9k_hw_set_interrupts(ah, ah->ah_sc->imask);
  1229. }
  1230. }
  1231. static void ath9k_gen_timer_stop(struct ath_hw *ah, struct ath_gen_timer *timer)
  1232. {
  1233. struct ath_gen_timer_table *timer_table = &ah->hw_gen_timers;
  1234. ath9k_hw_gen_timer_stop(ah, timer);
  1235. /* if no timer is enabled, turn off interrupt mask */
  1236. if (timer_table->timer_mask.val == 0) {
  1237. ath9k_hw_set_interrupts(ah, 0);
  1238. ah->ah_sc->imask &= ~ATH9K_INT_GENTIMER;
  1239. ath9k_hw_set_interrupts(ah, ah->ah_sc->imask);
  1240. }
  1241. }
  1242. /*
  1243. * This is the master bt coex timer which runs for every
  1244. * 45ms, bt traffic will be given priority during 55% of this
  1245. * period while wlan gets remaining 45%
  1246. */
  1247. static void ath_btcoex_period_timer(unsigned long data)
  1248. {
  1249. struct ath_softc *sc = (struct ath_softc *) data;
  1250. struct ath_hw *ah = sc->sc_ah;
  1251. struct ath_btcoex *btcoex = &sc->btcoex;
  1252. ath_detect_bt_priority(sc);
  1253. spin_lock_bh(&btcoex->btcoex_lock);
  1254. ath9k_btcoex_bt_stomp(sc, btcoex->bt_stomp_type);
  1255. spin_unlock_bh(&btcoex->btcoex_lock);
  1256. if (btcoex->btcoex_period != btcoex->btcoex_no_stomp) {
  1257. if (btcoex->hw_timer_enabled)
  1258. ath9k_gen_timer_stop(ah, btcoex->no_stomp_timer);
  1259. ath9k_gen_timer_start(ah,
  1260. btcoex->no_stomp_timer,
  1261. (ath9k_hw_gettsf32(ah) +
  1262. btcoex->btcoex_no_stomp),
  1263. btcoex->btcoex_no_stomp * 10);
  1264. btcoex->hw_timer_enabled = true;
  1265. }
  1266. mod_timer(&btcoex->period_timer, jiffies +
  1267. msecs_to_jiffies(ATH_BTCOEX_DEF_BT_PERIOD));
  1268. }
  1269. /*
  1270. * Generic tsf based hw timer which configures weight
  1271. * registers to time slice between wlan and bt traffic
  1272. */
  1273. static void ath_btcoex_no_stomp_timer(void *arg)
  1274. {
  1275. struct ath_softc *sc = (struct ath_softc *)arg;
  1276. struct ath_hw *ah = sc->sc_ah;
  1277. struct ath_btcoex *btcoex = &sc->btcoex;
  1278. ath_print(ath9k_hw_common(ah), ATH_DBG_BTCOEX,
  1279. "no stomp timer running \n");
  1280. spin_lock_bh(&btcoex->btcoex_lock);
  1281. if (btcoex->bt_stomp_type == ATH_BTCOEX_STOMP_LOW)
  1282. ath9k_btcoex_bt_stomp(sc, ATH_BTCOEX_STOMP_NONE);
  1283. else if (btcoex->bt_stomp_type == ATH_BTCOEX_STOMP_ALL)
  1284. ath9k_btcoex_bt_stomp(sc, ATH_BTCOEX_STOMP_LOW);
  1285. spin_unlock_bh(&btcoex->btcoex_lock);
  1286. }
  1287. static int ath_init_btcoex_timer(struct ath_softc *sc)
  1288. {
  1289. struct ath_btcoex *btcoex = &sc->btcoex;
  1290. btcoex->btcoex_period = ATH_BTCOEX_DEF_BT_PERIOD * 1000;
  1291. btcoex->btcoex_no_stomp = (100 - ATH_BTCOEX_DEF_DUTY_CYCLE) *
  1292. btcoex->btcoex_period / 100;
  1293. setup_timer(&btcoex->period_timer, ath_btcoex_period_timer,
  1294. (unsigned long) sc);
  1295. spin_lock_init(&btcoex->btcoex_lock);
  1296. btcoex->no_stomp_timer = ath_gen_timer_alloc(sc->sc_ah,
  1297. ath_btcoex_no_stomp_timer,
  1298. ath_btcoex_no_stomp_timer,
  1299. (void *) sc, AR_FIRST_NDP_TIMER);
  1300. if (!btcoex->no_stomp_timer)
  1301. return -ENOMEM;
  1302. return 0;
  1303. }
  1304. /*
  1305. * Read and write, they both share the same lock. We do this to serialize
  1306. * reads and writes on Atheros 802.11n PCI devices only. This is required
  1307. * as the FIFO on these devices can only accept sanely 2 requests. After
  1308. * that the device goes bananas. Serializing the reads/writes prevents this
  1309. * from happening.
  1310. */
  1311. static void ath9k_iowrite32(void *hw_priv, u32 val, u32 reg_offset)
  1312. {
  1313. struct ath_hw *ah = (struct ath_hw *) hw_priv;
  1314. if (ah->config.serialize_regmode == SER_REG_MODE_ON) {
  1315. unsigned long flags;
  1316. spin_lock_irqsave(&ah->ah_sc->sc_serial_rw, flags);
  1317. iowrite32(val, ah->ah_sc->mem + reg_offset);
  1318. spin_unlock_irqrestore(&ah->ah_sc->sc_serial_rw, flags);
  1319. } else
  1320. iowrite32(val, ah->ah_sc->mem + reg_offset);
  1321. }
  1322. static unsigned int ath9k_ioread32(void *hw_priv, u32 reg_offset)
  1323. {
  1324. struct ath_hw *ah = (struct ath_hw *) hw_priv;
  1325. u32 val;
  1326. if (ah->config.serialize_regmode == SER_REG_MODE_ON) {
  1327. unsigned long flags;
  1328. spin_lock_irqsave(&ah->ah_sc->sc_serial_rw, flags);
  1329. val = ioread32(ah->ah_sc->mem + reg_offset);
  1330. spin_unlock_irqrestore(&ah->ah_sc->sc_serial_rw, flags);
  1331. } else
  1332. val = ioread32(ah->ah_sc->mem + reg_offset);
  1333. return val;
  1334. }
  1335. static struct ath_ops ath9k_common_ops = {
  1336. .read = ath9k_ioread32,
  1337. .write = ath9k_iowrite32,
  1338. };
  1339. /*
  1340. * Initialize and fill ath_softc, ath_sofct is the
  1341. * "Software Carrier" struct. Historically it has existed
  1342. * to allow the separation between hardware specific
  1343. * variables (now in ath_hw) and driver specific variables.
  1344. */
  1345. static int ath_init_softc(u16 devid, struct ath_softc *sc, u16 subsysid)
  1346. {
  1347. struct ath_hw *ah = NULL;
  1348. struct ath_common *common;
  1349. int r = 0, i;
  1350. int csz = 0;
  1351. int qnum;
  1352. /* XXX: hardware will not be ready until ath_open() being called */
  1353. sc->sc_flags |= SC_OP_INVALID;
  1354. spin_lock_init(&sc->wiphy_lock);
  1355. spin_lock_init(&sc->sc_resetlock);
  1356. spin_lock_init(&sc->sc_serial_rw);
  1357. spin_lock_init(&sc->ani_lock);
  1358. spin_lock_init(&sc->sc_pm_lock);
  1359. mutex_init(&sc->mutex);
  1360. tasklet_init(&sc->intr_tq, ath9k_tasklet, (unsigned long)sc);
  1361. tasklet_init(&sc->bcon_tasklet, ath_beacon_tasklet,
  1362. (unsigned long)sc);
  1363. ah = kzalloc(sizeof(struct ath_hw), GFP_KERNEL);
  1364. if (!ah) {
  1365. r = -ENOMEM;
  1366. goto bad_no_ah;
  1367. }
  1368. ah->ah_sc = sc;
  1369. ah->hw_version.devid = devid;
  1370. ah->hw_version.subsysid = subsysid;
  1371. sc->sc_ah = ah;
  1372. common = ath9k_hw_common(ah);
  1373. common->ops = &ath9k_common_ops;
  1374. common->ah = ah;
  1375. common->hw = sc->hw;
  1376. /*
  1377. * Cache line size is used to size and align various
  1378. * structures used to communicate with the hardware.
  1379. */
  1380. ath_read_cachesize(sc, &csz);
  1381. /* XXX assert csz is non-zero */
  1382. common->cachelsz = csz << 2; /* convert to bytes */
  1383. if (ath9k_init_debug(ah) < 0)
  1384. dev_err(sc->dev, "Unable to create debugfs files\n");
  1385. r = ath9k_hw_init(ah);
  1386. if (r) {
  1387. ath_print(common, ATH_DBG_FATAL,
  1388. "Unable to initialize hardware; "
  1389. "initialization status: %d\n", r);
  1390. goto bad;
  1391. }
  1392. /* Get the hardware key cache size. */
  1393. sc->keymax = ah->caps.keycache_size;
  1394. if (sc->keymax > ATH_KEYMAX) {
  1395. ath_print(common, ATH_DBG_ANY,
  1396. "Warning, using only %u entries in %u key cache\n",
  1397. ATH_KEYMAX, sc->keymax);
  1398. sc->keymax = ATH_KEYMAX;
  1399. }
  1400. /*
  1401. * Reset the key cache since some parts do not
  1402. * reset the contents on initial power up.
  1403. */
  1404. for (i = 0; i < sc->keymax; i++)
  1405. ath9k_hw_keyreset(ah, (u16) i);
  1406. /* default to MONITOR mode */
  1407. sc->sc_ah->opmode = NL80211_IFTYPE_MONITOR;
  1408. /* Setup rate tables */
  1409. ath_rate_attach(sc);
  1410. ath_setup_rates(sc, IEEE80211_BAND_2GHZ);
  1411. ath_setup_rates(sc, IEEE80211_BAND_5GHZ);
  1412. /*
  1413. * Allocate hardware transmit queues: one queue for
  1414. * beacon frames and one data queue for each QoS
  1415. * priority. Note that the hal handles reseting
  1416. * these queues at the needed time.
  1417. */
  1418. sc->beacon.beaconq = ath_beaconq_setup(ah);
  1419. if (sc->beacon.beaconq == -1) {
  1420. ath_print(common, ATH_DBG_FATAL,
  1421. "Unable to setup a beacon xmit queue\n");
  1422. r = -EIO;
  1423. goto bad2;
  1424. }
  1425. sc->beacon.cabq = ath_txq_setup(sc, ATH9K_TX_QUEUE_CAB, 0);
  1426. if (sc->beacon.cabq == NULL) {
  1427. ath_print(common, ATH_DBG_FATAL,
  1428. "Unable to setup CAB xmit queue\n");
  1429. r = -EIO;
  1430. goto bad2;
  1431. }
  1432. sc->config.cabqReadytime = ATH_CABQ_READY_TIME;
  1433. ath_cabq_update(sc);
  1434. for (i = 0; i < ARRAY_SIZE(sc->tx.hwq_map); i++)
  1435. sc->tx.hwq_map[i] = -1;
  1436. /* Setup data queues */
  1437. /* NB: ensure BK queue is the lowest priority h/w queue */
  1438. if (!ath_tx_setup(sc, ATH9K_WME_AC_BK)) {
  1439. ath_print(common, ATH_DBG_FATAL,
  1440. "Unable to setup xmit queue for BK traffic\n");
  1441. r = -EIO;
  1442. goto bad2;
  1443. }
  1444. if (!ath_tx_setup(sc, ATH9K_WME_AC_BE)) {
  1445. ath_print(common, ATH_DBG_FATAL,
  1446. "Unable to setup xmit queue for BE traffic\n");
  1447. r = -EIO;
  1448. goto bad2;
  1449. }
  1450. if (!ath_tx_setup(sc, ATH9K_WME_AC_VI)) {
  1451. ath_print(common, ATH_DBG_FATAL,
  1452. "Unable to setup xmit queue for VI traffic\n");
  1453. r = -EIO;
  1454. goto bad2;
  1455. }
  1456. if (!ath_tx_setup(sc, ATH9K_WME_AC_VO)) {
  1457. ath_print(common, ATH_DBG_FATAL,
  1458. "Unable to setup xmit queue for VO traffic\n");
  1459. r = -EIO;
  1460. goto bad2;
  1461. }
  1462. /* Initializes the noise floor to a reasonable default value.
  1463. * Later on this will be updated during ANI processing. */
  1464. sc->ani.noise_floor = ATH_DEFAULT_NOISE_FLOOR;
  1465. setup_timer(&sc->ani.timer, ath_ani_calibrate, (unsigned long)sc);
  1466. if (ath9k_hw_getcapability(ah, ATH9K_CAP_CIPHER,
  1467. ATH9K_CIPHER_TKIP, NULL)) {
  1468. /*
  1469. * Whether we should enable h/w TKIP MIC.
  1470. * XXX: if we don't support WME TKIP MIC, then we wouldn't
  1471. * report WMM capable, so it's always safe to turn on
  1472. * TKIP MIC in this case.
  1473. */
  1474. ath9k_hw_setcapability(sc->sc_ah, ATH9K_CAP_TKIP_MIC,
  1475. 0, 1, NULL);
  1476. }
  1477. /*
  1478. * Check whether the separate key cache entries
  1479. * are required to handle both tx+rx MIC keys.
  1480. * With split mic keys the number of stations is limited
  1481. * to 27 otherwise 59.
  1482. */
  1483. if (ath9k_hw_getcapability(ah, ATH9K_CAP_CIPHER,
  1484. ATH9K_CIPHER_TKIP, NULL)
  1485. && ath9k_hw_getcapability(ah, ATH9K_CAP_CIPHER,
  1486. ATH9K_CIPHER_MIC, NULL)
  1487. && ath9k_hw_getcapability(ah, ATH9K_CAP_TKIP_SPLIT,
  1488. 0, NULL))
  1489. sc->splitmic = 1;
  1490. /* turn on mcast key search if possible */
  1491. if (!ath9k_hw_getcapability(ah, ATH9K_CAP_MCAST_KEYSRCH, 0, NULL))
  1492. (void)ath9k_hw_setcapability(ah, ATH9K_CAP_MCAST_KEYSRCH, 1,
  1493. 1, NULL);
  1494. sc->config.txpowlimit = ATH_TXPOWER_MAX;
  1495. /* 11n Capabilities */
  1496. if (ah->caps.hw_caps & ATH9K_HW_CAP_HT) {
  1497. sc->sc_flags |= SC_OP_TXAGGR;
  1498. sc->sc_flags |= SC_OP_RXAGGR;
  1499. }
  1500. common->tx_chainmask = ah->caps.tx_chainmask;
  1501. common->rx_chainmask = ah->caps.rx_chainmask;
  1502. ath9k_hw_setcapability(ah, ATH9K_CAP_DIVERSITY, 1, true, NULL);
  1503. sc->rx.defant = ath9k_hw_getdefantenna(ah);
  1504. if (ah->caps.hw_caps & ATH9K_HW_CAP_BSSIDMASK)
  1505. memcpy(common->bssidmask, ath_bcast_mac, ETH_ALEN);
  1506. sc->beacon.slottime = ATH9K_SLOT_TIME_9; /* default to short slot time */
  1507. /* initialize beacon slots */
  1508. for (i = 0; i < ARRAY_SIZE(sc->beacon.bslot); i++) {
  1509. sc->beacon.bslot[i] = NULL;
  1510. sc->beacon.bslot_aphy[i] = NULL;
  1511. }
  1512. /* setup channels and rates */
  1513. sc->sbands[IEEE80211_BAND_2GHZ].channels = ath9k_2ghz_chantable;
  1514. sc->sbands[IEEE80211_BAND_2GHZ].bitrates =
  1515. sc->rates[IEEE80211_BAND_2GHZ];
  1516. sc->sbands[IEEE80211_BAND_2GHZ].band = IEEE80211_BAND_2GHZ;
  1517. sc->sbands[IEEE80211_BAND_2GHZ].n_channels =
  1518. ARRAY_SIZE(ath9k_2ghz_chantable);
  1519. if (test_bit(ATH9K_MODE_11A, sc->sc_ah->caps.wireless_modes)) {
  1520. sc->sbands[IEEE80211_BAND_5GHZ].channels = ath9k_5ghz_chantable;
  1521. sc->sbands[IEEE80211_BAND_5GHZ].bitrates =
  1522. sc->rates[IEEE80211_BAND_5GHZ];
  1523. sc->sbands[IEEE80211_BAND_5GHZ].band = IEEE80211_BAND_5GHZ;
  1524. sc->sbands[IEEE80211_BAND_5GHZ].n_channels =
  1525. ARRAY_SIZE(ath9k_5ghz_chantable);
  1526. }
  1527. switch (ah->btcoex_hw.scheme) {
  1528. case ATH_BTCOEX_CFG_NONE:
  1529. break;
  1530. case ATH_BTCOEX_CFG_2WIRE:
  1531. ath9k_hw_btcoex_init_2wire(ah);
  1532. break;
  1533. case ATH_BTCOEX_CFG_3WIRE:
  1534. ath9k_hw_btcoex_init_3wire(ah);
  1535. r = ath_init_btcoex_timer(sc);
  1536. if (r)
  1537. goto bad2;
  1538. qnum = ath_tx_get_qnum(sc, ATH9K_TX_QUEUE_DATA, ATH9K_WME_AC_BE);
  1539. ath9k_hw_init_btcoex_hw(ah, qnum);
  1540. sc->btcoex.bt_stomp_type = ATH_BTCOEX_STOMP_LOW;
  1541. break;
  1542. default:
  1543. WARN_ON(1);
  1544. break;
  1545. }
  1546. return 0;
  1547. bad2:
  1548. /* cleanup tx queues */
  1549. for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++)
  1550. if (ATH_TXQ_SETUP(sc, i))
  1551. ath_tx_cleanupq(sc, &sc->tx.txq[i]);
  1552. bad:
  1553. ath9k_hw_detach(ah);
  1554. bad_no_ah:
  1555. ath9k_exit_debug(sc->sc_ah);
  1556. sc->sc_ah = NULL;
  1557. return r;
  1558. }
  1559. void ath_set_hw_capab(struct ath_softc *sc, struct ieee80211_hw *hw)
  1560. {
  1561. hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
  1562. IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
  1563. IEEE80211_HW_SIGNAL_DBM |
  1564. IEEE80211_HW_AMPDU_AGGREGATION |
  1565. IEEE80211_HW_SUPPORTS_PS |
  1566. IEEE80211_HW_PS_NULLFUNC_STACK |
  1567. IEEE80211_HW_SPECTRUM_MGMT;
  1568. if (AR_SREV_9160_10_OR_LATER(sc->sc_ah) || modparam_nohwcrypt)
  1569. hw->flags |= IEEE80211_HW_MFP_CAPABLE;
  1570. hw->wiphy->interface_modes =
  1571. BIT(NL80211_IFTYPE_AP) |
  1572. BIT(NL80211_IFTYPE_STATION) |
  1573. BIT(NL80211_IFTYPE_ADHOC) |
  1574. BIT(NL80211_IFTYPE_MESH_POINT);
  1575. hw->queues = 4;
  1576. hw->max_rates = 4;
  1577. hw->channel_change_time = 5000;
  1578. hw->max_listen_interval = 10;
  1579. /* Hardware supports 10 but we use 4 */
  1580. hw->max_rate_tries = 4;
  1581. hw->sta_data_size = sizeof(struct ath_node);
  1582. hw->vif_data_size = sizeof(struct ath_vif);
  1583. hw->rate_control_algorithm = "ath9k_rate_control";
  1584. hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
  1585. &sc->sbands[IEEE80211_BAND_2GHZ];
  1586. if (test_bit(ATH9K_MODE_11A, sc->sc_ah->caps.wireless_modes))
  1587. hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
  1588. &sc->sbands[IEEE80211_BAND_5GHZ];
  1589. }
  1590. /* Device driver core initialization */
  1591. int ath_init_device(u16 devid, struct ath_softc *sc, u16 subsysid)
  1592. {
  1593. struct ieee80211_hw *hw = sc->hw;
  1594. struct ath_common *common;
  1595. struct ath_hw *ah;
  1596. int error = 0, i;
  1597. struct ath_regulatory *reg;
  1598. dev_dbg(sc->dev, "Attach ATH hw\n");
  1599. error = ath_init_softc(devid, sc, subsysid);
  1600. if (error != 0)
  1601. return error;
  1602. ah = sc->sc_ah;
  1603. common = ath9k_hw_common(ah);
  1604. /* get mac address from hardware and set in mac80211 */
  1605. SET_IEEE80211_PERM_ADDR(hw, common->macaddr);
  1606. ath_set_hw_capab(sc, hw);
  1607. error = ath_regd_init(&common->regulatory, sc->hw->wiphy,
  1608. ath9k_reg_notifier);
  1609. if (error)
  1610. return error;
  1611. reg = &common->regulatory;
  1612. if (ah->caps.hw_caps & ATH9K_HW_CAP_HT) {
  1613. setup_ht_cap(sc, &sc->sbands[IEEE80211_BAND_2GHZ].ht_cap);
  1614. if (test_bit(ATH9K_MODE_11A, ah->caps.wireless_modes))
  1615. setup_ht_cap(sc, &sc->sbands[IEEE80211_BAND_5GHZ].ht_cap);
  1616. }
  1617. /* initialize tx/rx engine */
  1618. error = ath_tx_init(sc, ATH_TXBUF);
  1619. if (error != 0)
  1620. goto error_attach;
  1621. error = ath_rx_init(sc, ATH_RXBUF);
  1622. if (error != 0)
  1623. goto error_attach;
  1624. INIT_WORK(&sc->chan_work, ath9k_wiphy_chan_work);
  1625. INIT_DELAYED_WORK(&sc->wiphy_work, ath9k_wiphy_work);
  1626. sc->wiphy_scheduler_int = msecs_to_jiffies(500);
  1627. error = ieee80211_register_hw(hw);
  1628. if (!ath_is_world_regd(reg)) {
  1629. error = regulatory_hint(hw->wiphy, reg->alpha2);
  1630. if (error)
  1631. goto error_attach;
  1632. }
  1633. /* Initialize LED control */
  1634. ath_init_leds(sc);
  1635. ath_start_rfkill_poll(sc);
  1636. return 0;
  1637. error_attach:
  1638. /* cleanup tx queues */
  1639. for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++)
  1640. if (ATH_TXQ_SETUP(sc, i))
  1641. ath_tx_cleanupq(sc, &sc->tx.txq[i]);
  1642. ath9k_hw_detach(ah);
  1643. ath9k_exit_debug(ah);
  1644. sc->sc_ah = NULL;
  1645. return error;
  1646. }
  1647. int ath_reset(struct ath_softc *sc, bool retry_tx)
  1648. {
  1649. struct ath_hw *ah = sc->sc_ah;
  1650. struct ath_common *common = ath9k_hw_common(ah);
  1651. struct ieee80211_hw *hw = sc->hw;
  1652. int r;
  1653. ath9k_hw_set_interrupts(ah, 0);
  1654. ath_drain_all_txq(sc, retry_tx);
  1655. ath_stoprecv(sc);
  1656. ath_flushrecv(sc);
  1657. spin_lock_bh(&sc->sc_resetlock);
  1658. r = ath9k_hw_reset(ah, sc->sc_ah->curchan, false);
  1659. if (r)
  1660. ath_print(common, ATH_DBG_FATAL,
  1661. "Unable to reset hardware; reset status %d\n", r);
  1662. spin_unlock_bh(&sc->sc_resetlock);
  1663. if (ath_startrecv(sc) != 0)
  1664. ath_print(common, ATH_DBG_FATAL,
  1665. "Unable to start recv logic\n");
  1666. /*
  1667. * We may be doing a reset in response to a request
  1668. * that changes the channel so update any state that
  1669. * might change as a result.
  1670. */
  1671. ath_cache_conf_rate(sc, &hw->conf);
  1672. ath_update_txpow(sc);
  1673. if (sc->sc_flags & SC_OP_BEACONS)
  1674. ath_beacon_config(sc, NULL); /* restart beacons */
  1675. ath9k_hw_set_interrupts(ah, sc->imask);
  1676. if (retry_tx) {
  1677. int i;
  1678. for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
  1679. if (ATH_TXQ_SETUP(sc, i)) {
  1680. spin_lock_bh(&sc->tx.txq[i].axq_lock);
  1681. ath_txq_schedule(sc, &sc->tx.txq[i]);
  1682. spin_unlock_bh(&sc->tx.txq[i].axq_lock);
  1683. }
  1684. }
  1685. }
  1686. return r;
  1687. }
  1688. /*
  1689. * This function will allocate both the DMA descriptor structure, and the
  1690. * buffers it contains. These are used to contain the descriptors used
  1691. * by the system.
  1692. */
  1693. int ath_descdma_setup(struct ath_softc *sc, struct ath_descdma *dd,
  1694. struct list_head *head, const char *name,
  1695. int nbuf, int ndesc)
  1696. {
  1697. #define DS2PHYS(_dd, _ds) \
  1698. ((_dd)->dd_desc_paddr + ((caddr_t)(_ds) - (caddr_t)(_dd)->dd_desc))
  1699. #define ATH_DESC_4KB_BOUND_CHECK(_daddr) ((((_daddr) & 0xFFF) > 0xF7F) ? 1 : 0)
  1700. #define ATH_DESC_4KB_BOUND_NUM_SKIPPED(_len) ((_len) / 4096)
  1701. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  1702. struct ath_desc *ds;
  1703. struct ath_buf *bf;
  1704. int i, bsize, error;
  1705. ath_print(common, ATH_DBG_CONFIG, "%s DMA: %u buffers %u desc/buf\n",
  1706. name, nbuf, ndesc);
  1707. INIT_LIST_HEAD(head);
  1708. /* ath_desc must be a multiple of DWORDs */
  1709. if ((sizeof(struct ath_desc) % 4) != 0) {
  1710. ath_print(common, ATH_DBG_FATAL,
  1711. "ath_desc not DWORD aligned\n");
  1712. ASSERT((sizeof(struct ath_desc) % 4) == 0);
  1713. error = -ENOMEM;
  1714. goto fail;
  1715. }
  1716. dd->dd_desc_len = sizeof(struct ath_desc) * nbuf * ndesc;
  1717. /*
  1718. * Need additional DMA memory because we can't use
  1719. * descriptors that cross the 4K page boundary. Assume
  1720. * one skipped descriptor per 4K page.
  1721. */
  1722. if (!(sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_4KB_SPLITTRANS)) {
  1723. u32 ndesc_skipped =
  1724. ATH_DESC_4KB_BOUND_NUM_SKIPPED(dd->dd_desc_len);
  1725. u32 dma_len;
  1726. while (ndesc_skipped) {
  1727. dma_len = ndesc_skipped * sizeof(struct ath_desc);
  1728. dd->dd_desc_len += dma_len;
  1729. ndesc_skipped = ATH_DESC_4KB_BOUND_NUM_SKIPPED(dma_len);
  1730. };
  1731. }
  1732. /* allocate descriptors */
  1733. dd->dd_desc = dma_alloc_coherent(sc->dev, dd->dd_desc_len,
  1734. &dd->dd_desc_paddr, GFP_KERNEL);
  1735. if (dd->dd_desc == NULL) {
  1736. error = -ENOMEM;
  1737. goto fail;
  1738. }
  1739. ds = dd->dd_desc;
  1740. ath_print(common, ATH_DBG_CONFIG, "%s DMA map: %p (%u) -> %llx (%u)\n",
  1741. name, ds, (u32) dd->dd_desc_len,
  1742. ito64(dd->dd_desc_paddr), /*XXX*/(u32) dd->dd_desc_len);
  1743. /* allocate buffers */
  1744. bsize = sizeof(struct ath_buf) * nbuf;
  1745. bf = kzalloc(bsize, GFP_KERNEL);
  1746. if (bf == NULL) {
  1747. error = -ENOMEM;
  1748. goto fail2;
  1749. }
  1750. dd->dd_bufptr = bf;
  1751. for (i = 0; i < nbuf; i++, bf++, ds += ndesc) {
  1752. bf->bf_desc = ds;
  1753. bf->bf_daddr = DS2PHYS(dd, ds);
  1754. if (!(sc->sc_ah->caps.hw_caps &
  1755. ATH9K_HW_CAP_4KB_SPLITTRANS)) {
  1756. /*
  1757. * Skip descriptor addresses which can cause 4KB
  1758. * boundary crossing (addr + length) with a 32 dword
  1759. * descriptor fetch.
  1760. */
  1761. while (ATH_DESC_4KB_BOUND_CHECK(bf->bf_daddr)) {
  1762. ASSERT((caddr_t) bf->bf_desc <
  1763. ((caddr_t) dd->dd_desc +
  1764. dd->dd_desc_len));
  1765. ds += ndesc;
  1766. bf->bf_desc = ds;
  1767. bf->bf_daddr = DS2PHYS(dd, ds);
  1768. }
  1769. }
  1770. list_add_tail(&bf->list, head);
  1771. }
  1772. return 0;
  1773. fail2:
  1774. dma_free_coherent(sc->dev, dd->dd_desc_len, dd->dd_desc,
  1775. dd->dd_desc_paddr);
  1776. fail:
  1777. memset(dd, 0, sizeof(*dd));
  1778. return error;
  1779. #undef ATH_DESC_4KB_BOUND_CHECK
  1780. #undef ATH_DESC_4KB_BOUND_NUM_SKIPPED
  1781. #undef DS2PHYS
  1782. }
  1783. void ath_descdma_cleanup(struct ath_softc *sc,
  1784. struct ath_descdma *dd,
  1785. struct list_head *head)
  1786. {
  1787. dma_free_coherent(sc->dev, dd->dd_desc_len, dd->dd_desc,
  1788. dd->dd_desc_paddr);
  1789. INIT_LIST_HEAD(head);
  1790. kfree(dd->dd_bufptr);
  1791. memset(dd, 0, sizeof(*dd));
  1792. }
  1793. int ath_get_hal_qnum(u16 queue, struct ath_softc *sc)
  1794. {
  1795. int qnum;
  1796. switch (queue) {
  1797. case 0:
  1798. qnum = sc->tx.hwq_map[ATH9K_WME_AC_VO];
  1799. break;
  1800. case 1:
  1801. qnum = sc->tx.hwq_map[ATH9K_WME_AC_VI];
  1802. break;
  1803. case 2:
  1804. qnum = sc->tx.hwq_map[ATH9K_WME_AC_BE];
  1805. break;
  1806. case 3:
  1807. qnum = sc->tx.hwq_map[ATH9K_WME_AC_BK];
  1808. break;
  1809. default:
  1810. qnum = sc->tx.hwq_map[ATH9K_WME_AC_BE];
  1811. break;
  1812. }
  1813. return qnum;
  1814. }
  1815. int ath_get_mac80211_qnum(u32 queue, struct ath_softc *sc)
  1816. {
  1817. int qnum;
  1818. switch (queue) {
  1819. case ATH9K_WME_AC_VO:
  1820. qnum = 0;
  1821. break;
  1822. case ATH9K_WME_AC_VI:
  1823. qnum = 1;
  1824. break;
  1825. case ATH9K_WME_AC_BE:
  1826. qnum = 2;
  1827. break;
  1828. case ATH9K_WME_AC_BK:
  1829. qnum = 3;
  1830. break;
  1831. default:
  1832. qnum = -1;
  1833. break;
  1834. }
  1835. return qnum;
  1836. }
  1837. /* XXX: Remove me once we don't depend on ath9k_channel for all
  1838. * this redundant data */
  1839. void ath9k_update_ichannel(struct ath_softc *sc, struct ieee80211_hw *hw,
  1840. struct ath9k_channel *ichan)
  1841. {
  1842. struct ieee80211_channel *chan = hw->conf.channel;
  1843. struct ieee80211_conf *conf = &hw->conf;
  1844. ichan->channel = chan->center_freq;
  1845. ichan->chan = chan;
  1846. if (chan->band == IEEE80211_BAND_2GHZ) {
  1847. ichan->chanmode = CHANNEL_G;
  1848. ichan->channelFlags = CHANNEL_2GHZ | CHANNEL_OFDM | CHANNEL_G;
  1849. } else {
  1850. ichan->chanmode = CHANNEL_A;
  1851. ichan->channelFlags = CHANNEL_5GHZ | CHANNEL_OFDM;
  1852. }
  1853. if (conf_is_ht(conf))
  1854. ichan->chanmode = ath_get_extchanmode(sc, chan,
  1855. conf->channel_type);
  1856. }
  1857. /**********************/
  1858. /* mac80211 callbacks */
  1859. /**********************/
  1860. /*
  1861. * (Re)start btcoex timers
  1862. */
  1863. static void ath9k_btcoex_timer_resume(struct ath_softc *sc)
  1864. {
  1865. struct ath_btcoex *btcoex = &sc->btcoex;
  1866. struct ath_hw *ah = sc->sc_ah;
  1867. ath_print(ath9k_hw_common(ah), ATH_DBG_BTCOEX,
  1868. "Starting btcoex timers");
  1869. /* make sure duty cycle timer is also stopped when resuming */
  1870. if (btcoex->hw_timer_enabled)
  1871. ath9k_gen_timer_stop(sc->sc_ah, btcoex->no_stomp_timer);
  1872. btcoex->bt_priority_cnt = 0;
  1873. btcoex->bt_priority_time = jiffies;
  1874. sc->sc_flags &= ~SC_OP_BT_PRIORITY_DETECTED;
  1875. mod_timer(&btcoex->period_timer, jiffies);
  1876. }
  1877. static int ath9k_start(struct ieee80211_hw *hw)
  1878. {
  1879. struct ath_wiphy *aphy = hw->priv;
  1880. struct ath_softc *sc = aphy->sc;
  1881. struct ath_hw *ah = sc->sc_ah;
  1882. struct ath_common *common = ath9k_hw_common(ah);
  1883. struct ieee80211_channel *curchan = hw->conf.channel;
  1884. struct ath9k_channel *init_channel;
  1885. int r;
  1886. ath_print(common, ATH_DBG_CONFIG,
  1887. "Starting driver with initial channel: %d MHz\n",
  1888. curchan->center_freq);
  1889. mutex_lock(&sc->mutex);
  1890. if (ath9k_wiphy_started(sc)) {
  1891. if (sc->chan_idx == curchan->hw_value) {
  1892. /*
  1893. * Already on the operational channel, the new wiphy
  1894. * can be marked active.
  1895. */
  1896. aphy->state = ATH_WIPHY_ACTIVE;
  1897. ieee80211_wake_queues(hw);
  1898. } else {
  1899. /*
  1900. * Another wiphy is on another channel, start the new
  1901. * wiphy in paused state.
  1902. */
  1903. aphy->state = ATH_WIPHY_PAUSED;
  1904. ieee80211_stop_queues(hw);
  1905. }
  1906. mutex_unlock(&sc->mutex);
  1907. return 0;
  1908. }
  1909. aphy->state = ATH_WIPHY_ACTIVE;
  1910. /* setup initial channel */
  1911. sc->chan_idx = curchan->hw_value;
  1912. init_channel = ath_get_curchannel(sc, hw);
  1913. /* Reset SERDES registers */
  1914. ath9k_hw_configpcipowersave(ah, 0, 0);
  1915. /*
  1916. * The basic interface to setting the hardware in a good
  1917. * state is ``reset''. On return the hardware is known to
  1918. * be powered up and with interrupts disabled. This must
  1919. * be followed by initialization of the appropriate bits
  1920. * and then setup of the interrupt mask.
  1921. */
  1922. spin_lock_bh(&sc->sc_resetlock);
  1923. r = ath9k_hw_reset(ah, init_channel, false);
  1924. if (r) {
  1925. ath_print(common, ATH_DBG_FATAL,
  1926. "Unable to reset hardware; reset status %d "
  1927. "(freq %u MHz)\n", r,
  1928. curchan->center_freq);
  1929. spin_unlock_bh(&sc->sc_resetlock);
  1930. goto mutex_unlock;
  1931. }
  1932. spin_unlock_bh(&sc->sc_resetlock);
  1933. /*
  1934. * This is needed only to setup initial state
  1935. * but it's best done after a reset.
  1936. */
  1937. ath_update_txpow(sc);
  1938. /*
  1939. * Setup the hardware after reset:
  1940. * The receive engine is set going.
  1941. * Frame transmit is handled entirely
  1942. * in the frame output path; there's nothing to do
  1943. * here except setup the interrupt mask.
  1944. */
  1945. if (ath_startrecv(sc) != 0) {
  1946. ath_print(common, ATH_DBG_FATAL,
  1947. "Unable to start recv logic\n");
  1948. r = -EIO;
  1949. goto mutex_unlock;
  1950. }
  1951. /* Setup our intr mask. */
  1952. sc->imask = ATH9K_INT_RX | ATH9K_INT_TX
  1953. | ATH9K_INT_RXEOL | ATH9K_INT_RXORN
  1954. | ATH9K_INT_FATAL | ATH9K_INT_GLOBAL;
  1955. if (ah->caps.hw_caps & ATH9K_HW_CAP_GTT)
  1956. sc->imask |= ATH9K_INT_GTT;
  1957. if (ah->caps.hw_caps & ATH9K_HW_CAP_HT)
  1958. sc->imask |= ATH9K_INT_CST;
  1959. ath_cache_conf_rate(sc, &hw->conf);
  1960. sc->sc_flags &= ~SC_OP_INVALID;
  1961. /* Disable BMISS interrupt when we're not associated */
  1962. sc->imask &= ~(ATH9K_INT_SWBA | ATH9K_INT_BMISS);
  1963. ath9k_hw_set_interrupts(ah, sc->imask);
  1964. ieee80211_wake_queues(hw);
  1965. ieee80211_queue_delayed_work(sc->hw, &sc->tx_complete_work, 0);
  1966. if ((ah->btcoex_hw.scheme != ATH_BTCOEX_CFG_NONE) &&
  1967. !ah->btcoex_hw.enabled) {
  1968. ath9k_hw_btcoex_set_weight(ah, AR_BT_COEX_WGHT,
  1969. AR_STOMP_LOW_WLAN_WGHT);
  1970. ath9k_hw_btcoex_enable(ah);
  1971. if (sc->bus_ops->bt_coex_prep)
  1972. sc->bus_ops->bt_coex_prep(sc);
  1973. if (ah->btcoex_hw.scheme == ATH_BTCOEX_CFG_3WIRE)
  1974. ath9k_btcoex_timer_resume(sc);
  1975. }
  1976. mutex_unlock:
  1977. mutex_unlock(&sc->mutex);
  1978. return r;
  1979. }
  1980. static int ath9k_tx(struct ieee80211_hw *hw,
  1981. struct sk_buff *skb)
  1982. {
  1983. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1984. struct ath_wiphy *aphy = hw->priv;
  1985. struct ath_softc *sc = aphy->sc;
  1986. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  1987. struct ath_tx_control txctl;
  1988. int hdrlen, padsize;
  1989. if (aphy->state != ATH_WIPHY_ACTIVE && aphy->state != ATH_WIPHY_SCAN) {
  1990. ath_print(common, ATH_DBG_XMIT,
  1991. "ath9k: %s: TX in unexpected wiphy state "
  1992. "%d\n", wiphy_name(hw->wiphy), aphy->state);
  1993. goto exit;
  1994. }
  1995. if (sc->ps_enabled) {
  1996. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  1997. /*
  1998. * mac80211 does not set PM field for normal data frames, so we
  1999. * need to update that based on the current PS mode.
  2000. */
  2001. if (ieee80211_is_data(hdr->frame_control) &&
  2002. !ieee80211_is_nullfunc(hdr->frame_control) &&
  2003. !ieee80211_has_pm(hdr->frame_control)) {
  2004. ath_print(common, ATH_DBG_PS, "Add PM=1 for a TX frame "
  2005. "while in PS mode\n");
  2006. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
  2007. }
  2008. }
  2009. if (unlikely(sc->sc_ah->power_mode != ATH9K_PM_AWAKE)) {
  2010. /*
  2011. * We are using PS-Poll and mac80211 can request TX while in
  2012. * power save mode. Need to wake up hardware for the TX to be
  2013. * completed and if needed, also for RX of buffered frames.
  2014. */
  2015. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  2016. ath9k_ps_wakeup(sc);
  2017. ath9k_hw_setrxabort(sc->sc_ah, 0);
  2018. if (ieee80211_is_pspoll(hdr->frame_control)) {
  2019. ath_print(common, ATH_DBG_PS,
  2020. "Sending PS-Poll to pick a buffered frame\n");
  2021. sc->sc_flags |= SC_OP_WAIT_FOR_PSPOLL_DATA;
  2022. } else {
  2023. ath_print(common, ATH_DBG_PS,
  2024. "Wake up to complete TX\n");
  2025. sc->sc_flags |= SC_OP_WAIT_FOR_TX_ACK;
  2026. }
  2027. /*
  2028. * The actual restore operation will happen only after
  2029. * the sc_flags bit is cleared. We are just dropping
  2030. * the ps_usecount here.
  2031. */
  2032. ath9k_ps_restore(sc);
  2033. }
  2034. memset(&txctl, 0, sizeof(struct ath_tx_control));
  2035. /*
  2036. * As a temporary workaround, assign seq# here; this will likely need
  2037. * to be cleaned up to work better with Beacon transmission and virtual
  2038. * BSSes.
  2039. */
  2040. if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
  2041. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  2042. if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
  2043. sc->tx.seq_no += 0x10;
  2044. hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
  2045. hdr->seq_ctrl |= cpu_to_le16(sc->tx.seq_no);
  2046. }
  2047. /* Add the padding after the header if this is not already done */
  2048. hdrlen = ieee80211_get_hdrlen_from_skb(skb);
  2049. if (hdrlen & 3) {
  2050. padsize = hdrlen % 4;
  2051. if (skb_headroom(skb) < padsize)
  2052. return -1;
  2053. skb_push(skb, padsize);
  2054. memmove(skb->data, skb->data + padsize, hdrlen);
  2055. }
  2056. /* Check if a tx queue is available */
  2057. txctl.txq = ath_test_get_txq(sc, skb);
  2058. if (!txctl.txq)
  2059. goto exit;
  2060. ath_print(common, ATH_DBG_XMIT, "transmitting packet, skb: %p\n", skb);
  2061. if (ath_tx_start(hw, skb, &txctl) != 0) {
  2062. ath_print(common, ATH_DBG_XMIT, "TX failed\n");
  2063. goto exit;
  2064. }
  2065. return 0;
  2066. exit:
  2067. dev_kfree_skb_any(skb);
  2068. return 0;
  2069. }
  2070. /*
  2071. * Pause btcoex timer and bt duty cycle timer
  2072. */
  2073. static void ath9k_btcoex_timer_pause(struct ath_softc *sc)
  2074. {
  2075. struct ath_btcoex *btcoex = &sc->btcoex;
  2076. struct ath_hw *ah = sc->sc_ah;
  2077. del_timer_sync(&btcoex->period_timer);
  2078. if (btcoex->hw_timer_enabled)
  2079. ath9k_gen_timer_stop(ah, btcoex->no_stomp_timer);
  2080. btcoex->hw_timer_enabled = false;
  2081. }
  2082. static void ath9k_stop(struct ieee80211_hw *hw)
  2083. {
  2084. struct ath_wiphy *aphy = hw->priv;
  2085. struct ath_softc *sc = aphy->sc;
  2086. struct ath_hw *ah = sc->sc_ah;
  2087. struct ath_common *common = ath9k_hw_common(ah);
  2088. mutex_lock(&sc->mutex);
  2089. aphy->state = ATH_WIPHY_INACTIVE;
  2090. cancel_delayed_work_sync(&sc->ath_led_blink_work);
  2091. cancel_delayed_work_sync(&sc->tx_complete_work);
  2092. if (!sc->num_sec_wiphy) {
  2093. cancel_delayed_work_sync(&sc->wiphy_work);
  2094. cancel_work_sync(&sc->chan_work);
  2095. }
  2096. if (sc->sc_flags & SC_OP_INVALID) {
  2097. ath_print(common, ATH_DBG_ANY, "Device not present\n");
  2098. mutex_unlock(&sc->mutex);
  2099. return;
  2100. }
  2101. if (ath9k_wiphy_started(sc)) {
  2102. mutex_unlock(&sc->mutex);
  2103. return; /* another wiphy still in use */
  2104. }
  2105. if (ah->btcoex_hw.enabled) {
  2106. ath9k_hw_btcoex_disable(ah);
  2107. if (ah->btcoex_hw.scheme == ATH_BTCOEX_CFG_3WIRE)
  2108. ath9k_btcoex_timer_pause(sc);
  2109. }
  2110. /* make sure h/w will not generate any interrupt
  2111. * before setting the invalid flag. */
  2112. ath9k_hw_set_interrupts(ah, 0);
  2113. if (!(sc->sc_flags & SC_OP_INVALID)) {
  2114. ath_drain_all_txq(sc, false);
  2115. ath_stoprecv(sc);
  2116. ath9k_hw_phy_disable(ah);
  2117. } else
  2118. sc->rx.rxlink = NULL;
  2119. /* disable HAL and put h/w to sleep */
  2120. ath9k_hw_disable(ah);
  2121. ath9k_hw_configpcipowersave(ah, 1, 1);
  2122. ath9k_setpower(sc, ATH9K_PM_FULL_SLEEP);
  2123. sc->sc_flags |= SC_OP_INVALID;
  2124. mutex_unlock(&sc->mutex);
  2125. ath_print(common, ATH_DBG_CONFIG, "Driver halt\n");
  2126. }
  2127. static int ath9k_add_interface(struct ieee80211_hw *hw,
  2128. struct ieee80211_if_init_conf *conf)
  2129. {
  2130. struct ath_wiphy *aphy = hw->priv;
  2131. struct ath_softc *sc = aphy->sc;
  2132. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  2133. struct ath_vif *avp = (void *)conf->vif->drv_priv;
  2134. enum nl80211_iftype ic_opmode = NL80211_IFTYPE_UNSPECIFIED;
  2135. int ret = 0;
  2136. mutex_lock(&sc->mutex);
  2137. if (!(sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_BSSIDMASK) &&
  2138. sc->nvifs > 0) {
  2139. ret = -ENOBUFS;
  2140. goto out;
  2141. }
  2142. switch (conf->type) {
  2143. case NL80211_IFTYPE_STATION:
  2144. ic_opmode = NL80211_IFTYPE_STATION;
  2145. break;
  2146. case NL80211_IFTYPE_ADHOC:
  2147. case NL80211_IFTYPE_AP:
  2148. case NL80211_IFTYPE_MESH_POINT:
  2149. if (sc->nbcnvifs >= ATH_BCBUF) {
  2150. ret = -ENOBUFS;
  2151. goto out;
  2152. }
  2153. ic_opmode = conf->type;
  2154. break;
  2155. default:
  2156. ath_print(common, ATH_DBG_FATAL,
  2157. "Interface type %d not yet supported\n", conf->type);
  2158. ret = -EOPNOTSUPP;
  2159. goto out;
  2160. }
  2161. ath_print(common, ATH_DBG_CONFIG,
  2162. "Attach a VIF of type: %d\n", ic_opmode);
  2163. /* Set the VIF opmode */
  2164. avp->av_opmode = ic_opmode;
  2165. avp->av_bslot = -1;
  2166. sc->nvifs++;
  2167. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_BSSIDMASK)
  2168. ath9k_set_bssid_mask(hw);
  2169. if (sc->nvifs > 1)
  2170. goto out; /* skip global settings for secondary vif */
  2171. if (ic_opmode == NL80211_IFTYPE_AP) {
  2172. ath9k_hw_set_tsfadjust(sc->sc_ah, 1);
  2173. sc->sc_flags |= SC_OP_TSF_RESET;
  2174. }
  2175. /* Set the device opmode */
  2176. sc->sc_ah->opmode = ic_opmode;
  2177. /*
  2178. * Enable MIB interrupts when there are hardware phy counters.
  2179. * Note we only do this (at the moment) for station mode.
  2180. */
  2181. if ((conf->type == NL80211_IFTYPE_STATION) ||
  2182. (conf->type == NL80211_IFTYPE_ADHOC) ||
  2183. (conf->type == NL80211_IFTYPE_MESH_POINT)) {
  2184. sc->imask |= ATH9K_INT_MIB;
  2185. sc->imask |= ATH9K_INT_TSFOOR;
  2186. }
  2187. ath9k_hw_set_interrupts(sc->sc_ah, sc->imask);
  2188. if (conf->type == NL80211_IFTYPE_AP ||
  2189. conf->type == NL80211_IFTYPE_ADHOC ||
  2190. conf->type == NL80211_IFTYPE_MONITOR)
  2191. ath_start_ani(sc);
  2192. out:
  2193. mutex_unlock(&sc->mutex);
  2194. return ret;
  2195. }
  2196. static void ath9k_remove_interface(struct ieee80211_hw *hw,
  2197. struct ieee80211_if_init_conf *conf)
  2198. {
  2199. struct ath_wiphy *aphy = hw->priv;
  2200. struct ath_softc *sc = aphy->sc;
  2201. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  2202. struct ath_vif *avp = (void *)conf->vif->drv_priv;
  2203. int i;
  2204. ath_print(common, ATH_DBG_CONFIG, "Detach Interface\n");
  2205. mutex_lock(&sc->mutex);
  2206. /* Stop ANI */
  2207. del_timer_sync(&sc->ani.timer);
  2208. /* Reclaim beacon resources */
  2209. if ((sc->sc_ah->opmode == NL80211_IFTYPE_AP) ||
  2210. (sc->sc_ah->opmode == NL80211_IFTYPE_ADHOC) ||
  2211. (sc->sc_ah->opmode == NL80211_IFTYPE_MESH_POINT)) {
  2212. ath9k_hw_stoptxdma(sc->sc_ah, sc->beacon.beaconq);
  2213. ath_beacon_return(sc, avp);
  2214. }
  2215. sc->sc_flags &= ~SC_OP_BEACONS;
  2216. for (i = 0; i < ARRAY_SIZE(sc->beacon.bslot); i++) {
  2217. if (sc->beacon.bslot[i] == conf->vif) {
  2218. printk(KERN_DEBUG "%s: vif had allocated beacon "
  2219. "slot\n", __func__);
  2220. sc->beacon.bslot[i] = NULL;
  2221. sc->beacon.bslot_aphy[i] = NULL;
  2222. }
  2223. }
  2224. sc->nvifs--;
  2225. mutex_unlock(&sc->mutex);
  2226. }
  2227. static int ath9k_config(struct ieee80211_hw *hw, u32 changed)
  2228. {
  2229. struct ath_wiphy *aphy = hw->priv;
  2230. struct ath_softc *sc = aphy->sc;
  2231. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  2232. struct ieee80211_conf *conf = &hw->conf;
  2233. struct ath_hw *ah = sc->sc_ah;
  2234. bool all_wiphys_idle = false, disable_radio = false;
  2235. mutex_lock(&sc->mutex);
  2236. /* Leave this as the first check */
  2237. if (changed & IEEE80211_CONF_CHANGE_IDLE) {
  2238. spin_lock_bh(&sc->wiphy_lock);
  2239. all_wiphys_idle = ath9k_all_wiphys_idle(sc);
  2240. spin_unlock_bh(&sc->wiphy_lock);
  2241. if (conf->flags & IEEE80211_CONF_IDLE){
  2242. if (all_wiphys_idle)
  2243. disable_radio = true;
  2244. }
  2245. else if (all_wiphys_idle) {
  2246. ath_radio_enable(sc);
  2247. ath_print(common, ATH_DBG_CONFIG,
  2248. "not-idle: enabling radio\n");
  2249. }
  2250. }
  2251. if (changed & IEEE80211_CONF_CHANGE_PS) {
  2252. if (conf->flags & IEEE80211_CONF_PS) {
  2253. if (!(ah->caps.hw_caps &
  2254. ATH9K_HW_CAP_AUTOSLEEP)) {
  2255. if ((sc->imask & ATH9K_INT_TIM_TIMER) == 0) {
  2256. sc->imask |= ATH9K_INT_TIM_TIMER;
  2257. ath9k_hw_set_interrupts(sc->sc_ah,
  2258. sc->imask);
  2259. }
  2260. ath9k_hw_setrxabort(sc->sc_ah, 1);
  2261. }
  2262. sc->ps_enabled = true;
  2263. } else {
  2264. sc->ps_enabled = false;
  2265. ath9k_setpower(sc, ATH9K_PM_AWAKE);
  2266. if (!(ah->caps.hw_caps &
  2267. ATH9K_HW_CAP_AUTOSLEEP)) {
  2268. ath9k_hw_setrxabort(sc->sc_ah, 0);
  2269. sc->sc_flags &= ~(SC_OP_WAIT_FOR_BEACON |
  2270. SC_OP_WAIT_FOR_CAB |
  2271. SC_OP_WAIT_FOR_PSPOLL_DATA |
  2272. SC_OP_WAIT_FOR_TX_ACK);
  2273. if (sc->imask & ATH9K_INT_TIM_TIMER) {
  2274. sc->imask &= ~ATH9K_INT_TIM_TIMER;
  2275. ath9k_hw_set_interrupts(sc->sc_ah,
  2276. sc->imask);
  2277. }
  2278. }
  2279. }
  2280. }
  2281. if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
  2282. struct ieee80211_channel *curchan = hw->conf.channel;
  2283. int pos = curchan->hw_value;
  2284. aphy->chan_idx = pos;
  2285. aphy->chan_is_ht = conf_is_ht(conf);
  2286. if (aphy->state == ATH_WIPHY_SCAN ||
  2287. aphy->state == ATH_WIPHY_ACTIVE)
  2288. ath9k_wiphy_pause_all_forced(sc, aphy);
  2289. else {
  2290. /*
  2291. * Do not change operational channel based on a paused
  2292. * wiphy changes.
  2293. */
  2294. goto skip_chan_change;
  2295. }
  2296. ath_print(common, ATH_DBG_CONFIG, "Set channel: %d MHz\n",
  2297. curchan->center_freq);
  2298. /* XXX: remove me eventualy */
  2299. ath9k_update_ichannel(sc, hw, &sc->sc_ah->channels[pos]);
  2300. ath_update_chainmask(sc, conf_is_ht(conf));
  2301. if (ath_set_channel(sc, hw, &sc->sc_ah->channels[pos]) < 0) {
  2302. ath_print(common, ATH_DBG_FATAL,
  2303. "Unable to set channel\n");
  2304. mutex_unlock(&sc->mutex);
  2305. return -EINVAL;
  2306. }
  2307. }
  2308. skip_chan_change:
  2309. if (changed & IEEE80211_CONF_CHANGE_POWER)
  2310. sc->config.txpowlimit = 2 * conf->power_level;
  2311. if (disable_radio) {
  2312. ath_print(common, ATH_DBG_CONFIG, "idle: disabling radio\n");
  2313. ath_radio_disable(sc);
  2314. }
  2315. mutex_unlock(&sc->mutex);
  2316. return 0;
  2317. }
  2318. #define SUPPORTED_FILTERS \
  2319. (FIF_PROMISC_IN_BSS | \
  2320. FIF_ALLMULTI | \
  2321. FIF_CONTROL | \
  2322. FIF_PSPOLL | \
  2323. FIF_OTHER_BSS | \
  2324. FIF_BCN_PRBRESP_PROMISC | \
  2325. FIF_FCSFAIL)
  2326. /* FIXME: sc->sc_full_reset ? */
  2327. static void ath9k_configure_filter(struct ieee80211_hw *hw,
  2328. unsigned int changed_flags,
  2329. unsigned int *total_flags,
  2330. u64 multicast)
  2331. {
  2332. struct ath_wiphy *aphy = hw->priv;
  2333. struct ath_softc *sc = aphy->sc;
  2334. u32 rfilt;
  2335. changed_flags &= SUPPORTED_FILTERS;
  2336. *total_flags &= SUPPORTED_FILTERS;
  2337. sc->rx.rxfilter = *total_flags;
  2338. ath9k_ps_wakeup(sc);
  2339. rfilt = ath_calcrxfilter(sc);
  2340. ath9k_hw_setrxfilter(sc->sc_ah, rfilt);
  2341. ath9k_ps_restore(sc);
  2342. ath_print(ath9k_hw_common(sc->sc_ah), ATH_DBG_CONFIG,
  2343. "Set HW RX filter: 0x%x\n", rfilt);
  2344. }
  2345. static void ath9k_sta_notify(struct ieee80211_hw *hw,
  2346. struct ieee80211_vif *vif,
  2347. enum sta_notify_cmd cmd,
  2348. struct ieee80211_sta *sta)
  2349. {
  2350. struct ath_wiphy *aphy = hw->priv;
  2351. struct ath_softc *sc = aphy->sc;
  2352. switch (cmd) {
  2353. case STA_NOTIFY_ADD:
  2354. ath_node_attach(sc, sta);
  2355. break;
  2356. case STA_NOTIFY_REMOVE:
  2357. ath_node_detach(sc, sta);
  2358. break;
  2359. default:
  2360. break;
  2361. }
  2362. }
  2363. static int ath9k_conf_tx(struct ieee80211_hw *hw, u16 queue,
  2364. const struct ieee80211_tx_queue_params *params)
  2365. {
  2366. struct ath_wiphy *aphy = hw->priv;
  2367. struct ath_softc *sc = aphy->sc;
  2368. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  2369. struct ath9k_tx_queue_info qi;
  2370. int ret = 0, qnum;
  2371. if (queue >= WME_NUM_AC)
  2372. return 0;
  2373. mutex_lock(&sc->mutex);
  2374. memset(&qi, 0, sizeof(struct ath9k_tx_queue_info));
  2375. qi.tqi_aifs = params->aifs;
  2376. qi.tqi_cwmin = params->cw_min;
  2377. qi.tqi_cwmax = params->cw_max;
  2378. qi.tqi_burstTime = params->txop;
  2379. qnum = ath_get_hal_qnum(queue, sc);
  2380. ath_print(common, ATH_DBG_CONFIG,
  2381. "Configure tx [queue/halq] [%d/%d], "
  2382. "aifs: %d, cw_min: %d, cw_max: %d, txop: %d\n",
  2383. queue, qnum, params->aifs, params->cw_min,
  2384. params->cw_max, params->txop);
  2385. ret = ath_txq_update(sc, qnum, &qi);
  2386. if (ret)
  2387. ath_print(common, ATH_DBG_FATAL, "TXQ Update failed\n");
  2388. mutex_unlock(&sc->mutex);
  2389. return ret;
  2390. }
  2391. static int ath9k_set_key(struct ieee80211_hw *hw,
  2392. enum set_key_cmd cmd,
  2393. struct ieee80211_vif *vif,
  2394. struct ieee80211_sta *sta,
  2395. struct ieee80211_key_conf *key)
  2396. {
  2397. struct ath_wiphy *aphy = hw->priv;
  2398. struct ath_softc *sc = aphy->sc;
  2399. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  2400. int ret = 0;
  2401. if (modparam_nohwcrypt)
  2402. return -ENOSPC;
  2403. mutex_lock(&sc->mutex);
  2404. ath9k_ps_wakeup(sc);
  2405. ath_print(common, ATH_DBG_CONFIG, "Set HW Key\n");
  2406. switch (cmd) {
  2407. case SET_KEY:
  2408. ret = ath_key_config(sc, vif, sta, key);
  2409. if (ret >= 0) {
  2410. key->hw_key_idx = ret;
  2411. /* push IV and Michael MIC generation to stack */
  2412. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  2413. if (key->alg == ALG_TKIP)
  2414. key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
  2415. if (sc->sc_ah->sw_mgmt_crypto && key->alg == ALG_CCMP)
  2416. key->flags |= IEEE80211_KEY_FLAG_SW_MGMT;
  2417. ret = 0;
  2418. }
  2419. break;
  2420. case DISABLE_KEY:
  2421. ath_key_delete(sc, key);
  2422. break;
  2423. default:
  2424. ret = -EINVAL;
  2425. }
  2426. ath9k_ps_restore(sc);
  2427. mutex_unlock(&sc->mutex);
  2428. return ret;
  2429. }
  2430. static void ath9k_bss_info_changed(struct ieee80211_hw *hw,
  2431. struct ieee80211_vif *vif,
  2432. struct ieee80211_bss_conf *bss_conf,
  2433. u32 changed)
  2434. {
  2435. struct ath_wiphy *aphy = hw->priv;
  2436. struct ath_softc *sc = aphy->sc;
  2437. struct ath_hw *ah = sc->sc_ah;
  2438. struct ath_common *common = ath9k_hw_common(ah);
  2439. struct ath_vif *avp = (void *)vif->drv_priv;
  2440. u32 rfilt = 0;
  2441. int error, i;
  2442. mutex_lock(&sc->mutex);
  2443. /*
  2444. * TODO: Need to decide which hw opmode to use for
  2445. * multi-interface cases
  2446. * XXX: This belongs into add_interface!
  2447. */
  2448. if (vif->type == NL80211_IFTYPE_AP &&
  2449. ah->opmode != NL80211_IFTYPE_AP) {
  2450. ah->opmode = NL80211_IFTYPE_STATION;
  2451. ath9k_hw_setopmode(ah);
  2452. memcpy(common->curbssid, common->macaddr, ETH_ALEN);
  2453. common->curaid = 0;
  2454. ath9k_hw_write_associd(ah);
  2455. /* Request full reset to get hw opmode changed properly */
  2456. sc->sc_flags |= SC_OP_FULL_RESET;
  2457. }
  2458. if ((changed & BSS_CHANGED_BSSID) &&
  2459. !is_zero_ether_addr(bss_conf->bssid)) {
  2460. switch (vif->type) {
  2461. case NL80211_IFTYPE_STATION:
  2462. case NL80211_IFTYPE_ADHOC:
  2463. case NL80211_IFTYPE_MESH_POINT:
  2464. /* Set BSSID */
  2465. memcpy(common->curbssid, bss_conf->bssid, ETH_ALEN);
  2466. memcpy(avp->bssid, bss_conf->bssid, ETH_ALEN);
  2467. common->curaid = 0;
  2468. ath9k_hw_write_associd(ah);
  2469. /* Set aggregation protection mode parameters */
  2470. sc->config.ath_aggr_prot = 0;
  2471. ath_print(common, ATH_DBG_CONFIG,
  2472. "RX filter 0x%x bssid %pM aid 0x%x\n",
  2473. rfilt, common->curbssid, common->curaid);
  2474. /* need to reconfigure the beacon */
  2475. sc->sc_flags &= ~SC_OP_BEACONS ;
  2476. break;
  2477. default:
  2478. break;
  2479. }
  2480. }
  2481. if ((vif->type == NL80211_IFTYPE_ADHOC) ||
  2482. (vif->type == NL80211_IFTYPE_AP) ||
  2483. (vif->type == NL80211_IFTYPE_MESH_POINT)) {
  2484. if ((changed & BSS_CHANGED_BEACON) ||
  2485. (changed & BSS_CHANGED_BEACON_ENABLED &&
  2486. bss_conf->enable_beacon)) {
  2487. /*
  2488. * Allocate and setup the beacon frame.
  2489. *
  2490. * Stop any previous beacon DMA. This may be
  2491. * necessary, for example, when an ibss merge
  2492. * causes reconfiguration; we may be called
  2493. * with beacon transmission active.
  2494. */
  2495. ath9k_hw_stoptxdma(sc->sc_ah, sc->beacon.beaconq);
  2496. error = ath_beacon_alloc(aphy, vif);
  2497. if (!error)
  2498. ath_beacon_config(sc, vif);
  2499. }
  2500. }
  2501. /* Check for WLAN_CAPABILITY_PRIVACY ? */
  2502. if ((avp->av_opmode != NL80211_IFTYPE_STATION)) {
  2503. for (i = 0; i < IEEE80211_WEP_NKID; i++)
  2504. if (ath9k_hw_keyisvalid(sc->sc_ah, (u16)i))
  2505. ath9k_hw_keysetmac(sc->sc_ah,
  2506. (u16)i,
  2507. common->curbssid);
  2508. }
  2509. /* Only legacy IBSS for now */
  2510. if (vif->type == NL80211_IFTYPE_ADHOC)
  2511. ath_update_chainmask(sc, 0);
  2512. if (changed & BSS_CHANGED_ERP_PREAMBLE) {
  2513. ath_print(common, ATH_DBG_CONFIG, "BSS Changed PREAMBLE %d\n",
  2514. bss_conf->use_short_preamble);
  2515. if (bss_conf->use_short_preamble)
  2516. sc->sc_flags |= SC_OP_PREAMBLE_SHORT;
  2517. else
  2518. sc->sc_flags &= ~SC_OP_PREAMBLE_SHORT;
  2519. }
  2520. if (changed & BSS_CHANGED_ERP_CTS_PROT) {
  2521. ath_print(common, ATH_DBG_CONFIG, "BSS Changed CTS PROT %d\n",
  2522. bss_conf->use_cts_prot);
  2523. if (bss_conf->use_cts_prot &&
  2524. hw->conf.channel->band != IEEE80211_BAND_5GHZ)
  2525. sc->sc_flags |= SC_OP_PROTECT_ENABLE;
  2526. else
  2527. sc->sc_flags &= ~SC_OP_PROTECT_ENABLE;
  2528. }
  2529. if (changed & BSS_CHANGED_ASSOC) {
  2530. ath_print(common, ATH_DBG_CONFIG, "BSS Changed ASSOC %d\n",
  2531. bss_conf->assoc);
  2532. ath9k_bss_assoc_info(sc, vif, bss_conf);
  2533. }
  2534. /*
  2535. * The HW TSF has to be reset when the beacon interval changes.
  2536. * We set the flag here, and ath_beacon_config_ap() would take this
  2537. * into account when it gets called through the subsequent
  2538. * config_interface() call - with IFCC_BEACON in the changed field.
  2539. */
  2540. if (changed & BSS_CHANGED_BEACON_INT) {
  2541. sc->sc_flags |= SC_OP_TSF_RESET;
  2542. sc->beacon_interval = bss_conf->beacon_int;
  2543. }
  2544. mutex_unlock(&sc->mutex);
  2545. }
  2546. static u64 ath9k_get_tsf(struct ieee80211_hw *hw)
  2547. {
  2548. u64 tsf;
  2549. struct ath_wiphy *aphy = hw->priv;
  2550. struct ath_softc *sc = aphy->sc;
  2551. mutex_lock(&sc->mutex);
  2552. tsf = ath9k_hw_gettsf64(sc->sc_ah);
  2553. mutex_unlock(&sc->mutex);
  2554. return tsf;
  2555. }
  2556. static void ath9k_set_tsf(struct ieee80211_hw *hw, u64 tsf)
  2557. {
  2558. struct ath_wiphy *aphy = hw->priv;
  2559. struct ath_softc *sc = aphy->sc;
  2560. mutex_lock(&sc->mutex);
  2561. ath9k_hw_settsf64(sc->sc_ah, tsf);
  2562. mutex_unlock(&sc->mutex);
  2563. }
  2564. static void ath9k_reset_tsf(struct ieee80211_hw *hw)
  2565. {
  2566. struct ath_wiphy *aphy = hw->priv;
  2567. struct ath_softc *sc = aphy->sc;
  2568. mutex_lock(&sc->mutex);
  2569. ath9k_ps_wakeup(sc);
  2570. ath9k_hw_reset_tsf(sc->sc_ah);
  2571. ath9k_ps_restore(sc);
  2572. mutex_unlock(&sc->mutex);
  2573. }
  2574. static int ath9k_ampdu_action(struct ieee80211_hw *hw,
  2575. enum ieee80211_ampdu_mlme_action action,
  2576. struct ieee80211_sta *sta,
  2577. u16 tid, u16 *ssn)
  2578. {
  2579. struct ath_wiphy *aphy = hw->priv;
  2580. struct ath_softc *sc = aphy->sc;
  2581. int ret = 0;
  2582. switch (action) {
  2583. case IEEE80211_AMPDU_RX_START:
  2584. if (!(sc->sc_flags & SC_OP_RXAGGR))
  2585. ret = -ENOTSUPP;
  2586. break;
  2587. case IEEE80211_AMPDU_RX_STOP:
  2588. break;
  2589. case IEEE80211_AMPDU_TX_START:
  2590. ath_tx_aggr_start(sc, sta, tid, ssn);
  2591. ieee80211_start_tx_ba_cb_irqsafe(hw, sta->addr, tid);
  2592. break;
  2593. case IEEE80211_AMPDU_TX_STOP:
  2594. ath_tx_aggr_stop(sc, sta, tid);
  2595. ieee80211_stop_tx_ba_cb_irqsafe(hw, sta->addr, tid);
  2596. break;
  2597. case IEEE80211_AMPDU_TX_OPERATIONAL:
  2598. ath_tx_aggr_resume(sc, sta, tid);
  2599. break;
  2600. default:
  2601. ath_print(ath9k_hw_common(sc->sc_ah), ATH_DBG_FATAL,
  2602. "Unknown AMPDU action\n");
  2603. }
  2604. return ret;
  2605. }
  2606. static void ath9k_sw_scan_start(struct ieee80211_hw *hw)
  2607. {
  2608. struct ath_wiphy *aphy = hw->priv;
  2609. struct ath_softc *sc = aphy->sc;
  2610. mutex_lock(&sc->mutex);
  2611. if (ath9k_wiphy_scanning(sc)) {
  2612. printk(KERN_DEBUG "ath9k: Two wiphys trying to scan at the "
  2613. "same time\n");
  2614. /*
  2615. * Do not allow the concurrent scanning state for now. This
  2616. * could be improved with scanning control moved into ath9k.
  2617. */
  2618. mutex_unlock(&sc->mutex);
  2619. return;
  2620. }
  2621. aphy->state = ATH_WIPHY_SCAN;
  2622. ath9k_wiphy_pause_all_forced(sc, aphy);
  2623. spin_lock_bh(&sc->ani_lock);
  2624. sc->sc_flags |= SC_OP_SCANNING;
  2625. spin_unlock_bh(&sc->ani_lock);
  2626. mutex_unlock(&sc->mutex);
  2627. }
  2628. static void ath9k_sw_scan_complete(struct ieee80211_hw *hw)
  2629. {
  2630. struct ath_wiphy *aphy = hw->priv;
  2631. struct ath_softc *sc = aphy->sc;
  2632. mutex_lock(&sc->mutex);
  2633. spin_lock_bh(&sc->ani_lock);
  2634. aphy->state = ATH_WIPHY_ACTIVE;
  2635. sc->sc_flags &= ~SC_OP_SCANNING;
  2636. sc->sc_flags |= SC_OP_FULL_RESET;
  2637. spin_unlock_bh(&sc->ani_lock);
  2638. ath_beacon_config(sc, NULL);
  2639. mutex_unlock(&sc->mutex);
  2640. }
  2641. struct ieee80211_ops ath9k_ops = {
  2642. .tx = ath9k_tx,
  2643. .start = ath9k_start,
  2644. .stop = ath9k_stop,
  2645. .add_interface = ath9k_add_interface,
  2646. .remove_interface = ath9k_remove_interface,
  2647. .config = ath9k_config,
  2648. .configure_filter = ath9k_configure_filter,
  2649. .sta_notify = ath9k_sta_notify,
  2650. .conf_tx = ath9k_conf_tx,
  2651. .bss_info_changed = ath9k_bss_info_changed,
  2652. .set_key = ath9k_set_key,
  2653. .get_tsf = ath9k_get_tsf,
  2654. .set_tsf = ath9k_set_tsf,
  2655. .reset_tsf = ath9k_reset_tsf,
  2656. .ampdu_action = ath9k_ampdu_action,
  2657. .sw_scan_start = ath9k_sw_scan_start,
  2658. .sw_scan_complete = ath9k_sw_scan_complete,
  2659. .rfkill_poll = ath9k_rfkill_poll_state,
  2660. };
  2661. static struct {
  2662. u32 version;
  2663. const char * name;
  2664. } ath_mac_bb_names[] = {
  2665. { AR_SREV_VERSION_5416_PCI, "5416" },
  2666. { AR_SREV_VERSION_5416_PCIE, "5418" },
  2667. { AR_SREV_VERSION_9100, "9100" },
  2668. { AR_SREV_VERSION_9160, "9160" },
  2669. { AR_SREV_VERSION_9280, "9280" },
  2670. { AR_SREV_VERSION_9285, "9285" },
  2671. { AR_SREV_VERSION_9287, "9287" }
  2672. };
  2673. static struct {
  2674. u16 version;
  2675. const char * name;
  2676. } ath_rf_names[] = {
  2677. { 0, "5133" },
  2678. { AR_RAD5133_SREV_MAJOR, "5133" },
  2679. { AR_RAD5122_SREV_MAJOR, "5122" },
  2680. { AR_RAD2133_SREV_MAJOR, "2133" },
  2681. { AR_RAD2122_SREV_MAJOR, "2122" }
  2682. };
  2683. /*
  2684. * Return the MAC/BB name. "????" is returned if the MAC/BB is unknown.
  2685. */
  2686. const char *
  2687. ath_mac_bb_name(u32 mac_bb_version)
  2688. {
  2689. int i;
  2690. for (i=0; i<ARRAY_SIZE(ath_mac_bb_names); i++) {
  2691. if (ath_mac_bb_names[i].version == mac_bb_version) {
  2692. return ath_mac_bb_names[i].name;
  2693. }
  2694. }
  2695. return "????";
  2696. }
  2697. /*
  2698. * Return the RF name. "????" is returned if the RF is unknown.
  2699. */
  2700. const char *
  2701. ath_rf_name(u16 rf_version)
  2702. {
  2703. int i;
  2704. for (i=0; i<ARRAY_SIZE(ath_rf_names); i++) {
  2705. if (ath_rf_names[i].version == rf_version) {
  2706. return ath_rf_names[i].name;
  2707. }
  2708. }
  2709. return "????";
  2710. }
  2711. static int __init ath9k_init(void)
  2712. {
  2713. int error;
  2714. /* Register rate control algorithm */
  2715. error = ath_rate_control_register();
  2716. if (error != 0) {
  2717. printk(KERN_ERR
  2718. "ath9k: Unable to register rate control "
  2719. "algorithm: %d\n",
  2720. error);
  2721. goto err_out;
  2722. }
  2723. error = ath9k_debug_create_root();
  2724. if (error) {
  2725. printk(KERN_ERR
  2726. "ath9k: Unable to create debugfs root: %d\n",
  2727. error);
  2728. goto err_rate_unregister;
  2729. }
  2730. error = ath_pci_init();
  2731. if (error < 0) {
  2732. printk(KERN_ERR
  2733. "ath9k: No PCI devices found, driver not installed.\n");
  2734. error = -ENODEV;
  2735. goto err_remove_root;
  2736. }
  2737. error = ath_ahb_init();
  2738. if (error < 0) {
  2739. error = -ENODEV;
  2740. goto err_pci_exit;
  2741. }
  2742. return 0;
  2743. err_pci_exit:
  2744. ath_pci_exit();
  2745. err_remove_root:
  2746. ath9k_debug_remove_root();
  2747. err_rate_unregister:
  2748. ath_rate_control_unregister();
  2749. err_out:
  2750. return error;
  2751. }
  2752. module_init(ath9k_init);
  2753. static void __exit ath9k_exit(void)
  2754. {
  2755. ath_ahb_exit();
  2756. ath_pci_exit();
  2757. ath9k_debug_remove_root();
  2758. ath_rate_control_unregister();
  2759. printk(KERN_INFO "%s: Driver unloaded\n", dev_info);
  2760. }
  2761. module_exit(ath9k_exit);