main.c 64 KB

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