main.c 65 KB

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