]>
Commit | Line | Data |
---|---|---|
b481de9c ZY |
1 | /****************************************************************************** |
2 | * | |
3 | * Copyright(c) 2005 - 2007 Intel Corporation. All rights reserved. | |
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> | |
32 | #include <net/ieee80211.h> | |
33 | ||
34 | #include <linux/netdevice.h> | |
35 | #include <linux/etherdevice.h> | |
36 | #include <linux/delay.h> | |
37 | ||
38 | #include <linux/workqueue.h> | |
39 | ||
b481de9c ZY |
40 | #include "../net/mac80211/ieee80211_rate.h" |
41 | ||
5d08cd1d | 42 | #include "iwl-3945.h" |
b481de9c ZY |
43 | |
44 | #define RS_NAME "iwl-3945-rs" | |
45 | ||
bb8c093b | 46 | struct iwl3945_rate_scale_data { |
b481de9c ZY |
47 | u64 data; |
48 | s32 success_counter; | |
49 | s32 success_ratio; | |
50 | s32 counter; | |
51 | s32 average_tpt; | |
52 | unsigned long stamp; | |
53 | }; | |
54 | ||
fa254a68 | 55 | struct iwl3945_rs_sta { |
b481de9c ZY |
56 | spinlock_t lock; |
57 | s32 *expected_tpt; | |
58 | unsigned long last_partial_flush; | |
59 | unsigned long last_flush; | |
60 | u32 flush_time; | |
61 | u32 last_tx_packets; | |
62 | u32 tx_packets; | |
63 | u8 tgg; | |
64 | u8 flush_pending; | |
65 | u8 start_rate; | |
66 | u8 ibss_sta_added; | |
67 | struct timer_list rate_scale_flush; | |
bb8c093b | 68 | struct iwl3945_rate_scale_data win[IWL_RATE_COUNT]; |
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; | |
161 | window->success_ratio = IWL_INVALID_VALUE; | |
162 | window->counter = 0; | |
163 | window->average_tpt = IWL_INVALID_VALUE; | |
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 | ||
320 | static void rs_rate_init(void *priv_rate, void *priv_sta, | |
321 | struct ieee80211_local *local, struct sta_info *sta) | |
322 | { | |
323 | int i; | |
324 | ||
325 | IWL_DEBUG_RATE("enter\n"); | |
326 | ||
327 | /* TODO: what is a good starting rate for STA? About middle? Maybe not | |
328 | * the lowest or the highest rate.. Could consider using RSSI from | |
329 | * previous packets? Need to have IEEE 802.1X auth succeed immediately | |
330 | * after assoc.. */ | |
331 | ||
332 | for (i = IWL_RATE_COUNT - 1; i >= 0; i--) { | |
8318d78a JB |
333 | if (sta->supp_rates[local->hw.conf.channel->band] & (1 << i)) { |
334 | sta->txrate_idx = i; | |
b481de9c ZY |
335 | break; |
336 | } | |
337 | } | |
338 | ||
8318d78a | 339 | sta->last_txrate_idx = sta->txrate_idx; |
b481de9c | 340 | |
8318d78a JB |
341 | /* For 5 GHz band it start at IWL_FIRST_OFDM_RATE */ |
342 | if (local->hw.conf.channel->band == IEEE80211_BAND_5GHZ) | |
343 | sta->last_txrate_idx += IWL_FIRST_OFDM_RATE; | |
14577f23 | 344 | |
b481de9c ZY |
345 | IWL_DEBUG_RATE("leave\n"); |
346 | } | |
347 | ||
348 | static void *rs_alloc(struct ieee80211_local *local) | |
349 | { | |
350 | return local->hw.priv; | |
351 | } | |
352 | ||
01ebd063 | 353 | /* rate scale requires free function to be implemented */ |
b481de9c ZY |
354 | static void rs_free(void *priv) |
355 | { | |
356 | return; | |
357 | } | |
358 | static void rs_clear(void *priv) | |
359 | { | |
360 | return; | |
361 | } | |
362 | ||
363 | ||
364 | static void *rs_alloc_sta(void *priv, gfp_t gfp) | |
365 | { | |
fa254a68 | 366 | struct iwl3945_rs_sta *rs_sta; |
b481de9c ZY |
367 | int i; |
368 | ||
369 | IWL_DEBUG_RATE("enter\n"); | |
370 | ||
fa254a68 TW |
371 | rs_sta = kzalloc(sizeof(struct iwl3945_rs_sta), gfp); |
372 | if (!rs_sta) { | |
b481de9c ZY |
373 | IWL_DEBUG_RATE("leave: ENOMEM\n"); |
374 | return NULL; | |
375 | } | |
376 | ||
fa254a68 | 377 | spin_lock_init(&rs_sta->lock); |
b481de9c | 378 | |
fa254a68 | 379 | rs_sta->start_rate = IWL_RATE_INVALID; |
b481de9c ZY |
380 | |
381 | /* default to just 802.11b */ | |
fa254a68 | 382 | rs_sta->expected_tpt = iwl3945_expected_tpt_b; |
b481de9c | 383 | |
fa254a68 TW |
384 | rs_sta->last_partial_flush = jiffies; |
385 | rs_sta->last_flush = jiffies; | |
386 | rs_sta->flush_time = IWL_RATE_FLUSH; | |
387 | rs_sta->last_tx_packets = 0; | |
388 | rs_sta->ibss_sta_added = 0; | |
b481de9c | 389 | |
fa254a68 TW |
390 | init_timer(&rs_sta->rate_scale_flush); |
391 | rs_sta->rate_scale_flush.data = (unsigned long)rs_sta; | |
392 | rs_sta->rate_scale_flush.function = &iwl3945_bg_rate_scale_flush; | |
b481de9c ZY |
393 | |
394 | for (i = 0; i < IWL_RATE_COUNT; i++) | |
fa254a68 | 395 | iwl3945_clear_window(&rs_sta->win[i]); |
b481de9c ZY |
396 | |
397 | IWL_DEBUG_RATE("leave\n"); | |
398 | ||
fa254a68 | 399 | return rs_sta; |
b481de9c ZY |
400 | } |
401 | ||
402 | static void rs_free_sta(void *priv, void *priv_sta) | |
403 | { | |
fa254a68 | 404 | struct iwl3945_rs_sta *rs_sta = priv_sta; |
b481de9c ZY |
405 | |
406 | IWL_DEBUG_RATE("enter\n"); | |
fa254a68 TW |
407 | del_timer_sync(&rs_sta->rate_scale_flush); |
408 | kfree(rs_sta); | |
b481de9c ZY |
409 | IWL_DEBUG_RATE("leave\n"); |
410 | } | |
411 | ||
14577f23 MA |
412 | |
413 | /* | |
414 | * get ieee prev rate from rate scale table. | |
415 | * for A and B mode we need to overright prev | |
416 | * value | |
417 | */ | |
bb8c093b | 418 | static int rs_adjust_next_rate(struct iwl3945_priv *priv, int rate) |
14577f23 | 419 | { |
bb8c093b | 420 | int next_rate = iwl3945_get_prev_ieee_rate(rate); |
14577f23 | 421 | |
8318d78a JB |
422 | switch (priv->band) { |
423 | case IEEE80211_BAND_5GHZ: | |
14577f23 MA |
424 | if (rate == IWL_RATE_12M_INDEX) |
425 | next_rate = IWL_RATE_9M_INDEX; | |
426 | else if (rate == IWL_RATE_6M_INDEX) | |
427 | next_rate = IWL_RATE_6M_INDEX; | |
428 | break; | |
8318d78a | 429 | /* XXX cannot be invoked in current mac80211 so not a regression |
14577f23 MA |
430 | case MODE_IEEE80211B: |
431 | if (rate == IWL_RATE_11M_INDEX_TABLE) | |
432 | next_rate = IWL_RATE_5M_INDEX_TABLE; | |
433 | break; | |
8318d78a | 434 | */ |
14577f23 MA |
435 | default: |
436 | break; | |
437 | } | |
438 | ||
439 | return next_rate; | |
440 | } | |
b481de9c ZY |
441 | /** |
442 | * rs_tx_status - Update rate control values based on Tx results | |
443 | * | |
bb8c093b | 444 | * NOTE: Uses iwl3945_priv->retry_rate for the # of retries attempted by |
b481de9c ZY |
445 | * the hardware for each rate. |
446 | */ | |
447 | static void rs_tx_status(void *priv_rate, | |
448 | struct net_device *dev, | |
449 | struct sk_buff *skb, | |
450 | struct ieee80211_tx_status *tx_resp) | |
451 | { | |
452 | u8 retries, current_count; | |
453 | int scale_rate_index, first_index, last_index; | |
454 | unsigned long flags; | |
455 | struct sta_info *sta; | |
456 | struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data; | |
bb8c093b | 457 | struct iwl3945_priv *priv = (struct iwl3945_priv *)priv_rate; |
b481de9c | 458 | struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr); |
fa254a68 | 459 | struct iwl3945_rs_sta *rs_sta; |
8318d78a JB |
460 | struct ieee80211_supported_band *sband; |
461 | ||
462 | sband = local->hw.wiphy->bands[local->hw.conf.channel->band]; | |
b481de9c ZY |
463 | |
464 | IWL_DEBUG_RATE("enter\n"); | |
465 | ||
466 | retries = tx_resp->retry_count; | |
78330fdd | 467 | /* FIXME : this is wrong */ |
8318d78a | 468 | first_index = &sband->bitrates[0] - tx_resp->control.tx_rate; |
b481de9c | 469 | if ((first_index < 0) || (first_index >= IWL_RATE_COUNT)) { |
8318d78a | 470 | IWL_DEBUG_RATE("leave: Rate out of bounds: %d\n", first_index); |
b481de9c ZY |
471 | return; |
472 | } | |
473 | ||
474 | sta = sta_info_get(local, hdr->addr1); | |
475 | if (!sta || !sta->rate_ctrl_priv) { | |
476 | if (sta) | |
477 | sta_info_put(sta); | |
478 | IWL_DEBUG_RATE("leave: No STA priv data to update!\n"); | |
479 | return; | |
480 | } | |
481 | ||
fa254a68 | 482 | rs_sta = (void *)sta->rate_ctrl_priv; |
b481de9c | 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, | |
528 | (tx_resp->flags & IEEE80211_TX_STATUS_ACK) ? | |
529 | "success" : "failure"); | |
fa254a68 TW |
530 | iwl3945_collect_tx_data(rs_sta, |
531 | &rs_sta->win[last_index], | |
b481de9c ZY |
532 | tx_resp->flags & IEEE80211_TX_STATUS_ACK, 1); |
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 ZY |
549 | |
550 | sta_info_put(sta); | |
551 | ||
552 | IWL_DEBUG_RATE("leave\n"); | |
553 | ||
554 | return; | |
555 | } | |
556 | ||
fa254a68 | 557 | static u16 iwl3945_get_adjacent_rate(struct iwl3945_rs_sta *rs_sta, |
8318d78a | 558 | u8 index, u16 rate_mask, enum ieee80211_band band) |
b481de9c ZY |
559 | { |
560 | u8 high = IWL_RATE_INVALID; | |
561 | u8 low = IWL_RATE_INVALID; | |
562 | ||
01ebd063 | 563 | /* 802.11A walks to the next literal adjacent rate in |
b481de9c | 564 | * the rate table */ |
8318d78a | 565 | if (unlikely(band == IEEE80211_BAND_5GHZ)) { |
b481de9c ZY |
566 | int i; |
567 | u32 mask; | |
568 | ||
569 | /* Find the previous rate that is in the rate mask */ | |
570 | i = index - 1; | |
571 | for (mask = (1 << i); i >= 0; i--, mask >>= 1) { | |
572 | if (rate_mask & mask) { | |
573 | low = i; | |
574 | break; | |
575 | } | |
576 | } | |
577 | ||
578 | /* Find the next rate that is in the rate mask */ | |
579 | i = index + 1; | |
580 | for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) { | |
581 | if (rate_mask & mask) { | |
582 | high = i; | |
583 | break; | |
584 | } | |
585 | } | |
586 | ||
587 | return (high << 8) | low; | |
588 | } | |
589 | ||
590 | low = index; | |
591 | while (low != IWL_RATE_INVALID) { | |
fa254a68 | 592 | if (rs_sta->tgg) |
bb8c093b | 593 | low = iwl3945_rates[low].prev_rs_tgg; |
b481de9c | 594 | else |
bb8c093b | 595 | low = iwl3945_rates[low].prev_rs; |
b481de9c ZY |
596 | if (low == IWL_RATE_INVALID) |
597 | break; | |
598 | if (rate_mask & (1 << low)) | |
599 | break; | |
600 | IWL_DEBUG_RATE("Skipping masked lower rate: %d\n", low); | |
601 | } | |
602 | ||
603 | high = index; | |
604 | while (high != IWL_RATE_INVALID) { | |
fa254a68 | 605 | if (rs_sta->tgg) |
bb8c093b | 606 | high = iwl3945_rates[high].next_rs_tgg; |
b481de9c | 607 | else |
bb8c093b | 608 | high = iwl3945_rates[high].next_rs; |
b481de9c ZY |
609 | if (high == IWL_RATE_INVALID) |
610 | break; | |
611 | if (rate_mask & (1 << high)) | |
612 | break; | |
613 | IWL_DEBUG_RATE("Skipping masked higher rate: %d\n", high); | |
614 | } | |
615 | ||
616 | return (high << 8) | low; | |
617 | } | |
618 | ||
619 | /** | |
620 | * rs_get_rate - find the rate for the requested packet | |
621 | * | |
622 | * Returns the ieee80211_rate structure allocated by the driver. | |
623 | * | |
624 | * The rate control algorithm has no internal mapping between hw_mode's | |
625 | * rate ordering and the rate ordering used by the rate control algorithm. | |
626 | * | |
627 | * The rate control algorithm uses a single table of rates that goes across | |
628 | * the entire A/B/G spectrum vs. being limited to just one particular | |
629 | * hw_mode. | |
630 | * | |
631 | * As such, we can't convert the index obtained below into the hw_mode's | |
632 | * rate table and must reference the driver allocated rate table | |
633 | * | |
634 | */ | |
1abbe498 | 635 | static void rs_get_rate(void *priv_rate, struct net_device *dev, |
8318d78a JB |
636 | struct ieee80211_supported_band *band, |
637 | struct sk_buff *skb, | |
1abbe498 | 638 | struct rate_selection *sel) |
b481de9c ZY |
639 | { |
640 | u8 low = IWL_RATE_INVALID; | |
641 | u8 high = IWL_RATE_INVALID; | |
642 | u16 high_low; | |
643 | int index; | |
fa254a68 | 644 | struct iwl3945_rs_sta *rs_sta; |
bb8c093b | 645 | struct iwl3945_rate_scale_data *window = NULL; |
b481de9c ZY |
646 | int current_tpt = IWL_INVALID_VALUE; |
647 | int low_tpt = IWL_INVALID_VALUE; | |
648 | int high_tpt = IWL_INVALID_VALUE; | |
649 | u32 fail_count; | |
650 | s8 scale_action = 0; | |
651 | unsigned long flags; | |
652 | struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr); | |
653 | struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data; | |
654 | struct sta_info *sta; | |
2bc454b0 | 655 | u16 fc, rate_mask; |
bb8c093b | 656 | struct iwl3945_priv *priv = (struct iwl3945_priv *)priv_rate; |
0795af57 | 657 | DECLARE_MAC_BUF(mac); |
b481de9c ZY |
658 | |
659 | IWL_DEBUG_RATE("enter\n"); | |
660 | ||
b481de9c | 661 | sta = sta_info_get(local, hdr->addr1); |
2bc454b0 MW |
662 | |
663 | /* Send management frames and broadcast/multicast data using lowest | |
664 | * rate. */ | |
665 | fc = le16_to_cpu(hdr->frame_control); | |
666 | if ((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA || | |
667 | is_multicast_ether_addr(hdr->addr1) || | |
668 | !sta || !sta->rate_ctrl_priv) { | |
b481de9c | 669 | IWL_DEBUG_RATE("leave: No STA priv data to update!\n"); |
8318d78a | 670 | sel->rate = rate_lowest(local, band, sta); |
b481de9c ZY |
671 | if (sta) |
672 | sta_info_put(sta); | |
1abbe498 | 673 | return; |
b481de9c ZY |
674 | } |
675 | ||
8318d78a JB |
676 | rate_mask = sta->supp_rates[band->band]; |
677 | index = min(sta->last_txrate_idx & 0xffff, IWL_RATE_COUNT - 1); | |
14577f23 | 678 | |
8318d78a | 679 | if (priv->band == IEEE80211_BAND_5GHZ) |
14577f23 | 680 | rate_mask = rate_mask << IWL_FIRST_OFDM_RATE; |
b481de9c | 681 | |
fa254a68 | 682 | rs_sta = (void *)sta->rate_ctrl_priv; |
b481de9c ZY |
683 | |
684 | if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) && | |
fa254a68 | 685 | !rs_sta->ibss_sta_added) { |
bb8c093b | 686 | u8 sta_id = iwl3945_hw_find_station(priv, hdr->addr1); |
b481de9c ZY |
687 | |
688 | if (sta_id == IWL_INVALID_STATION) { | |
0795af57 JP |
689 | IWL_DEBUG_RATE("LQ: ADD station %s\n", |
690 | print_mac(mac, hdr->addr1)); | |
bb8c093b | 691 | sta_id = iwl3945_add_station(priv, |
b481de9c ZY |
692 | hdr->addr1, 0, CMD_ASYNC); |
693 | } | |
694 | if (sta_id != IWL_INVALID_STATION) | |
fa254a68 | 695 | rs_sta->ibss_sta_added = 1; |
b481de9c ZY |
696 | } |
697 | ||
fa254a68 | 698 | spin_lock_irqsave(&rs_sta->lock, flags); |
b481de9c | 699 | |
fa254a68 TW |
700 | if (rs_sta->start_rate != IWL_RATE_INVALID) { |
701 | index = rs_sta->start_rate; | |
702 | rs_sta->start_rate = IWL_RATE_INVALID; | |
b481de9c ZY |
703 | } |
704 | ||
fa254a68 | 705 | window = &(rs_sta->win[index]); |
b481de9c ZY |
706 | |
707 | fail_count = window->counter - window->success_counter; | |
708 | ||
709 | if (((fail_count <= IWL_RATE_MIN_FAILURE_TH) && | |
710 | (window->success_counter < IWL_RATE_MIN_SUCCESS_TH))) { | |
711 | window->average_tpt = IWL_INVALID_VALUE; | |
fa254a68 | 712 | spin_unlock_irqrestore(&rs_sta->lock, flags); |
b481de9c ZY |
713 | |
714 | IWL_DEBUG_RATE("Invalid average_tpt on rate %d: " | |
715 | "counter: %d, success_counter: %d, " | |
716 | "expected_tpt is %sNULL\n", | |
717 | index, | |
718 | window->counter, | |
719 | window->success_counter, | |
fa254a68 | 720 | rs_sta->expected_tpt ? "not " : ""); |
b481de9c ZY |
721 | goto out; |
722 | ||
723 | } | |
724 | ||
725 | window->average_tpt = ((window->success_ratio * | |
fa254a68 | 726 | rs_sta->expected_tpt[index] + 64) / 128); |
b481de9c ZY |
727 | current_tpt = window->average_tpt; |
728 | ||
fa254a68 | 729 | high_low = iwl3945_get_adjacent_rate(rs_sta, index, rate_mask, |
8318d78a | 730 | band->band); |
b481de9c ZY |
731 | low = high_low & 0xff; |
732 | high = (high_low >> 8) & 0xff; | |
733 | ||
734 | if (low != IWL_RATE_INVALID) | |
fa254a68 | 735 | low_tpt = rs_sta->win[low].average_tpt; |
b481de9c ZY |
736 | |
737 | if (high != IWL_RATE_INVALID) | |
fa254a68 | 738 | high_tpt = rs_sta->win[high].average_tpt; |
b481de9c | 739 | |
fa254a68 | 740 | spin_unlock_irqrestore(&rs_sta->lock, flags); |
b481de9c ZY |
741 | |
742 | scale_action = 1; | |
743 | ||
744 | if ((window->success_ratio < IWL_RATE_DECREASE_TH) || !current_tpt) { | |
745 | IWL_DEBUG_RATE("decrease rate because of low success_ratio\n"); | |
746 | scale_action = -1; | |
747 | } else if ((low_tpt == IWL_INVALID_VALUE) && | |
748 | (high_tpt == IWL_INVALID_VALUE)) | |
749 | scale_action = 1; | |
750 | else if ((low_tpt != IWL_INVALID_VALUE) && | |
751 | (high_tpt != IWL_INVALID_VALUE) | |
752 | && (low_tpt < current_tpt) | |
753 | && (high_tpt < current_tpt)) { | |
754 | IWL_DEBUG_RATE("No action -- low [%d] & high [%d] < " | |
755 | "current_tpt [%d]\n", | |
756 | low_tpt, high_tpt, current_tpt); | |
757 | scale_action = 0; | |
758 | } else { | |
759 | if (high_tpt != IWL_INVALID_VALUE) { | |
760 | if (high_tpt > current_tpt) | |
761 | scale_action = 1; | |
762 | else { | |
763 | IWL_DEBUG_RATE | |
764 | ("decrease rate because of high tpt\n"); | |
765 | scale_action = -1; | |
766 | } | |
767 | } else if (low_tpt != IWL_INVALID_VALUE) { | |
768 | if (low_tpt > current_tpt) { | |
769 | IWL_DEBUG_RATE | |
770 | ("decrease rate because of low tpt\n"); | |
771 | scale_action = -1; | |
772 | } else | |
773 | scale_action = 1; | |
774 | } | |
775 | } | |
776 | ||
777 | if ((window->success_ratio > IWL_RATE_HIGH_TH) || | |
778 | (current_tpt > window->average_tpt)) { | |
779 | IWL_DEBUG_RATE("No action -- success_ratio [%d] > HIGH_TH or " | |
780 | "current_tpt [%d] > average_tpt [%d]\n", | |
781 | window->success_ratio, | |
782 | current_tpt, window->average_tpt); | |
783 | scale_action = 0; | |
784 | } | |
785 | ||
786 | switch (scale_action) { | |
787 | case -1: | |
788 | if (low != IWL_RATE_INVALID) | |
789 | index = low; | |
790 | break; | |
791 | ||
792 | case 1: | |
793 | if (high != IWL_RATE_INVALID) | |
794 | index = high; | |
795 | ||
796 | break; | |
797 | ||
798 | case 0: | |
799 | default: | |
800 | break; | |
801 | } | |
802 | ||
803 | IWL_DEBUG_RATE("Selected %d (action %d) - low %d high %d\n", | |
804 | index, scale_action, low, high); | |
805 | ||
806 | out: | |
807 | ||
8318d78a JB |
808 | sta->last_txrate_idx = index; |
809 | if (priv->band == IEEE80211_BAND_5GHZ) | |
810 | sta->txrate_idx = sta->last_txrate_idx - IWL_FIRST_OFDM_RATE; | |
14577f23 | 811 | else |
8318d78a | 812 | sta->txrate_idx = sta->last_txrate_idx; |
14577f23 | 813 | |
b481de9c ZY |
814 | sta_info_put(sta); |
815 | ||
816 | IWL_DEBUG_RATE("leave: %d\n", index); | |
817 | ||
1abbe498 | 818 | sel->rate = &priv->ieee_rates[index]; |
b481de9c ZY |
819 | } |
820 | ||
821 | static struct rate_control_ops rs_ops = { | |
822 | .module = NULL, | |
823 | .name = RS_NAME, | |
824 | .tx_status = rs_tx_status, | |
825 | .get_rate = rs_get_rate, | |
826 | .rate_init = rs_rate_init, | |
827 | .clear = rs_clear, | |
828 | .alloc = rs_alloc, | |
829 | .free = rs_free, | |
830 | .alloc_sta = rs_alloc_sta, | |
831 | .free_sta = rs_free_sta, | |
832 | }; | |
833 | ||
bb8c093b | 834 | int iwl3945_fill_rs_info(struct ieee80211_hw *hw, char *buf, u8 sta_id) |
b481de9c ZY |
835 | { |
836 | struct ieee80211_local *local = hw_to_local(hw); | |
bb8c093b | 837 | struct iwl3945_priv *priv = hw->priv; |
fa254a68 | 838 | struct iwl3945_rs_sta *rs_sta; |
b481de9c ZY |
839 | struct sta_info *sta; |
840 | unsigned long flags; | |
841 | int count = 0, i; | |
842 | u32 samples = 0, success = 0, good = 0; | |
843 | unsigned long now = jiffies; | |
844 | u32 max_time = 0; | |
845 | ||
846 | sta = sta_info_get(local, priv->stations[sta_id].sta.sta.addr); | |
847 | if (!sta || !sta->rate_ctrl_priv) { | |
848 | if (sta) { | |
849 | sta_info_put(sta); | |
850 | IWL_DEBUG_RATE("leave - no private rate data!\n"); | |
851 | } else | |
852 | IWL_DEBUG_RATE("leave - no station!\n"); | |
853 | return sprintf(buf, "station %d not found\n", sta_id); | |
854 | } | |
855 | ||
fa254a68 TW |
856 | rs_sta = (void *)sta->rate_ctrl_priv; |
857 | spin_lock_irqsave(&rs_sta->lock, flags); | |
b481de9c ZY |
858 | i = IWL_RATE_54M_INDEX; |
859 | while (1) { | |
860 | u64 mask; | |
861 | int j; | |
862 | ||
863 | count += | |
bb8c093b | 864 | sprintf(&buf[count], " %2dMbs: ", iwl3945_rates[i].ieee / 2); |
b481de9c ZY |
865 | |
866 | mask = (1ULL << (IWL_RATE_MAX_WINDOW - 1)); | |
867 | for (j = 0; j < IWL_RATE_MAX_WINDOW; j++, mask >>= 1) | |
868 | buf[count++] = | |
fa254a68 | 869 | (rs_sta->win[i].data & mask) ? '1' : '0'; |
b481de9c | 870 | |
fa254a68 TW |
871 | samples += rs_sta->win[i].counter; |
872 | good += rs_sta->win[i].success_counter; | |
873 | success += rs_sta->win[i].success_counter * | |
874 | iwl3945_rates[i].ieee; | |
b481de9c | 875 | |
fa254a68 | 876 | if (rs_sta->win[i].stamp) { |
b481de9c | 877 | int delta = |
fa254a68 | 878 | jiffies_to_msecs(now - rs_sta->win[i].stamp); |
b481de9c ZY |
879 | |
880 | if (delta > max_time) | |
881 | max_time = delta; | |
882 | ||
883 | count += sprintf(&buf[count], "%5dms\n", delta); | |
884 | } else | |
885 | buf[count++] = '\n'; | |
886 | ||
bb8c093b | 887 | j = iwl3945_get_prev_ieee_rate(i); |
b481de9c ZY |
888 | if (j == i) |
889 | break; | |
890 | i = j; | |
891 | } | |
fa254a68 | 892 | spin_unlock_irqrestore(&rs_sta->lock, flags); |
b481de9c ZY |
893 | sta_info_put(sta); |
894 | ||
895 | /* Display the average rate of all samples taken. | |
896 | * | |
897 | * NOTE: We multiple # of samples by 2 since the IEEE measurement | |
bb8c093b | 898 | * added from iwl3945_rates is actually 2X the rate */ |
b481de9c ZY |
899 | if (samples) |
900 | count += sprintf( | |
901 | &buf[count], | |
902 | "\nAverage rate is %3d.%02dMbs over last %4dms\n" | |
903 | "%3d%% success (%d good packets over %d tries)\n", | |
904 | success / (2 * samples), (success * 5 / samples) % 10, | |
905 | max_time, good * 100 / samples, good, samples); | |
906 | else | |
907 | count += sprintf(&buf[count], "\nAverage rate: 0Mbs\n"); | |
908 | ||
909 | return count; | |
910 | } | |
911 | ||
bb8c093b | 912 | void iwl3945_rate_scale_init(struct ieee80211_hw *hw, s32 sta_id) |
b481de9c | 913 | { |
bb8c093b | 914 | struct iwl3945_priv *priv = hw->priv; |
b481de9c ZY |
915 | s32 rssi = 0; |
916 | unsigned long flags; | |
917 | struct ieee80211_local *local = hw_to_local(hw); | |
fa254a68 | 918 | struct iwl3945_rs_sta *rs_sta; |
b481de9c ZY |
919 | struct sta_info *sta; |
920 | ||
921 | IWL_DEBUG_RATE("enter\n"); | |
922 | ||
923 | if (!local->rate_ctrl->ops->name || | |
924 | strcmp(local->rate_ctrl->ops->name, RS_NAME)) { | |
925 | IWL_WARNING("iwl-3945-rs not selected as rate control algo!\n"); | |
926 | IWL_DEBUG_RATE("leave - mac80211 picked the wrong RC algo.\n"); | |
927 | return; | |
928 | } | |
929 | ||
930 | sta = sta_info_get(local, priv->stations[sta_id].sta.sta.addr); | |
931 | if (!sta || !sta->rate_ctrl_priv) { | |
932 | if (sta) | |
933 | sta_info_put(sta); | |
934 | IWL_DEBUG_RATE("leave - no private rate data!\n"); | |
935 | return; | |
936 | } | |
937 | ||
fa254a68 | 938 | rs_sta = (void *)sta->rate_ctrl_priv; |
b481de9c | 939 | |
fa254a68 | 940 | spin_lock_irqsave(&rs_sta->lock, flags); |
b481de9c | 941 | |
fa254a68 | 942 | rs_sta->tgg = 0; |
8318d78a JB |
943 | switch (priv->band) { |
944 | case IEEE80211_BAND_2GHZ: | |
945 | /* TODO: this always does G, not a regression */ | |
b481de9c | 946 | if (priv->active_rxon.flags & RXON_FLG_TGG_PROTECT_MSK) { |
fa254a68 TW |
947 | rs_sta->tgg = 1; |
948 | rs_sta->expected_tpt = iwl3945_expected_tpt_g_prot; | |
b481de9c | 949 | } else |
fa254a68 | 950 | rs_sta->expected_tpt = iwl3945_expected_tpt_g; |
b481de9c ZY |
951 | break; |
952 | ||
8318d78a | 953 | case IEEE80211_BAND_5GHZ: |
fa254a68 | 954 | rs_sta->expected_tpt = iwl3945_expected_tpt_a; |
b481de9c | 955 | break; |
8318d78a JB |
956 | case IEEE80211_NUM_BANDS: |
957 | BUG(); | |
b481de9c ZY |
958 | break; |
959 | } | |
960 | ||
961 | sta_info_put(sta); | |
fa254a68 | 962 | spin_unlock_irqrestore(&rs_sta->lock, flags); |
b481de9c ZY |
963 | |
964 | rssi = priv->last_rx_rssi; | |
965 | if (rssi == 0) | |
966 | rssi = IWL_MIN_RSSI_VAL; | |
967 | ||
968 | IWL_DEBUG(IWL_DL_INFO | IWL_DL_RATE, "Network RSSI: %d\n", rssi); | |
969 | ||
8318d78a | 970 | rs_sta->start_rate = iwl3945_get_rate_index_by_rssi(rssi, priv->band); |
b481de9c ZY |
971 | |
972 | IWL_DEBUG_RATE("leave: rssi %d assign rate index: " | |
fa254a68 TW |
973 | "%d (plcp 0x%x)\n", rssi, rs_sta->start_rate, |
974 | iwl3945_rates[rs_sta->start_rate].plcp); | |
b481de9c ZY |
975 | } |
976 | ||
bb8c093b | 977 | void iwl3945_rate_control_register(struct ieee80211_hw *hw) |
b481de9c ZY |
978 | { |
979 | ieee80211_rate_control_register(&rs_ops); | |
980 | } | |
981 | ||
bb8c093b | 982 | void iwl3945_rate_control_unregister(struct ieee80211_hw *hw) |
b481de9c ZY |
983 | { |
984 | ieee80211_rate_control_unregister(&rs_ops); | |
985 | } | |
986 | ||
987 |