base.c 95 KB

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