blob: 044abf734eb610ea60c084574095403dab45d080 [file] [log] [blame]
Zhu Yib481de92007-09-25 17:54:57 -07001/******************************************************************************
2 *
Reinette Chatre01f81622009-01-08 10:20:02 -08003 * Copyright(c) 2005 - 2009 Intel Corporation. All rights reserved.
Zhu Yib481de92007-09-25 17:54:57 -07004 *
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:
Winkler, Tomas759ef892008-12-09 11:28:58 -080022 * Intel Linux Wireless <ilw@linux.intel.com>
Zhu Yib481de92007-09-25 17:54:57 -070023 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24 *
25 *****************************************************************************/
26
27#include <linux/kernel.h>
28#include <linux/init.h>
29#include <linux/skbuff.h>
30#include <linux/wireless.h>
31#include <net/mac80211.h>
Zhu Yib481de92007-09-25 17:54:57 -070032
33#include <linux/netdevice.h>
34#include <linux/etherdevice.h>
35#include <linux/delay.h>
36
37#include <linux/workqueue.h>
38
Tomas Winkler600c0e12008-12-19 10:37:04 +080039#include "iwl-commands.h"
Christoph Hellwig5d08cd12007-10-25 17:15:50 +080040#include "iwl-3945.h"
Zhu Yib481de92007-09-25 17:54:57 -070041
42#define RS_NAME "iwl-3945-rs"
43
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080044struct iwl3945_rate_scale_data {
Zhu Yib481de92007-09-25 17:54:57 -070045 u64 data;
46 s32 success_counter;
47 s32 success_ratio;
48 s32 counter;
49 s32 average_tpt;
50 unsigned long stamp;
51};
52
Tomas Winklerfa254a62008-01-27 20:41:38 -080053struct iwl3945_rs_sta {
Zhu Yib481de92007-09-25 17:54:57 -070054 spinlock_t lock;
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +080055 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -070056 s32 *expected_tpt;
57 unsigned long last_partial_flush;
58 unsigned long last_flush;
59 u32 flush_time;
60 u32 last_tx_packets;
61 u32 tx_packets;
62 u8 tgg;
63 u8 flush_pending;
64 u8 start_rate;
65 u8 ibss_sta_added;
66 struct timer_list rate_scale_flush;
Samuel Ortizd9829a62008-12-19 10:37:12 +080067 struct iwl3945_rate_scale_data win[IWL_RATE_COUNT_3945];
Abbas, Mohamedc30e30e2008-12-02 12:14:02 -080068#ifdef CONFIG_MAC80211_DEBUGFS
69 struct dentry *rs_sta_dbgfs_stats_table_file;
70#endif
Johannes Bergb7e35002008-09-11 02:22:58 +020071
72 /* used to be in sta_info */
73 int last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -070074};
75
Samuel Ortizd9829a62008-12-19 10:37:12 +080076static s32 iwl3945_expected_tpt_g[IWL_RATE_COUNT_3945] = {
Mohamed Abbas14577f22007-11-12 11:37:42 +080077 7, 13, 35, 58, 0, 0, 76, 104, 130, 168, 191, 202
Zhu Yib481de92007-09-25 17:54:57 -070078};
79
Samuel Ortizd9829a62008-12-19 10:37:12 +080080static s32 iwl3945_expected_tpt_g_prot[IWL_RATE_COUNT_3945] = {
Mohamed Abbas14577f22007-11-12 11:37:42 +080081 7, 13, 35, 58, 0, 0, 0, 80, 93, 113, 123, 125
Zhu Yib481de92007-09-25 17:54:57 -070082};
83
Samuel Ortizd9829a62008-12-19 10:37:12 +080084static s32 iwl3945_expected_tpt_a[IWL_RATE_COUNT_3945] = {
Mohamed Abbas14577f22007-11-12 11:37:42 +080085 0, 0, 0, 0, 40, 57, 72, 98, 121, 154, 177, 186
Zhu Yib481de92007-09-25 17:54:57 -070086};
87
Samuel Ortizd9829a62008-12-19 10:37:12 +080088static s32 iwl3945_expected_tpt_b[IWL_RATE_COUNT_3945] = {
Mohamed Abbas14577f22007-11-12 11:37:42 +080089 7, 13, 35, 58, 0, 0, 0, 0, 0, 0, 0, 0
Zhu Yib481de92007-09-25 17:54:57 -070090};
91
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080092struct iwl3945_tpt_entry {
Zhu Yib481de92007-09-25 17:54:57 -070093 s8 min_rssi;
94 u8 index;
95};
96
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080097static struct iwl3945_tpt_entry iwl3945_tpt_table_a[] = {
Zhu Yib481de92007-09-25 17:54:57 -070098 {-60, IWL_RATE_54M_INDEX},
99 {-64, IWL_RATE_48M_INDEX},
100 {-72, IWL_RATE_36M_INDEX},
101 {-80, IWL_RATE_24M_INDEX},
102 {-84, IWL_RATE_18M_INDEX},
103 {-85, IWL_RATE_12M_INDEX},
104 {-87, IWL_RATE_9M_INDEX},
105 {-89, IWL_RATE_6M_INDEX}
106};
107
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800108static struct iwl3945_tpt_entry iwl3945_tpt_table_g[] = {
Zhu Yib481de92007-09-25 17:54:57 -0700109 {-60, IWL_RATE_54M_INDEX},
110 {-64, IWL_RATE_48M_INDEX},
111 {-68, IWL_RATE_36M_INDEX},
112 {-80, IWL_RATE_24M_INDEX},
113 {-84, IWL_RATE_18M_INDEX},
114 {-85, IWL_RATE_12M_INDEX},
115 {-86, IWL_RATE_11M_INDEX},
116 {-88, IWL_RATE_5M_INDEX},
117 {-90, IWL_RATE_2M_INDEX},
118 {-92, IWL_RATE_1M_INDEX}
119};
120
121#define IWL_RATE_MAX_WINDOW 62
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800122#define IWL_RATE_FLUSH (3*HZ)
Zhu Yib481de92007-09-25 17:54:57 -0700123#define IWL_RATE_WIN_FLUSH (HZ/2)
Samuel Ortizd9829a62008-12-19 10:37:12 +0800124#define IWL39_RATE_HIGH_TH 11520
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800125#define IWL_SUCCESS_UP_TH 8960
126#define IWL_SUCCESS_DOWN_TH 10880
Zhu Yib481de92007-09-25 17:54:57 -0700127#define IWL_RATE_MIN_FAILURE_TH 8
128#define IWL_RATE_MIN_SUCCESS_TH 8
129#define IWL_RATE_DECREASE_TH 1920
130
Johannes Berg8318d782008-01-24 19:38:38 +0100131static u8 iwl3945_get_rate_index_by_rssi(s32 rssi, enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -0700132{
133 u32 index = 0;
134 u32 table_size = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800135 struct iwl3945_tpt_entry *tpt_table = NULL;
Zhu Yib481de92007-09-25 17:54:57 -0700136
137 if ((rssi < IWL_MIN_RSSI_VAL) || (rssi > IWL_MAX_RSSI_VAL))
138 rssi = IWL_MIN_RSSI_VAL;
139
Johannes Berg8318d782008-01-24 19:38:38 +0100140 switch (band) {
141 case IEEE80211_BAND_2GHZ:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800142 tpt_table = iwl3945_tpt_table_g;
143 table_size = ARRAY_SIZE(iwl3945_tpt_table_g);
Zhu Yib481de92007-09-25 17:54:57 -0700144 break;
145
Johannes Berg8318d782008-01-24 19:38:38 +0100146 case IEEE80211_BAND_5GHZ:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800147 tpt_table = iwl3945_tpt_table_a;
148 table_size = ARRAY_SIZE(iwl3945_tpt_table_a);
Zhu Yib481de92007-09-25 17:54:57 -0700149 break;
150
151 default:
Johannes Berg8318d782008-01-24 19:38:38 +0100152 BUG();
Zhu Yib481de92007-09-25 17:54:57 -0700153 break;
154 }
155
156 while ((index < table_size) && (rssi < tpt_table[index].min_rssi))
157 index++;
158
159 index = min(index, (table_size - 1));
160
161 return tpt_table[index].index;
162}
163
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800164static void iwl3945_clear_window(struct iwl3945_rate_scale_data *window)
Zhu Yib481de92007-09-25 17:54:57 -0700165{
166 window->data = 0;
167 window->success_counter = 0;
Tomas Winkler28447f32008-03-06 17:36:54 -0800168 window->success_ratio = -1;
Zhu Yib481de92007-09-25 17:54:57 -0700169 window->counter = 0;
Samuel Ortizd9829a62008-12-19 10:37:12 +0800170 window->average_tpt = IWL_INVALID_VALUE;
Zhu Yib481de92007-09-25 17:54:57 -0700171 window->stamp = 0;
172}
173
174/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800175 * iwl3945_rate_scale_flush_windows - flush out the rate scale windows
Zhu Yib481de92007-09-25 17:54:57 -0700176 *
177 * Returns the number of windows that have gathered data but were
178 * not flushed. If there were any that were not flushed, then
179 * reschedule the rate flushing routine.
180 */
Tomas Winklerfa254a62008-01-27 20:41:38 -0800181static int iwl3945_rate_scale_flush_windows(struct iwl3945_rs_sta *rs_sta)
Zhu Yib481de92007-09-25 17:54:57 -0700182{
183 int unflushed = 0;
184 int i;
185 unsigned long flags;
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800186 struct iwl_priv *priv = rs_sta->priv;
Zhu Yib481de92007-09-25 17:54:57 -0700187
188 /*
189 * For each rate, if we have collected data on that rate
190 * and it has been more than IWL_RATE_WIN_FLUSH
191 * since we flushed, clear out the gathered statistics
192 */
Samuel Ortizd9829a62008-12-19 10:37:12 +0800193 for (i = 0; i < IWL_RATE_COUNT_3945; i++) {
Tomas Winklerfa254a62008-01-27 20:41:38 -0800194 if (!rs_sta->win[i].counter)
Zhu Yib481de92007-09-25 17:54:57 -0700195 continue;
196
Tomas Winklerfa254a62008-01-27 20:41:38 -0800197 spin_lock_irqsave(&rs_sta->lock, flags);
198 if (time_after(jiffies, rs_sta->win[i].stamp +
Zhu Yib481de92007-09-25 17:54:57 -0700199 IWL_RATE_WIN_FLUSH)) {
200 IWL_DEBUG_RATE("flushing %d samples of rate "
201 "index %d\n",
Tomas Winklerfa254a62008-01-27 20:41:38 -0800202 rs_sta->win[i].counter, i);
203 iwl3945_clear_window(&rs_sta->win[i]);
Zhu Yib481de92007-09-25 17:54:57 -0700204 } else
205 unflushed++;
Tomas Winklerfa254a62008-01-27 20:41:38 -0800206 spin_unlock_irqrestore(&rs_sta->lock, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700207 }
208
209 return unflushed;
210}
211
212#define IWL_RATE_FLUSH_MAX 5000 /* msec */
213#define IWL_RATE_FLUSH_MIN 50 /* msec */
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800214#define IWL_AVERAGE_PACKETS 1500
Zhu Yib481de92007-09-25 17:54:57 -0700215
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800216static void iwl3945_bg_rate_scale_flush(unsigned long data)
Zhu Yib481de92007-09-25 17:54:57 -0700217{
Tomas Winklerfa254a62008-01-27 20:41:38 -0800218 struct iwl3945_rs_sta *rs_sta = (void *)data;
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800219 struct iwl_priv *priv = rs_sta->priv;
Zhu Yib481de92007-09-25 17:54:57 -0700220 int unflushed = 0;
221 unsigned long flags;
222 u32 packet_count, duration, pps;
223
224 IWL_DEBUG_RATE("enter\n");
225
Tomas Winklerfa254a62008-01-27 20:41:38 -0800226 unflushed = iwl3945_rate_scale_flush_windows(rs_sta);
Zhu Yib481de92007-09-25 17:54:57 -0700227
Tomas Winklerfa254a62008-01-27 20:41:38 -0800228 spin_lock_irqsave(&rs_sta->lock, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700229
Zhu Yib481de92007-09-25 17:54:57 -0700230 /* Number of packets Rx'd since last time this timer ran */
Tomas Winklerfa254a62008-01-27 20:41:38 -0800231 packet_count = (rs_sta->tx_packets - rs_sta->last_tx_packets) + 1;
Zhu Yib481de92007-09-25 17:54:57 -0700232
Tomas Winklerfa254a62008-01-27 20:41:38 -0800233 rs_sta->last_tx_packets = rs_sta->tx_packets + 1;
Zhu Yib481de92007-09-25 17:54:57 -0700234
235 if (unflushed) {
236 duration =
Tomas Winklerfa254a62008-01-27 20:41:38 -0800237 jiffies_to_msecs(jiffies - rs_sta->last_partial_flush);
Zhu Yib481de92007-09-25 17:54:57 -0700238
239 IWL_DEBUG_RATE("Tx'd %d packets in %dms\n",
240 packet_count, duration);
241
242 /* Determine packets per second */
243 if (duration)
244 pps = (packet_count * 1000) / duration;
245 else
246 pps = 0;
247
248 if (pps) {
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800249 duration = (IWL_AVERAGE_PACKETS * 1000) / pps;
Zhu Yib481de92007-09-25 17:54:57 -0700250 if (duration < IWL_RATE_FLUSH_MIN)
251 duration = IWL_RATE_FLUSH_MIN;
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800252 else if (duration > IWL_RATE_FLUSH_MAX)
253 duration = IWL_RATE_FLUSH_MAX;
Zhu Yib481de92007-09-25 17:54:57 -0700254 } else
255 duration = IWL_RATE_FLUSH_MAX;
256
Tomas Winklerfa254a62008-01-27 20:41:38 -0800257 rs_sta->flush_time = msecs_to_jiffies(duration);
Zhu Yib481de92007-09-25 17:54:57 -0700258
259 IWL_DEBUG_RATE("new flush period: %d msec ave %d\n",
260 duration, packet_count);
261
Tomas Winklerfa254a62008-01-27 20:41:38 -0800262 mod_timer(&rs_sta->rate_scale_flush, jiffies +
263 rs_sta->flush_time);
Zhu Yib481de92007-09-25 17:54:57 -0700264
Tomas Winklerfa254a62008-01-27 20:41:38 -0800265 rs_sta->last_partial_flush = jiffies;
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800266 } else {
267 rs_sta->flush_time = IWL_RATE_FLUSH;
268 rs_sta->flush_pending = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700269 }
Zhu Yib481de92007-09-25 17:54:57 -0700270 /* If there weren't any unflushed entries, we don't schedule the timer
271 * to run again */
272
Tomas Winklerfa254a62008-01-27 20:41:38 -0800273 rs_sta->last_flush = jiffies;
Zhu Yib481de92007-09-25 17:54:57 -0700274
Tomas Winklerfa254a62008-01-27 20:41:38 -0800275 spin_unlock_irqrestore(&rs_sta->lock, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700276
277 IWL_DEBUG_RATE("leave\n");
278}
279
280/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800281 * iwl3945_collect_tx_data - Update the success/failure sliding window
Zhu Yib481de92007-09-25 17:54:57 -0700282 *
283 * We keep a sliding window of the last 64 packets transmitted
284 * at this rate. window->data contains the bitmask of successful
285 * packets.
286 */
Tomas Winklerfa254a62008-01-27 20:41:38 -0800287static void iwl3945_collect_tx_data(struct iwl3945_rs_sta *rs_sta,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800288 struct iwl3945_rate_scale_data *window,
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800289 int success, int retries, int index)
Zhu Yib481de92007-09-25 17:54:57 -0700290{
291 unsigned long flags;
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800292 s32 fail_count;
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800293 struct iwl_priv *priv = rs_sta->priv;
Zhu Yib481de92007-09-25 17:54:57 -0700294
295 if (!retries) {
296 IWL_DEBUG_RATE("leave: retries == 0 -- should be at least 1\n");
297 return;
298 }
299
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800300 spin_lock_irqsave(&rs_sta->lock, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700301 while (retries--) {
Zhu Yib481de92007-09-25 17:54:57 -0700302
303 /* If we have filled up the window then subtract one from the
304 * success counter if the high-bit is counting toward
305 * success */
306 if (window->counter == IWL_RATE_MAX_WINDOW) {
307 if (window->data & (1ULL << (IWL_RATE_MAX_WINDOW - 1)))
308 window->success_counter--;
309 } else
310 window->counter++;
311
312 /* Slide the window to the left one bit */
313 window->data = (window->data << 1);
314
315 /* If this packet was a success then set the low bit high */
316 if (success) {
317 window->success_counter++;
318 window->data |= 1;
319 }
320
321 /* window->counter can't be 0 -- it is either >0 or
322 * IWL_RATE_MAX_WINDOW */
323 window->success_ratio = 12800 * window->success_counter /
324 window->counter;
325
326 /* Tag this window as having been updated */
327 window->stamp = jiffies;
328
Zhu Yib481de92007-09-25 17:54:57 -0700329 }
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800330
331 fail_count = window->counter - window->success_counter;
332 if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
333 (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
334 window->average_tpt = ((window->success_ratio *
335 rs_sta->expected_tpt[index] + 64) / 128);
336 else
Samuel Ortizd9829a62008-12-19 10:37:12 +0800337 window->average_tpt = IWL_INVALID_VALUE;
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800338
339 spin_unlock_irqrestore(&rs_sta->lock, flags);
340
Zhu Yib481de92007-09-25 17:54:57 -0700341}
342
Abbas, Mohamed72627962008-12-05 07:58:37 -0800343static void rs_rate_init(void *priv_r, struct ieee80211_supported_band *sband,
Johannes Berg4b7679a2008-09-18 18:14:18 +0200344 struct ieee80211_sta *sta, void *priv_sta)
Zhu Yib481de92007-09-25 17:54:57 -0700345{
Johannes Berg4b7679a2008-09-18 18:14:18 +0200346 struct iwl3945_rs_sta *rs_sta = priv_sta;
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800347 struct iwl_priv *priv = (struct iwl_priv *)priv_r;
Zhu Yib481de92007-09-25 17:54:57 -0700348 int i;
349
350 IWL_DEBUG_RATE("enter\n");
351
352 /* TODO: what is a good starting rate for STA? About middle? Maybe not
353 * the lowest or the highest rate.. Could consider using RSSI from
354 * previous packets? Need to have IEEE 802.1X auth succeed immediately
355 * after assoc.. */
356
Abbas, Mohamed72627962008-12-05 07:58:37 -0800357 for (i = sband->n_bitrates - 1; i >= 0; i--) {
Johannes Berg4b7679a2008-09-18 18:14:18 +0200358 if (sta->supp_rates[sband->band] & (1 << i)) {
Johannes Bergae17e982008-09-11 03:04:36 +0200359 rs_sta->last_txrate_idx = i;
Zhu Yib481de92007-09-25 17:54:57 -0700360 break;
361 }
362 }
363
Abbas, Mohamed72627962008-12-05 07:58:37 -0800364 priv->sta_supp_rates = sta->supp_rates[sband->band];
Johannes Berg8318d782008-01-24 19:38:38 +0100365 /* For 5 GHz band it start at IWL_FIRST_OFDM_RATE */
Abbas, Mohamed72627962008-12-05 07:58:37 -0800366 if (sband->band == IEEE80211_BAND_5GHZ) {
Johannes Bergb7e35002008-09-11 02:22:58 +0200367 rs_sta->last_txrate_idx += IWL_FIRST_OFDM_RATE;
Abbas, Mohamed72627962008-12-05 07:58:37 -0800368 priv->sta_supp_rates = priv->sta_supp_rates <<
369 IWL_FIRST_OFDM_RATE;
370 }
371
Mohamed Abbas14577f22007-11-12 11:37:42 +0800372
Zhu Yib481de92007-09-25 17:54:57 -0700373 IWL_DEBUG_RATE("leave\n");
374}
375
Johannes Berg4b7679a2008-09-18 18:14:18 +0200376static void *rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
Zhu Yib481de92007-09-25 17:54:57 -0700377{
Johannes Berg4b7679a2008-09-18 18:14:18 +0200378 return hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -0700379}
380
Ian Schram01ebd062007-10-25 17:15:22 +0800381/* rate scale requires free function to be implemented */
Zhu Yib481de92007-09-25 17:54:57 -0700382static void rs_free(void *priv)
383{
384 return;
385}
Johannes Berg4b7679a2008-09-18 18:14:18 +0200386
Samuel Ortiz40b8ec02008-12-19 10:37:08 +0800387static void *rs_alloc_sta(void *iwl_priv, struct ieee80211_sta *sta, gfp_t gfp)
Zhu Yib481de92007-09-25 17:54:57 -0700388{
Tomas Winklerfa254a62008-01-27 20:41:38 -0800389 struct iwl3945_rs_sta *rs_sta;
Johannes Berg4b7679a2008-09-18 18:14:18 +0200390 struct iwl3945_sta_priv *psta = (void *) sta->drv_priv;
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800391 struct iwl_priv *priv = iwl_priv;
Zhu Yib481de92007-09-25 17:54:57 -0700392 int i;
393
Johannes Berg4b7679a2008-09-18 18:14:18 +0200394 /*
395 * XXX: If it's using sta->drv_priv anyway, it might
396 * as well just put all the information there.
397 */
398
Zhu Yib481de92007-09-25 17:54:57 -0700399 IWL_DEBUG_RATE("enter\n");
400
Tomas Winklerfa254a62008-01-27 20:41:38 -0800401 rs_sta = kzalloc(sizeof(struct iwl3945_rs_sta), gfp);
402 if (!rs_sta) {
Zhu Yib481de92007-09-25 17:54:57 -0700403 IWL_DEBUG_RATE("leave: ENOMEM\n");
404 return NULL;
405 }
406
Johannes Berg4b7679a2008-09-18 18:14:18 +0200407 psta->rs_sta = rs_sta;
408
Tomas Winklerfa254a62008-01-27 20:41:38 -0800409 spin_lock_init(&rs_sta->lock);
Zhu Yib481de92007-09-25 17:54:57 -0700410
Samuel Ortiz40b8ec02008-12-19 10:37:08 +0800411 rs_sta->priv = priv;
412
Tomas Winklerfa254a62008-01-27 20:41:38 -0800413 rs_sta->start_rate = IWL_RATE_INVALID;
Zhu Yib481de92007-09-25 17:54:57 -0700414
415 /* default to just 802.11b */
Tomas Winklerfa254a62008-01-27 20:41:38 -0800416 rs_sta->expected_tpt = iwl3945_expected_tpt_b;
Zhu Yib481de92007-09-25 17:54:57 -0700417
Tomas Winklerfa254a62008-01-27 20:41:38 -0800418 rs_sta->last_partial_flush = jiffies;
419 rs_sta->last_flush = jiffies;
420 rs_sta->flush_time = IWL_RATE_FLUSH;
421 rs_sta->last_tx_packets = 0;
422 rs_sta->ibss_sta_added = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700423
Tomas Winklerfa254a62008-01-27 20:41:38 -0800424 init_timer(&rs_sta->rate_scale_flush);
425 rs_sta->rate_scale_flush.data = (unsigned long)rs_sta;
426 rs_sta->rate_scale_flush.function = &iwl3945_bg_rate_scale_flush;
Zhu Yib481de92007-09-25 17:54:57 -0700427
Samuel Ortizd9829a62008-12-19 10:37:12 +0800428 for (i = 0; i < IWL_RATE_COUNT_3945; i++)
Tomas Winklerfa254a62008-01-27 20:41:38 -0800429 iwl3945_clear_window(&rs_sta->win[i]);
Zhu Yib481de92007-09-25 17:54:57 -0700430
431 IWL_DEBUG_RATE("leave\n");
432
Tomas Winklerfa254a62008-01-27 20:41:38 -0800433 return rs_sta;
Zhu Yib481de92007-09-25 17:54:57 -0700434}
435
Samuel Ortiz40b8ec02008-12-19 10:37:08 +0800436static void rs_free_sta(void *iwl_priv, struct ieee80211_sta *sta,
Johannes Berg4b7679a2008-09-18 18:14:18 +0200437 void *priv_sta)
Zhu Yib481de92007-09-25 17:54:57 -0700438{
Johannes Berg4b7679a2008-09-18 18:14:18 +0200439 struct iwl3945_sta_priv *psta = (void *) sta->drv_priv;
Tomas Winklerfa254a62008-01-27 20:41:38 -0800440 struct iwl3945_rs_sta *rs_sta = priv_sta;
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800441 struct iwl_priv *priv = rs_sta->priv;
Zhu Yib481de92007-09-25 17:54:57 -0700442
Johannes Berg4b7679a2008-09-18 18:14:18 +0200443 psta->rs_sta = NULL;
444
Zhu Yib481de92007-09-25 17:54:57 -0700445 IWL_DEBUG_RATE("enter\n");
Tomas Winklerfa254a62008-01-27 20:41:38 -0800446 del_timer_sync(&rs_sta->rate_scale_flush);
447 kfree(rs_sta);
Zhu Yib481de92007-09-25 17:54:57 -0700448 IWL_DEBUG_RATE("leave\n");
449}
450
Mohamed Abbas14577f22007-11-12 11:37:42 +0800451
Zhu Yib481de92007-09-25 17:54:57 -0700452/**
453 * rs_tx_status - Update rate control values based on Tx results
454 *
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800455 * NOTE: Uses iwl_priv->retry_rate for the # of retries attempted by
Zhu Yib481de92007-09-25 17:54:57 -0700456 * the hardware for each rate.
457 */
Johannes Berg4b7679a2008-09-18 18:14:18 +0200458static void rs_tx_status(void *priv_rate, struct ieee80211_supported_band *sband,
459 struct ieee80211_sta *sta, void *priv_sta,
Johannes Berge039fa42008-05-15 12:55:29 +0200460 struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -0700461{
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800462 s8 retries = 0, current_count;
Zhu Yib481de92007-09-25 17:54:57 -0700463 int scale_rate_index, first_index, last_index;
464 unsigned long flags;
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800465 struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
Johannes Berg4b7679a2008-09-18 18:14:18 +0200466 struct iwl3945_rs_sta *rs_sta = priv_sta;
Johannes Berge039fa42008-05-15 12:55:29 +0200467 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Johannes Berg8318d782008-01-24 19:38:38 +0100468
Zhu Yib481de92007-09-25 17:54:57 -0700469 IWL_DEBUG_RATE("enter\n");
470
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800471 retries = info->status.rates[0].count;
Johannes Berge6a98542008-10-21 12:40:02 +0200472
473 first_index = sband->bitrates[info->status.rates[0].idx].hw_value;
Samuel Ortizd9829a62008-12-19 10:37:12 +0800474 if ((first_index < 0) || (first_index >= IWL_RATE_COUNT_3945)) {
Johannes Berg8318d782008-01-24 19:38:38 +0100475 IWL_DEBUG_RATE("leave: Rate out of bounds: %d\n", first_index);
Zhu Yib481de92007-09-25 17:54:57 -0700476 return;
477 }
478
Johannes Berg4b7679a2008-09-18 18:14:18 +0200479 if (!priv_sta) {
Zhu Yib481de92007-09-25 17:54:57 -0700480 IWL_DEBUG_RATE("leave: No STA priv data to update!\n");
481 return;
482 }
483
Tomas Winklerfa254a62008-01-27 20:41:38 -0800484 rs_sta->tx_packets++;
Zhu Yib481de92007-09-25 17:54:57 -0700485
486 scale_rate_index = first_index;
487 last_index = first_index;
488
489 /*
490 * Update the window for each rate. We determine which rates
491 * were Tx'd based on the total number of retries vs. the number
492 * of retries configured for each rate -- currently set to the
493 * priv value 'retry_rate' vs. rate specific
494 *
495 * On exit from this while loop last_index indicates the rate
496 * at which the frame was finally transmitted (or failed if no
497 * ACK)
498 */
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800499 while (retries > 1) {
500 if ((retries - 1) < priv->retry_rate) {
501 current_count = (retries - 1);
Zhu Yib481de92007-09-25 17:54:57 -0700502 last_index = scale_rate_index;
503 } else {
504 current_count = priv->retry_rate;
Johannes Berge6a98542008-10-21 12:40:02 +0200505 last_index = iwl3945_rs_next_rate(priv,
Mohamed Abbas14577f22007-11-12 11:37:42 +0800506 scale_rate_index);
Zhu Yib481de92007-09-25 17:54:57 -0700507 }
508
509 /* Update this rate accounting for as many retries
510 * as was used for it (per current_count) */
Tomas Winklerfa254a62008-01-27 20:41:38 -0800511 iwl3945_collect_tx_data(rs_sta,
512 &rs_sta->win[scale_rate_index],
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800513 0, current_count, scale_rate_index);
Zhu Yib481de92007-09-25 17:54:57 -0700514 IWL_DEBUG_RATE("Update rate %d for %d retries.\n",
515 scale_rate_index, current_count);
516
517 retries -= current_count;
518
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800519 scale_rate_index = last_index;
Zhu Yib481de92007-09-25 17:54:57 -0700520 }
521
Mohamed Abbas14577f22007-11-12 11:37:42 +0800522
Zhu Yib481de92007-09-25 17:54:57 -0700523 /* Update the last index window with success/failure based on ACK */
524 IWL_DEBUG_RATE("Update rate %d with %s.\n",
525 last_index,
Johannes Berge039fa42008-05-15 12:55:29 +0200526 (info->flags & IEEE80211_TX_STAT_ACK) ?
Zhu Yib481de92007-09-25 17:54:57 -0700527 "success" : "failure");
Tomas Winklerfa254a62008-01-27 20:41:38 -0800528 iwl3945_collect_tx_data(rs_sta,
529 &rs_sta->win[last_index],
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800530 info->flags & IEEE80211_TX_STAT_ACK, 1, last_index);
Zhu Yib481de92007-09-25 17:54:57 -0700531
532 /* We updated the rate scale window -- if its been more than
533 * flush_time since the last run, schedule the flush
534 * again */
Tomas Winklerfa254a62008-01-27 20:41:38 -0800535 spin_lock_irqsave(&rs_sta->lock, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700536
Tomas Winklerfa254a62008-01-27 20:41:38 -0800537 if (!rs_sta->flush_pending &&
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800538 time_after(jiffies, rs_sta->last_flush +
Tomas Winklerfa254a62008-01-27 20:41:38 -0800539 rs_sta->flush_time)) {
Zhu Yib481de92007-09-25 17:54:57 -0700540
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800541 rs_sta->last_partial_flush = jiffies;
Tomas Winklerfa254a62008-01-27 20:41:38 -0800542 rs_sta->flush_pending = 1;
543 mod_timer(&rs_sta->rate_scale_flush,
544 jiffies + rs_sta->flush_time);
Zhu Yib481de92007-09-25 17:54:57 -0700545 }
546
Tomas Winklerfa254a62008-01-27 20:41:38 -0800547 spin_unlock_irqrestore(&rs_sta->lock, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700548
Zhu Yib481de92007-09-25 17:54:57 -0700549 IWL_DEBUG_RATE("leave\n");
550
551 return;
552}
553
Tomas Winklerfa254a62008-01-27 20:41:38 -0800554static u16 iwl3945_get_adjacent_rate(struct iwl3945_rs_sta *rs_sta,
Johannes Berg8318d782008-01-24 19:38:38 +0100555 u8 index, u16 rate_mask, enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -0700556{
557 u8 high = IWL_RATE_INVALID;
558 u8 low = IWL_RATE_INVALID;
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800559 struct iwl_priv *priv = rs_sta->priv;
Zhu Yib481de92007-09-25 17:54:57 -0700560
Ian Schram01ebd062007-10-25 17:15:22 +0800561 /* 802.11A walks to the next literal adjacent rate in
Zhu Yib481de92007-09-25 17:54:57 -0700562 * the rate table */
Johannes Berg8318d782008-01-24 19:38:38 +0100563 if (unlikely(band == IEEE80211_BAND_5GHZ)) {
Zhu Yib481de92007-09-25 17:54:57 -0700564 int i;
565 u32 mask;
566
567 /* Find the previous rate that is in the rate mask */
568 i = index - 1;
569 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
570 if (rate_mask & mask) {
571 low = i;
572 break;
573 }
574 }
575
576 /* Find the next rate that is in the rate mask */
577 i = index + 1;
Samuel Ortizd9829a62008-12-19 10:37:12 +0800578 for (mask = (1 << i); i < IWL_RATE_COUNT_3945;
579 i++, mask <<= 1) {
Zhu Yib481de92007-09-25 17:54:57 -0700580 if (rate_mask & mask) {
581 high = i;
582 break;
583 }
584 }
585
586 return (high << 8) | low;
587 }
588
589 low = index;
590 while (low != IWL_RATE_INVALID) {
Tomas Winklerfa254a62008-01-27 20:41:38 -0800591 if (rs_sta->tgg)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800592 low = iwl3945_rates[low].prev_rs_tgg;
Zhu Yib481de92007-09-25 17:54:57 -0700593 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800594 low = iwl3945_rates[low].prev_rs;
Zhu Yib481de92007-09-25 17:54:57 -0700595 if (low == IWL_RATE_INVALID)
596 break;
597 if (rate_mask & (1 << low))
598 break;
599 IWL_DEBUG_RATE("Skipping masked lower rate: %d\n", low);
600 }
601
602 high = index;
603 while (high != IWL_RATE_INVALID) {
Tomas Winklerfa254a62008-01-27 20:41:38 -0800604 if (rs_sta->tgg)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800605 high = iwl3945_rates[high].next_rs_tgg;
Zhu Yib481de92007-09-25 17:54:57 -0700606 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800607 high = iwl3945_rates[high].next_rs;
Zhu Yib481de92007-09-25 17:54:57 -0700608 if (high == IWL_RATE_INVALID)
609 break;
610 if (rate_mask & (1 << high))
611 break;
612 IWL_DEBUG_RATE("Skipping masked higher rate: %d\n", high);
613 }
614
615 return (high << 8) | low;
616}
617
618/**
619 * rs_get_rate - find the rate for the requested packet
620 *
621 * Returns the ieee80211_rate structure allocated by the driver.
622 *
623 * The rate control algorithm has no internal mapping between hw_mode's
624 * rate ordering and the rate ordering used by the rate control algorithm.
625 *
626 * The rate control algorithm uses a single table of rates that goes across
627 * the entire A/B/G spectrum vs. being limited to just one particular
628 * hw_mode.
629 *
630 * As such, we can't convert the index obtained below into the hw_mode's
631 * rate table and must reference the driver allocated rate table
632 *
633 */
Johannes Berge6a98542008-10-21 12:40:02 +0200634static void rs_get_rate(void *priv_r, struct ieee80211_sta *sta,
635 void *priv_sta, struct ieee80211_tx_rate_control *txrc)
Zhu Yib481de92007-09-25 17:54:57 -0700636{
Johannes Berge6a98542008-10-21 12:40:02 +0200637 struct ieee80211_supported_band *sband = txrc->sband;
638 struct sk_buff *skb = txrc->skb;
Zhu Yib481de92007-09-25 17:54:57 -0700639 u8 low = IWL_RATE_INVALID;
640 u8 high = IWL_RATE_INVALID;
641 u16 high_low;
642 int index;
Johannes Berg4b7679a2008-09-18 18:14:18 +0200643 struct iwl3945_rs_sta *rs_sta = priv_sta;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800644 struct iwl3945_rate_scale_data *window = NULL;
Samuel Ortizd9829a62008-12-19 10:37:12 +0800645 int current_tpt = IWL_INVALID_VALUE;
646 int low_tpt = IWL_INVALID_VALUE;
647 int high_tpt = IWL_INVALID_VALUE;
Zhu Yib481de92007-09-25 17:54:57 -0700648 u32 fail_count;
649 s8 scale_action = 0;
650 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -0700651 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Abbas, Mohamedc338ba32009-01-21 10:58:02 -0800652 u16 fc;
653 u16 rate_mask = 0;
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -0800654 s8 max_rate_idx = -1;
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800655 struct iwl_priv *priv = (struct iwl_priv *)priv_r;
Johannes Berge6a98542008-10-21 12:40:02 +0200656 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Zhu Yib481de92007-09-25 17:54:57 -0700657
658 IWL_DEBUG_RATE("enter\n");
659
Abbas, Mohamedc338ba32009-01-21 10:58:02 -0800660 if (sta)
661 rate_mask = sta->supp_rates[sband->band];
662
Michael Wu2bc454b2007-12-25 19:33:16 -0500663 /* Send management frames and broadcast/multicast data using lowest
664 * rate. */
665 fc = le16_to_cpu(hdr->frame_control);
666 if ((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA ||
667 is_multicast_ether_addr(hdr->addr1) ||
Johannes Berg4b7679a2008-09-18 18:14:18 +0200668 !sta || !priv_sta) {
Zhu Yib481de92007-09-25 17:54:57 -0700669 IWL_DEBUG_RATE("leave: No STA priv data to update!\n");
Abbas, Mohamedc338ba32009-01-21 10:58:02 -0800670 if (!rate_mask)
671 info->control.rates[0].idx =
672 rate_lowest_index(sband, NULL);
673 else
674 info->control.rates[0].idx =
675 rate_lowest_index(sband, sta);
Mattias Nissler1abbe492007-12-20 13:50:07 +0100676 return;
Zhu Yib481de92007-09-25 17:54:57 -0700677 }
678
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -0800679 /* get user max rate if set */
680 max_rate_idx = txrc->max_rate_idx;
681 if ((sband->band == IEEE80211_BAND_5GHZ) && (max_rate_idx != -1))
682 max_rate_idx += IWL_FIRST_OFDM_RATE;
683 if ((max_rate_idx < 0) || (max_rate_idx >= IWL_RATE_COUNT))
684 max_rate_idx = -1;
685
Samuel Ortizd9829a62008-12-19 10:37:12 +0800686 index = min(rs_sta->last_txrate_idx & 0xffff, IWL_RATE_COUNT_3945 - 1);
Mohamed Abbas14577f22007-11-12 11:37:42 +0800687
Tomas Winkler28447f32008-03-06 17:36:54 -0800688 if (sband->band == IEEE80211_BAND_5GHZ)
Mohamed Abbas14577f22007-11-12 11:37:42 +0800689 rate_mask = rate_mask << IWL_FIRST_OFDM_RATE;
Zhu Yib481de92007-09-25 17:54:57 -0700690
Johannes Berg05c914f2008-09-11 00:01:58 +0200691 if ((priv->iw_mode == NL80211_IFTYPE_ADHOC) &&
Tomas Winklerfa254a62008-01-27 20:41:38 -0800692 !rs_sta->ibss_sta_added) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800693 u8 sta_id = iwl3945_hw_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -0700694
695 if (sta_id == IWL_INVALID_STATION) {
Johannes Berge1749612008-10-27 15:59:26 -0700696 IWL_DEBUG_RATE("LQ: ADD station %pm\n",
697 hdr->addr1);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800698 sta_id = iwl3945_add_station(priv,
Zhu Yib481de92007-09-25 17:54:57 -0700699 hdr->addr1, 0, CMD_ASYNC);
700 }
701 if (sta_id != IWL_INVALID_STATION)
Tomas Winklerfa254a62008-01-27 20:41:38 -0800702 rs_sta->ibss_sta_added = 1;
Zhu Yib481de92007-09-25 17:54:57 -0700703 }
704
Tomas Winklerfa254a62008-01-27 20:41:38 -0800705 spin_lock_irqsave(&rs_sta->lock, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700706
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800707 /* for recent assoc, choose best rate regarding
708 * to rssi value
709 */
Tomas Winklerfa254a62008-01-27 20:41:38 -0800710 if (rs_sta->start_rate != IWL_RATE_INVALID) {
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800711 if (rs_sta->start_rate < index &&
712 (rate_mask & (1 << rs_sta->start_rate)))
713 index = rs_sta->start_rate;
Tomas Winklerfa254a62008-01-27 20:41:38 -0800714 rs_sta->start_rate = IWL_RATE_INVALID;
Zhu Yib481de92007-09-25 17:54:57 -0700715 }
716
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -0800717 /* force user max rate if set by user */
718 if ((max_rate_idx != -1) && (max_rate_idx < index)) {
719 if (rate_mask & (1 << max_rate_idx))
720 index = max_rate_idx;
721 }
722
Tomas Winklerfa254a62008-01-27 20:41:38 -0800723 window = &(rs_sta->win[index]);
Zhu Yib481de92007-09-25 17:54:57 -0700724
725 fail_count = window->counter - window->success_counter;
726
727 if (((fail_count <= IWL_RATE_MIN_FAILURE_TH) &&
728 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH))) {
Tomas Winklerfa254a62008-01-27 20:41:38 -0800729 spin_unlock_irqrestore(&rs_sta->lock, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700730
731 IWL_DEBUG_RATE("Invalid average_tpt on rate %d: "
732 "counter: %d, success_counter: %d, "
733 "expected_tpt is %sNULL\n",
734 index,
735 window->counter,
736 window->success_counter,
Tomas Winklerfa254a62008-01-27 20:41:38 -0800737 rs_sta->expected_tpt ? "not " : "");
Zhu Yib481de92007-09-25 17:54:57 -0700738 goto out;
739
740 }
741
Zhu Yib481de92007-09-25 17:54:57 -0700742 current_tpt = window->average_tpt;
743
Tomas Winklerfa254a62008-01-27 20:41:38 -0800744 high_low = iwl3945_get_adjacent_rate(rs_sta, index, rate_mask,
Tomas Winkler28447f32008-03-06 17:36:54 -0800745 sband->band);
Zhu Yib481de92007-09-25 17:54:57 -0700746 low = high_low & 0xff;
747 high = (high_low >> 8) & 0xff;
748
Abbas, Mohamedc6ec7a92009-01-20 21:33:52 -0800749 /* If user set max rate, dont allow higher than user constrain */
750 if ((max_rate_idx != -1) && (max_rate_idx < high))
751 high = IWL_RATE_INVALID;
752
Zhu Yib481de92007-09-25 17:54:57 -0700753 if (low != IWL_RATE_INVALID)
Tomas Winklerfa254a62008-01-27 20:41:38 -0800754 low_tpt = rs_sta->win[low].average_tpt;
Zhu Yib481de92007-09-25 17:54:57 -0700755
756 if (high != IWL_RATE_INVALID)
Tomas Winklerfa254a62008-01-27 20:41:38 -0800757 high_tpt = rs_sta->win[high].average_tpt;
Zhu Yib481de92007-09-25 17:54:57 -0700758
Tomas Winklerfa254a62008-01-27 20:41:38 -0800759 spin_unlock_irqrestore(&rs_sta->lock, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700760
761 scale_action = 1;
762
763 if ((window->success_ratio < IWL_RATE_DECREASE_TH) || !current_tpt) {
764 IWL_DEBUG_RATE("decrease rate because of low success_ratio\n");
765 scale_action = -1;
Samuel Ortizd9829a62008-12-19 10:37:12 +0800766 } else if ((low_tpt == IWL_INVALID_VALUE) &&
767 (high_tpt == IWL_INVALID_VALUE))
Zhu Yib481de92007-09-25 17:54:57 -0700768 scale_action = 1;
Samuel Ortizd9829a62008-12-19 10:37:12 +0800769 else if ((low_tpt != IWL_INVALID_VALUE) &&
770 (high_tpt != IWL_INVALID_VALUE) &&
Tomas Winkler28447f32008-03-06 17:36:54 -0800771 (low_tpt < current_tpt) && (high_tpt < current_tpt)) {
Zhu Yib481de92007-09-25 17:54:57 -0700772 IWL_DEBUG_RATE("No action -- low [%d] & high [%d] < "
773 "current_tpt [%d]\n",
774 low_tpt, high_tpt, current_tpt);
775 scale_action = 0;
776 } else {
Samuel Ortizd9829a62008-12-19 10:37:12 +0800777 if (high_tpt != IWL_INVALID_VALUE) {
Zhu Yib481de92007-09-25 17:54:57 -0700778 if (high_tpt > current_tpt)
779 scale_action = 1;
780 else {
781 IWL_DEBUG_RATE
782 ("decrease rate because of high tpt\n");
783 scale_action = -1;
784 }
Samuel Ortizd9829a62008-12-19 10:37:12 +0800785 } else if (low_tpt != IWL_INVALID_VALUE) {
Zhu Yib481de92007-09-25 17:54:57 -0700786 if (low_tpt > current_tpt) {
787 IWL_DEBUG_RATE
788 ("decrease rate because of low tpt\n");
789 scale_action = -1;
790 } else
791 scale_action = 1;
792 }
793 }
794
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800795 if (scale_action == -1) {
796 if (window->success_ratio > IWL_SUCCESS_DOWN_TH)
797 scale_action = 0;
798 } else if (scale_action == 1) {
799 if (window->success_ratio < IWL_SUCCESS_UP_TH) {
800 IWL_DEBUG_RATE("No action -- success_ratio [%d] < "
801 "SUCCESS UP\n", window->success_ratio);
802 scale_action = 0;
803 }
Zhu Yib481de92007-09-25 17:54:57 -0700804 }
805
806 switch (scale_action) {
807 case -1:
808 if (low != IWL_RATE_INVALID)
809 index = low;
810 break;
811
812 case 1:
813 if (high != IWL_RATE_INVALID)
814 index = high;
815
816 break;
817
818 case 0:
819 default:
820 break;
821 }
822
823 IWL_DEBUG_RATE("Selected %d (action %d) - low %d high %d\n",
824 index, scale_action, low, high);
825
826 out:
827
Johannes Bergb7e35002008-09-11 02:22:58 +0200828 rs_sta->last_txrate_idx = index;
Tomas Winkler28447f32008-03-06 17:36:54 -0800829 if (sband->band == IEEE80211_BAND_5GHZ)
Johannes Berge6a98542008-10-21 12:40:02 +0200830 info->control.rates[0].idx = rs_sta->last_txrate_idx -
831 IWL_FIRST_OFDM_RATE;
Mohamed Abbas14577f22007-11-12 11:37:42 +0800832 else
Johannes Berge6a98542008-10-21 12:40:02 +0200833 info->control.rates[0].idx = rs_sta->last_txrate_idx;
Mohamed Abbas14577f22007-11-12 11:37:42 +0800834
Zhu Yib481de92007-09-25 17:54:57 -0700835 IWL_DEBUG_RATE("leave: %d\n", index);
Zhu Yib481de92007-09-25 17:54:57 -0700836}
837
Abbas, Mohamedc30e30e2008-12-02 12:14:02 -0800838#ifdef CONFIG_MAC80211_DEBUGFS
839static int iwl3945_open_file_generic(struct inode *inode, struct file *file)
840{
841 file->private_data = inode->i_private;
842 return 0;
843}
844
845static ssize_t iwl3945_sta_dbgfs_stats_table_read(struct file *file,
846 char __user *user_buf,
847 size_t count, loff_t *ppos)
848{
849 char buff[1024];
850 int desc = 0;
851 int j;
852 struct iwl3945_rs_sta *lq_sta = file->private_data;
853
854 desc += sprintf(buff + desc, "tx packets=%d last rate index=%d\n"
855 "rate=0x%X flush time %d\n",
856 lq_sta->tx_packets,
857 lq_sta->last_txrate_idx,
858 lq_sta->start_rate, jiffies_to_msecs(lq_sta->flush_time));
Samuel Ortizd9829a62008-12-19 10:37:12 +0800859 for (j = 0; j < IWL_RATE_COUNT_3945; j++) {
Abbas, Mohamedc30e30e2008-12-02 12:14:02 -0800860 desc += sprintf(buff+desc,
861 "counter=%d success=%d %%=%d\n",
862 lq_sta->win[j].counter,
863 lq_sta->win[j].success_counter,
864 lq_sta->win[j].success_ratio);
865 }
866 return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
867}
868
869static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
870 .read = iwl3945_sta_dbgfs_stats_table_read,
871 .open = iwl3945_open_file_generic,
872};
873
874static void iwl3945_add_debugfs(void *priv, void *priv_sta,
875 struct dentry *dir)
876{
877 struct iwl3945_rs_sta *lq_sta = priv_sta;
878
879 lq_sta->rs_sta_dbgfs_stats_table_file =
880 debugfs_create_file("rate_stats_table", 0600, dir,
881 lq_sta, &rs_sta_dbgfs_stats_table_ops);
882
883}
884
885static void iwl3945_remove_debugfs(void *priv, void *priv_sta)
886{
887 struct iwl3945_rs_sta *lq_sta = priv_sta;
888 debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
889}
890#endif
891
Zhu Yib481de92007-09-25 17:54:57 -0700892static struct rate_control_ops rs_ops = {
893 .module = NULL,
894 .name = RS_NAME,
895 .tx_status = rs_tx_status,
896 .get_rate = rs_get_rate,
897 .rate_init = rs_rate_init,
Zhu Yib481de92007-09-25 17:54:57 -0700898 .alloc = rs_alloc,
899 .free = rs_free,
900 .alloc_sta = rs_alloc_sta,
901 .free_sta = rs_free_sta,
Abbas, Mohamedc30e30e2008-12-02 12:14:02 -0800902#ifdef CONFIG_MAC80211_DEBUGFS
903 .add_sta_debugfs = iwl3945_add_debugfs,
904 .remove_sta_debugfs = iwl3945_remove_debugfs,
905#endif
906
Zhu Yib481de92007-09-25 17:54:57 -0700907};
908
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800909void iwl3945_rate_scale_init(struct ieee80211_hw *hw, s32 sta_id)
Zhu Yib481de92007-09-25 17:54:57 -0700910{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800911 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -0700912 s32 rssi = 0;
913 unsigned long flags;
Tomas Winklerfa254a62008-01-27 20:41:38 -0800914 struct iwl3945_rs_sta *rs_sta;
Johannes Berg4b7679a2008-09-18 18:14:18 +0200915 struct ieee80211_sta *sta;
916 struct iwl3945_sta_priv *psta;
Zhu Yib481de92007-09-25 17:54:57 -0700917
918 IWL_DEBUG_RATE("enter\n");
919
Johannes Bergd0709a62008-02-25 16:27:46 +0100920 rcu_read_lock();
921
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800922 sta = ieee80211_find_sta(hw, priv->stations_39[sta_id].sta.sta.addr);
Johannes Berg52518182008-10-11 01:46:25 +0200923 if (!sta) {
Johannes Bergd0709a62008-02-25 16:27:46 +0100924 rcu_read_unlock();
Zhu Yib481de92007-09-25 17:54:57 -0700925 return;
926 }
927
Johannes Berg52518182008-10-11 01:46:25 +0200928 psta = (void *) sta->drv_priv;
Johannes Berg4b7679a2008-09-18 18:14:18 +0200929 rs_sta = psta->rs_sta;
Zhu Yib481de92007-09-25 17:54:57 -0700930
Tomas Winklerfa254a62008-01-27 20:41:38 -0800931 spin_lock_irqsave(&rs_sta->lock, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700932
Tomas Winklerfa254a62008-01-27 20:41:38 -0800933 rs_sta->tgg = 0;
Johannes Berg8318d782008-01-24 19:38:38 +0100934 switch (priv->band) {
935 case IEEE80211_BAND_2GHZ:
936 /* TODO: this always does G, not a regression */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800937 if (priv->active39_rxon.flags & RXON_FLG_TGG_PROTECT_MSK) {
Tomas Winklerfa254a62008-01-27 20:41:38 -0800938 rs_sta->tgg = 1;
939 rs_sta->expected_tpt = iwl3945_expected_tpt_g_prot;
Zhu Yib481de92007-09-25 17:54:57 -0700940 } else
Tomas Winklerfa254a62008-01-27 20:41:38 -0800941 rs_sta->expected_tpt = iwl3945_expected_tpt_g;
Zhu Yib481de92007-09-25 17:54:57 -0700942 break;
943
Johannes Berg8318d782008-01-24 19:38:38 +0100944 case IEEE80211_BAND_5GHZ:
Tomas Winklerfa254a62008-01-27 20:41:38 -0800945 rs_sta->expected_tpt = iwl3945_expected_tpt_a;
Zhu Yib481de92007-09-25 17:54:57 -0700946 break;
Johannes Berg8318d782008-01-24 19:38:38 +0100947 case IEEE80211_NUM_BANDS:
948 BUG();
Zhu Yib481de92007-09-25 17:54:57 -0700949 break;
950 }
951
Tomas Winklerfa254a62008-01-27 20:41:38 -0800952 spin_unlock_irqrestore(&rs_sta->lock, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700953
954 rssi = priv->last_rx_rssi;
955 if (rssi == 0)
956 rssi = IWL_MIN_RSSI_VAL;
957
958 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RATE, "Network RSSI: %d\n", rssi);
959
Johannes Berg8318d782008-01-24 19:38:38 +0100960 rs_sta->start_rate = iwl3945_get_rate_index_by_rssi(rssi, priv->band);
Zhu Yib481de92007-09-25 17:54:57 -0700961
962 IWL_DEBUG_RATE("leave: rssi %d assign rate index: "
Tomas Winklerfa254a62008-01-27 20:41:38 -0800963 "%d (plcp 0x%x)\n", rssi, rs_sta->start_rate,
964 iwl3945_rates[rs_sta->start_rate].plcp);
Johannes Berg52518182008-10-11 01:46:25 +0200965 rcu_read_unlock();
Zhu Yib481de92007-09-25 17:54:57 -0700966}
967
Reinette Chatre897e1cf2008-03-28 16:21:09 -0700968int iwl3945_rate_control_register(void)
Zhu Yib481de92007-09-25 17:54:57 -0700969{
Reinette Chatre897e1cf2008-03-28 16:21:09 -0700970 return ieee80211_rate_control_register(&rs_ops);
Zhu Yib481de92007-09-25 17:54:57 -0700971}
972
Reinette Chatre897e1cf2008-03-28 16:21:09 -0700973void iwl3945_rate_control_unregister(void)
Zhu Yib481de92007-09-25 17:54:57 -0700974{
975 ieee80211_rate_control_unregister(&rs_ops);
976}
977
978