main.c 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661
  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. return 0;
  604. }
  605. static void ath9k_tx(struct ieee80211_hw *hw,
  606. struct ieee80211_tx_control *control,
  607. struct sk_buff *skb)
  608. {
  609. struct ath_softc *sc = hw->priv;
  610. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  611. struct ath_tx_control txctl;
  612. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  613. unsigned long flags;
  614. if (sc->ps_enabled) {
  615. /*
  616. * mac80211 does not set PM field for normal data frames, so we
  617. * need to update that based on the current PS mode.
  618. */
  619. if (ieee80211_is_data(hdr->frame_control) &&
  620. !ieee80211_is_nullfunc(hdr->frame_control) &&
  621. !ieee80211_has_pm(hdr->frame_control)) {
  622. ath_dbg(common, PS,
  623. "Add PM=1 for a TX frame while in PS mode\n");
  624. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
  625. }
  626. }
  627. if (unlikely(sc->sc_ah->power_mode == ATH9K_PM_NETWORK_SLEEP)) {
  628. /*
  629. * We are using PS-Poll and mac80211 can request TX while in
  630. * power save mode. Need to wake up hardware for the TX to be
  631. * completed and if needed, also for RX of buffered frames.
  632. */
  633. ath9k_ps_wakeup(sc);
  634. spin_lock_irqsave(&sc->sc_pm_lock, flags);
  635. if (!(sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP))
  636. ath9k_hw_setrxabort(sc->sc_ah, 0);
  637. if (ieee80211_is_pspoll(hdr->frame_control)) {
  638. ath_dbg(common, PS,
  639. "Sending PS-Poll to pick a buffered frame\n");
  640. sc->ps_flags |= PS_WAIT_FOR_PSPOLL_DATA;
  641. } else {
  642. ath_dbg(common, PS, "Wake up to complete TX\n");
  643. sc->ps_flags |= PS_WAIT_FOR_TX_ACK;
  644. }
  645. /*
  646. * The actual restore operation will happen only after
  647. * the ps_flags bit is cleared. We are just dropping
  648. * the ps_usecount here.
  649. */
  650. spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
  651. ath9k_ps_restore(sc);
  652. }
  653. /*
  654. * Cannot tx while the hardware is in full sleep, it first needs a full
  655. * chip reset to recover from that
  656. */
  657. if (unlikely(sc->sc_ah->power_mode == ATH9K_PM_FULL_SLEEP)) {
  658. ath_err(common, "TX while HW is in FULL_SLEEP mode\n");
  659. goto exit;
  660. }
  661. memset(&txctl, 0, sizeof(struct ath_tx_control));
  662. txctl.txq = sc->tx.txq_map[skb_get_queue_mapping(skb)];
  663. txctl.sta = control->sta;
  664. ath_dbg(common, XMIT, "transmitting packet, skb: %p\n", skb);
  665. if (ath_tx_start(hw, skb, &txctl) != 0) {
  666. ath_dbg(common, XMIT, "TX failed\n");
  667. TX_STAT_INC(txctl.txq->axq_qnum, txfailed);
  668. goto exit;
  669. }
  670. return;
  671. exit:
  672. ieee80211_free_txskb(hw, skb);
  673. }
  674. static void ath9k_stop(struct ieee80211_hw *hw)
  675. {
  676. struct ath_softc *sc = hw->priv;
  677. struct ath_hw *ah = sc->sc_ah;
  678. struct ath_common *common = ath9k_hw_common(ah);
  679. bool prev_idle;
  680. ath9k_deinit_channel_context(sc);
  681. mutex_lock(&sc->mutex);
  682. ath_cancel_work(sc);
  683. if (test_bit(ATH_OP_INVALID, &common->op_flags)) {
  684. ath_dbg(common, ANY, "Device not present\n");
  685. mutex_unlock(&sc->mutex);
  686. return;
  687. }
  688. /* Ensure HW is awake when we try to shut it down. */
  689. ath9k_ps_wakeup(sc);
  690. spin_lock_bh(&sc->sc_pcu_lock);
  691. /* prevent tasklets to enable interrupts once we disable them */
  692. ah->imask &= ~ATH9K_INT_GLOBAL;
  693. /* make sure h/w will not generate any interrupt
  694. * before setting the invalid flag. */
  695. ath9k_hw_disable_interrupts(ah);
  696. spin_unlock_bh(&sc->sc_pcu_lock);
  697. /* we can now sync irq and kill any running tasklets, since we already
  698. * disabled interrupts and not holding a spin lock */
  699. synchronize_irq(sc->irq);
  700. tasklet_kill(&sc->intr_tq);
  701. tasklet_kill(&sc->bcon_tasklet);
  702. prev_idle = sc->ps_idle;
  703. sc->ps_idle = true;
  704. spin_lock_bh(&sc->sc_pcu_lock);
  705. if (ah->led_pin >= 0) {
  706. ath9k_hw_set_gpio(ah, ah->led_pin,
  707. (ah->config.led_active_high) ? 0 : 1);
  708. ath9k_hw_cfg_gpio_input(ah, ah->led_pin);
  709. }
  710. ath_prepare_reset(sc);
  711. if (sc->rx.frag) {
  712. dev_kfree_skb_any(sc->rx.frag);
  713. sc->rx.frag = NULL;
  714. }
  715. if (!ah->curchan)
  716. ah->curchan = ath9k_cmn_get_channel(hw, ah,
  717. &sc->cur_chan->chandef);
  718. ath9k_hw_reset(ah, ah->curchan, ah->caldata, false);
  719. set_bit(ATH_OP_INVALID, &common->op_flags);
  720. ath9k_hw_phy_disable(ah);
  721. ath9k_hw_configpcipowersave(ah, true);
  722. spin_unlock_bh(&sc->sc_pcu_lock);
  723. ath9k_ps_restore(sc);
  724. sc->ps_idle = prev_idle;
  725. mutex_unlock(&sc->mutex);
  726. ath_dbg(common, CONFIG, "Driver halt\n");
  727. }
  728. static bool ath9k_uses_beacons(int type)
  729. {
  730. switch (type) {
  731. case NL80211_IFTYPE_AP:
  732. case NL80211_IFTYPE_ADHOC:
  733. case NL80211_IFTYPE_MESH_POINT:
  734. return true;
  735. default:
  736. return false;
  737. }
  738. }
  739. static void ath9k_vif_iter(struct ath9k_vif_iter_data *iter_data,
  740. u8 *mac, struct ieee80211_vif *vif)
  741. {
  742. struct ath_vif *avp = (struct ath_vif *)vif->drv_priv;
  743. int i;
  744. if (iter_data->has_hw_macaddr) {
  745. for (i = 0; i < ETH_ALEN; i++)
  746. iter_data->mask[i] &=
  747. ~(iter_data->hw_macaddr[i] ^ mac[i]);
  748. } else {
  749. memcpy(iter_data->hw_macaddr, mac, ETH_ALEN);
  750. iter_data->has_hw_macaddr = true;
  751. }
  752. if (!vif->bss_conf.use_short_slot)
  753. iter_data->slottime = ATH9K_SLOT_TIME_20;
  754. switch (vif->type) {
  755. case NL80211_IFTYPE_AP:
  756. iter_data->naps++;
  757. break;
  758. case NL80211_IFTYPE_STATION:
  759. iter_data->nstations++;
  760. if (avp->assoc && !iter_data->primary_sta)
  761. iter_data->primary_sta = vif;
  762. break;
  763. case NL80211_IFTYPE_ADHOC:
  764. iter_data->nadhocs++;
  765. if (vif->bss_conf.enable_beacon)
  766. iter_data->beacons = true;
  767. break;
  768. case NL80211_IFTYPE_MESH_POINT:
  769. iter_data->nmeshes++;
  770. if (vif->bss_conf.enable_beacon)
  771. iter_data->beacons = true;
  772. break;
  773. case NL80211_IFTYPE_WDS:
  774. iter_data->nwds++;
  775. break;
  776. default:
  777. break;
  778. }
  779. }
  780. static void ath9k_update_bssid_mask(struct ath_softc *sc,
  781. struct ath_chanctx *ctx,
  782. struct ath9k_vif_iter_data *iter_data)
  783. {
  784. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  785. struct ath_vif *avp;
  786. int i;
  787. if (!ath9k_is_chanctx_enabled())
  788. return;
  789. list_for_each_entry(avp, &ctx->vifs, list) {
  790. if (ctx->nvifs_assigned != 1)
  791. continue;
  792. if (!avp->vif->p2p || !iter_data->has_hw_macaddr)
  793. continue;
  794. ether_addr_copy(common->curbssid, avp->bssid);
  795. /* perm_addr will be used as the p2p device address. */
  796. for (i = 0; i < ETH_ALEN; i++)
  797. iter_data->mask[i] &=
  798. ~(iter_data->hw_macaddr[i] ^
  799. sc->hw->wiphy->perm_addr[i]);
  800. }
  801. }
  802. /* Called with sc->mutex held. */
  803. void ath9k_calculate_iter_data(struct ath_softc *sc,
  804. struct ath_chanctx *ctx,
  805. struct ath9k_vif_iter_data *iter_data)
  806. {
  807. struct ath_vif *avp;
  808. /*
  809. * The hardware will use primary station addr together with the
  810. * BSSID mask when matching addresses.
  811. */
  812. memset(iter_data, 0, sizeof(*iter_data));
  813. eth_broadcast_addr(iter_data->mask);
  814. iter_data->slottime = ATH9K_SLOT_TIME_9;
  815. list_for_each_entry(avp, &ctx->vifs, list)
  816. ath9k_vif_iter(iter_data, avp->vif->addr, avp->vif);
  817. ath9k_update_bssid_mask(sc, ctx, iter_data);
  818. }
  819. static void ath9k_set_assoc_state(struct ath_softc *sc,
  820. struct ieee80211_vif *vif, bool changed)
  821. {
  822. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  823. struct ath_vif *avp = (struct ath_vif *)vif->drv_priv;
  824. unsigned long flags;
  825. set_bit(ATH_OP_PRIM_STA_VIF, &common->op_flags);
  826. ether_addr_copy(common->curbssid, avp->bssid);
  827. common->curaid = avp->aid;
  828. ath9k_hw_write_associd(sc->sc_ah);
  829. if (changed) {
  830. common->last_rssi = ATH_RSSI_DUMMY_MARKER;
  831. sc->sc_ah->stats.avgbrssi = ATH_RSSI_DUMMY_MARKER;
  832. spin_lock_irqsave(&sc->sc_pm_lock, flags);
  833. sc->ps_flags |= PS_BEACON_SYNC | PS_WAIT_FOR_BEACON;
  834. spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
  835. }
  836. if (ath9k_hw_mci_is_enabled(sc->sc_ah))
  837. ath9k_mci_update_wlan_channels(sc, false);
  838. ath_dbg(common, CONFIG,
  839. "Primary Station interface: %pM, BSSID: %pM\n",
  840. vif->addr, common->curbssid);
  841. }
  842. #ifdef CONFIG_ATH9K_CHANNEL_CONTEXT
  843. static void ath9k_set_offchannel_state(struct ath_softc *sc)
  844. {
  845. struct ath_hw *ah = sc->sc_ah;
  846. struct ath_common *common = ath9k_hw_common(ah);
  847. struct ieee80211_vif *vif = NULL;
  848. ath9k_ps_wakeup(sc);
  849. if (sc->offchannel.state < ATH_OFFCHANNEL_ROC_START)
  850. vif = sc->offchannel.scan_vif;
  851. else
  852. vif = sc->offchannel.roc_vif;
  853. if (WARN_ON(!vif))
  854. goto exit;
  855. eth_zero_addr(common->curbssid);
  856. eth_broadcast_addr(common->bssidmask);
  857. memcpy(common->macaddr, vif->addr, ETH_ALEN);
  858. common->curaid = 0;
  859. ah->opmode = vif->type;
  860. ah->imask &= ~ATH9K_INT_SWBA;
  861. ah->imask &= ~ATH9K_INT_TSFOOR;
  862. ah->slottime = ATH9K_SLOT_TIME_9;
  863. ath_hw_setbssidmask(common);
  864. ath9k_hw_setopmode(ah);
  865. ath9k_hw_write_associd(sc->sc_ah);
  866. ath9k_hw_set_interrupts(ah);
  867. ath9k_hw_init_global_settings(ah);
  868. exit:
  869. ath9k_ps_restore(sc);
  870. }
  871. #endif
  872. /* Called with sc->mutex held. */
  873. void ath9k_calculate_summary_state(struct ath_softc *sc,
  874. struct ath_chanctx *ctx)
  875. {
  876. struct ath_hw *ah = sc->sc_ah;
  877. struct ath_common *common = ath9k_hw_common(ah);
  878. struct ath9k_vif_iter_data iter_data;
  879. struct ath_beacon_config *cur_conf;
  880. ath_chanctx_check_active(sc, ctx);
  881. if (ctx != sc->cur_chan)
  882. return;
  883. #ifdef CONFIG_ATH9K_CHANNEL_CONTEXT
  884. if (ctx == &sc->offchannel.chan)
  885. return ath9k_set_offchannel_state(sc);
  886. #endif
  887. ath9k_ps_wakeup(sc);
  888. ath9k_calculate_iter_data(sc, ctx, &iter_data);
  889. if (iter_data.has_hw_macaddr)
  890. memcpy(common->macaddr, iter_data.hw_macaddr, ETH_ALEN);
  891. memcpy(common->bssidmask, iter_data.mask, ETH_ALEN);
  892. ath_hw_setbssidmask(common);
  893. if (iter_data.naps > 0) {
  894. cur_conf = &ctx->beacon;
  895. ath9k_hw_set_tsfadjust(ah, true);
  896. ah->opmode = NL80211_IFTYPE_AP;
  897. if (cur_conf->enable_beacon)
  898. iter_data.beacons = true;
  899. } else {
  900. ath9k_hw_set_tsfadjust(ah, false);
  901. if (iter_data.nmeshes)
  902. ah->opmode = NL80211_IFTYPE_MESH_POINT;
  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_AUTH &&
  1257. new_state == IEEE80211_STA_ASSOC) {
  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_ASSOC &&
  1262. new_state == IEEE80211_STA_AUTH) {
  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. memcpy(common->curbssid, bss_conf->bssid, ETH_ALEN);
  1442. common->curaid = bss_conf->aid;
  1443. ath9k_hw_write_associd(sc->sc_ah);
  1444. }
  1445. if ((changed & BSS_CHANGED_BEACON_ENABLED) ||
  1446. (changed & BSS_CHANGED_BEACON_INT) ||
  1447. (changed & BSS_CHANGED_BEACON_INFO)) {
  1448. ath9k_beacon_config(sc, vif, changed);
  1449. if (changed & BSS_CHANGED_BEACON_ENABLED)
  1450. ath9k_calculate_summary_state(sc, avp->chanctx);
  1451. }
  1452. if ((avp->chanctx == sc->cur_chan) &&
  1453. (changed & BSS_CHANGED_ERP_SLOT)) {
  1454. if (bss_conf->use_short_slot)
  1455. slottime = 9;
  1456. else
  1457. slottime = 20;
  1458. if (vif->type == NL80211_IFTYPE_AP) {
  1459. /*
  1460. * Defer update, so that connected stations can adjust
  1461. * their settings at the same time.
  1462. * See beacon.c for more details
  1463. */
  1464. sc->beacon.slottime = slottime;
  1465. sc->beacon.updateslot = UPDATE;
  1466. } else {
  1467. ah->slottime = slottime;
  1468. ath9k_hw_init_global_settings(ah);
  1469. }
  1470. }
  1471. if (changed & BSS_CHANGED_P2P_PS)
  1472. ath9k_p2p_bss_info_changed(sc, vif);
  1473. if (changed & CHECK_ANI)
  1474. ath_check_ani(sc);
  1475. if (changed & BSS_CHANGED_TXPOWER) {
  1476. ath_dbg(common, CONFIG, "vif %pM power %d dbm power_type %d\n",
  1477. vif->addr, bss_conf->txpower, bss_conf->txpower_type);
  1478. ath9k_set_txpower(sc, vif);
  1479. }
  1480. mutex_unlock(&sc->mutex);
  1481. ath9k_ps_restore(sc);
  1482. #undef CHECK_ANI
  1483. }
  1484. static u64 ath9k_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
  1485. {
  1486. struct ath_softc *sc = hw->priv;
  1487. u64 tsf;
  1488. mutex_lock(&sc->mutex);
  1489. ath9k_ps_wakeup(sc);
  1490. tsf = ath9k_hw_gettsf64(sc->sc_ah);
  1491. ath9k_ps_restore(sc);
  1492. mutex_unlock(&sc->mutex);
  1493. return tsf;
  1494. }
  1495. static void ath9k_set_tsf(struct ieee80211_hw *hw,
  1496. struct ieee80211_vif *vif,
  1497. u64 tsf)
  1498. {
  1499. struct ath_softc *sc = hw->priv;
  1500. mutex_lock(&sc->mutex);
  1501. ath9k_ps_wakeup(sc);
  1502. ath9k_hw_settsf64(sc->sc_ah, tsf);
  1503. ath9k_ps_restore(sc);
  1504. mutex_unlock(&sc->mutex);
  1505. }
  1506. static void ath9k_reset_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
  1507. {
  1508. struct ath_softc *sc = hw->priv;
  1509. mutex_lock(&sc->mutex);
  1510. ath9k_ps_wakeup(sc);
  1511. ath9k_hw_reset_tsf(sc->sc_ah);
  1512. ath9k_ps_restore(sc);
  1513. mutex_unlock(&sc->mutex);
  1514. }
  1515. static int ath9k_ampdu_action(struct ieee80211_hw *hw,
  1516. struct ieee80211_vif *vif,
  1517. enum ieee80211_ampdu_mlme_action action,
  1518. struct ieee80211_sta *sta,
  1519. u16 tid, u16 *ssn, u8 buf_size)
  1520. {
  1521. struct ath_softc *sc = hw->priv;
  1522. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  1523. bool flush = false;
  1524. int ret = 0;
  1525. mutex_lock(&sc->mutex);
  1526. switch (action) {
  1527. case IEEE80211_AMPDU_RX_START:
  1528. break;
  1529. case IEEE80211_AMPDU_RX_STOP:
  1530. break;
  1531. case IEEE80211_AMPDU_TX_START:
  1532. if (ath9k_is_chanctx_enabled()) {
  1533. if (test_bit(ATH_OP_SCANNING, &common->op_flags)) {
  1534. ret = -EBUSY;
  1535. break;
  1536. }
  1537. }
  1538. ath9k_ps_wakeup(sc);
  1539. ret = ath_tx_aggr_start(sc, sta, tid, ssn);
  1540. if (!ret)
  1541. ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  1542. ath9k_ps_restore(sc);
  1543. break;
  1544. case IEEE80211_AMPDU_TX_STOP_FLUSH:
  1545. case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
  1546. flush = true;
  1547. case IEEE80211_AMPDU_TX_STOP_CONT:
  1548. ath9k_ps_wakeup(sc);
  1549. ath_tx_aggr_stop(sc, sta, tid);
  1550. if (!flush)
  1551. ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  1552. ath9k_ps_restore(sc);
  1553. break;
  1554. case IEEE80211_AMPDU_TX_OPERATIONAL:
  1555. ath9k_ps_wakeup(sc);
  1556. ath_tx_aggr_resume(sc, sta, tid);
  1557. ath9k_ps_restore(sc);
  1558. break;
  1559. default:
  1560. ath_err(ath9k_hw_common(sc->sc_ah), "Unknown AMPDU action\n");
  1561. }
  1562. mutex_unlock(&sc->mutex);
  1563. return ret;
  1564. }
  1565. static int ath9k_get_survey(struct ieee80211_hw *hw, int idx,
  1566. struct survey_info *survey)
  1567. {
  1568. struct ath_softc *sc = hw->priv;
  1569. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  1570. struct ieee80211_supported_band *sband;
  1571. struct ieee80211_channel *chan;
  1572. int pos;
  1573. if (config_enabled(CONFIG_ATH9K_TX99))
  1574. return -EOPNOTSUPP;
  1575. spin_lock_bh(&common->cc_lock);
  1576. if (idx == 0)
  1577. ath_update_survey_stats(sc);
  1578. sband = hw->wiphy->bands[IEEE80211_BAND_2GHZ];
  1579. if (sband && idx >= sband->n_channels) {
  1580. idx -= sband->n_channels;
  1581. sband = NULL;
  1582. }
  1583. if (!sband)
  1584. sband = hw->wiphy->bands[IEEE80211_BAND_5GHZ];
  1585. if (!sband || idx >= sband->n_channels) {
  1586. spin_unlock_bh(&common->cc_lock);
  1587. return -ENOENT;
  1588. }
  1589. chan = &sband->channels[idx];
  1590. pos = chan->hw_value;
  1591. memcpy(survey, &sc->survey[pos], sizeof(*survey));
  1592. survey->channel = chan;
  1593. spin_unlock_bh(&common->cc_lock);
  1594. return 0;
  1595. }
  1596. static void ath9k_enable_dynack(struct ath_softc *sc)
  1597. {
  1598. #ifdef CONFIG_ATH9K_DYNACK
  1599. u32 rfilt;
  1600. struct ath_hw *ah = sc->sc_ah;
  1601. ath_dynack_reset(ah);
  1602. ah->dynack.enabled = true;
  1603. rfilt = ath_calcrxfilter(sc);
  1604. ath9k_hw_setrxfilter(ah, rfilt);
  1605. #endif
  1606. }
  1607. static void ath9k_set_coverage_class(struct ieee80211_hw *hw,
  1608. s16 coverage_class)
  1609. {
  1610. struct ath_softc *sc = hw->priv;
  1611. struct ath_hw *ah = sc->sc_ah;
  1612. if (config_enabled(CONFIG_ATH9K_TX99))
  1613. return;
  1614. mutex_lock(&sc->mutex);
  1615. if (coverage_class >= 0) {
  1616. ah->coverage_class = coverage_class;
  1617. if (ah->dynack.enabled) {
  1618. u32 rfilt;
  1619. ah->dynack.enabled = false;
  1620. rfilt = ath_calcrxfilter(sc);
  1621. ath9k_hw_setrxfilter(ah, rfilt);
  1622. }
  1623. ath9k_ps_wakeup(sc);
  1624. ath9k_hw_init_global_settings(ah);
  1625. ath9k_ps_restore(sc);
  1626. } else if (!ah->dynack.enabled) {
  1627. ath9k_enable_dynack(sc);
  1628. }
  1629. mutex_unlock(&sc->mutex);
  1630. }
  1631. static bool ath9k_has_tx_pending(struct ath_softc *sc,
  1632. bool sw_pending)
  1633. {
  1634. int i, npend = 0;
  1635. for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
  1636. if (!ATH_TXQ_SETUP(sc, i))
  1637. continue;
  1638. npend = ath9k_has_pending_frames(sc, &sc->tx.txq[i],
  1639. sw_pending);
  1640. if (npend)
  1641. break;
  1642. }
  1643. return !!npend;
  1644. }
  1645. static void ath9k_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  1646. u32 queues, bool drop)
  1647. {
  1648. struct ath_softc *sc = hw->priv;
  1649. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  1650. if (ath9k_is_chanctx_enabled()) {
  1651. if (!test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags))
  1652. goto flush;
  1653. /*
  1654. * If MCC is active, extend the flush timeout
  1655. * and wait for the HW/SW queues to become
  1656. * empty. This needs to be done outside the
  1657. * sc->mutex lock to allow the channel scheduler
  1658. * to switch channel contexts.
  1659. *
  1660. * The vif queues have been stopped in mac80211,
  1661. * so there won't be any incoming frames.
  1662. */
  1663. __ath9k_flush(hw, queues, drop, true, true);
  1664. return;
  1665. }
  1666. flush:
  1667. mutex_lock(&sc->mutex);
  1668. __ath9k_flush(hw, queues, drop, true, false);
  1669. mutex_unlock(&sc->mutex);
  1670. }
  1671. void __ath9k_flush(struct ieee80211_hw *hw, u32 queues, bool drop,
  1672. bool sw_pending, bool timeout_override)
  1673. {
  1674. struct ath_softc *sc = hw->priv;
  1675. struct ath_hw *ah = sc->sc_ah;
  1676. struct ath_common *common = ath9k_hw_common(ah);
  1677. int timeout;
  1678. bool drain_txq;
  1679. cancel_delayed_work_sync(&sc->tx_complete_work);
  1680. if (ah->ah_flags & AH_UNPLUGGED) {
  1681. ath_dbg(common, ANY, "Device has been unplugged!\n");
  1682. return;
  1683. }
  1684. if (test_bit(ATH_OP_INVALID, &common->op_flags)) {
  1685. ath_dbg(common, ANY, "Device not present\n");
  1686. return;
  1687. }
  1688. spin_lock_bh(&sc->chan_lock);
  1689. if (timeout_override)
  1690. timeout = HZ / 5;
  1691. else
  1692. timeout = sc->cur_chan->flush_timeout;
  1693. spin_unlock_bh(&sc->chan_lock);
  1694. ath_dbg(common, CHAN_CTX,
  1695. "Flush timeout: %d\n", jiffies_to_msecs(timeout));
  1696. if (wait_event_timeout(sc->tx_wait, !ath9k_has_tx_pending(sc, sw_pending),
  1697. timeout) > 0)
  1698. drop = false;
  1699. if (drop) {
  1700. ath9k_ps_wakeup(sc);
  1701. spin_lock_bh(&sc->sc_pcu_lock);
  1702. drain_txq = ath_drain_all_txq(sc);
  1703. spin_unlock_bh(&sc->sc_pcu_lock);
  1704. if (!drain_txq)
  1705. ath_reset(sc, NULL);
  1706. ath9k_ps_restore(sc);
  1707. }
  1708. ieee80211_queue_delayed_work(hw, &sc->tx_complete_work, 0);
  1709. }
  1710. static bool ath9k_tx_frames_pending(struct ieee80211_hw *hw)
  1711. {
  1712. struct ath_softc *sc = hw->priv;
  1713. return ath9k_has_tx_pending(sc, true);
  1714. }
  1715. static int ath9k_tx_last_beacon(struct ieee80211_hw *hw)
  1716. {
  1717. struct ath_softc *sc = hw->priv;
  1718. struct ath_hw *ah = sc->sc_ah;
  1719. struct ieee80211_vif *vif;
  1720. struct ath_vif *avp;
  1721. struct ath_buf *bf;
  1722. struct ath_tx_status ts;
  1723. bool edma = !!(ah->caps.hw_caps & ATH9K_HW_CAP_EDMA);
  1724. int status;
  1725. vif = sc->beacon.bslot[0];
  1726. if (!vif)
  1727. return 0;
  1728. if (!vif->bss_conf.enable_beacon)
  1729. return 0;
  1730. avp = (void *)vif->drv_priv;
  1731. if (!sc->beacon.tx_processed && !edma) {
  1732. tasklet_disable(&sc->bcon_tasklet);
  1733. bf = avp->av_bcbuf;
  1734. if (!bf || !bf->bf_mpdu)
  1735. goto skip;
  1736. status = ath9k_hw_txprocdesc(ah, bf->bf_desc, &ts);
  1737. if (status == -EINPROGRESS)
  1738. goto skip;
  1739. sc->beacon.tx_processed = true;
  1740. sc->beacon.tx_last = !(ts.ts_status & ATH9K_TXERR_MASK);
  1741. skip:
  1742. tasklet_enable(&sc->bcon_tasklet);
  1743. }
  1744. return sc->beacon.tx_last;
  1745. }
  1746. static int ath9k_get_stats(struct ieee80211_hw *hw,
  1747. struct ieee80211_low_level_stats *stats)
  1748. {
  1749. struct ath_softc *sc = hw->priv;
  1750. struct ath_hw *ah = sc->sc_ah;
  1751. struct ath9k_mib_stats *mib_stats = &ah->ah_mibStats;
  1752. stats->dot11ACKFailureCount = mib_stats->ackrcv_bad;
  1753. stats->dot11RTSFailureCount = mib_stats->rts_bad;
  1754. stats->dot11FCSErrorCount = mib_stats->fcs_bad;
  1755. stats->dot11RTSSuccessCount = mib_stats->rts_good;
  1756. return 0;
  1757. }
  1758. static u32 fill_chainmask(u32 cap, u32 new)
  1759. {
  1760. u32 filled = 0;
  1761. int i;
  1762. for (i = 0; cap && new; i++, cap >>= 1) {
  1763. if (!(cap & BIT(0)))
  1764. continue;
  1765. if (new & BIT(0))
  1766. filled |= BIT(i);
  1767. new >>= 1;
  1768. }
  1769. return filled;
  1770. }
  1771. static bool validate_antenna_mask(struct ath_hw *ah, u32 val)
  1772. {
  1773. if (AR_SREV_9300_20_OR_LATER(ah))
  1774. return true;
  1775. switch (val & 0x7) {
  1776. case 0x1:
  1777. case 0x3:
  1778. case 0x7:
  1779. return true;
  1780. case 0x2:
  1781. return (ah->caps.rx_chainmask == 1);
  1782. default:
  1783. return false;
  1784. }
  1785. }
  1786. static int ath9k_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
  1787. {
  1788. struct ath_softc *sc = hw->priv;
  1789. struct ath_hw *ah = sc->sc_ah;
  1790. if (ah->caps.rx_chainmask != 1)
  1791. rx_ant |= tx_ant;
  1792. if (!validate_antenna_mask(ah, rx_ant) || !tx_ant)
  1793. return -EINVAL;
  1794. sc->ant_rx = rx_ant;
  1795. sc->ant_tx = tx_ant;
  1796. if (ah->caps.rx_chainmask == 1)
  1797. return 0;
  1798. /* AR9100 runs into calibration issues if not all rx chains are enabled */
  1799. if (AR_SREV_9100(ah))
  1800. ah->rxchainmask = 0x7;
  1801. else
  1802. ah->rxchainmask = fill_chainmask(ah->caps.rx_chainmask, rx_ant);
  1803. ah->txchainmask = fill_chainmask(ah->caps.tx_chainmask, tx_ant);
  1804. ath9k_cmn_reload_chainmask(ah);
  1805. return 0;
  1806. }
  1807. static int ath9k_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
  1808. {
  1809. struct ath_softc *sc = hw->priv;
  1810. *tx_ant = sc->ant_tx;
  1811. *rx_ant = sc->ant_rx;
  1812. return 0;
  1813. }
  1814. static void ath9k_sw_scan_start(struct ieee80211_hw *hw,
  1815. struct ieee80211_vif *vif,
  1816. const u8 *mac_addr)
  1817. {
  1818. struct ath_softc *sc = hw->priv;
  1819. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  1820. set_bit(ATH_OP_SCANNING, &common->op_flags);
  1821. }
  1822. static void ath9k_sw_scan_complete(struct ieee80211_hw *hw,
  1823. struct ieee80211_vif *vif)
  1824. {
  1825. struct ath_softc *sc = hw->priv;
  1826. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  1827. clear_bit(ATH_OP_SCANNING, &common->op_flags);
  1828. }
  1829. #ifdef CONFIG_ATH9K_CHANNEL_CONTEXT
  1830. static void ath9k_cancel_pending_offchannel(struct ath_softc *sc)
  1831. {
  1832. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  1833. if (sc->offchannel.roc_vif) {
  1834. ath_dbg(common, CHAN_CTX,
  1835. "%s: Aborting RoC\n", __func__);
  1836. del_timer_sync(&sc->offchannel.timer);
  1837. if (sc->offchannel.state >= ATH_OFFCHANNEL_ROC_START)
  1838. ath_roc_complete(sc, ATH_ROC_COMPLETE_ABORT);
  1839. }
  1840. if (test_bit(ATH_OP_SCANNING, &common->op_flags)) {
  1841. ath_dbg(common, CHAN_CTX,
  1842. "%s: Aborting HW scan\n", __func__);
  1843. del_timer_sync(&sc->offchannel.timer);
  1844. ath_scan_complete(sc, true);
  1845. }
  1846. }
  1847. static int ath9k_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  1848. struct ieee80211_scan_request *hw_req)
  1849. {
  1850. struct cfg80211_scan_request *req = &hw_req->req;
  1851. struct ath_softc *sc = hw->priv;
  1852. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  1853. int ret = 0;
  1854. mutex_lock(&sc->mutex);
  1855. if (WARN_ON(sc->offchannel.scan_req)) {
  1856. ret = -EBUSY;
  1857. goto out;
  1858. }
  1859. ath9k_ps_wakeup(sc);
  1860. set_bit(ATH_OP_SCANNING, &common->op_flags);
  1861. sc->offchannel.scan_vif = vif;
  1862. sc->offchannel.scan_req = req;
  1863. sc->offchannel.scan_idx = 0;
  1864. ath_dbg(common, CHAN_CTX, "HW scan request received on vif: %pM\n",
  1865. vif->addr);
  1866. if (sc->offchannel.state == ATH_OFFCHANNEL_IDLE) {
  1867. ath_dbg(common, CHAN_CTX, "Starting HW scan\n");
  1868. ath_offchannel_next(sc);
  1869. }
  1870. out:
  1871. mutex_unlock(&sc->mutex);
  1872. return ret;
  1873. }
  1874. static void ath9k_cancel_hw_scan(struct ieee80211_hw *hw,
  1875. struct ieee80211_vif *vif)
  1876. {
  1877. struct ath_softc *sc = hw->priv;
  1878. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  1879. ath_dbg(common, CHAN_CTX, "Cancel HW scan on vif: %pM\n", vif->addr);
  1880. mutex_lock(&sc->mutex);
  1881. del_timer_sync(&sc->offchannel.timer);
  1882. ath_scan_complete(sc, true);
  1883. mutex_unlock(&sc->mutex);
  1884. }
  1885. static int ath9k_remain_on_channel(struct ieee80211_hw *hw,
  1886. struct ieee80211_vif *vif,
  1887. struct ieee80211_channel *chan, int duration,
  1888. enum ieee80211_roc_type type)
  1889. {
  1890. struct ath_softc *sc = hw->priv;
  1891. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  1892. int ret = 0;
  1893. mutex_lock(&sc->mutex);
  1894. if (WARN_ON(sc->offchannel.roc_vif)) {
  1895. ret = -EBUSY;
  1896. goto out;
  1897. }
  1898. ath9k_ps_wakeup(sc);
  1899. sc->offchannel.roc_vif = vif;
  1900. sc->offchannel.roc_chan = chan;
  1901. sc->offchannel.roc_duration = duration;
  1902. ath_dbg(common, CHAN_CTX,
  1903. "RoC request on vif: %pM, type: %d duration: %d\n",
  1904. vif->addr, type, duration);
  1905. if (sc->offchannel.state == ATH_OFFCHANNEL_IDLE) {
  1906. ath_dbg(common, CHAN_CTX, "Starting RoC period\n");
  1907. ath_offchannel_next(sc);
  1908. }
  1909. out:
  1910. mutex_unlock(&sc->mutex);
  1911. return ret;
  1912. }
  1913. static int ath9k_cancel_remain_on_channel(struct ieee80211_hw *hw)
  1914. {
  1915. struct ath_softc *sc = hw->priv;
  1916. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  1917. mutex_lock(&sc->mutex);
  1918. ath_dbg(common, CHAN_CTX, "Cancel RoC\n");
  1919. del_timer_sync(&sc->offchannel.timer);
  1920. if (sc->offchannel.roc_vif) {
  1921. if (sc->offchannel.state >= ATH_OFFCHANNEL_ROC_START)
  1922. ath_roc_complete(sc, ATH_ROC_COMPLETE_CANCEL);
  1923. }
  1924. mutex_unlock(&sc->mutex);
  1925. return 0;
  1926. }
  1927. static int ath9k_add_chanctx(struct ieee80211_hw *hw,
  1928. struct ieee80211_chanctx_conf *conf)
  1929. {
  1930. struct ath_softc *sc = hw->priv;
  1931. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  1932. struct ath_chanctx *ctx, **ptr;
  1933. int pos;
  1934. mutex_lock(&sc->mutex);
  1935. ath_for_each_chanctx(sc, ctx) {
  1936. if (ctx->assigned)
  1937. continue;
  1938. ptr = (void *) conf->drv_priv;
  1939. *ptr = ctx;
  1940. ctx->assigned = true;
  1941. pos = ctx - &sc->chanctx[0];
  1942. ctx->hw_queue_base = pos * IEEE80211_NUM_ACS;
  1943. ath_dbg(common, CHAN_CTX,
  1944. "Add channel context: %d MHz\n",
  1945. conf->def.chan->center_freq);
  1946. ath_chanctx_set_channel(sc, ctx, &conf->def);
  1947. mutex_unlock(&sc->mutex);
  1948. return 0;
  1949. }
  1950. mutex_unlock(&sc->mutex);
  1951. return -ENOSPC;
  1952. }
  1953. static void ath9k_remove_chanctx(struct ieee80211_hw *hw,
  1954. struct ieee80211_chanctx_conf *conf)
  1955. {
  1956. struct ath_softc *sc = hw->priv;
  1957. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  1958. struct ath_chanctx *ctx = ath_chanctx_get(conf);
  1959. mutex_lock(&sc->mutex);
  1960. ath_dbg(common, CHAN_CTX,
  1961. "Remove channel context: %d MHz\n",
  1962. conf->def.chan->center_freq);
  1963. ctx->assigned = false;
  1964. ctx->hw_queue_base = 0;
  1965. ath_chanctx_event(sc, NULL, ATH_CHANCTX_EVENT_UNASSIGN);
  1966. mutex_unlock(&sc->mutex);
  1967. }
  1968. static void ath9k_change_chanctx(struct ieee80211_hw *hw,
  1969. struct ieee80211_chanctx_conf *conf,
  1970. u32 changed)
  1971. {
  1972. struct ath_softc *sc = hw->priv;
  1973. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  1974. struct ath_chanctx *ctx = ath_chanctx_get(conf);
  1975. mutex_lock(&sc->mutex);
  1976. ath_dbg(common, CHAN_CTX,
  1977. "Change channel context: %d MHz\n",
  1978. conf->def.chan->center_freq);
  1979. ath_chanctx_set_channel(sc, ctx, &conf->def);
  1980. mutex_unlock(&sc->mutex);
  1981. }
  1982. static int ath9k_assign_vif_chanctx(struct ieee80211_hw *hw,
  1983. struct ieee80211_vif *vif,
  1984. struct ieee80211_chanctx_conf *conf)
  1985. {
  1986. struct ath_softc *sc = hw->priv;
  1987. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  1988. struct ath_vif *avp = (void *)vif->drv_priv;
  1989. struct ath_chanctx *ctx = ath_chanctx_get(conf);
  1990. int i;
  1991. ath9k_cancel_pending_offchannel(sc);
  1992. mutex_lock(&sc->mutex);
  1993. ath_dbg(common, CHAN_CTX,
  1994. "Assign VIF (addr: %pM, type: %d, p2p: %d) to channel context: %d MHz\n",
  1995. vif->addr, vif->type, vif->p2p,
  1996. conf->def.chan->center_freq);
  1997. avp->chanctx = ctx;
  1998. ctx->nvifs_assigned++;
  1999. list_add_tail(&avp->list, &ctx->vifs);
  2000. ath9k_calculate_summary_state(sc, ctx);
  2001. for (i = 0; i < IEEE80211_NUM_ACS; i++)
  2002. vif->hw_queue[i] = ctx->hw_queue_base + i;
  2003. mutex_unlock(&sc->mutex);
  2004. return 0;
  2005. }
  2006. static void ath9k_unassign_vif_chanctx(struct ieee80211_hw *hw,
  2007. struct ieee80211_vif *vif,
  2008. struct ieee80211_chanctx_conf *conf)
  2009. {
  2010. struct ath_softc *sc = hw->priv;
  2011. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  2012. struct ath_vif *avp = (void *)vif->drv_priv;
  2013. struct ath_chanctx *ctx = ath_chanctx_get(conf);
  2014. int ac;
  2015. ath9k_cancel_pending_offchannel(sc);
  2016. mutex_lock(&sc->mutex);
  2017. ath_dbg(common, CHAN_CTX,
  2018. "Remove VIF (addr: %pM, type: %d, p2p: %d) from channel context: %d MHz\n",
  2019. vif->addr, vif->type, vif->p2p,
  2020. conf->def.chan->center_freq);
  2021. avp->chanctx = NULL;
  2022. ctx->nvifs_assigned--;
  2023. list_del(&avp->list);
  2024. ath9k_calculate_summary_state(sc, ctx);
  2025. for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
  2026. vif->hw_queue[ac] = IEEE80211_INVAL_HW_QUEUE;
  2027. mutex_unlock(&sc->mutex);
  2028. }
  2029. static void ath9k_mgd_prepare_tx(struct ieee80211_hw *hw,
  2030. struct ieee80211_vif *vif)
  2031. {
  2032. struct ath_softc *sc = hw->priv;
  2033. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  2034. struct ath_vif *avp = (struct ath_vif *) vif->drv_priv;
  2035. struct ath_beacon_config *cur_conf;
  2036. struct ath_chanctx *go_ctx;
  2037. unsigned long timeout;
  2038. bool changed = false;
  2039. u32 beacon_int;
  2040. if (!test_bit(ATH_OP_MULTI_CHANNEL, &common->op_flags))
  2041. return;
  2042. if (!avp->chanctx)
  2043. return;
  2044. mutex_lock(&sc->mutex);
  2045. spin_lock_bh(&sc->chan_lock);
  2046. if (sc->next_chan || (sc->cur_chan != avp->chanctx))
  2047. changed = true;
  2048. spin_unlock_bh(&sc->chan_lock);
  2049. if (!changed)
  2050. goto out;
  2051. ath9k_cancel_pending_offchannel(sc);
  2052. go_ctx = ath_is_go_chanctx_present(sc);
  2053. if (go_ctx) {
  2054. /*
  2055. * Wait till the GO interface gets a chance
  2056. * to send out an NoA.
  2057. */
  2058. spin_lock_bh(&sc->chan_lock);
  2059. sc->sched.mgd_prepare_tx = true;
  2060. cur_conf = &go_ctx->beacon;
  2061. beacon_int = TU_TO_USEC(cur_conf->beacon_interval);
  2062. spin_unlock_bh(&sc->chan_lock);
  2063. timeout = usecs_to_jiffies(beacon_int * 2);
  2064. init_completion(&sc->go_beacon);
  2065. mutex_unlock(&sc->mutex);
  2066. if (wait_for_completion_timeout(&sc->go_beacon,
  2067. timeout) == 0) {
  2068. ath_dbg(common, CHAN_CTX,
  2069. "Failed to send new NoA\n");
  2070. spin_lock_bh(&sc->chan_lock);
  2071. sc->sched.mgd_prepare_tx = false;
  2072. spin_unlock_bh(&sc->chan_lock);
  2073. }
  2074. mutex_lock(&sc->mutex);
  2075. }
  2076. ath_dbg(common, CHAN_CTX,
  2077. "%s: Set chanctx state to FORCE_ACTIVE for vif: %pM\n",
  2078. __func__, vif->addr);
  2079. spin_lock_bh(&sc->chan_lock);
  2080. sc->next_chan = avp->chanctx;
  2081. sc->sched.state = ATH_CHANCTX_STATE_FORCE_ACTIVE;
  2082. spin_unlock_bh(&sc->chan_lock);
  2083. ath_chanctx_set_next(sc, true);
  2084. out:
  2085. mutex_unlock(&sc->mutex);
  2086. }
  2087. void ath9k_fill_chanctx_ops(void)
  2088. {
  2089. if (!ath9k_is_chanctx_enabled())
  2090. return;
  2091. ath9k_ops.hw_scan = ath9k_hw_scan;
  2092. ath9k_ops.cancel_hw_scan = ath9k_cancel_hw_scan;
  2093. ath9k_ops.remain_on_channel = ath9k_remain_on_channel;
  2094. ath9k_ops.cancel_remain_on_channel = ath9k_cancel_remain_on_channel;
  2095. ath9k_ops.add_chanctx = ath9k_add_chanctx;
  2096. ath9k_ops.remove_chanctx = ath9k_remove_chanctx;
  2097. ath9k_ops.change_chanctx = ath9k_change_chanctx;
  2098. ath9k_ops.assign_vif_chanctx = ath9k_assign_vif_chanctx;
  2099. ath9k_ops.unassign_vif_chanctx = ath9k_unassign_vif_chanctx;
  2100. ath9k_ops.mgd_prepare_tx = ath9k_mgd_prepare_tx;
  2101. }
  2102. #endif
  2103. static int ath9k_get_txpower(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  2104. int *dbm)
  2105. {
  2106. struct ath_softc *sc = hw->priv;
  2107. struct ath_vif *avp = (void *)vif->drv_priv;
  2108. mutex_lock(&sc->mutex);
  2109. if (avp->chanctx)
  2110. *dbm = avp->chanctx->cur_txpower;
  2111. else
  2112. *dbm = sc->cur_chan->cur_txpower;
  2113. mutex_unlock(&sc->mutex);
  2114. *dbm /= 2;
  2115. return 0;
  2116. }
  2117. struct ieee80211_ops ath9k_ops = {
  2118. .tx = ath9k_tx,
  2119. .start = ath9k_start,
  2120. .stop = ath9k_stop,
  2121. .add_interface = ath9k_add_interface,
  2122. .change_interface = ath9k_change_interface,
  2123. .remove_interface = ath9k_remove_interface,
  2124. .config = ath9k_config,
  2125. .configure_filter = ath9k_configure_filter,
  2126. .sta_state = ath9k_sta_state,
  2127. .sta_notify = ath9k_sta_notify,
  2128. .conf_tx = ath9k_conf_tx,
  2129. .bss_info_changed = ath9k_bss_info_changed,
  2130. .set_key = ath9k_set_key,
  2131. .get_tsf = ath9k_get_tsf,
  2132. .set_tsf = ath9k_set_tsf,
  2133. .reset_tsf = ath9k_reset_tsf,
  2134. .ampdu_action = ath9k_ampdu_action,
  2135. .get_survey = ath9k_get_survey,
  2136. .rfkill_poll = ath9k_rfkill_poll_state,
  2137. .set_coverage_class = ath9k_set_coverage_class,
  2138. .flush = ath9k_flush,
  2139. .tx_frames_pending = ath9k_tx_frames_pending,
  2140. .tx_last_beacon = ath9k_tx_last_beacon,
  2141. .release_buffered_frames = ath9k_release_buffered_frames,
  2142. .get_stats = ath9k_get_stats,
  2143. .set_antenna = ath9k_set_antenna,
  2144. .get_antenna = ath9k_get_antenna,
  2145. #ifdef CONFIG_ATH9K_WOW
  2146. .suspend = ath9k_suspend,
  2147. .resume = ath9k_resume,
  2148. .set_wakeup = ath9k_set_wakeup,
  2149. #endif
  2150. #ifdef CONFIG_ATH9K_DEBUGFS
  2151. .get_et_sset_count = ath9k_get_et_sset_count,
  2152. .get_et_stats = ath9k_get_et_stats,
  2153. .get_et_strings = ath9k_get_et_strings,
  2154. #endif
  2155. #if defined(CONFIG_MAC80211_DEBUGFS) && defined(CONFIG_ATH9K_STATION_STATISTICS)
  2156. .sta_add_debugfs = ath9k_sta_add_debugfs,
  2157. #endif
  2158. .sw_scan_start = ath9k_sw_scan_start,
  2159. .sw_scan_complete = ath9k_sw_scan_complete,
  2160. .get_txpower = ath9k_get_txpower,
  2161. };