blob: 56226032333a1647f53c09cd539a3d9f641d13bc [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 Winkler69d00d22008-12-19 10:37:02 +080039#include "iwl-3945-commands.h"
Christoph Hellwig5d08cd12007-10-25 17:15:50 +080040#include "iwl-3945.h"
Zhu Yib481de92007-09-25 17:54:57 -070041
42#define RS_NAME "iwl-3945-rs"
43
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080044struct iwl3945_rate_scale_data {
Zhu Yib481de92007-09-25 17:54:57 -070045 u64 data;
46 s32 success_counter;
47 s32 success_ratio;
48 s32 counter;
49 s32 average_tpt;
50 unsigned long stamp;
51};
52
Tomas Winklerfa254a62008-01-27 20:41:38 -080053struct iwl3945_rs_sta {
Zhu Yib481de92007-09-25 17:54:57 -070054 spinlock_t lock;
55 s32 *expected_tpt;
56 unsigned long last_partial_flush;
57 unsigned long last_flush;
58 u32 flush_time;
59 u32 last_tx_packets;
60 u32 tx_packets;
61 u8 tgg;
62 u8 flush_pending;
63 u8 start_rate;
64 u8 ibss_sta_added;
65 struct timer_list rate_scale_flush;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080066 struct iwl3945_rate_scale_data win[IWL_RATE_COUNT];
Abbas, Mohamedc30e30e2008-12-02 12:14:02 -080067#ifdef CONFIG_MAC80211_DEBUGFS
68 struct dentry *rs_sta_dbgfs_stats_table_file;
69#endif
Johannes Bergb7e35002008-09-11 02:22:58 +020070
71 /* used to be in sta_info */
72 int last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -070073};
74
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080075static s32 iwl3945_expected_tpt_g[IWL_RATE_COUNT] = {
Mohamed Abbas14577f22007-11-12 11:37:42 +080076 7, 13, 35, 58, 0, 0, 76, 104, 130, 168, 191, 202
Zhu Yib481de92007-09-25 17:54:57 -070077};
78
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080079static s32 iwl3945_expected_tpt_g_prot[IWL_RATE_COUNT] = {
Mohamed Abbas14577f22007-11-12 11:37:42 +080080 7, 13, 35, 58, 0, 0, 0, 80, 93, 113, 123, 125
Zhu Yib481de92007-09-25 17:54:57 -070081};
82
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080083static s32 iwl3945_expected_tpt_a[IWL_RATE_COUNT] = {
Mohamed Abbas14577f22007-11-12 11:37:42 +080084 0, 0, 0, 0, 40, 57, 72, 98, 121, 154, 177, 186
Zhu Yib481de92007-09-25 17:54:57 -070085};
86
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080087static s32 iwl3945_expected_tpt_b[IWL_RATE_COUNT] = {
Mohamed Abbas14577f22007-11-12 11:37:42 +080088 7, 13, 35, 58, 0, 0, 0, 0, 0, 0, 0, 0
Zhu Yib481de92007-09-25 17:54:57 -070089};
90
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080091struct iwl3945_tpt_entry {
Zhu Yib481de92007-09-25 17:54:57 -070092 s8 min_rssi;
93 u8 index;
94};
95
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080096static struct iwl3945_tpt_entry iwl3945_tpt_table_a[] = {
Zhu Yib481de92007-09-25 17:54:57 -070097 {-60, IWL_RATE_54M_INDEX},
98 {-64, IWL_RATE_48M_INDEX},
99 {-72, IWL_RATE_36M_INDEX},
100 {-80, IWL_RATE_24M_INDEX},
101 {-84, IWL_RATE_18M_INDEX},
102 {-85, IWL_RATE_12M_INDEX},
103 {-87, IWL_RATE_9M_INDEX},
104 {-89, IWL_RATE_6M_INDEX}
105};
106
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800107static struct iwl3945_tpt_entry iwl3945_tpt_table_g[] = {
Zhu Yib481de92007-09-25 17:54:57 -0700108 {-60, IWL_RATE_54M_INDEX},
109 {-64, IWL_RATE_48M_INDEX},
110 {-68, IWL_RATE_36M_INDEX},
111 {-80, IWL_RATE_24M_INDEX},
112 {-84, IWL_RATE_18M_INDEX},
113 {-85, IWL_RATE_12M_INDEX},
114 {-86, IWL_RATE_11M_INDEX},
115 {-88, IWL_RATE_5M_INDEX},
116 {-90, IWL_RATE_2M_INDEX},
117 {-92, IWL_RATE_1M_INDEX}
118};
119
120#define IWL_RATE_MAX_WINDOW 62
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800121#define IWL_RATE_FLUSH (3*HZ)
Zhu Yib481de92007-09-25 17:54:57 -0700122#define IWL_RATE_WIN_FLUSH (HZ/2)
123#define IWL_RATE_HIGH_TH 11520
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800124#define IWL_SUCCESS_UP_TH 8960
125#define IWL_SUCCESS_DOWN_TH 10880
Zhu Yib481de92007-09-25 17:54:57 -0700126#define IWL_RATE_MIN_FAILURE_TH 8
127#define IWL_RATE_MIN_SUCCESS_TH 8
128#define IWL_RATE_DECREASE_TH 1920
129
Johannes Berg8318d782008-01-24 19:38:38 +0100130static u8 iwl3945_get_rate_index_by_rssi(s32 rssi, enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -0700131{
132 u32 index = 0;
133 u32 table_size = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800134 struct iwl3945_tpt_entry *tpt_table = NULL;
Zhu Yib481de92007-09-25 17:54:57 -0700135
136 if ((rssi < IWL_MIN_RSSI_VAL) || (rssi > IWL_MAX_RSSI_VAL))
137 rssi = IWL_MIN_RSSI_VAL;
138
Johannes Berg8318d782008-01-24 19:38:38 +0100139 switch (band) {
140 case IEEE80211_BAND_2GHZ:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800141 tpt_table = iwl3945_tpt_table_g;
142 table_size = ARRAY_SIZE(iwl3945_tpt_table_g);
Zhu Yib481de92007-09-25 17:54:57 -0700143 break;
144
Johannes Berg8318d782008-01-24 19:38:38 +0100145 case IEEE80211_BAND_5GHZ:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800146 tpt_table = iwl3945_tpt_table_a;
147 table_size = ARRAY_SIZE(iwl3945_tpt_table_a);
Zhu Yib481de92007-09-25 17:54:57 -0700148 break;
149
150 default:
Johannes Berg8318d782008-01-24 19:38:38 +0100151 BUG();
Zhu Yib481de92007-09-25 17:54:57 -0700152 break;
153 }
154
155 while ((index < table_size) && (rssi < tpt_table[index].min_rssi))
156 index++;
157
158 index = min(index, (table_size - 1));
159
160 return tpt_table[index].index;
161}
162
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800163static void iwl3945_clear_window(struct iwl3945_rate_scale_data *window)
Zhu Yib481de92007-09-25 17:54:57 -0700164{
165 window->data = 0;
166 window->success_counter = 0;
Tomas Winkler28447f32008-03-06 17:36:54 -0800167 window->success_ratio = -1;
Zhu Yib481de92007-09-25 17:54:57 -0700168 window->counter = 0;
Tomas Winkler28447f32008-03-06 17:36:54 -0800169 window->average_tpt = IWL_INV_TPT;
Zhu Yib481de92007-09-25 17:54:57 -0700170 window->stamp = 0;
171}
172
173/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800174 * iwl3945_rate_scale_flush_windows - flush out the rate scale windows
Zhu Yib481de92007-09-25 17:54:57 -0700175 *
176 * Returns the number of windows that have gathered data but were
177 * not flushed. If there were any that were not flushed, then
178 * reschedule the rate flushing routine.
179 */
Tomas Winklerfa254a62008-01-27 20:41:38 -0800180static int iwl3945_rate_scale_flush_windows(struct iwl3945_rs_sta *rs_sta)
Zhu Yib481de92007-09-25 17:54:57 -0700181{
182 int unflushed = 0;
183 int i;
184 unsigned long flags;
185
186 /*
187 * For each rate, if we have collected data on that rate
188 * and it has been more than IWL_RATE_WIN_FLUSH
189 * since we flushed, clear out the gathered statistics
190 */
191 for (i = 0; i < IWL_RATE_COUNT; i++) {
Tomas Winklerfa254a62008-01-27 20:41:38 -0800192 if (!rs_sta->win[i].counter)
Zhu Yib481de92007-09-25 17:54:57 -0700193 continue;
194
Tomas Winklerfa254a62008-01-27 20:41:38 -0800195 spin_lock_irqsave(&rs_sta->lock, flags);
196 if (time_after(jiffies, rs_sta->win[i].stamp +
Zhu Yib481de92007-09-25 17:54:57 -0700197 IWL_RATE_WIN_FLUSH)) {
198 IWL_DEBUG_RATE("flushing %d samples of rate "
199 "index %d\n",
Tomas Winklerfa254a62008-01-27 20:41:38 -0800200 rs_sta->win[i].counter, i);
201 iwl3945_clear_window(&rs_sta->win[i]);
Zhu Yib481de92007-09-25 17:54:57 -0700202 } else
203 unflushed++;
Tomas Winklerfa254a62008-01-27 20:41:38 -0800204 spin_unlock_irqrestore(&rs_sta->lock, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700205 }
206
207 return unflushed;
208}
209
210#define IWL_RATE_FLUSH_MAX 5000 /* msec */
211#define IWL_RATE_FLUSH_MIN 50 /* msec */
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800212#define IWL_AVERAGE_PACKETS 1500
Zhu Yib481de92007-09-25 17:54:57 -0700213
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800214static void iwl3945_bg_rate_scale_flush(unsigned long data)
Zhu Yib481de92007-09-25 17:54:57 -0700215{
Tomas Winklerfa254a62008-01-27 20:41:38 -0800216 struct iwl3945_rs_sta *rs_sta = (void *)data;
Zhu Yib481de92007-09-25 17:54:57 -0700217 int unflushed = 0;
218 unsigned long flags;
219 u32 packet_count, duration, pps;
220
221 IWL_DEBUG_RATE("enter\n");
222
Tomas Winklerfa254a62008-01-27 20:41:38 -0800223 unflushed = iwl3945_rate_scale_flush_windows(rs_sta);
Zhu Yib481de92007-09-25 17:54:57 -0700224
Tomas Winklerfa254a62008-01-27 20:41:38 -0800225 spin_lock_irqsave(&rs_sta->lock, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700226
Zhu Yib481de92007-09-25 17:54:57 -0700227 /* Number of packets Rx'd since last time this timer ran */
Tomas Winklerfa254a62008-01-27 20:41:38 -0800228 packet_count = (rs_sta->tx_packets - rs_sta->last_tx_packets) + 1;
Zhu Yib481de92007-09-25 17:54:57 -0700229
Tomas Winklerfa254a62008-01-27 20:41:38 -0800230 rs_sta->last_tx_packets = rs_sta->tx_packets + 1;
Zhu Yib481de92007-09-25 17:54:57 -0700231
232 if (unflushed) {
233 duration =
Tomas Winklerfa254a62008-01-27 20:41:38 -0800234 jiffies_to_msecs(jiffies - rs_sta->last_partial_flush);
Zhu Yib481de92007-09-25 17:54:57 -0700235
236 IWL_DEBUG_RATE("Tx'd %d packets in %dms\n",
237 packet_count, duration);
238
239 /* Determine packets per second */
240 if (duration)
241 pps = (packet_count * 1000) / duration;
242 else
243 pps = 0;
244
245 if (pps) {
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800246 duration = (IWL_AVERAGE_PACKETS * 1000) / pps;
Zhu Yib481de92007-09-25 17:54:57 -0700247 if (duration < IWL_RATE_FLUSH_MIN)
248 duration = IWL_RATE_FLUSH_MIN;
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800249 else if (duration > IWL_RATE_FLUSH_MAX)
250 duration = IWL_RATE_FLUSH_MAX;
Zhu Yib481de92007-09-25 17:54:57 -0700251 } else
252 duration = IWL_RATE_FLUSH_MAX;
253
Tomas Winklerfa254a62008-01-27 20:41:38 -0800254 rs_sta->flush_time = msecs_to_jiffies(duration);
Zhu Yib481de92007-09-25 17:54:57 -0700255
256 IWL_DEBUG_RATE("new flush period: %d msec ave %d\n",
257 duration, packet_count);
258
Tomas Winklerfa254a62008-01-27 20:41:38 -0800259 mod_timer(&rs_sta->rate_scale_flush, jiffies +
260 rs_sta->flush_time);
Zhu Yib481de92007-09-25 17:54:57 -0700261
Tomas Winklerfa254a62008-01-27 20:41:38 -0800262 rs_sta->last_partial_flush = jiffies;
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800263 } else {
264 rs_sta->flush_time = IWL_RATE_FLUSH;
265 rs_sta->flush_pending = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700266 }
Zhu Yib481de92007-09-25 17:54:57 -0700267 /* If there weren't any unflushed entries, we don't schedule the timer
268 * to run again */
269
Tomas Winklerfa254a62008-01-27 20:41:38 -0800270 rs_sta->last_flush = jiffies;
Zhu Yib481de92007-09-25 17:54:57 -0700271
Tomas Winklerfa254a62008-01-27 20:41:38 -0800272 spin_unlock_irqrestore(&rs_sta->lock, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700273
274 IWL_DEBUG_RATE("leave\n");
275}
276
277/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800278 * iwl3945_collect_tx_data - Update the success/failure sliding window
Zhu Yib481de92007-09-25 17:54:57 -0700279 *
280 * We keep a sliding window of the last 64 packets transmitted
281 * at this rate. window->data contains the bitmask of successful
282 * packets.
283 */
Tomas Winklerfa254a62008-01-27 20:41:38 -0800284static void iwl3945_collect_tx_data(struct iwl3945_rs_sta *rs_sta,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800285 struct iwl3945_rate_scale_data *window,
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800286 int success, int retries, int index)
Zhu Yib481de92007-09-25 17:54:57 -0700287{
288 unsigned long flags;
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800289 s32 fail_count;
Zhu Yib481de92007-09-25 17:54:57 -0700290
291 if (!retries) {
292 IWL_DEBUG_RATE("leave: retries == 0 -- should be at least 1\n");
293 return;
294 }
295
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800296 spin_lock_irqsave(&rs_sta->lock, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700297 while (retries--) {
Zhu Yib481de92007-09-25 17:54:57 -0700298
299 /* If we have filled up the window then subtract one from the
300 * success counter if the high-bit is counting toward
301 * success */
302 if (window->counter == IWL_RATE_MAX_WINDOW) {
303 if (window->data & (1ULL << (IWL_RATE_MAX_WINDOW - 1)))
304 window->success_counter--;
305 } else
306 window->counter++;
307
308 /* Slide the window to the left one bit */
309 window->data = (window->data << 1);
310
311 /* If this packet was a success then set the low bit high */
312 if (success) {
313 window->success_counter++;
314 window->data |= 1;
315 }
316
317 /* window->counter can't be 0 -- it is either >0 or
318 * IWL_RATE_MAX_WINDOW */
319 window->success_ratio = 12800 * window->success_counter /
320 window->counter;
321
322 /* Tag this window as having been updated */
323 window->stamp = jiffies;
324
Zhu Yib481de92007-09-25 17:54:57 -0700325 }
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800326
327 fail_count = window->counter - window->success_counter;
328 if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
329 (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
330 window->average_tpt = ((window->success_ratio *
331 rs_sta->expected_tpt[index] + 64) / 128);
332 else
333 window->average_tpt = IWL_INV_TPT;
334
335 spin_unlock_irqrestore(&rs_sta->lock, flags);
336
Zhu Yib481de92007-09-25 17:54:57 -0700337}
338
Abbas, Mohamed72627962008-12-05 07:58:37 -0800339static void rs_rate_init(void *priv_r, struct ieee80211_supported_band *sband,
Johannes Berg4b7679a2008-09-18 18:14:18 +0200340 struct ieee80211_sta *sta, void *priv_sta)
Zhu Yib481de92007-09-25 17:54:57 -0700341{
Johannes Berg4b7679a2008-09-18 18:14:18 +0200342 struct iwl3945_rs_sta *rs_sta = priv_sta;
Abbas, Mohamed72627962008-12-05 07:58:37 -0800343 struct iwl3945_priv *priv = (struct iwl3945_priv *)priv_r;
Zhu Yib481de92007-09-25 17:54:57 -0700344 int i;
345
346 IWL_DEBUG_RATE("enter\n");
347
348 /* TODO: what is a good starting rate for STA? About middle? Maybe not
349 * the lowest or the highest rate.. Could consider using RSSI from
350 * previous packets? Need to have IEEE 802.1X auth succeed immediately
351 * after assoc.. */
352
Abbas, Mohamed72627962008-12-05 07:58:37 -0800353 for (i = sband->n_bitrates - 1; i >= 0; i--) {
Johannes Berg4b7679a2008-09-18 18:14:18 +0200354 if (sta->supp_rates[sband->band] & (1 << i)) {
Johannes Bergae17e982008-09-11 03:04:36 +0200355 rs_sta->last_txrate_idx = i;
Zhu Yib481de92007-09-25 17:54:57 -0700356 break;
357 }
358 }
359
Abbas, Mohamed72627962008-12-05 07:58:37 -0800360 priv->sta_supp_rates = sta->supp_rates[sband->band];
Johannes Berg8318d782008-01-24 19:38:38 +0100361 /* For 5 GHz band it start at IWL_FIRST_OFDM_RATE */
Abbas, Mohamed72627962008-12-05 07:58:37 -0800362 if (sband->band == IEEE80211_BAND_5GHZ) {
Johannes Bergb7e35002008-09-11 02:22:58 +0200363 rs_sta->last_txrate_idx += IWL_FIRST_OFDM_RATE;
Abbas, Mohamed72627962008-12-05 07:58:37 -0800364 priv->sta_supp_rates = priv->sta_supp_rates <<
365 IWL_FIRST_OFDM_RATE;
366 }
367
Mohamed Abbas14577f22007-11-12 11:37:42 +0800368
Zhu Yib481de92007-09-25 17:54:57 -0700369 IWL_DEBUG_RATE("leave\n");
370}
371
Johannes Berg4b7679a2008-09-18 18:14:18 +0200372static void *rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
Zhu Yib481de92007-09-25 17:54:57 -0700373{
Johannes Berg4b7679a2008-09-18 18:14:18 +0200374 return hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -0700375}
376
Ian Schram01ebd062007-10-25 17:15:22 +0800377/* rate scale requires free function to be implemented */
Zhu Yib481de92007-09-25 17:54:57 -0700378static void rs_free(void *priv)
379{
380 return;
381}
Johannes Berg4b7679a2008-09-18 18:14:18 +0200382
Johannes Berg4b7679a2008-09-18 18:14:18 +0200383static void *rs_alloc_sta(void *priv, struct ieee80211_sta *sta, gfp_t gfp)
Zhu Yib481de92007-09-25 17:54:57 -0700384{
Tomas Winklerfa254a62008-01-27 20:41:38 -0800385 struct iwl3945_rs_sta *rs_sta;
Johannes Berg4b7679a2008-09-18 18:14:18 +0200386 struct iwl3945_sta_priv *psta = (void *) sta->drv_priv;
Zhu Yib481de92007-09-25 17:54:57 -0700387 int i;
388
Johannes Berg4b7679a2008-09-18 18:14:18 +0200389 /*
390 * XXX: If it's using sta->drv_priv anyway, it might
391 * as well just put all the information there.
392 */
393
Zhu Yib481de92007-09-25 17:54:57 -0700394 IWL_DEBUG_RATE("enter\n");
395
Tomas Winklerfa254a62008-01-27 20:41:38 -0800396 rs_sta = kzalloc(sizeof(struct iwl3945_rs_sta), gfp);
397 if (!rs_sta) {
Zhu Yib481de92007-09-25 17:54:57 -0700398 IWL_DEBUG_RATE("leave: ENOMEM\n");
399 return NULL;
400 }
401
Johannes Berg4b7679a2008-09-18 18:14:18 +0200402 psta->rs_sta = rs_sta;
403
Tomas Winklerfa254a62008-01-27 20:41:38 -0800404 spin_lock_init(&rs_sta->lock);
Zhu Yib481de92007-09-25 17:54:57 -0700405
Tomas Winklerfa254a62008-01-27 20:41:38 -0800406 rs_sta->start_rate = IWL_RATE_INVALID;
Zhu Yib481de92007-09-25 17:54:57 -0700407
408 /* default to just 802.11b */
Tomas Winklerfa254a62008-01-27 20:41:38 -0800409 rs_sta->expected_tpt = iwl3945_expected_tpt_b;
Zhu Yib481de92007-09-25 17:54:57 -0700410
Tomas Winklerfa254a62008-01-27 20:41:38 -0800411 rs_sta->last_partial_flush = jiffies;
412 rs_sta->last_flush = jiffies;
413 rs_sta->flush_time = IWL_RATE_FLUSH;
414 rs_sta->last_tx_packets = 0;
415 rs_sta->ibss_sta_added = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700416
Tomas Winklerfa254a62008-01-27 20:41:38 -0800417 init_timer(&rs_sta->rate_scale_flush);
418 rs_sta->rate_scale_flush.data = (unsigned long)rs_sta;
419 rs_sta->rate_scale_flush.function = &iwl3945_bg_rate_scale_flush;
Zhu Yib481de92007-09-25 17:54:57 -0700420
421 for (i = 0; i < IWL_RATE_COUNT; i++)
Tomas Winklerfa254a62008-01-27 20:41:38 -0800422 iwl3945_clear_window(&rs_sta->win[i]);
Zhu Yib481de92007-09-25 17:54:57 -0700423
424 IWL_DEBUG_RATE("leave\n");
425
Tomas Winklerfa254a62008-01-27 20:41:38 -0800426 return rs_sta;
Zhu Yib481de92007-09-25 17:54:57 -0700427}
428
Johannes Berg4b7679a2008-09-18 18:14:18 +0200429static void rs_free_sta(void *priv, struct ieee80211_sta *sta,
430 void *priv_sta)
Zhu Yib481de92007-09-25 17:54:57 -0700431{
Johannes Berg4b7679a2008-09-18 18:14:18 +0200432 struct iwl3945_sta_priv *psta = (void *) sta->drv_priv;
Tomas Winklerfa254a62008-01-27 20:41:38 -0800433 struct iwl3945_rs_sta *rs_sta = priv_sta;
Zhu Yib481de92007-09-25 17:54:57 -0700434
Johannes Berg4b7679a2008-09-18 18:14:18 +0200435 psta->rs_sta = NULL;
436
Zhu Yib481de92007-09-25 17:54:57 -0700437 IWL_DEBUG_RATE("enter\n");
Tomas Winklerfa254a62008-01-27 20:41:38 -0800438 del_timer_sync(&rs_sta->rate_scale_flush);
439 kfree(rs_sta);
Zhu Yib481de92007-09-25 17:54:57 -0700440 IWL_DEBUG_RATE("leave\n");
441}
442
Mohamed Abbas14577f22007-11-12 11:37:42 +0800443
Zhu Yib481de92007-09-25 17:54:57 -0700444/**
445 * rs_tx_status - Update rate control values based on Tx results
446 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800447 * NOTE: Uses iwl3945_priv->retry_rate for the # of retries attempted by
Zhu Yib481de92007-09-25 17:54:57 -0700448 * the hardware for each rate.
449 */
Johannes Berg4b7679a2008-09-18 18:14:18 +0200450static void rs_tx_status(void *priv_rate, struct ieee80211_supported_band *sband,
451 struct ieee80211_sta *sta, void *priv_sta,
Johannes Berge039fa42008-05-15 12:55:29 +0200452 struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -0700453{
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800454 s8 retries = 0, current_count;
Zhu Yib481de92007-09-25 17:54:57 -0700455 int scale_rate_index, first_index, last_index;
456 unsigned long flags;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800457 struct iwl3945_priv *priv = (struct iwl3945_priv *)priv_rate;
Johannes Berg4b7679a2008-09-18 18:14:18 +0200458 struct iwl3945_rs_sta *rs_sta = priv_sta;
Johannes Berge039fa42008-05-15 12:55:29 +0200459 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Johannes Berg8318d782008-01-24 19:38:38 +0100460
Zhu Yib481de92007-09-25 17:54:57 -0700461 IWL_DEBUG_RATE("enter\n");
462
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800463 retries = info->status.rates[0].count;
Johannes Berge6a98542008-10-21 12:40:02 +0200464
465 first_index = sband->bitrates[info->status.rates[0].idx].hw_value;
Zhu Yib481de92007-09-25 17:54:57 -0700466 if ((first_index < 0) || (first_index >= IWL_RATE_COUNT)) {
Johannes Berg8318d782008-01-24 19:38:38 +0100467 IWL_DEBUG_RATE("leave: Rate out of bounds: %d\n", first_index);
Zhu Yib481de92007-09-25 17:54:57 -0700468 return;
469 }
470
Johannes Berg4b7679a2008-09-18 18:14:18 +0200471 if (!priv_sta) {
Zhu Yib481de92007-09-25 17:54:57 -0700472 IWL_DEBUG_RATE("leave: No STA priv data to update!\n");
473 return;
474 }
475
Tomas Winklerfa254a62008-01-27 20:41:38 -0800476 rs_sta->tx_packets++;
Zhu Yib481de92007-09-25 17:54:57 -0700477
478 scale_rate_index = first_index;
479 last_index = first_index;
480
481 /*
482 * Update the window for each rate. We determine which rates
483 * were Tx'd based on the total number of retries vs. the number
484 * of retries configured for each rate -- currently set to the
485 * priv value 'retry_rate' vs. rate specific
486 *
487 * On exit from this while loop last_index indicates the rate
488 * at which the frame was finally transmitted (or failed if no
489 * ACK)
490 */
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800491 while (retries > 1) {
492 if ((retries - 1) < priv->retry_rate) {
493 current_count = (retries - 1);
Zhu Yib481de92007-09-25 17:54:57 -0700494 last_index = scale_rate_index;
495 } else {
496 current_count = priv->retry_rate;
Johannes Berge6a98542008-10-21 12:40:02 +0200497 last_index = iwl3945_rs_next_rate(priv,
Mohamed Abbas14577f22007-11-12 11:37:42 +0800498 scale_rate_index);
Zhu Yib481de92007-09-25 17:54:57 -0700499 }
500
501 /* Update this rate accounting for as many retries
502 * as was used for it (per current_count) */
Tomas Winklerfa254a62008-01-27 20:41:38 -0800503 iwl3945_collect_tx_data(rs_sta,
504 &rs_sta->win[scale_rate_index],
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800505 0, current_count, scale_rate_index);
Zhu Yib481de92007-09-25 17:54:57 -0700506 IWL_DEBUG_RATE("Update rate %d for %d retries.\n",
507 scale_rate_index, current_count);
508
509 retries -= current_count;
510
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800511 scale_rate_index = last_index;
Zhu Yib481de92007-09-25 17:54:57 -0700512 }
513
Mohamed Abbas14577f22007-11-12 11:37:42 +0800514
Zhu Yib481de92007-09-25 17:54:57 -0700515 /* Update the last index window with success/failure based on ACK */
516 IWL_DEBUG_RATE("Update rate %d with %s.\n",
517 last_index,
Johannes Berge039fa42008-05-15 12:55:29 +0200518 (info->flags & IEEE80211_TX_STAT_ACK) ?
Zhu Yib481de92007-09-25 17:54:57 -0700519 "success" : "failure");
Tomas Winklerfa254a62008-01-27 20:41:38 -0800520 iwl3945_collect_tx_data(rs_sta,
521 &rs_sta->win[last_index],
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800522 info->flags & IEEE80211_TX_STAT_ACK, 1, last_index);
Zhu Yib481de92007-09-25 17:54:57 -0700523
524 /* We updated the rate scale window -- if its been more than
525 * flush_time since the last run, schedule the flush
526 * again */
Tomas Winklerfa254a62008-01-27 20:41:38 -0800527 spin_lock_irqsave(&rs_sta->lock, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700528
Tomas Winklerfa254a62008-01-27 20:41:38 -0800529 if (!rs_sta->flush_pending &&
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800530 time_after(jiffies, rs_sta->last_flush +
Tomas Winklerfa254a62008-01-27 20:41:38 -0800531 rs_sta->flush_time)) {
Zhu Yib481de92007-09-25 17:54:57 -0700532
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800533 rs_sta->last_partial_flush = jiffies;
Tomas Winklerfa254a62008-01-27 20:41:38 -0800534 rs_sta->flush_pending = 1;
535 mod_timer(&rs_sta->rate_scale_flush,
536 jiffies + rs_sta->flush_time);
Zhu Yib481de92007-09-25 17:54:57 -0700537 }
538
Tomas Winklerfa254a62008-01-27 20:41:38 -0800539 spin_unlock_irqrestore(&rs_sta->lock, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700540
Zhu Yib481de92007-09-25 17:54:57 -0700541 IWL_DEBUG_RATE("leave\n");
542
543 return;
544}
545
Tomas Winklerfa254a62008-01-27 20:41:38 -0800546static u16 iwl3945_get_adjacent_rate(struct iwl3945_rs_sta *rs_sta,
Johannes Berg8318d782008-01-24 19:38:38 +0100547 u8 index, u16 rate_mask, enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -0700548{
549 u8 high = IWL_RATE_INVALID;
550 u8 low = IWL_RATE_INVALID;
551
Ian Schram01ebd062007-10-25 17:15:22 +0800552 /* 802.11A walks to the next literal adjacent rate in
Zhu Yib481de92007-09-25 17:54:57 -0700553 * the rate table */
Johannes Berg8318d782008-01-24 19:38:38 +0100554 if (unlikely(band == IEEE80211_BAND_5GHZ)) {
Zhu Yib481de92007-09-25 17:54:57 -0700555 int i;
556 u32 mask;
557
558 /* Find the previous rate that is in the rate mask */
559 i = index - 1;
560 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
561 if (rate_mask & mask) {
562 low = i;
563 break;
564 }
565 }
566
567 /* Find the next rate that is in the rate mask */
568 i = index + 1;
569 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
570 if (rate_mask & mask) {
571 high = i;
572 break;
573 }
574 }
575
576 return (high << 8) | low;
577 }
578
579 low = index;
580 while (low != IWL_RATE_INVALID) {
Tomas Winklerfa254a62008-01-27 20:41:38 -0800581 if (rs_sta->tgg)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800582 low = iwl3945_rates[low].prev_rs_tgg;
Zhu Yib481de92007-09-25 17:54:57 -0700583 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800584 low = iwl3945_rates[low].prev_rs;
Zhu Yib481de92007-09-25 17:54:57 -0700585 if (low == IWL_RATE_INVALID)
586 break;
587 if (rate_mask & (1 << low))
588 break;
589 IWL_DEBUG_RATE("Skipping masked lower rate: %d\n", low);
590 }
591
592 high = index;
593 while (high != IWL_RATE_INVALID) {
Tomas Winklerfa254a62008-01-27 20:41:38 -0800594 if (rs_sta->tgg)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800595 high = iwl3945_rates[high].next_rs_tgg;
Zhu Yib481de92007-09-25 17:54:57 -0700596 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800597 high = iwl3945_rates[high].next_rs;
Zhu Yib481de92007-09-25 17:54:57 -0700598 if (high == IWL_RATE_INVALID)
599 break;
600 if (rate_mask & (1 << high))
601 break;
602 IWL_DEBUG_RATE("Skipping masked higher rate: %d\n", high);
603 }
604
605 return (high << 8) | low;
606}
607
608/**
609 * rs_get_rate - find the rate for the requested packet
610 *
611 * Returns the ieee80211_rate structure allocated by the driver.
612 *
613 * The rate control algorithm has no internal mapping between hw_mode's
614 * rate ordering and the rate ordering used by the rate control algorithm.
615 *
616 * The rate control algorithm uses a single table of rates that goes across
617 * the entire A/B/G spectrum vs. being limited to just one particular
618 * hw_mode.
619 *
620 * As such, we can't convert the index obtained below into the hw_mode's
621 * rate table and must reference the driver allocated rate table
622 *
623 */
Johannes Berge6a98542008-10-21 12:40:02 +0200624static void rs_get_rate(void *priv_r, struct ieee80211_sta *sta,
625 void *priv_sta, struct ieee80211_tx_rate_control *txrc)
Zhu Yib481de92007-09-25 17:54:57 -0700626{
Johannes Berge6a98542008-10-21 12:40:02 +0200627 struct ieee80211_supported_band *sband = txrc->sband;
628 struct sk_buff *skb = txrc->skb;
Zhu Yib481de92007-09-25 17:54:57 -0700629 u8 low = IWL_RATE_INVALID;
630 u8 high = IWL_RATE_INVALID;
631 u16 high_low;
632 int index;
Johannes Berg4b7679a2008-09-18 18:14:18 +0200633 struct iwl3945_rs_sta *rs_sta = priv_sta;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800634 struct iwl3945_rate_scale_data *window = NULL;
Tomas Winkler28447f32008-03-06 17:36:54 -0800635 int current_tpt = IWL_INV_TPT;
636 int low_tpt = IWL_INV_TPT;
637 int high_tpt = IWL_INV_TPT;
Zhu Yib481de92007-09-25 17:54:57 -0700638 u32 fail_count;
639 s8 scale_action = 0;
640 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -0700641 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Abbas, Mohamedc338ba32009-01-21 10:58:02 -0800642 u16 fc;
643 u16 rate_mask = 0;
Johannes Berg4b7679a2008-09-18 18:14:18 +0200644 struct iwl3945_priv *priv = (struct iwl3945_priv *)priv_r;
Johannes Berge6a98542008-10-21 12:40:02 +0200645 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Zhu Yib481de92007-09-25 17:54:57 -0700646
647 IWL_DEBUG_RATE("enter\n");
648
Abbas, Mohamedc338ba32009-01-21 10:58:02 -0800649 if (sta)
650 rate_mask = sta->supp_rates[sband->band];
651
Michael Wu2bc454b2007-12-25 19:33:16 -0500652 /* Send management frames and broadcast/multicast data using lowest
653 * rate. */
654 fc = le16_to_cpu(hdr->frame_control);
655 if ((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA ||
656 is_multicast_ether_addr(hdr->addr1) ||
Johannes Berg4b7679a2008-09-18 18:14:18 +0200657 !sta || !priv_sta) {
Zhu Yib481de92007-09-25 17:54:57 -0700658 IWL_DEBUG_RATE("leave: No STA priv data to update!\n");
Abbas, Mohamedc338ba32009-01-21 10:58:02 -0800659 if (!rate_mask)
660 info->control.rates[0].idx =
661 rate_lowest_index(sband, NULL);
662 else
663 info->control.rates[0].idx =
664 rate_lowest_index(sband, sta);
Mattias Nissler1abbe492007-12-20 13:50:07 +0100665 return;
Zhu Yib481de92007-09-25 17:54:57 -0700666 }
667
Johannes Bergb7e35002008-09-11 02:22:58 +0200668 index = min(rs_sta->last_txrate_idx & 0xffff, IWL_RATE_COUNT - 1);
Mohamed Abbas14577f22007-11-12 11:37:42 +0800669
Tomas Winkler28447f32008-03-06 17:36:54 -0800670 if (sband->band == IEEE80211_BAND_5GHZ)
Mohamed Abbas14577f22007-11-12 11:37:42 +0800671 rate_mask = rate_mask << IWL_FIRST_OFDM_RATE;
Zhu Yib481de92007-09-25 17:54:57 -0700672
Johannes Berg05c914f2008-09-11 00:01:58 +0200673 if ((priv->iw_mode == NL80211_IFTYPE_ADHOC) &&
Tomas Winklerfa254a62008-01-27 20:41:38 -0800674 !rs_sta->ibss_sta_added) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800675 u8 sta_id = iwl3945_hw_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -0700676
677 if (sta_id == IWL_INVALID_STATION) {
Johannes Berge1749612008-10-27 15:59:26 -0700678 IWL_DEBUG_RATE("LQ: ADD station %pm\n",
679 hdr->addr1);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800680 sta_id = iwl3945_add_station(priv,
Zhu Yib481de92007-09-25 17:54:57 -0700681 hdr->addr1, 0, CMD_ASYNC);
682 }
683 if (sta_id != IWL_INVALID_STATION)
Tomas Winklerfa254a62008-01-27 20:41:38 -0800684 rs_sta->ibss_sta_added = 1;
Zhu Yib481de92007-09-25 17:54:57 -0700685 }
686
Tomas Winklerfa254a62008-01-27 20:41:38 -0800687 spin_lock_irqsave(&rs_sta->lock, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700688
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800689 /* for recent assoc, choose best rate regarding
690 * to rssi value
691 */
Tomas Winklerfa254a62008-01-27 20:41:38 -0800692 if (rs_sta->start_rate != IWL_RATE_INVALID) {
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800693 if (rs_sta->start_rate < index &&
694 (rate_mask & (1 << rs_sta->start_rate)))
695 index = rs_sta->start_rate;
Tomas Winklerfa254a62008-01-27 20:41:38 -0800696 rs_sta->start_rate = IWL_RATE_INVALID;
Zhu Yib481de92007-09-25 17:54:57 -0700697 }
698
Tomas Winklerfa254a62008-01-27 20:41:38 -0800699 window = &(rs_sta->win[index]);
Zhu Yib481de92007-09-25 17:54:57 -0700700
701 fail_count = window->counter - window->success_counter;
702
703 if (((fail_count <= IWL_RATE_MIN_FAILURE_TH) &&
704 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH))) {
Tomas Winklerfa254a62008-01-27 20:41:38 -0800705 spin_unlock_irqrestore(&rs_sta->lock, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700706
707 IWL_DEBUG_RATE("Invalid average_tpt on rate %d: "
708 "counter: %d, success_counter: %d, "
709 "expected_tpt is %sNULL\n",
710 index,
711 window->counter,
712 window->success_counter,
Tomas Winklerfa254a62008-01-27 20:41:38 -0800713 rs_sta->expected_tpt ? "not " : "");
Zhu Yib481de92007-09-25 17:54:57 -0700714 goto out;
715
716 }
717
Zhu Yib481de92007-09-25 17:54:57 -0700718 current_tpt = window->average_tpt;
719
Tomas Winklerfa254a62008-01-27 20:41:38 -0800720 high_low = iwl3945_get_adjacent_rate(rs_sta, index, rate_mask,
Tomas Winkler28447f32008-03-06 17:36:54 -0800721 sband->band);
Zhu Yib481de92007-09-25 17:54:57 -0700722 low = high_low & 0xff;
723 high = (high_low >> 8) & 0xff;
724
725 if (low != IWL_RATE_INVALID)
Tomas Winklerfa254a62008-01-27 20:41:38 -0800726 low_tpt = rs_sta->win[low].average_tpt;
Zhu Yib481de92007-09-25 17:54:57 -0700727
728 if (high != IWL_RATE_INVALID)
Tomas Winklerfa254a62008-01-27 20:41:38 -0800729 high_tpt = rs_sta->win[high].average_tpt;
Zhu Yib481de92007-09-25 17:54:57 -0700730
Tomas Winklerfa254a62008-01-27 20:41:38 -0800731 spin_unlock_irqrestore(&rs_sta->lock, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700732
733 scale_action = 1;
734
735 if ((window->success_ratio < IWL_RATE_DECREASE_TH) || !current_tpt) {
736 IWL_DEBUG_RATE("decrease rate because of low success_ratio\n");
737 scale_action = -1;
Tomas Winkler28447f32008-03-06 17:36:54 -0800738 } else if ((low_tpt == IWL_INV_TPT) && (high_tpt == IWL_INV_TPT))
Zhu Yib481de92007-09-25 17:54:57 -0700739 scale_action = 1;
Tomas Winkler28447f32008-03-06 17:36:54 -0800740 else if ((low_tpt != IWL_INV_TPT) && (high_tpt != IWL_INV_TPT) &&
741 (low_tpt < current_tpt) && (high_tpt < current_tpt)) {
Zhu Yib481de92007-09-25 17:54:57 -0700742 IWL_DEBUG_RATE("No action -- low [%d] & high [%d] < "
743 "current_tpt [%d]\n",
744 low_tpt, high_tpt, current_tpt);
745 scale_action = 0;
746 } else {
Tomas Winkler28447f32008-03-06 17:36:54 -0800747 if (high_tpt != IWL_INV_TPT) {
Zhu Yib481de92007-09-25 17:54:57 -0700748 if (high_tpt > current_tpt)
749 scale_action = 1;
750 else {
751 IWL_DEBUG_RATE
752 ("decrease rate because of high tpt\n");
753 scale_action = -1;
754 }
Tomas Winkler28447f32008-03-06 17:36:54 -0800755 } else if (low_tpt != IWL_INV_TPT) {
Zhu Yib481de92007-09-25 17:54:57 -0700756 if (low_tpt > current_tpt) {
757 IWL_DEBUG_RATE
758 ("decrease rate because of low tpt\n");
759 scale_action = -1;
760 } else
761 scale_action = 1;
762 }
763 }
764
Abbas, Mohamed74221d02008-12-02 12:14:03 -0800765 if (scale_action == -1) {
766 if (window->success_ratio > IWL_SUCCESS_DOWN_TH)
767 scale_action = 0;
768 } else if (scale_action == 1) {
769 if (window->success_ratio < IWL_SUCCESS_UP_TH) {
770 IWL_DEBUG_RATE("No action -- success_ratio [%d] < "
771 "SUCCESS UP\n", window->success_ratio);
772 scale_action = 0;
773 }
Zhu Yib481de92007-09-25 17:54:57 -0700774 }
775
776 switch (scale_action) {
777 case -1:
778 if (low != IWL_RATE_INVALID)
779 index = low;
780 break;
781
782 case 1:
783 if (high != IWL_RATE_INVALID)
784 index = high;
785
786 break;
787
788 case 0:
789 default:
790 break;
791 }
792
793 IWL_DEBUG_RATE("Selected %d (action %d) - low %d high %d\n",
794 index, scale_action, low, high);
795
796 out:
797
Johannes Bergb7e35002008-09-11 02:22:58 +0200798 rs_sta->last_txrate_idx = index;
Tomas Winkler28447f32008-03-06 17:36:54 -0800799 if (sband->band == IEEE80211_BAND_5GHZ)
Johannes Berge6a98542008-10-21 12:40:02 +0200800 info->control.rates[0].idx = rs_sta->last_txrate_idx -
801 IWL_FIRST_OFDM_RATE;
Mohamed Abbas14577f22007-11-12 11:37:42 +0800802 else
Johannes Berge6a98542008-10-21 12:40:02 +0200803 info->control.rates[0].idx = rs_sta->last_txrate_idx;
Mohamed Abbas14577f22007-11-12 11:37:42 +0800804
Zhu Yib481de92007-09-25 17:54:57 -0700805 IWL_DEBUG_RATE("leave: %d\n", index);
Zhu Yib481de92007-09-25 17:54:57 -0700806}
807
Abbas, Mohamedc30e30e2008-12-02 12:14:02 -0800808#ifdef CONFIG_MAC80211_DEBUGFS
809static int iwl3945_open_file_generic(struct inode *inode, struct file *file)
810{
811 file->private_data = inode->i_private;
812 return 0;
813}
814
815static ssize_t iwl3945_sta_dbgfs_stats_table_read(struct file *file,
816 char __user *user_buf,
817 size_t count, loff_t *ppos)
818{
819 char buff[1024];
820 int desc = 0;
821 int j;
822 struct iwl3945_rs_sta *lq_sta = file->private_data;
823
824 desc += sprintf(buff + desc, "tx packets=%d last rate index=%d\n"
825 "rate=0x%X flush time %d\n",
826 lq_sta->tx_packets,
827 lq_sta->last_txrate_idx,
828 lq_sta->start_rate, jiffies_to_msecs(lq_sta->flush_time));
829 for (j = 0; j < IWL_RATE_COUNT; j++) {
830 desc += sprintf(buff+desc,
831 "counter=%d success=%d %%=%d\n",
832 lq_sta->win[j].counter,
833 lq_sta->win[j].success_counter,
834 lq_sta->win[j].success_ratio);
835 }
836 return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
837}
838
839static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
840 .read = iwl3945_sta_dbgfs_stats_table_read,
841 .open = iwl3945_open_file_generic,
842};
843
844static void iwl3945_add_debugfs(void *priv, void *priv_sta,
845 struct dentry *dir)
846{
847 struct iwl3945_rs_sta *lq_sta = priv_sta;
848
849 lq_sta->rs_sta_dbgfs_stats_table_file =
850 debugfs_create_file("rate_stats_table", 0600, dir,
851 lq_sta, &rs_sta_dbgfs_stats_table_ops);
852
853}
854
855static void iwl3945_remove_debugfs(void *priv, void *priv_sta)
856{
857 struct iwl3945_rs_sta *lq_sta = priv_sta;
858 debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
859}
860#endif
861
Zhu Yib481de92007-09-25 17:54:57 -0700862static struct rate_control_ops rs_ops = {
863 .module = NULL,
864 .name = RS_NAME,
865 .tx_status = rs_tx_status,
866 .get_rate = rs_get_rate,
867 .rate_init = rs_rate_init,
Zhu Yib481de92007-09-25 17:54:57 -0700868 .alloc = rs_alloc,
869 .free = rs_free,
870 .alloc_sta = rs_alloc_sta,
871 .free_sta = rs_free_sta,
Abbas, Mohamedc30e30e2008-12-02 12:14:02 -0800872#ifdef CONFIG_MAC80211_DEBUGFS
873 .add_sta_debugfs = iwl3945_add_debugfs,
874 .remove_sta_debugfs = iwl3945_remove_debugfs,
875#endif
876
Zhu Yib481de92007-09-25 17:54:57 -0700877};
878
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800879void iwl3945_rate_scale_init(struct ieee80211_hw *hw, s32 sta_id)
Zhu Yib481de92007-09-25 17:54:57 -0700880{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800881 struct iwl3945_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -0700882 s32 rssi = 0;
883 unsigned long flags;
Tomas Winklerfa254a62008-01-27 20:41:38 -0800884 struct iwl3945_rs_sta *rs_sta;
Johannes Berg4b7679a2008-09-18 18:14:18 +0200885 struct ieee80211_sta *sta;
886 struct iwl3945_sta_priv *psta;
Zhu Yib481de92007-09-25 17:54:57 -0700887
888 IWL_DEBUG_RATE("enter\n");
889
Johannes Bergd0709a62008-02-25 16:27:46 +0100890 rcu_read_lock();
891
Johannes Berg4b7679a2008-09-18 18:14:18 +0200892 sta = ieee80211_find_sta(hw, priv->stations[sta_id].sta.sta.addr);
Johannes Berg525181892008-10-11 01:46:25 +0200893 if (!sta) {
Johannes Bergd0709a62008-02-25 16:27:46 +0100894 rcu_read_unlock();
Zhu Yib481de92007-09-25 17:54:57 -0700895 return;
896 }
897
Johannes Berg525181892008-10-11 01:46:25 +0200898 psta = (void *) sta->drv_priv;
Johannes Berg4b7679a2008-09-18 18:14:18 +0200899 rs_sta = psta->rs_sta;
Zhu Yib481de92007-09-25 17:54:57 -0700900
Tomas Winklerfa254a62008-01-27 20:41:38 -0800901 spin_lock_irqsave(&rs_sta->lock, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700902
Tomas Winklerfa254a62008-01-27 20:41:38 -0800903 rs_sta->tgg = 0;
Johannes Berg8318d782008-01-24 19:38:38 +0100904 switch (priv->band) {
905 case IEEE80211_BAND_2GHZ:
906 /* TODO: this always does G, not a regression */
Zhu Yib481de92007-09-25 17:54:57 -0700907 if (priv->active_rxon.flags & RXON_FLG_TGG_PROTECT_MSK) {
Tomas Winklerfa254a62008-01-27 20:41:38 -0800908 rs_sta->tgg = 1;
909 rs_sta->expected_tpt = iwl3945_expected_tpt_g_prot;
Zhu Yib481de92007-09-25 17:54:57 -0700910 } else
Tomas Winklerfa254a62008-01-27 20:41:38 -0800911 rs_sta->expected_tpt = iwl3945_expected_tpt_g;
Zhu Yib481de92007-09-25 17:54:57 -0700912 break;
913
Johannes Berg8318d782008-01-24 19:38:38 +0100914 case IEEE80211_BAND_5GHZ:
Tomas Winklerfa254a62008-01-27 20:41:38 -0800915 rs_sta->expected_tpt = iwl3945_expected_tpt_a;
Zhu Yib481de92007-09-25 17:54:57 -0700916 break;
Johannes Berg8318d782008-01-24 19:38:38 +0100917 case IEEE80211_NUM_BANDS:
918 BUG();
Zhu Yib481de92007-09-25 17:54:57 -0700919 break;
920 }
921
Tomas Winklerfa254a62008-01-27 20:41:38 -0800922 spin_unlock_irqrestore(&rs_sta->lock, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700923
924 rssi = priv->last_rx_rssi;
925 if (rssi == 0)
926 rssi = IWL_MIN_RSSI_VAL;
927
928 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RATE, "Network RSSI: %d\n", rssi);
929
Johannes Berg8318d782008-01-24 19:38:38 +0100930 rs_sta->start_rate = iwl3945_get_rate_index_by_rssi(rssi, priv->band);
Zhu Yib481de92007-09-25 17:54:57 -0700931
932 IWL_DEBUG_RATE("leave: rssi %d assign rate index: "
Tomas Winklerfa254a62008-01-27 20:41:38 -0800933 "%d (plcp 0x%x)\n", rssi, rs_sta->start_rate,
934 iwl3945_rates[rs_sta->start_rate].plcp);
Johannes Berg525181892008-10-11 01:46:25 +0200935 rcu_read_unlock();
Zhu Yib481de92007-09-25 17:54:57 -0700936}
937
Reinette Chatre897e1cf2008-03-28 16:21:09 -0700938int iwl3945_rate_control_register(void)
Zhu Yib481de92007-09-25 17:54:57 -0700939{
Reinette Chatre897e1cf2008-03-28 16:21:09 -0700940 return ieee80211_rate_control_register(&rs_ops);
Zhu Yib481de92007-09-25 17:54:57 -0700941}
942
Reinette Chatre897e1cf2008-03-28 16:21:09 -0700943void iwl3945_rate_control_unregister(void)
Zhu Yib481de92007-09-25 17:54:57 -0700944{
945 ieee80211_rate_control_unregister(&rs_ops);
946}
947
948