main.c 65 KB

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