base.c 80 KB

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