main.c 55 KB

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