main.c 63 KB

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