link.c 15 KB

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