base.c 100 KB

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