Commit | Line | Data |
---|---|---|
b481de9c ZY |
1 | /****************************************************************************** |
2 | * | |
eb7ae89c | 3 | * Copyright(c) 2005 - 2008 Intel Corporation. All rights reserved. |
b481de9c ZY |
4 | * |
5 | * This program is free software; you can redistribute it and/or modify it | |
6 | * under the terms of version 2 of the GNU General Public License as | |
7 | * published by the Free Software Foundation. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, but WITHOUT | |
10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
12 | * more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License along with | |
15 | * this program; if not, write to the Free Software Foundation, Inc., | |
16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA | |
17 | * | |
18 | * The full GNU General Public License is included in this distribution in the | |
19 | * file called LICENSE. | |
20 | * | |
21 | * Contact Information: | |
22 | * James P. Ketrenos <ipw2100-admin@linux.intel.com> | |
23 | * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497 | |
24 | * | |
25 | *****************************************************************************/ | |
26 | ||
27 | #include <linux/kernel.h> | |
28 | #include <linux/init.h> | |
29 | #include <linux/skbuff.h> | |
30 | #include <linux/wireless.h> | |
31 | #include <net/mac80211.h> | |
b481de9c ZY |
32 | |
33 | #include <linux/netdevice.h> | |
34 | #include <linux/etherdevice.h> | |
35 | #include <linux/delay.h> | |
36 | ||
37 | #include <linux/workqueue.h> | |
38 | ||
5d08cd1d | 39 | #include "iwl-3945.h" |
b481de9c ZY |
40 | |
41 | #define RS_NAME "iwl-3945-rs" | |
42 | ||
bb8c093b | 43 | struct iwl3945_rate_scale_data { |
b481de9c ZY |
44 | u64 data; |
45 | s32 success_counter; | |
46 | s32 success_ratio; | |
47 | s32 counter; | |
48 | s32 average_tpt; | |
49 | unsigned long stamp; | |
50 | }; | |
51 | ||
fa254a68 | 52 | struct iwl3945_rs_sta { |
b481de9c ZY |
53 | spinlock_t lock; |
54 | s32 *expected_tpt; | |
55 | unsigned long last_partial_flush; | |
56 | unsigned long last_flush; | |
57 | u32 flush_time; | |
58 | u32 last_tx_packets; | |
59 | u32 tx_packets; | |
60 | u8 tgg; | |
61 | u8 flush_pending; | |
62 | u8 start_rate; | |
63 | u8 ibss_sta_added; | |
64 | struct timer_list rate_scale_flush; | |
bb8c093b | 65 | struct iwl3945_rate_scale_data win[IWL_RATE_COUNT]; |
b7e35008 JB |
66 | |
67 | /* used to be in sta_info */ | |
68 | int last_txrate_idx; | |
b481de9c ZY |
69 | }; |
70 | ||
bb8c093b | 71 | static s32 iwl3945_expected_tpt_g[IWL_RATE_COUNT] = { |
14577f23 | 72 | 7, 13, 35, 58, 0, 0, 76, 104, 130, 168, 191, 202 |
b481de9c ZY |
73 | }; |
74 | ||
bb8c093b | 75 | static s32 iwl3945_expected_tpt_g_prot[IWL_RATE_COUNT] = { |
14577f23 | 76 | 7, 13, 35, 58, 0, 0, 0, 80, 93, 113, 123, 125 |
b481de9c ZY |
77 | }; |
78 | ||
bb8c093b | 79 | static s32 iwl3945_expected_tpt_a[IWL_RATE_COUNT] = { |
14577f23 | 80 | 0, 0, 0, 0, 40, 57, 72, 98, 121, 154, 177, 186 |
b481de9c ZY |
81 | }; |
82 | ||
bb8c093b | 83 | static s32 iwl3945_expected_tpt_b[IWL_RATE_COUNT] = { |
14577f23 | 84 | 7, 13, 35, 58, 0, 0, 0, 0, 0, 0, 0, 0 |
b481de9c ZY |
85 | }; |
86 | ||
bb8c093b | 87 | struct iwl3945_tpt_entry { |
b481de9c ZY |
88 | s8 min_rssi; |
89 | u8 index; | |
90 | }; | |
91 | ||
bb8c093b | 92 | static struct iwl3945_tpt_entry iwl3945_tpt_table_a[] = { |
b481de9c ZY |
93 | {-60, IWL_RATE_54M_INDEX}, |
94 | {-64, IWL_RATE_48M_INDEX}, | |
95 | {-72, IWL_RATE_36M_INDEX}, | |
96 | {-80, IWL_RATE_24M_INDEX}, | |
97 | {-84, IWL_RATE_18M_INDEX}, | |
98 | {-85, IWL_RATE_12M_INDEX}, | |
99 | {-87, IWL_RATE_9M_INDEX}, | |
100 | {-89, IWL_RATE_6M_INDEX} | |
101 | }; | |
102 | ||
bb8c093b | 103 | static struct iwl3945_tpt_entry iwl3945_tpt_table_g[] = { |
b481de9c ZY |
104 | {-60, IWL_RATE_54M_INDEX}, |
105 | {-64, IWL_RATE_48M_INDEX}, | |
106 | {-68, IWL_RATE_36M_INDEX}, | |
107 | {-80, IWL_RATE_24M_INDEX}, | |
108 | {-84, IWL_RATE_18M_INDEX}, | |
109 | {-85, IWL_RATE_12M_INDEX}, | |
110 | {-86, IWL_RATE_11M_INDEX}, | |
111 | {-88, IWL_RATE_5M_INDEX}, | |
112 | {-90, IWL_RATE_2M_INDEX}, | |
113 | {-92, IWL_RATE_1M_INDEX} | |
114 | }; | |
115 | ||
116 | #define IWL_RATE_MAX_WINDOW 62 | |
117 | #define IWL_RATE_FLUSH (3*HZ/10) | |
118 | #define IWL_RATE_WIN_FLUSH (HZ/2) | |
119 | #define IWL_RATE_HIGH_TH 11520 | |
120 | #define IWL_RATE_MIN_FAILURE_TH 8 | |
121 | #define IWL_RATE_MIN_SUCCESS_TH 8 | |
122 | #define IWL_RATE_DECREASE_TH 1920 | |
123 | ||
8318d78a | 124 | static u8 iwl3945_get_rate_index_by_rssi(s32 rssi, enum ieee80211_band band) |
b481de9c ZY |
125 | { |
126 | u32 index = 0; | |
127 | u32 table_size = 0; | |
bb8c093b | 128 | struct iwl3945_tpt_entry *tpt_table = NULL; |
b481de9c ZY |
129 | |
130 | if ((rssi < IWL_MIN_RSSI_VAL) || (rssi > IWL_MAX_RSSI_VAL)) | |
131 | rssi = IWL_MIN_RSSI_VAL; | |
132 | ||
8318d78a JB |
133 | switch (band) { |
134 | case IEEE80211_BAND_2GHZ: | |
bb8c093b CH |
135 | tpt_table = iwl3945_tpt_table_g; |
136 | table_size = ARRAY_SIZE(iwl3945_tpt_table_g); | |
b481de9c ZY |
137 | break; |
138 | ||
8318d78a | 139 | case IEEE80211_BAND_5GHZ: |
bb8c093b CH |
140 | tpt_table = iwl3945_tpt_table_a; |
141 | table_size = ARRAY_SIZE(iwl3945_tpt_table_a); | |
b481de9c ZY |
142 | break; |
143 | ||
144 | default: | |
8318d78a | 145 | BUG(); |
b481de9c ZY |
146 | break; |
147 | } | |
148 | ||
149 | while ((index < table_size) && (rssi < tpt_table[index].min_rssi)) | |
150 | index++; | |
151 | ||
152 | index = min(index, (table_size - 1)); | |
153 | ||
154 | return tpt_table[index].index; | |
155 | } | |
156 | ||
bb8c093b | 157 | static void iwl3945_clear_window(struct iwl3945_rate_scale_data *window) |
b481de9c ZY |
158 | { |
159 | window->data = 0; | |
160 | window->success_counter = 0; | |
28447f3c | 161 | window->success_ratio = -1; |
b481de9c | 162 | window->counter = 0; |
28447f3c | 163 | window->average_tpt = IWL_INV_TPT; |
b481de9c ZY |
164 | window->stamp = 0; |
165 | } | |
166 | ||
167 | /** | |
bb8c093b | 168 | * iwl3945_rate_scale_flush_windows - flush out the rate scale windows |
b481de9c ZY |
169 | * |
170 | * Returns the number of windows that have gathered data but were | |
171 | * not flushed. If there were any that were not flushed, then | |
172 | * reschedule the rate flushing routine. | |
173 | */ | |
fa254a68 | 174 | static int iwl3945_rate_scale_flush_windows(struct iwl3945_rs_sta *rs_sta) |
b481de9c ZY |
175 | { |
176 | int unflushed = 0; | |
177 | int i; | |
178 | unsigned long flags; | |
179 | ||
180 | /* | |
181 | * For each rate, if we have collected data on that rate | |
182 | * and it has been more than IWL_RATE_WIN_FLUSH | |
183 | * since we flushed, clear out the gathered statistics | |
184 | */ | |
185 | for (i = 0; i < IWL_RATE_COUNT; i++) { | |
fa254a68 | 186 | if (!rs_sta->win[i].counter) |
b481de9c ZY |
187 | continue; |
188 | ||
fa254a68 TW |
189 | spin_lock_irqsave(&rs_sta->lock, flags); |
190 | if (time_after(jiffies, rs_sta->win[i].stamp + | |
b481de9c ZY |
191 | IWL_RATE_WIN_FLUSH)) { |
192 | IWL_DEBUG_RATE("flushing %d samples of rate " | |
193 | "index %d\n", | |
fa254a68 TW |
194 | rs_sta->win[i].counter, i); |
195 | iwl3945_clear_window(&rs_sta->win[i]); | |
b481de9c ZY |
196 | } else |
197 | unflushed++; | |
fa254a68 | 198 | spin_unlock_irqrestore(&rs_sta->lock, flags); |
b481de9c ZY |
199 | } |
200 | ||
201 | return unflushed; | |
202 | } | |
203 | ||
204 | #define IWL_RATE_FLUSH_MAX 5000 /* msec */ | |
205 | #define IWL_RATE_FLUSH_MIN 50 /* msec */ | |
206 | ||
bb8c093b | 207 | static void iwl3945_bg_rate_scale_flush(unsigned long data) |
b481de9c | 208 | { |
fa254a68 | 209 | struct iwl3945_rs_sta *rs_sta = (void *)data; |
b481de9c ZY |
210 | int unflushed = 0; |
211 | unsigned long flags; | |
212 | u32 packet_count, duration, pps; | |
213 | ||
214 | IWL_DEBUG_RATE("enter\n"); | |
215 | ||
fa254a68 | 216 | unflushed = iwl3945_rate_scale_flush_windows(rs_sta); |
b481de9c | 217 | |
fa254a68 | 218 | spin_lock_irqsave(&rs_sta->lock, flags); |
b481de9c | 219 | |
fa254a68 | 220 | rs_sta->flush_pending = 0; |
b481de9c ZY |
221 | |
222 | /* Number of packets Rx'd since last time this timer ran */ | |
fa254a68 | 223 | packet_count = (rs_sta->tx_packets - rs_sta->last_tx_packets) + 1; |
b481de9c | 224 | |
fa254a68 | 225 | rs_sta->last_tx_packets = rs_sta->tx_packets + 1; |
b481de9c ZY |
226 | |
227 | if (unflushed) { | |
228 | duration = | |
fa254a68 TW |
229 | jiffies_to_msecs(jiffies - rs_sta->last_partial_flush); |
230 | /* duration = jiffies_to_msecs(rs_sta->flush_time); */ | |
b481de9c ZY |
231 | |
232 | IWL_DEBUG_RATE("Tx'd %d packets in %dms\n", | |
233 | packet_count, duration); | |
234 | ||
235 | /* Determine packets per second */ | |
236 | if (duration) | |
237 | pps = (packet_count * 1000) / duration; | |
238 | else | |
239 | pps = 0; | |
240 | ||
241 | if (pps) { | |
242 | duration = IWL_RATE_FLUSH_MAX / pps; | |
243 | if (duration < IWL_RATE_FLUSH_MIN) | |
244 | duration = IWL_RATE_FLUSH_MIN; | |
245 | } else | |
246 | duration = IWL_RATE_FLUSH_MAX; | |
247 | ||
fa254a68 | 248 | rs_sta->flush_time = msecs_to_jiffies(duration); |
b481de9c ZY |
249 | |
250 | IWL_DEBUG_RATE("new flush period: %d msec ave %d\n", | |
251 | duration, packet_count); | |
252 | ||
fa254a68 TW |
253 | mod_timer(&rs_sta->rate_scale_flush, jiffies + |
254 | rs_sta->flush_time); | |
b481de9c | 255 | |
fa254a68 | 256 | rs_sta->last_partial_flush = jiffies; |
b481de9c ZY |
257 | } |
258 | ||
259 | /* If there weren't any unflushed entries, we don't schedule the timer | |
260 | * to run again */ | |
261 | ||
fa254a68 | 262 | rs_sta->last_flush = jiffies; |
b481de9c | 263 | |
fa254a68 | 264 | spin_unlock_irqrestore(&rs_sta->lock, flags); |
b481de9c ZY |
265 | |
266 | IWL_DEBUG_RATE("leave\n"); | |
267 | } | |
268 | ||
269 | /** | |
bb8c093b | 270 | * iwl3945_collect_tx_data - Update the success/failure sliding window |
b481de9c ZY |
271 | * |
272 | * We keep a sliding window of the last 64 packets transmitted | |
273 | * at this rate. window->data contains the bitmask of successful | |
274 | * packets. | |
275 | */ | |
fa254a68 | 276 | static void iwl3945_collect_tx_data(struct iwl3945_rs_sta *rs_sta, |
bb8c093b | 277 | struct iwl3945_rate_scale_data *window, |
b481de9c ZY |
278 | int success, int retries) |
279 | { | |
280 | unsigned long flags; | |
281 | ||
282 | if (!retries) { | |
283 | IWL_DEBUG_RATE("leave: retries == 0 -- should be at least 1\n"); | |
284 | return; | |
285 | } | |
286 | ||
287 | while (retries--) { | |
fa254a68 | 288 | spin_lock_irqsave(&rs_sta->lock, flags); |
b481de9c ZY |
289 | |
290 | /* If we have filled up the window then subtract one from the | |
291 | * success counter if the high-bit is counting toward | |
292 | * success */ | |
293 | if (window->counter == IWL_RATE_MAX_WINDOW) { | |
294 | if (window->data & (1ULL << (IWL_RATE_MAX_WINDOW - 1))) | |
295 | window->success_counter--; | |
296 | } else | |
297 | window->counter++; | |
298 | ||
299 | /* Slide the window to the left one bit */ | |
300 | window->data = (window->data << 1); | |
301 | ||
302 | /* If this packet was a success then set the low bit high */ | |
303 | if (success) { | |
304 | window->success_counter++; | |
305 | window->data |= 1; | |
306 | } | |
307 | ||
308 | /* window->counter can't be 0 -- it is either >0 or | |
309 | * IWL_RATE_MAX_WINDOW */ | |
310 | window->success_ratio = 12800 * window->success_counter / | |
311 | window->counter; | |
312 | ||
313 | /* Tag this window as having been updated */ | |
314 | window->stamp = jiffies; | |
315 | ||
fa254a68 | 316 | spin_unlock_irqrestore(&rs_sta->lock, flags); |
b481de9c ZY |
317 | } |
318 | } | |
319 | ||
4b7679a5 JB |
320 | static void rs_rate_init(void *priv, struct ieee80211_supported_band *sband, |
321 | struct ieee80211_sta *sta, void *priv_sta) | |
b481de9c | 322 | { |
4b7679a5 | 323 | struct iwl3945_rs_sta *rs_sta = priv_sta; |
b481de9c ZY |
324 | int i; |
325 | ||
326 | IWL_DEBUG_RATE("enter\n"); | |
327 | ||
328 | /* TODO: what is a good starting rate for STA? About middle? Maybe not | |
329 | * the lowest or the highest rate.. Could consider using RSSI from | |
330 | * previous packets? Need to have IEEE 802.1X auth succeed immediately | |
331 | * after assoc.. */ | |
332 | ||
333 | for (i = IWL_RATE_COUNT - 1; i >= 0; i--) { | |
4b7679a5 | 334 | if (sta->supp_rates[sband->band] & (1 << i)) { |
ae17e986 | 335 | rs_sta->last_txrate_idx = i; |
b481de9c ZY |
336 | break; |
337 | } | |
338 | } | |
339 | ||
8318d78a | 340 | /* For 5 GHz band it start at IWL_FIRST_OFDM_RATE */ |
4b7679a5 | 341 | if (sband->band == IEEE80211_BAND_5GHZ) |
b7e35008 | 342 | rs_sta->last_txrate_idx += IWL_FIRST_OFDM_RATE; |
14577f23 | 343 | |
b481de9c ZY |
344 | IWL_DEBUG_RATE("leave\n"); |
345 | } | |
346 | ||
4b7679a5 | 347 | static void *rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir) |
b481de9c | 348 | { |
4b7679a5 | 349 | return hw->priv; |
b481de9c ZY |
350 | } |
351 | ||
01ebd063 | 352 | /* rate scale requires free function to be implemented */ |
b481de9c ZY |
353 | static void rs_free(void *priv) |
354 | { | |
355 | return; | |
356 | } | |
4b7679a5 | 357 | |
b481de9c ZY |
358 | static void rs_clear(void *priv) |
359 | { | |
360 | return; | |
361 | } | |
362 | ||
363 | ||
4b7679a5 | 364 | static void *rs_alloc_sta(void *priv, struct ieee80211_sta *sta, gfp_t gfp) |
b481de9c | 365 | { |
fa254a68 | 366 | struct iwl3945_rs_sta *rs_sta; |
4b7679a5 | 367 | struct iwl3945_sta_priv *psta = (void *) sta->drv_priv; |
b481de9c ZY |
368 | int i; |
369 | ||
4b7679a5 JB |
370 | /* |
371 | * XXX: If it's using sta->drv_priv anyway, it might | |
372 | * as well just put all the information there. | |
373 | */ | |
374 | ||
b481de9c ZY |
375 | IWL_DEBUG_RATE("enter\n"); |
376 | ||
fa254a68 TW |
377 | rs_sta = kzalloc(sizeof(struct iwl3945_rs_sta), gfp); |
378 | if (!rs_sta) { | |
b481de9c ZY |
379 | IWL_DEBUG_RATE("leave: ENOMEM\n"); |
380 | return NULL; | |
381 | } | |
382 | ||
4b7679a5 JB |
383 | psta->rs_sta = rs_sta; |
384 | ||
fa254a68 | 385 | spin_lock_init(&rs_sta->lock); |
b481de9c | 386 | |
fa254a68 | 387 | rs_sta->start_rate = IWL_RATE_INVALID; |
b481de9c ZY |
388 | |
389 | /* default to just 802.11b */ | |
fa254a68 | 390 | rs_sta->expected_tpt = iwl3945_expected_tpt_b; |
b481de9c | 391 | |
fa254a68 TW |
392 | rs_sta->last_partial_flush = jiffies; |
393 | rs_sta->last_flush = jiffies; | |
394 | rs_sta->flush_time = IWL_RATE_FLUSH; | |
395 | rs_sta->last_tx_packets = 0; | |
396 | rs_sta->ibss_sta_added = 0; | |
b481de9c | 397 | |
fa254a68 TW |
398 | init_timer(&rs_sta->rate_scale_flush); |
399 | rs_sta->rate_scale_flush.data = (unsigned long)rs_sta; | |
400 | rs_sta->rate_scale_flush.function = &iwl3945_bg_rate_scale_flush; | |
b481de9c ZY |
401 | |
402 | for (i = 0; i < IWL_RATE_COUNT; i++) | |
fa254a68 | 403 | iwl3945_clear_window(&rs_sta->win[i]); |
b481de9c ZY |
404 | |
405 | IWL_DEBUG_RATE("leave\n"); | |
406 | ||
fa254a68 | 407 | return rs_sta; |
b481de9c ZY |
408 | } |
409 | ||
4b7679a5 JB |
410 | static void rs_free_sta(void *priv, struct ieee80211_sta *sta, |
411 | void *priv_sta) | |
b481de9c | 412 | { |
4b7679a5 | 413 | struct iwl3945_sta_priv *psta = (void *) sta->drv_priv; |
fa254a68 | 414 | struct iwl3945_rs_sta *rs_sta = priv_sta; |
b481de9c | 415 | |
4b7679a5 JB |
416 | psta->rs_sta = NULL; |
417 | ||
b481de9c | 418 | IWL_DEBUG_RATE("enter\n"); |
fa254a68 TW |
419 | del_timer_sync(&rs_sta->rate_scale_flush); |
420 | kfree(rs_sta); | |
b481de9c ZY |
421 | IWL_DEBUG_RATE("leave\n"); |
422 | } | |
423 | ||
14577f23 MA |
424 | |
425 | /* | |
426 | * get ieee prev rate from rate scale table. | |
427 | * for A and B mode we need to overright prev | |
428 | * value | |
429 | */ | |
bb8c093b | 430 | static int rs_adjust_next_rate(struct iwl3945_priv *priv, int rate) |
14577f23 | 431 | { |
bb8c093b | 432 | int next_rate = iwl3945_get_prev_ieee_rate(rate); |
14577f23 | 433 | |
8318d78a JB |
434 | switch (priv->band) { |
435 | case IEEE80211_BAND_5GHZ: | |
14577f23 MA |
436 | if (rate == IWL_RATE_12M_INDEX) |
437 | next_rate = IWL_RATE_9M_INDEX; | |
438 | else if (rate == IWL_RATE_6M_INDEX) | |
439 | next_rate = IWL_RATE_6M_INDEX; | |
440 | break; | |
8318d78a | 441 | /* XXX cannot be invoked in current mac80211 so not a regression |
14577f23 MA |
442 | case MODE_IEEE80211B: |
443 | if (rate == IWL_RATE_11M_INDEX_TABLE) | |
444 | next_rate = IWL_RATE_5M_INDEX_TABLE; | |
445 | break; | |
8318d78a | 446 | */ |
14577f23 MA |
447 | default: |
448 | break; | |
449 | } | |
450 | ||
451 | return next_rate; | |
452 | } | |
b481de9c ZY |
453 | /** |
454 | * rs_tx_status - Update rate control values based on Tx results | |
455 | * | |
bb8c093b | 456 | * NOTE: Uses iwl3945_priv->retry_rate for the # of retries attempted by |
b481de9c ZY |
457 | * the hardware for each rate. |
458 | */ | |
4b7679a5 JB |
459 | static void rs_tx_status(void *priv_rate, struct ieee80211_supported_band *sband, |
460 | struct ieee80211_sta *sta, void *priv_sta, | |
e039fa4a | 461 | struct sk_buff *skb) |
b481de9c ZY |
462 | { |
463 | u8 retries, current_count; | |
464 | int scale_rate_index, first_index, last_index; | |
465 | unsigned long flags; | |
bb8c093b | 466 | struct iwl3945_priv *priv = (struct iwl3945_priv *)priv_rate; |
4b7679a5 | 467 | struct iwl3945_rs_sta *rs_sta = priv_sta; |
e039fa4a | 468 | struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); |
8318d78a | 469 | |
28447f3c TW |
470 | IWL_DEBUG_RATE("enter\n"); |
471 | ||
e039fa4a JB |
472 | retries = info->status.retry_count; |
473 | first_index = sband->bitrates[info->tx_rate_idx].hw_value; | |
b481de9c | 474 | if ((first_index < 0) || (first_index >= IWL_RATE_COUNT)) { |
8318d78a | 475 | IWL_DEBUG_RATE("leave: Rate out of bounds: %d\n", first_index); |
b481de9c ZY |
476 | return; |
477 | } | |
478 | ||
4b7679a5 | 479 | if (!priv_sta) { |
b481de9c ZY |
480 | IWL_DEBUG_RATE("leave: No STA priv data to update!\n"); |
481 | return; | |
482 | } | |
483 | ||
fa254a68 | 484 | rs_sta->tx_packets++; |
b481de9c ZY |
485 | |
486 | scale_rate_index = first_index; | |
487 | last_index = first_index; | |
488 | ||
489 | /* | |
490 | * Update the window for each rate. We determine which rates | |
491 | * were Tx'd based on the total number of retries vs. the number | |
492 | * of retries configured for each rate -- currently set to the | |
493 | * priv value 'retry_rate' vs. rate specific | |
494 | * | |
495 | * On exit from this while loop last_index indicates the rate | |
496 | * at which the frame was finally transmitted (or failed if no | |
497 | * ACK) | |
498 | */ | |
499 | while (retries > 0) { | |
500 | if (retries < priv->retry_rate) { | |
501 | current_count = retries; | |
502 | last_index = scale_rate_index; | |
503 | } else { | |
504 | current_count = priv->retry_rate; | |
14577f23 MA |
505 | last_index = rs_adjust_next_rate(priv, |
506 | scale_rate_index); | |
b481de9c ZY |
507 | } |
508 | ||
509 | /* Update this rate accounting for as many retries | |
510 | * as was used for it (per current_count) */ | |
fa254a68 TW |
511 | iwl3945_collect_tx_data(rs_sta, |
512 | &rs_sta->win[scale_rate_index], | |
b481de9c ZY |
513 | 0, current_count); |
514 | IWL_DEBUG_RATE("Update rate %d for %d retries.\n", | |
515 | scale_rate_index, current_count); | |
516 | ||
517 | retries -= current_count; | |
518 | ||
519 | if (retries) | |
520 | scale_rate_index = | |
14577f23 | 521 | rs_adjust_next_rate(priv, scale_rate_index); |
b481de9c ZY |
522 | } |
523 | ||
14577f23 | 524 | |
b481de9c ZY |
525 | /* Update the last index window with success/failure based on ACK */ |
526 | IWL_DEBUG_RATE("Update rate %d with %s.\n", | |
527 | last_index, | |
e039fa4a | 528 | (info->flags & IEEE80211_TX_STAT_ACK) ? |
b481de9c | 529 | "success" : "failure"); |
fa254a68 TW |
530 | iwl3945_collect_tx_data(rs_sta, |
531 | &rs_sta->win[last_index], | |
e039fa4a | 532 | info->flags & IEEE80211_TX_STAT_ACK, 1); |
b481de9c ZY |
533 | |
534 | /* We updated the rate scale window -- if its been more than | |
535 | * flush_time since the last run, schedule the flush | |
536 | * again */ | |
fa254a68 | 537 | spin_lock_irqsave(&rs_sta->lock, flags); |
b481de9c | 538 | |
fa254a68 TW |
539 | if (!rs_sta->flush_pending && |
540 | time_after(jiffies, rs_sta->last_partial_flush + | |
541 | rs_sta->flush_time)) { | |
b481de9c | 542 | |
fa254a68 TW |
543 | rs_sta->flush_pending = 1; |
544 | mod_timer(&rs_sta->rate_scale_flush, | |
545 | jiffies + rs_sta->flush_time); | |
b481de9c ZY |
546 | } |
547 | ||
fa254a68 | 548 | spin_unlock_irqrestore(&rs_sta->lock, flags); |
b481de9c | 549 | |
b481de9c ZY |
550 | IWL_DEBUG_RATE("leave\n"); |
551 | ||
552 | return; | |
553 | } | |
554 | ||
fa254a68 | 555 | static u16 iwl3945_get_adjacent_rate(struct iwl3945_rs_sta *rs_sta, |
8318d78a | 556 | u8 index, u16 rate_mask, enum ieee80211_band band) |
b481de9c ZY |
557 | { |
558 | u8 high = IWL_RATE_INVALID; | |
559 | u8 low = IWL_RATE_INVALID; | |
560 | ||
01ebd063 | 561 | /* 802.11A walks to the next literal adjacent rate in |
b481de9c | 562 | * the rate table */ |
8318d78a | 563 | if (unlikely(band == IEEE80211_BAND_5GHZ)) { |
b481de9c ZY |
564 | int i; |
565 | u32 mask; | |
566 | ||
567 | /* Find the previous rate that is in the rate mask */ | |
568 | i = index - 1; | |
569 | for (mask = (1 << i); i >= 0; i--, mask >>= 1) { | |
570 | if (rate_mask & mask) { | |
571 | low = i; | |
572 | break; | |
573 | } | |
574 | } | |
575 | ||
576 | /* Find the next rate that is in the rate mask */ | |
577 | i = index + 1; | |
578 | for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) { | |
579 | if (rate_mask & mask) { | |
580 | high = i; | |
581 | break; | |
582 | } | |
583 | } | |
584 | ||
585 | return (high << 8) | low; | |
586 | } | |
587 | ||
588 | low = index; | |
589 | while (low != IWL_RATE_INVALID) { | |
fa254a68 | 590 | if (rs_sta->tgg) |
bb8c093b | 591 | low = iwl3945_rates[low].prev_rs_tgg; |
b481de9c | 592 | else |
bb8c093b | 593 | low = iwl3945_rates[low].prev_rs; |
b481de9c ZY |
594 | if (low == IWL_RATE_INVALID) |
595 | break; | |
596 | if (rate_mask & (1 << low)) | |
597 | break; | |
598 | IWL_DEBUG_RATE("Skipping masked lower rate: %d\n", low); | |
599 | } | |
600 | ||
601 | high = index; | |
602 | while (high != IWL_RATE_INVALID) { | |
fa254a68 | 603 | if (rs_sta->tgg) |
bb8c093b | 604 | high = iwl3945_rates[high].next_rs_tgg; |
b481de9c | 605 | else |
bb8c093b | 606 | high = iwl3945_rates[high].next_rs; |
b481de9c ZY |
607 | if (high == IWL_RATE_INVALID) |
608 | break; | |
609 | if (rate_mask & (1 << high)) | |
610 | break; | |
611 | IWL_DEBUG_RATE("Skipping masked higher rate: %d\n", high); | |
612 | } | |
613 | ||
614 | return (high << 8) | low; | |
615 | } | |
616 | ||
617 | /** | |
618 | * rs_get_rate - find the rate for the requested packet | |
619 | * | |
620 | * Returns the ieee80211_rate structure allocated by the driver. | |
621 | * | |
622 | * The rate control algorithm has no internal mapping between hw_mode's | |
623 | * rate ordering and the rate ordering used by the rate control algorithm. | |
624 | * | |
625 | * The rate control algorithm uses a single table of rates that goes across | |
626 | * the entire A/B/G spectrum vs. being limited to just one particular | |
627 | * hw_mode. | |
628 | * | |
629 | * As such, we can't convert the index obtained below into the hw_mode's | |
630 | * rate table and must reference the driver allocated rate table | |
631 | * | |
632 | */ | |
4b7679a5 JB |
633 | static void rs_get_rate(void *priv_r, struct ieee80211_supported_band *sband, |
634 | struct ieee80211_sta *sta, void *priv_sta, | |
635 | struct sk_buff *skb, struct rate_selection *sel) | |
b481de9c ZY |
636 | { |
637 | u8 low = IWL_RATE_INVALID; | |
638 | u8 high = IWL_RATE_INVALID; | |
639 | u16 high_low; | |
640 | int index; | |
4b7679a5 | 641 | struct iwl3945_rs_sta *rs_sta = priv_sta; |
bb8c093b | 642 | struct iwl3945_rate_scale_data *window = NULL; |
28447f3c TW |
643 | int current_tpt = IWL_INV_TPT; |
644 | int low_tpt = IWL_INV_TPT; | |
645 | int high_tpt = IWL_INV_TPT; | |
b481de9c ZY |
646 | u32 fail_count; |
647 | s8 scale_action = 0; | |
648 | unsigned long flags; | |
b481de9c | 649 | struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data; |
2bc454b0 | 650 | u16 fc, rate_mask; |
4b7679a5 | 651 | struct iwl3945_priv *priv = (struct iwl3945_priv *)priv_r; |
b481de9c ZY |
652 | |
653 | IWL_DEBUG_RATE("enter\n"); | |
654 | ||
2bc454b0 MW |
655 | /* Send management frames and broadcast/multicast data using lowest |
656 | * rate. */ | |
657 | fc = le16_to_cpu(hdr->frame_control); | |
658 | if ((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA || | |
659 | is_multicast_ether_addr(hdr->addr1) || | |
4b7679a5 | 660 | !sta || !priv_sta) { |
b481de9c | 661 | IWL_DEBUG_RATE("leave: No STA priv data to update!\n"); |
4b7679a5 | 662 | sel->rate_idx = rate_lowest_index(sband, sta); |
1abbe498 | 663 | return; |
b481de9c ZY |
664 | } |
665 | ||
4b7679a5 | 666 | rate_mask = sta->supp_rates[sband->band]; |
b7e35008 | 667 | index = min(rs_sta->last_txrate_idx & 0xffff, IWL_RATE_COUNT - 1); |
14577f23 | 668 | |
28447f3c | 669 | if (sband->band == IEEE80211_BAND_5GHZ) |
14577f23 | 670 | rate_mask = rate_mask << IWL_FIRST_OFDM_RATE; |
b481de9c | 671 | |
05c914fe | 672 | if ((priv->iw_mode == NL80211_IFTYPE_ADHOC) && |
fa254a68 | 673 | !rs_sta->ibss_sta_added) { |
bb8c093b | 674 | u8 sta_id = iwl3945_hw_find_station(priv, hdr->addr1); |
b481de9c ZY |
675 | |
676 | if (sta_id == IWL_INVALID_STATION) { | |
e174961c JB |
677 | IWL_DEBUG_RATE("LQ: ADD station %pm\n", |
678 | hdr->addr1); | |
bb8c093b | 679 | sta_id = iwl3945_add_station(priv, |
b481de9c ZY |
680 | hdr->addr1, 0, CMD_ASYNC); |
681 | } | |
682 | if (sta_id != IWL_INVALID_STATION) | |
fa254a68 | 683 | rs_sta->ibss_sta_added = 1; |
b481de9c ZY |
684 | } |
685 | ||
fa254a68 | 686 | spin_lock_irqsave(&rs_sta->lock, flags); |
b481de9c | 687 | |
fa254a68 TW |
688 | if (rs_sta->start_rate != IWL_RATE_INVALID) { |
689 | index = rs_sta->start_rate; | |
690 | rs_sta->start_rate = IWL_RATE_INVALID; | |
b481de9c ZY |
691 | } |
692 | ||
fa254a68 | 693 | window = &(rs_sta->win[index]); |
b481de9c ZY |
694 | |
695 | fail_count = window->counter - window->success_counter; | |
696 | ||
697 | if (((fail_count <= IWL_RATE_MIN_FAILURE_TH) && | |
698 | (window->success_counter < IWL_RATE_MIN_SUCCESS_TH))) { | |
28447f3c | 699 | window->average_tpt = IWL_INV_TPT; |
fa254a68 | 700 | spin_unlock_irqrestore(&rs_sta->lock, flags); |
b481de9c ZY |
701 | |
702 | IWL_DEBUG_RATE("Invalid average_tpt on rate %d: " | |
703 | "counter: %d, success_counter: %d, " | |
704 | "expected_tpt is %sNULL\n", | |
705 | index, | |
706 | window->counter, | |
707 | window->success_counter, | |
fa254a68 | 708 | rs_sta->expected_tpt ? "not " : ""); |
b481de9c ZY |
709 | goto out; |
710 | ||
711 | } | |
712 | ||
713 | window->average_tpt = ((window->success_ratio * | |
fa254a68 | 714 | rs_sta->expected_tpt[index] + 64) / 128); |
b481de9c ZY |
715 | current_tpt = window->average_tpt; |
716 | ||
fa254a68 | 717 | high_low = iwl3945_get_adjacent_rate(rs_sta, index, rate_mask, |
28447f3c | 718 | sband->band); |
b481de9c ZY |
719 | low = high_low & 0xff; |
720 | high = (high_low >> 8) & 0xff; | |
721 | ||
722 | if (low != IWL_RATE_INVALID) | |
fa254a68 | 723 | low_tpt = rs_sta->win[low].average_tpt; |
b481de9c ZY |
724 | |
725 | if (high != IWL_RATE_INVALID) | |
fa254a68 | 726 | high_tpt = rs_sta->win[high].average_tpt; |
b481de9c | 727 | |
fa254a68 | 728 | spin_unlock_irqrestore(&rs_sta->lock, flags); |
b481de9c ZY |
729 | |
730 | scale_action = 1; | |
731 | ||
732 | if ((window->success_ratio < IWL_RATE_DECREASE_TH) || !current_tpt) { | |
733 | IWL_DEBUG_RATE("decrease rate because of low success_ratio\n"); | |
734 | scale_action = -1; | |
28447f3c | 735 | } else if ((low_tpt == IWL_INV_TPT) && (high_tpt == IWL_INV_TPT)) |
b481de9c | 736 | scale_action = 1; |
28447f3c TW |
737 | else if ((low_tpt != IWL_INV_TPT) && (high_tpt != IWL_INV_TPT) && |
738 | (low_tpt < current_tpt) && (high_tpt < current_tpt)) { | |
b481de9c ZY |
739 | IWL_DEBUG_RATE("No action -- low [%d] & high [%d] < " |
740 | "current_tpt [%d]\n", | |
741 | low_tpt, high_tpt, current_tpt); | |
742 | scale_action = 0; | |
743 | } else { | |
28447f3c | 744 | if (high_tpt != IWL_INV_TPT) { |
b481de9c ZY |
745 | if (high_tpt > current_tpt) |
746 | scale_action = 1; | |
747 | else { | |
748 | IWL_DEBUG_RATE | |
749 | ("decrease rate because of high tpt\n"); | |
750 | scale_action = -1; | |
751 | } | |
28447f3c | 752 | } else if (low_tpt != IWL_INV_TPT) { |
b481de9c ZY |
753 | if (low_tpt > current_tpt) { |
754 | IWL_DEBUG_RATE | |
755 | ("decrease rate because of low tpt\n"); | |
756 | scale_action = -1; | |
757 | } else | |
758 | scale_action = 1; | |
759 | } | |
760 | } | |
761 | ||
762 | if ((window->success_ratio > IWL_RATE_HIGH_TH) || | |
763 | (current_tpt > window->average_tpt)) { | |
764 | IWL_DEBUG_RATE("No action -- success_ratio [%d] > HIGH_TH or " | |
765 | "current_tpt [%d] > average_tpt [%d]\n", | |
766 | window->success_ratio, | |
767 | current_tpt, window->average_tpt); | |
768 | scale_action = 0; | |
769 | } | |
770 | ||
771 | switch (scale_action) { | |
772 | case -1: | |
773 | if (low != IWL_RATE_INVALID) | |
774 | index = low; | |
775 | break; | |
776 | ||
777 | case 1: | |
778 | if (high != IWL_RATE_INVALID) | |
779 | index = high; | |
780 | ||
781 | break; | |
782 | ||
783 | case 0: | |
784 | default: | |
785 | break; | |
786 | } | |
787 | ||
788 | IWL_DEBUG_RATE("Selected %d (action %d) - low %d high %d\n", | |
789 | index, scale_action, low, high); | |
790 | ||
791 | out: | |
792 | ||
b7e35008 | 793 | rs_sta->last_txrate_idx = index; |
28447f3c | 794 | if (sband->band == IEEE80211_BAND_5GHZ) |
ae17e986 | 795 | sel->rate_idx = rs_sta->last_txrate_idx - IWL_FIRST_OFDM_RATE; |
14577f23 | 796 | else |
ae17e986 | 797 | sel->rate_idx = rs_sta->last_txrate_idx; |
14577f23 | 798 | |
b481de9c | 799 | IWL_DEBUG_RATE("leave: %d\n", index); |
b481de9c ZY |
800 | } |
801 | ||
802 | static struct rate_control_ops rs_ops = { | |
803 | .module = NULL, | |
804 | .name = RS_NAME, | |
805 | .tx_status = rs_tx_status, | |
806 | .get_rate = rs_get_rate, | |
807 | .rate_init = rs_rate_init, | |
808 | .clear = rs_clear, | |
809 | .alloc = rs_alloc, | |
810 | .free = rs_free, | |
811 | .alloc_sta = rs_alloc_sta, | |
812 | .free_sta = rs_free_sta, | |
813 | }; | |
814 | ||
bb8c093b | 815 | void iwl3945_rate_scale_init(struct ieee80211_hw *hw, s32 sta_id) |
b481de9c | 816 | { |
bb8c093b | 817 | struct iwl3945_priv *priv = hw->priv; |
b481de9c ZY |
818 | s32 rssi = 0; |
819 | unsigned long flags; | |
fa254a68 | 820 | struct iwl3945_rs_sta *rs_sta; |
4b7679a5 JB |
821 | struct ieee80211_sta *sta; |
822 | struct iwl3945_sta_priv *psta; | |
b481de9c ZY |
823 | |
824 | IWL_DEBUG_RATE("enter\n"); | |
825 | ||
d0709a65 JB |
826 | rcu_read_lock(); |
827 | ||
4b7679a5 | 828 | sta = ieee80211_find_sta(hw, priv->stations[sta_id].sta.sta.addr); |
52518189 | 829 | if (!sta) { |
d0709a65 | 830 | rcu_read_unlock(); |
b481de9c ZY |
831 | return; |
832 | } | |
833 | ||
52518189 | 834 | psta = (void *) sta->drv_priv; |
4b7679a5 | 835 | rs_sta = psta->rs_sta; |
b481de9c | 836 | |
fa254a68 | 837 | spin_lock_irqsave(&rs_sta->lock, flags); |
b481de9c | 838 | |
fa254a68 | 839 | rs_sta->tgg = 0; |
8318d78a JB |
840 | switch (priv->band) { |
841 | case IEEE80211_BAND_2GHZ: | |
842 | /* TODO: this always does G, not a regression */ | |
b481de9c | 843 | if (priv->active_rxon.flags & RXON_FLG_TGG_PROTECT_MSK) { |
fa254a68 TW |
844 | rs_sta->tgg = 1; |
845 | rs_sta->expected_tpt = iwl3945_expected_tpt_g_prot; | |
b481de9c | 846 | } else |
fa254a68 | 847 | rs_sta->expected_tpt = iwl3945_expected_tpt_g; |
b481de9c ZY |
848 | break; |
849 | ||
8318d78a | 850 | case IEEE80211_BAND_5GHZ: |
fa254a68 | 851 | rs_sta->expected_tpt = iwl3945_expected_tpt_a; |
b481de9c | 852 | break; |
8318d78a JB |
853 | case IEEE80211_NUM_BANDS: |
854 | BUG(); | |
b481de9c ZY |
855 | break; |
856 | } | |
857 | ||
fa254a68 | 858 | spin_unlock_irqrestore(&rs_sta->lock, flags); |
b481de9c ZY |
859 | |
860 | rssi = priv->last_rx_rssi; | |
861 | if (rssi == 0) | |
862 | rssi = IWL_MIN_RSSI_VAL; | |
863 | ||
864 | IWL_DEBUG(IWL_DL_INFO | IWL_DL_RATE, "Network RSSI: %d\n", rssi); | |
865 | ||
8318d78a | 866 | rs_sta->start_rate = iwl3945_get_rate_index_by_rssi(rssi, priv->band); |
b481de9c ZY |
867 | |
868 | IWL_DEBUG_RATE("leave: rssi %d assign rate index: " | |
fa254a68 TW |
869 | "%d (plcp 0x%x)\n", rssi, rs_sta->start_rate, |
870 | iwl3945_rates[rs_sta->start_rate].plcp); | |
52518189 | 871 | rcu_read_unlock(); |
b481de9c ZY |
872 | } |
873 | ||
897e1cf2 | 874 | int iwl3945_rate_control_register(void) |
b481de9c | 875 | { |
897e1cf2 | 876 | return ieee80211_rate_control_register(&rs_ops); |
b481de9c ZY |
877 | } |
878 | ||
897e1cf2 | 879 | void iwl3945_rate_control_unregister(void) |
b481de9c ZY |
880 | { |
881 | ieee80211_rate_control_unregister(&rs_ops); | |
882 | } | |
883 | ||
884 |