base.c 79 KB

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