base.c 80 KB

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