blob: e3444c4ea1a50aaee88ac91776625d70007816a4 [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;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700171 struct iwl_priv *drv;
Zhu Yi5ae212c2007-09-27 11:27:38 +0800172#endif
Zhu Yib481de92007-09-25 17:54:57 -0700173};
174
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700175static void rs_rate_scale_perform(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -0700176 struct net_device *dev,
177 struct ieee80211_hdr *hdr,
178 struct sta_info *sta);
Guy Cohenfde0db32008-04-21 15:42:01 -0700179static void rs_fill_link_cmd(const struct iwl_priv *priv,
180 struct iwl4965_lq_sta *lq_sta,
Guy 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
665static u16 rs_get_adjacent_rate(u8 index, u16 rate_mask, int rate_type)
666{
667 u8 high = IWL_RATE_INVALID;
668 u8 low = IWL_RATE_INVALID;
669
Ian Schram01ebd062007-10-25 17:15:22 +0800670 /* 802.11A or ht walks to the next literal adjacent rate in
Zhu Yib481de92007-09-25 17:54:57 -0700671 * the rate table */
672 if (is_a_band(rate_type) || !is_legacy(rate_type)) {
673 int i;
674 u32 mask;
675
676 /* Find the previous rate that is in the rate mask */
677 i = index - 1;
678 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
679 if (rate_mask & mask) {
680 low = i;
681 break;
682 }
683 }
684
685 /* Find the next rate that is in the rate mask */
686 i = index + 1;
687 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
688 if (rate_mask & mask) {
689 high = i;
690 break;
691 }
692 }
693
694 return (high << 8) | low;
695 }
696
697 low = index;
698 while (low != IWL_RATE_INVALID) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800699 low = iwl4965_rates[low].prev_rs;
Zhu Yib481de92007-09-25 17:54:57 -0700700 if (low == IWL_RATE_INVALID)
701 break;
702 if (rate_mask & (1 << low))
703 break;
704 IWL_DEBUG_RATE("Skipping masked lower rate: %d\n", low);
705 }
706
707 high = index;
708 while (high != IWL_RATE_INVALID) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800709 high = iwl4965_rates[high].next_rs;
Zhu Yib481de92007-09-25 17:54:57 -0700710 if (high == IWL_RATE_INVALID)
711 break;
712 if (rate_mask & (1 << high))
713 break;
714 IWL_DEBUG_RATE("Skipping masked higher rate: %d\n", high);
715 }
716
717 return (high << 8) | low;
718}
719
Guy Cohen39e88502008-04-23 17:14:57 -0700720static u32 rs_get_lower_rate(struct iwl4965_lq_sta *lq_sta,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800721 struct iwl4965_scale_tbl_info *tbl, u8 scale_index,
Guy Cohen39e88502008-04-23 17:14:57 -0700722 u8 ht_possible)
Zhu Yib481de92007-09-25 17:54:57 -0700723{
Zhu Yib481de92007-09-25 17:54:57 -0700724 s32 low;
725 u16 rate_mask;
726 u16 high_low;
727 u8 switch_to_legacy = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -0800728 u8 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -0700729
730 /* check if we need to switch from HT to legacy rates.
731 * assumption is that mandatory rates (1Mbps or 6Mbps)
732 * are always supported (spec demand) */
733 if (!is_legacy(tbl->lq_type) && (!ht_possible || !scale_index)) {
734 switch_to_legacy = 1;
735 scale_index = rs_ht_to_legacy[scale_index];
Johannes Berg8318d782008-01-24 19:38:38 +0100736 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700737 tbl->lq_type = LQ_A;
738 else
739 tbl->lq_type = LQ_G;
740
Guy Cohen69fdb302008-04-23 17:15:01 -0700741 if (num_of_ant(tbl->ant_type) > 1)
Guy Cohenfde0db32008-04-21 15:42:01 -0700742 tbl->ant_type = ANT_A;/*FIXME:RS*/
Zhu Yib481de92007-09-25 17:54:57 -0700743
744 tbl->is_fat = 0;
745 tbl->is_SGI = 0;
746 }
747
Guy Coheneecd6e52008-04-23 17:14:58 -0700748 rate_mask = rs_get_supported_rates(lq_sta, NULL, tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -0700749
Ben Cahill77626352007-11-29 11:09:44 +0800750 /* Mask with station rate restriction */
Zhu Yib481de92007-09-25 17:54:57 -0700751 if (is_legacy(tbl->lq_type)) {
Ben Cahill77626352007-11-29 11:09:44 +0800752 /* supp_rates has no CCK bits in A mode */
Johannes Berg8318d782008-01-24 19:38:38 +0100753 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700754 rate_mask = (u16)(rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -0800755 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
Zhu Yib481de92007-09-25 17:54:57 -0700756 else
Tomas Winklerc33104f2008-01-14 17:46:21 -0800757 rate_mask = (u16)(rate_mask & lq_sta->supp_rates);
Zhu Yib481de92007-09-25 17:54:57 -0700758 }
759
Ben Cahill77626352007-11-29 11:09:44 +0800760 /* If we switched from HT to legacy, check current rate */
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800761 if (switch_to_legacy && (rate_mask & (1 << scale_index))) {
Guy Cohen39e88502008-04-23 17:14:57 -0700762 low = scale_index;
763 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700764 }
765
766 high_low = rs_get_adjacent_rate(scale_index, rate_mask, tbl->lq_type);
767 low = high_low & 0xff;
768
Guy Cohen39e88502008-04-23 17:14:57 -0700769 if (low == IWL_RATE_INVALID)
770 low = scale_index;
771
772out:
773 return rate_n_flags_from_tbl(tbl, low, is_green);
Zhu Yib481de92007-09-25 17:54:57 -0700774}
775
Ben Cahill77626352007-11-29 11:09:44 +0800776/*
777 * mac80211 sends us Tx status
778 */
Tomas Winklerc33104f2008-01-14 17:46:21 -0800779static void rs_tx_status(void *priv_rate, struct net_device *dev,
Zhu Yib481de92007-09-25 17:54:57 -0700780 struct sk_buff *skb,
781 struct ieee80211_tx_status *tx_resp)
782{
783 int status;
784 u8 retries;
785 int rs_index, index = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -0800786 struct iwl4965_lq_sta *lq_sta;
Tomas Winkler66c73db2008-04-15 16:01:40 -0700787 struct iwl_link_quality_cmd *table;
Zhu Yib481de92007-09-25 17:54:57 -0700788 struct sta_info *sta;
789 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700790 struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
Zhu Yib481de92007-09-25 17:54:57 -0700791 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800792 struct ieee80211_hw *hw = local_to_hw(local);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800793 struct iwl4965_rate_scale_data *window = NULL;
794 struct iwl4965_rate_scale_data *search_win = NULL;
Guy Cohen39e88502008-04-23 17:14:57 -0700795 u32 tx_rate;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800796 struct iwl4965_scale_tbl_info tbl_type;
797 struct iwl4965_scale_tbl_info *curr_tbl, *search_tbl;
Zhu Yib481de92007-09-25 17:54:57 -0700798 u8 active_index = 0;
799 u16 fc = le16_to_cpu(hdr->frame_control);
800 s32 tpt = 0;
801
Zhu Yi58826352007-09-27 11:27:39 +0800802 IWL_DEBUG_RATE_LIMIT("get frame ack response, update rate scale window\n");
Zhu Yib481de92007-09-25 17:54:57 -0700803
804 if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1))
805 return;
806
Ron Rindjunsky99556432008-01-28 14:07:25 +0200807 /* This packet was aggregated but doesn't carry rate scale info */
808 if ((tx_resp->control.flags & IEEE80211_TXCTL_AMPDU) &&
809 !(tx_resp->flags & IEEE80211_TX_STATUS_AMPDU))
810 return;
811
Zhu Yib481de92007-09-25 17:54:57 -0700812 retries = tx_resp->retry_count;
813
814 if (retries > 15)
815 retries = 15;
816
Johannes Bergd0709a62008-02-25 16:27:46 +0100817 rcu_read_lock();
Zhu Yib481de92007-09-25 17:54:57 -0700818
819 sta = sta_info_get(local, hdr->addr1);
820
Tomas Winklerf4d60822008-03-05 11:31:00 -0800821 if (!sta || !sta->rate_ctrl_priv)
822 goto out;
823
Zhu Yib481de92007-09-25 17:54:57 -0700824
Tomas Winklerc33104f2008-01-14 17:46:21 -0800825 lq_sta = (struct iwl4965_lq_sta *)sta->rate_ctrl_priv;
Zhu Yib481de92007-09-25 17:54:57 -0700826
827 if (!priv->lq_mngr.lq_ready)
Tomas Winklerf4d60822008-03-05 11:31:00 -0800828 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700829
Tomas Winklerc33104f2008-01-14 17:46:21 -0800830 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
831 !lq_sta->ibss_sta_added)
Tomas Winklerf4d60822008-03-05 11:31:00 -0800832 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700833
Tomas Winklerc33104f2008-01-14 17:46:21 -0800834 table = &lq_sta->lq;
835 active_index = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -0700836
Tomas Winklerc33104f2008-01-14 17:46:21 -0800837 curr_tbl = &(lq_sta->lq_info[active_index]);
838 search_tbl = &(lq_sta->lq_info[(1 - active_index)]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800839 window = (struct iwl4965_rate_scale_data *)
Zhu Yib481de92007-09-25 17:54:57 -0700840 &(curr_tbl->win[0]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800841 search_win = (struct iwl4965_rate_scale_data *)
Zhu Yib481de92007-09-25 17:54:57 -0700842 &(search_tbl->win[0]);
843
Ben Cahill77626352007-11-29 11:09:44 +0800844 /*
845 * Ignore this Tx frame response if its initial rate doesn't match
846 * that of latest Link Quality command. There may be stragglers
847 * from a previous Link Quality command, but we're no longer interested
848 * in those; they're either from the "active" mode while we're trying
849 * to check "search" mode, or a prior "search" mode after we've moved
850 * to a new "search" mode (which might become the new "active" mode).
851 */
Guy Cohen39e88502008-04-23 17:14:57 -0700852 tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
853 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type, &rs_index);
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800854 if (priv->band == IEEE80211_BAND_5GHZ)
855 rs_index -= IWL_FIRST_OFDM_RATE;
856
857 if ((tx_resp->control.tx_rate == NULL) ||
858 (tbl_type.is_SGI ^
859 !!(tx_resp->control.flags & IEEE80211_TXCTL_SHORT_GI)) ||
860 (tbl_type.is_fat ^
861 !!(tx_resp->control.flags & IEEE80211_TXCTL_40_MHZ_WIDTH)) ||
862 (tbl_type.is_dup ^
863 !!(tx_resp->control.flags & IEEE80211_TXCTL_DUP_DATA)) ||
Guy Cohenfde0db32008-04-21 15:42:01 -0700864 (tbl_type.ant_type ^ tx_resp->control.antenna_sel_tx) ||
Guy Cohen39e88502008-04-23 17:14:57 -0700865 (!!(tx_rate & RATE_MCS_HT_MSK) ^
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800866 !!(tx_resp->control.flags & IEEE80211_TXCTL_OFDM_HT)) ||
Guy Cohen39e88502008-04-23 17:14:57 -0700867 (!!(tx_rate & RATE_MCS_GF_MSK) ^
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800868 !!(tx_resp->control.flags & IEEE80211_TXCTL_GREEN_FIELD)) ||
869 (hw->wiphy->bands[priv->band]->bitrates[rs_index].bitrate !=
870 tx_resp->control.tx_rate->bitrate)) {
Guy Cohen39e88502008-04-23 17:14:57 -0700871 IWL_DEBUG_RATE("initial rate does not match 0x%x\n", tx_rate);
Tomas Winklerf4d60822008-03-05 11:31:00 -0800872 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700873 }
874
Ben Cahill77626352007-11-29 11:09:44 +0800875 /* Update frame history window with "failure" for each Tx retry. */
Zhu Yib481de92007-09-25 17:54:57 -0700876 while (retries) {
Ben Cahill77626352007-11-29 11:09:44 +0800877 /* Look up the rate and other info used for each tx attempt.
878 * Each tx attempt steps one entry deeper in the rate table. */
Guy Cohen39e88502008-04-23 17:14:57 -0700879 tx_rate = le32_to_cpu(table->rs_table[index].rate_n_flags);
880 rs_get_tbl_info_from_mcs(tx_rate, priv->band,
Zhu Yib481de92007-09-25 17:54:57 -0700881 &tbl_type, &rs_index);
882
Ben Cahill77626352007-11-29 11:09:44 +0800883 /* If type matches "search" table,
884 * add failure to "search" history */
Zhu Yib481de92007-09-25 17:54:57 -0700885 if ((tbl_type.lq_type == search_tbl->lq_type) &&
Guy Cohenfde0db32008-04-21 15:42:01 -0700886 (tbl_type.ant_type == search_tbl->ant_type) &&
Zhu Yib481de92007-09-25 17:54:57 -0700887 (tbl_type.is_SGI == search_tbl->is_SGI)) {
888 if (search_tbl->expected_tpt)
889 tpt = search_tbl->expected_tpt[rs_index];
890 else
891 tpt = 0;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200892 rs_collect_tx_data(search_win, rs_index, tpt, 1, 0);
Ben Cahill77626352007-11-29 11:09:44 +0800893
894 /* Else if type matches "current/active" table,
895 * add failure to "current/active" history */
Zhu Yib481de92007-09-25 17:54:57 -0700896 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
Guy Cohenfde0db32008-04-21 15:42:01 -0700897 (tbl_type.ant_type == curr_tbl->ant_type) &&
Zhu Yib481de92007-09-25 17:54:57 -0700898 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
899 if (curr_tbl->expected_tpt)
900 tpt = curr_tbl->expected_tpt[rs_index];
901 else
902 tpt = 0;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200903 rs_collect_tx_data(window, rs_index, tpt, 1, 0);
Zhu Yib481de92007-09-25 17:54:57 -0700904 }
Ben Cahill77626352007-11-29 11:09:44 +0800905
906 /* If not searching for a new mode, increment failed counter
907 * ... this helps determine when to start searching again */
Tomas Winklerc33104f2008-01-14 17:46:21 -0800908 if (lq_sta->stay_in_tbl)
909 lq_sta->total_failed++;
Zhu Yib481de92007-09-25 17:54:57 -0700910 --retries;
911 index++;
912
913 }
914
Ben Cahill77626352007-11-29 11:09:44 +0800915 /*
916 * Find (by rate) the history window to update with final Tx attempt;
917 * if Tx was successful first try, use original rate,
918 * else look up the rate that was, finally, successful.
919 */
Guy Cohen39e88502008-04-23 17:14:57 -0700920 tx_rate = le32_to_cpu(table->rs_table[index].rate_n_flags);
921 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type, &rs_index);
Zhu Yib481de92007-09-25 17:54:57 -0700922
Ben Cahill77626352007-11-29 11:09:44 +0800923 /* Update frame history window with "success" if Tx got ACKed ... */
Zhu Yib481de92007-09-25 17:54:57 -0700924 if (tx_resp->flags & IEEE80211_TX_STATUS_ACK)
925 status = 1;
926 else
927 status = 0;
928
Ben Cahill77626352007-11-29 11:09:44 +0800929 /* If type matches "search" table,
930 * add final tx status to "search" history */
Zhu Yib481de92007-09-25 17:54:57 -0700931 if ((tbl_type.lq_type == search_tbl->lq_type) &&
Guy Cohenfde0db32008-04-21 15:42:01 -0700932 (tbl_type.ant_type == search_tbl->ant_type) &&
Zhu Yib481de92007-09-25 17:54:57 -0700933 (tbl_type.is_SGI == search_tbl->is_SGI)) {
934 if (search_tbl->expected_tpt)
935 tpt = search_tbl->expected_tpt[rs_index];
936 else
937 tpt = 0;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200938 if (tx_resp->control.flags & IEEE80211_TXCTL_AMPDU)
939 rs_collect_tx_data(search_win, rs_index, tpt,
940 tx_resp->ampdu_ack_len,
941 tx_resp->ampdu_ack_map);
942 else
943 rs_collect_tx_data(search_win, rs_index, tpt,
944 1, status);
Ben Cahill77626352007-11-29 11:09:44 +0800945 /* Else if type matches "current/active" table,
946 * add final tx status to "current/active" history */
Zhu Yib481de92007-09-25 17:54:57 -0700947 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
Guy Cohenfde0db32008-04-21 15:42:01 -0700948 (tbl_type.ant_type == curr_tbl->ant_type) &&
Zhu Yib481de92007-09-25 17:54:57 -0700949 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
950 if (curr_tbl->expected_tpt)
951 tpt = curr_tbl->expected_tpt[rs_index];
952 else
953 tpt = 0;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200954 if (tx_resp->control.flags & IEEE80211_TXCTL_AMPDU)
955 rs_collect_tx_data(window, rs_index, tpt,
956 tx_resp->ampdu_ack_len,
957 tx_resp->ampdu_ack_map);
958 else
959 rs_collect_tx_data(window, rs_index, tpt,
960 1, status);
Zhu Yib481de92007-09-25 17:54:57 -0700961 }
962
Ben Cahill77626352007-11-29 11:09:44 +0800963 /* If not searching for new mode, increment success/failed counter
964 * ... these help determine when to start searching again */
Tomas Winklerc33104f2008-01-14 17:46:21 -0800965 if (lq_sta->stay_in_tbl) {
Ron Rindjunsky99556432008-01-28 14:07:25 +0200966 if (tx_resp->control.flags & IEEE80211_TXCTL_AMPDU) {
967 lq_sta->total_success += tx_resp->ampdu_ack_map;
968 lq_sta->total_failed +=
969 (tx_resp->ampdu_ack_len - tx_resp->ampdu_ack_map);
970 } else {
971 if (status)
972 lq_sta->total_success++;
973 else
974 lq_sta->total_failed++;
975 }
Zhu Yib481de92007-09-25 17:54:57 -0700976 }
977
Ben Cahill77626352007-11-29 11:09:44 +0800978 /* See if there's a better rate or modulation mode to try. */
Zhu Yib481de92007-09-25 17:54:57 -0700979 rs_rate_scale_perform(priv, dev, hdr, sta);
Tomas Winklerf4d60822008-03-05 11:31:00 -0800980out:
Johannes Bergd0709a62008-02-25 16:27:46 +0100981 rcu_read_unlock();
Zhu Yib481de92007-09-25 17:54:57 -0700982 return;
983}
984
Ben Cahill77626352007-11-29 11:09:44 +0800985/*
986 * Begin a period of staying with a selected modulation mode.
987 * Set "stay_in_tbl" flag to prevent any mode switches.
988 * Set frame tx success limits according to legacy vs. high-throughput,
989 * and reset overall (spanning all rates) tx success history statistics.
990 * These control how long we stay using same modulation mode before
991 * searching for a new mode.
992 */
Zhu Yib481de92007-09-25 17:54:57 -0700993static void rs_set_stay_in_table(u8 is_legacy,
Tomas Winklerc33104f2008-01-14 17:46:21 -0800994 struct iwl4965_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -0700995{
Guy Coheneecd6e52008-04-23 17:14:58 -0700996 IWL_DEBUG_RATE("we are staying in the same table\n");
Tomas Winklerc33104f2008-01-14 17:46:21 -0800997 lq_sta->stay_in_tbl = 1; /* only place this gets set */
Zhu Yib481de92007-09-25 17:54:57 -0700998 if (is_legacy) {
Tomas Winklerc33104f2008-01-14 17:46:21 -0800999 lq_sta->table_count_limit = IWL_LEGACY_TABLE_COUNT;
1000 lq_sta->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
1001 lq_sta->max_success_limit = IWL_LEGACY_SUCCESS_LIMIT;
Zhu Yib481de92007-09-25 17:54:57 -07001002 } else {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001003 lq_sta->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
1004 lq_sta->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
1005 lq_sta->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
Zhu Yib481de92007-09-25 17:54:57 -07001006 }
Tomas Winklerc33104f2008-01-14 17:46:21 -08001007 lq_sta->table_count = 0;
1008 lq_sta->total_failed = 0;
1009 lq_sta->total_success = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001010}
1011
Ben Cahill77626352007-11-29 11:09:44 +08001012/*
1013 * Find correct throughput table for given mode of modulation
1014 */
Guy Coheneecd6e52008-04-23 17:14:58 -07001015static void rs_set_expected_tpt_table(struct iwl4965_lq_sta *lq_sta,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001016 struct iwl4965_scale_tbl_info *tbl)
Zhu Yib481de92007-09-25 17:54:57 -07001017{
1018 if (is_legacy(tbl->lq_type)) {
1019 if (!is_a_band(tbl->lq_type))
1020 tbl->expected_tpt = expected_tpt_G;
1021 else
1022 tbl->expected_tpt = expected_tpt_A;
1023 } else if (is_siso(tbl->lq_type)) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001024 if (tbl->is_fat && !lq_sta->is_dup)
Zhu Yib481de92007-09-25 17:54:57 -07001025 if (tbl->is_SGI)
1026 tbl->expected_tpt = expected_tpt_siso40MHzSGI;
1027 else
1028 tbl->expected_tpt = expected_tpt_siso40MHz;
1029 else if (tbl->is_SGI)
1030 tbl->expected_tpt = expected_tpt_siso20MHzSGI;
1031 else
1032 tbl->expected_tpt = expected_tpt_siso20MHz;
1033
Guy Cohenfde0db32008-04-21 15:42:01 -07001034 } else if (is_mimo(tbl->lq_type)) { /* FIXME:need to separate mimo2/3 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001035 if (tbl->is_fat && !lq_sta->is_dup)
Zhu Yib481de92007-09-25 17:54:57 -07001036 if (tbl->is_SGI)
1037 tbl->expected_tpt = expected_tpt_mimo40MHzSGI;
1038 else
1039 tbl->expected_tpt = expected_tpt_mimo40MHz;
1040 else if (tbl->is_SGI)
1041 tbl->expected_tpt = expected_tpt_mimo20MHzSGI;
1042 else
1043 tbl->expected_tpt = expected_tpt_mimo20MHz;
1044 } else
1045 tbl->expected_tpt = expected_tpt_G;
1046}
1047
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001048#ifdef CONFIG_IWL4965_HT
Ben Cahill77626352007-11-29 11:09:44 +08001049/*
1050 * Find starting rate for new "search" high-throughput mode of modulation.
1051 * Goal is to find lowest expected rate (under perfect conditions) that is
1052 * above the current measured throughput of "active" mode, to give new mode
1053 * a fair chance to prove itself without too many challenges.
1054 *
1055 * This gets called when transitioning to more aggressive modulation
1056 * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
1057 * (i.e. MIMO to SISO). When moving to MIMO, bit rate will typically need
1058 * to decrease to match "active" throughput. When moving from MIMO to SISO,
1059 * bit rate will typically need to increase, but not if performance was bad.
1060 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001061static s32 rs_get_best_rate(struct iwl_priv *priv,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001062 struct iwl4965_lq_sta *lq_sta,
Ben Cahill77626352007-11-29 11:09:44 +08001063 struct iwl4965_scale_tbl_info *tbl, /* "search" */
Guy Cohenf935a6d2008-04-23 17:15:02 -07001064 u16 rate_mask, s8 index)
Zhu Yib481de92007-09-25 17:54:57 -07001065{
Ben Cahill77626352007-11-29 11:09:44 +08001066 /* "active" values */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001067 struct iwl4965_scale_tbl_info *active_tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001068 &(lq_sta->lq_info[lq_sta->active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07001069 s32 active_sr = active_tbl->win[index].success_ratio;
Zhu Yib481de92007-09-25 17:54:57 -07001070 s32 active_tpt = active_tbl->expected_tpt[index];
Ben Cahill77626352007-11-29 11:09:44 +08001071
1072 /* expected "search" throughput */
1073 s32 *tpt_tbl = tbl->expected_tpt;
1074
1075 s32 new_rate, high, low, start_hi;
Zhu Yib481de92007-09-25 17:54:57 -07001076 u16 high_low;
Guy Cohenf935a6d2008-04-23 17:15:02 -07001077 s8 rate = index;
Zhu Yib481de92007-09-25 17:54:57 -07001078
1079 new_rate = high = low = start_hi = IWL_RATE_INVALID;
1080
1081 for (; ;) {
1082 high_low = rs_get_adjacent_rate(rate, rate_mask, tbl->lq_type);
1083
1084 low = high_low & 0xff;
1085 high = (high_low >> 8) & 0xff;
1086
Ben Cahill77626352007-11-29 11:09:44 +08001087 /*
1088 * Lower the "search" bit rate, to give new "search" mode
1089 * approximately the same throughput as "active" if:
1090 *
1091 * 1) "Active" mode has been working modestly well (but not
1092 * great), and expected "search" throughput (under perfect
1093 * conditions) at candidate rate is above the actual
1094 * measured "active" throughput (but less than expected
1095 * "active" throughput under perfect conditions).
1096 * OR
1097 * 2) "Active" mode has been working perfectly or very well
1098 * and expected "search" throughput (under perfect
1099 * conditions) at candidate rate is above expected
1100 * "active" throughput (under perfect conditions).
1101 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001102 if ((((100 * tpt_tbl[rate]) > lq_sta->last_tpt) &&
Zhu Yib481de92007-09-25 17:54:57 -07001103 ((active_sr > IWL_RATE_DECREASE_TH) &&
1104 (active_sr <= IWL_RATE_HIGH_TH) &&
1105 (tpt_tbl[rate] <= active_tpt))) ||
1106 ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
1107 (tpt_tbl[rate] > active_tpt))) {
1108
Ben Cahill77626352007-11-29 11:09:44 +08001109 /* (2nd or later pass)
1110 * If we've already tried to raise the rate, and are
1111 * now trying to lower it, use the higher rate. */
Zhu Yib481de92007-09-25 17:54:57 -07001112 if (start_hi != IWL_RATE_INVALID) {
1113 new_rate = start_hi;
1114 break;
1115 }
Ben Cahill77626352007-11-29 11:09:44 +08001116
Zhu Yib481de92007-09-25 17:54:57 -07001117 new_rate = rate;
Ben Cahill77626352007-11-29 11:09:44 +08001118
1119 /* Loop again with lower rate */
Zhu Yib481de92007-09-25 17:54:57 -07001120 if (low != IWL_RATE_INVALID)
1121 rate = low;
Ben Cahill77626352007-11-29 11:09:44 +08001122
1123 /* Lower rate not available, use the original */
Zhu Yib481de92007-09-25 17:54:57 -07001124 else
1125 break;
Ben Cahill77626352007-11-29 11:09:44 +08001126
1127 /* Else try to raise the "search" rate to match "active" */
Zhu Yib481de92007-09-25 17:54:57 -07001128 } else {
Ben Cahill77626352007-11-29 11:09:44 +08001129 /* (2nd or later pass)
1130 * If we've already tried to lower the rate, and are
1131 * now trying to raise it, use the lower rate. */
Zhu Yib481de92007-09-25 17:54:57 -07001132 if (new_rate != IWL_RATE_INVALID)
1133 break;
Ben Cahill77626352007-11-29 11:09:44 +08001134
1135 /* Loop again with higher rate */
Zhu Yib481de92007-09-25 17:54:57 -07001136 else if (high != IWL_RATE_INVALID) {
1137 start_hi = high;
1138 rate = high;
Ben Cahill77626352007-11-29 11:09:44 +08001139
1140 /* Higher rate not available, use the original */
Zhu Yib481de92007-09-25 17:54:57 -07001141 } else {
1142 new_rate = rate;
1143 break;
1144 }
1145 }
1146 }
1147
1148 return new_rate;
1149}
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001150#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001151
Ben Cahill77626352007-11-29 11:09:44 +08001152/*
1153 * Set up search table for MIMO
1154 */
Guy Cohenfde0db32008-04-21 15:42:01 -07001155#ifdef CONFIG_IWL4965_HT
1156static int rs_switch_to_mimo2(struct iwl_priv *priv,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001157 struct iwl4965_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001158 struct ieee80211_conf *conf,
1159 struct sta_info *sta,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001160 struct iwl4965_scale_tbl_info *tbl, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001161{
Zhu Yib481de92007-09-25 17:54:57 -07001162 u16 rate_mask;
1163 s32 rate;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001164 s8 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07001165
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001166 if (!(conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) ||
1167 !sta->ht_info.ht_supported)
Zhu Yib481de92007-09-25 17:54:57 -07001168 return -1;
1169
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001170 if (priv->current_ht_config.tx_mimo_ps_mode == IWL_MIMO_PS_STATIC)
Zhu Yib481de92007-09-25 17:54:57 -07001171 return -1;
1172
Ben Cahill77626352007-11-29 11:09:44 +08001173 /* Need both Tx chains/antennas to support MIMO */
Guy Cohenaade00c2008-04-23 17:14:59 -07001174 if (priv->hw_params.tx_chains_num < 2)
Zhu Yib481de92007-09-25 17:54:57 -07001175 return -1;
1176
Guy Coheneecd6e52008-04-23 17:14:58 -07001177 IWL_DEBUG_RATE("LQ: try to switch to MIMO2\n");
Guy Cohen39e88502008-04-23 17:14:57 -07001178
1179 tbl->lq_type = LQ_MIMO2;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001180 tbl->is_dup = lq_sta->is_dup;
Zhu Yib481de92007-09-25 17:54:57 -07001181 tbl->action = 0;
Guy Cohen39e88502008-04-23 17:14:57 -07001182 rate_mask = lq_sta->active_mimo2_rate;
1183
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001184 if (priv->current_ht_config.supported_chan_width
Guy Cohen39e88502008-04-23 17:14:57 -07001185 == IWL_CHANNEL_WIDTH_40MHZ)
Zhu Yib481de92007-09-25 17:54:57 -07001186 tbl->is_fat = 1;
1187 else
1188 tbl->is_fat = 0;
1189
Guy Cohen39e88502008-04-23 17:14:57 -07001190 /* FIXME: - don't toggle SGI here
Zhu Yib481de92007-09-25 17:54:57 -07001191 if (tbl->is_fat) {
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001192 if (priv->current_ht_config.sgf & HT_SHORT_GI_40MHZ_ONLY)
Zhu Yib481de92007-09-25 17:54:57 -07001193 tbl->is_SGI = 1;
1194 else
1195 tbl->is_SGI = 0;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001196 } else if (priv->current_ht_config.sgf & HT_SHORT_GI_20MHZ_ONLY)
Zhu Yib481de92007-09-25 17:54:57 -07001197 tbl->is_SGI = 1;
1198 else
1199 tbl->is_SGI = 0;
Guy Cohen39e88502008-04-23 17:14:57 -07001200 */
Zhu Yib481de92007-09-25 17:54:57 -07001201
Guy Coheneecd6e52008-04-23 17:14:58 -07001202 rs_set_expected_tpt_table(lq_sta, tbl);
Zhu Yib481de92007-09-25 17:54:57 -07001203
Guy Cohenf935a6d2008-04-23 17:15:02 -07001204 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
Zhu Yib481de92007-09-25 17:54:57 -07001205
Guy Coheneecd6e52008-04-23 17:14:58 -07001206 IWL_DEBUG_RATE("LQ: MIMO2 best rate %d mask %X\n", rate, rate_mask);
Guy Cohen39e88502008-04-23 17:14:57 -07001207
1208 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
Guy Coheneecd6e52008-04-23 17:14:58 -07001209 IWL_DEBUG_RATE("Can't switch with index %d rate mask %x\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001210 rate, rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -07001211 return -1;
Guy Cohen39e88502008-04-23 17:14:57 -07001212 }
1213 tbl->current_rate = rate_n_flags_from_tbl(tbl, rate, is_green);
Zhu Yib481de92007-09-25 17:54:57 -07001214
Guy Coheneecd6e52008-04-23 17:14:58 -07001215 IWL_DEBUG_RATE("LQ: Switch to new mcs %X index is green %X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001216 tbl->current_rate, is_green);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001217 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07001218}
Guy Cohenfde0db32008-04-21 15:42:01 -07001219#else
1220static int rs_switch_to_mimo2(struct iwl_priv *priv,
1221 struct iwl4965_lq_sta *lq_sta,
1222 struct ieee80211_conf *conf,
1223 struct sta_info *sta,
1224 struct iwl4965_scale_tbl_info *tbl, int index)
1225{
1226 return -1;
1227}
1228#endif /*CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001229
Ben Cahill77626352007-11-29 11:09:44 +08001230/*
1231 * Set up search table for SISO
1232 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001233static int rs_switch_to_siso(struct iwl_priv *priv,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001234 struct iwl4965_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001235 struct ieee80211_conf *conf,
1236 struct sta_info *sta,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001237 struct iwl4965_scale_tbl_info *tbl, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001238{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001239#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07001240 u16 rate_mask;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001241 u8 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07001242 s32 rate;
1243
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001244 if (!(conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) ||
1245 !sta->ht_info.ht_supported)
Zhu Yib481de92007-09-25 17:54:57 -07001246 return -1;
1247
Guy Coheneecd6e52008-04-23 17:14:58 -07001248 IWL_DEBUG_RATE("LQ: try to switch to SISO\n");
Guy Cohen39e88502008-04-23 17:14:57 -07001249
Tomas Winklerc33104f2008-01-14 17:46:21 -08001250 tbl->is_dup = lq_sta->is_dup;
Zhu Yib481de92007-09-25 17:54:57 -07001251 tbl->lq_type = LQ_SISO;
1252 tbl->action = 0;
Guy Cohen39e88502008-04-23 17:14:57 -07001253 rate_mask = lq_sta->active_siso_rate;
Zhu Yib481de92007-09-25 17:54:57 -07001254
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001255 if (priv->current_ht_config.supported_chan_width
1256 == IWL_CHANNEL_WIDTH_40MHZ)
Zhu Yib481de92007-09-25 17:54:57 -07001257 tbl->is_fat = 1;
1258 else
1259 tbl->is_fat = 0;
1260
Guy Cohen39e88502008-04-23 17:14:57 -07001261 /* FIXME: - don't toggle SGI here
Zhu Yib481de92007-09-25 17:54:57 -07001262 if (tbl->is_fat) {
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001263 if (priv->current_ht_config.sgf & HT_SHORT_GI_40MHZ_ONLY)
Zhu Yib481de92007-09-25 17:54:57 -07001264 tbl->is_SGI = 1;
1265 else
1266 tbl->is_SGI = 0;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001267 } else if (priv->current_ht_config.sgf & HT_SHORT_GI_20MHZ_ONLY)
Zhu Yib481de92007-09-25 17:54:57 -07001268 tbl->is_SGI = 1;
1269 else
1270 tbl->is_SGI = 0;
Guy Cohen39e88502008-04-23 17:14:57 -07001271 */
Zhu Yib481de92007-09-25 17:54:57 -07001272
1273 if (is_green)
Guy Cohen39e88502008-04-23 17:14:57 -07001274 tbl->is_SGI = 0; /*11n spec: no SGI in SISO+Greenfield*/
Zhu Yib481de92007-09-25 17:54:57 -07001275
Guy Coheneecd6e52008-04-23 17:14:58 -07001276 rs_set_expected_tpt_table(lq_sta, tbl);
Guy Cohenf935a6d2008-04-23 17:15:02 -07001277 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index);
Zhu Yib481de92007-09-25 17:54:57 -07001278
Guy Coheneecd6e52008-04-23 17:14:58 -07001279 IWL_DEBUG_RATE("LQ: get best rate %d mask %X\n", rate, rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -07001280 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
Guy Coheneecd6e52008-04-23 17:14:58 -07001281 IWL_DEBUG_RATE("can not switch with index %d rate mask %x\n",
Zhu Yib481de92007-09-25 17:54:57 -07001282 rate, rate_mask);
1283 return -1;
1284 }
Guy Cohen39e88502008-04-23 17:14:57 -07001285 tbl->current_rate = rate_n_flags_from_tbl(tbl, rate, is_green);
Guy Coheneecd6e52008-04-23 17:14:58 -07001286 IWL_DEBUG_RATE("LQ: Switch to new mcs %X index is green %X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001287 tbl->current_rate, is_green);
Mohamed Abbas403ab562007-11-06 22:06:25 -08001288 return 0;
1289#else
1290 return -1;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001291#endif /*CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001292}
1293
Ben Cahill77626352007-11-29 11:09:44 +08001294/*
1295 * Try to switch to new modulation mode from legacy
1296 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001297static int rs_move_legacy_other(struct iwl_priv *priv,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001298 struct iwl4965_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001299 struct ieee80211_conf *conf,
1300 struct sta_info *sta,
Zhu Yib481de92007-09-25 17:54:57 -07001301 int index)
1302{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001303 struct iwl4965_scale_tbl_info *tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001304 &(lq_sta->lq_info[lq_sta->active_tbl]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001305 struct iwl4965_scale_tbl_info *search_tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001306 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001307 struct iwl4965_rate_scale_data *window = &(tbl->win[index]);
1308 u32 sz = (sizeof(struct iwl4965_scale_tbl_info) -
1309 (sizeof(struct iwl4965_rate_scale_data) * IWL_RATE_COUNT));
Zhu Yib481de92007-09-25 17:54:57 -07001310 u8 start_action = tbl->action;
Guy Cohenfde0db32008-04-21 15:42:01 -07001311 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1312 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001313
1314 for (; ;) {
1315 switch (tbl->action) {
1316 case IWL_LEGACY_SWITCH_ANTENNA:
Guy Cohenf935a6d2008-04-23 17:15:02 -07001317 IWL_DEBUG_RATE("LQ: Legacy toggle Antenna\n");
Zhu Yib481de92007-09-25 17:54:57 -07001318
Tomas Winklerc33104f2008-01-14 17:46:21 -08001319 lq_sta->action_counter++;
Ben Cahill77626352007-11-29 11:09:44 +08001320
1321 /* Don't change antenna if success has been great */
Zhu Yib481de92007-09-25 17:54:57 -07001322 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1323 break;
Ben Cahill77626352007-11-29 11:09:44 +08001324
Ben Cahill77626352007-11-29 11:09:44 +08001325 /* Set up search table to try other antenna */
Zhu Yib481de92007-09-25 17:54:57 -07001326 memcpy(search_tbl, tbl, sz);
1327
Guy Cohenaade00c2008-04-23 17:14:59 -07001328 if (rs_toggle_antenna(valid_tx_ant,
1329 &search_tbl->current_rate, search_tbl)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07001330 lq_sta->search_better_tbl = 1;
1331 goto out;
1332 }
Zhu Yib481de92007-09-25 17:54:57 -07001333
1334 case IWL_LEGACY_SWITCH_SISO:
Guy Coheneecd6e52008-04-23 17:14:58 -07001335 IWL_DEBUG_RATE("LQ: Legacy switch to SISO\n");
Ben Cahill77626352007-11-29 11:09:44 +08001336
1337 /* Set up search table to try SISO */
Zhu Yib481de92007-09-25 17:54:57 -07001338 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001339 search_tbl->is_SGI = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001340 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001341 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001342 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001343 lq_sta->search_better_tbl = 1;
1344 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001345 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001346 }
Zhu Yib481de92007-09-25 17:54:57 -07001347
1348 break;
Guy Cohenfde0db32008-04-21 15:42:01 -07001349 case IWL_LEGACY_SWITCH_MIMO2:
Guy Coheneecd6e52008-04-23 17:14:58 -07001350 IWL_DEBUG_RATE("LQ: Legacy switch to MIMO2\n");
Ben Cahill77626352007-11-29 11:09:44 +08001351
1352 /* Set up search table to try MIMO */
Zhu Yib481de92007-09-25 17:54:57 -07001353 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001354 search_tbl->is_SGI = 0;
Guy Cohenfde0db32008-04-21 15:42:01 -07001355 search_tbl->ant_type = ANT_AB;/*FIXME:RS*/
1356 /*FIXME:RS:need to check ant validity*/
1357 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001358 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001359 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001360 lq_sta->search_better_tbl = 1;
1361 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001362 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001363 }
Zhu Yib481de92007-09-25 17:54:57 -07001364 break;
1365 }
1366 tbl->action++;
Guy Cohenfde0db32008-04-21 15:42:01 -07001367 if (tbl->action > IWL_LEGACY_SWITCH_MIMO2)
Zhu Yib481de92007-09-25 17:54:57 -07001368 tbl->action = IWL_LEGACY_SWITCH_ANTENNA;
1369
1370 if (tbl->action == start_action)
1371 break;
1372
1373 }
1374 return 0;
1375
1376 out:
1377 tbl->action++;
Guy Cohenfde0db32008-04-21 15:42:01 -07001378 if (tbl->action > IWL_LEGACY_SWITCH_MIMO2)
Zhu Yib481de92007-09-25 17:54:57 -07001379 tbl->action = IWL_LEGACY_SWITCH_ANTENNA;
1380 return 0;
1381
1382}
1383
Ben Cahill77626352007-11-29 11:09:44 +08001384/*
1385 * Try to switch to new modulation mode from SISO
1386 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001387static int rs_move_siso_to_other(struct iwl_priv *priv,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001388 struct iwl4965_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001389 struct ieee80211_conf *conf,
1390 struct sta_info *sta,
Zhu Yib481de92007-09-25 17:54:57 -07001391 int index)
1392{
Tomas Winklerc33104f2008-01-14 17:46:21 -08001393 u8 is_green = lq_sta->is_green;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001394 struct iwl4965_scale_tbl_info *tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001395 &(lq_sta->lq_info[lq_sta->active_tbl]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001396 struct iwl4965_scale_tbl_info *search_tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001397 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001398 struct iwl4965_rate_scale_data *window = &(tbl->win[index]);
1399 u32 sz = (sizeof(struct iwl4965_scale_tbl_info) -
1400 (sizeof(struct iwl4965_rate_scale_data) * IWL_RATE_COUNT));
Zhu Yib481de92007-09-25 17:54:57 -07001401 u8 start_action = tbl->action;
Guy Cohenfde0db32008-04-21 15:42:01 -07001402 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1403 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001404
1405 for (;;) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001406 lq_sta->action_counter++;
Zhu Yib481de92007-09-25 17:54:57 -07001407 switch (tbl->action) {
1408 case IWL_SISO_SWITCH_ANTENNA:
Guy Coheneecd6e52008-04-23 17:14:58 -07001409 IWL_DEBUG_RATE("LQ: SISO toggle Antenna\n");
Zhu Yib481de92007-09-25 17:54:57 -07001410 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1411 break;
Zhu Yib481de92007-09-25 17:54:57 -07001412
1413 memcpy(search_tbl, tbl, sz);
Guy Cohenaade00c2008-04-23 17:14:59 -07001414 if (rs_toggle_antenna(valid_tx_ant,
1415 &search_tbl->current_rate, search_tbl)) {
1416 lq_sta->search_better_tbl = 1;
1417 goto out;
1418 }
Zhu Yib481de92007-09-25 17:54:57 -07001419
Guy Cohenfde0db32008-04-21 15:42:01 -07001420 case IWL_SISO_SWITCH_MIMO2:
Guy Coheneecd6e52008-04-23 17:14:58 -07001421 IWL_DEBUG_RATE("LQ: SISO switch to MIMO\n");
Zhu Yib481de92007-09-25 17:54:57 -07001422 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001423 search_tbl->is_SGI = 0;
Guy Cohenfde0db32008-04-21 15:42:01 -07001424 search_tbl->ant_type = ANT_AB; /*FIXME:RS*/
1425 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001426 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001427 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001428 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001429 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001430 }
Zhu Yib481de92007-09-25 17:54:57 -07001431 break;
1432 case IWL_SISO_SWITCH_GI:
Guy Coheneecd6e52008-04-23 17:14:58 -07001433 IWL_DEBUG_RATE("LQ: SISO toggle SGI/NGI\n");
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001434
Zhu Yib481de92007-09-25 17:54:57 -07001435 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001436 if (is_green) {
1437 if (!tbl->is_SGI)
1438 break;
1439 else
1440 IWL_ERROR("SGI was set in GF+SISO\n");
Zhu Yib481de92007-09-25 17:54:57 -07001441 }
Guy Cohen39e88502008-04-23 17:14:57 -07001442 search_tbl->is_SGI = !tbl->is_SGI;
Guy Coheneecd6e52008-04-23 17:14:58 -07001443 rs_set_expected_tpt_table(lq_sta, search_tbl);
Guy Cohen39e88502008-04-23 17:14:57 -07001444 if (tbl->is_SGI) {
1445 s32 tpt = lq_sta->last_tpt / 100;
1446 if (tpt >= search_tbl->expected_tpt[index])
1447 break;
1448 }
1449 search_tbl->current_rate = rate_n_flags_from_tbl(
1450 search_tbl, index, is_green);
1451 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001452 goto out;
1453 }
1454 tbl->action++;
1455 if (tbl->action > IWL_SISO_SWITCH_GI)
1456 tbl->action = IWL_SISO_SWITCH_ANTENNA;
1457
1458 if (tbl->action == start_action)
1459 break;
1460 }
1461 return 0;
1462
1463 out:
1464 tbl->action++;
1465 if (tbl->action > IWL_SISO_SWITCH_GI)
1466 tbl->action = IWL_SISO_SWITCH_ANTENNA;
1467 return 0;
1468}
1469
Ben Cahill77626352007-11-29 11:09:44 +08001470/*
1471 * Try to switch to new modulation mode from MIMO
1472 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001473static int rs_move_mimo_to_other(struct iwl_priv *priv,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001474 struct iwl4965_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001475 struct ieee80211_conf *conf,
1476 struct sta_info *sta,
Zhu Yib481de92007-09-25 17:54:57 -07001477 int index)
1478{
Tomas Winklerc33104f2008-01-14 17:46:21 -08001479 s8 is_green = lq_sta->is_green;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001480 struct iwl4965_scale_tbl_info *tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001481 &(lq_sta->lq_info[lq_sta->active_tbl]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001482 struct iwl4965_scale_tbl_info *search_tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001483 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001484 u32 sz = (sizeof(struct iwl4965_scale_tbl_info) -
1485 (sizeof(struct iwl4965_rate_scale_data) * IWL_RATE_COUNT));
Zhu Yib481de92007-09-25 17:54:57 -07001486 u8 start_action = tbl->action;
Guy Cohenaade00c2008-04-23 17:14:59 -07001487 /*u8 valid_tx_ant = priv->hw_params.valid_tx_ant;*/
1488 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001489
1490 for (;;) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001491 lq_sta->action_counter++;
Zhu Yib481de92007-09-25 17:54:57 -07001492 switch (tbl->action) {
1493 case IWL_MIMO_SWITCH_ANTENNA_A:
1494 case IWL_MIMO_SWITCH_ANTENNA_B:
Guy Coheneecd6e52008-04-23 17:14:58 -07001495 IWL_DEBUG_RATE("LQ: MIMO2 switch to SISO\n");
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001496
Ben Cahill77626352007-11-29 11:09:44 +08001497 /* Set up new search table for SISO */
Zhu Yib481de92007-09-25 17:54:57 -07001498 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001499
1500 /*FIXME:RS:need to check ant validity + C*/
Zhu Yib481de92007-09-25 17:54:57 -07001501 if (tbl->action == IWL_MIMO_SWITCH_ANTENNA_A)
Guy Cohenfde0db32008-04-21 15:42:01 -07001502 search_tbl->ant_type = ANT_A;
Zhu Yib481de92007-09-25 17:54:57 -07001503 else
Guy Cohenfde0db32008-04-21 15:42:01 -07001504 search_tbl->ant_type = ANT_B;
Zhu Yib481de92007-09-25 17:54:57 -07001505
Tomas Winklerc33104f2008-01-14 17:46:21 -08001506 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001507 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001508 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001509 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001510 goto out;
1511 }
1512 break;
1513
1514 case IWL_MIMO_SWITCH_GI:
Guy Coheneecd6e52008-04-23 17:14:58 -07001515 IWL_DEBUG_RATE("LQ: MIMO toggle SGI/NGI\n");
Ben Cahill77626352007-11-29 11:09:44 +08001516
1517 /* Set up new search table for MIMO */
Zhu Yib481de92007-09-25 17:54:57 -07001518 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001519 search_tbl->is_SGI = !tbl->is_SGI;
Guy Coheneecd6e52008-04-23 17:14:58 -07001520 rs_set_expected_tpt_table(lq_sta, search_tbl);
Ben Cahill77626352007-11-29 11:09:44 +08001521 /*
1522 * If active table already uses the fastest possible
1523 * modulation (dual stream with short guard interval),
1524 * and it's working well, there's no need to look
1525 * for a better type of modulation!
1526 */
Guy Cohen39e88502008-04-23 17:14:57 -07001527 if (tbl->is_SGI) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001528 s32 tpt = lq_sta->last_tpt / 100;
Guy Cohen39e88502008-04-23 17:14:57 -07001529 if (tpt >= search_tbl->expected_tpt[index])
1530 break;
Zhu Yib481de92007-09-25 17:54:57 -07001531 }
Guy Cohen39e88502008-04-23 17:14:57 -07001532 search_tbl->current_rate = rate_n_flags_from_tbl(
1533 search_tbl, index, is_green);
1534 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001535 goto out;
1536
1537 }
1538 tbl->action++;
1539 if (tbl->action > IWL_MIMO_SWITCH_GI)
1540 tbl->action = IWL_MIMO_SWITCH_ANTENNA_A;
1541
1542 if (tbl->action == start_action)
1543 break;
1544 }
1545
1546 return 0;
1547 out:
1548 tbl->action++;
1549 if (tbl->action > IWL_MIMO_SWITCH_GI)
1550 tbl->action = IWL_MIMO_SWITCH_ANTENNA_A;
1551 return 0;
1552
1553}
1554
Ben Cahill77626352007-11-29 11:09:44 +08001555/*
1556 * Check whether we should continue using same modulation mode, or
1557 * begin search for a new mode, based on:
1558 * 1) # tx successes or failures while using this mode
1559 * 2) # times calling this function
1560 * 3) elapsed time in this mode (not used, for now)
1561 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001562static void rs_stay_in_table(struct iwl4965_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07001563{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001564 struct iwl4965_scale_tbl_info *tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001565 int i;
1566 int active_tbl;
1567 int flush_interval_passed = 0;
1568
Tomas Winklerc33104f2008-01-14 17:46:21 -08001569 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001570
Tomas Winklerc33104f2008-01-14 17:46:21 -08001571 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07001572
Ben Cahill77626352007-11-29 11:09:44 +08001573 /* If we've been disallowing search, see if we should now allow it */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001574 if (lq_sta->stay_in_tbl) {
Zhu Yib481de92007-09-25 17:54:57 -07001575
Ben Cahill77626352007-11-29 11:09:44 +08001576 /* Elapsed time using current modulation mode */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001577 if (lq_sta->flush_timer)
Zhu Yib481de92007-09-25 17:54:57 -07001578 flush_interval_passed =
1579 time_after(jiffies,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001580 (unsigned long)(lq_sta->flush_timer +
Zhu Yib481de92007-09-25 17:54:57 -07001581 IWL_RATE_SCALE_FLUSH_INTVL));
1582
Ben Cahill77626352007-11-29 11:09:44 +08001583 /*
1584 * Check if we should allow search for new modulation mode.
1585 * If many frames have failed or succeeded, or we've used
1586 * this same modulation for a long time, allow search, and
1587 * reset history stats that keep track of whether we should
1588 * allow a new search. Also (below) reset all bitmaps and
1589 * stats in active history.
1590 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001591 if ((lq_sta->total_failed > lq_sta->max_failure_limit) ||
1592 (lq_sta->total_success > lq_sta->max_success_limit) ||
1593 ((!lq_sta->search_better_tbl) && (lq_sta->flush_timer)
Zhu Yib481de92007-09-25 17:54:57 -07001594 && (flush_interval_passed))) {
Guy Coheneecd6e52008-04-23 17:14:58 -07001595 IWL_DEBUG_RATE("LQ: stay is expired %d %d %d\n:",
Tomas Winklerc33104f2008-01-14 17:46:21 -08001596 lq_sta->total_failed,
1597 lq_sta->total_success,
Zhu Yib481de92007-09-25 17:54:57 -07001598 flush_interval_passed);
Ben Cahill77626352007-11-29 11:09:44 +08001599
1600 /* Allow search for new mode */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001601 lq_sta->stay_in_tbl = 0; /* only place reset */
1602 lq_sta->total_failed = 0;
1603 lq_sta->total_success = 0;
1604 lq_sta->flush_timer = 0;
Ben Cahill77626352007-11-29 11:09:44 +08001605
1606 /*
1607 * Else if we've used this modulation mode enough repetitions
1608 * (regardless of elapsed time or success/failure), reset
1609 * history bitmaps and rate-specific stats for all rates in
1610 * active table.
1611 */
Mohamed Abbas403ab562007-11-06 22:06:25 -08001612 } else {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001613 lq_sta->table_count++;
1614 if (lq_sta->table_count >=
1615 lq_sta->table_count_limit) {
1616 lq_sta->table_count = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001617
Guy Coheneecd6e52008-04-23 17:14:58 -07001618 IWL_DEBUG_RATE("LQ: stay in table clear win\n");
Zhu Yib481de92007-09-25 17:54:57 -07001619 for (i = 0; i < IWL_RATE_COUNT; i++)
1620 rs_rate_scale_clear_window(
1621 &(tbl->win[i]));
1622 }
1623 }
1624
Ben Cahill77626352007-11-29 11:09:44 +08001625 /* If transitioning to allow "search", reset all history
1626 * bitmaps and stats in active table (this will become the new
1627 * "search" table). */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001628 if (!lq_sta->stay_in_tbl) {
Zhu Yib481de92007-09-25 17:54:57 -07001629 for (i = 0; i < IWL_RATE_COUNT; i++)
1630 rs_rate_scale_clear_window(&(tbl->win[i]));
1631 }
1632 }
1633}
1634
Ben Cahill77626352007-11-29 11:09:44 +08001635/*
1636 * Do rate scaling and search for new modulation mode.
1637 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001638static void rs_rate_scale_perform(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001639 struct net_device *dev,
1640 struct ieee80211_hdr *hdr,
1641 struct sta_info *sta)
1642{
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001643 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1644 struct ieee80211_hw *hw = local_to_hw(local);
1645 struct ieee80211_conf *conf = &hw->conf;
Zhu Yib481de92007-09-25 17:54:57 -07001646 int low = IWL_RATE_INVALID;
1647 int high = IWL_RATE_INVALID;
1648 int index;
1649 int i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001650 struct iwl4965_rate_scale_data *window = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001651 int current_tpt = IWL_INVALID_VALUE;
1652 int low_tpt = IWL_INVALID_VALUE;
1653 int high_tpt = IWL_INVALID_VALUE;
1654 u32 fail_count;
1655 s8 scale_action = 0;
1656 u16 fc, rate_mask;
1657 u8 update_lq = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001658 struct iwl4965_lq_sta *lq_sta;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001659 struct iwl4965_scale_tbl_info *tbl, *tbl1;
Zhu Yib481de92007-09-25 17:54:57 -07001660 u16 rate_scale_index_msk = 0;
Guy Cohen39e88502008-04-23 17:14:57 -07001661 u32 rate;
Zhu Yib481de92007-09-25 17:54:57 -07001662 u8 is_green = 0;
1663 u8 active_tbl = 0;
1664 u8 done_search = 0;
1665 u16 high_low;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001666#ifdef CONFIG_IWL4965_HT
1667 u8 tid = MAX_TID_COUNT;
1668 __le16 *qc;
1669#endif
Zhu Yib481de92007-09-25 17:54:57 -07001670
1671 IWL_DEBUG_RATE("rate scale calculate new rate for skb\n");
1672
1673 fc = le16_to_cpu(hdr->frame_control);
1674 if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1)) {
1675 /* Send management frames and broadcast/multicast data using
1676 * lowest rate. */
1677 /* TODO: this could probably be improved.. */
1678 return;
1679 }
1680
1681 if (!sta || !sta->rate_ctrl_priv)
1682 return;
1683
1684 if (!priv->lq_mngr.lq_ready) {
1685 IWL_DEBUG_RATE("still rate scaling not ready\n");
1686 return;
1687 }
Tomas Winklerc33104f2008-01-14 17:46:21 -08001688 lq_sta = (struct iwl4965_lq_sta *)sta->rate_ctrl_priv;
Zhu Yib481de92007-09-25 17:54:57 -07001689
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001690#ifdef CONFIG_IWL4965_HT
1691 qc = ieee80211_get_qos_ctrl(hdr);
1692 if (qc) {
1693 tid = (u8)(le16_to_cpu(*qc) & 0xf);
1694 rs_tl_add_packet(lq_sta, tid);
1695 }
1696#endif
Ben Cahill77626352007-11-29 11:09:44 +08001697 /*
1698 * Select rate-scale / modulation-mode table to work with in
1699 * the rest of this function: "search" if searching for better
1700 * modulation mode, or "active" if doing rate scaling within a mode.
1701 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001702 if (!lq_sta->search_better_tbl)
1703 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001704 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08001705 active_tbl = 1 - lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001706
Tomas Winklerc33104f2008-01-14 17:46:21 -08001707 tbl = &(lq_sta->lq_info[active_tbl]);
1708 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07001709
Ben Cahill77626352007-11-29 11:09:44 +08001710 /* current tx rate */
Johannes Berg8318d782008-01-24 19:38:38 +01001711 index = sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07001712
1713 IWL_DEBUG_RATE("Rate scale index %d for type %d\n", index,
1714 tbl->lq_type);
1715
Ben Cahill77626352007-11-29 11:09:44 +08001716 /* rates available for this association, and for modulation mode */
Guy Coheneecd6e52008-04-23 17:14:58 -07001717 rate_mask = rs_get_supported_rates(lq_sta, hdr, tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -07001718
1719 IWL_DEBUG_RATE("mask 0x%04X \n", rate_mask);
1720
1721 /* mask with station rate restriction */
1722 if (is_legacy(tbl->lq_type)) {
Johannes Berg8318d782008-01-24 19:38:38 +01001723 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Ben Cahill77626352007-11-29 11:09:44 +08001724 /* supp_rates has no CCK bits in A mode */
Zhu Yib481de92007-09-25 17:54:57 -07001725 rate_scale_index_msk = (u16) (rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -08001726 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
Zhu Yib481de92007-09-25 17:54:57 -07001727 else
1728 rate_scale_index_msk = (u16) (rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -08001729 lq_sta->supp_rates);
Zhu Yib481de92007-09-25 17:54:57 -07001730
1731 } else
1732 rate_scale_index_msk = rate_mask;
1733
1734 if (!rate_scale_index_msk)
1735 rate_scale_index_msk = rate_mask;
1736
Guy Cohen07bc28e2008-04-23 17:15:03 -07001737 if (!((1 << index) & rate_scale_index_msk)) {
1738 IWL_ERROR("Current Rate is not valid\n");
1739 return;
Zhu Yib481de92007-09-25 17:54:57 -07001740 }
1741
Ben Cahill77626352007-11-29 11:09:44 +08001742 /* Get expected throughput table and history window for current rate */
Guy Cohen07bc28e2008-04-23 17:15:03 -07001743 if (!tbl->expected_tpt) {
1744 IWL_ERROR("tbl->expected_tpt is NULL\n");
1745 return;
1746 }
Zhu Yib481de92007-09-25 17:54:57 -07001747
1748 window = &(tbl->win[index]);
1749
Ben Cahill77626352007-11-29 11:09:44 +08001750 /*
1751 * If there is not enough history to calculate actual average
1752 * throughput, keep analyzing results of more tx frames, without
1753 * changing rate or mode (bypass most of the rest of this function).
1754 * Set up new rate table in uCode only if old rate is not supported
1755 * in current association (use new rate found above).
1756 */
Zhu Yib481de92007-09-25 17:54:57 -07001757 fail_count = window->counter - window->success_counter;
Guy Cohen07bc28e2008-04-23 17:15:03 -07001758 if ((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
1759 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH)) {
1760 IWL_DEBUG_RATE("LQ: still below TH. succ=%d total=%d "
Zhu Yib481de92007-09-25 17:54:57 -07001761 "for index %d\n",
1762 window->success_counter, window->counter, index);
Ben Cahill77626352007-11-29 11:09:44 +08001763
1764 /* Can't calculate this yet; not enough history */
Zhu Yib481de92007-09-25 17:54:57 -07001765 window->average_tpt = IWL_INVALID_VALUE;
Ben Cahill77626352007-11-29 11:09:44 +08001766
1767 /* Should we stay with this modulation mode,
1768 * or search for a new one? */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001769 rs_stay_in_table(lq_sta);
Ben Cahill77626352007-11-29 11:09:44 +08001770
Zhu Yib481de92007-09-25 17:54:57 -07001771 goto out;
1772
Ben Cahill77626352007-11-29 11:09:44 +08001773 /* Else we have enough samples; calculate estimate of
1774 * actual average throughput */
Guy Cohen07bc28e2008-04-23 17:15:03 -07001775 } else {
1776 /*FIXME:RS remove this else if we don't get this error*/
1777 if (window->average_tpt != ((window->success_ratio *
1778 tbl->expected_tpt[index] + 64) / 128)) {
1779 IWL_ERROR("expected_tpt should have been calculated"
1780 " by now\n");
1781 window->average_tpt = ((window->success_ratio *
Zhu Yib481de92007-09-25 17:54:57 -07001782 tbl->expected_tpt[index] + 64) / 128);
Guy Cohen07bc28e2008-04-23 17:15:03 -07001783 }
1784 }
Zhu Yib481de92007-09-25 17:54:57 -07001785
Ben Cahill77626352007-11-29 11:09:44 +08001786 /* If we are searching for better modulation mode, check success. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001787 if (lq_sta->search_better_tbl) {
Zhu Yib481de92007-09-25 17:54:57 -07001788
Ben Cahill77626352007-11-29 11:09:44 +08001789 /* If good success, continue using the "search" mode;
1790 * no need to send new link quality command, since we're
1791 * continuing to use the setup that we've been trying. */
Guy Cohen07bc28e2008-04-23 17:15:03 -07001792 if (window->average_tpt > lq_sta->last_tpt) {
1793
1794 IWL_DEBUG_RATE("LQ: SWITCHING TO CURRENT TABLE "
1795 "suc=%d cur-tpt=%d old-tpt=%d\n",
1796 window->success_ratio,
1797 window->average_tpt,
1798 lq_sta->last_tpt);
1799
1800 if (!is_legacy(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08001801 lq_sta->enable_counter = 1;
Guy Cohen07bc28e2008-04-23 17:15:03 -07001802
Ben Cahill77626352007-11-29 11:09:44 +08001803 /* Swap tables; "search" becomes "active" */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001804 lq_sta->active_tbl = active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001805 current_tpt = window->average_tpt;
Ben Cahill77626352007-11-29 11:09:44 +08001806
1807 /* Else poor success; go back to mode in "active" table */
Zhu Yib481de92007-09-25 17:54:57 -07001808 } else {
Guy Cohen07bc28e2008-04-23 17:15:03 -07001809
1810 IWL_DEBUG_RATE("LQ: GOING BACK TO THE OLD TABLE "
1811 "suc=%d cur-tpt=%d old-tpt=%d\n",
1812 window->success_ratio,
1813 window->average_tpt,
1814 lq_sta->last_tpt);
1815
Ben Cahill77626352007-11-29 11:09:44 +08001816 /* Nullify "search" table */
Zhu Yib481de92007-09-25 17:54:57 -07001817 tbl->lq_type = LQ_NONE;
Ben Cahill77626352007-11-29 11:09:44 +08001818
1819 /* Revert to "active" table */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001820 active_tbl = lq_sta->active_tbl;
1821 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07001822
Ben Cahill77626352007-11-29 11:09:44 +08001823 /* Revert to "active" rate and throughput info */
Tomas Winkler17744ff2008-03-02 01:52:00 +02001824 index = iwl4965_hwrate_to_plcp_idx(
Guy Cohen39e88502008-04-23 17:14:57 -07001825 tbl->current_rate);
Tomas Winklerc33104f2008-01-14 17:46:21 -08001826 current_tpt = lq_sta->last_tpt;
Ben Cahill77626352007-11-29 11:09:44 +08001827
1828 /* Need to set up a new rate table in uCode */
1829 update_lq = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001830 }
Ben Cahill77626352007-11-29 11:09:44 +08001831
1832 /* Either way, we've made a decision; modulation mode
1833 * search is done, allow rate adjustment next time. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001834 lq_sta->search_better_tbl = 0;
Ben Cahill77626352007-11-29 11:09:44 +08001835 done_search = 1; /* Don't switch modes below! */
Zhu Yib481de92007-09-25 17:54:57 -07001836 goto lq_update;
1837 }
1838
Ben Cahill77626352007-11-29 11:09:44 +08001839 /* (Else) not in search of better modulation mode, try for better
1840 * starting rate, while staying in this mode. */
Zhu Yib481de92007-09-25 17:54:57 -07001841 high_low = rs_get_adjacent_rate(index, rate_scale_index_msk,
1842 tbl->lq_type);
1843 low = high_low & 0xff;
1844 high = (high_low >> 8) & 0xff;
1845
Ben Cahill77626352007-11-29 11:09:44 +08001846 /* Collect measured throughputs for current and adjacent rates */
Zhu Yib481de92007-09-25 17:54:57 -07001847 current_tpt = window->average_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07001848 if (low != IWL_RATE_INVALID)
1849 low_tpt = tbl->win[low].average_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07001850 if (high != IWL_RATE_INVALID)
1851 high_tpt = tbl->win[high].average_tpt;
1852
Ben Cahill77626352007-11-29 11:09:44 +08001853 /* Assume rate increase */
Zhu Yib481de92007-09-25 17:54:57 -07001854 scale_action = 1;
1855
Ben Cahill77626352007-11-29 11:09:44 +08001856 /* Too many failures, decrease rate */
Zhu Yib481de92007-09-25 17:54:57 -07001857 if ((window->success_ratio <= IWL_RATE_DECREASE_TH) ||
1858 (current_tpt == 0)) {
1859 IWL_DEBUG_RATE("decrease rate because of low success_ratio\n");
1860 scale_action = -1;
Ben Cahill77626352007-11-29 11:09:44 +08001861
1862 /* No throughput measured yet for adjacent rates; try increase. */
Zhu Yib481de92007-09-25 17:54:57 -07001863 } else if ((low_tpt == IWL_INVALID_VALUE) &&
1864 (high_tpt == IWL_INVALID_VALUE))
1865 scale_action = 1;
Ben Cahill77626352007-11-29 11:09:44 +08001866
1867 /* Both adjacent throughputs are measured, but neither one has better
1868 * throughput; we're using the best rate, don't change it! */
Zhu Yib481de92007-09-25 17:54:57 -07001869 else if ((low_tpt != IWL_INVALID_VALUE) &&
1870 (high_tpt != IWL_INVALID_VALUE) &&
1871 (low_tpt < current_tpt) &&
1872 (high_tpt < current_tpt))
1873 scale_action = 0;
Ben Cahill77626352007-11-29 11:09:44 +08001874
1875 /* At least one adjacent rate's throughput is measured,
1876 * and may have better performance. */
Zhu Yib481de92007-09-25 17:54:57 -07001877 else {
Ben Cahill77626352007-11-29 11:09:44 +08001878 /* Higher adjacent rate's throughput is measured */
Zhu Yib481de92007-09-25 17:54:57 -07001879 if (high_tpt != IWL_INVALID_VALUE) {
Ben Cahill77626352007-11-29 11:09:44 +08001880 /* Higher rate has better throughput */
Zhu Yib481de92007-09-25 17:54:57 -07001881 if (high_tpt > current_tpt)
1882 scale_action = 1;
1883 else {
1884 IWL_DEBUG_RATE
1885 ("decrease rate because of high tpt\n");
1886 scale_action = -1;
1887 }
Ben Cahill77626352007-11-29 11:09:44 +08001888
1889 /* Lower adjacent rate's throughput is measured */
Zhu Yib481de92007-09-25 17:54:57 -07001890 } else if (low_tpt != IWL_INVALID_VALUE) {
Ben Cahill77626352007-11-29 11:09:44 +08001891 /* Lower rate has better throughput */
Zhu Yib481de92007-09-25 17:54:57 -07001892 if (low_tpt > current_tpt) {
1893 IWL_DEBUG_RATE
1894 ("decrease rate because of low tpt\n");
1895 scale_action = -1;
1896 } else
1897 scale_action = 1;
1898 }
1899 }
1900
Ben Cahill77626352007-11-29 11:09:44 +08001901 /* Sanity check; asked for decrease, but success rate or throughput
1902 * has been good at old rate. Don't change it. */
Zhu Yib481de92007-09-25 17:54:57 -07001903 if (scale_action == -1) {
1904 if ((low != IWL_RATE_INVALID) &&
1905 ((window->success_ratio > IWL_RATE_HIGH_TH) ||
1906 (current_tpt > (100 * tbl->expected_tpt[low]))))
1907 scale_action = 0;
Ben Cahill77626352007-11-29 11:09:44 +08001908
1909 /* Sanity check; asked for increase, but success rate has not been great
1910 * even at old rate, higher rate will be worse. Don't change it. */
Zhu Yib481de92007-09-25 17:54:57 -07001911 } else if ((scale_action == 1) &&
1912 (window->success_ratio < IWL_RATE_INCREASE_TH))
1913 scale_action = 0;
1914
1915 switch (scale_action) {
1916 case -1:
Ben Cahill77626352007-11-29 11:09:44 +08001917 /* Decrease starting rate, update uCode's rate table */
Zhu Yib481de92007-09-25 17:54:57 -07001918 if (low != IWL_RATE_INVALID) {
1919 update_lq = 1;
1920 index = low;
1921 }
1922 break;
1923 case 1:
Ben Cahill77626352007-11-29 11:09:44 +08001924 /* Increase starting rate, update uCode's rate table */
Zhu Yib481de92007-09-25 17:54:57 -07001925 if (high != IWL_RATE_INVALID) {
1926 update_lq = 1;
1927 index = high;
1928 }
1929
1930 break;
1931 case 0:
Ben Cahill77626352007-11-29 11:09:44 +08001932 /* No change */
Zhu Yib481de92007-09-25 17:54:57 -07001933 default:
1934 break;
1935 }
1936
Guy Coheneecd6e52008-04-23 17:14:58 -07001937 IWL_DEBUG_RATE("choose rate scale index %d action %d low %d "
Zhu Yib481de92007-09-25 17:54:57 -07001938 "high %d type %d\n",
1939 index, scale_action, low, high, tbl->lq_type);
1940
1941 lq_update:
Ben Cahill77626352007-11-29 11:09:44 +08001942 /* Replace uCode's rate table for the destination station. */
Zhu Yib481de92007-09-25 17:54:57 -07001943 if (update_lq) {
Guy Cohen39e88502008-04-23 17:14:57 -07001944 rate = rate_n_flags_from_tbl(tbl, index, is_green);
Guy Cohen07bc28e2008-04-23 17:15:03 -07001945 rs_fill_link_cmd(priv, lq_sta, rate);
Tomas Winkler66c73db2008-04-15 16:01:40 -07001946 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07001947 }
Ben Cahill77626352007-11-29 11:09:44 +08001948
1949 /* Should we stay with this modulation mode, or search for a new one? */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001950 rs_stay_in_table(lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07001951
Ben Cahill77626352007-11-29 11:09:44 +08001952 /*
1953 * Search for new modulation mode if we're:
1954 * 1) Not changing rates right now
1955 * 2) Not just finishing up a search
1956 * 3) Allowing a new search
1957 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001958 if (!update_lq && !done_search && !lq_sta->stay_in_tbl) {
Ben Cahill77626352007-11-29 11:09:44 +08001959 /* Save current throughput to compare with "search" throughput*/
Tomas Winklerc33104f2008-01-14 17:46:21 -08001960 lq_sta->last_tpt = current_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07001961
Ben Cahill77626352007-11-29 11:09:44 +08001962 /* Select a new "search" modulation mode to try.
1963 * If one is found, set up the new "search" table. */
Zhu Yib481de92007-09-25 17:54:57 -07001964 if (is_legacy(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08001965 rs_move_legacy_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07001966 else if (is_siso(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08001967 rs_move_siso_to_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07001968 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08001969 rs_move_mimo_to_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07001970
Ben Cahill77626352007-11-29 11:09:44 +08001971 /* If new "search" mode was selected, set up in uCode table */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001972 if (lq_sta->search_better_tbl) {
Ben Cahill77626352007-11-29 11:09:44 +08001973 /* Access the "search" table, clear its history. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001974 tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Zhu Yib481de92007-09-25 17:54:57 -07001975 for (i = 0; i < IWL_RATE_COUNT; i++)
1976 rs_rate_scale_clear_window(&(tbl->win[i]));
1977
Ben Cahill77626352007-11-29 11:09:44 +08001978 /* Use new "search" start rate */
Tomas Winkler17744ff2008-03-02 01:52:00 +02001979 index = iwl4965_hwrate_to_plcp_idx(
Guy Cohen39e88502008-04-23 17:14:57 -07001980 tbl->current_rate);
Zhu Yib481de92007-09-25 17:54:57 -07001981
Guy Coheneecd6e52008-04-23 17:14:58 -07001982 IWL_DEBUG_RATE("Switch current mcs: %X index: %d\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001983 tbl->current_rate, index);
Guy Cohen07bc28e2008-04-23 17:15:03 -07001984 rs_fill_link_cmd(priv, lq_sta, tbl->current_rate);
Tomas Winkler66c73db2008-04-15 16:01:40 -07001985 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07001986 }
Zhu Yib481de92007-09-25 17:54:57 -07001987
Ben Cahill77626352007-11-29 11:09:44 +08001988 /* If the "active" (non-search) mode was legacy,
1989 * and we've tried switching antennas,
1990 * but we haven't been able to try HT modes (not available),
1991 * stay with best antenna legacy modulation for a while
1992 * before next round of mode comparisons. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001993 tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07001994 if (is_legacy(tbl1->lq_type) &&
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001995#ifdef CONFIG_IWL4965_HT
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001996 (!(conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE)) &&
Zhu Yib481de92007-09-25 17:54:57 -07001997#endif
Tomas Winklerc33104f2008-01-14 17:46:21 -08001998 (lq_sta->action_counter >= 1)) {
1999 lq_sta->action_counter = 0;
Guy Coheneecd6e52008-04-23 17:14:58 -07002000 IWL_DEBUG_RATE("LQ: STAY in legacy table\n");
Tomas Winklerc33104f2008-01-14 17:46:21 -08002001 rs_set_stay_in_table(1, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002002 }
2003
Ben Cahill77626352007-11-29 11:09:44 +08002004 /* If we're in an HT mode, and all 3 mode switch actions
2005 * have been tried and compared, stay in this best modulation
2006 * mode for a while before next round of mode comparisons. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002007 if (lq_sta->enable_counter &&
2008 (lq_sta->action_counter >= IWL_ACTION_LIMIT)) {
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002009#ifdef CONFIG_IWL4965_HT
2010 if ((lq_sta->last_tpt > IWL_AGG_TPT_THREHOLD) &&
2011 (lq_sta->tx_agg_tid_en & (1 << tid)) &&
2012 (tid != MAX_TID_COUNT)) {
Guy Coheneecd6e52008-04-23 17:14:58 -07002013 IWL_DEBUG_RATE("try to aggregate tid %d\n", tid);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002014 rs_tl_turn_on_agg(priv, tid, lq_sta, sta);
Zhu Yib481de92007-09-25 17:54:57 -07002015 }
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002016#endif /*CONFIG_IWL4965_HT */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002017 lq_sta->action_counter = 0;
2018 rs_set_stay_in_table(0, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002019 }
Ben Cahill77626352007-11-29 11:09:44 +08002020
2021 /*
2022 * Else, don't search for a new modulation mode.
2023 * Put new timestamp in stay-in-modulation-mode flush timer if:
2024 * 1) Not changing rates right now
2025 * 2) Not just finishing up a search
2026 * 3) flush timer is empty
2027 */
Zhu Yib481de92007-09-25 17:54:57 -07002028 } else {
Tomas Winklerc33104f2008-01-14 17:46:21 -08002029 if ((!update_lq) && (!done_search) && (!lq_sta->flush_timer))
2030 lq_sta->flush_timer = jiffies;
Zhu Yib481de92007-09-25 17:54:57 -07002031 }
2032
2033out:
Guy Cohen39e88502008-04-23 17:14:57 -07002034 tbl->current_rate = rate_n_flags_from_tbl(tbl, index, is_green);
Zhu Yib481de92007-09-25 17:54:57 -07002035 i = index;
Johannes Berg8318d782008-01-24 19:38:38 +01002036 sta->last_txrate_idx = i;
Zhu Yib481de92007-09-25 17:54:57 -07002037
Johannes Berg8318d782008-01-24 19:38:38 +01002038 /* sta->txrate_idx is an index to A mode rates which start
Zhu Yib481de92007-09-25 17:54:57 -07002039 * at IWL_FIRST_OFDM_RATE
2040 */
Johannes Berg8318d782008-01-24 19:38:38 +01002041 if (lq_sta->band == IEEE80211_BAND_5GHZ)
2042 sta->txrate_idx = i - IWL_FIRST_OFDM_RATE;
Zhu Yib481de92007-09-25 17:54:57 -07002043 else
Johannes Berg8318d782008-01-24 19:38:38 +01002044 sta->txrate_idx = i;
Zhu Yib481de92007-09-25 17:54:57 -07002045
2046 return;
2047}
2048
2049
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002050static void rs_initialize_lq(struct iwl_priv *priv,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002051 struct ieee80211_conf *conf,
Zhu Yib481de92007-09-25 17:54:57 -07002052 struct sta_info *sta)
2053{
Tomas Winklerc33104f2008-01-14 17:46:21 -08002054 struct iwl4965_lq_sta *lq_sta;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002055 struct iwl4965_scale_tbl_info *tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002056 int rate_idx;
Guy Cohenaade00c2008-04-23 17:14:59 -07002057 int i;
Guy Cohen39e88502008-04-23 17:14:57 -07002058 u32 rate;
Guy Cohenaade00c2008-04-23 17:14:59 -07002059 u8 use_green = rs_use_green(priv, conf);
2060 u8 active_tbl = 0;
2061 u8 valid_tx_ant;
Zhu Yib481de92007-09-25 17:54:57 -07002062
2063 if (!sta || !sta->rate_ctrl_priv)
2064 goto out;
2065
Tomas Winklerc33104f2008-01-14 17:46:21 -08002066 lq_sta = (struct iwl4965_lq_sta *)sta->rate_ctrl_priv;
Johannes Berg8318d782008-01-24 19:38:38 +01002067 i = sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002068
Tomas Winklerc33104f2008-01-14 17:46:21 -08002069 if ((lq_sta->lq.sta_id == 0xff) &&
Zhu Yib481de92007-09-25 17:54:57 -07002070 (priv->iw_mode == IEEE80211_IF_TYPE_IBSS))
2071 goto out;
2072
Guy Cohenaade00c2008-04-23 17:14:59 -07002073 valid_tx_ant = priv->hw_params.valid_tx_ant;
2074
Tomas Winklerc33104f2008-01-14 17:46:21 -08002075 if (!lq_sta->search_better_tbl)
2076 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002077 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08002078 active_tbl = 1 - lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002079
Tomas Winklerc33104f2008-01-14 17:46:21 -08002080 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07002081
2082 if ((i < 0) || (i >= IWL_RATE_COUNT))
2083 i = 0;
2084
Guy Cohenfde0db32008-04-21 15:42:01 -07002085 /* FIXME:RS: This is also wrong in 4965 */
Guy Cohen39e88502008-04-23 17:14:57 -07002086 rate = iwl4965_rates[i].plcp;
2087 rate |= RATE_MCS_ANT_B_MSK;
2088 rate &= ~RATE_MCS_ANT_A_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002089
2090 if (i >= IWL_FIRST_CCK_RATE && i <= IWL_LAST_CCK_RATE)
Guy Cohen39e88502008-04-23 17:14:57 -07002091 rate |= RATE_MCS_CCK_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002092
Guy Cohenfde0db32008-04-21 15:42:01 -07002093 tbl->ant_type = ANT_B;
Guy Cohen39e88502008-04-23 17:14:57 -07002094 rs_get_tbl_info_from_mcs(rate, priv->band, tbl, &rate_idx);
Guy Cohenaade00c2008-04-23 17:14:59 -07002095 if (!rs_is_valid_ant(valid_tx_ant, tbl->ant_type))
2096 rs_toggle_antenna(valid_tx_ant, &rate, tbl);
Zhu Yib481de92007-09-25 17:54:57 -07002097
Guy Cohen39e88502008-04-23 17:14:57 -07002098 rate = rate_n_flags_from_tbl(tbl, rate_idx, use_green);
2099 tbl->current_rate = rate;
Guy Coheneecd6e52008-04-23 17:14:58 -07002100 rs_set_expected_tpt_table(lq_sta, tbl);
Guy Cohen07bc28e2008-04-23 17:15:03 -07002101 rs_fill_link_cmd(NULL, lq_sta, rate);
Tomas Winkler66c73db2008-04-15 16:01:40 -07002102 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07002103 out:
2104 return;
2105}
2106
Mattias Nissler1abbe492007-12-20 13:50:07 +01002107static void rs_get_rate(void *priv_rate, struct net_device *dev,
Johannes Berg8318d782008-01-24 19:38:38 +01002108 struct ieee80211_supported_band *sband,
2109 struct sk_buff *skb,
Mattias Nissler1abbe492007-12-20 13:50:07 +01002110 struct rate_selection *sel)
Zhu Yib481de92007-09-25 17:54:57 -07002111{
2112
2113 int i;
2114 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002115 struct ieee80211_conf *conf = &local->hw.conf;
Zhu Yib481de92007-09-25 17:54:57 -07002116 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2117 struct sta_info *sta;
Michael Wu2bc454b2007-12-25 19:33:16 -05002118 u16 fc;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002119 struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002120 struct iwl4965_lq_sta *lq_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002121
Zhu Yi58826352007-09-27 11:27:39 +08002122 IWL_DEBUG_RATE_LIMIT("rate scale calculate new rate for skb\n");
Zhu Yib481de92007-09-25 17:54:57 -07002123
Johannes Bergd0709a62008-02-25 16:27:46 +01002124 rcu_read_lock();
2125
Zhu Yib481de92007-09-25 17:54:57 -07002126 sta = sta_info_get(local, hdr->addr1);
2127
Michael Wu2bc454b2007-12-25 19:33:16 -05002128 /* Send management frames and broadcast/multicast data using lowest
2129 * rate. */
2130 fc = le16_to_cpu(hdr->frame_control);
2131 if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1) ||
2132 !sta || !sta->rate_ctrl_priv) {
Johannes Berg8318d782008-01-24 19:38:38 +01002133 sel->rate = rate_lowest(local, sband, sta);
Tomas Winklerf4d60822008-03-05 11:31:00 -08002134 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07002135 }
2136
Tomas Winklerc33104f2008-01-14 17:46:21 -08002137 lq_sta = (struct iwl4965_lq_sta *)sta->rate_ctrl_priv;
Johannes Berg8318d782008-01-24 19:38:38 +01002138 i = sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002139
Tomas Winklerc33104f2008-01-14 17:46:21 -08002140 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
2141 !lq_sta->ibss_sta_added) {
Tomas Winkler947b13a2008-04-16 16:34:48 -07002142 u8 sta_id = iwl_find_station(priv, hdr->addr1);
Joe Perches0795af52007-10-03 17:59:30 -07002143 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07002144
2145 if (sta_id == IWL_INVALID_STATION) {
Joe Perches0795af52007-10-03 17:59:30 -07002146 IWL_DEBUG_RATE("LQ: ADD station %s\n",
2147 print_mac(mac, hdr->addr1));
Ron Rindjunsky67d62032007-11-26 16:14:40 +02002148 sta_id = iwl4965_add_station_flags(priv, hdr->addr1,
2149 0, CMD_ASYNC, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07002150 }
2151 if ((sta_id != IWL_INVALID_STATION)) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08002152 lq_sta->lq.sta_id = sta_id;
2153 lq_sta->lq.rs_table[0].rate_n_flags = 0;
2154 lq_sta->ibss_sta_added = 1;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002155 rs_initialize_lq(priv, conf, sta);
Zhu Yib481de92007-09-25 17:54:57 -07002156 }
Tomas Winklerc33104f2008-01-14 17:46:21 -08002157 if (!lq_sta->ibss_sta_added)
Zhu Yib481de92007-09-25 17:54:57 -07002158 goto done;
2159 }
2160
Tomas Winklerf4d60822008-03-05 11:31:00 -08002161done:
Mattias Nissler1abbe492007-12-20 13:50:07 +01002162 if ((i < 0) || (i > IWL_RATE_COUNT)) {
Johannes Berg8318d782008-01-24 19:38:38 +01002163 sel->rate = rate_lowest(local, sband, sta);
Tomas Winklerf4d60822008-03-05 11:31:00 -08002164 goto out;
Mattias Nissler1abbe492007-12-20 13:50:07 +01002165 }
Zhu Yib481de92007-09-25 17:54:57 -07002166
Mattias Nissler1abbe492007-12-20 13:50:07 +01002167 sel->rate = &priv->ieee_rates[i];
Tomas Winklerf4d60822008-03-05 11:31:00 -08002168out:
2169 rcu_read_unlock();
Zhu Yib481de92007-09-25 17:54:57 -07002170}
2171
2172static void *rs_alloc_sta(void *priv, gfp_t gfp)
2173{
Tomas Winklerc33104f2008-01-14 17:46:21 -08002174 struct iwl4965_lq_sta *lq_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002175 int i, j;
2176
2177 IWL_DEBUG_RATE("create station rate scale window\n");
2178
Tomas Winklerc33104f2008-01-14 17:46:21 -08002179 lq_sta = kzalloc(sizeof(struct iwl4965_lq_sta), gfp);
Zhu Yib481de92007-09-25 17:54:57 -07002180
Tomas Winklerc33104f2008-01-14 17:46:21 -08002181 if (lq_sta == NULL)
Zhu Yib481de92007-09-25 17:54:57 -07002182 return NULL;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002183 lq_sta->lq.sta_id = 0xff;
Zhu Yib481de92007-09-25 17:54:57 -07002184
Zhu Yi63fddb92007-09-27 11:27:36 +08002185
Zhu Yib481de92007-09-25 17:54:57 -07002186 for (j = 0; j < LQ_SIZE; j++)
2187 for (i = 0; i < IWL_RATE_COUNT; i++)
Tomas Winklerc33104f2008-01-14 17:46:21 -08002188 rs_rate_scale_clear_window(&(lq_sta->lq_info[j].win[i]));
Zhu Yib481de92007-09-25 17:54:57 -07002189
Tomas Winklerc33104f2008-01-14 17:46:21 -08002190 return lq_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002191}
2192
2193static void rs_rate_init(void *priv_rate, void *priv_sta,
2194 struct ieee80211_local *local,
2195 struct sta_info *sta)
2196{
2197 int i, j;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002198 struct ieee80211_conf *conf = &local->hw.conf;
Johannes Berg8318d782008-01-24 19:38:38 +01002199 struct ieee80211_supported_band *sband;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002200 struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002201 struct iwl4965_lq_sta *lq_sta = priv_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002202
Johannes Berg8318d782008-01-24 19:38:38 +01002203 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2204
Tomas Winklerc33104f2008-01-14 17:46:21 -08002205 lq_sta->flush_timer = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01002206 lq_sta->supp_rates = sta->supp_rates[sband->band];
2207 sta->txrate_idx = 3;
Zhu Yib481de92007-09-25 17:54:57 -07002208 for (j = 0; j < LQ_SIZE; j++)
2209 for (i = 0; i < IWL_RATE_COUNT; i++)
Tomas Winklerc33104f2008-01-14 17:46:21 -08002210 rs_rate_scale_clear_window(&(lq_sta->lq_info[j].win[i]));
Zhu Yib481de92007-09-25 17:54:57 -07002211
Guy Cohen07bc28e2008-04-23 17:15:03 -07002212 IWL_DEBUG_RATE("LQ: *** rate scale global init ***\n");
Zhu Yib481de92007-09-25 17:54:57 -07002213 /* TODO: what is a good starting rate for STA? About middle? Maybe not
2214 * the lowest or the highest rate.. Could consider using RSSI from
2215 * previous packets? Need to have IEEE 802.1X auth succeed immediately
2216 * after assoc.. */
2217
Tomas Winklerc33104f2008-01-14 17:46:21 -08002218 lq_sta->ibss_sta_added = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002219 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
Tomas Winkler947b13a2008-04-16 16:34:48 -07002220 u8 sta_id = iwl_find_station(priv, sta->addr);
Joe Perches0795af52007-10-03 17:59:30 -07002221 DECLARE_MAC_BUF(mac);
2222
Zhu Yib481de92007-09-25 17:54:57 -07002223 /* for IBSS the call are from tasklet */
Guy Coheneecd6e52008-04-23 17:14:58 -07002224 IWL_DEBUG_RATE("LQ: ADD station %s\n",
Joe Perches0795af52007-10-03 17:59:30 -07002225 print_mac(mac, sta->addr));
Zhu Yib481de92007-09-25 17:54:57 -07002226
2227 if (sta_id == IWL_INVALID_STATION) {
Joe Perches0795af52007-10-03 17:59:30 -07002228 IWL_DEBUG_RATE("LQ: ADD station %s\n",
2229 print_mac(mac, sta->addr));
Ron Rindjunsky67d62032007-11-26 16:14:40 +02002230 sta_id = iwl4965_add_station_flags(priv, sta->addr,
2231 0, CMD_ASYNC, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07002232 }
2233 if ((sta_id != IWL_INVALID_STATION)) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08002234 lq_sta->lq.sta_id = sta_id;
2235 lq_sta->lq.rs_table[0].rate_n_flags = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002236 }
2237 /* FIXME: this is w/a remove it later */
2238 priv->assoc_station_added = 1;
2239 }
2240
Ben Cahill77626352007-11-29 11:09:44 +08002241 /* Find highest tx rate supported by hardware and destination station */
Johannes Berg8318d782008-01-24 19:38:38 +01002242 for (i = 0; i < sband->n_bitrates; i++)
2243 if (sta->supp_rates[sband->band] & BIT(i))
2244 sta->txrate_idx = i;
2245
2246 sta->last_txrate_idx = sta->txrate_idx;
2247 /* WTF is with this bogus comment? A doesn't have cck rates */
Ben Cahill77626352007-11-29 11:09:44 +08002248 /* For MODE_IEEE80211A, cck rates are at end of rate table */
Johannes Berg8318d782008-01-24 19:38:38 +01002249 if (local->hw.conf.channel->band == IEEE80211_BAND_5GHZ)
2250 sta->last_txrate_idx += IWL_FIRST_OFDM_RATE;
Zhu Yib481de92007-09-25 17:54:57 -07002251
Tomas Winklerc33104f2008-01-14 17:46:21 -08002252 lq_sta->is_dup = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002253 lq_sta->is_green = rs_use_green(priv, conf);
Guy Cohen39e88502008-04-23 17:14:57 -07002254 lq_sta->active_legacy_rate = priv->active_rate & ~(0x1000);
Tomas Winklerc33104f2008-01-14 17:46:21 -08002255 lq_sta->active_rate_basic = priv->active_rate_basic;
Johannes Berg8318d782008-01-24 19:38:38 +01002256 lq_sta->band = priv->band;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002257#ifdef CONFIG_IWL4965_HT
Ben Cahill77626352007-11-29 11:09:44 +08002258 /*
2259 * active_siso_rate mask includes 9 MBits (bit 5), and CCK (bits 0-3),
2260 * supp_rates[] does not; shift to convert format, force 9 MBits off.
2261 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002262 lq_sta->active_siso_rate =
Guy Cohenfde0db32008-04-21 15:42:01 -07002263 priv->current_ht_config.supp_mcs_set[0] << 1;
2264 lq_sta->active_siso_rate |=
2265 priv->current_ht_config.supp_mcs_set[0] & 0x1;
2266 lq_sta->active_siso_rate &= ~((u16)0x2);
2267 lq_sta->active_siso_rate <<= IWL_FIRST_OFDM_RATE;
Zhu Yib481de92007-09-25 17:54:57 -07002268
Ben Cahill77626352007-11-29 11:09:44 +08002269 /* Same here */
Guy Cohenfde0db32008-04-21 15:42:01 -07002270 lq_sta->active_mimo2_rate =
2271 priv->current_ht_config.supp_mcs_set[1] << 1;
2272 lq_sta->active_mimo2_rate |=
2273 priv->current_ht_config.supp_mcs_set[1] & 0x1;
2274 lq_sta->active_mimo2_rate &= ~((u16)0x2);
2275 lq_sta->active_mimo2_rate <<= IWL_FIRST_OFDM_RATE;
2276
2277 lq_sta->active_mimo3_rate =
2278 priv->current_ht_config.supp_mcs_set[2] << 1;
2279 lq_sta->active_mimo3_rate |=
2280 priv->current_ht_config.supp_mcs_set[2] & 0x1;
2281 lq_sta->active_mimo3_rate &= ~((u16)0x2);
2282 lq_sta->active_mimo3_rate <<= IWL_FIRST_OFDM_RATE;
2283
Guy Cohen07bc28e2008-04-23 17:15:03 -07002284 IWL_DEBUG_RATE("SISO-RATE=%X MIMO2-RATE=%X MIMO3-RATE=%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002285 lq_sta->active_siso_rate,
Guy Cohenfde0db32008-04-21 15:42:01 -07002286 lq_sta->active_mimo2_rate,
2287 lq_sta->active_mimo3_rate);
2288
Guy Cohen07bc28e2008-04-23 17:15:03 -07002289 /* These values will be overriden later */
2290 lq_sta->lq.general_params.single_stream_ant_msk = ANT_A;
2291 lq_sta->lq.general_params.dual_stream_ant_msk = ANT_AB;
2292
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002293 /* as default allow aggregation for all tids */
2294 lq_sta->tx_agg_tid_en = IWL_AGG_ALL_TID;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002295#endif /*CONFIG_IWL4965_HT*/
Zhu Yi98d7e092007-09-27 11:27:42 +08002296#ifdef CONFIG_MAC80211_DEBUGFS
Tomas Winklerc33104f2008-01-14 17:46:21 -08002297 lq_sta->drv = priv;
Zhu Yi98d7e092007-09-27 11:27:42 +08002298#endif
Zhu Yib481de92007-09-25 17:54:57 -07002299
2300 if (priv->assoc_station_added)
2301 priv->lq_mngr.lq_ready = 1;
2302
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002303 rs_initialize_lq(priv, conf, sta);
Zhu Yib481de92007-09-25 17:54:57 -07002304}
2305
Guy Cohenfde0db32008-04-21 15:42:01 -07002306static void rs_fill_link_cmd(const struct iwl_priv *priv,
2307 struct iwl4965_lq_sta *lq_sta,
Guy Cohen07bc28e2008-04-23 17:15:03 -07002308 u32 new_rate)
Zhu Yib481de92007-09-25 17:54:57 -07002309{
Guy Cohenaade00c2008-04-23 17:14:59 -07002310 struct iwl4965_scale_tbl_info tbl_type;
Zhu Yib481de92007-09-25 17:54:57 -07002311 int index = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002312 int rate_idx;
Zhu Yi1b696de2007-09-27 11:27:41 +08002313 int repeat_rate = 0;
Guy Cohenaade00c2008-04-23 17:14:59 -07002314 u8 ant_toggle_cnt = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002315 u8 use_ht_possible = 1;
Guy Cohenaade00c2008-04-23 17:14:59 -07002316 u8 valid_tx_ant = 0;
Guy Cohen07bc28e2008-04-23 17:15:03 -07002317 struct iwl_link_quality_cmd *lq_cmd = &lq_sta->lq;
Zhu Yib481de92007-09-25 17:54:57 -07002318
Ben Cahill77626352007-11-29 11:09:44 +08002319 /* Override starting rate (index 0) if needed for debug purposes */
Guy Cohen39e88502008-04-23 17:14:57 -07002320 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Zhu Yi98d7e092007-09-27 11:27:42 +08002321
Guy Cohen39e88502008-04-23 17:14:57 -07002322 /* Interpret new_rate (rate_n_flags) */
Guy Cohenaade00c2008-04-23 17:14:59 -07002323 memset(&tbl_type, 0, sizeof(tbl_type));
Guy Cohen39e88502008-04-23 17:14:57 -07002324 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band,
Zhu Yib481de92007-09-25 17:54:57 -07002325 &tbl_type, &rate_idx);
2326
Ben Cahill77626352007-11-29 11:09:44 +08002327 /* How many times should we repeat the initial rate? */
Zhu Yib481de92007-09-25 17:54:57 -07002328 if (is_legacy(tbl_type.lq_type)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07002329 ant_toggle_cnt = 1;
Zhu Yi1b696de2007-09-27 11:27:41 +08002330 repeat_rate = IWL_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002331 } else {
Zhu Yi1b696de2007-09-27 11:27:41 +08002332 repeat_rate = IWL_HT_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002333 }
Zhu Yib481de92007-09-25 17:54:57 -07002334
2335 lq_cmd->general_params.mimo_delimiter =
2336 is_mimo(tbl_type.lq_type) ? 1 : 0;
Ben Cahill77626352007-11-29 11:09:44 +08002337
2338 /* Fill 1st table entry (index 0) */
Guy Cohen39e88502008-04-23 17:14:57 -07002339 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
Zhu Yib481de92007-09-25 17:54:57 -07002340
Guy Cohen07bc28e2008-04-23 17:15:03 -07002341 if (num_of_ant(tbl_type.ant_type) == 1) {
2342 lq_cmd->general_params.single_stream_ant_msk =
2343 tbl_type.ant_type;
2344 } else if (num_of_ant(tbl_type.ant_type) == 2) {
2345 lq_cmd->general_params.dual_stream_ant_msk =
2346 tbl_type.ant_type;
2347 } /* otherwise we don't modify the existing value */
Zhu Yib481de92007-09-25 17:54:57 -07002348
2349 index++;
Zhu Yi1b696de2007-09-27 11:27:41 +08002350 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002351
Guy Cohenaade00c2008-04-23 17:14:59 -07002352 if (priv)
2353 valid_tx_ant = priv->hw_params.valid_tx_ant;
2354
Ben Cahill77626352007-11-29 11:09:44 +08002355 /* Fill rest of rate table */
Zhu Yib481de92007-09-25 17:54:57 -07002356 while (index < LINK_QUAL_MAX_RETRY_NUM) {
Ben Cahill77626352007-11-29 11:09:44 +08002357 /* Repeat initial/next rate.
2358 * For legacy IWL_NUMBER_TRY == 1, this loop will not execute.
2359 * For HT IWL_HT_NUMBER_TRY == 3, this executes twice. */
Zhu Yi1b696de2007-09-27 11:27:41 +08002360 while (repeat_rate > 0 && (index < LINK_QUAL_MAX_RETRY_NUM)) {
Zhu Yib481de92007-09-25 17:54:57 -07002361 if (is_legacy(tbl_type.lq_type)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07002362 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2363 ant_toggle_cnt++;
2364 else if (priv &&
2365 rs_toggle_antenna(valid_tx_ant,
2366 &new_rate, &tbl_type))
2367 ant_toggle_cnt = 1;
2368}
Zhu Yi98d7e092007-09-27 11:27:42 +08002369
Ben Cahill77626352007-11-29 11:09:44 +08002370 /* Override next rate if needed for debug purposes */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002371 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Ben Cahill77626352007-11-29 11:09:44 +08002372
2373 /* Fill next table entry */
Zhu Yib481de92007-09-25 17:54:57 -07002374 lq_cmd->rs_table[index].rate_n_flags =
Guy Cohen39e88502008-04-23 17:14:57 -07002375 cpu_to_le32(new_rate);
Zhu Yi1b696de2007-09-27 11:27:41 +08002376 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002377 index++;
2378 }
2379
Guy Cohen39e88502008-04-23 17:14:57 -07002380 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band, &tbl_type,
Zhu Yib481de92007-09-25 17:54:57 -07002381 &rate_idx);
2382
Ben Cahill77626352007-11-29 11:09:44 +08002383 /* Indicate to uCode which entries might be MIMO.
2384 * If initial rate was MIMO, this will finally end up
2385 * as (IWL_HT_NUMBER_TRY * 2), after 2nd pass, otherwise 0. */
Zhu Yib481de92007-09-25 17:54:57 -07002386 if (is_mimo(tbl_type.lq_type))
2387 lq_cmd->general_params.mimo_delimiter = index;
2388
Ben Cahill77626352007-11-29 11:09:44 +08002389 /* Get next rate */
Guy Cohen39e88502008-04-23 17:14:57 -07002390 new_rate = rs_get_lower_rate(lq_sta, &tbl_type, rate_idx,
2391 use_ht_possible);
Zhu Yib481de92007-09-25 17:54:57 -07002392
Ben Cahill77626352007-11-29 11:09:44 +08002393 /* How many times should we repeat the next rate? */
Zhu Yib481de92007-09-25 17:54:57 -07002394 if (is_legacy(tbl_type.lq_type)) {
Guy Cohenaade00c2008-04-23 17:14:59 -07002395 if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
2396 ant_toggle_cnt++;
2397 else if (priv &&
2398 rs_toggle_antenna(valid_tx_ant,
2399 &new_rate, &tbl_type))
2400 ant_toggle_cnt = 1;
2401
Zhu Yi1b696de2007-09-27 11:27:41 +08002402 repeat_rate = IWL_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002403 } else {
Zhu Yi1b696de2007-09-27 11:27:41 +08002404 repeat_rate = IWL_HT_NUMBER_TRY;
Guy Cohenaade00c2008-04-23 17:14:59 -07002405 }
Zhu Yib481de92007-09-25 17:54:57 -07002406
Ben Cahill77626352007-11-29 11:09:44 +08002407 /* Don't allow HT rates after next pass.
2408 * rs_get_lower_rate() will change type to LQ_A or LQ_G. */
Zhu Yib481de92007-09-25 17:54:57 -07002409 use_ht_possible = 0;
2410
Ben Cahill77626352007-11-29 11:09:44 +08002411 /* Override next rate if needed for debug purposes */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002412 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Ben Cahill77626352007-11-29 11:09:44 +08002413
2414 /* Fill next table entry */
Guy Cohen39e88502008-04-23 17:14:57 -07002415 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
Zhu Yib481de92007-09-25 17:54:57 -07002416
2417 index++;
Zhu Yi1b696de2007-09-27 11:27:41 +08002418 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002419 }
2420
Zhu Yib481de92007-09-25 17:54:57 -07002421 lq_cmd->agg_params.agg_dis_start_th = 3;
2422 lq_cmd->agg_params.agg_time_limit = cpu_to_le16(4000);
Zhu Yib481de92007-09-25 17:54:57 -07002423}
2424
2425static void *rs_alloc(struct ieee80211_local *local)
2426{
2427 return local->hw.priv;
2428}
2429/* rate scale requires free function to be implemented */
2430static void rs_free(void *priv_rate)
2431{
2432 return;
2433}
2434
2435static void rs_clear(void *priv_rate)
2436{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002437 struct iwl_priv *priv = (struct iwl_priv *) priv_rate;
Zhu Yib481de92007-09-25 17:54:57 -07002438
2439 IWL_DEBUG_RATE("enter\n");
2440
2441 priv->lq_mngr.lq_ready = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002442
2443 IWL_DEBUG_RATE("leave\n");
2444}
2445
2446static void rs_free_sta(void *priv, void *priv_sta)
2447{
Tomas Winklerc33104f2008-01-14 17:46:21 -08002448 struct iwl4965_lq_sta *lq_sta = priv_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002449
2450 IWL_DEBUG_RATE("enter\n");
Tomas Winklerc33104f2008-01-14 17:46:21 -08002451 kfree(lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002452 IWL_DEBUG_RATE("leave\n");
2453}
2454
2455
Zhu Yi93dc6462007-09-27 11:27:37 +08002456#ifdef CONFIG_MAC80211_DEBUGFS
Zhu Yi5ae212c2007-09-27 11:27:38 +08002457static int open_file_generic(struct inode *inode, struct file *file)
2458{
2459 file->private_data = inode->i_private;
2460 return 0;
2461}
Tomas Winklerc33104f2008-01-14 17:46:21 -08002462static void rs_dbgfs_set_mcs(struct iwl4965_lq_sta *lq_sta,
Guy Cohen39e88502008-04-23 17:14:57 -07002463 u32 *rate_n_flags, int index)
Zhu Yi98d7e092007-09-27 11:27:42 +08002464{
Guy Cohen39e88502008-04-23 17:14:57 -07002465 if (lq_sta->dbg_fixed_rate) {
2466 if (index < 12) {
2467 *rate_n_flags = lq_sta->dbg_fixed_rate;
2468 } else {
2469 if (lq_sta->band == IEEE80211_BAND_5GHZ)
2470 *rate_n_flags = 0x800D;
2471 else
2472 *rate_n_flags = 0x820A;
2473 }
Zhu Yi98d7e092007-09-27 11:27:42 +08002474 IWL_DEBUG_RATE("Fixed rate ON\n");
Guy Cohen39e88502008-04-23 17:14:57 -07002475 } else {
2476 IWL_DEBUG_RATE("Fixed rate OFF\n");
Zhu Yi98d7e092007-09-27 11:27:42 +08002477 }
Zhu Yi98d7e092007-09-27 11:27:42 +08002478}
2479
2480static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
2481 const char __user *user_buf, size_t count, loff_t *ppos)
2482{
Tomas Winklerc33104f2008-01-14 17:46:21 -08002483 struct iwl4965_lq_sta *lq_sta = file->private_data;
Zhu Yi98d7e092007-09-27 11:27:42 +08002484 char buf[64];
2485 int buf_size;
2486 u32 parsed_rate;
2487
2488 memset(buf, 0, sizeof(buf));
2489 buf_size = min(count, sizeof(buf) - 1);
2490 if (copy_from_user(buf, user_buf, buf_size))
2491 return -EFAULT;
2492
2493 if (sscanf(buf, "%x", &parsed_rate) == 1)
Guy Cohen39e88502008-04-23 17:14:57 -07002494 lq_sta->dbg_fixed_rate = parsed_rate;
Zhu Yi98d7e092007-09-27 11:27:42 +08002495 else
Guy Cohen39e88502008-04-23 17:14:57 -07002496 lq_sta->dbg_fixed_rate = 0;
Zhu Yi98d7e092007-09-27 11:27:42 +08002497
Guy Cohen39e88502008-04-23 17:14:57 -07002498 lq_sta->active_legacy_rate = 0x0FFF; /* 1 - 54 MBits, includes CCK */
2499 lq_sta->active_siso_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2500 lq_sta->active_mimo2_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2501 lq_sta->active_mimo3_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
Zhu Yi98d7e092007-09-27 11:27:42 +08002502
2503 IWL_DEBUG_RATE("sta_id %d rate 0x%X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07002504 lq_sta->lq.sta_id, lq_sta->dbg_fixed_rate);
Zhu Yi98d7e092007-09-27 11:27:42 +08002505
Guy Cohen39e88502008-04-23 17:14:57 -07002506 if (lq_sta->dbg_fixed_rate) {
Guy Cohen07bc28e2008-04-23 17:15:03 -07002507 rs_fill_link_cmd(NULL, lq_sta, lq_sta->dbg_fixed_rate);
Tomas Winkler66c73db2008-04-15 16:01:40 -07002508 iwl_send_lq_cmd(lq_sta->drv, &lq_sta->lq, CMD_ASYNC);
Zhu Yi98d7e092007-09-27 11:27:42 +08002509 }
2510
2511 return count;
2512}
Zhu Yi0209dc12007-09-27 11:27:43 +08002513
Zhu Yi5ae212c2007-09-27 11:27:38 +08002514static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
2515 char __user *user_buf, size_t count, loff_t *ppos)
2516{
2517 char buff[1024];
2518 int desc = 0;
2519 int i = 0;
2520
Tomas Winklerc33104f2008-01-14 17:46:21 -08002521 struct iwl4965_lq_sta *lq_sta = file->private_data;
Zhu Yi5ae212c2007-09-27 11:27:38 +08002522
Tomas Winklerc33104f2008-01-14 17:46:21 -08002523 desc += sprintf(buff+desc, "sta_id %d\n", lq_sta->lq.sta_id);
Zhu Yi98d7e092007-09-27 11:27:42 +08002524 desc += sprintf(buff+desc, "failed=%d success=%d rate=0%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002525 lq_sta->total_failed, lq_sta->total_success,
Guy Cohen39e88502008-04-23 17:14:57 -07002526 lq_sta->active_legacy_rate);
Zhu Yi98d7e092007-09-27 11:27:42 +08002527 desc += sprintf(buff+desc, "fixed rate 0x%X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07002528 lq_sta->dbg_fixed_rate);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002529 desc += sprintf(buff+desc, "general:"
2530 "flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002531 lq_sta->lq.general_params.flags,
2532 lq_sta->lq.general_params.mimo_delimiter,
2533 lq_sta->lq.general_params.single_stream_ant_msk,
2534 lq_sta->lq.general_params.dual_stream_ant_msk);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002535
2536 desc += sprintf(buff+desc, "agg:"
2537 "time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002538 le16_to_cpu(lq_sta->lq.agg_params.agg_time_limit),
2539 lq_sta->lq.agg_params.agg_dis_start_th,
2540 lq_sta->lq.agg_params.agg_frame_cnt_limit);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002541
2542 desc += sprintf(buff+desc,
2543 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002544 lq_sta->lq.general_params.start_rate_index[0],
2545 lq_sta->lq.general_params.start_rate_index[1],
2546 lq_sta->lq.general_params.start_rate_index[2],
2547 lq_sta->lq.general_params.start_rate_index[3]);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002548
2549
2550 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
2551 desc += sprintf(buff+desc, " rate[%d] 0x%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002552 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags));
Zhu Yi5ae212c2007-09-27 11:27:38 +08002553
2554 return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2555}
2556
2557static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
Zhu Yi98d7e092007-09-27 11:27:42 +08002558 .write = rs_sta_dbgfs_scale_table_write,
Zhu Yi5ae212c2007-09-27 11:27:38 +08002559 .read = rs_sta_dbgfs_scale_table_read,
2560 .open = open_file_generic,
2561};
Zhu Yi0209dc12007-09-27 11:27:43 +08002562static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
2563 char __user *user_buf, size_t count, loff_t *ppos)
2564{
2565 char buff[1024];
2566 int desc = 0;
2567 int i, j;
2568
Tomas Winklerc33104f2008-01-14 17:46:21 -08002569 struct iwl4965_lq_sta *lq_sta = file->private_data;
Zhu Yi0209dc12007-09-27 11:27:43 +08002570 for (i = 0; i < LQ_SIZE; i++) {
2571 desc += sprintf(buff+desc, "%s type=%d SGI=%d FAT=%d DUP=%d\n"
2572 "rate=0x%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002573 lq_sta->active_tbl == i?"*":"x",
2574 lq_sta->lq_info[i].lq_type,
2575 lq_sta->lq_info[i].is_SGI,
2576 lq_sta->lq_info[i].is_fat,
2577 lq_sta->lq_info[i].is_dup,
Guy Cohen39e88502008-04-23 17:14:57 -07002578 lq_sta->lq_info[i].current_rate);
Zhu Yi0209dc12007-09-27 11:27:43 +08002579 for (j = 0; j < IWL_RATE_COUNT; j++) {
2580 desc += sprintf(buff+desc,
Tomas Winklerc33104f2008-01-14 17:46:21 -08002581 "counter=%d success=%d %%=%d\n",
2582 lq_sta->lq_info[i].win[j].counter,
2583 lq_sta->lq_info[i].win[j].success_counter,
2584 lq_sta->lq_info[i].win[j].success_ratio);
Zhu Yi0209dc12007-09-27 11:27:43 +08002585 }
2586 }
2587 return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2588}
2589
2590static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
2591 .read = rs_sta_dbgfs_stats_table_read,
2592 .open = open_file_generic,
2593};
Zhu Yi5ae212c2007-09-27 11:27:38 +08002594
Zhu Yi93dc6462007-09-27 11:27:37 +08002595static void rs_add_debugfs(void *priv, void *priv_sta,
2596 struct dentry *dir)
2597{
Tomas Winklerc33104f2008-01-14 17:46:21 -08002598 struct iwl4965_lq_sta *lq_sta = priv_sta;
2599 lq_sta->rs_sta_dbgfs_scale_table_file =
Zhu Yi0209dc12007-09-27 11:27:43 +08002600 debugfs_create_file("rate_scale_table", 0600, dir,
Tomas Winklerc33104f2008-01-14 17:46:21 -08002601 lq_sta, &rs_sta_dbgfs_scale_table_ops);
2602 lq_sta->rs_sta_dbgfs_stats_table_file =
Zhu Yi0209dc12007-09-27 11:27:43 +08002603 debugfs_create_file("rate_stats_table", 0600, dir,
Tomas Winklerc33104f2008-01-14 17:46:21 -08002604 lq_sta, &rs_sta_dbgfs_stats_table_ops);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002605#ifdef CONFIG_IWL4965_HT
2606 lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file =
2607 debugfs_create_u8("tx_agg_tid_enable", 0600, dir,
2608 &lq_sta->tx_agg_tid_en);
2609#endif
2610
Zhu Yi93dc6462007-09-27 11:27:37 +08002611}
2612
2613static void rs_remove_debugfs(void *priv, void *priv_sta)
2614{
Tomas Winklerc33104f2008-01-14 17:46:21 -08002615 struct iwl4965_lq_sta *lq_sta = priv_sta;
2616 debugfs_remove(lq_sta->rs_sta_dbgfs_scale_table_file);
2617 debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002618#ifdef CONFIG_IWL4965_HT
2619 debugfs_remove(lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file);
2620#endif
Zhu Yi93dc6462007-09-27 11:27:37 +08002621}
2622#endif
2623
Zhu Yib481de92007-09-25 17:54:57 -07002624static struct rate_control_ops rs_ops = {
2625 .module = NULL,
2626 .name = RS_NAME,
2627 .tx_status = rs_tx_status,
2628 .get_rate = rs_get_rate,
2629 .rate_init = rs_rate_init,
2630 .clear = rs_clear,
2631 .alloc = rs_alloc,
2632 .free = rs_free,
2633 .alloc_sta = rs_alloc_sta,
2634 .free_sta = rs_free_sta,
Zhu Yi93dc6462007-09-27 11:27:37 +08002635#ifdef CONFIG_MAC80211_DEBUGFS
2636 .add_sta_debugfs = rs_add_debugfs,
2637 .remove_sta_debugfs = rs_remove_debugfs,
2638#endif
Zhu Yib481de92007-09-25 17:54:57 -07002639};
2640
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002641int iwl4965_fill_rs_info(struct ieee80211_hw *hw, char *buf, u8 sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07002642{
2643 struct ieee80211_local *local = hw_to_local(hw);
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002644 struct iwl_priv *priv = hw->priv;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002645 struct iwl4965_lq_sta *lq_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002646 struct sta_info *sta;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002647 int cnt = 0, i;
Zhu Yib481de92007-09-25 17:54:57 -07002648 u32 samples = 0, success = 0, good = 0;
2649 unsigned long now = jiffies;
2650 u32 max_time = 0;
2651 u8 lq_type, antenna;
2652
Johannes Bergd0709a62008-02-25 16:27:46 +01002653 rcu_read_lock();
2654
Zhu Yib481de92007-09-25 17:54:57 -07002655 sta = sta_info_get(local, priv->stations[sta_id].sta.sta.addr);
2656 if (!sta || !sta->rate_ctrl_priv) {
Johannes Bergd0709a62008-02-25 16:27:46 +01002657 if (sta)
Zhu Yib481de92007-09-25 17:54:57 -07002658 IWL_DEBUG_RATE("leave - no private rate data!\n");
Johannes Bergd0709a62008-02-25 16:27:46 +01002659 else
Zhu Yib481de92007-09-25 17:54:57 -07002660 IWL_DEBUG_RATE("leave - no station!\n");
Johannes Bergd0709a62008-02-25 16:27:46 +01002661 rcu_read_unlock();
Zhu Yib481de92007-09-25 17:54:57 -07002662 return sprintf(buf, "station %d not found\n", sta_id);
2663 }
2664
Tomas Winklerc33104f2008-01-14 17:46:21 -08002665 lq_sta = (void *)sta->rate_ctrl_priv;
Zhu Yib481de92007-09-25 17:54:57 -07002666
Tomas Winklerc33104f2008-01-14 17:46:21 -08002667 lq_type = lq_sta->lq_info[lq_sta->active_tbl].lq_type;
Guy Cohenfde0db32008-04-21 15:42:01 -07002668 antenna = lq_sta->lq_info[lq_sta->active_tbl].ant_type;
Zhu Yib481de92007-09-25 17:54:57 -07002669
2670 if (is_legacy(lq_type))
2671 i = IWL_RATE_54M_INDEX;
2672 else
2673 i = IWL_RATE_60M_INDEX;
2674 while (1) {
2675 u64 mask;
2676 int j;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002677 int active = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002678
Tomas Winklerc33104f2008-01-14 17:46:21 -08002679 cnt +=
2680 sprintf(&buf[cnt], " %2dMbs: ", iwl4965_rates[i].ieee / 2);
Zhu Yib481de92007-09-25 17:54:57 -07002681
2682 mask = (1ULL << (IWL_RATE_MAX_WINDOW - 1));
2683 for (j = 0; j < IWL_RATE_MAX_WINDOW; j++, mask >>= 1)
Tomas Winklerc33104f2008-01-14 17:46:21 -08002684 buf[cnt++] =
2685 (lq_sta->lq_info[active].win[i].data & mask)
Zhu Yib481de92007-09-25 17:54:57 -07002686 ? '1' : '0';
2687
Tomas Winklerc33104f2008-01-14 17:46:21 -08002688 samples += lq_sta->lq_info[active].win[i].counter;
2689 good += lq_sta->lq_info[active].win[i].success_counter;
2690 success += lq_sta->lq_info[active].win[i].success_counter *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002691 iwl4965_rates[i].ieee;
Zhu Yib481de92007-09-25 17:54:57 -07002692
Tomas Winklerc33104f2008-01-14 17:46:21 -08002693 if (lq_sta->lq_info[active].win[i].stamp) {
Zhu Yib481de92007-09-25 17:54:57 -07002694 int delta =
2695 jiffies_to_msecs(now -
Tomas Winklerc33104f2008-01-14 17:46:21 -08002696 lq_sta->lq_info[active].win[i].stamp);
Zhu Yib481de92007-09-25 17:54:57 -07002697
2698 if (delta > max_time)
2699 max_time = delta;
2700
Tomas Winklerc33104f2008-01-14 17:46:21 -08002701 cnt += sprintf(&buf[cnt], "%5dms\n", delta);
Zhu Yib481de92007-09-25 17:54:57 -07002702 } else
Tomas Winklerc33104f2008-01-14 17:46:21 -08002703 buf[cnt++] = '\n';
Zhu Yib481de92007-09-25 17:54:57 -07002704
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002705 j = iwl4965_get_prev_ieee_rate(i);
Zhu Yib481de92007-09-25 17:54:57 -07002706 if (j == i)
2707 break;
2708 i = j;
2709 }
2710
2711 /* Display the average rate of all samples taken.
2712 *
Ben Cahill77626352007-11-29 11:09:44 +08002713 * NOTE: We multiply # of samples by 2 since the IEEE measurement
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002714 * added from iwl4965_rates is actually 2X the rate */
Zhu Yib481de92007-09-25 17:54:57 -07002715 if (samples)
Tomas Winklerc33104f2008-01-14 17:46:21 -08002716 cnt += sprintf(&buf[cnt],
Zhu Yib481de92007-09-25 17:54:57 -07002717 "\nAverage rate is %3d.%02dMbs over last %4dms\n"
2718 "%3d%% success (%d good packets over %d tries)\n",
2719 success / (2 * samples), (success * 5 / samples) % 10,
2720 max_time, good * 100 / samples, good, samples);
2721 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08002722 cnt += sprintf(&buf[cnt], "\nAverage rate: 0Mbs\n");
2723
2724 cnt += sprintf(&buf[cnt], "\nrate scale type %d antenna %d "
Zhu Yib481de92007-09-25 17:54:57 -07002725 "active_search %d rate index %d\n", lq_type, antenna,
Johannes Berg8318d782008-01-24 19:38:38 +01002726 lq_sta->search_better_tbl, sta->last_txrate_idx);
Zhu Yib481de92007-09-25 17:54:57 -07002727
Johannes Bergd0709a62008-02-25 16:27:46 +01002728 rcu_read_unlock();
Tomas Winklerc33104f2008-01-14 17:46:21 -08002729 return cnt;
Zhu Yib481de92007-09-25 17:54:57 -07002730}
2731
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002732void iwl4965_rate_scale_init(struct ieee80211_hw *hw, s32 sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07002733{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002734 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07002735
2736 priv->lq_mngr.lq_ready = 1;
2737}
2738
Reinette Chatre897e1cf2008-03-28 16:21:09 -07002739int iwl4965_rate_control_register(void)
Zhu Yib481de92007-09-25 17:54:57 -07002740{
Reinette Chatre897e1cf2008-03-28 16:21:09 -07002741 return ieee80211_rate_control_register(&rs_ops);
Zhu Yib481de92007-09-25 17:54:57 -07002742}
2743
Reinette Chatre897e1cf2008-03-28 16:21:09 -07002744void iwl4965_rate_control_unregister(void)
Zhu Yib481de92007-09-25 17:54:57 -07002745{
2746 ieee80211_rate_control_unregister(&rs_ops);
2747}
2748