main.c 83 KB

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