link.c 14 KB

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