base.c 81 KB

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