main.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234
  1. /*
  2. * Copyright (c) 2008-2009 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. else if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
  61. pending = !list_empty(&txq->txq_fifo_pending);
  62. spin_unlock_bh(&txq->axq_lock);
  63. return pending;
  64. }
  65. 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 ath9k_ps_wakeup(struct ath_softc *sc)
  75. {
  76. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  77. unsigned long flags;
  78. enum ath9k_power_mode power_mode;
  79. spin_lock_irqsave(&sc->sc_pm_lock, flags);
  80. if (++sc->ps_usecount != 1)
  81. goto unlock;
  82. power_mode = sc->sc_ah->power_mode;
  83. ath9k_hw_setpower(sc->sc_ah, ATH9K_PM_AWAKE);
  84. /*
  85. * While the hardware is asleep, the cycle counters contain no
  86. * useful data. Better clear them now so that they don't mess up
  87. * survey data results.
  88. */
  89. if (power_mode != ATH9K_PM_AWAKE) {
  90. spin_lock(&common->cc_lock);
  91. ath_hw_cycle_counters_update(common);
  92. memset(&common->cc_survey, 0, sizeof(common->cc_survey));
  93. spin_unlock(&common->cc_lock);
  94. }
  95. unlock:
  96. spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
  97. }
  98. void ath9k_ps_restore(struct ath_softc *sc)
  99. {
  100. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  101. unsigned long flags;
  102. spin_lock_irqsave(&sc->sc_pm_lock, flags);
  103. if (--sc->ps_usecount != 0)
  104. goto unlock;
  105. spin_lock(&common->cc_lock);
  106. ath_hw_cycle_counters_update(common);
  107. spin_unlock(&common->cc_lock);
  108. if (sc->ps_idle)
  109. ath9k_hw_setpower(sc->sc_ah, 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. ath9k_hw_setpower(sc->sc_ah, ATH9K_PM_NETWORK_SLEEP);
  116. unlock:
  117. spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
  118. }
  119. static void ath_start_ani(struct ath_common *common)
  120. {
  121. struct ath_hw *ah = common->ah;
  122. unsigned long timestamp = jiffies_to_msecs(jiffies);
  123. struct ath_softc *sc = (struct ath_softc *) common->priv;
  124. if (!(sc->sc_flags & SC_OP_ANI_RUN))
  125. return;
  126. if (sc->sc_flags & SC_OP_OFFCHANNEL)
  127. return;
  128. common->ani.longcal_timer = timestamp;
  129. common->ani.shortcal_timer = timestamp;
  130. common->ani.checkani_timer = timestamp;
  131. mod_timer(&common->ani.timer,
  132. jiffies +
  133. msecs_to_jiffies((u32)ah->config.ani_poll_interval));
  134. }
  135. static void ath_update_survey_nf(struct ath_softc *sc, int channel)
  136. {
  137. struct ath_hw *ah = sc->sc_ah;
  138. struct ath9k_channel *chan = &ah->channels[channel];
  139. struct survey_info *survey = &sc->survey[channel];
  140. if (chan->noisefloor) {
  141. survey->filled |= SURVEY_INFO_NOISE_DBM;
  142. survey->noise = chan->noisefloor;
  143. }
  144. }
  145. /*
  146. * Updates the survey statistics and returns the busy time since last
  147. * update in %, if the measurement duration was long enough for the
  148. * result to be useful, -1 otherwise.
  149. */
  150. static int ath_update_survey_stats(struct ath_softc *sc)
  151. {
  152. struct ath_hw *ah = sc->sc_ah;
  153. struct ath_common *common = ath9k_hw_common(ah);
  154. int pos = ah->curchan - &ah->channels[0];
  155. struct survey_info *survey = &sc->survey[pos];
  156. struct ath_cycle_counters *cc = &common->cc_survey;
  157. unsigned int div = common->clockrate * 1000;
  158. int ret = 0;
  159. if (!ah->curchan)
  160. return -1;
  161. if (ah->power_mode == ATH9K_PM_AWAKE)
  162. ath_hw_cycle_counters_update(common);
  163. if (cc->cycles > 0) {
  164. survey->filled |= SURVEY_INFO_CHANNEL_TIME |
  165. SURVEY_INFO_CHANNEL_TIME_BUSY |
  166. SURVEY_INFO_CHANNEL_TIME_RX |
  167. SURVEY_INFO_CHANNEL_TIME_TX;
  168. survey->channel_time += cc->cycles / div;
  169. survey->channel_time_busy += cc->rx_busy / div;
  170. survey->channel_time_rx += cc->rx_frame / div;
  171. survey->channel_time_tx += cc->tx_frame / div;
  172. }
  173. if (cc->cycles < div)
  174. return -1;
  175. if (cc->cycles > 0)
  176. ret = cc->rx_busy * 100 / cc->cycles;
  177. memset(cc, 0, sizeof(*cc));
  178. ath_update_survey_nf(sc, pos);
  179. return ret;
  180. }
  181. /*
  182. * Set/change channels. If the channel is really being changed, it's done
  183. * by reseting the chip. To accomplish this we must first cleanup any pending
  184. * DMA, then restart stuff.
  185. */
  186. int ath_set_channel(struct ath_softc *sc, struct ieee80211_hw *hw,
  187. struct ath9k_channel *hchan)
  188. {
  189. struct ath_hw *ah = sc->sc_ah;
  190. struct ath_common *common = ath9k_hw_common(ah);
  191. struct ieee80211_conf *conf = &common->hw->conf;
  192. bool fastcc = true, stopped;
  193. struct ieee80211_channel *channel = hw->conf.channel;
  194. struct ath9k_hw_cal_data *caldata = NULL;
  195. int r;
  196. if (sc->sc_flags & SC_OP_INVALID)
  197. return -EIO;
  198. sc->hw_busy_count = 0;
  199. del_timer_sync(&common->ani.timer);
  200. cancel_work_sync(&sc->paprd_work);
  201. cancel_work_sync(&sc->hw_check_work);
  202. cancel_delayed_work_sync(&sc->tx_complete_work);
  203. cancel_delayed_work_sync(&sc->hw_pll_work);
  204. ath9k_ps_wakeup(sc);
  205. spin_lock_bh(&sc->sc_pcu_lock);
  206. /*
  207. * This is only performed if the channel settings have
  208. * actually changed.
  209. *
  210. * To switch channels clear any pending DMA operations;
  211. * wait long enough for the RX fifo to drain, reset the
  212. * hardware at the new frequency, and then re-enable
  213. * the relevant bits of the h/w.
  214. */
  215. ath9k_hw_disable_interrupts(ah);
  216. stopped = ath_drain_all_txq(sc, false);
  217. if (!ath_stoprecv(sc))
  218. stopped = false;
  219. if (!ath9k_hw_check_alive(ah))
  220. stopped = false;
  221. /* XXX: do not flush receive queue here. We don't want
  222. * to flush data frames already in queue because of
  223. * changing channel. */
  224. if (!stopped || !(sc->sc_flags & SC_OP_OFFCHANNEL))
  225. fastcc = false;
  226. if (!(sc->sc_flags & SC_OP_OFFCHANNEL))
  227. caldata = &sc->caldata;
  228. ath_dbg(common, ATH_DBG_CONFIG,
  229. "(%u MHz) -> (%u MHz), conf_is_ht40: %d fastcc: %d\n",
  230. sc->sc_ah->curchan->channel,
  231. channel->center_freq, conf_is_ht40(conf),
  232. fastcc);
  233. r = ath9k_hw_reset(ah, hchan, caldata, fastcc);
  234. if (r) {
  235. ath_err(common,
  236. "Unable to reset channel (%u MHz), reset status %d\n",
  237. channel->center_freq, r);
  238. goto ps_restore;
  239. }
  240. if (ath_startrecv(sc) != 0) {
  241. ath_err(common, "Unable to restart recv logic\n");
  242. r = -EIO;
  243. goto ps_restore;
  244. }
  245. ath9k_cmn_update_txpow(ah, sc->curtxpow,
  246. sc->config.txpowlimit, &sc->curtxpow);
  247. ath9k_hw_set_interrupts(ah, ah->imask);
  248. if (!(sc->sc_flags & (SC_OP_OFFCHANNEL))) {
  249. if (sc->sc_flags & SC_OP_BEACONS)
  250. ath_set_beacon(sc);
  251. ieee80211_queue_delayed_work(sc->hw, &sc->tx_complete_work, 0);
  252. ieee80211_queue_delayed_work(sc->hw, &sc->hw_pll_work, HZ/2);
  253. ath_start_ani(common);
  254. }
  255. ps_restore:
  256. ieee80211_wake_queues(hw);
  257. spin_unlock_bh(&sc->sc_pcu_lock);
  258. ath9k_ps_restore(sc);
  259. return r;
  260. }
  261. static void ath_paprd_activate(struct ath_softc *sc)
  262. {
  263. struct ath_hw *ah = sc->sc_ah;
  264. struct ath9k_hw_cal_data *caldata = ah->caldata;
  265. struct ath_common *common = ath9k_hw_common(ah);
  266. int chain;
  267. if (!caldata || !caldata->paprd_done)
  268. return;
  269. ath9k_ps_wakeup(sc);
  270. ar9003_paprd_enable(ah, false);
  271. for (chain = 0; chain < AR9300_MAX_CHAINS; chain++) {
  272. if (!(common->tx_chainmask & BIT(chain)))
  273. continue;
  274. ar9003_paprd_populate_single_table(ah, caldata, chain);
  275. }
  276. ar9003_paprd_enable(ah, true);
  277. ath9k_ps_restore(sc);
  278. }
  279. static bool ath_paprd_send_frame(struct ath_softc *sc, struct sk_buff *skb, int chain)
  280. {
  281. struct ieee80211_hw *hw = sc->hw;
  282. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
  283. struct ath_hw *ah = sc->sc_ah;
  284. struct ath_common *common = ath9k_hw_common(ah);
  285. struct ath_tx_control txctl;
  286. int time_left;
  287. memset(&txctl, 0, sizeof(txctl));
  288. txctl.txq = sc->tx.txq_map[WME_AC_BE];
  289. memset(tx_info, 0, sizeof(*tx_info));
  290. tx_info->band = hw->conf.channel->band;
  291. tx_info->flags |= IEEE80211_TX_CTL_NO_ACK;
  292. tx_info->control.rates[0].idx = 0;
  293. tx_info->control.rates[0].count = 1;
  294. tx_info->control.rates[0].flags = IEEE80211_TX_RC_MCS;
  295. tx_info->control.rates[1].idx = -1;
  296. init_completion(&sc->paprd_complete);
  297. txctl.paprd = BIT(chain);
  298. if (ath_tx_start(hw, skb, &txctl) != 0) {
  299. ath_dbg(common, ATH_DBG_XMIT, "PAPRD TX failed\n");
  300. dev_kfree_skb_any(skb);
  301. return false;
  302. }
  303. time_left = wait_for_completion_timeout(&sc->paprd_complete,
  304. msecs_to_jiffies(ATH_PAPRD_TIMEOUT));
  305. if (!time_left)
  306. ath_dbg(ath9k_hw_common(sc->sc_ah), ATH_DBG_CALIBRATE,
  307. "Timeout waiting for paprd training on TX chain %d\n",
  308. chain);
  309. return !!time_left;
  310. }
  311. void ath_paprd_calibrate(struct work_struct *work)
  312. {
  313. struct ath_softc *sc = container_of(work, struct ath_softc, paprd_work);
  314. struct ieee80211_hw *hw = sc->hw;
  315. struct ath_hw *ah = sc->sc_ah;
  316. struct ieee80211_hdr *hdr;
  317. struct sk_buff *skb = NULL;
  318. struct ath9k_hw_cal_data *caldata = ah->caldata;
  319. struct ath_common *common = ath9k_hw_common(ah);
  320. int ftype;
  321. int chain_ok = 0;
  322. int chain;
  323. int len = 1800;
  324. if (!caldata)
  325. return;
  326. if (ar9003_paprd_init_table(ah) < 0)
  327. return;
  328. skb = alloc_skb(len, GFP_KERNEL);
  329. if (!skb)
  330. return;
  331. skb_put(skb, len);
  332. memset(skb->data, 0, len);
  333. hdr = (struct ieee80211_hdr *)skb->data;
  334. ftype = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC;
  335. hdr->frame_control = cpu_to_le16(ftype);
  336. hdr->duration_id = cpu_to_le16(10);
  337. memcpy(hdr->addr1, hw->wiphy->perm_addr, ETH_ALEN);
  338. memcpy(hdr->addr2, hw->wiphy->perm_addr, ETH_ALEN);
  339. memcpy(hdr->addr3, hw->wiphy->perm_addr, ETH_ALEN);
  340. ath9k_ps_wakeup(sc);
  341. for (chain = 0; chain < AR9300_MAX_CHAINS; chain++) {
  342. if (!(common->tx_chainmask & BIT(chain)))
  343. continue;
  344. chain_ok = 0;
  345. ath_dbg(common, ATH_DBG_CALIBRATE,
  346. "Sending PAPRD frame for thermal measurement "
  347. "on chain %d\n", chain);
  348. if (!ath_paprd_send_frame(sc, skb, chain))
  349. goto fail_paprd;
  350. ar9003_paprd_setup_gain_table(ah, chain);
  351. ath_dbg(common, ATH_DBG_CALIBRATE,
  352. "Sending PAPRD training frame on chain %d\n", chain);
  353. if (!ath_paprd_send_frame(sc, skb, chain))
  354. goto fail_paprd;
  355. if (!ar9003_paprd_is_done(ah))
  356. break;
  357. if (ar9003_paprd_create_curve(ah, caldata, chain) != 0)
  358. break;
  359. chain_ok = 1;
  360. }
  361. kfree_skb(skb);
  362. if (chain_ok) {
  363. caldata->paprd_done = true;
  364. ath_paprd_activate(sc);
  365. }
  366. fail_paprd:
  367. ath9k_ps_restore(sc);
  368. }
  369. /*
  370. * This routine performs the periodic noise floor calibration function
  371. * that is used to adjust and optimize the chip performance. This
  372. * takes environmental changes (location, temperature) into account.
  373. * When the task is complete, it reschedules itself depending on the
  374. * appropriate interval that was calculated.
  375. */
  376. void ath_ani_calibrate(unsigned long data)
  377. {
  378. struct ath_softc *sc = (struct ath_softc *)data;
  379. struct ath_hw *ah = sc->sc_ah;
  380. struct ath_common *common = ath9k_hw_common(ah);
  381. bool longcal = false;
  382. bool shortcal = false;
  383. bool aniflag = false;
  384. unsigned int timestamp = jiffies_to_msecs(jiffies);
  385. u32 cal_interval, short_cal_interval, long_cal_interval;
  386. unsigned long flags;
  387. if (ah->caldata && ah->caldata->nfcal_interference)
  388. long_cal_interval = ATH_LONG_CALINTERVAL_INT;
  389. else
  390. long_cal_interval = ATH_LONG_CALINTERVAL;
  391. short_cal_interval = (ah->opmode == NL80211_IFTYPE_AP) ?
  392. ATH_AP_SHORT_CALINTERVAL : ATH_STA_SHORT_CALINTERVAL;
  393. /* Only calibrate if awake */
  394. if (sc->sc_ah->power_mode != ATH9K_PM_AWAKE)
  395. goto set_timer;
  396. ath9k_ps_wakeup(sc);
  397. /* Long calibration runs independently of short calibration. */
  398. if ((timestamp - common->ani.longcal_timer) >= long_cal_interval) {
  399. longcal = true;
  400. ath_dbg(common, ATH_DBG_ANI, "longcal @%lu\n", jiffies);
  401. common->ani.longcal_timer = timestamp;
  402. }
  403. /* Short calibration applies only while caldone is false */
  404. if (!common->ani.caldone) {
  405. if ((timestamp - common->ani.shortcal_timer) >= short_cal_interval) {
  406. shortcal = true;
  407. ath_dbg(common, ATH_DBG_ANI,
  408. "shortcal @%lu\n", jiffies);
  409. common->ani.shortcal_timer = timestamp;
  410. common->ani.resetcal_timer = timestamp;
  411. }
  412. } else {
  413. if ((timestamp - common->ani.resetcal_timer) >=
  414. ATH_RESTART_CALINTERVAL) {
  415. common->ani.caldone = ath9k_hw_reset_calvalid(ah);
  416. if (common->ani.caldone)
  417. common->ani.resetcal_timer = timestamp;
  418. }
  419. }
  420. /* Verify whether we must check ANI */
  421. if ((timestamp - common->ani.checkani_timer) >=
  422. ah->config.ani_poll_interval) {
  423. aniflag = true;
  424. common->ani.checkani_timer = timestamp;
  425. }
  426. /* Skip all processing if there's nothing to do. */
  427. if (longcal || shortcal || aniflag) {
  428. /* Call ANI routine if necessary */
  429. if (aniflag) {
  430. spin_lock_irqsave(&common->cc_lock, flags);
  431. ath9k_hw_ani_monitor(ah, ah->curchan);
  432. ath_update_survey_stats(sc);
  433. spin_unlock_irqrestore(&common->cc_lock, flags);
  434. }
  435. /* Perform calibration if necessary */
  436. if (longcal || shortcal) {
  437. common->ani.caldone =
  438. ath9k_hw_calibrate(ah,
  439. ah->curchan,
  440. common->rx_chainmask,
  441. longcal);
  442. }
  443. }
  444. ath9k_ps_restore(sc);
  445. set_timer:
  446. /*
  447. * Set timer interval based on previous results.
  448. * The interval must be the shortest necessary to satisfy ANI,
  449. * short calibration and long calibration.
  450. */
  451. cal_interval = ATH_LONG_CALINTERVAL;
  452. if (sc->sc_ah->config.enable_ani)
  453. cal_interval = min(cal_interval,
  454. (u32)ah->config.ani_poll_interval);
  455. if (!common->ani.caldone)
  456. cal_interval = min(cal_interval, (u32)short_cal_interval);
  457. mod_timer(&common->ani.timer, jiffies + msecs_to_jiffies(cal_interval));
  458. if ((sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_PAPRD) && ah->caldata) {
  459. if (!ah->caldata->paprd_done)
  460. ieee80211_queue_work(sc->hw, &sc->paprd_work);
  461. else if (!ah->paprd_table_write_done)
  462. ath_paprd_activate(sc);
  463. }
  464. }
  465. static void ath_node_attach(struct ath_softc *sc, struct ieee80211_sta *sta)
  466. {
  467. struct ath_node *an;
  468. struct ath_hw *ah = sc->sc_ah;
  469. an = (struct ath_node *)sta->drv_priv;
  470. #ifdef CONFIG_ATH9K_DEBUGFS
  471. spin_lock(&sc->nodes_lock);
  472. list_add(&an->list, &sc->nodes);
  473. spin_unlock(&sc->nodes_lock);
  474. an->sta = sta;
  475. #endif
  476. if ((ah->caps.hw_caps) & ATH9K_HW_CAP_APM)
  477. sc->sc_flags |= SC_OP_ENABLE_APM;
  478. if (sc->sc_flags & SC_OP_TXAGGR) {
  479. ath_tx_node_init(sc, an);
  480. an->maxampdu = 1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
  481. sta->ht_cap.ampdu_factor);
  482. an->mpdudensity = parse_mpdudensity(sta->ht_cap.ampdu_density);
  483. }
  484. }
  485. static void ath_node_detach(struct ath_softc *sc, struct ieee80211_sta *sta)
  486. {
  487. struct ath_node *an = (struct ath_node *)sta->drv_priv;
  488. #ifdef CONFIG_ATH9K_DEBUGFS
  489. spin_lock(&sc->nodes_lock);
  490. list_del(&an->list);
  491. spin_unlock(&sc->nodes_lock);
  492. an->sta = NULL;
  493. #endif
  494. if (sc->sc_flags & SC_OP_TXAGGR)
  495. ath_tx_node_cleanup(sc, an);
  496. }
  497. void ath_hw_check(struct work_struct *work)
  498. {
  499. struct ath_softc *sc = container_of(work, struct ath_softc, hw_check_work);
  500. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  501. unsigned long flags;
  502. int busy;
  503. ath9k_ps_wakeup(sc);
  504. if (ath9k_hw_check_alive(sc->sc_ah))
  505. goto out;
  506. spin_lock_irqsave(&common->cc_lock, flags);
  507. busy = ath_update_survey_stats(sc);
  508. spin_unlock_irqrestore(&common->cc_lock, flags);
  509. ath_dbg(common, ATH_DBG_RESET, "Possible baseband hang, "
  510. "busy=%d (try %d)\n", busy, sc->hw_busy_count + 1);
  511. if (busy >= 99) {
  512. if (++sc->hw_busy_count >= 3)
  513. ath_reset(sc, true);
  514. } else if (busy >= 0)
  515. sc->hw_busy_count = 0;
  516. out:
  517. ath9k_ps_restore(sc);
  518. }
  519. void ath9k_tasklet(unsigned long data)
  520. {
  521. struct ath_softc *sc = (struct ath_softc *)data;
  522. struct ath_hw *ah = sc->sc_ah;
  523. struct ath_common *common = ath9k_hw_common(ah);
  524. u32 status = sc->intrstatus;
  525. u32 rxmask;
  526. if (status & ATH9K_INT_FATAL) {
  527. ath_reset(sc, true);
  528. return;
  529. }
  530. ath9k_ps_wakeup(sc);
  531. spin_lock(&sc->sc_pcu_lock);
  532. /*
  533. * Only run the baseband hang check if beacons stop working in AP or
  534. * IBSS mode, because it has a high false positive rate. For station
  535. * mode it should not be necessary, since the upper layers will detect
  536. * this through a beacon miss automatically and the following channel
  537. * change will trigger a hardware reset anyway
  538. */
  539. if (ath9k_hw_numtxpending(ah, sc->beacon.beaconq) != 0 &&
  540. !ath9k_hw_check_alive(ah))
  541. ieee80211_queue_work(sc->hw, &sc->hw_check_work);
  542. if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
  543. rxmask = (ATH9K_INT_RXHP | ATH9K_INT_RXLP | ATH9K_INT_RXEOL |
  544. ATH9K_INT_RXORN);
  545. else
  546. rxmask = (ATH9K_INT_RX | ATH9K_INT_RXEOL | ATH9K_INT_RXORN);
  547. if (status & rxmask) {
  548. /* Check for high priority Rx first */
  549. if ((ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) &&
  550. (status & ATH9K_INT_RXHP))
  551. ath_rx_tasklet(sc, 0, true);
  552. ath_rx_tasklet(sc, 0, false);
  553. }
  554. if (status & ATH9K_INT_TX) {
  555. if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
  556. ath_tx_edma_tasklet(sc);
  557. else
  558. ath_tx_tasklet(sc);
  559. }
  560. if ((status & ATH9K_INT_TSFOOR) && sc->ps_enabled) {
  561. /*
  562. * TSF sync does not look correct; remain awake to sync with
  563. * the next Beacon.
  564. */
  565. ath_dbg(common, ATH_DBG_PS,
  566. "TSFOOR - Sync with next Beacon\n");
  567. sc->ps_flags |= PS_WAIT_FOR_BEACON | PS_BEACON_SYNC;
  568. }
  569. if (ah->btcoex_hw.scheme == ATH_BTCOEX_CFG_3WIRE)
  570. if (status & ATH9K_INT_GENTIMER)
  571. ath_gen_timer_isr(sc->sc_ah);
  572. /* re-enable hardware interrupt */
  573. ath9k_hw_enable_interrupts(ah);
  574. spin_unlock(&sc->sc_pcu_lock);
  575. ath9k_ps_restore(sc);
  576. }
  577. irqreturn_t ath_isr(int irq, void *dev)
  578. {
  579. #define SCHED_INTR ( \
  580. ATH9K_INT_FATAL | \
  581. ATH9K_INT_RXORN | \
  582. ATH9K_INT_RXEOL | \
  583. ATH9K_INT_RX | \
  584. ATH9K_INT_RXLP | \
  585. ATH9K_INT_RXHP | \
  586. ATH9K_INT_TX | \
  587. ATH9K_INT_BMISS | \
  588. ATH9K_INT_CST | \
  589. ATH9K_INT_TSFOOR | \
  590. ATH9K_INT_GENTIMER)
  591. struct ath_softc *sc = dev;
  592. struct ath_hw *ah = sc->sc_ah;
  593. struct ath_common *common = ath9k_hw_common(ah);
  594. enum ath9k_int status;
  595. bool sched = false;
  596. /*
  597. * The hardware is not ready/present, don't
  598. * touch anything. Note this can happen early
  599. * on if the IRQ is shared.
  600. */
  601. if (sc->sc_flags & SC_OP_INVALID)
  602. return IRQ_NONE;
  603. /* shared irq, not for us */
  604. if (!ath9k_hw_intrpend(ah))
  605. return IRQ_NONE;
  606. /*
  607. * Figure out the reason(s) for the interrupt. Note
  608. * that the hal returns a pseudo-ISR that may include
  609. * bits we haven't explicitly enabled so we mask the
  610. * value to insure we only process bits we requested.
  611. */
  612. ath9k_hw_getisr(ah, &status); /* NB: clears ISR too */
  613. status &= ah->imask; /* discard unasked-for bits */
  614. /*
  615. * If there are no status bits set, then this interrupt was not
  616. * for me (should have been caught above).
  617. */
  618. if (!status)
  619. return IRQ_NONE;
  620. /* Cache the status */
  621. sc->intrstatus = status;
  622. if (status & SCHED_INTR)
  623. sched = true;
  624. /*
  625. * If a FATAL or RXORN interrupt is received, we have to reset the
  626. * chip immediately.
  627. */
  628. if ((status & ATH9K_INT_FATAL) || ((status & ATH9K_INT_RXORN) &&
  629. !(ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)))
  630. goto chip_reset;
  631. if ((ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) &&
  632. (status & ATH9K_INT_BB_WATCHDOG)) {
  633. spin_lock(&common->cc_lock);
  634. ath_hw_cycle_counters_update(common);
  635. ar9003_hw_bb_watchdog_dbg_info(ah);
  636. spin_unlock(&common->cc_lock);
  637. goto chip_reset;
  638. }
  639. if (status & ATH9K_INT_SWBA)
  640. tasklet_schedule(&sc->bcon_tasklet);
  641. if (status & ATH9K_INT_TXURN)
  642. ath9k_hw_updatetxtriglevel(ah, true);
  643. if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
  644. if (status & ATH9K_INT_RXEOL) {
  645. ah->imask &= ~(ATH9K_INT_RXEOL | ATH9K_INT_RXORN);
  646. ath9k_hw_set_interrupts(ah, ah->imask);
  647. }
  648. }
  649. if (status & ATH9K_INT_MIB) {
  650. /*
  651. * Disable interrupts until we service the MIB
  652. * interrupt; otherwise it will continue to
  653. * fire.
  654. */
  655. ath9k_hw_disable_interrupts(ah);
  656. /*
  657. * Let the hal handle the event. We assume
  658. * it will clear whatever condition caused
  659. * the interrupt.
  660. */
  661. spin_lock(&common->cc_lock);
  662. ath9k_hw_proc_mib_event(ah);
  663. spin_unlock(&common->cc_lock);
  664. ath9k_hw_enable_interrupts(ah);
  665. }
  666. if (!(ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP))
  667. if (status & ATH9K_INT_TIM_TIMER) {
  668. if (ATH_DBG_WARN_ON_ONCE(sc->ps_idle))
  669. goto chip_reset;
  670. /* Clear RxAbort bit so that we can
  671. * receive frames */
  672. ath9k_setpower(sc, ATH9K_PM_AWAKE);
  673. ath9k_hw_setrxabort(sc->sc_ah, 0);
  674. sc->ps_flags |= PS_WAIT_FOR_BEACON;
  675. }
  676. chip_reset:
  677. ath_debug_stat_interrupt(sc, status);
  678. if (sched) {
  679. /* turn off every interrupt */
  680. ath9k_hw_disable_interrupts(ah);
  681. tasklet_schedule(&sc->intr_tq);
  682. }
  683. return IRQ_HANDLED;
  684. #undef SCHED_INTR
  685. }
  686. void ath_radio_enable(struct ath_softc *sc, struct ieee80211_hw *hw)
  687. {
  688. struct ath_hw *ah = sc->sc_ah;
  689. struct ath_common *common = ath9k_hw_common(ah);
  690. struct ieee80211_channel *channel = hw->conf.channel;
  691. int r;
  692. ath9k_ps_wakeup(sc);
  693. spin_lock_bh(&sc->sc_pcu_lock);
  694. ath9k_hw_configpcipowersave(ah, 0, 0);
  695. if (!ah->curchan)
  696. ah->curchan = ath9k_cmn_get_curchannel(sc->hw, ah);
  697. r = ath9k_hw_reset(ah, ah->curchan, ah->caldata, false);
  698. if (r) {
  699. ath_err(common,
  700. "Unable to reset channel (%u MHz), reset status %d\n",
  701. channel->center_freq, r);
  702. }
  703. ath9k_cmn_update_txpow(ah, sc->curtxpow,
  704. sc->config.txpowlimit, &sc->curtxpow);
  705. if (ath_startrecv(sc) != 0) {
  706. ath_err(common, "Unable to restart recv logic\n");
  707. goto out;
  708. }
  709. if (sc->sc_flags & SC_OP_BEACONS)
  710. ath_set_beacon(sc); /* restart beacons */
  711. /* Re-Enable interrupts */
  712. ath9k_hw_set_interrupts(ah, ah->imask);
  713. /* Enable LED */
  714. ath9k_hw_cfg_output(ah, ah->led_pin,
  715. AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
  716. ath9k_hw_set_gpio(ah, ah->led_pin, 0);
  717. ieee80211_wake_queues(hw);
  718. ieee80211_queue_delayed_work(hw, &sc->hw_pll_work, HZ/2);
  719. out:
  720. spin_unlock_bh(&sc->sc_pcu_lock);
  721. ath9k_ps_restore(sc);
  722. }
  723. void ath_radio_disable(struct ath_softc *sc, struct ieee80211_hw *hw)
  724. {
  725. struct ath_hw *ah = sc->sc_ah;
  726. struct ieee80211_channel *channel = hw->conf.channel;
  727. int r;
  728. ath9k_ps_wakeup(sc);
  729. cancel_delayed_work_sync(&sc->hw_pll_work);
  730. spin_lock_bh(&sc->sc_pcu_lock);
  731. ieee80211_stop_queues(hw);
  732. /*
  733. * Keep the LED on when the radio is disabled
  734. * during idle unassociated state.
  735. */
  736. if (!sc->ps_idle) {
  737. ath9k_hw_set_gpio(ah, ah->led_pin, 1);
  738. ath9k_hw_cfg_gpio_input(ah, ah->led_pin);
  739. }
  740. /* Disable interrupts */
  741. ath9k_hw_disable_interrupts(ah);
  742. ath_drain_all_txq(sc, false); /* clear pending tx frames */
  743. ath_stoprecv(sc); /* turn off frame recv */
  744. ath_flushrecv(sc); /* flush recv queue */
  745. if (!ah->curchan)
  746. ah->curchan = ath9k_cmn_get_curchannel(hw, ah);
  747. r = ath9k_hw_reset(ah, ah->curchan, ah->caldata, false);
  748. if (r) {
  749. ath_err(ath9k_hw_common(sc->sc_ah),
  750. "Unable to reset channel (%u MHz), reset status %d\n",
  751. channel->center_freq, r);
  752. }
  753. ath9k_hw_phy_disable(ah);
  754. ath9k_hw_configpcipowersave(ah, 1, 1);
  755. spin_unlock_bh(&sc->sc_pcu_lock);
  756. ath9k_ps_restore(sc);
  757. }
  758. int ath_reset(struct ath_softc *sc, bool retry_tx)
  759. {
  760. struct ath_hw *ah = sc->sc_ah;
  761. struct ath_common *common = ath9k_hw_common(ah);
  762. struct ieee80211_hw *hw = sc->hw;
  763. int r;
  764. sc->hw_busy_count = 0;
  765. /* Stop ANI */
  766. del_timer_sync(&common->ani.timer);
  767. ath9k_ps_wakeup(sc);
  768. spin_lock_bh(&sc->sc_pcu_lock);
  769. ieee80211_stop_queues(hw);
  770. ath9k_hw_disable_interrupts(ah);
  771. ath_drain_all_txq(sc, retry_tx);
  772. ath_stoprecv(sc);
  773. ath_flushrecv(sc);
  774. r = ath9k_hw_reset(ah, sc->sc_ah->curchan, ah->caldata, false);
  775. if (r)
  776. ath_err(common,
  777. "Unable to reset hardware; reset status %d\n", r);
  778. if (ath_startrecv(sc) != 0)
  779. ath_err(common, "Unable to start recv logic\n");
  780. /*
  781. * We may be doing a reset in response to a request
  782. * that changes the channel so update any state that
  783. * might change as a result.
  784. */
  785. ath9k_cmn_update_txpow(ah, sc->curtxpow,
  786. sc->config.txpowlimit, &sc->curtxpow);
  787. if ((sc->sc_flags & SC_OP_BEACONS) || !(sc->sc_flags & (SC_OP_OFFCHANNEL)))
  788. ath_set_beacon(sc); /* restart beacons */
  789. ath9k_hw_set_interrupts(ah, ah->imask);
  790. if (retry_tx) {
  791. int i;
  792. for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
  793. if (ATH_TXQ_SETUP(sc, i)) {
  794. spin_lock_bh(&sc->tx.txq[i].axq_lock);
  795. ath_txq_schedule(sc, &sc->tx.txq[i]);
  796. spin_unlock_bh(&sc->tx.txq[i].axq_lock);
  797. }
  798. }
  799. }
  800. ieee80211_wake_queues(hw);
  801. spin_unlock_bh(&sc->sc_pcu_lock);
  802. /* Start ANI */
  803. ath_start_ani(common);
  804. ath9k_ps_restore(sc);
  805. return r;
  806. }
  807. /**********************/
  808. /* mac80211 callbacks */
  809. /**********************/
  810. static int ath9k_start(struct ieee80211_hw *hw)
  811. {
  812. struct ath_softc *sc = hw->priv;
  813. struct ath_hw *ah = sc->sc_ah;
  814. struct ath_common *common = ath9k_hw_common(ah);
  815. struct ieee80211_channel *curchan = hw->conf.channel;
  816. struct ath9k_channel *init_channel;
  817. int r;
  818. ath_dbg(common, ATH_DBG_CONFIG,
  819. "Starting driver with initial channel: %d MHz\n",
  820. curchan->center_freq);
  821. ath9k_ps_wakeup(sc);
  822. mutex_lock(&sc->mutex);
  823. /* setup initial channel */
  824. sc->chan_idx = curchan->hw_value;
  825. init_channel = ath9k_cmn_get_curchannel(hw, ah);
  826. /* Reset SERDES registers */
  827. ath9k_hw_configpcipowersave(ah, 0, 0);
  828. /*
  829. * The basic interface to setting the hardware in a good
  830. * state is ``reset''. On return the hardware is known to
  831. * be powered up and with interrupts disabled. This must
  832. * be followed by initialization of the appropriate bits
  833. * and then setup of the interrupt mask.
  834. */
  835. spin_lock_bh(&sc->sc_pcu_lock);
  836. r = ath9k_hw_reset(ah, init_channel, ah->caldata, false);
  837. if (r) {
  838. ath_err(common,
  839. "Unable to reset hardware; reset status %d (freq %u MHz)\n",
  840. r, curchan->center_freq);
  841. spin_unlock_bh(&sc->sc_pcu_lock);
  842. goto mutex_unlock;
  843. }
  844. /*
  845. * This is needed only to setup initial state
  846. * but it's best done after a reset.
  847. */
  848. ath9k_cmn_update_txpow(ah, sc->curtxpow,
  849. sc->config.txpowlimit, &sc->curtxpow);
  850. /*
  851. * Setup the hardware after reset:
  852. * The receive engine is set going.
  853. * Frame transmit is handled entirely
  854. * in the frame output path; there's nothing to do
  855. * here except setup the interrupt mask.
  856. */
  857. if (ath_startrecv(sc) != 0) {
  858. ath_err(common, "Unable to start recv logic\n");
  859. r = -EIO;
  860. spin_unlock_bh(&sc->sc_pcu_lock);
  861. goto mutex_unlock;
  862. }
  863. spin_unlock_bh(&sc->sc_pcu_lock);
  864. /* Setup our intr mask. */
  865. ah->imask = ATH9K_INT_TX | ATH9K_INT_RXEOL |
  866. ATH9K_INT_RXORN | ATH9K_INT_FATAL |
  867. ATH9K_INT_GLOBAL;
  868. if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
  869. ah->imask |= ATH9K_INT_RXHP |
  870. ATH9K_INT_RXLP |
  871. ATH9K_INT_BB_WATCHDOG;
  872. else
  873. ah->imask |= ATH9K_INT_RX;
  874. ah->imask |= ATH9K_INT_GTT;
  875. if (ah->caps.hw_caps & ATH9K_HW_CAP_HT)
  876. ah->imask |= ATH9K_INT_CST;
  877. sc->sc_flags &= ~SC_OP_INVALID;
  878. sc->sc_ah->is_monitoring = false;
  879. /* Disable BMISS interrupt when we're not associated */
  880. ah->imask &= ~(ATH9K_INT_SWBA | ATH9K_INT_BMISS);
  881. ath9k_hw_set_interrupts(ah, ah->imask);
  882. ieee80211_wake_queues(hw);
  883. ieee80211_queue_delayed_work(sc->hw, &sc->tx_complete_work, 0);
  884. if ((ah->btcoex_hw.scheme != ATH_BTCOEX_CFG_NONE) &&
  885. !ah->btcoex_hw.enabled) {
  886. ath9k_hw_btcoex_set_weight(ah, AR_BT_COEX_WGHT,
  887. AR_STOMP_LOW_WLAN_WGHT);
  888. ath9k_hw_btcoex_enable(ah);
  889. if (common->bus_ops->bt_coex_prep)
  890. common->bus_ops->bt_coex_prep(common);
  891. if (ah->btcoex_hw.scheme == ATH_BTCOEX_CFG_3WIRE)
  892. ath9k_btcoex_timer_resume(sc);
  893. }
  894. if (ah->caps.pcie_lcr_extsync_en && common->bus_ops->extn_synch_en)
  895. common->bus_ops->extn_synch_en(common);
  896. mutex_unlock:
  897. mutex_unlock(&sc->mutex);
  898. ath9k_ps_restore(sc);
  899. return r;
  900. }
  901. static void ath9k_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
  902. {
  903. struct ath_softc *sc = hw->priv;
  904. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  905. struct ath_tx_control txctl;
  906. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  907. if (sc->ps_enabled) {
  908. /*
  909. * mac80211 does not set PM field for normal data frames, so we
  910. * need to update that based on the current PS mode.
  911. */
  912. if (ieee80211_is_data(hdr->frame_control) &&
  913. !ieee80211_is_nullfunc(hdr->frame_control) &&
  914. !ieee80211_has_pm(hdr->frame_control)) {
  915. ath_dbg(common, ATH_DBG_PS,
  916. "Add PM=1 for a TX frame while in PS mode\n");
  917. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
  918. }
  919. }
  920. if (unlikely(sc->sc_ah->power_mode != ATH9K_PM_AWAKE)) {
  921. /*
  922. * We are using PS-Poll and mac80211 can request TX while in
  923. * power save mode. Need to wake up hardware for the TX to be
  924. * completed and if needed, also for RX of buffered frames.
  925. */
  926. ath9k_ps_wakeup(sc);
  927. if (!(sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP))
  928. ath9k_hw_setrxabort(sc->sc_ah, 0);
  929. if (ieee80211_is_pspoll(hdr->frame_control)) {
  930. ath_dbg(common, ATH_DBG_PS,
  931. "Sending PS-Poll to pick a buffered frame\n");
  932. sc->ps_flags |= PS_WAIT_FOR_PSPOLL_DATA;
  933. } else {
  934. ath_dbg(common, ATH_DBG_PS,
  935. "Wake up to complete TX\n");
  936. sc->ps_flags |= PS_WAIT_FOR_TX_ACK;
  937. }
  938. /*
  939. * The actual restore operation will happen only after
  940. * the sc_flags bit is cleared. We are just dropping
  941. * the ps_usecount here.
  942. */
  943. ath9k_ps_restore(sc);
  944. }
  945. memset(&txctl, 0, sizeof(struct ath_tx_control));
  946. txctl.txq = sc->tx.txq_map[skb_get_queue_mapping(skb)];
  947. ath_dbg(common, ATH_DBG_XMIT, "transmitting packet, skb: %p\n", skb);
  948. if (ath_tx_start(hw, skb, &txctl) != 0) {
  949. ath_dbg(common, ATH_DBG_XMIT, "TX failed\n");
  950. goto exit;
  951. }
  952. return;
  953. exit:
  954. dev_kfree_skb_any(skb);
  955. }
  956. static void ath9k_stop(struct ieee80211_hw *hw)
  957. {
  958. struct ath_softc *sc = hw->priv;
  959. struct ath_hw *ah = sc->sc_ah;
  960. struct ath_common *common = ath9k_hw_common(ah);
  961. mutex_lock(&sc->mutex);
  962. cancel_delayed_work_sync(&sc->tx_complete_work);
  963. cancel_delayed_work_sync(&sc->hw_pll_work);
  964. cancel_work_sync(&sc->paprd_work);
  965. cancel_work_sync(&sc->hw_check_work);
  966. if (sc->sc_flags & SC_OP_INVALID) {
  967. ath_dbg(common, ATH_DBG_ANY, "Device not present\n");
  968. mutex_unlock(&sc->mutex);
  969. return;
  970. }
  971. /* Ensure HW is awake when we try to shut it down. */
  972. ath9k_ps_wakeup(sc);
  973. if (ah->btcoex_hw.enabled) {
  974. ath9k_hw_btcoex_disable(ah);
  975. if (ah->btcoex_hw.scheme == ATH_BTCOEX_CFG_3WIRE)
  976. ath9k_btcoex_timer_pause(sc);
  977. }
  978. spin_lock_bh(&sc->sc_pcu_lock);
  979. /* prevent tasklets to enable interrupts once we disable them */
  980. ah->imask &= ~ATH9K_INT_GLOBAL;
  981. /* make sure h/w will not generate any interrupt
  982. * before setting the invalid flag. */
  983. ath9k_hw_disable_interrupts(ah);
  984. if (!(sc->sc_flags & SC_OP_INVALID)) {
  985. ath_drain_all_txq(sc, false);
  986. ath_stoprecv(sc);
  987. ath9k_hw_phy_disable(ah);
  988. } else
  989. sc->rx.rxlink = NULL;
  990. if (sc->rx.frag) {
  991. dev_kfree_skb_any(sc->rx.frag);
  992. sc->rx.frag = NULL;
  993. }
  994. /* disable HAL and put h/w to sleep */
  995. ath9k_hw_disable(ah);
  996. ath9k_hw_configpcipowersave(ah, 1, 1);
  997. spin_unlock_bh(&sc->sc_pcu_lock);
  998. /* we can now sync irq and kill any running tasklets, since we already
  999. * disabled interrupts and not holding a spin lock */
  1000. synchronize_irq(sc->irq);
  1001. tasklet_kill(&sc->intr_tq);
  1002. tasklet_kill(&sc->bcon_tasklet);
  1003. ath9k_ps_restore(sc);
  1004. sc->ps_idle = true;
  1005. ath_radio_disable(sc, hw);
  1006. sc->sc_flags |= SC_OP_INVALID;
  1007. mutex_unlock(&sc->mutex);
  1008. ath_dbg(common, ATH_DBG_CONFIG, "Driver halt\n");
  1009. }
  1010. bool ath9k_uses_beacons(int type)
  1011. {
  1012. switch (type) {
  1013. case NL80211_IFTYPE_AP:
  1014. case NL80211_IFTYPE_ADHOC:
  1015. case NL80211_IFTYPE_MESH_POINT:
  1016. return true;
  1017. default:
  1018. return false;
  1019. }
  1020. }
  1021. static void ath9k_reclaim_beacon(struct ath_softc *sc,
  1022. struct ieee80211_vif *vif)
  1023. {
  1024. struct ath_vif *avp = (void *)vif->drv_priv;
  1025. ath9k_set_beaconing_status(sc, false);
  1026. ath_beacon_return(sc, avp);
  1027. ath9k_set_beaconing_status(sc, true);
  1028. sc->sc_flags &= ~SC_OP_BEACONS;
  1029. }
  1030. static void ath9k_vif_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
  1031. {
  1032. struct ath9k_vif_iter_data *iter_data = data;
  1033. int i;
  1034. if (iter_data->hw_macaddr)
  1035. for (i = 0; i < ETH_ALEN; i++)
  1036. iter_data->mask[i] &=
  1037. ~(iter_data->hw_macaddr[i] ^ mac[i]);
  1038. switch (vif->type) {
  1039. case NL80211_IFTYPE_AP:
  1040. iter_data->naps++;
  1041. break;
  1042. case NL80211_IFTYPE_STATION:
  1043. iter_data->nstations++;
  1044. break;
  1045. case NL80211_IFTYPE_ADHOC:
  1046. iter_data->nadhocs++;
  1047. break;
  1048. case NL80211_IFTYPE_MESH_POINT:
  1049. iter_data->nmeshes++;
  1050. break;
  1051. case NL80211_IFTYPE_WDS:
  1052. iter_data->nwds++;
  1053. break;
  1054. default:
  1055. iter_data->nothers++;
  1056. break;
  1057. }
  1058. }
  1059. /* Called with sc->mutex held. */
  1060. void ath9k_calculate_iter_data(struct ieee80211_hw *hw,
  1061. struct ieee80211_vif *vif,
  1062. struct ath9k_vif_iter_data *iter_data)
  1063. {
  1064. struct ath_softc *sc = hw->priv;
  1065. struct ath_hw *ah = sc->sc_ah;
  1066. struct ath_common *common = ath9k_hw_common(ah);
  1067. /*
  1068. * Use the hardware MAC address as reference, the hardware uses it
  1069. * together with the BSSID mask when matching addresses.
  1070. */
  1071. memset(iter_data, 0, sizeof(*iter_data));
  1072. iter_data->hw_macaddr = common->macaddr;
  1073. memset(&iter_data->mask, 0xff, ETH_ALEN);
  1074. if (vif)
  1075. ath9k_vif_iter(iter_data, vif->addr, vif);
  1076. /* Get list of all active MAC addresses */
  1077. ieee80211_iterate_active_interfaces_atomic(sc->hw, ath9k_vif_iter,
  1078. iter_data);
  1079. }
  1080. /* Called with sc->mutex held. */
  1081. static void ath9k_calculate_summary_state(struct ieee80211_hw *hw,
  1082. struct ieee80211_vif *vif)
  1083. {
  1084. struct ath_softc *sc = hw->priv;
  1085. struct ath_hw *ah = sc->sc_ah;
  1086. struct ath_common *common = ath9k_hw_common(ah);
  1087. struct ath9k_vif_iter_data iter_data;
  1088. ath9k_calculate_iter_data(hw, vif, &iter_data);
  1089. /* Set BSSID mask. */
  1090. memcpy(common->bssidmask, iter_data.mask, ETH_ALEN);
  1091. ath_hw_setbssidmask(common);
  1092. /* Set op-mode & TSF */
  1093. if (iter_data.naps > 0) {
  1094. ath9k_hw_set_tsfadjust(ah, 1);
  1095. sc->sc_flags |= SC_OP_TSF_RESET;
  1096. ah->opmode = NL80211_IFTYPE_AP;
  1097. } else {
  1098. ath9k_hw_set_tsfadjust(ah, 0);
  1099. sc->sc_flags &= ~SC_OP_TSF_RESET;
  1100. if (iter_data.nwds + iter_data.nmeshes)
  1101. ah->opmode = NL80211_IFTYPE_AP;
  1102. else if (iter_data.nadhocs)
  1103. ah->opmode = NL80211_IFTYPE_ADHOC;
  1104. else
  1105. ah->opmode = NL80211_IFTYPE_STATION;
  1106. }
  1107. /*
  1108. * Enable MIB interrupts when there are hardware phy counters.
  1109. */
  1110. if ((iter_data.nstations + iter_data.nadhocs + iter_data.nmeshes) > 0) {
  1111. if (ah->config.enable_ani)
  1112. ah->imask |= ATH9K_INT_MIB;
  1113. ah->imask |= ATH9K_INT_TSFOOR;
  1114. } else {
  1115. ah->imask &= ~ATH9K_INT_MIB;
  1116. ah->imask &= ~ATH9K_INT_TSFOOR;
  1117. }
  1118. ath9k_hw_set_interrupts(ah, ah->imask);
  1119. /* Set up ANI */
  1120. if ((iter_data.naps + iter_data.nadhocs) > 0) {
  1121. sc->sc_flags |= SC_OP_ANI_RUN;
  1122. ath_start_ani(common);
  1123. }
  1124. }
  1125. /* Called with sc->mutex held, vif counts set up properly. */
  1126. static void ath9k_do_vif_add_setup(struct ieee80211_hw *hw,
  1127. struct ieee80211_vif *vif)
  1128. {
  1129. struct ath_softc *sc = hw->priv;
  1130. ath9k_calculate_summary_state(hw, vif);
  1131. if (ath9k_uses_beacons(vif->type)) {
  1132. int error;
  1133. /* This may fail because upper levels do not have beacons
  1134. * properly configured yet. That's OK, we assume it
  1135. * will be properly configured and then we will be notified
  1136. * in the info_changed method and set up beacons properly
  1137. * there.
  1138. */
  1139. ath9k_set_beaconing_status(sc, false);
  1140. error = ath_beacon_alloc(sc, vif);
  1141. if (!error)
  1142. ath_beacon_config(sc, vif);
  1143. ath9k_set_beaconing_status(sc, true);
  1144. }
  1145. }
  1146. static int ath9k_add_interface(struct ieee80211_hw *hw,
  1147. struct ieee80211_vif *vif)
  1148. {
  1149. struct ath_softc *sc = hw->priv;
  1150. struct ath_hw *ah = sc->sc_ah;
  1151. struct ath_common *common = ath9k_hw_common(ah);
  1152. int ret = 0;
  1153. ath9k_ps_wakeup(sc);
  1154. mutex_lock(&sc->mutex);
  1155. switch (vif->type) {
  1156. case NL80211_IFTYPE_STATION:
  1157. case NL80211_IFTYPE_WDS:
  1158. case NL80211_IFTYPE_ADHOC:
  1159. case NL80211_IFTYPE_AP:
  1160. case NL80211_IFTYPE_MESH_POINT:
  1161. break;
  1162. default:
  1163. ath_err(common, "Interface type %d not yet supported\n",
  1164. vif->type);
  1165. ret = -EOPNOTSUPP;
  1166. goto out;
  1167. }
  1168. if (ath9k_uses_beacons(vif->type)) {
  1169. if (sc->nbcnvifs >= ATH_BCBUF) {
  1170. ath_err(common, "Not enough beacon buffers when adding"
  1171. " new interface of type: %i\n",
  1172. vif->type);
  1173. ret = -ENOBUFS;
  1174. goto out;
  1175. }
  1176. }
  1177. if ((ah->opmode == NL80211_IFTYPE_ADHOC) ||
  1178. ((vif->type == NL80211_IFTYPE_ADHOC) &&
  1179. sc->nvifs > 0)) {
  1180. ath_err(common, "Cannot create ADHOC interface when other"
  1181. " interfaces already exist.\n");
  1182. ret = -EINVAL;
  1183. goto out;
  1184. }
  1185. ath_dbg(common, ATH_DBG_CONFIG,
  1186. "Attach a VIF of type: %d\n", vif->type);
  1187. sc->nvifs++;
  1188. ath9k_do_vif_add_setup(hw, vif);
  1189. out:
  1190. mutex_unlock(&sc->mutex);
  1191. ath9k_ps_restore(sc);
  1192. return ret;
  1193. }
  1194. static int ath9k_change_interface(struct ieee80211_hw *hw,
  1195. struct ieee80211_vif *vif,
  1196. enum nl80211_iftype new_type,
  1197. bool p2p)
  1198. {
  1199. struct ath_softc *sc = hw->priv;
  1200. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  1201. int ret = 0;
  1202. ath_dbg(common, ATH_DBG_CONFIG, "Change Interface\n");
  1203. mutex_lock(&sc->mutex);
  1204. ath9k_ps_wakeup(sc);
  1205. /* See if new interface type is valid. */
  1206. if ((new_type == NL80211_IFTYPE_ADHOC) &&
  1207. (sc->nvifs > 1)) {
  1208. ath_err(common, "When using ADHOC, it must be the only"
  1209. " interface.\n");
  1210. ret = -EINVAL;
  1211. goto out;
  1212. }
  1213. if (ath9k_uses_beacons(new_type) &&
  1214. !ath9k_uses_beacons(vif->type)) {
  1215. if (sc->nbcnvifs >= ATH_BCBUF) {
  1216. ath_err(common, "No beacon slot available\n");
  1217. ret = -ENOBUFS;
  1218. goto out;
  1219. }
  1220. }
  1221. /* Clean up old vif stuff */
  1222. if (ath9k_uses_beacons(vif->type))
  1223. ath9k_reclaim_beacon(sc, vif);
  1224. /* Add new settings */
  1225. vif->type = new_type;
  1226. vif->p2p = p2p;
  1227. ath9k_do_vif_add_setup(hw, vif);
  1228. out:
  1229. ath9k_ps_restore(sc);
  1230. mutex_unlock(&sc->mutex);
  1231. return ret;
  1232. }
  1233. static void ath9k_remove_interface(struct ieee80211_hw *hw,
  1234. struct ieee80211_vif *vif)
  1235. {
  1236. struct ath_softc *sc = hw->priv;
  1237. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  1238. ath_dbg(common, ATH_DBG_CONFIG, "Detach Interface\n");
  1239. ath9k_ps_wakeup(sc);
  1240. mutex_lock(&sc->mutex);
  1241. sc->nvifs--;
  1242. /* Reclaim beacon resources */
  1243. if (ath9k_uses_beacons(vif->type))
  1244. ath9k_reclaim_beacon(sc, vif);
  1245. ath9k_calculate_summary_state(hw, NULL);
  1246. mutex_unlock(&sc->mutex);
  1247. ath9k_ps_restore(sc);
  1248. }
  1249. static void ath9k_enable_ps(struct ath_softc *sc)
  1250. {
  1251. struct ath_hw *ah = sc->sc_ah;
  1252. sc->ps_enabled = true;
  1253. if (!(ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP)) {
  1254. if ((ah->imask & ATH9K_INT_TIM_TIMER) == 0) {
  1255. ah->imask |= ATH9K_INT_TIM_TIMER;
  1256. ath9k_hw_set_interrupts(ah, ah->imask);
  1257. }
  1258. ath9k_hw_setrxabort(ah, 1);
  1259. }
  1260. }
  1261. static void ath9k_disable_ps(struct ath_softc *sc)
  1262. {
  1263. struct ath_hw *ah = sc->sc_ah;
  1264. sc->ps_enabled = false;
  1265. ath9k_hw_setpower(ah, ATH9K_PM_AWAKE);
  1266. if (!(ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP)) {
  1267. ath9k_hw_setrxabort(ah, 0);
  1268. sc->ps_flags &= ~(PS_WAIT_FOR_BEACON |
  1269. PS_WAIT_FOR_CAB |
  1270. PS_WAIT_FOR_PSPOLL_DATA |
  1271. PS_WAIT_FOR_TX_ACK);
  1272. if (ah->imask & ATH9K_INT_TIM_TIMER) {
  1273. ah->imask &= ~ATH9K_INT_TIM_TIMER;
  1274. ath9k_hw_set_interrupts(ah, ah->imask);
  1275. }
  1276. }
  1277. }
  1278. static int ath9k_config(struct ieee80211_hw *hw, u32 changed)
  1279. {
  1280. struct ath_softc *sc = hw->priv;
  1281. struct ath_hw *ah = sc->sc_ah;
  1282. struct ath_common *common = ath9k_hw_common(ah);
  1283. struct ieee80211_conf *conf = &hw->conf;
  1284. bool disable_radio = false;
  1285. mutex_lock(&sc->mutex);
  1286. /*
  1287. * Leave this as the first check because we need to turn on the
  1288. * radio if it was disabled before prior to processing the rest
  1289. * of the changes. Likewise we must only disable the radio towards
  1290. * the end.
  1291. */
  1292. if (changed & IEEE80211_CONF_CHANGE_IDLE) {
  1293. sc->ps_idle = !!(conf->flags & IEEE80211_CONF_IDLE);
  1294. if (!sc->ps_idle) {
  1295. ath_radio_enable(sc, hw);
  1296. ath_dbg(common, ATH_DBG_CONFIG,
  1297. "not-idle: enabling radio\n");
  1298. } else {
  1299. disable_radio = true;
  1300. }
  1301. }
  1302. /*
  1303. * We just prepare to enable PS. We have to wait until our AP has
  1304. * ACK'd our null data frame to disable RX otherwise we'll ignore
  1305. * those ACKs and end up retransmitting the same null data frames.
  1306. * IEEE80211_CONF_CHANGE_PS is only passed by mac80211 for STA mode.
  1307. */
  1308. if (changed & IEEE80211_CONF_CHANGE_PS) {
  1309. unsigned long flags;
  1310. spin_lock_irqsave(&sc->sc_pm_lock, flags);
  1311. if (conf->flags & IEEE80211_CONF_PS)
  1312. ath9k_enable_ps(sc);
  1313. else
  1314. ath9k_disable_ps(sc);
  1315. spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
  1316. }
  1317. if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
  1318. if (conf->flags & IEEE80211_CONF_MONITOR) {
  1319. ath_dbg(common, ATH_DBG_CONFIG,
  1320. "Monitor mode is enabled\n");
  1321. sc->sc_ah->is_monitoring = true;
  1322. } else {
  1323. ath_dbg(common, ATH_DBG_CONFIG,
  1324. "Monitor mode is disabled\n");
  1325. sc->sc_ah->is_monitoring = false;
  1326. }
  1327. }
  1328. if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
  1329. struct ieee80211_channel *curchan = hw->conf.channel;
  1330. int pos = curchan->hw_value;
  1331. int old_pos = -1;
  1332. unsigned long flags;
  1333. if (ah->curchan)
  1334. old_pos = ah->curchan - &ah->channels[0];
  1335. if (hw->conf.flags & IEEE80211_CONF_OFFCHANNEL)
  1336. sc->sc_flags |= SC_OP_OFFCHANNEL;
  1337. else
  1338. sc->sc_flags &= ~SC_OP_OFFCHANNEL;
  1339. ath_dbg(common, ATH_DBG_CONFIG,
  1340. "Set channel: %d MHz type: %d\n",
  1341. curchan->center_freq, conf->channel_type);
  1342. ath9k_cmn_update_ichannel(&sc->sc_ah->channels[pos],
  1343. curchan, conf->channel_type);
  1344. /* update survey stats for the old channel before switching */
  1345. spin_lock_irqsave(&common->cc_lock, flags);
  1346. ath_update_survey_stats(sc);
  1347. spin_unlock_irqrestore(&common->cc_lock, flags);
  1348. /*
  1349. * If the operating channel changes, change the survey in-use flags
  1350. * along with it.
  1351. * Reset the survey data for the new channel, unless we're switching
  1352. * back to the operating channel from an off-channel operation.
  1353. */
  1354. if (!(hw->conf.flags & IEEE80211_CONF_OFFCHANNEL) &&
  1355. sc->cur_survey != &sc->survey[pos]) {
  1356. if (sc->cur_survey)
  1357. sc->cur_survey->filled &= ~SURVEY_INFO_IN_USE;
  1358. sc->cur_survey = &sc->survey[pos];
  1359. memset(sc->cur_survey, 0, sizeof(struct survey_info));
  1360. sc->cur_survey->filled |= SURVEY_INFO_IN_USE;
  1361. } else if (!(sc->survey[pos].filled & SURVEY_INFO_IN_USE)) {
  1362. memset(&sc->survey[pos], 0, sizeof(struct survey_info));
  1363. }
  1364. if (ath_set_channel(sc, hw, &sc->sc_ah->channels[pos]) < 0) {
  1365. ath_err(common, "Unable to set channel\n");
  1366. mutex_unlock(&sc->mutex);
  1367. return -EINVAL;
  1368. }
  1369. /*
  1370. * The most recent snapshot of channel->noisefloor for the old
  1371. * channel is only available after the hardware reset. Copy it to
  1372. * the survey stats now.
  1373. */
  1374. if (old_pos >= 0)
  1375. ath_update_survey_nf(sc, old_pos);
  1376. }
  1377. if (changed & IEEE80211_CONF_CHANGE_POWER) {
  1378. ath_dbg(common, ATH_DBG_CONFIG,
  1379. "Set power: %d\n", conf->power_level);
  1380. sc->config.txpowlimit = 2 * conf->power_level;
  1381. ath9k_ps_wakeup(sc);
  1382. ath9k_cmn_update_txpow(ah, sc->curtxpow,
  1383. sc->config.txpowlimit, &sc->curtxpow);
  1384. ath9k_ps_restore(sc);
  1385. }
  1386. if (disable_radio) {
  1387. ath_dbg(common, ATH_DBG_CONFIG, "idle: disabling radio\n");
  1388. ath_radio_disable(sc, hw);
  1389. }
  1390. mutex_unlock(&sc->mutex);
  1391. return 0;
  1392. }
  1393. #define SUPPORTED_FILTERS \
  1394. (FIF_PROMISC_IN_BSS | \
  1395. FIF_ALLMULTI | \
  1396. FIF_CONTROL | \
  1397. FIF_PSPOLL | \
  1398. FIF_OTHER_BSS | \
  1399. FIF_BCN_PRBRESP_PROMISC | \
  1400. FIF_PROBE_REQ | \
  1401. FIF_FCSFAIL)
  1402. /* FIXME: sc->sc_full_reset ? */
  1403. static void ath9k_configure_filter(struct ieee80211_hw *hw,
  1404. unsigned int changed_flags,
  1405. unsigned int *total_flags,
  1406. u64 multicast)
  1407. {
  1408. struct ath_softc *sc = hw->priv;
  1409. u32 rfilt;
  1410. changed_flags &= SUPPORTED_FILTERS;
  1411. *total_flags &= SUPPORTED_FILTERS;
  1412. sc->rx.rxfilter = *total_flags;
  1413. ath9k_ps_wakeup(sc);
  1414. rfilt = ath_calcrxfilter(sc);
  1415. ath9k_hw_setrxfilter(sc->sc_ah, rfilt);
  1416. ath9k_ps_restore(sc);
  1417. ath_dbg(ath9k_hw_common(sc->sc_ah), ATH_DBG_CONFIG,
  1418. "Set HW RX filter: 0x%x\n", rfilt);
  1419. }
  1420. static int ath9k_sta_add(struct ieee80211_hw *hw,
  1421. struct ieee80211_vif *vif,
  1422. struct ieee80211_sta *sta)
  1423. {
  1424. struct ath_softc *sc = hw->priv;
  1425. ath_node_attach(sc, sta);
  1426. return 0;
  1427. }
  1428. static int ath9k_sta_remove(struct ieee80211_hw *hw,
  1429. struct ieee80211_vif *vif,
  1430. struct ieee80211_sta *sta)
  1431. {
  1432. struct ath_softc *sc = hw->priv;
  1433. ath_node_detach(sc, sta);
  1434. return 0;
  1435. }
  1436. static int ath9k_conf_tx(struct ieee80211_hw *hw, u16 queue,
  1437. const struct ieee80211_tx_queue_params *params)
  1438. {
  1439. struct ath_softc *sc = hw->priv;
  1440. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  1441. struct ath_txq *txq;
  1442. struct ath9k_tx_queue_info qi;
  1443. int ret = 0;
  1444. if (queue >= WME_NUM_AC)
  1445. return 0;
  1446. txq = sc->tx.txq_map[queue];
  1447. ath9k_ps_wakeup(sc);
  1448. mutex_lock(&sc->mutex);
  1449. memset(&qi, 0, sizeof(struct ath9k_tx_queue_info));
  1450. qi.tqi_aifs = params->aifs;
  1451. qi.tqi_cwmin = params->cw_min;
  1452. qi.tqi_cwmax = params->cw_max;
  1453. qi.tqi_burstTime = params->txop;
  1454. ath_dbg(common, ATH_DBG_CONFIG,
  1455. "Configure tx [queue/halq] [%d/%d], aifs: %d, cw_min: %d, cw_max: %d, txop: %d\n",
  1456. queue, txq->axq_qnum, params->aifs, params->cw_min,
  1457. params->cw_max, params->txop);
  1458. ret = ath_txq_update(sc, txq->axq_qnum, &qi);
  1459. if (ret)
  1460. ath_err(common, "TXQ Update failed\n");
  1461. if (sc->sc_ah->opmode == NL80211_IFTYPE_ADHOC)
  1462. if (queue == WME_AC_BE && !ret)
  1463. ath_beaconq_config(sc);
  1464. mutex_unlock(&sc->mutex);
  1465. ath9k_ps_restore(sc);
  1466. return ret;
  1467. }
  1468. static int ath9k_set_key(struct ieee80211_hw *hw,
  1469. enum set_key_cmd cmd,
  1470. struct ieee80211_vif *vif,
  1471. struct ieee80211_sta *sta,
  1472. struct ieee80211_key_conf *key)
  1473. {
  1474. struct ath_softc *sc = hw->priv;
  1475. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  1476. int ret = 0;
  1477. if (ath9k_modparam_nohwcrypt)
  1478. return -ENOSPC;
  1479. if (vif->type == NL80211_IFTYPE_ADHOC &&
  1480. (key->cipher == WLAN_CIPHER_SUITE_TKIP ||
  1481. key->cipher == WLAN_CIPHER_SUITE_CCMP) &&
  1482. !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
  1483. /*
  1484. * For now, disable hw crypto for the RSN IBSS group keys. This
  1485. * could be optimized in the future to use a modified key cache
  1486. * design to support per-STA RX GTK, but until that gets
  1487. * implemented, use of software crypto for group addressed
  1488. * frames is a acceptable to allow RSN IBSS to be used.
  1489. */
  1490. return -EOPNOTSUPP;
  1491. }
  1492. mutex_lock(&sc->mutex);
  1493. ath9k_ps_wakeup(sc);
  1494. ath_dbg(common, ATH_DBG_CONFIG, "Set HW Key\n");
  1495. switch (cmd) {
  1496. case SET_KEY:
  1497. ret = ath_key_config(common, vif, sta, key);
  1498. if (ret >= 0) {
  1499. key->hw_key_idx = ret;
  1500. /* push IV and Michael MIC generation to stack */
  1501. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  1502. if (key->cipher == WLAN_CIPHER_SUITE_TKIP)
  1503. key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
  1504. if (sc->sc_ah->sw_mgmt_crypto &&
  1505. key->cipher == WLAN_CIPHER_SUITE_CCMP)
  1506. key->flags |= IEEE80211_KEY_FLAG_SW_MGMT;
  1507. ret = 0;
  1508. }
  1509. break;
  1510. case DISABLE_KEY:
  1511. ath_key_delete(common, key);
  1512. break;
  1513. default:
  1514. ret = -EINVAL;
  1515. }
  1516. ath9k_ps_restore(sc);
  1517. mutex_unlock(&sc->mutex);
  1518. return ret;
  1519. }
  1520. static void ath9k_bss_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
  1521. {
  1522. struct ath_softc *sc = data;
  1523. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  1524. struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
  1525. struct ath_vif *avp = (void *)vif->drv_priv;
  1526. switch (sc->sc_ah->opmode) {
  1527. case NL80211_IFTYPE_ADHOC:
  1528. /* There can be only one vif available */
  1529. memcpy(common->curbssid, bss_conf->bssid, ETH_ALEN);
  1530. common->curaid = bss_conf->aid;
  1531. ath9k_hw_write_associd(sc->sc_ah);
  1532. /* configure beacon */
  1533. if (bss_conf->enable_beacon)
  1534. ath_beacon_config(sc, vif);
  1535. break;
  1536. case NL80211_IFTYPE_STATION:
  1537. /*
  1538. * Skip iteration if primary station vif's bss info
  1539. * was not changed
  1540. */
  1541. if (sc->sc_flags & SC_OP_PRIM_STA_VIF)
  1542. break;
  1543. if (bss_conf->assoc) {
  1544. sc->sc_flags |= SC_OP_PRIM_STA_VIF;
  1545. avp->primary_sta_vif = true;
  1546. memcpy(common->curbssid, bss_conf->bssid, ETH_ALEN);
  1547. common->curaid = bss_conf->aid;
  1548. ath9k_hw_write_associd(sc->sc_ah);
  1549. ath_dbg(common, ATH_DBG_CONFIG,
  1550. "Bss Info ASSOC %d, bssid: %pM\n",
  1551. bss_conf->aid, common->curbssid);
  1552. ath_beacon_config(sc, vif);
  1553. /* Reset rssi stats */
  1554. sc->last_rssi = ATH_RSSI_DUMMY_MARKER;
  1555. sc->sc_ah->stats.avgbrssi = ATH_RSSI_DUMMY_MARKER;
  1556. sc->sc_flags |= SC_OP_ANI_RUN;
  1557. ath_start_ani(common);
  1558. }
  1559. break;
  1560. default:
  1561. break;
  1562. }
  1563. }
  1564. static void ath9k_config_bss(struct ath_softc *sc, struct ieee80211_vif *vif)
  1565. {
  1566. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  1567. struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
  1568. struct ath_vif *avp = (void *)vif->drv_priv;
  1569. /* Reconfigure bss info */
  1570. if (avp->primary_sta_vif && !bss_conf->assoc) {
  1571. ath_dbg(common, ATH_DBG_CONFIG,
  1572. "Bss Info DISASSOC %d, bssid %pM\n",
  1573. common->curaid, common->curbssid);
  1574. sc->sc_flags &= ~(SC_OP_PRIM_STA_VIF | SC_OP_BEACONS);
  1575. avp->primary_sta_vif = false;
  1576. memset(common->curbssid, 0, ETH_ALEN);
  1577. common->curaid = 0;
  1578. }
  1579. ieee80211_iterate_active_interfaces_atomic(
  1580. sc->hw, ath9k_bss_iter, sc);
  1581. /*
  1582. * None of station vifs are associated.
  1583. * Clear bssid & aid
  1584. */
  1585. if ((sc->sc_ah->opmode == NL80211_IFTYPE_STATION) &&
  1586. !(sc->sc_flags & SC_OP_PRIM_STA_VIF)) {
  1587. ath9k_hw_write_associd(sc->sc_ah);
  1588. /* Stop ANI */
  1589. sc->sc_flags &= ~SC_OP_ANI_RUN;
  1590. del_timer_sync(&common->ani.timer);
  1591. }
  1592. }
  1593. static void ath9k_bss_info_changed(struct ieee80211_hw *hw,
  1594. struct ieee80211_vif *vif,
  1595. struct ieee80211_bss_conf *bss_conf,
  1596. u32 changed)
  1597. {
  1598. struct ath_softc *sc = hw->priv;
  1599. struct ath_hw *ah = sc->sc_ah;
  1600. struct ath_common *common = ath9k_hw_common(ah);
  1601. struct ath_vif *avp = (void *)vif->drv_priv;
  1602. int slottime;
  1603. int error;
  1604. ath9k_ps_wakeup(sc);
  1605. mutex_lock(&sc->mutex);
  1606. if (changed & BSS_CHANGED_BSSID) {
  1607. ath9k_config_bss(sc, vif);
  1608. /* Set aggregation protection mode parameters */
  1609. sc->config.ath_aggr_prot = 0;
  1610. ath_dbg(common, ATH_DBG_CONFIG, "BSSID: %pM aid: 0x%x\n",
  1611. common->curbssid, common->curaid);
  1612. }
  1613. /* Enable transmission of beacons (AP, IBSS, MESH) */
  1614. if ((changed & BSS_CHANGED_BEACON) ||
  1615. ((changed & BSS_CHANGED_BEACON_ENABLED) && bss_conf->enable_beacon)) {
  1616. ath9k_set_beaconing_status(sc, false);
  1617. error = ath_beacon_alloc(sc, vif);
  1618. if (!error)
  1619. ath_beacon_config(sc, vif);
  1620. ath9k_set_beaconing_status(sc, true);
  1621. }
  1622. if (changed & BSS_CHANGED_ERP_SLOT) {
  1623. if (bss_conf->use_short_slot)
  1624. slottime = 9;
  1625. else
  1626. slottime = 20;
  1627. if (vif->type == NL80211_IFTYPE_AP) {
  1628. /*
  1629. * Defer update, so that connected stations can adjust
  1630. * their settings at the same time.
  1631. * See beacon.c for more details
  1632. */
  1633. sc->beacon.slottime = slottime;
  1634. sc->beacon.updateslot = UPDATE;
  1635. } else {
  1636. ah->slottime = slottime;
  1637. ath9k_hw_init_global_settings(ah);
  1638. }
  1639. }
  1640. /* Disable transmission of beacons */
  1641. if ((changed & BSS_CHANGED_BEACON_ENABLED) &&
  1642. !bss_conf->enable_beacon) {
  1643. ath9k_set_beaconing_status(sc, false);
  1644. avp->is_bslot_active = false;
  1645. ath9k_set_beaconing_status(sc, true);
  1646. }
  1647. if (changed & BSS_CHANGED_BEACON_INT) {
  1648. /*
  1649. * In case of AP mode, the HW TSF has to be reset
  1650. * when the beacon interval changes.
  1651. */
  1652. if (vif->type == NL80211_IFTYPE_AP) {
  1653. sc->sc_flags |= SC_OP_TSF_RESET;
  1654. ath9k_set_beaconing_status(sc, false);
  1655. error = ath_beacon_alloc(sc, vif);
  1656. if (!error)
  1657. ath_beacon_config(sc, vif);
  1658. ath9k_set_beaconing_status(sc, true);
  1659. } else
  1660. ath_beacon_config(sc, vif);
  1661. }
  1662. if (changed & BSS_CHANGED_ERP_PREAMBLE) {
  1663. ath_dbg(common, ATH_DBG_CONFIG, "BSS Changed PREAMBLE %d\n",
  1664. bss_conf->use_short_preamble);
  1665. if (bss_conf->use_short_preamble)
  1666. sc->sc_flags |= SC_OP_PREAMBLE_SHORT;
  1667. else
  1668. sc->sc_flags &= ~SC_OP_PREAMBLE_SHORT;
  1669. }
  1670. if (changed & BSS_CHANGED_ERP_CTS_PROT) {
  1671. ath_dbg(common, ATH_DBG_CONFIG, "BSS Changed CTS PROT %d\n",
  1672. bss_conf->use_cts_prot);
  1673. if (bss_conf->use_cts_prot &&
  1674. hw->conf.channel->band != IEEE80211_BAND_5GHZ)
  1675. sc->sc_flags |= SC_OP_PROTECT_ENABLE;
  1676. else
  1677. sc->sc_flags &= ~SC_OP_PROTECT_ENABLE;
  1678. }
  1679. mutex_unlock(&sc->mutex);
  1680. ath9k_ps_restore(sc);
  1681. }
  1682. static u64 ath9k_get_tsf(struct ieee80211_hw *hw)
  1683. {
  1684. struct ath_softc *sc = hw->priv;
  1685. u64 tsf;
  1686. mutex_lock(&sc->mutex);
  1687. ath9k_ps_wakeup(sc);
  1688. tsf = ath9k_hw_gettsf64(sc->sc_ah);
  1689. ath9k_ps_restore(sc);
  1690. mutex_unlock(&sc->mutex);
  1691. return tsf;
  1692. }
  1693. static void ath9k_set_tsf(struct ieee80211_hw *hw, u64 tsf)
  1694. {
  1695. struct ath_softc *sc = hw->priv;
  1696. mutex_lock(&sc->mutex);
  1697. ath9k_ps_wakeup(sc);
  1698. ath9k_hw_settsf64(sc->sc_ah, tsf);
  1699. ath9k_ps_restore(sc);
  1700. mutex_unlock(&sc->mutex);
  1701. }
  1702. static void ath9k_reset_tsf(struct ieee80211_hw *hw)
  1703. {
  1704. struct ath_softc *sc = hw->priv;
  1705. mutex_lock(&sc->mutex);
  1706. ath9k_ps_wakeup(sc);
  1707. ath9k_hw_reset_tsf(sc->sc_ah);
  1708. ath9k_ps_restore(sc);
  1709. mutex_unlock(&sc->mutex);
  1710. }
  1711. static int ath9k_ampdu_action(struct ieee80211_hw *hw,
  1712. struct ieee80211_vif *vif,
  1713. enum ieee80211_ampdu_mlme_action action,
  1714. struct ieee80211_sta *sta,
  1715. u16 tid, u16 *ssn, u8 buf_size)
  1716. {
  1717. struct ath_softc *sc = hw->priv;
  1718. int ret = 0;
  1719. local_bh_disable();
  1720. switch (action) {
  1721. case IEEE80211_AMPDU_RX_START:
  1722. if (!(sc->sc_flags & SC_OP_RXAGGR))
  1723. ret = -ENOTSUPP;
  1724. break;
  1725. case IEEE80211_AMPDU_RX_STOP:
  1726. break;
  1727. case IEEE80211_AMPDU_TX_START:
  1728. if (!(sc->sc_flags & SC_OP_TXAGGR))
  1729. return -EOPNOTSUPP;
  1730. ath9k_ps_wakeup(sc);
  1731. ret = ath_tx_aggr_start(sc, sta, tid, ssn);
  1732. if (!ret)
  1733. ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  1734. ath9k_ps_restore(sc);
  1735. break;
  1736. case IEEE80211_AMPDU_TX_STOP:
  1737. ath9k_ps_wakeup(sc);
  1738. ath_tx_aggr_stop(sc, sta, tid);
  1739. ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  1740. ath9k_ps_restore(sc);
  1741. break;
  1742. case IEEE80211_AMPDU_TX_OPERATIONAL:
  1743. ath9k_ps_wakeup(sc);
  1744. ath_tx_aggr_resume(sc, sta, tid);
  1745. ath9k_ps_restore(sc);
  1746. break;
  1747. default:
  1748. ath_err(ath9k_hw_common(sc->sc_ah), "Unknown AMPDU action\n");
  1749. }
  1750. local_bh_enable();
  1751. return ret;
  1752. }
  1753. static int ath9k_get_survey(struct ieee80211_hw *hw, int idx,
  1754. struct survey_info *survey)
  1755. {
  1756. struct ath_softc *sc = hw->priv;
  1757. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  1758. struct ieee80211_supported_band *sband;
  1759. struct ieee80211_channel *chan;
  1760. unsigned long flags;
  1761. int pos;
  1762. spin_lock_irqsave(&common->cc_lock, flags);
  1763. if (idx == 0)
  1764. ath_update_survey_stats(sc);
  1765. sband = hw->wiphy->bands[IEEE80211_BAND_2GHZ];
  1766. if (sband && idx >= sband->n_channels) {
  1767. idx -= sband->n_channels;
  1768. sband = NULL;
  1769. }
  1770. if (!sband)
  1771. sband = hw->wiphy->bands[IEEE80211_BAND_5GHZ];
  1772. if (!sband || idx >= sband->n_channels) {
  1773. spin_unlock_irqrestore(&common->cc_lock, flags);
  1774. return -ENOENT;
  1775. }
  1776. chan = &sband->channels[idx];
  1777. pos = chan->hw_value;
  1778. memcpy(survey, &sc->survey[pos], sizeof(*survey));
  1779. survey->channel = chan;
  1780. spin_unlock_irqrestore(&common->cc_lock, flags);
  1781. return 0;
  1782. }
  1783. static void ath9k_set_coverage_class(struct ieee80211_hw *hw, u8 coverage_class)
  1784. {
  1785. struct ath_softc *sc = hw->priv;
  1786. struct ath_hw *ah = sc->sc_ah;
  1787. mutex_lock(&sc->mutex);
  1788. ah->coverage_class = coverage_class;
  1789. ath9k_hw_init_global_settings(ah);
  1790. mutex_unlock(&sc->mutex);
  1791. }
  1792. static void ath9k_flush(struct ieee80211_hw *hw, bool drop)
  1793. {
  1794. struct ath_softc *sc = hw->priv;
  1795. int timeout = 200; /* ms */
  1796. int i, j;
  1797. ath9k_ps_wakeup(sc);
  1798. mutex_lock(&sc->mutex);
  1799. cancel_delayed_work_sync(&sc->tx_complete_work);
  1800. if (drop)
  1801. timeout = 1;
  1802. for (j = 0; j < timeout; j++) {
  1803. int npend = 0;
  1804. if (j)
  1805. usleep_range(1000, 2000);
  1806. for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
  1807. if (!ATH_TXQ_SETUP(sc, i))
  1808. continue;
  1809. npend += ath9k_has_pending_frames(sc, &sc->tx.txq[i]);
  1810. }
  1811. if (!npend)
  1812. goto out;
  1813. }
  1814. if (!ath_drain_all_txq(sc, false))
  1815. ath_reset(sc, false);
  1816. ieee80211_wake_queues(hw);
  1817. out:
  1818. ieee80211_queue_delayed_work(hw, &sc->tx_complete_work, 0);
  1819. mutex_unlock(&sc->mutex);
  1820. ath9k_ps_restore(sc);
  1821. }
  1822. struct ieee80211_ops ath9k_ops = {
  1823. .tx = ath9k_tx,
  1824. .start = ath9k_start,
  1825. .stop = ath9k_stop,
  1826. .add_interface = ath9k_add_interface,
  1827. .change_interface = ath9k_change_interface,
  1828. .remove_interface = ath9k_remove_interface,
  1829. .config = ath9k_config,
  1830. .configure_filter = ath9k_configure_filter,
  1831. .sta_add = ath9k_sta_add,
  1832. .sta_remove = ath9k_sta_remove,
  1833. .conf_tx = ath9k_conf_tx,
  1834. .bss_info_changed = ath9k_bss_info_changed,
  1835. .set_key = ath9k_set_key,
  1836. .get_tsf = ath9k_get_tsf,
  1837. .set_tsf = ath9k_set_tsf,
  1838. .reset_tsf = ath9k_reset_tsf,
  1839. .ampdu_action = ath9k_ampdu_action,
  1840. .get_survey = ath9k_get_survey,
  1841. .rfkill_poll = ath9k_rfkill_poll_state,
  1842. .set_coverage_class = ath9k_set_coverage_class,
  1843. .flush = ath9k_flush,
  1844. };