main.c 51 KB

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