blob: 939cfa5141c6a1cbd05edc7d1c8be0f0d256e69b [file] [log] [blame]
Thomas Gleixner1ccea772019-05-19 15:51:43 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Ivo van Doorn84e31962008-12-20 10:53:29 +01002/*
Gertjan van Wingerde9c9a0d12009-11-08 16:39:55 +01003 Copyright (C) 2004 - 2009 Ivo van Doorn <IvDoorn@gmail.com>
Ivo van Doorn84e31962008-12-20 10:53:29 +01004 <http://rt2x00.serialmonkey.com>
5
Ivo van Doorn84e31962008-12-20 10:53:29 +01006 */
7
8/*
9 Module: rt2x00lib
10 Abstract: rt2x00 generic link tuning routines.
11 */
12
13#include <linux/kernel.h>
14#include <linux/module.h>
15
16#include "rt2x00.h"
17#include "rt2x00lib.h"
18
Ivo van Doorn5352ff62008-12-20 10:54:54 +010019/*
20 * When we lack RSSI information return something less then -80 to
21 * tell the driver to tune the device to maximum sensitivity.
22 */
23#define DEFAULT_RSSI -128
24
Johannes Berg11ab35e2015-08-19 09:46:21 +020025static inline int rt2x00link_get_avg_rssi(struct ewma_rssi *ewma)
Gabor Juhos1dc254a2013-10-09 11:00:38 +020026{
27 unsigned long avg;
28
Johannes Berg11ab35e2015-08-19 09:46:21 +020029 avg = ewma_rssi_read(ewma);
Gabor Juhos1dc254a2013-10-09 11:00:38 +020030 if (avg)
31 return -avg;
32
33 return DEFAULT_RSSI;
34}
Ivo van Doorn5352ff62008-12-20 10:54:54 +010035
Ivo van Doorn84e31962008-12-20 10:53:29 +010036static int rt2x00link_antenna_get_link_rssi(struct rt2x00_dev *rt2x00dev)
37{
38 struct link_ant *ant = &rt2x00dev->link.ant;
39
Gabor Juhos1dc254a2013-10-09 11:00:38 +020040 if (rt2x00dev->link.qual.rx_success)
41 return rt2x00link_get_avg_rssi(&ant->rssi_ant);
42
Ivo van Doorn84e31962008-12-20 10:53:29 +010043 return DEFAULT_RSSI;
44}
45
Lars Ericsson193df182009-08-08 23:54:24 +020046static int rt2x00link_antenna_get_rssi_history(struct rt2x00_dev *rt2x00dev)
Ivo van Doorn84e31962008-12-20 10:53:29 +010047{
48 struct link_ant *ant = &rt2x00dev->link.ant;
49
Lars Ericsson193df182009-08-08 23:54:24 +020050 if (ant->rssi_history)
51 return ant->rssi_history;
Ivo van Doorn84e31962008-12-20 10:53:29 +010052 return DEFAULT_RSSI;
53}
Ivo van Doorn84e31962008-12-20 10:53:29 +010054
55static void rt2x00link_antenna_update_rssi_history(struct rt2x00_dev *rt2x00dev,
Ivo van Doorn84e31962008-12-20 10:53:29 +010056 int rssi)
57{
58 struct link_ant *ant = &rt2x00dev->link.ant;
Lars Ericsson193df182009-08-08 23:54:24 +020059 ant->rssi_history = rssi;
Ivo van Doorn84e31962008-12-20 10:53:29 +010060}
Ivo van Doorn84e31962008-12-20 10:53:29 +010061
62static void rt2x00link_antenna_reset(struct rt2x00_dev *rt2x00dev)
63{
Johannes Berg11ab35e2015-08-19 09:46:21 +020064 ewma_rssi_init(&rt2x00dev->link.ant.rssi_ant);
Ivo van Doorn84e31962008-12-20 10:53:29 +010065}
66
67static void rt2x00lib_antenna_diversity_sample(struct rt2x00_dev *rt2x00dev)
68{
69 struct link_ant *ant = &rt2x00dev->link.ant;
70 struct antenna_setup new_ant;
Lars Ericsson193df182009-08-08 23:54:24 +020071 int other_antenna;
72
73 int sample_current = rt2x00link_antenna_get_link_rssi(rt2x00dev);
74 int sample_other = rt2x00link_antenna_get_rssi_history(rt2x00dev);
Ivo van Doorn84e31962008-12-20 10:53:29 +010075
76 memcpy(&new_ant, &ant->active, sizeof(new_ant));
77
78 /*
79 * We are done sampling. Now we should evaluate the results.
80 */
81 ant->flags &= ~ANTENNA_MODE_SAMPLE;
82
83 /*
84 * During the last period we have sampled the RSSI
Luis Correia49513482009-07-17 21:39:19 +020085 * from both antennas. It now is time to determine
Ivo van Doorn84e31962008-12-20 10:53:29 +010086 * which antenna demonstrated the best performance.
87 * When we are already on the antenna with the best
Lars Ericsson193df182009-08-08 23:54:24 +020088 * performance, just create a good starting point
89 * for the history and we are done.
Ivo van Doorn84e31962008-12-20 10:53:29 +010090 */
Lars Ericsson193df182009-08-08 23:54:24 +020091 if (sample_current >= sample_other) {
92 rt2x00link_antenna_update_rssi_history(rt2x00dev,
93 sample_current);
Ivo van Doorn84e31962008-12-20 10:53:29 +010094 return;
Lars Ericsson193df182009-08-08 23:54:24 +020095 }
96
97 other_antenna = (ant->active.rx == ANTENNA_A) ? ANTENNA_B : ANTENNA_A;
Ivo van Doorn84e31962008-12-20 10:53:29 +010098
99 if (ant->flags & ANTENNA_RX_DIVERSITY)
Lars Ericsson193df182009-08-08 23:54:24 +0200100 new_ant.rx = other_antenna;
Ivo van Doorn84e31962008-12-20 10:53:29 +0100101
102 if (ant->flags & ANTENNA_TX_DIVERSITY)
Lars Ericsson193df182009-08-08 23:54:24 +0200103 new_ant.tx = other_antenna;
Ivo van Doorn84e31962008-12-20 10:53:29 +0100104
Lars Ericssoneb87eaa2009-07-18 20:21:52 +0200105 rt2x00lib_config_antenna(rt2x00dev, new_ant);
Ivo van Doorn84e31962008-12-20 10:53:29 +0100106}
107
108static void rt2x00lib_antenna_diversity_eval(struct rt2x00_dev *rt2x00dev)
109{
110 struct link_ant *ant = &rt2x00dev->link.ant;
111 struct antenna_setup new_ant;
112 int rssi_curr;
113 int rssi_old;
114
115 memcpy(&new_ant, &ant->active, sizeof(new_ant));
116
117 /*
118 * Get current RSSI value along with the historical value,
119 * after that update the history with the current value.
120 */
121 rssi_curr = rt2x00link_antenna_get_link_rssi(rt2x00dev);
Lars Ericsson193df182009-08-08 23:54:24 +0200122 rssi_old = rt2x00link_antenna_get_rssi_history(rt2x00dev);
123 rt2x00link_antenna_update_rssi_history(rt2x00dev, rssi_curr);
Ivo van Doorn84e31962008-12-20 10:53:29 +0100124
125 /*
126 * Legacy driver indicates that we should swap antenna's
127 * when the difference in RSSI is greater that 5. This
128 * also should be done when the RSSI was actually better
129 * then the previous sample.
130 * When the difference exceeds the threshold we should
131 * sample the rssi from the other antenna to make a valid
132 * comparison between the 2 antennas.
133 */
134 if (abs(rssi_curr - rssi_old) < 5)
135 return;
136
137 ant->flags |= ANTENNA_MODE_SAMPLE;
138
139 if (ant->flags & ANTENNA_RX_DIVERSITY)
140 new_ant.rx = (new_ant.rx == ANTENNA_A) ? ANTENNA_B : ANTENNA_A;
141
142 if (ant->flags & ANTENNA_TX_DIVERSITY)
143 new_ant.tx = (new_ant.tx == ANTENNA_A) ? ANTENNA_B : ANTENNA_A;
144
Lars Ericssoneb87eaa2009-07-18 20:21:52 +0200145 rt2x00lib_config_antenna(rt2x00dev, new_ant);
Ivo van Doorn84e31962008-12-20 10:53:29 +0100146}
147
Lars Ericsson193df182009-08-08 23:54:24 +0200148static bool rt2x00lib_antenna_diversity(struct rt2x00_dev *rt2x00dev)
Ivo van Doorn84e31962008-12-20 10:53:29 +0100149{
150 struct link_ant *ant = &rt2x00dev->link.ant;
151
152 /*
153 * Determine if software diversity is enabled for
154 * either the TX or RX antenna (or both).
Ivo van Doorn84e31962008-12-20 10:53:29 +0100155 */
Ivo van Doorn84e31962008-12-20 10:53:29 +0100156 if (!(ant->flags & ANTENNA_RX_DIVERSITY) &&
157 !(ant->flags & ANTENNA_TX_DIVERSITY)) {
158 ant->flags = 0;
Lars Ericsson193df182009-08-08 23:54:24 +0200159 return true;
Ivo van Doorn84e31962008-12-20 10:53:29 +0100160 }
161
162 /*
163 * If we have only sampled the data over the last period
164 * we should now harvest the data. Otherwise just evaluate
165 * the data. The latter should only be performed once
166 * every 2 seconds.
167 */
Lars Ericsson193df182009-08-08 23:54:24 +0200168 if (ant->flags & ANTENNA_MODE_SAMPLE) {
Ivo van Doorn84e31962008-12-20 10:53:29 +0100169 rt2x00lib_antenna_diversity_sample(rt2x00dev);
Lars Ericsson193df182009-08-08 23:54:24 +0200170 return true;
171 } else if (rt2x00dev->link.count & 1) {
Ivo van Doorn84e31962008-12-20 10:53:29 +0100172 rt2x00lib_antenna_diversity_eval(rt2x00dev);
Lars Ericsson193df182009-08-08 23:54:24 +0200173 return true;
174 }
175
176 return false;
Ivo van Doorn84e31962008-12-20 10:53:29 +0100177}
178
179void rt2x00link_update_stats(struct rt2x00_dev *rt2x00dev,
180 struct sk_buff *skb,
181 struct rxdone_entry_desc *rxdesc)
182{
Ivo van Doorn5352ff62008-12-20 10:54:54 +0100183 struct link *link = &rt2x00dev->link;
Ivo van Doorn84e31962008-12-20 10:53:29 +0100184 struct link_qual *qual = &rt2x00dev->link.qual;
185 struct link_ant *ant = &rt2x00dev->link.ant;
186 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Ivo van Doorn84e31962008-12-20 10:53:29 +0100187
188 /*
Helmut Schaaa23c2202010-10-09 13:33:16 +0200189 * No need to update the stats for !=STA interfaces
190 */
191 if (!rt2x00dev->intf_sta_count)
192 return;
193
194 /*
Ivo van Doorn84e31962008-12-20 10:53:29 +0100195 * Frame was received successfully since non-succesfull
196 * frames would have been dropped by the hardware.
197 */
198 qual->rx_success++;
199
200 /*
201 * We are only interested in quality statistics from
202 * beacons which came from the BSS which we are
203 * associated with.
204 */
205 if (!ieee80211_is_beacon(hdr->frame_control) ||
206 !(rxdesc->dev_flags & RXDONE_MY_BSS))
207 return;
208
209 /*
210 * Update global RSSI
211 */
Johannes Berg11ab35e2015-08-19 09:46:21 +0200212 ewma_rssi_add(&link->avg_rssi, -rxdesc->rssi);
Ivo van Doorn84e31962008-12-20 10:53:29 +0100213
214 /*
215 * Update antenna RSSI
216 */
Johannes Berg11ab35e2015-08-19 09:46:21 +0200217 ewma_rssi_add(&ant->rssi_ant, -rxdesc->rssi);
Ivo van Doorn84e31962008-12-20 10:53:29 +0100218}
219
Ivo van Doorn84e31962008-12-20 10:53:29 +0100220void rt2x00link_start_tuner(struct rt2x00_dev *rt2x00dev)
221{
Ivo van Doorn5352ff62008-12-20 10:54:54 +0100222 struct link *link = &rt2x00dev->link;
Ivo van Doorn84e31962008-12-20 10:53:29 +0100223
224 /*
Stanislaw Gruszkad9632472016-12-19 11:52:54 +0100225 * Single monitor mode interfaces should never have
226 * work with link tuners.
Ivo van Doorn84e31962008-12-20 10:53:29 +0100227 */
Stanislaw Gruszkad9632472016-12-19 11:52:54 +0100228 if (!rt2x00dev->intf_ap_count && !rt2x00dev->intf_sta_count)
Ivo van Doorn84e31962008-12-20 10:53:29 +0100229 return;
230
Stanislaw Gruszkad9632472016-12-19 11:52:54 +0100231 /*
Ivo van Doornd8147f92010-07-11 12:24:47 +0200232 * While scanning, link tuning is disabled. By default
233 * the most sensitive settings will be used to make sure
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300234 * that all beacons and probe responses will be received
Ivo van Doornd8147f92010-07-11 12:24:47 +0200235 * during the scan.
236 */
237 if (test_bit(DEVICE_STATE_SCANNING, &rt2x00dev->flags))
238 return;
239
Ivo van Doorn84e31962008-12-20 10:53:29 +0100240 rt2x00link_reset_tuner(rt2x00dev, false);
241
Sean Cross66f84d62009-11-05 20:22:03 +0100242 if (test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
243 ieee80211_queue_delayed_work(rt2x00dev->hw,
244 &link->work, LINK_TUNE_INTERVAL);
Ivo van Doorn84e31962008-12-20 10:53:29 +0100245}
246
247void rt2x00link_stop_tuner(struct rt2x00_dev *rt2x00dev)
248{
249 cancel_delayed_work_sync(&rt2x00dev->link.work);
250}
251
252void rt2x00link_reset_tuner(struct rt2x00_dev *rt2x00dev, bool antenna)
253{
Ivo van Doorn5352ff62008-12-20 10:54:54 +0100254 struct link_qual *qual = &rt2x00dev->link.qual;
Ivo van Doorn223dcc22010-07-11 12:25:17 +0200255 u8 vgc_level = qual->vgc_level_reg;
Ivo van Doorn5352ff62008-12-20 10:54:54 +0100256
Ivo van Doorn84e31962008-12-20 10:53:29 +0100257 if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
258 return;
259
260 /*
261 * Reset link information.
262 * Both the currently active vgc level as well as
263 * the link tuner counter should be reset. Resetting
264 * the counter is important for devices where the
265 * device should only perform link tuning during the
266 * first minute after being enabled.
267 */
268 rt2x00dev->link.count = 0;
Ivo van Doorn5352ff62008-12-20 10:54:54 +0100269 memset(qual, 0, sizeof(*qual));
Johannes Berg11ab35e2015-08-19 09:46:21 +0200270 ewma_rssi_init(&rt2x00dev->link.avg_rssi);
Ivo van Doorn84e31962008-12-20 10:53:29 +0100271
272 /*
Ivo van Doorn223dcc22010-07-11 12:25:17 +0200273 * Restore the VGC level as stored in the registers,
274 * the driver can use this to determine if the register
275 * must be updated during reset or not.
276 */
277 qual->vgc_level_reg = vgc_level;
278
279 /*
Ivo van Doorn84e31962008-12-20 10:53:29 +0100280 * Reset the link tuner.
281 */
Ivo van Doorn5352ff62008-12-20 10:54:54 +0100282 rt2x00dev->ops->lib->reset_tuner(rt2x00dev, qual);
Ivo van Doorn84e31962008-12-20 10:53:29 +0100283
284 if (antenna)
285 rt2x00link_antenna_reset(rt2x00dev);
286}
287
Ivo van Doorna57e2e82009-03-28 20:51:41 +0100288static void rt2x00link_reset_qual(struct rt2x00_dev *rt2x00dev)
Ivo van Doorn64abd802009-03-01 17:42:00 +0100289{
290 struct link_qual *qual = &rt2x00dev->link.qual;
291
292 qual->rx_success = 0;
293 qual->rx_failed = 0;
294 qual->tx_success = 0;
295 qual->tx_failed = 0;
296}
297
Stanislaw Gruszkad9632472016-12-19 11:52:54 +0100298static void rt2x00link_tuner_sta(struct rt2x00_dev *rt2x00dev, struct link *link)
Ivo van Doorn84e31962008-12-20 10:53:29 +0100299{
Ivo van Doorn84e31962008-12-20 10:53:29 +0100300 struct link_qual *qual = &rt2x00dev->link.qual;
301
302 /*
Ivo van Doorn84e31962008-12-20 10:53:29 +0100303 * Update statistics.
304 */
305 rt2x00dev->ops->lib->link_stats(rt2x00dev, qual);
306 rt2x00dev->low_level_stats.dot11FCSErrorCount += qual->rx_failed;
307
308 /*
Ivo van Doorn5352ff62008-12-20 10:54:54 +0100309 * Update quality RSSI for link tuning,
310 * when we have received some frames and we managed to
311 * collect the RSSI data we could use this. Otherwise we
312 * must fallback to the default RSSI value.
313 */
Gabor Juhos1dc254a2013-10-09 11:00:38 +0200314 if (!qual->rx_success)
Ivo van Doorn5352ff62008-12-20 10:54:54 +0100315 qual->rssi = DEFAULT_RSSI;
316 else
Gabor Juhos1dc254a2013-10-09 11:00:38 +0200317 qual->rssi = rt2x00link_get_avg_rssi(&link->avg_rssi);
Ivo van Doorn5352ff62008-12-20 10:54:54 +0100318
319 /*
Ivo van Doorn27df2a92010-07-11 12:24:22 +0200320 * Check if link tuning is supported by the hardware, some hardware
321 * do not support link tuning at all, while other devices can disable
322 * the feature from the EEPROM.
Ivo van Doorn84e31962008-12-20 10:53:29 +0100323 */
Gabor Juhos7b8a00d2013-10-11 13:18:41 +0200324 if (rt2x00_has_cap_link_tuning(rt2x00dev))
Ivo van Doorn5352ff62008-12-20 10:54:54 +0100325 rt2x00dev->ops->lib->link_tuner(rt2x00dev, qual, link->count);
Ivo van Doorn84e31962008-12-20 10:53:29 +0100326
327 /*
Ivo van Doorn84e31962008-12-20 10:53:29 +0100328 * Send a signal to the led to update the led signal strength.
329 */
Lars Ericsson66679a62009-08-08 23:54:51 +0200330 rt2x00leds_led_quality(rt2x00dev, qual->rssi);
Ivo van Doorn84e31962008-12-20 10:53:29 +0100331
332 /*
Lars Ericsson193df182009-08-08 23:54:24 +0200333 * Evaluate antenna setup, make this the last step when
334 * rt2x00lib_antenna_diversity made changes the quality
335 * statistics will be reset.
Ivo van Doorn84e31962008-12-20 10:53:29 +0100336 */
Lars Ericsson193df182009-08-08 23:54:24 +0200337 if (rt2x00lib_antenna_diversity(rt2x00dev))
338 rt2x00link_reset_qual(rt2x00dev);
Stanislaw Gruszkad9632472016-12-19 11:52:54 +0100339}
340
341static void rt2x00link_tuner(struct work_struct *work)
342{
343 struct rt2x00_dev *rt2x00dev =
344 container_of(work, struct rt2x00_dev, link.work.work);
345 struct link *link = &rt2x00dev->link;
346
347 /*
348 * When the radio is shutting down we should
349 * immediately cease all link tuning.
350 */
351 if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags) ||
352 test_bit(DEVICE_STATE_SCANNING, &rt2x00dev->flags))
353 return;
354
Stanislaw Gruszkac7d1c772016-12-19 11:52:57 +0100355 /* Do not race with rt2x00mac_config(). */
356 mutex_lock(&rt2x00dev->conf_mutex);
357
Stanislaw Gruszkad9632472016-12-19 11:52:54 +0100358 if (rt2x00dev->intf_sta_count)
359 rt2x00link_tuner_sta(rt2x00dev, link);
360
361 if (rt2x00dev->ops->lib->gain_calibration &&
362 (link->count % (AGC_SECONDS / LINK_TUNE_SECONDS)) == 0)
363 rt2x00dev->ops->lib->gain_calibration(rt2x00dev);
364
365 if (rt2x00dev->ops->lib->vco_calibration &&
366 rt2x00_has_cap_vco_recalibration(rt2x00dev) &&
367 (link->count % (VCO_SECONDS / LINK_TUNE_SECONDS)) == 0)
368 rt2x00dev->ops->lib->vco_calibration(rt2x00dev);
Ivo van Doorn64abd802009-03-01 17:42:00 +0100369
Stanislaw Gruszkac7d1c772016-12-19 11:52:57 +0100370 mutex_unlock(&rt2x00dev->conf_mutex);
371
Ivo van Doorn64abd802009-03-01 17:42:00 +0100372 /*
Ivo van Doorn84e31962008-12-20 10:53:29 +0100373 * Increase tuner counter, and reschedule the next link tuner run.
374 */
Ivo van Doorn5352ff62008-12-20 10:54:54 +0100375 link->count++;
Sean Cross66f84d62009-11-05 20:22:03 +0100376
377 if (test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
378 ieee80211_queue_delayed_work(rt2x00dev->hw,
379 &link->work, LINK_TUNE_INTERVAL);
Ivo van Doorn84e31962008-12-20 10:53:29 +0100380}
381
Ivo van Doornc965c742010-07-11 12:25:46 +0200382void rt2x00link_start_watchdog(struct rt2x00_dev *rt2x00dev)
383{
384 struct link *link = &rt2x00dev->link;
385
Helmut Schaaaca7305b2011-03-28 13:30:59 +0200386 if (test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags) &&
387 rt2x00dev->ops->lib->watchdog)
388 ieee80211_queue_delayed_work(rt2x00dev->hw,
389 &link->watchdog_work,
390 WATCHDOG_INTERVAL);
Ivo van Doornc965c742010-07-11 12:25:46 +0200391}
392
393void rt2x00link_stop_watchdog(struct rt2x00_dev *rt2x00dev)
394{
395 cancel_delayed_work_sync(&rt2x00dev->link.watchdog_work);
396}
397
398static void rt2x00link_watchdog(struct work_struct *work)
399{
400 struct rt2x00_dev *rt2x00dev =
401 container_of(work, struct rt2x00_dev, link.watchdog_work.work);
402 struct link *link = &rt2x00dev->link;
403
404 /*
405 * When the radio is shutting down we should
406 * immediately cease the watchdog monitoring.
407 */
408 if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
409 return;
410
411 rt2x00dev->ops->lib->watchdog(rt2x00dev);
412
413 if (test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
Ivo van Doorn0439f532011-01-30 13:24:05 +0100414 ieee80211_queue_delayed_work(rt2x00dev->hw,
415 &link->watchdog_work,
416 WATCHDOG_INTERVAL);
Ivo van Doornc965c742010-07-11 12:25:46 +0200417}
418
Ivo van Doorn84e31962008-12-20 10:53:29 +0100419void rt2x00link_register(struct rt2x00_dev *rt2x00dev)
420{
Ivo van Doornc965c742010-07-11 12:25:46 +0200421 INIT_DELAYED_WORK(&rt2x00dev->link.watchdog_work, rt2x00link_watchdog);
Ivo van Doorn84e31962008-12-20 10:53:29 +0100422 INIT_DELAYED_WORK(&rt2x00dev->link.work, rt2x00link_tuner);
423}