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 << RC_PID_ARITH_SHIFT)
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 void rate_control_pid_event_tx_status(struct rc_pid_event_buffer
*buf
,
123 struct ieee80211_tx_status
*stat
);
125 void rate_control_pid_event_rate_change(struct rc_pid_event_buffer
*buf
,
126 int index
, int rate
);
128 void rate_control_pid_event_tx_rate(struct rc_pid_event_buffer
*buf
,
129 int index
, int rate
);
131 void rate_control_pid_event_pf_sample(struct rc_pid_event_buffer
*buf
,
132 s32 pf_sample
, s32 prop_err
,
133 s32 int_err
, s32 der_err
);
135 void rate_control_pid_add_sta_debugfs(void *priv
, void *priv_sta
,
138 void rate_control_pid_remove_sta_debugfs(void *priv
, void *priv_sta
);
140 struct rc_pid_sta_info
{
141 unsigned long last_change
;
142 unsigned long last_sample
;
147 /* Average failed frames percentage error (i.e. actual vs. target
148 * percentage), scaled by RC_PID_SMOOTHING. This value is computed
149 * using using an exponential weighted average technique:
151 * (RC_PID_SMOOTHING - 1) * err_avg_old + err
152 * err_avg = ------------------------------------------
155 * where err_avg is the new approximation, err_avg_old the previous one
156 * and err is the error w.r.t. to the current failed frames percentage
157 * sample. Note that the bigger RC_PID_SMOOTHING the more weight is
158 * given to the previous estimate, resulting in smoother behavior (i.e.
159 * corresponding to a longer integration window).
161 * For computation, we actually don't use the above formula, but this
164 * err_avg_scaled = err_avg_old_scaled - err_avg_old + err
167 * err_avg_scaled = err * RC_PID_SMOOTHING
168 * err_avg_old_scaled = err_avg_old * RC_PID_SMOOTHING
170 * This avoids floating point numbers and the per_failed_old value can
171 * easily be obtained by shifting per_failed_old_scaled right by
172 * RC_PID_SMOOTHING_SHIFT.
176 /* Last framed failes percentage sample. */
179 /* Sharpening needed. */
182 #ifdef CONFIG_MAC80211_DEBUGFS
184 struct rc_pid_event_buffer events
;
186 /* Events debugfs file entry */
187 struct dentry
*events_entry
;
191 /* Algorithm parameters. We keep them on a per-algorithm approach, so they can
192 * be tuned individually for each interface.
194 struct rc_pid_rateinfo
{
196 /* Map sorted rates to rates in ieee80211_hw_mode. */
199 /* Map rates in ieee80211_hw_mode to sorted rates. */
202 /* Did we do any measurement on this rate? */
205 /* Comparison with the lowest rate. */
211 /* The failed frames percentage target. */
214 /* Rate at which failed frames percentage is sampled in 0.001s. */
215 unsigned int sampling_period
;
217 /* P, I and D coefficients. */
222 /* Exponential averaging shift. */
223 unsigned int smoothing_shift
;
225 /* Sharpening shift and duration. */
226 unsigned int sharpen_shift
;
227 unsigned int sharpen_duration
;
229 /* Normalization offset. */
230 unsigned int norm_offset
;
232 /* Fast starst parameter. */
233 unsigned int fast_start
;
235 /* Rates information. */
236 struct rc_pid_rateinfo
*rinfo
;
238 /* Index of the last used rate. */
242 #endif /* RC80211_PID_H */