base.c 81 KB

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