base.c 80 KB

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