main.c 55 KB

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