blob: 2da42b89540a8bb3d7082cb3666e6b6580d58642 [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
Tomas Winkler600c0e12008-12-19 10:37:04 +080039#include "iwl-commands.h"
Tomas Winkler69d00d22008-12-19 10:37:02 +080040#include "iwl-3945-commands.h"
Christoph Hellwig5d08cd12007-10-25 17:15:50 +080041#include "iwl-3945.h"
Zhu Yib481de92007-09-25 17:54:57 -070042
43#define RS_NAME "iwl-3945-rs"
44
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080045struct iwl3945_rate_scale_data {
Zhu Yib481de92007-09-25 17:54:57 -070046 u64 data;
47 s32 success_counter;
48 s32 success_ratio;
49 s32 counter;
50 s32 average_tpt;
51 unsigned long stamp;
52};
53
Tomas Winklerfa254a62008-01-27 20:41:38 -080054struct iwl3945_rs_sta {
Zhu Yib481de92007-09-25 17:54:57 -070055 spinlock_t lock;
56 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;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080067 struct iwl3945_rate_scale_data win[IWL_RATE_COUNT];
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
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080076static s32 iwl3945_expected_tpt_g[IWL_RATE_COUNT] = {
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
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080080static s32 iwl3945_expected_tpt_g_prot[IWL_RATE_COUNT] = {
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
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080084static s32 iwl3945_expected_tpt_a[IWL_RATE_COUNT] = {
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
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080088static s32 iwl3945_expected_tpt_b[IWL_RATE_COUNT] = {
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)
124#define IWL_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;
Tomas Winkler28447f32008-03-06 17:36:54 -0800170 window->average_tpt = IWL_INV_TPT;
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;
186
187 /*
188 * For each rate, if we have collected data on that rate
189 * and it has been more than IWL_RATE_WIN_FLUSH
190 * since we flushed, clear out the gathered statistics
191 */
192 for (i = 0; i < IWL_RATE_COUNT; i++) {
Tomas Winklerfa254a62008-01-27 20:41:38 -0800193 if (!rs_sta->win[i].counter)
Zhu Yib481de92007-09-25 17:54:57 -0700194 continue;
195
Tomas Winklerfa254a62008-01-27 20:41:38 -0800196 spin_lock_irqsave(&rs_sta->lock, flags);
197 if (time_after(jiffies, rs_sta->win[i].stamp +
Zhu Yib481de92007-09-25 17:54:57 -0700198 IWL_RATE_WIN_FLUSH)) {
199 IWL_DEBUG_RATE("flushing %d samples of rate "
200 "index %d\n",
Tomas Winklerfa254a62008-01-27 20:41:38 -0800201 rs_sta->win[i].counter, i);
202 iwl3945_clear_window(&rs_sta->win[i]);
Zhu Yib481de92007-09-25 17:54:57 -0700203 } else
204 unflushed++;
Tomas Winklerfa254a62008-01-27 20:41:38 -0800205 spin_unlock_irqrestore(&rs_sta->lock, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700206 }
207
208 return unflushed;
209}
210
211#define IWL_RATE_FLUSH_MAX 5000 /* msec */
212#define IWL_RATE_FLUSH_MIN 50 /* msec */
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800213#define IWL_AVERAGE_PACKETS 1500
Zhu Yib481de92007-09-25 17:54:57 -0700214
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800215static void iwl3945_bg_rate_scale_flush(unsigned long data)
Zhu Yib481de92007-09-25 17:54:57 -0700216{
Tomas Winklerfa254a62008-01-27 20:41:38 -0800217 struct iwl3945_rs_sta *rs_sta = (void *)data;
Zhu Yib481de92007-09-25 17:54:57 -0700218 int unflushed = 0;
219 unsigned long flags;
220 u32 packet_count, duration, pps;
221
222 IWL_DEBUG_RATE("enter\n");
223
Tomas Winklerfa254a62008-01-27 20:41:38 -0800224 unflushed = iwl3945_rate_scale_flush_windows(rs_sta);
Zhu Yib481de92007-09-25 17:54:57 -0700225
Tomas Winklerfa254a62008-01-27 20:41:38 -0800226 spin_lock_irqsave(&rs_sta->lock, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700227
Zhu Yib481de92007-09-25 17:54:57 -0700228 /* Number of packets Rx'd since last time this timer ran */
Tomas Winklerfa254a62008-01-27 20:41:38 -0800229 packet_count = (rs_sta->tx_packets - rs_sta->last_tx_packets) + 1;
Zhu Yib481de92007-09-25 17:54:57 -0700230
Tomas Winklerfa254a62008-01-27 20:41:38 -0800231 rs_sta->last_tx_packets = rs_sta->tx_packets + 1;
Zhu Yib481de92007-09-25 17:54:57 -0700232
233 if (unflushed) {
234 duration =
Tomas Winklerfa254a62008-01-27 20:41:38 -0800235 jiffies_to_msecs(jiffies - rs_sta->last_partial_flush);
Zhu Yib481de92007-09-25 17:54:57 -0700236
237 IWL_DEBUG_RATE("Tx'd %d packets in %dms\n",
238 packet_count, duration);
239
240 /* Determine packets per second */
241 if (duration)
242 pps = (packet_count * 1000) / duration;
243 else
244 pps = 0;
245
246 if (pps) {
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800247 duration = (IWL_AVERAGE_PACKETS * 1000) / pps;
Zhu Yib481de92007-09-25 17:54:57 -0700248 if (duration < IWL_RATE_FLUSH_MIN)
249 duration = IWL_RATE_FLUSH_MIN;
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800250 else if (duration > IWL_RATE_FLUSH_MAX)
251 duration = IWL_RATE_FLUSH_MAX;
Zhu Yib481de92007-09-25 17:54:57 -0700252 } else
253 duration = IWL_RATE_FLUSH_MAX;
254
Tomas Winklerfa254a62008-01-27 20:41:38 -0800255 rs_sta->flush_time = msecs_to_jiffies(duration);
Zhu Yib481de92007-09-25 17:54:57 -0700256
257 IWL_DEBUG_RATE("new flush period: %d msec ave %d\n",
258 duration, packet_count);
259
Tomas Winklerfa254a62008-01-27 20:41:38 -0800260 mod_timer(&rs_sta->rate_scale_flush, jiffies +
261 rs_sta->flush_time);
Zhu Yib481de92007-09-25 17:54:57 -0700262
Tomas Winklerfa254a62008-01-27 20:41:38 -0800263 rs_sta->last_partial_flush = jiffies;
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800264 } else {
265 rs_sta->flush_time = IWL_RATE_FLUSH;
266 rs_sta->flush_pending = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700267 }
Zhu Yib481de92007-09-25 17:54:57 -0700268 /* If there weren't any unflushed entries, we don't schedule the timer
269 * to run again */
270
Tomas Winklerfa254a62008-01-27 20:41:38 -0800271 rs_sta->last_flush = jiffies;
Zhu Yib481de92007-09-25 17:54:57 -0700272
Tomas Winklerfa254a62008-01-27 20:41:38 -0800273 spin_unlock_irqrestore(&rs_sta->lock, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700274
275 IWL_DEBUG_RATE("leave\n");
276}
277
278/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800279 * iwl3945_collect_tx_data - Update the success/failure sliding window
Zhu Yib481de92007-09-25 17:54:57 -0700280 *
281 * We keep a sliding window of the last 64 packets transmitted
282 * at this rate. window->data contains the bitmask of successful
283 * packets.
284 */
Tomas Winklerfa254a62008-01-27 20:41:38 -0800285static void iwl3945_collect_tx_data(struct iwl3945_rs_sta *rs_sta,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800286 struct iwl3945_rate_scale_data *window,
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800287 int success, int retries, int index)
Zhu Yib481de92007-09-25 17:54:57 -0700288{
289 unsigned long flags;
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800290 s32 fail_count;
Zhu Yib481de92007-09-25 17:54:57 -0700291
292 if (!retries) {
293 IWL_DEBUG_RATE("leave: retries == 0 -- should be at least 1\n");
294 return;
295 }
296
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800297 spin_lock_irqsave(&rs_sta->lock, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700298 while (retries--) {
Zhu Yib481de92007-09-25 17:54:57 -0700299
300 /* If we have filled up the window then subtract one from the
301 * success counter if the high-bit is counting toward
302 * success */
303 if (window->counter == IWL_RATE_MAX_WINDOW) {
304 if (window->data & (1ULL << (IWL_RATE_MAX_WINDOW - 1)))
305 window->success_counter--;
306 } else
307 window->counter++;
308
309 /* Slide the window to the left one bit */
310 window->data = (window->data << 1);
311
312 /* If this packet was a success then set the low bit high */
313 if (success) {
314 window->success_counter++;
315 window->data |= 1;
316 }
317
318 /* window->counter can't be 0 -- it is either >0 or
319 * IWL_RATE_MAX_WINDOW */
320 window->success_ratio = 12800 * window->success_counter /
321 window->counter;
322
323 /* Tag this window as having been updated */
324 window->stamp = jiffies;
325
Zhu Yib481de92007-09-25 17:54:57 -0700326 }
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800327
328 fail_count = window->counter - window->success_counter;
329 if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
330 (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
331 window->average_tpt = ((window->success_ratio *
332 rs_sta->expected_tpt[index] + 64) / 128);
333 else
334 window->average_tpt = IWL_INV_TPT;
335
336 spin_unlock_irqrestore(&rs_sta->lock, flags);
337
Zhu Yib481de92007-09-25 17:54:57 -0700338}
339
Abbas, Mohamed72627962008-12-05 07:58:37 -0800340static void rs_rate_init(void *priv_r, struct ieee80211_supported_band *sband,
Johannes Berg4b7679a2008-09-18 18:14:18 +0200341 struct ieee80211_sta *sta, void *priv_sta)
Zhu Yib481de92007-09-25 17:54:57 -0700342{
Johannes Berg4b7679a2008-09-18 18:14:18 +0200343 struct iwl3945_rs_sta *rs_sta = priv_sta;
Abbas, Mohamed72627962008-12-05 07:58:37 -0800344 struct iwl3945_priv *priv = (struct iwl3945_priv *)priv_r;
Zhu Yib481de92007-09-25 17:54:57 -0700345 int i;
346
347 IWL_DEBUG_RATE("enter\n");
348
349 /* TODO: what is a good starting rate for STA? About middle? Maybe not
350 * the lowest or the highest rate.. Could consider using RSSI from
351 * previous packets? Need to have IEEE 802.1X auth succeed immediately
352 * after assoc.. */
353
Abbas, Mohamed72627962008-12-05 07:58:37 -0800354 for (i = sband->n_bitrates - 1; i >= 0; i--) {
Johannes Berg4b7679a2008-09-18 18:14:18 +0200355 if (sta->supp_rates[sband->band] & (1 << i)) {
Johannes Bergae17e982008-09-11 03:04:36 +0200356 rs_sta->last_txrate_idx = i;
Zhu Yib481de92007-09-25 17:54:57 -0700357 break;
358 }
359 }
360
Abbas, Mohamed72627962008-12-05 07:58:37 -0800361 priv->sta_supp_rates = sta->supp_rates[sband->band];
Johannes Berg8318d782008-01-24 19:38:38 +0100362 /* For 5 GHz band it start at IWL_FIRST_OFDM_RATE */
Abbas, Mohamed72627962008-12-05 07:58:37 -0800363 if (sband->band == IEEE80211_BAND_5GHZ) {
Johannes Bergb7e35002008-09-11 02:22:58 +0200364 rs_sta->last_txrate_idx += IWL_FIRST_OFDM_RATE;
Abbas, Mohamed72627962008-12-05 07:58:37 -0800365 priv->sta_supp_rates = priv->sta_supp_rates <<
366 IWL_FIRST_OFDM_RATE;
367 }
368
Mohamed Abbas14577f22007-11-12 11:37:42 +0800369
Zhu Yib481de92007-09-25 17:54:57 -0700370 IWL_DEBUG_RATE("leave\n");
371}
372
Johannes Berg4b7679a2008-09-18 18:14:18 +0200373static void *rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
Zhu Yib481de92007-09-25 17:54:57 -0700374{
Johannes Berg4b7679a2008-09-18 18:14:18 +0200375 return hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -0700376}
377
Ian Schram01ebd062007-10-25 17:15:22 +0800378/* rate scale requires free function to be implemented */
Zhu Yib481de92007-09-25 17:54:57 -0700379static void rs_free(void *priv)
380{
381 return;
382}
Johannes Berg4b7679a2008-09-18 18:14:18 +0200383
Johannes Berg4b7679a2008-09-18 18:14:18 +0200384static void *rs_alloc_sta(void *priv, struct ieee80211_sta *sta, gfp_t gfp)
Zhu Yib481de92007-09-25 17:54:57 -0700385{
Tomas Winklerfa254a62008-01-27 20:41:38 -0800386 struct iwl3945_rs_sta *rs_sta;
Johannes Berg4b7679a2008-09-18 18:14:18 +0200387 struct iwl3945_sta_priv *psta = (void *) sta->drv_priv;
Zhu Yib481de92007-09-25 17:54:57 -0700388 int i;
389
Johannes Berg4b7679a2008-09-18 18:14:18 +0200390 /*
391 * XXX: If it's using sta->drv_priv anyway, it might
392 * as well just put all the information there.
393 */
394
Zhu Yib481de92007-09-25 17:54:57 -0700395 IWL_DEBUG_RATE("enter\n");
396
Tomas Winklerfa254a62008-01-27 20:41:38 -0800397 rs_sta = kzalloc(sizeof(struct iwl3945_rs_sta), gfp);
398 if (!rs_sta) {
Zhu Yib481de92007-09-25 17:54:57 -0700399 IWL_DEBUG_RATE("leave: ENOMEM\n");
400 return NULL;
401 }
402
Johannes Berg4b7679a2008-09-18 18:14:18 +0200403 psta->rs_sta = rs_sta;
404
Tomas Winklerfa254a62008-01-27 20:41:38 -0800405 spin_lock_init(&rs_sta->lock);
Zhu Yib481de92007-09-25 17:54:57 -0700406
Tomas Winklerfa254a62008-01-27 20:41:38 -0800407 rs_sta->start_rate = IWL_RATE_INVALID;
Zhu Yib481de92007-09-25 17:54:57 -0700408
409 /* default to just 802.11b */
Tomas Winklerfa254a62008-01-27 20:41:38 -0800410 rs_sta->expected_tpt = iwl3945_expected_tpt_b;
Zhu Yib481de92007-09-25 17:54:57 -0700411
Tomas Winklerfa254a62008-01-27 20:41:38 -0800412 rs_sta->last_partial_flush = jiffies;
413 rs_sta->last_flush = jiffies;
414 rs_sta->flush_time = IWL_RATE_FLUSH;
415 rs_sta->last_tx_packets = 0;
416 rs_sta->ibss_sta_added = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700417
Tomas Winklerfa254a62008-01-27 20:41:38 -0800418 init_timer(&rs_sta->rate_scale_flush);
419 rs_sta->rate_scale_flush.data = (unsigned long)rs_sta;
420 rs_sta->rate_scale_flush.function = &iwl3945_bg_rate_scale_flush;
Zhu Yib481de92007-09-25 17:54:57 -0700421
422 for (i = 0; i < IWL_RATE_COUNT; i++)
Tomas Winklerfa254a62008-01-27 20:41:38 -0800423 iwl3945_clear_window(&rs_sta->win[i]);
Zhu Yib481de92007-09-25 17:54:57 -0700424
425 IWL_DEBUG_RATE("leave\n");
426
Tomas Winklerfa254a62008-01-27 20:41:38 -0800427 return rs_sta;
Zhu Yib481de92007-09-25 17:54:57 -0700428}
429
Johannes Berg4b7679a2008-09-18 18:14:18 +0200430static void rs_free_sta(void *priv, struct ieee80211_sta *sta,
431 void *priv_sta)
Zhu Yib481de92007-09-25 17:54:57 -0700432{
Johannes Berg4b7679a2008-09-18 18:14:18 +0200433 struct iwl3945_sta_priv *psta = (void *) sta->drv_priv;
Tomas Winklerfa254a62008-01-27 20:41:38 -0800434 struct iwl3945_rs_sta *rs_sta = priv_sta;
Zhu Yib481de92007-09-25 17:54:57 -0700435
Johannes Berg4b7679a2008-09-18 18:14:18 +0200436 psta->rs_sta = NULL;
437
Zhu Yib481de92007-09-25 17:54:57 -0700438 IWL_DEBUG_RATE("enter\n");
Tomas Winklerfa254a62008-01-27 20:41:38 -0800439 del_timer_sync(&rs_sta->rate_scale_flush);
440 kfree(rs_sta);
Zhu Yib481de92007-09-25 17:54:57 -0700441 IWL_DEBUG_RATE("leave\n");
442}
443
Mohamed Abbas14577f22007-11-12 11:37:42 +0800444
Zhu Yib481de92007-09-25 17:54:57 -0700445/**
446 * rs_tx_status - Update rate control values based on Tx results
447 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800448 * NOTE: Uses iwl3945_priv->retry_rate for the # of retries attempted by
Zhu Yib481de92007-09-25 17:54:57 -0700449 * the hardware for each rate.
450 */
Johannes Berg4b7679a2008-09-18 18:14:18 +0200451static void rs_tx_status(void *priv_rate, struct ieee80211_supported_band *sband,
452 struct ieee80211_sta *sta, void *priv_sta,
Johannes Berge039fa42008-05-15 12:55:29 +0200453 struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -0700454{
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800455 s8 retries = 0, current_count;
Zhu Yib481de92007-09-25 17:54:57 -0700456 int scale_rate_index, first_index, last_index;
457 unsigned long flags;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800458 struct iwl3945_priv *priv = (struct iwl3945_priv *)priv_rate;
Johannes Berg4b7679a2008-09-18 18:14:18 +0200459 struct iwl3945_rs_sta *rs_sta = priv_sta;
Johannes Berge039fa42008-05-15 12:55:29 +0200460 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Johannes Berg8318d782008-01-24 19:38:38 +0100461
Zhu Yib481de92007-09-25 17:54:57 -0700462 IWL_DEBUG_RATE("enter\n");
463
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800464 retries = info->status.rates[0].count;
Johannes Berge6a98542008-10-21 12:40:02 +0200465
466 first_index = sband->bitrates[info->status.rates[0].idx].hw_value;
Zhu Yib481de92007-09-25 17:54:57 -0700467 if ((first_index < 0) || (first_index >= IWL_RATE_COUNT)) {
Johannes Berg8318d782008-01-24 19:38:38 +0100468 IWL_DEBUG_RATE("leave: Rate out of bounds: %d\n", first_index);
Zhu Yib481de92007-09-25 17:54:57 -0700469 return;
470 }
471
Johannes Berg4b7679a2008-09-18 18:14:18 +0200472 if (!priv_sta) {
Zhu Yib481de92007-09-25 17:54:57 -0700473 IWL_DEBUG_RATE("leave: No STA priv data to update!\n");
474 return;
475 }
476
Tomas Winklerfa254a62008-01-27 20:41:38 -0800477 rs_sta->tx_packets++;
Zhu Yib481de92007-09-25 17:54:57 -0700478
479 scale_rate_index = first_index;
480 last_index = first_index;
481
482 /*
483 * Update the window for each rate. We determine which rates
484 * were Tx'd based on the total number of retries vs. the number
485 * of retries configured for each rate -- currently set to the
486 * priv value 'retry_rate' vs. rate specific
487 *
488 * On exit from this while loop last_index indicates the rate
489 * at which the frame was finally transmitted (or failed if no
490 * ACK)
491 */
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800492 while (retries > 1) {
493 if ((retries - 1) < priv->retry_rate) {
494 current_count = (retries - 1);
Zhu Yib481de92007-09-25 17:54:57 -0700495 last_index = scale_rate_index;
496 } else {
497 current_count = priv->retry_rate;
Johannes Berge6a98542008-10-21 12:40:02 +0200498 last_index = iwl3945_rs_next_rate(priv,
Mohamed Abbas14577f22007-11-12 11:37:42 +0800499 scale_rate_index);
Zhu Yib481de92007-09-25 17:54:57 -0700500 }
501
502 /* Update this rate accounting for as many retries
503 * as was used for it (per current_count) */
Tomas Winklerfa254a62008-01-27 20:41:38 -0800504 iwl3945_collect_tx_data(rs_sta,
505 &rs_sta->win[scale_rate_index],
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800506 0, current_count, scale_rate_index);
Zhu Yib481de92007-09-25 17:54:57 -0700507 IWL_DEBUG_RATE("Update rate %d for %d retries.\n",
508 scale_rate_index, current_count);
509
510 retries -= current_count;
511
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800512 scale_rate_index = last_index;
Zhu Yib481de92007-09-25 17:54:57 -0700513 }
514
Mohamed Abbas14577f22007-11-12 11:37:42 +0800515
Zhu Yib481de92007-09-25 17:54:57 -0700516 /* Update the last index window with success/failure based on ACK */
517 IWL_DEBUG_RATE("Update rate %d with %s.\n",
518 last_index,
Johannes Berge039fa42008-05-15 12:55:29 +0200519 (info->flags & IEEE80211_TX_STAT_ACK) ?
Zhu Yib481de92007-09-25 17:54:57 -0700520 "success" : "failure");
Tomas Winklerfa254a62008-01-27 20:41:38 -0800521 iwl3945_collect_tx_data(rs_sta,
522 &rs_sta->win[last_index],
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800523 info->flags & IEEE80211_TX_STAT_ACK, 1, last_index);
Zhu Yib481de92007-09-25 17:54:57 -0700524
525 /* We updated the rate scale window -- if its been more than
526 * flush_time since the last run, schedule the flush
527 * again */
Tomas Winklerfa254a62008-01-27 20:41:38 -0800528 spin_lock_irqsave(&rs_sta->lock, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700529
Tomas Winklerfa254a62008-01-27 20:41:38 -0800530 if (!rs_sta->flush_pending &&
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800531 time_after(jiffies, rs_sta->last_flush +
Tomas Winklerfa254a62008-01-27 20:41:38 -0800532 rs_sta->flush_time)) {
Zhu Yib481de92007-09-25 17:54:57 -0700533
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800534 rs_sta->last_partial_flush = jiffies;
Tomas Winklerfa254a62008-01-27 20:41:38 -0800535 rs_sta->flush_pending = 1;
536 mod_timer(&rs_sta->rate_scale_flush,
537 jiffies + rs_sta->flush_time);
Zhu Yib481de92007-09-25 17:54:57 -0700538 }
539
Tomas Winklerfa254a62008-01-27 20:41:38 -0800540 spin_unlock_irqrestore(&rs_sta->lock, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700541
Zhu Yib481de92007-09-25 17:54:57 -0700542 IWL_DEBUG_RATE("leave\n");
543
544 return;
545}
546
Tomas Winklerfa254a62008-01-27 20:41:38 -0800547static u16 iwl3945_get_adjacent_rate(struct iwl3945_rs_sta *rs_sta,
Johannes Berg8318d782008-01-24 19:38:38 +0100548 u8 index, u16 rate_mask, enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -0700549{
550 u8 high = IWL_RATE_INVALID;
551 u8 low = IWL_RATE_INVALID;
552
Ian Schram01ebd062007-10-25 17:15:22 +0800553 /* 802.11A walks to the next literal adjacent rate in
Zhu Yib481de92007-09-25 17:54:57 -0700554 * the rate table */
Johannes Berg8318d782008-01-24 19:38:38 +0100555 if (unlikely(band == IEEE80211_BAND_5GHZ)) {
Zhu Yib481de92007-09-25 17:54:57 -0700556 int i;
557 u32 mask;
558
559 /* Find the previous rate that is in the rate mask */
560 i = index - 1;
561 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
562 if (rate_mask & mask) {
563 low = i;
564 break;
565 }
566 }
567
568 /* Find the next rate that is in the rate mask */
569 i = index + 1;
570 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
571 if (rate_mask & mask) {
572 high = i;
573 break;
574 }
575 }
576
577 return (high << 8) | low;
578 }
579
580 low = index;
581 while (low != IWL_RATE_INVALID) {
Tomas Winklerfa254a62008-01-27 20:41:38 -0800582 if (rs_sta->tgg)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800583 low = iwl3945_rates[low].prev_rs_tgg;
Zhu Yib481de92007-09-25 17:54:57 -0700584 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800585 low = iwl3945_rates[low].prev_rs;
Zhu Yib481de92007-09-25 17:54:57 -0700586 if (low == IWL_RATE_INVALID)
587 break;
588 if (rate_mask & (1 << low))
589 break;
590 IWL_DEBUG_RATE("Skipping masked lower rate: %d\n", low);
591 }
592
593 high = index;
594 while (high != IWL_RATE_INVALID) {
Tomas Winklerfa254a62008-01-27 20:41:38 -0800595 if (rs_sta->tgg)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800596 high = iwl3945_rates[high].next_rs_tgg;
Zhu Yib481de92007-09-25 17:54:57 -0700597 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800598 high = iwl3945_rates[high].next_rs;
Zhu Yib481de92007-09-25 17:54:57 -0700599 if (high == IWL_RATE_INVALID)
600 break;
601 if (rate_mask & (1 << high))
602 break;
603 IWL_DEBUG_RATE("Skipping masked higher rate: %d\n", high);
604 }
605
606 return (high << 8) | low;
607}
608
609/**
610 * rs_get_rate - find the rate for the requested packet
611 *
612 * Returns the ieee80211_rate structure allocated by the driver.
613 *
614 * The rate control algorithm has no internal mapping between hw_mode's
615 * rate ordering and the rate ordering used by the rate control algorithm.
616 *
617 * The rate control algorithm uses a single table of rates that goes across
618 * the entire A/B/G spectrum vs. being limited to just one particular
619 * hw_mode.
620 *
621 * As such, we can't convert the index obtained below into the hw_mode's
622 * rate table and must reference the driver allocated rate table
623 *
624 */
Johannes Berge6a98542008-10-21 12:40:02 +0200625static void rs_get_rate(void *priv_r, struct ieee80211_sta *sta,
626 void *priv_sta, struct ieee80211_tx_rate_control *txrc)
Zhu Yib481de92007-09-25 17:54:57 -0700627{
Johannes Berge6a98542008-10-21 12:40:02 +0200628 struct ieee80211_supported_band *sband = txrc->sband;
629 struct sk_buff *skb = txrc->skb;
Zhu Yib481de92007-09-25 17:54:57 -0700630 u8 low = IWL_RATE_INVALID;
631 u8 high = IWL_RATE_INVALID;
632 u16 high_low;
633 int index;
Johannes Berg4b7679a2008-09-18 18:14:18 +0200634 struct iwl3945_rs_sta *rs_sta = priv_sta;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800635 struct iwl3945_rate_scale_data *window = NULL;
Tomas Winkler28447f32008-03-06 17:36:54 -0800636 int current_tpt = IWL_INV_TPT;
637 int low_tpt = IWL_INV_TPT;
638 int high_tpt = IWL_INV_TPT;
Zhu Yib481de92007-09-25 17:54:57 -0700639 u32 fail_count;
640 s8 scale_action = 0;
641 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -0700642 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Abbas, Mohamedc338ba32009-01-21 10:58:02 -0800643 u16 fc;
644 u16 rate_mask = 0;
Johannes Berg4b7679a2008-09-18 18:14:18 +0200645 struct iwl3945_priv *priv = (struct iwl3945_priv *)priv_r;
Johannes Berge6a98542008-10-21 12:40:02 +0200646 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Zhu Yib481de92007-09-25 17:54:57 -0700647
648 IWL_DEBUG_RATE("enter\n");
649
Abbas, Mohamedc338ba32009-01-21 10:58:02 -0800650 if (sta)
651 rate_mask = sta->supp_rates[sband->band];
652
Michael Wu2bc454b2007-12-25 19:33:16 -0500653 /* Send management frames and broadcast/multicast data using lowest
654 * rate. */
655 fc = le16_to_cpu(hdr->frame_control);
656 if ((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA ||
657 is_multicast_ether_addr(hdr->addr1) ||
Johannes Berg4b7679a2008-09-18 18:14:18 +0200658 !sta || !priv_sta) {
Zhu Yib481de92007-09-25 17:54:57 -0700659 IWL_DEBUG_RATE("leave: No STA priv data to update!\n");
Abbas, Mohamedc338ba32009-01-21 10:58:02 -0800660 if (!rate_mask)
661 info->control.rates[0].idx =
662 rate_lowest_index(sband, NULL);
663 else
664 info->control.rates[0].idx =
665 rate_lowest_index(sband, sta);
Mattias Nissler1abbe492007-12-20 13:50:07 +0100666 return;
Zhu Yib481de92007-09-25 17:54:57 -0700667 }
668
Johannes Bergb7e35002008-09-11 02:22:58 +0200669 index = min(rs_sta->last_txrate_idx & 0xffff, IWL_RATE_COUNT - 1);
Mohamed Abbas14577f22007-11-12 11:37:42 +0800670
Tomas Winkler28447f32008-03-06 17:36:54 -0800671 if (sband->band == IEEE80211_BAND_5GHZ)
Mohamed Abbas14577f22007-11-12 11:37:42 +0800672 rate_mask = rate_mask << IWL_FIRST_OFDM_RATE;
Zhu Yib481de92007-09-25 17:54:57 -0700673
Johannes Berg05c914f2008-09-11 00:01:58 +0200674 if ((priv->iw_mode == NL80211_IFTYPE_ADHOC) &&
Tomas Winklerfa254a62008-01-27 20:41:38 -0800675 !rs_sta->ibss_sta_added) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800676 u8 sta_id = iwl3945_hw_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -0700677
678 if (sta_id == IWL_INVALID_STATION) {
Johannes Berge1749612008-10-27 15:59:26 -0700679 IWL_DEBUG_RATE("LQ: ADD station %pm\n",
680 hdr->addr1);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800681 sta_id = iwl3945_add_station(priv,
Zhu Yib481de92007-09-25 17:54:57 -0700682 hdr->addr1, 0, CMD_ASYNC);
683 }
684 if (sta_id != IWL_INVALID_STATION)
Tomas Winklerfa254a62008-01-27 20:41:38 -0800685 rs_sta->ibss_sta_added = 1;
Zhu Yib481de92007-09-25 17:54:57 -0700686 }
687
Tomas Winklerfa254a62008-01-27 20:41:38 -0800688 spin_lock_irqsave(&rs_sta->lock, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700689
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800690 /* for recent assoc, choose best rate regarding
691 * to rssi value
692 */
Tomas Winklerfa254a62008-01-27 20:41:38 -0800693 if (rs_sta->start_rate != IWL_RATE_INVALID) {
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800694 if (rs_sta->start_rate < index &&
695 (rate_mask & (1 << rs_sta->start_rate)))
696 index = rs_sta->start_rate;
Tomas Winklerfa254a62008-01-27 20:41:38 -0800697 rs_sta->start_rate = IWL_RATE_INVALID;
Zhu Yib481de92007-09-25 17:54:57 -0700698 }
699
Tomas Winklerfa254a62008-01-27 20:41:38 -0800700 window = &(rs_sta->win[index]);
Zhu Yib481de92007-09-25 17:54:57 -0700701
702 fail_count = window->counter - window->success_counter;
703
704 if (((fail_count <= IWL_RATE_MIN_FAILURE_TH) &&
705 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH))) {
Tomas Winklerfa254a62008-01-27 20:41:38 -0800706 spin_unlock_irqrestore(&rs_sta->lock, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700707
708 IWL_DEBUG_RATE("Invalid average_tpt on rate %d: "
709 "counter: %d, success_counter: %d, "
710 "expected_tpt is %sNULL\n",
711 index,
712 window->counter,
713 window->success_counter,
Tomas Winklerfa254a62008-01-27 20:41:38 -0800714 rs_sta->expected_tpt ? "not " : "");
Zhu Yib481de92007-09-25 17:54:57 -0700715 goto out;
716
717 }
718
Zhu Yib481de92007-09-25 17:54:57 -0700719 current_tpt = window->average_tpt;
720
Tomas Winklerfa254a62008-01-27 20:41:38 -0800721 high_low = iwl3945_get_adjacent_rate(rs_sta, index, rate_mask,
Tomas Winkler28447f32008-03-06 17:36:54 -0800722 sband->band);
Zhu Yib481de92007-09-25 17:54:57 -0700723 low = high_low & 0xff;
724 high = (high_low >> 8) & 0xff;
725
726 if (low != IWL_RATE_INVALID)
Tomas Winklerfa254a62008-01-27 20:41:38 -0800727 low_tpt = rs_sta->win[low].average_tpt;
Zhu Yib481de92007-09-25 17:54:57 -0700728
729 if (high != IWL_RATE_INVALID)
Tomas Winklerfa254a62008-01-27 20:41:38 -0800730 high_tpt = rs_sta->win[high].average_tpt;
Zhu Yib481de92007-09-25 17:54:57 -0700731
Tomas Winklerfa254a62008-01-27 20:41:38 -0800732 spin_unlock_irqrestore(&rs_sta->lock, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700733
734 scale_action = 1;
735
736 if ((window->success_ratio < IWL_RATE_DECREASE_TH) || !current_tpt) {
737 IWL_DEBUG_RATE("decrease rate because of low success_ratio\n");
738 scale_action = -1;
Tomas Winkler28447f32008-03-06 17:36:54 -0800739 } else if ((low_tpt == IWL_INV_TPT) && (high_tpt == IWL_INV_TPT))
Zhu Yib481de92007-09-25 17:54:57 -0700740 scale_action = 1;
Tomas Winkler28447f32008-03-06 17:36:54 -0800741 else if ((low_tpt != IWL_INV_TPT) && (high_tpt != IWL_INV_TPT) &&
742 (low_tpt < current_tpt) && (high_tpt < current_tpt)) {
Zhu Yib481de92007-09-25 17:54:57 -0700743 IWL_DEBUG_RATE("No action -- low [%d] & high [%d] < "
744 "current_tpt [%d]\n",
745 low_tpt, high_tpt, current_tpt);
746 scale_action = 0;
747 } else {
Tomas Winkler28447f32008-03-06 17:36:54 -0800748 if (high_tpt != IWL_INV_TPT) {
Zhu Yib481de92007-09-25 17:54:57 -0700749 if (high_tpt > current_tpt)
750 scale_action = 1;
751 else {
752 IWL_DEBUG_RATE
753 ("decrease rate because of high tpt\n");
754 scale_action = -1;
755 }
Tomas Winkler28447f32008-03-06 17:36:54 -0800756 } else if (low_tpt != IWL_INV_TPT) {
Zhu Yib481de92007-09-25 17:54:57 -0700757 if (low_tpt > current_tpt) {
758 IWL_DEBUG_RATE
759 ("decrease rate because of low tpt\n");
760 scale_action = -1;
761 } else
762 scale_action = 1;
763 }
764 }
765
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800766 if (scale_action == -1) {
767 if (window->success_ratio > IWL_SUCCESS_DOWN_TH)
768 scale_action = 0;
769 } else if (scale_action == 1) {
770 if (window->success_ratio < IWL_SUCCESS_UP_TH) {
771 IWL_DEBUG_RATE("No action -- success_ratio [%d] < "
772 "SUCCESS UP\n", window->success_ratio);
773 scale_action = 0;
774 }
Zhu Yib481de92007-09-25 17:54:57 -0700775 }
776
777 switch (scale_action) {
778 case -1:
779 if (low != IWL_RATE_INVALID)
780 index = low;
781 break;
782
783 case 1:
784 if (high != IWL_RATE_INVALID)
785 index = high;
786
787 break;
788
789 case 0:
790 default:
791 break;
792 }
793
794 IWL_DEBUG_RATE("Selected %d (action %d) - low %d high %d\n",
795 index, scale_action, low, high);
796
797 out:
798
Johannes Bergb7e35002008-09-11 02:22:58 +0200799 rs_sta->last_txrate_idx = index;
Tomas Winkler28447f32008-03-06 17:36:54 -0800800 if (sband->band == IEEE80211_BAND_5GHZ)
Johannes Berge6a98542008-10-21 12:40:02 +0200801 info->control.rates[0].idx = rs_sta->last_txrate_idx -
802 IWL_FIRST_OFDM_RATE;
Mohamed Abbas14577f22007-11-12 11:37:42 +0800803 else
Johannes Berge6a98542008-10-21 12:40:02 +0200804 info->control.rates[0].idx = rs_sta->last_txrate_idx;
Mohamed Abbas14577f22007-11-12 11:37:42 +0800805
Zhu Yib481de92007-09-25 17:54:57 -0700806 IWL_DEBUG_RATE("leave: %d\n", index);
Zhu Yib481de92007-09-25 17:54:57 -0700807}
808
Abbas, Mohamedc30e30e2008-12-02 12:14:02 -0800809#ifdef CONFIG_MAC80211_DEBUGFS
810static int iwl3945_open_file_generic(struct inode *inode, struct file *file)
811{
812 file->private_data = inode->i_private;
813 return 0;
814}
815
816static ssize_t iwl3945_sta_dbgfs_stats_table_read(struct file *file,
817 char __user *user_buf,
818 size_t count, loff_t *ppos)
819{
820 char buff[1024];
821 int desc = 0;
822 int j;
823 struct iwl3945_rs_sta *lq_sta = file->private_data;
824
825 desc += sprintf(buff + desc, "tx packets=%d last rate index=%d\n"
826 "rate=0x%X flush time %d\n",
827 lq_sta->tx_packets,
828 lq_sta->last_txrate_idx,
829 lq_sta->start_rate, jiffies_to_msecs(lq_sta->flush_time));
830 for (j = 0; j < IWL_RATE_COUNT; j++) {
831 desc += sprintf(buff+desc,
832 "counter=%d success=%d %%=%d\n",
833 lq_sta->win[j].counter,
834 lq_sta->win[j].success_counter,
835 lq_sta->win[j].success_ratio);
836 }
837 return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
838}
839
840static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
841 .read = iwl3945_sta_dbgfs_stats_table_read,
842 .open = iwl3945_open_file_generic,
843};
844
845static void iwl3945_add_debugfs(void *priv, void *priv_sta,
846 struct dentry *dir)
847{
848 struct iwl3945_rs_sta *lq_sta = priv_sta;
849
850 lq_sta->rs_sta_dbgfs_stats_table_file =
851 debugfs_create_file("rate_stats_table", 0600, dir,
852 lq_sta, &rs_sta_dbgfs_stats_table_ops);
853
854}
855
856static void iwl3945_remove_debugfs(void *priv, void *priv_sta)
857{
858 struct iwl3945_rs_sta *lq_sta = priv_sta;
859 debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
860}
861#endif
862
Zhu Yib481de92007-09-25 17:54:57 -0700863static struct rate_control_ops rs_ops = {
864 .module = NULL,
865 .name = RS_NAME,
866 .tx_status = rs_tx_status,
867 .get_rate = rs_get_rate,
868 .rate_init = rs_rate_init,
Zhu Yib481de92007-09-25 17:54:57 -0700869 .alloc = rs_alloc,
870 .free = rs_free,
871 .alloc_sta = rs_alloc_sta,
872 .free_sta = rs_free_sta,
Abbas, Mohamedc30e30e2008-12-02 12:14:02 -0800873#ifdef CONFIG_MAC80211_DEBUGFS
874 .add_sta_debugfs = iwl3945_add_debugfs,
875 .remove_sta_debugfs = iwl3945_remove_debugfs,
876#endif
877
Zhu Yib481de92007-09-25 17:54:57 -0700878};
879
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800880void iwl3945_rate_scale_init(struct ieee80211_hw *hw, s32 sta_id)
Zhu Yib481de92007-09-25 17:54:57 -0700881{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800882 struct iwl3945_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -0700883 s32 rssi = 0;
884 unsigned long flags;
Tomas Winklerfa254a62008-01-27 20:41:38 -0800885 struct iwl3945_rs_sta *rs_sta;
Johannes Berg4b7679a2008-09-18 18:14:18 +0200886 struct ieee80211_sta *sta;
887 struct iwl3945_sta_priv *psta;
Zhu Yib481de92007-09-25 17:54:57 -0700888
889 IWL_DEBUG_RATE("enter\n");
890
Johannes Bergd0709a62008-02-25 16:27:46 +0100891 rcu_read_lock();
892
Johannes Berg4b7679a2008-09-18 18:14:18 +0200893 sta = ieee80211_find_sta(hw, priv->stations[sta_id].sta.sta.addr);
Johannes Berg525181892008-10-11 01:46:25 +0200894 if (!sta) {
Johannes Bergd0709a62008-02-25 16:27:46 +0100895 rcu_read_unlock();
Zhu Yib481de92007-09-25 17:54:57 -0700896 return;
897 }
898
Johannes Berg525181892008-10-11 01:46:25 +0200899 psta = (void *) sta->drv_priv;
Johannes Berg4b7679a2008-09-18 18:14:18 +0200900 rs_sta = psta->rs_sta;
Zhu Yib481de92007-09-25 17:54:57 -0700901
Tomas Winklerfa254a62008-01-27 20:41:38 -0800902 spin_lock_irqsave(&rs_sta->lock, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700903
Tomas Winklerfa254a62008-01-27 20:41:38 -0800904 rs_sta->tgg = 0;
Johannes Berg8318d782008-01-24 19:38:38 +0100905 switch (priv->band) {
906 case IEEE80211_BAND_2GHZ:
907 /* TODO: this always does G, not a regression */
Zhu Yib481de92007-09-25 17:54:57 -0700908 if (priv->active_rxon.flags & RXON_FLG_TGG_PROTECT_MSK) {
Tomas Winklerfa254a62008-01-27 20:41:38 -0800909 rs_sta->tgg = 1;
910 rs_sta->expected_tpt = iwl3945_expected_tpt_g_prot;
Zhu Yib481de92007-09-25 17:54:57 -0700911 } else
Tomas Winklerfa254a62008-01-27 20:41:38 -0800912 rs_sta->expected_tpt = iwl3945_expected_tpt_g;
Zhu Yib481de92007-09-25 17:54:57 -0700913 break;
914
Johannes Berg8318d782008-01-24 19:38:38 +0100915 case IEEE80211_BAND_5GHZ:
Tomas Winklerfa254a62008-01-27 20:41:38 -0800916 rs_sta->expected_tpt = iwl3945_expected_tpt_a;
Zhu Yib481de92007-09-25 17:54:57 -0700917 break;
Johannes Berg8318d782008-01-24 19:38:38 +0100918 case IEEE80211_NUM_BANDS:
919 BUG();
Zhu Yib481de92007-09-25 17:54:57 -0700920 break;
921 }
922
Tomas Winklerfa254a62008-01-27 20:41:38 -0800923 spin_unlock_irqrestore(&rs_sta->lock, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700924
925 rssi = priv->last_rx_rssi;
926 if (rssi == 0)
927 rssi = IWL_MIN_RSSI_VAL;
928
929 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RATE, "Network RSSI: %d\n", rssi);
930
Johannes Berg8318d782008-01-24 19:38:38 +0100931 rs_sta->start_rate = iwl3945_get_rate_index_by_rssi(rssi, priv->band);
Zhu Yib481de92007-09-25 17:54:57 -0700932
933 IWL_DEBUG_RATE("leave: rssi %d assign rate index: "
Tomas Winklerfa254a62008-01-27 20:41:38 -0800934 "%d (plcp 0x%x)\n", rssi, rs_sta->start_rate,
935 iwl3945_rates[rs_sta->start_rate].plcp);
Johannes Berg525181892008-10-11 01:46:25 +0200936 rcu_read_unlock();
Zhu Yib481de92007-09-25 17:54:57 -0700937}
938
Reinette Chatre897e1cf2008-03-28 16:21:09 -0700939int iwl3945_rate_control_register(void)
Zhu Yib481de92007-09-25 17:54:57 -0700940{
Reinette Chatre897e1cf2008-03-28 16:21:09 -0700941 return ieee80211_rate_control_register(&rs_ops);
Zhu Yib481de92007-09-25 17:54:57 -0700942}
943
Reinette Chatre897e1cf2008-03-28 16:21:09 -0700944void iwl3945_rate_control_unregister(void)
Zhu Yib481de92007-09-25 17:54:57 -0700945{
946 ieee80211_rate_control_unregister(&rs_ops);
947}
948
949