main.c 83 KB

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