base.c 81 KB

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