base.c 81 KB

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