base.c 94 KB

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