main.c 83 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232
  1. /*
  2. * Copyright (c) 2008-2009 Atheros Communications Inc.
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <linux/nl80211.h>
  17. #include "ath9k.h"
  18. #include "btcoex.h"
  19. static char *dev_info = "ath9k";
  20. MODULE_AUTHOR("Atheros Communications");
  21. MODULE_DESCRIPTION("Support for Atheros 802.11n wireless LAN cards.");
  22. MODULE_SUPPORTED_DEVICE("Atheros 802.11n WLAN cards");
  23. MODULE_LICENSE("Dual BSD/GPL");
  24. static int modparam_nohwcrypt;
  25. module_param_named(nohwcrypt, modparam_nohwcrypt, int, 0444);
  26. MODULE_PARM_DESC(nohwcrypt, "Disable hardware encryption");
  27. static unsigned int ath9k_debug = ATH_DBG_DEFAULT;
  28. module_param_named(debug, ath9k_debug, uint, 0);
  29. MODULE_PARM_DESC(debug, "Debugging mask");
  30. /* We use the hw_value as an index into our private channel structure */
  31. #define CHAN2G(_freq, _idx) { \
  32. .center_freq = (_freq), \
  33. .hw_value = (_idx), \
  34. .max_power = 20, \
  35. }
  36. #define CHAN5G(_freq, _idx) { \
  37. .band = IEEE80211_BAND_5GHZ, \
  38. .center_freq = (_freq), \
  39. .hw_value = (_idx), \
  40. .max_power = 20, \
  41. }
  42. /* Some 2 GHz radios are actually tunable on 2312-2732
  43. * on 5 MHz steps, we support the channels which we know
  44. * we have calibration data for all cards though to make
  45. * this static */
  46. static struct ieee80211_channel ath9k_2ghz_chantable[] = {
  47. CHAN2G(2412, 0), /* Channel 1 */
  48. CHAN2G(2417, 1), /* Channel 2 */
  49. CHAN2G(2422, 2), /* Channel 3 */
  50. CHAN2G(2427, 3), /* Channel 4 */
  51. CHAN2G(2432, 4), /* Channel 5 */
  52. CHAN2G(2437, 5), /* Channel 6 */
  53. CHAN2G(2442, 6), /* Channel 7 */
  54. CHAN2G(2447, 7), /* Channel 8 */
  55. CHAN2G(2452, 8), /* Channel 9 */
  56. CHAN2G(2457, 9), /* Channel 10 */
  57. CHAN2G(2462, 10), /* Channel 11 */
  58. CHAN2G(2467, 11), /* Channel 12 */
  59. CHAN2G(2472, 12), /* Channel 13 */
  60. CHAN2G(2484, 13), /* Channel 14 */
  61. };
  62. /* Some 5 GHz radios are actually tunable on XXXX-YYYY
  63. * on 5 MHz steps, we support the channels which we know
  64. * we have calibration data for all cards though to make
  65. * this static */
  66. static struct ieee80211_channel ath9k_5ghz_chantable[] = {
  67. /* _We_ call this UNII 1 */
  68. CHAN5G(5180, 14), /* Channel 36 */
  69. CHAN5G(5200, 15), /* Channel 40 */
  70. CHAN5G(5220, 16), /* Channel 44 */
  71. CHAN5G(5240, 17), /* Channel 48 */
  72. /* _We_ call this UNII 2 */
  73. CHAN5G(5260, 18), /* Channel 52 */
  74. CHAN5G(5280, 19), /* Channel 56 */
  75. CHAN5G(5300, 20), /* Channel 60 */
  76. CHAN5G(5320, 21), /* Channel 64 */
  77. /* _We_ call this "Middle band" */
  78. CHAN5G(5500, 22), /* Channel 100 */
  79. CHAN5G(5520, 23), /* Channel 104 */
  80. CHAN5G(5540, 24), /* Channel 108 */
  81. CHAN5G(5560, 25), /* Channel 112 */
  82. CHAN5G(5580, 26), /* Channel 116 */
  83. CHAN5G(5600, 27), /* Channel 120 */
  84. CHAN5G(5620, 28), /* Channel 124 */
  85. CHAN5G(5640, 29), /* Channel 128 */
  86. CHAN5G(5660, 30), /* Channel 132 */
  87. CHAN5G(5680, 31), /* Channel 136 */
  88. CHAN5G(5700, 32), /* Channel 140 */
  89. /* _We_ call this UNII 3 */
  90. CHAN5G(5745, 33), /* Channel 149 */
  91. CHAN5G(5765, 34), /* Channel 153 */
  92. CHAN5G(5785, 35), /* Channel 157 */
  93. CHAN5G(5805, 36), /* Channel 161 */
  94. CHAN5G(5825, 37), /* Channel 165 */
  95. };
  96. /* Atheros hardware rate code addition for short premble */
  97. #define SHPCHECK(__hw_rate, __flags) \
  98. ((__flags & IEEE80211_RATE_SHORT_PREAMBLE) ? (__hw_rate | 0x04 ) : 0)
  99. #define RATE(_bitrate, _hw_rate, _flags) { \
  100. .bitrate = (_bitrate), \
  101. .flags = (_flags), \
  102. .hw_value = (_hw_rate), \
  103. .hw_value_short = (SHPCHECK(_hw_rate, _flags)) \
  104. }
  105. static struct ieee80211_rate ath9k_legacy_rates[] = {
  106. RATE(10, 0x1b, 0),
  107. RATE(20, 0x1a, IEEE80211_RATE_SHORT_PREAMBLE),
  108. RATE(55, 0x19, IEEE80211_RATE_SHORT_PREAMBLE),
  109. RATE(110, 0x18, IEEE80211_RATE_SHORT_PREAMBLE),
  110. RATE(60, 0x0b, 0),
  111. RATE(90, 0x0f, 0),
  112. RATE(120, 0x0a, 0),
  113. RATE(180, 0x0e, 0),
  114. RATE(240, 0x09, 0),
  115. RATE(360, 0x0d, 0),
  116. RATE(480, 0x08, 0),
  117. RATE(540, 0x0c, 0),
  118. };
  119. static void ath_cache_conf_rate(struct ath_softc *sc,
  120. struct ieee80211_conf *conf)
  121. {
  122. switch (conf->channel->band) {
  123. case IEEE80211_BAND_2GHZ:
  124. if (conf_is_ht20(conf))
  125. sc->cur_rate_mode = ATH9K_MODE_11NG_HT20;
  126. else if (conf_is_ht40_minus(conf))
  127. sc->cur_rate_mode = ATH9K_MODE_11NG_HT40MINUS;
  128. else if (conf_is_ht40_plus(conf))
  129. sc->cur_rate_mode = ATH9K_MODE_11NG_HT40PLUS;
  130. else
  131. sc->cur_rate_mode = ATH9K_MODE_11G;
  132. break;
  133. case IEEE80211_BAND_5GHZ:
  134. if (conf_is_ht20(conf))
  135. sc->cur_rate_mode = ATH9K_MODE_11NA_HT20;
  136. else if (conf_is_ht40_minus(conf))
  137. sc->cur_rate_mode = ATH9K_MODE_11NA_HT40MINUS;
  138. else if (conf_is_ht40_plus(conf))
  139. sc->cur_rate_mode = ATH9K_MODE_11NA_HT40PLUS;
  140. else
  141. sc->cur_rate_mode = ATH9K_MODE_11A;
  142. break;
  143. default:
  144. BUG_ON(1);
  145. break;
  146. }
  147. }
  148. static void ath_update_txpow(struct ath_softc *sc)
  149. {
  150. struct ath_hw *ah = sc->sc_ah;
  151. u32 txpow;
  152. if (sc->curtxpow != sc->config.txpowlimit) {
  153. ath9k_hw_set_txpowerlimit(ah, sc->config.txpowlimit);
  154. /* read back in case value is clamped */
  155. ath9k_hw_getcapability(ah, ATH9K_CAP_TXPOW, 1, &txpow);
  156. sc->curtxpow = txpow;
  157. }
  158. }
  159. static u8 parse_mpdudensity(u8 mpdudensity)
  160. {
  161. /*
  162. * 802.11n D2.0 defined values for "Minimum MPDU Start Spacing":
  163. * 0 for no restriction
  164. * 1 for 1/4 us
  165. * 2 for 1/2 us
  166. * 3 for 1 us
  167. * 4 for 2 us
  168. * 5 for 4 us
  169. * 6 for 8 us
  170. * 7 for 16 us
  171. */
  172. switch (mpdudensity) {
  173. case 0:
  174. return 0;
  175. case 1:
  176. case 2:
  177. case 3:
  178. /* Our lower layer calculations limit our precision to
  179. 1 microsecond */
  180. return 1;
  181. case 4:
  182. return 2;
  183. case 5:
  184. return 4;
  185. case 6:
  186. return 8;
  187. case 7:
  188. return 16;
  189. default:
  190. return 0;
  191. }
  192. }
  193. static struct ath9k_channel *ath_get_curchannel(struct ath_softc *sc,
  194. struct ieee80211_hw *hw)
  195. {
  196. struct ieee80211_channel *curchan = hw->conf.channel;
  197. struct ath9k_channel *channel;
  198. u8 chan_idx;
  199. chan_idx = curchan->hw_value;
  200. channel = &sc->sc_ah->channels[chan_idx];
  201. ath9k_update_ichannel(sc, hw, channel);
  202. return channel;
  203. }
  204. static bool ath9k_setpower(struct ath_softc *sc, enum ath9k_power_mode mode)
  205. {
  206. unsigned long flags;
  207. bool ret;
  208. spin_lock_irqsave(&sc->sc_pm_lock, flags);
  209. ret = ath9k_hw_setpower(sc->sc_ah, mode);
  210. spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
  211. return ret;
  212. }
  213. void ath9k_ps_wakeup(struct ath_softc *sc)
  214. {
  215. unsigned long flags;
  216. spin_lock_irqsave(&sc->sc_pm_lock, flags);
  217. if (++sc->ps_usecount != 1)
  218. goto unlock;
  219. ath9k_hw_setpower(sc->sc_ah, ATH9K_PM_AWAKE);
  220. unlock:
  221. spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
  222. }
  223. void ath9k_ps_restore(struct ath_softc *sc)
  224. {
  225. unsigned long flags;
  226. spin_lock_irqsave(&sc->sc_pm_lock, flags);
  227. if (--sc->ps_usecount != 0)
  228. goto unlock;
  229. if (sc->ps_enabled &&
  230. !(sc->sc_flags & (SC_OP_WAIT_FOR_BEACON |
  231. SC_OP_WAIT_FOR_CAB |
  232. SC_OP_WAIT_FOR_PSPOLL_DATA |
  233. SC_OP_WAIT_FOR_TX_ACK)))
  234. ath9k_hw_setpower(sc->sc_ah, ATH9K_PM_NETWORK_SLEEP);
  235. unlock:
  236. spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
  237. }
  238. /*
  239. * Set/change channels. If the channel is really being changed, it's done
  240. * by reseting the chip. To accomplish this we must first cleanup any pending
  241. * DMA, then restart stuff.
  242. */
  243. int ath_set_channel(struct ath_softc *sc, struct ieee80211_hw *hw,
  244. struct ath9k_channel *hchan)
  245. {
  246. struct ath_hw *ah = sc->sc_ah;
  247. struct ath_common *common = ath9k_hw_common(ah);
  248. struct ieee80211_conf *conf = &common->hw->conf;
  249. bool fastcc = true, stopped;
  250. struct ieee80211_channel *channel = hw->conf.channel;
  251. int r;
  252. if (sc->sc_flags & SC_OP_INVALID)
  253. return -EIO;
  254. ath9k_ps_wakeup(sc);
  255. /*
  256. * This is only performed if the channel settings have
  257. * actually changed.
  258. *
  259. * To switch channels clear any pending DMA operations;
  260. * wait long enough for the RX fifo to drain, reset the
  261. * hardware at the new frequency, and then re-enable
  262. * the relevant bits of the h/w.
  263. */
  264. ath9k_hw_set_interrupts(ah, 0);
  265. ath_drain_all_txq(sc, false);
  266. stopped = ath_stoprecv(sc);
  267. /* XXX: do not flush receive queue here. We don't want
  268. * to flush data frames already in queue because of
  269. * changing channel. */
  270. if (!stopped || (sc->sc_flags & SC_OP_FULL_RESET))
  271. fastcc = false;
  272. ath_print(common, ATH_DBG_CONFIG,
  273. "(%u MHz) -> (%u MHz), conf_is_ht40: %d\n",
  274. sc->sc_ah->curchan->channel,
  275. channel->center_freq, conf_is_ht40(conf));
  276. spin_lock_bh(&sc->sc_resetlock);
  277. r = ath9k_hw_reset(ah, hchan, fastcc);
  278. if (r) {
  279. ath_print(common, ATH_DBG_FATAL,
  280. "Unable to reset channel (%u Mhz) "
  281. "reset status %d\n",
  282. channel->center_freq, r);
  283. spin_unlock_bh(&sc->sc_resetlock);
  284. goto ps_restore;
  285. }
  286. spin_unlock_bh(&sc->sc_resetlock);
  287. sc->sc_flags &= ~SC_OP_FULL_RESET;
  288. if (ath_startrecv(sc) != 0) {
  289. ath_print(common, ATH_DBG_FATAL,
  290. "Unable to restart recv logic\n");
  291. r = -EIO;
  292. goto ps_restore;
  293. }
  294. ath_cache_conf_rate(sc, &hw->conf);
  295. ath_update_txpow(sc);
  296. ath9k_hw_set_interrupts(ah, sc->imask);
  297. ps_restore:
  298. ath9k_ps_restore(sc);
  299. return r;
  300. }
  301. /*
  302. * This routine performs the periodic noise floor calibration function
  303. * that is used to adjust and optimize the chip performance. This
  304. * takes environmental changes (location, temperature) into account.
  305. * When the task is complete, it reschedules itself depending on the
  306. * appropriate interval that was calculated.
  307. */
  308. static void ath_ani_calibrate(unsigned long data)
  309. {
  310. struct ath_softc *sc = (struct ath_softc *)data;
  311. struct ath_hw *ah = sc->sc_ah;
  312. struct ath_common *common = ath9k_hw_common(ah);
  313. bool longcal = false;
  314. bool shortcal = false;
  315. bool aniflag = false;
  316. unsigned int timestamp = jiffies_to_msecs(jiffies);
  317. u32 cal_interval, short_cal_interval;
  318. short_cal_interval = (ah->opmode == NL80211_IFTYPE_AP) ?
  319. ATH_AP_SHORT_CALINTERVAL : ATH_STA_SHORT_CALINTERVAL;
  320. /*
  321. * don't calibrate when we're scanning.
  322. * we are most likely not on our home channel.
  323. */
  324. spin_lock(&sc->ani_lock);
  325. if (sc->sc_flags & SC_OP_SCANNING)
  326. goto set_timer;
  327. /* Only calibrate if awake */
  328. if (sc->sc_ah->power_mode != ATH9K_PM_AWAKE)
  329. goto set_timer;
  330. ath9k_ps_wakeup(sc);
  331. /* Long calibration runs independently of short calibration. */
  332. if ((timestamp - common->ani.longcal_timer) >= ATH_LONG_CALINTERVAL) {
  333. longcal = true;
  334. ath_print(common, ATH_DBG_ANI, "longcal @%lu\n", jiffies);
  335. common->ani.longcal_timer = timestamp;
  336. }
  337. /* Short calibration applies only while caldone is false */
  338. if (!common->ani.caldone) {
  339. if ((timestamp - common->ani.shortcal_timer) >= short_cal_interval) {
  340. shortcal = true;
  341. ath_print(common, ATH_DBG_ANI,
  342. "shortcal @%lu\n", jiffies);
  343. common->ani.shortcal_timer = timestamp;
  344. common->ani.resetcal_timer = timestamp;
  345. }
  346. } else {
  347. if ((timestamp - common->ani.resetcal_timer) >=
  348. ATH_RESTART_CALINTERVAL) {
  349. common->ani.caldone = ath9k_hw_reset_calvalid(ah);
  350. if (common->ani.caldone)
  351. common->ani.resetcal_timer = timestamp;
  352. }
  353. }
  354. /* Verify whether we must check ANI */
  355. if ((timestamp - common->ani.checkani_timer) >= ATH_ANI_POLLINTERVAL) {
  356. aniflag = true;
  357. common->ani.checkani_timer = timestamp;
  358. }
  359. /* Skip all processing if there's nothing to do. */
  360. if (longcal || shortcal || aniflag) {
  361. /* Call ANI routine if necessary */
  362. if (aniflag)
  363. ath9k_hw_ani_monitor(ah, ah->curchan);
  364. /* Perform calibration if necessary */
  365. if (longcal || shortcal) {
  366. common->ani.caldone =
  367. ath9k_hw_calibrate(ah,
  368. ah->curchan,
  369. common->rx_chainmask,
  370. longcal);
  371. if (longcal)
  372. common->ani.noise_floor = ath9k_hw_getchan_noise(ah,
  373. ah->curchan);
  374. ath_print(common, ATH_DBG_ANI,
  375. " calibrate chan %u/%x nf: %d\n",
  376. ah->curchan->channel,
  377. ah->curchan->channelFlags,
  378. common->ani.noise_floor);
  379. }
  380. }
  381. ath9k_ps_restore(sc);
  382. set_timer:
  383. spin_unlock(&sc->ani_lock);
  384. /*
  385. * Set timer interval based on previous results.
  386. * The interval must be the shortest necessary to satisfy ANI,
  387. * short calibration and long calibration.
  388. */
  389. cal_interval = ATH_LONG_CALINTERVAL;
  390. if (sc->sc_ah->config.enable_ani)
  391. cal_interval = min(cal_interval, (u32)ATH_ANI_POLLINTERVAL);
  392. if (!common->ani.caldone)
  393. cal_interval = min(cal_interval, (u32)short_cal_interval);
  394. mod_timer(&common->ani.timer, jiffies + msecs_to_jiffies(cal_interval));
  395. }
  396. static void ath_start_ani(struct ath_common *common)
  397. {
  398. unsigned long timestamp = jiffies_to_msecs(jiffies);
  399. common->ani.longcal_timer = timestamp;
  400. common->ani.shortcal_timer = timestamp;
  401. common->ani.checkani_timer = timestamp;
  402. mod_timer(&common->ani.timer,
  403. jiffies + msecs_to_jiffies(ATH_ANI_POLLINTERVAL));
  404. }
  405. /*
  406. * Update tx/rx chainmask. For legacy association,
  407. * hard code chainmask to 1x1, for 11n association, use
  408. * the chainmask configuration, for bt coexistence, use
  409. * the chainmask configuration even in legacy mode.
  410. */
  411. void ath_update_chainmask(struct ath_softc *sc, int is_ht)
  412. {
  413. struct ath_hw *ah = sc->sc_ah;
  414. struct ath_common *common = ath9k_hw_common(ah);
  415. if ((sc->sc_flags & SC_OP_SCANNING) || is_ht ||
  416. (ah->btcoex_hw.scheme != ATH_BTCOEX_CFG_NONE)) {
  417. common->tx_chainmask = ah->caps.tx_chainmask;
  418. common->rx_chainmask = ah->caps.rx_chainmask;
  419. } else {
  420. common->tx_chainmask = 1;
  421. common->rx_chainmask = 1;
  422. }
  423. ath_print(common, ATH_DBG_CONFIG,
  424. "tx chmask: %d, rx chmask: %d\n",
  425. common->tx_chainmask,
  426. common->rx_chainmask);
  427. }
  428. static void ath_node_attach(struct ath_softc *sc, struct ieee80211_sta *sta)
  429. {
  430. struct ath_node *an;
  431. an = (struct ath_node *)sta->drv_priv;
  432. if (sc->sc_flags & SC_OP_TXAGGR) {
  433. ath_tx_node_init(sc, an);
  434. an->maxampdu = 1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
  435. sta->ht_cap.ampdu_factor);
  436. an->mpdudensity = parse_mpdudensity(sta->ht_cap.ampdu_density);
  437. an->last_rssi = ATH_RSSI_DUMMY_MARKER;
  438. }
  439. }
  440. static void ath_node_detach(struct ath_softc *sc, struct ieee80211_sta *sta)
  441. {
  442. struct ath_node *an = (struct ath_node *)sta->drv_priv;
  443. if (sc->sc_flags & SC_OP_TXAGGR)
  444. ath_tx_node_cleanup(sc, an);
  445. }
  446. static void ath9k_tasklet(unsigned long data)
  447. {
  448. struct ath_softc *sc = (struct ath_softc *)data;
  449. struct ath_hw *ah = sc->sc_ah;
  450. struct ath_common *common = ath9k_hw_common(ah);
  451. u32 status = sc->intrstatus;
  452. ath9k_ps_wakeup(sc);
  453. if (status & ATH9K_INT_FATAL) {
  454. ath_reset(sc, false);
  455. ath9k_ps_restore(sc);
  456. return;
  457. }
  458. if (status & (ATH9K_INT_RX | ATH9K_INT_RXEOL | ATH9K_INT_RXORN)) {
  459. spin_lock_bh(&sc->rx.rxflushlock);
  460. ath_rx_tasklet(sc, 0);
  461. spin_unlock_bh(&sc->rx.rxflushlock);
  462. }
  463. if (status & ATH9K_INT_TX)
  464. ath_tx_tasklet(sc);
  465. if ((status & ATH9K_INT_TSFOOR) && sc->ps_enabled) {
  466. /*
  467. * TSF sync does not look correct; remain awake to sync with
  468. * the next Beacon.
  469. */
  470. ath_print(common, ATH_DBG_PS,
  471. "TSFOOR - Sync with next Beacon\n");
  472. sc->sc_flags |= SC_OP_WAIT_FOR_BEACON | SC_OP_BEACON_SYNC;
  473. }
  474. if (ah->btcoex_hw.scheme == ATH_BTCOEX_CFG_3WIRE)
  475. if (status & ATH9K_INT_GENTIMER)
  476. ath_gen_timer_isr(sc->sc_ah);
  477. /* re-enable hardware interrupt */
  478. ath9k_hw_set_interrupts(ah, sc->imask);
  479. ath9k_ps_restore(sc);
  480. }
  481. irqreturn_t ath_isr(int irq, void *dev)
  482. {
  483. #define SCHED_INTR ( \
  484. ATH9K_INT_FATAL | \
  485. ATH9K_INT_RXORN | \
  486. ATH9K_INT_RXEOL | \
  487. ATH9K_INT_RX | \
  488. ATH9K_INT_TX | \
  489. ATH9K_INT_BMISS | \
  490. ATH9K_INT_CST | \
  491. ATH9K_INT_TSFOOR | \
  492. ATH9K_INT_GENTIMER)
  493. struct ath_softc *sc = dev;
  494. struct ath_hw *ah = sc->sc_ah;
  495. enum ath9k_int status;
  496. bool sched = false;
  497. /*
  498. * The hardware is not ready/present, don't
  499. * touch anything. Note this can happen early
  500. * on if the IRQ is shared.
  501. */
  502. if (sc->sc_flags & SC_OP_INVALID)
  503. return IRQ_NONE;
  504. /* shared irq, not for us */
  505. if (!ath9k_hw_intrpend(ah))
  506. return IRQ_NONE;
  507. /*
  508. * Figure out the reason(s) for the interrupt. Note
  509. * that the hal returns a pseudo-ISR that may include
  510. * bits we haven't explicitly enabled so we mask the
  511. * value to insure we only process bits we requested.
  512. */
  513. ath9k_hw_getisr(ah, &status); /* NB: clears ISR too */
  514. status &= sc->imask; /* discard unasked-for bits */
  515. /*
  516. * If there are no status bits set, then this interrupt was not
  517. * for me (should have been caught above).
  518. */
  519. if (!status)
  520. return IRQ_NONE;
  521. /* Cache the status */
  522. sc->intrstatus = status;
  523. if (status & SCHED_INTR)
  524. sched = true;
  525. /*
  526. * If a FATAL or RXORN interrupt is received, we have to reset the
  527. * chip immediately.
  528. */
  529. if (status & (ATH9K_INT_FATAL | ATH9K_INT_RXORN))
  530. goto chip_reset;
  531. if (status & ATH9K_INT_SWBA)
  532. tasklet_schedule(&sc->bcon_tasklet);
  533. if (status & ATH9K_INT_TXURN)
  534. ath9k_hw_updatetxtriglevel(ah, true);
  535. if (status & ATH9K_INT_MIB) {
  536. /*
  537. * Disable interrupts until we service the MIB
  538. * interrupt; otherwise it will continue to
  539. * fire.
  540. */
  541. ath9k_hw_set_interrupts(ah, 0);
  542. /*
  543. * Let the hal handle the event. We assume
  544. * it will clear whatever condition caused
  545. * the interrupt.
  546. */
  547. ath9k_hw_procmibevent(ah);
  548. ath9k_hw_set_interrupts(ah, sc->imask);
  549. }
  550. if (!(ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP))
  551. if (status & ATH9K_INT_TIM_TIMER) {
  552. /* Clear RxAbort bit so that we can
  553. * receive frames */
  554. ath9k_setpower(sc, ATH9K_PM_AWAKE);
  555. ath9k_hw_setrxabort(sc->sc_ah, 0);
  556. sc->sc_flags |= SC_OP_WAIT_FOR_BEACON;
  557. }
  558. chip_reset:
  559. ath_debug_stat_interrupt(sc, status);
  560. if (sched) {
  561. /* turn off every interrupt except SWBA */
  562. ath9k_hw_set_interrupts(ah, (sc->imask & ATH9K_INT_SWBA));
  563. tasklet_schedule(&sc->intr_tq);
  564. }
  565. return IRQ_HANDLED;
  566. #undef SCHED_INTR
  567. }
  568. static u32 ath_get_extchanmode(struct ath_softc *sc,
  569. struct ieee80211_channel *chan,
  570. enum nl80211_channel_type channel_type)
  571. {
  572. u32 chanmode = 0;
  573. switch (chan->band) {
  574. case IEEE80211_BAND_2GHZ:
  575. switch(channel_type) {
  576. case NL80211_CHAN_NO_HT:
  577. case NL80211_CHAN_HT20:
  578. chanmode = CHANNEL_G_HT20;
  579. break;
  580. case NL80211_CHAN_HT40PLUS:
  581. chanmode = CHANNEL_G_HT40PLUS;
  582. break;
  583. case NL80211_CHAN_HT40MINUS:
  584. chanmode = CHANNEL_G_HT40MINUS;
  585. break;
  586. }
  587. break;
  588. case IEEE80211_BAND_5GHZ:
  589. switch(channel_type) {
  590. case NL80211_CHAN_NO_HT:
  591. case NL80211_CHAN_HT20:
  592. chanmode = CHANNEL_A_HT20;
  593. break;
  594. case NL80211_CHAN_HT40PLUS:
  595. chanmode = CHANNEL_A_HT40PLUS;
  596. break;
  597. case NL80211_CHAN_HT40MINUS:
  598. chanmode = CHANNEL_A_HT40MINUS;
  599. break;
  600. }
  601. break;
  602. default:
  603. break;
  604. }
  605. return chanmode;
  606. }
  607. static int ath_setkey_tkip(struct ath_common *common, u16 keyix, const u8 *key,
  608. struct ath9k_keyval *hk, const u8 *addr,
  609. bool authenticator)
  610. {
  611. struct ath_hw *ah = common->ah;
  612. const u8 *key_rxmic;
  613. const u8 *key_txmic;
  614. key_txmic = key + NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY;
  615. key_rxmic = key + NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY;
  616. if (addr == NULL) {
  617. /*
  618. * Group key installation - only two key cache entries are used
  619. * regardless of splitmic capability since group key is only
  620. * used either for TX or RX.
  621. */
  622. if (authenticator) {
  623. memcpy(hk->kv_mic, key_txmic, sizeof(hk->kv_mic));
  624. memcpy(hk->kv_txmic, key_txmic, sizeof(hk->kv_mic));
  625. } else {
  626. memcpy(hk->kv_mic, key_rxmic, sizeof(hk->kv_mic));
  627. memcpy(hk->kv_txmic, key_rxmic, sizeof(hk->kv_mic));
  628. }
  629. return ath9k_hw_set_keycache_entry(ah, keyix, hk, addr);
  630. }
  631. if (!common->splitmic) {
  632. /* TX and RX keys share the same key cache entry. */
  633. memcpy(hk->kv_mic, key_rxmic, sizeof(hk->kv_mic));
  634. memcpy(hk->kv_txmic, key_txmic, sizeof(hk->kv_txmic));
  635. return ath9k_hw_set_keycache_entry(ah, keyix, hk, addr);
  636. }
  637. /* Separate key cache entries for TX and RX */
  638. /* TX key goes at first index, RX key at +32. */
  639. memcpy(hk->kv_mic, key_txmic, sizeof(hk->kv_mic));
  640. if (!ath9k_hw_set_keycache_entry(ah, keyix, hk, NULL)) {
  641. /* TX MIC entry failed. No need to proceed further */
  642. ath_print(common, ATH_DBG_FATAL,
  643. "Setting TX MIC Key Failed\n");
  644. return 0;
  645. }
  646. memcpy(hk->kv_mic, key_rxmic, sizeof(hk->kv_mic));
  647. /* XXX delete tx key on failure? */
  648. return ath9k_hw_set_keycache_entry(ah, keyix + 32, hk, addr);
  649. }
  650. static int ath_reserve_key_cache_slot_tkip(struct ath_common *common)
  651. {
  652. int i;
  653. for (i = IEEE80211_WEP_NKID; i < common->keymax / 2; i++) {
  654. if (test_bit(i, common->keymap) ||
  655. test_bit(i + 64, common->keymap))
  656. continue; /* At least one part of TKIP key allocated */
  657. if (common->splitmic &&
  658. (test_bit(i + 32, common->keymap) ||
  659. test_bit(i + 64 + 32, common->keymap)))
  660. continue; /* At least one part of TKIP key allocated */
  661. /* Found a free slot for a TKIP key */
  662. return i;
  663. }
  664. return -1;
  665. }
  666. static int ath_reserve_key_cache_slot(struct ath_common *common)
  667. {
  668. int i;
  669. /* First, try to find slots that would not be available for TKIP. */
  670. if (common->splitmic) {
  671. for (i = IEEE80211_WEP_NKID; i < common->keymax / 4; i++) {
  672. if (!test_bit(i, common->keymap) &&
  673. (test_bit(i + 32, common->keymap) ||
  674. test_bit(i + 64, common->keymap) ||
  675. test_bit(i + 64 + 32, common->keymap)))
  676. return i;
  677. if (!test_bit(i + 32, common->keymap) &&
  678. (test_bit(i, common->keymap) ||
  679. test_bit(i + 64, common->keymap) ||
  680. test_bit(i + 64 + 32, common->keymap)))
  681. return i + 32;
  682. if (!test_bit(i + 64, common->keymap) &&
  683. (test_bit(i , common->keymap) ||
  684. test_bit(i + 32, common->keymap) ||
  685. test_bit(i + 64 + 32, common->keymap)))
  686. return i + 64;
  687. if (!test_bit(i + 64 + 32, common->keymap) &&
  688. (test_bit(i, common->keymap) ||
  689. test_bit(i + 32, common->keymap) ||
  690. test_bit(i + 64, common->keymap)))
  691. return i + 64 + 32;
  692. }
  693. } else {
  694. for (i = IEEE80211_WEP_NKID; i < common->keymax / 2; i++) {
  695. if (!test_bit(i, common->keymap) &&
  696. test_bit(i + 64, common->keymap))
  697. return i;
  698. if (test_bit(i, common->keymap) &&
  699. !test_bit(i + 64, common->keymap))
  700. return i + 64;
  701. }
  702. }
  703. /* No partially used TKIP slots, pick any available slot */
  704. for (i = IEEE80211_WEP_NKID; i < common->keymax; i++) {
  705. /* Do not allow slots that could be needed for TKIP group keys
  706. * to be used. This limitation could be removed if we know that
  707. * TKIP will not be used. */
  708. if (i >= 64 && i < 64 + IEEE80211_WEP_NKID)
  709. continue;
  710. if (common->splitmic) {
  711. if (i >= 32 && i < 32 + IEEE80211_WEP_NKID)
  712. continue;
  713. if (i >= 64 + 32 && i < 64 + 32 + IEEE80211_WEP_NKID)
  714. continue;
  715. }
  716. if (!test_bit(i, common->keymap))
  717. return i; /* Found a free slot for a key */
  718. }
  719. /* No free slot found */
  720. return -1;
  721. }
  722. static int ath_key_config(struct ath_common *common,
  723. struct ieee80211_vif *vif,
  724. struct ieee80211_sta *sta,
  725. struct ieee80211_key_conf *key)
  726. {
  727. struct ath_hw *ah = common->ah;
  728. struct ath9k_keyval hk;
  729. const u8 *mac = NULL;
  730. int ret = 0;
  731. int idx;
  732. memset(&hk, 0, sizeof(hk));
  733. switch (key->alg) {
  734. case ALG_WEP:
  735. hk.kv_type = ATH9K_CIPHER_WEP;
  736. break;
  737. case ALG_TKIP:
  738. hk.kv_type = ATH9K_CIPHER_TKIP;
  739. break;
  740. case ALG_CCMP:
  741. hk.kv_type = ATH9K_CIPHER_AES_CCM;
  742. break;
  743. default:
  744. return -EOPNOTSUPP;
  745. }
  746. hk.kv_len = key->keylen;
  747. memcpy(hk.kv_val, key->key, key->keylen);
  748. if (!(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
  749. /* For now, use the default keys for broadcast keys. This may
  750. * need to change with virtual interfaces. */
  751. idx = key->keyidx;
  752. } else if (key->keyidx) {
  753. if (WARN_ON(!sta))
  754. return -EOPNOTSUPP;
  755. mac = sta->addr;
  756. if (vif->type != NL80211_IFTYPE_AP) {
  757. /* Only keyidx 0 should be used with unicast key, but
  758. * allow this for client mode for now. */
  759. idx = key->keyidx;
  760. } else
  761. return -EIO;
  762. } else {
  763. if (WARN_ON(!sta))
  764. return -EOPNOTSUPP;
  765. mac = sta->addr;
  766. if (key->alg == ALG_TKIP)
  767. idx = ath_reserve_key_cache_slot_tkip(common);
  768. else
  769. idx = ath_reserve_key_cache_slot(common);
  770. if (idx < 0)
  771. return -ENOSPC; /* no free key cache entries */
  772. }
  773. if (key->alg == ALG_TKIP)
  774. ret = ath_setkey_tkip(common, idx, key->key, &hk, mac,
  775. vif->type == NL80211_IFTYPE_AP);
  776. else
  777. ret = ath9k_hw_set_keycache_entry(ah, idx, &hk, mac);
  778. if (!ret)
  779. return -EIO;
  780. set_bit(idx, common->keymap);
  781. if (key->alg == ALG_TKIP) {
  782. set_bit(idx + 64, common->keymap);
  783. if (common->splitmic) {
  784. set_bit(idx + 32, common->keymap);
  785. set_bit(idx + 64 + 32, common->keymap);
  786. }
  787. }
  788. return idx;
  789. }
  790. static void ath_key_delete(struct ath_common *common, struct ieee80211_key_conf *key)
  791. {
  792. struct ath_hw *ah = common->ah;
  793. ath9k_hw_keyreset(ah, key->hw_key_idx);
  794. if (key->hw_key_idx < IEEE80211_WEP_NKID)
  795. return;
  796. clear_bit(key->hw_key_idx, common->keymap);
  797. if (key->alg != ALG_TKIP)
  798. return;
  799. clear_bit(key->hw_key_idx + 64, common->keymap);
  800. if (common->splitmic) {
  801. clear_bit(key->hw_key_idx + 32, common->keymap);
  802. clear_bit(key->hw_key_idx + 64 + 32, common->keymap);
  803. }
  804. }
  805. static void setup_ht_cap(struct ath_softc *sc,
  806. struct ieee80211_sta_ht_cap *ht_info)
  807. {
  808. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  809. u8 tx_streams, rx_streams;
  810. ht_info->ht_supported = true;
  811. ht_info->cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
  812. IEEE80211_HT_CAP_SM_PS |
  813. IEEE80211_HT_CAP_SGI_40 |
  814. IEEE80211_HT_CAP_DSSSCCK40;
  815. ht_info->ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
  816. ht_info->ampdu_density = IEEE80211_HT_MPDU_DENSITY_8;
  817. /* set up supported mcs set */
  818. memset(&ht_info->mcs, 0, sizeof(ht_info->mcs));
  819. tx_streams = !(common->tx_chainmask & (common->tx_chainmask - 1)) ?
  820. 1 : 2;
  821. rx_streams = !(common->rx_chainmask & (common->rx_chainmask - 1)) ?
  822. 1 : 2;
  823. if (tx_streams != rx_streams) {
  824. ath_print(common, ATH_DBG_CONFIG,
  825. "TX streams %d, RX streams: %d\n",
  826. tx_streams, rx_streams);
  827. ht_info->mcs.tx_params |= IEEE80211_HT_MCS_TX_RX_DIFF;
  828. ht_info->mcs.tx_params |= ((tx_streams - 1) <<
  829. IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT);
  830. }
  831. ht_info->mcs.rx_mask[0] = 0xff;
  832. if (rx_streams >= 2)
  833. ht_info->mcs.rx_mask[1] = 0xff;
  834. ht_info->mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
  835. }
  836. static void ath9k_bss_assoc_info(struct ath_softc *sc,
  837. struct ieee80211_vif *vif,
  838. struct ieee80211_bss_conf *bss_conf)
  839. {
  840. struct ath_hw *ah = sc->sc_ah;
  841. struct ath_common *common = ath9k_hw_common(ah);
  842. if (bss_conf->assoc) {
  843. ath_print(common, ATH_DBG_CONFIG,
  844. "Bss Info ASSOC %d, bssid: %pM\n",
  845. bss_conf->aid, common->curbssid);
  846. /* New association, store aid */
  847. common->curaid = bss_conf->aid;
  848. ath9k_hw_write_associd(ah);
  849. /*
  850. * Request a re-configuration of Beacon related timers
  851. * on the receipt of the first Beacon frame (i.e.,
  852. * after time sync with the AP).
  853. */
  854. sc->sc_flags |= SC_OP_BEACON_SYNC;
  855. /* Configure the beacon */
  856. ath_beacon_config(sc, vif);
  857. /* Reset rssi stats */
  858. sc->sc_ah->stats.avgbrssi = ATH_RSSI_DUMMY_MARKER;
  859. ath_start_ani(common);
  860. } else {
  861. ath_print(common, ATH_DBG_CONFIG, "Bss Info DISASSOC\n");
  862. common->curaid = 0;
  863. /* Stop ANI */
  864. del_timer_sync(&common->ani.timer);
  865. }
  866. }
  867. /********************************/
  868. /* LED functions */
  869. /********************************/
  870. static void ath_led_blink_work(struct work_struct *work)
  871. {
  872. struct ath_softc *sc = container_of(work, struct ath_softc,
  873. ath_led_blink_work.work);
  874. if (!(sc->sc_flags & SC_OP_LED_ASSOCIATED))
  875. return;
  876. if ((sc->led_on_duration == ATH_LED_ON_DURATION_IDLE) ||
  877. (sc->led_off_duration == ATH_LED_OFF_DURATION_IDLE))
  878. ath9k_hw_set_gpio(sc->sc_ah, sc->sc_ah->led_pin, 0);
  879. else
  880. ath9k_hw_set_gpio(sc->sc_ah, sc->sc_ah->led_pin,
  881. (sc->sc_flags & SC_OP_LED_ON) ? 1 : 0);
  882. ieee80211_queue_delayed_work(sc->hw,
  883. &sc->ath_led_blink_work,
  884. (sc->sc_flags & SC_OP_LED_ON) ?
  885. msecs_to_jiffies(sc->led_off_duration) :
  886. msecs_to_jiffies(sc->led_on_duration));
  887. sc->led_on_duration = sc->led_on_cnt ?
  888. max((ATH_LED_ON_DURATION_IDLE - sc->led_on_cnt), 25) :
  889. ATH_LED_ON_DURATION_IDLE;
  890. sc->led_off_duration = sc->led_off_cnt ?
  891. max((ATH_LED_OFF_DURATION_IDLE - sc->led_off_cnt), 10) :
  892. ATH_LED_OFF_DURATION_IDLE;
  893. sc->led_on_cnt = sc->led_off_cnt = 0;
  894. if (sc->sc_flags & SC_OP_LED_ON)
  895. sc->sc_flags &= ~SC_OP_LED_ON;
  896. else
  897. sc->sc_flags |= SC_OP_LED_ON;
  898. }
  899. static void ath_led_brightness(struct led_classdev *led_cdev,
  900. enum led_brightness brightness)
  901. {
  902. struct ath_led *led = container_of(led_cdev, struct ath_led, led_cdev);
  903. struct ath_softc *sc = led->sc;
  904. switch (brightness) {
  905. case LED_OFF:
  906. if (led->led_type == ATH_LED_ASSOC ||
  907. led->led_type == ATH_LED_RADIO) {
  908. ath9k_hw_set_gpio(sc->sc_ah, sc->sc_ah->led_pin,
  909. (led->led_type == ATH_LED_RADIO));
  910. sc->sc_flags &= ~SC_OP_LED_ASSOCIATED;
  911. if (led->led_type == ATH_LED_RADIO)
  912. sc->sc_flags &= ~SC_OP_LED_ON;
  913. } else {
  914. sc->led_off_cnt++;
  915. }
  916. break;
  917. case LED_FULL:
  918. if (led->led_type == ATH_LED_ASSOC) {
  919. sc->sc_flags |= SC_OP_LED_ASSOCIATED;
  920. ieee80211_queue_delayed_work(sc->hw,
  921. &sc->ath_led_blink_work, 0);
  922. } else if (led->led_type == ATH_LED_RADIO) {
  923. ath9k_hw_set_gpio(sc->sc_ah, sc->sc_ah->led_pin, 0);
  924. sc->sc_flags |= SC_OP_LED_ON;
  925. } else {
  926. sc->led_on_cnt++;
  927. }
  928. break;
  929. default:
  930. break;
  931. }
  932. }
  933. static int ath_register_led(struct ath_softc *sc, struct ath_led *led,
  934. char *trigger)
  935. {
  936. int ret;
  937. led->sc = sc;
  938. led->led_cdev.name = led->name;
  939. led->led_cdev.default_trigger = trigger;
  940. led->led_cdev.brightness_set = ath_led_brightness;
  941. ret = led_classdev_register(wiphy_dev(sc->hw->wiphy), &led->led_cdev);
  942. if (ret)
  943. ath_print(ath9k_hw_common(sc->sc_ah), ATH_DBG_FATAL,
  944. "Failed to register led:%s", led->name);
  945. else
  946. led->registered = 1;
  947. return ret;
  948. }
  949. static void ath_unregister_led(struct ath_led *led)
  950. {
  951. if (led->registered) {
  952. led_classdev_unregister(&led->led_cdev);
  953. led->registered = 0;
  954. }
  955. }
  956. static void ath_deinit_leds(struct ath_softc *sc)
  957. {
  958. ath_unregister_led(&sc->assoc_led);
  959. sc->sc_flags &= ~SC_OP_LED_ASSOCIATED;
  960. ath_unregister_led(&sc->tx_led);
  961. ath_unregister_led(&sc->rx_led);
  962. ath_unregister_led(&sc->radio_led);
  963. ath9k_hw_set_gpio(sc->sc_ah, sc->sc_ah->led_pin, 1);
  964. }
  965. static void ath_init_leds(struct ath_softc *sc)
  966. {
  967. char *trigger;
  968. int ret;
  969. if (AR_SREV_9287(sc->sc_ah))
  970. sc->sc_ah->led_pin = ATH_LED_PIN_9287;
  971. else
  972. sc->sc_ah->led_pin = ATH_LED_PIN_DEF;
  973. /* Configure gpio 1 for output */
  974. ath9k_hw_cfg_output(sc->sc_ah, sc->sc_ah->led_pin,
  975. AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
  976. /* LED off, active low */
  977. ath9k_hw_set_gpio(sc->sc_ah, sc->sc_ah->led_pin, 1);
  978. INIT_DELAYED_WORK(&sc->ath_led_blink_work, ath_led_blink_work);
  979. trigger = ieee80211_get_radio_led_name(sc->hw);
  980. snprintf(sc->radio_led.name, sizeof(sc->radio_led.name),
  981. "ath9k-%s::radio", wiphy_name(sc->hw->wiphy));
  982. ret = ath_register_led(sc, &sc->radio_led, trigger);
  983. sc->radio_led.led_type = ATH_LED_RADIO;
  984. if (ret)
  985. goto fail;
  986. trigger = ieee80211_get_assoc_led_name(sc->hw);
  987. snprintf(sc->assoc_led.name, sizeof(sc->assoc_led.name),
  988. "ath9k-%s::assoc", wiphy_name(sc->hw->wiphy));
  989. ret = ath_register_led(sc, &sc->assoc_led, trigger);
  990. sc->assoc_led.led_type = ATH_LED_ASSOC;
  991. if (ret)
  992. goto fail;
  993. trigger = ieee80211_get_tx_led_name(sc->hw);
  994. snprintf(sc->tx_led.name, sizeof(sc->tx_led.name),
  995. "ath9k-%s::tx", wiphy_name(sc->hw->wiphy));
  996. ret = ath_register_led(sc, &sc->tx_led, trigger);
  997. sc->tx_led.led_type = ATH_LED_TX;
  998. if (ret)
  999. goto fail;
  1000. trigger = ieee80211_get_rx_led_name(sc->hw);
  1001. snprintf(sc->rx_led.name, sizeof(sc->rx_led.name),
  1002. "ath9k-%s::rx", wiphy_name(sc->hw->wiphy));
  1003. ret = ath_register_led(sc, &sc->rx_led, trigger);
  1004. sc->rx_led.led_type = ATH_LED_RX;
  1005. if (ret)
  1006. goto fail;
  1007. return;
  1008. fail:
  1009. cancel_delayed_work_sync(&sc->ath_led_blink_work);
  1010. ath_deinit_leds(sc);
  1011. }
  1012. void ath_radio_enable(struct ath_softc *sc, struct ieee80211_hw *hw)
  1013. {
  1014. struct ath_hw *ah = sc->sc_ah;
  1015. struct ath_common *common = ath9k_hw_common(ah);
  1016. struct ieee80211_channel *channel = hw->conf.channel;
  1017. int r;
  1018. ath9k_ps_wakeup(sc);
  1019. ath9k_hw_configpcipowersave(ah, 0, 0);
  1020. if (!ah->curchan)
  1021. ah->curchan = ath_get_curchannel(sc, sc->hw);
  1022. spin_lock_bh(&sc->sc_resetlock);
  1023. r = ath9k_hw_reset(ah, ah->curchan, false);
  1024. if (r) {
  1025. ath_print(common, ATH_DBG_FATAL,
  1026. "Unable to reset channel %u (%uMhz) ",
  1027. "reset status %d\n",
  1028. channel->center_freq, r);
  1029. }
  1030. spin_unlock_bh(&sc->sc_resetlock);
  1031. ath_update_txpow(sc);
  1032. if (ath_startrecv(sc) != 0) {
  1033. ath_print(common, ATH_DBG_FATAL,
  1034. "Unable to restart recv logic\n");
  1035. return;
  1036. }
  1037. if (sc->sc_flags & SC_OP_BEACONS)
  1038. ath_beacon_config(sc, NULL); /* restart beacons */
  1039. /* Re-Enable interrupts */
  1040. ath9k_hw_set_interrupts(ah, sc->imask);
  1041. /* Enable LED */
  1042. ath9k_hw_cfg_output(ah, ah->led_pin,
  1043. AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
  1044. ath9k_hw_set_gpio(ah, ah->led_pin, 0);
  1045. ieee80211_wake_queues(hw);
  1046. ath9k_ps_restore(sc);
  1047. }
  1048. void ath_radio_disable(struct ath_softc *sc, struct ieee80211_hw *hw)
  1049. {
  1050. struct ath_hw *ah = sc->sc_ah;
  1051. struct ieee80211_channel *channel = hw->conf.channel;
  1052. int r;
  1053. ath9k_ps_wakeup(sc);
  1054. ieee80211_stop_queues(hw);
  1055. /* Disable LED */
  1056. ath9k_hw_set_gpio(ah, ah->led_pin, 1);
  1057. ath9k_hw_cfg_gpio_input(ah, ah->led_pin);
  1058. /* Disable interrupts */
  1059. ath9k_hw_set_interrupts(ah, 0);
  1060. ath_drain_all_txq(sc, false); /* clear pending tx frames */
  1061. ath_stoprecv(sc); /* turn off frame recv */
  1062. ath_flushrecv(sc); /* flush recv queue */
  1063. if (!ah->curchan)
  1064. ah->curchan = ath_get_curchannel(sc, hw);
  1065. spin_lock_bh(&sc->sc_resetlock);
  1066. r = ath9k_hw_reset(ah, ah->curchan, false);
  1067. if (r) {
  1068. ath_print(ath9k_hw_common(sc->sc_ah), ATH_DBG_FATAL,
  1069. "Unable to reset channel %u (%uMhz) "
  1070. "reset status %d\n",
  1071. channel->center_freq, r);
  1072. }
  1073. spin_unlock_bh(&sc->sc_resetlock);
  1074. ath9k_hw_phy_disable(ah);
  1075. ath9k_hw_configpcipowersave(ah, 1, 1);
  1076. ath9k_ps_restore(sc);
  1077. ath9k_setpower(sc, ATH9K_PM_FULL_SLEEP);
  1078. }
  1079. /*******************/
  1080. /* Rfkill */
  1081. /*******************/
  1082. static bool ath_is_rfkill_set(struct ath_softc *sc)
  1083. {
  1084. struct ath_hw *ah = sc->sc_ah;
  1085. return ath9k_hw_gpio_get(ah, ah->rfkill_gpio) ==
  1086. ah->rfkill_polarity;
  1087. }
  1088. static void ath9k_rfkill_poll_state(struct ieee80211_hw *hw)
  1089. {
  1090. struct ath_wiphy *aphy = hw->priv;
  1091. struct ath_softc *sc = aphy->sc;
  1092. bool blocked = !!ath_is_rfkill_set(sc);
  1093. wiphy_rfkill_set_hw_state(hw->wiphy, blocked);
  1094. }
  1095. static void ath_start_rfkill_poll(struct ath_softc *sc)
  1096. {
  1097. struct ath_hw *ah = sc->sc_ah;
  1098. if (ah->caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
  1099. wiphy_rfkill_start_polling(sc->hw->wiphy);
  1100. }
  1101. static void ath9k_uninit_hw(struct ath_softc *sc)
  1102. {
  1103. struct ath_hw *ah = sc->sc_ah;
  1104. BUG_ON(!ah);
  1105. ath9k_exit_debug(ah);
  1106. ath9k_hw_detach(ah);
  1107. sc->sc_ah = NULL;
  1108. }
  1109. static void ath_clean_core(struct ath_softc *sc)
  1110. {
  1111. struct ieee80211_hw *hw = sc->hw;
  1112. struct ath_hw *ah = sc->sc_ah;
  1113. int i = 0;
  1114. ath9k_ps_wakeup(sc);
  1115. dev_dbg(sc->dev, "Detach ATH hw\n");
  1116. ath_deinit_leds(sc);
  1117. wiphy_rfkill_stop_polling(sc->hw->wiphy);
  1118. for (i = 0; i < sc->num_sec_wiphy; i++) {
  1119. struct ath_wiphy *aphy = sc->sec_wiphy[i];
  1120. if (aphy == NULL)
  1121. continue;
  1122. sc->sec_wiphy[i] = NULL;
  1123. ieee80211_unregister_hw(aphy->hw);
  1124. ieee80211_free_hw(aphy->hw);
  1125. }
  1126. ieee80211_unregister_hw(hw);
  1127. ath_rx_cleanup(sc);
  1128. ath_tx_cleanup(sc);
  1129. tasklet_kill(&sc->intr_tq);
  1130. tasklet_kill(&sc->bcon_tasklet);
  1131. if (!(sc->sc_flags & SC_OP_INVALID))
  1132. ath9k_setpower(sc, ATH9K_PM_AWAKE);
  1133. /* cleanup tx queues */
  1134. for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++)
  1135. if (ATH_TXQ_SETUP(sc, i))
  1136. ath_tx_cleanupq(sc, &sc->tx.txq[i]);
  1137. if ((sc->btcoex.no_stomp_timer) &&
  1138. ah->btcoex_hw.scheme == ATH_BTCOEX_CFG_3WIRE)
  1139. ath_gen_timer_free(ah, sc->btcoex.no_stomp_timer);
  1140. }
  1141. void ath_detach(struct ath_softc *sc)
  1142. {
  1143. ath_clean_core(sc);
  1144. ath9k_uninit_hw(sc);
  1145. }
  1146. void ath_cleanup(struct ath_softc *sc)
  1147. {
  1148. struct ath_hw *ah = sc->sc_ah;
  1149. struct ath_common *common = ath9k_hw_common(ah);
  1150. ath_clean_core(sc);
  1151. free_irq(sc->irq, sc);
  1152. ath_bus_cleanup(common);
  1153. kfree(sc->sec_wiphy);
  1154. ieee80211_free_hw(sc->hw);
  1155. ath9k_uninit_hw(sc);
  1156. }
  1157. static int ath9k_reg_notifier(struct wiphy *wiphy,
  1158. struct regulatory_request *request)
  1159. {
  1160. struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
  1161. struct ath_wiphy *aphy = hw->priv;
  1162. struct ath_softc *sc = aphy->sc;
  1163. struct ath_regulatory *reg = ath9k_hw_regulatory(sc->sc_ah);
  1164. return ath_reg_notifier_apply(wiphy, request, reg);
  1165. }
  1166. /*
  1167. * Detects if there is any priority bt traffic
  1168. */
  1169. static void ath_detect_bt_priority(struct ath_softc *sc)
  1170. {
  1171. struct ath_btcoex *btcoex = &sc->btcoex;
  1172. struct ath_hw *ah = sc->sc_ah;
  1173. if (ath9k_hw_gpio_get(sc->sc_ah, ah->btcoex_hw.btpriority_gpio))
  1174. btcoex->bt_priority_cnt++;
  1175. if (time_after(jiffies, btcoex->bt_priority_time +
  1176. msecs_to_jiffies(ATH_BT_PRIORITY_TIME_THRESHOLD))) {
  1177. if (btcoex->bt_priority_cnt >= ATH_BT_CNT_THRESHOLD) {
  1178. ath_print(ath9k_hw_common(sc->sc_ah), ATH_DBG_BTCOEX,
  1179. "BT priority traffic detected");
  1180. sc->sc_flags |= SC_OP_BT_PRIORITY_DETECTED;
  1181. } else {
  1182. sc->sc_flags &= ~SC_OP_BT_PRIORITY_DETECTED;
  1183. }
  1184. btcoex->bt_priority_cnt = 0;
  1185. btcoex->bt_priority_time = jiffies;
  1186. }
  1187. }
  1188. /*
  1189. * Configures appropriate weight based on stomp type.
  1190. */
  1191. static void ath9k_btcoex_bt_stomp(struct ath_softc *sc,
  1192. enum ath_stomp_type stomp_type)
  1193. {
  1194. struct ath_hw *ah = sc->sc_ah;
  1195. switch (stomp_type) {
  1196. case ATH_BTCOEX_STOMP_ALL:
  1197. ath9k_hw_btcoex_set_weight(ah, AR_BT_COEX_WGHT,
  1198. AR_STOMP_ALL_WLAN_WGHT);
  1199. break;
  1200. case ATH_BTCOEX_STOMP_LOW:
  1201. ath9k_hw_btcoex_set_weight(ah, AR_BT_COEX_WGHT,
  1202. AR_STOMP_LOW_WLAN_WGHT);
  1203. break;
  1204. case ATH_BTCOEX_STOMP_NONE:
  1205. ath9k_hw_btcoex_set_weight(ah, AR_BT_COEX_WGHT,
  1206. AR_STOMP_NONE_WLAN_WGHT);
  1207. break;
  1208. default:
  1209. ath_print(ath9k_hw_common(ah), ATH_DBG_BTCOEX,
  1210. "Invalid Stomptype\n");
  1211. break;
  1212. }
  1213. ath9k_hw_btcoex_enable(ah);
  1214. }
  1215. static void ath9k_gen_timer_start(struct ath_hw *ah,
  1216. struct ath_gen_timer *timer,
  1217. u32 timer_next,
  1218. u32 timer_period)
  1219. {
  1220. struct ath_common *common = ath9k_hw_common(ah);
  1221. struct ath_softc *sc = (struct ath_softc *) common->priv;
  1222. ath9k_hw_gen_timer_start(ah, timer, timer_next, timer_period);
  1223. if ((sc->imask & ATH9K_INT_GENTIMER) == 0) {
  1224. ath9k_hw_set_interrupts(ah, 0);
  1225. sc->imask |= ATH9K_INT_GENTIMER;
  1226. ath9k_hw_set_interrupts(ah, sc->imask);
  1227. }
  1228. }
  1229. static void ath9k_gen_timer_stop(struct ath_hw *ah, struct ath_gen_timer *timer)
  1230. {
  1231. struct ath_common *common = ath9k_hw_common(ah);
  1232. struct ath_softc *sc = (struct ath_softc *) common->priv;
  1233. struct ath_gen_timer_table *timer_table = &ah->hw_gen_timers;
  1234. ath9k_hw_gen_timer_stop(ah, timer);
  1235. /* if no timer is enabled, turn off interrupt mask */
  1236. if (timer_table->timer_mask.val == 0) {
  1237. ath9k_hw_set_interrupts(ah, 0);
  1238. sc->imask &= ~ATH9K_INT_GENTIMER;
  1239. ath9k_hw_set_interrupts(ah, sc->imask);
  1240. }
  1241. }
  1242. /*
  1243. * This is the master bt coex timer which runs for every
  1244. * 45ms, bt traffic will be given priority during 55% of this
  1245. * period while wlan gets remaining 45%
  1246. */
  1247. static void ath_btcoex_period_timer(unsigned long data)
  1248. {
  1249. struct ath_softc *sc = (struct ath_softc *) data;
  1250. struct ath_hw *ah = sc->sc_ah;
  1251. struct ath_btcoex *btcoex = &sc->btcoex;
  1252. ath_detect_bt_priority(sc);
  1253. spin_lock_bh(&btcoex->btcoex_lock);
  1254. ath9k_btcoex_bt_stomp(sc, btcoex->bt_stomp_type);
  1255. spin_unlock_bh(&btcoex->btcoex_lock);
  1256. if (btcoex->btcoex_period != btcoex->btcoex_no_stomp) {
  1257. if (btcoex->hw_timer_enabled)
  1258. ath9k_gen_timer_stop(ah, btcoex->no_stomp_timer);
  1259. ath9k_gen_timer_start(ah,
  1260. btcoex->no_stomp_timer,
  1261. (ath9k_hw_gettsf32(ah) +
  1262. btcoex->btcoex_no_stomp),
  1263. btcoex->btcoex_no_stomp * 10);
  1264. btcoex->hw_timer_enabled = true;
  1265. }
  1266. mod_timer(&btcoex->period_timer, jiffies +
  1267. msecs_to_jiffies(ATH_BTCOEX_DEF_BT_PERIOD));
  1268. }
  1269. /*
  1270. * Generic tsf based hw timer which configures weight
  1271. * registers to time slice between wlan and bt traffic
  1272. */
  1273. static void ath_btcoex_no_stomp_timer(void *arg)
  1274. {
  1275. struct ath_softc *sc = (struct ath_softc *)arg;
  1276. struct ath_hw *ah = sc->sc_ah;
  1277. struct ath_btcoex *btcoex = &sc->btcoex;
  1278. ath_print(ath9k_hw_common(ah), ATH_DBG_BTCOEX,
  1279. "no stomp timer running \n");
  1280. spin_lock_bh(&btcoex->btcoex_lock);
  1281. if (btcoex->bt_stomp_type == ATH_BTCOEX_STOMP_LOW)
  1282. ath9k_btcoex_bt_stomp(sc, ATH_BTCOEX_STOMP_NONE);
  1283. else if (btcoex->bt_stomp_type == ATH_BTCOEX_STOMP_ALL)
  1284. ath9k_btcoex_bt_stomp(sc, ATH_BTCOEX_STOMP_LOW);
  1285. spin_unlock_bh(&btcoex->btcoex_lock);
  1286. }
  1287. static int ath_init_btcoex_timer(struct ath_softc *sc)
  1288. {
  1289. struct ath_btcoex *btcoex = &sc->btcoex;
  1290. btcoex->btcoex_period = ATH_BTCOEX_DEF_BT_PERIOD * 1000;
  1291. btcoex->btcoex_no_stomp = (100 - ATH_BTCOEX_DEF_DUTY_CYCLE) *
  1292. btcoex->btcoex_period / 100;
  1293. setup_timer(&btcoex->period_timer, ath_btcoex_period_timer,
  1294. (unsigned long) sc);
  1295. spin_lock_init(&btcoex->btcoex_lock);
  1296. btcoex->no_stomp_timer = ath_gen_timer_alloc(sc->sc_ah,
  1297. ath_btcoex_no_stomp_timer,
  1298. ath_btcoex_no_stomp_timer,
  1299. (void *) sc, AR_FIRST_NDP_TIMER);
  1300. if (!btcoex->no_stomp_timer)
  1301. return -ENOMEM;
  1302. return 0;
  1303. }
  1304. /*
  1305. * Read and write, they both share the same lock. We do this to serialize
  1306. * reads and writes on Atheros 802.11n PCI devices only. This is required
  1307. * as the FIFO on these devices can only accept sanely 2 requests. After
  1308. * that the device goes bananas. Serializing the reads/writes prevents this
  1309. * from happening.
  1310. */
  1311. static void ath9k_iowrite32(void *hw_priv, u32 val, u32 reg_offset)
  1312. {
  1313. struct ath_hw *ah = (struct ath_hw *) hw_priv;
  1314. struct ath_common *common = ath9k_hw_common(ah);
  1315. struct ath_softc *sc = (struct ath_softc *) common->priv;
  1316. if (ah->config.serialize_regmode == SER_REG_MODE_ON) {
  1317. unsigned long flags;
  1318. spin_lock_irqsave(&sc->sc_serial_rw, flags);
  1319. iowrite32(val, sc->mem + reg_offset);
  1320. spin_unlock_irqrestore(&sc->sc_serial_rw, flags);
  1321. } else
  1322. iowrite32(val, sc->mem + reg_offset);
  1323. }
  1324. static unsigned int ath9k_ioread32(void *hw_priv, u32 reg_offset)
  1325. {
  1326. struct ath_hw *ah = (struct ath_hw *) hw_priv;
  1327. struct ath_common *common = ath9k_hw_common(ah);
  1328. struct ath_softc *sc = (struct ath_softc *) common->priv;
  1329. u32 val;
  1330. if (ah->config.serialize_regmode == SER_REG_MODE_ON) {
  1331. unsigned long flags;
  1332. spin_lock_irqsave(&sc->sc_serial_rw, flags);
  1333. val = ioread32(sc->mem + reg_offset);
  1334. spin_unlock_irqrestore(&sc->sc_serial_rw, flags);
  1335. } else
  1336. val = ioread32(sc->mem + reg_offset);
  1337. return val;
  1338. }
  1339. static const struct ath_ops ath9k_common_ops = {
  1340. .read = ath9k_ioread32,
  1341. .write = ath9k_iowrite32,
  1342. };
  1343. /*
  1344. * Initialize and fill ath_softc, ath_sofct is the
  1345. * "Software Carrier" struct. Historically it has existed
  1346. * to allow the separation between hardware specific
  1347. * variables (now in ath_hw) and driver specific variables.
  1348. */
  1349. static int ath_init_softc(u16 devid, struct ath_softc *sc, u16 subsysid,
  1350. const struct ath_bus_ops *bus_ops)
  1351. {
  1352. struct ath_hw *ah = NULL;
  1353. struct ath_common *common;
  1354. int r = 0, i;
  1355. int csz = 0;
  1356. int qnum;
  1357. /* XXX: hardware will not be ready until ath_open() being called */
  1358. sc->sc_flags |= SC_OP_INVALID;
  1359. spin_lock_init(&sc->wiphy_lock);
  1360. spin_lock_init(&sc->sc_resetlock);
  1361. spin_lock_init(&sc->sc_serial_rw);
  1362. spin_lock_init(&sc->ani_lock);
  1363. spin_lock_init(&sc->sc_pm_lock);
  1364. mutex_init(&sc->mutex);
  1365. tasklet_init(&sc->intr_tq, ath9k_tasklet, (unsigned long)sc);
  1366. tasklet_init(&sc->bcon_tasklet, ath_beacon_tasklet,
  1367. (unsigned long)sc);
  1368. ah = kzalloc(sizeof(struct ath_hw), GFP_KERNEL);
  1369. if (!ah)
  1370. return -ENOMEM;
  1371. ah->hw_version.devid = devid;
  1372. ah->hw_version.subsysid = subsysid;
  1373. sc->sc_ah = ah;
  1374. common = ath9k_hw_common(ah);
  1375. common->ops = &ath9k_common_ops;
  1376. common->bus_ops = bus_ops;
  1377. common->ah = ah;
  1378. common->hw = sc->hw;
  1379. common->priv = sc;
  1380. common->debug_mask = ath9k_debug;
  1381. /*
  1382. * Cache line size is used to size and align various
  1383. * structures used to communicate with the hardware.
  1384. */
  1385. ath_read_cachesize(common, &csz);
  1386. /* XXX assert csz is non-zero */
  1387. common->cachelsz = csz << 2; /* convert to bytes */
  1388. r = ath9k_hw_init(ah);
  1389. if (r) {
  1390. ath_print(common, ATH_DBG_FATAL,
  1391. "Unable to initialize hardware; "
  1392. "initialization status: %d\n", r);
  1393. goto bad_free_hw;
  1394. }
  1395. if (ath9k_init_debug(ah) < 0) {
  1396. ath_print(common, ATH_DBG_FATAL,
  1397. "Unable to create debugfs files\n");
  1398. goto bad_free_hw;
  1399. }
  1400. /* Get the hardware key cache size. */
  1401. common->keymax = ah->caps.keycache_size;
  1402. if (common->keymax > ATH_KEYMAX) {
  1403. ath_print(common, ATH_DBG_ANY,
  1404. "Warning, using only %u entries in %u key cache\n",
  1405. ATH_KEYMAX, common->keymax);
  1406. common->keymax = ATH_KEYMAX;
  1407. }
  1408. /*
  1409. * Reset the key cache since some parts do not
  1410. * reset the contents on initial power up.
  1411. */
  1412. for (i = 0; i < common->keymax; i++)
  1413. ath9k_hw_keyreset(ah, (u16) i);
  1414. /* default to MONITOR mode */
  1415. sc->sc_ah->opmode = NL80211_IFTYPE_MONITOR;
  1416. /*
  1417. * Allocate hardware transmit queues: one queue for
  1418. * beacon frames and one data queue for each QoS
  1419. * priority. Note that the hal handles reseting
  1420. * these queues at the needed time.
  1421. */
  1422. sc->beacon.beaconq = ath9k_hw_beaconq_setup(ah);
  1423. if (sc->beacon.beaconq == -1) {
  1424. ath_print(common, ATH_DBG_FATAL,
  1425. "Unable to setup a beacon xmit queue\n");
  1426. r = -EIO;
  1427. goto bad2;
  1428. }
  1429. sc->beacon.cabq = ath_txq_setup(sc, ATH9K_TX_QUEUE_CAB, 0);
  1430. if (sc->beacon.cabq == NULL) {
  1431. ath_print(common, ATH_DBG_FATAL,
  1432. "Unable to setup CAB xmit queue\n");
  1433. r = -EIO;
  1434. goto bad2;
  1435. }
  1436. sc->config.cabqReadytime = ATH_CABQ_READY_TIME;
  1437. ath_cabq_update(sc);
  1438. for (i = 0; i < ARRAY_SIZE(sc->tx.hwq_map); i++)
  1439. sc->tx.hwq_map[i] = -1;
  1440. /* Setup data queues */
  1441. /* NB: ensure BK queue is the lowest priority h/w queue */
  1442. if (!ath_tx_setup(sc, ATH9K_WME_AC_BK)) {
  1443. ath_print(common, ATH_DBG_FATAL,
  1444. "Unable to setup xmit queue for BK traffic\n");
  1445. r = -EIO;
  1446. goto bad2;
  1447. }
  1448. if (!ath_tx_setup(sc, ATH9K_WME_AC_BE)) {
  1449. ath_print(common, ATH_DBG_FATAL,
  1450. "Unable to setup xmit queue for BE traffic\n");
  1451. r = -EIO;
  1452. goto bad2;
  1453. }
  1454. if (!ath_tx_setup(sc, ATH9K_WME_AC_VI)) {
  1455. ath_print(common, ATH_DBG_FATAL,
  1456. "Unable to setup xmit queue for VI traffic\n");
  1457. r = -EIO;
  1458. goto bad2;
  1459. }
  1460. if (!ath_tx_setup(sc, ATH9K_WME_AC_VO)) {
  1461. ath_print(common, ATH_DBG_FATAL,
  1462. "Unable to setup xmit queue for VO traffic\n");
  1463. r = -EIO;
  1464. goto bad2;
  1465. }
  1466. /* Initializes the noise floor to a reasonable default value.
  1467. * Later on this will be updated during ANI processing. */
  1468. common->ani.noise_floor = ATH_DEFAULT_NOISE_FLOOR;
  1469. setup_timer(&common->ani.timer, ath_ani_calibrate, (unsigned long)sc);
  1470. if (ath9k_hw_getcapability(ah, ATH9K_CAP_CIPHER,
  1471. ATH9K_CIPHER_TKIP, NULL)) {
  1472. /*
  1473. * Whether we should enable h/w TKIP MIC.
  1474. * XXX: if we don't support WME TKIP MIC, then we wouldn't
  1475. * report WMM capable, so it's always safe to turn on
  1476. * TKIP MIC in this case.
  1477. */
  1478. ath9k_hw_setcapability(sc->sc_ah, ATH9K_CAP_TKIP_MIC,
  1479. 0, 1, NULL);
  1480. }
  1481. /*
  1482. * Check whether the separate key cache entries
  1483. * are required to handle both tx+rx MIC keys.
  1484. * With split mic keys the number of stations is limited
  1485. * to 27 otherwise 59.
  1486. */
  1487. if (ath9k_hw_getcapability(ah, ATH9K_CAP_CIPHER,
  1488. ATH9K_CIPHER_TKIP, NULL)
  1489. && ath9k_hw_getcapability(ah, ATH9K_CAP_CIPHER,
  1490. ATH9K_CIPHER_MIC, NULL)
  1491. && ath9k_hw_getcapability(ah, ATH9K_CAP_TKIP_SPLIT,
  1492. 0, NULL))
  1493. common->splitmic = 1;
  1494. /* turn on mcast key search if possible */
  1495. if (!ath9k_hw_getcapability(ah, ATH9K_CAP_MCAST_KEYSRCH, 0, NULL))
  1496. (void)ath9k_hw_setcapability(ah, ATH9K_CAP_MCAST_KEYSRCH, 1,
  1497. 1, NULL);
  1498. sc->config.txpowlimit = ATH_TXPOWER_MAX;
  1499. /* 11n Capabilities */
  1500. if (ah->caps.hw_caps & ATH9K_HW_CAP_HT) {
  1501. sc->sc_flags |= SC_OP_TXAGGR;
  1502. sc->sc_flags |= SC_OP_RXAGGR;
  1503. }
  1504. common->tx_chainmask = ah->caps.tx_chainmask;
  1505. common->rx_chainmask = ah->caps.rx_chainmask;
  1506. ath9k_hw_setcapability(ah, ATH9K_CAP_DIVERSITY, 1, true, NULL);
  1507. sc->rx.defant = ath9k_hw_getdefantenna(ah);
  1508. if (ah->caps.hw_caps & ATH9K_HW_CAP_BSSIDMASK)
  1509. memcpy(common->bssidmask, ath_bcast_mac, ETH_ALEN);
  1510. sc->beacon.slottime = ATH9K_SLOT_TIME_9; /* default to short slot time */
  1511. /* initialize beacon slots */
  1512. for (i = 0; i < ARRAY_SIZE(sc->beacon.bslot); i++) {
  1513. sc->beacon.bslot[i] = NULL;
  1514. sc->beacon.bslot_aphy[i] = NULL;
  1515. }
  1516. /* setup channels and rates */
  1517. if (test_bit(ATH9K_MODE_11G, sc->sc_ah->caps.wireless_modes)) {
  1518. sc->sbands[IEEE80211_BAND_2GHZ].channels = ath9k_2ghz_chantable;
  1519. sc->sbands[IEEE80211_BAND_2GHZ].band = IEEE80211_BAND_2GHZ;
  1520. sc->sbands[IEEE80211_BAND_2GHZ].n_channels =
  1521. ARRAY_SIZE(ath9k_2ghz_chantable);
  1522. sc->sbands[IEEE80211_BAND_2GHZ].bitrates = ath9k_legacy_rates;
  1523. sc->sbands[IEEE80211_BAND_2GHZ].n_bitrates =
  1524. ARRAY_SIZE(ath9k_legacy_rates);
  1525. }
  1526. if (test_bit(ATH9K_MODE_11A, sc->sc_ah->caps.wireless_modes)) {
  1527. sc->sbands[IEEE80211_BAND_5GHZ].channels = ath9k_5ghz_chantable;
  1528. sc->sbands[IEEE80211_BAND_5GHZ].band = IEEE80211_BAND_5GHZ;
  1529. sc->sbands[IEEE80211_BAND_5GHZ].n_channels =
  1530. ARRAY_SIZE(ath9k_5ghz_chantable);
  1531. sc->sbands[IEEE80211_BAND_5GHZ].bitrates =
  1532. ath9k_legacy_rates + 4;
  1533. sc->sbands[IEEE80211_BAND_5GHZ].n_bitrates =
  1534. ARRAY_SIZE(ath9k_legacy_rates) - 4;
  1535. }
  1536. switch (ah->btcoex_hw.scheme) {
  1537. case ATH_BTCOEX_CFG_NONE:
  1538. break;
  1539. case ATH_BTCOEX_CFG_2WIRE:
  1540. ath9k_hw_btcoex_init_2wire(ah);
  1541. break;
  1542. case ATH_BTCOEX_CFG_3WIRE:
  1543. ath9k_hw_btcoex_init_3wire(ah);
  1544. r = ath_init_btcoex_timer(sc);
  1545. if (r)
  1546. goto bad2;
  1547. qnum = ath_tx_get_qnum(sc, ATH9K_TX_QUEUE_DATA, ATH9K_WME_AC_BE);
  1548. ath9k_hw_init_btcoex_hw(ah, qnum);
  1549. sc->btcoex.bt_stomp_type = ATH_BTCOEX_STOMP_LOW;
  1550. break;
  1551. default:
  1552. WARN_ON(1);
  1553. break;
  1554. }
  1555. return 0;
  1556. bad2:
  1557. /* cleanup tx queues */
  1558. for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++)
  1559. if (ATH_TXQ_SETUP(sc, i))
  1560. ath_tx_cleanupq(sc, &sc->tx.txq[i]);
  1561. bad_free_hw:
  1562. ath9k_uninit_hw(sc);
  1563. return r;
  1564. }
  1565. void ath_set_hw_capab(struct ath_softc *sc, struct ieee80211_hw *hw)
  1566. {
  1567. hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
  1568. IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
  1569. IEEE80211_HW_SIGNAL_DBM |
  1570. IEEE80211_HW_AMPDU_AGGREGATION |
  1571. IEEE80211_HW_SUPPORTS_PS |
  1572. IEEE80211_HW_PS_NULLFUNC_STACK |
  1573. IEEE80211_HW_SPECTRUM_MGMT;
  1574. if (AR_SREV_9160_10_OR_LATER(sc->sc_ah) || modparam_nohwcrypt)
  1575. hw->flags |= IEEE80211_HW_MFP_CAPABLE;
  1576. hw->wiphy->interface_modes =
  1577. BIT(NL80211_IFTYPE_AP) |
  1578. BIT(NL80211_IFTYPE_STATION) |
  1579. BIT(NL80211_IFTYPE_ADHOC) |
  1580. BIT(NL80211_IFTYPE_MESH_POINT);
  1581. hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
  1582. hw->queues = 4;
  1583. hw->max_rates = 4;
  1584. hw->channel_change_time = 5000;
  1585. hw->max_listen_interval = 10;
  1586. /* Hardware supports 10 but we use 4 */
  1587. hw->max_rate_tries = 4;
  1588. hw->sta_data_size = sizeof(struct ath_node);
  1589. hw->vif_data_size = sizeof(struct ath_vif);
  1590. hw->rate_control_algorithm = "ath9k_rate_control";
  1591. if (test_bit(ATH9K_MODE_11G, sc->sc_ah->caps.wireless_modes))
  1592. hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
  1593. &sc->sbands[IEEE80211_BAND_2GHZ];
  1594. if (test_bit(ATH9K_MODE_11A, sc->sc_ah->caps.wireless_modes))
  1595. hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
  1596. &sc->sbands[IEEE80211_BAND_5GHZ];
  1597. }
  1598. /* Device driver core initialization */
  1599. int ath_init_device(u16 devid, struct ath_softc *sc, u16 subsysid,
  1600. const struct ath_bus_ops *bus_ops)
  1601. {
  1602. struct ieee80211_hw *hw = sc->hw;
  1603. struct ath_common *common;
  1604. struct ath_hw *ah;
  1605. int error = 0, i;
  1606. struct ath_regulatory *reg;
  1607. dev_dbg(sc->dev, "Attach ATH hw\n");
  1608. error = ath_init_softc(devid, sc, subsysid, bus_ops);
  1609. if (error != 0)
  1610. return error;
  1611. ah = sc->sc_ah;
  1612. common = ath9k_hw_common(ah);
  1613. /* get mac address from hardware and set in mac80211 */
  1614. SET_IEEE80211_PERM_ADDR(hw, common->macaddr);
  1615. ath_set_hw_capab(sc, hw);
  1616. error = ath_regd_init(&common->regulatory, sc->hw->wiphy,
  1617. ath9k_reg_notifier);
  1618. if (error)
  1619. return error;
  1620. reg = &common->regulatory;
  1621. if (ah->caps.hw_caps & ATH9K_HW_CAP_HT) {
  1622. if (test_bit(ATH9K_MODE_11G, ah->caps.wireless_modes))
  1623. setup_ht_cap(sc,
  1624. &sc->sbands[IEEE80211_BAND_2GHZ].ht_cap);
  1625. if (test_bit(ATH9K_MODE_11A, ah->caps.wireless_modes))
  1626. setup_ht_cap(sc,
  1627. &sc->sbands[IEEE80211_BAND_5GHZ].ht_cap);
  1628. }
  1629. /* initialize tx/rx engine */
  1630. error = ath_tx_init(sc, ATH_TXBUF);
  1631. if (error != 0)
  1632. goto error_attach;
  1633. error = ath_rx_init(sc, ATH_RXBUF);
  1634. if (error != 0)
  1635. goto error_attach;
  1636. INIT_WORK(&sc->chan_work, ath9k_wiphy_chan_work);
  1637. INIT_DELAYED_WORK(&sc->wiphy_work, ath9k_wiphy_work);
  1638. sc->wiphy_scheduler_int = msecs_to_jiffies(500);
  1639. error = ieee80211_register_hw(hw);
  1640. if (!ath_is_world_regd(reg)) {
  1641. error = regulatory_hint(hw->wiphy, reg->alpha2);
  1642. if (error)
  1643. goto error_attach;
  1644. }
  1645. /* Initialize LED control */
  1646. ath_init_leds(sc);
  1647. ath_start_rfkill_poll(sc);
  1648. return 0;
  1649. error_attach:
  1650. /* cleanup tx queues */
  1651. for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++)
  1652. if (ATH_TXQ_SETUP(sc, i))
  1653. ath_tx_cleanupq(sc, &sc->tx.txq[i]);
  1654. ath9k_uninit_hw(sc);
  1655. return error;
  1656. }
  1657. int ath_reset(struct ath_softc *sc, bool retry_tx)
  1658. {
  1659. struct ath_hw *ah = sc->sc_ah;
  1660. struct ath_common *common = ath9k_hw_common(ah);
  1661. struct ieee80211_hw *hw = sc->hw;
  1662. int r;
  1663. ath9k_hw_set_interrupts(ah, 0);
  1664. ath_drain_all_txq(sc, retry_tx);
  1665. ath_stoprecv(sc);
  1666. ath_flushrecv(sc);
  1667. spin_lock_bh(&sc->sc_resetlock);
  1668. r = ath9k_hw_reset(ah, sc->sc_ah->curchan, false);
  1669. if (r)
  1670. ath_print(common, ATH_DBG_FATAL,
  1671. "Unable to reset hardware; reset status %d\n", r);
  1672. spin_unlock_bh(&sc->sc_resetlock);
  1673. if (ath_startrecv(sc) != 0)
  1674. ath_print(common, ATH_DBG_FATAL,
  1675. "Unable to start recv logic\n");
  1676. /*
  1677. * We may be doing a reset in response to a request
  1678. * that changes the channel so update any state that
  1679. * might change as a result.
  1680. */
  1681. ath_cache_conf_rate(sc, &hw->conf);
  1682. ath_update_txpow(sc);
  1683. if (sc->sc_flags & SC_OP_BEACONS)
  1684. ath_beacon_config(sc, NULL); /* restart beacons */
  1685. ath9k_hw_set_interrupts(ah, sc->imask);
  1686. if (retry_tx) {
  1687. int i;
  1688. for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
  1689. if (ATH_TXQ_SETUP(sc, i)) {
  1690. spin_lock_bh(&sc->tx.txq[i].axq_lock);
  1691. ath_txq_schedule(sc, &sc->tx.txq[i]);
  1692. spin_unlock_bh(&sc->tx.txq[i].axq_lock);
  1693. }
  1694. }
  1695. }
  1696. return r;
  1697. }
  1698. /*
  1699. * This function will allocate both the DMA descriptor structure, and the
  1700. * buffers it contains. These are used to contain the descriptors used
  1701. * by the system.
  1702. */
  1703. int ath_descdma_setup(struct ath_softc *sc, struct ath_descdma *dd,
  1704. struct list_head *head, const char *name,
  1705. int nbuf, int ndesc)
  1706. {
  1707. #define DS2PHYS(_dd, _ds) \
  1708. ((_dd)->dd_desc_paddr + ((caddr_t)(_ds) - (caddr_t)(_dd)->dd_desc))
  1709. #define ATH_DESC_4KB_BOUND_CHECK(_daddr) ((((_daddr) & 0xFFF) > 0xF7F) ? 1 : 0)
  1710. #define ATH_DESC_4KB_BOUND_NUM_SKIPPED(_len) ((_len) / 4096)
  1711. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  1712. struct ath_desc *ds;
  1713. struct ath_buf *bf;
  1714. int i, bsize, error;
  1715. ath_print(common, ATH_DBG_CONFIG, "%s DMA: %u buffers %u desc/buf\n",
  1716. name, nbuf, ndesc);
  1717. INIT_LIST_HEAD(head);
  1718. /* ath_desc must be a multiple of DWORDs */
  1719. if ((sizeof(struct ath_desc) % 4) != 0) {
  1720. ath_print(common, ATH_DBG_FATAL,
  1721. "ath_desc not DWORD aligned\n");
  1722. BUG_ON((sizeof(struct ath_desc) % 4) != 0);
  1723. error = -ENOMEM;
  1724. goto fail;
  1725. }
  1726. dd->dd_desc_len = sizeof(struct ath_desc) * nbuf * ndesc;
  1727. /*
  1728. * Need additional DMA memory because we can't use
  1729. * descriptors that cross the 4K page boundary. Assume
  1730. * one skipped descriptor per 4K page.
  1731. */
  1732. if (!(sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_4KB_SPLITTRANS)) {
  1733. u32 ndesc_skipped =
  1734. ATH_DESC_4KB_BOUND_NUM_SKIPPED(dd->dd_desc_len);
  1735. u32 dma_len;
  1736. while (ndesc_skipped) {
  1737. dma_len = ndesc_skipped * sizeof(struct ath_desc);
  1738. dd->dd_desc_len += dma_len;
  1739. ndesc_skipped = ATH_DESC_4KB_BOUND_NUM_SKIPPED(dma_len);
  1740. };
  1741. }
  1742. /* allocate descriptors */
  1743. dd->dd_desc = dma_alloc_coherent(sc->dev, dd->dd_desc_len,
  1744. &dd->dd_desc_paddr, GFP_KERNEL);
  1745. if (dd->dd_desc == NULL) {
  1746. error = -ENOMEM;
  1747. goto fail;
  1748. }
  1749. ds = dd->dd_desc;
  1750. ath_print(common, ATH_DBG_CONFIG, "%s DMA map: %p (%u) -> %llx (%u)\n",
  1751. name, ds, (u32) dd->dd_desc_len,
  1752. ito64(dd->dd_desc_paddr), /*XXX*/(u32) dd->dd_desc_len);
  1753. /* allocate buffers */
  1754. bsize = sizeof(struct ath_buf) * nbuf;
  1755. bf = kzalloc(bsize, GFP_KERNEL);
  1756. if (bf == NULL) {
  1757. error = -ENOMEM;
  1758. goto fail2;
  1759. }
  1760. dd->dd_bufptr = bf;
  1761. for (i = 0; i < nbuf; i++, bf++, ds += ndesc) {
  1762. bf->bf_desc = ds;
  1763. bf->bf_daddr = DS2PHYS(dd, ds);
  1764. if (!(sc->sc_ah->caps.hw_caps &
  1765. ATH9K_HW_CAP_4KB_SPLITTRANS)) {
  1766. /*
  1767. * Skip descriptor addresses which can cause 4KB
  1768. * boundary crossing (addr + length) with a 32 dword
  1769. * descriptor fetch.
  1770. */
  1771. while (ATH_DESC_4KB_BOUND_CHECK(bf->bf_daddr)) {
  1772. BUG_ON((caddr_t) bf->bf_desc >=
  1773. ((caddr_t) dd->dd_desc +
  1774. dd->dd_desc_len));
  1775. ds += ndesc;
  1776. bf->bf_desc = ds;
  1777. bf->bf_daddr = DS2PHYS(dd, ds);
  1778. }
  1779. }
  1780. list_add_tail(&bf->list, head);
  1781. }
  1782. return 0;
  1783. fail2:
  1784. dma_free_coherent(sc->dev, dd->dd_desc_len, dd->dd_desc,
  1785. dd->dd_desc_paddr);
  1786. fail:
  1787. memset(dd, 0, sizeof(*dd));
  1788. return error;
  1789. #undef ATH_DESC_4KB_BOUND_CHECK
  1790. #undef ATH_DESC_4KB_BOUND_NUM_SKIPPED
  1791. #undef DS2PHYS
  1792. }
  1793. void ath_descdma_cleanup(struct ath_softc *sc,
  1794. struct ath_descdma *dd,
  1795. struct list_head *head)
  1796. {
  1797. dma_free_coherent(sc->dev, dd->dd_desc_len, dd->dd_desc,
  1798. dd->dd_desc_paddr);
  1799. INIT_LIST_HEAD(head);
  1800. kfree(dd->dd_bufptr);
  1801. memset(dd, 0, sizeof(*dd));
  1802. }
  1803. int ath_get_hal_qnum(u16 queue, struct ath_softc *sc)
  1804. {
  1805. int qnum;
  1806. switch (queue) {
  1807. case 0:
  1808. qnum = sc->tx.hwq_map[ATH9K_WME_AC_VO];
  1809. break;
  1810. case 1:
  1811. qnum = sc->tx.hwq_map[ATH9K_WME_AC_VI];
  1812. break;
  1813. case 2:
  1814. qnum = sc->tx.hwq_map[ATH9K_WME_AC_BE];
  1815. break;
  1816. case 3:
  1817. qnum = sc->tx.hwq_map[ATH9K_WME_AC_BK];
  1818. break;
  1819. default:
  1820. qnum = sc->tx.hwq_map[ATH9K_WME_AC_BE];
  1821. break;
  1822. }
  1823. return qnum;
  1824. }
  1825. int ath_get_mac80211_qnum(u32 queue, struct ath_softc *sc)
  1826. {
  1827. int qnum;
  1828. switch (queue) {
  1829. case ATH9K_WME_AC_VO:
  1830. qnum = 0;
  1831. break;
  1832. case ATH9K_WME_AC_VI:
  1833. qnum = 1;
  1834. break;
  1835. case ATH9K_WME_AC_BE:
  1836. qnum = 2;
  1837. break;
  1838. case ATH9K_WME_AC_BK:
  1839. qnum = 3;
  1840. break;
  1841. default:
  1842. qnum = -1;
  1843. break;
  1844. }
  1845. return qnum;
  1846. }
  1847. /* XXX: Remove me once we don't depend on ath9k_channel for all
  1848. * this redundant data */
  1849. void ath9k_update_ichannel(struct ath_softc *sc, struct ieee80211_hw *hw,
  1850. struct ath9k_channel *ichan)
  1851. {
  1852. struct ieee80211_channel *chan = hw->conf.channel;
  1853. struct ieee80211_conf *conf = &hw->conf;
  1854. ichan->channel = chan->center_freq;
  1855. ichan->chan = chan;
  1856. if (chan->band == IEEE80211_BAND_2GHZ) {
  1857. ichan->chanmode = CHANNEL_G;
  1858. ichan->channelFlags = CHANNEL_2GHZ | CHANNEL_OFDM | CHANNEL_G;
  1859. } else {
  1860. ichan->chanmode = CHANNEL_A;
  1861. ichan->channelFlags = CHANNEL_5GHZ | CHANNEL_OFDM;
  1862. }
  1863. if (conf_is_ht(conf))
  1864. ichan->chanmode = ath_get_extchanmode(sc, chan,
  1865. conf->channel_type);
  1866. }
  1867. /**********************/
  1868. /* mac80211 callbacks */
  1869. /**********************/
  1870. /*
  1871. * (Re)start btcoex timers
  1872. */
  1873. static void ath9k_btcoex_timer_resume(struct ath_softc *sc)
  1874. {
  1875. struct ath_btcoex *btcoex = &sc->btcoex;
  1876. struct ath_hw *ah = sc->sc_ah;
  1877. ath_print(ath9k_hw_common(ah), ATH_DBG_BTCOEX,
  1878. "Starting btcoex timers");
  1879. /* make sure duty cycle timer is also stopped when resuming */
  1880. if (btcoex->hw_timer_enabled)
  1881. ath9k_gen_timer_stop(sc->sc_ah, btcoex->no_stomp_timer);
  1882. btcoex->bt_priority_cnt = 0;
  1883. btcoex->bt_priority_time = jiffies;
  1884. sc->sc_flags &= ~SC_OP_BT_PRIORITY_DETECTED;
  1885. mod_timer(&btcoex->period_timer, jiffies);
  1886. }
  1887. static int ath9k_start(struct ieee80211_hw *hw)
  1888. {
  1889. struct ath_wiphy *aphy = hw->priv;
  1890. struct ath_softc *sc = aphy->sc;
  1891. struct ath_hw *ah = sc->sc_ah;
  1892. struct ath_common *common = ath9k_hw_common(ah);
  1893. struct ieee80211_channel *curchan = hw->conf.channel;
  1894. struct ath9k_channel *init_channel;
  1895. int r;
  1896. ath_print(common, ATH_DBG_CONFIG,
  1897. "Starting driver with initial channel: %d MHz\n",
  1898. curchan->center_freq);
  1899. mutex_lock(&sc->mutex);
  1900. if (ath9k_wiphy_started(sc)) {
  1901. if (sc->chan_idx == curchan->hw_value) {
  1902. /*
  1903. * Already on the operational channel, the new wiphy
  1904. * can be marked active.
  1905. */
  1906. aphy->state = ATH_WIPHY_ACTIVE;
  1907. ieee80211_wake_queues(hw);
  1908. } else {
  1909. /*
  1910. * Another wiphy is on another channel, start the new
  1911. * wiphy in paused state.
  1912. */
  1913. aphy->state = ATH_WIPHY_PAUSED;
  1914. ieee80211_stop_queues(hw);
  1915. }
  1916. mutex_unlock(&sc->mutex);
  1917. return 0;
  1918. }
  1919. aphy->state = ATH_WIPHY_ACTIVE;
  1920. /* setup initial channel */
  1921. sc->chan_idx = curchan->hw_value;
  1922. init_channel = ath_get_curchannel(sc, hw);
  1923. /* Reset SERDES registers */
  1924. ath9k_hw_configpcipowersave(ah, 0, 0);
  1925. /*
  1926. * The basic interface to setting the hardware in a good
  1927. * state is ``reset''. On return the hardware is known to
  1928. * be powered up and with interrupts disabled. This must
  1929. * be followed by initialization of the appropriate bits
  1930. * and then setup of the interrupt mask.
  1931. */
  1932. spin_lock_bh(&sc->sc_resetlock);
  1933. r = ath9k_hw_reset(ah, init_channel, false);
  1934. if (r) {
  1935. ath_print(common, ATH_DBG_FATAL,
  1936. "Unable to reset hardware; reset status %d "
  1937. "(freq %u MHz)\n", r,
  1938. curchan->center_freq);
  1939. spin_unlock_bh(&sc->sc_resetlock);
  1940. goto mutex_unlock;
  1941. }
  1942. spin_unlock_bh(&sc->sc_resetlock);
  1943. /*
  1944. * This is needed only to setup initial state
  1945. * but it's best done after a reset.
  1946. */
  1947. ath_update_txpow(sc);
  1948. /*
  1949. * Setup the hardware after reset:
  1950. * The receive engine is set going.
  1951. * Frame transmit is handled entirely
  1952. * in the frame output path; there's nothing to do
  1953. * here except setup the interrupt mask.
  1954. */
  1955. if (ath_startrecv(sc) != 0) {
  1956. ath_print(common, ATH_DBG_FATAL,
  1957. "Unable to start recv logic\n");
  1958. r = -EIO;
  1959. goto mutex_unlock;
  1960. }
  1961. /* Setup our intr mask. */
  1962. sc->imask = ATH9K_INT_RX | ATH9K_INT_TX
  1963. | ATH9K_INT_RXEOL | ATH9K_INT_RXORN
  1964. | ATH9K_INT_FATAL | ATH9K_INT_GLOBAL;
  1965. if (ah->caps.hw_caps & ATH9K_HW_CAP_GTT)
  1966. sc->imask |= ATH9K_INT_GTT;
  1967. if (ah->caps.hw_caps & ATH9K_HW_CAP_HT)
  1968. sc->imask |= ATH9K_INT_CST;
  1969. ath_cache_conf_rate(sc, &hw->conf);
  1970. sc->sc_flags &= ~SC_OP_INVALID;
  1971. /* Disable BMISS interrupt when we're not associated */
  1972. sc->imask &= ~(ATH9K_INT_SWBA | ATH9K_INT_BMISS);
  1973. ath9k_hw_set_interrupts(ah, sc->imask);
  1974. ieee80211_wake_queues(hw);
  1975. ieee80211_queue_delayed_work(sc->hw, &sc->tx_complete_work, 0);
  1976. if ((ah->btcoex_hw.scheme != ATH_BTCOEX_CFG_NONE) &&
  1977. !ah->btcoex_hw.enabled) {
  1978. ath9k_hw_btcoex_set_weight(ah, AR_BT_COEX_WGHT,
  1979. AR_STOMP_LOW_WLAN_WGHT);
  1980. ath9k_hw_btcoex_enable(ah);
  1981. if (common->bus_ops->bt_coex_prep)
  1982. common->bus_ops->bt_coex_prep(common);
  1983. if (ah->btcoex_hw.scheme == ATH_BTCOEX_CFG_3WIRE)
  1984. ath9k_btcoex_timer_resume(sc);
  1985. }
  1986. mutex_unlock:
  1987. mutex_unlock(&sc->mutex);
  1988. return r;
  1989. }
  1990. static int ath9k_tx(struct ieee80211_hw *hw,
  1991. struct sk_buff *skb)
  1992. {
  1993. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1994. struct ath_wiphy *aphy = hw->priv;
  1995. struct ath_softc *sc = aphy->sc;
  1996. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  1997. struct ath_tx_control txctl;
  1998. int padpos, padsize;
  1999. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  2000. if (aphy->state != ATH_WIPHY_ACTIVE && aphy->state != ATH_WIPHY_SCAN) {
  2001. ath_print(common, ATH_DBG_XMIT,
  2002. "ath9k: %s: TX in unexpected wiphy state "
  2003. "%d\n", wiphy_name(hw->wiphy), aphy->state);
  2004. goto exit;
  2005. }
  2006. if (sc->ps_enabled) {
  2007. /*
  2008. * mac80211 does not set PM field for normal data frames, so we
  2009. * need to update that based on the current PS mode.
  2010. */
  2011. if (ieee80211_is_data(hdr->frame_control) &&
  2012. !ieee80211_is_nullfunc(hdr->frame_control) &&
  2013. !ieee80211_has_pm(hdr->frame_control)) {
  2014. ath_print(common, ATH_DBG_PS, "Add PM=1 for a TX frame "
  2015. "while in PS mode\n");
  2016. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
  2017. }
  2018. }
  2019. if (unlikely(sc->sc_ah->power_mode != ATH9K_PM_AWAKE)) {
  2020. /*
  2021. * We are using PS-Poll and mac80211 can request TX while in
  2022. * power save mode. Need to wake up hardware for the TX to be
  2023. * completed and if needed, also for RX of buffered frames.
  2024. */
  2025. ath9k_ps_wakeup(sc);
  2026. ath9k_hw_setrxabort(sc->sc_ah, 0);
  2027. if (ieee80211_is_pspoll(hdr->frame_control)) {
  2028. ath_print(common, ATH_DBG_PS,
  2029. "Sending PS-Poll to pick a buffered frame\n");
  2030. sc->sc_flags |= SC_OP_WAIT_FOR_PSPOLL_DATA;
  2031. } else {
  2032. ath_print(common, ATH_DBG_PS,
  2033. "Wake up to complete TX\n");
  2034. sc->sc_flags |= SC_OP_WAIT_FOR_TX_ACK;
  2035. }
  2036. /*
  2037. * The actual restore operation will happen only after
  2038. * the sc_flags bit is cleared. We are just dropping
  2039. * the ps_usecount here.
  2040. */
  2041. ath9k_ps_restore(sc);
  2042. }
  2043. memset(&txctl, 0, sizeof(struct ath_tx_control));
  2044. /*
  2045. * As a temporary workaround, assign seq# here; this will likely need
  2046. * to be cleaned up to work better with Beacon transmission and virtual
  2047. * BSSes.
  2048. */
  2049. if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
  2050. if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
  2051. sc->tx.seq_no += 0x10;
  2052. hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
  2053. hdr->seq_ctrl |= cpu_to_le16(sc->tx.seq_no);
  2054. }
  2055. /* Add the padding after the header if this is not already done */
  2056. padpos = ath9k_cmn_padpos(hdr->frame_control);
  2057. padsize = padpos & 3;
  2058. if (padsize && skb->len>padpos) {
  2059. if (skb_headroom(skb) < padsize)
  2060. return -1;
  2061. skb_push(skb, padsize);
  2062. memmove(skb->data, skb->data + padsize, padpos);
  2063. }
  2064. /* Check if a tx queue is available */
  2065. txctl.txq = ath_test_get_txq(sc, skb);
  2066. if (!txctl.txq)
  2067. goto exit;
  2068. ath_print(common, ATH_DBG_XMIT, "transmitting packet, skb: %p\n", skb);
  2069. if (ath_tx_start(hw, skb, &txctl) != 0) {
  2070. ath_print(common, ATH_DBG_XMIT, "TX failed\n");
  2071. goto exit;
  2072. }
  2073. return 0;
  2074. exit:
  2075. dev_kfree_skb_any(skb);
  2076. return 0;
  2077. }
  2078. /*
  2079. * Pause btcoex timer and bt duty cycle timer
  2080. */
  2081. static void ath9k_btcoex_timer_pause(struct ath_softc *sc)
  2082. {
  2083. struct ath_btcoex *btcoex = &sc->btcoex;
  2084. struct ath_hw *ah = sc->sc_ah;
  2085. del_timer_sync(&btcoex->period_timer);
  2086. if (btcoex->hw_timer_enabled)
  2087. ath9k_gen_timer_stop(ah, btcoex->no_stomp_timer);
  2088. btcoex->hw_timer_enabled = false;
  2089. }
  2090. static void ath9k_stop(struct ieee80211_hw *hw)
  2091. {
  2092. struct ath_wiphy *aphy = hw->priv;
  2093. struct ath_softc *sc = aphy->sc;
  2094. struct ath_hw *ah = sc->sc_ah;
  2095. struct ath_common *common = ath9k_hw_common(ah);
  2096. mutex_lock(&sc->mutex);
  2097. aphy->state = ATH_WIPHY_INACTIVE;
  2098. cancel_delayed_work_sync(&sc->ath_led_blink_work);
  2099. cancel_delayed_work_sync(&sc->tx_complete_work);
  2100. if (!sc->num_sec_wiphy) {
  2101. cancel_delayed_work_sync(&sc->wiphy_work);
  2102. cancel_work_sync(&sc->chan_work);
  2103. }
  2104. if (sc->sc_flags & SC_OP_INVALID) {
  2105. ath_print(common, ATH_DBG_ANY, "Device not present\n");
  2106. mutex_unlock(&sc->mutex);
  2107. return;
  2108. }
  2109. if (ath9k_wiphy_started(sc)) {
  2110. mutex_unlock(&sc->mutex);
  2111. return; /* another wiphy still in use */
  2112. }
  2113. if (ah->btcoex_hw.enabled) {
  2114. ath9k_hw_btcoex_disable(ah);
  2115. if (ah->btcoex_hw.scheme == ATH_BTCOEX_CFG_3WIRE)
  2116. ath9k_btcoex_timer_pause(sc);
  2117. }
  2118. /* make sure h/w will not generate any interrupt
  2119. * before setting the invalid flag. */
  2120. ath9k_hw_set_interrupts(ah, 0);
  2121. if (!(sc->sc_flags & SC_OP_INVALID)) {
  2122. ath_drain_all_txq(sc, false);
  2123. ath_stoprecv(sc);
  2124. ath9k_hw_phy_disable(ah);
  2125. } else
  2126. sc->rx.rxlink = NULL;
  2127. /* disable HAL and put h/w to sleep */
  2128. ath9k_hw_disable(ah);
  2129. ath9k_hw_configpcipowersave(ah, 1, 1);
  2130. ath9k_setpower(sc, ATH9K_PM_FULL_SLEEP);
  2131. sc->sc_flags |= SC_OP_INVALID;
  2132. mutex_unlock(&sc->mutex);
  2133. ath_print(common, ATH_DBG_CONFIG, "Driver halt\n");
  2134. }
  2135. static int ath9k_add_interface(struct ieee80211_hw *hw,
  2136. struct ieee80211_if_init_conf *conf)
  2137. {
  2138. struct ath_wiphy *aphy = hw->priv;
  2139. struct ath_softc *sc = aphy->sc;
  2140. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  2141. struct ath_vif *avp = (void *)conf->vif->drv_priv;
  2142. enum nl80211_iftype ic_opmode = NL80211_IFTYPE_UNSPECIFIED;
  2143. int ret = 0;
  2144. mutex_lock(&sc->mutex);
  2145. if (!(sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_BSSIDMASK) &&
  2146. sc->nvifs > 0) {
  2147. ret = -ENOBUFS;
  2148. goto out;
  2149. }
  2150. switch (conf->type) {
  2151. case NL80211_IFTYPE_STATION:
  2152. ic_opmode = NL80211_IFTYPE_STATION;
  2153. break;
  2154. case NL80211_IFTYPE_ADHOC:
  2155. case NL80211_IFTYPE_AP:
  2156. case NL80211_IFTYPE_MESH_POINT:
  2157. if (sc->nbcnvifs >= ATH_BCBUF) {
  2158. ret = -ENOBUFS;
  2159. goto out;
  2160. }
  2161. ic_opmode = conf->type;
  2162. break;
  2163. default:
  2164. ath_print(common, ATH_DBG_FATAL,
  2165. "Interface type %d not yet supported\n", conf->type);
  2166. ret = -EOPNOTSUPP;
  2167. goto out;
  2168. }
  2169. ath_print(common, ATH_DBG_CONFIG,
  2170. "Attach a VIF of type: %d\n", ic_opmode);
  2171. /* Set the VIF opmode */
  2172. avp->av_opmode = ic_opmode;
  2173. avp->av_bslot = -1;
  2174. sc->nvifs++;
  2175. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_BSSIDMASK)
  2176. ath9k_set_bssid_mask(hw);
  2177. if (sc->nvifs > 1)
  2178. goto out; /* skip global settings for secondary vif */
  2179. if (ic_opmode == NL80211_IFTYPE_AP) {
  2180. ath9k_hw_set_tsfadjust(sc->sc_ah, 1);
  2181. sc->sc_flags |= SC_OP_TSF_RESET;
  2182. }
  2183. /* Set the device opmode */
  2184. sc->sc_ah->opmode = ic_opmode;
  2185. /*
  2186. * Enable MIB interrupts when there are hardware phy counters.
  2187. * Note we only do this (at the moment) for station mode.
  2188. */
  2189. if ((conf->type == NL80211_IFTYPE_STATION) ||
  2190. (conf->type == NL80211_IFTYPE_ADHOC) ||
  2191. (conf->type == NL80211_IFTYPE_MESH_POINT)) {
  2192. sc->imask |= ATH9K_INT_MIB;
  2193. sc->imask |= ATH9K_INT_TSFOOR;
  2194. }
  2195. ath9k_hw_set_interrupts(sc->sc_ah, sc->imask);
  2196. if (conf->type == NL80211_IFTYPE_AP ||
  2197. conf->type == NL80211_IFTYPE_ADHOC ||
  2198. conf->type == NL80211_IFTYPE_MONITOR)
  2199. ath_start_ani(common);
  2200. out:
  2201. mutex_unlock(&sc->mutex);
  2202. return ret;
  2203. }
  2204. static void ath9k_remove_interface(struct ieee80211_hw *hw,
  2205. struct ieee80211_if_init_conf *conf)
  2206. {
  2207. struct ath_wiphy *aphy = hw->priv;
  2208. struct ath_softc *sc = aphy->sc;
  2209. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  2210. struct ath_vif *avp = (void *)conf->vif->drv_priv;
  2211. int i;
  2212. ath_print(common, ATH_DBG_CONFIG, "Detach Interface\n");
  2213. mutex_lock(&sc->mutex);
  2214. /* Stop ANI */
  2215. del_timer_sync(&common->ani.timer);
  2216. /* Reclaim beacon resources */
  2217. if ((sc->sc_ah->opmode == NL80211_IFTYPE_AP) ||
  2218. (sc->sc_ah->opmode == NL80211_IFTYPE_ADHOC) ||
  2219. (sc->sc_ah->opmode == NL80211_IFTYPE_MESH_POINT)) {
  2220. ath9k_hw_stoptxdma(sc->sc_ah, sc->beacon.beaconq);
  2221. ath_beacon_return(sc, avp);
  2222. }
  2223. sc->sc_flags &= ~SC_OP_BEACONS;
  2224. for (i = 0; i < ARRAY_SIZE(sc->beacon.bslot); i++) {
  2225. if (sc->beacon.bslot[i] == conf->vif) {
  2226. printk(KERN_DEBUG "%s: vif had allocated beacon "
  2227. "slot\n", __func__);
  2228. sc->beacon.bslot[i] = NULL;
  2229. sc->beacon.bslot_aphy[i] = NULL;
  2230. }
  2231. }
  2232. sc->nvifs--;
  2233. mutex_unlock(&sc->mutex);
  2234. }
  2235. static int ath9k_config(struct ieee80211_hw *hw, u32 changed)
  2236. {
  2237. struct ath_wiphy *aphy = hw->priv;
  2238. struct ath_softc *sc = aphy->sc;
  2239. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  2240. struct ieee80211_conf *conf = &hw->conf;
  2241. struct ath_hw *ah = sc->sc_ah;
  2242. bool disable_radio;
  2243. mutex_lock(&sc->mutex);
  2244. /*
  2245. * Leave this as the first check because we need to turn on the
  2246. * radio if it was disabled before prior to processing the rest
  2247. * of the changes. Likewise we must only disable the radio towards
  2248. * the end.
  2249. */
  2250. if (changed & IEEE80211_CONF_CHANGE_IDLE) {
  2251. bool enable_radio;
  2252. bool all_wiphys_idle;
  2253. bool idle = !!(conf->flags & IEEE80211_CONF_IDLE);
  2254. spin_lock_bh(&sc->wiphy_lock);
  2255. all_wiphys_idle = ath9k_all_wiphys_idle(sc);
  2256. ath9k_set_wiphy_idle(aphy, idle);
  2257. if (!idle && all_wiphys_idle)
  2258. enable_radio = true;
  2259. /*
  2260. * After we unlock here its possible another wiphy
  2261. * can be re-renabled so to account for that we will
  2262. * only disable the radio toward the end of this routine
  2263. * if by then all wiphys are still idle.
  2264. */
  2265. spin_unlock_bh(&sc->wiphy_lock);
  2266. if (enable_radio) {
  2267. ath_radio_enable(sc, hw);
  2268. ath_print(common, ATH_DBG_CONFIG,
  2269. "not-idle: enabling radio\n");
  2270. }
  2271. }
  2272. /*
  2273. * We just prepare to enable PS. We have to wait until our AP has
  2274. * ACK'd our null data frame to disable RX otherwise we'll ignore
  2275. * those ACKs and end up retransmitting the same null data frames.
  2276. * IEEE80211_CONF_CHANGE_PS is only passed by mac80211 for STA mode.
  2277. */
  2278. if (changed & IEEE80211_CONF_CHANGE_PS) {
  2279. if (conf->flags & IEEE80211_CONF_PS) {
  2280. sc->sc_flags |= SC_OP_PS_ENABLED;
  2281. if (!(ah->caps.hw_caps &
  2282. ATH9K_HW_CAP_AUTOSLEEP)) {
  2283. if ((sc->imask & ATH9K_INT_TIM_TIMER) == 0) {
  2284. sc->imask |= ATH9K_INT_TIM_TIMER;
  2285. ath9k_hw_set_interrupts(sc->sc_ah,
  2286. sc->imask);
  2287. }
  2288. }
  2289. /*
  2290. * At this point we know hardware has received an ACK
  2291. * of a previously sent null data frame.
  2292. */
  2293. if ((sc->sc_flags & SC_OP_NULLFUNC_COMPLETED)) {
  2294. sc->sc_flags &= ~SC_OP_NULLFUNC_COMPLETED;
  2295. sc->ps_enabled = true;
  2296. ath9k_hw_setrxabort(sc->sc_ah, 1);
  2297. }
  2298. } else {
  2299. sc->ps_enabled = false;
  2300. sc->sc_flags &= ~(SC_OP_PS_ENABLED |
  2301. SC_OP_NULLFUNC_COMPLETED);
  2302. ath9k_setpower(sc, ATH9K_PM_AWAKE);
  2303. if (!(ah->caps.hw_caps &
  2304. ATH9K_HW_CAP_AUTOSLEEP)) {
  2305. ath9k_hw_setrxabort(sc->sc_ah, 0);
  2306. sc->sc_flags &= ~(SC_OP_WAIT_FOR_BEACON |
  2307. SC_OP_WAIT_FOR_CAB |
  2308. SC_OP_WAIT_FOR_PSPOLL_DATA |
  2309. SC_OP_WAIT_FOR_TX_ACK);
  2310. if (sc->imask & ATH9K_INT_TIM_TIMER) {
  2311. sc->imask &= ~ATH9K_INT_TIM_TIMER;
  2312. ath9k_hw_set_interrupts(sc->sc_ah,
  2313. sc->imask);
  2314. }
  2315. }
  2316. }
  2317. }
  2318. if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
  2319. struct ieee80211_channel *curchan = hw->conf.channel;
  2320. int pos = curchan->hw_value;
  2321. aphy->chan_idx = pos;
  2322. aphy->chan_is_ht = conf_is_ht(conf);
  2323. if (aphy->state == ATH_WIPHY_SCAN ||
  2324. aphy->state == ATH_WIPHY_ACTIVE)
  2325. ath9k_wiphy_pause_all_forced(sc, aphy);
  2326. else {
  2327. /*
  2328. * Do not change operational channel based on a paused
  2329. * wiphy changes.
  2330. */
  2331. goto skip_chan_change;
  2332. }
  2333. ath_print(common, ATH_DBG_CONFIG, "Set channel: %d MHz\n",
  2334. curchan->center_freq);
  2335. /* XXX: remove me eventualy */
  2336. ath9k_update_ichannel(sc, hw, &sc->sc_ah->channels[pos]);
  2337. ath_update_chainmask(sc, conf_is_ht(conf));
  2338. if (ath_set_channel(sc, hw, &sc->sc_ah->channels[pos]) < 0) {
  2339. ath_print(common, ATH_DBG_FATAL,
  2340. "Unable to set channel\n");
  2341. mutex_unlock(&sc->mutex);
  2342. return -EINVAL;
  2343. }
  2344. }
  2345. skip_chan_change:
  2346. if (changed & IEEE80211_CONF_CHANGE_POWER)
  2347. sc->config.txpowlimit = 2 * conf->power_level;
  2348. spin_lock_bh(&sc->wiphy_lock);
  2349. disable_radio = ath9k_all_wiphys_idle(sc);
  2350. spin_unlock_bh(&sc->wiphy_lock);
  2351. if (disable_radio) {
  2352. ath_print(common, ATH_DBG_CONFIG, "idle: disabling radio\n");
  2353. ath_radio_disable(sc, hw);
  2354. }
  2355. mutex_unlock(&sc->mutex);
  2356. return 0;
  2357. }
  2358. #define SUPPORTED_FILTERS \
  2359. (FIF_PROMISC_IN_BSS | \
  2360. FIF_ALLMULTI | \
  2361. FIF_CONTROL | \
  2362. FIF_PSPOLL | \
  2363. FIF_OTHER_BSS | \
  2364. FIF_BCN_PRBRESP_PROMISC | \
  2365. FIF_FCSFAIL)
  2366. /* FIXME: sc->sc_full_reset ? */
  2367. static void ath9k_configure_filter(struct ieee80211_hw *hw,
  2368. unsigned int changed_flags,
  2369. unsigned int *total_flags,
  2370. u64 multicast)
  2371. {
  2372. struct ath_wiphy *aphy = hw->priv;
  2373. struct ath_softc *sc = aphy->sc;
  2374. u32 rfilt;
  2375. changed_flags &= SUPPORTED_FILTERS;
  2376. *total_flags &= SUPPORTED_FILTERS;
  2377. sc->rx.rxfilter = *total_flags;
  2378. ath9k_ps_wakeup(sc);
  2379. rfilt = ath_calcrxfilter(sc);
  2380. ath9k_hw_setrxfilter(sc->sc_ah, rfilt);
  2381. ath9k_ps_restore(sc);
  2382. ath_print(ath9k_hw_common(sc->sc_ah), ATH_DBG_CONFIG,
  2383. "Set HW RX filter: 0x%x\n", rfilt);
  2384. }
  2385. static void ath9k_sta_notify(struct ieee80211_hw *hw,
  2386. struct ieee80211_vif *vif,
  2387. enum sta_notify_cmd cmd,
  2388. struct ieee80211_sta *sta)
  2389. {
  2390. struct ath_wiphy *aphy = hw->priv;
  2391. struct ath_softc *sc = aphy->sc;
  2392. switch (cmd) {
  2393. case STA_NOTIFY_ADD:
  2394. ath_node_attach(sc, sta);
  2395. break;
  2396. case STA_NOTIFY_REMOVE:
  2397. ath_node_detach(sc, sta);
  2398. break;
  2399. default:
  2400. break;
  2401. }
  2402. }
  2403. static int ath9k_conf_tx(struct ieee80211_hw *hw, u16 queue,
  2404. const struct ieee80211_tx_queue_params *params)
  2405. {
  2406. struct ath_wiphy *aphy = hw->priv;
  2407. struct ath_softc *sc = aphy->sc;
  2408. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  2409. struct ath9k_tx_queue_info qi;
  2410. int ret = 0, qnum;
  2411. if (queue >= WME_NUM_AC)
  2412. return 0;
  2413. mutex_lock(&sc->mutex);
  2414. memset(&qi, 0, sizeof(struct ath9k_tx_queue_info));
  2415. qi.tqi_aifs = params->aifs;
  2416. qi.tqi_cwmin = params->cw_min;
  2417. qi.tqi_cwmax = params->cw_max;
  2418. qi.tqi_burstTime = params->txop;
  2419. qnum = ath_get_hal_qnum(queue, sc);
  2420. ath_print(common, ATH_DBG_CONFIG,
  2421. "Configure tx [queue/halq] [%d/%d], "
  2422. "aifs: %d, cw_min: %d, cw_max: %d, txop: %d\n",
  2423. queue, qnum, params->aifs, params->cw_min,
  2424. params->cw_max, params->txop);
  2425. ret = ath_txq_update(sc, qnum, &qi);
  2426. if (ret)
  2427. ath_print(common, ATH_DBG_FATAL, "TXQ Update failed\n");
  2428. if (sc->sc_ah->opmode == NL80211_IFTYPE_ADHOC)
  2429. if ((qnum == sc->tx.hwq_map[ATH9K_WME_AC_BE]) && !ret)
  2430. ath_beaconq_config(sc);
  2431. mutex_unlock(&sc->mutex);
  2432. return ret;
  2433. }
  2434. static int ath9k_set_key(struct ieee80211_hw *hw,
  2435. enum set_key_cmd cmd,
  2436. struct ieee80211_vif *vif,
  2437. struct ieee80211_sta *sta,
  2438. struct ieee80211_key_conf *key)
  2439. {
  2440. struct ath_wiphy *aphy = hw->priv;
  2441. struct ath_softc *sc = aphy->sc;
  2442. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  2443. int ret = 0;
  2444. if (modparam_nohwcrypt)
  2445. return -ENOSPC;
  2446. mutex_lock(&sc->mutex);
  2447. ath9k_ps_wakeup(sc);
  2448. ath_print(common, ATH_DBG_CONFIG, "Set HW Key\n");
  2449. switch (cmd) {
  2450. case SET_KEY:
  2451. ret = ath_key_config(common, vif, sta, key);
  2452. if (ret >= 0) {
  2453. key->hw_key_idx = ret;
  2454. /* push IV and Michael MIC generation to stack */
  2455. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  2456. if (key->alg == ALG_TKIP)
  2457. key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
  2458. if (sc->sc_ah->sw_mgmt_crypto && key->alg == ALG_CCMP)
  2459. key->flags |= IEEE80211_KEY_FLAG_SW_MGMT;
  2460. ret = 0;
  2461. }
  2462. break;
  2463. case DISABLE_KEY:
  2464. ath_key_delete(common, key);
  2465. break;
  2466. default:
  2467. ret = -EINVAL;
  2468. }
  2469. ath9k_ps_restore(sc);
  2470. mutex_unlock(&sc->mutex);
  2471. return ret;
  2472. }
  2473. static void ath9k_bss_info_changed(struct ieee80211_hw *hw,
  2474. struct ieee80211_vif *vif,
  2475. struct ieee80211_bss_conf *bss_conf,
  2476. u32 changed)
  2477. {
  2478. struct ath_wiphy *aphy = hw->priv;
  2479. struct ath_softc *sc = aphy->sc;
  2480. struct ath_hw *ah = sc->sc_ah;
  2481. struct ath_common *common = ath9k_hw_common(ah);
  2482. struct ath_vif *avp = (void *)vif->drv_priv;
  2483. int error;
  2484. mutex_lock(&sc->mutex);
  2485. if (changed & BSS_CHANGED_BSSID) {
  2486. /* Set BSSID */
  2487. memcpy(common->curbssid, bss_conf->bssid, ETH_ALEN);
  2488. memcpy(avp->bssid, bss_conf->bssid, ETH_ALEN);
  2489. common->curaid = 0;
  2490. ath9k_hw_write_associd(ah);
  2491. /* Set aggregation protection mode parameters */
  2492. sc->config.ath_aggr_prot = 0;
  2493. /* Only legacy IBSS for now */
  2494. if (vif->type == NL80211_IFTYPE_ADHOC)
  2495. ath_update_chainmask(sc, 0);
  2496. ath_print(common, ATH_DBG_CONFIG,
  2497. "BSSID: %pM aid: 0x%x\n",
  2498. common->curbssid, common->curaid);
  2499. /* need to reconfigure the beacon */
  2500. sc->sc_flags &= ~SC_OP_BEACONS ;
  2501. }
  2502. /* Enable transmission of beacons (AP, IBSS, MESH) */
  2503. if ((changed & BSS_CHANGED_BEACON) ||
  2504. ((changed & BSS_CHANGED_BEACON_ENABLED) && bss_conf->enable_beacon)) {
  2505. ath9k_hw_stoptxdma(sc->sc_ah, sc->beacon.beaconq);
  2506. error = ath_beacon_alloc(aphy, vif);
  2507. if (!error)
  2508. ath_beacon_config(sc, vif);
  2509. }
  2510. /* Disable transmission of beacons */
  2511. if ((changed & BSS_CHANGED_BEACON_ENABLED) && !bss_conf->enable_beacon)
  2512. ath9k_hw_stoptxdma(sc->sc_ah, sc->beacon.beaconq);
  2513. if (changed & BSS_CHANGED_BEACON_INT) {
  2514. sc->beacon_interval = bss_conf->beacon_int;
  2515. /*
  2516. * In case of AP mode, the HW TSF has to be reset
  2517. * when the beacon interval changes.
  2518. */
  2519. if (vif->type == NL80211_IFTYPE_AP) {
  2520. sc->sc_flags |= SC_OP_TSF_RESET;
  2521. ath9k_hw_stoptxdma(sc->sc_ah, sc->beacon.beaconq);
  2522. error = ath_beacon_alloc(aphy, vif);
  2523. if (!error)
  2524. ath_beacon_config(sc, vif);
  2525. } else {
  2526. ath_beacon_config(sc, vif);
  2527. }
  2528. }
  2529. if (changed & BSS_CHANGED_ERP_PREAMBLE) {
  2530. ath_print(common, ATH_DBG_CONFIG, "BSS Changed PREAMBLE %d\n",
  2531. bss_conf->use_short_preamble);
  2532. if (bss_conf->use_short_preamble)
  2533. sc->sc_flags |= SC_OP_PREAMBLE_SHORT;
  2534. else
  2535. sc->sc_flags &= ~SC_OP_PREAMBLE_SHORT;
  2536. }
  2537. if (changed & BSS_CHANGED_ERP_CTS_PROT) {
  2538. ath_print(common, ATH_DBG_CONFIG, "BSS Changed CTS PROT %d\n",
  2539. bss_conf->use_cts_prot);
  2540. if (bss_conf->use_cts_prot &&
  2541. hw->conf.channel->band != IEEE80211_BAND_5GHZ)
  2542. sc->sc_flags |= SC_OP_PROTECT_ENABLE;
  2543. else
  2544. sc->sc_flags &= ~SC_OP_PROTECT_ENABLE;
  2545. }
  2546. if (changed & BSS_CHANGED_ASSOC) {
  2547. ath_print(common, ATH_DBG_CONFIG, "BSS Changed ASSOC %d\n",
  2548. bss_conf->assoc);
  2549. ath9k_bss_assoc_info(sc, vif, bss_conf);
  2550. }
  2551. mutex_unlock(&sc->mutex);
  2552. }
  2553. static u64 ath9k_get_tsf(struct ieee80211_hw *hw)
  2554. {
  2555. u64 tsf;
  2556. struct ath_wiphy *aphy = hw->priv;
  2557. struct ath_softc *sc = aphy->sc;
  2558. mutex_lock(&sc->mutex);
  2559. tsf = ath9k_hw_gettsf64(sc->sc_ah);
  2560. mutex_unlock(&sc->mutex);
  2561. return tsf;
  2562. }
  2563. static void ath9k_set_tsf(struct ieee80211_hw *hw, u64 tsf)
  2564. {
  2565. struct ath_wiphy *aphy = hw->priv;
  2566. struct ath_softc *sc = aphy->sc;
  2567. mutex_lock(&sc->mutex);
  2568. ath9k_hw_settsf64(sc->sc_ah, tsf);
  2569. mutex_unlock(&sc->mutex);
  2570. }
  2571. static void ath9k_reset_tsf(struct ieee80211_hw *hw)
  2572. {
  2573. struct ath_wiphy *aphy = hw->priv;
  2574. struct ath_softc *sc = aphy->sc;
  2575. mutex_lock(&sc->mutex);
  2576. ath9k_ps_wakeup(sc);
  2577. ath9k_hw_reset_tsf(sc->sc_ah);
  2578. ath9k_ps_restore(sc);
  2579. mutex_unlock(&sc->mutex);
  2580. }
  2581. static int ath9k_ampdu_action(struct ieee80211_hw *hw,
  2582. struct ieee80211_vif *vif,
  2583. enum ieee80211_ampdu_mlme_action action,
  2584. struct ieee80211_sta *sta,
  2585. u16 tid, u16 *ssn)
  2586. {
  2587. struct ath_wiphy *aphy = hw->priv;
  2588. struct ath_softc *sc = aphy->sc;
  2589. int ret = 0;
  2590. switch (action) {
  2591. case IEEE80211_AMPDU_RX_START:
  2592. if (!(sc->sc_flags & SC_OP_RXAGGR))
  2593. ret = -ENOTSUPP;
  2594. break;
  2595. case IEEE80211_AMPDU_RX_STOP:
  2596. break;
  2597. case IEEE80211_AMPDU_TX_START:
  2598. ath_tx_aggr_start(sc, sta, tid, ssn);
  2599. ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  2600. break;
  2601. case IEEE80211_AMPDU_TX_STOP:
  2602. ath_tx_aggr_stop(sc, sta, tid);
  2603. ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  2604. break;
  2605. case IEEE80211_AMPDU_TX_OPERATIONAL:
  2606. ath_tx_aggr_resume(sc, sta, tid);
  2607. break;
  2608. default:
  2609. ath_print(ath9k_hw_common(sc->sc_ah), ATH_DBG_FATAL,
  2610. "Unknown AMPDU action\n");
  2611. }
  2612. return ret;
  2613. }
  2614. static void ath9k_sw_scan_start(struct ieee80211_hw *hw)
  2615. {
  2616. struct ath_wiphy *aphy = hw->priv;
  2617. struct ath_softc *sc = aphy->sc;
  2618. mutex_lock(&sc->mutex);
  2619. if (ath9k_wiphy_scanning(sc)) {
  2620. printk(KERN_DEBUG "ath9k: Two wiphys trying to scan at the "
  2621. "same time\n");
  2622. /*
  2623. * Do not allow the concurrent scanning state for now. This
  2624. * could be improved with scanning control moved into ath9k.
  2625. */
  2626. mutex_unlock(&sc->mutex);
  2627. return;
  2628. }
  2629. aphy->state = ATH_WIPHY_SCAN;
  2630. ath9k_wiphy_pause_all_forced(sc, aphy);
  2631. spin_lock_bh(&sc->ani_lock);
  2632. sc->sc_flags |= SC_OP_SCANNING;
  2633. spin_unlock_bh(&sc->ani_lock);
  2634. mutex_unlock(&sc->mutex);
  2635. }
  2636. static void ath9k_sw_scan_complete(struct ieee80211_hw *hw)
  2637. {
  2638. struct ath_wiphy *aphy = hw->priv;
  2639. struct ath_softc *sc = aphy->sc;
  2640. mutex_lock(&sc->mutex);
  2641. spin_lock_bh(&sc->ani_lock);
  2642. aphy->state = ATH_WIPHY_ACTIVE;
  2643. sc->sc_flags &= ~SC_OP_SCANNING;
  2644. sc->sc_flags |= SC_OP_FULL_RESET;
  2645. spin_unlock_bh(&sc->ani_lock);
  2646. ath_beacon_config(sc, NULL);
  2647. mutex_unlock(&sc->mutex);
  2648. }
  2649. struct ieee80211_ops ath9k_ops = {
  2650. .tx = ath9k_tx,
  2651. .start = ath9k_start,
  2652. .stop = ath9k_stop,
  2653. .add_interface = ath9k_add_interface,
  2654. .remove_interface = ath9k_remove_interface,
  2655. .config = ath9k_config,
  2656. .configure_filter = ath9k_configure_filter,
  2657. .sta_notify = ath9k_sta_notify,
  2658. .conf_tx = ath9k_conf_tx,
  2659. .bss_info_changed = ath9k_bss_info_changed,
  2660. .set_key = ath9k_set_key,
  2661. .get_tsf = ath9k_get_tsf,
  2662. .set_tsf = ath9k_set_tsf,
  2663. .reset_tsf = ath9k_reset_tsf,
  2664. .ampdu_action = ath9k_ampdu_action,
  2665. .sw_scan_start = ath9k_sw_scan_start,
  2666. .sw_scan_complete = ath9k_sw_scan_complete,
  2667. .rfkill_poll = ath9k_rfkill_poll_state,
  2668. };
  2669. static int __init ath9k_init(void)
  2670. {
  2671. int error;
  2672. /* Register rate control algorithm */
  2673. error = ath_rate_control_register();
  2674. if (error != 0) {
  2675. printk(KERN_ERR
  2676. "ath9k: Unable to register rate control "
  2677. "algorithm: %d\n",
  2678. error);
  2679. goto err_out;
  2680. }
  2681. error = ath9k_debug_create_root();
  2682. if (error) {
  2683. printk(KERN_ERR
  2684. "ath9k: Unable to create debugfs root: %d\n",
  2685. error);
  2686. goto err_rate_unregister;
  2687. }
  2688. error = ath_pci_init();
  2689. if (error < 0) {
  2690. printk(KERN_ERR
  2691. "ath9k: No PCI devices found, driver not installed.\n");
  2692. error = -ENODEV;
  2693. goto err_remove_root;
  2694. }
  2695. error = ath_ahb_init();
  2696. if (error < 0) {
  2697. error = -ENODEV;
  2698. goto err_pci_exit;
  2699. }
  2700. return 0;
  2701. err_pci_exit:
  2702. ath_pci_exit();
  2703. err_remove_root:
  2704. ath9k_debug_remove_root();
  2705. err_rate_unregister:
  2706. ath_rate_control_unregister();
  2707. err_out:
  2708. return error;
  2709. }
  2710. module_init(ath9k_init);
  2711. static void __exit ath9k_exit(void)
  2712. {
  2713. ath_ahb_exit();
  2714. ath_pci_exit();
  2715. ath9k_debug_remove_root();
  2716. ath_rate_control_unregister();
  2717. printk(KERN_INFO "%s: Driver unloaded\n", dev_info);
  2718. }
  2719. module_exit(ath9k_exit);