base.c 92 KB

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