base.c 79 KB

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