base.c 95 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549
  1. /*-
  2. * Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
  3. * Copyright (c) 2004-2005 Atheros Communications, Inc.
  4. * Copyright (c) 2006 Devicescape Software, Inc.
  5. * Copyright (c) 2007 Jiri Slaby <jirislaby@gmail.com>
  6. * Copyright (c) 2007 Luis R. Rodriguez <mcgrof@winlab.rutgers.edu>
  7. *
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer,
  15. * without modification.
  16. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  17. * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
  18. * redistribution must be conditioned upon including a substantially
  19. * similar Disclaimer requirement for further binary redistribution.
  20. * 3. Neither the names of the above-listed copyright holders nor the names
  21. * of any contributors may be used to endorse or promote products derived
  22. * from this software without specific prior written permission.
  23. *
  24. * Alternatively, this software may be distributed under the terms of the
  25. * GNU General Public License ("GPL") version 2 as published by the Free
  26. * Software Foundation.
  27. *
  28. * NO WARRANTY
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  30. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  31. * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
  32. * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
  33. * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
  34. * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  35. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  36. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  37. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  38. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  39. * THE POSSIBILITY OF SUCH DAMAGES.
  40. *
  41. */
  42. #include <linux/module.h>
  43. #include <linux/delay.h>
  44. #include <linux/hardirq.h>
  45. #include <linux/if.h>
  46. #include <linux/io.h>
  47. #include <linux/netdevice.h>
  48. #include <linux/cache.h>
  49. #include <linux/pci.h>
  50. #include <linux/pci-aspm.h>
  51. #include <linux/ethtool.h>
  52. #include <linux/uaccess.h>
  53. #include <linux/slab.h>
  54. #include <net/ieee80211_radiotap.h>
  55. #include <asm/unaligned.h>
  56. #include "base.h"
  57. #include "reg.h"
  58. #include "debug.h"
  59. #include "ani.h"
  60. static int modparam_nohwcrypt;
  61. module_param_named(nohwcrypt, modparam_nohwcrypt, bool, S_IRUGO);
  62. MODULE_PARM_DESC(nohwcrypt, "Disable hardware encryption.");
  63. static int modparam_all_channels;
  64. module_param_named(all_channels, modparam_all_channels, bool, S_IRUGO);
  65. MODULE_PARM_DESC(all_channels, "Expose all channels the device can use.");
  66. /******************\
  67. * Internal defines *
  68. \******************/
  69. /* Module info */
  70. MODULE_AUTHOR("Jiri Slaby");
  71. MODULE_AUTHOR("Nick Kossifidis");
  72. MODULE_DESCRIPTION("Support for 5xxx series of Atheros 802.11 wireless LAN cards.");
  73. MODULE_SUPPORTED_DEVICE("Atheros 5xxx WLAN cards");
  74. MODULE_LICENSE("Dual BSD/GPL");
  75. MODULE_VERSION("0.6.0 (EXPERIMENTAL)");
  76. /* Known PCI ids */
  77. static DEFINE_PCI_DEVICE_TABLE(ath5k_pci_id_table) = {
  78. { PCI_VDEVICE(ATHEROS, 0x0207) }, /* 5210 early */
  79. { PCI_VDEVICE(ATHEROS, 0x0007) }, /* 5210 */
  80. { PCI_VDEVICE(ATHEROS, 0x0011) }, /* 5311 - this is on AHB bus !*/
  81. { PCI_VDEVICE(ATHEROS, 0x0012) }, /* 5211 */
  82. { PCI_VDEVICE(ATHEROS, 0x0013) }, /* 5212 */
  83. { PCI_VDEVICE(3COM_2, 0x0013) }, /* 3com 5212 */
  84. { PCI_VDEVICE(3COM, 0x0013) }, /* 3com 3CRDAG675 5212 */
  85. { PCI_VDEVICE(ATHEROS, 0x1014) }, /* IBM minipci 5212 */
  86. { PCI_VDEVICE(ATHEROS, 0x0014) }, /* 5212 combatible */
  87. { PCI_VDEVICE(ATHEROS, 0x0015) }, /* 5212 combatible */
  88. { PCI_VDEVICE(ATHEROS, 0x0016) }, /* 5212 combatible */
  89. { PCI_VDEVICE(ATHEROS, 0x0017) }, /* 5212 combatible */
  90. { PCI_VDEVICE(ATHEROS, 0x0018) }, /* 5212 combatible */
  91. { PCI_VDEVICE(ATHEROS, 0x0019) }, /* 5212 combatible */
  92. { PCI_VDEVICE(ATHEROS, 0x001a) }, /* 2413 Griffin-lite */
  93. { PCI_VDEVICE(ATHEROS, 0x001b) }, /* 5413 Eagle */
  94. { PCI_VDEVICE(ATHEROS, 0x001c) }, /* PCI-E cards */
  95. { PCI_VDEVICE(ATHEROS, 0x001d) }, /* 2417 Nala */
  96. { 0 }
  97. };
  98. MODULE_DEVICE_TABLE(pci, ath5k_pci_id_table);
  99. /* Known SREVs */
  100. static const struct ath5k_srev_name srev_names[] = {
  101. { "5210", AR5K_VERSION_MAC, AR5K_SREV_AR5210 },
  102. { "5311", AR5K_VERSION_MAC, AR5K_SREV_AR5311 },
  103. { "5311A", AR5K_VERSION_MAC, AR5K_SREV_AR5311A },
  104. { "5311B", AR5K_VERSION_MAC, AR5K_SREV_AR5311B },
  105. { "5211", AR5K_VERSION_MAC, AR5K_SREV_AR5211 },
  106. { "5212", AR5K_VERSION_MAC, AR5K_SREV_AR5212 },
  107. { "5213", AR5K_VERSION_MAC, AR5K_SREV_AR5213 },
  108. { "5213A", AR5K_VERSION_MAC, AR5K_SREV_AR5213A },
  109. { "2413", AR5K_VERSION_MAC, AR5K_SREV_AR2413 },
  110. { "2414", AR5K_VERSION_MAC, AR5K_SREV_AR2414 },
  111. { "5424", AR5K_VERSION_MAC, AR5K_SREV_AR5424 },
  112. { "5413", AR5K_VERSION_MAC, AR5K_SREV_AR5413 },
  113. { "5414", AR5K_VERSION_MAC, AR5K_SREV_AR5414 },
  114. { "2415", AR5K_VERSION_MAC, AR5K_SREV_AR2415 },
  115. { "5416", AR5K_VERSION_MAC, AR5K_SREV_AR5416 },
  116. { "5418", AR5K_VERSION_MAC, AR5K_SREV_AR5418 },
  117. { "2425", AR5K_VERSION_MAC, AR5K_SREV_AR2425 },
  118. { "2417", AR5K_VERSION_MAC, AR5K_SREV_AR2417 },
  119. { "xxxxx", AR5K_VERSION_MAC, AR5K_SREV_UNKNOWN },
  120. { "5110", AR5K_VERSION_RAD, AR5K_SREV_RAD_5110 },
  121. { "5111", AR5K_VERSION_RAD, AR5K_SREV_RAD_5111 },
  122. { "5111A", AR5K_VERSION_RAD, AR5K_SREV_RAD_5111A },
  123. { "2111", AR5K_VERSION_RAD, AR5K_SREV_RAD_2111 },
  124. { "5112", AR5K_VERSION_RAD, AR5K_SREV_RAD_5112 },
  125. { "5112A", AR5K_VERSION_RAD, AR5K_SREV_RAD_5112A },
  126. { "5112B", AR5K_VERSION_RAD, AR5K_SREV_RAD_5112B },
  127. { "2112", AR5K_VERSION_RAD, AR5K_SREV_RAD_2112 },
  128. { "2112A", AR5K_VERSION_RAD, AR5K_SREV_RAD_2112A },
  129. { "2112B", AR5K_VERSION_RAD, AR5K_SREV_RAD_2112B },
  130. { "2413", AR5K_VERSION_RAD, AR5K_SREV_RAD_2413 },
  131. { "5413", AR5K_VERSION_RAD, AR5K_SREV_RAD_5413 },
  132. { "2316", AR5K_VERSION_RAD, AR5K_SREV_RAD_2316 },
  133. { "2317", AR5K_VERSION_RAD, AR5K_SREV_RAD_2317 },
  134. { "5424", AR5K_VERSION_RAD, AR5K_SREV_RAD_5424 },
  135. { "5133", AR5K_VERSION_RAD, AR5K_SREV_RAD_5133 },
  136. { "xxxxx", AR5K_VERSION_RAD, AR5K_SREV_UNKNOWN },
  137. };
  138. static const struct ieee80211_rate ath5k_rates[] = {
  139. { .bitrate = 10,
  140. .hw_value = ATH5K_RATE_CODE_1M, },
  141. { .bitrate = 20,
  142. .hw_value = ATH5K_RATE_CODE_2M,
  143. .hw_value_short = ATH5K_RATE_CODE_2M | AR5K_SET_SHORT_PREAMBLE,
  144. .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  145. { .bitrate = 55,
  146. .hw_value = ATH5K_RATE_CODE_5_5M,
  147. .hw_value_short = ATH5K_RATE_CODE_5_5M | AR5K_SET_SHORT_PREAMBLE,
  148. .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  149. { .bitrate = 110,
  150. .hw_value = ATH5K_RATE_CODE_11M,
  151. .hw_value_short = ATH5K_RATE_CODE_11M | AR5K_SET_SHORT_PREAMBLE,
  152. .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  153. { .bitrate = 60,
  154. .hw_value = ATH5K_RATE_CODE_6M,
  155. .flags = 0 },
  156. { .bitrate = 90,
  157. .hw_value = ATH5K_RATE_CODE_9M,
  158. .flags = 0 },
  159. { .bitrate = 120,
  160. .hw_value = ATH5K_RATE_CODE_12M,
  161. .flags = 0 },
  162. { .bitrate = 180,
  163. .hw_value = ATH5K_RATE_CODE_18M,
  164. .flags = 0 },
  165. { .bitrate = 240,
  166. .hw_value = ATH5K_RATE_CODE_24M,
  167. .flags = 0 },
  168. { .bitrate = 360,
  169. .hw_value = ATH5K_RATE_CODE_36M,
  170. .flags = 0 },
  171. { .bitrate = 480,
  172. .hw_value = ATH5K_RATE_CODE_48M,
  173. .flags = 0 },
  174. { .bitrate = 540,
  175. .hw_value = ATH5K_RATE_CODE_54M,
  176. .flags = 0 },
  177. /* XR missing */
  178. };
  179. /*
  180. * Prototypes - PCI stack related functions
  181. */
  182. static int __devinit ath5k_pci_probe(struct pci_dev *pdev,
  183. const struct pci_device_id *id);
  184. static void __devexit ath5k_pci_remove(struct pci_dev *pdev);
  185. #ifdef CONFIG_PM_SLEEP
  186. static int ath5k_pci_suspend(struct device *dev);
  187. static int ath5k_pci_resume(struct device *dev);
  188. static SIMPLE_DEV_PM_OPS(ath5k_pm_ops, ath5k_pci_suspend, ath5k_pci_resume);
  189. #define ATH5K_PM_OPS (&ath5k_pm_ops)
  190. #else
  191. #define ATH5K_PM_OPS NULL
  192. #endif /* CONFIG_PM_SLEEP */
  193. static struct pci_driver ath5k_pci_driver = {
  194. .name = KBUILD_MODNAME,
  195. .id_table = ath5k_pci_id_table,
  196. .probe = ath5k_pci_probe,
  197. .remove = __devexit_p(ath5k_pci_remove),
  198. .driver.pm = ATH5K_PM_OPS,
  199. };
  200. /*
  201. * Prototypes - MAC 802.11 stack related functions
  202. */
  203. static int ath5k_tx(struct ieee80211_hw *hw, struct sk_buff *skb);
  204. static int ath5k_tx_queue(struct ieee80211_hw *hw, struct sk_buff *skb,
  205. struct ath5k_txq *txq);
  206. static int ath5k_reset(struct ath5k_softc *sc, struct ieee80211_channel *chan);
  207. static int ath5k_start(struct ieee80211_hw *hw);
  208. static void ath5k_stop(struct ieee80211_hw *hw);
  209. static int ath5k_add_interface(struct ieee80211_hw *hw,
  210. struct ieee80211_vif *vif);
  211. static void ath5k_remove_interface(struct ieee80211_hw *hw,
  212. struct ieee80211_vif *vif);
  213. static int ath5k_config(struct ieee80211_hw *hw, u32 changed);
  214. static u64 ath5k_prepare_multicast(struct ieee80211_hw *hw,
  215. struct netdev_hw_addr_list *mc_list);
  216. static void ath5k_configure_filter(struct ieee80211_hw *hw,
  217. unsigned int changed_flags,
  218. unsigned int *new_flags,
  219. u64 multicast);
  220. static int ath5k_set_key(struct ieee80211_hw *hw,
  221. enum set_key_cmd cmd,
  222. struct ieee80211_vif *vif, struct ieee80211_sta *sta,
  223. struct ieee80211_key_conf *key);
  224. static int ath5k_get_stats(struct ieee80211_hw *hw,
  225. struct ieee80211_low_level_stats *stats);
  226. static int ath5k_get_survey(struct ieee80211_hw *hw,
  227. int idx, struct survey_info *survey);
  228. static u64 ath5k_get_tsf(struct ieee80211_hw *hw);
  229. static void ath5k_set_tsf(struct ieee80211_hw *hw, u64 tsf);
  230. static void ath5k_reset_tsf(struct ieee80211_hw *hw);
  231. static int ath5k_beacon_update(struct ieee80211_hw *hw,
  232. struct ieee80211_vif *vif);
  233. static void ath5k_bss_info_changed(struct ieee80211_hw *hw,
  234. struct ieee80211_vif *vif,
  235. struct ieee80211_bss_conf *bss_conf,
  236. u32 changes);
  237. static void ath5k_sw_scan_start(struct ieee80211_hw *hw);
  238. static void ath5k_sw_scan_complete(struct ieee80211_hw *hw);
  239. static void ath5k_set_coverage_class(struct ieee80211_hw *hw,
  240. u8 coverage_class);
  241. static const struct ieee80211_ops ath5k_hw_ops = {
  242. .tx = ath5k_tx,
  243. .start = ath5k_start,
  244. .stop = ath5k_stop,
  245. .add_interface = ath5k_add_interface,
  246. .remove_interface = ath5k_remove_interface,
  247. .config = ath5k_config,
  248. .prepare_multicast = ath5k_prepare_multicast,
  249. .configure_filter = ath5k_configure_filter,
  250. .set_key = ath5k_set_key,
  251. .get_stats = ath5k_get_stats,
  252. .get_survey = ath5k_get_survey,
  253. .conf_tx = NULL,
  254. .get_tsf = ath5k_get_tsf,
  255. .set_tsf = ath5k_set_tsf,
  256. .reset_tsf = ath5k_reset_tsf,
  257. .bss_info_changed = ath5k_bss_info_changed,
  258. .sw_scan_start = ath5k_sw_scan_start,
  259. .sw_scan_complete = ath5k_sw_scan_complete,
  260. .set_coverage_class = ath5k_set_coverage_class,
  261. };
  262. /*
  263. * Prototypes - Internal functions
  264. */
  265. /* Attach detach */
  266. static int ath5k_attach(struct pci_dev *pdev,
  267. struct ieee80211_hw *hw);
  268. static void ath5k_detach(struct pci_dev *pdev,
  269. struct ieee80211_hw *hw);
  270. /* Channel/mode setup */
  271. static inline short ath5k_ieee2mhz(short chan);
  272. static unsigned int ath5k_copy_channels(struct ath5k_hw *ah,
  273. struct ieee80211_channel *channels,
  274. unsigned int mode,
  275. unsigned int max);
  276. static int ath5k_setup_bands(struct ieee80211_hw *hw);
  277. static int ath5k_chan_set(struct ath5k_softc *sc,
  278. struct ieee80211_channel *chan);
  279. static void ath5k_setcurmode(struct ath5k_softc *sc,
  280. unsigned int mode);
  281. static void ath5k_mode_setup(struct ath5k_softc *sc);
  282. /* Descriptor setup */
  283. static int ath5k_desc_alloc(struct ath5k_softc *sc,
  284. struct pci_dev *pdev);
  285. static void ath5k_desc_free(struct ath5k_softc *sc,
  286. struct pci_dev *pdev);
  287. /* Buffers setup */
  288. static int ath5k_rxbuf_setup(struct ath5k_softc *sc,
  289. struct ath5k_buf *bf);
  290. static int ath5k_txbuf_setup(struct ath5k_softc *sc,
  291. struct ath5k_buf *bf,
  292. struct ath5k_txq *txq, int padsize);
  293. static inline void ath5k_txbuf_free_skb(struct ath5k_softc *sc,
  294. struct ath5k_buf *bf)
  295. {
  296. BUG_ON(!bf);
  297. if (!bf->skb)
  298. return;
  299. pci_unmap_single(sc->pdev, bf->skbaddr, bf->skb->len,
  300. PCI_DMA_TODEVICE);
  301. dev_kfree_skb_any(bf->skb);
  302. bf->skb = NULL;
  303. bf->skbaddr = 0;
  304. bf->desc->ds_data = 0;
  305. }
  306. static inline void ath5k_rxbuf_free_skb(struct ath5k_softc *sc,
  307. struct ath5k_buf *bf)
  308. {
  309. struct ath5k_hw *ah = sc->ah;
  310. struct ath_common *common = ath5k_hw_common(ah);
  311. BUG_ON(!bf);
  312. if (!bf->skb)
  313. return;
  314. pci_unmap_single(sc->pdev, bf->skbaddr, common->rx_bufsize,
  315. PCI_DMA_FROMDEVICE);
  316. dev_kfree_skb_any(bf->skb);
  317. bf->skb = NULL;
  318. bf->skbaddr = 0;
  319. bf->desc->ds_data = 0;
  320. }
  321. /* Queues setup */
  322. static struct ath5k_txq *ath5k_txq_setup(struct ath5k_softc *sc,
  323. int qtype, int subtype);
  324. static int ath5k_beaconq_setup(struct ath5k_hw *ah);
  325. static int ath5k_beaconq_config(struct ath5k_softc *sc);
  326. static void ath5k_txq_drainq(struct ath5k_softc *sc,
  327. struct ath5k_txq *txq);
  328. static void ath5k_txq_cleanup(struct ath5k_softc *sc);
  329. static void ath5k_txq_release(struct ath5k_softc *sc);
  330. /* Rx handling */
  331. static int ath5k_rx_start(struct ath5k_softc *sc);
  332. static void ath5k_rx_stop(struct ath5k_softc *sc);
  333. static unsigned int ath5k_rx_decrypted(struct ath5k_softc *sc,
  334. struct sk_buff *skb,
  335. struct ath5k_rx_status *rs);
  336. static void ath5k_tasklet_rx(unsigned long data);
  337. /* Tx handling */
  338. static void ath5k_tx_processq(struct ath5k_softc *sc,
  339. struct ath5k_txq *txq);
  340. static void ath5k_tasklet_tx(unsigned long data);
  341. /* Beacon handling */
  342. static int ath5k_beacon_setup(struct ath5k_softc *sc,
  343. struct ath5k_buf *bf);
  344. static void ath5k_beacon_send(struct ath5k_softc *sc);
  345. static void ath5k_beacon_config(struct ath5k_softc *sc);
  346. static void ath5k_beacon_update_timers(struct ath5k_softc *sc, u64 bc_tsf);
  347. static void ath5k_tasklet_beacon(unsigned long data);
  348. static void ath5k_tasklet_ani(unsigned long data);
  349. static inline u64 ath5k_extend_tsf(struct ath5k_hw *ah, u32 rstamp)
  350. {
  351. u64 tsf = ath5k_hw_get_tsf64(ah);
  352. if ((tsf & 0x7fff) < rstamp)
  353. tsf -= 0x8000;
  354. return (tsf & ~0x7fff) | rstamp;
  355. }
  356. /* Interrupt handling */
  357. static int ath5k_init(struct ath5k_softc *sc);
  358. static int ath5k_stop_locked(struct ath5k_softc *sc);
  359. static int ath5k_stop_hw(struct ath5k_softc *sc);
  360. static irqreturn_t ath5k_intr(int irq, void *dev_id);
  361. static void ath5k_reset_work(struct work_struct *work);
  362. static void ath5k_tasklet_calibrate(unsigned long data);
  363. /*
  364. * Module init/exit functions
  365. */
  366. static int __init
  367. init_ath5k_pci(void)
  368. {
  369. int ret;
  370. ath5k_debug_init();
  371. ret = pci_register_driver(&ath5k_pci_driver);
  372. if (ret) {
  373. printk(KERN_ERR "ath5k_pci: can't register pci driver\n");
  374. return ret;
  375. }
  376. return 0;
  377. }
  378. static void __exit
  379. exit_ath5k_pci(void)
  380. {
  381. pci_unregister_driver(&ath5k_pci_driver);
  382. ath5k_debug_finish();
  383. }
  384. module_init(init_ath5k_pci);
  385. module_exit(exit_ath5k_pci);
  386. /********************\
  387. * PCI Initialization *
  388. \********************/
  389. static const char *
  390. ath5k_chip_name(enum ath5k_srev_type type, u_int16_t val)
  391. {
  392. const char *name = "xxxxx";
  393. unsigned int i;
  394. for (i = 0; i < ARRAY_SIZE(srev_names); i++) {
  395. if (srev_names[i].sr_type != type)
  396. continue;
  397. if ((val & 0xf0) == srev_names[i].sr_val)
  398. name = srev_names[i].sr_name;
  399. if ((val & 0xff) == srev_names[i].sr_val) {
  400. name = srev_names[i].sr_name;
  401. break;
  402. }
  403. }
  404. return name;
  405. }
  406. static unsigned int ath5k_ioread32(void *hw_priv, u32 reg_offset)
  407. {
  408. struct ath5k_hw *ah = (struct ath5k_hw *) hw_priv;
  409. return ath5k_hw_reg_read(ah, reg_offset);
  410. }
  411. static void ath5k_iowrite32(void *hw_priv, u32 val, u32 reg_offset)
  412. {
  413. struct ath5k_hw *ah = (struct ath5k_hw *) hw_priv;
  414. ath5k_hw_reg_write(ah, val, reg_offset);
  415. }
  416. static const struct ath_ops ath5k_common_ops = {
  417. .read = ath5k_ioread32,
  418. .write = ath5k_iowrite32,
  419. };
  420. static int __devinit
  421. ath5k_pci_probe(struct pci_dev *pdev,
  422. const struct pci_device_id *id)
  423. {
  424. void __iomem *mem;
  425. struct ath5k_softc *sc;
  426. struct ath_common *common;
  427. struct ieee80211_hw *hw;
  428. int ret;
  429. u8 csz;
  430. /*
  431. * L0s needs to be disabled on all ath5k cards.
  432. *
  433. * For distributions shipping with CONFIG_PCIEASPM (this will be enabled
  434. * by default in the future in 2.6.36) this will also mean both L1 and
  435. * L0s will be disabled when a pre 1.1 PCIe device is detected. We do
  436. * know L1 works correctly even for all ath5k pre 1.1 PCIe devices
  437. * though but cannot currently undue the effect of a blacklist, for
  438. * details you can read pcie_aspm_sanity_check() and see how it adjusts
  439. * the device link capability.
  440. *
  441. * It may be possible in the future to implement some PCI API to allow
  442. * drivers to override blacklists for pre 1.1 PCIe but for now it is
  443. * best to accept that both L0s and L1 will be disabled completely for
  444. * distributions shipping with CONFIG_PCIEASPM rather than having this
  445. * issue present. Motivation for adding this new API will be to help
  446. * with power consumption for some of these devices.
  447. */
  448. pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S);
  449. ret = pci_enable_device(pdev);
  450. if (ret) {
  451. dev_err(&pdev->dev, "can't enable device\n");
  452. goto err;
  453. }
  454. /* XXX 32-bit addressing only */
  455. ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  456. if (ret) {
  457. dev_err(&pdev->dev, "32-bit DMA not available\n");
  458. goto err_dis;
  459. }
  460. /*
  461. * Cache line size is used to size and align various
  462. * structures used to communicate with the hardware.
  463. */
  464. pci_read_config_byte(pdev, PCI_CACHE_LINE_SIZE, &csz);
  465. if (csz == 0) {
  466. /*
  467. * Linux 2.4.18 (at least) writes the cache line size
  468. * register as a 16-bit wide register which is wrong.
  469. * We must have this setup properly for rx buffer
  470. * DMA to work so force a reasonable value here if it
  471. * comes up zero.
  472. */
  473. csz = L1_CACHE_BYTES >> 2;
  474. pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, csz);
  475. }
  476. /*
  477. * The default setting of latency timer yields poor results,
  478. * set it to the value used by other systems. It may be worth
  479. * tweaking this setting more.
  480. */
  481. pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0xa8);
  482. /* Enable bus mastering */
  483. pci_set_master(pdev);
  484. /*
  485. * Disable the RETRY_TIMEOUT register (0x41) to keep
  486. * PCI Tx retries from interfering with C3 CPU state.
  487. */
  488. pci_write_config_byte(pdev, 0x41, 0);
  489. ret = pci_request_region(pdev, 0, "ath5k");
  490. if (ret) {
  491. dev_err(&pdev->dev, "cannot reserve PCI memory region\n");
  492. goto err_dis;
  493. }
  494. mem = pci_iomap(pdev, 0, 0);
  495. if (!mem) {
  496. dev_err(&pdev->dev, "cannot remap PCI memory region\n") ;
  497. ret = -EIO;
  498. goto err_reg;
  499. }
  500. /*
  501. * Allocate hw (mac80211 main struct)
  502. * and hw->priv (driver private data)
  503. */
  504. hw = ieee80211_alloc_hw(sizeof(*sc), &ath5k_hw_ops);
  505. if (hw == NULL) {
  506. dev_err(&pdev->dev, "cannot allocate ieee80211_hw\n");
  507. ret = -ENOMEM;
  508. goto err_map;
  509. }
  510. dev_info(&pdev->dev, "registered as '%s'\n", wiphy_name(hw->wiphy));
  511. /* Initialize driver private data */
  512. SET_IEEE80211_DEV(hw, &pdev->dev);
  513. hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
  514. IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
  515. IEEE80211_HW_SIGNAL_DBM;
  516. hw->wiphy->interface_modes =
  517. BIT(NL80211_IFTYPE_AP) |
  518. BIT(NL80211_IFTYPE_STATION) |
  519. BIT(NL80211_IFTYPE_ADHOC) |
  520. BIT(NL80211_IFTYPE_MESH_POINT);
  521. hw->extra_tx_headroom = 2;
  522. hw->channel_change_time = 5000;
  523. sc = hw->priv;
  524. sc->hw = hw;
  525. sc->pdev = pdev;
  526. ath5k_debug_init_device(sc);
  527. /*
  528. * Mark the device as detached to avoid processing
  529. * interrupts until setup is complete.
  530. */
  531. __set_bit(ATH_STAT_INVALID, sc->status);
  532. sc->iobase = mem; /* So we can unmap it on detach */
  533. sc->opmode = NL80211_IFTYPE_STATION;
  534. sc->bintval = 1000;
  535. mutex_init(&sc->lock);
  536. spin_lock_init(&sc->rxbuflock);
  537. spin_lock_init(&sc->txbuflock);
  538. spin_lock_init(&sc->block);
  539. /* Set private data */
  540. pci_set_drvdata(pdev, sc);
  541. /* Setup interrupt handler */
  542. ret = request_irq(pdev->irq, ath5k_intr, IRQF_SHARED, "ath", sc);
  543. if (ret) {
  544. ATH5K_ERR(sc, "request_irq failed\n");
  545. goto err_free;
  546. }
  547. /*If we passed the test malloc a ath5k_hw struct*/
  548. sc->ah = kzalloc(sizeof(struct ath5k_hw), GFP_KERNEL);
  549. if (!sc->ah) {
  550. ret = -ENOMEM;
  551. ATH5K_ERR(sc, "out of memory\n");
  552. goto err_irq;
  553. }
  554. sc->ah->ah_sc = sc;
  555. sc->ah->ah_iobase = sc->iobase;
  556. common = ath5k_hw_common(sc->ah);
  557. common->ops = &ath5k_common_ops;
  558. common->ah = sc->ah;
  559. common->hw = hw;
  560. common->cachelsz = csz << 2; /* convert to bytes */
  561. /* Initialize device */
  562. ret = ath5k_hw_attach(sc);
  563. if (ret) {
  564. goto err_free_ah;
  565. }
  566. /* set up multi-rate retry capabilities */
  567. if (sc->ah->ah_version == AR5K_AR5212) {
  568. hw->max_rates = 4;
  569. hw->max_rate_tries = 11;
  570. }
  571. /* Finish private driver data initialization */
  572. ret = ath5k_attach(pdev, hw);
  573. if (ret)
  574. goto err_ah;
  575. ATH5K_INFO(sc, "Atheros AR%s chip found (MAC: 0x%x, PHY: 0x%x)\n",
  576. ath5k_chip_name(AR5K_VERSION_MAC, sc->ah->ah_mac_srev),
  577. sc->ah->ah_mac_srev,
  578. sc->ah->ah_phy_revision);
  579. if (!sc->ah->ah_single_chip) {
  580. /* Single chip radio (!RF5111) */
  581. if (sc->ah->ah_radio_5ghz_revision &&
  582. !sc->ah->ah_radio_2ghz_revision) {
  583. /* No 5GHz support -> report 2GHz radio */
  584. if (!test_bit(AR5K_MODE_11A,
  585. sc->ah->ah_capabilities.cap_mode)) {
  586. ATH5K_INFO(sc, "RF%s 2GHz radio found (0x%x)\n",
  587. ath5k_chip_name(AR5K_VERSION_RAD,
  588. sc->ah->ah_radio_5ghz_revision),
  589. sc->ah->ah_radio_5ghz_revision);
  590. /* No 2GHz support (5110 and some
  591. * 5Ghz only cards) -> report 5Ghz radio */
  592. } else if (!test_bit(AR5K_MODE_11B,
  593. sc->ah->ah_capabilities.cap_mode)) {
  594. ATH5K_INFO(sc, "RF%s 5GHz radio found (0x%x)\n",
  595. ath5k_chip_name(AR5K_VERSION_RAD,
  596. sc->ah->ah_radio_5ghz_revision),
  597. sc->ah->ah_radio_5ghz_revision);
  598. /* Multiband radio */
  599. } else {
  600. ATH5K_INFO(sc, "RF%s multiband radio found"
  601. " (0x%x)\n",
  602. ath5k_chip_name(AR5K_VERSION_RAD,
  603. sc->ah->ah_radio_5ghz_revision),
  604. sc->ah->ah_radio_5ghz_revision);
  605. }
  606. }
  607. /* Multi chip radio (RF5111 - RF2111) ->
  608. * report both 2GHz/5GHz radios */
  609. else if (sc->ah->ah_radio_5ghz_revision &&
  610. sc->ah->ah_radio_2ghz_revision){
  611. ATH5K_INFO(sc, "RF%s 5GHz radio found (0x%x)\n",
  612. ath5k_chip_name(AR5K_VERSION_RAD,
  613. sc->ah->ah_radio_5ghz_revision),
  614. sc->ah->ah_radio_5ghz_revision);
  615. ATH5K_INFO(sc, "RF%s 2GHz radio found (0x%x)\n",
  616. ath5k_chip_name(AR5K_VERSION_RAD,
  617. sc->ah->ah_radio_2ghz_revision),
  618. sc->ah->ah_radio_2ghz_revision);
  619. }
  620. }
  621. /* ready to process interrupts */
  622. __clear_bit(ATH_STAT_INVALID, sc->status);
  623. return 0;
  624. err_ah:
  625. ath5k_hw_detach(sc->ah);
  626. err_irq:
  627. free_irq(pdev->irq, sc);
  628. err_free_ah:
  629. kfree(sc->ah);
  630. err_free:
  631. ieee80211_free_hw(hw);
  632. err_map:
  633. pci_iounmap(pdev, mem);
  634. err_reg:
  635. pci_release_region(pdev, 0);
  636. err_dis:
  637. pci_disable_device(pdev);
  638. err:
  639. return ret;
  640. }
  641. static void __devexit
  642. ath5k_pci_remove(struct pci_dev *pdev)
  643. {
  644. struct ath5k_softc *sc = pci_get_drvdata(pdev);
  645. ath5k_debug_finish_device(sc);
  646. ath5k_detach(pdev, sc->hw);
  647. ath5k_hw_detach(sc->ah);
  648. kfree(sc->ah);
  649. free_irq(pdev->irq, sc);
  650. pci_iounmap(pdev, sc->iobase);
  651. pci_release_region(pdev, 0);
  652. pci_disable_device(pdev);
  653. ieee80211_free_hw(sc->hw);
  654. }
  655. #ifdef CONFIG_PM_SLEEP
  656. static int ath5k_pci_suspend(struct device *dev)
  657. {
  658. struct ath5k_softc *sc = pci_get_drvdata(to_pci_dev(dev));
  659. ath5k_led_off(sc);
  660. return 0;
  661. }
  662. static int ath5k_pci_resume(struct device *dev)
  663. {
  664. struct pci_dev *pdev = to_pci_dev(dev);
  665. struct ath5k_softc *sc = pci_get_drvdata(pdev);
  666. /*
  667. * Suspend/Resume resets the PCI configuration space, so we have to
  668. * re-disable the RETRY_TIMEOUT register (0x41) to keep
  669. * PCI Tx retries from interfering with C3 CPU state
  670. */
  671. pci_write_config_byte(pdev, 0x41, 0);
  672. ath5k_led_enable(sc);
  673. return 0;
  674. }
  675. #endif /* CONFIG_PM_SLEEP */
  676. /***********************\
  677. * Driver Initialization *
  678. \***********************/
  679. static int ath5k_reg_notifier(struct wiphy *wiphy, struct regulatory_request *request)
  680. {
  681. struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
  682. struct ath5k_softc *sc = hw->priv;
  683. struct ath_regulatory *regulatory = ath5k_hw_regulatory(sc->ah);
  684. return ath_reg_notifier_apply(wiphy, request, regulatory);
  685. }
  686. static int
  687. ath5k_attach(struct pci_dev *pdev, struct ieee80211_hw *hw)
  688. {
  689. struct ath5k_softc *sc = hw->priv;
  690. struct ath5k_hw *ah = sc->ah;
  691. struct ath_regulatory *regulatory = ath5k_hw_regulatory(ah);
  692. u8 mac[ETH_ALEN] = {};
  693. int ret;
  694. ATH5K_DBG(sc, ATH5K_DEBUG_ANY, "devid 0x%x\n", pdev->device);
  695. /*
  696. * Check if the MAC has multi-rate retry support.
  697. * We do this by trying to setup a fake extended
  698. * descriptor. MAC's that don't have support will
  699. * return false w/o doing anything. MAC's that do
  700. * support it will return true w/o doing anything.
  701. */
  702. ret = ath5k_hw_setup_mrr_tx_desc(ah, NULL, 0, 0, 0, 0, 0, 0);
  703. if (ret < 0)
  704. goto err;
  705. if (ret > 0)
  706. __set_bit(ATH_STAT_MRRETRY, sc->status);
  707. /*
  708. * Collect the channel list. The 802.11 layer
  709. * is resposible for filtering this list based
  710. * on settings like the phy mode and regulatory
  711. * domain restrictions.
  712. */
  713. ret = ath5k_setup_bands(hw);
  714. if (ret) {
  715. ATH5K_ERR(sc, "can't get channels\n");
  716. goto err;
  717. }
  718. /* NB: setup here so ath5k_rate_update is happy */
  719. if (test_bit(AR5K_MODE_11A, ah->ah_modes))
  720. ath5k_setcurmode(sc, AR5K_MODE_11A);
  721. else
  722. ath5k_setcurmode(sc, AR5K_MODE_11B);
  723. /*
  724. * Allocate tx+rx descriptors and populate the lists.
  725. */
  726. ret = ath5k_desc_alloc(sc, pdev);
  727. if (ret) {
  728. ATH5K_ERR(sc, "can't allocate descriptors\n");
  729. goto err;
  730. }
  731. /*
  732. * Allocate hardware transmit queues: one queue for
  733. * beacon frames and one data queue for each QoS
  734. * priority. Note that hw functions handle reseting
  735. * these queues at the needed time.
  736. */
  737. ret = ath5k_beaconq_setup(ah);
  738. if (ret < 0) {
  739. ATH5K_ERR(sc, "can't setup a beacon xmit queue\n");
  740. goto err_desc;
  741. }
  742. sc->bhalq = ret;
  743. sc->cabq = ath5k_txq_setup(sc, AR5K_TX_QUEUE_CAB, 0);
  744. if (IS_ERR(sc->cabq)) {
  745. ATH5K_ERR(sc, "can't setup cab queue\n");
  746. ret = PTR_ERR(sc->cabq);
  747. goto err_bhal;
  748. }
  749. sc->txq = ath5k_txq_setup(sc, AR5K_TX_QUEUE_DATA, AR5K_WME_AC_BK);
  750. if (IS_ERR(sc->txq)) {
  751. ATH5K_ERR(sc, "can't setup xmit queue\n");
  752. ret = PTR_ERR(sc->txq);
  753. goto err_queues;
  754. }
  755. tasklet_init(&sc->rxtq, ath5k_tasklet_rx, (unsigned long)sc);
  756. tasklet_init(&sc->txtq, ath5k_tasklet_tx, (unsigned long)sc);
  757. tasklet_init(&sc->calib, ath5k_tasklet_calibrate, (unsigned long)sc);
  758. tasklet_init(&sc->beacontq, ath5k_tasklet_beacon, (unsigned long)sc);
  759. tasklet_init(&sc->ani_tasklet, ath5k_tasklet_ani, (unsigned long)sc);
  760. INIT_WORK(&sc->reset_work, ath5k_reset_work);
  761. ret = ath5k_eeprom_read_mac(ah, mac);
  762. if (ret) {
  763. ATH5K_ERR(sc, "unable to read address from EEPROM: 0x%04x\n",
  764. sc->pdev->device);
  765. goto err_queues;
  766. }
  767. SET_IEEE80211_PERM_ADDR(hw, mac);
  768. /* All MAC address bits matter for ACKs */
  769. memcpy(sc->bssidmask, ath_bcast_mac, ETH_ALEN);
  770. ath5k_hw_set_bssid_mask(sc->ah, sc->bssidmask);
  771. regulatory->current_rd = ah->ah_capabilities.cap_eeprom.ee_regdomain;
  772. ret = ath_regd_init(regulatory, hw->wiphy, ath5k_reg_notifier);
  773. if (ret) {
  774. ATH5K_ERR(sc, "can't initialize regulatory system\n");
  775. goto err_queues;
  776. }
  777. ret = ieee80211_register_hw(hw);
  778. if (ret) {
  779. ATH5K_ERR(sc, "can't register ieee80211 hw\n");
  780. goto err_queues;
  781. }
  782. if (!ath_is_world_regd(regulatory))
  783. regulatory_hint(hw->wiphy, regulatory->alpha2);
  784. ath5k_init_leds(sc);
  785. ath5k_sysfs_register(sc);
  786. return 0;
  787. err_queues:
  788. ath5k_txq_release(sc);
  789. err_bhal:
  790. ath5k_hw_release_tx_queue(ah, sc->bhalq);
  791. err_desc:
  792. ath5k_desc_free(sc, pdev);
  793. err:
  794. return ret;
  795. }
  796. static void
  797. ath5k_detach(struct pci_dev *pdev, struct ieee80211_hw *hw)
  798. {
  799. struct ath5k_softc *sc = hw->priv;
  800. /*
  801. * NB: the order of these is important:
  802. * o call the 802.11 layer before detaching ath5k_hw to
  803. * insure callbacks into the driver to delete global
  804. * key cache entries can be handled
  805. * o reclaim the tx queue data structures after calling
  806. * the 802.11 layer as we'll get called back to reclaim
  807. * node state and potentially want to use them
  808. * o to cleanup the tx queues the hal is called, so detach
  809. * it last
  810. * XXX: ??? detach ath5k_hw ???
  811. * Other than that, it's straightforward...
  812. */
  813. ieee80211_unregister_hw(hw);
  814. ath5k_desc_free(sc, pdev);
  815. ath5k_txq_release(sc);
  816. ath5k_hw_release_tx_queue(sc->ah, sc->bhalq);
  817. ath5k_unregister_leds(sc);
  818. ath5k_sysfs_unregister(sc);
  819. /*
  820. * NB: can't reclaim these until after ieee80211_ifdetach
  821. * returns because we'll get called back to reclaim node
  822. * state and potentially want to use them.
  823. */
  824. }
  825. /********************\
  826. * Channel/mode setup *
  827. \********************/
  828. /*
  829. * Convert IEEE channel number to MHz frequency.
  830. */
  831. static inline short
  832. ath5k_ieee2mhz(short chan)
  833. {
  834. if (chan <= 14 || chan >= 27)
  835. return ieee80211chan2mhz(chan);
  836. else
  837. return 2212 + chan * 20;
  838. }
  839. /*
  840. * Returns true for the channel numbers used without all_channels modparam.
  841. */
  842. static bool ath5k_is_standard_channel(short chan)
  843. {
  844. return ((chan <= 14) ||
  845. /* UNII 1,2 */
  846. ((chan & 3) == 0 && chan >= 36 && chan <= 64) ||
  847. /* midband */
  848. ((chan & 3) == 0 && chan >= 100 && chan <= 140) ||
  849. /* UNII-3 */
  850. ((chan & 3) == 1 && chan >= 149 && chan <= 165));
  851. }
  852. static unsigned int
  853. ath5k_copy_channels(struct ath5k_hw *ah,
  854. struct ieee80211_channel *channels,
  855. unsigned int mode,
  856. unsigned int max)
  857. {
  858. unsigned int i, count, size, chfreq, freq, ch;
  859. if (!test_bit(mode, ah->ah_modes))
  860. return 0;
  861. switch (mode) {
  862. case AR5K_MODE_11A:
  863. case AR5K_MODE_11A_TURBO:
  864. /* 1..220, but 2GHz frequencies are filtered by check_channel */
  865. size = 220 ;
  866. chfreq = CHANNEL_5GHZ;
  867. break;
  868. case AR5K_MODE_11B:
  869. case AR5K_MODE_11G:
  870. case AR5K_MODE_11G_TURBO:
  871. size = 26;
  872. chfreq = CHANNEL_2GHZ;
  873. break;
  874. default:
  875. ATH5K_WARN(ah->ah_sc, "bad mode, not copying channels\n");
  876. return 0;
  877. }
  878. for (i = 0, count = 0; i < size && max > 0; i++) {
  879. ch = i + 1 ;
  880. freq = ath5k_ieee2mhz(ch);
  881. /* Check if channel is supported by the chipset */
  882. if (!ath5k_channel_ok(ah, freq, chfreq))
  883. continue;
  884. if (!modparam_all_channels && !ath5k_is_standard_channel(ch))
  885. continue;
  886. /* Write channel info and increment counter */
  887. channels[count].center_freq = freq;
  888. channels[count].band = (chfreq == CHANNEL_2GHZ) ?
  889. IEEE80211_BAND_2GHZ : IEEE80211_BAND_5GHZ;
  890. switch (mode) {
  891. case AR5K_MODE_11A:
  892. case AR5K_MODE_11G:
  893. channels[count].hw_value = chfreq | CHANNEL_OFDM;
  894. break;
  895. case AR5K_MODE_11A_TURBO:
  896. case AR5K_MODE_11G_TURBO:
  897. channels[count].hw_value = chfreq |
  898. CHANNEL_OFDM | CHANNEL_TURBO;
  899. break;
  900. case AR5K_MODE_11B:
  901. channels[count].hw_value = CHANNEL_B;
  902. }
  903. count++;
  904. max--;
  905. }
  906. return count;
  907. }
  908. static void
  909. ath5k_setup_rate_idx(struct ath5k_softc *sc, struct ieee80211_supported_band *b)
  910. {
  911. u8 i;
  912. for (i = 0; i < AR5K_MAX_RATES; i++)
  913. sc->rate_idx[b->band][i] = -1;
  914. for (i = 0; i < b->n_bitrates; i++) {
  915. sc->rate_idx[b->band][b->bitrates[i].hw_value] = i;
  916. if (b->bitrates[i].hw_value_short)
  917. sc->rate_idx[b->band][b->bitrates[i].hw_value_short] = i;
  918. }
  919. }
  920. static int
  921. ath5k_setup_bands(struct ieee80211_hw *hw)
  922. {
  923. struct ath5k_softc *sc = hw->priv;
  924. struct ath5k_hw *ah = sc->ah;
  925. struct ieee80211_supported_band *sband;
  926. int max_c, count_c = 0;
  927. int i;
  928. BUILD_BUG_ON(ARRAY_SIZE(sc->sbands) < IEEE80211_NUM_BANDS);
  929. max_c = ARRAY_SIZE(sc->channels);
  930. /* 2GHz band */
  931. sband = &sc->sbands[IEEE80211_BAND_2GHZ];
  932. sband->band = IEEE80211_BAND_2GHZ;
  933. sband->bitrates = &sc->rates[IEEE80211_BAND_2GHZ][0];
  934. if (test_bit(AR5K_MODE_11G, sc->ah->ah_capabilities.cap_mode)) {
  935. /* G mode */
  936. memcpy(sband->bitrates, &ath5k_rates[0],
  937. sizeof(struct ieee80211_rate) * 12);
  938. sband->n_bitrates = 12;
  939. sband->channels = sc->channels;
  940. sband->n_channels = ath5k_copy_channels(ah, sband->channels,
  941. AR5K_MODE_11G, max_c);
  942. hw->wiphy->bands[IEEE80211_BAND_2GHZ] = sband;
  943. count_c = sband->n_channels;
  944. max_c -= count_c;
  945. } else if (test_bit(AR5K_MODE_11B, sc->ah->ah_capabilities.cap_mode)) {
  946. /* B mode */
  947. memcpy(sband->bitrates, &ath5k_rates[0],
  948. sizeof(struct ieee80211_rate) * 4);
  949. sband->n_bitrates = 4;
  950. /* 5211 only supports B rates and uses 4bit rate codes
  951. * (e.g normally we have 0x1B for 1M, but on 5211 we have 0x0B)
  952. * fix them up here:
  953. */
  954. if (ah->ah_version == AR5K_AR5211) {
  955. for (i = 0; i < 4; i++) {
  956. sband->bitrates[i].hw_value =
  957. sband->bitrates[i].hw_value & 0xF;
  958. sband->bitrates[i].hw_value_short =
  959. sband->bitrates[i].hw_value_short & 0xF;
  960. }
  961. }
  962. sband->channels = sc->channels;
  963. sband->n_channels = ath5k_copy_channels(ah, sband->channels,
  964. AR5K_MODE_11B, max_c);
  965. hw->wiphy->bands[IEEE80211_BAND_2GHZ] = sband;
  966. count_c = sband->n_channels;
  967. max_c -= count_c;
  968. }
  969. ath5k_setup_rate_idx(sc, sband);
  970. /* 5GHz band, A mode */
  971. if (test_bit(AR5K_MODE_11A, sc->ah->ah_capabilities.cap_mode)) {
  972. sband = &sc->sbands[IEEE80211_BAND_5GHZ];
  973. sband->band = IEEE80211_BAND_5GHZ;
  974. sband->bitrates = &sc->rates[IEEE80211_BAND_5GHZ][0];
  975. memcpy(sband->bitrates, &ath5k_rates[4],
  976. sizeof(struct ieee80211_rate) * 8);
  977. sband->n_bitrates = 8;
  978. sband->channels = &sc->channels[count_c];
  979. sband->n_channels = ath5k_copy_channels(ah, sband->channels,
  980. AR5K_MODE_11A, max_c);
  981. hw->wiphy->bands[IEEE80211_BAND_5GHZ] = sband;
  982. }
  983. ath5k_setup_rate_idx(sc, sband);
  984. ath5k_debug_dump_bands(sc);
  985. return 0;
  986. }
  987. /*
  988. * Set/change channels. We always reset the chip.
  989. * To accomplish this we must first cleanup any pending DMA,
  990. * then restart stuff after a la ath5k_init.
  991. *
  992. * Called with sc->lock.
  993. */
  994. static int
  995. ath5k_chan_set(struct ath5k_softc *sc, struct ieee80211_channel *chan)
  996. {
  997. ATH5K_DBG(sc, ATH5K_DEBUG_RESET,
  998. "channel set, resetting (%u -> %u MHz)\n",
  999. sc->curchan->center_freq, chan->center_freq);
  1000. /*
  1001. * To switch channels clear any pending DMA operations;
  1002. * wait long enough for the RX fifo to drain, reset the
  1003. * hardware at the new frequency, and then re-enable
  1004. * the relevant bits of the h/w.
  1005. */
  1006. return ath5k_reset(sc, chan);
  1007. }
  1008. static void
  1009. ath5k_setcurmode(struct ath5k_softc *sc, unsigned int mode)
  1010. {
  1011. sc->curmode = mode;
  1012. if (mode == AR5K_MODE_11A) {
  1013. sc->curband = &sc->sbands[IEEE80211_BAND_5GHZ];
  1014. } else {
  1015. sc->curband = &sc->sbands[IEEE80211_BAND_2GHZ];
  1016. }
  1017. }
  1018. static void
  1019. ath5k_mode_setup(struct ath5k_softc *sc)
  1020. {
  1021. struct ath5k_hw *ah = sc->ah;
  1022. u32 rfilt;
  1023. /* configure rx filter */
  1024. rfilt = sc->filter_flags;
  1025. ath5k_hw_set_rx_filter(ah, rfilt);
  1026. if (ath5k_hw_hasbssidmask(ah))
  1027. ath5k_hw_set_bssid_mask(ah, sc->bssidmask);
  1028. /* configure operational mode */
  1029. ath5k_hw_set_opmode(ah, sc->opmode);
  1030. ATH5K_DBG(sc, ATH5K_DEBUG_MODE, "mode setup opmode %d\n", sc->opmode);
  1031. ATH5K_DBG(sc, ATH5K_DEBUG_MODE, "RX filter 0x%x\n", rfilt);
  1032. }
  1033. static inline int
  1034. ath5k_hw_to_driver_rix(struct ath5k_softc *sc, int hw_rix)
  1035. {
  1036. int rix;
  1037. /* return base rate on errors */
  1038. if (WARN(hw_rix < 0 || hw_rix >= AR5K_MAX_RATES,
  1039. "hw_rix out of bounds: %x\n", hw_rix))
  1040. return 0;
  1041. rix = sc->rate_idx[sc->curband->band][hw_rix];
  1042. if (WARN(rix < 0, "invalid hw_rix: %x\n", hw_rix))
  1043. rix = 0;
  1044. return rix;
  1045. }
  1046. /***************\
  1047. * Buffers setup *
  1048. \***************/
  1049. static
  1050. struct sk_buff *ath5k_rx_skb_alloc(struct ath5k_softc *sc, dma_addr_t *skb_addr)
  1051. {
  1052. struct ath_common *common = ath5k_hw_common(sc->ah);
  1053. struct sk_buff *skb;
  1054. /*
  1055. * Allocate buffer with headroom_needed space for the
  1056. * fake physical layer header at the start.
  1057. */
  1058. skb = ath_rxbuf_alloc(common,
  1059. common->rx_bufsize,
  1060. GFP_ATOMIC);
  1061. if (!skb) {
  1062. ATH5K_ERR(sc, "can't alloc skbuff of size %u\n",
  1063. common->rx_bufsize);
  1064. return NULL;
  1065. }
  1066. *skb_addr = pci_map_single(sc->pdev,
  1067. skb->data, common->rx_bufsize,
  1068. PCI_DMA_FROMDEVICE);
  1069. if (unlikely(pci_dma_mapping_error(sc->pdev, *skb_addr))) {
  1070. ATH5K_ERR(sc, "%s: DMA mapping failed\n", __func__);
  1071. dev_kfree_skb(skb);
  1072. return NULL;
  1073. }
  1074. return skb;
  1075. }
  1076. static int
  1077. ath5k_rxbuf_setup(struct ath5k_softc *sc, struct ath5k_buf *bf)
  1078. {
  1079. struct ath5k_hw *ah = sc->ah;
  1080. struct sk_buff *skb = bf->skb;
  1081. struct ath5k_desc *ds;
  1082. int ret;
  1083. if (!skb) {
  1084. skb = ath5k_rx_skb_alloc(sc, &bf->skbaddr);
  1085. if (!skb)
  1086. return -ENOMEM;
  1087. bf->skb = skb;
  1088. }
  1089. /*
  1090. * Setup descriptors. For receive we always terminate
  1091. * the descriptor list with a self-linked entry so we'll
  1092. * not get overrun under high load (as can happen with a
  1093. * 5212 when ANI processing enables PHY error frames).
  1094. *
  1095. * To ensure the last descriptor is self-linked we create
  1096. * each descriptor as self-linked and add it to the end. As
  1097. * each additional descriptor is added the previous self-linked
  1098. * entry is "fixed" naturally. This should be safe even
  1099. * if DMA is happening. When processing RX interrupts we
  1100. * never remove/process the last, self-linked, entry on the
  1101. * descriptor list. This ensures the hardware always has
  1102. * someplace to write a new frame.
  1103. */
  1104. ds = bf->desc;
  1105. ds->ds_link = bf->daddr; /* link to self */
  1106. ds->ds_data = bf->skbaddr;
  1107. ret = ath5k_hw_setup_rx_desc(ah, ds, ah->common.rx_bufsize, 0);
  1108. if (ret) {
  1109. ATH5K_ERR(sc, "%s: could not setup RX desc\n", __func__);
  1110. return ret;
  1111. }
  1112. if (sc->rxlink != NULL)
  1113. *sc->rxlink = bf->daddr;
  1114. sc->rxlink = &ds->ds_link;
  1115. return 0;
  1116. }
  1117. static enum ath5k_pkt_type get_hw_packet_type(struct sk_buff *skb)
  1118. {
  1119. struct ieee80211_hdr *hdr;
  1120. enum ath5k_pkt_type htype;
  1121. __le16 fc;
  1122. hdr = (struct ieee80211_hdr *)skb->data;
  1123. fc = hdr->frame_control;
  1124. if (ieee80211_is_beacon(fc))
  1125. htype = AR5K_PKT_TYPE_BEACON;
  1126. else if (ieee80211_is_probe_resp(fc))
  1127. htype = AR5K_PKT_TYPE_PROBE_RESP;
  1128. else if (ieee80211_is_atim(fc))
  1129. htype = AR5K_PKT_TYPE_ATIM;
  1130. else if (ieee80211_is_pspoll(fc))
  1131. htype = AR5K_PKT_TYPE_PSPOLL;
  1132. else
  1133. htype = AR5K_PKT_TYPE_NORMAL;
  1134. return htype;
  1135. }
  1136. static int
  1137. ath5k_txbuf_setup(struct ath5k_softc *sc, struct ath5k_buf *bf,
  1138. struct ath5k_txq *txq, int padsize)
  1139. {
  1140. struct ath5k_hw *ah = sc->ah;
  1141. struct ath5k_desc *ds = bf->desc;
  1142. struct sk_buff *skb = bf->skb;
  1143. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1144. unsigned int pktlen, flags, keyidx = AR5K_TXKEYIX_INVALID;
  1145. struct ieee80211_rate *rate;
  1146. unsigned int mrr_rate[3], mrr_tries[3];
  1147. int i, ret;
  1148. u16 hw_rate;
  1149. u16 cts_rate = 0;
  1150. u16 duration = 0;
  1151. u8 rc_flags;
  1152. flags = AR5K_TXDESC_INTREQ | AR5K_TXDESC_CLRDMASK;
  1153. /* XXX endianness */
  1154. bf->skbaddr = pci_map_single(sc->pdev, skb->data, skb->len,
  1155. PCI_DMA_TODEVICE);
  1156. rate = ieee80211_get_tx_rate(sc->hw, info);
  1157. if (info->flags & IEEE80211_TX_CTL_NO_ACK)
  1158. flags |= AR5K_TXDESC_NOACK;
  1159. rc_flags = info->control.rates[0].flags;
  1160. hw_rate = (rc_flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE) ?
  1161. rate->hw_value_short : rate->hw_value;
  1162. pktlen = skb->len;
  1163. /* FIXME: If we are in g mode and rate is a CCK rate
  1164. * subtract ah->ah_txpower.txp_cck_ofdm_pwr_delta
  1165. * from tx power (value is in dB units already) */
  1166. if (info->control.hw_key) {
  1167. keyidx = info->control.hw_key->hw_key_idx;
  1168. pktlen += info->control.hw_key->icv_len;
  1169. }
  1170. if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
  1171. flags |= AR5K_TXDESC_RTSENA;
  1172. cts_rate = ieee80211_get_rts_cts_rate(sc->hw, info)->hw_value;
  1173. duration = le16_to_cpu(ieee80211_rts_duration(sc->hw,
  1174. sc->vif, pktlen, info));
  1175. }
  1176. if (rc_flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
  1177. flags |= AR5K_TXDESC_CTSENA;
  1178. cts_rate = ieee80211_get_rts_cts_rate(sc->hw, info)->hw_value;
  1179. duration = le16_to_cpu(ieee80211_ctstoself_duration(sc->hw,
  1180. sc->vif, pktlen, info));
  1181. }
  1182. ret = ah->ah_setup_tx_desc(ah, ds, pktlen,
  1183. ieee80211_get_hdrlen_from_skb(skb), padsize,
  1184. get_hw_packet_type(skb),
  1185. (sc->power_level * 2),
  1186. hw_rate,
  1187. info->control.rates[0].count, keyidx, ah->ah_tx_ant, flags,
  1188. cts_rate, duration);
  1189. if (ret)
  1190. goto err_unmap;
  1191. memset(mrr_rate, 0, sizeof(mrr_rate));
  1192. memset(mrr_tries, 0, sizeof(mrr_tries));
  1193. for (i = 0; i < 3; i++) {
  1194. rate = ieee80211_get_alt_retry_rate(sc->hw, info, i);
  1195. if (!rate)
  1196. break;
  1197. mrr_rate[i] = rate->hw_value;
  1198. mrr_tries[i] = info->control.rates[i + 1].count;
  1199. }
  1200. ath5k_hw_setup_mrr_tx_desc(ah, ds,
  1201. mrr_rate[0], mrr_tries[0],
  1202. mrr_rate[1], mrr_tries[1],
  1203. mrr_rate[2], mrr_tries[2]);
  1204. ds->ds_link = 0;
  1205. ds->ds_data = bf->skbaddr;
  1206. spin_lock_bh(&txq->lock);
  1207. list_add_tail(&bf->list, &txq->q);
  1208. if (txq->link == NULL) /* is this first packet? */
  1209. ath5k_hw_set_txdp(ah, txq->qnum, bf->daddr);
  1210. else /* no, so only link it */
  1211. *txq->link = bf->daddr;
  1212. txq->link = &ds->ds_link;
  1213. ath5k_hw_start_tx_dma(ah, txq->qnum);
  1214. mmiowb();
  1215. spin_unlock_bh(&txq->lock);
  1216. return 0;
  1217. err_unmap:
  1218. pci_unmap_single(sc->pdev, bf->skbaddr, skb->len, PCI_DMA_TODEVICE);
  1219. return ret;
  1220. }
  1221. /*******************\
  1222. * Descriptors setup *
  1223. \*******************/
  1224. static int
  1225. ath5k_desc_alloc(struct ath5k_softc *sc, struct pci_dev *pdev)
  1226. {
  1227. struct ath5k_desc *ds;
  1228. struct ath5k_buf *bf;
  1229. dma_addr_t da;
  1230. unsigned int i;
  1231. int ret;
  1232. /* allocate descriptors */
  1233. sc->desc_len = sizeof(struct ath5k_desc) *
  1234. (ATH_TXBUF + ATH_RXBUF + ATH_BCBUF + 1);
  1235. sc->desc = pci_alloc_consistent(pdev, sc->desc_len, &sc->desc_daddr);
  1236. if (sc->desc == NULL) {
  1237. ATH5K_ERR(sc, "can't allocate descriptors\n");
  1238. ret = -ENOMEM;
  1239. goto err;
  1240. }
  1241. ds = sc->desc;
  1242. da = sc->desc_daddr;
  1243. ATH5K_DBG(sc, ATH5K_DEBUG_ANY, "DMA map: %p (%zu) -> %llx\n",
  1244. ds, sc->desc_len, (unsigned long long)sc->desc_daddr);
  1245. bf = kcalloc(1 + ATH_TXBUF + ATH_RXBUF + ATH_BCBUF,
  1246. sizeof(struct ath5k_buf), GFP_KERNEL);
  1247. if (bf == NULL) {
  1248. ATH5K_ERR(sc, "can't allocate bufptr\n");
  1249. ret = -ENOMEM;
  1250. goto err_free;
  1251. }
  1252. sc->bufptr = bf;
  1253. INIT_LIST_HEAD(&sc->rxbuf);
  1254. for (i = 0; i < ATH_RXBUF; i++, bf++, ds++, da += sizeof(*ds)) {
  1255. bf->desc = ds;
  1256. bf->daddr = da;
  1257. list_add_tail(&bf->list, &sc->rxbuf);
  1258. }
  1259. INIT_LIST_HEAD(&sc->txbuf);
  1260. sc->txbuf_len = ATH_TXBUF;
  1261. for (i = 0; i < ATH_TXBUF; i++, bf++, ds++,
  1262. da += sizeof(*ds)) {
  1263. bf->desc = ds;
  1264. bf->daddr = da;
  1265. list_add_tail(&bf->list, &sc->txbuf);
  1266. }
  1267. /* beacon buffer */
  1268. bf->desc = ds;
  1269. bf->daddr = da;
  1270. sc->bbuf = bf;
  1271. return 0;
  1272. err_free:
  1273. pci_free_consistent(pdev, sc->desc_len, sc->desc, sc->desc_daddr);
  1274. err:
  1275. sc->desc = NULL;
  1276. return ret;
  1277. }
  1278. static void
  1279. ath5k_desc_free(struct ath5k_softc *sc, struct pci_dev *pdev)
  1280. {
  1281. struct ath5k_buf *bf;
  1282. ath5k_txbuf_free_skb(sc, sc->bbuf);
  1283. list_for_each_entry(bf, &sc->txbuf, list)
  1284. ath5k_txbuf_free_skb(sc, bf);
  1285. list_for_each_entry(bf, &sc->rxbuf, list)
  1286. ath5k_rxbuf_free_skb(sc, bf);
  1287. /* Free memory associated with all descriptors */
  1288. pci_free_consistent(pdev, sc->desc_len, sc->desc, sc->desc_daddr);
  1289. sc->desc = NULL;
  1290. sc->desc_daddr = 0;
  1291. kfree(sc->bufptr);
  1292. sc->bufptr = NULL;
  1293. sc->bbuf = NULL;
  1294. }
  1295. /**************\
  1296. * Queues setup *
  1297. \**************/
  1298. static struct ath5k_txq *
  1299. ath5k_txq_setup(struct ath5k_softc *sc,
  1300. int qtype, int subtype)
  1301. {
  1302. struct ath5k_hw *ah = sc->ah;
  1303. struct ath5k_txq *txq;
  1304. struct ath5k_txq_info qi = {
  1305. .tqi_subtype = subtype,
  1306. .tqi_aifs = AR5K_TXQ_USEDEFAULT,
  1307. .tqi_cw_min = AR5K_TXQ_USEDEFAULT,
  1308. .tqi_cw_max = AR5K_TXQ_USEDEFAULT
  1309. };
  1310. int qnum;
  1311. /*
  1312. * Enable interrupts only for EOL and DESC conditions.
  1313. * We mark tx descriptors to receive a DESC interrupt
  1314. * when a tx queue gets deep; otherwise waiting for the
  1315. * EOL to reap descriptors. Note that this is done to
  1316. * reduce interrupt load and this only defers reaping
  1317. * descriptors, never transmitting frames. Aside from
  1318. * reducing interrupts this also permits more concurrency.
  1319. * The only potential downside is if the tx queue backs
  1320. * up in which case the top half of the kernel may backup
  1321. * due to a lack of tx descriptors.
  1322. */
  1323. qi.tqi_flags = AR5K_TXQ_FLAG_TXEOLINT_ENABLE |
  1324. AR5K_TXQ_FLAG_TXDESCINT_ENABLE;
  1325. qnum = ath5k_hw_setup_tx_queue(ah, qtype, &qi);
  1326. if (qnum < 0) {
  1327. /*
  1328. * NB: don't print a message, this happens
  1329. * normally on parts with too few tx queues
  1330. */
  1331. return ERR_PTR(qnum);
  1332. }
  1333. if (qnum >= ARRAY_SIZE(sc->txqs)) {
  1334. ATH5K_ERR(sc, "hw qnum %u out of range, max %tu!\n",
  1335. qnum, ARRAY_SIZE(sc->txqs));
  1336. ath5k_hw_release_tx_queue(ah, qnum);
  1337. return ERR_PTR(-EINVAL);
  1338. }
  1339. txq = &sc->txqs[qnum];
  1340. if (!txq->setup) {
  1341. txq->qnum = qnum;
  1342. txq->link = NULL;
  1343. INIT_LIST_HEAD(&txq->q);
  1344. spin_lock_init(&txq->lock);
  1345. txq->setup = true;
  1346. }
  1347. return &sc->txqs[qnum];
  1348. }
  1349. static int
  1350. ath5k_beaconq_setup(struct ath5k_hw *ah)
  1351. {
  1352. struct ath5k_txq_info qi = {
  1353. .tqi_aifs = AR5K_TXQ_USEDEFAULT,
  1354. .tqi_cw_min = AR5K_TXQ_USEDEFAULT,
  1355. .tqi_cw_max = AR5K_TXQ_USEDEFAULT,
  1356. /* NB: for dynamic turbo, don't enable any other interrupts */
  1357. .tqi_flags = AR5K_TXQ_FLAG_TXDESCINT_ENABLE
  1358. };
  1359. return ath5k_hw_setup_tx_queue(ah, AR5K_TX_QUEUE_BEACON, &qi);
  1360. }
  1361. static int
  1362. ath5k_beaconq_config(struct ath5k_softc *sc)
  1363. {
  1364. struct ath5k_hw *ah = sc->ah;
  1365. struct ath5k_txq_info qi;
  1366. int ret;
  1367. ret = ath5k_hw_get_tx_queueprops(ah, sc->bhalq, &qi);
  1368. if (ret)
  1369. goto err;
  1370. if (sc->opmode == NL80211_IFTYPE_AP ||
  1371. sc->opmode == NL80211_IFTYPE_MESH_POINT) {
  1372. /*
  1373. * Always burst out beacon and CAB traffic
  1374. * (aifs = cwmin = cwmax = 0)
  1375. */
  1376. qi.tqi_aifs = 0;
  1377. qi.tqi_cw_min = 0;
  1378. qi.tqi_cw_max = 0;
  1379. } else if (sc->opmode == NL80211_IFTYPE_ADHOC) {
  1380. /*
  1381. * Adhoc mode; backoff between 0 and (2 * cw_min).
  1382. */
  1383. qi.tqi_aifs = 0;
  1384. qi.tqi_cw_min = 0;
  1385. qi.tqi_cw_max = 2 * ah->ah_cw_min;
  1386. }
  1387. ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
  1388. "beacon queueprops tqi_aifs:%d tqi_cw_min:%d tqi_cw_max:%d\n",
  1389. qi.tqi_aifs, qi.tqi_cw_min, qi.tqi_cw_max);
  1390. ret = ath5k_hw_set_tx_queueprops(ah, sc->bhalq, &qi);
  1391. if (ret) {
  1392. ATH5K_ERR(sc, "%s: unable to update parameters for beacon "
  1393. "hardware queue!\n", __func__);
  1394. goto err;
  1395. }
  1396. ret = ath5k_hw_reset_tx_queue(ah, sc->bhalq); /* push to h/w */
  1397. if (ret)
  1398. goto err;
  1399. /* reconfigure cabq with ready time to 80% of beacon_interval */
  1400. ret = ath5k_hw_get_tx_queueprops(ah, AR5K_TX_QUEUE_ID_CAB, &qi);
  1401. if (ret)
  1402. goto err;
  1403. qi.tqi_ready_time = (sc->bintval * 80) / 100;
  1404. ret = ath5k_hw_set_tx_queueprops(ah, AR5K_TX_QUEUE_ID_CAB, &qi);
  1405. if (ret)
  1406. goto err;
  1407. ret = ath5k_hw_reset_tx_queue(ah, AR5K_TX_QUEUE_ID_CAB);
  1408. err:
  1409. return ret;
  1410. }
  1411. static void
  1412. ath5k_txq_drainq(struct ath5k_softc *sc, struct ath5k_txq *txq)
  1413. {
  1414. struct ath5k_buf *bf, *bf0;
  1415. /*
  1416. * NB: this assumes output has been stopped and
  1417. * we do not need to block ath5k_tx_tasklet
  1418. */
  1419. spin_lock_bh(&txq->lock);
  1420. list_for_each_entry_safe(bf, bf0, &txq->q, list) {
  1421. ath5k_debug_printtxbuf(sc, bf);
  1422. ath5k_txbuf_free_skb(sc, bf);
  1423. spin_lock_bh(&sc->txbuflock);
  1424. list_move_tail(&bf->list, &sc->txbuf);
  1425. sc->txbuf_len++;
  1426. spin_unlock_bh(&sc->txbuflock);
  1427. }
  1428. txq->link = NULL;
  1429. spin_unlock_bh(&txq->lock);
  1430. }
  1431. /*
  1432. * Drain the transmit queues and reclaim resources.
  1433. */
  1434. static void
  1435. ath5k_txq_cleanup(struct ath5k_softc *sc)
  1436. {
  1437. struct ath5k_hw *ah = sc->ah;
  1438. unsigned int i;
  1439. /* XXX return value */
  1440. if (likely(!test_bit(ATH_STAT_INVALID, sc->status))) {
  1441. /* don't touch the hardware if marked invalid */
  1442. ath5k_hw_stop_tx_dma(ah, sc->bhalq);
  1443. ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "beacon queue %x\n",
  1444. ath5k_hw_get_txdp(ah, sc->bhalq));
  1445. for (i = 0; i < ARRAY_SIZE(sc->txqs); i++)
  1446. if (sc->txqs[i].setup) {
  1447. ath5k_hw_stop_tx_dma(ah, sc->txqs[i].qnum);
  1448. ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "txq [%u] %x, "
  1449. "link %p\n",
  1450. sc->txqs[i].qnum,
  1451. ath5k_hw_get_txdp(ah,
  1452. sc->txqs[i].qnum),
  1453. sc->txqs[i].link);
  1454. }
  1455. }
  1456. for (i = 0; i < ARRAY_SIZE(sc->txqs); i++)
  1457. if (sc->txqs[i].setup)
  1458. ath5k_txq_drainq(sc, &sc->txqs[i]);
  1459. }
  1460. static void
  1461. ath5k_txq_release(struct ath5k_softc *sc)
  1462. {
  1463. struct ath5k_txq *txq = sc->txqs;
  1464. unsigned int i;
  1465. for (i = 0; i < ARRAY_SIZE(sc->txqs); i++, txq++)
  1466. if (txq->setup) {
  1467. ath5k_hw_release_tx_queue(sc->ah, txq->qnum);
  1468. txq->setup = false;
  1469. }
  1470. }
  1471. /*************\
  1472. * RX Handling *
  1473. \*************/
  1474. /*
  1475. * Enable the receive h/w following a reset.
  1476. */
  1477. static int
  1478. ath5k_rx_start(struct ath5k_softc *sc)
  1479. {
  1480. struct ath5k_hw *ah = sc->ah;
  1481. struct ath_common *common = ath5k_hw_common(ah);
  1482. struct ath5k_buf *bf;
  1483. int ret;
  1484. common->rx_bufsize = roundup(IEEE80211_MAX_LEN, common->cachelsz);
  1485. ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "cachelsz %u rx_bufsize %u\n",
  1486. common->cachelsz, common->rx_bufsize);
  1487. spin_lock_bh(&sc->rxbuflock);
  1488. sc->rxlink = NULL;
  1489. list_for_each_entry(bf, &sc->rxbuf, list) {
  1490. ret = ath5k_rxbuf_setup(sc, bf);
  1491. if (ret != 0) {
  1492. spin_unlock_bh(&sc->rxbuflock);
  1493. goto err;
  1494. }
  1495. }
  1496. bf = list_first_entry(&sc->rxbuf, struct ath5k_buf, list);
  1497. ath5k_hw_set_rxdp(ah, bf->daddr);
  1498. spin_unlock_bh(&sc->rxbuflock);
  1499. ath5k_hw_start_rx_dma(ah); /* enable recv descriptors */
  1500. ath5k_mode_setup(sc); /* set filters, etc. */
  1501. ath5k_hw_start_rx_pcu(ah); /* re-enable PCU/DMA engine */
  1502. return 0;
  1503. err:
  1504. return ret;
  1505. }
  1506. /*
  1507. * Disable the receive h/w in preparation for a reset.
  1508. */
  1509. static void
  1510. ath5k_rx_stop(struct ath5k_softc *sc)
  1511. {
  1512. struct ath5k_hw *ah = sc->ah;
  1513. ath5k_hw_stop_rx_pcu(ah); /* disable PCU */
  1514. ath5k_hw_set_rx_filter(ah, 0); /* clear recv filter */
  1515. ath5k_hw_stop_rx_dma(ah); /* disable DMA engine */
  1516. ath5k_debug_printrxbuffs(sc, ah);
  1517. }
  1518. static unsigned int
  1519. ath5k_rx_decrypted(struct ath5k_softc *sc, struct sk_buff *skb,
  1520. struct ath5k_rx_status *rs)
  1521. {
  1522. struct ath5k_hw *ah = sc->ah;
  1523. struct ath_common *common = ath5k_hw_common(ah);
  1524. struct ieee80211_hdr *hdr = (void *)skb->data;
  1525. unsigned int keyix, hlen;
  1526. if (!(rs->rs_status & AR5K_RXERR_DECRYPT) &&
  1527. rs->rs_keyix != AR5K_RXKEYIX_INVALID)
  1528. return RX_FLAG_DECRYPTED;
  1529. /* Apparently when a default key is used to decrypt the packet
  1530. the hw does not set the index used to decrypt. In such cases
  1531. get the index from the packet. */
  1532. hlen = ieee80211_hdrlen(hdr->frame_control);
  1533. if (ieee80211_has_protected(hdr->frame_control) &&
  1534. !(rs->rs_status & AR5K_RXERR_DECRYPT) &&
  1535. skb->len >= hlen + 4) {
  1536. keyix = skb->data[hlen + 3] >> 6;
  1537. if (test_bit(keyix, common->keymap))
  1538. return RX_FLAG_DECRYPTED;
  1539. }
  1540. return 0;
  1541. }
  1542. static void
  1543. ath5k_check_ibss_tsf(struct ath5k_softc *sc, struct sk_buff *skb,
  1544. struct ieee80211_rx_status *rxs)
  1545. {
  1546. struct ath_common *common = ath5k_hw_common(sc->ah);
  1547. u64 tsf, bc_tstamp;
  1548. u32 hw_tu;
  1549. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)skb->data;
  1550. if (ieee80211_is_beacon(mgmt->frame_control) &&
  1551. le16_to_cpu(mgmt->u.beacon.capab_info) & WLAN_CAPABILITY_IBSS &&
  1552. memcmp(mgmt->bssid, common->curbssid, ETH_ALEN) == 0) {
  1553. /*
  1554. * Received an IBSS beacon with the same BSSID. Hardware *must*
  1555. * have updated the local TSF. We have to work around various
  1556. * hardware bugs, though...
  1557. */
  1558. tsf = ath5k_hw_get_tsf64(sc->ah);
  1559. bc_tstamp = le64_to_cpu(mgmt->u.beacon.timestamp);
  1560. hw_tu = TSF_TO_TU(tsf);
  1561. ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
  1562. "beacon %llx mactime %llx (diff %lld) tsf now %llx\n",
  1563. (unsigned long long)bc_tstamp,
  1564. (unsigned long long)rxs->mactime,
  1565. (unsigned long long)(rxs->mactime - bc_tstamp),
  1566. (unsigned long long)tsf);
  1567. /*
  1568. * Sometimes the HW will give us a wrong tstamp in the rx
  1569. * status, causing the timestamp extension to go wrong.
  1570. * (This seems to happen especially with beacon frames bigger
  1571. * than 78 byte (incl. FCS))
  1572. * But we know that the receive timestamp must be later than the
  1573. * timestamp of the beacon since HW must have synced to that.
  1574. *
  1575. * NOTE: here we assume mactime to be after the frame was
  1576. * received, not like mac80211 which defines it at the start.
  1577. */
  1578. if (bc_tstamp > rxs->mactime) {
  1579. ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
  1580. "fixing mactime from %llx to %llx\n",
  1581. (unsigned long long)rxs->mactime,
  1582. (unsigned long long)tsf);
  1583. rxs->mactime = tsf;
  1584. }
  1585. /*
  1586. * Local TSF might have moved higher than our beacon timers,
  1587. * in that case we have to update them to continue sending
  1588. * beacons. This also takes care of synchronizing beacon sending
  1589. * times with other stations.
  1590. */
  1591. if (hw_tu >= sc->nexttbtt)
  1592. ath5k_beacon_update_timers(sc, bc_tstamp);
  1593. }
  1594. }
  1595. static void
  1596. ath5k_update_beacon_rssi(struct ath5k_softc *sc, struct sk_buff *skb, int rssi)
  1597. {
  1598. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)skb->data;
  1599. struct ath5k_hw *ah = sc->ah;
  1600. struct ath_common *common = ath5k_hw_common(ah);
  1601. /* only beacons from our BSSID */
  1602. if (!ieee80211_is_beacon(mgmt->frame_control) ||
  1603. memcmp(mgmt->bssid, common->curbssid, ETH_ALEN) != 0)
  1604. return;
  1605. ah->ah_beacon_rssi_avg = ath5k_moving_average(ah->ah_beacon_rssi_avg,
  1606. rssi);
  1607. /* in IBSS mode we should keep RSSI statistics per neighbour */
  1608. /* le16_to_cpu(mgmt->u.beacon.capab_info) & WLAN_CAPABILITY_IBSS */
  1609. }
  1610. /*
  1611. * Compute padding position. skb must contains an IEEE 802.11 frame
  1612. */
  1613. static int ath5k_common_padpos(struct sk_buff *skb)
  1614. {
  1615. struct ieee80211_hdr * hdr = (struct ieee80211_hdr *)skb->data;
  1616. __le16 frame_control = hdr->frame_control;
  1617. int padpos = 24;
  1618. if (ieee80211_has_a4(frame_control)) {
  1619. padpos += ETH_ALEN;
  1620. }
  1621. if (ieee80211_is_data_qos(frame_control)) {
  1622. padpos += IEEE80211_QOS_CTL_LEN;
  1623. }
  1624. return padpos;
  1625. }
  1626. /*
  1627. * This function expects a 802.11 frame and returns the number of
  1628. * bytes added, or -1 if we don't have enought header room.
  1629. */
  1630. static int ath5k_add_padding(struct sk_buff *skb)
  1631. {
  1632. int padpos = ath5k_common_padpos(skb);
  1633. int padsize = padpos & 3;
  1634. if (padsize && skb->len>padpos) {
  1635. if (skb_headroom(skb) < padsize)
  1636. return -1;
  1637. skb_push(skb, padsize);
  1638. memmove(skb->data, skb->data+padsize, padpos);
  1639. return padsize;
  1640. }
  1641. return 0;
  1642. }
  1643. /*
  1644. * This function expects a 802.11 frame and returns the number of
  1645. * bytes removed
  1646. */
  1647. static int ath5k_remove_padding(struct sk_buff *skb)
  1648. {
  1649. int padpos = ath5k_common_padpos(skb);
  1650. int padsize = padpos & 3;
  1651. if (padsize && skb->len>=padpos+padsize) {
  1652. memmove(skb->data + padsize, skb->data, padpos);
  1653. skb_pull(skb, padsize);
  1654. return padsize;
  1655. }
  1656. return 0;
  1657. }
  1658. static void
  1659. ath5k_receive_frame(struct ath5k_softc *sc, struct sk_buff *skb,
  1660. struct ath5k_rx_status *rs)
  1661. {
  1662. struct ieee80211_rx_status *rxs;
  1663. /* The MAC header is padded to have 32-bit boundary if the
  1664. * packet payload is non-zero. The general calculation for
  1665. * padsize would take into account odd header lengths:
  1666. * padsize = (4 - hdrlen % 4) % 4; However, since only
  1667. * even-length headers are used, padding can only be 0 or 2
  1668. * bytes and we can optimize this a bit. In addition, we must
  1669. * not try to remove padding from short control frames that do
  1670. * not have payload. */
  1671. ath5k_remove_padding(skb);
  1672. rxs = IEEE80211_SKB_RXCB(skb);
  1673. rxs->flag = 0;
  1674. if (unlikely(rs->rs_status & AR5K_RXERR_MIC))
  1675. rxs->flag |= RX_FLAG_MMIC_ERROR;
  1676. /*
  1677. * always extend the mac timestamp, since this information is
  1678. * also needed for proper IBSS merging.
  1679. *
  1680. * XXX: it might be too late to do it here, since rs_tstamp is
  1681. * 15bit only. that means TSF extension has to be done within
  1682. * 32768usec (about 32ms). it might be necessary to move this to
  1683. * the interrupt handler, like it is done in madwifi.
  1684. *
  1685. * Unfortunately we don't know when the hardware takes the rx
  1686. * timestamp (beginning of phy frame, data frame, end of rx?).
  1687. * The only thing we know is that it is hardware specific...
  1688. * On AR5213 it seems the rx timestamp is at the end of the
  1689. * frame, but i'm not sure.
  1690. *
  1691. * NOTE: mac80211 defines mactime at the beginning of the first
  1692. * data symbol. Since we don't have any time references it's
  1693. * impossible to comply to that. This affects IBSS merge only
  1694. * right now, so it's not too bad...
  1695. */
  1696. rxs->mactime = ath5k_extend_tsf(sc->ah, rs->rs_tstamp);
  1697. rxs->flag |= RX_FLAG_TSFT;
  1698. rxs->freq = sc->curchan->center_freq;
  1699. rxs->band = sc->curband->band;
  1700. rxs->signal = sc->ah->ah_noise_floor + rs->rs_rssi;
  1701. rxs->antenna = rs->rs_antenna;
  1702. if (rs->rs_antenna > 0 && rs->rs_antenna < 5)
  1703. sc->stats.antenna_rx[rs->rs_antenna]++;
  1704. else
  1705. sc->stats.antenna_rx[0]++; /* invalid */
  1706. rxs->rate_idx = ath5k_hw_to_driver_rix(sc, rs->rs_rate);
  1707. rxs->flag |= ath5k_rx_decrypted(sc, skb, rs);
  1708. if (rxs->rate_idx >= 0 && rs->rs_rate ==
  1709. sc->curband->bitrates[rxs->rate_idx].hw_value_short)
  1710. rxs->flag |= RX_FLAG_SHORTPRE;
  1711. ath5k_debug_dump_skb(sc, skb, "RX ", 0);
  1712. ath5k_update_beacon_rssi(sc, skb, rs->rs_rssi);
  1713. /* check beacons in IBSS mode */
  1714. if (sc->opmode == NL80211_IFTYPE_ADHOC)
  1715. ath5k_check_ibss_tsf(sc, skb, rxs);
  1716. ieee80211_rx(sc->hw, skb);
  1717. }
  1718. /** ath5k_frame_receive_ok() - Do we want to receive this frame or not?
  1719. *
  1720. * Check if we want to further process this frame or not. Also update
  1721. * statistics. Return true if we want this frame, false if not.
  1722. */
  1723. static bool
  1724. ath5k_receive_frame_ok(struct ath5k_softc *sc, struct ath5k_rx_status *rs)
  1725. {
  1726. sc->stats.rx_all_count++;
  1727. if (unlikely(rs->rs_status)) {
  1728. if (rs->rs_status & AR5K_RXERR_CRC)
  1729. sc->stats.rxerr_crc++;
  1730. if (rs->rs_status & AR5K_RXERR_FIFO)
  1731. sc->stats.rxerr_fifo++;
  1732. if (rs->rs_status & AR5K_RXERR_PHY) {
  1733. sc->stats.rxerr_phy++;
  1734. if (rs->rs_phyerr > 0 && rs->rs_phyerr < 32)
  1735. sc->stats.rxerr_phy_code[rs->rs_phyerr]++;
  1736. return false;
  1737. }
  1738. if (rs->rs_status & AR5K_RXERR_DECRYPT) {
  1739. /*
  1740. * Decrypt error. If the error occurred
  1741. * because there was no hardware key, then
  1742. * let the frame through so the upper layers
  1743. * can process it. This is necessary for 5210
  1744. * parts which have no way to setup a ``clear''
  1745. * key cache entry.
  1746. *
  1747. * XXX do key cache faulting
  1748. */
  1749. sc->stats.rxerr_decrypt++;
  1750. if (rs->rs_keyix == AR5K_RXKEYIX_INVALID &&
  1751. !(rs->rs_status & AR5K_RXERR_CRC))
  1752. return true;
  1753. }
  1754. if (rs->rs_status & AR5K_RXERR_MIC) {
  1755. sc->stats.rxerr_mic++;
  1756. return true;
  1757. }
  1758. /* let crypto-error packets fall through in MNTR */
  1759. if ((rs->rs_status & ~(AR5K_RXERR_DECRYPT|AR5K_RXERR_MIC)) ||
  1760. sc->opmode != NL80211_IFTYPE_MONITOR)
  1761. return false;
  1762. }
  1763. if (unlikely(rs->rs_more)) {
  1764. sc->stats.rxerr_jumbo++;
  1765. return false;
  1766. }
  1767. return true;
  1768. }
  1769. static void
  1770. ath5k_tasklet_rx(unsigned long data)
  1771. {
  1772. struct ath5k_rx_status rs = {};
  1773. struct sk_buff *skb, *next_skb;
  1774. dma_addr_t next_skb_addr;
  1775. struct ath5k_softc *sc = (void *)data;
  1776. struct ath5k_hw *ah = sc->ah;
  1777. struct ath_common *common = ath5k_hw_common(ah);
  1778. struct ath5k_buf *bf;
  1779. struct ath5k_desc *ds;
  1780. int ret;
  1781. spin_lock(&sc->rxbuflock);
  1782. if (list_empty(&sc->rxbuf)) {
  1783. ATH5K_WARN(sc, "empty rx buf pool\n");
  1784. goto unlock;
  1785. }
  1786. do {
  1787. bf = list_first_entry(&sc->rxbuf, struct ath5k_buf, list);
  1788. BUG_ON(bf->skb == NULL);
  1789. skb = bf->skb;
  1790. ds = bf->desc;
  1791. /* bail if HW is still using self-linked descriptor */
  1792. if (ath5k_hw_get_rxdp(sc->ah) == bf->daddr)
  1793. break;
  1794. ret = sc->ah->ah_proc_rx_desc(sc->ah, ds, &rs);
  1795. if (unlikely(ret == -EINPROGRESS))
  1796. break;
  1797. else if (unlikely(ret)) {
  1798. ATH5K_ERR(sc, "error in processing rx descriptor\n");
  1799. sc->stats.rxerr_proc++;
  1800. break;
  1801. }
  1802. if (ath5k_receive_frame_ok(sc, &rs)) {
  1803. next_skb = ath5k_rx_skb_alloc(sc, &next_skb_addr);
  1804. /*
  1805. * If we can't replace bf->skb with a new skb under
  1806. * memory pressure, just skip this packet
  1807. */
  1808. if (!next_skb)
  1809. goto next;
  1810. pci_unmap_single(sc->pdev, bf->skbaddr,
  1811. common->rx_bufsize,
  1812. PCI_DMA_FROMDEVICE);
  1813. skb_put(skb, rs.rs_datalen);
  1814. ath5k_receive_frame(sc, skb, &rs);
  1815. bf->skb = next_skb;
  1816. bf->skbaddr = next_skb_addr;
  1817. }
  1818. next:
  1819. list_move_tail(&bf->list, &sc->rxbuf);
  1820. } while (ath5k_rxbuf_setup(sc, bf) == 0);
  1821. unlock:
  1822. spin_unlock(&sc->rxbuflock);
  1823. }
  1824. /*************\
  1825. * TX Handling *
  1826. \*************/
  1827. static void
  1828. ath5k_tx_processq(struct ath5k_softc *sc, struct ath5k_txq *txq)
  1829. {
  1830. struct ath5k_tx_status ts = {};
  1831. struct ath5k_buf *bf, *bf0;
  1832. struct ath5k_desc *ds;
  1833. struct sk_buff *skb;
  1834. struct ieee80211_tx_info *info;
  1835. int i, ret;
  1836. spin_lock(&txq->lock);
  1837. list_for_each_entry_safe(bf, bf0, &txq->q, list) {
  1838. ds = bf->desc;
  1839. /*
  1840. * It's possible that the hardware can say the buffer is
  1841. * completed when it hasn't yet loaded the ds_link from
  1842. * host memory and moved on. If there are more TX
  1843. * descriptors in the queue, wait for TXDP to change
  1844. * before processing this one.
  1845. */
  1846. if (ath5k_hw_get_txdp(sc->ah, txq->qnum) == bf->daddr &&
  1847. !list_is_last(&bf->list, &txq->q))
  1848. break;
  1849. ret = sc->ah->ah_proc_tx_desc(sc->ah, ds, &ts);
  1850. if (unlikely(ret == -EINPROGRESS))
  1851. break;
  1852. else if (unlikely(ret)) {
  1853. ATH5K_ERR(sc, "error %d while processing queue %u\n",
  1854. ret, txq->qnum);
  1855. break;
  1856. }
  1857. sc->stats.tx_all_count++;
  1858. skb = bf->skb;
  1859. info = IEEE80211_SKB_CB(skb);
  1860. bf->skb = NULL;
  1861. pci_unmap_single(sc->pdev, bf->skbaddr, skb->len,
  1862. PCI_DMA_TODEVICE);
  1863. ieee80211_tx_info_clear_status(info);
  1864. for (i = 0; i < 4; i++) {
  1865. struct ieee80211_tx_rate *r =
  1866. &info->status.rates[i];
  1867. if (ts.ts_rate[i]) {
  1868. r->idx = ath5k_hw_to_driver_rix(sc, ts.ts_rate[i]);
  1869. r->count = ts.ts_retry[i];
  1870. } else {
  1871. r->idx = -1;
  1872. r->count = 0;
  1873. }
  1874. }
  1875. /* count the successful attempt as well */
  1876. info->status.rates[ts.ts_final_idx].count++;
  1877. if (unlikely(ts.ts_status)) {
  1878. sc->stats.ack_fail++;
  1879. if (ts.ts_status & AR5K_TXERR_FILT) {
  1880. info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
  1881. sc->stats.txerr_filt++;
  1882. }
  1883. if (ts.ts_status & AR5K_TXERR_XRETRY)
  1884. sc->stats.txerr_retry++;
  1885. if (ts.ts_status & AR5K_TXERR_FIFO)
  1886. sc->stats.txerr_fifo++;
  1887. } else {
  1888. info->flags |= IEEE80211_TX_STAT_ACK;
  1889. info->status.ack_signal = ts.ts_rssi;
  1890. }
  1891. /*
  1892. * Remove MAC header padding before giving the frame
  1893. * back to mac80211.
  1894. */
  1895. ath5k_remove_padding(skb);
  1896. if (ts.ts_antenna > 0 && ts.ts_antenna < 5)
  1897. sc->stats.antenna_tx[ts.ts_antenna]++;
  1898. else
  1899. sc->stats.antenna_tx[0]++; /* invalid */
  1900. ieee80211_tx_status(sc->hw, skb);
  1901. spin_lock(&sc->txbuflock);
  1902. list_move_tail(&bf->list, &sc->txbuf);
  1903. sc->txbuf_len++;
  1904. spin_unlock(&sc->txbuflock);
  1905. }
  1906. if (likely(list_empty(&txq->q)))
  1907. txq->link = NULL;
  1908. spin_unlock(&txq->lock);
  1909. if (sc->txbuf_len > ATH_TXBUF / 5)
  1910. ieee80211_wake_queues(sc->hw);
  1911. }
  1912. static void
  1913. ath5k_tasklet_tx(unsigned long data)
  1914. {
  1915. int i;
  1916. struct ath5k_softc *sc = (void *)data;
  1917. for (i=0; i < AR5K_NUM_TX_QUEUES; i++)
  1918. if (sc->txqs[i].setup && (sc->ah->ah_txq_isr & BIT(i)))
  1919. ath5k_tx_processq(sc, &sc->txqs[i]);
  1920. }
  1921. /*****************\
  1922. * Beacon handling *
  1923. \*****************/
  1924. /*
  1925. * Setup the beacon frame for transmit.
  1926. */
  1927. static int
  1928. ath5k_beacon_setup(struct ath5k_softc *sc, struct ath5k_buf *bf)
  1929. {
  1930. struct sk_buff *skb = bf->skb;
  1931. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1932. struct ath5k_hw *ah = sc->ah;
  1933. struct ath5k_desc *ds;
  1934. int ret = 0;
  1935. u8 antenna;
  1936. u32 flags;
  1937. const int padsize = 0;
  1938. bf->skbaddr = pci_map_single(sc->pdev, skb->data, skb->len,
  1939. PCI_DMA_TODEVICE);
  1940. ATH5K_DBG(sc, ATH5K_DEBUG_BEACON, "skb %p [data %p len %u] "
  1941. "skbaddr %llx\n", skb, skb->data, skb->len,
  1942. (unsigned long long)bf->skbaddr);
  1943. if (pci_dma_mapping_error(sc->pdev, bf->skbaddr)) {
  1944. ATH5K_ERR(sc, "beacon DMA mapping failed\n");
  1945. return -EIO;
  1946. }
  1947. ds = bf->desc;
  1948. antenna = ah->ah_tx_ant;
  1949. flags = AR5K_TXDESC_NOACK;
  1950. if (sc->opmode == NL80211_IFTYPE_ADHOC && ath5k_hw_hasveol(ah)) {
  1951. ds->ds_link = bf->daddr; /* self-linked */
  1952. flags |= AR5K_TXDESC_VEOL;
  1953. } else
  1954. ds->ds_link = 0;
  1955. /*
  1956. * If we use multiple antennas on AP and use
  1957. * the Sectored AP scenario, switch antenna every
  1958. * 4 beacons to make sure everybody hears our AP.
  1959. * When a client tries to associate, hw will keep
  1960. * track of the tx antenna to be used for this client
  1961. * automaticaly, based on ACKed packets.
  1962. *
  1963. * Note: AP still listens and transmits RTS on the
  1964. * default antenna which is supposed to be an omni.
  1965. *
  1966. * Note2: On sectored scenarios it's possible to have
  1967. * multiple antennas (1omni -the default- and 14 sectors)
  1968. * so if we choose to actually support this mode we need
  1969. * to allow user to set how many antennas we have and tweak
  1970. * the code below to send beacons on all of them.
  1971. */
  1972. if (ah->ah_ant_mode == AR5K_ANTMODE_SECTOR_AP)
  1973. antenna = sc->bsent & 4 ? 2 : 1;
  1974. /* FIXME: If we are in g mode and rate is a CCK rate
  1975. * subtract ah->ah_txpower.txp_cck_ofdm_pwr_delta
  1976. * from tx power (value is in dB units already) */
  1977. ds->ds_data = bf->skbaddr;
  1978. ret = ah->ah_setup_tx_desc(ah, ds, skb->len,
  1979. ieee80211_get_hdrlen_from_skb(skb), padsize,
  1980. AR5K_PKT_TYPE_BEACON, (sc->power_level * 2),
  1981. ieee80211_get_tx_rate(sc->hw, info)->hw_value,
  1982. 1, AR5K_TXKEYIX_INVALID,
  1983. antenna, flags, 0, 0);
  1984. if (ret)
  1985. goto err_unmap;
  1986. return 0;
  1987. err_unmap:
  1988. pci_unmap_single(sc->pdev, bf->skbaddr, skb->len, PCI_DMA_TODEVICE);
  1989. return ret;
  1990. }
  1991. /*
  1992. * Transmit a beacon frame at SWBA. Dynamic updates to the
  1993. * frame contents are done as needed and the slot time is
  1994. * also adjusted based on current state.
  1995. *
  1996. * This is called from software irq context (beacontq tasklets)
  1997. * or user context from ath5k_beacon_config.
  1998. */
  1999. static void
  2000. ath5k_beacon_send(struct ath5k_softc *sc)
  2001. {
  2002. struct ath5k_buf *bf = sc->bbuf;
  2003. struct ath5k_hw *ah = sc->ah;
  2004. struct sk_buff *skb;
  2005. ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON, "in beacon_send\n");
  2006. if (unlikely(bf->skb == NULL || sc->opmode == NL80211_IFTYPE_STATION ||
  2007. sc->opmode == NL80211_IFTYPE_MONITOR)) {
  2008. ATH5K_WARN(sc, "bf=%p bf_skb=%p\n", bf, bf ? bf->skb : NULL);
  2009. return;
  2010. }
  2011. /*
  2012. * Check if the previous beacon has gone out. If
  2013. * not don't don't try to post another, skip this
  2014. * period and wait for the next. Missed beacons
  2015. * indicate a problem and should not occur. If we
  2016. * miss too many consecutive beacons reset the device.
  2017. */
  2018. if (unlikely(ath5k_hw_num_tx_pending(ah, sc->bhalq) != 0)) {
  2019. sc->bmisscount++;
  2020. ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
  2021. "missed %u consecutive beacons\n", sc->bmisscount);
  2022. if (sc->bmisscount > 10) { /* NB: 10 is a guess */
  2023. ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
  2024. "stuck beacon time (%u missed)\n",
  2025. sc->bmisscount);
  2026. ATH5K_DBG(sc, ATH5K_DEBUG_RESET,
  2027. "stuck beacon, resetting\n");
  2028. ieee80211_queue_work(sc->hw, &sc->reset_work);
  2029. }
  2030. return;
  2031. }
  2032. if (unlikely(sc->bmisscount != 0)) {
  2033. ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
  2034. "resume beacon xmit after %u misses\n",
  2035. sc->bmisscount);
  2036. sc->bmisscount = 0;
  2037. }
  2038. /*
  2039. * Stop any current dma and put the new frame on the queue.
  2040. * This should never fail since we check above that no frames
  2041. * are still pending on the queue.
  2042. */
  2043. if (unlikely(ath5k_hw_stop_tx_dma(ah, sc->bhalq))) {
  2044. ATH5K_WARN(sc, "beacon queue %u didn't start/stop ?\n", sc->bhalq);
  2045. /* NB: hw still stops DMA, so proceed */
  2046. }
  2047. /* refresh the beacon for AP mode */
  2048. if (sc->opmode == NL80211_IFTYPE_AP)
  2049. ath5k_beacon_update(sc->hw, sc->vif);
  2050. ath5k_hw_set_txdp(ah, sc->bhalq, bf->daddr);
  2051. ath5k_hw_start_tx_dma(ah, sc->bhalq);
  2052. ATH5K_DBG(sc, ATH5K_DEBUG_BEACON, "TXDP[%u] = %llx (%p)\n",
  2053. sc->bhalq, (unsigned long long)bf->daddr, bf->desc);
  2054. skb = ieee80211_get_buffered_bc(sc->hw, sc->vif);
  2055. while (skb) {
  2056. ath5k_tx_queue(sc->hw, skb, sc->cabq);
  2057. skb = ieee80211_get_buffered_bc(sc->hw, sc->vif);
  2058. }
  2059. sc->bsent++;
  2060. }
  2061. /**
  2062. * ath5k_beacon_update_timers - update beacon timers
  2063. *
  2064. * @sc: struct ath5k_softc pointer we are operating on
  2065. * @bc_tsf: the timestamp of the beacon. 0 to reset the TSF. -1 to perform a
  2066. * beacon timer update based on the current HW TSF.
  2067. *
  2068. * Calculate the next target beacon transmit time (TBTT) based on the timestamp
  2069. * of a received beacon or the current local hardware TSF and write it to the
  2070. * beacon timer registers.
  2071. *
  2072. * This is called in a variety of situations, e.g. when a beacon is received,
  2073. * when a TSF update has been detected, but also when an new IBSS is created or
  2074. * when we otherwise know we have to update the timers, but we keep it in this
  2075. * function to have it all together in one place.
  2076. */
  2077. static void
  2078. ath5k_beacon_update_timers(struct ath5k_softc *sc, u64 bc_tsf)
  2079. {
  2080. struct ath5k_hw *ah = sc->ah;
  2081. u32 nexttbtt, intval, hw_tu, bc_tu;
  2082. u64 hw_tsf;
  2083. intval = sc->bintval & AR5K_BEACON_PERIOD;
  2084. if (WARN_ON(!intval))
  2085. return;
  2086. /* beacon TSF converted to TU */
  2087. bc_tu = TSF_TO_TU(bc_tsf);
  2088. /* current TSF converted to TU */
  2089. hw_tsf = ath5k_hw_get_tsf64(ah);
  2090. hw_tu = TSF_TO_TU(hw_tsf);
  2091. #define FUDGE 3
  2092. /* we use FUDGE to make sure the next TBTT is ahead of the current TU */
  2093. if (bc_tsf == -1) {
  2094. /*
  2095. * no beacons received, called internally.
  2096. * just need to refresh timers based on HW TSF.
  2097. */
  2098. nexttbtt = roundup(hw_tu + FUDGE, intval);
  2099. } else if (bc_tsf == 0) {
  2100. /*
  2101. * no beacon received, probably called by ath5k_reset_tsf().
  2102. * reset TSF to start with 0.
  2103. */
  2104. nexttbtt = intval;
  2105. intval |= AR5K_BEACON_RESET_TSF;
  2106. } else if (bc_tsf > hw_tsf) {
  2107. /*
  2108. * beacon received, SW merge happend but HW TSF not yet updated.
  2109. * not possible to reconfigure timers yet, but next time we
  2110. * receive a beacon with the same BSSID, the hardware will
  2111. * automatically update the TSF and then we need to reconfigure
  2112. * the timers.
  2113. */
  2114. ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
  2115. "need to wait for HW TSF sync\n");
  2116. return;
  2117. } else {
  2118. /*
  2119. * most important case for beacon synchronization between STA.
  2120. *
  2121. * beacon received and HW TSF has been already updated by HW.
  2122. * update next TBTT based on the TSF of the beacon, but make
  2123. * sure it is ahead of our local TSF timer.
  2124. */
  2125. nexttbtt = bc_tu + roundup(hw_tu + FUDGE - bc_tu, intval);
  2126. }
  2127. #undef FUDGE
  2128. sc->nexttbtt = nexttbtt;
  2129. intval |= AR5K_BEACON_ENA;
  2130. ath5k_hw_init_beacon(ah, nexttbtt, intval);
  2131. /*
  2132. * debugging output last in order to preserve the time critical aspect
  2133. * of this function
  2134. */
  2135. if (bc_tsf == -1)
  2136. ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
  2137. "reconfigured timers based on HW TSF\n");
  2138. else if (bc_tsf == 0)
  2139. ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
  2140. "reset HW TSF and timers\n");
  2141. else
  2142. ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
  2143. "updated timers based on beacon TSF\n");
  2144. ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
  2145. "bc_tsf %llx hw_tsf %llx bc_tu %u hw_tu %u nexttbtt %u\n",
  2146. (unsigned long long) bc_tsf,
  2147. (unsigned long long) hw_tsf, bc_tu, hw_tu, nexttbtt);
  2148. ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON, "intval %u %s %s\n",
  2149. intval & AR5K_BEACON_PERIOD,
  2150. intval & AR5K_BEACON_ENA ? "AR5K_BEACON_ENA" : "",
  2151. intval & AR5K_BEACON_RESET_TSF ? "AR5K_BEACON_RESET_TSF" : "");
  2152. }
  2153. /**
  2154. * ath5k_beacon_config - Configure the beacon queues and interrupts
  2155. *
  2156. * @sc: struct ath5k_softc pointer we are operating on
  2157. *
  2158. * In IBSS mode we use a self-linked tx descriptor if possible. We enable SWBA
  2159. * interrupts to detect TSF updates only.
  2160. */
  2161. static void
  2162. ath5k_beacon_config(struct ath5k_softc *sc)
  2163. {
  2164. struct ath5k_hw *ah = sc->ah;
  2165. unsigned long flags;
  2166. spin_lock_irqsave(&sc->block, flags);
  2167. sc->bmisscount = 0;
  2168. sc->imask &= ~(AR5K_INT_BMISS | AR5K_INT_SWBA);
  2169. if (sc->enable_beacon) {
  2170. /*
  2171. * In IBSS mode we use a self-linked tx descriptor and let the
  2172. * hardware send the beacons automatically. We have to load it
  2173. * only once here.
  2174. * We use the SWBA interrupt only to keep track of the beacon
  2175. * timers in order to detect automatic TSF updates.
  2176. */
  2177. ath5k_beaconq_config(sc);
  2178. sc->imask |= AR5K_INT_SWBA;
  2179. if (sc->opmode == NL80211_IFTYPE_ADHOC) {
  2180. if (ath5k_hw_hasveol(ah))
  2181. ath5k_beacon_send(sc);
  2182. } else
  2183. ath5k_beacon_update_timers(sc, -1);
  2184. } else {
  2185. ath5k_hw_stop_tx_dma(sc->ah, sc->bhalq);
  2186. }
  2187. ath5k_hw_set_imr(ah, sc->imask);
  2188. mmiowb();
  2189. spin_unlock_irqrestore(&sc->block, flags);
  2190. }
  2191. static void ath5k_tasklet_beacon(unsigned long data)
  2192. {
  2193. struct ath5k_softc *sc = (struct ath5k_softc *) data;
  2194. /*
  2195. * Software beacon alert--time to send a beacon.
  2196. *
  2197. * In IBSS mode we use this interrupt just to
  2198. * keep track of the next TBTT (target beacon
  2199. * transmission time) in order to detect wether
  2200. * automatic TSF updates happened.
  2201. */
  2202. if (sc->opmode == NL80211_IFTYPE_ADHOC) {
  2203. /* XXX: only if VEOL suppported */
  2204. u64 tsf = ath5k_hw_get_tsf64(sc->ah);
  2205. sc->nexttbtt += sc->bintval;
  2206. ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
  2207. "SWBA nexttbtt: %x hw_tu: %x "
  2208. "TSF: %llx\n",
  2209. sc->nexttbtt,
  2210. TSF_TO_TU(tsf),
  2211. (unsigned long long) tsf);
  2212. } else {
  2213. spin_lock(&sc->block);
  2214. ath5k_beacon_send(sc);
  2215. spin_unlock(&sc->block);
  2216. }
  2217. }
  2218. /********************\
  2219. * Interrupt handling *
  2220. \********************/
  2221. static int
  2222. ath5k_init(struct ath5k_softc *sc)
  2223. {
  2224. struct ath5k_hw *ah = sc->ah;
  2225. int ret, i;
  2226. mutex_lock(&sc->lock);
  2227. ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "mode %d\n", sc->opmode);
  2228. /*
  2229. * Stop anything previously setup. This is safe
  2230. * no matter this is the first time through or not.
  2231. */
  2232. ath5k_stop_locked(sc);
  2233. /*
  2234. * The basic interface to setting the hardware in a good
  2235. * state is ``reset''. On return the hardware is known to
  2236. * be powered up and with interrupts disabled. This must
  2237. * be followed by initialization of the appropriate bits
  2238. * and then setup of the interrupt mask.
  2239. */
  2240. sc->curchan = sc->hw->conf.channel;
  2241. sc->curband = &sc->sbands[sc->curchan->band];
  2242. sc->imask = AR5K_INT_RXOK | AR5K_INT_RXERR | AR5K_INT_RXEOL |
  2243. AR5K_INT_RXORN | AR5K_INT_TXDESC | AR5K_INT_TXEOL |
  2244. AR5K_INT_FATAL | AR5K_INT_GLOBAL | AR5K_INT_MIB;
  2245. ret = ath5k_reset(sc, NULL);
  2246. if (ret)
  2247. goto done;
  2248. ath5k_rfkill_hw_start(ah);
  2249. /*
  2250. * Reset the key cache since some parts do not reset the
  2251. * contents on initial power up or resume from suspend.
  2252. */
  2253. for (i = 0; i < AR5K_KEYTABLE_SIZE; i++)
  2254. ath5k_hw_reset_key(ah, i);
  2255. ath5k_hw_set_ack_bitrate_high(ah, true);
  2256. ret = 0;
  2257. done:
  2258. mmiowb();
  2259. mutex_unlock(&sc->lock);
  2260. return ret;
  2261. }
  2262. static int
  2263. ath5k_stop_locked(struct ath5k_softc *sc)
  2264. {
  2265. struct ath5k_hw *ah = sc->ah;
  2266. ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "invalid %u\n",
  2267. test_bit(ATH_STAT_INVALID, sc->status));
  2268. /*
  2269. * Shutdown the hardware and driver:
  2270. * stop output from above
  2271. * disable interrupts
  2272. * turn off timers
  2273. * turn off the radio
  2274. * clear transmit machinery
  2275. * clear receive machinery
  2276. * drain and release tx queues
  2277. * reclaim beacon resources
  2278. * power down hardware
  2279. *
  2280. * Note that some of this work is not possible if the
  2281. * hardware is gone (invalid).
  2282. */
  2283. ieee80211_stop_queues(sc->hw);
  2284. if (!test_bit(ATH_STAT_INVALID, sc->status)) {
  2285. ath5k_led_off(sc);
  2286. ath5k_hw_set_imr(ah, 0);
  2287. synchronize_irq(sc->pdev->irq);
  2288. }
  2289. ath5k_txq_cleanup(sc);
  2290. if (!test_bit(ATH_STAT_INVALID, sc->status)) {
  2291. ath5k_rx_stop(sc);
  2292. ath5k_hw_phy_disable(ah);
  2293. }
  2294. return 0;
  2295. }
  2296. static void stop_tasklets(struct ath5k_softc *sc)
  2297. {
  2298. tasklet_kill(&sc->rxtq);
  2299. tasklet_kill(&sc->txtq);
  2300. tasklet_kill(&sc->calib);
  2301. tasklet_kill(&sc->beacontq);
  2302. tasklet_kill(&sc->ani_tasklet);
  2303. }
  2304. /*
  2305. * Stop the device, grabbing the top-level lock to protect
  2306. * against concurrent entry through ath5k_init (which can happen
  2307. * if another thread does a system call and the thread doing the
  2308. * stop is preempted).
  2309. */
  2310. static int
  2311. ath5k_stop_hw(struct ath5k_softc *sc)
  2312. {
  2313. int ret;
  2314. mutex_lock(&sc->lock);
  2315. ret = ath5k_stop_locked(sc);
  2316. if (ret == 0 && !test_bit(ATH_STAT_INVALID, sc->status)) {
  2317. /*
  2318. * Don't set the card in full sleep mode!
  2319. *
  2320. * a) When the device is in this state it must be carefully
  2321. * woken up or references to registers in the PCI clock
  2322. * domain may freeze the bus (and system). This varies
  2323. * by chip and is mostly an issue with newer parts
  2324. * (madwifi sources mentioned srev >= 0x78) that go to
  2325. * sleep more quickly.
  2326. *
  2327. * b) On older chips full sleep results a weird behaviour
  2328. * during wakeup. I tested various cards with srev < 0x78
  2329. * and they don't wake up after module reload, a second
  2330. * module reload is needed to bring the card up again.
  2331. *
  2332. * Until we figure out what's going on don't enable
  2333. * full chip reset on any chip (this is what Legacy HAL
  2334. * and Sam's HAL do anyway). Instead Perform a full reset
  2335. * on the device (same as initial state after attach) and
  2336. * leave it idle (keep MAC/BB on warm reset) */
  2337. ret = ath5k_hw_on_hold(sc->ah);
  2338. ATH5K_DBG(sc, ATH5K_DEBUG_RESET,
  2339. "putting device to sleep\n");
  2340. }
  2341. ath5k_txbuf_free_skb(sc, sc->bbuf);
  2342. mmiowb();
  2343. mutex_unlock(&sc->lock);
  2344. stop_tasklets(sc);
  2345. ath5k_rfkill_hw_stop(sc->ah);
  2346. return ret;
  2347. }
  2348. static void
  2349. ath5k_intr_calibration_poll(struct ath5k_hw *ah)
  2350. {
  2351. if (time_is_before_eq_jiffies(ah->ah_cal_next_ani) &&
  2352. !(ah->ah_cal_mask & AR5K_CALIBRATION_FULL)) {
  2353. /* run ANI only when full calibration is not active */
  2354. ah->ah_cal_next_ani = jiffies +
  2355. msecs_to_jiffies(ATH5K_TUNE_CALIBRATION_INTERVAL_ANI);
  2356. tasklet_schedule(&ah->ah_sc->ani_tasklet);
  2357. } else if (time_is_before_eq_jiffies(ah->ah_cal_next_full)) {
  2358. ah->ah_cal_next_full = jiffies +
  2359. msecs_to_jiffies(ATH5K_TUNE_CALIBRATION_INTERVAL_FULL);
  2360. tasklet_schedule(&ah->ah_sc->calib);
  2361. }
  2362. /* we could use SWI to generate enough interrupts to meet our
  2363. * calibration interval requirements, if necessary:
  2364. * AR5K_REG_ENABLE_BITS(ah, AR5K_CR, AR5K_CR_SWI); */
  2365. }
  2366. static irqreturn_t
  2367. ath5k_intr(int irq, void *dev_id)
  2368. {
  2369. struct ath5k_softc *sc = dev_id;
  2370. struct ath5k_hw *ah = sc->ah;
  2371. enum ath5k_int status;
  2372. unsigned int counter = 1000;
  2373. if (unlikely(test_bit(ATH_STAT_INVALID, sc->status) ||
  2374. !ath5k_hw_is_intr_pending(ah)))
  2375. return IRQ_NONE;
  2376. do {
  2377. ath5k_hw_get_isr(ah, &status); /* NB: clears IRQ too */
  2378. ATH5K_DBG(sc, ATH5K_DEBUG_INTR, "status 0x%x/0x%x\n",
  2379. status, sc->imask);
  2380. if (unlikely(status & AR5K_INT_FATAL)) {
  2381. /*
  2382. * Fatal errors are unrecoverable.
  2383. * Typically these are caused by DMA errors.
  2384. */
  2385. ATH5K_DBG(sc, ATH5K_DEBUG_RESET,
  2386. "fatal int, resetting\n");
  2387. ieee80211_queue_work(sc->hw, &sc->reset_work);
  2388. } else if (unlikely(status & AR5K_INT_RXORN)) {
  2389. /*
  2390. * Receive buffers are full. Either the bus is busy or
  2391. * the CPU is not fast enough to process all received
  2392. * frames.
  2393. * Older chipsets need a reset to come out of this
  2394. * condition, but we treat it as RX for newer chips.
  2395. * We don't know exactly which versions need a reset -
  2396. * this guess is copied from the HAL.
  2397. */
  2398. sc->stats.rxorn_intr++;
  2399. if (ah->ah_mac_srev < AR5K_SREV_AR5212) {
  2400. ATH5K_DBG(sc, ATH5K_DEBUG_RESET,
  2401. "rx overrun, resetting\n");
  2402. ieee80211_queue_work(sc->hw, &sc->reset_work);
  2403. }
  2404. else
  2405. tasklet_schedule(&sc->rxtq);
  2406. } else {
  2407. if (status & AR5K_INT_SWBA) {
  2408. tasklet_hi_schedule(&sc->beacontq);
  2409. }
  2410. if (status & AR5K_INT_RXEOL) {
  2411. /*
  2412. * NB: the hardware should re-read the link when
  2413. * RXE bit is written, but it doesn't work at
  2414. * least on older hardware revs.
  2415. */
  2416. sc->stats.rxeol_intr++;
  2417. }
  2418. if (status & AR5K_INT_TXURN) {
  2419. /* bump tx trigger level */
  2420. ath5k_hw_update_tx_triglevel(ah, true);
  2421. }
  2422. if (status & (AR5K_INT_RXOK | AR5K_INT_RXERR))
  2423. tasklet_schedule(&sc->rxtq);
  2424. if (status & (AR5K_INT_TXOK | AR5K_INT_TXDESC
  2425. | AR5K_INT_TXERR | AR5K_INT_TXEOL))
  2426. tasklet_schedule(&sc->txtq);
  2427. if (status & AR5K_INT_BMISS) {
  2428. /* TODO */
  2429. }
  2430. if (status & AR5K_INT_MIB) {
  2431. sc->stats.mib_intr++;
  2432. ath5k_hw_update_mib_counters(ah);
  2433. ath5k_ani_mib_intr(ah);
  2434. }
  2435. if (status & AR5K_INT_GPIO)
  2436. tasklet_schedule(&sc->rf_kill.toggleq);
  2437. }
  2438. } while (ath5k_hw_is_intr_pending(ah) && --counter > 0);
  2439. if (unlikely(!counter))
  2440. ATH5K_WARN(sc, "too many interrupts, giving up for now\n");
  2441. ath5k_intr_calibration_poll(ah);
  2442. return IRQ_HANDLED;
  2443. }
  2444. /*
  2445. * Periodically recalibrate the PHY to account
  2446. * for temperature/environment changes.
  2447. */
  2448. static void
  2449. ath5k_tasklet_calibrate(unsigned long data)
  2450. {
  2451. struct ath5k_softc *sc = (void *)data;
  2452. struct ath5k_hw *ah = sc->ah;
  2453. /* Only full calibration for now */
  2454. ah->ah_cal_mask |= AR5K_CALIBRATION_FULL;
  2455. ATH5K_DBG(sc, ATH5K_DEBUG_CALIBRATE, "channel %u/%x\n",
  2456. ieee80211_frequency_to_channel(sc->curchan->center_freq),
  2457. sc->curchan->hw_value);
  2458. if (ath5k_hw_gainf_calibrate(ah) == AR5K_RFGAIN_NEED_CHANGE) {
  2459. /*
  2460. * Rfgain is out of bounds, reset the chip
  2461. * to load new gain values.
  2462. */
  2463. ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "calibration, resetting\n");
  2464. ieee80211_queue_work(sc->hw, &sc->reset_work);
  2465. }
  2466. if (ath5k_hw_phy_calibrate(ah, sc->curchan))
  2467. ATH5K_ERR(sc, "calibration of channel %u failed\n",
  2468. ieee80211_frequency_to_channel(
  2469. sc->curchan->center_freq));
  2470. /* Noise floor calibration interrupts rx/tx path while I/Q calibration
  2471. * doesn't. We stop the queues so that calibration doesn't interfere
  2472. * with TX and don't run it as often */
  2473. if (time_is_before_eq_jiffies(ah->ah_cal_next_nf)) {
  2474. ah->ah_cal_next_nf = jiffies +
  2475. msecs_to_jiffies(ATH5K_TUNE_CALIBRATION_INTERVAL_NF);
  2476. ieee80211_stop_queues(sc->hw);
  2477. ath5k_hw_update_noise_floor(ah);
  2478. ieee80211_wake_queues(sc->hw);
  2479. }
  2480. ah->ah_cal_mask &= ~AR5K_CALIBRATION_FULL;
  2481. }
  2482. static void
  2483. ath5k_tasklet_ani(unsigned long data)
  2484. {
  2485. struct ath5k_softc *sc = (void *)data;
  2486. struct ath5k_hw *ah = sc->ah;
  2487. ah->ah_cal_mask |= AR5K_CALIBRATION_ANI;
  2488. ath5k_ani_calibration(ah);
  2489. ah->ah_cal_mask &= ~AR5K_CALIBRATION_ANI;
  2490. }
  2491. /********************\
  2492. * Mac80211 functions *
  2493. \********************/
  2494. static int
  2495. ath5k_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
  2496. {
  2497. struct ath5k_softc *sc = hw->priv;
  2498. return ath5k_tx_queue(hw, skb, sc->txq);
  2499. }
  2500. static int ath5k_tx_queue(struct ieee80211_hw *hw, struct sk_buff *skb,
  2501. struct ath5k_txq *txq)
  2502. {
  2503. struct ath5k_softc *sc = hw->priv;
  2504. struct ath5k_buf *bf;
  2505. unsigned long flags;
  2506. int padsize;
  2507. ath5k_debug_dump_skb(sc, skb, "TX ", 1);
  2508. if (sc->opmode == NL80211_IFTYPE_MONITOR)
  2509. ATH5K_DBG(sc, ATH5K_DEBUG_XMIT, "tx in monitor (scan?)\n");
  2510. /*
  2511. * the hardware expects the header padded to 4 byte boundaries
  2512. * if this is not the case we add the padding after the header
  2513. */
  2514. padsize = ath5k_add_padding(skb);
  2515. if (padsize < 0) {
  2516. ATH5K_ERR(sc, "tx hdrlen not %%4: not enough"
  2517. " headroom to pad");
  2518. goto drop_packet;
  2519. }
  2520. spin_lock_irqsave(&sc->txbuflock, flags);
  2521. if (list_empty(&sc->txbuf)) {
  2522. ATH5K_ERR(sc, "no further txbuf available, dropping packet\n");
  2523. spin_unlock_irqrestore(&sc->txbuflock, flags);
  2524. ieee80211_stop_queue(hw, skb_get_queue_mapping(skb));
  2525. goto drop_packet;
  2526. }
  2527. bf = list_first_entry(&sc->txbuf, struct ath5k_buf, list);
  2528. list_del(&bf->list);
  2529. sc->txbuf_len--;
  2530. if (list_empty(&sc->txbuf))
  2531. ieee80211_stop_queues(hw);
  2532. spin_unlock_irqrestore(&sc->txbuflock, flags);
  2533. bf->skb = skb;
  2534. if (ath5k_txbuf_setup(sc, bf, txq, padsize)) {
  2535. bf->skb = NULL;
  2536. spin_lock_irqsave(&sc->txbuflock, flags);
  2537. list_add_tail(&bf->list, &sc->txbuf);
  2538. sc->txbuf_len++;
  2539. spin_unlock_irqrestore(&sc->txbuflock, flags);
  2540. goto drop_packet;
  2541. }
  2542. return NETDEV_TX_OK;
  2543. drop_packet:
  2544. dev_kfree_skb_any(skb);
  2545. return NETDEV_TX_OK;
  2546. }
  2547. /*
  2548. * Reset the hardware. If chan is not NULL, then also pause rx/tx
  2549. * and change to the given channel.
  2550. *
  2551. * This should be called with sc->lock.
  2552. */
  2553. static int
  2554. ath5k_reset(struct ath5k_softc *sc, struct ieee80211_channel *chan)
  2555. {
  2556. struct ath5k_hw *ah = sc->ah;
  2557. int ret;
  2558. ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "resetting\n");
  2559. ath5k_hw_set_imr(ah, 0);
  2560. synchronize_irq(sc->pdev->irq);
  2561. stop_tasklets(sc);
  2562. if (chan) {
  2563. ath5k_txq_cleanup(sc);
  2564. ath5k_rx_stop(sc);
  2565. sc->curchan = chan;
  2566. sc->curband = &sc->sbands[chan->band];
  2567. }
  2568. ret = ath5k_hw_reset(ah, sc->opmode, sc->curchan, chan != NULL);
  2569. if (ret) {
  2570. ATH5K_ERR(sc, "can't reset hardware (%d)\n", ret);
  2571. goto err;
  2572. }
  2573. ret = ath5k_rx_start(sc);
  2574. if (ret) {
  2575. ATH5K_ERR(sc, "can't start recv logic\n");
  2576. goto err;
  2577. }
  2578. ath5k_ani_init(ah, ah->ah_sc->ani_state.ani_mode);
  2579. ah->ah_cal_next_full = jiffies;
  2580. ah->ah_cal_next_ani = jiffies;
  2581. ah->ah_cal_next_nf = jiffies;
  2582. /*
  2583. * Change channels and update the h/w rate map if we're switching;
  2584. * e.g. 11a to 11b/g.
  2585. *
  2586. * We may be doing a reset in response to an ioctl that changes the
  2587. * channel so update any state that might change as a result.
  2588. *
  2589. * XXX needed?
  2590. */
  2591. /* ath5k_chan_change(sc, c); */
  2592. ath5k_beacon_config(sc);
  2593. /* intrs are enabled by ath5k_beacon_config */
  2594. ieee80211_wake_queues(sc->hw);
  2595. return 0;
  2596. err:
  2597. return ret;
  2598. }
  2599. static void ath5k_reset_work(struct work_struct *work)
  2600. {
  2601. struct ath5k_softc *sc = container_of(work, struct ath5k_softc,
  2602. reset_work);
  2603. mutex_lock(&sc->lock);
  2604. ath5k_reset(sc, sc->curchan);
  2605. mutex_unlock(&sc->lock);
  2606. }
  2607. static int ath5k_start(struct ieee80211_hw *hw)
  2608. {
  2609. return ath5k_init(hw->priv);
  2610. }
  2611. static void ath5k_stop(struct ieee80211_hw *hw)
  2612. {
  2613. ath5k_stop_hw(hw->priv);
  2614. }
  2615. static int ath5k_add_interface(struct ieee80211_hw *hw,
  2616. struct ieee80211_vif *vif)
  2617. {
  2618. struct ath5k_softc *sc = hw->priv;
  2619. int ret;
  2620. mutex_lock(&sc->lock);
  2621. if (sc->vif) {
  2622. ret = 0;
  2623. goto end;
  2624. }
  2625. sc->vif = vif;
  2626. switch (vif->type) {
  2627. case NL80211_IFTYPE_AP:
  2628. case NL80211_IFTYPE_STATION:
  2629. case NL80211_IFTYPE_ADHOC:
  2630. case NL80211_IFTYPE_MESH_POINT:
  2631. case NL80211_IFTYPE_MONITOR:
  2632. sc->opmode = vif->type;
  2633. break;
  2634. default:
  2635. ret = -EOPNOTSUPP;
  2636. goto end;
  2637. }
  2638. ATH5K_DBG(sc, ATH5K_DEBUG_MODE, "add interface mode %d\n", sc->opmode);
  2639. ath5k_hw_set_lladdr(sc->ah, vif->addr);
  2640. ath5k_mode_setup(sc);
  2641. ret = 0;
  2642. end:
  2643. mutex_unlock(&sc->lock);
  2644. return ret;
  2645. }
  2646. static void
  2647. ath5k_remove_interface(struct ieee80211_hw *hw,
  2648. struct ieee80211_vif *vif)
  2649. {
  2650. struct ath5k_softc *sc = hw->priv;
  2651. u8 mac[ETH_ALEN] = {};
  2652. mutex_lock(&sc->lock);
  2653. if (sc->vif != vif)
  2654. goto end;
  2655. ath5k_hw_set_lladdr(sc->ah, mac);
  2656. sc->vif = NULL;
  2657. end:
  2658. mutex_unlock(&sc->lock);
  2659. }
  2660. /*
  2661. * TODO: Phy disable/diversity etc
  2662. */
  2663. static int
  2664. ath5k_config(struct ieee80211_hw *hw, u32 changed)
  2665. {
  2666. struct ath5k_softc *sc = hw->priv;
  2667. struct ath5k_hw *ah = sc->ah;
  2668. struct ieee80211_conf *conf = &hw->conf;
  2669. int ret = 0;
  2670. mutex_lock(&sc->lock);
  2671. if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
  2672. ret = ath5k_chan_set(sc, conf->channel);
  2673. if (ret < 0)
  2674. goto unlock;
  2675. }
  2676. if ((changed & IEEE80211_CONF_CHANGE_POWER) &&
  2677. (sc->power_level != conf->power_level)) {
  2678. sc->power_level = conf->power_level;
  2679. /* Half dB steps */
  2680. ath5k_hw_set_txpower_limit(ah, (conf->power_level * 2));
  2681. }
  2682. /* TODO:
  2683. * 1) Move this on config_interface and handle each case
  2684. * separately eg. when we have only one STA vif, use
  2685. * AR5K_ANTMODE_SINGLE_AP
  2686. *
  2687. * 2) Allow the user to change antenna mode eg. when only
  2688. * one antenna is present
  2689. *
  2690. * 3) Allow the user to set default/tx antenna when possible
  2691. *
  2692. * 4) Default mode should handle 90% of the cases, together
  2693. * with fixed a/b and single AP modes we should be able to
  2694. * handle 99%. Sectored modes are extreme cases and i still
  2695. * haven't found a usage for them. If we decide to support them,
  2696. * then we must allow the user to set how many tx antennas we
  2697. * have available
  2698. */
  2699. ath5k_hw_set_antenna_mode(ah, ah->ah_ant_mode);
  2700. unlock:
  2701. mutex_unlock(&sc->lock);
  2702. return ret;
  2703. }
  2704. static u64 ath5k_prepare_multicast(struct ieee80211_hw *hw,
  2705. struct netdev_hw_addr_list *mc_list)
  2706. {
  2707. u32 mfilt[2], val;
  2708. u8 pos;
  2709. struct netdev_hw_addr *ha;
  2710. mfilt[0] = 0;
  2711. mfilt[1] = 1;
  2712. netdev_hw_addr_list_for_each(ha, mc_list) {
  2713. /* calculate XOR of eight 6-bit values */
  2714. val = get_unaligned_le32(ha->addr + 0);
  2715. pos = (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
  2716. val = get_unaligned_le32(ha->addr + 3);
  2717. pos ^= (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
  2718. pos &= 0x3f;
  2719. mfilt[pos / 32] |= (1 << (pos % 32));
  2720. /* XXX: we might be able to just do this instead,
  2721. * but not sure, needs testing, if we do use this we'd
  2722. * neet to inform below to not reset the mcast */
  2723. /* ath5k_hw_set_mcast_filterindex(ah,
  2724. * ha->addr[5]); */
  2725. }
  2726. return ((u64)(mfilt[1]) << 32) | mfilt[0];
  2727. }
  2728. #define SUPPORTED_FIF_FLAGS \
  2729. FIF_PROMISC_IN_BSS | FIF_ALLMULTI | FIF_FCSFAIL | \
  2730. FIF_PLCPFAIL | FIF_CONTROL | FIF_OTHER_BSS | \
  2731. FIF_BCN_PRBRESP_PROMISC
  2732. /*
  2733. * o always accept unicast, broadcast, and multicast traffic
  2734. * o multicast traffic for all BSSIDs will be enabled if mac80211
  2735. * says it should be
  2736. * o maintain current state of phy ofdm or phy cck error reception.
  2737. * If the hardware detects any of these type of errors then
  2738. * ath5k_hw_get_rx_filter() will pass to us the respective
  2739. * hardware filters to be able to receive these type of frames.
  2740. * o probe request frames are accepted only when operating in
  2741. * hostap, adhoc, or monitor modes
  2742. * o enable promiscuous mode according to the interface state
  2743. * o accept beacons:
  2744. * - when operating in adhoc mode so the 802.11 layer creates
  2745. * node table entries for peers,
  2746. * - when operating in station mode for collecting rssi data when
  2747. * the station is otherwise quiet, or
  2748. * - when scanning
  2749. */
  2750. static void ath5k_configure_filter(struct ieee80211_hw *hw,
  2751. unsigned int changed_flags,
  2752. unsigned int *new_flags,
  2753. u64 multicast)
  2754. {
  2755. struct ath5k_softc *sc = hw->priv;
  2756. struct ath5k_hw *ah = sc->ah;
  2757. u32 mfilt[2], rfilt;
  2758. mutex_lock(&sc->lock);
  2759. mfilt[0] = multicast;
  2760. mfilt[1] = multicast >> 32;
  2761. /* Only deal with supported flags */
  2762. changed_flags &= SUPPORTED_FIF_FLAGS;
  2763. *new_flags &= SUPPORTED_FIF_FLAGS;
  2764. /* If HW detects any phy or radar errors, leave those filters on.
  2765. * Also, always enable Unicast, Broadcasts and Multicast
  2766. * XXX: move unicast, bssid broadcasts and multicast to mac80211 */
  2767. rfilt = (ath5k_hw_get_rx_filter(ah) & (AR5K_RX_FILTER_PHYERR)) |
  2768. (AR5K_RX_FILTER_UCAST | AR5K_RX_FILTER_BCAST |
  2769. AR5K_RX_FILTER_MCAST);
  2770. if (changed_flags & (FIF_PROMISC_IN_BSS | FIF_OTHER_BSS)) {
  2771. if (*new_flags & FIF_PROMISC_IN_BSS) {
  2772. __set_bit(ATH_STAT_PROMISC, sc->status);
  2773. } else {
  2774. __clear_bit(ATH_STAT_PROMISC, sc->status);
  2775. }
  2776. }
  2777. if (test_bit(ATH_STAT_PROMISC, sc->status))
  2778. rfilt |= AR5K_RX_FILTER_PROM;
  2779. /* Note, AR5K_RX_FILTER_MCAST is already enabled */
  2780. if (*new_flags & FIF_ALLMULTI) {
  2781. mfilt[0] = ~0;
  2782. mfilt[1] = ~0;
  2783. }
  2784. /* This is the best we can do */
  2785. if (*new_flags & (FIF_FCSFAIL | FIF_PLCPFAIL))
  2786. rfilt |= AR5K_RX_FILTER_PHYERR;
  2787. /* FIF_BCN_PRBRESP_PROMISC really means to enable beacons
  2788. * and probes for any BSSID, this needs testing */
  2789. if (*new_flags & FIF_BCN_PRBRESP_PROMISC)
  2790. rfilt |= AR5K_RX_FILTER_BEACON | AR5K_RX_FILTER_PROBEREQ;
  2791. /* FIF_CONTROL doc says that if FIF_PROMISC_IN_BSS is not
  2792. * set we should only pass on control frames for this
  2793. * station. This needs testing. I believe right now this
  2794. * enables *all* control frames, which is OK.. but
  2795. * but we should see if we can improve on granularity */
  2796. if (*new_flags & FIF_CONTROL)
  2797. rfilt |= AR5K_RX_FILTER_CONTROL;
  2798. /* Additional settings per mode -- this is per ath5k */
  2799. /* XXX move these to mac80211, and add a beacon IFF flag to mac80211 */
  2800. switch (sc->opmode) {
  2801. case NL80211_IFTYPE_MESH_POINT:
  2802. case NL80211_IFTYPE_MONITOR:
  2803. rfilt |= AR5K_RX_FILTER_CONTROL |
  2804. AR5K_RX_FILTER_BEACON |
  2805. AR5K_RX_FILTER_PROBEREQ |
  2806. AR5K_RX_FILTER_PROM;
  2807. break;
  2808. case NL80211_IFTYPE_AP:
  2809. case NL80211_IFTYPE_ADHOC:
  2810. rfilt |= AR5K_RX_FILTER_PROBEREQ |
  2811. AR5K_RX_FILTER_BEACON;
  2812. break;
  2813. case NL80211_IFTYPE_STATION:
  2814. if (sc->assoc)
  2815. rfilt |= AR5K_RX_FILTER_BEACON;
  2816. default:
  2817. break;
  2818. }
  2819. /* Set filters */
  2820. ath5k_hw_set_rx_filter(ah, rfilt);
  2821. /* Set multicast bits */
  2822. ath5k_hw_set_mcast_filter(ah, mfilt[0], mfilt[1]);
  2823. /* Set the cached hw filter flags, this will alter actually
  2824. * be set in HW */
  2825. sc->filter_flags = rfilt;
  2826. mutex_unlock(&sc->lock);
  2827. }
  2828. static int
  2829. ath5k_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
  2830. struct ieee80211_vif *vif, struct ieee80211_sta *sta,
  2831. struct ieee80211_key_conf *key)
  2832. {
  2833. struct ath5k_softc *sc = hw->priv;
  2834. struct ath5k_hw *ah = sc->ah;
  2835. struct ath_common *common = ath5k_hw_common(ah);
  2836. int ret = 0;
  2837. if (modparam_nohwcrypt)
  2838. return -EOPNOTSUPP;
  2839. if (sc->opmode == NL80211_IFTYPE_AP)
  2840. return -EOPNOTSUPP;
  2841. switch (key->alg) {
  2842. case ALG_WEP:
  2843. case ALG_TKIP:
  2844. break;
  2845. case ALG_CCMP:
  2846. if (sc->ah->ah_aes_support)
  2847. break;
  2848. return -EOPNOTSUPP;
  2849. default:
  2850. WARN_ON(1);
  2851. return -EINVAL;
  2852. }
  2853. mutex_lock(&sc->lock);
  2854. switch (cmd) {
  2855. case SET_KEY:
  2856. ret = ath5k_hw_set_key(sc->ah, key->keyidx, key,
  2857. sta ? sta->addr : NULL);
  2858. if (ret) {
  2859. ATH5K_ERR(sc, "can't set the key\n");
  2860. goto unlock;
  2861. }
  2862. __set_bit(key->keyidx, common->keymap);
  2863. key->hw_key_idx = key->keyidx;
  2864. key->flags |= (IEEE80211_KEY_FLAG_GENERATE_IV |
  2865. IEEE80211_KEY_FLAG_GENERATE_MMIC);
  2866. break;
  2867. case DISABLE_KEY:
  2868. ath5k_hw_reset_key(sc->ah, key->keyidx);
  2869. __clear_bit(key->keyidx, common->keymap);
  2870. break;
  2871. default:
  2872. ret = -EINVAL;
  2873. goto unlock;
  2874. }
  2875. unlock:
  2876. mmiowb();
  2877. mutex_unlock(&sc->lock);
  2878. return ret;
  2879. }
  2880. static int
  2881. ath5k_get_stats(struct ieee80211_hw *hw,
  2882. struct ieee80211_low_level_stats *stats)
  2883. {
  2884. struct ath5k_softc *sc = hw->priv;
  2885. /* Force update */
  2886. ath5k_hw_update_mib_counters(sc->ah);
  2887. stats->dot11ACKFailureCount = sc->stats.ack_fail;
  2888. stats->dot11RTSFailureCount = sc->stats.rts_fail;
  2889. stats->dot11RTSSuccessCount = sc->stats.rts_ok;
  2890. stats->dot11FCSErrorCount = sc->stats.fcs_error;
  2891. return 0;
  2892. }
  2893. static int ath5k_get_survey(struct ieee80211_hw *hw, int idx,
  2894. struct survey_info *survey)
  2895. {
  2896. struct ath5k_softc *sc = hw->priv;
  2897. struct ieee80211_conf *conf = &hw->conf;
  2898. if (idx != 0)
  2899. return -ENOENT;
  2900. survey->channel = conf->channel;
  2901. survey->filled = SURVEY_INFO_NOISE_DBM;
  2902. survey->noise = sc->ah->ah_noise_floor;
  2903. return 0;
  2904. }
  2905. static u64
  2906. ath5k_get_tsf(struct ieee80211_hw *hw)
  2907. {
  2908. struct ath5k_softc *sc = hw->priv;
  2909. return ath5k_hw_get_tsf64(sc->ah);
  2910. }
  2911. static void
  2912. ath5k_set_tsf(struct ieee80211_hw *hw, u64 tsf)
  2913. {
  2914. struct ath5k_softc *sc = hw->priv;
  2915. ath5k_hw_set_tsf64(sc->ah, tsf);
  2916. }
  2917. static void
  2918. ath5k_reset_tsf(struct ieee80211_hw *hw)
  2919. {
  2920. struct ath5k_softc *sc = hw->priv;
  2921. /*
  2922. * in IBSS mode we need to update the beacon timers too.
  2923. * this will also reset the TSF if we call it with 0
  2924. */
  2925. if (sc->opmode == NL80211_IFTYPE_ADHOC)
  2926. ath5k_beacon_update_timers(sc, 0);
  2927. else
  2928. ath5k_hw_reset_tsf(sc->ah);
  2929. }
  2930. /*
  2931. * Updates the beacon that is sent by ath5k_beacon_send. For adhoc,
  2932. * this is called only once at config_bss time, for AP we do it every
  2933. * SWBA interrupt so that the TIM will reflect buffered frames.
  2934. *
  2935. * Called with the beacon lock.
  2936. */
  2937. static int
  2938. ath5k_beacon_update(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
  2939. {
  2940. int ret;
  2941. struct ath5k_softc *sc = hw->priv;
  2942. struct sk_buff *skb;
  2943. if (WARN_ON(!vif)) {
  2944. ret = -EINVAL;
  2945. goto out;
  2946. }
  2947. skb = ieee80211_beacon_get(hw, vif);
  2948. if (!skb) {
  2949. ret = -ENOMEM;
  2950. goto out;
  2951. }
  2952. ath5k_debug_dump_skb(sc, skb, "BC ", 1);
  2953. ath5k_txbuf_free_skb(sc, sc->bbuf);
  2954. sc->bbuf->skb = skb;
  2955. ret = ath5k_beacon_setup(sc, sc->bbuf);
  2956. if (ret)
  2957. sc->bbuf->skb = NULL;
  2958. out:
  2959. return ret;
  2960. }
  2961. static void
  2962. set_beacon_filter(struct ieee80211_hw *hw, bool enable)
  2963. {
  2964. struct ath5k_softc *sc = hw->priv;
  2965. struct ath5k_hw *ah = sc->ah;
  2966. u32 rfilt;
  2967. rfilt = ath5k_hw_get_rx_filter(ah);
  2968. if (enable)
  2969. rfilt |= AR5K_RX_FILTER_BEACON;
  2970. else
  2971. rfilt &= ~AR5K_RX_FILTER_BEACON;
  2972. ath5k_hw_set_rx_filter(ah, rfilt);
  2973. sc->filter_flags = rfilt;
  2974. }
  2975. static void ath5k_bss_info_changed(struct ieee80211_hw *hw,
  2976. struct ieee80211_vif *vif,
  2977. struct ieee80211_bss_conf *bss_conf,
  2978. u32 changes)
  2979. {
  2980. struct ath5k_softc *sc = hw->priv;
  2981. struct ath5k_hw *ah = sc->ah;
  2982. struct ath_common *common = ath5k_hw_common(ah);
  2983. unsigned long flags;
  2984. mutex_lock(&sc->lock);
  2985. if (WARN_ON(sc->vif != vif))
  2986. goto unlock;
  2987. if (changes & BSS_CHANGED_BSSID) {
  2988. /* Cache for later use during resets */
  2989. memcpy(common->curbssid, bss_conf->bssid, ETH_ALEN);
  2990. common->curaid = 0;
  2991. ath5k_hw_set_associd(ah);
  2992. mmiowb();
  2993. }
  2994. if (changes & BSS_CHANGED_BEACON_INT)
  2995. sc->bintval = bss_conf->beacon_int;
  2996. if (changes & BSS_CHANGED_ASSOC) {
  2997. sc->assoc = bss_conf->assoc;
  2998. if (sc->opmode == NL80211_IFTYPE_STATION)
  2999. set_beacon_filter(hw, sc->assoc);
  3000. ath5k_hw_set_ledstate(sc->ah, sc->assoc ?
  3001. AR5K_LED_ASSOC : AR5K_LED_INIT);
  3002. if (bss_conf->assoc) {
  3003. ATH5K_DBG(sc, ATH5K_DEBUG_ANY,
  3004. "Bss Info ASSOC %d, bssid: %pM\n",
  3005. bss_conf->aid, common->curbssid);
  3006. common->curaid = bss_conf->aid;
  3007. ath5k_hw_set_associd(ah);
  3008. /* Once ANI is available you would start it here */
  3009. }
  3010. }
  3011. if (changes & BSS_CHANGED_BEACON) {
  3012. spin_lock_irqsave(&sc->block, flags);
  3013. ath5k_beacon_update(hw, vif);
  3014. spin_unlock_irqrestore(&sc->block, flags);
  3015. }
  3016. if (changes & BSS_CHANGED_BEACON_ENABLED)
  3017. sc->enable_beacon = bss_conf->enable_beacon;
  3018. if (changes & (BSS_CHANGED_BEACON | BSS_CHANGED_BEACON_ENABLED |
  3019. BSS_CHANGED_BEACON_INT))
  3020. ath5k_beacon_config(sc);
  3021. unlock:
  3022. mutex_unlock(&sc->lock);
  3023. }
  3024. static void ath5k_sw_scan_start(struct ieee80211_hw *hw)
  3025. {
  3026. struct ath5k_softc *sc = hw->priv;
  3027. if (!sc->assoc)
  3028. ath5k_hw_set_ledstate(sc->ah, AR5K_LED_SCAN);
  3029. }
  3030. static void ath5k_sw_scan_complete(struct ieee80211_hw *hw)
  3031. {
  3032. struct ath5k_softc *sc = hw->priv;
  3033. ath5k_hw_set_ledstate(sc->ah, sc->assoc ?
  3034. AR5K_LED_ASSOC : AR5K_LED_INIT);
  3035. }
  3036. /**
  3037. * ath5k_set_coverage_class - Set IEEE 802.11 coverage class
  3038. *
  3039. * @hw: struct ieee80211_hw pointer
  3040. * @coverage_class: IEEE 802.11 coverage class number
  3041. *
  3042. * Mac80211 callback. Sets slot time, ACK timeout and CTS timeout for given
  3043. * coverage class. The values are persistent, they are restored after device
  3044. * reset.
  3045. */
  3046. static void ath5k_set_coverage_class(struct ieee80211_hw *hw, u8 coverage_class)
  3047. {
  3048. struct ath5k_softc *sc = hw->priv;
  3049. mutex_lock(&sc->lock);
  3050. ath5k_hw_set_coverage_class(sc->ah, coverage_class);
  3051. mutex_unlock(&sc->lock);
  3052. }