base.c 79 KB

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