base.c 82 KB

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