main.c 53 KB

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