base.c 82 KB

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