base.c 85 KB

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