base.c 83 KB

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