blob: 21c841847d886022072f44fea41792ae64227d0c [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:
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
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
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800120#define IWL_RATE_FLUSH (3*HZ)
Zhu Yib481de92007-09-25 17:54:57 -0700121#define IWL_RATE_WIN_FLUSH (HZ/2)
122#define IWL_RATE_HIGH_TH 11520
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800123#define IWL_SUCCESS_UP_TH 8960
124#define IWL_SUCCESS_DOWN_TH 10880
Zhu Yib481de92007-09-25 17:54:57 -0700125#define IWL_RATE_MIN_FAILURE_TH 8
126#define IWL_RATE_MIN_SUCCESS_TH 8
127#define IWL_RATE_DECREASE_TH 1920
128
Johannes Berg8318d782008-01-24 19:38:38 +0100129static u8 iwl3945_get_rate_index_by_rssi(s32 rssi, enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -0700130{
131 u32 index = 0;
132 u32 table_size = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800133 struct iwl3945_tpt_entry *tpt_table = NULL;
Zhu Yib481de92007-09-25 17:54:57 -0700134
135 if ((rssi < IWL_MIN_RSSI_VAL) || (rssi > IWL_MAX_RSSI_VAL))
136 rssi = IWL_MIN_RSSI_VAL;
137
Johannes Berg8318d782008-01-24 19:38:38 +0100138 switch (band) {
139 case IEEE80211_BAND_2GHZ:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800140 tpt_table = iwl3945_tpt_table_g;
141 table_size = ARRAY_SIZE(iwl3945_tpt_table_g);
Zhu Yib481de92007-09-25 17:54:57 -0700142 break;
143
Johannes Berg8318d782008-01-24 19:38:38 +0100144 case IEEE80211_BAND_5GHZ:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800145 tpt_table = iwl3945_tpt_table_a;
146 table_size = ARRAY_SIZE(iwl3945_tpt_table_a);
Zhu Yib481de92007-09-25 17:54:57 -0700147 break;
148
149 default:
Johannes Berg8318d782008-01-24 19:38:38 +0100150 BUG();
Zhu Yib481de92007-09-25 17:54:57 -0700151 break;
152 }
153
154 while ((index < table_size) && (rssi < tpt_table[index].min_rssi))
155 index++;
156
157 index = min(index, (table_size - 1));
158
159 return tpt_table[index].index;
160}
161
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800162static void iwl3945_clear_window(struct iwl3945_rate_scale_data *window)
Zhu Yib481de92007-09-25 17:54:57 -0700163{
164 window->data = 0;
165 window->success_counter = 0;
Tomas Winkler28447f32008-03-06 17:36:54 -0800166 window->success_ratio = -1;
Zhu Yib481de92007-09-25 17:54:57 -0700167 window->counter = 0;
Tomas Winkler28447f32008-03-06 17:36:54 -0800168 window->average_tpt = IWL_INV_TPT;
Zhu Yib481de92007-09-25 17:54:57 -0700169 window->stamp = 0;
170}
171
172/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800173 * iwl3945_rate_scale_flush_windows - flush out the rate scale windows
Zhu Yib481de92007-09-25 17:54:57 -0700174 *
175 * Returns the number of windows that have gathered data but were
176 * not flushed. If there were any that were not flushed, then
177 * reschedule the rate flushing routine.
178 */
Tomas Winklerfa254a62008-01-27 20:41:38 -0800179static int iwl3945_rate_scale_flush_windows(struct iwl3945_rs_sta *rs_sta)
Zhu Yib481de92007-09-25 17:54:57 -0700180{
181 int unflushed = 0;
182 int i;
183 unsigned long flags;
184
185 /*
186 * For each rate, if we have collected data on that rate
187 * and it has been more than IWL_RATE_WIN_FLUSH
188 * since we flushed, clear out the gathered statistics
189 */
190 for (i = 0; i < IWL_RATE_COUNT; i++) {
Tomas Winklerfa254a62008-01-27 20:41:38 -0800191 if (!rs_sta->win[i].counter)
Zhu Yib481de92007-09-25 17:54:57 -0700192 continue;
193
Tomas Winklerfa254a62008-01-27 20:41:38 -0800194 spin_lock_irqsave(&rs_sta->lock, flags);
195 if (time_after(jiffies, rs_sta->win[i].stamp +
Zhu Yib481de92007-09-25 17:54:57 -0700196 IWL_RATE_WIN_FLUSH)) {
197 IWL_DEBUG_RATE("flushing %d samples of rate "
198 "index %d\n",
Tomas Winklerfa254a62008-01-27 20:41:38 -0800199 rs_sta->win[i].counter, i);
200 iwl3945_clear_window(&rs_sta->win[i]);
Zhu Yib481de92007-09-25 17:54:57 -0700201 } else
202 unflushed++;
Tomas Winklerfa254a62008-01-27 20:41:38 -0800203 spin_unlock_irqrestore(&rs_sta->lock, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700204 }
205
206 return unflushed;
207}
208
209#define IWL_RATE_FLUSH_MAX 5000 /* msec */
210#define IWL_RATE_FLUSH_MIN 50 /* msec */
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800211#define IWL_AVERAGE_PACKETS 1500
Zhu Yib481de92007-09-25 17:54:57 -0700212
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800213static void iwl3945_bg_rate_scale_flush(unsigned long data)
Zhu Yib481de92007-09-25 17:54:57 -0700214{
Tomas Winklerfa254a62008-01-27 20:41:38 -0800215 struct iwl3945_rs_sta *rs_sta = (void *)data;
Zhu Yib481de92007-09-25 17:54:57 -0700216 int unflushed = 0;
217 unsigned long flags;
218 u32 packet_count, duration, pps;
219
220 IWL_DEBUG_RATE("enter\n");
221
Tomas Winklerfa254a62008-01-27 20:41:38 -0800222 unflushed = iwl3945_rate_scale_flush_windows(rs_sta);
Zhu Yib481de92007-09-25 17:54:57 -0700223
Tomas Winklerfa254a62008-01-27 20:41:38 -0800224 spin_lock_irqsave(&rs_sta->lock, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700225
Zhu Yib481de92007-09-25 17:54:57 -0700226 /* Number of packets Rx'd since last time this timer ran */
Tomas Winklerfa254a62008-01-27 20:41:38 -0800227 packet_count = (rs_sta->tx_packets - rs_sta->last_tx_packets) + 1;
Zhu Yib481de92007-09-25 17:54:57 -0700228
Tomas Winklerfa254a62008-01-27 20:41:38 -0800229 rs_sta->last_tx_packets = rs_sta->tx_packets + 1;
Zhu Yib481de92007-09-25 17:54:57 -0700230
231 if (unflushed) {
232 duration =
Tomas Winklerfa254a62008-01-27 20:41:38 -0800233 jiffies_to_msecs(jiffies - rs_sta->last_partial_flush);
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) {
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800245 duration = (IWL_AVERAGE_PACKETS * 1000) / pps;
Zhu Yib481de92007-09-25 17:54:57 -0700246 if (duration < IWL_RATE_FLUSH_MIN)
247 duration = IWL_RATE_FLUSH_MIN;
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800248 else if (duration > IWL_RATE_FLUSH_MAX)
249 duration = IWL_RATE_FLUSH_MAX;
Zhu Yib481de92007-09-25 17:54:57 -0700250 } else
251 duration = IWL_RATE_FLUSH_MAX;
252
Tomas Winklerfa254a62008-01-27 20:41:38 -0800253 rs_sta->flush_time = msecs_to_jiffies(duration);
Zhu Yib481de92007-09-25 17:54:57 -0700254
255 IWL_DEBUG_RATE("new flush period: %d msec ave %d\n",
256 duration, packet_count);
257
Tomas Winklerfa254a62008-01-27 20:41:38 -0800258 mod_timer(&rs_sta->rate_scale_flush, jiffies +
259 rs_sta->flush_time);
Zhu Yib481de92007-09-25 17:54:57 -0700260
Tomas Winklerfa254a62008-01-27 20:41:38 -0800261 rs_sta->last_partial_flush = jiffies;
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800262 } else {
263 rs_sta->flush_time = IWL_RATE_FLUSH;
264 rs_sta->flush_pending = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700265 }
Zhu Yib481de92007-09-25 17:54:57 -0700266 /* If there weren't any unflushed entries, we don't schedule the timer
267 * to run again */
268
Tomas Winklerfa254a62008-01-27 20:41:38 -0800269 rs_sta->last_flush = jiffies;
Zhu Yib481de92007-09-25 17:54:57 -0700270
Tomas Winklerfa254a62008-01-27 20:41:38 -0800271 spin_unlock_irqrestore(&rs_sta->lock, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700272
273 IWL_DEBUG_RATE("leave\n");
274}
275
276/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800277 * iwl3945_collect_tx_data - Update the success/failure sliding window
Zhu Yib481de92007-09-25 17:54:57 -0700278 *
279 * We keep a sliding window of the last 64 packets transmitted
280 * at this rate. window->data contains the bitmask of successful
281 * packets.
282 */
Tomas Winklerfa254a62008-01-27 20:41:38 -0800283static void iwl3945_collect_tx_data(struct iwl3945_rs_sta *rs_sta,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800284 struct iwl3945_rate_scale_data *window,
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800285 int success, int retries, int index)
Zhu Yib481de92007-09-25 17:54:57 -0700286{
287 unsigned long flags;
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800288 s32 fail_count;
Zhu Yib481de92007-09-25 17:54:57 -0700289
290 if (!retries) {
291 IWL_DEBUG_RATE("leave: retries == 0 -- should be at least 1\n");
292 return;
293 }
294
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800295 spin_lock_irqsave(&rs_sta->lock, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700296 while (retries--) {
Zhu Yib481de92007-09-25 17:54:57 -0700297
298 /* If we have filled up the window then subtract one from the
299 * success counter if the high-bit is counting toward
300 * success */
301 if (window->counter == IWL_RATE_MAX_WINDOW) {
302 if (window->data & (1ULL << (IWL_RATE_MAX_WINDOW - 1)))
303 window->success_counter--;
304 } else
305 window->counter++;
306
307 /* Slide the window to the left one bit */
308 window->data = (window->data << 1);
309
310 /* If this packet was a success then set the low bit high */
311 if (success) {
312 window->success_counter++;
313 window->data |= 1;
314 }
315
316 /* window->counter can't be 0 -- it is either >0 or
317 * IWL_RATE_MAX_WINDOW */
318 window->success_ratio = 12800 * window->success_counter /
319 window->counter;
320
321 /* Tag this window as having been updated */
322 window->stamp = jiffies;
323
Zhu Yib481de92007-09-25 17:54:57 -0700324 }
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800325
326 fail_count = window->counter - window->success_counter;
327 if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
328 (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
329 window->average_tpt = ((window->success_ratio *
330 rs_sta->expected_tpt[index] + 64) / 128);
331 else
332 window->average_tpt = IWL_INV_TPT;
333
334 spin_unlock_irqrestore(&rs_sta->lock, flags);
335
Zhu Yib481de92007-09-25 17:54:57 -0700336}
337
Abbas, Mohamed72627962008-12-05 07:58:37 -0800338static void rs_rate_init(void *priv_r, struct ieee80211_supported_band *sband,
Johannes Berg4b7679a2008-09-18 18:14:18 +0200339 struct ieee80211_sta *sta, void *priv_sta)
Zhu Yib481de92007-09-25 17:54:57 -0700340{
Johannes Berg4b7679a2008-09-18 18:14:18 +0200341 struct iwl3945_rs_sta *rs_sta = priv_sta;
Abbas, Mohamed72627962008-12-05 07:58:37 -0800342 struct iwl3945_priv *priv = (struct iwl3945_priv *)priv_r;
Zhu Yib481de92007-09-25 17:54:57 -0700343 int i;
344
345 IWL_DEBUG_RATE("enter\n");
346
347 /* TODO: what is a good starting rate for STA? About middle? Maybe not
348 * the lowest or the highest rate.. Could consider using RSSI from
349 * previous packets? Need to have IEEE 802.1X auth succeed immediately
350 * after assoc.. */
351
Abbas, Mohamed72627962008-12-05 07:58:37 -0800352 for (i = sband->n_bitrates - 1; i >= 0; i--) {
Johannes Berg4b7679a2008-09-18 18:14:18 +0200353 if (sta->supp_rates[sband->band] & (1 << i)) {
Johannes Bergae17e982008-09-11 03:04:36 +0200354 rs_sta->last_txrate_idx = i;
Zhu Yib481de92007-09-25 17:54:57 -0700355 break;
356 }
357 }
358
Abbas, Mohamed72627962008-12-05 07:58:37 -0800359 priv->sta_supp_rates = sta->supp_rates[sband->band];
Johannes Berg8318d782008-01-24 19:38:38 +0100360 /* For 5 GHz band it start at IWL_FIRST_OFDM_RATE */
Abbas, Mohamed72627962008-12-05 07:58:37 -0800361 if (sband->band == IEEE80211_BAND_5GHZ) {
Johannes Bergb7e35002008-09-11 02:22:58 +0200362 rs_sta->last_txrate_idx += IWL_FIRST_OFDM_RATE;
Abbas, Mohamed72627962008-12-05 07:58:37 -0800363 priv->sta_supp_rates = priv->sta_supp_rates <<
364 IWL_FIRST_OFDM_RATE;
365 }
366
Mohamed Abbas14577f22007-11-12 11:37:42 +0800367
Zhu Yib481de92007-09-25 17:54:57 -0700368 IWL_DEBUG_RATE("leave\n");
369}
370
Johannes Berg4b7679a2008-09-18 18:14:18 +0200371static void *rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
Zhu Yib481de92007-09-25 17:54:57 -0700372{
Johannes Berg4b7679a2008-09-18 18:14:18 +0200373 return hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -0700374}
375
Ian Schram01ebd062007-10-25 17:15:22 +0800376/* rate scale requires free function to be implemented */
Zhu Yib481de92007-09-25 17:54:57 -0700377static void rs_free(void *priv)
378{
379 return;
380}
Johannes Berg4b7679a2008-09-18 18:14:18 +0200381
Johannes Berg4b7679a2008-09-18 18:14:18 +0200382static void *rs_alloc_sta(void *priv, struct ieee80211_sta *sta, gfp_t gfp)
Zhu Yib481de92007-09-25 17:54:57 -0700383{
Tomas Winklerfa254a62008-01-27 20:41:38 -0800384 struct iwl3945_rs_sta *rs_sta;
Johannes Berg4b7679a2008-09-18 18:14:18 +0200385 struct iwl3945_sta_priv *psta = (void *) sta->drv_priv;
Zhu Yib481de92007-09-25 17:54:57 -0700386 int i;
387
Johannes Berg4b7679a2008-09-18 18:14:18 +0200388 /*
389 * XXX: If it's using sta->drv_priv anyway, it might
390 * as well just put all the information there.
391 */
392
Zhu Yib481de92007-09-25 17:54:57 -0700393 IWL_DEBUG_RATE("enter\n");
394
Tomas Winklerfa254a62008-01-27 20:41:38 -0800395 rs_sta = kzalloc(sizeof(struct iwl3945_rs_sta), gfp);
396 if (!rs_sta) {
Zhu Yib481de92007-09-25 17:54:57 -0700397 IWL_DEBUG_RATE("leave: ENOMEM\n");
398 return NULL;
399 }
400
Johannes Berg4b7679a2008-09-18 18:14:18 +0200401 psta->rs_sta = rs_sta;
402
Tomas Winklerfa254a62008-01-27 20:41:38 -0800403 spin_lock_init(&rs_sta->lock);
Zhu Yib481de92007-09-25 17:54:57 -0700404
Tomas Winklerfa254a62008-01-27 20:41:38 -0800405 rs_sta->start_rate = IWL_RATE_INVALID;
Zhu Yib481de92007-09-25 17:54:57 -0700406
407 /* default to just 802.11b */
Tomas Winklerfa254a62008-01-27 20:41:38 -0800408 rs_sta->expected_tpt = iwl3945_expected_tpt_b;
Zhu Yib481de92007-09-25 17:54:57 -0700409
Tomas Winklerfa254a62008-01-27 20:41:38 -0800410 rs_sta->last_partial_flush = jiffies;
411 rs_sta->last_flush = jiffies;
412 rs_sta->flush_time = IWL_RATE_FLUSH;
413 rs_sta->last_tx_packets = 0;
414 rs_sta->ibss_sta_added = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700415
Tomas Winklerfa254a62008-01-27 20:41:38 -0800416 init_timer(&rs_sta->rate_scale_flush);
417 rs_sta->rate_scale_flush.data = (unsigned long)rs_sta;
418 rs_sta->rate_scale_flush.function = &iwl3945_bg_rate_scale_flush;
Zhu Yib481de92007-09-25 17:54:57 -0700419
420 for (i = 0; i < IWL_RATE_COUNT; i++)
Tomas Winklerfa254a62008-01-27 20:41:38 -0800421 iwl3945_clear_window(&rs_sta->win[i]);
Zhu Yib481de92007-09-25 17:54:57 -0700422
423 IWL_DEBUG_RATE("leave\n");
424
Tomas Winklerfa254a62008-01-27 20:41:38 -0800425 return rs_sta;
Zhu Yib481de92007-09-25 17:54:57 -0700426}
427
Johannes Berg4b7679a2008-09-18 18:14:18 +0200428static void rs_free_sta(void *priv, struct ieee80211_sta *sta,
429 void *priv_sta)
Zhu Yib481de92007-09-25 17:54:57 -0700430{
Johannes Berg4b7679a2008-09-18 18:14:18 +0200431 struct iwl3945_sta_priv *psta = (void *) sta->drv_priv;
Tomas Winklerfa254a62008-01-27 20:41:38 -0800432 struct iwl3945_rs_sta *rs_sta = priv_sta;
Zhu Yib481de92007-09-25 17:54:57 -0700433
Johannes Berg4b7679a2008-09-18 18:14:18 +0200434 psta->rs_sta = NULL;
435
Zhu Yib481de92007-09-25 17:54:57 -0700436 IWL_DEBUG_RATE("enter\n");
Tomas Winklerfa254a62008-01-27 20:41:38 -0800437 del_timer_sync(&rs_sta->rate_scale_flush);
438 kfree(rs_sta);
Zhu Yib481de92007-09-25 17:54:57 -0700439 IWL_DEBUG_RATE("leave\n");
440}
441
Mohamed Abbas14577f22007-11-12 11:37:42 +0800442
Zhu Yib481de92007-09-25 17:54:57 -0700443/**
444 * rs_tx_status - Update rate control values based on Tx results
445 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800446 * NOTE: Uses iwl3945_priv->retry_rate for the # of retries attempted by
Zhu Yib481de92007-09-25 17:54:57 -0700447 * the hardware for each rate.
448 */
Johannes Berg4b7679a2008-09-18 18:14:18 +0200449static void rs_tx_status(void *priv_rate, struct ieee80211_supported_band *sband,
450 struct ieee80211_sta *sta, void *priv_sta,
Johannes Berge039fa42008-05-15 12:55:29 +0200451 struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -0700452{
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800453 s8 retries = 0, current_count;
Zhu Yib481de92007-09-25 17:54:57 -0700454 int scale_rate_index, first_index, last_index;
455 unsigned long flags;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800456 struct iwl3945_priv *priv = (struct iwl3945_priv *)priv_rate;
Johannes Berg4b7679a2008-09-18 18:14:18 +0200457 struct iwl3945_rs_sta *rs_sta = priv_sta;
Johannes Berge039fa42008-05-15 12:55:29 +0200458 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Johannes Berg8318d782008-01-24 19:38:38 +0100459
Zhu Yib481de92007-09-25 17:54:57 -0700460 IWL_DEBUG_RATE("enter\n");
461
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800462 retries = info->status.rates[0].count;
Johannes Berge6a98542008-10-21 12:40:02 +0200463
464 first_index = sband->bitrates[info->status.rates[0].idx].hw_value;
Zhu Yib481de92007-09-25 17:54:57 -0700465 if ((first_index < 0) || (first_index >= IWL_RATE_COUNT)) {
Johannes Berg8318d782008-01-24 19:38:38 +0100466 IWL_DEBUG_RATE("leave: Rate out of bounds: %d\n", first_index);
Zhu Yib481de92007-09-25 17:54:57 -0700467 return;
468 }
469
Johannes Berg4b7679a2008-09-18 18:14:18 +0200470 if (!priv_sta) {
Zhu Yib481de92007-09-25 17:54:57 -0700471 IWL_DEBUG_RATE("leave: No STA priv data to update!\n");
472 return;
473 }
474
Tomas Winklerfa254a62008-01-27 20:41:38 -0800475 rs_sta->tx_packets++;
Zhu Yib481de92007-09-25 17:54:57 -0700476
477 scale_rate_index = first_index;
478 last_index = first_index;
479
480 /*
481 * Update the window for each rate. We determine which rates
482 * were Tx'd based on the total number of retries vs. the number
483 * of retries configured for each rate -- currently set to the
484 * priv value 'retry_rate' vs. rate specific
485 *
486 * On exit from this while loop last_index indicates the rate
487 * at which the frame was finally transmitted (or failed if no
488 * ACK)
489 */
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800490 while (retries > 1) {
491 if ((retries - 1) < priv->retry_rate) {
492 current_count = (retries - 1);
Zhu Yib481de92007-09-25 17:54:57 -0700493 last_index = scale_rate_index;
494 } else {
495 current_count = priv->retry_rate;
Johannes Berge6a98542008-10-21 12:40:02 +0200496 last_index = iwl3945_rs_next_rate(priv,
Mohamed Abbas14577f22007-11-12 11:37:42 +0800497 scale_rate_index);
Zhu Yib481de92007-09-25 17:54:57 -0700498 }
499
500 /* Update this rate accounting for as many retries
501 * as was used for it (per current_count) */
Tomas Winklerfa254a62008-01-27 20:41:38 -0800502 iwl3945_collect_tx_data(rs_sta,
503 &rs_sta->win[scale_rate_index],
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800504 0, current_count, scale_rate_index);
Zhu Yib481de92007-09-25 17:54:57 -0700505 IWL_DEBUG_RATE("Update rate %d for %d retries.\n",
506 scale_rate_index, current_count);
507
508 retries -= current_count;
509
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800510 scale_rate_index = last_index;
Zhu Yib481de92007-09-25 17:54:57 -0700511 }
512
Mohamed Abbas14577f22007-11-12 11:37:42 +0800513
Zhu Yib481de92007-09-25 17:54:57 -0700514 /* Update the last index window with success/failure based on ACK */
515 IWL_DEBUG_RATE("Update rate %d with %s.\n",
516 last_index,
Johannes Berge039fa42008-05-15 12:55:29 +0200517 (info->flags & IEEE80211_TX_STAT_ACK) ?
Zhu Yib481de92007-09-25 17:54:57 -0700518 "success" : "failure");
Tomas Winklerfa254a62008-01-27 20:41:38 -0800519 iwl3945_collect_tx_data(rs_sta,
520 &rs_sta->win[last_index],
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800521 info->flags & IEEE80211_TX_STAT_ACK, 1, last_index);
Zhu Yib481de92007-09-25 17:54:57 -0700522
523 /* We updated the rate scale window -- if its been more than
524 * flush_time since the last run, schedule the flush
525 * again */
Tomas Winklerfa254a62008-01-27 20:41:38 -0800526 spin_lock_irqsave(&rs_sta->lock, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700527
Tomas Winklerfa254a62008-01-27 20:41:38 -0800528 if (!rs_sta->flush_pending &&
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800529 time_after(jiffies, rs_sta->last_flush +
Tomas Winklerfa254a62008-01-27 20:41:38 -0800530 rs_sta->flush_time)) {
Zhu Yib481de92007-09-25 17:54:57 -0700531
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800532 rs_sta->last_partial_flush = jiffies;
Tomas Winklerfa254a62008-01-27 20:41:38 -0800533 rs_sta->flush_pending = 1;
534 mod_timer(&rs_sta->rate_scale_flush,
535 jiffies + rs_sta->flush_time);
Zhu Yib481de92007-09-25 17:54:57 -0700536 }
537
Tomas Winklerfa254a62008-01-27 20:41:38 -0800538 spin_unlock_irqrestore(&rs_sta->lock, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700539
Zhu Yib481de92007-09-25 17:54:57 -0700540 IWL_DEBUG_RATE("leave\n");
541
542 return;
543}
544
Tomas Winklerfa254a62008-01-27 20:41:38 -0800545static u16 iwl3945_get_adjacent_rate(struct iwl3945_rs_sta *rs_sta,
Johannes Berg8318d782008-01-24 19:38:38 +0100546 u8 index, u16 rate_mask, enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -0700547{
548 u8 high = IWL_RATE_INVALID;
549 u8 low = IWL_RATE_INVALID;
550
Ian Schram01ebd062007-10-25 17:15:22 +0800551 /* 802.11A walks to the next literal adjacent rate in
Zhu Yib481de92007-09-25 17:54:57 -0700552 * the rate table */
Johannes Berg8318d782008-01-24 19:38:38 +0100553 if (unlikely(band == IEEE80211_BAND_5GHZ)) {
Zhu Yib481de92007-09-25 17:54:57 -0700554 int i;
555 u32 mask;
556
557 /* Find the previous rate that is in the rate mask */
558 i = index - 1;
559 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 */
567 i = index + 1;
568 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
569 if (rate_mask & mask) {
570 high = i;
571 break;
572 }
573 }
574
575 return (high << 8) | low;
576 }
577
578 low = index;
579 while (low != IWL_RATE_INVALID) {
Tomas Winklerfa254a62008-01-27 20:41:38 -0800580 if (rs_sta->tgg)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800581 low = iwl3945_rates[low].prev_rs_tgg;
Zhu Yib481de92007-09-25 17:54:57 -0700582 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800583 low = iwl3945_rates[low].prev_rs;
Zhu Yib481de92007-09-25 17:54:57 -0700584 if (low == IWL_RATE_INVALID)
585 break;
586 if (rate_mask & (1 << low))
587 break;
588 IWL_DEBUG_RATE("Skipping masked lower rate: %d\n", low);
589 }
590
591 high = index;
592 while (high != IWL_RATE_INVALID) {
Tomas Winklerfa254a62008-01-27 20:41:38 -0800593 if (rs_sta->tgg)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800594 high = iwl3945_rates[high].next_rs_tgg;
Zhu Yib481de92007-09-25 17:54:57 -0700595 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800596 high = iwl3945_rates[high].next_rs;
Zhu Yib481de92007-09-25 17:54:57 -0700597 if (high == IWL_RATE_INVALID)
598 break;
599 if (rate_mask & (1 << high))
600 break;
601 IWL_DEBUG_RATE("Skipping masked higher rate: %d\n", high);
602 }
603
604 return (high << 8) | low;
605}
606
607/**
608 * rs_get_rate - find the rate for the requested packet
609 *
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 *
619 * As such, we can't convert the index obtained below into the hw_mode's
620 * rate table and must reference the driver allocated rate table
621 *
622 */
Johannes Berge6a98542008-10-21 12:40:02 +0200623static void rs_get_rate(void *priv_r, struct ieee80211_sta *sta,
624 void *priv_sta, struct ieee80211_tx_rate_control *txrc)
Zhu Yib481de92007-09-25 17:54:57 -0700625{
Johannes Berge6a98542008-10-21 12:40:02 +0200626 struct ieee80211_supported_band *sband = txrc->sband;
627 struct sk_buff *skb = txrc->skb;
Zhu Yib481de92007-09-25 17:54:57 -0700628 u8 low = IWL_RATE_INVALID;
629 u8 high = IWL_RATE_INVALID;
630 u16 high_low;
631 int index;
Johannes Berg4b7679a2008-09-18 18:14:18 +0200632 struct iwl3945_rs_sta *rs_sta = priv_sta;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800633 struct iwl3945_rate_scale_data *window = NULL;
Tomas Winkler28447f32008-03-06 17:36:54 -0800634 int current_tpt = IWL_INV_TPT;
635 int low_tpt = IWL_INV_TPT;
636 int high_tpt = IWL_INV_TPT;
Zhu Yib481de92007-09-25 17:54:57 -0700637 u32 fail_count;
638 s8 scale_action = 0;
639 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -0700640 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Abbas, Mohamedc338ba32009-01-21 10:58:02 -0800641 u16 fc;
642 u16 rate_mask = 0;
Johannes Berg4b7679a2008-09-18 18:14:18 +0200643 struct iwl3945_priv *priv = (struct iwl3945_priv *)priv_r;
Johannes Berge6a98542008-10-21 12:40:02 +0200644 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Zhu Yib481de92007-09-25 17:54:57 -0700645
646 IWL_DEBUG_RATE("enter\n");
647
Abbas, Mohamedc338ba32009-01-21 10:58:02 -0800648 if (sta)
649 rate_mask = sta->supp_rates[sband->band];
650
Michael Wu2bc454b2007-12-25 19:33:16 -0500651 /* Send management frames and broadcast/multicast data using lowest
652 * rate. */
653 fc = le16_to_cpu(hdr->frame_control);
654 if ((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA ||
655 is_multicast_ether_addr(hdr->addr1) ||
Johannes Berg4b7679a2008-09-18 18:14:18 +0200656 !sta || !priv_sta) {
Zhu Yib481de92007-09-25 17:54:57 -0700657 IWL_DEBUG_RATE("leave: No STA priv data to update!\n");
Abbas, Mohamedc338ba32009-01-21 10:58:02 -0800658 if (!rate_mask)
659 info->control.rates[0].idx =
660 rate_lowest_index(sband, NULL);
661 else
662 info->control.rates[0].idx =
663 rate_lowest_index(sband, sta);
Mattias Nissler1abbe492007-12-20 13:50:07 +0100664 return;
Zhu Yib481de92007-09-25 17:54:57 -0700665 }
666
Johannes Bergb7e35002008-09-11 02:22:58 +0200667 index = min(rs_sta->last_txrate_idx & 0xffff, IWL_RATE_COUNT - 1);
Mohamed Abbas14577f22007-11-12 11:37:42 +0800668
Tomas Winkler28447f32008-03-06 17:36:54 -0800669 if (sband->band == IEEE80211_BAND_5GHZ)
Mohamed Abbas14577f22007-11-12 11:37:42 +0800670 rate_mask = rate_mask << IWL_FIRST_OFDM_RATE;
Zhu Yib481de92007-09-25 17:54:57 -0700671
Johannes Berg05c914f2008-09-11 00:01:58 +0200672 if ((priv->iw_mode == NL80211_IFTYPE_ADHOC) &&
Tomas Winklerfa254a62008-01-27 20:41:38 -0800673 !rs_sta->ibss_sta_added) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800674 u8 sta_id = iwl3945_hw_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -0700675
676 if (sta_id == IWL_INVALID_STATION) {
Johannes Berge1749612008-10-27 15:59:26 -0700677 IWL_DEBUG_RATE("LQ: ADD station %pm\n",
678 hdr->addr1);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800679 sta_id = iwl3945_add_station(priv,
Zhu Yib481de92007-09-25 17:54:57 -0700680 hdr->addr1, 0, CMD_ASYNC);
681 }
682 if (sta_id != IWL_INVALID_STATION)
Tomas Winklerfa254a62008-01-27 20:41:38 -0800683 rs_sta->ibss_sta_added = 1;
Zhu Yib481de92007-09-25 17:54:57 -0700684 }
685
Tomas Winklerfa254a62008-01-27 20:41:38 -0800686 spin_lock_irqsave(&rs_sta->lock, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700687
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800688 /* for recent assoc, choose best rate regarding
689 * to rssi value
690 */
Tomas Winklerfa254a62008-01-27 20:41:38 -0800691 if (rs_sta->start_rate != IWL_RATE_INVALID) {
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800692 if (rs_sta->start_rate < index &&
693 (rate_mask & (1 << rs_sta->start_rate)))
694 index = rs_sta->start_rate;
Tomas Winklerfa254a62008-01-27 20:41:38 -0800695 rs_sta->start_rate = IWL_RATE_INVALID;
Zhu Yib481de92007-09-25 17:54:57 -0700696 }
697
Tomas Winklerfa254a62008-01-27 20:41:38 -0800698 window = &(rs_sta->win[index]);
Zhu Yib481de92007-09-25 17:54:57 -0700699
700 fail_count = window->counter - window->success_counter;
701
702 if (((fail_count <= IWL_RATE_MIN_FAILURE_TH) &&
703 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH))) {
Tomas Winklerfa254a62008-01-27 20:41:38 -0800704 spin_unlock_irqrestore(&rs_sta->lock, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700705
706 IWL_DEBUG_RATE("Invalid average_tpt on rate %d: "
707 "counter: %d, success_counter: %d, "
708 "expected_tpt is %sNULL\n",
709 index,
710 window->counter,
711 window->success_counter,
Tomas Winklerfa254a62008-01-27 20:41:38 -0800712 rs_sta->expected_tpt ? "not " : "");
Zhu Yib481de92007-09-25 17:54:57 -0700713 goto out;
714
715 }
716
Zhu Yib481de92007-09-25 17:54:57 -0700717 current_tpt = window->average_tpt;
718
Tomas Winklerfa254a62008-01-27 20:41:38 -0800719 high_low = iwl3945_get_adjacent_rate(rs_sta, index, rate_mask,
Tomas Winkler28447f32008-03-06 17:36:54 -0800720 sband->band);
Zhu Yib481de92007-09-25 17:54:57 -0700721 low = high_low & 0xff;
722 high = (high_low >> 8) & 0xff;
723
724 if (low != IWL_RATE_INVALID)
Tomas Winklerfa254a62008-01-27 20:41:38 -0800725 low_tpt = rs_sta->win[low].average_tpt;
Zhu Yib481de92007-09-25 17:54:57 -0700726
727 if (high != IWL_RATE_INVALID)
Tomas Winklerfa254a62008-01-27 20:41:38 -0800728 high_tpt = rs_sta->win[high].average_tpt;
Zhu Yib481de92007-09-25 17:54:57 -0700729
Tomas Winklerfa254a62008-01-27 20:41:38 -0800730 spin_unlock_irqrestore(&rs_sta->lock, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700731
732 scale_action = 1;
733
734 if ((window->success_ratio < IWL_RATE_DECREASE_TH) || !current_tpt) {
735 IWL_DEBUG_RATE("decrease rate because of low success_ratio\n");
736 scale_action = -1;
Tomas Winkler28447f32008-03-06 17:36:54 -0800737 } else if ((low_tpt == IWL_INV_TPT) && (high_tpt == IWL_INV_TPT))
Zhu Yib481de92007-09-25 17:54:57 -0700738 scale_action = 1;
Tomas Winkler28447f32008-03-06 17:36:54 -0800739 else if ((low_tpt != IWL_INV_TPT) && (high_tpt != IWL_INV_TPT) &&
740 (low_tpt < current_tpt) && (high_tpt < current_tpt)) {
Zhu Yib481de92007-09-25 17:54:57 -0700741 IWL_DEBUG_RATE("No action -- low [%d] & high [%d] < "
742 "current_tpt [%d]\n",
743 low_tpt, high_tpt, current_tpt);
744 scale_action = 0;
745 } else {
Tomas Winkler28447f32008-03-06 17:36:54 -0800746 if (high_tpt != IWL_INV_TPT) {
Zhu Yib481de92007-09-25 17:54:57 -0700747 if (high_tpt > current_tpt)
748 scale_action = 1;
749 else {
750 IWL_DEBUG_RATE
751 ("decrease rate because of high tpt\n");
752 scale_action = -1;
753 }
Tomas Winkler28447f32008-03-06 17:36:54 -0800754 } else if (low_tpt != IWL_INV_TPT) {
Zhu Yib481de92007-09-25 17:54:57 -0700755 if (low_tpt > current_tpt) {
756 IWL_DEBUG_RATE
757 ("decrease rate because of low tpt\n");
758 scale_action = -1;
759 } else
760 scale_action = 1;
761 }
762 }
763
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800764 if (scale_action == -1) {
765 if (window->success_ratio > IWL_SUCCESS_DOWN_TH)
766 scale_action = 0;
767 } else if (scale_action == 1) {
768 if (window->success_ratio < IWL_SUCCESS_UP_TH) {
769 IWL_DEBUG_RATE("No action -- success_ratio [%d] < "
770 "SUCCESS UP\n", window->success_ratio);
771 scale_action = 0;
772 }
Zhu Yib481de92007-09-25 17:54:57 -0700773 }
774
775 switch (scale_action) {
776 case -1:
777 if (low != IWL_RATE_INVALID)
778 index = low;
779 break;
780
781 case 1:
782 if (high != IWL_RATE_INVALID)
783 index = high;
784
785 break;
786
787 case 0:
788 default:
789 break;
790 }
791
792 IWL_DEBUG_RATE("Selected %d (action %d) - low %d high %d\n",
793 index, scale_action, low, high);
794
795 out:
796
Johannes Bergb7e35002008-09-11 02:22:58 +0200797 rs_sta->last_txrate_idx = index;
Tomas Winkler28447f32008-03-06 17:36:54 -0800798 if (sband->band == IEEE80211_BAND_5GHZ)
Johannes Berge6a98542008-10-21 12:40:02 +0200799 info->control.rates[0].idx = rs_sta->last_txrate_idx -
800 IWL_FIRST_OFDM_RATE;
Mohamed Abbas14577f22007-11-12 11:37:42 +0800801 else
Johannes Berge6a98542008-10-21 12:40:02 +0200802 info->control.rates[0].idx = rs_sta->last_txrate_idx;
Mohamed Abbas14577f22007-11-12 11:37:42 +0800803
Zhu Yib481de92007-09-25 17:54:57 -0700804 IWL_DEBUG_RATE("leave: %d\n", index);
Zhu Yib481de92007-09-25 17:54:57 -0700805}
806
Abbas, Mohamedc30e30e2008-12-02 12:14:02 -0800807#ifdef CONFIG_MAC80211_DEBUGFS
808static int iwl3945_open_file_generic(struct inode *inode, struct file *file)
809{
810 file->private_data = inode->i_private;
811 return 0;
812}
813
814static ssize_t iwl3945_sta_dbgfs_stats_table_read(struct file *file,
815 char __user *user_buf,
816 size_t count, loff_t *ppos)
817{
818 char buff[1024];
819 int desc = 0;
820 int j;
821 struct iwl3945_rs_sta *lq_sta = file->private_data;
822
823 desc += sprintf(buff + desc, "tx packets=%d last rate index=%d\n"
824 "rate=0x%X flush time %d\n",
825 lq_sta->tx_packets,
826 lq_sta->last_txrate_idx,
827 lq_sta->start_rate, jiffies_to_msecs(lq_sta->flush_time));
828 for (j = 0; j < IWL_RATE_COUNT; j++) {
829 desc += sprintf(buff+desc,
830 "counter=%d success=%d %%=%d\n",
831 lq_sta->win[j].counter,
832 lq_sta->win[j].success_counter,
833 lq_sta->win[j].success_ratio);
834 }
835 return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
836}
837
838static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
839 .read = iwl3945_sta_dbgfs_stats_table_read,
840 .open = iwl3945_open_file_generic,
841};
842
843static void iwl3945_add_debugfs(void *priv, void *priv_sta,
844 struct dentry *dir)
845{
846 struct iwl3945_rs_sta *lq_sta = priv_sta;
847
848 lq_sta->rs_sta_dbgfs_stats_table_file =
849 debugfs_create_file("rate_stats_table", 0600, dir,
850 lq_sta, &rs_sta_dbgfs_stats_table_ops);
851
852}
853
854static void iwl3945_remove_debugfs(void *priv, void *priv_sta)
855{
856 struct iwl3945_rs_sta *lq_sta = priv_sta;
857 debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
858}
859#endif
860
Zhu Yib481de92007-09-25 17:54:57 -0700861static struct rate_control_ops rs_ops = {
862 .module = NULL,
863 .name = RS_NAME,
864 .tx_status = rs_tx_status,
865 .get_rate = rs_get_rate,
866 .rate_init = rs_rate_init,
Zhu Yib481de92007-09-25 17:54:57 -0700867 .alloc = rs_alloc,
868 .free = rs_free,
869 .alloc_sta = rs_alloc_sta,
870 .free_sta = rs_free_sta,
Abbas, Mohamedc30e30e2008-12-02 12:14:02 -0800871#ifdef CONFIG_MAC80211_DEBUGFS
872 .add_sta_debugfs = iwl3945_add_debugfs,
873 .remove_sta_debugfs = iwl3945_remove_debugfs,
874#endif
875
Zhu Yib481de92007-09-25 17:54:57 -0700876};
877
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800878void iwl3945_rate_scale_init(struct ieee80211_hw *hw, s32 sta_id)
Zhu Yib481de92007-09-25 17:54:57 -0700879{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800880 struct iwl3945_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -0700881 s32 rssi = 0;
882 unsigned long flags;
Tomas Winklerfa254a62008-01-27 20:41:38 -0800883 struct iwl3945_rs_sta *rs_sta;
Johannes Berg4b7679a2008-09-18 18:14:18 +0200884 struct ieee80211_sta *sta;
885 struct iwl3945_sta_priv *psta;
Zhu Yib481de92007-09-25 17:54:57 -0700886
887 IWL_DEBUG_RATE("enter\n");
888
Johannes Bergd0709a62008-02-25 16:27:46 +0100889 rcu_read_lock();
890
Johannes Berg4b7679a2008-09-18 18:14:18 +0200891 sta = ieee80211_find_sta(hw, priv->stations[sta_id].sta.sta.addr);
Johannes Berg525181892008-10-11 01:46:25 +0200892 if (!sta) {
Johannes Bergd0709a62008-02-25 16:27:46 +0100893 rcu_read_unlock();
Zhu Yib481de92007-09-25 17:54:57 -0700894 return;
895 }
896
Johannes Berg525181892008-10-11 01:46:25 +0200897 psta = (void *) sta->drv_priv;
Johannes Berg4b7679a2008-09-18 18:14:18 +0200898 rs_sta = psta->rs_sta;
Zhu Yib481de92007-09-25 17:54:57 -0700899
Tomas Winklerfa254a62008-01-27 20:41:38 -0800900 spin_lock_irqsave(&rs_sta->lock, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700901
Tomas Winklerfa254a62008-01-27 20:41:38 -0800902 rs_sta->tgg = 0;
Johannes Berg8318d782008-01-24 19:38:38 +0100903 switch (priv->band) {
904 case IEEE80211_BAND_2GHZ:
905 /* TODO: this always does G, not a regression */
Zhu Yib481de92007-09-25 17:54:57 -0700906 if (priv->active_rxon.flags & RXON_FLG_TGG_PROTECT_MSK) {
Tomas Winklerfa254a62008-01-27 20:41:38 -0800907 rs_sta->tgg = 1;
908 rs_sta->expected_tpt = iwl3945_expected_tpt_g_prot;
Zhu Yib481de92007-09-25 17:54:57 -0700909 } else
Tomas Winklerfa254a62008-01-27 20:41:38 -0800910 rs_sta->expected_tpt = iwl3945_expected_tpt_g;
Zhu Yib481de92007-09-25 17:54:57 -0700911 break;
912
Johannes Berg8318d782008-01-24 19:38:38 +0100913 case IEEE80211_BAND_5GHZ:
Tomas Winklerfa254a62008-01-27 20:41:38 -0800914 rs_sta->expected_tpt = iwl3945_expected_tpt_a;
Zhu Yib481de92007-09-25 17:54:57 -0700915 break;
Johannes Berg8318d782008-01-24 19:38:38 +0100916 case IEEE80211_NUM_BANDS:
917 BUG();
Zhu Yib481de92007-09-25 17:54:57 -0700918 break;
919 }
920
Tomas Winklerfa254a62008-01-27 20:41:38 -0800921 spin_unlock_irqrestore(&rs_sta->lock, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700922
923 rssi = priv->last_rx_rssi;
924 if (rssi == 0)
925 rssi = IWL_MIN_RSSI_VAL;
926
927 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RATE, "Network RSSI: %d\n", rssi);
928
Johannes Berg8318d782008-01-24 19:38:38 +0100929 rs_sta->start_rate = iwl3945_get_rate_index_by_rssi(rssi, priv->band);
Zhu Yib481de92007-09-25 17:54:57 -0700930
931 IWL_DEBUG_RATE("leave: rssi %d assign rate index: "
Tomas Winklerfa254a62008-01-27 20:41:38 -0800932 "%d (plcp 0x%x)\n", rssi, rs_sta->start_rate,
933 iwl3945_rates[rs_sta->start_rate].plcp);
Johannes Berg525181892008-10-11 01:46:25 +0200934 rcu_read_unlock();
Zhu Yib481de92007-09-25 17:54:57 -0700935}
936
Reinette Chatre897e1cf2008-03-28 16:21:09 -0700937int iwl3945_rate_control_register(void)
Zhu Yib481de92007-09-25 17:54:57 -0700938{
Reinette Chatre897e1cf2008-03-28 16:21:09 -0700939 return ieee80211_rate_control_register(&rs_ops);
Zhu Yib481de92007-09-25 17:54:57 -0700940}
941
Reinette Chatre897e1cf2008-03-28 16:21:09 -0700942void iwl3945_rate_control_unregister(void)
Zhu Yib481de92007-09-25 17:54:57 -0700943{
944 ieee80211_rate_control_unregister(&rs_ops);
945}
946
947