base.c 82 KB

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