link.c 15 KB

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