blob: 5129d57ed591e4f263d0eca814a9e516db1de69d [file] [log] [blame]
Zhu Yib481de92007-09-25 17:54:57 -07001/******************************************************************************
2 *
Reinette Chatreeb7ae892008-03-11 16:17:17 -07003 * Copyright(c) 2005 - 2008 Intel Corporation. All rights reserved.
Zhu Yib481de92007-09-25 17:54:57 -07004 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
17 *
18 * The full GNU General Public License is included in this distribution in the
19 * file called LICENSE.
20 *
21 * Contact Information:
22 * James P. Ketrenos <ipw2100-admin@linux.intel.com>
23 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24 *
25 *****************************************************************************/
26#include <linux/kernel.h>
27#include <linux/init.h>
28#include <linux/skbuff.h>
29#include <linux/wireless.h>
30#include <net/mac80211.h>
Zhu Yib481de92007-09-25 17:54:57 -070031
32#include <linux/netdevice.h>
33#include <linux/etherdevice.h>
34#include <linux/delay.h>
35
36#include <linux/workqueue.h>
37
Johannes Berg2c8dccc2008-04-08 15:14:40 -040038#include "../net/mac80211/rate.h"
Zhu Yib481de92007-09-25 17:54:57 -070039
Christoph Hellwig5d08cd12007-10-25 17:15:50 +080040#include "iwl-4965.h"
Tomas Winkler857485c2008-03-21 13:53:44 -070041#include "iwl-core.h"
Zhu Yib481de92007-09-25 17:54:57 -070042#include "iwl-helpers.h"
43
44#define RS_NAME "iwl-4965-rs"
45
Guy Cohenaade00c2008-04-23 17:14:59 -070046#define NUM_TRY_BEFORE_ANT_TOGGLE 1
Zhu Yib481de92007-09-25 17:54:57 -070047#define IWL_NUMBER_TRY 1
48#define IWL_HT_NUMBER_TRY 3
49
Ben Cahill77626352007-11-29 11:09:44 +080050#define IWL_RATE_MAX_WINDOW 62 /* # tx in history window */
51#define IWL_RATE_MIN_FAILURE_TH 6 /* min failures to calc tpt */
52#define IWL_RATE_MIN_SUCCESS_TH 8 /* min successes to calc tpt */
53
54/* max time to accum history 2 seconds */
55#define IWL_RATE_SCALE_FLUSH_INTVL (2*HZ)
Zhu Yib481de92007-09-25 17:54:57 -070056
57static u8 rs_ht_to_legacy[] = {
58 IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
59 IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
60 IWL_RATE_6M_INDEX,
61 IWL_RATE_6M_INDEX, IWL_RATE_9M_INDEX,
62 IWL_RATE_12M_INDEX, IWL_RATE_18M_INDEX,
63 IWL_RATE_24M_INDEX, IWL_RATE_36M_INDEX,
64 IWL_RATE_48M_INDEX, IWL_RATE_54M_INDEX
65};
66
Guy Cohenaade00c2008-04-23 17:14:59 -070067static const u8 ant_toggle_lookup[] = {
68 /*ANT_NONE -> */ ANT_NONE,
69 /*ANT_A -> */ ANT_B,
70 /*ANT_B -> */ ANT_C,
71 /*ANT_AB -> */ ANT_BC,
72 /*ANT_C -> */ ANT_A,
73 /*ANT_AC -> */ ANT_AB,
74 /*ANT_BC -> */ ANT_AC,
75 /*ANT_ABC -> */ ANT_ABC,
76};
77
Ben Cahill77626352007-11-29 11:09:44 +080078/**
79 * struct iwl4965_rate_scale_data -- tx success history for one rate
80 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080081struct iwl4965_rate_scale_data {
Ben Cahill77626352007-11-29 11:09:44 +080082 u64 data; /* bitmap of successful frames */
83 s32 success_counter; /* number of frames successful */
84 s32 success_ratio; /* per-cent * 128 */
85 s32 counter; /* number of frames attempted */
86 s32 average_tpt; /* success ratio * expected throughput */
Zhu Yib481de92007-09-25 17:54:57 -070087 unsigned long stamp;
88};
89
Ben Cahill77626352007-11-29 11:09:44 +080090/**
91 * struct iwl4965_scale_tbl_info -- tx params and success history for all rates
92 *
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +020093 * There are two of these in struct iwl4965_lq_sta,
Ben Cahill77626352007-11-29 11:09:44 +080094 * one for "active", and one for "search".
95 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080096struct iwl4965_scale_tbl_info {
Guy Cohenfde0db32008-04-21 15:42:01 -070097 enum iwl_table_type lq_type;
98 u8 ant_type;
Ben Cahill77626352007-11-29 11:09:44 +080099 u8 is_SGI; /* 1 = short guard interval */
100 u8 is_fat; /* 1 = 40 MHz channel width */
101 u8 is_dup; /* 1 = duplicated data streams */
102 u8 action; /* change modulation; IWL_[LEGACY/SISO/MIMO]_SWITCH_* */
103 s32 *expected_tpt; /* throughput metrics; expected_tpt_G, etc. */
Guy Cohen39e88502008-04-23 17:14:57 -0700104 u32 current_rate; /* rate_n_flags, uCode API format */
Ben Cahill77626352007-11-29 11:09:44 +0800105 struct iwl4965_rate_scale_data win[IWL_RATE_COUNT]; /* rate histories */
Zhu Yib481de92007-09-25 17:54:57 -0700106};
107
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200108#ifdef CONFIG_IWL4965_HT
109
110struct iwl4965_traffic_load {
111 unsigned long time_stamp; /* age of the oldest statistics */
112 u32 packet_count[TID_QUEUE_MAX_SIZE]; /* packet count in this time
113 * slice */
114 u32 total; /* total num of packets during the
115 * last TID_MAX_TIME_DIFF */
116 u8 queue_count; /* number of queues that has
117 * been used since the last cleanup */
118 u8 head; /* start of the circular buffer */
119};
120
121#endif /* CONFIG_IWL4965_HT */
122
Ben Cahill77626352007-11-29 11:09:44 +0800123/**
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200124 * struct iwl4965_lq_sta -- driver's rate scaling private structure
Ben Cahill77626352007-11-29 11:09:44 +0800125 *
126 * Pointer to this gets passed back and forth between driver and mac80211.
127 */
Tomas Winklerc33104f2008-01-14 17:46:21 -0800128struct iwl4965_lq_sta {
Ben Cahill77626352007-11-29 11:09:44 +0800129 u8 active_tbl; /* index of active table, range 0-1 */
130 u8 enable_counter; /* indicates HT mode */
131 u8 stay_in_tbl; /* 1: disallow, 0: allow search for new mode */
132 u8 search_better_tbl; /* 1: currently trying alternate mode */
Zhu Yib481de92007-09-25 17:54:57 -0700133 s32 last_tpt;
Ben Cahill77626352007-11-29 11:09:44 +0800134
135 /* The following determine when to search for a new mode */
Zhu Yib481de92007-09-25 17:54:57 -0700136 u32 table_count_limit;
Ben Cahill77626352007-11-29 11:09:44 +0800137 u32 max_failure_limit; /* # failed frames before new search */
138 u32 max_success_limit; /* # successful frames before new search */
Zhu Yib481de92007-09-25 17:54:57 -0700139 u32 table_count;
Ben Cahill77626352007-11-29 11:09:44 +0800140 u32 total_failed; /* total failed frames, any/all rates */
141 u32 total_success; /* total successful frames, any/all rates */
142 u32 flush_timer; /* time staying in mode before new search */
143
144 u8 action_counter; /* # mode-switch actions tried */
Zhu Yib481de92007-09-25 17:54:57 -0700145 u8 is_green;
146 u8 is_dup;
Johannes Berg8318d782008-01-24 19:38:38 +0100147 enum ieee80211_band band;
Zhu Yib481de92007-09-25 17:54:57 -0700148 u8 ibss_sta_added;
Ben Cahill77626352007-11-29 11:09:44 +0800149
150 /* The following are bitmaps of rates; IWL_RATE_6M_MASK, etc. */
Zhu Yi02dede02007-09-27 11:27:40 +0800151 u32 supp_rates;
Guy Cohen39e88502008-04-23 17:14:57 -0700152 u16 active_legacy_rate;
Zhu Yib481de92007-09-25 17:54:57 -0700153 u16 active_siso_rate;
Guy Cohenfde0db32008-04-21 15:42:01 -0700154 u16 active_mimo2_rate;
155 u16 active_mimo3_rate;
Zhu Yib481de92007-09-25 17:54:57 -0700156 u16 active_rate_basic;
Ben Cahill77626352007-11-29 11:09:44 +0800157
Tomas Winkler66c73db2008-04-15 16:01:40 -0700158 struct iwl_link_quality_cmd lq;
Ben Cahill77626352007-11-29 11:09:44 +0800159 struct iwl4965_scale_tbl_info lq_info[LQ_SIZE]; /* "active", "search" */
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200160#ifdef CONFIG_IWL4965_HT
161 struct iwl4965_traffic_load load[TID_MAX_LOAD_COUNT];
162 u8 tx_agg_tid_en;
163#endif
Zhu Yi5ae212c2007-09-27 11:27:38 +0800164#ifdef CONFIG_MAC80211_DEBUGFS
Zhu Yi98d7e092007-09-27 11:27:42 +0800165 struct dentry *rs_sta_dbgfs_scale_table_file;
Zhu Yi0209dc12007-09-27 11:27:43 +0800166 struct dentry *rs_sta_dbgfs_stats_table_file;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200167#ifdef CONFIG_IWL4965_HT
168 struct dentry *rs_sta_dbgfs_tx_agg_tid_en_file;
169#endif
Guy Cohen39e88502008-04-23 17:14:57 -0700170 u32 dbg_fixed_rate;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700171 struct iwl_priv *drv;
Zhu Yi5ae212c2007-09-27 11:27:38 +0800172#endif
Zhu Yib481de92007-09-25 17:54:57 -0700173};
174
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700175static void rs_rate_scale_perform(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -0700176 struct net_device *dev,
177 struct ieee80211_hdr *hdr,
178 struct sta_info *sta);
Guy Cohenfde0db32008-04-21 15:42:01 -0700179static void rs_fill_link_cmd(const struct iwl_priv *priv,
180 struct iwl4965_lq_sta *lq_sta,
Guy Cohen39e88502008-04-23 17:14:57 -0700181 u32 rate_n_flags,
Tomas Winkler66c73db2008-04-15 16:01:40 -0700182 struct iwl_link_quality_cmd *tbl);
Zhu Yib481de92007-09-25 17:54:57 -0700183
184
Zhu Yi98d7e092007-09-27 11:27:42 +0800185#ifdef CONFIG_MAC80211_DEBUGFS
Tomas Winklerc33104f2008-01-14 17:46:21 -0800186static void rs_dbgfs_set_mcs(struct iwl4965_lq_sta *lq_sta,
Guy Cohen39e88502008-04-23 17:14:57 -0700187 u32 *rate_n_flags, int index);
Zhu Yi98d7e092007-09-27 11:27:42 +0800188#else
Tomas Winklerc33104f2008-01-14 17:46:21 -0800189static void rs_dbgfs_set_mcs(struct iwl4965_lq_sta *lq_sta,
Guy Cohen39e88502008-04-23 17:14:57 -0700190 u32 *rate_n_flags, int index)
Zhu Yi98d7e092007-09-27 11:27:42 +0800191{}
192#endif
Ben Cahill77626352007-11-29 11:09:44 +0800193
194/*
195 * Expected throughput metrics for following rates:
196 * 1, 2, 5.5, 11, 6, 9, 12, 18, 24, 36, 48, 54, 60 MBits
197 * "G" is the only table that supports CCK (the first 4 rates).
198 */
Guy Cohenfde0db32008-04-21 15:42:01 -0700199/*FIXME:RS:need to spearate tables for MIMO2/MIMO3*/
Zhu Yib481de92007-09-25 17:54:57 -0700200static s32 expected_tpt_A[IWL_RATE_COUNT] = {
201 0, 0, 0, 0, 40, 57, 72, 98, 121, 154, 177, 186, 186
202};
203
204static s32 expected_tpt_G[IWL_RATE_COUNT] = {
205 7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 186
206};
207
208static s32 expected_tpt_siso20MHz[IWL_RATE_COUNT] = {
209 0, 0, 0, 0, 42, 42, 76, 102, 124, 159, 183, 193, 202
210};
211
212static s32 expected_tpt_siso20MHzSGI[IWL_RATE_COUNT] = {
213 0, 0, 0, 0, 46, 46, 82, 110, 132, 168, 192, 202, 211
214};
215
216static s32 expected_tpt_mimo20MHz[IWL_RATE_COUNT] = {
217 0, 0, 0, 0, 74, 74, 123, 155, 179, 214, 236, 244, 251
218};
219
220static s32 expected_tpt_mimo20MHzSGI[IWL_RATE_COUNT] = {
221 0, 0, 0, 0, 81, 81, 131, 164, 188, 222, 243, 251, 257
222};
223
224static s32 expected_tpt_siso40MHz[IWL_RATE_COUNT] = {
225 0, 0, 0, 0, 77, 77, 127, 160, 184, 220, 242, 250, 257
226};
227
228static s32 expected_tpt_siso40MHzSGI[IWL_RATE_COUNT] = {
229 0, 0, 0, 0, 83, 83, 135, 169, 193, 229, 250, 257, 264
230};
231
232static s32 expected_tpt_mimo40MHz[IWL_RATE_COUNT] = {
233 0, 0, 0, 0, 123, 123, 182, 214, 235, 264, 279, 285, 289
234};
235
236static s32 expected_tpt_mimo40MHzSGI[IWL_RATE_COUNT] = {
237 0, 0, 0, 0, 131, 131, 191, 222, 242, 270, 284, 289, 293
238};
239
Guy Cohen39e88502008-04-23 17:14:57 -0700240static inline u8 rs_extract_rate(u32 rate_n_flags)
Zhu Yib481de92007-09-25 17:54:57 -0700241{
242 return (u8)(rate_n_flags & 0xFF);
243}
244
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800245static void rs_rate_scale_clear_window(struct iwl4965_rate_scale_data *window)
Zhu Yib481de92007-09-25 17:54:57 -0700246{
247 window->data = 0;
248 window->success_counter = 0;
249 window->success_ratio = IWL_INVALID_VALUE;
250 window->counter = 0;
251 window->average_tpt = IWL_INVALID_VALUE;
252 window->stamp = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700253}
254
Guy Cohenaade00c2008-04-23 17:14:59 -0700255static inline u8 rs_is_valid_ant(u8 valid_antenna, u8 ant_type)
256{
257 return ((ant_type & valid_antenna) == ant_type);
258}
259
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200260#ifdef CONFIG_IWL4965_HT
261/*
262 * removes the old data from the statistics. All data that is older than
263 * TID_MAX_TIME_DIFF, will be deleted.
264 */
265static void rs_tl_rm_old_stats(struct iwl4965_traffic_load *tl, u32 curr_time)
266{
267 /* The oldest age we want to keep */
268 u32 oldest_time = curr_time - TID_MAX_TIME_DIFF;
269
270 while (tl->queue_count &&
271 (tl->time_stamp < oldest_time)) {
272 tl->total -= tl->packet_count[tl->head];
273 tl->packet_count[tl->head] = 0;
274 tl->time_stamp += TID_QUEUE_CELL_SPACING;
275 tl->queue_count--;
276 tl->head++;
277 if (tl->head >= TID_QUEUE_MAX_SIZE)
278 tl->head = 0;
279 }
280}
281
282/*
283 * increment traffic load value for tid and also remove
284 * any old values if passed the certain time period
285 */
286static void rs_tl_add_packet(struct iwl4965_lq_sta *lq_data, u8 tid)
287{
288 u32 curr_time = jiffies_to_msecs(jiffies);
289 u32 time_diff;
290 s32 index;
291 struct iwl4965_traffic_load *tl = NULL;
292
293 if (tid >= TID_MAX_LOAD_COUNT)
294 return;
295
296 tl = &lq_data->load[tid];
297
298 curr_time -= curr_time % TID_ROUND_VALUE;
299
300 /* Happens only for the first packet. Initialize the data */
301 if (!(tl->queue_count)) {
302 tl->total = 1;
303 tl->time_stamp = curr_time;
304 tl->queue_count = 1;
305 tl->head = 0;
306 tl->packet_count[0] = 1;
307 return;
308 }
309
310 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
311 index = time_diff / TID_QUEUE_CELL_SPACING;
312
313 /* The history is too long: remove data that is older than */
314 /* TID_MAX_TIME_DIFF */
315 if (index >= TID_QUEUE_MAX_SIZE)
316 rs_tl_rm_old_stats(tl, curr_time);
317
318 index = (tl->head + index) % TID_QUEUE_MAX_SIZE;
319 tl->packet_count[index] = tl->packet_count[index] + 1;
320 tl->total = tl->total + 1;
321
322 if ((index + 1) > tl->queue_count)
323 tl->queue_count = index + 1;
324}
325
326/*
327 get the traffic load value for tid
328*/
329static u32 rs_tl_get_load(struct iwl4965_lq_sta *lq_data, u8 tid)
330{
331 u32 curr_time = jiffies_to_msecs(jiffies);
332 u32 time_diff;
333 s32 index;
334 struct iwl4965_traffic_load *tl = NULL;
335
336 if (tid >= TID_MAX_LOAD_COUNT)
337 return 0;
338
339 tl = &(lq_data->load[tid]);
340
341 curr_time -= curr_time % TID_ROUND_VALUE;
342
343 if (!(tl->queue_count))
344 return 0;
345
346 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
347 index = time_diff / TID_QUEUE_CELL_SPACING;
348
349 /* The history is too long: remove data that is older than */
350 /* TID_MAX_TIME_DIFF */
351 if (index >= TID_QUEUE_MAX_SIZE)
352 rs_tl_rm_old_stats(tl, curr_time);
353
354 return tl->total;
355}
356
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700357static void rs_tl_turn_on_agg_for_tid(struct iwl_priv *priv,
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200358 struct iwl4965_lq_sta *lq_data, u8 tid,
359 struct sta_info *sta)
360{
361 unsigned long state;
362 DECLARE_MAC_BUF(mac);
363
364 spin_lock_bh(&sta->ampdu_mlme.ampdu_tx);
Ron Rindjunskycee24a32008-03-26 20:36:03 +0200365 state = sta->ampdu_mlme.tid_state_tx[tid];
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200366 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
367
368 if (state == HT_AGG_STATE_IDLE &&
369 rs_tl_get_load(lq_data, tid) > IWL_AGG_LOAD_THRESHOLD) {
370 IWL_DEBUG_HT("Starting Tx agg: STA: %s tid: %d\n",
371 print_mac(mac, sta->addr), tid);
372 ieee80211_start_tx_ba_session(priv->hw, sta->addr, tid);
373 }
374}
375
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700376static void rs_tl_turn_on_agg(struct iwl_priv *priv, u8 tid,
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200377 struct iwl4965_lq_sta *lq_data,
378 struct sta_info *sta)
379{
380 if ((tid < TID_MAX_LOAD_COUNT))
381 rs_tl_turn_on_agg_for_tid(priv, lq_data, tid, sta);
382 else if (tid == IWL_AGG_ALL_TID)
383 for (tid = 0; tid < TID_MAX_LOAD_COUNT; tid++)
384 rs_tl_turn_on_agg_for_tid(priv, lq_data, tid, sta);
385}
386
387#endif /* CONFIG_IWLWIFI_HT */
388
Guy Cohen39e88502008-04-23 17:14:57 -0700389static inline int get_num_of_ant_from_rate(u32 rate_n_flags)
Guy Cohenfde0db32008-04-21 15:42:01 -0700390{
Guy Cohen39e88502008-04-23 17:14:57 -0700391 return (!!(rate_n_flags & RATE_MCS_ANT_A_MSK) +
392 !!(rate_n_flags & RATE_MCS_ANT_B_MSK) +
393 !!(rate_n_flags & RATE_MCS_ANT_C_MSK));
Guy Cohenfde0db32008-04-21 15:42:01 -0700394}
395
Ben Cahill77626352007-11-29 11:09:44 +0800396/**
397 * rs_collect_tx_data - Update the success/failure sliding window
398 *
399 * We keep a sliding window of the last 62 packets transmitted
400 * at this rate. window->data contains the bitmask of successful
401 * packets.
402 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800403static int rs_collect_tx_data(struct iwl4965_rate_scale_data *windows,
Ron Rindjunsky99556432008-01-28 14:07:25 +0200404 int scale_index, s32 tpt, int retries,
405 int successes)
Zhu Yib481de92007-09-25 17:54:57 -0700406{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800407 struct iwl4965_rate_scale_data *window = NULL;
Guy Cohen39e88502008-04-23 17:14:57 -0700408 static const u64 mask = (((u64)1) << (IWL_RATE_MAX_WINDOW - 1));
Zhu Yib481de92007-09-25 17:54:57 -0700409 s32 fail_count;
410
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800411 if (scale_index < 0 || scale_index >= IWL_RATE_COUNT)
412 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -0700413
Ben Cahill77626352007-11-29 11:09:44 +0800414 /* Select data for current tx bit rate */
Zhu Yib481de92007-09-25 17:54:57 -0700415 window = &(windows[scale_index]);
416
Ben Cahill77626352007-11-29 11:09:44 +0800417 /*
418 * Keep track of only the latest 62 tx frame attempts in this rate's
419 * history window; anything older isn't really relevant any more.
420 * If we have filled up the sliding window, drop the oldest attempt;
421 * if the oldest attempt (highest bit in bitmap) shows "success",
422 * subtract "1" from the success counter (this is the main reason
423 * we keep these bitmaps!).
424 */
Ron Rindjunsky99556432008-01-28 14:07:25 +0200425 while (retries > 0) {
Guy Cohen39e88502008-04-23 17:14:57 -0700426 if (window->counter >= IWL_RATE_MAX_WINDOW) {
427
428 /* remove earliest */
429 window->counter = IWL_RATE_MAX_WINDOW - 1;
430
Ron Rindjunsky99556432008-01-28 14:07:25 +0200431 if (window->data & mask) {
432 window->data &= ~mask;
Guy Cohen39e88502008-04-23 17:14:57 -0700433 window->success_counter--;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200434 }
Zhu Yib481de92007-09-25 17:54:57 -0700435 }
Zhu Yib481de92007-09-25 17:54:57 -0700436
Ron Rindjunsky99556432008-01-28 14:07:25 +0200437 /* Increment frames-attempted counter */
438 window->counter++;
Ben Cahill77626352007-11-29 11:09:44 +0800439
Ron Rindjunsky99556432008-01-28 14:07:25 +0200440 /* Shift bitmap by one frame (throw away oldest history),
441 * OR in "1", and increment "success" if this
442 * frame was successful. */
Guy Cohen39e88502008-04-23 17:14:57 -0700443 window->data <<= 1;;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200444 if (successes > 0) {
Guy Cohen39e88502008-04-23 17:14:57 -0700445 window->success_counter++;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200446 window->data |= 0x1;
447 successes--;
448 }
449
450 retries--;
Zhu Yib481de92007-09-25 17:54:57 -0700451 }
452
Ben Cahill77626352007-11-29 11:09:44 +0800453 /* Calculate current success ratio, avoid divide-by-0! */
Zhu Yib481de92007-09-25 17:54:57 -0700454 if (window->counter > 0)
455 window->success_ratio = 128 * (100 * window->success_counter)
456 / window->counter;
457 else
458 window->success_ratio = IWL_INVALID_VALUE;
459
460 fail_count = window->counter - window->success_counter;
461
Ben Cahill77626352007-11-29 11:09:44 +0800462 /* Calculate average throughput, if we have enough history. */
Zhu Yib481de92007-09-25 17:54:57 -0700463 if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
464 (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
465 window->average_tpt = (window->success_ratio * tpt + 64) / 128;
466 else
467 window->average_tpt = IWL_INVALID_VALUE;
468
Ben Cahill77626352007-11-29 11:09:44 +0800469 /* Tag this window as having been updated */
Zhu Yib481de92007-09-25 17:54:57 -0700470 window->stamp = jiffies;
471
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800472 return 0;
Zhu Yib481de92007-09-25 17:54:57 -0700473}
474
Ben Cahill77626352007-11-29 11:09:44 +0800475/*
476 * Fill uCode API rate_n_flags field, based on "search" or "active" table.
477 */
Guy Cohen39e88502008-04-23 17:14:57 -0700478/* FIXME:RS:remove this function and put the flags statically in the table */
479static u32 rate_n_flags_from_tbl(struct iwl4965_scale_tbl_info *tbl,
480 int index, u8 use_green)
Zhu Yib481de92007-09-25 17:54:57 -0700481{
Guy Cohen39e88502008-04-23 17:14:57 -0700482 u32 rate_n_flags = 0;
483
Zhu Yib481de92007-09-25 17:54:57 -0700484 if (is_legacy(tbl->lq_type)) {
Guy Cohen39e88502008-04-23 17:14:57 -0700485 rate_n_flags = iwl4965_rates[index].plcp;
Zhu Yib481de92007-09-25 17:54:57 -0700486 if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE)
Guy Cohen39e88502008-04-23 17:14:57 -0700487 rate_n_flags |= RATE_MCS_CCK_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700488
Guy Cohenfde0db32008-04-21 15:42:01 -0700489 } else if (is_Ht(tbl->lq_type)) {
490 if (index > IWL_LAST_OFDM_RATE) {
491 IWL_ERROR("invalid HT rate index %d\n", index);
Zhu Yib481de92007-09-25 17:54:57 -0700492 index = IWL_LAST_OFDM_RATE;
Guy Cohenfde0db32008-04-21 15:42:01 -0700493 }
Guy Cohen39e88502008-04-23 17:14:57 -0700494 rate_n_flags = RATE_MCS_HT_MSK;
Guy Cohenfde0db32008-04-21 15:42:01 -0700495
496 if (is_siso(tbl->lq_type))
Guy Cohen39e88502008-04-23 17:14:57 -0700497 rate_n_flags |= iwl4965_rates[index].plcp_siso;
Guy Cohenfde0db32008-04-21 15:42:01 -0700498 else if (is_mimo2(tbl->lq_type))
Guy Cohen39e88502008-04-23 17:14:57 -0700499 rate_n_flags |= iwl4965_rates[index].plcp_mimo2;
Guy Cohenfde0db32008-04-21 15:42:01 -0700500 else
Guy Cohen39e88502008-04-23 17:14:57 -0700501 rate_n_flags |= iwl4965_rates[index].plcp_mimo3;
Zhu Yib481de92007-09-25 17:54:57 -0700502 } else {
Guy Cohenfde0db32008-04-21 15:42:01 -0700503 IWL_ERROR("Invalid tbl->lq_type %d\n", tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -0700504 }
505
Guy Cohen39e88502008-04-23 17:14:57 -0700506 rate_n_flags |= ((tbl->ant_type << RATE_MCS_ANT_POS) &
Guy Cohenfde0db32008-04-21 15:42:01 -0700507 RATE_MCS_ANT_ABC_MSK);
Zhu Yib481de92007-09-25 17:54:57 -0700508
Guy Cohen39e88502008-04-23 17:14:57 -0700509 if (is_Ht(tbl->lq_type)) {
510 if (tbl->is_fat) {
511 if (tbl->is_dup)
512 rate_n_flags |= RATE_MCS_DUP_MSK;
513 else
514 rate_n_flags |= RATE_MCS_FAT_MSK;
515 }
516 if (tbl->is_SGI)
517 rate_n_flags |= RATE_MCS_SGI_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700518
Guy Cohen39e88502008-04-23 17:14:57 -0700519 if (use_green) {
520 rate_n_flags |= RATE_MCS_GF_MSK;
521 if (is_siso(tbl->lq_type) && tbl->is_SGI) {
522 rate_n_flags &= ~RATE_MCS_SGI_MSK;
523 IWL_ERROR("GF was set with SGI:SISO\n");
524 }
525 }
Zhu Yib481de92007-09-25 17:54:57 -0700526 }
Guy Cohen39e88502008-04-23 17:14:57 -0700527 return rate_n_flags;
Zhu Yib481de92007-09-25 17:54:57 -0700528}
529
Ben Cahill77626352007-11-29 11:09:44 +0800530/*
531 * Interpret uCode API's rate_n_flags format,
532 * fill "search" or "active" tx mode table.
533 */
Guy Cohen39e88502008-04-23 17:14:57 -0700534static int rs_get_tbl_info_from_mcs(const u32 rate_n_flags,
Johannes Berg8318d782008-01-24 19:38:38 +0100535 enum ieee80211_band band,
536 struct iwl4965_scale_tbl_info *tbl,
Zhu Yib481de92007-09-25 17:54:57 -0700537 int *rate_idx)
538{
Guy Cohen39e88502008-04-23 17:14:57 -0700539 u32 ant_msk = (rate_n_flags & RATE_MCS_ANT_ABC_MSK);
540 u8 num_of_ant = get_num_of_ant_from_rate(rate_n_flags);
541 u8 mcs;
Zhu Yib481de92007-09-25 17:54:57 -0700542
Guy Cohen39e88502008-04-23 17:14:57 -0700543 *rate_idx = iwl4965_hwrate_to_plcp_idx(rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -0700544
Guy Cohenfde0db32008-04-21 15:42:01 -0700545 if (*rate_idx == IWL_RATE_INVALID) {
Zhu Yib481de92007-09-25 17:54:57 -0700546 *rate_idx = -1;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800547 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -0700548 }
Ben Cahill77626352007-11-29 11:09:44 +0800549 tbl->is_SGI = 0; /* default legacy setup */
Zhu Yib481de92007-09-25 17:54:57 -0700550 tbl->is_fat = 0;
551 tbl->is_dup = 0;
Guy Cohenfde0db32008-04-21 15:42:01 -0700552 tbl->ant_type = (ant_msk >> RATE_MCS_ANT_POS);
553 tbl->lq_type = LQ_NONE;
Zhu Yib481de92007-09-25 17:54:57 -0700554
Ben Cahill77626352007-11-29 11:09:44 +0800555 /* legacy rate format */
Guy Cohen39e88502008-04-23 17:14:57 -0700556 if (!(rate_n_flags & RATE_MCS_HT_MSK)) {
Guy Cohenfde0db32008-04-21 15:42:01 -0700557 if (num_of_ant == 1) {
Johannes Berg8318d782008-01-24 19:38:38 +0100558 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700559 tbl->lq_type = LQ_A;
560 else
561 tbl->lq_type = LQ_G;
Zhu Yib481de92007-09-25 17:54:57 -0700562 }
Guy Cohenfde0db32008-04-21 15:42:01 -0700563 /* HT rate format */
Zhu Yib481de92007-09-25 17:54:57 -0700564 } else {
Guy Cohen39e88502008-04-23 17:14:57 -0700565 if (rate_n_flags & RATE_MCS_SGI_MSK)
Zhu Yib481de92007-09-25 17:54:57 -0700566 tbl->is_SGI = 1;
567
Guy Cohen39e88502008-04-23 17:14:57 -0700568 if ((rate_n_flags & RATE_MCS_FAT_MSK) ||
569 (rate_n_flags & RATE_MCS_DUP_MSK))
Zhu Yib481de92007-09-25 17:54:57 -0700570 tbl->is_fat = 1;
571
Guy Cohen39e88502008-04-23 17:14:57 -0700572 if (rate_n_flags & RATE_MCS_DUP_MSK)
Zhu Yib481de92007-09-25 17:54:57 -0700573 tbl->is_dup = 1;
Guy Cohenfde0db32008-04-21 15:42:01 -0700574
Guy Cohen39e88502008-04-23 17:14:57 -0700575 mcs = rs_extract_rate(rate_n_flags);
Guy Cohenfde0db32008-04-21 15:42:01 -0700576
Guy Cohen39e88502008-04-23 17:14:57 -0700577 /* SISO */
578 if (mcs <= IWL_RATE_SISO_60M_PLCP) {
Guy Cohenfde0db32008-04-21 15:42:01 -0700579 if (num_of_ant == 1)
580 tbl->lq_type = LQ_SISO; /*else NONE*/
581 /* MIMO2 */
Guy Cohen39e88502008-04-23 17:14:57 -0700582 } else if (mcs <= IWL_RATE_MIMO2_60M_PLCP) {
Guy Cohenfde0db32008-04-21 15:42:01 -0700583 if (num_of_ant == 2)
584 tbl->lq_type = LQ_MIMO2;
585 /* MIMO3 */
586 } else {
587 if (num_of_ant == 3)
588 tbl->lq_type = LQ_MIMO3;
589 }
Zhu Yib481de92007-09-25 17:54:57 -0700590 }
591 return 0;
592}
Guy Cohenaade00c2008-04-23 17:14:59 -0700593
594/* switch to another antenna/antennas and return 1 */
595/* if no other valid antenna found, return 0 */
596static int rs_toggle_antenna(u32 valid_ant, u32 *rate_n_flags,
597 struct iwl4965_scale_tbl_info *tbl)
Zhu Yib481de92007-09-25 17:54:57 -0700598{
Guy Cohenaade00c2008-04-23 17:14:59 -0700599 u8 new_ant_type;
600
601 if (!tbl->ant_type || tbl->ant_type > ANT_ABC)
602 return 0;
603
604 if (!rs_is_valid_ant(valid_ant, tbl->ant_type))
605 return 0;
606
607 new_ant_type = ant_toggle_lookup[tbl->ant_type];
608
609 while ((new_ant_type != tbl->ant_type) &&
610 !rs_is_valid_ant(valid_ant, new_ant_type))
611 new_ant_type = ant_toggle_lookup[new_ant_type];
612
613 if (new_ant_type == tbl->ant_type)
614 return 0;
615
616 tbl->ant_type = new_ant_type;
617 *rate_n_flags &= ~RATE_MCS_ANT_ABC_MSK;
618 *rate_n_flags |= new_ant_type << RATE_MCS_ANT_POS;
619 return 1;
Zhu Yib481de92007-09-25 17:54:57 -0700620}
621
Guy Cohen39e88502008-04-23 17:14:57 -0700622/* FIXME:RS: in 4965 we don't use greenfield at all */
Guy Cohenf935a6d2008-04-23 17:15:02 -0700623/* FIXME:RS: don't use greenfield for now in TX */
624/* #ifdef CONFIG_IWL4965_HT */
625#if 0
626static inline u8 rs_use_green(struct iwl_priv *priv, struct ieee80211_conf *conf)
Zhu Yib481de92007-09-25 17:54:57 -0700627{
Ron Rindjunsky270243a2007-11-26 16:14:41 +0200628 return ((conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) &&
629 priv->current_ht_config.is_green_field &&
630 !priv->current_ht_config.non_GF_STA_present);
Zhu Yib481de92007-09-25 17:54:57 -0700631}
Guy Cohenf935a6d2008-04-23 17:15:02 -0700632#else
633static inline u8 rs_use_green(struct iwl_priv *priv, struct ieee80211_conf *conf)
634{
635 return 0;
636}
637#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -0700638
639/**
640 * rs_get_supported_rates - get the available rates
641 *
642 * if management frame or broadcast frame only return
643 * basic available rates.
644 *
645 */
Guy Coheneecd6e52008-04-23 17:14:58 -0700646static u16 rs_get_supported_rates(struct iwl4965_lq_sta *lq_sta,
Zhu Yib481de92007-09-25 17:54:57 -0700647 struct ieee80211_hdr *hdr,
Guy Coheneecd6e52008-04-23 17:14:58 -0700648 enum iwl_table_type rate_type)
Zhu Yib481de92007-09-25 17:54:57 -0700649{
Zhu Yib481de92007-09-25 17:54:57 -0700650 if (hdr && is_multicast_ether_addr(hdr->addr1) &&
Guy Coheneecd6e52008-04-23 17:14:58 -0700651 lq_sta->active_rate_basic)
652 return lq_sta->active_rate_basic;
653
654 if (is_legacy(rate_type)) {
655 return lq_sta->active_legacy_rate;
656 } else {
657 if (is_siso(rate_type))
658 return lq_sta->active_siso_rate;
659 else if (is_mimo2(rate_type))
660 return lq_sta->active_mimo2_rate;
661 else
662 return lq_sta->active_mimo3_rate;
Tomas Winklerc33104f2008-01-14 17:46:21 -0800663 }
Zhu Yib481de92007-09-25 17:54:57 -0700664}
665
666static u16 rs_get_adjacent_rate(u8 index, u16 rate_mask, int rate_type)
667{
668 u8 high = IWL_RATE_INVALID;
669 u8 low = IWL_RATE_INVALID;
670
Ian Schram01ebd062007-10-25 17:15:22 +0800671 /* 802.11A or ht walks to the next literal adjacent rate in
Zhu Yib481de92007-09-25 17:54:57 -0700672 * the rate table */
673 if (is_a_band(rate_type) || !is_legacy(rate_type)) {
674 int i;
675 u32 mask;
676
677 /* Find the previous rate that is in the rate mask */
678 i = index - 1;
679 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
680 if (rate_mask & mask) {
681 low = i;
682 break;
683 }
684 }
685
686 /* Find the next rate that is in the rate mask */
687 i = index + 1;
688 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
689 if (rate_mask & mask) {
690 high = i;
691 break;
692 }
693 }
694
695 return (high << 8) | low;
696 }
697
698 low = index;
699 while (low != IWL_RATE_INVALID) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800700 low = iwl4965_rates[low].prev_rs;
Zhu Yib481de92007-09-25 17:54:57 -0700701 if (low == IWL_RATE_INVALID)
702 break;
703 if (rate_mask & (1 << low))
704 break;
705 IWL_DEBUG_RATE("Skipping masked lower rate: %d\n", low);
706 }
707
708 high = index;
709 while (high != IWL_RATE_INVALID) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800710 high = iwl4965_rates[high].next_rs;
Zhu Yib481de92007-09-25 17:54:57 -0700711 if (high == IWL_RATE_INVALID)
712 break;
713 if (rate_mask & (1 << high))
714 break;
715 IWL_DEBUG_RATE("Skipping masked higher rate: %d\n", high);
716 }
717
718 return (high << 8) | low;
719}
720
Guy Cohen39e88502008-04-23 17:14:57 -0700721static u32 rs_get_lower_rate(struct iwl4965_lq_sta *lq_sta,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800722 struct iwl4965_scale_tbl_info *tbl, u8 scale_index,
Guy Cohen39e88502008-04-23 17:14:57 -0700723 u8 ht_possible)
Zhu Yib481de92007-09-25 17:54:57 -0700724{
Zhu Yib481de92007-09-25 17:54:57 -0700725 s32 low;
726 u16 rate_mask;
727 u16 high_low;
728 u8 switch_to_legacy = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -0800729 u8 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -0700730
731 /* check if we need to switch from HT to legacy rates.
732 * assumption is that mandatory rates (1Mbps or 6Mbps)
733 * are always supported (spec demand) */
734 if (!is_legacy(tbl->lq_type) && (!ht_possible || !scale_index)) {
735 switch_to_legacy = 1;
736 scale_index = rs_ht_to_legacy[scale_index];
Johannes Berg8318d782008-01-24 19:38:38 +0100737 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700738 tbl->lq_type = LQ_A;
739 else
740 tbl->lq_type = LQ_G;
741
Guy Cohen69fdb302008-04-23 17:15:01 -0700742 if (num_of_ant(tbl->ant_type) > 1)
Guy Cohenfde0db32008-04-21 15:42:01 -0700743 tbl->ant_type = ANT_A;/*FIXME:RS*/
Zhu Yib481de92007-09-25 17:54:57 -0700744
745 tbl->is_fat = 0;
746 tbl->is_SGI = 0;
747 }
748
Guy Coheneecd6e52008-04-23 17:14:58 -0700749 rate_mask = rs_get_supported_rates(lq_sta, NULL, tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -0700750
Ben Cahill77626352007-11-29 11:09:44 +0800751 /* Mask with station rate restriction */
Zhu Yib481de92007-09-25 17:54:57 -0700752 if (is_legacy(tbl->lq_type)) {
Ben Cahill77626352007-11-29 11:09:44 +0800753 /* supp_rates has no CCK bits in A mode */
Johannes Berg8318d782008-01-24 19:38:38 +0100754 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700755 rate_mask = (u16)(rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -0800756 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
Zhu Yib481de92007-09-25 17:54:57 -0700757 else
Tomas Winklerc33104f2008-01-14 17:46:21 -0800758 rate_mask = (u16)(rate_mask & lq_sta->supp_rates);
Zhu Yib481de92007-09-25 17:54:57 -0700759 }
760
Ben Cahill77626352007-11-29 11:09:44 +0800761 /* If we switched from HT to legacy, check current rate */
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800762 if (switch_to_legacy && (rate_mask & (1 << scale_index))) {
Guy Cohen39e88502008-04-23 17:14:57 -0700763 low = scale_index;
764 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700765 }
766
767 high_low = rs_get_adjacent_rate(scale_index, rate_mask, tbl->lq_type);
768 low = high_low & 0xff;
769
Guy Cohen39e88502008-04-23 17:14:57 -0700770 if (low == IWL_RATE_INVALID)
771 low = scale_index;
772
773out:
774 return rate_n_flags_from_tbl(tbl, low, is_green);
Zhu Yib481de92007-09-25 17:54:57 -0700775}
776
Ben Cahill77626352007-11-29 11:09:44 +0800777/*
778 * mac80211 sends us Tx status
779 */
Tomas Winklerc33104f2008-01-14 17:46:21 -0800780static void rs_tx_status(void *priv_rate, struct net_device *dev,
Zhu Yib481de92007-09-25 17:54:57 -0700781 struct sk_buff *skb,
782 struct ieee80211_tx_status *tx_resp)
783{
784 int status;
785 u8 retries;
786 int rs_index, index = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -0800787 struct iwl4965_lq_sta *lq_sta;
Tomas Winkler66c73db2008-04-15 16:01:40 -0700788 struct iwl_link_quality_cmd *table;
Zhu Yib481de92007-09-25 17:54:57 -0700789 struct sta_info *sta;
790 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700791 struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
Zhu Yib481de92007-09-25 17:54:57 -0700792 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800793 struct ieee80211_hw *hw = local_to_hw(local);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800794 struct iwl4965_rate_scale_data *window = NULL;
795 struct iwl4965_rate_scale_data *search_win = NULL;
Guy Cohen39e88502008-04-23 17:14:57 -0700796 u32 tx_rate;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800797 struct iwl4965_scale_tbl_info tbl_type;
798 struct iwl4965_scale_tbl_info *curr_tbl, *search_tbl;
Zhu Yib481de92007-09-25 17:54:57 -0700799 u8 active_index = 0;
800 u16 fc = le16_to_cpu(hdr->frame_control);
801 s32 tpt = 0;
802
Zhu Yi58826352007-09-27 11:27:39 +0800803 IWL_DEBUG_RATE_LIMIT("get frame ack response, update rate scale window\n");
Zhu Yib481de92007-09-25 17:54:57 -0700804
805 if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1))
806 return;
807
Ron Rindjunsky99556432008-01-28 14:07:25 +0200808 /* This packet was aggregated but doesn't carry rate scale info */
809 if ((tx_resp->control.flags & IEEE80211_TXCTL_AMPDU) &&
810 !(tx_resp->flags & IEEE80211_TX_STATUS_AMPDU))
811 return;
812
Zhu Yib481de92007-09-25 17:54:57 -0700813 retries = tx_resp->retry_count;
814
815 if (retries > 15)
816 retries = 15;
817
Johannes Bergd0709a62008-02-25 16:27:46 +0100818 rcu_read_lock();
Zhu Yib481de92007-09-25 17:54:57 -0700819
820 sta = sta_info_get(local, hdr->addr1);
821
Tomas Winklerf4d60822008-03-05 11:31:00 -0800822 if (!sta || !sta->rate_ctrl_priv)
823 goto out;
824
Zhu Yib481de92007-09-25 17:54:57 -0700825
Tomas Winklerc33104f2008-01-14 17:46:21 -0800826 lq_sta = (struct iwl4965_lq_sta *)sta->rate_ctrl_priv;
Zhu Yib481de92007-09-25 17:54:57 -0700827
828 if (!priv->lq_mngr.lq_ready)
Tomas Winklerf4d60822008-03-05 11:31:00 -0800829 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700830
Tomas Winklerc33104f2008-01-14 17:46:21 -0800831 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
832 !lq_sta->ibss_sta_added)
Tomas Winklerf4d60822008-03-05 11:31:00 -0800833 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700834
Tomas Winklerc33104f2008-01-14 17:46:21 -0800835 table = &lq_sta->lq;
836 active_index = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -0700837
Tomas Winklerc33104f2008-01-14 17:46:21 -0800838 curr_tbl = &(lq_sta->lq_info[active_index]);
839 search_tbl = &(lq_sta->lq_info[(1 - active_index)]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800840 window = (struct iwl4965_rate_scale_data *)
Zhu Yib481de92007-09-25 17:54:57 -0700841 &(curr_tbl->win[0]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800842 search_win = (struct iwl4965_rate_scale_data *)
Zhu Yib481de92007-09-25 17:54:57 -0700843 &(search_tbl->win[0]);
844
Ben Cahill77626352007-11-29 11:09:44 +0800845 /*
846 * Ignore this Tx frame response if its initial rate doesn't match
847 * that of latest Link Quality command. There may be stragglers
848 * from a previous Link Quality command, but we're no longer interested
849 * in those; they're either from the "active" mode while we're trying
850 * to check "search" mode, or a prior "search" mode after we've moved
851 * to a new "search" mode (which might become the new "active" mode).
852 */
Guy Cohen39e88502008-04-23 17:14:57 -0700853 tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
854 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type, &rs_index);
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800855 if (priv->band == IEEE80211_BAND_5GHZ)
856 rs_index -= IWL_FIRST_OFDM_RATE;
857
858 if ((tx_resp->control.tx_rate == NULL) ||
859 (tbl_type.is_SGI ^
860 !!(tx_resp->control.flags & IEEE80211_TXCTL_SHORT_GI)) ||
861 (tbl_type.is_fat ^
862 !!(tx_resp->control.flags & IEEE80211_TXCTL_40_MHZ_WIDTH)) ||
863 (tbl_type.is_dup ^
864 !!(tx_resp->control.flags & IEEE80211_TXCTL_DUP_DATA)) ||
Guy Cohenfde0db32008-04-21 15:42:01 -0700865 (tbl_type.ant_type ^ tx_resp->control.antenna_sel_tx) ||
Guy Cohen39e88502008-04-23 17:14:57 -0700866 (!!(tx_rate & RATE_MCS_HT_MSK) ^
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800867 !!(tx_resp->control.flags & IEEE80211_TXCTL_OFDM_HT)) ||
Guy Cohen39e88502008-04-23 17:14:57 -0700868 (!!(tx_rate & RATE_MCS_GF_MSK) ^
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800869 !!(tx_resp->control.flags & IEEE80211_TXCTL_GREEN_FIELD)) ||
870 (hw->wiphy->bands[priv->band]->bitrates[rs_index].bitrate !=
871 tx_resp->control.tx_rate->bitrate)) {
Guy Cohen39e88502008-04-23 17:14:57 -0700872 IWL_DEBUG_RATE("initial rate does not match 0x%x\n", tx_rate);
Tomas Winklerf4d60822008-03-05 11:31:00 -0800873 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700874 }
875
Ben Cahill77626352007-11-29 11:09:44 +0800876 /* Update frame history window with "failure" for each Tx retry. */
Zhu Yib481de92007-09-25 17:54:57 -0700877 while (retries) {
Ben Cahill77626352007-11-29 11:09:44 +0800878 /* Look up the rate and other info used for each tx attempt.
879 * Each tx attempt steps one entry deeper in the rate table. */
Guy Cohen39e88502008-04-23 17:14:57 -0700880 tx_rate = le32_to_cpu(table->rs_table[index].rate_n_flags);
881 rs_get_tbl_info_from_mcs(tx_rate, priv->band,
Zhu Yib481de92007-09-25 17:54:57 -0700882 &tbl_type, &rs_index);
883
Ben Cahill77626352007-11-29 11:09:44 +0800884 /* If type matches "search" table,
885 * add failure to "search" history */
Zhu Yib481de92007-09-25 17:54:57 -0700886 if ((tbl_type.lq_type == search_tbl->lq_type) &&
Guy Cohenfde0db32008-04-21 15:42:01 -0700887 (tbl_type.ant_type == search_tbl->ant_type) &&
Zhu Yib481de92007-09-25 17:54:57 -0700888 (tbl_type.is_SGI == search_tbl->is_SGI)) {
889 if (search_tbl->expected_tpt)
890 tpt = search_tbl->expected_tpt[rs_index];
891 else
892 tpt = 0;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200893 rs_collect_tx_data(search_win, rs_index, tpt, 1, 0);
Ben Cahill77626352007-11-29 11:09:44 +0800894
895 /* Else if type matches "current/active" table,
896 * add failure to "current/active" history */
Zhu Yib481de92007-09-25 17:54:57 -0700897 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
Guy Cohenfde0db32008-04-21 15:42:01 -0700898 (tbl_type.ant_type == curr_tbl->ant_type) &&
Zhu Yib481de92007-09-25 17:54:57 -0700899 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
900 if (curr_tbl->expected_tpt)
901 tpt = curr_tbl->expected_tpt[rs_index];
902 else
903 tpt = 0;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200904 rs_collect_tx_data(window, rs_index, tpt, 1, 0);
Zhu Yib481de92007-09-25 17:54:57 -0700905 }
Ben Cahill77626352007-11-29 11:09:44 +0800906
907 /* If not searching for a new mode, increment failed counter
908 * ... this helps determine when to start searching again */
Tomas Winklerc33104f2008-01-14 17:46:21 -0800909 if (lq_sta->stay_in_tbl)
910 lq_sta->total_failed++;
Zhu Yib481de92007-09-25 17:54:57 -0700911 --retries;
912 index++;
913
914 }
915
Ben Cahill77626352007-11-29 11:09:44 +0800916 /*
917 * Find (by rate) the history window to update with final Tx attempt;
918 * if Tx was successful first try, use original rate,
919 * else look up the rate that was, finally, successful.
920 */
Guy Cohen39e88502008-04-23 17:14:57 -0700921 tx_rate = le32_to_cpu(table->rs_table[index].rate_n_flags);
922 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type, &rs_index);
Zhu Yib481de92007-09-25 17:54:57 -0700923
Ben Cahill77626352007-11-29 11:09:44 +0800924 /* Update frame history window with "success" if Tx got ACKed ... */
Zhu Yib481de92007-09-25 17:54:57 -0700925 if (tx_resp->flags & IEEE80211_TX_STATUS_ACK)
926 status = 1;
927 else
928 status = 0;
929
Ben Cahill77626352007-11-29 11:09:44 +0800930 /* If type matches "search" table,
931 * add final tx status to "search" history */
Zhu Yib481de92007-09-25 17:54:57 -0700932 if ((tbl_type.lq_type == search_tbl->lq_type) &&
Guy Cohenfde0db32008-04-21 15:42:01 -0700933 (tbl_type.ant_type == search_tbl->ant_type) &&
Zhu Yib481de92007-09-25 17:54:57 -0700934 (tbl_type.is_SGI == search_tbl->is_SGI)) {
935 if (search_tbl->expected_tpt)
936 tpt = search_tbl->expected_tpt[rs_index];
937 else
938 tpt = 0;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200939 if (tx_resp->control.flags & IEEE80211_TXCTL_AMPDU)
940 rs_collect_tx_data(search_win, rs_index, tpt,
941 tx_resp->ampdu_ack_len,
942 tx_resp->ampdu_ack_map);
943 else
944 rs_collect_tx_data(search_win, rs_index, tpt,
945 1, status);
Ben Cahill77626352007-11-29 11:09:44 +0800946 /* Else if type matches "current/active" table,
947 * add final tx status to "current/active" history */
Zhu Yib481de92007-09-25 17:54:57 -0700948 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
Guy Cohenfde0db32008-04-21 15:42:01 -0700949 (tbl_type.ant_type == curr_tbl->ant_type) &&
Zhu Yib481de92007-09-25 17:54:57 -0700950 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
951 if (curr_tbl->expected_tpt)
952 tpt = curr_tbl->expected_tpt[rs_index];
953 else
954 tpt = 0;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200955 if (tx_resp->control.flags & IEEE80211_TXCTL_AMPDU)
956 rs_collect_tx_data(window, rs_index, tpt,
957 tx_resp->ampdu_ack_len,
958 tx_resp->ampdu_ack_map);
959 else
960 rs_collect_tx_data(window, rs_index, tpt,
961 1, status);
Zhu Yib481de92007-09-25 17:54:57 -0700962 }
963
Ben Cahill77626352007-11-29 11:09:44 +0800964 /* If not searching for new mode, increment success/failed counter
965 * ... these help determine when to start searching again */
Tomas Winklerc33104f2008-01-14 17:46:21 -0800966 if (lq_sta->stay_in_tbl) {
Ron Rindjunsky99556432008-01-28 14:07:25 +0200967 if (tx_resp->control.flags & IEEE80211_TXCTL_AMPDU) {
968 lq_sta->total_success += tx_resp->ampdu_ack_map;
969 lq_sta->total_failed +=
970 (tx_resp->ampdu_ack_len - tx_resp->ampdu_ack_map);
971 } else {
972 if (status)
973 lq_sta->total_success++;
974 else
975 lq_sta->total_failed++;
976 }
Zhu Yib481de92007-09-25 17:54:57 -0700977 }
978
Ben Cahill77626352007-11-29 11:09:44 +0800979 /* See if there's a better rate or modulation mode to try. */
Zhu Yib481de92007-09-25 17:54:57 -0700980 rs_rate_scale_perform(priv, dev, hdr, sta);
Tomas Winklerf4d60822008-03-05 11:31:00 -0800981out:
Johannes Bergd0709a62008-02-25 16:27:46 +0100982 rcu_read_unlock();
Zhu Yib481de92007-09-25 17:54:57 -0700983 return;
984}
985
Ben Cahill77626352007-11-29 11:09:44 +0800986/*
987 * Begin a period of staying with a selected modulation mode.
988 * Set "stay_in_tbl" flag to prevent any mode switches.
989 * Set frame tx success limits according to legacy vs. high-throughput,
990 * and reset overall (spanning all rates) tx success history statistics.
991 * These control how long we stay using same modulation mode before
992 * searching for a new mode.
993 */
Zhu Yib481de92007-09-25 17:54:57 -0700994static void rs_set_stay_in_table(u8 is_legacy,
Tomas Winklerc33104f2008-01-14 17:46:21 -0800995 struct iwl4965_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -0700996{
Guy Coheneecd6e52008-04-23 17:14:58 -0700997 IWL_DEBUG_RATE("we are staying in the same table\n");
Tomas Winklerc33104f2008-01-14 17:46:21 -0800998 lq_sta->stay_in_tbl = 1; /* only place this gets set */
Zhu Yib481de92007-09-25 17:54:57 -0700999 if (is_legacy) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001000 lq_sta->table_count_limit = IWL_LEGACY_TABLE_COUNT;
1001 lq_sta->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
1002 lq_sta->max_success_limit = IWL_LEGACY_SUCCESS_LIMIT;
Zhu Yib481de92007-09-25 17:54:57 -07001003 } else {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001004 lq_sta->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
1005 lq_sta->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
1006 lq_sta->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
Zhu Yib481de92007-09-25 17:54:57 -07001007 }
Tomas Winklerc33104f2008-01-14 17:46:21 -08001008 lq_sta->table_count = 0;
1009 lq_sta->total_failed = 0;
1010 lq_sta->total_success = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001011}
1012
Ben Cahill77626352007-11-29 11:09:44 +08001013/*
1014 * Find correct throughput table for given mode of modulation
1015 */
Guy Coheneecd6e52008-04-23 17:14:58 -07001016static void rs_set_expected_tpt_table(struct iwl4965_lq_sta *lq_sta,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001017 struct iwl4965_scale_tbl_info *tbl)
Zhu Yib481de92007-09-25 17:54:57 -07001018{
1019 if (is_legacy(tbl->lq_type)) {
1020 if (!is_a_band(tbl->lq_type))
1021 tbl->expected_tpt = expected_tpt_G;
1022 else
1023 tbl->expected_tpt = expected_tpt_A;
1024 } else if (is_siso(tbl->lq_type)) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001025 if (tbl->is_fat && !lq_sta->is_dup)
Zhu Yib481de92007-09-25 17:54:57 -07001026 if (tbl->is_SGI)
1027 tbl->expected_tpt = expected_tpt_siso40MHzSGI;
1028 else
1029 tbl->expected_tpt = expected_tpt_siso40MHz;
1030 else if (tbl->is_SGI)
1031 tbl->expected_tpt = expected_tpt_siso20MHzSGI;
1032 else
1033 tbl->expected_tpt = expected_tpt_siso20MHz;
1034
Guy Cohenfde0db32008-04-21 15:42:01 -07001035 } else if (is_mimo(tbl->lq_type)) { /* FIXME:need to separate mimo2/3 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001036 if (tbl->is_fat && !lq_sta->is_dup)
Zhu Yib481de92007-09-25 17:54:57 -07001037 if (tbl->is_SGI)
1038 tbl->expected_tpt = expected_tpt_mimo40MHzSGI;
1039 else
1040 tbl->expected_tpt = expected_tpt_mimo40MHz;
1041 else if (tbl->is_SGI)
1042 tbl->expected_tpt = expected_tpt_mimo20MHzSGI;
1043 else
1044 tbl->expected_tpt = expected_tpt_mimo20MHz;
1045 } else
1046 tbl->expected_tpt = expected_tpt_G;
1047}
1048
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001049#ifdef CONFIG_IWL4965_HT
Ben Cahill77626352007-11-29 11:09:44 +08001050/*
1051 * Find starting rate for new "search" high-throughput mode of modulation.
1052 * Goal is to find lowest expected rate (under perfect conditions) that is
1053 * above the current measured throughput of "active" mode, to give new mode
1054 * a fair chance to prove itself without too many challenges.
1055 *
1056 * This gets called when transitioning to more aggressive modulation
1057 * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
1058 * (i.e. MIMO to SISO). When moving to MIMO, bit rate will typically need
1059 * to decrease to match "active" throughput. When moving from MIMO to SISO,
1060 * bit rate will typically need to increase, but not if performance was bad.
1061 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001062static s32 rs_get_best_rate(struct iwl_priv *priv,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001063 struct iwl4965_lq_sta *lq_sta,
Ben Cahill77626352007-11-29 11:09:44 +08001064 struct iwl4965_scale_tbl_info *tbl, /* "search" */
Guy Cohenf935a6d2008-04-23 17:15:02 -07001065 u16 rate_mask, s8 index)
Zhu Yib481de92007-09-25 17:54:57 -07001066{
Ben Cahill77626352007-11-29 11:09:44 +08001067 /* "active" values */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001068 struct iwl4965_scale_tbl_info *active_tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001069 &(lq_sta->lq_info[lq_sta->active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07001070 s32 active_sr = active_tbl->win[index].success_ratio;
Zhu Yib481de92007-09-25 17:54:57 -07001071 s32 active_tpt = active_tbl->expected_tpt[index];
Ben Cahill77626352007-11-29 11:09:44 +08001072
1073 /* expected "search" throughput */
1074 s32 *tpt_tbl = tbl->expected_tpt;
1075
1076 s32 new_rate, high, low, start_hi;
Zhu Yib481de92007-09-25 17:54:57 -07001077 u16 high_low;
Guy Cohenf935a6d2008-04-23 17:15:02 -07001078 s8 rate = index;
Zhu Yib481de92007-09-25 17:54:57 -07001079
1080 new_rate = high = low = start_hi = IWL_RATE_INVALID;
1081
1082 for (; ;) {
1083 high_low = rs_get_adjacent_rate(rate, rate_mask, tbl->lq_type);
1084
1085 low = high_low & 0xff;
1086 high = (high_low >> 8) & 0xff;
1087
Ben Cahill77626352007-11-29 11:09:44 +08001088 /*
1089 * Lower the "search" bit rate, to give new "search" mode
1090 * approximately the same throughput as "active" if:
1091 *
1092 * 1) "Active" mode has been working modestly well (but not
1093 * great), and expected "search" throughput (under perfect
1094 * conditions) at candidate rate is above the actual
1095 * measured "active" throughput (but less than expected
1096 * "active" throughput under perfect conditions).
1097 * OR
1098 * 2) "Active" mode has been working perfectly or very well
1099 * and expected "search" throughput (under perfect
1100 * conditions) at candidate rate is above expected
1101 * "active" throughput (under perfect conditions).
1102 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001103 if ((((100 * tpt_tbl[rate]) > lq_sta->last_tpt) &&
Zhu Yib481de92007-09-25 17:54:57 -07001104 ((active_sr > IWL_RATE_DECREASE_TH) &&
1105 (active_sr <= IWL_RATE_HIGH_TH) &&
1106 (tpt_tbl[rate] <= active_tpt))) ||
1107 ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
1108 (tpt_tbl[rate] > active_tpt))) {
1109
Ben Cahill77626352007-11-29 11:09:44 +08001110 /* (2nd or later pass)
1111 * If we've already tried to raise the rate, and are
1112 * now trying to lower it, use the higher rate. */
Zhu Yib481de92007-09-25 17:54:57 -07001113 if (start_hi != IWL_RATE_INVALID) {
1114 new_rate = start_hi;
1115 break;
1116 }
Ben Cahill77626352007-11-29 11:09:44 +08001117
Zhu Yib481de92007-09-25 17:54:57 -07001118 new_rate = rate;
Ben Cahill77626352007-11-29 11:09:44 +08001119
1120 /* Loop again with lower rate */
Zhu Yib481de92007-09-25 17:54:57 -07001121 if (low != IWL_RATE_INVALID)
1122 rate = low;
Ben Cahill77626352007-11-29 11:09:44 +08001123
1124 /* Lower rate not available, use the original */
Zhu Yib481de92007-09-25 17:54:57 -07001125 else
1126 break;
Ben Cahill77626352007-11-29 11:09:44 +08001127
1128 /* Else try to raise the "search" rate to match "active" */
Zhu Yib481de92007-09-25 17:54:57 -07001129 } else {
Ben Cahill77626352007-11-29 11:09:44 +08001130 /* (2nd or later pass)
1131 * If we've already tried to lower the rate, and are
1132 * now trying to raise it, use the lower rate. */
Zhu Yib481de92007-09-25 17:54:57 -07001133 if (new_rate != IWL_RATE_INVALID)
1134 break;
Ben Cahill77626352007-11-29 11:09:44 +08001135
1136 /* Loop again with higher rate */
Zhu Yib481de92007-09-25 17:54:57 -07001137 else if (high != IWL_RATE_INVALID) {
1138 start_hi = high;
1139 rate = high;
Ben Cahill77626352007-11-29 11:09:44 +08001140
1141 /* Higher rate not available, use the original */
Zhu Yib481de92007-09-25 17:54:57 -07001142 } else {
1143 new_rate = rate;
1144 break;
1145 }
1146 }
1147 }
1148
1149 return new_rate;
1150}
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001151#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001152
Ben Cahill77626352007-11-29 11:09:44 +08001153/*
1154 * Set up search table for MIMO
1155 */
Guy Cohenfde0db32008-04-21 15:42:01 -07001156#ifdef CONFIG_IWL4965_HT
1157static int rs_switch_to_mimo2(struct iwl_priv *priv,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001158 struct iwl4965_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001159 struct ieee80211_conf *conf,
1160 struct sta_info *sta,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001161 struct iwl4965_scale_tbl_info *tbl, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001162{
Zhu Yib481de92007-09-25 17:54:57 -07001163 u16 rate_mask;
1164 s32 rate;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001165 s8 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07001166
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001167 if (!(conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) ||
1168 !sta->ht_info.ht_supported)
Zhu Yib481de92007-09-25 17:54:57 -07001169 return -1;
1170
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001171 if (priv->current_ht_config.tx_mimo_ps_mode == IWL_MIMO_PS_STATIC)
Zhu Yib481de92007-09-25 17:54:57 -07001172 return -1;
1173
Ben Cahill77626352007-11-29 11:09:44 +08001174 /* Need both Tx chains/antennas to support MIMO */
Guy Cohenaade00c2008-04-23 17:14:59 -07001175 if (priv->hw_params.tx_chains_num < 2)
Zhu Yib481de92007-09-25 17:54:57 -07001176 return -1;
1177
Guy Coheneecd6e52008-04-23 17:14:58 -07001178 IWL_DEBUG_RATE("LQ: try to switch to MIMO2\n");
Guy Cohen39e88502008-04-23 17:14:57 -07001179
1180 tbl->lq_type = LQ_MIMO2;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001181 tbl->is_dup = lq_sta->is_dup;
Zhu Yib481de92007-09-25 17:54:57 -07001182 tbl->action = 0;
Guy Cohen39e88502008-04-23 17:14:57 -07001183 rate_mask = lq_sta->active_mimo2_rate;
1184
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001185 if (priv->current_ht_config.supported_chan_width
Guy Cohen39e88502008-04-23 17:14:57 -07001186 == IWL_CHANNEL_WIDTH_40MHZ)
Zhu Yib481de92007-09-25 17:54:57 -07001187 tbl->is_fat = 1;
1188 else
1189 tbl->is_fat = 0;
1190
Guy Cohen39e88502008-04-23 17:14:57 -07001191 /* FIXME: - don't toggle SGI here
Zhu Yib481de92007-09-25 17:54:57 -07001192 if (tbl->is_fat) {
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001193 if (priv->current_ht_config.sgf & HT_SHORT_GI_40MHZ_ONLY)
Zhu Yib481de92007-09-25 17:54:57 -07001194 tbl->is_SGI = 1;
1195 else
1196 tbl->is_SGI = 0;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001197 } else if (priv->current_ht_config.sgf & HT_SHORT_GI_20MHZ_ONLY)
Zhu Yib481de92007-09-25 17:54:57 -07001198 tbl->is_SGI = 1;
1199 else
1200 tbl->is_SGI = 0;
Guy Cohen39e88502008-04-23 17:14:57 -07001201 */
Zhu Yib481de92007-09-25 17:54:57 -07001202
Guy Coheneecd6e52008-04-23 17:14:58 -07001203 rs_set_expected_tpt_table(lq_sta, tbl);
Zhu Yib481de92007-09-25 17:54:57 -07001204
Guy Cohenf935a6d2008-04-23 17:15:02 -07001205 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
Zhu Yib481de92007-09-25 17:54:57 -07001206
Guy Coheneecd6e52008-04-23 17:14:58 -07001207 IWL_DEBUG_RATE("LQ: MIMO2 best rate %d mask %X\n", rate, rate_mask);
Guy Cohen39e88502008-04-23 17:14:57 -07001208
1209 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
Guy Coheneecd6e52008-04-23 17:14:58 -07001210 IWL_DEBUG_RATE("Can't switch with index %d rate mask %x\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001211 rate, rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -07001212 return -1;
Guy Cohen39e88502008-04-23 17:14:57 -07001213 }
1214 tbl->current_rate = rate_n_flags_from_tbl(tbl, rate, is_green);
Zhu Yib481de92007-09-25 17:54:57 -07001215
Guy Coheneecd6e52008-04-23 17:14:58 -07001216 IWL_DEBUG_RATE("LQ: Switch to new mcs %X index is green %X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001217 tbl->current_rate, is_green);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001218 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07001219}
Guy Cohenfde0db32008-04-21 15:42:01 -07001220#else
1221static int rs_switch_to_mimo2(struct iwl_priv *priv,
1222 struct iwl4965_lq_sta *lq_sta,
1223 struct ieee80211_conf *conf,
1224 struct sta_info *sta,
1225 struct iwl4965_scale_tbl_info *tbl, int index)
1226{
1227 return -1;
1228}
1229#endif /*CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001230
Ben Cahill77626352007-11-29 11:09:44 +08001231/*
1232 * Set up search table for SISO
1233 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001234static int rs_switch_to_siso(struct iwl_priv *priv,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001235 struct iwl4965_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001236 struct ieee80211_conf *conf,
1237 struct sta_info *sta,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001238 struct iwl4965_scale_tbl_info *tbl, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001239{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001240#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07001241 u16 rate_mask;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001242 u8 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07001243 s32 rate;
1244
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001245 if (!(conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) ||
1246 !sta->ht_info.ht_supported)
Zhu Yib481de92007-09-25 17:54:57 -07001247 return -1;
1248
Guy Coheneecd6e52008-04-23 17:14:58 -07001249 IWL_DEBUG_RATE("LQ: try to switch to SISO\n");
Guy Cohen39e88502008-04-23 17:14:57 -07001250
Tomas Winklerc33104f2008-01-14 17:46:21 -08001251 tbl->is_dup = lq_sta->is_dup;
Zhu Yib481de92007-09-25 17:54:57 -07001252 tbl->lq_type = LQ_SISO;
1253 tbl->action = 0;
Guy Cohen39e88502008-04-23 17:14:57 -07001254 rate_mask = lq_sta->active_siso_rate;
Zhu Yib481de92007-09-25 17:54:57 -07001255
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001256 if (priv->current_ht_config.supported_chan_width
1257 == IWL_CHANNEL_WIDTH_40MHZ)
Zhu Yib481de92007-09-25 17:54:57 -07001258 tbl->is_fat = 1;
1259 else
1260 tbl->is_fat = 0;
1261
Guy Cohen39e88502008-04-23 17:14:57 -07001262 /* FIXME: - don't toggle SGI here
Zhu Yib481de92007-09-25 17:54:57 -07001263 if (tbl->is_fat) {
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001264 if (priv->current_ht_config.sgf & HT_SHORT_GI_40MHZ_ONLY)
Zhu Yib481de92007-09-25 17:54:57 -07001265 tbl->is_SGI = 1;
1266 else
1267 tbl->is_SGI = 0;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001268 } else if (priv->current_ht_config.sgf & HT_SHORT_GI_20MHZ_ONLY)
Zhu Yib481de92007-09-25 17:54:57 -07001269 tbl->is_SGI = 1;
1270 else
1271 tbl->is_SGI = 0;
Guy Cohen39e88502008-04-23 17:14:57 -07001272 */
Zhu Yib481de92007-09-25 17:54:57 -07001273
1274 if (is_green)
Guy Cohen39e88502008-04-23 17:14:57 -07001275 tbl->is_SGI = 0; /*11n spec: no SGI in SISO+Greenfield*/
Zhu Yib481de92007-09-25 17:54:57 -07001276
Guy Coheneecd6e52008-04-23 17:14:58 -07001277 rs_set_expected_tpt_table(lq_sta, tbl);
Guy Cohenf935a6d2008-04-23 17:15:02 -07001278 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
Zhu Yib481de92007-09-25 17:54:57 -07001279
Guy Coheneecd6e52008-04-23 17:14:58 -07001280 IWL_DEBUG_RATE("LQ: get best rate %d mask %X\n", rate, rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -07001281 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
Guy Coheneecd6e52008-04-23 17:14:58 -07001282 IWL_DEBUG_RATE("can not switch with index %d rate mask %x\n",
Zhu Yib481de92007-09-25 17:54:57 -07001283 rate, rate_mask);
1284 return -1;
1285 }
Guy Cohen39e88502008-04-23 17:14:57 -07001286 tbl->current_rate = rate_n_flags_from_tbl(tbl, rate, is_green);
Guy Coheneecd6e52008-04-23 17:14:58 -07001287 IWL_DEBUG_RATE("LQ: Switch to new mcs %X index is green %X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001288 tbl->current_rate, is_green);
Mohamed Abbas403ab562007-11-06 22:06:25 -08001289 return 0;
1290#else
1291 return -1;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001292#endif /*CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001293}
1294
Ben Cahill77626352007-11-29 11:09:44 +08001295/*
1296 * Try to switch to new modulation mode from legacy
1297 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001298static int rs_move_legacy_other(struct iwl_priv *priv,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001299 struct iwl4965_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001300 struct ieee80211_conf *conf,
1301 struct sta_info *sta,
Zhu Yib481de92007-09-25 17:54:57 -07001302 int index)
1303{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001304 struct iwl4965_scale_tbl_info *tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001305 &(lq_sta->lq_info[lq_sta->active_tbl]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001306 struct iwl4965_scale_tbl_info *search_tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001307 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001308 struct iwl4965_rate_scale_data *window = &(tbl->win[index]);
1309 u32 sz = (sizeof(struct iwl4965_scale_tbl_info) -
1310 (sizeof(struct iwl4965_rate_scale_data) * IWL_RATE_COUNT));
Zhu Yib481de92007-09-25 17:54:57 -07001311 u8 start_action = tbl->action;
Guy Cohenfde0db32008-04-21 15:42:01 -07001312 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1313 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001314
1315 for (; ;) {
1316 switch (tbl->action) {
1317 case IWL_LEGACY_SWITCH_ANTENNA:
Guy Cohenf935a6d2008-04-23 17:15:02 -07001318 IWL_DEBUG_RATE("LQ: Legacy toggle Antenna\n");
Zhu Yib481de92007-09-25 17:54:57 -07001319
Tomas Winklerc33104f2008-01-14 17:46:21 -08001320 lq_sta->action_counter++;
Ben Cahill77626352007-11-29 11:09:44 +08001321
1322 /* Don't change antenna if success has been great */
Guy Cohen39e88502008-04-23 17:14:57 -07001323 /*FIXME:RS:not sure this is really needed*/
Zhu Yib481de92007-09-25 17:54:57 -07001324 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1325 break;
Ben Cahill77626352007-11-29 11:09:44 +08001326
Ben Cahill77626352007-11-29 11:09:44 +08001327 /* Set up search table to try other antenna */
Zhu Yib481de92007-09-25 17:54:57 -07001328 memcpy(search_tbl, tbl, sz);
1329
Guy Cohenaade00c2008-04-23 17:14:59 -07001330 if (rs_toggle_antenna(valid_tx_ant,
1331 &search_tbl->current_rate, search_tbl)) {
1332 rs_set_expected_tpt_table(lq_sta, search_tbl);
1333 lq_sta->search_better_tbl = 1;
1334 goto out;
1335 }
Zhu Yib481de92007-09-25 17:54:57 -07001336
1337 case IWL_LEGACY_SWITCH_SISO:
Guy Coheneecd6e52008-04-23 17:14:58 -07001338 IWL_DEBUG_RATE("LQ: Legacy switch to SISO\n");
Ben Cahill77626352007-11-29 11:09:44 +08001339
1340 /* Set up search table to try SISO */
Zhu Yib481de92007-09-25 17:54:57 -07001341 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001342 search_tbl->is_SGI = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001343 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001344 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001345 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001346 lq_sta->search_better_tbl = 1;
1347 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001348 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001349 }
Zhu Yib481de92007-09-25 17:54:57 -07001350
1351 break;
Guy Cohenfde0db32008-04-21 15:42:01 -07001352 case IWL_LEGACY_SWITCH_MIMO2:
Guy Coheneecd6e52008-04-23 17:14:58 -07001353 IWL_DEBUG_RATE("LQ: Legacy switch to MIMO2\n");
Ben Cahill77626352007-11-29 11:09:44 +08001354
1355 /* Set up search table to try MIMO */
Zhu Yib481de92007-09-25 17:54:57 -07001356 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001357 search_tbl->is_SGI = 0;
Guy Cohenfde0db32008-04-21 15:42:01 -07001358 search_tbl->ant_type = ANT_AB;/*FIXME:RS*/
1359 /*FIXME:RS:need to check ant validity*/
1360 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001361 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001362 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001363 lq_sta->search_better_tbl = 1;
1364 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001365 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001366 }
Zhu Yib481de92007-09-25 17:54:57 -07001367 break;
1368 }
1369 tbl->action++;
Guy Cohenfde0db32008-04-21 15:42:01 -07001370 if (tbl->action > IWL_LEGACY_SWITCH_MIMO2)
Zhu Yib481de92007-09-25 17:54:57 -07001371 tbl->action = IWL_LEGACY_SWITCH_ANTENNA;
1372
1373 if (tbl->action == start_action)
1374 break;
1375
1376 }
1377 return 0;
1378
1379 out:
1380 tbl->action++;
Guy Cohenfde0db32008-04-21 15:42:01 -07001381 if (tbl->action > IWL_LEGACY_SWITCH_MIMO2)
Zhu Yib481de92007-09-25 17:54:57 -07001382 tbl->action = IWL_LEGACY_SWITCH_ANTENNA;
1383 return 0;
1384
1385}
1386
Ben Cahill77626352007-11-29 11:09:44 +08001387/*
1388 * Try to switch to new modulation mode from SISO
1389 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001390static int rs_move_siso_to_other(struct iwl_priv *priv,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001391 struct iwl4965_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001392 struct ieee80211_conf *conf,
1393 struct sta_info *sta,
Zhu Yib481de92007-09-25 17:54:57 -07001394 int index)
1395{
Tomas Winklerc33104f2008-01-14 17:46:21 -08001396 u8 is_green = lq_sta->is_green;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001397 struct iwl4965_scale_tbl_info *tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001398 &(lq_sta->lq_info[lq_sta->active_tbl]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001399 struct iwl4965_scale_tbl_info *search_tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001400 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001401 struct iwl4965_rate_scale_data *window = &(tbl->win[index]);
1402 u32 sz = (sizeof(struct iwl4965_scale_tbl_info) -
1403 (sizeof(struct iwl4965_rate_scale_data) * IWL_RATE_COUNT));
Zhu Yib481de92007-09-25 17:54:57 -07001404 u8 start_action = tbl->action;
Guy Cohenfde0db32008-04-21 15:42:01 -07001405 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1406 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001407
1408 for (;;) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001409 lq_sta->action_counter++;
Zhu Yib481de92007-09-25 17:54:57 -07001410 switch (tbl->action) {
1411 case IWL_SISO_SWITCH_ANTENNA:
Guy Coheneecd6e52008-04-23 17:14:58 -07001412 IWL_DEBUG_RATE("LQ: SISO toggle Antenna\n");
Guy Cohen39e88502008-04-23 17:14:57 -07001413 /*FIXME:RS: is this really needed for SISO?*/
Zhu Yib481de92007-09-25 17:54:57 -07001414 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1415 break;
Zhu Yib481de92007-09-25 17:54:57 -07001416
1417 memcpy(search_tbl, tbl, sz);
Guy Cohenaade00c2008-04-23 17:14:59 -07001418 if (rs_toggle_antenna(valid_tx_ant,
1419 &search_tbl->current_rate, search_tbl)) {
1420 lq_sta->search_better_tbl = 1;
1421 goto out;
1422 }
Zhu Yib481de92007-09-25 17:54:57 -07001423
Guy Cohenfde0db32008-04-21 15:42:01 -07001424 case IWL_SISO_SWITCH_MIMO2:
Guy Coheneecd6e52008-04-23 17:14:58 -07001425 IWL_DEBUG_RATE("LQ: SISO switch to MIMO\n");
Zhu Yib481de92007-09-25 17:54:57 -07001426 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001427 search_tbl->is_SGI = 0;
Guy Cohenfde0db32008-04-21 15:42:01 -07001428 search_tbl->ant_type = ANT_AB; /*FIXME:RS*/
1429 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001430 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001431 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001432 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001433 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001434 }
Zhu Yib481de92007-09-25 17:54:57 -07001435 break;
1436 case IWL_SISO_SWITCH_GI:
Guy Coheneecd6e52008-04-23 17:14:58 -07001437 IWL_DEBUG_RATE("LQ: SISO toggle SGI/NGI\n");
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001438
Zhu Yib481de92007-09-25 17:54:57 -07001439 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001440 if (is_green) {
1441 if (!tbl->is_SGI)
1442 break;
1443 else
1444 IWL_ERROR("SGI was set in GF+SISO\n");
Zhu Yib481de92007-09-25 17:54:57 -07001445 }
Guy Cohen39e88502008-04-23 17:14:57 -07001446 search_tbl->is_SGI = !tbl->is_SGI;
Guy Coheneecd6e52008-04-23 17:14:58 -07001447 rs_set_expected_tpt_table(lq_sta, search_tbl);
Guy Cohen39e88502008-04-23 17:14:57 -07001448 if (tbl->is_SGI) {
1449 s32 tpt = lq_sta->last_tpt / 100;
1450 if (tpt >= search_tbl->expected_tpt[index])
1451 break;
1452 }
1453 search_tbl->current_rate = rate_n_flags_from_tbl(
1454 search_tbl, index, is_green);
1455 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001456 goto out;
1457 }
1458 tbl->action++;
1459 if (tbl->action > IWL_SISO_SWITCH_GI)
1460 tbl->action = IWL_SISO_SWITCH_ANTENNA;
1461
1462 if (tbl->action == start_action)
1463 break;
1464 }
1465 return 0;
1466
1467 out:
1468 tbl->action++;
1469 if (tbl->action > IWL_SISO_SWITCH_GI)
1470 tbl->action = IWL_SISO_SWITCH_ANTENNA;
1471 return 0;
1472}
1473
Ben Cahill77626352007-11-29 11:09:44 +08001474/*
1475 * Try to switch to new modulation mode from MIMO
1476 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001477static int rs_move_mimo_to_other(struct iwl_priv *priv,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001478 struct iwl4965_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001479 struct ieee80211_conf *conf,
1480 struct sta_info *sta,
Zhu Yib481de92007-09-25 17:54:57 -07001481 int index)
1482{
Tomas Winklerc33104f2008-01-14 17:46:21 -08001483 s8 is_green = lq_sta->is_green;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001484 struct iwl4965_scale_tbl_info *tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001485 &(lq_sta->lq_info[lq_sta->active_tbl]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001486 struct iwl4965_scale_tbl_info *search_tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001487 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001488 u32 sz = (sizeof(struct iwl4965_scale_tbl_info) -
1489 (sizeof(struct iwl4965_rate_scale_data) * IWL_RATE_COUNT));
Zhu Yib481de92007-09-25 17:54:57 -07001490 u8 start_action = tbl->action;
Guy Cohenaade00c2008-04-23 17:14:59 -07001491 /*u8 valid_tx_ant = priv->hw_params.valid_tx_ant;*/
1492 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001493
1494 for (;;) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001495 lq_sta->action_counter++;
Zhu Yib481de92007-09-25 17:54:57 -07001496 switch (tbl->action) {
1497 case IWL_MIMO_SWITCH_ANTENNA_A:
1498 case IWL_MIMO_SWITCH_ANTENNA_B:
Guy Coheneecd6e52008-04-23 17:14:58 -07001499 IWL_DEBUG_RATE("LQ: MIMO2 switch to SISO\n");
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001500
Ben Cahill77626352007-11-29 11:09:44 +08001501 /* Set up new search table for SISO */
Zhu Yib481de92007-09-25 17:54:57 -07001502 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001503
1504 /*FIXME:RS:need to check ant validity + C*/
Zhu Yib481de92007-09-25 17:54:57 -07001505 if (tbl->action == IWL_MIMO_SWITCH_ANTENNA_A)
Guy Cohenfde0db32008-04-21 15:42:01 -07001506 search_tbl->ant_type = ANT_A;
Zhu Yib481de92007-09-25 17:54:57 -07001507 else
Guy Cohenfde0db32008-04-21 15:42:01 -07001508 search_tbl->ant_type = ANT_B;
Zhu Yib481de92007-09-25 17:54:57 -07001509
Tomas Winklerc33104f2008-01-14 17:46:21 -08001510 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001511 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001512 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001513 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001514 goto out;
1515 }
1516 break;
1517
1518 case IWL_MIMO_SWITCH_GI:
Guy Coheneecd6e52008-04-23 17:14:58 -07001519 IWL_DEBUG_RATE("LQ: MIMO toggle SGI/NGI\n");
Ben Cahill77626352007-11-29 11:09:44 +08001520
1521 /* Set up new search table for MIMO */
Zhu Yib481de92007-09-25 17:54:57 -07001522 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001523 search_tbl->is_SGI = !tbl->is_SGI;
Guy Coheneecd6e52008-04-23 17:14:58 -07001524 rs_set_expected_tpt_table(lq_sta, search_tbl);
Ben Cahill77626352007-11-29 11:09:44 +08001525 /*
1526 * If active table already uses the fastest possible
1527 * modulation (dual stream with short guard interval),
1528 * and it's working well, there's no need to look
1529 * for a better type of modulation!
1530 */
Guy Cohen39e88502008-04-23 17:14:57 -07001531 if (tbl->is_SGI) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001532 s32 tpt = lq_sta->last_tpt / 100;
Guy Cohen39e88502008-04-23 17:14:57 -07001533 if (tpt >= search_tbl->expected_tpt[index])
1534 break;
Zhu Yib481de92007-09-25 17:54:57 -07001535 }
Guy Cohen39e88502008-04-23 17:14:57 -07001536 search_tbl->current_rate = rate_n_flags_from_tbl(
1537 search_tbl, index, is_green);
1538 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001539 goto out;
1540
1541 }
1542 tbl->action++;
1543 if (tbl->action > IWL_MIMO_SWITCH_GI)
1544 tbl->action = IWL_MIMO_SWITCH_ANTENNA_A;
1545
1546 if (tbl->action == start_action)
1547 break;
1548 }
1549
1550 return 0;
1551 out:
1552 tbl->action++;
1553 if (tbl->action > IWL_MIMO_SWITCH_GI)
1554 tbl->action = IWL_MIMO_SWITCH_ANTENNA_A;
1555 return 0;
1556
1557}
1558
Ben Cahill77626352007-11-29 11:09:44 +08001559/*
1560 * Check whether we should continue using same modulation mode, or
1561 * begin search for a new mode, based on:
1562 * 1) # tx successes or failures while using this mode
1563 * 2) # times calling this function
1564 * 3) elapsed time in this mode (not used, for now)
1565 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001566static void rs_stay_in_table(struct iwl4965_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07001567{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001568 struct iwl4965_scale_tbl_info *tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001569 int i;
1570 int active_tbl;
1571 int flush_interval_passed = 0;
1572
Tomas Winklerc33104f2008-01-14 17:46:21 -08001573 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001574
Tomas Winklerc33104f2008-01-14 17:46:21 -08001575 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07001576
Ben Cahill77626352007-11-29 11:09:44 +08001577 /* If we've been disallowing search, see if we should now allow it */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001578 if (lq_sta->stay_in_tbl) {
Zhu Yib481de92007-09-25 17:54:57 -07001579
Ben Cahill77626352007-11-29 11:09:44 +08001580 /* Elapsed time using current modulation mode */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001581 if (lq_sta->flush_timer)
Zhu Yib481de92007-09-25 17:54:57 -07001582 flush_interval_passed =
1583 time_after(jiffies,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001584 (unsigned long)(lq_sta->flush_timer +
Zhu Yib481de92007-09-25 17:54:57 -07001585 IWL_RATE_SCALE_FLUSH_INTVL));
1586
Ben Cahill77626352007-11-29 11:09:44 +08001587 /*
1588 * Check if we should allow search for new modulation mode.
1589 * If many frames have failed or succeeded, or we've used
1590 * this same modulation for a long time, allow search, and
1591 * reset history stats that keep track of whether we should
1592 * allow a new search. Also (below) reset all bitmaps and
1593 * stats in active history.
1594 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001595 if ((lq_sta->total_failed > lq_sta->max_failure_limit) ||
1596 (lq_sta->total_success > lq_sta->max_success_limit) ||
1597 ((!lq_sta->search_better_tbl) && (lq_sta->flush_timer)
Zhu Yib481de92007-09-25 17:54:57 -07001598 && (flush_interval_passed))) {
Guy Coheneecd6e52008-04-23 17:14:58 -07001599 IWL_DEBUG_RATE("LQ: stay is expired %d %d %d\n:",
Tomas Winklerc33104f2008-01-14 17:46:21 -08001600 lq_sta->total_failed,
1601 lq_sta->total_success,
Zhu Yib481de92007-09-25 17:54:57 -07001602 flush_interval_passed);
Ben Cahill77626352007-11-29 11:09:44 +08001603
1604 /* Allow search for new mode */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001605 lq_sta->stay_in_tbl = 0; /* only place reset */
1606 lq_sta->total_failed = 0;
1607 lq_sta->total_success = 0;
1608 lq_sta->flush_timer = 0;
Ben Cahill77626352007-11-29 11:09:44 +08001609
1610 /*
1611 * Else if we've used this modulation mode enough repetitions
1612 * (regardless of elapsed time or success/failure), reset
1613 * history bitmaps and rate-specific stats for all rates in
1614 * active table.
1615 */
Mohamed Abbas403ab562007-11-06 22:06:25 -08001616 } else {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001617 lq_sta->table_count++;
1618 if (lq_sta->table_count >=
1619 lq_sta->table_count_limit) {
1620 lq_sta->table_count = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001621
Guy Coheneecd6e52008-04-23 17:14:58 -07001622 IWL_DEBUG_RATE("LQ: stay in table clear win\n");
Zhu Yib481de92007-09-25 17:54:57 -07001623 for (i = 0; i < IWL_RATE_COUNT; i++)
1624 rs_rate_scale_clear_window(
1625 &(tbl->win[i]));
1626 }
1627 }
1628
Ben Cahill77626352007-11-29 11:09:44 +08001629 /* If transitioning to allow "search", reset all history
1630 * bitmaps and stats in active table (this will become the new
1631 * "search" table). */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001632 if (!lq_sta->stay_in_tbl) {
Zhu Yib481de92007-09-25 17:54:57 -07001633 for (i = 0; i < IWL_RATE_COUNT; i++)
1634 rs_rate_scale_clear_window(&(tbl->win[i]));
1635 }
1636 }
1637}
1638
Ben Cahill77626352007-11-29 11:09:44 +08001639/*
1640 * Do rate scaling and search for new modulation mode.
1641 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001642static void rs_rate_scale_perform(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001643 struct net_device *dev,
1644 struct ieee80211_hdr *hdr,
1645 struct sta_info *sta)
1646{
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001647 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1648 struct ieee80211_hw *hw = local_to_hw(local);
1649 struct ieee80211_conf *conf = &hw->conf;
Zhu Yib481de92007-09-25 17:54:57 -07001650 int low = IWL_RATE_INVALID;
1651 int high = IWL_RATE_INVALID;
1652 int index;
1653 int i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001654 struct iwl4965_rate_scale_data *window = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001655 int current_tpt = IWL_INVALID_VALUE;
1656 int low_tpt = IWL_INVALID_VALUE;
1657 int high_tpt = IWL_INVALID_VALUE;
1658 u32 fail_count;
1659 s8 scale_action = 0;
1660 u16 fc, rate_mask;
1661 u8 update_lq = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001662 struct iwl4965_lq_sta *lq_sta;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001663 struct iwl4965_scale_tbl_info *tbl, *tbl1;
Zhu Yib481de92007-09-25 17:54:57 -07001664 u16 rate_scale_index_msk = 0;
Guy Cohen39e88502008-04-23 17:14:57 -07001665 u32 rate;
Zhu Yib481de92007-09-25 17:54:57 -07001666 u8 is_green = 0;
1667 u8 active_tbl = 0;
1668 u8 done_search = 0;
1669 u16 high_low;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001670#ifdef CONFIG_IWL4965_HT
1671 u8 tid = MAX_TID_COUNT;
1672 __le16 *qc;
1673#endif
Zhu Yib481de92007-09-25 17:54:57 -07001674
1675 IWL_DEBUG_RATE("rate scale calculate new rate for skb\n");
1676
1677 fc = le16_to_cpu(hdr->frame_control);
1678 if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1)) {
1679 /* Send management frames and broadcast/multicast data using
1680 * lowest rate. */
1681 /* TODO: this could probably be improved.. */
1682 return;
1683 }
1684
1685 if (!sta || !sta->rate_ctrl_priv)
1686 return;
1687
1688 if (!priv->lq_mngr.lq_ready) {
1689 IWL_DEBUG_RATE("still rate scaling not ready\n");
1690 return;
1691 }
Tomas Winklerc33104f2008-01-14 17:46:21 -08001692 lq_sta = (struct iwl4965_lq_sta *)sta->rate_ctrl_priv;
Zhu Yib481de92007-09-25 17:54:57 -07001693
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001694#ifdef CONFIG_IWL4965_HT
1695 qc = ieee80211_get_qos_ctrl(hdr);
1696 if (qc) {
1697 tid = (u8)(le16_to_cpu(*qc) & 0xf);
1698 rs_tl_add_packet(lq_sta, tid);
1699 }
1700#endif
Ben Cahill77626352007-11-29 11:09:44 +08001701 /*
1702 * Select rate-scale / modulation-mode table to work with in
1703 * the rest of this function: "search" if searching for better
1704 * modulation mode, or "active" if doing rate scaling within a mode.
1705 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001706 if (!lq_sta->search_better_tbl)
1707 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001708 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08001709 active_tbl = 1 - lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001710
Tomas Winklerc33104f2008-01-14 17:46:21 -08001711 tbl = &(lq_sta->lq_info[active_tbl]);
1712 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07001713
Ben Cahill77626352007-11-29 11:09:44 +08001714 /* current tx rate */
Johannes Berg8318d782008-01-24 19:38:38 +01001715 index = sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07001716
1717 IWL_DEBUG_RATE("Rate scale index %d for type %d\n", index,
1718 tbl->lq_type);
1719
Ben Cahill77626352007-11-29 11:09:44 +08001720 /* rates available for this association, and for modulation mode */
Guy Coheneecd6e52008-04-23 17:14:58 -07001721 rate_mask = rs_get_supported_rates(lq_sta, hdr, tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -07001722
1723 IWL_DEBUG_RATE("mask 0x%04X \n", rate_mask);
1724
1725 /* mask with station rate restriction */
1726 if (is_legacy(tbl->lq_type)) {
Johannes Berg8318d782008-01-24 19:38:38 +01001727 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Ben Cahill77626352007-11-29 11:09:44 +08001728 /* supp_rates has no CCK bits in A mode */
Zhu Yib481de92007-09-25 17:54:57 -07001729 rate_scale_index_msk = (u16) (rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -08001730 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
Zhu Yib481de92007-09-25 17:54:57 -07001731 else
1732 rate_scale_index_msk = (u16) (rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -08001733 lq_sta->supp_rates);
Zhu Yib481de92007-09-25 17:54:57 -07001734
1735 } else
1736 rate_scale_index_msk = rate_mask;
1737
1738 if (!rate_scale_index_msk)
1739 rate_scale_index_msk = rate_mask;
1740
Ben Cahill77626352007-11-29 11:09:44 +08001741 /* If current rate is no longer supported on current association,
1742 * or user changed preferences for rates, find a new supported rate. */
Zhu Yib481de92007-09-25 17:54:57 -07001743 if (index < 0 || !((1 << index) & rate_scale_index_msk)) {
1744 index = IWL_INVALID_VALUE;
1745 update_lq = 1;
1746
Ben Cahill77626352007-11-29 11:09:44 +08001747 /* get the highest available rate */
Zhu Yib481de92007-09-25 17:54:57 -07001748 for (i = 0; i <= IWL_RATE_COUNT; i++) {
1749 if ((1 << i) & rate_scale_index_msk)
1750 index = i;
1751 }
1752
1753 if (index == IWL_INVALID_VALUE) {
1754 IWL_WARNING("Can not find a suitable rate\n");
1755 return;
1756 }
1757 }
1758
Ben Cahill77626352007-11-29 11:09:44 +08001759 /* Get expected throughput table and history window for current rate */
Zhu Yib481de92007-09-25 17:54:57 -07001760 if (!tbl->expected_tpt)
Guy Coheneecd6e52008-04-23 17:14:58 -07001761 rs_set_expected_tpt_table(lq_sta, tbl);
Zhu Yib481de92007-09-25 17:54:57 -07001762
1763 window = &(tbl->win[index]);
1764
Ben Cahill77626352007-11-29 11:09:44 +08001765 /*
1766 * If there is not enough history to calculate actual average
1767 * throughput, keep analyzing results of more tx frames, without
1768 * changing rate or mode (bypass most of the rest of this function).
1769 * Set up new rate table in uCode only if old rate is not supported
1770 * in current association (use new rate found above).
1771 */
Zhu Yib481de92007-09-25 17:54:57 -07001772 fail_count = window->counter - window->success_counter;
1773 if (((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
1774 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH))
1775 || (tbl->expected_tpt == NULL)) {
1776 IWL_DEBUG_RATE("LQ: still below TH succ %d total %d "
1777 "for index %d\n",
1778 window->success_counter, window->counter, index);
Ben Cahill77626352007-11-29 11:09:44 +08001779
1780 /* Can't calculate this yet; not enough history */
Zhu Yib481de92007-09-25 17:54:57 -07001781 window->average_tpt = IWL_INVALID_VALUE;
Ben Cahill77626352007-11-29 11:09:44 +08001782
1783 /* Should we stay with this modulation mode,
1784 * or search for a new one? */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001785 rs_stay_in_table(lq_sta);
Ben Cahill77626352007-11-29 11:09:44 +08001786
1787 /* Set up new rate table in uCode, if needed */
Zhu Yib481de92007-09-25 17:54:57 -07001788 if (update_lq) {
Guy Cohen39e88502008-04-23 17:14:57 -07001789 rate = rate_n_flags_from_tbl(tbl, index, is_green);
1790 rs_fill_link_cmd(priv, lq_sta, rate, &lq_sta->lq);
Tomas Winkler66c73db2008-04-15 16:01:40 -07001791 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07001792 }
1793 goto out;
1794
Ben Cahill77626352007-11-29 11:09:44 +08001795 /* Else we have enough samples; calculate estimate of
1796 * actual average throughput */
Zhu Yib481de92007-09-25 17:54:57 -07001797 } else
1798 window->average_tpt = ((window->success_ratio *
1799 tbl->expected_tpt[index] + 64) / 128);
1800
Ben Cahill77626352007-11-29 11:09:44 +08001801 /* If we are searching for better modulation mode, check success. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001802 if (lq_sta->search_better_tbl) {
Zhu Yib481de92007-09-25 17:54:57 -07001803 int success_limit = IWL_RATE_SCALE_SWITCH;
1804
Ben Cahill77626352007-11-29 11:09:44 +08001805 /* If good success, continue using the "search" mode;
1806 * no need to send new link quality command, since we're
1807 * continuing to use the setup that we've been trying. */
Zhu Yib481de92007-09-25 17:54:57 -07001808 if ((window->success_ratio > success_limit) ||
Tomas Winklerc33104f2008-01-14 17:46:21 -08001809 (window->average_tpt > lq_sta->last_tpt)) {
Zhu Yib481de92007-09-25 17:54:57 -07001810 if (!is_legacy(tbl->lq_type)) {
Guy Coheneecd6e52008-04-23 17:14:58 -07001811 IWL_DEBUG_RATE("LQ: we are switching to HT"
Zhu Yib481de92007-09-25 17:54:57 -07001812 " rate suc %d current tpt %d"
1813 " old tpt %d\n",
1814 window->success_ratio,
1815 window->average_tpt,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001816 lq_sta->last_tpt);
1817 lq_sta->enable_counter = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001818 }
Ben Cahill77626352007-11-29 11:09:44 +08001819 /* Swap tables; "search" becomes "active" */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001820 lq_sta->active_tbl = active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001821 current_tpt = window->average_tpt;
Ben Cahill77626352007-11-29 11:09:44 +08001822
1823 /* Else poor success; go back to mode in "active" table */
Zhu Yib481de92007-09-25 17:54:57 -07001824 } else {
Ben Cahill77626352007-11-29 11:09:44 +08001825 /* Nullify "search" table */
Zhu Yib481de92007-09-25 17:54:57 -07001826 tbl->lq_type = LQ_NONE;
Ben Cahill77626352007-11-29 11:09:44 +08001827
1828 /* Revert to "active" table */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001829 active_tbl = lq_sta->active_tbl;
1830 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07001831
Ben Cahill77626352007-11-29 11:09:44 +08001832 /* Revert to "active" rate and throughput info */
Tomas Winkler17744ff2008-03-02 01:52:00 +02001833 index = iwl4965_hwrate_to_plcp_idx(
Guy Cohen39e88502008-04-23 17:14:57 -07001834 tbl->current_rate);
Tomas Winklerc33104f2008-01-14 17:46:21 -08001835 current_tpt = lq_sta->last_tpt;
Ben Cahill77626352007-11-29 11:09:44 +08001836
1837 /* Need to set up a new rate table in uCode */
1838 update_lq = 1;
Guy Coheneecd6e52008-04-23 17:14:58 -07001839 IWL_DEBUG_RATE("XXY GO BACK TO OLD TABLE\n");
Zhu Yib481de92007-09-25 17:54:57 -07001840 }
Ben Cahill77626352007-11-29 11:09:44 +08001841
1842 /* Either way, we've made a decision; modulation mode
1843 * search is done, allow rate adjustment next time. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001844 lq_sta->search_better_tbl = 0;
Ben Cahill77626352007-11-29 11:09:44 +08001845 done_search = 1; /* Don't switch modes below! */
Zhu Yib481de92007-09-25 17:54:57 -07001846 goto lq_update;
1847 }
1848
Ben Cahill77626352007-11-29 11:09:44 +08001849 /* (Else) not in search of better modulation mode, try for better
1850 * starting rate, while staying in this mode. */
Zhu Yib481de92007-09-25 17:54:57 -07001851 high_low = rs_get_adjacent_rate(index, rate_scale_index_msk,
1852 tbl->lq_type);
1853 low = high_low & 0xff;
1854 high = (high_low >> 8) & 0xff;
1855
Ben Cahill77626352007-11-29 11:09:44 +08001856 /* Collect measured throughputs for current and adjacent rates */
Zhu Yib481de92007-09-25 17:54:57 -07001857 current_tpt = window->average_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07001858 if (low != IWL_RATE_INVALID)
1859 low_tpt = tbl->win[low].average_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07001860 if (high != IWL_RATE_INVALID)
1861 high_tpt = tbl->win[high].average_tpt;
1862
Ben Cahill77626352007-11-29 11:09:44 +08001863 /* Assume rate increase */
Zhu Yib481de92007-09-25 17:54:57 -07001864 scale_action = 1;
1865
Ben Cahill77626352007-11-29 11:09:44 +08001866 /* Too many failures, decrease rate */
Zhu Yib481de92007-09-25 17:54:57 -07001867 if ((window->success_ratio <= IWL_RATE_DECREASE_TH) ||
1868 (current_tpt == 0)) {
1869 IWL_DEBUG_RATE("decrease rate because of low success_ratio\n");
1870 scale_action = -1;
Ben Cahill77626352007-11-29 11:09:44 +08001871
1872 /* No throughput measured yet for adjacent rates; try increase. */
Zhu Yib481de92007-09-25 17:54:57 -07001873 } else if ((low_tpt == IWL_INVALID_VALUE) &&
1874 (high_tpt == IWL_INVALID_VALUE))
1875 scale_action = 1;
Ben Cahill77626352007-11-29 11:09:44 +08001876
1877 /* Both adjacent throughputs are measured, but neither one has better
1878 * throughput; we're using the best rate, don't change it! */
Zhu Yib481de92007-09-25 17:54:57 -07001879 else if ((low_tpt != IWL_INVALID_VALUE) &&
1880 (high_tpt != IWL_INVALID_VALUE) &&
1881 (low_tpt < current_tpt) &&
1882 (high_tpt < current_tpt))
1883 scale_action = 0;
Ben Cahill77626352007-11-29 11:09:44 +08001884
1885 /* At least one adjacent rate's throughput is measured,
1886 * and may have better performance. */
Zhu Yib481de92007-09-25 17:54:57 -07001887 else {
Ben Cahill77626352007-11-29 11:09:44 +08001888 /* Higher adjacent rate's throughput is measured */
Zhu Yib481de92007-09-25 17:54:57 -07001889 if (high_tpt != IWL_INVALID_VALUE) {
Ben Cahill77626352007-11-29 11:09:44 +08001890 /* Higher rate has better throughput */
Zhu Yib481de92007-09-25 17:54:57 -07001891 if (high_tpt > current_tpt)
1892 scale_action = 1;
1893 else {
1894 IWL_DEBUG_RATE
1895 ("decrease rate because of high tpt\n");
1896 scale_action = -1;
1897 }
Ben Cahill77626352007-11-29 11:09:44 +08001898
1899 /* Lower adjacent rate's throughput is measured */
Zhu Yib481de92007-09-25 17:54:57 -07001900 } else if (low_tpt != IWL_INVALID_VALUE) {
Ben Cahill77626352007-11-29 11:09:44 +08001901 /* Lower rate has better throughput */
Zhu Yib481de92007-09-25 17:54:57 -07001902 if (low_tpt > current_tpt) {
1903 IWL_DEBUG_RATE
1904 ("decrease rate because of low tpt\n");
1905 scale_action = -1;
1906 } else
1907 scale_action = 1;
1908 }
1909 }
1910
Ben Cahill77626352007-11-29 11:09:44 +08001911 /* Sanity check; asked for decrease, but success rate or throughput
1912 * has been good at old rate. Don't change it. */
Zhu Yib481de92007-09-25 17:54:57 -07001913 if (scale_action == -1) {
1914 if ((low != IWL_RATE_INVALID) &&
1915 ((window->success_ratio > IWL_RATE_HIGH_TH) ||
1916 (current_tpt > (100 * tbl->expected_tpt[low]))))
1917 scale_action = 0;
Ben Cahill77626352007-11-29 11:09:44 +08001918
1919 /* Sanity check; asked for increase, but success rate has not been great
1920 * even at old rate, higher rate will be worse. Don't change it. */
Zhu Yib481de92007-09-25 17:54:57 -07001921 } else if ((scale_action == 1) &&
1922 (window->success_ratio < IWL_RATE_INCREASE_TH))
1923 scale_action = 0;
1924
1925 switch (scale_action) {
1926 case -1:
Ben Cahill77626352007-11-29 11:09:44 +08001927 /* Decrease starting rate, update uCode's rate table */
Zhu Yib481de92007-09-25 17:54:57 -07001928 if (low != IWL_RATE_INVALID) {
1929 update_lq = 1;
1930 index = low;
1931 }
1932 break;
1933 case 1:
Ben Cahill77626352007-11-29 11:09:44 +08001934 /* Increase starting rate, update uCode's rate table */
Zhu Yib481de92007-09-25 17:54:57 -07001935 if (high != IWL_RATE_INVALID) {
1936 update_lq = 1;
1937 index = high;
1938 }
1939
1940 break;
1941 case 0:
Ben Cahill77626352007-11-29 11:09:44 +08001942 /* No change */
Zhu Yib481de92007-09-25 17:54:57 -07001943 default:
1944 break;
1945 }
1946
Guy Coheneecd6e52008-04-23 17:14:58 -07001947 IWL_DEBUG_RATE("choose rate scale index %d action %d low %d "
Zhu Yib481de92007-09-25 17:54:57 -07001948 "high %d type %d\n",
1949 index, scale_action, low, high, tbl->lq_type);
1950
1951 lq_update:
Ben Cahill77626352007-11-29 11:09:44 +08001952 /* Replace uCode's rate table for the destination station. */
Zhu Yib481de92007-09-25 17:54:57 -07001953 if (update_lq) {
Guy Cohen39e88502008-04-23 17:14:57 -07001954 rate = rate_n_flags_from_tbl(tbl, index, is_green);
1955 rs_fill_link_cmd(priv, lq_sta, rate, &lq_sta->lq);
Tomas Winkler66c73db2008-04-15 16:01:40 -07001956 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07001957 }
Ben Cahill77626352007-11-29 11:09:44 +08001958
1959 /* Should we stay with this modulation mode, or search for a new one? */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001960 rs_stay_in_table(lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07001961
Ben Cahill77626352007-11-29 11:09:44 +08001962 /*
1963 * Search for new modulation mode if we're:
1964 * 1) Not changing rates right now
1965 * 2) Not just finishing up a search
1966 * 3) Allowing a new search
1967 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001968 if (!update_lq && !done_search && !lq_sta->stay_in_tbl) {
Ben Cahill77626352007-11-29 11:09:44 +08001969 /* Save current throughput to compare with "search" throughput*/
Tomas Winklerc33104f2008-01-14 17:46:21 -08001970 lq_sta->last_tpt = current_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07001971
Ben Cahill77626352007-11-29 11:09:44 +08001972 /* Select a new "search" modulation mode to try.
1973 * If one is found, set up the new "search" table. */
Zhu Yib481de92007-09-25 17:54:57 -07001974 if (is_legacy(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08001975 rs_move_legacy_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07001976 else if (is_siso(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08001977 rs_move_siso_to_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07001978 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08001979 rs_move_mimo_to_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07001980
Ben Cahill77626352007-11-29 11:09:44 +08001981 /* If new "search" mode was selected, set up in uCode table */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001982 if (lq_sta->search_better_tbl) {
Ben Cahill77626352007-11-29 11:09:44 +08001983 /* Access the "search" table, clear its history. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001984 tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Zhu Yib481de92007-09-25 17:54:57 -07001985 for (i = 0; i < IWL_RATE_COUNT; i++)
1986 rs_rate_scale_clear_window(&(tbl->win[i]));
1987
Ben Cahill77626352007-11-29 11:09:44 +08001988 /* Use new "search" start rate */
Tomas Winkler17744ff2008-03-02 01:52:00 +02001989 index = iwl4965_hwrate_to_plcp_idx(
Guy Cohen39e88502008-04-23 17:14:57 -07001990 tbl->current_rate);
Zhu Yib481de92007-09-25 17:54:57 -07001991
Guy Coheneecd6e52008-04-23 17:14:58 -07001992 IWL_DEBUG_RATE("Switch current mcs: %X index: %d\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001993 tbl->current_rate, index);
1994 rs_fill_link_cmd(priv, lq_sta, tbl->current_rate,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001995 &lq_sta->lq);
Tomas Winkler66c73db2008-04-15 16:01:40 -07001996 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07001997 }
Zhu Yib481de92007-09-25 17:54:57 -07001998
Ben Cahill77626352007-11-29 11:09:44 +08001999 /* If the "active" (non-search) mode was legacy,
2000 * and we've tried switching antennas,
2001 * but we haven't been able to try HT modes (not available),
2002 * stay with best antenna legacy modulation for a while
2003 * before next round of mode comparisons. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002004 tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07002005 if (is_legacy(tbl1->lq_type) &&
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002006#ifdef CONFIG_IWL4965_HT
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002007 (!(conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE)) &&
Zhu Yib481de92007-09-25 17:54:57 -07002008#endif
Tomas Winklerc33104f2008-01-14 17:46:21 -08002009 (lq_sta->action_counter >= 1)) {
2010 lq_sta->action_counter = 0;
Guy Coheneecd6e52008-04-23 17:14:58 -07002011 IWL_DEBUG_RATE("LQ: STAY in legacy table\n");
Tomas Winklerc33104f2008-01-14 17:46:21 -08002012 rs_set_stay_in_table(1, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002013 }
2014
Ben Cahill77626352007-11-29 11:09:44 +08002015 /* If we're in an HT mode, and all 3 mode switch actions
2016 * have been tried and compared, stay in this best modulation
2017 * mode for a while before next round of mode comparisons. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002018 if (lq_sta->enable_counter &&
2019 (lq_sta->action_counter >= IWL_ACTION_LIMIT)) {
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002020#ifdef CONFIG_IWL4965_HT
2021 if ((lq_sta->last_tpt > IWL_AGG_TPT_THREHOLD) &&
2022 (lq_sta->tx_agg_tid_en & (1 << tid)) &&
2023 (tid != MAX_TID_COUNT)) {
Guy Coheneecd6e52008-04-23 17:14:58 -07002024 IWL_DEBUG_RATE("try to aggregate tid %d\n", tid);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002025 rs_tl_turn_on_agg(priv, tid, lq_sta, sta);
Zhu Yib481de92007-09-25 17:54:57 -07002026 }
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002027#endif /*CONFIG_IWL4965_HT */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002028 lq_sta->action_counter = 0;
2029 rs_set_stay_in_table(0, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002030 }
Ben Cahill77626352007-11-29 11:09:44 +08002031
2032 /*
2033 * Else, don't search for a new modulation mode.
2034 * Put new timestamp in stay-in-modulation-mode flush timer if:
2035 * 1) Not changing rates right now
2036 * 2) Not just finishing up a search
2037 * 3) flush timer is empty
2038 */
Zhu Yib481de92007-09-25 17:54:57 -07002039 } else {
Tomas Winklerc33104f2008-01-14 17:46:21 -08002040 if ((!update_lq) && (!done_search) && (!lq_sta->flush_timer))
2041 lq_sta->flush_timer = jiffies;
Zhu Yib481de92007-09-25 17:54:57 -07002042 }
2043
2044out:
Guy Cohen39e88502008-04-23 17:14:57 -07002045 tbl->current_rate = rate_n_flags_from_tbl(tbl, index, is_green);
Zhu Yib481de92007-09-25 17:54:57 -07002046 i = index;
Johannes Berg8318d782008-01-24 19:38:38 +01002047 sta->last_txrate_idx = i;
Zhu Yib481de92007-09-25 17:54:57 -07002048
Johannes Berg8318d782008-01-24 19:38:38 +01002049 /* sta->txrate_idx is an index to A mode rates which start
Zhu Yib481de92007-09-25 17:54:57 -07002050 * at IWL_FIRST_OFDM_RATE
2051 */
Johannes Berg8318d782008-01-24 19:38:38 +01002052 if (lq_sta->band == IEEE80211_BAND_5GHZ)
2053 sta->txrate_idx = i - IWL_FIRST_OFDM_RATE;
Zhu Yib481de92007-09-25 17:54:57 -07002054 else
Johannes Berg8318d782008-01-24 19:38:38 +01002055 sta->txrate_idx = i;
Zhu Yib481de92007-09-25 17:54:57 -07002056
2057 return;
2058}
2059
2060
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002061static void rs_initialize_lq(struct iwl_priv *priv,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002062 struct ieee80211_conf *conf,
Zhu Yib481de92007-09-25 17:54:57 -07002063 struct sta_info *sta)
2064{
Tomas Winklerc33104f2008-01-14 17:46:21 -08002065 struct iwl4965_lq_sta *lq_sta;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002066 struct iwl4965_scale_tbl_info *tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002067 int rate_idx;
Guy Cohenaade00c2008-04-23 17:14:59 -07002068 int i;
Guy Cohen39e88502008-04-23 17:14:57 -07002069 u32 rate;
Guy Cohenaade00c2008-04-23 17:14:59 -07002070 u8 use_green = rs_use_green(priv, conf);
2071 u8 active_tbl = 0;
2072 u8 valid_tx_ant;
Zhu Yib481de92007-09-25 17:54:57 -07002073
2074 if (!sta || !sta->rate_ctrl_priv)
2075 goto out;
2076
Tomas Winklerc33104f2008-01-14 17:46:21 -08002077 lq_sta = (struct iwl4965_lq_sta *)sta->rate_ctrl_priv;
Johannes Berg8318d782008-01-24 19:38:38 +01002078 i = sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002079
Tomas Winklerc33104f2008-01-14 17:46:21 -08002080 if ((lq_sta->lq.sta_id == 0xff) &&
Zhu Yib481de92007-09-25 17:54:57 -07002081 (priv->iw_mode == IEEE80211_IF_TYPE_IBSS))
2082 goto out;
2083
Guy Cohenaade00c2008-04-23 17:14:59 -07002084 valid_tx_ant = priv->hw_params.valid_tx_ant;
2085
Tomas Winklerc33104f2008-01-14 17:46:21 -08002086 if (!lq_sta->search_better_tbl)
2087 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002088 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08002089 active_tbl = 1 - lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002090
Tomas Winklerc33104f2008-01-14 17:46:21 -08002091 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07002092
2093 if ((i < 0) || (i >= IWL_RATE_COUNT))
2094 i = 0;
2095
Guy Cohenfde0db32008-04-21 15:42:01 -07002096 /* FIXME:RS: This is also wrong in 4965 */
Guy Cohen39e88502008-04-23 17:14:57 -07002097 rate = iwl4965_rates[i].plcp;
2098 rate |= RATE_MCS_ANT_B_MSK;
2099 rate &= ~RATE_MCS_ANT_A_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002100
2101 if (i >= IWL_FIRST_CCK_RATE && i <= IWL_LAST_CCK_RATE)
Guy Cohen39e88502008-04-23 17:14:57 -07002102 rate |= RATE_MCS_CCK_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002103
Guy Cohenfde0db32008-04-21 15:42:01 -07002104 tbl->ant_type = ANT_B;
Guy Cohen39e88502008-04-23 17:14:57 -07002105 rs_get_tbl_info_from_mcs(rate, priv->band, tbl, &rate_idx);
Guy Cohenaade00c2008-04-23 17:14:59 -07002106 if (!rs_is_valid_ant(valid_tx_ant, tbl->ant_type))
2107 rs_toggle_antenna(valid_tx_ant, &rate, tbl);
Zhu Yib481de92007-09-25 17:54:57 -07002108
Guy Cohen39e88502008-04-23 17:14:57 -07002109 rate = rate_n_flags_from_tbl(tbl, rate_idx, use_green);
2110 tbl->current_rate = rate;
Guy Coheneecd6e52008-04-23 17:14:58 -07002111 rs_set_expected_tpt_table(lq_sta, tbl);
Guy Cohen39e88502008-04-23 17:14:57 -07002112 rs_fill_link_cmd(NULL, lq_sta, rate, &lq_sta->lq);
Tomas Winkler66c73db2008-04-15 16:01:40 -07002113 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07002114 out:
2115 return;
2116}
2117
Mattias Nissler1abbe492007-12-20 13:50:07 +01002118static void rs_get_rate(void *priv_rate, struct net_device *dev,
Johannes Berg8318d782008-01-24 19:38:38 +01002119 struct ieee80211_supported_band *sband,
2120 struct sk_buff *skb,
Mattias Nissler1abbe492007-12-20 13:50:07 +01002121 struct rate_selection *sel)
Zhu Yib481de92007-09-25 17:54:57 -07002122{
2123
2124 int i;
2125 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002126 struct ieee80211_conf *conf = &local->hw.conf;
Zhu Yib481de92007-09-25 17:54:57 -07002127 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2128 struct sta_info *sta;
Michael Wu2bc454b2007-12-25 19:33:16 -05002129 u16 fc;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002130 struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002131 struct iwl4965_lq_sta *lq_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002132
Zhu Yi58826352007-09-27 11:27:39 +08002133 IWL_DEBUG_RATE_LIMIT("rate scale calculate new rate for skb\n");
Zhu Yib481de92007-09-25 17:54:57 -07002134
Johannes Bergd0709a62008-02-25 16:27:46 +01002135 rcu_read_lock();
2136
Zhu Yib481de92007-09-25 17:54:57 -07002137 sta = sta_info_get(local, hdr->addr1);
2138
Michael Wu2bc454b2007-12-25 19:33:16 -05002139 /* Send management frames and broadcast/multicast data using lowest
2140 * rate. */
2141 fc = le16_to_cpu(hdr->frame_control);
2142 if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1) ||
2143 !sta || !sta->rate_ctrl_priv) {
Johannes Berg8318d782008-01-24 19:38:38 +01002144 sel->rate = rate_lowest(local, sband, sta);
Tomas Winklerf4d60822008-03-05 11:31:00 -08002145 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07002146 }
2147
Tomas Winklerc33104f2008-01-14 17:46:21 -08002148 lq_sta = (struct iwl4965_lq_sta *)sta->rate_ctrl_priv;
Johannes Berg8318d782008-01-24 19:38:38 +01002149 i = sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002150
Tomas Winklerc33104f2008-01-14 17:46:21 -08002151 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
2152 !lq_sta->ibss_sta_added) {
Tomas Winkler947b13a2008-04-16 16:34:48 -07002153 u8 sta_id = iwl_find_station(priv, hdr->addr1);
Joe Perches0795af52007-10-03 17:59:30 -07002154 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07002155
2156 if (sta_id == IWL_INVALID_STATION) {
Joe Perches0795af52007-10-03 17:59:30 -07002157 IWL_DEBUG_RATE("LQ: ADD station %s\n",
2158 print_mac(mac, hdr->addr1));
Ron Rindjunsky67d62032007-11-26 16:14:40 +02002159 sta_id = iwl4965_add_station_flags(priv, hdr->addr1,
2160 0, CMD_ASYNC, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07002161 }
2162 if ((sta_id != IWL_INVALID_STATION)) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08002163 lq_sta->lq.sta_id = sta_id;
2164 lq_sta->lq.rs_table[0].rate_n_flags = 0;
2165 lq_sta->ibss_sta_added = 1;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002166 rs_initialize_lq(priv, conf, sta);
Zhu Yib481de92007-09-25 17:54:57 -07002167 }
Tomas Winklerc33104f2008-01-14 17:46:21 -08002168 if (!lq_sta->ibss_sta_added)
Zhu Yib481de92007-09-25 17:54:57 -07002169 goto done;
2170 }
2171
Tomas Winklerf4d60822008-03-05 11:31:00 -08002172done:
Mattias Nissler1abbe492007-12-20 13:50:07 +01002173 if ((i < 0) || (i > IWL_RATE_COUNT)) {
Johannes Berg8318d782008-01-24 19:38:38 +01002174 sel->rate = rate_lowest(local, sband, sta);
Tomas Winklerf4d60822008-03-05 11:31:00 -08002175 goto out;
Mattias Nissler1abbe492007-12-20 13:50:07 +01002176 }
Zhu Yib481de92007-09-25 17:54:57 -07002177
Mattias Nissler1abbe492007-12-20 13:50:07 +01002178 sel->rate = &priv->ieee_rates[i];
Tomas Winklerf4d60822008-03-05 11:31:00 -08002179out:
2180 rcu_read_unlock();
Zhu Yib481de92007-09-25 17:54:57 -07002181}
2182
2183static void *rs_alloc_sta(void *priv, gfp_t gfp)
2184{
Tomas Winklerc33104f2008-01-14 17:46:21 -08002185 struct iwl4965_lq_sta *lq_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002186 int i, j;
2187
2188 IWL_DEBUG_RATE("create station rate scale window\n");
2189
Tomas Winklerc33104f2008-01-14 17:46:21 -08002190 lq_sta = kzalloc(sizeof(struct iwl4965_lq_sta), gfp);
Zhu Yib481de92007-09-25 17:54:57 -07002191
Tomas Winklerc33104f2008-01-14 17:46:21 -08002192 if (lq_sta == NULL)
Zhu Yib481de92007-09-25 17:54:57 -07002193 return NULL;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002194 lq_sta->lq.sta_id = 0xff;
Zhu Yib481de92007-09-25 17:54:57 -07002195
Zhu Yi63fddb92007-09-27 11:27:36 +08002196
Zhu Yib481de92007-09-25 17:54:57 -07002197 for (j = 0; j < LQ_SIZE; j++)
2198 for (i = 0; i < IWL_RATE_COUNT; i++)
Tomas Winklerc33104f2008-01-14 17:46:21 -08002199 rs_rate_scale_clear_window(&(lq_sta->lq_info[j].win[i]));
Zhu Yib481de92007-09-25 17:54:57 -07002200
Tomas Winklerc33104f2008-01-14 17:46:21 -08002201 return lq_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002202}
2203
2204static void rs_rate_init(void *priv_rate, void *priv_sta,
2205 struct ieee80211_local *local,
2206 struct sta_info *sta)
2207{
2208 int i, j;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002209 struct ieee80211_conf *conf = &local->hw.conf;
Johannes Berg8318d782008-01-24 19:38:38 +01002210 struct ieee80211_supported_band *sband;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002211 struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002212 struct iwl4965_lq_sta *lq_sta = priv_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002213
Johannes Berg8318d782008-01-24 19:38:38 +01002214 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2215
Tomas Winklerc33104f2008-01-14 17:46:21 -08002216 lq_sta->flush_timer = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01002217 lq_sta->supp_rates = sta->supp_rates[sband->band];
2218 sta->txrate_idx = 3;
Zhu Yib481de92007-09-25 17:54:57 -07002219 for (j = 0; j < LQ_SIZE; j++)
2220 for (i = 0; i < IWL_RATE_COUNT; i++)
Tomas Winklerc33104f2008-01-14 17:46:21 -08002221 rs_rate_scale_clear_window(&(lq_sta->lq_info[j].win[i]));
Zhu Yib481de92007-09-25 17:54:57 -07002222
2223 IWL_DEBUG_RATE("rate scale global init\n");
2224 /* TODO: what is a good starting rate for STA? About middle? Maybe not
2225 * the lowest or the highest rate.. Could consider using RSSI from
2226 * previous packets? Need to have IEEE 802.1X auth succeed immediately
2227 * after assoc.. */
2228
Tomas Winklerc33104f2008-01-14 17:46:21 -08002229 lq_sta->ibss_sta_added = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002230 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
Tomas Winkler947b13a2008-04-16 16:34:48 -07002231 u8 sta_id = iwl_find_station(priv, sta->addr);
Joe Perches0795af52007-10-03 17:59:30 -07002232 DECLARE_MAC_BUF(mac);
2233
Zhu Yib481de92007-09-25 17:54:57 -07002234 /* for IBSS the call are from tasklet */
Guy Coheneecd6e52008-04-23 17:14:58 -07002235 IWL_DEBUG_RATE("LQ: ADD station %s\n",
Joe Perches0795af52007-10-03 17:59:30 -07002236 print_mac(mac, sta->addr));
Zhu Yib481de92007-09-25 17:54:57 -07002237
2238 if (sta_id == IWL_INVALID_STATION) {
Joe Perches0795af52007-10-03 17:59:30 -07002239 IWL_DEBUG_RATE("LQ: ADD station %s\n",
2240 print_mac(mac, sta->addr));
Ron Rindjunsky67d62032007-11-26 16:14:40 +02002241 sta_id = iwl4965_add_station_flags(priv, sta->addr,
2242 0, CMD_ASYNC, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07002243 }
2244 if ((sta_id != IWL_INVALID_STATION)) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08002245 lq_sta->lq.sta_id = sta_id;
2246 lq_sta->lq.rs_table[0].rate_n_flags = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002247 }
2248 /* FIXME: this is w/a remove it later */
2249 priv->assoc_station_added = 1;
2250 }
2251
Ben Cahill77626352007-11-29 11:09:44 +08002252 /* Find highest tx rate supported by hardware and destination station */
Johannes Berg8318d782008-01-24 19:38:38 +01002253 for (i = 0; i < sband->n_bitrates; i++)
2254 if (sta->supp_rates[sband->band] & BIT(i))
2255 sta->txrate_idx = i;
2256
2257 sta->last_txrate_idx = sta->txrate_idx;
2258 /* WTF is with this bogus comment? A doesn't have cck rates */
Ben Cahill77626352007-11-29 11:09:44 +08002259 /* For MODE_IEEE80211A, cck rates are at end of rate table */
Johannes Berg8318d782008-01-24 19:38:38 +01002260 if (local->hw.conf.channel->band == IEEE80211_BAND_5GHZ)
2261 sta->last_txrate_idx += IWL_FIRST_OFDM_RATE;
Zhu Yib481de92007-09-25 17:54:57 -07002262
Tomas Winklerc33104f2008-01-14 17:46:21 -08002263 lq_sta->is_dup = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002264 lq_sta->is_green = rs_use_green(priv, conf);
Guy Cohen39e88502008-04-23 17:14:57 -07002265 lq_sta->active_legacy_rate = priv->active_rate & ~(0x1000);
Tomas Winklerc33104f2008-01-14 17:46:21 -08002266 lq_sta->active_rate_basic = priv->active_rate_basic;
Johannes Berg8318d782008-01-24 19:38:38 +01002267 lq_sta->band = priv->band;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002268#ifdef CONFIG_IWL4965_HT
Ben Cahill77626352007-11-29 11:09:44 +08002269 /*
2270 * active_siso_rate mask includes 9 MBits (bit 5), and CCK (bits 0-3),
2271 * supp_rates[] does not; shift to convert format, force 9 MBits off.
2272 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002273 lq_sta->active_siso_rate =
Guy Cohenfde0db32008-04-21 15:42:01 -07002274 priv->current_ht_config.supp_mcs_set[0] << 1;
2275 lq_sta->active_siso_rate |=
2276 priv->current_ht_config.supp_mcs_set[0] & 0x1;
2277 lq_sta->active_siso_rate &= ~((u16)0x2);
2278 lq_sta->active_siso_rate <<= IWL_FIRST_OFDM_RATE;
Zhu Yib481de92007-09-25 17:54:57 -07002279
Ben Cahill77626352007-11-29 11:09:44 +08002280 /* Same here */
Guy Cohenfde0db32008-04-21 15:42:01 -07002281 lq_sta->active_mimo2_rate =
2282 priv->current_ht_config.supp_mcs_set[1] << 1;
2283 lq_sta->active_mimo2_rate |=
2284 priv->current_ht_config.supp_mcs_set[1] & 0x1;
2285 lq_sta->active_mimo2_rate &= ~((u16)0x2);
2286 lq_sta->active_mimo2_rate <<= IWL_FIRST_OFDM_RATE;
2287
2288 lq_sta->active_mimo3_rate =
2289 priv->current_ht_config.supp_mcs_set[2] << 1;
2290 lq_sta->active_mimo3_rate |=
2291 priv->current_ht_config.supp_mcs_set[2] & 0x1;
2292 lq_sta->active_mimo3_rate &= ~((u16)0x2);
2293 lq_sta->active_mimo3_rate <<= IWL_FIRST_OFDM_RATE;
2294
Guy Coheneecd6e52008-04-23 17:14:58 -07002295 IWL_DEBUG_RATE("SISO RATE %X MIMO2 RATE %X MIMO3 RATE %X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002296 lq_sta->active_siso_rate,
Guy Cohenfde0db32008-04-21 15:42:01 -07002297 lq_sta->active_mimo2_rate,
2298 lq_sta->active_mimo3_rate);
2299
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002300 /* as default allow aggregation for all tids */
2301 lq_sta->tx_agg_tid_en = IWL_AGG_ALL_TID;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002302#endif /*CONFIG_IWL4965_HT*/
Zhu Yi98d7e092007-09-27 11:27:42 +08002303#ifdef CONFIG_MAC80211_DEBUGFS
Tomas Winklerc33104f2008-01-14 17:46:21 -08002304 lq_sta->drv = priv;
Zhu Yi98d7e092007-09-27 11:27:42 +08002305#endif
Zhu Yib481de92007-09-25 17:54:57 -07002306
2307 if (priv->assoc_station_added)
2308 priv->lq_mngr.lq_ready = 1;
2309
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002310 rs_initialize_lq(priv, conf, sta);
Zhu Yib481de92007-09-25 17:54:57 -07002311}
2312
Guy Cohenfde0db32008-04-21 15:42:01 -07002313static void rs_fill_link_cmd(const struct iwl_priv *priv,
2314 struct iwl4965_lq_sta *lq_sta,
Guy Cohen39e88502008-04-23 17:14:57 -07002315 u32 new_rate,
Guy Cohenfde0db32008-04-21 15:42:01 -07002316 struct iwl_link_quality_cmd *lq_cmd)
Zhu Yib481de92007-09-25 17:54:57 -07002317{
Guy Cohenaade00c2008-04-23 17:14:59 -07002318 struct iwl4965_scale_tbl_info tbl_type;
Zhu Yib481de92007-09-25 17:54:57 -07002319 int index = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002320 int rate_idx;
Zhu Yi1b696de2007-09-27 11:27:41 +08002321 int repeat_rate = 0;
Guy Cohenaade00c2008-04-23 17:14:59 -07002322 u8 ant_toggle_cnt = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002323 u8 use_ht_possible = 1;
Guy Cohenaade00c2008-04-23 17:14:59 -07002324 u8 valid_tx_ant = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002325
Ben Cahill77626352007-11-29 11:09:44 +08002326 /* Override starting rate (index 0) if needed for debug purposes */
Guy Cohen39e88502008-04-23 17:14:57 -07002327 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Zhu Yi98d7e092007-09-27 11:27:42 +08002328
Guy Cohen39e88502008-04-23 17:14:57 -07002329 /* Interpret new_rate (rate_n_flags) */
Guy Cohenaade00c2008-04-23 17:14:59 -07002330 memset(&tbl_type, 0, sizeof(tbl_type));
Guy Cohen39e88502008-04-23 17:14:57 -07002331 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band,
Zhu Yib481de92007-09-25 17:54:57 -07002332 &tbl_type, &rate_idx);
2333
Ben Cahill77626352007-11-29 11:09:44 +08002334 /* How many times should we repeat the initial rate? */
Zhu Yib481de92007-09-25 17:54:57 -07002335 if (is_legacy(tbl_type.lq_type)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07002336 ant_toggle_cnt = 1;
Zhu Yi1b696de2007-09-27 11:27:41 +08002337 repeat_rate = IWL_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002338 } else {
Zhu Yi1b696de2007-09-27 11:27:41 +08002339 repeat_rate = IWL_HT_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002340 }
Zhu Yib481de92007-09-25 17:54:57 -07002341
2342 lq_cmd->general_params.mimo_delimiter =
2343 is_mimo(tbl_type.lq_type) ? 1 : 0;
Ben Cahill77626352007-11-29 11:09:44 +08002344
2345 /* Fill 1st table entry (index 0) */
Guy Cohen39e88502008-04-23 17:14:57 -07002346 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
Zhu Yib481de92007-09-25 17:54:57 -07002347
Guy Cohenfde0db32008-04-21 15:42:01 -07002348 /*FIXME:RS*/
2349 if (is_mimo(tbl_type.lq_type) || (tbl_type.ant_type == ANT_A))
Ben Cahill77626352007-11-29 11:09:44 +08002350 lq_cmd->general_params.single_stream_ant_msk
2351 = LINK_QUAL_ANT_A_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002352 else
Ben Cahill77626352007-11-29 11:09:44 +08002353 lq_cmd->general_params.single_stream_ant_msk
2354 = LINK_QUAL_ANT_B_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002355
2356 index++;
Zhu Yi1b696de2007-09-27 11:27:41 +08002357 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002358
Guy Cohenaade00c2008-04-23 17:14:59 -07002359 if (priv)
2360 valid_tx_ant = priv->hw_params.valid_tx_ant;
2361
Ben Cahill77626352007-11-29 11:09:44 +08002362 /* Fill rest of rate table */
Zhu Yib481de92007-09-25 17:54:57 -07002363 while (index < LINK_QUAL_MAX_RETRY_NUM) {
Ben Cahill77626352007-11-29 11:09:44 +08002364 /* Repeat initial/next rate.
2365 * For legacy IWL_NUMBER_TRY == 1, this loop will not execute.
2366 * For HT IWL_HT_NUMBER_TRY == 3, this executes twice. */
Zhu Yi1b696de2007-09-27 11:27:41 +08002367 while (repeat_rate > 0 && (index < LINK_QUAL_MAX_RETRY_NUM)) {
Zhu Yib481de92007-09-25 17:54:57 -07002368 if (is_legacy(tbl_type.lq_type)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07002369 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2370 ant_toggle_cnt++;
2371 else if (priv &&
2372 rs_toggle_antenna(valid_tx_ant,
2373 &new_rate, &tbl_type))
2374 ant_toggle_cnt = 1;
2375}
Zhu Yi98d7e092007-09-27 11:27:42 +08002376
Ben Cahill77626352007-11-29 11:09:44 +08002377 /* Override next rate if needed for debug purposes */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002378 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Ben Cahill77626352007-11-29 11:09:44 +08002379
2380 /* Fill next table entry */
Zhu Yib481de92007-09-25 17:54:57 -07002381 lq_cmd->rs_table[index].rate_n_flags =
Guy Cohen39e88502008-04-23 17:14:57 -07002382 cpu_to_le32(new_rate);
Zhu Yi1b696de2007-09-27 11:27:41 +08002383 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002384 index++;
2385 }
2386
Guy Cohen39e88502008-04-23 17:14:57 -07002387 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band, &tbl_type,
Zhu Yib481de92007-09-25 17:54:57 -07002388 &rate_idx);
2389
Ben Cahill77626352007-11-29 11:09:44 +08002390 /* Indicate to uCode which entries might be MIMO.
2391 * If initial rate was MIMO, this will finally end up
2392 * as (IWL_HT_NUMBER_TRY * 2), after 2nd pass, otherwise 0. */
Zhu Yib481de92007-09-25 17:54:57 -07002393 if (is_mimo(tbl_type.lq_type))
2394 lq_cmd->general_params.mimo_delimiter = index;
2395
Ben Cahill77626352007-11-29 11:09:44 +08002396 /* Get next rate */
Guy Cohen39e88502008-04-23 17:14:57 -07002397 new_rate = rs_get_lower_rate(lq_sta, &tbl_type, rate_idx,
2398 use_ht_possible);
Zhu Yib481de92007-09-25 17:54:57 -07002399
Ben Cahill77626352007-11-29 11:09:44 +08002400 /* How many times should we repeat the next rate? */
Zhu Yib481de92007-09-25 17:54:57 -07002401 if (is_legacy(tbl_type.lq_type)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07002402 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2403 ant_toggle_cnt++;
2404 else if (priv &&
2405 rs_toggle_antenna(valid_tx_ant,
2406 &new_rate, &tbl_type))
2407 ant_toggle_cnt = 1;
2408
Zhu Yi1b696de2007-09-27 11:27:41 +08002409 repeat_rate = IWL_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002410 } else {
Zhu Yi1b696de2007-09-27 11:27:41 +08002411 repeat_rate = IWL_HT_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002412 }
Zhu Yib481de92007-09-25 17:54:57 -07002413
Ben Cahill77626352007-11-29 11:09:44 +08002414 /* Don't allow HT rates after next pass.
2415 * rs_get_lower_rate() will change type to LQ_A or LQ_G. */
Zhu Yib481de92007-09-25 17:54:57 -07002416 use_ht_possible = 0;
2417
Ben Cahill77626352007-11-29 11:09:44 +08002418 /* Override next rate if needed for debug purposes */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002419 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Ben Cahill77626352007-11-29 11:09:44 +08002420
2421 /* Fill next table entry */
Guy Cohen39e88502008-04-23 17:14:57 -07002422 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
Zhu Yib481de92007-09-25 17:54:57 -07002423
2424 index++;
Zhu Yi1b696de2007-09-27 11:27:41 +08002425 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002426 }
2427
Zhu Yib481de92007-09-25 17:54:57 -07002428 lq_cmd->general_params.dual_stream_ant_msk = 3;
2429 lq_cmd->agg_params.agg_dis_start_th = 3;
2430 lq_cmd->agg_params.agg_time_limit = cpu_to_le16(4000);
Zhu Yib481de92007-09-25 17:54:57 -07002431}
2432
2433static void *rs_alloc(struct ieee80211_local *local)
2434{
2435 return local->hw.priv;
2436}
2437/* rate scale requires free function to be implemented */
2438static void rs_free(void *priv_rate)
2439{
2440 return;
2441}
2442
2443static void rs_clear(void *priv_rate)
2444{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002445 struct iwl_priv *priv = (struct iwl_priv *) priv_rate;
Zhu Yib481de92007-09-25 17:54:57 -07002446
2447 IWL_DEBUG_RATE("enter\n");
2448
2449 priv->lq_mngr.lq_ready = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002450
2451 IWL_DEBUG_RATE("leave\n");
2452}
2453
2454static void rs_free_sta(void *priv, void *priv_sta)
2455{
Tomas Winklerc33104f2008-01-14 17:46:21 -08002456 struct iwl4965_lq_sta *lq_sta = priv_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002457
2458 IWL_DEBUG_RATE("enter\n");
Tomas Winklerc33104f2008-01-14 17:46:21 -08002459 kfree(lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002460 IWL_DEBUG_RATE("leave\n");
2461}
2462
2463
Zhu Yi93dc6462007-09-27 11:27:37 +08002464#ifdef CONFIG_MAC80211_DEBUGFS
Zhu Yi5ae212c2007-09-27 11:27:38 +08002465static int open_file_generic(struct inode *inode, struct file *file)
2466{
2467 file->private_data = inode->i_private;
2468 return 0;
2469}
Tomas Winklerc33104f2008-01-14 17:46:21 -08002470static void rs_dbgfs_set_mcs(struct iwl4965_lq_sta *lq_sta,
Guy Cohen39e88502008-04-23 17:14:57 -07002471 u32 *rate_n_flags, int index)
Zhu Yi98d7e092007-09-27 11:27:42 +08002472{
Guy Cohen39e88502008-04-23 17:14:57 -07002473 if (lq_sta->dbg_fixed_rate) {
2474 if (index < 12) {
2475 *rate_n_flags = lq_sta->dbg_fixed_rate;
2476 } else {
2477 if (lq_sta->band == IEEE80211_BAND_5GHZ)
2478 *rate_n_flags = 0x800D;
2479 else
2480 *rate_n_flags = 0x820A;
2481 }
Zhu Yi98d7e092007-09-27 11:27:42 +08002482 IWL_DEBUG_RATE("Fixed rate ON\n");
Guy Cohen39e88502008-04-23 17:14:57 -07002483 } else {
2484 IWL_DEBUG_RATE("Fixed rate OFF\n");
Zhu Yi98d7e092007-09-27 11:27:42 +08002485 }
Zhu Yi98d7e092007-09-27 11:27:42 +08002486}
2487
2488static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
2489 const char __user *user_buf, size_t count, loff_t *ppos)
2490{
Tomas Winklerc33104f2008-01-14 17:46:21 -08002491 struct iwl4965_lq_sta *lq_sta = file->private_data;
Zhu Yi98d7e092007-09-27 11:27:42 +08002492 char buf[64];
2493 int buf_size;
2494 u32 parsed_rate;
2495
2496 memset(buf, 0, sizeof(buf));
2497 buf_size = min(count, sizeof(buf) - 1);
2498 if (copy_from_user(buf, user_buf, buf_size))
2499 return -EFAULT;
2500
2501 if (sscanf(buf, "%x", &parsed_rate) == 1)
Guy Cohen39e88502008-04-23 17:14:57 -07002502 lq_sta->dbg_fixed_rate = parsed_rate;
Zhu Yi98d7e092007-09-27 11:27:42 +08002503 else
Guy Cohen39e88502008-04-23 17:14:57 -07002504 lq_sta->dbg_fixed_rate = 0;
Zhu Yi98d7e092007-09-27 11:27:42 +08002505
Guy Cohen39e88502008-04-23 17:14:57 -07002506 lq_sta->active_legacy_rate = 0x0FFF; /* 1 - 54 MBits, includes CCK */
2507 lq_sta->active_siso_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2508 lq_sta->active_mimo2_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2509 lq_sta->active_mimo3_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
Zhu Yi98d7e092007-09-27 11:27:42 +08002510
2511 IWL_DEBUG_RATE("sta_id %d rate 0x%X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07002512 lq_sta->lq.sta_id, lq_sta->dbg_fixed_rate);
Zhu Yi98d7e092007-09-27 11:27:42 +08002513
Guy Cohen39e88502008-04-23 17:14:57 -07002514 if (lq_sta->dbg_fixed_rate) {
2515 rs_fill_link_cmd(NULL, lq_sta, lq_sta->dbg_fixed_rate,
2516 &lq_sta->lq);
Tomas Winkler66c73db2008-04-15 16:01:40 -07002517 iwl_send_lq_cmd(lq_sta->drv, &lq_sta->lq, CMD_ASYNC);
Zhu Yi98d7e092007-09-27 11:27:42 +08002518 }
2519
2520 return count;
2521}
Zhu Yi0209dc12007-09-27 11:27:43 +08002522
Zhu Yi5ae212c2007-09-27 11:27:38 +08002523static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
2524 char __user *user_buf, size_t count, loff_t *ppos)
2525{
2526 char buff[1024];
2527 int desc = 0;
2528 int i = 0;
2529
Tomas Winklerc33104f2008-01-14 17:46:21 -08002530 struct iwl4965_lq_sta *lq_sta = file->private_data;
Zhu Yi5ae212c2007-09-27 11:27:38 +08002531
Tomas Winklerc33104f2008-01-14 17:46:21 -08002532 desc += sprintf(buff+desc, "sta_id %d\n", lq_sta->lq.sta_id);
Zhu Yi98d7e092007-09-27 11:27:42 +08002533 desc += sprintf(buff+desc, "failed=%d success=%d rate=0%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002534 lq_sta->total_failed, lq_sta->total_success,
Guy Cohen39e88502008-04-23 17:14:57 -07002535 lq_sta->active_legacy_rate);
Zhu Yi98d7e092007-09-27 11:27:42 +08002536 desc += sprintf(buff+desc, "fixed rate 0x%X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07002537 lq_sta->dbg_fixed_rate);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002538 desc += sprintf(buff+desc, "general:"
2539 "flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002540 lq_sta->lq.general_params.flags,
2541 lq_sta->lq.general_params.mimo_delimiter,
2542 lq_sta->lq.general_params.single_stream_ant_msk,
2543 lq_sta->lq.general_params.dual_stream_ant_msk);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002544
2545 desc += sprintf(buff+desc, "agg:"
2546 "time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002547 le16_to_cpu(lq_sta->lq.agg_params.agg_time_limit),
2548 lq_sta->lq.agg_params.agg_dis_start_th,
2549 lq_sta->lq.agg_params.agg_frame_cnt_limit);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002550
2551 desc += sprintf(buff+desc,
2552 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002553 lq_sta->lq.general_params.start_rate_index[0],
2554 lq_sta->lq.general_params.start_rate_index[1],
2555 lq_sta->lq.general_params.start_rate_index[2],
2556 lq_sta->lq.general_params.start_rate_index[3]);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002557
2558
2559 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
2560 desc += sprintf(buff+desc, " rate[%d] 0x%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002561 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags));
Zhu Yi5ae212c2007-09-27 11:27:38 +08002562
2563 return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2564}
2565
2566static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
Zhu Yi98d7e092007-09-27 11:27:42 +08002567 .write = rs_sta_dbgfs_scale_table_write,
Zhu Yi5ae212c2007-09-27 11:27:38 +08002568 .read = rs_sta_dbgfs_scale_table_read,
2569 .open = open_file_generic,
2570};
Zhu Yi0209dc12007-09-27 11:27:43 +08002571static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
2572 char __user *user_buf, size_t count, loff_t *ppos)
2573{
2574 char buff[1024];
2575 int desc = 0;
2576 int i, j;
2577
Tomas Winklerc33104f2008-01-14 17:46:21 -08002578 struct iwl4965_lq_sta *lq_sta = file->private_data;
Zhu Yi0209dc12007-09-27 11:27:43 +08002579 for (i = 0; i < LQ_SIZE; i++) {
2580 desc += sprintf(buff+desc, "%s type=%d SGI=%d FAT=%d DUP=%d\n"
2581 "rate=0x%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002582 lq_sta->active_tbl == i?"*":"x",
2583 lq_sta->lq_info[i].lq_type,
2584 lq_sta->lq_info[i].is_SGI,
2585 lq_sta->lq_info[i].is_fat,
2586 lq_sta->lq_info[i].is_dup,
Guy Cohen39e88502008-04-23 17:14:57 -07002587 lq_sta->lq_info[i].current_rate);
Zhu Yi0209dc12007-09-27 11:27:43 +08002588 for (j = 0; j < IWL_RATE_COUNT; j++) {
2589 desc += sprintf(buff+desc,
Tomas Winklerc33104f2008-01-14 17:46:21 -08002590 "counter=%d success=%d %%=%d\n",
2591 lq_sta->lq_info[i].win[j].counter,
2592 lq_sta->lq_info[i].win[j].success_counter,
2593 lq_sta->lq_info[i].win[j].success_ratio);
Zhu Yi0209dc12007-09-27 11:27:43 +08002594 }
2595 }
2596 return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2597}
2598
2599static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
2600 .read = rs_sta_dbgfs_stats_table_read,
2601 .open = open_file_generic,
2602};
Zhu Yi5ae212c2007-09-27 11:27:38 +08002603
Zhu Yi93dc6462007-09-27 11:27:37 +08002604static void rs_add_debugfs(void *priv, void *priv_sta,
2605 struct dentry *dir)
2606{
Tomas Winklerc33104f2008-01-14 17:46:21 -08002607 struct iwl4965_lq_sta *lq_sta = priv_sta;
2608 lq_sta->rs_sta_dbgfs_scale_table_file =
Zhu Yi0209dc12007-09-27 11:27:43 +08002609 debugfs_create_file("rate_scale_table", 0600, dir,
Tomas Winklerc33104f2008-01-14 17:46:21 -08002610 lq_sta, &rs_sta_dbgfs_scale_table_ops);
2611 lq_sta->rs_sta_dbgfs_stats_table_file =
Zhu Yi0209dc12007-09-27 11:27:43 +08002612 debugfs_create_file("rate_stats_table", 0600, dir,
Tomas Winklerc33104f2008-01-14 17:46:21 -08002613 lq_sta, &rs_sta_dbgfs_stats_table_ops);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002614#ifdef CONFIG_IWL4965_HT
2615 lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file =
2616 debugfs_create_u8("tx_agg_tid_enable", 0600, dir,
2617 &lq_sta->tx_agg_tid_en);
2618#endif
2619
Zhu Yi93dc6462007-09-27 11:27:37 +08002620}
2621
2622static void rs_remove_debugfs(void *priv, void *priv_sta)
2623{
Tomas Winklerc33104f2008-01-14 17:46:21 -08002624 struct iwl4965_lq_sta *lq_sta = priv_sta;
2625 debugfs_remove(lq_sta->rs_sta_dbgfs_scale_table_file);
2626 debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002627#ifdef CONFIG_IWL4965_HT
2628 debugfs_remove(lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file);
2629#endif
Zhu Yi93dc6462007-09-27 11:27:37 +08002630}
2631#endif
2632
Zhu Yib481de92007-09-25 17:54:57 -07002633static struct rate_control_ops rs_ops = {
2634 .module = NULL,
2635 .name = RS_NAME,
2636 .tx_status = rs_tx_status,
2637 .get_rate = rs_get_rate,
2638 .rate_init = rs_rate_init,
2639 .clear = rs_clear,
2640 .alloc = rs_alloc,
2641 .free = rs_free,
2642 .alloc_sta = rs_alloc_sta,
2643 .free_sta = rs_free_sta,
Zhu Yi93dc6462007-09-27 11:27:37 +08002644#ifdef CONFIG_MAC80211_DEBUGFS
2645 .add_sta_debugfs = rs_add_debugfs,
2646 .remove_sta_debugfs = rs_remove_debugfs,
2647#endif
Zhu Yib481de92007-09-25 17:54:57 -07002648};
2649
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002650int iwl4965_fill_rs_info(struct ieee80211_hw *hw, char *buf, u8 sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07002651{
2652 struct ieee80211_local *local = hw_to_local(hw);
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002653 struct iwl_priv *priv = hw->priv;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002654 struct iwl4965_lq_sta *lq_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002655 struct sta_info *sta;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002656 int cnt = 0, i;
Zhu Yib481de92007-09-25 17:54:57 -07002657 u32 samples = 0, success = 0, good = 0;
2658 unsigned long now = jiffies;
2659 u32 max_time = 0;
2660 u8 lq_type, antenna;
2661
Johannes Bergd0709a62008-02-25 16:27:46 +01002662 rcu_read_lock();
2663
Zhu Yib481de92007-09-25 17:54:57 -07002664 sta = sta_info_get(local, priv->stations[sta_id].sta.sta.addr);
2665 if (!sta || !sta->rate_ctrl_priv) {
Johannes Bergd0709a62008-02-25 16:27:46 +01002666 if (sta)
Zhu Yib481de92007-09-25 17:54:57 -07002667 IWL_DEBUG_RATE("leave - no private rate data!\n");
Johannes Bergd0709a62008-02-25 16:27:46 +01002668 else
Zhu Yib481de92007-09-25 17:54:57 -07002669 IWL_DEBUG_RATE("leave - no station!\n");
Johannes Bergd0709a62008-02-25 16:27:46 +01002670 rcu_read_unlock();
Zhu Yib481de92007-09-25 17:54:57 -07002671 return sprintf(buf, "station %d not found\n", sta_id);
2672 }
2673
Tomas Winklerc33104f2008-01-14 17:46:21 -08002674 lq_sta = (void *)sta->rate_ctrl_priv;
Zhu Yib481de92007-09-25 17:54:57 -07002675
Tomas Winklerc33104f2008-01-14 17:46:21 -08002676 lq_type = lq_sta->lq_info[lq_sta->active_tbl].lq_type;
Guy Cohenfde0db32008-04-21 15:42:01 -07002677 antenna = lq_sta->lq_info[lq_sta->active_tbl].ant_type;
Zhu Yib481de92007-09-25 17:54:57 -07002678
2679 if (is_legacy(lq_type))
2680 i = IWL_RATE_54M_INDEX;
2681 else
2682 i = IWL_RATE_60M_INDEX;
2683 while (1) {
2684 u64 mask;
2685 int j;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002686 int active = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002687
Tomas Winklerc33104f2008-01-14 17:46:21 -08002688 cnt +=
2689 sprintf(&buf[cnt], " %2dMbs: ", iwl4965_rates[i].ieee / 2);
Zhu Yib481de92007-09-25 17:54:57 -07002690
2691 mask = (1ULL << (IWL_RATE_MAX_WINDOW - 1));
2692 for (j = 0; j < IWL_RATE_MAX_WINDOW; j++, mask >>= 1)
Tomas Winklerc33104f2008-01-14 17:46:21 -08002693 buf[cnt++] =
2694 (lq_sta->lq_info[active].win[i].data & mask)
Zhu Yib481de92007-09-25 17:54:57 -07002695 ? '1' : '0';
2696
Tomas Winklerc33104f2008-01-14 17:46:21 -08002697 samples += lq_sta->lq_info[active].win[i].counter;
2698 good += lq_sta->lq_info[active].win[i].success_counter;
2699 success += lq_sta->lq_info[active].win[i].success_counter *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002700 iwl4965_rates[i].ieee;
Zhu Yib481de92007-09-25 17:54:57 -07002701
Tomas Winklerc33104f2008-01-14 17:46:21 -08002702 if (lq_sta->lq_info[active].win[i].stamp) {
Zhu Yib481de92007-09-25 17:54:57 -07002703 int delta =
2704 jiffies_to_msecs(now -
Tomas Winklerc33104f2008-01-14 17:46:21 -08002705 lq_sta->lq_info[active].win[i].stamp);
Zhu Yib481de92007-09-25 17:54:57 -07002706
2707 if (delta > max_time)
2708 max_time = delta;
2709
Tomas Winklerc33104f2008-01-14 17:46:21 -08002710 cnt += sprintf(&buf[cnt], "%5dms\n", delta);
Zhu Yib481de92007-09-25 17:54:57 -07002711 } else
Tomas Winklerc33104f2008-01-14 17:46:21 -08002712 buf[cnt++] = '\n';
Zhu Yib481de92007-09-25 17:54:57 -07002713
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002714 j = iwl4965_get_prev_ieee_rate(i);
Zhu Yib481de92007-09-25 17:54:57 -07002715 if (j == i)
2716 break;
2717 i = j;
2718 }
2719
2720 /* Display the average rate of all samples taken.
2721 *
Ben Cahill77626352007-11-29 11:09:44 +08002722 * NOTE: We multiply # of samples by 2 since the IEEE measurement
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002723 * added from iwl4965_rates is actually 2X the rate */
Zhu Yib481de92007-09-25 17:54:57 -07002724 if (samples)
Tomas Winklerc33104f2008-01-14 17:46:21 -08002725 cnt += sprintf(&buf[cnt],
Zhu Yib481de92007-09-25 17:54:57 -07002726 "\nAverage rate is %3d.%02dMbs over last %4dms\n"
2727 "%3d%% success (%d good packets over %d tries)\n",
2728 success / (2 * samples), (success * 5 / samples) % 10,
2729 max_time, good * 100 / samples, good, samples);
2730 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08002731 cnt += sprintf(&buf[cnt], "\nAverage rate: 0Mbs\n");
2732
2733 cnt += sprintf(&buf[cnt], "\nrate scale type %d antenna %d "
Zhu Yib481de92007-09-25 17:54:57 -07002734 "active_search %d rate index %d\n", lq_type, antenna,
Johannes Berg8318d782008-01-24 19:38:38 +01002735 lq_sta->search_better_tbl, sta->last_txrate_idx);
Zhu Yib481de92007-09-25 17:54:57 -07002736
Johannes Bergd0709a62008-02-25 16:27:46 +01002737 rcu_read_unlock();
Tomas Winklerc33104f2008-01-14 17:46:21 -08002738 return cnt;
Zhu Yib481de92007-09-25 17:54:57 -07002739}
2740
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002741void iwl4965_rate_scale_init(struct ieee80211_hw *hw, s32 sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07002742{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002743 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07002744
2745 priv->lq_mngr.lq_ready = 1;
2746}
2747
Reinette Chatre897e1cf2008-03-28 16:21:09 -07002748int iwl4965_rate_control_register(void)
Zhu Yib481de92007-09-25 17:54:57 -07002749{
Reinette Chatre897e1cf2008-03-28 16:21:09 -07002750 return ieee80211_rate_control_register(&rs_ops);
Zhu Yib481de92007-09-25 17:54:57 -07002751}
2752
Reinette Chatre897e1cf2008-03-28 16:21:09 -07002753void iwl4965_rate_control_unregister(void)
Zhu Yib481de92007-09-25 17:54:57 -07002754{
2755 ieee80211_rate_control_unregister(&rs_ops);
2756}
2757