main.c 63 KB

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