base.c 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973
  1. /*-
  2. * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
  3. * Copyright (c) 2004-2005 Atheros Communications, Inc.
  4. * Copyright (c) 2006 Devicescape Software, Inc.
  5. * Copyright (c) 2007 Jiri Slaby <jirislaby@gmail.com>
  6. * Copyright (c) 2007 Luis R. Rodriguez <mcgrof@winlab.rutgers.edu>
  7. *
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer,
  15. * without modification.
  16. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  17. * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
  18. * redistribution must be conditioned upon including a substantially
  19. * similar Disclaimer requirement for further binary redistribution.
  20. * 3. Neither the names of the above-listed copyright holders nor the names
  21. * of any contributors may be used to endorse or promote products derived
  22. * from this software without specific prior written permission.
  23. *
  24. * Alternatively, this software may be distributed under the terms of the
  25. * GNU General Public License ("GPL") version 2 as published by the Free
  26. * Software Foundation.
  27. *
  28. * NO WARRANTY
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  30. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  31. * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
  32. * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
  33. * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
  34. * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  35. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  36. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  37. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  38. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  39. * THE POSSIBILITY OF SUCH DAMAGES.
  40. *
  41. */
  42. #include <linux/module.h>
  43. #include <linux/delay.h>
  44. #include <linux/hardirq.h>
  45. #include <linux/if.h>
  46. #include <linux/io.h>
  47. #include <linux/netdevice.h>
  48. #include <linux/cache.h>
  49. #include <linux/ethtool.h>
  50. #include <linux/uaccess.h>
  51. #include <linux/slab.h>
  52. #include <linux/etherdevice.h>
  53. #include <net/ieee80211_radiotap.h>
  54. #include <asm/unaligned.h>
  55. #include "base.h"
  56. #include "reg.h"
  57. #include "debug.h"
  58. #include "ani.h"
  59. int ath5k_modparam_nohwcrypt;
  60. module_param_named(nohwcrypt, ath5k_modparam_nohwcrypt, bool, S_IRUGO);
  61. MODULE_PARM_DESC(nohwcrypt, "Disable hardware encryption.");
  62. static int modparam_all_channels;
  63. module_param_named(all_channels, modparam_all_channels, bool, S_IRUGO);
  64. MODULE_PARM_DESC(all_channels, "Expose all channels the device can use.");
  65. /* Module info */
  66. MODULE_AUTHOR("Jiri Slaby");
  67. MODULE_AUTHOR("Nick Kossifidis");
  68. MODULE_DESCRIPTION("Support for 5xxx series of Atheros 802.11 wireless LAN cards.");
  69. MODULE_SUPPORTED_DEVICE("Atheros 5xxx WLAN cards");
  70. MODULE_LICENSE("Dual BSD/GPL");
  71. static int ath5k_init(struct ieee80211_hw *hw);
  72. static int ath5k_reset(struct ath5k_softc *sc, struct ieee80211_channel *chan,
  73. bool skip_pcu);
  74. int ath5k_beacon_update(struct ieee80211_hw *hw, struct ieee80211_vif *vif);
  75. void ath5k_beacon_update_timers(struct ath5k_softc *sc, u64 bc_tsf);
  76. /* Known SREVs */
  77. static const struct ath5k_srev_name srev_names[] = {
  78. #ifdef CONFIG_ATHEROS_AR231X
  79. { "5312", AR5K_VERSION_MAC, AR5K_SREV_AR5312_R2 },
  80. { "5312", AR5K_VERSION_MAC, AR5K_SREV_AR5312_R7 },
  81. { "2313", AR5K_VERSION_MAC, AR5K_SREV_AR2313_R8 },
  82. { "2315", AR5K_VERSION_MAC, AR5K_SREV_AR2315_R6 },
  83. { "2315", AR5K_VERSION_MAC, AR5K_SREV_AR2315_R7 },
  84. { "2317", AR5K_VERSION_MAC, AR5K_SREV_AR2317_R1 },
  85. { "2317", AR5K_VERSION_MAC, AR5K_SREV_AR2317_R2 },
  86. #else
  87. { "5210", AR5K_VERSION_MAC, AR5K_SREV_AR5210 },
  88. { "5311", AR5K_VERSION_MAC, AR5K_SREV_AR5311 },
  89. { "5311A", AR5K_VERSION_MAC, AR5K_SREV_AR5311A },
  90. { "5311B", AR5K_VERSION_MAC, AR5K_SREV_AR5311B },
  91. { "5211", AR5K_VERSION_MAC, AR5K_SREV_AR5211 },
  92. { "5212", AR5K_VERSION_MAC, AR5K_SREV_AR5212 },
  93. { "5213", AR5K_VERSION_MAC, AR5K_SREV_AR5213 },
  94. { "5213A", AR5K_VERSION_MAC, AR5K_SREV_AR5213A },
  95. { "2413", AR5K_VERSION_MAC, AR5K_SREV_AR2413 },
  96. { "2414", AR5K_VERSION_MAC, AR5K_SREV_AR2414 },
  97. { "5424", AR5K_VERSION_MAC, AR5K_SREV_AR5424 },
  98. { "5413", AR5K_VERSION_MAC, AR5K_SREV_AR5413 },
  99. { "5414", AR5K_VERSION_MAC, AR5K_SREV_AR5414 },
  100. { "2415", AR5K_VERSION_MAC, AR5K_SREV_AR2415 },
  101. { "5416", AR5K_VERSION_MAC, AR5K_SREV_AR5416 },
  102. { "5418", AR5K_VERSION_MAC, AR5K_SREV_AR5418 },
  103. { "2425", AR5K_VERSION_MAC, AR5K_SREV_AR2425 },
  104. { "2417", AR5K_VERSION_MAC, AR5K_SREV_AR2417 },
  105. #endif
  106. { "xxxxx", AR5K_VERSION_MAC, AR5K_SREV_UNKNOWN },
  107. { "5110", AR5K_VERSION_RAD, AR5K_SREV_RAD_5110 },
  108. { "5111", AR5K_VERSION_RAD, AR5K_SREV_RAD_5111 },
  109. { "5111A", AR5K_VERSION_RAD, AR5K_SREV_RAD_5111A },
  110. { "2111", AR5K_VERSION_RAD, AR5K_SREV_RAD_2111 },
  111. { "5112", AR5K_VERSION_RAD, AR5K_SREV_RAD_5112 },
  112. { "5112A", AR5K_VERSION_RAD, AR5K_SREV_RAD_5112A },
  113. { "5112B", AR5K_VERSION_RAD, AR5K_SREV_RAD_5112B },
  114. { "2112", AR5K_VERSION_RAD, AR5K_SREV_RAD_2112 },
  115. { "2112A", AR5K_VERSION_RAD, AR5K_SREV_RAD_2112A },
  116. { "2112B", AR5K_VERSION_RAD, AR5K_SREV_RAD_2112B },
  117. { "2413", AR5K_VERSION_RAD, AR5K_SREV_RAD_2413 },
  118. { "5413", AR5K_VERSION_RAD, AR5K_SREV_RAD_5413 },
  119. { "5424", AR5K_VERSION_RAD, AR5K_SREV_RAD_5424 },
  120. { "5133", AR5K_VERSION_RAD, AR5K_SREV_RAD_5133 },
  121. #ifdef CONFIG_ATHEROS_AR231X
  122. { "2316", AR5K_VERSION_RAD, AR5K_SREV_RAD_2316 },
  123. { "2317", AR5K_VERSION_RAD, AR5K_SREV_RAD_2317 },
  124. #endif
  125. { "xxxxx", AR5K_VERSION_RAD, AR5K_SREV_UNKNOWN },
  126. };
  127. static const struct ieee80211_rate ath5k_rates[] = {
  128. { .bitrate = 10,
  129. .hw_value = ATH5K_RATE_CODE_1M, },
  130. { .bitrate = 20,
  131. .hw_value = ATH5K_RATE_CODE_2M,
  132. .hw_value_short = ATH5K_RATE_CODE_2M | AR5K_SET_SHORT_PREAMBLE,
  133. .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  134. { .bitrate = 55,
  135. .hw_value = ATH5K_RATE_CODE_5_5M,
  136. .hw_value_short = ATH5K_RATE_CODE_5_5M | AR5K_SET_SHORT_PREAMBLE,
  137. .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  138. { .bitrate = 110,
  139. .hw_value = ATH5K_RATE_CODE_11M,
  140. .hw_value_short = ATH5K_RATE_CODE_11M | AR5K_SET_SHORT_PREAMBLE,
  141. .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  142. { .bitrate = 60,
  143. .hw_value = ATH5K_RATE_CODE_6M,
  144. .flags = 0 },
  145. { .bitrate = 90,
  146. .hw_value = ATH5K_RATE_CODE_9M,
  147. .flags = 0 },
  148. { .bitrate = 120,
  149. .hw_value = ATH5K_RATE_CODE_12M,
  150. .flags = 0 },
  151. { .bitrate = 180,
  152. .hw_value = ATH5K_RATE_CODE_18M,
  153. .flags = 0 },
  154. { .bitrate = 240,
  155. .hw_value = ATH5K_RATE_CODE_24M,
  156. .flags = 0 },
  157. { .bitrate = 360,
  158. .hw_value = ATH5K_RATE_CODE_36M,
  159. .flags = 0 },
  160. { .bitrate = 480,
  161. .hw_value = ATH5K_RATE_CODE_48M,
  162. .flags = 0 },
  163. { .bitrate = 540,
  164. .hw_value = ATH5K_RATE_CODE_54M,
  165. .flags = 0 },
  166. /* XR missing */
  167. };
  168. static inline u64 ath5k_extend_tsf(struct ath5k_hw *ah, u32 rstamp)
  169. {
  170. u64 tsf = ath5k_hw_get_tsf64(ah);
  171. if ((tsf & 0x7fff) < rstamp)
  172. tsf -= 0x8000;
  173. return (tsf & ~0x7fff) | rstamp;
  174. }
  175. const char *
  176. ath5k_chip_name(enum ath5k_srev_type type, u_int16_t val)
  177. {
  178. const char *name = "xxxxx";
  179. unsigned int i;
  180. for (i = 0; i < ARRAY_SIZE(srev_names); i++) {
  181. if (srev_names[i].sr_type != type)
  182. continue;
  183. if ((val & 0xf0) == srev_names[i].sr_val)
  184. name = srev_names[i].sr_name;
  185. if ((val & 0xff) == srev_names[i].sr_val) {
  186. name = srev_names[i].sr_name;
  187. break;
  188. }
  189. }
  190. return name;
  191. }
  192. static unsigned int ath5k_ioread32(void *hw_priv, u32 reg_offset)
  193. {
  194. struct ath5k_hw *ah = (struct ath5k_hw *) hw_priv;
  195. return ath5k_hw_reg_read(ah, reg_offset);
  196. }
  197. static void ath5k_iowrite32(void *hw_priv, u32 val, u32 reg_offset)
  198. {
  199. struct ath5k_hw *ah = (struct ath5k_hw *) hw_priv;
  200. ath5k_hw_reg_write(ah, val, reg_offset);
  201. }
  202. static const struct ath_ops ath5k_common_ops = {
  203. .read = ath5k_ioread32,
  204. .write = ath5k_iowrite32,
  205. };
  206. /***********************\
  207. * Driver Initialization *
  208. \***********************/
  209. static int ath5k_reg_notifier(struct wiphy *wiphy, struct regulatory_request *request)
  210. {
  211. struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
  212. struct ath5k_softc *sc = hw->priv;
  213. struct ath_regulatory *regulatory = ath5k_hw_regulatory(sc->ah);
  214. return ath_reg_notifier_apply(wiphy, request, regulatory);
  215. }
  216. /********************\
  217. * Channel/mode setup *
  218. \********************/
  219. /*
  220. * Convert IEEE channel number to MHz frequency.
  221. */
  222. static inline short
  223. ath5k_ieee2mhz(short chan)
  224. {
  225. if (chan <= 14 || chan >= 27)
  226. return ieee80211chan2mhz(chan);
  227. else
  228. return 2212 + chan * 20;
  229. }
  230. /*
  231. * Returns true for the channel numbers used without all_channels modparam.
  232. */
  233. static bool ath5k_is_standard_channel(short chan)
  234. {
  235. return ((chan <= 14) ||
  236. /* UNII 1,2 */
  237. ((chan & 3) == 0 && chan >= 36 && chan <= 64) ||
  238. /* midband */
  239. ((chan & 3) == 0 && chan >= 100 && chan <= 140) ||
  240. /* UNII-3 */
  241. ((chan & 3) == 1 && chan >= 149 && chan <= 165));
  242. }
  243. static unsigned int
  244. ath5k_copy_channels(struct ath5k_hw *ah,
  245. struct ieee80211_channel *channels,
  246. unsigned int mode,
  247. unsigned int max)
  248. {
  249. unsigned int i, count, size, chfreq, freq, ch;
  250. if (!test_bit(mode, ah->ah_modes))
  251. return 0;
  252. switch (mode) {
  253. case AR5K_MODE_11A:
  254. /* 1..220, but 2GHz frequencies are filtered by check_channel */
  255. size = 220 ;
  256. chfreq = CHANNEL_5GHZ;
  257. break;
  258. case AR5K_MODE_11B:
  259. case AR5K_MODE_11G:
  260. size = 26;
  261. chfreq = CHANNEL_2GHZ;
  262. break;
  263. default:
  264. ATH5K_WARN(ah->ah_sc, "bad mode, not copying channels\n");
  265. return 0;
  266. }
  267. for (i = 0, count = 0; i < size && max > 0; i++) {
  268. ch = i + 1 ;
  269. freq = ath5k_ieee2mhz(ch);
  270. /* Check if channel is supported by the chipset */
  271. if (!ath5k_channel_ok(ah, freq, chfreq))
  272. continue;
  273. if (!modparam_all_channels && !ath5k_is_standard_channel(ch))
  274. continue;
  275. /* Write channel info and increment counter */
  276. channels[count].center_freq = freq;
  277. channels[count].band = (chfreq == CHANNEL_2GHZ) ?
  278. IEEE80211_BAND_2GHZ : IEEE80211_BAND_5GHZ;
  279. switch (mode) {
  280. case AR5K_MODE_11A:
  281. case AR5K_MODE_11G:
  282. channels[count].hw_value = chfreq | CHANNEL_OFDM;
  283. break;
  284. case AR5K_MODE_11B:
  285. channels[count].hw_value = CHANNEL_B;
  286. }
  287. count++;
  288. max--;
  289. }
  290. return count;
  291. }
  292. static void
  293. ath5k_setup_rate_idx(struct ath5k_softc *sc, struct ieee80211_supported_band *b)
  294. {
  295. u8 i;
  296. for (i = 0; i < AR5K_MAX_RATES; i++)
  297. sc->rate_idx[b->band][i] = -1;
  298. for (i = 0; i < b->n_bitrates; i++) {
  299. sc->rate_idx[b->band][b->bitrates[i].hw_value] = i;
  300. if (b->bitrates[i].hw_value_short)
  301. sc->rate_idx[b->band][b->bitrates[i].hw_value_short] = i;
  302. }
  303. }
  304. static int
  305. ath5k_setup_bands(struct ieee80211_hw *hw)
  306. {
  307. struct ath5k_softc *sc = hw->priv;
  308. struct ath5k_hw *ah = sc->ah;
  309. struct ieee80211_supported_band *sband;
  310. int max_c, count_c = 0;
  311. int i;
  312. BUILD_BUG_ON(ARRAY_SIZE(sc->sbands) < IEEE80211_NUM_BANDS);
  313. max_c = ARRAY_SIZE(sc->channels);
  314. /* 2GHz band */
  315. sband = &sc->sbands[IEEE80211_BAND_2GHZ];
  316. sband->band = IEEE80211_BAND_2GHZ;
  317. sband->bitrates = &sc->rates[IEEE80211_BAND_2GHZ][0];
  318. if (test_bit(AR5K_MODE_11G, sc->ah->ah_capabilities.cap_mode)) {
  319. /* G mode */
  320. memcpy(sband->bitrates, &ath5k_rates[0],
  321. sizeof(struct ieee80211_rate) * 12);
  322. sband->n_bitrates = 12;
  323. sband->channels = sc->channels;
  324. sband->n_channels = ath5k_copy_channels(ah, sband->channels,
  325. AR5K_MODE_11G, max_c);
  326. hw->wiphy->bands[IEEE80211_BAND_2GHZ] = sband;
  327. count_c = sband->n_channels;
  328. max_c -= count_c;
  329. } else if (test_bit(AR5K_MODE_11B, sc->ah->ah_capabilities.cap_mode)) {
  330. /* B mode */
  331. memcpy(sband->bitrates, &ath5k_rates[0],
  332. sizeof(struct ieee80211_rate) * 4);
  333. sband->n_bitrates = 4;
  334. /* 5211 only supports B rates and uses 4bit rate codes
  335. * (e.g normally we have 0x1B for 1M, but on 5211 we have 0x0B)
  336. * fix them up here:
  337. */
  338. if (ah->ah_version == AR5K_AR5211) {
  339. for (i = 0; i < 4; i++) {
  340. sband->bitrates[i].hw_value =
  341. sband->bitrates[i].hw_value & 0xF;
  342. sband->bitrates[i].hw_value_short =
  343. sband->bitrates[i].hw_value_short & 0xF;
  344. }
  345. }
  346. sband->channels = sc->channels;
  347. sband->n_channels = ath5k_copy_channels(ah, sband->channels,
  348. AR5K_MODE_11B, max_c);
  349. hw->wiphy->bands[IEEE80211_BAND_2GHZ] = sband;
  350. count_c = sband->n_channels;
  351. max_c -= count_c;
  352. }
  353. ath5k_setup_rate_idx(sc, sband);
  354. /* 5GHz band, A mode */
  355. if (test_bit(AR5K_MODE_11A, sc->ah->ah_capabilities.cap_mode)) {
  356. sband = &sc->sbands[IEEE80211_BAND_5GHZ];
  357. sband->band = IEEE80211_BAND_5GHZ;
  358. sband->bitrates = &sc->rates[IEEE80211_BAND_5GHZ][0];
  359. memcpy(sband->bitrates, &ath5k_rates[4],
  360. sizeof(struct ieee80211_rate) * 8);
  361. sband->n_bitrates = 8;
  362. sband->channels = &sc->channels[count_c];
  363. sband->n_channels = ath5k_copy_channels(ah, sband->channels,
  364. AR5K_MODE_11A, max_c);
  365. hw->wiphy->bands[IEEE80211_BAND_5GHZ] = sband;
  366. }
  367. ath5k_setup_rate_idx(sc, sband);
  368. ath5k_debug_dump_bands(sc);
  369. return 0;
  370. }
  371. /*
  372. * Set/change channels. We always reset the chip.
  373. * To accomplish this we must first cleanup any pending DMA,
  374. * then restart stuff after a la ath5k_init.
  375. *
  376. * Called with sc->lock.
  377. */
  378. int
  379. ath5k_chan_set(struct ath5k_softc *sc, struct ieee80211_channel *chan)
  380. {
  381. ATH5K_DBG(sc, ATH5K_DEBUG_RESET,
  382. "channel set, resetting (%u -> %u MHz)\n",
  383. sc->curchan->center_freq, chan->center_freq);
  384. /*
  385. * To switch channels clear any pending DMA operations;
  386. * wait long enough for the RX fifo to drain, reset the
  387. * hardware at the new frequency, and then re-enable
  388. * the relevant bits of the h/w.
  389. */
  390. return ath5k_reset(sc, chan, true);
  391. }
  392. static void
  393. ath5k_setcurmode(struct ath5k_softc *sc, unsigned int mode)
  394. {
  395. sc->curmode = mode;
  396. if (mode == AR5K_MODE_11A) {
  397. sc->curband = &sc->sbands[IEEE80211_BAND_5GHZ];
  398. } else {
  399. sc->curband = &sc->sbands[IEEE80211_BAND_2GHZ];
  400. }
  401. }
  402. struct ath_vif_iter_data {
  403. const u8 *hw_macaddr;
  404. u8 mask[ETH_ALEN];
  405. u8 active_mac[ETH_ALEN]; /* first active MAC */
  406. bool need_set_hw_addr;
  407. bool found_active;
  408. bool any_assoc;
  409. enum nl80211_iftype opmode;
  410. };
  411. static void ath_vif_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
  412. {
  413. struct ath_vif_iter_data *iter_data = data;
  414. int i;
  415. struct ath5k_vif *avf = (void *)vif->drv_priv;
  416. if (iter_data->hw_macaddr)
  417. for (i = 0; i < ETH_ALEN; i++)
  418. iter_data->mask[i] &=
  419. ~(iter_data->hw_macaddr[i] ^ mac[i]);
  420. if (!iter_data->found_active) {
  421. iter_data->found_active = true;
  422. memcpy(iter_data->active_mac, mac, ETH_ALEN);
  423. }
  424. if (iter_data->need_set_hw_addr && iter_data->hw_macaddr)
  425. if (compare_ether_addr(iter_data->hw_macaddr, mac) == 0)
  426. iter_data->need_set_hw_addr = false;
  427. if (!iter_data->any_assoc) {
  428. if (avf->assoc)
  429. iter_data->any_assoc = true;
  430. }
  431. /* Calculate combined mode - when APs are active, operate in AP mode.
  432. * Otherwise use the mode of the new interface. This can currently
  433. * only deal with combinations of APs and STAs. Only one ad-hoc
  434. * interfaces is allowed.
  435. */
  436. if (avf->opmode == NL80211_IFTYPE_AP)
  437. iter_data->opmode = NL80211_IFTYPE_AP;
  438. else
  439. if (iter_data->opmode == NL80211_IFTYPE_UNSPECIFIED)
  440. iter_data->opmode = avf->opmode;
  441. }
  442. void
  443. ath5k_update_bssid_mask_and_opmode(struct ath5k_softc *sc,
  444. struct ieee80211_vif *vif)
  445. {
  446. struct ath_common *common = ath5k_hw_common(sc->ah);
  447. struct ath_vif_iter_data iter_data;
  448. /*
  449. * Use the hardware MAC address as reference, the hardware uses it
  450. * together with the BSSID mask when matching addresses.
  451. */
  452. iter_data.hw_macaddr = common->macaddr;
  453. memset(&iter_data.mask, 0xff, ETH_ALEN);
  454. iter_data.found_active = false;
  455. iter_data.need_set_hw_addr = true;
  456. iter_data.opmode = NL80211_IFTYPE_UNSPECIFIED;
  457. if (vif)
  458. ath_vif_iter(&iter_data, vif->addr, vif);
  459. /* Get list of all active MAC addresses */
  460. ieee80211_iterate_active_interfaces_atomic(sc->hw, ath_vif_iter,
  461. &iter_data);
  462. memcpy(sc->bssidmask, iter_data.mask, ETH_ALEN);
  463. sc->opmode = iter_data.opmode;
  464. if (sc->opmode == NL80211_IFTYPE_UNSPECIFIED)
  465. /* Nothing active, default to station mode */
  466. sc->opmode = NL80211_IFTYPE_STATION;
  467. ath5k_hw_set_opmode(sc->ah, sc->opmode);
  468. ATH5K_DBG(sc, ATH5K_DEBUG_MODE, "mode setup opmode %d (%s)\n",
  469. sc->opmode, ath_opmode_to_string(sc->opmode));
  470. if (iter_data.need_set_hw_addr && iter_data.found_active)
  471. ath5k_hw_set_lladdr(sc->ah, iter_data.active_mac);
  472. if (ath5k_hw_hasbssidmask(sc->ah))
  473. ath5k_hw_set_bssid_mask(sc->ah, sc->bssidmask);
  474. }
  475. void
  476. ath5k_mode_setup(struct ath5k_softc *sc, struct ieee80211_vif *vif)
  477. {
  478. struct ath5k_hw *ah = sc->ah;
  479. u32 rfilt;
  480. /* configure rx filter */
  481. rfilt = sc->filter_flags;
  482. ath5k_hw_set_rx_filter(ah, rfilt);
  483. ATH5K_DBG(sc, ATH5K_DEBUG_MODE, "RX filter 0x%x\n", rfilt);
  484. ath5k_update_bssid_mask_and_opmode(sc, vif);
  485. }
  486. static inline int
  487. ath5k_hw_to_driver_rix(struct ath5k_softc *sc, int hw_rix)
  488. {
  489. int rix;
  490. /* return base rate on errors */
  491. if (WARN(hw_rix < 0 || hw_rix >= AR5K_MAX_RATES,
  492. "hw_rix out of bounds: %x\n", hw_rix))
  493. return 0;
  494. rix = sc->rate_idx[sc->curband->band][hw_rix];
  495. if (WARN(rix < 0, "invalid hw_rix: %x\n", hw_rix))
  496. rix = 0;
  497. return rix;
  498. }
  499. /***************\
  500. * Buffers setup *
  501. \***************/
  502. static
  503. struct sk_buff *ath5k_rx_skb_alloc(struct ath5k_softc *sc, dma_addr_t *skb_addr)
  504. {
  505. struct ath_common *common = ath5k_hw_common(sc->ah);
  506. struct sk_buff *skb;
  507. /*
  508. * Allocate buffer with headroom_needed space for the
  509. * fake physical layer header at the start.
  510. */
  511. skb = ath_rxbuf_alloc(common,
  512. common->rx_bufsize,
  513. GFP_ATOMIC);
  514. if (!skb) {
  515. ATH5K_ERR(sc, "can't alloc skbuff of size %u\n",
  516. common->rx_bufsize);
  517. return NULL;
  518. }
  519. *skb_addr = dma_map_single(sc->dev,
  520. skb->data, common->rx_bufsize,
  521. DMA_FROM_DEVICE);
  522. if (unlikely(dma_mapping_error(sc->dev, *skb_addr))) {
  523. ATH5K_ERR(sc, "%s: DMA mapping failed\n", __func__);
  524. dev_kfree_skb(skb);
  525. return NULL;
  526. }
  527. return skb;
  528. }
  529. static int
  530. ath5k_rxbuf_setup(struct ath5k_softc *sc, struct ath5k_buf *bf)
  531. {
  532. struct ath5k_hw *ah = sc->ah;
  533. struct sk_buff *skb = bf->skb;
  534. struct ath5k_desc *ds;
  535. int ret;
  536. if (!skb) {
  537. skb = ath5k_rx_skb_alloc(sc, &bf->skbaddr);
  538. if (!skb)
  539. return -ENOMEM;
  540. bf->skb = skb;
  541. }
  542. /*
  543. * Setup descriptors. For receive we always terminate
  544. * the descriptor list with a self-linked entry so we'll
  545. * not get overrun under high load (as can happen with a
  546. * 5212 when ANI processing enables PHY error frames).
  547. *
  548. * To ensure the last descriptor is self-linked we create
  549. * each descriptor as self-linked and add it to the end. As
  550. * each additional descriptor is added the previous self-linked
  551. * entry is "fixed" naturally. This should be safe even
  552. * if DMA is happening. When processing RX interrupts we
  553. * never remove/process the last, self-linked, entry on the
  554. * descriptor list. This ensures the hardware always has
  555. * someplace to write a new frame.
  556. */
  557. ds = bf->desc;
  558. ds->ds_link = bf->daddr; /* link to self */
  559. ds->ds_data = bf->skbaddr;
  560. ret = ath5k_hw_setup_rx_desc(ah, ds, ah->common.rx_bufsize, 0);
  561. if (ret) {
  562. ATH5K_ERR(sc, "%s: could not setup RX desc\n", __func__);
  563. return ret;
  564. }
  565. if (sc->rxlink != NULL)
  566. *sc->rxlink = bf->daddr;
  567. sc->rxlink = &ds->ds_link;
  568. return 0;
  569. }
  570. static enum ath5k_pkt_type get_hw_packet_type(struct sk_buff *skb)
  571. {
  572. struct ieee80211_hdr *hdr;
  573. enum ath5k_pkt_type htype;
  574. __le16 fc;
  575. hdr = (struct ieee80211_hdr *)skb->data;
  576. fc = hdr->frame_control;
  577. if (ieee80211_is_beacon(fc))
  578. htype = AR5K_PKT_TYPE_BEACON;
  579. else if (ieee80211_is_probe_resp(fc))
  580. htype = AR5K_PKT_TYPE_PROBE_RESP;
  581. else if (ieee80211_is_atim(fc))
  582. htype = AR5K_PKT_TYPE_ATIM;
  583. else if (ieee80211_is_pspoll(fc))
  584. htype = AR5K_PKT_TYPE_PSPOLL;
  585. else
  586. htype = AR5K_PKT_TYPE_NORMAL;
  587. return htype;
  588. }
  589. static int
  590. ath5k_txbuf_setup(struct ath5k_softc *sc, struct ath5k_buf *bf,
  591. struct ath5k_txq *txq, int padsize)
  592. {
  593. struct ath5k_hw *ah = sc->ah;
  594. struct ath5k_desc *ds = bf->desc;
  595. struct sk_buff *skb = bf->skb;
  596. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  597. unsigned int pktlen, flags, keyidx = AR5K_TXKEYIX_INVALID;
  598. struct ieee80211_rate *rate;
  599. unsigned int mrr_rate[3], mrr_tries[3];
  600. int i, ret;
  601. u16 hw_rate;
  602. u16 cts_rate = 0;
  603. u16 duration = 0;
  604. u8 rc_flags;
  605. flags = AR5K_TXDESC_INTREQ | AR5K_TXDESC_CLRDMASK;
  606. /* XXX endianness */
  607. bf->skbaddr = dma_map_single(sc->dev, skb->data, skb->len,
  608. DMA_TO_DEVICE);
  609. rate = ieee80211_get_tx_rate(sc->hw, info);
  610. if (!rate) {
  611. ret = -EINVAL;
  612. goto err_unmap;
  613. }
  614. if (info->flags & IEEE80211_TX_CTL_NO_ACK)
  615. flags |= AR5K_TXDESC_NOACK;
  616. rc_flags = info->control.rates[0].flags;
  617. hw_rate = (rc_flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE) ?
  618. rate->hw_value_short : rate->hw_value;
  619. pktlen = skb->len;
  620. /* FIXME: If we are in g mode and rate is a CCK rate
  621. * subtract ah->ah_txpower.txp_cck_ofdm_pwr_delta
  622. * from tx power (value is in dB units already) */
  623. if (info->control.hw_key) {
  624. keyidx = info->control.hw_key->hw_key_idx;
  625. pktlen += info->control.hw_key->icv_len;
  626. }
  627. if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
  628. flags |= AR5K_TXDESC_RTSENA;
  629. cts_rate = ieee80211_get_rts_cts_rate(sc->hw, info)->hw_value;
  630. duration = le16_to_cpu(ieee80211_rts_duration(sc->hw,
  631. info->control.vif, pktlen, info));
  632. }
  633. if (rc_flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
  634. flags |= AR5K_TXDESC_CTSENA;
  635. cts_rate = ieee80211_get_rts_cts_rate(sc->hw, info)->hw_value;
  636. duration = le16_to_cpu(ieee80211_ctstoself_duration(sc->hw,
  637. info->control.vif, pktlen, info));
  638. }
  639. ret = ah->ah_setup_tx_desc(ah, ds, pktlen,
  640. ieee80211_get_hdrlen_from_skb(skb), padsize,
  641. get_hw_packet_type(skb),
  642. (sc->power_level * 2),
  643. hw_rate,
  644. info->control.rates[0].count, keyidx, ah->ah_tx_ant, flags,
  645. cts_rate, duration);
  646. if (ret)
  647. goto err_unmap;
  648. memset(mrr_rate, 0, sizeof(mrr_rate));
  649. memset(mrr_tries, 0, sizeof(mrr_tries));
  650. for (i = 0; i < 3; i++) {
  651. rate = ieee80211_get_alt_retry_rate(sc->hw, info, i);
  652. if (!rate)
  653. break;
  654. mrr_rate[i] = rate->hw_value;
  655. mrr_tries[i] = info->control.rates[i + 1].count;
  656. }
  657. ath5k_hw_setup_mrr_tx_desc(ah, ds,
  658. mrr_rate[0], mrr_tries[0],
  659. mrr_rate[1], mrr_tries[1],
  660. mrr_rate[2], mrr_tries[2]);
  661. ds->ds_link = 0;
  662. ds->ds_data = bf->skbaddr;
  663. spin_lock_bh(&txq->lock);
  664. list_add_tail(&bf->list, &txq->q);
  665. txq->txq_len++;
  666. if (txq->link == NULL) /* is this first packet? */
  667. ath5k_hw_set_txdp(ah, txq->qnum, bf->daddr);
  668. else /* no, so only link it */
  669. *txq->link = bf->daddr;
  670. txq->link = &ds->ds_link;
  671. ath5k_hw_start_tx_dma(ah, txq->qnum);
  672. mmiowb();
  673. spin_unlock_bh(&txq->lock);
  674. return 0;
  675. err_unmap:
  676. dma_unmap_single(sc->dev, bf->skbaddr, skb->len, DMA_TO_DEVICE);
  677. return ret;
  678. }
  679. /*******************\
  680. * Descriptors setup *
  681. \*******************/
  682. static int
  683. ath5k_desc_alloc(struct ath5k_softc *sc)
  684. {
  685. struct ath5k_desc *ds;
  686. struct ath5k_buf *bf;
  687. dma_addr_t da;
  688. unsigned int i;
  689. int ret;
  690. /* allocate descriptors */
  691. sc->desc_len = sizeof(struct ath5k_desc) *
  692. (ATH_TXBUF + ATH_RXBUF + ATH_BCBUF + 1);
  693. sc->desc = dma_alloc_coherent(sc->dev, sc->desc_len,
  694. &sc->desc_daddr, GFP_KERNEL);
  695. if (sc->desc == NULL) {
  696. ATH5K_ERR(sc, "can't allocate descriptors\n");
  697. ret = -ENOMEM;
  698. goto err;
  699. }
  700. ds = sc->desc;
  701. da = sc->desc_daddr;
  702. ATH5K_DBG(sc, ATH5K_DEBUG_ANY, "DMA map: %p (%zu) -> %llx\n",
  703. ds, sc->desc_len, (unsigned long long)sc->desc_daddr);
  704. bf = kcalloc(1 + ATH_TXBUF + ATH_RXBUF + ATH_BCBUF,
  705. sizeof(struct ath5k_buf), GFP_KERNEL);
  706. if (bf == NULL) {
  707. ATH5K_ERR(sc, "can't allocate bufptr\n");
  708. ret = -ENOMEM;
  709. goto err_free;
  710. }
  711. sc->bufptr = bf;
  712. INIT_LIST_HEAD(&sc->rxbuf);
  713. for (i = 0; i < ATH_RXBUF; i++, bf++, ds++, da += sizeof(*ds)) {
  714. bf->desc = ds;
  715. bf->daddr = da;
  716. list_add_tail(&bf->list, &sc->rxbuf);
  717. }
  718. INIT_LIST_HEAD(&sc->txbuf);
  719. sc->txbuf_len = ATH_TXBUF;
  720. for (i = 0; i < ATH_TXBUF; i++, bf++, ds++,
  721. da += sizeof(*ds)) {
  722. bf->desc = ds;
  723. bf->daddr = da;
  724. list_add_tail(&bf->list, &sc->txbuf);
  725. }
  726. /* beacon buffers */
  727. INIT_LIST_HEAD(&sc->bcbuf);
  728. for (i = 0; i < ATH_BCBUF; i++, bf++, ds++, da += sizeof(*ds)) {
  729. bf->desc = ds;
  730. bf->daddr = da;
  731. list_add_tail(&bf->list, &sc->bcbuf);
  732. }
  733. return 0;
  734. err_free:
  735. dma_free_coherent(sc->dev, sc->desc_len, sc->desc, sc->desc_daddr);
  736. err:
  737. sc->desc = NULL;
  738. return ret;
  739. }
  740. void
  741. ath5k_txbuf_free_skb(struct ath5k_softc *sc, struct ath5k_buf *bf)
  742. {
  743. BUG_ON(!bf);
  744. if (!bf->skb)
  745. return;
  746. dma_unmap_single(sc->dev, bf->skbaddr, bf->skb->len,
  747. DMA_TO_DEVICE);
  748. dev_kfree_skb_any(bf->skb);
  749. bf->skb = NULL;
  750. bf->skbaddr = 0;
  751. bf->desc->ds_data = 0;
  752. }
  753. void
  754. ath5k_rxbuf_free_skb(struct ath5k_softc *sc, struct ath5k_buf *bf)
  755. {
  756. struct ath5k_hw *ah = sc->ah;
  757. struct ath_common *common = ath5k_hw_common(ah);
  758. BUG_ON(!bf);
  759. if (!bf->skb)
  760. return;
  761. dma_unmap_single(sc->dev, bf->skbaddr, common->rx_bufsize,
  762. DMA_FROM_DEVICE);
  763. dev_kfree_skb_any(bf->skb);
  764. bf->skb = NULL;
  765. bf->skbaddr = 0;
  766. bf->desc->ds_data = 0;
  767. }
  768. static void
  769. ath5k_desc_free(struct ath5k_softc *sc)
  770. {
  771. struct ath5k_buf *bf;
  772. list_for_each_entry(bf, &sc->txbuf, list)
  773. ath5k_txbuf_free_skb(sc, bf);
  774. list_for_each_entry(bf, &sc->rxbuf, list)
  775. ath5k_rxbuf_free_skb(sc, bf);
  776. list_for_each_entry(bf, &sc->bcbuf, list)
  777. ath5k_txbuf_free_skb(sc, bf);
  778. /* Free memory associated with all descriptors */
  779. dma_free_coherent(sc->dev, sc->desc_len, sc->desc, sc->desc_daddr);
  780. sc->desc = NULL;
  781. sc->desc_daddr = 0;
  782. kfree(sc->bufptr);
  783. sc->bufptr = NULL;
  784. }
  785. /**************\
  786. * Queues setup *
  787. \**************/
  788. static struct ath5k_txq *
  789. ath5k_txq_setup(struct ath5k_softc *sc,
  790. int qtype, int subtype)
  791. {
  792. struct ath5k_hw *ah = sc->ah;
  793. struct ath5k_txq *txq;
  794. struct ath5k_txq_info qi = {
  795. .tqi_subtype = subtype,
  796. /* XXX: default values not correct for B and XR channels,
  797. * but who cares? */
  798. .tqi_aifs = AR5K_TUNE_AIFS,
  799. .tqi_cw_min = AR5K_TUNE_CWMIN,
  800. .tqi_cw_max = AR5K_TUNE_CWMAX
  801. };
  802. int qnum;
  803. /*
  804. * Enable interrupts only for EOL and DESC conditions.
  805. * We mark tx descriptors to receive a DESC interrupt
  806. * when a tx queue gets deep; otherwise we wait for the
  807. * EOL to reap descriptors. Note that this is done to
  808. * reduce interrupt load and this only defers reaping
  809. * descriptors, never transmitting frames. Aside from
  810. * reducing interrupts this also permits more concurrency.
  811. * The only potential downside is if the tx queue backs
  812. * up in which case the top half of the kernel may backup
  813. * due to a lack of tx descriptors.
  814. */
  815. qi.tqi_flags = AR5K_TXQ_FLAG_TXEOLINT_ENABLE |
  816. AR5K_TXQ_FLAG_TXDESCINT_ENABLE;
  817. qnum = ath5k_hw_setup_tx_queue(ah, qtype, &qi);
  818. if (qnum < 0) {
  819. /*
  820. * NB: don't print a message, this happens
  821. * normally on parts with too few tx queues
  822. */
  823. return ERR_PTR(qnum);
  824. }
  825. if (qnum >= ARRAY_SIZE(sc->txqs)) {
  826. ATH5K_ERR(sc, "hw qnum %u out of range, max %tu!\n",
  827. qnum, ARRAY_SIZE(sc->txqs));
  828. ath5k_hw_release_tx_queue(ah, qnum);
  829. return ERR_PTR(-EINVAL);
  830. }
  831. txq = &sc->txqs[qnum];
  832. if (!txq->setup) {
  833. txq->qnum = qnum;
  834. txq->link = NULL;
  835. INIT_LIST_HEAD(&txq->q);
  836. spin_lock_init(&txq->lock);
  837. txq->setup = true;
  838. txq->txq_len = 0;
  839. txq->txq_poll_mark = false;
  840. txq->txq_stuck = 0;
  841. }
  842. return &sc->txqs[qnum];
  843. }
  844. static int
  845. ath5k_beaconq_setup(struct ath5k_hw *ah)
  846. {
  847. struct ath5k_txq_info qi = {
  848. /* XXX: default values not correct for B and XR channels,
  849. * but who cares? */
  850. .tqi_aifs = AR5K_TUNE_AIFS,
  851. .tqi_cw_min = AR5K_TUNE_CWMIN,
  852. .tqi_cw_max = AR5K_TUNE_CWMAX,
  853. /* NB: for dynamic turbo, don't enable any other interrupts */
  854. .tqi_flags = AR5K_TXQ_FLAG_TXDESCINT_ENABLE
  855. };
  856. return ath5k_hw_setup_tx_queue(ah, AR5K_TX_QUEUE_BEACON, &qi);
  857. }
  858. static int
  859. ath5k_beaconq_config(struct ath5k_softc *sc)
  860. {
  861. struct ath5k_hw *ah = sc->ah;
  862. struct ath5k_txq_info qi;
  863. int ret;
  864. ret = ath5k_hw_get_tx_queueprops(ah, sc->bhalq, &qi);
  865. if (ret)
  866. goto err;
  867. if (sc->opmode == NL80211_IFTYPE_AP ||
  868. sc->opmode == NL80211_IFTYPE_MESH_POINT) {
  869. /*
  870. * Always burst out beacon and CAB traffic
  871. * (aifs = cwmin = cwmax = 0)
  872. */
  873. qi.tqi_aifs = 0;
  874. qi.tqi_cw_min = 0;
  875. qi.tqi_cw_max = 0;
  876. } else if (sc->opmode == NL80211_IFTYPE_ADHOC) {
  877. /*
  878. * Adhoc mode; backoff between 0 and (2 * cw_min).
  879. */
  880. qi.tqi_aifs = 0;
  881. qi.tqi_cw_min = 0;
  882. qi.tqi_cw_max = 2 * AR5K_TUNE_CWMIN;
  883. }
  884. ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
  885. "beacon queueprops tqi_aifs:%d tqi_cw_min:%d tqi_cw_max:%d\n",
  886. qi.tqi_aifs, qi.tqi_cw_min, qi.tqi_cw_max);
  887. ret = ath5k_hw_set_tx_queueprops(ah, sc->bhalq, &qi);
  888. if (ret) {
  889. ATH5K_ERR(sc, "%s: unable to update parameters for beacon "
  890. "hardware queue!\n", __func__);
  891. goto err;
  892. }
  893. ret = ath5k_hw_reset_tx_queue(ah, sc->bhalq); /* push to h/w */
  894. if (ret)
  895. goto err;
  896. /* reconfigure cabq with ready time to 80% of beacon_interval */
  897. ret = ath5k_hw_get_tx_queueprops(ah, AR5K_TX_QUEUE_ID_CAB, &qi);
  898. if (ret)
  899. goto err;
  900. qi.tqi_ready_time = (sc->bintval * 80) / 100;
  901. ret = ath5k_hw_set_tx_queueprops(ah, AR5K_TX_QUEUE_ID_CAB, &qi);
  902. if (ret)
  903. goto err;
  904. ret = ath5k_hw_reset_tx_queue(ah, AR5K_TX_QUEUE_ID_CAB);
  905. err:
  906. return ret;
  907. }
  908. /**
  909. * ath5k_drain_tx_buffs - Empty tx buffers
  910. *
  911. * @sc The &struct ath5k_softc
  912. *
  913. * Empty tx buffers from all queues in preparation
  914. * of a reset or during shutdown.
  915. *
  916. * NB: this assumes output has been stopped and
  917. * we do not need to block ath5k_tx_tasklet
  918. */
  919. static void
  920. ath5k_drain_tx_buffs(struct ath5k_softc *sc)
  921. {
  922. struct ath5k_txq *txq;
  923. struct ath5k_buf *bf, *bf0;
  924. int i;
  925. for (i = 0; i < ARRAY_SIZE(sc->txqs); i++) {
  926. if (sc->txqs[i].setup) {
  927. txq = &sc->txqs[i];
  928. spin_lock_bh(&txq->lock);
  929. list_for_each_entry_safe(bf, bf0, &txq->q, list) {
  930. ath5k_debug_printtxbuf(sc, bf);
  931. ath5k_txbuf_free_skb(sc, bf);
  932. spin_lock_bh(&sc->txbuflock);
  933. list_move_tail(&bf->list, &sc->txbuf);
  934. sc->txbuf_len++;
  935. txq->txq_len--;
  936. spin_unlock_bh(&sc->txbuflock);
  937. }
  938. txq->link = NULL;
  939. txq->txq_poll_mark = false;
  940. spin_unlock_bh(&txq->lock);
  941. }
  942. }
  943. }
  944. static void
  945. ath5k_txq_release(struct ath5k_softc *sc)
  946. {
  947. struct ath5k_txq *txq = sc->txqs;
  948. unsigned int i;
  949. for (i = 0; i < ARRAY_SIZE(sc->txqs); i++, txq++)
  950. if (txq->setup) {
  951. ath5k_hw_release_tx_queue(sc->ah, txq->qnum);
  952. txq->setup = false;
  953. }
  954. }
  955. /*************\
  956. * RX Handling *
  957. \*************/
  958. /*
  959. * Enable the receive h/w following a reset.
  960. */
  961. static int
  962. ath5k_rx_start(struct ath5k_softc *sc)
  963. {
  964. struct ath5k_hw *ah = sc->ah;
  965. struct ath_common *common = ath5k_hw_common(ah);
  966. struct ath5k_buf *bf;
  967. int ret;
  968. common->rx_bufsize = roundup(IEEE80211_MAX_FRAME_LEN, common->cachelsz);
  969. ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "cachelsz %u rx_bufsize %u\n",
  970. common->cachelsz, common->rx_bufsize);
  971. spin_lock_bh(&sc->rxbuflock);
  972. sc->rxlink = NULL;
  973. list_for_each_entry(bf, &sc->rxbuf, list) {
  974. ret = ath5k_rxbuf_setup(sc, bf);
  975. if (ret != 0) {
  976. spin_unlock_bh(&sc->rxbuflock);
  977. goto err;
  978. }
  979. }
  980. bf = list_first_entry(&sc->rxbuf, struct ath5k_buf, list);
  981. ath5k_hw_set_rxdp(ah, bf->daddr);
  982. spin_unlock_bh(&sc->rxbuflock);
  983. ath5k_hw_start_rx_dma(ah); /* enable recv descriptors */
  984. ath5k_mode_setup(sc, NULL); /* set filters, etc. */
  985. ath5k_hw_start_rx_pcu(ah); /* re-enable PCU/DMA engine */
  986. return 0;
  987. err:
  988. return ret;
  989. }
  990. /*
  991. * Disable the receive logic on PCU (DRU)
  992. * In preparation for a shutdown.
  993. *
  994. * Note: Doesn't stop rx DMA, ath5k_hw_dma_stop
  995. * does.
  996. */
  997. static void
  998. ath5k_rx_stop(struct ath5k_softc *sc)
  999. {
  1000. struct ath5k_hw *ah = sc->ah;
  1001. ath5k_hw_set_rx_filter(ah, 0); /* clear recv filter */
  1002. ath5k_hw_stop_rx_pcu(ah); /* disable PCU */
  1003. ath5k_debug_printrxbuffs(sc, ah);
  1004. }
  1005. static unsigned int
  1006. ath5k_rx_decrypted(struct ath5k_softc *sc, struct sk_buff *skb,
  1007. struct ath5k_rx_status *rs)
  1008. {
  1009. struct ath5k_hw *ah = sc->ah;
  1010. struct ath_common *common = ath5k_hw_common(ah);
  1011. struct ieee80211_hdr *hdr = (void *)skb->data;
  1012. unsigned int keyix, hlen;
  1013. if (!(rs->rs_status & AR5K_RXERR_DECRYPT) &&
  1014. rs->rs_keyix != AR5K_RXKEYIX_INVALID)
  1015. return RX_FLAG_DECRYPTED;
  1016. /* Apparently when a default key is used to decrypt the packet
  1017. the hw does not set the index used to decrypt. In such cases
  1018. get the index from the packet. */
  1019. hlen = ieee80211_hdrlen(hdr->frame_control);
  1020. if (ieee80211_has_protected(hdr->frame_control) &&
  1021. !(rs->rs_status & AR5K_RXERR_DECRYPT) &&
  1022. skb->len >= hlen + 4) {
  1023. keyix = skb->data[hlen + 3] >> 6;
  1024. if (test_bit(keyix, common->keymap))
  1025. return RX_FLAG_DECRYPTED;
  1026. }
  1027. return 0;
  1028. }
  1029. static void
  1030. ath5k_check_ibss_tsf(struct ath5k_softc *sc, struct sk_buff *skb,
  1031. struct ieee80211_rx_status *rxs)
  1032. {
  1033. struct ath_common *common = ath5k_hw_common(sc->ah);
  1034. u64 tsf, bc_tstamp;
  1035. u32 hw_tu;
  1036. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)skb->data;
  1037. if (ieee80211_is_beacon(mgmt->frame_control) &&
  1038. le16_to_cpu(mgmt->u.beacon.capab_info) & WLAN_CAPABILITY_IBSS &&
  1039. memcmp(mgmt->bssid, common->curbssid, ETH_ALEN) == 0) {
  1040. /*
  1041. * Received an IBSS beacon with the same BSSID. Hardware *must*
  1042. * have updated the local TSF. We have to work around various
  1043. * hardware bugs, though...
  1044. */
  1045. tsf = ath5k_hw_get_tsf64(sc->ah);
  1046. bc_tstamp = le64_to_cpu(mgmt->u.beacon.timestamp);
  1047. hw_tu = TSF_TO_TU(tsf);
  1048. ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
  1049. "beacon %llx mactime %llx (diff %lld) tsf now %llx\n",
  1050. (unsigned long long)bc_tstamp,
  1051. (unsigned long long)rxs->mactime,
  1052. (unsigned long long)(rxs->mactime - bc_tstamp),
  1053. (unsigned long long)tsf);
  1054. /*
  1055. * Sometimes the HW will give us a wrong tstamp in the rx
  1056. * status, causing the timestamp extension to go wrong.
  1057. * (This seems to happen especially with beacon frames bigger
  1058. * than 78 byte (incl. FCS))
  1059. * But we know that the receive timestamp must be later than the
  1060. * timestamp of the beacon since HW must have synced to that.
  1061. *
  1062. * NOTE: here we assume mactime to be after the frame was
  1063. * received, not like mac80211 which defines it at the start.
  1064. */
  1065. if (bc_tstamp > rxs->mactime) {
  1066. ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
  1067. "fixing mactime from %llx to %llx\n",
  1068. (unsigned long long)rxs->mactime,
  1069. (unsigned long long)tsf);
  1070. rxs->mactime = tsf;
  1071. }
  1072. /*
  1073. * Local TSF might have moved higher than our beacon timers,
  1074. * in that case we have to update them to continue sending
  1075. * beacons. This also takes care of synchronizing beacon sending
  1076. * times with other stations.
  1077. */
  1078. if (hw_tu >= sc->nexttbtt)
  1079. ath5k_beacon_update_timers(sc, bc_tstamp);
  1080. /* Check if the beacon timers are still correct, because a TSF
  1081. * update might have created a window between them - for a
  1082. * longer description see the comment of this function: */
  1083. if (!ath5k_hw_check_beacon_timers(sc->ah, sc->bintval)) {
  1084. ath5k_beacon_update_timers(sc, bc_tstamp);
  1085. ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
  1086. "fixed beacon timers after beacon receive\n");
  1087. }
  1088. }
  1089. }
  1090. static void
  1091. ath5k_update_beacon_rssi(struct ath5k_softc *sc, struct sk_buff *skb, int rssi)
  1092. {
  1093. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)skb->data;
  1094. struct ath5k_hw *ah = sc->ah;
  1095. struct ath_common *common = ath5k_hw_common(ah);
  1096. /* only beacons from our BSSID */
  1097. if (!ieee80211_is_beacon(mgmt->frame_control) ||
  1098. memcmp(mgmt->bssid, common->curbssid, ETH_ALEN) != 0)
  1099. return;
  1100. ewma_add(&ah->ah_beacon_rssi_avg, rssi);
  1101. /* in IBSS mode we should keep RSSI statistics per neighbour */
  1102. /* le16_to_cpu(mgmt->u.beacon.capab_info) & WLAN_CAPABILITY_IBSS */
  1103. }
  1104. /*
  1105. * Compute padding position. skb must contain an IEEE 802.11 frame
  1106. */
  1107. static int ath5k_common_padpos(struct sk_buff *skb)
  1108. {
  1109. struct ieee80211_hdr * hdr = (struct ieee80211_hdr *)skb->data;
  1110. __le16 frame_control = hdr->frame_control;
  1111. int padpos = 24;
  1112. if (ieee80211_has_a4(frame_control)) {
  1113. padpos += ETH_ALEN;
  1114. }
  1115. if (ieee80211_is_data_qos(frame_control)) {
  1116. padpos += IEEE80211_QOS_CTL_LEN;
  1117. }
  1118. return padpos;
  1119. }
  1120. /*
  1121. * This function expects an 802.11 frame and returns the number of
  1122. * bytes added, or -1 if we don't have enough header room.
  1123. */
  1124. static int ath5k_add_padding(struct sk_buff *skb)
  1125. {
  1126. int padpos = ath5k_common_padpos(skb);
  1127. int padsize = padpos & 3;
  1128. if (padsize && skb->len>padpos) {
  1129. if (skb_headroom(skb) < padsize)
  1130. return -1;
  1131. skb_push(skb, padsize);
  1132. memmove(skb->data, skb->data+padsize, padpos);
  1133. return padsize;
  1134. }
  1135. return 0;
  1136. }
  1137. /*
  1138. * The MAC header is padded to have 32-bit boundary if the
  1139. * packet payload is non-zero. The general calculation for
  1140. * padsize would take into account odd header lengths:
  1141. * padsize = 4 - (hdrlen & 3); however, since only
  1142. * even-length headers are used, padding can only be 0 or 2
  1143. * bytes and we can optimize this a bit. We must not try to
  1144. * remove padding from short control frames that do not have a
  1145. * payload.
  1146. *
  1147. * This function expects an 802.11 frame and returns the number of
  1148. * bytes removed.
  1149. */
  1150. static int ath5k_remove_padding(struct sk_buff *skb)
  1151. {
  1152. int padpos = ath5k_common_padpos(skb);
  1153. int padsize = padpos & 3;
  1154. if (padsize && skb->len>=padpos+padsize) {
  1155. memmove(skb->data + padsize, skb->data, padpos);
  1156. skb_pull(skb, padsize);
  1157. return padsize;
  1158. }
  1159. return 0;
  1160. }
  1161. static void
  1162. ath5k_receive_frame(struct ath5k_softc *sc, struct sk_buff *skb,
  1163. struct ath5k_rx_status *rs)
  1164. {
  1165. struct ieee80211_rx_status *rxs;
  1166. ath5k_remove_padding(skb);
  1167. rxs = IEEE80211_SKB_RXCB(skb);
  1168. rxs->flag = 0;
  1169. if (unlikely(rs->rs_status & AR5K_RXERR_MIC))
  1170. rxs->flag |= RX_FLAG_MMIC_ERROR;
  1171. /*
  1172. * always extend the mac timestamp, since this information is
  1173. * also needed for proper IBSS merging.
  1174. *
  1175. * XXX: it might be too late to do it here, since rs_tstamp is
  1176. * 15bit only. that means TSF extension has to be done within
  1177. * 32768usec (about 32ms). it might be necessary to move this to
  1178. * the interrupt handler, like it is done in madwifi.
  1179. *
  1180. * Unfortunately we don't know when the hardware takes the rx
  1181. * timestamp (beginning of phy frame, data frame, end of rx?).
  1182. * The only thing we know is that it is hardware specific...
  1183. * On AR5213 it seems the rx timestamp is at the end of the
  1184. * frame, but i'm not sure.
  1185. *
  1186. * NOTE: mac80211 defines mactime at the beginning of the first
  1187. * data symbol. Since we don't have any time references it's
  1188. * impossible to comply to that. This affects IBSS merge only
  1189. * right now, so it's not too bad...
  1190. */
  1191. rxs->mactime = ath5k_extend_tsf(sc->ah, rs->rs_tstamp);
  1192. rxs->flag |= RX_FLAG_TSFT;
  1193. rxs->freq = sc->curchan->center_freq;
  1194. rxs->band = sc->curband->band;
  1195. rxs->signal = sc->ah->ah_noise_floor + rs->rs_rssi;
  1196. rxs->antenna = rs->rs_antenna;
  1197. if (rs->rs_antenna > 0 && rs->rs_antenna < 5)
  1198. sc->stats.antenna_rx[rs->rs_antenna]++;
  1199. else
  1200. sc->stats.antenna_rx[0]++; /* invalid */
  1201. rxs->rate_idx = ath5k_hw_to_driver_rix(sc, rs->rs_rate);
  1202. rxs->flag |= ath5k_rx_decrypted(sc, skb, rs);
  1203. if (rxs->rate_idx >= 0 && rs->rs_rate ==
  1204. sc->curband->bitrates[rxs->rate_idx].hw_value_short)
  1205. rxs->flag |= RX_FLAG_SHORTPRE;
  1206. ath5k_debug_dump_skb(sc, skb, "RX ", 0);
  1207. ath5k_update_beacon_rssi(sc, skb, rs->rs_rssi);
  1208. /* check beacons in IBSS mode */
  1209. if (sc->opmode == NL80211_IFTYPE_ADHOC)
  1210. ath5k_check_ibss_tsf(sc, skb, rxs);
  1211. ieee80211_rx(sc->hw, skb);
  1212. }
  1213. /** ath5k_frame_receive_ok() - Do we want to receive this frame or not?
  1214. *
  1215. * Check if we want to further process this frame or not. Also update
  1216. * statistics. Return true if we want this frame, false if not.
  1217. */
  1218. static bool
  1219. ath5k_receive_frame_ok(struct ath5k_softc *sc, struct ath5k_rx_status *rs)
  1220. {
  1221. sc->stats.rx_all_count++;
  1222. sc->stats.rx_bytes_count += rs->rs_datalen;
  1223. if (unlikely(rs->rs_status)) {
  1224. if (rs->rs_status & AR5K_RXERR_CRC)
  1225. sc->stats.rxerr_crc++;
  1226. if (rs->rs_status & AR5K_RXERR_FIFO)
  1227. sc->stats.rxerr_fifo++;
  1228. if (rs->rs_status & AR5K_RXERR_PHY) {
  1229. sc->stats.rxerr_phy++;
  1230. if (rs->rs_phyerr > 0 && rs->rs_phyerr < 32)
  1231. sc->stats.rxerr_phy_code[rs->rs_phyerr]++;
  1232. return false;
  1233. }
  1234. if (rs->rs_status & AR5K_RXERR_DECRYPT) {
  1235. /*
  1236. * Decrypt error. If the error occurred
  1237. * because there was no hardware key, then
  1238. * let the frame through so the upper layers
  1239. * can process it. This is necessary for 5210
  1240. * parts which have no way to setup a ``clear''
  1241. * key cache entry.
  1242. *
  1243. * XXX do key cache faulting
  1244. */
  1245. sc->stats.rxerr_decrypt++;
  1246. if (rs->rs_keyix == AR5K_RXKEYIX_INVALID &&
  1247. !(rs->rs_status & AR5K_RXERR_CRC))
  1248. return true;
  1249. }
  1250. if (rs->rs_status & AR5K_RXERR_MIC) {
  1251. sc->stats.rxerr_mic++;
  1252. return true;
  1253. }
  1254. /* reject any frames with non-crypto errors */
  1255. if (rs->rs_status & ~(AR5K_RXERR_DECRYPT))
  1256. return false;
  1257. }
  1258. if (unlikely(rs->rs_more)) {
  1259. sc->stats.rxerr_jumbo++;
  1260. return false;
  1261. }
  1262. return true;
  1263. }
  1264. static void
  1265. ath5k_tasklet_rx(unsigned long data)
  1266. {
  1267. struct ath5k_rx_status rs = {};
  1268. struct sk_buff *skb, *next_skb;
  1269. dma_addr_t next_skb_addr;
  1270. struct ath5k_softc *sc = (void *)data;
  1271. struct ath5k_hw *ah = sc->ah;
  1272. struct ath_common *common = ath5k_hw_common(ah);
  1273. struct ath5k_buf *bf;
  1274. struct ath5k_desc *ds;
  1275. int ret;
  1276. spin_lock(&sc->rxbuflock);
  1277. if (list_empty(&sc->rxbuf)) {
  1278. ATH5K_WARN(sc, "empty rx buf pool\n");
  1279. goto unlock;
  1280. }
  1281. do {
  1282. bf = list_first_entry(&sc->rxbuf, struct ath5k_buf, list);
  1283. BUG_ON(bf->skb == NULL);
  1284. skb = bf->skb;
  1285. ds = bf->desc;
  1286. /* bail if HW is still using self-linked descriptor */
  1287. if (ath5k_hw_get_rxdp(sc->ah) == bf->daddr)
  1288. break;
  1289. ret = sc->ah->ah_proc_rx_desc(sc->ah, ds, &rs);
  1290. if (unlikely(ret == -EINPROGRESS))
  1291. break;
  1292. else if (unlikely(ret)) {
  1293. ATH5K_ERR(sc, "error in processing rx descriptor\n");
  1294. sc->stats.rxerr_proc++;
  1295. break;
  1296. }
  1297. if (ath5k_receive_frame_ok(sc, &rs)) {
  1298. next_skb = ath5k_rx_skb_alloc(sc, &next_skb_addr);
  1299. /*
  1300. * If we can't replace bf->skb with a new skb under
  1301. * memory pressure, just skip this packet
  1302. */
  1303. if (!next_skb)
  1304. goto next;
  1305. dma_unmap_single(sc->dev, bf->skbaddr,
  1306. common->rx_bufsize,
  1307. DMA_FROM_DEVICE);
  1308. skb_put(skb, rs.rs_datalen);
  1309. ath5k_receive_frame(sc, skb, &rs);
  1310. bf->skb = next_skb;
  1311. bf->skbaddr = next_skb_addr;
  1312. }
  1313. next:
  1314. list_move_tail(&bf->list, &sc->rxbuf);
  1315. } while (ath5k_rxbuf_setup(sc, bf) == 0);
  1316. unlock:
  1317. spin_unlock(&sc->rxbuflock);
  1318. }
  1319. /*************\
  1320. * TX Handling *
  1321. \*************/
  1322. int
  1323. ath5k_tx_queue(struct ieee80211_hw *hw, struct sk_buff *skb,
  1324. struct ath5k_txq *txq)
  1325. {
  1326. struct ath5k_softc *sc = hw->priv;
  1327. struct ath5k_buf *bf;
  1328. unsigned long flags;
  1329. int padsize;
  1330. ath5k_debug_dump_skb(sc, skb, "TX ", 1);
  1331. /*
  1332. * The hardware expects the header padded to 4 byte boundaries.
  1333. * If this is not the case, we add the padding after the header.
  1334. */
  1335. padsize = ath5k_add_padding(skb);
  1336. if (padsize < 0) {
  1337. ATH5K_ERR(sc, "tx hdrlen not %%4: not enough"
  1338. " headroom to pad");
  1339. goto drop_packet;
  1340. }
  1341. if (txq->txq_len >= ATH5K_TXQ_LEN_MAX)
  1342. ieee80211_stop_queue(hw, txq->qnum);
  1343. spin_lock_irqsave(&sc->txbuflock, flags);
  1344. if (list_empty(&sc->txbuf)) {
  1345. ATH5K_ERR(sc, "no further txbuf available, dropping packet\n");
  1346. spin_unlock_irqrestore(&sc->txbuflock, flags);
  1347. ieee80211_stop_queues(hw);
  1348. goto drop_packet;
  1349. }
  1350. bf = list_first_entry(&sc->txbuf, struct ath5k_buf, list);
  1351. list_del(&bf->list);
  1352. sc->txbuf_len--;
  1353. if (list_empty(&sc->txbuf))
  1354. ieee80211_stop_queues(hw);
  1355. spin_unlock_irqrestore(&sc->txbuflock, flags);
  1356. bf->skb = skb;
  1357. if (ath5k_txbuf_setup(sc, bf, txq, padsize)) {
  1358. bf->skb = NULL;
  1359. spin_lock_irqsave(&sc->txbuflock, flags);
  1360. list_add_tail(&bf->list, &sc->txbuf);
  1361. sc->txbuf_len++;
  1362. spin_unlock_irqrestore(&sc->txbuflock, flags);
  1363. goto drop_packet;
  1364. }
  1365. return NETDEV_TX_OK;
  1366. drop_packet:
  1367. dev_kfree_skb_any(skb);
  1368. return NETDEV_TX_OK;
  1369. }
  1370. static void
  1371. ath5k_tx_frame_completed(struct ath5k_softc *sc, struct sk_buff *skb,
  1372. struct ath5k_tx_status *ts)
  1373. {
  1374. struct ieee80211_tx_info *info;
  1375. int i;
  1376. sc->stats.tx_all_count++;
  1377. sc->stats.tx_bytes_count += skb->len;
  1378. info = IEEE80211_SKB_CB(skb);
  1379. ieee80211_tx_info_clear_status(info);
  1380. for (i = 0; i < 4; i++) {
  1381. struct ieee80211_tx_rate *r =
  1382. &info->status.rates[i];
  1383. if (ts->ts_rate[i]) {
  1384. r->idx = ath5k_hw_to_driver_rix(sc, ts->ts_rate[i]);
  1385. r->count = ts->ts_retry[i];
  1386. } else {
  1387. r->idx = -1;
  1388. r->count = 0;
  1389. }
  1390. }
  1391. /* count the successful attempt as well */
  1392. info->status.rates[ts->ts_final_idx].count++;
  1393. if (unlikely(ts->ts_status)) {
  1394. sc->stats.ack_fail++;
  1395. if (ts->ts_status & AR5K_TXERR_FILT) {
  1396. info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
  1397. sc->stats.txerr_filt++;
  1398. }
  1399. if (ts->ts_status & AR5K_TXERR_XRETRY)
  1400. sc->stats.txerr_retry++;
  1401. if (ts->ts_status & AR5K_TXERR_FIFO)
  1402. sc->stats.txerr_fifo++;
  1403. } else {
  1404. info->flags |= IEEE80211_TX_STAT_ACK;
  1405. info->status.ack_signal = ts->ts_rssi;
  1406. }
  1407. /*
  1408. * Remove MAC header padding before giving the frame
  1409. * back to mac80211.
  1410. */
  1411. ath5k_remove_padding(skb);
  1412. if (ts->ts_antenna > 0 && ts->ts_antenna < 5)
  1413. sc->stats.antenna_tx[ts->ts_antenna]++;
  1414. else
  1415. sc->stats.antenna_tx[0]++; /* invalid */
  1416. ieee80211_tx_status(sc->hw, skb);
  1417. }
  1418. static void
  1419. ath5k_tx_processq(struct ath5k_softc *sc, struct ath5k_txq *txq)
  1420. {
  1421. struct ath5k_tx_status ts = {};
  1422. struct ath5k_buf *bf, *bf0;
  1423. struct ath5k_desc *ds;
  1424. struct sk_buff *skb;
  1425. int ret;
  1426. spin_lock(&txq->lock);
  1427. list_for_each_entry_safe(bf, bf0, &txq->q, list) {
  1428. txq->txq_poll_mark = false;
  1429. /* skb might already have been processed last time. */
  1430. if (bf->skb != NULL) {
  1431. ds = bf->desc;
  1432. ret = sc->ah->ah_proc_tx_desc(sc->ah, ds, &ts);
  1433. if (unlikely(ret == -EINPROGRESS))
  1434. break;
  1435. else if (unlikely(ret)) {
  1436. ATH5K_ERR(sc,
  1437. "error %d while processing "
  1438. "queue %u\n", ret, txq->qnum);
  1439. break;
  1440. }
  1441. skb = bf->skb;
  1442. bf->skb = NULL;
  1443. dma_unmap_single(sc->dev, bf->skbaddr, skb->len,
  1444. DMA_TO_DEVICE);
  1445. ath5k_tx_frame_completed(sc, skb, &ts);
  1446. }
  1447. /*
  1448. * It's possible that the hardware can say the buffer is
  1449. * completed when it hasn't yet loaded the ds_link from
  1450. * host memory and moved on.
  1451. * Always keep the last descriptor to avoid HW races...
  1452. */
  1453. if (ath5k_hw_get_txdp(sc->ah, txq->qnum) != bf->daddr) {
  1454. spin_lock(&sc->txbuflock);
  1455. list_move_tail(&bf->list, &sc->txbuf);
  1456. sc->txbuf_len++;
  1457. txq->txq_len--;
  1458. spin_unlock(&sc->txbuflock);
  1459. }
  1460. }
  1461. spin_unlock(&txq->lock);
  1462. if (txq->txq_len < ATH5K_TXQ_LEN_LOW && txq->qnum < 4)
  1463. ieee80211_wake_queue(sc->hw, txq->qnum);
  1464. }
  1465. static void
  1466. ath5k_tasklet_tx(unsigned long data)
  1467. {
  1468. int i;
  1469. struct ath5k_softc *sc = (void *)data;
  1470. for (i=0; i < AR5K_NUM_TX_QUEUES; i++)
  1471. if (sc->txqs[i].setup && (sc->ah->ah_txq_isr & BIT(i)))
  1472. ath5k_tx_processq(sc, &sc->txqs[i]);
  1473. }
  1474. /*****************\
  1475. * Beacon handling *
  1476. \*****************/
  1477. /*
  1478. * Setup the beacon frame for transmit.
  1479. */
  1480. static int
  1481. ath5k_beacon_setup(struct ath5k_softc *sc, struct ath5k_buf *bf)
  1482. {
  1483. struct sk_buff *skb = bf->skb;
  1484. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1485. struct ath5k_hw *ah = sc->ah;
  1486. struct ath5k_desc *ds;
  1487. int ret = 0;
  1488. u8 antenna;
  1489. u32 flags;
  1490. const int padsize = 0;
  1491. bf->skbaddr = dma_map_single(sc->dev, skb->data, skb->len,
  1492. DMA_TO_DEVICE);
  1493. ATH5K_DBG(sc, ATH5K_DEBUG_BEACON, "skb %p [data %p len %u] "
  1494. "skbaddr %llx\n", skb, skb->data, skb->len,
  1495. (unsigned long long)bf->skbaddr);
  1496. if (dma_mapping_error(sc->dev, bf->skbaddr)) {
  1497. ATH5K_ERR(sc, "beacon DMA mapping failed\n");
  1498. return -EIO;
  1499. }
  1500. ds = bf->desc;
  1501. antenna = ah->ah_tx_ant;
  1502. flags = AR5K_TXDESC_NOACK;
  1503. if (sc->opmode == NL80211_IFTYPE_ADHOC && ath5k_hw_hasveol(ah)) {
  1504. ds->ds_link = bf->daddr; /* self-linked */
  1505. flags |= AR5K_TXDESC_VEOL;
  1506. } else
  1507. ds->ds_link = 0;
  1508. /*
  1509. * If we use multiple antennas on AP and use
  1510. * the Sectored AP scenario, switch antenna every
  1511. * 4 beacons to make sure everybody hears our AP.
  1512. * When a client tries to associate, hw will keep
  1513. * track of the tx antenna to be used for this client
  1514. * automaticaly, based on ACKed packets.
  1515. *
  1516. * Note: AP still listens and transmits RTS on the
  1517. * default antenna which is supposed to be an omni.
  1518. *
  1519. * Note2: On sectored scenarios it's possible to have
  1520. * multiple antennas (1 omni -- the default -- and 14
  1521. * sectors), so if we choose to actually support this
  1522. * mode, we need to allow the user to set how many antennas
  1523. * we have and tweak the code below to send beacons
  1524. * on all of them.
  1525. */
  1526. if (ah->ah_ant_mode == AR5K_ANTMODE_SECTOR_AP)
  1527. antenna = sc->bsent & 4 ? 2 : 1;
  1528. /* FIXME: If we are in g mode and rate is a CCK rate
  1529. * subtract ah->ah_txpower.txp_cck_ofdm_pwr_delta
  1530. * from tx power (value is in dB units already) */
  1531. ds->ds_data = bf->skbaddr;
  1532. ret = ah->ah_setup_tx_desc(ah, ds, skb->len,
  1533. ieee80211_get_hdrlen_from_skb(skb), padsize,
  1534. AR5K_PKT_TYPE_BEACON, (sc->power_level * 2),
  1535. ieee80211_get_tx_rate(sc->hw, info)->hw_value,
  1536. 1, AR5K_TXKEYIX_INVALID,
  1537. antenna, flags, 0, 0);
  1538. if (ret)
  1539. goto err_unmap;
  1540. return 0;
  1541. err_unmap:
  1542. dma_unmap_single(sc->dev, bf->skbaddr, skb->len, DMA_TO_DEVICE);
  1543. return ret;
  1544. }
  1545. /*
  1546. * Updates the beacon that is sent by ath5k_beacon_send. For adhoc,
  1547. * this is called only once at config_bss time, for AP we do it every
  1548. * SWBA interrupt so that the TIM will reflect buffered frames.
  1549. *
  1550. * Called with the beacon lock.
  1551. */
  1552. int
  1553. ath5k_beacon_update(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
  1554. {
  1555. int ret;
  1556. struct ath5k_softc *sc = hw->priv;
  1557. struct ath5k_vif *avf = (void *)vif->drv_priv;
  1558. struct sk_buff *skb;
  1559. if (WARN_ON(!vif)) {
  1560. ret = -EINVAL;
  1561. goto out;
  1562. }
  1563. skb = ieee80211_beacon_get(hw, vif);
  1564. if (!skb) {
  1565. ret = -ENOMEM;
  1566. goto out;
  1567. }
  1568. ath5k_debug_dump_skb(sc, skb, "BC ", 1);
  1569. ath5k_txbuf_free_skb(sc, avf->bbuf);
  1570. avf->bbuf->skb = skb;
  1571. ret = ath5k_beacon_setup(sc, avf->bbuf);
  1572. if (ret)
  1573. avf->bbuf->skb = NULL;
  1574. out:
  1575. return ret;
  1576. }
  1577. /*
  1578. * Transmit a beacon frame at SWBA. Dynamic updates to the
  1579. * frame contents are done as needed and the slot time is
  1580. * also adjusted based on current state.
  1581. *
  1582. * This is called from software irq context (beacontq tasklets)
  1583. * or user context from ath5k_beacon_config.
  1584. */
  1585. static void
  1586. ath5k_beacon_send(struct ath5k_softc *sc)
  1587. {
  1588. struct ath5k_hw *ah = sc->ah;
  1589. struct ieee80211_vif *vif;
  1590. struct ath5k_vif *avf;
  1591. struct ath5k_buf *bf;
  1592. struct sk_buff *skb;
  1593. ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON, "in beacon_send\n");
  1594. /*
  1595. * Check if the previous beacon has gone out. If
  1596. * not, don't don't try to post another: skip this
  1597. * period and wait for the next. Missed beacons
  1598. * indicate a problem and should not occur. If we
  1599. * miss too many consecutive beacons reset the device.
  1600. */
  1601. if (unlikely(ath5k_hw_num_tx_pending(ah, sc->bhalq) != 0)) {
  1602. sc->bmisscount++;
  1603. ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
  1604. "missed %u consecutive beacons\n", sc->bmisscount);
  1605. if (sc->bmisscount > 10) { /* NB: 10 is a guess */
  1606. ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
  1607. "stuck beacon time (%u missed)\n",
  1608. sc->bmisscount);
  1609. ATH5K_DBG(sc, ATH5K_DEBUG_RESET,
  1610. "stuck beacon, resetting\n");
  1611. ieee80211_queue_work(sc->hw, &sc->reset_work);
  1612. }
  1613. return;
  1614. }
  1615. if (unlikely(sc->bmisscount != 0)) {
  1616. ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
  1617. "resume beacon xmit after %u misses\n",
  1618. sc->bmisscount);
  1619. sc->bmisscount = 0;
  1620. }
  1621. if ((sc->opmode == NL80211_IFTYPE_AP && sc->num_ap_vifs > 1) ||
  1622. sc->opmode == NL80211_IFTYPE_MESH_POINT) {
  1623. u64 tsf = ath5k_hw_get_tsf64(ah);
  1624. u32 tsftu = TSF_TO_TU(tsf);
  1625. int slot = ((tsftu % sc->bintval) * ATH_BCBUF) / sc->bintval;
  1626. vif = sc->bslot[(slot + 1) % ATH_BCBUF];
  1627. ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
  1628. "tsf %llx tsftu %x intval %u slot %u vif %p\n",
  1629. (unsigned long long)tsf, tsftu, sc->bintval, slot, vif);
  1630. } else /* only one interface */
  1631. vif = sc->bslot[0];
  1632. if (!vif)
  1633. return;
  1634. avf = (void *)vif->drv_priv;
  1635. bf = avf->bbuf;
  1636. if (unlikely(bf->skb == NULL || sc->opmode == NL80211_IFTYPE_STATION ||
  1637. sc->opmode == NL80211_IFTYPE_MONITOR)) {
  1638. ATH5K_WARN(sc, "bf=%p bf_skb=%p\n", bf, bf ? bf->skb : NULL);
  1639. return;
  1640. }
  1641. /*
  1642. * Stop any current dma and put the new frame on the queue.
  1643. * This should never fail since we check above that no frames
  1644. * are still pending on the queue.
  1645. */
  1646. if (unlikely(ath5k_hw_stop_beacon_queue(ah, sc->bhalq))) {
  1647. ATH5K_WARN(sc, "beacon queue %u didn't start/stop ?\n", sc->bhalq);
  1648. /* NB: hw still stops DMA, so proceed */
  1649. }
  1650. /* refresh the beacon for AP or MESH mode */
  1651. if (sc->opmode == NL80211_IFTYPE_AP ||
  1652. sc->opmode == NL80211_IFTYPE_MESH_POINT)
  1653. ath5k_beacon_update(sc->hw, vif);
  1654. ath5k_hw_set_txdp(ah, sc->bhalq, bf->daddr);
  1655. ath5k_hw_start_tx_dma(ah, sc->bhalq);
  1656. ATH5K_DBG(sc, ATH5K_DEBUG_BEACON, "TXDP[%u] = %llx (%p)\n",
  1657. sc->bhalq, (unsigned long long)bf->daddr, bf->desc);
  1658. skb = ieee80211_get_buffered_bc(sc->hw, vif);
  1659. while (skb) {
  1660. ath5k_tx_queue(sc->hw, skb, sc->cabq);
  1661. skb = ieee80211_get_buffered_bc(sc->hw, vif);
  1662. }
  1663. sc->bsent++;
  1664. }
  1665. /**
  1666. * ath5k_beacon_update_timers - update beacon timers
  1667. *
  1668. * @sc: struct ath5k_softc pointer we are operating on
  1669. * @bc_tsf: the timestamp of the beacon. 0 to reset the TSF. -1 to perform a
  1670. * beacon timer update based on the current HW TSF.
  1671. *
  1672. * Calculate the next target beacon transmit time (TBTT) based on the timestamp
  1673. * of a received beacon or the current local hardware TSF and write it to the
  1674. * beacon timer registers.
  1675. *
  1676. * This is called in a variety of situations, e.g. when a beacon is received,
  1677. * when a TSF update has been detected, but also when an new IBSS is created or
  1678. * when we otherwise know we have to update the timers, but we keep it in this
  1679. * function to have it all together in one place.
  1680. */
  1681. void
  1682. ath5k_beacon_update_timers(struct ath5k_softc *sc, u64 bc_tsf)
  1683. {
  1684. struct ath5k_hw *ah = sc->ah;
  1685. u32 nexttbtt, intval, hw_tu, bc_tu;
  1686. u64 hw_tsf;
  1687. intval = sc->bintval & AR5K_BEACON_PERIOD;
  1688. if (sc->opmode == NL80211_IFTYPE_AP && sc->num_ap_vifs > 1) {
  1689. intval /= ATH_BCBUF; /* staggered multi-bss beacons */
  1690. if (intval < 15)
  1691. ATH5K_WARN(sc, "intval %u is too low, min 15\n",
  1692. intval);
  1693. }
  1694. if (WARN_ON(!intval))
  1695. return;
  1696. /* beacon TSF converted to TU */
  1697. bc_tu = TSF_TO_TU(bc_tsf);
  1698. /* current TSF converted to TU */
  1699. hw_tsf = ath5k_hw_get_tsf64(ah);
  1700. hw_tu = TSF_TO_TU(hw_tsf);
  1701. #define FUDGE AR5K_TUNE_SW_BEACON_RESP + 3
  1702. /* We use FUDGE to make sure the next TBTT is ahead of the current TU.
  1703. * Since we later substract AR5K_TUNE_SW_BEACON_RESP (10) in the timer
  1704. * configuration we need to make sure it is bigger than that. */
  1705. if (bc_tsf == -1) {
  1706. /*
  1707. * no beacons received, called internally.
  1708. * just need to refresh timers based on HW TSF.
  1709. */
  1710. nexttbtt = roundup(hw_tu + FUDGE, intval);
  1711. } else if (bc_tsf == 0) {
  1712. /*
  1713. * no beacon received, probably called by ath5k_reset_tsf().
  1714. * reset TSF to start with 0.
  1715. */
  1716. nexttbtt = intval;
  1717. intval |= AR5K_BEACON_RESET_TSF;
  1718. } else if (bc_tsf > hw_tsf) {
  1719. /*
  1720. * beacon received, SW merge happend but HW TSF not yet updated.
  1721. * not possible to reconfigure timers yet, but next time we
  1722. * receive a beacon with the same BSSID, the hardware will
  1723. * automatically update the TSF and then we need to reconfigure
  1724. * the timers.
  1725. */
  1726. ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
  1727. "need to wait for HW TSF sync\n");
  1728. return;
  1729. } else {
  1730. /*
  1731. * most important case for beacon synchronization between STA.
  1732. *
  1733. * beacon received and HW TSF has been already updated by HW.
  1734. * update next TBTT based on the TSF of the beacon, but make
  1735. * sure it is ahead of our local TSF timer.
  1736. */
  1737. nexttbtt = bc_tu + roundup(hw_tu + FUDGE - bc_tu, intval);
  1738. }
  1739. #undef FUDGE
  1740. sc->nexttbtt = nexttbtt;
  1741. intval |= AR5K_BEACON_ENA;
  1742. ath5k_hw_init_beacon(ah, nexttbtt, intval);
  1743. /*
  1744. * debugging output last in order to preserve the time critical aspect
  1745. * of this function
  1746. */
  1747. if (bc_tsf == -1)
  1748. ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
  1749. "reconfigured timers based on HW TSF\n");
  1750. else if (bc_tsf == 0)
  1751. ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
  1752. "reset HW TSF and timers\n");
  1753. else
  1754. ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
  1755. "updated timers based on beacon TSF\n");
  1756. ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
  1757. "bc_tsf %llx hw_tsf %llx bc_tu %u hw_tu %u nexttbtt %u\n",
  1758. (unsigned long long) bc_tsf,
  1759. (unsigned long long) hw_tsf, bc_tu, hw_tu, nexttbtt);
  1760. ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON, "intval %u %s %s\n",
  1761. intval & AR5K_BEACON_PERIOD,
  1762. intval & AR5K_BEACON_ENA ? "AR5K_BEACON_ENA" : "",
  1763. intval & AR5K_BEACON_RESET_TSF ? "AR5K_BEACON_RESET_TSF" : "");
  1764. }
  1765. /**
  1766. * ath5k_beacon_config - Configure the beacon queues and interrupts
  1767. *
  1768. * @sc: struct ath5k_softc pointer we are operating on
  1769. *
  1770. * In IBSS mode we use a self-linked tx descriptor if possible. We enable SWBA
  1771. * interrupts to detect TSF updates only.
  1772. */
  1773. void
  1774. ath5k_beacon_config(struct ath5k_softc *sc)
  1775. {
  1776. struct ath5k_hw *ah = sc->ah;
  1777. unsigned long flags;
  1778. spin_lock_irqsave(&sc->block, flags);
  1779. sc->bmisscount = 0;
  1780. sc->imask &= ~(AR5K_INT_BMISS | AR5K_INT_SWBA);
  1781. if (sc->enable_beacon) {
  1782. /*
  1783. * In IBSS mode we use a self-linked tx descriptor and let the
  1784. * hardware send the beacons automatically. We have to load it
  1785. * only once here.
  1786. * We use the SWBA interrupt only to keep track of the beacon
  1787. * timers in order to detect automatic TSF updates.
  1788. */
  1789. ath5k_beaconq_config(sc);
  1790. sc->imask |= AR5K_INT_SWBA;
  1791. if (sc->opmode == NL80211_IFTYPE_ADHOC) {
  1792. if (ath5k_hw_hasveol(ah))
  1793. ath5k_beacon_send(sc);
  1794. } else
  1795. ath5k_beacon_update_timers(sc, -1);
  1796. } else {
  1797. ath5k_hw_stop_beacon_queue(sc->ah, sc->bhalq);
  1798. }
  1799. ath5k_hw_set_imr(ah, sc->imask);
  1800. mmiowb();
  1801. spin_unlock_irqrestore(&sc->block, flags);
  1802. }
  1803. static void ath5k_tasklet_beacon(unsigned long data)
  1804. {
  1805. struct ath5k_softc *sc = (struct ath5k_softc *) data;
  1806. /*
  1807. * Software beacon alert--time to send a beacon.
  1808. *
  1809. * In IBSS mode we use this interrupt just to
  1810. * keep track of the next TBTT (target beacon
  1811. * transmission time) in order to detect wether
  1812. * automatic TSF updates happened.
  1813. */
  1814. if (sc->opmode == NL80211_IFTYPE_ADHOC) {
  1815. /* XXX: only if VEOL suppported */
  1816. u64 tsf = ath5k_hw_get_tsf64(sc->ah);
  1817. sc->nexttbtt += sc->bintval;
  1818. ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
  1819. "SWBA nexttbtt: %x hw_tu: %x "
  1820. "TSF: %llx\n",
  1821. sc->nexttbtt,
  1822. TSF_TO_TU(tsf),
  1823. (unsigned long long) tsf);
  1824. } else {
  1825. spin_lock(&sc->block);
  1826. ath5k_beacon_send(sc);
  1827. spin_unlock(&sc->block);
  1828. }
  1829. }
  1830. /********************\
  1831. * Interrupt handling *
  1832. \********************/
  1833. static void
  1834. ath5k_intr_calibration_poll(struct ath5k_hw *ah)
  1835. {
  1836. if (time_is_before_eq_jiffies(ah->ah_cal_next_ani) &&
  1837. !(ah->ah_cal_mask & AR5K_CALIBRATION_FULL)) {
  1838. /* run ANI only when full calibration is not active */
  1839. ah->ah_cal_next_ani = jiffies +
  1840. msecs_to_jiffies(ATH5K_TUNE_CALIBRATION_INTERVAL_ANI);
  1841. tasklet_schedule(&ah->ah_sc->ani_tasklet);
  1842. } else if (time_is_before_eq_jiffies(ah->ah_cal_next_full)) {
  1843. ah->ah_cal_next_full = jiffies +
  1844. msecs_to_jiffies(ATH5K_TUNE_CALIBRATION_INTERVAL_FULL);
  1845. tasklet_schedule(&ah->ah_sc->calib);
  1846. }
  1847. /* we could use SWI to generate enough interrupts to meet our
  1848. * calibration interval requirements, if necessary:
  1849. * AR5K_REG_ENABLE_BITS(ah, AR5K_CR, AR5K_CR_SWI); */
  1850. }
  1851. irqreturn_t
  1852. ath5k_intr(int irq, void *dev_id)
  1853. {
  1854. struct ath5k_softc *sc = dev_id;
  1855. struct ath5k_hw *ah = sc->ah;
  1856. enum ath5k_int status;
  1857. unsigned int counter = 1000;
  1858. if (unlikely(test_bit(ATH_STAT_INVALID, sc->status) ||
  1859. ((ath5k_get_bus_type(ah) != ATH_AHB) &&
  1860. !ath5k_hw_is_intr_pending(ah))))
  1861. return IRQ_NONE;
  1862. do {
  1863. ath5k_hw_get_isr(ah, &status); /* NB: clears IRQ too */
  1864. ATH5K_DBG(sc, ATH5K_DEBUG_INTR, "status 0x%x/0x%x\n",
  1865. status, sc->imask);
  1866. if (unlikely(status & AR5K_INT_FATAL)) {
  1867. /*
  1868. * Fatal errors are unrecoverable.
  1869. * Typically these are caused by DMA errors.
  1870. */
  1871. ATH5K_DBG(sc, ATH5K_DEBUG_RESET,
  1872. "fatal int, resetting\n");
  1873. ieee80211_queue_work(sc->hw, &sc->reset_work);
  1874. } else if (unlikely(status & AR5K_INT_RXORN)) {
  1875. /*
  1876. * Receive buffers are full. Either the bus is busy or
  1877. * the CPU is not fast enough to process all received
  1878. * frames.
  1879. * Older chipsets need a reset to come out of this
  1880. * condition, but we treat it as RX for newer chips.
  1881. * We don't know exactly which versions need a reset -
  1882. * this guess is copied from the HAL.
  1883. */
  1884. sc->stats.rxorn_intr++;
  1885. if (ah->ah_mac_srev < AR5K_SREV_AR5212) {
  1886. ATH5K_DBG(sc, ATH5K_DEBUG_RESET,
  1887. "rx overrun, resetting\n");
  1888. ieee80211_queue_work(sc->hw, &sc->reset_work);
  1889. }
  1890. else
  1891. tasklet_schedule(&sc->rxtq);
  1892. } else {
  1893. if (status & AR5K_INT_SWBA) {
  1894. tasklet_hi_schedule(&sc->beacontq);
  1895. }
  1896. if (status & AR5K_INT_RXEOL) {
  1897. /*
  1898. * NB: the hardware should re-read the link when
  1899. * RXE bit is written, but it doesn't work at
  1900. * least on older hardware revs.
  1901. */
  1902. sc->stats.rxeol_intr++;
  1903. }
  1904. if (status & AR5K_INT_TXURN) {
  1905. /* bump tx trigger level */
  1906. ath5k_hw_update_tx_triglevel(ah, true);
  1907. }
  1908. if (status & (AR5K_INT_RXOK | AR5K_INT_RXERR))
  1909. tasklet_schedule(&sc->rxtq);
  1910. if (status & (AR5K_INT_TXOK | AR5K_INT_TXDESC
  1911. | AR5K_INT_TXERR | AR5K_INT_TXEOL))
  1912. tasklet_schedule(&sc->txtq);
  1913. if (status & AR5K_INT_BMISS) {
  1914. /* TODO */
  1915. }
  1916. if (status & AR5K_INT_MIB) {
  1917. sc->stats.mib_intr++;
  1918. ath5k_hw_update_mib_counters(ah);
  1919. ath5k_ani_mib_intr(ah);
  1920. }
  1921. if (status & AR5K_INT_GPIO)
  1922. tasklet_schedule(&sc->rf_kill.toggleq);
  1923. }
  1924. if (ath5k_get_bus_type(ah) == ATH_AHB)
  1925. break;
  1926. } while (ath5k_hw_is_intr_pending(ah) && --counter > 0);
  1927. if (unlikely(!counter))
  1928. ATH5K_WARN(sc, "too many interrupts, giving up for now\n");
  1929. ath5k_intr_calibration_poll(ah);
  1930. return IRQ_HANDLED;
  1931. }
  1932. /*
  1933. * Periodically recalibrate the PHY to account
  1934. * for temperature/environment changes.
  1935. */
  1936. static void
  1937. ath5k_tasklet_calibrate(unsigned long data)
  1938. {
  1939. struct ath5k_softc *sc = (void *)data;
  1940. struct ath5k_hw *ah = sc->ah;
  1941. /* Only full calibration for now */
  1942. ah->ah_cal_mask |= AR5K_CALIBRATION_FULL;
  1943. ATH5K_DBG(sc, ATH5K_DEBUG_CALIBRATE, "channel %u/%x\n",
  1944. ieee80211_frequency_to_channel(sc->curchan->center_freq),
  1945. sc->curchan->hw_value);
  1946. if (ath5k_hw_gainf_calibrate(ah) == AR5K_RFGAIN_NEED_CHANGE) {
  1947. /*
  1948. * Rfgain is out of bounds, reset the chip
  1949. * to load new gain values.
  1950. */
  1951. ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "calibration, resetting\n");
  1952. ieee80211_queue_work(sc->hw, &sc->reset_work);
  1953. }
  1954. if (ath5k_hw_phy_calibrate(ah, sc->curchan))
  1955. ATH5K_ERR(sc, "calibration of channel %u failed\n",
  1956. ieee80211_frequency_to_channel(
  1957. sc->curchan->center_freq));
  1958. /* Noise floor calibration interrupts rx/tx path while I/Q calibration
  1959. * doesn't.
  1960. * TODO: We should stop TX here, so that it doesn't interfere.
  1961. * Note that stopping the queues is not enough to stop TX! */
  1962. if (time_is_before_eq_jiffies(ah->ah_cal_next_nf)) {
  1963. ah->ah_cal_next_nf = jiffies +
  1964. msecs_to_jiffies(ATH5K_TUNE_CALIBRATION_INTERVAL_NF);
  1965. ath5k_hw_update_noise_floor(ah);
  1966. }
  1967. ah->ah_cal_mask &= ~AR5K_CALIBRATION_FULL;
  1968. }
  1969. static void
  1970. ath5k_tasklet_ani(unsigned long data)
  1971. {
  1972. struct ath5k_softc *sc = (void *)data;
  1973. struct ath5k_hw *ah = sc->ah;
  1974. ah->ah_cal_mask |= AR5K_CALIBRATION_ANI;
  1975. ath5k_ani_calibration(ah);
  1976. ah->ah_cal_mask &= ~AR5K_CALIBRATION_ANI;
  1977. }
  1978. static void
  1979. ath5k_tx_complete_poll_work(struct work_struct *work)
  1980. {
  1981. struct ath5k_softc *sc = container_of(work, struct ath5k_softc,
  1982. tx_complete_work.work);
  1983. struct ath5k_txq *txq;
  1984. int i;
  1985. bool needreset = false;
  1986. mutex_lock(&sc->lock);
  1987. for (i = 0; i < ARRAY_SIZE(sc->txqs); i++) {
  1988. if (sc->txqs[i].setup) {
  1989. txq = &sc->txqs[i];
  1990. spin_lock_bh(&txq->lock);
  1991. if (txq->txq_len > 1) {
  1992. if (txq->txq_poll_mark) {
  1993. ATH5K_DBG(sc, ATH5K_DEBUG_XMIT,
  1994. "TX queue stuck %d\n",
  1995. txq->qnum);
  1996. needreset = true;
  1997. txq->txq_stuck++;
  1998. spin_unlock_bh(&txq->lock);
  1999. break;
  2000. } else {
  2001. txq->txq_poll_mark = true;
  2002. }
  2003. }
  2004. spin_unlock_bh(&txq->lock);
  2005. }
  2006. }
  2007. if (needreset) {
  2008. ATH5K_DBG(sc, ATH5K_DEBUG_RESET,
  2009. "TX queues stuck, resetting\n");
  2010. ath5k_reset(sc, NULL, true);
  2011. }
  2012. mutex_unlock(&sc->lock);
  2013. ieee80211_queue_delayed_work(sc->hw, &sc->tx_complete_work,
  2014. msecs_to_jiffies(ATH5K_TX_COMPLETE_POLL_INT));
  2015. }
  2016. /*************************\
  2017. * Initialization routines *
  2018. \*************************/
  2019. int
  2020. ath5k_init_softc(struct ath5k_softc *sc, const struct ath_bus_ops *bus_ops)
  2021. {
  2022. struct ieee80211_hw *hw = sc->hw;
  2023. struct ath_common *common;
  2024. int ret;
  2025. int csz;
  2026. /* Initialize driver private data */
  2027. SET_IEEE80211_DEV(hw, sc->dev);
  2028. hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
  2029. IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
  2030. IEEE80211_HW_SIGNAL_DBM |
  2031. IEEE80211_HW_REPORTS_TX_ACK_STATUS;
  2032. hw->wiphy->interface_modes =
  2033. BIT(NL80211_IFTYPE_AP) |
  2034. BIT(NL80211_IFTYPE_STATION) |
  2035. BIT(NL80211_IFTYPE_ADHOC) |
  2036. BIT(NL80211_IFTYPE_MESH_POINT);
  2037. /* both antennas can be configured as RX or TX */
  2038. hw->wiphy->available_antennas_tx = 0x3;
  2039. hw->wiphy->available_antennas_rx = 0x3;
  2040. hw->extra_tx_headroom = 2;
  2041. hw->channel_change_time = 5000;
  2042. /*
  2043. * Mark the device as detached to avoid processing
  2044. * interrupts until setup is complete.
  2045. */
  2046. __set_bit(ATH_STAT_INVALID, sc->status);
  2047. sc->opmode = NL80211_IFTYPE_STATION;
  2048. sc->bintval = 1000;
  2049. mutex_init(&sc->lock);
  2050. spin_lock_init(&sc->rxbuflock);
  2051. spin_lock_init(&sc->txbuflock);
  2052. spin_lock_init(&sc->block);
  2053. /* Setup interrupt handler */
  2054. ret = request_irq(sc->irq, ath5k_intr, IRQF_SHARED, "ath", sc);
  2055. if (ret) {
  2056. ATH5K_ERR(sc, "request_irq failed\n");
  2057. goto err;
  2058. }
  2059. /* If we passed the test, malloc an ath5k_hw struct */
  2060. sc->ah = kzalloc(sizeof(struct ath5k_hw), GFP_KERNEL);
  2061. if (!sc->ah) {
  2062. ret = -ENOMEM;
  2063. ATH5K_ERR(sc, "out of memory\n");
  2064. goto err_irq;
  2065. }
  2066. sc->ah->ah_sc = sc;
  2067. sc->ah->ah_iobase = sc->iobase;
  2068. common = ath5k_hw_common(sc->ah);
  2069. common->ops = &ath5k_common_ops;
  2070. common->bus_ops = bus_ops;
  2071. common->ah = sc->ah;
  2072. common->hw = hw;
  2073. common->priv = sc;
  2074. /*
  2075. * Cache line size is used to size and align various
  2076. * structures used to communicate with the hardware.
  2077. */
  2078. ath5k_read_cachesize(common, &csz);
  2079. common->cachelsz = csz << 2; /* convert to bytes */
  2080. spin_lock_init(&common->cc_lock);
  2081. /* Initialize device */
  2082. ret = ath5k_hw_init(sc);
  2083. if (ret)
  2084. goto err_free_ah;
  2085. /* set up multi-rate retry capabilities */
  2086. if (sc->ah->ah_version == AR5K_AR5212) {
  2087. hw->max_rates = 4;
  2088. hw->max_rate_tries = 11;
  2089. }
  2090. hw->vif_data_size = sizeof(struct ath5k_vif);
  2091. /* Finish private driver data initialization */
  2092. ret = ath5k_init(hw);
  2093. if (ret)
  2094. goto err_ah;
  2095. ATH5K_INFO(sc, "Atheros AR%s chip found (MAC: 0x%x, PHY: 0x%x)\n",
  2096. ath5k_chip_name(AR5K_VERSION_MAC, sc->ah->ah_mac_srev),
  2097. sc->ah->ah_mac_srev,
  2098. sc->ah->ah_phy_revision);
  2099. if (!sc->ah->ah_single_chip) {
  2100. /* Single chip radio (!RF5111) */
  2101. if (sc->ah->ah_radio_5ghz_revision &&
  2102. !sc->ah->ah_radio_2ghz_revision) {
  2103. /* No 5GHz support -> report 2GHz radio */
  2104. if (!test_bit(AR5K_MODE_11A,
  2105. sc->ah->ah_capabilities.cap_mode)) {
  2106. ATH5K_INFO(sc, "RF%s 2GHz radio found (0x%x)\n",
  2107. ath5k_chip_name(AR5K_VERSION_RAD,
  2108. sc->ah->ah_radio_5ghz_revision),
  2109. sc->ah->ah_radio_5ghz_revision);
  2110. /* No 2GHz support (5110 and some
  2111. * 5Ghz only cards) -> report 5Ghz radio */
  2112. } else if (!test_bit(AR5K_MODE_11B,
  2113. sc->ah->ah_capabilities.cap_mode)) {
  2114. ATH5K_INFO(sc, "RF%s 5GHz radio found (0x%x)\n",
  2115. ath5k_chip_name(AR5K_VERSION_RAD,
  2116. sc->ah->ah_radio_5ghz_revision),
  2117. sc->ah->ah_radio_5ghz_revision);
  2118. /* Multiband radio */
  2119. } else {
  2120. ATH5K_INFO(sc, "RF%s multiband radio found"
  2121. " (0x%x)\n",
  2122. ath5k_chip_name(AR5K_VERSION_RAD,
  2123. sc->ah->ah_radio_5ghz_revision),
  2124. sc->ah->ah_radio_5ghz_revision);
  2125. }
  2126. }
  2127. /* Multi chip radio (RF5111 - RF2111) ->
  2128. * report both 2GHz/5GHz radios */
  2129. else if (sc->ah->ah_radio_5ghz_revision &&
  2130. sc->ah->ah_radio_2ghz_revision){
  2131. ATH5K_INFO(sc, "RF%s 5GHz radio found (0x%x)\n",
  2132. ath5k_chip_name(AR5K_VERSION_RAD,
  2133. sc->ah->ah_radio_5ghz_revision),
  2134. sc->ah->ah_radio_5ghz_revision);
  2135. ATH5K_INFO(sc, "RF%s 2GHz radio found (0x%x)\n",
  2136. ath5k_chip_name(AR5K_VERSION_RAD,
  2137. sc->ah->ah_radio_2ghz_revision),
  2138. sc->ah->ah_radio_2ghz_revision);
  2139. }
  2140. }
  2141. ath5k_debug_init_device(sc);
  2142. /* ready to process interrupts */
  2143. __clear_bit(ATH_STAT_INVALID, sc->status);
  2144. return 0;
  2145. err_ah:
  2146. ath5k_hw_deinit(sc->ah);
  2147. err_free_ah:
  2148. kfree(sc->ah);
  2149. err_irq:
  2150. free_irq(sc->irq, sc);
  2151. err:
  2152. return ret;
  2153. }
  2154. static int
  2155. ath5k_stop_locked(struct ath5k_softc *sc)
  2156. {
  2157. struct ath5k_hw *ah = sc->ah;
  2158. ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "invalid %u\n",
  2159. test_bit(ATH_STAT_INVALID, sc->status));
  2160. /*
  2161. * Shutdown the hardware and driver:
  2162. * stop output from above
  2163. * disable interrupts
  2164. * turn off timers
  2165. * turn off the radio
  2166. * clear transmit machinery
  2167. * clear receive machinery
  2168. * drain and release tx queues
  2169. * reclaim beacon resources
  2170. * power down hardware
  2171. *
  2172. * Note that some of this work is not possible if the
  2173. * hardware is gone (invalid).
  2174. */
  2175. ieee80211_stop_queues(sc->hw);
  2176. if (!test_bit(ATH_STAT_INVALID, sc->status)) {
  2177. ath5k_led_off(sc);
  2178. ath5k_hw_set_imr(ah, 0);
  2179. synchronize_irq(sc->irq);
  2180. ath5k_rx_stop(sc);
  2181. ath5k_hw_dma_stop(ah);
  2182. ath5k_drain_tx_buffs(sc);
  2183. ath5k_hw_phy_disable(ah);
  2184. }
  2185. return 0;
  2186. }
  2187. int
  2188. ath5k_init_hw(struct ath5k_softc *sc)
  2189. {
  2190. struct ath5k_hw *ah = sc->ah;
  2191. struct ath_common *common = ath5k_hw_common(ah);
  2192. int ret, i;
  2193. mutex_lock(&sc->lock);
  2194. ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "mode %d\n", sc->opmode);
  2195. /*
  2196. * Stop anything previously setup. This is safe
  2197. * no matter this is the first time through or not.
  2198. */
  2199. ath5k_stop_locked(sc);
  2200. /*
  2201. * The basic interface to setting the hardware in a good
  2202. * state is ``reset''. On return the hardware is known to
  2203. * be powered up and with interrupts disabled. This must
  2204. * be followed by initialization of the appropriate bits
  2205. * and then setup of the interrupt mask.
  2206. */
  2207. sc->curchan = sc->hw->conf.channel;
  2208. sc->curband = &sc->sbands[sc->curchan->band];
  2209. sc->imask = AR5K_INT_RXOK | AR5K_INT_RXERR | AR5K_INT_RXEOL |
  2210. AR5K_INT_RXORN | AR5K_INT_TXDESC | AR5K_INT_TXEOL |
  2211. AR5K_INT_FATAL | AR5K_INT_GLOBAL | AR5K_INT_MIB;
  2212. ret = ath5k_reset(sc, NULL, false);
  2213. if (ret)
  2214. goto done;
  2215. ath5k_rfkill_hw_start(ah);
  2216. /*
  2217. * Reset the key cache since some parts do not reset the
  2218. * contents on initial power up or resume from suspend.
  2219. */
  2220. for (i = 0; i < common->keymax; i++)
  2221. ath_hw_keyreset(common, (u16) i);
  2222. /* Use higher rates for acks instead of base
  2223. * rate */
  2224. ah->ah_ack_bitrate_high = true;
  2225. for (i = 0; i < ARRAY_SIZE(sc->bslot); i++)
  2226. sc->bslot[i] = NULL;
  2227. ret = 0;
  2228. done:
  2229. mmiowb();
  2230. mutex_unlock(&sc->lock);
  2231. ieee80211_queue_delayed_work(sc->hw, &sc->tx_complete_work,
  2232. msecs_to_jiffies(ATH5K_TX_COMPLETE_POLL_INT));
  2233. return ret;
  2234. }
  2235. static void stop_tasklets(struct ath5k_softc *sc)
  2236. {
  2237. tasklet_kill(&sc->rxtq);
  2238. tasklet_kill(&sc->txtq);
  2239. tasklet_kill(&sc->calib);
  2240. tasklet_kill(&sc->beacontq);
  2241. tasklet_kill(&sc->ani_tasklet);
  2242. }
  2243. /*
  2244. * Stop the device, grabbing the top-level lock to protect
  2245. * against concurrent entry through ath5k_init (which can happen
  2246. * if another thread does a system call and the thread doing the
  2247. * stop is preempted).
  2248. */
  2249. int
  2250. ath5k_stop_hw(struct ath5k_softc *sc)
  2251. {
  2252. int ret;
  2253. mutex_lock(&sc->lock);
  2254. ret = ath5k_stop_locked(sc);
  2255. if (ret == 0 && !test_bit(ATH_STAT_INVALID, sc->status)) {
  2256. /*
  2257. * Don't set the card in full sleep mode!
  2258. *
  2259. * a) When the device is in this state it must be carefully
  2260. * woken up or references to registers in the PCI clock
  2261. * domain may freeze the bus (and system). This varies
  2262. * by chip and is mostly an issue with newer parts
  2263. * (madwifi sources mentioned srev >= 0x78) that go to
  2264. * sleep more quickly.
  2265. *
  2266. * b) On older chips full sleep results a weird behaviour
  2267. * during wakeup. I tested various cards with srev < 0x78
  2268. * and they don't wake up after module reload, a second
  2269. * module reload is needed to bring the card up again.
  2270. *
  2271. * Until we figure out what's going on don't enable
  2272. * full chip reset on any chip (this is what Legacy HAL
  2273. * and Sam's HAL do anyway). Instead Perform a full reset
  2274. * on the device (same as initial state after attach) and
  2275. * leave it idle (keep MAC/BB on warm reset) */
  2276. ret = ath5k_hw_on_hold(sc->ah);
  2277. ATH5K_DBG(sc, ATH5K_DEBUG_RESET,
  2278. "putting device to sleep\n");
  2279. }
  2280. mmiowb();
  2281. mutex_unlock(&sc->lock);
  2282. stop_tasklets(sc);
  2283. cancel_delayed_work_sync(&sc->tx_complete_work);
  2284. ath5k_rfkill_hw_stop(sc->ah);
  2285. return ret;
  2286. }
  2287. /*
  2288. * Reset the hardware. If chan is not NULL, then also pause rx/tx
  2289. * and change to the given channel.
  2290. *
  2291. * This should be called with sc->lock.
  2292. */
  2293. static int
  2294. ath5k_reset(struct ath5k_softc *sc, struct ieee80211_channel *chan,
  2295. bool skip_pcu)
  2296. {
  2297. struct ath5k_hw *ah = sc->ah;
  2298. struct ath_common *common = ath5k_hw_common(ah);
  2299. int ret, ani_mode;
  2300. ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "resetting\n");
  2301. ath5k_hw_set_imr(ah, 0);
  2302. synchronize_irq(sc->irq);
  2303. stop_tasklets(sc);
  2304. /* Save ani mode and disable ANI durring
  2305. * reset. If we don't we might get false
  2306. * PHY error interrupts. */
  2307. ani_mode = ah->ah_sc->ani_state.ani_mode;
  2308. ath5k_ani_init(ah, ATH5K_ANI_MODE_OFF);
  2309. /* We are going to empty hw queues
  2310. * so we should also free any remaining
  2311. * tx buffers */
  2312. ath5k_drain_tx_buffs(sc);
  2313. if (chan) {
  2314. sc->curchan = chan;
  2315. sc->curband = &sc->sbands[chan->band];
  2316. }
  2317. ret = ath5k_hw_reset(ah, sc->opmode, sc->curchan, chan != NULL,
  2318. skip_pcu);
  2319. if (ret) {
  2320. ATH5K_ERR(sc, "can't reset hardware (%d)\n", ret);
  2321. goto err;
  2322. }
  2323. ret = ath5k_rx_start(sc);
  2324. if (ret) {
  2325. ATH5K_ERR(sc, "can't start recv logic\n");
  2326. goto err;
  2327. }
  2328. ath5k_ani_init(ah, ani_mode);
  2329. ah->ah_cal_next_full = jiffies;
  2330. ah->ah_cal_next_ani = jiffies;
  2331. ah->ah_cal_next_nf = jiffies;
  2332. ewma_init(&ah->ah_beacon_rssi_avg, 1024, 8);
  2333. /* clear survey data and cycle counters */
  2334. memset(&sc->survey, 0, sizeof(sc->survey));
  2335. spin_lock_bh(&common->cc_lock);
  2336. ath_hw_cycle_counters_update(common);
  2337. memset(&common->cc_survey, 0, sizeof(common->cc_survey));
  2338. memset(&common->cc_ani, 0, sizeof(common->cc_ani));
  2339. spin_unlock_bh(&common->cc_lock);
  2340. /*
  2341. * Change channels and update the h/w rate map if we're switching;
  2342. * e.g. 11a to 11b/g.
  2343. *
  2344. * We may be doing a reset in response to an ioctl that changes the
  2345. * channel so update any state that might change as a result.
  2346. *
  2347. * XXX needed?
  2348. */
  2349. /* ath5k_chan_change(sc, c); */
  2350. ath5k_beacon_config(sc);
  2351. /* intrs are enabled by ath5k_beacon_config */
  2352. ieee80211_wake_queues(sc->hw);
  2353. return 0;
  2354. err:
  2355. return ret;
  2356. }
  2357. static void ath5k_reset_work(struct work_struct *work)
  2358. {
  2359. struct ath5k_softc *sc = container_of(work, struct ath5k_softc,
  2360. reset_work);
  2361. mutex_lock(&sc->lock);
  2362. ath5k_reset(sc, NULL, true);
  2363. mutex_unlock(&sc->lock);
  2364. }
  2365. static int
  2366. ath5k_init(struct ieee80211_hw *hw)
  2367. {
  2368. struct ath5k_softc *sc = hw->priv;
  2369. struct ath5k_hw *ah = sc->ah;
  2370. struct ath_regulatory *regulatory = ath5k_hw_regulatory(ah);
  2371. struct ath5k_txq *txq;
  2372. u8 mac[ETH_ALEN] = {};
  2373. int ret;
  2374. /*
  2375. * Check if the MAC has multi-rate retry support.
  2376. * We do this by trying to setup a fake extended
  2377. * descriptor. MACs that don't have support will
  2378. * return false w/o doing anything. MACs that do
  2379. * support it will return true w/o doing anything.
  2380. */
  2381. ret = ath5k_hw_setup_mrr_tx_desc(ah, NULL, 0, 0, 0, 0, 0, 0);
  2382. if (ret < 0)
  2383. goto err;
  2384. if (ret > 0)
  2385. __set_bit(ATH_STAT_MRRETRY, sc->status);
  2386. /*
  2387. * Collect the channel list. The 802.11 layer
  2388. * is resposible for filtering this list based
  2389. * on settings like the phy mode and regulatory
  2390. * domain restrictions.
  2391. */
  2392. ret = ath5k_setup_bands(hw);
  2393. if (ret) {
  2394. ATH5K_ERR(sc, "can't get channels\n");
  2395. goto err;
  2396. }
  2397. /* NB: setup here so ath5k_rate_update is happy */
  2398. if (test_bit(AR5K_MODE_11A, ah->ah_modes))
  2399. ath5k_setcurmode(sc, AR5K_MODE_11A);
  2400. else
  2401. ath5k_setcurmode(sc, AR5K_MODE_11B);
  2402. /*
  2403. * Allocate tx+rx descriptors and populate the lists.
  2404. */
  2405. ret = ath5k_desc_alloc(sc);
  2406. if (ret) {
  2407. ATH5K_ERR(sc, "can't allocate descriptors\n");
  2408. goto err;
  2409. }
  2410. /*
  2411. * Allocate hardware transmit queues: one queue for
  2412. * beacon frames and one data queue for each QoS
  2413. * priority. Note that hw functions handle resetting
  2414. * these queues at the needed time.
  2415. */
  2416. ret = ath5k_beaconq_setup(ah);
  2417. if (ret < 0) {
  2418. ATH5K_ERR(sc, "can't setup a beacon xmit queue\n");
  2419. goto err_desc;
  2420. }
  2421. sc->bhalq = ret;
  2422. sc->cabq = ath5k_txq_setup(sc, AR5K_TX_QUEUE_CAB, 0);
  2423. if (IS_ERR(sc->cabq)) {
  2424. ATH5K_ERR(sc, "can't setup cab queue\n");
  2425. ret = PTR_ERR(sc->cabq);
  2426. goto err_bhal;
  2427. }
  2428. /* 5211 and 5212 usually support 10 queues but we better rely on the
  2429. * capability information */
  2430. if (ah->ah_capabilities.cap_queues.q_tx_num >= 6) {
  2431. /* This order matches mac80211's queue priority, so we can
  2432. * directly use the mac80211 queue number without any mapping */
  2433. txq = ath5k_txq_setup(sc, AR5K_TX_QUEUE_DATA, AR5K_WME_AC_VO);
  2434. if (IS_ERR(txq)) {
  2435. ATH5K_ERR(sc, "can't setup xmit queue\n");
  2436. ret = PTR_ERR(txq);
  2437. goto err_queues;
  2438. }
  2439. txq = ath5k_txq_setup(sc, AR5K_TX_QUEUE_DATA, AR5K_WME_AC_VI);
  2440. if (IS_ERR(txq)) {
  2441. ATH5K_ERR(sc, "can't setup xmit queue\n");
  2442. ret = PTR_ERR(txq);
  2443. goto err_queues;
  2444. }
  2445. txq = ath5k_txq_setup(sc, AR5K_TX_QUEUE_DATA, AR5K_WME_AC_BE);
  2446. if (IS_ERR(txq)) {
  2447. ATH5K_ERR(sc, "can't setup xmit queue\n");
  2448. ret = PTR_ERR(txq);
  2449. goto err_queues;
  2450. }
  2451. txq = ath5k_txq_setup(sc, AR5K_TX_QUEUE_DATA, AR5K_WME_AC_BK);
  2452. if (IS_ERR(txq)) {
  2453. ATH5K_ERR(sc, "can't setup xmit queue\n");
  2454. ret = PTR_ERR(txq);
  2455. goto err_queues;
  2456. }
  2457. hw->queues = 4;
  2458. } else {
  2459. /* older hardware (5210) can only support one data queue */
  2460. txq = ath5k_txq_setup(sc, AR5K_TX_QUEUE_DATA, AR5K_WME_AC_BE);
  2461. if (IS_ERR(txq)) {
  2462. ATH5K_ERR(sc, "can't setup xmit queue\n");
  2463. ret = PTR_ERR(txq);
  2464. goto err_queues;
  2465. }
  2466. hw->queues = 1;
  2467. }
  2468. tasklet_init(&sc->rxtq, ath5k_tasklet_rx, (unsigned long)sc);
  2469. tasklet_init(&sc->txtq, ath5k_tasklet_tx, (unsigned long)sc);
  2470. tasklet_init(&sc->calib, ath5k_tasklet_calibrate, (unsigned long)sc);
  2471. tasklet_init(&sc->beacontq, ath5k_tasklet_beacon, (unsigned long)sc);
  2472. tasklet_init(&sc->ani_tasklet, ath5k_tasklet_ani, (unsigned long)sc);
  2473. INIT_WORK(&sc->reset_work, ath5k_reset_work);
  2474. INIT_DELAYED_WORK(&sc->tx_complete_work, ath5k_tx_complete_poll_work);
  2475. ret = ath5k_eeprom_read_mac(ah, mac);
  2476. if (ret) {
  2477. ATH5K_ERR(sc, "unable to read address from EEPROM\n");
  2478. goto err_queues;
  2479. }
  2480. SET_IEEE80211_PERM_ADDR(hw, mac);
  2481. memcpy(&sc->lladdr, mac, ETH_ALEN);
  2482. /* All MAC address bits matter for ACKs */
  2483. ath5k_update_bssid_mask_and_opmode(sc, NULL);
  2484. regulatory->current_rd = ah->ah_capabilities.cap_eeprom.ee_regdomain;
  2485. ret = ath_regd_init(regulatory, hw->wiphy, ath5k_reg_notifier);
  2486. if (ret) {
  2487. ATH5K_ERR(sc, "can't initialize regulatory system\n");
  2488. goto err_queues;
  2489. }
  2490. ret = ieee80211_register_hw(hw);
  2491. if (ret) {
  2492. ATH5K_ERR(sc, "can't register ieee80211 hw\n");
  2493. goto err_queues;
  2494. }
  2495. if (!ath_is_world_regd(regulatory))
  2496. regulatory_hint(hw->wiphy, regulatory->alpha2);
  2497. ath5k_init_leds(sc);
  2498. ath5k_sysfs_register(sc);
  2499. return 0;
  2500. err_queues:
  2501. ath5k_txq_release(sc);
  2502. err_bhal:
  2503. ath5k_hw_release_tx_queue(ah, sc->bhalq);
  2504. err_desc:
  2505. ath5k_desc_free(sc);
  2506. err:
  2507. return ret;
  2508. }
  2509. void
  2510. ath5k_deinit_softc(struct ath5k_softc *sc)
  2511. {
  2512. struct ieee80211_hw *hw = sc->hw;
  2513. /*
  2514. * NB: the order of these is important:
  2515. * o call the 802.11 layer before detaching ath5k_hw to
  2516. * ensure callbacks into the driver to delete global
  2517. * key cache entries can be handled
  2518. * o reclaim the tx queue data structures after calling
  2519. * the 802.11 layer as we'll get called back to reclaim
  2520. * node state and potentially want to use them
  2521. * o to cleanup the tx queues the hal is called, so detach
  2522. * it last
  2523. * XXX: ??? detach ath5k_hw ???
  2524. * Other than that, it's straightforward...
  2525. */
  2526. ath5k_debug_finish_device(sc);
  2527. ieee80211_unregister_hw(hw);
  2528. ath5k_desc_free(sc);
  2529. ath5k_txq_release(sc);
  2530. ath5k_hw_release_tx_queue(sc->ah, sc->bhalq);
  2531. ath5k_unregister_leds(sc);
  2532. ath5k_sysfs_unregister(sc);
  2533. /*
  2534. * NB: can't reclaim these until after ieee80211_ifdetach
  2535. * returns because we'll get called back to reclaim node
  2536. * state and potentially want to use them.
  2537. */
  2538. ath5k_hw_deinit(sc->ah);
  2539. free_irq(sc->irq, sc);
  2540. }
  2541. bool
  2542. ath_any_vif_assoc(struct ath5k_softc *sc)
  2543. {
  2544. struct ath_vif_iter_data iter_data;
  2545. iter_data.hw_macaddr = NULL;
  2546. iter_data.any_assoc = false;
  2547. iter_data.need_set_hw_addr = false;
  2548. iter_data.found_active = true;
  2549. ieee80211_iterate_active_interfaces_atomic(sc->hw, ath_vif_iter,
  2550. &iter_data);
  2551. return iter_data.any_assoc;
  2552. }
  2553. void
  2554. set_beacon_filter(struct ieee80211_hw *hw, bool enable)
  2555. {
  2556. struct ath5k_softc *sc = hw->priv;
  2557. struct ath5k_hw *ah = sc->ah;
  2558. u32 rfilt;
  2559. rfilt = ath5k_hw_get_rx_filter(ah);
  2560. if (enable)
  2561. rfilt |= AR5K_RX_FILTER_BEACON;
  2562. else
  2563. rfilt &= ~AR5K_RX_FILTER_BEACON;
  2564. ath5k_hw_set_rx_filter(ah, rfilt);
  2565. sc->filter_flags = rfilt;
  2566. }