blob: b829263e3d0a7f2a849bb96bfeed4ab948a0c1bc [file] [log] [blame]
Sujith Manoharanef1b6cd2012-06-04 20:23:37 +05301/*
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
17#include "ath9k.h"
18
19/*
20 * TX polling - checks if the TX engine is stuck somewhere
21 * and issues a chip reset if so.
22 */
23void ath_tx_complete_poll_work(struct work_struct *work)
24{
25 struct ath_softc *sc = container_of(work, struct ath_softc,
26 tx_complete_work.work);
27 struct ath_txq *txq;
28 int i;
29 bool needreset = false;
Sujith Manoharanef1b6cd2012-06-04 20:23:37 +053030
Luis R. Rodriguez89f927a2013-10-14 17:42:11 -070031
32 if (sc->tx99_state) {
33 ath_dbg(ath9k_hw_common(sc->sc_ah), RESET,
34 "skip tx hung detection on tx99\n");
35 return;
36 }
37
Felix Fietkau01d4ab92013-03-15 16:18:44 +010038 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
39 txq = sc->tx.txq_map[i];
40
41 ath_txq_lock(sc, txq);
42 if (txq->axq_depth) {
43 if (txq->axq_tx_inprogress) {
44 needreset = true;
45 ath_txq_unlock(sc, txq);
46 break;
47 } else {
48 txq->axq_tx_inprogress = true;
Sujith Manoharanef1b6cd2012-06-04 20:23:37 +053049 }
Sujith Manoharanef1b6cd2012-06-04 20:23:37 +053050 }
Sujith Manoharand4c04ba2013-08-26 11:47:22 +053051 ath_txq_unlock(sc, txq);
Felix Fietkau01d4ab92013-03-15 16:18:44 +010052 }
Sujith Manoharanef1b6cd2012-06-04 20:23:37 +053053
54 if (needreset) {
55 ath_dbg(ath9k_hw_common(sc->sc_ah), RESET,
56 "tx hung, resetting the chip\n");
Rajkumar Manoharan124b9792012-07-17 17:16:42 +053057 ath9k_queue_reset(sc, RESET_TYPE_TX_HANG);
Sujith Manoharanaf68aba2012-06-04 20:23:43 +053058 return;
Sujith Manoharanef1b6cd2012-06-04 20:23:37 +053059 }
60
61 ieee80211_queue_delayed_work(sc->hw, &sc->tx_complete_work,
62 msecs_to_jiffies(ATH_TX_COMPLETE_POLL_INT));
63}
64
65/*
66 * Checks if the BB/MAC is hung.
67 */
Sujith Manoharan415ec612013-12-24 10:44:25 +053068bool ath_hw_check(struct ath_softc *sc)
Sujith Manoharanef1b6cd2012-06-04 20:23:37 +053069{
Sujith Manoharanef1b6cd2012-06-04 20:23:37 +053070 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Rajkumar Manoharan124b9792012-07-17 17:16:42 +053071 enum ath_reset_type type;
Sujith Manoharan415ec612013-12-24 10:44:25 +053072 bool is_alive;
Sujith Manoharanef1b6cd2012-06-04 20:23:37 +053073
74 ath9k_ps_wakeup(sc);
Sujith Manoharan415ec612013-12-24 10:44:25 +053075
Sujith Manoharanef1b6cd2012-06-04 20:23:37 +053076 is_alive = ath9k_hw_check_alive(sc->sc_ah);
77
Sujith Manoharan415ec612013-12-24 10:44:25 +053078 if (!is_alive) {
Sujith Manoharanef1b6cd2012-06-04 20:23:37 +053079 ath_dbg(common, RESET,
Sujith Manoharan415ec612013-12-24 10:44:25 +053080 "HW hang detected, schedule chip reset\n");
Rajkumar Manoharan124b9792012-07-17 17:16:42 +053081 type = RESET_TYPE_MAC_HANG;
Sujith Manoharan415ec612013-12-24 10:44:25 +053082 ath9k_queue_reset(sc, type);
Sujith Manoharanef1b6cd2012-06-04 20:23:37 +053083 }
84
Sujith Manoharanef1b6cd2012-06-04 20:23:37 +053085 ath9k_ps_restore(sc);
Sujith Manoharan415ec612013-12-24 10:44:25 +053086
87 return is_alive;
Sujith Manoharanef1b6cd2012-06-04 20:23:37 +053088}
89
90/*
Sujith Manoharanaf68aba2012-06-04 20:23:43 +053091 * PLL-WAR for AR9485/AR9340
Sujith Manoharanef1b6cd2012-06-04 20:23:37 +053092 */
Sujith Manoharanaf68aba2012-06-04 20:23:43 +053093static bool ath_hw_pll_rx_hang_check(struct ath_softc *sc, u32 pll_sqsum)
Sujith Manoharanef1b6cd2012-06-04 20:23:37 +053094{
95 static int count;
96 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
97
98 if (pll_sqsum >= 0x40000) {
99 count++;
100 if (count == 3) {
Sujith Manoharanaf68aba2012-06-04 20:23:43 +0530101 ath_dbg(common, RESET, "PLL WAR, resetting the chip\n");
Rajkumar Manoharan124b9792012-07-17 17:16:42 +0530102 ath9k_queue_reset(sc, RESET_TYPE_PLL_HANG);
Sujith Manoharanef1b6cd2012-06-04 20:23:37 +0530103 count = 0;
Sujith Manoharanaf68aba2012-06-04 20:23:43 +0530104 return true;
Sujith Manoharanef1b6cd2012-06-04 20:23:37 +0530105 }
Sujith Manoharanaf68aba2012-06-04 20:23:43 +0530106 } else {
Sujith Manoharanef1b6cd2012-06-04 20:23:37 +0530107 count = 0;
Sujith Manoharanaf68aba2012-06-04 20:23:43 +0530108 }
109
110 return false;
Sujith Manoharanef1b6cd2012-06-04 20:23:37 +0530111}
112
113void ath_hw_pll_work(struct work_struct *work)
114{
Sujith Manoharanaf68aba2012-06-04 20:23:43 +0530115 u32 pll_sqsum;
Sujith Manoharanef1b6cd2012-06-04 20:23:37 +0530116 struct ath_softc *sc = container_of(work, struct ath_softc,
117 hw_pll_work.work);
Oleksij Rempeleefa01d2014-02-27 11:40:46 +0100118 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Mohammed Shafi Shajakhan64bc1232012-06-12 20:13:43 +0530119 /*
120 * ensure that the PLL WAR is executed only
121 * after the STA is associated (or) if the
122 * beaconing had started in interfaces that
123 * uses beacons.
124 */
Oleksij Rempeleefa01d2014-02-27 11:40:46 +0100125 if (!test_bit(ATH_OP_BEACONS, &common->op_flags))
Mohammed Shafi Shajakhan64bc1232012-06-12 20:13:43 +0530126 return;
Sujith Manoharanef1b6cd2012-06-04 20:23:37 +0530127
Luis R. Rodriguez89f927a2013-10-14 17:42:11 -0700128 if (sc->tx99_state)
129 return;
130
Sujith Manoharanaf68aba2012-06-04 20:23:43 +0530131 ath9k_ps_wakeup(sc);
132 pll_sqsum = ar9003_get_pll_sqsum_dvc(sc->sc_ah);
133 ath9k_ps_restore(sc);
134 if (ath_hw_pll_rx_hang_check(sc, pll_sqsum))
135 return;
136
137 ieee80211_queue_delayed_work(sc->hw, &sc->hw_pll_work,
138 msecs_to_jiffies(ATH_PLL_WORK_INTERVAL));
Sujith Manoharanef1b6cd2012-06-04 20:23:37 +0530139}
140
141/*
Sujith Manoharanef1b6cd2012-06-04 20:23:37 +0530142 * PA Pre-distortion.
143 */
144static void ath_paprd_activate(struct ath_softc *sc)
145{
146 struct ath_hw *ah = sc->sc_ah;
Sujith Manoharan914d0f42012-12-10 07:22:33 +0530147 struct ath_common *common = ath9k_hw_common(ah);
Sujith Manoharanef1b6cd2012-06-04 20:23:37 +0530148 struct ath9k_hw_cal_data *caldata = ah->caldata;
149 int chain;
150
Sujith Manoharan4b9b42b2013-09-11 16:36:31 +0530151 if (!caldata || !test_bit(PAPRD_DONE, &caldata->cal_flags)) {
Sujith Manoharan914d0f42012-12-10 07:22:33 +0530152 ath_dbg(common, CALIBRATE, "Failed to activate PAPRD\n");
Sujith Manoharanef1b6cd2012-06-04 20:23:37 +0530153 return;
Sujith Manoharan914d0f42012-12-10 07:22:33 +0530154 }
Sujith Manoharanef1b6cd2012-06-04 20:23:37 +0530155
Sujith Manoharanef1b6cd2012-06-04 20:23:37 +0530156 ar9003_paprd_enable(ah, false);
157 for (chain = 0; chain < AR9300_MAX_CHAINS; chain++) {
158 if (!(ah->txchainmask & BIT(chain)))
159 continue;
160
161 ar9003_paprd_populate_single_table(ah, caldata, chain);
162 }
163
Sujith Manoharan914d0f42012-12-10 07:22:33 +0530164 ath_dbg(common, CALIBRATE, "Activating PAPRD\n");
Sujith Manoharanef1b6cd2012-06-04 20:23:37 +0530165 ar9003_paprd_enable(ah, true);
Sujith Manoharanef1b6cd2012-06-04 20:23:37 +0530166}
167
168static bool ath_paprd_send_frame(struct ath_softc *sc, struct sk_buff *skb, int chain)
169{
170 struct ieee80211_hw *hw = sc->hw;
171 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
172 struct ath_hw *ah = sc->sc_ah;
173 struct ath_common *common = ath9k_hw_common(ah);
174 struct ath_tx_control txctl;
175 int time_left;
176
177 memset(&txctl, 0, sizeof(txctl));
Sujith Manoharanbea843c2012-11-21 18:13:10 +0530178 txctl.txq = sc->tx.txq_map[IEEE80211_AC_BE];
Sujith Manoharanef1b6cd2012-06-04 20:23:37 +0530179
180 memset(tx_info, 0, sizeof(*tx_info));
Felix Fietkaubff11762014-06-11 16:17:52 +0530181 tx_info->band = sc->cur_chandef.chan->band;
Sujith Manoharanef1b6cd2012-06-04 20:23:37 +0530182 tx_info->flags |= IEEE80211_TX_CTL_NO_ACK;
183 tx_info->control.rates[0].idx = 0;
184 tx_info->control.rates[0].count = 1;
185 tx_info->control.rates[0].flags = IEEE80211_TX_RC_MCS;
186 tx_info->control.rates[1].idx = -1;
187
188 init_completion(&sc->paprd_complete);
189 txctl.paprd = BIT(chain);
190
191 if (ath_tx_start(hw, skb, &txctl) != 0) {
192 ath_dbg(common, CALIBRATE, "PAPRD TX failed\n");
193 dev_kfree_skb_any(skb);
194 return false;
195 }
196
197 time_left = wait_for_completion_timeout(&sc->paprd_complete,
198 msecs_to_jiffies(ATH_PAPRD_TIMEOUT));
199
200 if (!time_left)
201 ath_dbg(common, CALIBRATE,
202 "Timeout waiting for paprd training on TX chain %d\n",
203 chain);
204
205 return !!time_left;
206}
207
208void ath_paprd_calibrate(struct work_struct *work)
209{
210 struct ath_softc *sc = container_of(work, struct ath_softc, paprd_work);
211 struct ieee80211_hw *hw = sc->hw;
212 struct ath_hw *ah = sc->sc_ah;
213 struct ieee80211_hdr *hdr;
214 struct sk_buff *skb = NULL;
215 struct ath9k_hw_cal_data *caldata = ah->caldata;
216 struct ath_common *common = ath9k_hw_common(ah);
217 int ftype;
218 int chain_ok = 0;
219 int chain;
220 int len = 1800;
Felix Fietkau381c7262012-08-27 17:00:05 +0200221 int ret;
Sujith Manoharanef1b6cd2012-06-04 20:23:37 +0530222
Sujith Manoharan4b9b42b2013-09-11 16:36:31 +0530223 if (!caldata ||
224 !test_bit(PAPRD_PACKET_SENT, &caldata->cal_flags) ||
225 test_bit(PAPRD_DONE, &caldata->cal_flags)) {
Sujith Manoharan914d0f42012-12-10 07:22:33 +0530226 ath_dbg(common, CALIBRATE, "Skipping PAPRD calibration\n");
Sujith Manoharanef1b6cd2012-06-04 20:23:37 +0530227 return;
Sujith Manoharan914d0f42012-12-10 07:22:33 +0530228 }
Sujith Manoharanef1b6cd2012-06-04 20:23:37 +0530229
230 ath9k_ps_wakeup(sc);
231
232 if (ar9003_paprd_init_table(ah) < 0)
233 goto fail_paprd;
234
235 skb = alloc_skb(len, GFP_KERNEL);
236 if (!skb)
237 goto fail_paprd;
238
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
249 for (chain = 0; chain < AR9300_MAX_CHAINS; chain++) {
250 if (!(ah->txchainmask & BIT(chain)))
251 continue;
252
253 chain_ok = 0;
Sujith Manoharanef1b6cd2012-06-04 20:23:37 +0530254 ar9003_paprd_setup_gain_table(ah, chain);
255
256 ath_dbg(common, CALIBRATE,
257 "Sending PAPRD training frame on chain %d\n", chain);
258 if (!ath_paprd_send_frame(sc, skb, chain))
259 goto fail_paprd;
260
261 if (!ar9003_paprd_is_done(ah)) {
262 ath_dbg(common, CALIBRATE,
263 "PAPRD not yet done on chain %d\n", chain);
264 break;
265 }
266
Felix Fietkau381c7262012-08-27 17:00:05 +0200267 ret = ar9003_paprd_create_curve(ah, caldata, chain);
268 if (ret == -EINPROGRESS) {
269 ath_dbg(common, CALIBRATE,
270 "PAPRD curve on chain %d needs to be re-trained\n",
271 chain);
272 break;
273 } else if (ret) {
Sujith Manoharanef1b6cd2012-06-04 20:23:37 +0530274 ath_dbg(common, CALIBRATE,
275 "PAPRD create curve failed on chain %d\n",
Sujith Manoharanaf68aba2012-06-04 20:23:43 +0530276 chain);
Sujith Manoharanef1b6cd2012-06-04 20:23:37 +0530277 break;
278 }
279
280 chain_ok = 1;
281 }
282 kfree_skb(skb);
283
284 if (chain_ok) {
Sujith Manoharan4b9b42b2013-09-11 16:36:31 +0530285 set_bit(PAPRD_DONE, &caldata->cal_flags);
Sujith Manoharanef1b6cd2012-06-04 20:23:37 +0530286 ath_paprd_activate(sc);
287 }
288
289fail_paprd:
290 ath9k_ps_restore(sc);
291}
292
293/*
294 * ANI performs periodic noise floor calibration
295 * that is used to adjust and optimize the chip performance. This
296 * takes environmental changes (location, temperature) into account.
297 * When the task is complete, it reschedules itself depending on the
298 * appropriate interval that was calculated.
299 */
300void ath_ani_calibrate(unsigned long data)
301{
302 struct ath_softc *sc = (struct ath_softc *)data;
303 struct ath_hw *ah = sc->sc_ah;
304 struct ath_common *common = ath9k_hw_common(ah);
305 bool longcal = false;
306 bool shortcal = false;
307 bool aniflag = false;
308 unsigned int timestamp = jiffies_to_msecs(jiffies);
309 u32 cal_interval, short_cal_interval, long_cal_interval;
310 unsigned long flags;
311
Sujith Manoharan4b9b42b2013-09-11 16:36:31 +0530312 if (ah->caldata && test_bit(NFCAL_INTF, &ah->caldata->cal_flags))
Sujith Manoharanef1b6cd2012-06-04 20:23:37 +0530313 long_cal_interval = ATH_LONG_CALINTERVAL_INT;
314 else
315 long_cal_interval = ATH_LONG_CALINTERVAL;
316
317 short_cal_interval = (ah->opmode == NL80211_IFTYPE_AP) ?
318 ATH_AP_SHORT_CALINTERVAL : ATH_STA_SHORT_CALINTERVAL;
319
320 /* Only calibrate if awake */
Rajkumar Manoharan424749c2012-10-10 23:03:02 +0530321 if (sc->sc_ah->power_mode != ATH9K_PM_AWAKE) {
322 if (++ah->ani_skip_count >= ATH_ANI_MAX_SKIP_COUNT) {
323 spin_lock_irqsave(&sc->sc_pm_lock, flags);
324 sc->ps_flags |= PS_WAIT_FOR_ANI;
325 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
326 }
Sujith Manoharanef1b6cd2012-06-04 20:23:37 +0530327 goto set_timer;
Rajkumar Manoharan424749c2012-10-10 23:03:02 +0530328 }
329 ah->ani_skip_count = 0;
330 spin_lock_irqsave(&sc->sc_pm_lock, flags);
331 sc->ps_flags &= ~PS_WAIT_FOR_ANI;
332 spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
Sujith Manoharanef1b6cd2012-06-04 20:23:37 +0530333
334 ath9k_ps_wakeup(sc);
335
336 /* Long calibration runs independently of short calibration. */
337 if ((timestamp - common->ani.longcal_timer) >= long_cal_interval) {
338 longcal = true;
339 common->ani.longcal_timer = timestamp;
340 }
341
342 /* Short calibration applies only while caldone is false */
343 if (!common->ani.caldone) {
344 if ((timestamp - common->ani.shortcal_timer) >= short_cal_interval) {
345 shortcal = true;
346 common->ani.shortcal_timer = timestamp;
347 common->ani.resetcal_timer = timestamp;
348 }
349 } else {
350 if ((timestamp - common->ani.resetcal_timer) >=
351 ATH_RESTART_CALINTERVAL) {
352 common->ani.caldone = ath9k_hw_reset_calvalid(ah);
353 if (common->ani.caldone)
354 common->ani.resetcal_timer = timestamp;
355 }
356 }
357
358 /* Verify whether we must check ANI */
Sujith Manoharane3233002013-06-03 09:19:26 +0530359 if ((timestamp - common->ani.checkani_timer) >= ah->config.ani_poll_interval) {
Sujith Manoharanef1b6cd2012-06-04 20:23:37 +0530360 aniflag = true;
361 common->ani.checkani_timer = timestamp;
362 }
363
364 /* Call ANI routine if necessary */
365 if (aniflag) {
Sujith Manoharanb7cc9b92013-12-26 08:14:40 +0530366 spin_lock(&common->cc_lock);
Sujith Manoharanef1b6cd2012-06-04 20:23:37 +0530367 ath9k_hw_ani_monitor(ah, ah->curchan);
368 ath_update_survey_stats(sc);
Sujith Manoharanb7cc9b92013-12-26 08:14:40 +0530369 spin_unlock(&common->cc_lock);
Sujith Manoharanef1b6cd2012-06-04 20:23:37 +0530370 }
371
372 /* Perform calibration if necessary */
373 if (longcal || shortcal) {
Felix Fietkau7b8aaea2014-10-25 17:19:30 +0200374 int ret = ath9k_hw_calibrate(ah, ah->curchan, ah->rxchainmask,
375 longcal);
376 if (ret < 0) {
377 common->ani.caldone = 0;
378 ath9k_queue_reset(sc, RESET_TYPE_CALIBRATION);
379 return;
380 }
381
382 common->ani.caldone = ret;
Sujith Manoharanef1b6cd2012-06-04 20:23:37 +0530383 }
384
385 ath_dbg(common, ANI,
386 "Calibration @%lu finished: %s %s %s, caldone: %s\n",
387 jiffies,
388 longcal ? "long" : "", shortcal ? "short" : "",
389 aniflag ? "ani" : "", common->ani.caldone ? "true" : "false");
390
391 ath9k_ps_restore(sc);
392
393set_timer:
394 /*
395 * Set timer interval based on previous results.
396 * The interval must be the shortest necessary to satisfy ANI,
397 * short calibration and long calibration.
398 */
Sujith Manoharanef1b6cd2012-06-04 20:23:37 +0530399 cal_interval = ATH_LONG_CALINTERVAL;
Sujith Manoharane3233002013-06-03 09:19:26 +0530400 cal_interval = min(cal_interval, (u32)ah->config.ani_poll_interval);
Sujith Manoharanef1b6cd2012-06-04 20:23:37 +0530401 if (!common->ani.caldone)
402 cal_interval = min(cal_interval, (u32)short_cal_interval);
403
404 mod_timer(&common->ani.timer, jiffies + msecs_to_jiffies(cal_interval));
Sujith Manoharan19f78422012-12-10 07:22:34 +0530405
Sujith Manoharan0f21ee82012-12-10 07:22:37 +0530406 if (ar9003_is_paprd_enabled(ah) && ah->caldata) {
Sujith Manoharan4b9b42b2013-09-11 16:36:31 +0530407 if (!test_bit(PAPRD_DONE, &ah->caldata->cal_flags)) {
Sujith Manoharanef1b6cd2012-06-04 20:23:37 +0530408 ieee80211_queue_work(sc->hw, &sc->paprd_work);
Sujith Manoharan19f78422012-12-10 07:22:34 +0530409 } else if (!ah->paprd_table_write_done) {
410 ath9k_ps_wakeup(sc);
Sujith Manoharanef1b6cd2012-06-04 20:23:37 +0530411 ath_paprd_activate(sc);
Sujith Manoharan19f78422012-12-10 07:22:34 +0530412 ath9k_ps_restore(sc);
413 }
Sujith Manoharanef1b6cd2012-06-04 20:23:37 +0530414 }
415}
416
Sujith Manoharanda0d45f2012-07-17 17:16:29 +0530417void ath_start_ani(struct ath_softc *sc)
Sujith Manoharanef1b6cd2012-06-04 20:23:37 +0530418{
Sujith Manoharanda0d45f2012-07-17 17:16:29 +0530419 struct ath_hw *ah = sc->sc_ah;
420 struct ath_common *common = ath9k_hw_common(ah);
Sujith Manoharanef1b6cd2012-06-04 20:23:37 +0530421 unsigned long timestamp = jiffies_to_msecs(jiffies);
Sujith Manoharanef1b6cd2012-06-04 20:23:37 +0530422
Sujith Manoharanda0d45f2012-07-17 17:16:29 +0530423 if (common->disable_ani ||
Oleksij Rempeleefa01d2014-02-27 11:40:46 +0100424 !test_bit(ATH_OP_ANI_RUN, &common->op_flags) ||
Felix Fietkaufbbcd142014-06-11 16:17:49 +0530425 sc->cur_chan->offchannel)
Sujith Manoharanef1b6cd2012-06-04 20:23:37 +0530426 return;
427
428 common->ani.longcal_timer = timestamp;
429 common->ani.shortcal_timer = timestamp;
430 common->ani.checkani_timer = timestamp;
431
Sujith Manoharanda0d45f2012-07-17 17:16:29 +0530432 ath_dbg(common, ANI, "Starting ANI\n");
Sujith Manoharanef1b6cd2012-06-04 20:23:37 +0530433 mod_timer(&common->ani.timer,
434 jiffies + msecs_to_jiffies((u32)ah->config.ani_poll_interval));
435}
436
Sujith Manoharanda0d45f2012-07-17 17:16:29 +0530437void ath_stop_ani(struct ath_softc *sc)
438{
439 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
440
441 ath_dbg(common, ANI, "Stopping ANI\n");
442 del_timer_sync(&common->ani.timer);
443}
444
445void ath_check_ani(struct ath_softc *sc)
446{
447 struct ath_hw *ah = sc->sc_ah;
Oleksij Rempeleefa01d2014-02-27 11:40:46 +0100448 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Rajkumar Manoharanca900ac2014-06-11 16:18:02 +0530449 struct ath_beacon_config *cur_conf = &sc->cur_chan->beacon;
Sujith Manoharanda0d45f2012-07-17 17:16:29 +0530450
451 /*
452 * Check for the various conditions in which ANI has to
453 * be stopped.
454 */
455 if (ah->opmode == NL80211_IFTYPE_ADHOC) {
456 if (!cur_conf->enable_beacon)
457 goto stop_ani;
458 } else if (ah->opmode == NL80211_IFTYPE_AP) {
459 if (!cur_conf->enable_beacon) {
460 /*
461 * Disable ANI only when there are no
462 * associated stations.
463 */
Oleksij Rempeleefa01d2014-02-27 11:40:46 +0100464 if (!test_bit(ATH_OP_PRIM_STA_VIF, &common->op_flags))
Sujith Manoharanda0d45f2012-07-17 17:16:29 +0530465 goto stop_ani;
466 }
467 } else if (ah->opmode == NL80211_IFTYPE_STATION) {
Oleksij Rempeleefa01d2014-02-27 11:40:46 +0100468 if (!test_bit(ATH_OP_PRIM_STA_VIF, &common->op_flags))
Sujith Manoharanda0d45f2012-07-17 17:16:29 +0530469 goto stop_ani;
470 }
471
Oleksij Rempeleefa01d2014-02-27 11:40:46 +0100472 if (!test_bit(ATH_OP_ANI_RUN, &common->op_flags)) {
473 set_bit(ATH_OP_ANI_RUN, &common->op_flags);
Sujith Manoharanda0d45f2012-07-17 17:16:29 +0530474 ath_start_ani(sc);
475 }
476
477 return;
478
479stop_ani:
Oleksij Rempeleefa01d2014-02-27 11:40:46 +0100480 clear_bit(ATH_OP_ANI_RUN, &common->op_flags);
Sujith Manoharanda0d45f2012-07-17 17:16:29 +0530481 ath_stop_ani(sc);
482}
483
Sujith Manoharanef1b6cd2012-06-04 20:23:37 +0530484void ath_update_survey_nf(struct ath_softc *sc, int channel)
485{
486 struct ath_hw *ah = sc->sc_ah;
487 struct ath9k_channel *chan = &ah->channels[channel];
488 struct survey_info *survey = &sc->survey[channel];
489
490 if (chan->noisefloor) {
491 survey->filled |= SURVEY_INFO_NOISE_DBM;
Lorenzo Bianconi5bc225a2013-10-11 14:09:54 +0200492 survey->noise = ath9k_hw_getchan_noise(ah, chan,
493 chan->noisefloor);
Sujith Manoharanef1b6cd2012-06-04 20:23:37 +0530494 }
495}
496
497/*
498 * Updates the survey statistics and returns the busy time since last
499 * update in %, if the measurement duration was long enough for the
500 * result to be useful, -1 otherwise.
501 */
502int ath_update_survey_stats(struct ath_softc *sc)
503{
504 struct ath_hw *ah = sc->sc_ah;
505 struct ath_common *common = ath9k_hw_common(ah);
506 int pos = ah->curchan - &ah->channels[0];
507 struct survey_info *survey = &sc->survey[pos];
508 struct ath_cycle_counters *cc = &common->cc_survey;
509 unsigned int div = common->clockrate * 1000;
510 int ret = 0;
511
512 if (!ah->curchan)
513 return -1;
514
515 if (ah->power_mode == ATH9K_PM_AWAKE)
516 ath_hw_cycle_counters_update(common);
517
518 if (cc->cycles > 0) {
519 survey->filled |= SURVEY_INFO_CHANNEL_TIME |
520 SURVEY_INFO_CHANNEL_TIME_BUSY |
521 SURVEY_INFO_CHANNEL_TIME_RX |
522 SURVEY_INFO_CHANNEL_TIME_TX;
523 survey->channel_time += cc->cycles / div;
524 survey->channel_time_busy += cc->rx_busy / div;
525 survey->channel_time_rx += cc->rx_frame / div;
526 survey->channel_time_tx += cc->tx_frame / div;
527 }
528
529 if (cc->cycles < div)
530 return -1;
531
532 if (cc->cycles > 0)
533 ret = cc->rx_busy * 100 / cc->cycles;
534
535 memset(cc, 0, sizeof(*cc));
536
537 ath_update_survey_nf(sc, pos);
538
539 return ret;
540}