base.c 100 KB

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