base.c 86 KB

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