link.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. /*
  2. * Copyright (c) 2012 Qualcomm Atheros, 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 "ath9k.h"
  17. /*
  18. * TX polling - checks if the TX engine is stuck somewhere
  19. * and issues a chip reset if so.
  20. */
  21. void ath_tx_complete_poll_work(struct work_struct *work)
  22. {
  23. struct ath_softc *sc = container_of(work, struct ath_softc,
  24. tx_complete_work.work);
  25. struct ath_txq *txq;
  26. int i;
  27. bool needreset = false;
  28. if (sc->tx99_state) {
  29. ath_dbg(ath9k_hw_common(sc->sc_ah), RESET,
  30. "skip tx hung detection on tx99\n");
  31. return;
  32. }
  33. for (i = 0; i < IEEE80211_NUM_ACS; i++) {
  34. txq = sc->tx.txq_map[i];
  35. ath_txq_lock(sc, txq);
  36. if (txq->axq_depth) {
  37. if (txq->axq_tx_inprogress) {
  38. needreset = true;
  39. ath_txq_unlock(sc, txq);
  40. break;
  41. } else {
  42. txq->axq_tx_inprogress = true;
  43. }
  44. }
  45. ath_txq_unlock(sc, txq);
  46. }
  47. if (needreset) {
  48. ath_dbg(ath9k_hw_common(sc->sc_ah), RESET,
  49. "tx hung, resetting the chip\n");
  50. ath9k_queue_reset(sc, RESET_TYPE_TX_HANG);
  51. return;
  52. }
  53. ieee80211_queue_delayed_work(sc->hw, &sc->tx_complete_work,
  54. msecs_to_jiffies(ATH_TX_COMPLETE_POLL_INT));
  55. }
  56. /*
  57. * Checks if the BB/MAC is hung.
  58. */
  59. bool ath_hw_check(struct ath_softc *sc)
  60. {
  61. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  62. enum ath_reset_type type;
  63. bool is_alive;
  64. ath9k_ps_wakeup(sc);
  65. is_alive = ath9k_hw_check_alive(sc->sc_ah);
  66. if (!is_alive) {
  67. ath_dbg(common, RESET,
  68. "HW hang detected, schedule chip reset\n");
  69. type = RESET_TYPE_MAC_HANG;
  70. ath9k_queue_reset(sc, type);
  71. }
  72. ath9k_ps_restore(sc);
  73. return is_alive;
  74. }
  75. /*
  76. * PLL-WAR for AR9485/AR9340
  77. */
  78. static bool ath_hw_pll_rx_hang_check(struct ath_softc *sc, u32 pll_sqsum)
  79. {
  80. static int count;
  81. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  82. if (pll_sqsum >= 0x40000) {
  83. count++;
  84. if (count == 3) {
  85. ath_dbg(common, RESET, "PLL WAR, resetting the chip\n");
  86. ath9k_queue_reset(sc, RESET_TYPE_PLL_HANG);
  87. count = 0;
  88. return true;
  89. }
  90. } else {
  91. count = 0;
  92. }
  93. return false;
  94. }
  95. void ath_hw_pll_work(struct work_struct *work)
  96. {
  97. u32 pll_sqsum;
  98. struct ath_softc *sc = container_of(work, struct ath_softc,
  99. hw_pll_work.work);
  100. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  101. /*
  102. * ensure that the PLL WAR is executed only
  103. * after the STA is associated (or) if the
  104. * beaconing had started in interfaces that
  105. * uses beacons.
  106. */
  107. if (!test_bit(ATH_OP_BEACONS, &common->op_flags))
  108. return;
  109. if (sc->tx99_state)
  110. return;
  111. ath9k_ps_wakeup(sc);
  112. pll_sqsum = ar9003_get_pll_sqsum_dvc(sc->sc_ah);
  113. ath9k_ps_restore(sc);
  114. if (ath_hw_pll_rx_hang_check(sc, pll_sqsum))
  115. return;
  116. ieee80211_queue_delayed_work(sc->hw, &sc->hw_pll_work,
  117. msecs_to_jiffies(ATH_PLL_WORK_INTERVAL));
  118. }
  119. /*
  120. * PA Pre-distortion.
  121. */
  122. static void ath_paprd_activate(struct ath_softc *sc)
  123. {
  124. struct ath_hw *ah = sc->sc_ah;
  125. struct ath_common *common = ath9k_hw_common(ah);
  126. struct ath9k_hw_cal_data *caldata = ah->caldata;
  127. int chain;
  128. if (!caldata || !test_bit(PAPRD_DONE, &caldata->cal_flags)) {
  129. ath_dbg(common, CALIBRATE, "Failed to activate PAPRD\n");
  130. return;
  131. }
  132. ar9003_paprd_enable(ah, false);
  133. for (chain = 0; chain < AR9300_MAX_CHAINS; chain++) {
  134. if (!(ah->txchainmask & BIT(chain)))
  135. continue;
  136. ar9003_paprd_populate_single_table(ah, caldata, chain);
  137. }
  138. ath_dbg(common, CALIBRATE, "Activating PAPRD\n");
  139. ar9003_paprd_enable(ah, true);
  140. }
  141. static bool ath_paprd_send_frame(struct ath_softc *sc, struct sk_buff *skb, int chain)
  142. {
  143. struct ieee80211_hw *hw = sc->hw;
  144. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
  145. struct ath_hw *ah = sc->sc_ah;
  146. struct ath_common *common = ath9k_hw_common(ah);
  147. struct ath_tx_control txctl;
  148. unsigned long time_left;
  149. memset(&txctl, 0, sizeof(txctl));
  150. txctl.txq = sc->tx.txq_map[IEEE80211_AC_BE];
  151. memset(tx_info, 0, sizeof(*tx_info));
  152. tx_info->band = sc->cur_chandef.chan->band;
  153. tx_info->flags |= IEEE80211_TX_CTL_NO_ACK;
  154. tx_info->control.rates[0].idx = 0;
  155. tx_info->control.rates[0].count = 1;
  156. tx_info->control.rates[0].flags = IEEE80211_TX_RC_MCS;
  157. tx_info->control.rates[1].idx = -1;
  158. init_completion(&sc->paprd_complete);
  159. txctl.paprd = BIT(chain);
  160. if (ath_tx_start(hw, skb, &txctl) != 0) {
  161. ath_dbg(common, CALIBRATE, "PAPRD TX failed\n");
  162. dev_kfree_skb_any(skb);
  163. return false;
  164. }
  165. time_left = wait_for_completion_timeout(&sc->paprd_complete,
  166. msecs_to_jiffies(ATH_PAPRD_TIMEOUT));
  167. if (!time_left)
  168. ath_dbg(common, CALIBRATE,
  169. "Timeout waiting for paprd training on TX chain %d\n",
  170. chain);
  171. return !!time_left;
  172. }
  173. void ath_paprd_calibrate(struct work_struct *work)
  174. {
  175. struct ath_softc *sc = container_of(work, struct ath_softc, paprd_work);
  176. struct ieee80211_hw *hw = sc->hw;
  177. struct ath_hw *ah = sc->sc_ah;
  178. struct ieee80211_hdr *hdr;
  179. struct sk_buff *skb = NULL;
  180. struct ath9k_hw_cal_data *caldata = ah->caldata;
  181. struct ath_common *common = ath9k_hw_common(ah);
  182. int ftype;
  183. int chain_ok = 0;
  184. int chain;
  185. int len = 1800;
  186. int ret;
  187. if (!caldata ||
  188. !test_bit(PAPRD_PACKET_SENT, &caldata->cal_flags) ||
  189. test_bit(PAPRD_DONE, &caldata->cal_flags)) {
  190. ath_dbg(common, CALIBRATE, "Skipping PAPRD calibration\n");
  191. return;
  192. }
  193. ath9k_ps_wakeup(sc);
  194. if (ar9003_paprd_init_table(ah) < 0)
  195. goto fail_paprd;
  196. skb = alloc_skb(len, GFP_KERNEL);
  197. if (!skb)
  198. goto fail_paprd;
  199. skb_put(skb, len);
  200. memset(skb->data, 0, len);
  201. hdr = (struct ieee80211_hdr *)skb->data;
  202. ftype = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC;
  203. hdr->frame_control = cpu_to_le16(ftype);
  204. hdr->duration_id = cpu_to_le16(10);
  205. memcpy(hdr->addr1, hw->wiphy->perm_addr, ETH_ALEN);
  206. memcpy(hdr->addr2, hw->wiphy->perm_addr, ETH_ALEN);
  207. memcpy(hdr->addr3, hw->wiphy->perm_addr, ETH_ALEN);
  208. for (chain = 0; chain < AR9300_MAX_CHAINS; chain++) {
  209. if (!(ah->txchainmask & BIT(chain)))
  210. continue;
  211. chain_ok = 0;
  212. ar9003_paprd_setup_gain_table(ah, chain);
  213. ath_dbg(common, CALIBRATE,
  214. "Sending PAPRD training frame on chain %d\n", chain);
  215. if (!ath_paprd_send_frame(sc, skb, chain))
  216. goto fail_paprd;
  217. if (!ar9003_paprd_is_done(ah)) {
  218. ath_dbg(common, CALIBRATE,
  219. "PAPRD not yet done on chain %d\n", chain);
  220. break;
  221. }
  222. ret = ar9003_paprd_create_curve(ah, caldata, chain);
  223. if (ret == -EINPROGRESS) {
  224. ath_dbg(common, CALIBRATE,
  225. "PAPRD curve on chain %d needs to be re-trained\n",
  226. chain);
  227. break;
  228. } else if (ret) {
  229. ath_dbg(common, CALIBRATE,
  230. "PAPRD create curve failed on chain %d\n",
  231. chain);
  232. break;
  233. }
  234. chain_ok = 1;
  235. }
  236. kfree_skb(skb);
  237. if (chain_ok) {
  238. set_bit(PAPRD_DONE, &caldata->cal_flags);
  239. ath_paprd_activate(sc);
  240. }
  241. fail_paprd:
  242. ath9k_ps_restore(sc);
  243. }
  244. /*
  245. * ANI performs periodic noise floor calibration
  246. * that is used to adjust and optimize the chip performance. This
  247. * takes environmental changes (location, temperature) into account.
  248. * When the task is complete, it reschedules itself depending on the
  249. * appropriate interval that was calculated.
  250. */
  251. void ath_ani_calibrate(unsigned long data)
  252. {
  253. struct ath_softc *sc = (struct ath_softc *)data;
  254. struct ath_hw *ah = sc->sc_ah;
  255. struct ath_common *common = ath9k_hw_common(ah);
  256. bool longcal = false;
  257. bool shortcal = false;
  258. bool aniflag = false;
  259. unsigned int timestamp = jiffies_to_msecs(jiffies);
  260. u32 cal_interval, short_cal_interval, long_cal_interval;
  261. unsigned long flags;
  262. if (ah->caldata && test_bit(NFCAL_INTF, &ah->caldata->cal_flags))
  263. long_cal_interval = ATH_LONG_CALINTERVAL_INT;
  264. else
  265. long_cal_interval = ATH_LONG_CALINTERVAL;
  266. short_cal_interval = (ah->opmode == NL80211_IFTYPE_AP) ?
  267. ATH_AP_SHORT_CALINTERVAL : ATH_STA_SHORT_CALINTERVAL;
  268. /* Only calibrate if awake */
  269. if (sc->sc_ah->power_mode != ATH9K_PM_AWAKE) {
  270. if (++ah->ani_skip_count >= ATH_ANI_MAX_SKIP_COUNT) {
  271. spin_lock_irqsave(&sc->sc_pm_lock, flags);
  272. sc->ps_flags |= PS_WAIT_FOR_ANI;
  273. spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
  274. }
  275. goto set_timer;
  276. }
  277. ah->ani_skip_count = 0;
  278. spin_lock_irqsave(&sc->sc_pm_lock, flags);
  279. sc->ps_flags &= ~PS_WAIT_FOR_ANI;
  280. spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
  281. ath9k_ps_wakeup(sc);
  282. /* Long calibration runs independently of short calibration. */
  283. if ((timestamp - common->ani.longcal_timer) >= long_cal_interval) {
  284. longcal = true;
  285. common->ani.longcal_timer = timestamp;
  286. }
  287. /* Short calibration applies only while caldone is false */
  288. if (!common->ani.caldone) {
  289. if ((timestamp - common->ani.shortcal_timer) >= short_cal_interval) {
  290. shortcal = true;
  291. common->ani.shortcal_timer = timestamp;
  292. common->ani.resetcal_timer = timestamp;
  293. }
  294. } else {
  295. if ((timestamp - common->ani.resetcal_timer) >=
  296. ATH_RESTART_CALINTERVAL) {
  297. common->ani.caldone = ath9k_hw_reset_calvalid(ah);
  298. if (common->ani.caldone)
  299. common->ani.resetcal_timer = timestamp;
  300. }
  301. }
  302. /* Verify whether we must check ANI */
  303. if ((timestamp - common->ani.checkani_timer) >= ah->config.ani_poll_interval) {
  304. aniflag = true;
  305. common->ani.checkani_timer = timestamp;
  306. }
  307. /* Call ANI routine if necessary */
  308. if (aniflag) {
  309. spin_lock(&common->cc_lock);
  310. ath9k_hw_ani_monitor(ah, ah->curchan);
  311. ath_update_survey_stats(sc);
  312. spin_unlock(&common->cc_lock);
  313. }
  314. /* Perform calibration if necessary */
  315. if (longcal || shortcal) {
  316. int ret = ath9k_hw_calibrate(ah, ah->curchan, ah->rxchainmask,
  317. longcal);
  318. if (ret < 0) {
  319. common->ani.caldone = 0;
  320. ath9k_queue_reset(sc, RESET_TYPE_CALIBRATION);
  321. return;
  322. }
  323. common->ani.caldone = ret;
  324. }
  325. ath_dbg(common, ANI,
  326. "Calibration @%lu finished: %s %s %s, caldone: %s\n",
  327. jiffies,
  328. longcal ? "long" : "", shortcal ? "short" : "",
  329. aniflag ? "ani" : "", common->ani.caldone ? "true" : "false");
  330. ath9k_ps_restore(sc);
  331. set_timer:
  332. /*
  333. * Set timer interval based on previous results.
  334. * The interval must be the shortest necessary to satisfy ANI,
  335. * short calibration and long calibration.
  336. */
  337. cal_interval = ATH_LONG_CALINTERVAL;
  338. cal_interval = min(cal_interval, (u32)ah->config.ani_poll_interval);
  339. if (!common->ani.caldone)
  340. cal_interval = min(cal_interval, (u32)short_cal_interval);
  341. mod_timer(&common->ani.timer, jiffies + msecs_to_jiffies(cal_interval));
  342. if (ar9003_is_paprd_enabled(ah) && ah->caldata) {
  343. if (!test_bit(PAPRD_DONE, &ah->caldata->cal_flags)) {
  344. ieee80211_queue_work(sc->hw, &sc->paprd_work);
  345. } else if (!ah->paprd_table_write_done) {
  346. ath9k_ps_wakeup(sc);
  347. ath_paprd_activate(sc);
  348. ath9k_ps_restore(sc);
  349. }
  350. }
  351. }
  352. void ath_start_ani(struct ath_softc *sc)
  353. {
  354. struct ath_hw *ah = sc->sc_ah;
  355. struct ath_common *common = ath9k_hw_common(ah);
  356. unsigned long timestamp = jiffies_to_msecs(jiffies);
  357. if (common->disable_ani ||
  358. !test_bit(ATH_OP_ANI_RUN, &common->op_flags) ||
  359. sc->cur_chan->offchannel)
  360. return;
  361. common->ani.longcal_timer = timestamp;
  362. common->ani.shortcal_timer = timestamp;
  363. common->ani.checkani_timer = timestamp;
  364. ath_dbg(common, ANI, "Starting ANI\n");
  365. mod_timer(&common->ani.timer,
  366. jiffies + msecs_to_jiffies((u32)ah->config.ani_poll_interval));
  367. }
  368. void ath_stop_ani(struct ath_softc *sc)
  369. {
  370. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  371. ath_dbg(common, ANI, "Stopping ANI\n");
  372. del_timer_sync(&common->ani.timer);
  373. }
  374. void ath_check_ani(struct ath_softc *sc)
  375. {
  376. struct ath_hw *ah = sc->sc_ah;
  377. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  378. struct ath_beacon_config *cur_conf = &sc->cur_chan->beacon;
  379. /*
  380. * Check for the various conditions in which ANI has to
  381. * be stopped.
  382. */
  383. if (ah->opmode == NL80211_IFTYPE_ADHOC) {
  384. if (!cur_conf->enable_beacon)
  385. goto stop_ani;
  386. } else if (ah->opmode == NL80211_IFTYPE_AP) {
  387. if (!cur_conf->enable_beacon) {
  388. /*
  389. * Disable ANI only when there are no
  390. * associated stations.
  391. */
  392. if (!test_bit(ATH_OP_PRIM_STA_VIF, &common->op_flags))
  393. goto stop_ani;
  394. }
  395. } else if (ah->opmode == NL80211_IFTYPE_STATION) {
  396. if (!test_bit(ATH_OP_PRIM_STA_VIF, &common->op_flags))
  397. goto stop_ani;
  398. }
  399. if (!test_bit(ATH_OP_ANI_RUN, &common->op_flags)) {
  400. set_bit(ATH_OP_ANI_RUN, &common->op_flags);
  401. ath_start_ani(sc);
  402. }
  403. return;
  404. stop_ani:
  405. clear_bit(ATH_OP_ANI_RUN, &common->op_flags);
  406. ath_stop_ani(sc);
  407. }
  408. void ath_update_survey_nf(struct ath_softc *sc, int channel)
  409. {
  410. struct ath_hw *ah = sc->sc_ah;
  411. struct ath9k_channel *chan = &ah->channels[channel];
  412. struct survey_info *survey = &sc->survey[channel];
  413. if (chan->noisefloor) {
  414. survey->filled |= SURVEY_INFO_NOISE_DBM;
  415. survey->noise = ath9k_hw_getchan_noise(ah, chan,
  416. chan->noisefloor);
  417. }
  418. }
  419. /*
  420. * Updates the survey statistics and returns the busy time since last
  421. * update in %, if the measurement duration was long enough for the
  422. * result to be useful, -1 otherwise.
  423. */
  424. int ath_update_survey_stats(struct ath_softc *sc)
  425. {
  426. struct ath_hw *ah = sc->sc_ah;
  427. struct ath_common *common = ath9k_hw_common(ah);
  428. int pos = ah->curchan - &ah->channels[0];
  429. struct survey_info *survey = &sc->survey[pos];
  430. struct ath_cycle_counters *cc = &common->cc_survey;
  431. unsigned int div = common->clockrate * 1000;
  432. int ret = 0;
  433. if (!ah->curchan)
  434. return -1;
  435. if (ah->power_mode == ATH9K_PM_AWAKE)
  436. ath_hw_cycle_counters_update(common);
  437. if (cc->cycles > 0) {
  438. survey->filled |= SURVEY_INFO_TIME |
  439. SURVEY_INFO_TIME_BUSY |
  440. SURVEY_INFO_TIME_RX |
  441. SURVEY_INFO_TIME_TX;
  442. survey->time += cc->cycles / div;
  443. survey->time_busy += cc->rx_busy / div;
  444. survey->time_rx += cc->rx_frame / div;
  445. survey->time_tx += cc->tx_frame / div;
  446. }
  447. if (cc->cycles < div)
  448. return -1;
  449. if (cc->cycles > 0)
  450. ret = cc->rx_busy * 100 / cc->cycles;
  451. memset(cc, 0, sizeof(*cc));
  452. ath_update_survey_nf(sc, pos);
  453. return ret;
  454. }