main.c 84 KB

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