2 * Copyright 2007, Mattias Nissler <mattias.nissler@gmx.de>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
12 /* Sampling period for measuring percentage of failed frames. */
13 #define RC_PID_INTERVAL (HZ / 8)
15 /* Exponential averaging smoothness (used for I part of PID controller) */
16 #define RC_PID_SMOOTHING_SHIFT 3
17 #define RC_PID_SMOOTHING (1 << RC_PID_SMOOTHING_SHIFT)
19 /* Sharpening factor (used for D part of PID controller) */
20 #define RC_PID_SHARPENING_FACTOR 0
21 #define RC_PID_SHARPENING_DURATION 0
23 /* Fixed point arithmetic shifting amount. */
24 #define RC_PID_ARITH_SHIFT 8
26 /* Fixed point arithmetic factor. */
27 #define RC_PID_ARITH_FACTOR (1 << RC_PID_ARITH_SHIFT)
29 /* Proportional PID component coefficient. */
30 #define RC_PID_COEFF_P 15
31 /* Integral PID component coefficient. */
32 #define RC_PID_COEFF_I 9
33 /* Derivative PID component coefficient. */
34 #define RC_PID_COEFF_D 15
36 /* Target failed frames rate for the PID controller. NB: This effectively gives
37 * maximum failed frames percentage we're willing to accept. If the wireless
38 * link quality is good, the controller will fail to adjust failed frames
39 * percentage to the target. This is intentional.
41 #define RC_PID_TARGET_PF 11
43 /* Rate behaviour normalization quantity over time. */
44 #define RC_PID_NORM_OFFSET 3
46 /* Push high rates right after loading. */
47 #define RC_PID_FAST_START 0
49 /* Arithmetic right shift for positive and negative values for ISO C. */
50 #define RC_PID_DO_ARITH_RIGHT_SHIFT(x, y) \
51 (x) < 0 ? -((-(x)) >> (y)) : (x) >> (y)
53 enum rc_pid_event_type {
54 RC_PID_EVENT_TYPE_TX_STATUS,
55 RC_PID_EVENT_TYPE_RATE_CHANGE,
56 RC_PID_EVENT_TYPE_TX_RATE,
57 RC_PID_EVENT_TYPE_PF_SAMPLE,
60 union rc_pid_event_data {
61 /* RC_PID_EVENT_TX_STATUS */
63 struct ieee80211_tx_status tx_status;
65 /* RC_PID_EVENT_TYPE_RATE_CHANGE */
66 /* RC_PID_EVENT_TYPE_TX_RATE */
71 /* RC_PID_EVENT_TYPE_PF_SAMPLE */
81 /* The time when the event occured */
82 unsigned long timestamp;
88 enum rc_pid_event_type type;
90 /* type specific data */
91 union rc_pid_event_data data;
94 /* Size of the event ring buffer. */
95 #define RC_PID_EVENT_RING_SIZE 32
97 struct rc_pid_event_buffer {
98 /* Counter that generates event IDs */
99 unsigned int ev_count;
101 /* Ring buffer of events */
102 struct rc_pid_event ring[RC_PID_EVENT_RING_SIZE];
104 /* Index to the entry in events_buf to be reused */
105 unsigned int next_entry;
107 /* Lock that guards against concurrent access to this buffer struct */
110 /* Wait queue for poll/select and blocking I/O */
111 wait_queue_head_t waitqueue;
114 struct rc_pid_events_file_info {
115 /* The event buffer we read */
116 struct rc_pid_event_buffer *events;
118 /* The entry we have should read next */
119 unsigned int next_entry;
122 struct rc_pid_debugfs_entries {
124 struct dentry *target;
125 struct dentry *sampling_period;
126 struct dentry *coeff_p;
127 struct dentry *coeff_i;
128 struct dentry *coeff_d;
129 struct dentry *smoothing_shift;
130 struct dentry *sharpen_factor;
131 struct dentry *sharpen_duration;
132 struct dentry *norm_offset;
133 struct dentry *fast_start;
136 void rate_control_pid_event_tx_status(struct rc_pid_event_buffer *buf,
137 struct ieee80211_tx_status *stat);
139 void rate_control_pid_event_rate_change(struct rc_pid_event_buffer *buf,
140 int index, int rate);
142 void rate_control_pid_event_tx_rate(struct rc_pid_event_buffer *buf,
143 int index, int rate);
145 void rate_control_pid_event_pf_sample(struct rc_pid_event_buffer *buf,
146 s32 pf_sample, s32 prop_err,
147 s32 int_err, s32 der_err);
149 void rate_control_pid_add_sta_debugfs(void *priv, void *priv_sta,
152 void rate_control_pid_remove_sta_debugfs(void *priv, void *priv_sta);
154 struct rc_pid_sta_info {
155 unsigned long last_change;
156 unsigned long last_sample;
161 /* Average failed frames percentage error (i.e. actual vs. target
162 * percentage), scaled by RC_PID_SMOOTHING. This value is computed
163 * using using an exponential weighted average technique:
165 * (RC_PID_SMOOTHING - 1) * err_avg_old + err
166 * err_avg = ------------------------------------------
169 * where err_avg is the new approximation, err_avg_old the previous one
170 * and err is the error w.r.t. to the current failed frames percentage
171 * sample. Note that the bigger RC_PID_SMOOTHING the more weight is
172 * given to the previous estimate, resulting in smoother behavior (i.e.
173 * corresponding to a longer integration window).
175 * For computation, we actually don't use the above formula, but this
178 * err_avg_scaled = err_avg_old_scaled - err_avg_old + err
181 * err_avg_scaled = err * RC_PID_SMOOTHING
182 * err_avg_old_scaled = err_avg_old * RC_PID_SMOOTHING
184 * This avoids floating point numbers and the per_failed_old value can
185 * easily be obtained by shifting per_failed_old_scaled right by
186 * RC_PID_SMOOTHING_SHIFT.
190 /* Last framed failes percentage sample. */
193 /* Sharpening needed. */
196 #ifdef CONFIG_MAC80211_DEBUGFS
198 struct rc_pid_event_buffer events;
200 /* Events debugfs file entry */
201 struct dentry *events_entry;
205 /* Algorithm parameters. We keep them on a per-algorithm approach, so they can
206 * be tuned individually for each interface.
208 struct rc_pid_rateinfo {
210 /* Map sorted rates to rates in ieee80211_hw_mode. */
213 /* Map rates in ieee80211_hw_mode to sorted rates. */
216 /* Did we do any measurement on this rate? */
219 /* Comparison with the lowest rate. */
225 /* The failed frames percentage target. */
228 /* Rate at which failed frames percentage is sampled in 0.001s. */
229 unsigned int sampling_period;
231 /* P, I and D coefficients. */
236 /* Exponential averaging shift. */
237 unsigned int smoothing_shift;
239 /* Sharpening factor and duration. */
240 unsigned int sharpen_factor;
241 unsigned int sharpen_duration;
243 /* Normalization offset. */
244 unsigned int norm_offset;
246 /* Fast starst parameter. */
247 unsigned int fast_start;
249 /* Rates information. */
250 struct rc_pid_rateinfo *rinfo;
252 /* Index of the last used rate. */
255 #ifdef CONFIG_MAC80211_DEBUGFS
256 /* Debugfs entries created for the parameters above. */
257 struct rc_pid_debugfs_entries dentries;
261 #endif /* RC80211_PID_H */