link.c 15 KB

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