blob: 76100d5786fb9c34e55d2e6bb30d38024cdcd591 [file] [log] [blame]
Zhu Yib481de92007-09-25 17:54:57 -07001/******************************************************************************
2 *
Reinette Chatreeb7ae892008-03-11 16:17:17 -07003 * Copyright(c) 2005 - 2008 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:
22 * James P. Ketrenos <ipw2100-admin@linux.intel.com>
23 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24 *
25 *****************************************************************************/
26
27#include <linux/kernel.h>
28#include <linux/init.h>
29#include <linux/skbuff.h>
30#include <linux/wireless.h>
31#include <net/mac80211.h>
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
Christoph Hellwig5d08cd12007-10-25 17:15:50 +080039#include "iwl-3945.h"
Zhu Yib481de92007-09-25 17:54:57 -070040
41#define RS_NAME "iwl-3945-rs"
42
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080043struct iwl3945_rate_scale_data {
Zhu Yib481de92007-09-25 17:54:57 -070044 u64 data;
45 s32 success_counter;
46 s32 success_ratio;
47 s32 counter;
48 s32 average_tpt;
49 unsigned long stamp;
50};
51
Tomas Winklerfa254a62008-01-27 20:41:38 -080052struct iwl3945_rs_sta {
Zhu Yib481de92007-09-25 17:54:57 -070053 spinlock_t lock;
54 s32 *expected_tpt;
55 unsigned long last_partial_flush;
56 unsigned long last_flush;
57 u32 flush_time;
58 u32 last_tx_packets;
59 u32 tx_packets;
60 u8 tgg;
61 u8 flush_pending;
62 u8 start_rate;
63 u8 ibss_sta_added;
64 struct timer_list rate_scale_flush;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080065 struct iwl3945_rate_scale_data win[IWL_RATE_COUNT];
Abbas, Mohamedc30e30e2008-12-02 12:14:02 -080066#ifdef CONFIG_MAC80211_DEBUGFS
67 struct dentry *rs_sta_dbgfs_stats_table_file;
68#endif
Johannes Bergb7e35002008-09-11 02:22:58 +020069
70 /* used to be in sta_info */
71 int last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -070072};
73
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080074static s32 iwl3945_expected_tpt_g[IWL_RATE_COUNT] = {
Mohamed Abbas14577f22007-11-12 11:37:42 +080075 7, 13, 35, 58, 0, 0, 76, 104, 130, 168, 191, 202
Zhu Yib481de92007-09-25 17:54:57 -070076};
77
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080078static s32 iwl3945_expected_tpt_g_prot[IWL_RATE_COUNT] = {
Mohamed Abbas14577f22007-11-12 11:37:42 +080079 7, 13, 35, 58, 0, 0, 0, 80, 93, 113, 123, 125
Zhu Yib481de92007-09-25 17:54:57 -070080};
81
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080082static s32 iwl3945_expected_tpt_a[IWL_RATE_COUNT] = {
Mohamed Abbas14577f22007-11-12 11:37:42 +080083 0, 0, 0, 0, 40, 57, 72, 98, 121, 154, 177, 186
Zhu Yib481de92007-09-25 17:54:57 -070084};
85
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080086static s32 iwl3945_expected_tpt_b[IWL_RATE_COUNT] = {
Mohamed Abbas14577f22007-11-12 11:37:42 +080087 7, 13, 35, 58, 0, 0, 0, 0, 0, 0, 0, 0
Zhu Yib481de92007-09-25 17:54:57 -070088};
89
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080090struct iwl3945_tpt_entry {
Zhu Yib481de92007-09-25 17:54:57 -070091 s8 min_rssi;
92 u8 index;
93};
94
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080095static struct iwl3945_tpt_entry iwl3945_tpt_table_a[] = {
Zhu Yib481de92007-09-25 17:54:57 -070096 {-60, IWL_RATE_54M_INDEX},
97 {-64, IWL_RATE_48M_INDEX},
98 {-72, IWL_RATE_36M_INDEX},
99 {-80, IWL_RATE_24M_INDEX},
100 {-84, IWL_RATE_18M_INDEX},
101 {-85, IWL_RATE_12M_INDEX},
102 {-87, IWL_RATE_9M_INDEX},
103 {-89, IWL_RATE_6M_INDEX}
104};
105
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800106static struct iwl3945_tpt_entry iwl3945_tpt_table_g[] = {
Zhu Yib481de92007-09-25 17:54:57 -0700107 {-60, IWL_RATE_54M_INDEX},
108 {-64, IWL_RATE_48M_INDEX},
109 {-68, IWL_RATE_36M_INDEX},
110 {-80, IWL_RATE_24M_INDEX},
111 {-84, IWL_RATE_18M_INDEX},
112 {-85, IWL_RATE_12M_INDEX},
113 {-86, IWL_RATE_11M_INDEX},
114 {-88, IWL_RATE_5M_INDEX},
115 {-90, IWL_RATE_2M_INDEX},
116 {-92, IWL_RATE_1M_INDEX}
117};
118
119#define IWL_RATE_MAX_WINDOW 62
120#define IWL_RATE_FLUSH (3*HZ/10)
121#define IWL_RATE_WIN_FLUSH (HZ/2)
122#define IWL_RATE_HIGH_TH 11520
123#define IWL_RATE_MIN_FAILURE_TH 8
124#define IWL_RATE_MIN_SUCCESS_TH 8
125#define IWL_RATE_DECREASE_TH 1920
126
Johannes Berg8318d782008-01-24 19:38:38 +0100127static u8 iwl3945_get_rate_index_by_rssi(s32 rssi, enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -0700128{
129 u32 index = 0;
130 u32 table_size = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800131 struct iwl3945_tpt_entry *tpt_table = NULL;
Zhu Yib481de92007-09-25 17:54:57 -0700132
133 if ((rssi < IWL_MIN_RSSI_VAL) || (rssi > IWL_MAX_RSSI_VAL))
134 rssi = IWL_MIN_RSSI_VAL;
135
Johannes Berg8318d782008-01-24 19:38:38 +0100136 switch (band) {
137 case IEEE80211_BAND_2GHZ:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800138 tpt_table = iwl3945_tpt_table_g;
139 table_size = ARRAY_SIZE(iwl3945_tpt_table_g);
Zhu Yib481de92007-09-25 17:54:57 -0700140 break;
141
Johannes Berg8318d782008-01-24 19:38:38 +0100142 case IEEE80211_BAND_5GHZ:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800143 tpt_table = iwl3945_tpt_table_a;
144 table_size = ARRAY_SIZE(iwl3945_tpt_table_a);
Zhu Yib481de92007-09-25 17:54:57 -0700145 break;
146
147 default:
Johannes Berg8318d782008-01-24 19:38:38 +0100148 BUG();
Zhu Yib481de92007-09-25 17:54:57 -0700149 break;
150 }
151
152 while ((index < table_size) && (rssi < tpt_table[index].min_rssi))
153 index++;
154
155 index = min(index, (table_size - 1));
156
157 return tpt_table[index].index;
158}
159
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800160static void iwl3945_clear_window(struct iwl3945_rate_scale_data *window)
Zhu Yib481de92007-09-25 17:54:57 -0700161{
162 window->data = 0;
163 window->success_counter = 0;
Tomas Winkler28447f32008-03-06 17:36:54 -0800164 window->success_ratio = -1;
Zhu Yib481de92007-09-25 17:54:57 -0700165 window->counter = 0;
Tomas Winkler28447f32008-03-06 17:36:54 -0800166 window->average_tpt = IWL_INV_TPT;
Zhu Yib481de92007-09-25 17:54:57 -0700167 window->stamp = 0;
168}
169
170/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800171 * iwl3945_rate_scale_flush_windows - flush out the rate scale windows
Zhu Yib481de92007-09-25 17:54:57 -0700172 *
173 * Returns the number of windows that have gathered data but were
174 * not flushed. If there were any that were not flushed, then
175 * reschedule the rate flushing routine.
176 */
Tomas Winklerfa254a62008-01-27 20:41:38 -0800177static int iwl3945_rate_scale_flush_windows(struct iwl3945_rs_sta *rs_sta)
Zhu Yib481de92007-09-25 17:54:57 -0700178{
179 int unflushed = 0;
180 int i;
181 unsigned long flags;
182
183 /*
184 * For each rate, if we have collected data on that rate
185 * and it has been more than IWL_RATE_WIN_FLUSH
186 * since we flushed, clear out the gathered statistics
187 */
188 for (i = 0; i < IWL_RATE_COUNT; i++) {
Tomas Winklerfa254a62008-01-27 20:41:38 -0800189 if (!rs_sta->win[i].counter)
Zhu Yib481de92007-09-25 17:54:57 -0700190 continue;
191
Tomas Winklerfa254a62008-01-27 20:41:38 -0800192 spin_lock_irqsave(&rs_sta->lock, flags);
193 if (time_after(jiffies, rs_sta->win[i].stamp +
Zhu Yib481de92007-09-25 17:54:57 -0700194 IWL_RATE_WIN_FLUSH)) {
195 IWL_DEBUG_RATE("flushing %d samples of rate "
196 "index %d\n",
Tomas Winklerfa254a62008-01-27 20:41:38 -0800197 rs_sta->win[i].counter, i);
198 iwl3945_clear_window(&rs_sta->win[i]);
Zhu Yib481de92007-09-25 17:54:57 -0700199 } else
200 unflushed++;
Tomas Winklerfa254a62008-01-27 20:41:38 -0800201 spin_unlock_irqrestore(&rs_sta->lock, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700202 }
203
204 return unflushed;
205}
206
207#define IWL_RATE_FLUSH_MAX 5000 /* msec */
208#define IWL_RATE_FLUSH_MIN 50 /* msec */
209
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800210static void iwl3945_bg_rate_scale_flush(unsigned long data)
Zhu Yib481de92007-09-25 17:54:57 -0700211{
Tomas Winklerfa254a62008-01-27 20:41:38 -0800212 struct iwl3945_rs_sta *rs_sta = (void *)data;
Zhu Yib481de92007-09-25 17:54:57 -0700213 int unflushed = 0;
214 unsigned long flags;
215 u32 packet_count, duration, pps;
216
217 IWL_DEBUG_RATE("enter\n");
218
Tomas Winklerfa254a62008-01-27 20:41:38 -0800219 unflushed = iwl3945_rate_scale_flush_windows(rs_sta);
Zhu Yib481de92007-09-25 17:54:57 -0700220
Tomas Winklerfa254a62008-01-27 20:41:38 -0800221 spin_lock_irqsave(&rs_sta->lock, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700222
Tomas Winklerfa254a62008-01-27 20:41:38 -0800223 rs_sta->flush_pending = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700224
225 /* Number of packets Rx'd since last time this timer ran */
Tomas Winklerfa254a62008-01-27 20:41:38 -0800226 packet_count = (rs_sta->tx_packets - rs_sta->last_tx_packets) + 1;
Zhu Yib481de92007-09-25 17:54:57 -0700227
Tomas Winklerfa254a62008-01-27 20:41:38 -0800228 rs_sta->last_tx_packets = rs_sta->tx_packets + 1;
Zhu Yib481de92007-09-25 17:54:57 -0700229
230 if (unflushed) {
231 duration =
Tomas Winklerfa254a62008-01-27 20:41:38 -0800232 jiffies_to_msecs(jiffies - rs_sta->last_partial_flush);
233/* duration = jiffies_to_msecs(rs_sta->flush_time); */
Zhu Yib481de92007-09-25 17:54:57 -0700234
235 IWL_DEBUG_RATE("Tx'd %d packets in %dms\n",
236 packet_count, duration);
237
238 /* Determine packets per second */
239 if (duration)
240 pps = (packet_count * 1000) / duration;
241 else
242 pps = 0;
243
244 if (pps) {
245 duration = IWL_RATE_FLUSH_MAX / pps;
246 if (duration < IWL_RATE_FLUSH_MIN)
247 duration = IWL_RATE_FLUSH_MIN;
248 } else
249 duration = IWL_RATE_FLUSH_MAX;
250
Tomas Winklerfa254a62008-01-27 20:41:38 -0800251 rs_sta->flush_time = msecs_to_jiffies(duration);
Zhu Yib481de92007-09-25 17:54:57 -0700252
253 IWL_DEBUG_RATE("new flush period: %d msec ave %d\n",
254 duration, packet_count);
255
Tomas Winklerfa254a62008-01-27 20:41:38 -0800256 mod_timer(&rs_sta->rate_scale_flush, jiffies +
257 rs_sta->flush_time);
Zhu Yib481de92007-09-25 17:54:57 -0700258
Tomas Winklerfa254a62008-01-27 20:41:38 -0800259 rs_sta->last_partial_flush = jiffies;
Zhu Yib481de92007-09-25 17:54:57 -0700260 }
261
262 /* If there weren't any unflushed entries, we don't schedule the timer
263 * to run again */
264
Tomas Winklerfa254a62008-01-27 20:41:38 -0800265 rs_sta->last_flush = jiffies;
Zhu Yib481de92007-09-25 17:54:57 -0700266
Tomas Winklerfa254a62008-01-27 20:41:38 -0800267 spin_unlock_irqrestore(&rs_sta->lock, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700268
269 IWL_DEBUG_RATE("leave\n");
270}
271
272/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800273 * iwl3945_collect_tx_data - Update the success/failure sliding window
Zhu Yib481de92007-09-25 17:54:57 -0700274 *
275 * We keep a sliding window of the last 64 packets transmitted
276 * at this rate. window->data contains the bitmask of successful
277 * packets.
278 */
Tomas Winklerfa254a62008-01-27 20:41:38 -0800279static void iwl3945_collect_tx_data(struct iwl3945_rs_sta *rs_sta,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800280 struct iwl3945_rate_scale_data *window,
Zhu Yib481de92007-09-25 17:54:57 -0700281 int success, int retries)
282{
283 unsigned long flags;
284
285 if (!retries) {
286 IWL_DEBUG_RATE("leave: retries == 0 -- should be at least 1\n");
287 return;
288 }
289
290 while (retries--) {
Tomas Winklerfa254a62008-01-27 20:41:38 -0800291 spin_lock_irqsave(&rs_sta->lock, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700292
293 /* If we have filled up the window then subtract one from the
294 * success counter if the high-bit is counting toward
295 * success */
296 if (window->counter == IWL_RATE_MAX_WINDOW) {
297 if (window->data & (1ULL << (IWL_RATE_MAX_WINDOW - 1)))
298 window->success_counter--;
299 } else
300 window->counter++;
301
302 /* Slide the window to the left one bit */
303 window->data = (window->data << 1);
304
305 /* If this packet was a success then set the low bit high */
306 if (success) {
307 window->success_counter++;
308 window->data |= 1;
309 }
310
311 /* window->counter can't be 0 -- it is either >0 or
312 * IWL_RATE_MAX_WINDOW */
313 window->success_ratio = 12800 * window->success_counter /
314 window->counter;
315
316 /* Tag this window as having been updated */
317 window->stamp = jiffies;
318
Tomas Winklerfa254a62008-01-27 20:41:38 -0800319 spin_unlock_irqrestore(&rs_sta->lock, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700320 }
321}
322
Johannes Berg4b7679a2008-09-18 18:14:18 +0200323static void rs_rate_init(void *priv, struct ieee80211_supported_band *sband,
324 struct ieee80211_sta *sta, void *priv_sta)
Zhu Yib481de92007-09-25 17:54:57 -0700325{
Johannes Berg4b7679a2008-09-18 18:14:18 +0200326 struct iwl3945_rs_sta *rs_sta = priv_sta;
Zhu Yib481de92007-09-25 17:54:57 -0700327 int i;
328
329 IWL_DEBUG_RATE("enter\n");
330
331 /* TODO: what is a good starting rate for STA? About middle? Maybe not
332 * the lowest or the highest rate.. Could consider using RSSI from
333 * previous packets? Need to have IEEE 802.1X auth succeed immediately
334 * after assoc.. */
335
336 for (i = IWL_RATE_COUNT - 1; i >= 0; i--) {
Johannes Berg4b7679a2008-09-18 18:14:18 +0200337 if (sta->supp_rates[sband->band] & (1 << i)) {
Johannes Bergae17e982008-09-11 03:04:36 +0200338 rs_sta->last_txrate_idx = i;
Zhu Yib481de92007-09-25 17:54:57 -0700339 break;
340 }
341 }
342
Johannes Berg8318d782008-01-24 19:38:38 +0100343 /* For 5 GHz band it start at IWL_FIRST_OFDM_RATE */
Johannes Berg4b7679a2008-09-18 18:14:18 +0200344 if (sband->band == IEEE80211_BAND_5GHZ)
Johannes Bergb7e35002008-09-11 02:22:58 +0200345 rs_sta->last_txrate_idx += IWL_FIRST_OFDM_RATE;
Mohamed Abbas14577f22007-11-12 11:37:42 +0800346
Zhu Yib481de92007-09-25 17:54:57 -0700347 IWL_DEBUG_RATE("leave\n");
348}
349
Johannes Berg4b7679a2008-09-18 18:14:18 +0200350static void *rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
Zhu Yib481de92007-09-25 17:54:57 -0700351{
Johannes Berg4b7679a2008-09-18 18:14:18 +0200352 return hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -0700353}
354
Ian Schram01ebd062007-10-25 17:15:22 +0800355/* rate scale requires free function to be implemented */
Zhu Yib481de92007-09-25 17:54:57 -0700356static void rs_free(void *priv)
357{
358 return;
359}
Johannes Berg4b7679a2008-09-18 18:14:18 +0200360
Johannes Berg4b7679a2008-09-18 18:14:18 +0200361static void *rs_alloc_sta(void *priv, struct ieee80211_sta *sta, gfp_t gfp)
Zhu Yib481de92007-09-25 17:54:57 -0700362{
Tomas Winklerfa254a62008-01-27 20:41:38 -0800363 struct iwl3945_rs_sta *rs_sta;
Johannes Berg4b7679a2008-09-18 18:14:18 +0200364 struct iwl3945_sta_priv *psta = (void *) sta->drv_priv;
Zhu Yib481de92007-09-25 17:54:57 -0700365 int i;
366
Johannes Berg4b7679a2008-09-18 18:14:18 +0200367 /*
368 * XXX: If it's using sta->drv_priv anyway, it might
369 * as well just put all the information there.
370 */
371
Zhu Yib481de92007-09-25 17:54:57 -0700372 IWL_DEBUG_RATE("enter\n");
373
Tomas Winklerfa254a62008-01-27 20:41:38 -0800374 rs_sta = kzalloc(sizeof(struct iwl3945_rs_sta), gfp);
375 if (!rs_sta) {
Zhu Yib481de92007-09-25 17:54:57 -0700376 IWL_DEBUG_RATE("leave: ENOMEM\n");
377 return NULL;
378 }
379
Johannes Berg4b7679a2008-09-18 18:14:18 +0200380 psta->rs_sta = rs_sta;
381
Tomas Winklerfa254a62008-01-27 20:41:38 -0800382 spin_lock_init(&rs_sta->lock);
Zhu Yib481de92007-09-25 17:54:57 -0700383
Tomas Winklerfa254a62008-01-27 20:41:38 -0800384 rs_sta->start_rate = IWL_RATE_INVALID;
Zhu Yib481de92007-09-25 17:54:57 -0700385
386 /* default to just 802.11b */
Tomas Winklerfa254a62008-01-27 20:41:38 -0800387 rs_sta->expected_tpt = iwl3945_expected_tpt_b;
Zhu Yib481de92007-09-25 17:54:57 -0700388
Tomas Winklerfa254a62008-01-27 20:41:38 -0800389 rs_sta->last_partial_flush = jiffies;
390 rs_sta->last_flush = jiffies;
391 rs_sta->flush_time = IWL_RATE_FLUSH;
392 rs_sta->last_tx_packets = 0;
393 rs_sta->ibss_sta_added = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700394
Tomas Winklerfa254a62008-01-27 20:41:38 -0800395 init_timer(&rs_sta->rate_scale_flush);
396 rs_sta->rate_scale_flush.data = (unsigned long)rs_sta;
397 rs_sta->rate_scale_flush.function = &iwl3945_bg_rate_scale_flush;
Zhu Yib481de92007-09-25 17:54:57 -0700398
399 for (i = 0; i < IWL_RATE_COUNT; i++)
Tomas Winklerfa254a62008-01-27 20:41:38 -0800400 iwl3945_clear_window(&rs_sta->win[i]);
Zhu Yib481de92007-09-25 17:54:57 -0700401
402 IWL_DEBUG_RATE("leave\n");
403
Tomas Winklerfa254a62008-01-27 20:41:38 -0800404 return rs_sta;
Zhu Yib481de92007-09-25 17:54:57 -0700405}
406
Johannes Berg4b7679a2008-09-18 18:14:18 +0200407static void rs_free_sta(void *priv, struct ieee80211_sta *sta,
408 void *priv_sta)
Zhu Yib481de92007-09-25 17:54:57 -0700409{
Johannes Berg4b7679a2008-09-18 18:14:18 +0200410 struct iwl3945_sta_priv *psta = (void *) sta->drv_priv;
Tomas Winklerfa254a62008-01-27 20:41:38 -0800411 struct iwl3945_rs_sta *rs_sta = priv_sta;
Zhu Yib481de92007-09-25 17:54:57 -0700412
Johannes Berg4b7679a2008-09-18 18:14:18 +0200413 psta->rs_sta = NULL;
414
Zhu Yib481de92007-09-25 17:54:57 -0700415 IWL_DEBUG_RATE("enter\n");
Tomas Winklerfa254a62008-01-27 20:41:38 -0800416 del_timer_sync(&rs_sta->rate_scale_flush);
417 kfree(rs_sta);
Zhu Yib481de92007-09-25 17:54:57 -0700418 IWL_DEBUG_RATE("leave\n");
419}
420
Mohamed Abbas14577f22007-11-12 11:37:42 +0800421
Zhu Yib481de92007-09-25 17:54:57 -0700422/**
423 * rs_tx_status - Update rate control values based on Tx results
424 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800425 * NOTE: Uses iwl3945_priv->retry_rate for the # of retries attempted by
Zhu Yib481de92007-09-25 17:54:57 -0700426 * the hardware for each rate.
427 */
Johannes Berg4b7679a2008-09-18 18:14:18 +0200428static void rs_tx_status(void *priv_rate, struct ieee80211_supported_band *sband,
429 struct ieee80211_sta *sta, void *priv_sta,
Johannes Berge039fa42008-05-15 12:55:29 +0200430 struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -0700431{
Johannes Berge6a98542008-10-21 12:40:02 +0200432 u8 retries = 0, current_count;
Zhu Yib481de92007-09-25 17:54:57 -0700433 int scale_rate_index, first_index, last_index;
434 unsigned long flags;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800435 struct iwl3945_priv *priv = (struct iwl3945_priv *)priv_rate;
Johannes Berg4b7679a2008-09-18 18:14:18 +0200436 struct iwl3945_rs_sta *rs_sta = priv_sta;
Johannes Berge039fa42008-05-15 12:55:29 +0200437 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Johannes Berge6a98542008-10-21 12:40:02 +0200438 int i;
Johannes Berg8318d782008-01-24 19:38:38 +0100439
Zhu Yib481de92007-09-25 17:54:57 -0700440 IWL_DEBUG_RATE("enter\n");
441
Johannes Berge6a98542008-10-21 12:40:02 +0200442 for (i = 0; i < IEEE80211_TX_MAX_RATES; i++)
443 retries += info->status.rates[i].count;
444 retries--;
445
446 first_index = sband->bitrates[info->status.rates[0].idx].hw_value;
Zhu Yib481de92007-09-25 17:54:57 -0700447 if ((first_index < 0) || (first_index >= IWL_RATE_COUNT)) {
Johannes Berg8318d782008-01-24 19:38:38 +0100448 IWL_DEBUG_RATE("leave: Rate out of bounds: %d\n", first_index);
Zhu Yib481de92007-09-25 17:54:57 -0700449 return;
450 }
451
Johannes Berg4b7679a2008-09-18 18:14:18 +0200452 if (!priv_sta) {
Zhu Yib481de92007-09-25 17:54:57 -0700453 IWL_DEBUG_RATE("leave: No STA priv data to update!\n");
454 return;
455 }
456
Tomas Winklerfa254a62008-01-27 20:41:38 -0800457 rs_sta->tx_packets++;
Zhu Yib481de92007-09-25 17:54:57 -0700458
459 scale_rate_index = first_index;
460 last_index = first_index;
461
462 /*
463 * Update the window for each rate. We determine which rates
464 * were Tx'd based on the total number of retries vs. the number
465 * of retries configured for each rate -- currently set to the
466 * priv value 'retry_rate' vs. rate specific
467 *
468 * On exit from this while loop last_index indicates the rate
469 * at which the frame was finally transmitted (or failed if no
470 * ACK)
471 */
472 while (retries > 0) {
473 if (retries < priv->retry_rate) {
474 current_count = retries;
475 last_index = scale_rate_index;
476 } else {
477 current_count = priv->retry_rate;
Johannes Berge6a98542008-10-21 12:40:02 +0200478 last_index = iwl3945_rs_next_rate(priv,
Mohamed Abbas14577f22007-11-12 11:37:42 +0800479 scale_rate_index);
Zhu Yib481de92007-09-25 17:54:57 -0700480 }
481
482 /* Update this rate accounting for as many retries
483 * as was used for it (per current_count) */
Tomas Winklerfa254a62008-01-27 20:41:38 -0800484 iwl3945_collect_tx_data(rs_sta,
485 &rs_sta->win[scale_rate_index],
Zhu Yib481de92007-09-25 17:54:57 -0700486 0, current_count);
487 IWL_DEBUG_RATE("Update rate %d for %d retries.\n",
488 scale_rate_index, current_count);
489
490 retries -= current_count;
491
492 if (retries)
493 scale_rate_index =
Johannes Berge6a98542008-10-21 12:40:02 +0200494 iwl3945_rs_next_rate(priv, scale_rate_index);
Zhu Yib481de92007-09-25 17:54:57 -0700495 }
496
Mohamed Abbas14577f22007-11-12 11:37:42 +0800497
Zhu Yib481de92007-09-25 17:54:57 -0700498 /* Update the last index window with success/failure based on ACK */
499 IWL_DEBUG_RATE("Update rate %d with %s.\n",
500 last_index,
Johannes Berge039fa42008-05-15 12:55:29 +0200501 (info->flags & IEEE80211_TX_STAT_ACK) ?
Zhu Yib481de92007-09-25 17:54:57 -0700502 "success" : "failure");
Tomas Winklerfa254a62008-01-27 20:41:38 -0800503 iwl3945_collect_tx_data(rs_sta,
504 &rs_sta->win[last_index],
Johannes Berge039fa42008-05-15 12:55:29 +0200505 info->flags & IEEE80211_TX_STAT_ACK, 1);
Zhu Yib481de92007-09-25 17:54:57 -0700506
507 /* We updated the rate scale window -- if its been more than
508 * flush_time since the last run, schedule the flush
509 * again */
Tomas Winklerfa254a62008-01-27 20:41:38 -0800510 spin_lock_irqsave(&rs_sta->lock, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700511
Tomas Winklerfa254a62008-01-27 20:41:38 -0800512 if (!rs_sta->flush_pending &&
513 time_after(jiffies, rs_sta->last_partial_flush +
514 rs_sta->flush_time)) {
Zhu Yib481de92007-09-25 17:54:57 -0700515
Tomas Winklerfa254a62008-01-27 20:41:38 -0800516 rs_sta->flush_pending = 1;
517 mod_timer(&rs_sta->rate_scale_flush,
518 jiffies + rs_sta->flush_time);
Zhu Yib481de92007-09-25 17:54:57 -0700519 }
520
Tomas Winklerfa254a62008-01-27 20:41:38 -0800521 spin_unlock_irqrestore(&rs_sta->lock, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700522
Zhu Yib481de92007-09-25 17:54:57 -0700523 IWL_DEBUG_RATE("leave\n");
524
525 return;
526}
527
Tomas Winklerfa254a62008-01-27 20:41:38 -0800528static u16 iwl3945_get_adjacent_rate(struct iwl3945_rs_sta *rs_sta,
Johannes Berg8318d782008-01-24 19:38:38 +0100529 u8 index, u16 rate_mask, enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -0700530{
531 u8 high = IWL_RATE_INVALID;
532 u8 low = IWL_RATE_INVALID;
533
Ian Schram01ebd062007-10-25 17:15:22 +0800534 /* 802.11A walks to the next literal adjacent rate in
Zhu Yib481de92007-09-25 17:54:57 -0700535 * the rate table */
Johannes Berg8318d782008-01-24 19:38:38 +0100536 if (unlikely(band == IEEE80211_BAND_5GHZ)) {
Zhu Yib481de92007-09-25 17:54:57 -0700537 int i;
538 u32 mask;
539
540 /* Find the previous rate that is in the rate mask */
541 i = index - 1;
542 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
543 if (rate_mask & mask) {
544 low = i;
545 break;
546 }
547 }
548
549 /* Find the next rate that is in the rate mask */
550 i = index + 1;
551 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
552 if (rate_mask & mask) {
553 high = i;
554 break;
555 }
556 }
557
558 return (high << 8) | low;
559 }
560
561 low = index;
562 while (low != IWL_RATE_INVALID) {
Tomas Winklerfa254a62008-01-27 20:41:38 -0800563 if (rs_sta->tgg)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800564 low = iwl3945_rates[low].prev_rs_tgg;
Zhu Yib481de92007-09-25 17:54:57 -0700565 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800566 low = iwl3945_rates[low].prev_rs;
Zhu Yib481de92007-09-25 17:54:57 -0700567 if (low == IWL_RATE_INVALID)
568 break;
569 if (rate_mask & (1 << low))
570 break;
571 IWL_DEBUG_RATE("Skipping masked lower rate: %d\n", low);
572 }
573
574 high = index;
575 while (high != IWL_RATE_INVALID) {
Tomas Winklerfa254a62008-01-27 20:41:38 -0800576 if (rs_sta->tgg)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800577 high = iwl3945_rates[high].next_rs_tgg;
Zhu Yib481de92007-09-25 17:54:57 -0700578 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800579 high = iwl3945_rates[high].next_rs;
Zhu Yib481de92007-09-25 17:54:57 -0700580 if (high == IWL_RATE_INVALID)
581 break;
582 if (rate_mask & (1 << high))
583 break;
584 IWL_DEBUG_RATE("Skipping masked higher rate: %d\n", high);
585 }
586
587 return (high << 8) | low;
588}
589
590/**
591 * rs_get_rate - find the rate for the requested packet
592 *
593 * Returns the ieee80211_rate structure allocated by the driver.
594 *
595 * The rate control algorithm has no internal mapping between hw_mode's
596 * rate ordering and the rate ordering used by the rate control algorithm.
597 *
598 * The rate control algorithm uses a single table of rates that goes across
599 * the entire A/B/G spectrum vs. being limited to just one particular
600 * hw_mode.
601 *
602 * As such, we can't convert the index obtained below into the hw_mode's
603 * rate table and must reference the driver allocated rate table
604 *
605 */
Johannes Berge6a98542008-10-21 12:40:02 +0200606static void rs_get_rate(void *priv_r, struct ieee80211_sta *sta,
607 void *priv_sta, struct ieee80211_tx_rate_control *txrc)
Zhu Yib481de92007-09-25 17:54:57 -0700608{
Johannes Berge6a98542008-10-21 12:40:02 +0200609 struct ieee80211_supported_band *sband = txrc->sband;
610 struct sk_buff *skb = txrc->skb;
Zhu Yib481de92007-09-25 17:54:57 -0700611 u8 low = IWL_RATE_INVALID;
612 u8 high = IWL_RATE_INVALID;
613 u16 high_low;
614 int index;
Johannes Berg4b7679a2008-09-18 18:14:18 +0200615 struct iwl3945_rs_sta *rs_sta = priv_sta;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800616 struct iwl3945_rate_scale_data *window = NULL;
Tomas Winkler28447f32008-03-06 17:36:54 -0800617 int current_tpt = IWL_INV_TPT;
618 int low_tpt = IWL_INV_TPT;
619 int high_tpt = IWL_INV_TPT;
Zhu Yib481de92007-09-25 17:54:57 -0700620 u32 fail_count;
621 s8 scale_action = 0;
622 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -0700623 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Michael Wu2bc454b2007-12-25 19:33:16 -0500624 u16 fc, rate_mask;
Johannes Berg4b7679a2008-09-18 18:14:18 +0200625 struct iwl3945_priv *priv = (struct iwl3945_priv *)priv_r;
Johannes Berge6a98542008-10-21 12:40:02 +0200626 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Zhu Yib481de92007-09-25 17:54:57 -0700627
628 IWL_DEBUG_RATE("enter\n");
629
Michael Wu2bc454b2007-12-25 19:33:16 -0500630 /* Send management frames and broadcast/multicast data using lowest
631 * rate. */
632 fc = le16_to_cpu(hdr->frame_control);
633 if ((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA ||
634 is_multicast_ether_addr(hdr->addr1) ||
Johannes Berg4b7679a2008-09-18 18:14:18 +0200635 !sta || !priv_sta) {
Zhu Yib481de92007-09-25 17:54:57 -0700636 IWL_DEBUG_RATE("leave: No STA priv data to update!\n");
Johannes Berge6a98542008-10-21 12:40:02 +0200637 info->control.rates[0].idx = rate_lowest_index(sband, sta);
Mattias Nissler1abbe492007-12-20 13:50:07 +0100638 return;
Zhu Yib481de92007-09-25 17:54:57 -0700639 }
640
Johannes Berg4b7679a2008-09-18 18:14:18 +0200641 rate_mask = sta->supp_rates[sband->band];
Johannes Bergb7e35002008-09-11 02:22:58 +0200642 index = min(rs_sta->last_txrate_idx & 0xffff, IWL_RATE_COUNT - 1);
Mohamed Abbas14577f22007-11-12 11:37:42 +0800643
Tomas Winkler28447f32008-03-06 17:36:54 -0800644 if (sband->band == IEEE80211_BAND_5GHZ)
Mohamed Abbas14577f22007-11-12 11:37:42 +0800645 rate_mask = rate_mask << IWL_FIRST_OFDM_RATE;
Zhu Yib481de92007-09-25 17:54:57 -0700646
Johannes Berg05c914f2008-09-11 00:01:58 +0200647 if ((priv->iw_mode == NL80211_IFTYPE_ADHOC) &&
Tomas Winklerfa254a62008-01-27 20:41:38 -0800648 !rs_sta->ibss_sta_added) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800649 u8 sta_id = iwl3945_hw_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -0700650
651 if (sta_id == IWL_INVALID_STATION) {
Johannes Berge1749612008-10-27 15:59:26 -0700652 IWL_DEBUG_RATE("LQ: ADD station %pm\n",
653 hdr->addr1);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800654 sta_id = iwl3945_add_station(priv,
Zhu Yib481de92007-09-25 17:54:57 -0700655 hdr->addr1, 0, CMD_ASYNC);
656 }
657 if (sta_id != IWL_INVALID_STATION)
Tomas Winklerfa254a62008-01-27 20:41:38 -0800658 rs_sta->ibss_sta_added = 1;
Zhu Yib481de92007-09-25 17:54:57 -0700659 }
660
Tomas Winklerfa254a62008-01-27 20:41:38 -0800661 spin_lock_irqsave(&rs_sta->lock, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700662
Tomas Winklerfa254a62008-01-27 20:41:38 -0800663 if (rs_sta->start_rate != IWL_RATE_INVALID) {
664 index = rs_sta->start_rate;
665 rs_sta->start_rate = IWL_RATE_INVALID;
Zhu Yib481de92007-09-25 17:54:57 -0700666 }
667
Tomas Winklerfa254a62008-01-27 20:41:38 -0800668 window = &(rs_sta->win[index]);
Zhu Yib481de92007-09-25 17:54:57 -0700669
670 fail_count = window->counter - window->success_counter;
671
672 if (((fail_count <= IWL_RATE_MIN_FAILURE_TH) &&
673 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH))) {
Tomas Winkler28447f32008-03-06 17:36:54 -0800674 window->average_tpt = IWL_INV_TPT;
Tomas Winklerfa254a62008-01-27 20:41:38 -0800675 spin_unlock_irqrestore(&rs_sta->lock, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700676
677 IWL_DEBUG_RATE("Invalid average_tpt on rate %d: "
678 "counter: %d, success_counter: %d, "
679 "expected_tpt is %sNULL\n",
680 index,
681 window->counter,
682 window->success_counter,
Tomas Winklerfa254a62008-01-27 20:41:38 -0800683 rs_sta->expected_tpt ? "not " : "");
Zhu Yib481de92007-09-25 17:54:57 -0700684 goto out;
685
686 }
687
688 window->average_tpt = ((window->success_ratio *
Tomas Winklerfa254a62008-01-27 20:41:38 -0800689 rs_sta->expected_tpt[index] + 64) / 128);
Zhu Yib481de92007-09-25 17:54:57 -0700690 current_tpt = window->average_tpt;
691
Tomas Winklerfa254a62008-01-27 20:41:38 -0800692 high_low = iwl3945_get_adjacent_rate(rs_sta, index, rate_mask,
Tomas Winkler28447f32008-03-06 17:36:54 -0800693 sband->band);
Zhu Yib481de92007-09-25 17:54:57 -0700694 low = high_low & 0xff;
695 high = (high_low >> 8) & 0xff;
696
697 if (low != IWL_RATE_INVALID)
Tomas Winklerfa254a62008-01-27 20:41:38 -0800698 low_tpt = rs_sta->win[low].average_tpt;
Zhu Yib481de92007-09-25 17:54:57 -0700699
700 if (high != IWL_RATE_INVALID)
Tomas Winklerfa254a62008-01-27 20:41:38 -0800701 high_tpt = rs_sta->win[high].average_tpt;
Zhu Yib481de92007-09-25 17:54:57 -0700702
Tomas Winklerfa254a62008-01-27 20:41:38 -0800703 spin_unlock_irqrestore(&rs_sta->lock, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700704
705 scale_action = 1;
706
707 if ((window->success_ratio < IWL_RATE_DECREASE_TH) || !current_tpt) {
708 IWL_DEBUG_RATE("decrease rate because of low success_ratio\n");
709 scale_action = -1;
Tomas Winkler28447f32008-03-06 17:36:54 -0800710 } else if ((low_tpt == IWL_INV_TPT) && (high_tpt == IWL_INV_TPT))
Zhu Yib481de92007-09-25 17:54:57 -0700711 scale_action = 1;
Tomas Winkler28447f32008-03-06 17:36:54 -0800712 else if ((low_tpt != IWL_INV_TPT) && (high_tpt != IWL_INV_TPT) &&
713 (low_tpt < current_tpt) && (high_tpt < current_tpt)) {
Zhu Yib481de92007-09-25 17:54:57 -0700714 IWL_DEBUG_RATE("No action -- low [%d] & high [%d] < "
715 "current_tpt [%d]\n",
716 low_tpt, high_tpt, current_tpt);
717 scale_action = 0;
718 } else {
Tomas Winkler28447f32008-03-06 17:36:54 -0800719 if (high_tpt != IWL_INV_TPT) {
Zhu Yib481de92007-09-25 17:54:57 -0700720 if (high_tpt > current_tpt)
721 scale_action = 1;
722 else {
723 IWL_DEBUG_RATE
724 ("decrease rate because of high tpt\n");
725 scale_action = -1;
726 }
Tomas Winkler28447f32008-03-06 17:36:54 -0800727 } else if (low_tpt != IWL_INV_TPT) {
Zhu Yib481de92007-09-25 17:54:57 -0700728 if (low_tpt > current_tpt) {
729 IWL_DEBUG_RATE
730 ("decrease rate because of low tpt\n");
731 scale_action = -1;
732 } else
733 scale_action = 1;
734 }
735 }
736
737 if ((window->success_ratio > IWL_RATE_HIGH_TH) ||
738 (current_tpt > window->average_tpt)) {
739 IWL_DEBUG_RATE("No action -- success_ratio [%d] > HIGH_TH or "
740 "current_tpt [%d] > average_tpt [%d]\n",
741 window->success_ratio,
742 current_tpt, window->average_tpt);
743 scale_action = 0;
744 }
745
746 switch (scale_action) {
747 case -1:
748 if (low != IWL_RATE_INVALID)
749 index = low;
750 break;
751
752 case 1:
753 if (high != IWL_RATE_INVALID)
754 index = high;
755
756 break;
757
758 case 0:
759 default:
760 break;
761 }
762
763 IWL_DEBUG_RATE("Selected %d (action %d) - low %d high %d\n",
764 index, scale_action, low, high);
765
766 out:
767
Johannes Bergb7e35002008-09-11 02:22:58 +0200768 rs_sta->last_txrate_idx = index;
Tomas Winkler28447f32008-03-06 17:36:54 -0800769 if (sband->band == IEEE80211_BAND_5GHZ)
Johannes Berge6a98542008-10-21 12:40:02 +0200770 info->control.rates[0].idx = rs_sta->last_txrate_idx -
771 IWL_FIRST_OFDM_RATE;
Mohamed Abbas14577f22007-11-12 11:37:42 +0800772 else
Johannes Berge6a98542008-10-21 12:40:02 +0200773 info->control.rates[0].idx = rs_sta->last_txrate_idx;
Mohamed Abbas14577f22007-11-12 11:37:42 +0800774
Zhu Yib481de92007-09-25 17:54:57 -0700775 IWL_DEBUG_RATE("leave: %d\n", index);
Zhu Yib481de92007-09-25 17:54:57 -0700776}
777
Abbas, Mohamedc30e30e2008-12-02 12:14:02 -0800778#ifdef CONFIG_MAC80211_DEBUGFS
779static int iwl3945_open_file_generic(struct inode *inode, struct file *file)
780{
781 file->private_data = inode->i_private;
782 return 0;
783}
784
785static ssize_t iwl3945_sta_dbgfs_stats_table_read(struct file *file,
786 char __user *user_buf,
787 size_t count, loff_t *ppos)
788{
789 char buff[1024];
790 int desc = 0;
791 int j;
792 struct iwl3945_rs_sta *lq_sta = file->private_data;
793
794 desc += sprintf(buff + desc, "tx packets=%d last rate index=%d\n"
795 "rate=0x%X flush time %d\n",
796 lq_sta->tx_packets,
797 lq_sta->last_txrate_idx,
798 lq_sta->start_rate, jiffies_to_msecs(lq_sta->flush_time));
799 for (j = 0; j < IWL_RATE_COUNT; j++) {
800 desc += sprintf(buff+desc,
801 "counter=%d success=%d %%=%d\n",
802 lq_sta->win[j].counter,
803 lq_sta->win[j].success_counter,
804 lq_sta->win[j].success_ratio);
805 }
806 return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
807}
808
809static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
810 .read = iwl3945_sta_dbgfs_stats_table_read,
811 .open = iwl3945_open_file_generic,
812};
813
814static void iwl3945_add_debugfs(void *priv, void *priv_sta,
815 struct dentry *dir)
816{
817 struct iwl3945_rs_sta *lq_sta = priv_sta;
818
819 lq_sta->rs_sta_dbgfs_stats_table_file =
820 debugfs_create_file("rate_stats_table", 0600, dir,
821 lq_sta, &rs_sta_dbgfs_stats_table_ops);
822
823}
824
825static void iwl3945_remove_debugfs(void *priv, void *priv_sta)
826{
827 struct iwl3945_rs_sta *lq_sta = priv_sta;
828 debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
829}
830#endif
831
Zhu Yib481de92007-09-25 17:54:57 -0700832static struct rate_control_ops rs_ops = {
833 .module = NULL,
834 .name = RS_NAME,
835 .tx_status = rs_tx_status,
836 .get_rate = rs_get_rate,
837 .rate_init = rs_rate_init,
Zhu Yib481de92007-09-25 17:54:57 -0700838 .alloc = rs_alloc,
839 .free = rs_free,
840 .alloc_sta = rs_alloc_sta,
841 .free_sta = rs_free_sta,
Abbas, Mohamedc30e30e2008-12-02 12:14:02 -0800842#ifdef CONFIG_MAC80211_DEBUGFS
843 .add_sta_debugfs = iwl3945_add_debugfs,
844 .remove_sta_debugfs = iwl3945_remove_debugfs,
845#endif
846
Zhu Yib481de92007-09-25 17:54:57 -0700847};
848
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800849void iwl3945_rate_scale_init(struct ieee80211_hw *hw, s32 sta_id)
Zhu Yib481de92007-09-25 17:54:57 -0700850{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800851 struct iwl3945_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -0700852 s32 rssi = 0;
853 unsigned long flags;
Tomas Winklerfa254a62008-01-27 20:41:38 -0800854 struct iwl3945_rs_sta *rs_sta;
Johannes Berg4b7679a2008-09-18 18:14:18 +0200855 struct ieee80211_sta *sta;
856 struct iwl3945_sta_priv *psta;
Zhu Yib481de92007-09-25 17:54:57 -0700857
858 IWL_DEBUG_RATE("enter\n");
859
Johannes Bergd0709a62008-02-25 16:27:46 +0100860 rcu_read_lock();
861
Johannes Berg4b7679a2008-09-18 18:14:18 +0200862 sta = ieee80211_find_sta(hw, priv->stations[sta_id].sta.sta.addr);
Johannes Berg525181892008-10-11 01:46:25 +0200863 if (!sta) {
Johannes Bergd0709a62008-02-25 16:27:46 +0100864 rcu_read_unlock();
Zhu Yib481de92007-09-25 17:54:57 -0700865 return;
866 }
867
Johannes Berg525181892008-10-11 01:46:25 +0200868 psta = (void *) sta->drv_priv;
Johannes Berg4b7679a2008-09-18 18:14:18 +0200869 rs_sta = psta->rs_sta;
Zhu Yib481de92007-09-25 17:54:57 -0700870
Tomas Winklerfa254a62008-01-27 20:41:38 -0800871 spin_lock_irqsave(&rs_sta->lock, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700872
Tomas Winklerfa254a62008-01-27 20:41:38 -0800873 rs_sta->tgg = 0;
Johannes Berg8318d782008-01-24 19:38:38 +0100874 switch (priv->band) {
875 case IEEE80211_BAND_2GHZ:
876 /* TODO: this always does G, not a regression */
Zhu Yib481de92007-09-25 17:54:57 -0700877 if (priv->active_rxon.flags & RXON_FLG_TGG_PROTECT_MSK) {
Tomas Winklerfa254a62008-01-27 20:41:38 -0800878 rs_sta->tgg = 1;
879 rs_sta->expected_tpt = iwl3945_expected_tpt_g_prot;
Zhu Yib481de92007-09-25 17:54:57 -0700880 } else
Tomas Winklerfa254a62008-01-27 20:41:38 -0800881 rs_sta->expected_tpt = iwl3945_expected_tpt_g;
Zhu Yib481de92007-09-25 17:54:57 -0700882 break;
883
Johannes Berg8318d782008-01-24 19:38:38 +0100884 case IEEE80211_BAND_5GHZ:
Tomas Winklerfa254a62008-01-27 20:41:38 -0800885 rs_sta->expected_tpt = iwl3945_expected_tpt_a;
Zhu Yib481de92007-09-25 17:54:57 -0700886 break;
Johannes Berg8318d782008-01-24 19:38:38 +0100887 case IEEE80211_NUM_BANDS:
888 BUG();
Zhu Yib481de92007-09-25 17:54:57 -0700889 break;
890 }
891
Tomas Winklerfa254a62008-01-27 20:41:38 -0800892 spin_unlock_irqrestore(&rs_sta->lock, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700893
894 rssi = priv->last_rx_rssi;
895 if (rssi == 0)
896 rssi = IWL_MIN_RSSI_VAL;
897
898 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RATE, "Network RSSI: %d\n", rssi);
899
Johannes Berg8318d782008-01-24 19:38:38 +0100900 rs_sta->start_rate = iwl3945_get_rate_index_by_rssi(rssi, priv->band);
Zhu Yib481de92007-09-25 17:54:57 -0700901
902 IWL_DEBUG_RATE("leave: rssi %d assign rate index: "
Tomas Winklerfa254a62008-01-27 20:41:38 -0800903 "%d (plcp 0x%x)\n", rssi, rs_sta->start_rate,
904 iwl3945_rates[rs_sta->start_rate].plcp);
Johannes Berg525181892008-10-11 01:46:25 +0200905 rcu_read_unlock();
Zhu Yib481de92007-09-25 17:54:57 -0700906}
907
Reinette Chatre897e1cf2008-03-28 16:21:09 -0700908int iwl3945_rate_control_register(void)
Zhu Yib481de92007-09-25 17:54:57 -0700909{
Reinette Chatre897e1cf2008-03-28 16:21:09 -0700910 return ieee80211_rate_control_register(&rs_ops);
Zhu Yib481de92007-09-25 17:54:57 -0700911}
912
Reinette Chatre897e1cf2008-03-28 16:21:09 -0700913void iwl3945_rate_control_unregister(void)
Zhu Yib481de92007-09-25 17:54:57 -0700914{
915 ieee80211_rate_control_unregister(&rs_ops);
916}
917
918