blob: fe31590a51b2d81b41b40b2f862c96c5b2e10980 [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 Gruszka5d0bef92011-12-23 08:13:48 +010089#define RATE_MAX_WINDOW 62
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +020090#define RATE_FLUSH (3*HZ)
Stanislaw Gruszka5d0bef92011-12-23 08:13:48 +010091#define RATE_WIN_FLUSH (HZ/2)
92#define IL39_RATE_HIGH_TH 11520
93#define IL_SUCCESS_UP_TH 8960
94#define IL_SUCCESS_DOWN_TH 10880
95#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 Gruszkae7392362011-11-15 14:45:59 +0100100static u8
101il3945_get_rate_idx_by_rssi(s32 rssi, enum ieee80211_band band)
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800102{
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100103 u32 idx = 0;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800104 u32 table_size = 0;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200105 struct il3945_tpt_entry *tpt_table = NULL;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800106
Stanislaw Gruszka232913b2011-08-26 10:45:16 +0200107 if (rssi < IL_MIN_RSSI_VAL || rssi > IL_MAX_RSSI_VAL)
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200108 rssi = IL_MIN_RSSI_VAL;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800109
110 switch (band) {
111 case IEEE80211_BAND_2GHZ:
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200112 tpt_table = il3945_tpt_table_g;
113 table_size = ARRAY_SIZE(il3945_tpt_table_g);
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800114 break;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800115 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;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800119 default:
120 BUG();
121 break;
122 }
123
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100124 while (idx < table_size && rssi < tpt_table[idx].min_rssi)
125 idx++;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800126
Stanislaw Gruszka5d0bef92011-12-23 08:13:48 +0100127 idx = min(idx, table_size - 1);
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800128
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100129 return tpt_table[idx].idx;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800130}
131
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100132static void
133il3945_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 Gruszkae7392362011-11-15 14:45:59 +0100150static int
151il3945_rate_scale_flush_wins(struct il3945_rs_sta *rs_sta)
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800152{
153 int unflushed = 0;
154 int i;
155 unsigned long flags;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200156 struct il_priv *il __maybe_unused = rs_sta->il;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800157
158 /*
159 * For each rate, if we have collected data on that rate
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200160 * and it has been more than RATE_WIN_FLUSH
Stanislaw Gruszkaebf0d902011-08-26 15:43:47 +0200161 * since we flushed, clear out the gathered stats
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800162 */
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200163 for (i = 0; i < RATE_COUNT_3945; i++) {
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800164 if (!rs_sta->win[i].counter)
165 continue;
166
167 spin_lock_irqsave(&rs_sta->lock, flags);
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100168 if (time_after(jiffies, rs_sta->win[i].stamp + RATE_WIN_FLUSH)) {
169 D_RATE("flushing %d samples of rate " "idx %d\n",
170 rs_sta->win[i].counter, i);
Stanislaw Gruszka6ce1dc42011-08-26 15:49:28 +0200171 il3945_clear_win(&rs_sta->win[i]);
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800172 } else
173 unflushed++;
174 spin_unlock_irqrestore(&rs_sta->lock, flags);
175 }
176
177 return unflushed;
178}
179
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200180#define RATE_FLUSH_MAX 5000 /* msec */
181#define RATE_FLUSH_MIN 50 /* msec */
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200182#define IL_AVERAGE_PACKETS 1500
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800183
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100184static void
185il3945_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 Gruszkae7392362011-11-15 14:45:59 +0100208 D_RATE("Tx'd %d packets in %dms\n", packet_count, duration);
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800209
210 /* Determine packets per second */
211 if (duration)
212 pps = (packet_count * 1000) / duration;
213 else
214 pps = 0;
215
216 if (pps) {
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200217 duration = (IL_AVERAGE_PACKETS * 1000) / pps;
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200218 if (duration < RATE_FLUSH_MIN)
219 duration = RATE_FLUSH_MIN;
220 else if (duration > RATE_FLUSH_MAX)
221 duration = RATE_FLUSH_MAX;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800222 } else
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200223 duration = RATE_FLUSH_MAX;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800224
225 rs_sta->flush_time = msecs_to_jiffies(duration);
226
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100227 D_RATE("new flush period: %d msec ave %d\n", duration,
228 packet_count);
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800229
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100230 mod_timer(&rs_sta->rate_scale_flush,
231 jiffies + rs_sta->flush_time);
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800232
233 rs_sta->last_partial_flush = jiffies;
234 } else {
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200235 rs_sta->flush_time = RATE_FLUSH;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800236 rs_sta->flush_pending = 0;
237 }
238 /* If there weren't any unflushed entries, we don't schedule the timer
239 * to run again */
240
241 rs_sta->last_flush = jiffies;
242
243 spin_unlock_irqrestore(&rs_sta->lock, flags);
244
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100245 D_RATE("leave\n");
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800246}
247
248/**
Stanislaw Gruszka6ce1dc42011-08-26 15:49:28 +0200249 * il3945_collect_tx_data - Update the success/failure sliding win
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800250 *
Stanislaw Gruszka6ce1dc42011-08-26 15:49:28 +0200251 * We keep a sliding win of the last 64 packets transmitted
252 * at this rate. win->data contains the bitmask of successful
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800253 * packets.
254 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100255static void
256il3945_collect_tx_data(struct il3945_rs_sta *rs_sta,
257 struct il3945_rate_scale_data *win, int success,
258 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)
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100309 win->success_ratio =
310 128 * (100 * win->success_counter) / 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 Gruszkae7392362011-11-15 14:45:59 +0100319 win->average_tpt =
320 ((win->success_ratio * rs_sta->expected_tpt[idx] +
321 64) / 128);
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800322 else
Stanislaw Gruszka6ce1dc42011-08-26 15:49:28 +0200323 win->average_tpt = IL_INVALID_VALUE;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800324
Stanislaw Gruszka6ce1dc42011-08-26 15:49:28 +0200325 /* Tag this win as having been updated */
326 win->stamp = jiffies;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800327
328 spin_unlock_irqrestore(&rs_sta->lock, flags);
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800329}
330
331/*
332 * Called after adding a new station to initialize rate scaling
333 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100334void
335il3945_rs_rate_init(struct il_priv *il, struct ieee80211_sta *sta, u8 sta_id)
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800336{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200337 struct ieee80211_hw *hw = il->hw;
338 struct ieee80211_conf *conf = &il->hw->conf;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200339 struct il3945_sta_priv *psta;
340 struct il3945_rs_sta *rs_sta;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800341 struct ieee80211_supported_band *sband;
342 int i;
343
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100344 D_INFO("enter\n");
Stanislaw Gruszkab16db502012-02-03 17:31:44 +0100345 if (sta_id == il->hw_params.bcast_id)
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800346 goto out;
347
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100348 psta = (struct il3945_sta_priv *)sta->drv_priv;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800349 rs_sta = &psta->rs_sta;
Karl Beldan675a0b02013-03-25 16:26:57 +0100350 sband = hw->wiphy->bands[conf->chandef.chan->band];
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800351
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200352 rs_sta->il = il;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800353
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200354 rs_sta->start_rate = RATE_INVALID;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800355
356 /* default to just 802.11b */
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200357 rs_sta->expected_tpt = il3945_expected_tpt_b;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800358
359 rs_sta->last_partial_flush = jiffies;
360 rs_sta->last_flush = jiffies;
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200361 rs_sta->flush_time = RATE_FLUSH;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800362 rs_sta->last_tx_packets = 0;
363
364 rs_sta->rate_scale_flush.data = (unsigned long)rs_sta;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200365 rs_sta->rate_scale_flush.function = il3945_bg_rate_scale_flush;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800366
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200367 for (i = 0; i < RATE_COUNT_3945; i++)
Stanislaw Gruszka6ce1dc42011-08-26 15:49:28 +0200368 il3945_clear_win(&rs_sta->win[i]);
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800369
370 /* TODO: what is a good starting rate for STA? About middle? Maybe not
371 * the lowest or the highest rate.. Could consider using RSSI from
372 * previous packets? Need to have IEEE 802.1X auth succeed immediately
373 * after assoc.. */
374
375 for (i = sband->n_bitrates - 1; i >= 0; i--) {
376 if (sta->supp_rates[sband->band] & (1 << i)) {
377 rs_sta->last_txrate_idx = i;
378 break;
379 }
380 }
381
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200382 il->_3945.sta_supp_rates = sta->supp_rates[sband->band];
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200383 /* For 5 GHz band it start at IL_FIRST_OFDM_RATE */
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800384 if (sband->band == IEEE80211_BAND_5GHZ) {
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200385 rs_sta->last_txrate_idx += IL_FIRST_OFDM_RATE;
Stanislaw Gruszka5d0bef92011-12-23 08:13:48 +0100386 il->_3945.sta_supp_rates <<= 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 Gruszkae7392362011-11-15 14:45:59 +0100395static void *
396il3945_rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800397{
398 return hw->priv;
399}
400
401/* rate scale requires free function to be implemented */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100402static void
403il3945_rs_free(void *il)
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800404{
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800405}
406
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100407static void *
408il3945_rs_alloc_sta(void *il_priv, struct ieee80211_sta *sta, gfp_t gfp)
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800409{
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200410 struct il3945_rs_sta *rs_sta;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100411 struct il3945_sta_priv *psta = (void *)sta->drv_priv;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200412 struct il_priv *il __maybe_unused = il_priv;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800413
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100414 D_RATE("enter\n");
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800415
416 rs_sta = &psta->rs_sta;
417
418 spin_lock_init(&rs_sta->lock);
419 init_timer(&rs_sta->rate_scale_flush);
420
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100421 D_RATE("leave\n");
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800422
423 return rs_sta;
424}
425
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100426static void
427il3945_rs_free_sta(void *il_priv, struct ieee80211_sta *sta, void *il_sta)
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800428{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200429 struct il3945_rs_sta *rs_sta = il_sta;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800430
431 /*
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200432 * Be careful not to use any members of il3945_rs_sta (like trying
433 * to use il_priv to print out debugging) since it may not be fully
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800434 * initialized at this point.
435 */
436 del_timer_sync(&rs_sta->rate_scale_flush);
437}
438
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800439/**
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200440 * il3945_rs_tx_status - Update rate control values based on Tx results
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800441 *
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200442 * NOTE: Uses il_priv->retry_rate for the # of retries attempted by
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800443 * the hardware for each rate.
444 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100445static void
446il3945_rs_tx_status(void *il_rate, struct ieee80211_supported_band *sband,
447 struct ieee80211_sta *sta, void *il_sta,
448 struct sk_buff *skb)
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800449{
450 s8 retries = 0, current_count;
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100451 int scale_rate_idx, first_idx, last_idx;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800452 unsigned long flags;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200453 struct il_priv *il = (struct il_priv *)il_rate;
454 struct il3945_rs_sta *rs_sta = il_sta;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800455 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
456
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100457 D_RATE("enter\n");
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800458
459 retries = info->status.rates[0].count;
460 /* Sanity Check for retries */
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200461 if (retries > RATE_RETRY_TH)
462 retries = RATE_RETRY_TH;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800463
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100464 first_idx = sband->bitrates[info->status.rates[0].idx].hw_value;
465 if (first_idx < 0 || first_idx >= RATE_COUNT_3945) {
466 D_RATE("leave: Rate out of bounds: %d\n", first_idx);
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800467 return;
468 }
469
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200470 if (!il_sta) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100471 D_RATE("leave: No STA il data to update!\n");
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800472 return;
473 }
474
475 /* Treat uninitialized rate scaling data same as non-existing. */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200476 if (!rs_sta->il) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100477 D_RATE("leave: STA il data uninitialized!\n");
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800478 return;
479 }
480
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800481 rs_sta->tx_packets++;
482
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100483 scale_rate_idx = first_idx;
484 last_idx = first_idx;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800485
486 /*
Stanislaw Gruszka6ce1dc42011-08-26 15:49:28 +0200487 * Update the win for each rate. We determine which rates
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800488 * were Tx'd based on the total number of retries vs. the number
489 * of retries configured for each rate -- currently set to the
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200490 * il value 'retry_rate' vs. rate specific
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800491 *
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100492 * On exit from this while loop last_idx indicates the rate
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800493 * at which the frame was finally transmitted (or failed if no
494 * ACK)
495 */
496 while (retries > 1) {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200497 if ((retries - 1) < il->retry_rate) {
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800498 current_count = (retries - 1);
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100499 last_idx = scale_rate_idx;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800500 } else {
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200501 current_count = il->retry_rate;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100502 last_idx = il3945_rs_next_rate(il, 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 Gruszkae7392362011-11-15 14:45:59 +0100507 il3945_collect_tx_data(rs_sta, &rs_sta->win[scale_rate_idx], 0,
508 current_count, scale_rate_idx);
509 D_RATE("Update rate %d for %d retries.\n", scale_rate_idx,
510 current_count);
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800511
512 retries -= current_count;
513
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100514 scale_rate_idx = last_idx;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800515 }
516
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100517 /* Update the last idx win with success/failure based on ACK */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100518 D_RATE("Update rate %d with %s.\n", last_idx,
519 (info->flags & IEEE80211_TX_STAT_ACK) ? "success" : "failure");
520 il3945_collect_tx_data(rs_sta, &rs_sta->win[last_idx],
521 info->flags & IEEE80211_TX_STAT_ACK, 1,
522 last_idx);
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800523
Stanislaw Gruszka6ce1dc42011-08-26 15:49:28 +0200524 /* We updated the rate scale win -- if its been more than
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800525 * flush_time since the last run, schedule the flush
526 * again */
527 spin_lock_irqsave(&rs_sta->lock, flags);
528
529 if (!rs_sta->flush_pending &&
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100530 time_after(jiffies, rs_sta->last_flush + rs_sta->flush_time)) {
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800531
532 rs_sta->last_partial_flush = jiffies;
533 rs_sta->flush_pending = 1;
534 mod_timer(&rs_sta->rate_scale_flush,
535 jiffies + rs_sta->flush_time);
536 }
537
538 spin_unlock_irqrestore(&rs_sta->lock, flags);
539
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100540 D_RATE("leave\n");
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800541}
542
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100543static u16
544il3945_get_adjacent_rate(struct il3945_rs_sta *rs_sta, u8 idx, u16 rate_mask,
545 enum ieee80211_band band)
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800546{
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200547 u8 high = RATE_INVALID;
548 u8 low = RATE_INVALID;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200549 struct il_priv *il __maybe_unused = rs_sta->il;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800550
551 /* 802.11A walks to the next literal adjacent rate in
552 * the rate table */
553 if (unlikely(band == IEEE80211_BAND_5GHZ)) {
554 int i;
555 u32 mask;
556
557 /* Find the previous rate that is in the rate mask */
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100558 i = idx - 1;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800559 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
560 if (rate_mask & mask) {
561 low = i;
562 break;
563 }
564 }
565
566 /* Find the next rate that is in the rate mask */
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100567 i = idx + 1;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100568 for (mask = (1 << i); i < RATE_COUNT_3945; i++, mask <<= 1) {
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800569 if (rate_mask & mask) {
570 high = i;
571 break;
572 }
573 }
574
575 return (high << 8) | low;
576 }
577
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100578 low = idx;
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200579 while (low != RATE_INVALID) {
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800580 if (rs_sta->tgg)
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200581 low = il3945_rates[low].prev_rs_tgg;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800582 else
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200583 low = il3945_rates[low].prev_rs;
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200584 if (low == RATE_INVALID)
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800585 break;
586 if (rate_mask & (1 << low))
587 break;
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100588 D_RATE("Skipping masked lower rate: %d\n", low);
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800589 }
590
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100591 high = idx;
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200592 while (high != RATE_INVALID) {
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800593 if (rs_sta->tgg)
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200594 high = il3945_rates[high].next_rs_tgg;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800595 else
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200596 high = il3945_rates[high].next_rs;
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200597 if (high == RATE_INVALID)
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800598 break;
599 if (rate_mask & (1 << high))
600 break;
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100601 D_RATE("Skipping masked higher rate: %d\n", high);
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800602 }
603
604 return (high << 8) | low;
605}
606
607/**
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200608 * il3945_rs_get_rate - find the rate for the requested packet
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800609 *
610 * Returns the ieee80211_rate structure allocated by the driver.
611 *
612 * The rate control algorithm has no internal mapping between hw_mode's
613 * rate ordering and the rate ordering used by the rate control algorithm.
614 *
615 * The rate control algorithm uses a single table of rates that goes across
616 * the entire A/B/G spectrum vs. being limited to just one particular
617 * hw_mode.
618 *
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100619 * As such, we can't convert the idx obtained below into the hw_mode's
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800620 * rate table and must reference the driver allocated rate table
621 *
622 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100623static void
624il3945_rs_get_rate(void *il_r, struct ieee80211_sta *sta, void *il_sta,
625 struct ieee80211_tx_rate_control *txrc)
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800626{
627 struct ieee80211_supported_band *sband = txrc->sband;
628 struct sk_buff *skb = txrc->skb;
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200629 u8 low = RATE_INVALID;
630 u8 high = RATE_INVALID;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800631 u16 high_low;
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100632 int idx;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200633 struct il3945_rs_sta *rs_sta = il_sta;
Stanislaw Gruszka6ce1dc42011-08-26 15:49:28 +0200634 struct il3945_rate_scale_data *win = NULL;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200635 int current_tpt = IL_INVALID_VALUE;
636 int low_tpt = IL_INVALID_VALUE;
637 int high_tpt = IL_INVALID_VALUE;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800638 u32 fail_count;
639 s8 scale_action = 0;
640 unsigned long flags;
Dan Carpenterc3371d62011-02-26 04:56:53 +0300641 u16 rate_mask;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800642 s8 max_rate_idx = -1;
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200643 struct il_priv *il __maybe_unused = (struct il_priv *)il_r;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800644 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
645
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100646 D_RATE("enter\n");
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800647
648 /* Treat uninitialized rate scaling data same as non-existing. */
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200649 if (rs_sta && !rs_sta->il) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100650 D_RATE("Rate scaling information not initialized yet.\n");
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200651 il_sta = NULL;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800652 }
653
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200654 if (rate_control_send_low(sta, il_sta, txrc))
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800655 return;
656
657 rate_mask = sta->supp_rates[sband->band];
658
659 /* get user max rate if set */
660 max_rate_idx = txrc->max_rate_idx;
Stanislaw Gruszka232913b2011-08-26 10:45:16 +0200661 if (sband->band == IEEE80211_BAND_5GHZ && max_rate_idx != -1)
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200662 max_rate_idx += IL_FIRST_OFDM_RATE;
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200663 if (max_rate_idx < 0 || max_rate_idx >= RATE_COUNT)
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800664 max_rate_idx = -1;
665
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100666 idx = min(rs_sta->last_txrate_idx & 0xffff, RATE_COUNT_3945 - 1);
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800667
668 if (sband->band == IEEE80211_BAND_5GHZ)
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200669 rate_mask = rate_mask << IL_FIRST_OFDM_RATE;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800670
671 spin_lock_irqsave(&rs_sta->lock, flags);
672
673 /* for recent assoc, choose best rate regarding
674 * to rssi value
675 */
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200676 if (rs_sta->start_rate != RATE_INVALID) {
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100677 if (rs_sta->start_rate < idx &&
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100678 (rate_mask & (1 << rs_sta->start_rate)))
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100679 idx = rs_sta->start_rate;
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200680 rs_sta->start_rate = RATE_INVALID;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800681 }
682
683 /* force user max rate if set by user */
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100684 if (max_rate_idx != -1 && max_rate_idx < idx) {
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800685 if (rate_mask & (1 << max_rate_idx))
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100686 idx = max_rate_idx;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800687 }
688
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100689 win = &(rs_sta->win[idx]);
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800690
Stanislaw Gruszka6ce1dc42011-08-26 15:49:28 +0200691 fail_count = win->counter - win->success_counter;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800692
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200693 if (fail_count < RATE_MIN_FAILURE_TH &&
694 win->success_counter < RATE_MIN_SUCCESS_TH) {
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800695 spin_unlock_irqrestore(&rs_sta->lock, flags);
696
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100697 D_RATE("Invalid average_tpt on rate %d: "
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100698 "counter: %d, success_counter: %d, "
699 "expected_tpt is %sNULL\n", idx, win->counter,
700 win->success_counter,
701 rs_sta->expected_tpt ? "not " : "");
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800702
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100703 /* Can't calculate this yet; not enough history */
Stanislaw Gruszka6ce1dc42011-08-26 15:49:28 +0200704 win->average_tpt = IL_INVALID_VALUE;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800705 goto out;
706
707 }
708
Stanislaw Gruszka6ce1dc42011-08-26 15:49:28 +0200709 current_tpt = win->average_tpt;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800710
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100711 high_low =
712 il3945_get_adjacent_rate(rs_sta, idx, rate_mask, sband->band);
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800713 low = high_low & 0xff;
714 high = (high_low >> 8) & 0xff;
715
716 /* If user set max rate, dont allow higher than user constrain */
Stanislaw Gruszka232913b2011-08-26 10:45:16 +0200717 if (max_rate_idx != -1 && max_rate_idx < high)
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200718 high = RATE_INVALID;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800719
720 /* Collect Measured throughputs of adjacent rates */
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200721 if (low != RATE_INVALID)
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800722 low_tpt = rs_sta->win[low].average_tpt;
723
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200724 if (high != RATE_INVALID)
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800725 high_tpt = rs_sta->win[high].average_tpt;
726
727 spin_unlock_irqrestore(&rs_sta->lock, flags);
728
729 scale_action = 0;
730
731 /* Low success ratio , need to drop the rate */
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200732 if (win->success_ratio < RATE_DECREASE_TH || !current_tpt) {
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100733 D_RATE("decrease rate because of low success_ratio\n");
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800734 scale_action = -1;
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100735 /* No throughput measured yet for adjacent rates,
736 * try increase */
737 } else if (low_tpt == IL_INVALID_VALUE && high_tpt == IL_INVALID_VALUE) {
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800738
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100739 if (high != RATE_INVALID &&
740 win->success_ratio >= RATE_INCREASE_TH)
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800741 scale_action = 1;
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200742 else if (low != RATE_INVALID)
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800743 scale_action = 0;
744
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100745 /* Both adjacent throughputs are measured, but neither one has
746 * better throughput; we're using the best rate, don't change
747 * it! */
748 } else if (low_tpt != IL_INVALID_VALUE && high_tpt != IL_INVALID_VALUE
749 && low_tpt < current_tpt && high_tpt < current_tpt) {
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800750
Stanislaw Gruszka58de00a2011-11-15 11:21:01 +0100751 D_RATE("No action -- low [%d] & high [%d] < "
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100752 "current_tpt [%d]\n", low_tpt, high_tpt, current_tpt);
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800753 scale_action = 0;
754
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100755 /* At least one of the rates has better throughput */
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800756 } else {
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200757 if (high_tpt != IL_INVALID_VALUE) {
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800758
759 /* High rate has better throughput, Increase
760 * rate */
761 if (high_tpt > current_tpt &&
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100762 win->success_ratio >= RATE_INCREASE_TH)
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800763 scale_action = 1;
764 else {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100765 D_RATE("decrease rate because of high tpt\n");
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800766 scale_action = 0;
767 }
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200768 } else if (low_tpt != IL_INVALID_VALUE) {
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800769 if (low_tpt > current_tpt) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100770 D_RATE("decrease rate because of low tpt\n");
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800771 scale_action = -1;
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200772 } else if (win->success_ratio >= RATE_INCREASE_TH) {
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800773 /* Lower rate has better
774 * throughput,decrease rate */
775 scale_action = 1;
776 }
777 }
778 }
779
780 /* Sanity check; asked for decrease, but success rate or throughput
781 * has been good at old rate. Don't change it. */
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200782 if (scale_action == -1 && low != RATE_INVALID &&
783 (win->success_ratio > RATE_HIGH_TH ||
Stanislaw Gruszka232913b2011-08-26 10:45:16 +0200784 current_tpt > 100 * rs_sta->expected_tpt[low]))
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800785 scale_action = 0;
786
787 switch (scale_action) {
788 case -1:
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800789 /* Decrese rate */
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200790 if (low != RATE_INVALID)
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100791 idx = low;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800792 break;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800793 case 1:
794 /* Increase rate */
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200795 if (high != RATE_INVALID)
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100796 idx = high;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800797
798 break;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800799 case 0:
800 default:
801 /* No change */
802 break;
803 }
804
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100805 D_RATE("Selected %d (action %d) - low %d high %d\n", idx, scale_action,
806 low, high);
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800807
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100808out:
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800809
Stanislaw Gruszka7c251012011-08-26 17:24:59 +0200810 if (sband->band == IEEE80211_BAND_5GHZ) {
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100811 if (WARN_ON_ONCE(idx < IL_FIRST_OFDM_RATE))
812 idx = IL_FIRST_OFDM_RATE;
813 rs_sta->last_txrate_idx = idx;
814 info->control.rates[0].idx = idx - IL_FIRST_OFDM_RATE;
Stanislaw Gruszka7c251012011-08-26 17:24:59 +0200815 } else {
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100816 rs_sta->last_txrate_idx = idx;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800817 info->control.rates[0].idx = rs_sta->last_txrate_idx;
Stanislaw Gruszka7c251012011-08-26 17:24:59 +0200818 }
Stanislaw Gruszkaa8cf0192013-06-04 14:19:10 +0200819 info->control.rates[0].count = 1;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800820
Stanislaw Gruszka0c2c8852011-11-15 12:30:17 +0100821 D_RATE("leave: %d\n", idx);
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800822}
823
824#ifdef CONFIG_MAC80211_DEBUGFS
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800825
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100826static ssize_t
Stanislaw Gruszka1722f8e2011-11-15 14:51:01 +0100827il3945_sta_dbgfs_stats_table_read(struct file *file, char __user *user_buf,
828 size_t count, loff_t *ppos)
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800829{
830 char *buff;
831 int desc = 0;
832 int j;
833 ssize_t ret;
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200834 struct il3945_rs_sta *lq_sta = file->private_data;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800835
836 buff = kmalloc(1024, GFP_KERNEL);
837 if (!buff)
838 return -ENOMEM;
839
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100840 desc +=
841 sprintf(buff + desc,
842 "tx packets=%d last rate idx=%d\n"
843 "rate=0x%X flush time %d\n", lq_sta->tx_packets,
844 lq_sta->last_txrate_idx, lq_sta->start_rate,
845 jiffies_to_msecs(lq_sta->flush_time));
Stanislaw Gruszka2eb05812011-08-26 16:07:43 +0200846 for (j = 0; j < RATE_COUNT_3945; j++) {
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100847 desc +=
848 sprintf(buff + desc, "counter=%d success=%d %%=%d\n",
849 lq_sta->win[j].counter,
850 lq_sta->win[j].success_counter,
851 lq_sta->win[j].success_ratio);
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800852 }
853 ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
854 kfree(buff);
855 return ret;
856}
857
858static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
Stanislaw Gruszkae2ebc832011-10-24 15:41:30 +0200859 .read = il3945_sta_dbgfs_stats_table_read,
Stephen Boyd234e3402012-04-05 14:25:11 -0700860 .open = simple_open,
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800861 .llseek = default_llseek,
862};
863
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100864static void
865il3945_add_debugfs(void *il, void *il_sta, struct dentry *dir)
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800866{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200867 struct il3945_rs_sta *lq_sta = il_sta;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800868
869 lq_sta->rs_sta_dbgfs_stats_table_file =
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100870 debugfs_create_file("rate_stats_table", 0600, dir, lq_sta,
871 &rs_sta_dbgfs_stats_table_ops);
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800872
873}
874
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100875static void
876il3945_remove_debugfs(void *il, void *il_sta)
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800877{
Stanislaw Gruszka46bc8d42011-10-24 16:49:25 +0200878 struct il3945_rs_sta *lq_sta = il_sta;
Wey-Yi Guy4bc85c12011-02-21 11:11:05 -0800879 debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
880}
881#endif
882
883/*
884 * Initialization of rate scaling information is done by driver after
885 * the station is added. Since mac80211 calls this function before a
886 * station is added we ignore it.
887 */
Stanislaw Gruszkae7392362011-11-15 14:45:59 +0100888static void
889il3945_rs_rate_init_stub(void *il_r, struct ieee80211_supported_band *sband,
890 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}