base.c 83 KB

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