main.c 84 KB

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