main.c 52 KB

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