x86/amd-iommu: Add per IOMMU reference counting
[linux/fpc-iii.git] / drivers / net / wireless / rt2x00 / rt2x00link.c
blobc708d0be9155066d0b2edbf13e1d797b49510bd8
1 /*
2 Copyright (C) 2004 - 2009 rt2x00 SourceForge Project
3 <http://rt2x00.serialmonkey.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the
17 Free Software Foundation, Inc.,
18 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 Module: rt2x00lib
23 Abstract: rt2x00 generic link tuning routines.
26 #include <linux/kernel.h>
27 #include <linux/module.h>
29 #include "rt2x00.h"
30 #include "rt2x00lib.h"
33 * When we lack RSSI information return something less then -80 to
34 * tell the driver to tune the device to maximum sensitivity.
36 #define DEFAULT_RSSI -128
39 * When no TX/RX percentage could be calculated due to lack of
40 * frames on the air, we fallback to a percentage of 50%.
41 * This will assure we will get at least get some decent value
42 * when the link tuner starts.
43 * The value will be dropped and overwritten with the correct (measured)
44 * value anyway during the first run of the link tuner.
46 #define DEFAULT_PERCENTAGE 50
49 * Small helper macro for percentage calculation
50 * This is a very simple macro with the only catch that it will
51 * produce a default value in case no total value was provided.
53 #define PERCENTAGE(__value, __total) \
54 ( (__total) ? (((__value) * 100) / (__total)) : (DEFAULT_PERCENTAGE) )
57 * Helper struct and macro to work with moving/walking averages.
58 * When adding a value to the average value the following calculation
59 * is needed:
61 * avg_rssi = ((avg_rssi * 7) + rssi) / 8;
63 * The advantage of this approach is that we only need 1 variable
64 * to store the average in (No need for a count and a total).
65 * But more importantly, normal average values will over time
66 * move less and less towards newly added values this results
67 * that with link tuning, the device can have a very good RSSI
68 * for a few minutes but when the device is moved away from the AP
69 * the average will not decrease fast enough to compensate.
70 * The walking average compensates this and will move towards
71 * the new values correctly allowing a effective link tuning,
72 * the speed of the average moving towards other values depends
73 * on the value for the number of samples. The higher the number
74 * of samples, the slower the average will move.
75 * We use two variables to keep track of the average value to
76 * compensate for the rounding errors. This can be a significant
77 * error (>5dBm) if the factor is too low.
79 #define AVG_SAMPLES 8
80 #define AVG_FACTOR 1000
81 #define MOVING_AVERAGE(__avg, __val) \
82 ({ \
83 struct avg_val __new; \
84 __new.avg_weight = \
85 (__avg).avg_weight ? \
86 ((((__avg).avg_weight * ((AVG_SAMPLES) - 1)) + \
87 ((__val) * (AVG_FACTOR))) / \
88 (AVG_SAMPLES) ) : \
89 ((__val) * (AVG_FACTOR)); \
90 __new.avg = __new.avg_weight / (AVG_FACTOR); \
91 __new; \
95 * For calculating the Signal quality we have determined
96 * the total number of success and failed RX and TX frames.
97 * With the addition of the average RSSI value we can determine
98 * the link quality using the following algorithm:
100 * rssi_percentage = (avg_rssi * 100) / rssi_offset
101 * rx_percentage = (rx_success * 100) / rx_total
102 * tx_percentage = (tx_success * 100) / tx_total
103 * avg_signal = ((WEIGHT_RSSI * avg_rssi) +
104 * (WEIGHT_TX * tx_percentage) +
105 * (WEIGHT_RX * rx_percentage)) / 100
107 * This value should then be checked to not be greater then 100.
108 * This means the values of WEIGHT_RSSI, WEIGHT_RX, WEIGHT_TX must
109 * sum up to 100 as well.
111 #define WEIGHT_RSSI 20
112 #define WEIGHT_RX 40
113 #define WEIGHT_TX 40
115 static int rt2x00link_antenna_get_link_rssi(struct rt2x00_dev *rt2x00dev)
117 struct link_ant *ant = &rt2x00dev->link.ant;
119 if (ant->rssi_ant.avg && rt2x00dev->link.qual.rx_success)
120 return ant->rssi_ant.avg;
121 return DEFAULT_RSSI;
124 static int rt2x00link_antenna_get_rssi_history(struct rt2x00_dev *rt2x00dev)
126 struct link_ant *ant = &rt2x00dev->link.ant;
128 if (ant->rssi_history)
129 return ant->rssi_history;
130 return DEFAULT_RSSI;
133 static void rt2x00link_antenna_update_rssi_history(struct rt2x00_dev *rt2x00dev,
134 int rssi)
136 struct link_ant *ant = &rt2x00dev->link.ant;
137 ant->rssi_history = rssi;
140 static void rt2x00link_antenna_reset(struct rt2x00_dev *rt2x00dev)
142 rt2x00dev->link.ant.rssi_ant.avg = 0;
143 rt2x00dev->link.ant.rssi_ant.avg_weight = 0;
146 static void rt2x00lib_antenna_diversity_sample(struct rt2x00_dev *rt2x00dev)
148 struct link_ant *ant = &rt2x00dev->link.ant;
149 struct antenna_setup new_ant;
150 int other_antenna;
152 int sample_current = rt2x00link_antenna_get_link_rssi(rt2x00dev);
153 int sample_other = rt2x00link_antenna_get_rssi_history(rt2x00dev);
155 memcpy(&new_ant, &ant->active, sizeof(new_ant));
158 * We are done sampling. Now we should evaluate the results.
160 ant->flags &= ~ANTENNA_MODE_SAMPLE;
163 * During the last period we have sampled the RSSI
164 * from both antennas. It now is time to determine
165 * which antenna demonstrated the best performance.
166 * When we are already on the antenna with the best
167 * performance, just create a good starting point
168 * for the history and we are done.
170 if (sample_current >= sample_other) {
171 rt2x00link_antenna_update_rssi_history(rt2x00dev,
172 sample_current);
173 return;
176 other_antenna = (ant->active.rx == ANTENNA_A) ? ANTENNA_B : ANTENNA_A;
178 if (ant->flags & ANTENNA_RX_DIVERSITY)
179 new_ant.rx = other_antenna;
181 if (ant->flags & ANTENNA_TX_DIVERSITY)
182 new_ant.tx = other_antenna;
184 rt2x00lib_config_antenna(rt2x00dev, new_ant);
187 static void rt2x00lib_antenna_diversity_eval(struct rt2x00_dev *rt2x00dev)
189 struct link_ant *ant = &rt2x00dev->link.ant;
190 struct antenna_setup new_ant;
191 int rssi_curr;
192 int rssi_old;
194 memcpy(&new_ant, &ant->active, sizeof(new_ant));
197 * Get current RSSI value along with the historical value,
198 * after that update the history with the current value.
200 rssi_curr = rt2x00link_antenna_get_link_rssi(rt2x00dev);
201 rssi_old = rt2x00link_antenna_get_rssi_history(rt2x00dev);
202 rt2x00link_antenna_update_rssi_history(rt2x00dev, rssi_curr);
205 * Legacy driver indicates that we should swap antenna's
206 * when the difference in RSSI is greater that 5. This
207 * also should be done when the RSSI was actually better
208 * then the previous sample.
209 * When the difference exceeds the threshold we should
210 * sample the rssi from the other antenna to make a valid
211 * comparison between the 2 antennas.
213 if (abs(rssi_curr - rssi_old) < 5)
214 return;
216 ant->flags |= ANTENNA_MODE_SAMPLE;
218 if (ant->flags & ANTENNA_RX_DIVERSITY)
219 new_ant.rx = (new_ant.rx == ANTENNA_A) ? ANTENNA_B : ANTENNA_A;
221 if (ant->flags & ANTENNA_TX_DIVERSITY)
222 new_ant.tx = (new_ant.tx == ANTENNA_A) ? ANTENNA_B : ANTENNA_A;
224 rt2x00lib_config_antenna(rt2x00dev, new_ant);
227 static bool rt2x00lib_antenna_diversity(struct rt2x00_dev *rt2x00dev)
229 struct link_ant *ant = &rt2x00dev->link.ant;
230 unsigned int flags = ant->flags;
233 * Determine if software diversity is enabled for
234 * either the TX or RX antenna (or both).
235 * Always perform this check since within the link
236 * tuner interval the configuration might have changed.
238 flags &= ~ANTENNA_RX_DIVERSITY;
239 flags &= ~ANTENNA_TX_DIVERSITY;
241 if (rt2x00dev->default_ant.rx == ANTENNA_SW_DIVERSITY)
242 flags |= ANTENNA_RX_DIVERSITY;
243 if (rt2x00dev->default_ant.tx == ANTENNA_SW_DIVERSITY)
244 flags |= ANTENNA_TX_DIVERSITY;
246 if (!(ant->flags & ANTENNA_RX_DIVERSITY) &&
247 !(ant->flags & ANTENNA_TX_DIVERSITY)) {
248 ant->flags = 0;
249 return true;
252 /* Update flags */
253 ant->flags = flags;
256 * If we have only sampled the data over the last period
257 * we should now harvest the data. Otherwise just evaluate
258 * the data. The latter should only be performed once
259 * every 2 seconds.
261 if (ant->flags & ANTENNA_MODE_SAMPLE) {
262 rt2x00lib_antenna_diversity_sample(rt2x00dev);
263 return true;
264 } else if (rt2x00dev->link.count & 1) {
265 rt2x00lib_antenna_diversity_eval(rt2x00dev);
266 return true;
269 return false;
272 void rt2x00link_update_stats(struct rt2x00_dev *rt2x00dev,
273 struct sk_buff *skb,
274 struct rxdone_entry_desc *rxdesc)
276 struct link *link = &rt2x00dev->link;
277 struct link_qual *qual = &rt2x00dev->link.qual;
278 struct link_ant *ant = &rt2x00dev->link.ant;
279 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
282 * Frame was received successfully since non-succesfull
283 * frames would have been dropped by the hardware.
285 qual->rx_success++;
288 * We are only interested in quality statistics from
289 * beacons which came from the BSS which we are
290 * associated with.
292 if (!ieee80211_is_beacon(hdr->frame_control) ||
293 !(rxdesc->dev_flags & RXDONE_MY_BSS))
294 return;
297 * Update global RSSI
299 link->avg_rssi = MOVING_AVERAGE(link->avg_rssi, rxdesc->rssi);
302 * Update antenna RSSI
304 ant->rssi_ant = MOVING_AVERAGE(ant->rssi_ant, rxdesc->rssi);
307 static void rt2x00link_precalculate_signal(struct rt2x00_dev *rt2x00dev)
309 struct link *link = &rt2x00dev->link;
310 struct link_qual *qual = &rt2x00dev->link.qual;
312 link->rx_percentage =
313 PERCENTAGE(qual->rx_success, qual->rx_failed + qual->rx_success);
314 link->tx_percentage =
315 PERCENTAGE(qual->tx_success, qual->tx_failed + qual->tx_success);
318 int rt2x00link_calculate_signal(struct rt2x00_dev *rt2x00dev, int rssi)
320 struct link *link = &rt2x00dev->link;
321 int rssi_percentage = 0;
322 int signal;
325 * We need a positive value for the RSSI.
327 if (rssi < 0)
328 rssi += rt2x00dev->rssi_offset;
331 * Calculate the different percentages,
332 * which will be used for the signal.
334 rssi_percentage = PERCENTAGE(rssi, rt2x00dev->rssi_offset);
337 * Add the individual percentages and use the WEIGHT
338 * defines to calculate the current link signal.
340 signal = ((WEIGHT_RSSI * rssi_percentage) +
341 (WEIGHT_TX * link->tx_percentage) +
342 (WEIGHT_RX * link->rx_percentage)) / 100;
344 return max_t(int, signal, 100);
347 void rt2x00link_start_tuner(struct rt2x00_dev *rt2x00dev)
349 struct link *link = &rt2x00dev->link;
352 * Link tuning should only be performed when
353 * an active sta or master interface exists.
354 * Single monitor mode interfaces should never have
355 * work with link tuners.
357 if (!rt2x00dev->intf_ap_count && !rt2x00dev->intf_sta_count)
358 return;
360 link->rx_percentage = DEFAULT_PERCENTAGE;
361 link->tx_percentage = DEFAULT_PERCENTAGE;
363 rt2x00link_reset_tuner(rt2x00dev, false);
365 if (test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
366 ieee80211_queue_delayed_work(rt2x00dev->hw,
367 &link->work, LINK_TUNE_INTERVAL);
370 void rt2x00link_stop_tuner(struct rt2x00_dev *rt2x00dev)
372 cancel_delayed_work_sync(&rt2x00dev->link.work);
375 void rt2x00link_reset_tuner(struct rt2x00_dev *rt2x00dev, bool antenna)
377 struct link_qual *qual = &rt2x00dev->link.qual;
379 if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
380 return;
383 * Reset link information.
384 * Both the currently active vgc level as well as
385 * the link tuner counter should be reset. Resetting
386 * the counter is important for devices where the
387 * device should only perform link tuning during the
388 * first minute after being enabled.
390 rt2x00dev->link.count = 0;
391 memset(qual, 0, sizeof(*qual));
394 * Reset the link tuner.
396 rt2x00dev->ops->lib->reset_tuner(rt2x00dev, qual);
398 if (antenna)
399 rt2x00link_antenna_reset(rt2x00dev);
402 static void rt2x00link_reset_qual(struct rt2x00_dev *rt2x00dev)
404 struct link_qual *qual = &rt2x00dev->link.qual;
406 qual->rx_success = 0;
407 qual->rx_failed = 0;
408 qual->tx_success = 0;
409 qual->tx_failed = 0;
412 static void rt2x00link_tuner(struct work_struct *work)
414 struct rt2x00_dev *rt2x00dev =
415 container_of(work, struct rt2x00_dev, link.work.work);
416 struct link *link = &rt2x00dev->link;
417 struct link_qual *qual = &rt2x00dev->link.qual;
420 * When the radio is shutting down we should
421 * immediately cease all link tuning.
423 if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
424 return;
427 * Update statistics.
429 rt2x00dev->ops->lib->link_stats(rt2x00dev, qual);
430 rt2x00dev->low_level_stats.dot11FCSErrorCount += qual->rx_failed;
433 * Update quality RSSI for link tuning,
434 * when we have received some frames and we managed to
435 * collect the RSSI data we could use this. Otherwise we
436 * must fallback to the default RSSI value.
438 if (!link->avg_rssi.avg || !qual->rx_success)
439 qual->rssi = DEFAULT_RSSI;
440 else
441 qual->rssi = link->avg_rssi.avg;
444 * Only perform the link tuning when Link tuning
445 * has been enabled (This could have been disabled from the EEPROM).
447 if (!test_bit(CONFIG_DISABLE_LINK_TUNING, &rt2x00dev->flags))
448 rt2x00dev->ops->lib->link_tuner(rt2x00dev, qual, link->count);
451 * Precalculate a portion of the link signal which is
452 * in based on the tx/rx success/failure counters.
454 rt2x00link_precalculate_signal(rt2x00dev);
457 * Send a signal to the led to update the led signal strength.
459 rt2x00leds_led_quality(rt2x00dev, qual->rssi);
462 * Evaluate antenna setup, make this the last step when
463 * rt2x00lib_antenna_diversity made changes the quality
464 * statistics will be reset.
466 if (rt2x00lib_antenna_diversity(rt2x00dev))
467 rt2x00link_reset_qual(rt2x00dev);
470 * Increase tuner counter, and reschedule the next link tuner run.
472 link->count++;
474 if (test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
475 ieee80211_queue_delayed_work(rt2x00dev->hw,
476 &link->work, LINK_TUNE_INTERVAL);
479 void rt2x00link_register(struct rt2x00_dev *rt2x00dev)
481 INIT_DELAYED_WORK(&rt2x00dev->link.work, rt2x00link_tuner);