blob: 7c55aa9a2ba90c0cfa32045ce4f6d43b19a12bca [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 */
Tomas Winklerc33104f2008-01-14 17:46:21 -0800607static void rs_get_supported_rates(struct iwl4965_lq_sta *lq_sta,
Zhu Yib481de92007-09-25 17:54:57 -0700608 struct ieee80211_hdr *hdr,
Guy Cohenfde0db32008-04-21 15:42:01 -0700609 enum iwl_table_type rate_type,
Zhu Yib481de92007-09-25 17:54:57 -0700610 u16 *data_rate)
611{
612 if (is_legacy(rate_type))
Guy Cohen39e88502008-04-23 17:14:57 -0700613 *data_rate = lq_sta->active_legacy_rate;
Zhu Yib481de92007-09-25 17:54:57 -0700614 else {
615 if (is_siso(rate_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -0800616 *data_rate = lq_sta->active_siso_rate;
Guy Cohenfde0db32008-04-21 15:42:01 -0700617 else if (is_mimo2(rate_type))
618 *data_rate = lq_sta->active_mimo2_rate;
Zhu Yib481de92007-09-25 17:54:57 -0700619 else
Guy Cohenfde0db32008-04-21 15:42:01 -0700620 *data_rate = lq_sta->active_mimo3_rate;
Zhu Yib481de92007-09-25 17:54:57 -0700621 }
622
623 if (hdr && is_multicast_ether_addr(hdr->addr1) &&
Tomas Winklerc33104f2008-01-14 17:46:21 -0800624 lq_sta->active_rate_basic) {
625 *data_rate = lq_sta->active_rate_basic;
626 }
Zhu Yib481de92007-09-25 17:54:57 -0700627}
628
629static u16 rs_get_adjacent_rate(u8 index, u16 rate_mask, int rate_type)
630{
631 u8 high = IWL_RATE_INVALID;
632 u8 low = IWL_RATE_INVALID;
633
Ian Schram01ebd062007-10-25 17:15:22 +0800634 /* 802.11A or ht walks to the next literal adjacent rate in
Zhu Yib481de92007-09-25 17:54:57 -0700635 * the rate table */
636 if (is_a_band(rate_type) || !is_legacy(rate_type)) {
637 int i;
638 u32 mask;
639
640 /* Find the previous rate that is in the rate mask */
641 i = index - 1;
642 for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
643 if (rate_mask & mask) {
644 low = i;
645 break;
646 }
647 }
648
649 /* Find the next rate that is in the rate mask */
650 i = index + 1;
651 for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
652 if (rate_mask & mask) {
653 high = i;
654 break;
655 }
656 }
657
658 return (high << 8) | low;
659 }
660
661 low = index;
662 while (low != IWL_RATE_INVALID) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800663 low = iwl4965_rates[low].prev_rs;
Zhu Yib481de92007-09-25 17:54:57 -0700664 if (low == IWL_RATE_INVALID)
665 break;
666 if (rate_mask & (1 << low))
667 break;
668 IWL_DEBUG_RATE("Skipping masked lower rate: %d\n", low);
669 }
670
671 high = index;
672 while (high != IWL_RATE_INVALID) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800673 high = iwl4965_rates[high].next_rs;
Zhu Yib481de92007-09-25 17:54:57 -0700674 if (high == IWL_RATE_INVALID)
675 break;
676 if (rate_mask & (1 << high))
677 break;
678 IWL_DEBUG_RATE("Skipping masked higher rate: %d\n", high);
679 }
680
681 return (high << 8) | low;
682}
683
Guy Cohen39e88502008-04-23 17:14:57 -0700684static u32 rs_get_lower_rate(struct iwl4965_lq_sta *lq_sta,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800685 struct iwl4965_scale_tbl_info *tbl, u8 scale_index,
Guy Cohen39e88502008-04-23 17:14:57 -0700686 u8 ht_possible)
Zhu Yib481de92007-09-25 17:54:57 -0700687{
Zhu Yib481de92007-09-25 17:54:57 -0700688 s32 low;
689 u16 rate_mask;
690 u16 high_low;
691 u8 switch_to_legacy = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -0800692 u8 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -0700693
694 /* check if we need to switch from HT to legacy rates.
695 * assumption is that mandatory rates (1Mbps or 6Mbps)
696 * are always supported (spec demand) */
697 if (!is_legacy(tbl->lq_type) && (!ht_possible || !scale_index)) {
698 switch_to_legacy = 1;
699 scale_index = rs_ht_to_legacy[scale_index];
Johannes Berg8318d782008-01-24 19:38:38 +0100700 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700701 tbl->lq_type = LQ_A;
702 else
703 tbl->lq_type = LQ_G;
704
Guy Cohenfde0db32008-04-21 15:42:01 -0700705 if (num_of_ant(tbl->ant_type > 1))
706 tbl->ant_type = ANT_A;/*FIXME:RS*/
Zhu Yib481de92007-09-25 17:54:57 -0700707
708 tbl->is_fat = 0;
709 tbl->is_SGI = 0;
710 }
711
Tomas Winklerc33104f2008-01-14 17:46:21 -0800712 rs_get_supported_rates(lq_sta, NULL, tbl->lq_type, &rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -0700713
Ben Cahill77626352007-11-29 11:09:44 +0800714 /* Mask with station rate restriction */
Zhu Yib481de92007-09-25 17:54:57 -0700715 if (is_legacy(tbl->lq_type)) {
Ben Cahill77626352007-11-29 11:09:44 +0800716 /* supp_rates has no CCK bits in A mode */
Johannes Berg8318d782008-01-24 19:38:38 +0100717 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700718 rate_mask = (u16)(rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -0800719 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
Zhu Yib481de92007-09-25 17:54:57 -0700720 else
Tomas Winklerc33104f2008-01-14 17:46:21 -0800721 rate_mask = (u16)(rate_mask & lq_sta->supp_rates);
Zhu Yib481de92007-09-25 17:54:57 -0700722 }
723
Ben Cahill77626352007-11-29 11:09:44 +0800724 /* If we switched from HT to legacy, check current rate */
Tomas Winklerdc2453ae2007-10-25 17:15:25 +0800725 if (switch_to_legacy && (rate_mask & (1 << scale_index))) {
Guy Cohen39e88502008-04-23 17:14:57 -0700726 low = scale_index;
727 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700728 }
729
730 high_low = rs_get_adjacent_rate(scale_index, rate_mask, tbl->lq_type);
731 low = high_low & 0xff;
732
Guy Cohen39e88502008-04-23 17:14:57 -0700733 if (low == IWL_RATE_INVALID)
734 low = scale_index;
735
736out:
737 return rate_n_flags_from_tbl(tbl, low, is_green);
Zhu Yib481de92007-09-25 17:54:57 -0700738}
739
Ben Cahill77626352007-11-29 11:09:44 +0800740/*
741 * mac80211 sends us Tx status
742 */
Tomas Winklerc33104f2008-01-14 17:46:21 -0800743static void rs_tx_status(void *priv_rate, struct net_device *dev,
Zhu Yib481de92007-09-25 17:54:57 -0700744 struct sk_buff *skb,
745 struct ieee80211_tx_status *tx_resp)
746{
747 int status;
748 u8 retries;
749 int rs_index, index = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -0800750 struct iwl4965_lq_sta *lq_sta;
Tomas Winkler66c73db2008-04-15 16:01:40 -0700751 struct iwl_link_quality_cmd *table;
Zhu Yib481de92007-09-25 17:54:57 -0700752 struct sta_info *sta;
753 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700754 struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
Zhu Yib481de92007-09-25 17:54:57 -0700755 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800756 struct ieee80211_hw *hw = local_to_hw(local);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800757 struct iwl4965_rate_scale_data *window = NULL;
758 struct iwl4965_rate_scale_data *search_win = NULL;
Guy Cohen39e88502008-04-23 17:14:57 -0700759 u32 tx_rate;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800760 struct iwl4965_scale_tbl_info tbl_type;
761 struct iwl4965_scale_tbl_info *curr_tbl, *search_tbl;
Zhu Yib481de92007-09-25 17:54:57 -0700762 u8 active_index = 0;
763 u16 fc = le16_to_cpu(hdr->frame_control);
764 s32 tpt = 0;
765
Zhu Yi58826352007-09-27 11:27:39 +0800766 IWL_DEBUG_RATE_LIMIT("get frame ack response, update rate scale window\n");
Zhu Yib481de92007-09-25 17:54:57 -0700767
768 if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1))
769 return;
770
Ron Rindjunsky99556432008-01-28 14:07:25 +0200771 /* This packet was aggregated but doesn't carry rate scale info */
772 if ((tx_resp->control.flags & IEEE80211_TXCTL_AMPDU) &&
773 !(tx_resp->flags & IEEE80211_TX_STATUS_AMPDU))
774 return;
775
Zhu Yib481de92007-09-25 17:54:57 -0700776 retries = tx_resp->retry_count;
777
778 if (retries > 15)
779 retries = 15;
780
Johannes Bergd0709a62008-02-25 16:27:46 +0100781 rcu_read_lock();
Zhu Yib481de92007-09-25 17:54:57 -0700782
783 sta = sta_info_get(local, hdr->addr1);
784
Tomas Winklerf4d60822008-03-05 11:31:00 -0800785 if (!sta || !sta->rate_ctrl_priv)
786 goto out;
787
Zhu Yib481de92007-09-25 17:54:57 -0700788
Tomas Winklerc33104f2008-01-14 17:46:21 -0800789 lq_sta = (struct iwl4965_lq_sta *)sta->rate_ctrl_priv;
Zhu Yib481de92007-09-25 17:54:57 -0700790
791 if (!priv->lq_mngr.lq_ready)
Tomas Winklerf4d60822008-03-05 11:31:00 -0800792 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700793
Tomas Winklerc33104f2008-01-14 17:46:21 -0800794 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
795 !lq_sta->ibss_sta_added)
Tomas Winklerf4d60822008-03-05 11:31:00 -0800796 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700797
Tomas Winklerc33104f2008-01-14 17:46:21 -0800798 table = &lq_sta->lq;
799 active_index = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -0700800
Tomas Winklerc33104f2008-01-14 17:46:21 -0800801 lq_sta->antenna = lq_sta->valid_antenna;
Ben Cahill77626352007-11-29 11:09:44 +0800802
Tomas Winklerc33104f2008-01-14 17:46:21 -0800803 curr_tbl = &(lq_sta->lq_info[active_index]);
804 search_tbl = &(lq_sta->lq_info[(1 - active_index)]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800805 window = (struct iwl4965_rate_scale_data *)
Zhu Yib481de92007-09-25 17:54:57 -0700806 &(curr_tbl->win[0]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800807 search_win = (struct iwl4965_rate_scale_data *)
Zhu Yib481de92007-09-25 17:54:57 -0700808 &(search_tbl->win[0]);
809
Ben Cahill77626352007-11-29 11:09:44 +0800810 /*
811 * Ignore this Tx frame response if its initial rate doesn't match
812 * that of latest Link Quality command. There may be stragglers
813 * from a previous Link Quality command, but we're no longer interested
814 * in those; they're either from the "active" mode while we're trying
815 * to check "search" mode, or a prior "search" mode after we've moved
816 * to a new "search" mode (which might become the new "active" mode).
817 */
Guy Cohen39e88502008-04-23 17:14:57 -0700818 tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
819 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type, &rs_index);
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800820 if (priv->band == IEEE80211_BAND_5GHZ)
821 rs_index -= IWL_FIRST_OFDM_RATE;
822
823 if ((tx_resp->control.tx_rate == NULL) ||
824 (tbl_type.is_SGI ^
825 !!(tx_resp->control.flags & IEEE80211_TXCTL_SHORT_GI)) ||
826 (tbl_type.is_fat ^
827 !!(tx_resp->control.flags & IEEE80211_TXCTL_40_MHZ_WIDTH)) ||
828 (tbl_type.is_dup ^
829 !!(tx_resp->control.flags & IEEE80211_TXCTL_DUP_DATA)) ||
Guy Cohenfde0db32008-04-21 15:42:01 -0700830 (tbl_type.ant_type ^ tx_resp->control.antenna_sel_tx) ||
Guy Cohen39e88502008-04-23 17:14:57 -0700831 (!!(tx_rate & RATE_MCS_HT_MSK) ^
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800832 !!(tx_resp->control.flags & IEEE80211_TXCTL_OFDM_HT)) ||
Guy Cohen39e88502008-04-23 17:14:57 -0700833 (!!(tx_rate & RATE_MCS_GF_MSK) ^
Ron Rindjunsky4c424e42008-03-04 18:09:27 -0800834 !!(tx_resp->control.flags & IEEE80211_TXCTL_GREEN_FIELD)) ||
835 (hw->wiphy->bands[priv->band]->bitrates[rs_index].bitrate !=
836 tx_resp->control.tx_rate->bitrate)) {
Guy Cohen39e88502008-04-23 17:14:57 -0700837 IWL_DEBUG_RATE("initial rate does not match 0x%x\n", tx_rate);
Tomas Winklerf4d60822008-03-05 11:31:00 -0800838 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700839 }
840
Ben Cahill77626352007-11-29 11:09:44 +0800841 /* Update frame history window with "failure" for each Tx retry. */
Zhu Yib481de92007-09-25 17:54:57 -0700842 while (retries) {
Ben Cahill77626352007-11-29 11:09:44 +0800843 /* Look up the rate and other info used for each tx attempt.
844 * Each tx attempt steps one entry deeper in the rate table. */
Guy Cohen39e88502008-04-23 17:14:57 -0700845 tx_rate = le32_to_cpu(table->rs_table[index].rate_n_flags);
846 rs_get_tbl_info_from_mcs(tx_rate, priv->band,
Zhu Yib481de92007-09-25 17:54:57 -0700847 &tbl_type, &rs_index);
848
Ben Cahill77626352007-11-29 11:09:44 +0800849 /* If type matches "search" table,
850 * add failure to "search" history */
Zhu Yib481de92007-09-25 17:54:57 -0700851 if ((tbl_type.lq_type == search_tbl->lq_type) &&
Guy Cohenfde0db32008-04-21 15:42:01 -0700852 (tbl_type.ant_type == search_tbl->ant_type) &&
Zhu Yib481de92007-09-25 17:54:57 -0700853 (tbl_type.is_SGI == search_tbl->is_SGI)) {
854 if (search_tbl->expected_tpt)
855 tpt = search_tbl->expected_tpt[rs_index];
856 else
857 tpt = 0;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200858 rs_collect_tx_data(search_win, rs_index, tpt, 1, 0);
Ben Cahill77626352007-11-29 11:09:44 +0800859
860 /* Else if type matches "current/active" table,
861 * add failure to "current/active" history */
Zhu Yib481de92007-09-25 17:54:57 -0700862 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
Guy Cohenfde0db32008-04-21 15:42:01 -0700863 (tbl_type.ant_type == curr_tbl->ant_type) &&
Zhu Yib481de92007-09-25 17:54:57 -0700864 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
865 if (curr_tbl->expected_tpt)
866 tpt = curr_tbl->expected_tpt[rs_index];
867 else
868 tpt = 0;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200869 rs_collect_tx_data(window, rs_index, tpt, 1, 0);
Zhu Yib481de92007-09-25 17:54:57 -0700870 }
Ben Cahill77626352007-11-29 11:09:44 +0800871
872 /* If not searching for a new mode, increment failed counter
873 * ... this helps determine when to start searching again */
Tomas Winklerc33104f2008-01-14 17:46:21 -0800874 if (lq_sta->stay_in_tbl)
875 lq_sta->total_failed++;
Zhu Yib481de92007-09-25 17:54:57 -0700876 --retries;
877 index++;
878
879 }
880
Ben Cahill77626352007-11-29 11:09:44 +0800881 /*
882 * Find (by rate) the history window to update with final Tx attempt;
883 * if Tx was successful first try, use original rate,
884 * else look up the rate that was, finally, successful.
885 */
Guy Cohen39e88502008-04-23 17:14:57 -0700886 tx_rate = le32_to_cpu(table->rs_table[index].rate_n_flags);
887 rs_get_tbl_info_from_mcs(tx_rate, priv->band, &tbl_type, &rs_index);
Zhu Yib481de92007-09-25 17:54:57 -0700888
Ben Cahill77626352007-11-29 11:09:44 +0800889 /* Update frame history window with "success" if Tx got ACKed ... */
Zhu Yib481de92007-09-25 17:54:57 -0700890 if (tx_resp->flags & IEEE80211_TX_STATUS_ACK)
891 status = 1;
892 else
893 status = 0;
894
Ben Cahill77626352007-11-29 11:09:44 +0800895 /* If type matches "search" table,
896 * add final tx status to "search" history */
Zhu Yib481de92007-09-25 17:54:57 -0700897 if ((tbl_type.lq_type == search_tbl->lq_type) &&
Guy Cohenfde0db32008-04-21 15:42:01 -0700898 (tbl_type.ant_type == search_tbl->ant_type) &&
Zhu Yib481de92007-09-25 17:54:57 -0700899 (tbl_type.is_SGI == search_tbl->is_SGI)) {
900 if (search_tbl->expected_tpt)
901 tpt = search_tbl->expected_tpt[rs_index];
902 else
903 tpt = 0;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200904 if (tx_resp->control.flags & IEEE80211_TXCTL_AMPDU)
905 rs_collect_tx_data(search_win, rs_index, tpt,
906 tx_resp->ampdu_ack_len,
907 tx_resp->ampdu_ack_map);
908 else
909 rs_collect_tx_data(search_win, rs_index, tpt,
910 1, status);
Ben Cahill77626352007-11-29 11:09:44 +0800911 /* Else if type matches "current/active" table,
912 * add final tx status to "current/active" history */
Zhu Yib481de92007-09-25 17:54:57 -0700913 } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
Guy Cohenfde0db32008-04-21 15:42:01 -0700914 (tbl_type.ant_type == curr_tbl->ant_type) &&
Zhu Yib481de92007-09-25 17:54:57 -0700915 (tbl_type.is_SGI == curr_tbl->is_SGI)) {
916 if (curr_tbl->expected_tpt)
917 tpt = curr_tbl->expected_tpt[rs_index];
918 else
919 tpt = 0;
Ron Rindjunsky99556432008-01-28 14:07:25 +0200920 if (tx_resp->control.flags & IEEE80211_TXCTL_AMPDU)
921 rs_collect_tx_data(window, rs_index, tpt,
922 tx_resp->ampdu_ack_len,
923 tx_resp->ampdu_ack_map);
924 else
925 rs_collect_tx_data(window, rs_index, tpt,
926 1, status);
Zhu Yib481de92007-09-25 17:54:57 -0700927 }
928
Ben Cahill77626352007-11-29 11:09:44 +0800929 /* If not searching for new mode, increment success/failed counter
930 * ... these help determine when to start searching again */
Tomas Winklerc33104f2008-01-14 17:46:21 -0800931 if (lq_sta->stay_in_tbl) {
Ron Rindjunsky99556432008-01-28 14:07:25 +0200932 if (tx_resp->control.flags & IEEE80211_TXCTL_AMPDU) {
933 lq_sta->total_success += tx_resp->ampdu_ack_map;
934 lq_sta->total_failed +=
935 (tx_resp->ampdu_ack_len - tx_resp->ampdu_ack_map);
936 } else {
937 if (status)
938 lq_sta->total_success++;
939 else
940 lq_sta->total_failed++;
941 }
Zhu Yib481de92007-09-25 17:54:57 -0700942 }
943
Ben Cahill77626352007-11-29 11:09:44 +0800944 /* See if there's a better rate or modulation mode to try. */
Zhu Yib481de92007-09-25 17:54:57 -0700945 rs_rate_scale_perform(priv, dev, hdr, sta);
Tomas Winklerf4d60822008-03-05 11:31:00 -0800946out:
Johannes Bergd0709a62008-02-25 16:27:46 +0100947 rcu_read_unlock();
Zhu Yib481de92007-09-25 17:54:57 -0700948 return;
949}
950
Guy Cohenfde0db32008-04-21 15:42:01 -0700951static inline u8 rs_is_ant_connected(u8 valid_antenna, u8 ant_type)
Zhu Yib481de92007-09-25 17:54:57 -0700952{
Guy Cohenfde0db32008-04-21 15:42:01 -0700953 return ((ant_type & valid_antenna) == ant_type);
Zhu Yib481de92007-09-25 17:54:57 -0700954}
955
Guy Cohenfde0db32008-04-21 15:42:01 -0700956/*FIXME:RS: this function should be replaced*/
957static u8 rs_is_other_ant_connected(u8 valid_antenna, u8 ant_type)
Zhu Yib481de92007-09-25 17:54:57 -0700958{
Guy Cohenfde0db32008-04-21 15:42:01 -0700959 if (ant_type == ANT_B)
960 return rs_is_ant_connected(valid_antenna, ANT_A);
Zhu Yib481de92007-09-25 17:54:57 -0700961 else
Guy Cohenfde0db32008-04-21 15:42:01 -0700962 return rs_is_ant_connected(valid_antenna, ANT_B);
Zhu Yib481de92007-09-25 17:54:57 -0700963
964 return 0;
965}
966
Ben Cahill77626352007-11-29 11:09:44 +0800967/*
968 * Begin a period of staying with a selected modulation mode.
969 * Set "stay_in_tbl" flag to prevent any mode switches.
970 * Set frame tx success limits according to legacy vs. high-throughput,
971 * and reset overall (spanning all rates) tx success history statistics.
972 * These control how long we stay using same modulation mode before
973 * searching for a new mode.
974 */
Zhu Yib481de92007-09-25 17:54:57 -0700975static void rs_set_stay_in_table(u8 is_legacy,
Tomas Winklerc33104f2008-01-14 17:46:21 -0800976 struct iwl4965_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -0700977{
978 IWL_DEBUG_HT("we are staying in the same table\n");
Tomas Winklerc33104f2008-01-14 17:46:21 -0800979 lq_sta->stay_in_tbl = 1; /* only place this gets set */
Zhu Yib481de92007-09-25 17:54:57 -0700980 if (is_legacy) {
Tomas Winklerc33104f2008-01-14 17:46:21 -0800981 lq_sta->table_count_limit = IWL_LEGACY_TABLE_COUNT;
982 lq_sta->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
983 lq_sta->max_success_limit = IWL_LEGACY_SUCCESS_LIMIT;
Zhu Yib481de92007-09-25 17:54:57 -0700984 } else {
Tomas Winklerc33104f2008-01-14 17:46:21 -0800985 lq_sta->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
986 lq_sta->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
987 lq_sta->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
Zhu Yib481de92007-09-25 17:54:57 -0700988 }
Tomas Winklerc33104f2008-01-14 17:46:21 -0800989 lq_sta->table_count = 0;
990 lq_sta->total_failed = 0;
991 lq_sta->total_success = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700992}
993
Ben Cahill77626352007-11-29 11:09:44 +0800994/*
995 * Find correct throughput table for given mode of modulation
996 */
Tomas Winklerc33104f2008-01-14 17:46:21 -0800997static void rs_get_expected_tpt_table(struct iwl4965_lq_sta *lq_sta,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800998 struct iwl4965_scale_tbl_info *tbl)
Zhu Yib481de92007-09-25 17:54:57 -0700999{
1000 if (is_legacy(tbl->lq_type)) {
1001 if (!is_a_band(tbl->lq_type))
1002 tbl->expected_tpt = expected_tpt_G;
1003 else
1004 tbl->expected_tpt = expected_tpt_A;
1005 } else if (is_siso(tbl->lq_type)) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001006 if (tbl->is_fat && !lq_sta->is_dup)
Zhu Yib481de92007-09-25 17:54:57 -07001007 if (tbl->is_SGI)
1008 tbl->expected_tpt = expected_tpt_siso40MHzSGI;
1009 else
1010 tbl->expected_tpt = expected_tpt_siso40MHz;
1011 else if (tbl->is_SGI)
1012 tbl->expected_tpt = expected_tpt_siso20MHzSGI;
1013 else
1014 tbl->expected_tpt = expected_tpt_siso20MHz;
1015
Guy Cohenfde0db32008-04-21 15:42:01 -07001016 } else if (is_mimo(tbl->lq_type)) { /* FIXME:need to separate mimo2/3 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001017 if (tbl->is_fat && !lq_sta->is_dup)
Zhu Yib481de92007-09-25 17:54:57 -07001018 if (tbl->is_SGI)
1019 tbl->expected_tpt = expected_tpt_mimo40MHzSGI;
1020 else
1021 tbl->expected_tpt = expected_tpt_mimo40MHz;
1022 else if (tbl->is_SGI)
1023 tbl->expected_tpt = expected_tpt_mimo20MHzSGI;
1024 else
1025 tbl->expected_tpt = expected_tpt_mimo20MHz;
1026 } else
1027 tbl->expected_tpt = expected_tpt_G;
1028}
1029
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001030#ifdef CONFIG_IWL4965_HT
Ben Cahill77626352007-11-29 11:09:44 +08001031/*
1032 * Find starting rate for new "search" high-throughput mode of modulation.
1033 * Goal is to find lowest expected rate (under perfect conditions) that is
1034 * above the current measured throughput of "active" mode, to give new mode
1035 * a fair chance to prove itself without too many challenges.
1036 *
1037 * This gets called when transitioning to more aggressive modulation
1038 * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
1039 * (i.e. MIMO to SISO). When moving to MIMO, bit rate will typically need
1040 * to decrease to match "active" throughput. When moving from MIMO to SISO,
1041 * bit rate will typically need to increase, but not if performance was bad.
1042 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001043static s32 rs_get_best_rate(struct iwl_priv *priv,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001044 struct iwl4965_lq_sta *lq_sta,
Ben Cahill77626352007-11-29 11:09:44 +08001045 struct iwl4965_scale_tbl_info *tbl, /* "search" */
Zhu Yib481de92007-09-25 17:54:57 -07001046 u16 rate_mask, s8 index, s8 rate)
1047{
Ben Cahill77626352007-11-29 11:09:44 +08001048 /* "active" values */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001049 struct iwl4965_scale_tbl_info *active_tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001050 &(lq_sta->lq_info[lq_sta->active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07001051 s32 active_sr = active_tbl->win[index].success_ratio;
Zhu Yib481de92007-09-25 17:54:57 -07001052 s32 active_tpt = active_tbl->expected_tpt[index];
Ben Cahill77626352007-11-29 11:09:44 +08001053
1054 /* expected "search" throughput */
1055 s32 *tpt_tbl = tbl->expected_tpt;
1056
1057 s32 new_rate, high, low, start_hi;
Zhu Yib481de92007-09-25 17:54:57 -07001058 u16 high_low;
1059
1060 new_rate = high = low = start_hi = IWL_RATE_INVALID;
1061
1062 for (; ;) {
1063 high_low = rs_get_adjacent_rate(rate, rate_mask, tbl->lq_type);
1064
1065 low = high_low & 0xff;
1066 high = (high_low >> 8) & 0xff;
1067
Ben Cahill77626352007-11-29 11:09:44 +08001068 /*
1069 * Lower the "search" bit rate, to give new "search" mode
1070 * approximately the same throughput as "active" if:
1071 *
1072 * 1) "Active" mode has been working modestly well (but not
1073 * great), and expected "search" throughput (under perfect
1074 * conditions) at candidate rate is above the actual
1075 * measured "active" throughput (but less than expected
1076 * "active" throughput under perfect conditions).
1077 * OR
1078 * 2) "Active" mode has been working perfectly or very well
1079 * and expected "search" throughput (under perfect
1080 * conditions) at candidate rate is above expected
1081 * "active" throughput (under perfect conditions).
1082 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001083 if ((((100 * tpt_tbl[rate]) > lq_sta->last_tpt) &&
Zhu Yib481de92007-09-25 17:54:57 -07001084 ((active_sr > IWL_RATE_DECREASE_TH) &&
1085 (active_sr <= IWL_RATE_HIGH_TH) &&
1086 (tpt_tbl[rate] <= active_tpt))) ||
1087 ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
1088 (tpt_tbl[rate] > active_tpt))) {
1089
Ben Cahill77626352007-11-29 11:09:44 +08001090 /* (2nd or later pass)
1091 * If we've already tried to raise the rate, and are
1092 * now trying to lower it, use the higher rate. */
Zhu Yib481de92007-09-25 17:54:57 -07001093 if (start_hi != IWL_RATE_INVALID) {
1094 new_rate = start_hi;
1095 break;
1096 }
Ben Cahill77626352007-11-29 11:09:44 +08001097
Zhu Yib481de92007-09-25 17:54:57 -07001098 new_rate = rate;
Ben Cahill77626352007-11-29 11:09:44 +08001099
1100 /* Loop again with lower rate */
Zhu Yib481de92007-09-25 17:54:57 -07001101 if (low != IWL_RATE_INVALID)
1102 rate = low;
Ben Cahill77626352007-11-29 11:09:44 +08001103
1104 /* Lower rate not available, use the original */
Zhu Yib481de92007-09-25 17:54:57 -07001105 else
1106 break;
Ben Cahill77626352007-11-29 11:09:44 +08001107
1108 /* Else try to raise the "search" rate to match "active" */
Zhu Yib481de92007-09-25 17:54:57 -07001109 } else {
Ben Cahill77626352007-11-29 11:09:44 +08001110 /* (2nd or later pass)
1111 * If we've already tried to lower the rate, and are
1112 * now trying to raise it, use the lower rate. */
Zhu Yib481de92007-09-25 17:54:57 -07001113 if (new_rate != IWL_RATE_INVALID)
1114 break;
Ben Cahill77626352007-11-29 11:09:44 +08001115
1116 /* Loop again with higher rate */
Zhu Yib481de92007-09-25 17:54:57 -07001117 else if (high != IWL_RATE_INVALID) {
1118 start_hi = high;
1119 rate = high;
Ben Cahill77626352007-11-29 11:09:44 +08001120
1121 /* Higher rate not available, use the original */
Zhu Yib481de92007-09-25 17:54:57 -07001122 } else {
1123 new_rate = rate;
1124 break;
1125 }
1126 }
1127 }
1128
1129 return new_rate;
1130}
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001131#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001132
Guy Cohenfde0db32008-04-21 15:42:01 -07001133/*FIXME:RS:this function should be replaced*/
Zhu Yib481de92007-09-25 17:54:57 -07001134static inline u8 rs_is_both_ant_supp(u8 valid_antenna)
1135{
Guy Cohenfde0db32008-04-21 15:42:01 -07001136 return (rs_is_ant_connected(valid_antenna, ANT_AB));
Zhu Yib481de92007-09-25 17:54:57 -07001137}
1138
Ben Cahill77626352007-11-29 11:09:44 +08001139/*
1140 * Set up search table for MIMO
1141 */
Guy Cohenfde0db32008-04-21 15:42:01 -07001142#ifdef CONFIG_IWL4965_HT
1143static int rs_switch_to_mimo2(struct iwl_priv *priv,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001144 struct iwl4965_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001145 struct ieee80211_conf *conf,
1146 struct sta_info *sta,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001147 struct iwl4965_scale_tbl_info *tbl, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001148{
Zhu Yib481de92007-09-25 17:54:57 -07001149 u16 rate_mask;
1150 s32 rate;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001151 s8 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07001152
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001153 if (!(conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) ||
1154 !sta->ht_info.ht_supported)
Zhu Yib481de92007-09-25 17:54:57 -07001155 return -1;
1156
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001157 if (priv->current_ht_config.tx_mimo_ps_mode == IWL_MIMO_PS_STATIC)
Zhu Yib481de92007-09-25 17:54:57 -07001158 return -1;
1159
Ben Cahill77626352007-11-29 11:09:44 +08001160 /* Need both Tx chains/antennas to support MIMO */
Guy Cohenfde0db32008-04-21 15:42:01 -07001161 if (!rs_is_both_ant_supp(priv->hw_params.valid_tx_ant))
Zhu Yib481de92007-09-25 17:54:57 -07001162 return -1;
1163
Guy Cohen39e88502008-04-23 17:14:57 -07001164 IWL_DEBUG_HT("LQ: try to switch to MIMO2\n");
1165
1166 tbl->lq_type = LQ_MIMO2;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001167 tbl->is_dup = lq_sta->is_dup;
Zhu Yib481de92007-09-25 17:54:57 -07001168 tbl->action = 0;
Guy Cohen39e88502008-04-23 17:14:57 -07001169 rate_mask = lq_sta->active_mimo2_rate;
1170
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001171 if (priv->current_ht_config.supported_chan_width
Guy Cohen39e88502008-04-23 17:14:57 -07001172 == IWL_CHANNEL_WIDTH_40MHZ)
Zhu Yib481de92007-09-25 17:54:57 -07001173 tbl->is_fat = 1;
1174 else
1175 tbl->is_fat = 0;
1176
Guy Cohen39e88502008-04-23 17:14:57 -07001177 /* FIXME: - don't toggle SGI here
Zhu Yib481de92007-09-25 17:54:57 -07001178 if (tbl->is_fat) {
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001179 if (priv->current_ht_config.sgf & HT_SHORT_GI_40MHZ_ONLY)
Zhu Yib481de92007-09-25 17:54:57 -07001180 tbl->is_SGI = 1;
1181 else
1182 tbl->is_SGI = 0;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001183 } else if (priv->current_ht_config.sgf & HT_SHORT_GI_20MHZ_ONLY)
Zhu Yib481de92007-09-25 17:54:57 -07001184 tbl->is_SGI = 1;
1185 else
1186 tbl->is_SGI = 0;
Guy Cohen39e88502008-04-23 17:14:57 -07001187 */
Zhu Yib481de92007-09-25 17:54:57 -07001188
Tomas Winklerc33104f2008-01-14 17:46:21 -08001189 rs_get_expected_tpt_table(lq_sta, tbl);
Zhu Yib481de92007-09-25 17:54:57 -07001190
Tomas Winklerc33104f2008-01-14 17:46:21 -08001191 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index, index);
Zhu Yib481de92007-09-25 17:54:57 -07001192
Guy Cohen39e88502008-04-23 17:14:57 -07001193 IWL_DEBUG_HT("LQ: MIMO2 best rate %d mask %X\n", rate, rate_mask);
1194
1195 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1196 IWL_DEBUG_HT("Can't switch with index %d rate mask %x\n",
1197 rate, rate_mask);
Zhu Yib481de92007-09-25 17:54:57 -07001198 return -1;
Guy Cohen39e88502008-04-23 17:14:57 -07001199 }
1200 tbl->current_rate = rate_n_flags_from_tbl(tbl, rate, is_green);
Zhu Yib481de92007-09-25 17:54:57 -07001201
1202 IWL_DEBUG_HT("LQ: Switch to new mcs %X index is green %X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001203 tbl->current_rate, is_green);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001204 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07001205}
Guy Cohenfde0db32008-04-21 15:42:01 -07001206#else
1207static int rs_switch_to_mimo2(struct iwl_priv *priv,
1208 struct iwl4965_lq_sta *lq_sta,
1209 struct ieee80211_conf *conf,
1210 struct sta_info *sta,
1211 struct iwl4965_scale_tbl_info *tbl, int index)
1212{
1213 return -1;
1214}
1215#endif /*CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001216
Ben Cahill77626352007-11-29 11:09:44 +08001217/*
1218 * Set up search table for SISO
1219 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001220static int rs_switch_to_siso(struct iwl_priv *priv,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001221 struct iwl4965_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001222 struct ieee80211_conf *conf,
1223 struct sta_info *sta,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001224 struct iwl4965_scale_tbl_info *tbl, int index)
Zhu Yib481de92007-09-25 17:54:57 -07001225{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001226#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07001227 u16 rate_mask;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001228 u8 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07001229 s32 rate;
1230
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001231 if (!(conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) ||
1232 !sta->ht_info.ht_supported)
Zhu Yib481de92007-09-25 17:54:57 -07001233 return -1;
1234
Guy Cohen39e88502008-04-23 17:14:57 -07001235 IWL_DEBUG_HT("LQ: try to switch to SISO\n");
1236
Tomas Winklerc33104f2008-01-14 17:46:21 -08001237 tbl->is_dup = lq_sta->is_dup;
Zhu Yib481de92007-09-25 17:54:57 -07001238 tbl->lq_type = LQ_SISO;
1239 tbl->action = 0;
Guy Cohen39e88502008-04-23 17:14:57 -07001240 rate_mask = lq_sta->active_siso_rate;
Zhu Yib481de92007-09-25 17:54:57 -07001241
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001242 if (priv->current_ht_config.supported_chan_width
1243 == IWL_CHANNEL_WIDTH_40MHZ)
Zhu Yib481de92007-09-25 17:54:57 -07001244 tbl->is_fat = 1;
1245 else
1246 tbl->is_fat = 0;
1247
Guy Cohen39e88502008-04-23 17:14:57 -07001248 /* FIXME: - don't toggle SGI here
Zhu Yib481de92007-09-25 17:54:57 -07001249 if (tbl->is_fat) {
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001250 if (priv->current_ht_config.sgf & HT_SHORT_GI_40MHZ_ONLY)
Zhu Yib481de92007-09-25 17:54:57 -07001251 tbl->is_SGI = 1;
1252 else
1253 tbl->is_SGI = 0;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001254 } else if (priv->current_ht_config.sgf & HT_SHORT_GI_20MHZ_ONLY)
Zhu Yib481de92007-09-25 17:54:57 -07001255 tbl->is_SGI = 1;
1256 else
1257 tbl->is_SGI = 0;
Guy Cohen39e88502008-04-23 17:14:57 -07001258 */
Zhu Yib481de92007-09-25 17:54:57 -07001259
1260 if (is_green)
Guy Cohen39e88502008-04-23 17:14:57 -07001261 tbl->is_SGI = 0; /*11n spec: no SGI in SISO+Greenfield*/
Zhu Yib481de92007-09-25 17:54:57 -07001262
Tomas Winklerc33104f2008-01-14 17:46:21 -08001263 rs_get_expected_tpt_table(lq_sta, tbl);
1264 rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index, index);
Zhu Yib481de92007-09-25 17:54:57 -07001265
1266 IWL_DEBUG_HT("LQ: get best rate %d mask %X\n", rate, rate_mask);
1267 if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
1268 IWL_DEBUG_HT("can not switch with index %d rate mask %x\n",
1269 rate, rate_mask);
1270 return -1;
1271 }
Guy Cohen39e88502008-04-23 17:14:57 -07001272 tbl->current_rate = rate_n_flags_from_tbl(tbl, rate, is_green);
Zhu Yib481de92007-09-25 17:54:57 -07001273 IWL_DEBUG_HT("LQ: Switch to new mcs %X index is green %X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001274 tbl->current_rate, is_green);
Mohamed Abbas403ab562007-11-06 22:06:25 -08001275 return 0;
1276#else
1277 return -1;
Zhu Yib481de92007-09-25 17:54:57 -07001278
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001279#endif /*CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001280}
1281
Ben Cahill77626352007-11-29 11:09:44 +08001282/*
1283 * Try to switch to new modulation mode from legacy
1284 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001285static int rs_move_legacy_other(struct iwl_priv *priv,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001286 struct iwl4965_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001287 struct ieee80211_conf *conf,
1288 struct sta_info *sta,
Zhu Yib481de92007-09-25 17:54:57 -07001289 int index)
1290{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001291 struct iwl4965_scale_tbl_info *tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001292 &(lq_sta->lq_info[lq_sta->active_tbl]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001293 struct iwl4965_scale_tbl_info *search_tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001294 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001295 struct iwl4965_rate_scale_data *window = &(tbl->win[index]);
1296 u32 sz = (sizeof(struct iwl4965_scale_tbl_info) -
1297 (sizeof(struct iwl4965_rate_scale_data) * IWL_RATE_COUNT));
Zhu Yib481de92007-09-25 17:54:57 -07001298 u8 start_action = tbl->action;
Guy Cohenfde0db32008-04-21 15:42:01 -07001299 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1300 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001301
1302 for (; ;) {
1303 switch (tbl->action) {
1304 case IWL_LEGACY_SWITCH_ANTENNA:
Guy Cohen39e88502008-04-23 17:14:57 -07001305 IWL_DEBUG_HT("LQ Legacy toggle Antenna\n");
Zhu Yib481de92007-09-25 17:54:57 -07001306
1307 search_tbl->lq_type = LQ_NONE;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001308 lq_sta->action_counter++;
Ben Cahill77626352007-11-29 11:09:44 +08001309
1310 /* Don't change antenna if success has been great */
Guy Cohen39e88502008-04-23 17:14:57 -07001311 /*FIXME:RS:not sure this is really needed*/
Zhu Yib481de92007-09-25 17:54:57 -07001312 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1313 break;
Ben Cahill77626352007-11-29 11:09:44 +08001314
1315 /* Don't change antenna if other one is not connected */
Guy Cohenfde0db32008-04-21 15:42:01 -07001316 if (!rs_is_other_ant_connected(valid_tx_ant,
1317 tbl->ant_type))
Zhu Yib481de92007-09-25 17:54:57 -07001318 break;
1319
Ben Cahill77626352007-11-29 11:09:44 +08001320 /* Set up search table to try other antenna */
Zhu Yib481de92007-09-25 17:54:57 -07001321 memcpy(search_tbl, tbl, sz);
1322
Guy Cohen39e88502008-04-23 17:14:57 -07001323 rs_toggle_antenna(&search_tbl->current_rate,
Zhu Yib481de92007-09-25 17:54:57 -07001324 search_tbl);
Tomas Winklerc33104f2008-01-14 17:46:21 -08001325 rs_get_expected_tpt_table(lq_sta, search_tbl);
1326 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001327 goto out;
1328
1329 case IWL_LEGACY_SWITCH_SISO:
1330 IWL_DEBUG_HT("LQ: Legacy switch to SISO\n");
Ben Cahill77626352007-11-29 11:09:44 +08001331
1332 /* Set up search table to try SISO */
Zhu Yib481de92007-09-25 17:54:57 -07001333 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001334 search_tbl->is_SGI = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001335 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001336 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001337 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001338 lq_sta->search_better_tbl = 1;
1339 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001340 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001341 }
Zhu Yib481de92007-09-25 17:54:57 -07001342
1343 break;
Guy Cohenfde0db32008-04-21 15:42:01 -07001344 case IWL_LEGACY_SWITCH_MIMO2:
Guy Cohen39e88502008-04-23 17:14:57 -07001345 IWL_DEBUG_HT("LQ: Legacy switch to MIMO2\n");
Ben Cahill77626352007-11-29 11:09:44 +08001346
1347 /* Set up search table to try MIMO */
Zhu Yib481de92007-09-25 17:54:57 -07001348 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001349 search_tbl->is_SGI = 0;
Guy Cohenfde0db32008-04-21 15:42:01 -07001350 search_tbl->ant_type = ANT_AB;/*FIXME:RS*/
1351 /*FIXME:RS:need to check ant validity*/
1352 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001353 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001354 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001355 lq_sta->search_better_tbl = 1;
1356 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001357 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001358 }
Zhu Yib481de92007-09-25 17:54:57 -07001359 break;
1360 }
1361 tbl->action++;
Guy Cohenfde0db32008-04-21 15:42:01 -07001362 if (tbl->action > IWL_LEGACY_SWITCH_MIMO2)
Zhu Yib481de92007-09-25 17:54:57 -07001363 tbl->action = IWL_LEGACY_SWITCH_ANTENNA;
1364
1365 if (tbl->action == start_action)
1366 break;
1367
1368 }
1369 return 0;
1370
1371 out:
1372 tbl->action++;
Guy Cohenfde0db32008-04-21 15:42:01 -07001373 if (tbl->action > IWL_LEGACY_SWITCH_MIMO2)
Zhu Yib481de92007-09-25 17:54:57 -07001374 tbl->action = IWL_LEGACY_SWITCH_ANTENNA;
1375 return 0;
1376
1377}
1378
Ben Cahill77626352007-11-29 11:09:44 +08001379/*
1380 * Try to switch to new modulation mode from SISO
1381 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001382static int rs_move_siso_to_other(struct iwl_priv *priv,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001383 struct iwl4965_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001384 struct ieee80211_conf *conf,
1385 struct sta_info *sta,
Zhu Yib481de92007-09-25 17:54:57 -07001386 int index)
1387{
Tomas Winklerc33104f2008-01-14 17:46:21 -08001388 u8 is_green = lq_sta->is_green;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001389 struct iwl4965_scale_tbl_info *tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001390 &(lq_sta->lq_info[lq_sta->active_tbl]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001391 struct iwl4965_scale_tbl_info *search_tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001392 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001393 struct iwl4965_rate_scale_data *window = &(tbl->win[index]);
1394 u32 sz = (sizeof(struct iwl4965_scale_tbl_info) -
1395 (sizeof(struct iwl4965_rate_scale_data) * IWL_RATE_COUNT));
Zhu Yib481de92007-09-25 17:54:57 -07001396 u8 start_action = tbl->action;
Guy Cohenfde0db32008-04-21 15:42:01 -07001397 u8 valid_tx_ant = priv->hw_params.valid_tx_ant;
1398 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001399
1400 for (;;) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001401 lq_sta->action_counter++;
Zhu Yib481de92007-09-25 17:54:57 -07001402 switch (tbl->action) {
1403 case IWL_SISO_SWITCH_ANTENNA:
Guy Cohen39e88502008-04-23 17:14:57 -07001404 IWL_DEBUG_HT("LQ: SISO toggle Antenna\n");
1405 /*FIXME:RS: is this really needed for SISO?*/
Zhu Yib481de92007-09-25 17:54:57 -07001406 if (window->success_ratio >= IWL_RS_GOOD_RATIO)
1407 break;
Guy Cohenfde0db32008-04-21 15:42:01 -07001408 if (!rs_is_other_ant_connected(valid_tx_ant,
1409 tbl->ant_type))
Zhu Yib481de92007-09-25 17:54:57 -07001410 break;
1411
1412 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001413 rs_toggle_antenna(&search_tbl->current_rate,
Zhu Yib481de92007-09-25 17:54:57 -07001414 search_tbl);
Tomas Winklerc33104f2008-01-14 17:46:21 -08001415 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001416
1417 goto out;
1418
Guy Cohenfde0db32008-04-21 15:42:01 -07001419 case IWL_SISO_SWITCH_MIMO2:
Guy Cohen39e88502008-04-23 17:14:57 -07001420 IWL_DEBUG_HT("LQ: SISO switch to MIMO\n");
Zhu Yib481de92007-09-25 17:54:57 -07001421 memcpy(search_tbl, tbl, sz);
Zhu Yib481de92007-09-25 17:54:57 -07001422 search_tbl->is_SGI = 0;
Guy Cohenfde0db32008-04-21 15:42:01 -07001423 search_tbl->ant_type = ANT_AB; /*FIXME:RS*/
1424 ret = rs_switch_to_mimo2(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001425 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001426 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001427 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001428 goto out;
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001429 }
Zhu Yib481de92007-09-25 17:54:57 -07001430 break;
1431 case IWL_SISO_SWITCH_GI:
Guy Cohen39e88502008-04-23 17:14:57 -07001432 IWL_DEBUG_HT("LQ: SISO toggle SGI/NGI\n");
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001433
Zhu Yib481de92007-09-25 17:54:57 -07001434 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001435 if (is_green) {
1436 if (!tbl->is_SGI)
1437 break;
1438 else
1439 IWL_ERROR("SGI was set in GF+SISO\n");
Zhu Yib481de92007-09-25 17:54:57 -07001440 }
Guy Cohen39e88502008-04-23 17:14:57 -07001441 search_tbl->is_SGI = !tbl->is_SGI;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001442 rs_get_expected_tpt_table(lq_sta, search_tbl);
Guy Cohen39e88502008-04-23 17:14:57 -07001443 if (tbl->is_SGI) {
1444 s32 tpt = lq_sta->last_tpt / 100;
1445 if (tpt >= search_tbl->expected_tpt[index])
1446 break;
1447 }
1448 search_tbl->current_rate = rate_n_flags_from_tbl(
1449 search_tbl, index, is_green);
1450 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001451 goto out;
1452 }
1453 tbl->action++;
1454 if (tbl->action > IWL_SISO_SWITCH_GI)
1455 tbl->action = IWL_SISO_SWITCH_ANTENNA;
1456
1457 if (tbl->action == start_action)
1458 break;
1459 }
1460 return 0;
1461
1462 out:
1463 tbl->action++;
1464 if (tbl->action > IWL_SISO_SWITCH_GI)
1465 tbl->action = IWL_SISO_SWITCH_ANTENNA;
1466 return 0;
1467}
1468
Ben Cahill77626352007-11-29 11:09:44 +08001469/*
1470 * Try to switch to new modulation mode from MIMO
1471 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001472static int rs_move_mimo_to_other(struct iwl_priv *priv,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001473 struct iwl4965_lq_sta *lq_sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001474 struct ieee80211_conf *conf,
1475 struct sta_info *sta,
Zhu Yib481de92007-09-25 17:54:57 -07001476 int index)
1477{
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001478 int ret;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001479 s8 is_green = lq_sta->is_green;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001480 struct iwl4965_scale_tbl_info *tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001481 &(lq_sta->lq_info[lq_sta->active_tbl]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001482 struct iwl4965_scale_tbl_info *search_tbl =
Tomas Winklerc33104f2008-01-14 17:46:21 -08001483 &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001484 u32 sz = (sizeof(struct iwl4965_scale_tbl_info) -
1485 (sizeof(struct iwl4965_rate_scale_data) * IWL_RATE_COUNT));
Zhu Yib481de92007-09-25 17:54:57 -07001486 u8 start_action = tbl->action;
1487
1488 for (;;) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001489 lq_sta->action_counter++;
Zhu Yib481de92007-09-25 17:54:57 -07001490 switch (tbl->action) {
1491 case IWL_MIMO_SWITCH_ANTENNA_A:
1492 case IWL_MIMO_SWITCH_ANTENNA_B:
Guy Cohen39e88502008-04-23 17:14:57 -07001493 IWL_DEBUG_HT("LQ: MIMO2 switch to SISO\n");
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001494
Ben Cahill77626352007-11-29 11:09:44 +08001495 /* Set up new search table for SISO */
Zhu Yib481de92007-09-25 17:54:57 -07001496 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001497
1498 /*FIXME:RS:need to check ant validity + C*/
Zhu Yib481de92007-09-25 17:54:57 -07001499 if (tbl->action == IWL_MIMO_SWITCH_ANTENNA_A)
Guy Cohenfde0db32008-04-21 15:42:01 -07001500 search_tbl->ant_type = ANT_A;
Zhu Yib481de92007-09-25 17:54:57 -07001501 else
Guy Cohenfde0db32008-04-21 15:42:01 -07001502 search_tbl->ant_type = ANT_B;
Zhu Yib481de92007-09-25 17:54:57 -07001503
Tomas Winklerc33104f2008-01-14 17:46:21 -08001504 ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001505 search_tbl, index);
Tomas Winklerdc2453ae2007-10-25 17:15:25 +08001506 if (!ret) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001507 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001508 goto out;
1509 }
1510 break;
1511
1512 case IWL_MIMO_SWITCH_GI:
Guy Cohen39e88502008-04-23 17:14:57 -07001513 IWL_DEBUG_HT("LQ: MIMO toggle SGI/NGI\n");
Ben Cahill77626352007-11-29 11:09:44 +08001514
1515 /* Set up new search table for MIMO */
Zhu Yib481de92007-09-25 17:54:57 -07001516 memcpy(search_tbl, tbl, sz);
Guy Cohen39e88502008-04-23 17:14:57 -07001517 search_tbl->is_SGI = !tbl->is_SGI;
1518 rs_get_expected_tpt_table(lq_sta, search_tbl);
Ben Cahill77626352007-11-29 11:09:44 +08001519 /*
1520 * If active table already uses the fastest possible
1521 * modulation (dual stream with short guard interval),
1522 * and it's working well, there's no need to look
1523 * for a better type of modulation!
1524 */
Guy Cohen39e88502008-04-23 17:14:57 -07001525 if (tbl->is_SGI) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001526 s32 tpt = lq_sta->last_tpt / 100;
Guy Cohen39e88502008-04-23 17:14:57 -07001527 if (tpt >= search_tbl->expected_tpt[index])
1528 break;
Zhu Yib481de92007-09-25 17:54:57 -07001529 }
Guy Cohen39e88502008-04-23 17:14:57 -07001530 search_tbl->current_rate = rate_n_flags_from_tbl(
1531 search_tbl, index, is_green);
1532 lq_sta->search_better_tbl = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001533 goto out;
1534
1535 }
1536 tbl->action++;
1537 if (tbl->action > IWL_MIMO_SWITCH_GI)
1538 tbl->action = IWL_MIMO_SWITCH_ANTENNA_A;
1539
1540 if (tbl->action == start_action)
1541 break;
1542 }
1543
1544 return 0;
1545 out:
1546 tbl->action++;
1547 if (tbl->action > IWL_MIMO_SWITCH_GI)
1548 tbl->action = IWL_MIMO_SWITCH_ANTENNA_A;
1549 return 0;
1550
1551}
1552
Ben Cahill77626352007-11-29 11:09:44 +08001553/*
1554 * Check whether we should continue using same modulation mode, or
1555 * begin search for a new mode, based on:
1556 * 1) # tx successes or failures while using this mode
1557 * 2) # times calling this function
1558 * 3) elapsed time in this mode (not used, for now)
1559 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001560static void rs_stay_in_table(struct iwl4965_lq_sta *lq_sta)
Zhu Yib481de92007-09-25 17:54:57 -07001561{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001562 struct iwl4965_scale_tbl_info *tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001563 int i;
1564 int active_tbl;
1565 int flush_interval_passed = 0;
1566
Tomas Winklerc33104f2008-01-14 17:46:21 -08001567 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001568
Tomas Winklerc33104f2008-01-14 17:46:21 -08001569 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07001570
Ben Cahill77626352007-11-29 11:09:44 +08001571 /* If we've been disallowing search, see if we should now allow it */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001572 if (lq_sta->stay_in_tbl) {
Zhu Yib481de92007-09-25 17:54:57 -07001573
Ben Cahill77626352007-11-29 11:09:44 +08001574 /* Elapsed time using current modulation mode */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001575 if (lq_sta->flush_timer)
Zhu Yib481de92007-09-25 17:54:57 -07001576 flush_interval_passed =
1577 time_after(jiffies,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001578 (unsigned long)(lq_sta->flush_timer +
Zhu Yib481de92007-09-25 17:54:57 -07001579 IWL_RATE_SCALE_FLUSH_INTVL));
1580
Ben Cahill77626352007-11-29 11:09:44 +08001581 /* For now, disable the elapsed time criterion */
Zhu Yib481de92007-09-25 17:54:57 -07001582 flush_interval_passed = 0;
Ben Cahill77626352007-11-29 11:09:44 +08001583
1584 /*
1585 * Check if we should allow search for new modulation mode.
1586 * If many frames have failed or succeeded, or we've used
1587 * this same modulation for a long time, allow search, and
1588 * reset history stats that keep track of whether we should
1589 * allow a new search. Also (below) reset all bitmaps and
1590 * stats in active history.
1591 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001592 if ((lq_sta->total_failed > lq_sta->max_failure_limit) ||
1593 (lq_sta->total_success > lq_sta->max_success_limit) ||
1594 ((!lq_sta->search_better_tbl) && (lq_sta->flush_timer)
Zhu Yib481de92007-09-25 17:54:57 -07001595 && (flush_interval_passed))) {
1596 IWL_DEBUG_HT("LQ: stay is expired %d %d %d\n:",
Tomas Winklerc33104f2008-01-14 17:46:21 -08001597 lq_sta->total_failed,
1598 lq_sta->total_success,
Zhu Yib481de92007-09-25 17:54:57 -07001599 flush_interval_passed);
Ben Cahill77626352007-11-29 11:09:44 +08001600
1601 /* Allow search for new mode */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001602 lq_sta->stay_in_tbl = 0; /* only place reset */
1603 lq_sta->total_failed = 0;
1604 lq_sta->total_success = 0;
1605 lq_sta->flush_timer = 0;
Ben Cahill77626352007-11-29 11:09:44 +08001606
1607 /*
1608 * Else if we've used this modulation mode enough repetitions
1609 * (regardless of elapsed time or success/failure), reset
1610 * history bitmaps and rate-specific stats for all rates in
1611 * active table.
1612 */
Mohamed Abbas403ab562007-11-06 22:06:25 -08001613 } else {
Tomas Winklerc33104f2008-01-14 17:46:21 -08001614 lq_sta->table_count++;
1615 if (lq_sta->table_count >=
1616 lq_sta->table_count_limit) {
1617 lq_sta->table_count = 0;
Zhu Yib481de92007-09-25 17:54:57 -07001618
1619 IWL_DEBUG_HT("LQ: stay in table clear win\n");
1620 for (i = 0; i < IWL_RATE_COUNT; i++)
1621 rs_rate_scale_clear_window(
1622 &(tbl->win[i]));
1623 }
1624 }
1625
Ben Cahill77626352007-11-29 11:09:44 +08001626 /* If transitioning to allow "search", reset all history
1627 * bitmaps and stats in active table (this will become the new
1628 * "search" table). */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001629 if (!lq_sta->stay_in_tbl) {
Zhu Yib481de92007-09-25 17:54:57 -07001630 for (i = 0; i < IWL_RATE_COUNT; i++)
1631 rs_rate_scale_clear_window(&(tbl->win[i]));
1632 }
1633 }
1634}
1635
Ben Cahill77626352007-11-29 11:09:44 +08001636/*
1637 * Do rate scaling and search for new modulation mode.
1638 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001639static void rs_rate_scale_perform(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001640 struct net_device *dev,
1641 struct ieee80211_hdr *hdr,
1642 struct sta_info *sta)
1643{
Ron Rindjunsky270243a2007-11-26 16:14:41 +02001644 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
1645 struct ieee80211_hw *hw = local_to_hw(local);
1646 struct ieee80211_conf *conf = &hw->conf;
Zhu Yib481de92007-09-25 17:54:57 -07001647 int low = IWL_RATE_INVALID;
1648 int high = IWL_RATE_INVALID;
1649 int index;
1650 int i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001651 struct iwl4965_rate_scale_data *window = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001652 int current_tpt = IWL_INVALID_VALUE;
1653 int low_tpt = IWL_INVALID_VALUE;
1654 int high_tpt = IWL_INVALID_VALUE;
1655 u32 fail_count;
1656 s8 scale_action = 0;
1657 u16 fc, rate_mask;
1658 u8 update_lq = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -08001659 struct iwl4965_lq_sta *lq_sta;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001660 struct iwl4965_scale_tbl_info *tbl, *tbl1;
Zhu Yib481de92007-09-25 17:54:57 -07001661 u16 rate_scale_index_msk = 0;
Guy Cohen39e88502008-04-23 17:14:57 -07001662 u32 rate;
Zhu Yib481de92007-09-25 17:54:57 -07001663 u8 is_green = 0;
1664 u8 active_tbl = 0;
1665 u8 done_search = 0;
1666 u16 high_low;
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001667#ifdef CONFIG_IWL4965_HT
1668 u8 tid = MAX_TID_COUNT;
1669 __le16 *qc;
1670#endif
Zhu Yib481de92007-09-25 17:54:57 -07001671
1672 IWL_DEBUG_RATE("rate scale calculate new rate for skb\n");
1673
1674 fc = le16_to_cpu(hdr->frame_control);
1675 if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1)) {
1676 /* Send management frames and broadcast/multicast data using
1677 * lowest rate. */
1678 /* TODO: this could probably be improved.. */
1679 return;
1680 }
1681
1682 if (!sta || !sta->rate_ctrl_priv)
1683 return;
1684
1685 if (!priv->lq_mngr.lq_ready) {
1686 IWL_DEBUG_RATE("still rate scaling not ready\n");
1687 return;
1688 }
Tomas Winklerc33104f2008-01-14 17:46:21 -08001689 lq_sta = (struct iwl4965_lq_sta *)sta->rate_ctrl_priv;
Zhu Yib481de92007-09-25 17:54:57 -07001690
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02001691#ifdef CONFIG_IWL4965_HT
1692 qc = ieee80211_get_qos_ctrl(hdr);
1693 if (qc) {
1694 tid = (u8)(le16_to_cpu(*qc) & 0xf);
1695 rs_tl_add_packet(lq_sta, tid);
1696 }
1697#endif
Ben Cahill77626352007-11-29 11:09:44 +08001698 /*
1699 * Select rate-scale / modulation-mode table to work with in
1700 * the rest of this function: "search" if searching for better
1701 * modulation mode, or "active" if doing rate scaling within a mode.
1702 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001703 if (!lq_sta->search_better_tbl)
1704 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001705 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08001706 active_tbl = 1 - lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001707
Tomas Winklerc33104f2008-01-14 17:46:21 -08001708 tbl = &(lq_sta->lq_info[active_tbl]);
1709 is_green = lq_sta->is_green;
Zhu Yib481de92007-09-25 17:54:57 -07001710
Ben Cahill77626352007-11-29 11:09:44 +08001711 /* current tx rate */
Johannes Berg8318d782008-01-24 19:38:38 +01001712 index = sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07001713
1714 IWL_DEBUG_RATE("Rate scale index %d for type %d\n", index,
1715 tbl->lq_type);
1716
Ben Cahill77626352007-11-29 11:09:44 +08001717 /* rates available for this association, and for modulation mode */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001718 rs_get_supported_rates(lq_sta, hdr, tbl->lq_type,
Zhu Yib481de92007-09-25 17:54:57 -07001719 &rate_mask);
1720
1721 IWL_DEBUG_RATE("mask 0x%04X \n", rate_mask);
1722
1723 /* mask with station rate restriction */
1724 if (is_legacy(tbl->lq_type)) {
Johannes Berg8318d782008-01-24 19:38:38 +01001725 if (lq_sta->band == IEEE80211_BAND_5GHZ)
Ben Cahill77626352007-11-29 11:09:44 +08001726 /* supp_rates has no CCK bits in A mode */
Zhu Yib481de92007-09-25 17:54:57 -07001727 rate_scale_index_msk = (u16) (rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -08001728 (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
Zhu Yib481de92007-09-25 17:54:57 -07001729 else
1730 rate_scale_index_msk = (u16) (rate_mask &
Tomas Winklerc33104f2008-01-14 17:46:21 -08001731 lq_sta->supp_rates);
Zhu Yib481de92007-09-25 17:54:57 -07001732
1733 } else
1734 rate_scale_index_msk = rate_mask;
1735
1736 if (!rate_scale_index_msk)
1737 rate_scale_index_msk = rate_mask;
1738
Ben Cahill77626352007-11-29 11:09:44 +08001739 /* If current rate is no longer supported on current association,
1740 * or user changed preferences for rates, find a new supported rate. */
Zhu Yib481de92007-09-25 17:54:57 -07001741 if (index < 0 || !((1 << index) & rate_scale_index_msk)) {
1742 index = IWL_INVALID_VALUE;
1743 update_lq = 1;
1744
Ben Cahill77626352007-11-29 11:09:44 +08001745 /* get the highest available rate */
Zhu Yib481de92007-09-25 17:54:57 -07001746 for (i = 0; i <= IWL_RATE_COUNT; i++) {
1747 if ((1 << i) & rate_scale_index_msk)
1748 index = i;
1749 }
1750
1751 if (index == IWL_INVALID_VALUE) {
1752 IWL_WARNING("Can not find a suitable rate\n");
1753 return;
1754 }
1755 }
1756
Ben Cahill77626352007-11-29 11:09:44 +08001757 /* Get expected throughput table and history window for current rate */
Zhu Yib481de92007-09-25 17:54:57 -07001758 if (!tbl->expected_tpt)
Tomas Winklerc33104f2008-01-14 17:46:21 -08001759 rs_get_expected_tpt_table(lq_sta, tbl);
Zhu Yib481de92007-09-25 17:54:57 -07001760
1761 window = &(tbl->win[index]);
1762
Ben Cahill77626352007-11-29 11:09:44 +08001763 /*
1764 * If there is not enough history to calculate actual average
1765 * throughput, keep analyzing results of more tx frames, without
1766 * changing rate or mode (bypass most of the rest of this function).
1767 * Set up new rate table in uCode only if old rate is not supported
1768 * in current association (use new rate found above).
1769 */
Zhu Yib481de92007-09-25 17:54:57 -07001770 fail_count = window->counter - window->success_counter;
1771 if (((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
1772 (window->success_counter < IWL_RATE_MIN_SUCCESS_TH))
1773 || (tbl->expected_tpt == NULL)) {
1774 IWL_DEBUG_RATE("LQ: still below TH succ %d total %d "
1775 "for index %d\n",
1776 window->success_counter, window->counter, index);
Ben Cahill77626352007-11-29 11:09:44 +08001777
1778 /* Can't calculate this yet; not enough history */
Zhu Yib481de92007-09-25 17:54:57 -07001779 window->average_tpt = IWL_INVALID_VALUE;
Ben Cahill77626352007-11-29 11:09:44 +08001780
1781 /* Should we stay with this modulation mode,
1782 * or search for a new one? */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001783 rs_stay_in_table(lq_sta);
Ben Cahill77626352007-11-29 11:09:44 +08001784
1785 /* Set up new rate table in uCode, if needed */
Zhu Yib481de92007-09-25 17:54:57 -07001786 if (update_lq) {
Guy Cohen39e88502008-04-23 17:14:57 -07001787 rate = rate_n_flags_from_tbl(tbl, index, is_green);
1788 rs_fill_link_cmd(priv, lq_sta, rate, &lq_sta->lq);
Tomas Winkler66c73db2008-04-15 16:01:40 -07001789 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07001790 }
1791 goto out;
1792
Ben Cahill77626352007-11-29 11:09:44 +08001793 /* Else we have enough samples; calculate estimate of
1794 * actual average throughput */
Zhu Yib481de92007-09-25 17:54:57 -07001795 } else
1796 window->average_tpt = ((window->success_ratio *
1797 tbl->expected_tpt[index] + 64) / 128);
1798
Ben Cahill77626352007-11-29 11:09:44 +08001799 /* If we are searching for better modulation mode, check success. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001800 if (lq_sta->search_better_tbl) {
Zhu Yib481de92007-09-25 17:54:57 -07001801 int success_limit = IWL_RATE_SCALE_SWITCH;
1802
Ben Cahill77626352007-11-29 11:09:44 +08001803 /* If good success, continue using the "search" mode;
1804 * no need to send new link quality command, since we're
1805 * continuing to use the setup that we've been trying. */
Zhu Yib481de92007-09-25 17:54:57 -07001806 if ((window->success_ratio > success_limit) ||
Tomas Winklerc33104f2008-01-14 17:46:21 -08001807 (window->average_tpt > lq_sta->last_tpt)) {
Zhu Yib481de92007-09-25 17:54:57 -07001808 if (!is_legacy(tbl->lq_type)) {
1809 IWL_DEBUG_HT("LQ: we are switching to HT"
1810 " rate suc %d current tpt %d"
1811 " old tpt %d\n",
1812 window->success_ratio,
1813 window->average_tpt,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001814 lq_sta->last_tpt);
1815 lq_sta->enable_counter = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001816 }
Ben Cahill77626352007-11-29 11:09:44 +08001817 /* Swap tables; "search" becomes "active" */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001818 lq_sta->active_tbl = active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07001819 current_tpt = window->average_tpt;
Ben Cahill77626352007-11-29 11:09:44 +08001820
1821 /* Else poor success; go back to mode in "active" table */
Zhu Yib481de92007-09-25 17:54:57 -07001822 } else {
Ben Cahill77626352007-11-29 11:09:44 +08001823 /* Nullify "search" table */
Zhu Yib481de92007-09-25 17:54:57 -07001824 tbl->lq_type = LQ_NONE;
Ben Cahill77626352007-11-29 11:09:44 +08001825
1826 /* Revert to "active" table */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001827 active_tbl = lq_sta->active_tbl;
1828 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07001829
Ben Cahill77626352007-11-29 11:09:44 +08001830 /* Revert to "active" rate and throughput info */
Tomas Winkler17744ff2008-03-02 01:52:00 +02001831 index = iwl4965_hwrate_to_plcp_idx(
Guy Cohen39e88502008-04-23 17:14:57 -07001832 tbl->current_rate);
Tomas Winklerc33104f2008-01-14 17:46:21 -08001833 current_tpt = lq_sta->last_tpt;
Ben Cahill77626352007-11-29 11:09:44 +08001834
1835 /* Need to set up a new rate table in uCode */
1836 update_lq = 1;
Zhu Yib481de92007-09-25 17:54:57 -07001837 IWL_DEBUG_HT("XXY GO BACK TO OLD TABLE\n");
1838 }
Ben Cahill77626352007-11-29 11:09:44 +08001839
1840 /* Either way, we've made a decision; modulation mode
1841 * search is done, allow rate adjustment next time. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001842 lq_sta->search_better_tbl = 0;
Ben Cahill77626352007-11-29 11:09:44 +08001843 done_search = 1; /* Don't switch modes below! */
Zhu Yib481de92007-09-25 17:54:57 -07001844 goto lq_update;
1845 }
1846
Ben Cahill77626352007-11-29 11:09:44 +08001847 /* (Else) not in search of better modulation mode, try for better
1848 * starting rate, while staying in this mode. */
Zhu Yib481de92007-09-25 17:54:57 -07001849 high_low = rs_get_adjacent_rate(index, rate_scale_index_msk,
1850 tbl->lq_type);
1851 low = high_low & 0xff;
1852 high = (high_low >> 8) & 0xff;
1853
Ben Cahill77626352007-11-29 11:09:44 +08001854 /* Collect measured throughputs for current and adjacent rates */
Zhu Yib481de92007-09-25 17:54:57 -07001855 current_tpt = window->average_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07001856 if (low != IWL_RATE_INVALID)
1857 low_tpt = tbl->win[low].average_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07001858 if (high != IWL_RATE_INVALID)
1859 high_tpt = tbl->win[high].average_tpt;
1860
Ben Cahill77626352007-11-29 11:09:44 +08001861 /* Assume rate increase */
Zhu Yib481de92007-09-25 17:54:57 -07001862 scale_action = 1;
1863
Ben Cahill77626352007-11-29 11:09:44 +08001864 /* Too many failures, decrease rate */
Zhu Yib481de92007-09-25 17:54:57 -07001865 if ((window->success_ratio <= IWL_RATE_DECREASE_TH) ||
1866 (current_tpt == 0)) {
1867 IWL_DEBUG_RATE("decrease rate because of low success_ratio\n");
1868 scale_action = -1;
Ben Cahill77626352007-11-29 11:09:44 +08001869
1870 /* No throughput measured yet for adjacent rates; try increase. */
Zhu Yib481de92007-09-25 17:54:57 -07001871 } else if ((low_tpt == IWL_INVALID_VALUE) &&
1872 (high_tpt == IWL_INVALID_VALUE))
1873 scale_action = 1;
Ben Cahill77626352007-11-29 11:09:44 +08001874
1875 /* Both adjacent throughputs are measured, but neither one has better
1876 * throughput; we're using the best rate, don't change it! */
Zhu Yib481de92007-09-25 17:54:57 -07001877 else if ((low_tpt != IWL_INVALID_VALUE) &&
1878 (high_tpt != IWL_INVALID_VALUE) &&
1879 (low_tpt < current_tpt) &&
1880 (high_tpt < current_tpt))
1881 scale_action = 0;
Ben Cahill77626352007-11-29 11:09:44 +08001882
1883 /* At least one adjacent rate's throughput is measured,
1884 * and may have better performance. */
Zhu Yib481de92007-09-25 17:54:57 -07001885 else {
Ben Cahill77626352007-11-29 11:09:44 +08001886 /* Higher adjacent rate's throughput is measured */
Zhu Yib481de92007-09-25 17:54:57 -07001887 if (high_tpt != IWL_INVALID_VALUE) {
Ben Cahill77626352007-11-29 11:09:44 +08001888 /* Higher rate has better throughput */
Zhu Yib481de92007-09-25 17:54:57 -07001889 if (high_tpt > current_tpt)
1890 scale_action = 1;
1891 else {
1892 IWL_DEBUG_RATE
1893 ("decrease rate because of high tpt\n");
1894 scale_action = -1;
1895 }
Ben Cahill77626352007-11-29 11:09:44 +08001896
1897 /* Lower adjacent rate's throughput is measured */
Zhu Yib481de92007-09-25 17:54:57 -07001898 } else if (low_tpt != IWL_INVALID_VALUE) {
Ben Cahill77626352007-11-29 11:09:44 +08001899 /* Lower rate has better throughput */
Zhu Yib481de92007-09-25 17:54:57 -07001900 if (low_tpt > current_tpt) {
1901 IWL_DEBUG_RATE
1902 ("decrease rate because of low tpt\n");
1903 scale_action = -1;
1904 } else
1905 scale_action = 1;
1906 }
1907 }
1908
Ben Cahill77626352007-11-29 11:09:44 +08001909 /* Sanity check; asked for decrease, but success rate or throughput
1910 * has been good at old rate. Don't change it. */
Zhu Yib481de92007-09-25 17:54:57 -07001911 if (scale_action == -1) {
1912 if ((low != IWL_RATE_INVALID) &&
1913 ((window->success_ratio > IWL_RATE_HIGH_TH) ||
1914 (current_tpt > (100 * tbl->expected_tpt[low]))))
1915 scale_action = 0;
Ben Cahill77626352007-11-29 11:09:44 +08001916
1917 /* Sanity check; asked for increase, but success rate has not been great
1918 * even at old rate, higher rate will be worse. Don't change it. */
Zhu Yib481de92007-09-25 17:54:57 -07001919 } else if ((scale_action == 1) &&
1920 (window->success_ratio < IWL_RATE_INCREASE_TH))
1921 scale_action = 0;
1922
1923 switch (scale_action) {
1924 case -1:
Ben Cahill77626352007-11-29 11:09:44 +08001925 /* Decrease starting rate, update uCode's rate table */
Zhu Yib481de92007-09-25 17:54:57 -07001926 if (low != IWL_RATE_INVALID) {
1927 update_lq = 1;
1928 index = low;
1929 }
1930 break;
1931 case 1:
Ben Cahill77626352007-11-29 11:09:44 +08001932 /* Increase starting rate, update uCode's rate table */
Zhu Yib481de92007-09-25 17:54:57 -07001933 if (high != IWL_RATE_INVALID) {
1934 update_lq = 1;
1935 index = high;
1936 }
1937
1938 break;
1939 case 0:
Ben Cahill77626352007-11-29 11:09:44 +08001940 /* No change */
Zhu Yib481de92007-09-25 17:54:57 -07001941 default:
1942 break;
1943 }
1944
1945 IWL_DEBUG_HT("choose rate scale index %d action %d low %d "
1946 "high %d type %d\n",
1947 index, scale_action, low, high, tbl->lq_type);
1948
1949 lq_update:
Ben Cahill77626352007-11-29 11:09:44 +08001950 /* Replace uCode's rate table for the destination station. */
Zhu Yib481de92007-09-25 17:54:57 -07001951 if (update_lq) {
Guy Cohen39e88502008-04-23 17:14:57 -07001952 rate = rate_n_flags_from_tbl(tbl, index, is_green);
1953 rs_fill_link_cmd(priv, lq_sta, rate, &lq_sta->lq);
Tomas Winkler66c73db2008-04-15 16:01:40 -07001954 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07001955 }
Ben Cahill77626352007-11-29 11:09:44 +08001956
1957 /* Should we stay with this modulation mode, or search for a new one? */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001958 rs_stay_in_table(lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07001959
Ben Cahill77626352007-11-29 11:09:44 +08001960 /*
1961 * Search for new modulation mode if we're:
1962 * 1) Not changing rates right now
1963 * 2) Not just finishing up a search
1964 * 3) Allowing a new search
1965 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001966 if (!update_lq && !done_search && !lq_sta->stay_in_tbl) {
Ben Cahill77626352007-11-29 11:09:44 +08001967 /* Save current throughput to compare with "search" throughput*/
Tomas Winklerc33104f2008-01-14 17:46:21 -08001968 lq_sta->last_tpt = current_tpt;
Zhu Yib481de92007-09-25 17:54:57 -07001969
Ben Cahill77626352007-11-29 11:09:44 +08001970 /* Select a new "search" modulation mode to try.
1971 * If one is found, set up the new "search" table. */
Zhu Yib481de92007-09-25 17:54:57 -07001972 if (is_legacy(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08001973 rs_move_legacy_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07001974 else if (is_siso(tbl->lq_type))
Tomas Winklerc33104f2008-01-14 17:46:21 -08001975 rs_move_siso_to_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07001976 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08001977 rs_move_mimo_to_other(priv, lq_sta, conf, sta, index);
Zhu Yib481de92007-09-25 17:54:57 -07001978
Ben Cahill77626352007-11-29 11:09:44 +08001979 /* If new "search" mode was selected, set up in uCode table */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001980 if (lq_sta->search_better_tbl) {
Ben Cahill77626352007-11-29 11:09:44 +08001981 /* Access the "search" table, clear its history. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08001982 tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
Zhu Yib481de92007-09-25 17:54:57 -07001983 for (i = 0; i < IWL_RATE_COUNT; i++)
1984 rs_rate_scale_clear_window(&(tbl->win[i]));
1985
Ben Cahill77626352007-11-29 11:09:44 +08001986 /* Use new "search" start rate */
Tomas Winkler17744ff2008-03-02 01:52:00 +02001987 index = iwl4965_hwrate_to_plcp_idx(
Guy Cohen39e88502008-04-23 17:14:57 -07001988 tbl->current_rate);
Zhu Yib481de92007-09-25 17:54:57 -07001989
1990 IWL_DEBUG_HT("Switch current mcs: %X index: %d\n",
Guy Cohen39e88502008-04-23 17:14:57 -07001991 tbl->current_rate, index);
1992 rs_fill_link_cmd(priv, lq_sta, tbl->current_rate,
Tomas Winklerc33104f2008-01-14 17:46:21 -08001993 &lq_sta->lq);
Tomas Winkler66c73db2008-04-15 16:01:40 -07001994 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07001995 }
Zhu Yib481de92007-09-25 17:54:57 -07001996
Ben Cahill77626352007-11-29 11:09:44 +08001997 /* If the "active" (non-search) mode was legacy,
1998 * and we've tried switching antennas,
1999 * but we haven't been able to try HT modes (not available),
2000 * stay with best antenna legacy modulation for a while
2001 * before next round of mode comparisons. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002002 tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07002003 if (is_legacy(tbl1->lq_type) &&
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002004#ifdef CONFIG_IWL4965_HT
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002005 (!(conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE)) &&
Zhu Yib481de92007-09-25 17:54:57 -07002006#endif
Tomas Winklerc33104f2008-01-14 17:46:21 -08002007 (lq_sta->action_counter >= 1)) {
2008 lq_sta->action_counter = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002009 IWL_DEBUG_HT("LQ: STAY in legacy table\n");
Tomas Winklerc33104f2008-01-14 17:46:21 -08002010 rs_set_stay_in_table(1, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002011 }
2012
Ben Cahill77626352007-11-29 11:09:44 +08002013 /* If we're in an HT mode, and all 3 mode switch actions
2014 * have been tried and compared, stay in this best modulation
2015 * mode for a while before next round of mode comparisons. */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002016 if (lq_sta->enable_counter &&
2017 (lq_sta->action_counter >= IWL_ACTION_LIMIT)) {
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002018#ifdef CONFIG_IWL4965_HT
2019 if ((lq_sta->last_tpt > IWL_AGG_TPT_THREHOLD) &&
2020 (lq_sta->tx_agg_tid_en & (1 << tid)) &&
2021 (tid != MAX_TID_COUNT)) {
2022 IWL_DEBUG_HT("try to aggregate tid %d\n", tid);
2023 rs_tl_turn_on_agg(priv, tid, lq_sta, sta);
Zhu Yib481de92007-09-25 17:54:57 -07002024 }
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002025#endif /*CONFIG_IWL4965_HT */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002026 lq_sta->action_counter = 0;
2027 rs_set_stay_in_table(0, lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002028 }
Ben Cahill77626352007-11-29 11:09:44 +08002029
2030 /*
2031 * Else, don't search for a new modulation mode.
2032 * Put new timestamp in stay-in-modulation-mode flush timer if:
2033 * 1) Not changing rates right now
2034 * 2) Not just finishing up a search
2035 * 3) flush timer is empty
2036 */
Zhu Yib481de92007-09-25 17:54:57 -07002037 } else {
Tomas Winklerc33104f2008-01-14 17:46:21 -08002038 if ((!update_lq) && (!done_search) && (!lq_sta->flush_timer))
2039 lq_sta->flush_timer = jiffies;
Zhu Yib481de92007-09-25 17:54:57 -07002040 }
2041
2042out:
Guy Cohen39e88502008-04-23 17:14:57 -07002043 tbl->current_rate = rate_n_flags_from_tbl(tbl, index, is_green);
Zhu Yib481de92007-09-25 17:54:57 -07002044 i = index;
Johannes Berg8318d782008-01-24 19:38:38 +01002045 sta->last_txrate_idx = i;
Zhu Yib481de92007-09-25 17:54:57 -07002046
Johannes Berg8318d782008-01-24 19:38:38 +01002047 /* sta->txrate_idx is an index to A mode rates which start
Zhu Yib481de92007-09-25 17:54:57 -07002048 * at IWL_FIRST_OFDM_RATE
2049 */
Johannes Berg8318d782008-01-24 19:38:38 +01002050 if (lq_sta->band == IEEE80211_BAND_5GHZ)
2051 sta->txrate_idx = i - IWL_FIRST_OFDM_RATE;
Zhu Yib481de92007-09-25 17:54:57 -07002052 else
Johannes Berg8318d782008-01-24 19:38:38 +01002053 sta->txrate_idx = i;
Zhu Yib481de92007-09-25 17:54:57 -07002054
2055 return;
2056}
2057
2058
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002059static void rs_initialize_lq(struct iwl_priv *priv,
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002060 struct ieee80211_conf *conf,
Zhu Yib481de92007-09-25 17:54:57 -07002061 struct sta_info *sta)
2062{
2063 int i;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002064 struct iwl4965_lq_sta *lq_sta;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002065 struct iwl4965_scale_tbl_info *tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002066 u8 active_tbl = 0;
2067 int rate_idx;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002068 u8 use_green = rs_use_green(priv, conf);
Guy Cohen39e88502008-04-23 17:14:57 -07002069 u32 rate;
Zhu Yib481de92007-09-25 17:54:57 -07002070
2071 if (!sta || !sta->rate_ctrl_priv)
2072 goto out;
2073
Tomas Winklerc33104f2008-01-14 17:46:21 -08002074 lq_sta = (struct iwl4965_lq_sta *)sta->rate_ctrl_priv;
Johannes Berg8318d782008-01-24 19:38:38 +01002075 i = sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002076
Tomas Winklerc33104f2008-01-14 17:46:21 -08002077 if ((lq_sta->lq.sta_id == 0xff) &&
Zhu Yib481de92007-09-25 17:54:57 -07002078 (priv->iw_mode == IEEE80211_IF_TYPE_IBSS))
2079 goto out;
2080
Tomas Winklerc33104f2008-01-14 17:46:21 -08002081 if (!lq_sta->search_better_tbl)
2082 active_tbl = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002083 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08002084 active_tbl = 1 - lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002085
Tomas Winklerc33104f2008-01-14 17:46:21 -08002086 tbl = &(lq_sta->lq_info[active_tbl]);
Zhu Yib481de92007-09-25 17:54:57 -07002087
2088 if ((i < 0) || (i >= IWL_RATE_COUNT))
2089 i = 0;
2090
Guy Cohenfde0db32008-04-21 15:42:01 -07002091 /* FIXME:RS: This is also wrong in 4965 */
Guy Cohen39e88502008-04-23 17:14:57 -07002092 rate = iwl4965_rates[i].plcp;
2093 rate |= RATE_MCS_ANT_B_MSK;
2094 rate &= ~RATE_MCS_ANT_A_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002095
2096 if (i >= IWL_FIRST_CCK_RATE && i <= IWL_LAST_CCK_RATE)
Guy Cohen39e88502008-04-23 17:14:57 -07002097 rate |= RATE_MCS_CCK_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002098
Guy Cohenfde0db32008-04-21 15:42:01 -07002099 tbl->ant_type = ANT_B;
Guy Cohen39e88502008-04-23 17:14:57 -07002100 rs_get_tbl_info_from_mcs(rate, priv->band, tbl, &rate_idx);
Guy Cohenfde0db32008-04-21 15:42:01 -07002101 if (!rs_is_ant_connected(priv->hw_params.valid_tx_ant, tbl->ant_type))
Guy Cohen39e88502008-04-23 17:14:57 -07002102 rs_toggle_antenna(&rate, tbl);
Zhu Yib481de92007-09-25 17:54:57 -07002103
Guy Cohen39e88502008-04-23 17:14:57 -07002104 rate = rate_n_flags_from_tbl(tbl, rate_idx, use_green);
2105 tbl->current_rate = rate;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002106 rs_get_expected_tpt_table(lq_sta, tbl);
Guy Cohen39e88502008-04-23 17:14:57 -07002107 rs_fill_link_cmd(NULL, lq_sta, rate, &lq_sta->lq);
Tomas Winkler66c73db2008-04-15 16:01:40 -07002108 iwl_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07002109 out:
2110 return;
2111}
2112
Mattias Nissler1abbe492007-12-20 13:50:07 +01002113static void rs_get_rate(void *priv_rate, struct net_device *dev,
Johannes Berg8318d782008-01-24 19:38:38 +01002114 struct ieee80211_supported_band *sband,
2115 struct sk_buff *skb,
Mattias Nissler1abbe492007-12-20 13:50:07 +01002116 struct rate_selection *sel)
Zhu Yib481de92007-09-25 17:54:57 -07002117{
2118
2119 int i;
2120 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002121 struct ieee80211_conf *conf = &local->hw.conf;
Zhu Yib481de92007-09-25 17:54:57 -07002122 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2123 struct sta_info *sta;
Michael Wu2bc454b2007-12-25 19:33:16 -05002124 u16 fc;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002125 struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002126 struct iwl4965_lq_sta *lq_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002127
Zhu Yi58826352007-09-27 11:27:39 +08002128 IWL_DEBUG_RATE_LIMIT("rate scale calculate new rate for skb\n");
Zhu Yib481de92007-09-25 17:54:57 -07002129
Johannes Bergd0709a62008-02-25 16:27:46 +01002130 rcu_read_lock();
2131
Zhu Yib481de92007-09-25 17:54:57 -07002132 sta = sta_info_get(local, hdr->addr1);
2133
Michael Wu2bc454b2007-12-25 19:33:16 -05002134 /* Send management frames and broadcast/multicast data using lowest
2135 * rate. */
2136 fc = le16_to_cpu(hdr->frame_control);
2137 if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1) ||
2138 !sta || !sta->rate_ctrl_priv) {
Johannes Berg8318d782008-01-24 19:38:38 +01002139 sel->rate = rate_lowest(local, sband, sta);
Tomas Winklerf4d60822008-03-05 11:31:00 -08002140 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07002141 }
2142
Tomas Winklerc33104f2008-01-14 17:46:21 -08002143 lq_sta = (struct iwl4965_lq_sta *)sta->rate_ctrl_priv;
Johannes Berg8318d782008-01-24 19:38:38 +01002144 i = sta->last_txrate_idx;
Zhu Yib481de92007-09-25 17:54:57 -07002145
Tomas Winklerc33104f2008-01-14 17:46:21 -08002146 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
2147 !lq_sta->ibss_sta_added) {
Tomas Winkler947b13a2008-04-16 16:34:48 -07002148 u8 sta_id = iwl_find_station(priv, hdr->addr1);
Joe Perches0795af52007-10-03 17:59:30 -07002149 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07002150
2151 if (sta_id == IWL_INVALID_STATION) {
Joe Perches0795af52007-10-03 17:59:30 -07002152 IWL_DEBUG_RATE("LQ: ADD station %s\n",
2153 print_mac(mac, hdr->addr1));
Ron Rindjunsky67d62032007-11-26 16:14:40 +02002154 sta_id = iwl4965_add_station_flags(priv, hdr->addr1,
2155 0, CMD_ASYNC, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07002156 }
2157 if ((sta_id != IWL_INVALID_STATION)) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08002158 lq_sta->lq.sta_id = sta_id;
2159 lq_sta->lq.rs_table[0].rate_n_flags = 0;
2160 lq_sta->ibss_sta_added = 1;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002161 rs_initialize_lq(priv, conf, sta);
Zhu Yib481de92007-09-25 17:54:57 -07002162 }
Tomas Winklerc33104f2008-01-14 17:46:21 -08002163 if (!lq_sta->ibss_sta_added)
Zhu Yib481de92007-09-25 17:54:57 -07002164 goto done;
2165 }
2166
Tomas Winklerf4d60822008-03-05 11:31:00 -08002167done:
Mattias Nissler1abbe492007-12-20 13:50:07 +01002168 if ((i < 0) || (i > IWL_RATE_COUNT)) {
Johannes Berg8318d782008-01-24 19:38:38 +01002169 sel->rate = rate_lowest(local, sband, sta);
Tomas Winklerf4d60822008-03-05 11:31:00 -08002170 goto out;
Mattias Nissler1abbe492007-12-20 13:50:07 +01002171 }
Zhu Yib481de92007-09-25 17:54:57 -07002172
Mattias Nissler1abbe492007-12-20 13:50:07 +01002173 sel->rate = &priv->ieee_rates[i];
Tomas Winklerf4d60822008-03-05 11:31:00 -08002174out:
2175 rcu_read_unlock();
Zhu Yib481de92007-09-25 17:54:57 -07002176}
2177
2178static void *rs_alloc_sta(void *priv, gfp_t gfp)
2179{
Tomas Winklerc33104f2008-01-14 17:46:21 -08002180 struct iwl4965_lq_sta *lq_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002181 int i, j;
2182
2183 IWL_DEBUG_RATE("create station rate scale window\n");
2184
Tomas Winklerc33104f2008-01-14 17:46:21 -08002185 lq_sta = kzalloc(sizeof(struct iwl4965_lq_sta), gfp);
Zhu Yib481de92007-09-25 17:54:57 -07002186
Tomas Winklerc33104f2008-01-14 17:46:21 -08002187 if (lq_sta == NULL)
Zhu Yib481de92007-09-25 17:54:57 -07002188 return NULL;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002189 lq_sta->lq.sta_id = 0xff;
Zhu Yib481de92007-09-25 17:54:57 -07002190
Zhu Yi63fddb92007-09-27 11:27:36 +08002191
Zhu Yib481de92007-09-25 17:54:57 -07002192 for (j = 0; j < LQ_SIZE; j++)
2193 for (i = 0; i < IWL_RATE_COUNT; i++)
Tomas Winklerc33104f2008-01-14 17:46:21 -08002194 rs_rate_scale_clear_window(&(lq_sta->lq_info[j].win[i]));
Zhu Yib481de92007-09-25 17:54:57 -07002195
Tomas Winklerc33104f2008-01-14 17:46:21 -08002196 return lq_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002197}
2198
2199static void rs_rate_init(void *priv_rate, void *priv_sta,
2200 struct ieee80211_local *local,
2201 struct sta_info *sta)
2202{
2203 int i, j;
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002204 struct ieee80211_conf *conf = &local->hw.conf;
Johannes Berg8318d782008-01-24 19:38:38 +01002205 struct ieee80211_supported_band *sband;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002206 struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002207 struct iwl4965_lq_sta *lq_sta = priv_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002208
Johannes Berg8318d782008-01-24 19:38:38 +01002209 sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
2210
Tomas Winklerc33104f2008-01-14 17:46:21 -08002211 lq_sta->flush_timer = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01002212 lq_sta->supp_rates = sta->supp_rates[sband->band];
2213 sta->txrate_idx = 3;
Zhu Yib481de92007-09-25 17:54:57 -07002214 for (j = 0; j < LQ_SIZE; j++)
2215 for (i = 0; i < IWL_RATE_COUNT; i++)
Tomas Winklerc33104f2008-01-14 17:46:21 -08002216 rs_rate_scale_clear_window(&(lq_sta->lq_info[j].win[i]));
Zhu Yib481de92007-09-25 17:54:57 -07002217
2218 IWL_DEBUG_RATE("rate scale global init\n");
2219 /* TODO: what is a good starting rate for STA? About middle? Maybe not
2220 * the lowest or the highest rate.. Could consider using RSSI from
2221 * previous packets? Need to have IEEE 802.1X auth succeed immediately
2222 * after assoc.. */
2223
Tomas Winklerc33104f2008-01-14 17:46:21 -08002224 lq_sta->ibss_sta_added = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002225 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
Tomas Winkler947b13a2008-04-16 16:34:48 -07002226 u8 sta_id = iwl_find_station(priv, sta->addr);
Joe Perches0795af52007-10-03 17:59:30 -07002227 DECLARE_MAC_BUF(mac);
2228
Zhu Yib481de92007-09-25 17:54:57 -07002229 /* for IBSS the call are from tasklet */
Joe Perches0795af52007-10-03 17:59:30 -07002230 IWL_DEBUG_HT("LQ: ADD station %s\n",
2231 print_mac(mac, sta->addr));
Zhu Yib481de92007-09-25 17:54:57 -07002232
2233 if (sta_id == IWL_INVALID_STATION) {
Joe Perches0795af52007-10-03 17:59:30 -07002234 IWL_DEBUG_RATE("LQ: ADD station %s\n",
2235 print_mac(mac, sta->addr));
Ron Rindjunsky67d62032007-11-26 16:14:40 +02002236 sta_id = iwl4965_add_station_flags(priv, sta->addr,
2237 0, CMD_ASYNC, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07002238 }
2239 if ((sta_id != IWL_INVALID_STATION)) {
Tomas Winklerc33104f2008-01-14 17:46:21 -08002240 lq_sta->lq.sta_id = sta_id;
2241 lq_sta->lq.rs_table[0].rate_n_flags = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002242 }
2243 /* FIXME: this is w/a remove it later */
2244 priv->assoc_station_added = 1;
2245 }
2246
Ben Cahill77626352007-11-29 11:09:44 +08002247 /* Find highest tx rate supported by hardware and destination station */
Johannes Berg8318d782008-01-24 19:38:38 +01002248 for (i = 0; i < sband->n_bitrates; i++)
2249 if (sta->supp_rates[sband->band] & BIT(i))
2250 sta->txrate_idx = i;
2251
2252 sta->last_txrate_idx = sta->txrate_idx;
2253 /* WTF is with this bogus comment? A doesn't have cck rates */
Ben Cahill77626352007-11-29 11:09:44 +08002254 /* For MODE_IEEE80211A, cck rates are at end of rate table */
Johannes Berg8318d782008-01-24 19:38:38 +01002255 if (local->hw.conf.channel->band == IEEE80211_BAND_5GHZ)
2256 sta->last_txrate_idx += IWL_FIRST_OFDM_RATE;
Zhu Yib481de92007-09-25 17:54:57 -07002257
Tomas Winklerc33104f2008-01-14 17:46:21 -08002258 lq_sta->is_dup = 0;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002259 lq_sta->is_green = rs_use_green(priv, conf);
Guy Cohen39e88502008-04-23 17:14:57 -07002260 lq_sta->active_legacy_rate = priv->active_rate & ~(0x1000);
Tomas Winklerc33104f2008-01-14 17:46:21 -08002261 lq_sta->active_rate_basic = priv->active_rate_basic;
Johannes Berg8318d782008-01-24 19:38:38 +01002262 lq_sta->band = priv->band;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002263#ifdef CONFIG_IWL4965_HT
Ben Cahill77626352007-11-29 11:09:44 +08002264 /*
2265 * active_siso_rate mask includes 9 MBits (bit 5), and CCK (bits 0-3),
2266 * supp_rates[] does not; shift to convert format, force 9 MBits off.
2267 */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002268 lq_sta->active_siso_rate =
Guy Cohenfde0db32008-04-21 15:42:01 -07002269 priv->current_ht_config.supp_mcs_set[0] << 1;
2270 lq_sta->active_siso_rate |=
2271 priv->current_ht_config.supp_mcs_set[0] & 0x1;
2272 lq_sta->active_siso_rate &= ~((u16)0x2);
2273 lq_sta->active_siso_rate <<= IWL_FIRST_OFDM_RATE;
Zhu Yib481de92007-09-25 17:54:57 -07002274
Ben Cahill77626352007-11-29 11:09:44 +08002275 /* Same here */
Guy Cohenfde0db32008-04-21 15:42:01 -07002276 lq_sta->active_mimo2_rate =
2277 priv->current_ht_config.supp_mcs_set[1] << 1;
2278 lq_sta->active_mimo2_rate |=
2279 priv->current_ht_config.supp_mcs_set[1] & 0x1;
2280 lq_sta->active_mimo2_rate &= ~((u16)0x2);
2281 lq_sta->active_mimo2_rate <<= IWL_FIRST_OFDM_RATE;
2282
2283 lq_sta->active_mimo3_rate =
2284 priv->current_ht_config.supp_mcs_set[2] << 1;
2285 lq_sta->active_mimo3_rate |=
2286 priv->current_ht_config.supp_mcs_set[2] & 0x1;
2287 lq_sta->active_mimo3_rate &= ~((u16)0x2);
2288 lq_sta->active_mimo3_rate <<= IWL_FIRST_OFDM_RATE;
2289
2290 IWL_DEBUG_HT("SISO RATE %X MIMO2 RATE %X MIMO3 RATE %X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002291 lq_sta->active_siso_rate,
Guy Cohenfde0db32008-04-21 15:42:01 -07002292 lq_sta->active_mimo2_rate,
2293 lq_sta->active_mimo3_rate);
2294
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002295 /* as default allow aggregation for all tids */
2296 lq_sta->tx_agg_tid_en = IWL_AGG_ALL_TID;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002297#endif /*CONFIG_IWL4965_HT*/
Zhu Yi98d7e092007-09-27 11:27:42 +08002298#ifdef CONFIG_MAC80211_DEBUGFS
Tomas Winklerc33104f2008-01-14 17:46:21 -08002299 lq_sta->drv = priv;
Zhu Yi98d7e092007-09-27 11:27:42 +08002300#endif
Zhu Yib481de92007-09-25 17:54:57 -07002301
2302 if (priv->assoc_station_added)
2303 priv->lq_mngr.lq_ready = 1;
2304
Ron Rindjunsky270243a2007-11-26 16:14:41 +02002305 rs_initialize_lq(priv, conf, sta);
Zhu Yib481de92007-09-25 17:54:57 -07002306}
2307
Guy Cohenfde0db32008-04-21 15:42:01 -07002308static void rs_fill_link_cmd(const struct iwl_priv *priv,
2309 struct iwl4965_lq_sta *lq_sta,
Guy Cohen39e88502008-04-23 17:14:57 -07002310 u32 new_rate,
Guy Cohenfde0db32008-04-21 15:42:01 -07002311 struct iwl_link_quality_cmd *lq_cmd)
Zhu Yib481de92007-09-25 17:54:57 -07002312{
2313 int index = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002314 int rate_idx;
Zhu Yi1b696de2007-09-27 11:27:41 +08002315 int repeat_rate = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002316 u8 ant_toggle_count = 0;
2317 u8 use_ht_possible = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002318 struct iwl4965_scale_tbl_info tbl_type = { 0 };
Zhu Yib481de92007-09-25 17:54:57 -07002319
Ben Cahill77626352007-11-29 11:09:44 +08002320 /* Override starting rate (index 0) if needed for debug purposes */
Guy Cohen39e88502008-04-23 17:14:57 -07002321 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Zhu Yi98d7e092007-09-27 11:27:42 +08002322
Guy Cohen39e88502008-04-23 17:14:57 -07002323 /* Interpret new_rate (rate_n_flags) */
2324 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band,
Zhu Yib481de92007-09-25 17:54:57 -07002325 &tbl_type, &rate_idx);
2326
Ben Cahill77626352007-11-29 11:09:44 +08002327 /* How many times should we repeat the initial rate? */
Zhu Yib481de92007-09-25 17:54:57 -07002328 if (is_legacy(tbl_type.lq_type)) {
2329 ant_toggle_count = 1;
Zhu Yi1b696de2007-09-27 11:27:41 +08002330 repeat_rate = IWL_NUMBER_TRY;
Zhu Yib481de92007-09-25 17:54:57 -07002331 } else
Zhu Yi1b696de2007-09-27 11:27:41 +08002332 repeat_rate = IWL_HT_NUMBER_TRY;
Zhu Yib481de92007-09-25 17:54:57 -07002333
2334 lq_cmd->general_params.mimo_delimiter =
2335 is_mimo(tbl_type.lq_type) ? 1 : 0;
Ben Cahill77626352007-11-29 11:09:44 +08002336
2337 /* Fill 1st table entry (index 0) */
Guy Cohen39e88502008-04-23 17:14:57 -07002338 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
Zhu Yib481de92007-09-25 17:54:57 -07002339
Guy Cohenfde0db32008-04-21 15:42:01 -07002340 /*FIXME:RS*/
2341 if (is_mimo(tbl_type.lq_type) || (tbl_type.ant_type == ANT_A))
Ben Cahill77626352007-11-29 11:09:44 +08002342 lq_cmd->general_params.single_stream_ant_msk
2343 = LINK_QUAL_ANT_A_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002344 else
Ben Cahill77626352007-11-29 11:09:44 +08002345 lq_cmd->general_params.single_stream_ant_msk
2346 = LINK_QUAL_ANT_B_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002347
2348 index++;
Zhu Yi1b696de2007-09-27 11:27:41 +08002349 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002350
Ben Cahill77626352007-11-29 11:09:44 +08002351 /* Fill rest of rate table */
Zhu Yib481de92007-09-25 17:54:57 -07002352 while (index < LINK_QUAL_MAX_RETRY_NUM) {
Ben Cahill77626352007-11-29 11:09:44 +08002353 /* Repeat initial/next rate.
2354 * For legacy IWL_NUMBER_TRY == 1, this loop will not execute.
2355 * For HT IWL_HT_NUMBER_TRY == 3, this executes twice. */
Zhu Yi1b696de2007-09-27 11:27:41 +08002356 while (repeat_rate > 0 && (index < LINK_QUAL_MAX_RETRY_NUM)) {
Zhu Yib481de92007-09-25 17:54:57 -07002357 if (is_legacy(tbl_type.lq_type)) {
2358 if (ant_toggle_count <
2359 NUM_TRY_BEFORE_ANTENNA_TOGGLE)
2360 ant_toggle_count++;
Guy Cohenfde0db32008-04-21 15:42:01 -07002361 else if (priv && rs_is_other_ant_connected(
2362 priv->hw_params.valid_tx_ant,
2363 tbl_type.ant_type)) {
Zhu Yib481de92007-09-25 17:54:57 -07002364 rs_toggle_antenna(&new_rate, &tbl_type);
2365 ant_toggle_count = 1;
2366 }
2367 }
Zhu Yi98d7e092007-09-27 11:27:42 +08002368
Ben Cahill77626352007-11-29 11:09:44 +08002369 /* Override next rate if needed for debug purposes */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002370 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Ben Cahill77626352007-11-29 11:09:44 +08002371
2372 /* Fill next table entry */
Zhu Yib481de92007-09-25 17:54:57 -07002373 lq_cmd->rs_table[index].rate_n_flags =
Guy Cohen39e88502008-04-23 17:14:57 -07002374 cpu_to_le32(new_rate);
Zhu Yi1b696de2007-09-27 11:27:41 +08002375 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002376 index++;
2377 }
2378
Guy Cohen39e88502008-04-23 17:14:57 -07002379 rs_get_tbl_info_from_mcs(new_rate, lq_sta->band, &tbl_type,
Zhu Yib481de92007-09-25 17:54:57 -07002380 &rate_idx);
2381
Ben Cahill77626352007-11-29 11:09:44 +08002382 /* Indicate to uCode which entries might be MIMO.
2383 * If initial rate was MIMO, this will finally end up
2384 * as (IWL_HT_NUMBER_TRY * 2), after 2nd pass, otherwise 0. */
Zhu Yib481de92007-09-25 17:54:57 -07002385 if (is_mimo(tbl_type.lq_type))
2386 lq_cmd->general_params.mimo_delimiter = index;
2387
Ben Cahill77626352007-11-29 11:09:44 +08002388 /* Get next rate */
Guy Cohen39e88502008-04-23 17:14:57 -07002389 new_rate = rs_get_lower_rate(lq_sta, &tbl_type, rate_idx,
2390 use_ht_possible);
Zhu Yib481de92007-09-25 17:54:57 -07002391
Ben Cahill77626352007-11-29 11:09:44 +08002392 /* How many times should we repeat the next rate? */
Zhu Yib481de92007-09-25 17:54:57 -07002393 if (is_legacy(tbl_type.lq_type)) {
2394 if (ant_toggle_count < NUM_TRY_BEFORE_ANTENNA_TOGGLE)
2395 ant_toggle_count++;
Guy Cohenfde0db32008-04-21 15:42:01 -07002396 else if (priv && rs_is_other_ant_connected(
2397 priv->hw_params.valid_tx_ant,
2398 tbl_type.ant_type)) {
Zhu Yib481de92007-09-25 17:54:57 -07002399 rs_toggle_antenna(&new_rate, &tbl_type);
2400 ant_toggle_count = 1;
2401 }
Zhu Yi1b696de2007-09-27 11:27:41 +08002402 repeat_rate = IWL_NUMBER_TRY;
Zhu Yib481de92007-09-25 17:54:57 -07002403 } else
Zhu Yi1b696de2007-09-27 11:27:41 +08002404 repeat_rate = IWL_HT_NUMBER_TRY;
Zhu Yib481de92007-09-25 17:54:57 -07002405
Ben Cahill77626352007-11-29 11:09:44 +08002406 /* Don't allow HT rates after next pass.
2407 * rs_get_lower_rate() will change type to LQ_A or LQ_G. */
Zhu Yib481de92007-09-25 17:54:57 -07002408 use_ht_possible = 0;
2409
Ben Cahill77626352007-11-29 11:09:44 +08002410 /* Override next rate if needed for debug purposes */
Tomas Winklerc33104f2008-01-14 17:46:21 -08002411 rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
Ben Cahill77626352007-11-29 11:09:44 +08002412
2413 /* Fill next table entry */
Guy Cohen39e88502008-04-23 17:14:57 -07002414 lq_cmd->rs_table[index].rate_n_flags = cpu_to_le32(new_rate);
Zhu Yib481de92007-09-25 17:54:57 -07002415
2416 index++;
Zhu Yi1b696de2007-09-27 11:27:41 +08002417 repeat_rate--;
Zhu Yib481de92007-09-25 17:54:57 -07002418 }
2419
Zhu Yib481de92007-09-25 17:54:57 -07002420 lq_cmd->general_params.dual_stream_ant_msk = 3;
2421 lq_cmd->agg_params.agg_dis_start_th = 3;
2422 lq_cmd->agg_params.agg_time_limit = cpu_to_le16(4000);
Zhu Yib481de92007-09-25 17:54:57 -07002423}
2424
2425static void *rs_alloc(struct ieee80211_local *local)
2426{
2427 return local->hw.priv;
2428}
2429/* rate scale requires free function to be implemented */
2430static void rs_free(void *priv_rate)
2431{
2432 return;
2433}
2434
2435static void rs_clear(void *priv_rate)
2436{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002437 struct iwl_priv *priv = (struct iwl_priv *) priv_rate;
Zhu Yib481de92007-09-25 17:54:57 -07002438
2439 IWL_DEBUG_RATE("enter\n");
2440
2441 priv->lq_mngr.lq_ready = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002442
2443 IWL_DEBUG_RATE("leave\n");
2444}
2445
2446static void rs_free_sta(void *priv, void *priv_sta)
2447{
Tomas Winklerc33104f2008-01-14 17:46:21 -08002448 struct iwl4965_lq_sta *lq_sta = priv_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002449
2450 IWL_DEBUG_RATE("enter\n");
Tomas Winklerc33104f2008-01-14 17:46:21 -08002451 kfree(lq_sta);
Zhu Yib481de92007-09-25 17:54:57 -07002452 IWL_DEBUG_RATE("leave\n");
2453}
2454
2455
Zhu Yi93dc6462007-09-27 11:27:37 +08002456#ifdef CONFIG_MAC80211_DEBUGFS
Zhu Yi5ae212c2007-09-27 11:27:38 +08002457static int open_file_generic(struct inode *inode, struct file *file)
2458{
2459 file->private_data = inode->i_private;
2460 return 0;
2461}
Tomas Winklerc33104f2008-01-14 17:46:21 -08002462static void rs_dbgfs_set_mcs(struct iwl4965_lq_sta *lq_sta,
Guy Cohen39e88502008-04-23 17:14:57 -07002463 u32 *rate_n_flags, int index)
Zhu Yi98d7e092007-09-27 11:27:42 +08002464{
Guy Cohen39e88502008-04-23 17:14:57 -07002465 if (lq_sta->dbg_fixed_rate) {
2466 if (index < 12) {
2467 *rate_n_flags = lq_sta->dbg_fixed_rate;
2468 } else {
2469 if (lq_sta->band == IEEE80211_BAND_5GHZ)
2470 *rate_n_flags = 0x800D;
2471 else
2472 *rate_n_flags = 0x820A;
2473 }
Zhu Yi98d7e092007-09-27 11:27:42 +08002474 IWL_DEBUG_RATE("Fixed rate ON\n");
Guy Cohen39e88502008-04-23 17:14:57 -07002475 } else {
2476 IWL_DEBUG_RATE("Fixed rate OFF\n");
Zhu Yi98d7e092007-09-27 11:27:42 +08002477 }
Zhu Yi98d7e092007-09-27 11:27:42 +08002478}
2479
2480static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
2481 const char __user *user_buf, size_t count, loff_t *ppos)
2482{
Tomas Winklerc33104f2008-01-14 17:46:21 -08002483 struct iwl4965_lq_sta *lq_sta = file->private_data;
Zhu Yi98d7e092007-09-27 11:27:42 +08002484 char buf[64];
2485 int buf_size;
2486 u32 parsed_rate;
2487
2488 memset(buf, 0, sizeof(buf));
2489 buf_size = min(count, sizeof(buf) - 1);
2490 if (copy_from_user(buf, user_buf, buf_size))
2491 return -EFAULT;
2492
2493 if (sscanf(buf, "%x", &parsed_rate) == 1)
Guy Cohen39e88502008-04-23 17:14:57 -07002494 lq_sta->dbg_fixed_rate = parsed_rate;
Zhu Yi98d7e092007-09-27 11:27:42 +08002495 else
Guy Cohen39e88502008-04-23 17:14:57 -07002496 lq_sta->dbg_fixed_rate = 0;
Zhu Yi98d7e092007-09-27 11:27:42 +08002497
Guy Cohen39e88502008-04-23 17:14:57 -07002498 lq_sta->active_legacy_rate = 0x0FFF; /* 1 - 54 MBits, includes CCK */
2499 lq_sta->active_siso_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2500 lq_sta->active_mimo2_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
2501 lq_sta->active_mimo3_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
Zhu Yi98d7e092007-09-27 11:27:42 +08002502
2503 IWL_DEBUG_RATE("sta_id %d rate 0x%X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07002504 lq_sta->lq.sta_id, lq_sta->dbg_fixed_rate);
Zhu Yi98d7e092007-09-27 11:27:42 +08002505
Guy Cohen39e88502008-04-23 17:14:57 -07002506 if (lq_sta->dbg_fixed_rate) {
2507 rs_fill_link_cmd(NULL, lq_sta, lq_sta->dbg_fixed_rate,
2508 &lq_sta->lq);
Tomas Winkler66c73db2008-04-15 16:01:40 -07002509 iwl_send_lq_cmd(lq_sta->drv, &lq_sta->lq, CMD_ASYNC);
Zhu Yi98d7e092007-09-27 11:27:42 +08002510 }
2511
2512 return count;
2513}
Zhu Yi0209dc12007-09-27 11:27:43 +08002514
Zhu Yi5ae212c2007-09-27 11:27:38 +08002515static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
2516 char __user *user_buf, size_t count, loff_t *ppos)
2517{
2518 char buff[1024];
2519 int desc = 0;
2520 int i = 0;
2521
Tomas Winklerc33104f2008-01-14 17:46:21 -08002522 struct iwl4965_lq_sta *lq_sta = file->private_data;
Zhu Yi5ae212c2007-09-27 11:27:38 +08002523
Tomas Winklerc33104f2008-01-14 17:46:21 -08002524 desc += sprintf(buff+desc, "sta_id %d\n", lq_sta->lq.sta_id);
Zhu Yi98d7e092007-09-27 11:27:42 +08002525 desc += sprintf(buff+desc, "failed=%d success=%d rate=0%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002526 lq_sta->total_failed, lq_sta->total_success,
Guy Cohen39e88502008-04-23 17:14:57 -07002527 lq_sta->active_legacy_rate);
Zhu Yi98d7e092007-09-27 11:27:42 +08002528 desc += sprintf(buff+desc, "fixed rate 0x%X\n",
Guy Cohen39e88502008-04-23 17:14:57 -07002529 lq_sta->dbg_fixed_rate);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002530 desc += sprintf(buff+desc, "general:"
2531 "flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002532 lq_sta->lq.general_params.flags,
2533 lq_sta->lq.general_params.mimo_delimiter,
2534 lq_sta->lq.general_params.single_stream_ant_msk,
2535 lq_sta->lq.general_params.dual_stream_ant_msk);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002536
2537 desc += sprintf(buff+desc, "agg:"
2538 "time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002539 le16_to_cpu(lq_sta->lq.agg_params.agg_time_limit),
2540 lq_sta->lq.agg_params.agg_dis_start_th,
2541 lq_sta->lq.agg_params.agg_frame_cnt_limit);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002542
2543 desc += sprintf(buff+desc,
2544 "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002545 lq_sta->lq.general_params.start_rate_index[0],
2546 lq_sta->lq.general_params.start_rate_index[1],
2547 lq_sta->lq.general_params.start_rate_index[2],
2548 lq_sta->lq.general_params.start_rate_index[3]);
Zhu Yi5ae212c2007-09-27 11:27:38 +08002549
2550
2551 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
2552 desc += sprintf(buff+desc, " rate[%d] 0x%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002553 i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags));
Zhu Yi5ae212c2007-09-27 11:27:38 +08002554
2555 return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2556}
2557
2558static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
Zhu Yi98d7e092007-09-27 11:27:42 +08002559 .write = rs_sta_dbgfs_scale_table_write,
Zhu Yi5ae212c2007-09-27 11:27:38 +08002560 .read = rs_sta_dbgfs_scale_table_read,
2561 .open = open_file_generic,
2562};
Zhu Yi0209dc12007-09-27 11:27:43 +08002563static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
2564 char __user *user_buf, size_t count, loff_t *ppos)
2565{
2566 char buff[1024];
2567 int desc = 0;
2568 int i, j;
2569
Tomas Winklerc33104f2008-01-14 17:46:21 -08002570 struct iwl4965_lq_sta *lq_sta = file->private_data;
Zhu Yi0209dc12007-09-27 11:27:43 +08002571 for (i = 0; i < LQ_SIZE; i++) {
2572 desc += sprintf(buff+desc, "%s type=%d SGI=%d FAT=%d DUP=%d\n"
2573 "rate=0x%X\n",
Tomas Winklerc33104f2008-01-14 17:46:21 -08002574 lq_sta->active_tbl == i?"*":"x",
2575 lq_sta->lq_info[i].lq_type,
2576 lq_sta->lq_info[i].is_SGI,
2577 lq_sta->lq_info[i].is_fat,
2578 lq_sta->lq_info[i].is_dup,
Guy Cohen39e88502008-04-23 17:14:57 -07002579 lq_sta->lq_info[i].current_rate);
Zhu Yi0209dc12007-09-27 11:27:43 +08002580 for (j = 0; j < IWL_RATE_COUNT; j++) {
2581 desc += sprintf(buff+desc,
Tomas Winklerc33104f2008-01-14 17:46:21 -08002582 "counter=%d success=%d %%=%d\n",
2583 lq_sta->lq_info[i].win[j].counter,
2584 lq_sta->lq_info[i].win[j].success_counter,
2585 lq_sta->lq_info[i].win[j].success_ratio);
Zhu Yi0209dc12007-09-27 11:27:43 +08002586 }
2587 }
2588 return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
2589}
2590
2591static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
2592 .read = rs_sta_dbgfs_stats_table_read,
2593 .open = open_file_generic,
2594};
Zhu Yi5ae212c2007-09-27 11:27:38 +08002595
Zhu Yi93dc6462007-09-27 11:27:37 +08002596static void rs_add_debugfs(void *priv, void *priv_sta,
2597 struct dentry *dir)
2598{
Tomas Winklerc33104f2008-01-14 17:46:21 -08002599 struct iwl4965_lq_sta *lq_sta = priv_sta;
2600 lq_sta->rs_sta_dbgfs_scale_table_file =
Zhu Yi0209dc12007-09-27 11:27:43 +08002601 debugfs_create_file("rate_scale_table", 0600, dir,
Tomas Winklerc33104f2008-01-14 17:46:21 -08002602 lq_sta, &rs_sta_dbgfs_scale_table_ops);
2603 lq_sta->rs_sta_dbgfs_stats_table_file =
Zhu Yi0209dc12007-09-27 11:27:43 +08002604 debugfs_create_file("rate_stats_table", 0600, dir,
Tomas Winklerc33104f2008-01-14 17:46:21 -08002605 lq_sta, &rs_sta_dbgfs_stats_table_ops);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002606#ifdef CONFIG_IWL4965_HT
2607 lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file =
2608 debugfs_create_u8("tx_agg_tid_enable", 0600, dir,
2609 &lq_sta->tx_agg_tid_en);
2610#endif
2611
Zhu Yi93dc6462007-09-27 11:27:37 +08002612}
2613
2614static void rs_remove_debugfs(void *priv, void *priv_sta)
2615{
Tomas Winklerc33104f2008-01-14 17:46:21 -08002616 struct iwl4965_lq_sta *lq_sta = priv_sta;
2617 debugfs_remove(lq_sta->rs_sta_dbgfs_scale_table_file);
2618 debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
Ron Rindjunsky0c11b4d2008-01-28 14:07:26 +02002619#ifdef CONFIG_IWL4965_HT
2620 debugfs_remove(lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file);
2621#endif
Zhu Yi93dc6462007-09-27 11:27:37 +08002622}
2623#endif
2624
Zhu Yib481de92007-09-25 17:54:57 -07002625static struct rate_control_ops rs_ops = {
2626 .module = NULL,
2627 .name = RS_NAME,
2628 .tx_status = rs_tx_status,
2629 .get_rate = rs_get_rate,
2630 .rate_init = rs_rate_init,
2631 .clear = rs_clear,
2632 .alloc = rs_alloc,
2633 .free = rs_free,
2634 .alloc_sta = rs_alloc_sta,
2635 .free_sta = rs_free_sta,
Zhu Yi93dc6462007-09-27 11:27:37 +08002636#ifdef CONFIG_MAC80211_DEBUGFS
2637 .add_sta_debugfs = rs_add_debugfs,
2638 .remove_sta_debugfs = rs_remove_debugfs,
2639#endif
Zhu Yib481de92007-09-25 17:54:57 -07002640};
2641
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002642int iwl4965_fill_rs_info(struct ieee80211_hw *hw, char *buf, u8 sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07002643{
2644 struct ieee80211_local *local = hw_to_local(hw);
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002645 struct iwl_priv *priv = hw->priv;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002646 struct iwl4965_lq_sta *lq_sta;
Zhu Yib481de92007-09-25 17:54:57 -07002647 struct sta_info *sta;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002648 int cnt = 0, i;
Zhu Yib481de92007-09-25 17:54:57 -07002649 u32 samples = 0, success = 0, good = 0;
2650 unsigned long now = jiffies;
2651 u32 max_time = 0;
2652 u8 lq_type, antenna;
2653
Johannes Bergd0709a62008-02-25 16:27:46 +01002654 rcu_read_lock();
2655
Zhu Yib481de92007-09-25 17:54:57 -07002656 sta = sta_info_get(local, priv->stations[sta_id].sta.sta.addr);
2657 if (!sta || !sta->rate_ctrl_priv) {
Johannes Bergd0709a62008-02-25 16:27:46 +01002658 if (sta)
Zhu Yib481de92007-09-25 17:54:57 -07002659 IWL_DEBUG_RATE("leave - no private rate data!\n");
Johannes Bergd0709a62008-02-25 16:27:46 +01002660 else
Zhu Yib481de92007-09-25 17:54:57 -07002661 IWL_DEBUG_RATE("leave - no station!\n");
Johannes Bergd0709a62008-02-25 16:27:46 +01002662 rcu_read_unlock();
Zhu Yib481de92007-09-25 17:54:57 -07002663 return sprintf(buf, "station %d not found\n", sta_id);
2664 }
2665
Tomas Winklerc33104f2008-01-14 17:46:21 -08002666 lq_sta = (void *)sta->rate_ctrl_priv;
Zhu Yib481de92007-09-25 17:54:57 -07002667
Tomas Winklerc33104f2008-01-14 17:46:21 -08002668 lq_type = lq_sta->lq_info[lq_sta->active_tbl].lq_type;
Guy Cohenfde0db32008-04-21 15:42:01 -07002669 antenna = lq_sta->lq_info[lq_sta->active_tbl].ant_type;
Zhu Yib481de92007-09-25 17:54:57 -07002670
2671 if (is_legacy(lq_type))
2672 i = IWL_RATE_54M_INDEX;
2673 else
2674 i = IWL_RATE_60M_INDEX;
2675 while (1) {
2676 u64 mask;
2677 int j;
Tomas Winklerc33104f2008-01-14 17:46:21 -08002678 int active = lq_sta->active_tbl;
Zhu Yib481de92007-09-25 17:54:57 -07002679
Tomas Winklerc33104f2008-01-14 17:46:21 -08002680 cnt +=
2681 sprintf(&buf[cnt], " %2dMbs: ", iwl4965_rates[i].ieee / 2);
Zhu Yib481de92007-09-25 17:54:57 -07002682
2683 mask = (1ULL << (IWL_RATE_MAX_WINDOW - 1));
2684 for (j = 0; j < IWL_RATE_MAX_WINDOW; j++, mask >>= 1)
Tomas Winklerc33104f2008-01-14 17:46:21 -08002685 buf[cnt++] =
2686 (lq_sta->lq_info[active].win[i].data & mask)
Zhu Yib481de92007-09-25 17:54:57 -07002687 ? '1' : '0';
2688
Tomas Winklerc33104f2008-01-14 17:46:21 -08002689 samples += lq_sta->lq_info[active].win[i].counter;
2690 good += lq_sta->lq_info[active].win[i].success_counter;
2691 success += lq_sta->lq_info[active].win[i].success_counter *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002692 iwl4965_rates[i].ieee;
Zhu Yib481de92007-09-25 17:54:57 -07002693
Tomas Winklerc33104f2008-01-14 17:46:21 -08002694 if (lq_sta->lq_info[active].win[i].stamp) {
Zhu Yib481de92007-09-25 17:54:57 -07002695 int delta =
2696 jiffies_to_msecs(now -
Tomas Winklerc33104f2008-01-14 17:46:21 -08002697 lq_sta->lq_info[active].win[i].stamp);
Zhu Yib481de92007-09-25 17:54:57 -07002698
2699 if (delta > max_time)
2700 max_time = delta;
2701
Tomas Winklerc33104f2008-01-14 17:46:21 -08002702 cnt += sprintf(&buf[cnt], "%5dms\n", delta);
Zhu Yib481de92007-09-25 17:54:57 -07002703 } else
Tomas Winklerc33104f2008-01-14 17:46:21 -08002704 buf[cnt++] = '\n';
Zhu Yib481de92007-09-25 17:54:57 -07002705
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002706 j = iwl4965_get_prev_ieee_rate(i);
Zhu Yib481de92007-09-25 17:54:57 -07002707 if (j == i)
2708 break;
2709 i = j;
2710 }
2711
2712 /* Display the average rate of all samples taken.
2713 *
Ben Cahill77626352007-11-29 11:09:44 +08002714 * NOTE: We multiply # of samples by 2 since the IEEE measurement
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002715 * added from iwl4965_rates is actually 2X the rate */
Zhu Yib481de92007-09-25 17:54:57 -07002716 if (samples)
Tomas Winklerc33104f2008-01-14 17:46:21 -08002717 cnt += sprintf(&buf[cnt],
Zhu Yib481de92007-09-25 17:54:57 -07002718 "\nAverage rate is %3d.%02dMbs over last %4dms\n"
2719 "%3d%% success (%d good packets over %d tries)\n",
2720 success / (2 * samples), (success * 5 / samples) % 10,
2721 max_time, good * 100 / samples, good, samples);
2722 else
Tomas Winklerc33104f2008-01-14 17:46:21 -08002723 cnt += sprintf(&buf[cnt], "\nAverage rate: 0Mbs\n");
2724
2725 cnt += sprintf(&buf[cnt], "\nrate scale type %d antenna %d "
Zhu Yib481de92007-09-25 17:54:57 -07002726 "active_search %d rate index %d\n", lq_type, antenna,
Johannes Berg8318d782008-01-24 19:38:38 +01002727 lq_sta->search_better_tbl, sta->last_txrate_idx);
Zhu Yib481de92007-09-25 17:54:57 -07002728
Johannes Bergd0709a62008-02-25 16:27:46 +01002729 rcu_read_unlock();
Tomas Winklerc33104f2008-01-14 17:46:21 -08002730 return cnt;
Zhu Yib481de92007-09-25 17:54:57 -07002731}
2732
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002733void iwl4965_rate_scale_init(struct ieee80211_hw *hw, s32 sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07002734{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002735 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07002736
2737 priv->lq_mngr.lq_ready = 1;
2738}
2739
Reinette Chatre897e1cf2008-03-28 16:21:09 -07002740int iwl4965_rate_control_register(void)
Zhu Yib481de92007-09-25 17:54:57 -07002741{
Reinette Chatre897e1cf2008-03-28 16:21:09 -07002742 return ieee80211_rate_control_register(&rs_ops);
Zhu Yib481de92007-09-25 17:54:57 -07002743}
2744
Reinette Chatre897e1cf2008-03-28 16:21:09 -07002745void iwl4965_rate_control_unregister(void)
Zhu Yib481de92007-09-25 17:54:57 -07002746{
2747 ieee80211_rate_control_unregister(&rs_ops);
2748}
2749