blob: 9a45f6f626f69633c938bd386242cdacff6496e7 [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>
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -080028#include <linux/skbuff.h>
29#include <linux/slab.h>
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -080030#include <net/mac80211.h>
31
32#include <linux/netdevice.h>
33#include <linux/etherdevice.h>
34#include <linux/delay.h>
35
36#include <linux/workqueue.h>
37
Stanislaw Gruszkad4459a92011-08-31 10:59:02 +020038#include "commands.h"
Stanislaw Gruszka6bbb1372011-08-30 14:12:12 +020039#include "3945.h"
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -080040
41#define RS_NAME "iwl-3945-rs"
42
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +020043static s32 il3945_expected_tpt_g[RATE_COUNT_3945] = {
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -080044 7, 13, 35, 58, 0, 0, 76, 104, 130, 168, 191, 202
45};
46
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +020047static s32 il3945_expected_tpt_g_prot[RATE_COUNT_3945] = {
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -080048 7, 13, 35, 58, 0, 0, 0, 80, 93, 113, 123, 125
49};
50
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +020051static s32 il3945_expected_tpt_a[RATE_COUNT_3945] = {
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -080052 0, 0, 0, 0, 40, 57, 72, 98, 121, 154, 177, 186
53};
54
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +020055static s32 il3945_expected_tpt_b[RATE_COUNT_3945] = {
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -080056 7, 13, 35, 58, 0, 0, 0, 0, 0, 0, 0, 0
57};
58
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +020059struct il3945_tpt_entry {
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -080060 s8 min_rssi;
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +010061 u8 idx;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -080062};
63
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +020064static struct il3945_tpt_entry il3945_tpt_table_a[] = {
Stanislaw Gruszka2d09b062011-08-26 16:10:40 +020065 {-60, RATE_54M_IDX},
66 {-64, RATE_48M_IDX},
67 {-72, RATE_36M_IDX},
68 {-80, RATE_24M_IDX},
69 {-84, RATE_18M_IDX},
70 {-85, RATE_12M_IDX},
71 {-87, RATE_9M_IDX},
72 {-89, RATE_6M_IDX}
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -080073};
74
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +020075static struct il3945_tpt_entry il3945_tpt_table_g[] = {
Stanislaw Gruszka2d09b062011-08-26 16:10:40 +020076 {-60, RATE_54M_IDX},
77 {-64, RATE_48M_IDX},
78 {-68, RATE_36M_IDX},
79 {-80, RATE_24M_IDX},
80 {-84, RATE_18M_IDX},
81 {-85, RATE_12M_IDX},
82 {-86, RATE_11M_IDX},
83 {-88, RATE_5M_IDX},
84 {-90, RATE_2M_IDX},
85 {-92, RATE_1M_IDX}
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -080086};
87
Stanislaw Gruszka5d0bef92011-12-23 08:13:48 +010088#define RATE_MAX_WINDOW 62
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +020089#define RATE_FLUSH (3*HZ)
Stanislaw Gruszka5d0bef92011-12-23 08:13:48 +010090#define RATE_WIN_FLUSH (HZ/2)
91#define IL39_RATE_HIGH_TH 11520
92#define IL_SUCCESS_UP_TH 8960
93#define IL_SUCCESS_DOWN_TH 10880
94#define RATE_MIN_FAILURE_TH 6
95#define RATE_MIN_SUCCESS_TH 8
96#define RATE_DECREASE_TH 1920
97#define RATE_RETRY_TH 15
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -080098
Stanislaw Gruszkae7392362011-11-15 14:45:59 +010099static u8
100il3945_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;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800114 case IEEE80211_BAND_5GHZ:
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200115 tpt_table = il3945_tpt_table_a;
116 table_size = ARRAY_SIZE(il3945_tpt_table_a);
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800117 break;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800118 default:
119 BUG();
120 break;
121 }
122
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100123 while (idx < table_size && rssi < tpt_table[idx].min_rssi)
124 idx++;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800125
Stanislaw Gruszka5d0bef92011-12-23 08:13:48 +0100126 idx = min(idx, table_size - 1);
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800127
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100128 return tpt_table[idx].idx;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800129}
130
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100131static void
132il3945_clear_win(struct il3945_rate_scale_data *win)
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800133{
Stanislaw Gruszka6ce1dc42011-08-26 15:49:28 +0200134 win->data = 0;
135 win->success_counter = 0;
136 win->success_ratio = -1;
137 win->counter = 0;
138 win->average_tpt = IL_INVALID_VALUE;
139 win->stamp = 0;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800140}
141
142/**
Stanislaw Gruszka6ce1dc42011-08-26 15:49:28 +0200143 * il3945_rate_scale_flush_wins - flush out the rate scale wins
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800144 *
Stanislaw Gruszka6ce1dc42011-08-26 15:49:28 +0200145 * Returns the number of wins that have gathered data but were
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800146 * not flushed. If there were any that were not flushed, then
147 * reschedule the rate flushing routine.
148 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100149static int
150il3945_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);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100167 if (time_after(jiffies, rs_sta->win[i].stamp + RATE_WIN_FLUSH)) {
168 D_RATE("flushing %d samples of rate " "idx %d\n",
169 rs_sta->win[i].counter, i);
Stanislaw Gruszka6ce1dc42011-08-26 15:49:28 +0200170 il3945_clear_win(&rs_sta->win[i]);
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800171 } else
172 unflushed++;
173 spin_unlock_irqrestore(&rs_sta->lock, flags);
174 }
175
176 return unflushed;
177}
178
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200179#define RATE_FLUSH_MAX 5000 /* msec */
180#define RATE_FLUSH_MIN 50 /* msec */
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200181#define IL_AVERAGE_PACKETS 1500
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800182
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100183static void
184il3945_bg_rate_scale_flush(unsigned long data)
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800185{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200186 struct il3945_rs_sta *rs_sta = (void *)data;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200187 struct il_priv *il __maybe_unused = rs_sta->il;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800188 int unflushed = 0;
189 unsigned long flags;
190 u32 packet_count, duration, pps;
191
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100192 D_RATE("enter\n");
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800193
Stanislaw Gruszka6ce1dc42011-08-26 15:49:28 +0200194 unflushed = il3945_rate_scale_flush_wins(rs_sta);
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800195
196 spin_lock_irqsave(&rs_sta->lock, flags);
197
198 /* Number of packets Rx'd since last time this timer ran */
199 packet_count = (rs_sta->tx_packets - rs_sta->last_tx_packets) + 1;
200
201 rs_sta->last_tx_packets = rs_sta->tx_packets + 1;
202
203 if (unflushed) {
204 duration =
205 jiffies_to_msecs(jiffies - rs_sta->last_partial_flush);
206
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100207 D_RATE("Tx'd %d packets in %dms\n", packet_count, duration);
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800208
209 /* Determine packets per second */
210 if (duration)
211 pps = (packet_count * 1000) / duration;
212 else
213 pps = 0;
214
215 if (pps) {
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200216 duration = (IL_AVERAGE_PACKETS * 1000) / pps;
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200217 if (duration < RATE_FLUSH_MIN)
218 duration = RATE_FLUSH_MIN;
219 else if (duration > RATE_FLUSH_MAX)
220 duration = RATE_FLUSH_MAX;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800221 } else
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200222 duration = RATE_FLUSH_MAX;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800223
224 rs_sta->flush_time = msecs_to_jiffies(duration);
225
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100226 D_RATE("new flush period: %d msec ave %d\n", duration,
227 packet_count);
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800228
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100229 mod_timer(&rs_sta->rate_scale_flush,
230 jiffies + rs_sta->flush_time);
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800231
232 rs_sta->last_partial_flush = jiffies;
233 } else {
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200234 rs_sta->flush_time = RATE_FLUSH;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800235 rs_sta->flush_pending = 0;
236 }
237 /* If there weren't any unflushed entries, we don't schedule the timer
238 * to run again */
239
240 rs_sta->last_flush = jiffies;
241
242 spin_unlock_irqrestore(&rs_sta->lock, flags);
243
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100244 D_RATE("leave\n");
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800245}
246
247/**
Stanislaw Gruszka6ce1dc42011-08-26 15:49:28 +0200248 * il3945_collect_tx_data - Update the success/failure sliding win
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800249 *
Stanislaw Gruszka6ce1dc42011-08-26 15:49:28 +0200250 * We keep a sliding win of the last 64 packets transmitted
251 * at this rate. win->data contains the bitmask of successful
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800252 * packets.
253 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100254static void
255il3945_collect_tx_data(struct il3945_rs_sta *rs_sta,
256 struct il3945_rate_scale_data *win, int success,
257 int retries, int idx)
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800258{
259 unsigned long flags;
260 s32 fail_count;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200261 struct il_priv *il __maybe_unused = rs_sta->il;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800262
263 if (!retries) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100264 D_RATE("leave: retries == 0 -- should be at least 1\n");
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800265 return;
266 }
267
268 spin_lock_irqsave(&rs_sta->lock, flags);
269
270 /*
271 * Keep track of only the latest 62 tx frame attempts in this rate's
Stanislaw Gruszka6ce1dc42011-08-26 15:49:28 +0200272 * history win; anything older isn't really relevant any more.
273 * If we have filled up the sliding win, drop the oldest attempt;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800274 * if the oldest attempt (highest bit in bitmap) shows "success",
275 * subtract "1" from the success counter (this is the main reason
276 * we keep these bitmaps!).
277 * */
278 while (retries > 0) {
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200279 if (win->counter >= RATE_MAX_WINDOW) {
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800280
281 /* remove earliest */
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200282 win->counter = RATE_MAX_WINDOW - 1;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800283
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200284 if (win->data & (1ULL << (RATE_MAX_WINDOW - 1))) {
285 win->data &= ~(1ULL << (RATE_MAX_WINDOW - 1));
Stanislaw Gruszka6ce1dc42011-08-26 15:49:28 +0200286 win->success_counter--;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800287 }
288 }
289
290 /* Increment frames-attempted counter */
Stanislaw Gruszka6ce1dc42011-08-26 15:49:28 +0200291 win->counter++;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800292
293 /* Shift bitmap by one frame (throw away oldest history),
294 * OR in "1", and increment "success" if this
295 * frame was successful. */
Stanislaw Gruszka6ce1dc42011-08-26 15:49:28 +0200296 win->data <<= 1;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800297 if (success > 0) {
Stanislaw Gruszka6ce1dc42011-08-26 15:49:28 +0200298 win->success_counter++;
299 win->data |= 0x1;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800300 success--;
301 }
302
303 retries--;
304 }
305
306 /* Calculate current success ratio, avoid divide-by-0! */
Stanislaw Gruszka6ce1dc42011-08-26 15:49:28 +0200307 if (win->counter > 0)
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100308 win->success_ratio =
309 128 * (100 * win->success_counter) / win->counter;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800310 else
Stanislaw Gruszka6ce1dc42011-08-26 15:49:28 +0200311 win->success_ratio = IL_INVALID_VALUE;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800312
Stanislaw Gruszka6ce1dc42011-08-26 15:49:28 +0200313 fail_count = win->counter - win->success_counter;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800314
315 /* Calculate average throughput, if we have enough history. */
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200316 if (fail_count >= RATE_MIN_FAILURE_TH ||
317 win->success_counter >= RATE_MIN_SUCCESS_TH)
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100318 win->average_tpt =
319 ((win->success_ratio * rs_sta->expected_tpt[idx] +
320 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);
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800328}
329
330/*
331 * Called after adding a new station to initialize rate scaling
332 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100333void
334il3945_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 Gruszkab16db502012-02-03 17:31:44 +0100344 if (sta_id == il->hw_params.bcast_id)
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800345 goto out;
346
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100347 psta = (struct il3945_sta_priv *)sta->drv_priv;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800348 rs_sta = &psta->rs_sta;
Karl Beldan675a0b02013-03-25 16:26:57 +0100349 sband = hw->wiphy->bands[conf->chandef.chan->band];
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800350
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 Gruszka5d0bef92011-12-23 08:13:48 +0100385 il->_3945.sta_supp_rates <<= IL_FIRST_OFDM_RATE;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800386 }
387
388out:
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200389 il->stations[sta_id].used &= ~IL_STA_UCODE_INPROGRESS;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800390
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100391 D_INFO("leave\n");
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800392}
393
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100394static void *
395il3945_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 Gruszkae7392362011-11-15 14:45:59 +0100401static void
402il3945_rs_free(void *il)
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800403{
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800404}
405
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100406static void *
407il3945_rs_alloc_sta(void *il_priv, struct ieee80211_sta *sta, gfp_t gfp)
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800408{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200409 struct il3945_rs_sta *rs_sta;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100410 struct il3945_sta_priv *psta = (void *)sta->drv_priv;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200411 struct il_priv *il __maybe_unused = il_priv;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800412
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100413 D_RATE("enter\n");
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800414
415 rs_sta = &psta->rs_sta;
416
417 spin_lock_init(&rs_sta->lock);
418 init_timer(&rs_sta->rate_scale_flush);
419
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100420 D_RATE("leave\n");
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800421
422 return rs_sta;
423}
424
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100425static void
426il3945_rs_free_sta(void *il_priv, struct ieee80211_sta *sta, void *il_sta)
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800427{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200428 struct il3945_rs_sta *rs_sta = il_sta;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800429
430 /*
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200431 * Be careful not to use any members of il3945_rs_sta (like trying
432 * to use il_priv to print out debugging) since it may not be fully
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800433 * initialized at this point.
434 */
435 del_timer_sync(&rs_sta->rate_scale_flush);
436}
437
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800438/**
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 Gruszkae7392362011-11-15 14:45:59 +0100444static void
445il3945_rs_tx_status(void *il_rate, struct ieee80211_supported_band *sband,
446 struct ieee80211_sta *sta, void *il_sta,
447 struct sk_buff *skb)
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800448{
449 s8 retries = 0, current_count;
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100450 int scale_rate_idx, first_idx, last_idx;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800451 unsigned long flags;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200452 struct il_priv *il = (struct il_priv *)il_rate;
453 struct il3945_rs_sta *rs_sta = il_sta;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800454 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
455
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100456 D_RATE("enter\n");
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800457
458 retries = info->status.rates[0].count;
459 /* Sanity Check for retries */
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200460 if (retries > RATE_RETRY_TH)
461 retries = RATE_RETRY_TH;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800462
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100463 first_idx = sband->bitrates[info->status.rates[0].idx].hw_value;
464 if (first_idx < 0 || first_idx >= RATE_COUNT_3945) {
465 D_RATE("leave: Rate out of bounds: %d\n", first_idx);
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800466 return;
467 }
468
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200469 if (!il_sta) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100470 D_RATE("leave: No STA il data to update!\n");
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800471 return;
472 }
473
474 /* Treat uninitialized rate scaling data same as non-existing. */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200475 if (!rs_sta->il) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100476 D_RATE("leave: STA il data uninitialized!\n");
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800477 return;
478 }
479
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800480 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 Gruszkae7392362011-11-15 14:45:59 +0100501 last_idx = il3945_rs_next_rate(il, scale_rate_idx);
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800502 }
503
504 /* Update this rate accounting for as many retries
505 * as was used for it (per current_count) */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100506 il3945_collect_tx_data(rs_sta, &rs_sta->win[scale_rate_idx], 0,
507 current_count, scale_rate_idx);
508 D_RATE("Update rate %d for %d retries.\n", scale_rate_idx,
509 current_count);
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800510
511 retries -= current_count;
512
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100513 scale_rate_idx = last_idx;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800514 }
515
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100516 /* Update the last idx win with success/failure based on ACK */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100517 D_RATE("Update rate %d with %s.\n", last_idx,
518 (info->flags & IEEE80211_TX_STAT_ACK) ? "success" : "failure");
519 il3945_collect_tx_data(rs_sta, &rs_sta->win[last_idx],
520 info->flags & IEEE80211_TX_STAT_ACK, 1,
521 last_idx);
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800522
Stanislaw Gruszka6ce1dc42011-08-26 15:49:28 +0200523 /* We updated the rate scale win -- if its been more than
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800524 * flush_time since the last run, schedule the flush
525 * again */
526 spin_lock_irqsave(&rs_sta->lock, flags);
527
528 if (!rs_sta->flush_pending &&
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100529 time_after(jiffies, rs_sta->last_flush + rs_sta->flush_time)) {
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800530
531 rs_sta->last_partial_flush = jiffies;
532 rs_sta->flush_pending = 1;
533 mod_timer(&rs_sta->rate_scale_flush,
534 jiffies + rs_sta->flush_time);
535 }
536
537 spin_unlock_irqrestore(&rs_sta->lock, flags);
538
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100539 D_RATE("leave\n");
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800540}
541
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100542static u16
543il3945_get_adjacent_rate(struct il3945_rs_sta *rs_sta, u8 idx, u16 rate_mask,
544 enum ieee80211_band band)
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800545{
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200546 u8 high = RATE_INVALID;
547 u8 low = RATE_INVALID;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200548 struct il_priv *il __maybe_unused = rs_sta->il;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800549
550 /* 802.11A walks to the next literal adjacent rate in
551 * the rate table */
552 if (unlikely(band == IEEE80211_BAND_5GHZ)) {
553 int i;
554 u32 mask;
555
556 /* Find the previous rate that is in the rate mask */
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100557 i = idx - 1;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800558 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
559 if (rate_mask & mask) {
560 low = i;
561 break;
562 }
563 }
564
565 /* Find the next rate that is in the rate mask */
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100566 i = idx + 1;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100567 for (mask = (1 << i); i < RATE_COUNT_3945; i++, mask <<= 1) {
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800568 if (rate_mask & mask) {
569 high = i;
570 break;
571 }
572 }
573
574 return (high << 8) | low;
575 }
576
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100577 low = idx;
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200578 while (low != RATE_INVALID) {
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800579 if (rs_sta->tgg)
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200580 low = il3945_rates[low].prev_rs_tgg;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800581 else
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200582 low = il3945_rates[low].prev_rs;
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200583 if (low == RATE_INVALID)
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800584 break;
585 if (rate_mask & (1 << low))
586 break;
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100587 D_RATE("Skipping masked lower rate: %d\n", low);
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800588 }
589
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100590 high = idx;
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200591 while (high != RATE_INVALID) {
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800592 if (rs_sta->tgg)
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200593 high = il3945_rates[high].next_rs_tgg;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800594 else
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200595 high = il3945_rates[high].next_rs;
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200596 if (high == RATE_INVALID)
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800597 break;
598 if (rate_mask & (1 << high))
599 break;
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100600 D_RATE("Skipping masked higher rate: %d\n", high);
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800601 }
602
603 return (high << 8) | low;
604}
605
606/**
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200607 * il3945_rs_get_rate - find the rate for the requested packet
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800608 *
609 * Returns the ieee80211_rate structure allocated by the driver.
610 *
611 * The rate control algorithm has no internal mapping between hw_mode's
612 * rate ordering and the rate ordering used by the rate control algorithm.
613 *
614 * The rate control algorithm uses a single table of rates that goes across
615 * the entire A/B/G spectrum vs. being limited to just one particular
616 * hw_mode.
617 *
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100618 * As such, we can't convert the idx obtained below into the hw_mode's
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800619 * rate table and must reference the driver allocated rate table
620 *
621 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100622static void
623il3945_rs_get_rate(void *il_r, struct ieee80211_sta *sta, void *il_sta,
624 struct ieee80211_tx_rate_control *txrc)
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800625{
626 struct ieee80211_supported_band *sband = txrc->sband;
627 struct sk_buff *skb = txrc->skb;
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200628 u8 low = RATE_INVALID;
629 u8 high = RATE_INVALID;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800630 u16 high_low;
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100631 int idx;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200632 struct il3945_rs_sta *rs_sta = il_sta;
Stanislaw Gruszka6ce1dc42011-08-26 15:49:28 +0200633 struct il3945_rate_scale_data *win = NULL;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200634 int current_tpt = IL_INVALID_VALUE;
635 int low_tpt = IL_INVALID_VALUE;
636 int high_tpt = IL_INVALID_VALUE;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800637 u32 fail_count;
638 s8 scale_action = 0;
639 unsigned long flags;
Dan Carpenterc3371d62011-02-26 04:56:53 +0300640 u16 rate_mask;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800641 s8 max_rate_idx = -1;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200642 struct il_priv *il __maybe_unused = (struct il_priv *)il_r;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800643 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
644
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100645 D_RATE("enter\n");
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800646
647 /* Treat uninitialized rate scaling data same as non-existing. */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200648 if (rs_sta && !rs_sta->il) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100649 D_RATE("Rate scaling information not initialized yet.\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200650 il_sta = NULL;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800651 }
652
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200653 if (rate_control_send_low(sta, il_sta, txrc))
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800654 return;
655
656 rate_mask = sta->supp_rates[sband->band];
657
658 /* get user max rate if set */
659 max_rate_idx = txrc->max_rate_idx;
Stanislaw Gruszka232913b2011-08-26 10:45:16 +0200660 if (sband->band == IEEE80211_BAND_5GHZ && max_rate_idx != -1)
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200661 max_rate_idx += IL_FIRST_OFDM_RATE;
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200662 if (max_rate_idx < 0 || max_rate_idx >= RATE_COUNT)
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800663 max_rate_idx = -1;
664
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100665 idx = min(rs_sta->last_txrate_idx & 0xffff, RATE_COUNT_3945 - 1);
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800666
667 if (sband->band == IEEE80211_BAND_5GHZ)
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200668 rate_mask = rate_mask << IL_FIRST_OFDM_RATE;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800669
670 spin_lock_irqsave(&rs_sta->lock, flags);
671
672 /* for recent assoc, choose best rate regarding
673 * to rssi value
674 */
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200675 if (rs_sta->start_rate != RATE_INVALID) {
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100676 if (rs_sta->start_rate < idx &&
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100677 (rate_mask & (1 << rs_sta->start_rate)))
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100678 idx = rs_sta->start_rate;
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200679 rs_sta->start_rate = RATE_INVALID;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800680 }
681
682 /* force user max rate if set by user */
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100683 if (max_rate_idx != -1 && max_rate_idx < idx) {
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800684 if (rate_mask & (1 << max_rate_idx))
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100685 idx = max_rate_idx;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800686 }
687
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100688 win = &(rs_sta->win[idx]);
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800689
Stanislaw Gruszka6ce1dc42011-08-26 15:49:28 +0200690 fail_count = win->counter - win->success_counter;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800691
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200692 if (fail_count < RATE_MIN_FAILURE_TH &&
693 win->success_counter < RATE_MIN_SUCCESS_TH) {
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800694 spin_unlock_irqrestore(&rs_sta->lock, flags);
695
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100696 D_RATE("Invalid average_tpt on rate %d: "
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100697 "counter: %d, success_counter: %d, "
698 "expected_tpt is %sNULL\n", idx, win->counter,
699 win->success_counter,
700 rs_sta->expected_tpt ? "not " : "");
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800701
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100702 /* Can't calculate this yet; not enough history */
Stanislaw Gruszka6ce1dc42011-08-26 15:49:28 +0200703 win->average_tpt = IL_INVALID_VALUE;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800704 goto out;
705
706 }
707
Stanislaw Gruszka6ce1dc42011-08-26 15:49:28 +0200708 current_tpt = win->average_tpt;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800709
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100710 high_low =
711 il3945_get_adjacent_rate(rs_sta, idx, rate_mask, sband->band);
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800712 low = high_low & 0xff;
713 high = (high_low >> 8) & 0xff;
714
715 /* If user set max rate, dont allow higher than user constrain */
Stanislaw Gruszka232913b2011-08-26 10:45:16 +0200716 if (max_rate_idx != -1 && max_rate_idx < high)
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200717 high = RATE_INVALID;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800718
719 /* Collect Measured throughputs of adjacent rates */
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200720 if (low != RATE_INVALID)
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800721 low_tpt = rs_sta->win[low].average_tpt;
722
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200723 if (high != RATE_INVALID)
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800724 high_tpt = rs_sta->win[high].average_tpt;
725
726 spin_unlock_irqrestore(&rs_sta->lock, flags);
727
728 scale_action = 0;
729
730 /* Low success ratio , need to drop the rate */
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200731 if (win->success_ratio < RATE_DECREASE_TH || !current_tpt) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100732 D_RATE("decrease rate because of low success_ratio\n");
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800733 scale_action = -1;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100734 /* No throughput measured yet for adjacent rates,
735 * try increase */
736 } else if (low_tpt == IL_INVALID_VALUE && high_tpt == IL_INVALID_VALUE) {
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800737
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100738 if (high != RATE_INVALID &&
739 win->success_ratio >= RATE_INCREASE_TH)
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800740 scale_action = 1;
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200741 else if (low != RATE_INVALID)
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800742 scale_action = 0;
743
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100744 /* Both adjacent throughputs are measured, but neither one has
745 * better throughput; we're using the best rate, don't change
746 * it! */
747 } else if (low_tpt != IL_INVALID_VALUE && high_tpt != IL_INVALID_VALUE
748 && low_tpt < current_tpt && high_tpt < current_tpt) {
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800749
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100750 D_RATE("No action -- low [%d] & high [%d] < "
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100751 "current_tpt [%d]\n", low_tpt, high_tpt, current_tpt);
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800752 scale_action = 0;
753
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100754 /* At least one of the rates has better throughput */
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800755 } else {
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200756 if (high_tpt != IL_INVALID_VALUE) {
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800757
758 /* High rate has better throughput, Increase
759 * rate */
760 if (high_tpt > current_tpt &&
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100761 win->success_ratio >= RATE_INCREASE_TH)
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800762 scale_action = 1;
763 else {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100764 D_RATE("decrease rate because of high tpt\n");
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800765 scale_action = 0;
766 }
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200767 } else if (low_tpt != IL_INVALID_VALUE) {
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800768 if (low_tpt > current_tpt) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100769 D_RATE("decrease rate because of low tpt\n");
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800770 scale_action = -1;
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200771 } else if (win->success_ratio >= RATE_INCREASE_TH) {
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800772 /* Lower rate has better
773 * throughput,decrease rate */
774 scale_action = 1;
775 }
776 }
777 }
778
779 /* Sanity check; asked for decrease, but success rate or throughput
780 * has been good at old rate. Don't change it. */
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200781 if (scale_action == -1 && low != RATE_INVALID &&
782 (win->success_ratio > RATE_HIGH_TH ||
Stanislaw Gruszka232913b2011-08-26 10:45:16 +0200783 current_tpt > 100 * rs_sta->expected_tpt[low]))
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800784 scale_action = 0;
785
786 switch (scale_action) {
787 case -1:
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800788 /* Decrese rate */
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200789 if (low != RATE_INVALID)
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100790 idx = low;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800791 break;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800792 case 1:
793 /* Increase rate */
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200794 if (high != RATE_INVALID)
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100795 idx = high;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800796
797 break;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800798 case 0:
799 default:
800 /* No change */
801 break;
802 }
803
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100804 D_RATE("Selected %d (action %d) - low %d high %d\n", idx, scale_action,
805 low, high);
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800806
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100807out:
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800808
Stanislaw Gruszka7c251012011-08-26 17:24:59 +0200809 if (sband->band == IEEE80211_BAND_5GHZ) {
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100810 if (WARN_ON_ONCE(idx < IL_FIRST_OFDM_RATE))
811 idx = IL_FIRST_OFDM_RATE;
812 rs_sta->last_txrate_idx = idx;
813 info->control.rates[0].idx = idx - IL_FIRST_OFDM_RATE;
Stanislaw Gruszka7c251012011-08-26 17:24:59 +0200814 } else {
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100815 rs_sta->last_txrate_idx = idx;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800816 info->control.rates[0].idx = rs_sta->last_txrate_idx;
Stanislaw Gruszka7c251012011-08-26 17:24:59 +0200817 }
Stanislaw Gruszkaa8cf0192013-06-04 14:19:10 +0200818 info->control.rates[0].count = 1;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800819
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100820 D_RATE("leave: %d\n", idx);
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800821}
822
823#ifdef CONFIG_MAC80211_DEBUGFS
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800824
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100825static ssize_t
Stanislaw Gruszka1722f8e2011-11-15 14:51:01 +0100826il3945_sta_dbgfs_stats_table_read(struct file *file, char __user *user_buf,
827 size_t count, loff_t *ppos)
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800828{
829 char *buff;
830 int desc = 0;
831 int j;
832 ssize_t ret;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200833 struct il3945_rs_sta *lq_sta = file->private_data;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800834
835 buff = kmalloc(1024, GFP_KERNEL);
836 if (!buff)
837 return -ENOMEM;
838
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100839 desc +=
840 sprintf(buff + desc,
841 "tx packets=%d last rate idx=%d\n"
842 "rate=0x%X flush time %d\n", lq_sta->tx_packets,
843 lq_sta->last_txrate_idx, lq_sta->start_rate,
844 jiffies_to_msecs(lq_sta->flush_time));
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200845 for (j = 0; j < RATE_COUNT_3945; j++) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100846 desc +=
847 sprintf(buff + desc, "counter=%d success=%d %%=%d\n",
848 lq_sta->win[j].counter,
849 lq_sta->win[j].success_counter,
850 lq_sta->win[j].success_ratio);
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800851 }
852 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
853 kfree(buff);
854 return ret;
855}
856
857static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200858 .read = il3945_sta_dbgfs_stats_table_read,
Stephen Boyd234e3402012-04-05 14:25:11 -0700859 .open = simple_open,
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800860 .llseek = default_llseek,
861};
862
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100863static void
864il3945_add_debugfs(void *il, void *il_sta, struct dentry *dir)
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800865{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200866 struct il3945_rs_sta *lq_sta = il_sta;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800867
868 lq_sta->rs_sta_dbgfs_stats_table_file =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100869 debugfs_create_file("rate_stats_table", 0600, dir, lq_sta,
870 &rs_sta_dbgfs_stats_table_ops);
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800871
872}
873
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100874static void
875il3945_remove_debugfs(void *il, void *il_sta)
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800876{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200877 struct il3945_rs_sta *lq_sta = il_sta;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800878 debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
879}
880#endif
881
882/*
883 * Initialization of rate scaling information is done by driver after
884 * the station is added. Since mac80211 calls this function before a
885 * station is added we ignore it.
886 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100887static void
888il3945_rs_rate_init_stub(void *il_r, struct ieee80211_supported_band *sband,
Simon Wunderlich3de805c2013-07-08 16:55:50 +0200889 struct cfg80211_chan_def *chandef,
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100890 struct ieee80211_sta *sta, void *il_sta)
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800891{
892}
893
894static struct rate_control_ops rs_ops = {
895 .module = NULL,
896 .name = RS_NAME,
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200897 .tx_status = il3945_rs_tx_status,
898 .get_rate = il3945_rs_get_rate,
899 .rate_init = il3945_rs_rate_init_stub,
900 .alloc = il3945_rs_alloc,
901 .free = il3945_rs_free,
902 .alloc_sta = il3945_rs_alloc_sta,
903 .free_sta = il3945_rs_free_sta,
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800904#ifdef CONFIG_MAC80211_DEBUGFS
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200905 .add_sta_debugfs = il3945_add_debugfs,
906 .remove_sta_debugfs = il3945_remove_debugfs,
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800907#endif
908
909};
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100910
911void
912il3945_rate_scale_init(struct ieee80211_hw *hw, s32 sta_id)
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800913{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200914 struct il_priv *il = hw->priv;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800915 s32 rssi = 0;
916 unsigned long flags;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200917 struct il3945_rs_sta *rs_sta;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800918 struct ieee80211_sta *sta;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200919 struct il3945_sta_priv *psta;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800920
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100921 D_RATE("enter\n");
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800922
923 rcu_read_lock();
924
Stanislaw Gruszka83007192012-02-03 17:31:57 +0100925 sta = ieee80211_find_sta(il->vif, il->stations[sta_id].sta.sta.addr);
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800926 if (!sta) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100927 D_RATE("Unable to find station to initialize rate scaling.\n");
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800928 rcu_read_unlock();
929 return;
930 }
931
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100932 psta = (void *)sta->drv_priv;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800933 rs_sta = &psta->rs_sta;
934
935 spin_lock_irqsave(&rs_sta->lock, flags);
936
937 rs_sta->tgg = 0;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200938 switch (il->band) {
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800939 case IEEE80211_BAND_2GHZ:
940 /* TODO: this always does G, not a regression */
Stanislaw Gruszkac8b03952012-02-03 17:31:37 +0100941 if (il->active.flags & RXON_FLG_TGG_PROTECT_MSK) {
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800942 rs_sta->tgg = 1;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200943 rs_sta->expected_tpt = il3945_expected_tpt_g_prot;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800944 } else
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200945 rs_sta->expected_tpt = il3945_expected_tpt_g;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800946 break;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800947 case IEEE80211_BAND_5GHZ:
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200948 rs_sta->expected_tpt = il3945_expected_tpt_a;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800949 break;
Vladimir Kondratiev3a0c52a2012-07-02 09:32:32 +0300950 default:
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800951 BUG();
952 break;
953 }
954
955 spin_unlock_irqrestore(&rs_sta->lock, flags);
956
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200957 rssi = il->_3945.last_rx_rssi;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800958 if (rssi == 0)
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200959 rssi = IL_MIN_RSSI_VAL;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800960
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100961 D_RATE("Network RSSI: %d\n", rssi);
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800962
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100963 rs_sta->start_rate = il3945_get_rate_idx_by_rssi(rssi, il->band);
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800964
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100965 D_RATE("leave: rssi %d assign rate idx: " "%d (plcp 0x%x)\n", rssi,
966 rs_sta->start_rate, il3945_rates[rs_sta->start_rate].plcp);
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800967 rcu_read_unlock();
968}
969
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100970int
971il3945_rate_control_register(void)
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800972{
973 return ieee80211_rate_control_register(&rs_ops);
974}
975
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100976void
977il3945_rate_control_unregister(void)
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800978{
979 ieee80211_rate_control_unregister(&rs_ops);
980}