blob: dc0dfdd520ab00d7f6289786009b56cff978f563 [file] [log] [blame]
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -08001/******************************************************************************
2 *
Wey-Yi Guybe663ab2011-02-21 11:27:26 -08003 * Copyright(c) 2005 - 2011 Intel Corporation. All rights reserved.
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -08004 *
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 * Intel Linux Wireless <ilw@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/slab.h>
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -080031#include <net/mac80211.h>
32
33#include <linux/netdevice.h>
34#include <linux/etherdevice.h>
35#include <linux/delay.h>
36
37#include <linux/workqueue.h>
38
Stanislaw Gruszkad4459a92011-08-31 10:59:02 +020039#include "commands.h"
Stanislaw Gruszka6bbb1372011-08-30 14:12:12 +020040#include "3945.h"
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -080041
42#define RS_NAME "iwl-3945-rs"
43
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +020044static s32 il3945_expected_tpt_g[RATE_COUNT_3945] = {
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -080045 7, 13, 35, 58, 0, 0, 76, 104, 130, 168, 191, 202
46};
47
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +020048static s32 il3945_expected_tpt_g_prot[RATE_COUNT_3945] = {
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -080049 7, 13, 35, 58, 0, 0, 0, 80, 93, 113, 123, 125
50};
51
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +020052static s32 il3945_expected_tpt_a[RATE_COUNT_3945] = {
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -080053 0, 0, 0, 0, 40, 57, 72, 98, 121, 154, 177, 186
54};
55
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +020056static s32 il3945_expected_tpt_b[RATE_COUNT_3945] = {
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -080057 7, 13, 35, 58, 0, 0, 0, 0, 0, 0, 0, 0
58};
59
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +020060struct il3945_tpt_entry {
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -080061 s8 min_rssi;
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +010062 u8 idx;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -080063};
64
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +020065static struct il3945_tpt_entry il3945_tpt_table_a[] = {
Stanislaw Gruszka2d09b062011-08-26 16:10:40 +020066 {-60, RATE_54M_IDX},
67 {-64, RATE_48M_IDX},
68 {-72, RATE_36M_IDX},
69 {-80, RATE_24M_IDX},
70 {-84, RATE_18M_IDX},
71 {-85, RATE_12M_IDX},
72 {-87, RATE_9M_IDX},
73 {-89, RATE_6M_IDX}
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -080074};
75
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +020076static struct il3945_tpt_entry il3945_tpt_table_g[] = {
Stanislaw Gruszka2d09b062011-08-26 16:10:40 +020077 {-60, RATE_54M_IDX},
78 {-64, RATE_48M_IDX},
79 {-68, RATE_36M_IDX},
80 {-80, RATE_24M_IDX},
81 {-84, RATE_18M_IDX},
82 {-85, RATE_12M_IDX},
83 {-86, RATE_11M_IDX},
84 {-88, RATE_5M_IDX},
85 {-90, RATE_2M_IDX},
86 {-92, RATE_1M_IDX}
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -080087};
88
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +020089#define RATE_MAX_WINDOW 62
90#define RATE_FLUSH (3*HZ)
91#define RATE_WIN_FLUSH (HZ/2)
Stanislaw Gruszkad3175162011-11-15 11:25:42 +010092#define IL39_RATE_HIGH_TH 11520
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +020093#define IL_SUCCESS_UP_TH 8960
94#define IL_SUCCESS_DOWN_TH 10880
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +020095#define RATE_MIN_FAILURE_TH 6
96#define RATE_MIN_SUCCESS_TH 8
97#define RATE_DECREASE_TH 1920
98#define RATE_RETRY_TH 15
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -080099
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100100static u8 il3945_get_rate_idx_by_rssi(s32 rssi, enum ieee80211_band band)
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800101{
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100102 u32 idx = 0;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800103 u32 table_size = 0;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200104 struct il3945_tpt_entry *tpt_table = NULL;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800105
Stanislaw Gruszka232913b2011-08-26 10:45:16 +0200106 if (rssi < IL_MIN_RSSI_VAL || rssi > IL_MAX_RSSI_VAL)
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200107 rssi = IL_MIN_RSSI_VAL;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800108
109 switch (band) {
110 case IEEE80211_BAND_2GHZ:
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200111 tpt_table = il3945_tpt_table_g;
112 table_size = ARRAY_SIZE(il3945_tpt_table_g);
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800113 break;
114
115 case IEEE80211_BAND_5GHZ:
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200116 tpt_table = il3945_tpt_table_a;
117 table_size = ARRAY_SIZE(il3945_tpt_table_a);
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800118 break;
119
120 default:
121 BUG();
122 break;
123 }
124
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100125 while (idx < table_size && rssi < tpt_table[idx].min_rssi)
126 idx++;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800127
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100128 idx = min(idx, (table_size - 1));
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800129
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100130 return tpt_table[idx].idx;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800131}
132
Stanislaw Gruszka6ce1dc42011-08-26 15:49:28 +0200133static void il3945_clear_win(struct il3945_rate_scale_data *win)
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800134{
Stanislaw Gruszka6ce1dc42011-08-26 15:49:28 +0200135 win->data = 0;
136 win->success_counter = 0;
137 win->success_ratio = -1;
138 win->counter = 0;
139 win->average_tpt = IL_INVALID_VALUE;
140 win->stamp = 0;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800141}
142
143/**
Stanislaw Gruszka6ce1dc42011-08-26 15:49:28 +0200144 * il3945_rate_scale_flush_wins - flush out the rate scale wins
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800145 *
Stanislaw Gruszka6ce1dc42011-08-26 15:49:28 +0200146 * Returns the number of wins that have gathered data but were
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800147 * not flushed. If there were any that were not flushed, then
148 * reschedule the rate flushing routine.
149 */
Stanislaw Gruszka6ce1dc42011-08-26 15:49:28 +0200150static int il3945_rate_scale_flush_wins(struct il3945_rs_sta *rs_sta)
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800151{
152 int unflushed = 0;
153 int i;
154 unsigned long flags;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200155 struct il_priv *il __maybe_unused = rs_sta->il;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800156
157 /*
158 * For each rate, if we have collected data on that rate
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200159 * and it has been more than RATE_WIN_FLUSH
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +0200160 * since we flushed, clear out the gathered stats
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800161 */
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200162 for (i = 0; i < RATE_COUNT_3945; i++) {
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800163 if (!rs_sta->win[i].counter)
164 continue;
165
166 spin_lock_irqsave(&rs_sta->lock, flags);
167 if (time_after(jiffies, rs_sta->win[i].stamp +
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200168 RATE_WIN_FLUSH)) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100169 D_RATE("flushing %d samples of rate "
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100170 "idx %d\n",
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800171 rs_sta->win[i].counter, i);
Stanislaw Gruszka6ce1dc42011-08-26 15:49:28 +0200172 il3945_clear_win(&rs_sta->win[i]);
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800173 } else
174 unflushed++;
175 spin_unlock_irqrestore(&rs_sta->lock, flags);
176 }
177
178 return unflushed;
179}
180
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200181#define RATE_FLUSH_MAX 5000 /* msec */
182#define RATE_FLUSH_MIN 50 /* msec */
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200183#define IL_AVERAGE_PACKETS 1500
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800184
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200185static void il3945_bg_rate_scale_flush(unsigned long data)
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800186{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200187 struct il3945_rs_sta *rs_sta = (void *)data;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200188 struct il_priv *il __maybe_unused = rs_sta->il;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800189 int unflushed = 0;
190 unsigned long flags;
191 u32 packet_count, duration, pps;
192
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100193 D_RATE("enter\n");
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800194
Stanislaw Gruszka6ce1dc42011-08-26 15:49:28 +0200195 unflushed = il3945_rate_scale_flush_wins(rs_sta);
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800196
197 spin_lock_irqsave(&rs_sta->lock, flags);
198
199 /* Number of packets Rx'd since last time this timer ran */
200 packet_count = (rs_sta->tx_packets - rs_sta->last_tx_packets) + 1;
201
202 rs_sta->last_tx_packets = rs_sta->tx_packets + 1;
203
204 if (unflushed) {
205 duration =
206 jiffies_to_msecs(jiffies - rs_sta->last_partial_flush);
207
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100208 D_RATE("Tx'd %d packets in %dms\n",
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800209 packet_count, duration);
210
211 /* Determine packets per second */
212 if (duration)
213 pps = (packet_count * 1000) / duration;
214 else
215 pps = 0;
216
217 if (pps) {
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200218 duration = (IL_AVERAGE_PACKETS * 1000) / pps;
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200219 if (duration < RATE_FLUSH_MIN)
220 duration = RATE_FLUSH_MIN;
221 else if (duration > RATE_FLUSH_MAX)
222 duration = RATE_FLUSH_MAX;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800223 } else
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200224 duration = RATE_FLUSH_MAX;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800225
226 rs_sta->flush_time = msecs_to_jiffies(duration);
227
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100228 D_RATE("new flush period: %d msec ave %d\n",
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800229 duration, packet_count);
230
231 mod_timer(&rs_sta->rate_scale_flush, jiffies +
232 rs_sta->flush_time);
233
234 rs_sta->last_partial_flush = jiffies;
235 } else {
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200236 rs_sta->flush_time = RATE_FLUSH;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800237 rs_sta->flush_pending = 0;
238 }
239 /* If there weren't any unflushed entries, we don't schedule the timer
240 * to run again */
241
242 rs_sta->last_flush = jiffies;
243
244 spin_unlock_irqrestore(&rs_sta->lock, flags);
245
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100246 D_RATE("leave\n");
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800247}
248
249/**
Stanislaw Gruszka6ce1dc42011-08-26 15:49:28 +0200250 * il3945_collect_tx_data - Update the success/failure sliding win
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800251 *
Stanislaw Gruszka6ce1dc42011-08-26 15:49:28 +0200252 * We keep a sliding win of the last 64 packets transmitted
253 * at this rate. win->data contains the bitmask of successful
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800254 * packets.
255 */
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200256static void il3945_collect_tx_data(struct il3945_rs_sta *rs_sta,
Stanislaw Gruszka6ce1dc42011-08-26 15:49:28 +0200257 struct il3945_rate_scale_data *win,
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100258 int success, int retries, int idx)
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800259{
260 unsigned long flags;
261 s32 fail_count;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200262 struct il_priv *il __maybe_unused = rs_sta->il;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800263
264 if (!retries) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100265 D_RATE("leave: retries == 0 -- should be at least 1\n");
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800266 return;
267 }
268
269 spin_lock_irqsave(&rs_sta->lock, flags);
270
271 /*
272 * Keep track of only the latest 62 tx frame attempts in this rate's
Stanislaw Gruszka6ce1dc42011-08-26 15:49:28 +0200273 * history win; anything older isn't really relevant any more.
274 * If we have filled up the sliding win, drop the oldest attempt;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800275 * if the oldest attempt (highest bit in bitmap) shows "success",
276 * subtract "1" from the success counter (this is the main reason
277 * we keep these bitmaps!).
278 * */
279 while (retries > 0) {
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200280 if (win->counter >= RATE_MAX_WINDOW) {
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800281
282 /* remove earliest */
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200283 win->counter = RATE_MAX_WINDOW - 1;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800284
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200285 if (win->data & (1ULL << (RATE_MAX_WINDOW - 1))) {
286 win->data &= ~(1ULL << (RATE_MAX_WINDOW - 1));
Stanislaw Gruszka6ce1dc42011-08-26 15:49:28 +0200287 win->success_counter--;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800288 }
289 }
290
291 /* Increment frames-attempted counter */
Stanislaw Gruszka6ce1dc42011-08-26 15:49:28 +0200292 win->counter++;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800293
294 /* Shift bitmap by one frame (throw away oldest history),
295 * OR in "1", and increment "success" if this
296 * frame was successful. */
Stanislaw Gruszka6ce1dc42011-08-26 15:49:28 +0200297 win->data <<= 1;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800298 if (success > 0) {
Stanislaw Gruszka6ce1dc42011-08-26 15:49:28 +0200299 win->success_counter++;
300 win->data |= 0x1;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800301 success--;
302 }
303
304 retries--;
305 }
306
307 /* Calculate current success ratio, avoid divide-by-0! */
Stanislaw Gruszka6ce1dc42011-08-26 15:49:28 +0200308 if (win->counter > 0)
309 win->success_ratio = 128 * (100 * win->success_counter)
310 / win->counter;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800311 else
Stanislaw Gruszka6ce1dc42011-08-26 15:49:28 +0200312 win->success_ratio = IL_INVALID_VALUE;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800313
Stanislaw Gruszka6ce1dc42011-08-26 15:49:28 +0200314 fail_count = win->counter - win->success_counter;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800315
316 /* Calculate average throughput, if we have enough history. */
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200317 if (fail_count >= RATE_MIN_FAILURE_TH ||
318 win->success_counter >= RATE_MIN_SUCCESS_TH)
Stanislaw Gruszka6ce1dc42011-08-26 15:49:28 +0200319 win->average_tpt = ((win->success_ratio *
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100320 rs_sta->expected_tpt[idx] + 64) / 128);
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800321 else
Stanislaw Gruszka6ce1dc42011-08-26 15:49:28 +0200322 win->average_tpt = IL_INVALID_VALUE;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800323
Stanislaw Gruszka6ce1dc42011-08-26 15:49:28 +0200324 /* Tag this win as having been updated */
325 win->stamp = jiffies;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800326
327 spin_unlock_irqrestore(&rs_sta->lock, flags);
328
329}
330
331/*
332 * Called after adding a new station to initialize rate scaling
333 */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200334void il3945_rs_rate_init(struct il_priv *il, struct ieee80211_sta *sta, u8 sta_id)
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800335{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200336 struct ieee80211_hw *hw = il->hw;
337 struct ieee80211_conf *conf = &il->hw->conf;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200338 struct il3945_sta_priv *psta;
339 struct il3945_rs_sta *rs_sta;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800340 struct ieee80211_supported_band *sband;
341 int i;
342
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100343 D_INFO("enter\n");
Stanislaw Gruszka7c2cde22011-11-15 11:29:04 +0100344 if (sta_id == il->ctx.bcast_sta_id)
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800345 goto out;
346
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200347 psta = (struct il3945_sta_priv *) sta->drv_priv;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800348 rs_sta = &psta->rs_sta;
349 sband = hw->wiphy->bands[conf->channel->band];
350
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200351 rs_sta->il = il;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800352
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200353 rs_sta->start_rate = RATE_INVALID;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800354
355 /* default to just 802.11b */
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200356 rs_sta->expected_tpt = il3945_expected_tpt_b;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800357
358 rs_sta->last_partial_flush = jiffies;
359 rs_sta->last_flush = jiffies;
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200360 rs_sta->flush_time = RATE_FLUSH;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800361 rs_sta->last_tx_packets = 0;
362
363 rs_sta->rate_scale_flush.data = (unsigned long)rs_sta;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200364 rs_sta->rate_scale_flush.function = il3945_bg_rate_scale_flush;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800365
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200366 for (i = 0; i < RATE_COUNT_3945; i++)
Stanislaw Gruszka6ce1dc42011-08-26 15:49:28 +0200367 il3945_clear_win(&rs_sta->win[i]);
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800368
369 /* TODO: what is a good starting rate for STA? About middle? Maybe not
370 * the lowest or the highest rate.. Could consider using RSSI from
371 * previous packets? Need to have IEEE 802.1X auth succeed immediately
372 * after assoc.. */
373
374 for (i = sband->n_bitrates - 1; i >= 0; i--) {
375 if (sta->supp_rates[sband->band] & (1 << i)) {
376 rs_sta->last_txrate_idx = i;
377 break;
378 }
379 }
380
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200381 il->_3945.sta_supp_rates = sta->supp_rates[sband->band];
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200382 /* For 5 GHz band it start at IL_FIRST_OFDM_RATE */
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800383 if (sband->band == IEEE80211_BAND_5GHZ) {
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200384 rs_sta->last_txrate_idx += IL_FIRST_OFDM_RATE;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200385 il->_3945.sta_supp_rates = il->_3945.sta_supp_rates <<
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200386 IL_FIRST_OFDM_RATE;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800387 }
388
389out:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200390 il->stations[sta_id].used &= ~IL_STA_UCODE_INPROGRESS;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800391
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100392 D_INFO("leave\n");
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800393}
394
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200395static void *il3945_rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800396{
397 return hw->priv;
398}
399
400/* rate scale requires free function to be implemented */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200401static void il3945_rs_free(void *il)
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800402{
403 return;
404}
405
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200406static void *il3945_rs_alloc_sta(void *il_priv, struct ieee80211_sta *sta, gfp_t gfp)
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800407{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200408 struct il3945_rs_sta *rs_sta;
409 struct il3945_sta_priv *psta = (void *) sta->drv_priv;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200410 struct il_priv *il __maybe_unused = il_priv;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800411
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100412 D_RATE("enter\n");
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800413
414 rs_sta = &psta->rs_sta;
415
416 spin_lock_init(&rs_sta->lock);
417 init_timer(&rs_sta->rate_scale_flush);
418
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100419 D_RATE("leave\n");
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800420
421 return rs_sta;
422}
423
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200424static void il3945_rs_free_sta(void *il_priv, struct ieee80211_sta *sta,
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200425 void *il_sta)
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800426{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200427 struct il3945_rs_sta *rs_sta = il_sta;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800428
429 /*
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200430 * Be careful not to use any members of il3945_rs_sta (like trying
431 * to use il_priv to print out debugging) since it may not be fully
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800432 * initialized at this point.
433 */
434 del_timer_sync(&rs_sta->rate_scale_flush);
435}
436
437
438/**
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200439 * il3945_rs_tx_status - Update rate control values based on Tx results
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800440 *
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200441 * NOTE: Uses il_priv->retry_rate for the # of retries attempted by
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800442 * the hardware for each rate.
443 */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200444static void il3945_rs_tx_status(void *il_rate, struct ieee80211_supported_band *sband,
445 struct ieee80211_sta *sta, void *il_sta,
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800446 struct sk_buff *skb)
447{
448 s8 retries = 0, current_count;
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100449 int scale_rate_idx, first_idx, last_idx;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800450 unsigned long flags;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200451 struct il_priv *il = (struct il_priv *)il_rate;
452 struct il3945_rs_sta *rs_sta = il_sta;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800453 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
454
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100455 D_RATE("enter\n");
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800456
457 retries = info->status.rates[0].count;
458 /* Sanity Check for retries */
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200459 if (retries > RATE_RETRY_TH)
460 retries = RATE_RETRY_TH;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800461
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100462 first_idx = sband->bitrates[info->status.rates[0].idx].hw_value;
463 if (first_idx < 0 || first_idx >= RATE_COUNT_3945) {
464 D_RATE("leave: Rate out of bounds: %d\n", first_idx);
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800465 return;
466 }
467
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200468 if (!il_sta) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100469 D_RATE("leave: No STA il data to update!\n");
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800470 return;
471 }
472
473 /* Treat uninitialized rate scaling data same as non-existing. */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200474 if (!rs_sta->il) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100475 D_RATE("leave: STA il data uninitialized!\n");
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800476 return;
477 }
478
479
480 rs_sta->tx_packets++;
481
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100482 scale_rate_idx = first_idx;
483 last_idx = first_idx;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800484
485 /*
Stanislaw Gruszka6ce1dc42011-08-26 15:49:28 +0200486 * Update the win for each rate. We determine which rates
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800487 * were Tx'd based on the total number of retries vs. the number
488 * of retries configured for each rate -- currently set to the
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200489 * il value 'retry_rate' vs. rate specific
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800490 *
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100491 * On exit from this while loop last_idx indicates the rate
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800492 * at which the frame was finally transmitted (or failed if no
493 * ACK)
494 */
495 while (retries > 1) {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200496 if ((retries - 1) < il->retry_rate) {
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800497 current_count = (retries - 1);
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100498 last_idx = scale_rate_idx;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800499 } else {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200500 current_count = il->retry_rate;
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100501 last_idx = il3945_rs_next_rate(il,
502 scale_rate_idx);
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800503 }
504
505 /* Update this rate accounting for as many retries
506 * as was used for it (per current_count) */
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200507 il3945_collect_tx_data(rs_sta,
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100508 &rs_sta->win[scale_rate_idx],
509 0, current_count, scale_rate_idx);
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100510 D_RATE("Update rate %d for %d retries.\n",
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100511 scale_rate_idx, current_count);
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800512
513 retries -= current_count;
514
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100515 scale_rate_idx = last_idx;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800516 }
517
518
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100519 /* Update the last idx win with success/failure based on ACK */
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100520 D_RATE("Update rate %d with %s.\n",
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100521 last_idx,
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800522 (info->flags & IEEE80211_TX_STAT_ACK) ?
523 "success" : "failure");
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200524 il3945_collect_tx_data(rs_sta,
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100525 &rs_sta->win[last_idx],
526 info->flags & IEEE80211_TX_STAT_ACK, 1, last_idx);
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800527
Stanislaw Gruszka6ce1dc42011-08-26 15:49:28 +0200528 /* We updated the rate scale win -- if its been more than
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800529 * flush_time since the last run, schedule the flush
530 * again */
531 spin_lock_irqsave(&rs_sta->lock, flags);
532
533 if (!rs_sta->flush_pending &&
534 time_after(jiffies, rs_sta->last_flush +
535 rs_sta->flush_time)) {
536
537 rs_sta->last_partial_flush = jiffies;
538 rs_sta->flush_pending = 1;
539 mod_timer(&rs_sta->rate_scale_flush,
540 jiffies + rs_sta->flush_time);
541 }
542
543 spin_unlock_irqrestore(&rs_sta->lock, flags);
544
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100545 D_RATE("leave\n");
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800546}
547
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200548static u16 il3945_get_adjacent_rate(struct il3945_rs_sta *rs_sta,
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100549 u8 idx, u16 rate_mask, enum ieee80211_band band)
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800550{
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200551 u8 high = RATE_INVALID;
552 u8 low = RATE_INVALID;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200553 struct il_priv *il __maybe_unused = rs_sta->il;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800554
555 /* 802.11A walks to the next literal adjacent rate in
556 * the rate table */
557 if (unlikely(band == IEEE80211_BAND_5GHZ)) {
558 int i;
559 u32 mask;
560
561 /* Find the previous rate that is in the rate mask */
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100562 i = idx - 1;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800563 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
564 if (rate_mask & mask) {
565 low = i;
566 break;
567 }
568 }
569
570 /* Find the next rate that is in the rate mask */
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100571 i = idx + 1;
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200572 for (mask = (1 << i); i < RATE_COUNT_3945;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800573 i++, mask <<= 1) {
574 if (rate_mask & mask) {
575 high = i;
576 break;
577 }
578 }
579
580 return (high << 8) | low;
581 }
582
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100583 low = idx;
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200584 while (low != RATE_INVALID) {
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800585 if (rs_sta->tgg)
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200586 low = il3945_rates[low].prev_rs_tgg;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800587 else
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200588 low = il3945_rates[low].prev_rs;
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200589 if (low == RATE_INVALID)
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800590 break;
591 if (rate_mask & (1 << low))
592 break;
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100593 D_RATE("Skipping masked lower rate: %d\n", low);
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800594 }
595
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100596 high = idx;
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200597 while (high != RATE_INVALID) {
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800598 if (rs_sta->tgg)
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200599 high = il3945_rates[high].next_rs_tgg;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800600 else
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200601 high = il3945_rates[high].next_rs;
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200602 if (high == RATE_INVALID)
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800603 break;
604 if (rate_mask & (1 << high))
605 break;
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100606 D_RATE("Skipping masked higher rate: %d\n", high);
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800607 }
608
609 return (high << 8) | low;
610}
611
612/**
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200613 * il3945_rs_get_rate - find the rate for the requested packet
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800614 *
615 * Returns the ieee80211_rate structure allocated by the driver.
616 *
617 * The rate control algorithm has no internal mapping between hw_mode's
618 * rate ordering and the rate ordering used by the rate control algorithm.
619 *
620 * The rate control algorithm uses a single table of rates that goes across
621 * the entire A/B/G spectrum vs. being limited to just one particular
622 * hw_mode.
623 *
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100624 * As such, we can't convert the idx obtained below into the hw_mode's
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800625 * rate table and must reference the driver allocated rate table
626 *
627 */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200628static void il3945_rs_get_rate(void *il_r, struct ieee80211_sta *sta,
629 void *il_sta, struct ieee80211_tx_rate_control *txrc)
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800630{
631 struct ieee80211_supported_band *sband = txrc->sband;
632 struct sk_buff *skb = txrc->skb;
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200633 u8 low = RATE_INVALID;
634 u8 high = RATE_INVALID;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800635 u16 high_low;
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100636 int idx;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200637 struct il3945_rs_sta *rs_sta = il_sta;
Stanislaw Gruszka6ce1dc42011-08-26 15:49:28 +0200638 struct il3945_rate_scale_data *win = NULL;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200639 int current_tpt = IL_INVALID_VALUE;
640 int low_tpt = IL_INVALID_VALUE;
641 int high_tpt = IL_INVALID_VALUE;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800642 u32 fail_count;
643 s8 scale_action = 0;
644 unsigned long flags;
Dan Carpenterc3371d62011-02-26 04:56:53 +0300645 u16 rate_mask;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800646 s8 max_rate_idx = -1;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200647 struct il_priv *il __maybe_unused = (struct il_priv *)il_r;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800648 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
649
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100650 D_RATE("enter\n");
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800651
652 /* Treat uninitialized rate scaling data same as non-existing. */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200653 if (rs_sta && !rs_sta->il) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100654 D_RATE("Rate scaling information not initialized yet.\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200655 il_sta = NULL;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800656 }
657
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200658 if (rate_control_send_low(sta, il_sta, txrc))
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800659 return;
660
661 rate_mask = sta->supp_rates[sband->band];
662
663 /* get user max rate if set */
664 max_rate_idx = txrc->max_rate_idx;
Stanislaw Gruszka232913b2011-08-26 10:45:16 +0200665 if (sband->band == IEEE80211_BAND_5GHZ && max_rate_idx != -1)
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200666 max_rate_idx += IL_FIRST_OFDM_RATE;
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200667 if (max_rate_idx < 0 || max_rate_idx >= RATE_COUNT)
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800668 max_rate_idx = -1;
669
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100670 idx = min(rs_sta->last_txrate_idx & 0xffff, RATE_COUNT_3945 - 1);
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800671
672 if (sband->band == IEEE80211_BAND_5GHZ)
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200673 rate_mask = rate_mask << IL_FIRST_OFDM_RATE;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800674
675 spin_lock_irqsave(&rs_sta->lock, flags);
676
677 /* for recent assoc, choose best rate regarding
678 * to rssi value
679 */
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200680 if (rs_sta->start_rate != RATE_INVALID) {
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100681 if (rs_sta->start_rate < idx &&
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800682 (rate_mask & (1 << rs_sta->start_rate)))
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100683 idx = rs_sta->start_rate;
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200684 rs_sta->start_rate = RATE_INVALID;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800685 }
686
687 /* force user max rate if set by user */
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100688 if (max_rate_idx != -1 && max_rate_idx < idx) {
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800689 if (rate_mask & (1 << max_rate_idx))
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100690 idx = max_rate_idx;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800691 }
692
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100693 win = &(rs_sta->win[idx]);
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800694
Stanislaw Gruszka6ce1dc42011-08-26 15:49:28 +0200695 fail_count = win->counter - win->success_counter;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800696
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200697 if (fail_count < RATE_MIN_FAILURE_TH &&
698 win->success_counter < RATE_MIN_SUCCESS_TH) {
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800699 spin_unlock_irqrestore(&rs_sta->lock, flags);
700
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100701 D_RATE("Invalid average_tpt on rate %d: "
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800702 "counter: %d, success_counter: %d, "
703 "expected_tpt is %sNULL\n",
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100704 idx,
Stanislaw Gruszka6ce1dc42011-08-26 15:49:28 +0200705 win->counter,
706 win->success_counter,
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800707 rs_sta->expected_tpt ? "not " : "");
708
709 /* Can't calculate this yet; not enough history */
Stanislaw Gruszka6ce1dc42011-08-26 15:49:28 +0200710 win->average_tpt = IL_INVALID_VALUE;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800711 goto out;
712
713 }
714
Stanislaw Gruszka6ce1dc42011-08-26 15:49:28 +0200715 current_tpt = win->average_tpt;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800716
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100717 high_low = il3945_get_adjacent_rate(rs_sta, idx, rate_mask,
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800718 sband->band);
719 low = high_low & 0xff;
720 high = (high_low >> 8) & 0xff;
721
722 /* If user set max rate, dont allow higher than user constrain */
Stanislaw Gruszka232913b2011-08-26 10:45:16 +0200723 if (max_rate_idx != -1 && max_rate_idx < high)
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200724 high = RATE_INVALID;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800725
726 /* Collect Measured throughputs of adjacent rates */
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200727 if (low != RATE_INVALID)
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800728 low_tpt = rs_sta->win[low].average_tpt;
729
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200730 if (high != RATE_INVALID)
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800731 high_tpt = rs_sta->win[high].average_tpt;
732
733 spin_unlock_irqrestore(&rs_sta->lock, flags);
734
735 scale_action = 0;
736
737 /* Low success ratio , need to drop the rate */
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200738 if (win->success_ratio < RATE_DECREASE_TH || !current_tpt) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100739 D_RATE("decrease rate because of low success_ratio\n");
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800740 scale_action = -1;
741 /* No throughput measured yet for adjacent rates,
742 * try increase */
Stanislaw Gruszka232913b2011-08-26 10:45:16 +0200743 } else if (low_tpt == IL_INVALID_VALUE &&
744 high_tpt == IL_INVALID_VALUE) {
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800745
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200746 if (high != RATE_INVALID && win->success_ratio >= RATE_INCREASE_TH)
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800747 scale_action = 1;
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200748 else if (low != RATE_INVALID)
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800749 scale_action = 0;
750
751 /* Both adjacent throughputs are measured, but neither one has
752 * better throughput; we're using the best rate, don't change
753 * it! */
Stanislaw Gruszka232913b2011-08-26 10:45:16 +0200754 } else if (low_tpt != IL_INVALID_VALUE &&
755 high_tpt != IL_INVALID_VALUE &&
756 low_tpt < current_tpt && high_tpt < current_tpt) {
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800757
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100758 D_RATE("No action -- low [%d] & high [%d] < "
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800759 "current_tpt [%d]\n",
760 low_tpt, high_tpt, current_tpt);
761 scale_action = 0;
762
763 /* At least one of the rates has better throughput */
764 } else {
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200765 if (high_tpt != IL_INVALID_VALUE) {
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800766
767 /* High rate has better throughput, Increase
768 * rate */
769 if (high_tpt > current_tpt &&
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200770 win->success_ratio >= RATE_INCREASE_TH)
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800771 scale_action = 1;
772 else {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100773 D_RATE(
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800774 "decrease rate because of high tpt\n");
775 scale_action = 0;
776 }
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200777 } else if (low_tpt != IL_INVALID_VALUE) {
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800778 if (low_tpt > current_tpt) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100779 D_RATE(
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800780 "decrease rate because of low tpt\n");
781 scale_action = -1;
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200782 } else if (win->success_ratio >= RATE_INCREASE_TH) {
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800783 /* Lower rate has better
784 * throughput,decrease rate */
785 scale_action = 1;
786 }
787 }
788 }
789
790 /* Sanity check; asked for decrease, but success rate or throughput
791 * has been good at old rate. Don't change it. */
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200792 if (scale_action == -1 && low != RATE_INVALID &&
793 (win->success_ratio > RATE_HIGH_TH ||
Stanislaw Gruszka232913b2011-08-26 10:45:16 +0200794 current_tpt > 100 * rs_sta->expected_tpt[low]))
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800795 scale_action = 0;
796
797 switch (scale_action) {
798 case -1:
799
800 /* Decrese rate */
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200801 if (low != RATE_INVALID)
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100802 idx = low;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800803 break;
804
805 case 1:
806 /* Increase rate */
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200807 if (high != RATE_INVALID)
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100808 idx = high;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800809
810 break;
811
812 case 0:
813 default:
814 /* No change */
815 break;
816 }
817
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100818 D_RATE("Selected %d (action %d) - low %d high %d\n",
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100819 idx, scale_action, low, high);
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800820
821 out:
822
Stanislaw Gruszka7c251012011-08-26 17:24:59 +0200823 if (sband->band == IEEE80211_BAND_5GHZ) {
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100824 if (WARN_ON_ONCE(idx < IL_FIRST_OFDM_RATE))
825 idx = IL_FIRST_OFDM_RATE;
826 rs_sta->last_txrate_idx = idx;
827 info->control.rates[0].idx = idx - IL_FIRST_OFDM_RATE;
Stanislaw Gruszka7c251012011-08-26 17:24:59 +0200828 } else {
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100829 rs_sta->last_txrate_idx = idx;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800830 info->control.rates[0].idx = rs_sta->last_txrate_idx;
Stanislaw Gruszka7c251012011-08-26 17:24:59 +0200831 }
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800832
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100833 D_RATE("leave: %d\n", idx);
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800834}
835
836#ifdef CONFIG_MAC80211_DEBUGFS
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200837static int il3945_open_file_generic(struct inode *inode, struct file *file)
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800838{
839 file->private_data = inode->i_private;
840 return 0;
841}
842
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200843static ssize_t il3945_sta_dbgfs_stats_table_read(struct file *file,
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800844 char __user *user_buf,
845 size_t count, loff_t *ppos)
846{
847 char *buff;
848 int desc = 0;
849 int j;
850 ssize_t ret;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200851 struct il3945_rs_sta *lq_sta = file->private_data;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800852
853 buff = kmalloc(1024, GFP_KERNEL);
854 if (!buff)
855 return -ENOMEM;
856
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100857 desc += sprintf(buff + desc, "tx packets=%d last rate idx=%d\n"
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800858 "rate=0x%X flush time %d\n",
859 lq_sta->tx_packets,
860 lq_sta->last_txrate_idx,
861 lq_sta->start_rate, jiffies_to_msecs(lq_sta->flush_time));
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200862 for (j = 0; j < RATE_COUNT_3945; j++) {
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800863 desc += sprintf(buff+desc,
864 "counter=%d success=%d %%=%d\n",
865 lq_sta->win[j].counter,
866 lq_sta->win[j].success_counter,
867 lq_sta->win[j].success_ratio);
868 }
869 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
870 kfree(buff);
871 return ret;
872}
873
874static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200875 .read = il3945_sta_dbgfs_stats_table_read,
876 .open = il3945_open_file_generic,
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800877 .llseek = default_llseek,
878};
879
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200880static void il3945_add_debugfs(void *il, void *il_sta,
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800881 struct dentry *dir)
882{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200883 struct il3945_rs_sta *lq_sta = il_sta;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800884
885 lq_sta->rs_sta_dbgfs_stats_table_file =
886 debugfs_create_file("rate_stats_table", 0600, dir,
887 lq_sta, &rs_sta_dbgfs_stats_table_ops);
888
889}
890
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200891static void il3945_remove_debugfs(void *il, void *il_sta)
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800892{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200893 struct il3945_rs_sta *lq_sta = il_sta;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800894 debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
895}
896#endif
897
898/*
899 * Initialization of rate scaling information is done by driver after
900 * the station is added. Since mac80211 calls this function before a
901 * station is added we ignore it.
902 */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200903static void il3945_rs_rate_init_stub(void *il_r,
Wey-Yi Guybe663ab2011-02-21 11:27:26 -0800904 struct ieee80211_supported_band *sband,
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200905 struct ieee80211_sta *sta, void *il_sta)
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800906{
907}
908
909static struct rate_control_ops rs_ops = {
910 .module = NULL,
911 .name = RS_NAME,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200912 .tx_status = il3945_rs_tx_status,
913 .get_rate = il3945_rs_get_rate,
914 .rate_init = il3945_rs_rate_init_stub,
915 .alloc = il3945_rs_alloc,
916 .free = il3945_rs_free,
917 .alloc_sta = il3945_rs_alloc_sta,
918 .free_sta = il3945_rs_free_sta,
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800919#ifdef CONFIG_MAC80211_DEBUGFS
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200920 .add_sta_debugfs = il3945_add_debugfs,
921 .remove_sta_debugfs = il3945_remove_debugfs,
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800922#endif
923
924};
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200925void il3945_rate_scale_init(struct ieee80211_hw *hw, s32 sta_id)
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800926{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200927 struct il_priv *il = hw->priv;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800928 s32 rssi = 0;
929 unsigned long flags;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200930 struct il3945_rs_sta *rs_sta;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800931 struct ieee80211_sta *sta;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200932 struct il3945_sta_priv *psta;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800933
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100934 D_RATE("enter\n");
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800935
936 rcu_read_lock();
937
Stanislaw Gruszka7c2cde22011-11-15 11:29:04 +0100938 sta = ieee80211_find_sta(il->ctx.vif,
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200939 il->stations[sta_id].sta.sta.addr);
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800940 if (!sta) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100941 D_RATE("Unable to find station to initialize rate scaling.\n");
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800942 rcu_read_unlock();
943 return;
944 }
945
946 psta = (void *) sta->drv_priv;
947 rs_sta = &psta->rs_sta;
948
949 spin_lock_irqsave(&rs_sta->lock, flags);
950
951 rs_sta->tgg = 0;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200952 switch (il->band) {
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800953 case IEEE80211_BAND_2GHZ:
954 /* TODO: this always does G, not a regression */
Stanislaw Gruszka7c2cde22011-11-15 11:29:04 +0100955 if (il->ctx.active.flags &
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800956 RXON_FLG_TGG_PROTECT_MSK) {
957 rs_sta->tgg = 1;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200958 rs_sta->expected_tpt = il3945_expected_tpt_g_prot;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800959 } else
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200960 rs_sta->expected_tpt = il3945_expected_tpt_g;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800961 break;
962
963 case IEEE80211_BAND_5GHZ:
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200964 rs_sta->expected_tpt = il3945_expected_tpt_a;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800965 break;
966 case IEEE80211_NUM_BANDS:
967 BUG();
968 break;
969 }
970
971 spin_unlock_irqrestore(&rs_sta->lock, flags);
972
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200973 rssi = il->_3945.last_rx_rssi;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800974 if (rssi == 0)
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200975 rssi = IL_MIN_RSSI_VAL;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800976
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100977 D_RATE("Network RSSI: %d\n", rssi);
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800978
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100979 rs_sta->start_rate = il3945_get_rate_idx_by_rssi(rssi, il->band);
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800980
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100981 D_RATE("leave: rssi %d assign rate idx: "
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800982 "%d (plcp 0x%x)\n", rssi, rs_sta->start_rate,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200983 il3945_rates[rs_sta->start_rate].plcp);
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800984 rcu_read_unlock();
985}
986
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200987int il3945_rate_control_register(void)
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800988{
989 return ieee80211_rate_control_register(&rs_ops);
990}
991
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200992void il3945_rate_control_unregister(void)
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800993{
994 ieee80211_rate_control_unregister(&rs_ops);
995}