base.c 79 KB

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