main.c 51 KB

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