blob: 4063534b64c8ad5ed2bce8dcad81067f67edd94d [file] [log] [blame]
Zhu Yib481de92007-09-25 17:54:57 -07001/******************************************************************************
2 *
Reinette Chatreeb7ae892008-03-11 16:17:17 -07003 * Copyright(c) 2005 - 2008 Intel Corporation. All rights reserved.
Zhu Yib481de92007-09-25 17:54:57 -07004 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
17 *
18 * The full GNU General Public License is included in this distribution in the
19 * file called LICENSE.
20 *
21 * Contact Information:
22 * James P. Ketrenos <ipw2100-admin@linux.intel.com>
23 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24 *
25 *****************************************************************************/
26#include <linux/kernel.h>
27#include <linux/init.h>
28#include <linux/skbuff.h>
29#include <linux/wireless.h>
30#include <net/mac80211.h>
Zhu Yib481de92007-09-25 17:54:57 -070031
32#include <linux/netdevice.h>
33#include <linux/etherdevice.h>
34#include <linux/delay.h>
35
36#include <linux/workqueue.h>
37
Johannes Berg2c8dccc2008-04-08 15:14:40 -040038#include "../net/mac80211/rate.h"
Zhu Yib481de92007-09-25 17:54:57 -070039
Christoph Hellwig5d08cd12007-10-25 17:15:50 +080040#include "iwl-4965.h"
Tomas Winkler857485c2008-03-21 13:53:44 -070041#include "iwl-core.h"
Zhu Yib481de92007-09-25 17:54:57 -070042#include "iwl-helpers.h"
43
44#define RS_NAME "iwl-4965-rs"
45
46#define NUM_TRY_BEFORE_ANTENNA_TOGGLE 1
47#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
Ben Cahill77626352007-11-29 11:09:44 +080067/**
68 * struct iwl4965_rate_scale_data -- tx success history for one rate
69 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080070struct iwl4965_rate_scale_data {
Ben Cahill77626352007-11-29 11:09:44 +080071 u64 data; /* bitmap of successful frames */
72 s32 success_counter; /* number of frames successful */
73 s32 success_ratio; /* per-cent * 128 */
74 s32 counter; /* number of frames attempted */
75 s32 average_tpt; /* success ratio * expected throughput */
Zhu Yib481de92007-09-25 17:54:57 -070076 unsigned long stamp;
77};
78
Ben Cahill77626352007-11-29 11:09:44 +080079/**
80 * struct iwl4965_scale_tbl_info -- tx params and success history for all rates
81 *
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +020082 * There are two of these in struct iwl4965_lq_sta,
Ben Cahill77626352007-11-29 11:09:44 +080083 * one for "active", and one for "search".
84 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080085struct iwl4965_scale_tbl_info {
Guy Cohenfde0db32008-04-21 15:42:01 -070086 enum iwl_table_type lq_type;
87 u8 ant_type;
Ben Cahill77626352007-11-29 11:09:44 +080088 u8 is_SGI; /* 1 = short guard interval */
89 u8 is_fat; /* 1 = 40 MHz channel width */
90 u8 is_dup; /* 1 = duplicated data streams */
91 u8 action; /* change modulation; IWL_[LEGACY/SISO/MIMO]_SWITCH_* */
92 s32 *expected_tpt; /* throughput metrics; expected_tpt_G, etc. */
Guy Cohen39e88502008-04-23 17:14:57 -070093 u32 current_rate; /* rate_n_flags, uCode API format */
Ben Cahill77626352007-11-29 11:09:44 +080094 struct iwl4965_rate_scale_data win[IWL_RATE_COUNT]; /* rate histories */
Zhu Yib481de92007-09-25 17:54:57 -070095};
96
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +020097#ifdef CONFIG_IWL4965_HT
98
99struct iwl4965_traffic_load {
100 unsigned long time_stamp; /* age of the oldest statistics */
101 u32 packet_count[TID_QUEUE_MAX_SIZE]; /* packet count in this time
102 * slice */
103 u32 total; /* total num of packets during the
104 * last TID_MAX_TIME_DIFF */
105 u8 queue_count; /* number of queues that has
106 * been used since the last cleanup */
107 u8 head; /* start of the circular buffer */
108};
109
110#endif /* CONFIG_IWL4965_HT */
111
Ben Cahill77626352007-11-29 11:09:44 +0800112/**
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200113 * struct iwl4965_lq_sta -- driver's rate scaling private structure
Ben Cahill77626352007-11-29 11:09:44 +0800114 *
115 * Pointer to this gets passed back and forth between driver and mac80211.
116 */
Tomas Winklerc33104f2008-01-14 17:46:21 -0800117struct iwl4965_lq_sta {
Ben Cahill77626352007-11-29 11:09:44 +0800118 u8 active_tbl; /* index of active table, range 0-1 */
119 u8 enable_counter; /* indicates HT mode */
120 u8 stay_in_tbl; /* 1: disallow, 0: allow search for new mode */
121 u8 search_better_tbl; /* 1: currently trying alternate mode */
Zhu Yib481de92007-09-25 17:54:57 -0700122 s32 last_tpt;
Ben Cahill77626352007-11-29 11:09:44 +0800123
124 /* The following determine when to search for a new mode */
Zhu Yib481de92007-09-25 17:54:57 -0700125 u32 table_count_limit;
Ben Cahill77626352007-11-29 11:09:44 +0800126 u32 max_failure_limit; /* # failed frames before new search */
127 u32 max_success_limit; /* # successful frames before new search */
Zhu Yib481de92007-09-25 17:54:57 -0700128 u32 table_count;
Ben Cahill77626352007-11-29 11:09:44 +0800129 u32 total_failed; /* total failed frames, any/all rates */
130 u32 total_success; /* total successful frames, any/all rates */
131 u32 flush_timer; /* time staying in mode before new search */
132
133 u8 action_counter; /* # mode-switch actions tried */
Zhu Yib481de92007-09-25 17:54:57 -0700134 u8 antenna;
135 u8 valid_antenna;
136 u8 is_green;
137 u8 is_dup;
Johannes Berg8318d782008-01-24 19:38:38 +0100138 enum ieee80211_band band;
Zhu Yib481de92007-09-25 17:54:57 -0700139 u8 ibss_sta_added;
Ben Cahill77626352007-11-29 11:09:44 +0800140
141 /* The following are bitmaps of rates; IWL_RATE_6M_MASK, etc. */
Zhu Yi02dede02007-09-27 11:27:40 +0800142 u32 supp_rates;
Guy Cohen39e88502008-04-23 17:14:57 -0700143 u16 active_legacy_rate;
Zhu Yib481de92007-09-25 17:54:57 -0700144 u16 active_siso_rate;
Guy Cohenfde0db32008-04-21 15:42:01 -0700145 u16 active_mimo2_rate;
146 u16 active_mimo3_rate;
Zhu Yib481de92007-09-25 17:54:57 -0700147 u16 active_rate_basic;
Ben Cahill77626352007-11-29 11:09:44 +0800148
Tomas Winkler66c73db2008-04-15 16:01:40 -0700149 struct iwl_link_quality_cmd lq;
Ben Cahill77626352007-11-29 11:09:44 +0800150 struct iwl4965_scale_tbl_info lq_info[LQ_SIZE]; /* "active", "search" */
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200151#ifdef CONFIG_IWL4965_HT
152 struct iwl4965_traffic_load load[TID_MAX_LOAD_COUNT];
153 u8 tx_agg_tid_en;
154#endif
Zhu Yi5ae212c2007-09-27 11:27:38 +0800155#ifdef CONFIG_MAC80211_DEBUGFS
Zhu Yi98d7e092007-09-27 11:27:42 +0800156 struct dentry *rs_sta_dbgfs_scale_table_file;
Zhu Yi0209dc12007-09-27 11:27:43 +0800157 struct dentry *rs_sta_dbgfs_stats_table_file;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200158#ifdef CONFIG_IWL4965_HT
159 struct dentry *rs_sta_dbgfs_tx_agg_tid_en_file;
160#endif
Guy Cohen39e88502008-04-23 17:14:57 -0700161 u32 dbg_fixed_rate;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700162 struct iwl_priv *drv;
Zhu Yi5ae212c2007-09-27 11:27:38 +0800163#endif
Zhu Yib481de92007-09-25 17:54:57 -0700164};
165
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700166static void rs_rate_scale_perform(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -0700167 struct net_device *dev,
168 struct ieee80211_hdr *hdr,
169 struct sta_info *sta);
Guy Cohenfde0db32008-04-21 15:42:01 -0700170static void rs_fill_link_cmd(const struct iwl_priv *priv,
171 struct iwl4965_lq_sta *lq_sta,
Guy Cohen39e88502008-04-23 17:14:57 -0700172 u32 rate_n_flags,
Tomas Winkler66c73db2008-04-15 16:01:40 -0700173 struct iwl_link_quality_cmd *tbl);
Zhu Yib481de92007-09-25 17:54:57 -0700174
175
Zhu Yi98d7e092007-09-27 11:27:42 +0800176#ifdef CONFIG_MAC80211_DEBUGFS
Tomas Winklerc33104f2008-01-14 17:46:21 -0800177static void rs_dbgfs_set_mcs(struct iwl4965_lq_sta *lq_sta,
Guy Cohen39e88502008-04-23 17:14:57 -0700178 u32 *rate_n_flags, int index);
Zhu Yi98d7e092007-09-27 11:27:42 +0800179#else
Tomas Winklerc33104f2008-01-14 17:46:21 -0800180static void rs_dbgfs_set_mcs(struct iwl4965_lq_sta *lq_sta,
Guy Cohen39e88502008-04-23 17:14:57 -0700181 u32 *rate_n_flags, int index)
Zhu Yi98d7e092007-09-27 11:27:42 +0800182{}
183#endif
Ben Cahill77626352007-11-29 11:09:44 +0800184
185/*
186 * Expected throughput metrics for following rates:
187 * 1, 2, 5.5, 11, 6, 9, 12, 18, 24, 36, 48, 54, 60 MBits
188 * "G" is the only table that supports CCK (the first 4 rates).
189 */
Guy Cohenfde0db32008-04-21 15:42:01 -0700190/*FIXME:RS:need to spearate tables for MIMO2/MIMO3*/
Zhu Yib481de92007-09-25 17:54:57 -0700191static s32 expected_tpt_A[IWL_RATE_COUNT] = {
192 0, 0, 0, 0, 40, 57, 72, 98, 121, 154, 177, 186, 186
193};
194
195static s32 expected_tpt_G[IWL_RATE_COUNT] = {
196 7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 186
197};
198
199static s32 expected_tpt_siso20MHz[IWL_RATE_COUNT] = {
200 0, 0, 0, 0, 42, 42, 76, 102, 124, 159, 183, 193, 202
201};
202
203static s32 expected_tpt_siso20MHzSGI[IWL_RATE_COUNT] = {
204 0, 0, 0, 0, 46, 46, 82, 110, 132, 168, 192, 202, 211
205};
206
207static s32 expected_tpt_mimo20MHz[IWL_RATE_COUNT] = {
208 0, 0, 0, 0, 74, 74, 123, 155, 179, 214, 236, 244, 251
209};
210
211static s32 expected_tpt_mimo20MHzSGI[IWL_RATE_COUNT] = {
212 0, 0, 0, 0, 81, 81, 131, 164, 188, 222, 243, 251, 257
213};
214
215static s32 expected_tpt_siso40MHz[IWL_RATE_COUNT] = {
216 0, 0, 0, 0, 77, 77, 127, 160, 184, 220, 242, 250, 257
217};
218
219static s32 expected_tpt_siso40MHzSGI[IWL_RATE_COUNT] = {
220 0, 0, 0, 0, 83, 83, 135, 169, 193, 229, 250, 257, 264
221};
222
223static s32 expected_tpt_mimo40MHz[IWL_RATE_COUNT] = {
224 0, 0, 0, 0, 123, 123, 182, 214, 235, 264, 279, 285, 289
225};
226
227static s32 expected_tpt_mimo40MHzSGI[IWL_RATE_COUNT] = {
228 0, 0, 0, 0, 131, 131, 191, 222, 242, 270, 284, 289, 293
229};
230
Guy Cohen39e88502008-04-23 17:14:57 -0700231static inline u8 rs_extract_rate(u32 rate_n_flags)
Zhu Yib481de92007-09-25 17:54:57 -0700232{
233 return (u8)(rate_n_flags & 0xFF);
234}
235
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800236static void rs_rate_scale_clear_window(struct iwl4965_rate_scale_data *window)
Zhu Yib481de92007-09-25 17:54:57 -0700237{
238 window->data = 0;
239 window->success_counter = 0;
240 window->success_ratio = IWL_INVALID_VALUE;
241 window->counter = 0;
242 window->average_tpt = IWL_INVALID_VALUE;
243 window->stamp = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700244}
245
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200246#ifdef CONFIG_IWL4965_HT
247/*
248 * removes the old data from the statistics. All data that is older than
249 * TID_MAX_TIME_DIFF, will be deleted.
250 */
251static void rs_tl_rm_old_stats(struct iwl4965_traffic_load *tl, u32 curr_time)
252{
253 /* The oldest age we want to keep */
254 u32 oldest_time = curr_time - TID_MAX_TIME_DIFF;
255
256 while (tl->queue_count &&
257 (tl->time_stamp < oldest_time)) {
258 tl->total -= tl->packet_count[tl->head];
259 tl->packet_count[tl->head] = 0;
260 tl->time_stamp += TID_QUEUE_CELL_SPACING;
261 tl->queue_count--;
262 tl->head++;
263 if (tl->head >= TID_QUEUE_MAX_SIZE)
264 tl->head = 0;
265 }
266}
267
268/*
269 * increment traffic load value for tid and also remove
270 * any old values if passed the certain time period
271 */
272static void rs_tl_add_packet(struct iwl4965_lq_sta *lq_data, u8 tid)
273{
274 u32 curr_time = jiffies_to_msecs(jiffies);
275 u32 time_diff;
276 s32 index;
277 struct iwl4965_traffic_load *tl = NULL;
278
279 if (tid >= TID_MAX_LOAD_COUNT)
280 return;
281
282 tl = &lq_data->load[tid];
283
284 curr_time -= curr_time % TID_ROUND_VALUE;
285
286 /* Happens only for the first packet. Initialize the data */
287 if (!(tl->queue_count)) {
288 tl->total = 1;
289 tl->time_stamp = curr_time;
290 tl->queue_count = 1;
291 tl->head = 0;
292 tl->packet_count[0] = 1;
293 return;
294 }
295
296 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
297 index = time_diff / TID_QUEUE_CELL_SPACING;
298
299 /* The history is too long: remove data that is older than */
300 /* TID_MAX_TIME_DIFF */
301 if (index >= TID_QUEUE_MAX_SIZE)
302 rs_tl_rm_old_stats(tl, curr_time);
303
304 index = (tl->head + index) % TID_QUEUE_MAX_SIZE;
305 tl->packet_count[index] = tl->packet_count[index] + 1;
306 tl->total = tl->total + 1;
307
308 if ((index + 1) > tl->queue_count)
309 tl->queue_count = index + 1;
310}
311
312/*
313 get the traffic load value for tid
314*/
315static u32 rs_tl_get_load(struct iwl4965_lq_sta *lq_data, u8 tid)
316{
317 u32 curr_time = jiffies_to_msecs(jiffies);
318 u32 time_diff;
319 s32 index;
320 struct iwl4965_traffic_load *tl = NULL;
321
322 if (tid >= TID_MAX_LOAD_COUNT)
323 return 0;
324
325 tl = &(lq_data->load[tid]);
326
327 curr_time -= curr_time % TID_ROUND_VALUE;
328
329 if (!(tl->queue_count))
330 return 0;
331
332 time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
333 index = time_diff / TID_QUEUE_CELL_SPACING;
334
335 /* The history is too long: remove data that is older than */
336 /* TID_MAX_TIME_DIFF */
337 if (index >= TID_QUEUE_MAX_SIZE)
338 rs_tl_rm_old_stats(tl, curr_time);
339
340 return tl->total;
341}
342
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700343static void rs_tl_turn_on_agg_for_tid(struct iwl_priv *priv,
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200344 struct iwl4965_lq_sta *lq_data, u8 tid,
345 struct sta_info *sta)
346{
347 unsigned long state;
348 DECLARE_MAC_BUF(mac);
349
350 spin_lock_bh(&sta->ampdu_mlme.ampdu_tx);
Ron Rindjunskycee24a32008-03-26 20:36:03 +0200351 state = sta->ampdu_mlme.tid_state_tx[tid];
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200352 spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
353
354 if (state == HT_AGG_STATE_IDLE &&
355 rs_tl_get_load(lq_data, tid) > IWL_AGG_LOAD_THRESHOLD) {
356 IWL_DEBUG_HT("Starting Tx agg: STA: %s tid: %d\n",
357 print_mac(mac, sta->addr), tid);
358 ieee80211_start_tx_ba_session(priv->hw, sta->addr, tid);
359 }
360}
361
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700362static void rs_tl_turn_on_agg(struct iwl_priv *priv, u8 tid,
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +0200363 struct iwl4965_lq_sta *lq_data,
364 struct sta_info *sta)
365{
366 if ((tid < TID_MAX_LOAD_COUNT))
367 rs_tl_turn_on_agg_for_tid(priv, lq_data, tid, sta);
368 else if (tid == IWL_AGG_ALL_TID)
369 for (tid = 0; tid < TID_MAX_LOAD_COUNT; tid++)
370 rs_tl_turn_on_agg_for_tid(priv, lq_data, tid, sta);
371}
372
373#endif /* CONFIG_IWLWIFI_HT */
374
Guy Cohen39e88502008-04-23 17:14:57 -0700375static inline int get_num_of_ant_from_rate(u32 rate_n_flags)
Guy Cohenfde0db32008-04-21 15:42:01 -0700376{
Guy Cohen39e88502008-04-23 17:14:57 -0700377 return (!!(rate_n_flags & RATE_MCS_ANT_A_MSK) +
378 !!(rate_n_flags & RATE_MCS_ANT_B_MSK) +
379 !!(rate_n_flags & RATE_MCS_ANT_C_MSK));
Guy Cohenfde0db32008-04-21 15:42:01 -0700380}
381
Ben Cahill77626352007-11-29 11:09:44 +0800382/**
383 * rs_collect_tx_data - Update the success/failure sliding window
384 *
385 * We keep a sliding window of the last 62 packets transmitted
386 * at this rate. window->data contains the bitmask of successful
387 * packets.
388 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800389static int rs_collect_tx_data(struct iwl4965_rate_scale_data *windows,
Ron Rindjunsky99556432008-01-28 14:07:25 +0200390 int scale_index, s32 tpt, int retries,
391 int successes)
Zhu Yib481de92007-09-25 17:54:57 -0700392{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800393 struct iwl4965_rate_scale_data *window = NULL;
Guy Cohen39e88502008-04-23 17:14:57 -0700394 static const u64 mask = (((u64)1) << (IWL_RATE_MAX_WINDOW - 1));
Zhu Yib481de92007-09-25 17:54:57 -0700395 s32 fail_count;
396
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800397 if (scale_index < 0 || scale_index >= IWL_RATE_COUNT)
398 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -0700399
Ben Cahill77626352007-11-29 11:09:44 +0800400 /* Select data for current tx bit rate */
Zhu Yib481de92007-09-25 17:54:57 -0700401 window = &(windows[scale_index]);
402
Ben Cahill77626352007-11-29 11:09:44 +0800403 /*
404 * Keep track of only the latest 62 tx frame attempts in this rate's
405 * history window; anything older isn't really relevant any more.
406 * If we have filled up the sliding window, drop the oldest attempt;
407 * if the oldest attempt (highest bit in bitmap) shows "success",
408 * subtract "1" from the success counter (this is the main reason
409 * we keep these bitmaps!).
410 */
Ron Rindjunsky99556432008-01-28 14:07:25 +0200411 while (retries > 0) {
Guy Cohen39e88502008-04-23 17:14:57 -0700412 if (window->counter >= IWL_RATE_MAX_WINDOW) {
413
414 /* remove earliest */
415 window->counter = IWL_RATE_MAX_WINDOW - 1;
416
Ron Rindjunsky99556432008-01-28 14:07:25 +0200417 if (window->data & mask) {
418 window->data &= ~mask;
Guy Cohen39e88502008-04-23 17:14:57 -0700419 window->success_counter--;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200420 }
Zhu Yib481de92007-09-25 17:54:57 -0700421 }
Zhu Yib481de92007-09-25 17:54:57 -0700422
Ron Rindjunsky99556432008-01-28 14:07:25 +0200423 /* Increment frames-attempted counter */
424 window->counter++;
Ben Cahill77626352007-11-29 11:09:44 +0800425
Ron Rindjunsky99556432008-01-28 14:07:25 +0200426 /* Shift bitmap by one frame (throw away oldest history),
427 * OR in "1", and increment "success" if this
428 * frame was successful. */
Guy Cohen39e88502008-04-23 17:14:57 -0700429 window->data <<= 1;;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200430 if (successes > 0) {
Guy Cohen39e88502008-04-23 17:14:57 -0700431 window->success_counter++;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200432 window->data |= 0x1;
433 successes--;
434 }
435
436 retries--;
Zhu Yib481de92007-09-25 17:54:57 -0700437 }
438
Ben Cahill77626352007-11-29 11:09:44 +0800439 /* Calculate current success ratio, avoid divide-by-0! */
Zhu Yib481de92007-09-25 17:54:57 -0700440 if (window->counter > 0)
441 window->success_ratio = 128 * (100 * window->success_counter)
442 / window->counter;
443 else
444 window->success_ratio = IWL_INVALID_VALUE;
445
446 fail_count = window->counter - window->success_counter;
447
Ben Cahill77626352007-11-29 11:09:44 +0800448 /* Calculate average throughput, if we have enough history. */
Zhu Yib481de92007-09-25 17:54:57 -0700449 if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
450 (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
451 window->average_tpt = (window->success_ratio * tpt + 64) / 128;
452 else
453 window->average_tpt = IWL_INVALID_VALUE;
454
Ben Cahill77626352007-11-29 11:09:44 +0800455 /* Tag this window as having been updated */
Zhu Yib481de92007-09-25 17:54:57 -0700456 window->stamp = jiffies;
457
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800458 return 0;
Zhu Yib481de92007-09-25 17:54:57 -0700459}
460
Ben Cahill77626352007-11-29 11:09:44 +0800461/*
462 * Fill uCode API rate_n_flags field, based on "search" or "active" table.
463 */
Guy Cohen39e88502008-04-23 17:14:57 -0700464/* FIXME:RS:remove this function and put the flags statically in the table */
465static u32 rate_n_flags_from_tbl(struct iwl4965_scale_tbl_info *tbl,
466 int index, u8 use_green)
Zhu Yib481de92007-09-25 17:54:57 -0700467{
Guy Cohen39e88502008-04-23 17:14:57 -0700468 u32 rate_n_flags = 0;
469
Zhu Yib481de92007-09-25 17:54:57 -0700470 if (is_legacy(tbl->lq_type)) {
Guy Cohen39e88502008-04-23 17:14:57 -0700471 rate_n_flags = iwl4965_rates[index].plcp;
Zhu Yib481de92007-09-25 17:54:57 -0700472 if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE)
Guy Cohen39e88502008-04-23 17:14:57 -0700473 rate_n_flags |= RATE_MCS_CCK_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700474
Guy Cohenfde0db32008-04-21 15:42:01 -0700475 } else if (is_Ht(tbl->lq_type)) {
476 if (index > IWL_LAST_OFDM_RATE) {
477 IWL_ERROR("invalid HT rate index %d\n", index);
Zhu Yib481de92007-09-25 17:54:57 -0700478 index = IWL_LAST_OFDM_RATE;
Guy Cohenfde0db32008-04-21 15:42:01 -0700479 }
Guy Cohen39e88502008-04-23 17:14:57 -0700480 rate_n_flags = RATE_MCS_HT_MSK;
Guy Cohenfde0db32008-04-21 15:42:01 -0700481
482 if (is_siso(tbl->lq_type))
Guy Cohen39e88502008-04-23 17:14:57 -0700483 rate_n_flags |= iwl4965_rates[index].plcp_siso;
Guy Cohenfde0db32008-04-21 15:42:01 -0700484 else if (is_mimo2(tbl->lq_type))
Guy Cohen39e88502008-04-23 17:14:57 -0700485 rate_n_flags |= iwl4965_rates[index].plcp_mimo2;
Guy Cohenfde0db32008-04-21 15:42:01 -0700486 else
Guy Cohen39e88502008-04-23 17:14:57 -0700487 rate_n_flags |= iwl4965_rates[index].plcp_mimo3;
Zhu Yib481de92007-09-25 17:54:57 -0700488 } else {
Guy Cohenfde0db32008-04-21 15:42:01 -0700489 IWL_ERROR("Invalid tbl->lq_type %d\n", tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -0700490 }
491
Guy Cohen39e88502008-04-23 17:14:57 -0700492 rate_n_flags |= ((tbl->ant_type << RATE_MCS_ANT_POS) &
Guy Cohenfde0db32008-04-21 15:42:01 -0700493 RATE_MCS_ANT_ABC_MSK);
Zhu Yib481de92007-09-25 17:54:57 -0700494
Guy Cohen39e88502008-04-23 17:14:57 -0700495 if (is_Ht(tbl->lq_type)) {
496 if (tbl->is_fat) {
497 if (tbl->is_dup)
498 rate_n_flags |= RATE_MCS_DUP_MSK;
499 else
500 rate_n_flags |= RATE_MCS_FAT_MSK;
501 }
502 if (tbl->is_SGI)
503 rate_n_flags |= RATE_MCS_SGI_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700504
Guy Cohen39e88502008-04-23 17:14:57 -0700505 if (use_green) {
506 rate_n_flags |= RATE_MCS_GF_MSK;
507 if (is_siso(tbl->lq_type) && tbl->is_SGI) {
508 rate_n_flags &= ~RATE_MCS_SGI_MSK;
509 IWL_ERROR("GF was set with SGI:SISO\n");
510 }
511 }
Zhu Yib481de92007-09-25 17:54:57 -0700512 }
Guy Cohen39e88502008-04-23 17:14:57 -0700513 return rate_n_flags;
Zhu Yib481de92007-09-25 17:54:57 -0700514}
515
Ben Cahill77626352007-11-29 11:09:44 +0800516/*
517 * Interpret uCode API's rate_n_flags format,
518 * fill "search" or "active" tx mode table.
519 */
Guy Cohen39e88502008-04-23 17:14:57 -0700520static int rs_get_tbl_info_from_mcs(const u32 rate_n_flags,
Johannes Berg8318d782008-01-24 19:38:38 +0100521 enum ieee80211_band band,
522 struct iwl4965_scale_tbl_info *tbl,
Zhu Yib481de92007-09-25 17:54:57 -0700523 int *rate_idx)
524{
Guy Cohen39e88502008-04-23 17:14:57 -0700525 u32 ant_msk = (rate_n_flags & RATE_MCS_ANT_ABC_MSK);
526 u8 num_of_ant = get_num_of_ant_from_rate(rate_n_flags);
527 u8 mcs;
Zhu Yib481de92007-09-25 17:54:57 -0700528
Guy Cohen39e88502008-04-23 17:14:57 -0700529 *rate_idx = iwl4965_hwrate_to_plcp_idx(rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -0700530
Guy Cohenfde0db32008-04-21 15:42:01 -0700531 if (*rate_idx == IWL_RATE_INVALID) {
Zhu Yib481de92007-09-25 17:54:57 -0700532 *rate_idx = -1;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800533 return -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -0700534 }
Ben Cahill77626352007-11-29 11:09:44 +0800535 tbl->is_SGI = 0; /* default legacy setup */
Zhu Yib481de92007-09-25 17:54:57 -0700536 tbl->is_fat = 0;
537 tbl->is_dup = 0;
Guy Cohenfde0db32008-04-21 15:42:01 -0700538 tbl->ant_type = (ant_msk >> RATE_MCS_ANT_POS);
539 tbl->lq_type = LQ_NONE;
Zhu Yib481de92007-09-25 17:54:57 -0700540
Ben Cahill77626352007-11-29 11:09:44 +0800541 /* legacy rate format */
Guy Cohen39e88502008-04-23 17:14:57 -0700542 if (!(rate_n_flags & RATE_MCS_HT_MSK)) {
Guy Cohenfde0db32008-04-21 15:42:01 -0700543 if (num_of_ant == 1) {
Johannes Berg8318d782008-01-24 19:38:38 +0100544 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700545 tbl->lq_type = LQ_A;
546 else
547 tbl->lq_type = LQ_G;
Zhu Yib481de92007-09-25 17:54:57 -0700548 }
Guy Cohenfde0db32008-04-21 15:42:01 -0700549 /* HT rate format */
Zhu Yib481de92007-09-25 17:54:57 -0700550 } else {
Guy Cohen39e88502008-04-23 17:14:57 -0700551 if (rate_n_flags & RATE_MCS_SGI_MSK)
Zhu Yib481de92007-09-25 17:54:57 -0700552 tbl->is_SGI = 1;
553
Guy Cohen39e88502008-04-23 17:14:57 -0700554 if ((rate_n_flags & RATE_MCS_FAT_MSK) ||
555 (rate_n_flags & RATE_MCS_DUP_MSK))
Zhu Yib481de92007-09-25 17:54:57 -0700556 tbl->is_fat = 1;
557
Guy Cohen39e88502008-04-23 17:14:57 -0700558 if (rate_n_flags & RATE_MCS_DUP_MSK)
Zhu Yib481de92007-09-25 17:54:57 -0700559 tbl->is_dup = 1;
Guy Cohenfde0db32008-04-21 15:42:01 -0700560
Guy Cohen39e88502008-04-23 17:14:57 -0700561 mcs = rs_extract_rate(rate_n_flags);
Guy Cohenfde0db32008-04-21 15:42:01 -0700562
Guy Cohen39e88502008-04-23 17:14:57 -0700563 /* SISO */
564 if (mcs <= IWL_RATE_SISO_60M_PLCP) {
Guy Cohenfde0db32008-04-21 15:42:01 -0700565 if (num_of_ant == 1)
566 tbl->lq_type = LQ_SISO; /*else NONE*/
567 /* MIMO2 */
Guy Cohen39e88502008-04-23 17:14:57 -0700568 } else if (mcs <= IWL_RATE_MIMO2_60M_PLCP) {
Guy Cohenfde0db32008-04-21 15:42:01 -0700569 if (num_of_ant == 2)
570 tbl->lq_type = LQ_MIMO2;
571 /* MIMO3 */
572 } else {
573 if (num_of_ant == 3)
574 tbl->lq_type = LQ_MIMO3;
575 }
Zhu Yib481de92007-09-25 17:54:57 -0700576 }
577 return 0;
578}
Guy Cohenfde0db32008-04-21 15:42:01 -0700579/* FIXME:RS: need to toggle also ANT_C, and also AB,AC,BC */
Guy Cohen39e88502008-04-23 17:14:57 -0700580static inline void rs_toggle_antenna(u32 *rate_n_flags,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800581 struct iwl4965_scale_tbl_info *tbl)
Zhu Yib481de92007-09-25 17:54:57 -0700582{
Guy Cohenfde0db32008-04-21 15:42:01 -0700583 tbl->ant_type ^= ANT_AB;
Guy Cohen39e88502008-04-23 17:14:57 -0700584 *rate_n_flags ^= (RATE_MCS_ANT_A_MSK|RATE_MCS_ANT_B_MSK);
Zhu Yib481de92007-09-25 17:54:57 -0700585}
586
Guy Cohen39e88502008-04-23 17:14:57 -0700587/* FIXME:RS: in 4965 we don't use greenfield at all */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700588static inline u8 rs_use_green(struct iwl_priv *priv,
Ron Rindjunsky270243a2007-11-26 16:14:41 +0200589 struct ieee80211_conf *conf)
Zhu Yib481de92007-09-25 17:54:57 -0700590{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800591#ifdef CONFIG_IWL4965_HT
Ron Rindjunsky270243a2007-11-26 16:14:41 +0200592 return ((conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) &&
593 priv->current_ht_config.is_green_field &&
594 !priv->current_ht_config.non_GF_STA_present);
Guy Cohen39e88502008-04-23 17:14:57 -0700595#else
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800596 return 0;
Guy Cohen39e88502008-04-23 17:14:57 -0700597#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -0700598}
599
600/**
601 * rs_get_supported_rates - get the available rates
602 *
603 * if management frame or broadcast frame only return
604 * basic available rates.
605 *
606 */
Guy Coheneecd6e52008-04-23 17:14:58 -0700607static u16 rs_get_supported_rates(struct iwl4965_lq_sta *lq_sta,
Zhu Yib481de92007-09-25 17:54:57 -0700608 struct ieee80211_hdr *hdr,
Guy Coheneecd6e52008-04-23 17:14:58 -0700609 enum iwl_table_type rate_type)
Zhu Yib481de92007-09-25 17:54:57 -0700610{
Zhu Yib481de92007-09-25 17:54:57 -0700611 if (hdr && is_multicast_ether_addr(hdr->addr1) &&
Guy Coheneecd6e52008-04-23 17:14:58 -0700612 lq_sta->active_rate_basic)
613 return lq_sta->active_rate_basic;
614
615 if (is_legacy(rate_type)) {
616 return lq_sta->active_legacy_rate;
617 } else {
618 if (is_siso(rate_type))
619 return lq_sta->active_siso_rate;
620 else if (is_mimo2(rate_type))
621 return lq_sta->active_mimo2_rate;
622 else
623 return lq_sta->active_mimo3_rate;
Tomas Winklerc33104f2008-01-14 17:46:21 -0800624 }
Zhu Yib481de92007-09-25 17:54:57 -0700625}
626
627static u16 rs_get_adjacent_rate(u8 index, u16 rate_mask, int rate_type)
628{
629 u8 high = IWL_RATE_INVALID;
630 u8 low = IWL_RATE_INVALID;
631
Ian Schram01ebd062007-10-25 17:15:22 +0800632 /* 802.11A or ht walks to the next literal adjacent rate in
Zhu Yib481de92007-09-25 17:54:57 -0700633 * the rate table */
634 if (is_a_band(rate_type) || !is_legacy(rate_type)) {
635 int i;
636 u32 mask;
637
638 /* Find the previous rate that is in the rate mask */
639 i = index - 1;
640 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
641 if (rate_mask & mask) {
642 low = i;
643 break;
644 }
645 }
646
647 /* Find the next rate that is in the rate mask */
648 i = index + 1;
649 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
650 if (rate_mask & mask) {
651 high = i;
652 break;
653 }
654 }
655
656 return (high << 8) | low;
657 }
658
659 low = index;
660 while (low != IWL_RATE_INVALID) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800661 low = iwl4965_rates[low].prev_rs;
Zhu Yib481de92007-09-25 17:54:57 -0700662 if (low == IWL_RATE_INVALID)
663 break;
664 if (rate_mask & (1 << low))
665 break;
666 IWL_DEBUG_RATE("Skipping masked lower rate: %d\n", low);
667 }
668
669 high = index;
670 while (high != IWL_RATE_INVALID) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800671 high = iwl4965_rates[high].next_rs;
Zhu Yib481de92007-09-25 17:54:57 -0700672 if (high == IWL_RATE_INVALID)
673 break;
674 if (rate_mask & (1 << high))
675 break;
676 IWL_DEBUG_RATE("Skipping masked higher rate: %d\n", high);
677 }
678
679 return (high << 8) | low;
680}
681
Guy Cohen39e88502008-04-23 17:14:57 -0700682static u32 rs_get_lower_rate(struct iwl4965_lq_sta *lq_sta,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800683 struct iwl4965_scale_tbl_info *tbl, u8 scale_index,
Guy Cohen39e88502008-04-23 17:14:57 -0700684 u8 ht_possible)
Zhu Yib481de92007-09-25 17:54:57 -0700685{
Zhu Yib481de92007-09-25 17:54:57 -0700686 s32 low;
687 u16 rate_mask;
688 u16 high_low;
689 u8 switch_to_legacy = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -0800690 u8 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -0700691
692 /* check if we need to switch from HT to legacy rates.
693 * assumption is that mandatory rates (1Mbps or 6Mbps)
694 * are always supported (spec demand) */
695 if (!is_legacy(tbl->lq_type) && (!ht_possible || !scale_index)) {
696 switch_to_legacy = 1;
697 scale_index = rs_ht_to_legacy[scale_index];
Johannes Berg8318d782008-01-24 19:38:38 +0100698 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700699 tbl->lq_type = LQ_A;
700 else
701 tbl->lq_type = LQ_G;
702
Guy Cohenfde0db32008-04-21 15:42:01 -0700703 if (num_of_ant(tbl->ant_type > 1))
704 tbl->ant_type = ANT_A;/*FIXME:RS*/
Zhu Yib481de92007-09-25 17:54:57 -0700705
706 tbl->is_fat = 0;
707 tbl->is_SGI = 0;
708 }
709
Guy Coheneecd6e52008-04-23 17:14:58 -0700710 rate_mask = rs_get_supported_rates(lq_sta, NULL, tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -0700711
Ben Cahill77626352007-11-29 11:09:44 +0800712 /* Mask with station rate restriction */
Zhu Yib481de92007-09-25 17:54:57 -0700713 if (is_legacy(tbl->lq_type)) {
Ben Cahill77626352007-11-29 11:09:44 +0800714 /* supp_rates has no CCK bits in A mode */
Johannes Berg8318d782008-01-24 19:38:38 +0100715 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700716 rate_mask = (u16)(rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -0800717 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
Zhu Yib481de92007-09-25 17:54:57 -0700718 else
Tomas Winklerc33104f2008-01-14 17:46:21 -0800719 rate_mask = (u16)(rate_mask & lq_sta->supp_rates);
Zhu Yib481de92007-09-25 17:54:57 -0700720 }
721
Ben Cahill77626352007-11-29 11:09:44 +0800722 /* If we switched from HT to legacy, check current rate */
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800723 if (switch_to_legacy && (rate_mask & (1 << scale_index))) {
Guy Cohen39e88502008-04-23 17:14:57 -0700724 low = scale_index;
725 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700726 }
727
728 high_low = rs_get_adjacent_rate(scale_index, rate_mask, tbl->lq_type);
729 low = high_low & 0xff;
730
Guy Cohen39e88502008-04-23 17:14:57 -0700731 if (low == IWL_RATE_INVALID)
732 low = scale_index;
733
734out:
735 return rate_n_flags_from_tbl(tbl, low, is_green);
Zhu Yib481de92007-09-25 17:54:57 -0700736}
737
Ben Cahill77626352007-11-29 11:09:44 +0800738/*
739 * mac80211 sends us Tx status
740 */
Tomas Winklerc33104f2008-01-14 17:46:21 -0800741static void rs_tx_status(void *priv_rate, struct net_device *dev,
Zhu Yib481de92007-09-25 17:54:57 -0700742 struct sk_buff *skb,
743 struct ieee80211_tx_status *tx_resp)
744{
745 int status;
746 u8 retries;
747 int rs_index, index = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -0800748 struct iwl4965_lq_sta *lq_sta;
Tomas Winkler66c73db2008-04-15 16:01:40 -0700749 struct iwl_link_quality_cmd *table;
Zhu Yib481de92007-09-25 17:54:57 -0700750 struct sta_info *sta;
751 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700752 struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
Zhu Yib481de92007-09-25 17:54:57 -0700753 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800754 struct ieee80211_hw *hw = local_to_hw(local);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800755 struct iwl4965_rate_scale_data *window = NULL;
756 struct iwl4965_rate_scale_data *search_win = NULL;
Guy Cohen39e88502008-04-23 17:14:57 -0700757 u32 tx_rate;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800758 struct iwl4965_scale_tbl_info tbl_type;
759 struct iwl4965_scale_tbl_info *curr_tbl, *search_tbl;
Zhu Yib481de92007-09-25 17:54:57 -0700760 u8 active_index = 0;
761 u16 fc = le16_to_cpu(hdr->frame_control);
762 s32 tpt = 0;
763
Zhu Yi58826352007-09-27 11:27:39 +0800764 IWL_DEBUG_RATE_LIMIT("get frame ack response, update rate scale window\n");
Zhu Yib481de92007-09-25 17:54:57 -0700765
766 if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1))
767 return;
768
Ron Rindjunsky99556432008-01-28 14:07:25 +0200769 /* This packet was aggregated but doesn't carry rate scale info */
770 if ((tx_resp->control.flags & IEEE80211_TXCTL_AMPDU) &&
771 !(tx_resp->flags & IEEE80211_TX_STATUS_AMPDU))
772 return;
773
Zhu Yib481de92007-09-25 17:54:57 -0700774 retries = tx_resp->retry_count;
775
776 if (retries > 15)
777 retries = 15;
778
Johannes Bergd0709a62008-02-25 16:27:46 +0100779 rcu_read_lock();
Zhu Yib481de92007-09-25 17:54:57 -0700780
781 sta = sta_info_get(local, hdr->addr1);
782
Tomas Winklerf4d60822008-03-05 11:31:00 -0800783 if (!sta || !sta->rate_ctrl_priv)
784 goto out;
785
Zhu Yib481de92007-09-25 17:54:57 -0700786
Tomas Winklerc33104f2008-01-14 17:46:21 -0800787 lq_sta = (struct iwl4965_lq_sta *)sta->rate_ctrl_priv;
Zhu Yib481de92007-09-25 17:54:57 -0700788
789 if (!priv->lq_mngr.lq_ready)
Tomas Winklerf4d60822008-03-05 11:31:00 -0800790 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700791
Tomas Winklerc33104f2008-01-14 17:46:21 -0800792 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
793 !lq_sta->ibss_sta_added)
Tomas Winklerf4d60822008-03-05 11:31:00 -0800794 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700795
Tomas Winklerc33104f2008-01-14 17:46:21 -0800796 table = &lq_sta->lq;
797 active_index = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -0700798
Tomas Winklerc33104f2008-01-14 17:46:21 -0800799 lq_sta->antenna = lq_sta->valid_antenna;
Ben Cahill77626352007-11-29 11:09:44 +0800800
Tomas Winklerc33104f2008-01-14 17:46:21 -0800801 curr_tbl = &(lq_sta->lq_info[active_index]);
802 search_tbl = &(lq_sta->lq_info[(1 - active_index)]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800803 window = (struct iwl4965_rate_scale_data *)
Zhu Yib481de92007-09-25 17:54:57 -0700804 &(curr_tbl->win[0]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800805 search_win = (struct iwl4965_rate_scale_data *)
Zhu Yib481de92007-09-25 17:54:57 -0700806 &(search_tbl->win[0]);
807
Ben Cahill77626352007-11-29 11:09:44 +0800808 /*
809 * Ignore this Tx frame response if its initial rate doesn't match
810 * that of latest Link Quality command. There may be stragglers
811 * from a previous Link Quality command, but we're no longer interested
812 * in those; they're either from the "active" mode while we're trying
813 * to check "search" mode, or a prior "search" mode after we've moved
814 * to a new "search" mode (which might become the new "active" mode).
815 */
Guy Cohen39e88502008-04-23 17:14:57 -0700816 tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
817 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type, &rs_index);
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800818 if (priv->band == IEEE80211_BAND_5GHZ)
819 rs_index -= IWL_FIRST_OFDM_RATE;
820
821 if ((tx_resp->control.tx_rate == NULL) ||
822 (tbl_type.is_SGI ^
823 !!(tx_resp->control.flags & IEEE80211_TXCTL_SHORT_GI)) ||
824 (tbl_type.is_fat ^
825 !!(tx_resp->control.flags & IEEE80211_TXCTL_40_MHZ_WIDTH)) ||
826 (tbl_type.is_dup ^
827 !!(tx_resp->control.flags & IEEE80211_TXCTL_DUP_DATA)) ||
Guy Cohenfde0db32008-04-21 15:42:01 -0700828 (tbl_type.ant_type ^ tx_resp->control.antenna_sel_tx) ||
Guy Cohen39e88502008-04-23 17:14:57 -0700829 (!!(tx_rate & RATE_MCS_HT_MSK) ^
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800830 !!(tx_resp->control.flags & IEEE80211_TXCTL_OFDM_HT)) ||
Guy Cohen39e88502008-04-23 17:14:57 -0700831 (!!(tx_rate & RATE_MCS_GF_MSK) ^
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800832 !!(tx_resp->control.flags & IEEE80211_TXCTL_GREEN_FIELD)) ||
833 (hw->wiphy->bands[priv->band]->bitrates[rs_index].bitrate !=
834 tx_resp->control.tx_rate->bitrate)) {
Guy Cohen39e88502008-04-23 17:14:57 -0700835 IWL_DEBUG_RATE("initial rate does not match 0x%x\n", tx_rate);
Tomas Winklerf4d60822008-03-05 11:31:00 -0800836 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700837 }
838
Ben Cahill77626352007-11-29 11:09:44 +0800839 /* Update frame history window with "failure" for each Tx retry. */
Zhu Yib481de92007-09-25 17:54:57 -0700840 while (retries) {
Ben Cahill77626352007-11-29 11:09:44 +0800841 /* Look up the rate and other info used for each tx attempt.
842 * Each tx attempt steps one entry deeper in the rate table. */
Guy Cohen39e88502008-04-23 17:14:57 -0700843 tx_rate = le32_to_cpu(table->rs_table[index].rate_n_flags);
844 rs_get_tbl_info_from_mcs(tx_rate, priv->band,
Zhu Yib481de92007-09-25 17:54:57 -0700845 &tbl_type, &rs_index);
846
Ben Cahill77626352007-11-29 11:09:44 +0800847 /* If type matches "search" table,
848 * add failure to "search" history */
Zhu Yib481de92007-09-25 17:54:57 -0700849 if ((tbl_type.lq_type == search_tbl->lq_type) &&
Guy Cohenfde0db32008-04-21 15:42:01 -0700850 (tbl_type.ant_type == search_tbl->ant_type) &&
Zhu Yib481de92007-09-25 17:54:57 -0700851 (tbl_type.is_SGI == search_tbl->is_SGI)) {
852 if (search_tbl->expected_tpt)
853 tpt = search_tbl->expected_tpt[rs_index];
854 else
855 tpt = 0;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200856 rs_collect_tx_data(search_win, rs_index, tpt, 1, 0);
Ben Cahill77626352007-11-29 11:09:44 +0800857
858 /* Else if type matches "current/active" table,
859 * add failure to "current/active" history */
Zhu Yib481de92007-09-25 17:54:57 -0700860 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
Guy Cohenfde0db32008-04-21 15:42:01 -0700861 (tbl_type.ant_type == curr_tbl->ant_type) &&
Zhu Yib481de92007-09-25 17:54:57 -0700862 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
863 if (curr_tbl->expected_tpt)
864 tpt = curr_tbl->expected_tpt[rs_index];
865 else
866 tpt = 0;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200867 rs_collect_tx_data(window, rs_index, tpt, 1, 0);
Zhu Yib481de92007-09-25 17:54:57 -0700868 }
Ben Cahill77626352007-11-29 11:09:44 +0800869
870 /* If not searching for a new mode, increment failed counter
871 * ... this helps determine when to start searching again */
Tomas Winklerc33104f2008-01-14 17:46:21 -0800872 if (lq_sta->stay_in_tbl)
873 lq_sta->total_failed++;
Zhu Yib481de92007-09-25 17:54:57 -0700874 --retries;
875 index++;
876
877 }
878
Ben Cahill77626352007-11-29 11:09:44 +0800879 /*
880 * Find (by rate) the history window to update with final Tx attempt;
881 * if Tx was successful first try, use original rate,
882 * else look up the rate that was, finally, successful.
883 */
Guy Cohen39e88502008-04-23 17:14:57 -0700884 tx_rate = le32_to_cpu(table->rs_table[index].rate_n_flags);
885 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type, &rs_index);
Zhu Yib481de92007-09-25 17:54:57 -0700886
Ben Cahill77626352007-11-29 11:09:44 +0800887 /* Update frame history window with "success" if Tx got ACKed ... */
Zhu Yib481de92007-09-25 17:54:57 -0700888 if (tx_resp->flags & IEEE80211_TX_STATUS_ACK)
889 status = 1;
890 else
891 status = 0;
892
Ben Cahill77626352007-11-29 11:09:44 +0800893 /* If type matches "search" table,
894 * add final tx status to "search" history */
Zhu Yib481de92007-09-25 17:54:57 -0700895 if ((tbl_type.lq_type == search_tbl->lq_type) &&
Guy Cohenfde0db32008-04-21 15:42:01 -0700896 (tbl_type.ant_type == search_tbl->ant_type) &&
Zhu Yib481de92007-09-25 17:54:57 -0700897 (tbl_type.is_SGI == search_tbl->is_SGI)) {
898 if (search_tbl->expected_tpt)
899 tpt = search_tbl->expected_tpt[rs_index];
900 else
901 tpt = 0;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200902 if (tx_resp->control.flags & IEEE80211_TXCTL_AMPDU)
903 rs_collect_tx_data(search_win, rs_index, tpt,
904 tx_resp->ampdu_ack_len,
905 tx_resp->ampdu_ack_map);
906 else
907 rs_collect_tx_data(search_win, rs_index, tpt,
908 1, status);
Ben Cahill77626352007-11-29 11:09:44 +0800909 /* Else if type matches "current/active" table,
910 * add final tx status to "current/active" history */
Zhu Yib481de92007-09-25 17:54:57 -0700911 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
Guy Cohenfde0db32008-04-21 15:42:01 -0700912 (tbl_type.ant_type == curr_tbl->ant_type) &&
Zhu Yib481de92007-09-25 17:54:57 -0700913 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
914 if (curr_tbl->expected_tpt)
915 tpt = curr_tbl->expected_tpt[rs_index];
916 else
917 tpt = 0;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200918 if (tx_resp->control.flags & IEEE80211_TXCTL_AMPDU)
919 rs_collect_tx_data(window, rs_index, tpt,
920 tx_resp->ampdu_ack_len,
921 tx_resp->ampdu_ack_map);
922 else
923 rs_collect_tx_data(window, rs_index, tpt,
924 1, status);
Zhu Yib481de92007-09-25 17:54:57 -0700925 }
926
Ben Cahill77626352007-11-29 11:09:44 +0800927 /* If not searching for new mode, increment success/failed counter
928 * ... these help determine when to start searching again */
Tomas Winklerc33104f2008-01-14 17:46:21 -0800929 if (lq_sta->stay_in_tbl) {
Ron Rindjunsky99556432008-01-28 14:07:25 +0200930 if (tx_resp->control.flags & IEEE80211_TXCTL_AMPDU) {
931 lq_sta->total_success += tx_resp->ampdu_ack_map;
932 lq_sta->total_failed +=
933 (tx_resp->ampdu_ack_len - tx_resp->ampdu_ack_map);
934 } else {
935 if (status)
936 lq_sta->total_success++;
937 else
938 lq_sta->total_failed++;
939 }
Zhu Yib481de92007-09-25 17:54:57 -0700940 }
941
Ben Cahill77626352007-11-29 11:09:44 +0800942 /* See if there's a better rate or modulation mode to try. */
Zhu Yib481de92007-09-25 17:54:57 -0700943 rs_rate_scale_perform(priv, dev, hdr, sta);
Tomas Winklerf4d60822008-03-05 11:31:00 -0800944out:
Johannes Bergd0709a62008-02-25 16:27:46 +0100945 rcu_read_unlock();
Zhu Yib481de92007-09-25 17:54:57 -0700946 return;
947}
948
Guy Cohenfde0db32008-04-21 15:42:01 -0700949static inline u8 rs_is_ant_connected(u8 valid_antenna, u8 ant_type)
Zhu Yib481de92007-09-25 17:54:57 -0700950{
Guy Cohenfde0db32008-04-21 15:42:01 -0700951 return ((ant_type & valid_antenna) == ant_type);
Zhu Yib481de92007-09-25 17:54:57 -0700952}
953
Guy Cohenfde0db32008-04-21 15:42:01 -0700954/*FIXME:RS: this function should be replaced*/
955static u8 rs_is_other_ant_connected(u8 valid_antenna, u8 ant_type)
Zhu Yib481de92007-09-25 17:54:57 -0700956{
Guy Cohenfde0db32008-04-21 15:42:01 -0700957 if (ant_type == ANT_B)
958 return rs_is_ant_connected(valid_antenna, ANT_A);
Zhu Yib481de92007-09-25 17:54:57 -0700959 else
Guy Cohenfde0db32008-04-21 15:42:01 -0700960 return rs_is_ant_connected(valid_antenna, ANT_B);
Zhu Yib481de92007-09-25 17:54:57 -0700961
962 return 0;
963}
964
Ben Cahill77626352007-11-29 11:09:44 +0800965/*
966 * Begin a period of staying with a selected modulation mode.
967 * Set "stay_in_tbl" flag to prevent any mode switches.
968 * Set frame tx success limits according to legacy vs. high-throughput,
969 * and reset overall (spanning all rates) tx success history statistics.
970 * These control how long we stay using same modulation mode before
971 * searching for a new mode.
972 */
Zhu Yib481de92007-09-25 17:54:57 -0700973static void rs_set_stay_in_table(u8 is_legacy,
Tomas Winklerc33104f2008-01-14 17:46:21 -0800974 struct iwl4965_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -0700975{
Guy Coheneecd6e52008-04-23 17:14:58 -0700976 IWL_DEBUG_RATE("we are staying in the same table\n");
Tomas Winklerc33104f2008-01-14 17:46:21 -0800977 lq_sta->stay_in_tbl = 1; /* only place this gets set */
Zhu Yib481de92007-09-25 17:54:57 -0700978 if (is_legacy) {
Tomas Winklerc33104f2008-01-14 17:46:21 -0800979 lq_sta->table_count_limit = IWL_LEGACY_TABLE_COUNT;
980 lq_sta->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
981 lq_sta->max_success_limit = IWL_LEGACY_SUCCESS_LIMIT;
Zhu Yib481de92007-09-25 17:54:57 -0700982 } else {
Tomas Winklerc33104f2008-01-14 17:46:21 -0800983 lq_sta->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
984 lq_sta->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
985 lq_sta->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
Zhu Yib481de92007-09-25 17:54:57 -0700986 }
Tomas Winklerc33104f2008-01-14 17:46:21 -0800987 lq_sta->table_count = 0;
988 lq_sta->total_failed = 0;
989 lq_sta->total_success = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700990}
991
Ben Cahill77626352007-11-29 11:09:44 +0800992/*
993 * Find correct throughput table for given mode of modulation
994 */
Guy Coheneecd6e52008-04-23 17:14:58 -0700995static void rs_set_expected_tpt_table(struct iwl4965_lq_sta *lq_sta,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800996 struct iwl4965_scale_tbl_info *tbl)
Zhu Yib481de92007-09-25 17:54:57 -0700997{
998 if (is_legacy(tbl->lq_type)) {
999 if (!is_a_band(tbl->lq_type))
1000 tbl->expected_tpt = expected_tpt_G;
1001 else
1002 tbl->expected_tpt = expected_tpt_A;
1003 } else if (is_siso(tbl->lq_type)) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001004 if (tbl->is_fat && !lq_sta->is_dup)
Zhu Yib481de92007-09-25 17:54:57 -07001005 if (tbl->is_SGI)
1006 tbl->expected_tpt = expected_tpt_siso40MHzSGI;
1007 else
1008 tbl->expected_tpt = expected_tpt_siso40MHz;
1009 else if (tbl->is_SGI)
1010 tbl->expected_tpt = expected_tpt_siso20MHzSGI;
1011 else
1012 tbl->expected_tpt = expected_tpt_siso20MHz;
1013
Guy Cohenfde0db32008-04-21 15:42:01 -07001014 } else if (is_mimo(tbl->lq_type)) { /* FIXME:need to separate mimo2/3 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001015 if (tbl->is_fat && !lq_sta->is_dup)
Zhu Yib481de92007-09-25 17:54:57 -07001016 if (tbl->is_SGI)
1017 tbl->expected_tpt = expected_tpt_mimo40MHzSGI;
1018 else
1019 tbl->expected_tpt = expected_tpt_mimo40MHz;
1020 else if (tbl->is_SGI)
1021 tbl->expected_tpt = expected_tpt_mimo20MHzSGI;
1022 else
1023 tbl->expected_tpt = expected_tpt_mimo20MHz;
1024 } else
1025 tbl->expected_tpt = expected_tpt_G;
1026}
1027
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001028#ifdef CONFIG_IWL4965_HT
Ben Cahill77626352007-11-29 11:09:44 +08001029/*
1030 * Find starting rate for new "search" high-throughput mode of modulation.
1031 * Goal is to find lowest expected rate (under perfect conditions) that is
1032 * above the current measured throughput of "active" mode, to give new mode
1033 * a fair chance to prove itself without too many challenges.
1034 *
1035 * This gets called when transitioning to more aggressive modulation
1036 * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
1037 * (i.e. MIMO to SISO). When moving to MIMO, bit rate will typically need
1038 * to decrease to match "active" throughput. When moving from MIMO to SISO,
1039 * bit rate will typically need to increase, but not if performance was bad.
1040 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001041static s32 rs_get_best_rate(struct iwl_priv *priv,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001042 struct iwl4965_lq_sta *lq_sta,
Ben Cahill77626352007-11-29 11:09:44 +08001043 struct iwl4965_scale_tbl_info *tbl, /* "search" */
Zhu Yib481de92007-09-25 17:54:57 -07001044 u16 rate_mask, s8 index, s8 rate)
1045{
Ben Cahill77626352007-11-29 11:09:44 +08001046 /* "active" values */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001047 struct iwl4965_scale_tbl_info *active_tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001048 &(lq_sta->lq_info[lq_sta->active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07001049 s32 active_sr = active_tbl->win[index].success_ratio;
Zhu Yib481de92007-09-25 17:54:57 -07001050 s32 active_tpt = active_tbl->expected_tpt[index];
Ben Cahill77626352007-11-29 11:09:44 +08001051
1052 /* expected "search" throughput */
1053 s32 *tpt_tbl = tbl->expected_tpt;
1054
1055 s32 new_rate, high, low, start_hi;
Zhu Yib481de92007-09-25 17:54:57 -07001056 u16 high_low;
1057
1058 new_rate = high = low = start_hi = IWL_RATE_INVALID;
1059
1060 for (; ;) {
1061 high_low = rs_get_adjacent_rate(rate, rate_mask, tbl->lq_type);
1062
1063 low = high_low & 0xff;
1064 high = (high_low >> 8) & 0xff;
1065
Ben Cahill77626352007-11-29 11:09:44 +08001066 /*
1067 * Lower the "search" bit rate, to give new "search" mode
1068 * approximately the same throughput as "active" if:
1069 *
1070 * 1) "Active" mode has been working modestly well (but not
1071 * great), and expected "search" throughput (under perfect
1072 * conditions) at candidate rate is above the actual
1073 * measured "active" throughput (but less than expected
1074 * "active" throughput under perfect conditions).
1075 * OR
1076 * 2) "Active" mode has been working perfectly or very well
1077 * and expected "search" throughput (under perfect
1078 * conditions) at candidate rate is above expected
1079 * "active" throughput (under perfect conditions).
1080 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001081 if ((((100 * tpt_tbl[rate]) > lq_sta->last_tpt) &&
Zhu Yib481de92007-09-25 17:54:57 -07001082 ((active_sr > IWL_RATE_DECREASE_TH) &&
1083 (active_sr <= IWL_RATE_HIGH_TH) &&
1084 (tpt_tbl[rate] <= active_tpt))) ||
1085 ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
1086 (tpt_tbl[rate] > active_tpt))) {
1087
Ben Cahill77626352007-11-29 11:09:44 +08001088 /* (2nd or later pass)
1089 * If we've already tried to raise the rate, and are
1090 * now trying to lower it, use the higher rate. */
Zhu Yib481de92007-09-25 17:54:57 -07001091 if (start_hi != IWL_RATE_INVALID) {
1092 new_rate = start_hi;
1093 break;
1094 }
Ben Cahill77626352007-11-29 11:09:44 +08001095
Zhu Yib481de92007-09-25 17:54:57 -07001096 new_rate = rate;
Ben Cahill77626352007-11-29 11:09:44 +08001097
1098 /* Loop again with lower rate */
Zhu Yib481de92007-09-25 17:54:57 -07001099 if (low != IWL_RATE_INVALID)
1100 rate = low;
Ben Cahill77626352007-11-29 11:09:44 +08001101
1102 /* Lower rate not available, use the original */
Zhu Yib481de92007-09-25 17:54:57 -07001103 else
1104 break;
Ben Cahill77626352007-11-29 11:09:44 +08001105
1106 /* Else try to raise the "search" rate to match "active" */
Zhu Yib481de92007-09-25 17:54:57 -07001107 } else {
Ben Cahill77626352007-11-29 11:09:44 +08001108 /* (2nd or later pass)
1109 * If we've already tried to lower the rate, and are
1110 * now trying to raise it, use the lower rate. */
Zhu Yib481de92007-09-25 17:54:57 -07001111 if (new_rate != IWL_RATE_INVALID)
1112 break;
Ben Cahill77626352007-11-29 11:09:44 +08001113
1114 /* Loop again with higher rate */
Zhu Yib481de92007-09-25 17:54:57 -07001115 else if (high != IWL_RATE_INVALID) {
1116 start_hi = high;
1117 rate = high;
Ben Cahill77626352007-11-29 11:09:44 +08001118
1119 /* Higher rate not available, use the original */
Zhu Yib481de92007-09-25 17:54:57 -07001120 } else {
1121 new_rate = rate;
1122 break;
1123 }
1124 }
1125 }
1126
1127 return new_rate;
1128}
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001129#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001130
Guy Cohenfde0db32008-04-21 15:42:01 -07001131/*FIXME:RS:this function should be replaced*/
Zhu Yib481de92007-09-25 17:54:57 -07001132static inline u8 rs_is_both_ant_supp(u8 valid_antenna)
1133{
Guy Cohenfde0db32008-04-21 15:42:01 -07001134 return (rs_is_ant_connected(valid_antenna, ANT_AB));
Zhu Yib481de92007-09-25 17:54:57 -07001135}
1136
Ben Cahill77626352007-11-29 11:09:44 +08001137/*
1138 * Set up search table for MIMO
1139 */
Guy Cohenfde0db32008-04-21 15:42:01 -07001140#ifdef CONFIG_IWL4965_HT
1141static int rs_switch_to_mimo2(struct iwl_priv *priv,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001142 struct iwl4965_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001143 struct ieee80211_conf *conf,
1144 struct sta_info *sta,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001145 struct iwl4965_scale_tbl_info *tbl, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001146{
Zhu Yib481de92007-09-25 17:54:57 -07001147 u16 rate_mask;
1148 s32 rate;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001149 s8 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07001150
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001151 if (!(conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) ||
1152 !sta->ht_info.ht_supported)
Zhu Yib481de92007-09-25 17:54:57 -07001153 return -1;
1154
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001155 if (priv->current_ht_config.tx_mimo_ps_mode == IWL_MIMO_PS_STATIC)
Zhu Yib481de92007-09-25 17:54:57 -07001156 return -1;
1157
Ben Cahill77626352007-11-29 11:09:44 +08001158 /* Need both Tx chains/antennas to support MIMO */
Guy Cohenfde0db32008-04-21 15:42:01 -07001159 if (!rs_is_both_ant_supp(priv->hw_params.valid_tx_ant))
Zhu Yib481de92007-09-25 17:54:57 -07001160 return -1;
1161
Guy Coheneecd6e52008-04-23 17:14:58 -07001162 IWL_DEBUG_RATE("LQ: try to switch to MIMO2\n");
Guy Cohen39e88502008-04-23 17:14:57 -07001163
1164 tbl->lq_type = LQ_MIMO2;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001165 tbl->is_dup = lq_sta->is_dup;
Zhu Yib481de92007-09-25 17:54:57 -07001166 tbl->action = 0;
Guy Cohen39e88502008-04-23 17:14:57 -07001167 rate_mask = lq_sta->active_mimo2_rate;
1168
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001169 if (priv->current_ht_config.supported_chan_width
Guy Cohen39e88502008-04-23 17:14:57 -07001170 == IWL_CHANNEL_WIDTH_40MHZ)
Zhu Yib481de92007-09-25 17:54:57 -07001171 tbl->is_fat = 1;
1172 else
1173 tbl->is_fat = 0;
1174
Guy Cohen39e88502008-04-23 17:14:57 -07001175 /* FIXME: - don't toggle SGI here
Zhu Yib481de92007-09-25 17:54:57 -07001176 if (tbl->is_fat) {
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001177 if (priv->current_ht_config.sgf & HT_SHORT_GI_40MHZ_ONLY)
Zhu Yib481de92007-09-25 17:54:57 -07001178 tbl->is_SGI = 1;
1179 else
1180 tbl->is_SGI = 0;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001181 } else if (priv->current_ht_config.sgf & HT_SHORT_GI_20MHZ_ONLY)
Zhu Yib481de92007-09-25 17:54:57 -07001182 tbl->is_SGI = 1;
1183 else
1184 tbl->is_SGI = 0;
Guy Cohen39e88502008-04-23 17:14:57 -07001185 */
Zhu Yib481de92007-09-25 17:54:57 -07001186
Guy Coheneecd6e52008-04-23 17:14:58 -07001187 rs_set_expected_tpt_table(lq_sta, tbl);
Zhu Yib481de92007-09-25 17:54:57 -07001188
Tomas Winklerc33104f2008-01-14 17:46:21 -08001189 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index, index);
Zhu Yib481de92007-09-25 17:54:57 -07001190
Guy Coheneecd6e52008-04-23 17:14:58 -07001191 IWL_DEBUG_RATE("LQ: MIMO2 best rate %d mask %X\n", rate, rate_mask);
Guy Cohen39e88502008-04-23 17:14:57 -07001192
1193 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
Guy Coheneecd6e52008-04-23 17:14:58 -07001194 IWL_DEBUG_RATE("Can't switch with index %d rate mask %x\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001195 rate, rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -07001196 return -1;
Guy Cohen39e88502008-04-23 17:14:57 -07001197 }
1198 tbl->current_rate = rate_n_flags_from_tbl(tbl, rate, is_green);
Zhu Yib481de92007-09-25 17:54:57 -07001199
Guy Coheneecd6e52008-04-23 17:14:58 -07001200 IWL_DEBUG_RATE("LQ: Switch to new mcs %X index is green %X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001201 tbl->current_rate, is_green);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001202 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07001203}
Guy Cohenfde0db32008-04-21 15:42:01 -07001204#else
1205static int rs_switch_to_mimo2(struct iwl_priv *priv,
1206 struct iwl4965_lq_sta *lq_sta,
1207 struct ieee80211_conf *conf,
1208 struct sta_info *sta,
1209 struct iwl4965_scale_tbl_info *tbl, int index)
1210{
1211 return -1;
1212}
1213#endif /*CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001214
Ben Cahill77626352007-11-29 11:09:44 +08001215/*
1216 * Set up search table for SISO
1217 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001218static int rs_switch_to_siso(struct iwl_priv *priv,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001219 struct iwl4965_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001220 struct ieee80211_conf *conf,
1221 struct sta_info *sta,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001222 struct iwl4965_scale_tbl_info *tbl, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001223{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001224#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07001225 u16 rate_mask;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001226 u8 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07001227 s32 rate;
1228
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001229 if (!(conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) ||
1230 !sta->ht_info.ht_supported)
Zhu Yib481de92007-09-25 17:54:57 -07001231 return -1;
1232
Guy Coheneecd6e52008-04-23 17:14:58 -07001233 IWL_DEBUG_RATE("LQ: try to switch to SISO\n");
Guy Cohen39e88502008-04-23 17:14:57 -07001234
Tomas Winklerc33104f2008-01-14 17:46:21 -08001235 tbl->is_dup = lq_sta->is_dup;
Zhu Yib481de92007-09-25 17:54:57 -07001236 tbl->lq_type = LQ_SISO;
1237 tbl->action = 0;
Guy Cohen39e88502008-04-23 17:14:57 -07001238 rate_mask = lq_sta->active_siso_rate;
Zhu Yib481de92007-09-25 17:54:57 -07001239
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001240 if (priv->current_ht_config.supported_chan_width
1241 == IWL_CHANNEL_WIDTH_40MHZ)
Zhu Yib481de92007-09-25 17:54:57 -07001242 tbl->is_fat = 1;
1243 else
1244 tbl->is_fat = 0;
1245
Guy Cohen39e88502008-04-23 17:14:57 -07001246 /* FIXME: - don't toggle SGI here
Zhu Yib481de92007-09-25 17:54:57 -07001247 if (tbl->is_fat) {
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001248 if (priv->current_ht_config.sgf & HT_SHORT_GI_40MHZ_ONLY)
Zhu Yib481de92007-09-25 17:54:57 -07001249 tbl->is_SGI = 1;
1250 else
1251 tbl->is_SGI = 0;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001252 } else if (priv->current_ht_config.sgf & HT_SHORT_GI_20MHZ_ONLY)
Zhu Yib481de92007-09-25 17:54:57 -07001253 tbl->is_SGI = 1;
1254 else
1255 tbl->is_SGI = 0;
Guy Cohen39e88502008-04-23 17:14:57 -07001256 */
Zhu Yib481de92007-09-25 17:54:57 -07001257
1258 if (is_green)
Guy Cohen39e88502008-04-23 17:14:57 -07001259 tbl->is_SGI = 0; /*11n spec: no SGI in SISO+Greenfield*/
Zhu Yib481de92007-09-25 17:54:57 -07001260
Guy Coheneecd6e52008-04-23 17:14:58 -07001261 rs_set_expected_tpt_table(lq_sta, tbl);
Tomas Winklerc33104f2008-01-14 17:46:21 -08001262 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index, index);
Zhu Yib481de92007-09-25 17:54:57 -07001263
Guy Coheneecd6e52008-04-23 17:14:58 -07001264 IWL_DEBUG_RATE("LQ: get best rate %d mask %X\n", rate, rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -07001265 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
Guy Coheneecd6e52008-04-23 17:14:58 -07001266 IWL_DEBUG_RATE("can not switch with index %d rate mask %x\n",
Zhu Yib481de92007-09-25 17:54:57 -07001267 rate, rate_mask);
1268 return -1;
1269 }
Guy Cohen39e88502008-04-23 17:14:57 -07001270 tbl->current_rate = rate_n_flags_from_tbl(tbl, rate, is_green);
Guy Coheneecd6e52008-04-23 17:14:58 -07001271 IWL_DEBUG_RATE("LQ: Switch to new mcs %X index is green %X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001272 tbl->current_rate, is_green);
Mohamed Abbas403ab562007-11-06 22:06:25 -08001273 return 0;
1274#else
1275 return -1;
Zhu Yib481de92007-09-25 17:54:57 -07001276
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001277#endif /*CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001278}
1279
Ben Cahill77626352007-11-29 11:09:44 +08001280/*
1281 * Try to switch to new modulation mode from legacy
1282 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001283static int rs_move_legacy_other(struct iwl_priv *priv,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001284 struct iwl4965_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001285 struct ieee80211_conf *conf,
1286 struct sta_info *sta,
Zhu Yib481de92007-09-25 17:54:57 -07001287 int index)
1288{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001289 struct iwl4965_scale_tbl_info *tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001290 &(lq_sta->lq_info[lq_sta->active_tbl]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001291 struct iwl4965_scale_tbl_info *search_tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001292 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001293 struct iwl4965_rate_scale_data *window = &(tbl->win[index]);
1294 u32 sz = (sizeof(struct iwl4965_scale_tbl_info) -
1295 (sizeof(struct iwl4965_rate_scale_data) * IWL_RATE_COUNT));
Zhu Yib481de92007-09-25 17:54:57 -07001296 u8 start_action = tbl->action;
Guy Cohenfde0db32008-04-21 15:42:01 -07001297 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1298 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001299
1300 for (; ;) {
1301 switch (tbl->action) {
1302 case IWL_LEGACY_SWITCH_ANTENNA:
Guy Coheneecd6e52008-04-23 17:14:58 -07001303 IWL_DEBUG_RATE("LQ Legacy toggle Antenna\n");
Zhu Yib481de92007-09-25 17:54:57 -07001304
1305 search_tbl->lq_type = LQ_NONE;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001306 lq_sta->action_counter++;
Ben Cahill77626352007-11-29 11:09:44 +08001307
1308 /* Don't change antenna if success has been great */
Guy Cohen39e88502008-04-23 17:14:57 -07001309 /*FIXME:RS:not sure this is really needed*/
Zhu Yib481de92007-09-25 17:54:57 -07001310 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1311 break;
Ben Cahill77626352007-11-29 11:09:44 +08001312
1313 /* Don't change antenna if other one is not connected */
Guy Cohenfde0db32008-04-21 15:42:01 -07001314 if (!rs_is_other_ant_connected(valid_tx_ant,
1315 tbl->ant_type))
Zhu Yib481de92007-09-25 17:54:57 -07001316 break;
1317
Ben Cahill77626352007-11-29 11:09:44 +08001318 /* Set up search table to try other antenna */
Zhu Yib481de92007-09-25 17:54:57 -07001319 memcpy(search_tbl, tbl, sz);
1320
Guy Cohen39e88502008-04-23 17:14:57 -07001321 rs_toggle_antenna(&search_tbl->current_rate,
Zhu Yib481de92007-09-25 17:54:57 -07001322 search_tbl);
Guy Coheneecd6e52008-04-23 17:14:58 -07001323 rs_set_expected_tpt_table(lq_sta, search_tbl);
Tomas Winklerc33104f2008-01-14 17:46:21 -08001324 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001325 goto out;
1326
1327 case IWL_LEGACY_SWITCH_SISO:
Guy Coheneecd6e52008-04-23 17:14:58 -07001328 IWL_DEBUG_RATE("LQ: Legacy switch to SISO\n");
Ben Cahill77626352007-11-29 11:09:44 +08001329
1330 /* Set up search table to try SISO */
Zhu Yib481de92007-09-25 17:54:57 -07001331 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001332 search_tbl->is_SGI = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001333 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001334 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001335 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001336 lq_sta->search_better_tbl = 1;
1337 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001338 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001339 }
Zhu Yib481de92007-09-25 17:54:57 -07001340
1341 break;
Guy Cohenfde0db32008-04-21 15:42:01 -07001342 case IWL_LEGACY_SWITCH_MIMO2:
Guy Coheneecd6e52008-04-23 17:14:58 -07001343 IWL_DEBUG_RATE("LQ: Legacy switch to MIMO2\n");
Ben Cahill77626352007-11-29 11:09:44 +08001344
1345 /* Set up search table to try MIMO */
Zhu Yib481de92007-09-25 17:54:57 -07001346 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001347 search_tbl->is_SGI = 0;
Guy Cohenfde0db32008-04-21 15:42:01 -07001348 search_tbl->ant_type = ANT_AB;/*FIXME:RS*/
1349 /*FIXME:RS:need to check ant validity*/
1350 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001351 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001352 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001353 lq_sta->search_better_tbl = 1;
1354 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001355 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001356 }
Zhu Yib481de92007-09-25 17:54:57 -07001357 break;
1358 }
1359 tbl->action++;
Guy Cohenfde0db32008-04-21 15:42:01 -07001360 if (tbl->action > IWL_LEGACY_SWITCH_MIMO2)
Zhu Yib481de92007-09-25 17:54:57 -07001361 tbl->action = IWL_LEGACY_SWITCH_ANTENNA;
1362
1363 if (tbl->action == start_action)
1364 break;
1365
1366 }
1367 return 0;
1368
1369 out:
1370 tbl->action++;
Guy Cohenfde0db32008-04-21 15:42:01 -07001371 if (tbl->action > IWL_LEGACY_SWITCH_MIMO2)
Zhu Yib481de92007-09-25 17:54:57 -07001372 tbl->action = IWL_LEGACY_SWITCH_ANTENNA;
1373 return 0;
1374
1375}
1376
Ben Cahill77626352007-11-29 11:09:44 +08001377/*
1378 * Try to switch to new modulation mode from SISO
1379 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001380static int rs_move_siso_to_other(struct iwl_priv *priv,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001381 struct iwl4965_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001382 struct ieee80211_conf *conf,
1383 struct sta_info *sta,
Zhu Yib481de92007-09-25 17:54:57 -07001384 int index)
1385{
Tomas Winklerc33104f2008-01-14 17:46:21 -08001386 u8 is_green = lq_sta->is_green;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001387 struct iwl4965_scale_tbl_info *tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001388 &(lq_sta->lq_info[lq_sta->active_tbl]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001389 struct iwl4965_scale_tbl_info *search_tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001390 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001391 struct iwl4965_rate_scale_data *window = &(tbl->win[index]);
1392 u32 sz = (sizeof(struct iwl4965_scale_tbl_info) -
1393 (sizeof(struct iwl4965_rate_scale_data) * IWL_RATE_COUNT));
Zhu Yib481de92007-09-25 17:54:57 -07001394 u8 start_action = tbl->action;
Guy Cohenfde0db32008-04-21 15:42:01 -07001395 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1396 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001397
1398 for (;;) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001399 lq_sta->action_counter++;
Zhu Yib481de92007-09-25 17:54:57 -07001400 switch (tbl->action) {
1401 case IWL_SISO_SWITCH_ANTENNA:
Guy Coheneecd6e52008-04-23 17:14:58 -07001402 IWL_DEBUG_RATE("LQ: SISO toggle Antenna\n");
Guy Cohen39e88502008-04-23 17:14:57 -07001403 /*FIXME:RS: is this really needed for SISO?*/
Zhu Yib481de92007-09-25 17:54:57 -07001404 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1405 break;
Guy Cohenfde0db32008-04-21 15:42:01 -07001406 if (!rs_is_other_ant_connected(valid_tx_ant,
1407 tbl->ant_type))
Zhu Yib481de92007-09-25 17:54:57 -07001408 break;
1409
1410 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001411 rs_toggle_antenna(&search_tbl->current_rate,
Zhu Yib481de92007-09-25 17:54:57 -07001412 search_tbl);
Tomas Winklerc33104f2008-01-14 17:46:21 -08001413 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001414
1415 goto out;
1416
Guy Cohenfde0db32008-04-21 15:42:01 -07001417 case IWL_SISO_SWITCH_MIMO2:
Guy Coheneecd6e52008-04-23 17:14:58 -07001418 IWL_DEBUG_RATE("LQ: SISO switch to MIMO\n");
Zhu Yib481de92007-09-25 17:54:57 -07001419 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001420 search_tbl->is_SGI = 0;
Guy Cohenfde0db32008-04-21 15:42:01 -07001421 search_tbl->ant_type = ANT_AB; /*FIXME:RS*/
1422 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001423 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001424 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001425 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001426 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001427 }
Zhu Yib481de92007-09-25 17:54:57 -07001428 break;
1429 case IWL_SISO_SWITCH_GI:
Guy Coheneecd6e52008-04-23 17:14:58 -07001430 IWL_DEBUG_RATE("LQ: SISO toggle SGI/NGI\n");
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001431
Zhu Yib481de92007-09-25 17:54:57 -07001432 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001433 if (is_green) {
1434 if (!tbl->is_SGI)
1435 break;
1436 else
1437 IWL_ERROR("SGI was set in GF+SISO\n");
Zhu Yib481de92007-09-25 17:54:57 -07001438 }
Guy Cohen39e88502008-04-23 17:14:57 -07001439 search_tbl->is_SGI = !tbl->is_SGI;
Guy Coheneecd6e52008-04-23 17:14:58 -07001440 rs_set_expected_tpt_table(lq_sta, search_tbl);
Guy Cohen39e88502008-04-23 17:14:57 -07001441 if (tbl->is_SGI) {
1442 s32 tpt = lq_sta->last_tpt / 100;
1443 if (tpt >= search_tbl->expected_tpt[index])
1444 break;
1445 }
1446 search_tbl->current_rate = rate_n_flags_from_tbl(
1447 search_tbl, index, is_green);
1448 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001449 goto out;
1450 }
1451 tbl->action++;
1452 if (tbl->action > IWL_SISO_SWITCH_GI)
1453 tbl->action = IWL_SISO_SWITCH_ANTENNA;
1454
1455 if (tbl->action == start_action)
1456 break;
1457 }
1458 return 0;
1459
1460 out:
1461 tbl->action++;
1462 if (tbl->action > IWL_SISO_SWITCH_GI)
1463 tbl->action = IWL_SISO_SWITCH_ANTENNA;
1464 return 0;
1465}
1466
Ben Cahill77626352007-11-29 11:09:44 +08001467/*
1468 * Try to switch to new modulation mode from MIMO
1469 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001470static int rs_move_mimo_to_other(struct iwl_priv *priv,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001471 struct iwl4965_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001472 struct ieee80211_conf *conf,
1473 struct sta_info *sta,
Zhu Yib481de92007-09-25 17:54:57 -07001474 int index)
1475{
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001476 int ret;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001477 s8 is_green = lq_sta->is_green;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001478 struct iwl4965_scale_tbl_info *tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001479 &(lq_sta->lq_info[lq_sta->active_tbl]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001480 struct iwl4965_scale_tbl_info *search_tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001481 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001482 u32 sz = (sizeof(struct iwl4965_scale_tbl_info) -
1483 (sizeof(struct iwl4965_rate_scale_data) * IWL_RATE_COUNT));
Zhu Yib481de92007-09-25 17:54:57 -07001484 u8 start_action = tbl->action;
1485
1486 for (;;) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001487 lq_sta->action_counter++;
Zhu Yib481de92007-09-25 17:54:57 -07001488 switch (tbl->action) {
1489 case IWL_MIMO_SWITCH_ANTENNA_A:
1490 case IWL_MIMO_SWITCH_ANTENNA_B:
Guy Coheneecd6e52008-04-23 17:14:58 -07001491 IWL_DEBUG_RATE("LQ: MIMO2 switch to SISO\n");
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001492
Ben Cahill77626352007-11-29 11:09:44 +08001493 /* Set up new search table for SISO */
Zhu Yib481de92007-09-25 17:54:57 -07001494 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001495
1496 /*FIXME:RS:need to check ant validity + C*/
Zhu Yib481de92007-09-25 17:54:57 -07001497 if (tbl->action == IWL_MIMO_SWITCH_ANTENNA_A)
Guy Cohenfde0db32008-04-21 15:42:01 -07001498 search_tbl->ant_type = ANT_A;
Zhu Yib481de92007-09-25 17:54:57 -07001499 else
Guy Cohenfde0db32008-04-21 15:42:01 -07001500 search_tbl->ant_type = ANT_B;
Zhu Yib481de92007-09-25 17:54:57 -07001501
Tomas Winklerc33104f2008-01-14 17:46:21 -08001502 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001503 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001504 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001505 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001506 goto out;
1507 }
1508 break;
1509
1510 case IWL_MIMO_SWITCH_GI:
Guy Coheneecd6e52008-04-23 17:14:58 -07001511 IWL_DEBUG_RATE("LQ: MIMO toggle SGI/NGI\n");
Ben Cahill77626352007-11-29 11:09:44 +08001512
1513 /* Set up new search table for MIMO */
Zhu Yib481de92007-09-25 17:54:57 -07001514 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001515 search_tbl->is_SGI = !tbl->is_SGI;
Guy Coheneecd6e52008-04-23 17:14:58 -07001516 rs_set_expected_tpt_table(lq_sta, search_tbl);
Ben Cahill77626352007-11-29 11:09:44 +08001517 /*
1518 * If active table already uses the fastest possible
1519 * modulation (dual stream with short guard interval),
1520 * and it's working well, there's no need to look
1521 * for a better type of modulation!
1522 */
Guy Cohen39e88502008-04-23 17:14:57 -07001523 if (tbl->is_SGI) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001524 s32 tpt = lq_sta->last_tpt / 100;
Guy Cohen39e88502008-04-23 17:14:57 -07001525 if (tpt >= search_tbl->expected_tpt[index])
1526 break;
Zhu Yib481de92007-09-25 17:54:57 -07001527 }
Guy Cohen39e88502008-04-23 17:14:57 -07001528 search_tbl->current_rate = rate_n_flags_from_tbl(
1529 search_tbl, index, is_green);
1530 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001531 goto out;
1532
1533 }
1534 tbl->action++;
1535 if (tbl->action > IWL_MIMO_SWITCH_GI)
1536 tbl->action = IWL_MIMO_SWITCH_ANTENNA_A;
1537
1538 if (tbl->action == start_action)
1539 break;
1540 }
1541
1542 return 0;
1543 out:
1544 tbl->action++;
1545 if (tbl->action > IWL_MIMO_SWITCH_GI)
1546 tbl->action = IWL_MIMO_SWITCH_ANTENNA_A;
1547 return 0;
1548
1549}
1550
Ben Cahill77626352007-11-29 11:09:44 +08001551/*
1552 * Check whether we should continue using same modulation mode, or
1553 * begin search for a new mode, based on:
1554 * 1) # tx successes or failures while using this mode
1555 * 2) # times calling this function
1556 * 3) elapsed time in this mode (not used, for now)
1557 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001558static void rs_stay_in_table(struct iwl4965_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07001559{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001560 struct iwl4965_scale_tbl_info *tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001561 int i;
1562 int active_tbl;
1563 int flush_interval_passed = 0;
1564
Tomas Winklerc33104f2008-01-14 17:46:21 -08001565 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001566
Tomas Winklerc33104f2008-01-14 17:46:21 -08001567 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07001568
Ben Cahill77626352007-11-29 11:09:44 +08001569 /* If we've been disallowing search, see if we should now allow it */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001570 if (lq_sta->stay_in_tbl) {
Zhu Yib481de92007-09-25 17:54:57 -07001571
Ben Cahill77626352007-11-29 11:09:44 +08001572 /* Elapsed time using current modulation mode */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001573 if (lq_sta->flush_timer)
Zhu Yib481de92007-09-25 17:54:57 -07001574 flush_interval_passed =
1575 time_after(jiffies,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001576 (unsigned long)(lq_sta->flush_timer +
Zhu Yib481de92007-09-25 17:54:57 -07001577 IWL_RATE_SCALE_FLUSH_INTVL));
1578
Ben Cahill77626352007-11-29 11:09:44 +08001579 /* For now, disable the elapsed time criterion */
Zhu Yib481de92007-09-25 17:54:57 -07001580 flush_interval_passed = 0;
Ben Cahill77626352007-11-29 11:09:44 +08001581
1582 /*
1583 * Check if we should allow search for new modulation mode.
1584 * If many frames have failed or succeeded, or we've used
1585 * this same modulation for a long time, allow search, and
1586 * reset history stats that keep track of whether we should
1587 * allow a new search. Also (below) reset all bitmaps and
1588 * stats in active history.
1589 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001590 if ((lq_sta->total_failed > lq_sta->max_failure_limit) ||
1591 (lq_sta->total_success > lq_sta->max_success_limit) ||
1592 ((!lq_sta->search_better_tbl) && (lq_sta->flush_timer)
Zhu Yib481de92007-09-25 17:54:57 -07001593 && (flush_interval_passed))) {
Guy Coheneecd6e52008-04-23 17:14:58 -07001594 IWL_DEBUG_RATE("LQ: stay is expired %d %d %d\n:",
Tomas Winklerc33104f2008-01-14 17:46:21 -08001595 lq_sta->total_failed,
1596 lq_sta->total_success,
Zhu Yib481de92007-09-25 17:54:57 -07001597 flush_interval_passed);
Ben Cahill77626352007-11-29 11:09:44 +08001598
1599 /* Allow search for new mode */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001600 lq_sta->stay_in_tbl = 0; /* only place reset */
1601 lq_sta->total_failed = 0;
1602 lq_sta->total_success = 0;
1603 lq_sta->flush_timer = 0;
Ben Cahill77626352007-11-29 11:09:44 +08001604
1605 /*
1606 * Else if we've used this modulation mode enough repetitions
1607 * (regardless of elapsed time or success/failure), reset
1608 * history bitmaps and rate-specific stats for all rates in
1609 * active table.
1610 */
Mohamed Abbas403ab562007-11-06 22:06:25 -08001611 } else {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001612 lq_sta->table_count++;
1613 if (lq_sta->table_count >=
1614 lq_sta->table_count_limit) {
1615 lq_sta->table_count = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001616
Guy Coheneecd6e52008-04-23 17:14:58 -07001617 IWL_DEBUG_RATE("LQ: stay in table clear win\n");
Zhu Yib481de92007-09-25 17:54:57 -07001618 for (i = 0; i < IWL_RATE_COUNT; i++)
1619 rs_rate_scale_clear_window(
1620 &(tbl->win[i]));
1621 }
1622 }
1623
Ben Cahill77626352007-11-29 11:09:44 +08001624 /* If transitioning to allow "search", reset all history
1625 * bitmaps and stats in active table (this will become the new
1626 * "search" table). */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001627 if (!lq_sta->stay_in_tbl) {
Zhu Yib481de92007-09-25 17:54:57 -07001628 for (i = 0; i < IWL_RATE_COUNT; i++)
1629 rs_rate_scale_clear_window(&(tbl->win[i]));
1630 }
1631 }
1632}
1633
Ben Cahill77626352007-11-29 11:09:44 +08001634/*
1635 * Do rate scaling and search for new modulation mode.
1636 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001637static void rs_rate_scale_perform(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001638 struct net_device *dev,
1639 struct ieee80211_hdr *hdr,
1640 struct sta_info *sta)
1641{
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001642 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1643 struct ieee80211_hw *hw = local_to_hw(local);
1644 struct ieee80211_conf *conf = &hw->conf;
Zhu Yib481de92007-09-25 17:54:57 -07001645 int low = IWL_RATE_INVALID;
1646 int high = IWL_RATE_INVALID;
1647 int index;
1648 int i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001649 struct iwl4965_rate_scale_data *window = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001650 int current_tpt = IWL_INVALID_VALUE;
1651 int low_tpt = IWL_INVALID_VALUE;
1652 int high_tpt = IWL_INVALID_VALUE;
1653 u32 fail_count;
1654 s8 scale_action = 0;
1655 u16 fc, rate_mask;
1656 u8 update_lq = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001657 struct iwl4965_lq_sta *lq_sta;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001658 struct iwl4965_scale_tbl_info *tbl, *tbl1;
Zhu Yib481de92007-09-25 17:54:57 -07001659 u16 rate_scale_index_msk = 0;
Guy Cohen39e88502008-04-23 17:14:57 -07001660 u32 rate;
Zhu Yib481de92007-09-25 17:54:57 -07001661 u8 is_green = 0;
1662 u8 active_tbl = 0;
1663 u8 done_search = 0;
1664 u16 high_low;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001665#ifdef CONFIG_IWL4965_HT
1666 u8 tid = MAX_TID_COUNT;
1667 __le16 *qc;
1668#endif
Zhu Yib481de92007-09-25 17:54:57 -07001669
1670 IWL_DEBUG_RATE("rate scale calculate new rate for skb\n");
1671
1672 fc = le16_to_cpu(hdr->frame_control);
1673 if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1)) {
1674 /* Send management frames and broadcast/multicast data using
1675 * lowest rate. */
1676 /* TODO: this could probably be improved.. */
1677 return;
1678 }
1679
1680 if (!sta || !sta->rate_ctrl_priv)
1681 return;
1682
1683 if (!priv->lq_mngr.lq_ready) {
1684 IWL_DEBUG_RATE("still rate scaling not ready\n");
1685 return;
1686 }
Tomas Winklerc33104f2008-01-14 17:46:21 -08001687 lq_sta = (struct iwl4965_lq_sta *)sta->rate_ctrl_priv;
Zhu Yib481de92007-09-25 17:54:57 -07001688
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001689#ifdef CONFIG_IWL4965_HT
1690 qc = ieee80211_get_qos_ctrl(hdr);
1691 if (qc) {
1692 tid = (u8)(le16_to_cpu(*qc) & 0xf);
1693 rs_tl_add_packet(lq_sta, tid);
1694 }
1695#endif
Ben Cahill77626352007-11-29 11:09:44 +08001696 /*
1697 * Select rate-scale / modulation-mode table to work with in
1698 * the rest of this function: "search" if searching for better
1699 * modulation mode, or "active" if doing rate scaling within a mode.
1700 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001701 if (!lq_sta->search_better_tbl)
1702 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001703 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08001704 active_tbl = 1 - lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001705
Tomas Winklerc33104f2008-01-14 17:46:21 -08001706 tbl = &(lq_sta->lq_info[active_tbl]);
1707 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07001708
Ben Cahill77626352007-11-29 11:09:44 +08001709 /* current tx rate */
Johannes Berg8318d782008-01-24 19:38:38 +01001710 index = sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07001711
1712 IWL_DEBUG_RATE("Rate scale index %d for type %d\n", index,
1713 tbl->lq_type);
1714
Ben Cahill77626352007-11-29 11:09:44 +08001715 /* rates available for this association, and for modulation mode */
Guy Coheneecd6e52008-04-23 17:14:58 -07001716 rate_mask = rs_get_supported_rates(lq_sta, hdr, tbl->lq_type);
Zhu Yib481de92007-09-25 17:54:57 -07001717
1718 IWL_DEBUG_RATE("mask 0x%04X \n", rate_mask);
1719
1720 /* mask with station rate restriction */
1721 if (is_legacy(tbl->lq_type)) {
Johannes Berg8318d782008-01-24 19:38:38 +01001722 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Ben Cahill77626352007-11-29 11:09:44 +08001723 /* supp_rates has no CCK bits in A mode */
Zhu Yib481de92007-09-25 17:54:57 -07001724 rate_scale_index_msk = (u16) (rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -08001725 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
Zhu Yib481de92007-09-25 17:54:57 -07001726 else
1727 rate_scale_index_msk = (u16) (rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -08001728 lq_sta->supp_rates);
Zhu Yib481de92007-09-25 17:54:57 -07001729
1730 } else
1731 rate_scale_index_msk = rate_mask;
1732
1733 if (!rate_scale_index_msk)
1734 rate_scale_index_msk = rate_mask;
1735
Ben Cahill77626352007-11-29 11:09:44 +08001736 /* If current rate is no longer supported on current association,
1737 * or user changed preferences for rates, find a new supported rate. */
Zhu Yib481de92007-09-25 17:54:57 -07001738 if (index < 0 || !((1 << index) & rate_scale_index_msk)) {
1739 index = IWL_INVALID_VALUE;
1740 update_lq = 1;
1741
Ben Cahill77626352007-11-29 11:09:44 +08001742 /* get the highest available rate */
Zhu Yib481de92007-09-25 17:54:57 -07001743 for (i = 0; i <= IWL_RATE_COUNT; i++) {
1744 if ((1 << i) & rate_scale_index_msk)
1745 index = i;
1746 }
1747
1748 if (index == IWL_INVALID_VALUE) {
1749 IWL_WARNING("Can not find a suitable rate\n");
1750 return;
1751 }
1752 }
1753
Ben Cahill77626352007-11-29 11:09:44 +08001754 /* Get expected throughput table and history window for current rate */
Zhu Yib481de92007-09-25 17:54:57 -07001755 if (!tbl->expected_tpt)
Guy Coheneecd6e52008-04-23 17:14:58 -07001756 rs_set_expected_tpt_table(lq_sta, tbl);
Zhu Yib481de92007-09-25 17:54:57 -07001757
1758 window = &(tbl->win[index]);
1759
Ben Cahill77626352007-11-29 11:09:44 +08001760 /*
1761 * If there is not enough history to calculate actual average
1762 * throughput, keep analyzing results of more tx frames, without
1763 * changing rate or mode (bypass most of the rest of this function).
1764 * Set up new rate table in uCode only if old rate is not supported
1765 * in current association (use new rate found above).
1766 */
Zhu Yib481de92007-09-25 17:54:57 -07001767 fail_count = window->counter - window->success_counter;
1768 if (((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
1769 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH))
1770 || (tbl->expected_tpt == NULL)) {
1771 IWL_DEBUG_RATE("LQ: still below TH succ %d total %d "
1772 "for index %d\n",
1773 window->success_counter, window->counter, index);
Ben Cahill77626352007-11-29 11:09:44 +08001774
1775 /* Can't calculate this yet; not enough history */
Zhu Yib481de92007-09-25 17:54:57 -07001776 window->average_tpt = IWL_INVALID_VALUE;
Ben Cahill77626352007-11-29 11:09:44 +08001777
1778 /* Should we stay with this modulation mode,
1779 * or search for a new one? */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001780 rs_stay_in_table(lq_sta);
Ben Cahill77626352007-11-29 11:09:44 +08001781
1782 /* Set up new rate table in uCode, if needed */
Zhu Yib481de92007-09-25 17:54:57 -07001783 if (update_lq) {
Guy Cohen39e88502008-04-23 17:14:57 -07001784 rate = rate_n_flags_from_tbl(tbl, index, is_green);
1785 rs_fill_link_cmd(priv, lq_sta, rate, &lq_sta->lq);
Tomas Winkler66c73db2008-04-15 16:01:40 -07001786 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07001787 }
1788 goto out;
1789
Ben Cahill77626352007-11-29 11:09:44 +08001790 /* Else we have enough samples; calculate estimate of
1791 * actual average throughput */
Zhu Yib481de92007-09-25 17:54:57 -07001792 } else
1793 window->average_tpt = ((window->success_ratio *
1794 tbl->expected_tpt[index] + 64) / 128);
1795
Ben Cahill77626352007-11-29 11:09:44 +08001796 /* If we are searching for better modulation mode, check success. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001797 if (lq_sta->search_better_tbl) {
Zhu Yib481de92007-09-25 17:54:57 -07001798 int success_limit = IWL_RATE_SCALE_SWITCH;
1799
Ben Cahill77626352007-11-29 11:09:44 +08001800 /* If good success, continue using the "search" mode;
1801 * no need to send new link quality command, since we're
1802 * continuing to use the setup that we've been trying. */
Zhu Yib481de92007-09-25 17:54:57 -07001803 if ((window->success_ratio > success_limit) ||
Tomas Winklerc33104f2008-01-14 17:46:21 -08001804 (window->average_tpt > lq_sta->last_tpt)) {
Zhu Yib481de92007-09-25 17:54:57 -07001805 if (!is_legacy(tbl->lq_type)) {
Guy Coheneecd6e52008-04-23 17:14:58 -07001806 IWL_DEBUG_RATE("LQ: we are switching to HT"
Zhu Yib481de92007-09-25 17:54:57 -07001807 " rate suc %d current tpt %d"
1808 " old tpt %d\n",
1809 window->success_ratio,
1810 window->average_tpt,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001811 lq_sta->last_tpt);
1812 lq_sta->enable_counter = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001813 }
Ben Cahill77626352007-11-29 11:09:44 +08001814 /* Swap tables; "search" becomes "active" */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001815 lq_sta->active_tbl = active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001816 current_tpt = window->average_tpt;
Ben Cahill77626352007-11-29 11:09:44 +08001817
1818 /* Else poor success; go back to mode in "active" table */
Zhu Yib481de92007-09-25 17:54:57 -07001819 } else {
Ben Cahill77626352007-11-29 11:09:44 +08001820 /* Nullify "search" table */
Zhu Yib481de92007-09-25 17:54:57 -07001821 tbl->lq_type = LQ_NONE;
Ben Cahill77626352007-11-29 11:09:44 +08001822
1823 /* Revert to "active" table */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001824 active_tbl = lq_sta->active_tbl;
1825 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07001826
Ben Cahill77626352007-11-29 11:09:44 +08001827 /* Revert to "active" rate and throughput info */
Tomas Winkler17744ff2008-03-02 01:52:00 +02001828 index = iwl4965_hwrate_to_plcp_idx(
Guy Cohen39e88502008-04-23 17:14:57 -07001829 tbl->current_rate);
Tomas Winklerc33104f2008-01-14 17:46:21 -08001830 current_tpt = lq_sta->last_tpt;
Ben Cahill77626352007-11-29 11:09:44 +08001831
1832 /* Need to set up a new rate table in uCode */
1833 update_lq = 1;
Guy Coheneecd6e52008-04-23 17:14:58 -07001834 IWL_DEBUG_RATE("XXY GO BACK TO OLD TABLE\n");
Zhu Yib481de92007-09-25 17:54:57 -07001835 }
Ben Cahill77626352007-11-29 11:09:44 +08001836
1837 /* Either way, we've made a decision; modulation mode
1838 * search is done, allow rate adjustment next time. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001839 lq_sta->search_better_tbl = 0;
Ben Cahill77626352007-11-29 11:09:44 +08001840 done_search = 1; /* Don't switch modes below! */
Zhu Yib481de92007-09-25 17:54:57 -07001841 goto lq_update;
1842 }
1843
Ben Cahill77626352007-11-29 11:09:44 +08001844 /* (Else) not in search of better modulation mode, try for better
1845 * starting rate, while staying in this mode. */
Zhu Yib481de92007-09-25 17:54:57 -07001846 high_low = rs_get_adjacent_rate(index, rate_scale_index_msk,
1847 tbl->lq_type);
1848 low = high_low & 0xff;
1849 high = (high_low >> 8) & 0xff;
1850
Ben Cahill77626352007-11-29 11:09:44 +08001851 /* Collect measured throughputs for current and adjacent rates */
Zhu Yib481de92007-09-25 17:54:57 -07001852 current_tpt = window->average_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07001853 if (low != IWL_RATE_INVALID)
1854 low_tpt = tbl->win[low].average_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07001855 if (high != IWL_RATE_INVALID)
1856 high_tpt = tbl->win[high].average_tpt;
1857
Ben Cahill77626352007-11-29 11:09:44 +08001858 /* Assume rate increase */
Zhu Yib481de92007-09-25 17:54:57 -07001859 scale_action = 1;
1860
Ben Cahill77626352007-11-29 11:09:44 +08001861 /* Too many failures, decrease rate */
Zhu Yib481de92007-09-25 17:54:57 -07001862 if ((window->success_ratio <= IWL_RATE_DECREASE_TH) ||
1863 (current_tpt == 0)) {
1864 IWL_DEBUG_RATE("decrease rate because of low success_ratio\n");
1865 scale_action = -1;
Ben Cahill77626352007-11-29 11:09:44 +08001866
1867 /* No throughput measured yet for adjacent rates; try increase. */
Zhu Yib481de92007-09-25 17:54:57 -07001868 } else if ((low_tpt == IWL_INVALID_VALUE) &&
1869 (high_tpt == IWL_INVALID_VALUE))
1870 scale_action = 1;
Ben Cahill77626352007-11-29 11:09:44 +08001871
1872 /* Both adjacent throughputs are measured, but neither one has better
1873 * throughput; we're using the best rate, don't change it! */
Zhu Yib481de92007-09-25 17:54:57 -07001874 else if ((low_tpt != IWL_INVALID_VALUE) &&
1875 (high_tpt != IWL_INVALID_VALUE) &&
1876 (low_tpt < current_tpt) &&
1877 (high_tpt < current_tpt))
1878 scale_action = 0;
Ben Cahill77626352007-11-29 11:09:44 +08001879
1880 /* At least one adjacent rate's throughput is measured,
1881 * and may have better performance. */
Zhu Yib481de92007-09-25 17:54:57 -07001882 else {
Ben Cahill77626352007-11-29 11:09:44 +08001883 /* Higher adjacent rate's throughput is measured */
Zhu Yib481de92007-09-25 17:54:57 -07001884 if (high_tpt != IWL_INVALID_VALUE) {
Ben Cahill77626352007-11-29 11:09:44 +08001885 /* Higher rate has better throughput */
Zhu Yib481de92007-09-25 17:54:57 -07001886 if (high_tpt > current_tpt)
1887 scale_action = 1;
1888 else {
1889 IWL_DEBUG_RATE
1890 ("decrease rate because of high tpt\n");
1891 scale_action = -1;
1892 }
Ben Cahill77626352007-11-29 11:09:44 +08001893
1894 /* Lower adjacent rate's throughput is measured */
Zhu Yib481de92007-09-25 17:54:57 -07001895 } else if (low_tpt != IWL_INVALID_VALUE) {
Ben Cahill77626352007-11-29 11:09:44 +08001896 /* Lower rate has better throughput */
Zhu Yib481de92007-09-25 17:54:57 -07001897 if (low_tpt > current_tpt) {
1898 IWL_DEBUG_RATE
1899 ("decrease rate because of low tpt\n");
1900 scale_action = -1;
1901 } else
1902 scale_action = 1;
1903 }
1904 }
1905
Ben Cahill77626352007-11-29 11:09:44 +08001906 /* Sanity check; asked for decrease, but success rate or throughput
1907 * has been good at old rate. Don't change it. */
Zhu Yib481de92007-09-25 17:54:57 -07001908 if (scale_action == -1) {
1909 if ((low != IWL_RATE_INVALID) &&
1910 ((window->success_ratio > IWL_RATE_HIGH_TH) ||
1911 (current_tpt > (100 * tbl->expected_tpt[low]))))
1912 scale_action = 0;
Ben Cahill77626352007-11-29 11:09:44 +08001913
1914 /* Sanity check; asked for increase, but success rate has not been great
1915 * even at old rate, higher rate will be worse. Don't change it. */
Zhu Yib481de92007-09-25 17:54:57 -07001916 } else if ((scale_action == 1) &&
1917 (window->success_ratio < IWL_RATE_INCREASE_TH))
1918 scale_action = 0;
1919
1920 switch (scale_action) {
1921 case -1:
Ben Cahill77626352007-11-29 11:09:44 +08001922 /* Decrease starting rate, update uCode's rate table */
Zhu Yib481de92007-09-25 17:54:57 -07001923 if (low != IWL_RATE_INVALID) {
1924 update_lq = 1;
1925 index = low;
1926 }
1927 break;
1928 case 1:
Ben Cahill77626352007-11-29 11:09:44 +08001929 /* Increase starting rate, update uCode's rate table */
Zhu Yib481de92007-09-25 17:54:57 -07001930 if (high != IWL_RATE_INVALID) {
1931 update_lq = 1;
1932 index = high;
1933 }
1934
1935 break;
1936 case 0:
Ben Cahill77626352007-11-29 11:09:44 +08001937 /* No change */
Zhu Yib481de92007-09-25 17:54:57 -07001938 default:
1939 break;
1940 }
1941
Guy Coheneecd6e52008-04-23 17:14:58 -07001942 IWL_DEBUG_RATE("choose rate scale index %d action %d low %d "
Zhu Yib481de92007-09-25 17:54:57 -07001943 "high %d type %d\n",
1944 index, scale_action, low, high, tbl->lq_type);
1945
1946 lq_update:
Ben Cahill77626352007-11-29 11:09:44 +08001947 /* Replace uCode's rate table for the destination station. */
Zhu Yib481de92007-09-25 17:54:57 -07001948 if (update_lq) {
Guy Cohen39e88502008-04-23 17:14:57 -07001949 rate = rate_n_flags_from_tbl(tbl, index, is_green);
1950 rs_fill_link_cmd(priv, lq_sta, rate, &lq_sta->lq);
Tomas Winkler66c73db2008-04-15 16:01:40 -07001951 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07001952 }
Ben Cahill77626352007-11-29 11:09:44 +08001953
1954 /* Should we stay with this modulation mode, or search for a new one? */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001955 rs_stay_in_table(lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07001956
Ben Cahill77626352007-11-29 11:09:44 +08001957 /*
1958 * Search for new modulation mode if we're:
1959 * 1) Not changing rates right now
1960 * 2) Not just finishing up a search
1961 * 3) Allowing a new search
1962 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001963 if (!update_lq && !done_search && !lq_sta->stay_in_tbl) {
Ben Cahill77626352007-11-29 11:09:44 +08001964 /* Save current throughput to compare with "search" throughput*/
Tomas Winklerc33104f2008-01-14 17:46:21 -08001965 lq_sta->last_tpt = current_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07001966
Ben Cahill77626352007-11-29 11:09:44 +08001967 /* Select a new "search" modulation mode to try.
1968 * If one is found, set up the new "search" table. */
Zhu Yib481de92007-09-25 17:54:57 -07001969 if (is_legacy(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08001970 rs_move_legacy_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07001971 else if (is_siso(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08001972 rs_move_siso_to_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07001973 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08001974 rs_move_mimo_to_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07001975
Ben Cahill77626352007-11-29 11:09:44 +08001976 /* If new "search" mode was selected, set up in uCode table */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001977 if (lq_sta->search_better_tbl) {
Ben Cahill77626352007-11-29 11:09:44 +08001978 /* Access the "search" table, clear its history. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001979 tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Zhu Yib481de92007-09-25 17:54:57 -07001980 for (i = 0; i < IWL_RATE_COUNT; i++)
1981 rs_rate_scale_clear_window(&(tbl->win[i]));
1982
Ben Cahill77626352007-11-29 11:09:44 +08001983 /* Use new "search" start rate */
Tomas Winkler17744ff2008-03-02 01:52:00 +02001984 index = iwl4965_hwrate_to_plcp_idx(
Guy Cohen39e88502008-04-23 17:14:57 -07001985 tbl->current_rate);
Zhu Yib481de92007-09-25 17:54:57 -07001986
Guy Coheneecd6e52008-04-23 17:14:58 -07001987 IWL_DEBUG_RATE("Switch current mcs: %X index: %d\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001988 tbl->current_rate, index);
1989 rs_fill_link_cmd(priv, lq_sta, tbl->current_rate,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001990 &lq_sta->lq);
Tomas Winkler66c73db2008-04-15 16:01:40 -07001991 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07001992 }
Zhu Yib481de92007-09-25 17:54:57 -07001993
Ben Cahill77626352007-11-29 11:09:44 +08001994 /* If the "active" (non-search) mode was legacy,
1995 * and we've tried switching antennas,
1996 * but we haven't been able to try HT modes (not available),
1997 * stay with best antenna legacy modulation for a while
1998 * before next round of mode comparisons. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001999 tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07002000 if (is_legacy(tbl1->lq_type) &&
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002001#ifdef CONFIG_IWL4965_HT
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002002 (!(conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE)) &&
Zhu Yib481de92007-09-25 17:54:57 -07002003#endif
Tomas Winklerc33104f2008-01-14 17:46:21 -08002004 (lq_sta->action_counter >= 1)) {
2005 lq_sta->action_counter = 0;
Guy Coheneecd6e52008-04-23 17:14:58 -07002006 IWL_DEBUG_RATE("LQ: STAY in legacy table\n");
Tomas Winklerc33104f2008-01-14 17:46:21 -08002007 rs_set_stay_in_table(1, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002008 }
2009
Ben Cahill77626352007-11-29 11:09:44 +08002010 /* If we're in an HT mode, and all 3 mode switch actions
2011 * have been tried and compared, stay in this best modulation
2012 * mode for a while before next round of mode comparisons. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002013 if (lq_sta->enable_counter &&
2014 (lq_sta->action_counter >= IWL_ACTION_LIMIT)) {
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002015#ifdef CONFIG_IWL4965_HT
2016 if ((lq_sta->last_tpt > IWL_AGG_TPT_THREHOLD) &&
2017 (lq_sta->tx_agg_tid_en & (1 << tid)) &&
2018 (tid != MAX_TID_COUNT)) {
Guy Coheneecd6e52008-04-23 17:14:58 -07002019 IWL_DEBUG_RATE("try to aggregate tid %d\n", tid);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002020 rs_tl_turn_on_agg(priv, tid, lq_sta, sta);
Zhu Yib481de92007-09-25 17:54:57 -07002021 }
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002022#endif /*CONFIG_IWL4965_HT */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002023 lq_sta->action_counter = 0;
2024 rs_set_stay_in_table(0, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002025 }
Ben Cahill77626352007-11-29 11:09:44 +08002026
2027 /*
2028 * Else, don't search for a new modulation mode.
2029 * Put new timestamp in stay-in-modulation-mode flush timer if:
2030 * 1) Not changing rates right now
2031 * 2) Not just finishing up a search
2032 * 3) flush timer is empty
2033 */
Zhu Yib481de92007-09-25 17:54:57 -07002034 } else {
Tomas Winklerc33104f2008-01-14 17:46:21 -08002035 if ((!update_lq) && (!done_search) && (!lq_sta->flush_timer))
2036 lq_sta->flush_timer = jiffies;
Zhu Yib481de92007-09-25 17:54:57 -07002037 }
2038
2039out:
Guy Cohen39e88502008-04-23 17:14:57 -07002040 tbl->current_rate = rate_n_flags_from_tbl(tbl, index, is_green);
Zhu Yib481de92007-09-25 17:54:57 -07002041 i = index;
Johannes Berg8318d782008-01-24 19:38:38 +01002042 sta->last_txrate_idx = i;
Zhu Yib481de92007-09-25 17:54:57 -07002043
Johannes Berg8318d782008-01-24 19:38:38 +01002044 /* sta->txrate_idx is an index to A mode rates which start
Zhu Yib481de92007-09-25 17:54:57 -07002045 * at IWL_FIRST_OFDM_RATE
2046 */
Johannes Berg8318d782008-01-24 19:38:38 +01002047 if (lq_sta->band == IEEE80211_BAND_5GHZ)
2048 sta->txrate_idx = i - IWL_FIRST_OFDM_RATE;
Zhu Yib481de92007-09-25 17:54:57 -07002049 else
Johannes Berg8318d782008-01-24 19:38:38 +01002050 sta->txrate_idx = i;
Zhu Yib481de92007-09-25 17:54:57 -07002051
2052 return;
2053}
2054
2055
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002056static void rs_initialize_lq(struct iwl_priv *priv,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002057 struct ieee80211_conf *conf,
Zhu Yib481de92007-09-25 17:54:57 -07002058 struct sta_info *sta)
2059{
2060 int i;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002061 struct iwl4965_lq_sta *lq_sta;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002062 struct iwl4965_scale_tbl_info *tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002063 u8 active_tbl = 0;
2064 int rate_idx;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002065 u8 use_green = rs_use_green(priv, conf);
Guy Cohen39e88502008-04-23 17:14:57 -07002066 u32 rate;
Zhu Yib481de92007-09-25 17:54:57 -07002067
2068 if (!sta || !sta->rate_ctrl_priv)
2069 goto out;
2070
Tomas Winklerc33104f2008-01-14 17:46:21 -08002071 lq_sta = (struct iwl4965_lq_sta *)sta->rate_ctrl_priv;
Johannes Berg8318d782008-01-24 19:38:38 +01002072 i = sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002073
Tomas Winklerc33104f2008-01-14 17:46:21 -08002074 if ((lq_sta->lq.sta_id == 0xff) &&
Zhu Yib481de92007-09-25 17:54:57 -07002075 (priv->iw_mode == IEEE80211_IF_TYPE_IBSS))
2076 goto out;
2077
Tomas Winklerc33104f2008-01-14 17:46:21 -08002078 if (!lq_sta->search_better_tbl)
2079 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002080 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08002081 active_tbl = 1 - lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002082
Tomas Winklerc33104f2008-01-14 17:46:21 -08002083 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07002084
2085 if ((i < 0) || (i >= IWL_RATE_COUNT))
2086 i = 0;
2087
Guy Cohenfde0db32008-04-21 15:42:01 -07002088 /* FIXME:RS: This is also wrong in 4965 */
Guy Cohen39e88502008-04-23 17:14:57 -07002089 rate = iwl4965_rates[i].plcp;
2090 rate |= RATE_MCS_ANT_B_MSK;
2091 rate &= ~RATE_MCS_ANT_A_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002092
2093 if (i >= IWL_FIRST_CCK_RATE && i <= IWL_LAST_CCK_RATE)
Guy Cohen39e88502008-04-23 17:14:57 -07002094 rate |= RATE_MCS_CCK_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002095
Guy Cohenfde0db32008-04-21 15:42:01 -07002096 tbl->ant_type = ANT_B;
Guy Cohen39e88502008-04-23 17:14:57 -07002097 rs_get_tbl_info_from_mcs(rate, priv->band, tbl, &rate_idx);
Guy Cohenfde0db32008-04-21 15:42:01 -07002098 if (!rs_is_ant_connected(priv->hw_params.valid_tx_ant, tbl->ant_type))
Guy Cohen39e88502008-04-23 17:14:57 -07002099 rs_toggle_antenna(&rate, tbl);
Zhu Yib481de92007-09-25 17:54:57 -07002100
Guy Cohen39e88502008-04-23 17:14:57 -07002101 rate = rate_n_flags_from_tbl(tbl, rate_idx, use_green);
2102 tbl->current_rate = rate;
Guy Coheneecd6e52008-04-23 17:14:58 -07002103 rs_set_expected_tpt_table(lq_sta, tbl);
Guy Cohen39e88502008-04-23 17:14:57 -07002104 rs_fill_link_cmd(NULL, lq_sta, rate, &lq_sta->lq);
Tomas Winkler66c73db2008-04-15 16:01:40 -07002105 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07002106 out:
2107 return;
2108}
2109
Mattias Nissler1abbe492007-12-20 13:50:07 +01002110static void rs_get_rate(void *priv_rate, struct net_device *dev,
Johannes Berg8318d782008-01-24 19:38:38 +01002111 struct ieee80211_supported_band *sband,
2112 struct sk_buff *skb,
Mattias Nissler1abbe492007-12-20 13:50:07 +01002113 struct rate_selection *sel)
Zhu Yib481de92007-09-25 17:54:57 -07002114{
2115
2116 int i;
2117 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002118 struct ieee80211_conf *conf = &local->hw.conf;
Zhu Yib481de92007-09-25 17:54:57 -07002119 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2120 struct sta_info *sta;
Michael Wu2bc454b2007-12-25 19:33:16 -05002121 u16 fc;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002122 struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002123 struct iwl4965_lq_sta *lq_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002124
Zhu Yi58826352007-09-27 11:27:39 +08002125 IWL_DEBUG_RATE_LIMIT("rate scale calculate new rate for skb\n");
Zhu Yib481de92007-09-25 17:54:57 -07002126
Johannes Bergd0709a62008-02-25 16:27:46 +01002127 rcu_read_lock();
2128
Zhu Yib481de92007-09-25 17:54:57 -07002129 sta = sta_info_get(local, hdr->addr1);
2130
Michael Wu2bc454b2007-12-25 19:33:16 -05002131 /* Send management frames and broadcast/multicast data using lowest
2132 * rate. */
2133 fc = le16_to_cpu(hdr->frame_control);
2134 if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1) ||
2135 !sta || !sta->rate_ctrl_priv) {
Johannes Berg8318d782008-01-24 19:38:38 +01002136 sel->rate = rate_lowest(local, sband, sta);
Tomas Winklerf4d60822008-03-05 11:31:00 -08002137 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07002138 }
2139
Tomas Winklerc33104f2008-01-14 17:46:21 -08002140 lq_sta = (struct iwl4965_lq_sta *)sta->rate_ctrl_priv;
Johannes Berg8318d782008-01-24 19:38:38 +01002141 i = sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002142
Tomas Winklerc33104f2008-01-14 17:46:21 -08002143 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
2144 !lq_sta->ibss_sta_added) {
Tomas Winkler947b13a2008-04-16 16:34:48 -07002145 u8 sta_id = iwl_find_station(priv, hdr->addr1);
Joe Perches0795af52007-10-03 17:59:30 -07002146 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07002147
2148 if (sta_id == IWL_INVALID_STATION) {
Joe Perches0795af52007-10-03 17:59:30 -07002149 IWL_DEBUG_RATE("LQ: ADD station %s\n",
2150 print_mac(mac, hdr->addr1));
Ron Rindjunsky67d62032007-11-26 16:14:40 +02002151 sta_id = iwl4965_add_station_flags(priv, hdr->addr1,
2152 0, CMD_ASYNC, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07002153 }
2154 if ((sta_id != IWL_INVALID_STATION)) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08002155 lq_sta->lq.sta_id = sta_id;
2156 lq_sta->lq.rs_table[0].rate_n_flags = 0;
2157 lq_sta->ibss_sta_added = 1;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002158 rs_initialize_lq(priv, conf, sta);
Zhu Yib481de92007-09-25 17:54:57 -07002159 }
Tomas Winklerc33104f2008-01-14 17:46:21 -08002160 if (!lq_sta->ibss_sta_added)
Zhu Yib481de92007-09-25 17:54:57 -07002161 goto done;
2162 }
2163
Tomas Winklerf4d60822008-03-05 11:31:00 -08002164done:
Mattias Nissler1abbe492007-12-20 13:50:07 +01002165 if ((i < 0) || (i > IWL_RATE_COUNT)) {
Johannes Berg8318d782008-01-24 19:38:38 +01002166 sel->rate = rate_lowest(local, sband, sta);
Tomas Winklerf4d60822008-03-05 11:31:00 -08002167 goto out;
Mattias Nissler1abbe492007-12-20 13:50:07 +01002168 }
Zhu Yib481de92007-09-25 17:54:57 -07002169
Mattias Nissler1abbe492007-12-20 13:50:07 +01002170 sel->rate = &priv->ieee_rates[i];
Tomas Winklerf4d60822008-03-05 11:31:00 -08002171out:
2172 rcu_read_unlock();
Zhu Yib481de92007-09-25 17:54:57 -07002173}
2174
2175static void *rs_alloc_sta(void *priv, gfp_t gfp)
2176{
Tomas Winklerc33104f2008-01-14 17:46:21 -08002177 struct iwl4965_lq_sta *lq_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002178 int i, j;
2179
2180 IWL_DEBUG_RATE("create station rate scale window\n");
2181
Tomas Winklerc33104f2008-01-14 17:46:21 -08002182 lq_sta = kzalloc(sizeof(struct iwl4965_lq_sta), gfp);
Zhu Yib481de92007-09-25 17:54:57 -07002183
Tomas Winklerc33104f2008-01-14 17:46:21 -08002184 if (lq_sta == NULL)
Zhu Yib481de92007-09-25 17:54:57 -07002185 return NULL;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002186 lq_sta->lq.sta_id = 0xff;
Zhu Yib481de92007-09-25 17:54:57 -07002187
Zhu Yi63fddb92007-09-27 11:27:36 +08002188
Zhu Yib481de92007-09-25 17:54:57 -07002189 for (j = 0; j < LQ_SIZE; j++)
2190 for (i = 0; i < IWL_RATE_COUNT; i++)
Tomas Winklerc33104f2008-01-14 17:46:21 -08002191 rs_rate_scale_clear_window(&(lq_sta->lq_info[j].win[i]));
Zhu Yib481de92007-09-25 17:54:57 -07002192
Tomas Winklerc33104f2008-01-14 17:46:21 -08002193 return lq_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002194}
2195
2196static void rs_rate_init(void *priv_rate, void *priv_sta,
2197 struct ieee80211_local *local,
2198 struct sta_info *sta)
2199{
2200 int i, j;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002201 struct ieee80211_conf *conf = &local->hw.conf;
Johannes Berg8318d782008-01-24 19:38:38 +01002202 struct ieee80211_supported_band *sband;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002203 struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002204 struct iwl4965_lq_sta *lq_sta = priv_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002205
Johannes Berg8318d782008-01-24 19:38:38 +01002206 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2207
Tomas Winklerc33104f2008-01-14 17:46:21 -08002208 lq_sta->flush_timer = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01002209 lq_sta->supp_rates = sta->supp_rates[sband->band];
2210 sta->txrate_idx = 3;
Zhu Yib481de92007-09-25 17:54:57 -07002211 for (j = 0; j < LQ_SIZE; j++)
2212 for (i = 0; i < IWL_RATE_COUNT; i++)
Tomas Winklerc33104f2008-01-14 17:46:21 -08002213 rs_rate_scale_clear_window(&(lq_sta->lq_info[j].win[i]));
Zhu Yib481de92007-09-25 17:54:57 -07002214
2215 IWL_DEBUG_RATE("rate scale global init\n");
2216 /* TODO: what is a good starting rate for STA? About middle? Maybe not
2217 * the lowest or the highest rate.. Could consider using RSSI from
2218 * previous packets? Need to have IEEE 802.1X auth succeed immediately
2219 * after assoc.. */
2220
Tomas Winklerc33104f2008-01-14 17:46:21 -08002221 lq_sta->ibss_sta_added = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002222 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
Tomas Winkler947b13a2008-04-16 16:34:48 -07002223 u8 sta_id = iwl_find_station(priv, sta->addr);
Joe Perches0795af52007-10-03 17:59:30 -07002224 DECLARE_MAC_BUF(mac);
2225
Zhu Yib481de92007-09-25 17:54:57 -07002226 /* for IBSS the call are from tasklet */
Guy Coheneecd6e52008-04-23 17:14:58 -07002227 IWL_DEBUG_RATE("LQ: ADD station %s\n",
Joe Perches0795af52007-10-03 17:59:30 -07002228 print_mac(mac, sta->addr));
Zhu Yib481de92007-09-25 17:54:57 -07002229
2230 if (sta_id == IWL_INVALID_STATION) {
Joe Perches0795af52007-10-03 17:59:30 -07002231 IWL_DEBUG_RATE("LQ: ADD station %s\n",
2232 print_mac(mac, sta->addr));
Ron Rindjunsky67d62032007-11-26 16:14:40 +02002233 sta_id = iwl4965_add_station_flags(priv, sta->addr,
2234 0, CMD_ASYNC, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07002235 }
2236 if ((sta_id != IWL_INVALID_STATION)) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08002237 lq_sta->lq.sta_id = sta_id;
2238 lq_sta->lq.rs_table[0].rate_n_flags = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002239 }
2240 /* FIXME: this is w/a remove it later */
2241 priv->assoc_station_added = 1;
2242 }
2243
Ben Cahill77626352007-11-29 11:09:44 +08002244 /* Find highest tx rate supported by hardware and destination station */
Johannes Berg8318d782008-01-24 19:38:38 +01002245 for (i = 0; i < sband->n_bitrates; i++)
2246 if (sta->supp_rates[sband->band] & BIT(i))
2247 sta->txrate_idx = i;
2248
2249 sta->last_txrate_idx = sta->txrate_idx;
2250 /* WTF is with this bogus comment? A doesn't have cck rates */
Ben Cahill77626352007-11-29 11:09:44 +08002251 /* For MODE_IEEE80211A, cck rates are at end of rate table */
Johannes Berg8318d782008-01-24 19:38:38 +01002252 if (local->hw.conf.channel->band == IEEE80211_BAND_5GHZ)
2253 sta->last_txrate_idx += IWL_FIRST_OFDM_RATE;
Zhu Yib481de92007-09-25 17:54:57 -07002254
Tomas Winklerc33104f2008-01-14 17:46:21 -08002255 lq_sta->is_dup = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002256 lq_sta->is_green = rs_use_green(priv, conf);
Guy Cohen39e88502008-04-23 17:14:57 -07002257 lq_sta->active_legacy_rate = priv->active_rate & ~(0x1000);
Tomas Winklerc33104f2008-01-14 17:46:21 -08002258 lq_sta->active_rate_basic = priv->active_rate_basic;
Johannes Berg8318d782008-01-24 19:38:38 +01002259 lq_sta->band = priv->band;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002260#ifdef CONFIG_IWL4965_HT
Ben Cahill77626352007-11-29 11:09:44 +08002261 /*
2262 * active_siso_rate mask includes 9 MBits (bit 5), and CCK (bits 0-3),
2263 * supp_rates[] does not; shift to convert format, force 9 MBits off.
2264 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002265 lq_sta->active_siso_rate =
Guy Cohenfde0db32008-04-21 15:42:01 -07002266 priv->current_ht_config.supp_mcs_set[0] << 1;
2267 lq_sta->active_siso_rate |=
2268 priv->current_ht_config.supp_mcs_set[0] & 0x1;
2269 lq_sta->active_siso_rate &= ~((u16)0x2);
2270 lq_sta->active_siso_rate <<= IWL_FIRST_OFDM_RATE;
Zhu Yib481de92007-09-25 17:54:57 -07002271
Ben Cahill77626352007-11-29 11:09:44 +08002272 /* Same here */
Guy Cohenfde0db32008-04-21 15:42:01 -07002273 lq_sta->active_mimo2_rate =
2274 priv->current_ht_config.supp_mcs_set[1] << 1;
2275 lq_sta->active_mimo2_rate |=
2276 priv->current_ht_config.supp_mcs_set[1] & 0x1;
2277 lq_sta->active_mimo2_rate &= ~((u16)0x2);
2278 lq_sta->active_mimo2_rate <<= IWL_FIRST_OFDM_RATE;
2279
2280 lq_sta->active_mimo3_rate =
2281 priv->current_ht_config.supp_mcs_set[2] << 1;
2282 lq_sta->active_mimo3_rate |=
2283 priv->current_ht_config.supp_mcs_set[2] & 0x1;
2284 lq_sta->active_mimo3_rate &= ~((u16)0x2);
2285 lq_sta->active_mimo3_rate <<= IWL_FIRST_OFDM_RATE;
2286
Guy Coheneecd6e52008-04-23 17:14:58 -07002287 IWL_DEBUG_RATE("SISO RATE %X MIMO2 RATE %X MIMO3 RATE %X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002288 lq_sta->active_siso_rate,
Guy Cohenfde0db32008-04-21 15:42:01 -07002289 lq_sta->active_mimo2_rate,
2290 lq_sta->active_mimo3_rate);
2291
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002292 /* as default allow aggregation for all tids */
2293 lq_sta->tx_agg_tid_en = IWL_AGG_ALL_TID;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002294#endif /*CONFIG_IWL4965_HT*/
Zhu Yi98d7e092007-09-27 11:27:42 +08002295#ifdef CONFIG_MAC80211_DEBUGFS
Tomas Winklerc33104f2008-01-14 17:46:21 -08002296 lq_sta->drv = priv;
Zhu Yi98d7e092007-09-27 11:27:42 +08002297#endif
Zhu Yib481de92007-09-25 17:54:57 -07002298
2299 if (priv->assoc_station_added)
2300 priv->lq_mngr.lq_ready = 1;
2301
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002302 rs_initialize_lq(priv, conf, sta);
Zhu Yib481de92007-09-25 17:54:57 -07002303}
2304
Guy Cohenfde0db32008-04-21 15:42:01 -07002305static void rs_fill_link_cmd(const struct iwl_priv *priv,
2306 struct iwl4965_lq_sta *lq_sta,
Guy Cohen39e88502008-04-23 17:14:57 -07002307 u32 new_rate,
Guy Cohenfde0db32008-04-21 15:42:01 -07002308 struct iwl_link_quality_cmd *lq_cmd)
Zhu Yib481de92007-09-25 17:54:57 -07002309{
2310 int index = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002311 int rate_idx;
Zhu Yi1b696de2007-09-27 11:27:41 +08002312 int repeat_rate = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002313 u8 ant_toggle_count = 0;
2314 u8 use_ht_possible = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002315 struct iwl4965_scale_tbl_info tbl_type = { 0 };
Zhu Yib481de92007-09-25 17:54:57 -07002316
Ben Cahill77626352007-11-29 11:09:44 +08002317 /* Override starting rate (index 0) if needed for debug purposes */
Guy Cohen39e88502008-04-23 17:14:57 -07002318 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Zhu Yi98d7e092007-09-27 11:27:42 +08002319
Guy Cohen39e88502008-04-23 17:14:57 -07002320 /* Interpret new_rate (rate_n_flags) */
2321 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band,
Zhu Yib481de92007-09-25 17:54:57 -07002322 &tbl_type, &rate_idx);
2323
Ben Cahill77626352007-11-29 11:09:44 +08002324 /* How many times should we repeat the initial rate? */
Zhu Yib481de92007-09-25 17:54:57 -07002325 if (is_legacy(tbl_type.lq_type)) {
2326 ant_toggle_count = 1;
Zhu Yi1b696de2007-09-27 11:27:41 +08002327 repeat_rate = IWL_NUMBER_TRY;
Zhu Yib481de92007-09-25 17:54:57 -07002328 } else
Zhu Yi1b696de2007-09-27 11:27:41 +08002329 repeat_rate = IWL_HT_NUMBER_TRY;
Zhu Yib481de92007-09-25 17:54:57 -07002330
2331 lq_cmd->general_params.mimo_delimiter =
2332 is_mimo(tbl_type.lq_type) ? 1 : 0;
Ben Cahill77626352007-11-29 11:09:44 +08002333
2334 /* Fill 1st table entry (index 0) */
Guy Cohen39e88502008-04-23 17:14:57 -07002335 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
Zhu Yib481de92007-09-25 17:54:57 -07002336
Guy Cohenfde0db32008-04-21 15:42:01 -07002337 /*FIXME:RS*/
2338 if (is_mimo(tbl_type.lq_type) || (tbl_type.ant_type == ANT_A))
Ben Cahill77626352007-11-29 11:09:44 +08002339 lq_cmd->general_params.single_stream_ant_msk
2340 = LINK_QUAL_ANT_A_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002341 else
Ben Cahill77626352007-11-29 11:09:44 +08002342 lq_cmd->general_params.single_stream_ant_msk
2343 = LINK_QUAL_ANT_B_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002344
2345 index++;
Zhu Yi1b696de2007-09-27 11:27:41 +08002346 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002347
Ben Cahill77626352007-11-29 11:09:44 +08002348 /* Fill rest of rate table */
Zhu Yib481de92007-09-25 17:54:57 -07002349 while (index < LINK_QUAL_MAX_RETRY_NUM) {
Ben Cahill77626352007-11-29 11:09:44 +08002350 /* Repeat initial/next rate.
2351 * For legacy IWL_NUMBER_TRY == 1, this loop will not execute.
2352 * For HT IWL_HT_NUMBER_TRY == 3, this executes twice. */
Zhu Yi1b696de2007-09-27 11:27:41 +08002353 while (repeat_rate > 0 && (index < LINK_QUAL_MAX_RETRY_NUM)) {
Zhu Yib481de92007-09-25 17:54:57 -07002354 if (is_legacy(tbl_type.lq_type)) {
2355 if (ant_toggle_count <
2356 NUM_TRY_BEFORE_ANTENNA_TOGGLE)
2357 ant_toggle_count++;
Guy Cohenfde0db32008-04-21 15:42:01 -07002358 else if (priv && rs_is_other_ant_connected(
2359 priv->hw_params.valid_tx_ant,
2360 tbl_type.ant_type)) {
Zhu Yib481de92007-09-25 17:54:57 -07002361 rs_toggle_antenna(&new_rate, &tbl_type);
2362 ant_toggle_count = 1;
2363 }
2364 }
Zhu Yi98d7e092007-09-27 11:27:42 +08002365
Ben Cahill77626352007-11-29 11:09:44 +08002366 /* Override next rate if needed for debug purposes */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002367 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Ben Cahill77626352007-11-29 11:09:44 +08002368
2369 /* Fill next table entry */
Zhu Yib481de92007-09-25 17:54:57 -07002370 lq_cmd->rs_table[index].rate_n_flags =
Guy Cohen39e88502008-04-23 17:14:57 -07002371 cpu_to_le32(new_rate);
Zhu Yi1b696de2007-09-27 11:27:41 +08002372 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002373 index++;
2374 }
2375
Guy Cohen39e88502008-04-23 17:14:57 -07002376 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band, &tbl_type,
Zhu Yib481de92007-09-25 17:54:57 -07002377 &rate_idx);
2378
Ben Cahill77626352007-11-29 11:09:44 +08002379 /* Indicate to uCode which entries might be MIMO.
2380 * If initial rate was MIMO, this will finally end up
2381 * as (IWL_HT_NUMBER_TRY * 2), after 2nd pass, otherwise 0. */
Zhu Yib481de92007-09-25 17:54:57 -07002382 if (is_mimo(tbl_type.lq_type))
2383 lq_cmd->general_params.mimo_delimiter = index;
2384
Ben Cahill77626352007-11-29 11:09:44 +08002385 /* Get next rate */
Guy Cohen39e88502008-04-23 17:14:57 -07002386 new_rate = rs_get_lower_rate(lq_sta, &tbl_type, rate_idx,
2387 use_ht_possible);
Zhu Yib481de92007-09-25 17:54:57 -07002388
Ben Cahill77626352007-11-29 11:09:44 +08002389 /* How many times should we repeat the next rate? */
Zhu Yib481de92007-09-25 17:54:57 -07002390 if (is_legacy(tbl_type.lq_type)) {
2391 if (ant_toggle_count < NUM_TRY_BEFORE_ANTENNA_TOGGLE)
2392 ant_toggle_count++;
Guy Cohenfde0db32008-04-21 15:42:01 -07002393 else if (priv && rs_is_other_ant_connected(
2394 priv->hw_params.valid_tx_ant,
2395 tbl_type.ant_type)) {
Zhu Yib481de92007-09-25 17:54:57 -07002396 rs_toggle_antenna(&new_rate, &tbl_type);
2397 ant_toggle_count = 1;
2398 }
Zhu Yi1b696de2007-09-27 11:27:41 +08002399 repeat_rate = IWL_NUMBER_TRY;
Zhu Yib481de92007-09-25 17:54:57 -07002400 } else
Zhu Yi1b696de2007-09-27 11:27:41 +08002401 repeat_rate = IWL_HT_NUMBER_TRY;
Zhu Yib481de92007-09-25 17:54:57 -07002402
Ben Cahill77626352007-11-29 11:09:44 +08002403 /* Don't allow HT rates after next pass.
2404 * rs_get_lower_rate() will change type to LQ_A or LQ_G. */
Zhu Yib481de92007-09-25 17:54:57 -07002405 use_ht_possible = 0;
2406
Ben Cahill77626352007-11-29 11:09:44 +08002407 /* Override next rate if needed for debug purposes */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002408 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Ben Cahill77626352007-11-29 11:09:44 +08002409
2410 /* Fill next table entry */
Guy Cohen39e88502008-04-23 17:14:57 -07002411 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
Zhu Yib481de92007-09-25 17:54:57 -07002412
2413 index++;
Zhu Yi1b696de2007-09-27 11:27:41 +08002414 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002415 }
2416
Zhu Yib481de92007-09-25 17:54:57 -07002417 lq_cmd->general_params.dual_stream_ant_msk = 3;
2418 lq_cmd->agg_params.agg_dis_start_th = 3;
2419 lq_cmd->agg_params.agg_time_limit = cpu_to_le16(4000);
Zhu Yib481de92007-09-25 17:54:57 -07002420}
2421
2422static void *rs_alloc(struct ieee80211_local *local)
2423{
2424 return local->hw.priv;
2425}
2426/* rate scale requires free function to be implemented */
2427static void rs_free(void *priv_rate)
2428{
2429 return;
2430}
2431
2432static void rs_clear(void *priv_rate)
2433{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002434 struct iwl_priv *priv = (struct iwl_priv *) priv_rate;
Zhu Yib481de92007-09-25 17:54:57 -07002435
2436 IWL_DEBUG_RATE("enter\n");
2437
2438 priv->lq_mngr.lq_ready = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002439
2440 IWL_DEBUG_RATE("leave\n");
2441}
2442
2443static void rs_free_sta(void *priv, void *priv_sta)
2444{
Tomas Winklerc33104f2008-01-14 17:46:21 -08002445 struct iwl4965_lq_sta *lq_sta = priv_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002446
2447 IWL_DEBUG_RATE("enter\n");
Tomas Winklerc33104f2008-01-14 17:46:21 -08002448 kfree(lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002449 IWL_DEBUG_RATE("leave\n");
2450}
2451
2452
Zhu Yi93dc6462007-09-27 11:27:37 +08002453#ifdef CONFIG_MAC80211_DEBUGFS
Zhu Yi5ae212c2007-09-27 11:27:38 +08002454static int open_file_generic(struct inode *inode, struct file *file)
2455{
2456 file->private_data = inode->i_private;
2457 return 0;
2458}
Tomas Winklerc33104f2008-01-14 17:46:21 -08002459static void rs_dbgfs_set_mcs(struct iwl4965_lq_sta *lq_sta,
Guy Cohen39e88502008-04-23 17:14:57 -07002460 u32 *rate_n_flags, int index)
Zhu Yi98d7e092007-09-27 11:27:42 +08002461{
Guy Cohen39e88502008-04-23 17:14:57 -07002462 if (lq_sta->dbg_fixed_rate) {
2463 if (index < 12) {
2464 *rate_n_flags = lq_sta->dbg_fixed_rate;
2465 } else {
2466 if (lq_sta->band == IEEE80211_BAND_5GHZ)
2467 *rate_n_flags = 0x800D;
2468 else
2469 *rate_n_flags = 0x820A;
2470 }
Zhu Yi98d7e092007-09-27 11:27:42 +08002471 IWL_DEBUG_RATE("Fixed rate ON\n");
Guy Cohen39e88502008-04-23 17:14:57 -07002472 } else {
2473 IWL_DEBUG_RATE("Fixed rate OFF\n");
Zhu Yi98d7e092007-09-27 11:27:42 +08002474 }
Zhu Yi98d7e092007-09-27 11:27:42 +08002475}
2476
2477static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
2478 const char __user *user_buf, size_t count, loff_t *ppos)
2479{
Tomas Winklerc33104f2008-01-14 17:46:21 -08002480 struct iwl4965_lq_sta *lq_sta = file->private_data;
Zhu Yi98d7e092007-09-27 11:27:42 +08002481 char buf[64];
2482 int buf_size;
2483 u32 parsed_rate;
2484
2485 memset(buf, 0, sizeof(buf));
2486 buf_size = min(count, sizeof(buf) - 1);
2487 if (copy_from_user(buf, user_buf, buf_size))
2488 return -EFAULT;
2489
2490 if (sscanf(buf, "%x", &parsed_rate) == 1)
Guy Cohen39e88502008-04-23 17:14:57 -07002491 lq_sta->dbg_fixed_rate = parsed_rate;
Zhu Yi98d7e092007-09-27 11:27:42 +08002492 else
Guy Cohen39e88502008-04-23 17:14:57 -07002493 lq_sta->dbg_fixed_rate = 0;
Zhu Yi98d7e092007-09-27 11:27:42 +08002494
Guy Cohen39e88502008-04-23 17:14:57 -07002495 lq_sta->active_legacy_rate = 0x0FFF; /* 1 - 54 MBits, includes CCK */
2496 lq_sta->active_siso_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2497 lq_sta->active_mimo2_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2498 lq_sta->active_mimo3_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
Zhu Yi98d7e092007-09-27 11:27:42 +08002499
2500 IWL_DEBUG_RATE("sta_id %d rate 0x%X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07002501 lq_sta->lq.sta_id, lq_sta->dbg_fixed_rate);
Zhu Yi98d7e092007-09-27 11:27:42 +08002502
Guy Cohen39e88502008-04-23 17:14:57 -07002503 if (lq_sta->dbg_fixed_rate) {
2504 rs_fill_link_cmd(NULL, lq_sta, lq_sta->dbg_fixed_rate,
2505 &lq_sta->lq);
Tomas Winkler66c73db2008-04-15 16:01:40 -07002506 iwl_send_lq_cmd(lq_sta->drv, &lq_sta->lq, CMD_ASYNC);
Zhu Yi98d7e092007-09-27 11:27:42 +08002507 }
2508
2509 return count;
2510}
Zhu Yi0209dc12007-09-27 11:27:43 +08002511
Zhu Yi5ae212c2007-09-27 11:27:38 +08002512static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
2513 char __user *user_buf, size_t count, loff_t *ppos)
2514{
2515 char buff[1024];
2516 int desc = 0;
2517 int i = 0;
2518
Tomas Winklerc33104f2008-01-14 17:46:21 -08002519 struct iwl4965_lq_sta *lq_sta = file->private_data;
Zhu Yi5ae212c2007-09-27 11:27:38 +08002520
Tomas Winklerc33104f2008-01-14 17:46:21 -08002521 desc += sprintf(buff+desc, "sta_id %d\n", lq_sta->lq.sta_id);
Zhu Yi98d7e092007-09-27 11:27:42 +08002522 desc += sprintf(buff+desc, "failed=%d success=%d rate=0%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002523 lq_sta->total_failed, lq_sta->total_success,
Guy Cohen39e88502008-04-23 17:14:57 -07002524 lq_sta->active_legacy_rate);
Zhu Yi98d7e092007-09-27 11:27:42 +08002525 desc += sprintf(buff+desc, "fixed rate 0x%X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07002526 lq_sta->dbg_fixed_rate);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002527 desc += sprintf(buff+desc, "general:"
2528 "flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002529 lq_sta->lq.general_params.flags,
2530 lq_sta->lq.general_params.mimo_delimiter,
2531 lq_sta->lq.general_params.single_stream_ant_msk,
2532 lq_sta->lq.general_params.dual_stream_ant_msk);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002533
2534 desc += sprintf(buff+desc, "agg:"
2535 "time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002536 le16_to_cpu(lq_sta->lq.agg_params.agg_time_limit),
2537 lq_sta->lq.agg_params.agg_dis_start_th,
2538 lq_sta->lq.agg_params.agg_frame_cnt_limit);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002539
2540 desc += sprintf(buff+desc,
2541 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002542 lq_sta->lq.general_params.start_rate_index[0],
2543 lq_sta->lq.general_params.start_rate_index[1],
2544 lq_sta->lq.general_params.start_rate_index[2],
2545 lq_sta->lq.general_params.start_rate_index[3]);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002546
2547
2548 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
2549 desc += sprintf(buff+desc, " rate[%d] 0x%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002550 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags));
Zhu Yi5ae212c2007-09-27 11:27:38 +08002551
2552 return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2553}
2554
2555static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
Zhu Yi98d7e092007-09-27 11:27:42 +08002556 .write = rs_sta_dbgfs_scale_table_write,
Zhu Yi5ae212c2007-09-27 11:27:38 +08002557 .read = rs_sta_dbgfs_scale_table_read,
2558 .open = open_file_generic,
2559};
Zhu Yi0209dc12007-09-27 11:27:43 +08002560static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
2561 char __user *user_buf, size_t count, loff_t *ppos)
2562{
2563 char buff[1024];
2564 int desc = 0;
2565 int i, j;
2566
Tomas Winklerc33104f2008-01-14 17:46:21 -08002567 struct iwl4965_lq_sta *lq_sta = file->private_data;
Zhu Yi0209dc12007-09-27 11:27:43 +08002568 for (i = 0; i < LQ_SIZE; i++) {
2569 desc += sprintf(buff+desc, "%s type=%d SGI=%d FAT=%d DUP=%d\n"
2570 "rate=0x%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002571 lq_sta->active_tbl == i?"*":"x",
2572 lq_sta->lq_info[i].lq_type,
2573 lq_sta->lq_info[i].is_SGI,
2574 lq_sta->lq_info[i].is_fat,
2575 lq_sta->lq_info[i].is_dup,
Guy Cohen39e88502008-04-23 17:14:57 -07002576 lq_sta->lq_info[i].current_rate);
Zhu Yi0209dc12007-09-27 11:27:43 +08002577 for (j = 0; j < IWL_RATE_COUNT; j++) {
2578 desc += sprintf(buff+desc,
Tomas Winklerc33104f2008-01-14 17:46:21 -08002579 "counter=%d success=%d %%=%d\n",
2580 lq_sta->lq_info[i].win[j].counter,
2581 lq_sta->lq_info[i].win[j].success_counter,
2582 lq_sta->lq_info[i].win[j].success_ratio);
Zhu Yi0209dc12007-09-27 11:27:43 +08002583 }
2584 }
2585 return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2586}
2587
2588static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
2589 .read = rs_sta_dbgfs_stats_table_read,
2590 .open = open_file_generic,
2591};
Zhu Yi5ae212c2007-09-27 11:27:38 +08002592
Zhu Yi93dc6462007-09-27 11:27:37 +08002593static void rs_add_debugfs(void *priv, void *priv_sta,
2594 struct dentry *dir)
2595{
Tomas Winklerc33104f2008-01-14 17:46:21 -08002596 struct iwl4965_lq_sta *lq_sta = priv_sta;
2597 lq_sta->rs_sta_dbgfs_scale_table_file =
Zhu Yi0209dc12007-09-27 11:27:43 +08002598 debugfs_create_file("rate_scale_table", 0600, dir,
Tomas Winklerc33104f2008-01-14 17:46:21 -08002599 lq_sta, &rs_sta_dbgfs_scale_table_ops);
2600 lq_sta->rs_sta_dbgfs_stats_table_file =
Zhu Yi0209dc12007-09-27 11:27:43 +08002601 debugfs_create_file("rate_stats_table", 0600, dir,
Tomas Winklerc33104f2008-01-14 17:46:21 -08002602 lq_sta, &rs_sta_dbgfs_stats_table_ops);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002603#ifdef CONFIG_IWL4965_HT
2604 lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file =
2605 debugfs_create_u8("tx_agg_tid_enable", 0600, dir,
2606 &lq_sta->tx_agg_tid_en);
2607#endif
2608
Zhu Yi93dc6462007-09-27 11:27:37 +08002609}
2610
2611static void rs_remove_debugfs(void *priv, void *priv_sta)
2612{
Tomas Winklerc33104f2008-01-14 17:46:21 -08002613 struct iwl4965_lq_sta *lq_sta = priv_sta;
2614 debugfs_remove(lq_sta->rs_sta_dbgfs_scale_table_file);
2615 debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002616#ifdef CONFIG_IWL4965_HT
2617 debugfs_remove(lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file);
2618#endif
Zhu Yi93dc6462007-09-27 11:27:37 +08002619}
2620#endif
2621
Zhu Yib481de92007-09-25 17:54:57 -07002622static struct rate_control_ops rs_ops = {
2623 .module = NULL,
2624 .name = RS_NAME,
2625 .tx_status = rs_tx_status,
2626 .get_rate = rs_get_rate,
2627 .rate_init = rs_rate_init,
2628 .clear = rs_clear,
2629 .alloc = rs_alloc,
2630 .free = rs_free,
2631 .alloc_sta = rs_alloc_sta,
2632 .free_sta = rs_free_sta,
Zhu Yi93dc6462007-09-27 11:27:37 +08002633#ifdef CONFIG_MAC80211_DEBUGFS
2634 .add_sta_debugfs = rs_add_debugfs,
2635 .remove_sta_debugfs = rs_remove_debugfs,
2636#endif
Zhu Yib481de92007-09-25 17:54:57 -07002637};
2638
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002639int iwl4965_fill_rs_info(struct ieee80211_hw *hw, char *buf, u8 sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07002640{
2641 struct ieee80211_local *local = hw_to_local(hw);
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002642 struct iwl_priv *priv = hw->priv;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002643 struct iwl4965_lq_sta *lq_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002644 struct sta_info *sta;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002645 int cnt = 0, i;
Zhu Yib481de92007-09-25 17:54:57 -07002646 u32 samples = 0, success = 0, good = 0;
2647 unsigned long now = jiffies;
2648 u32 max_time = 0;
2649 u8 lq_type, antenna;
2650
Johannes Bergd0709a62008-02-25 16:27:46 +01002651 rcu_read_lock();
2652
Zhu Yib481de92007-09-25 17:54:57 -07002653 sta = sta_info_get(local, priv->stations[sta_id].sta.sta.addr);
2654 if (!sta || !sta->rate_ctrl_priv) {
Johannes Bergd0709a62008-02-25 16:27:46 +01002655 if (sta)
Zhu Yib481de92007-09-25 17:54:57 -07002656 IWL_DEBUG_RATE("leave - no private rate data!\n");
Johannes Bergd0709a62008-02-25 16:27:46 +01002657 else
Zhu Yib481de92007-09-25 17:54:57 -07002658 IWL_DEBUG_RATE("leave - no station!\n");
Johannes Bergd0709a62008-02-25 16:27:46 +01002659 rcu_read_unlock();
Zhu Yib481de92007-09-25 17:54:57 -07002660 return sprintf(buf, "station %d not found\n", sta_id);
2661 }
2662
Tomas Winklerc33104f2008-01-14 17:46:21 -08002663 lq_sta = (void *)sta->rate_ctrl_priv;
Zhu Yib481de92007-09-25 17:54:57 -07002664
Tomas Winklerc33104f2008-01-14 17:46:21 -08002665 lq_type = lq_sta->lq_info[lq_sta->active_tbl].lq_type;
Guy Cohenfde0db32008-04-21 15:42:01 -07002666 antenna = lq_sta->lq_info[lq_sta->active_tbl].ant_type;
Zhu Yib481de92007-09-25 17:54:57 -07002667
2668 if (is_legacy(lq_type))
2669 i = IWL_RATE_54M_INDEX;
2670 else
2671 i = IWL_RATE_60M_INDEX;
2672 while (1) {
2673 u64 mask;
2674 int j;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002675 int active = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002676
Tomas Winklerc33104f2008-01-14 17:46:21 -08002677 cnt +=
2678 sprintf(&buf[cnt], " %2dMbs: ", iwl4965_rates[i].ieee / 2);
Zhu Yib481de92007-09-25 17:54:57 -07002679
2680 mask = (1ULL << (IWL_RATE_MAX_WINDOW - 1));
2681 for (j = 0; j < IWL_RATE_MAX_WINDOW; j++, mask >>= 1)
Tomas Winklerc33104f2008-01-14 17:46:21 -08002682 buf[cnt++] =
2683 (lq_sta->lq_info[active].win[i].data & mask)
Zhu Yib481de92007-09-25 17:54:57 -07002684 ? '1' : '0';
2685
Tomas Winklerc33104f2008-01-14 17:46:21 -08002686 samples += lq_sta->lq_info[active].win[i].counter;
2687 good += lq_sta->lq_info[active].win[i].success_counter;
2688 success += lq_sta->lq_info[active].win[i].success_counter *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002689 iwl4965_rates[i].ieee;
Zhu Yib481de92007-09-25 17:54:57 -07002690
Tomas Winklerc33104f2008-01-14 17:46:21 -08002691 if (lq_sta->lq_info[active].win[i].stamp) {
Zhu Yib481de92007-09-25 17:54:57 -07002692 int delta =
2693 jiffies_to_msecs(now -
Tomas Winklerc33104f2008-01-14 17:46:21 -08002694 lq_sta->lq_info[active].win[i].stamp);
Zhu Yib481de92007-09-25 17:54:57 -07002695
2696 if (delta > max_time)
2697 max_time = delta;
2698
Tomas Winklerc33104f2008-01-14 17:46:21 -08002699 cnt += sprintf(&buf[cnt], "%5dms\n", delta);
Zhu Yib481de92007-09-25 17:54:57 -07002700 } else
Tomas Winklerc33104f2008-01-14 17:46:21 -08002701 buf[cnt++] = '\n';
Zhu Yib481de92007-09-25 17:54:57 -07002702
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002703 j = iwl4965_get_prev_ieee_rate(i);
Zhu Yib481de92007-09-25 17:54:57 -07002704 if (j == i)
2705 break;
2706 i = j;
2707 }
2708
2709 /* Display the average rate of all samples taken.
2710 *
Ben Cahill77626352007-11-29 11:09:44 +08002711 * NOTE: We multiply # of samples by 2 since the IEEE measurement
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002712 * added from iwl4965_rates is actually 2X the rate */
Zhu Yib481de92007-09-25 17:54:57 -07002713 if (samples)
Tomas Winklerc33104f2008-01-14 17:46:21 -08002714 cnt += sprintf(&buf[cnt],
Zhu Yib481de92007-09-25 17:54:57 -07002715 "\nAverage rate is %3d.%02dMbs over last %4dms\n"
2716 "%3d%% success (%d good packets over %d tries)\n",
2717 success / (2 * samples), (success * 5 / samples) % 10,
2718 max_time, good * 100 / samples, good, samples);
2719 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08002720 cnt += sprintf(&buf[cnt], "\nAverage rate: 0Mbs\n");
2721
2722 cnt += sprintf(&buf[cnt], "\nrate scale type %d antenna %d "
Zhu Yib481de92007-09-25 17:54:57 -07002723 "active_search %d rate index %d\n", lq_type, antenna,
Johannes Berg8318d782008-01-24 19:38:38 +01002724 lq_sta->search_better_tbl, sta->last_txrate_idx);
Zhu Yib481de92007-09-25 17:54:57 -07002725
Johannes Bergd0709a62008-02-25 16:27:46 +01002726 rcu_read_unlock();
Tomas Winklerc33104f2008-01-14 17:46:21 -08002727 return cnt;
Zhu Yib481de92007-09-25 17:54:57 -07002728}
2729
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002730void iwl4965_rate_scale_init(struct ieee80211_hw *hw, s32 sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07002731{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002732 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07002733
2734 priv->lq_mngr.lq_ready = 1;
2735}
2736
Reinette Chatre897e1cf2008-03-28 16:21:09 -07002737int iwl4965_rate_control_register(void)
Zhu Yib481de92007-09-25 17:54:57 -07002738{
Reinette Chatre897e1cf2008-03-28 16:21:09 -07002739 return ieee80211_rate_control_register(&rs_ops);
Zhu Yib481de92007-09-25 17:54:57 -07002740}
2741
Reinette Chatre897e1cf2008-03-28 16:21:09 -07002742void iwl4965_rate_control_unregister(void)
Zhu Yib481de92007-09-25 17:54:57 -07002743{
2744 ieee80211_rate_control_unregister(&rs_ops);
2745}
2746