main.c 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673
  1. /*
  2. * Copyright (c) 2008-2011 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 <linux/delay.h>
  18. #include "ath9k.h"
  19. #include "btcoex.h"
  20. u8 ath9k_parse_mpdudensity(u8 mpdudensity)
  21. {
  22. /*
  23. * 802.11n D2.0 defined values for "Minimum MPDU Start Spacing":
  24. * 0 for no restriction
  25. * 1 for 1/4 us
  26. * 2 for 1/2 us
  27. * 3 for 1 us
  28. * 4 for 2 us
  29. * 5 for 4 us
  30. * 6 for 8 us
  31. * 7 for 16 us
  32. */
  33. switch (mpdudensity) {
  34. case 0:
  35. return 0;
  36. case 1:
  37. case 2:
  38. case 3:
  39. /* Our lower layer calculations limit our precision to
  40. 1 microsecond */
  41. return 1;
  42. case 4:
  43. return 2;
  44. case 5:
  45. return 4;
  46. case 6:
  47. return 8;
  48. case 7:
  49. return 16;
  50. default:
  51. return 0;
  52. }
  53. }
  54. static bool ath9k_has_pending_frames(struct ath_softc *sc, struct ath_txq *txq,
  55. bool sw_pending)
  56. {
  57. bool pending = false;
  58. spin_lock_bh(&txq->axq_lock);
  59. if (txq->axq_depth) {
  60. pending = true;
  61. goto out;
  62. }
  63. if (!sw_pending)
  64. goto out;
  65. if (txq->mac80211_qnum >= 0) {
  66. struct list_head *list;
  67. list = &sc->cur_chan->acq[txq->mac80211_qnum];
  68. if (!list_empty(list))
  69. pending = true;
  70. }
  71. out:
  72. spin_unlock_bh(&txq->axq_lock);
  73. return pending;
  74. }
  75. static bool ath9k_setpower(struct ath_softc *sc, enum ath9k_power_mode mode)
  76. {
  77. unsigned long flags;
  78. bool ret;
  79. spin_lock_irqsave(&sc->sc_pm_lock, flags);
  80. ret = ath9k_hw_setpower(sc->sc_ah, mode);
  81. spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
  82. return ret;
  83. }
  84. void ath_ps_full_sleep(unsigned long data)
  85. {
  86. struct ath_softc *sc = (struct ath_softc *) data;
  87. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  88. bool reset;
  89. spin_lock(&common->cc_lock);
  90. ath_hw_cycle_counters_update(common);
  91. spin_unlock(&common->cc_lock);
  92. ath9k_hw_setrxabort(sc->sc_ah, 1);
  93. ath9k_hw_stopdmarecv(sc->sc_ah, &reset);
  94. ath9k_hw_setpower(sc->sc_ah, ATH9K_PM_FULL_SLEEP);
  95. }
  96. void ath9k_ps_wakeup(struct ath_softc *sc)
  97. {
  98. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  99. unsigned long flags;
  100. enum ath9k_power_mode power_mode;
  101. spin_lock_irqsave(&sc->sc_pm_lock, flags);
  102. if (++sc->ps_usecount != 1)
  103. goto unlock;
  104. del_timer_sync(&sc->sleep_timer);
  105. power_mode = sc->sc_ah->power_mode;
  106. ath9k_hw_setpower(sc->sc_ah, ATH9K_PM_AWAKE);
  107. /*
  108. * While the hardware is asleep, the cycle counters contain no
  109. * useful data. Better clear them now so that they don't mess up
  110. * survey data results.
  111. */
  112. if (power_mode != ATH9K_PM_AWAKE) {
  113. spin_lock(&common->cc_lock);
  114. ath_hw_cycle_counters_update(common);
  115. memset(&common->cc_survey, 0, sizeof(common->cc_survey));
  116. memset(&common->cc_ani, 0, sizeof(common->cc_ani));
  117. spin_unlock(&common->cc_lock);
  118. }
  119. unlock:
  120. spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
  121. }
  122. void ath9k_ps_restore(struct ath_softc *sc)
  123. {
  124. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  125. enum ath9k_power_mode mode;
  126. unsigned long flags;
  127. spin_lock_irqsave(&sc->sc_pm_lock, flags);
  128. if (--sc->ps_usecount != 0)
  129. goto unlock;
  130. if (sc->ps_idle) {
  131. mod_timer(&sc->sleep_timer, jiffies + HZ / 10);
  132. goto unlock;
  133. }
  134. if (sc->ps_enabled &&
  135. !(sc->ps_flags & (PS_WAIT_FOR_BEACON |
  136. PS_WAIT_FOR_CAB |
  137. PS_WAIT_FOR_PSPOLL_DATA |
  138. PS_WAIT_FOR_TX_ACK |
  139. PS_WAIT_FOR_ANI))) {
  140. mode = ATH9K_PM_NETWORK_SLEEP;
  141. if (ath9k_hw_btcoex_is_enabled(sc->sc_ah))
  142. ath9k_btcoex_stop_gen_timer(sc);
  143. } else {
  144. goto unlock;
  145. }
  146. spin_lock(&common->cc_lock);
  147. ath_hw_cycle_counters_update(common);
  148. spin_unlock(&common->cc_lock);
  149. ath9k_hw_setpower(sc->sc_ah, mode);
  150. unlock:
  151. spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
  152. }
  153. static void __ath_cancel_work(struct ath_softc *sc)
  154. {
  155. cancel_work_sync(&sc->paprd_work);
  156. cancel_delayed_work_sync(&sc->tx_complete_work);
  157. cancel_delayed_work_sync(&sc->hw_pll_work);
  158. #ifdef CONFIG_ATH9K_BTCOEX_SUPPORT
  159. if (ath9k_hw_mci_is_enabled(sc->sc_ah))
  160. cancel_work_sync(&sc->mci_work);
  161. #endif
  162. }
  163. void ath_cancel_work(struct ath_softc *sc)
  164. {
  165. __ath_cancel_work(sc);
  166. cancel_work_sync(&sc->hw_reset_work);
  167. }
  168. void ath_restart_work(struct ath_softc *sc)
  169. {
  170. ieee80211_queue_delayed_work(sc->hw, &sc->tx_complete_work, 0);
  171. if (AR_SREV_9340(sc->sc_ah) || AR_SREV_9330(sc->sc_ah))
  172. ieee80211_queue_delayed_work(sc->hw, &sc->hw_pll_work,
  173. msecs_to_jiffies(ATH_PLL_WORK_INTERVAL));
  174. ath_start_ani(sc);
  175. }
  176. static bool ath_prepare_reset(struct ath_softc *sc)
  177. {
  178. struct ath_hw *ah = sc->sc_ah;
  179. bool ret = true;
  180. ieee80211_stop_queues(sc->hw);
  181. ath_stop_ani(sc);
  182. ath9k_hw_disable_interrupts(ah);
  183. if (AR_SREV_9300_20_OR_LATER(ah)) {
  184. ret &= ath_stoprecv(sc);
  185. ret &= ath_drain_all_txq(sc);
  186. } else {
  187. ret &= ath_drain_all_txq(sc);
  188. ret &= ath_stoprecv(sc);
  189. }
  190. return ret;
  191. }
  192. static bool ath_complete_reset(struct ath_softc *sc, bool start)
  193. {
  194. struct ath_hw *ah = sc->sc_ah;
  195. struct ath_common *common = ath9k_hw_common(ah);
  196. unsigned long flags;
  197. ath9k_calculate_summary_state(sc, sc->cur_chan);
  198. ath_startrecv(sc);
  199. ath9k_cmn_update_txpow(ah, sc->cur_chan->cur_txpower,
  200. sc->cur_chan->txpower,
  201. &sc->cur_chan->cur_txpower);
  202. clear_bit(ATH_OP_HW_RESET, &common->op_flags);
  203. if (!sc->cur_chan->offchannel && start) {
  204. /* restore per chanctx TSF timer */
  205. if (sc->cur_chan->tsf_val) {
  206. u32 offset;
  207. offset = ath9k_hw_get_tsf_offset(&sc->cur_chan->tsf_ts,
  208. NULL);
  209. ath9k_hw_settsf64(ah, sc->cur_chan->tsf_val + offset);
  210. }
  211. if (!test_bit(ATH_OP_BEACONS, &common->op_flags))
  212. goto work;
  213. if (ah->opmode == NL80211_IFTYPE_STATION &&
  214. test_bit(ATH_OP_PRIM_STA_VIF, &common->op_flags)) {
  215. spin_lock_irqsave(&sc->sc_pm_lock, flags);
  216. sc->ps_flags |= PS_BEACON_SYNC | PS_WAIT_FOR_BEACON;
  217. spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
  218. } else {
  219. ath9k_set_beacon(sc);
  220. }
  221. work:
  222. ath_restart_work(sc);
  223. ath_txq_schedule_all(sc);
  224. }
  225. sc->gtt_cnt = 0;
  226. ath9k_hw_set_interrupts(ah);
  227. ath9k_hw_enable_interrupts(ah);
  228. ieee80211_wake_queues(sc->hw);
  229. ath9k_p2p_ps_timer(sc);
  230. return true;
  231. }
  232. static int ath_reset_internal(struct ath_softc *sc, struct ath9k_channel *hchan)
  233. {
  234. struct ath_hw *ah = sc->sc_ah;
  235. struct ath_common *common = ath9k_hw_common(ah);
  236. struct ath9k_hw_cal_data *caldata = NULL;
  237. bool fastcc = true;
  238. int r;
  239. __ath_cancel_work(sc);
  240. disable_irq(sc->irq);
  241. tasklet_disable(&sc->intr_tq);
  242. tasklet_disable(&sc->bcon_tasklet);
  243. spin_lock_bh(&sc->sc_pcu_lock);
  244. if (!sc->cur_chan->offchannel) {
  245. fastcc = false;
  246. caldata = &sc->cur_chan->caldata;
  247. }
  248. if (!hchan) {
  249. fastcc = false;
  250. hchan = ah->curchan;
  251. }
  252. if (!ath_prepare_reset(sc))
  253. fastcc = false;
  254. if (ath9k_is_chanctx_enabled())
  255. fastcc = false;
  256. spin_lock_bh(&sc->chan_lock);
  257. sc->cur_chandef = sc->cur_chan->chandef;
  258. spin_unlock_bh(&sc->chan_lock);
  259. ath_dbg(common, CONFIG, "Reset to %u MHz, HT40: %d fastcc: %d\n",
  260. hchan->channel, IS_CHAN_HT40(hchan), fastcc);
  261. r = ath9k_hw_reset(ah, hchan, caldata, fastcc);
  262. if (r) {
  263. ath_err(common,
  264. "Unable to reset channel, reset status %d\n", r);
  265. ath9k_hw_enable_interrupts(ah);
  266. ath9k_queue_reset(sc, RESET_TYPE_BB_HANG);
  267. goto out;
  268. }
  269. if (ath9k_hw_mci_is_enabled(sc->sc_ah) &&
  270. sc->cur_chan->offchannel)
  271. ath9k_mci_set_txpower(sc, true, false);
  272. if (!ath_complete_reset(sc, true))
  273. r = -EIO;
  274. out:
  275. enable_irq(sc->irq);
  276. spin_unlock_bh(&sc->sc_pcu_lock);
  277. tasklet_enable(&sc->bcon_tasklet);
  278. tasklet_enable(&sc->intr_tq);
  279. return r;
  280. }
  281. static void ath_node_attach(struct ath_softc *sc, struct ieee80211_sta *sta,
  282. struct ieee80211_vif *vif)
  283. {
  284. struct ath_node *an;
  285. an = (struct ath_node *)sta->drv_priv;
  286. an->sc = sc;
  287. an->sta = sta;
  288. an->vif = vif;
  289. memset(&an->key_idx, 0, sizeof(an->key_idx));
  290. ath_tx_node_init(sc, an);
  291. ath_dynack_node_init(sc->sc_ah, an);
  292. }
  293. static void ath_node_detach(struct ath_softc *sc, struct ieee80211_sta *sta)
  294. {
  295. struct ath_node *an = (struct ath_node *)sta->drv_priv;
  296. ath_tx_node_cleanup(sc, an);
  297. ath_dynack_node_deinit(sc->sc_ah, an);
  298. }
  299. void ath9k_tasklet(unsigned long data)
  300. {
  301. struct ath_softc *sc = (struct ath_softc *)data;
  302. struct ath_hw *ah = sc->sc_ah;
  303. struct ath_common *common = ath9k_hw_common(ah);
  304. enum ath_reset_type type;
  305. unsigned long flags;
  306. u32 status = sc->intrstatus;
  307. u32 rxmask;
  308. ath9k_ps_wakeup(sc);
  309. spin_lock(&sc->sc_pcu_lock);
  310. if (status & ATH9K_INT_FATAL) {
  311. type = RESET_TYPE_FATAL_INT;
  312. ath9k_queue_reset(sc, type);
  313. /*
  314. * Increment the ref. counter here so that
  315. * interrupts are enabled in the reset routine.
  316. */
  317. atomic_inc(&ah->intr_ref_cnt);
  318. ath_dbg(common, RESET, "FATAL: Skipping interrupts\n");
  319. goto out;
  320. }
  321. if ((ah->config.hw_hang_checks & HW_BB_WATCHDOG) &&
  322. (status & ATH9K_INT_BB_WATCHDOG)) {
  323. spin_lock(&common->cc_lock);
  324. ath_hw_cycle_counters_update(common);
  325. ar9003_hw_bb_watchdog_dbg_info(ah);
  326. spin_unlock(&common->cc_lock);
  327. if (ar9003_hw_bb_watchdog_check(ah)) {
  328. type = RESET_TYPE_BB_WATCHDOG;
  329. ath9k_queue_reset(sc, type);
  330. /*
  331. * Increment the ref. counter here so that
  332. * interrupts are enabled in the reset routine.
  333. */
  334. atomic_inc(&ah->intr_ref_cnt);
  335. ath_dbg(common, RESET,
  336. "BB_WATCHDOG: Skipping interrupts\n");
  337. goto out;
  338. }
  339. }
  340. if (status & ATH9K_INT_GTT) {
  341. sc->gtt_cnt++;
  342. if ((sc->gtt_cnt >= MAX_GTT_CNT) && !ath9k_hw_check_alive(ah)) {
  343. type = RESET_TYPE_TX_GTT;
  344. ath9k_queue_reset(sc, type);
  345. atomic_inc(&ah->intr_ref_cnt);
  346. ath_dbg(common, RESET,
  347. "GTT: Skipping interrupts\n");
  348. goto out;
  349. }
  350. }
  351. spin_lock_irqsave(&sc->sc_pm_lock, flags);
  352. if ((status & ATH9K_INT_TSFOOR) && sc->ps_enabled) {
  353. /*
  354. * TSF sync does not look correct; remain awake to sync with
  355. * the next Beacon.
  356. */
  357. ath_dbg(common, PS, "TSFOOR - Sync with next Beacon\n");
  358. sc->ps_flags |= PS_WAIT_FOR_BEACON | PS_BEACON_SYNC;
  359. }
  360. spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
  361. if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
  362. rxmask = (ATH9K_INT_RXHP | ATH9K_INT_RXLP | ATH9K_INT_RXEOL |
  363. ATH9K_INT_RXORN);
  364. else
  365. rxmask = (ATH9K_INT_RX | ATH9K_INT_RXEOL | ATH9K_INT_RXORN);
  366. if (status & rxmask) {
  367. /* Check for high priority Rx first */
  368. if ((ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) &&
  369. (status & ATH9K_INT_RXHP))
  370. ath_rx_tasklet(sc, 0, true);
  371. ath_rx_tasklet(sc, 0, false);
  372. }
  373. if (status & ATH9K_INT_TX) {
  374. if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
  375. /*
  376. * For EDMA chips, TX completion is enabled for the
  377. * beacon queue, so if a beacon has been transmitted
  378. * successfully after a GTT interrupt, the GTT counter
  379. * gets reset to zero here.
  380. */
  381. sc->gtt_cnt = 0;
  382. ath_tx_edma_tasklet(sc);
  383. } else {
  384. ath_tx_tasklet(sc);
  385. }
  386. wake_up(&sc->tx_wait);
  387. }
  388. if (status & ATH9K_INT_GENTIMER)
  389. ath_gen_timer_isr(sc->sc_ah);
  390. ath9k_btcoex_handle_interrupt(sc, status);
  391. /* re-enable hardware interrupt */
  392. ath9k_hw_enable_interrupts(ah);
  393. out:
  394. spin_unlock(&sc->sc_pcu_lock);
  395. ath9k_ps_restore(sc);
  396. }
  397. irqreturn_t ath_isr(int irq, void *dev)
  398. {
  399. #define SCHED_INTR ( \
  400. ATH9K_INT_FATAL | \
  401. ATH9K_INT_BB_WATCHDOG | \
  402. ATH9K_INT_RXORN | \
  403. ATH9K_INT_RXEOL | \
  404. ATH9K_INT_RX | \
  405. ATH9K_INT_RXLP | \
  406. ATH9K_INT_RXHP | \
  407. ATH9K_INT_TX | \
  408. ATH9K_INT_BMISS | \
  409. ATH9K_INT_CST | \
  410. ATH9K_INT_GTT | \
  411. ATH9K_INT_TSFOOR | \
  412. ATH9K_INT_GENTIMER | \
  413. ATH9K_INT_MCI)
  414. struct ath_softc *sc = dev;
  415. struct ath_hw *ah = sc->sc_ah;
  416. struct ath_common *common = ath9k_hw_common(ah);
  417. enum ath9k_int status;
  418. u32 sync_cause = 0;
  419. bool sched = false;
  420. /*
  421. * The hardware is not ready/present, don't
  422. * touch anything. Note this can happen early
  423. * on if the IRQ is shared.
  424. */
  425. if (!ah || test_bit(ATH_OP_INVALID, &common->op_flags))
  426. return IRQ_NONE;
  427. /* shared irq, not for us */
  428. if (!ath9k_hw_intrpend(ah))
  429. return IRQ_NONE;
  430. /*
  431. * Figure out the reason(s) for the interrupt. Note
  432. * that the hal returns a pseudo-ISR that may include
  433. * bits we haven't explicitly enabled so we mask the
  434. * value to insure we only process bits we requested.
  435. */
  436. ath9k_hw_getisr(ah, &status, &sync_cause); /* NB: clears ISR too */
  437. ath9k_debug_sync_cause(sc, sync_cause);
  438. status &= ah->imask; /* discard unasked-for bits */
  439. if (test_bit(ATH_OP_HW_RESET, &common->op_flags))
  440. return IRQ_HANDLED;
  441. /*
  442. * If there are no status bits set, then this interrupt was not
  443. * for me (should have been caught above).
  444. */
  445. if (!status)
  446. return IRQ_NONE;
  447. /* Cache the status */
  448. sc->intrstatus = status;
  449. if (status & SCHED_INTR)
  450. sched = true;
  451. /*
  452. * If a FATAL interrupt is received, we have to reset the chip
  453. * immediately.
  454. */
  455. if (status & ATH9K_INT_FATAL)
  456. goto chip_reset;
  457. if ((ah->config.hw_hang_checks & HW_BB_WATCHDOG) &&
  458. (status & ATH9K_INT_BB_WATCHDOG))
  459. goto chip_reset;
  460. if (status & ATH9K_INT_SWBA)
  461. tasklet_schedule(&sc->bcon_tasklet);
  462. if (status & ATH9K_INT_TXURN)
  463. ath9k_hw_updatetxtriglevel(ah, true);
  464. if (status & ATH9K_INT_RXEOL) {
  465. ah->imask &= ~(ATH9K_INT_RXEOL | ATH9K_INT_RXORN);
  466. ath9k_hw_set_interrupts(ah);
  467. }
  468. if (!(ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP))
  469. if (status & ATH9K_INT_TIM_TIMER) {
  470. if (ATH_DBG_WARN_ON_ONCE(sc->ps_idle))
  471. goto chip_reset;
  472. /* Clear RxAbort bit so that we can
  473. * receive frames */
  474. ath9k_setpower(sc, ATH9K_PM_AWAKE);
  475. spin_lock(&sc->sc_pm_lock);
  476. ath9k_hw_setrxabort(sc->sc_ah, 0);
  477. sc->ps_flags |= PS_WAIT_FOR_BEACON;
  478. spin_unlock(&sc->sc_pm_lock);
  479. }
  480. chip_reset:
  481. ath_debug_stat_interrupt(sc, status);
  482. if (sched) {
  483. /* turn off every interrupt */
  484. ath9k_hw_disable_interrupts(ah);
  485. tasklet_schedule(&sc->intr_tq);
  486. }
  487. return IRQ_HANDLED;
  488. #undef SCHED_INTR
  489. }
  490. /*
  491. * This function is called when a HW reset cannot be deferred
  492. * and has to be immediate.
  493. */
  494. int ath_reset(struct ath_softc *sc, struct ath9k_channel *hchan)
  495. {
  496. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  497. int r;
  498. ath9k_hw_kill_interrupts(sc->sc_ah);
  499. set_bit(ATH_OP_HW_RESET, &common->op_flags);
  500. ath9k_ps_wakeup(sc);
  501. r = ath_reset_internal(sc, hchan);
  502. ath9k_ps_restore(sc);
  503. return r;
  504. }
  505. /*
  506. * When a HW reset can be deferred, it is added to the
  507. * hw_reset_work workqueue, but we set ATH_OP_HW_RESET before
  508. * queueing.
  509. */
  510. void ath9k_queue_reset(struct ath_softc *sc, enum ath_reset_type type)
  511. {
  512. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  513. #ifdef CONFIG_ATH9K_DEBUGFS
  514. RESET_STAT_INC(sc, type);
  515. #endif
  516. ath9k_hw_kill_interrupts(sc->sc_ah);
  517. set_bit(ATH_OP_HW_RESET, &common->op_flags);
  518. ieee80211_queue_work(sc->hw, &sc->hw_reset_work);
  519. }
  520. void ath_reset_work(struct work_struct *work)
  521. {
  522. struct ath_softc *sc = container_of(work, struct ath_softc, hw_reset_work);
  523. ath9k_ps_wakeup(sc);
  524. ath_reset_internal(sc, NULL);
  525. ath9k_ps_restore(sc);
  526. }
  527. /**********************/
  528. /* mac80211 callbacks */
  529. /**********************/
  530. static int ath9k_start(struct ieee80211_hw *hw)
  531. {
  532. struct ath_softc *sc = hw->priv;
  533. struct ath_hw *ah = sc->sc_ah;
  534. struct ath_common *common = ath9k_hw_common(ah);
  535. struct ieee80211_channel *curchan = sc->cur_chan->chandef.chan;
  536. struct ath_chanctx *ctx = sc->cur_chan;
  537. struct ath9k_channel *init_channel;
  538. int r;
  539. ath_dbg(common, CONFIG,
  540. "Starting driver with initial channel: %d MHz\n",
  541. curchan->center_freq);
  542. ath9k_ps_wakeup(sc);
  543. mutex_lock(&sc->mutex);
  544. init_channel = ath9k_cmn_get_channel(hw, ah, &ctx->chandef);
  545. sc->cur_chandef = hw->conf.chandef;
  546. /* Reset SERDES registers */
  547. ath9k_hw_configpcipowersave(ah, false);
  548. /*
  549. * The basic interface to setting the hardware in a good
  550. * state is ``reset''. On return the hardware is known to
  551. * be powered up and with interrupts disabled. This must
  552. * be followed by initialization of the appropriate bits
  553. * and then setup of the interrupt mask.
  554. */
  555. spin_lock_bh(&sc->sc_pcu_lock);
  556. atomic_set(&ah->intr_ref_cnt, -1);
  557. r = ath9k_hw_reset(ah, init_channel, ah->caldata, false);
  558. if (r) {
  559. ath_err(common,
  560. "Unable to reset hardware; reset status %d (freq %u MHz)\n",
  561. r, curchan->center_freq);
  562. ah->reset_power_on = false;
  563. }
  564. /* Setup our intr mask. */
  565. ah->imask = ATH9K_INT_TX | ATH9K_INT_RXEOL |
  566. ATH9K_INT_RXORN | ATH9K_INT_FATAL |
  567. ATH9K_INT_GLOBAL;
  568. if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
  569. ah->imask |= ATH9K_INT_RXHP |
  570. ATH9K_INT_RXLP;
  571. else
  572. ah->imask |= ATH9K_INT_RX;
  573. if (ah->config.hw_hang_checks & HW_BB_WATCHDOG)
  574. ah->imask |= ATH9K_INT_BB_WATCHDOG;
  575. /*
  576. * Enable GTT interrupts only for AR9003/AR9004 chips
  577. * for now.
  578. */
  579. if (AR_SREV_9300_20_OR_LATER(ah))
  580. ah->imask |= ATH9K_INT_GTT;
  581. if (ah->caps.hw_caps & ATH9K_HW_CAP_HT)
  582. ah->imask |= ATH9K_INT_CST;
  583. ath_mci_enable(sc);
  584. clear_bit(ATH_OP_INVALID, &common->op_flags);
  585. sc->sc_ah->is_monitoring = false;
  586. if (!ath_complete_reset(sc, false))
  587. ah->reset_power_on = false;
  588. if (ah->led_pin >= 0) {
  589. ath9k_hw_cfg_output(ah, ah->led_pin,
  590. AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
  591. ath9k_hw_set_gpio(ah, ah->led_pin,
  592. (ah->config.led_active_high) ? 1 : 0);
  593. }
  594. /*
  595. * Reset key cache to sane defaults (all entries cleared) instead of
  596. * semi-random values after suspend/resume.
  597. */
  598. ath9k_cmn_init_crypto(sc->sc_ah);
  599. ath9k_hw_reset_tsf(ah);
  600. spin_unlock_bh(&sc->sc_pcu_lock);
  601. mutex_unlock(&sc->mutex);
  602. ath9k_ps_restore(sc);
  603. ath9k_rng_start(sc);
  604. return 0;
  605. }
  606. static void ath9k_tx(struct ieee80211_hw *hw,
  607. struct ieee80211_tx_control *control,
  608. struct sk_buff *skb)
  609. {
  610. struct ath_softc *sc = hw->priv;
  611. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  612. struct ath_tx_control txctl;
  613. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  614. unsigned long flags;
  615. if (sc->ps_enabled) {
  616. /*
  617. * mac80211 does not set PM field for normal data frames, so we
  618. * need to update that based on the current PS mode.
  619. */
  620. if (ieee80211_is_data(hdr->frame_control) &&
  621. !ieee80211_is_nullfunc(hdr->frame_control) &&
  622. !ieee80211_has_pm(hdr->frame_control)) {
  623. ath_dbg(common, PS,
  624. "Add PM=1 for a TX frame while in PS mode\n");
  625. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
  626. }
  627. }
  628. if (unlikely(sc->sc_ah->power_mode == ATH9K_PM_NETWORK_SLEEP)) {
  629. /*
  630. * We are using PS-Poll and mac80211 can request TX while in
  631. * power save mode. Need to wake up hardware for the TX to be
  632. * completed and if needed, also for RX of buffered frames.
  633. */
  634. ath9k_ps_wakeup(sc);
  635. spin_lock_irqsave(&sc->sc_pm_lock, flags);
  636. if (!(sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP))
  637. ath9k_hw_setrxabort(sc->sc_ah, 0);
  638. if (ieee80211_is_pspoll(hdr->frame_control)) {
  639. ath_dbg(common, PS,
  640. "Sending PS-Poll to pick a buffered frame\n");
  641. sc->ps_flags |= PS_WAIT_FOR_PSPOLL_DATA;
  642. } else {
  643. ath_dbg(common, PS, "Wake up to complete TX\n");
  644. sc->ps_flags |= PS_WAIT_FOR_TX_ACK;
  645. }
  646. /*
  647. * The actual restore operation will happen only after
  648. * the ps_flags bit is cleared. We are just dropping
  649. * the ps_usecount here.
  650. */
  651. spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
  652. ath9k_ps_restore(sc);
  653. }
  654. /*
  655. * Cannot tx while the hardware is in full sleep, it first needs a full
  656. * chip reset to recover from that
  657. */
  658. if (unlikely(sc->sc_ah->power_mode == ATH9K_PM_FULL_SLEEP)) {
  659. ath_err(common, "TX while HW is in FULL_SLEEP mode\n");
  660. goto exit;
  661. }
  662. memset(&txctl, 0, sizeof(struct ath_tx_control));
  663. txctl.txq = sc->tx.txq_map[skb_get_queue_mapping(skb)];
  664. txctl.sta = control->sta;
  665. ath_dbg(common, XMIT, "transmitting packet, skb: %p\n", skb);
  666. if (ath_tx_start(hw, skb, &txctl) != 0) {
  667. ath_dbg(common, XMIT, "TX failed\n");
  668. TX_STAT_INC(txctl.txq->axq_qnum, txfailed);
  669. goto exit;
  670. }
  671. return;
  672. exit:
  673. ieee80211_free_txskb(hw, skb);
  674. }
  675. static void ath9k_stop(struct ieee80211_hw *hw)
  676. {
  677. struct ath_softc *sc = hw->priv;
  678. struct ath_hw *ah = sc->sc_ah;
  679. struct ath_common *common = ath9k_hw_common(ah);
  680. bool prev_idle;
  681. ath9k_deinit_channel_context(sc);
  682. ath9k_rng_stop(sc);
  683. mutex_lock(&sc->mutex);
  684. ath_cancel_work(sc);
  685. if (test_bit(ATH_OP_INVALID, &common->op_flags)) {
  686. ath_dbg(common, ANY, "Device not present\n");
  687. mutex_unlock(&sc->mutex);
  688. return;
  689. }
  690. /* Ensure HW is awake when we try to shut it down. */
  691. ath9k_ps_wakeup(sc);
  692. spin_lock_bh(&sc->sc_pcu_lock);
  693. /* prevent tasklets to enable interrupts once we disable them */
  694. ah->imask &= ~ATH9K_INT_GLOBAL;
  695. /* make sure h/w will not generate any interrupt
  696. * before setting the invalid flag. */
  697. ath9k_hw_disable_interrupts(ah);
  698. spin_unlock_bh(&sc->sc_pcu_lock);
  699. /* we can now sync irq and kill any running tasklets, since we already
  700. * disabled interrupts and not holding a spin lock */
  701. synchronize_irq(sc->irq);
  702. tasklet_kill(&sc->intr_tq);
  703. tasklet_kill(&sc->bcon_tasklet);
  704. prev_idle = sc->ps_idle;
  705. sc->ps_idle = true;
  706. spin_lock_bh(&sc->sc_pcu_lock);
  707. if (ah->led_pin >= 0) {
  708. ath9k_hw_set_gpio(ah, ah->led_pin,
  709. (ah->config.led_active_high) ? 0 : 1);
  710. ath9k_hw_cfg_gpio_input(ah, ah->led_pin);
  711. }
  712. ath_prepare_reset(sc);
  713. if (sc->rx.frag) {
  714. dev_kfree_skb_any(sc->rx.frag);
  715. sc->rx.frag = NULL;
  716. }
  717. if (!ah->curchan)
  718. ah->curchan = ath9k_cmn_get_channel(hw, ah,
  719. &sc->cur_chan->chandef);
  720. ath9k_hw_reset(ah, ah->curchan, ah->caldata, false);
  721. set_bit(ATH_OP_INVALID, &common->op_flags);
  722. ath9k_hw_phy_disable(ah);
  723. ath9k_hw_configpcipowersave(ah, true);
  724. spin_unlock_bh(&sc->sc_pcu_lock);
  725. ath9k_ps_restore(sc);
  726. sc->ps_idle = prev_idle;
  727. mutex_unlock(&sc->mutex);
  728. ath_dbg(common, CONFIG, "Driver halt\n");
  729. }
  730. static bool ath9k_uses_beacons(int type)
  731. {
  732. switch (type) {
  733. case NL80211_IFTYPE_AP:
  734. case NL80211_IFTYPE_ADHOC:
  735. case NL80211_IFTYPE_MESH_POINT:
  736. return true;
  737. default:
  738. return false;
  739. }
  740. }
  741. static void ath9k_vif_iter(struct ath9k_vif_iter_data *iter_data,
  742. u8 *mac, struct ieee80211_vif *vif)
  743. {
  744. struct ath_vif *avp = (struct ath_vif *)vif->drv_priv;
  745. int i;
  746. if (iter_data->has_hw_macaddr) {
  747. for (i = 0; i < ETH_ALEN; i++)
  748. iter_data->mask[i] &=
  749. ~(iter_data->hw_macaddr[i] ^ mac[i]);
  750. } else {
  751. memcpy(iter_data->hw_macaddr, mac, ETH_ALEN);
  752. iter_data->has_hw_macaddr = true;
  753. }
  754. if (!vif->bss_conf.use_short_slot)
  755. iter_data->slottime = ATH9K_SLOT_TIME_20;
  756. switch (vif->type) {
  757. case NL80211_IFTYPE_AP:
  758. iter_data->naps++;
  759. break;
  760. case NL80211_IFTYPE_STATION:
  761. iter_data->nstations++;
  762. if (avp->assoc && !iter_data->primary_sta)
  763. iter_data->primary_sta = vif;
  764. break;
  765. case NL80211_IFTYPE_OCB:
  766. iter_data->nocbs++;
  767. break;
  768. case NL80211_IFTYPE_ADHOC:
  769. iter_data->nadhocs++;
  770. if (vif->bss_conf.enable_beacon)
  771. iter_data->beacons = true;
  772. break;
  773. case NL80211_IFTYPE_MESH_POINT:
  774. iter_data->nmeshes++;
  775. if (vif->bss_conf.enable_beacon)
  776. iter_data->beacons = true;
  777. break;
  778. case NL80211_IFTYPE_WDS:
  779. iter_data->nwds++;
  780. break;
  781. default:
  782. break;
  783. }
  784. }
  785. static void ath9k_update_bssid_mask(struct ath_softc *sc,
  786. struct ath_chanctx *ctx,
  787. struct ath9k_vif_iter_data *iter_data)
  788. {
  789. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  790. struct ath_vif *avp;
  791. int i;
  792. if (!ath9k_is_chanctx_enabled())
  793. return;
  794. list_for_each_entry(avp, &ctx->vifs, list) {
  795. if (ctx->nvifs_assigned != 1)
  796. continue;
  797. if (!avp->vif->p2p || !iter_data->has_hw_macaddr)
  798. continue;
  799. ether_addr_copy(common->curbssid, avp->bssid);
  800. /* perm_addr will be used as the p2p device address. */
  801. for (i = 0; i < ETH_ALEN; i++)
  802. iter_data->mask[i] &=
  803. ~(iter_data->hw_macaddr[i] ^
  804. sc->hw->wiphy->perm_addr[i]);
  805. }
  806. }
  807. /* Called with sc->mutex held. */
  808. void ath9k_calculate_iter_data(struct ath_softc *sc,
  809. struct ath_chanctx *ctx,
  810. struct ath9k_vif_iter_data *iter_data)
  811. {
  812. struct ath_vif *avp;
  813. /*
  814. * The hardware will use primary station addr together with the
  815. * BSSID mask when matching addresses.
  816. */
  817. memset(iter_data, 0, sizeof(*iter_data));
  818. eth_broadcast_addr(iter_data->mask);
  819. iter_data->slottime = ATH9K_SLOT_TIME_9;
  820. list_for_each_entry(avp, &ctx->vifs, list)
  821. ath9k_vif_iter(iter_data, avp->vif->addr, avp->vif);
  822. ath9k_update_bssid_mask(sc, ctx, iter_data);
  823. }
  824. static void ath9k_set_assoc_state(struct ath_softc *sc,
  825. struct ieee80211_vif *vif, bool changed)
  826. {
  827. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  828. struct ath_vif *avp = (struct ath_vif *)vif->drv_priv;
  829. unsigned long flags;
  830. set_bit(ATH_OP_PRIM_STA_VIF, &common->op_flags);
  831. ether_addr_copy(common->curbssid, avp->bssid);
  832. common->curaid = avp->aid;
  833. ath9k_hw_write_associd(sc->sc_ah);
  834. if (changed) {
  835. common->last_rssi = ATH_RSSI_DUMMY_MARKER;
  836. sc->sc_ah->stats.avgbrssi = ATH_RSSI_DUMMY_MARKER;
  837. spin_lock_irqsave(&sc->sc_pm_lock, flags);
  838. sc->ps_flags |= PS_BEACON_SYNC | PS_WAIT_FOR_BEACON;
  839. spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
  840. }
  841. if (ath9k_hw_mci_is_enabled(sc->sc_ah))
  842. ath9k_mci_update_wlan_channels(sc, false);
  843. ath_dbg(common, CONFIG,
  844. "Primary Station interface: %pM, BSSID: %pM\n",
  845. vif->addr, common->curbssid);
  846. }
  847. #ifdef CONFIG_ATH9K_CHANNEL_CONTEXT
  848. static void ath9k_set_offchannel_state(struct ath_softc *sc)
  849. {
  850. struct ath_hw *ah = sc->sc_ah;
  851. struct ath_common *common = ath9k_hw_common(ah);
  852. struct ieee80211_vif *vif = NULL;
  853. ath9k_ps_wakeup(sc);
  854. if (sc->offchannel.state < ATH_OFFCHANNEL_ROC_START)
  855. vif = sc->offchannel.scan_vif;
  856. else
  857. vif = sc->offchannel.roc_vif;
  858. if (WARN_ON(!vif))
  859. goto exit;
  860. eth_zero_addr(common->curbssid);
  861. eth_broadcast_addr(common->bssidmask);
  862. memcpy(common->macaddr, vif->addr, ETH_ALEN);
  863. common->curaid = 0;
  864. ah->opmode = vif->type;
  865. ah->imask &= ~ATH9K_INT_SWBA;
  866. ah->imask &= ~ATH9K_INT_TSFOOR;
  867. ah->slottime = ATH9K_SLOT_TIME_9;
  868. ath_hw_setbssidmask(common);
  869. ath9k_hw_setopmode(ah);
  870. ath9k_hw_write_associd(sc->sc_ah);
  871. ath9k_hw_set_interrupts(ah);
  872. ath9k_hw_init_global_settings(ah);
  873. exit:
  874. ath9k_ps_restore(sc);
  875. }
  876. #endif
  877. /* Called with sc->mutex held. */
  878. void ath9k_calculate_summary_state(struct ath_softc *sc,
  879. struct ath_chanctx *ctx)
  880. {
  881. struct ath_hw *ah = sc->sc_ah;
  882. struct ath_common *common = ath9k_hw_common(ah);
  883. struct ath9k_vif_iter_data iter_data;
  884. struct ath_beacon_config *cur_conf;
  885. ath_chanctx_check_active(sc, ctx);
  886. if (ctx != sc->cur_chan)
  887. return;
  888. #ifdef CONFIG_ATH9K_CHANNEL_CONTEXT
  889. if (ctx == &sc->offchannel.chan)
  890. return ath9k_set_offchannel_state(sc);
  891. #endif
  892. ath9k_ps_wakeup(sc);
  893. ath9k_calculate_iter_data(sc, ctx, &iter_data);
  894. if (iter_data.has_hw_macaddr)
  895. memcpy(common->macaddr, iter_data.hw_macaddr, ETH_ALEN);
  896. memcpy(common->bssidmask, iter_data.mask, ETH_ALEN);
  897. ath_hw_setbssidmask(common);
  898. if (iter_data.naps > 0) {
  899. cur_conf = &ctx->beacon;
  900. ath9k_hw_set_tsfadjust(ah, true);
  901. ah->opmode = NL80211_IFTYPE_AP;
  902. if (cur_conf->enable_beacon)
  903. iter_data.beacons = true;
  904. } else {
  905. ath9k_hw_set_tsfadjust(ah, false);
  906. if (iter_data.nmeshes)
  907. ah->opmode = NL80211_IFTYPE_MESH_POINT;
  908. else if (iter_data.nocbs)
  909. ah->opmode = NL80211_IFTYPE_OCB;
  910. else if (iter_data.nwds)
  911. ah->opmode = NL80211_IFTYPE_AP;
  912. else if (iter_data.nadhocs)
  913. ah->opmode = NL80211_IFTYPE_ADHOC;
  914. else
  915. ah->opmode = NL80211_IFTYPE_STATION;
  916. }
  917. ath9k_hw_setopmode(ah);
  918. ctx->switch_after_beacon = false;
  919. if ((iter_data.nstations + iter_data.nadhocs + iter_data.nmeshes) > 0)
  920. ah->imask |= ATH9K_INT_TSFOOR;
  921. else {
  922. ah->imask &= ~ATH9K_INT_TSFOOR;
  923. if (iter_data.naps == 1 && iter_data.beacons)
  924. ctx->switch_after_beacon = true;
  925. }
  926. ah->imask &= ~ATH9K_INT_SWBA;
  927. if (ah->opmode == NL80211_IFTYPE_STATION) {
  928. bool changed = (iter_data.primary_sta != ctx->primary_sta);
  929. if (iter_data.primary_sta) {
  930. iter_data.beacons = true;
  931. ath9k_set_assoc_state(sc, iter_data.primary_sta,
  932. changed);
  933. ctx->primary_sta = iter_data.primary_sta;
  934. } else {
  935. ctx->primary_sta = NULL;
  936. eth_zero_addr(common->curbssid);
  937. common->curaid = 0;
  938. ath9k_hw_write_associd(sc->sc_ah);
  939. if (ath9k_hw_mci_is_enabled(sc->sc_ah))
  940. ath9k_mci_update_wlan_channels(sc, true);
  941. }
  942. } else if (iter_data.beacons) {
  943. ah->imask |= ATH9K_INT_SWBA;
  944. }
  945. ath9k_hw_set_interrupts(ah);
  946. if (iter_data.beacons)
  947. set_bit(ATH_OP_BEACONS, &common->op_flags);
  948. else
  949. clear_bit(ATH_OP_BEACONS, &common->op_flags);
  950. if (ah->slottime != iter_data.slottime) {
  951. ah->slottime = iter_data.slottime;
  952. ath9k_hw_init_global_settings(ah);
  953. }
  954. if (iter_data.primary_sta)
  955. set_bit(ATH_OP_PRIM_STA_VIF, &common->op_flags);
  956. else
  957. clear_bit(ATH_OP_PRIM_STA_VIF, &common->op_flags);
  958. ath_dbg(common, CONFIG,
  959. "macaddr: %pM, bssid: %pM, bssidmask: %pM\n",
  960. common->macaddr, common->curbssid, common->bssidmask);
  961. ath9k_ps_restore(sc);
  962. }
  963. static void ath9k_tpc_vif_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
  964. {
  965. int *power = (int *)data;
  966. if (*power < vif->bss_conf.txpower)
  967. *power = vif->bss_conf.txpower;
  968. }
  969. /* Called with sc->mutex held. */
  970. void ath9k_set_txpower(struct ath_softc *sc, struct ieee80211_vif *vif)
  971. {
  972. int power;
  973. struct ath_hw *ah = sc->sc_ah;
  974. struct ath_regulatory *reg = ath9k_hw_regulatory(ah);
  975. ath9k_ps_wakeup(sc);
  976. if (ah->tpc_enabled) {
  977. power = (vif) ? vif->bss_conf.txpower : -1;
  978. ieee80211_iterate_active_interfaces_atomic(
  979. sc->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
  980. ath9k_tpc_vif_iter, &power);
  981. if (power == -1)
  982. power = sc->hw->conf.power_level;
  983. } else {
  984. power = sc->hw->conf.power_level;
  985. }
  986. sc->cur_chan->txpower = 2 * power;
  987. ath9k_hw_set_txpowerlimit(ah, sc->cur_chan->txpower, false);
  988. sc->cur_chan->cur_txpower = reg->max_power_level;
  989. ath9k_ps_restore(sc);
  990. }
  991. static void ath9k_assign_hw_queues(struct ieee80211_hw *hw,
  992. struct ieee80211_vif *vif)
  993. {
  994. int i;
  995. if (!ath9k_is_chanctx_enabled())
  996. return;
  997. for (i = 0; i < IEEE80211_NUM_ACS; i++)
  998. vif->hw_queue[i] = i;
  999. if (vif->type == NL80211_IFTYPE_AP ||
  1000. vif->type == NL80211_IFTYPE_MESH_POINT)
  1001. vif->cab_queue = hw->queues - 2;
  1002. else
  1003. vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
  1004. }
  1005. static int ath9k_add_interface(struct ieee80211_hw *hw,
  1006. struct ieee80211_vif *vif)
  1007. {
  1008. struct ath_softc *sc = hw->priv;
  1009. struct ath_hw *ah = sc->sc_ah;
  1010. struct ath_common *common = ath9k_hw_common(ah);
  1011. struct ath_vif *avp = (void *)vif->drv_priv;
  1012. struct ath_node *an = &avp->mcast_node;
  1013. mutex_lock(&sc->mutex);
  1014. if (config_enabled(CONFIG_ATH9K_TX99)) {
  1015. if (sc->cur_chan->nvifs >= 1) {
  1016. mutex_unlock(&sc->mutex);
  1017. return -EOPNOTSUPP;
  1018. }
  1019. sc->tx99_vif = vif;
  1020. }
  1021. ath_dbg(common, CONFIG, "Attach a VIF of type: %d\n", vif->type);
  1022. sc->cur_chan->nvifs++;
  1023. if (ath9k_uses_beacons(vif->type))
  1024. ath9k_beacon_assign_slot(sc, vif);
  1025. avp->vif = vif;
  1026. if (!ath9k_is_chanctx_enabled()) {
  1027. avp->chanctx = sc->cur_chan;
  1028. list_add_tail(&avp->list, &avp->chanctx->vifs);
  1029. }
  1030. ath9k_calculate_summary_state(sc, avp->chanctx);
  1031. ath9k_assign_hw_queues(hw, vif);
  1032. ath9k_set_txpower(sc, vif);
  1033. an->sc = sc;
  1034. an->sta = NULL;
  1035. an->vif = vif;
  1036. an->no_ps_filter = true;
  1037. ath_tx_node_init(sc, an);
  1038. mutex_unlock(&sc->mutex);
  1039. return 0;
  1040. }
  1041. static int ath9k_change_interface(struct ieee80211_hw *hw,
  1042. struct ieee80211_vif *vif,
  1043. enum nl80211_iftype new_type,
  1044. bool p2p)
  1045. {
  1046. struct ath_softc *sc = hw->priv;
  1047. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  1048. struct ath_vif *avp = (void *)vif->drv_priv;
  1049. mutex_lock(&sc->mutex);
  1050. if (config_enabled(CONFIG_ATH9K_TX99)) {
  1051. mutex_unlock(&sc->mutex);
  1052. return -EOPNOTSUPP;
  1053. }
  1054. ath_dbg(common, CONFIG, "Change Interface\n");
  1055. if (ath9k_uses_beacons(vif->type))
  1056. ath9k_beacon_remove_slot(sc, vif);
  1057. vif->type = new_type;
  1058. vif->p2p = p2p;
  1059. if (ath9k_uses_beacons(vif->type))
  1060. ath9k_beacon_assign_slot(sc, vif);
  1061. ath9k_assign_hw_queues(hw, vif);
  1062. ath9k_calculate_summary_state(sc, avp->chanctx);
  1063. ath9k_set_txpower(sc, vif);
  1064. mutex_unlock(&sc->mutex);
  1065. return 0;
  1066. }
  1067. static void ath9k_remove_interface(struct ieee80211_hw *hw,
  1068. struct ieee80211_vif *vif)
  1069. {
  1070. struct ath_softc *sc = hw->priv;
  1071. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  1072. struct ath_vif *avp = (void *)vif->drv_priv;
  1073. ath_dbg(common, CONFIG, "Detach Interface\n");
  1074. mutex_lock(&sc->mutex);
  1075. ath9k_p2p_remove_vif(sc, vif);
  1076. sc->cur_chan->nvifs--;
  1077. sc->tx99_vif = NULL;
  1078. if (!ath9k_is_chanctx_enabled())
  1079. list_del(&avp->list);
  1080. if (ath9k_uses_beacons(vif->type))
  1081. ath9k_beacon_remove_slot(sc, vif);
  1082. ath_tx_node_cleanup(sc, &avp->mcast_node);
  1083. ath9k_calculate_summary_state(sc, avp->chanctx);
  1084. ath9k_set_txpower(sc, NULL);
  1085. mutex_unlock(&sc->mutex);
  1086. }
  1087. static void ath9k_enable_ps(struct ath_softc *sc)
  1088. {
  1089. struct ath_hw *ah = sc->sc_ah;
  1090. struct ath_common *common = ath9k_hw_common(ah);
  1091. if (config_enabled(CONFIG_ATH9K_TX99))
  1092. return;
  1093. sc->ps_enabled = true;
  1094. if (!(ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP)) {
  1095. if ((ah->imask & ATH9K_INT_TIM_TIMER) == 0) {
  1096. ah->imask |= ATH9K_INT_TIM_TIMER;
  1097. ath9k_hw_set_interrupts(ah);
  1098. }
  1099. ath9k_hw_setrxabort(ah, 1);
  1100. }
  1101. ath_dbg(common, PS, "PowerSave enabled\n");
  1102. }
  1103. static void ath9k_disable_ps(struct ath_softc *sc)
  1104. {
  1105. struct ath_hw *ah = sc->sc_ah;
  1106. struct ath_common *common = ath9k_hw_common(ah);
  1107. if (config_enabled(CONFIG_ATH9K_TX99))
  1108. return;
  1109. sc->ps_enabled = false;
  1110. ath9k_hw_setpower(ah, ATH9K_PM_AWAKE);
  1111. if (!(ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP)) {
  1112. ath9k_hw_setrxabort(ah, 0);
  1113. sc->ps_flags &= ~(PS_WAIT_FOR_BEACON |
  1114. PS_WAIT_FOR_CAB |
  1115. PS_WAIT_FOR_PSPOLL_DATA |
  1116. PS_WAIT_FOR_TX_ACK);
  1117. if (ah->imask & ATH9K_INT_TIM_TIMER) {
  1118. ah->imask &= ~ATH9K_INT_TIM_TIMER;
  1119. ath9k_hw_set_interrupts(ah);
  1120. }
  1121. }
  1122. ath_dbg(common, PS, "PowerSave disabled\n");
  1123. }
  1124. static int ath9k_config(struct ieee80211_hw *hw, u32 changed)
  1125. {
  1126. struct ath_softc *sc = hw->priv;
  1127. struct ath_hw *ah = sc->sc_ah;
  1128. struct ath_common *common = ath9k_hw_common(ah);
  1129. struct ieee80211_conf *conf = &hw->conf;
  1130. struct ath_chanctx *ctx = sc->cur_chan;
  1131. ath9k_ps_wakeup(sc);
  1132. mutex_lock(&sc->mutex);
  1133. if (changed & IEEE80211_CONF_CHANGE_IDLE) {
  1134. sc->ps_idle = !!(conf->flags & IEEE80211_CONF_IDLE);
  1135. if (sc->ps_idle) {
  1136. ath_cancel_work(sc);
  1137. ath9k_stop_btcoex(sc);
  1138. } else {
  1139. ath9k_start_btcoex(sc);
  1140. /*
  1141. * The chip needs a reset to properly wake up from
  1142. * full sleep
  1143. */
  1144. ath_chanctx_set_channel(sc, ctx, &ctx->chandef);
  1145. }
  1146. }
  1147. /*
  1148. * We just prepare to enable PS. We have to wait until our AP has
  1149. * ACK'd our null data frame to disable RX otherwise we'll ignore
  1150. * those ACKs and end up retransmitting the same null data frames.
  1151. * IEEE80211_CONF_CHANGE_PS is only passed by mac80211 for STA mode.
  1152. */
  1153. if (changed & IEEE80211_CONF_CHANGE_PS) {
  1154. unsigned long flags;
  1155. spin_lock_irqsave(&sc->sc_pm_lock, flags);
  1156. if (conf->flags & IEEE80211_CONF_PS)
  1157. ath9k_enable_ps(sc);
  1158. else
  1159. ath9k_disable_ps(sc);
  1160. spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
  1161. }
  1162. if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
  1163. if (conf->flags & IEEE80211_CONF_MONITOR) {
  1164. ath_dbg(common, CONFIG, "Monitor mode is enabled\n");
  1165. sc->sc_ah->is_monitoring = true;
  1166. } else {
  1167. ath_dbg(common, CONFIG, "Monitor mode is disabled\n");
  1168. sc->sc_ah->is_monitoring = false;
  1169. }
  1170. }
  1171. if (!ath9k_is_chanctx_enabled() && (changed & IEEE80211_CONF_CHANGE_CHANNEL)) {
  1172. ctx->offchannel = !!(conf->flags & IEEE80211_CONF_OFFCHANNEL);
  1173. ath_chanctx_set_channel(sc, ctx, &hw->conf.chandef);
  1174. }
  1175. mutex_unlock(&sc->mutex);
  1176. ath9k_ps_restore(sc);
  1177. return 0;
  1178. }
  1179. #define SUPPORTED_FILTERS \
  1180. (FIF_ALLMULTI | \
  1181. FIF_CONTROL | \
  1182. FIF_PSPOLL | \
  1183. FIF_OTHER_BSS | \
  1184. FIF_BCN_PRBRESP_PROMISC | \
  1185. FIF_PROBE_REQ | \
  1186. FIF_FCSFAIL)
  1187. /* FIXME: sc->sc_full_reset ? */
  1188. static void ath9k_configure_filter(struct ieee80211_hw *hw,
  1189. unsigned int changed_flags,
  1190. unsigned int *total_flags,
  1191. u64 multicast)
  1192. {
  1193. struct ath_softc *sc = hw->priv;
  1194. struct ath_chanctx *ctx;
  1195. u32 rfilt;
  1196. changed_flags &= SUPPORTED_FILTERS;
  1197. *total_flags &= SUPPORTED_FILTERS;
  1198. spin_lock_bh(&sc->chan_lock);
  1199. ath_for_each_chanctx(sc, ctx)
  1200. ctx->rxfilter = *total_flags;
  1201. #ifdef CONFIG_ATH9K_CHANNEL_CONTEXT
  1202. sc->offchannel.chan.rxfilter = *total_flags;
  1203. #endif
  1204. spin_unlock_bh(&sc->chan_lock);
  1205. ath9k_ps_wakeup(sc);
  1206. rfilt = ath_calcrxfilter(sc);
  1207. ath9k_hw_setrxfilter(sc->sc_ah, rfilt);
  1208. ath9k_ps_restore(sc);
  1209. ath_dbg(ath9k_hw_common(sc->sc_ah), CONFIG, "Set HW RX filter: 0x%x\n",
  1210. rfilt);
  1211. }
  1212. static int ath9k_sta_add(struct ieee80211_hw *hw,
  1213. struct ieee80211_vif *vif,
  1214. struct ieee80211_sta *sta)
  1215. {
  1216. struct ath_softc *sc = hw->priv;
  1217. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  1218. struct ath_node *an = (struct ath_node *) sta->drv_priv;
  1219. struct ieee80211_key_conf ps_key = { };
  1220. int key;
  1221. ath_node_attach(sc, sta, vif);
  1222. if (vif->type != NL80211_IFTYPE_AP &&
  1223. vif->type != NL80211_IFTYPE_AP_VLAN)
  1224. return 0;
  1225. key = ath_key_config(common, vif, sta, &ps_key);
  1226. if (key > 0) {
  1227. an->ps_key = key;
  1228. an->key_idx[0] = key;
  1229. }
  1230. return 0;
  1231. }
  1232. static void ath9k_del_ps_key(struct ath_softc *sc,
  1233. struct ieee80211_vif *vif,
  1234. struct ieee80211_sta *sta)
  1235. {
  1236. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  1237. struct ath_node *an = (struct ath_node *) sta->drv_priv;
  1238. struct ieee80211_key_conf ps_key = { .hw_key_idx = an->ps_key };
  1239. if (!an->ps_key)
  1240. return;
  1241. ath_key_delete(common, &ps_key);
  1242. an->ps_key = 0;
  1243. an->key_idx[0] = 0;
  1244. }
  1245. static int ath9k_sta_remove(struct ieee80211_hw *hw,
  1246. struct ieee80211_vif *vif,
  1247. struct ieee80211_sta *sta)
  1248. {
  1249. struct ath_softc *sc = hw->priv;
  1250. ath9k_del_ps_key(sc, vif, sta);
  1251. ath_node_detach(sc, sta);
  1252. return 0;
  1253. }
  1254. static int ath9k_sta_state(struct ieee80211_hw *hw,
  1255. struct ieee80211_vif *vif,
  1256. struct ieee80211_sta *sta,
  1257. enum ieee80211_sta_state old_state,
  1258. enum ieee80211_sta_state new_state)
  1259. {
  1260. struct ath_softc *sc = hw->priv;
  1261. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  1262. int ret = 0;
  1263. if (old_state == IEEE80211_STA_AUTH &&
  1264. new_state == IEEE80211_STA_ASSOC) {
  1265. ret = ath9k_sta_add(hw, vif, sta);
  1266. ath_dbg(common, CONFIG,
  1267. "Add station: %pM\n", sta->addr);
  1268. } else if (old_state == IEEE80211_STA_ASSOC &&
  1269. new_state == IEEE80211_STA_AUTH) {
  1270. ret = ath9k_sta_remove(hw, vif, sta);
  1271. ath_dbg(common, CONFIG,
  1272. "Remove station: %pM\n", sta->addr);
  1273. }
  1274. if (ath9k_is_chanctx_enabled()) {
  1275. if (vif->type == NL80211_IFTYPE_STATION) {
  1276. if (old_state == IEEE80211_STA_ASSOC &&
  1277. new_state == IEEE80211_STA_AUTHORIZED)
  1278. ath_chanctx_event(sc, vif,
  1279. ATH_CHANCTX_EVENT_AUTHORIZED);
  1280. }
  1281. }
  1282. return ret;
  1283. }
  1284. static void ath9k_sta_set_tx_filter(struct ath_hw *ah,
  1285. struct ath_node *an,
  1286. bool set)
  1287. {
  1288. int i;
  1289. for (i = 0; i < ARRAY_SIZE(an->key_idx); i++) {
  1290. if (!an->key_idx[i])
  1291. continue;
  1292. ath9k_hw_set_tx_filter(ah, an->key_idx[i], set);
  1293. }
  1294. }
  1295. static void ath9k_sta_notify(struct ieee80211_hw *hw,
  1296. struct ieee80211_vif *vif,
  1297. enum sta_notify_cmd cmd,
  1298. struct ieee80211_sta *sta)
  1299. {
  1300. struct ath_softc *sc = hw->priv;
  1301. struct ath_node *an = (struct ath_node *) sta->drv_priv;
  1302. switch (cmd) {
  1303. case STA_NOTIFY_SLEEP:
  1304. an->sleeping = true;
  1305. ath_tx_aggr_sleep(sta, sc, an);
  1306. ath9k_sta_set_tx_filter(sc->sc_ah, an, true);
  1307. break;
  1308. case STA_NOTIFY_AWAKE:
  1309. ath9k_sta_set_tx_filter(sc->sc_ah, an, false);
  1310. an->sleeping = false;
  1311. ath_tx_aggr_wakeup(sc, an);
  1312. break;
  1313. }
  1314. }
  1315. static int ath9k_conf_tx(struct ieee80211_hw *hw,
  1316. struct ieee80211_vif *vif, u16 queue,
  1317. const struct ieee80211_tx_queue_params *params)
  1318. {
  1319. struct ath_softc *sc = hw->priv;
  1320. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  1321. struct ath_txq *txq;
  1322. struct ath9k_tx_queue_info qi;
  1323. int ret = 0;
  1324. if (queue >= IEEE80211_NUM_ACS)
  1325. return 0;
  1326. txq = sc->tx.txq_map[queue];
  1327. ath9k_ps_wakeup(sc);
  1328. mutex_lock(&sc->mutex);
  1329. memset(&qi, 0, sizeof(struct ath9k_tx_queue_info));
  1330. qi.tqi_aifs = params->aifs;
  1331. qi.tqi_cwmin = params->cw_min;
  1332. qi.tqi_cwmax = params->cw_max;
  1333. qi.tqi_burstTime = params->txop * 32;
  1334. ath_dbg(common, CONFIG,
  1335. "Configure tx [queue/halq] [%d/%d], aifs: %d, cw_min: %d, cw_max: %d, txop: %d\n",
  1336. queue, txq->axq_qnum, params->aifs, params->cw_min,
  1337. params->cw_max, params->txop);
  1338. ath_update_max_aggr_framelen(sc, queue, qi.tqi_burstTime);
  1339. ret = ath_txq_update(sc, txq->axq_qnum, &qi);
  1340. if (ret)
  1341. ath_err(common, "TXQ Update failed\n");
  1342. mutex_unlock(&sc->mutex);
  1343. ath9k_ps_restore(sc);
  1344. return ret;
  1345. }
  1346. static int ath9k_set_key(struct ieee80211_hw *hw,
  1347. enum set_key_cmd cmd,
  1348. struct ieee80211_vif *vif,
  1349. struct ieee80211_sta *sta,
  1350. struct ieee80211_key_conf *key)
  1351. {
  1352. struct ath_softc *sc = hw->priv;
  1353. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  1354. struct ath_node *an = NULL;
  1355. int ret = 0, i;
  1356. if (ath9k_modparam_nohwcrypt)
  1357. return -ENOSPC;
  1358. if ((vif->type == NL80211_IFTYPE_ADHOC ||
  1359. vif->type == NL80211_IFTYPE_MESH_POINT) &&
  1360. (key->cipher == WLAN_CIPHER_SUITE_TKIP ||
  1361. key->cipher == WLAN_CIPHER_SUITE_CCMP) &&
  1362. !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
  1363. /*
  1364. * For now, disable hw crypto for the RSN IBSS group keys. This
  1365. * could be optimized in the future to use a modified key cache
  1366. * design to support per-STA RX GTK, but until that gets
  1367. * implemented, use of software crypto for group addressed
  1368. * frames is a acceptable to allow RSN IBSS to be used.
  1369. */
  1370. return -EOPNOTSUPP;
  1371. }
  1372. mutex_lock(&sc->mutex);
  1373. ath9k_ps_wakeup(sc);
  1374. ath_dbg(common, CONFIG, "Set HW Key %d\n", cmd);
  1375. if (sta)
  1376. an = (struct ath_node *)sta->drv_priv;
  1377. switch (cmd) {
  1378. case SET_KEY:
  1379. if (sta)
  1380. ath9k_del_ps_key(sc, vif, sta);
  1381. key->hw_key_idx = 0;
  1382. ret = ath_key_config(common, vif, sta, key);
  1383. if (ret >= 0) {
  1384. key->hw_key_idx = ret;
  1385. /* push IV and Michael MIC generation to stack */
  1386. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  1387. if (key->cipher == WLAN_CIPHER_SUITE_TKIP)
  1388. key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
  1389. if (sc->sc_ah->sw_mgmt_crypto_tx &&
  1390. key->cipher == WLAN_CIPHER_SUITE_CCMP)
  1391. key->flags |= IEEE80211_KEY_FLAG_SW_MGMT_TX;
  1392. ret = 0;
  1393. }
  1394. if (an && key->hw_key_idx) {
  1395. for (i = 0; i < ARRAY_SIZE(an->key_idx); i++) {
  1396. if (an->key_idx[i])
  1397. continue;
  1398. an->key_idx[i] = key->hw_key_idx;
  1399. break;
  1400. }
  1401. WARN_ON(i == ARRAY_SIZE(an->key_idx));
  1402. }
  1403. break;
  1404. case DISABLE_KEY:
  1405. ath_key_delete(common, key);
  1406. if (an) {
  1407. for (i = 0; i < ARRAY_SIZE(an->key_idx); i++) {
  1408. if (an->key_idx[i] != key->hw_key_idx)
  1409. continue;
  1410. an->key_idx[i] = 0;
  1411. break;
  1412. }
  1413. }
  1414. key->hw_key_idx = 0;
  1415. break;
  1416. default:
  1417. ret = -EINVAL;
  1418. }
  1419. ath9k_ps_restore(sc);
  1420. mutex_unlock(&sc->mutex);
  1421. return ret;
  1422. }
  1423. static void ath9k_bss_info_changed(struct ieee80211_hw *hw,
  1424. struct ieee80211_vif *vif,
  1425. struct ieee80211_bss_conf *bss_conf,
  1426. u32 changed)
  1427. {
  1428. #define CHECK_ANI \
  1429. (BSS_CHANGED_ASSOC | \
  1430. BSS_CHANGED_IBSS | \
  1431. BSS_CHANGED_BEACON_ENABLED)
  1432. struct ath_softc *sc = hw->priv;
  1433. struct ath_hw *ah = sc->sc_ah;
  1434. struct ath_common *common = ath9k_hw_common(ah);
  1435. struct ath_vif *avp = (void *)vif->drv_priv;
  1436. int slottime;
  1437. ath9k_ps_wakeup(sc);
  1438. mutex_lock(&sc->mutex);
  1439. if (changed & BSS_CHANGED_ASSOC) {
  1440. ath_dbg(common, CONFIG, "BSSID %pM Changed ASSOC %d\n",
  1441. bss_conf->bssid, bss_conf->assoc);
  1442. memcpy(avp->bssid, bss_conf->bssid, ETH_ALEN);
  1443. avp->aid = bss_conf->aid;
  1444. avp->assoc = bss_conf->assoc;
  1445. ath9k_calculate_summary_state(sc, avp->chanctx);
  1446. }
  1447. if ((changed & BSS_CHANGED_IBSS) ||
  1448. (changed & BSS_CHANGED_OCB)) {
  1449. memcpy(common->curbssid, bss_conf->bssid, ETH_ALEN);
  1450. common->curaid = bss_conf->aid;
  1451. ath9k_hw_write_associd(sc->sc_ah);
  1452. }
  1453. if ((changed & BSS_CHANGED_BEACON_ENABLED) ||
  1454. (changed & BSS_CHANGED_BEACON_INT) ||
  1455. (changed & BSS_CHANGED_BEACON_INFO)) {
  1456. ath9k_beacon_config(sc, vif, changed);
  1457. if (changed & BSS_CHANGED_BEACON_ENABLED)
  1458. ath9k_calculate_summary_state(sc, avp->chanctx);
  1459. }
  1460. if ((avp->chanctx == sc->cur_chan) &&
  1461. (changed & BSS_CHANGED_ERP_SLOT)) {
  1462. if (bss_conf->use_short_slot)
  1463. slottime = 9;
  1464. else
  1465. slottime = 20;
  1466. if (vif->type == NL80211_IFTYPE_AP) {
  1467. /*
  1468. * Defer update, so that connected stations can adjust
  1469. * their settings at the same time.
  1470. * See beacon.c for more details
  1471. */
  1472. sc->beacon.slottime = slottime;
  1473. sc->beacon.updateslot = UPDATE;
  1474. } else {
  1475. ah->slottime = slottime;
  1476. ath9k_hw_init_global_settings(ah);
  1477. }
  1478. }
  1479. if (changed & BSS_CHANGED_P2P_PS)
  1480. ath9k_p2p_bss_info_changed(sc, vif);
  1481. if (changed & CHECK_ANI)
  1482. ath_check_ani(sc);
  1483. if (changed & BSS_CHANGED_TXPOWER) {
  1484. ath_dbg(common, CONFIG, "vif %pM power %d dbm power_type %d\n",
  1485. vif->addr, bss_conf->txpower, bss_conf->txpower_type);
  1486. ath9k_set_txpower(sc, vif);
  1487. }
  1488. mutex_unlock(&sc->mutex);
  1489. ath9k_ps_restore(sc);
  1490. #undef CHECK_ANI
  1491. }
  1492. static u64 ath9k_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
  1493. {
  1494. struct ath_softc *sc = hw->priv;
  1495. u64 tsf;
  1496. mutex_lock(&sc->mutex);
  1497. ath9k_ps_wakeup(sc);
  1498. tsf = ath9k_hw_gettsf64(sc->sc_ah);
  1499. ath9k_ps_restore(sc);
  1500. mutex_unlock(&sc->mutex);
  1501. return tsf;
  1502. }
  1503. static void ath9k_set_tsf(struct ieee80211_hw *hw,
  1504. struct ieee80211_vif *vif,
  1505. u64 tsf)
  1506. {
  1507. struct ath_softc *sc = hw->priv;
  1508. mutex_lock(&sc->mutex);
  1509. ath9k_ps_wakeup(sc);
  1510. ath9k_hw_settsf64(sc->sc_ah, tsf);
  1511. ath9k_ps_restore(sc);
  1512. mutex_unlock(&sc->mutex);
  1513. }
  1514. static void ath9k_reset_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
  1515. {
  1516. struct ath_softc *sc = hw->priv;
  1517. mutex_lock(&sc->mutex);
  1518. ath9k_ps_wakeup(sc);
  1519. ath9k_hw_reset_tsf(sc->sc_ah);
  1520. ath9k_ps_restore(sc);
  1521. mutex_unlock(&sc->mutex);
  1522. }
  1523. static int ath9k_ampdu_action(struct ieee80211_hw *hw,
  1524. struct ieee80211_vif *vif,
  1525. struct ieee80211_ampdu_params *params)
  1526. {
  1527. struct ath_softc *sc = hw->priv;
  1528. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  1529. bool flush = false;
  1530. int ret = 0;
  1531. struct ieee80211_sta *sta = params->sta;
  1532. enum ieee80211_ampdu_mlme_action action = params->action;
  1533. u16 tid = params->tid;
  1534. u16 *ssn = &params->ssn;
  1535. mutex_lock(&sc->mutex);
  1536. switch (action) {
  1537. case IEEE80211_AMPDU_RX_START:
  1538. break;
  1539. case IEEE80211_AMPDU_RX_STOP:
  1540. break;
  1541. case IEEE80211_AMPDU_TX_START:
  1542. if (ath9k_is_chanctx_enabled()) {
  1543. if (test_bit(ATH_OP_SCANNING, &common->op_flags)) {
  1544. ret = -EBUSY;
  1545. break;
  1546. }
  1547. }
  1548. ath9k_ps_wakeup(sc);
  1549. ret = ath_tx_aggr_start(sc, sta, tid, ssn);
  1550. if (!ret)
  1551. ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  1552. ath9k_ps_restore(sc);
  1553. break;
  1554. case IEEE80211_AMPDU_TX_STOP_FLUSH:
  1555. case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
  1556. flush = true;
  1557. case IEEE80211_AMPDU_TX_STOP_CONT:
  1558. ath9k_ps_wakeup(sc);
  1559. ath_tx_aggr_stop(sc, sta, tid);
  1560. if (!flush)
  1561. ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  1562. ath9k_ps_restore(sc);
  1563. break;
  1564. case IEEE80211_AMPDU_TX_OPERATIONAL:
  1565. ath9k_ps_wakeup(sc);
  1566. ath_tx_aggr_resume(sc, sta, tid);
  1567. ath9k_ps_restore(sc);
  1568. break;
  1569. default:
  1570. ath_err(ath9k_hw_common(sc->sc_ah), "Unknown AMPDU action\n");
  1571. }
  1572. mutex_unlock(&sc->mutex);
  1573. return ret;
  1574. }
  1575. static int ath9k_get_survey(struct ieee80211_hw *hw, int idx,
  1576. struct survey_info *survey)
  1577. {
  1578. struct ath_softc *sc = hw->priv;
  1579. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  1580. struct ieee80211_supported_band *sband;
  1581. struct ieee80211_channel *chan;
  1582. int pos;
  1583. if (config_enabled(CONFIG_ATH9K_TX99))
  1584. return -EOPNOTSUPP;
  1585. spin_lock_bh(&common->cc_lock);
  1586. if (idx == 0)
  1587. ath_update_survey_stats(sc);
  1588. sband = hw->wiphy->bands[IEEE80211_BAND_2GHZ];
  1589. if (sband && idx >= sband->n_channels) {
  1590. idx -= sband->n_channels;
  1591. sband = NULL;
  1592. }
  1593. if (!sband)
  1594. sband = hw->wiphy->bands[IEEE80211_BAND_5GHZ];
  1595. if (!sband || idx >= sband->n_channels) {
  1596. spin_unlock_bh(&common->cc_lock);
  1597. return -ENOENT;
  1598. }
  1599. chan = &sband->channels[idx];
  1600. pos = chan->hw_value;
  1601. memcpy(survey, &sc->survey[pos], sizeof(*survey));
  1602. survey->channel = chan;
  1603. spin_unlock_bh(&common->cc_lock);
  1604. return 0;
  1605. }
  1606. static void ath9k_enable_dynack(struct ath_softc *sc)
  1607. {
  1608. #ifdef CONFIG_ATH9K_DYNACK
  1609. u32 rfilt;
  1610. struct ath_hw *ah = sc->sc_ah;
  1611. ath_dynack_reset(ah);
  1612. ah->dynack.enabled = true;
  1613. rfilt = ath_calcrxfilter(sc);
  1614. ath9k_hw_setrxfilter(ah, rfilt);
  1615. #endif
  1616. }
  1617. static void ath9k_set_coverage_class(struct ieee80211_hw *hw,
  1618. s16 coverage_class)
  1619. {
  1620. struct ath_softc *sc = hw->priv;
  1621. struct ath_hw *ah = sc->sc_ah;
  1622. if (config_enabled(CONFIG_ATH9K_TX99))
  1623. return;
  1624. mutex_lock(&sc->mutex);
  1625. if (coverage_class >= 0) {
  1626. ah->coverage_class = coverage_class;
  1627. if (ah->dynack.enabled) {
  1628. u32 rfilt;
  1629. ah->dynack.enabled = false;
  1630. rfilt = ath_calcrxfilter(sc);
  1631. ath9k_hw_setrxfilter(ah, rfilt);
  1632. }
  1633. ath9k_ps_wakeup(sc);
  1634. ath9k_hw_init_global_settings(ah);
  1635. ath9k_ps_restore(sc);
  1636. } else if (!ah->dynack.enabled) {
  1637. ath9k_enable_dynack(sc);
  1638. }
  1639. mutex_unlock(&sc->mutex);
  1640. }
  1641. static bool ath9k_has_tx_pending(struct ath_softc *sc,
  1642. bool sw_pending)
  1643. {
  1644. int i, npend = 0;
  1645. for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
  1646. if (!ATH_TXQ_SETUP(sc, i))
  1647. continue;
  1648. npend = ath9k_has_pending_frames(sc, &sc->tx.txq[i],
  1649. sw_pending);
  1650. if (npend)
  1651. break;
  1652. }
  1653. return !!npend;
  1654. }
  1655. static void ath9k_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  1656. u32 queues, bool drop)
  1657. {
  1658. struct ath_softc *sc = hw->priv;
  1659. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  1660. if (ath9k_is_chanctx_enabled()) {
  1661. if (!test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags))
  1662. goto flush;
  1663. /*
  1664. * If MCC is active, extend the flush timeout
  1665. * and wait for the HW/SW queues to become
  1666. * empty. This needs to be done outside the
  1667. * sc->mutex lock to allow the channel scheduler
  1668. * to switch channel contexts.
  1669. *
  1670. * The vif queues have been stopped in mac80211,
  1671. * so there won't be any incoming frames.
  1672. */
  1673. __ath9k_flush(hw, queues, drop, true, true);
  1674. return;
  1675. }
  1676. flush:
  1677. mutex_lock(&sc->mutex);
  1678. __ath9k_flush(hw, queues, drop, true, false);
  1679. mutex_unlock(&sc->mutex);
  1680. }
  1681. void __ath9k_flush(struct ieee80211_hw *hw, u32 queues, bool drop,
  1682. bool sw_pending, bool timeout_override)
  1683. {
  1684. struct ath_softc *sc = hw->priv;
  1685. struct ath_hw *ah = sc->sc_ah;
  1686. struct ath_common *common = ath9k_hw_common(ah);
  1687. int timeout;
  1688. bool drain_txq;
  1689. cancel_delayed_work_sync(&sc->tx_complete_work);
  1690. if (ah->ah_flags & AH_UNPLUGGED) {
  1691. ath_dbg(common, ANY, "Device has been unplugged!\n");
  1692. return;
  1693. }
  1694. if (test_bit(ATH_OP_INVALID, &common->op_flags)) {
  1695. ath_dbg(common, ANY, "Device not present\n");
  1696. return;
  1697. }
  1698. spin_lock_bh(&sc->chan_lock);
  1699. if (timeout_override)
  1700. timeout = HZ / 5;
  1701. else
  1702. timeout = sc->cur_chan->flush_timeout;
  1703. spin_unlock_bh(&sc->chan_lock);
  1704. ath_dbg(common, CHAN_CTX,
  1705. "Flush timeout: %d\n", jiffies_to_msecs(timeout));
  1706. if (wait_event_timeout(sc->tx_wait, !ath9k_has_tx_pending(sc, sw_pending),
  1707. timeout) > 0)
  1708. drop = false;
  1709. if (drop) {
  1710. ath9k_ps_wakeup(sc);
  1711. spin_lock_bh(&sc->sc_pcu_lock);
  1712. drain_txq = ath_drain_all_txq(sc);
  1713. spin_unlock_bh(&sc->sc_pcu_lock);
  1714. if (!drain_txq)
  1715. ath_reset(sc, NULL);
  1716. ath9k_ps_restore(sc);
  1717. }
  1718. ieee80211_queue_delayed_work(hw, &sc->tx_complete_work, 0);
  1719. }
  1720. static bool ath9k_tx_frames_pending(struct ieee80211_hw *hw)
  1721. {
  1722. struct ath_softc *sc = hw->priv;
  1723. return ath9k_has_tx_pending(sc, true);
  1724. }
  1725. static int ath9k_tx_last_beacon(struct ieee80211_hw *hw)
  1726. {
  1727. struct ath_softc *sc = hw->priv;
  1728. struct ath_hw *ah = sc->sc_ah;
  1729. struct ieee80211_vif *vif;
  1730. struct ath_vif *avp;
  1731. struct ath_buf *bf;
  1732. struct ath_tx_status ts;
  1733. bool edma = !!(ah->caps.hw_caps & ATH9K_HW_CAP_EDMA);
  1734. int status;
  1735. vif = sc->beacon.bslot[0];
  1736. if (!vif)
  1737. return 0;
  1738. if (!vif->bss_conf.enable_beacon)
  1739. return 0;
  1740. avp = (void *)vif->drv_priv;
  1741. if (!sc->beacon.tx_processed && !edma) {
  1742. tasklet_disable(&sc->bcon_tasklet);
  1743. bf = avp->av_bcbuf;
  1744. if (!bf || !bf->bf_mpdu)
  1745. goto skip;
  1746. status = ath9k_hw_txprocdesc(ah, bf->bf_desc, &ts);
  1747. if (status == -EINPROGRESS)
  1748. goto skip;
  1749. sc->beacon.tx_processed = true;
  1750. sc->beacon.tx_last = !(ts.ts_status & ATH9K_TXERR_MASK);
  1751. skip:
  1752. tasklet_enable(&sc->bcon_tasklet);
  1753. }
  1754. return sc->beacon.tx_last;
  1755. }
  1756. static int ath9k_get_stats(struct ieee80211_hw *hw,
  1757. struct ieee80211_low_level_stats *stats)
  1758. {
  1759. struct ath_softc *sc = hw->priv;
  1760. struct ath_hw *ah = sc->sc_ah;
  1761. struct ath9k_mib_stats *mib_stats = &ah->ah_mibStats;
  1762. stats->dot11ACKFailureCount = mib_stats->ackrcv_bad;
  1763. stats->dot11RTSFailureCount = mib_stats->rts_bad;
  1764. stats->dot11FCSErrorCount = mib_stats->fcs_bad;
  1765. stats->dot11RTSSuccessCount = mib_stats->rts_good;
  1766. return 0;
  1767. }
  1768. static u32 fill_chainmask(u32 cap, u32 new)
  1769. {
  1770. u32 filled = 0;
  1771. int i;
  1772. for (i = 0; cap && new; i++, cap >>= 1) {
  1773. if (!(cap & BIT(0)))
  1774. continue;
  1775. if (new & BIT(0))
  1776. filled |= BIT(i);
  1777. new >>= 1;
  1778. }
  1779. return filled;
  1780. }
  1781. static bool validate_antenna_mask(struct ath_hw *ah, u32 val)
  1782. {
  1783. if (AR_SREV_9300_20_OR_LATER(ah))
  1784. return true;
  1785. switch (val & 0x7) {
  1786. case 0x1:
  1787. case 0x3:
  1788. case 0x7:
  1789. return true;
  1790. case 0x2:
  1791. return (ah->caps.rx_chainmask == 1);
  1792. default:
  1793. return false;
  1794. }
  1795. }
  1796. static int ath9k_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
  1797. {
  1798. struct ath_softc *sc = hw->priv;
  1799. struct ath_hw *ah = sc->sc_ah;
  1800. if (ah->caps.rx_chainmask != 1)
  1801. rx_ant |= tx_ant;
  1802. if (!validate_antenna_mask(ah, rx_ant) || !tx_ant)
  1803. return -EINVAL;
  1804. sc->ant_rx = rx_ant;
  1805. sc->ant_tx = tx_ant;
  1806. if (ah->caps.rx_chainmask == 1)
  1807. return 0;
  1808. /* AR9100 runs into calibration issues if not all rx chains are enabled */
  1809. if (AR_SREV_9100(ah))
  1810. ah->rxchainmask = 0x7;
  1811. else
  1812. ah->rxchainmask = fill_chainmask(ah->caps.rx_chainmask, rx_ant);
  1813. ah->txchainmask = fill_chainmask(ah->caps.tx_chainmask, tx_ant);
  1814. ath9k_cmn_reload_chainmask(ah);
  1815. return 0;
  1816. }
  1817. static int ath9k_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
  1818. {
  1819. struct ath_softc *sc = hw->priv;
  1820. *tx_ant = sc->ant_tx;
  1821. *rx_ant = sc->ant_rx;
  1822. return 0;
  1823. }
  1824. static void ath9k_sw_scan_start(struct ieee80211_hw *hw,
  1825. struct ieee80211_vif *vif,
  1826. const u8 *mac_addr)
  1827. {
  1828. struct ath_softc *sc = hw->priv;
  1829. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  1830. set_bit(ATH_OP_SCANNING, &common->op_flags);
  1831. }
  1832. static void ath9k_sw_scan_complete(struct ieee80211_hw *hw,
  1833. struct ieee80211_vif *vif)
  1834. {
  1835. struct ath_softc *sc = hw->priv;
  1836. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  1837. clear_bit(ATH_OP_SCANNING, &common->op_flags);
  1838. }
  1839. #ifdef CONFIG_ATH9K_CHANNEL_CONTEXT
  1840. static void ath9k_cancel_pending_offchannel(struct ath_softc *sc)
  1841. {
  1842. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  1843. if (sc->offchannel.roc_vif) {
  1844. ath_dbg(common, CHAN_CTX,
  1845. "%s: Aborting RoC\n", __func__);
  1846. del_timer_sync(&sc->offchannel.timer);
  1847. if (sc->offchannel.state >= ATH_OFFCHANNEL_ROC_START)
  1848. ath_roc_complete(sc, ATH_ROC_COMPLETE_ABORT);
  1849. }
  1850. if (test_bit(ATH_OP_SCANNING, &common->op_flags)) {
  1851. ath_dbg(common, CHAN_CTX,
  1852. "%s: Aborting HW scan\n", __func__);
  1853. del_timer_sync(&sc->offchannel.timer);
  1854. ath_scan_complete(sc, true);
  1855. }
  1856. }
  1857. static int ath9k_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  1858. struct ieee80211_scan_request *hw_req)
  1859. {
  1860. struct cfg80211_scan_request *req = &hw_req->req;
  1861. struct ath_softc *sc = hw->priv;
  1862. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  1863. int ret = 0;
  1864. mutex_lock(&sc->mutex);
  1865. if (WARN_ON(sc->offchannel.scan_req)) {
  1866. ret = -EBUSY;
  1867. goto out;
  1868. }
  1869. ath9k_ps_wakeup(sc);
  1870. set_bit(ATH_OP_SCANNING, &common->op_flags);
  1871. sc->offchannel.scan_vif = vif;
  1872. sc->offchannel.scan_req = req;
  1873. sc->offchannel.scan_idx = 0;
  1874. ath_dbg(common, CHAN_CTX, "HW scan request received on vif: %pM\n",
  1875. vif->addr);
  1876. if (sc->offchannel.state == ATH_OFFCHANNEL_IDLE) {
  1877. ath_dbg(common, CHAN_CTX, "Starting HW scan\n");
  1878. ath_offchannel_next(sc);
  1879. }
  1880. out:
  1881. mutex_unlock(&sc->mutex);
  1882. return ret;
  1883. }
  1884. static void ath9k_cancel_hw_scan(struct ieee80211_hw *hw,
  1885. struct ieee80211_vif *vif)
  1886. {
  1887. struct ath_softc *sc = hw->priv;
  1888. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  1889. ath_dbg(common, CHAN_CTX, "Cancel HW scan on vif: %pM\n", vif->addr);
  1890. mutex_lock(&sc->mutex);
  1891. del_timer_sync(&sc->offchannel.timer);
  1892. ath_scan_complete(sc, true);
  1893. mutex_unlock(&sc->mutex);
  1894. }
  1895. static int ath9k_remain_on_channel(struct ieee80211_hw *hw,
  1896. struct ieee80211_vif *vif,
  1897. struct ieee80211_channel *chan, int duration,
  1898. enum ieee80211_roc_type type)
  1899. {
  1900. struct ath_softc *sc = hw->priv;
  1901. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  1902. int ret = 0;
  1903. mutex_lock(&sc->mutex);
  1904. if (WARN_ON(sc->offchannel.roc_vif)) {
  1905. ret = -EBUSY;
  1906. goto out;
  1907. }
  1908. ath9k_ps_wakeup(sc);
  1909. sc->offchannel.roc_vif = vif;
  1910. sc->offchannel.roc_chan = chan;
  1911. sc->offchannel.roc_duration = duration;
  1912. ath_dbg(common, CHAN_CTX,
  1913. "RoC request on vif: %pM, type: %d duration: %d\n",
  1914. vif->addr, type, duration);
  1915. if (sc->offchannel.state == ATH_OFFCHANNEL_IDLE) {
  1916. ath_dbg(common, CHAN_CTX, "Starting RoC period\n");
  1917. ath_offchannel_next(sc);
  1918. }
  1919. out:
  1920. mutex_unlock(&sc->mutex);
  1921. return ret;
  1922. }
  1923. static int ath9k_cancel_remain_on_channel(struct ieee80211_hw *hw)
  1924. {
  1925. struct ath_softc *sc = hw->priv;
  1926. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  1927. mutex_lock(&sc->mutex);
  1928. ath_dbg(common, CHAN_CTX, "Cancel RoC\n");
  1929. del_timer_sync(&sc->offchannel.timer);
  1930. if (sc->offchannel.roc_vif) {
  1931. if (sc->offchannel.state >= ATH_OFFCHANNEL_ROC_START)
  1932. ath_roc_complete(sc, ATH_ROC_COMPLETE_CANCEL);
  1933. }
  1934. mutex_unlock(&sc->mutex);
  1935. return 0;
  1936. }
  1937. static int ath9k_add_chanctx(struct ieee80211_hw *hw,
  1938. struct ieee80211_chanctx_conf *conf)
  1939. {
  1940. struct ath_softc *sc = hw->priv;
  1941. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  1942. struct ath_chanctx *ctx, **ptr;
  1943. int pos;
  1944. mutex_lock(&sc->mutex);
  1945. ath_for_each_chanctx(sc, ctx) {
  1946. if (ctx->assigned)
  1947. continue;
  1948. ptr = (void *) conf->drv_priv;
  1949. *ptr = ctx;
  1950. ctx->assigned = true;
  1951. pos = ctx - &sc->chanctx[0];
  1952. ctx->hw_queue_base = pos * IEEE80211_NUM_ACS;
  1953. ath_dbg(common, CHAN_CTX,
  1954. "Add channel context: %d MHz\n",
  1955. conf->def.chan->center_freq);
  1956. ath_chanctx_set_channel(sc, ctx, &conf->def);
  1957. mutex_unlock(&sc->mutex);
  1958. return 0;
  1959. }
  1960. mutex_unlock(&sc->mutex);
  1961. return -ENOSPC;
  1962. }
  1963. static void ath9k_remove_chanctx(struct ieee80211_hw *hw,
  1964. struct ieee80211_chanctx_conf *conf)
  1965. {
  1966. struct ath_softc *sc = hw->priv;
  1967. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  1968. struct ath_chanctx *ctx = ath_chanctx_get(conf);
  1969. mutex_lock(&sc->mutex);
  1970. ath_dbg(common, CHAN_CTX,
  1971. "Remove channel context: %d MHz\n",
  1972. conf->def.chan->center_freq);
  1973. ctx->assigned = false;
  1974. ctx->hw_queue_base = 0;
  1975. ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_UNASSIGN);
  1976. mutex_unlock(&sc->mutex);
  1977. }
  1978. static void ath9k_change_chanctx(struct ieee80211_hw *hw,
  1979. struct ieee80211_chanctx_conf *conf,
  1980. u32 changed)
  1981. {
  1982. struct ath_softc *sc = hw->priv;
  1983. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  1984. struct ath_chanctx *ctx = ath_chanctx_get(conf);
  1985. mutex_lock(&sc->mutex);
  1986. ath_dbg(common, CHAN_CTX,
  1987. "Change channel context: %d MHz\n",
  1988. conf->def.chan->center_freq);
  1989. ath_chanctx_set_channel(sc, ctx, &conf->def);
  1990. mutex_unlock(&sc->mutex);
  1991. }
  1992. static int ath9k_assign_vif_chanctx(struct ieee80211_hw *hw,
  1993. struct ieee80211_vif *vif,
  1994. struct ieee80211_chanctx_conf *conf)
  1995. {
  1996. struct ath_softc *sc = hw->priv;
  1997. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  1998. struct ath_vif *avp = (void *)vif->drv_priv;
  1999. struct ath_chanctx *ctx = ath_chanctx_get(conf);
  2000. int i;
  2001. ath9k_cancel_pending_offchannel(sc);
  2002. mutex_lock(&sc->mutex);
  2003. ath_dbg(common, CHAN_CTX,
  2004. "Assign VIF (addr: %pM, type: %d, p2p: %d) to channel context: %d MHz\n",
  2005. vif->addr, vif->type, vif->p2p,
  2006. conf->def.chan->center_freq);
  2007. avp->chanctx = ctx;
  2008. ctx->nvifs_assigned++;
  2009. list_add_tail(&avp->list, &ctx->vifs);
  2010. ath9k_calculate_summary_state(sc, ctx);
  2011. for (i = 0; i < IEEE80211_NUM_ACS; i++)
  2012. vif->hw_queue[i] = ctx->hw_queue_base + i;
  2013. mutex_unlock(&sc->mutex);
  2014. return 0;
  2015. }
  2016. static void ath9k_unassign_vif_chanctx(struct ieee80211_hw *hw,
  2017. struct ieee80211_vif *vif,
  2018. struct ieee80211_chanctx_conf *conf)
  2019. {
  2020. struct ath_softc *sc = hw->priv;
  2021. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  2022. struct ath_vif *avp = (void *)vif->drv_priv;
  2023. struct ath_chanctx *ctx = ath_chanctx_get(conf);
  2024. int ac;
  2025. ath9k_cancel_pending_offchannel(sc);
  2026. mutex_lock(&sc->mutex);
  2027. ath_dbg(common, CHAN_CTX,
  2028. "Remove VIF (addr: %pM, type: %d, p2p: %d) from channel context: %d MHz\n",
  2029. vif->addr, vif->type, vif->p2p,
  2030. conf->def.chan->center_freq);
  2031. avp->chanctx = NULL;
  2032. ctx->nvifs_assigned--;
  2033. list_del(&avp->list);
  2034. ath9k_calculate_summary_state(sc, ctx);
  2035. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
  2036. vif->hw_queue[ac] = IEEE80211_INVAL_HW_QUEUE;
  2037. mutex_unlock(&sc->mutex);
  2038. }
  2039. static void ath9k_mgd_prepare_tx(struct ieee80211_hw *hw,
  2040. struct ieee80211_vif *vif)
  2041. {
  2042. struct ath_softc *sc = hw->priv;
  2043. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  2044. struct ath_vif *avp = (struct ath_vif *) vif->drv_priv;
  2045. struct ath_beacon_config *cur_conf;
  2046. struct ath_chanctx *go_ctx;
  2047. unsigned long timeout;
  2048. bool changed = false;
  2049. u32 beacon_int;
  2050. if (!test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags))
  2051. return;
  2052. if (!avp->chanctx)
  2053. return;
  2054. mutex_lock(&sc->mutex);
  2055. spin_lock_bh(&sc->chan_lock);
  2056. if (sc->next_chan || (sc->cur_chan != avp->chanctx))
  2057. changed = true;
  2058. spin_unlock_bh(&sc->chan_lock);
  2059. if (!changed)
  2060. goto out;
  2061. ath9k_cancel_pending_offchannel(sc);
  2062. go_ctx = ath_is_go_chanctx_present(sc);
  2063. if (go_ctx) {
  2064. /*
  2065. * Wait till the GO interface gets a chance
  2066. * to send out an NoA.
  2067. */
  2068. spin_lock_bh(&sc->chan_lock);
  2069. sc->sched.mgd_prepare_tx = true;
  2070. cur_conf = &go_ctx->beacon;
  2071. beacon_int = TU_TO_USEC(cur_conf->beacon_interval);
  2072. spin_unlock_bh(&sc->chan_lock);
  2073. timeout = usecs_to_jiffies(beacon_int * 2);
  2074. init_completion(&sc->go_beacon);
  2075. mutex_unlock(&sc->mutex);
  2076. if (wait_for_completion_timeout(&sc->go_beacon,
  2077. timeout) == 0) {
  2078. ath_dbg(common, CHAN_CTX,
  2079. "Failed to send new NoA\n");
  2080. spin_lock_bh(&sc->chan_lock);
  2081. sc->sched.mgd_prepare_tx = false;
  2082. spin_unlock_bh(&sc->chan_lock);
  2083. }
  2084. mutex_lock(&sc->mutex);
  2085. }
  2086. ath_dbg(common, CHAN_CTX,
  2087. "%s: Set chanctx state to FORCE_ACTIVE for vif: %pM\n",
  2088. __func__, vif->addr);
  2089. spin_lock_bh(&sc->chan_lock);
  2090. sc->next_chan = avp->chanctx;
  2091. sc->sched.state = ATH_CHANCTX_STATE_FORCE_ACTIVE;
  2092. spin_unlock_bh(&sc->chan_lock);
  2093. ath_chanctx_set_next(sc, true);
  2094. out:
  2095. mutex_unlock(&sc->mutex);
  2096. }
  2097. void ath9k_fill_chanctx_ops(void)
  2098. {
  2099. if (!ath9k_is_chanctx_enabled())
  2100. return;
  2101. ath9k_ops.hw_scan = ath9k_hw_scan;
  2102. ath9k_ops.cancel_hw_scan = ath9k_cancel_hw_scan;
  2103. ath9k_ops.remain_on_channel = ath9k_remain_on_channel;
  2104. ath9k_ops.cancel_remain_on_channel = ath9k_cancel_remain_on_channel;
  2105. ath9k_ops.add_chanctx = ath9k_add_chanctx;
  2106. ath9k_ops.remove_chanctx = ath9k_remove_chanctx;
  2107. ath9k_ops.change_chanctx = ath9k_change_chanctx;
  2108. ath9k_ops.assign_vif_chanctx = ath9k_assign_vif_chanctx;
  2109. ath9k_ops.unassign_vif_chanctx = ath9k_unassign_vif_chanctx;
  2110. ath9k_ops.mgd_prepare_tx = ath9k_mgd_prepare_tx;
  2111. }
  2112. #endif
  2113. static int ath9k_get_txpower(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  2114. int *dbm)
  2115. {
  2116. struct ath_softc *sc = hw->priv;
  2117. struct ath_vif *avp = (void *)vif->drv_priv;
  2118. mutex_lock(&sc->mutex);
  2119. if (avp->chanctx)
  2120. *dbm = avp->chanctx->cur_txpower;
  2121. else
  2122. *dbm = sc->cur_chan->cur_txpower;
  2123. mutex_unlock(&sc->mutex);
  2124. *dbm /= 2;
  2125. return 0;
  2126. }
  2127. struct ieee80211_ops ath9k_ops = {
  2128. .tx = ath9k_tx,
  2129. .start = ath9k_start,
  2130. .stop = ath9k_stop,
  2131. .add_interface = ath9k_add_interface,
  2132. .change_interface = ath9k_change_interface,
  2133. .remove_interface = ath9k_remove_interface,
  2134. .config = ath9k_config,
  2135. .configure_filter = ath9k_configure_filter,
  2136. .sta_state = ath9k_sta_state,
  2137. .sta_notify = ath9k_sta_notify,
  2138. .conf_tx = ath9k_conf_tx,
  2139. .bss_info_changed = ath9k_bss_info_changed,
  2140. .set_key = ath9k_set_key,
  2141. .get_tsf = ath9k_get_tsf,
  2142. .set_tsf = ath9k_set_tsf,
  2143. .reset_tsf = ath9k_reset_tsf,
  2144. .ampdu_action = ath9k_ampdu_action,
  2145. .get_survey = ath9k_get_survey,
  2146. .rfkill_poll = ath9k_rfkill_poll_state,
  2147. .set_coverage_class = ath9k_set_coverage_class,
  2148. .flush = ath9k_flush,
  2149. .tx_frames_pending = ath9k_tx_frames_pending,
  2150. .tx_last_beacon = ath9k_tx_last_beacon,
  2151. .release_buffered_frames = ath9k_release_buffered_frames,
  2152. .get_stats = ath9k_get_stats,
  2153. .set_antenna = ath9k_set_antenna,
  2154. .get_antenna = ath9k_get_antenna,
  2155. #ifdef CONFIG_ATH9K_WOW
  2156. .suspend = ath9k_suspend,
  2157. .resume = ath9k_resume,
  2158. .set_wakeup = ath9k_set_wakeup,
  2159. #endif
  2160. #ifdef CONFIG_ATH9K_DEBUGFS
  2161. .get_et_sset_count = ath9k_get_et_sset_count,
  2162. .get_et_stats = ath9k_get_et_stats,
  2163. .get_et_strings = ath9k_get_et_strings,
  2164. #endif
  2165. #if defined(CONFIG_MAC80211_DEBUGFS) && defined(CONFIG_ATH9K_STATION_STATISTICS)
  2166. .sta_add_debugfs = ath9k_sta_add_debugfs,
  2167. #endif
  2168. .sw_scan_start = ath9k_sw_scan_start,
  2169. .sw_scan_complete = ath9k_sw_scan_complete,
  2170. .get_txpower = ath9k_get_txpower,
  2171. };