blob: 8e3660ebba7d8d4733fe0ecd7a536f5d3a10a9c2 [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
Tomas Winkler3e0d4cb2008-04-24 11:55:38 -070040#include "iwl-dev.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;
Zhu Yi5ae212c2007-09-27 11:27:38 +0800171#endif
Zhu Yi62430652008-05-05 10:22:46 +0800172 struct iwl_priv *drv;
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 Cohen07bc28e2008-04-23 17:15:03 -0700181 u32 rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -0700182
183
Zhu Yi98d7e092007-09-27 11:27:42 +0800184#ifdef CONFIG_MAC80211_DEBUGFS
Tomas Winklerc33104f2008-01-14 17:46:21 -0800185static void rs_dbgfs_set_mcs(struct iwl4965_lq_sta *lq_sta,
Guy Cohen39e88502008-04-23 17:14:57 -0700186 u32 *rate_n_flags, int index);
Zhu Yi98d7e092007-09-27 11:27:42 +0800187#else
Tomas Winklerc33104f2008-01-14 17:46:21 -0800188static void rs_dbgfs_set_mcs(struct iwl4965_lq_sta *lq_sta,
Guy Cohen39e88502008-04-23 17:14:57 -0700189 u32 *rate_n_flags, int index)
Zhu Yi98d7e092007-09-27 11:27:42 +0800190{}
191#endif
Ben Cahill77626352007-11-29 11:09:44 +0800192
193/*
194 * Expected throughput metrics for following rates:
195 * 1, 2, 5.5, 11, 6, 9, 12, 18, 24, 36, 48, 54, 60 MBits
196 * "G" is the only table that supports CCK (the first 4 rates).
197 */
Guy Cohenfde0db32008-04-21 15:42:01 -0700198/*FIXME:RS:need to spearate tables for MIMO2/MIMO3*/
Zhu Yib481de92007-09-25 17:54:57 -0700199static s32 expected_tpt_A[IWL_RATE_COUNT] = {
200 0, 0, 0, 0, 40, 57, 72, 98, 121, 154, 177, 186, 186
201};
202
203static s32 expected_tpt_G[IWL_RATE_COUNT] = {
204 7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 186
205};
206
207static s32 expected_tpt_siso20MHz[IWL_RATE_COUNT] = {
208 0, 0, 0, 0, 42, 42, 76, 102, 124, 159, 183, 193, 202
209};
210
211static s32 expected_tpt_siso20MHzSGI[IWL_RATE_COUNT] = {
212 0, 0, 0, 0, 46, 46, 82, 110, 132, 168, 192, 202, 211
213};
214
215static s32 expected_tpt_mimo20MHz[IWL_RATE_COUNT] = {
216 0, 0, 0, 0, 74, 74, 123, 155, 179, 214, 236, 244, 251
217};
218
219static s32 expected_tpt_mimo20MHzSGI[IWL_RATE_COUNT] = {
220 0, 0, 0, 0, 81, 81, 131, 164, 188, 222, 243, 251, 257
221};
222
223static s32 expected_tpt_siso40MHz[IWL_RATE_COUNT] = {
224 0, 0, 0, 0, 77, 77, 127, 160, 184, 220, 242, 250, 257
225};
226
227static s32 expected_tpt_siso40MHzSGI[IWL_RATE_COUNT] = {
228 0, 0, 0, 0, 83, 83, 135, 169, 193, 229, 250, 257, 264
229};
230
231static s32 expected_tpt_mimo40MHz[IWL_RATE_COUNT] = {
232 0, 0, 0, 0, 123, 123, 182, 214, 235, 264, 279, 285, 289
233};
234
235static s32 expected_tpt_mimo40MHzSGI[IWL_RATE_COUNT] = {
236 0, 0, 0, 0, 131, 131, 191, 222, 242, 270, 284, 289, 293
237};
238
Guy Cohen39e88502008-04-23 17:14:57 -0700239static inline u8 rs_extract_rate(u32 rate_n_flags)
Zhu Yib481de92007-09-25 17:54:57 -0700240{
241 return (u8)(rate_n_flags & 0xFF);
242}
243
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800244static void rs_rate_scale_clear_window(struct iwl4965_rate_scale_data *window)
Zhu Yib481de92007-09-25 17:54:57 -0700245{
246 window->data = 0;
247 window->success_counter = 0;
248 window->success_ratio = IWL_INVALID_VALUE;
249 window->counter = 0;
250 window->average_tpt = IWL_INVALID_VALUE;
251 window->stamp = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700252}
253
Guy Cohenaade00c2008-04-23 17:14:59 -0700254static inline u8 rs_is_valid_ant(u8 valid_antenna, u8 ant_type)
255{
256 return ((ant_type & valid_antenna) == ant_type);
257}
258
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200259#ifdef CONFIG_IWL4965_HT
260/*
261 * removes the old data from the statistics. All data that is older than
262 * TID_MAX_TIME_DIFF, will be deleted.
263 */
264static void rs_tl_rm_old_stats(struct iwl4965_traffic_load *tl, u32 curr_time)
265{
266 /* The oldest age we want to keep */
267 u32 oldest_time = curr_time - TID_MAX_TIME_DIFF;
268
269 while (tl->queue_count &&
270 (tl->time_stamp < oldest_time)) {
271 tl->total -= tl->packet_count[tl->head];
272 tl->packet_count[tl->head] = 0;
273 tl->time_stamp += TID_QUEUE_CELL_SPACING;
274 tl->queue_count--;
275 tl->head++;
276 if (tl->head >= TID_QUEUE_MAX_SIZE)
277 tl->head = 0;
278 }
279}
280
281/*
282 * increment traffic load value for tid and also remove
283 * any old values if passed the certain time period
284 */
285static void rs_tl_add_packet(struct iwl4965_lq_sta *lq_data, u8 tid)
286{
287 u32 curr_time = jiffies_to_msecs(jiffies);
288 u32 time_diff;
289 s32 index;
290 struct iwl4965_traffic_load *tl = NULL;
291
292 if (tid >= TID_MAX_LOAD_COUNT)
293 return;
294
295 tl = &lq_data->load[tid];
296
297 curr_time -= curr_time % TID_ROUND_VALUE;
298
299 /* Happens only for the first packet. Initialize the data */
300 if (!(tl->queue_count)) {
301 tl->total = 1;
302 tl->time_stamp = curr_time;
303 tl->queue_count = 1;
304 tl->head = 0;
305 tl->packet_count[0] = 1;
306 return;
307 }
308
309 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
310 index = time_diff / TID_QUEUE_CELL_SPACING;
311
312 /* The history is too long: remove data that is older than */
313 /* TID_MAX_TIME_DIFF */
314 if (index >= TID_QUEUE_MAX_SIZE)
315 rs_tl_rm_old_stats(tl, curr_time);
316
317 index = (tl->head + index) % TID_QUEUE_MAX_SIZE;
318 tl->packet_count[index] = tl->packet_count[index] + 1;
319 tl->total = tl->total + 1;
320
321 if ((index + 1) > tl->queue_count)
322 tl->queue_count = index + 1;
323}
324
325/*
326 get the traffic load value for tid
327*/
328static u32 rs_tl_get_load(struct iwl4965_lq_sta *lq_data, u8 tid)
329{
330 u32 curr_time = jiffies_to_msecs(jiffies);
331 u32 time_diff;
332 s32 index;
333 struct iwl4965_traffic_load *tl = NULL;
334
335 if (tid >= TID_MAX_LOAD_COUNT)
336 return 0;
337
338 tl = &(lq_data->load[tid]);
339
340 curr_time -= curr_time % TID_ROUND_VALUE;
341
342 if (!(tl->queue_count))
343 return 0;
344
345 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
346 index = time_diff / TID_QUEUE_CELL_SPACING;
347
348 /* The history is too long: remove data that is older than */
349 /* TID_MAX_TIME_DIFF */
350 if (index >= TID_QUEUE_MAX_SIZE)
351 rs_tl_rm_old_stats(tl, curr_time);
352
353 return tl->total;
354}
355
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700356static void rs_tl_turn_on_agg_for_tid(struct iwl_priv *priv,
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200357 struct iwl4965_lq_sta *lq_data, u8 tid,
358 struct sta_info *sta)
359{
360 unsigned long state;
361 DECLARE_MAC_BUF(mac);
362
Johannes Berg07346f812008-05-03 01:02:02 +0200363 spin_lock_bh(&sta->lock);
Ron Rindjunskycee24a32008-03-26 20:36:03 +0200364 state = sta->ampdu_mlme.tid_state_tx[tid];
Johannes Berg07346f812008-05-03 01:02:02 +0200365 spin_unlock_bh(&sta->lock);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200366
367 if (state == HT_AGG_STATE_IDLE &&
368 rs_tl_get_load(lq_data, tid) > IWL_AGG_LOAD_THRESHOLD) {
369 IWL_DEBUG_HT("Starting Tx agg: STA: %s tid: %d\n",
370 print_mac(mac, sta->addr), tid);
371 ieee80211_start_tx_ba_session(priv->hw, sta->addr, tid);
372 }
373}
374
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700375static void rs_tl_turn_on_agg(struct iwl_priv *priv, u8 tid,
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200376 struct iwl4965_lq_sta *lq_data,
377 struct sta_info *sta)
378{
379 if ((tid < TID_MAX_LOAD_COUNT))
380 rs_tl_turn_on_agg_for_tid(priv, lq_data, tid, sta);
381 else if (tid == IWL_AGG_ALL_TID)
382 for (tid = 0; tid < TID_MAX_LOAD_COUNT; tid++)
383 rs_tl_turn_on_agg_for_tid(priv, lq_data, tid, sta);
384}
385
386#endif /* CONFIG_IWLWIFI_HT */
387
Guy Cohen39e88502008-04-23 17:14:57 -0700388static inline int get_num_of_ant_from_rate(u32 rate_n_flags)
Guy Cohenfde0db32008-04-21 15:42:01 -0700389{
Guy Cohen39e88502008-04-23 17:14:57 -0700390 return (!!(rate_n_flags & RATE_MCS_ANT_A_MSK) +
391 !!(rate_n_flags & RATE_MCS_ANT_B_MSK) +
392 !!(rate_n_flags & RATE_MCS_ANT_C_MSK));
Guy Cohenfde0db32008-04-21 15:42:01 -0700393}
394
Ben Cahill77626352007-11-29 11:09:44 +0800395/**
396 * rs_collect_tx_data - Update the success/failure sliding window
397 *
398 * We keep a sliding window of the last 62 packets transmitted
399 * at this rate. window->data contains the bitmask of successful
400 * packets.
401 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800402static int rs_collect_tx_data(struct iwl4965_rate_scale_data *windows,
Ron Rindjunsky99556432008-01-28 14:07:25 +0200403 int scale_index, s32 tpt, int retries,
404 int successes)
Zhu Yib481de92007-09-25 17:54:57 -0700405{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800406 struct iwl4965_rate_scale_data *window = NULL;
Guy Cohen39e88502008-04-23 17:14:57 -0700407 static const u64 mask = (((u64)1) << (IWL_RATE_MAX_WINDOW - 1));
Zhu Yib481de92007-09-25 17:54:57 -0700408 s32 fail_count;
409
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800410 if (scale_index < 0 || scale_index >= IWL_RATE_COUNT)
411 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -0700412
Ben Cahill77626352007-11-29 11:09:44 +0800413 /* Select data for current tx bit rate */
Zhu Yib481de92007-09-25 17:54:57 -0700414 window = &(windows[scale_index]);
415
Ben Cahill77626352007-11-29 11:09:44 +0800416 /*
417 * Keep track of only the latest 62 tx frame attempts in this rate's
418 * history window; anything older isn't really relevant any more.
419 * If we have filled up the sliding window, drop the oldest attempt;
420 * if the oldest attempt (highest bit in bitmap) shows "success",
421 * subtract "1" from the success counter (this is the main reason
422 * we keep these bitmaps!).
423 */
Ron Rindjunsky99556432008-01-28 14:07:25 +0200424 while (retries > 0) {
Guy Cohen39e88502008-04-23 17:14:57 -0700425 if (window->counter >= IWL_RATE_MAX_WINDOW) {
426
427 /* remove earliest */
428 window->counter = IWL_RATE_MAX_WINDOW - 1;
429
Ron Rindjunsky99556432008-01-28 14:07:25 +0200430 if (window->data & mask) {
431 window->data &= ~mask;
Guy Cohen39e88502008-04-23 17:14:57 -0700432 window->success_counter--;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200433 }
Zhu Yib481de92007-09-25 17:54:57 -0700434 }
Zhu Yib481de92007-09-25 17:54:57 -0700435
Ron Rindjunsky99556432008-01-28 14:07:25 +0200436 /* Increment frames-attempted counter */
437 window->counter++;
Ben Cahill77626352007-11-29 11:09:44 +0800438
Ron Rindjunsky99556432008-01-28 14:07:25 +0200439 /* Shift bitmap by one frame (throw away oldest history),
440 * OR in "1", and increment "success" if this
441 * frame was successful. */
Guy Cohen39e88502008-04-23 17:14:57 -0700442 window->data <<= 1;;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200443 if (successes > 0) {
Guy Cohen39e88502008-04-23 17:14:57 -0700444 window->success_counter++;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200445 window->data |= 0x1;
446 successes--;
447 }
448
449 retries--;
Zhu Yib481de92007-09-25 17:54:57 -0700450 }
451
Ben Cahill77626352007-11-29 11:09:44 +0800452 /* Calculate current success ratio, avoid divide-by-0! */
Zhu Yib481de92007-09-25 17:54:57 -0700453 if (window->counter > 0)
454 window->success_ratio = 128 * (100 * window->success_counter)
455 / window->counter;
456 else
457 window->success_ratio = IWL_INVALID_VALUE;
458
459 fail_count = window->counter - window->success_counter;
460
Ben Cahill77626352007-11-29 11:09:44 +0800461 /* Calculate average throughput, if we have enough history. */
Zhu Yib481de92007-09-25 17:54:57 -0700462 if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
463 (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
464 window->average_tpt = (window->success_ratio * tpt + 64) / 128;
465 else
466 window->average_tpt = IWL_INVALID_VALUE;
467
Ben Cahill77626352007-11-29 11:09:44 +0800468 /* Tag this window as having been updated */
Zhu Yib481de92007-09-25 17:54:57 -0700469 window->stamp = jiffies;
470
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800471 return 0;
Zhu Yib481de92007-09-25 17:54:57 -0700472}
473
Ben Cahill77626352007-11-29 11:09:44 +0800474/*
475 * Fill uCode API rate_n_flags field, based on "search" or "active" table.
476 */
Guy Cohen39e88502008-04-23 17:14:57 -0700477/* FIXME:RS:remove this function and put the flags statically in the table */
478static u32 rate_n_flags_from_tbl(struct iwl4965_scale_tbl_info *tbl,
479 int index, u8 use_green)
Zhu Yib481de92007-09-25 17:54:57 -0700480{
Guy Cohen39e88502008-04-23 17:14:57 -0700481 u32 rate_n_flags = 0;
482
Zhu Yib481de92007-09-25 17:54:57 -0700483 if (is_legacy(tbl->lq_type)) {
Guy Cohen39e88502008-04-23 17:14:57 -0700484 rate_n_flags = iwl4965_rates[index].plcp;
Zhu Yib481de92007-09-25 17:54:57 -0700485 if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE)
Guy Cohen39e88502008-04-23 17:14:57 -0700486 rate_n_flags |= RATE_MCS_CCK_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700487
Guy Cohenfde0db32008-04-21 15:42:01 -0700488 } else if (is_Ht(tbl->lq_type)) {
489 if (index > IWL_LAST_OFDM_RATE) {
490 IWL_ERROR("invalid HT rate index %d\n", index);
Zhu Yib481de92007-09-25 17:54:57 -0700491 index = IWL_LAST_OFDM_RATE;
Guy Cohenfde0db32008-04-21 15:42:01 -0700492 }
Guy Cohen39e88502008-04-23 17:14:57 -0700493 rate_n_flags = RATE_MCS_HT_MSK;
Guy Cohenfde0db32008-04-21 15:42:01 -0700494
495 if (is_siso(tbl->lq_type))
Guy Cohen39e88502008-04-23 17:14:57 -0700496 rate_n_flags |= iwl4965_rates[index].plcp_siso;
Guy Cohenfde0db32008-04-21 15:42:01 -0700497 else if (is_mimo2(tbl->lq_type))
Guy Cohen39e88502008-04-23 17:14:57 -0700498 rate_n_flags |= iwl4965_rates[index].plcp_mimo2;
Guy Cohenfde0db32008-04-21 15:42:01 -0700499 else
Guy Cohen39e88502008-04-23 17:14:57 -0700500 rate_n_flags |= iwl4965_rates[index].plcp_mimo3;
Zhu Yib481de92007-09-25 17:54:57 -0700501 } else {
Guy Cohenfde0db32008-04-21 15:42:01 -0700502 IWL_ERROR("Invalid tbl->lq_type %d\n", tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -0700503 }
504
Guy Cohen39e88502008-04-23 17:14:57 -0700505 rate_n_flags |= ((tbl->ant_type << RATE_MCS_ANT_POS) &
Guy Cohenfde0db32008-04-21 15:42:01 -0700506 RATE_MCS_ANT_ABC_MSK);
Zhu Yib481de92007-09-25 17:54:57 -0700507
Guy Cohen39e88502008-04-23 17:14:57 -0700508 if (is_Ht(tbl->lq_type)) {
509 if (tbl->is_fat) {
510 if (tbl->is_dup)
511 rate_n_flags |= RATE_MCS_DUP_MSK;
512 else
513 rate_n_flags |= RATE_MCS_FAT_MSK;
514 }
515 if (tbl->is_SGI)
516 rate_n_flags |= RATE_MCS_SGI_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700517
Guy Cohen39e88502008-04-23 17:14:57 -0700518 if (use_green) {
519 rate_n_flags |= RATE_MCS_GF_MSK;
520 if (is_siso(tbl->lq_type) && tbl->is_SGI) {
521 rate_n_flags &= ~RATE_MCS_SGI_MSK;
522 IWL_ERROR("GF was set with SGI:SISO\n");
523 }
524 }
Zhu Yib481de92007-09-25 17:54:57 -0700525 }
Guy Cohen39e88502008-04-23 17:14:57 -0700526 return rate_n_flags;
Zhu Yib481de92007-09-25 17:54:57 -0700527}
528
Ben Cahill77626352007-11-29 11:09:44 +0800529/*
530 * Interpret uCode API's rate_n_flags format,
531 * fill "search" or "active" tx mode table.
532 */
Guy Cohen39e88502008-04-23 17:14:57 -0700533static int rs_get_tbl_info_from_mcs(const u32 rate_n_flags,
Johannes Berg8318d782008-01-24 19:38:38 +0100534 enum ieee80211_band band,
535 struct iwl4965_scale_tbl_info *tbl,
Zhu Yib481de92007-09-25 17:54:57 -0700536 int *rate_idx)
537{
Guy Cohen39e88502008-04-23 17:14:57 -0700538 u32 ant_msk = (rate_n_flags & RATE_MCS_ANT_ABC_MSK);
539 u8 num_of_ant = get_num_of_ant_from_rate(rate_n_flags);
540 u8 mcs;
Zhu Yib481de92007-09-25 17:54:57 -0700541
Guy Cohen39e88502008-04-23 17:14:57 -0700542 *rate_idx = iwl4965_hwrate_to_plcp_idx(rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -0700543
Guy Cohenfde0db32008-04-21 15:42:01 -0700544 if (*rate_idx == IWL_RATE_INVALID) {
Zhu Yib481de92007-09-25 17:54:57 -0700545 *rate_idx = -1;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800546 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -0700547 }
Ben Cahill77626352007-11-29 11:09:44 +0800548 tbl->is_SGI = 0; /* default legacy setup */
Zhu Yib481de92007-09-25 17:54:57 -0700549 tbl->is_fat = 0;
550 tbl->is_dup = 0;
Guy Cohenfde0db32008-04-21 15:42:01 -0700551 tbl->ant_type = (ant_msk >> RATE_MCS_ANT_POS);
552 tbl->lq_type = LQ_NONE;
Zhu Yib481de92007-09-25 17:54:57 -0700553
Ben Cahill77626352007-11-29 11:09:44 +0800554 /* legacy rate format */
Guy Cohen39e88502008-04-23 17:14:57 -0700555 if (!(rate_n_flags & RATE_MCS_HT_MSK)) {
Guy Cohenfde0db32008-04-21 15:42:01 -0700556 if (num_of_ant == 1) {
Johannes Berg8318d782008-01-24 19:38:38 +0100557 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700558 tbl->lq_type = LQ_A;
559 else
560 tbl->lq_type = LQ_G;
Zhu Yib481de92007-09-25 17:54:57 -0700561 }
Guy Cohenfde0db32008-04-21 15:42:01 -0700562 /* HT rate format */
Zhu Yib481de92007-09-25 17:54:57 -0700563 } else {
Guy Cohen39e88502008-04-23 17:14:57 -0700564 if (rate_n_flags & RATE_MCS_SGI_MSK)
Zhu Yib481de92007-09-25 17:54:57 -0700565 tbl->is_SGI = 1;
566
Guy Cohen39e88502008-04-23 17:14:57 -0700567 if ((rate_n_flags & RATE_MCS_FAT_MSK) ||
568 (rate_n_flags & RATE_MCS_DUP_MSK))
Zhu Yib481de92007-09-25 17:54:57 -0700569 tbl->is_fat = 1;
570
Guy Cohen39e88502008-04-23 17:14:57 -0700571 if (rate_n_flags & RATE_MCS_DUP_MSK)
Zhu Yib481de92007-09-25 17:54:57 -0700572 tbl->is_dup = 1;
Guy Cohenfde0db32008-04-21 15:42:01 -0700573
Guy Cohen39e88502008-04-23 17:14:57 -0700574 mcs = rs_extract_rate(rate_n_flags);
Guy Cohenfde0db32008-04-21 15:42:01 -0700575
Guy Cohen39e88502008-04-23 17:14:57 -0700576 /* SISO */
577 if (mcs <= IWL_RATE_SISO_60M_PLCP) {
Guy Cohenfde0db32008-04-21 15:42:01 -0700578 if (num_of_ant == 1)
579 tbl->lq_type = LQ_SISO; /*else NONE*/
580 /* MIMO2 */
Guy Cohen39e88502008-04-23 17:14:57 -0700581 } else if (mcs <= IWL_RATE_MIMO2_60M_PLCP) {
Guy Cohenfde0db32008-04-21 15:42:01 -0700582 if (num_of_ant == 2)
583 tbl->lq_type = LQ_MIMO2;
584 /* MIMO3 */
585 } else {
586 if (num_of_ant == 3)
587 tbl->lq_type = LQ_MIMO3;
588 }
Zhu Yib481de92007-09-25 17:54:57 -0700589 }
590 return 0;
591}
Guy Cohenaade00c2008-04-23 17:14:59 -0700592
593/* switch to another antenna/antennas and return 1 */
594/* if no other valid antenna found, return 0 */
595static int rs_toggle_antenna(u32 valid_ant, u32 *rate_n_flags,
596 struct iwl4965_scale_tbl_info *tbl)
Zhu Yib481de92007-09-25 17:54:57 -0700597{
Guy Cohenaade00c2008-04-23 17:14:59 -0700598 u8 new_ant_type;
599
600 if (!tbl->ant_type || tbl->ant_type > ANT_ABC)
601 return 0;
602
603 if (!rs_is_valid_ant(valid_ant, tbl->ant_type))
604 return 0;
605
606 new_ant_type = ant_toggle_lookup[tbl->ant_type];
607
608 while ((new_ant_type != tbl->ant_type) &&
609 !rs_is_valid_ant(valid_ant, new_ant_type))
610 new_ant_type = ant_toggle_lookup[new_ant_type];
611
612 if (new_ant_type == tbl->ant_type)
613 return 0;
614
615 tbl->ant_type = new_ant_type;
616 *rate_n_flags &= ~RATE_MCS_ANT_ABC_MSK;
617 *rate_n_flags |= new_ant_type << RATE_MCS_ANT_POS;
618 return 1;
Zhu Yib481de92007-09-25 17:54:57 -0700619}
620
Guy Cohen39e88502008-04-23 17:14:57 -0700621/* FIXME:RS: in 4965 we don't use greenfield at all */
Guy Cohenf935a6d2008-04-23 17:15:02 -0700622/* FIXME:RS: don't use greenfield for now in TX */
623/* #ifdef CONFIG_IWL4965_HT */
624#if 0
625static inline u8 rs_use_green(struct iwl_priv *priv, struct ieee80211_conf *conf)
Zhu Yib481de92007-09-25 17:54:57 -0700626{
Ron Rindjunsky270243a2007-11-26 16:14:41 +0200627 return ((conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) &&
628 priv->current_ht_config.is_green_field &&
629 !priv->current_ht_config.non_GF_STA_present);
Zhu Yib481de92007-09-25 17:54:57 -0700630}
Guy Cohenf935a6d2008-04-23 17:15:02 -0700631#else
632static inline u8 rs_use_green(struct iwl_priv *priv, struct ieee80211_conf *conf)
633{
634 return 0;
635}
636#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -0700637
638/**
639 * rs_get_supported_rates - get the available rates
640 *
641 * if management frame or broadcast frame only return
642 * basic available rates.
643 *
644 */
Guy Coheneecd6e52008-04-23 17:14:58 -0700645static u16 rs_get_supported_rates(struct iwl4965_lq_sta *lq_sta,
Zhu Yib481de92007-09-25 17:54:57 -0700646 struct ieee80211_hdr *hdr,
Guy Coheneecd6e52008-04-23 17:14:58 -0700647 enum iwl_table_type rate_type)
Zhu Yib481de92007-09-25 17:54:57 -0700648{
Zhu Yib481de92007-09-25 17:54:57 -0700649 if (hdr && is_multicast_ether_addr(hdr->addr1) &&
Guy Coheneecd6e52008-04-23 17:14:58 -0700650 lq_sta->active_rate_basic)
651 return lq_sta->active_rate_basic;
652
653 if (is_legacy(rate_type)) {
654 return lq_sta->active_legacy_rate;
655 } else {
656 if (is_siso(rate_type))
657 return lq_sta->active_siso_rate;
658 else if (is_mimo2(rate_type))
659 return lq_sta->active_mimo2_rate;
660 else
661 return lq_sta->active_mimo3_rate;
Tomas Winklerc33104f2008-01-14 17:46:21 -0800662 }
Zhu Yib481de92007-09-25 17:54:57 -0700663}
664
Ester Kummerbf403db2008-05-05 10:22:40 +0800665static u16 rs_get_adjacent_rate(struct iwl_priv *priv, u8 index, u16 rate_mask,
666 int rate_type)
Zhu Yib481de92007-09-25 17:54:57 -0700667{
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
Ester Kummerbf403db2008-05-05 10:22:40 +0800767 high_low = rs_get_adjacent_rate(lq_sta->drv, scale_index, rate_mask,
768 tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -0700769 low = high_low & 0xff;
770
Guy Cohen39e88502008-04-23 17:14:57 -0700771 if (low == IWL_RATE_INVALID)
772 low = scale_index;
773
774out:
775 return rate_n_flags_from_tbl(tbl, low, is_green);
Zhu Yib481de92007-09-25 17:54:57 -0700776}
777
Ben Cahill77626352007-11-29 11:09:44 +0800778/*
779 * mac80211 sends us Tx status
780 */
Tomas Winklerc33104f2008-01-14 17:46:21 -0800781static void rs_tx_status(void *priv_rate, struct net_device *dev,
Zhu Yib481de92007-09-25 17:54:57 -0700782 struct sk_buff *skb,
783 struct ieee80211_tx_status *tx_resp)
784{
785 int status;
786 u8 retries;
787 int rs_index, index = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -0800788 struct iwl4965_lq_sta *lq_sta;
Tomas Winkler66c73db2008-04-15 16:01:40 -0700789 struct iwl_link_quality_cmd *table;
Zhu Yib481de92007-09-25 17:54:57 -0700790 struct sta_info *sta;
791 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700792 struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
Zhu Yib481de92007-09-25 17:54:57 -0700793 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800794 struct ieee80211_hw *hw = local_to_hw(local);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800795 struct iwl4965_rate_scale_data *window = NULL;
796 struct iwl4965_rate_scale_data *search_win = NULL;
Guy Cohen39e88502008-04-23 17:14:57 -0700797 u32 tx_rate;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800798 struct iwl4965_scale_tbl_info tbl_type;
799 struct iwl4965_scale_tbl_info *curr_tbl, *search_tbl;
Zhu Yib481de92007-09-25 17:54:57 -0700800 u8 active_index = 0;
801 u16 fc = le16_to_cpu(hdr->frame_control);
802 s32 tpt = 0;
803
Zhu Yi58826352007-09-27 11:27:39 +0800804 IWL_DEBUG_RATE_LIMIT("get frame ack response, update rate scale window\n");
Zhu Yib481de92007-09-25 17:54:57 -0700805
806 if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1))
807 return;
808
Ron Rindjunsky99556432008-01-28 14:07:25 +0200809 /* This packet was aggregated but doesn't carry rate scale info */
810 if ((tx_resp->control.flags & IEEE80211_TXCTL_AMPDU) &&
811 !(tx_resp->flags & IEEE80211_TX_STATUS_AMPDU))
812 return;
813
Zhu Yib481de92007-09-25 17:54:57 -0700814 retries = tx_resp->retry_count;
815
816 if (retries > 15)
817 retries = 15;
818
Johannes Bergd0709a62008-02-25 16:27:46 +0100819 rcu_read_lock();
Zhu Yib481de92007-09-25 17:54:57 -0700820
821 sta = sta_info_get(local, hdr->addr1);
822
Tomas Winklerf4d60822008-03-05 11:31:00 -0800823 if (!sta || !sta->rate_ctrl_priv)
824 goto out;
825
Zhu Yib481de92007-09-25 17:54:57 -0700826
Tomas Winklerc33104f2008-01-14 17:46:21 -0800827 lq_sta = (struct iwl4965_lq_sta *)sta->rate_ctrl_priv;
Zhu Yib481de92007-09-25 17:54:57 -0700828
829 if (!priv->lq_mngr.lq_ready)
Tomas Winklerf4d60822008-03-05 11:31:00 -0800830 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700831
Tomas Winklerc33104f2008-01-14 17:46:21 -0800832 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
833 !lq_sta->ibss_sta_added)
Tomas Winklerf4d60822008-03-05 11:31:00 -0800834 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700835
Tomas Winklerc33104f2008-01-14 17:46:21 -0800836 table = &lq_sta->lq;
837 active_index = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -0700838
Tomas Winklerc33104f2008-01-14 17:46:21 -0800839 curr_tbl = &(lq_sta->lq_info[active_index]);
840 search_tbl = &(lq_sta->lq_info[(1 - active_index)]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800841 window = (struct iwl4965_rate_scale_data *)
Zhu Yib481de92007-09-25 17:54:57 -0700842 &(curr_tbl->win[0]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800843 search_win = (struct iwl4965_rate_scale_data *)
Zhu Yib481de92007-09-25 17:54:57 -0700844 &(search_tbl->win[0]);
845
Ben Cahill77626352007-11-29 11:09:44 +0800846 /*
847 * Ignore this Tx frame response if its initial rate doesn't match
848 * that of latest Link Quality command. There may be stragglers
849 * from a previous Link Quality command, but we're no longer interested
850 * in those; they're either from the "active" mode while we're trying
851 * to check "search" mode, or a prior "search" mode after we've moved
852 * to a new "search" mode (which might become the new "active" mode).
853 */
Guy Cohen39e88502008-04-23 17:14:57 -0700854 tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
855 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type, &rs_index);
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800856 if (priv->band == IEEE80211_BAND_5GHZ)
857 rs_index -= IWL_FIRST_OFDM_RATE;
858
859 if ((tx_resp->control.tx_rate == NULL) ||
860 (tbl_type.is_SGI ^
861 !!(tx_resp->control.flags & IEEE80211_TXCTL_SHORT_GI)) ||
862 (tbl_type.is_fat ^
863 !!(tx_resp->control.flags & IEEE80211_TXCTL_40_MHZ_WIDTH)) ||
864 (tbl_type.is_dup ^
865 !!(tx_resp->control.flags & IEEE80211_TXCTL_DUP_DATA)) ||
Guy Cohenfde0db32008-04-21 15:42:01 -0700866 (tbl_type.ant_type ^ tx_resp->control.antenna_sel_tx) ||
Guy Cohen39e88502008-04-23 17:14:57 -0700867 (!!(tx_rate & RATE_MCS_HT_MSK) ^
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800868 !!(tx_resp->control.flags & IEEE80211_TXCTL_OFDM_HT)) ||
Guy Cohen39e88502008-04-23 17:14:57 -0700869 (!!(tx_rate & RATE_MCS_GF_MSK) ^
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800870 !!(tx_resp->control.flags & IEEE80211_TXCTL_GREEN_FIELD)) ||
871 (hw->wiphy->bands[priv->band]->bitrates[rs_index].bitrate !=
872 tx_resp->control.tx_rate->bitrate)) {
Guy Cohen39e88502008-04-23 17:14:57 -0700873 IWL_DEBUG_RATE("initial rate does not match 0x%x\n", tx_rate);
Tomas Winklerf4d60822008-03-05 11:31:00 -0800874 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700875 }
876
Ben Cahill77626352007-11-29 11:09:44 +0800877 /* Update frame history window with "failure" for each Tx retry. */
Zhu Yib481de92007-09-25 17:54:57 -0700878 while (retries) {
Ben Cahill77626352007-11-29 11:09:44 +0800879 /* Look up the rate and other info used for each tx attempt.
880 * Each tx attempt steps one entry deeper in the rate table. */
Guy Cohen39e88502008-04-23 17:14:57 -0700881 tx_rate = le32_to_cpu(table->rs_table[index].rate_n_flags);
882 rs_get_tbl_info_from_mcs(tx_rate, priv->band,
Zhu Yib481de92007-09-25 17:54:57 -0700883 &tbl_type, &rs_index);
884
Ben Cahill77626352007-11-29 11:09:44 +0800885 /* If type matches "search" table,
886 * add failure to "search" history */
Zhu Yib481de92007-09-25 17:54:57 -0700887 if ((tbl_type.lq_type == search_tbl->lq_type) &&
Guy Cohenfde0db32008-04-21 15:42:01 -0700888 (tbl_type.ant_type == search_tbl->ant_type) &&
Zhu Yib481de92007-09-25 17:54:57 -0700889 (tbl_type.is_SGI == search_tbl->is_SGI)) {
890 if (search_tbl->expected_tpt)
891 tpt = search_tbl->expected_tpt[rs_index];
892 else
893 tpt = 0;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200894 rs_collect_tx_data(search_win, rs_index, tpt, 1, 0);
Ben Cahill77626352007-11-29 11:09:44 +0800895
896 /* Else if type matches "current/active" table,
897 * add failure to "current/active" history */
Zhu Yib481de92007-09-25 17:54:57 -0700898 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
Guy Cohenfde0db32008-04-21 15:42:01 -0700899 (tbl_type.ant_type == curr_tbl->ant_type) &&
Zhu Yib481de92007-09-25 17:54:57 -0700900 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
901 if (curr_tbl->expected_tpt)
902 tpt = curr_tbl->expected_tpt[rs_index];
903 else
904 tpt = 0;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200905 rs_collect_tx_data(window, rs_index, tpt, 1, 0);
Zhu Yib481de92007-09-25 17:54:57 -0700906 }
Ben Cahill77626352007-11-29 11:09:44 +0800907
908 /* If not searching for a new mode, increment failed counter
909 * ... this helps determine when to start searching again */
Tomas Winklerc33104f2008-01-14 17:46:21 -0800910 if (lq_sta->stay_in_tbl)
911 lq_sta->total_failed++;
Zhu Yib481de92007-09-25 17:54:57 -0700912 --retries;
913 index++;
914
915 }
916
Ben Cahill77626352007-11-29 11:09:44 +0800917 /*
918 * Find (by rate) the history window to update with final Tx attempt;
919 * if Tx was successful first try, use original rate,
920 * else look up the rate that was, finally, successful.
921 */
Guy Cohen39e88502008-04-23 17:14:57 -0700922 tx_rate = le32_to_cpu(table->rs_table[index].rate_n_flags);
923 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type, &rs_index);
Zhu Yib481de92007-09-25 17:54:57 -0700924
Ben Cahill77626352007-11-29 11:09:44 +0800925 /* Update frame history window with "success" if Tx got ACKed ... */
Zhu Yib481de92007-09-25 17:54:57 -0700926 if (tx_resp->flags & IEEE80211_TX_STATUS_ACK)
927 status = 1;
928 else
929 status = 0;
930
Ben Cahill77626352007-11-29 11:09:44 +0800931 /* If type matches "search" table,
932 * add final tx status to "search" history */
Zhu Yib481de92007-09-25 17:54:57 -0700933 if ((tbl_type.lq_type == search_tbl->lq_type) &&
Guy Cohenfde0db32008-04-21 15:42:01 -0700934 (tbl_type.ant_type == search_tbl->ant_type) &&
Zhu Yib481de92007-09-25 17:54:57 -0700935 (tbl_type.is_SGI == search_tbl->is_SGI)) {
936 if (search_tbl->expected_tpt)
937 tpt = search_tbl->expected_tpt[rs_index];
938 else
939 tpt = 0;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200940 if (tx_resp->control.flags & IEEE80211_TXCTL_AMPDU)
941 rs_collect_tx_data(search_win, rs_index, tpt,
942 tx_resp->ampdu_ack_len,
943 tx_resp->ampdu_ack_map);
944 else
945 rs_collect_tx_data(search_win, rs_index, tpt,
946 1, status);
Ben Cahill77626352007-11-29 11:09:44 +0800947 /* Else if type matches "current/active" table,
948 * add final tx status to "current/active" history */
Zhu Yib481de92007-09-25 17:54:57 -0700949 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
Guy Cohenfde0db32008-04-21 15:42:01 -0700950 (tbl_type.ant_type == curr_tbl->ant_type) &&
Zhu Yib481de92007-09-25 17:54:57 -0700951 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
952 if (curr_tbl->expected_tpt)
953 tpt = curr_tbl->expected_tpt[rs_index];
954 else
955 tpt = 0;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200956 if (tx_resp->control.flags & IEEE80211_TXCTL_AMPDU)
957 rs_collect_tx_data(window, rs_index, tpt,
958 tx_resp->ampdu_ack_len,
959 tx_resp->ampdu_ack_map);
960 else
961 rs_collect_tx_data(window, rs_index, tpt,
962 1, status);
Zhu Yib481de92007-09-25 17:54:57 -0700963 }
964
Ben Cahill77626352007-11-29 11:09:44 +0800965 /* If not searching for new mode, increment success/failed counter
966 * ... these help determine when to start searching again */
Tomas Winklerc33104f2008-01-14 17:46:21 -0800967 if (lq_sta->stay_in_tbl) {
Ron Rindjunsky99556432008-01-28 14:07:25 +0200968 if (tx_resp->control.flags & IEEE80211_TXCTL_AMPDU) {
969 lq_sta->total_success += tx_resp->ampdu_ack_map;
970 lq_sta->total_failed +=
971 (tx_resp->ampdu_ack_len - tx_resp->ampdu_ack_map);
972 } else {
973 if (status)
974 lq_sta->total_success++;
975 else
976 lq_sta->total_failed++;
977 }
Zhu Yib481de92007-09-25 17:54:57 -0700978 }
979
Ben Cahill77626352007-11-29 11:09:44 +0800980 /* See if there's a better rate or modulation mode to try. */
Zhu Yib481de92007-09-25 17:54:57 -0700981 rs_rate_scale_perform(priv, dev, hdr, sta);
Tomas Winklerf4d60822008-03-05 11:31:00 -0800982out:
Johannes Bergd0709a62008-02-25 16:27:46 +0100983 rcu_read_unlock();
Zhu Yib481de92007-09-25 17:54:57 -0700984 return;
985}
986
Ben Cahill77626352007-11-29 11:09:44 +0800987/*
988 * Begin a period of staying with a selected modulation mode.
989 * Set "stay_in_tbl" flag to prevent any mode switches.
990 * Set frame tx success limits according to legacy vs. high-throughput,
991 * and reset overall (spanning all rates) tx success history statistics.
992 * These control how long we stay using same modulation mode before
993 * searching for a new mode.
994 */
Ester Kummerbf403db2008-05-05 10:22:40 +0800995static void rs_set_stay_in_table(struct iwl_priv *priv, u8 is_legacy,
Tomas Winklerc33104f2008-01-14 17:46:21 -0800996 struct iwl4965_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -0700997{
Guy Coheneecd6e52008-04-23 17:14:58 -0700998 IWL_DEBUG_RATE("we are staying in the same table\n");
Tomas Winklerc33104f2008-01-14 17:46:21 -0800999 lq_sta->stay_in_tbl = 1; /* only place this gets set */
Zhu Yib481de92007-09-25 17:54:57 -07001000 if (is_legacy) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001001 lq_sta->table_count_limit = IWL_LEGACY_TABLE_COUNT;
1002 lq_sta->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
1003 lq_sta->max_success_limit = IWL_LEGACY_SUCCESS_LIMIT;
Zhu Yib481de92007-09-25 17:54:57 -07001004 } else {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001005 lq_sta->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
1006 lq_sta->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
1007 lq_sta->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
Zhu Yib481de92007-09-25 17:54:57 -07001008 }
Tomas Winklerc33104f2008-01-14 17:46:21 -08001009 lq_sta->table_count = 0;
1010 lq_sta->total_failed = 0;
1011 lq_sta->total_success = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001012}
1013
Ben Cahill77626352007-11-29 11:09:44 +08001014/*
1015 * Find correct throughput table for given mode of modulation
1016 */
Guy Coheneecd6e52008-04-23 17:14:58 -07001017static void rs_set_expected_tpt_table(struct iwl4965_lq_sta *lq_sta,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001018 struct iwl4965_scale_tbl_info *tbl)
Zhu Yib481de92007-09-25 17:54:57 -07001019{
1020 if (is_legacy(tbl->lq_type)) {
1021 if (!is_a_band(tbl->lq_type))
1022 tbl->expected_tpt = expected_tpt_G;
1023 else
1024 tbl->expected_tpt = expected_tpt_A;
1025 } else if (is_siso(tbl->lq_type)) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001026 if (tbl->is_fat && !lq_sta->is_dup)
Zhu Yib481de92007-09-25 17:54:57 -07001027 if (tbl->is_SGI)
1028 tbl->expected_tpt = expected_tpt_siso40MHzSGI;
1029 else
1030 tbl->expected_tpt = expected_tpt_siso40MHz;
1031 else if (tbl->is_SGI)
1032 tbl->expected_tpt = expected_tpt_siso20MHzSGI;
1033 else
1034 tbl->expected_tpt = expected_tpt_siso20MHz;
1035
Guy Cohenfde0db32008-04-21 15:42:01 -07001036 } else if (is_mimo(tbl->lq_type)) { /* FIXME:need to separate mimo2/3 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001037 if (tbl->is_fat && !lq_sta->is_dup)
Zhu Yib481de92007-09-25 17:54:57 -07001038 if (tbl->is_SGI)
1039 tbl->expected_tpt = expected_tpt_mimo40MHzSGI;
1040 else
1041 tbl->expected_tpt = expected_tpt_mimo40MHz;
1042 else if (tbl->is_SGI)
1043 tbl->expected_tpt = expected_tpt_mimo20MHzSGI;
1044 else
1045 tbl->expected_tpt = expected_tpt_mimo20MHz;
1046 } else
1047 tbl->expected_tpt = expected_tpt_G;
1048}
1049
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001050#ifdef CONFIG_IWL4965_HT
Ben Cahill77626352007-11-29 11:09:44 +08001051/*
1052 * Find starting rate for new "search" high-throughput mode of modulation.
1053 * Goal is to find lowest expected rate (under perfect conditions) that is
1054 * above the current measured throughput of "active" mode, to give new mode
1055 * a fair chance to prove itself without too many challenges.
1056 *
1057 * This gets called when transitioning to more aggressive modulation
1058 * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
1059 * (i.e. MIMO to SISO). When moving to MIMO, bit rate will typically need
1060 * to decrease to match "active" throughput. When moving from MIMO to SISO,
1061 * bit rate will typically need to increase, but not if performance was bad.
1062 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001063static s32 rs_get_best_rate(struct iwl_priv *priv,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001064 struct iwl4965_lq_sta *lq_sta,
Ben Cahill77626352007-11-29 11:09:44 +08001065 struct iwl4965_scale_tbl_info *tbl, /* "search" */
Guy Cohenf935a6d2008-04-23 17:15:02 -07001066 u16 rate_mask, s8 index)
Zhu Yib481de92007-09-25 17:54:57 -07001067{
Ben Cahill77626352007-11-29 11:09:44 +08001068 /* "active" values */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001069 struct iwl4965_scale_tbl_info *active_tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001070 &(lq_sta->lq_info[lq_sta->active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07001071 s32 active_sr = active_tbl->win[index].success_ratio;
Zhu Yib481de92007-09-25 17:54:57 -07001072 s32 active_tpt = active_tbl->expected_tpt[index];
Ben Cahill77626352007-11-29 11:09:44 +08001073
1074 /* expected "search" throughput */
1075 s32 *tpt_tbl = tbl->expected_tpt;
1076
1077 s32 new_rate, high, low, start_hi;
Zhu Yib481de92007-09-25 17:54:57 -07001078 u16 high_low;
Guy Cohenf935a6d2008-04-23 17:15:02 -07001079 s8 rate = index;
Zhu Yib481de92007-09-25 17:54:57 -07001080
1081 new_rate = high = low = start_hi = IWL_RATE_INVALID;
1082
1083 for (; ;) {
Ester Kummerbf403db2008-05-05 10:22:40 +08001084 high_low = rs_get_adjacent_rate(priv, rate, rate_mask,
1085 tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -07001086
1087 low = high_low & 0xff;
1088 high = (high_low >> 8) & 0xff;
1089
Ben Cahill77626352007-11-29 11:09:44 +08001090 /*
1091 * Lower the "search" bit rate, to give new "search" mode
1092 * approximately the same throughput as "active" if:
1093 *
1094 * 1) "Active" mode has been working modestly well (but not
1095 * great), and expected "search" throughput (under perfect
1096 * conditions) at candidate rate is above the actual
1097 * measured "active" throughput (but less than expected
1098 * "active" throughput under perfect conditions).
1099 * OR
1100 * 2) "Active" mode has been working perfectly or very well
1101 * and expected "search" throughput (under perfect
1102 * conditions) at candidate rate is above expected
1103 * "active" throughput (under perfect conditions).
1104 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001105 if ((((100 * tpt_tbl[rate]) > lq_sta->last_tpt) &&
Zhu Yib481de92007-09-25 17:54:57 -07001106 ((active_sr > IWL_RATE_DECREASE_TH) &&
1107 (active_sr <= IWL_RATE_HIGH_TH) &&
1108 (tpt_tbl[rate] <= active_tpt))) ||
1109 ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
1110 (tpt_tbl[rate] > active_tpt))) {
1111
Ben Cahill77626352007-11-29 11:09:44 +08001112 /* (2nd or later pass)
1113 * If we've already tried to raise the rate, and are
1114 * now trying to lower it, use the higher rate. */
Zhu Yib481de92007-09-25 17:54:57 -07001115 if (start_hi != IWL_RATE_INVALID) {
1116 new_rate = start_hi;
1117 break;
1118 }
Ben Cahill77626352007-11-29 11:09:44 +08001119
Zhu Yib481de92007-09-25 17:54:57 -07001120 new_rate = rate;
Ben Cahill77626352007-11-29 11:09:44 +08001121
1122 /* Loop again with lower rate */
Zhu Yib481de92007-09-25 17:54:57 -07001123 if (low != IWL_RATE_INVALID)
1124 rate = low;
Ben Cahill77626352007-11-29 11:09:44 +08001125
1126 /* Lower rate not available, use the original */
Zhu Yib481de92007-09-25 17:54:57 -07001127 else
1128 break;
Ben Cahill77626352007-11-29 11:09:44 +08001129
1130 /* Else try to raise the "search" rate to match "active" */
Zhu Yib481de92007-09-25 17:54:57 -07001131 } else {
Ben Cahill77626352007-11-29 11:09:44 +08001132 /* (2nd or later pass)
1133 * If we've already tried to lower the rate, and are
1134 * now trying to raise it, use the lower rate. */
Zhu Yib481de92007-09-25 17:54:57 -07001135 if (new_rate != IWL_RATE_INVALID)
1136 break;
Ben Cahill77626352007-11-29 11:09:44 +08001137
1138 /* Loop again with higher rate */
Zhu Yib481de92007-09-25 17:54:57 -07001139 else if (high != IWL_RATE_INVALID) {
1140 start_hi = high;
1141 rate = high;
Ben Cahill77626352007-11-29 11:09:44 +08001142
1143 /* Higher rate not available, use the original */
Zhu Yib481de92007-09-25 17:54:57 -07001144 } else {
1145 new_rate = rate;
1146 break;
1147 }
1148 }
1149 }
1150
1151 return new_rate;
1152}
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001153#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001154
Ben Cahill77626352007-11-29 11:09:44 +08001155/*
1156 * Set up search table for MIMO
1157 */
Guy Cohenfde0db32008-04-21 15:42:01 -07001158#ifdef CONFIG_IWL4965_HT
1159static int rs_switch_to_mimo2(struct iwl_priv *priv,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001160 struct iwl4965_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001161 struct ieee80211_conf *conf,
1162 struct sta_info *sta,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001163 struct iwl4965_scale_tbl_info *tbl, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001164{
Zhu Yib481de92007-09-25 17:54:57 -07001165 u16 rate_mask;
1166 s32 rate;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001167 s8 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07001168
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001169 if (!(conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) ||
1170 !sta->ht_info.ht_supported)
Zhu Yib481de92007-09-25 17:54:57 -07001171 return -1;
1172
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001173 if (priv->current_ht_config.tx_mimo_ps_mode == IWL_MIMO_PS_STATIC)
Zhu Yib481de92007-09-25 17:54:57 -07001174 return -1;
1175
Ben Cahill77626352007-11-29 11:09:44 +08001176 /* Need both Tx chains/antennas to support MIMO */
Guy Cohenaade00c2008-04-23 17:14:59 -07001177 if (priv->hw_params.tx_chains_num < 2)
Zhu Yib481de92007-09-25 17:54:57 -07001178 return -1;
1179
Guy Coheneecd6e52008-04-23 17:14:58 -07001180 IWL_DEBUG_RATE("LQ: try to switch to MIMO2\n");
Guy Cohen39e88502008-04-23 17:14:57 -07001181
1182 tbl->lq_type = LQ_MIMO2;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001183 tbl->is_dup = lq_sta->is_dup;
Zhu Yib481de92007-09-25 17:54:57 -07001184 tbl->action = 0;
Guy Cohen39e88502008-04-23 17:14:57 -07001185 rate_mask = lq_sta->active_mimo2_rate;
1186
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001187 if (priv->current_ht_config.supported_chan_width
Guy Cohen39e88502008-04-23 17:14:57 -07001188 == IWL_CHANNEL_WIDTH_40MHZ)
Zhu Yib481de92007-09-25 17:54:57 -07001189 tbl->is_fat = 1;
1190 else
1191 tbl->is_fat = 0;
1192
Guy Cohen39e88502008-04-23 17:14:57 -07001193 /* FIXME: - don't toggle SGI here
Zhu Yib481de92007-09-25 17:54:57 -07001194 if (tbl->is_fat) {
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001195 if (priv->current_ht_config.sgf & HT_SHORT_GI_40MHZ_ONLY)
Zhu Yib481de92007-09-25 17:54:57 -07001196 tbl->is_SGI = 1;
1197 else
1198 tbl->is_SGI = 0;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001199 } else if (priv->current_ht_config.sgf & HT_SHORT_GI_20MHZ_ONLY)
Zhu Yib481de92007-09-25 17:54:57 -07001200 tbl->is_SGI = 1;
1201 else
1202 tbl->is_SGI = 0;
Guy Cohen39e88502008-04-23 17:14:57 -07001203 */
Zhu Yib481de92007-09-25 17:54:57 -07001204
Guy Coheneecd6e52008-04-23 17:14:58 -07001205 rs_set_expected_tpt_table(lq_sta, tbl);
Zhu Yib481de92007-09-25 17:54:57 -07001206
Guy Cohenf935a6d2008-04-23 17:15:02 -07001207 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
Zhu Yib481de92007-09-25 17:54:57 -07001208
Guy Coheneecd6e52008-04-23 17:14:58 -07001209 IWL_DEBUG_RATE("LQ: MIMO2 best rate %d mask %X\n", rate, rate_mask);
Guy Cohen39e88502008-04-23 17:14:57 -07001210
1211 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
Guy Coheneecd6e52008-04-23 17:14:58 -07001212 IWL_DEBUG_RATE("Can't switch with index %d rate mask %x\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001213 rate, rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -07001214 return -1;
Guy Cohen39e88502008-04-23 17:14:57 -07001215 }
1216 tbl->current_rate = rate_n_flags_from_tbl(tbl, rate, is_green);
Zhu Yib481de92007-09-25 17:54:57 -07001217
Guy Coheneecd6e52008-04-23 17:14:58 -07001218 IWL_DEBUG_RATE("LQ: Switch to new mcs %X index is green %X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001219 tbl->current_rate, is_green);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001220 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07001221}
Guy Cohenfde0db32008-04-21 15:42:01 -07001222#else
1223static int rs_switch_to_mimo2(struct iwl_priv *priv,
1224 struct iwl4965_lq_sta *lq_sta,
1225 struct ieee80211_conf *conf,
1226 struct sta_info *sta,
1227 struct iwl4965_scale_tbl_info *tbl, int index)
1228{
1229 return -1;
1230}
1231#endif /*CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001232
Ben Cahill77626352007-11-29 11:09:44 +08001233/*
1234 * Set up search table for SISO
1235 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001236static int rs_switch_to_siso(struct iwl_priv *priv,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001237 struct iwl4965_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001238 struct ieee80211_conf *conf,
1239 struct sta_info *sta,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001240 struct iwl4965_scale_tbl_info *tbl, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001241{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001242#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07001243 u16 rate_mask;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001244 u8 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07001245 s32 rate;
1246
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001247 if (!(conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) ||
1248 !sta->ht_info.ht_supported)
Zhu Yib481de92007-09-25 17:54:57 -07001249 return -1;
1250
Guy Coheneecd6e52008-04-23 17:14:58 -07001251 IWL_DEBUG_RATE("LQ: try to switch to SISO\n");
Guy Cohen39e88502008-04-23 17:14:57 -07001252
Tomas Winklerc33104f2008-01-14 17:46:21 -08001253 tbl->is_dup = lq_sta->is_dup;
Zhu Yib481de92007-09-25 17:54:57 -07001254 tbl->lq_type = LQ_SISO;
1255 tbl->action = 0;
Guy Cohen39e88502008-04-23 17:14:57 -07001256 rate_mask = lq_sta->active_siso_rate;
Zhu Yib481de92007-09-25 17:54:57 -07001257
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001258 if (priv->current_ht_config.supported_chan_width
1259 == IWL_CHANNEL_WIDTH_40MHZ)
Zhu Yib481de92007-09-25 17:54:57 -07001260 tbl->is_fat = 1;
1261 else
1262 tbl->is_fat = 0;
1263
Guy Cohen39e88502008-04-23 17:14:57 -07001264 /* FIXME: - don't toggle SGI here
Zhu Yib481de92007-09-25 17:54:57 -07001265 if (tbl->is_fat) {
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001266 if (priv->current_ht_config.sgf & HT_SHORT_GI_40MHZ_ONLY)
Zhu Yib481de92007-09-25 17:54:57 -07001267 tbl->is_SGI = 1;
1268 else
1269 tbl->is_SGI = 0;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001270 } else if (priv->current_ht_config.sgf & HT_SHORT_GI_20MHZ_ONLY)
Zhu Yib481de92007-09-25 17:54:57 -07001271 tbl->is_SGI = 1;
1272 else
1273 tbl->is_SGI = 0;
Guy Cohen39e88502008-04-23 17:14:57 -07001274 */
Zhu Yib481de92007-09-25 17:54:57 -07001275
1276 if (is_green)
Guy Cohen39e88502008-04-23 17:14:57 -07001277 tbl->is_SGI = 0; /*11n spec: no SGI in SISO+Greenfield*/
Zhu Yib481de92007-09-25 17:54:57 -07001278
Guy Coheneecd6e52008-04-23 17:14:58 -07001279 rs_set_expected_tpt_table(lq_sta, tbl);
Guy Cohenf935a6d2008-04-23 17:15:02 -07001280 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
Zhu Yib481de92007-09-25 17:54:57 -07001281
Guy Coheneecd6e52008-04-23 17:14:58 -07001282 IWL_DEBUG_RATE("LQ: get best rate %d mask %X\n", rate, rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -07001283 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
Guy Coheneecd6e52008-04-23 17:14:58 -07001284 IWL_DEBUG_RATE("can not switch with index %d rate mask %x\n",
Zhu Yib481de92007-09-25 17:54:57 -07001285 rate, rate_mask);
1286 return -1;
1287 }
Guy Cohen39e88502008-04-23 17:14:57 -07001288 tbl->current_rate = rate_n_flags_from_tbl(tbl, rate, is_green);
Guy Coheneecd6e52008-04-23 17:14:58 -07001289 IWL_DEBUG_RATE("LQ: Switch to new mcs %X index is green %X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001290 tbl->current_rate, is_green);
Mohamed Abbas403ab562007-11-06 22:06:25 -08001291 return 0;
1292#else
1293 return -1;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001294#endif /*CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001295}
1296
Ben Cahill77626352007-11-29 11:09:44 +08001297/*
1298 * Try to switch to new modulation mode from legacy
1299 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001300static int rs_move_legacy_other(struct iwl_priv *priv,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001301 struct iwl4965_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001302 struct ieee80211_conf *conf,
1303 struct sta_info *sta,
Zhu Yib481de92007-09-25 17:54:57 -07001304 int index)
1305{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001306 struct iwl4965_scale_tbl_info *tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001307 &(lq_sta->lq_info[lq_sta->active_tbl]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001308 struct iwl4965_scale_tbl_info *search_tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001309 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001310 struct iwl4965_rate_scale_data *window = &(tbl->win[index]);
1311 u32 sz = (sizeof(struct iwl4965_scale_tbl_info) -
1312 (sizeof(struct iwl4965_rate_scale_data) * IWL_RATE_COUNT));
Zhu Yib481de92007-09-25 17:54:57 -07001313 u8 start_action = tbl->action;
Guy Cohenfde0db32008-04-21 15:42:01 -07001314 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1315 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001316
1317 for (; ;) {
1318 switch (tbl->action) {
1319 case IWL_LEGACY_SWITCH_ANTENNA:
Guy Cohenf935a6d2008-04-23 17:15:02 -07001320 IWL_DEBUG_RATE("LQ: Legacy toggle Antenna\n");
Zhu Yib481de92007-09-25 17:54:57 -07001321
Tomas Winklerc33104f2008-01-14 17:46:21 -08001322 lq_sta->action_counter++;
Ben Cahill77626352007-11-29 11:09:44 +08001323
1324 /* Don't change antenna if success has been great */
Zhu Yib481de92007-09-25 17:54:57 -07001325 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1326 break;
Ben Cahill77626352007-11-29 11:09:44 +08001327
Ben Cahill77626352007-11-29 11:09:44 +08001328 /* Set up search table to try other antenna */
Zhu Yib481de92007-09-25 17:54:57 -07001329 memcpy(search_tbl, tbl, sz);
1330
Guy Cohenaade00c2008-04-23 17:14:59 -07001331 if (rs_toggle_antenna(valid_tx_ant,
1332 &search_tbl->current_rate, search_tbl)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07001333 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");
Zhu Yib481de92007-09-25 17:54:57 -07001413 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1414 break;
Zhu Yib481de92007-09-25 17:54:57 -07001415
1416 memcpy(search_tbl, tbl, sz);
Guy Cohenaade00c2008-04-23 17:14:59 -07001417 if (rs_toggle_antenna(valid_tx_ant,
1418 &search_tbl->current_rate, search_tbl)) {
1419 lq_sta->search_better_tbl = 1;
1420 goto out;
1421 }
Zhu Yib481de92007-09-25 17:54:57 -07001422
Guy Cohenfde0db32008-04-21 15:42:01 -07001423 case IWL_SISO_SWITCH_MIMO2:
Guy Coheneecd6e52008-04-23 17:14:58 -07001424 IWL_DEBUG_RATE("LQ: SISO switch to MIMO\n");
Zhu Yib481de92007-09-25 17:54:57 -07001425 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001426 search_tbl->is_SGI = 0;
Guy Cohenfde0db32008-04-21 15:42:01 -07001427 search_tbl->ant_type = ANT_AB; /*FIXME:RS*/
1428 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001429 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001430 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001431 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001432 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001433 }
Zhu Yib481de92007-09-25 17:54:57 -07001434 break;
1435 case IWL_SISO_SWITCH_GI:
Guy Coheneecd6e52008-04-23 17:14:58 -07001436 IWL_DEBUG_RATE("LQ: SISO toggle SGI/NGI\n");
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001437
Zhu Yib481de92007-09-25 17:54:57 -07001438 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001439 if (is_green) {
1440 if (!tbl->is_SGI)
1441 break;
1442 else
1443 IWL_ERROR("SGI was set in GF+SISO\n");
Zhu Yib481de92007-09-25 17:54:57 -07001444 }
Guy Cohen39e88502008-04-23 17:14:57 -07001445 search_tbl->is_SGI = !tbl->is_SGI;
Guy Coheneecd6e52008-04-23 17:14:58 -07001446 rs_set_expected_tpt_table(lq_sta, search_tbl);
Guy Cohen39e88502008-04-23 17:14:57 -07001447 if (tbl->is_SGI) {
1448 s32 tpt = lq_sta->last_tpt / 100;
1449 if (tpt >= search_tbl->expected_tpt[index])
1450 break;
1451 }
1452 search_tbl->current_rate = rate_n_flags_from_tbl(
1453 search_tbl, index, is_green);
1454 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001455 goto out;
1456 }
1457 tbl->action++;
1458 if (tbl->action > IWL_SISO_SWITCH_GI)
1459 tbl->action = IWL_SISO_SWITCH_ANTENNA;
1460
1461 if (tbl->action == start_action)
1462 break;
1463 }
1464 return 0;
1465
1466 out:
1467 tbl->action++;
1468 if (tbl->action > IWL_SISO_SWITCH_GI)
1469 tbl->action = IWL_SISO_SWITCH_ANTENNA;
1470 return 0;
1471}
1472
Ben Cahill77626352007-11-29 11:09:44 +08001473/*
1474 * Try to switch to new modulation mode from MIMO
1475 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001476static int rs_move_mimo_to_other(struct iwl_priv *priv,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001477 struct iwl4965_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001478 struct ieee80211_conf *conf,
1479 struct sta_info *sta,
Zhu Yib481de92007-09-25 17:54:57 -07001480 int index)
1481{
Tomas Winklerc33104f2008-01-14 17:46:21 -08001482 s8 is_green = lq_sta->is_green;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001483 struct iwl4965_scale_tbl_info *tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001484 &(lq_sta->lq_info[lq_sta->active_tbl]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001485 struct iwl4965_scale_tbl_info *search_tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001486 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001487 u32 sz = (sizeof(struct iwl4965_scale_tbl_info) -
1488 (sizeof(struct iwl4965_rate_scale_data) * IWL_RATE_COUNT));
Zhu Yib481de92007-09-25 17:54:57 -07001489 u8 start_action = tbl->action;
Guy Cohenaade00c2008-04-23 17:14:59 -07001490 /*u8 valid_tx_ant = priv->hw_params.valid_tx_ant;*/
1491 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001492
1493 for (;;) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001494 lq_sta->action_counter++;
Zhu Yib481de92007-09-25 17:54:57 -07001495 switch (tbl->action) {
1496 case IWL_MIMO_SWITCH_ANTENNA_A:
1497 case IWL_MIMO_SWITCH_ANTENNA_B:
Guy Coheneecd6e52008-04-23 17:14:58 -07001498 IWL_DEBUG_RATE("LQ: MIMO2 switch to SISO\n");
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001499
Ben Cahill77626352007-11-29 11:09:44 +08001500 /* Set up new search table for SISO */
Zhu Yib481de92007-09-25 17:54:57 -07001501 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001502
1503 /*FIXME:RS:need to check ant validity + C*/
Zhu Yib481de92007-09-25 17:54:57 -07001504 if (tbl->action == IWL_MIMO_SWITCH_ANTENNA_A)
Guy Cohenfde0db32008-04-21 15:42:01 -07001505 search_tbl->ant_type = ANT_A;
Zhu Yib481de92007-09-25 17:54:57 -07001506 else
Guy Cohenfde0db32008-04-21 15:42:01 -07001507 search_tbl->ant_type = ANT_B;
Zhu Yib481de92007-09-25 17:54:57 -07001508
Tomas Winklerc33104f2008-01-14 17:46:21 -08001509 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001510 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001511 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001512 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001513 goto out;
1514 }
1515 break;
1516
1517 case IWL_MIMO_SWITCH_GI:
Guy Coheneecd6e52008-04-23 17:14:58 -07001518 IWL_DEBUG_RATE("LQ: MIMO toggle SGI/NGI\n");
Ben Cahill77626352007-11-29 11:09:44 +08001519
1520 /* Set up new search table for MIMO */
Zhu Yib481de92007-09-25 17:54:57 -07001521 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001522 search_tbl->is_SGI = !tbl->is_SGI;
Guy Coheneecd6e52008-04-23 17:14:58 -07001523 rs_set_expected_tpt_table(lq_sta, search_tbl);
Ben Cahill77626352007-11-29 11:09:44 +08001524 /*
1525 * If active table already uses the fastest possible
1526 * modulation (dual stream with short guard interval),
1527 * and it's working well, there's no need to look
1528 * for a better type of modulation!
1529 */
Guy Cohen39e88502008-04-23 17:14:57 -07001530 if (tbl->is_SGI) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001531 s32 tpt = lq_sta->last_tpt / 100;
Guy Cohen39e88502008-04-23 17:14:57 -07001532 if (tpt >= search_tbl->expected_tpt[index])
1533 break;
Zhu Yib481de92007-09-25 17:54:57 -07001534 }
Guy Cohen39e88502008-04-23 17:14:57 -07001535 search_tbl->current_rate = rate_n_flags_from_tbl(
1536 search_tbl, index, is_green);
1537 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001538 goto out;
1539
1540 }
1541 tbl->action++;
1542 if (tbl->action > IWL_MIMO_SWITCH_GI)
1543 tbl->action = IWL_MIMO_SWITCH_ANTENNA_A;
1544
1545 if (tbl->action == start_action)
1546 break;
1547 }
1548
1549 return 0;
1550 out:
1551 tbl->action++;
1552 if (tbl->action > IWL_MIMO_SWITCH_GI)
1553 tbl->action = IWL_MIMO_SWITCH_ANTENNA_A;
1554 return 0;
1555
1556}
1557
Ben Cahill77626352007-11-29 11:09:44 +08001558/*
1559 * Check whether we should continue using same modulation mode, or
1560 * begin search for a new mode, based on:
1561 * 1) # tx successes or failures while using this mode
1562 * 2) # times calling this function
1563 * 3) elapsed time in this mode (not used, for now)
1564 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001565static void rs_stay_in_table(struct iwl4965_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07001566{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001567 struct iwl4965_scale_tbl_info *tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001568 int i;
1569 int active_tbl;
1570 int flush_interval_passed = 0;
Ester Kummerbf403db2008-05-05 10:22:40 +08001571 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07001572
Ester Kummerbf403db2008-05-05 10:22:40 +08001573 priv = lq_sta->drv;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001574 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001575
Tomas Winklerc33104f2008-01-14 17:46:21 -08001576 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07001577
Ben Cahill77626352007-11-29 11:09:44 +08001578 /* If we've been disallowing search, see if we should now allow it */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001579 if (lq_sta->stay_in_tbl) {
Zhu Yib481de92007-09-25 17:54:57 -07001580
Ben Cahill77626352007-11-29 11:09:44 +08001581 /* Elapsed time using current modulation mode */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001582 if (lq_sta->flush_timer)
Zhu Yib481de92007-09-25 17:54:57 -07001583 flush_interval_passed =
1584 time_after(jiffies,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001585 (unsigned long)(lq_sta->flush_timer +
Zhu Yib481de92007-09-25 17:54:57 -07001586 IWL_RATE_SCALE_FLUSH_INTVL));
1587
Ben Cahill77626352007-11-29 11:09:44 +08001588 /*
1589 * Check if we should allow search for new modulation mode.
1590 * If many frames have failed or succeeded, or we've used
1591 * this same modulation for a long time, allow search, and
1592 * reset history stats that keep track of whether we should
1593 * allow a new search. Also (below) reset all bitmaps and
1594 * stats in active history.
1595 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001596 if ((lq_sta->total_failed > lq_sta->max_failure_limit) ||
1597 (lq_sta->total_success > lq_sta->max_success_limit) ||
1598 ((!lq_sta->search_better_tbl) && (lq_sta->flush_timer)
Zhu Yib481de92007-09-25 17:54:57 -07001599 && (flush_interval_passed))) {
Guy Coheneecd6e52008-04-23 17:14:58 -07001600 IWL_DEBUG_RATE("LQ: stay is expired %d %d %d\n:",
Tomas Winklerc33104f2008-01-14 17:46:21 -08001601 lq_sta->total_failed,
1602 lq_sta->total_success,
Zhu Yib481de92007-09-25 17:54:57 -07001603 flush_interval_passed);
Ben Cahill77626352007-11-29 11:09:44 +08001604
1605 /* Allow search for new mode */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001606 lq_sta->stay_in_tbl = 0; /* only place reset */
1607 lq_sta->total_failed = 0;
1608 lq_sta->total_success = 0;
1609 lq_sta->flush_timer = 0;
Ben Cahill77626352007-11-29 11:09:44 +08001610
1611 /*
1612 * Else if we've used this modulation mode enough repetitions
1613 * (regardless of elapsed time or success/failure), reset
1614 * history bitmaps and rate-specific stats for all rates in
1615 * active table.
1616 */
Mohamed Abbas403ab562007-11-06 22:06:25 -08001617 } else {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001618 lq_sta->table_count++;
1619 if (lq_sta->table_count >=
1620 lq_sta->table_count_limit) {
1621 lq_sta->table_count = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001622
Guy Coheneecd6e52008-04-23 17:14:58 -07001623 IWL_DEBUG_RATE("LQ: stay in table clear win\n");
Zhu Yib481de92007-09-25 17:54:57 -07001624 for (i = 0; i < IWL_RATE_COUNT; i++)
1625 rs_rate_scale_clear_window(
1626 &(tbl->win[i]));
1627 }
1628 }
1629
Ben Cahill77626352007-11-29 11:09:44 +08001630 /* If transitioning to allow "search", reset all history
1631 * bitmaps and stats in active table (this will become the new
1632 * "search" table). */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001633 if (!lq_sta->stay_in_tbl) {
Zhu Yib481de92007-09-25 17:54:57 -07001634 for (i = 0; i < IWL_RATE_COUNT; i++)
1635 rs_rate_scale_clear_window(&(tbl->win[i]));
1636 }
1637 }
1638}
1639
Ben Cahill77626352007-11-29 11:09:44 +08001640/*
1641 * Do rate scaling and search for new modulation mode.
1642 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001643static void rs_rate_scale_perform(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001644 struct net_device *dev,
1645 struct ieee80211_hdr *hdr,
1646 struct sta_info *sta)
1647{
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001648 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1649 struct ieee80211_hw *hw = local_to_hw(local);
1650 struct ieee80211_conf *conf = &hw->conf;
Zhu Yib481de92007-09-25 17:54:57 -07001651 int low = IWL_RATE_INVALID;
1652 int high = IWL_RATE_INVALID;
1653 int index;
1654 int i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001655 struct iwl4965_rate_scale_data *window = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001656 int current_tpt = IWL_INVALID_VALUE;
1657 int low_tpt = IWL_INVALID_VALUE;
1658 int high_tpt = IWL_INVALID_VALUE;
1659 u32 fail_count;
1660 s8 scale_action = 0;
1661 u16 fc, rate_mask;
1662 u8 update_lq = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001663 struct iwl4965_lq_sta *lq_sta;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001664 struct iwl4965_scale_tbl_info *tbl, *tbl1;
Zhu Yib481de92007-09-25 17:54:57 -07001665 u16 rate_scale_index_msk = 0;
Guy Cohen39e88502008-04-23 17:14:57 -07001666 u32 rate;
Zhu Yib481de92007-09-25 17:54:57 -07001667 u8 is_green = 0;
1668 u8 active_tbl = 0;
1669 u8 done_search = 0;
1670 u16 high_low;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001671#ifdef CONFIG_IWL4965_HT
1672 u8 tid = MAX_TID_COUNT;
1673 __le16 *qc;
1674#endif
Zhu Yib481de92007-09-25 17:54:57 -07001675
1676 IWL_DEBUG_RATE("rate scale calculate new rate for skb\n");
1677
1678 fc = le16_to_cpu(hdr->frame_control);
1679 if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1)) {
1680 /* Send management frames and broadcast/multicast data using
1681 * lowest rate. */
1682 /* TODO: this could probably be improved.. */
1683 return;
1684 }
1685
1686 if (!sta || !sta->rate_ctrl_priv)
1687 return;
1688
1689 if (!priv->lq_mngr.lq_ready) {
1690 IWL_DEBUG_RATE("still rate scaling not ready\n");
1691 return;
1692 }
Tomas Winklerc33104f2008-01-14 17:46:21 -08001693 lq_sta = (struct iwl4965_lq_sta *)sta->rate_ctrl_priv;
Zhu Yib481de92007-09-25 17:54:57 -07001694
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001695#ifdef CONFIG_IWL4965_HT
1696 qc = ieee80211_get_qos_ctrl(hdr);
1697 if (qc) {
1698 tid = (u8)(le16_to_cpu(*qc) & 0xf);
1699 rs_tl_add_packet(lq_sta, tid);
1700 }
1701#endif
Ben Cahill77626352007-11-29 11:09:44 +08001702 /*
1703 * Select rate-scale / modulation-mode table to work with in
1704 * the rest of this function: "search" if searching for better
1705 * modulation mode, or "active" if doing rate scaling within a mode.
1706 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001707 if (!lq_sta->search_better_tbl)
1708 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001709 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08001710 active_tbl = 1 - lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001711
Tomas Winklerc33104f2008-01-14 17:46:21 -08001712 tbl = &(lq_sta->lq_info[active_tbl]);
1713 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07001714
Ben Cahill77626352007-11-29 11:09:44 +08001715 /* current tx rate */
Johannes Berg8318d782008-01-24 19:38:38 +01001716 index = sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07001717
1718 IWL_DEBUG_RATE("Rate scale index %d for type %d\n", index,
1719 tbl->lq_type);
1720
Ben Cahill77626352007-11-29 11:09:44 +08001721 /* rates available for this association, and for modulation mode */
Guy Coheneecd6e52008-04-23 17:14:58 -07001722 rate_mask = rs_get_supported_rates(lq_sta, hdr, tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -07001723
1724 IWL_DEBUG_RATE("mask 0x%04X \n", rate_mask);
1725
1726 /* mask with station rate restriction */
1727 if (is_legacy(tbl->lq_type)) {
Johannes Berg8318d782008-01-24 19:38:38 +01001728 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Ben Cahill77626352007-11-29 11:09:44 +08001729 /* supp_rates has no CCK bits in A mode */
Zhu Yib481de92007-09-25 17:54:57 -07001730 rate_scale_index_msk = (u16) (rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -08001731 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
Zhu Yib481de92007-09-25 17:54:57 -07001732 else
1733 rate_scale_index_msk = (u16) (rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -08001734 lq_sta->supp_rates);
Zhu Yib481de92007-09-25 17:54:57 -07001735
1736 } else
1737 rate_scale_index_msk = rate_mask;
1738
1739 if (!rate_scale_index_msk)
1740 rate_scale_index_msk = rate_mask;
1741
Guy Cohen07bc28e2008-04-23 17:15:03 -07001742 if (!((1 << index) & rate_scale_index_msk)) {
1743 IWL_ERROR("Current Rate is not valid\n");
1744 return;
Zhu Yib481de92007-09-25 17:54:57 -07001745 }
1746
Ben Cahill77626352007-11-29 11:09:44 +08001747 /* Get expected throughput table and history window for current rate */
Guy Cohen07bc28e2008-04-23 17:15:03 -07001748 if (!tbl->expected_tpt) {
1749 IWL_ERROR("tbl->expected_tpt is NULL\n");
1750 return;
1751 }
Zhu Yib481de92007-09-25 17:54:57 -07001752
1753 window = &(tbl->win[index]);
1754
Ben Cahill77626352007-11-29 11:09:44 +08001755 /*
1756 * If there is not enough history to calculate actual average
1757 * throughput, keep analyzing results of more tx frames, without
1758 * changing rate or mode (bypass most of the rest of this function).
1759 * Set up new rate table in uCode only if old rate is not supported
1760 * in current association (use new rate found above).
1761 */
Zhu Yib481de92007-09-25 17:54:57 -07001762 fail_count = window->counter - window->success_counter;
Guy Cohen07bc28e2008-04-23 17:15:03 -07001763 if ((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
1764 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH)) {
1765 IWL_DEBUG_RATE("LQ: still below TH. succ=%d total=%d "
Zhu Yib481de92007-09-25 17:54:57 -07001766 "for index %d\n",
1767 window->success_counter, window->counter, index);
Ben Cahill77626352007-11-29 11:09:44 +08001768
1769 /* Can't calculate this yet; not enough history */
Zhu Yib481de92007-09-25 17:54:57 -07001770 window->average_tpt = IWL_INVALID_VALUE;
Ben Cahill77626352007-11-29 11:09:44 +08001771
1772 /* Should we stay with this modulation mode,
1773 * or search for a new one? */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001774 rs_stay_in_table(lq_sta);
Ben Cahill77626352007-11-29 11:09:44 +08001775
Zhu Yib481de92007-09-25 17:54:57 -07001776 goto out;
1777
Ben Cahill77626352007-11-29 11:09:44 +08001778 /* Else we have enough samples; calculate estimate of
1779 * actual average throughput */
Guy Cohen07bc28e2008-04-23 17:15:03 -07001780 } else {
1781 /*FIXME:RS remove this else if we don't get this error*/
1782 if (window->average_tpt != ((window->success_ratio *
1783 tbl->expected_tpt[index] + 64) / 128)) {
1784 IWL_ERROR("expected_tpt should have been calculated"
1785 " by now\n");
1786 window->average_tpt = ((window->success_ratio *
Zhu Yib481de92007-09-25 17:54:57 -07001787 tbl->expected_tpt[index] + 64) / 128);
Guy Cohen07bc28e2008-04-23 17:15:03 -07001788 }
1789 }
Zhu Yib481de92007-09-25 17:54:57 -07001790
Ben Cahill77626352007-11-29 11:09:44 +08001791 /* If we are searching for better modulation mode, check success. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001792 if (lq_sta->search_better_tbl) {
Zhu Yib481de92007-09-25 17:54:57 -07001793
Ben Cahill77626352007-11-29 11:09:44 +08001794 /* If good success, continue using the "search" mode;
1795 * no need to send new link quality command, since we're
1796 * continuing to use the setup that we've been trying. */
Guy Cohen07bc28e2008-04-23 17:15:03 -07001797 if (window->average_tpt > lq_sta->last_tpt) {
1798
1799 IWL_DEBUG_RATE("LQ: SWITCHING TO CURRENT TABLE "
1800 "suc=%d cur-tpt=%d old-tpt=%d\n",
1801 window->success_ratio,
1802 window->average_tpt,
1803 lq_sta->last_tpt);
1804
1805 if (!is_legacy(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08001806 lq_sta->enable_counter = 1;
Guy Cohen07bc28e2008-04-23 17:15:03 -07001807
Ben Cahill77626352007-11-29 11:09:44 +08001808 /* Swap tables; "search" becomes "active" */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001809 lq_sta->active_tbl = active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001810 current_tpt = window->average_tpt;
Ben Cahill77626352007-11-29 11:09:44 +08001811
1812 /* Else poor success; go back to mode in "active" table */
Zhu Yib481de92007-09-25 17:54:57 -07001813 } else {
Guy Cohen07bc28e2008-04-23 17:15:03 -07001814
1815 IWL_DEBUG_RATE("LQ: GOING BACK TO THE OLD TABLE "
1816 "suc=%d cur-tpt=%d old-tpt=%d\n",
1817 window->success_ratio,
1818 window->average_tpt,
1819 lq_sta->last_tpt);
1820
Ben Cahill77626352007-11-29 11:09:44 +08001821 /* Nullify "search" table */
Zhu Yib481de92007-09-25 17:54:57 -07001822 tbl->lq_type = LQ_NONE;
Ben Cahill77626352007-11-29 11:09:44 +08001823
1824 /* Revert to "active" table */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001825 active_tbl = lq_sta->active_tbl;
1826 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07001827
Ben Cahill77626352007-11-29 11:09:44 +08001828 /* Revert to "active" rate and throughput info */
Tomas Winkler17744ff2008-03-02 01:52:00 +02001829 index = iwl4965_hwrate_to_plcp_idx(
Guy Cohen39e88502008-04-23 17:14:57 -07001830 tbl->current_rate);
Tomas Winklerc33104f2008-01-14 17:46:21 -08001831 current_tpt = lq_sta->last_tpt;
Ben Cahill77626352007-11-29 11:09:44 +08001832
1833 /* Need to set up a new rate table in uCode */
1834 update_lq = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001835 }
Ben Cahill77626352007-11-29 11:09:44 +08001836
1837 /* Either way, we've made a decision; modulation mode
1838 * search is done, allow rate adjustment next time. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001839 lq_sta->search_better_tbl = 0;
Ben Cahill77626352007-11-29 11:09:44 +08001840 done_search = 1; /* Don't switch modes below! */
Zhu Yib481de92007-09-25 17:54:57 -07001841 goto lq_update;
1842 }
1843
Ben Cahill77626352007-11-29 11:09:44 +08001844 /* (Else) not in search of better modulation mode, try for better
1845 * starting rate, while staying in this mode. */
Ester Kummerbf403db2008-05-05 10:22:40 +08001846 high_low = rs_get_adjacent_rate(priv, index, rate_scale_index_msk,
Zhu Yib481de92007-09-25 17:54:57 -07001847 tbl->lq_type);
1848 low = high_low & 0xff;
1849 high = (high_low >> 8) & 0xff;
1850
Ben Cahill77626352007-11-29 11:09:44 +08001851 /* Collect measured throughputs for current and adjacent rates */
Zhu Yib481de92007-09-25 17:54:57 -07001852 current_tpt = window->average_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07001853 if (low != IWL_RATE_INVALID)
1854 low_tpt = tbl->win[low].average_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07001855 if (high != IWL_RATE_INVALID)
1856 high_tpt = tbl->win[high].average_tpt;
1857
Ben Cahill77626352007-11-29 11:09:44 +08001858 /* Assume rate increase */
Zhu Yib481de92007-09-25 17:54:57 -07001859 scale_action = 1;
1860
Ben Cahill77626352007-11-29 11:09:44 +08001861 /* Too many failures, decrease rate */
Zhu Yib481de92007-09-25 17:54:57 -07001862 if ((window->success_ratio <= IWL_RATE_DECREASE_TH) ||
1863 (current_tpt == 0)) {
1864 IWL_DEBUG_RATE("decrease rate because of low success_ratio\n");
1865 scale_action = -1;
Ben Cahill77626352007-11-29 11:09:44 +08001866
1867 /* No throughput measured yet for adjacent rates; try increase. */
Zhu Yib481de92007-09-25 17:54:57 -07001868 } else if ((low_tpt == IWL_INVALID_VALUE) &&
1869 (high_tpt == IWL_INVALID_VALUE))
1870 scale_action = 1;
Ben Cahill77626352007-11-29 11:09:44 +08001871
1872 /* Both adjacent throughputs are measured, but neither one has better
1873 * throughput; we're using the best rate, don't change it! */
Zhu Yib481de92007-09-25 17:54:57 -07001874 else if ((low_tpt != IWL_INVALID_VALUE) &&
1875 (high_tpt != IWL_INVALID_VALUE) &&
1876 (low_tpt < current_tpt) &&
1877 (high_tpt < current_tpt))
1878 scale_action = 0;
Ben Cahill77626352007-11-29 11:09:44 +08001879
1880 /* At least one adjacent rate's throughput is measured,
1881 * and may have better performance. */
Zhu Yib481de92007-09-25 17:54:57 -07001882 else {
Ben Cahill77626352007-11-29 11:09:44 +08001883 /* Higher adjacent rate's throughput is measured */
Zhu Yib481de92007-09-25 17:54:57 -07001884 if (high_tpt != IWL_INVALID_VALUE) {
Ben Cahill77626352007-11-29 11:09:44 +08001885 /* Higher rate has better throughput */
Zhu Yib481de92007-09-25 17:54:57 -07001886 if (high_tpt > current_tpt)
1887 scale_action = 1;
1888 else {
1889 IWL_DEBUG_RATE
1890 ("decrease rate because of high tpt\n");
1891 scale_action = -1;
1892 }
Ben Cahill77626352007-11-29 11:09:44 +08001893
1894 /* Lower adjacent rate's throughput is measured */
Zhu Yib481de92007-09-25 17:54:57 -07001895 } else if (low_tpt != IWL_INVALID_VALUE) {
Ben Cahill77626352007-11-29 11:09:44 +08001896 /* Lower rate has better throughput */
Zhu Yib481de92007-09-25 17:54:57 -07001897 if (low_tpt > current_tpt) {
1898 IWL_DEBUG_RATE
1899 ("decrease rate because of low tpt\n");
1900 scale_action = -1;
1901 } else
1902 scale_action = 1;
1903 }
1904 }
1905
Ben Cahill77626352007-11-29 11:09:44 +08001906 /* Sanity check; asked for decrease, but success rate or throughput
1907 * has been good at old rate. Don't change it. */
Zhu Yib481de92007-09-25 17:54:57 -07001908 if (scale_action == -1) {
1909 if ((low != IWL_RATE_INVALID) &&
1910 ((window->success_ratio > IWL_RATE_HIGH_TH) ||
1911 (current_tpt > (100 * tbl->expected_tpt[low]))))
1912 scale_action = 0;
Ben Cahill77626352007-11-29 11:09:44 +08001913
1914 /* Sanity check; asked for increase, but success rate has not been great
1915 * even at old rate, higher rate will be worse. Don't change it. */
Zhu Yib481de92007-09-25 17:54:57 -07001916 } else if ((scale_action == 1) &&
1917 (window->success_ratio < IWL_RATE_INCREASE_TH))
1918 scale_action = 0;
1919
1920 switch (scale_action) {
1921 case -1:
Ben Cahill77626352007-11-29 11:09:44 +08001922 /* Decrease starting rate, update uCode's rate table */
Zhu Yib481de92007-09-25 17:54:57 -07001923 if (low != IWL_RATE_INVALID) {
1924 update_lq = 1;
1925 index = low;
1926 }
1927 break;
1928 case 1:
Ben Cahill77626352007-11-29 11:09:44 +08001929 /* Increase starting rate, update uCode's rate table */
Zhu Yib481de92007-09-25 17:54:57 -07001930 if (high != IWL_RATE_INVALID) {
1931 update_lq = 1;
1932 index = high;
1933 }
1934
1935 break;
1936 case 0:
Ben Cahill77626352007-11-29 11:09:44 +08001937 /* No change */
Zhu Yib481de92007-09-25 17:54:57 -07001938 default:
1939 break;
1940 }
1941
Guy Coheneecd6e52008-04-23 17:14:58 -07001942 IWL_DEBUG_RATE("choose rate scale index %d action %d low %d "
Zhu Yib481de92007-09-25 17:54:57 -07001943 "high %d type %d\n",
1944 index, scale_action, low, high, tbl->lq_type);
1945
1946 lq_update:
Ben Cahill77626352007-11-29 11:09:44 +08001947 /* Replace uCode's rate table for the destination station. */
Zhu Yib481de92007-09-25 17:54:57 -07001948 if (update_lq) {
Guy Cohen39e88502008-04-23 17:14:57 -07001949 rate = rate_n_flags_from_tbl(tbl, index, is_green);
Guy Cohen07bc28e2008-04-23 17:15:03 -07001950 rs_fill_link_cmd(priv, lq_sta, rate);
Tomas Winkler66c73db2008-04-15 16:01:40 -07001951 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07001952 }
Ben Cahill77626352007-11-29 11:09:44 +08001953
1954 /* Should we stay with this modulation mode, or search for a new one? */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001955 rs_stay_in_table(lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07001956
Ben Cahill77626352007-11-29 11:09:44 +08001957 /*
1958 * Search for new modulation mode if we're:
1959 * 1) Not changing rates right now
1960 * 2) Not just finishing up a search
1961 * 3) Allowing a new search
1962 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001963 if (!update_lq && !done_search && !lq_sta->stay_in_tbl) {
Ben Cahill77626352007-11-29 11:09:44 +08001964 /* Save current throughput to compare with "search" throughput*/
Tomas Winklerc33104f2008-01-14 17:46:21 -08001965 lq_sta->last_tpt = current_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07001966
Ben Cahill77626352007-11-29 11:09:44 +08001967 /* Select a new "search" modulation mode to try.
1968 * If one is found, set up the new "search" table. */
Zhu Yib481de92007-09-25 17:54:57 -07001969 if (is_legacy(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08001970 rs_move_legacy_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07001971 else if (is_siso(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08001972 rs_move_siso_to_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07001973 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08001974 rs_move_mimo_to_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07001975
Ben Cahill77626352007-11-29 11:09:44 +08001976 /* If new "search" mode was selected, set up in uCode table */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001977 if (lq_sta->search_better_tbl) {
Ben Cahill77626352007-11-29 11:09:44 +08001978 /* Access the "search" table, clear its history. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001979 tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Zhu Yib481de92007-09-25 17:54:57 -07001980 for (i = 0; i < IWL_RATE_COUNT; i++)
1981 rs_rate_scale_clear_window(&(tbl->win[i]));
1982
Ben Cahill77626352007-11-29 11:09:44 +08001983 /* Use new "search" start rate */
Tomas Winkler17744ff2008-03-02 01:52:00 +02001984 index = iwl4965_hwrate_to_plcp_idx(
Guy Cohen39e88502008-04-23 17:14:57 -07001985 tbl->current_rate);
Zhu Yib481de92007-09-25 17:54:57 -07001986
Guy Coheneecd6e52008-04-23 17:14:58 -07001987 IWL_DEBUG_RATE("Switch current mcs: %X index: %d\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001988 tbl->current_rate, index);
Guy Cohen07bc28e2008-04-23 17:15:03 -07001989 rs_fill_link_cmd(priv, lq_sta, tbl->current_rate);
Tomas Winkler66c73db2008-04-15 16:01:40 -07001990 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07001991 }
Zhu Yib481de92007-09-25 17:54:57 -07001992
Ben Cahill77626352007-11-29 11:09:44 +08001993 /* If the "active" (non-search) mode was legacy,
1994 * and we've tried switching antennas,
1995 * but we haven't been able to try HT modes (not available),
1996 * stay with best antenna legacy modulation for a while
1997 * before next round of mode comparisons. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001998 tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07001999 if (is_legacy(tbl1->lq_type) &&
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002000#ifdef CONFIG_IWL4965_HT
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002001 (!(conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE)) &&
Zhu Yib481de92007-09-25 17:54:57 -07002002#endif
Tomas Winklerc33104f2008-01-14 17:46:21 -08002003 (lq_sta->action_counter >= 1)) {
2004 lq_sta->action_counter = 0;
Guy Coheneecd6e52008-04-23 17:14:58 -07002005 IWL_DEBUG_RATE("LQ: STAY in legacy table\n");
Ester Kummerbf403db2008-05-05 10:22:40 +08002006 rs_set_stay_in_table(priv, 1, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002007 }
2008
Ben Cahill77626352007-11-29 11:09:44 +08002009 /* If we're in an HT mode, and all 3 mode switch actions
2010 * have been tried and compared, stay in this best modulation
2011 * mode for a while before next round of mode comparisons. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002012 if (lq_sta->enable_counter &&
2013 (lq_sta->action_counter >= IWL_ACTION_LIMIT)) {
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002014#ifdef CONFIG_IWL4965_HT
2015 if ((lq_sta->last_tpt > IWL_AGG_TPT_THREHOLD) &&
2016 (lq_sta->tx_agg_tid_en & (1 << tid)) &&
2017 (tid != MAX_TID_COUNT)) {
Guy Coheneecd6e52008-04-23 17:14:58 -07002018 IWL_DEBUG_RATE("try to aggregate tid %d\n", tid);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002019 rs_tl_turn_on_agg(priv, tid, lq_sta, sta);
Zhu Yib481de92007-09-25 17:54:57 -07002020 }
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002021#endif /*CONFIG_IWL4965_HT */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002022 lq_sta->action_counter = 0;
Ester Kummerbf403db2008-05-05 10:22:40 +08002023 rs_set_stay_in_table(priv, 0, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002024 }
Ben Cahill77626352007-11-29 11:09:44 +08002025
2026 /*
2027 * Else, don't search for a new modulation mode.
2028 * Put new timestamp in stay-in-modulation-mode flush timer if:
2029 * 1) Not changing rates right now
2030 * 2) Not just finishing up a search
2031 * 3) flush timer is empty
2032 */
Zhu Yib481de92007-09-25 17:54:57 -07002033 } else {
Tomas Winklerc33104f2008-01-14 17:46:21 -08002034 if ((!update_lq) && (!done_search) && (!lq_sta->flush_timer))
2035 lq_sta->flush_timer = jiffies;
Zhu Yib481de92007-09-25 17:54:57 -07002036 }
2037
2038out:
Guy Cohen39e88502008-04-23 17:14:57 -07002039 tbl->current_rate = rate_n_flags_from_tbl(tbl, index, is_green);
Zhu Yib481de92007-09-25 17:54:57 -07002040 i = index;
Johannes Berg8318d782008-01-24 19:38:38 +01002041 sta->last_txrate_idx = i;
Zhu Yib481de92007-09-25 17:54:57 -07002042
Johannes Berg8318d782008-01-24 19:38:38 +01002043 /* sta->txrate_idx is an index to A mode rates which start
Zhu Yib481de92007-09-25 17:54:57 -07002044 * at IWL_FIRST_OFDM_RATE
2045 */
Johannes Berg8318d782008-01-24 19:38:38 +01002046 if (lq_sta->band == IEEE80211_BAND_5GHZ)
2047 sta->txrate_idx = i - IWL_FIRST_OFDM_RATE;
Zhu Yib481de92007-09-25 17:54:57 -07002048 else
Johannes Berg8318d782008-01-24 19:38:38 +01002049 sta->txrate_idx = i;
Zhu Yib481de92007-09-25 17:54:57 -07002050
2051 return;
2052}
2053
2054
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002055static void rs_initialize_lq(struct iwl_priv *priv,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002056 struct ieee80211_conf *conf,
Zhu Yib481de92007-09-25 17:54:57 -07002057 struct sta_info *sta)
2058{
Tomas Winklerc33104f2008-01-14 17:46:21 -08002059 struct iwl4965_lq_sta *lq_sta;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002060 struct iwl4965_scale_tbl_info *tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002061 int rate_idx;
Guy Cohenaade00c2008-04-23 17:14:59 -07002062 int i;
Guy Cohen39e88502008-04-23 17:14:57 -07002063 u32 rate;
Guy Cohenaade00c2008-04-23 17:14:59 -07002064 u8 use_green = rs_use_green(priv, conf);
2065 u8 active_tbl = 0;
2066 u8 valid_tx_ant;
Zhu Yib481de92007-09-25 17:54:57 -07002067
2068 if (!sta || !sta->rate_ctrl_priv)
2069 goto out;
2070
Tomas Winklerc33104f2008-01-14 17:46:21 -08002071 lq_sta = (struct iwl4965_lq_sta *)sta->rate_ctrl_priv;
Johannes Berg8318d782008-01-24 19:38:38 +01002072 i = sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002073
Tomas Winklerc33104f2008-01-14 17:46:21 -08002074 if ((lq_sta->lq.sta_id == 0xff) &&
Zhu Yib481de92007-09-25 17:54:57 -07002075 (priv->iw_mode == IEEE80211_IF_TYPE_IBSS))
2076 goto out;
2077
Guy Cohenaade00c2008-04-23 17:14:59 -07002078 valid_tx_ant = priv->hw_params.valid_tx_ant;
2079
Tomas Winklerc33104f2008-01-14 17:46:21 -08002080 if (!lq_sta->search_better_tbl)
2081 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002082 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08002083 active_tbl = 1 - lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002084
Tomas Winklerc33104f2008-01-14 17:46:21 -08002085 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07002086
2087 if ((i < 0) || (i >= IWL_RATE_COUNT))
2088 i = 0;
2089
Guy Cohenfde0db32008-04-21 15:42:01 -07002090 /* FIXME:RS: This is also wrong in 4965 */
Guy Cohen39e88502008-04-23 17:14:57 -07002091 rate = iwl4965_rates[i].plcp;
2092 rate |= RATE_MCS_ANT_B_MSK;
2093 rate &= ~RATE_MCS_ANT_A_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002094
2095 if (i >= IWL_FIRST_CCK_RATE && i <= IWL_LAST_CCK_RATE)
Guy Cohen39e88502008-04-23 17:14:57 -07002096 rate |= RATE_MCS_CCK_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002097
Guy Cohenfde0db32008-04-21 15:42:01 -07002098 tbl->ant_type = ANT_B;
Guy Cohen39e88502008-04-23 17:14:57 -07002099 rs_get_tbl_info_from_mcs(rate, priv->band, tbl, &rate_idx);
Guy Cohenaade00c2008-04-23 17:14:59 -07002100 if (!rs_is_valid_ant(valid_tx_ant, tbl->ant_type))
2101 rs_toggle_antenna(valid_tx_ant, &rate, tbl);
Zhu Yib481de92007-09-25 17:54:57 -07002102
Guy Cohen39e88502008-04-23 17:14:57 -07002103 rate = rate_n_flags_from_tbl(tbl, rate_idx, use_green);
2104 tbl->current_rate = rate;
Guy Coheneecd6e52008-04-23 17:14:58 -07002105 rs_set_expected_tpt_table(lq_sta, tbl);
Guy Cohen07bc28e2008-04-23 17:15:03 -07002106 rs_fill_link_cmd(NULL, lq_sta, rate);
Tomas Winkler66c73db2008-04-15 16:01:40 -07002107 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07002108 out:
2109 return;
2110}
2111
Mattias Nissler1abbe492007-12-20 13:50:07 +01002112static void rs_get_rate(void *priv_rate, struct net_device *dev,
Johannes Berg8318d782008-01-24 19:38:38 +01002113 struct ieee80211_supported_band *sband,
2114 struct sk_buff *skb,
Mattias Nissler1abbe492007-12-20 13:50:07 +01002115 struct rate_selection *sel)
Zhu Yib481de92007-09-25 17:54:57 -07002116{
2117
2118 int i;
2119 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002120 struct ieee80211_conf *conf = &local->hw.conf;
Zhu Yib481de92007-09-25 17:54:57 -07002121 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2122 struct sta_info *sta;
Michael Wu2bc454b2007-12-25 19:33:16 -05002123 u16 fc;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002124 struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002125 struct iwl4965_lq_sta *lq_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002126
Zhu Yi58826352007-09-27 11:27:39 +08002127 IWL_DEBUG_RATE_LIMIT("rate scale calculate new rate for skb\n");
Zhu Yib481de92007-09-25 17:54:57 -07002128
Johannes Bergd0709a62008-02-25 16:27:46 +01002129 rcu_read_lock();
2130
Zhu Yib481de92007-09-25 17:54:57 -07002131 sta = sta_info_get(local, hdr->addr1);
2132
Michael Wu2bc454b2007-12-25 19:33:16 -05002133 /* Send management frames and broadcast/multicast data using lowest
2134 * rate. */
2135 fc = le16_to_cpu(hdr->frame_control);
2136 if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1) ||
2137 !sta || !sta->rate_ctrl_priv) {
Johannes Berg8318d782008-01-24 19:38:38 +01002138 sel->rate = rate_lowest(local, sband, sta);
Tomas Winklerf4d60822008-03-05 11:31:00 -08002139 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07002140 }
2141
Tomas Winklerc33104f2008-01-14 17:46:21 -08002142 lq_sta = (struct iwl4965_lq_sta *)sta->rate_ctrl_priv;
Johannes Berg8318d782008-01-24 19:38:38 +01002143 i = sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002144
Tomas Winklerc33104f2008-01-14 17:46:21 -08002145 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
2146 !lq_sta->ibss_sta_added) {
Tomas Winkler947b13a2008-04-16 16:34:48 -07002147 u8 sta_id = iwl_find_station(priv, hdr->addr1);
Joe Perches0795af52007-10-03 17:59:30 -07002148 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07002149
2150 if (sta_id == IWL_INVALID_STATION) {
Joe Perches0795af52007-10-03 17:59:30 -07002151 IWL_DEBUG_RATE("LQ: ADD station %s\n",
2152 print_mac(mac, hdr->addr1));
Ron Rindjunsky67d62032007-11-26 16:14:40 +02002153 sta_id = iwl4965_add_station_flags(priv, hdr->addr1,
2154 0, CMD_ASYNC, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07002155 }
2156 if ((sta_id != IWL_INVALID_STATION)) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08002157 lq_sta->lq.sta_id = sta_id;
2158 lq_sta->lq.rs_table[0].rate_n_flags = 0;
2159 lq_sta->ibss_sta_added = 1;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002160 rs_initialize_lq(priv, conf, sta);
Zhu Yib481de92007-09-25 17:54:57 -07002161 }
Tomas Winklerc33104f2008-01-14 17:46:21 -08002162 if (!lq_sta->ibss_sta_added)
Zhu Yib481de92007-09-25 17:54:57 -07002163 goto done;
2164 }
2165
Tomas Winklerf4d60822008-03-05 11:31:00 -08002166done:
Mattias Nissler1abbe492007-12-20 13:50:07 +01002167 if ((i < 0) || (i > IWL_RATE_COUNT)) {
Johannes Berg8318d782008-01-24 19:38:38 +01002168 sel->rate = rate_lowest(local, sband, sta);
Tomas Winklerf4d60822008-03-05 11:31:00 -08002169 goto out;
Mattias Nissler1abbe492007-12-20 13:50:07 +01002170 }
Zhu Yib481de92007-09-25 17:54:57 -07002171
Mattias Nissler1abbe492007-12-20 13:50:07 +01002172 sel->rate = &priv->ieee_rates[i];
Tomas Winklerf4d60822008-03-05 11:31:00 -08002173out:
2174 rcu_read_unlock();
Zhu Yib481de92007-09-25 17:54:57 -07002175}
2176
Ester Kummerbf403db2008-05-05 10:22:40 +08002177static void *rs_alloc_sta(void *priv_rate, gfp_t gfp)
Zhu Yib481de92007-09-25 17:54:57 -07002178{
Tomas Winklerc33104f2008-01-14 17:46:21 -08002179 struct iwl4965_lq_sta *lq_sta;
Ester Kummerbf403db2008-05-05 10:22:40 +08002180 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07002181 int i, j;
2182
Ester Kummerbf403db2008-05-05 10:22:40 +08002183 priv = (struct iwl_priv *)priv_rate;
Zhu Yib481de92007-09-25 17:54:57 -07002184 IWL_DEBUG_RATE("create station rate scale window\n");
2185
Tomas Winklerc33104f2008-01-14 17:46:21 -08002186 lq_sta = kzalloc(sizeof(struct iwl4965_lq_sta), gfp);
Zhu Yib481de92007-09-25 17:54:57 -07002187
Tomas Winklerc33104f2008-01-14 17:46:21 -08002188 if (lq_sta == NULL)
Zhu Yib481de92007-09-25 17:54:57 -07002189 return NULL;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002190 lq_sta->lq.sta_id = 0xff;
Zhu Yib481de92007-09-25 17:54:57 -07002191
Zhu Yi63fddb92007-09-27 11:27:36 +08002192
Zhu Yib481de92007-09-25 17:54:57 -07002193 for (j = 0; j < LQ_SIZE; j++)
2194 for (i = 0; i < IWL_RATE_COUNT; i++)
Tomas Winklerc33104f2008-01-14 17:46:21 -08002195 rs_rate_scale_clear_window(&(lq_sta->lq_info[j].win[i]));
Zhu Yib481de92007-09-25 17:54:57 -07002196
Tomas Winklerc33104f2008-01-14 17:46:21 -08002197 return lq_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002198}
2199
2200static void rs_rate_init(void *priv_rate, void *priv_sta,
2201 struct ieee80211_local *local,
2202 struct sta_info *sta)
2203{
2204 int i, j;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002205 struct ieee80211_conf *conf = &local->hw.conf;
Johannes Berg8318d782008-01-24 19:38:38 +01002206 struct ieee80211_supported_band *sband;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002207 struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002208 struct iwl4965_lq_sta *lq_sta = priv_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002209
Johannes Berg8318d782008-01-24 19:38:38 +01002210 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2211
Tomas Winklerc33104f2008-01-14 17:46:21 -08002212 lq_sta->flush_timer = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01002213 lq_sta->supp_rates = sta->supp_rates[sband->band];
2214 sta->txrate_idx = 3;
Zhu Yib481de92007-09-25 17:54:57 -07002215 for (j = 0; j < LQ_SIZE; j++)
2216 for (i = 0; i < IWL_RATE_COUNT; i++)
Tomas Winklerc33104f2008-01-14 17:46:21 -08002217 rs_rate_scale_clear_window(&(lq_sta->lq_info[j].win[i]));
Zhu Yib481de92007-09-25 17:54:57 -07002218
Guy Cohen07bc28e2008-04-23 17:15:03 -07002219 IWL_DEBUG_RATE("LQ: *** rate scale global init ***\n");
Zhu Yib481de92007-09-25 17:54:57 -07002220 /* TODO: what is a good starting rate for STA? About middle? Maybe not
2221 * the lowest or the highest rate.. Could consider using RSSI from
2222 * previous packets? Need to have IEEE 802.1X auth succeed immediately
2223 * after assoc.. */
2224
Tomas Winklerc33104f2008-01-14 17:46:21 -08002225 lq_sta->ibss_sta_added = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002226 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
Tomas Winkler947b13a2008-04-16 16:34:48 -07002227 u8 sta_id = iwl_find_station(priv, sta->addr);
Joe Perches0795af52007-10-03 17:59:30 -07002228 DECLARE_MAC_BUF(mac);
2229
Zhu Yib481de92007-09-25 17:54:57 -07002230 /* for IBSS the call are from tasklet */
Guy Coheneecd6e52008-04-23 17:14:58 -07002231 IWL_DEBUG_RATE("LQ: ADD station %s\n",
Joe Perches0795af52007-10-03 17:59:30 -07002232 print_mac(mac, sta->addr));
Zhu Yib481de92007-09-25 17:54:57 -07002233
2234 if (sta_id == IWL_INVALID_STATION) {
Joe Perches0795af52007-10-03 17:59:30 -07002235 IWL_DEBUG_RATE("LQ: ADD station %s\n",
2236 print_mac(mac, sta->addr));
Ron Rindjunsky67d62032007-11-26 16:14:40 +02002237 sta_id = iwl4965_add_station_flags(priv, sta->addr,
2238 0, CMD_ASYNC, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07002239 }
2240 if ((sta_id != IWL_INVALID_STATION)) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08002241 lq_sta->lq.sta_id = sta_id;
2242 lq_sta->lq.rs_table[0].rate_n_flags = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002243 }
2244 /* FIXME: this is w/a remove it later */
2245 priv->assoc_station_added = 1;
2246 }
2247
Ben Cahill77626352007-11-29 11:09:44 +08002248 /* Find highest tx rate supported by hardware and destination station */
Johannes Berg8318d782008-01-24 19:38:38 +01002249 for (i = 0; i < sband->n_bitrates; i++)
2250 if (sta->supp_rates[sband->band] & BIT(i))
2251 sta->txrate_idx = i;
2252
2253 sta->last_txrate_idx = sta->txrate_idx;
2254 /* WTF is with this bogus comment? A doesn't have cck rates */
Ben Cahill77626352007-11-29 11:09:44 +08002255 /* For MODE_IEEE80211A, cck rates are at end of rate table */
Johannes Berg8318d782008-01-24 19:38:38 +01002256 if (local->hw.conf.channel->band == IEEE80211_BAND_5GHZ)
2257 sta->last_txrate_idx += IWL_FIRST_OFDM_RATE;
Zhu Yib481de92007-09-25 17:54:57 -07002258
Tomas Winklerc33104f2008-01-14 17:46:21 -08002259 lq_sta->is_dup = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002260 lq_sta->is_green = rs_use_green(priv, conf);
Guy Cohen39e88502008-04-23 17:14:57 -07002261 lq_sta->active_legacy_rate = priv->active_rate & ~(0x1000);
Tomas Winklerc33104f2008-01-14 17:46:21 -08002262 lq_sta->active_rate_basic = priv->active_rate_basic;
Johannes Berg8318d782008-01-24 19:38:38 +01002263 lq_sta->band = priv->band;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002264#ifdef CONFIG_IWL4965_HT
Ben Cahill77626352007-11-29 11:09:44 +08002265 /*
2266 * active_siso_rate mask includes 9 MBits (bit 5), and CCK (bits 0-3),
2267 * supp_rates[] does not; shift to convert format, force 9 MBits off.
2268 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002269 lq_sta->active_siso_rate =
Guy Cohenfde0db32008-04-21 15:42:01 -07002270 priv->current_ht_config.supp_mcs_set[0] << 1;
2271 lq_sta->active_siso_rate |=
2272 priv->current_ht_config.supp_mcs_set[0] & 0x1;
2273 lq_sta->active_siso_rate &= ~((u16)0x2);
2274 lq_sta->active_siso_rate <<= IWL_FIRST_OFDM_RATE;
Zhu Yib481de92007-09-25 17:54:57 -07002275
Ben Cahill77626352007-11-29 11:09:44 +08002276 /* Same here */
Guy Cohenfde0db32008-04-21 15:42:01 -07002277 lq_sta->active_mimo2_rate =
2278 priv->current_ht_config.supp_mcs_set[1] << 1;
2279 lq_sta->active_mimo2_rate |=
2280 priv->current_ht_config.supp_mcs_set[1] & 0x1;
2281 lq_sta->active_mimo2_rate &= ~((u16)0x2);
2282 lq_sta->active_mimo2_rate <<= IWL_FIRST_OFDM_RATE;
2283
2284 lq_sta->active_mimo3_rate =
2285 priv->current_ht_config.supp_mcs_set[2] << 1;
2286 lq_sta->active_mimo3_rate |=
2287 priv->current_ht_config.supp_mcs_set[2] & 0x1;
2288 lq_sta->active_mimo3_rate &= ~((u16)0x2);
2289 lq_sta->active_mimo3_rate <<= IWL_FIRST_OFDM_RATE;
2290
Guy Cohen07bc28e2008-04-23 17:15:03 -07002291 IWL_DEBUG_RATE("SISO-RATE=%X MIMO2-RATE=%X MIMO3-RATE=%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002292 lq_sta->active_siso_rate,
Guy Cohenfde0db32008-04-21 15:42:01 -07002293 lq_sta->active_mimo2_rate,
2294 lq_sta->active_mimo3_rate);
2295
Guy Cohen07bc28e2008-04-23 17:15:03 -07002296 /* These values will be overriden later */
2297 lq_sta->lq.general_params.single_stream_ant_msk = ANT_A;
2298 lq_sta->lq.general_params.dual_stream_ant_msk = ANT_AB;
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 Cohen07bc28e2008-04-23 17:15:03 -07002315 u32 new_rate)
Zhu Yib481de92007-09-25 17:54:57 -07002316{
Guy Cohenaade00c2008-04-23 17:14:59 -07002317 struct iwl4965_scale_tbl_info tbl_type;
Zhu Yib481de92007-09-25 17:54:57 -07002318 int index = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002319 int rate_idx;
Zhu Yi1b696de2007-09-27 11:27:41 +08002320 int repeat_rate = 0;
Guy Cohenaade00c2008-04-23 17:14:59 -07002321 u8 ant_toggle_cnt = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002322 u8 use_ht_possible = 1;
Guy Cohenaade00c2008-04-23 17:14:59 -07002323 u8 valid_tx_ant = 0;
Guy Cohen07bc28e2008-04-23 17:15:03 -07002324 struct iwl_link_quality_cmd *lq_cmd = &lq_sta->lq;
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 Cohen07bc28e2008-04-23 17:15:03 -07002348 if (num_of_ant(tbl_type.ant_type) == 1) {
2349 lq_cmd->general_params.single_stream_ant_msk =
2350 tbl_type.ant_type;
2351 } else if (num_of_ant(tbl_type.ant_type) == 2) {
2352 lq_cmd->general_params.dual_stream_ant_msk =
2353 tbl_type.ant_type;
2354 } /* otherwise we don't modify the existing value */
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->agg_params.agg_dis_start_th = 3;
2429 lq_cmd->agg_params.agg_time_limit = cpu_to_le16(4000);
Zhu Yib481de92007-09-25 17:54:57 -07002430}
2431
2432static void *rs_alloc(struct ieee80211_local *local)
2433{
2434 return local->hw.priv;
2435}
2436/* rate scale requires free function to be implemented */
2437static void rs_free(void *priv_rate)
2438{
2439 return;
2440}
2441
2442static void rs_clear(void *priv_rate)
2443{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002444 struct iwl_priv *priv = (struct iwl_priv *) priv_rate;
Zhu Yib481de92007-09-25 17:54:57 -07002445
2446 IWL_DEBUG_RATE("enter\n");
2447
2448 priv->lq_mngr.lq_ready = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002449
2450 IWL_DEBUG_RATE("leave\n");
2451}
2452
Ester Kummerbf403db2008-05-05 10:22:40 +08002453static void rs_free_sta(void *priv_rate, void *priv_sta)
Zhu Yib481de92007-09-25 17:54:57 -07002454{
Tomas Winklerc33104f2008-01-14 17:46:21 -08002455 struct iwl4965_lq_sta *lq_sta = priv_sta;
Ester Kummerbf403db2008-05-05 10:22:40 +08002456 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07002457
Ester Kummerbf403db2008-05-05 10:22:40 +08002458 priv = (struct iwl_priv *)priv_rate;
Zhu Yib481de92007-09-25 17:54:57 -07002459 IWL_DEBUG_RATE("enter\n");
Tomas Winklerc33104f2008-01-14 17:46:21 -08002460 kfree(lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002461 IWL_DEBUG_RATE("leave\n");
2462}
2463
2464
Zhu Yi93dc6462007-09-27 11:27:37 +08002465#ifdef CONFIG_MAC80211_DEBUGFS
Zhu Yi5ae212c2007-09-27 11:27:38 +08002466static int open_file_generic(struct inode *inode, struct file *file)
2467{
2468 file->private_data = inode->i_private;
2469 return 0;
2470}
Tomas Winklerc33104f2008-01-14 17:46:21 -08002471static void rs_dbgfs_set_mcs(struct iwl4965_lq_sta *lq_sta,
Guy Cohen39e88502008-04-23 17:14:57 -07002472 u32 *rate_n_flags, int index)
Zhu Yi98d7e092007-09-27 11:27:42 +08002473{
Ester Kummerbf403db2008-05-05 10:22:40 +08002474 struct iwl_priv *priv;
2475
2476 priv = lq_sta->drv;
Guy Cohen39e88502008-04-23 17:14:57 -07002477 if (lq_sta->dbg_fixed_rate) {
2478 if (index < 12) {
2479 *rate_n_flags = lq_sta->dbg_fixed_rate;
2480 } else {
2481 if (lq_sta->band == IEEE80211_BAND_5GHZ)
2482 *rate_n_flags = 0x800D;
2483 else
2484 *rate_n_flags = 0x820A;
2485 }
Zhu Yi98d7e092007-09-27 11:27:42 +08002486 IWL_DEBUG_RATE("Fixed rate ON\n");
Guy Cohen39e88502008-04-23 17:14:57 -07002487 } else {
2488 IWL_DEBUG_RATE("Fixed rate OFF\n");
Zhu Yi98d7e092007-09-27 11:27:42 +08002489 }
Zhu Yi98d7e092007-09-27 11:27:42 +08002490}
2491
2492static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
2493 const char __user *user_buf, size_t count, loff_t *ppos)
2494{
Tomas Winklerc33104f2008-01-14 17:46:21 -08002495 struct iwl4965_lq_sta *lq_sta = file->private_data;
Ester Kummerbf403db2008-05-05 10:22:40 +08002496 struct iwl_priv *priv;
Zhu Yi98d7e092007-09-27 11:27:42 +08002497 char buf[64];
2498 int buf_size;
2499 u32 parsed_rate;
2500
Ester Kummerbf403db2008-05-05 10:22:40 +08002501 priv = lq_sta->drv;
Zhu Yi98d7e092007-09-27 11:27:42 +08002502 memset(buf, 0, sizeof(buf));
2503 buf_size = min(count, sizeof(buf) - 1);
2504 if (copy_from_user(buf, user_buf, buf_size))
2505 return -EFAULT;
2506
2507 if (sscanf(buf, "%x", &parsed_rate) == 1)
Guy Cohen39e88502008-04-23 17:14:57 -07002508 lq_sta->dbg_fixed_rate = parsed_rate;
Zhu Yi98d7e092007-09-27 11:27:42 +08002509 else
Guy Cohen39e88502008-04-23 17:14:57 -07002510 lq_sta->dbg_fixed_rate = 0;
Zhu Yi98d7e092007-09-27 11:27:42 +08002511
Guy Cohen39e88502008-04-23 17:14:57 -07002512 lq_sta->active_legacy_rate = 0x0FFF; /* 1 - 54 MBits, includes CCK */
2513 lq_sta->active_siso_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2514 lq_sta->active_mimo2_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2515 lq_sta->active_mimo3_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
Zhu Yi98d7e092007-09-27 11:27:42 +08002516
2517 IWL_DEBUG_RATE("sta_id %d rate 0x%X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07002518 lq_sta->lq.sta_id, lq_sta->dbg_fixed_rate);
Zhu Yi98d7e092007-09-27 11:27:42 +08002519
Guy Cohen39e88502008-04-23 17:14:57 -07002520 if (lq_sta->dbg_fixed_rate) {
Guy Cohen07bc28e2008-04-23 17:15:03 -07002521 rs_fill_link_cmd(NULL, lq_sta, lq_sta->dbg_fixed_rate);
Tomas Winkler66c73db2008-04-15 16:01:40 -07002522 iwl_send_lq_cmd(lq_sta->drv, &lq_sta->lq, CMD_ASYNC);
Zhu Yi98d7e092007-09-27 11:27:42 +08002523 }
2524
2525 return count;
2526}
Zhu Yi0209dc12007-09-27 11:27:43 +08002527
Zhu Yi5ae212c2007-09-27 11:27:38 +08002528static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
2529 char __user *user_buf, size_t count, loff_t *ppos)
2530{
2531 char buff[1024];
2532 int desc = 0;
2533 int i = 0;
2534
Tomas Winklerc33104f2008-01-14 17:46:21 -08002535 struct iwl4965_lq_sta *lq_sta = file->private_data;
Zhu Yi5ae212c2007-09-27 11:27:38 +08002536
Tomas Winklerc33104f2008-01-14 17:46:21 -08002537 desc += sprintf(buff+desc, "sta_id %d\n", lq_sta->lq.sta_id);
Zhu Yi98d7e092007-09-27 11:27:42 +08002538 desc += sprintf(buff+desc, "failed=%d success=%d rate=0%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002539 lq_sta->total_failed, lq_sta->total_success,
Guy Cohen39e88502008-04-23 17:14:57 -07002540 lq_sta->active_legacy_rate);
Zhu Yi98d7e092007-09-27 11:27:42 +08002541 desc += sprintf(buff+desc, "fixed rate 0x%X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07002542 lq_sta->dbg_fixed_rate);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002543 desc += sprintf(buff+desc, "general:"
2544 "flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002545 lq_sta->lq.general_params.flags,
2546 lq_sta->lq.general_params.mimo_delimiter,
2547 lq_sta->lq.general_params.single_stream_ant_msk,
2548 lq_sta->lq.general_params.dual_stream_ant_msk);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002549
2550 desc += sprintf(buff+desc, "agg:"
2551 "time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002552 le16_to_cpu(lq_sta->lq.agg_params.agg_time_limit),
2553 lq_sta->lq.agg_params.agg_dis_start_th,
2554 lq_sta->lq.agg_params.agg_frame_cnt_limit);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002555
2556 desc += sprintf(buff+desc,
2557 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002558 lq_sta->lq.general_params.start_rate_index[0],
2559 lq_sta->lq.general_params.start_rate_index[1],
2560 lq_sta->lq.general_params.start_rate_index[2],
2561 lq_sta->lq.general_params.start_rate_index[3]);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002562
2563
2564 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
2565 desc += sprintf(buff+desc, " rate[%d] 0x%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002566 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags));
Zhu Yi5ae212c2007-09-27 11:27:38 +08002567
2568 return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2569}
2570
2571static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
Zhu Yi98d7e092007-09-27 11:27:42 +08002572 .write = rs_sta_dbgfs_scale_table_write,
Zhu Yi5ae212c2007-09-27 11:27:38 +08002573 .read = rs_sta_dbgfs_scale_table_read,
2574 .open = open_file_generic,
2575};
Zhu Yi0209dc12007-09-27 11:27:43 +08002576static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
2577 char __user *user_buf, size_t count, loff_t *ppos)
2578{
2579 char buff[1024];
2580 int desc = 0;
2581 int i, j;
2582
Tomas Winklerc33104f2008-01-14 17:46:21 -08002583 struct iwl4965_lq_sta *lq_sta = file->private_data;
Zhu Yi0209dc12007-09-27 11:27:43 +08002584 for (i = 0; i < LQ_SIZE; i++) {
2585 desc += sprintf(buff+desc, "%s type=%d SGI=%d FAT=%d DUP=%d\n"
2586 "rate=0x%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002587 lq_sta->active_tbl == i?"*":"x",
2588 lq_sta->lq_info[i].lq_type,
2589 lq_sta->lq_info[i].is_SGI,
2590 lq_sta->lq_info[i].is_fat,
2591 lq_sta->lq_info[i].is_dup,
Guy Cohen39e88502008-04-23 17:14:57 -07002592 lq_sta->lq_info[i].current_rate);
Zhu Yi0209dc12007-09-27 11:27:43 +08002593 for (j = 0; j < IWL_RATE_COUNT; j++) {
2594 desc += sprintf(buff+desc,
Tomas Winklerc33104f2008-01-14 17:46:21 -08002595 "counter=%d success=%d %%=%d\n",
2596 lq_sta->lq_info[i].win[j].counter,
2597 lq_sta->lq_info[i].win[j].success_counter,
2598 lq_sta->lq_info[i].win[j].success_ratio);
Zhu Yi0209dc12007-09-27 11:27:43 +08002599 }
2600 }
2601 return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2602}
2603
2604static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
2605 .read = rs_sta_dbgfs_stats_table_read,
2606 .open = open_file_generic,
2607};
Zhu Yi5ae212c2007-09-27 11:27:38 +08002608
Zhu Yi93dc6462007-09-27 11:27:37 +08002609static void rs_add_debugfs(void *priv, void *priv_sta,
2610 struct dentry *dir)
2611{
Tomas Winklerc33104f2008-01-14 17:46:21 -08002612 struct iwl4965_lq_sta *lq_sta = priv_sta;
2613 lq_sta->rs_sta_dbgfs_scale_table_file =
Zhu Yi0209dc12007-09-27 11:27:43 +08002614 debugfs_create_file("rate_scale_table", 0600, dir,
Tomas Winklerc33104f2008-01-14 17:46:21 -08002615 lq_sta, &rs_sta_dbgfs_scale_table_ops);
2616 lq_sta->rs_sta_dbgfs_stats_table_file =
Zhu Yi0209dc12007-09-27 11:27:43 +08002617 debugfs_create_file("rate_stats_table", 0600, dir,
Tomas Winklerc33104f2008-01-14 17:46:21 -08002618 lq_sta, &rs_sta_dbgfs_stats_table_ops);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002619#ifdef CONFIG_IWL4965_HT
2620 lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file =
2621 debugfs_create_u8("tx_agg_tid_enable", 0600, dir,
2622 &lq_sta->tx_agg_tid_en);
2623#endif
2624
Zhu Yi93dc6462007-09-27 11:27:37 +08002625}
2626
2627static void rs_remove_debugfs(void *priv, void *priv_sta)
2628{
Tomas Winklerc33104f2008-01-14 17:46:21 -08002629 struct iwl4965_lq_sta *lq_sta = priv_sta;
2630 debugfs_remove(lq_sta->rs_sta_dbgfs_scale_table_file);
2631 debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002632#ifdef CONFIG_IWL4965_HT
2633 debugfs_remove(lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file);
2634#endif
Zhu Yi93dc6462007-09-27 11:27:37 +08002635}
2636#endif
2637
Zhu Yib481de92007-09-25 17:54:57 -07002638static struct rate_control_ops rs_ops = {
2639 .module = NULL,
2640 .name = RS_NAME,
2641 .tx_status = rs_tx_status,
2642 .get_rate = rs_get_rate,
2643 .rate_init = rs_rate_init,
2644 .clear = rs_clear,
2645 .alloc = rs_alloc,
2646 .free = rs_free,
2647 .alloc_sta = rs_alloc_sta,
2648 .free_sta = rs_free_sta,
Zhu Yi93dc6462007-09-27 11:27:37 +08002649#ifdef CONFIG_MAC80211_DEBUGFS
2650 .add_sta_debugfs = rs_add_debugfs,
2651 .remove_sta_debugfs = rs_remove_debugfs,
2652#endif
Zhu Yib481de92007-09-25 17:54:57 -07002653};
2654
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002655int iwl4965_fill_rs_info(struct ieee80211_hw *hw, char *buf, u8 sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07002656{
2657 struct ieee80211_local *local = hw_to_local(hw);
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002658 struct iwl_priv *priv = hw->priv;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002659 struct iwl4965_lq_sta *lq_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002660 struct sta_info *sta;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002661 int cnt = 0, i;
Zhu Yib481de92007-09-25 17:54:57 -07002662 u32 samples = 0, success = 0, good = 0;
2663 unsigned long now = jiffies;
2664 u32 max_time = 0;
2665 u8 lq_type, antenna;
2666
Johannes Bergd0709a62008-02-25 16:27:46 +01002667 rcu_read_lock();
2668
Zhu Yib481de92007-09-25 17:54:57 -07002669 sta = sta_info_get(local, priv->stations[sta_id].sta.sta.addr);
2670 if (!sta || !sta->rate_ctrl_priv) {
Johannes Bergd0709a62008-02-25 16:27:46 +01002671 if (sta)
Zhu Yib481de92007-09-25 17:54:57 -07002672 IWL_DEBUG_RATE("leave - no private rate data!\n");
Johannes Bergd0709a62008-02-25 16:27:46 +01002673 else
Zhu Yib481de92007-09-25 17:54:57 -07002674 IWL_DEBUG_RATE("leave - no station!\n");
Johannes Bergd0709a62008-02-25 16:27:46 +01002675 rcu_read_unlock();
Zhu Yib481de92007-09-25 17:54:57 -07002676 return sprintf(buf, "station %d not found\n", sta_id);
2677 }
2678
Tomas Winklerc33104f2008-01-14 17:46:21 -08002679 lq_sta = (void *)sta->rate_ctrl_priv;
Zhu Yib481de92007-09-25 17:54:57 -07002680
Tomas Winklerc33104f2008-01-14 17:46:21 -08002681 lq_type = lq_sta->lq_info[lq_sta->active_tbl].lq_type;
Guy Cohenfde0db32008-04-21 15:42:01 -07002682 antenna = lq_sta->lq_info[lq_sta->active_tbl].ant_type;
Zhu Yib481de92007-09-25 17:54:57 -07002683
2684 if (is_legacy(lq_type))
2685 i = IWL_RATE_54M_INDEX;
2686 else
2687 i = IWL_RATE_60M_INDEX;
2688 while (1) {
2689 u64 mask;
2690 int j;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002691 int active = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002692
Tomas Winklerc33104f2008-01-14 17:46:21 -08002693 cnt +=
2694 sprintf(&buf[cnt], " %2dMbs: ", iwl4965_rates[i].ieee / 2);
Zhu Yib481de92007-09-25 17:54:57 -07002695
2696 mask = (1ULL << (IWL_RATE_MAX_WINDOW - 1));
2697 for (j = 0; j < IWL_RATE_MAX_WINDOW; j++, mask >>= 1)
Tomas Winklerc33104f2008-01-14 17:46:21 -08002698 buf[cnt++] =
2699 (lq_sta->lq_info[active].win[i].data & mask)
Zhu Yib481de92007-09-25 17:54:57 -07002700 ? '1' : '0';
2701
Tomas Winklerc33104f2008-01-14 17:46:21 -08002702 samples += lq_sta->lq_info[active].win[i].counter;
2703 good += lq_sta->lq_info[active].win[i].success_counter;
2704 success += lq_sta->lq_info[active].win[i].success_counter *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002705 iwl4965_rates[i].ieee;
Zhu Yib481de92007-09-25 17:54:57 -07002706
Tomas Winklerc33104f2008-01-14 17:46:21 -08002707 if (lq_sta->lq_info[active].win[i].stamp) {
Zhu Yib481de92007-09-25 17:54:57 -07002708 int delta =
2709 jiffies_to_msecs(now -
Tomas Winklerc33104f2008-01-14 17:46:21 -08002710 lq_sta->lq_info[active].win[i].stamp);
Zhu Yib481de92007-09-25 17:54:57 -07002711
2712 if (delta > max_time)
2713 max_time = delta;
2714
Tomas Winklerc33104f2008-01-14 17:46:21 -08002715 cnt += sprintf(&buf[cnt], "%5dms\n", delta);
Zhu Yib481de92007-09-25 17:54:57 -07002716 } else
Tomas Winklerc33104f2008-01-14 17:46:21 -08002717 buf[cnt++] = '\n';
Zhu Yib481de92007-09-25 17:54:57 -07002718
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002719 j = iwl4965_get_prev_ieee_rate(i);
Zhu Yib481de92007-09-25 17:54:57 -07002720 if (j == i)
2721 break;
2722 i = j;
2723 }
2724
2725 /* Display the average rate of all samples taken.
2726 *
Ben Cahill77626352007-11-29 11:09:44 +08002727 * NOTE: We multiply # of samples by 2 since the IEEE measurement
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002728 * added from iwl4965_rates is actually 2X the rate */
Zhu Yib481de92007-09-25 17:54:57 -07002729 if (samples)
Tomas Winklerc33104f2008-01-14 17:46:21 -08002730 cnt += sprintf(&buf[cnt],
Zhu Yib481de92007-09-25 17:54:57 -07002731 "\nAverage rate is %3d.%02dMbs over last %4dms\n"
2732 "%3d%% success (%d good packets over %d tries)\n",
2733 success / (2 * samples), (success * 5 / samples) % 10,
2734 max_time, good * 100 / samples, good, samples);
2735 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08002736 cnt += sprintf(&buf[cnt], "\nAverage rate: 0Mbs\n");
2737
2738 cnt += sprintf(&buf[cnt], "\nrate scale type %d antenna %d "
Zhu Yib481de92007-09-25 17:54:57 -07002739 "active_search %d rate index %d\n", lq_type, antenna,
Johannes Berg8318d782008-01-24 19:38:38 +01002740 lq_sta->search_better_tbl, sta->last_txrate_idx);
Zhu Yib481de92007-09-25 17:54:57 -07002741
Johannes Bergd0709a62008-02-25 16:27:46 +01002742 rcu_read_unlock();
Tomas Winklerc33104f2008-01-14 17:46:21 -08002743 return cnt;
Zhu Yib481de92007-09-25 17:54:57 -07002744}
2745
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002746void iwl4965_rate_scale_init(struct ieee80211_hw *hw, s32 sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07002747{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002748 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07002749
2750 priv->lq_mngr.lq_ready = 1;
2751}
2752
Reinette Chatre897e1cf2008-03-28 16:21:09 -07002753int iwl4965_rate_control_register(void)
Zhu Yib481de92007-09-25 17:54:57 -07002754{
Reinette Chatre897e1cf2008-03-28 16:21:09 -07002755 return ieee80211_rate_control_register(&rs_ops);
Zhu Yib481de92007-09-25 17:54:57 -07002756}
2757
Reinette Chatre897e1cf2008-03-28 16:21:09 -07002758void iwl4965_rate_control_unregister(void)
Zhu Yib481de92007-09-25 17:54:57 -07002759{
2760 ieee80211_rate_control_unregister(&rs_ops);
2761}
2762