main.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280
  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. static void ath9k_set_assoc_state(struct ath_softc *sc,
  21. struct ieee80211_vif *vif);
  22. u8 ath9k_parse_mpdudensity(u8 mpdudensity)
  23. {
  24. /*
  25. * 802.11n D2.0 defined values for "Minimum MPDU Start Spacing":
  26. * 0 for no restriction
  27. * 1 for 1/4 us
  28. * 2 for 1/2 us
  29. * 3 for 1 us
  30. * 4 for 2 us
  31. * 5 for 4 us
  32. * 6 for 8 us
  33. * 7 for 16 us
  34. */
  35. switch (mpdudensity) {
  36. case 0:
  37. return 0;
  38. case 1:
  39. case 2:
  40. case 3:
  41. /* Our lower layer calculations limit our precision to
  42. 1 microsecond */
  43. return 1;
  44. case 4:
  45. return 2;
  46. case 5:
  47. return 4;
  48. case 6:
  49. return 8;
  50. case 7:
  51. return 16;
  52. default:
  53. return 0;
  54. }
  55. }
  56. static bool ath9k_has_pending_frames(struct ath_softc *sc, struct ath_txq *txq)
  57. {
  58. bool pending = false;
  59. spin_lock_bh(&txq->axq_lock);
  60. if (txq->axq_depth || !list_empty(&txq->axq_acq))
  61. pending = true;
  62. spin_unlock_bh(&txq->axq_lock);
  63. return pending;
  64. }
  65. static bool ath9k_setpower(struct ath_softc *sc, enum ath9k_power_mode mode)
  66. {
  67. unsigned long flags;
  68. bool ret;
  69. spin_lock_irqsave(&sc->sc_pm_lock, flags);
  70. ret = ath9k_hw_setpower(sc->sc_ah, mode);
  71. spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
  72. return ret;
  73. }
  74. void ath_ps_full_sleep(unsigned long data)
  75. {
  76. struct ath_softc *sc = (struct ath_softc *) data;
  77. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  78. bool reset;
  79. spin_lock(&common->cc_lock);
  80. ath_hw_cycle_counters_update(common);
  81. spin_unlock(&common->cc_lock);
  82. ath9k_hw_setrxabort(sc->sc_ah, 1);
  83. ath9k_hw_stopdmarecv(sc->sc_ah, &reset);
  84. ath9k_hw_setpower(sc->sc_ah, ATH9K_PM_FULL_SLEEP);
  85. }
  86. void ath9k_ps_wakeup(struct ath_softc *sc)
  87. {
  88. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  89. unsigned long flags;
  90. enum ath9k_power_mode power_mode;
  91. spin_lock_irqsave(&sc->sc_pm_lock, flags);
  92. if (++sc->ps_usecount != 1)
  93. goto unlock;
  94. del_timer_sync(&sc->sleep_timer);
  95. power_mode = sc->sc_ah->power_mode;
  96. ath9k_hw_setpower(sc->sc_ah, ATH9K_PM_AWAKE);
  97. /*
  98. * While the hardware is asleep, the cycle counters contain no
  99. * useful data. Better clear them now so that they don't mess up
  100. * survey data results.
  101. */
  102. if (power_mode != ATH9K_PM_AWAKE) {
  103. spin_lock(&common->cc_lock);
  104. ath_hw_cycle_counters_update(common);
  105. memset(&common->cc_survey, 0, sizeof(common->cc_survey));
  106. memset(&common->cc_ani, 0, sizeof(common->cc_ani));
  107. spin_unlock(&common->cc_lock);
  108. }
  109. unlock:
  110. spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
  111. }
  112. void ath9k_ps_restore(struct ath_softc *sc)
  113. {
  114. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  115. enum ath9k_power_mode mode;
  116. unsigned long flags;
  117. spin_lock_irqsave(&sc->sc_pm_lock, flags);
  118. if (--sc->ps_usecount != 0)
  119. goto unlock;
  120. if (sc->ps_idle) {
  121. mod_timer(&sc->sleep_timer, jiffies + HZ / 10);
  122. goto unlock;
  123. }
  124. if (sc->ps_enabled &&
  125. !(sc->ps_flags & (PS_WAIT_FOR_BEACON |
  126. PS_WAIT_FOR_CAB |
  127. PS_WAIT_FOR_PSPOLL_DATA |
  128. PS_WAIT_FOR_TX_ACK |
  129. PS_WAIT_FOR_ANI))) {
  130. mode = ATH9K_PM_NETWORK_SLEEP;
  131. if (ath9k_hw_btcoex_is_enabled(sc->sc_ah))
  132. ath9k_btcoex_stop_gen_timer(sc);
  133. } else {
  134. goto unlock;
  135. }
  136. spin_lock(&common->cc_lock);
  137. ath_hw_cycle_counters_update(common);
  138. spin_unlock(&common->cc_lock);
  139. ath9k_hw_setpower(sc->sc_ah, mode);
  140. unlock:
  141. spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
  142. }
  143. static void __ath_cancel_work(struct ath_softc *sc)
  144. {
  145. cancel_work_sync(&sc->paprd_work);
  146. cancel_delayed_work_sync(&sc->tx_complete_work);
  147. cancel_delayed_work_sync(&sc->hw_pll_work);
  148. #ifdef CONFIG_ATH9K_BTCOEX_SUPPORT
  149. if (ath9k_hw_mci_is_enabled(sc->sc_ah))
  150. cancel_work_sync(&sc->mci_work);
  151. #endif
  152. }
  153. void ath_cancel_work(struct ath_softc *sc)
  154. {
  155. __ath_cancel_work(sc);
  156. cancel_work_sync(&sc->hw_reset_work);
  157. }
  158. void ath_restart_work(struct ath_softc *sc)
  159. {
  160. ieee80211_queue_delayed_work(sc->hw, &sc->tx_complete_work, 0);
  161. if (AR_SREV_9340(sc->sc_ah) || AR_SREV_9330(sc->sc_ah))
  162. ieee80211_queue_delayed_work(sc->hw, &sc->hw_pll_work,
  163. msecs_to_jiffies(ATH_PLL_WORK_INTERVAL));
  164. ath_start_ani(sc);
  165. }
  166. static bool ath_prepare_reset(struct ath_softc *sc)
  167. {
  168. struct ath_hw *ah = sc->sc_ah;
  169. bool ret = true;
  170. ieee80211_stop_queues(sc->hw);
  171. ath_stop_ani(sc);
  172. ath9k_hw_disable_interrupts(ah);
  173. if (!ath_drain_all_txq(sc))
  174. ret = false;
  175. if (!ath_stoprecv(sc))
  176. ret = false;
  177. return ret;
  178. }
  179. static bool ath_complete_reset(struct ath_softc *sc, bool start)
  180. {
  181. struct ath_hw *ah = sc->sc_ah;
  182. struct ath_common *common = ath9k_hw_common(ah);
  183. unsigned long flags;
  184. int i;
  185. if (ath_startrecv(sc) != 0) {
  186. ath_err(common, "Unable to restart recv logic\n");
  187. return false;
  188. }
  189. ath9k_cmn_update_txpow(ah, sc->curtxpow,
  190. sc->config.txpowlimit, &sc->curtxpow);
  191. clear_bit(ATH_OP_HW_RESET, &common->op_flags);
  192. ath9k_hw_set_interrupts(ah);
  193. ath9k_hw_enable_interrupts(ah);
  194. if (!(sc->hw->conf.flags & IEEE80211_CONF_OFFCHANNEL) && start) {
  195. if (!test_bit(ATH_OP_BEACONS, &common->op_flags))
  196. goto work;
  197. if (ah->opmode == NL80211_IFTYPE_STATION &&
  198. test_bit(ATH_OP_PRIM_STA_VIF, &common->op_flags)) {
  199. spin_lock_irqsave(&sc->sc_pm_lock, flags);
  200. sc->ps_flags |= PS_BEACON_SYNC | PS_WAIT_FOR_BEACON;
  201. spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
  202. } else {
  203. ath9k_set_beacon(sc);
  204. }
  205. work:
  206. ath_restart_work(sc);
  207. for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
  208. if (!ATH_TXQ_SETUP(sc, i))
  209. continue;
  210. spin_lock_bh(&sc->tx.txq[i].axq_lock);
  211. ath_txq_schedule(sc, &sc->tx.txq[i]);
  212. spin_unlock_bh(&sc->tx.txq[i].axq_lock);
  213. }
  214. }
  215. sc->gtt_cnt = 0;
  216. ieee80211_wake_queues(sc->hw);
  217. ath9k_p2p_ps_timer(sc);
  218. return true;
  219. }
  220. static int ath_reset_internal(struct ath_softc *sc, struct ath9k_channel *hchan)
  221. {
  222. struct ath_hw *ah = sc->sc_ah;
  223. struct ath_common *common = ath9k_hw_common(ah);
  224. struct ath9k_hw_cal_data *caldata = NULL;
  225. bool fastcc = true;
  226. int r;
  227. __ath_cancel_work(sc);
  228. tasklet_disable(&sc->intr_tq);
  229. spin_lock_bh(&sc->sc_pcu_lock);
  230. if (!(sc->hw->conf.flags & IEEE80211_CONF_OFFCHANNEL)) {
  231. fastcc = false;
  232. caldata = &sc->caldata;
  233. }
  234. if (!hchan) {
  235. fastcc = false;
  236. hchan = ah->curchan;
  237. }
  238. if (!ath_prepare_reset(sc))
  239. fastcc = false;
  240. ath_dbg(common, CONFIG, "Reset to %u MHz, HT40: %d fastcc: %d\n",
  241. hchan->channel, IS_CHAN_HT40(hchan), fastcc);
  242. r = ath9k_hw_reset(ah, hchan, caldata, fastcc);
  243. if (r) {
  244. ath_err(common,
  245. "Unable to reset channel, reset status %d\n", r);
  246. ath9k_hw_enable_interrupts(ah);
  247. ath9k_queue_reset(sc, RESET_TYPE_BB_HANG);
  248. goto out;
  249. }
  250. if (ath9k_hw_mci_is_enabled(sc->sc_ah) &&
  251. (sc->hw->conf.flags & IEEE80211_CONF_OFFCHANNEL))
  252. ath9k_mci_set_txpower(sc, true, false);
  253. if (!ath_complete_reset(sc, true))
  254. r = -EIO;
  255. out:
  256. spin_unlock_bh(&sc->sc_pcu_lock);
  257. tasklet_enable(&sc->intr_tq);
  258. return r;
  259. }
  260. /*
  261. * Set/change channels. If the channel is really being changed, it's done
  262. * by reseting the chip. To accomplish this we must first cleanup any pending
  263. * DMA, then restart stuff.
  264. */
  265. static int ath_set_channel(struct ath_softc *sc, struct cfg80211_chan_def *chandef)
  266. {
  267. struct ath_hw *ah = sc->sc_ah;
  268. struct ath_common *common = ath9k_hw_common(ah);
  269. struct ieee80211_hw *hw = sc->hw;
  270. struct ath9k_channel *hchan;
  271. struct ieee80211_channel *chan = chandef->chan;
  272. bool offchannel;
  273. int pos = chan->hw_value;
  274. int old_pos = -1;
  275. int r;
  276. if (test_bit(ATH_OP_INVALID, &common->op_flags))
  277. return -EIO;
  278. offchannel = !!(hw->conf.flags & IEEE80211_CONF_OFFCHANNEL);
  279. if (ah->curchan)
  280. old_pos = ah->curchan - &ah->channels[0];
  281. ath_dbg(common, CONFIG, "Set channel: %d MHz width: %d\n",
  282. chan->center_freq, chandef->width);
  283. /* update survey stats for the old channel before switching */
  284. spin_lock_bh(&common->cc_lock);
  285. ath_update_survey_stats(sc);
  286. spin_unlock_bh(&common->cc_lock);
  287. ath9k_cmn_get_channel(hw, ah, chandef);
  288. /*
  289. * If the operating channel changes, change the survey in-use flags
  290. * along with it.
  291. * Reset the survey data for the new channel, unless we're switching
  292. * back to the operating channel from an off-channel operation.
  293. */
  294. if (!offchannel && sc->cur_survey != &sc->survey[pos]) {
  295. if (sc->cur_survey)
  296. sc->cur_survey->filled &= ~SURVEY_INFO_IN_USE;
  297. sc->cur_survey = &sc->survey[pos];
  298. memset(sc->cur_survey, 0, sizeof(struct survey_info));
  299. sc->cur_survey->filled |= SURVEY_INFO_IN_USE;
  300. } else if (!(sc->survey[pos].filled & SURVEY_INFO_IN_USE)) {
  301. memset(&sc->survey[pos], 0, sizeof(struct survey_info));
  302. }
  303. hchan = &sc->sc_ah->channels[pos];
  304. r = ath_reset_internal(sc, hchan);
  305. if (r)
  306. return r;
  307. /*
  308. * The most recent snapshot of channel->noisefloor for the old
  309. * channel is only available after the hardware reset. Copy it to
  310. * the survey stats now.
  311. */
  312. if (old_pos >= 0)
  313. ath_update_survey_nf(sc, old_pos);
  314. /*
  315. * Enable radar pulse detection if on a DFS channel. Spectral
  316. * scanning and radar detection can not be used concurrently.
  317. */
  318. if (hw->conf.radar_enabled) {
  319. u32 rxfilter;
  320. /* set HW specific DFS configuration */
  321. ath9k_hw_set_radar_params(ah);
  322. rxfilter = ath9k_hw_getrxfilter(ah);
  323. rxfilter |= ATH9K_RX_FILTER_PHYRADAR |
  324. ATH9K_RX_FILTER_PHYERR;
  325. ath9k_hw_setrxfilter(ah, rxfilter);
  326. ath_dbg(common, DFS, "DFS enabled at freq %d\n",
  327. chan->center_freq);
  328. } else {
  329. /* perform spectral scan if requested. */
  330. if (test_bit(ATH_OP_SCANNING, &common->op_flags) &&
  331. sc->spectral_mode == SPECTRAL_CHANSCAN)
  332. ath9k_spectral_scan_trigger(hw);
  333. }
  334. return 0;
  335. }
  336. static void ath_node_attach(struct ath_softc *sc, struct ieee80211_sta *sta,
  337. struct ieee80211_vif *vif)
  338. {
  339. struct ath_node *an;
  340. an = (struct ath_node *)sta->drv_priv;
  341. an->sc = sc;
  342. an->sta = sta;
  343. an->vif = vif;
  344. memset(&an->key_idx, 0, sizeof(an->key_idx));
  345. ath_tx_node_init(sc, an);
  346. }
  347. static void ath_node_detach(struct ath_softc *sc, struct ieee80211_sta *sta)
  348. {
  349. struct ath_node *an = (struct ath_node *)sta->drv_priv;
  350. ath_tx_node_cleanup(sc, an);
  351. }
  352. void ath9k_tasklet(unsigned long data)
  353. {
  354. struct ath_softc *sc = (struct ath_softc *)data;
  355. struct ath_hw *ah = sc->sc_ah;
  356. struct ath_common *common = ath9k_hw_common(ah);
  357. enum ath_reset_type type;
  358. unsigned long flags;
  359. u32 status = sc->intrstatus;
  360. u32 rxmask;
  361. ath9k_ps_wakeup(sc);
  362. spin_lock(&sc->sc_pcu_lock);
  363. if (status & ATH9K_INT_FATAL) {
  364. type = RESET_TYPE_FATAL_INT;
  365. ath9k_queue_reset(sc, type);
  366. /*
  367. * Increment the ref. counter here so that
  368. * interrupts are enabled in the reset routine.
  369. */
  370. atomic_inc(&ah->intr_ref_cnt);
  371. ath_dbg(common, RESET, "FATAL: Skipping interrupts\n");
  372. goto out;
  373. }
  374. if ((ah->config.hw_hang_checks & HW_BB_WATCHDOG) &&
  375. (status & ATH9K_INT_BB_WATCHDOG)) {
  376. spin_lock(&common->cc_lock);
  377. ath_hw_cycle_counters_update(common);
  378. ar9003_hw_bb_watchdog_dbg_info(ah);
  379. spin_unlock(&common->cc_lock);
  380. if (ar9003_hw_bb_watchdog_check(ah)) {
  381. type = RESET_TYPE_BB_WATCHDOG;
  382. ath9k_queue_reset(sc, type);
  383. /*
  384. * Increment the ref. counter here so that
  385. * interrupts are enabled in the reset routine.
  386. */
  387. atomic_inc(&ah->intr_ref_cnt);
  388. ath_dbg(common, RESET,
  389. "BB_WATCHDOG: Skipping interrupts\n");
  390. goto out;
  391. }
  392. }
  393. if (status & ATH9K_INT_GTT) {
  394. sc->gtt_cnt++;
  395. if ((sc->gtt_cnt >= MAX_GTT_CNT) && !ath9k_hw_check_alive(ah)) {
  396. type = RESET_TYPE_TX_GTT;
  397. ath9k_queue_reset(sc, type);
  398. atomic_inc(&ah->intr_ref_cnt);
  399. ath_dbg(common, RESET,
  400. "GTT: Skipping interrupts\n");
  401. goto out;
  402. }
  403. }
  404. spin_lock_irqsave(&sc->sc_pm_lock, flags);
  405. if ((status & ATH9K_INT_TSFOOR) && sc->ps_enabled) {
  406. /*
  407. * TSF sync does not look correct; remain awake to sync with
  408. * the next Beacon.
  409. */
  410. ath_dbg(common, PS, "TSFOOR - Sync with next Beacon\n");
  411. sc->ps_flags |= PS_WAIT_FOR_BEACON | PS_BEACON_SYNC;
  412. }
  413. spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
  414. if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
  415. rxmask = (ATH9K_INT_RXHP | ATH9K_INT_RXLP | ATH9K_INT_RXEOL |
  416. ATH9K_INT_RXORN);
  417. else
  418. rxmask = (ATH9K_INT_RX | ATH9K_INT_RXEOL | ATH9K_INT_RXORN);
  419. if (status & rxmask) {
  420. /* Check for high priority Rx first */
  421. if ((ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) &&
  422. (status & ATH9K_INT_RXHP))
  423. ath_rx_tasklet(sc, 0, true);
  424. ath_rx_tasklet(sc, 0, false);
  425. }
  426. if (status & ATH9K_INT_TX) {
  427. if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
  428. /*
  429. * For EDMA chips, TX completion is enabled for the
  430. * beacon queue, so if a beacon has been transmitted
  431. * successfully after a GTT interrupt, the GTT counter
  432. * gets reset to zero here.
  433. */
  434. sc->gtt_cnt = 0;
  435. ath_tx_edma_tasklet(sc);
  436. } else {
  437. ath_tx_tasklet(sc);
  438. }
  439. wake_up(&sc->tx_wait);
  440. }
  441. if (status & ATH9K_INT_GENTIMER)
  442. ath_gen_timer_isr(sc->sc_ah);
  443. ath9k_btcoex_handle_interrupt(sc, status);
  444. /* re-enable hardware interrupt */
  445. ath9k_hw_enable_interrupts(ah);
  446. out:
  447. spin_unlock(&sc->sc_pcu_lock);
  448. ath9k_ps_restore(sc);
  449. }
  450. irqreturn_t ath_isr(int irq, void *dev)
  451. {
  452. #define SCHED_INTR ( \
  453. ATH9K_INT_FATAL | \
  454. ATH9K_INT_BB_WATCHDOG | \
  455. ATH9K_INT_RXORN | \
  456. ATH9K_INT_RXEOL | \
  457. ATH9K_INT_RX | \
  458. ATH9K_INT_RXLP | \
  459. ATH9K_INT_RXHP | \
  460. ATH9K_INT_TX | \
  461. ATH9K_INT_BMISS | \
  462. ATH9K_INT_CST | \
  463. ATH9K_INT_GTT | \
  464. ATH9K_INT_TSFOOR | \
  465. ATH9K_INT_GENTIMER | \
  466. ATH9K_INT_MCI)
  467. struct ath_softc *sc = dev;
  468. struct ath_hw *ah = sc->sc_ah;
  469. struct ath_common *common = ath9k_hw_common(ah);
  470. enum ath9k_int status;
  471. u32 sync_cause = 0;
  472. bool sched = false;
  473. /*
  474. * The hardware is not ready/present, don't
  475. * touch anything. Note this can happen early
  476. * on if the IRQ is shared.
  477. */
  478. if (test_bit(ATH_OP_INVALID, &common->op_flags))
  479. return IRQ_NONE;
  480. /* shared irq, not for us */
  481. if (!ath9k_hw_intrpend(ah))
  482. return IRQ_NONE;
  483. if (test_bit(ATH_OP_HW_RESET, &common->op_flags)) {
  484. ath9k_hw_kill_interrupts(ah);
  485. return IRQ_HANDLED;
  486. }
  487. /*
  488. * Figure out the reason(s) for the interrupt. Note
  489. * that the hal returns a pseudo-ISR that may include
  490. * bits we haven't explicitly enabled so we mask the
  491. * value to insure we only process bits we requested.
  492. */
  493. ath9k_hw_getisr(ah, &status, &sync_cause); /* NB: clears ISR too */
  494. ath9k_debug_sync_cause(sc, sync_cause);
  495. status &= ah->imask; /* discard unasked-for bits */
  496. /*
  497. * If there are no status bits set, then this interrupt was not
  498. * for me (should have been caught above).
  499. */
  500. if (!status)
  501. return IRQ_NONE;
  502. /* Cache the status */
  503. sc->intrstatus = status;
  504. if (status & SCHED_INTR)
  505. sched = true;
  506. /*
  507. * If a FATAL or RXORN interrupt is received, we have to reset the
  508. * chip immediately.
  509. */
  510. if ((status & ATH9K_INT_FATAL) || ((status & ATH9K_INT_RXORN) &&
  511. !(ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)))
  512. goto chip_reset;
  513. if ((ah->config.hw_hang_checks & HW_BB_WATCHDOG) &&
  514. (status & ATH9K_INT_BB_WATCHDOG))
  515. goto chip_reset;
  516. #ifdef CONFIG_ATH9K_WOW
  517. if (status & ATH9K_INT_BMISS) {
  518. if (atomic_read(&sc->wow_sleep_proc_intr) == 0) {
  519. atomic_inc(&sc->wow_got_bmiss_intr);
  520. atomic_dec(&sc->wow_sleep_proc_intr);
  521. }
  522. }
  523. #endif
  524. if (status & ATH9K_INT_SWBA)
  525. tasklet_schedule(&sc->bcon_tasklet);
  526. if (status & ATH9K_INT_TXURN)
  527. ath9k_hw_updatetxtriglevel(ah, true);
  528. if (status & ATH9K_INT_RXEOL) {
  529. ah->imask &= ~(ATH9K_INT_RXEOL | ATH9K_INT_RXORN);
  530. ath9k_hw_set_interrupts(ah);
  531. }
  532. if (!(ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP))
  533. if (status & ATH9K_INT_TIM_TIMER) {
  534. if (ATH_DBG_WARN_ON_ONCE(sc->ps_idle))
  535. goto chip_reset;
  536. /* Clear RxAbort bit so that we can
  537. * receive frames */
  538. ath9k_setpower(sc, ATH9K_PM_AWAKE);
  539. spin_lock(&sc->sc_pm_lock);
  540. ath9k_hw_setrxabort(sc->sc_ah, 0);
  541. sc->ps_flags |= PS_WAIT_FOR_BEACON;
  542. spin_unlock(&sc->sc_pm_lock);
  543. }
  544. chip_reset:
  545. ath_debug_stat_interrupt(sc, status);
  546. if (sched) {
  547. /* turn off every interrupt */
  548. ath9k_hw_disable_interrupts(ah);
  549. tasklet_schedule(&sc->intr_tq);
  550. }
  551. return IRQ_HANDLED;
  552. #undef SCHED_INTR
  553. }
  554. int ath_reset(struct ath_softc *sc)
  555. {
  556. int r;
  557. ath9k_ps_wakeup(sc);
  558. r = ath_reset_internal(sc, NULL);
  559. ath9k_ps_restore(sc);
  560. return r;
  561. }
  562. void ath9k_queue_reset(struct ath_softc *sc, enum ath_reset_type type)
  563. {
  564. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  565. #ifdef CONFIG_ATH9K_DEBUGFS
  566. RESET_STAT_INC(sc, type);
  567. #endif
  568. set_bit(ATH_OP_HW_RESET, &common->op_flags);
  569. ieee80211_queue_work(sc->hw, &sc->hw_reset_work);
  570. }
  571. void ath_reset_work(struct work_struct *work)
  572. {
  573. struct ath_softc *sc = container_of(work, struct ath_softc, hw_reset_work);
  574. ath_reset(sc);
  575. }
  576. /**********************/
  577. /* mac80211 callbacks */
  578. /**********************/
  579. static int ath9k_start(struct ieee80211_hw *hw)
  580. {
  581. struct ath_softc *sc = hw->priv;
  582. struct ath_hw *ah = sc->sc_ah;
  583. struct ath_common *common = ath9k_hw_common(ah);
  584. struct ieee80211_channel *curchan = hw->conf.chandef.chan;
  585. struct ath9k_channel *init_channel;
  586. int r;
  587. ath_dbg(common, CONFIG,
  588. "Starting driver with initial channel: %d MHz\n",
  589. curchan->center_freq);
  590. ath9k_ps_wakeup(sc);
  591. mutex_lock(&sc->mutex);
  592. init_channel = ath9k_cmn_get_channel(hw, ah, &hw->conf.chandef);
  593. /* Reset SERDES registers */
  594. ath9k_hw_configpcipowersave(ah, false);
  595. /*
  596. * The basic interface to setting the hardware in a good
  597. * state is ``reset''. On return the hardware is known to
  598. * be powered up and with interrupts disabled. This must
  599. * be followed by initialization of the appropriate bits
  600. * and then setup of the interrupt mask.
  601. */
  602. spin_lock_bh(&sc->sc_pcu_lock);
  603. atomic_set(&ah->intr_ref_cnt, -1);
  604. r = ath9k_hw_reset(ah, init_channel, ah->caldata, false);
  605. if (r) {
  606. ath_err(common,
  607. "Unable to reset hardware; reset status %d (freq %u MHz)\n",
  608. r, curchan->center_freq);
  609. ah->reset_power_on = false;
  610. }
  611. /* Setup our intr mask. */
  612. ah->imask = ATH9K_INT_TX | ATH9K_INT_RXEOL |
  613. ATH9K_INT_RXORN | ATH9K_INT_FATAL |
  614. ATH9K_INT_GLOBAL;
  615. if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
  616. ah->imask |= ATH9K_INT_RXHP |
  617. ATH9K_INT_RXLP;
  618. else
  619. ah->imask |= ATH9K_INT_RX;
  620. if (ah->config.hw_hang_checks & HW_BB_WATCHDOG)
  621. ah->imask |= ATH9K_INT_BB_WATCHDOG;
  622. /*
  623. * Enable GTT interrupts only for AR9003/AR9004 chips
  624. * for now.
  625. */
  626. if (AR_SREV_9300_20_OR_LATER(ah))
  627. ah->imask |= ATH9K_INT_GTT;
  628. if (ah->caps.hw_caps & ATH9K_HW_CAP_HT)
  629. ah->imask |= ATH9K_INT_CST;
  630. ath_mci_enable(sc);
  631. clear_bit(ATH_OP_INVALID, &common->op_flags);
  632. sc->sc_ah->is_monitoring = false;
  633. if (!ath_complete_reset(sc, false))
  634. ah->reset_power_on = false;
  635. if (ah->led_pin >= 0) {
  636. ath9k_hw_cfg_output(ah, ah->led_pin,
  637. AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
  638. ath9k_hw_set_gpio(ah, ah->led_pin, 0);
  639. }
  640. /*
  641. * Reset key cache to sane defaults (all entries cleared) instead of
  642. * semi-random values after suspend/resume.
  643. */
  644. ath9k_cmn_init_crypto(sc->sc_ah);
  645. ath9k_hw_reset_tsf(ah);
  646. spin_unlock_bh(&sc->sc_pcu_lock);
  647. mutex_unlock(&sc->mutex);
  648. ath9k_ps_restore(sc);
  649. return 0;
  650. }
  651. static void ath9k_tx(struct ieee80211_hw *hw,
  652. struct ieee80211_tx_control *control,
  653. struct sk_buff *skb)
  654. {
  655. struct ath_softc *sc = hw->priv;
  656. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  657. struct ath_tx_control txctl;
  658. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  659. unsigned long flags;
  660. if (sc->ps_enabled) {
  661. /*
  662. * mac80211 does not set PM field for normal data frames, so we
  663. * need to update that based on the current PS mode.
  664. */
  665. if (ieee80211_is_data(hdr->frame_control) &&
  666. !ieee80211_is_nullfunc(hdr->frame_control) &&
  667. !ieee80211_has_pm(hdr->frame_control)) {
  668. ath_dbg(common, PS,
  669. "Add PM=1 for a TX frame while in PS mode\n");
  670. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
  671. }
  672. }
  673. if (unlikely(sc->sc_ah->power_mode == ATH9K_PM_NETWORK_SLEEP)) {
  674. /*
  675. * We are using PS-Poll and mac80211 can request TX while in
  676. * power save mode. Need to wake up hardware for the TX to be
  677. * completed and if needed, also for RX of buffered frames.
  678. */
  679. ath9k_ps_wakeup(sc);
  680. spin_lock_irqsave(&sc->sc_pm_lock, flags);
  681. if (!(sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP))
  682. ath9k_hw_setrxabort(sc->sc_ah, 0);
  683. if (ieee80211_is_pspoll(hdr->frame_control)) {
  684. ath_dbg(common, PS,
  685. "Sending PS-Poll to pick a buffered frame\n");
  686. sc->ps_flags |= PS_WAIT_FOR_PSPOLL_DATA;
  687. } else {
  688. ath_dbg(common, PS, "Wake up to complete TX\n");
  689. sc->ps_flags |= PS_WAIT_FOR_TX_ACK;
  690. }
  691. /*
  692. * The actual restore operation will happen only after
  693. * the ps_flags bit is cleared. We are just dropping
  694. * the ps_usecount here.
  695. */
  696. spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
  697. ath9k_ps_restore(sc);
  698. }
  699. /*
  700. * Cannot tx while the hardware is in full sleep, it first needs a full
  701. * chip reset to recover from that
  702. */
  703. if (unlikely(sc->sc_ah->power_mode == ATH9K_PM_FULL_SLEEP)) {
  704. ath_err(common, "TX while HW is in FULL_SLEEP mode\n");
  705. goto exit;
  706. }
  707. memset(&txctl, 0, sizeof(struct ath_tx_control));
  708. txctl.txq = sc->tx.txq_map[skb_get_queue_mapping(skb)];
  709. txctl.sta = control->sta;
  710. ath_dbg(common, XMIT, "transmitting packet, skb: %p\n", skb);
  711. if (ath_tx_start(hw, skb, &txctl) != 0) {
  712. ath_dbg(common, XMIT, "TX failed\n");
  713. TX_STAT_INC(txctl.txq->axq_qnum, txfailed);
  714. goto exit;
  715. }
  716. return;
  717. exit:
  718. ieee80211_free_txskb(hw, skb);
  719. }
  720. static void ath9k_stop(struct ieee80211_hw *hw)
  721. {
  722. struct ath_softc *sc = hw->priv;
  723. struct ath_hw *ah = sc->sc_ah;
  724. struct ath_common *common = ath9k_hw_common(ah);
  725. bool prev_idle;
  726. mutex_lock(&sc->mutex);
  727. ath_cancel_work(sc);
  728. if (test_bit(ATH_OP_INVALID, &common->op_flags)) {
  729. ath_dbg(common, ANY, "Device not present\n");
  730. mutex_unlock(&sc->mutex);
  731. return;
  732. }
  733. /* Ensure HW is awake when we try to shut it down. */
  734. ath9k_ps_wakeup(sc);
  735. spin_lock_bh(&sc->sc_pcu_lock);
  736. /* prevent tasklets to enable interrupts once we disable them */
  737. ah->imask &= ~ATH9K_INT_GLOBAL;
  738. /* make sure h/w will not generate any interrupt
  739. * before setting the invalid flag. */
  740. ath9k_hw_disable_interrupts(ah);
  741. spin_unlock_bh(&sc->sc_pcu_lock);
  742. /* we can now sync irq and kill any running tasklets, since we already
  743. * disabled interrupts and not holding a spin lock */
  744. synchronize_irq(sc->irq);
  745. tasklet_kill(&sc->intr_tq);
  746. tasklet_kill(&sc->bcon_tasklet);
  747. prev_idle = sc->ps_idle;
  748. sc->ps_idle = true;
  749. spin_lock_bh(&sc->sc_pcu_lock);
  750. if (ah->led_pin >= 0) {
  751. ath9k_hw_set_gpio(ah, ah->led_pin, 1);
  752. ath9k_hw_cfg_gpio_input(ah, ah->led_pin);
  753. }
  754. ath_prepare_reset(sc);
  755. if (sc->rx.frag) {
  756. dev_kfree_skb_any(sc->rx.frag);
  757. sc->rx.frag = NULL;
  758. }
  759. if (!ah->curchan)
  760. ah->curchan = ath9k_cmn_get_channel(hw, ah, &hw->conf.chandef);
  761. ath9k_hw_reset(ah, ah->curchan, ah->caldata, false);
  762. ath9k_hw_phy_disable(ah);
  763. ath9k_hw_configpcipowersave(ah, true);
  764. spin_unlock_bh(&sc->sc_pcu_lock);
  765. ath9k_ps_restore(sc);
  766. set_bit(ATH_OP_INVALID, &common->op_flags);
  767. sc->ps_idle = prev_idle;
  768. mutex_unlock(&sc->mutex);
  769. ath_dbg(common, CONFIG, "Driver halt\n");
  770. }
  771. static bool ath9k_uses_beacons(int type)
  772. {
  773. switch (type) {
  774. case NL80211_IFTYPE_AP:
  775. case NL80211_IFTYPE_ADHOC:
  776. case NL80211_IFTYPE_MESH_POINT:
  777. return true;
  778. default:
  779. return false;
  780. }
  781. }
  782. static void ath9k_vif_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
  783. {
  784. struct ath9k_vif_iter_data *iter_data = data;
  785. int i;
  786. if (iter_data->has_hw_macaddr) {
  787. for (i = 0; i < ETH_ALEN; i++)
  788. iter_data->mask[i] &=
  789. ~(iter_data->hw_macaddr[i] ^ mac[i]);
  790. } else {
  791. memcpy(iter_data->hw_macaddr, mac, ETH_ALEN);
  792. iter_data->has_hw_macaddr = true;
  793. }
  794. switch (vif->type) {
  795. case NL80211_IFTYPE_AP:
  796. iter_data->naps++;
  797. break;
  798. case NL80211_IFTYPE_STATION:
  799. iter_data->nstations++;
  800. break;
  801. case NL80211_IFTYPE_ADHOC:
  802. iter_data->nadhocs++;
  803. break;
  804. case NL80211_IFTYPE_MESH_POINT:
  805. iter_data->nmeshes++;
  806. break;
  807. case NL80211_IFTYPE_WDS:
  808. iter_data->nwds++;
  809. break;
  810. default:
  811. break;
  812. }
  813. }
  814. static void ath9k_sta_vif_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
  815. {
  816. struct ath_softc *sc = data;
  817. struct ath_vif *avp = (void *)vif->drv_priv;
  818. if (vif->type != NL80211_IFTYPE_STATION)
  819. return;
  820. if (avp->primary_sta_vif)
  821. ath9k_set_assoc_state(sc, vif);
  822. }
  823. /* Called with sc->mutex held. */
  824. void ath9k_calculate_iter_data(struct ieee80211_hw *hw,
  825. struct ieee80211_vif *vif,
  826. struct ath9k_vif_iter_data *iter_data)
  827. {
  828. struct ath_softc *sc = hw->priv;
  829. struct ath_hw *ah = sc->sc_ah;
  830. struct ath_common *common = ath9k_hw_common(ah);
  831. /*
  832. * Pick the MAC address of the first interface as the new hardware
  833. * MAC address. The hardware will use it together with the BSSID mask
  834. * when matching addresses.
  835. */
  836. memset(iter_data, 0, sizeof(*iter_data));
  837. memset(&iter_data->mask, 0xff, ETH_ALEN);
  838. if (vif)
  839. ath9k_vif_iter(iter_data, vif->addr, vif);
  840. /* Get list of all active MAC addresses */
  841. ieee80211_iterate_active_interfaces_atomic(
  842. sc->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
  843. ath9k_vif_iter, iter_data);
  844. memcpy(common->macaddr, iter_data->hw_macaddr, ETH_ALEN);
  845. }
  846. /* Called with sc->mutex held. */
  847. static void ath9k_calculate_summary_state(struct ieee80211_hw *hw,
  848. struct ieee80211_vif *vif)
  849. {
  850. struct ath_softc *sc = hw->priv;
  851. struct ath_hw *ah = sc->sc_ah;
  852. struct ath_common *common = ath9k_hw_common(ah);
  853. struct ath9k_vif_iter_data iter_data;
  854. enum nl80211_iftype old_opmode = ah->opmode;
  855. ath9k_calculate_iter_data(hw, vif, &iter_data);
  856. memcpy(common->bssidmask, iter_data.mask, ETH_ALEN);
  857. ath_hw_setbssidmask(common);
  858. if (iter_data.naps > 0) {
  859. ath9k_hw_set_tsfadjust(ah, true);
  860. ah->opmode = NL80211_IFTYPE_AP;
  861. } else {
  862. ath9k_hw_set_tsfadjust(ah, false);
  863. if (iter_data.nmeshes)
  864. ah->opmode = NL80211_IFTYPE_MESH_POINT;
  865. else if (iter_data.nwds)
  866. ah->opmode = NL80211_IFTYPE_AP;
  867. else if (iter_data.nadhocs)
  868. ah->opmode = NL80211_IFTYPE_ADHOC;
  869. else
  870. ah->opmode = NL80211_IFTYPE_STATION;
  871. }
  872. ath9k_hw_setopmode(ah);
  873. if ((iter_data.nstations + iter_data.nadhocs + iter_data.nmeshes) > 0)
  874. ah->imask |= ATH9K_INT_TSFOOR;
  875. else
  876. ah->imask &= ~ATH9K_INT_TSFOOR;
  877. ath9k_hw_set_interrupts(ah);
  878. /*
  879. * If we are changing the opmode to STATION,
  880. * a beacon sync needs to be done.
  881. */
  882. if (ah->opmode == NL80211_IFTYPE_STATION &&
  883. old_opmode == NL80211_IFTYPE_AP &&
  884. test_bit(ATH_OP_PRIM_STA_VIF, &common->op_flags)) {
  885. ieee80211_iterate_active_interfaces_atomic(
  886. sc->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
  887. ath9k_sta_vif_iter, sc);
  888. }
  889. }
  890. static int ath9k_add_interface(struct ieee80211_hw *hw,
  891. struct ieee80211_vif *vif)
  892. {
  893. struct ath_softc *sc = hw->priv;
  894. struct ath_hw *ah = sc->sc_ah;
  895. struct ath_common *common = ath9k_hw_common(ah);
  896. struct ath_vif *avp = (void *)vif->drv_priv;
  897. struct ath_node *an = &avp->mcast_node;
  898. mutex_lock(&sc->mutex);
  899. if (config_enabled(CONFIG_ATH9K_TX99)) {
  900. if (sc->nvifs >= 1) {
  901. mutex_unlock(&sc->mutex);
  902. return -EOPNOTSUPP;
  903. }
  904. sc->tx99_vif = vif;
  905. }
  906. ath_dbg(common, CONFIG, "Attach a VIF of type: %d\n", vif->type);
  907. sc->nvifs++;
  908. ath9k_ps_wakeup(sc);
  909. ath9k_calculate_summary_state(hw, vif);
  910. ath9k_ps_restore(sc);
  911. if (ath9k_uses_beacons(vif->type))
  912. ath9k_beacon_assign_slot(sc, vif);
  913. avp->vif = vif;
  914. an->sc = sc;
  915. an->sta = NULL;
  916. an->vif = vif;
  917. an->no_ps_filter = true;
  918. ath_tx_node_init(sc, an);
  919. mutex_unlock(&sc->mutex);
  920. return 0;
  921. }
  922. static int ath9k_change_interface(struct ieee80211_hw *hw,
  923. struct ieee80211_vif *vif,
  924. enum nl80211_iftype new_type,
  925. bool p2p)
  926. {
  927. struct ath_softc *sc = hw->priv;
  928. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  929. mutex_lock(&sc->mutex);
  930. if (config_enabled(CONFIG_ATH9K_TX99)) {
  931. mutex_unlock(&sc->mutex);
  932. return -EOPNOTSUPP;
  933. }
  934. ath_dbg(common, CONFIG, "Change Interface\n");
  935. if (ath9k_uses_beacons(vif->type))
  936. ath9k_beacon_remove_slot(sc, vif);
  937. vif->type = new_type;
  938. vif->p2p = p2p;
  939. ath9k_ps_wakeup(sc);
  940. ath9k_calculate_summary_state(hw, vif);
  941. ath9k_ps_restore(sc);
  942. if (ath9k_uses_beacons(vif->type))
  943. ath9k_beacon_assign_slot(sc, vif);
  944. mutex_unlock(&sc->mutex);
  945. return 0;
  946. }
  947. static void
  948. ath9k_update_p2p_ps_timer(struct ath_softc *sc, struct ath_vif *avp)
  949. {
  950. struct ath_hw *ah = sc->sc_ah;
  951. s32 tsf, target_tsf;
  952. if (!avp || !avp->noa.has_next_tsf)
  953. return;
  954. ath9k_hw_gen_timer_stop(ah, sc->p2p_ps_timer);
  955. tsf = ath9k_hw_gettsf32(sc->sc_ah);
  956. target_tsf = avp->noa.next_tsf;
  957. if (!avp->noa.absent)
  958. target_tsf -= ATH_P2P_PS_STOP_TIME;
  959. if (target_tsf - tsf < ATH_P2P_PS_STOP_TIME)
  960. target_tsf = tsf + ATH_P2P_PS_STOP_TIME;
  961. ath9k_hw_gen_timer_start(ah, sc->p2p_ps_timer, (u32) target_tsf, 1000000);
  962. }
  963. static void ath9k_remove_interface(struct ieee80211_hw *hw,
  964. struct ieee80211_vif *vif)
  965. {
  966. struct ath_softc *sc = hw->priv;
  967. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  968. struct ath_vif *avp = (void *)vif->drv_priv;
  969. ath_dbg(common, CONFIG, "Detach Interface\n");
  970. mutex_lock(&sc->mutex);
  971. spin_lock_bh(&sc->sc_pcu_lock);
  972. if (avp == sc->p2p_ps_vif) {
  973. sc->p2p_ps_vif = NULL;
  974. ath9k_update_p2p_ps_timer(sc, NULL);
  975. }
  976. spin_unlock_bh(&sc->sc_pcu_lock);
  977. sc->nvifs--;
  978. sc->tx99_vif = NULL;
  979. if (ath9k_uses_beacons(vif->type))
  980. ath9k_beacon_remove_slot(sc, vif);
  981. ath9k_ps_wakeup(sc);
  982. ath9k_calculate_summary_state(hw, NULL);
  983. ath9k_ps_restore(sc);
  984. ath_tx_node_cleanup(sc, &avp->mcast_node);
  985. mutex_unlock(&sc->mutex);
  986. }
  987. static void ath9k_enable_ps(struct ath_softc *sc)
  988. {
  989. struct ath_hw *ah = sc->sc_ah;
  990. struct ath_common *common = ath9k_hw_common(ah);
  991. if (config_enabled(CONFIG_ATH9K_TX99))
  992. return;
  993. sc->ps_enabled = true;
  994. if (!(ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP)) {
  995. if ((ah->imask & ATH9K_INT_TIM_TIMER) == 0) {
  996. ah->imask |= ATH9K_INT_TIM_TIMER;
  997. ath9k_hw_set_interrupts(ah);
  998. }
  999. ath9k_hw_setrxabort(ah, 1);
  1000. }
  1001. ath_dbg(common, PS, "PowerSave enabled\n");
  1002. }
  1003. static void ath9k_disable_ps(struct ath_softc *sc)
  1004. {
  1005. struct ath_hw *ah = sc->sc_ah;
  1006. struct ath_common *common = ath9k_hw_common(ah);
  1007. if (config_enabled(CONFIG_ATH9K_TX99))
  1008. return;
  1009. sc->ps_enabled = false;
  1010. ath9k_hw_setpower(ah, ATH9K_PM_AWAKE);
  1011. if (!(ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP)) {
  1012. ath9k_hw_setrxabort(ah, 0);
  1013. sc->ps_flags &= ~(PS_WAIT_FOR_BEACON |
  1014. PS_WAIT_FOR_CAB |
  1015. PS_WAIT_FOR_PSPOLL_DATA |
  1016. PS_WAIT_FOR_TX_ACK);
  1017. if (ah->imask & ATH9K_INT_TIM_TIMER) {
  1018. ah->imask &= ~ATH9K_INT_TIM_TIMER;
  1019. ath9k_hw_set_interrupts(ah);
  1020. }
  1021. }
  1022. ath_dbg(common, PS, "PowerSave disabled\n");
  1023. }
  1024. void ath9k_spectral_scan_trigger(struct ieee80211_hw *hw)
  1025. {
  1026. struct ath_softc *sc = hw->priv;
  1027. struct ath_hw *ah = sc->sc_ah;
  1028. struct ath_common *common = ath9k_hw_common(ah);
  1029. u32 rxfilter;
  1030. if (config_enabled(CONFIG_ATH9K_TX99))
  1031. return;
  1032. if (!ath9k_hw_ops(ah)->spectral_scan_trigger) {
  1033. ath_err(common, "spectrum analyzer not implemented on this hardware\n");
  1034. return;
  1035. }
  1036. ath9k_ps_wakeup(sc);
  1037. rxfilter = ath9k_hw_getrxfilter(ah);
  1038. ath9k_hw_setrxfilter(ah, rxfilter |
  1039. ATH9K_RX_FILTER_PHYRADAR |
  1040. ATH9K_RX_FILTER_PHYERR);
  1041. /* TODO: usually this should not be neccesary, but for some reason
  1042. * (or in some mode?) the trigger must be called after the
  1043. * configuration, otherwise the register will have its values reset
  1044. * (on my ar9220 to value 0x01002310)
  1045. */
  1046. ath9k_spectral_scan_config(hw, sc->spectral_mode);
  1047. ath9k_hw_ops(ah)->spectral_scan_trigger(ah);
  1048. ath9k_ps_restore(sc);
  1049. }
  1050. int ath9k_spectral_scan_config(struct ieee80211_hw *hw,
  1051. enum spectral_mode spectral_mode)
  1052. {
  1053. struct ath_softc *sc = hw->priv;
  1054. struct ath_hw *ah = sc->sc_ah;
  1055. struct ath_common *common = ath9k_hw_common(ah);
  1056. if (!ath9k_hw_ops(ah)->spectral_scan_trigger) {
  1057. ath_err(common, "spectrum analyzer not implemented on this hardware\n");
  1058. return -1;
  1059. }
  1060. switch (spectral_mode) {
  1061. case SPECTRAL_DISABLED:
  1062. sc->spec_config.enabled = 0;
  1063. break;
  1064. case SPECTRAL_BACKGROUND:
  1065. /* send endless samples.
  1066. * TODO: is this really useful for "background"?
  1067. */
  1068. sc->spec_config.endless = 1;
  1069. sc->spec_config.enabled = 1;
  1070. break;
  1071. case SPECTRAL_CHANSCAN:
  1072. case SPECTRAL_MANUAL:
  1073. sc->spec_config.endless = 0;
  1074. sc->spec_config.enabled = 1;
  1075. break;
  1076. default:
  1077. return -1;
  1078. }
  1079. ath9k_ps_wakeup(sc);
  1080. ath9k_hw_ops(ah)->spectral_scan_config(ah, &sc->spec_config);
  1081. ath9k_ps_restore(sc);
  1082. sc->spectral_mode = spectral_mode;
  1083. return 0;
  1084. }
  1085. static int ath9k_config(struct ieee80211_hw *hw, u32 changed)
  1086. {
  1087. struct ath_softc *sc = hw->priv;
  1088. struct ath_hw *ah = sc->sc_ah;
  1089. struct ath_common *common = ath9k_hw_common(ah);
  1090. struct ieee80211_conf *conf = &hw->conf;
  1091. bool reset_channel = false;
  1092. ath9k_ps_wakeup(sc);
  1093. mutex_lock(&sc->mutex);
  1094. if (changed & IEEE80211_CONF_CHANGE_IDLE) {
  1095. sc->ps_idle = !!(conf->flags & IEEE80211_CONF_IDLE);
  1096. if (sc->ps_idle) {
  1097. ath_cancel_work(sc);
  1098. ath9k_stop_btcoex(sc);
  1099. } else {
  1100. ath9k_start_btcoex(sc);
  1101. /*
  1102. * The chip needs a reset to properly wake up from
  1103. * full sleep
  1104. */
  1105. reset_channel = ah->chip_fullsleep;
  1106. }
  1107. }
  1108. /*
  1109. * We just prepare to enable PS. We have to wait until our AP has
  1110. * ACK'd our null data frame to disable RX otherwise we'll ignore
  1111. * those ACKs and end up retransmitting the same null data frames.
  1112. * IEEE80211_CONF_CHANGE_PS is only passed by mac80211 for STA mode.
  1113. */
  1114. if (changed & IEEE80211_CONF_CHANGE_PS) {
  1115. unsigned long flags;
  1116. spin_lock_irqsave(&sc->sc_pm_lock, flags);
  1117. if (conf->flags & IEEE80211_CONF_PS)
  1118. ath9k_enable_ps(sc);
  1119. else
  1120. ath9k_disable_ps(sc);
  1121. spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
  1122. }
  1123. if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
  1124. if (conf->flags & IEEE80211_CONF_MONITOR) {
  1125. ath_dbg(common, CONFIG, "Monitor mode is enabled\n");
  1126. sc->sc_ah->is_monitoring = true;
  1127. } else {
  1128. ath_dbg(common, CONFIG, "Monitor mode is disabled\n");
  1129. sc->sc_ah->is_monitoring = false;
  1130. }
  1131. }
  1132. if ((changed & IEEE80211_CONF_CHANGE_CHANNEL) || reset_channel) {
  1133. if (ath_set_channel(sc, &hw->conf.chandef) < 0) {
  1134. ath_err(common, "Unable to set channel\n");
  1135. mutex_unlock(&sc->mutex);
  1136. ath9k_ps_restore(sc);
  1137. return -EINVAL;
  1138. }
  1139. }
  1140. if (changed & IEEE80211_CONF_CHANGE_POWER) {
  1141. ath_dbg(common, CONFIG, "Set power: %d\n", conf->power_level);
  1142. sc->config.txpowlimit = 2 * conf->power_level;
  1143. ath9k_cmn_update_txpow(ah, sc->curtxpow,
  1144. sc->config.txpowlimit, &sc->curtxpow);
  1145. }
  1146. mutex_unlock(&sc->mutex);
  1147. ath9k_ps_restore(sc);
  1148. return 0;
  1149. }
  1150. #define SUPPORTED_FILTERS \
  1151. (FIF_PROMISC_IN_BSS | \
  1152. FIF_ALLMULTI | \
  1153. FIF_CONTROL | \
  1154. FIF_PSPOLL | \
  1155. FIF_OTHER_BSS | \
  1156. FIF_BCN_PRBRESP_PROMISC | \
  1157. FIF_PROBE_REQ | \
  1158. FIF_FCSFAIL)
  1159. /* FIXME: sc->sc_full_reset ? */
  1160. static void ath9k_configure_filter(struct ieee80211_hw *hw,
  1161. unsigned int changed_flags,
  1162. unsigned int *total_flags,
  1163. u64 multicast)
  1164. {
  1165. struct ath_softc *sc = hw->priv;
  1166. u32 rfilt;
  1167. changed_flags &= SUPPORTED_FILTERS;
  1168. *total_flags &= SUPPORTED_FILTERS;
  1169. sc->rx.rxfilter = *total_flags;
  1170. ath9k_ps_wakeup(sc);
  1171. rfilt = ath_calcrxfilter(sc);
  1172. ath9k_hw_setrxfilter(sc->sc_ah, rfilt);
  1173. ath9k_ps_restore(sc);
  1174. ath_dbg(ath9k_hw_common(sc->sc_ah), CONFIG, "Set HW RX filter: 0x%x\n",
  1175. rfilt);
  1176. }
  1177. static int ath9k_sta_add(struct ieee80211_hw *hw,
  1178. struct ieee80211_vif *vif,
  1179. struct ieee80211_sta *sta)
  1180. {
  1181. struct ath_softc *sc = hw->priv;
  1182. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  1183. struct ath_node *an = (struct ath_node *) sta->drv_priv;
  1184. struct ieee80211_key_conf ps_key = { };
  1185. int key;
  1186. ath_node_attach(sc, sta, vif);
  1187. if (vif->type != NL80211_IFTYPE_AP &&
  1188. vif->type != NL80211_IFTYPE_AP_VLAN)
  1189. return 0;
  1190. key = ath_key_config(common, vif, sta, &ps_key);
  1191. if (key > 0) {
  1192. an->ps_key = key;
  1193. an->key_idx[0] = key;
  1194. }
  1195. return 0;
  1196. }
  1197. static void ath9k_del_ps_key(struct ath_softc *sc,
  1198. struct ieee80211_vif *vif,
  1199. struct ieee80211_sta *sta)
  1200. {
  1201. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  1202. struct ath_node *an = (struct ath_node *) sta->drv_priv;
  1203. struct ieee80211_key_conf ps_key = { .hw_key_idx = an->ps_key };
  1204. if (!an->ps_key)
  1205. return;
  1206. ath_key_delete(common, &ps_key);
  1207. an->ps_key = 0;
  1208. an->key_idx[0] = 0;
  1209. }
  1210. static int ath9k_sta_remove(struct ieee80211_hw *hw,
  1211. struct ieee80211_vif *vif,
  1212. struct ieee80211_sta *sta)
  1213. {
  1214. struct ath_softc *sc = hw->priv;
  1215. ath9k_del_ps_key(sc, vif, sta);
  1216. ath_node_detach(sc, sta);
  1217. return 0;
  1218. }
  1219. static void ath9k_sta_set_tx_filter(struct ath_hw *ah,
  1220. struct ath_node *an,
  1221. bool set)
  1222. {
  1223. int i;
  1224. for (i = 0; i < ARRAY_SIZE(an->key_idx); i++) {
  1225. if (!an->key_idx[i])
  1226. continue;
  1227. ath9k_hw_set_tx_filter(ah, an->key_idx[i], set);
  1228. }
  1229. }
  1230. static void ath9k_sta_notify(struct ieee80211_hw *hw,
  1231. struct ieee80211_vif *vif,
  1232. enum sta_notify_cmd cmd,
  1233. struct ieee80211_sta *sta)
  1234. {
  1235. struct ath_softc *sc = hw->priv;
  1236. struct ath_node *an = (struct ath_node *) sta->drv_priv;
  1237. switch (cmd) {
  1238. case STA_NOTIFY_SLEEP:
  1239. an->sleeping = true;
  1240. ath_tx_aggr_sleep(sta, sc, an);
  1241. ath9k_sta_set_tx_filter(sc->sc_ah, an, true);
  1242. break;
  1243. case STA_NOTIFY_AWAKE:
  1244. ath9k_sta_set_tx_filter(sc->sc_ah, an, false);
  1245. an->sleeping = false;
  1246. ath_tx_aggr_wakeup(sc, an);
  1247. break;
  1248. }
  1249. }
  1250. static int ath9k_conf_tx(struct ieee80211_hw *hw,
  1251. struct ieee80211_vif *vif, u16 queue,
  1252. const struct ieee80211_tx_queue_params *params)
  1253. {
  1254. struct ath_softc *sc = hw->priv;
  1255. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  1256. struct ath_txq *txq;
  1257. struct ath9k_tx_queue_info qi;
  1258. int ret = 0;
  1259. if (queue >= IEEE80211_NUM_ACS)
  1260. return 0;
  1261. txq = sc->tx.txq_map[queue];
  1262. ath9k_ps_wakeup(sc);
  1263. mutex_lock(&sc->mutex);
  1264. memset(&qi, 0, sizeof(struct ath9k_tx_queue_info));
  1265. qi.tqi_aifs = params->aifs;
  1266. qi.tqi_cwmin = params->cw_min;
  1267. qi.tqi_cwmax = params->cw_max;
  1268. qi.tqi_burstTime = params->txop * 32;
  1269. ath_dbg(common, CONFIG,
  1270. "Configure tx [queue/halq] [%d/%d], aifs: %d, cw_min: %d, cw_max: %d, txop: %d\n",
  1271. queue, txq->axq_qnum, params->aifs, params->cw_min,
  1272. params->cw_max, params->txop);
  1273. ath_update_max_aggr_framelen(sc, queue, qi.tqi_burstTime);
  1274. ret = ath_txq_update(sc, txq->axq_qnum, &qi);
  1275. if (ret)
  1276. ath_err(common, "TXQ Update failed\n");
  1277. mutex_unlock(&sc->mutex);
  1278. ath9k_ps_restore(sc);
  1279. return ret;
  1280. }
  1281. static int ath9k_set_key(struct ieee80211_hw *hw,
  1282. enum set_key_cmd cmd,
  1283. struct ieee80211_vif *vif,
  1284. struct ieee80211_sta *sta,
  1285. struct ieee80211_key_conf *key)
  1286. {
  1287. struct ath_softc *sc = hw->priv;
  1288. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  1289. struct ath_node *an = NULL;
  1290. int ret = 0, i;
  1291. if (ath9k_modparam_nohwcrypt)
  1292. return -ENOSPC;
  1293. if ((vif->type == NL80211_IFTYPE_ADHOC ||
  1294. vif->type == NL80211_IFTYPE_MESH_POINT) &&
  1295. (key->cipher == WLAN_CIPHER_SUITE_TKIP ||
  1296. key->cipher == WLAN_CIPHER_SUITE_CCMP) &&
  1297. !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
  1298. /*
  1299. * For now, disable hw crypto for the RSN IBSS group keys. This
  1300. * could be optimized in the future to use a modified key cache
  1301. * design to support per-STA RX GTK, but until that gets
  1302. * implemented, use of software crypto for group addressed
  1303. * frames is a acceptable to allow RSN IBSS to be used.
  1304. */
  1305. return -EOPNOTSUPP;
  1306. }
  1307. mutex_lock(&sc->mutex);
  1308. ath9k_ps_wakeup(sc);
  1309. ath_dbg(common, CONFIG, "Set HW Key %d\n", cmd);
  1310. if (sta)
  1311. an = (struct ath_node *)sta->drv_priv;
  1312. switch (cmd) {
  1313. case SET_KEY:
  1314. if (sta)
  1315. ath9k_del_ps_key(sc, vif, sta);
  1316. key->hw_key_idx = 0;
  1317. ret = ath_key_config(common, vif, sta, key);
  1318. if (ret >= 0) {
  1319. key->hw_key_idx = ret;
  1320. /* push IV and Michael MIC generation to stack */
  1321. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  1322. if (key->cipher == WLAN_CIPHER_SUITE_TKIP)
  1323. key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
  1324. if (sc->sc_ah->sw_mgmt_crypto &&
  1325. key->cipher == WLAN_CIPHER_SUITE_CCMP)
  1326. key->flags |= IEEE80211_KEY_FLAG_SW_MGMT_TX;
  1327. ret = 0;
  1328. }
  1329. if (an && key->hw_key_idx) {
  1330. for (i = 0; i < ARRAY_SIZE(an->key_idx); i++) {
  1331. if (an->key_idx[i])
  1332. continue;
  1333. an->key_idx[i] = key->hw_key_idx;
  1334. break;
  1335. }
  1336. WARN_ON(i == ARRAY_SIZE(an->key_idx));
  1337. }
  1338. break;
  1339. case DISABLE_KEY:
  1340. ath_key_delete(common, key);
  1341. if (an) {
  1342. for (i = 0; i < ARRAY_SIZE(an->key_idx); i++) {
  1343. if (an->key_idx[i] != key->hw_key_idx)
  1344. continue;
  1345. an->key_idx[i] = 0;
  1346. break;
  1347. }
  1348. }
  1349. key->hw_key_idx = 0;
  1350. break;
  1351. default:
  1352. ret = -EINVAL;
  1353. }
  1354. ath9k_ps_restore(sc);
  1355. mutex_unlock(&sc->mutex);
  1356. return ret;
  1357. }
  1358. static void ath9k_set_assoc_state(struct ath_softc *sc,
  1359. struct ieee80211_vif *vif)
  1360. {
  1361. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  1362. struct ath_vif *avp = (void *)vif->drv_priv;
  1363. struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
  1364. unsigned long flags;
  1365. set_bit(ATH_OP_PRIM_STA_VIF, &common->op_flags);
  1366. avp->primary_sta_vif = true;
  1367. /*
  1368. * Set the AID, BSSID and do beacon-sync only when
  1369. * the HW opmode is STATION.
  1370. *
  1371. * But the primary bit is set above in any case.
  1372. */
  1373. if (sc->sc_ah->opmode != NL80211_IFTYPE_STATION)
  1374. return;
  1375. memcpy(common->curbssid, bss_conf->bssid, ETH_ALEN);
  1376. common->curaid = bss_conf->aid;
  1377. ath9k_hw_write_associd(sc->sc_ah);
  1378. common->last_rssi = ATH_RSSI_DUMMY_MARKER;
  1379. sc->sc_ah->stats.avgbrssi = ATH_RSSI_DUMMY_MARKER;
  1380. spin_lock_irqsave(&sc->sc_pm_lock, flags);
  1381. sc->ps_flags |= PS_BEACON_SYNC | PS_WAIT_FOR_BEACON;
  1382. spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
  1383. if (ath9k_hw_mci_is_enabled(sc->sc_ah))
  1384. ath9k_mci_update_wlan_channels(sc, false);
  1385. ath_dbg(common, CONFIG,
  1386. "Primary Station interface: %pM, BSSID: %pM\n",
  1387. vif->addr, common->curbssid);
  1388. }
  1389. static void ath9k_bss_assoc_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
  1390. {
  1391. struct ath_softc *sc = data;
  1392. struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
  1393. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  1394. if (test_bit(ATH_OP_PRIM_STA_VIF, &common->op_flags))
  1395. return;
  1396. if (bss_conf->assoc)
  1397. ath9k_set_assoc_state(sc, vif);
  1398. }
  1399. void ath9k_p2p_ps_timer(void *priv)
  1400. {
  1401. struct ath_softc *sc = priv;
  1402. struct ath_vif *avp = sc->p2p_ps_vif;
  1403. struct ieee80211_vif *vif;
  1404. struct ieee80211_sta *sta;
  1405. struct ath_node *an;
  1406. u32 tsf;
  1407. if (!avp)
  1408. return;
  1409. tsf = ath9k_hw_gettsf32(sc->sc_ah);
  1410. if (!avp->noa.absent)
  1411. tsf += ATH_P2P_PS_STOP_TIME;
  1412. if (!avp->noa.has_next_tsf ||
  1413. avp->noa.next_tsf - tsf > BIT(31))
  1414. ieee80211_update_p2p_noa(&avp->noa, tsf);
  1415. ath9k_update_p2p_ps_timer(sc, avp);
  1416. rcu_read_lock();
  1417. vif = avp->vif;
  1418. sta = ieee80211_find_sta(vif, vif->bss_conf.bssid);
  1419. if (!sta)
  1420. goto out;
  1421. an = (void *) sta->drv_priv;
  1422. if (an->sleeping == !!avp->noa.absent)
  1423. goto out;
  1424. an->sleeping = avp->noa.absent;
  1425. if (an->sleeping)
  1426. ath_tx_aggr_sleep(sta, sc, an);
  1427. else
  1428. ath_tx_aggr_wakeup(sc, an);
  1429. out:
  1430. rcu_read_unlock();
  1431. }
  1432. void ath9k_update_p2p_ps(struct ath_softc *sc, struct ieee80211_vif *vif)
  1433. {
  1434. struct ath_vif *avp = (void *)vif->drv_priv;
  1435. u32 tsf;
  1436. if (!sc->p2p_ps_timer)
  1437. return;
  1438. if (vif->type != NL80211_IFTYPE_STATION || !vif->p2p)
  1439. return;
  1440. sc->p2p_ps_vif = avp;
  1441. tsf = ath9k_hw_gettsf32(sc->sc_ah);
  1442. ieee80211_parse_p2p_noa(&vif->bss_conf.p2p_noa_attr, &avp->noa, tsf);
  1443. ath9k_update_p2p_ps_timer(sc, avp);
  1444. }
  1445. static void ath9k_bss_info_changed(struct ieee80211_hw *hw,
  1446. struct ieee80211_vif *vif,
  1447. struct ieee80211_bss_conf *bss_conf,
  1448. u32 changed)
  1449. {
  1450. #define CHECK_ANI \
  1451. (BSS_CHANGED_ASSOC | \
  1452. BSS_CHANGED_IBSS | \
  1453. BSS_CHANGED_BEACON_ENABLED)
  1454. struct ath_softc *sc = hw->priv;
  1455. struct ath_hw *ah = sc->sc_ah;
  1456. struct ath_common *common = ath9k_hw_common(ah);
  1457. struct ath_vif *avp = (void *)vif->drv_priv;
  1458. unsigned long flags;
  1459. int slottime;
  1460. ath9k_ps_wakeup(sc);
  1461. mutex_lock(&sc->mutex);
  1462. if (changed & BSS_CHANGED_ASSOC) {
  1463. ath_dbg(common, CONFIG, "BSSID %pM Changed ASSOC %d\n",
  1464. bss_conf->bssid, bss_conf->assoc);
  1465. if (avp->primary_sta_vif && !bss_conf->assoc) {
  1466. clear_bit(ATH_OP_PRIM_STA_VIF, &common->op_flags);
  1467. avp->primary_sta_vif = false;
  1468. if (ah->opmode == NL80211_IFTYPE_STATION)
  1469. clear_bit(ATH_OP_BEACONS, &common->op_flags);
  1470. }
  1471. ieee80211_iterate_active_interfaces_atomic(
  1472. sc->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
  1473. ath9k_bss_assoc_iter, sc);
  1474. if (!test_bit(ATH_OP_PRIM_STA_VIF, &common->op_flags) &&
  1475. ah->opmode == NL80211_IFTYPE_STATION) {
  1476. memset(common->curbssid, 0, ETH_ALEN);
  1477. common->curaid = 0;
  1478. ath9k_hw_write_associd(sc->sc_ah);
  1479. if (ath9k_hw_mci_is_enabled(sc->sc_ah))
  1480. ath9k_mci_update_wlan_channels(sc, true);
  1481. }
  1482. }
  1483. if (changed & BSS_CHANGED_IBSS) {
  1484. memcpy(common->curbssid, bss_conf->bssid, ETH_ALEN);
  1485. common->curaid = bss_conf->aid;
  1486. ath9k_hw_write_associd(sc->sc_ah);
  1487. }
  1488. if ((changed & BSS_CHANGED_BEACON_ENABLED) ||
  1489. (changed & BSS_CHANGED_BEACON_INT))
  1490. ath9k_beacon_config(sc, vif, changed);
  1491. if (changed & BSS_CHANGED_ERP_SLOT) {
  1492. if (bss_conf->use_short_slot)
  1493. slottime = 9;
  1494. else
  1495. slottime = 20;
  1496. if (vif->type == NL80211_IFTYPE_AP) {
  1497. /*
  1498. * Defer update, so that connected stations can adjust
  1499. * their settings at the same time.
  1500. * See beacon.c for more details
  1501. */
  1502. sc->beacon.slottime = slottime;
  1503. sc->beacon.updateslot = UPDATE;
  1504. } else {
  1505. ah->slottime = slottime;
  1506. ath9k_hw_init_global_settings(ah);
  1507. }
  1508. }
  1509. if (changed & BSS_CHANGED_P2P_PS) {
  1510. spin_lock_bh(&sc->sc_pcu_lock);
  1511. spin_lock_irqsave(&sc->sc_pm_lock, flags);
  1512. if (!(sc->ps_flags & PS_BEACON_SYNC))
  1513. ath9k_update_p2p_ps(sc, vif);
  1514. spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
  1515. spin_unlock_bh(&sc->sc_pcu_lock);
  1516. }
  1517. if (changed & CHECK_ANI)
  1518. ath_check_ani(sc);
  1519. mutex_unlock(&sc->mutex);
  1520. ath9k_ps_restore(sc);
  1521. #undef CHECK_ANI
  1522. }
  1523. static u64 ath9k_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
  1524. {
  1525. struct ath_softc *sc = hw->priv;
  1526. u64 tsf;
  1527. mutex_lock(&sc->mutex);
  1528. ath9k_ps_wakeup(sc);
  1529. tsf = ath9k_hw_gettsf64(sc->sc_ah);
  1530. ath9k_ps_restore(sc);
  1531. mutex_unlock(&sc->mutex);
  1532. return tsf;
  1533. }
  1534. static void ath9k_set_tsf(struct ieee80211_hw *hw,
  1535. struct ieee80211_vif *vif,
  1536. u64 tsf)
  1537. {
  1538. struct ath_softc *sc = hw->priv;
  1539. mutex_lock(&sc->mutex);
  1540. ath9k_ps_wakeup(sc);
  1541. ath9k_hw_settsf64(sc->sc_ah, tsf);
  1542. ath9k_ps_restore(sc);
  1543. mutex_unlock(&sc->mutex);
  1544. }
  1545. static void ath9k_reset_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
  1546. {
  1547. struct ath_softc *sc = hw->priv;
  1548. mutex_lock(&sc->mutex);
  1549. ath9k_ps_wakeup(sc);
  1550. ath9k_hw_reset_tsf(sc->sc_ah);
  1551. ath9k_ps_restore(sc);
  1552. mutex_unlock(&sc->mutex);
  1553. }
  1554. static int ath9k_ampdu_action(struct ieee80211_hw *hw,
  1555. struct ieee80211_vif *vif,
  1556. enum ieee80211_ampdu_mlme_action action,
  1557. struct ieee80211_sta *sta,
  1558. u16 tid, u16 *ssn, u8 buf_size)
  1559. {
  1560. struct ath_softc *sc = hw->priv;
  1561. bool flush = false;
  1562. int ret = 0;
  1563. mutex_lock(&sc->mutex);
  1564. switch (action) {
  1565. case IEEE80211_AMPDU_RX_START:
  1566. break;
  1567. case IEEE80211_AMPDU_RX_STOP:
  1568. break;
  1569. case IEEE80211_AMPDU_TX_START:
  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 (config_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[IEEE80211_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[IEEE80211_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_set_coverage_class(struct ieee80211_hw *hw, u8 coverage_class)
  1629. {
  1630. struct ath_softc *sc = hw->priv;
  1631. struct ath_hw *ah = sc->sc_ah;
  1632. if (config_enabled(CONFIG_ATH9K_TX99))
  1633. return;
  1634. mutex_lock(&sc->mutex);
  1635. ah->coverage_class = coverage_class;
  1636. ath9k_ps_wakeup(sc);
  1637. ath9k_hw_init_global_settings(ah);
  1638. ath9k_ps_restore(sc);
  1639. mutex_unlock(&sc->mutex);
  1640. }
  1641. static bool ath9k_has_tx_pending(struct ath_softc *sc)
  1642. {
  1643. int i, npend = 0;
  1644. for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
  1645. if (!ATH_TXQ_SETUP(sc, i))
  1646. continue;
  1647. if (!sc->tx.txq[i].axq_depth)
  1648. continue;
  1649. npend = ath9k_has_pending_frames(sc, &sc->tx.txq[i]);
  1650. if (npend)
  1651. break;
  1652. }
  1653. return !!npend;
  1654. }
  1655. static void ath9k_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  1656. u32 queues, bool drop)
  1657. {
  1658. struct ath_softc *sc = hw->priv;
  1659. struct ath_hw *ah = sc->sc_ah;
  1660. struct ath_common *common = ath9k_hw_common(ah);
  1661. int timeout = HZ / 5; /* 200 ms */
  1662. bool drain_txq;
  1663. mutex_lock(&sc->mutex);
  1664. cancel_delayed_work_sync(&sc->tx_complete_work);
  1665. if (ah->ah_flags & AH_UNPLUGGED) {
  1666. ath_dbg(common, ANY, "Device has been unplugged!\n");
  1667. mutex_unlock(&sc->mutex);
  1668. return;
  1669. }
  1670. if (test_bit(ATH_OP_INVALID, &common->op_flags)) {
  1671. ath_dbg(common, ANY, "Device not present\n");
  1672. mutex_unlock(&sc->mutex);
  1673. return;
  1674. }
  1675. if (wait_event_timeout(sc->tx_wait, !ath9k_has_tx_pending(sc),
  1676. timeout) > 0)
  1677. drop = false;
  1678. if (drop) {
  1679. ath9k_ps_wakeup(sc);
  1680. spin_lock_bh(&sc->sc_pcu_lock);
  1681. drain_txq = ath_drain_all_txq(sc);
  1682. spin_unlock_bh(&sc->sc_pcu_lock);
  1683. if (!drain_txq)
  1684. ath_reset(sc);
  1685. ath9k_ps_restore(sc);
  1686. ieee80211_wake_queues(hw);
  1687. }
  1688. ieee80211_queue_delayed_work(hw, &sc->tx_complete_work, 0);
  1689. mutex_unlock(&sc->mutex);
  1690. }
  1691. static bool ath9k_tx_frames_pending(struct ieee80211_hw *hw)
  1692. {
  1693. struct ath_softc *sc = hw->priv;
  1694. int i;
  1695. for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
  1696. if (!ATH_TXQ_SETUP(sc, i))
  1697. continue;
  1698. if (ath9k_has_pending_frames(sc, &sc->tx.txq[i]))
  1699. return true;
  1700. }
  1701. return false;
  1702. }
  1703. static int ath9k_tx_last_beacon(struct ieee80211_hw *hw)
  1704. {
  1705. struct ath_softc *sc = hw->priv;
  1706. struct ath_hw *ah = sc->sc_ah;
  1707. struct ieee80211_vif *vif;
  1708. struct ath_vif *avp;
  1709. struct ath_buf *bf;
  1710. struct ath_tx_status ts;
  1711. bool edma = !!(ah->caps.hw_caps & ATH9K_HW_CAP_EDMA);
  1712. int status;
  1713. vif = sc->beacon.bslot[0];
  1714. if (!vif)
  1715. return 0;
  1716. if (!vif->bss_conf.enable_beacon)
  1717. return 0;
  1718. avp = (void *)vif->drv_priv;
  1719. if (!sc->beacon.tx_processed && !edma) {
  1720. tasklet_disable(&sc->bcon_tasklet);
  1721. bf = avp->av_bcbuf;
  1722. if (!bf || !bf->bf_mpdu)
  1723. goto skip;
  1724. status = ath9k_hw_txprocdesc(ah, bf->bf_desc, &ts);
  1725. if (status == -EINPROGRESS)
  1726. goto skip;
  1727. sc->beacon.tx_processed = true;
  1728. sc->beacon.tx_last = !(ts.ts_status & ATH9K_TXERR_MASK);
  1729. skip:
  1730. tasklet_enable(&sc->bcon_tasklet);
  1731. }
  1732. return sc->beacon.tx_last;
  1733. }
  1734. static int ath9k_get_stats(struct ieee80211_hw *hw,
  1735. struct ieee80211_low_level_stats *stats)
  1736. {
  1737. struct ath_softc *sc = hw->priv;
  1738. struct ath_hw *ah = sc->sc_ah;
  1739. struct ath9k_mib_stats *mib_stats = &ah->ah_mibStats;
  1740. stats->dot11ACKFailureCount = mib_stats->ackrcv_bad;
  1741. stats->dot11RTSFailureCount = mib_stats->rts_bad;
  1742. stats->dot11FCSErrorCount = mib_stats->fcs_bad;
  1743. stats->dot11RTSSuccessCount = mib_stats->rts_good;
  1744. return 0;
  1745. }
  1746. static u32 fill_chainmask(u32 cap, u32 new)
  1747. {
  1748. u32 filled = 0;
  1749. int i;
  1750. for (i = 0; cap && new; i++, cap >>= 1) {
  1751. if (!(cap & BIT(0)))
  1752. continue;
  1753. if (new & BIT(0))
  1754. filled |= BIT(i);
  1755. new >>= 1;
  1756. }
  1757. return filled;
  1758. }
  1759. static bool validate_antenna_mask(struct ath_hw *ah, u32 val)
  1760. {
  1761. if (AR_SREV_9300_20_OR_LATER(ah))
  1762. return true;
  1763. switch (val & 0x7) {
  1764. case 0x1:
  1765. case 0x3:
  1766. case 0x7:
  1767. return true;
  1768. case 0x2:
  1769. return (ah->caps.rx_chainmask == 1);
  1770. default:
  1771. return false;
  1772. }
  1773. }
  1774. static int ath9k_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
  1775. {
  1776. struct ath_softc *sc = hw->priv;
  1777. struct ath_hw *ah = sc->sc_ah;
  1778. if (ah->caps.rx_chainmask != 1)
  1779. rx_ant |= tx_ant;
  1780. if (!validate_antenna_mask(ah, rx_ant) || !tx_ant)
  1781. return -EINVAL;
  1782. sc->ant_rx = rx_ant;
  1783. sc->ant_tx = tx_ant;
  1784. if (ah->caps.rx_chainmask == 1)
  1785. return 0;
  1786. /* AR9100 runs into calibration issues if not all rx chains are enabled */
  1787. if (AR_SREV_9100(ah))
  1788. ah->rxchainmask = 0x7;
  1789. else
  1790. ah->rxchainmask = fill_chainmask(ah->caps.rx_chainmask, rx_ant);
  1791. ah->txchainmask = fill_chainmask(ah->caps.tx_chainmask, tx_ant);
  1792. ath9k_cmn_reload_chainmask(ah);
  1793. return 0;
  1794. }
  1795. static int ath9k_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
  1796. {
  1797. struct ath_softc *sc = hw->priv;
  1798. *tx_ant = sc->ant_tx;
  1799. *rx_ant = sc->ant_rx;
  1800. return 0;
  1801. }
  1802. static void ath9k_sw_scan_start(struct ieee80211_hw *hw)
  1803. {
  1804. struct ath_softc *sc = hw->priv;
  1805. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  1806. set_bit(ATH_OP_SCANNING, &common->op_flags);
  1807. }
  1808. static void ath9k_sw_scan_complete(struct ieee80211_hw *hw)
  1809. {
  1810. struct ath_softc *sc = hw->priv;
  1811. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  1812. clear_bit(ATH_OP_SCANNING, &common->op_flags);
  1813. }
  1814. struct ieee80211_ops ath9k_ops = {
  1815. .tx = ath9k_tx,
  1816. .start = ath9k_start,
  1817. .stop = ath9k_stop,
  1818. .add_interface = ath9k_add_interface,
  1819. .change_interface = ath9k_change_interface,
  1820. .remove_interface = ath9k_remove_interface,
  1821. .config = ath9k_config,
  1822. .configure_filter = ath9k_configure_filter,
  1823. .sta_add = ath9k_sta_add,
  1824. .sta_remove = ath9k_sta_remove,
  1825. .sta_notify = ath9k_sta_notify,
  1826. .conf_tx = ath9k_conf_tx,
  1827. .bss_info_changed = ath9k_bss_info_changed,
  1828. .set_key = ath9k_set_key,
  1829. .get_tsf = ath9k_get_tsf,
  1830. .set_tsf = ath9k_set_tsf,
  1831. .reset_tsf = ath9k_reset_tsf,
  1832. .ampdu_action = ath9k_ampdu_action,
  1833. .get_survey = ath9k_get_survey,
  1834. .rfkill_poll = ath9k_rfkill_poll_state,
  1835. .set_coverage_class = ath9k_set_coverage_class,
  1836. .flush = ath9k_flush,
  1837. .tx_frames_pending = ath9k_tx_frames_pending,
  1838. .tx_last_beacon = ath9k_tx_last_beacon,
  1839. .release_buffered_frames = ath9k_release_buffered_frames,
  1840. .get_stats = ath9k_get_stats,
  1841. .set_antenna = ath9k_set_antenna,
  1842. .get_antenna = ath9k_get_antenna,
  1843. #ifdef CONFIG_ATH9K_WOW
  1844. .suspend = ath9k_suspend,
  1845. .resume = ath9k_resume,
  1846. .set_wakeup = ath9k_set_wakeup,
  1847. #endif
  1848. #ifdef CONFIG_ATH9K_DEBUGFS
  1849. .get_et_sset_count = ath9k_get_et_sset_count,
  1850. .get_et_stats = ath9k_get_et_stats,
  1851. .get_et_strings = ath9k_get_et_strings,
  1852. #endif
  1853. #if defined(CONFIG_MAC80211_DEBUGFS) && defined(CONFIG_ATH9K_STATION_STATISTICS)
  1854. .sta_add_debugfs = ath9k_sta_add_debugfs,
  1855. #endif
  1856. .sw_scan_start = ath9k_sw_scan_start,
  1857. .sw_scan_complete = ath9k_sw_scan_complete,
  1858. };