base.c 79 KB

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