main.c 54 KB

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