base.c 96 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617
  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. sc->opmode == NL80211_IFTYPE_MESH_POINT) {
  1622. u64 tsf = ath5k_hw_get_tsf64(ah);
  1623. u32 tsftu = TSF_TO_TU(tsf);
  1624. int slot = ((tsftu % sc->bintval) * ATH_BCBUF) / sc->bintval;
  1625. vif = sc->bslot[(slot + 1) % ATH_BCBUF];
  1626. ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
  1627. "tsf %llx tsftu %x intval %u slot %u vif %p\n",
  1628. (unsigned long long)tsf, tsftu, sc->bintval, slot, vif);
  1629. } else /* only one interface */
  1630. vif = sc->bslot[0];
  1631. if (!vif)
  1632. return;
  1633. avf = (void *)vif->drv_priv;
  1634. bf = avf->bbuf;
  1635. if (unlikely(bf->skb == NULL || sc->opmode == NL80211_IFTYPE_STATION ||
  1636. sc->opmode == NL80211_IFTYPE_MONITOR)) {
  1637. ATH5K_WARN(sc, "bf=%p bf_skb=%p\n", bf, bf ? bf->skb : NULL);
  1638. return;
  1639. }
  1640. /*
  1641. * Stop any current dma and put the new frame on the queue.
  1642. * This should never fail since we check above that no frames
  1643. * are still pending on the queue.
  1644. */
  1645. if (unlikely(ath5k_hw_stop_beacon_queue(ah, sc->bhalq))) {
  1646. ATH5K_WARN(sc, "beacon queue %u didn't start/stop ?\n", sc->bhalq);
  1647. /* NB: hw still stops DMA, so proceed */
  1648. }
  1649. /* refresh the beacon for AP or MESH mode */
  1650. if (sc->opmode == NL80211_IFTYPE_AP ||
  1651. sc->opmode == NL80211_IFTYPE_MESH_POINT)
  1652. ath5k_beacon_update(sc->hw, vif);
  1653. ath5k_hw_set_txdp(ah, sc->bhalq, bf->daddr);
  1654. ath5k_hw_start_tx_dma(ah, sc->bhalq);
  1655. ATH5K_DBG(sc, ATH5K_DEBUG_BEACON, "TXDP[%u] = %llx (%p)\n",
  1656. sc->bhalq, (unsigned long long)bf->daddr, bf->desc);
  1657. skb = ieee80211_get_buffered_bc(sc->hw, vif);
  1658. while (skb) {
  1659. ath5k_tx_queue(sc->hw, skb, sc->cabq);
  1660. skb = ieee80211_get_buffered_bc(sc->hw, vif);
  1661. }
  1662. sc->bsent++;
  1663. }
  1664. /**
  1665. * ath5k_beacon_update_timers - update beacon timers
  1666. *
  1667. * @sc: struct ath5k_softc pointer we are operating on
  1668. * @bc_tsf: the timestamp of the beacon. 0 to reset the TSF. -1 to perform a
  1669. * beacon timer update based on the current HW TSF.
  1670. *
  1671. * Calculate the next target beacon transmit time (TBTT) based on the timestamp
  1672. * of a received beacon or the current local hardware TSF and write it to the
  1673. * beacon timer registers.
  1674. *
  1675. * This is called in a variety of situations, e.g. when a beacon is received,
  1676. * when a TSF update has been detected, but also when an new IBSS is created or
  1677. * when we otherwise know we have to update the timers, but we keep it in this
  1678. * function to have it all together in one place.
  1679. */
  1680. static void
  1681. ath5k_beacon_update_timers(struct ath5k_softc *sc, u64 bc_tsf)
  1682. {
  1683. struct ath5k_hw *ah = sc->ah;
  1684. u32 nexttbtt, intval, hw_tu, bc_tu;
  1685. u64 hw_tsf;
  1686. intval = sc->bintval & AR5K_BEACON_PERIOD;
  1687. if (sc->opmode == NL80211_IFTYPE_AP && sc->num_ap_vifs > 1) {
  1688. intval /= ATH_BCBUF; /* staggered multi-bss beacons */
  1689. if (intval < 15)
  1690. ATH5K_WARN(sc, "intval %u is too low, min 15\n",
  1691. intval);
  1692. }
  1693. if (WARN_ON(!intval))
  1694. return;
  1695. /* beacon TSF converted to TU */
  1696. bc_tu = TSF_TO_TU(bc_tsf);
  1697. /* current TSF converted to TU */
  1698. hw_tsf = ath5k_hw_get_tsf64(ah);
  1699. hw_tu = TSF_TO_TU(hw_tsf);
  1700. #define FUDGE AR5K_TUNE_SW_BEACON_RESP + 3
  1701. /* We use FUDGE to make sure the next TBTT is ahead of the current TU.
  1702. * Since we later substract AR5K_TUNE_SW_BEACON_RESP (10) in the timer
  1703. * configuration we need to make sure it is bigger than that. */
  1704. if (bc_tsf == -1) {
  1705. /*
  1706. * no beacons received, called internally.
  1707. * just need to refresh timers based on HW TSF.
  1708. */
  1709. nexttbtt = roundup(hw_tu + FUDGE, intval);
  1710. } else if (bc_tsf == 0) {
  1711. /*
  1712. * no beacon received, probably called by ath5k_reset_tsf().
  1713. * reset TSF to start with 0.
  1714. */
  1715. nexttbtt = intval;
  1716. intval |= AR5K_BEACON_RESET_TSF;
  1717. } else if (bc_tsf > hw_tsf) {
  1718. /*
  1719. * beacon received, SW merge happend but HW TSF not yet updated.
  1720. * not possible to reconfigure timers yet, but next time we
  1721. * receive a beacon with the same BSSID, the hardware will
  1722. * automatically update the TSF and then we need to reconfigure
  1723. * the timers.
  1724. */
  1725. ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
  1726. "need to wait for HW TSF sync\n");
  1727. return;
  1728. } else {
  1729. /*
  1730. * most important case for beacon synchronization between STA.
  1731. *
  1732. * beacon received and HW TSF has been already updated by HW.
  1733. * update next TBTT based on the TSF of the beacon, but make
  1734. * sure it is ahead of our local TSF timer.
  1735. */
  1736. nexttbtt = bc_tu + roundup(hw_tu + FUDGE - bc_tu, intval);
  1737. }
  1738. #undef FUDGE
  1739. sc->nexttbtt = nexttbtt;
  1740. intval |= AR5K_BEACON_ENA;
  1741. ath5k_hw_init_beacon(ah, nexttbtt, intval);
  1742. /*
  1743. * debugging output last in order to preserve the time critical aspect
  1744. * of this function
  1745. */
  1746. if (bc_tsf == -1)
  1747. ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
  1748. "reconfigured timers based on HW TSF\n");
  1749. else if (bc_tsf == 0)
  1750. ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
  1751. "reset HW TSF and timers\n");
  1752. else
  1753. ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
  1754. "updated timers based on beacon TSF\n");
  1755. ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON,
  1756. "bc_tsf %llx hw_tsf %llx bc_tu %u hw_tu %u nexttbtt %u\n",
  1757. (unsigned long long) bc_tsf,
  1758. (unsigned long long) hw_tsf, bc_tu, hw_tu, nexttbtt);
  1759. ATH5K_DBG_UNLIMIT(sc, ATH5K_DEBUG_BEACON, "intval %u %s %s\n",
  1760. intval & AR5K_BEACON_PERIOD,
  1761. intval & AR5K_BEACON_ENA ? "AR5K_BEACON_ENA" : "",
  1762. intval & AR5K_BEACON_RESET_TSF ? "AR5K_BEACON_RESET_TSF" : "");
  1763. }
  1764. /**
  1765. * ath5k_beacon_config - Configure the beacon queues and interrupts
  1766. *
  1767. * @sc: struct ath5k_softc pointer we are operating on
  1768. *
  1769. * In IBSS mode we use a self-linked tx descriptor if possible. We enable SWBA
  1770. * interrupts to detect TSF updates only.
  1771. */
  1772. static void
  1773. ath5k_beacon_config(struct ath5k_softc *sc)
  1774. {
  1775. struct ath5k_hw *ah = sc->ah;
  1776. unsigned long flags;
  1777. spin_lock_irqsave(&sc->block, flags);
  1778. sc->bmisscount = 0;
  1779. sc->imask &= ~(AR5K_INT_BMISS | AR5K_INT_SWBA);
  1780. if (sc->enable_beacon) {
  1781. /*
  1782. * In IBSS mode we use a self-linked tx descriptor and let the
  1783. * hardware send the beacons automatically. We have to load it
  1784. * only once here.
  1785. * We use the SWBA interrupt only to keep track of the beacon
  1786. * timers in order to detect automatic TSF updates.
  1787. */
  1788. ath5k_beaconq_config(sc);
  1789. sc->imask |= AR5K_INT_SWBA;
  1790. if (sc->opmode == NL80211_IFTYPE_ADHOC) {
  1791. if (ath5k_hw_hasveol(ah))
  1792. ath5k_beacon_send(sc);
  1793. } else
  1794. ath5k_beacon_update_timers(sc, -1);
  1795. } else {
  1796. ath5k_hw_stop_beacon_queue(sc->ah, sc->bhalq);
  1797. }
  1798. ath5k_hw_set_imr(ah, sc->imask);
  1799. mmiowb();
  1800. spin_unlock_irqrestore(&sc->block, flags);
  1801. }
  1802. static void ath5k_tasklet_beacon(unsigned long data)
  1803. {
  1804. struct ath5k_softc *sc = (struct ath5k_softc *) data;
  1805. /*
  1806. * Software beacon alert--time to send a beacon.
  1807. *
  1808. * In IBSS mode we use this interrupt just to
  1809. * keep track of the next TBTT (target beacon
  1810. * transmission time) in order to detect wether
  1811. * automatic TSF updates happened.
  1812. */
  1813. if (sc->opmode == NL80211_IFTYPE_ADHOC) {
  1814. /* XXX: only if VEOL suppported */
  1815. u64 tsf = ath5k_hw_get_tsf64(sc->ah);
  1816. sc->nexttbtt += sc->bintval;
  1817. ATH5K_DBG(sc, ATH5K_DEBUG_BEACON,
  1818. "SWBA nexttbtt: %x hw_tu: %x "
  1819. "TSF: %llx\n",
  1820. sc->nexttbtt,
  1821. TSF_TO_TU(tsf),
  1822. (unsigned long long) tsf);
  1823. } else {
  1824. spin_lock(&sc->block);
  1825. ath5k_beacon_send(sc);
  1826. spin_unlock(&sc->block);
  1827. }
  1828. }
  1829. /********************\
  1830. * Interrupt handling *
  1831. \********************/
  1832. static void
  1833. ath5k_intr_calibration_poll(struct ath5k_hw *ah)
  1834. {
  1835. if (time_is_before_eq_jiffies(ah->ah_cal_next_ani) &&
  1836. !(ah->ah_cal_mask & AR5K_CALIBRATION_FULL)) {
  1837. /* run ANI only when full calibration is not active */
  1838. ah->ah_cal_next_ani = jiffies +
  1839. msecs_to_jiffies(ATH5K_TUNE_CALIBRATION_INTERVAL_ANI);
  1840. tasklet_schedule(&ah->ah_sc->ani_tasklet);
  1841. } else if (time_is_before_eq_jiffies(ah->ah_cal_next_full)) {
  1842. ah->ah_cal_next_full = jiffies +
  1843. msecs_to_jiffies(ATH5K_TUNE_CALIBRATION_INTERVAL_FULL);
  1844. tasklet_schedule(&ah->ah_sc->calib);
  1845. }
  1846. /* we could use SWI to generate enough interrupts to meet our
  1847. * calibration interval requirements, if necessary:
  1848. * AR5K_REG_ENABLE_BITS(ah, AR5K_CR, AR5K_CR_SWI); */
  1849. }
  1850. irqreturn_t
  1851. ath5k_intr(int irq, void *dev_id)
  1852. {
  1853. struct ath5k_softc *sc = dev_id;
  1854. struct ath5k_hw *ah = sc->ah;
  1855. enum ath5k_int status;
  1856. unsigned int counter = 1000;
  1857. if (unlikely(test_bit(ATH_STAT_INVALID, sc->status) ||
  1858. ((ath5k_get_bus_type(ah) != ATH_AHB) &&
  1859. !ath5k_hw_is_intr_pending(ah))))
  1860. return IRQ_NONE;
  1861. do {
  1862. ath5k_hw_get_isr(ah, &status); /* NB: clears IRQ too */
  1863. ATH5K_DBG(sc, ATH5K_DEBUG_INTR, "status 0x%x/0x%x\n",
  1864. status, sc->imask);
  1865. if (unlikely(status & AR5K_INT_FATAL)) {
  1866. /*
  1867. * Fatal errors are unrecoverable.
  1868. * Typically these are caused by DMA errors.
  1869. */
  1870. ATH5K_DBG(sc, ATH5K_DEBUG_RESET,
  1871. "fatal int, resetting\n");
  1872. ieee80211_queue_work(sc->hw, &sc->reset_work);
  1873. } else if (unlikely(status & AR5K_INT_RXORN)) {
  1874. /*
  1875. * Receive buffers are full. Either the bus is busy or
  1876. * the CPU is not fast enough to process all received
  1877. * frames.
  1878. * Older chipsets need a reset to come out of this
  1879. * condition, but we treat it as RX for newer chips.
  1880. * We don't know exactly which versions need a reset -
  1881. * this guess is copied from the HAL.
  1882. */
  1883. sc->stats.rxorn_intr++;
  1884. if (ah->ah_mac_srev < AR5K_SREV_AR5212) {
  1885. ATH5K_DBG(sc, ATH5K_DEBUG_RESET,
  1886. "rx overrun, resetting\n");
  1887. ieee80211_queue_work(sc->hw, &sc->reset_work);
  1888. }
  1889. else
  1890. tasklet_schedule(&sc->rxtq);
  1891. } else {
  1892. if (status & AR5K_INT_SWBA) {
  1893. tasklet_hi_schedule(&sc->beacontq);
  1894. }
  1895. if (status & AR5K_INT_RXEOL) {
  1896. /*
  1897. * NB: the hardware should re-read the link when
  1898. * RXE bit is written, but it doesn't work at
  1899. * least on older hardware revs.
  1900. */
  1901. sc->stats.rxeol_intr++;
  1902. }
  1903. if (status & AR5K_INT_TXURN) {
  1904. /* bump tx trigger level */
  1905. ath5k_hw_update_tx_triglevel(ah, true);
  1906. }
  1907. if (status & (AR5K_INT_RXOK | AR5K_INT_RXERR))
  1908. tasklet_schedule(&sc->rxtq);
  1909. if (status & (AR5K_INT_TXOK | AR5K_INT_TXDESC
  1910. | AR5K_INT_TXERR | AR5K_INT_TXEOL))
  1911. tasklet_schedule(&sc->txtq);
  1912. if (status & AR5K_INT_BMISS) {
  1913. /* TODO */
  1914. }
  1915. if (status & AR5K_INT_MIB) {
  1916. sc->stats.mib_intr++;
  1917. ath5k_hw_update_mib_counters(ah);
  1918. ath5k_ani_mib_intr(ah);
  1919. }
  1920. if (status & AR5K_INT_GPIO)
  1921. tasklet_schedule(&sc->rf_kill.toggleq);
  1922. }
  1923. if (ath5k_get_bus_type(ah) == ATH_AHB)
  1924. break;
  1925. } while (ath5k_hw_is_intr_pending(ah) && --counter > 0);
  1926. if (unlikely(!counter))
  1927. ATH5K_WARN(sc, "too many interrupts, giving up for now\n");
  1928. ath5k_intr_calibration_poll(ah);
  1929. return IRQ_HANDLED;
  1930. }
  1931. /*
  1932. * Periodically recalibrate the PHY to account
  1933. * for temperature/environment changes.
  1934. */
  1935. static void
  1936. ath5k_tasklet_calibrate(unsigned long data)
  1937. {
  1938. struct ath5k_softc *sc = (void *)data;
  1939. struct ath5k_hw *ah = sc->ah;
  1940. /* Only full calibration for now */
  1941. ah->ah_cal_mask |= AR5K_CALIBRATION_FULL;
  1942. ATH5K_DBG(sc, ATH5K_DEBUG_CALIBRATE, "channel %u/%x\n",
  1943. ieee80211_frequency_to_channel(sc->curchan->center_freq),
  1944. sc->curchan->hw_value);
  1945. if (ath5k_hw_gainf_calibrate(ah) == AR5K_RFGAIN_NEED_CHANGE) {
  1946. /*
  1947. * Rfgain is out of bounds, reset the chip
  1948. * to load new gain values.
  1949. */
  1950. ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "calibration, resetting\n");
  1951. ieee80211_queue_work(sc->hw, &sc->reset_work);
  1952. }
  1953. if (ath5k_hw_phy_calibrate(ah, sc->curchan))
  1954. ATH5K_ERR(sc, "calibration of channel %u failed\n",
  1955. ieee80211_frequency_to_channel(
  1956. sc->curchan->center_freq));
  1957. /* Noise floor calibration interrupts rx/tx path while I/Q calibration
  1958. * doesn't.
  1959. * TODO: We should stop TX here, so that it doesn't interfere.
  1960. * Note that stopping the queues is not enough to stop TX! */
  1961. if (time_is_before_eq_jiffies(ah->ah_cal_next_nf)) {
  1962. ah->ah_cal_next_nf = jiffies +
  1963. msecs_to_jiffies(ATH5K_TUNE_CALIBRATION_INTERVAL_NF);
  1964. ath5k_hw_update_noise_floor(ah);
  1965. }
  1966. ah->ah_cal_mask &= ~AR5K_CALIBRATION_FULL;
  1967. }
  1968. static void
  1969. ath5k_tasklet_ani(unsigned long data)
  1970. {
  1971. struct ath5k_softc *sc = (void *)data;
  1972. struct ath5k_hw *ah = sc->ah;
  1973. ah->ah_cal_mask |= AR5K_CALIBRATION_ANI;
  1974. ath5k_ani_calibration(ah);
  1975. ah->ah_cal_mask &= ~AR5K_CALIBRATION_ANI;
  1976. }
  1977. static void
  1978. ath5k_tx_complete_poll_work(struct work_struct *work)
  1979. {
  1980. struct ath5k_softc *sc = container_of(work, struct ath5k_softc,
  1981. tx_complete_work.work);
  1982. struct ath5k_txq *txq;
  1983. int i;
  1984. bool needreset = false;
  1985. for (i = 0; i < ARRAY_SIZE(sc->txqs); i++) {
  1986. if (sc->txqs[i].setup) {
  1987. txq = &sc->txqs[i];
  1988. spin_lock_bh(&txq->lock);
  1989. if (txq->txq_len > 1) {
  1990. if (txq->txq_poll_mark) {
  1991. ATH5K_DBG(sc, ATH5K_DEBUG_XMIT,
  1992. "TX queue stuck %d\n",
  1993. txq->qnum);
  1994. needreset = true;
  1995. txq->txq_stuck++;
  1996. spin_unlock_bh(&txq->lock);
  1997. break;
  1998. } else {
  1999. txq->txq_poll_mark = true;
  2000. }
  2001. }
  2002. spin_unlock_bh(&txq->lock);
  2003. }
  2004. }
  2005. if (needreset) {
  2006. ATH5K_DBG(sc, ATH5K_DEBUG_RESET,
  2007. "TX queues stuck, resetting\n");
  2008. ath5k_reset(sc, NULL, true);
  2009. }
  2010. ieee80211_queue_delayed_work(sc->hw, &sc->tx_complete_work,
  2011. msecs_to_jiffies(ATH5K_TX_COMPLETE_POLL_INT));
  2012. }
  2013. /*************************\
  2014. * Initialization routines *
  2015. \*************************/
  2016. int
  2017. ath5k_init_softc(struct ath5k_softc *sc, const struct ath_bus_ops *bus_ops)
  2018. {
  2019. struct ieee80211_hw *hw = sc->hw;
  2020. struct ath_common *common;
  2021. int ret;
  2022. int csz;
  2023. /* Initialize driver private data */
  2024. SET_IEEE80211_DEV(hw, sc->dev);
  2025. hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
  2026. IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
  2027. IEEE80211_HW_SIGNAL_DBM |
  2028. IEEE80211_HW_REPORTS_TX_ACK_STATUS;
  2029. hw->wiphy->interface_modes =
  2030. BIT(NL80211_IFTYPE_AP) |
  2031. BIT(NL80211_IFTYPE_STATION) |
  2032. BIT(NL80211_IFTYPE_ADHOC) |
  2033. BIT(NL80211_IFTYPE_MESH_POINT);
  2034. hw->extra_tx_headroom = 2;
  2035. hw->channel_change_time = 5000;
  2036. /*
  2037. * Mark the device as detached to avoid processing
  2038. * interrupts until setup is complete.
  2039. */
  2040. __set_bit(ATH_STAT_INVALID, sc->status);
  2041. sc->opmode = NL80211_IFTYPE_STATION;
  2042. sc->bintval = 1000;
  2043. mutex_init(&sc->lock);
  2044. spin_lock_init(&sc->rxbuflock);
  2045. spin_lock_init(&sc->txbuflock);
  2046. spin_lock_init(&sc->block);
  2047. /* Setup interrupt handler */
  2048. ret = request_irq(sc->irq, ath5k_intr, IRQF_SHARED, "ath", sc);
  2049. if (ret) {
  2050. ATH5K_ERR(sc, "request_irq failed\n");
  2051. goto err;
  2052. }
  2053. /* If we passed the test, malloc an ath5k_hw struct */
  2054. sc->ah = kzalloc(sizeof(struct ath5k_hw), GFP_KERNEL);
  2055. if (!sc->ah) {
  2056. ret = -ENOMEM;
  2057. ATH5K_ERR(sc, "out of memory\n");
  2058. goto err_irq;
  2059. }
  2060. sc->ah->ah_sc = sc;
  2061. sc->ah->ah_iobase = sc->iobase;
  2062. common = ath5k_hw_common(sc->ah);
  2063. common->ops = &ath5k_common_ops;
  2064. common->bus_ops = bus_ops;
  2065. common->ah = sc->ah;
  2066. common->hw = hw;
  2067. common->priv = sc;
  2068. /*
  2069. * Cache line size is used to size and align various
  2070. * structures used to communicate with the hardware.
  2071. */
  2072. ath5k_read_cachesize(common, &csz);
  2073. common->cachelsz = csz << 2; /* convert to bytes */
  2074. spin_lock_init(&common->cc_lock);
  2075. /* Initialize device */
  2076. ret = ath5k_hw_init(sc);
  2077. if (ret)
  2078. goto err_free_ah;
  2079. /* set up multi-rate retry capabilities */
  2080. if (sc->ah->ah_version == AR5K_AR5212) {
  2081. hw->max_rates = 4;
  2082. hw->max_rate_tries = 11;
  2083. }
  2084. hw->vif_data_size = sizeof(struct ath5k_vif);
  2085. /* Finish private driver data initialization */
  2086. ret = ath5k_init(hw);
  2087. if (ret)
  2088. goto err_ah;
  2089. ATH5K_INFO(sc, "Atheros AR%s chip found (MAC: 0x%x, PHY: 0x%x)\n",
  2090. ath5k_chip_name(AR5K_VERSION_MAC, sc->ah->ah_mac_srev),
  2091. sc->ah->ah_mac_srev,
  2092. sc->ah->ah_phy_revision);
  2093. if (!sc->ah->ah_single_chip) {
  2094. /* Single chip radio (!RF5111) */
  2095. if (sc->ah->ah_radio_5ghz_revision &&
  2096. !sc->ah->ah_radio_2ghz_revision) {
  2097. /* No 5GHz support -> report 2GHz radio */
  2098. if (!test_bit(AR5K_MODE_11A,
  2099. sc->ah->ah_capabilities.cap_mode)) {
  2100. ATH5K_INFO(sc, "RF%s 2GHz radio found (0x%x)\n",
  2101. ath5k_chip_name(AR5K_VERSION_RAD,
  2102. sc->ah->ah_radio_5ghz_revision),
  2103. sc->ah->ah_radio_5ghz_revision);
  2104. /* No 2GHz support (5110 and some
  2105. * 5Ghz only cards) -> report 5Ghz radio */
  2106. } else if (!test_bit(AR5K_MODE_11B,
  2107. sc->ah->ah_capabilities.cap_mode)) {
  2108. ATH5K_INFO(sc, "RF%s 5GHz radio found (0x%x)\n",
  2109. ath5k_chip_name(AR5K_VERSION_RAD,
  2110. sc->ah->ah_radio_5ghz_revision),
  2111. sc->ah->ah_radio_5ghz_revision);
  2112. /* Multiband radio */
  2113. } else {
  2114. ATH5K_INFO(sc, "RF%s multiband radio found"
  2115. " (0x%x)\n",
  2116. ath5k_chip_name(AR5K_VERSION_RAD,
  2117. sc->ah->ah_radio_5ghz_revision),
  2118. sc->ah->ah_radio_5ghz_revision);
  2119. }
  2120. }
  2121. /* Multi chip radio (RF5111 - RF2111) ->
  2122. * report both 2GHz/5GHz radios */
  2123. else if (sc->ah->ah_radio_5ghz_revision &&
  2124. sc->ah->ah_radio_2ghz_revision){
  2125. ATH5K_INFO(sc, "RF%s 5GHz radio found (0x%x)\n",
  2126. ath5k_chip_name(AR5K_VERSION_RAD,
  2127. sc->ah->ah_radio_5ghz_revision),
  2128. sc->ah->ah_radio_5ghz_revision);
  2129. ATH5K_INFO(sc, "RF%s 2GHz radio found (0x%x)\n",
  2130. ath5k_chip_name(AR5K_VERSION_RAD,
  2131. sc->ah->ah_radio_2ghz_revision),
  2132. sc->ah->ah_radio_2ghz_revision);
  2133. }
  2134. }
  2135. ath5k_debug_init_device(sc);
  2136. /* ready to process interrupts */
  2137. __clear_bit(ATH_STAT_INVALID, sc->status);
  2138. return 0;
  2139. err_ah:
  2140. ath5k_hw_deinit(sc->ah);
  2141. err_free_ah:
  2142. kfree(sc->ah);
  2143. err_irq:
  2144. free_irq(sc->irq, sc);
  2145. err:
  2146. return ret;
  2147. }
  2148. static int
  2149. ath5k_stop_locked(struct ath5k_softc *sc)
  2150. {
  2151. struct ath5k_hw *ah = sc->ah;
  2152. ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "invalid %u\n",
  2153. test_bit(ATH_STAT_INVALID, sc->status));
  2154. /*
  2155. * Shutdown the hardware and driver:
  2156. * stop output from above
  2157. * disable interrupts
  2158. * turn off timers
  2159. * turn off the radio
  2160. * clear transmit machinery
  2161. * clear receive machinery
  2162. * drain and release tx queues
  2163. * reclaim beacon resources
  2164. * power down hardware
  2165. *
  2166. * Note that some of this work is not possible if the
  2167. * hardware is gone (invalid).
  2168. */
  2169. ieee80211_stop_queues(sc->hw);
  2170. if (!test_bit(ATH_STAT_INVALID, sc->status)) {
  2171. ath5k_led_off(sc);
  2172. ath5k_hw_set_imr(ah, 0);
  2173. synchronize_irq(sc->irq);
  2174. ath5k_rx_stop(sc);
  2175. ath5k_hw_dma_stop(ah);
  2176. ath5k_drain_tx_buffs(sc);
  2177. ath5k_hw_phy_disable(ah);
  2178. }
  2179. return 0;
  2180. }
  2181. static int
  2182. ath5k_init_hw(struct ath5k_softc *sc)
  2183. {
  2184. struct ath5k_hw *ah = sc->ah;
  2185. struct ath_common *common = ath5k_hw_common(ah);
  2186. int ret, i;
  2187. mutex_lock(&sc->lock);
  2188. ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "mode %d\n", sc->opmode);
  2189. /*
  2190. * Stop anything previously setup. This is safe
  2191. * no matter this is the first time through or not.
  2192. */
  2193. ath5k_stop_locked(sc);
  2194. /*
  2195. * The basic interface to setting the hardware in a good
  2196. * state is ``reset''. On return the hardware is known to
  2197. * be powered up and with interrupts disabled. This must
  2198. * be followed by initialization of the appropriate bits
  2199. * and then setup of the interrupt mask.
  2200. */
  2201. sc->curchan = sc->hw->conf.channel;
  2202. sc->curband = &sc->sbands[sc->curchan->band];
  2203. sc->imask = AR5K_INT_RXOK | AR5K_INT_RXERR | AR5K_INT_RXEOL |
  2204. AR5K_INT_RXORN | AR5K_INT_TXDESC | AR5K_INT_TXEOL |
  2205. AR5K_INT_FATAL | AR5K_INT_GLOBAL | AR5K_INT_MIB;
  2206. ret = ath5k_reset(sc, NULL, false);
  2207. if (ret)
  2208. goto done;
  2209. ath5k_rfkill_hw_start(ah);
  2210. /*
  2211. * Reset the key cache since some parts do not reset the
  2212. * contents on initial power up or resume from suspend.
  2213. */
  2214. for (i = 0; i < common->keymax; i++)
  2215. ath_hw_keyreset(common, (u16) i);
  2216. /* Use higher rates for acks instead of base
  2217. * rate */
  2218. ah->ah_ack_bitrate_high = true;
  2219. for (i = 0; i < ARRAY_SIZE(sc->bslot); i++)
  2220. sc->bslot[i] = NULL;
  2221. ret = 0;
  2222. done:
  2223. mmiowb();
  2224. mutex_unlock(&sc->lock);
  2225. ieee80211_queue_delayed_work(sc->hw, &sc->tx_complete_work,
  2226. msecs_to_jiffies(ATH5K_TX_COMPLETE_POLL_INT));
  2227. return ret;
  2228. }
  2229. static void stop_tasklets(struct ath5k_softc *sc)
  2230. {
  2231. tasklet_kill(&sc->rxtq);
  2232. tasklet_kill(&sc->txtq);
  2233. tasklet_kill(&sc->calib);
  2234. tasklet_kill(&sc->beacontq);
  2235. tasklet_kill(&sc->ani_tasklet);
  2236. }
  2237. /*
  2238. * Stop the device, grabbing the top-level lock to protect
  2239. * against concurrent entry through ath5k_init (which can happen
  2240. * if another thread does a system call and the thread doing the
  2241. * stop is preempted).
  2242. */
  2243. static int
  2244. ath5k_stop_hw(struct ath5k_softc *sc)
  2245. {
  2246. int ret;
  2247. mutex_lock(&sc->lock);
  2248. ret = ath5k_stop_locked(sc);
  2249. if (ret == 0 && !test_bit(ATH_STAT_INVALID, sc->status)) {
  2250. /*
  2251. * Don't set the card in full sleep mode!
  2252. *
  2253. * a) When the device is in this state it must be carefully
  2254. * woken up or references to registers in the PCI clock
  2255. * domain may freeze the bus (and system). This varies
  2256. * by chip and is mostly an issue with newer parts
  2257. * (madwifi sources mentioned srev >= 0x78) that go to
  2258. * sleep more quickly.
  2259. *
  2260. * b) On older chips full sleep results a weird behaviour
  2261. * during wakeup. I tested various cards with srev < 0x78
  2262. * and they don't wake up after module reload, a second
  2263. * module reload is needed to bring the card up again.
  2264. *
  2265. * Until we figure out what's going on don't enable
  2266. * full chip reset on any chip (this is what Legacy HAL
  2267. * and Sam's HAL do anyway). Instead Perform a full reset
  2268. * on the device (same as initial state after attach) and
  2269. * leave it idle (keep MAC/BB on warm reset) */
  2270. ret = ath5k_hw_on_hold(sc->ah);
  2271. ATH5K_DBG(sc, ATH5K_DEBUG_RESET,
  2272. "putting device to sleep\n");
  2273. }
  2274. mmiowb();
  2275. mutex_unlock(&sc->lock);
  2276. stop_tasklets(sc);
  2277. cancel_delayed_work_sync(&sc->tx_complete_work);
  2278. ath5k_rfkill_hw_stop(sc->ah);
  2279. return ret;
  2280. }
  2281. /*
  2282. * Reset the hardware. If chan is not NULL, then also pause rx/tx
  2283. * and change to the given channel.
  2284. *
  2285. * This should be called with sc->lock.
  2286. */
  2287. static int
  2288. ath5k_reset(struct ath5k_softc *sc, struct ieee80211_channel *chan,
  2289. bool skip_pcu)
  2290. {
  2291. struct ath5k_hw *ah = sc->ah;
  2292. int ret, ani_mode;
  2293. ATH5K_DBG(sc, ATH5K_DEBUG_RESET, "resetting\n");
  2294. ath5k_hw_set_imr(ah, 0);
  2295. synchronize_irq(sc->irq);
  2296. stop_tasklets(sc);
  2297. /* Save ani mode and disable ANI durring
  2298. * reset. If we don't we might get false
  2299. * PHY error interrupts. */
  2300. ani_mode = ah->ah_sc->ani_state.ani_mode;
  2301. ath5k_ani_init(ah, ATH5K_ANI_MODE_OFF);
  2302. /* We are going to empty hw queues
  2303. * so we should also free any remaining
  2304. * tx buffers */
  2305. ath5k_drain_tx_buffs(sc);
  2306. if (chan) {
  2307. sc->curchan = chan;
  2308. sc->curband = &sc->sbands[chan->band];
  2309. }
  2310. ret = ath5k_hw_reset(ah, sc->opmode, sc->curchan, chan != NULL,
  2311. skip_pcu);
  2312. if (ret) {
  2313. ATH5K_ERR(sc, "can't reset hardware (%d)\n", ret);
  2314. goto err;
  2315. }
  2316. ret = ath5k_rx_start(sc);
  2317. if (ret) {
  2318. ATH5K_ERR(sc, "can't start recv logic\n");
  2319. goto err;
  2320. }
  2321. ath5k_ani_init(ah, ani_mode);
  2322. ah->ah_cal_next_full = jiffies;
  2323. ah->ah_cal_next_ani = jiffies;
  2324. ah->ah_cal_next_nf = jiffies;
  2325. ewma_init(&ah->ah_beacon_rssi_avg, 1024, 8);
  2326. /*
  2327. * Change channels and update the h/w rate map if we're switching;
  2328. * e.g. 11a to 11b/g.
  2329. *
  2330. * We may be doing a reset in response to an ioctl that changes the
  2331. * channel so update any state that might change as a result.
  2332. *
  2333. * XXX needed?
  2334. */
  2335. /* ath5k_chan_change(sc, c); */
  2336. ath5k_beacon_config(sc);
  2337. /* intrs are enabled by ath5k_beacon_config */
  2338. ieee80211_wake_queues(sc->hw);
  2339. return 0;
  2340. err:
  2341. return ret;
  2342. }
  2343. static void ath5k_reset_work(struct work_struct *work)
  2344. {
  2345. struct ath5k_softc *sc = container_of(work, struct ath5k_softc,
  2346. reset_work);
  2347. mutex_lock(&sc->lock);
  2348. ath5k_reset(sc, NULL, true);
  2349. mutex_unlock(&sc->lock);
  2350. }
  2351. static int
  2352. ath5k_init(struct ieee80211_hw *hw)
  2353. {
  2354. struct ath5k_softc *sc = hw->priv;
  2355. struct ath5k_hw *ah = sc->ah;
  2356. struct ath_regulatory *regulatory = ath5k_hw_regulatory(ah);
  2357. struct ath5k_txq *txq;
  2358. u8 mac[ETH_ALEN] = {};
  2359. int ret;
  2360. /*
  2361. * Check if the MAC has multi-rate retry support.
  2362. * We do this by trying to setup a fake extended
  2363. * descriptor. MACs that don't have support will
  2364. * return false w/o doing anything. MACs that do
  2365. * support it will return true w/o doing anything.
  2366. */
  2367. ret = ath5k_hw_setup_mrr_tx_desc(ah, NULL, 0, 0, 0, 0, 0, 0);
  2368. if (ret < 0)
  2369. goto err;
  2370. if (ret > 0)
  2371. __set_bit(ATH_STAT_MRRETRY, sc->status);
  2372. /*
  2373. * Collect the channel list. The 802.11 layer
  2374. * is resposible for filtering this list based
  2375. * on settings like the phy mode and regulatory
  2376. * domain restrictions.
  2377. */
  2378. ret = ath5k_setup_bands(hw);
  2379. if (ret) {
  2380. ATH5K_ERR(sc, "can't get channels\n");
  2381. goto err;
  2382. }
  2383. /* NB: setup here so ath5k_rate_update is happy */
  2384. if (test_bit(AR5K_MODE_11A, ah->ah_modes))
  2385. ath5k_setcurmode(sc, AR5K_MODE_11A);
  2386. else
  2387. ath5k_setcurmode(sc, AR5K_MODE_11B);
  2388. /*
  2389. * Allocate tx+rx descriptors and populate the lists.
  2390. */
  2391. ret = ath5k_desc_alloc(sc);
  2392. if (ret) {
  2393. ATH5K_ERR(sc, "can't allocate descriptors\n");
  2394. goto err;
  2395. }
  2396. /*
  2397. * Allocate hardware transmit queues: one queue for
  2398. * beacon frames and one data queue for each QoS
  2399. * priority. Note that hw functions handle resetting
  2400. * these queues at the needed time.
  2401. */
  2402. ret = ath5k_beaconq_setup(ah);
  2403. if (ret < 0) {
  2404. ATH5K_ERR(sc, "can't setup a beacon xmit queue\n");
  2405. goto err_desc;
  2406. }
  2407. sc->bhalq = ret;
  2408. sc->cabq = ath5k_txq_setup(sc, AR5K_TX_QUEUE_CAB, 0);
  2409. if (IS_ERR(sc->cabq)) {
  2410. ATH5K_ERR(sc, "can't setup cab queue\n");
  2411. ret = PTR_ERR(sc->cabq);
  2412. goto err_bhal;
  2413. }
  2414. /* 5211 and 5212 usually support 10 queues but we better rely on the
  2415. * capability information */
  2416. if (ah->ah_capabilities.cap_queues.q_tx_num >= 6) {
  2417. /* This order matches mac80211's queue priority, so we can
  2418. * directly use the mac80211 queue number without any mapping */
  2419. txq = ath5k_txq_setup(sc, AR5K_TX_QUEUE_DATA, AR5K_WME_AC_VO);
  2420. if (IS_ERR(txq)) {
  2421. ATH5K_ERR(sc, "can't setup xmit queue\n");
  2422. ret = PTR_ERR(txq);
  2423. goto err_queues;
  2424. }
  2425. txq = ath5k_txq_setup(sc, AR5K_TX_QUEUE_DATA, AR5K_WME_AC_VI);
  2426. if (IS_ERR(txq)) {
  2427. ATH5K_ERR(sc, "can't setup xmit queue\n");
  2428. ret = PTR_ERR(txq);
  2429. goto err_queues;
  2430. }
  2431. txq = ath5k_txq_setup(sc, AR5K_TX_QUEUE_DATA, AR5K_WME_AC_BE);
  2432. if (IS_ERR(txq)) {
  2433. ATH5K_ERR(sc, "can't setup xmit queue\n");
  2434. ret = PTR_ERR(txq);
  2435. goto err_queues;
  2436. }
  2437. txq = ath5k_txq_setup(sc, AR5K_TX_QUEUE_DATA, AR5K_WME_AC_BK);
  2438. if (IS_ERR(txq)) {
  2439. ATH5K_ERR(sc, "can't setup xmit queue\n");
  2440. ret = PTR_ERR(txq);
  2441. goto err_queues;
  2442. }
  2443. hw->queues = 4;
  2444. } else {
  2445. /* older hardware (5210) can only support one data queue */
  2446. txq = ath5k_txq_setup(sc, AR5K_TX_QUEUE_DATA, AR5K_WME_AC_BE);
  2447. if (IS_ERR(txq)) {
  2448. ATH5K_ERR(sc, "can't setup xmit queue\n");
  2449. ret = PTR_ERR(txq);
  2450. goto err_queues;
  2451. }
  2452. hw->queues = 1;
  2453. }
  2454. tasklet_init(&sc->rxtq, ath5k_tasklet_rx, (unsigned long)sc);
  2455. tasklet_init(&sc->txtq, ath5k_tasklet_tx, (unsigned long)sc);
  2456. tasklet_init(&sc->calib, ath5k_tasklet_calibrate, (unsigned long)sc);
  2457. tasklet_init(&sc->beacontq, ath5k_tasklet_beacon, (unsigned long)sc);
  2458. tasklet_init(&sc->ani_tasklet, ath5k_tasklet_ani, (unsigned long)sc);
  2459. INIT_WORK(&sc->reset_work, ath5k_reset_work);
  2460. INIT_DELAYED_WORK(&sc->tx_complete_work, ath5k_tx_complete_poll_work);
  2461. ret = ath5k_eeprom_read_mac(ah, mac);
  2462. if (ret) {
  2463. ATH5K_ERR(sc, "unable to read address from EEPROM\n");
  2464. goto err_queues;
  2465. }
  2466. SET_IEEE80211_PERM_ADDR(hw, mac);
  2467. memcpy(&sc->lladdr, mac, ETH_ALEN);
  2468. /* All MAC address bits matter for ACKs */
  2469. ath5k_update_bssid_mask_and_opmode(sc, NULL);
  2470. regulatory->current_rd = ah->ah_capabilities.cap_eeprom.ee_regdomain;
  2471. ret = ath_regd_init(regulatory, hw->wiphy, ath5k_reg_notifier);
  2472. if (ret) {
  2473. ATH5K_ERR(sc, "can't initialize regulatory system\n");
  2474. goto err_queues;
  2475. }
  2476. ret = ieee80211_register_hw(hw);
  2477. if (ret) {
  2478. ATH5K_ERR(sc, "can't register ieee80211 hw\n");
  2479. goto err_queues;
  2480. }
  2481. if (!ath_is_world_regd(regulatory))
  2482. regulatory_hint(hw->wiphy, regulatory->alpha2);
  2483. ath5k_init_leds(sc);
  2484. ath5k_sysfs_register(sc);
  2485. return 0;
  2486. err_queues:
  2487. ath5k_txq_release(sc);
  2488. err_bhal:
  2489. ath5k_hw_release_tx_queue(ah, sc->bhalq);
  2490. err_desc:
  2491. ath5k_desc_free(sc);
  2492. err:
  2493. return ret;
  2494. }
  2495. void
  2496. ath5k_deinit_softc(struct ath5k_softc *sc)
  2497. {
  2498. struct ieee80211_hw *hw = sc->hw;
  2499. /*
  2500. * NB: the order of these is important:
  2501. * o call the 802.11 layer before detaching ath5k_hw to
  2502. * ensure callbacks into the driver to delete global
  2503. * key cache entries can be handled
  2504. * o reclaim the tx queue data structures after calling
  2505. * the 802.11 layer as we'll get called back to reclaim
  2506. * node state and potentially want to use them
  2507. * o to cleanup the tx queues the hal is called, so detach
  2508. * it last
  2509. * XXX: ??? detach ath5k_hw ???
  2510. * Other than that, it's straightforward...
  2511. */
  2512. ath5k_debug_finish_device(sc);
  2513. ieee80211_unregister_hw(hw);
  2514. ath5k_desc_free(sc);
  2515. ath5k_txq_release(sc);
  2516. ath5k_hw_release_tx_queue(sc->ah, sc->bhalq);
  2517. ath5k_unregister_leds(sc);
  2518. ath5k_sysfs_unregister(sc);
  2519. /*
  2520. * NB: can't reclaim these until after ieee80211_ifdetach
  2521. * returns because we'll get called back to reclaim node
  2522. * state and potentially want to use them.
  2523. */
  2524. ath5k_hw_deinit(sc->ah);
  2525. free_irq(sc->irq, sc);
  2526. }
  2527. /********************\
  2528. * Mac80211 functions *
  2529. \********************/
  2530. static int
  2531. ath5k_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
  2532. {
  2533. struct ath5k_softc *sc = hw->priv;
  2534. u16 qnum = skb_get_queue_mapping(skb);
  2535. if (WARN_ON(qnum >= sc->ah->ah_capabilities.cap_queues.q_tx_num)) {
  2536. dev_kfree_skb_any(skb);
  2537. return 0;
  2538. }
  2539. return ath5k_tx_queue(hw, skb, &sc->txqs[qnum]);
  2540. }
  2541. static int ath5k_start(struct ieee80211_hw *hw)
  2542. {
  2543. return ath5k_init_hw(hw->priv);
  2544. }
  2545. static void ath5k_stop(struct ieee80211_hw *hw)
  2546. {
  2547. ath5k_stop_hw(hw->priv);
  2548. }
  2549. static int ath5k_add_interface(struct ieee80211_hw *hw,
  2550. struct ieee80211_vif *vif)
  2551. {
  2552. struct ath5k_softc *sc = hw->priv;
  2553. int ret;
  2554. struct ath5k_vif *avf = (void *)vif->drv_priv;
  2555. mutex_lock(&sc->lock);
  2556. if ((vif->type == NL80211_IFTYPE_AP ||
  2557. vif->type == NL80211_IFTYPE_ADHOC)
  2558. && (sc->num_ap_vifs + sc->num_adhoc_vifs) >= ATH_BCBUF) {
  2559. ret = -ELNRNG;
  2560. goto end;
  2561. }
  2562. /* Don't allow other interfaces if one ad-hoc is configured.
  2563. * TODO: Fix the problems with ad-hoc and multiple other interfaces.
  2564. * We would need to operate the HW in ad-hoc mode to allow TSF updates
  2565. * for the IBSS, but this breaks with additional AP or STA interfaces
  2566. * at the moment. */
  2567. if (sc->num_adhoc_vifs ||
  2568. (sc->nvifs && vif->type == NL80211_IFTYPE_ADHOC)) {
  2569. ATH5K_ERR(sc, "Only one single ad-hoc interface is allowed.\n");
  2570. ret = -ELNRNG;
  2571. goto end;
  2572. }
  2573. switch (vif->type) {
  2574. case NL80211_IFTYPE_AP:
  2575. case NL80211_IFTYPE_STATION:
  2576. case NL80211_IFTYPE_ADHOC:
  2577. case NL80211_IFTYPE_MESH_POINT:
  2578. avf->opmode = vif->type;
  2579. break;
  2580. default:
  2581. ret = -EOPNOTSUPP;
  2582. goto end;
  2583. }
  2584. sc->nvifs++;
  2585. ATH5K_DBG(sc, ATH5K_DEBUG_MODE, "add interface mode %d\n", avf->opmode);
  2586. /* Assign the vap/adhoc to a beacon xmit slot. */
  2587. if ((avf->opmode == NL80211_IFTYPE_AP) ||
  2588. (avf->opmode == NL80211_IFTYPE_ADHOC) ||
  2589. (avf->opmode == NL80211_IFTYPE_MESH_POINT)) {
  2590. int slot;
  2591. WARN_ON(list_empty(&sc->bcbuf));
  2592. avf->bbuf = list_first_entry(&sc->bcbuf, struct ath5k_buf,
  2593. list);
  2594. list_del(&avf->bbuf->list);
  2595. avf->bslot = 0;
  2596. for (slot = 0; slot < ATH_BCBUF; slot++) {
  2597. if (!sc->bslot[slot]) {
  2598. avf->bslot = slot;
  2599. break;
  2600. }
  2601. }
  2602. BUG_ON(sc->bslot[avf->bslot] != NULL);
  2603. sc->bslot[avf->bslot] = vif;
  2604. if (avf->opmode == NL80211_IFTYPE_AP)
  2605. sc->num_ap_vifs++;
  2606. else if (avf->opmode == NL80211_IFTYPE_ADHOC)
  2607. sc->num_adhoc_vifs++;
  2608. }
  2609. /* Any MAC address is fine, all others are included through the
  2610. * filter.
  2611. */
  2612. memcpy(&sc->lladdr, vif->addr, ETH_ALEN);
  2613. ath5k_hw_set_lladdr(sc->ah, vif->addr);
  2614. memcpy(&avf->lladdr, vif->addr, ETH_ALEN);
  2615. ath5k_mode_setup(sc, vif);
  2616. ret = 0;
  2617. end:
  2618. mutex_unlock(&sc->lock);
  2619. return ret;
  2620. }
  2621. static void
  2622. ath5k_remove_interface(struct ieee80211_hw *hw,
  2623. struct ieee80211_vif *vif)
  2624. {
  2625. struct ath5k_softc *sc = hw->priv;
  2626. struct ath5k_vif *avf = (void *)vif->drv_priv;
  2627. unsigned int i;
  2628. mutex_lock(&sc->lock);
  2629. sc->nvifs--;
  2630. if (avf->bbuf) {
  2631. ath5k_txbuf_free_skb(sc, avf->bbuf);
  2632. list_add_tail(&avf->bbuf->list, &sc->bcbuf);
  2633. for (i = 0; i < ATH_BCBUF; i++) {
  2634. if (sc->bslot[i] == vif) {
  2635. sc->bslot[i] = NULL;
  2636. break;
  2637. }
  2638. }
  2639. avf->bbuf = NULL;
  2640. }
  2641. if (avf->opmode == NL80211_IFTYPE_AP)
  2642. sc->num_ap_vifs--;
  2643. else if (avf->opmode == NL80211_IFTYPE_ADHOC)
  2644. sc->num_adhoc_vifs--;
  2645. ath5k_update_bssid_mask_and_opmode(sc, NULL);
  2646. mutex_unlock(&sc->lock);
  2647. }
  2648. /*
  2649. * TODO: Phy disable/diversity etc
  2650. */
  2651. static int
  2652. ath5k_config(struct ieee80211_hw *hw, u32 changed)
  2653. {
  2654. struct ath5k_softc *sc = hw->priv;
  2655. struct ath5k_hw *ah = sc->ah;
  2656. struct ieee80211_conf *conf = &hw->conf;
  2657. int ret = 0;
  2658. mutex_lock(&sc->lock);
  2659. if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
  2660. ret = ath5k_chan_set(sc, conf->channel);
  2661. if (ret < 0)
  2662. goto unlock;
  2663. }
  2664. if ((changed & IEEE80211_CONF_CHANGE_POWER) &&
  2665. (sc->power_level != conf->power_level)) {
  2666. sc->power_level = conf->power_level;
  2667. /* Half dB steps */
  2668. ath5k_hw_set_txpower_limit(ah, (conf->power_level * 2));
  2669. }
  2670. /* TODO:
  2671. * 1) Move this on config_interface and handle each case
  2672. * separately eg. when we have only one STA vif, use
  2673. * AR5K_ANTMODE_SINGLE_AP
  2674. *
  2675. * 2) Allow the user to change antenna mode eg. when only
  2676. * one antenna is present
  2677. *
  2678. * 3) Allow the user to set default/tx antenna when possible
  2679. *
  2680. * 4) Default mode should handle 90% of the cases, together
  2681. * with fixed a/b and single AP modes we should be able to
  2682. * handle 99%. Sectored modes are extreme cases and i still
  2683. * haven't found a usage for them. If we decide to support them,
  2684. * then we must allow the user to set how many tx antennas we
  2685. * have available
  2686. */
  2687. ath5k_hw_set_antenna_mode(ah, ah->ah_ant_mode);
  2688. unlock:
  2689. mutex_unlock(&sc->lock);
  2690. return ret;
  2691. }
  2692. static u64 ath5k_prepare_multicast(struct ieee80211_hw *hw,
  2693. struct netdev_hw_addr_list *mc_list)
  2694. {
  2695. u32 mfilt[2], val;
  2696. u8 pos;
  2697. struct netdev_hw_addr *ha;
  2698. mfilt[0] = 0;
  2699. mfilt[1] = 1;
  2700. netdev_hw_addr_list_for_each(ha, mc_list) {
  2701. /* calculate XOR of eight 6-bit values */
  2702. val = get_unaligned_le32(ha->addr + 0);
  2703. pos = (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
  2704. val = get_unaligned_le32(ha->addr + 3);
  2705. pos ^= (val >> 18) ^ (val >> 12) ^ (val >> 6) ^ val;
  2706. pos &= 0x3f;
  2707. mfilt[pos / 32] |= (1 << (pos % 32));
  2708. /* XXX: we might be able to just do this instead,
  2709. * but not sure, needs testing, if we do use this we'd
  2710. * neet to inform below to not reset the mcast */
  2711. /* ath5k_hw_set_mcast_filterindex(ah,
  2712. * ha->addr[5]); */
  2713. }
  2714. return ((u64)(mfilt[1]) << 32) | mfilt[0];
  2715. }
  2716. static bool ath_any_vif_assoc(struct ath5k_softc *sc)
  2717. {
  2718. struct ath_vif_iter_data iter_data;
  2719. iter_data.hw_macaddr = NULL;
  2720. iter_data.any_assoc = false;
  2721. iter_data.need_set_hw_addr = false;
  2722. iter_data.found_active = true;
  2723. ieee80211_iterate_active_interfaces_atomic(sc->hw, ath_vif_iter,
  2724. &iter_data);
  2725. return iter_data.any_assoc;
  2726. }
  2727. #define SUPPORTED_FIF_FLAGS \
  2728. FIF_PROMISC_IN_BSS | FIF_ALLMULTI | FIF_FCSFAIL | \
  2729. FIF_PLCPFAIL | FIF_CONTROL | FIF_OTHER_BSS | \
  2730. FIF_BCN_PRBRESP_PROMISC
  2731. /*
  2732. * o always accept unicast, broadcast, and multicast traffic
  2733. * o multicast traffic for all BSSIDs will be enabled if mac80211
  2734. * says it should be
  2735. * o maintain current state of phy ofdm or phy cck error reception.
  2736. * If the hardware detects any of these type of errors then
  2737. * ath5k_hw_get_rx_filter() will pass to us the respective
  2738. * hardware filters to be able to receive these type of frames.
  2739. * o probe request frames are accepted only when operating in
  2740. * hostap, adhoc, or monitor modes
  2741. * o enable promiscuous mode according to the interface state
  2742. * o accept beacons:
  2743. * - when operating in adhoc mode so the 802.11 layer creates
  2744. * node table entries for peers,
  2745. * - when operating in station mode for collecting rssi data when
  2746. * the station is otherwise quiet, or
  2747. * - when scanning
  2748. */
  2749. static void ath5k_configure_filter(struct ieee80211_hw *hw,
  2750. unsigned int changed_flags,
  2751. unsigned int *new_flags,
  2752. u64 multicast)
  2753. {
  2754. struct ath5k_softc *sc = hw->priv;
  2755. struct ath5k_hw *ah = sc->ah;
  2756. u32 mfilt[2], rfilt;
  2757. mutex_lock(&sc->lock);
  2758. mfilt[0] = multicast;
  2759. mfilt[1] = multicast >> 32;
  2760. /* Only deal with supported flags */
  2761. changed_flags &= SUPPORTED_FIF_FLAGS;
  2762. *new_flags &= SUPPORTED_FIF_FLAGS;
  2763. /* If HW detects any phy or radar errors, leave those filters on.
  2764. * Also, always enable Unicast, Broadcasts and Multicast
  2765. * XXX: move unicast, bssid broadcasts and multicast to mac80211 */
  2766. rfilt = (ath5k_hw_get_rx_filter(ah) & (AR5K_RX_FILTER_PHYERR)) |
  2767. (AR5K_RX_FILTER_UCAST | AR5K_RX_FILTER_BCAST |
  2768. AR5K_RX_FILTER_MCAST);
  2769. if (changed_flags & (FIF_PROMISC_IN_BSS | FIF_OTHER_BSS)) {
  2770. if (*new_flags & FIF_PROMISC_IN_BSS) {
  2771. __set_bit(ATH_STAT_PROMISC, sc->status);
  2772. } else {
  2773. __clear_bit(ATH_STAT_PROMISC, sc->status);
  2774. }
  2775. }
  2776. if (test_bit(ATH_STAT_PROMISC, sc->status))
  2777. rfilt |= AR5K_RX_FILTER_PROM;
  2778. /* Note, AR5K_RX_FILTER_MCAST is already enabled */
  2779. if (*new_flags & FIF_ALLMULTI) {
  2780. mfilt[0] = ~0;
  2781. mfilt[1] = ~0;
  2782. }
  2783. /* This is the best we can do */
  2784. if (*new_flags & (FIF_FCSFAIL | FIF_PLCPFAIL))
  2785. rfilt |= AR5K_RX_FILTER_PHYERR;
  2786. /* FIF_BCN_PRBRESP_PROMISC really means to enable beacons
  2787. * and probes for any BSSID */
  2788. if ((*new_flags & FIF_BCN_PRBRESP_PROMISC) || (sc->nvifs > 1))
  2789. rfilt |= AR5K_RX_FILTER_BEACON;
  2790. /* FIF_CONTROL doc says that if FIF_PROMISC_IN_BSS is not
  2791. * set we should only pass on control frames for this
  2792. * station. This needs testing. I believe right now this
  2793. * enables *all* control frames, which is OK.. but
  2794. * but we should see if we can improve on granularity */
  2795. if (*new_flags & FIF_CONTROL)
  2796. rfilt |= AR5K_RX_FILTER_CONTROL;
  2797. /* Additional settings per mode -- this is per ath5k */
  2798. /* XXX move these to mac80211, and add a beacon IFF flag to mac80211 */
  2799. switch (sc->opmode) {
  2800. case NL80211_IFTYPE_MESH_POINT:
  2801. rfilt |= AR5K_RX_FILTER_CONTROL |
  2802. AR5K_RX_FILTER_BEACON |
  2803. AR5K_RX_FILTER_PROBEREQ |
  2804. AR5K_RX_FILTER_PROM;
  2805. break;
  2806. case NL80211_IFTYPE_AP:
  2807. case NL80211_IFTYPE_ADHOC:
  2808. rfilt |= AR5K_RX_FILTER_PROBEREQ |
  2809. AR5K_RX_FILTER_BEACON;
  2810. break;
  2811. case NL80211_IFTYPE_STATION:
  2812. if (sc->assoc)
  2813. rfilt |= AR5K_RX_FILTER_BEACON;
  2814. default:
  2815. break;
  2816. }
  2817. /* Set filters */
  2818. ath5k_hw_set_rx_filter(ah, rfilt);
  2819. /* Set multicast bits */
  2820. ath5k_hw_set_mcast_filter(ah, mfilt[0], mfilt[1]);
  2821. /* Set the cached hw filter flags, this will later actually
  2822. * be set in HW */
  2823. sc->filter_flags = rfilt;
  2824. mutex_unlock(&sc->lock);
  2825. }
  2826. static int
  2827. ath5k_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
  2828. struct ieee80211_vif *vif, struct ieee80211_sta *sta,
  2829. struct ieee80211_key_conf *key)
  2830. {
  2831. struct ath5k_softc *sc = hw->priv;
  2832. struct ath5k_hw *ah = sc->ah;
  2833. struct ath_common *common = ath5k_hw_common(ah);
  2834. int ret = 0;
  2835. if (modparam_nohwcrypt)
  2836. return -EOPNOTSUPP;
  2837. switch (key->cipher) {
  2838. case WLAN_CIPHER_SUITE_WEP40:
  2839. case WLAN_CIPHER_SUITE_WEP104:
  2840. case WLAN_CIPHER_SUITE_TKIP:
  2841. break;
  2842. case WLAN_CIPHER_SUITE_CCMP:
  2843. if (common->crypt_caps & ATH_CRYPT_CAP_CIPHER_AESCCM)
  2844. break;
  2845. return -EOPNOTSUPP;
  2846. default:
  2847. WARN_ON(1);
  2848. return -EINVAL;
  2849. }
  2850. mutex_lock(&sc->lock);
  2851. switch (cmd) {
  2852. case SET_KEY:
  2853. ret = ath_key_config(common, vif, sta, key);
  2854. if (ret >= 0) {
  2855. key->hw_key_idx = ret;
  2856. /* push IV and Michael MIC generation to stack */
  2857. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  2858. if (key->cipher == WLAN_CIPHER_SUITE_TKIP)
  2859. key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
  2860. if (key->cipher == WLAN_CIPHER_SUITE_CCMP)
  2861. key->flags |= IEEE80211_KEY_FLAG_SW_MGMT;
  2862. ret = 0;
  2863. }
  2864. break;
  2865. case DISABLE_KEY:
  2866. ath_key_delete(common, key);
  2867. break;
  2868. default:
  2869. ret = -EINVAL;
  2870. }
  2871. mmiowb();
  2872. mutex_unlock(&sc->lock);
  2873. return ret;
  2874. }
  2875. static int
  2876. ath5k_get_stats(struct ieee80211_hw *hw,
  2877. struct ieee80211_low_level_stats *stats)
  2878. {
  2879. struct ath5k_softc *sc = hw->priv;
  2880. /* Force update */
  2881. ath5k_hw_update_mib_counters(sc->ah);
  2882. stats->dot11ACKFailureCount = sc->stats.ack_fail;
  2883. stats->dot11RTSFailureCount = sc->stats.rts_fail;
  2884. stats->dot11RTSSuccessCount = sc->stats.rts_ok;
  2885. stats->dot11FCSErrorCount = sc->stats.fcs_error;
  2886. return 0;
  2887. }
  2888. static int ath5k_get_survey(struct ieee80211_hw *hw, int idx,
  2889. struct survey_info *survey)
  2890. {
  2891. struct ath5k_softc *sc = hw->priv;
  2892. struct ieee80211_conf *conf = &hw->conf;
  2893. struct ath_common *common = ath5k_hw_common(sc->ah);
  2894. struct ath_cycle_counters *cc = &common->cc_survey;
  2895. unsigned int div = common->clockrate * 1000;
  2896. if (idx != 0)
  2897. return -ENOENT;
  2898. survey->channel = conf->channel;
  2899. survey->filled = SURVEY_INFO_NOISE_DBM;
  2900. survey->noise = sc->ah->ah_noise_floor;
  2901. spin_lock_bh(&common->cc_lock);
  2902. ath_hw_cycle_counters_update(common);
  2903. if (cc->cycles > 0) {
  2904. survey->filled |= SURVEY_INFO_CHANNEL_TIME |
  2905. SURVEY_INFO_CHANNEL_TIME_BUSY |
  2906. SURVEY_INFO_CHANNEL_TIME_RX |
  2907. SURVEY_INFO_CHANNEL_TIME_TX;
  2908. survey->channel_time += cc->cycles / div;
  2909. survey->channel_time_busy += cc->rx_busy / div;
  2910. survey->channel_time_rx += cc->rx_frame / div;
  2911. survey->channel_time_tx += cc->tx_frame / div;
  2912. }
  2913. memset(cc, 0, sizeof(*cc));
  2914. spin_unlock_bh(&common->cc_lock);
  2915. return 0;
  2916. }
  2917. static u64
  2918. ath5k_get_tsf(struct ieee80211_hw *hw)
  2919. {
  2920. struct ath5k_softc *sc = hw->priv;
  2921. return ath5k_hw_get_tsf64(sc->ah);
  2922. }
  2923. static void
  2924. ath5k_set_tsf(struct ieee80211_hw *hw, u64 tsf)
  2925. {
  2926. struct ath5k_softc *sc = hw->priv;
  2927. ath5k_hw_set_tsf64(sc->ah, tsf);
  2928. }
  2929. static void
  2930. ath5k_reset_tsf(struct ieee80211_hw *hw)
  2931. {
  2932. struct ath5k_softc *sc = hw->priv;
  2933. /*
  2934. * in IBSS mode we need to update the beacon timers too.
  2935. * this will also reset the TSF if we call it with 0
  2936. */
  2937. if (sc->opmode == NL80211_IFTYPE_ADHOC)
  2938. ath5k_beacon_update_timers(sc, 0);
  2939. else
  2940. ath5k_hw_reset_tsf(sc->ah);
  2941. }
  2942. static void
  2943. set_beacon_filter(struct ieee80211_hw *hw, bool enable)
  2944. {
  2945. struct ath5k_softc *sc = hw->priv;
  2946. struct ath5k_hw *ah = sc->ah;
  2947. u32 rfilt;
  2948. rfilt = ath5k_hw_get_rx_filter(ah);
  2949. if (enable)
  2950. rfilt |= AR5K_RX_FILTER_BEACON;
  2951. else
  2952. rfilt &= ~AR5K_RX_FILTER_BEACON;
  2953. ath5k_hw_set_rx_filter(ah, rfilt);
  2954. sc->filter_flags = rfilt;
  2955. }
  2956. static void ath5k_bss_info_changed(struct ieee80211_hw *hw,
  2957. struct ieee80211_vif *vif,
  2958. struct ieee80211_bss_conf *bss_conf,
  2959. u32 changes)
  2960. {
  2961. struct ath5k_vif *avf = (void *)vif->drv_priv;
  2962. struct ath5k_softc *sc = hw->priv;
  2963. struct ath5k_hw *ah = sc->ah;
  2964. struct ath_common *common = ath5k_hw_common(ah);
  2965. unsigned long flags;
  2966. mutex_lock(&sc->lock);
  2967. if (changes & BSS_CHANGED_BSSID) {
  2968. /* Cache for later use during resets */
  2969. memcpy(common->curbssid, bss_conf->bssid, ETH_ALEN);
  2970. common->curaid = 0;
  2971. ath5k_hw_set_bssid(ah);
  2972. mmiowb();
  2973. }
  2974. if (changes & BSS_CHANGED_BEACON_INT)
  2975. sc->bintval = bss_conf->beacon_int;
  2976. if (changes & BSS_CHANGED_ASSOC) {
  2977. avf->assoc = bss_conf->assoc;
  2978. if (bss_conf->assoc)
  2979. sc->assoc = bss_conf->assoc;
  2980. else
  2981. sc->assoc = ath_any_vif_assoc(sc);
  2982. if (sc->opmode == NL80211_IFTYPE_STATION)
  2983. set_beacon_filter(hw, sc->assoc);
  2984. ath5k_hw_set_ledstate(sc->ah, sc->assoc ?
  2985. AR5K_LED_ASSOC : AR5K_LED_INIT);
  2986. if (bss_conf->assoc) {
  2987. ATH5K_DBG(sc, ATH5K_DEBUG_ANY,
  2988. "Bss Info ASSOC %d, bssid: %pM\n",
  2989. bss_conf->aid, common->curbssid);
  2990. common->curaid = bss_conf->aid;
  2991. ath5k_hw_set_bssid(ah);
  2992. /* Once ANI is available you would start it here */
  2993. }
  2994. }
  2995. if (changes & BSS_CHANGED_BEACON) {
  2996. spin_lock_irqsave(&sc->block, flags);
  2997. ath5k_beacon_update(hw, vif);
  2998. spin_unlock_irqrestore(&sc->block, flags);
  2999. }
  3000. if (changes & BSS_CHANGED_BEACON_ENABLED)
  3001. sc->enable_beacon = bss_conf->enable_beacon;
  3002. if (changes & (BSS_CHANGED_BEACON | BSS_CHANGED_BEACON_ENABLED |
  3003. BSS_CHANGED_BEACON_INT))
  3004. ath5k_beacon_config(sc);
  3005. mutex_unlock(&sc->lock);
  3006. }
  3007. static void ath5k_sw_scan_start(struct ieee80211_hw *hw)
  3008. {
  3009. struct ath5k_softc *sc = hw->priv;
  3010. if (!sc->assoc)
  3011. ath5k_hw_set_ledstate(sc->ah, AR5K_LED_SCAN);
  3012. }
  3013. static void ath5k_sw_scan_complete(struct ieee80211_hw *hw)
  3014. {
  3015. struct ath5k_softc *sc = hw->priv;
  3016. ath5k_hw_set_ledstate(sc->ah, sc->assoc ?
  3017. AR5K_LED_ASSOC : AR5K_LED_INIT);
  3018. }
  3019. /**
  3020. * ath5k_set_coverage_class - Set IEEE 802.11 coverage class
  3021. *
  3022. * @hw: struct ieee80211_hw pointer
  3023. * @coverage_class: IEEE 802.11 coverage class number
  3024. *
  3025. * Mac80211 callback. Sets slot time, ACK timeout and CTS timeout for given
  3026. * coverage class. The values are persistent, they are restored after device
  3027. * reset.
  3028. */
  3029. static void ath5k_set_coverage_class(struct ieee80211_hw *hw, u8 coverage_class)
  3030. {
  3031. struct ath5k_softc *sc = hw->priv;
  3032. mutex_lock(&sc->lock);
  3033. ath5k_hw_set_coverage_class(sc->ah, coverage_class);
  3034. mutex_unlock(&sc->lock);
  3035. }
  3036. static int ath5k_conf_tx(struct ieee80211_hw *hw, u16 queue,
  3037. const struct ieee80211_tx_queue_params *params)
  3038. {
  3039. struct ath5k_softc *sc = hw->priv;
  3040. struct ath5k_hw *ah = sc->ah;
  3041. struct ath5k_txq_info qi;
  3042. int ret = 0;
  3043. if (queue >= ah->ah_capabilities.cap_queues.q_tx_num)
  3044. return 0;
  3045. mutex_lock(&sc->lock);
  3046. ath5k_hw_get_tx_queueprops(ah, queue, &qi);
  3047. qi.tqi_aifs = params->aifs;
  3048. qi.tqi_cw_min = params->cw_min;
  3049. qi.tqi_cw_max = params->cw_max;
  3050. qi.tqi_burst_time = params->txop;
  3051. ATH5K_DBG(sc, ATH5K_DEBUG_ANY,
  3052. "Configure tx [queue %d], "
  3053. "aifs: %d, cw_min: %d, cw_max: %d, txop: %d\n",
  3054. queue, params->aifs, params->cw_min,
  3055. params->cw_max, params->txop);
  3056. if (ath5k_hw_set_tx_queueprops(ah, queue, &qi)) {
  3057. ATH5K_ERR(sc,
  3058. "Unable to update hardware queue %u!\n", queue);
  3059. ret = -EIO;
  3060. } else
  3061. ath5k_hw_reset_tx_queue(ah, queue);
  3062. mutex_unlock(&sc->lock);
  3063. return ret;
  3064. }
  3065. static int ath5k_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
  3066. {
  3067. struct ath5k_softc *sc = hw->priv;
  3068. if (tx_ant == 1 && rx_ant == 1)
  3069. ath5k_hw_set_antenna_mode(sc->ah, AR5K_ANTMODE_FIXED_A);
  3070. else if (tx_ant == 2 && rx_ant == 2)
  3071. ath5k_hw_set_antenna_mode(sc->ah, AR5K_ANTMODE_FIXED_B);
  3072. else if ((tx_ant & 3) == 3 && (rx_ant & 3) == 3)
  3073. ath5k_hw_set_antenna_mode(sc->ah, AR5K_ANTMODE_DEFAULT);
  3074. else
  3075. return -EINVAL;
  3076. return 0;
  3077. }
  3078. static int ath5k_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
  3079. {
  3080. struct ath5k_softc *sc = hw->priv;
  3081. switch (sc->ah->ah_ant_mode) {
  3082. case AR5K_ANTMODE_FIXED_A:
  3083. *tx_ant = 1; *rx_ant = 1; break;
  3084. case AR5K_ANTMODE_FIXED_B:
  3085. *tx_ant = 2; *rx_ant = 2; break;
  3086. case AR5K_ANTMODE_DEFAULT:
  3087. *tx_ant = 3; *rx_ant = 3; break;
  3088. }
  3089. return 0;
  3090. }
  3091. const struct ieee80211_ops ath5k_hw_ops = {
  3092. .tx = ath5k_tx,
  3093. .start = ath5k_start,
  3094. .stop = ath5k_stop,
  3095. .add_interface = ath5k_add_interface,
  3096. .remove_interface = ath5k_remove_interface,
  3097. .config = ath5k_config,
  3098. .prepare_multicast = ath5k_prepare_multicast,
  3099. .configure_filter = ath5k_configure_filter,
  3100. .set_key = ath5k_set_key,
  3101. .get_stats = ath5k_get_stats,
  3102. .get_survey = ath5k_get_survey,
  3103. .conf_tx = ath5k_conf_tx,
  3104. .get_tsf = ath5k_get_tsf,
  3105. .set_tsf = ath5k_set_tsf,
  3106. .reset_tsf = ath5k_reset_tsf,
  3107. .bss_info_changed = ath5k_bss_info_changed,
  3108. .sw_scan_start = ath5k_sw_scan_start,
  3109. .sw_scan_complete = ath5k_sw_scan_complete,
  3110. .set_coverage_class = ath5k_set_coverage_class,
  3111. .set_antenna = ath5k_set_antenna,
  3112. .get_antenna = ath5k_get_antenna,
  3113. };