main.c 63 KB

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